Commit Graph
2304 Commits
Author SHA1 Message Date
wuliwen 332798211e include: mailbox: correct RPMI CPPC request field ordering
Per the RPMI specification, CPPC_PROBE_REG, CPPC_READ_REG, and
CPPC_WRITE_REG require REG_ID in word 0 and HART_ID in word 1.

The current request structures have these fields swapped, causing
incompatibility with spec-compliant RPMI servers.

Swap the fields to match the specification and reorder the
corresponding field assignments in lib for consistency.

Fixes: 591a98bdd5 ("lib: utils/cppc: Add RPMI CPPC driver")
Signed-off-by: wuliwen <liwen.wu@spacemit.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261022020525.1503945-1-liwen.wu@spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-04 12:02:53 +05:30
Nickolai Zeldovich f6abdaf25d lib: sbi_pmu: Do not return uninitialized stack data on FW read errors
sbi_ecall_pmu_handler() passes the uninitialized local temp to
sbi_pmu_ctr_fw_read() and copies it into out->value unconditionally.
sbi_pmu_ctr_fw_read() only writes *cval on success, so every error
return of SBI_EXT_PMU_COUNTER_FW_READ (e.g. a hardware or inactive
counter index) hands an M-mode stack word to the supervisor in a1.
SBI_EXT_PMU_COUNTER_FW_READ_HI does the same with the upper half of
temp on RV32.

Initialize temp to zero so that the error paths return zero in
sbiret.value; the success paths are unchanged.

Fixes: 1664d0efce ("lib: sbi_pmu: Replace sbi_pmu_ctr_read() with sbi_pmu_ctr_fw_read()")
Fixes: 51951d9e9a ("lib: sbi_pmu: Implement sbi_pmu_counter_fw_read_hi")
Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
Assisted-By: Claude Fable 5.1
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261011222115.769450-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-04 11:34:46 +05:30
Nickolai Zeldovich 6e0338f425 lib: sbi: Install the next-stage entry as a direct-mode xtvec
sbi_hart_switch_mode() writes next_addr unmasked into stvec (vstvec,
utvec).  Bits [1:0] of xtvec are the MODE field, so a 2-byte aligned
entry, legal with the C extension, selects a reserved mode: QEMU drops
the write, the Sail model keeps the old mode, and the next stage starts
with a trap vector that is not its entry.

Mask the mode bits and install the entry as a direct-mode vector; mepc
still receives the exact address.

Signed-off-by: Nickolai Zeldovich <nickolai.zeldovich@gmail.com>
Assisted-by: Claude Fable 5.1
Tested-by: Chen Pei <cp0613@linux.alibaba.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261006193619.735580-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-03 18:19:37 +05:30
Nickolai Zeldovich 00b9807b78 firmware: fw_base: add an acquire fence after the _boot_status wait loop
Non-boot harts exit _wait_for_boot_hart on a plain load of _boot_status
and go on to read platform.hart_index2id and other data the boot hart
relocated before publishing.  Nothing orders those loads after the
status load, so under RVWMO a hart can read the unrelocated pointer and
fault through it before mtvec is set.

Pair the boot hart's release fence with a "fence r, rw" after the loop.

Signed-off-by: Nickolai Zeldovich <nickolai.zeldovich@gmail.com>
Assisted-by: Claude Fable 5.1
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261006193609.735547-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-03 18:09:38 +05:30
Nia Su 2c7788e4c3 lib: sbi: allow NULL smrnmi_handlers_init and report init mode at boot
Platforms with fixed or mtvec-based NMI vectors need no vendor
register programming, so calling sbi_panic() when smrnmi_handlers_init
is missing is too strict; an unhandled RNMI already falls back to
sbi_trap_error().

Allow a NULL smrnmi_handlers_init only when the platform has set
SBI_PLATFORM_HAS_FIXED_SMRNMI_VECTOR to indicate it uses a fixed or
mtvec-based vector. This distinguishes platforms that intentionally
have nothing to program from ones that simply forgot to set the
callback, so the latter now fails instead of enabling NMIs and
jumping to an undefined vector on the first RNMI.

Report how Smrnmi was initialized in the boot HART banner: "Platform"
when smrnmi_handlers_init programmed it, "Default" when the flag was
set instead.

Suggested-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Signed-off-by: Nylon Chen <nylon.chen@sifive.com>
Co-developed-by: Nia Su <nia.su@sifive.com>
Signed-off-by: Nia Su <nia.su@sifive.com>
Reviewed-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260915-smrnmi-context-restore-v3-3-e9dd31152515@sifive.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-03 13:15:33 +05:30
Nia Su 894435d28f lib: sbi: report smrnmi_handlers_init failures instead of ignoring them
smrnmi_handlers_init returned void, so a failed callback went
unreported and NMIE was enabled anyway.

Change it to return int and skip enabling NMIE on failure. Also
update the doc comment on rnmi_handler to use SBI_OK instead of
SBI_SUCCESS, so both callbacks consistently reference the correct
firmware-internal success constant rather than the ecall-facing one.

