system.h 9.6 KB

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