platform/kernel/opensbi.git
3 years agoinclude: sbi: Allow direct initialization via SPIN_LOCK_INIT()
Christoph Muellner [Tue, 6 Apr 2021 01:53:53 +0000 (03:53 +0200)]
include: sbi: Allow direct initialization via SPIN_LOCK_INIT()

The current implementation of SPIN_LOCK_INIT() provides the spinlock
to be initialized as reference. This does not allow a direct
initialization of the spinlock object at the creation site.

Let's pass the spinlock directly instead (like Linux does as well)
and adjust all users of the macro (in fact there is only one user).

Signed-off-by: Christoph Muellner <cmuellner@linux.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agoinclude: types: Add __aligned(x) to define the minimum alignement
Christoph Muellner [Tue, 6 Apr 2021 01:53:52 +0000 (03:53 +0200)]
include: types: Add __aligned(x) to define the minimum alignement

The __aligned(x) macro is a common wrapper around compiler's
aligned attribute, which allow to define the minimum alignement
of a data type. Let's add this macro.

Signed-off-by: Christoph Muellner <cmuellner@linux.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agofirmware: Remove redundant add instruction from trap restore path
Anup Patel [Thu, 1 Apr 2021 11:01:00 +0000 (16:31 +0530)]
firmware: Remove redundant add instruction from trap restore path

The "add sp, a0, zero" instruction in the trap restore path is redundant
and can be avoided if TRAP_RESTORE_xyz() assembly macros use a0 as the
base register instead of sp.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Xiang W <wxjstz@126.com>
3 years agolib: fix csr detect support
Xiang W [Thu, 1 Apr 2021 07:59:40 +0000 (15:59 +0800)]
lib: fix csr detect support

csr_read_allowed/csr_read_allowed requires trap.case to detect the results,
but if no exception occurs, the value of trap.case will remain unchanged,
which makes the detection results unreliable. Add code to initialize
trap.case to 0.

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agofirware: optimize the exception exit code
Xiang W [Tue, 30 Mar 2021 12:07:35 +0000 (20:07 +0800)]
firware: optimize the exception exit code

There are two copies of the same abnormal exit code, this patch deletes one

Signed-off-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agolib/utils: Support fixing up the official DT bindings of PLIC
Bin Meng [Sat, 27 Mar 2021 05:05:27 +0000 (13:05 +0800)]
lib/utils: Support fixing up the official DT bindings of PLIC

Current fdt_plic_fixup() only does necessary fix-up against the legacy
"riscv,plic0" node. The upstream Linux kernel defines its official DT
bindings which uses "sifive,plic-1.0.0" as the compatible string and
we should check that first, and if not present fall back to legacy.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agolib/utils: Drop the 'compat' parameter of fdt_plic_fixup()
Bin Meng [Sat, 27 Mar 2021 05:05:26 +0000 (13:05 +0800)]
lib/utils: Drop the 'compat' parameter of fdt_plic_fixup()

At present fdt_plic_fixup() accepts a 'compat' parameter for PLIC
compatible string. In preparation to support the new DT bindings,
drop this and use "riscv,plic0" directly in fdt_plic_fixup().

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agolib/utils: Support the official clint DT bindings
Bin Meng [Sat, 27 Mar 2021 05:05:25 +0000 (13:05 +0800)]
lib/utils: Support the official clint DT bindings

Linux kernel commit a2770b57d083 ("dt-bindings: timer: Add CLINT bindings")
adds the official DT bindings for CLINT, which uses "sifive,clint0"
as the compatible string. "riscv,clint0" is now legacy and has to
be kept for backward compatibility of legacy systems.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agoinclude: headers: Replace __ASSEMBLY__ with __ASSEMBLER__
Marouene Boubakri [Fri, 12 Mar 2021 11:00:01 +0000 (12:00 +0100)]
include: headers: Replace __ASSEMBLY__ with __ASSEMBLER__

GCC has already a predefined macro __ASSEMBLER__ therefore, it can be
used without the need to define a new flag with -D__ASSEMBLY__.
This is useful when adding the library to projects having a build
system such one can build without the need to make changes.
THe build system does not use the Makefile in the sources tree.

Signed-off-by: Marouene Boubakri <marouene.boubakri@nxp.com>
Signed-off-by: Anup Patel <anup.patel@wdc.com>
3 years agolib: sbi: illegal CSR 0x306 access in hpm_allowed()
Heinrich Schuchardt [Fri, 19 Mar 2021 10:04:50 +0000 (11:04 +0100)]
lib: sbi: illegal CSR 0x306 access in hpm_allowed()

The trap handler sbi_emulate_csr_read() invokes hpm_allowed() which reads
CSR 0x306 (mcounteren). The K210 does not support CSR 0x306. While trying
to handle a trap occurring in S-mode code this creates an additional trap
in M-mode. This results in failure to redirect to S-mode and the system
hanging in sbi_hart_hang().

In hart_detect_features() we have already determined if CSR 0x306 is
available and stored that information in the scratch area. We can use this
information to decide if CSR 0x306 shall be accessed in hpm_allowed() and
thus avoid the M-mode trap.

Likewise if CSR scounteren is not available we have to avoid reading CSR
0x106.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agofirmware: Support position independent execution
Vincent Chen [Wed, 17 Mar 2021 01:16:38 +0000 (09:16 +0800)]
firmware: Support position independent execution

Enable OpenSBI to support position independent execution. Because the
position independent code will cause an additional GOT reference when
accessing the global variables, it will reduce performance a bit. Therefore,
the position independent execution is disabled by default. Users can
through specifying "FW_PIC=y" on the make command to enable this feature.

In theory, after enabling position-independent execution, the OpenSBI
can run at arbitrary address with appropriate alignment. Therefore, the
original relocation mechanism will be skipped. In other words, OpenSBI will
directly run at the load address without any code movement.

Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agofirmware: Use lla to access all global symbols
Vincent Chen [Wed, 17 Mar 2021 01:16:37 +0000 (09:16 +0800)]
firmware: Use lla to access all global symbols