Signed-off-by: Nylon Chen <nylon.chen@sifive.com>
Co-developed-by: Nia Su <nia.su@sifive.com>
Signed-off-by: Nia Su <nia.su@sifive.com>
Reviewed-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260915-smrnmi-context-restore-v3-2-e9dd31152515@sifive.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-03 13:15:33 +05:30
Nia Su 54fad5bdda lib: sbi: extract smrnmi_hart_init() and call on non-retentive resume
On non-retentive suspend resume, the hart never initializes the
Smrnmi CSRs the way hart_detect_features() does at boot, so a hart
waking from non-retentive suspend resumes with NMIs disabled and no
RNMI handler installed.

Extract the setup into smrnmi_hart_init() and call it from both
hart_detect_features() and sbi_hart_reinit().

Fixes: 2d211fe6f9 ("lib: sbi: hart: Detect and enable Smrnmi before trap-based feature detection")
Signed-off-by: Nylon Chen <nylon.chen@sifive.com>
Co-developed-by: Nia Su <nia.su@sifive.com>
Signed-off-by: Nia Su <nia.su@sifive.com>
Reviewed-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260915-smrnmi-context-restore-v3-1-e9dd31152515@sifive.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-03 13:15:33 +05:30
Zhongyi Cheng c9a7d00992 platform: generic/starfive: deny S-mode access to the JH7110 firmware alias
The JH7110 DDR controller folds physical addresses beyond the 8GB DDR
window back into DRAM: PA (X + 0x200000000) resolves to the same cells
as PA X. PMP entries match the presented address, so an aliased S-mode
access to the firmware region bypasses the entry protecting the
firmware and matches the allow-everything root region, letting root
read and write the firmware through the alias.

Add a root domain memory region covering the alias of the firmware
image (fw_start + 0x200000000 .. fw_start + fw_size) with M-mode-only
permissions, in platform early_init() so that it is in place before
sbi_domain_finalize(). The region sorts before the allow-everything
region, so it gets a lower PMP entry index and denies S/U-mode access
to the firmware alias. The rest of the alias window is left untouched.

This follows the same approach as the RZ/Five ILM/DLM protection.

Verified on QEMU riscv64 (root region list and PMP encodings) and on a
VisionFive 2 board: S/U-mode accesses to both the alias (0x240000000)
and the firmware region (0x40000000) fault as expected after this
patch, while normal boot is unaffected.

Reported-by: Zhongyi Cheng <zhongyicheng241@mails.ucas.ac.cn>
Signed-off-by: Zhongyi Cheng <zhongyicheng241@mails.ucas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260928032414.4125686-1-zhongyicheng241@mails.ucas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-02 13:02:06 +05:30
Khem Raj 95ca87fd6b lib: utils/serial: Remove unused baudrate/frequency globals
The sifive-uart and cadence-uart drivers store the input clock
frequency and baudrate into file-static globals during init, but
never read them back; the divisor is computed directly from the
function arguments. Clang 23 adds -Wunused-but-set-global, which is
enabled by -Wall, so the build fails with -Werror:

  sifive-uart.c:34:12: error: variable 'uart_in_freq' set but not used [-Werror,-Wunused-but-set-global]
  sifive-uart.c:35:12: error: variable 'uart_baudrate' set but not used [-Werror,-Wunused-but-set-global]

Drop the dead variables.

Signed-off-by: Khem Raj <raj.khem@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261015203610.835320-1-khem.raj@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-02 12:11:06 +05:30
Michael Estner a7307c3acc lib: tests: add bitmap set and clear tests
Add SBIUnit word-boundary coverage for bitmap_set() and bitmap_clear().

Derive the base bit index from BITS_PER_LONG so that the tested range
crosses the 31/32 boundary on RV32 and the 63/64 boundary on RV64.

Signed-off-by: Michael Estner <michaelestner@web.de>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260928111442.289267-1-michaelestner@web.de
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-02 12:04:23 +05:30
Fengwei Tan 827f8bf26b lib: sbi_pmu: fix mapping reset for already stopped counters
Linux stops a counter before issuing another stop with RESET flag when
deleting an event [1]. Returning early on SBI_EALREADY_STOPPED skips
counter-to-event mapping reset, eventually exhausting the hardware
counters.

Continue the stop and reset process for the remaining counters when a
counter in the set is already stopped. Preserve SBI_EALREADY_STOPPED in
the final result, while still returning immediately on other errors.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/perf/riscv_pmu.c?id=72d3fcf802c45d00b300f25b848a93c3a2bd7c7e#n318

Fixes: c5077d497d ("lib: sbi_pmu: Fix counter and event info error codes as per SBI v3.0 spec")
Signed-off-by: Fengwei Tan <tfx2001@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/SY8P300MB00410B44E7D55CB5CF7B276AB5882@SY8P300MB0041.AUSP300.PROD.OUTLOOK.COM
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-02 12:02:17 +05:30
Sebastian Alba Vives 7a7a60dc87 lib: sbi_pmu: reject out-of-bounds counter index in pmu_ctr_write_hw()
pmu_ctr_write_hw() writes CSR_MCYCLE + cidx with no check on cidx,
while its sibling pmu_ctr_start_hw() guards the same index space:

	if (cidx >= num_hw_ctrs || cidx == 1)
		return SBI_EINVAL;

