1# Copyright (c) 2016 Jean-Paul Etienne <fractalclone@gmail.com>
2# Copyright (c) 2024 Antmicro <www.antmicro.com>
3# SPDX-License-Identifier: Apache-2.0
4
5menu "RISCV Options"
6	depends on RISCV
7
8config ARCH
9	string
10	default "riscv"
11
12config FLOAT_HARD
13	bool "Hard-float calling convention"
14	default y
15	depends on FPU
16	help
17	  This option enables the hard-float calling convention.
18
19choice RISCV_GP_PURPOSE
20	prompt "Purpose of the global pointer (GP) register"
21	default RISCV_GP if RISCV_SOC_HAS_GP_RELATIVE_ADDRESSING
22
23config RISCV_GP
24	bool "RISC-V global pointer relative addressing"
25	depends on RISCV_SOC_HAS_GP_RELATIVE_ADDRESSING
26	help
27	  Use global pointer relative addressing for small globals declared
28	  anywhere in the executable. It can benefit performance and reduce
29	  the code size.
30
31	  Note: To support this feature, RISC-V SoC needs to initialize
32	  global pointer at program start or earlier than any instruction
33	  using GP relative addressing.
34
35config RISCV_CURRENT_VIA_GP
36	bool "Store current thread into the global pointer (GP) register"
37	depends on MP_MAX_NUM_CPUS > 1
38	select ARCH_HAS_CUSTOM_CURRENT_IMPL
39	help
40	  Store the current thread's pointer into the global pointer (GP) register.
41	  When is enabled, calls to `arch_current_thread()` & `k_sched_current_thread_query()` will
42	  be reduced to a single register read.
43
44endchoice # RISCV_GP_PURPOSE
45
46config RISCV_ALWAYS_SWITCH_THROUGH_ECALL
47	bool "Do not use mret outside a trap handler context"
48	depends on MULTITHREADING
49	help
50	  Use mret instruction only when in a trap handler.
51	  This is for RISC-V implementations that require every mret to be
52	  balanced with an ecall. This is not required by the RISC-V spec
53	  and most people should say n here to minimize context switching
54	  overhead.
55
56choice RISCV_SMP_IPI_IMPL
57	prompt "RISC-V SMP IPI implementation"
58	depends on SMP
59	default RISCV_SMP_IPI_CLINT if DT_HAS_SIFIVE_CLINT0_ENABLED
60	default RISCV_SMP_IPI_CUSTOM
61
62config RISCV_SMP_IPI_CLINT
63	bool "CLINT-based IPI"
64	depends on DT_HAS_SIFIVE_CLINT0_ENABLED
65	help
66	  Use CLINT-based IPI implementation.
67
68config RISCV_SMP_IPI_CUSTOM
69	bool "Custom IPI implementation"
70	help
71	  Allow custom IPI implementation.
72
73	  When this is selected, the following functions must be provided:
74	   - arch_sched_directed_ipi()
75	   - arch_flush_fpu_ipi() if CONFIG_FPU_SHARING
76	   - arch_spin_relax() if CONFIG_FPU_SHARING
77	   - arch_smp_init()
78
79endchoice # RISCV_SMP_IPI_IMPL
80
81menu "RISCV Processor Options"
82
83config INCLUDE_RESET_VECTOR
84	bool "Jumps to __initialize directly"
85	help
86	  Select 'y' here to use the Zephyr provided default implementation that
87	  jumps to `__initialize` directly. Otherwise a SOC needs to provide its
88	  custom `__reset` routine.
89
90config RISCV_PRIVILEGED
91	bool
92	select ARCH_HAS_RAMFUNC_SUPPORT if XIP
93	help
94	  Option selected by SoCs implementing the RISC-V privileged ISA.
95
96config RISCV_SOC_HAS_ISR_STACKING
97	bool
98	depends on !USERSPACE
99	help
100	  Enable low-level SOC-specific hardware stacking / unstacking
101	  operations during ISR. This hidden option needs to be selected by SoC
102	  if this feature is supported.
103
104	  Some SOCs implement a mechanism for which, on interrupt handling,
105	  part of the context is automatically saved by the hardware on the
106	  stack according to a custom ESF format. The same part of the context
107	  is automatically restored by hardware on mret.
108
109	  Enabling this option requires that the SoC provides a
110	  soc_isr_stacking.h header which defines the following:
111
112	  - SOC_ISR_SW_STACKING: macro guarded by _ASMLANGUAGE called by the
113	    IRQ wrapper assembly code on ISR entry to save in the ESF the
114	    remaining part of the context not pushed already on the stack by
115	    the hardware.
116
117	  - SOC_ISR_SW_UNSTACKING: macro guarded by _ASMLANGUAGE called by the
118	    IRQ wrapper assembly code on ISR exit to restore the part of the
119	    context from the ESF that won't be restored by hardware on mret.
120
121	  - SOC_ISR_STACKING_ESF_DECLARE: structure declaration for the ESF
122	    guarded by !_ASMLANGUAGE. The ESF should be defined to account for
123	    the hardware stacked registers in the proper order as they are
124	    saved on the stack by the hardware, and the registers saved by the
125	    software macros. The structure must be called 'struct arch_esf'.
126
127config RISCV_SOC_HAS_CUSTOM_IRQ_HANDLING
128	bool
129	help
130	  This allows the SoC to overwrite the irq handling. If enabled, the
131	  function __soc_handle_all_irqs has to be implemented. It shall service
132	  and clear all pending interrupts.
133
134config RISCV_SOC_HAS_CUSTOM_IRQ_LOCK_OPS
135	bool
136	help
137	  Hidden option to allow SoC to overwrite arch_irq_lock(),
138	  arch_irq_unlock() and arch_irq_unlocked() functions with
139	  platform-specific versions named z_soc_irq_lock(), z_soc_irq_unlock()
140	  and z_soc_irq_unlocked().
141
142	  Enable this hidden option and specialize the z_soc_* functions when
143	  the RISC-V SoC needs to do something different and more than reading and
144	  writing the mstatus register to lock and unlock the IRQs.
145
146config RISCV_SOC_HAS_CUSTOM_SYS_IO
147	bool
148	help
149	  Hidden option to allow SoC to overwrite sys_read*(), sys_write*() functions with
150	  platform-specific versions named z_soc_sys_read*() and z_soc_sys_write*().
151
152	  Enable this hidden option and specialize the z_soc_* functions when
153	  the RISC-V SoC needs to do something different and more than reading and
154	  writing the registers.
155
156config RISCV_SOC_HAS_GP_RELATIVE_ADDRESSING
157	bool
158	help
159	  Selected when SoC has implemented the initialization of global pointer (GP)
160	  at program start, or earlier than any instruction using GP relative addressing.
161
162config RISCV_SOC_CONTEXT_SAVE
163	bool "SOC-based context saving in IRQ handlers"
164	select RISCV_SOC_OFFSETS
165	help
166	  Enable low-level SOC-specific context management, for SOCs
167	  with extra state that must be saved when entering an
168	  interrupt/exception, and restored on exit. If unsure, leave
169	  this at the default value.
170
171	  Enabling this option requires that the SoC provide a
172	  soc_context.h header which defines the following macros:
173
174	  - SOC_ESF_MEMBERS: structure component declarations to
175	    allocate space for. The last such declaration should not
176	    end in a semicolon, for portability. The generic RISC-V
177	    architecture code will allocate space for these members in
178	    a "struct soc_esf" type (typedefed to soc_esf_t), which will
179	    be available if arch.h is included.
180
181	  - SOC_ESF_INIT: structure contents initializer for struct soc_esf
182	    state. The last initialized member should not end in a comma.
183
184	  The generic architecture IRQ wrapper will also call
185	  \_\_soc_save_context and \_\_soc_restore_context routines at
186	  ISR entry and exit, respectively. These should typically
187	  be implemented in assembly. If they were C functions, they
188	  would have these signatures:
189
190	  ``void __soc_save_context(soc_esf_t *state);``
191
192	  ``void __soc_restore_context(soc_esf_t *state);``
193
194	  The calls obey standard calling conventions; i.e., the state
195	  pointer address is in a0, and ra contains the return address.
196
197config RISCV_SOC_OFFSETS
198	bool "SOC-based offsets"
199	help
200	  Enabling this option requires that the SoC provide a soc_offsets.h
201	  header which defines the following macros:
202
203	  - GEN_SOC_OFFSET_SYMS(): a macro which expands to
204	    GEN_OFFSET_SYM(soc_esf_t, soc_specific_member) calls
205	    to ensure offset macros for SOC_ESF_MEMBERS are defined
206	    in offsets.h. The last one should not end in a semicolon.
207	    See gen_offset.h for more details.
208
209config RISCV_HAS_PLIC
210	bool
211	depends on RISCV_PRIVILEGED
212	help
213	  Does the SOC provide support for a Platform Level Interrupt Controller (PLIC).
214
215config RISCV_HAS_CLIC
216	bool
217	depends on RISCV_PRIVILEGED
218	help
219	  Does the SOC provide support for a Core-Local Interrupt Controller (CLIC).
220
221config RISCV_SOC_EXCEPTION_FROM_IRQ
222	bool
223	help
224	  Option selected by SoCs that require a custom mechanism to check if
225	  an exception is the result of an interrupt or not. If selected,
226	  __soc_is_irq() needs to be implemented by the SoC.
227
228config RISCV_SOC_INTERRUPT_INIT
229	bool "SOC-based interrupt initialization"
230	help
231	  Enable SOC-based interrupt initialization
232	  (call soc_interrupt_init, within _IntLibInit when enabled)
233
234config RISCV_MCAUSE_EXCEPTION_MASK
235	hex
236	default 0x7FFFFFFFFFFFFFFF if 64BIT
237	default 0x7FFFFFFF
238	help
239	  Specify the bits to use for exception code in mcause register.
240
241config RISCV_GENERIC_TOOLCHAIN
242	bool "Compile using generic riscv32 toolchain"
243	default y
244	help
245	  Compile using generic riscv32 toolchain.
246	  Allow SOCs that have custom extended riscv ISA to still
247	  compile with generic riscv32 toolchain.
248
249config GEN_ISR_TABLES
250	default y
251
252config GEN_IRQ_VECTOR_TABLE
253	default n
254
255config RISCV_RESERVED_IRQ_ISR_TABLES_OFFSET
256	int
257	default 0
258	depends on GEN_ISR_TABLES
259	help
260	  On some RISCV platform the first interrupt vectors are primarly
261	  intended for inter-hart interrupt signaling and so retained for that
262	  purpose and not available. When this option is set, all the IRQ
263	  vectors are shifted by this offset value when installed into the
264	  software ISR table and the IRQ vector table. CONFIG_NUM_IRQS must be
265	  properly sized to take into account this offset. This is a hidden
266	  option which needs to be set per architecture and left alone.
267
268config NUM_IRQS
269	int
270
271config RV_BOOT_HART
272	int "Starting HART ID"
273	default 0
274	help
275	  This option sets the starting HART ID for the SMP core.
276	  For RISC-V systems such as MPFS and FU540 this would be set to 1 to
277	  skip the E51 HART 0 as it is not usable in SMP configurations.
278
279config RISCV_HART_MASK
280	int
281	default -1
282	help
283	  Configures the mask for the HART ID.
284	  For RISC-V systems with HART ID starting from non-zero value,
285	  i.e. 128, 129, ..(0x80, 8x81, ..), this can be configured to 63 (0x7f)
286	  such that we can extract the bits that start from 0.
287
288config EXTRA_EXCEPTION_INFO
289	bool "Collect extra exception info"
290	depends on EXCEPTION_DEBUG
291	help
292	  This option enables the collection of extra information, such as
293	  register state, when a fault occurs. This information can be useful
294	  to collect for post-mortem analysis and debug of issues.
295
296config RISCV_PMP
297	bool "RISC-V PMP Support"
298	select THREAD_STACK_INFO
299	select CPU_HAS_MPU
300	select ARCH_HAS_USERSPACE
301	select ARCH_HAS_STACK_PROTECTION
302	select MPU
303	select SRAM_REGION_PERMISSIONS
304	select ARCH_MEM_DOMAIN_SYNCHRONOUS_API if USERSPACE
305	select ARCH_MEM_DOMAIN_DATA if USERSPACE
306	select THREAD_LOCAL_STORAGE if USERSPACE
307	select ARCH_MEM_DOMAIN_SUPPORTS_ISOLATED_STACKS
308	select MEM_DOMAIN_ISOLATED_STACKS
309	help
310	  MCU implements Physical Memory Protection.
311
312if RISCV_PMP
313
314config PMP_SLOTS
315	int "Number of PMP slots"
316	default 8
317	help
318	  This is the number of PMP entries implemented by the hardware.
319	  Typical values are 8 or 16.
320
321config PMP_NO_TOR
322	bool
323	help
324	  Set this if TOR (Top Of Range) mode is not supported.
325
326config PMP_NO_NA4
327	bool
328	help
329	  Set this if NA4 (Naturally Aligned 4-byte) mode is not supported.
330
331config PMP_NO_NAPOT
332	bool
333	help
334	  Set this if NAPOT (Naturally Aligned Power Of Two) is not supported.
335
336config PMP_POWER_OF_TWO_ALIGNMENT
337	bool "Enforce power-of-two alignment on PMP memory areas" if !PMP_NO_TOR
338	default y if TEST_USERSPACE
339	default y if (PMP_SLOTS = 8)
340	default y if PMP_NO_TOR
341	select MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT
342	select GEN_PRIV_STACKS
343	help
344	  This option reduces the PMP slot usage but increases
345	  memory consumption. Useful when enabling userspace mode with
346	  many memory domains and/or few PMP slots available.
347
348config PMP_GRANULARITY
349	int "The granularity of PMP address matching"
350	default 8 if (PMP_NO_TOR && PMP_NO_NA4)
351	default 4
352	help
353	  The granularity must be a power of 2 greater than or equal to 4
354	  (ie 4, 8, 16, ...), but if neither TOR mode nor NA4 mode is
355	  supported, the minimum granularity is 8.
356
357endif #RISCV_PMP
358
359config PMP_STACK_GUARD
360	def_bool y
361	depends on HW_STACK_PROTECTION
362
363config PMP_STACK_GUARD_MIN_SIZE
364	int "Stack Guard area size"
365	depends on PMP_STACK_GUARD
366	default 1024 if 64BIT
367	default 512
368	help
369	  The Hardware Stack Protection implements a guard area at the bottom
370	  of the stack using the PMP to catch stack overflows by marking that
371	  guard area not accessible.
372
373	  This is the size of the guard area. This should be large enough to
374	  catch any sudden jump in stack pointer decrement, plus some
375	  wiggle room to accommodate the eventual overflow exception
376	  stack usage.
377
378# Implement the null pointer detection using the Physical Memory Protection
379# (PMP) Unit.
380config NULL_POINTER_EXCEPTION_DETECTION_PMP
381	bool "Use PMP for null pointer exception detection"
382	depends on RISCV_PMP
383	help
384	  Null pointer dereference detection implemented
385	  using PMP functionality.
386
387if NULL_POINTER_EXCEPTION_DETECTION_PMP
388
389config NULL_POINTER_EXCEPTION_REGION_SIZE
390	hex "Inaccessible region to implement null pointer detection"
391	default 0x10
392	help
393	  Use a PMP slot to make region (starting at address 0x0) inaccessible for
394	  detecting null pointer dereferencing (raising a CPU access fault).
395	  Minimum is 4 bytes.
396
397endif # NULL_POINTER_EXCEPTION_DETECTION_PMP
398
399config RISCV_IMPRECISE_FPU_STATE_TRACKING
400	bool "Imprecise implementation of FPU state tracking"
401	depends on FPU
402	help
403	  According to the RISC-V Instruction Set Manual: Volume II, Version 20240411
404	  (Section 3.1.6.6), some implementations may choose to track the dirtiness of
405	  the floating-point register state imprecisely by reporting the state to be
406	  dirty even when it has not been modified. This option reflects that.
407
408endmenu
409
410config MAIN_STACK_SIZE
411	default 4096 if 64BIT
412	default 2048 if PMP_STACK_GUARD
413
414config TEST_EXTRA_STACK_SIZE
415	default 1536
416
417config CMSIS_THREAD_MAX_STACK_SIZE
418	default 1024 if 64BIT
419
420config CMSIS_V2_THREAD_MAX_STACK_SIZE
421	default 1024 if 64BIT
422
423config ARCH_IRQ_VECTOR_TABLE_ALIGN
424	default 256
425
426config RISCV_TRAP_HANDLER_ALIGNMENT
427	int "Alignment of RISC-V trap handler in bytes"
428	default 64 if RISCV_HAS_CLIC
429	default 4
430	help
431	  This value configures the alignment of RISC-V trap handling
432	  code. The requirement for a particular alignment arises from
433	  the format of MTVEC register which is RISC-V platform-specific.
434	  The minimum alignment is 4 bytes according to the Spec.
435
436config GEN_IRQ_VECTOR_TABLE
437	select RISCV_VECTORED_MODE if RISCV_PRIVILEGED
438
439config ARCH_HAS_SINGLE_THREAD_SUPPORT
440	default y if !SMP
441
442config ARCH_HAS_STACKWALK
443	bool
444	default y
445	imply THREAD_STACK_INFO
446	help
447	  Internal config to indicate that the arch_stack_walk() API is implemented
448	  and it can be enabled.
449
450rsource "Kconfig.isa"
451
452endmenu
453