r4kcache.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 (dm@engr.sgi.com)
  9. * Copyright (C) 1997 - 2002 Ralf Baechle (ralf@gnu.org)
  10. * Copyright (C) 2004 Ralf Baechle (ralf@linux-mips.org)
  11. */
  12. #ifndef _ASM_R4KCACHE_H
  13. #define _ASM_R4KCACHE_H
  14. #include <asm/asm.h>
  15. #include <asm/cacheops.h>
  16. #include <asm/cpu-features.h>
  17. /*
  18. * This macro return a properly sign-extended address suitable as base address
  19. * for indexed cache operations. Two issues here:
  20. *
  21. * - The MIPS32 and MIPS64 specs permit an implementation to directly derive
  22. * the index bits from the virtual address. This breaks with tradition
  23. * set by the R4000. To keep unpleasant surprises from happening we pick
  24. * an address in KSEG0 / CKSEG0.
  25. * - We need a properly sign extended address for 64-bit code. To get away
  26. * without ifdefs we let the compiler do it by a type cast.
  27. */
  28. #define INDEX_BASE CKSEG0
  29. #define cache_op(op,addr) \
  30. __asm__ __volatile__( \
  31. " .set push \n" \
  32. " .set noreorder \n" \
  33. " .set mips3\n\t \n" \
  34. " cache %0, %1 \n" \
  35. " .set pop \n" \
  36. : \
  37. : "i" (op), "m" (*(unsigned char *)(addr)))
  38. static inline void flush_icache_line_indexed(unsigned long addr)
  39. {
  40. cache_op(Index_Invalidate_I, addr);
  41. }
  42. static inline void flush_dcache_line_indexed(unsigned long addr)
  43. {
  44. cache_op(Index_Writeback_Inv_D, addr);
  45. }
  46. static inline void flush_scache_line_indexed(unsigned long addr)
  47. {
  48. cache_op(Index_Writeback_Inv_SD, addr);
  49. }
  50. static inline void flush_icache_line(unsigned long addr)
  51. {
  52. cache_op(Hit_Invalidate_I, addr);
  53. }
  54. static inline void flush_dcache_line(unsigned long addr)
  55. {
  56. cache_op(Hit_Writeback_Inv_D, addr);
  57. }
  58. static inline void invalidate_dcache_line(unsigned long addr)
  59. {
  60. cache_op(Hit_Invalidate_D, addr);
  61. }
  62. static inline void invalidate_scache_line(unsigned long addr)
  63. {
  64. cache_op(Hit_Invalidate_SD, addr);
  65. }
  66. static inline void flush_scache_line(unsigned long addr)
  67. {
  68. cache_op(Hit_Writeback_Inv_SD, addr);
  69. }
  70. #define protected_cache_op(op,addr) \
  71. __asm__ __volatile__( \
  72. " .set push \n" \
  73. " .set noreorder \n" \
  74. " .set mips3 \n" \
  75. "1: cache %0, (%1) \n" \
  76. "2: .set pop \n" \
  77. " .section __ex_table,\"a\" \n" \
  78. " "STR(PTR)" 1b, 2b \n" \
  79. " .previous" \
  80. : \
  81. : "i" (op), "r" (addr))
  82. /*
  83. * The next two are for badland addresses like signal trampolines.
  84. */
  85. static inline void protected_flush_icache_line(unsigned long addr)
  86. {
  87. protected_cache_op(Hit_Invalidate_I, addr);
  88. }
  89. /*
  90. * R10000 / R12000 hazard - these processors don't support the Hit_Writeback_D
  91. * cacheop so we use Hit_Writeback_Inv_D which is supported by all R4000-style
  92. * caches. We're talking about one cacheline unnecessarily getting invalidated
  93. * here so the penalty isn't overly hard.
  94. */
  95. static inline void protected_writeback_dcache_line(unsigned long addr)
  96. {
  97. protected_cache_op(Hit_Writeback_Inv_D, addr);
  98. }
  99. static inline void protected_writeback_scache_line(unsigned long addr)
  100. {
  101. protected_cache_op(Hit_Writeback_Inv_SD, addr);
  102. }
  103. /*
  104. * This one is RM7000-specific
  105. */
  106. static inline void invalidate_tcache_page(unsigned long addr)
  107. {
  108. cache_op(Page_Invalidate_T, addr);
  109. }
  110. #define cache16_unroll32(base,op) \
  111. __asm__ __volatile__( \
  112. " .set push \n" \
  113. " .set noreorder \n" \
  114. " .set mips3 \n" \
  115. " cache %1, 0x000(%0); cache %1, 0x010(%0) \n" \
  116. " cache %1, 0x020(%0); cache %1, 0x030(%0) \n" \
  117. " cache %1, 0x040(%0); cache %1, 0x050(%0) \n" \
  118. " cache %1, 0x060(%0); cache %1, 0x070(%0) \n" \
  119. " cache %1, 0x080(%0); cache %1, 0x090(%0) \n" \
  120. " cache %1, 0x0a0(%0); cache %1, 0x0b0(%0) \n" \
  121. " cache %1, 0x0c0(%0); cache %1, 0x0d0(%0) \n" \
  122. " cache %1, 0x0e0(%0); cache %1, 0x0f0(%0) \n" \
  123. " cache %1, 0x100(%0); cache %1, 0x110(%0) \n" \
  124. " cache %1, 0x120(%0); cache %1, 0x130(%0) \n" \
  125. " cache %1, 0x140(%0); cache %1, 0x150(%0) \n" \
  126. " cache %1, 0x160(%0); cache %1, 0x170(%0) \n" \
  127. " cache %1, 0x180(%0); cache %1, 0x190(%0) \n" \
  128. " cache %1, 0x1a0(%0); cache %1, 0x1b0(%0) \n" \
  129. " cache %1, 0x1c0(%0); cache %1, 0x1d0(%0) \n" \
  130. " cache %1, 0x1e0(%0); cache %1, 0x1f0(%0) \n" \
  131. " .set pop \n" \
  132. : \
  133. : "r" (base), \
  134. "i" (op));
  135. #define cache32_unroll32(base,op) \
  136. __asm__ __volatile__( \
  137. " .set push \n" \
  138. " .set noreorder \n" \
  139. " .set mips3 \n" \
  140. " cache %1, 0x000(%0); cache %1, 0x020(%0) \n" \
  141. " cache %1, 0x040(%0); cache %1, 0x060(%0) \n" \
  142. " cache %1, 0x080(%0); cache %1, 0x0a0(%0) \n" \
  143. " cache %1, 0x0c0(%0); cache %1, 0x0e0(%0) \n" \
  144. " cache %1, 0x100(%0); cache %1, 0x120(%0) \n" \
  145. " cache %1, 0x140(%0); cache %1, 0x160(%0) \n" \
  146. " cache %1, 0x180(%0); cache %1, 0x1a0(%0) \n" \
  147. " cache %1, 0x1c0(%0); cache %1, 0x1e0(%0) \n" \
  148. " cache %1, 0x200(%0); cache %1, 0x220(%0) \n" \
  149. " cache %1, 0x240(%0); cache %1, 0x260(%0) \n" \
  150. " cache %1, 0x280(%0); cache %1, 0x2a0(%0) \n" \
  151. " cache %1, 0x2c0(%0); cache %1, 0x2e0(%0) \n" \
  152. " cache %1, 0x300(%0); cache %1, 0x320(%0) \n" \
  153. " cache %1, 0x340(%0); cache %1, 0x360(%0) \n" \
  154. " cache %1, 0x380(%0); cache %1, 0x3a0(%0) \n" \
  155. " cache %1, 0x3c0(%0); cache %1, 0x3e0(%0) \n" \
  156. " .set pop \n" \
  157. : \
  158. : "r" (base), \
  159. "i" (op));
  160. #define cache64_unroll32(base,op) \
  161. __asm__ __volatile__( \
  162. " .set push \n" \
  163. " .set noreorder \n" \
  164. " .set mips3 \n" \
  165. " cache %1, 0x000(%0); cache %1, 0x040(%0) \n" \
  166. " cache %1, 0x080(%0); cache %1, 0x0c0(%0) \n" \
  167. " cache %1, 0x100(%0); cache %1, 0x140(%0) \n" \
  168. " cache %1, 0x180(%0); cache %1, 0x1c0(%0) \n" \
  169. " cache %1, 0x200(%0); cache %1, 0x240(%0) \n" \
  170. " cache %1, 0x280(%0); cache %1, 0x2c0(%0) \n" \
  171. " cache %1, 0x300(%0); cache %1, 0x340(%0) \n" \
  172. " cache %1, 0x380(%0); cache %1, 0x3c0(%0) \n" \
  173. " cache %1, 0x400(%0); cache %1, 0x440(%0) \n" \
  174. " cache %1, 0x480(%0); cache %1, 0x4c0(%0) \n" \
  175. " cache %1, 0x500(%0); cache %1, 0x540(%0) \n" \
  176. " cache %1, 0x580(%0); cache %1, 0x5c0(%0) \n" \
  177. " cache %1, 0x600(%0); cache %1, 0x640(%0) \n" \
  178. " cache %1, 0x680(%0); cache %1, 0x6c0(%0) \n" \
  179. " cache %1, 0x700(%0); cache %1, 0x740(%0) \n" \
  180. " cache %1, 0x780(%0); cache %1, 0x7c0(%0) \n" \
  181. " .set pop \n" \
  182. : \
  183. : "r" (base), \
  184. "i" (op));
  185. #define cache128_unroll32(base,op) \
  186. __asm__ __volatile__( \
  187. " .set push \n" \
  188. " .set noreorder \n" \
  189. " .set mips3 \n" \
  190. " cache %1, 0x000(%0); cache %1, 0x080(%0) \n" \
  191. " cache %1, 0x100(%0); cache %1, 0x180(%0) \n" \
  192. " cache %1, 0x200(%0); cache %1, 0x280(%0) \n" \
  193. " cache %1, 0x300(%0); cache %1, 0x380(%0) \n" \
  194. " cache %1, 0x400(%0); cache %1, 0x480(%0) \n" \
  195. " cache %1, 0x500(%0); cache %1, 0x580(%0) \n" \
  196. " cache %1, 0x600(%0); cache %1, 0x680(%0) \n" \
  197. " cache %1, 0x700(%0); cache %1, 0x780(%0) \n" \
  198. " cache %1, 0x800(%0); cache %1, 0x880(%0) \n" \
  199. " cache %1, 0x900(%0); cache %1, 0x980(%0) \n" \
  200. " cache %1, 0xa00(%0); cache %1, 0xa80(%0) \n" \
  201. " cache %1, 0xb00(%0); cache %1, 0xb80(%0) \n" \
  202. " cache %1, 0xc00(%0); cache %1, 0xc80(%0) \n" \
  203. " cache %1, 0xd00(%0); cache %1, 0xd80(%0) \n" \
  204. " cache %1, 0xe00(%0); cache %1, 0xe80(%0) \n" \
  205. " cache %1, 0xf00(%0); cache %1, 0xf80(%0) \n" \
  206. " .set pop \n" \
  207. : \
  208. : "r" (base), \
  209. "i" (op));
  210. /* build blast_xxx, blast_xxx_page, blast_xxx_page_indexed */
  211. #define __BUILD_BLAST_CACHE(pfx, desc, indexop, hitop, lsize) \
  212. static inline void blast_##pfx##cache##lsize(void) \
  213. { \
  214. unsigned long start = INDEX_BASE; \
  215. unsigned long end = start + current_cpu_data.desc.waysize; \
  216. unsigned long ws_inc = 1UL << current_cpu_data.desc.waybit; \
  217. unsigned long ws_end = current_cpu_data.desc.ways << \
  218. current_cpu_data.desc.waybit; \
  219. unsigned long ws, addr; \
  220. \
  221. for (ws = 0; ws < ws_end; ws += ws_inc) \
  222. for (addr = start; addr < end; addr += lsize * 32) \
  223. cache##lsize##_unroll32(addr|ws,indexop); \
  224. } \
  225. \
  226. static inline void blast_##pfx##cache##lsize##_page(unsigned long page) \
  227. { \
  228. unsigned long start = page; \
  229. unsigned long end = page + PAGE_SIZE; \
  230. \
  231. do { \
  232. cache##lsize##_unroll32(start,hitop); \
  233. start += lsize * 32; \
  234. } while (start < end); \
  235. } \
  236. \
  237. static inline void blast_##pfx##cache##lsize##_page_indexed(unsigned long page) \
  238. { \
  239. unsigned long indexmask = current_cpu_data.desc.waysize - 1; \
  240. unsigned long start = INDEX_BASE + (page & indexmask); \
  241. unsigned long end = start + PAGE_SIZE; \
  242. unsigned long ws_inc = 1UL << current_cpu_data.desc.waybit; \
  243. unsigned long ws_end = current_cpu_data.desc.ways << \
  244. current_cpu_data.desc.waybit; \
  245. unsigned long ws, addr; \
  246. \
  247. for (ws = 0; ws < ws_end; ws += ws_inc) \
  248. for (addr = start; addr < end; addr += lsize * 32) \
  249. cache##lsize##_unroll32(addr|ws,indexop); \
  250. }
  251. __BUILD_BLAST_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D, 16)
  252. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 16)
  253. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 16)
  254. __BUILD_BLAST_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D, 32)
  255. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 32)
  256. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 32)
  257. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 64)
  258. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 64)
  259. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 128)
  260. /* build blast_xxx_range, protected_blast_xxx_range */
  261. #define __BUILD_BLAST_CACHE_RANGE(pfx, desc, hitop, prot) \
  262. static inline void prot##blast_##pfx##cache##_range(unsigned long start, \
  263. unsigned long end) \
  264. { \
  265. unsigned long lsize = cpu_##desc##_line_size(); \
  266. unsigned long addr = start & ~(lsize - 1); \
  267. unsigned long aend = (end - 1) & ~(lsize - 1); \
  268. while (1) { \
  269. prot##cache_op(hitop, addr); \
  270. if (addr == aend) \
  271. break; \
  272. addr += lsize; \
  273. } \
  274. }
  275. __BUILD_BLAST_CACHE_RANGE(d, dcache, Hit_Writeback_Inv_D, protected_)
  276. __BUILD_BLAST_CACHE_RANGE(s, scache, Hit_Writeback_Inv_SD, protected_)
  277. __BUILD_BLAST_CACHE_RANGE(i, icache, Hit_Invalidate_I, protected_)
  278. __BUILD_BLAST_CACHE_RANGE(d, dcache, Hit_Writeback_Inv_D, )
  279. __BUILD_BLAST_CACHE_RANGE(s, scache, Hit_Writeback_Inv_SD, )
  280. /* blast_inv_dcache_range */
  281. __BUILD_BLAST_CACHE_RANGE(inv_d, dcache, Hit_Invalidate_D, )
  282. __BUILD_BLAST_CACHE_RANGE(inv_s, scache, Hit_Invalidate_SD, )
  283. #endif /* _ASM_R4KCACHE_H */