When OpenSBI is compiled as fPIE mode, the assembler will translate "la"
to GOT reference pattern. It will cause to cost an additional load
instruction when obtaining the symbol address. However, if the symbol
locates within the positive or negative 2GB region, we can use "lla"
instead of "la" to avoid unneeded GOT references. This patch assumes that
the OpenSBI image excluding the payload does not exceed 2GB. Based on
this assumption, all "la" instructions are replaced by "lla" to avoid
performance degradation when compiling as fPIE mode.

Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agoinclude: sbi: SBI function IDs for RFENCE extension
Heinrich Schuchardt [Tue, 9 Mar 2021 11:31:22 +0000 (12:31 +0100)]
include: sbi: SBI function IDs for RFENCE extension

The SBI function IDs for RFENCE extension must match the SBI specification.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: Remove redundant sbi_platform_ipi_clear() calls
Anup Patel [Fri, 19 Feb 2021 04:24:05 +0000 (09:54 +0530)]
lib: Remove redundant sbi_platform_ipi_clear() calls

The sbi_platform_ipi_clear() called from wait_for_coldboot() and
sbi_hsm_hart_wait() is redundant because IPI will be automatically
cleared by sbi_platform_ipi_init() called from sbi_ipi_init().

Further, wait_for_coldboot() is common for warm startup and warm
resume path so the sbi_platform_ipi_clear() called in warm resume
path cause resuming HART to miss an IPI injected other HART to
wakeup the HART.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Upgrade SBI implementation version to v0.3
Anup Patel [Fri, 12 Feb 2021 06:28:50 +0000 (11:58 +0530)]
include: sbi: Upgrade SBI implementation version to v0.3

The OpenSBI SBI implementation is now compliant with latest draft
SBI v0.3 specification so let's upgrade SBI implementation version.

This will also help HSM suspend function detection in S-mode because
HSM suspend function is only present when HSM extension is present
and SBI implementation version is 0.3 (or higher).

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Implement SBI HSM suspend function
Anup Patel [Sat, 6 Feb 2021 09:18:56 +0000 (14:48 +0530)]
lib: sbi: Implement SBI HSM suspend function

This patch implements the SBI HSM suspend function. Using this
new SBI call, the S-mode software can put calling HART in platform
specific suspend (i.e. low-power) state. For a successful retentive
suspend, the SBI call will return without errors upon resuming
whereas for a successful non-retentive suspend, the SBI call will
resume from a user provided resume address.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Add hart_suspend() platform callback
Anup Patel [Fri, 5 Feb 2021 12:17:48 +0000 (17:47 +0530)]
include: sbi: Add hart_suspend() platform callback

We add hart_suspend() callback in platform operations which will
be used by HSM implementation to enter retentive or non-retentive
suspend state.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Add sbi_hart_reinit() function
Anup Patel [Fri, 5 Feb 2021 12:02:30 +0000 (17:32 +0530)]
lib: sbi: Add sbi_hart_reinit() function

We add sbi_hart_reinit() function which will re-initialize HART CSRs
assuming HART features are already detected. This new function will
be useful in re-initializing HART after it resumes from HSM SUSPENDED
state.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Add HSM suspend related defines
Anup Patel [Mon, 1 Feb 2021 11:54:54 +0000 (17:24 +0530)]
include: sbi: Add HSM suspend related defines

This patch adds SBI HSM suspend related defines to ecall interface
header.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Fix error codes returned by HSM start() and stop() functions
Anup Patel [Wed, 24 Feb 2021 09:56:44 +0000 (15:26 +0530)]
lib: sbi: Fix error codes returned by HSM start() and stop() functions

The sbi_hsm_hart_start() and sbi_hsm_hart_stop() functions should
only return error codes as defined by the SBI specification.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Remove redundant sbi_hsm_hart_started() function
Anup Patel [Tue, 2 Feb 2021 05:06:14 +0000 (10:36 +0530)]
lib: sbi: Remove redundant sbi_hsm_hart_started() function

The sbi_hsm_hart_started() function is only used by sbi_hsm_hart_stop()
for checking state of calling HART and current domain assignment.

The atomic_cmpxchg() called by sbi_hsm_hart_stop() will check state of
calling hart anyway and domain assignment can be checked by other domain
function such as sbi_domain_is_assigned_hart().

This means sbi_hsm_hart_started() is redundant and can be removed.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Rename sbi_hsm_hart_started_mask() function
Anup Patel [Tue, 2 Feb 2021 04:21:54 +0000 (09:51 +0530)]
lib: sbi: Rename sbi_hsm_hart_started_mask() function

A hart can take interrupt in the new HSM states introduced by the
SBI HSM suspend function (such as SUSPENDED state) so we rename
sbi_hsm_hart_started_mask() to something more generic such as
sbi_hsm_hart_interruptible_mask().

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Use SBI_HSM_STATE_xyz defines instead of SBI_STATE_xyz defines
Anup Patel [Mon, 1 Feb 2021 10:43:03 +0000 (16:13 +0530)]
lib: sbi: Use SBI_HSM_STATE_xyz defines instead of SBI_STATE_xyz defines

We replace the use of SBI_STATE_xyz defines with SBI_HSM_STATE_xyz
defines because the HSM state defines are complete enough to implement
HSM state machine in OpenSBI. As a result of this, we can now remove
sbi_hsm_hart_state_to_status() function because it is now redundant
and sbi_hsm_hart_get_state() can directly return HSM state or error.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Simplify HSM state define names
Anup Patel [Mon, 1 Feb 2021 04:42:47 +0000 (10:12 +0530)]
include: sbi: Simplify HSM state define names

