platform/kernel/opensbi.git
20 months agolib: sbi: Fix pmp_flags for Smepmp read-only shared region
Anup Patel [Thu, 7 Sep 2023 11:49:44 +0000 (17:19 +0530)]
lib: sbi: Fix pmp_flags for Smepmp read-only shared region

The Smepmp read-only shared region must have pmpcfg.L, pmpcfg.R,
pmpcfg.W, and pmpcfg.X bits set so sbi_hart_get_smepmp_flags()
must return pmp_flags accordingly.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
20 months agolib: sbi: Don't clear mseccfg.MML bit in sbi_hart_smepmp_configure()
Anup Patel [Thu, 7 Sep 2023 11:49:43 +0000 (17:19 +0530)]
lib: sbi: Don't clear mseccfg.MML bit in sbi_hart_smepmp_configure()

The mseccfg.MML bit is a sticky bit which remains unchanged once set
so no need to clear it in sbi_hart_smepmp_configure().

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
20 months agolib: sbi: Factor-out Smepmp configuration as separate function
Anup Patel [Thu, 7 Sep 2023 11:49:42 +0000 (17:19 +0530)]
lib: sbi: Factor-out Smepmp configuration as separate function

Let us factor-out Smepmp configuaration as separate function so
that code is more readable.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
20 months agoinclude: sbi_domain: Fix permission test macros
Mayuresh Chitale [Thu, 7 Sep 2023 11:49:41 +0000 (17:19 +0530)]
include: sbi_domain: Fix permission test macros

The macros to test permissions must perform an exact match of all the
bits in the input with the desired permission bits. Otherwise, the check
returns false positives in those cases where only some of the desired
permissions are set in the input.

