r4kcache.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 (davem@davemloft.net)
  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. #include <asm/mipsmtregs.h>
  18. /*
  19. * This macro return a properly sign-extended address suitable as base address
  20. * for indexed cache operations. Two issues here:
  21. *
  22. * - The MIPS32 and MIPS64 specs permit an implementation to directly derive
  23. * the index bits from the virtual address. This breaks with tradition
  24. * set by the R4000. To keep unpleasant surprises from happening we pick
  25. * an address in KSEG0 / CKSEG0.
  26. * - We need a properly sign extended address for 64-bit code. To get away
  27. * without ifdefs we let the compiler do it by a type cast.
  28. */
  29. #define INDEX_BASE CKSEG0
  30. #define cache_op(op,addr) \
  31. __asm__ __volatile__( \
  32. " .set push \n" \
  33. " .set noreorder \n" \
  34. " .set mips3\n\t \n" \
  35. " cache %0, %1 \n" \
  36. " .set pop \n" \
  37. : \
  38. : "i" (op), "R" (*(unsigned char *)(addr)))
  39. #ifdef CONFIG_MIPS_MT
  40. /*
  41. * Temporary hacks for SMTC debug. Optionally force single-threaded
  42. * execution during I-cache flushes.
  43. */
  44. #define PROTECT_CACHE_FLUSHES 1
  45. #ifdef PROTECT_CACHE_FLUSHES
  46. extern int mt_protiflush;
  47. extern int mt_protdflush;
  48. extern void mt_cflush_lockdown(void);
  49. extern void mt_cflush_release(void);
  50. #define BEGIN_MT_IPROT \
  51. unsigned long flags = 0; \
  52. unsigned long mtflags = 0; \
  53. if(mt_protiflush) { \
  54. local_irq_save(flags); \
  55. ehb(); \
  56. mtflags = dvpe(); \
  57. mt_cflush_lockdown(); \
  58. }
  59. #define END_MT_IPROT \
  60. if(mt_protiflush) { \
  61. mt_cflush_release(); \
  62. evpe(mtflags); \
  63. local_irq_restore(flags); \
  64. }
  65. #define BEGIN_MT_DPROT \
  66. unsigned long flags = 0; \
  67. unsigned long mtflags = 0; \
  68. if(mt_protdflush) { \
  69. local_irq_save(flags); \
  70. ehb(); \
  71. mtflags = dvpe(); \
  72. mt_cflush_lockdown(); \
  73. }
  74. #define END_MT_DPROT \
  75. if(mt_protdflush) { \
  76. mt_cflush_release(); \
  77. evpe(mtflags); \
  78. local_irq_restore(flags); \
  79. }
  80. #else
  81. #define BEGIN_MT_IPROT
  82. #define BEGIN_MT_DPROT
  83. #define END_MT_IPROT
  84. #define END_MT_DPROT
  85. #endif /* PROTECT_CACHE_FLUSHES */
  86. #define __iflush_prologue \
  87. unsigned long redundance; \
  88. extern int mt_n_iflushes; \
  89. BEGIN_MT_IPROT \
  90. for (redundance = 0; redundance < mt_n_iflushes; redundance++) {
  91. #define __iflush_epilogue \
  92. END_MT_IPROT \
  93. }
  94. #define __dflush_prologue \
  95. unsigned long redundance; \
  96. extern int mt_n_dflushes; \
  97. BEGIN_MT_DPROT \
  98. for (redundance = 0; redundance < mt_n_dflushes; redundance++) {
  99. #define __dflush_epilogue \
  100. END_MT_DPROT \
  101. }
  102. #define __inv_dflush_prologue __dflush_prologue
  103. #define __inv_dflush_epilogue __dflush_epilogue
  104. #define __sflush_prologue {
  105. #define __sflush_epilogue }
  106. #define __inv_sflush_prologue __sflush_prologue
  107. #define __inv_sflush_epilogue __sflush_epilogue
  108. #else /* CONFIG_MIPS_MT */
  109. #define __iflush_prologue {
  110. #define __iflush_epilogue }
  111. #define __dflush_prologue {
  112. #define __dflush_epilogue }
  113. #define __inv_dflush_prologue {
  114. #define __inv_dflush_epilogue }
  115. #define __sflush_prologue {
  116. #define __sflush_epilogue }
  117. #define __inv_sflush_prologue {
  118. #define __inv_sflush_epilogue }
  119. #endif /* CONFIG_MIPS_MT */
  120. static inline void flush_icache_line_indexed(unsigned long addr)
  121. {
  122. __iflush_prologue
  123. cache_op(Index_Invalidate_I, addr);
  124. __iflush_epilogue
  125. }
  126. static inline void flush_dcache_line_indexed(unsigned long addr)
  127. {
  128. __dflush_prologue
  129. cache_op(Index_Writeback_Inv_D, addr);
  130. __dflush_epilogue
  131. }
  132. static inline void flush_scache_line_indexed(unsigned long addr)
  133. {
  134. cache_op(Index_Writeback_Inv_SD, addr);
  135. }
  136. static inline void flush_icache_line(unsigned long addr)
  137. {
  138. __iflush_prologue
  139. cache_op(Hit_Invalidate_I, addr);
  140. __iflush_epilogue
  141. }
  142. static inline void flush_dcache_line(unsigned long addr)
  143. {
  144. __dflush_prologue
  145. cache_op(Hit_Writeback_Inv_D, addr);
  146. __dflush_epilogue
  147. }
  148. static inline void invalidate_dcache_line(unsigned long addr)
  149. {
  150. __dflush_prologue
  151. cache_op(Hit_Invalidate_D, addr);
  152. __dflush_epilogue
  153. }
  154. static inline void invalidate_scache_line(unsigned long addr)
  155. {
  156. cache_op(Hit_Invalidate_SD, addr);
  157. }
  158. static inline void flush_scache_line(unsigned long addr)
  159. {
  160. cache_op(Hit_Writeback_Inv_SD, addr);
  161. }
  162. #define protected_cache_op(op,addr) \
  163. __asm__ __volatile__( \
  164. " .set push \n" \
  165. " .set noreorder \n" \
  166. " .set mips3 \n" \
  167. "1: cache %0, (%1) \n" \
  168. "2: .set pop \n" \
  169. " .section __ex_table,\"a\" \n" \
  170. " "STR(PTR)" 1b, 2b \n" \
  171. " .previous" \
  172. : \
  173. : "i" (op), "r" (addr))
  174. /*
  175. * The next two are for badland addresses like signal trampolines.
  176. */
  177. static inline void protected_flush_icache_line(unsigned long addr)
  178. {
  179. protected_cache_op(Hit_Invalidate_I, addr);
  180. }
  181. /*
  182. * R10000 / R12000 hazard - these processors don't support the Hit_Writeback_D
  183. * cacheop so we use Hit_Writeback_Inv_D which is supported by all R4000-style
  184. * caches. We're talking about one cacheline unnecessarily getting invalidated
  185. * here so the penalty isn't overly hard.
  186. */
  187. static inline void protected_writeback_dcache_line(unsigned long addr)
  188. {
  189. protected_cache_op(Hit_Writeback_Inv_D, addr);
  190. }
  191. static inline void protected_writeback_scache_line(unsigned long addr)
  192. {
  193. protected_cache_op(Hit_Writeback_Inv_SD, addr);
  194. }
  195. /*
  196. * This one is RM7000-specific
  197. */
  198. static inline void invalidate_tcache_page(unsigned long addr)
  199. {
  200. cache_op(Page_Invalidate_T, addr);
  201. }
  202. #define cache16_unroll32(base,op) \
  203. __asm__ __volatile__( \
  204. " .set push \n" \
  205. " .set noreorder \n" \
  206. " .set mips3 \n" \
  207. " cache %1, 0x000(%0); cache %1, 0x010(%0) \n" \
  208. " cache %1, 0x020(%0); cache %1, 0x030(%0) \n" \
  209. " cache %1, 0x040(%0); cache %1, 0x050(%0) \n" \
  210. " cache %1, 0x060(%0); cache %1, 0x070(%0) \n" \
  211. " cache %1, 0x080(%0); cache %1, 0x090(%0) \n" \
  212. " cache %1, 0x0a0(%0); cache %1, 0x0b0(%0) \n" \
  213. " cache %1, 0x0c0(%0); cache %1, 0x0d0(%0) \n" \
  214. " cache %1, 0x0e0(%0); cache %1, 0x0f0(%0) \n" \
  215. " cache %1, 0x100(%0); cache %1, 0x110(%0) \n" \
  216. " cache %1, 0x120(%0); cache %1, 0x130(%0) \n" \
  217. " cache %1, 0x140(%0); cache %1, 0x150(%0) \n" \
  218. " cache %1, 0x160(%0); cache %1, 0x170(%0) \n" \
  219. " cache %1, 0x180(%0); cache %1, 0x190(%0) \n" \
  220. " cache %1, 0x1a0(%0); cache %1, 0x1b0(%0) \n" \
  221. " cache %1, 0x1c0(%0); cache %1, 0x1d0(%0) \n" \
  222. " cache %1, 0x1e0(%0); cache %1, 0x1f0(%0) \n" \
  223. " .set pop \n" \
  224. : \
  225. : "r" (base), \
  226. "i" (op));
  227. #define cache32_unroll32(base,op) \
  228. __asm__ __volatile__( \
  229. " .set push \n" \
  230. " .set noreorder \n" \
  231. " .set mips3 \n" \
  232. " cache %1, 0x000(%0); cache %1, 0x020(%0) \n" \
  233. " cache %1, 0x040(%0); cache %1, 0x060(%0) \n" \
  234. " cache %1, 0x080(%0); cache %1, 0x0a0(%0) \n" \
  235. " cache %1, 0x0c0(%0); cache %1, 0x0e0(%0) \n" \
  236. " cache %1, 0x100(%0); cache %1, 0x120(%0) \n" \
  237. " cache %1, 0x140(%0); cache %1, 0x160(%0) \n" \
  238. " cache %1, 0x180(%0); cache %1, 0x1a0(%0) \n" \
  239. " cache %1, 0x1c0(%0); cache %1, 0x1e0(%0) \n" \
  240. " cache %1, 0x200(%0); cache %1, 0x220(%0) \n" \
  241. " cache %1, 0x240(%0); cache %1, 0x260(%0) \n" \
  242. " cache %1, 0x280(%0); cache %1, 0x2a0(%0) \n" \
  243. " cache %1, 0x2c0(%0); cache %1, 0x2e0(%0) \n" \
  244. " cache %1, 0x300(%0); cache %1, 0x320(%0) \n" \
  245. " cache %1, 0x340(%0); cache %1, 0x360(%0) \n" \
  246. " cache %1, 0x380(%0); cache %1, 0x3a0(%0) \n" \
  247. " cache %1, 0x3c0(%0); cache %1, 0x3e0(%0) \n" \
  248. " .set pop \n" \
  249. : \
  250. : "r" (base), \
  251. "i" (op));
  252. #define cache64_unroll32(base,op) \
  253. __asm__ __volatile__( \
  254. " .set push \n" \
  255. " .set noreorder \n" \
  256. " .set mips3 \n" \
  257. " cache %1, 0x000(%0); cache %1, 0x040(%0) \n" \
  258. " cache %1, 0x080(%0); cache %1, 0x0c0(%0) \n" \
  259. " cache %1, 0x100(%0); cache %1, 0x140(%0) \n" \
  260. " cache %1, 0x180(%0); cache %1, 0x1c0(%0) \n" \
  261. " cache %1, 0x200(%0); cache %1, 0x240(%0) \n" \
  262. " cache %1, 0x280(%0); cache %1, 0x2c0(%0) \n" \
  263. " cache %1, 0x300(%0); cache %1, 0x340(%0) \n" \
  264. " cache %1, 0x380(%0); cache %1, 0x3c0(%0) \n" \
  265. " cache %1, 0x400(%0); cache %1, 0x440(%0) \n" \
  266. " cache %1, 0x480(%0); cache %1, 0x4c0(%0) \n" \
  267. " cache %1, 0x500(%0); cache %1, 0x540(%0) \n" \
  268. " cache %1, 0x580(%0); cache %1, 0x5c0(%0) \n" \
  269. " cache %1, 0x600(%0); cache %1, 0x640(%0) \n" \
  270. " cache %1, 0x680(%0); cache %1, 0x6c0(%0) \n" \
  271. " cache %1, 0x700(%0); cache %1, 0x740(%0) \n" \
  272. " cache %1, 0x780(%0); cache %1, 0x7c0(%0) \n" \
  273. " .set pop \n" \
  274. : \
  275. : "r" (base), \
  276. "i" (op));
  277. #define cache128_unroll32(base,op) \
  278. __asm__ __volatile__( \
  279. " .set push \n" \
  280. " .set noreorder \n" \
  281. " .set mips3 \n" \
  282. " cache %1, 0x000(%0); cache %1, 0x080(%0) \n" \
  283. " cache %1, 0x100(%0); cache %1, 0x180(%0) \n" \
  284. " cache %1, 0x200(%0); cache %1, 0x280(%0) \n" \
  285. " cache %1, 0x300(%0); cache %1, 0x380(%0) \n" \
  286. " cache %1, 0x400(%0); cache %1, 0x480(%0) \n" \
  287. " cache %1, 0x500(%0); cache %1, 0x580(%0) \n" \
  288. " cache %1, 0x600(%0); cache %1, 0x680(%0) \n" \
  289. " cache %1, 0x700(%0); cache %1, 0x780(%0) \n" \
  290. " cache %1, 0x800(%0); cache %1, 0x880(%0) \n" \
  291. " cache %1, 0x900(%0); cache %1, 0x980(%0) \n" \
  292. " cache %1, 0xa00(%0); cache %1, 0xa80(%0) \n" \
  293. " cache %1, 0xb00(%0); cache %1, 0xb80(%0) \n" \
  294. " cache %1, 0xc00(%0); cache %1, 0xc80(%0) \n" \
  295. " cache %1, 0xd00(%0); cache %1, 0xd80(%0) \n" \
  296. " cache %1, 0xe00(%0); cache %1, 0xe80(%0) \n" \
  297. " cache %1, 0xf00(%0); cache %1, 0xf80(%0) \n" \
  298. " .set pop \n" \
  299. : \
  300. : "r" (base), \
  301. "i" (op));
  302. /* build blast_xxx, blast_xxx_page, blast_xxx_page_indexed */
  303. #define __BUILD_BLAST_CACHE(pfx, desc, indexop, hitop, lsize) \
  304. static inline void blast_##pfx##cache##lsize(void) \
  305. { \
  306. unsigned long start = INDEX_BASE; \
  307. unsigned long end = start + current_cpu_data.desc.waysize; \
  308. unsigned long ws_inc = 1UL << current_cpu_data.desc.waybit; \
  309. unsigned long ws_end = current_cpu_data.desc.ways << \
  310. current_cpu_data.desc.waybit; \
  311. unsigned long ws, addr; \
  312. \
  313. __##pfx##flush_prologue \
  314. \
  315. for (ws = 0; ws < ws_end; ws += ws_inc) \
  316. for (addr = start; addr < end; addr += lsize * 32) \
  317. cache##lsize##_unroll32(addr|ws, indexop); \
  318. \
  319. __##pfx##flush_epilogue \
  320. } \
  321. \
  322. static inline void blast_##pfx##cache##lsize##_page(unsigned long page) \
  323. { \
  324. unsigned long start = page; \
  325. unsigned long end = page + PAGE_SIZE; \
  326. \
  327. __##pfx##flush_prologue \
  328. \
  329. do { \
  330. cache##lsize##_unroll32(start, hitop); \
  331. start += lsize * 32; \
  332. } while (start < end); \
  333. \
  334. __##pfx##flush_epilogue \
  335. } \
  336. \
  337. static inline void blast_##pfx##cache##lsize##_page_indexed(unsigned long page) \
  338. { \
  339. unsigned long indexmask = current_cpu_data.desc.waysize - 1; \
  340. unsigned long start = INDEX_BASE + (page & indexmask); \
  341. unsigned long end = start + PAGE_SIZE; \
  342. unsigned long ws_inc = 1UL << current_cpu_data.desc.waybit; \
  343. unsigned long ws_end = current_cpu_data.desc.ways << \
  344. current_cpu_data.desc.waybit; \
  345. unsigned long ws, addr; \
  346. \
  347. __##pfx##flush_prologue \
  348. \
  349. for (ws = 0; ws < ws_end; ws += ws_inc) \
  350. for (addr = start; addr < end; addr += lsize * 32) \
  351. cache##lsize##_unroll32(addr|ws, indexop); \
  352. \
  353. __##pfx##flush_epilogue \
  354. }
  355. __BUILD_BLAST_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D, 16)
  356. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 16)
  357. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 16)
  358. __BUILD_BLAST_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D, 32)
  359. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 32)
  360. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 32)
  361. __BUILD_BLAST_CACHE(d, dcache, Index_Writeback_Inv_D, Hit_Writeback_Inv_D, 64)
  362. __BUILD_BLAST_CACHE(i, icache, Index_Invalidate_I, Hit_Invalidate_I, 64)
  363. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 64)
  364. __BUILD_BLAST_CACHE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 128)
  365. __BUILD_BLAST_CACHE(inv_d, dcache, Index_Writeback_Inv_D, Hit_Invalidate_D, 16)
  366. __BUILD_BLAST_CACHE(inv_d, dcache, Index_Writeback_Inv_D, Hit_Invalidate_D, 32)
  367. __BUILD_BLAST_CACHE(inv_s, scache, Index_Writeback_Inv_SD, Hit_Invalidate_SD, 16)
  368. __BUILD_BLAST_CACHE(inv_s, scache, Index_Writeback_Inv_SD, Hit_Invalidate_SD, 32)
  369. __BUILD_BLAST_CACHE(inv_s, scache, Index_Writeback_Inv_SD, Hit_Invalidate_SD, 64)
  370. __BUILD_BLAST_CACHE(inv_s, scache, Index_Writeback_Inv_SD, Hit_Invalidate_SD, 128)
  371. /* build blast_xxx_range, protected_blast_xxx_range */
  372. #define __BUILD_BLAST_CACHE_RANGE(pfx, desc, hitop, prot) \
  373. static inline void prot##blast_##pfx##cache##_range(unsigned long start, \
  374. unsigned long end) \
  375. { \
  376. unsigned long lsize = cpu_##desc##_line_size(); \
  377. unsigned long addr = start & ~(lsize - 1); \
  378. unsigned long aend = (end - 1) & ~(lsize - 1); \
  379. \
  380. __##pfx##flush_prologue \
  381. \
  382. while (1) { \
  383. prot##cache_op(hitop, addr); \
  384. if (addr == aend) \
  385. break; \
  386. addr += lsize; \
  387. } \
  388. \
  389. __##pfx##flush_epilogue \
  390. }
  391. __BUILD_BLAST_CACHE_RANGE(d, dcache, Hit_Writeback_Inv_D, protected_)
  392. __BUILD_BLAST_CACHE_RANGE(s, scache, Hit_Writeback_Inv_SD, protected_)
  393. __BUILD_BLAST_CACHE_RANGE(i, icache, Hit_Invalidate_I, protected_)
  394. __BUILD_BLAST_CACHE_RANGE(d, dcache, Hit_Writeback_Inv_D, )
  395. __BUILD_BLAST_CACHE_RANGE(s, scache, Hit_Writeback_Inv_SD, )
  396. /* blast_inv_dcache_range */
  397. __BUILD_BLAST_CACHE_RANGE(inv_d, dcache, Hit_Invalidate_D, )
  398. __BUILD_BLAST_CACHE_RANGE(inv_s, scache, Hit_Invalidate_SD, )
  399. #endif /* _ASM_R4KCACHE_H */