xtruntime.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_COREF_PSO 0x00000001 /* do power shutoff */
  40. #define XTOS_COREF_PSO_SHIFT 0
  41. #define _xtos_set_execption_handler _xtos_set_exception_handler /* backward compatibility */
  42. #define _xtos_set_saved_intenable _xtos_ints_on /* backward compatibility */
  43. #define _xtos_clear_saved_intenable _xtos_ints_off /* backward compatibility */
  44. #if !defined(_ASMLANGUAGE) && !defined(__ASSEMBLER__)
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /*typedef void (_xtos_timerdelta_func)(int);*/
  49. #ifdef __cplusplus
  50. typedef void (_xtos_handler_func)(...);
  51. #else
  52. typedef void (_xtos_handler_func)();
  53. #endif
  54. typedef _xtos_handler_func *_xtos_handler;
  55. /*
  56. * unsigned XTOS_SET_INTLEVEL(int intlevel);
  57. * This macro sets the current interrupt level.
  58. * The 'intlevel' parameter must be a constant.
  59. * This macro returns a 32-bit value that must be passed to
  60. * XTOS_RESTORE_INTLEVEL() to restore the previous interrupt level.
  61. * XTOS_RESTORE_JUST_INTLEVEL() also does this, but in XEA2 configs
  62. * it restores only PS.INTLEVEL rather than the entire PS register
  63. * and thus is slower.
  64. */
  65. #if !XCHAL_HAVE_INTERRUPTS
  66. # define XTOS_SET_INTLEVEL(intlevel) 0
  67. # define XTOS_SET_MIN_INTLEVEL(intlevel) 0
  68. # define XTOS_RESTORE_INTLEVEL(restoreval)
  69. # define XTOS_RESTORE_JUST_INTLEVEL(restoreval)
  70. #elif XCHAL_HAVE_XEA2
  71. /* In XEA2, we can simply safely set PS.INTLEVEL directly: */
  72. /* NOTE: these asm macros don't modify memory, but they are marked
  73. * as such to act as memory access barriers to the compiler because
  74. * these macros are sometimes used to delineate critical sections;
  75. * function calls are natural barriers (the compiler does not know
  76. * whether a function modifies memory) unless declared to be inlined. */
  77. # define XTOS_SET_INTLEVEL(intlevel) ({ unsigned __tmp; \
  78. __asm__ __volatile__( "rsil %0, " XTSTR(intlevel) "\n" \
  79. : "=a" (__tmp) : : "memory" ); \
  80. __tmp;})
  81. # define XTOS_SET_MIN_INTLEVEL(intlevel) ({ unsigned __tmp, __tmp2, __tmp3; \
  82. __asm__ __volatile__( "rsr %0, " XTSTR(PS) "\n" /* get old (current) PS.INTLEVEL */ \
  83. "movi %2, " XTSTR(intlevel) "\n" \
  84. "extui %1, %0, 0, 4\n" /* keep only INTLEVEL bits of parameter */ \
  85. "blt %2, %1, 1f\n" \
  86. "rsil %0, " XTSTR(intlevel) "\n" \
  87. "1:\n" \
  88. : "=a" (__tmp), "=&a" (__tmp2), "=&a" (__tmp3) : : "memory" ); \
  89. __tmp;})
  90. # define XTOS_RESTORE_INTLEVEL(restoreval) do{ unsigned __tmp = (restoreval); \
  91. __asm__ __volatile__( "wsr %0, " XTSTR(PS) " ; rsync\n" \
  92. : : "a" (__tmp) : "memory" ); \
  93. }while(0)
  94. # define XTOS_RESTORE_JUST_INTLEVEL(restoreval) _xtos_set_intlevel(restoreval)
  95. #else
  96. /* In XEA1, we have to rely on INTENABLE register virtualization: */
  97. extern unsigned _xtos_set_vpri( unsigned vpri );
  98. extern unsigned _xtos_vpri_enabled; /* current virtual priority */
  99. # define XTOS_SET_INTLEVEL(intlevel) _xtos_set_vpri(~XCHAL_INTLEVEL_ANDBELOW_MASK(intlevel))
  100. # define XTOS_SET_MIN_INTLEVEL(intlevel) _xtos_set_vpri(_xtos_vpri_enabled & ~XCHAL_INTLEVEL_ANDBELOW_MASK(intlevel))
  101. # define XTOS_RESTORE_INTLEVEL(restoreval) _xtos_set_vpri(restoreval)
  102. # define XTOS_RESTORE_JUST_INTLEVEL(restoreval) _xtos_set_vpri(restoreval)
  103. #endif
  104. /*
  105. * The following macros build upon the above. They are generally used
  106. * instead of invoking the SET_INTLEVEL and SET_MIN_INTLEVEL macros directly.
  107. * They all return a value that can be used with XTOS_RESTORE_INTLEVEL()
  108. * or _xtos_restore_intlevel() or _xtos_restore_just_intlevel() to restore
  109. * the effective interrupt level to what it was before the macro was invoked.
  110. * In XEA2, the DISABLE macros are much faster than the MASK macros
  111. * (in all configs, DISABLE sets the effective interrupt level, whereas MASK
  112. * makes ensures the effective interrupt level is at least the level given
  113. * without lowering it; in XEA2 with INTENABLE virtualization, these macros
  114. * affect PS.INTLEVEL only, not the virtual priority, so DISABLE has partial
  115. * MASK semantics).
  116. *
  117. * A typical critical section sequence might be:
  118. * unsigned rval = XTOS_DISABLE_EXCM_INTERRUPTS;
  119. * ... critical section ...
  120. * XTOS_RESTORE_INTLEVEL(rval);
  121. */
  122. /* Enable all interrupts (those activated with _xtos_ints_on()): */
  123. #define XTOS_ENABLE_INTERRUPTS XTOS_SET_INTLEVEL(0)
  124. /* Disable low priority level interrupts (they can interact with the OS): */
  125. #define XTOS_DISABLE_LOWPRI_INTERRUPTS XTOS_SET_INTLEVEL(XCHAL_NUM_LOWPRI_LEVELS)
  126. #define XTOS_MASK_LOWPRI_INTERRUPTS XTOS_SET_MIN_INTLEVEL(XCHAL_NUM_LOWPRI_LEVELS)
  127. /* Disable interrupts that can interact with the OS: */
  128. #define XTOS_DISABLE_EXCM_INTERRUPTS XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL)
  129. #define XTOS_MASK_EXCM_INTERRUPTS XTOS_SET_MIN_INTLEVEL(XCHAL_EXCM_LEVEL)
  130. #if 0 /* XTOS_LOCK_LEVEL is not exported to applications */
  131. /* Disable interrupts that can interact with the OS, or manipulate virtual INTENABLE: */
  132. #define XTOS_DISABLE_LOCK_INTERRUPTS XTOS_SET_INTLEVEL(XTOS_LOCK_LEVEL)
  133. #define XTOS_MASK_LOCK_INTERRUPTS XTOS_SET_MIN_INTLEVEL(XTOS_LOCK_LEVEL)
  134. #endif
  135. /* Disable ALL interrupts (not for common use, particularly if one's processor
  136. * configuration has high-level interrupts and one cares about their latency): */
  137. #define XTOS_DISABLE_ALL_INTERRUPTS XTOS_SET_INTLEVEL(15)
  138. extern unsigned int _xtos_ints_off( unsigned int mask );
  139. extern unsigned int _xtos_ints_on( unsigned int mask );
  140. extern unsigned _xtos_set_intlevel( int intlevel );
  141. extern unsigned _xtos_set_min_intlevel( int intlevel );
  142. extern unsigned _xtos_restore_intlevel( unsigned restoreval );
  143. extern unsigned _xtos_restore_just_intlevel( unsigned restoreval );
  144. extern _xtos_handler _xtos_set_interrupt_handler( int n, _xtos_handler f );
  145. extern _xtos_handler _xtos_set_interrupt_handler_arg( int n, _xtos_handler f, void *arg );
  146. extern _xtos_handler _xtos_set_exception_handler( int n, _xtos_handler f );
  147. extern void _xtos_memep_initrams( void );
  148. extern void _xtos_memep_enable( int flags );
  149. /* For use with the tiny LSP (see LSP reference manual). */
  150. #if XCHAL_NUM_INTLEVELS >= 1
  151. extern void _xtos_dispatch_level1_interrupts( void );
  152. #endif
  153. #if XCHAL_NUM_INTLEVELS >= 2
  154. extern void _xtos_dispatch_level2_interrupts( void );
  155. #endif
  156. #if XCHAL_NUM_INTLEVELS >= 3
  157. extern void _xtos_dispatch_level3_interrupts( void );
  158. #endif
  159. #if XCHAL_NUM_INTLEVELS >= 4
  160. extern void _xtos_dispatch_level4_interrupts( void );
  161. #endif
  162. #if XCHAL_NUM_INTLEVELS >= 5
  163. extern void _xtos_dispatch_level5_interrupts( void );
  164. #endif
  165. #if XCHAL_NUM_INTLEVELS >= 6
  166. extern void _xtos_dispatch_level6_interrupts( void );
  167. #endif
  168. /* Deprecated (but kept because they were documented): */
  169. extern unsigned int _xtos_read_ints( void ); /* use xthal_get_interrupt() instead */
  170. extern void _xtos_clear_ints( unsigned int mask ); /* use xthal_set_intclear() instead */
  171. /* Power shut-off related routines. */
  172. extern int _xtos_core_shutoff(unsigned flags);
  173. extern int _xtos_core_save(unsigned flags, XtosCoreState *savearea, void *code);
  174. extern void _xtos_core_restore(unsigned retvalue, XtosCoreState *savearea);
  175. #if XCHAL_NUM_CONTEXTS > 1
  176. extern unsigned _xtos_init_context(int context_num, int stack_size,
  177. _xtos_handler_func *start_func, int arg1);
  178. #endif
  179. /* Deprecated: */
  180. #if XCHAL_NUM_TIMERS > 0
  181. extern void _xtos_timer_0_delta( int cycles );
  182. #endif
  183. #if XCHAL_NUM_TIMERS > 1
  184. extern void _xtos_timer_1_delta( int cycles );
  185. #endif
  186. #if XCHAL_NUM_TIMERS > 2
  187. extern void _xtos_timer_2_delta( int cycles );
  188. #endif
  189. #if XCHAL_NUM_TIMERS > 3
  190. extern void _xtos_timer_3_delta( int cycles );
  191. #endif
  192. #ifdef __cplusplus
  193. }
  194. #endif
  195. #endif /* !_ASMLANGUAGE && !__ASSEMBLER__ */
  196. #endif /* XTRUNTIME_H */