A cidx at or above num_hw_ctrs addresses an mhpmcounter the platform
need not implement, and cidx == 1 addresses 0xb01, which is not
assigned. Either one traps in M-mode instead of returning an error.

Give the function the same guard and a return value. The guard is
copied from pmu_ctr_start_hw() rather than limited to the upper
bound, so that the two functions accept exactly the same index space.

Only the call site in sbi_pmu_ctr_cfg_match() propagates the error.
The two call sites inside pmu_ctr_start_hw() run after the identical
guard in that function, so they cannot fail and are left unchanged.

Fixes: 13d40f21d5 ("lib: sbi: Add PMU support")
Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261025145338.2569358-3-sebasjosue84@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-29 13:49:28 +05:30
Sebastian Alba Vives 1e418fc818 lib: sbi_pmu: do not ignore pmu_ctr_start_hw() error in cfg_match
sbi_pmu_ctr_cfg_match() calls pmu_ctr_start_hw() for the AUTO_START
flag and drops the result. pmu_ctr_start_hw() carries its own index
guard

	if (cidx >= num_hw_ctrs || cidx == 1)
		return SBI_EINVAL;

and can also return SBI_EALREADY_STARTED when the counter is already
running. Both are reported to the caller as a successful configure.

Neither is benign. A rejected index means nothing was configured, and
an already running counter may have been configured for a different
event previously, so the supervisor must not be told its configure
succeeded.

This is also the only one of the three start paths in the file that
discards the error. sbi_pmu_ctr_start() propagates it:

	ret = pmu_ctr_start_hw(cidx, ival, bUpdate);
	if (ret)
		return ret;

and the firmware branch directly below this call site propagates the
error from pmu_dev->fw_counter_start() as well.

Fixes: 13d40f21d5 ("lib: sbi: Add PMU support")
Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261025145338.2569358-2-sebasjosue84@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-29 13:49:28 +05:30
Valentin Haudiquet 90ec61b228 platform: generic: spacemit: k3: override cold_boot_allowed for hart 0 only
Only hart 0 is powered at reset on K3; secondary harts are started later
through HSM. Restrict cold-boot eligibility to hart 0 instead of using
the generic DT-configured hart mask, matching K1.

Signed-off-by: Valentin Haudiquet <valentin.haudiquet@canonical.com>
Reviewed-by: E Shattow <e@freeshell.de>
Signed-off-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Tested-by: E Shattow <e@freeshell.de>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261028-spacemit-k3-v6-6-01d984f60941@linux.spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-29 11:24:08 +05:30
Xianbin Zhu cca121056f platform: generic: spacemit: k3: add platform support
SpacemiT K3 has 16 harts split across four clusters, with eight X100
harts in C0/C1 and eight A100 harts in C2/C3. Only hart 0 is powered at
reset, so secondary harts cannot reach the OpenSBI warm-boot path until
the platform removes their PMU power-down votes and wakes them.

Add a start-only HSM backend which performs those operations for the
requested hart. This lets the common HSM state machine start secondary
harts on demand instead of waking every hart during cold boot.
Select the HSM backend from the root compatible, preserving K1's
existing start and stop behavior.

The Boot ROM maps the audio buffer region as I/O although later firmware
uses it as normal memory. Restore its cacheable PMA attribute and mark
the XIP region as I/O before enabling caches. The boot hart performs the
per-hart setup during early platform discovery, while secondary harts do
the same in their assembly entry before reaching common warm-boot code.
This also enables MISA.H on X100 before common code selects its trap
entry; A100 does not implement H. Fence the PMA and guest translation
changes before continuing.

Use assembler-safe constants shared by the C and warm-entry assembly
paths. Since K1 and K3 are RV64-only, omit the SpacemiT platform objects
from RV32 generic builds.

Program the four cluster warm-boot vectors once and enable CCI-550 snoop
and DVM requests before any secondary hart is started.

Signed-off-by: Xianbin Zhu <xianbin.zhu@linux.spacemit.com>
Tested-by: E Shattow <e@freeshell.de>
Co-developed-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Signed-off-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261028-spacemit-k3-v6-5-01d984f60941@linux.spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-29 11:24:05 +05:30
Xianbin Zhu b223774f20 platform: generic: spacemit: k1: refactor platform support
K3 shares cache-control CSRs, PMU idle fields, cluster sizing, and the
CCI-550 programming sequence with K1. Keeping these definitions and the
CCI helper in K1-specific files would either duplicate them or make K3
depend on K1 support.

Add a hidden PLATFORM_SPACEMIT option and move the shared definitions to
spacemit/common.h. Move cci_enable_snoop_dvm_reqs() to spacemit.c while
keeping each SoC's CCI topology and boot flow in its own platform driver.

Rename the PMU idle fields to match the K1 and K3 User Manuals. Keep the
K1 power-down mask at 0x1b, so K1 power-management behavior remains
unchanged. Keep the K1 HSM implementation K1-specific and switch it to
the shared definitions.

