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