platform/kernel/opensbi.git
17 months agoinclude: Bump-up version to 1.4 upstream v1.4
Anup Patel [Wed, 27 Dec 2023 07:02:58 +0000 (12:32 +0530)]
include: Bump-up version to 1.4

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

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Anup Patel <anup@brainfault.org>
17 months agolib: sbi: Detect Zicntr extension only based on traps
Anup Patel [Wed, 27 Dec 2023 06:55:09 +0000 (12:25 +0530)]
lib: sbi: Detect Zicntr extension only based on traps

OpenSBI uses time CSR if Zicntr extension present which causes
it to crash on an older QEMU because QEMU generates Zicntr in
the ISA string for unleashed machine which only has CYCLE and
INSTRET counters.

Fixes: 776770d2adbf ("lib: sbi: Using one array to define the
name of extensions")
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Anup Patel <anup@brainfault.org>
17 months agolib: utils/timer: mtimer: only use regname for aclint
Inochi Amaoto [Wed, 27 Dec 2023 00:46:11 +0000 (08:46 +0800)]
lib: utils/timer: mtimer: only use regname for aclint

The parser will fail if the timer is clint timer and has regname
property. As the regname is only meaningful for aclint, it is more
robust to only check regname for aclint timer.

Fixes: 6112d58 ("lib: utils/fdt: Allow to use reg-names when parsing ACLINT")
Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
17 months agolib: sbi: Fix shift bug in sbi_system_reset
Xiang W [Thu, 21 Dec 2023 01:44:27 +0000 (09:44 +0800)]
lib: sbi: Fix shift bug in sbi_system_reset

There is a problem with judging whether the current hart belongs to
hmask. If cur_hartid minus hbase is greater than BITS_PER_LONG, the
previous hmask will also have a bit cleared incorrectly, which will
cause some harts to lose ipi.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
17 months agolib: sbi: Do not enter OpenSBI with mseccfg.MML == 1
Anup Patel [Tue, 12 Dec 2023 09:49:11 +0000 (15:19 +0530)]
lib: sbi: Do not enter OpenSBI with mseccfg.MML == 1

On platforms with Smepmp, the previous booting stage must enter
OpenSBI with mseccfg.MML == 0. This allows OpenSBI to configure
it's own M-mode only regions without depending on the previous
booting stage.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
17 months agolib: sbi: Remove the SBI_ETRAP error code
Anup Patel [Mon, 11 Dec 2023 15:41:03 +0000 (21:11 +0530)]
lib: sbi: Remove the SBI_ETRAP error code

The SBI_ETRAP error code was introduced only for doing trap
redirection in generic sbi_ecall_handler(). Now the trap
redirection is moved into sbi_ecall_legacy.c and SBI_ETRAP
error code is only used in this source file so let us remove
it.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
17 months agolib: sbi: Allow ecall handlers to directly update register state
Anup Patel [Mon, 11 Dec 2023 15:29:14 +0000 (20:59 +0530)]
lib: sbi: Allow ecall handlers to directly update register state

Some of the upcoming SBI extensions (such as SSE) will directly
update register state so improve the prototype of ecall handler
to accommodate this. Further, this flexibility allows us to
push the trap redirection from sbi_ecall_handler() to the
sbi_ecall_legacy_handler().

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
17 months agolib: utils/irqchip: Add shared MMIO region for PLIC in root domain
Anup Patel [Mon, 11 Dec 2023 08:37:56 +0000 (14:07 +0530)]
lib: utils/irqchip: Add shared MMIO region for PLIC in root domain

On platforms with Smepmp, the MMIO regions accessed by M-mode need
to be explicitly marked with M-mode only read/write or shared (both
(M-mode and S-mode) read/write permission.

If the above is not done then runtime PLIC access from M-mode on
platforms with Smepmp will result in access fault when further
results in CPU hotplug not working.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
17 months agoplatform: generic: Fine tune fw_platform_calculate_heap_size()
Anup Patel [Mon, 11 Dec 2023 08:03:58 +0000 (13:33 +0530)]
platform: generic: Fine tune fw_platform_calculate_heap_size()

Let's use SBI_TLB_INFO_SIZE instead of hard-coded 0x40 in
fw_platform_calculate_heap_size() to fine tune the heap size
required for per-hart TLB fifos.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
17 months agolib: sbi_tlb: Reduce size of struct sbi_tlb_info
Anup Patel [Mon, 11 Dec 2023 07:56:49 +0000 (13:26 +0530)]
lib: sbi_tlb: Reduce size of struct sbi_tlb_info

Let us reduce the size of struct sbi_tlb_info by doing the
following:
1) Change the data type of asid and vmid fields to uint16_t
2) Replace local_fn() function pointer with an enum

Based on the above, the size of struct sbi_tlb_info is reduced
by 16 bytes on RV64 and 4 bytes on RV32.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
17 months agolib: sbi: Detect extensions from the ISA string in DT
Yong-Xuan Wang [Tue, 12 Dec 2023 08:58:35 +0000 (08:58 +0000)]
lib: sbi: Detect extensions from the ISA string in DT

Enable access to some extensions through menvcfg and show them in "Boot
HART ISA Extensions" if they are present in the device tree.

Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
17 months agolib: sbi: Using one array to define the name of extensions
Yong-Xuan Wang [Tue, 12 Dec 2023 08:58:34 +0000 (08:58 +0000)]
lib: sbi: Using one array to define the name of extensions

Define an array sbi_hart_ext to map extension ID and name , and use it
for ISA parsing and printing out the supported extensions.

Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
17 months agolib: sbi: Refactor the code for enable extensions in menvfg CSR
Yong-Xuan Wang [Tue, 12 Dec 2023 08:58:33 +0000 (08:58 +0000)]
lib: sbi: Refactor the code for enable extensions in menvfg CSR