This is a non-functional refactoring that prepares the common support
used by K3.

Signed-off-by: Xianbin Zhu <xianbin.zhu@linux.spacemit.com>
Tested-by: E Shattow <e@freeshell.de>
Co-developed-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Signed-off-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261028-spacemit-k3-v6-4-01d984f60941@linux.spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-29 11:24:04 +05:30
Troy Mitchell acc2508c47 platform: generic: spacemit: k1: move hart init to nascent hook
cold_boot_allowed() is a policy query and should not modify per-hart
state. The K1 callback currently programs ML2SETUP as a side effect.

Move the ML2SETUP programming to nascent_init(), which runs on every
hart before common initialization. Chain generic_nascent_init() to keep
the generic per-hart setup intact.

Fixes: 1f84ec2ac2 ("platform: generic: spacemit: add K1")
Reported-by: Bo Gan <ganboing@gmail.com>
Link: https://lore.kernel.org/r/56220293-88e3-451f-833e-8251656a83d9@gmail.com
Reviewed-by: Bo Gan <ganboing@gmail.com>
Signed-off-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Tested-by: E Shattow <e@freeshell.de>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261028-spacemit-k3-v6-3-01d984f60941@linux.spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-29 11:24:02 +05:30
Xianbin Zhu 2c4172cd3c platform: generic: spacemit: k1: rename cache flush operation
MRAOP_ICACHE_INVALID is used to flush the local D-cache before a hart
stops, so the name incorrectly describes I-cache invalidation. Rename
it to MRAOP_CACHE_FLUSH without changing its encoded value.

Signed-off-by: Xianbin Zhu <xianbin.zhu@linux.spacemit.com>
Reviewed-by: E Shattow <e@freeshell.de>
Co-developed-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Signed-off-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Tested-by: E Shattow <e@freeshell.de>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261028-spacemit-k3-v6-2-01d984f60941@linux.spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-29 11:24:00 +05:30
Troy Mitchell 5dd2a0619e lib: sbi: select expected trap handler per hart
The expected trap handler is selected once by the cold boot hart. This
breaks heterogeneous systems where the cold boot hart implements H but
another hart does not, because the H-aware handler accesses mtval2 and
mtinst.

Store the handler in each hart's feature data and select it from that
hart's MISA before feature detection. Load the address without function
calls, avoiding stack-protector instrumentation in the CSR probing and
unprivileged memory access paths.

Early M-mode CSR probes, such as those in the IMSIC nascent hook, can run
before hart feature data is ready. Use the non-H handler until the current
hart's handler has been initialized, and publish the feature-data offset
only after every hart's storage has been cleared. This leaves the fixed
scratch layout and allocator unchanged.

Fixes: 1de66d170e ("lib: Optimize unpriv load/store implementation")
Reported-by: Bo Gan <ganboing@gmail.com>
Link: https://lore.kernel.org/r/e702f291-dde8-4b99-a65e-182d2b847720@gmail.com
Suggested-by: Bo Gan <ganboing@gmail.com>
Cc: Alvin Chang <alvinga@andestech.com>
Link: https://lore.kernel.org/r/20260831022156.2060561-1-alvinga@andestech.com
Signed-off-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Tested-by: E Shattow <e@freeshell.de>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261028-spacemit-k3-v6-1-01d984f60941@linux.spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-29 11:22:51 +05:30
Fengwei Tan 2bbee7d82a lib: sbi_pmu: Fix fixed-counter events reported as unsupported
OpenSBI assigns SBI_PMU_HW_CPU_CYCLES and SBI_PMU_HW_INSTRUCTIONS to
fixed counters during PMU initialization. sbi_pmu_ctr_cfg_match()
recognizes these events without searching hw_event_map[].

However, sbi_pmu_event_get_info() only checks hw_event_map[] when
determining whether a hardware event is supported. As a result, these
fixed-counter events are reported as unsupported when no riscv,pmu
device tree node is provided.

Check fixed-counter events separately using pmu_ctr_find_fixed_hw() and
report them as supported.

Fixes: e434584216 ("lib: sbi_pmu: Implement SBI PMU event info function")
Signed-off-by: Fengwei Tan <tfx2001@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/SY8P300MB0041D04649621E79FE950B43B58E2@SY8P300MB0041.AUSP300.PROD.OUTLOOK.COM
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-29 11:06:47 +05:30
Evgenii Prokopiev d28bc6f898 lib: sbi_hart: Retrun zero length of mhpmcounter when probing failed
If writing access is not allowed, it will raise a trap, but we increase
the number of allowed bits to 32 in this approach, which isn't right.
There must be a write of 0.

Move the operation of increasing allowed bits into the 'if' condition.

Signed-off-by: Evgenii Prokopiev <eestelle0626@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260910182026.6776-1-eestelle0626@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-27 17:28:45 +05:30
Evgenii Prokopiev 3a51dc070e lib: sbi_hart: Reset mhpmcounter after probing its bits
Writting mhpmcounter3 (and 3H) to determine the allowed bit width
has a side effect: the counter starts counting.

