r4kcache.h 14 KB

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