xtruntime.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * xtruntime.h -- general C definitions for single-threaded run-time
  3. *
  4. * Copyright (c) 2002-2013 Tensilica Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef XTRUNTIME_H
  26. #define XTRUNTIME_H
  27. #include <xtensa/config/core.h>
  28. #include <xtensa/config/specreg.h>
  29. #include <xtensa/xtruntime-core-state.h>
  30. #ifndef XTSTR
  31. #define _XTSTR(x) # x
  32. #define XTSTR(x) _XTSTR(x)
  33. #endif
  34. /* _xtos_core_shutoff() flags parameter values: */
  35. #define XTOS_KEEPON_MEM 0x00000100 /* ==PWRCTL_MEM_WAKEUP */
  36. #define XTOS_KEEPON_MEM_SHIFT 8
  37. #define XTOS_KEEPON_DEBUG 0x00001000 /* ==PWRCTL_DEBUG_WAKEUP */
  38. #define XTOS_KEEPON_DEBUG_SHIFT 12
  39. #define XTOS_IDMA_NO_WAIT 0x00010000 /* Do not wait for idma to finish. Disable if necessary */
  40. #define XTOS_IDMA_WAIT_STANDBY 0x00020000 /* Also treat standby state as the end of wait */
  41. #define XTOS_COREF_PSO 0x00000001 /* do power shutoff */
  42. #define XTOS_COREF_PSO_SHIFT 0
  43. #define _xtos_set_execption_handler _xtos_set_exception_handler /* backward compatibility */
  44. #define _xtos_set_saved_intenable _xtos_ints_on /* backward compatibility */
  45. #define _xtos_clear_saved_intenable _xtos_ints_off /* backward compatibility */
  46. #if !defined(_ASMLANGUAGE) && !defined(__ASSEMBLER__)
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. #if defined(XTOS_MISRA)
  51. typedef void (_xtos_handler_func)(void *);
  52. #elif defined(__cplusplus)
  53. typedef void (_xtos_handler_func)(...);
  54. #else
  55. typedef void (_xtos_handler_func)(void);
  56. #endif
  57. typedef _xtos_handler_func *_xtos_handler;
  58. /*
  59. * unsigned XTOS_SET_INTLEVEL(int intlevel);
  60. * This macro sets the current interrupt level.
  61. * The 'intlevel' parameter must be a constant.
  62. * This macro returns a 32-bit value that must be passed to
  63. * XTOS_RESTORE_INTLEVEL() to restore the previous interrupt level.
  64. * XTOS_RESTORE_JUST_INTLEVEL() also does this, but in XEA2 configs
  65. * it restores only PS.INTLEVEL rather than the entire PS register
  66. * and thus is slower.
  67. */
  68. #if !XCHAL_HAVE_INTERRUPTS
  69. # define XTOS_SET_INTLEVEL(intlevel) 0
  70. # define XTOS_SET_MIN_INTLEVEL(intlevel) 0
  71. # define XTOS_RESTORE_INTLEVEL(restoreval)
  72. # define XTOS_RESTORE_JUST_INTLEVEL(restoreval)
  73. #elif XCHAL_HAVE_XEA2
  74. /* In XEA2, we can simply safely set PS.INTLEVEL directly: */
  75. /* NOTE: these asm macros don't modify memory, but they are marked
  76. * as such to act as memory access barriers to the compiler because
  77. * these macros are sometimes used to delineate critical sections;
  78. * function calls are natural barriers (the compiler does not know
  79. * whether a function modifies memory) unless declared to be inlined. */
  80. # define XTOS_SET_INTLEVEL(intlevel) __extension__({ unsigned __tmp; \
  81. __asm__ __volatile__( "rsil %0, " XTSTR(intlevel) "\n" \
  82. : "=a" (__tmp) : : "memory" ); \
  83. __tmp;})
  84. # define XTOS_SET_MIN_INTLEVEL(intlevel) ({ unsigned __tmp, __tmp2, __tmp3; \
  85. __asm__ __volatile__( "rsr.ps %0\n" /* get old (current) PS.INTLEVEL */ \
  86. "movi %2, " XTSTR(intlevel) "\n" \
  87. "extui %1, %0, 0, 4\n" /* keep only INTLEVEL bits of parameter */ \
  88. "blt %2, %1, 1f\n" \
  89. "rsil %0, " XTSTR(intlevel) "\n" \
  90. "1:\n" \
  91. : "=a" (__tmp), "=&a" (__tmp2), "=&a" (__tmp3) : : "memory" ); \
  92. __tmp;})
  93. # define XTOS_RESTORE_INTLEVEL(restoreval) do{ unsigned __tmp = (restoreval); \
  94. __asm__ __volatile__( "wsr.ps %0 ; rsync\n" \
  95. : : "a" (__tmp) : "memory" ); \
  96. }while(0)
  97. # define XTOS_RESTORE_JUST_INTLEVEL(restoreval) _xtos_set_intlevel(restoreval)
  98. #else
  99. /* In XEA1, we have to rely on INTENABLE register virtualization: */
  100. extern unsigned _xtos_set_vpri( unsigned vpri );
  101. extern unsigned _xtos_vpri_enabled; /* current virtual priority */
  102. # define XTOS_SET_INTLEVEL(intlevel) _xtos_set_vpri(~XCHAL_INTLEVEL_ANDBELOW_MASK(intlevel))
  103. # define XTOS_SET_MIN_INTLEVEL(intlevel) _xtos_set_vpri(_xtos_vpri_enabled & ~XCHAL_INTLEVEL_ANDBELOW_MASK(intlevel))
  104. # define XTOS_RESTORE_INTLEVEL(restoreval) _xtos_set_vpri(restoreval)
  105. # define XTOS_RESTORE_JUST_INTLEVEL(restoreval) _xtos_set_vpri(restoreval)
  106. #endif
  107. /*
  108. * The following macros build upon the above. They are generally used
  109. * instead of invoking the SET_INTLEVEL and SET_MIN_INTLEVEL macros directly.
  110. * They all return a value that can be used with XTOS_RESTORE_INTLEVEL()
  111. * or _xtos_restore_intlevel() or _xtos_restore_just_intlevel() to restore
  112. * the effective interrupt level to what it was before the macro was invoked.
  113. * In XEA2, the DISABLE macros are much faster than the MASK macros
  114. * (in all configs, DISABLE sets the effective interrupt level, whereas MASK
  115. * makes ensures the effective interrupt level is at least the level given
  116. * without lowering it; in XEA2 with INTENABLE virtualization, these macros
  117. * affect PS.INTLEVEL only, not the virtual priority, so DISABLE has partial
  118. * MASK semantics).
  119. *
  120. * A typical critical section sequence might be:
  121. * unsigned rval = XTOS_DISABLE_EXCM_INTERRUPTS;
  122. * ... critical section ...
  123. * XTOS_RESTORE_INTLEVEL(rval);
  124. */
  125. /* Enable all interrupts (those activated with _xtos_ints_on()): */
  126. #define XTOS_ENABLE_INTERRUPTS XTOS_SET_INTLEVEL(0)
  127. /* Disable low priority level interrupts (they can interact with the OS): */
  128. #define XTOS_DISABLE_LOWPRI_INTERRUPTS XTOS_SET_INTLEVEL(XCHAL_NUM_LOWPRI_LEVELS)
  129. #define XTOS_MASK_LOWPRI_INTERRUPTS XTOS_SET_MIN_INTLEVEL(XCHAL_NUM_LOWPRI_LEVELS)
  130. /* Disable interrupts that can interact with the OS: */
  131. #define XTOS_DISABLE_EXCM_INTERRUPTS XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL)
  132. #define XTOS_MASK_EXCM_INTERRUPTS XTOS_SET_MIN_INTLEVEL(XCHAL_EXCM_LEVEL)
  133. #if 0 /* XTOS_LOCK_LEVEL is not exported to applications */
  134. /* Disable interrupts that can interact with the OS, or manipulate virtual INTENABLE: */
  135. #define XTOS_DISABLE_LOCK_INTERRUPTS XTOS_SET_INTLEVEL(XTOS_LOCK_LEVEL)
  136. #define XTOS_MASK_LOCK_INTERRUPTS XTOS_SET_MIN_INTLEVEL(XTOS_LOCK_LEVEL)
  137. #endif
  138. /* Disable ALL interrupts (not for common use, particularly if one's processor
  139. * configuration has high-level interrupts and one cares about their latency): */
  140. #define XTOS_DISABLE_ALL_INTERRUPTS XTOS_SET_INTLEVEL(15)
  141. /* These two are deprecated. Use the newer functions below. */
  142. extern unsigned int _xtos_ints_off( unsigned int mask );
  143. extern unsigned int _xtos_ints_on( unsigned int mask );
  144. /* Newer functions to enable/disable the specified interrupt. */
  145. static inline void _xtos_interrupt_enable(unsigned int intnum)
  146. {
  147. _xtos_ints_on(1U << intnum);
  148. }
  149. static inline void _xtos_interrupt_disable(unsigned int intnum)
  150. {
  151. _xtos_ints_off(1U << intnum);
  152. }
  153. extern unsigned _xtos_set_intlevel( int intlevel );
  154. extern unsigned _xtos_set_min_intlevel( int intlevel );
  155. extern unsigned _xtos_restore_intlevel( unsigned restoreval );
  156. extern unsigned _xtos_restore_just_intlevel( unsigned restoreval );
  157. extern _xtos_handler _xtos_set_interrupt_handler( int n, _xtos_handler f );
  158. extern _xtos_handler _xtos_set_interrupt_handler_arg( int n, _xtos_handler f, void *arg );
  159. extern _xtos_handler _xtos_set_exception_handler( int n, _xtos_handler f );
  160. extern void _xtos_memep_initrams( void );
  161. extern void _xtos_memep_enable( int flags );
  162. /* For use with the tiny LSP (see LSP reference manual). */
  163. #if XCHAL_NUM_INTLEVELS >= 1
  164. extern void _xtos_dispatch_level1_interrupts( void );
  165. #endif
  166. #if XCHAL_NUM_INTLEVELS >= 2
  167. extern void _xtos_dispatch_level2_interrupts( void );
  168. #endif
  169. #if XCHAL_NUM_INTLEVELS >= 3
  170. extern void _xtos_dispatch_level3_interrupts( void );
  171. #endif
  172. #if XCHAL_NUM_INTLEVELS >= 4
  173. extern void _xtos_dispatch_level4_interrupts( void );
  174. #endif
  175. #if XCHAL_NUM_INTLEVELS >= 5
  176. extern void _xtos_dispatch_level5_interrupts( void );
  177. #endif
  178. #if XCHAL_NUM_INTLEVELS >= 6
  179. extern void _xtos_dispatch_level6_interrupts( void );
  180. #endif
  181. /* Deprecated (but kept because they were documented): */
  182. extern unsigned int _xtos_read_ints( void );
  183. extern void _xtos_clear_ints( unsigned int mask );
  184. /* Power shut-off related routines. */
  185. extern int _xtos_core_shutoff(unsigned flags);
  186. extern int _xtos_core_save(unsigned flags, XtosCoreState *savearea, void *code);
  187. extern void _xtos_core_restore(unsigned retvalue, XtosCoreState *savearea);
  188. #if XCHAL_NUM_CONTEXTS > 1
  189. extern unsigned _xtos_init_context(int context_num, int stack_size,
  190. _xtos_handler_func *start_func, int arg1);
  191. #endif
  192. /* Deprecated: */
  193. #if XCHAL_NUM_TIMERS > 0
  194. extern void _xtos_timer_0_delta( int cycles );
  195. #endif
  196. #if XCHAL_NUM_TIMERS > 1
  197. extern void _xtos_timer_1_delta( int cycles );
  198. #endif
  199. #if XCHAL_NUM_TIMERS > 2
  200. extern void _xtos_timer_2_delta( int cycles );
  201. #endif
  202. #if XCHAL_NUM_TIMERS > 3
  203. extern void _xtos_timer_3_delta( int cycles );
  204. #endif
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. #endif /* !_ASMLANGUAGE && !__ASSEMBLER__ */
  209. #endif /* XTRUNTIME_H */