Explicitly clear the counter right after the probe so the probing
writes do not leave any persistent hardware side effect.

Signed-off-by: Evgenii Prokopiev <eestelle0626@gmail.com>
Link: https://lore.kernel.org/r/20260910180217.3256-1-eestelle0626@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
2026-10-27 13:58:47 +05:30
Indrek Kruusa f5622b4bb4 platform: generic: allwinner: Support the D1s as well
The Allwinner D1s SoC is a stripped-down version of the D1 and D1s
boards use "allwinner,sun20i-d1s" as their root compatible string
instead of "allwinner,sun20i-d1". So, currently the sun20i override is
not selected on them.

Add the D1s compatible to the match table.

Tested on the MangoPi MQ board with the Allwinner F133-A (D1s) SoC.

Signed-off-by: Indrek Kruusa <indrek.kruusa@gmail.com>
Link: https://lore.kernel.org/r/20260921182017.999610-1-indrek.kruusa@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-27 13:46:09 +05:30
Maxim Kochetkov 113d719547 Makefile: Speed up parallel build
Parallel build is extremely slow when using vars (O, I,
PLATFORM_DIR+PLATFORM). And all this is because the variables are
calculated continuously, with the simultaneous invocation of
the $(shell ...). So use "Simply expanded variables" to
calculate value and call $(shell ...) just once.

Signed-off-by: Maxim Kochetkov <fido_max@inbox.ru>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260924060133.294142-1-fido_max@inbox.ru
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-27 13:34:29 +05:30
Nia Su a0531af7ad lib: sbi: Fix stale prev_context in RNMI handler
An earlier SBI trap may have already used the same M-mode exception
stack slot to save its trap context before an RNMI is taken.
sbi_trap_rnmi_handler() never initializes prev_context before
sbi_trap_error() walks it to print trap diagnostics, so it can read
whatever stale value happens to be left in that slot.

Link prev_context on entry and restore it on exit, the same way
sbi_trap_handler() does. This also lets an exception taken while
already inside RNMI handling chain back to the RNMI's own trap
context instead of dropping it, so sbi_trap_error() prints every
nested context correctly, including the RNMI's.

Fixes: 00fec20b49 ("firmware: Add RNMI handler infrastructure")
Suggested-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Nia Su <nia.su@sifive.com>
Reviewed-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260914-trap-context-fix-v2-2-3a6bedc6fcf0@sifive.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-27 13:26:22 +05:30
Nia Su f84bdb9877 firmware: Clear scratch->trap_context on warm boot
sbi_trap_handler() links tcntx->prev_context on entry and restores
scratch->trap_context on exit, forming a stack that unwinds through
the same path it was pushed from.

Non-retentive suspend/resume breaks that unwind: the SBI call
requesting suspend jumps straight to warm boot (jump_warmboot())
instead of returning through the normal exit path, leaving
scratch->trap_context stale. Since each hart's scratch space sits at
a fixed address, the next trap taken after warm boot resume reads
that same stale scratch->trap_context as its prev_context, which
sbi_trap_error() then walks to print trap diagnostics -- producing a
broken trap dump.

Clear trap_context in the scratch space on warm boot entry.

Fixes: 60ffc154c8 ("include: sbi: Add trap_context pointer in struct sbi_scratch")
Signed-off-by: Nia Su <nia.su@sifive.com>
Reviewed-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260914-trap-context-fix-v2-1-3a6bedc6fcf0@sifive.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-27 13:26:22 +05:30
liutong 2afa777592 lib: sbi_mpxy: fix integer overflow in attribute range endpoint
sbi_mpxy_read_attrs() and sbi_mpxy_write_attrs() compute the end of the
requested range as a u32:

  end_id = base_attr_id + attr_count - 1;

Message protocol attribute ids extend to
SBI_MPXY_ATTR_MSGPROTO_ATTR_END (0xffffffff), so the guard
"end_id > SBI_MPXY_ATTR_MSGPROTO_ATTR_END" cannot hold for a u32 and
never rejects anything.

Such a request then reaches the message protocol driver, which computes
the same endpoint in a u32 and so passes its own range check as well,
and indexes its attribute array with attr_id2index(base_attr_id). For
base_attr_id 0xffffffff that is index 0x7fffffff, an out-of-bounds read
whose result is copied into the shared memory.

Widen end_id to u64 so the endpoint is exact and such a request is
rejected before a driver runs. The standard attribute path is not
affected, since attr_count is already limited to the shared memory size
there.

Fixes: 7939bf1329 ("lib: sbi: Add SBI Message Proxy (MPXY) framework")
Signed-off-by: liutong <liutong@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260910052629.3359493-5-liutong@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-26 11:43:21 +05:30
liutong f45b9433ed lib: sbi_pmu: fix integer overflow and zero-address in event_get_info
sbi_pmu_event_get_info() computes the shared memory size as
num_events * sizeof(struct sbi_pmu_event_info) before num_events is
validated. The product can wrap, in which case the domain check runs
against a truncated size while the loop below still iterates over the
original count, that is, past the region that was checked.

