summaryrefslogtreecommitdiff
path: root/lib
AgeCommit message (Collapse)Author
2025-04-30lib: sbi_hart: add Ssstateen extensionRadim Krčmář
We already detect Smstateen, but Ssstateen exists as well and it doesn't have the M-state CSRs. Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Radim Krčmář <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-30lib: sbi_hart: reset mstateen0Radim Krčmář
The current logic clears some bits based on SBI known extensions. Be safe and do not leave enabled anything that SBI doesn't control. This is not a breaking change, because the register must be initialized to 0 by the ISA on reset, but it is better to not depend on it when we don't need to. Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Radim Krčmář <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-30lib: sbi_hart: fix sstateen emulationRadim Krčmář
The Sstateen extension defines 4 sstateen registers, but SBI currently configures the execution environment to throw illegal instruction exception when accessing sstateen1-3. SBI should implement all sstateen registers, so delegate the implementation to hardware by setting the SE bit. Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Radim Krčmář <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-30lib: sbi_hart: reset sstateen and hstateenRadim Krčmář
Not resetting sstateen is a potential security hole, because U might be able to access state that S does not properly context-switch. Similar for hstateen with VS and HS. Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Radim Krčmář <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-30lib: sbi_hart: reset hstatusRadim Krčmář
hstatus.HU must be cleared, because U-mode could otherwise use the HLS/HSV instructions. This would allow U-mode to read physical memory directly if vgatp and vsatp was 0. The remaining fields don't seem like a security vulnerability now, but clearing the whole CSR is not an issue, so do that be safe. Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Radim Krčmář <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-30lib: sbi: use 64 bit csr macrosRadim Krčmář
Switch the most obvious cases to new macros. Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Radim Krčmář <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-24lib: sbi: Set the scratch allocation to alignment to cacheline sizeRaj Vishwanathan
Set the scratch allocation alignment to cacheline size specified by riscv,cbom-block-size in the DTS file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value. Signed-off-by: Raj Vishwanathan <[email protected]> Reviewed-by: Anup Patel <[email protected]> Reviewed-by: Samuel Holland <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-23lib: sbi_domain: Reduce memory usage of per-domain hart contextAlvin Chang
In current implementation, the length of hartindex_to_context_table[] array is fixed as SBI_HARTMASK_MAX_BITS. However, the number of harts supported by the platform might not be SBI_HARTMASK_MAX_BITS and is usually smaller than SBI_HARTMASK_MAX_BITS. This means it is unnecessary to allocate such fixed-length array here. Precisely, current implementation always allocates 1024 bytes for hartindex_to_context_table[128] on RV64 platform. However, a platform supports two harts only needs hartindex_to_context_table[2], which only needs 16 bytes. This commit calculates needed size of hartindex_to_context_table[] according to supported number of harts on the platform when registering per-domain data, so that memory usage of per-domain context data can be reduced. Signed-off-by: Alvin Chang <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-23lib: utils/fdt: Remove fdt_match_node()Samuel Holland
This function has been obsoleted by the fdt_driver library and is no longer used. Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-21lib: sbi_mpxy: Change MPXY state as per-domain dataAlvin Chang
OpenSBI supports multiple supervisor domains run on same platform. When these supervisor domains want to communicate with OpenSBI through MPXY channels, they will allocate MPXY shared memory from their own memory regions. Therefore, the MPXY state data structure must be per-domain and per-hart data structure. This commit registers per-domain MPXY state data in sbi_mpxy_init(). The original MPXY state allocated in scratch region is also removed. We also replace sbi_scratch_thishart_offset_ptr() macro as new sbi_domain_mpxy_state_thishart_ptr() macro which gets MPXY state from per-domain data. Signed-off-by: Alvin Chang <[email protected]> Reviewed-by: Yu-Chien Peter Lin <[email protected]> Reviewed-by: Samuel Holland <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-21lib: sbi: Handle length of extension name string exceed buffer size errorJimmy Ho
print error message and turncat the string when length of extension name string exceed buffer size Signed-off-by: Jimmy Ho <[email protected]> Reviewed-by: Nick Hu <[email protected]> Reviewed-by: Zong Li <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-15lib: sbi: Remove unnecessary SBI_INIT_LIST_HEADXiang W
No need to initialise the nodes to be added to the linked list Signed-off-by: Xiang W <[email protected]> Reviewed-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-15lib: sbi: Fix SHMEM_PHYS_ADDR for RV32Xiang W
Obtaining a 64-bit address under rv32 does not require combining two 32-bit registers because we ignore upper 32-bits on rv32. Signed-off-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-15lib: sbi: Add parameter check in sbi_mpxy_set_shmem()Xiang W
Shared memory needs to be accessed in M-Mode so for now the high address of shared memory can't non-zero. Signed-off-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-14lib: sbi: Allow programmable counters to monitor cycle/instret events for ↵Leo Yu-Chi Liang
Andes PMU Referencing commit 0c304b661965 ("lib: sbi: Allow programmable counters to monitor cycle/instret events") to support this functionality for Andes PMU. Signed-off-by: Leo Yu-Chi Liang <[email protected]> Reviewed-by: Samuel Holland <[email protected]> Reviewed-by: Atish Patra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-14lib: sbi_ipi: Return error for invalid hartidsAndrew Jones
sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid. Signed-off-by: Andrew Jones <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-13lib: sbi: Enable Control Transfer Records (CTR) Ext using xstateen.Rajnesh Kanwal
The Control Transfer Records (CTR) extension provides a method to record a limited branch history in register-accessible internal chip storage. This extension is similar to Arch LBR in x86 and BRBE in ARM. The Extension has been stable and the latest release can be found here https://github.com/riscv/riscv-control-transfer-records/release Signed-off-by: Rajnesh Kanwal <[email protected]> Reviewed-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-13lib: sbi_trap: Add support for vectored interruptsSamuel Holland
When redirecting an exception to S-mode, transform the (v)stvec CSR value as described in the privileged spec to derive the S-mode PC. Since OpenSBI never redirects interrupts, only synchronous exceptions, the only action needed is to mask out the (v)stvec.MODE field. Reported-by: Jan Reinhard <[email protected]> Closes: https://github.com/riscv-software-src/opensbi/issues/391 Signed-off-by: Samuel Holland <[email protected]> Reviwed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-03-28lib: Emit lr and sc instructions based on -march flagsChao-ying Fu
When -march=rv64im_zalrsc_zicsr is used, provide atomic operations and locks using lr and sc instructions only. Signed-off-by: Chao-ying Fu <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-03-27lib: utils/serial: Add PXA UARTs supportJunhui Liu
The PXA variant of the uart8250 adds the UART Unit Enable bit (UUE) that needs to be set to enable the XScale PXA UART. And it is required for some RISC-V SoCs like the Spacemit K1 that implement the PXA UART. This introduces the "intel,xscale-uart" compatible to handle setting the UUE bit. Signed-off-by: Junhui Liu <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-03-27lib: sbi: sse: Add support for SSTATUS.SDTClément Léger
Similarly to what is done for SPELP, handle SSTATUS.SDT upon event injection. In order to mimick an interrupt, set SDT to 1 for injection and save its previous value in interrupted_flags[5:5]. Restore it upon completion. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Andrew Jones <[email protected]>
2025-03-27lib: sbi: sse: Add support for SSTATUS.SPELPClément Léger
As raised during the ARC review, SPELP was not handled during the event injection process. Save it as part of the interrupted flags, clear it before injecting the event and restore it after completion. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Andrew Jones <[email protected]>
2025-03-27lib: sbi: sse: Remove superfluous parenthesis around MSTATUS_* valuesClément Léger
For some reason, there was a pair of useless parenthesis around MSTATUS_* value usage. Remove them. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Andrew Jones <[email protected]>
2025-03-27lib: sbi: sse: Rename STATUS* interrupted flags to SSTATUS*Clément Léger
As raised by Andrew on the kvm-unit-test review, this flags are meant to hold SSTATUS bits in the specification. Rename them to match that. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Andrew Jones <[email protected]>
2025-03-27lib: sbi: sse: Return SBI_EDENIED for read only parameters.Clément Léger
The SSE specification did specified that read only parameters should return SBI_EBADRANGE but was modified recently to return SBI_EDENIED. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Andrew Jones <[email protected]>
2025-03-27lib: sbi: sse: Remove printf from sbi_sse_exit()Clément Léger
This printf is mainly useful for debugging, remove it. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Andrew Jones <[email protected]>
2025-03-27lib: sbi: sse: Update SSE event idsClément Léger
The latest specification added new high priority RAS events and renamed the PMU to PMU_OVERFLOW. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Andrew Jones <[email protected]>
2025-03-26lib: utils: Make sure that hartid and the scratch are alignedRaj Vishwanathan
Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hart-ids' generate hart-count. Signed-off-by: Raj Vishwanathan <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-24lib: sbi: Use sbi_hart_count() and sbi_for_each_hartindex()Samuel Holland
Simplify the code and improve consistency by using the new macros where possible. sbi_hart_count() obsoletes sbi_scratch_last_hartindex(). Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-24lib: sbi_scratch: Add sbi_hart_count() and for_each_hartindex()Samuel Holland
There is currently no helper for iterating through the harts in a system, and code must choose between sbi_scratch_last_hartindex() and sbi_platform_hart_count() for the loop condition. sbi_scratch_last_hartindex() has unusual semantics, leading to the likelihood of off-by-one errors, and sbi_platform_hart_count() is provided by the platform and so may not be properly bounded. Add a new helper which definitively reports the number of harts managed by this OpenSBI instance, i.e. the number of valid hart indexes, and a convenient iterator macro. Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-24lib: sbi_scratch: Optimize hartid and scratch lookupSamuel Holland
The compiler generates much better code for sbi_hartindex_to_hartid() and sbi_hartindex_to_scratch() when using a constant for the bounds check. This works out nicely because the underlying arrays are already a constant size, so the only change needed is to fill the remainder of each array with the appropriate default/out-of-bounds value. The ellipsis in the designated initializer is a GCC extension (also supported by Clang), but avoids runtime initialization of the array. Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-24lib: sbi_scratch: Apply bounds check to platform hart_countSamuel Holland
The internal limit on the number of harts is SBI_HARTMASK_MAX_BITS, as this value determines the size of various bitmaps and arrays (including hartindex_to_hartid_table and hartindex_to_scratch_table). Clamp the value provided by the platform, and drop the extra array element. Update the documentation to indicate that hart_index2id must be sized based on hart_count, and that hart indexes must be contiguous. As of commit 5e90e54a1a53 ("lib: utils:Check that hartid is valid"), there is no restriction on the valid hart ID values. Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-24lib: sbi: Avoid GOT indirection for global symbol referencesSamuel Holland
OpenSBI is compiled with -fPIE, which generally implies dynamic linking. This causes the compiler to generate GOT references for global symbols in order to support runtime symbol interposition. However, OpenSBI does not actually perform dynamic linking, so the GOT indirection just adds unnecessary overhead. The GOT references can be avoided by declaring global symbols with hidden visibility, thus making them local to this dynamic object and non-interposable. GCC/Clang's -fvisibility parameter is insufficient for this purpose when referencing objects from other translation units; either __attribute__((visibility(...)) or the pragma is required. Use the pragma since it is easier to apply to every symbol. Additionally clean up the one GOT reference from inline assembly. With this change, a firmware linked with LLD does not contain either a GOT or a PLT, and a firmware linked with BFD ld contains only a GOT with a single (unreferenced, legacy) _GLOBAL_OFFSET_TABLE_ entry. Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-24Revert "lib: utils/irqchip: Match against more specific compatible strings ↵Samuel Holland
first" This reverts commit 6019259dfbdf9322858b4e7cfc3d1448376e2aa0. Now that fdt_driver_init_by_offset() respects the compatible string fallback priority order, this workaround is no longer necessary. Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-24lib: utils/fdt: Respect compatible string fallback prioritySamuel Holland
When matching drivers to DT nodes, always match all drivers against the first compatible string before considering fallback compatible strings. This ensures the most specific match is always selected, regardless of the order of the drivers or match structures, as long as no compatible string appears in multiple match structures. Fixes: 1ccc52c427f3 ("lib: utils/fdt: Add helpers for generic driver initialization") Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-23lib: sbi: Return SBI_EALREADY error code if SSE event is presentHimanshu Chauhan
Return SBI_EALREADY error code instead of SBI_EINVAL, in case an event is already added to the supported list. Signed-off-by: Himanshu Chauhan <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-23lib: tests: add bitwise operations unit testsDongdong Zhang
Added unit tests for various bitwise operations using SBI unit test framework. Signed-off-by: Dongdong Zhang <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-23lib: sbi: Fix potential garbage data in string copy functionsDongdong Zhang
In the original implementation of `sbi_strcpy` and `sbi_strncpy`, if the destination buffer (`dest`) was longer than the source string (`src`), the functions did not ensure that the remaining bytes in `dest` were properly null-terminated. This could result in garbage data being present in the destination buffer after the copy operation, as the functions only copied characters from `src` without explicitly terminating `dest`. Signed-off-by: Dongdong Zhang <[email protected]> Reviewed-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-03-23lib: add tests for sbi_ecall functionalityAkshay Behl
This patch adds unit tests for verifying the sbi_ecall version, impid handling, and extension registration functions. The tests ensure that the extension registration and unregistration work as expected. Signed-off-by: Akshay Behl <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-02-19lib: sbi: fwft: Return SBI_ERR_DENIED_LOCKED when setting a locked featureClément Léger
Latest modifications to the spec mandates that a set on a lock feature returns SBI_ERR_DENIED_LOCKED. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-02-19lib: utils/irqchip: Match against more specific compatible strings firstAlex Studer
The T-HEAD C90x PLIC has some special quirks, such as the S-mode delegation bit. OpenSBI currently handles this by checking the compatible string in the device tree. However, this matching is done in the order of the fdt_match array. So if a device tree contains both strings, for example: compatible = "thead,c900-plic", "riscv,plic0"; Then OpenSBI will match against the generic "riscv,plic0" string, since that appears first in the fdt_match array. This means it will fail to set the S-mode delegation bit, and Linux will fail to boot. In some cases, it is not possible to change the compatible string to just the T-HEAD PLIC, as older versions of Linux only recognize the RISC-V compatible string. This patch fixes that by moving the RISC-V string to the end, ensuring that the more specific options get matched first. Signed-off-by: Alex Studer <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-02-19lib: utils/fdt: Allocate fdt_pmu_evt_select on the heapSamuel Holland
This reduces .bss size by 8 KiB, and should reduce overall memory usage since most platforms will have significantly fewer than 512 entries in this table. At the same time, it removes the fixed table size limit. Since the table is only used within fdt_pmu.c, instead of updating the extern declaration, make the table local to this file. Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-02-19lib: utils/fdt: Remove redundant PMU property length checksSamuel Holland
If a property value is too small, len will be zero after the division on the next line, so the property will be ignored. This is the same behavior as when the length check fails. Furthermore, the first two length checks were already ineffectual, because each item in those arrays is 12 bytes long, not 8. Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-02-19lib: utils: Constify FDT driver definitionsSamuel Holland
The carray referencing these definitions assumes they are const. Fixes: 6a26726e08e4 ("lib/utils: reset: Add RPMI System Reset driver") Fixes: 13f55f33a1d3 ("lib: utils/suspend: Add RPMI system suspend driver") Fixes: 33ee9b8240fe ("lib: utils/hsm: Add RPMI HSM driver") Fixes: 591a98bdd549 ("lib: utils/cppc: Add RPMI CPPC driver") Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-02-19lib: sbi: sse: fix invalid errors returned for sse_hart_mask/unmask()Clément Léger
When called twice, sse_hart_mask()/sse_hart_unmask() should return SBI_EALREADY_STOPPED/SBI_EALREADY_STARTED. This was currently inverted. Fixes: b919daf49582 ("lib: sbi: Add support to mask/unmask SSE events") Reported-by: Andrew Jones <[email protected]> Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2025-02-19lib: sbi: Fix non-root domain startupAnup Patel
Currently, the sbi_sse_init() in cold boot path is called after sbi_domain_finalize() so boot HART of non-root domains will start before SSE cold boot init which can cause warm boot of such HARTs to crash in sbi_sse_init(). To address the above issue, factor-out the non-root domain startup from sbi_domain_finalize() function as a separate sbi_domain_startup() function which can be called after sbi_sse_init() in cold boot path. Fixes: 93f7d819fdeb ("lib: sbi: sse: allow adding new events") Signed-off-by: Anup Patel <[email protected]>
2025-02-13lib: sbi: Update MPXY framework and SBI extension as per latest specAnup Patel
The latest SBI 3.0 spec defines a new sbi_mpxy_get_shmem_size() function and simplifies sbi_mpxy_set_shmem() function so update the MPXY framework and SBI extension accordingly. Signed-off-by: Anup Patel <[email protected]>
2025-02-13lib: utils: Add MPXY RPMI mailbox driver for System MSI service groupAnup Patel
The supervisor software can directly receive most of the system MSIs except P2A doorbell and MSIs preferred to be handled in M-mode. Add MPXY RPMI mailbox client driver for the System MSI service group. Signed-off-by: Anup Patel <[email protected]>
2025-02-13include: sbi_utils: Update RPMI service group IDs and BASE service groupAnup Patel
The service group ID assignment and some of the BASE services have changes in the latest RPMI specification so let's update the RPMI implementation accordingly. Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Samuel Holland <[email protected]>
2025-02-13lib: utils: Populate MPXY channel attributes from RPMI channel attributesAnup Patel
Use the RPMI mailbox channel attributes to populate MPXY channel attributes instead of hard coding them. Signed-off-by: Anup Patel <[email protected]>