platform/kernel/opensbi.git
19 months agolib: sbi_pmu: Update sbi_pmu dev ops
Mayuresh Chitale [Thu, 9 Mar 2023 13:13:55 +0000 (18:43 +0530)]
lib: sbi_pmu: Update sbi_pmu dev ops

Update fw_event_validate_code, fw_counter_match_code and fw_counter_start
ops which used a 32 bit event code to use the 64 bit event data instead.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
19 months agolib: sbi_pmu: Rename fw_counter_value
Mayuresh Chitale [Thu, 9 Mar 2023 13:13:54 +0000 (18:43 +0530)]
lib: sbi_pmu: Rename fw_counter_value

Rename and reuse fw_counter_value array to save both the counter values
for the SBI firmware events and event data for the SBI platform specific
firmware events.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
19 months agolib: sbi_pmu: Reserve space for implementation specific firmware events
Mayuresh Chitale [Thu, 9 Mar 2023 13:13:53 +0000 (18:43 +0530)]
lib: sbi_pmu: Reserve space for implementation specific firmware events

We reserve space for SBI implementation specific custom firmware
events which can be used by M-mode firmwares and HS-mode hypervisors
for their own use. This reserved space is intentionally large to
ensure that SBI implementation has enough space to accommodate
platform specific firmware events as well.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
19 months agolib: sbi_pmu: Implement sbi_pmu_counter_fw_read_hi
Mayuresh Chitale [Thu, 9 Mar 2023 13:13:52 +0000 (18:43 +0530)]
lib: sbi_pmu: Implement sbi_pmu_counter_fw_read_hi

To support 64 bit firmware counters on RV32 systems, we implement
sbi_pmu_counter_fw_read_hi() which returns the upper 32 bits of
the firmware counter value. On RV64 (or higher) systems, this
function will always return zero.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
19 months agolib: sbi_pmu: add callback for counter width
Mayuresh Chitale [Thu, 9 Mar 2023 13:13:51 +0000 (18:43 +0530)]
lib: sbi_pmu: add callback for counter width

This patch adds a callback to fetch the number of bits implemented for a
custom firmware counter. If the callback fails or is not implemented then
width defaults to 63.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
19 months agolib: serial: Cadence: Enable compatibility for cdns,uart-r1p8
Mayuresh Chitale [Wed, 1 Mar 2023 14:38:00 +0000 (20:08 +0530)]
lib: serial: Cadence: Enable compatibility for cdns,uart-r1p8

The Cadence driver does not use the RX byte status feature and hence can
be advertised to be compatible with cdns,uart-r1p8 as well.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
19 months agoplatform: starfive: add PMIC power ops in JH7110 visionfive2 board
Minda Chen [Thu, 9 Mar 2023 06:19:59 +0000 (14:19 +0800)]
platform: starfive: add PMIC power ops in JH7110 visionfive2 board

add reboot and poweroff support. The whole reboot and shutdown
pm op includes shutdown jh7110 pmu device power domain
and access on board pmic register through I2C.

Signed-off-by: Minda Chen <minda.chen@starfivetech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
19 months agolib: utils/i2c: Add minimal StarFive jh7110 I2C driver
Minda Chen [Thu, 9 Mar 2023 06:19:58 +0000 (14:19 +0800)]
lib: utils/i2c: Add minimal StarFive jh7110 I2C driver

Starfive JH7110 I2C IP is synopsys designware.
Minimum StarFIve I2C driver to read/send bytes over I2C bus.

This allows querying information and perform operation of onboard PMIC,
as well as power-off and reset.

Signed-off-by: Minda Chen <minda.chen@starfivetech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agomake: Add a command line option for debugging OpenSBI
Bin Meng [Mon, 27 Feb 2023 02:35:06 +0000 (10:35 +0800)]
make: Add a command line option for debugging OpenSBI

Add a new make command line option "make DEBUG=1" to prevent compiler
optimizations using -O2.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agogpio/starfive: add gpio driver and support gpio reset
minda.chen [Thu, 16 Feb 2023 09:21:26 +0000 (17:21 +0800)]
gpio/starfive: add gpio driver and support gpio reset

Add gpio driver and gpio reset function in Starfive
JH7110 SOC platform.

Signed-off-by: minda.chen <minda.chen@starfivetech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoplatform: generic: Add system suspend test
Andrew Jones [Mon, 27 Feb 2023 10:31:06 +0000 (11:31 +0100)]
platform: generic: Add system suspend test