Check num_events against the largest value that cannot wrap, and reject
a zero address, both before the multiplication.

Fixes: e434584216 ("lib: sbi_pmu: Implement SBI PMU event info function")
Signed-off-by: liutong <liutong@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260910052629.3359493-3-liutong@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-26 10:28:28 +05:30
liutong a14e4d24a1 lib: sbi: dbtr: fix integer overflow in read_trig bounds check
In sbi_dbtr_read_trig(), the range check is:

  if (trig_idx_base + trig_count >= hs->total_trigs)

When trig_idx_base and trig_count are both unsigned long values supplied
by S-mode, their sum can wrap past ULONG_MAX to a small value, making
the check pass. For example trig_idx_base=1, trig_count=ULONG_MAX wraps
to 0, which is less than total_trigs.

This allows the subsequent for_each_trig_entry loop to access trigger
entries far beyond the triggers[] array, corrupting M-mode heap memory
via CSR read-back writes and leaking M-mode internal state to S-mode
shared memory.

Rewrite the condition as trig_count >= total_trigs - trig_idx_base. The
subtraction is safe because the preceding check already guarantees
trig_idx_base < total_trigs.

Fixes: 97f234f15c ("lib: sbi: Introduce the SBI debug triggers extension support")
Signed-off-by: liutong <liutong@iscas.ac.cn>
Reviewed-by: Himanshu Chauhan <himanshu.chauhan@oss.qualcomm.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260910052629.3359493-2-liutong@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-26 10:27:20 +05:30
Nickolai Zeldovich bcf587a987 lib: sbi_timer: Insert timer event before the later event
The per-HART event list is kept sorted by expiry time: the head is the
event programmed into the timer device by __sbi_timer_update_device(),
and sbi_timer_process() stops at the first unexpired entry.

__sbi_timer_event_start() finds the first event that expires after the
new one, but then inserts the new event with sbi_list_add(), i.e. after
that event instead of before it.  Starting an event at time 50 while an
event at time 1000 is pending yields the list [1000, 50]: the device is
programmed for 1000 and the earlier event fires 950 ticks late.

Today each HART only ever has its S-mode timer event on the list, so
the misordering needs a second timer event user to show up.  Once there
is one, on HARTs without Sstc an S-mode timer programmed while a later
M-mode event is pending is delivered only when that later event
expires.

Use sbi_list_add_tail() so the new event goes in front of next_ev.

Fixes: b7fa8a246c ("lib: sbi_timer: Add support for timer events")
Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
Assisted-By: Claude Fable 5.1
Reviewe-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261011222412.769530-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 15:05:29 +05:30
Anup Patel 8c04100096 lib: utils/mpxy: Use domain notifier for deferred creation of MPXY RPMI
The owner domain may not be available at the time of creating MPXY RPMI
instance so use domain notifier for deferred creation.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-12-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:32:38 +05:30
Oza Pawandeep f4ed5aa8b7 lib: sbi_domain: Add domain notifier infrastructure
Introduce a notifier callback mechanism that allows subsystems to
register interest in domain events (such as registration). Whenever
such events happen for a domain, all registered notifiers are invoked.

The following public interfaces are added to sbi_domain.h:

  - sbi_domain_register_notifier(): registers a notifier callback.
    Duplicate (fn, event, priv) pairs are silently ignored.

  - sbi_domain_unregister_notifier(): removes a previously registered
    notifier callback.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Signed-off-by: Oza Pawandeep <pawandeep.oza@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-11-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel f909590830 lib: utils/mpxy: Add domain parameter to setup_group() callback
The MPXY channels can be domain specific so the setup_group() callback
must know which domain the MPXY RPMI instance is associated with.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-10-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel c4f098bfa3 lib: sbi_domain: Allow adding memrange to a particular domain from drivers
Currently, memregions of non-root domain only populated at domain creation
time which not flexible for drivers which create domain specific resources
(such as MPXY channels). Add new sbi_domain_add_memrange() function which
allows adding memrange to any domain at boot time.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-9-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel 145bd4ccc0 lib: sbi_domain: Make domain regions as fixed array
Instead of separately allocating domain regions, let us have a fixed
size array so that regions are automatically allocated along with
domain allocation and one SBI_DOMAIN_MEMREGION_MAX macro defines
maximum number of regions for all domains.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-8-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel 89b3afb16f lib: utils/mpxy: Simplify fdt_mpxy_rpmi.h by moving things to fdt_mpxy_rpmi.c
Currently, the fdt_mpxy_rpmi.h has lot things which are not used by
any of MPXY RPMI drivers so simplify fdt_mpxy_rpmi.h by moving such
things to fdt_mpxy_rpmi.c.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-7-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel dc8ceae9d0 lib: utils/mpxy: Introduce mpxy_rpmi_no_mbox_init() function
Add new mpxy_rpmi_no_mbox_init() function to instantiate MPXY RPMI
channel without any mailbox channel backing. This new function can
be used by MPXY RPMI drivers that want to emulate MPXY channels in
software.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-6-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel bdd7874991 lib: sbi_domain: Introduce sbi_domain_find_by_name() function
Add sbi_domain_find_by_name() to allow driver locate domain instance
using domain name. This also means domain names must be unique.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-5-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel db877c0908 lib: utils/mpxy: Introduce get_attribute_group() for MPXY RPMI data
Fully emulated MPXY channels need a custom way to provide RPMI attributes
so introduce optional get_attribute_group() callback in MPXY RPMI data
for this purpose.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-4-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel fa2bbaf349 lib: utils/mpxy: Setup group early in fdt_mpxy_rpmi_init()
Instead of setting group late in fdt_mpxy_rpmi_init(), it is better
to do it as early as possible right after requesting mailbox channel
so that we define new callback for channel attributes.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-3-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel 0298a2c343 lib: utils/mpxy: Rename fdt_mpxy_rpmi_mbox to fdt_mpxy_rpmi
The common fdt_mpxy_rpmi_mbox driver is being upgraded to support emulated
MPXY channels hence drop term "mbox" from fdt_mpxy_rpmi_mbox everywhere.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-2-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Prajwal N fde5dfe96f include: sbi_utils/mailbox/rpmi_msgprot: Fix warm reboot failure
When sbi_system_reset() is invoked with SBI_SRST_RESET_TYPE_WARM_REBOOT,
the RPMI system reset driver reports warm reboot as unsupported.