Fixes: 6c202c5 ("include: sbi: Add Smepmp specific access flags for PMP
entries")
Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoinclude: sbi: Remove sbi_hartmask_for_each_hart() macro
Anup Patel [Sat, 2 Sep 2023 12:04:32 +0000 (17:34 +0530)]
include: sbi: Remove sbi_hartmask_for_each_hart() macro

The sbi_hartmask_for_each_hart() macro is slow and has only one user
so let us completely remove the sbi_hartmask_for_each_hart() macro.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Maximize the use of HART index in sbi_domain
Anup Patel [Sat, 2 Sep 2023 10:46:20 +0000 (16:16 +0530)]
lib: sbi: Maximize the use of HART index in sbi_domain

Let us maximize the use of HART index in sbi_domain because hartindex
based hartmask access and sbi_scratch lookup is faster.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Remove sbi_scratch_last_hartid() macro
Anup Patel [Fri, 1 Sep 2023 16:11:48 +0000 (21:41 +0530)]
lib: sbi: Remove sbi_scratch_last_hartid() macro

The sbi_scratch_last_hartid() macro is not of much use on platforms
with really sparse hartids so let us replace use of this macro with
other approaches.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Prefer hartindex over hartid in IPI framework
Anup Patel [Fri, 1 Sep 2023 12:11:07 +0000 (17:41 +0530)]
lib: sbi: Prefer hartindex over hartid in IPI framework

Let us prefer hartindex over hartid in IPI framework which in-turn
forces IPI users to also prefer hartindex.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Use sbi_scratch_last_hartindex() in remote TLB managment
Anup Patel [Fri, 1 Sep 2023 11:29:58 +0000 (16:59 +0530)]
lib: sbi: Use sbi_scratch_last_hartindex() in remote TLB managment

The sbi_hartid_to_scratch() involves translating hartid to hartindex
which is expensive so let's use sbi_hartindex_to_scratch() instead.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Extend sbi_hartmask to support both hartid and hartindex
Xiang W [Thu, 31 Aug 2023 03:39:30 +0000 (11:39 +0800)]
lib: sbi: Extend sbi_hartmask to support both hartid and hartindex

Currently, the sbi_hartmask is indexed by hartid which puts a
limit on hartid to be less than SBI_HARTMASK_MAX_BITS.

We extend the sbi_hartmask implementation to use hartindex and
support updating sbi_hartmask using hartid. This removes the
limit on hartid and existing code works largely unmodified.

Signed-off-by: Xiang W <wxjstz@126.com>
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Remove sbi_platform_hart_index/invalid() functions
Anup Patel [Fri, 1 Sep 2023 09:13:34 +0000 (14:43 +0530)]
lib: sbi: Remove sbi_platform_hart_index/invalid() functions

The hartid to hartindex mapping is now tracked in sbi_scratch so we
don't need sbi_platform_hart_index() and sbi_platform_hart_invalid()
functions hence let us remove them.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Introduce HART index in sbi_scratch
Anup Patel [Mon, 12 Sep 2022 04:20:43 +0000 (09:50 +0530)]
lib: sbi: Introduce HART index in sbi_scratch

We introduce HART index and related helper functions in sbi_scratch
where HART index is contiguous and each HART index maps to a physical
HART id such that 0 <= HART index and HART index < SBI_HARTMASK_MAX_BITS.

The HART index to HART id mapping follows the index2id mapping provided
by the platform. If the platform does not provide index2id mapping then
identity mapping is assumed.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Implement SET_FS_DIRTY() to make sure the mstatus FS dirty is set
Greentime Hu [Wed, 6 Sep 2023 09:01:14 +0000 (09:01 +0000)]
lib: sbi: Implement SET_FS_DIRTY() to make sure the mstatus FS dirty is set

We found the mstatus.FS status is not set correctly after the SET_F64_REG()
and SET_F32_REG(). We should set mstatus.FS dirty after we emulate the FPU
instructions.

Co-developed-by: Roy Lin <roy.lin@sifive.com>
Signed-off-by: Roy Lin <roy.lin@sifive.com>
Signed-off-by: Greentime Hu <greentime.hu@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi: alloc tlb fifo by sbi_malloc
Xiang W [Wed, 6 Sep 2023 13:10:04 +0000 (21:10 +0800)]
lib: sbi: alloc tlb fifo by sbi_malloc

If the system is defined from tlb_fifo_num_entries, the scratch may be
too small to hold the fifo, so it is alloc through the heap.

Signed-off-by: Xiang W <wxjstz@126.com>
Signed-off-by: Xing Xiaoguang <xiaoguang.xing@sophgo.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agoplatform: Allow platforms to specify the size of tlb fifo
Xiang W [Wed, 6 Sep 2023 13:10:03 +0000 (21:10 +0800)]
platform: Allow platforms to specify the size of tlb fifo

For some platforms with a particularly high number of harts, if the
tlb fifo is too small, it case harts to wait. Platforms should be
allowed to specify the size of the tlb fifo.

Signed-off-by: Xiang W <wxjstz@126.com>
Signed-off-by: Xing Xiaoguang <xiaoguang.xing@sophgo.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi_pmu: keep overflow interrupt of stopped hpm counter disabled
Inochi Amaoto [Tue, 15 Aug 2023 09:40:33 +0000 (17:40 +0800)]
lib: sbi_pmu: keep overflow interrupt of stopped hpm counter disabled

After the hardware hpm counter is stopped, it should not raise any new
interrupt as it is already stopped. So add the hw_counter_disable_irq
callback to allow the custom pmu device to control this behavior.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Samuel Holland <samuel@sholland.org>
21 months agoplatform: generic: allwinner: fix OF process for T-HEAD c9xx pmu
Inochi Amaoto [Tue, 15 Aug 2023 09:40:32 +0000 (17:40 +0800)]
platform: generic: allwinner: fix OF process for T-HEAD c9xx pmu

T-HEAD c9xx pmu needs to clear OV bits of MCOUNTEROF in any condition
to avoid unnecessary OF interrupts.

In addition, the S-mode SCOUNTEROF only have OF bit set when the related
bits of MCOUNTERWEN is set, so also configure MCOUNTERWEN to allow kernel
to access valid SCOUNTEROF.

Signed-off-by: Haijiao Liu <haijiao.liu@sophgo.com>
Co-authored-by: Inochi Amaoto <inochiama@outlook.com>
Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Samuel Holland <samuel@sholland.org>
21 months agolib: sbi_pmu: ensure update hpm counter before starting counting
Inochi Amaoto [Tue, 15 Aug 2023 09:40:31 +0000 (17:40 +0800)]
lib: sbi_pmu: ensure update hpm counter before starting counting

When detecting features of PMU, the hpm counter may be written to some
value, this will cause some unexpected behavior in some cases. So ensure
the hpm counter is updated before starting the counter and the related
interrupt.

Signed-off-by: Haijiao Liu <haijiao.liu@sophgo.com>
Co-authored-by: Inochi Amaoto <inochiama@outlook.com>
Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Samuel Holland <samuel@sholland.org>
21 months agolib: sbi: improve the definition of SBI_IPI_EVENT_MAX
Xiang W [Mon, 21 Aug 2023 14:11:45 +0000 (22:11 +0800)]
lib: sbi: improve the definition of SBI_IPI_EVENT_MAX

The previous definition had the assumption that the machine word length
is equal to the word length of LONG. Remove this assumption and add a
static check to prevent errors in subsequent modifications.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agofirmware: payload: test: Change to SBI v2.0 DBCN ecalls
Inochi Amaoto [Wed, 30 Aug 2023 00:34:43 +0000 (08:34 +0800)]
firmware: payload: test: Change to SBI v2.0 DBCN ecalls

As the the "Console Putchar" extension is already legacy and may
be removed in the furture. So replace it with the SBI v2.0 "DBCN"
extension.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi_pmu: remove mhpm_count field in hart feature
Inochi Amaoto [Fri, 11 Aug 2023 00:24:43 +0000 (08:24 +0800)]
lib: sbi_pmu: remove mhpm_count field in hart feature

After supporting noncontigous hpm event and counters in opensbi, the
number of hpm counters can be calculated by the mhpm_mask. So this field
is unnecessary and can be removed to save some space.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agoplatform: generic: allwinner: correct mhpmevent count
Inochi Amaoto [Fri, 11 Aug 2023 00:13:01 +0000 (08:13 +0800)]
platform: generic: allwinner: correct mhpmevent count

Only the CSR mhpmevent 3-9,13-17 of D1 have valid function, so change
the mhpm_mask to a valid value to avoid invalid usage.

Due to the openc906 pmu code
https://github.com/T-head-Semi/openc906/blob/main/C906_RTL_FACTORY/gen_rtl/pmu/rtl/aq_hpcp_top.v

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agoMakefile: Fix grep warning
Andrew Jones [Thu, 10 Aug 2023 12:15:28 +0000 (14:15 +0200)]
Makefile: Fix grep warning

grep (at least my version, grep-3.8-3.fc38.x86_64) warns with
"grep: warning: stray \ before -". Fix the warning by making
the command line input to grep less ambiguous.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: sbi: Add support for smcntrpmf
Kaiwen Xue [Thu, 20 Jul 2023 21:07:02 +0000 (14:07 -0700)]
lib: sbi: Add support for smcntrpmf

This adds the support for ISA extension smcntrpmf. When some inhibit flags
are set by a lower privilege mode for new CSRs added by smcntrpmf, OpenSBI
sets the appropriate values correspondingly.

Signed-off-by: Kaiwen Xue <kaiwenx@andrew.cmu.edu>
Signed-off-by: Kaiwen Xue <kaiwenx@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
22 months agofw_base.S: Fix assembler error with clang 16+
Mitchell Horne [Sun, 6 Aug 2023 05:15:36 +0000 (10:45 +0530)]
fw_base.S: Fix assembler error with clang 16+

Attempting to build OpenSBI with clang 16 and the following command:

   $ make LLVM=1 PLATFORM=generic

Results in the following error:

    AS        platform/generic/firmware/fw_dynamic.o
   /tmp/fw_dynamic-d000a6.s:429:9: error: symbol '_fw_start' can not be undefined in a subtraction expression
    .dword _fw_rw_start - _fw_start

Work around this issue by eliminating the __fw_rw_offset variable and
performing the offset calculation at run-time instead. This takes
advantage of the fact that the a4 register contains the value of
_fw_start.

Signed-off-by: Mitchell Horne <mhorne@FreeBSD.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: sbi: Fix typo for finding fixed event counter
Kaiwen Xue [Thu, 3 Aug 2023 18:34:15 +0000 (11:34 -0700)]
lib: sbi: Fix typo for finding fixed event counter

Cycle and instructions are hardware events instead of firmware ones. Fix
the typo in the name of this function.

Signed-off-by: Kaiwen Xue <kaiwenx@andrew.cmu.edu>
Signed-off-by: Kaiwen Xue <kaiwenx@rivosinc.com>
Reviewed-by: Anup patel <anup@brainfault.org>
22 months agolib: utils/fdt: Fix fdt_parse_isa_extensions() implementation
Anup Patel [Thu, 3 Aug 2023 07:11:01 +0000 (12:41 +0530)]
lib: utils/fdt: Fix fdt_parse_isa_extensions() implementation

Currently, the fdt_parse_isa_extensions() tries to parse the ISA
string once for each HART. This ISA string parsing can fail for
secondary HARTs if the FDT memory is already overwritten by the
supervisor OS.

To tackle this issue, we improve the fdt_parse_isa_extensions()
implementation to pre-parse ISA string for all HARTs during
cold boot.

Fixes: d72f5f17478d ("lib: utils: Add detection of Smepmp from ISA
string in FDT")
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Tested-By: Mayuresh Chitale<mchitale@ventanamicro.com>
22 months agolib: sbi_pmu: Enable noncontigous hpm event and counters
Mayuresh Chitale [Wed, 2 Aug 2023 11:15:23 +0000 (16:45 +0530)]
lib: sbi_pmu: Enable noncontigous hpm event and counters

Platforms may implement hpm events/counters non contiguously but the current
implementation assumes them to be always contigous. Add a bitmap that
captures the hpm events/counters as implemented in the hardware and use
it to set the max limit of hardware counters visible to the OS. Counters
not implemented in the hardware can't be used by the OS because those
wont be described in the DT.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agogitignore: allow gitignore to ignore most dot file
Inochi Amaoto [Tue, 1 Aug 2023 11:41:19 +0000 (19:41 +0800)]
gitignore: allow gitignore to ignore most dot file

Nowadays, most of the editor use files or directories begin with dot to
store some settings. So let git ignore these files and directories to
reduce potential mistakes.

Add dot match to ignore any editor file and there are two exceptions:
- .gitignore
- .clang-format

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: utils/reset: Remove SiFive Test reset driver
Anup Patel [Fri, 21 Jul 2023 12:50:27 +0000 (18:20 +0530)]
lib: utils/reset: Remove SiFive Test reset driver

The functionality of SiFive Test reset driver is easily available
through Syscon reset driver so let us remove the SiFive Test driver.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
22 months agolib: utils/reset: Add syscon based reboot and poweroff
Anup Patel [Fri, 21 Jul 2023 12:41:52 +0000 (18:11 +0530)]
lib: utils/reset: Add syscon based reboot and poweroff

Let us have common FDT based reset driver for syscon reboot and
poweroff. The device tree bindings for syscon reboot and poweroff
are already available in the Linux kernel sources.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
22 months agolib: utils/regmap: Add simple FDT based syscon regmap driver
Anup Patel [Fri, 21 Jul 2023 08:10:03 +0000 (13:40 +0530)]
lib: utils/regmap: Add simple FDT based syscon regmap driver

Let us add a simple FDT based system regmap driver which follows the
device tree bindings already defined in the Linux kernel.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
22 months agolib: utils/regmap: Add simple FDT based regmap framework
Anup Patel [Fri, 21 Jul 2023 08:09:57 +0000 (13:39 +0530)]
lib: utils/regmap: Add simple FDT based regmap framework

We add a simple FDT based regmap framework which is built on top of
generic regmap library. The phandle of FDT regmap DT node is treated
as unique regmap ID. The FDT based regmap drivers will be probed
on-demand from fdt_regmap_get_by_phandle() and fdt_regmap_get()
called by the regmap client drivers.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
22 months agolib: utils/regmap: Add generic regmap access library
Anup Patel [Fri, 21 Jul 2023 08:09:50 +0000 (13:39 +0530)]
lib: utils/regmap: Add generic regmap access library

We add generic regmap access library which is independent of
hardware description format (FDT or ACPI). The OpenSBI platform
support or regmap drivers can register regmap instances which
can be discovered by different regmap clients based on the
unique ID of regmap instances.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
22 months agoinclude: sbi_utils: Remove driver pointer from struct i2c_adapter
Anup Patel [Fri, 21 Jul 2023 08:09:43 +0000 (13:39 +0530)]
include: sbi_utils: Remove driver pointer from struct i2c_adapter

The "driver" pointer in struct i2c_adapter is not used anywhere
so let us remove it.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
22 months agoinclude: sbi: fix CSR define of mseccfg
Xiang W [Fri, 14 Jul 2023 10:08:34 +0000 (18:08 +0800)]
include: sbi: fix CSR define of mseccfg

Because the CSR names in the spec are mseccfg and mseccfgh. Remove
CSR_MSECCFG_LOWER and CSR_MSECCFG_UPPER and directly define
CSR_MSECCFG and CSR_MSECCFGH.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: sbi: Update system suspend to spec
Andrew Jones [Thu, 20 Jul 2023 15:48:37 +0000 (17:48 +0200)]
lib: sbi: Update system suspend to spec

commit 68e66106120f ("SUSP: Add SBI_ERR_DENIED") of the SBI spec adds
a new error code, SBI_ERR_DENIED, which is returned when entry criteria
has not be meant. Update the system suspend implementation to return
this error when it has detected that not all harts are in the STOPPED
state.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolibfdt: fix SPDX license identifiers
Heinrich Schuchardt [Fri, 14 Jul 2023 11:07:36 +0000 (13:07 +0200)]
libfdt: fix SPDX license identifiers

License identifiers should be machine readable.

According to the SPDX v2.3.0 specification annex E parentheses are not
used in the SPDX identifier field when specifying multiple licenses [1].

[1] https://spdx.github.io/spdx-spec/v2.3/using-SPDX-short-identifiers-in-source-files/

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Map/Unmap debug console shared memory buffers
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:36 +0000 (10:04 +0530)]
lib: sbi: Map/Unmap debug console shared memory buffers

With Smepmp enabled, it is necessary for shared memory from
S/U mode to be mapped/unmapped before and after read/write
of the memory region. This patch maps the debug console
shared memory before accessing it.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Add functions to map/unmap shared memory
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:35 +0000 (10:04 +0530)]
lib: sbi: Add functions to map/unmap shared memory

