[ARCH/RISC-V] Next RISC-V (#10320)

* [libcpu][risc-v] Refactor the RISC-V architecture port

Introduce an opt-in unified RISC-V common layer for RV32 and RV64 while keeping existing BSPs on the legacy implementation during migration.

- use ARCH_RISCV and ARCH_CPU_64BIT for architecture and XLEN selection
- unify context, trap, MMU, cache, timer, SBI, SMP and ISA handling
- add PLIC, CLIC, ACLINT, APLIC and IMSIC support
- map physical hart IDs to contiguous logical CPU IDs
- add T-Head vendor overrides while preserving legacy ports
- migrate qemu-virt64-riscv with RV32 and RV64 support
- update clock_time, MM, LWP, OFW, PCI and Rust integration

Existing BSPs continue using legacy_common or common64 unless they select RT_USING_RISCV_NEW_COMMON.

Signed-off-by: GuEe-GUI <2991707448@qq.com>

* [libcpu][risc-v] skip disabled CPU nodes in early FDT walk

Rbb666 noted that rt_hw_fdt_install_early() walked every /cpus child
without checking status, so disabled cores still joined hart registration
and MMU capability probing. Skip nodes that are not okay/ok via
rt_fdt_device_is_available() before reading reg or mmu-type.

Signed-off-by: seteiro <chenmoushun@163.com>

* [libcpu][risc-v] Fix per-CPU page table mapping

- reuse existing non-leaf page table entries when mapping per-CPU areas
- reject leaf entry conflicts instead of overwriting mappings

Signed-off-by: GuEe-GUI <2991707448@qq.com>

---------

Signed-off-by: GuEe-GUI <2991707448@qq.com>
Signed-off-by: seteiro <chenmoushun@163.com>
Co-authored-by: seteiro <chenmoushun@163.com>
This commit is contained in:
GUIandseteiro authored and GitHub committed 2026-10-17 09:44:26 +08:00
1 parent c1d931bcc7
commit 8d8dc96b67
165 files changed
+15150 -2633

No files matched your search

+39
View File
@@ -269,6 +269,12 @@ jobs:
- name: Build BSP
if: ${{ env.TOOLCHAIN_INSTALLED != '' && success() }}
run: |
# Keep regular RISC-V jobs UP; SMP coverage has its own matrix entry.
if [[ "$TEST_BSP_ROOT" == "bsp/qemu-virt64-riscv" && "${{ matrix.platform.SMP_RUN }}" != "smp" ]]; then
echo "# CONFIG_RT_USING_SMP is not set" >> $TEST_BSP_ROOT/.config
echo "CONFIG_RT_CPUS_NR=1" >> $TEST_BSP_ROOT/.config
fi
# Whether plan to run SMP?
if [[ "${{ matrix.platform.SMP_RUN }}" == "smp" ]]; then
cat $UTEST_CONFIG_ROOT/smp/smp.cfg >> $TEST_BSP_ROOT/.config
@@ -338,6 +344,39 @@ jobs:
$QEMU_BIN -cpu c906fd -M $TEST_QEMU_MACHINE -kernel $TEST_BSP_ROOT/rtthread.bin -nographic \
-drive if=none,file=$TEST_BSP_ROOT/sd.bin,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
> qemu_output_$TEST_QEMU_ARCH.log 2>&1 &
elif [[ "$TEST_BSP_ROOT" == "bsp/qemu-virt64-riscv" ]]; then
cpus=$(grep -E '^#define[[:space:]]+RT_CPUS_NR' "$TEST_BSP_ROOT/rtconfig.h" | awk '{print $3}')
cpus=${cpus:-1}
dtb_args=()
if [[ "${{ matrix.platform.KERNEL }}" == "rtsmart" ]]; then
# QEMU 6.2 has no satp-mode CPU properties; RT-Smart currently uses an Sv39 VA layout.
dtb="$RUNNER_TEMP/qemu-${TEST_QEMU_ARCH}-sv39.dtb"
qemu-system-$TEST_QEMU_ARCH \
-M "$TEST_QEMU_MACHINE,dumpdtb=$dtb" \
-cpu rv64 \
-smp "$cpus" \
-m 256M
# QEMU 6.2 recreates fw-cfg after loading an external DTB.
if fdtget "$dtb" /fw-cfg@10100000 compatible >/dev/null 2>&1; then
fdtput -r "$dtb" /fw-cfg@10100000
fi
for ((cpu = 0; cpu < cpus; cpu++)); do
fdtput -t s "$dtb" "/cpus/cpu@$cpu" mmu-type riscv,sv39
done
dtb_args=(-dtb "$dtb")
fi
qemu-system-$TEST_QEMU_ARCH \
-nographic \
-M $TEST_QEMU_MACHINE \
-cpu rv64 \
-smp "$cpus" \
-m 256M \
-kernel "$TEST_BSP_ROOT/rtthread.bin" \
"${dtb_args[@]}" \
-append "console=ttyS0 earlycon=sbi" \
> qemu_output_$TEST_QEMU_ARCH.log 2>&1 &
elif [[ "${{ matrix.platform.QEMU_ARCH }}" == "arm" ]]; then
qemu-system-$TEST_QEMU_ARCH \
-nographic \
File diff suppressed because it is too large. Load diff
+4 -2
View File
@@ -1,3 +1,5 @@
mnt.c
romfs_data.c
*.dtb
*.dts
*.qcow2
opensbi
romfs_data.c
+35 -40
View File
@@ -6,52 +6,47 @@ RTT_DIR := ../../
PKGS_DIR := packages
SOC_DM_PIC_DIR := $(RTT_DIR)/libcpu/risc-v/common/pic
SOC_DM_SERIAL_DIR := $(RTT_DIR)/libcpu/risc-v/common/serial
SOC_DM_HWCACHE_DIR := $(RTT_DIR)/libcpu/risc-v/common/hwcache
SOC_DM_SOC_DIR := $(BSP_DIR)/driver
choice
prompt "RISC-V XLEN"
default BSP_QEMU_RISCV64
config BSP_QEMU_RISCV32
bool "RV32"
config BSP_QEMU_RISCV64
bool "RV64"
endchoice
config ARCH_TEXT_OFFSET
range 0x400000 0x400000 if BSP_QEMU_RISCV32
range 0x200000 0x200000 if BSP_QEMU_RISCV64
default 0x400000 if BSP_QEMU_RISCV32
default 0x200000 if BSP_QEMU_RISCV64
config ARCH_RAM_OFFSET
range 0x80000000 0x80000000
default 0x80000000
source "$(RTT_DIR)/Kconfig"
osource "$PKGS_DIR/Kconfig"
rsource "driver/Kconfig"
config BOARD_QEMU_VIRT_RV64
config BSP_USING_QEMU_RISCV_VIRT
bool
select ARCH_RISCV64
select ARCH_USING_RISCV_COMMON64
select ARCH_RISCV32 if BSP_QEMU_RISCV32
select ARCH_RISCV64 if BSP_QEMU_RISCV64
select RT_USING_RISCV_NEW_COMMON
select RT_USING_COMPONENTS_INIT
select RT_USING_USER_MAIN
select RT_USING_CACHE
select ARCH_MM_MMU
select ARCH_REMAP_KERNEL
select ARCH_REMAP_KERNEL if RT_USING_SMART
select ARCH_RISCV_FPU_D if BSP_QEMU_RISCV64
select RT_KLIBC_USING_VSNPRINTF_LONGLONG
select RT_USING_HW_ATOMIC
select RT_USING_STDC_ATOMIC
default y
config ENABLE_FPU
bool "Enable FPU"
select ARCH_RISCV_FPU
default y
config ENABLE_VECTOR
bool "Using RISC-V Vector Extension"
select ARCH_RISCV_VECTOR
default n
if ENABLE_VECTOR
choice ARCH_VECTOR_VLEN_SEL
prompt "Vector Registers Length in Bits"
default ARCH_VECTOR_VLEN_128
config ARCH_VECTOR_VLEN_128
bool "128"
config ARCH_VECTOR_VLEN_256
bool "256"
endchoice
endif
config RT_USING_USERSPACE_32BIT_LIMIT
bool "Enable userspace 32bit limit"
default n
config RT_USING_VIRTIO_MMIO_ALIGN
bool "Open packed attribution, this may caused an error on virtio"
default n
config __STACKSIZE__
int "stack size for interrupt"
default 4096
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
-36
View File
@@ -21,45 +21,9 @@ env['ASCOM'] = env['ASPPCOM']
Export('RTT_ROOT')
Export('rtconfig')
rtconfig.CPU='virt64'
rtconfig.ARCH='risc-v'
# prepare building environment
objs = PrepareBuilding(env, RTT_ROOT, has_libcpu = False)
if GetDepend('ENABLE_VECTOR'):
env['ASFLAGS'] = str(env['ASFLAGS']).replace('-march=rv64imafdc ', '-march=rv64imafdcv ')
stack_size = 4096
if GetDepend('RT_USING_SMART'):
# use smart link.lds
env['LINKFLAGS'] = env['LINKFLAGS'].replace('link.lds', 'link_smart.lds')
stack_lds = open('link_stacksize.lds', 'w')
if GetDepend('__STACKSIZE__'): stack_size = GetDepend('__STACKSIZE__')
stack_lds.write('__STACKSIZE__ = %d;\n' % stack_size)
stack_lds.close()
# Obtain the number of harts from rtconfig.h and write
# it into link_cpus.lds for the linker script
try:
with open('rtconfig.h', 'r') as f:
rtconfig_content = f.readlines()
except FileNotFoundError:
cpus_nr = 1
else:
cpus_nr = 1 # default value
for line in rtconfig_content:
line = line.strip()
if line.startswith('#define') and 'RT_CPUS_NR' in line:
parts = line.split()
if len(parts) >= 3 and parts[2].isdigit():
cpus_nr = int(parts[2])
break
with open('link_cpus.lds', 'w') as cpus_lds:
cpus_lds.write(f'RT_CPUS_NR = {cpus_nr};\n')
# make a building
DoBuilding(TARGET, objs)
-42
View File
@@ -1,42 +0,0 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021/08/19 bernard the first version
*/
#include <rtthread.h>
#ifdef RT_USING_DFS
#include <dfs_fs.h>
#include <rtdbg.h>
int mnt_init(void)
{
if (rt_device_find("virtio-blk0"))
{
/* mount virtio-blk as root directory */
if (dfs_mount("virtio-blk0", "/", "elm", 0, RT_NULL) == 0)
{
rt_kprintf("file system initialization done!\n");
}
else
{
if (dfs_mount("virtio-blk0", "/", "ext", 0, RT_NULL) == 0)
{
rt_kprintf("file system initialization done!\n");
}
else
{
rt_kprintf("file system initialization fail!\n");
}
}
}
return 0;
}
INIT_ENV_EXPORT(mnt_init);
#endif
+8 -32
View File
@@ -1,33 +1,9 @@
menu "RISC-V QEMU virt64 configs"
config RT_GRAPHIC_ROMFB_WIDTH
int "ROM FB width"
depends on RT_GRAPHIC_FB
default 800
config BSP_USING_VIRTIO_BLK
bool "Using VirtIO BLK"
select RT_USING_LEGACY_VIRTIO
select RT_USING_VIRTIO_BLK
default y
config BSP_USING_VIRTIO_NET
bool "Using VirtIO NET"
select RT_USING_LEGACY_VIRTIO
select RT_USING_VIRTIO_NET
default y
config BSP_USING_VIRTIO_CONSOLE
bool "Using VirtIO Console"
select RT_USING_LEGACY_VIRTIO
select RT_USING_VIRTIO_CONSOLE
default y
config BSP_USING_VIRTIO_GPU
bool "Using VirtIO GPU"
select RT_USING_LEGACY_VIRTIO
select RT_USING_VIRTIO_GPU
default y
config BSP_USING_VIRTIO_INPUT
bool "Using VirtIO Input"
select RT_USING_LEGACY_VIRTIO
select RT_USING_VIRTIO_INPUT
default y
endmenu
config RT_GRAPHIC_ROMFB_HEIGHT
int "ROM FB height"
depends on RT_GRAPHIC_FB
default 600
+5 -10
View File
@@ -3,17 +3,12 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
src = ['board.c']
CPPPATH = [cwd]
if GetDepend(['RT_GRAPHIC_FB']):
src += ['drv_romfb.c']
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)
objs = [group]
list = os.listdir(cwd)
for item in list:
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
objs = objs + SConscript(os.path.join(item, 'SConscript'))
Return('objs')
Return('group')
+3 -113
View File
@@ -1,122 +1,12 @@
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-01-30 lizhirui first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include "mm_aspace.h"
#include "tick.h"
#include "drv_uart.h"
#include "encoding.h"
#include "stack.h"
#include "sbi.h"
#include "riscv.h"
#include "plic.h"
#include "stack.h"
#ifdef RT_USING_SMP
#include "interrupt.h"
#endif /* RT_USING_SMP */
#ifdef RT_USING_SMART
#include "riscv_mmu.h"
#include "mmu.h"
#include "page.h"
#include "lwp_arch.h"
rt_region_t init_page_region = {(rt_size_t)RT_HW_PAGE_START, (rt_size_t)RT_HW_PAGE_END};
extern size_t MMUTable[];
struct mem_desc platform_mem_desc[] = {
{KERNEL_VADDR_START, (rt_size_t)RT_HW_PAGE_END - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
};
#define NUM_MEM_DESC (sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]))
#endif
rt_uint64_t rt_hw_get_clock_timer_freq(void)
{
return 10000000ULL;
}
void primary_cpu_entry(void)
{
/* disable global interrupt */
rt_hw_interrupt_disable();
entry();
}
#define IOREMAP_SIZE (1ul << 30)
#ifndef ARCH_REMAP_KERNEL
#define IOREMAP_VEND USER_VADDR_START
#else
#define IOREMAP_VEND 0ul
#endif /* ARCH_REMAP_KERNEL */
#include <setup.h>
void rt_hw_board_init(void)
{
#ifdef RT_USING_SMART
/* init data structure */
rt_hw_mmu_map_init(&rt_kernel_space, (void *)(IOREMAP_VEND - IOREMAP_SIZE), IOREMAP_SIZE, (rt_size_t *)MMUTable, PV_OFFSET);
/* init page allocator */
rt_page_init(init_page_region);
/* setup region, and enable MMU */
rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, NUM_MEM_DESC);
#endif
#ifdef RT_USING_HEAP
/* initialize memory system */
rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
#endif
plic_init();
rt_hw_interrupt_init();
rt_hw_uart_init();
#ifdef RT_USING_CONSOLE
/* set console device */
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif /* RT_USING_CONSOLE */
rt_hw_tick_init();
#ifdef RT_USING_SMP
/* ipi init */
rt_hw_ipi_init();
#endif /* RT_USING_SMP */
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
#ifdef RT_USING_HEAP
rt_kprintf("heap: [0x%08x - 0x%08x]\n", (rt_ubase_t)RT_HW_HEAP_BEGIN, (rt_ubase_t)RT_HW_HEAP_END);
#endif /* RT_USING_HEAP */
rt_hw_common_setup();
}
void rt_hw_cpu_reset(void)
{
sbi_shutdown();
while (1)
;
}
MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_reset, reboot, reset machine);
+3 -20
View File
@@ -1,11 +1,7 @@
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-01-30 lizhirui first version
*/
#ifndef BOARD_H__
@@ -13,22 +9,9 @@
#include <rtconfig.h>
extern unsigned int __bss_start;
extern unsigned int __bss_end;
#ifndef RT_USING_SMART
#define KERNEL_VADDR_START 0x0
#endif
#define VIRT64_SBI_MEMSZ (0x200000)
#define RT_HW_HEAP_BEGIN ((void *)&__bss_end)
#define RT_HW_HEAP_END ((void *)(RT_HW_HEAP_BEGIN + 64 * 1024 * 1024))
#define RT_HW_PAGE_START RT_HW_HEAP_END
#define RT_HW_PAGE_END ((void *)(KERNEL_VADDR_START + (128 * 1024 * 1024 - VIRT64_SBI_MEMSZ)))
extern unsigned char __bss_start;
extern unsigned char __bss_end;
void rt_hw_board_init(void);
void rt_init_user_mem(struct rt_thread *thread, const char *name,
unsigned long *entry);
#endif
+13
View File
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rtthread.h>
void platform_get_ramfb_params(rt_uint32_t *width, rt_uint32_t *height)
{
*width = RT_GRAPHIC_ROMFB_WIDTH;
*height = RT_GRAPHIC_ROMFB_HEIGHT;
}
-165
View File
@@ -1,165 +0,0 @@
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include <rthw.h>
#include <rtdevice.h>
#include <rtthread.h>
#include "board.h"
#include "drv_uart.h"
#include <stdio.h>
#ifdef RT_USING_SMART
#include <ioremap.h>
#endif
#include "sbi.h"
struct device_uart
{
rt_ubase_t hw_base;
rt_uint32_t irqno;
};
void *uart0_base = (void*)0x10000000;
struct rt_serial_device serial0;
struct device_uart uart0;
void uart_init(void)
{
rt_uint32_t div = UART_REFERENCE_CLOCK / (UART_DEFAULT_BAUDRATE * 16);
write8_uart0(UART_IER, 0x00);
write8_uart0(UART_LCR, UART_LCR_BAUD_LATCH);
// LSB
write8_uart0(0, div & 0xff);
// MSB
write8_uart0(1, (div >> 8) & 0xff);
// set word length to 8 bits, no parity
write8_uart0(UART_LCR, UART_LCR_EIGHT_BITS);
write8_uart0(UART_FCR, UART_FCR_FIFO_ENABLE | UART_FCR_FIFO_CLEAR);
return;
}
static rt_err_t _uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
return (RT_EOK);
}
static rt_err_t _uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
struct device_uart *uart = (struct device_uart*)serial->parent.user_data;
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
{
rt_uint8_t value = read8_uart0(UART_IER);
write8_uart0(UART_IER, value & ~UART_IER_RX_ENABLE);
}
break;
case RT_DEVICE_CTRL_SET_INT:
if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
{
rt_uint8_t value = read8_uart0(UART_IER);
write8_uart0(UART_IER, value | UART_IER_RX_ENABLE);
}
break;
}
return (RT_EOK);
}
static int _uart_putc(struct rt_serial_device *serial, char c)
{
struct device_uart *uart;
uart = (struct device_uart*)serial->parent.user_data;
// wait for Transmit Holding Empty to be set in LSR.
while((read8_uart0(UART_LSR) & UART_LSR_TX_IDLE) == 0)
;
write8_uart0(UART_THR, c);
return (1);
}
static int _uart_getc(struct rt_serial_device *serial)
{
struct device_uart *uart;
volatile rt_uint32_t lsr;
int ch = -1;
uart = (struct device_uart*)serial->parent.user_data;
lsr = read8_uart0(UART_LSR);
if (lsr & UART_LSR_RX_READY)
{
ch = read8_uart0(UART_RHR);
}
return ch;
}
const struct rt_uart_ops _uart_ops = {
_uart_configure,
_uart_control,
_uart_putc,
_uart_getc,
// TODO: add DMA support
RT_NULL};
static void rt_hw_uart_isr(int irqno, void *param)
{
rt_ubase_t level = rt_hw_interrupt_disable();
struct rt_serial_device *serial = (struct rt_serial_device *)param;
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
rt_hw_interrupt_enable(level);
}
/*
* UART Initiation
*/
int rt_hw_uart_init(void)
{
struct rt_serial_device *serial;
struct device_uart *uart;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
#ifdef RT_USING_SMART
uart0_base = rt_ioremap(uart0_base, 4096);
#endif
// register device
serial = &serial0;
uart = &uart0;
serial->ops = &_uart_ops;
serial->config = config;
serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
uart->hw_base = (rt_ubase_t)uart0_base;
uart->irqno = 0x0a;
rt_hw_serial_register(serial,
RT_CONSOLE_DEVICE_NAME,
RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, serial, RT_CONSOLE_DEVICE_NAME);
rt_hw_interrupt_umask(uart->irqno);
return 0;
}
/* WEAK for SDK 0.5.6 */
rt_weak void uart_debug_init(int uart_channel)
{
}
-62
View File
@@ -1,62 +0,0 @@
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#ifndef __DRV_UART_H__
#define __DRV_UART_H__
#include "riscv_io.h"
/**
* uart ns16550a
* http://byterunner.com/16550.html
*/
/* TRANSMIT AND RECEIVE HOLDING REGISTER */
#define UART_RHR 0
#define UART_THR 0
/* INTERRUPT ENABLE REGISTER */
#define UART_IER 1
#define UART_IER_RX_ENABLE (1 << 0)
#define UART_IER_TX_ENABLE (1 << 1)
/* FIFO CONTROL REGISTER */
#define UART_FCR 2
#define UART_FCR_FIFO_ENABLE (1 << 0)
#define UART_FCR_FIFO_CLEAR (3 << 1)
/* INTERRUPT STATUS REGISTER */
#define UART_ISR 2
/* LINE CONTROL REGISTER */
#define UART_LCR 3
#define UART_LCR_EIGHT_BITS (3 << 0)
// special mode to set baud rate
#define UART_LCR_BAUD_LATCH (1 << 7)
/* LINE STATUS REGISTER */
#define UART_LSR 5
// input is waiting to be read from RHR
#define UART_LSR_RX_READY (1 << 0)
// THR can accept another character to send
#define UART_LSR_TX_IDLE (1 << 5)
#define UART_REFERENCE_CLOCK 1843200
#define UART_DEFAULT_BAUDRATE 115200
extern void *uart0_base;
#define write8_uart0(idx, value) __raw_writeb(((rt_uint8_t)value), (void*)((size_t)uart0_base + (idx)))
#define read8_uart0(idx) __raw_readb((void*)((size_t)uart0_base + (idx)))
void rt_hw_uart_start_rx_thread();
int rt_hw_uart_init(void);
void drv_uart_puts(char *str); // for syscall
#endif /* __DRV_UART_H__ */
-100
View File
@@ -1,100 +0,0 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-11-11 GuEe-GUI the first version
*/
#include <rtthread.h>
#include <virt.h>
#ifdef RT_USING_LEGACY_VIRTIO
#include <virtio.h>
#ifdef BSP_USING_VIRTIO_BLK
#include <virtio_blk.h>
#endif
#ifdef BSP_USING_VIRTIO_NET
#include <virtio_net.h>
#endif
#ifdef BSP_USING_VIRTIO_CONSOLE
#include <virtio_console.h>
#endif
#ifdef BSP_USING_VIRTIO_GPU
#include <virtio_gpu.h>
#endif
#ifdef BSP_USING_VIRTIO_INPUT
#include <virtio_input.h>
#endif
#include <board.h>
static virtio_device_init_handler virtio_device_init_handlers[] =
{
#ifdef BSP_USING_VIRTIO_BLK
[VIRTIO_DEVICE_ID_BLOCK] = rt_virtio_blk_init,
#endif
#ifdef BSP_USING_VIRTIO_NET
[VIRTIO_DEVICE_ID_NET] = rt_virtio_net_init,
#endif
#ifdef BSP_USING_VIRTIO_CONSOLE
[VIRTIO_DEVICE_ID_CONSOLE] = rt_virtio_console_init,
#endif
#ifdef BSP_USING_VIRTIO_GPU
[VIRTIO_DEVICE_ID_GPU] = rt_virtio_gpu_init,
#endif
#ifdef BSP_USING_VIRTIO_INPUT
[VIRTIO_DEVICE_ID_INPUT] = rt_virtio_input_init,
#endif
[VIRTIO_DEVICE_TYPE_SIZE] = RT_NULL
};
int rt_virtio_devices_init(void)
{
int i;
rt_uint32_t irq = VIRTIO_IRQ_BASE;
rt_ubase_t mmio_base = VIRTIO_MMIO_BASE;
struct virtio_mmio_config *mmio_config;
virtio_device_init_handler init_handler;
if (sizeof(virtio_device_init_handlers) == 0)
{
/* The compiler will optimize the codes after here. */
return 0;
}
#ifdef RT_USING_SMART
mmio_base = (rt_ubase_t)rt_ioremap((void *)mmio_base, VIRTIO_MMIO_SIZE * VIRTIO_MAX_NR);
if (mmio_base == RT_NULL)
{
return -RT_ERROR;
}
#endif
for (i = 0; i < VIRTIO_MAX_NR; ++i, ++irq, mmio_base += VIRTIO_MMIO_SIZE)
{
mmio_config = (struct virtio_mmio_config *)mmio_base;
if (mmio_config->magic != VIRTIO_MAGIC_VALUE ||
mmio_config->version != RT_USING_VIRTIO_VERSION ||
mmio_config->vendor_id != VIRTIO_VENDOR_ID)
{
continue;
}
init_handler = virtio_device_init_handlers[mmio_config->device_id];
if (init_handler != RT_NULL)
{
init_handler((rt_ubase_t *)mmio_base, irq);
}
}
return 0;
}
INIT_DEVICE_EXPORT(rt_virtio_devices_init);
#endif /* RT_USING_LEGACY_VIRTIO */
-16
View File
@@ -1,16 +0,0 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-11-11 GuEe-GUI the first version
*/
#ifndef __DRV_VIRTIO_H__
#define __DRV_VIRTIO_H__
int rt_virtio_devices_init(void);
#endif /* __DRV_VIRTIO_H__ */
-30
View File
@@ -1,30 +0,0 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-02-17 GuEe-GUI the first version
*/
#ifndef VIRT_H__
#define VIRT_H__
#include <rtdef.h>
#ifdef RT_USING_SMART
#include <mmu.h>
#include <ioremap.h>
#endif
/* VirtIO */
#define VIRTIO_MMIO_BASE 0x10001000
#define VIRTIO_MMIO_SIZE 0x00001000
#define VIRTIO_MAX_NR 8
#define VIRTIO_IRQ_BASE 1
#define VIRTIO_VENDOR_ID 0x554d4551 /* "QEMU" */
#define MAX_HANDLERS 128
#endif
File diff suppressed because it is too large. Load diff
-1
View File
@@ -1 +0,0 @@
RT_CPUS_NR = 1;
File diff suppressed because it is too large. Load diff
-1
View File
@@ -1 +0,0 @@
__STACKSIZE__ = 16384;
-16
View File
@@ -1,16 +0,0 @@
QEMU_CMD="qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin -s -S"
if grep -q "#define RT_USING_SMP" ./rtconfig.h 2>/dev/null; then
hart_num=$(grep "RT_CPUS_NR = [0-9]*;" ./link_cpus.lds | awk -F'[=;]' '{gsub(/ /, "", $2); print $2}')
if [ -z "$hart_num" ]; then
hart_num=1
fi
QEMU_CMD="$QEMU_CMD -smp $hart_num"
fi
QEMU_CMD="$QEMU_CMD \
-drive if=none,file=sd.bin,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
-netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 \
-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0"
eval $QEMU_CMD
-1
View File
@@ -1 +0,0 @@
qemu-system-riscv64 -nographic -machine virt,dumpdtb=virt.dtb -m 256M -kernel rtthread.bin
-9
View File
@@ -1,9 +0,0 @@
@echo off
if exist sd.bin goto run
qemu-img create -f raw sd.bin 64M
:run
qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin ^
-drive if=none,file=sd.bin,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 ^
-netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 ^
-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0
@@ -1 +0,0 @@
qemu-system-riscv64ilp32 -cpu rv64 -M virt -m 256M -nographic -kernel rtthread.elf
-4
View File
@@ -1,4 +0,0 @@
qemu-system-riscv64 -nographic -machine virt -cpu rv64,v=true,vlen=128,vext_spec=v1.0 -m 256M -kernel rtthread.bin \
-drive if=none,file=sd.bin,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
-netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 -s -S \
-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0
@@ -1,9 +0,0 @@
if [ ! -f "sd.bin" ]; then
dd if=/dev/zero of=sd.bin bs=1024 count=65536
mkfs.fat sd.bin
fi
qemu-system-riscv64 -nographic -machine virt -cpu rv64,v=true,vlen=128,vext_spec=v1.0 -m 256M -kernel rtthread.bin \
-drive if=none,file=sd.bin,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
-netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 \
-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0
+356
View File
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+55 -2
View File
@@ -1,9 +1,33 @@
import os
import subprocess
def config_enabled(name):
try:
with open('rtconfig.h', 'r') as config:
return '#define {}'.format(name) in config.read()
except OSError:
return False
def compiler_accepts(compiler, options):
try:
result = subprocess.run(
[compiler] + options + ['-x', 'c', '-c', '-o', os.devnull, '-'],
input=b'',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
except OSError:
return False
return result.returncode == 0
# toolchains options
ARCH ='risc-v'
CPU ='virt64'
CPU =''
CROSS_TOOL ='gcc'
RISCV_ABI = os.getenv('RTT_RISCV_ABI')
RTT_ROOT = os.getenv('RTT_ROOT') or os.path.join(os.getcwd(), '..', '..')
@@ -24,6 +48,7 @@ if PLATFORM == 'gcc':
PREFIX = os.getenv('RTT_CC_PREFIX') or 'riscv64-unknown-elf-'
CC = PREFIX + 'gcc'
CXX = PREFIX + 'g++'
CPP = PREFIX + 'cpp'
AS = PREFIX + 'gcc'
AR = PREFIX + 'ar'
LINK = PREFIX + 'gcc'
@@ -32,7 +57,35 @@ if PLATFORM == 'gcc':
OBJDUMP = PREFIX + 'objdump'
OBJCPY = PREFIX + 'objcopy'
DEVICE = ' -mcmodel=medany -march=rv64imafdc -mabi=lp64 '
xlen = '64' if config_enabled('ARCH_RISCV64') else '32'
abi_base = 'lp64' if xlen == '64' else 'ilp32'
extensions = 'imac'
if config_enabled('ARCH_RISCV_FPU_D'):
extensions = 'imafdc'
elif config_enabled('ARCH_RISCV_FPU_S'):
extensions = 'imafc'
abi = RISCV_ABI
if not abi and (xlen == '32' or config_enabled('RT_USING_SMART')):
abi = abi_base
march_base = 'rv{}{}'.format(xlen, extensions)
march_modern = '{}_zicsr_zifencei'.format(march_base)
compiler = os.path.join(EXEC_PATH, CC)
if not os.path.isfile(compiler):
compiler = CC
# Preserve the toolchain ABI unless it requires unavailable ISA extensions.
if not abi and not any(compiler_accepts(compiler, ['-march=' + candidate])
for candidate in (march_modern, march_base)):
abi = abi_base
abi_option = ['-mabi={}'.format(abi)] if abi else []
march = march_modern if compiler_accepts(
compiler, ['-march={}'.format(march_modern)] + abi_option) else march_base
DEVICE = ' -mcmodel=medany -march={} '.format(march)
if abi:
DEVICE += ' -mabi={} '.format(abi)
CPPFLAGS= ' -nostdinc -undef -E -P -x assembler-with-cpp'
CFLAGS = DEVICE + '-ffreestanding -flax-vector-conversions -Wno-cpp -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -fdiagnostics-color=always'
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -D__ASSEMBLY__ '
LFLAGS = DEVICE + ' -nostartfiles -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,_start -T link.lds' + ' -lsupc++ -lgcc -static'
-43
View File
@@ -1,43 +0,0 @@
#!/bin/bash
usage ()
{
echo "Usage:"
echo "./run.sh [<path_to_image>]"
echo "Note: if <path_to_image> is not provided, will create a 'sd.bin'"
echo "in the current directory and load it by default."
}
path_image=${1}
if [ -z $path_image ]; then
path_image="./sd.bin"
if [ ! -f $path_image ]; then
dd if=/dev/zero of=$path_image bs=1024 count=65536
mkfs.fat $path_image
fi
fi
if [ ! -f $path_image ]; then
echo "ERROR: $path_image does not exist!"
usage
exit
fi
QEMU_CMD="qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin"
if grep -q "#define RT_USING_SMP" ./rtconfig.h 2>/dev/null; then
hart_num=$(grep "RT_CPUS_NR = [0-9]*;" ./link_cpus.lds 2>/dev/null | awk -F'[=;]' '{gsub(/ /, "", $2); print $2}')
if [ -z "$hart_num" ] || [ "$hart_num" -lt 1 ]; then
echo "Warning: Invalid or missing RT_CPUS_NR, defaulting to 1"
hart_num=1
fi
QEMU_CMD="$QEMU_CMD -smp $hart_num"
fi
QEMU_CMD="$QEMU_CMD \
-drive if=none,file=$path_image,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
-netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 \
-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0"
eval $QEMU_CMD
+3 -2
View File
@@ -492,11 +492,12 @@ static const char *blk_get_mounted_path(struct rt_device *device)
const char *convert_size(struct rt_device_blk_geometry *geome,
rt_size_t sector_count, rt_size_t *out_cap, rt_size_t *out_minor)
{
rt_size_t cap, minor = 0;
rt_uint64_t cap;
rt_size_t minor = 0;
int size_index = 0;
const char *size_name[] = { "B", "K", "M", "G", "T", "P", "E" };
cap = geome->bytes_per_sector * sector_count;
cap = (rt_uint64_t)geome->bytes_per_sector * sector_count;
for (size_index = 0; size_index < RT_ARRAY_SIZE(size_name) - 1; ++size_index)
{
+6 -1
View File
@@ -25,7 +25,12 @@ if os.path.isdir(arch_dir):
arch_list = os.listdir(arch_dir)
if rtconfig.ARCH in arch_list:
cpu_dir = os.path.join(arch_dir, rtconfig.ARCH, rtconfig.CPU)
if os.path.exists(cpu_dir):
if rtconfig.ARCH == 'risc-v':
if GetDepend('RT_USING_RISCV_NEW_COMMON'):
src += Glob("arch/risc-v/*.c")
elif os.path.exists(cpu_dir):
src += Glob("arch/%s/%s/*.c" % (rtconfig.ARCH, rtconfig.CPU))
elif os.path.exists(cpu_dir):
src += Glob("arch/%s/%s/*.c" % (rtconfig.ARCH, rtconfig.CPU))
else:
src += Glob("arch/%s/*.c" % rtconfig.ARCH)
@@ -1,40 +1,40 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-07-10 xqyjlj The first version.
* 2025-04-20 GuEe-GUI Port to common RISC-V timer.
*/
#include <drivers/clock_time.h>
#include <riscv_timer.h>
static volatile unsigned long _init_cnt = 0;
static volatile rt_uint64_t _init_cnt = 0;
static rt_bool_t _riscv_clock_registered = RT_FALSE;
static struct rt_clock_time_device _riscv_clock_dev;
extern rt_uint64_t rt_hw_get_clock_timer_freq(void);
static rt_uint64_t _riscv_clock_get_freq(struct rt_clock_time_device *dev)
{
RT_UNUSED(dev);
return rt_hw_get_clock_timer_freq();
return riscv_timer_get_frequency();
}
static rt_uint64_t _riscv_clock_get_counter(struct rt_clock_time_device *dev)
{
unsigned long time_elapsed;
RT_UNUSED(dev);
__asm__ __volatile__("rdtime %0" : "=r"(time_elapsed));
return (rt_uint64_t)(time_elapsed - _init_cnt);
return riscv_timer_rdtime() - _init_cnt;
}
static rt_err_t _riscv_clock_set_timeout(struct rt_clock_time_device *dev, rt_uint64_t delta)
{
RT_UNUSED(dev);
RT_UNUSED(delta);
return -RT_ENOSYS;
}
@@ -47,7 +47,7 @@ static const struct rt_clock_time_ops _riscv_clock_ops =
void rt_clock_time_source_init(void)
{
__asm__ __volatile__("rdtime %0" : "=r"(_init_cnt));
_init_cnt = riscv_timer_rdtime();
if (!_riscv_clock_registered)
{
+4
View File
@@ -20,8 +20,10 @@
#ifdef RT_USING_FINSH
#include <msh.h>
#endif
#ifdef ARCH_MM_MMU
#include <ioremap.h>
#include <mm_memblock.h>
#endif
#ifdef RT_USING_OFW
#define bootargs_select rt_ofw_bootargs_select
@@ -38,6 +40,7 @@ static int rootfs_mnt_init(void)
const char *fstype = bootargs_select("rootfstype=", 0);
const char *rw = bootargs_select("rw", 0);
#ifdef ARCH_MM_MMU
if (!dev || !fstype)
{
const char *name = "initrd";
@@ -64,6 +67,7 @@ static int rootfs_mnt_init(void)
}
}
}
#endif /* ARCH_MM_MMU */
if (fstype != cromfs_type && dev)
{
+22 -4
View File
@@ -20,6 +20,7 @@
#include <drivers/platform.h>
#include <drivers/core/bus.h>
#include <drivers/core/dm.h>
#include <drivers/misc.h>
#include "../ofw/ofw_internal.h"
@@ -42,6 +43,20 @@ static const struct rt_ofw_node_id platform_ofw_ids[] =
{ /* sentinel */ }
};
static void ofw_address_to_string(char *buffer, rt_size_t size, rt_uint64_t address)
{
rt_uint32_t address_hi = rt_upper_32_bits(address);
if (address_hi)
{
rt_snprintf(buffer, size, "%x%08x", address_hi, rt_lower_32_bits(address));
}
else
{
rt_snprintf(buffer, size, "%x", rt_lower_32_bits(address));
}
}
static void ofw_device_rename(struct rt_device *dev)
{
rt_uint32_t mask;
@@ -60,18 +75,21 @@ static void ofw_device_rename(struct rt_device *dev)
{
if (!rt_ofw_get_address(np, 0, &addr, RT_NULL))
{
char addr_str[sizeof(addr) * 2 + 1];
const char *node_name = rt_fdt_node_name(np->full_name);
rt_size_t tag_len = strchrnul(node_name, '@') - node_name;
ofw_address_to_string(addr_str, sizeof(addr_str), addr);
if (!rt_ofw_prop_read_u32(np, "mask", &mask))
{
rt_dm_dev_set_name(dev, dev_name ? "%lx.%x.%.*s:%s" : "%lx.%x.%.*s",
addr, __rt_ffs(mask) - 1, tag_len, node_name, dev_name);
rt_dm_dev_set_name(dev, dev_name ? "%s.%x.%.*s:%s" : "%s.%x.%.*s",
addr_str, __rt_ffs(mask) - 1, tag_len, node_name, dev_name);
}
else
{
rt_dm_dev_set_name(dev, dev_name ? "%lx.%.*s:%s" : "%lx.%.*s",
addr, tag_len, node_name, dev_name);
rt_dm_dev_set_name(dev, dev_name ? "%s.%.*s:%s" : "%s.%.*s",
addr_str, tag_len, node_name, dev_name);
}
return;
+4
View File
@@ -1007,6 +1007,7 @@ rt_err_t rt_dma_sync_in_data(struct rt_device *dev, void *out_data, rt_size_t si
*/
static rt_err_t dma_pool_map_linear(struct rt_dma_pool *pool)
{
#ifdef ARCH_MM_MMU
rt_region_t *region = &pool->region;
rt_size_t start = RT_ALIGN_DOWN(region->start, ARCH_PAGE_SIZE);
rt_size_t end = RT_ALIGN(region->end, ARCH_PAGE_SIZE);
@@ -1032,6 +1033,9 @@ static rt_err_t dma_pool_map_linear(struct rt_dma_pool *pool)
(unsigned long)start, (unsigned long)end);
return -RT_ERROR;
}
#else
RT_UNUSED(pool);
#endif /* ARCH_MM_MMU */
return RT_EOK;
}
+1 -1
View File
@@ -182,7 +182,7 @@ static rt_err_t syscon_probe(struct rt_platform_device *pdev)
}
syscon->iomem_size = (rt_size_t)iomem_range[1];
syscon->iomem_base = rt_ioremap((void *)iomem_range[0], syscon->iomem_size);
syscon->iomem_base = rt_ioremap((void *)(rt_ubase_t)iomem_range[0], syscon->iomem_size);
if (!syscon->iomem_base)
{
+7 -8
View File
@@ -14,6 +14,7 @@
#include <drivers/ofw_fdt.h>
#include <drivers/ofw_raw.h>
#include <drivers/core/dm.h>
#include <drivers/misc.h>
#ifdef RT_USING_MEMBLOCK
#include <mm_memblock.h>
@@ -39,12 +40,6 @@ static rt_phandle _phandle_max;
static rt_size_t _root_size_cells;
static rt_size_t _root_addr_cells;
#ifdef ARCH_CPU_64BIT
#define MIN_BIT 16
#else
#define MIN_BIT 8
#endif
const char *rt_fdt_node_name(const char *full_name)
{
const char *node_name = strrchr(full_name, '/');
@@ -367,11 +362,15 @@ static rt_err_t fdt_scan_memory(void)
if (!err)
{
LOG_I("Memory node(%d) ranges: 0x%.*lx - 0x%.*lx%s", no, MIN_BIT, base, MIN_BIT, base + size, "");
LOG_I("Memory node(%d) ranges: 0x%08x%08x - 0x%08x%08x", no,
rt_upper_32_bits(base), rt_lower_32_bits(base),
rt_upper_32_bits(base + size), rt_lower_32_bits(base + size));
}
else
{
LOG_W("Memory node(%d) ranges: 0x%.*lx - 0x%.*lx%s", no, MIN_BIT, base, MIN_BIT, base + size, " unable to record");
LOG_W("Memory node(%d) ranges: 0x%08x%08x - 0x%08x%08x unable to record", no,
rt_upper_32_bits(base), rt_lower_32_bits(base),
rt_upper_32_bits(base + size), rt_lower_32_bits(base + size));
}
}
}
+15 -15
View File
@@ -526,22 +526,22 @@ rt_uint64_t rt_ofw_reverse_address(struct rt_ofw_node *np, const char *range_typ
return address;
}
#ifdef ARCH_CPU_64BIT
#define ofw_address_cpu_cast(np, address) (void *)(address)
static void *ofw_address_to_pointer(struct rt_ofw_node *np, rt_uint64_t address)
{
#ifndef ARCH_CPU_64BIT
if (rt_upper_32_bits(address))
{
LOG_W("%s found 64-bit address = %x%08x",
rt_ofw_node_full_name(np),
rt_upper_32_bits(address), rt_lower_32_bits(address));
}
#else
#define ofw_address_cpu_cast(np, address) \
({ \
if (((address) >> 32)) \
{ \
LOG_W("%s find 64 bits address = %x%x", \
rt_ofw_node_full_name(np), \
ofw_static_cast(rt_ubase_t, (address) >> 32), \
ofw_static_cast(rt_ubase_t, (address))); \
} \
(void *)ofw_static_cast(rt_ubase_t, (address)); \
})
RT_UNUSED(np);
#endif
return (void *)(rt_ubase_t)address;
}
void *rt_ofw_iomap(struct rt_ofw_node *np, int index)
{
void *iomem = RT_NULL;
@@ -552,7 +552,7 @@ void *rt_ofw_iomap(struct rt_ofw_node *np, int index)
if (!ofw_get_address(np, index, &regs[0], &regs[1]))
{
iomem = rt_ioremap(ofw_address_cpu_cast(np, regs[0]), (size_t)regs[1]);
iomem = rt_ioremap(ofw_address_to_pointer(np, regs[0]), (size_t)regs[1]);
}
}
@@ -569,7 +569,7 @@ void *rt_ofw_iomap_by_name(struct rt_ofw_node *np, const char *name)
if (!ofw_get_address_by_name(np, name, &regs[0], &regs[1]))
{
iomem = rt_ioremap(ofw_address_cpu_cast(np, regs[0]), (size_t)regs[1]);
iomem = rt_ioremap(ofw_address_to_pointer(np, regs[0]), (size_t)regs[1]);
}
}
+7 -3
View File
@@ -13,6 +13,7 @@
#include <drivers/platform.h>
#include <drivers/core/bus.h>
#include <drivers/serial_dm.h>
#include <drivers/misc.h>
#define DBG_TAG "rtdm.ofw"
#define DBG_LVL DBG_INFO
@@ -678,9 +679,12 @@ void rt_ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too)
for (int i = header->rsvmap_nr - 1; i >= 0; --i)
{
rt_kprintf("/memreserve/\t%p %p;\n",
ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->address)),
ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->size)));
rt_uint64_t address = fdt64_to_cpu(rsvmap->address);
rt_uint64_t size = fdt64_to_cpu(rsvmap->size);
rt_kprintf("/memreserve/\t0x%08x%08x 0x%08x%08x;\n",
rt_upper_32_bits(address), rt_lower_32_bits(address),
rt_upper_32_bits(size), rt_lower_32_bits(size));
++rsvmap;
}
-3
View File
@@ -66,9 +66,6 @@ extern struct rt_ofw_node *ofw_node_reserved_memory;
extern struct rt_fdt_earlycon fdt_earlycon;
#define ofw_static_cast(to_type, value) \
(to_type)(((value) >> ((sizeof(value) - sizeof(to_type)) * 8)))
rt_err_t ofw_alias_scan(void);
int ofw_alias_node_id(struct rt_ofw_node *np);
rt_err_t ofw_phandle_hash_reset(rt_phandle min, rt_phandle max);
+2 -1
View File
@@ -248,7 +248,8 @@ rt_err_t rt_pci_ofw_parse_ranges(struct rt_ofw_node *dev_np,
struct rt_pci_host_bridge *host_bridge)
{
rt_err_t err;
int phy_addr_cells = -1, phy_size_cells = -1, cpu_addr_cells;
rt_int32_t phy_addr_cells = -1, phy_size_cells = -1;
int cpu_addr_cells;
if (!dev_np || !host_bridge)
{
+9 -3
View File
@@ -534,12 +534,18 @@ rt_err_t rt_pci_region_setup(struct rt_pci_host_bridge *host_bridge)
* according to PCI 2.1 and moreover. Use a reasonable starting value of
* 0x1000 instead if the bus start address is below 0x1000.
*/
region->bus_start = rt_max_t(rt_size_t, 0x1000, region->phy_addr);
region->bus_start = rt_max_t(rt_uint64_t, 0x1000, region->phy_addr);
LOG_I("Bus %s region(%d):",
region->flags == PCI_BUS_REGION_F_MEM ? "Memory" : (region->flags == PCI_BUS_REGION_F_PREFETCH ? "Prefetchable Mem" : (region->flags == PCI_BUS_REGION_F_IO ? "I/O" : "Unknown")), i);
LOG_I(" cpu: [%p, %p]", region->cpu_addr, (region->cpu_addr + region->size - 1));
LOG_I(" physical: [%p, %p]", region->phy_addr, (region->phy_addr + region->size - 1));
LOG_I(" cpu: [0x%08x%08x, 0x%08x%08x]",
rt_upper_32_bits(region->cpu_addr), rt_lower_32_bits(region->cpu_addr),
rt_upper_32_bits(region->cpu_addr + region->size - 1),
rt_lower_32_bits(region->cpu_addr + region->size - 1));
LOG_I(" physical: [0x%08x%08x, 0x%08x%08x]",
rt_upper_32_bits(region->phy_addr), rt_lower_32_bits(region->phy_addr),
rt_upper_32_bits(region->phy_addr + region->size - 1),
rt_lower_32_bits(region->phy_addr + region->size - 1));
}
return err;
+12 -8
View File
@@ -18,12 +18,6 @@ config MAX_HANDLERS
range 1 4294967294
default 256
config RT_PIC_ARM_GIC
bool "ARM GICv2/v1"
depends on RT_USING_PIC
select RT_USING_OFW
default n
config RT_PIC_ARM_NVIC
bool "ARM Cortex-M NVIC"
depends on RT_USING_PIC
@@ -31,8 +25,16 @@ config RT_PIC_ARM_NVIC
select RT_USING_OFW
default n
config RT_PIC_ARM_GIC
bool "ARM GICv2/v1"
depends on RT_USING_PIC
select RT_USING_OFW
default n
config RT_PIC_ARM_GIC_V2M
bool "ARM GIC V2M" if RT_PIC_ARM_GIC && RT_PCI_MSI
bool "ARM GIC V2M"
depends on RT_USING_PIC
depends on RT_PCI_MSI
depends on RT_USING_OFW
default n
@@ -43,7 +45,9 @@ config RT_PIC_ARM_GIC_V3
default n
config RT_PIC_ARM_GIC_V3_ITS
bool "ARM GICv3 ITS (Interrupt Translation Service)" if RT_PIC_ARM_GIC_V3 && RT_PCI_MSI
bool "ARM GICv3 ITS (Interrupt Translation Service)"
depends on RT_PIC_ARM_GIC_V3
depends on RT_PCI_MSI
depends on RT_USING_OFW
select RT_USING_ADT_REF
default n
@@ -34,7 +34,11 @@
#include <page.h>
#include <cpuport.h>
#ifdef RT_USING_RISCV_NEW_COMMON
#include <csr.h>
#else
#include <encoding.h>
#endif
#include <stack.h>
#include <cache.h>
@@ -113,7 +117,11 @@ int arch_user_space_init(struct rt_lwp *lwp)
void *arch_kernel_mmu_table_get(void)
{
#if defined(RT_USING_RISCV_NEW_COMMON) && defined(RT_USING_SMP) && RT_CPUS_NR > 1
return (void *)((char *)MMUTable + rt_hw_cpu_id() * ARCH_PAGE_SIZE);
#else
return (void *)((char *)MMUTable);
#endif
}
void arch_user_space_free(struct rt_lwp *lwp)
@@ -165,15 +173,25 @@ int arch_set_thread_context(void (*exit)(void), void *new_thread_stack,
RT_ASSERT(new_thread_stack != RT_NULL);
RT_ASSERT(thread_sp != RT_NULL);
struct rt_hw_stack_frame *syscall_frame;
#ifdef RT_USING_RISCV_NEW_COMMON
struct rt_hw_switch_frame *frame;
#else
struct rt_hw_stack_frame *thread_frame;
#endif
rt_uint8_t *stk;
#ifndef RT_USING_RISCV_NEW_COMMON
rt_uint8_t *syscall_stk;
#endif
stk = (rt_uint8_t *)new_thread_stack;
/* reserve syscall context, all the registers are copyed from parent */
#ifdef RT_USING_RISCV_NEW_COMMON
stk -= CTX_ALL_REG_NR * sizeof(void *);
#else
stk -= CTX_REG_NR * REGBYTES;
syscall_stk = stk;
#endif
syscall_frame = (struct rt_hw_stack_frame *)stk;
@@ -191,6 +209,15 @@ int arch_set_thread_context(void (*exit)(void), void *new_thread_stack,
rt_thread_t thread = rt_container_of((unsigned long)thread_sp, struct rt_thread, sp);
syscall_frame->tp = (rt_ubase_t)thread->thread_idr;
#ifdef RT_USING_RISCV_NEW_COMMON
frame = (struct rt_hw_switch_frame *)((rt_ubase_t)stk - sizeof(*frame));
rt_memset(frame, 0, sizeof(*frame));
frame->regs[RT_HW_SWITCH_CONTEXT_RA] = (rt_ubase_t)exit;
frame->regs[RT_HW_SWITCH_CONTEXT_XSTATUS] = (csr_read(CSR_STATUS) | SR_PP) & ~SR_IE;
stk = (void *)frame;
#else
#ifdef ARCH_USING_NEW_CTX_SWITCH
extern void *_rt_hw_stack_init(rt_ubase_t *sp, rt_ubase_t ra, rt_ubase_t sstatus);
rt_ubase_t sstatus = read_csr(sstatus) | SSTATUS_SPP;
@@ -221,6 +248,7 @@ int arch_set_thread_context(void (*exit)(void), void *new_thread_stack,
thread_frame->user_sp_exc_stack = (rt_ubase_t)syscall_stk;
#endif /* ARCH_USING_NEW_CTX_SWITCH */
#endif /* RT_USING_RISCV_NEW_COMMON */
/* save new stack top */
*thread_sp = (void *)stk;
+21 -4
View File
@@ -19,10 +19,27 @@
#define __ASSEMBLY__
#endif /* __ASSEMBLY__ */
#ifdef RT_USING_RISCV_NEW_COMMON
#include "asm.h"
#include "csr.h"
#define LOAD REG_L
#define STORE REG_S
#define REGBYTES SZREG
#define CTX_REG_NR CTX_ALL_REG_NR
#define FRAME_OFF_SSTATUS FRAME_OFF_XSTATUS
#define SSTATUS_SPP SR_PP
#define SSTATUS_SIE SR_IE
#define SSTATUS_SPIE SR_PIE
#define sstatus CSR_STATUS
#define sscratch CSR_SCRATCH
#define sepc CSR_EPC
#else
#include "cpuport.h"
#include "encoding.h"
#include "stackframe.h"
#include "asm-generic.h"
#endif
.section .text.lwp
@@ -267,13 +284,13 @@ copy_context_loop:
LOAD t0, 0(t1)
STORE t0, 0(t2)
addi s0, s0, -1
addi t1, t1, 8
addi t2, t2, 8
addi t1, t1, REGBYTES
addi t2, t2, REGBYTES
bnez s0, copy_context_loop
#endif /* ARCH_USING_NEW_CTX_SWITCH */
/* fetch SYSCALL ID */
LOAD a7, 17 * REGBYTES(sp)
LOAD a7, FRAME_OFF_A7(sp)
addi a7, a7, -0xfe
beqz a7, arch_signal_quit
@@ -307,7 +324,7 @@ dont_ret_to_user:
call lwp_user_setting_restore
/* after restore the reg `tp`, need modify context */
STORE tp, 4 * REGBYTES(sp)
STORE tp, FRAME_OFF_TP(sp)
#endif
//restore context
+8 -1
View File
@@ -4,7 +4,14 @@ from building import *
group = []
cwd = GetCurrentDir()
if GetDepend('ARCH_ARM_CORTEX_A') or GetDepend('ARCH_ARMV8') or GetDepend('ARCH_RISCV64'):
new_riscv_common = GetDepend('RT_USING_RISCV_NEW_COMMON')
legacy_mmu_arch = (GetDepend('ARCH_ARM_CORTEX_A') or
GetDepend('ARCH_ARMV8') or
(GetDepend('ARCH_RISCV64') and not new_riscv_common))
new_riscv_mmu_arch = (new_riscv_common and
GetDepend('ARCH_MM_MMU'))
if legacy_mmu_arch or new_riscv_mmu_arch:
src = Glob('*.c') + Glob('*_gcc.S')
SrcRemove(src, ['ioremap-nommu.c'])
if not GetDepend('RT_USING_MEMBLOCK'):
+21
View File
@@ -7,10 +7,31 @@
#include <rtthread.h>
#include <ioremap.h>
#include <mm_aspace.h>
void *rt_ioremap_start;
size_t rt_ioremap_size;
const rt_ubase_t rt_kmem_pvoff(void)
{
return 0;
}
void rt_kmem_pvoff_set(rt_ubase_t pvoff)
{
RT_UNUSED(pvoff);
}
void *rt_kmem_v2p(void *vaddr)
{
return vaddr;
}
void *rt_kmem_p2v(void *paddr)
{
return paddr;
}
void *rt_ioremap_early(void *paddr, size_t size)
{
return size ? paddr : RT_NULL;
+2
View File
@@ -12,7 +12,9 @@
#define __TEST_CACHE_RV64_H
#ifdef ARCH_RISCV64
#ifndef RT_USING_RISCV_NEW_COMMON
#include "riscv_mmu.h"
#endif
#include <utest.h>
#include <cache.h>
#include <page.h>
+4
View File
@@ -288,6 +288,10 @@ config ARCH_RISCV64
select ARCH_CPU_64BIT
bool
if ARCH_RISCV
orsource "./risc-v/Kconfig"
endif
if ARCH_RISCV64
config ARCH_USING_NEW_CTX_SWITCH
bool
+1
View File
@@ -0,0 +1 @@
link.lds
+83
View File
@@ -0,0 +1,83 @@
menu "RISC-V Architecture Configuration"
config RT_USING_RISCV_NEW_COMMON
bool
select RT_USING_CPU_FFS
select RT_USING_HW_ATOMIC if RT_USING_SMP
select RT_PAGE_MPR_SIZE_DYNAMIC if ARCH_MM_MMU
help
Select the unified RISC-V architecture implementation. BSPs that do
not select this option continue to use the legacy implementation while
the architecture migration is in progress.
if RT_USING_RISCV_NEW_COMMON
config ARCH_HAVE_EFFICIENT_UNALIGNED_ACCESS
bool
default n
config ARCH_RISCV_DISABLE_FENCE_I
bool
default n
config ARCH_RISCV_DISABLE_SFENCE_VMA_ASID
bool
default n
config ARCH_RISCV_M_MODE
bool
depends on !ARCH_MM_MMU
default n
config ARCH_RISCV_32E
bool
depends on ARCH_RISCV32
depends on !ARCH_MM_MMU
depends on !RT_USING_DM
config ARCH_RISCV_USING_IRQ_STACK
bool "Use a dedicated interrupt stack"
depends on !RT_USING_SMART
default y if ARCH_RISCV_M_MODE || !RT_USING_DM
help
Keep the saved exception frame on the interrupted thread stack, then
run C-level trap and interrupt handlers on a per-CPU interrupt stack.
This is recommended for MCU ports.
config ARCH_RISCV_IRQ_STACK_SIZE
int "Interrupt stack size per CPU"
depends on ARCH_RISCV_USING_IRQ_STACK
range 256 65536
default 1024
config RT_USING_USERSPACE_32BIT_LIMIT
bool "Enable userspace 32bit limit"
depends on ARCH_RISCV64
default n
config ARCH_TEXT_OFFSET
hex "Kernel image offset from RAM base"
default 0x200000 if ARCH_RISCV64
default 0x400000
config ARCH_RAM_OFFSET
hex "RAM base address"
default 0
config ARCH_SECONDARY_CPU_STACK_SIZE
int "Secondary CPU stack size"
depends on RT_USING_SMP
default 4096
config ARCH_HEAP_SIZE
hex "Size of system heap"
depends on RT_USING_DM
config ARCH_INIT_PAGE_SIZE
hex "Size of init page region"
depends on RT_USING_DM && ARCH_MM_MMU
default 0x200000
endif
endmenu
+55 -14
View File
@@ -1,27 +1,68 @@
# RT-Thread building script for bridge
# RT-Thread building script for RISC-V
import os
from building import *
Import('rtconfig')
common64_arch = ['virt64', 'c906', 'c908','ur-cp100']
cwd = GetCurrentDir()
cwd = GetCurrentDir()
group = []
list = os.listdir(cwd)
cpu = getattr(rtconfig, 'CPU', '')
vendor = getattr(rtconfig, 'VENDOR', '')
if GetDepend('RT_USING_RISCV_NEW_COMMON'):
bsp_path = Dir('#').abspath
if not os.path.exists(os.path.join(bsp_path, 'link.lds')):
Env['LINKFLAGS'] = Env['LINKFLAGS'].replace(
'link.lds', os.path.join(cwd, 'link.lds'))
Preprocessing('link.lds.S', '.lds',
CPPPATH=[bsp_path, os.path.join(cwd, 'common', 'include')])
# The new common owns the complete C runtime entry.
Env['LINKFLAGS'] += ' -nostartfiles'
# add common code files
if rtconfig.CPU in common64_arch :
group += SConscript(os.path.join('common64', 'SConscript'))
else :
group += SConscript(os.path.join('common', 'SConscript'))
group += SConscript(os.path.join('vector', 'SConscript'))
# New vendor ports are architecture-wide overrides, not per-CPU copies.
if vendor:
vendor_path = os.path.join(cwd, vendor)
vendor_script = os.path.join(vendor, 'SConscript')
# cpu porting code files
if 'VENDOR' in vars(rtconfig) and rtconfig.VENDOR != '':
group = group + SConscript(os.path.join(rtconfig.VENDOR, rtconfig.CPU, 'SConscript'))
elif rtconfig.CPU in list:
group = group + SConscript(os.path.join(rtconfig.CPU, 'SConscript'))
if os.path.isfile(os.path.join(cwd, vendor_script)):
Env.Append(CPPPATH=[vendor_path])
group += SConscript(vendor_script)
if os.path.exists(os.path.join(vendor_path, 'mmu_vendor.h')):
Env.Append(ASFLAGS=' -D__HAS_MMU_VENDOR__')
Env.Append(CFLAGS=' -D__HAS_MMU_VENDOR__')
Env.Append(CXXFLAGS=' -D__HAS_MMU_VENDOR__')
if os.path.exists(os.path.join(vendor_path, 'opcode_vendor.h')):
Env.Append(ASFLAGS=' -D__HAS_OPCODE_VENDOR__')
Env.Append(CFLAGS=' -D__HAS_OPCODE_VENDOR__')
Env.Append(CXXFLAGS=' -D__HAS_OPCODE_VENDOR__')
else:
common64_cpus = ['virt64', 'c906', 'c908', 'ur-cp100']
if cpu in common64_cpus:
group += SConscript(os.path.join('common64', 'SConscript'))
else:
group += SConscript(os.path.join('legacy_common', 'SConscript'))
group += SConscript(os.path.join('vector', 'SConscript'))
if vendor:
if vendor == 't-head':
port_script = os.path.join('legacy-t-head', cpu, 'SConscript')
else:
port_script = os.path.join(vendor, cpu, 'SConscript')
if os.path.isfile(os.path.join(cwd, port_script)):
group += SConscript(port_script)
elif cpu:
port_script = os.path.join(cpu, 'SConscript')
if os.path.isfile(os.path.join(cwd, port_script)):
group += SConscript(port_script)
Return('group')
File diff suppressed because it is too large. Load diff
+86 -8
View File
@@ -1,15 +1,93 @@
# RT-Thread building script for component
# RT-Thread building script for the unified RISC-V CPU port
import os
from building import *
cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp') + Glob('*_gcc.S')
CPPPATH = [cwd]
ASFLAGS = ' -I ' + cwd
Import('rtconfig')
if not GetDepend('RT_USING_HW_ATOMIC'):
SrcRemove(src, 'atomic_riscv.c')
cwd = GetCurrentDir()
bsp_path = Dir('#').abspath
CPPPATH = [cwd + '/include', cwd]
group = DefineGroup('CPU', src, depend = [''], CPPPATH = CPPPATH, ASFLAGS = ASFLAGS)
if GetDepend('ARCH_RISCV_VECTOR'):
CPPPATH += [os.path.join(cwd, '..', 'vector', 'rvv-1.0')]
bsp_startup_obj = []
builtin_fdt_obj = []
src = [
'backtrace.c',
'cpu.c',
'cpuport.c',
'isa_ext.c',
'riscv_timer.c',
'stack.c',
'trap.c',
]
if GetDepend('RT_USING_CACHE'):
src += ['cache.c']
if rtconfig.PLATFORM in ['gcc']:
src += [
'context_gcc.S',
'cpu_gcc.S',
'stack_gcc.S',
'startup.c',
'vector_gcc.S',
]
bsp_startup = os.path.join(bsp_path, 'startup_gcc.S')
if os.path.isfile(bsp_startup):
bsp_startup_obj = Env.Object('startup_gcc.o', '#startup_gcc.S')
else:
src += ['startup_gcc.S']
if GetDepend('RT_USING_BUILTIN_FDT'):
dtb_path = os.path.join(bsp_path, GetDepend('RT_BUILTIN_FDT_PATH').strip('"'))
builtin_fdt_obj = Env.Object('builtin_fdt_gcc.o', 'builtin_fdt_gcc.S')
Env.Depends(builtin_fdt_obj, dtb_path)
if GetDepend('ARCH_MM_MMU'):
src += ['mmu.c']
if GetDepend('RT_USING_HW_ATOMIC'):
src += ['atomic_riscv.c']
if GetDepend('RT_USING_LWP'):
src += ['syscall.c']
if not GetDepend('ARCH_RISCV_M_MODE'):
src += ['sbi.c']
if not GetDepend('RT_USING_DM'):
src += ['console.c']
if GetDepend('RT_USING_DM'):
src += ['setup.c']
if GetDepend('RT_USING_SMP'):
src += ['cpu_spinwait.c']
if not GetDepend('ARCH_RISCV_M_MODE'):
src += ['cpu_sbi.c']
if not GetDepend('RT_USING_DM'):
src += ['interrupt.c']
if GetDepend('ARCH_RISCV_USING_IRQ_STACK'):
src += ['irq_stack.c']
group = DefineGroup('libcpu', src, depend = [''], CPPPATH = CPPPATH)
group += bsp_startup_obj
group += builtin_fdt_obj
# build for sub-directory
list = os.listdir(cwd)
objs = []
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
objs = objs + SConscript(os.path.join(d, 'SConscript'))
group = group + objs
Return('group')
+38 -75
View File
@@ -10,150 +10,113 @@
#include <rtthread.h>
#include <asm-generic.h>
rt_atomic_t rt_hw_atomic_exchange(volatile rt_atomic_t *ptr, rt_atomic_t val)
{
rt_atomic_t result = 0;
#if __riscv_xlen == 32
asm volatile ("amoswap.w %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoswap.d %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#endif
__asm__ volatile ("amoswap." REG_WIDTH " %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
return result;
}
rt_atomic_t rt_hw_atomic_add(volatile rt_atomic_t *ptr, rt_atomic_t val)
{
rt_atomic_t result = 0;
#if __riscv_xlen == 32
asm volatile ("amoadd.w %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoadd.d %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#endif
__asm__ volatile ("amoadd." REG_WIDTH " %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
return result;
}
rt_atomic_t rt_hw_atomic_sub(volatile rt_atomic_t *ptr, rt_atomic_t val)
{
rt_atomic_t result = 0;
val = -val;
#if __riscv_xlen == 32
asm volatile ("amoadd.w %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoadd.d %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#endif
__asm__ volatile ("amoadd." REG_WIDTH " %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
return result;
}
rt_atomic_t rt_hw_atomic_xor(volatile rt_atomic_t *ptr, rt_atomic_t val)
{
rt_atomic_t result = 0;
#if __riscv_xlen == 32
asm volatile ("amoxor.w %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoxor.d %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#endif
__asm__ volatile ("amoxor." REG_WIDTH " %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
return result;
}
rt_atomic_t rt_hw_atomic_and(volatile rt_atomic_t *ptr, rt_atomic_t val)
{
rt_atomic_t result = 0;
#if __riscv_xlen == 32
asm volatile ("amoand.w %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoand.d %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#endif
__asm__ volatile ("amoand." REG_WIDTH " %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
return result;
}
rt_atomic_t rt_hw_atomic_or(volatile rt_atomic_t *ptr, rt_atomic_t val)
{
rt_atomic_t result = 0;
#if __riscv_xlen == 32
asm volatile ("amoor.w %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoor.d %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#endif
__asm__ volatile ("amoor." REG_WIDTH " %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
return result;
}
rt_atomic_t rt_hw_atomic_load(volatile rt_atomic_t *ptr)
{
rt_atomic_t result = 0;
#if __riscv_xlen == 32
asm volatile ("amoxor.w %0, x0, (%1)" : "=r"(result) : "r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoxor.d %0, x0, (%1)" : "=r"(result) : "r"(ptr) : "memory");
#endif
__asm__ volatile ("amoxor." REG_WIDTH " %0, x0, (%1)" : "=r"(result) : "r"(ptr) : "memory");
return result;
}
void rt_hw_atomic_store(volatile rt_atomic_t *ptr, rt_atomic_t val)
{
rt_atomic_t result = 0;
#if __riscv_xlen == 32
asm volatile ("amoswap.w %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoswap.d %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
#endif
__asm__ volatile ("amoswap." REG_WIDTH " %0, %1, (%2)" : "=r"(result) : "r"(val), "r"(ptr) : "memory");
}
rt_atomic_t rt_hw_atomic_flag_test_and_set(volatile rt_atomic_t *ptr)
{
rt_atomic_t result = 0;
rt_atomic_t temp = 1;
#if __riscv_xlen == 32
asm volatile ("amoor.w %0, %1, (%2)" : "=r"(result) : "r"(temp), "r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoor.d %0, %1, (%2)" : "=r"(result) : "r"(temp), "r"(ptr) : "memory");
#endif
rt_atomic_t result = 0, temp = 1;
__asm__ volatile ("amoor." REG_WIDTH " %0, %1, (%2)" : "=r"(result) : "r"(temp), "r"(ptr) : "memory");
return result;
}
void rt_hw_atomic_flag_clear(volatile rt_atomic_t *ptr)
{
rt_atomic_t result = 0;
#if __riscv_xlen == 32
asm volatile ("amoand.w %0, x0, (%1)" : "=r"(result) :"r"(ptr) : "memory");
#elif __riscv_xlen == 64
asm volatile ("amoand.d %0, x0, (%1)" : "=r"(result) :"r"(ptr) : "memory");
#endif
__asm__ volatile ("amoand." REG_WIDTH " %0, x0, (%1)" : "=r"(result) :"r"(ptr) : "memory");
}
rt_atomic_t rt_hw_atomic_compare_exchange_strong(volatile rt_atomic_t *ptr, rt_atomic_t *old, rt_atomic_t desired)
{
rt_atomic_t tmp = *old;
rt_atomic_t result = 0;
#if __riscv_xlen == 32
asm volatile(
rt_atomic_t tmp = *old, result = 0;
__asm__ volatile (
" fence iorw, ow\n"
"1: lr.w.aq %[result], (%[ptr])\n"
"1: lr." REG_WIDTH ".aq %[result], (%[ptr])\n"
" bne %[result], %[tmp], 2f\n"
" sc.w.rl %[tmp], %[desired], (%[ptr])\n"
" sc." REG_WIDTH ".rl %[tmp], %[desired], (%[ptr])\n"
" bnez %[tmp], 1b\n"
" li %[result], 1\n"
" j 3f\n"
" 2:sw %[result], (%[old])\n"
" 2:" REG_S " %[result], (%[old])\n"
" li %[result], 0\n"
" 3:\n"
: [result]"+r" (result), [tmp]"+r" (tmp), [ptr]"+r" (ptr)
: [desired]"r" (desired), [old]"r"(old)
: "memory");
#elif __riscv_xlen == 64
asm volatile(
" fence iorw, ow\n"
"1: lr.d.aq %[result], (%[ptr])\n"
" bne %[result], %[tmp], 2f\n"
" sc.d.rl %[tmp], %[desired], (%[ptr])\n"
" bnez %[tmp], 1b\n"
" li %[result], 1\n"
" j 3f\n"
" 2:sd %[result], (%[old])\n"
" li %[result], 0\n"
" 3:\n"
: [result]"+r" (result), [tmp]"+r" (tmp), [ptr]"+r" (ptr)
: [desired]"r" (desired), [old]"r"(old)
: "memory");
#endif
return result;
}
+158
View File
@@ -0,0 +1,158 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-10-18 Shell Add backtrace support
*/
#define DBG_TAG "hw.backtrace"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include <stack.h>
#include <rtthread.h>
#ifdef ARCH_MM_MMU
#include <mmu.h>
#include <mm_aspace.h>
#endif
#define WORD sizeof(rt_base_t)
#define ARCH_CONTEXT_FETCH(pctx, id) (*(((unsigned long *)pctx) + (id)))
rt_inline rt_err_t _bt_kaddr(rt_ubase_t *fp, struct rt_hw_backtrace_frame *frame)
{
rt_err_t rc;
frame->fp = *(fp - 2);
frame->pc = *(fp - 1);
if ((rt_ubase_t)fp == frame->fp)
{
rc = -RT_ERROR;
}
else
{
rc = RT_EOK;
}
return rc;
}
#ifndef ARCH_MM_MMU
static rt_bool_t _bt_thread_stack_contains(rt_thread_t thread, rt_ubase_t *fp)
{
rt_ubase_t frame_start;
rt_ubase_t stack_start;
rt_ubase_t stack_end;
if (!thread)
{
thread = rt_thread_self();
}
if (!thread || (rt_ubase_t)fp < 2 * WORD)
{
return RT_FALSE;
}
frame_start = (rt_ubase_t)(fp - 2);
stack_start = (rt_ubase_t)thread->stack_addr;
stack_end = stack_start + thread->stack_size;
return frame_start >= stack_start &&
frame_start + 2 * WORD <= stack_end;
}
#endif
#ifdef RT_USING_SMART
#include <lwp_arch.h>
#include <lwp_user_mm.h>
rt_inline rt_err_t _bt_uaddr(rt_lwp_t lwp, rt_ubase_t *fp, struct rt_hw_backtrace_frame *frame)
{
rt_err_t rc;
if (lwp_data_get(lwp, &frame->fp, fp - 2, WORD) != WORD)
{
rc = -RT_EFAULT;
}
else if (lwp_data_get(lwp, &frame->pc, fp - 1, WORD) != WORD)
{
rc = -RT_EFAULT;
}
else if ((rt_ubase_t)fp == frame->fp)
{
rc = -RT_ERROR;
}
else
{
frame->pc -= 0;
rc = RT_EOK;
}
return rc;
}
#endif /* RT_USING_SMART */
rt_err_t rt_hw_backtrace_frame_unwind(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
{
rt_err_t rc = -RT_ERROR;
rt_ubase_t *fp = (rt_ubase_t *)frame->fp;
if (fp && !((long)fp & 0x7))
{
#ifdef RT_USING_SMART
if (thread && thread->lwp)
{
void *lwp = thread->lwp;
void *this_lwp = lwp_self();
if (this_lwp == lwp && rt_hw_mmu_v2p(((rt_lwp_t)lwp)->aspace, fp) != ARCH_MAP_FAILED)
{
rc = _bt_kaddr(fp, frame);
}
else if (lwp_user_accessible_ext(lwp, (void *)fp, WORD))
{
rc = _bt_uaddr(lwp, fp, frame);
}
else
{
rc = -RT_EFAULT;
}
}
else
#endif /* RT_USING_SMART */
#ifdef ARCH_MM_MMU
if (rt_kmem_v2p(fp) != ARCH_MAP_FAILED)
#else
if (_bt_thread_stack_contains(thread, fp))
#endif
{
rc = _bt_kaddr(fp, frame);
}
else
{
rc = -RT_EINVAL;
}
}
else
{
rc = -RT_EFAULT;
}
return rc;
}
rt_err_t rt_hw_backtrace_frame_get(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
{
rt_err_t rc;
if (!thread || !frame)
{
rc = -RT_EINVAL;
}
else
{
rt_hw_switch_frame_t sframe = thread->sp;
frame->pc = sframe->regs[RT_HW_SWITCH_CONTEXT_RA];
frame->fp = sframe->regs[RT_HW_SWITCH_CONTEXT_S0];
rc = RT_EOK;
}
return rc;
}
+20
View File
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-04-20 GuEe-GUI The first version
*/
#include <asm-generic.h>
.section ".data"
.align RISCV_SZPTR
.globl rt_hw_builtin_fdt
rt_hw_builtin_fdt:
#ifdef RT_BUILTIN_FDT_PATH
.incbin RT_BUILTIN_FDT_PATH
#endif
File diff suppressed because it is too large. Load diff
+19
View File
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-05-18 Bernard port from FreeBSD
*/
#include <sbi.h>
void rt_hw_console_output(const char *str)
{
while (*str)
{
sbi_console_putchar(*str++);
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-04-20 GuEe-GUI The first version
*/
#include <csr.h>
#ifdef RT_USING_SMP
#define rt_hw_interrupt_disable rt_hw_local_irq_disable
#define rt_hw_interrupt_enable rt_hw_local_irq_enable
#endif
.section ".text","ax"
START_POINT(rt_hw_interrupt_is_disabled)
csrr a0, CSR_STATUS
andi a0, a0, SR_IE
seqz a0, a0
ret
START_POINT_END(rt_hw_interrupt_is_disabled)
START_POINT(rt_hw_interrupt_enable)
/* Restore to old csr */
csrs CSR_STATUS, a0
ret
START_POINT_END(rt_hw_interrupt_enable)
START_POINT(rt_hw_interrupt_disable)
/* Clear IE */
csrrci a0, CSR_STATUS, SR_IE
ret
START_POINT_END(rt_hw_interrupt_disable)
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-04-20 GuEe-GUI The first version
*/
#include <sbi.h>
#include <cpu.h>
#include <cpuport.h>
static int sbi_cpu_boot(rt_uint32_t cpuid, rt_uint64_t entry)
{
rt_ubase_t hartid;
if (!riscv_cpu_id_available(cpuid))
{
return -RT_EINVAL;
}
hartid = riscv_cpu_id_to_hartid(cpuid);
return sbi_hsm_hart_start(hartid, entry, entry);
}
static void sbi_cpu_shutdown(void)
{
sbi_hsm_hart_stop();
}
struct cpu_ops_t cpu_sbi_ops =
{
.method = "sbi",
.cpu_boot = sbi_cpu_boot,
.cpu_shutdown = sbi_cpu_shutdown,
};
+31
View File
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-04-20 GuEe-GUI The first version
*/
#include <rthw.h>
#include <cpu.h>
rt_uint8_t riscv_spinwait_table = 0;
static int spinwait_cpu_boot(rt_uint32_t cpuid, rt_uint64_t entry)
{
rt_hw_barrier();
HWREG8(&riscv_spinwait_table) = RT_UINT8_MAX;
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, &riscv_spinwait_table, sizeof(riscv_spinwait_table));
return 0;
}
struct cpu_ops_t cpu_spinwait_ops =
{
.method = "spinwait",
.cpu_boot = spinwait_cpu_boot,
};
+94 -73
View File
@@ -1,99 +1,120 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018/10/28 Bernard The unify RISC-V porting code.
* 2020/11/20 BalanceTWK Add FPU support
* 2023/01/04 WangShun Adapt to CH32
* 2018-10-28 Bernard The unify RISC-V porting code.
* 2025-04-20 GuEe-GUI Port ffsN and clz, spinlock
*/
#include <rthw.h>
#include <rtthread.h>
#include "cpuport.h"
#include "rt_hw_stack_frame.h"
#include <asm-generic.h>
#ifndef RT_USING_SMP
volatile rt_ubase_t rt_interrupt_from_thread = 0;
volatile rt_ubase_t rt_interrupt_to_thread = 0;
volatile rt_uint32_t rt_thread_switch_interrupt_flag = 0;
#endif
#ifdef RT_USING_CPU_FFS
/**
* This function will initialize thread stack
*
* @param tentry the entry of thread
* @param parameter the parameter of entry
* @param stack_addr the beginning stack address
* @param texit the function will be called when thread exit
*
* @return stack address
*/
rt_uint8_t *rt_hw_stack_init(void *tentry,
void *parameter,
rt_uint8_t *stack_addr,
void *texit)
int __rt_ffs(int value)
{
struct rt_hw_stack_frame *frame;
rt_uint8_t *stk;
int i;
stk = stack_addr + sizeof(rt_ubase_t);
stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_ubase_t)stk, REGBYTES);
stk -= sizeof(struct rt_hw_stack_frame);
frame = (struct rt_hw_stack_frame *)stk;
for (i = 0; i < sizeof(struct rt_hw_stack_frame) / sizeof(rt_ubase_t); i++)
{
((rt_ubase_t *)frame)[i] = 0xdeadbeef;
}
frame->ra = (rt_ubase_t)texit;
frame->a0 = (rt_ubase_t)parameter;
frame->epc = (rt_ubase_t)tentry;
/* force to machine mode(MPP=11) and set MPIE to 1 */
#ifdef ARCH_RISCV_FPU
frame->mstatus = 0x7880;
#ifdef __GNUC__
return __builtin_ffs(value);
#else
frame->mstatus = 0x1880;
#error Unsupport ffs
#endif
return stk;
}
rt_weak void rt_trigger_software_interrupt(void)
unsigned long __rt_ffsl(unsigned long value)
{
while (0);
#ifdef __GNUC__
return __builtin_ffsl(value);
#else
#error Unsupport ffsl
#endif
}
rt_weak void rt_hw_do_after_save_above(void)
unsigned long __rt_clz(unsigned long value)
{
while (1);
#ifdef __GNUC__
return __builtin_clzl(value);
#else
#error Unsupport clz
#endif
}
#endif /* RT_USING_CPU_FFS */
/*
* #ifdef RT_USING_SMP
* void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
* #else
* void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);
* #endif
*/
#ifndef RT_USING_SMP
rt_weak void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
#ifdef RT_USING_SMART
#if defined(RT_USING_SMP) && RT_CPUS_NR > 1
extern void percpu_write(rt_ubase_t offset, const void *data, rt_size_t size);
void rt_hw_set_process_id(int pid)
{
if (rt_thread_switch_interrupt_flag == 0)
rt_interrupt_from_thread = from;
rt_interrupt_to_thread = to;
rt_thread_switch_interrupt_flag = 1;
rt_trigger_software_interrupt();
return ;
percpu_write(0, &pid, sizeof(pid));
}
#endif /* end of RT_USING_SMP */
#else
static int process_id;
void rt_hw_set_process_id(int pid)
{
process_id = pid;
}
#endif /* RT_USING_SMP && RT_CPUS_NR > 1 */
#endif /* RT_USING_SMART */
#ifdef RT_USING_SMP
void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock)
{
lock->slock = 0;
}
void rt_hw_spin_lock(rt_hw_spinlock_t *lock)
{
unsigned long tmp, new_val, mask;
unsigned short ticket;
__asm__ volatile (
"1:\n\t"
" lr." REG_WIDTH ".aq %0, (%3)\n\t"
" srli %1, %0, 16\n\t"
" addi %1, %1, 1\n\t"
" slli %1, %1, 16\n\t"
" li %2, 0xffff\n\t"
" and %2, %0, %2\n\t"
" or %1, %1, %2\n\t"
" sc." REG_WIDTH ".rl %1, %1, (%3)\n\t"
" bnez %1, 1b"
: "=&r" (tmp), "=&r" (new_val), "=&r" (mask)
: "r" (&lock->slock)
: "memory");
ticket = (tmp >> 16) & 0xffff;
while (HWREG16(&lock->tickets.owner) != ticket)
{
__asm__ volatile (OPC_PAUSE_I);
}
}
void rt_hw_spin_unlock(rt_hw_spinlock_t *lock)
{
unsigned long tmp, new_val, mask;
__asm__ volatile (
"1:\n\t"
" lr." REG_WIDTH ".aq %0, (%3)\n\t"
" addi %1, %0, 1\n\t"
" li %2, 0xffff\n\t"
" and %1, %1, %2\n\t"
" slli %2, %2, 16\n\t"
" and %0, %0, %2\n\t"
" or %1, %0, %1\n\t"
" sc." REG_WIDTH ".rl %1, %1, (%3)\n\t"
" bnez %1, 1b"
: "=&r" (tmp), "=&r" (new_val), "=&r" (mask)
: "r" (&lock->slock)
: "memory");
rt_hw_barrier();
}
#endif /* RT_USING_SMP */
+3
View File
@@ -0,0 +1,3 @@
config RT_CACHE_SIFIVE_CCACHE
bool "Sifive Composable Cache controller"
depends on RT_USING_HWCACHE
+17
View File
@@ -0,0 +1,17 @@
from building import *
group = []
if not GetDepend(['RT_USING_HWCACHE']):
Return('group')
src = []
cwd = GetCurrentDir()
CPPPATH = [cwd]
if GetDepend(['RT_CACHE_SIFIVE_CCACHE']):
src += ['hwcache-sifive_ccache.c']
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
File diff suppressed because it is too large. Load diff
+105
View File
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-03-12 WangXiaoyao the first version
* 2025-04-20 GuEe-GUI Merge 64 and 32 mode
*/
#ifndef __ASM_GENERIC_H__
#define __ASM_GENERIC_H__
#if defined(__ASSEMBLER__) && !defined(__ASSEMBLY__)
/* For XuanTie GCC */
#define __ASSEMBLY__
#endif
#include <rtconfig.h>
#ifdef __ASSEMBLY__
/* use to mark a start point where every task start from */
#define START_POINT(funcname) \
.global funcname; \
.type funcname, %function; \
funcname: \
.cfi_sections .debug_frame, .eh_frame; \
.cfi_startproc; \
.cfi_undefined ra
#define START_POINT_END(name) \
.cfi_endproc; \
.size name, .-name;
#define _AC(X,Y) X
#define _AT(T,X) X
#define __ASM_STR(x) x
#else
#define _AC(X,Y) (X##Y)
#define _AT(T,X) ((T)(X))
#define __ASM_STR(x) #x
#endif
#define _UL(x) (_AC(x, UL))
#define _ULL(x) (_AC(x, ULL))
#if __riscv_xlen == 64
#define BITS_PER_LONG 64
#define __REG_SEL(a, b) __ASM_STR(a)
#elif __riscv_xlen == 32
#define BITS_PER_LONG 32
#define __REG_SEL(a, b) __ASM_STR(b)
#else
#error "Unexpected __riscv_xlen"
#endif
#ifndef RT_BITS_PER_LONG
#define RT_BITS_PER_LONG BITS_PER_LONG
#endif
#ifndef BIT
#define BIT(x) (_UL(1) << (x))
#endif
#ifndef GENMASK
#define GENMASK(h, l) (((_UL(~0)) << (l)) & (_UL(~0) >> (BITS_PER_LONG - 1 - (h))))
#endif
#define REG_WIDTH __REG_SEL(d, w)
#define REG_L __REG_SEL(ld, lw)
#define REG_S __REG_SEL(sd, sw)
#define REG_SC __REG_SEL(sc.d, sc.w)
#define REG_AMOSWAP_AQ __REG_SEL(amoswap.d.aq, amoswap.w.aq)
#define REG_ASM __REG_SEL(.dword, .word)
#define SZREG __REG_SEL(8, 4)
#define LGREG __REG_SEL(3, 2)
#define FREG_L __REG_SEL(fld, flw)
#define FREG_S __REG_SEL(fsd, fsw)
#if __riscv_xlen == 64
#ifdef __ASSEMBLY__
#define RISCV_PTR .dword
#define RISCV_SZPTR 8
#define RISCV_LGPTR 3
#else
#define RISCV_PTR ".dword"
#define RISCV_SZPTR "8"
#define RISCV_LGPTR "3"
#endif
#elif __riscv_xlen == 32
#ifdef __ASSEMBLY__
#define RISCV_PTR .word
#define RISCV_SZPTR 4
#define RISCV_LGPTR 2
#else
#define RISCV_PTR ".word"
#define RISCV_SZPTR "4"
#define RISCV_LGPTR "2"
#endif
#else
#error "Unexpected __riscv_xlen"
#endif
#endif /* __ASM_GENERIC_H__ */
File diff suppressed because it is too large. Load diff
+32
View File
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-04-20 GuEe-GUI the first version
*/
#ifndef __CACHE_H__
#define __CACHE_H__
#define L1_CACHE_SHIFT 6
#define L1_CACHE_BYTES (1 << L1_CACHE_SHIFT)
#ifndef __ASSEMBLY__
#include <rtdef.h>
void riscv_cache_set_cbom_block_size(rt_uint32_t size);
rt_uint32_t riscv_cache_get_cbom_block_size(void);
void rt_hw_cpu_icache_invalidate_all(void);
void rt_hw_cpu_icache_invalidate(void *addr, rt_size_t size);
void rt_hw_cpu_dcache_clean_and_invalidate(void *addr, rt_size_t size);
void rt_hw_icache_invalidate_all(void);
void rt_hw_sync_cache_local(void *addr, int size);
#endif /* !__ASSEMBLY__ */
#endif /* __CACHE_H__ */
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-10-10 RT-Thread the first version
*/
#ifndef __CONTEXT_EXT_H__
#define __CONTEXT_EXT_H__
#include <asm-generic.h>
#ifdef ARCH_RISCV_FPU
#ifndef __riscv_flen
#error "ARCH_RISCV_FPU requires a toolchain target with F or D extension"
#endif
/* Number of XLEN-sized stack slots occupied by 32 floating-point registers. */
#define CTX_FPU_REG_NR (32 * __riscv_flen / __riscv_xlen)
#else
#define CTX_FPU_REG_NR 0
#endif
#ifdef __ASSEMBLY__
/**
* ==================================
* RISC-V D ISA (Floating)
* ==================================
*/
#ifdef ARCH_RISCV_FPU
#define FPU_CTX_REG_OFF(n) ((__riscv_flen / 8) * (n))
#define FPU_CTX_F0_OFF FPU_CTX_REG_OFF(0)
#define FPU_CTX_F1_OFF FPU_CTX_REG_OFF(1)
#define FPU_CTX_F2_OFF FPU_CTX_REG_OFF(2)
#define FPU_CTX_F3_OFF FPU_CTX_REG_OFF(3)
#define FPU_CTX_F4_OFF FPU_CTX_REG_OFF(4)
#define FPU_CTX_F5_OFF FPU_CTX_REG_OFF(5)
#define FPU_CTX_F6_OFF FPU_CTX_REG_OFF(6)
#define FPU_CTX_F7_OFF FPU_CTX_REG_OFF(7)
#define FPU_CTX_F8_OFF FPU_CTX_REG_OFF(8)
#define FPU_CTX_F9_OFF FPU_CTX_REG_OFF(9)
#define FPU_CTX_F10_OFF FPU_CTX_REG_OFF(10)
#define FPU_CTX_F11_OFF FPU_CTX_REG_OFF(11)
#define FPU_CTX_F12_OFF FPU_CTX_REG_OFF(12)
#define FPU_CTX_F13_OFF FPU_CTX_REG_OFF(13)
#define FPU_CTX_F14_OFF FPU_CTX_REG_OFF(14)
#define FPU_CTX_F15_OFF FPU_CTX_REG_OFF(15)
#define FPU_CTX_F16_OFF FPU_CTX_REG_OFF(16)
#define FPU_CTX_F17_OFF FPU_CTX_REG_OFF(17)
#define FPU_CTX_F18_OFF FPU_CTX_REG_OFF(18)
#define FPU_CTX_F19_OFF FPU_CTX_REG_OFF(19)
#define FPU_CTX_F20_OFF FPU_CTX_REG_OFF(20)
#define FPU_CTX_F21_OFF FPU_CTX_REG_OFF(21)
#define FPU_CTX_F22_OFF FPU_CTX_REG_OFF(22)
#define FPU_CTX_F23_OFF FPU_CTX_REG_OFF(23)
#define FPU_CTX_F24_OFF FPU_CTX_REG_OFF(24)
#define FPU_CTX_F25_OFF FPU_CTX_REG_OFF(25)
#define FPU_CTX_F26_OFF FPU_CTX_REG_OFF(26)
#define FPU_CTX_F27_OFF FPU_CTX_REG_OFF(27)
#define FPU_CTX_F28_OFF FPU_CTX_REG_OFF(28)
#define FPU_CTX_F29_OFF FPU_CTX_REG_OFF(29)
#define FPU_CTX_F30_OFF FPU_CTX_REG_OFF(30)
#define FPU_CTX_F31_OFF FPU_CTX_REG_OFF(31)
#if __riscv_flen == 64
#define CTX_FREG_L fld
#define CTX_FREG_S fsd
#elif __riscv_flen == 32
#define CTX_FREG_L flw
#define CTX_FREG_S fsw
#else
#error "Unexpected __riscv_flen"
#endif
#endif /* ARCH_RISCV_FPU */
#endif /* __ASSEMBLY__ */
#ifdef ARCH_RISCV_VECTOR
#include "rvv_context.h"
#else /* !ARCH_RISCV_VECTOR */
#define CTX_VECTOR_REG_NR 0
#endif /* ARCH_RISCV_VECTOR */
#endif /* __CONTEXT_EXT_H__ */
+47
View File
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-04-20 GuEe-GUI The first version
*/
#ifndef __CPU_H__
#define __CPU_H__
#include <rtdef.h>
#ifndef RT_CPUS_NR
#define RT_CPUS_NR 1
#endif
#if defined(RT_USING_DM) && defined(RT_USING_SMP)
struct cpu_ops_t
{
const char *method;
int (*cpu_init)(rt_uint32_t id, void *param);
int (*cpu_boot)(rt_uint32_t id, rt_uint64_t entry);
void (*cpu_shutdown)(void);
};
#endif
extern rt_ubase_t boot_cpu_hartid;
extern void _secondary_cpu_entry(void);
#if defined(RT_USING_SMP) && defined(ARCH_MM_MMU)
extern char __percpu_real_end, __percpu_end, __percpu_start;
#endif /* RT_USING_SMP && ARCH_MM_MMU */
rt_ubase_t riscv_cpu_id_to_hartid(int cpu_id);
rt_bool_t riscv_cpu_id_available(int cpu_id);
int riscv_hartid_to_cpu_id(rt_ubase_t hartid);
int riscv_hartid_register(rt_ubase_t hartid);
void riscv_cpu_id_set(int cpu_id);
#if defined(RT_USING_SMP) && !defined(RT_USING_DM)
rt_size_t riscv_hartid_list_get(rt_ubase_t *hartids, rt_size_t max_harts);
#endif
void riscv_boot_hart_init(rt_ubase_t hartid);
#endif /* __CPU_H__ */
+71
View File
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-10-03 Bernard The first version
*/
#ifndef CPUPORT_H__
#define CPUPORT_H__
#include <rtconfig.h>
#ifdef RT_USING_CACHE
#include <cache.h>
#define RT_CPU_CACHE_LINE_SZ L1_CACHE_BYTES
#endif
#include <opcode.h>
#ifdef RT_USING_SMP
typedef union
{
unsigned long slock;
struct __arch_tickets
{
unsigned short owner;
unsigned short next;
} tickets;
} rt_hw_spinlock_t;
#endif /* RT_USING_SMP */
#define RISCV_FENCE(p, s) __asm__ volatile ("fence " #p "," #s:::"memory")
#define rt_hw_barrier(...) RISCV_FENCE(iorw, iorw)
#define rt_hw_wfi() __asm__ volatile ("wfi")
#define rt_hw_rmb() RISCV_FENCE(ir, ir)
#define rt_hw_wmb() RISCV_FENCE(ow, ow)
#ifndef __ASSEMBLY__
#ifdef RT_USING_CACHE
rt_inline void rt_hw_dsb(void)
{
__asm__ volatile ("fence":::"memory");
}
rt_inline void rt_hw_dmb(void)
{
__asm__ volatile ("fence":::"memory");
}
rt_inline void rt_hw_isb(void)
{
#ifndef ARCH_RISCV_DISABLE_FENCE_I
__asm__ volatile (OPC_FENCE_I:::"memory");
#endif
}
#endif /* RT_USING_CACHE */
rt_inline void rt_hw_cpu_relax(void)
{
__asm__ volatile (OPC_PAUSE_I);
rt_hw_barrier();
}
void _thread_start(void);
#endif /* !__ASSEMBLY__ */
#endif /* CPUPORT_H__ */
File diff suppressed because it is too large. Load diff
+67
View File
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-10-01 Bernard The first version
* 2025-04-20 GuEe-GUI Port for not DM
*/
#ifndef __INTERRUPT_H__
#define __INTERRUPT_H__
#include <rthw.h>
#ifndef RT_USING_DM
#ifdef RT_USING_ADT_BITMAP
#include <bitmap.h>
#define RISCV_PLIC_QUIRK_EDGE_INTERRUPT RT_BIT(0)
#define RISCV_CLIC_QUIRK_MODE_FIXED RT_BIT(0)
#define RISCV_CLIC_QUIRK_MINTTHRESH RT_BIT(1)
#define RISCV_CLIC_QUIRK_SHV RT_BIT(2)
#endif /* RT_USING_ADT_BITMAP */
#define INTC_TYPE_RISCV 0
#define INTC_TYPE_ANDESTECH 1
#define INTC_TYPE_CLIC 2
#define IRQ_INTC(vector) (vector)
#ifdef RT_USING_SMP
#define IPI_INTC(vector) (ipi_intc_base + (vector))
#endif
#define IRQ_PLIC(vector) (irq_plic_base + (vector))
#define IRQ_CLIC(vector) (vector)
#ifdef RT_USING_SMP
#define IPI_CLIC(vector) (ipi_clic_base + (vector))
extern int ipi_intc_base;
extern int ipi_clic_base;
#endif
extern int irq_plic_base;
rt_ubase_t platform_get_plic_base(void);
rt_size_t platform_get_plic_size(void);
rt_ubase_t platform_get_plic_quirks(void);
rt_ubase_t platform_get_clic_base(void);
rt_size_t platform_get_clic_size(void);
rt_ubase_t platform_get_clic_quirks(void);
#ifdef ARCH_RISCV_M_MODE
rt_ubase_t platform_get_clint_base(void);
rt_size_t platform_get_clint_size(void);
#endif
#endif /* !RT_USING_DM */
#ifdef RT_USING_SMP
void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler);
#endif
void rt_hw_interrupt_handle(rt_uint32_t vector, void *param);
#endif /* __INTERRUPT_H__ */
+95
View File
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-04-20 GuEe-GUI The first version
*/
#ifndef __ISA_EXT_H__
#define __ISA_EXT_H__
#include <rtdef.h>
#define RISCV_ISA_EXT_I 0
#define RISCV_ISA_EXT_M 1
#define RISCV_ISA_EXT_A 2
#define RISCV_ISA_EXT_F 3
#define RISCV_ISA_EXT_D 4
#define RISCV_ISA_EXT_Q 5
#define RISCV_ISA_EXT_C 6
#define RISCV_ISA_EXT_V 7
#define RISCV_ISA_EXT_H 8
#define RISCV_ISA_EXT_BASE 26
#define RISCV_ISA_EXT_SSCOFPMF 26
#define RISCV_ISA_EXT_SSTC 27
#define RISCV_ISA_EXT_SVINVAL 28
#define RISCV_ISA_EXT_SVPBMT 29
#define RISCV_ISA_EXT_ZBB 30
#define RISCV_ISA_EXT_ZICBOM 31
#define RISCV_ISA_EXT_ZIHINTPAUSE 32
#define RISCV_ISA_EXT_SVNAPOT 33
#define RISCV_ISA_EXT_ZICBOZ 34
#define RISCV_ISA_EXT_SMAIA 35
#define RISCV_ISA_EXT_SSAIA 36
#define RISCV_ISA_EXT_ZBA 37
#define RISCV_ISA_EXT_ZBS 38
#define RISCV_ISA_EXT_ZICNTR 39
#define RISCV_ISA_EXT_ZICSR 40
#define RISCV_ISA_EXT_ZIFENCEI 41
#define RISCV_ISA_EXT_ZIHPM 42
#define RISCV_ISA_EXT_SMSTATEEN 43
#define RISCV_ISA_EXT_ZICOND 44
#define RISCV_ISA_EXT_ZBC 45
#define RISCV_ISA_EXT_ZBKB 46
#define RISCV_ISA_EXT_ZBKC 47
#define RISCV_ISA_EXT_ZBKX 48
#define RISCV_ISA_EXT_ZKND 49
#define RISCV_ISA_EXT_ZKNE 50
#define RISCV_ISA_EXT_ZKNH 51
#define RISCV_ISA_EXT_ZKR 52
#define RISCV_ISA_EXT_ZKSED 53
#define RISCV_ISA_EXT_ZKSH 54
#define RISCV_ISA_EXT_ZKT 55
#define RISCV_ISA_EXT_ZVBB 56
#define RISCV_ISA_EXT_ZVBC 57
#define RISCV_ISA_EXT_ZVKB 58
#define RISCV_ISA_EXT_ZVKG 59
#define RISCV_ISA_EXT_ZVKNED 60
#define RISCV_ISA_EXT_ZVKNHA 61
#define RISCV_ISA_EXT_ZVKNHB 62
#define RISCV_ISA_EXT_ZVKSED 63
#define RISCV_ISA_EXT_ZVKSH 64
#define RISCV_ISA_EXT_ZVKT 65
#define RISCV_ISA_EXT_ZFH 66
#define RISCV_ISA_EXT_ZFHMIN 67
#define RISCV_ISA_EXT_ZIHINTNTL 68
#define RISCV_ISA_EXT_ZVFH 69
#define RISCV_ISA_EXT_ZVFHMIN 70
#define RISCV_ISA_EXT_ZFA 71
#define RISCV_ISA_EXT_ZTSO 72
#define RISCV_ISA_EXT_ZACAS 73
#define RISCV_ISA_EXT_XANDESPMU 74
#define RISCV_ISA_EXT_MAX 75
#define RISCV_ISA_EXT_INVALID RT_UINT32_MAX
#ifdef ARCH_RISCV_M_MODE
#define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SMAIA
#else
#define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SSAIA
#endif
void riscv_isa_ext_set_raw(rt_uint32_t isa);
void riscv_isa_ext_clear_raw(rt_uint32_t isa);
rt_bool_t riscv_isa_ext_test_raw(rt_uint32_t isa);
#define riscv_isa_ext_set(isa) riscv_isa_ext_set_raw(RISCV_ISA_EXT_##isa)
#define riscv_isa_ext_clear(isa) riscv_isa_ext_clear_raw(RISCV_ISA_EXT_##isa)
#define riscv_isa_ext_test(isa) riscv_isa_ext_test_raw(RISCV_ISA_EXT_##isa)
#endif /* __ISA_EXT_H__ */
File diff suppressed because it is too large. Load diff
+85
View File
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-09 Shell Add portable asm support
* 2025-04-26 GuEe-GUI Follow the specific and vendor
*/
#ifndef __OPCODE_H__
#define __OPCODE_H__
#include <rtcompiler.h>
#include <asm-generic.h>
#ifdef __HAS_OPCODE_VENDOR__
#include <opcode_vendor.h>
#endif
/**
* @brief binary opcode pseudo operations
* Used to bypass toolchain restriction on extension ISA
*
*/
/**
* @brief RISC-V instruction formats
*/
#define OPCODE_MISC_MEM 15
#define OPCODE_SYSTEM 115
#ifndef __OPC_INSN_FORMAT_R
/**
* R type: .insn r opcode6, func3, func7, rd, rs1, rs2
*
* +-------+-----+-----+-------+----+---------+
* | func7 | rs2 | rs1 | func3 | rd | opcode6 |
* +-------+-----+-----+-------+----+---------+
* 31 25 20 15 12 7 0
*/
#define __OPC_INSN_FORMAT_R(opcode, func3, func7, rd, rs1, rs2) \
".insn r "RT_STRINGIFY(opcode)","RT_STRINGIFY(func3)","RT_STRINGIFY(func7)","RT_STRINGIFY(rd)","RT_STRINGIFY(rs1)","RT_STRINGIFY(rs2)"\n\t"
#endif /* !__OPC_INSN_FORMAT_R */
#ifndef __OPC_INSN_FORMAT_I
/**
* I type: .insn i opcode6, func3, rd, rs1, simm12
*
* +--------+-----+-------+----+---------+
* | simm12 | rs1 | func3 | rd | opcode6 |
* +--------+-----+-------+----+---------+
* 31 20 15 12 7 0
*/
#define __OPC_INSN_FORMAT_I(opcode, func3, rd, rs1, simm12) \
".insn i "RT_STRINGIFY(opcode)","RT_STRINGIFY(func3)","RT_STRINGIFY(rd)","RT_STRINGIFY(rs1)","RT_STRINGIFY(simm12)"\n\t"
#endif /* !__OPC_INSN_FORMAT_I */
#ifndef OPC_CBO_INVAL
#define OPC_CBO_INVAL(base) __OPC_INSN_FORMAT_I(OPCODE_MISC_MEM, 2, x0, base, 0)
#endif
#ifndef OPC_CBO_CLEAN
#define OPC_CBO_CLEAN(base) __OPC_INSN_FORMAT_I(OPCODE_MISC_MEM, 2, x0, base, 1)
#endif
#ifndef OPC_FENCE_I
#ifdef ARCH_TOOLCHAIN_HAS_ZIFENCEI_ISA
#define OPC_FENCE_I __ASM_STR(fence.i)
#else /* !ARCH_TOOLCHAIN_HAS_ZIFENCEI_ISA */
#define OPC_FENCE_I __ASM_STR(.long 0x0000100f)
#endif /* ARCH_TOOLCHAIN_HAS_ZIFENCEI_ISA */
#endif /* !OPC_FENCE_I */
#ifndef OPC_PAUSE_I
#ifdef ARCH_TOOLCHAIN_HAS_ZIHINTPAUSE
#define OPC_PAUSE_I __ASM_STR(pause)
#else /* !ARCH_TOOLCHAIN_HAS_ZIHINTPAUSE */
#define OPC_PAUSE_I __ASM_STR(.4byte 0x0100000f)
#endif /* ARCH_TOOLCHAIN_HAS_ZIHINTPAUSE */
#endif /* !OPC_PAUSE_I */
#endif /* __OPCODE_H__ */
+31
View File
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-04-20 GuEe-GUI The first version
*/
#ifndef __PAGE_DEF_H__
#define __PAGE_DEF_H__
#include <asm-generic.h>
#ifndef ARCH_PAGE_SHIFT
#define ARCH_PAGE_SHIFT 12
#elif ARCH_PAGE_SHIFT != 12
#warning Make sure you fully understand what you are doing
#endif
#define ARCH_PAGE_SIZE (_UL(1) << ARCH_PAGE_SHIFT)
#define ARCH_PAGE_MASK (ARCH_PAGE_SIZE - 1)
#if __riscv_xlen == 32
#define SUPPER_PAGE_SIZE 0x400000
#else
#define SUPPER_PAGE_SIZE 0x200000
#endif
#define SUPPER_PAGE_MASK (SUPPER_PAGE_SIZE - 1)
#endif /* __PAGE_DEF_H__ */
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-10-28 Bernard The unify RISC-V porting code.
*/
#ifndef __RISCV_TIMER_H__
#define __RISCV_TIMER_H__
#include <rtdef.h>
void riscv_timer_set_frequency(rt_uint32_t freq);
rt_uint32_t riscv_timer_get_frequency(void);
rt_uint64_t riscv_timer_rdtime(void);
void riscv_timer_set_compare(rt_uint64_t value);
void riscv_timer_isr(void);
void riscv_timer_init(void);
void riscv_timer_probe(void *clint_timer_cmp, void *clint_timer_val);
void riscv_timer_us_delay(rt_uint32_t us);
#endif /* __RISCV_TIMER_H__ */
File diff suppressed because it is too large. Load diff
+17
View File
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-02-21 GuEe-GUI first version
*/
#ifndef __SETUP_H__
#define __SETUP_H__
void rt_hw_fdt_install_early(void *fdt);
void rt_hw_common_setup(void);
#endif /* __SETUP_H__ */
+145
View File
@@ -0,0 +1,145 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-01-30 lizhirui first version
* 2021-11-18 JasonHu add fpu member
* 2022-10-22 Shell Support kernel mode RVV
*/
#ifndef __STACK_H__
#define __STACK_H__
#include <rtconfig.h>
#include <context_ext.h>
#ifndef __ASSEMBLY__
#include <rtdef.h>
#endif
#ifdef ARCH_RISCV_FPU
#define K_XSTATUS_EXT SR_FS
#else
#define K_XSTATUS_EXT 0
#endif
#ifdef ARCH_RISCV_M_MODE
#define K_XSTATUS_DEFAULT_BASE (SR_PP | SR_PIE | K_XSTATUS_EXT)
#else
#define K_XSTATUS_DEFAULT_BASE (SR_PP | SR_PIE | SR_SUM | K_XSTATUS_EXT)
#endif
#ifdef ARCH_RISCV_VECTOR
#define K_XSTATUS_DEFAULT (K_XSTATUS_DEFAULT_BASE | SR_VS)
#else
#define K_XSTATUS_DEFAULT K_XSTATUS_DEFAULT_BASE
#endif
#ifdef ARCH_RISCV_32E
#define CTX_GENERAL_REG_NR 18
#define FRAME_OFF_SP (17 * SZREG)
#else
#define CTX_GENERAL_REG_NR 34
#define FRAME_OFF_SP (33 * SZREG)
#endif
#define CTX_ALL_REG_NR (CTX_GENERAL_REG_NR + CTX_FPU_REG_NR + CTX_VECTOR_REG_NR)
#define FRAME_OFF_XSTATUS (2 * SZREG)
#define FRAME_OFF_XCAUSE (3 * SZREG)
#define FRAME_OFF_GP (4 * SZREG)
#define FRAME_OFF_TP (5 * SZREG)
#define FRAME_OFF_A7 (18 * SZREG)
/* Switch frame */
#define RT_HW_SWITCH_CONTEXT_XSTATUS 0
#define RT_HW_SWITCH_CONTEXT_S1 1
#define RT_HW_SWITCH_CONTEXT_S0 2
#define RT_HW_SWITCH_CONTEXT_RA 3
#define RT_HW_SWITCH_CONTEXT_TP 4
#ifdef ARCH_RISCV_32E
#define RT_HW_SWITCH_CONTEXT_SIZE 8 /* Padding for alignment */
#else
#define RT_HW_SWITCH_CONTEXT_S2 5
#define RT_HW_SWITCH_CONTEXT_S3 6
#define RT_HW_SWITCH_CONTEXT_S4 7
#define RT_HW_SWITCH_CONTEXT_S5 8
#define RT_HW_SWITCH_CONTEXT_S6 9
#define RT_HW_SWITCH_CONTEXT_S7 10
#define RT_HW_SWITCH_CONTEXT_S8 11
#define RT_HW_SWITCH_CONTEXT_S9 12
#define RT_HW_SWITCH_CONTEXT_S10 13
#define RT_HW_SWITCH_CONTEXT_S11 14
#define RT_HW_SWITCH_CONTEXT_SIZE 16 /* Padding for alignment */
#endif
#ifndef __ASSEMBLY__
typedef struct rt_hw_switch_frame
{
rt_ubase_t regs[RT_HW_SWITCH_CONTEXT_SIZE];
} *rt_hw_switch_frame_t;
RT_STATIC_ASSERT(riscv_switch_frame_size,
sizeof(struct rt_hw_switch_frame) == RT_HW_SWITCH_CONTEXT_SIZE * sizeof(rt_ubase_t));
#ifdef ARCH_RISCV_USING_IRQ_STACK
void riscv_irq_stack_init(void);
void *riscv_irq_stack_top(void);
#endif
struct rt_hw_stack_frame
{
rt_ubase_t epc; /* epc - epc - program counter */
rt_ubase_t ra; /* x1 - ra - return address for jumps */
rt_ubase_t xstatus; /* - supervisor/machine status register */
rt_ubase_t xcause; /* - supervisor/machine cause register */
rt_ubase_t gp; /* x3 - gp - global pointer */
rt_ubase_t tp; /* x4 - tp - thread pointer */
rt_ubase_t t0; /* x5 - t0 - temporary register 0 */
rt_ubase_t t1; /* x6 - t1 - temporary register 1 */
rt_ubase_t t2; /* x7 - t2 - temporary register 2 */
rt_ubase_t s0_fp; /* x8 - s0/fp - saved register 0 or frame pointer */
rt_ubase_t s1; /* x9 - s1 - saved register 1 */
rt_ubase_t a0; /* x10 - a0 - return value or function argument 0 */
rt_ubase_t a1; /* x11 - a1 - return value or function argument 1 */
rt_ubase_t a2; /* x12 - a2 - function argument 2 */
rt_ubase_t a3; /* x13 - a3 - function argument 3 */
rt_ubase_t a4; /* x14 - a4 - function argument 4 */
rt_ubase_t a5; /* x15 - a5 - function argument 5 */
#ifndef ARCH_RISCV_32E
rt_ubase_t a6; /* x16 - a6 - function argument 6 */
rt_ubase_t a7; /* x17 - s7 - function argument 7 */
rt_ubase_t s2; /* x18 - s2 - saved register 2 */
rt_ubase_t s3; /* x19 - s3 - saved register 3 */
rt_ubase_t s4; /* x20 - s4 - saved register 4 */
rt_ubase_t s5; /* x21 - s5 - saved register 5 */
rt_ubase_t s6; /* x22 - s6 - saved register 6 */
rt_ubase_t s7; /* x23 - s7 - saved register 7 */
rt_ubase_t s8; /* x24 - s8 - saved register 8 */
rt_ubase_t s9; /* x25 - s9 - saved register 9 */
rt_ubase_t s10; /* x26 - s10 - saved register 10 */
rt_ubase_t s11; /* x27 - s11 - saved register 11 */
rt_ubase_t t3; /* x28 - t3 - temporary register 3 */
rt_ubase_t t4; /* x29 - t4 - temporary register 4 */
rt_ubase_t t5; /* x30 - t5 - temporary register 5 */
rt_ubase_t t6; /* x31 - t6 - temporary register 6 */
#endif /* !ARCH_RISCV_32E */
rt_ubase_t user_sp_exc_stack; /* sscratch - user mode sp/exception stack */
#ifdef ARCH_RISCV_FPU
rt_ubase_t f[CTX_FPU_REG_NR]; /* f0~f31 */
#endif
#ifdef ARCH_RISCV_VECTOR
rt_ubase_t v[CTX_VECTOR_REG_NR];
#endif
};
RT_STATIC_ASSERT(riscv_trap_frame_size,
sizeof(struct rt_hw_stack_frame) == CTX_ALL_REG_NR * sizeof(rt_ubase_t));
#endif /* __ASSEMBLY__ */
#endif /* __STACK_H__ */
+103
View File
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-28 WangXiaoyao the first version
* 2025-04-26 GuEe-GUI Follow the specific and vendor
*/
#ifndef __TLB_H__
#define __TLB_H__
#include <rtthread.h>
#include <csr.h>
#include <sbi.h>
#include <page_def.h>
#include <mm_aspace.h>
#ifdef ARCH_USING_ASID
/* Implemented ASID bits, discovered before secondary CPUs are started. */
extern rt_ubase_t riscv_asid_mask;
#endif
#define SFENCE_VMA_ASID(asid) \
__asm__ volatile ("sfence.vma x0, %0"::"r"(asid) : "memory");
#define SFENCE_VMA_ADDR_INV(start) \
__asm__ volatile ("sfence.vma %0, zero" ::"r"(start) : "memory");
rt_inline void rt_hw_tlb_invalidate_all(void)
{
#ifdef RT_USING_SMP
unsigned long mask = 0;
#endif
__asm__ volatile ("sfence.vma" ::: "memory");
#ifdef RT_USING_SMP
/* RFENCE completes on all target harts before returning. */
if (sbi_remote_sfence_vma(&mask, SBI_HART_MASK_BASE_ALL, 0, -1UL) != SBI_SUCCESS)
{
/* Continuing could reuse memory still reachable through a stale TLB. */
RT_ASSERT(0);
while (1)
{
}
}
#endif
}
rt_inline void rt_hw_tlb_invalidate_all_local(void)
{
__asm__ volatile ("sfence.vma" ::: "memory");
}
rt_inline void rt_hw_tlb_invalidate_aspace(rt_aspace_t aspace)
{
#if defined(RT_USING_SMP)
rt_hw_tlb_invalidate_all();
#elif defined(ARCH_RISCV_DISABLE_SFENCE_VMA_ASID) || !defined(ARCH_USING_ASID)
rt_hw_tlb_invalidate_all_local();
#else
if (riscv_asid_mask)
{
rt_ubase_t asid_value = aspace->asid & riscv_asid_mask;
SFENCE_VMA_ASID(asid_value);
}
else
{
rt_hw_tlb_invalidate_all_local();
}
#endif
}
rt_inline void rt_hw_tlb_invalidate_page(rt_aspace_t aspace, void *start)
{
RT_UNUSED(aspace);
#ifdef RT_USING_SMP
RT_UNUSED(start);
rt_hw_tlb_invalidate_all();
#else
SFENCE_VMA_ADDR_INV(start);
#endif
}
rt_inline void rt_hw_tlb_invalidate_range(rt_aspace_t aspace, void *start,
size_t size, size_t stride)
{
/* huge page is taking as normal page */
if (size <= ARCH_PAGE_SIZE)
{
rt_hw_tlb_invalidate_page(aspace, start);
}
else
{
rt_hw_tlb_invalidate_aspace(aspace);
}
}
#endif /* __TLB_H__ */
File diff suppressed because it is too large. Load diff
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2006-2026, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rthw.h>
#include <rtthread.h>
#include <cpu.h>
#include <csr.h>
#include <stack.h>
#define RISCV_IRQ_STACK_ALIGN 16
#define RISCV_IRQ_STACK_STRIDE RT_ALIGN(ARCH_RISCV_IRQ_STACK_SIZE, RISCV_IRQ_STACK_ALIGN)
static rt_uint8_t irq_stacks[RT_CPUS_NR][RISCV_IRQ_STACK_STRIDE]
rt_align(RISCV_IRQ_STACK_ALIGN);
void riscv_irq_stack_init(void)
{
csr_write(CSR_SCRATCH, riscv_irq_stack_top());
}
void *riscv_irq_stack_top(void)
{
int cpu_id = rt_hw_cpu_id();
RT_ASSERT(cpu_id >= 0 && cpu_id < RT_CPUS_NR);
if (cpu_id < 0 || cpu_id >= RT_CPUS_NR)
{
cpu_id = 0;
}
return &irq_stacks[cpu_id][RISCV_IRQ_STACK_STRIDE];
}
+49
View File
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-04-20 GuEe-GUI The first version
*/
#include <rthw.h>
#include <cpuport.h>
#include <isa_ext.h>
#define RISCV_ISA_WORD_BITS (sizeof(rt_ubase_t) * 8)
#define RISCV_ISA_WORDS ((RISCV_ISA_EXT_MAX + RISCV_ISA_WORD_BITS - 1) / \
RISCV_ISA_WORD_BITS)
static rt_ubase_t isa_map[RISCV_ISA_WORDS];
void riscv_isa_ext_set_raw(rt_uint32_t isa)
{
if (isa < RISCV_ISA_EXT_MAX)
{
isa_map[isa / RISCV_ISA_WORD_BITS] |=
(rt_ubase_t)1 << (isa % RISCV_ISA_WORD_BITS);
rt_hw_wmb();
}
}
void riscv_isa_ext_clear_raw(rt_uint32_t isa)
{
if (isa < RISCV_ISA_EXT_MAX)
{
isa_map[isa / RISCV_ISA_WORD_BITS] &=
~((rt_ubase_t)1 << (isa % RISCV_ISA_WORD_BITS));
rt_hw_wmb();
}
}
rt_bool_t riscv_isa_ext_test_raw(rt_uint32_t isa)
{
rt_hw_rmb();
return isa < RISCV_ISA_EXT_MAX &&
!!(isa_map[isa / RISCV_ISA_WORD_BITS] &
((rt_ubase_t)1 << (isa % RISCV_ISA_WORD_BITS)));
}
File diff suppressed because it is too large. Load diff
+24
View File
@@ -0,0 +1,24 @@
config RT_PIC_RISCV_AIA
bool "RISC-V AIA (Advanced Interrupt Architecture)"
depends on RT_USING_PIC
select RT_USING_OFW
select RT_PIC_RISCV_INTC
help
with APLIC (Advanced Platform-Level Interrupt Controller) and
IMSIC (Incoming MSI Controller)
config RT_PIC_RISCV_CLIC
bool "RISC-V CLIC (Core-Local Interrupt Controller)"
depends on RT_USING_PIC
select RT_USING_OFW
config RT_PIC_RISCV_INTC
bool "RISC-V CPU interrupt controller"
depends on RT_USING_PIC
select RT_USING_OFW
config RT_PIC_SIFIVE_PLIC
bool "SiFive PLIC (Platform-Level Interrupt Controller)"
depends on RT_USING_PIC
select RT_USING_OFW
select RT_PIC_RISCV_INTC
+29
View File
@@ -0,0 +1,29 @@
from building import *
group = []
if not GetDepend(['RT_USING_PIC']):
Return('group')
src = []
cwd = GetCurrentDir()
CPPPATH = [cwd]
if GetDepend(['RT_PIC_RISCV_AIA']):
src += ['pic-riscv-aplic.c', 'pic-riscv-imsic.c']
if GetDepend(['RT_PIC_RISCV_CLIC']):
src += ['pic-riscv-clic.c']
if GetDepend(['RT_PIC_RISCV_INTC']):
src += ['pic-riscv-intc.c']
if GetDepend(['RT_PIC_RISCV_CLIC']) or GetDepend(['RT_PIC_RISCV_INTC']):
src += ['pic-riscv-clint.c']
if GetDepend(['RT_PIC_SIFIVE_PLIC']):
src += ['pic-sifive-plic.c']
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+186
View File
@@ -0,0 +1,186 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-01-30 GuEe-GUI first version
*/
#include <rthw.h>
#include <rtthread.h>
#ifndef PIC_DISABLE_DM
#include <rtdevice.h>
#endif
#include <cpu.h>
#include <csr.h>
#include <riscv_timer.h>
#include "pic-riscv-clint.h"
#define CLINT_IPI_OFF 0
#define CLINT_TIMER_CMP_OFF 0x4000
#define CLINT_TIMER_VAL_OFF 0xbff8
#define CLINT_QUIRK_IPI_FORCE 0
#define CLINT_QUIRK_IPI_CSR_CLEAR 1
static rt_uint32_t *riscv_clint_ipi_base;
rt_uint32_t *riscv_aclint_ipi_base;
static rt_bool_t riscv_clint_ipi_csr_clear;
void clint_send_ipi(int cpu)
{
if (!riscv_cpu_id_available(cpu))
{
return;
}
if (riscv_clint_ipi_base)
{
HWREG32(riscv_clint_ipi_base + riscv_cpu_id_to_hartid(cpu)) = 1;
}
}
void clint_clear_ipi(void)
{
if (riscv_clint_ipi_base)
{
if (riscv_clint_ipi_csr_clear)
{
csr_clear(CSR_IP, IE_SIE);
}
else
{
rt_ubase_t hartid = riscv_cpu_id_to_hartid(rt_hw_cpu_id());
HWREG32(riscv_clint_ipi_base + hartid) = 0;
}
}
}
static rt_err_t riscv_clint_common_init(rt_ubase_t *mmio_ranges, unsigned mmio_count)
{
void *clint_timer_val, *clint_timer_cmp;
if (mmio_count == 1)
{
rt_uint8_t *base = rt_ioremap((void *)mmio_ranges[0], mmio_ranges[1]);
if (!base)
{
return -RT_EIO;
}
riscv_clint_ipi_base = (rt_uint32_t *)(base + CLINT_IPI_OFF);
clint_timer_cmp = base + CLINT_TIMER_CMP_OFF;
clint_timer_val = base + CLINT_TIMER_VAL_OFF;
}
else
{
riscv_clint_ipi_base = RT_NULL;
clint_timer_val = rt_ioremap((void *)mmio_ranges[0], mmio_ranges[1]);
if (!clint_timer_val)
{
return -RT_EIO;
}
clint_timer_cmp = rt_ioremap((void *)mmio_ranges[2], mmio_ranges[3]);
if (!clint_timer_cmp)
{
rt_iounmap(clint_timer_val);
return -RT_EIO;
}
}
riscv_timer_probe(clint_timer_cmp, clint_timer_val);
return RT_EOK;
}
#ifndef PIC_DISABLE_DM
#ifdef ARCH_RISCV_M_MODE
static rt_ubase_t aclint_quirks = RT_BIT(CLINT_QUIRK_IPI_FORCE);
#else
static rt_ubase_t aclint_quirks = RT_BIT(CLINT_QUIRK_IPI_FORCE) |
RT_BIT(CLINT_QUIRK_IPI_CSR_CLEAR);
#endif
static const struct rt_ofw_node_id riscv_clint_ofw_ids[] =
{
{ .compatible = "riscv,clint0", },
{ .compatible = "sifive,clint0", },
{ .compatible = "thead,c900-clint", },
#ifdef ARCH_RISCV_M_MODE
{ .compatible = "riscv,aclint-mtimer", },
{ .compatible = "riscv,aclint-mswi", .data = &aclint_quirks },
#else
{ .compatible = "riscv,aclint-sswi", .data = &aclint_quirks },
#endif
{ /* sentinel */ }
};
rt_err_t riscv_clint_ofw_init(void)
{
rt_err_t err;
rt_ubase_t quirks;
unsigned mmio_count;
rt_uint64_t addr, size;
rt_ubase_t mmio_ranges[4];
struct rt_ofw_node *np;
const struct rt_ofw_node_id *id;
if (!(np = rt_ofw_find_node_by_ids_r(RT_NULL, riscv_clint_ofw_ids, &id)))
{
return -RT_ENOSYS;
}
quirks = id->data ? *(rt_ubase_t *)id->data : 0;
if ((err = rt_ofw_get_address(np, 0, &addr, &size)))
{
goto _out;
}
mmio_ranges[0] = addr;
mmio_ranges[1] = size;
mmio_count = 1;
if (!rt_ofw_get_address(np, 1, &addr, &size))
{
mmio_ranges[2] = addr;
mmio_ranges[3] = size;
mmio_count = 2;
}
rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
if (quirks & RT_BIT(CLINT_QUIRK_IPI_FORCE))
{
riscv_clint_ipi_base = rt_ioremap((void *)mmio_ranges[0], mmio_ranges[1]);
if (!riscv_clint_ipi_base)
{
err = -RT_EIO;
goto _out;
}
riscv_aclint_ipi_base = riscv_clint_ipi_base;
riscv_clint_ipi_csr_clear = !!(quirks & RT_BIT(CLINT_QUIRK_IPI_CSR_CLEAR));
err = RT_EOK;
}
else
{
err = riscv_clint_common_init(mmio_ranges, mmio_count);
}
_out:
rt_ofw_node_put(np);
return err;
}
#endif /* !PIC_DISABLE_DM */
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __PIC_RISCV_CLINT_H__
#define __PIC_RISCV_CLINT_H__
#include <rtthread.h>
extern rt_uint32_t *riscv_aclint_ipi_base;
void clint_send_ipi(int cpu);
void clint_clear_ipi(void);
#ifndef PIC_DISABLE_DM
rt_err_t riscv_clint_ofw_init(void);
#endif
#endif /* __PIC_RISCV_CLINT_H__ */
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-01-30 GuEe-GUI first version
*/
#ifndef __PIC_RISCV_IMSIC_H__
#define __PIC_RISCV_IMSIC_H__
#include <drivers/ofw.h>
#include <drivers/misc.h>
#include <drivers/core/dm.h>
#define IMSIC_MMIO_PAGE_SHIFT 12
#define IMSIC_MMIO_PAGE_SZ RT_BIT(IMSIC_MMIO_PAGE_SHIFT)
#define IMSIC_MMIO_PAGE_LE 0x00
#define IMSIC_MMIO_PAGE_BE 0x04
#define IMSIC_MIN_ID 63
#define IMSIC_MAX_ID 2048
#define IMSIC_EIDELIVERY 0x70
#define IMSIC_EITHRESHOLD 0x72
#define IMSIC_EIP0 0x80
#define IMSIC_EIP63 0xbf
#define IMSIC_EIPx_BITS 32
#define IMSIC_EIE0 0xc0
#define IMSIC_EIE63 0xff
#define IMSIC_EIEx_BITS 32
#define IMSIC_FIRST IMSIC_EIDELIVERY
#define IMSIC_LAST IMSIC_EIE63
#define IMSIC_MMIO_SETIPNUM_LE 0x00
#define IMSIC_MMIO_SETIPNUM_BE 0x04
struct imsic_local_config
{
void *msi;
rt_uint64_t msi_phy;
rt_uint32_t hart_index;
rt_bool_t valid;
rt_bool_t online;
};
struct imsic_global_config
{
/*
* MSI Target Address Scheme
*
* XLEN-1 12 0
* | | |
* -------------------------------------------------------------
* |xxxxxx|Group Index|xxxxxxxxxxx|HART Index|Guest Index| 0 |
* -------------------------------------------------------------
*/
/* Bits representing Guest index, HART index, and Group index */
rt_uint32_t guest_index_bits;
rt_uint32_t hart_index_bits;
rt_uint32_t group_index_bits;
rt_uint32_t group_index_shift;
/* Global base address matching all target MSI addresses */
rt_ubase_t base_addr;
/* Number of interrupt identities */
rt_uint32_t nr_ids;
/* Number of guest interrupt identities */
rt_uint32_t nr_guest_ids;
/* IMSIC addresses */
struct imsic_local_config local[RT_CPUS_NR];
};
const struct imsic_global_config *imsic_global_config_read(struct rt_ofw_node *np);
rt_bool_t imsic_ofw_is_available(void);
rt_bool_t imsic_ofw_is_current_mode(struct rt_ofw_node *np);
rt_err_t imsic_ofw_probe(struct rt_ofw_node *np,
struct rt_ofw_node *ic_np, const struct rt_ofw_node_id *ic_id);
#endif /* __PIC_RISCV_IMSIC_H__ */
Loaded 100 of 165 files, more files were not shown because too many files have changed in this diff. Show more