system.h 5.7 KB

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