platform/kernel/opensbi.git
11 months agoAdd gbs.conf file to build opensbi with version tag accepted/tizen_unified accepted/tizen_unified_toolchain accepted/tizen_unified_x accepted/tizen_unified_x_asan tizen tizen-next accepted/tizen/unified/20241121.055543 accepted/tizen/unified/toolchain/20240812.131824 accepted/tizen/unified/x/20240715.090416 accepted/tizen/unified/x/asan/20240813.230059
Jaehoon Chung [Thu, 28 Dec 2023 00:19:14 +0000 (09:19 +0900)]
Add gbs.conf file to build opensbi with version tag

Add .gbs.conf file to build opensbi with upstream version tag.

Change-Id: I13a31be218a12d513966b29bcafbb3344b739ef0
Signed-off-by: Jaehoon Chung <jh80.chung@samsung.com>
(cherry picked from commit 8b8f706e43cc90a59c425a40e6a12d697f9af4f5)
Signed-off-by: Jaehoon Chung <jh80.chung@samsung.com>
11 months agopackaging: opensbi: Add opensbi spec file to build
Jaehoon Chung [Tue, 2 May 2023 04:59:12 +0000 (13:59 +0900)]
packaging: opensbi: Add opensbi spec file to build

Add opensbi spec file to build on gbs system.

Change-Id: I905a75b0ea419c14fcc1fd36d093b6e927913907
Signed-off-by: Jaehoon Chung <jh80.chung@samsung.com>
11 months agoinclude: Bump-up version to 1.5 v1.5
Anup Patel [Sun, 30 Jun 2024 08:33:26 +0000 (14:03 +0530)]
include: Bump-up version to 1.5

This patch updates OpenSBI version to 1.5 as part of
release preparation.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Anup Patel <anup@brainfault.org>
11 months agolib: sbi: check incoming dbtr shmem address
Sergey Matyukevich [Thu, 27 Jun 2024 12:04:09 +0000 (15:04 +0300)]
lib: sbi: check incoming dbtr shmem address

Current Debug Trigger SBI extension proposal suggests to activate
shmem area and obtain its physical address from S-mode software
in the following way:

: If both `shmem_phys_lo` and `shmem_phys_hi` parameters are not
: all-ones bitwise then `shmem_phys_lo` specifies the lower XLEN
: bits and `shmem_phys_hi` specifies the upper XLEN bits of the
: shared memory physical base address. The `shmem_phys_lo` MUST
: be `(XLEN / 8)` byte aligned and the size of shared memory is
: assumed to be `trig_max * (XLEN / 2)` bytes.

For more details see the current version of the proposal:
- https://lists.riscv.org/g/tech-debug/message/1302

On the other hand, on RV32, the M-mode can only access the first 4GB of
the physical address space because M-mode does not have MMU to access
full 34-bit physical address space. Similarly, on RV64, the M-mode can
only access memory addressed by 64 bits.

This commit checks shmem address in function sbi_dbtr_setup_shmem
to make sure that shmem_phys_hi part of the valid address is zero.
Besides, the macro DBTR_SHMEM_MAKE_PHYS is updated to take into
account only low XLEN part.

Signed-off-by: Sergey Matyukevich <geomatsi@gmail.com>
Reviewed-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
11 months agodocs: Fix wrong filename
Jacob Lin [Thu, 27 Jun 2024 03:21:37 +0000 (11:21 +0800)]
docs: Fix wrong filename

Correct the compiled FW_PAYLOAD firmware ELF filename.

Signed-off-by: Jacob Lin <lovetaeyeon507@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
11 months agolib: sbi: fwft: return SBI_EINVAL rather than SBI_ERR_INVALID_PARAM
Clément Léger [Mon, 24 Jun 2024 10:29:09 +0000 (12:29 +0200)]
lib: sbi: fwft: return SBI_EINVAL rather than SBI_ERR_INVALID_PARAM

Error code returned by the ecall handles should use the defines from
sbi_ecall_interface.h rather than sbi_error.h.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
11 months agolib: sbi: fwft: check feature value to be exactly 1 or 0
Clément Léger [Mon, 24 Jun 2024 10:29:08 +0000 (12:29 +0200)]
lib: sbi: fwft: check feature value to be exactly 1 or 0

As stated by the spec and pointed out by Andrew Jones, the value passed
for MISALIGNED_EXC_DELEG and PTE_AD_HW_UPDATING should be either 0 or 1.
Add check for these values and return SBI_EINVAL if not.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
11 months agolib: sbi: implement SBI FWFT extension
Clément Léger [Wed, 19 Jun 2024 09:42:42 +0000 (11:42 +0200)]
lib: sbi: implement SBI FWFT extension

The SBI FWFT extension defines a set of function that can be called
to control the configuration of some platform features (misaligned
trap delegation, etc). This patch implements sbi_fwft_set() and
sbi_fwft_get() as defined in the specification [1].

Link: https://lists.riscv.org/g/tech-prs/message/924
Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
11 months agolib: sbi: fwft: add support for SBI_FWFT_PTE_AD_HW_UPDATING
Clément Léger [Wed, 19 Jun 2024 09:42:41 +0000 (11:42 +0200)]
lib: sbi: fwft: add support for SBI_FWFT_PTE_AD_HW_UPDATING

Add support for SBI_FWFT_PTE_AD_HW_UPDATING based on SVADU presence.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
11 months agolib: sbi: fwft: add support for SBI_FWFT_MISALIGNED_EXC_DELEG
Clément Léger [Wed, 19 Jun 2024 09:42:40 +0000 (11:42 +0200)]
lib: sbi: fwft: add support for SBI_FWFT_MISALIGNED_EXC_DELEG