Use 1 variable to store the value of menvcfg.

Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
17 months agolib: sbi: Improve the code of privilege mode and extensions detection
Yong-Xuan Wang [Tue, 12 Dec 2023 08:58:32 +0000 (08:58 +0000)]
lib: sbi: Improve the code of privilege mode and extensions detection

We can enhance the code by creating 2 unified interface with macro  for
privilege mode and extensions detection, which relies on supported
privilege modes and CSRs.

Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
17 months agolib: sbi: Simplify the initialization of root_hmask in sbi_domain_init
Xiang W [Mon, 11 Dec 2023 13:29:50 +0000 (21:29 +0800)]
lib: sbi: Simplify the initialization of root_hmask in sbi_domain_init

The original code has multiple conversions between hartid and
hartindex. Can call sbi_hartmask_set_hartindex directly to
avoid conversion.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
17 months agolib: sbi_ipi: Drop unnecessary ipi_process check
Samuel Holland [Fri, 8 Dec 2023 21:22:59 +0000 (13:22 -0800)]
lib: sbi_ipi: Drop unnecessary ipi_process check

sbi_ipi_event_create() disallows registering an IPI event with a NULL
.process callback, so the function pointer will never be NULL here.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Xiang W <wxjstz@126.com>
17 months agolib: sbi_ipi: Process self-IPIs in sbi_ipi_send()
Samuel Holland [Fri, 8 Dec 2023 21:22:58 +0000 (13:22 -0800)]
lib: sbi_ipi: Process self-IPIs in sbi_ipi_send()

An IPI sent to the local hart can be processed directly instead of
triggering the IPI device. This is more efficient, and it avoids a
deadlock when the .sync callback is defined. Since interrupts are
disabled while handling an ecall, the IPI would not get delivered
until the next mret, but sbi_ipi_sync() is called before then.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Xiang W <wxjstz@126.com>
17 months agolib: sbi_ipi: Do not ignore errors from sbi_ipi_send()
Samuel Holland [Mon, 18 Dec 2023 12:43:45 +0000 (18:13 +0530)]
lib: sbi_ipi: Do not ignore errors from sbi_ipi_send()

Currently, failures in sbi_ipi_send() are silently ignored, which makes
them difficult to debug. Instead, abort sending the IPI and pass back
the error, but still synchronize any IPIs already sent.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Signed-off-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi_tlb: Check tlb_range_flush_limit only once per request
Samuel Holland [Fri, 8 Dec 2023 20:29:55 +0000 (12:29 -0800)]
lib: sbi_tlb: Check tlb_range_flush_limit only once per request

The tlb_update() callback is called for each destination hart.
Move the size check earlier, so it is executed only once.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: utils/fdt: Allow to use reg-names when parsing ACLINT
Inochi Amaoto [Fri, 17 Nov 2023 12:13:09 +0000 (20:13 +0800)]
lib: utils/fdt: Allow to use reg-names when parsing ACLINT

Currently, the fdt_parse_aclint_node() follows a fixed order to parse
ACLINT timer. This may cause the undesirable result when the ACLINT
device does not support mtime without adding an empty entry for it in
the DT.

To be robust, make fdt_parse_aclint_node() support "reg-names" property,
so it can parse the DT in an order independent way. For compatibility,
fdt_parse_aclint_node() only use "reg-names" when parsing ACLINT timer,
and will fallback to the old way if "reg-names" property is not found.

Link: https://lore.kernel.org/all/20231114-skedaddle-precinct-66c8897227bb@squawk/
Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup patel <anup@brainfault.org>
18 months agolib: sbi: skip wait_for_coldboot when coolboot done
Xiang W [Sun, 10 Dec 2023 15:21:39 +0000 (23:21 +0800)]
lib: sbi: skip wait_for_coldboot when coolboot done

When warmboot via HSM, coolboot has been completed and
wait_for_coldboot can be skipped to speed up.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: recalculate heap size to support new tlb entry number
Inochi Amaoto [Sun, 10 Dec 2023 09:44:54 +0000 (17:44 +0800)]
platform: recalculate heap size to support new tlb entry number

Previous patch introduced a change that using hart count as the default
number of tlb entries in the fifo. This makes the default tlb fifo size
grow in square with the number of harts. So the default heap size is
not enough to allocate tlb fifo when the hart count is big.

Fixes: 52fd64b ("platform: Uses hart count as the default size of tlb info")
Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi_hsm: Put the resume_pending hart in the interruptible hart mask
Nick Hu [Fri, 1 Dec 2023 09:51:47 +0000 (17:51 +0800)]
lib: sbi_hsm: Put the resume_pending hart in the interruptible hart mask

Current interruptible hart mask doesn't include the hart which HSM state
is SBI_HSM_STATE_RESUME_PENDING. So when there is a request to send an
IPI to the hart which is in the resume process, this hart would miss the
IPI forever. Put the SBI_HSM_STATE_RESUME_PENDING hart in the
interruptible hart mask to fix the issue.

Signed-off-by: Nick Hu <nick.hu@sifive.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agofirmware: fix section types
Matt Waltz [Tue, 5 Dec 2023 15:13:44 +0000 (08:13 -0700)]
firmware: fix section types

These sections are only intended to hold data, and should not be executable.

Signed-off-by: Matt Waltz <matthewwaltzis@gmail.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi_pmu: Fix the counter info function
Atish Patra [Thu, 7 Dec 2023 22:23:50 +0000 (14:23 -0800)]
lib: sbi_pmu: Fix the counter info function

The counter info should only return valid hardware counters for the ones
set in the counter mask. Otherwise, it will report incorrect number of
hardware counters to the supervisor if the platform has discontiguous
counters.

