system_evalsoc.c 60 KB

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