We simplify HSM state define names so that these defines can directly
replace SBI_HART_xyz defines used by SBI HSM implementation.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoplatform: implement K210 system reset
Heinrich Schuchardt [Fri, 26 Feb 2021 09:25:10 +0000 (09:25 +0000)]
platform: implement K210 system reset

Implement rebooting the K210 via the system reset extension.

All reset types are treated in the same way.
A request for shutdown results in a reboot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Damien Le Moal <damien.lemoal@wdc.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agoinclude: Bump-up version to 0.9 v0.9
Anup Patel [Mon, 18 Jan 2021 13:17:19 +0000 (18:47 +0530)]
include: Bump-up version to 0.9

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

Signed-off-by: Anup Patel <anup.patel@wdc.com>
3 years agoplatform: template: Fix compile error
Anup Patel [Mon, 18 Jan 2021 03:59:33 +0000 (09:29 +0530)]
platform: template: Fix compile error

This clint driver init functions were incorrectly used in
template platform which resulted in compile error. This patch
fixes clint driver function usage in template platform.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Don't pack struct sbi_platform and sbi_platform_operations
Anup Patel [Mon, 11 Jan 2021 11:45:00 +0000 (17:15 +0530)]
include: sbi: Don't pack struct sbi_platform and sbi_platform_operations

We don't need to pack struct sbi_platform and sbi_platform_operations
because GCC ensures member offsets match member data type irrespective
to the target system (RV32 or RV64). This also allows GCC to generate
more optimized instruction sequence when accessing members of struct
sbi_platform and struct sbi_platform_operations.

Reported-by: Paul Campbell <taniwha@gmail.com>
Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: No need to pack struct sbi_scratch
Anup Patel [Mon, 11 Jan 2021 11:15:14 +0000 (16:45 +0530)]
include: sbi: No need to pack struct sbi_scratch

All members in struct sbi_scrach are of machine word size so no need
to pack this structure. This also allows GCC to generate more optimized
instruction sequence when accessing members of struct sbi_scratch.

Reported-by: Paul Campbell <taniwha@gmail.com>
Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: No need to pack struct sbi_trap_regs
Anup Patel [Mon, 11 Jan 2021 10:55:38 +0000 (16:25 +0530)]
include: sbi: No need to pack struct sbi_trap_regs

All members in struct sbi_trap_regs are of machine word size so
no need to pack this structure. This also allows GCC to generate
more optimized instruction sequence when accessing members of
struct sbi_trap_regs.

Reported-by: Paul Campbell <taniwha@gmail.com>
Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Allow platforms to provide root domain memory regions
Anup Patel [Fri, 8 Jan 2021 04:37:01 +0000 (10:07 +0530)]
lib: sbi: Allow platforms to provide root domain memory regions

Currently, the root domain memory regions are fixed in generic
code but some of the platforms may want to explicitly define
memory regions for the root domain.

This patch adds optional domains_root_regions() platform callback
which platforms can use to provide platform specific root domain
memory regions. Due to this changes, the root domain should also
undergo all sanity checks (just like regular domain) so we use
sbi_domain_register() to register root domain.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: utils: Fix shakti uart implementation
Vijai Kumar K [Mon, 7 Dec 2020 16:54:16 +0000 (22:24 +0530)]
lib: utils: Fix shakti uart implementation

Fix uart_putc implementation.
Due to a bug in the IP, this went unnoticed.
Use macros instead of magic numbers to make the code
more readable.

Signed-off-by: Vijai Kumar K <vijai@behindbytes.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agolib: sbi: Allow custom local TLB flush function
Anup Patel [Tue, 29 Dec 2020 08:21:06 +0000 (13:51 +0530)]
lib: sbi: Allow custom local TLB flush function

Currently, we have fixed TLB flush types supported by the
remote TLB library. This approach is not flexible and does
not allow custom local TLB flush function. For example,
after updating PMP entries on a set of HARTs at runtime,
we have to flush TLB on these HARTs as well.

To support custom local TLB flush function, we replace the
"type" field of "struct sbi_tlb_info" with a local TLB flush
function pointer. We also provide definitions of standard TLB
flush operations (such as fence_i, sfence.vma, hfence.vvma,
hfence.gvma, etc).

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Introduce sbi_trap_exit() API
Anup Patel [Tue, 29 Dec 2020 06:09:22 +0000 (11:39 +0530)]
lib: sbi: Introduce sbi_trap_exit() API

We introduce sbi_trap_exit() API which can help non-firmware
(i.e. generic or platform) code to force exit trap/interrupt
handling and resume execution at context pointed by parameter
"const struct sbi_trap_regs *regs".

This new sbi_trap_exit() API will help Keystone Enclave project
to resume execution of enclave from custom SBI call handler.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Replace args with trap registers in ecall handler
Anup Patel [Mon, 28 Dec 2020 12:55:18 +0000 (18:25 +0530)]
lib: sbi: Replace args with trap registers in ecall handler

We had added args pointer in ecall handler to ensure that ecall
handler only implements functionality and does not deal with
SBI calling convention. This also helped us to keep SBI calling
convention related code in one place at sbi_ecall_handler().

The Keystone Enclavce project needs access to the trap regsiters
in their ecall handler so that they can context switch enclaves
in custom SBI calls. To help the Keystone Enclave project, we
replace the args pointer in ecall handler parameter with a const
pointer to trap registers.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Fix sign-extension in sbi_misaligned_load_handler()
Anup Patel [Sat, 19 Dec 2020 12:09:57 +0000 (17:39 +0530)]
lib: sbi: Fix sign-extension in sbi_misaligned_load_handler()

The misaligned load emulation does not sign-extend values correctly
due to missing sign typecast in value passed to the SET_RD() macro.

A very easy way to reproduce this issue is to load 16-bit value
0xff1e from a byte aligned address using LH instruction on hardware
lacking misaligned load/store.