When Smepmp is enabled, M-mode will need to map/unmap the
shared memory before it can read/write to it. This patch
adds functions to create dynamic short-lived mappings.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Add support for Smepmp
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:33 +0000 (10:04 +0530)]
lib: sbi: Add support for Smepmp

- If Smepmp is enabled, the access flags of an entry are determined based on
  truth table defined in Smepmp.
- First PMP entry (index 0) is reserved.
- Existing boot PMP entries start from index 1.
- Since enabling Smepmp revokes the access privileges of the M-mode software
  on S/U-mode region, first PMP entry is used to map/unmap the shared memory
  between M and S/U-mode. This allows a temporary access window for the M-mode
  software to read/write to S/U-mode memory region.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Change the order of PMP initialization
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:34 +0000 (10:04 +0530)]
lib: sbi: Change the order of PMP initialization

Configure PMP at last when all other initializations have been done.
Because if SMEPMP is detected, M-mode access to the S/U space will be
rescinded.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Grant SU R/W/X permissions to whole memory
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:32 +0000 (10:04 +0530)]
lib: sbi: Grant SU R/W/X permissions to whole memory

Since pmp entries have implicit priority on index, previous entries will
deny access to SU on M-mode region. Also, M-mode will not have access to
SU region while previous entries will allow access to M-mode regions.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: utils: Add detection of Smepmp from ISA string in FDT
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:31 +0000 (10:04 +0530)]
lib: utils: Add detection of Smepmp from ISA string in FDT

