system_evalsoc.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /*
  2. * Copyright (c) 2009-2018 Arm Limited. All rights reserved.
  3. * Copyright (c) 2019 Nuclei Limited. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the License); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /******************************************************************************
  20. * @file system_evalsoc.c
  21. * @brief NMSIS Nuclei Core Device Peripheral Access Layer Source File for
  22. * Nuclei Eval SoC which support Nuclei N/NX class cores
  23. * @version V1.00
  24. * @date 22. Nov 2019
  25. ******************************************************************************/
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include "nuclei_sdk_hal.h"
  29. // TODO: This implementation contains many extra code controlled by macros
  30. // which may be not suitable for your SoC, you can directly remove the code
  31. /*----------------------------------------------------------------------------
  32. Define clocks
  33. *----------------------------------------------------------------------------*/
  34. /* ToDo: add here your necessary defines for device initialization
  35. following is an example for different system frequencies */
  36. #ifndef SYSTEM_CLOCK
  37. #define SYSTEM_CLOCK (16000000UL)
  38. #endif
  39. /**
  40. * \defgroup NMSIS_Core_SystemConfig System Device Configuration
  41. * \brief Functions for system and clock setup available in system_<device>.c.
  42. * \details
  43. * Nuclei provides a template file **system_Device.c** that must be adapted by
  44. * the silicon vendor to match their actual device. As a <b>minimum requirement</b>,
  45. * this file must provide:
  46. * - A device-specific system configuration function, \ref SystemInit.
  47. * - Global c library \ref _premain_init and \ref _postmain_fini functions called right before calling main function.
  48. * - A global variable that contains the system frequency, \ref SystemCoreClock.
  49. * - A global eclic configuration initialization, \ref ECLIC_Init.
  50. * - A global exception and trap configuration initialization, \ref Trap_Init and \ref Exception_Init.
  51. * - Vendor customized interrupt, exception and nmi handling code, see \ref NMSIS_Core_IntExcNMI_Handling
  52. *
  53. * The file configures the device and, typically, initializes the oscillator (PLL) that is part
  54. * of the microcontroller device. This file might export other functions or variables that provide
  55. * a more flexible configuration of the microcontroller system.
  56. *
  57. * And this file also provided common interrupt, exception and NMI exception handling framework template,
  58. * Silicon vendor can customize these template code as they want.
  59. *
  60. * \note Please pay special attention to the static variable \c SystemCoreClock. This variable might be
  61. * used throughout the whole system initialization and runtime to calculate frequency/time related values.
  62. * Thus one must assure that the variable always reflects the actual system clock speed.
  63. *
  64. * \attention
  65. * Be aware that a value stored to \c SystemCoreClock during low level initialization (i.e. \c SystemInit()) might get
  66. * overwritten by C libray startup code and/or .bss section initialization.
  67. * Thus its highly recommended to call \ref SystemCoreClockUpdate at the beginning of the user \c main() routine.
  68. *
  69. * @{
  70. */
  71. #if defined(__TEE_PRESENT) && (__TEE_PRESENT == 1)
  72. /* for the following variables, see intexc_evalsoc.S and intexc_evalsoc_s.S */
  73. /** default entry for s-mode non-vector irq entry */
  74. extern void irq_entry_s(void);
  75. /** default entry for s-mode exception entry */
  76. extern void exc_entry_s(void);
  77. /** default eclic interrupt or exception interrupt handler */
  78. extern void default_intexc_handler(void);
  79. #ifndef __ICCRISCV__
  80. /** eclic s-mode software interrupt handler in eclic mode */
  81. extern void eclic_ssip_handler(void) __WEAK;
  82. /** eclic s-mode time interrupt handler in eclic mode */
  83. extern void eclic_stip_handler(void) __WEAK;
  84. #else
  85. /** eclic s-mode software interrupt handler in eclic mode */
  86. __WEAK __SUPERVISOR_INTERRUPT void eclic_ssip_handler(void)
  87. {
  88. }
  89. /** eclic s-mode time interrupt handler in eclic mode */
  90. __WEAK __SUPERVISOR_INTERRUPT __WEAK void eclic_stip_handler(void)
  91. {
  92. }
  93. #endif
  94. /* default s-mode exception handler, which user can modify it at your need */
  95. static void system_default_exception_handler_s(unsigned long scause, unsigned long sp);
  96. #ifndef __ICCRISCV__
  97. #define __SMODE_VECTOR_ATTR __attribute__((section (".text.vtable_s"), aligned(512)))
  98. #else
  99. #define __SMODE_VECTOR_ATTR __attribute__((section (".sintvec"), aligned(512)))
  100. #endif
  101. // TODO: change the aligned(512) to match stvt alignment requirement according to your eclic max interrupt number
  102. // TODO: place your interrupt handler into this vector table, important if your vector table is in flash
  103. /**
  104. * \var unsigned long vector_table_s[SOC_INT_MAX]
  105. * \brief vector interrupt storing ISRs for supervisor mode
  106. * \details
  107. * vector_table_s is hold by stvt register, the address must align according
  108. * to actual interrupt numbers as below, now align to 512 bytes considering we put up to 128 interrupts here
  109. * alignment must comply to table below if you increase or decrease vector interrupt number
  110. * interrupt number alignment
  111. * 0 to 16 64-byte
  112. * 17 to 32 128-byte
  113. * 33 to 64 256-byte
  114. * 65 to 128 512-byte
  115. * 129 to 256 1KB
  116. * 257 to 512 2KB
  117. * 513 to 1024 4KB
  118. */
  119. const unsigned long vector_table_s[SOC_INT_MAX] __SMODE_VECTOR_ATTR =
  120. {
  121. (unsigned long)(default_intexc_handler), /* 0: Reserved */
  122. (unsigned long)(default_intexc_handler), /* 1: Reserved */
  123. (unsigned long)(default_intexc_handler), /* 2: Reserved */
  124. (unsigned long)(eclic_ssip_handler), /* 3: supervisor software interrupt in eclic mode */
  125. (unsigned long)(default_intexc_handler), /* 4: Reserved */
  126. (unsigned long)(default_intexc_handler), /* 5: Reserved */
  127. (unsigned long)(default_intexc_handler), /* 6: Reserved */
  128. (unsigned long)(eclic_stip_handler), /* 7: supervisor timer interrupt in eclic mode */
  129. (unsigned long)(default_intexc_handler), /* 8: Reserved */
  130. (unsigned long)(default_intexc_handler), /* 9: Reserved */
  131. (unsigned long)(default_intexc_handler), /* 10: Reserved */
  132. (unsigned long)(default_intexc_handler), /* 11: Reserved */
  133. (unsigned long)(default_intexc_handler), /* 12: Reserved */
  134. (unsigned long)(default_intexc_handler), /* 13: Reserved */
  135. (unsigned long)(default_intexc_handler), /* 14: Reserved */
  136. (unsigned long)(default_intexc_handler), /* 15: Reserved */
  137. (unsigned long)(default_intexc_handler), /* 16: Reserved */
  138. (unsigned long)(default_intexc_handler), /* 17: Reserved */
  139. (unsigned long)(default_intexc_handler), /* 18: Reserved */
  140. /* TODO other external interrupt handler don't provide default value, if you want to provide default value, please do it by yourself */
  141. };
  142. #endif
  143. /*----------------------------------------------------------------------------
  144. System Core Clock Variable
  145. *----------------------------------------------------------------------------*/
  146. /* ToDo: initialize SystemCoreClock with the system core clock frequency value
  147. achieved after system intitialization.
  148. This means system core clock frequency after call to SystemInit() */
  149. /**
  150. * \brief Variable to hold the system core clock value
  151. * \details
  152. * Holds the system core clock, which is the system clock frequency supplied to the SysTick
  153. * timer and the processor core clock. This variable can be used by debuggers to query the
  154. * frequency of the debug timer or to configure the trace clock speed.
  155. *
  156. * \attention
  157. * Compilers must be configured to avoid removing this variable in case the application
  158. * program is not using it. Debugging systems require the variable to be physically
  159. * present in memory so that it can be examined to configure the debugger.
  160. */
  161. volatile uint32_t SystemCoreClock = SYSTEM_CLOCK; /* System Clock Frequency (Core Clock) */
  162. /*----------------------------------------------------------------------------
  163. Clock functions
  164. *----------------------------------------------------------------------------*/
  165. /**
  166. * \brief Function to update the variable \ref SystemCoreClock
  167. * \details
  168. * Updates the variable \ref SystemCoreClock and must be called whenever the core clock is changed
  169. * during program execution. The function evaluates the clock register settings and calculates
  170. * the current core clock.
  171. */
  172. void SystemCoreClockUpdate(void) /* Get Core Clock Frequency */
  173. {
  174. /* ToDo: add code to calculate the system frequency based upon the current
  175. * register settings.
  176. * Note: This function can be used to retrieve the system core clock frequeny
  177. * after user changed register settings.
  178. */
  179. }
  180. /**
  181. * \brief Function to Initialize the system.
  182. * \details
  183. * Initializes the microcontroller system. Typically, this function configures the
  184. * oscillator (PLL) that is part of the microcontroller device. For systems
  185. * with a variable clock speed, it updates the variable \ref SystemCoreClock.
  186. * SystemInit is called from the file <b>startup<i>_device</i></b>.
  187. */
  188. void SystemInit(void)
  189. {
  190. /* ToDo: add code to initialize the system
  191. * Warn: do not use global variables because this function is called before
  192. * reaching pre-main. RW section maybe overwritten afterwards.
  193. */
  194. }
  195. /**
  196. * \defgroup NMSIS_Core_IntExcNMI_Handling Interrupt and Exception and NMI Handling
  197. * \brief Functions for interrupt, exception and nmi handle available in system_<device>.c.
  198. * \details
  199. * Nuclei provide a template for interrupt, exception and NMI handling. Silicon Vendor could adapat according
  200. * to their requirement. Silicon vendor could implement interface for different exception code and
  201. * replace current implementation.
  202. *
  203. * @{
  204. */
  205. /** \brief Max exception handler number, don't include the NMI(0xFFF) one */
  206. #define MAX_SYSTEM_EXCEPTION_NUM 26
  207. /**
  208. * \brief Store the exception handlers for each exception ID
  209. * \note
  210. * - This SystemExceptionHandlers are used to store all the handlers for all
  211. * the exception codes Nuclei N/NX core provided.
  212. * - Exception code 0 - 25, totally 26 exceptions are mapped to SystemExceptionHandlers[0:25]
  213. * - Exception for NMI is also re-routed to exception handling(exception code 0xFFF) in startup code configuration, the handler itself is mapped to SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM]
  214. */
  215. static unsigned long SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM + 1];
  216. #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
  217. static unsigned long SystemMExtInterruptHandlers[__PLIC_INTNUM];
  218. #endif
  219. /**
  220. * \brief Exception Handler Function Typedef
  221. * \note
  222. * This typedef is only used internal in this system_<Device>.c file.
  223. * It is used to do type conversion for registered exception handler before calling it.
  224. */
  225. typedef void (*EXC_HANDLER)(unsigned long cause, unsigned long sp);
  226. typedef void (*INT_HANDLER)(unsigned long cause, unsigned long sp);
  227. #define SYSTEM_CORE_INTNUM 12 // 0-11 stop at machine external interrupt
  228. static unsigned long SystemCoreInterruptHandlers[SYSTEM_CORE_INTNUM];
  229. static void system_mmode_extirq_handler(unsigned long exccode, unsigned long sp);
  230. static void system_smode_extirq_handler(unsigned long exccode, unsigned long sp);
  231. static void core_interrupt_handler(unsigned long exccode, unsigned long sp);
  232. uint32_t core_exception_handler(unsigned long mcause, unsigned long sp);
  233. static INT_HANDLER system_core_interrupt_handler = NULL;
  234. /**
  235. * \brief Store the exception handlers for each exception ID in supervisor mode
  236. * \note
  237. * - This SystemExceptionHandlers_S are used to store all the handlers for all
  238. * the exception codes Nuclei N/NX core provided.
  239. * - Exception code 0 - 11, totally 12 exceptions are mapped to SystemExceptionHandlers_S[0:11]
  240. * - The NMI (Non-maskable-interrupt) cannot be trapped to the supervisor-mode or user-mode for any configuration
  241. */
  242. #if defined(__TEE_PRESENT) && (__TEE_PRESENT == 1)
  243. static unsigned long SystemExceptionHandlers_S[MAX_SYSTEM_EXCEPTION_NUM];
  244. #endif
  245. /**
  246. * \brief System Default Exception Handler
  247. * \details
  248. * This function provides a default exception and NMI handler for all exception ids.
  249. * By default, It will just print some information for debug, Vendor can customize it according to its requirements.
  250. * \param [in] mcause code indicating the reason that caused the trap in machine mode
  251. * \param [in] sp stack pointer
  252. */
  253. static void system_default_exception_handler(unsigned long mcause, unsigned long sp)
  254. {
  255. #if defined(CODESIZE) && (CODESIZE == 1)
  256. #else
  257. NSDK_DEBUG("MCAUSE : 0x%lx\r\n", mcause);
  258. NSDK_DEBUG("MDCAUSE: 0x%lx\r\n", __RV_CSR_READ(CSR_MDCAUSE));
  259. NSDK_DEBUG("MEPC : 0x%lx\r\n", __RV_CSR_READ(CSR_MEPC));
  260. NSDK_DEBUG("MTVAL : 0x%lx\r\n", __RV_CSR_READ(CSR_MTVAL));
  261. NSDK_DEBUG("HARTID : %u\r\n", (unsigned int)__get_hart_id());
  262. Exception_DumpFrame(sp, PRV_M);
  263. #if defined(SIMULATION_MODE)
  264. // directly exit if in SIMULATION
  265. extern void simulation_exit(int status);
  266. simulation_exit(1);
  267. #else
  268. while (1);
  269. #endif
  270. #endif
  271. }
  272. /**
  273. * \brief System Default Interrupt Handler for CLINT/PLIC Interrupt Mode
  274. * \details
  275. * This function provided a default interrupt handling code for all interrupt ids.
  276. */
  277. static void system_default_interrupt_handler(unsigned long mcause, unsigned long sp)
  278. {
  279. #if defined(CODESIZE) && (CODESIZE == 1)
  280. #else
  281. NSDK_DEBUG("Trap in Interrupt\r\n");
  282. NSDK_DEBUG("MCAUSE: 0x%lx\r\n", mcause);
  283. NSDK_DEBUG("MEPC : 0x%lx\r\n", __RV_CSR_READ(CSR_MEPC));
  284. NSDK_DEBUG("MTVAL : 0x%lx\r\n", __RV_CSR_READ(CSR_MBADADDR));
  285. #endif
  286. }
  287. /**
  288. * \brief Initialize all the default core exception handlers
  289. * \details
  290. * The core exception handler for each exception id will be initialized to \ref system_default_exception_handler.
  291. * \note
  292. * Called in \ref _init function, used to initialize default exception handlers for all exception IDs
  293. * SystemExceptionHandlers contains NMI, but SystemExceptionHandlers_S not, because NMI can't be delegated to S-mode.
  294. */
  295. static void Exception_Init(void)
  296. {
  297. #if defined(CODESIZE) && (CODESIZE == 1)
  298. // TODO when CODESIZE macro is defined
  299. // the exception handler table for m/s mode will not be initialized
  300. // since all the exception handlers will not be classified, and just
  301. // goto core_exception_handler or core_exception_handler_s for m/s exception
  302. #else
  303. for (int i = 0; i < MAX_SYSTEM_EXCEPTION_NUM; i++) {
  304. SystemExceptionHandlers[i] = (unsigned long)system_default_exception_handler;
  305. #if defined(__TEE_PRESENT) && (__TEE_PRESENT == 1)
  306. SystemExceptionHandlers_S[i] = (unsigned long)system_default_exception_handler_s;
  307. #endif
  308. }
  309. SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM] = (unsigned long)system_default_exception_handler;
  310. #endif
  311. }
  312. /**
  313. * \brief Register an core interrupt handler for core interrupt number
  314. * \details
  315. * * For irqn <= 10, it will be registered into SystemCoreInterruptHandlers[irqn-1].
  316. * \param irqn See \ref IRQn
  317. * \param int_handler The core interrupt handler for this interrupt code irqn
  318. * \remarks
  319. * You can only use it when you are not in ECLIC interrupt mode.
  320. */
  321. void Interrupt_Register_CoreIRQ(uint32_t irqn, unsigned long int_handler)
  322. {
  323. if ((irqn < SYSTEM_CORE_INTNUM) && (irqn >= 0)) {
  324. SystemCoreInterruptHandlers[irqn] = int_handler;
  325. }
  326. }
  327. /**
  328. * \brief Register an external interrupt handler for plic external interrupt number
  329. * \details
  330. * * For irqn <= \ref __PLIC_INTNUM, it will be registered into SystemMExtInterruptHandlers[irqn-1].
  331. * \param irqn See \ref IRQn
  332. * \param int_handler The external interrupt handler for this interrupt code irqn
  333. * \remarks
  334. * You can only use it when you are in PLIC interrupt mode.
  335. */
  336. void Interrupt_Register_ExtIRQ(uint32_t irqn, unsigned long int_handler)
  337. {
  338. #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
  339. if ((irqn < __PLIC_INTNUM) && (irqn >= 0)) {
  340. SystemMExtInterruptHandlers[irqn] = int_handler;
  341. }
  342. #endif
  343. }
  344. /**
  345. * \brief Get an core interrupt handler for core interrupt number
  346. * \param irqn See \ref IRQn
  347. * \return
  348. * The core interrupt handler for this interrupt code irqn
  349. * \remarks
  350. * You can only use it when you are not in ECLIC interrupt mode.
  351. */
  352. unsigned long Interrupt_Get_CoreIRQ(uint32_t irqn)
  353. {
  354. if ((irqn < SYSTEM_CORE_INTNUM) && (irqn >= 0)) {
  355. return SystemCoreInterruptHandlers[irqn];
  356. }
  357. return 0;
  358. }
  359. /**
  360. * \brief Get an external interrupt handler for external interrupt number
  361. * \param irqn See \ref IRQn
  362. * \return
  363. * The external interrupt handler for this interrupt code irqn
  364. * \remarks
  365. * You can only use it when you are in PLIC interrupt mode.
  366. */
  367. unsigned long Interrupt_Get_ExtIRQ(uint32_t irqn)
  368. {
  369. #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
  370. if ((irqn < __PLIC_INTNUM) && (irqn >= 0)) {
  371. return SystemMExtInterruptHandlers[irqn];
  372. }
  373. #endif
  374. return 0;
  375. }
  376. /**
  377. * \brief Dump Exception Frame
  378. * \details
  379. * This function provided feature to dump exception frame stored in stack.
  380. * \param [in] sp stackpoint
  381. * \param [in] mode privileged mode to decide whether to dump msubm CSR
  382. */
  383. void Exception_DumpFrame(unsigned long sp, uint8_t mode)
  384. {
  385. #if defined(CODESIZE) && (CODESIZE == 1)
  386. #else
  387. EXC_Frame_Type *exc_frame = (EXC_Frame_Type *)sp;
  388. #ifndef __riscv_32e
  389. NSDK_DEBUG("ra: 0x%lx, tp: 0x%lx, t0: 0x%lx, t1: 0x%lx, t2: 0x%lx, t3: 0x%lx, t4: 0x%lx, t5: 0x%lx, t6: 0x%lx\n" \
  390. "a0: 0x%lx, a1: 0x%lx, a2: 0x%lx, a3: 0x%lx, a4: 0x%lx, a5: 0x%lx, a6: 0x%lx, a7: 0x%lx\n" \
  391. "cause: 0x%lx, epc: 0x%lx\n", exc_frame->ra, exc_frame->tp, exc_frame->t0, \
  392. exc_frame->t1, exc_frame->t2, exc_frame->t3, exc_frame->t4, exc_frame->t5, exc_frame->t6, \
  393. exc_frame->a0, exc_frame->a1, exc_frame->a2, exc_frame->a3, exc_frame->a4, exc_frame->a5, \
  394. exc_frame->a6, exc_frame->a7, exc_frame->cause, exc_frame->epc);
  395. #else
  396. NSDK_DEBUG("ra: 0x%lx, tp: 0x%lx, t0: 0x%lx, t1: 0x%lx, t2: 0x%lx\n" \
  397. "a0: 0x%lx, a1: 0x%lx, a2: 0x%lx, a3: 0x%lx, a4: 0x%lx, a5: 0x%lx\n" \
  398. "cause: 0x%lx, epc: 0x%lx\n", exc_frame->ra, exc_frame->tp, exc_frame->t0, \
  399. exc_frame->t1, exc_frame->t2, exc_frame->a0, exc_frame->a1, exc_frame->a2, exc_frame->a3, \
  400. exc_frame->a4, exc_frame->a5, exc_frame->cause, exc_frame->epc);
  401. #endif
  402. if (PRV_M == mode) {
  403. /* msubm is exclusive to machine mode */
  404. NSDK_DEBUG("msubm: 0x%lx\n", exc_frame->msubm);
  405. }
  406. #endif
  407. }
  408. /**
  409. * \brief Register an exception handler for exception code EXCn
  410. * \details
  411. * - For EXCn < \ref MAX_SYSTEM_EXCEPTION_NUM, it will be registered into SystemExceptionHandlers[EXCn-1].
  412. * - For EXCn == NMI_EXCn, it will be registered into SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM].
  413. * \param [in] EXCn See \ref EXCn_Type
  414. * \param [in] exc_handler The exception handler for this exception code EXCn
  415. */
  416. void Exception_Register_EXC(uint32_t EXCn, unsigned long exc_handler)
  417. {
  418. #if defined(CODESIZE) && (CODESIZE == 1)
  419. #else
  420. if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
  421. SystemExceptionHandlers[EXCn] = exc_handler;
  422. } else if (EXCn == NMI_EXCn) {
  423. SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM] = exc_handler;
  424. }
  425. #endif
  426. }
  427. /**
  428. * \brief Get current exception handler for exception code EXCn
  429. * \details
  430. * - For EXCn < \ref MAX_SYSTEM_EXCEPTION_NUM, it will return SystemExceptionHandlers[EXCn-1].
  431. * - For EXCn == NMI_EXCn, it will return SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM].
  432. * \param [in] EXCn See \ref EXCn_Type
  433. * \return Current exception handler for exception code EXCn, if not found, return 0.
  434. */
  435. unsigned long Exception_Get_EXC(uint32_t EXCn)
  436. {
  437. #if defined(CODESIZE) && (CODESIZE == 1)
  438. return 0;
  439. #else
  440. if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
  441. return SystemExceptionHandlers[EXCn];
  442. } else if (EXCn == NMI_EXCn) {
  443. return SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM];
  444. } else {
  445. return 0;
  446. }
  447. #endif
  448. }
  449. /**
  450. * \brief M-Mode external interrupt handler common entry for plic interrupt mode
  451. * \details
  452. * This function provide common entry for m-mode external interrupt for plic interrupt mode.
  453. * \param [in] exccode exception code indicating the reason that caused the trap in machine mode
  454. * \param [in] sp stack pointer
  455. */
  456. static void system_mmode_extirq_handler(unsigned long exccode, unsigned long sp)
  457. {
  458. #if defined(__PLIC_PRESENT) && __PLIC_PRESENT == 1
  459. uint32_t irqn = PLIC_ClaimInterrupt();
  460. INT_HANDLER int_handler = NULL;
  461. if (irqn < __PLIC_INTNUM) {
  462. int_handler = (INT_HANDLER)(SystemMExtInterruptHandlers[irqn]);
  463. if (int_handler != NULL) {
  464. int_handler(exccode, sp);
  465. }
  466. }
  467. PLIC_CompleteInterrupt(irqn);
  468. #endif
  469. }
  470. /**
  471. * \brief S-Mode external interrupt handler common entry for plic interrupt mode
  472. * \details
  473. * This function provide common entry for s-mode external interrupt for plic interrupt mode.
  474. * \param [in] exccode exception code indicating the reason that caused the trap in supervisor mode
  475. * \param [in] sp stack pointer
  476. */
  477. static void system_smode_extirq_handler(unsigned long exccode, unsigned long sp)
  478. {
  479. // TODO not yet implemented
  480. }
  481. /**
  482. * \brief Common Interrupt handler entry when in clint/plic mode
  483. * \details
  484. * This function provided a command entry for interrupt in clint/plic mode
  485. * \param [in] exccode Exception Code
  486. * \param [in] sp stack pointer
  487. * \remarks
  488. * - This is not used for clic interrupt mode, which is only used for clint/plic interrupt mode,
  489. * you should call \ref CLINT_Interrupt_Init or \ref PLIC_Interrupt_Init first to make sure this handler entry registered
  490. * - If you are not in eclic interrupt mode, please use please use \ref Interrupt_Register_CoreIRQ to register internal interrupt
  491. * and use \ref Interrupt_Register_ExtIRQ to register external interrupt
  492. */
  493. static void core_interrupt_handler(unsigned long exccode, unsigned long sp)
  494. {
  495. INT_HANDLER int_handler = NULL;
  496. int_handler = (INT_HANDLER)(SystemCoreInterruptHandlers[exccode]);
  497. if (int_handler != NULL) {
  498. int_handler(exccode, sp);
  499. }
  500. }
  501. /**
  502. * \brief Common NMI/Exception/Interrupt handler entry
  503. * \details
  504. * This function provided a command entry for NMI and exception. Silicon Vendor could modify
  505. * this template implementation according to requirement.
  506. * \param [in] mcause code indicating the reason that caused the trap in machine mode
  507. * \param [in] sp stack pointer
  508. * \remarks
  509. * - RISCV provided common entry for all types of exception and interrupt if not in eclic mode. This is proposed code template
  510. * for exception entry function, Silicon Vendor could modify the implementation.
  511. * - For the core_exception_handler template, we provided exception register function \ref Exception_Register_EXC
  512. * which can help developer to register your exception handler for specific exception number.
  513. * - If you are in eclic interrupt mode, please use \ref ECLIC_Register_IRQ to register both internal and external interrupt
  514. * - If you are not in eclic interrupt mode, please use please use \ref Interrupt_Register_CoreIRQ to register internal interrupt
  515. * and use \ref Interrupt_Register_ExtIRQ to register external interrupt
  516. */
  517. uint32_t core_exception_handler(unsigned long mcause, unsigned long sp)
  518. {
  519. #if defined(CODESIZE) && (CODESIZE == 1)
  520. // TODO when CODESIZE macro is defined
  521. // Exception_xxx APIs will not be used, all the m-mode exception handlers
  522. // will goto this function, and you can handle it here by yourself
  523. while (1);
  524. #else
  525. unsigned long exccode = (mcause & MCAUSE_CAUSE);
  526. EXC_HANDLER exc_handler;
  527. if (mcause & MCAUSE_INTR) {
  528. if (system_core_interrupt_handler != NULL) {
  529. system_core_interrupt_handler(exccode, sp);
  530. }
  531. } else {
  532. if (exccode < MAX_SYSTEM_EXCEPTION_NUM) {
  533. exc_handler = (EXC_HANDLER)SystemExceptionHandlers[exccode];
  534. } else if (exccode == NMI_EXCn) {
  535. exc_handler = (EXC_HANDLER)SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM];
  536. } else {
  537. exc_handler = (EXC_HANDLER)system_default_exception_handler;
  538. }
  539. if (exc_handler != NULL) {
  540. exc_handler(mcause, sp);
  541. }
  542. }
  543. return 0;
  544. #endif
  545. }
  546. #if defined(__TEE_PRESENT) && (__TEE_PRESENT == 1)
  547. /**
  548. * \brief Supervisor mode system Default Exception Handler
  549. * \details
  550. * This function provided a default supervisor mode exception and NMI handling code for all exception ids.
  551. * By default, It will just print some information for debug, Vendor can customize it according to its requirements.
  552. * \param [in] scause code indicating the reason that caused the trap in supervisor mode
  553. * \param [in] sp stack pointer
  554. */
  555. static void system_default_exception_handler_s(unsigned long scause, unsigned long sp)
  556. {
  557. #if defined(CODESIZE) && (CODESIZE == 1)
  558. #else
  559. /* TODO: Uncomment this if you have implement NSDK_DEBUG function */
  560. NSDK_DEBUG("SCAUSE : 0x%lx\r\n", scause);
  561. NSDK_DEBUG("SDCAUSE: 0x%lx\r\n", __RV_CSR_READ(CSR_SDCAUSE));
  562. NSDK_DEBUG("SEPC : 0x%lx\r\n", __RV_CSR_READ(CSR_SEPC));
  563. NSDK_DEBUG("STVAL : 0x%lx\r\n", __RV_CSR_READ(CSR_STVAL));
  564. Exception_DumpFrame(sp, PRV_S);
  565. #if defined(SIMULATION_MODE)
  566. // directly exit if in SIMULATION
  567. extern void simulation_exit(int status);
  568. simulation_exit(1);
  569. #else
  570. while (1);
  571. #endif
  572. #endif
  573. }
  574. /**
  575. * \brief Register an exception handler for exception code EXCn of supervisor mode
  576. * \details
  577. * -For EXCn < \ref MAX_SYSTEM_EXCEPTION_NUM, it will be registered into SystemExceptionHandlers_S[EXCn-1].
  578. * -For EXCn == NMI_EXCn, The NMI (Non-maskable-interrupt) cannot be trapped to the supervisor-mode or user-mode for any
  579. * configuration, so NMI won't be registered into SystemExceptionHandlers_S.
  580. * \param [in] EXCn See \ref EXCn_Type
  581. * \param [in] exc_handler The exception handler for this exception code EXCn
  582. */
  583. void Exception_Register_EXC_S(uint32_t EXCn, unsigned long exc_handler)
  584. {
  585. #if defined(CODESIZE) && (CODESIZE == 1)
  586. #else
  587. if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
  588. SystemExceptionHandlers_S[EXCn] = exc_handler;
  589. }
  590. #endif
  591. }
  592. /**
  593. * \brief Get current exception handler for exception code EXCn of supervisor mode
  594. * \details
  595. * - For EXCn < \ref MAX_SYSTEM_EXCEPTION_NUM, it will return SystemExceptionHandlers_S[EXCn-1].
  596. * \param [in] EXCn See \ref EXCn_Type
  597. * \return Current exception handler for exception code EXCn, if not found, return 0.
  598. */
  599. unsigned long Exception_Get_EXC_S(uint32_t EXCn)
  600. {
  601. #if defined(CODESIZE) && (CODESIZE == 1)
  602. return 0;
  603. #else
  604. if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
  605. return SystemExceptionHandlers[EXCn];
  606. } else {
  607. return 0;
  608. }
  609. #endif
  610. }
  611. /**
  612. * \brief common Exception handler entry of supervisor mode
  613. * \details
  614. * This function provided a supervisor mode common entry for exception. Silicon Vendor could modify
  615. * this template implementation according to requirement.
  616. * \param [in] scause code indicating the reason that caused the trap in supervisor mode
  617. * \param [in] sp stack pointer
  618. * \remarks
  619. * - RISCV provided supervisor mode common entry for all types of exception. This is proposed code template
  620. * for exception entry function, Silicon Vendor could modify the implementation.
  621. * - For the core_exception_handler_s template, we provided exception register function \ref Exception_Register_EXC_S
  622. * which can help developer to register your exception handler for specific exception number.
  623. */
  624. uint32_t core_exception_handler_s(unsigned long scause, unsigned long sp)
  625. {
  626. #if defined(CODESIZE) && (CODESIZE == 1)
  627. // TODO when CODESIZE macro is defined
  628. // Exception_xxx_S APIs will not be used, all the s-mode exception handlers
  629. // will goto this function, and you can handle it here by yourself
  630. while(1);
  631. #else
  632. uint32_t EXCn = (uint32_t)(scause & 0X00000fff);
  633. EXC_HANDLER exc_handler;
  634. if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
  635. exc_handler = (EXC_HANDLER)SystemExceptionHandlers_S[EXCn];
  636. } else {
  637. exc_handler = (EXC_HANDLER)system_default_exception_handler_s;
  638. }
  639. if (exc_handler != NULL) {
  640. exc_handler(scause, sp);
  641. }
  642. return 0;
  643. }
  644. #endif
  645. #endif
  646. /** @} */ /* End of Doxygen Group NMSIS_Core_ExceptionAndNMI */
  647. /** Banner Print for Nuclei SDK */
  648. void SystemBannerPrint(void)
  649. {
  650. #if defined(NUCLEI_BANNER) && (NUCLEI_BANNER == 1)
  651. NSDK_DEBUG("Nuclei SDK Build Time: %s, %s\r\n", __DATE__, __TIME__);
  652. #ifdef DOWNLOAD_MODE_STRING
  653. NSDK_DEBUG("Download Mode: %s\r\n", DOWNLOAD_MODE_STRING);
  654. #endif
  655. NSDK_DEBUG("CPU Frequency %u Hz\r\n", (unsigned int)SystemCoreClock);
  656. NSDK_DEBUG("CPU HartID: %u\r\n", (unsigned int)__get_hart_id());
  657. #endif
  658. }
  659. #if defined(__ECLIC_PRESENT) && (__ECLIC_PRESENT == 1)
  660. extern unsigned long vector_base[];
  661. extern void irq_entry(void);
  662. #endif
  663. extern void exc_entry(void);
  664. /**
  665. * \brief Do ECLIC Interrupt configuration
  666. * \details
  667. * This function will initialize cpu interrupt mode to eclic mode. It will
  668. * - set common non-vector entry to irq_entry
  669. * - set vector interrupt table to vector_base
  670. * - set exception entry to exc_entry
  671. * - set eclic mth to 0, and nlbits to the bigest bits it supports
  672. * - set s-mode common non-vector entry to irq_entry_s if tee present
  673. * - set s-mode vector interrupt table to vector_base_s if tee present
  674. * - set s-mode exception entry to exc_entry_s if tee present
  675. * - set eclic sth to 0 if tee present
  676. */
  677. void ECLIC_Interrupt_Init(void)
  678. {
  679. #if defined(__ECLIC_PRESENT) && (__ECLIC_PRESENT == 1)
  680. unsigned long mcfg_info;
  681. mcfg_info = __RV_CSR_READ(CSR_MCFG_INFO);
  682. if (mcfg_info & MCFG_INFO_CLIC) {
  683. /* Set ECLIC vector interrupt base address to vector_base */
  684. __RV_CSR_WRITE(CSR_MTVT, (unsigned long)vector_base);
  685. /* Set ECLIC non-vector entry to irq_entry */
  686. __RV_CSR_WRITE(CSR_MTVT2, (unsigned long)irq_entry | 0x1);
  687. /* Set as CLIC interrupt mode */
  688. __RV_CSR_WRITE(CSR_MTVEC, (unsigned long)exc_entry | 0x3);
  689. /* Global Configuration about MTH and NLBits.
  690. * TODO: Please adapt it according to your system requirement.
  691. * This function is called in _init function */
  692. ECLIC_SetMth(0);
  693. ECLIC_SetCfgNlbits(__ECLIC_INTCTLBITS);
  694. #if defined(__TEE_PRESENT) && (__TEE_PRESENT == 1)
  695. /*
  696. * Intialize ECLIC supervisor mode vector interrupt
  697. * base address stvt to vector_table_s
  698. */
  699. __RV_CSR_WRITE(CSR_STVT, (unsigned long)vector_table_s);
  700. /*
  701. * Set ECLIC supervisor mode non-vector entry to be controlled
  702. * by stvt2 CSR register.
  703. * Intialize supervisor mode ECLIC non-vector interrupt
  704. * base address stvt2 to irq_entry_s.
  705. */
  706. __RV_CSR_WRITE(CSR_STVT2, (unsigned long)irq_entry_s);
  707. __RV_CSR_SET(CSR_STVT2, 0x01);
  708. /*
  709. * Set supervisor exception entry stvec to exc_entry_s */
  710. __RV_CSR_WRITE(CSR_STVEC, (unsigned long)exc_entry_s);
  711. /* Global Configuration about STH */
  712. ECLIC_SetSth(0);
  713. #endif
  714. } else {
  715. /* Set as CLINT interrupt mode */
  716. __RV_CSR_WRITE(CSR_MTVEC, (unsigned long)exc_entry);
  717. }
  718. #endif
  719. }
  720. /**
  721. * \brief Do CLINT Interrupt configuration
  722. * \details
  723. * This function will initialize cpu interrupt mode to clint mode. It will
  724. * - Set exception/interrupt entry to exc_entry, now interrupt and exception share the same entry point
  725. * - Register interrupt handling routine system_core_interrupt_handler to core_interrupt_handler function,
  726. * which will be called in core_exception_handler function
  727. */
  728. void CLINT_Interrupt_Init(void)
  729. {
  730. /* Register core interrupt handler for clint/plic interrupt mode */
  731. system_core_interrupt_handler = core_interrupt_handler;
  732. /* Set as CLINT interrupt mode */
  733. __RV_CSR_WRITE(CSR_MTVEC, (unsigned long)exc_entry);
  734. }
  735. /**
  736. * \brief Do PLIC Interrupt configuration
  737. * \details
  738. * This function will initialize cpu interrupt mode to clint/plic mode. It will
  739. * - Initialize a software maintained SystemMExtInterruptHandlers and SystemCoreInterruptHandlers to default value
  740. * - Set exception/interrupt entry to exc_entry, now interrupt and exception share the same entry point
  741. */
  742. void PLIC_Interrupt_Init(void)
  743. {
  744. #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
  745. int i;
  746. for (i = 0; i < __PLIC_INTNUM; i++) {
  747. SystemMExtInterruptHandlers[i] = (unsigned long)system_default_interrupt_handler;
  748. }
  749. for (i = 0; i < SYSTEM_CORE_INTNUM; i++) {
  750. SystemCoreInterruptHandlers[i] = (unsigned long)system_default_interrupt_handler;
  751. }
  752. SystemCoreInterruptHandlers[9] = (unsigned long)system_smode_extirq_handler;
  753. SystemCoreInterruptHandlers[11] = (unsigned long)system_mmode_extirq_handler;
  754. #endif
  755. CLINT_Interrupt_Init();
  756. }
  757. /**
  758. * \brief initialize interrupt controller
  759. * \details
  760. * Do CPU interrupt initialization, if plic present, init it, then init eclic if present.
  761. * So if ECLIC present, the interrupt will default configured to ECLIC interrupt mode,
  762. * if you want to switch to PLIC interrupt mode, you need to call PLIC_Interrupt_Init in
  763. * you application code.
  764. *
  765. * By default, if ECLIC present, eclic interrupt mode will be set, otherwise it will be
  766. * clint/plic interrupt mode
  767. * \remarks
  768. * This function previously was ECLIC_Init, now ECLIC_Init is removed
  769. */
  770. void Interrupt_Init(void)
  771. {
  772. #if defined(CODESIZE) && (CODESIZE == 1)
  773. #else
  774. /* Set as CLINT interrupt mode */
  775. __RV_CSR_WRITE(CSR_MTVEC, (unsigned long)exc_entry);
  776. /* Init interrupt as eclic mode when ECLIC present
  777. * Otherwise will init interrupt as plic mode when PLIC present
  778. * Only initialize necessary ones to reduce initialization code size usage */
  779. #if defined(__ECLIC_PRESENT) && (__ECLIC_PRESENT == 1)
  780. ECLIC_Interrupt_Init();
  781. #elif defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
  782. PLIC_Interrupt_Init();
  783. #endif
  784. #endif
  785. }
  786. #if defined(__ECLIC_PRESENT) && (__ECLIC_PRESENT == 1)
  787. /**
  788. * \brief Initialize a specific IRQ and register the handler
  789. * \details
  790. * This function set vector mode, trigger mode and polarity, interrupt level and priority,
  791. * assign handler for specific IRQn.
  792. * \param [in] IRQn NMI interrupt handler address
  793. * \param [in] shv \ref ECLIC_NON_VECTOR_INTERRUPT means non-vector mode, and \ref ECLIC_VECTOR_INTERRUPT is vector mode
  794. * \param [in] trig_mode see \ref ECLIC_TRIGGER_Type
  795. * \param [in] lvl interupt level
  796. * \param [in] priority interrupt priority
  797. * \param [in] handler interrupt handler, if NULL, handler will not be installed
  798. * \return -1 means invalid input parameter. 0 means successful.
  799. * \remarks
  800. * - This function use to configure specific eclic interrupt and register its interrupt handler and enable its interrupt.
  801. * - If the vector table is placed in read-only section(FLASHXIP mode), handler could not be installed
  802. */
  803. int32_t ECLIC_Register_IRQ(IRQn_Type IRQn, uint8_t shv, ECLIC_TRIGGER_Type trig_mode, uint8_t lvl, uint8_t priority, void* handler)
  804. {
  805. if ((IRQn > SOC_INT_MAX) || (shv > ECLIC_VECTOR_INTERRUPT) \
  806. || (trig_mode > ECLIC_NEGTIVE_EDGE_TRIGGER)) {
  807. return -1;
  808. }
  809. /* set interrupt vector mode */
  810. ECLIC_SetShvIRQ(IRQn, shv);
  811. /* set interrupt trigger mode and polarity */
  812. ECLIC_SetTrigIRQ(IRQn, trig_mode);
  813. /* set interrupt level */
  814. ECLIC_SetLevelIRQ(IRQn, lvl);
  815. /* set interrupt priority */
  816. ECLIC_SetPriorityIRQ(IRQn, priority);
  817. if (handler != NULL) {
  818. /* set interrupt handler entry to vector table */
  819. ECLIC_SetVector(IRQn, (rv_csr_t)handler);
  820. }
  821. /* enable interrupt */
  822. ECLIC_EnableIRQ(IRQn);
  823. return 0;
  824. }
  825. #endif
  826. /**
  827. * \brief Register a riscv core interrupt and register the handler
  828. * \details
  829. * This function set interrupt handler for core interrupt
  830. * \param [in] irqn interrupt number
  831. * \param [in] handler interrupt handler, if NULL, handler will not be installed
  832. * \return -1 means invalid input parameter. 0 means successful.
  833. * \remarks
  834. * - This function use to configure riscv core interrupt and register its interrupt handler and enable its interrupt.
  835. * - You can only use it when you are not in eclic interrupt mode
  836. */
  837. int32_t Core_Register_IRQ(uint32_t irqn, void *handler)
  838. {
  839. if ((irqn > SYSTEM_CORE_INTNUM)) {
  840. return -1;
  841. }
  842. if (handler != NULL) {
  843. /* register interrupt handler entry to core handlers */
  844. Interrupt_Register_CoreIRQ(irqn, (unsigned long)handler);
  845. }
  846. switch (irqn) {
  847. case SysTimerSW_IRQn:
  848. __enable_sw_irq();
  849. break;
  850. case SysTimer_IRQn:
  851. __enable_timer_irq();
  852. break;
  853. default:
  854. break;
  855. }
  856. return 0;
  857. }
  858. #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
  859. /**
  860. * \brief Register a specific plic interrupt and register the handler
  861. * \details
  862. * This function set priority and handler for plic interrupt
  863. * \param [in] source interrupt source
  864. * \param [in] priority interrupt priority
  865. * \param [in] handler interrupt handler, if NULL, handler will not be installed
  866. * \return -1 means invalid input parameter. 0 means successful.
  867. * \remarks
  868. * - This function use to configure specific plic interrupt and register its interrupt handler and enable its interrupt.
  869. * - You can only use it when you are in plic interrupt mode
  870. */
  871. int32_t PLIC_Register_IRQ(uint32_t source, uint8_t priority, void *handler)
  872. {
  873. if ((source >= __PLIC_INTNUM)) {
  874. return -1;
  875. }
  876. /* set interrupt priority */
  877. PLIC_SetPriority(source, priority);
  878. if (handler != NULL) {
  879. /* register interrupt handler entry to external handlers */
  880. Interrupt_Register_ExtIRQ(source, (unsigned long)handler);
  881. }
  882. /* enable interrupt */
  883. PLIC_EnableInterrupt(source);
  884. __enable_ext_irq();
  885. return 0;
  886. }
  887. #endif
  888. #if defined(__TEE_PRESENT) && (__TEE_PRESENT == 1)
  889. /**
  890. * \brief Initialize a specific IRQ and register the handler for supervisor mode
  891. * \details
  892. * This function set vector mode, trigger mode and polarity, interrupt level and priority,
  893. * assign handler for specific IRQn.
  894. * \param [in] IRQn NMI interrupt handler address
  895. * \param [in] shv \ref ECLIC_NON_VECTOR_INTERRUPT means non-vector mode, and \ref ECLIC_VECTOR_INTERRUPT is vector mode
  896. * \param [in] trig_mode see \ref ECLIC_TRIGGER_Type
  897. * \param [in] lvl interupt level
  898. * \param [in] priority interrupt priority
  899. * \param [in] handler interrupt handler, if NULL, handler will not be installed
  900. * \return -1 means invalid input parameter. 0 means successful.
  901. * \remarks
  902. * - This function use to configure specific eclic S-mode interrupt and register its interrupt handler and enable its interrupt.
  903. * - If the vector table is placed in read-only section (FLASHXIP mode), handler could not be installed.
  904. */
  905. int32_t ECLIC_Register_IRQ_S(IRQn_Type IRQn, uint8_t shv, ECLIC_TRIGGER_Type trig_mode, uint8_t lvl, uint8_t priority, void* handler)
  906. {
  907. if ((IRQn > SOC_INT_MAX) || (shv > ECLIC_VECTOR_INTERRUPT) \
  908. || (trig_mode > ECLIC_NEGTIVE_EDGE_TRIGGER)) {
  909. return -1;
  910. }
  911. /* set interrupt vector mode */
  912. ECLIC_SetShvIRQ_S(IRQn, shv);
  913. /* set interrupt trigger mode and polarity */
  914. ECLIC_SetTrigIRQ_S(IRQn, trig_mode);
  915. /* set interrupt level */
  916. ECLIC_SetLevelIRQ_S(IRQn, lvl);
  917. /* set interrupt priority */
  918. ECLIC_SetPriorityIRQ_S(IRQn, priority);
  919. if (handler != NULL) {
  920. /* set interrupt handler entry to vector table */
  921. ECLIC_SetVector_S(IRQn, (rv_csr_t)handler);
  922. }
  923. /* enable interrupt */
  924. ECLIC_EnableIRQ_S(IRQn);
  925. return 0;
  926. }
  927. #endif
  928. // NOTE: FALLBACK_DEFAULT_ECLIC_BASE/FALLBACK_DEFAULT_SYSTIMER_BASE macros are removed
  929. // No longer support for cpu without iregion feature
  930. #ifndef CFG_IREGION_BASE_ADDR
  931. /** Nuclei RISC-V CPU IRegion Base Address Probed, you should avoid to use it in your application code, please use __IREGION_BASEADDR if you want */
  932. volatile unsigned long CpuIRegionBase = 0xFFFFFFFF;
  933. #endif
  934. #define CLINT_MSIP(base, hartid) (*(volatile uint32_t *)((uintptr_t)((base) + ((hartid) * 4))))
  935. #define SMP_CTRLREG(base, ofs) (*(volatile uint32_t *)((uintptr_t)((base) + (ofs))))
  936. void __sync_harts(void) __attribute__((section(".text.init")));
  937. /**
  938. * \brief Synchronize all harts
  939. * \details
  940. * This function is used to synchronize all the harts,
  941. * especially to wait the boot hart finish initialization of
  942. * data section, bss section and c runtines initialization
  943. * This function must be placed in .text.init section, since
  944. * section initialization is not ready, global variable
  945. * and static variable should be avoid to use in this function,
  946. * and avoid to call other functions
  947. */
  948. void __sync_harts(void)
  949. {
  950. // Only do synchronize when SMP_CPU_CNT is defined and number > 0
  951. // TODO: If you don't need to support SMP, you can directly remove code in it
  952. #if defined(SMP_CPU_CNT) && (SMP_CPU_CNT > 1)
  953. unsigned long hartid = __get_hart_id();
  954. unsigned long tmr_hartid = __get_hart_index();
  955. unsigned long clint_base, irgb_base, smp_base;
  956. unsigned long mcfg_info;
  957. // NOTE: we should avoid to use global variable such as CpuIRegionBase before smp cpu are configured
  958. mcfg_info = __RV_CSR_READ(CSR_MCFG_INFO);
  959. // Assume IREGION feature present
  960. if (mcfg_info & MCFG_INFO_IREGION_EXIST) { // IRegion Info present
  961. // clint base = system timer base + 0x1000
  962. irgb_base = (__RV_CSR_READ(CSR_MIRGB_INFO) >> 10) << 10;
  963. clint_base = irgb_base + IREGION_TIMER_OFS + 0x1000;
  964. smp_base = irgb_base + IREGION_SMP_OFS;
  965. } else {
  966. // Should not enter to here if iregion feature present
  967. while(1);
  968. }
  969. // Enable SMP
  970. SMP_CTRLREG(smp_base, 0xc) = 0xFFFFFFFF;
  971. // Enaable L2, disable cluster local memory
  972. if (SMP_CTRLREG(smp_base, 0x4) & 0x1) {
  973. SMP_CTRLREG(smp_base, 0x10) = 0x1;
  974. SMP_CTRLREG(smp_base, 0xd8) = 0x0;
  975. }
  976. __SMP_RWMB();
  977. // pre-condition: interrupt must be disabled, this is done before calling this function
  978. // BOOT_HARTID is defined <Device.h>
  979. if (hartid == BOOT_HARTID) { // boot hart
  980. // clear msip pending
  981. for (int i = 0; i < SMP_CPU_CNT; i ++) {
  982. CLINT_MSIP(clint_base, i) = 0;
  983. }
  984. __SMP_RWMB();
  985. } else {
  986. // Set machine software interrupt pending to 1
  987. CLINT_MSIP(clint_base, tmr_hartid) = 1;
  988. __SMP_RWMB();
  989. // wait for pending bit cleared by boot hart
  990. while (CLINT_MSIP(clint_base, tmr_hartid) == 1);
  991. }
  992. #endif
  993. }
  994. /**
  995. * \brief do the init for trap
  996. * \details
  997. */
  998. static void Trap_Init(void)
  999. {
  1000. }
  1001. /**
  1002. * \brief early init function before main
  1003. * \details
  1004. * This function is executed right before main function.
  1005. * For RISC-V gnu toolchain, _init function might not be called
  1006. * by __libc_init_array function, so we defined a new function
  1007. * to do initialization.
  1008. */
  1009. void _premain_init(void)
  1010. {
  1011. #if defined(CODESIZE) && (CODESIZE == 1)
  1012. // TODO to reduce the code size of application
  1013. // No need to do so complex premain initialization steps
  1014. // You just need to initialize the cpu resource you need to use in your
  1015. // application code.
  1016. #ifndef CFG_IREGION_BASE_ADDR // Need to probe the cpu iregion base address
  1017. // Probe CPUIRegionBase for other cpu internal peripheral to use
  1018. CpuIRegionBase = (__RV_CSR_READ(CSR_MIRGB_INFO) >> 10) << 10;
  1019. #endif
  1020. // TODO Still need to initialize uart for other code need to do printf
  1021. // If you want to reduce more code, you can comment below code
  1022. uart_init(SOC_DEBUG_UART, 115200);
  1023. #else
  1024. // TODO to make it possible for configurable boot hartid
  1025. unsigned long hartid = __get_hart_id();
  1026. unsigned long mcfginfo = __RV_CSR_READ(CSR_MCFG_INFO);
  1027. /* TODO: Add your own initialization code here, called before main */
  1028. // TODO This code controlled by macros RUNMODE_* are only used internally by Nuclei
  1029. // You can remove it if you don't want it
  1030. // No need to use in your code
  1031. #if defined(RUNMODE_ILM_EN) || defined(RUNMODE_ECC_EN)
  1032. // Only disable ilm when it is present
  1033. if (mcfginfo & MCFG_INFO_ILM) {
  1034. #if defined(RUNMODE_ECC_EN)
  1035. #if RUNMODE_ECC_EN == 0
  1036. __RV_CSR_CLEAR(CSR_MILM_CTL, MILM_CTL_ILM_ECC_EN | MILM_CTL_ILM_ECC_EXCP_EN | MILM_CTL_ILM_ECC_CHK_EN);
  1037. #else
  1038. __RV_CSR_SET(CSR_MILM_CTL, MILM_CTL_ILM_ECC_EN | MILM_CTL_ILM_ECC_EXCP_EN | MILM_CTL_ILM_ECC_CHK_EN);
  1039. #endif
  1040. #endif
  1041. #if defined(RUNMODE_ILM_EN)
  1042. #if RUNMODE_ILM_EN == 0
  1043. __RV_CSR_CLEAR(CSR_MILM_CTL, MILM_CTL_ILM_EN);
  1044. #else
  1045. __RV_CSR_SET(CSR_MILM_CTL, MILM_CTL_ILM_EN);
  1046. #endif
  1047. #endif
  1048. }
  1049. #endif
  1050. #if defined(RUNMODE_DLM_EN) || defined(RUNMODE_ECC_EN)
  1051. // Only disable dlm when it is present
  1052. if (mcfginfo & MCFG_INFO_DLM) {
  1053. #if defined(RUNMODE_ECC_EN)
  1054. #if RUNMODE_ECC_EN == 0
  1055. __RV_CSR_CLEAR(CSR_MDLM_CTL, MDLM_CTL_DLM_ECC_EN | MDLM_CTL_DLM_ECC_EXCP_EN | MDLM_CTL_DLM_ECC_CHK_EN);
  1056. #else
  1057. __RV_CSR_SET(CSR_MDLM_CTL, MDLM_CTL_DLM_ECC_EN | MDLM_CTL_DLM_ECC_EXCP_EN | MDLM_CTL_DLM_ECC_CHK_EN);
  1058. #endif
  1059. #endif
  1060. #if defined(RUNMODE_DLM_EN)
  1061. #if RUNMODE_DLM_EN == 0
  1062. __RV_CSR_CLEAR(CSR_MDLM_CTL, MDLM_CTL_DLM_EN);
  1063. #else
  1064. __RV_CSR_SET(CSR_MDLM_CTL, MDLM_CTL_DLM_EN);
  1065. #endif
  1066. #endif
  1067. }
  1068. #endif
  1069. #if defined(RUNMODE_LDSPEC_EN)
  1070. #if RUNMODE_LDSPEC_EN == 1
  1071. __RV_CSR_SET(CSR_MMISC_CTL, MMISC_CTL_LDSPEC_ENABLE);
  1072. #else
  1073. __RV_CSR_CLEAR(CSR_MMISC_CTL, MMISC_CTL_LDSPEC_ENABLE);
  1074. #endif
  1075. #endif
  1076. /* __ICACHE_PRESENT and __DCACHE_PRESENT are defined in evalsoc.h */
  1077. // For our internal cpu testing, they want to set evalsoc __ICACHE_PRESENT/__DCACHE_PRESENT to be 1
  1078. // __CCM_PRESENT is still default to 0 in evalsoc.h, since it is used in core_feature_eclic.h to register interrupt, if set to 1, it might cause exception
  1079. // but in the cpu, icache or dcache might not exist due to cpu configuration, so here
  1080. // we need to check whether icache/dcache really exist, if yes, then turn on it
  1081. #if defined(__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1)
  1082. if (ICachePresent()) { // Check whether icache real present or not
  1083. #if defined(RUNMODE_ECC_EN)
  1084. #if RUNMODE_ECC_EN == 0
  1085. __RV_CSR_CLEAR(CSR_MCACHE_CTL, MCACHE_CTL_IC_ECC_EN | MCACHE_CTL_IC_ECC_EXCP_EN | MCACHE_CTL_IC_ECC_CHK_EN);
  1086. #else
  1087. __RV_CSR_SET(CSR_MCACHE_CTL, MCACHE_CTL_IC_ECC_EN | MCACHE_CTL_IC_ECC_EXCP_EN | MCACHE_CTL_IC_ECC_CHK_EN);
  1088. #endif
  1089. #endif
  1090. EnableICache();
  1091. }
  1092. #endif
  1093. #if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1)
  1094. if (DCachePresent()) { // Check whether dcache real present or not
  1095. #if defined(RUNMODE_ECC_EN)
  1096. #if RUNMODE_ECC_EN == 0
  1097. __RV_CSR_CLEAR(CSR_MCACHE_CTL, MCACHE_CTL_DC_ECC_EN | MCACHE_CTL_DC_ECC_EXCP_EN | MCACHE_CTL_DC_ECC_CHK_EN);
  1098. #else
  1099. __RV_CSR_SET(CSR_MCACHE_CTL, MCACHE_CTL_DC_ECC_EN | MCACHE_CTL_DC_ECC_EXCP_EN | MCACHE_CTL_DC_ECC_CHK_EN);
  1100. #endif
  1101. #endif
  1102. EnableDCache();
  1103. }
  1104. #endif
  1105. /* Do fence and fence.i to make sure previous ilm/dlm/icache/dcache control done */
  1106. __RWMB();
  1107. __FENCE_I();
  1108. // BOOT_HARTID is defined <Device.h> and also controlled by BOOT_HARTID in conf/evalsoc/build.mk
  1109. #ifndef CFG_IREGION_BASE_ADDR // Need to probe the cpu iregion base address
  1110. if (hartid == BOOT_HARTID) { // only done in boot hart
  1111. // IREGION INFO MUST BE AFTER L1/L2 Cache enabled and SMP enabled if SMP present
  1112. CpuIRegionBase = (__RV_CSR_READ(CSR_MIRGB_INFO) >> 10) << 10;
  1113. } else {
  1114. // wait for correct iregion base addr is set by boot hart
  1115. while (CpuIRegionBase == 0xFFFFFFFF);
  1116. }
  1117. #endif
  1118. #if defined(RUNMODE_L2_EN)
  1119. if ((mcfginfo & (0x1 << 11)) && SMP_CTRLREG(__SMPCC_BASEADDR, 0x4) & 0x1 ) { // L2 Cache present
  1120. #if RUNMODE_L2_EN == 1
  1121. // Enable L2, disable cluster local memory
  1122. SMP_CTRLREG(__SMPCC_BASEADDR, 0x10) = 0x1;
  1123. SMP_CTRLREG(__SMPCC_BASEADDR, 0xd8) = 0x0;
  1124. __SMP_RWMB();
  1125. #else
  1126. // Disable L2, enable cluster local memory
  1127. SMP_CTRLREG(__SMPCC_BASEADDR, 0x10) = 0x0;
  1128. // use as clm or cache, when l2 disable, the affect to ddr is the same, l2 is really disabled
  1129. SMP_CTRLREG(__SMPCC_BASEADDR, 0xd8) = 0;//0xFFFFFFFF;
  1130. __SMP_RWMB();
  1131. #endif
  1132. }
  1133. #endif
  1134. #if defined(RUNMODE_BPU_EN)
  1135. #if RUNMODE_BPU_EN == 1
  1136. __RV_CSR_SET(CSR_MMISC_CTL, MMISC_CTL_BPU);
  1137. #else
  1138. __RV_CSR_CLEAR(CSR_MMISC_CTL, MMISC_CTL_BPU);
  1139. #endif
  1140. #endif
  1141. #if defined(__CCM_PRESENT) && (__CCM_PRESENT == 1)
  1142. // NOTE: CFG_HAS_SMODE and CFG_HAS_UMODE are defined in auto generated cpufeature.h if present in cpu
  1143. #if defined(CFG_HAS_SMODE) || defined(CFG_HAS_UMODE)
  1144. EnableSUCCM();
  1145. #endif
  1146. #endif
  1147. if (hartid == BOOT_HARTID) { // only required for boot hartid
  1148. // TODO implement get_cpu_freq function to get real cpu clock freq in HZ or directly give the real cpu HZ
  1149. // TODO you can directly give the correct cpu frequency here, if you know it without call get_cpu_freq function
  1150. SystemCoreClock = get_cpu_freq();
  1151. uart_init(SOC_DEBUG_UART, 115200);
  1152. /* Display banner after UART initialized */
  1153. SystemBannerPrint();
  1154. /* Initialize exception default handlers */
  1155. Exception_Init();
  1156. /* Interrupt initialization */
  1157. Interrupt_Init();
  1158. // TODO: internal usage for Nuclei
  1159. #ifdef RUNMODE_CONTROL
  1160. NSDK_DEBUG("Current RUNMODE=%s, ilm:%d, dlm %d, icache %d, dcache %d, ccm %d\n", \
  1161. RUNMODE_STRING, RUNMODE_ILM_EN, RUNMODE_DLM_EN, \
  1162. RUNMODE_IC_EN, RUNMODE_DC_EN, RUNMODE_CCM_EN);
  1163. // ILM and DLM need to be present
  1164. if (mcfginfo & 0x180 == 0x180) {
  1165. NSDK_DEBUG("CSR: MILM_CTL 0x%x, MDLM_CTL 0x%x\n", \
  1166. __RV_CSR_READ(CSR_MILM_CTL), __RV_CSR_READ(CSR_MDLM_CTL));
  1167. }
  1168. // I/D cache need to be present
  1169. if (mcfginfo & 0x600) {
  1170. NSDK_DEBUG("CSR: MCACHE_CTL 0x%x\n", __RV_CSR_READ(CSR_MCACHE_CTL));
  1171. }
  1172. NSDK_DEBUG("CSR: MMISC_CTL 0x%x\n", __RV_CSR_READ(CSR_MMISC_CTL));
  1173. #endif
  1174. } else {
  1175. /* Interrupt initialization */
  1176. Interrupt_Init();
  1177. }
  1178. #endif
  1179. }
  1180. /**
  1181. * \brief finish function after main
  1182. * \param [in] status status code return from main
  1183. * \details
  1184. * This function is executed right after main function.
  1185. * For RISC-V gnu toolchain, _fini function might not be called
  1186. * by __libc_fini_array function, so we defined a new function
  1187. * to do initialization
  1188. */
  1189. void _postmain_fini(int status)
  1190. {
  1191. #if defined(CODESIZE) && (CODESIZE == 1)
  1192. #ifdef CFG_SIMULATION
  1193. SIMULATION_EXIT(status);
  1194. #endif
  1195. #else
  1196. /* TODO: Add your own finishing code here, called after main */
  1197. extern void simulation_exit(int status);
  1198. simulation_exit(status);
  1199. #endif
  1200. }
  1201. /**
  1202. * \brief _init function called in __libc_init_array()
  1203. * \details
  1204. * This `__libc_init_array()` function is called during startup code,
  1205. * user need to implement this function, otherwise when link it will
  1206. * error init.c:(.text.__libc_init_array+0x26): undefined reference to `_init'
  1207. * \note
  1208. * Please use \ref _premain_init function now
  1209. */
  1210. void _init(void)
  1211. {
  1212. /* Don't put any code here, please use _premain_init now */
  1213. }
  1214. /**
  1215. * \brief _fini function called in __libc_fini_array()
  1216. * \details
  1217. * This `__libc_fini_array()` function is called when exit main.
  1218. * user need to implement this function, otherwise when link it will
  1219. * error fini.c:(.text.__libc_fini_array+0x28): undefined reference to `_fini'
  1220. * \note
  1221. * Please use \ref _postmain_fini function now
  1222. */
  1223. void _fini(void)
  1224. {
  1225. /* Don't put any code here, please use _postmain_fini now */
  1226. }
  1227. /** @} */ /* End of Doxygen Group NMSIS_Core_SystemConfig */