Add support for SBI_FWFT_MISALIGNED_EXC_DELEG withing FWFT support. This
support allows to delegate misaligned accesses traps.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
11 months agolib: sbi: add support for firmware features extension
Clément Léger [Wed, 19 Jun 2024 09:42:39 +0000 (11:42 +0200)]
lib: sbi: add support for firmware features extension

This extension allows the software running in supervisor mode to control
the behavior of various features of the SBI [1]. Implement the support
for such extension.

Link: https://lists.riscv.org/g/tech-prs/message/924
Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
11 months agolib: sbi: Add support for Svade and Svadu extensions
Yong-Xuan Wang [Wed, 5 Jun 2024 12:11:09 +0000 (20:11 +0800)]
lib: sbi: Add support for Svade and Svadu extensions

Add support for Svade and Svadu extensions. When both are present in the
device tree, the M-mode firmware should select the Svade extension
to comply with the RVA23 profile, which mandates Svade and lists Svadu as
an optional extension.

Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
11 months agoplatform: generic: andes: Add support for RV32 to set up PMA
Ben Zong-You Xie [Fri, 31 May 2024 05:21:08 +0000 (13:21 +0800)]
platform: generic: andes: Add support for RV32 to set up PMA

Like PMP, the behaviors to configure PMA will be different from
RV64 and RV32. RV64 uses two Andes custom CSRs, pmacfg0 and pmacfg2,
but RV32 uses four Andes custom CSRs, pmacfg0 ~ pmacfg3. This patch
adds support to PMA for RV32.

Signed-off-by: Ben Zong-You Xie <ben717@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
11 months agoplatform: generic: andes: Refine Andes PMA related code
Ben Zong-You Xie [Fri, 31 May 2024 05:19:04 +0000 (13:19 +0800)]
platform: generic: andes: Refine Andes PMA related code

This patch refines the Andes PMA related code. The main change is
refactor andes_pma_[read|write]_cfg() and andes_pma_[read|write]_addr()
into new functions andes_pma_[read|write]_num().

Also, fix some coding style problems.

Signed-off-by: Ben Zong-You Xie <ben717@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: serial: fix RX path in litex-uart
Gabriel Somlo [Fri, 7 Jun 2024 18:23:42 +0000 (14:23 -0400)]
lib: serial: fix RX path in litex-uart

When used to read characters from the terminal (e.g., when the SBI
console is used via ecall from linux with `console=hvc0`), we must
acknowledge receipt of each character to "pop" it off the LiteUART
hardware queue, and allow the next character to be made available.

Fixes: 52af6e4b ("lib: utils: Add LiteX UART support")
Suggested-by: Dolu1990 <charles.papon.90@gmail.com>
Signed-off-by: Gabriel Somlo <gsomlo@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: utils/reset: Try initializing all reset devices in dt
Xiang W [Thu, 13 Jun 2024 15:03:07 +0000 (23:03 +0800)]
lib: utils/reset: Try initializing all reset devices in dt

In DT, multiple reset devices may use the same driver, and they
may have different priorities. If rc is returned after the first
initialization, the highest priority device may be lost.

Fixes: a73ff043e991 (lib: utils/reset: Fix fdt_reset to search for more dt nodes)
Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agosbi: sbi_domain_context: Fix trap context for domain context switching
Yu Chien Peter Lin [Fri, 7 Jun 2024 11:39:28 +0000 (19:39 +0800)]
sbi: sbi_domain_context: Fix trap context for domain context switching

Save/restore sbi_trap_context during domain context switching to
ensure proper trap handling and isolation. This maintains correct
domain-specific state, avoiding context corruption.

Fixes: abea949721bc ("lib: sbi: Introduce trap context")
Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Alvin Chang <alvinga@andestech.com>
Tested-by: Alvin Chang <alvinga@andestech.com>
Reviewed-by: Yong Li <yong.li@intel.com>
Tested-by: Yong Li <yong.li@intel.com>
12 months agolib: utils/reset: Fix fdt_reset to search for more dt nodes
Xiang W [Tue, 11 Jun 2024 11:19:34 +0000 (19:19 +0800)]
lib: utils/reset: Fix fdt_reset to search for more dt nodes

If there are multiple dt nodes, the previous code only tries to match
the first one, which may lose initialization.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: utils/reset: Skip initialize reset when dt is not enabled
Xiang W [Tue, 11 Jun 2024 11:19:33 +0000 (19:19 +0800)]
lib: utils/reset: Skip initialize reset when dt is not enabled

When the dt node has a status property and the value is not ok or
okay, skip initializing reset.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: utils/serial: Fix fdt_serial to match more dt nodes
Xiang W [Tue, 11 Jun 2024 11:19:32 +0000 (19:19 +0800)]
lib: utils/serial: Fix fdt_serial to match more dt nodes

If there are multiple dt nodes, the previous code only tries to match
the first one, which may lose initialization.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: utils/serial: Skip initialize serial when dt is not enabled
Xiang W [Tue, 11 Jun 2024 11:19:31 +0000 (19:19 +0800)]
lib: utils/serial: Skip initialize serial when dt is not enabled

When the dt node has a status property and the value is not ok or
okay, skip initializing serial.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: utils/irqchip: Skip initialize irqchip when dt is not enabled
Xiang W [Tue, 11 Jun 2024 11:19:30 +0000 (19:19 +0800)]
lib: utils/irqchip: Skip initialize irqchip when dt is not enabled

When the dt node has a status property and the value is not ok or
okay, skip initializing irqchip.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: utils/timer: Skip initialize timer when dt is not enabled
Xiang W [Tue, 11 Jun 2024 11:19:29 +0000 (19:19 +0800)]
lib: utils/timer: Skip initialize timer when dt is not enabled

When the dt node has a status property and the value is not ok or
okay, skip initializing timer.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: utils/ipi: Skip initialize ipi when dt is not enabled
Xiang W [Tue, 11 Jun 2024 11:19:28 +0000 (19:19 +0800)]
lib: utils/ipi: Skip initialize ipi when dt is not enabled