This patch fixes sbi_misaligned_load_handler() for above issue.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Remove domain_get() platform callback function
Anup Patel [Sun, 13 Dec 2020 10:14:22 +0000 (15:44 +0530)]
lib: sbi: Remove domain_get() platform callback function

The domain_get() platform callback function is now redundant
because fdt_domain_populate() register new domain explicitly
using the sbi_domain_register() function.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: utils: Remove fdt_domain_get() function
Anup Patel [Sun, 13 Dec 2020 10:07:34 +0000 (15:37 +0530)]
lib: utils: Remove fdt_domain_get() function

The fdt_domain_get() function is now redundant because the
fdt_domains_populate() function can explicitly register new
domains using the sbi_domain_register() function.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Add function to register new domain
Anup Patel [Sun, 13 Dec 2020 09:39:42 +0000 (15:09 +0530)]
lib: sbi: Add function to register new domain

We add sbi_domain_register() function to help platform code register
a new domain explicitly instead of depending on domain_get() platform
callback. This function will eventually help us completely remove the
domain_get() platform callback.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: utils: Allow FDT domain iteration functions to fail
Anup Patel [Sun, 13 Dec 2020 05:53:05 +0000 (11:23 +0530)]
lib: utils: Allow FDT domain iteration functions to fail

We extend fdt_iterate_each_domain() and fdt_iterate_each_memregion()
functions to allow underlying iteration function to fail. This will
help us catch more domain misconfiguration issues at boot time.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: utils/libfdt: Upgrade to v1.6.0 release
Dimitri John Ledkov [Thu, 10 Dec 2020 11:03:57 +0000 (11:03 +0000)]
lib: utils/libfdt: Upgrade to v1.6.0 release

Sync with libfdt v1.6.0 release source codes.

Signed-off-by: Dimitri John Ledkov <xnox@ubuntu.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agoplatform: generic: Populate domains from FDT
Anup Patel [Thu, 19 Nov 2020 06:55:31 +0000 (12:25 +0530)]
platform: generic: Populate domains from FDT

The generic platform should populate domains from FDT using the
domain FDT helper routines.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: utils: Add helper routines to populate domains from FDT
Anup Patel [Wed, 18 Nov 2020 11:10:21 +0000 (16:40 +0530)]
lib: utils: Add helper routines to populate domains from FDT

We add various helper routines to populate domains, iterate domains,
iterate domain memregions, and parse HART to domain assignment from
the FDT.

These helper routines can be used by platform support code and FDT
fixup code.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agodocs: Add domain device tree binding documentation
Anup Patel [Sun, 15 Nov 2020 10:07:21 +0000 (15:37 +0530)]
docs: Add domain device tree binding documentation

This patch adds domain device tree binding documentation in the
OpenSBI domain support documentation.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Add domains_init() platform operation
Anup Patel [Fri, 20 Nov 2020 11:12:15 +0000 (16:42 +0530)]
include: sbi: Add domains_init() platform operation

We introduce domains_init() platform operation which can be used by
platform support to initialize/populate domains in the coldboot path.

The domains_init() is called late in the coldboot sequence from the
sbi_domain_finalize() so sbi_printf() can be used by platform support
to print errors/warnings at time of populating domains.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Add error prints in sbi_domain_finalize()
Anup Patel [Sun, 15 Nov 2020 09:41:21 +0000 (15:11 +0530)]
lib: sbi: Add error prints in sbi_domain_finalize()

We add error prints in sbi_domain_finalize() and sanitize_domain()
to help debug domain configuration issues.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Override domain boot HART when coldboot HART assigned to it
Anup Patel [Sun, 15 Nov 2020 08:00:29 +0000 (13:30 +0530)]
lib: sbi: Override domain boot HART when coldboot HART assigned to it

If coldboot HART is assigned to a domain then we should use coldboot
HART as the boot HART of the domain. This allows coldboot HART to
continue boot sequence even when it is assigned to some non-root domain.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Use lower bits for domain memory region permissions
Anup Patel [Sun, 15 Nov 2020 06:16:20 +0000 (11:46 +0530)]
include: sbi: Use lower bits for domain memory region permissions

We should use lower bits for domain memory region access permissions
and higher bits for other domain memory region attributes. This helps
us use same bit assignment for parsing domain memory region access
permissions from device tree.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Auto start domain only if boot HART within limits
Anup Patel [Wed, 11 Nov 2020 05:13:11 +0000 (10:43 +0530)]
lib: sbi: Auto start domain only if boot HART within limits

The sbi_domain_finalize() should auto start a domain only if the
boot HART is withing limits (i.e. less than SBI_HARTMASK_MAX_BITS).

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Fix domain_count check in sbi_domain_finalize()
Anup Patel [Fri, 20 Nov 2020 04:47:38 +0000 (10:17 +0530)]
lib: sbi: Fix domain_count check in sbi_domain_finalize()

The domain_count check in sbi_domain_finalize() for newly discovered
domain is incorrect and should be reversed.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Fix typo in sbi_domain_finalize()
Anup Patel [Wed, 11 Nov 2020 04:01:39 +0000 (09:31 +0530)]
lib: sbi: Fix typo in sbi_domain_finalize()

There has been typo in automatic domain boot hart startup
for non-root domains so this patch fixes it.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Fix sbi_hart_switch_mode() for u-mode
Anup Patel [Sat, 21 Nov 2020 05:58:39 +0000 (11:28 +0530)]
lib: sbi: Fix sbi_hart_switch_mode() for u-mode

We should check and access N-extension CSRs in sbi_hart_switch_mode()
when next_mode is u-mode because N-extension is optional.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agofirmware: fw_base: Optimize trap handler for RV32 systems
Anup Patel [Sun, 22 Nov 2020 06:17:22 +0000 (11:47 +0530)]
firmware: fw_base: Optimize trap handler for RV32 systems

