system.h 9.6 KB

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