When the dt node has a status property and the value is not ok or
okay, skip initializing ipi.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: utils/fdt: Fix DT property for APLIC delegation
Anup Patel [Wed, 5 Jun 2024 05:18:46 +0000 (10:48 +0530)]
lib: utils/fdt: Fix DT property for APLIC delegation

During Linux AIA driver review, the APLIC DT property for interrupt
delegation was renamed to "riscv,delegation" so let's use the new DT
property name and fallback to old DT property name if the new DT
property name is not available.

Fixes: 34612193af92 ("lib: utils/irqchip: Add FDT based driver for APLIC")
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
12 months agolib: utils/gpio: respect flag GPIO_FLAG_ACTIVE_LOW
Inochi Amaoto [Thu, 23 May 2024 05:07:45 +0000 (13:07 +0800)]
lib: utils/gpio: respect flag GPIO_FLAG_ACTIVE_LOW

"gpio-poweroff" and "gpio-restart" always set gpio to high to
active the function, but some chips need a low signal to active.
Fortunately, it can be achieved by setting GPIO_FLAG_ACTIVE_LOW
for the gpio. Implement this flag support for the gpio library
so the gpio reset can function well.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: sbi: sse: handle missing writable attributes
Clément Léger [Thu, 16 May 2024 12:30:28 +0000 (14:30 +0200)]
lib: sbi: sse: handle missing writable attributes

The spec states that a6, a7, flags and sepc are writable but the
implementation was not allowing that. Add support for these 4 writable
attributes.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: utils/irqchip: Add sanity checks in imsic_get_data() and imsic_get_target_file()
Cyan Yang [Thu, 16 May 2024 08:32:43 +0000 (16:32 +0800)]
lib: utils/irqchip: Add sanity checks in imsic_get_data() and imsic_get_target_file()

Add extra sanity checks to prevent the caller getting the invalid result from
imsic_get_data() or imsic_get_target_file() when imsic is not initialized
correctly.

Signed-off-by: Cyan Yang <cyan.yang@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agofirmware: Bring back FW_TEXT_START as an optional parameter
Anup Patel [Wed, 15 May 2024 04:25:50 +0000 (09:55 +0530)]
firmware: Bring back FW_TEXT_START as an optional parameter

Bring back FW_TEXT_START as an optional parameter to allow users
explicitly specify compile time address for loading debug symbols.
When not specified, the FW_TEXT_START is assumed to be 0.

Fixes: d4d2582eef7a ("firmware: remove FW_TEXT_START")
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Tested-by: Clément Léger <cleger@rivosinc.com>
12 months agolib: utils/irqchip: Check before initializing imsic
Cheng Yang [Wed, 15 May 2024 06:15:14 +0000 (14:15 +0800)]
lib: utils/irqchip: Check before initializing imsic

The current mlevel imsic check is only for the platform, which
may cause hart without imsic in the platform to trigger an
illegal instruction exception when initializing imsic. For
example, the platform contains a management hart that only
supports wired interrupts.

This patch will check if each hart supports Smaia extension
before doing imsic initialization to avoid triggering illegal
instruction exceptions.

Signed-off-by: Cheng Yang <yangcheng.work@foxmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agoplatform: generic: thead: add Sophgo CV18XX/SG200X series
Inochi Amaoto [Tue, 14 May 2024 04:53:40 +0000 (12:53 +0800)]
platform: generic: thead: add Sophgo CV18XX/SG200X series

The Sophgo CV18XX/SG200X series SoCs have a standard C906
core. Add support for it.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
12 months agolib: sbi: Put event after use in sbi_sse_exit() loop
Anup Patel [Wed, 8 May 2024 11:32:39 +0000 (17:02 +0530)]
lib: sbi: Put event after use in sbi_sse_exit() loop

Currently, the sbi_sse_exit() gets event in a loop but does not put
it back after use. This results in global events remaining locked
causing hangs on sub-sequent calls to sse_event_get() for global
events.

Fixes: c8cdf01d8f3a ("lib: sbi: Add support for Supervisor Software Events extension")
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
12 months agoutils/reset: Add SG2042 hwmon MCU support.
Inochi Amaoto [Sun, 28 Apr 2024 01:24:10 +0000 (09:24 +0800)]
utils/reset: Add SG2042 hwmon MCU support.

SG2042 uses an onboard MCU to provide reset function.
Add reset driver to support this onboard MCU.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: simplify inline function in sbi_dtbr.c
Xiang W [Fri, 3 May 2024 13:53:38 +0000 (21:53 +0800)]
lib: sbi: simplify inline function in sbi_dtbr.c

The inline function can simplify the code by setting some call
restrictions. This ensures logical smoothness

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: fix return type of sbi_dbtr_shmem_disabled
Xiang W [Fri, 3 May 2024 13:53:37 +0000 (21:53 +0800)]
lib: sbi: fix return type of sbi_dbtr_shmem_disabled

Modify the return value of the sbi_dbtr_shmem_disabled function to
bool to make the semantics clearer.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: Fixed memory permission check in sbi_dbtr_setup_shmem
Xiang W [Fri, 3 May 2024 13:53:36 +0000 (21:53 +0800)]
lib: sbi: Fixed memory permission check in sbi_dbtr_setup_shmem

The previous code detected shmem_phys_hi and shmem_phys_lo as two
addresses. fix this bug

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
13 months agolib: sbi: fix DBTR_SHMEM_MAKE_PHYS for RV64
Xiang W [Fri, 3 May 2024 13:53:35 +0000 (21:53 +0800)]
lib: sbi: fix DBTR_SHMEM_MAKE_PHYS for RV64

