system_evalsoc.c 64 KB

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