r4kcache.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Inline assembly cache operations.
  7. *
  8. * Copyright (C) 1996 David S. Miller ([email protected])
  9. * Copyright (C) 1997 - 2002 Ralf Baechle ([email protected])
  10. * Copyright (C) 2004 Ralf Baechle ([email protected])
  11. */
  12. #ifndef _ASM_R4KCACHE_H
  13. #define _ASM_R4KCACHE_H
  14. #include <linux/stringify.h>
  15. #include <asm/asm.h>
  16. #include <asm/asm-eva.h>
  17. #include <asm/cacheops.h>
  18. #include <asm/compiler.h>
  19. #include <asm/cpu-features.h>
  20. #include <asm/cpu-type.h>
  21. #include <asm/mipsmtregs.h>
  22. #include <asm/mmzone.h>
  23. #include <asm/unroll.h>
  24. extern void (*r4k_blast_dcache)(void);
  25. extern void (*r4k_blast_icache)(void);
  26. /*
  27. * This macro return a properly sign-extended address suitable as base address
  28. * for indexed cache operations. Two issues here:
  29. *
  30. * - The MIPS32 and MIPS64 specs permit an implementation to directly derive
  31. * the index bits from the virtual address. This breaks with tradition
  32. * set by the R4000. To keep unpleasant surprises from happening we pick
  33. * an address in KSEG0 / CKSEG0.
  34. * - We need a properly sign extended address for 64-bit code. To get away
  35. * without ifdefs we let the compiler do it by a type cast.
  36. */
  37. #define INDEX_BASE CKSEG0
  38. #define _cache_op(insn, op, addr) \
  39. __asm__ __volatile__( \
  40. " .set push \n" \
  41. " .set noreorder \n" \
  42. " .set "MIPS_ISA_ARCH_LEVEL" \n" \
  43. " " insn("%0", "%1") " \n" \
  44. " .set pop \n" \
  45. : \
  46. : "i" (op), "R" (*(unsigned char *)(addr)))
  47. #define cache_op(op, addr) \
  48. _cache_op(kernel_cache, op, addr)
  49. static inline void flush_icache_line_indexed(unsigned long addr)
  50. {
  51. cache_op(Index_Invalidate_I, addr);
  52. }
  53. static inline void flush_dcache_line_indexed(unsigned long addr)
  54. {
  55. cache_op(Index_Writeback_Inv_D, addr);
  56. }
  57. static inline void flush_scache_line_indexed(unsigned long addr)
  58. {
  59. cache_op(Index_Writeback_Inv_SD, addr);
  60. }
  61. static inline void flush_icache_line(unsigned long addr)
  62. {
  63. switch (boot_cpu_type()) {
  64. case CPU_LOONGSON2EF:
  65. cache_op(Hit_Invalidate_I_Loongson2, addr);
  66. break;
  67. default:
  68. cache_op(Hit_Invalidate_I, addr);
  69. break;
  70. }
  71. }
  72. static inline void flush_dcache_line(unsigned long addr)
  73. {
  74. cache_op(Hit_Writeback_Inv_D, addr);
  75. }
  76. static inline void invalidate_dcache_line(unsigned long addr)
  77. {
  78. cache_op(Hit_Invalidate_D, addr);
  79. }
  80. static inline void invalidate_scache_line(unsigned long addr)
  81. {
  82. cache_op(Hit_Invalidate_SD, addr);
  83. }
  84. static inline void flush_scache_line(unsigned long addr)
  85. {
  86. cache_op(Hit_Writeback_Inv_SD, addr);
  87. }
  88. #ifdef CONFIG_EVA
  89. #define protected_cache_op(op, addr) \
  90. ({ \
  91. int __err = 0; \
  92. __asm__ __volatile__( \
  93. " .set push \n" \
  94. " .set noreorder \n" \
  95. " .set mips0 \n" \
  96. " .set eva \n" \
  97. "1: cachee %1, (%2) \n" \
  98. "2: .insn \n" \
  99. " .set pop \n" \
  100. " .section .fixup,\"ax\" \n" \
  101. "3: li %0, %3 \n" \
  102. " j 2b \n" \
  103. " .previous \n" \
  104. " .section __ex_table,\"a\" \n" \
  105. " "STR(PTR_WD)" 1b, 3b \n" \
  106. " .previous" \
  107. : "+r" (__err) \
  108. : "i" (op), "r" (addr), "i" (-EFAULT)); \
  109. __err; \
  110. })
  111. #else
  112. #define protected_cache_op(op, addr) \
  113. ({ \
  114. int __err = 0; \
  115. __asm__ __volatile__( \
  116. " .set push \n" \
  117. " .set noreorder \n" \
  118. " .set "MIPS_ISA_ARCH_LEVEL" \n" \
  119. "1: cache %1, (%2) \n" \
  120. "2: .insn \n" \
  121. " .set pop \n" \
  122. " .section .fixup,\"ax\" \n" \
  123. "3: li %0, %3 \n" \
  124. " j 2b \n" \
  125. " .previous \n" \
  126. " .section __ex_table,\"a\" \n" \
  127. " "STR(PTR_WD)" 1b, 3b \n" \
  128. " .previous" \
  129. : "+r" (__err) \
  130. : "i" (op), "r" (addr), "i" (-EFAULT)); \
  131. __err; \
  132. })
  133. #endif
  134. /*
  135. * The next two are for badland addresses like signal trampolines.
  136. */
  137. static inline int protected_flush_icache_line(unsigned long addr)
  138. {
  139. switch (boot_cpu_type()) {
  140. case CPU_LOONGSON2EF:
  141. return protected_cache_op(Hit_Invalidate_I_Loongson2, addr);
  142. default:
  143. return protected_cache_op(Hit_Invalidate_I, addr);
  144. }
  145. }
  146. /*
  147. * R10000 / R12000 hazard - these processors don't support the Hit_Writeback_D
  148. * cacheop so we use Hit_Writeback_Inv_D which is supported by all R4000-style
  149. * caches. We're talking about one cacheline unnecessarily getting invalidated
  150. * here so the penalty isn't overly hard.
  151. */
  152. static inline int protected_writeback_dcache_line(unsigned long addr)
  153. {
  154. return protected_cache_op(Hit_Writeback_Inv_D, addr);
  155. }
  156. static inline int protected_writeback_scache_line(unsigned long addr)
  157. {
  158. return protected_cache_op(Hit_Writeback_Inv_SD, addr);
  159. }
  160. /*
  161. * This one is RM7000-specific
  162. */
  163. static inline void invalidate_tcache_page(unsigned long addr)
  164. {
  165. cache_op(Page_Invalidate_T, addr);
  166. }
  167. #define cache_unroll(times, insn, op, addr, lsize) do { \
  168. int i = 0; \
  169. unroll(times, _cache_op, insn, op, (addr) + (i++ * (lsize))); \
  170. } while (0)
  171. /* build blast_xxx, blast_xxx_page, blast_xxx_page_indexed */
  172. #define __BUILD_BLAST_CACHE(pfx, desc, indexop, hitop, lsize, extra) \
  173. static inline void extra##blast_##pfx##cache##lsize(void) \
  174. { \
  175. unsigned long start = INDEX_BASE; \
  176. unsigned long end = start + current_cpu_data.desc.waysize; \
  177. unsigned long ws_inc = 1UL << current_cpu_data.desc.waybit; \
  178. unsigned long ws_end = current_cpu_data.desc.ways << \
  179. current_cpu_data.desc.waybit; \
  180. unsigned long ws, addr; \
  181. \
  182. for (ws = 0; ws < ws_end; ws += ws_inc) \
  183. for (addr = start; addr < end; addr += lsize * 32) \
  184. cache_unroll(32, kernel_cache, indexop, \
  185. addr | ws, lsize); \
  186. } \
  187. \
  188. static inline void extra##blast_##pfx##cache##lsize##_page(unsigned long page) \
  189. { \
  190. unsigned long start = page; \
  191. unsigned long end = page + PAGE_SIZE; \
  192. \
  193. do { \
  194. cache_unroll(32, kernel_cache, hitop, start, lsize); \
  195. start += lsize * 32; \
  196. } while (start < end); \
  197. } \
  198. \
  199. static inline void extra##blast_##pfx##cache##lsize##_page_indexed(unsigned long page) \
  200. { \
  201. unsigned long indexmask = current_cpu_data.desc.waysize - 1; \
  202. unsigned long start = INDEX_BASE + (page & indexmask); \
  203. unsigned long end = start + PAGE_SIZE; \
  204. unsigned long ws_inc = 1UL << current_cpu_data.desc.waybit; \
  205. unsigned long ws_end = current_cpu_data.desc.ways << \
  206. current_cpu_data.desc.waybit; \
  207. unsigned long ws, addr; \
  208. \
  209. for (ws = 0; ws < ws_end; ws += ws_inc) \
  210. for (addr = start; addr < end; addr += lsize * 32) \
  211. cache_unroll(32, kernel_cache, indexop, \
  212. addr | ws, lsize); \
  213. }
  214. __BUILD_BLAST_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D, 16, )
  215. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 16, )
  216. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 16, )
  217. __BUILD_BLAST_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D, 32, )
  218. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 32, )
  219. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I_Loongson2, 32, loongson2_)
  220. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 32, )
  221. __BUILD_BLAST_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D, 64, )
  222. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 64, )
  223. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 64, )
  224. __BUILD_BLAST_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D, 128, )
  225. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 128, )
  226. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 128, )
  227. __BUILD_BLAST_CACHE(inv_d, dcache, Index_Writeback_Inv_D, Hit_Invalidate_D, 16, )
  228. __BUILD_BLAST_CACHE(inv_d, dcache, Index_Writeback_Inv_D, Hit_Invalidate_D, 32, )
  229. __BUILD_BLAST_CACHE(inv_s, scache, Index_Writeback_Inv_SD, Hit_Invalidate_SD, 16, )
  230. __BUILD_BLAST_CACHE(inv_s, scache, Index_Writeback_Inv_SD, Hit_Invalidate_SD, 32, )
  231. __BUILD_BLAST_CACHE(inv_s, scache, Index_Writeback_Inv_SD, Hit_Invalidate_SD, 64, )
  232. __BUILD_BLAST_CACHE(inv_s, scache, Index_Writeback_Inv_SD, Hit_Invalidate_SD, 128, )
  233. #define __BUILD_BLAST_USER_CACHE(pfx, desc, indexop, hitop, lsize) \
  234. static inline void blast_##pfx##cache##lsize##_user_page(unsigned long page) \
  235. { \
  236. unsigned long start = page; \
  237. unsigned long end = page + PAGE_SIZE; \
  238. \
  239. do { \
  240. cache_unroll(32, user_cache, hitop, start, lsize); \
  241. start += lsize * 32; \
  242. } while (start < end); \
  243. }
  244. __BUILD_BLAST_USER_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D,
  245. 16)
  246. __BUILD_BLAST_USER_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 16)
  247. __BUILD_BLAST_USER_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D,
  248. 32)
  249. __BUILD_BLAST_USER_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 32)
  250. __BUILD_BLAST_USER_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D,
  251. 64)
  252. __BUILD_BLAST_USER_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 64)
  253. /* build blast_xxx_range, protected_blast_xxx_range */
  254. #define __BUILD_BLAST_CACHE_RANGE(pfx, desc, hitop, prot, extra) \
  255. static inline void prot##extra##blast_##pfx##cache##_range(unsigned long start, \
  256. unsigned long end) \
  257. { \
  258. unsigned long lsize = cpu_##desc##_line_size(); \
  259. unsigned long addr = start & ~(lsize - 1); \
  260. unsigned long aend = (end - 1) & ~(lsize - 1); \
  261. \
  262. while (1) { \
  263. prot##cache_op(hitop, addr); \
  264. if (addr == aend) \
  265. break; \
  266. addr += lsize; \
  267. } \
  268. }
  269. __BUILD_BLAST_CACHE_RANGE(d, dcache, Hit_Writeback_Inv_D, protected_, )
  270. __BUILD_BLAST_CACHE_RANGE(i, icache, Hit_Invalidate_I, protected_, )
  271. __BUILD_BLAST_CACHE_RANGE(s, scache, Hit_Writeback_Inv_SD, protected_, )
  272. __BUILD_BLAST_CACHE_RANGE(i, icache, Hit_Invalidate_I_Loongson2, \
  273. protected_, loongson2_)
  274. __BUILD_BLAST_CACHE_RANGE(d, dcache, Hit_Writeback_Inv_D, , )
  275. __BUILD_BLAST_CACHE_RANGE(i, icache, Hit_Invalidate_I, , )
  276. __BUILD_BLAST_CACHE_RANGE(s, scache, Hit_Writeback_Inv_SD, , )
  277. /* blast_inv_dcache_range */
  278. __BUILD_BLAST_CACHE_RANGE(inv_d, dcache, Hit_Invalidate_D, , )
  279. __BUILD_BLAST_CACHE_RANGE(inv_s, scache, Hit_Invalidate_SD, , )
  280. /* Currently, this is very specific to Loongson-3 */
  281. #define __BUILD_BLAST_CACHE_NODE(pfx, desc, indexop, hitop, lsize) \
  282. static inline void blast_##pfx##cache##lsize##_node(long node) \
  283. { \
  284. unsigned long start = CAC_BASE | nid_to_addrbase(node); \
  285. unsigned long end = start + current_cpu_data.desc.waysize; \
  286. unsigned long ws_inc = 1UL << current_cpu_data.desc.waybit; \
  287. unsigned long ws_end = current_cpu_data.desc.ways << \
  288. current_cpu_data.desc.waybit; \
  289. unsigned long ws, addr; \
  290. \
  291. for (ws = 0; ws < ws_end; ws += ws_inc) \
  292. for (addr = start; addr < end; addr += lsize * 32) \
  293. cache_unroll(32, kernel_cache, indexop, \
  294. addr | ws, lsize); \
  295. }
  296. __BUILD_BLAST_CACHE_NODE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 16)
  297. __BUILD_BLAST_CACHE_NODE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 32)
  298. __BUILD_BLAST_CACHE_NODE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 64)
  299. __BUILD_BLAST_CACHE_NODE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 128)
  300. #endif /* _ASM_R4KCACHE_H */