Obtaining a 64-bit address under rv64 does not require combining
32-bit registers

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
13 months agolib: sbi: Removal unnecessary check dbtr_thishart_state_ptr
Xiang W [Fri, 3 May 2024 13:53:34 +0000 (21:53 +0800)]
lib: sbi: Removal unnecessary check dbtr_thishart_state_ptr

After getting hart_shmem_base, dbtr_thishart_state_ptr cannot be
empty. So remove check code.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
13 months agolib: sbi: fix hart_shmem_base
Xiang W [Fri, 3 May 2024 13:53:33 +0000 (21:53 +0800)]
lib: sbi: fix hart_shmem_base

When only phys_hi is equal to SBI_DBTR_SHMEM_INVALID_ADDR, it may be
a legal address. The old code would modify the legal address.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
13 months agoplatform: generic: thead: add Canaan Kendryte K230
Yangyu Chen [Sun, 21 Apr 2024 15:52:09 +0000 (23:52 +0800)]
platform: generic: thead: add Canaan Kendryte K230

Canaan Kendryte K230 SoC has T-Head C908 cores inside. The dt-binding has
been merged into the linux kernel [1]. However, it has early version of
C908 core which does not have Sscofpmf and need to use T-Head PMU
extension. Thus, we add a K230 compatible string to thead_generic_match
and set quirk for T-Head PMU.

[1] https://lore.kernel.org/linux-riscv/tencent_4D85743622F39109466913393EE2F6C5980A@qq.com/

Signed-off-by: Yangyu Chen <cyy@cyyself.name>
Reviewed-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agoplatform: generic: thead: separate T-Head PMU Errata
Yangyu Chen [Sun, 21 Apr 2024 15:52:08 +0000 (23:52 +0800)]
platform: generic: thead: separate T-Head PMU Errata

As Guo Ren said from the kernel mailing list [1], future T-Head CPUs,
including the newer versions of T-Head C908, will feature standard
Sscofpmf extension. For these CPUs, T-Head's implementation of PMU
Overflow Interrupts may not needed anymore. In this case, we shouldn't
apply T-Head PMU for all T-Head CPUs. Thus, this patch separated T-Head PMU
errata.

[1] https://lore.kernel.org/linux-riscv/Zh9sUUUT09LZb0MO@gmail.com/

Signed-off-by: Yangyu Chen <cyy@cyyself.name>
Reviewed-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agoplatform: andes: Change all occurrences of andes45 to andes
Ben Zong-You Xie [Fri, 19 Apr 2024 03:58:24 +0000 (11:58 +0800)]
platform: andes: Change all occurrences of andes45 to andes

To make the framework suit all Andes CPUs, change all occurrences of
andes45 to andes.

In addition, we fix some coding style problems and remove an unused
macro in andes.h.

Signed-off-by: Ben Zong-You Xie <ben717@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agoplatform: andes: Rename files with the prefix andes45
Ben Zong-You Xie [Fri, 19 Apr 2024 03:58:23 +0000 (11:58 +0800)]
platform: andes: Rename files with the prefix andes45

Rename files with the prefix andes45 to andes.

Signed-off-by: Ben Zong-You Xie <ben717@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agosbi: sbi_domain_context: Add spinlock for updating domain assigned_harts
Alvin Chang [Tue, 16 Apr 2024 05:02:13 +0000 (13:02 +0800)]
sbi: sbi_domain_context: Add spinlock for updating domain assigned_harts

Add spinlock protection to avoid race condition on assigned_harts
during domain context switching. Also, rename/add variables for
accessing the corresponding domain of target/current context.

Signed-off-by: Alvin Chang <alvinga@andestech.com>
Reviewed-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: Refine the settings for switching to Virtual Supervisor Mode.
Pope B.Lei [Wed, 10 Apr 2024 02:05:20 +0000 (10:05 +0800)]
lib: sbi: Refine the settings for switching to Virtual Supervisor Mode.

Although Mstatus.MPV is set, before executing mret, access to VS mode
registers should use the actual register addresses, not the pseudonyms
of S registers.

Signed-off-by: Pope B.Lei <popeblei@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: sse: check handler entry to belong to supervisor mode
Clément Léger [Tue, 9 Apr 2024 10:02:08 +0000 (12:02 +0200)]
lib: sbi: sse: check handler entry to belong to supervisor mode

When registering an SSE event, check for the handler_entry_pc to belong
to supervisor mode domain using sbi_domain_check_addr_range().

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reported-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: sse: use PRV_S instead of hardcoded value for mode
Clément Léger [Tue, 9 Apr 2024 10:02:07 +0000 (12:02 +0200)]
lib: sbi: sse: use PRV_S instead of hardcoded value for mode

Rather then passing 1 to sbi_domain_check_addr_range() for supervisor
mode, use PRV_S.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: sse: call enable callback before sending IPI
Clément Léger [Tue, 9 Apr 2024 10:02:06 +0000 (12:02 +0200)]
lib: sbi: sse: call enable callback before sending IPI

Move the enable callback call before sending the IPI. Even though the
event is locked and no race condition can happen, this is more logical.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reported-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: sse: remove superfluous sbi_list_empty() check
Clément Léger [Tue, 9 Apr 2024 10:02:05 +0000 (12:02 +0200)]
lib: sbi: sse: remove superfluous sbi_list_empty() check

The list loop below that check is actually not looping if the list is
empty so there was no need for this check.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reported-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: sse: simplify 32bits overflow check
Clément Léger [Tue, 9 Apr 2024 10:02:04 +0000 (12:02 +0200)]
lib: sbi: sse: simplify 32bits overflow check

Rather than checking 32bits overflow with some absolute value, check the
value to be different from the cast itself.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reported-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: sse: rename sse_hart_unlock() to sse_enabled_event_unlock()
Clément Léger [Tue, 9 Apr 2024 10:02:03 +0000 (12:02 +0200)]
lib: sbi: sse: rename sse_hart_unlock() to sse_enabled_event_unlock()