On RV32 systems, we have two CSRs for M-mode status (MSTATUS and
MSTATUSH) when H-extension is implemented. This means we have to
save/restore MSTATUSH for RV32 systems only when H-extension is
implemented. The current _trap_handler() has extra instructions
(roughly 10) for conditional save/restore of MSTATUSH CSR.

These extra instructions in RV32 _trap_handler() can be avoided
if we create separate low-level trap handler for RV32 systems
having H-extension. This patch optimizes low-level trap handler
for RV32 systems accordingly.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
3 years agolib: sbi: Implement System Reset (SRST) SBI extension
Anup Patel [Tue, 24 Nov 2020 08:50:19 +0000 (14:20 +0530)]
lib: sbi: Implement System Reset (SRST) SBI extension

The SBI SRST extension has been accepted and merged in the latest
SBI v0.3-draft specification.
(Refer, https://github.com/riscv/riscv-sbi-doc)

It allows to S-mode software to request system shutdown, cold reboot,
and warm reboot.

This patch implements SBI SRST extension as a replacement of the
legacy sbi_shutdown() call of SBI v0.1 specification.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Improve system reset platform operations
Anup Patel [Tue, 24 Nov 2020 06:29:05 +0000 (11:59 +0530)]
lib: sbi: Improve system reset platform operations

To implement the SBI SRST extension, we need two platform operations
for system reset:
1) system_reset_check() - This operation will check whether given
   reset type and reason are supported by the platform
2) system_reset() - This operation will do the actual platform
   system reset and it will not return if reset type and reason
   are supported by the platform

This patch updates system reset related code everywhere as-per above.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoplatform: Remove dummy system reset functions
Anup Patel [Tue, 24 Nov 2020 05:48:27 +0000 (11:18 +0530)]
platform: Remove dummy system reset functions

Few platforms have dummy system reset functions so let's remove
these dummy system reset functions to allow generic code deal
with it in the right way.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Remove opensbi specific reset type defines
Anup Patel [Tue, 24 Nov 2020 04:43:16 +0000 (10:13 +0530)]
include: sbi: Remove opensbi specific reset type defines

We can now use the standard SBI SRST extension reset types instead
of the opensbi specific (SBI_PLATFORM_RESET_xyz) reset types hence
remove related opensbi specific defines. The "platform_" prefix of
the reset type parameter of sbi_system_reset() function should also
be removed.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Add SBI SRST extension related defines
Anup Patel [Tue, 24 Nov 2020 04:20:00 +0000 (09:50 +0530)]
include: sbi: Add SBI SRST extension related defines

We extend the SBI ecall interface header for the SBI SRST extension
recently accepted in SBI specification v0.3-draft.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agodocs: fix a typo error
Yuan Li [Wed, 25 Nov 2020 15:13:19 +0000 (23:13 +0800)]
docs: fix a typo error

fix a typo error in docs/platform/sifive_fu540.md

Signed-off-by: Yuan Li <dskwelmcy@163.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Improve boot prints in cold boot sequence
Anup Patel [Wed, 4 Nov 2020 09:46:20 +0000 (15:16 +0530)]
lib: sbi: Improve boot prints in cold boot sequence

Currently, we have all boot prints at the end of cold boot sequence
which means if there is any failure in cold boot sequence before
boot prints then we don't get any print.

This patch improves boot prints in cold boot sequence as follows:
1. We divide the boot prints into multiple parts and print it
   from different locations after sbi_console_init()
2. We throw an error print if there is any failure in cold boot
   sequence after sbi_console_init()

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoMakefile: Add option to use toolchain default ABI and ISA string
Anup Patel [Tue, 20 Oct 2020 04:59:43 +0000 (10:29 +0530)]
Makefile: Add option to use toolchain default ABI and ISA string

When PLATFORM_RISCV_ABI and PLATFORM_RISCV_ISA are not specified,
we force "-mabi=lp64 -march=rv64gc" for RV64 and force "-mabi=ilp32
-march=rv32gc" for RV32. This can prevent users from using the
toolchain default "-mabi" and "-march" options.

To allow using toolchain defaults, we add compile-time option
PLATFORM_RISCV_TOOLCHAIN_DEFAULT which when enabled forces the
top-level makefile to use toolchain default ABI and ISA string.

To enable the option, pass "PLATFORM_RISCV_TOOLCHAIN_DEFAULT=1"
to top-level make.

Reported-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
3 years agolib: sbi: Improve boot time print with additional PMP information
Atish Patra [Mon, 26 Oct 2020 21:03:34 +0000 (14:03 -0700)]
lib: sbi: Improve boot time print with additional PMP information

We know about pmp granularity and number of bits supported by PMP.
Show those information in the boot time info print

Signed-off-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agolib: sbi: Detect PMP granularity and number of address bits
Atish Patra [Mon, 26 Oct 2020 21:03:33 +0000 (14:03 -0700)]
lib: sbi: Detect PMP granularity and number of address bits

As per RISC-V privilege specification, a platform may choose to implement
a coarser granularity scheme for PMP addresses. In that case, we shouldn't
allow any pmp region size smaller than the platform supports. A platform
may not also implement all the bits for a PMP address specified in the priv
specification.

The pmp range granularity and address bits should be detected dynamically
before detecing PMP regions. Any pmp modification request beyond these detected
value must not succeed.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agolib: utils: Implement "ranges" property parsing
Atish Patra [Fri, 23 Oct 2020 22:47:39 +0000 (15:47 -0700)]
lib: utils: Implement "ranges" property parsing

The "reg" property in a device node may not be the correct address always.
If a parent node defines a "ranges" property, the child address need to be
translated with respect to parents address. If the ranges property is not
present, it will just use 1:1 translation.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agodocs: Add initial documentation for domain support
Anup Patel [Thu, 24 Sep 2020 09:45:31 +0000 (15:15 +0530)]
docs: Add initial documentation for domain support

