system.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * include/asm-xtensa/system.h
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2001 - 2005 Tensilica Inc.
  9. */
  10. #ifndef _XTENSA_SYSTEM_H
  11. #define _XTENSA_SYSTEM_H
  12. #include <linux/config.h>
  13. #include <linux/stringify.h>
  14. #include <asm/processor.h>
  15. /* interrupt control */
  16. #define local_save_flags(x) \
  17. __asm__ __volatile__ ("rsr %0,"__stringify(PS) : "=a" (x));
  18. #define local_irq_restore(x) do { \
  19. __asm__ __volatile__ ("wsr %0, "__stringify(PS)" ; rsync" \
  20. :: "a" (x) : "memory"); } while(0);
  21. #define local_irq_save(x) do { \
  22. __asm__ __volatile__ ("rsil %0, "__stringify(LOCKLEVEL) \
  23. : "=a" (x) :: "memory");} while(0);
  24. static inline void local_irq_disable(void)
  25. {
  26. unsigned long flags;
  27. __asm__ __volatile__ ("rsil %0, "__stringify(LOCKLEVEL)
  28. : "=a" (flags) :: "memory");
  29. }
  30. static inline void local_irq_enable(void)
  31. {
  32. unsigned long flags;
  33. __asm__ __volatile__ ("rsil %0, 0" : "=a" (flags) :: "memory");
  34. }
  35. static inline int irqs_disabled(void)
  36. {
  37. unsigned long flags;
  38. local_save_flags(flags);
  39. return flags & 0xf;
  40. }
  41. #define RSR_CPENABLE(x) do { \
  42. __asm__ __volatile__("rsr %0," __stringify(CPENABLE) : "=a" (x)); \
  43. } while(0);
  44. #define WSR_CPENABLE(x) do { \
  45. __asm__ __volatile__("wsr %0," __stringify(CPENABLE)";rsync" \
  46. :: "a" (x));} while(0);
  47. #define clear_cpenable() __clear_cpenable()
  48. static inline void __clear_cpenable(void)
  49. {
  50. #if XCHAL_HAVE_CP
  51. unsigned long i = 0;
  52. WSR_CPENABLE(i);
  53. #endif
  54. }
  55. static inline void enable_coprocessor(int i)
  56. {
  57. #if XCHAL_HAVE_CP
  58. int cp;
  59. RSR_CPENABLE(cp);
  60. cp |= 1 << i;
  61. WSR_CPENABLE(cp);
  62. #endif
  63. }
  64. static inline void disable_coprocessor(int i)
  65. {
  66. #if XCHAL_HAVE_CP
  67. int cp;
  68. RSR_CPENABLE(cp);
  69. cp &= ~(1 << i);
  70. WSR_CPENABLE(cp);
  71. #endif
  72. }
  73. #define smp_read_barrier_depends() do { } while(0)
  74. #define read_barrier_depends() do { } while(0)
  75. #define mb() barrier()
  76. #define rmb() mb()
  77. #define wmb() mb()
  78. #ifdef CONFIG_SMP
  79. #error smp_* not defined
  80. #else
  81. #define smp_mb() barrier()
  82. #define smp_rmb() barrier()
  83. #define smp_wmb() barrier()
  84. #endif
  85. #define set_mb(var, value) do { var = value; mb(); } while (0)
  86. #define set_wmb(var, value) do { var = value; wmb(); } while (0)
  87. #if !defined (__ASSEMBLY__)
  88. /* * switch_to(n) should switch tasks to task nr n, first
  89. * checking that n isn't the current task, in which case it does nothing.
  90. */
  91. extern void *_switch_to(void *last, void *next);
  92. #endif /* __ASSEMBLY__ */
  93. #define prepare_to_switch() do { } while(0)
  94. #define switch_to(prev,next,last) \
  95. do { \
  96. clear_cpenable(); \
  97. (last) = _switch_to(prev, next); \
  98. } while(0)
  99. /*
  100. * cmpxchg
  101. */
  102. static inline unsigned long
  103. __cmpxchg_u32(volatile int *p, int old, int new)
  104. {
  105. __asm__ __volatile__("rsil a15, "__stringify(LOCKLEVEL)"\n\t"
  106. "l32i %0, %1, 0 \n\t"
  107. "bne %0, %2, 1f \n\t"
  108. "s32i %3, %1, 0 \n\t"
  109. "1: \n\t"
  110. "wsr a15, "__stringify(PS)" \n\t"
  111. "rsync \n\t"
  112. : "=&a" (old)
  113. : "a" (p), "a" (old), "r" (new)
  114. : "a15", "memory");
  115. return old;
  116. }
  117. /* This function doesn't exist, so you'll get a linker error
  118. * if something tries to do an invalid cmpxchg(). */
  119. extern void __cmpxchg_called_with_bad_pointer(void);
  120. static __inline__ unsigned long
  121. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  122. {
  123. switch (size) {
  124. case 4: return __cmpxchg_u32(ptr, old, new);
  125. default: __cmpxchg_called_with_bad_pointer();
  126. return old;
  127. }
  128. }
  129. #define cmpxchg(ptr,o,n) \
  130. ({ __typeof__(*(ptr)) _o_ = (o); \
  131. __typeof__(*(ptr)) _n_ = (n); \
  132. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  133. (unsigned long)_n_, sizeof (*(ptr))); \
  134. })
  135. /*
  136. * xchg_u32
  137. *
  138. * Note that a15 is used here because the register allocation
  139. * done by the compiler is not guaranteed and a window overflow
  140. * may not occur between the rsil and wsr instructions. By using
  141. * a15 in the rsil, the machine is guaranteed to be in a state
  142. * where no register reference will cause an overflow.
  143. */
  144. static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
  145. {
  146. unsigned long tmp;
  147. __asm__ __volatile__("rsil a15, "__stringify(LOCKLEVEL)"\n\t"
  148. "l32i %0, %1, 0 \n\t"
  149. "s32i %2, %1, 0 \n\t"
  150. "wsr a15, "__stringify(PS)" \n\t"
  151. "rsync \n\t"
  152. : "=&a" (tmp)
  153. : "a" (m), "a" (val)
  154. : "a15", "memory");
  155. return tmp;
  156. }
  157. #define tas(ptr) (xchg((ptr),1))
  158. #if ( __XCC__ == 1 )
  159. /* xt-xcc processes __inline__ differently than xt-gcc and decides to
  160. * insert an out-of-line copy of function __xchg. This presents the
  161. * unresolved symbol at link time of __xchg_called_with_bad_pointer,
  162. * even though such a function would never be called at run-time.
  163. * xt-gcc always inlines __xchg, and optimizes away the undefined
  164. * bad_pointer function.
  165. */
  166. #define xchg(ptr,x) xchg_u32(ptr,x)
  167. #else /* assume xt-gcc */
  168. #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  169. /*
  170. * This only works if the compiler isn't horribly bad at optimizing.
  171. * gcc-2.5.8 reportedly can't handle this, but I define that one to
  172. * be dead anyway.
  173. */
  174. extern void __xchg_called_with_bad_pointer(void);
  175. static __inline__ unsigned long
  176. __xchg(unsigned long x, volatile void * ptr, int size)
  177. {
  178. switch (size) {
  179. case 4:
  180. return xchg_u32(ptr, x);
  181. }
  182. __xchg_called_with_bad_pointer();
  183. return x;
  184. }
  185. #endif
  186. extern void set_except_vector(int n, void *addr);
  187. static inline void spill_registers(void)
  188. {
  189. unsigned int a0, ps;
  190. __asm__ __volatile__ (
  191. "movi a14," __stringify (PS_EXCM_MASK) " | 1\n\t"
  192. "mov a12, a0\n\t"
  193. "rsr a13," __stringify(SAR) "\n\t"
  194. "xsr a14," __stringify(PS) "\n\t"
  195. "movi a0, _spill_registers\n\t"
  196. "rsync\n\t"
  197. "callx0 a0\n\t"
  198. "mov a0, a12\n\t"
  199. "wsr a13," __stringify(SAR) "\n\t"
  200. "wsr a14," __stringify(PS) "\n\t"
  201. :: "a" (&a0), "a" (&ps)
  202. : "a2", "a3", "a12", "a13", "a14", "a15", "memory");
  203. }
  204. #define arch_align_stack(x) (x)
  205. #endif /* _XTENSA_SYSTEM_H */