There was a naming incoherency between enabled events list lock/unlock.
Rename sse_hart_unlock() to sse_enabled_event_unlock() to be coherent
and reword comments above lock()/unlock() functions.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: sbi: sse: fix typos, comments and spacing errors
Clément Léger [Tue, 9 Apr 2024 10:02:02 +0000 (12:02 +0200)]
lib: sbi: sse: fix typos, comments and spacing errors

Fix some errors spotted by Samuel while reviewing the SSE implementation.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reported-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: tests: Add test for spinlocks
Ivan Orlov [Tue, 23 Apr 2024 15:52:45 +0000 (16:52 +0100)]
lib: tests: Add test for spinlocks

Implement the test which covers some of the functions from the
`riscv_locks.h` file. This test consists of 3 test cases:

1) For lock/unlock functions
2) Unsuccessful trylock (the lock was previously taken)
3) Successful trylock (the lock is free and can be taken)

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: tests: Add test for atomic_t
Ivan Orlov [Tue, 23 Apr 2024 15:52:44 +0000 (16:52 +0100)]
lib: tests: Add test for atomic_t

Implement the test which covers some of the functions from the
`riscv_atomic.h` header file. The test contains 9 test cases:

1) atomic read/write test
2) add/return test
3) sub/return test
4) cmpxchg test
5) atomic_xchg test
6) atomic_raw_set_bit test
7) atomic_raw_clear_bit test
8) atomic_set_bit test
9) atomic_clear_bit test

Some of the test cases operate on the `test_atomic` variable. It gets
initialized in the suite init function.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
13 months agolib: tests: Add test suite init function
Ivan Orlov [Tue, 23 Apr 2024 15:52:43 +0000 (16:52 +0100)]
lib: tests: Add test suite init function

Allow to define an init function for the test suite. It could help us
to initialize global variable once, and use them in multiple test cases
after the initialization.

For instance, if multiple test cases use the same atomic_t var, it
could be helpful to call ATOMIC_INIT once during the suite
initialization.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agofirmware: remove FW_TEXT_START
Xiang W [Mon, 8 Apr 2024 15:27:42 +0000 (23:27 +0800)]
firmware: remove FW_TEXT_START

Now opensbi can run at any address via dynamic relocation. We can
remove FW_TEXT_START.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
14 months agolib: utils: check correct value in fdt_node_offset_by_compatible
Heinrich Schuchardt [Mon, 8 Apr 2024 07:37:32 +0000 (09:37 +0200)]
lib: utils: check correct value in fdt_node_offset_by_compatible

After calling fdt_node_offset_by_compatible() we must check its return
value and not an unrelated value.

Addresses-Coverity-ID: 1584993 Logically dead code
Fixes: 67ce5a763cfb ("platform: generic: Add support for specify coldboot harts in DT")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agolib: sbi: sse_event_get() may return NULL
Heinrich Schuchardt [Mon, 8 Apr 2024 07:29:04 +0000 (09:29 +0200)]
lib: sbi: sse_event_get() may return NULL

sse_event_get() may return NULL. We should not dereference the return value
in sbi_sse_exit() without checking.

Fixes: c8cdf01d8f3a ("lib: sbi: Add support for Supervisor Software Events extension")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agolib: sbi: Add missing sscrind and sscfg extensions in sbi_hart_ext[]
Clément Léger [Wed, 20 Mar 2024 14:50:43 +0000 (15:50 +0100)]
lib: sbi: Add missing sscrind and sscfg extensions in sbi_hart_ext[]

The sbi_hart_ext[] array is missing these two extensions ids. It is
expected that this array contains all the extensions declaration at the
same index of the SBI_HART_EXT_* define. Without this, when adding a new
extension, there is a mismatch between ids and extension names and it
can even display corrupted extension names.

Addresses-Coverity-ID: 1584994 Out-of-bounds read
Fixes: 6bb6b61c27eb ("lib: sbi: Add support for smcsrind and smcdeleg")
Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agoMakefile: Remove unnecessary dependencies
Samuel Holland [Mon, 18 Mar 2024 20:49:41 +0000 (13:49 -0700)]
Makefile: Remove unnecessary dependencies

The rule included from auto.conf.cmd adds a dependency on every Kconfig
file, so these two Kconfig files do not need to be specified again here.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
14 months agoMakefile: Respect manual changes to .config
Samuel Holland [Mon, 18 Mar 2024 20:49:40 +0000 (13:49 -0700)]
Makefile: Respect manual changes to .config

The .config file may be manually edited or copied from another location.
Since genconfig.py is responsible for generating auto.conf (the Makefile
fragment) and autoconf.h (the C header) from .config, it must be run any
time .config changes, not just when running menuconfig.

Fixes: 662e631ccef2 ("Makefile: Add initial kconfig support for each platform")
Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
14 months agosbi: sbi_domain_context: Check privilege spec version before accessing S-mode CSRs
Yu Chien Peter Lin [Thu, 28 Mar 2024 03:33:02 +0000 (11:33 +0800)]
sbi: sbi_domain_context: Check privilege spec version before accessing S-mode CSRs

SCOUNTEREN and SENVCFG may not be supported on certain RISC-V core,
so check the existence of these CSRs via privilege spec version to
prevent illegal instructions.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Signed-off-by: Alvin Chang <alvinga@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agolib: sbi: Wakeup non-coldboot HARTs early in the coldboot path
Anup Patel [Tue, 19 Mar 2024 12:26:24 +0000 (17:56 +0530)]
lib: sbi: Wakeup non-coldboot HARTs early in the coldboot path

Currently, all non-coldboot HARTs busy spin in wait_for_coldboot()
until the entire coldboot init sequence is completed.