The driver was checking the wrong bit in the FLAGS field returned
by rpmi_sysreset_get_attributes().

To fix this, check the correct bit to detect warm reboot support.

Fixes: 6a26726e08 (lib/utils: reset: Add RPMI System Reset driver)
Signed-off-by: Prajwal N <prajwal.n@oss.qualcomm.com>
Reviewed-by: Rahul Pathak <rahul.pathak@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261001084412.1383854-1-prajwal.n@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:22:06 +05:30
Prajwal N 97bf679708 lib: utils/irqchip/imsic: Fix the MSI capability assignment
The struct sbi_irqchip_device is copied to the struct imsic_data
and the MSI capability is set in sbi_irqchip_device. Since the
struct assignment is not a pointer, the MSI capability setting
does not take effect.

Set the MSI capability directly in the struct imsic_data to
ensure it takes effect.

Fixes: 193b1d9e7e ("lib: utils/irqchip/imsic:migrate to irqchip framework")
Signed-off-by: Prajwal N <prajwal.n@oss.qualcomm.com>
Reviewed-by: Rahul Pathak <rahul.pathak@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261001060820.1267933-1-prajwal.n@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:19:19 +05:30
Sebastian Alba Vives 0ce50ae8fa lib: sbi_pmu: unmap shared memory on the early exit in event_get_info
sbi_pmu_event_get_info() maps the caller's shared memory into M-mode with
sbi_hart_protection_temp_map_range(). The must-be-zero check added inside the
event loop returns straight to the supervisor without reaching the matching
temp_unmap_range() at the end of the function, so the mapping is left behind.

This has a security impact beyond leaking a mapping. Under Smepmp the
temporary mapping is a single reserved PMP entry, SBI_SMEPMP_RESV_ENTRY.
sbi_hart_smepmp_temp_map_range() refuses to reprogram that entry while it is
already enabled:

	if (sbi_hart_is_pmp_enabled(SBI_SMEPMP_RESV_ENTRY))
		return SBI_ENOSPC;

and no caller in the tree checks that return value. sbi_pmu.c, sbi_sse.c,
sbi_mpxy.c, sbi_dbtr.c and sbi_ecall_dbcn.c all discard it. So once the entry
is left behind, every later shared memory ecall on that hart runs with PMP 0
still covering the previous region, and M-mode faults the first time it
dereferences the new buffer.

A supervisor triggers this with one ecall carrying a reserved bit in the first
event_idx. The next SBI_EXT_DBCN_CONSOLE_WRITE then takes an M-mode load
access fault on the console buffer and the firmware stops making forward
progress, taking DBCN, SSE, MPXY and DBTR down with it on that hart. It also
breaks the Smepmp invariant that M-mode does not retain standing access to
supervisor memory.

Confirmed on qemu virt with a bare metal S-mode payload, QEMU 8.2.2:

  build                 -cpu                protection   result
  master 6ad246a1       rv64                pmp          completes
  master 6ad246a1       rv64,smepmp=true    epmp         firmware dies
  master + this patch   rv64,smepmp=true    epmp         completes

The middle row leaves this as the last entry in the QEMU exception trace,
where the epc is inside nputs() in M-mode and the tval is the supervisor
console buffer:

  cause:0000000000000005, epc:0x000000008000204c,
  tval:0x0000000080500000, desc=fault_load

Without Smepmp the map and unmap pair compiles down to nothing, which is why
the first row survives.

This only affects master. v1.9 and earlier have the same map and unmap pair
but no return between them, so no released version needs a backport.

Route the early exit through the existing unmap.