We add initial documentation for OpenSBI domain support to help
RISC-V platform vendors achieve system-level partitioning.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Display domain details in boot prints
Anup Patel [Wed, 23 Sep 2020 16:19:01 +0000 (21:49 +0530)]
lib: sbi: Display domain details in boot prints

We extend boot prints to display details of each domain. In the
process, we remove sbi_hart_pmp_dump() because it shows redundant
information which domain details already show.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Configure PMP based on domain memory regions
Anup Patel [Tue, 22 Sep 2020 10:06:15 +0000 (15:36 +0530)]
lib: sbi: Configure PMP based on domain memory regions

The PMP configuration on each HART should be only based on the memory
regions of the assigned domain because each domain includes a memory
region to protect the underlying firmware.

This patch updates the sbi_hart_pmp_configure() function accordingly.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Remove redundant sbi_hart_pmp_xyz() functions
Anup Patel [Tue, 22 Sep 2020 05:42:51 +0000 (11:12 +0530)]
lib: sbi: Remove redundant sbi_hart_pmp_xyz() functions

The sbi_hart_pmp_get() and sbi_hart_pmp_check_addr() functions
are not used anymore hence this patch removes these functions.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: utils: Update fdt_cpu_fixup() to use current domain
Anup Patel [Tue, 22 Sep 2020 09:10:08 +0000 (14:40 +0530)]
lib: utils: Update fdt_cpu_fixup() to use current domain

The fdt_cpu_fixup() should disable a HART in DT if the HART
is not assigned to the current HART domain. This patch updates
fdt_cpu_fixup() accordingly.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: utils: Update fdt_reserved_memory_fixup() to use current domain
Anup Patel [Mon, 21 Sep 2020 10:28:14 +0000 (15:58 +0530)]
lib: utils: Update fdt_reserved_memory_fixup() to use current domain

Now that each HART is mapped to a domain having a set of memory
regions, we update fdt_reserved_memory_fixup() to use domain memory
regions for adding reserved memory nodes in device tree.

We also change reserved memory node name prefix from "mmode_pmp"
to "mmode_resv" because domain memory regions can impact other
hardware configurations (such as IOPMP, etc) along with PMP.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Extend sbi_system_reset() for domains
Anup Patel [Sat, 19 Sep 2020 11:53:46 +0000 (17:23 +0530)]
lib: sbi: Extend sbi_system_reset() for domains

The sbi_system_reset() should issue platform system reset only if
domain of current HART is allowed to do system reset.

This patch extends sbi_system_reset() as-per above.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Optimize sbi_hsm_hart_started_mask() implementation
Anup Patel [Fri, 25 Sep 2020 04:38:24 +0000 (10:08 +0530)]
lib: sbi: Optimize sbi_hsm_hart_started_mask() implementation

Instead of calling sbi_hsm_hart_get_state() in a loop, we can simply
call a new inline __sbi_hsm_hart_get_state() which only takes "hartid"
and enforce domain checks using sbi_domain_assigned_hartmask().

This patch optimizes sbi_hsm_hart_started_mask() as-per above.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Extend sbi_hsm_hart_start() for domains
Anup Patel [Sat, 19 Sep 2020 08:56:52 +0000 (14:26 +0530)]
lib: sbi: Extend sbi_hsm_hart_start() for domains

The sbi_hsm_hart_start() should consider the domain under which we
are trying to start the HART. This will help ensure that HART A can
start HART B only if both HARTs A and B belong to the same domain.

We also have a special case when we bring-up boot HART of non-root
domains in sbi_domain_finalize() where we should skip domain checks
in sbi_hsm_hart_start(). To achieve this, sbi_hsm_hart_start() should
do domain checks only when domain parameter is non-NULL.

This patch extends sbi_hsm_hart_start() as-per above.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Extend sbi_hsm_hart_started_mask() for domains
Anup Patel [Thu, 24 Sep 2020 12:19:27 +0000 (17:49 +0530)]
lib: sbi: Extend sbi_hsm_hart_started_mask() for domains

The sbi_hsm_hart_started_mask() API should take one more parameter
to allow caller specify domain under which started_mask is being
generated. Further, the sbi_hsm_hart_started_mask() depends on
sbi_hsm_hart_get_state() which also should return HART state under
specified domain.

This patch updates both sbi_hsm_hart_started_mask() and
sbi_hsm_hart_get_state() as-per above.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
3 years agolib: sbi: Add initial domain support
Anup Patel [Fri, 18 Sep 2020 11:37:49 +0000 (17:07 +0530)]
lib: sbi: Add initial domain support

An OpenSBI domain is a logical entity representing a set of HARTs
and a set of memory regions for these HARTs.

The OpenSBI domains support will allow OpenSBI platforms and previous
booting stage (i.e. U-Boot SPL, Coreboot, etc) to partition a system
into multiple domains where each domain will run it's own software.

For inter-domain isolation, OpenSBI will eventually use various HW
features such as PMP, ePMP, IOPMP, SiFive shield, etc but initial
implementation only use HW PMP support.

This patch provides initial implementation of OpenSBI domains where
we have a root/default domain and OpenSBI platforms can provide
non-root/custom domains using domain_get() callback.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Make hartmask pointer const in sbi_hartmask_test_hart()
Anup Patel [Thu, 24 Sep 2020 10:34:02 +0000 (16:04 +0530)]
include: sbi: Make hartmask pointer const in sbi_hartmask_test_hart()

The sbi_hartmask_test_hart() does not modify hartmask so the
hartmask pointer parameter should be declared const.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
3 years agolib: sbi: Remove redundant hartid parameter from sbi_hart_init()
Anup Patel [Sun, 20 Sep 2020 08:19:02 +0000 (13:49 +0530)]
lib: sbi: Remove redundant hartid parameter from sbi_hart_init()