Fixes: c744ed77b18c ("lib: sbi_pmu: Enable noncontigous hpm event and counters")
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi_pmu: Add PMU snapshot definitions
Atish Patra [Thu, 7 Dec 2023 22:23:49 +0000 (14:23 -0800)]
lib: sbi_pmu: Add PMU snapshot definitions

OpenSBI doesn't support SBI PMU snapshot yet as there is not much benefit
unless the multiple counters overflow at the same time.

Just add the definition and return not supported error at this moment. The
default returned error is also not supported. Thus, no functional change
intended.

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
18 months agolib: sbi_hart: Store PMP granularity as log base 2
Samuel Holland [Wed, 22 Nov 2023 19:44:04 +0000 (11:44 -0800)]
lib: sbi_hart: Store PMP granularity as log base 2

This minimizes the need to call log2roundup() to recover the log value.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Prevent redundant sbi_ipi_process
Xiang W [Wed, 22 Nov 2023 12:54:38 +0000 (20:54 +0800)]
lib: sbi: Prevent redundant sbi_ipi_process

Multiple harts may try to send IPI to a particular target hart A
in which case the send_ipi() should be called only when the old
value of the hart A ipi_type is zero.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Replace __atomic_op_bit_ord with __atomic intrinsics
Anup Patel [Fri, 8 Dec 2023 08:24:07 +0000 (13:54 +0530)]
lib: sbi: Replace __atomic_op_bit_ord with __atomic intrinsics

Simplify atomic-related bit operations through __atomic intrinsics.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Fix __atomic_op_bit_ord and comments
Xiang W [Wed, 15 Nov 2023 14:59:21 +0000 (22:59 +0800)]
lib: sbi: Fix __atomic_op_bit_ord and comments

The original code returns the value of the word before modification.
When modifying the upper 32 bits under RV64, the value returned via
int return will have no meaning. Corrected to return the value of the
bit. And modify the function description.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Remove xchg/cmpxchg implemented via lr/sc
Xiang W [Wed, 8 Nov 2023 03:28:16 +0000 (11:28 +0800)]
lib: sbi: Remove xchg/cmpxchg implemented via lr/sc

lr/sc is part of the A extension. If the A extension is not supported,
lr/sc cannot be used. So remove xchg/cmpxchg.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agodocs: pmu: Add Andes PMU node example
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:13 +0000 (20:42 +0800)]
docs: pmu: Add Andes PMU node example

Add PMU node example for event index to counter index mapping
and selector value translation of Andes' CPUs.

Currently, there are 4 HPM counters that can be used to monitor
all of the events for each hart.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Locus Wei-Han Chen <locus84@andestech.com>
Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
18 months agolib: utils: fdt_pmu: Do not iterate over the fdt_pmu_evt_select table
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:11 +0000 (20:42 +0800)]
lib: utils: fdt_pmu: Do not iterate over the fdt_pmu_evt_select table

The valid entry count is tracking by hw_event_count so there
is no need to check the whole table.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: utils: fdt_pmu: Make the fdt_pmu_evt_select table global variable
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:10 +0000 (20:42 +0800)]
lib: utils: fdt_pmu: Make the fdt_pmu_evt_select table global variable

To allow platform override pmu_init() filling the translation table
fdt_pmu_evt_select[] when PMU node doesn't provide such information,
we need to share the table and its entry counter with other .c file.

We also define the structures of PMU property in fdt_helper.h, so we
can initialize the mappings in arrays.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: andes: Factor out is_andes() helper
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:09 +0000 (20:42 +0800)]
platform: andes: Factor out is_andes() helper

We will need is_andes(45) in the following patch,
so factor out the code that parses marchid to make
it reusable for checking any Andes CPU variants.

Also improves the comment in ae350_hart_start().

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: utils: fdt_fixup: Allow preserving PMU properties
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:08 +0000 (20:42 +0800)]
lib: utils: fdt_fixup: Allow preserving PMU properties

Add a Kconfig option to control PMU fixup, so the next
stage software can dump the PMU node including event
mapping information for debugging purposes.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: rzfive: Enable Andes PMU for RZ/Five
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:07 +0000 (20:42 +0800)]
platform: rzfive: Enable Andes PMU for RZ/Five

Enable Andes PMU extension support for RZ/Five.
We also staticize renesas_rzfive_early_init() as
it is not used outside of this unit.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Tested-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: andes: Enable Andes PMU for AE350
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:06 +0000 (20:42 +0800)]
platform: andes: Enable Andes PMU for AE350

Enable Andes PMU extension support for AE350 platforms.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Tested-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: andes: Add Andes custom PMU support
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:05 +0000 (20:42 +0800)]
platform: andes: Add Andes custom PMU support

Before the ratification of Sscofpmf, the Andes PMU extension
was designed to support the sampling and filtering with hardware
performance counters (zihpm), it works with the current SBI PMU
extension and Linux SBI PMU driver.

We implement 1) the PMU device callbacks that update the
corresponding bits on custom CSRs, 2) extentions_init() to detect
the hardware support of Andes PMU and initialize the per-hart
PMU related CSR, and 3) pmu_init() to register PMU device and
populate event mappings.

Also define a andes_pmu_setup() function which is in preparation
for adding default PMU mappings in andes_hpm.h

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: generic: Introduce pmu_init() platform override
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:04 +0000 (20:42 +0800)]
platform: generic: Introduce pmu_init() platform override

Add pmu_init() platform override, which will be used to register
PMU device and populate event mappings.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: include: andes45: Add PMU related CSR defines
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:03 +0000 (20:42 +0800)]
platform: include: andes45: Add PMU related CSR defines

Add CSR definitions for Andes PMU extension.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Tested-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
18 months agosbi: sbi_pmu: Add hw_counter_filter_mode() to pmu device
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:02 +0000 (20:42 +0800)]
sbi: sbi_pmu: Add hw_counter_filter_mode() to pmu device

