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_PRIORITY_DEFINED__
91# define __VSF_ARCH_PRIORITY_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
109static VSF_CAL_ALWAYS_INLINE vsf_gint_state_t vsf_get_interrupt(void)
110{
111 vsf_gint_state_t result;
112 __asm volatile("csrr %0, mstatus" : "=r"(result) : );
113 return result;
114}
115
116static VSF_CAL_ALWAYS_INLINE vsf_gint_state_t vsf_set_interrupt(vsf_gint_state_t level)
117{
118 vsf_gint_state_t result;
119 __asm volatile("csrrs %0, mstatus, %1" : "=r"(result) : "r"(level));
120 return result & 8;
121}
122
123static VSF_CAL_ALWAYS_INLINE vsf_gint_state_t vsf_disable_interrupt(void)
124{
125 vsf_gint_state_t result;
126 __asm volatile("csrrci %0, mstatus, 8" : "=r"(result) :);
127 return result & 8;
128}
129
130static VSF_CAL_ALWAYS_INLINE vsf_gint_state_t vsf_enable_interrupt(void)
131{
132 vsf_gint_state_t result;
133 __asm volatile("csrrsi %0, mstatus, 8" : "=r"(result) :);
134 return result & 8;
135}
136
137static VSF_CAL_ALWAYS_INLINE void vsf_arch_sleep(uint_fast32_t mode)
138{
139 __asm volatile("wfi" : :);
140}
141
142static VSF_CAL_ALWAYS_INLINE void vsf_arch_set_stack(uintptr_t stack, uint32_t stack_size)
143{
144 __asm volatile("mv sp, %0" : : "r"(stack) : );
145}
146
147static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_stack(void)
148{
149 uintptr_t result;
150 __asm volatile("mv %0, sp" : "=r"(result) : );
151 return result;
152}
153
154// x3(gp) is used as the thread register(eg: GOT base of applets in vsf linux,
155// the ARM counterpart is r9). enabled by the BOARD config
156// (VSF_ARCH_USE_THREAD_REG, see board/<chip>/vsf_board_cfg.h), and unlike
157// ARM r9 it has toolchain side effects, ALL required:
158// 1. firmware built with -mno-relax -msmall-data-limit=0, so gp is never
159// used for small-data addressing and stays owned by the thread register;
160// 2. applets with GOT accesses rebound to gp at link time(patched binutils
161// or a post-link rewriter like elfpatch_rv.py), see gnuriscvemb.cmake;
162// 3. the gp-aware setjmp/longjmp injected below(standard RV setjmp does
163// not save gp)
164#if VSF_ARCH_USE_THREAD_REG == ENABLED && !defined(__cplusplus)
165static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_set_thread_reg(uintptr_t value)
166{
167 uintptr_t result;
168 __asm volatile("mv %0, gp" : "=r"(result) : );
169 __asm volatile("mv gp, %0" : : "r"(value));
170 return result;
171}
172static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_thread_reg(void)
173{
174 uintptr_t result;
175 __asm volatile("mv %0, gp" : "=r"(result) : );
176 return result;
177}
178
179// thread context switches rely on setjmp/longjmp, and the standard RISC-V
180// setjmp does not save gp(x3) - inject the gp-aware implementation from
181// rv_setjmp.S so that the thread register is preserved across switches.
182// NOTE: prototypes come from simple_libc setjmp.h(jmp_buf typed)
183# ifndef VSF_ARCH_SETJMP
184# define VSF_ARCH_SETJMP vsf_arch_rv_setjmp
185# endif
186# ifndef VSF_ARCH_LONGJMP
187# define VSF_ARCH_LONGJMP vsf_arch_rv_longjmp
188# endif
189#endif
190
191#if VSF_ARCH_CFG_CALLSTACK_TRACE == ENABLED
194extern uint_fast16_t vsf_arch_get_callstack(uintptr_t sp, uintptr_t *callstack, uint_fast16_t callstack_num);
195#endif
196
197#ifdef __cplusplus
198}
199#endif
200
201#endif
202/* EOF */
203
vsf_gint_state_t vsf_disable_interrupt(void)
Definition arm9_generic.c:176
vsf_gint_state_t vsf_get_interrupt(void)
Definition arm9_generic.c:164
void vsf_arch_sleep(uint32_t mode)
Definition arm9_generic.c:193
vsf_gint_state_t vsf_enable_interrupt(void)
Definition arm9_generic.c:181
vsf_gint_state_t vsf_set_interrupt(vsf_gint_state_t level)
Definition arm9_generic.c:169
bool
Definition type.h:60
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
void vsf_arch_remove_text_region(vsf_arch_text_region_t *region)
Definition cortex_m_generic.c:597
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@a5104db