summaryrefslogtreecommitdiff
path: root/lib/sbi
AgeCommit message (Collapse)AuthorFilesLines
2021-08-07lib: sbi: Fix bug in strncmp function when count is 0Dong Du1-0/+4
No need to compare characters when the count turns to 0. Fix the issue in sbi_strncmp. Signed-off-by: Dong Du <Dd_nirvana@sjtu.edu.cn> Reviewed-by: Bin Meng <bmeng.cn@gmail.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2021-07-11lib: sbi: Fix sbi_pmu_exit() for systems not having MCOUNTINHIBIT csrAnup Patel1-0/+4
The sbi_pmu_exit() crashes on systems not having MCOUNTINHIBIT csr so to fix this we check SBI_HART_HAS_MCOUNTINHIBIT feature in sbi_pmu_exit() and do nothing if it is not available. Fixes: 13d40f21d588 ("lib: sbi: Add PMU support") Signed-off-by: Anup Patel <anup.patel@wdc.com>
2021-07-11lib: sbi: Improve TLB function namingAtish Patra1-21/+20
Follow the standard conventon for static function names: All global functions should be start with sbi_<module name>_ All static functions should be start with <module name>_ Reviewed-by: Anup Patel <anup.patel@wdc.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-07-11lib: sbi: Implement firmware countersAtish Patra6-0/+60
RISC-V SBI v0.3 specification defines a set of firmware events that can provide additional information about the current firmware context. All of the firmware event monitoring are enabled now. The firmware events must be defined as raw perf event with MSB set as specified in the specification. Reviewed-by: Anup Patel <anup.patel@wdc.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-07-11lib: sbi: Implement SBI PMU extensionAtish Patra4-1/+98
RISC-V SBI specfication 0.3 defines a PMU extension that allows supervisor mode to start/stop/configure pmu related events. This patch implements all of the functionality defined in the specification. Reviewed-by: Anup Patel <anup.patel@wdc.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-07-11lib: sbi: Add PMU supportAtish Patra3-0/+632
RISC-V SBI v0.3 specification defined a PMU extension to configure/start/stop the hardware/firmware pmu events. Implement PMU support in OpenSBI library. The implementation is agnostic of event to counter mapping & mhpmevent value configuration. That means, it expects platform hooks will be used to set up the mapping and provide the mhpmevent value at runtime. Reviewed-by: Anup Patel <anup.patel@wdc.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-07-11lib: sbi: Use csr_read/write_num to read/update PMU countersAtish Patra1-0/+35
Currently, csr_read/write_num functions are used to read/write PMP related CSRs where CSR value is decided at runtime. Expand this function to include PMU related CSRs as well. Reviewed-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-07-11lib: sbi: Remove redundant boot time print statementAtish Patra1-2/+0
This patch removes redundant print from sbi_boot_print_hart(). Reviewed-by: Anup Patel <anup.patel@wdc.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-07-11lib: sbi: Disable m/scounteren & enable mcountinhibitAtish Patra1-4/+21
Currently, all bits in mcountern are enabled unconditionally at boot time. With SBI PMU extension, all the programmable counters should enabled only during performance monitoring for a particular event. However, this is done only if mcountinhibit is implemented because the supervisor mode can not start/stop any event without mcountinhibit. Similarly, supervisor should take care enabling scounteren which allows U-mode to access programmable pmu counters. All the non-programmable ones (CY, TM, IR) should be enabled in M-mode because some userspace may rely on builtins such as __builtin_readcyclecounter. Supervisor OS can still disable them during initial configuration. Reviewed-by: Anup Patel <anup.patel@wdc.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-07-11lib: sbi: Detect number of bits implemented in mhpmcounterAtish Patra1-0/+48
RISC-V privilege specification allows the implementation to have less than 64 bits. Add a function to detect the number of implemented bits in mhpmcounter dynamically at runtime. Reviewed-by: Xiang W <wxjstz@126.com> Reviewed-by: Anup Patel <anup.patel@wdc.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-07-11lib: sbi: Remove stray '\' characterAtish Patra1-1/+1
Reviewed-by: Xiang W <wxjstz@126.com> Reviewed-by: Anup Patel <anup.patel@wdc.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-07-11lib: sbi: Detect mcountinihibit support at runtimeAtish Patra1-0/+11
RISC-V ISA specification v1.11 defined mcountinhibit CSR that allows software to stop any counter from incrementing. The SBI PMU extension depends on this CSR support in hardware. Define mcountinhibit as a hart specific feature and detect it at runtime. Reviewed-by: Anup Patel <anup.patel@wdc.com> Signed-off-by: Atish Patra <atish.patra@wdc.com>
2021-06-22lib: simplify sbi_fifo_inplace_update()Heinrich Schuchardt1-2/+2
Don't assign an unused value to variable index. Use operator '-=' where applicable. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Atish Patra <atish.patra@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com>
2021-06-22lib: sign conflict in wake_coldboot_harts()Heinrich Schuchardt1-1/+1
Compiling wake_coldboot_harts() with GCC 11 and -Wextra yields: lib/sbi/sbi_init.c:208:27: error: comparison of integer expressions of different signedness: ‘int’ and ‘u32’ {aka ‘unsigned int’} [-Werror=sign-compare] 208 | for (int i = 0; i <= sbi_scratch_last_hartid(); i++) { | ^~ Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Atish Patra <atish.patra@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com>
2021-06-22lib: sign conflict in sbi_tlb_entry_process()Heinrich Schuchardt1-2/+3
Compiling sbi_tlb_entry_process() with GCC 11 and -Wextra yields: lib/sbi/sbi_tlb.c: In function ‘sbi_tlb_process_count’: lib/sbi/sbi_tlb.c:206:31: error: comparison of integer expressions of different signedness: ‘u32’ {aka ‘unsigned int’} and ‘int’ [-Werror=sign-compare] 206 | if (deq_count > count) Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Atish Patra <atish.patra@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com>
2021-06-11lib: sbi_scratch: zero out scratch memory on all hartsHeinrich Schuchardt1-2/+2
In sbi_scratch_init() we determine the last hart. The index of the last hart cannot exceed SBI_HARTMASK_MAX_BITS - 1. We should not initialize last_hartid_having_scratch to a higher number to avoid buffer overflows when using this value before calling sbi_scratch_init(). When allocating scratch memory in sbi_scratch_alloc_offset() we zero out the allocated memory for all harts except for the last one. We should not skip the last hart. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2021-06-02lib: sbi_scratch: remove owner from sbi_scratch_alloc_offsetHeinrich Schuchardt7-17/+9
The parameter owner of function sbi_scratch_alloc_offset() is never used. The scratch memory is small. We should not use it for debug information in future. Hence eliminate the parameter. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Xiang W <wxjstz@126.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2021-05-24lib: Check region base for merging in sbi_domain_root_add_memregion()Anup Patel1-1/+2
We can merge region B onto region A only if base of region A is aligned to region A order + 1. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com>
2021-05-14lib: sbi: Have spinlock checks return boolDaniel Schaefer1-3/+3
spin_lock_check already returned bool in the source file but not in the header. With some toolchains that causes an error, as it should. Because it and related functions all essentially return a bool, we can use this opportunity to change them. Signed-off-by: Daniel Schaefer <git@danielschaefer.me> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2021-04-28lib: sbi: Show devices provided by platform in boot printsAnup Patel1-0/+20
We extend the boot-time prints to show various devices provided (or registered) by the platform support. This will help users verify hardware devices available for OpenSBI firmwares. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-28lib: sbi: Simplify HSM platform operationsAnup Patel2-18/+56
Instead of having hsm_start(), hsm_stop() and hsm_suspend() callbacks in platform operations, it will be much simpler for HSM driver to directly register these operations as a device to the sbi_hsm implementation. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-28lib: sbi: Simplify system reset platform operationsAnup Patel1-5/+20
Instead of having system_reset_check() and system_reset() callbacks in platform operations, it will be much simpler for reset driver to directly register these operations as a device to the sbi_system implementation. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-28lib: sbi: Simplify ipi platform operationsAnup Patel3-10/+28
Instead of having ipi_send() and ipi_clear() callbacks in platform operations, it will be much simpler for ipi driver to directly register these operations as a device to sbi_ipi implementation. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-28lib: sbi: Simplify timer platform operationsAnup Patel2-23/+35
Instead of having timer_value(), timer_event_start(), and timer_event_stop() callbacks in platform operations, it will be much simpler for timer driver to directly register these operations as device to the sbi_timer implementation. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-28lib: sbi: Simplify console platform operationsAnup Patel1-8/+23
Instead of having console_putc() and console_getc() callbacks in platform operations, it will be much simpler for console driver to directly register these operations as device to the sbi_console implementation. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com>
2021-04-28lib: sbi: Remove domains_root_regions() platform callbackAnup Patel1-6/+0
We now have sbi_domain_root_add_memregion() which allows platform support to add root domain regions at boot-time so let's remove the domains_root_regions() platform callback which was added for this purpose. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com>
2021-04-24lib: sbi: Fix sbi_domain_root_add_memregion() for merging memregionsAnup Patel1-0/+1
We should decrement root_memregs_count by one after merging two memregions otherwise new memregion added after a merge will be appended after last sentinel memregion of zero order. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com>
2021-04-13lib: sbi: Make sbi_domain_memregion_initfw() a local functionAnup Patel1-2/+2
The sbi_domain_memregion_initfw() is no longer used outside sbi_domain.c so let's make it a local function. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-13lib: sbi: Make the root domain instance global variableAnup Patel1-1/+1
We make the the root domain instance global variable so that platform support and drivers can iterate over the root domain regions. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-13lib: sbi: Add sbi_domain_root_add_memregion() APIAnup Patel1-11/+72
We should allow platform support to add more root memory regions before domains are finalized. This will help platform support to protect critical M-mode only resources. This patch adds sbi_domain_root_add_memregion() API for above described purpose. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-13lib: sbi: Add sbi_domain_memregion_init() APIAnup Patel1-9/+36
This patch adds sbi_domain_memregion_init() helper API which can be used by platform support to initialize a domain memory region before adding it to the root domain. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-13lib: sbi: Domains can be registered only before finalizing domainsAnup Patel1-2/+9
The domains are boot-time system-level partitions so we should allow platform support to register domains only before hart domain assignments are finalized. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
2021-04-09lib: sbi: fix atomic_add_returnXiang W1-10/+8
The unsigned length may be 4 bytes or 8 bytes, amoadd.w only applies to 4 bytes hence this patch. Signed-off-by: Xiang W <wxjstz@126.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2021-04-09lib: sbi: Replace test-and-set locks by ticket locksChristoph Muellner1-17/+49
Replace the test-and-set spinlock implementation with ticket locks in order to get fairness (in form of FIFO order). The implementation uses a 32-bit wide struct, which consists of two 16-bit counters (owner and next). This is inspired by similar spinlock implementations on other architectures. This allows that the code works for both, RV32 and RV64. Signed-off-by: Christoph Muellner <cmuellner@linux.com> Reviewed-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com>
2021-04-09include: sbi: Allow direct initialization via SPIN_LOCK_INIT()Christoph Muellner1-1/+1
The current implementation of SPIN_LOCK_INIT() provides the spinlock to be initialized as reference. This does not allow a direct initialization of the spinlock object at the creation site. Let's pass the spinlock directly instead (like Linux does as well) and adjust all users of the macro (in fact there is only one user). Signed-off-by: Christoph Muellner <cmuellner@linux.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2021-04-05lib: fix csr detect supportXiang W1-3/+0
csr_read_allowed/csr_read_allowed requires trap.case to detect the results, but if no exception occurs, the value of trap.case will remain unchanged, which makes the detection results unreliable. Add code to initialize trap.case to 0. Signed-off-by: Xiang W <wxjstz@126.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2021-04-01firware: optimize the exception exit codeXiang W1-2/+3
There are two copies of the same abnormal exit code, this patch deletes one Signed-off-by: Xiang W <wxjstz@126.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2021-03-22lib: sbi: illegal CSR 0x306 access in hpm_allowed()Heinrich Schuchardt1-5/+14
The trap handler sbi_emulate_csr_read() invokes hpm_allowed() which reads CSR 0x306 (mcounteren). The K210 does not support CSR 0x306. While trying to handle a trap occurring in S-mode code this creates an additional trap in M-mode. This results in failure to redirect to S-mode and the system hanging in sbi_hart_hang(). In hart_detect_features() we have already determined if CSR 0x306 is available and stored that information in the scratch area. We can use this information to decide if CSR 0x306 shall be accessed in hpm_allowed() and thus avoid the M-mode trap. Likewise if CSR scounteren is not available we have to avoid reading CSR 0x106. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2021-03-03lib: Remove redundant sbi_platform_ipi_clear() callsAnup Patel3-7/+16
The sbi_platform_ipi_clear() called from wait_for_coldboot() and sbi_hsm_hart_wait() is redundant because IPI will be automatically cleared by sbi_platform_ipi_init() called from sbi_ipi_init(). Further, wait_for_coldboot() is common for warm startup and warm resume path so the sbi_platform_ipi_clear() called in warm resume path cause resuming HART to miss an IPI injected other HART to wakeup the HART. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-03-03lib: sbi: Implement SBI HSM suspend functionAnup Patel3-9/+231
This patch implements the SBI HSM suspend function. Using this new SBI call, the S-mode software can put calling HART in platform specific suspend (i.e. low-power) state. For a successful retentive suspend, the SBI call will return without errors upon resuming whereas for a successful non-retentive suspend, the SBI call will resume from a user provided resume address. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-03-03lib: sbi: Add sbi_hart_reinit() functionAnup Patel1-12/+17
We add sbi_hart_reinit() function which will re-initialize HART CSRs assuming HART features are already detected. This new function will be useful in re-initializing HART after it resumes from HSM SUSPENDED state. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-03-03lib: sbi: Fix error codes returned by HSM start() and stop() functionsAnup Patel1-2/+2
The sbi_hsm_hart_start() and sbi_hsm_hart_stop() functions should only return error codes as defined by the SBI specification. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-03-03lib: sbi: Remove redundant sbi_hsm_hart_started() functionAnup Patel1-11/+3
The sbi_hsm_hart_started() function is only used by sbi_hsm_hart_stop() for checking state of calling HART and current domain assignment. The atomic_cmpxchg() called by sbi_hsm_hart_stop() will check state of calling hart anyway and domain assignment can be checked by other domain function such as sbi_domain_is_assigned_hart(). This means sbi_hsm_hart_started() is redundant and can be removed. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-03-03lib: sbi: Rename sbi_hsm_hart_started_mask() functionAnup Patel4-7/+7
A hart can take interrupt in the new HSM states introduced by the SBI HSM suspend function (such as SUSPENDED state) so we rename sbi_hsm_hart_started_mask() to something more generic such as sbi_hsm_hart_interruptible_mask(). Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-03-03lib: sbi: Use SBI_HSM_STATE_xyz defines instead of SBI_STATE_xyz definesAnup Patel2-47/+24
We replace the use of SBI_STATE_xyz defines with SBI_HSM_STATE_xyz defines because the HSM state defines are complete enough to implement HSM state machine in OpenSBI. As a result of this, we can now remove sbi_hsm_hart_state_to_status() function because it is now redundant and sbi_hsm_hart_get_state() can directly return HSM state or error. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-03-03include: sbi: Simplify HSM state define namesAnup Patel1-4/+4
We simplify HSM state define names so that these defines can directly replace SBI_HART_xyz defines used by SBI HSM implementation. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-01-12lib: sbi: Allow platforms to provide root domain memory regionsAnup Patel1-8/+8
Currently, the root domain memory regions are fixed in generic code but some of the platforms may want to explicitly define memory regions for the root domain. This patch adds optional domains_root_regions() platform callback which platforms can use to provide platform specific root domain memory regions. Due to this changes, the root domain should also undergo all sanity checks (just like regular domain) so we use sbi_domain_register() to register root domain. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-01-07lib: sbi: Allow custom local TLB flush functionAnup Patel3-51/+33
Currently, we have fixed TLB flush types supported by the remote TLB library. This approach is not flexible and does not allow custom local TLB flush function. For example, after updating PMP entries on a set of HARTs at runtime, we have to flush TLB on these HARTs as well. To support custom local TLB flush function, we replace the "type" field of "struct sbi_tlb_info" with a local TLB flush function pointer. We also provide definitions of standard TLB flush operations (such as fence_i, sfence.vma, hfence.vvma, hfence.gvma, etc). Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-01-07lib: sbi: Introduce sbi_trap_exit() APIAnup Patel1-0/+20
We introduce sbi_trap_exit() API which can help non-firmware (i.e. generic or platform) code to force exit trap/interrupt handling and resume execution at context pointed by parameter "const struct sbi_trap_regs *regs". This new sbi_trap_exit() API will help Keystone Enclave project to resume execution of enclave from custom SBI call handler. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2021-01-07lib: sbi: Replace args with trap registers in ecall handlerAnup Patel6-55/+59
We had added args pointer in ecall handler to ensure that ecall handler only implements functionality and does not deal with SBI calling convention. This also helped us to keep SBI calling convention related code in one place at sbi_ecall_handler(). The Keystone Enclavce project needs access to the trap regsiters in their ecall handler so that they can context switch enclaves in custom SBI calls. To help the Keystone Enclave project, we replace the args pointer in ecall handler parameter with a const pointer to trap registers. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>