- Add function to parse ISA string in FDT.
- Set Smepmp feature bit in extensions if "smepmp" string is found in ISA string.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Add smepmp in hart extensions
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:30 +0000 (10:04 +0530)]
lib: sbi: Add smepmp in hart extensions

- Add Smepmp as extension in sbi_hart_extensions enum
- Return "smepmp" string for Smepmp extension from sbi_hart_extension_id2string

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agoinclude: sbi: Add Smepmp specific access flags for PMP entries
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:29 +0000 (10:04 +0530)]
include: sbi: Add Smepmp specific access flags for PMP entries

Smepmp specification defines a truth table based on which the access is allowed to
different modes. This patch adds different flags based on this truth table.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Add functions to manipulate PMP entries
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:28 +0000 (10:04 +0530)]
lib: sbi: Add functions to manipulate PMP entries

- Add a function to disable a given PMP entry.
- Add a function to check if a given entry is disabled.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agoinclude: sbi: Add macro definitions for mseccfg CSR
Himanshu Chauhan [Wed, 12 Jul 2023 04:34:27 +0000 (10:04 +0530)]
include: sbi: Add macro definitions for mseccfg CSR

- Add macros for Machine Security Configuration (mseccfg) CSR
- Add macros to access/manipulate bits in msecfg CSR

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: utils/gpio: Fix RV32 compile error for designware GPIO driver
Anup Patel [Tue, 11 Jul 2023 03:30:31 +0000 (09:00 +0530)]
lib: utils/gpio: Fix RV32 compile error for designware GPIO driver

