system.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_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. #define xchg_local(ptr, x) \
  113. ((__typeof__(*(ptr)))__xchg_local((unsigned long)(x), (ptr), \
  114. sizeof(*(ptr))))
  115. extern void __xchg_called_with_bad_pointer(void);
  116. #ifdef CONFIG_CHIP_M32700_TS1
  117. #define DCACHE_CLEAR(reg0, reg1, addr) \
  118. "seth "reg1", #high(dcache_dummy); \n\t" \
  119. "or3 "reg1", "reg1", #low(dcache_dummy); \n\t" \
  120. "lock "reg0", @"reg1"; \n\t" \
  121. "add3 "reg0", "addr", #0x1000; \n\t" \
  122. "ld "reg0", @"reg0"; \n\t" \
  123. "add3 "reg0", "addr", #0x2000; \n\t" \
  124. "ld "reg0", @"reg0"; \n\t" \
  125. "unlock "reg0", @"reg1"; \n\t"
  126. /* FIXME: This workaround code cannot handle kernel modules
  127. * correctly under SMP environment.
  128. */
  129. #else /* CONFIG_CHIP_M32700_TS1 */
  130. #define DCACHE_CLEAR(reg0, reg1, addr)
  131. #endif /* CONFIG_CHIP_M32700_TS1 */
  132. static __always_inline unsigned long
  133. __xchg(unsigned long x, volatile void *ptr, int size)
  134. {
  135. unsigned long flags;
  136. unsigned long tmp = 0;
  137. local_irq_save(flags);
  138. switch (size) {
  139. #ifndef CONFIG_SMP
  140. case 1:
  141. __asm__ __volatile__ (
  142. "ldb %0, @%2 \n\t"
  143. "stb %1, @%2 \n\t"
  144. : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
  145. break;
  146. case 2:
  147. __asm__ __volatile__ (
  148. "ldh %0, @%2 \n\t"
  149. "sth %1, @%2 \n\t"
  150. : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
  151. break;
  152. case 4:
  153. __asm__ __volatile__ (
  154. "ld %0, @%2 \n\t"
  155. "st %1, @%2 \n\t"
  156. : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
  157. break;
  158. #else /* CONFIG_SMP */
  159. case 4:
  160. __asm__ __volatile__ (
  161. DCACHE_CLEAR("%0", "r4", "%2")
  162. "lock %0, @%2; \n\t"
  163. "unlock %1, @%2; \n\t"
  164. : "=&r" (tmp) : "r" (x), "r" (ptr)
  165. : "memory"
  166. #ifdef CONFIG_CHIP_M32700_TS1
  167. , "r4"
  168. #endif /* CONFIG_CHIP_M32700_TS1 */
  169. );
  170. break;
  171. #endif /* CONFIG_SMP */
  172. default:
  173. __xchg_called_with_bad_pointer();
  174. }
  175. local_irq_restore(flags);
  176. return (tmp);
  177. }
  178. static __always_inline unsigned long
  179. __xchg_local(unsigned long x, volatile void *ptr, int size)
  180. {
  181. unsigned long flags;
  182. unsigned long tmp = 0;
  183. local_irq_save(flags);
  184. switch (size) {
  185. case 1:
  186. __asm__ __volatile__ (
  187. "ldb %0, @%2 \n\t"
  188. "stb %1, @%2 \n\t"
  189. : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
  190. break;
  191. case 2:
  192. __asm__ __volatile__ (
  193. "ldh %0, @%2 \n\t"
  194. "sth %1, @%2 \n\t"
  195. : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
  196. break;
  197. case 4:
  198. __asm__ __volatile__ (
  199. "ld %0, @%2 \n\t"
  200. "st %1, @%2 \n\t"
  201. : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
  202. break;
  203. default:
  204. __xchg_called_with_bad_pointer();
  205. }
  206. local_irq_restore(flags);
  207. return (tmp);
  208. }
  209. #define __HAVE_ARCH_CMPXCHG 1
  210. static inline unsigned long
  211. __cmpxchg_u32(volatile unsigned int *p, unsigned int old, unsigned int new)
  212. {
  213. unsigned long flags;
  214. unsigned int retval;
  215. local_irq_save(flags);
  216. __asm__ __volatile__ (
  217. DCACHE_CLEAR("%0", "r4", "%1")
  218. M32R_LOCK" %0, @%1; \n"
  219. " bne %0, %2, 1f; \n"
  220. M32R_UNLOCK" %3, @%1; \n"
  221. " bra 2f; \n"
  222. " .fillinsn \n"
  223. "1:"
  224. M32R_UNLOCK" %0, @%1; \n"
  225. " .fillinsn \n"
  226. "2:"
  227. : "=&r" (retval)
  228. : "r" (p), "r" (old), "r" (new)
  229. : "cbit", "memory"
  230. #ifdef CONFIG_CHIP_M32700_TS1
  231. , "r4"
  232. #endif /* CONFIG_CHIP_M32700_TS1 */
  233. );
  234. local_irq_restore(flags);
  235. return retval;
  236. }
  237. static inline unsigned long
  238. __cmpxchg_local_u32(volatile unsigned int *p, unsigned int old,
  239. unsigned int new)
  240. {
  241. unsigned long flags;
  242. unsigned int retval;
  243. local_irq_save(flags);
  244. __asm__ __volatile__ (
  245. DCACHE_CLEAR("%0", "r4", "%1")
  246. "ld %0, @%1; \n"
  247. " bne %0, %2, 1f; \n"
  248. "st %3, @%1; \n"
  249. " bra 2f; \n"
  250. " .fillinsn \n"
  251. "1:"
  252. "st %0, @%1; \n"
  253. " .fillinsn \n"
  254. "2:"
  255. : "=&r" (retval)
  256. : "r" (p), "r" (old), "r" (new)
  257. : "cbit", "memory"
  258. #ifdef CONFIG_CHIP_M32700_TS1
  259. , "r4"
  260. #endif /* CONFIG_CHIP_M32700_TS1 */
  261. );
  262. local_irq_restore(flags);
  263. return retval;
  264. }
  265. /* This function doesn't exist, so you'll get a linker error
  266. if something tries to do an invalid cmpxchg(). */
  267. extern void __cmpxchg_called_with_bad_pointer(void);
  268. static inline unsigned long
  269. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  270. {
  271. switch (size) {
  272. case 4:
  273. return __cmpxchg_u32(ptr, old, new);
  274. #if 0 /* we don't have __cmpxchg_u64 */
  275. case 8:
  276. return __cmpxchg_u64(ptr, old, new);
  277. #endif /* 0 */
  278. }
  279. __cmpxchg_called_with_bad_pointer();
  280. return old;
  281. }
  282. #define cmpxchg(ptr, o, n) \
  283. ((__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)(o), \
  284. (unsigned long)(n), sizeof(*(ptr))))
  285. #include <asm-generic/cmpxchg-local.h>
  286. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  287. unsigned long old,
  288. unsigned long new, int size)
  289. {
  290. switch (size) {
  291. case 4:
  292. return __cmpxchg_local_u32(ptr, old, new);
  293. default:
  294. return __cmpxchg_local_generic(ptr, old, new, size);
  295. }
  296. return old;
  297. }
  298. /*
  299. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  300. * them available.
  301. */
  302. #define cmpxchg_local(ptr, o, n) \
  303. ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
  304. (unsigned long)(n), sizeof(*(ptr))))
  305. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  306. #endif /* __KERNEL__ */
  307. /*
  308. * Memory barrier.
  309. *
  310. * mb() prevents loads and stores being reordered across this point.
  311. * rmb() prevents loads being reordered across this point.
  312. * wmb() prevents stores being reordered across this point.
  313. */
  314. #define mb() barrier()
  315. #define rmb() mb()
  316. #define wmb() mb()
  317. /**
  318. * read_barrier_depends - Flush all pending reads that subsequents reads
  319. * depend on.
  320. *
  321. * No data-dependent reads from memory-like regions are ever reordered
  322. * over this barrier. All reads preceding this primitive are guaranteed
  323. * to access memory (but not necessarily other CPUs' caches) before any
  324. * reads following this primitive that depend on the data return by
  325. * any of the preceding reads. This primitive is much lighter weight than
  326. * rmb() on most CPUs, and is never heavier weight than is
  327. * rmb().
  328. *
  329. * These ordering constraints are respected by both the local CPU
  330. * and the compiler.
  331. *
  332. * Ordering is not guaranteed by anything other than these primitives,
  333. * not even by data dependencies. See the documentation for
  334. * memory_barrier() for examples and URLs to more information.
  335. *
  336. * For example, the following code would force ordering (the initial
  337. * value of "a" is zero, "b" is one, and "p" is "&a"):
  338. *
  339. * <programlisting>
  340. * CPU 0 CPU 1
  341. *
  342. * b = 2;
  343. * memory_barrier();
  344. * p = &b; q = p;
  345. * read_barrier_depends();
  346. * d = *q;
  347. * </programlisting>
  348. *
  349. *
  350. * because the read of "*q" depends on the read of "p" and these
  351. * two reads are separated by a read_barrier_depends(). However,
  352. * the following code, with the same initial values for "a" and "b":
  353. *
  354. * <programlisting>
  355. * CPU 0 CPU 1
  356. *
  357. * a = 2;
  358. * memory_barrier();
  359. * b = 3; y = b;
  360. * read_barrier_depends();
  361. * x = a;
  362. * </programlisting>
  363. *
  364. * does not enforce ordering, since there is no data dependency between
  365. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  366. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  367. * in cases like this where there are no data dependencies.
  368. **/
  369. #define read_barrier_depends() do { } while (0)
  370. #ifdef CONFIG_SMP
  371. #define smp_mb() mb()
  372. #define smp_rmb() rmb()
  373. #define smp_wmb() wmb()
  374. #define smp_read_barrier_depends() read_barrier_depends()
  375. #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
  376. #else
  377. #define smp_mb() barrier()
  378. #define smp_rmb() barrier()
  379. #define smp_wmb() barrier()
  380. #define smp_read_barrier_depends() do { } while (0)
  381. #define set_mb(var, value) do { var = value; barrier(); } while (0)
  382. #endif
  383. #define arch_align_stack(x) (x)
  384. #endif /* _ASM_M32R_SYSTEM_H */