Commit graph

930 commits

Author SHA1 Message Date
Anup Patel
4e2cd47820 lib: Flush everything when remote TLB flush range is too large
On latest Linux kernel (i.e. 5.2-rc1), we get large TLB flush
request for user space addresses (typically, start=x and end=-1).
This is caused by Linux kernel commit a21344dfc6 ("riscv: fix
sbi_remote_sfence_vma{,_asid}").

It's not practical to execute large number of sfence instructions
for a large TLB flush range because it takes too much time and
eventually causes CPU stall in Linux kernel.

This patch addresses above issue by upgrading TLB flush range to
TLB flush all whenever TLB flush range is greater than 1GB.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
2019-05-21 10:06:08 +05:30
Olof Johansson
10baa64c02 all: run clang-format and update checked-in files
Noisy commit, no functional changes.

Generated with an current upstream clang-format and:

clang-format -i $(find . -name \*.[ch])

Signed-off-by: Olof Johansson <olof@lixom.net>
2019-04-24 09:49:46 +05:30
Nick Kossifidis
804b997ed4 lib: Redirect unhandled traps from non-M modes to S mode
In case we didn't handle a trap with one of the available
handlers, check if the trap comes from S or U mode and
redirect it to S mode's trap handler.

Signed-off-by: Nick Kossifidis <mick@ics.forth.gr>
2019-04-22 09:56:20 +05:30
Atish Patra
999823c597 lib: Optimize TLB flush IPIs
Simulatenous requests for tlbflush IPIs can have overlapping address
ranges.

Ignore if address range is same or within already existing fifo entries.
Update the tlb flush info in fifo directly if the one of the existing
entry lies within the new flush request.
Delete all entries if flush all request is recieved for the vma.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-04-10 15:46:35 +05:30
Atish Patra
110eef44f0 lib: Provide a lock enabled iteration of fifo.
Implement a lock enabled iteration for fifo so that
caller can determine if next entry can be skipped or
any existing entries in fifo can be updated before enqueue.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-04-10 15:46:35 +05:30
Anup Patel
3fbe233a15 lib: Pack struct sbi_fifo
This patch reduces memory consumed by struct sbi_fifo by droping
redundant "head" member and using u16 in-place of "unsigned long".

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-04-03 13:48:38 -07:00
Anup Patel
9dc95021db lib: More improvements to sbi_fifo
This patch does following improvements to sbi_fifo:
1. Use valid SBI_Exxxx error codes instead of -1
2. The sbi_fifo_is_full() and sbi_fifo_is_empty() did
   not acquire qlock before accessing head and tail
   hence fixed it
3. Added avail member for ease in debugging and simplifying
   head/tail updates.

Due to above changes size of sbi_fifo changes from 48 bytes
to 56 bytes.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-04-03 13:48:38 -07:00
Atish Patra
f700216cb5 lib: Use a fifo to keep track of sfence related IPIs.
Currently, there is no provision for tracking multiple IPIs sent
to a single hart at the same time by different harts.

Use a fifo manage the outstanding requests. While dequeueing, read all
the entries once, because we have only 1 bit to track the type of IPI.
Once the queue is full, busy wait until the there is space available in
queue. This is not the most elegant approach. It should be changed in
favor of a wakeup event once available in opensbi.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-04-03 09:57:42 +05:30
Atish Patra
8334a88c63 lib: Add a fifo implementation.
Implement a fifo to accomodate outstanding IPIs for a specific hart
at the same time.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-04-03 09:57:42 +05:30
Anup Patel
78c87cd13a include: Make mstatus parameter optional for get_insn()
The mstatus parameter of get_insn() is used to return MSTATUS CSR
value which get_insn() saw. Most of the get_insn() callers don't
use the value returned in mstatus so this patch makes mstatus
parameter optional for get_insn().

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-04-01 10:10:38 +05:30
Anup Patel
13877c3a67 include: Rename sbi_unpriv.h to riscv_unpriv.h
The sbi_unpriv.h has quite a few load_xyz() and store_xyz() helper
routines based on RISC-V inline assembly for unpriviledged accesses
from M-mode. These helper routines are similar to helper routines
present in riscv_locks.h, riscv_io.h, and riscv_atomic.h so let's
rename sbi_unpriv.h to riscv_unpriv.h.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-04-01 10:10:38 +05:30
Anup Patel
cfff0126ab lib: Remove unused mepc arg from load_xyz() and store_xyz() functions
This patch removes unused mepc arg from load_xyz() and store_xyz()
unpriviledge access functions.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-04-01 10:10:38 +05:30
Bin Meng
f9cfe301c9 lib: Disable the boot prints if SBI_SCRATCH_NO_BOOT_PRINTS is set
Use the newly introduced "options" in "struct sbi_scratch" to
conditionally disable the boot prints.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
2019-03-29 15:49:11 +05:30
Bin Meng
215c200ccb lib: Group all prints during boot into sbi_boot_prints()
Refactor the codes a little bit to put all prints during boot into
sbi_boot_prints().

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
2019-03-29 15:49:11 +05:30
Atish Patra
6c39ea99ee lib: Return ENOTSUPP incase of invalid SBI function ID
OpenSBI should show error trace only if any valid SBI function
does not perform as expected.

However, OpenSBI should show notify the caller with a negative
error if given SBI function ID is not valid.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-03-14 20:44:22 +05:30
Atish Patra
1e24e21d56 lib: Fix full tlb flush behavior
Currently, global page mappings are not flushed if start and size
arguments are zero.

Flush entire TLB if both size and start argument is passed as zero.

Fixes : 90cb491 (lib: Implement sfence.vma correctly)

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-03-14 20:41:13 +05:30
Atish Patra
90cb4917b5 lib: Implement sfence.vma correctly.
Currently, OpenSBI doesn't distinguish between sfence.vma
and sfence.vm asid calls. Moreover, it ignores the page
ranges and just flush entire TLB everytime.

Fix the sfence implementation by keeping all the tlb flush
info in scratch area.

The relevant Linux kernel code was added by
https://patchwork.kernel.org/project/linux-riscv/list/?series=89695

However, this patch is backward compatible with older version kernel
that doesn't have the above patches as well.

Fixes #87
Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-03-12 22:07:25 +05:30
Alistair Francis
508a27204c lib: Create a sbi_ipi_data structure
Create a sbi_ipi_data structure that holds unpacked IPI information. At
the same time remove ipi_type from the sbi_scratch struct and use a
fixed offset to access it.

This structure fits in behind the sbi_scratch structure.

This fixes https://github.com/riscv/opensbi/issues/81

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
2019-03-10 21:08:21 +05:30
Xiang Wang
9eb8f0f90d platform: Make the platform read-only
platform should be a read-only variable, if it is placed in the data
segment, it may be exploited.

Signed-off-by: Xiang Wang <wxjstz@126.com>
2019-03-06 11:10:35 -08:00
Atish Patra
86cc9b8633 lib:platform: Fix sbi_getc return type.
As per the current SBI specification, sbi_getc should return
an int instead of char.

In case of FIFO is empty, return -1 as per the specification.

Reported-by: Sergi Granell <xerpi.g.12@gmail.com>
Suggested-by:Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-03-05 14:05:12 +05:30
Anup Patel
ce6189f7a5 include: Add PRILX define to help print unsigned long
The unsigned long is always machine word size. This means it is
4 bytes on 32bit system and 8 bytes on 64bit system.

This patch adds PRILX define for sbi_printf() which will help us
print unsigned long without worrying whether it is 32bit or 64bit
system.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-02-20 12:02:40 +05:30
Anup Patel
4e774f5470 lib: Fix mask shift in sbi_ipi_send_many()
The mask shift in for-loop of sbi_ipi_send_many() is
broken with commit 918c1354b7
("lib: Improve delivery of SBI_IPI_EVENT_HALT")

This patch fix it.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-02-18 18:39:02 +05:30
Nick Kossifidis
148423d141 lib: Fix small typo on sbi_ipi.c 2019-02-18 09:21:58 +05:30
Nick Kossifidis
918c1354b7 lib: Improve delivery of SBI_IPI_EVENT_HALT
When sbi_ipi_send_many gets called with the current hartid
included on pmask (or when pmask is NULL), and we send
a HALT event, since the for loop works sequentially over
all hartids on the mask, we may send a HALT event to the
current hart before the loop finishes. So we will halt
the current hart before it can deliver a HALT IPI to the
rest and some harts will remain active.

Make sure we send an IPI to the current hart after we've
finished with everybody else.

Signed-off-by: Nick Kossifidis <mick@ics.forth.gr>
2019-02-18 09:21:58 +05:30
Nick Kossifidis
b44878b773 lib: Send IPI for all harts to hang on system shutdown
In case the platform specific method for shutting down
the system fails (or is not implemented), at least make
sure that all harts hang instead of just the current hart.

Signed-off-by: Nick Kossifidis <mick@ics.forth.gr>
2019-02-18 09:21:58 +05:30
Atish Patra
70a474d2c2 lib: Use CSR_<FOO> instead of <foo> for csr_*()
Some older toolchains may not have all the csr's defined. Update all
the csr functions to use the CSR_ #define values instead of the
toolchain defined values.

Suggested-by: Olof Johansson <olof@lixom.net>
Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-02-14 09:31:18 +05:30
Alistair Francis
f4cf6da7ff sbi_emulate_csr: Shift sbi_timer_value directly
csr_val is a tartget length based variable, so on 32-bit devices it's
only 32-bits. To avoid clearing the entire register perform both steps
in a single line.

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
2019-02-08 09:18:46 +05:30
Alistair Francis
b4c72deba9 sbi_ecall: Fix logical OR to be bitwise OR
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
2019-02-08 09:18:46 +05:30
Olof Johansson
c0addfe751 riscv_asm.h: Use CSR_<FOO> instead of <foo> for csr_read()
Some toolchains might not have all the CSRs available (as seen with
GCC 7.2). So, instead use the defined CSR_ values.

Signed-off-by: Olof Johansson <olof@lixom.net>
2019-02-05 18:53:09 +05:30
Anup Patel
bd98d77298 include: Add separate header for OpenSBI version
Currently, the OpenSBI version is in top-level Makefile so
firmware linking to OpenSBI static library have no-way to
know OpenSBI version.

This patch moves OpenSBI version from top-level Makefile to
sbi/sbi_version.h header which provides OPENSBI_VERSION_MAJOR
and OPENSBI_VERSION_MINOR defines.

NOTE: the SBI spec (or SBI ecall interface) version is
different. The SBI spec version is provided by functions
sbi_ecall_version_major() and sbi_ecall_version_minor().

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-25 11:05:49 -08:00
Anup patel
20990ee0ab all: Update copyright header in all files
This patch updates copyright header in all files as follows:
1. Makes "SPDX-License-Identifier: BSD-2-Clause" as first line
2. Change copyright year to 2019 for Western Digital

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-24 14:07:47 +05:30
Anup Patel
025d0ae994 include: Rename ipi_inject() to ipi_send() for sbi_platform
For better naming, we rename ipi_inject() to ipi_send() in
struct sbi_platform. We also replace term "inject" with
"send" in all related places.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-23 08:39:26 +05:30
Anup Patel
36394d2f24 include: Drop MMIO from SBI_PLATFORM_HAS_MMIO_TIMER_VALUE
It is not necessary that platform has MMIO-based timer value
register. It can also have some custom (implementation specific)
CSR for timer value.

This patch renames SBI_PLATFORM_HAS_MMIO_TIMER_VALUE to
SBI_PLATFORM_HAS_TIMER_VALUE to imply "platform timer value"
instead of "mmio timer value".

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-23 08:39:26 +05:30
Anup Patel
9895d446ae lib: Remove target_hart and hartid parameter from TIMER callbacks
The target_hart and hartid paramter of TIMER callbacks is not
required because it always current hartid which can be obtained
using sbi_current_hartid() API.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-23 08:39:26 +05:30
Anup Patel
74fd2e5bb2 lib: Remove hartid parameter from IRQCHIP callbacks
The hartid parameter in IRQCHIP callbacks of sbi_platform
is not required because current hartid can be determined
using sbi_current_hartid() API.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-23 08:39:26 +05:30
Anup patel
18ec89e46e lib: Remove source_hart and hartid parameter from IPI callbacks
The source_hart and hartid parameter is really not required in
IPI callbacks of sbi_platform because current hartid can always
be obtained by calling sbi_current_hartid() API.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-23 08:39:26 +05:30
Anup Patel
fea9e2b5f3 lib: Remove hartid paramter from early_init() and final_init() callbacks
We simplify early_init() and final_init() callbacks of sbi_platform
by removing "hartid" parameter.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-23 08:39:26 +05:30
Anup Patel
5c2ab99971 lib: Add doxygen style documentation for sbi_platform
This patch adds doxygen style documenation for struct sbi_platform
and related functions/macros/defines.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-22 11:40:15 +05:30
Anup Patel
3fde8f3f64 lib: Update documentation of sbi_init()
We don't need to pre-enable MSIP in MIE CSR when
calling sbi_init() from firmware. This patch updates
documentation accordingly.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-22 11:40:15 +05:30
Atish Patra
023aa6bb04 lib: Do not access mi/edeleg register if S mode is not present.
As per the RISC-V ISA, mideleg and medeleg registers should not exist
if S-mode is not present for a hart.

We shouldn't access these CSRs if non S-mode harts.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-01-22 10:03:49 +05:30
Atish Patra
bc545539d2 lib: Allow sending IPI to self.
S-mode software may send IPI to self. For example,
tlbflush may be executed for the same hart.

Let the hart send IPI to itself. It's an overhead
but doesn't break anything. However, if we don't
allow it, it breaks if S-mode keep sending IPIs.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-01-22 10:03:49 +05:30
Atish Patra
b9c517f559 lib: Fix ipi type update
IPIs are updated in scratch space by source hart.
However, different harts or same hart may want to
send different IPIs before previous IPI was read
by the target hart. Currently, previous IPI type
is overwritten in that case.

Use atomic bit set/clear operations to update IPIs.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-01-22 10:03:49 +05:30
Atish Patra
312b6bf32f lib: Add atomic bit set/clear operations.
Add addtional functionlities for set/clear bits
atomically.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-01-22 10:03:49 +05:30
Atish Patra
784a4657c0 lib: Move software interrupt enablement from firmware to lib
The secondary hart waits for an IPI signal from the boot hart to
executing boot code (hot boot). As a result, software generated
interrupts have to be enabled for secondary harts before waiting for
the boot hart boot completion IPI signal.

Enabling software generated interrupts (IPI) can be done independently
of the firmware code and moved to libsbi code so that the different
firmware do not have to implement this.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
2019-01-22 10:03:49 +05:30
Anup Patel
1ee745fe57 lib: Use AMO instructions whenever __riscv_atomic is defined
We should use AMO instructions whenever __riscv_atomic is
defined (i.e. atomics are supported). We use LR/SC only when
__riscv_atomic is not defined.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-21 21:12:48 +05:30
Damien Le Moal
f4373731e3 lib: code cleanup
Use commonly accepted styles: newlines after declarations and before
return to make the code more readable.

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
2019-01-21 09:58:33 +05:30
Damien Le Moal
6939d345ab lib: Fix banner
Define the "OpenSBI" logo string as a macro renamed BANNER, since
it is one rather than a logo.

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
2019-01-21 09:58:33 +05:30
Anup Patel
3298251f0c include: Add documentation for sbi_trap_regs and friends
This patch adds doxygen style documentation for struct sbi_trap_regs
and related macros/defines/functions.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-16 11:25:25 +05:30
Anup Patel
5f097cbf07 lib: Add documentation for sbi_init() API
This patch adds doxygen style documentation for sbi_init() API.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-16 11:25:25 +05:30
Anup Patel
48dc50df8f lib: Handle misaligned load/store on FP regs
This patch extends misaligned load/store trap handling to
handle FP regs as well.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
2019-01-03 14:21:08 -08:00