Add support for custom PMU extensions to set inhibit bits
on custom CSRs by introducing the PMU device callback
hw_counter_filter_mode(). This allows the perf tool to
restrict event counting under a specified privileged
mode by appending a modifier, e.g. perf record -e event:k
to count events only happening in kernel mode.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Add XAndesPMU in hart extensions
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:01 +0000 (20:42 +0800)]
lib: sbi: Add XAndesPMU in hart extensions

Add the custom extension to hart extension list.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Tested-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agosbi: sbi_pmu: Improve sbi_pmu_init() error handling
Yu Chien Peter Lin [Thu, 30 Nov 2023 12:42:00 +0000 (20:42 +0800)]
sbi: sbi_pmu: Improve sbi_pmu_init() error handling

This patch makes the following changes:

- As sbi_platform_pmu_init() returns a negative error code on
  failure, let sbi_pmu_init() print out the error code with
  sbi_dprintf().

- In order to distinguish the SBI_EFAIL error returned by
  sbi_pmu_add_*_counter_map(), return SBI_ENOENT to indicate
  that fdt_pmu_setup() failed to locate "riscv,pmu" node, and
  generic_pmu_init() ignores such case.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
18 months agolib: ipi: Adjust Andes PLICSW to single-bit-per-hart scheme
Leo Yu-Chi Liang [Thu, 30 Nov 2023 12:41:59 +0000 (20:41 +0800)]
lib: ipi: Adjust Andes PLICSW to single-bit-per-hart scheme

The old scheme doesn't allow sending hart0 self-IPI as the
corresponding bit on pending register is hardwired to 0, this
could lead to unhandle IPIs on SMP systems, esp. on single-core.

Furthermore, the limitation of old scheme is 8-core, instead of
reserving source hart information, we assign bit (x + 1) as the
enable and pending bit of hartx, this also expands the bootable
hart number.

The following diagram shows the enable bits of the new scheme
on 32-core Andes platform.

   Pending regs: 0x1000  x---0---0---0---0------0---0
Pending hart ID:             0   1   2   3 ... 30  31
   Interrupt ID:         0   1   2   3   4 ... 31  32
                         |   |   |   |   |      |   |
    Enable regs: 0x2000  x---1---0---0---0-...--0---0---> hart0
                         |   |   |   |   |      |   |
                 0x2080  x---0---1---0---0-...--0---0---> hart1
                         |   |   |   |   |      |   |
                 0x2100  x---0---0---1---0-...--0---0---> hart2
                         |   |   |   |   |      |   |
                 0x2180  x---0---0---0---1-...--0---0---> hart3
                         .   .   .   .   .      .   .
                         .   .   .   .   .      .   .
                         .   .   .   .   .      .   .
                 0x2f00  x---0---0---0---0-...--1---0---> hart30
                         |   |   |   |   |      |   |
                 0x2f80  x---0---0---0---0-...--0---1---> hart31
                         <-------- word 0 -------><--- word 1 --->

To send IPI to hart0, for example, another hart (including hart0
itself) will set bit 1 of first word on the pending register.

We also fix indentation in andes_plicsw.h along with this patch.

Fixes: ce7c490719ed ("lib: utils/ipi: Add Andes fdt ipi driver support")
Signed-off-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Randolph <randolph@andestech.com>
Reported-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Link: https://lists.infradead.org/pipermail/opensbi/2023-October/005665.html
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Tested-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Allow relaxed MMIO writes in device ipi_clear() callback
Anup Patel [Tue, 21 Nov 2023 10:22:17 +0000 (15:52 +0530)]
lib: sbi: Allow relaxed MMIO writes in device ipi_clear() callback

Currently, there are no barriers before or after the ipi_clear()
device callback which forces ipi_clear() device callback to always
use non-relaxed MMIO writes.

Instead of above, we use wmb() in after the ipi_clear() device
callback which pairs with the wmb() done before the ipi_send()
device callback. This also allows device ipi_clear() callback
to use relaxed MMIO writes.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reported-by: Bo Gan <ganboing@gmail.com>
18 months agolib: sbi: Allow relaxed MMIO writes in device ipi_send() callback
Anup Patel [Tue, 21 Nov 2023 09:19:42 +0000 (14:49 +0530)]
lib: sbi: Allow relaxed MMIO writes in device ipi_send() callback

Currently, we have a smp_wmb() between atomic_raw_set_bit() and
ipi_send() device callback whereas the MMIO writes done by the
device ipi_send() callback will also include a barrier.

We can avoid unnecessary/redundant barriers described above by
allowing relaxed MMIO writes in device ipi_send() callback. To
achieve this, we simply use  wmb() instead of smp_wmb() before
calling device ipi_send().

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reported-by: Bo Gan <ganboing@gmail.com>
18 months agolib: utils/irqchip: Avoid redundant writes to APLIC CLRIE register
Anup Patel [Tue, 21 Nov 2023 06:04:14 +0000 (11:34 +0530)]
lib: utils/irqchip: Avoid redundant writes to APLIC CLRIE register

Each APLIC CLRIE register allows disabling 32 interrupt sources at
a time by writing -1 so no need to write CLRIE register separately
for each interrupt source.

Fixes: 99792653de29 ("lib: utils/irqchip: Add APLIC initialization library")
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
18 months agolib: sbi_pmu: avoid buffer overflow
Heinrich Schuchardt [Fri, 17 Nov 2023 13:59:31 +0000 (14:59 +0100)]
lib: sbi_pmu: avoid buffer overflow

total_ctrs is bounded by

    SBI_PMU_FW_CTR_MAX + SBI_PMU_HW_CTR_MAX) == 48

which exceeds BITS_PER_LONG on 32 bit systems.

Iterating over the bits of &cmask results in a buffer overflow when looking
for a bit >= BITS_PER_LONG.

