xos_common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // xos_common.h - Macros and definitions common to C and assembly code.
  2. // Copyright (c) 2003-2015 Cadence Design Systems, Inc.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included
  13. // in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #ifndef __XOS_COMMON_H__
  23. #define __XOS_COMMON_H__
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <xtensa/corebits.h>
  28. #include <xtensa/config/system.h>
  29. #include <xtensa/config/tie.h>
  30. #include "xos_params.h"
  31. //-----------------------------------------------------------------------------
  32. // Macros that help define structures for both C and assembler.
  33. // These are somewhat different from the XTOS version in xtruntime-frames.h.
  34. //-----------------------------------------------------------------------------
  35. #if defined(_ASMLANGUAGE) || defined(__ASSEMBLER__)
  36. #define STRUCT_BEGIN .pushsection .text; .struct 0
  37. #define STRUCT_FIELD(ctype,size,asname,name) asname: .space size
  38. #define STRUCT_AFIELD(ctype,size,asname,name,n) asname: .space (size)*(n)
  39. #define STRUCT_END(sname) sname##Size:; .popsection
  40. #else
  41. #define STRUCT_BEGIN typedef struct {
  42. #define STRUCT_FIELD(ctype,size,asname,name) ctype name;
  43. #define STRUCT_AFIELD(ctype,size,asname,name,n) ctype name[n];
  44. #define STRUCT_END(sname) } sname;
  45. #endif //_ASMLANGUAGE || __ASSEMBLER__
  46. //-----------------------------------------------------------------------------
  47. // Offsets relative to xos_globals.
  48. //-----------------------------------------------------------------------------
  49. #define XOS_INTLEVEL_MASK 0 // offset to the level mask
  50. #define XOS_INTENABLE_MASK 4 // offset to the enable mask
  51. #define XOS_CURR_THREADPTR 8 // offset to the current thread ptr
  52. #define XOS_NEXT_THREADPTR 12 // offset to the next thread ptr
  53. #define XOS_INTERRUPT_TABLE 16 // offset to the interrupt table
  54. //-----------------------------------------------------------------------------
  55. // Offsets for xos_interrupt_table[] entries.
  56. //-----------------------------------------------------------------------------
  57. #define XOS_INTTAB_HANDLER (XOS_INTERRUPT_TABLE+0) // ofs to interrupt handler
  58. #define XOS_INTTAB_ARG (XOS_INTERRUPT_TABLE+4) // ofs to interrupt handler arg
  59. #define XOS_INTTAB_PS (XOS_INTERRUPT_TABLE+8) // (hwpri) PS for interrupt level
  60. #define XOS_INTTAB_LEVEL (XOS_INTERRUPT_TABLE+8) // (swpri) interrupt level (1..7)
  61. #define XOS_INTTAB_PRI (XOS_INTERRUPT_TABLE+9) // (swpri) interrupt priority (0..255)
  62. #define XOS_INTTAB_PRIMASK (XOS_INTERRUPT_TABLE+12) // (swpri) mask of higher pri. interrupts
  63. //-----------------------------------------------------------------------------
  64. // Exception/interrupt stack frame layout for a pre-empted thread
  65. // tcb->resume_fn == &xos_resume_preempted_thread).
  66. // Pointed to by thread->esf. Located just below thread's current stack ptr.
  67. // Thread's a1 == thread->esf + XosExcFrameSize.
  68. // NOTE: exception frame size is a multiple of 16.
  69. //-----------------------------------------------------------------------------
  70. STRUCT_BEGIN
  71. STRUCT_AFIELD(long,4,FRAME_AREG,areg, 12) // a4-a15 (offsets 0 thru 44)
  72. // (a1 is computed, a0,a2-a3 are in s32e range of a1)
  73. //#if XCHAL_HAVE_LOOPS
  74. STRUCT_FIELD (long,4,FRAME_LBEG,lbeg)
  75. STRUCT_FIELD (long,4,FRAME_LEND,lend)
  76. STRUCT_FIELD (long,4,FRAME_LCOUNT,lcount)
  77. //#endif
  78. //#if XCHAL_HAVE_MAC16
  79. STRUCT_FIELD (long,4,FRAME_ACCLO,acclo)
  80. STRUCT_FIELD (char,1,FRAME_ACCHI,acchi)
  81. //#endif
  82. STRUCT_FIELD (char,1,FRAME_SAR,sar)
  83. STRUCT_FIELD (short,2,FRAME_PAD0,pad0) // unused
  84. STRUCT_FIELD (long,4,FRAME_EXCCAUSE,exccause)
  85. STRUCT_FIELD (long,4,FRAME_EXCVADDR,excvaddr)
  86. STRUCT_FIELD (long,4,FRAME_PAD1,pad1) // unused -- pad to make multiple of 16 bytes
  87. STRUCT_FIELD (long,4,FRAME_PAD2,pad2)
  88. STRUCT_FIELD (long,4,FRAME_PS,ps) // (XOS_FRAME_SIZE-44) in S32E range of end
  89. STRUCT_FIELD (long,4,FRAME_PC,pc) // (XOS_FRAME_SIZE-40) in S32E range of end
  90. STRUCT_FIELD (long,4,FRAME_A0,a0)
  91. STRUCT_FIELD (long,4,FRAME_A2,a2) // (XOS_FRAME_SIZE-32) in S32E range of end
  92. STRUCT_FIELD (long,4,FRAME_A3,a3) // (XOS_FRAME_SIZE-28) in S32E range of end
  93. STRUCT_FIELD (long,4,FRAME_LEVELMASK,levelmask) //
  94. STRUCT_FIELD (long,4,FRAME_NESTCHAIN,nestchain) // nested C function call chain ptr
  95. // Caller's a0-a3 save area below SP. These fields MUST be the last ones in the
  96. // struct so that they are guaranteed to be just under the original SP (before
  97. // we allocate the exception frame).
  98. STRUCT_AFIELD (long,4,FRAME_CWINSAVE,cwinsave, 4) // (XOS_FRAME_SIZE-16)
  99. STRUCT_END(XosExcFrame) // NOTE: exception frame size is 128
  100. #define FRAME_AR(x) (FRAME_AREG + x*4 - 16)
  101. #if defined(_ASMLANGUAGE) || defined(__ASSEMBLER__)
  102. #define XOS_FRAME_SIZE XosExcFrameSize
  103. #else
  104. #define XOS_FRAME_SIZE sizeof(XosExcFrame)
  105. #endif
  106. //-----------------------------------------------------------------------------
  107. // Stack frame layout for a cooperatively switched out thread
  108. // (tcb->resume_fn == &xos_resume_cooperative_thread).
  109. // Pointed to by thread->esf. This is a function frame.
  110. // Thread's a1 == thread->esf.
  111. //-----------------------------------------------------------------------------
  112. STRUCT_BEGIN
  113. STRUCT_FIELD (long,4,CFRAME_A0,a0) // return PC
  114. STRUCT_FIELD (long,4,CFRAME_LEVELMASK,levelmask)
  115. STRUCT_FIELD (long,4,CFRAME_PS,ps)
  116. #ifdef __XTENSA_CALL0_ABI__
  117. STRUCT_FIELD (long,4,CFRAME_PAD0,pad0)
  118. STRUCT_AFIELD(long,4,CFRAME_AREG,areg,4) // callee-saved regs a12-a15
  119. #endif
  120. STRUCT_END(XosCoopFrame)
  121. //-----------------------------------------------------------------------------
  122. // Offsets into thread control block (must match xos_thread.h !!)
  123. //-----------------------------------------------------------------------------
  124. #define TCB_RESUME_FN 12 // ptr to thread resume asm sequence
  125. #define TCB_STACK_ESF 16 // saved stack ptr (actually, ptr to ESF)
  126. #define TCB_TIE_SAVE 20 // ptr to TIE save area
  127. #define TCB_RETVALUE 24 // ptr to xos_block return value
  128. #define TCB_STACK_END 36 // ptr to end of stack (thread's initial stack ptr)
  129. #define TCB_STARTUP_ENTRY 40 // ptr to thread entry function
  130. #define TCB_STARTUP_ARG 44 // ptr to thread entry function's arg
  131. #define TCB_READY 48 // thread ready state (1 byte)
  132. #define TCB_CLIB_PTR 108 // thread C lib context pointer
  133. #define TCB_RESUME_CCOUNT 116 // cycle count at last resume
  134. #define TCB_CYCLE_COUNT 120 // number of cycles consumed
  135. #define TCB_NORMAL_RESUMES 128 // number of cooperative/restart thread resumes
  136. #define TCB_PREEMPT_RESUMES 132 // number of pre-emptive thread resumes
  137. //-----------------------------------------------------------------------------
  138. // Coprocessor state handling:
  139. // The coprocessor state save area is allocated on the thread stack. The stack
  140. // must be sized appropriately. Threads that do not use coprocessors need not
  141. // allocate the storage area.
  142. //
  143. // Along with the save area for each coprocessor, two bitmasks with flags per
  144. // coprocessor (laid out as in the CPENABLE reg) help manage context switching
  145. // coprocessors as efficiently as possible:
  146. //
  147. // XT_CPENABLE
  148. // The contents of a non-running thread's CPENABLE register.
  149. // It represents the coprocessors owned (and whose state is still needed)
  150. // by the thread. When a thread is preempted, its CPENABLE is saved here.
  151. // When a thread solicits a context switch, its CPENABLE is cleared - the
  152. // compiler has saved the (caller-saved) coprocessor state if needed.
  153. // When a non-running thread loses ownership of a CP, its bit is cleared.
  154. // When a thread runs, it's XT_CPENABLE is loaded into the CPENABLE reg.
  155. // Avoids coprocessor exceptions when no change of ownership is needed.
  156. //
  157. // XT_CPSTORED
  158. // A bitmask with the same layout as CPENABLE, a bit per coprocessor.
  159. // Indicates whether the state of each coprocessor is saved in the state
  160. // save area. When a thread enters the kernel, only the state of coprocs
  161. // still enabled in CPENABLE is saved. When the coprocessor exception
  162. // handler assigns ownership of a coprocessor to a thread, it restores
  163. // the saved state only if this bit is set, and clears this bit.
  164. //
  165. // XT_CP_CS_ST
  166. // A bitmask with the same layout as CPENABLE, a bit per co-processor.
  167. // Indicates whether callee-saved state is saved in the state save area.
  168. // Callee-saved state is saved by itself on a solicited context switch,
  169. // and restored when needed by the coprocessor exception handler.
  170. // Unsolicited switches will cause the entire coprocessor to be saved
  171. // when necessary.
  172. //
  173. // XT_NCP_ASA
  174. // Pointer to aligned save area for non-CP state. This is always filled
  175. // in, even if there is no non-CP state to be saved. If there is no state
  176. // to be saved then no space is actually allocated and this pointer is
  177. // not used.
  178. //
  179. // XT_CP_ASA
  180. // Pointer to aligned save area for coprocessor state. This is filled in
  181. // only if coprocessor state is to be saved for the thread. Allows it to be
  182. // aligned more than the overall save area (which might be stack-aligned
  183. // or TCB-aligned). Especially relevant for Xtensa cores configured with a
  184. // very large data path that requires alignment greater than 16 bytes (ABI
  185. // stack alignment).
  186. //-----------------------------------------------------------------------------
  187. #define ALIGNUP(n, val) (((val) + (n)-1) & -(n))
  188. // Offsets of each coprocessor save area within the 'aligned save area'.
  189. // The non-CP TIE state save area is at offset 0, so that it does not
  190. // move around if some or all coprocessors are not to be saved.
  191. #define XT_NCP_SA 0
  192. #define XT_CP0_SA ALIGNUP(XCHAL_CP0_SA_ALIGN, XT_NCP_SA + XCHAL_NCP_SA_SIZE)
  193. #define XT_CP1_SA ALIGNUP(XCHAL_CP1_SA_ALIGN, XT_CP0_SA + XCHAL_CP0_SA_SIZE)
  194. #define XT_CP2_SA ALIGNUP(XCHAL_CP2_SA_ALIGN, XT_CP1_SA + XCHAL_CP1_SA_SIZE)
  195. #define XT_CP3_SA ALIGNUP(XCHAL_CP3_SA_ALIGN, XT_CP2_SA + XCHAL_CP2_SA_SIZE)
  196. #define XT_CP4_SA ALIGNUP(XCHAL_CP4_SA_ALIGN, XT_CP3_SA + XCHAL_CP3_SA_SIZE)
  197. #define XT_CP5_SA ALIGNUP(XCHAL_CP5_SA_ALIGN, XT_CP4_SA + XCHAL_CP4_SA_SIZE)
  198. #define XT_CP6_SA ALIGNUP(XCHAL_CP6_SA_ALIGN, XT_CP5_SA + XCHAL_CP5_SA_SIZE)
  199. #define XT_CP7_SA ALIGNUP(XCHAL_CP7_SA_ALIGN, XT_CP6_SA + XCHAL_CP6_SA_SIZE)
  200. #define XT_TOT_SA_SIZE ALIGNUP(16, XT_CP7_SA + XCHAL_CP7_SA_SIZE)
  201. #define XT_NCP_SA_SIZE XCHAL_NCP_SA_SIZE
  202. // Offsets within the overall save area
  203. #define XT_CPENABLE 0 // (2 bytes) coprocessors active for this thread
  204. #define XT_CPSTORED 2 // (2 bytes) coprocessors saved for this thread
  205. #define XT_CP_CS_ST 4 // (2 bytes) coprocessor callee-saved regs for this thread
  206. #define XT_NCP_ASA 8 // (4 bytes) ptr to aligned save area for nonCP state
  207. #define XT_CP_ASA 12 // (4 bytes) ptr to aligned save area for CP state
  208. // Overall size allows for dynamic alignment, make sure multiple of 4 bytes.
  209. // XT_CP_SIZE - total space needed for all coprocessors + nonCP state + hdr
  210. // XT_NCP_SIZE - total space needed for nonCP state + hdr
  211. #define XT_CP_SIZE ALIGNUP(4, (16 + XT_TOT_SA_SIZE + XCHAL_TOTAL_SA_ALIGN))
  212. #define XT_NCP_SIZE ALIGNUP(4, (16 + XT_NCP_SA_SIZE + XCHAL_TOTAL_SA_ALIGN))
  213. //-----------------------------------------------------------------------------
  214. // Stack size computation.
  215. //
  216. // XOS_STACK_MIN_SIZE
  217. // The minimum recommended stack size for any XOS thread. If you want to
  218. // use a stack size smaller than this, you will have to verify that the
  219. // smaller size will work under all operating conditions.
  220. //
  221. // XOS_STACK_MIN_SIZE_NO_CP
  222. // The minimum recommended atack size for threads that will not use any
  223. // coprocessor resources. No coprocessor state will be saved/restored
  224. // for these threads. Non-CP TIE state will still be saved/restored.
  225. // These threads must be created with the flag XOS_THREAD_NO_CP.
  226. //
  227. // XOS_STACK_EXTRA
  228. // The amount of stack space used by the system to:
  229. // - save coprocessor state
  230. // - save non-coprocessor TIE state
  231. // - allocate an interrupt/exception frame
  232. //
  233. // XOS_STACK_EXTRA_NO_CP
  234. // The amount of stack space used by the system to:
  235. // - save non-coprocessor TIE state
  236. // - allocate an interrupt/exception frame
  237. //-----------------------------------------------------------------------------
  238. #define XOS_STACK_EXTRA (XOS_FRAME_SIZE + XT_CP_SIZE)
  239. #define XOS_STACK_EXTRA_NO_CP (XOS_FRAME_SIZE + XT_NCP_SIZE)
  240. #ifdef __XTENSA_CALL0_ABI__
  241. #define XOS_STACK_MIN_SIZE (XOS_STACK_EXTRA + 0x180)
  242. #define XOS_STACK_MIN_SIZE_NO_CP (XOS_STACK_EXTRA_NO_CP + 0x180)
  243. #else
  244. #define XOS_STACK_MIN_SIZE (XOS_STACK_EXTRA + 0x200)
  245. #define XOS_STACK_MIN_SIZE_NO_CP (XOS_STACK_EXTRA_NO_CP + 0x200)
  246. #endif
  247. //-----------------------------------------------------------------------------
  248. // Items related to C library thread safety.
  249. //-----------------------------------------------------------------------------
  250. #if XOS_OPT_THREAD_SAFE_CLIB
  251. #if XSHAL_CLIB == XTHAL_CLIB_XCLIB
  252. #if !defined(_ASMLANGUAGE) && !defined(__ASSEMBLER__)
  253. #include <sys/reent.h>
  254. #endif
  255. #define CLIB_THREAD_STRUCT struct _reent xclib_reent
  256. #define GLOBAL_CLIB_PTR _reent_ptr
  257. #elif XSHAL_CLIB == XTHAL_CLIB_NEWLIB
  258. #if !defined(_ASMLANGUAGE) && !defined(__ASSEMBLER__)
  259. #include <sys/reent.h>
  260. #endif
  261. #define CLIB_THREAD_STRUCT struct _reent newlib_reent
  262. #define GLOBAL_CLIB_PTR _impure_ptr
  263. #else
  264. #error The selected C runtime library is not thread safe.
  265. #endif
  266. #endif // XOS_OPT_THREAD_SAFE_CLIB
  267. //-----------------------------------------------------------------------------
  268. // Check (MAX_OS_INTLEVEL,EXCM_LEVEL)
  269. //-----------------------------------------------------------------------------
  270. #if XOS_MAX_OS_INTLEVEL >= XCHAL_EXCM_LEVEL
  271. # define XOS_MAX_OSEXCM_LEVEL XOS_MAX_OS_INTLEVEL
  272. #else
  273. # warning "XOS_MAX_OS_INTLEVEL was set below XCHAL_EXCM_LEVEL: this was never tested"
  274. # define XOS_MAX_OSEXCM_LEVEL XCHAL_EXCM_LEVEL
  275. #endif
  276. //-----------------------------------------------------------------------------
  277. // Detect if in interrupt context.
  278. //-----------------------------------------------------------------------------
  279. #if XCHAL_HAVE_INTERRUPTS
  280. #define INTERRUPT_CONTEXT ((XT_RSR_PS() & PS_UM) == 0)
  281. #else
  282. #define INTERRUPT_CONTEXT 0
  283. #endif
  284. //-----------------------------------------------------------------------------
  285. // Xtensa tools version.
  286. //-----------------------------------------------------------------------------
  287. #if defined __XCC__
  288. #define XTTOOLS_VERSION (__XCC__ + __XCC_MINOR__)
  289. #else
  290. #define XTTOOLS_VERSION (0)
  291. #endif
  292. //-----------------------------------------------------------------------------
  293. // Erratum workarounds.
  294. //-----------------------------------------------------------------------------
  295. // Erratum 487 fix is available in version RF.3 onwards and RG.2 onwards.
  296. #if ((__XCC__ == 11000) && (__XCC_MINOR__ >= 3)) || (XTTOOLS_VERSION >= 12002)
  297. #define HWERR_487_FIX hw_erratum_487_fix
  298. #else
  299. #define HWERR_487_FIX
  300. #endif
  301. #ifdef __cplusplus
  302. }
  303. #endif
  304. #endif // __XOS_COMMON_H__