This means:
1) On QEMU, all non-coldboot HARTs will eat host CPU time and
   also slow down the coldboot HART until the entire coldboot
   init sequence is completed.
2) On real HW, all non-coldboot HARTs will consume more CPU
   power until the entire coldboot init sequence is completed.

To address this, wake up all non-coldboot HARTs as early as
possible in the coldboot init sequence.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
14 months agolib: sbi: Simplify wait_for_coldboot() implementation
Anup Patel [Fri, 15 Mar 2024 14:25:23 +0000 (19:55 +0530)]
lib: sbi: Simplify wait_for_coldboot() implementation

On QEMU virt machine with large number of HARTs, some of the HARTs
randomly fail to come out of wait_for_coldboot() due to one of the
following race-conditions:

1) Failing HARTs are not able to acquire the coldboot_lock and
   update the coldboot_hartmask in wait_for_coldboot() before
   the coldboot HART acquires the coldboot_lock and sends IPI
   in wake_coldboot_harts() hence the failing HARTs never
   receive IPI from the coldboot HART.

2) Failing HARTs acquire the coldbood_lock and update the
   coldboot_hartmask before coldboot HART does sbi_scratch_init()
   so the sbi_hartmask_set_hartid() does not update the
   coldboot_hartmask on the failing HARTs hence they never
   receive IPI from the coldboot HART.

To address this, use a simple busy-loop in wait_for_coldboot() for
polling on coldboot_done flag.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
14 months agoplatform: generic: allwinner: Optimize current hart scratch access
Samuel Holland [Mon, 18 Mar 2024 14:49:29 +0000 (07:49 -0700)]
platform: generic: allwinner: Optimize current hart scratch access

The address of the local scratch area is stored in each hart's mscratch
CSR. It is more efficient to read the CSR than to compute the address
from the hart ID.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agolib: utils/timer: Optimize current hart scratch access
Samuel Holland [Mon, 18 Mar 2024 14:49:28 +0000 (07:49 -0700)]
lib: utils/timer: Optimize current hart scratch access

The address of the local scratch area is stored in each hart's mscratch
CSR. It is more efficient to read the CSR than to compute the address
from the hart ID.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agoinclude: sbi: Support byteorder macros in assembly
Vivian Wang [Fri, 15 Mar 2024 17:36:39 +0000 (01:36 +0800)]
include: sbi: Support byteorder macros in assembly

Avoid using C types and casts if sbi/sbi_byteorder.h is included in
assembly code

Signed-off-by: Vivian Wang <dramforever@live.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agolib: sbi: Add SSE support for PMU events
Clément Léger [Thu, 21 Mar 2024 15:57:18 +0000 (16:57 +0100)]
lib: sbi: Add SSE support for PMU events

Add SSE callbacks registration to PMU driver in order to disable
interrupt delegation for PMU interrupts. When interrupts are
undelegated send the PMU SSE event upon LCOFIP IRQ.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agolib: sbi: Implement SBI SSE extension
Clément Léger [Thu, 21 Mar 2024 15:57:17 +0000 (16:57 +0100)]
lib: sbi: Implement SBI SSE extension

The SBI SSE extension defines a set of function that can be called to
register and handle supervisor sofwtare events. This patch implements
all of the functionality defined in the specification.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agolib: sbi: Add support for Supervisor Software Events extension
Clément Léger [Thu, 21 Mar 2024 15:57:16 +0000 (16:57 +0100)]
lib: sbi: Add support for Supervisor Software Events extension

This extension [1] allows to deliver events from SBI to supervisor via
a software mechanism. This extension defines events (either local or
global) which are signaled by the SBI on specific signal sources (IRQ,
exceptions, etc) and are injected to be executed in supervisor mode.

[1] https://lists.riscv.org/g/tech-prs/message/798

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agofirmware: remove copy-base relocation
Xiang W [Tue, 12 Mar 2024 08:24:59 +0000 (16:24 +0800)]
firmware: remove copy-base relocation

Remove copy-base relocations that are no longer needed.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agoplatform: generic: allwinner: sun20i-d1: Remove duplicated CSR definitions
Christoph Müllner [Tue, 19 Mar 2024 09:26:42 +0000 (10:26 +0100)]
platform: generic: allwinner: sun20i-d1: Remove duplicated CSR definitions

All T-Head CSRs are already defined in thead/c9xx_encoding.h.
Let's reuse the values from there instead of redefining them with
a slightly different name.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agodocs: writing_tests: Make docs correspond the latest changes
Ivan Orlov [Tue, 19 Mar 2024 16:02:39 +0000 (16:02 +0000)]
docs: writing_tests: Make docs correspond the latest changes

We should store test object files list in the `libsbi-objs-y` Makefile
variable, not in `libsbitests-objs-y`. Update the documentation
correspondingly.

Since we don't use the `console_dev` static variable directly in the
`sbi_console_test` unit test anymore, remove the paragraph which says
that we do.

Fixes: 86224ec36aed ("docs/writing_tests: Update tests paths")
Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agolib: sbi: Extend sbi_trap_error() to dump state in a nested trap
Anup Patel [Tue, 12 Mar 2024 08:22:53 +0000 (13:52 +0530)]
lib: sbi: Extend sbi_trap_error() to dump state in a nested trap

The sbi_trap_error() should dump state of all in-flight traps upon
failure in a nested trap so extend it accordingly.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
14 months agolib: sbi: Pass trap context pointer to sbi_ecall_handler()
Anup Patel [Mon, 11 Mar 2024 12:45:20 +0000 (18:15 +0530)]
lib: sbi: Pass trap context pointer to sbi_ecall_handler()

To be consistent with other trap handlers, pass trap context pointer
to sbi_ecall_handler().

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Clément Léger <cleger@rivosinc.com>
14 months agolib: sbi: Remove regs parameter from trap irq handling functions
Anup Patel [Mon, 11 Mar 2024 12:30:50 +0000 (18:00 +0530)]
lib: sbi: Remove regs parameter from trap irq handling functions