The hartid parameter of sbi_hart_init() is not used anywhere in
sbi_hart_init() implementation so let's remove it.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
3 years agolib: sbi: Configure PMP late in coldboot and warmboot path
Anup Patel [Sun, 20 Sep 2020 04:39:32 +0000 (10:09 +0530)]
lib: sbi: Configure PMP late in coldboot and warmboot path

We factor-out PMP configuration from sbi_hart_init() as a separate
sbi_hart_pmp_configure() function and do the PMP configuration very
late in coldboot and warmboot path just before platform_final_init().

Eventually, this enable us to configure PMP totally based on OpenSBI
domain where OpenSBI domains can be parsed from device-tree in any of
the platform operations except platform_final_init().

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
3 years agoinclude: sbi: Remove pmp_region callbacks from sbi_platform_operations
Anup Patel [Thu, 17 Sep 2020 10:34:55 +0000 (16:04 +0530)]
include: sbi: Remove pmp_region callbacks from sbi_platform_operations

The pmp_region_xyz() callbacks are not required in sbi_platform_operations
because we will be using OpenSBI domain memory regions to program the
PMP CSRs.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
3 years agoinclude: sbi: Remove redundant includes from sbi_platform.h
Anup Patel [Thu, 17 Sep 2020 10:21:00 +0000 (15:51 +0530)]
include: sbi: Remove redundant includes from sbi_platform.h

The sbi_ecall.h and sbi_trap.h are not required to be included in
sbi_platform.h hence we remove it. This way sources requiring
these headers will have to explicitly include it thereby reducing
implicit includes.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
3 years agoplatform: kendryte/k210: Add some padding for FDT fixups
Anup Patel [Thu, 15 Oct 2020 04:57:57 +0000 (10:27 +0530)]
platform: kendryte/k210: Add some padding for FDT fixups

The Kendryte K210 platform has built-in DTB and does not provides
FW_PAYLOAD_FDT_ADDR. This means the FDT fixups will be done on
the built-in DTB in absence of FW_PAYLOAD_FDT_ADDR. This patch
adds some padding in built-in DTB for FDT fixups.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoMakefile: Allow padding zeros when converting DTB to C source
Heinrich Schuchardt [Wed, 14 Oct 2020 16:35:31 +0000 (22:05 +0530)]
Makefile: Allow padding zeros when converting DTB to C source

We extend d2c.sh to allow padding zeros in output C source when
converting DTB to C source. Using this feature, platforms can
create extra room for in-place FDT fixups on built-in DTBs.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agofirmware: Remove FW_PAYLOAD_FDT_PATH compile-time option
Anup Patel [Wed, 14 Oct 2020 11:13:34 +0000 (16:43 +0530)]
firmware: Remove FW_PAYLOAD_FDT_PATH compile-time option

The FW_PAYLOAD_FDT_PATH compile-time option is replaced by
FW_FDT_PATH compile-time option which is more flexible and
common across all OpenSBI firmwares.

This patch removes FW_PAYLOAD_FDT_PATH and updates related
documentation to use FW_FDT_PATH.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agofirmware: Add common FW_FDT_PATH compile-time option
Anup Patel [Wed, 14 Oct 2020 10:33:35 +0000 (16:03 +0530)]
firmware: Add common FW_FDT_PATH compile-time option

Currently, only FW_PAYLOAD has mechanism to embed external
FDT using FW_PAYLOAD_FDT_PATH compile-time option.

This patch adds a common FW_FDT_PATH compile-time option to
embed external FDT for all OpenSBI firmwares (i.e FW_JUMP,
FW_PAYLOAD, and FW_DYNAMIC).

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoMakefile: Build ELF, BIN and LD script in platform build directory
Anup Patel [Wed, 14 Oct 2020 08:14:18 +0000 (13:44 +0530)]
Makefile: Build ELF, BIN and LD script in platform build directory

Currently, the make rules for ELF, BIN and LD script are little
generic to allowing ELF, BIN and LD script to be anywhere in the
build directory. For OpenSBI firmwares, the ELF, BIN, and LD script
are always platform specific so we update make rules accordingly.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agomakefile: fix clean directive
Damien Le Moal [Mon, 5 Oct 2020 14:55:53 +0000 (23:55 +0900)]
makefile: fix clean directive

Add cleaning of compiled device tree files (.dtb files).

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agolib: sbi_scratch: typo scatch
Heinrich Schuchardt [Thu, 1 Oct 2020 10:27:47 +0000 (12:27 +0200)]
lib: sbi_scratch: typo scatch

%s/scatch/scratch

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agolib: sbi: Fix PMP CSR detection
Pragnesh Patel [Wed, 23 Sep 2020 07:14:17 +0000 (12:44 +0530)]
lib: sbi: Fix PMP CSR detection

HiFive unleashed currently implements fewer than 56 bits of physical
address so existing PMP CSR detection is broken.

PMP address register encodes bits 55-2 of a 56-bit physical address,
Not all physical address bits may be implemented, So just check
minimum 1 bit.

