summaryrefslogtreecommitdiff
path: root/lib/sbi
AgeCommit message (Collapse)AuthorFilesLines
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>
2020-12-24lib: sbi: Fix sign-extension in sbi_misaligned_load_handler()Anup Patel1-1/+1
The misaligned load emulation does not sign-extend values correctly due to missing sign typecast in value passed to the SET_RD() macro. A very easy way to reproduce this issue is to load 16-bit value 0xff1e from a byte aligned address using LH instruction on hardware lacking misaligned load/store. This patch fixes sbi_misaligned_load_handler() for above issue. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-16lib: sbi: Remove domain_get() platform callback functionAnup Patel1-75/+3
The domain_get() platform callback function is now redundant because fdt_domain_populate() register new domain explicitly using the sbi_domain_register() function. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-16lib: sbi: Add function to register new domainAnup Patel1-0/+71
We add sbi_domain_register() function to help platform code register a new domain explicitly instead of depending on domain_get() platform callback. This function will eventually help us completely remove the domain_get() platform callback. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-04include: sbi: Add domains_init() platform operationAnup Patel1-0/+8
We introduce domains_init() platform operation which can be used by platform support to initialize/populate domains in the coldboot path. The domains_init() is called late in the coldboot sequence from the sbi_domain_finalize() so sbi_printf() can be used by platform support to print errors/warnings at time of populating domains. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-04lib: sbi: Add error prints in sbi_domain_finalize()Anup Patel1-11/+52
We add error prints in sbi_domain_finalize() and sanitize_domain() to help debug domain configuration issues. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-04lib: sbi: Override domain boot HART when coldboot HART assigned to itAnup Patel1-0/+11
If coldboot HART is assigned to a domain then we should use coldboot HART as the boot HART of the domain. This allows coldboot HART to continue boot sequence even when it is assigned to some non-root domain. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-04lib: sbi: Auto start domain only if boot HART within limitsAnup Patel1-0/+4
The sbi_domain_finalize() should auto start a domain only if the boot HART is withing limits (i.e. less than SBI_HARTMASK_MAX_BITS). Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-04lib: sbi: Fix domain_count check in sbi_domain_finalize()Anup Patel1-1/+1
The domain_count check in sbi_domain_finalize() for newly discovered domain is incorrect and should be reversed. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-04lib: sbi: Fix typo in sbi_domain_finalize()Anup Patel1-2/+2
There has been typo in automatic domain boot hart startup for non-root domains so this patch fixes it. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-04lib: sbi: Fix sbi_hart_switch_mode() for u-modeAnup Patel1-3/+5
We should check and access N-extension CSRs in sbi_hart_switch_mode() when next_mode is u-mode because N-extension is optional. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-01lib: sbi: Implement System Reset (SRST) SBI extensionAnup Patel2-0/+63
The SBI SRST extension has been accepted and merged in the latest SBI v0.3-draft specification. (Refer, https://github.com/riscv/riscv-sbi-doc) It allows to S-mode software to request system shutdown, cold reboot, and warm reboot. This patch implements SBI SRST extension as a replacement of the legacy sbi_shutdown() call of SBI v0.1 specification. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-01lib: sbi: Improve system reset platform operationsAnup Patel2-3/+13
To implement the SBI SRST extension, we need two platform operations for system reset: 1) system_reset_check() - This operation will check whether given reset type and reason are supported by the platform 2) system_reset() - This operation will do the actual platform system reset and it will not return if reset type and reason are supported by the platform This patch updates system reset related code everywhere as-per above. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-12-01include: sbi: Remove opensbi specific reset type definesAnup Patel2-3/+3
We can now use the standard SBI SRST extension reset types instead of the opensbi specific (SBI_PLATFORM_RESET_xyz) reset types hence remove related opensbi specific defines. The "platform_" prefix of the reset type parameter of sbi_system_reset() function should also be removed. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-11-10lib: sbi: Improve boot prints in cold boot sequenceAnup Patel1-21/+69
Currently, we have all boot prints at the end of cold boot sequence which means if there is any failure in cold boot sequence before boot prints then we don't get any print. This patch improves boot prints in cold boot sequence as follows: 1. We divide the boot prints into multiple parts and print it from different locations after sbi_console_init() 2. We throw an error print if there is any failure in cold boot sequence after sbi_console_init() Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
2020-10-26lib: sbi: Improve boot time print with additional PMP informationAtish Patra1-14/+23
We know about pmp granularity and number of bits supported by PMP. Show those information in the boot time info print Signed-off-by: Atish Patra <atish.patra@wdc.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>
2020-10-26lib: sbi: Detect PMP granularity and number of address bitsAtish Patra1-4/+58
As per RISC-V privilege specification, a platform may choose to implement a coarser granularity scheme for PMP addresses. In that case, we shouldn't allow any pmp region size smaller than the platform supports. A platform may not also implement all the bits for a PMP address specified in the priv specification. The pmp range granularity and address bits should be detected dynamically before detecing PMP regions. Any pmp modification request beyond these detected value must not succeed. Signed-off-by: Atish Patra <atish.patra@wdc.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>