The trap irq handling functions no longer require regs parameter
so remove it.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
14 months agolib: sbi: Remove regs paramter of sbi_irqchip_process()
Anup Patel [Mon, 11 Mar 2024 12:23:41 +0000 (17:53 +0530)]
lib: sbi: Remove regs paramter of sbi_irqchip_process()

The irqchip handlers will typically not need pointer to trap registers
so remove regs parameter of sbi_irqchip_process().

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Clément Léger <cleger@rivosinc.com>
14 months agolib: sbi: Simplify parameters of sbi_illegal_insn_handler()
Anup Patel [Mon, 11 Mar 2024 10:24:56 +0000 (15:54 +0530)]
lib: sbi: Simplify parameters of sbi_illegal_insn_handler()

The struct sbi_trap_context already has the information needed by
sbi_illegal_insn_handler() so directly pass struct sbi_trap_context
pointer to this function.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
14 months agolib: sbi: Simplify parameters of misaligned and access fault handlers
Anup Patel [Mon, 11 Mar 2024 09:51:53 +0000 (15:21 +0530)]
lib: sbi: Simplify parameters of misaligned and access fault handlers

The struct sbi_trap_context already has the information needed by
misaligned load/store and access fault load/store handlers so directly
pass struct sbi_trap_context pointer to these functions.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Clément Léger <cleger@rivosinc.com>
14 months agolib: sbi: Introduce trap context
Anup Patel [Mon, 11 Mar 2024 09:10:12 +0000 (14:40 +0530)]
lib: sbi: Introduce trap context

Club the struct sbi_trap_regs and struct sbi_trap_info a new
struct sbi_trap_context (aka trap context) which must be saved
by low-level trap handler before calling sbi_trap_handler().

To track nested traps, the struct sbi_scratch points to the current
trap context and the trap context has pointer to pervious context
of previous trap.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
14 months agoinclude: sbi: Add trap_context pointer in struct sbi_scratch
Anup Patel [Mon, 11 Mar 2024 07:09:00 +0000 (12:39 +0530)]
include: sbi: Add trap_context pointer in struct sbi_scratch

To track nested traps, the struct sbi_scratch needs a pointer the
current trap context so add trap_context pointer in struct sbi_context.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
14 months agolib: sbi: Remove sbi_trap_exit() and related code
Anup Patel [Mon, 11 Mar 2024 05:34:50 +0000 (11:04 +0530)]
lib: sbi: Remove sbi_trap_exit() and related code

Over the years, no uses of sbi_trap_exit() have been found so remove
it and also remove related code from fw_base.S and sbi_scratch.h.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Tested-by: Samuel Holland <samuel.holland@sifive.com>
14 months agolib: sbi: Remove epc from struct sbi_trap_info
Samuel Holland [Mon, 11 Mar 2024 17:47:50 +0000 (10:47 -0700)]
lib: sbi: Remove epc from struct sbi_trap_info

In the only places this value is used, it duplicates mepc from
struct sbi_trap_regs.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agodocs/writing_tests: Update tests paths
Ivan Orlov [Wed, 13 Mar 2024 15:01:58 +0000 (15:01 +0000)]
docs/writing_tests: Update tests paths

Since the tests should be moved to the lib/sbi/tests directory, the
documentation should be updated correspondingly. So, update the paths
where they have to be changed.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
14 months agolib: tests: Move tests to a separate directory
Ivan Orlov [Wed, 13 Mar 2024 15:01:57 +0000 (15:01 +0000)]
lib: tests: Move tests to a separate directory

Move all of the SBIUnit-related code into the lib/sbi/tests directory.
Update 'Makefile' to index objects from the tests subdirectory.

I don't think creating the full separate list of Makefile variables
(libsbitests-objs-path-y, libsbitests-object-mks, etc. as it is done for
libsbiutils) is necessary for the tests because:

1) `lib/sbi/tests/objects.mk` is already indexed into
'libsbi-objects-mks' since the find expression for the libsbi-object-mks
variable looks for objects.mk files in the nested directories as well).

2) Tests are tightly coupled with the `lib/sbi/` sources, therefore it
may be reasonable to store the list of lib/sbi and lib/sbi/tests object
files together in the libsbi-objs-path-y variable.

Additionally, update relative paths in the tests where necessary.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agolib: sbi: call platform load/store emulators
Bo Gan [Wed, 6 Mar 2024 02:35:40 +0000 (18:35 -0800)]
lib: sbi: call platform load/store emulators

sbi_load/store_access_handler now tries to call platform emulators
if defined. Otherwise, redirects the fault. If the platform code
returns failure, this means the H/S/U has accessed the emulated
devices in an unexpected manner, which is very likely caused by
buggy code in H/S/U. We redirect the fault, so lower privileged
level can get notified, and act accordingly. (E.g., oops in Linux)

We let the handler truly fail if the trap was originated from M mode.
In this case, something must be very wrong and we should just fail.

Signed-off-by: Bo Gan <ganboing@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agoinclude: sbi: add emulate_load/store handler to platform ops
Bo Gan [Wed, 6 Mar 2024 02:35:39 +0000 (18:35 -0800)]
include: sbi: add emulate_load/store handler to platform ops

This patch allows the platform to define load/store emulators. This
enables a platform to trap-and-emulate special devices or filter
access to existing physical devices.

Signed-off-by: Bo Gan <ganboing@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agolib: sbi: abstract out insn decoding to unify mem fault handlers
Bo Gan [Wed, 6 Mar 2024 02:35:38 +0000 (18:35 -0800)]
lib: sbi: abstract out insn decoding to unify mem fault handlers