Currently, we see following compile error in the designeware GPIO driver
for RV32 systems:

lib/utils/gpio/fdt_gpio_designware.c:115:20: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
  115 |         chip->dr = (void *)addr + (bank * 0xc);
      |                    ^
lib/utils/gpio/fdt_gpio_designware.c:116:21: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
  116 |         chip->ext = (void *)addr + (bank * 4) + 0x50;

We fix the above error using an explicit type-cast to 'unsigned long'.

Fixes: 7828eebaaa77 ("gpio/desginware: add Synopsys DesignWare APB GPIO support")
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
23 months agolib: sbi: Fix missing '\0' when buffer szie equal 1
Xiang W [Sun, 9 Jul 2023 16:03:26 +0000 (00:03 +0800)]
lib: sbi: Fix missing '\0' when buffer szie equal 1

Fix special case: sbi_snprintf(out, out_len, ...) when out_len equal
1, The previous code will not fill the buffer with any char.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Fix timing of clearing tbuf
Xiang W [Sun, 9 Jul 2023 16:02:30 +0000 (00:02 +0800)]
lib: sbi: Fix timing of clearing tbuf

A single scan of the format char may add multiple characters to the
tbuf, causing a buffer overflow. You should check if tbuf is full in
printc so that it does not cause a buffer overflow.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Fix printc
Xiang W [Sun, 9 Jul 2023 16:02:29 +0000 (00:02 +0800)]
lib: sbi: Fix printc

Because *out needs to reserve a byte to hold '\0', no more characters
should be added to the buffer when *out has one byte left, and the
buffer size *out_len should not be modified. this patch prevents
the correction of *out_len when *out_len is 1.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Simplify prints
Xiang W [Sun, 9 Jul 2023 16:02:28 +0000 (00:02 +0800)]
lib: sbi: Simplify prints

When doing width = width - strlen(string) in prints there is no need
to consider the case that witdh may be less than 0. This is because
the code to do filling needs to be executed under the condition that
width > 0.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Fix printi
Xiang W [Sun, 9 Jul 2023 16:02:27 +0000 (00:02 +0800)]
lib: sbi: Fix printi

Fix two bug:
> printf("%#08x", 0x123); /* print 0000x123 */
> printf("%#x", 0); /* print 0x0 */

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: print add 'o' type
Xiang W [Sun, 9 Jul 2023 16:02:26 +0000 (00:02 +0800)]
lib: sbi: print add 'o' type

Add o type for print to print octal numbers

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: implifying the parameters of printi
Xiang W [Sun, 9 Jul 2023 16:02:25 +0000 (00:02 +0800)]
lib: sbi: implifying the parameters of printi

The information of sg/b/letbase can be obtained by the type character,
simplifying the parameter by passing the type directly.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Add ' ' '\'' flags for print
Xiang W [Sun, 9 Jul 2023 16:02:24 +0000 (00:02 +0800)]
lib: sbi: Add ' ' '\'' flags for print

The space flag is used to add a space before positive numbers, and
apostrophe is used to print the thousand separator. Add code to
ignore these two flags

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Add '+' flags for print
Xiang W [Sun, 9 Jul 2023 16:02:23 +0000 (00:02 +0800)]
lib: sbi: Add '+' flags for print

Adds + flags for print, prefixing positive numbers with + when this
flags is present

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: print not fill '0' when left-aligned
Xiang W [Sun, 9 Jul 2023 16:02:22 +0000 (00:02 +0800)]
lib: sbi: print not fill '0' when left-aligned

Left alignment and padding '0' should not exist at the same time,
this patch skips padding.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Fix how print gets flags
Xiang W [Sun, 9 Jul 2023 16:02:21 +0000 (00:02 +0800)]
lib: sbi: Fix how print gets flags

The flags for print should be able to appear in any order. The
previous code required the order to be fixed.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Fix Priv spec version for [m|s]counteren and mcountinhibit CSRs
Ley Foon Tan [Thu, 6 Jul 2023 06:30:28 +0000 (14:30 +0800)]
lib: sbi: Fix Priv spec version for [m|s]counteren and mcountinhibit CSRs

