system.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 switch_to(prev,next,last) \
  94. do { \
  95. clear_cpenable(); \
  96. (last) = _switch_to(prev, next); \
  97. } while(0)
  98. /*
  99. * cmpxchg
  100. */
  101. static inline unsigned long
  102. __cmpxchg_u32(volatile int *p, int old, int new)
  103. {
  104. __asm__ __volatile__("rsil a15, "__stringify(LOCKLEVEL)"\n\t"
  105. "l32i %0, %1, 0 \n\t"
  106. "bne %0, %2, 1f \n\t"
  107. "s32i %3, %1, 0 \n\t"
  108. "1: \n\t"
  109. "wsr a15, "__stringify(PS)" \n\t"
  110. "rsync \n\t"
  111. : "=&a" (old)
  112. : "a" (p), "a" (old), "r" (new)
  113. : "a15", "memory");
  114. return old;
  115. }
  116. /* This function doesn't exist, so you'll get a linker error
  117. * if something tries to do an invalid cmpxchg(). */
  118. extern void __cmpxchg_called_with_bad_pointer(void);
  119. static __inline__ unsigned long
  120. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  121. {
  122. switch (size) {
  123. case 4: return __cmpxchg_u32(ptr, old, new);
  124. default: __cmpxchg_called_with_bad_pointer();
  125. return old;
  126. }
  127. }
  128. #define cmpxchg(ptr,o,n) \
  129. ({ __typeof__(*(ptr)) _o_ = (o); \
  130. __typeof__(*(ptr)) _n_ = (n); \
  131. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  132. (unsigned long)_n_, sizeof (*(ptr))); \
  133. })
  134. /*
  135. * xchg_u32
  136. *
  137. * Note that a15 is used here because the register allocation
  138. * done by the compiler is not guaranteed and a window overflow
  139. * may not occur between the rsil and wsr instructions. By using
  140. * a15 in the rsil, the machine is guaranteed to be in a state
  141. * where no register reference will cause an overflow.
  142. */
  143. static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
  144. {
  145. unsigned long tmp;
  146. __asm__ __volatile__("rsil a15, "__stringify(LOCKLEVEL)"\n\t"
  147. "l32i %0, %1, 0 \n\t"
  148. "s32i %2, %1, 0 \n\t"
  149. "wsr a15, "__stringify(PS)" \n\t"
  150. "rsync \n\t"
  151. : "=&a" (tmp)
  152. : "a" (m), "a" (val)
  153. : "a15", "memory");
  154. return tmp;
  155. }
  156. #define tas(ptr) (xchg((ptr),1))
  157. #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  158. /*
  159. * This only works if the compiler isn't horribly bad at optimizing.
  160. * gcc-2.5.8 reportedly can't handle this, but I define that one to
  161. * be dead anyway.
  162. */
  163. extern void __xchg_called_with_bad_pointer(void);
  164. static __inline__ unsigned long
  165. __xchg(unsigned long x, volatile void * ptr, int size)
  166. {
  167. switch (size) {
  168. case 4:
  169. return xchg_u32(ptr, x);
  170. }
  171. __xchg_called_with_bad_pointer();
  172. return x;
  173. }
  174. extern void set_except_vector(int n, void *addr);
  175. static inline void spill_registers(void)
  176. {
  177. unsigned int a0, ps;
  178. __asm__ __volatile__ (
  179. "movi a14," __stringify (PS_EXCM_MASK) " | 1\n\t"
  180. "mov a12, a0\n\t"
  181. "rsr a13," __stringify(SAR) "\n\t"
  182. "xsr a14," __stringify(PS) "\n\t"
  183. "movi a0, _spill_registers\n\t"
  184. "rsync\n\t"
  185. "callx0 a0\n\t"
  186. "mov a0, a12\n\t"
  187. "wsr a13," __stringify(SAR) "\n\t"
  188. "wsr a14," __stringify(PS) "\n\t"
  189. :: "a" (&a0), "a" (&ps)
  190. : "a2", "a3", "a12", "a13", "a14", "a15", "memory");
  191. }
  192. #define arch_align_stack(x) (x)
  193. #endif /* _XTENSA_SYSTEM_H */