VSF Documented
rv_generic.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18#ifndef __RV_GENERIC_H__
19#define __RV_GENERIC_H__
20
21/*============================ INCLUDES ======================================*/
22
23#include "hal/vsf_hal_cfg.h"
24
25#define __VSF_HEADER_ONLY_SHOW_ARCH_INFO__
26#include "hal/driver/driver.h"
27#undef __VSF_HEADER_ONLY_SHOW_ARCH_INFO__
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/*============================ MACROS ========================================*/
34
35#ifndef __BYTE_ORDER
36# define __BYTE_ORDER __LITTLE_ENDIAN
37#endif
38
39#ifndef __VSF_ARCH_SYSTIMER_BITS
40# define __VSF_ARCH_SYSTIMER_BITS 64
41#endif
42
43// software interrupt provided by arch
44#ifndef VSF_ARCH_SWI_NUM
45# define VSF_ARCH_SWI_NUM 0
46#endif
47
48// VSF_ARCH_PRI_NUM is required by the kernel config for compile-time
49// clamps(vsf_kernel_cfg.h) when the chip device.h is unreachable
50// (applet builds, see below). it sizes data structures only,
51// it does NOT program any hardware
52#ifndef VSF_ARCH_PRI_NUM
53# define VSF_ARCH_PRI_NUM 8
54#endif
55
56// default systimer implementation mode, chip device.h can override before
57// this header is parsed. non-NONE default is required so that timer related
58// types(eg: vsf_systimer_tick_t) are available in applet builds
59#ifndef VSF_SYSTIMER_CFG_IMPL_MODE
60# define VSF_SYSTIMER_CFG_IMPL_MODE VSF_SYSTIMER_IMPL_WITH_COMP_TIMER
61#endif
62
63// callstack trace is disabled on RiscV by default.
64// To use callstack trace, please add -fno-omit-frame-pointer to compile options
65#ifndef VSF_ARCH_CFG_CALLSTACK_TRACE
66# define VSF_ARCH_CFG_CALLSTACK_TRACE DISABLED
67#endif
68
69/*============================ MACROFIED FUNCTIONS ===========================*/
70
71#define vsf_arch_wakeup()
72
73/*============================ TYPES =========================================*/
74
90#ifndef __VSF_ARCH_PRIO_DEFINED
91# define __VSF_ARCH_PRIO_DEFINED
92typedef int vsf_arch_prio_t;
93#endif
94
95#if VSF_SYSTIMER_CFG_IMPL_MODE != VSF_SYSTIMER_CFG_IMPL_NONE
96# if VSF_SYSTIMER_CFG_IMPL_MODE == VSF_SYSTIMER_IMPL_TICK_MODE
99# else
101# endif
102#endif
104
105/*============================ GLOBAL VARIABLES ==============================*/
106/*============================ LOCAL VARIABLES ===============================*/
107/*============================ PROTOTYPES ====================================*/
108
109// interrupt control is implemented as WEAK functions in rv_generic.c
110// (mstatus.MIE based); a chip with a BASEPRI-style threshold register
111// (eg: Qingke PFIC ITHRESDR) can provide strong overrides in its driver,
112// same as cortex_m_generic.c
113
114static VSF_CAL_ALWAYS_INLINE void vsf_arch_sleep(uint_fast32_t mode)
115{
116 __asm volatile("wfi" : :);
117}
118
119static VSF_CAL_ALWAYS_INLINE void vsf_arch_set_stack(uintptr_t stack, uint32_t stack_size)
120{
121 __asm volatile("mv sp, %0" : : "r"(stack) : );
122}
123
124static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_stack(void)
125{
126 uintptr_t result;
127 __asm volatile("mv %0, sp" : "=r"(result) : );
128 return result;
129}
130
131// x3(gp) is used as the thread register(eg: GOT base of applets in vsf linux,
132// the ARM counterpart is r9). enabled by the BOARD config
133// (VSF_ARCH_USE_THREAD_REG, see board/<chip>/vsf_board_cfg.h), and unlike
134// ARM r9 it has toolchain side effects, ALL required:
135// 1. firmware built with -mno-relax -msmall-data-limit=0, so gp is never
136// used for small-data addressing and stays owned by the thread register;
137// 2. applets with GOT accesses rebound to gp at link time(patched binutils
138// or a post-link rewriter like elfpatch_rv.py), see gnuriscvemb.cmake;
139// 3. the gp-aware setjmp/longjmp injected below(standard RV setjmp does
140// not save gp)
141#if VSF_ARCH_USE_THREAD_REG == ENABLED && !defined(__cplusplus)
142static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_set_thread_reg(uintptr_t value)
143{
144 uintptr_t result;
145 __asm volatile("mv %0, gp" : "=r"(result) : );
146 __asm volatile("mv gp, %0" : : "r"(value));
147 return result;
148}
149static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_thread_reg(void)
150{
151 uintptr_t result;
152 __asm volatile("mv %0, gp" : "=r"(result) : );
153 return result;
154}
155
156// thread context switches rely on setjmp/longjmp, and the standard RISC-V
157// setjmp does not save gp(x3) - inject the gp-aware implementation from
158// rv_setjmp.S so that the thread register is preserved across switches.
159// NOTE: prototypes come from simple_libc setjmp.h(jmp_buf typed)
160# ifndef VSF_ARCH_SETJMP
161# define VSF_ARCH_SETJMP vsf_arch_rv_setjmp
162# endif
163# ifndef VSF_ARCH_LONGJMP
164# define VSF_ARCH_LONGJMP vsf_arch_rv_longjmp
165# endif
166#endif
167
168#if VSF_ARCH_CFG_CALLSTACK_TRACE == ENABLED
170extern uint_fast16_t vsf_arch_get_callstack(uintptr_t sp, uintptr_t *callstack, uint_fast16_t callstack_num);
171#endif
172
173#ifdef __cplusplus
174}
175#endif
176
177#endif
178/* EOF */
179
void vsf_arch_sleep(uint32_t mode)
Definition arm9_generic.c:193
uint64_t vsf_systimer_tick_t
Definition cortex_a_generic.h:70
vsf_arch_prio_t
Definition cortex_a_generic.h:85
int32_t vsf_systimer_tick_signed_t
Definition mcs51_generic.h:55
uint32_t vsf_gint_state_t
Definition rv_generic.h:103
void vsf_arch_add_text_region(vsf_arch_text_region_t *region)
Definition cortex_m_generic.c:587
uint_fast16_t vsf_arch_get_callstack(uintptr_t sp, uintptr_t *callstack, uint_fast16_t callstack_num)
Definition cortex_m_generic.c:609
uint32_t uintptr_t
Definition stdint.h:38
unsigned uint32_t
Definition stdint.h:9
unsigned int uint_fast32_t
Definition stdint.h:27
unsigned long long uint64_t
Definition stdint.h:11
unsigned short uint_fast16_t
Definition stdint.h:25
int int32_t
Definition stdint.h:8
Definition vsf_arch_abstraction.h:67
vk_av_control_value_t value
Definition vsf_audio.h:171
Generated from commit: vsfteam/vsf@4327394