Fixes: 74d1db706293 ("lib: sbi: Improve PMP CSR detection and
progamming")

Signed-off-by: Pragnesh Patel <pragnesh.patel@sifive.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
3 years agoplatform: generic: Don't mark non-MMU HARTs as invalid
Anup Patel [Wed, 9 Sep 2020 08:24:03 +0000 (13:54 +0530)]
platform: generic: Don't mark non-MMU HARTs as invalid

Currently, the generic platform fw_platform_init() marks non-MMU
HARTs (e.g. E-core on SiFive Unleashed) as invalid. This means
such non-MMU HARTs won't be allowed to go ahead by sbi_init().

The sbi_init() now has a check for next stage privilege mode when
selecting coldboot HART. This check will force non-MMU HARTS (i.e.
HARTs without S-mode) to proceed in warmboot path and wait in the
HSM STOPPED state. This means we don't need to mark non-MMU HARTs
as invalid in generic platform fw_platform_init().

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Ensure coldboot HART supports next privilege mode
Anup Patel [Wed, 9 Sep 2020 03:49:09 +0000 (09:19 +0530)]
lib: sbi: Ensure coldboot HART supports next privilege mode

The coldboot HART jumps to the next booting stage at the end of
init_coldboot() so it is absolutely necessary for coldboot HART
to support the privilege mode expected by the next booting stage.

We extend the coldboot HART selection in sbi_init() to ensure that
the selected coldboot HART always supports privilege mode specified
in scratch space (i.e. scratch->next_mode). This will further help
us allow E-core (without MMU) on SiFive Unleashed and PolarFire
ICICLE boards to proceed further and wait in HSM STOPPED state.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: utils: Improve fdt_cpu_fixup() implementation
Anup Patel [Mon, 7 Sep 2020 13:46:27 +0000 (19:16 +0530)]
lib: utils: Improve fdt_cpu_fixup() implementation

The fdt_cpu_fixup() should work fine even if HARTs without MMU
are not marked invalid by platform support code.

In future, we plan to treat HARTs without MMU as valid in the
generic platform support so that we can hold these HARTs in
HSM STOPPED state.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Allow specifying mode in sbi_hart_pmp_check_addr() API
Anup Patel [Mon, 7 Sep 2020 10:20:33 +0000 (15:50 +0530)]
lib: sbi: Allow specifying mode in sbi_hart_pmp_check_addr() API

We extend sbi_hart_pmp_check_addr() API so that users can specify
privilege mode of the address for checking PMP access permissions.

To achieve this, we end-up converting "unsigned long *size" parameter
to "unsigned long *log2len" for pmp_get() implementation so that we
can deal with regions of "1UL << __riscv_xlen" size in a special case
in sbi_hart_pmp_check_addr() implementation.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Allow specifying start mode to sbi_hsm_hart_start() API
Anup Patel [Mon, 7 Sep 2020 05:41:29 +0000 (11:11 +0530)]
lib: sbi: Allow specifying start mode to sbi_hsm_hart_start() API

The sbi_scratch already has provision to specify the next stage mode
so we can leverage this to specify start mode to sbi_hsm_hart_start().

In future, this will be useful in providing SBI calls to U-mode on
embedded cores where we M-mode and U-mode but no S-mode.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi_trap: Allow M-mode to M-mode ECALLs
Anup Patel [Fri, 4 Sep 2020 11:09:49 +0000 (16:39 +0530)]
lib: sbi_trap: Allow M-mode to M-mode ECALLs

We should allow M-mode to M-mode ECALLs because:
1. No other mode can handle M-mode ECALLs
2. In future, we can allow M-mode baremetal application (not
   linked to OpenSBI) running independently and cooperatively
   without corrupting OpenSBI M-mode state (CSRs and memory).

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: Rename ECALL defines to match latest RISC-V spec
Anup Patel [Tue, 1 Sep 2020 12:24:29 +0000 (17:54 +0530)]
include: Rename ECALL defines to match latest RISC-V spec

We rename CAUSE_HYPERVISOR_ECALL to CAUSE_SUPERVISOR_ECALL and
CAUSE_SUPERVISOR_ECALL to CAUSE_VIRTUAL_SUPERVISOR_ECALL so that
it matches latest RISC-V privilege spec.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Don't handle VS-mode ecall in sbi_trap_handler()
Anup Patel [Tue, 1 Sep 2020 12:20:14 +0000 (17:50 +0530)]
lib: sbi: Don't handle VS-mode ecall in sbi_trap_handler()

The VS-mode ecall is supposed to be handled by HS-mode so
sbi_trap_handler() should fallback to default case for
VS-mode ecall trap.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Improve HPM CSR read/write emulation
Anup Patel [Fri, 21 Aug 2020 12:54:14 +0000 (18:24 +0530)]
lib: sbi: Improve HPM CSR read/write emulation

We improve HPM CSR read/write emulation as follows:
1. Fail for unimplemented counters so that trap is redirected
   to S-mode which can further help debugging S-mode software.
2. Check permissions in both MCOUNTEREN and SCOUNTEREN for
   HS-mode and U-mode.
3. Don't check permissions for TIME CSR because we emulate
   TIME CSR for both Host (HS/U-mode) and Guest (VS/VU-mode).
   Also, faster TIME CSR read is very helpful for good
   performance of S-mode software.
4. Don't emulate S-mode CSR read/write to M-mode HPM CSRs
   because these should not be accessible to S-mode software.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agoinclude: sbi: Few cosmetic changes in riscv_encoding.h
Anup Patel [Fri, 21 Aug 2020 12:49:32 +0000 (18:19 +0530)]
include: sbi: Few cosmetic changes in riscv_encoding.h

This patch does following cosmentic changes to riscv_encoding.h:
1. Re-organize CSR defines to match CSR listing in latest RISC-V
   privilege spec
2. Rename CSR_HCOUNTERNEN to CSR_HCOUNTEREN

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Detect and print MHPM counters at boot-time
Anup Patel [Wed, 19 Aug 2020 12:01:37 +0000 (17:31 +0530)]
lib: sbi: Detect and print MHPM counters at boot-time

A RISC-V platform can leave unimplemented MHPM counters hard-wired
to zero. We extend hart_detect_features() to detect MHPM counters
which are accessible and not hard-wired to zero. We also print
number of available MHPM counters as part of boot prints.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
3 years agolib: sbi: Improve PMP CSR detection and progamming
Anup Patel [Tue, 18 Aug 2020 07:58:08 +0000 (13:28 +0530)]
lib: sbi: Improve PMP CSR detection and progamming

As-per latest RISC-V privilege spec up to 64 PMP entries are supported.
Implementations may implement zero, 16, or 64 PMP CSRs. All PMP CSR
fields are WARL and may be hardwired to zero.

This patch improves PMP CSR detection and progamming considering
above facts.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>