system.h 9.3 KB

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