Fix Priv spec version typo in commit d4b563c881d6 ("lib: sbi: Remove MCOUNTEREN
and SCOUNTEREN hart features").

At least Priv spec v1.11 is required for [m|s]counteren and mcountinhibit CSRs.

Fixes: d4b563c881d6 ("lib: sbi: Remove MCOUNTEREN and SCOUNTEREN hart features")
Signed-off-by: Ley Foon Tan <leyfoon.tan@starfivetech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: Rename hart_pmu_get_allowed_bits() function
Anup Patel [Wed, 5 Jul 2023 07:04:31 +0000 (12:34 +0530)]
lib: sbi: Rename hart_pmu_get_allowed_bits() function

The hart_pmu_get_allowed_bits() function detects implemented bits
of mhpm counters so let us rename this function accordingly.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
23 months agolib: sbi: Alphabetically sort HART ISA extensions
Anup Patel [Wed, 5 Jul 2023 06:53:57 +0000 (12:23 +0530)]
lib: sbi: Alphabetically sort HART ISA extensions

Let us follow alphabetical order for HART ISA extension so that
it is simpler to maintain.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
23 months agolib: sbi: Add Zihpm as a HART ISA extension
Anup Patel [Wed, 5 Jul 2023 06:45:56 +0000 (12:15 +0530)]
lib: sbi: Add Zihpm as a HART ISA extension

Recently ratified Zihpm ISA extension covers all [m]hpm* CSRs
so we add Zihpm as a HART ISA extension in OpenSBI.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
23 months agolib: sbi: Add Zicntr as a HART ISA extension
Anup Patel [Wed, 5 Jul 2023 06:27:17 +0000 (11:57 +0530)]
lib: sbi: Add Zicntr as a HART ISA extension

Recently ratified Zicntr ISA extension covers cycle, time and
instret CSRs so we replace the "time" ISA extension with "zicntr"
ISA extension in OpenSBI.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
23 months agolib: utils: Fix sbi_hartid_to_scratch() usage in ACLINT drivers
Anup Patel [Wed, 5 Jul 2023 06:45:15 +0000 (12:15 +0530)]
lib: utils: Fix sbi_hartid_to_scratch() usage in ACLINT drivers

The cold_init() functions of ACLINT drivers should skip the HART
if sbi_hartid_to_scratch() returns NULL because we might be dealing
with a HART that is disabled in the device tree.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
23 months agogpio/desginware: add Synopsys DesignWare APB GPIO support
Ben Dooks [Wed, 1 Mar 2023 12:25:25 +0000 (12:25 +0000)]
gpio/desginware: add Synopsys DesignWare APB GPIO support

Add a driver for the Synopsys DesignWare APB GPIO IP block found in many
SoCs.

Signed-off-by: Ben Dooks <ben.dooks@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi_pmu: Avoid out of bounds access
Heinrich Schuchardt [Mon, 3 Jul 2023 13:43:18 +0000 (15:43 +0200)]
lib: sbi_pmu: Avoid out of bounds access

On a misconfigured system we could access phs->active_events[] out of
bounds. Check that num_hw_ctrs is less or equal SBI_PMU_HW_CTR_MAX.

Addresses-Coverity-ID: 1566113 ("Out-of-bounds read")
Addresses-Coverity-ID: 1566114 ("Out-of-bounds write")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: sbi: fix comment indent
Gianluca Guida [Thu, 29 Jun 2023 14:24:08 +0000 (15:24 +0100)]
lib: sbi: fix comment indent

Use tabs rather than spaces.

Signed-off-by: Gianluca Guida <gianluca@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agoinclude: Bump-up version to 1.3 master v1.3
Anup Patel [Fri, 23 Jun 2023 05:31:49 +0000 (11:01 +0530)]
include: Bump-up version to 1.3

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

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
23 months agolib: sbi: check A2 register in ecall_dbcn_handler.
Gianluca Guida [Thu, 22 Jun 2023 17:38:59 +0000 (18:38 +0100)]
lib: sbi: check A2 register in ecall_dbcn_handler.

Do not ignore register A2 (high bits of physical address) in the dbcn
handler (RV64).

Signed-off-by: Gianluca Guida <gianluca@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agolib: reset: Move fdt_reset_init into generic_early_init
Guo Ren [Sun, 18 Jun 2023 15:07:16 +0000 (11:07 -0400)]
lib: reset: Move fdt_reset_init into generic_early_init

The fdt_reset_thead driver needs to modify the __reset_thead_csr_stub
text region for the secondary harts booting. After that, the
sbi_hart_pmp_configure may lock down the text region with M_READABLE &
M_EXECUTABLE attributes in the future. Currently, the M_READABLE &
M_EXECUtABLE have no effect on m-mode, the L-bit in pmpcfg csr is
useless for the current opensbi scenario. See:

Priv-isa-spec 3.7.1.2. Locking and Privilege Mode
When the L bit is clear, any M-mode access matching the PMP entry will
succeed; the R/W/X permissions apply only to S and U modes.

That's why current fdt_reset_thead could still work well after commit:
230278dcf127 ("lib: sbi: Add separate entries for firmware RX and RW
regions"). So this patch fixes up a fake bug for the M-mode permission
setting of the future.

Fixes: 230278dcf127 ("lib: sbi: Add separate entries for firmware RX and RW regions")
Link: http://lists.infradead.org/pipermail/opensbi/2023-June/005176.html
Reported-by: Jessica Clarke <jrtc27@jrtc27.com>
Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
Signed-off-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
23 months agofirmware: Fix find hart index
Xiang W [Fri, 16 Jun 2023 07:03:51 +0000 (15:03 +0800)]
firmware: Fix find hart index

After the loop to find the hartid is launched, assigning -1 to
index will fail in the subsequent compare instruction bge. Fix
This.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
2 years agoplatform/lib: Set no-map attribute on all PMP regions
Alexandre Ghiti [Wed, 14 Jun 2023 08:20:39 +0000 (10:20 +0200)]
platform/lib: Set no-map attribute on all PMP regions

This reverts commit 6966ad0abe70 ("platform/lib: Allow the OS to map the
regions that are protected by PMP").

It was thought at the time of this commit that allowing the kernel to map
PMP protected regions was safe but it is actually not: for example, the
hibernation process will try to access any linear mapping page and then
will fault on such mapped PMP regions [1]. Another issue is that the
device tree specification [2] states that a !no-map region must be
declared as EfiBootServicesData/Code in the EFI memory map which would make
the PMP protected regions reclaimable by the kernel. And to circumvent
this, RISC-V edk2 diverges from the DT specification to declare those
regions as EfiReserved.

The no-map attribute was removed to allow the kernel to use hugepages
larger than 2MB to map the linear mapping to improve the performance but
actually a recent talk from Mike Rapoport [3] stated that the
performance benefit was marginal.

For all those reasons, let's mark all the PMP protected regions as "no-map".

[1] https://lore.kernel.org/linux-riscv/CAAYs2=gQvkhTeioMmqRDVGjdtNF_vhB+vm_1dHJxPNi75YDQ_Q@mail.gmail.com/
[2] "3.5.4 /reserved-memory and UEFI" https://github.com/devicetree-org/devicetree-specification/releases/download/v0.4-rc1/devicetree-specification-v0.4-rc1.pdf
[3] https://lwn.net/Articles/931406/

Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Xiang W <wxjstz@126.com>
2 years agoREADME.md: Improve project copyright information
Anup Patel [Fri, 9 Jun 2023 11:59:10 +0000 (17:29 +0530)]
README.md: Improve project copyright information

Over-time a lot of organizations and individuals have contributed to
the OpenSBI project so let us add copyright RISC-V International to
respect the contributions from all RISC-V members.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agodocs: Add OpenSBI logo and use it in the top-level README.md
Anup Patel [Fri, 9 Jun 2023 11:51:08 +0000 (17:21 +0530)]
docs: Add OpenSBI logo and use it in the top-level README.md

We do have an official OpenSBI logo which was designed few months ago
and was also approved by RISC-V International. Lets add this logo
under docs and also use it in the top-level README.md

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/irqchip: Use scratch space to save per-HART IMSIC pointer
Anup Patel [Thu, 20 Apr 2023 04:48:08 +0000 (10:18 +0530)]
lib: utils/irqchip: Use scratch space to save per-HART IMSIC pointer

Instead of using a global array indexed by hartid, we should use
scratch space to save per-HART IMSIC pointer and IMSIC file number.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/irqchip: Don't check hartid in imsic_update_hartid_table()
Anup Patel [Thu, 20 Apr 2023 04:44:43 +0000 (10:14 +0530)]
lib: utils/irqchip: Don't check hartid in imsic_update_hartid_table()

The imsic_map_hartid_to_data() already checks hartid before using
so we don't need to check in imsic_update_hartid_table().

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/irqchip: Use scratch space to save per-HART PLIC pointer
Anup Patel [Wed, 19 Apr 2023 15:59:50 +0000 (21:29 +0530)]
lib: utils/irqchip: Use scratch space to save per-HART PLIC pointer

Instead of using a global array indexed by hartid, we should use
scratch space to save per-HART PLIC pointer and PLIC context numbers.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/timer: Use scratch space to save per-HART MTIMER pointer
Anup Patel [Wed, 19 Apr 2023 15:57:27 +0000 (21:27 +0530)]
lib: utils/timer: Use scratch space to save per-HART MTIMER pointer

Instead of using a global array indexed by hartid, we should use
scratch space to save per-HART MTIMER pointer.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/ipi: Use scratch space to save per-HART MSWI pointer
Anup Patel [Wed, 19 Apr 2023 15:53:53 +0000 (21:23 +0530)]
lib: utils/ipi: Use scratch space to save per-HART MSWI pointer

Instead of using a global array indexed by hartid, we should use
scratch space to save per-HART MSWI pointer.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/fdt: Use heap in FDT domain parsing
Anup Patel [Thu, 20 Apr 2023 12:39:55 +0000 (18:09 +0530)]
lib: utils/fdt: Use heap in FDT domain parsing

Let's use heap allocation in FDT domain parsing instead of using
a fixed size global array.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/timer: Use heap in ACLINT MTIMER driver
Anup Patel [Wed, 19 Apr 2023 11:43:22 +0000 (17:13 +0530)]
lib: utils/timer: Use heap in ACLINT MTIMER driver

Let's use heap allocation in ACLINT MTIMER driver instead of using
a fixed size global array.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/irqchip: Use heap in PLIC, APLIC and IMSIC drivers
Anup Patel [Wed, 19 Apr 2023 11:25:59 +0000 (16:55 +0530)]
lib: utils/irqchip: Use heap in PLIC, APLIC and IMSIC drivers

Let's use heap allocation in PLIC, APLIC, and IMSIC irqchip drivers
instead of using a fixed size global array.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/ipi: Use heap in ACLINT MSWI driver
Anup Patel [Wed, 19 Apr 2023 11:24:13 +0000 (16:54 +0530)]
lib: utils/ipi: Use heap in ACLINT MSWI driver

Let's use heap allocation in ACLINT MSWI driver instead of using
a fixed size global array.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/i2c: Use heap in DesignWare and SiFive I2C drivers
Anup Patel [Wed, 19 Apr 2023 11:22:44 +0000 (16:52 +0530)]
lib: utils/i2c: Use heap in DesignWare and SiFive I2C drivers

Let's use heap allocation in DesignWare and SiFive I2C drivers
instead of using a fixed size global array.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: utils/gpio: Use heap in SiFive and StartFive GPIO drivers
Anup Patel [Wed, 19 Apr 2023 11:19:19 +0000 (16:49 +0530)]
lib: utils/gpio: Use heap in SiFive and StartFive GPIO drivers

Let's use heap allocation in SiFive and Starfive GPIO drivers
instead of using a fixed size global array.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: sbi: Use scratch space to save per-HART domain pointer
Anup Patel [Tue, 25 Apr 2023 03:42:36 +0000 (09:12 +0530)]
lib: sbi: Use scratch space to save per-HART domain pointer

Instead of using a global array indexed by hartid, we should use
scratch space to save per-HART domain pointer.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: sbi: Use heap for root domain creation
Anup Patel [Fri, 21 Apr 2023 08:18:37 +0000 (13:48 +0530)]
lib: sbi: Use heap for root domain creation

Let's use heap allocation in root domain creation instead of using
a fixed size global array.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: sbi_pmu: Use heap for per-HART PMU state
Anup Patel [Wed, 19 Apr 2023 08:10:19 +0000 (13:40 +0530)]
lib: sbi_pmu: Use heap for per-HART PMU state

Instead of using a global array for per-HART PMU state, we should
use heap to on-demand allocate per-HART PMU state when the HART
is initialized in cold boot or warm boot path.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: sbi: Print scratch size and usage at boot time
Anup Patel [Thu, 20 Apr 2023 08:32:37 +0000 (14:02 +0530)]
lib: sbi: Print scratch size and usage at boot time

The scratch space being a scarce resource so let us print it's
size and usage at boot time.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agolib: sbi: Introduce simple heap allocator
Anup Patel [Tue, 18 Apr 2023 13:08:21 +0000 (18:38 +0530)]
lib: sbi: Introduce simple heap allocator

We provide simple heap allocator to manage the heap space provided
by OpenSBI firmware and platform.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agoplatform: Allow platforms to specify heap size
Anup Patel [Mon, 17 Apr 2023 05:58:03 +0000 (11:28 +0530)]
platform: Allow platforms to specify heap size

We extend struct sbi_platform and struct sbi_scratch to allow platforms
specify the heap size to the OpenSBI firmwares. The OpenSBI firmwares
will use this information to determine the location of heap and provide
heap base address in per-HART scratch space.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
2 years agoinclude: sbi_scratch: Add helper macros to access data type
Anup Patel [Mon, 5 Jun 2023 06:52:37 +0000 (12:22 +0530)]
include: sbi_scratch: Add helper macros to access data type

Reading and writing a data type in scratch space is a very common
use-case so let us add related helper macros in sbi_scratch.h.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
2 years agolib: sbi: Align system suspend errors with spec
Andrew Jones [Thu, 1 Jun 2023 07:48:10 +0000 (09:48 +0200)]
lib: sbi: Align system suspend errors with spec

The spec says sbi_system_suspend() will return SBI_ERR_INVALID_PARAM
when "sleep_type is reserved or is platform-specific and unimplemented"
and SBI_ERR_NOT_SUPPORTED when sleep_type "is not reserved and is
implemented, but the platform does not support it due to one or more
missing dependencies." Ensure SBI_ERR_INVALID_PARAM is returned for
reserved sleep types and that the system suspend driver can choose
which of the two error types to return itself by returning an error
from its check function rather than a boolean.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
2 years agolib: utils/ipi: buffer overrun aclint_mswi_cold_init
Heinrich Schuchardt [Mon, 29 May 2023 09:27:20 +0000 (11:27 +0200)]
lib: utils/ipi: buffer overrun aclint_mswi_cold_init

The parameter checks in aclint_mswi_cold_init() don't guard against a
buffer overrun.

mswi_hartid2data is defined as an array of SBI_HARTMASK_MAX_BITS entries.
The current check allows

    mswi->hart_count = ACLINT_MSWI_MAX_HARTS
    mswi->first_hartid = SBI_HARTMASK_MAX_BITS - 1.

With these values mswi_hartid2data will be accessed at index

    SBI_HARTMASK_MAX_BITS + SBI_HARTMASK_MAX_BITS - 2.

We have to check the sum of mswi->first_hartid and mswi->hart_count.

Furthermore mswi->hart_count = 0 would not make much sense.

Addresses-Coverity-ID: 1529705 ("Out-of-bounds write")
Fixes: 5a049fe1d6a5 ("lib: utils/ipi: Add ACLINT MSWI library")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
2 years agolib: utils: Improve fdt_timer
Xiang W [Mon, 22 May 2023 05:18:09 +0000 (13:18 +0800)]
lib: utils: Improve fdt_timer

Remove dummy driver. Optimize fdt_timer_cold_init to exit the
loop early.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
2 years agolib: utils: Improve fdt_ipi
Xiang W [Mon, 22 May 2023 05:18:08 +0000 (13:18 +0800)]
lib: utils: Improve fdt_ipi

Remove dummy driver. Optimize fdt_ipi_cold_init to exit the loop
early.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
2 years agolib: utils: Improve fdt_serial_init
Xiang W [Mon, 22 May 2023 05:18:07 +0000 (13:18 +0800)]
lib: utils: Improve fdt_serial_init

A final check of all DT nodes does not necessarily find a match, so
SBI_ENODEV needs to be returned. Optimize removal of current_driver.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
2 years agolib: sbi: Fix return of sbi_console_init
Xiang W [Mon, 22 May 2023 05:18:06 +0000 (13:18 +0800)]
lib: sbi: Fix return of sbi_console_init

console is not a required peripheral. So it should return success when
the console does not exist.

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