Adjust the iterators in sbi_pmu_ctr_start() and sbi_pmu_ctr_stop()
accordingly.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Fix PMP granularity handling in sbi_hart_map_saddr()
Anup Patel [Sun, 19 Nov 2023 13:02:10 +0000 (18:32 +0530)]
lib: sbi: Fix PMP granularity handling in sbi_hart_map_saddr()

The sbi_hart_map_saddr() must create PMP mapping of size greater
than or equal to PMP granularity otherwise PMP mapping does not
work when size parameter less than sbi_hart_pmp_granularity(scratch).

Fixes: 6e44ef686a9b ("lib: sbi: Add functions to map/unmap shared memory")
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
18 months agolib: utils/serial: remove semihosting_putc
Xiang W [Fri, 17 Nov 2023 01:03:59 +0000 (09:03 +0800)]
lib: utils/serial: remove semihosting_putc

For some debuggers that do not implement SYSWRITEC and SYSREADC
operations, we have to use SYSWRITE and SYSREAD.

Instead of implementing semihosting_putc() using SYSWRITE, let us
simply remove semihosting_putc() because console_putc/console_puts
are now interchangeable.

Signed-off-by: Chen Pei <cp0613@linux.alibaba.com>
Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Make console_puts/console_putc interchangeable
Xiang W [Fri, 17 Nov 2023 01:03:58 +0000 (09:03 +0800)]
lib: sbi: Make console_puts/console_putc interchangeable

console_puts/console_putc should replace each other, but the previous
sbi_putc can only use console_putc. This patch addresses this problem.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: change sbi_hart_features.extensions as an array
Xiang W [Mon, 30 Oct 2023 16:39:40 +0000 (00:39 +0800)]
lib: sbi: change sbi_hart_features.extensions as an array

In the future there may be a lot of ISA extensions, a 'long' may not
be able to accommodate, changed to an array for the future.

