system.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #ifndef _ASM_M32R_SYSTEM_H
  2. #define _ASM_M32R_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 Hiroyuki Kondo, Hirokazu Takata, and Hitoshi Yamamoto
  9. * Copyright (C) 2004, 2006 Hirokazu Takata <takata at linux-m32r.org>
  10. */
  11. #include <linux/compiler.h>
  12. #include <asm/assembler.h>
  13. #ifdef __KERNEL__
  14. /*
  15. * switch_to(prev, next) should switch from task `prev' to `next'
  16. * `prev' will never be the same as `next'.
  17. *
  18. * `next' and `prev' should be struct task_struct, but it isn't always defined
  19. */
  20. #if defined(CONFIG_FRAME_POINTER) || \
  21. !defined(CONFIG_SCHED_NO_NO_OMIT_FRAME_POINTER)
  22. #define M32R_PUSH_FP " push fp\n"
  23. #define M32R_POP_FP " pop fp\n"
  24. #else
  25. #define M32R_PUSH_FP ""
  26. #define M32R_POP_FP ""
  27. #endif
  28. #define switch_to(prev, next, last) do { \
  29. __asm__ __volatile__ ( \
  30. " seth lr, #high(1f) \n" \
  31. " or3 lr, lr, #low(1f) \n" \
  32. " st lr, @%4 ; store old LR \n" \
  33. " ld lr, @%5 ; load new LR \n" \
  34. M32R_PUSH_FP \
  35. " st sp, @%2 ; store old SP \n" \
  36. " ld sp, @%3 ; load new SP \n" \
  37. " push %1 ; store `prev' on new stack \n" \
  38. " jmp lr \n" \
  39. " .fillinsn \n" \
  40. "1: \n" \
  41. " pop %0 ; restore `__last' from new stack \n" \
  42. M32R_POP_FP \
  43. : "=r" (last) \
  44. : "0" (prev), \
  45. "r" (&(prev->thread.sp)), "r" (&(next->thread.sp)), \
  46. "r" (&(prev->thread.lr)), "r" (&(next->thread.lr)) \
  47. : "memory", "lr" \
  48. ); \
  49. } while(0)
  50. /* Interrupt Control */
  51. #if !defined(CONFIG_CHIP_M32102) && !defined(CONFIG_CHIP_M32104)
  52. #define local_irq_enable() \
  53. __asm__ __volatile__ ("setpsw #0x40 -> nop": : :"memory")
  54. #define local_irq_disable() \
  55. __asm__ __volatile__ ("clrpsw #0x40 -> nop": : :"memory")
  56. #else /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
  57. static inline void local_irq_enable(void)
  58. {
  59. unsigned long tmpreg;
  60. __asm__ __volatile__(
  61. "mvfc %0, psw; \n\t"
  62. "or3 %0, %0, #0x0040; \n\t"
  63. "mvtc %0, psw; \n\t"
  64. : "=&r" (tmpreg) : : "cbit", "memory");
  65. }
  66. static inline void local_irq_disable(void)
  67. {
  68. unsigned long tmpreg0, tmpreg1;
  69. __asm__ __volatile__(
  70. "ld24 %0, #0 ; Use 32-bit insn. \n\t"
  71. "mvfc %1, psw ; No interrupt can be accepted here. \n\t"
  72. "mvtc %0, psw \n\t"
  73. "and3 %0, %1, #0xffbf \n\t"
  74. "mvtc %0, psw \n\t"
  75. : "=&r" (tmpreg0), "=&r" (tmpreg1) : : "cbit", "memory");
  76. }
  77. #endif /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
  78. #define local_save_flags(x) \
  79. __asm__ __volatile__("mvfc %0,psw" : "=r"(x) : /* no input */)
  80. #define local_irq_restore(x) \
  81. __asm__ __volatile__("mvtc %0,psw" : /* no outputs */ \
  82. : "r" (x) : "cbit", "memory")
  83. #if !(defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_M32104))
  84. #define local_irq_save(x) \
  85. __asm__ __volatile__( \
  86. "mvfc %0, psw; \n\t" \
  87. "clrpsw #0x40 -> nop; \n\t" \
  88. : "=r" (x) : /* no input */ : "memory")
  89. #else /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
  90. #define local_irq_save(x) \
  91. ({ \
  92. unsigned long tmpreg; \
  93. __asm__ __volatile__( \
  94. "ld24 %1, #0 \n\t" \
  95. "mvfc %0, psw \n\t" \
  96. "mvtc %1, psw \n\t" \
  97. "and3 %1, %0, #0xffbf \n\t" \
  98. "mvtc %1, psw \n\t" \
  99. : "=r" (x), "=&r" (tmpreg) \
  100. : : "cbit", "memory"); \
  101. })
  102. #endif /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
  103. #define irqs_disabled() \
  104. ({ \
  105. unsigned long flags; \
  106. local_save_flags(flags); \
  107. !(flags & 0x40); \
  108. })
  109. #define nop() __asm__ __volatile__ ("nop" : : )
  110. #define xchg(ptr,x) \
  111. ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  112. #ifdef CONFIG_SMP
  113. extern void __xchg_called_with_bad_pointer(void);
  114. #endif
  115. #ifdef CONFIG_CHIP_M32700_TS1
  116. #define DCACHE_CLEAR(reg0, reg1, addr) \
  117. "seth "reg1", #high(dcache_dummy); \n\t" \
  118. "or3 "reg1", "reg1", #low(dcache_dummy); \n\t" \
  119. "lock "reg0", @"reg1"; \n\t" \
  120. "add3 "reg0", "addr", #0x1000; \n\t" \
  121. "ld "reg0", @"reg0"; \n\t" \
  122. "add3 "reg0", "addr", #0x2000; \n\t" \
  123. "ld "reg0", @"reg0"; \n\t" \
  124. "unlock "reg0", @"reg1"; \n\t"
  125. /* FIXME: This workaround code cannot handle kernel modules
  126. * correctly under SMP environment.
  127. */
  128. #else /* CONFIG_CHIP_M32700_TS1 */
  129. #define DCACHE_CLEAR(reg0, reg1, addr)
  130. #endif /* CONFIG_CHIP_M32700_TS1 */
  131. static __always_inline unsigned long
  132. __xchg(unsigned long x, volatile void * ptr, int size)
  133. {
  134. unsigned long flags;
  135. unsigned long tmp = 0;
  136. local_irq_save(flags);
  137. switch (size) {
  138. #ifndef CONFIG_SMP
  139. case 1:
  140. __asm__ __volatile__ (
  141. "ldb %0, @%2 \n\t"
  142. "stb %1, @%2 \n\t"
  143. : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
  144. break;
  145. case 2:
  146. __asm__ __volatile__ (
  147. "ldh %0, @%2 \n\t"
  148. "sth %1, @%2 \n\t"
  149. : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
  150. break;
  151. case 4:
  152. __asm__ __volatile__ (
  153. "ld %0, @%2 \n\t"
  154. "st %1, @%2 \n\t"
  155. : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
  156. break;
  157. #else /* CONFIG_SMP */
  158. case 4:
  159. __asm__ __volatile__ (
  160. DCACHE_CLEAR("%0", "r4", "%2")
  161. "lock %0, @%2; \n\t"
  162. "unlock %1, @%2; \n\t"
  163. : "=&r" (tmp) : "r" (x), "r" (ptr)
  164. : "memory"
  165. #ifdef CONFIG_CHIP_M32700_TS1
  166. , "r4"
  167. #endif /* CONFIG_CHIP_M32700_TS1 */
  168. );
  169. break;
  170. default:
  171. __xchg_called_with_bad_pointer();
  172. #endif /* CONFIG_SMP */
  173. }
  174. local_irq_restore(flags);
  175. return (tmp);
  176. }
  177. #define __HAVE_ARCH_CMPXCHG 1
  178. static inline unsigned long
  179. __cmpxchg_u32(volatile unsigned int *p, unsigned int old, unsigned int new)
  180. {
  181. unsigned long flags;
  182. unsigned int retval;
  183. local_irq_save(flags);
  184. __asm__ __volatile__ (
  185. DCACHE_CLEAR("%0", "r4", "%1")
  186. M32R_LOCK" %0, @%1; \n"
  187. " bne %0, %2, 1f; \n"
  188. M32R_UNLOCK" %3, @%1; \n"
  189. " bra 2f; \n"
  190. " .fillinsn \n"
  191. "1:"
  192. M32R_UNLOCK" %0, @%1; \n"
  193. " .fillinsn \n"
  194. "2:"
  195. : "=&r" (retval)
  196. : "r" (p), "r" (old), "r" (new)
  197. : "cbit", "memory"
  198. #ifdef CONFIG_CHIP_M32700_TS1
  199. , "r4"
  200. #endif /* CONFIG_CHIP_M32700_TS1 */
  201. );
  202. local_irq_restore(flags);
  203. return retval;
  204. }
  205. /* This function doesn't exist, so you'll get a linker error
  206. if something tries to do an invalid cmpxchg(). */
  207. extern void __cmpxchg_called_with_bad_pointer(void);
  208. static inline unsigned long
  209. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  210. {
  211. switch (size) {
  212. case 4:
  213. return __cmpxchg_u32(ptr, old, new);
  214. #if 0 /* we don't have __cmpxchg_u64 */
  215. case 8:
  216. return __cmpxchg_u64(ptr, old, new);
  217. #endif /* 0 */
  218. }
  219. __cmpxchg_called_with_bad_pointer();
  220. return old;
  221. }
  222. #define cmpxchg(ptr,o,n) \
  223. ({ \
  224. __typeof__(*(ptr)) _o_ = (o); \
  225. __typeof__(*(ptr)) _n_ = (n); \
  226. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  227. (unsigned long)_n_, sizeof(*(ptr))); \
  228. })
  229. #endif /* __KERNEL__ */
  230. /*
  231. * Memory barrier.
  232. *
  233. * mb() prevents loads and stores being reordered across this point.
  234. * rmb() prevents loads being reordered across this point.
  235. * wmb() prevents stores being reordered across this point.
  236. */
  237. #define mb() barrier()
  238. #define rmb() mb()
  239. #define wmb() mb()
  240. /**
  241. * read_barrier_depends - Flush all pending reads that subsequents reads
  242. * depend on.
  243. *
  244. * No data-dependent reads from memory-like regions are ever reordered
  245. * over this barrier. All reads preceding this primitive are guaranteed
  246. * to access memory (but not necessarily other CPUs' caches) before any
  247. * reads following this primitive that depend on the data return by
  248. * any of the preceding reads. This primitive is much lighter weight than
  249. * rmb() on most CPUs, and is never heavier weight than is
  250. * rmb().
  251. *
  252. * These ordering constraints are respected by both the local CPU
  253. * and the compiler.
  254. *
  255. * Ordering is not guaranteed by anything other than these primitives,
  256. * not even by data dependencies. See the documentation for
  257. * memory_barrier() for examples and URLs to more information.
  258. *
  259. * For example, the following code would force ordering (the initial
  260. * value of "a" is zero, "b" is one, and "p" is "&a"):
  261. *
  262. * <programlisting>
  263. * CPU 0 CPU 1
  264. *
  265. * b = 2;
  266. * memory_barrier();
  267. * p = &b; q = p;
  268. * read_barrier_depends();
  269. * d = *q;
  270. * </programlisting>
  271. *
  272. *
  273. * because the read of "*q" depends on the read of "p" and these
  274. * two reads are separated by a read_barrier_depends(). However,
  275. * the following code, with the same initial values for "a" and "b":
  276. *
  277. * <programlisting>
  278. * CPU 0 CPU 1
  279. *
  280. * a = 2;
  281. * memory_barrier();
  282. * b = 3; y = b;
  283. * read_barrier_depends();
  284. * x = a;
  285. * </programlisting>
  286. *
  287. * does not enforce ordering, since there is no data dependency between
  288. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  289. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  290. * in cases like this where there are no data dependencies.
  291. **/
  292. #define read_barrier_depends() do { } while (0)
  293. #ifdef CONFIG_SMP
  294. #define smp_mb() mb()
  295. #define smp_rmb() rmb()
  296. #define smp_wmb() wmb()
  297. #define smp_read_barrier_depends() read_barrier_depends()
  298. #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
  299. #else
  300. #define smp_mb() barrier()
  301. #define smp_rmb() barrier()
  302. #define smp_wmb() barrier()
  303. #define smp_read_barrier_depends() do { } while (0)
  304. #define set_mb(var, value) do { var = value; barrier(); } while (0)
  305. #endif
  306. #define arch_align_stack(x) (x)
  307. #endif /* _ASM_M32R_SYSTEM_H */