system.h 9.5 KB

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