system.h 5.6 KB

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