Fixes: d97cfb33ae ("lib: sbi_pmu: Return invalid param error for reserved event_idx bits")
Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261012022033.1165060-1-sebasjosue84@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:07:17 +05:30
Sebastian Alba Vives 663786edae lib: sbi_trap_v_ldst: fix mask buffer overflow on VLEN > 128
mask_len counts mask bits: it is derived from vlenb * 8, and the mask
indexing below divides by 8 to turn a bit index into a byte index.
get_vreg() counts bytes: its size argument feeds "vsetvli x0, %0, e8,
m8" and a vse8.v, both of which operate on 8-bit elements.

Both emulators pass mask_len directly as get_vreg()'s size, so the
firmware writes eight times more than intended into mask[], a
MASK_BUFFLEN / 8 == 128 byte stack buffer. The write stays in bounds
only while vlenb * 8 <= 128, that is VLEN <= 128. Above that it runs
past the end of the buffer and corrupts the M-mode stack, including the
saved return address.

The pre-rework code passed vlenb, a byte count, which was correct; the
chunked version replaced it with a bit count.

Reachable from S-mode or U-mode with a single masked, misaligned vector
load or store on a hart whose VLEN exceeds 128 bits. Observed on
qemu-system-riscv64 with -cpu rv64,v=true,vlen=256,zicclsm=false: the
firmware takes an instruction access fault in M-mode with mepc and ra
both zero. The same payload at vlen=128 enters the emulator and
completes normally.

Convert pos and size to bytes at both call sites. mask_len is a power of
two and at least 8, and pos is already a multiple of mask_len, so both
divisions are exact.

Fixes: a8be5e94 ("lib: sbi: Rework misaligned vector load/store")
Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260922030633.10886-1-sebasjosue84@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:01:44 +05:30
Jakub Janeczko 16fa280b69 lib: sbi_pmu: Fix race on pmu_ctr_write_hw
On platforms where a bit of mcountinhibit cannot be set to 1,
and when on rv32, pmu_ctr_write_hw() was given value whose lower
bits were close to 2^32, the write could miss the lower bit overflow.

Signed-off-by: Jakub Janeczko <kuba@ev1.pl>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261024195739.585621-1-kuba@ev1.pl
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 13:42:21 +05:30
Pengpeng Hou 9f37e25f80 lib: sbi: reject PMU skip-match counter class mismatches
SKIP_MATCH selects the first counter in the caller-provided set, but the
current path checks only that the index exists and is active.  A supervisor
can select an active hardware counter for a firmware event.  CLEAR_VALUE or
AUTO_START then indexes firmware-counter state with
ctr_idx - num_hw_ctrs, which underflows.  The inverse mismatch sends a
firmware-counter index to hardware-counter operations.

Reject the selected counter when its hardware/firmware class cannot monitor
the requested event.  Return SBI_ENOTSUPP, matching the PMU specification's
case where the selected counter cannot monitor that event.

Fixes: 13d40f21d5 ("lib: sbi: Add PMU support")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Tested-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260827115718.50124-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 11:20:03 +05:30
Pengpeng Hou 131bda9447 lib: sbi_hsm: Validate state before updating start context
sbi_hsm_hart_start() acquires the target hart's start ticket, then
writes next_arg1, next_addr and next_mode before checking whether the
target is stopped.

A start request for a non-retentively suspended hart is rejected, but
those writes have already replaced the resume context consumed by
sbi_hsm_hart_resume_finish(). The same ordering also lets other rejected
start requests modify target scratch state.

Check the state immediately after acquiring the start ticket and before
updating the scratch context. The ticket serializes start requests for
the target; retain the cmpxchg as the publication barrier and defensive
state check.

Fixes: e8e9ed3790 ("lib: sbi: Set the state of a hart to START_PENDING after the hart is ready")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260831130817.54625-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-23 17:27:48 +05:30
Pengpeng Hou 5930665a17 lib: sbi_timer: Restart events from the restart list
Timer callbacks can request that an expired event be restarted. Such
events are moved to a local restart_list, but the restart loop tests that
list while reading its first entry from tstate->event_list.

If the main event list is empty, the loop treats its list head as a timer
event. Otherwise it repeatedly removes and reinserts the wrong event
while restart_list remains nonempty, so timer processing cannot
complete.

No in-tree callback currently requests a restart, but the timer-event
callback interface exposes this path to OpenSBI library and platform
users.

Read the event from restart_list, matching the loop condition and the
list that owns the entry.

Fixes: b7fa8a246c ("lib: sbi_timer: Add support for timer events")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260831130641.54100-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-23 13:55:43 +05:30
Pengpeng Hou 9bc841226d lib: utils/mailbox: Reject invalid RPMI queue indices
The RPMI shared-memory helpers validate a queue index before using it
to select queue_ctx_tbl, but the current comparison accepts queue_id
equal to queue_count.

Only entries 0 through queue_count - 1 are initialized from the device
tree. Accepting queue_count can therefore use an uninitialized queue
context when a protocol operation selects a queue omitted by the
description.

Reject queue_id greater than or equal to queue_count in both transfer
directions.

Fixes: 91f46fb47e ("lib/utils: Add RPMI messaging protocol and shared memory transport support")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260831130444.53629-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-23 13:18:45 +05:30