[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

+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__ */
@@ -0,0 +1,24 @@
/*
* 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_INTC_ANDES_H__
#define __PIC_RISCV_INTC_ANDES_H__
/* Andes PMU irq number */
#define ANDES_RV_IRQ_PMOVI 18
#define ANDES_RV_IRQ_LAST ANDES_RV_IRQ_PMOVI
#define ANDES_SLI_CAUSE_BASE 256
/* Andes PMU related registers */
#define ANDES_CSR_SLIE 0x9c4
#define ANDES_CSR_SLIP 0x9c5
#define ANDES_CSR_SCOUNTEROF 0x9d4
#endif /* __PIC_RISCV_INTC_ANDES_H__ */
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+5
View File
@@ -0,0 +1,5 @@
config RT_SERIAL_EARLY_SBI
bool "Early SBI"
depends on RT_USING_SERIAL
depends on !ARCH_RISCV_M_MODE
default y
+17
View File
@@ -0,0 +1,17 @@
from building import *
group = []
if not GetDepend(['RT_USING_SERIAL']):
Return('group')
src = []
cwd = GetCurrentDir()
CPPPATH = [cwd]
if GetDepend(['RT_SERIAL_EARLY_SBI']):
src += ['serial-early-sbi.c']
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
@@ -0,0 +1,26 @@
/*
* 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 <rtdevice.h>
#include <sbi.h>
static void sbi_early_console_putchar(void *data, char c)
{
sbi_console_putchar(c);
}
static rt_err_t sbi_early_setup(struct rt_fdt_earlycon *con, const char *options)
{
con->console_putc = sbi_early_console_putchar;
return RT_EOK;
}
RT_FDT_EARLYCON_EXPORT(sbi, "sbi", "riscv,sbi-console", sbi_early_setup);
File diff suppressed because it is too large. Load diff
+62
View File
@@ -0,0 +1,62 @@
/*
* 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.
* 2021-02-11 lizhirui add gp support
* 2021-11-19 JasonHu add fpu support
* 2025-04-20 GuEe-GUI Port for CPU ID
*/
#include <rtthread.h>
#include <cpu.h>
#include <csr.h>
#include <stack.h>
#include <cpuport.h>
#ifdef ARCH_MM_MMU
#include <mmu.h>
#endif
/**
* This function will initialize thread stack, we assuming
* when scheduler restore this new thread, context will restore
* an entry to user first application
*
* s0-s11, ra, sstatus, a0
* @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)
{
rt_ubase_t *sp;
rt_hw_switch_frame_t frame;
/* We use a strict alignment requirement for Q extension */
sp = (rt_ubase_t *)RT_ALIGN_DOWN((rt_ubase_t)stack_addr, 16);
*(--sp) = (rt_ubase_t)tentry;
*(--sp) = (rt_ubase_t)parameter;
*(--sp) = (rt_ubase_t)texit;
/* Alignment */
--sp;
frame = (rt_hw_switch_frame_t)((rt_ubase_t)sp - sizeof(*frame));
rt_memset(frame, 0, sizeof(*frame));
frame->regs[RT_HW_SWITCH_CONTEXT_RA] = (rt_ubase_t)_thread_start;
frame->regs[RT_HW_SWITCH_CONTEXT_XSTATUS] = K_XSTATUS_DEFAULT;
/* Bare SMP keeps the current hart's tp across context switches. */
return (void *)frame;
}
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-10-19 RT-Thread the first version
*/
#include "asm-generic.h"
.section ".text","ax"
START_POINT(_thread_start)
REG_L ra, SZREG(sp) /* thread exit */
addi sp, sp, 2 * SZREG
REG_L a0, (sp) /* parameter */
REG_L t0, SZREG(sp) /* tentry */
addi sp, sp, 2 * SZREG
mv s1, ra
jalr t0
jalr s1
j . /* never here */
START_POINT_END(_thread_start)
+54
View File
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2006-2026, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rtthread.h>
/*
* Generic startup preserves the firmware-provided FDT before entering the
* kernel. The generic DM setup provides the strong implementation; a BSP with
* custom setup may override this hook when it still uses the common startup.
*/
rt_weak void rt_hw_fdt_install_early(void *fdt)
{
RT_UNUSED(fdt);
}
/*
* This runs after gp and a temporary stack are ready, before normal C
* runtime state is available.
*/
rt_weak void riscv_early_setup(void)
{
}
rt_weak void riscv_bss_init(void)
{
extern rt_uint8_t __bss_start[];
extern rt_uint8_t __bss_end[];
volatile rt_uint8_t *ptr = __bss_start;
rt_ubase_t *word, *word_end, word_start;
word_start = RT_ALIGN((rt_ubase_t)__bss_start, sizeof(rt_ubase_t));
while (ptr < __bss_end && (rt_ubase_t)ptr < word_start)
{
*ptr++ = 0;
}
word = (rt_ubase_t *)ptr;
word_end = (rt_ubase_t *)RT_ALIGN_DOWN((rt_ubase_t)__bss_end, sizeof(rt_ubase_t));
while (word < word_end)
{
*word++ = 0;
}
ptr = (rt_uint8_t *)word;
while (ptr < __bss_end)
{
*ptr++ = 0;
}
}
File diff suppressed because it is too large. Load diff
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-02-03 lizhirui first version
* 2022-11-10 WangXiaoyao Add readable syscall tracing
*/
#include <rthw.h>
#include <rtthread.h>
#define DBG_TAG "syscall"
#define DBG_LVL DBG_WARNING
#include <rtdbg.h>
#include <mmu.h>
#include <page.h>
#include <stack.h>
#include <lwp_user_mm.h>
typedef rt_ubase_t (*syscallfunc_t)(rt_ubase_t, rt_ubase_t, rt_ubase_t, rt_ubase_t, rt_ubase_t, rt_ubase_t, rt_ubase_t);
void syscall_handler(struct rt_hw_stack_frame *regs)
{
const char *syscall_name;
int syscallid = regs->a7;
if (syscallid == 0)
{
LOG_E("syscall id = 0!\n");
while (RT_TRUE)
{
rt_hw_cpu_relax();
}
}
syscallfunc_t syscallfunc = (syscallfunc_t)lwp_get_sys_api(syscallid);
if (syscallfunc == RT_NULL)
{
LOG_E("unsupported syscall!\n");
sys_exit_group(-1);
}
#if DBG_LVL >= DBG_INFO
syscall_name = lwp_get_syscall_name(syscallid);
#else
RT_UNUSED(syscall_name);
#endif
LOG_I("[0x%lx] %s(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)", rt_thread_self(), syscall_name,
regs->a0, regs->a1, regs->a2, regs->a3, regs->a4, regs->a5, regs->a6);
regs->a0 = syscallfunc(regs->a0, regs->a1, regs->a2, regs->a3, regs->a4, regs->a5, regs->a6);
regs->a7 = 0;
regs->epc += 4; // skip ecall instruction
LOG_I("[0x%lx] %s ret: 0x%lx", rt_thread_self(), syscall_name, regs->a0);
}
File diff suppressed because it is too large. Load diff
+157
View File
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-10-02 Bernard The first version
* 2018-12-27 Jesven Add SMP schedule
* 2021-02-02 lizhirui Add userspace support
* 2021-12-24 JasonHu Add user setting save/restore
* 2022-10-22 Shell Support kernel mode RVV;
* Rewrite trap handling routine
* 2025-04-20 GuEe-GUI Support SMP
*/
#include <asm.h>
#include <csr.h>
.section ".text","ax"
.align 6
START_POINT(system_vectors)
#ifdef ARCH_RISCV_USING_IRQ_STACK
/*
* Keep the exception frame on the interrupted stack so the scheduler can
* store it in the thread TCB. Only C-level trap handling uses the IRQ
* stack. A zero xscratch means that a nested trap is already on it.
*/
csrrw sp, CSR_SCRATCH, sp
beqz sp, _irq_stack_nested
/* Recover the interrupted SP without losing the original t0 and t1. */
addi sp, sp, -(2 * SZREG)
REG_S t0, 0 * SZREG(sp)
REG_S t1, 1 * SZREG(sp)
mv t1, sp
csrr t0, CSR_SCRATCH
mv sp, t0
REG_L t0, 0 * SZREG(t1)
REG_L t1, 1 * SZREG(t1)
csrw CSR_SCRATCH, sp
SAVE_ALL
mv s0, sp
li s1, 1
call riscv_irq_stack_top
mv sp, a0
csrw CSR_SCRATCH, zero
j _context_saved
_irq_stack_nested:
csrr sp, CSR_SCRATCH
SAVE_ALL
mv s0, sp
mv s1, zero
csrw CSR_SCRATCH, zero
#else
/* Distingush exception from kernel or user */
csrrw sp, CSR_SCRATCH, sp
bnez sp, _save_context
/*
* BE REALLY careful with xscratch,
* if it's wrong, we could looping here forever
* or accessing random memory and seeing things totally
* messy after a long time and don't even know why
*/
_from_kernel:
csrr sp, CSR_SCRATCH
j _save_context
_save_context:
SAVE_ALL
/* Clear xscratch to say 'now in kernel mode' */
csrw CSR_SCRATCH, zero
#endif /* ARCH_RISCV_USING_IRQ_STACK */
_context_saved:
LOAD_GLOBAL_POINTER
/* Now we are ready to enter interrupt / excepiton handler */
#ifdef RT_USING_SMART
_distinguish_syscall:
csrr t0, CSR_CAUSE
li t1, EXC_SYSCALL
bne t0, t1, _handle_interrupt_and_exception
call syscall_entry
/* Syscall never return here */
#endif /* RT_USING_SMART */
_handle_interrupt_and_exception:
mv a0, sp
#ifdef ARCH_RISCV_USING_IRQ_STACK
mv a0, s0
#endif
call rt_hw_trap
_interrupt_exit:
#ifdef ARCH_RISCV_USING_IRQ_STACK
/* Restore the exception stack before invoking the scheduler. */
mv sp, s0
beqz s1, 1f
call riscv_irq_stack_top
csrw CSR_SCRATCH, a0
j 2f
1:
csrw CSR_SCRATCH, zero
2:
#endif
#ifdef RT_USING_SMP
mv a0, sp
call rt_scheduler_do_irq_switch
#else
la t0, rt_thread_switch_interrupt_flag
lw t1, 0(t0)
beqz t1, _resume_execution
sw zero, 0(t0)
_context_switch:
la t0, rt_interrupt_from_thread
REG_L a0, 0(t0)
la t0, rt_interrupt_to_thread
REG_L a1, 0(t0)
csrr t0, CSR_STATUS
andi t0, t0, ~SR_PIE
csrw CSR_STATUS, t0
jal rt_hw_context_switch
#endif /* RT_USING_SMP */
_resume_execution:
.globl rt_hw_resume_interrupt
rt_hw_resume_interrupt:
#ifdef RT_USING_SMART
REG_L t0, FRAME_OFF_XSTATUS(sp)
andi t0, t0, SR_PP
bnez t0, rt_hw_resume_kernel
call arch_ret_to_user
#endif /* RT_USING_SMART */
j rt_hw_resume_kernel
START_POINT_END(system_vectors)
.globl rt_hw_resume_kernel
rt_hw_resume_kernel:
RESTORE_ALL
#ifndef ARCH_RISCV_USING_IRQ_STACK
csrw CSR_SCRATCH, zero
#endif
XRET
.align 2
.weak fast_irq_vectors
START_POINT(fast_irq_vectors)
j system_vectors
START_POINT_END(fast_irq_vectors)
+3 -2
View File
@@ -45,8 +45,9 @@
(2)PLIC中断控制器支持
`libcpu/risc-v/virt64/plic.[c|h]`提供了符合《RISC-V Platform-Level Interrupt Controller Specification version 1.0.0 》标准的PLIC中断控制器驱动代码,可作为移植参考。
PLIC 驱动请参考 `libcpu/risc-v/common/pic/pic-sifive-plic.c`。
(3)串口uart支持
目前串口驱动在各bsp目录下,可参考`bsp/qemu-virt64-riscv/driver/drv_uart.[c|h]`
目前串口驱动在各 BSP 目录下,可参考
`bsp/allwinner/libraries/drivers/drv_uart.c`。
+1 -1
View File
@@ -7,7 +7,7 @@ CPPPATH = [cwd]
if not GetDepend('ARCH_USING_ASID'):
SrcRemove(src, ['asid.c'])
src.append('../common/atomic_riscv.c')
src.append('../legacy_common/atomic_riscv.c')
group = DefineGroup('CPU', src, depend = [''], CPPPATH = CPPPATH)
+1
View File
@@ -42,6 +42,7 @@
#define FRAME_OFF_SP BYTES(32)
#define FRAME_OFF_GP BYTES(3)
#define FRAME_OFF_TP BYTES(4)
#define FRAME_OFF_A7 BYTES(17)
/* switch frame */
#define RT_HW_SWITCH_CONTEXT_SSTATUS 0
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+15
View File
@@ -0,0 +1,15 @@
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp') + Glob('*_gcc.S')
CPPPATH = [cwd]
ASFLAGS = ' -I ' + cwd
if not GetDepend('RT_USING_HW_ATOMIC'):
SrcRemove(src, 'atomic_riscv.c')
group = DefineGroup('CPU', src, depend = [''], CPPPATH = CPPPATH, ASFLAGS = ASFLAGS)
Return('group')
+159
View File
@@ -0,0 +1,159 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-03-14 WangShun first version
*/
#include <rtthread.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
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
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
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
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
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
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
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
}
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
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
}
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(
" fence iorw, ow\n"
"1: lr.w.aq %[result], (%[ptr])\n"
" bne %[result], %[tmp], 2f\n"
" sc.w.rl %[tmp], %[desired], (%[ptr])\n"
" bnez %[tmp], 1b\n"
" li %[result], 1\n"
" j 3f\n"
" 2:sw %[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;
}
File diff suppressed because it is too large. Load diff
+99
View File
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2006-2023, 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
*/
#include <rthw.h>
#include <rtthread.h>
#include "cpuport.h"
#include "rt_hw_stack_frame.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
/**
* 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)
{
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;
#else
frame->mstatus = 0x1880;
#endif
return stk;
}
rt_weak void rt_trigger_software_interrupt(void)
{
while (0);
}
rt_weak void rt_hw_do_after_save_above(void)
{
while (1);
}
/*
* #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)
{
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 ;
}
#endif /* end of RT_USING_SMP */
File renamed without changes.
Loaded 100 of 165 files, more files were not shown because too many files have changed in this diff. Show more