When the system-suspend-test property is present in the domain config
node as shown below, implement system suspend with a simple 5 second
delay followed by a WFI. This allows testing system suspend when the
low-level firmware doesn't support it.

  / {
    chosen {
      opensbi-domains {
          compatible = "opensbi,domain,config";
          system-suspend-test;
      };

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agodocs: Correct opensbi-domain property name
Andrew Jones [Mon, 27 Feb 2023 10:31:05 +0000 (11:31 +0100)]
docs: Correct opensbi-domain property name

Replace the commas with dashes to correct the name.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi: Implement system suspend
Andrew Jones [Mon, 27 Feb 2023 10:31:04 +0000 (11:31 +0100)]
lib: sbi: Implement system suspend

Fill the implementation of the system suspend ecall. A platform
implementation of the suspend callbacks is still required for this
to do anything.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi: Add system_suspend_allowed domain property
Andrew Jones [Mon, 27 Feb 2023 10:31:03 +0000 (11:31 +0100)]
lib: sbi: Add system_suspend_allowed domain property

Only privileged domains should be allowed to suspend the entire
system. Give the root domain this property by default and allow
other domains to be given the property by specifying it in the
DT.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi: Add system suspend skeleton
Andrew Jones [Mon, 27 Feb 2023 10:31:02 +0000 (11:31 +0100)]
lib: sbi: Add system suspend skeleton

Add the SUSP extension probe and ecall support, but for now the
system suspend function is just a stub.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi_hsm: Export some functions
Andrew Jones [Mon, 27 Feb 2023 10:31:01 +0000 (11:31 +0100)]
lib: sbi_hsm: Export some functions

A coming patch can make use of a few internal hsm functions if
we export them.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi_hsm: Remove unnecessary include
Andrew Jones [Mon, 27 Feb 2023 10:31:00 +0000 (11:31 +0100)]
lib: sbi_hsm: Remove unnecessary include

Also remove a superfluous semicolon and add a blank line.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi_hsm: Move misplaced comment
Andrew Jones [Mon, 27 Feb 2023 10:30:59 +0000 (11:30 +0100)]
lib: sbi_hsm: Move misplaced comment

While non-retentive suspend is not allowed for M-mode, the comment
at the top of sbi_hsm_hart_suspend() implied suspend wasn't allowed
for M-mode at all. Move the comment above the mode check which is
inside a suspend type is non-retentive check.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi_hsm: Ensure errors are consistent with spec
Andrew Jones [Mon, 27 Feb 2023 10:30:58 +0000 (11:30 +0100)]
lib: sbi_hsm: Ensure errors are consistent with spec

HSM functions define when SBI_ERR_INVALID_PARAM should be returned.
Ensure it's not used for reasons that don't meet the definitions by
using the catch-all code, SBI_ERR_FAILED, for those reasons instead.
Also, in one case sbi_hart_suspend() may have returned SBI_ERR_DENIED,
which isn't defined for that function at all. Use SBI_ERR_FAILED for
that case too.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi_hsm: Don't try to restore state on failed change
Andrew Jones [Mon, 27 Feb 2023 10:30:57 +0000 (11:30 +0100)]
lib: sbi_hsm: Don't try to restore state on failed change

When a state change fails there's no need to restore the original
state as it remains the same.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi_hsm: Factor out invalid state detection
Andrew Jones [Mon, 27 Feb 2023 10:30:56 +0000 (11:30 +0100)]
lib: sbi_hsm: Factor out invalid state detection

Remove some redundant code by creating an invalid state detection
macro.

No functional change intended.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agodocs: domain_support: Update the DT example
Bin Meng [Fri, 24 Feb 2023 04:28:27 +0000 (12:28 +0800)]
docs: domain_support: Update the DT example

commit 3e2f573e707e ("lib: utils: Disallow non-root domains from adding M-mode regions")
added access permission check in __fdt_parse_region(). With the
existing DT example in the doc OpenSBI won't boot anymore.

Let's update the DT example so that it can work out of the box.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: utils/fdt/fdt_domain: Simplify region access permission check
Bin Meng [Fri, 24 Feb 2023 04:28:26 +0000 (12:28 +0800)]
lib: utils/fdt/fdt_domain: Simplify region access permission check

The region access permission check in __fdt_parse_region() can be
simplified as masking SBI_DOMAIN_MEMREGION_{M,SU}_ACCESS_MASK is
enough.

While we are here, update the confusing comments to match the codes.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi/sbi_domain: cosmetic style fixes
Bin Meng [Fri, 24 Feb 2023 04:28:25 +0000 (12:28 +0800)]
lib: sbi/sbi_domain: cosmetic style fixes

Minor updates to the comments for language and style fixes.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi: sbi_ecall: Check the range of SBI error
Yu Chien Peter Lin [Wed, 22 Feb 2023 02:48:54 +0000 (10:48 +0800)]
lib: sbi: sbi_ecall: Check the range of SBI error

We should also check if the return error code is greater than 0
(SBI_SUCCESS), as this is an invalid error.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoplatform: generic: renesas: rzfive: Configure the PMA region
Lad Prabhakar [Mon, 13 Feb 2023 21:51:10 +0000 (21:51 +0000)]
platform: generic: renesas: rzfive: Configure the PMA region

On the Renesas RZ/Five SoC by default we want to configure 128MiB of memory
ranging from 0x58000000 as a non-cacheable + bufferable region in the PMA
and populate this region as PMA reserve DT node with shared DMA pool and
no-map flags set so that Linux drivers requesting any DMA'able memory go
through this region.

PMA node passed to the above stack:

        reserved-memory {
            #address-cells = <2>;
            #size-cells = <2>;
            ranges;

            pma_resv0@58000000 {
                compatible = "shared-dma-pool";
                reg = <0x0 0x58000000 0x0 0x08000000>;
                no-map;
                linux,dma-default;
            };
        };

Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoplatform: generic: renesas: rzfive: Add support to configure the PMA
Lad Prabhakar [Mon, 13 Feb 2023 21:51:09 +0000 (21:51 +0000)]
platform: generic: renesas: rzfive: Add support to configure the PMA

I/O Coherence Port (IOCP) provides an AXI interface for connecting
external non-caching masters, such as DMA controllers. The accesses
from IOCP are coherent with D-Caches and L2 Cache.

IOCP is a specification option and is disabled on the Renesas RZ/Five
SoC due to this reason IP blocks using DMA will fail.

The Andes AX45MP core has a Programmable Physical Memory Attributes (PMA)
block that allows dynamic adjustment of memory attributes in the runtime.
It contains a configurable amount of PMA entries implemented as CSR
registers to control the attributes of memory locations in interest.
Below are the memory attributes supported:
* Device, Non-bufferable
* Device, bufferable
* Memory, Non-cacheable, Non-bufferable
* Memory, Non-cacheable, Bufferable
* Memory, Write-back, No-allocate
* Memory, Write-back, Read-allocate
* Memory, Write-back, Write-allocate
* Memory, Write-back, Read and Write-allocate

More info about PMA (section 10.3):
Link: http://www.andestech.com/wp-content/uploads/AX45MP-1C-Rev.-5.0.0-Datasheet.pdf
As a workaround for SoCs with IOCP disabled CMO needs to be handled by
software. Firstly OpenSBI configures the memory region as
"Memory, Non-cacheable, Bufferable" and passes this region as a global
shared dma pool as a DT node. With DMA_GLOBAL_POOL enabled all DMA
allocations happen from this region and synchronization callbacks are
implemented to synchronize when doing DMA transactions.

Example PMA region passed as a DT node from OpenSBI:
    reserved-memory {
        #address-cells = <2>;
        #size-cells = <2>;
        ranges;

        pma_resv0@58000000 {
            compatible = "shared-dma-pool";
            reg = <0x0 0x58000000 0x0 0x08000000>;
            no-map;
            linux,dma-default;
        };
    };

Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoinclude: sbi: Remove extid parameter from vendor_ext_provider() callback
Anup Patel [Mon, 13 Feb 2023 05:09:06 +0000 (10:39 +0530)]
include: sbi: Remove extid parameter from vendor_ext_provider() callback

The extid parameter of vendor_ext_provider() is redundant so let us
remove it.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi: Align SBI vendor extension id with mvendorid CSR
Anup Patel [Mon, 13 Feb 2023 04:50:30 +0000 (10:20 +0530)]
lib: sbi: Align SBI vendor extension id with mvendorid CSR

As-per the SBI specification, the lower 24bits of the SBI vendor
extension id is same as lower 24bits of the mvendorid CSR.

We update the SBI vendor extension id checking based on above.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agolib: sbi_hart: Enable hcontext and scontext
Nylon Chen [Fri, 10 Feb 2023 08:52:38 +0000 (16:52 +0800)]
lib: sbi_hart: Enable hcontext and scontext

According to the description in "riscv-state-enable[0]", to access
h/scontext in S-Mode, we need to enable the 57th bit.

If it is not enabled, an "illegal instruction" error will occur.

Link: https://github.com/riscv/riscv-state-enable/blob/a28bfae443f350d5b4c42874f428367d5b322ffe/content.adoc
Signed-off-by: Nylon Chen <nylon.chen@sifive.com>
Reviewed-by: Zong Li <zong.li@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agodocs: fix typo in fw.md
Shengyu Qu [Wed, 15 Feb 2023 18:26:28 +0000 (02:26 +0800)]
docs: fix typo in fw.md

In docs/firmware/fw.md, there's a configuration parameter called
FW_TEXT_ADDR, which actually should be FW_TEXT_START, so fix it.

Signed-off-by: Shengyu Qu <wiagn233@outlook.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: utils: fdt_fixup: Fix compile error
Xiang W [Tue, 21 Feb 2023 05:07:07 +0000 (13:07 +0800)]
lib: utils: fdt_fixup: Fix compile error

When building with GCC-10 or older versions, it throws the following
error:

 CC-DEP    platform/generic/lib/utils/fdt/fdt_fixup.dep
 CC        platform/generic/lib/utils/fdt/fdt_fixup.o
lib/utils/fdt/fdt_fixup.c: In function 'fdt_reserved_memory_fixup':
lib/utils/fdt/fdt_fixup.c:376:2: error: label at end of compound statement
  376 |  next_entry:
      |  ^~~~~~~~~~

Remove the goto statement.

Resolves: https://github.com/riscv-software-src/opensbi/issues/288

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Bin Meng <bmeng@tinylab.org>
20 months agoinclude: fdt/fdt_helper: Change fdt_get_address() to return root.next_arg1
Bin Meng [Thu, 23 Feb 2023 10:40:09 +0000 (18:40 +0800)]
include: fdt/fdt_helper: Change fdt_get_address() to return root.next_arg1

In sbi_domain_finalize(), when locating the coldboot hart's domain,
the coldboot hart's scratch->arg1 will be overwritten by the domain
configuration. However scratch->arg1 holds the FDT address of the
coldboot hart, and is still being accessed by fdt_get_address() in
later boot process. scratch->arg1 could then contain completely
garbage and lead to a crash.

To fix this, we change fdt_get_address() to return root domain's
next_arg1 as the FDT pointer.

Resolves: https://github.com/riscv-software-src/opensbi/issues/281
Fixes: b1678af210dc ("lib: sbi: Add initial domain support")
Reported-by: Marouene Boubakri <marouene.boubakri@nxp.com>
Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoplatform: generic/andes: Fix ae350.c header dependency
Bin Meng [Thu, 23 Feb 2023 10:40:08 +0000 (18:40 +0800)]
platform: generic/andes: Fix ae350.c header dependency

The code calls various macros from riscv_asm.h which is not directly
included. Fix such dependency.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoplatform: generic/allwinner: Fix sun20i-d1.c header dependency
Bin Meng [Thu, 23 Feb 2023 10:40:07 +0000 (18:40 +0800)]
platform: generic/allwinner: Fix sun20i-d1.c header dependency

The code calls various macros from riscv_asm.h and sbi_scratch.h
which are not directly included. Fix such dependency.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: utils/fdt: Fix fdt_pmu.c header dependency
Bin Meng [Thu, 23 Feb 2023 10:40:06 +0000 (18:40 +0800)]
lib: utils/fdt: Fix fdt_pmu.c header dependency

The code calls sbi_scratch_thishart_ptr() from sbi_scratch.h which
is not directly included. Fix such dependency.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi: Speed-up sbi_printf() and friends using nputs()
Anup Patel [Fri, 13 Jan 2023 09:23:53 +0000 (14:53 +0530)]
lib: sbi: Speed-up sbi_printf() and friends using nputs()

The sbi_printf() is slow for semihosting because it prints one
character at a time. To speed-up sbi_printf() for semihosting,
we use a temporary buffer and nputs().

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
20 months agolib: utils/serial: Implement console_puts() for semihosting
Anup Patel [Wed, 23 Nov 2022 10:10:17 +0000 (15:40 +0530)]
lib: utils/serial: Implement console_puts() for semihosting

We implement console_puts() for semihosting serial driver to speed-up
semihosting based prints.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
20 months agolib: sbi: Add console_puts() callback in the console device
Anup Patel [Wed, 23 Nov 2022 07:02:06 +0000 (12:32 +0530)]
lib: sbi: Add console_puts() callback in the console device

We add console_puts() callback in the console device which allows
console drivers (such as semihosting) to implement a specialized
way to output character string.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
20 months agolib: sbi: Implement SBI debug console extension
Anup Patel [Wed, 23 Nov 2022 05:41:43 +0000 (11:11 +0530)]
lib: sbi: Implement SBI debug console extension

We implement SBI debug console extension as one of the replacement
SBI extensions. This extension is only available when OpenSBI platform
provides a console device to generic library.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Bin Meng <bmeng@tinylab.org>
20 months agolib: sbi: Add sbi_domain_check_addr_range() function
Anup Patel [Wed, 23 Nov 2022 06:16:16 +0000 (11:46 +0530)]
lib: sbi: Add sbi_domain_check_addr_range() function

We add sbi_domain_check_addr_range() helper function to check
whether a given address range is accessible under a particular
domain.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
20 months agolib: sbi: Add sbi_ngets() function
Anup Patel [Wed, 4 Jan 2023 12:06:29 +0000 (17:36 +0530)]
lib: sbi: Add sbi_ngets() function

We add new sbi_ngets() which help us read characters into a
physical memory location.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
20 months agolib: sbi: Add sbi_nputs() function
Anup Patel [Fri, 22 Jul 2022 10:58:21 +0000 (16:28 +0530)]
lib: sbi: Add sbi_nputs() function

We add new sbi_nputs() which help us print a fixed number of characters
from a physical memory location.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Xiang W <wxjstz@126.com>
20 months agoinclude: Add defines for SBI debug console extension
Anup Patel [Fri, 22 Jul 2022 10:55:51 +0000 (16:25 +0530)]
include: Add defines for SBI debug console extension

We add SBI debug console extension related defines to the
SBI ecall interface header.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Xiang W <wxjstz@126.com>
20 months agoinclude: sbi: Fix BSWAPx() macros for big-endian host
Anup Patel [Thu, 9 Feb 2023 03:45:19 +0000 (09:15 +0530)]
include: sbi: Fix BSWAPx() macros for big-endian host

The BSWAPx() macros won't do any swapping for big-endian host
because the EXTRACT_BYTE() macro will pickup bytes in reverse
order. Also, the EXTRACT_BYTE() will generate compile error
for constants.

To fix this, we get remove the EXTRACT_BYTE() macro and re-write
BSWAPx() using simple mask and shift operations.

Fixes: 09b34d8cca51 ("include: Add support for byteorder/endianness
conversion")
Reported-by: Samuel Holland <samuel@sholland.org>
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
20 months agoinclude: types: Add typedefs for endianness
Rahul Pathak [Thu, 2 Feb 2023 04:44:27 +0000 (10:14 +0530)]
include: types: Add typedefs for endianness

If any variable/memory-location follows certain
endianness then its important to annotate it properly
so that proper conversion can be done before read/write
from that variable/memory.

Also, use these new typedefs in libfdt_env.h for deriving
its own custom fdtX_t types

Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: utils/fdt: Use byteorder conversion functions in libfdt_env.h
Rahul Pathak [Thu, 2 Feb 2023 04:44:26 +0000 (10:14 +0530)]
lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h

FDT follows big-endian and CPU can be little or big
endian as per the implementation.
libfdt_env.h defines function for conversion between
fdt and cpu byteorder according to the endianness.

Currently, libfdt_env.h defines custom byte swapping
macros and then undefines them. Instead, use the generic
endianness conversion functions

Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoinclude: Add support for byteorder/endianness conversion
Rahul Pathak [Thu, 2 Feb 2023 04:44:25 +0000 (10:14 +0530)]
include: Add support for byteorder/endianness conversion

Define macros general byteorder conversion
Define functions for endianness conversion
from general byteorder conversion macros

Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Sergey Matyukevich <sergey.matyukevich@syntacore.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agoMakefile: Add missing .dep files for fw_*.elf.ld
Jessica Clarke [Wed, 1 Feb 2023 16:49:16 +0000 (16:49 +0000)]
Makefile: Add missing .dep files for fw_*.elf.ld

Since we don't currently create these, changes to fw_base.ldS do not
cause the preprocessed fw_*.elf.ld files to be rebuilt, and thus
incremental builds can end up failing with missing symbols if crossing
the recent commits that introduced _fw_rw_offset and then replaced it
with _fw_rw_start.

Reported-by: Ben Dooks <ben.dooks@sifive.com>
Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi: Ensure domidx_to_domain_table is null-terminated
Andrew Jones [Mon, 30 Jan 2023 16:42:24 +0000 (17:42 +0100)]
lib: sbi: Ensure domidx_to_domain_table is null-terminated

sbi_domain_for_each() requires domidx_to_domain_table[] to be
null-terminated. Allocate one extra element which will always
be null.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: utils: Mark only the largest region as reserved in FDT
Himanshu Chauhan [Fri, 27 Jan 2023 04:19:23 +0000 (09:49 +0530)]
lib: utils: Mark only the largest region as reserved in FDT

In commit 230278dcf, RX and RW regions were marked separately.
When the RW region grows (e.g. with more harts) and it isn't a
power-of-two, sbi_domain_memregion_init will upgrade the region
to the next power-of-two. This will make RX and RW both start
at the same base address, like so (with 64 harts):
Domain0 Region01 : 0x0000000080000000-0x000000008001ffff M: (R,X) S/U: ()
Domain0 Region02 : 0x0000000080000000-0x00000000800fffff M: (R,W) S/U: ()

This doesn't break the permission enforcement because of static
priorities in PMP but makes the kernel complain about the regions
overlapping each other. Like so:
[    0.000000] OF: reserved mem: OVERLAP DETECTED!
[    0.000000] mmode_resv0@80000000 (0x0000000080000000--0x0000000080020000) \
overlaps with mmode_resv1@80000000 (0x0000000080000000--0x0000000080100000)

To fix this warning, among the multiple regions having same base
address but different sizes, add only the largest region as reserved
region during fdt fixup.

Fixes: 230278dcf (lib: sbi: Add separate entries for firmware RX and RW regions)
Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agolib: sbi_hsm: Use csr_set to restore the MIP
Nick Hu [Tue, 17 Jan 2023 08:14:28 +0000 (16:14 +0800)]
lib: sbi_hsm: Use csr_set to restore the MIP

If we use the csr_write to restore the MIP, we may clear the SEIP.
In generic behavior of QEMU, if the pending bits of PLIC are set and we
clear the SEIP, the QEMU may not set it back immediately. It may cause
the interrupts won't be handled anymore until the new interrupts arrived
and QEMU set the bits back.

Signed-off-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Jim Shu <jim.shu@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
20 months agofirmware: Not to clear all the MIP
Nick Hu [Tue, 17 Jan 2023 08:14:27 +0000 (16:14 +0800)]
firmware: Not to clear all the MIP

In generic behavior of QEMU, if the pending bits of PLIC are still set and
we clear the SEIP, the QEMU may not set the SEIP back immediately and the
interrupt may not be handled anymore until the new interrupts arrived and
QEMU set the SEIP back which is a generic behavior in QEMU.

Signed-off-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Jim Shu <jim.shu@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agofirmware: Fix fw_rw_offset computation in fw_base.S
Jessica Clarke [Thu, 26 Jan 2023 15:40:03 +0000 (21:10 +0530)]
firmware: Fix fw_rw_offset computation in fw_base.S

It seems BFD just does totally nonsensical things for SHN_ABS symbols
when producing position-independent outputs (both -pie and -shared)
for various historical reasons, and so SHN_ABS symbols are still
subject to relocation as far as BFD is concerned (except AArch64,
which fixes it in limited cases that don’t apply here...).

The above affects the _fw_rw_offset provided through fw_base.ldS
linker script which results in OpenSBI firmware failing to boot
when loaded at an address different from FW_TEXT_START.

Fixes: c10e3fe5f9a1 ("firmware: Add RW section offset in scratch")
Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
Reported-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Tested-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Tested-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
21 months agoplatform: generic: allwinner: Advertise nonretentive suspend
Samuel Holland [Mon, 23 Jan 2023 06:32:07 +0000 (00:32 -0600)]
platform: generic: allwinner: Advertise nonretentive suspend

Add D1's nonretentive suspend state to the devicetree so S-mode software
knows about it and can use it.

Latency and power measurements were taken on an Allwinner Nezha board:
 - Entry latency was measured from the beginning of sbi_ecall_handler()
   to before the call to wfi() in sun20i_d1_hart_suspend().
 - Exit latency was measured from the beginning of sbi_init() to before
   the call to sbi_hart_switch_mode() in init_warmboot().
 - There was a 17.5 mW benefit from non-retentive suspend compared to
   WFI, with a 170 mW cost during the 107 us entry/exit period. This
   provides a break-even point around 1040 us. Residency includes entry
   latency, so round this up to 1100 us.
 - The hardware power sequence latency (after the WFI) is assumed to be
   negligible, so set the wakeup latency to the exit latency.

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Samuel Holland <samuel@sholland.org>
21 months agolib: utils: Add fdt_add_cpu_idle_states() helper function
Samuel Holland [Mon, 23 Jan 2023 06:32:06 +0000 (00:32 -0600)]
lib: utils: Add fdt_add_cpu_idle_states() helper function

Since the availability and latency properties of CPU idle states depend
on the specific SBI HSM implementation, it is appropriate that the idle
states are added to the devicetree at runtime by that implementation.

This helper function adds a platform-provided array of idle states to
the devicetree, following the SBI idle state binding.

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Samuel Holland <samuel@sholland.org>
21 months agoplatform: renesas/rzfive: Configure Local memory regions as part of root domain
Lad Prabhakar [Fri, 13 Jan 2023 16:47:19 +0000 (16:47 +0000)]
platform: renesas/rzfive: Configure Local memory regions as part of root domain

Renesas RZ/Five RISC-V SoC has Instruction local memory and Data local
memory (ILM & DLM) mapped between region 0x30000 - 0x4FFFF. When a
virtual address falls within this range, the MMU doesn't trigger a page
fault; it assumes the virtual address is a physical address which can
cause undesired behaviours for statically linked applications/libraries.

To avoid this, add the ILM/DLM memory regions to the root domain region
of the PMPU with permissions set to 0x0 for S/U modes so that any access
to these regions gets blocked and for M-mode we grant full access (R/W/X).

Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi: Add separate entries for firmware RX and RW regions
Himanshu Chauhan [Thu, 19 Jan 2023 15:18:26 +0000 (20:48 +0530)]
lib: sbi: Add separate entries for firmware RX and RW regions

Add two entries for firmware in the root domain:

1. TEXT: fw_start to _fw_rw_offset with RX permissions
2. DATA: _fw_rw_offset to fw_size with RW permissions

These permissions are still not enforced from M-mode but lay
the ground work for enforcing them for M-mode. SU-mode don't
have any access to these regions.

Sample output:
 Domain0 Region01  : 0x0000000080000000-0x000000008001ffff M: (R,X) S/U: ()
 Domain0 Region02  : 0x0000000080020000-0x000000008003ffff M: (R,W) S/U: ()

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi: Print the RW section offset
Himanshu Chauhan [Thu, 19 Jan 2023 15:18:25 +0000 (20:48 +0530)]
lib: sbi: Print the RW section offset

Print the RW section offset when firmware base and size is
being printed.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agofirmware: Add RW section offset in scratch
Himanshu Chauhan [Thu, 19 Jan 2023 15:18:24 +0000 (20:48 +0530)]
firmware: Add RW section offset in scratch

Add the RW section offset, provided by _fw_rw_offset symbol,
to the scratch structure. This will be used to program
separate pmp entry for RW section.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agofirmware: Move dynsym and reladyn sections to RX section
Himanshu Chauhan [Thu, 19 Jan 2023 15:18:23 +0000 (20:48 +0530)]
firmware: Move dynsym and reladyn sections to RX section

Currently, the dynsym and reladyn sections are under RW data.
They are moved to the Read-only/Executable region.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agofirmware: Split RO/RX and RW sections
Himanshu Chauhan [Thu, 19 Jan 2023 15:18:22 +0000 (20:48 +0530)]
firmware: Split RO/RX and RW sections

Split the RO/RX and RW sections so that they can have
independent pmp entries with required permissions. The
split size is ensured to be a power-of-2 as required by
pmp.

_fw_rw_offset symbol marks the beginning of the data
section.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: utils: Fix reserved memory node for firmware memory
Mayuresh Chitale [Sat, 21 Jan 2023 06:45:59 +0000 (12:15 +0530)]
lib: utils: Fix reserved memory node for firmware memory

The commit 9e0ba090 introduced more fine grained permissions for memory
regions and did not update the fdt_reserved_memory_fixup() function. As
a result, the fdt_reserved_memory_fixup continued to use the older coarse
permissions which causes the reserved memory node to be not inserted
into the DT.

To fix the above issue, we correct the flags used for memory region
permission checks in the fdt_reserved_memory_fixup() function.

Fixes: 9e0ba090 ("include: sbi: Fine grain the permissions for M and SU modes")
Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: reset/fdt_reset_atcwdt200: Use defined macros and function in atcsmu.h
Yu Chien Peter Lin [Fri, 20 Jan 2023 03:05:12 +0000 (11:05 +0800)]
lib: reset/fdt_reset_atcwdt200: Use defined macros and function in atcsmu.h

Reuse the smu related macros and function in atcsmu.h.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agoplatform: andes/ae350: Implement hart hotplug using HSM extension
Yu Chien Peter Lin [Fri, 20 Jan 2023 03:05:11 +0000 (11:05 +0800)]
platform: andes/ae350: Implement hart hotplug using HSM extension

Add hart_start() and hart_stop() callbacks for the multi-core ae350
platform, it utilizes the ATCSMU to put the harts into power-gated
deep sleep mode. The programming sequence is stated as below:

1. Set the wakeup events to PCSm_WE
2. Set the sleep command to PCSm_CTL
3. Set the reset vector to HARTm_RESET_VECTOR_{LO|HI}
4. Write back and invalidate D-cache by executing the CCTL command L1D_WBINVAL_ALL
5. Disable I/D-cache by clearing mcache_ctl.{I|D}C_EN
6. Disable D-cache coherency by clearing mcache_ctl_.DC_COHEN
7. Wait for mcache_ctl.DC_COHSTA to be cleared to ensure the previous step is completed
8. Execute WFI

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: utils: atcsmu: Add Andes System Management Unit support
Yu Chien Peter Lin [Fri, 20 Jan 2023 03:05:10 +0000 (11:05 +0800)]
lib: utils: atcsmu: Add Andes System Management Unit support

This patch adds atcsmu support for Andes AE350 platforms. The SMU
provides system management capabilities, including clock, reset
and power control based on power domain partitions.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agoinclude: types: add always inline compiler attribute
Yu Chien Peter Lin [Fri, 20 Jan 2023 03:05:09 +0000 (11:05 +0800)]
include: types: add always inline compiler attribute

Provide __always_inline to sbi_types header.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi_hsm: handle failure when hart_stop returns SBI_ENOTSUPP
Yu Chien Peter Lin [Fri, 20 Jan 2023 03:05:08 +0000 (11:05 +0800)]
lib: sbi_hsm: handle failure when hart_stop returns SBI_ENOTSUPP

Make use of generic warm-boot path when platform hart_stop callback
returns SBI_ENOTSUPP, in case certain hart can not turn off its
power domain, or it detects some error occured in power management
unit, it can fall through warm-boot flow and wait for interrupt in
sbi_hsm_hart_wait().

Also improves comment in sbi_hsm_hart_wait().

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agodocs: generic.md: fix typo of andes-ae350
Yu Chien Peter Lin [Fri, 20 Jan 2023 03:05:07 +0000 (11:05 +0800)]
docs: generic.md: fix typo of andes-ae350

Fix hyperlink due to the typo.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agoplatform: generic: allwinner: Fix PLIC array bounds
Samuel Holland [Tue, 27 Dec 2022 18:44:44 +0000 (12:44 -0600)]
platform: generic: allwinner: Fix PLIC array bounds

The two referenced commits passed incorrect bounds to the PLIC save/
restore functions, causing out-of-bounds memory access. The functions
expect "num" to be the 1-based number of interrupt sources, equivalent
to the "riscv,ndev" devicetree property. Thus, "num" must be strictly
smaller than the 0-based size of the array storing the register values.

However, the referenced commits incorrectly passed in the unmodified
size of the array as "num". Fix this by reducing PLIC_SOURCES (matching
"riscv,ndev" on this platform), while keeping the same array sizes.

Addresses-Coverity-ID: 1530251 ("Out-of-bounds access")
Addresses-Coverity-ID: 1530252 ("Out-of-bounds access")
Fixes: 8509e46ca63a ("lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers")
Fixes: 9a2eeb4aaeac ("lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers")
Signed-off-by: Samuel Holland <samuel@sholland.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agodocs: Update domain's region permissions and requirements
Himanshu Chauhan [Mon, 9 Jan 2023 05:20:43 +0000 (05:20 +0000)]
docs: Update domain's region permissions and requirements

Updated the various permissions bits available for domains
defined in DT node and restrictions on them.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
21 months agolib: utils: Add M-mode {R/W} flags to the MMIO regions
Himanshu Chauhan [Mon, 9 Jan 2023 05:20:42 +0000 (05:20 +0000)]
lib: utils: Add M-mode {R/W} flags to the MMIO regions

Add the M-mode readable/writable flags to mmio regions
of various drivers.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
21 months agolib: utils: Disallow non-root domains from adding M-mode regions
Himanshu Chauhan [Mon, 9 Jan 2023 05:20:41 +0000 (05:20 +0000)]
lib: utils: Disallow non-root domains from adding M-mode regions

The M-mode regions can only be added to the root domain. The non-root
domains shouldn't be able to add them from FDT.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
21 months agolib: utils: Use SU-{R/W/X} flags for region permissions during parsing
Himanshu Chauhan [Mon, 9 Jan 2023 05:20:40 +0000 (05:20 +0000)]
lib: utils: Use SU-{R/W/X} flags for region permissions during parsing

Use the newer SU-{R/W/X} flags for checking and assigning region
permissions.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi: Modify the boot time region flag prints
Himanshu Chauhan [Mon, 9 Jan 2023 05:20:39 +0000 (05:20 +0000)]
lib: sbi: Modify the boot time region flag prints

With the finer permission semantics, the region access
permissions must be displayed separately for M and SU mode.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi: Use finer permission sematics to decide on PMP bits
Himanshu Chauhan [Mon, 9 Jan 2023 05:20:38 +0000 (05:20 +0000)]
lib: sbi: Use finer permission sematics to decide on PMP bits

Use the fine grained permission bits to decide if the region
permissions are to be enforced on all modes. Also use the new
permission bits for deciding on R/W/X bits in pmpcfg register.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi: Add permissions for the firmware start till end
Himanshu Chauhan [Mon, 9 Jan 2023 05:20:37 +0000 (05:20 +0000)]
lib: sbi: Add permissions for the firmware start till end

Change the zero flag to M-mode R/W/X flag for the firmware
region.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi: Use finer permission semantics for address validation
Himanshu Chauhan [Mon, 9 Jan 2023 05:20:36 +0000 (05:20 +0000)]
lib: sbi: Use finer permission semantics for address validation

Use the fine grained permisssion semantics for address validation
of a given region.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
21 months agoinclude: sbi: Fine grain the permissions for M and SU modes
Himanshu Chauhan [Mon, 9 Jan 2023 05:20:35 +0000 (05:20 +0000)]
include: sbi: Fine grain the permissions for M and SU modes

Split the permissions for M-mode and SU-mode. This would
help if different sections of OpenSBI need to be given
different permissions and if M-mode has different permisssions
than the SU-mode over a region.

Signed-off-by: Himanshu Chauhan <hchauhan@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Anup Patel <anup@brainfault.org>
21 months agodocs: domain_support: Use capital letter for privilege modes
Bin Meng [Fri, 30 Dec 2022 05:07:51 +0000 (13:07 +0800)]
docs: domain_support: Use capital letter for privilege modes

The RISC-V convention for the privilege mode is capital letter, like
'M-mode', instead of 'm-mode'.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agolib: sbi_hsm: Rename 'priv' argument to 'arg1'
Bin Meng [Fri, 30 Dec 2022 05:07:23 +0000 (13:07 +0800)]
lib: sbi_hsm: Rename 'priv' argument to 'arg1'

'priv' argument of sbi_hsm_hart_start() and sbi_hsm_hart_suspend()
may mislead people to think it stands for 'privilege mode', but it
is not. Change it to 'arg1' to clearly indicate the a1 register.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Samuel Holland <samuel@sholland.org>
Tested-by: Samuel Holland <samuel@sholland.org>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agogeneric/starfive: Add Starfive JH7110 platform implementation
Wei Liang Lim [Thu, 29 Dec 2022 02:56:18 +0000 (10:56 +0800)]
generic/starfive: Add Starfive JH7110 platform implementation

Add Starfive JH7110 platform implementation

Signed-off-by: Wei Liang Lim <weiliang.lim@starfivetech.com>
Reviewed-by: Chee Hong Ang <cheehong.ang@starfivetech.com>
Reviewed-by: Jun Liang Tan <junliang.tan@starfivetech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agoplatform: generic: Allow platform_override to perform firmware init
Wei Liang Lim [Thu, 29 Dec 2022 02:56:17 +0000 (10:56 +0800)]
platform: generic: Allow platform_override to perform firmware init

We add a generic platform override callback to allow platform specific firmware init.

Signed-off-by: Wei Liang Lim <weiliang.lim@starfivetech.com>
Reviewed-by: Chee Hong Ang <cheehong.ang@starfivetech.com>
Reviewed-by: Jun Liang Tan <junliang.tan@starfivetech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
21 months agoplatform: generic: Allow platform_override to select cold boot HART
Anup Patel [Thu, 29 Dec 2022 02:56:16 +0000 (10:56 +0800)]
platform: generic: Allow platform_override to select cold boot HART

We add a generic platform override callback to allow platform specific
selection of cold boot HART.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
21 months agolib: sbi: Allow platform to influence cold boot HART selection
Anup Patel [Thu, 29 Dec 2022 02:56:15 +0000 (10:56 +0800)]
lib: sbi: Allow platform to influence cold boot HART selection

We add an optional cold_boot_allowed() platform callback which allows
platform support to decide which HARTs can do cold boot initialization.

If this platform callback is not available then any HART can do cold
boot initialization.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
22 months agolib: utils/sys: Allow custom HTIF base address for RV32
Bin Meng [Mon, 26 Dec 2022 03:36:03 +0000 (11:36 +0800)]
lib: utils/sys: Allow custom HTIF base address for RV32

commit 6dde43584f18 ("lib: utils/sys: Extend HTIF library to allow custom base address")
forgot to update do_tohost_fromhost() codes for RV32, which still
accesses the HTIF registers using the ELF symbol address directly.

Fixes: 6dde43584f18 ("lib: utils/sys: Extend HTIF library to allow custom base address")
Signed-off-by: Bin Meng <bmeng@tinylab.org>
Tested-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agoMakefile: Remove -N ldflag to prevent linker RWX warning
Rahul Pathak [Sun, 25 Dec 2022 03:22:11 +0000 (08:52 +0530)]
Makefile: Remove -N ldflag to prevent linker RWX warning

-N option coalesce all sections into single LOAD segment which causes
data and other sections to have executable permission causing warning
with new binutils ld 2.39.
New ld emits warning when any segment have all three permissions RWX.

ld.bfd: warning: test.elf has a LOAD segment with RWX permissions
ld.bfd: warning: fw_dynamic.elf has a LOAD segment with RWX permissions
ld.bfd: warning: fw_jump.elf has a LOAD segment with RWX permissions
ld.bfd: warning: fw_payload.elf has a LOAD segment with RWX permissions

This option was added in below commit -
commit: eeab92f2423e ("Makefile: Convert to a more standard format")

Removing -N option allows to have text and rodata into one LOAD
segment and other sections into separate LOAD segment which prevents
RWX permissions on single LOAD segment. Here X == E

Current
 LOAD           0x0000000000000120 0x0000000080000000 0x0000000080000000
                 0x000000000001d4d0 0x0000000000032ed8  RWE    0x10

-N removed
  LOAD           0x0000000000001000 0x0000000080000000 0x0000000080000000
                 0x00000000000198cc 0x00000000000198cc  R E    0x1000
  LOAD           0x000000000001b000 0x000000008001a000 0x000000008001a000
                 0x00000000000034d0 0x0000000000018ed8  RW     0x1000

Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Tested-by: Samuel Holland <samuel@sholland.org>
22 months agotreewide: Replace TRUE/FALSE with true/false
Bin Meng [Wed, 21 Dec 2022 11:38:06 +0000 (19:38 +0800)]
treewide: Replace TRUE/FALSE with true/false

C language standard uses true/false for the boolean type.
Let's switch to that for better language compatibility.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Samuel Holland <samuel@sholland.org>
Tested-by: Samuel Holland <samuel@sholland.org>
22 months agoinclude: Bump-up version to 1.2 v1.2
Anup Patel [Sat, 24 Dec 2022 06:06:49 +0000 (11:36 +0530)]
include: Bump-up version to 1.2

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

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
22 months agodocs: pmu: Update the pmu doc with removal of mcountinhbit restriction
Atish Patra [Thu, 22 Dec 2022 20:38:50 +0000 (12:38 -0800)]
docs: pmu: Update the pmu doc with removal of mcountinhbit restriction

Since commit b28f070, it is possible for platforms to run perf monitoring
even if mcountinhibit is not supported. Sampling still won't be possible
though as it requires sscofpmf extension.

Update the docs to remove the restriction.

Signed-off-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agodocs: pmu: update a reference to a deprecated property name
Conor Dooley [Thu, 22 Dec 2022 19:39:14 +0000 (19:39 +0000)]
docs: pmu: update a reference to a deprecated property name

event-to-mhpmevent was deprecated and replaced by
riscv,event-to-mhpmevent, but a reference remains to the old name.
Replace it with the new one.

Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agodocs: pmu: fix binding example
Conor Dooley [Wed, 21 Dec 2022 13:59:40 +0000 (13:59 +0000)]
docs: pmu: fix binding example

The first PMU binding example does not terminate properties with a ;,
which is invalid. Noticed while converting the binding to yaml.

Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
Reviewed-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: utils/timer: mtimer: add T-Head C9xx CLINT compatible
Icenowy Zheng [Mon, 12 Dec 2022 08:22:38 +0000 (16:22 +0800)]
lib: utils/timer: mtimer: add T-Head C9xx CLINT compatible

As we already added the quirk for lacking mtime register to MTIMER
driver, add T-Head C9xx CLINT compatible to it and wire the quirk.

Signed-off-by: Icenowy Zheng <uwu@icenowy.me>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: utils/timer: mtimer: add a quirk for lacking mtime register
Icenowy Zheng [Mon, 12 Dec 2022 08:22:37 +0000 (16:22 +0800)]
lib: utils/timer: mtimer: add a quirk for lacking mtime register

T-Head developers surely have a different understanding of time CSR and
CLINT's mtime register with SiFive ones, that they did not implement
the mtime register at all -- as shown in openC906 source code, their
time CSR value is just exposed at the top of their processor IP block
and expects an external continous counter, which makes it not
overrideable, and thus mtime register is not implemented, even not for
reading. However, if CLINTEE is not enabled in T-Head's MXSTATUS
extended CSR, these systems still rely on the mtimecmp registers to
generate timer interrupts. This makes it necessary to implement T-Head
C9xx CLINT support in OpenSBI MTIMER driver, which skips implementing
reading mtime register and falls back to default code that reads time
CSR.

Add a quirk into MTIMER driver, which represents a mtime register is
lacking and time CSR value should be used instead.

Signed-off-by: Icenowy Zheng <uwu@icenowy.me>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: utils/ipi: mswi: add T-Head C9xx CLINT compatible
Icenowy Zheng [Mon, 12 Dec 2022 08:22:36 +0000 (16:22 +0800)]
lib: utils/ipi: mswi: add T-Head C9xx CLINT compatible

Althoug the MTIMER part of a C9xx CLINT differs from a SiFive one, the
MSWI part is compliant.

Add T-Head C9xx CLINT compatible string to fdt_ipi_mswi code, sharing
the same codepath with SiFive CLINT.

Signed-off-by: Icenowy Zheng <uwu@icenowy.me>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers
Bin Meng [Sun, 11 Dec 2022 06:54:24 +0000 (14:54 +0800)]
lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers

Currently the context save/restore helpers writes/reads the provided
array using an index whose maximum value is determined by PLIC, which
potentially may disagree with the caller to these helpers.

Add a parameter to ask the caller to provide the size limit of the
array to ensure no out-of-bound access happens.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: utils/irqchip: plic: Fix the off-by-one error in context save/restore helpers
Bin Meng [Sun, 11 Dec 2022 06:54:23 +0000 (14:54 +0800)]
lib: utils/irqchip: plic: Fix the off-by-one error in context save/restore helpers

plic->num_src holds the number of interrupt sources without interrupt
source 0 but the interrupt enable register includes a bit for the
interrupt source 0 in the first word.

Fixes: 415ecf28f7ad ("lib: irqchip/plic: Add context save/restore helpers")
Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Samuel Holland <samuel@sholland.org>
22 months agolib: utils/irqchip: plic: Fix the off-by-one error in plic_context_init()
Bin Meng [Sun, 11 Dec 2022 06:54:22 +0000 (14:54 +0800)]
lib: utils/irqchip: plic: Fix the off-by-one error in plic_context_init()

The number of interrupt enable register in words was once correct,
but was wrongly changed to have an off-by-one error since
commit 8c362e7d065e ("lib: irqchip/plic: Factor out a context init function").

Fixes: 8c362e7d065e ("lib: irqchip/plic: Factor out a context init function")
Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Samuel Holland <samuel@sholland.org>
22 months agolib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore...
Bin Meng [Sun, 11 Dec 2022 06:54:21 +0000 (14:54 +0800)]
lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers

Currently the priority save/restore helpers writes/reads the provided
array using an index whose maximum value is determined by PLIC, which
potentially may disagree with the caller to these helpers.

Add a parameter to ask the caller to provide the size limit of the
array to ensure no out-of-bound access happens.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>
22 months agolib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
Bin Meng [Sun, 11 Dec 2022 06:54:20 +0000 (14:54 +0800)]
lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers

Interrupt source 0 is reserved. Hence the irq should start from 1.

Fixes: 2b79b694a805 ("lib: irqchip/plic: Add priority save/restore helpers")
Signed-off-by: Bin Meng <bmeng@tinylab.org>
Reviewed-by: Anup Patel <anup@brainfault.org>