This patch abstracts out the instruction decoding part of misaligned ld/st
fault handlers, so it can be reused by ld/st access fault handlers.
Also Added lb/lbu/sb decoding. (previously unreachable by misaligned fault)

sbi_trap_emulate_load/store is now the common handler which takes a `emu`
parameter that is responsible for emulating the misaligned or access fault.
The `emu` callback is expected to fixup the fault, and based on the return
code of `emu`, sbi_trap_emulate_load/store will:

  r/wlen => the fixup is successful and regs/mepc needs to be updated.
  0      => the fixup is successful, but regs/mepc should be left untouched
            (this is usually used if `emu` does `sbi_trap_redirect`)
  -err   => failed, sbi_trap_error will be called

For now, load/store access faults are blindly redirected. It will be
enhanced in the following patches.

Signed-off-by: Bo Gan <ganboing@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agolib: sbi: change prototype of sbi_misaligned_load/store_handler
Bo Gan [Wed, 6 Mar 2024 02:35:37 +0000 (18:35 -0800)]
lib: sbi: change prototype of sbi_misaligned_load/store_handler

This simplifies both handlers such that when the handler needs to
redirect the original trap, it's readily available.

Signed-off-by: Bo Gan <ganboing@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agolib: sbi: change prototype of sbi_trap_redirect
Bo Gan [Wed, 6 Mar 2024 02:35:36 +0000 (18:35 -0800)]
lib: sbi: change prototype of sbi_trap_redirect

sbi_trap_redirect now uses const pointer to `trap`.
This ensures the caller that we never change `trap` in sbi_trap_redirect.

Signed-off-by: Bo Gan <ganboing@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agoinclude: sbi: rename sbi_misaligned_ldst.h to sbi_trap_ldst.h
Bo Gan [Wed, 6 Mar 2024 02:35:35 +0000 (18:35 -0800)]
include: sbi: rename sbi_misaligned_ldst.h to sbi_trap_ldst.h

Signed-off-by: Bo Gan <ganboing@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agolib: sbi: rename sbi_misaligned_ldst.c to sbi_trap_ldst.c
Bo Gan [Wed, 6 Mar 2024 02:35:34 +0000 (18:35 -0800)]
lib: sbi: rename sbi_misaligned_ldst.c to sbi_trap_ldst.c

Signed-off-by: Bo Gan <ganboing@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agolib: sbi: Add initial domain context management support
Qingyu Shang [Wed, 6 Mar 2024 11:15:58 +0000 (19:15 +0800)]
lib: sbi: Add initial domain context management support

The domain context management component in OpenSBI provides basic CPU
context management routines for existing OpenSBI domain. As domain
extension, it was initially designed to facilitate the suspension
and resumption of domains, enabling secure domains to efficiently
share CPU resources.

The patch also provides an addition to the OpenSBI domain to provide
updates on hart-domain assignment and declarations of contexts within
the domain.

Signed-off-by: Qingyu Shang <2931013282@sjtu.edu.cn>
Reviewed-by: Yu Chien Peter Lin <peterlin@andestech.com>
Tested-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agolib: tests: Add sbi_console test
Ivan Orlov [Mon, 4 Mar 2024 21:45:51 +0000 (21:45 +0000)]
lib: tests: Add sbi_console test

Add the test suite covering some of the functions from
lib/sbi/sbi_console.c: putc, puts and printf. The test covers a variety
of format specifiers for printf and different strings and characters for
putc and puts.

In order to do that, the test "mocks" the sbi_console_device structure
by setting the 'console_dev' variable to the virtual console.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
15 months agolib: tests: Add a test for sbi_bitmap
Ivan Orlov [Mon, 4 Mar 2024 21:45:50 +0000 (21:45 +0000)]
lib: tests: Add a test for sbi_bitmap

Add test suite covering all of the functions from lib/sbi/sbi_bitmap.c:
__bitmap_and, __bitmap_or and __bitmap_xor.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
15 months agolib: Add SBIUnit testing macros and functions
Ivan Orlov [Mon, 4 Mar 2024 21:45:49 +0000 (21:45 +0000)]
lib: Add SBIUnit testing macros and functions

This patch introduces all of the SBIUnit macros and functions which
can be used during the test development process. Also, it defines
the 'run_all_tests' function, which is being called during the
'init_coldboot' right after printing the boot hart information.

Also, add the CONFIG_SBIUNIT Kconfig entry in order to be able to
turn the tests on and off. When the CONFIG_SBIUNIT is disabled,
the tests and all related code is excluded completely on the
compilation stage.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
15 months agodocs: Add documentation about tests and SBIUnit
Ivan Orlov [Mon, 4 Mar 2024 21:45:48 +0000 (21:45 +0000)]
docs: Add documentation about tests and SBIUnit

This patch contains the documentation for SBIUnit. It describes:

- What is SBIUnit
- Simple test writing scenario
- How we can cover static functions
- How we can "mock" structures in order to test the functions which
operate on them
- SBIUnit API Reference

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
15 months agofirmware: fw_base.S: fix _reset_regs
Xiang W [Mon, 4 Mar 2024 12:15:52 +0000 (20:15 +0800)]
firmware: fw_base.S: fix _reset_regs

a3 and a4 cannot be reset because used in fw_platform_init.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agofirmware: fw_base.S: Remove _relocate_lottery
Xiang W [Mon, 4 Mar 2024 12:15:51 +0000 (20:15 +0800)]
firmware: fw_base.S: Remove _relocate_lottery

Remove _relocate_lottery and use _boot_status instead.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
15 months agofirmware: fw_dynamic.S: Remove _bad_dynamic_info
Xiang W [Mon, 4 Mar 2024 12:15:50 +0000 (20:15 +0800)]
firmware: fw_dynamic.S: Remove _bad_dynamic_info

_bad_dynamic_info is same as _start_hang, so remove it.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>