c-r4k.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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. * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
  7. * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Ralf Baechle (ralf@gnu.org)
  8. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  9. */
  10. #include <linux/config.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/bitops.h>
  16. #include <asm/bcache.h>
  17. #include <asm/bootinfo.h>
  18. #include <asm/cache.h>
  19. #include <asm/cacheops.h>
  20. #include <asm/cpu.h>
  21. #include <asm/cpu-features.h>
  22. #include <asm/io.h>
  23. #include <asm/page.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/r4kcache.h>
  26. #include <asm/system.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/war.h>
  29. #include <asm/cacheflush.h> /* for run_uncached() */
  30. /*
  31. * Must die.
  32. */
  33. static unsigned long icache_size __read_mostly;
  34. static unsigned long dcache_size __read_mostly;
  35. static unsigned long scache_size __read_mostly;
  36. /*
  37. * Dummy cache handling routines for machines without boardcaches
  38. */
  39. static void no_sc_noop(void) {}
  40. static struct bcache_ops no_sc_ops = {
  41. .bc_enable = (void *)no_sc_noop,
  42. .bc_disable = (void *)no_sc_noop,
  43. .bc_wback_inv = (void *)no_sc_noop,
  44. .bc_inv = (void *)no_sc_noop
  45. };
  46. struct bcache_ops *bcops = &no_sc_ops;
  47. #define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
  48. #define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
  49. #define R4600_HIT_CACHEOP_WAR_IMPL \
  50. do { \
  51. if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
  52. *(volatile unsigned long *)CKSEG1; \
  53. if (R4600_V1_HIT_CACHEOP_WAR) \
  54. __asm__ __volatile__("nop;nop;nop;nop"); \
  55. } while (0)
  56. static void (*r4k_blast_dcache_page)(unsigned long addr);
  57. static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
  58. {
  59. R4600_HIT_CACHEOP_WAR_IMPL;
  60. blast_dcache32_page(addr);
  61. }
  62. static inline void r4k_blast_dcache_page_setup(void)
  63. {
  64. unsigned long dc_lsize = cpu_dcache_line_size();
  65. if (dc_lsize == 16)
  66. r4k_blast_dcache_page = blast_dcache16_page;
  67. else if (dc_lsize == 32)
  68. r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
  69. }
  70. static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
  71. static inline void r4k_blast_dcache_page_indexed_setup(void)
  72. {
  73. unsigned long dc_lsize = cpu_dcache_line_size();
  74. if (dc_lsize == 16)
  75. r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
  76. else if (dc_lsize == 32)
  77. r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
  78. }
  79. static void (* r4k_blast_dcache)(void);
  80. static inline void r4k_blast_dcache_setup(void)
  81. {
  82. unsigned long dc_lsize = cpu_dcache_line_size();
  83. if (dc_lsize == 16)
  84. r4k_blast_dcache = blast_dcache16;
  85. else if (dc_lsize == 32)
  86. r4k_blast_dcache = blast_dcache32;
  87. }
  88. /* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
  89. #define JUMP_TO_ALIGN(order) \
  90. __asm__ __volatile__( \
  91. "b\t1f\n\t" \
  92. ".align\t" #order "\n\t" \
  93. "1:\n\t" \
  94. )
  95. #define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
  96. #define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
  97. static inline void blast_r4600_v1_icache32(void)
  98. {
  99. unsigned long flags;
  100. local_irq_save(flags);
  101. blast_icache32();
  102. local_irq_restore(flags);
  103. }
  104. static inline void tx49_blast_icache32(void)
  105. {
  106. unsigned long start = INDEX_BASE;
  107. unsigned long end = start + current_cpu_data.icache.waysize;
  108. unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
  109. unsigned long ws_end = current_cpu_data.icache.ways <<
  110. current_cpu_data.icache.waybit;
  111. unsigned long ws, addr;
  112. CACHE32_UNROLL32_ALIGN2;
  113. /* I'm in even chunk. blast odd chunks */
  114. for (ws = 0; ws < ws_end; ws += ws_inc)
  115. for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
  116. cache32_unroll32(addr|ws,Index_Invalidate_I);
  117. CACHE32_UNROLL32_ALIGN;
  118. /* I'm in odd chunk. blast even chunks */
  119. for (ws = 0; ws < ws_end; ws += ws_inc)
  120. for (addr = start; addr < end; addr += 0x400 * 2)
  121. cache32_unroll32(addr|ws,Index_Invalidate_I);
  122. }
  123. static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
  124. {
  125. unsigned long flags;
  126. local_irq_save(flags);
  127. blast_icache32_page_indexed(page);
  128. local_irq_restore(flags);
  129. }
  130. static inline void tx49_blast_icache32_page_indexed(unsigned long page)
  131. {
  132. unsigned long start = page;
  133. unsigned long end = start + PAGE_SIZE;
  134. unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
  135. unsigned long ws_end = current_cpu_data.icache.ways <<
  136. current_cpu_data.icache.waybit;
  137. unsigned long ws, addr;
  138. CACHE32_UNROLL32_ALIGN2;
  139. /* I'm in even chunk. blast odd chunks */
  140. for (ws = 0; ws < ws_end; ws += ws_inc)
  141. for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
  142. cache32_unroll32(addr|ws,Index_Invalidate_I);
  143. CACHE32_UNROLL32_ALIGN;
  144. /* I'm in odd chunk. blast even chunks */
  145. for (ws = 0; ws < ws_end; ws += ws_inc)
  146. for (addr = start; addr < end; addr += 0x400 * 2)
  147. cache32_unroll32(addr|ws,Index_Invalidate_I);
  148. }
  149. static void (* r4k_blast_icache_page)(unsigned long addr);
  150. static inline void r4k_blast_icache_page_setup(void)
  151. {
  152. unsigned long ic_lsize = cpu_icache_line_size();
  153. if (ic_lsize == 16)
  154. r4k_blast_icache_page = blast_icache16_page;
  155. else if (ic_lsize == 32)
  156. r4k_blast_icache_page = blast_icache32_page;
  157. else if (ic_lsize == 64)
  158. r4k_blast_icache_page = blast_icache64_page;
  159. }
  160. static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
  161. static inline void r4k_blast_icache_page_indexed_setup(void)
  162. {
  163. unsigned long ic_lsize = cpu_icache_line_size();
  164. if (ic_lsize == 16)
  165. r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
  166. else if (ic_lsize == 32) {
  167. if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
  168. r4k_blast_icache_page_indexed =
  169. blast_icache32_r4600_v1_page_indexed;
  170. else if (TX49XX_ICACHE_INDEX_INV_WAR)
  171. r4k_blast_icache_page_indexed =
  172. tx49_blast_icache32_page_indexed;
  173. else
  174. r4k_blast_icache_page_indexed =
  175. blast_icache32_page_indexed;
  176. } else if (ic_lsize == 64)
  177. r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
  178. }
  179. static void (* r4k_blast_icache)(void);
  180. static inline void r4k_blast_icache_setup(void)
  181. {
  182. unsigned long ic_lsize = cpu_icache_line_size();
  183. if (ic_lsize == 16)
  184. r4k_blast_icache = blast_icache16;
  185. else if (ic_lsize == 32) {
  186. if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
  187. r4k_blast_icache = blast_r4600_v1_icache32;
  188. else if (TX49XX_ICACHE_INDEX_INV_WAR)
  189. r4k_blast_icache = tx49_blast_icache32;
  190. else
  191. r4k_blast_icache = blast_icache32;
  192. } else if (ic_lsize == 64)
  193. r4k_blast_icache = blast_icache64;
  194. }
  195. static void (* r4k_blast_scache_page)(unsigned long addr);
  196. static inline void r4k_blast_scache_page_setup(void)
  197. {
  198. unsigned long sc_lsize = cpu_scache_line_size();
  199. if (scache_size == 0)
  200. r4k_blast_scache_page = (void *)no_sc_noop;
  201. else if (sc_lsize == 16)
  202. r4k_blast_scache_page = blast_scache16_page;
  203. else if (sc_lsize == 32)
  204. r4k_blast_scache_page = blast_scache32_page;
  205. else if (sc_lsize == 64)
  206. r4k_blast_scache_page = blast_scache64_page;
  207. else if (sc_lsize == 128)
  208. r4k_blast_scache_page = blast_scache128_page;
  209. }
  210. static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
  211. static inline void r4k_blast_scache_page_indexed_setup(void)
  212. {
  213. unsigned long sc_lsize = cpu_scache_line_size();
  214. if (scache_size == 0)
  215. r4k_blast_scache_page_indexed = (void *)no_sc_noop;
  216. else if (sc_lsize == 16)
  217. r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
  218. else if (sc_lsize == 32)
  219. r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
  220. else if (sc_lsize == 64)
  221. r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
  222. else if (sc_lsize == 128)
  223. r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
  224. }
  225. static void (* r4k_blast_scache)(void);
  226. static inline void r4k_blast_scache_setup(void)
  227. {
  228. unsigned long sc_lsize = cpu_scache_line_size();
  229. if (scache_size == 0)
  230. r4k_blast_scache = (void *)no_sc_noop;
  231. else if (sc_lsize == 16)
  232. r4k_blast_scache = blast_scache16;
  233. else if (sc_lsize == 32)
  234. r4k_blast_scache = blast_scache32;
  235. else if (sc_lsize == 64)
  236. r4k_blast_scache = blast_scache64;
  237. else if (sc_lsize == 128)
  238. r4k_blast_scache = blast_scache128;
  239. }
  240. /*
  241. * This is former mm's flush_cache_all() which really should be
  242. * flush_cache_vunmap these days ...
  243. */
  244. static inline void local_r4k_flush_cache_all(void * args)
  245. {
  246. r4k_blast_dcache();
  247. r4k_blast_icache();
  248. }
  249. static void r4k_flush_cache_all(void)
  250. {
  251. if (!cpu_has_dc_aliases)
  252. return;
  253. on_each_cpu(local_r4k_flush_cache_all, NULL, 1, 1);
  254. }
  255. static inline void local_r4k___flush_cache_all(void * args)
  256. {
  257. r4k_blast_dcache();
  258. r4k_blast_icache();
  259. switch (current_cpu_data.cputype) {
  260. case CPU_R4000SC:
  261. case CPU_R4000MC:
  262. case CPU_R4400SC:
  263. case CPU_R4400MC:
  264. case CPU_R10000:
  265. case CPU_R12000:
  266. r4k_blast_scache();
  267. }
  268. }
  269. static void r4k___flush_cache_all(void)
  270. {
  271. on_each_cpu(local_r4k___flush_cache_all, NULL, 1, 1);
  272. }
  273. static inline void local_r4k_flush_cache_range(void * args)
  274. {
  275. struct vm_area_struct *vma = args;
  276. int exec;
  277. if (!(cpu_context(smp_processor_id(), vma->vm_mm)))
  278. return;
  279. exec = vma->vm_flags & VM_EXEC;
  280. if (cpu_has_dc_aliases || exec)
  281. r4k_blast_dcache();
  282. if (exec)
  283. r4k_blast_icache();
  284. }
  285. static void r4k_flush_cache_range(struct vm_area_struct *vma,
  286. unsigned long start, unsigned long end)
  287. {
  288. on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
  289. }
  290. static inline void local_r4k_flush_cache_mm(void * args)
  291. {
  292. struct mm_struct *mm = args;
  293. if (!cpu_context(smp_processor_id(), mm))
  294. return;
  295. r4k_blast_dcache();
  296. r4k_blast_icache();
  297. /*
  298. * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
  299. * only flush the primary caches but R10000 and R12000 behave sane ...
  300. */
  301. if (current_cpu_data.cputype == CPU_R4000SC ||
  302. current_cpu_data.cputype == CPU_R4000MC ||
  303. current_cpu_data.cputype == CPU_R4400SC ||
  304. current_cpu_data.cputype == CPU_R4400MC)
  305. r4k_blast_scache();
  306. }
  307. static void r4k_flush_cache_mm(struct mm_struct *mm)
  308. {
  309. if (!cpu_has_dc_aliases)
  310. return;
  311. on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
  312. }
  313. struct flush_cache_page_args {
  314. struct vm_area_struct *vma;
  315. unsigned long addr;
  316. unsigned long pfn;
  317. };
  318. static inline void local_r4k_flush_cache_page(void *args)
  319. {
  320. struct flush_cache_page_args *fcp_args = args;
  321. struct vm_area_struct *vma = fcp_args->vma;
  322. unsigned long addr = fcp_args->addr;
  323. unsigned long paddr = fcp_args->pfn << PAGE_SHIFT;
  324. int exec = vma->vm_flags & VM_EXEC;
  325. struct mm_struct *mm = vma->vm_mm;
  326. pgd_t *pgdp;
  327. pud_t *pudp;
  328. pmd_t *pmdp;
  329. pte_t *ptep;
  330. /*
  331. * If ownes no valid ASID yet, cannot possibly have gotten
  332. * this page into the cache.
  333. */
  334. if (cpu_context(smp_processor_id(), mm) == 0)
  335. return;
  336. addr &= PAGE_MASK;
  337. pgdp = pgd_offset(mm, addr);
  338. pudp = pud_offset(pgdp, addr);
  339. pmdp = pmd_offset(pudp, addr);
  340. ptep = pte_offset(pmdp, addr);
  341. /*
  342. * If the page isn't marked valid, the page cannot possibly be
  343. * in the cache.
  344. */
  345. if (!(pte_val(*ptep) & _PAGE_PRESENT))
  346. return;
  347. /*
  348. * Doing flushes for another ASID than the current one is
  349. * too difficult since stupid R4k caches do a TLB translation
  350. * for every cache flush operation. So we do indexed flushes
  351. * in that case, which doesn't overly flush the cache too much.
  352. */
  353. if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID)) {
  354. if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
  355. r4k_blast_dcache_page(addr);
  356. if (exec && !cpu_icache_snoops_remote_store)
  357. r4k_blast_scache_page(addr);
  358. }
  359. if (exec)
  360. r4k_blast_icache_page(addr);
  361. return;
  362. }
  363. /*
  364. * Do indexed flush, too much work to get the (possible) TLB refills
  365. * to work correctly.
  366. */
  367. if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
  368. r4k_blast_dcache_page_indexed(cpu_has_pindexed_dcache ?
  369. paddr : addr);
  370. if (exec && !cpu_icache_snoops_remote_store) {
  371. r4k_blast_scache_page_indexed(paddr);
  372. }
  373. }
  374. if (exec) {
  375. if (cpu_has_vtag_icache) {
  376. int cpu = smp_processor_id();
  377. if (cpu_context(cpu, mm) != 0)
  378. drop_mmu_context(mm, cpu);
  379. } else
  380. r4k_blast_icache_page_indexed(addr);
  381. }
  382. }
  383. static void r4k_flush_cache_page(struct vm_area_struct *vma,
  384. unsigned long addr, unsigned long pfn)
  385. {
  386. struct flush_cache_page_args args;
  387. args.vma = vma;
  388. args.addr = addr;
  389. args.pfn = pfn;
  390. on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
  391. }
  392. static inline void local_r4k_flush_data_cache_page(void * addr)
  393. {
  394. r4k_blast_dcache_page((unsigned long) addr);
  395. }
  396. static void r4k_flush_data_cache_page(unsigned long addr)
  397. {
  398. on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr, 1, 1);
  399. }
  400. struct flush_icache_range_args {
  401. unsigned long start;
  402. unsigned long end;
  403. };
  404. static inline void local_r4k_flush_icache_range(void *args)
  405. {
  406. struct flush_icache_range_args *fir_args = args;
  407. unsigned long start = fir_args->start;
  408. unsigned long end = fir_args->end;
  409. if (!cpu_has_ic_fills_f_dc) {
  410. if (end - start > dcache_size) {
  411. r4k_blast_dcache();
  412. } else {
  413. R4600_HIT_CACHEOP_WAR_IMPL;
  414. protected_blast_dcache_range(start, end);
  415. }
  416. if (!cpu_icache_snoops_remote_store && scache_size) {
  417. if (end - start > scache_size)
  418. r4k_blast_scache();
  419. else
  420. protected_blast_scache_range(start, end);
  421. }
  422. }
  423. if (end - start > icache_size)
  424. r4k_blast_icache();
  425. else
  426. protected_blast_icache_range(start, end);
  427. }
  428. static void r4k_flush_icache_range(unsigned long start, unsigned long end)
  429. {
  430. struct flush_icache_range_args args;
  431. args.start = start;
  432. args.end = end;
  433. on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
  434. instruction_hazard();
  435. }
  436. /*
  437. * Ok, this seriously sucks. We use them to flush a user page but don't
  438. * know the virtual address, so we have to blast away the whole icache
  439. * which is significantly more expensive than the real thing. Otoh we at
  440. * least know the kernel address of the page so we can flush it
  441. * selectivly.
  442. */
  443. struct flush_icache_page_args {
  444. struct vm_area_struct *vma;
  445. struct page *page;
  446. };
  447. static inline void local_r4k_flush_icache_page(void *args)
  448. {
  449. struct flush_icache_page_args *fip_args = args;
  450. struct vm_area_struct *vma = fip_args->vma;
  451. struct page *page = fip_args->page;
  452. /*
  453. * Tricky ... Because we don't know the virtual address we've got the
  454. * choice of either invalidating the entire primary and secondary
  455. * caches or invalidating the secondary caches also. With the subset
  456. * enforcment on R4000SC, R4400SC, R10000 and R12000 invalidating the
  457. * secondary cache will result in any entries in the primary caches
  458. * also getting invalidated which hopefully is a bit more economical.
  459. */
  460. if (cpu_has_subset_pcaches) {
  461. unsigned long addr = (unsigned long) page_address(page);
  462. r4k_blast_scache_page(addr);
  463. ClearPageDcacheDirty(page);
  464. return;
  465. }
  466. if (!cpu_has_ic_fills_f_dc) {
  467. unsigned long addr = (unsigned long) page_address(page);
  468. r4k_blast_dcache_page(addr);
  469. if (!cpu_icache_snoops_remote_store)
  470. r4k_blast_scache_page(addr);
  471. ClearPageDcacheDirty(page);
  472. }
  473. /*
  474. * We're not sure of the virtual address(es) involved here, so
  475. * we have to flush the entire I-cache.
  476. */
  477. if (cpu_has_vtag_icache) {
  478. int cpu = smp_processor_id();
  479. if (cpu_context(cpu, vma->vm_mm) != 0)
  480. drop_mmu_context(vma->vm_mm, cpu);
  481. } else
  482. r4k_blast_icache();
  483. }
  484. static void r4k_flush_icache_page(struct vm_area_struct *vma,
  485. struct page *page)
  486. {
  487. struct flush_icache_page_args args;
  488. /*
  489. * If there's no context yet, or the page isn't executable, no I-cache
  490. * flush is needed.
  491. */
  492. if (!(vma->vm_flags & VM_EXEC))
  493. return;
  494. args.vma = vma;
  495. args.page = page;
  496. on_each_cpu(local_r4k_flush_icache_page, &args, 1, 1);
  497. }
  498. #ifdef CONFIG_DMA_NONCOHERENT
  499. static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
  500. {
  501. /* Catch bad driver code */
  502. BUG_ON(size == 0);
  503. if (cpu_has_subset_pcaches) {
  504. if (size >= scache_size)
  505. r4k_blast_scache();
  506. else
  507. blast_scache_range(addr, addr + size);
  508. return;
  509. }
  510. /*
  511. * Either no secondary cache or the available caches don't have the
  512. * subset property so we have to flush the primary caches
  513. * explicitly
  514. */
  515. if (size >= dcache_size) {
  516. r4k_blast_dcache();
  517. } else {
  518. R4600_HIT_CACHEOP_WAR_IMPL;
  519. blast_dcache_range(addr, addr + size);
  520. }
  521. bc_wback_inv(addr, size);
  522. }
  523. static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
  524. {
  525. /* Catch bad driver code */
  526. BUG_ON(size == 0);
  527. if (cpu_has_subset_pcaches) {
  528. if (size >= scache_size)
  529. r4k_blast_scache();
  530. else
  531. blast_scache_range(addr, addr + size);
  532. return;
  533. }
  534. if (size >= dcache_size) {
  535. r4k_blast_dcache();
  536. } else {
  537. R4600_HIT_CACHEOP_WAR_IMPL;
  538. blast_dcache_range(addr, addr + size);
  539. }
  540. bc_inv(addr, size);
  541. }
  542. #endif /* CONFIG_DMA_NONCOHERENT */
  543. /*
  544. * While we're protected against bad userland addresses we don't care
  545. * very much about what happens in that case. Usually a segmentation
  546. * fault will dump the process later on anyway ...
  547. */
  548. static void local_r4k_flush_cache_sigtramp(void * arg)
  549. {
  550. unsigned long ic_lsize = cpu_icache_line_size();
  551. unsigned long dc_lsize = cpu_dcache_line_size();
  552. unsigned long sc_lsize = cpu_scache_line_size();
  553. unsigned long addr = (unsigned long) arg;
  554. R4600_HIT_CACHEOP_WAR_IMPL;
  555. protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
  556. if (!cpu_icache_snoops_remote_store && scache_size)
  557. protected_writeback_scache_line(addr & ~(sc_lsize - 1));
  558. protected_flush_icache_line(addr & ~(ic_lsize - 1));
  559. if (MIPS4K_ICACHE_REFILL_WAR) {
  560. __asm__ __volatile__ (
  561. ".set push\n\t"
  562. ".set noat\n\t"
  563. ".set mips3\n\t"
  564. #ifdef CONFIG_32BIT
  565. "la $at,1f\n\t"
  566. #endif
  567. #ifdef CONFIG_64BIT
  568. "dla $at,1f\n\t"
  569. #endif
  570. "cache %0,($at)\n\t"
  571. "nop; nop; nop\n"
  572. "1:\n\t"
  573. ".set pop"
  574. :
  575. : "i" (Hit_Invalidate_I));
  576. }
  577. if (MIPS_CACHE_SYNC_WAR)
  578. __asm__ __volatile__ ("sync");
  579. }
  580. static void r4k_flush_cache_sigtramp(unsigned long addr)
  581. {
  582. on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
  583. }
  584. static void r4k_flush_icache_all(void)
  585. {
  586. if (cpu_has_vtag_icache)
  587. r4k_blast_icache();
  588. }
  589. static inline void rm7k_erratum31(void)
  590. {
  591. const unsigned long ic_lsize = 32;
  592. unsigned long addr;
  593. /* RM7000 erratum #31. The icache is screwed at startup. */
  594. write_c0_taglo(0);
  595. write_c0_taghi(0);
  596. for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
  597. __asm__ __volatile__ (
  598. ".set push\n\t"
  599. ".set noreorder\n\t"
  600. ".set mips3\n\t"
  601. "cache\t%1, 0(%0)\n\t"
  602. "cache\t%1, 0x1000(%0)\n\t"
  603. "cache\t%1, 0x2000(%0)\n\t"
  604. "cache\t%1, 0x3000(%0)\n\t"
  605. "cache\t%2, 0(%0)\n\t"
  606. "cache\t%2, 0x1000(%0)\n\t"
  607. "cache\t%2, 0x2000(%0)\n\t"
  608. "cache\t%2, 0x3000(%0)\n\t"
  609. "cache\t%1, 0(%0)\n\t"
  610. "cache\t%1, 0x1000(%0)\n\t"
  611. "cache\t%1, 0x2000(%0)\n\t"
  612. "cache\t%1, 0x3000(%0)\n\t"
  613. ".set pop\n"
  614. :
  615. : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
  616. }
  617. }
  618. static char *way_string[] __initdata = { NULL, "direct mapped", "2-way",
  619. "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
  620. };
  621. static void __init probe_pcache(void)
  622. {
  623. struct cpuinfo_mips *c = &current_cpu_data;
  624. unsigned int config = read_c0_config();
  625. unsigned int prid = read_c0_prid();
  626. unsigned long config1;
  627. unsigned int lsize;
  628. switch (c->cputype) {
  629. case CPU_R4600: /* QED style two way caches? */
  630. case CPU_R4700:
  631. case CPU_R5000:
  632. case CPU_NEVADA:
  633. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  634. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  635. c->icache.ways = 2;
  636. c->icache.waybit = ffs(icache_size/2) - 1;
  637. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  638. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  639. c->dcache.ways = 2;
  640. c->dcache.waybit= ffs(dcache_size/2) - 1;
  641. c->options |= MIPS_CPU_CACHE_CDEX_P;
  642. break;
  643. case CPU_R5432:
  644. case CPU_R5500:
  645. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  646. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  647. c->icache.ways = 2;
  648. c->icache.waybit= 0;
  649. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  650. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  651. c->dcache.ways = 2;
  652. c->dcache.waybit = 0;
  653. c->options |= MIPS_CPU_CACHE_CDEX_P;
  654. break;
  655. case CPU_TX49XX:
  656. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  657. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  658. c->icache.ways = 4;
  659. c->icache.waybit= 0;
  660. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  661. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  662. c->dcache.ways = 4;
  663. c->dcache.waybit = 0;
  664. c->options |= MIPS_CPU_CACHE_CDEX_P;
  665. break;
  666. case CPU_R4000PC:
  667. case CPU_R4000SC:
  668. case CPU_R4000MC:
  669. case CPU_R4400PC:
  670. case CPU_R4400SC:
  671. case CPU_R4400MC:
  672. case CPU_R4300:
  673. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  674. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  675. c->icache.ways = 1;
  676. c->icache.waybit = 0; /* doesn't matter */
  677. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  678. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  679. c->dcache.ways = 1;
  680. c->dcache.waybit = 0; /* does not matter */
  681. c->options |= MIPS_CPU_CACHE_CDEX_P;
  682. break;
  683. case CPU_R10000:
  684. case CPU_R12000:
  685. icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
  686. c->icache.linesz = 64;
  687. c->icache.ways = 2;
  688. c->icache.waybit = 0;
  689. dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
  690. c->dcache.linesz = 32;
  691. c->dcache.ways = 2;
  692. c->dcache.waybit = 0;
  693. c->options |= MIPS_CPU_PREFETCH;
  694. break;
  695. case CPU_VR4133:
  696. write_c0_config(config & ~CONF_EB);
  697. case CPU_VR4131:
  698. /* Workaround for cache instruction bug of VR4131 */
  699. if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
  700. c->processor_id == 0x0c82U) {
  701. config &= ~0x00000030U;
  702. config |= 0x00410000U;
  703. write_c0_config(config);
  704. }
  705. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  706. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  707. c->icache.ways = 2;
  708. c->icache.waybit = ffs(icache_size/2) - 1;
  709. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  710. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  711. c->dcache.ways = 2;
  712. c->dcache.waybit = ffs(dcache_size/2) - 1;
  713. c->options |= MIPS_CPU_CACHE_CDEX_P;
  714. break;
  715. case CPU_VR41XX:
  716. case CPU_VR4111:
  717. case CPU_VR4121:
  718. case CPU_VR4122:
  719. case CPU_VR4181:
  720. case CPU_VR4181A:
  721. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  722. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  723. c->icache.ways = 1;
  724. c->icache.waybit = 0; /* doesn't matter */
  725. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  726. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  727. c->dcache.ways = 1;
  728. c->dcache.waybit = 0; /* does not matter */
  729. c->options |= MIPS_CPU_CACHE_CDEX_P;
  730. break;
  731. case CPU_RM7000:
  732. rm7k_erratum31();
  733. case CPU_RM9000:
  734. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  735. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  736. c->icache.ways = 4;
  737. c->icache.waybit = ffs(icache_size / c->icache.ways) - 1;
  738. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  739. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  740. c->dcache.ways = 4;
  741. c->dcache.waybit = ffs(dcache_size / c->dcache.ways) - 1;
  742. #if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
  743. c->options |= MIPS_CPU_CACHE_CDEX_P;
  744. #endif
  745. c->options |= MIPS_CPU_PREFETCH;
  746. break;
  747. default:
  748. if (!(config & MIPS_CONF_M))
  749. panic("Don't know how to probe P-caches on this cpu.");
  750. /*
  751. * So we seem to be a MIPS32 or MIPS64 CPU
  752. * So let's probe the I-cache ...
  753. */
  754. config1 = read_c0_config1();
  755. if ((lsize = ((config1 >> 19) & 7)))
  756. c->icache.linesz = 2 << lsize;
  757. else
  758. c->icache.linesz = lsize;
  759. c->icache.sets = 64 << ((config1 >> 22) & 7);
  760. c->icache.ways = 1 + ((config1 >> 16) & 7);
  761. icache_size = c->icache.sets *
  762. c->icache.ways *
  763. c->icache.linesz;
  764. c->icache.waybit = ffs(icache_size/c->icache.ways) - 1;
  765. if (config & 0x8) /* VI bit */
  766. c->icache.flags |= MIPS_CACHE_VTAG;
  767. /*
  768. * Now probe the MIPS32 / MIPS64 data cache.
  769. */
  770. c->dcache.flags = 0;
  771. if ((lsize = ((config1 >> 10) & 7)))
  772. c->dcache.linesz = 2 << lsize;
  773. else
  774. c->dcache.linesz= lsize;
  775. c->dcache.sets = 64 << ((config1 >> 13) & 7);
  776. c->dcache.ways = 1 + ((config1 >> 7) & 7);
  777. dcache_size = c->dcache.sets *
  778. c->dcache.ways *
  779. c->dcache.linesz;
  780. c->dcache.waybit = ffs(dcache_size/c->dcache.ways) - 1;
  781. c->options |= MIPS_CPU_PREFETCH;
  782. break;
  783. }
  784. /*
  785. * Processor configuration sanity check for the R4000SC erratum
  786. * #5. With page sizes larger than 32kB there is no possibility
  787. * to get a VCE exception anymore so we don't care about this
  788. * misconfiguration. The case is rather theoretical anyway;
  789. * presumably no vendor is shipping his hardware in the "bad"
  790. * configuration.
  791. */
  792. if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
  793. !(config & CONF_SC) && c->icache.linesz != 16 &&
  794. PAGE_SIZE <= 0x8000)
  795. panic("Improper R4000SC processor configuration detected");
  796. /* compute a couple of other cache variables */
  797. c->icache.waysize = icache_size / c->icache.ways;
  798. c->dcache.waysize = dcache_size / c->dcache.ways;
  799. c->icache.sets = icache_size / (c->icache.linesz * c->icache.ways);
  800. c->dcache.sets = dcache_size / (c->dcache.linesz * c->dcache.ways);
  801. /*
  802. * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
  803. * 2-way virtually indexed so normally would suffer from aliases. So
  804. * normally they'd suffer from aliases but magic in the hardware deals
  805. * with that for us so we don't need to take care ourselves.
  806. */
  807. switch (c->cputype) {
  808. case CPU_20KC:
  809. case CPU_25KF:
  810. c->dcache.flags |= MIPS_CACHE_PINDEX;
  811. case CPU_R10000:
  812. case CPU_R12000:
  813. case CPU_SB1:
  814. break;
  815. case CPU_24K:
  816. if (!(read_c0_config7() & (1 << 16)))
  817. default:
  818. if (c->dcache.waysize > PAGE_SIZE)
  819. c->dcache.flags |= MIPS_CACHE_ALIASES;
  820. }
  821. switch (c->cputype) {
  822. case CPU_20KC:
  823. /*
  824. * Some older 20Kc chips doesn't have the 'VI' bit in
  825. * the config register.
  826. */
  827. c->icache.flags |= MIPS_CACHE_VTAG;
  828. break;
  829. case CPU_AU1000:
  830. case CPU_AU1500:
  831. case CPU_AU1100:
  832. case CPU_AU1550:
  833. case CPU_AU1200:
  834. c->icache.flags |= MIPS_CACHE_IC_F_DC;
  835. break;
  836. }
  837. printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
  838. icache_size >> 10,
  839. cpu_has_vtag_icache ? "virtually tagged" : "physically tagged",
  840. way_string[c->icache.ways], c->icache.linesz);
  841. printk("Primary data cache %ldkB, %s, linesize %d bytes.\n",
  842. dcache_size >> 10, way_string[c->dcache.ways], c->dcache.linesz);
  843. }
  844. /*
  845. * If you even _breathe_ on this function, look at the gcc output and make sure
  846. * it does not pop things on and off the stack for the cache sizing loop that
  847. * executes in KSEG1 space or else you will crash and burn badly. You have
  848. * been warned.
  849. */
  850. static int __init probe_scache(void)
  851. {
  852. extern unsigned long stext;
  853. unsigned long flags, addr, begin, end, pow2;
  854. unsigned int config = read_c0_config();
  855. struct cpuinfo_mips *c = &current_cpu_data;
  856. int tmp;
  857. if (config & CONF_SC)
  858. return 0;
  859. begin = (unsigned long) &stext;
  860. begin &= ~((4 * 1024 * 1024) - 1);
  861. end = begin + (4 * 1024 * 1024);
  862. /*
  863. * This is such a bitch, you'd think they would make it easy to do
  864. * this. Away you daemons of stupidity!
  865. */
  866. local_irq_save(flags);
  867. /* Fill each size-multiple cache line with a valid tag. */
  868. pow2 = (64 * 1024);
  869. for (addr = begin; addr < end; addr = (begin + pow2)) {
  870. unsigned long *p = (unsigned long *) addr;
  871. __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
  872. pow2 <<= 1;
  873. }
  874. /* Load first line with zero (therefore invalid) tag. */
  875. write_c0_taglo(0);
  876. write_c0_taghi(0);
  877. __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
  878. cache_op(Index_Store_Tag_I, begin);
  879. cache_op(Index_Store_Tag_D, begin);
  880. cache_op(Index_Store_Tag_SD, begin);
  881. /* Now search for the wrap around point. */
  882. pow2 = (128 * 1024);
  883. tmp = 0;
  884. for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
  885. cache_op(Index_Load_Tag_SD, addr);
  886. __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
  887. if (!read_c0_taglo())
  888. break;
  889. pow2 <<= 1;
  890. }
  891. local_irq_restore(flags);
  892. addr -= begin;
  893. scache_size = addr;
  894. c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
  895. c->scache.ways = 1;
  896. c->dcache.waybit = 0; /* does not matter */
  897. return 1;
  898. }
  899. extern int r5k_sc_init(void);
  900. extern int rm7k_sc_init(void);
  901. static void __init setup_scache(void)
  902. {
  903. struct cpuinfo_mips *c = &current_cpu_data;
  904. unsigned int config = read_c0_config();
  905. int sc_present = 0;
  906. /*
  907. * Do the probing thing on R4000SC and R4400SC processors. Other
  908. * processors don't have a S-cache that would be relevant to the
  909. * Linux memory managment.
  910. */
  911. switch (c->cputype) {
  912. case CPU_R4000SC:
  913. case CPU_R4000MC:
  914. case CPU_R4400SC:
  915. case CPU_R4400MC:
  916. sc_present = run_uncached(probe_scache);
  917. if (sc_present)
  918. c->options |= MIPS_CPU_CACHE_CDEX_S;
  919. break;
  920. case CPU_R10000:
  921. case CPU_R12000:
  922. scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
  923. c->scache.linesz = 64 << ((config >> 13) & 1);
  924. c->scache.ways = 2;
  925. c->scache.waybit= 0;
  926. sc_present = 1;
  927. break;
  928. case CPU_R5000:
  929. case CPU_NEVADA:
  930. #ifdef CONFIG_R5000_CPU_SCACHE
  931. r5k_sc_init();
  932. #endif
  933. return;
  934. case CPU_RM7000:
  935. case CPU_RM9000:
  936. #ifdef CONFIG_RM7000_CPU_SCACHE
  937. rm7k_sc_init();
  938. #endif
  939. return;
  940. default:
  941. sc_present = 0;
  942. }
  943. if (!sc_present)
  944. return;
  945. if ((c->isa_level == MIPS_CPU_ISA_M32R1 ||
  946. c->isa_level == MIPS_CPU_ISA_M64R1) &&
  947. !(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
  948. panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
  949. /* compute a couple of other cache variables */
  950. c->scache.waysize = scache_size / c->scache.ways;
  951. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  952. printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  953. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  954. c->options |= MIPS_CPU_SUBSET_CACHES;
  955. }
  956. static inline void coherency_setup(void)
  957. {
  958. change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
  959. /*
  960. * c0_status.cu=0 specifies that updates by the sc instruction use
  961. * the coherency mode specified by the TLB; 1 means cachable
  962. * coherent update on write will be used. Not all processors have
  963. * this bit and; some wire it to zero, others like Toshiba had the
  964. * silly idea of putting something else there ...
  965. */
  966. switch (current_cpu_data.cputype) {
  967. case CPU_R4000PC:
  968. case CPU_R4000SC:
  969. case CPU_R4000MC:
  970. case CPU_R4400PC:
  971. case CPU_R4400SC:
  972. case CPU_R4400MC:
  973. clear_c0_config(CONF_CU);
  974. break;
  975. }
  976. }
  977. void __init r4k_cache_init(void)
  978. {
  979. extern void build_clear_page(void);
  980. extern void build_copy_page(void);
  981. extern char except_vec2_generic;
  982. struct cpuinfo_mips *c = &current_cpu_data;
  983. /* Default cache error handler for R4000 and R5000 family */
  984. set_uncached_handler (0x100, &except_vec2_generic, 0x80);
  985. probe_pcache();
  986. setup_scache();
  987. r4k_blast_dcache_page_setup();
  988. r4k_blast_dcache_page_indexed_setup();
  989. r4k_blast_dcache_setup();
  990. r4k_blast_icache_page_setup();
  991. r4k_blast_icache_page_indexed_setup();
  992. r4k_blast_icache_setup();
  993. r4k_blast_scache_page_setup();
  994. r4k_blast_scache_page_indexed_setup();
  995. r4k_blast_scache_setup();
  996. /*
  997. * Some MIPS32 and MIPS64 processors have physically indexed caches.
  998. * This code supports virtually indexed processors and will be
  999. * unnecessarily inefficient on physically indexed processors.
  1000. */
  1001. shm_align_mask = max_t( unsigned long,
  1002. c->dcache.sets * c->dcache.linesz - 1,
  1003. PAGE_SIZE - 1);
  1004. flush_cache_all = r4k_flush_cache_all;
  1005. __flush_cache_all = r4k___flush_cache_all;
  1006. flush_cache_mm = r4k_flush_cache_mm;
  1007. flush_cache_page = r4k_flush_cache_page;
  1008. flush_icache_page = r4k_flush_icache_page;
  1009. flush_cache_range = r4k_flush_cache_range;
  1010. flush_cache_sigtramp = r4k_flush_cache_sigtramp;
  1011. flush_icache_all = r4k_flush_icache_all;
  1012. flush_data_cache_page = r4k_flush_data_cache_page;
  1013. flush_icache_range = r4k_flush_icache_range;
  1014. #ifdef CONFIG_DMA_NONCOHERENT
  1015. _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
  1016. _dma_cache_wback = r4k_dma_cache_wback_inv;
  1017. _dma_cache_inv = r4k_dma_cache_inv;
  1018. #endif
  1019. build_clear_page();
  1020. build_copy_page();
  1021. local_r4k___flush_cache_all(NULL);
  1022. coherency_setup();
  1023. }