Addresses-Coverity-ID: 1568357 Out-of-bounds access
Fixes: 6259b2ec2d09 ("lib: utils/fdt: Fix fdt_parse_isa_extensions()
implementation")
Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: enable seed access in S-mode
Heinrich Schuchardt [Sat, 28 Oct 2023 22:49:59 +0000 (00:49 +0200)]
lib: sbi: enable seed access in S-mode

If ISA extension Zkr is available, set

    mseccfg.sseed=1
    mseccfg.useed=0

This enables access to the seed CSR in S-mode but not in U-mode.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Add Zkr in hart extensions
Heinrich Schuchardt [Sat, 28 Oct 2023 22:49:58 +0000 (00:49 +0200)]
lib: sbi: Add Zkr in hart extensions

- Add Zkr as extension in sbi_hart_extensions enum
- Return "zkr" string for Zkr extension from sbi_hart_extension_id2string

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoinclude: sbi: macros for mseccfg.sseed and .useed
Heinrich Schuchardt [Sat, 28 Oct 2023 22:49:57 +0000 (00:49 +0200)]
include: sbi: macros for mseccfg.sseed and .useed

Define macros to access the sseed and the useed bit in the machine
security configuration register (mseccfg).

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: generic: Add Sophgo sg2042 platform support
Inochi Amaoto [Thu, 19 Oct 2023 23:57:18 +0000 (07:57 +0800)]
platform: generic: Add Sophgo sg2042 platform support

Add Sophgo sg2042 soc support

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Add regions merging when sanitizing domain region
Inochi Amaoto [Thu, 16 Nov 2023 10:49:39 +0000 (18:49 +0800)]
lib: sbi: Add regions merging when sanitizing domain region

As the domain will reject a new memory region which has a sub-regions
already in the domain, even the new region is bigger and has the same
flags. This problem can be solved by relaxing region restriction and
rechecking when adding and sanitizing domains.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: separate the swap operation of domain region
Inochi Amaoto [Thu, 16 Nov 2023 10:49:38 +0000 (18:49 +0800)]
lib: sbi: separate the swap operation of domain region

Swapping domain region is a common operation when sorting domain region,
so separate it as a function to make code clean.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: utils/ipi: mswi: add separate T-Head C9xx CLINT mswi compatible
Inochi Amaoto [Thu, 16 Nov 2023 09:07:15 +0000 (17:07 +0800)]
lib: utils/ipi: mswi: add separate T-Head C9xx CLINT mswi compatible

Like the mtimer of T-HEAD C9xx clint, the mswi also needs new compatible
string to avoid misuse.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Link: https://lore.kernel.org/linux-riscv/1f6b82a1864477a51db33d3f295889ff985b497b.1696433229.git.unicorn_wang@outlook.com/
Reviewed-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: utils/timer: mtimer: add separate T-Head C9xx CLINT mtimer compatible
Inochi Amaoto [Thu, 16 Nov 2023 09:07:14 +0000 (17:07 +0800)]
lib: utils/timer: mtimer: add separate T-Head C9xx CLINT mtimer compatible

T-HEAD allows soc vendor to map the mtimer and mswi of C9xx clint on
different address, which may cause a misuse if use the same compatible
string, add a new timer compatible string to avoid this.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Link: https://lore.kernel.org/linux-riscv/6e48cbe5e60f9ada2fd1fe58e803e127f1a678e5.1696433229.git.unicorn_wang@outlook.com/
Reviewed-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: utils/timer: Allow ACLINT MTIMER driver to setup quirks
Inochi Amaoto [Thu, 16 Nov 2023 09:07:13 +0000 (17:07 +0800)]
lib: utils/timer: Allow ACLINT MTIMER driver to setup quirks

The quirks checking will cause ACLINT step into a CLINT code path, this
is not expected when ACLINT needs custom quirks.

Add a new quirk to identify custom ACLINT, and apply the general quirks
after applying CLINT specific quirks.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoutils/reset: Remove fdt_reset_thead
Guo Ren [Thu, 12 Oct 2023 08:59:13 +0000 (04:59 -0400)]
utils/reset: Remove fdt_reset_thead

In the past, we used fdt_reset_thead to help customers with prototype
verification. However, with the emergence of the Big-little SoC system,
it can no longer meet the demand. Therefore, we use zero_stage_boot
instead of fdt_reset_thead. It cleans up the opensbi code and ends the
disputation of reset_sample's dts.

This patch removes the fdt_reset_thead component and updates the related
doc.

Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
Signed-off-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agofirmware: fw_base.S: Fix boot hart status synchronization
Guo Ren [Thu, 19 Oct 2023 09:27:34 +0000 (05:27 -0400)]
firmware: fw_base.S: Fix boot hart status synchronization

It's wrong to put the fence after setting the boot status flag because
all relocation operations must be finished before setting the status
flag. So, this fence must be put before the setting status flag, and
there is no use in putting a fence between _start_warm and setting
status flag.

Also, nop can't delay other harts too much, so use div instead, just
like Linux cpu_relax. Current opensbi force enables “M” Standard
Extension, and mul instructions have been used in the fw_base.S.

After the above two fixes, the boot hart index param of the
fw_dynamic_info could be guaranteed properly for all platforms.

Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
Signed-off-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: utils/serial: Optimize semihosting_putc implementation
Chen Pei [Wed, 18 Oct 2023 02:42:05 +0000 (10:42 +0800)]
lib: utils/serial: Optimize semihosting_putc implementation

For some debuggers that do not implement SYSWRITEC and SYSREADC
operations, we can use SYSWRITE and SYSREAD instead like the
implementation of semihosting_getc().

Signed-off-by: Chen Pei <cp0613@linux.alibaba.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: Uses hart count as the default size of tlb info
Inochi Amaoto [Thu, 12 Oct 2023 08:33:28 +0000 (16:33 +0800)]
platform: Uses hart count as the default size of tlb info

For platform with high number of harts, it is better to auto detect a
suitable number of entries in tlb fifo. Since allocating tlb entry for
all online harts can reduce the wait time significantly, using the
number of the online harts can make most platforms happy. This auto
detection can avoid most duplicate code for setting tlb fifo size.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Acked-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: generic: thead: improve tlb flush errata
Inochi Amaoto [Thu, 12 Oct 2023 01:54:18 +0000 (09:54 +0800)]
platform: generic: thead: improve tlb flush errata

Flushing the tlb entries can solve the thead tlb problem, but flushing
it by address will miss something and lead to a exception in some rare
cases, and this is more common for sg2042.

To solve this problem, flush the tlb entries by asid in the custom trap
handler to ensure it is refreshed.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: Correctly limit flushes to a single ASID/VMID
Samuel Holland [Tue, 17 Oct 2023 22:20:19 +0000 (15:20 -0700)]
lib: sbi: Correctly limit flushes to a single ASID/VMID

Per the SBI specification, the effects of these functions are limited to
a specific ASID and/or VMID. This applies even when flushing the entire
address space.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: generic: thead: initialize PMU by default in thead generic platform
Inochi Amaoto [Sat, 7 Oct 2023 03:06:31 +0000 (11:06 +0800)]
platform: generic: thead: initialize PMU by default in thead generic platform

Since all the SoC with thead c9xx cores need this initialization at now,
initialize the c9xx pmu in the thead generic platform by default.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: generic: thead: separate implement of T-HEAD c9xx errata
Inochi Amaoto [Sat, 7 Oct 2023 03:06:30 +0000 (11:06 +0800)]
platform: generic: thead: separate implement of T-HEAD c9xx errata

Separate the implement of T-HEAD c9xx errata to allow any platform
with bug related to c9xx cores can use it.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: generic: thead: separate implement of T-HEAD c9xx pmu
Inochi Amaoto [Sat, 7 Oct 2023 03:06:29 +0000 (11:06 +0800)]
platform: generic: thead: separate implement of T-HEAD c9xx pmu

Separate the implement of T-HEAD c9xx pmu to allow any platform with
c9xx cores can use it.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agoplatform: generic: thead: move to thead c9xx header to vendor specific postion
Inochi Amaoto [Sat, 7 Oct 2023 03:06:28 +0000 (11:06 +0800)]
platform: generic: thead: move to thead c9xx header to vendor specific postion

The CSR encoding for t-head c9xx cores is shared across all the
platforms with these cores. So move header thead_c9xx.h to the
thead subdir.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Acked-by: Guo Ren <guoren@kernel.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
18 months agolib: sbi: simplify sanitize_domain()
Heinrich Schuchardt [Fri, 6 Oct 2023 20:59:26 +0000 (22:59 +0200)]
lib: sbi: simplify sanitize_domain()

Since commit 112daa2e647c ("lib: sbi: Maximize the use of HART index in
sbi_domain") the platform parameter is unused.

Fixes: 112daa2e647c ("lib: sbi: Maximize the use of HART index in sbi_domain")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
19 months agoinclude: sbi: Change spec version to 2.0
Anup Patel [Tue, 10 Oct 2023 08:40:41 +0000 (14:10 +0530)]
include: sbi: Change spec version to 2.0

Now that SBI v2.0 specification is frozen, we change spec version
implemented by OpenSBI to v2.0.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
20 months agolib: sbi_misaligned_ldst: Fix handling of C.SWSP and C.SDSP
Amanieu d'Antras [Sun, 1 Oct 2023 09:12:02 +0000 (10:12 +0100)]
lib: sbi_misaligned_ldst: Fix handling of C.SWSP and C.SDSP

Unlike C.LWSP/C.LDSP, these encodings can be used with the zero
register, so checking that the rs2 field is non-zero is unnecessary.

Additionally, the previous check was incorrect since it was checking
the immediate field of the instruction instead of the rs2 field.

Signed-off-by: Amanieu d'Antras <amanieu@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi: Add support for mconfigptr
Yangjie Zhang [Thu, 28 Sep 2023 08:27:09 +0000 (16:27 +0800)]
lib: sbi: Add support for mconfigptr

RISC-V privileged specification v1.12 introduced the mconfigptr CSR
which will hold the physical address of a configuration data
structure.

Signed-off-by: Yangjie Zhang <jay1273062855@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agodocs: platform: update platform_requirements.md
Yangjie Zhang [Thu, 28 Sep 2023 08:25:23 +0000 (16:25 +0800)]
docs: platform: update platform_requirements.md

"Zicsr" isa extension has been separated from "I" extension.
This patch add the isa requirement of "Zicsr" extension in
platform requirements documentation.

Signed-off-by: Yangjie Zhang <jay1273062855@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agogpio/starfive: redundant readl() call
Heinrich Schuchardt [Fri, 29 Sep 2023 21:53:55 +0000 (23:53 +0200)]
gpio/starfive: redundant readl() call

In starfive_gpio_direction_output() readl() is called twice to read the
gpio direction register. The result of the first read is discarded.

Remove the redundant read.

Fixes: 908be1b85c8f ("gpio/starfive: add gpio driver and support gpio reset")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi_pmu: check for index overflows
Heinrich Schuchardt [Thu, 28 Sep 2023 13:03:57 +0000 (15:03 +0200)]
lib: sbi_pmu: check for index overflows

sbi_pmu_ctr_cfg_match() receives data from a lower privilege level mode.
We must catch maliciously wrong values.

We already check against total_ctrs. But we do not check that total_ctrs is
less than SBI_PMU_HW_CTR_MAX + SBI_PMU_FW_CTR_MAX.

Check that the number of hardware counters is in the valid range.

Addresses-Coverity-ID: 1566114 Out-of-bounds write
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
20 months agolib: utils: Simplify SET_ISA_EXT_MAP()
Heinrich Schuchardt [Thu, 28 Sep 2023 12:17:20 +0000 (14:17 +0200)]
lib: utils: Simplify SET_ISA_EXT_MAP()

The define is hard to read. The continue statement does not do what was
intended.

* Remove do {} while (false);
* Change the name to set_multi_letter_ext
  - Other local macros are lower case too.
  - Refer to the fact that this is only used for multi-letter extensions.

Addresses-Coverity-ID: 1568359 Unexpected control flow
Fixes: d72f5f17478d ("lib: utils: Add detection of Smepmp from ISA string in FDT")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: utils/fdt: simplify dt_parse_isa_extensions
Heinrich Schuchardt [Wed, 27 Sep 2023 14:19:12 +0000 (16:19 +0200)]
lib: utils/fdt: simplify dt_parse_isa_extensions

hart_exts == NULL can only occur if offset and node address lead to an
overflow resulting in exactly NULL. As we don't catch other values of
overflow it does not make sense to treat this one as special.

Addresses-Coverity-ID: 1568355 Logically dead code
Addresses-Coverity-ID: 1568358 Logically dead code
Fixes: 6259b2ec2d09 ("lib: utils/fdt: Fix fdt_parse_isa_extensions() implementation")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi: fix sbi_domain_get_assigned_hartmask()
Heinrich Schuchardt [Wed, 27 Sep 2023 14:14:51 +0000 (16:14 +0200)]
lib: sbi: fix sbi_domain_get_assigned_hartmask()

'1' is a 32 bit integer. When shifting it by more than 31 bits it becomes
zero and we get an incorrect return value.

Addresses-Coverity-ID: 1568356 Bad bit shift operation
Fixes: 296e70d69da7 ("lib: sbi: Extend sbi_hartmask to support both hartid and hartindex")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: utils: timer/ipi: Update memregion flags for PLMT and PLICSW
Yu Chien Peter Lin [Wed, 27 Sep 2023 10:08:31 +0000 (18:08 +0800)]
lib: utils: timer/ipi: Update memregion flags for PLMT and PLICSW

This patch adds unspecified permission flags for the PLICSW region
and updates the permission of the PLMT region.

With this update, both regions will become M-mode only read/write
regions in the root domain.

  Domain0 Region00: 0x00000000f0300000-0x00000000f0300fff M: (I,R,W) S/U: (R,W)
  Domain0 Region01: 0x0000000000040000-0x000000000005ffff M: (R,W) S/U: ()
  Domain0 Region02: 0x0000000000000000-0x000000000003ffff M: (R,X) S/U: ()
> Domain0 Region03: 0x00000000e6000000-0x00000000e60fffff M: (I,R,W) S/U: ()
> Domain0 Region04: 0x00000000e6400000-0x00000000e67fffff M: (I,R,W) S/U: ()
  Domain0 Region05: 0x0000000000000000-0xffffffffffffffff M: () S/U: (R,W,X)

The PMP rules of AE350-AX65 (single-core) w/ Smepmp:

  p/x $pmpcfg0
  $1 = {0x1f9b9b9d9b1e00,
  pmp0cfg = {0x0},
                    L--AAXWR
  pmp1cfg = {0x1e} (00011110), pmpaddr1: 0xf0300000 ~   0xf0300fff  (UART1)
  pmp2cfg = {0x9b} (10011011), pmpaddr2:    0x40000 ~      0x5ffff
  pmp3cfg = {0x9d} (10011101), pmpaddr3:        0x0 ~      0x3ffff
  pmp4cfg = {0x9b} (10011011), pmpaddr4: 0xe6000000 ~   0xe60fffff  (PLMT)
  pmp5cfg = {0x9b} (10011011), pmpaddr5: 0xe6400000 ~   0xe67fffff  (PLICSW)
  pmp6cfg = {0x1f} (00011111), pmpaddr6:        0x0 ~ 0xffffffffff
  pmp7cfg = {0x0 }}

The PMP rules of AE350-AX45MP (qual-core) w/o Smepmp:

  p/x $pmpcfg0
  $1 = {0x1f181818181b,
                     L--AAXWR
  pmp0cfg = {0x1b}, (00011011), pmpaddr0: 0xf0300000 ~  0xf0300fff  (UART1)
  pmp1cfg = {0x18}, (00011000), pmpaddr1:    0x40000 ~     0x5ffff
  pmp2cfg = {0x18}, (00011000), pmpaddr2:        0x0 ~     0x3ffff
  pmp3cfg = {0x18}, (00011000), pmpaddr3: 0xe6000000 ~  0xe60fffff  (PLMT)
  pmp4cfg = {0x18}, (00011000), pmpaddr4: 0xe6400000 ~  0xe67fffff  (PLICSW)
  pmp5cfg = {0x1f}, (00011111), pmpaddr5:        0x0 ~ 0x1ffffffff
  pmp6cfg = {0x0 }}

Note that starting from this patch, we restrict the S/U-mode read
permission to the PLMT region, since we should read the TIME CSR
in a lower privilege mode.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: utils/serial: Ensure proper allocation of PMP entries for uart8250
Yu Chien Peter Lin [Mon, 25 Sep 2023 12:24:26 +0000 (20:24 +0800)]
lib: utils/serial: Ensure proper allocation of PMP entries for uart8250

The added memory region should start from the base address.
Otherwise, the range will be shifted by reg_offset and not
able to merge consecutive NAPOT regions in the root domain,
resulting in wasted PMP entries.

Fixes: e8bc1621 ("lib: utils/serial: Add shared regions for
serial drivers")

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agodoc: Fix fw_payload.md
Hoa Nguyen [Sun, 24 Sep 2023 21:48:23 +0000 (14:48 -0700)]
doc: Fix fw_payload.md

The base of .text is defined by `FW_TEXT_START`, not `FW_TEXT_BASE`.

Signed-off-by: Hoa Nguyen <hn@hnpl.org>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoMakefile: Add --exclude-libs ALL to avoid .dynsym
Vivian Wang [Sun, 17 Sep 2023 17:52:51 +0000 (01:52 +0800)]
Makefile: Add --exclude-libs ALL to avoid .dynsym

Since everything is statically linked, we don't need to expose symbols
for dynamic linking.

For a default build this saves about 2 KiB of useless read only data in
.dynsym, .dynstr, .hash, .gnu.hash sections.

Signed-off-by: Vivian Wang <dramforever@live.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agofirmware: Remove handling of R_RISCV_{32,64}
Vivian Wang [Sun, 17 Sep 2023 17:52:50 +0000 (01:52 +0800)]
firmware: Remove handling of R_RISCV_{32,64}

Since everything is statically linked, we won't actually have
R_RISCV_{32,64} relocations. No need to handle these.

Fixes: 0f20e8adcf42 ("firmware: Support position independent execution")
Signed-off-by: Vivian Wang <dramforever@live.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agofirmware: Remove ALIGN in .rela.dyn in linker script
Vivian Wang [Sun, 17 Sep 2023 17:52:49 +0000 (01:52 +0800)]
firmware: Remove ALIGN in .rela.dyn in linker script

The .rela.dyn section should be exactly the size of the relocations,
without padding. On RV64, .rela* sections are already aligned and
there's no need for padding. On RV32, this adds padding up to 4 bytes,
which, if present, confuses the relocation loop into processing an extra
entry past the end of .rela*, and it crashes with an invalid memory
access.

Fixes: 0f20e8adcf42 ("firmware: Support position independent execution")
Signed-off-by: Vivian Wang <dramforever@live.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoplatform: generic: thead: fix stale TLB entries for th1520/sg2042
Inochi Amaoto [Fri, 15 Sep 2023 09:39:36 +0000 (17:39 +0800)]
platform: generic: thead: fix stale TLB entries for th1520/sg2042

The TLB entries remain functional all the time once added in T-HEAD th1520
and Sophgo sg2042 (even if the MMU is then disabled afterwards). If there
are some stale TLB entries that contains the address of SBI, it will cause
unexpected memory access and issue a illegal instruction error. To avoid
this, a TLB flush is needed to drop these TLB entries before any memory
access in the trap handler.

To handle this workaroud, add a custom trap handler with executing TLB flush
first in the T-HEAD platform to fix affected socs.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: utils/regmap: Mark syscon region as shared read-write
Anup Patel [Thu, 7 Sep 2023 11:49:47 +0000 (17:19 +0530)]
lib: utils/regmap: Mark syscon region as shared read-write

The syscon region used by OpenSBI should be marked as a shared
read-write region between M-mode and SU-mode.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
20 months agolib: utils/serial: Add shared regions for serial drivers
Mayuresh Chitale [Thu, 7 Sep 2023 11:49:46 +0000 (17:19 +0530)]
lib: utils/serial: Add shared regions for serial drivers

The serial driver regions used by OpenSBI should be marked as a shared
read-write regions between M-mode and SU-mode as those are accessed
by earlycon and the corresponding tty serial drivers running in 'S' mode.
When the smepmp extension is enabled, PMP entries for these shared regions
will get programmed.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Populate M-only Smepmp entries before setting mseccfg.MML
Anup Patel [Thu, 7 Sep 2023 11:49:45 +0000 (17:19 +0530)]
lib: sbi: Populate M-only Smepmp entries before setting mseccfg.MML

Based on sections 4.c and 4.d in Ch.2 of the Smepmp spec the PMP entries
must be programmed as below:
1. Program M-only entries
2. Enable mseccfg.MML
3. Program shared-region entries
4. Program SU-only entries

Co-developed-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
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>