c-r4k.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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 (davem@davemloft.net)
  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/hardirq.h>
  11. #include <linux/init.h>
  12. #include <linux/highmem.h>
  13. #include <linux/kernel.h>
  14. #include <linux/linkage.h>
  15. #include <linux/preempt.h>
  16. #include <linux/sched.h>
  17. #include <linux/smp.h>
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/bitops.h>
  21. #include <asm/bcache.h>
  22. #include <asm/bootinfo.h>
  23. #include <asm/cache.h>
  24. #include <asm/cacheops.h>
  25. #include <asm/cpu.h>
  26. #include <asm/cpu-features.h>
  27. #include <asm/cpu-type.h>
  28. #include <asm/io.h>
  29. #include <asm/page.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/r4kcache.h>
  32. #include <asm/sections.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/war.h>
  35. #include <asm/cacheflush.h> /* for run_uncached() */
  36. #include <asm/traps.h>
  37. #include <asm/dma-coherence.h>
  38. /*
  39. * Special Variant of smp_call_function for use by cache functions:
  40. *
  41. * o No return value
  42. * o collapses to normal function call on UP kernels
  43. * o collapses to normal function call on systems with a single shared
  44. * primary cache.
  45. * o doesn't disable interrupts on the local CPU
  46. */
  47. static inline void r4k_on_each_cpu(void (*func) (void *info), void *info)
  48. {
  49. preempt_disable();
  50. #if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
  51. smp_call_function(func, info, 1);
  52. #endif
  53. func(info);
  54. preempt_enable();
  55. }
  56. #if defined(CONFIG_MIPS_CMP)
  57. #define cpu_has_safe_index_cacheops 0
  58. #else
  59. #define cpu_has_safe_index_cacheops 1
  60. #endif
  61. /*
  62. * Must die.
  63. */
  64. static unsigned long icache_size __read_mostly;
  65. static unsigned long dcache_size __read_mostly;
  66. static unsigned long scache_size __read_mostly;
  67. /*
  68. * Dummy cache handling routines for machines without boardcaches
  69. */
  70. static void cache_noop(void) {}
  71. static struct bcache_ops no_sc_ops = {
  72. .bc_enable = (void *)cache_noop,
  73. .bc_disable = (void *)cache_noop,
  74. .bc_wback_inv = (void *)cache_noop,
  75. .bc_inv = (void *)cache_noop
  76. };
  77. struct bcache_ops *bcops = &no_sc_ops;
  78. #define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
  79. #define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
  80. #define R4600_HIT_CACHEOP_WAR_IMPL \
  81. do { \
  82. if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
  83. *(volatile unsigned long *)CKSEG1; \
  84. if (R4600_V1_HIT_CACHEOP_WAR) \
  85. __asm__ __volatile__("nop;nop;nop;nop"); \
  86. } while (0)
  87. static void (*r4k_blast_dcache_page)(unsigned long addr);
  88. static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
  89. {
  90. R4600_HIT_CACHEOP_WAR_IMPL;
  91. blast_dcache32_page(addr);
  92. }
  93. static inline void r4k_blast_dcache_page_dc64(unsigned long addr)
  94. {
  95. R4600_HIT_CACHEOP_WAR_IMPL;
  96. blast_dcache64_page(addr);
  97. }
  98. static void r4k_blast_dcache_page_setup(void)
  99. {
  100. unsigned long dc_lsize = cpu_dcache_line_size();
  101. if (dc_lsize == 0)
  102. r4k_blast_dcache_page = (void *)cache_noop;
  103. else if (dc_lsize == 16)
  104. r4k_blast_dcache_page = blast_dcache16_page;
  105. else if (dc_lsize == 32)
  106. r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
  107. else if (dc_lsize == 64)
  108. r4k_blast_dcache_page = r4k_blast_dcache_page_dc64;
  109. }
  110. static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
  111. static void r4k_blast_dcache_page_indexed_setup(void)
  112. {
  113. unsigned long dc_lsize = cpu_dcache_line_size();
  114. if (dc_lsize == 0)
  115. r4k_blast_dcache_page_indexed = (void *)cache_noop;
  116. else if (dc_lsize == 16)
  117. r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
  118. else if (dc_lsize == 32)
  119. r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
  120. else if (dc_lsize == 64)
  121. r4k_blast_dcache_page_indexed = blast_dcache64_page_indexed;
  122. }
  123. void (* r4k_blast_dcache)(void);
  124. EXPORT_SYMBOL(r4k_blast_dcache);
  125. static void r4k_blast_dcache_setup(void)
  126. {
  127. unsigned long dc_lsize = cpu_dcache_line_size();
  128. if (dc_lsize == 0)
  129. r4k_blast_dcache = (void *)cache_noop;
  130. else if (dc_lsize == 16)
  131. r4k_blast_dcache = blast_dcache16;
  132. else if (dc_lsize == 32)
  133. r4k_blast_dcache = blast_dcache32;
  134. else if (dc_lsize == 64)
  135. r4k_blast_dcache = blast_dcache64;
  136. }
  137. /* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
  138. #define JUMP_TO_ALIGN(order) \
  139. __asm__ __volatile__( \
  140. "b\t1f\n\t" \
  141. ".align\t" #order "\n\t" \
  142. "1:\n\t" \
  143. )
  144. #define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
  145. #define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
  146. static inline void blast_r4600_v1_icache32(void)
  147. {
  148. unsigned long flags;
  149. local_irq_save(flags);
  150. blast_icache32();
  151. local_irq_restore(flags);
  152. }
  153. static inline void tx49_blast_icache32(void)
  154. {
  155. unsigned long start = INDEX_BASE;
  156. unsigned long end = start + current_cpu_data.icache.waysize;
  157. unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
  158. unsigned long ws_end = current_cpu_data.icache.ways <<
  159. current_cpu_data.icache.waybit;
  160. unsigned long ws, addr;
  161. CACHE32_UNROLL32_ALIGN2;
  162. /* I'm in even chunk. blast odd chunks */
  163. for (ws = 0; ws < ws_end; ws += ws_inc)
  164. for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
  165. cache32_unroll32(addr|ws, Index_Invalidate_I);
  166. CACHE32_UNROLL32_ALIGN;
  167. /* I'm in odd chunk. blast even chunks */
  168. for (ws = 0; ws < ws_end; ws += ws_inc)
  169. for (addr = start; addr < end; addr += 0x400 * 2)
  170. cache32_unroll32(addr|ws, Index_Invalidate_I);
  171. }
  172. static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
  173. {
  174. unsigned long flags;
  175. local_irq_save(flags);
  176. blast_icache32_page_indexed(page);
  177. local_irq_restore(flags);
  178. }
  179. static inline void tx49_blast_icache32_page_indexed(unsigned long page)
  180. {
  181. unsigned long indexmask = current_cpu_data.icache.waysize - 1;
  182. unsigned long start = INDEX_BASE + (page & indexmask);
  183. unsigned long end = start + PAGE_SIZE;
  184. unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
  185. unsigned long ws_end = current_cpu_data.icache.ways <<
  186. current_cpu_data.icache.waybit;
  187. unsigned long ws, addr;
  188. CACHE32_UNROLL32_ALIGN2;
  189. /* I'm in even chunk. blast odd chunks */
  190. for (ws = 0; ws < ws_end; ws += ws_inc)
  191. for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
  192. cache32_unroll32(addr|ws, Index_Invalidate_I);
  193. CACHE32_UNROLL32_ALIGN;
  194. /* I'm in odd chunk. blast even chunks */
  195. for (ws = 0; ws < ws_end; ws += ws_inc)
  196. for (addr = start; addr < end; addr += 0x400 * 2)
  197. cache32_unroll32(addr|ws, Index_Invalidate_I);
  198. }
  199. static void (* r4k_blast_icache_page)(unsigned long addr);
  200. static void r4k_blast_icache_page_setup(void)
  201. {
  202. unsigned long ic_lsize = cpu_icache_line_size();
  203. if (ic_lsize == 0)
  204. r4k_blast_icache_page = (void *)cache_noop;
  205. else if (ic_lsize == 16)
  206. r4k_blast_icache_page = blast_icache16_page;
  207. else if (ic_lsize == 32 && current_cpu_type() == CPU_LOONGSON2)
  208. r4k_blast_icache_page = loongson2_blast_icache32_page;
  209. else if (ic_lsize == 32)
  210. r4k_blast_icache_page = blast_icache32_page;
  211. else if (ic_lsize == 64)
  212. r4k_blast_icache_page = blast_icache64_page;
  213. }
  214. static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
  215. static void r4k_blast_icache_page_indexed_setup(void)
  216. {
  217. unsigned long ic_lsize = cpu_icache_line_size();
  218. if (ic_lsize == 0)
  219. r4k_blast_icache_page_indexed = (void *)cache_noop;
  220. else if (ic_lsize == 16)
  221. r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
  222. else if (ic_lsize == 32) {
  223. if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
  224. r4k_blast_icache_page_indexed =
  225. blast_icache32_r4600_v1_page_indexed;
  226. else if (TX49XX_ICACHE_INDEX_INV_WAR)
  227. r4k_blast_icache_page_indexed =
  228. tx49_blast_icache32_page_indexed;
  229. else if (current_cpu_type() == CPU_LOONGSON2)
  230. r4k_blast_icache_page_indexed =
  231. loongson2_blast_icache32_page_indexed;
  232. else
  233. r4k_blast_icache_page_indexed =
  234. blast_icache32_page_indexed;
  235. } else if (ic_lsize == 64)
  236. r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
  237. }
  238. void (* r4k_blast_icache)(void);
  239. EXPORT_SYMBOL(r4k_blast_icache);
  240. static void r4k_blast_icache_setup(void)
  241. {
  242. unsigned long ic_lsize = cpu_icache_line_size();
  243. if (ic_lsize == 0)
  244. r4k_blast_icache = (void *)cache_noop;
  245. else if (ic_lsize == 16)
  246. r4k_blast_icache = blast_icache16;
  247. else if (ic_lsize == 32) {
  248. if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
  249. r4k_blast_icache = blast_r4600_v1_icache32;
  250. else if (TX49XX_ICACHE_INDEX_INV_WAR)
  251. r4k_blast_icache = tx49_blast_icache32;
  252. else if (current_cpu_type() == CPU_LOONGSON2)
  253. r4k_blast_icache = loongson2_blast_icache32;
  254. else
  255. r4k_blast_icache = blast_icache32;
  256. } else if (ic_lsize == 64)
  257. r4k_blast_icache = blast_icache64;
  258. }
  259. static void (* r4k_blast_scache_page)(unsigned long addr);
  260. static void r4k_blast_scache_page_setup(void)
  261. {
  262. unsigned long sc_lsize = cpu_scache_line_size();
  263. if (scache_size == 0)
  264. r4k_blast_scache_page = (void *)cache_noop;
  265. else if (sc_lsize == 16)
  266. r4k_blast_scache_page = blast_scache16_page;
  267. else if (sc_lsize == 32)
  268. r4k_blast_scache_page = blast_scache32_page;
  269. else if (sc_lsize == 64)
  270. r4k_blast_scache_page = blast_scache64_page;
  271. else if (sc_lsize == 128)
  272. r4k_blast_scache_page = blast_scache128_page;
  273. }
  274. static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
  275. static void r4k_blast_scache_page_indexed_setup(void)
  276. {
  277. unsigned long sc_lsize = cpu_scache_line_size();
  278. if (scache_size == 0)
  279. r4k_blast_scache_page_indexed = (void *)cache_noop;
  280. else if (sc_lsize == 16)
  281. r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
  282. else if (sc_lsize == 32)
  283. r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
  284. else if (sc_lsize == 64)
  285. r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
  286. else if (sc_lsize == 128)
  287. r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
  288. }
  289. static void (* r4k_blast_scache)(void);
  290. static void r4k_blast_scache_setup(void)
  291. {
  292. unsigned long sc_lsize = cpu_scache_line_size();
  293. if (scache_size == 0)
  294. r4k_blast_scache = (void *)cache_noop;
  295. else if (sc_lsize == 16)
  296. r4k_blast_scache = blast_scache16;
  297. else if (sc_lsize == 32)
  298. r4k_blast_scache = blast_scache32;
  299. else if (sc_lsize == 64)
  300. r4k_blast_scache = blast_scache64;
  301. else if (sc_lsize == 128)
  302. r4k_blast_scache = blast_scache128;
  303. }
  304. static inline void local_r4k___flush_cache_all(void * args)
  305. {
  306. switch (current_cpu_type()) {
  307. case CPU_LOONGSON2:
  308. case CPU_R4000SC:
  309. case CPU_R4000MC:
  310. case CPU_R4400SC:
  311. case CPU_R4400MC:
  312. case CPU_R10000:
  313. case CPU_R12000:
  314. case CPU_R14000:
  315. /*
  316. * These caches are inclusive caches, that is, if something
  317. * is not cached in the S-cache, we know it also won't be
  318. * in one of the primary caches.
  319. */
  320. r4k_blast_scache();
  321. break;
  322. default:
  323. r4k_blast_dcache();
  324. r4k_blast_icache();
  325. break;
  326. }
  327. }
  328. static void r4k___flush_cache_all(void)
  329. {
  330. r4k_on_each_cpu(local_r4k___flush_cache_all, NULL);
  331. }
  332. static inline int has_valid_asid(const struct mm_struct *mm)
  333. {
  334. #if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
  335. int i;
  336. for_each_online_cpu(i)
  337. if (cpu_context(i, mm))
  338. return 1;
  339. return 0;
  340. #else
  341. return cpu_context(smp_processor_id(), mm);
  342. #endif
  343. }
  344. static void r4k__flush_cache_vmap(void)
  345. {
  346. r4k_blast_dcache();
  347. }
  348. static void r4k__flush_cache_vunmap(void)
  349. {
  350. r4k_blast_dcache();
  351. }
  352. static inline void local_r4k_flush_cache_range(void * args)
  353. {
  354. struct vm_area_struct *vma = args;
  355. int exec = vma->vm_flags & VM_EXEC;
  356. if (!(has_valid_asid(vma->vm_mm)))
  357. return;
  358. r4k_blast_dcache();
  359. if (exec)
  360. r4k_blast_icache();
  361. }
  362. static void r4k_flush_cache_range(struct vm_area_struct *vma,
  363. unsigned long start, unsigned long end)
  364. {
  365. int exec = vma->vm_flags & VM_EXEC;
  366. if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
  367. r4k_on_each_cpu(local_r4k_flush_cache_range, vma);
  368. }
  369. static inline void local_r4k_flush_cache_mm(void * args)
  370. {
  371. struct mm_struct *mm = args;
  372. if (!has_valid_asid(mm))
  373. return;
  374. /*
  375. * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
  376. * only flush the primary caches but R10000 and R12000 behave sane ...
  377. * R4000SC and R4400SC indexed S-cache ops also invalidate primary
  378. * caches, so we can bail out early.
  379. */
  380. if (current_cpu_type() == CPU_R4000SC ||
  381. current_cpu_type() == CPU_R4000MC ||
  382. current_cpu_type() == CPU_R4400SC ||
  383. current_cpu_type() == CPU_R4400MC) {
  384. r4k_blast_scache();
  385. return;
  386. }
  387. r4k_blast_dcache();
  388. }
  389. static void r4k_flush_cache_mm(struct mm_struct *mm)
  390. {
  391. if (!cpu_has_dc_aliases)
  392. return;
  393. r4k_on_each_cpu(local_r4k_flush_cache_mm, mm);
  394. }
  395. struct flush_cache_page_args {
  396. struct vm_area_struct *vma;
  397. unsigned long addr;
  398. unsigned long pfn;
  399. };
  400. static inline void local_r4k_flush_cache_page(void *args)
  401. {
  402. struct flush_cache_page_args *fcp_args = args;
  403. struct vm_area_struct *vma = fcp_args->vma;
  404. unsigned long addr = fcp_args->addr;
  405. struct page *page = pfn_to_page(fcp_args->pfn);
  406. int exec = vma->vm_flags & VM_EXEC;
  407. struct mm_struct *mm = vma->vm_mm;
  408. int map_coherent = 0;
  409. pgd_t *pgdp;
  410. pud_t *pudp;
  411. pmd_t *pmdp;
  412. pte_t *ptep;
  413. void *vaddr;
  414. /*
  415. * If ownes no valid ASID yet, cannot possibly have gotten
  416. * this page into the cache.
  417. */
  418. if (!has_valid_asid(mm))
  419. return;
  420. addr &= PAGE_MASK;
  421. pgdp = pgd_offset(mm, addr);
  422. pudp = pud_offset(pgdp, addr);
  423. pmdp = pmd_offset(pudp, addr);
  424. ptep = pte_offset(pmdp, addr);
  425. /*
  426. * If the page isn't marked valid, the page cannot possibly be
  427. * in the cache.
  428. */
  429. if (!(pte_present(*ptep)))
  430. return;
  431. if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
  432. vaddr = NULL;
  433. else {
  434. /*
  435. * Use kmap_coherent or kmap_atomic to do flushes for
  436. * another ASID than the current one.
  437. */
  438. map_coherent = (cpu_has_dc_aliases &&
  439. page_mapped(page) && !Page_dcache_dirty(page));
  440. if (map_coherent)
  441. vaddr = kmap_coherent(page, addr);
  442. else
  443. vaddr = kmap_atomic(page);
  444. addr = (unsigned long)vaddr;
  445. }
  446. if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
  447. r4k_blast_dcache_page(addr);
  448. if (exec && !cpu_icache_snoops_remote_store)
  449. r4k_blast_scache_page(addr);
  450. }
  451. if (exec) {
  452. if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
  453. int cpu = smp_processor_id();
  454. if (cpu_context(cpu, mm) != 0)
  455. drop_mmu_context(mm, cpu);
  456. } else
  457. r4k_blast_icache_page(addr);
  458. }
  459. if (vaddr) {
  460. if (map_coherent)
  461. kunmap_coherent();
  462. else
  463. kunmap_atomic(vaddr);
  464. }
  465. }
  466. static void r4k_flush_cache_page(struct vm_area_struct *vma,
  467. unsigned long addr, unsigned long pfn)
  468. {
  469. struct flush_cache_page_args args;
  470. args.vma = vma;
  471. args.addr = addr;
  472. args.pfn = pfn;
  473. r4k_on_each_cpu(local_r4k_flush_cache_page, &args);
  474. }
  475. static inline void local_r4k_flush_data_cache_page(void * addr)
  476. {
  477. r4k_blast_dcache_page((unsigned long) addr);
  478. }
  479. static void r4k_flush_data_cache_page(unsigned long addr)
  480. {
  481. if (in_atomic())
  482. local_r4k_flush_data_cache_page((void *)addr);
  483. else
  484. r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr);
  485. }
  486. struct flush_icache_range_args {
  487. unsigned long start;
  488. unsigned long end;
  489. };
  490. static inline void local_r4k_flush_icache_range(unsigned long start, unsigned long end)
  491. {
  492. if (!cpu_has_ic_fills_f_dc) {
  493. if (end - start >= dcache_size) {
  494. r4k_blast_dcache();
  495. } else {
  496. R4600_HIT_CACHEOP_WAR_IMPL;
  497. protected_blast_dcache_range(start, end);
  498. }
  499. }
  500. if (end - start > icache_size)
  501. r4k_blast_icache();
  502. else {
  503. switch (boot_cpu_type()) {
  504. case CPU_LOONGSON2:
  505. protected_loongson2_blast_icache_range(start, end);
  506. break;
  507. default:
  508. protected_blast_icache_range(start, end);
  509. break;
  510. }
  511. }
  512. }
  513. static inline void local_r4k_flush_icache_range_ipi(void *args)
  514. {
  515. struct flush_icache_range_args *fir_args = args;
  516. unsigned long start = fir_args->start;
  517. unsigned long end = fir_args->end;
  518. local_r4k_flush_icache_range(start, end);
  519. }
  520. static void r4k_flush_icache_range(unsigned long start, unsigned long end)
  521. {
  522. struct flush_icache_range_args args;
  523. args.start = start;
  524. args.end = end;
  525. r4k_on_each_cpu(local_r4k_flush_icache_range_ipi, &args);
  526. instruction_hazard();
  527. }
  528. #ifdef CONFIG_DMA_NONCOHERENT
  529. static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
  530. {
  531. /* Catch bad driver code */
  532. BUG_ON(size == 0);
  533. preempt_disable();
  534. if (cpu_has_inclusive_pcaches) {
  535. if (size >= scache_size)
  536. r4k_blast_scache();
  537. else
  538. blast_scache_range(addr, addr + size);
  539. preempt_enable();
  540. __sync();
  541. return;
  542. }
  543. /*
  544. * Either no secondary cache or the available caches don't have the
  545. * subset property so we have to flush the primary caches
  546. * explicitly
  547. */
  548. if (cpu_has_safe_index_cacheops && size >= dcache_size) {
  549. r4k_blast_dcache();
  550. } else {
  551. R4600_HIT_CACHEOP_WAR_IMPL;
  552. blast_dcache_range(addr, addr + size);
  553. }
  554. preempt_enable();
  555. bc_wback_inv(addr, size);
  556. __sync();
  557. }
  558. static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
  559. {
  560. /* Catch bad driver code */
  561. BUG_ON(size == 0);
  562. preempt_disable();
  563. if (cpu_has_inclusive_pcaches) {
  564. if (size >= scache_size)
  565. r4k_blast_scache();
  566. else {
  567. /*
  568. * There is no clearly documented alignment requirement
  569. * for the cache instruction on MIPS processors and
  570. * some processors, among them the RM5200 and RM7000
  571. * QED processors will throw an address error for cache
  572. * hit ops with insufficient alignment. Solved by
  573. * aligning the address to cache line size.
  574. */
  575. blast_inv_scache_range(addr, addr + size);
  576. }
  577. preempt_enable();
  578. __sync();
  579. return;
  580. }
  581. if (cpu_has_safe_index_cacheops && size >= dcache_size) {
  582. r4k_blast_dcache();
  583. } else {
  584. R4600_HIT_CACHEOP_WAR_IMPL;
  585. blast_inv_dcache_range(addr, addr + size);
  586. }
  587. preempt_enable();
  588. bc_inv(addr, size);
  589. __sync();
  590. }
  591. #endif /* CONFIG_DMA_NONCOHERENT */
  592. /*
  593. * While we're protected against bad userland addresses we don't care
  594. * very much about what happens in that case. Usually a segmentation
  595. * fault will dump the process later on anyway ...
  596. */
  597. static void local_r4k_flush_cache_sigtramp(void * arg)
  598. {
  599. unsigned long ic_lsize = cpu_icache_line_size();
  600. unsigned long dc_lsize = cpu_dcache_line_size();
  601. unsigned long sc_lsize = cpu_scache_line_size();
  602. unsigned long addr = (unsigned long) arg;
  603. R4600_HIT_CACHEOP_WAR_IMPL;
  604. if (dc_lsize)
  605. protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
  606. if (!cpu_icache_snoops_remote_store && scache_size)
  607. protected_writeback_scache_line(addr & ~(sc_lsize - 1));
  608. if (ic_lsize)
  609. protected_flush_icache_line(addr & ~(ic_lsize - 1));
  610. if (MIPS4K_ICACHE_REFILL_WAR) {
  611. __asm__ __volatile__ (
  612. ".set push\n\t"
  613. ".set noat\n\t"
  614. ".set mips3\n\t"
  615. #ifdef CONFIG_32BIT
  616. "la $at,1f\n\t"
  617. #endif
  618. #ifdef CONFIG_64BIT
  619. "dla $at,1f\n\t"
  620. #endif
  621. "cache %0,($at)\n\t"
  622. "nop; nop; nop\n"
  623. "1:\n\t"
  624. ".set pop"
  625. :
  626. : "i" (Hit_Invalidate_I));
  627. }
  628. if (MIPS_CACHE_SYNC_WAR)
  629. __asm__ __volatile__ ("sync");
  630. }
  631. static void r4k_flush_cache_sigtramp(unsigned long addr)
  632. {
  633. r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr);
  634. }
  635. static void r4k_flush_icache_all(void)
  636. {
  637. if (cpu_has_vtag_icache)
  638. r4k_blast_icache();
  639. }
  640. struct flush_kernel_vmap_range_args {
  641. unsigned long vaddr;
  642. int size;
  643. };
  644. static inline void local_r4k_flush_kernel_vmap_range(void *args)
  645. {
  646. struct flush_kernel_vmap_range_args *vmra = args;
  647. unsigned long vaddr = vmra->vaddr;
  648. int size = vmra->size;
  649. /*
  650. * Aliases only affect the primary caches so don't bother with
  651. * S-caches or T-caches.
  652. */
  653. if (cpu_has_safe_index_cacheops && size >= dcache_size)
  654. r4k_blast_dcache();
  655. else {
  656. R4600_HIT_CACHEOP_WAR_IMPL;
  657. blast_dcache_range(vaddr, vaddr + size);
  658. }
  659. }
  660. static void r4k_flush_kernel_vmap_range(unsigned long vaddr, int size)
  661. {
  662. struct flush_kernel_vmap_range_args args;
  663. args.vaddr = (unsigned long) vaddr;
  664. args.size = size;
  665. r4k_on_each_cpu(local_r4k_flush_kernel_vmap_range, &args);
  666. }
  667. static inline void rm7k_erratum31(void)
  668. {
  669. const unsigned long ic_lsize = 32;
  670. unsigned long addr;
  671. /* RM7000 erratum #31. The icache is screwed at startup. */
  672. write_c0_taglo(0);
  673. write_c0_taghi(0);
  674. for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
  675. __asm__ __volatile__ (
  676. ".set push\n\t"
  677. ".set noreorder\n\t"
  678. ".set mips3\n\t"
  679. "cache\t%1, 0(%0)\n\t"
  680. "cache\t%1, 0x1000(%0)\n\t"
  681. "cache\t%1, 0x2000(%0)\n\t"
  682. "cache\t%1, 0x3000(%0)\n\t"
  683. "cache\t%2, 0(%0)\n\t"
  684. "cache\t%2, 0x1000(%0)\n\t"
  685. "cache\t%2, 0x2000(%0)\n\t"
  686. "cache\t%2, 0x3000(%0)\n\t"
  687. "cache\t%1, 0(%0)\n\t"
  688. "cache\t%1, 0x1000(%0)\n\t"
  689. "cache\t%1, 0x2000(%0)\n\t"
  690. "cache\t%1, 0x3000(%0)\n\t"
  691. ".set pop\n"
  692. :
  693. : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
  694. }
  695. }
  696. static inline void alias_74k_erratum(struct cpuinfo_mips *c)
  697. {
  698. unsigned int imp = c->processor_id & PRID_IMP_MASK;
  699. unsigned int rev = c->processor_id & PRID_REV_MASK;
  700. /*
  701. * Early versions of the 74K do not update the cache tags on a
  702. * vtag miss/ptag hit which can occur in the case of KSEG0/KUSEG
  703. * aliases. In this case it is better to treat the cache as always
  704. * having aliases.
  705. */
  706. switch (imp) {
  707. case PRID_IMP_74K:
  708. if (rev <= PRID_REV_ENCODE_332(2, 4, 0))
  709. c->dcache.flags |= MIPS_CACHE_VTAG;
  710. if (rev == PRID_REV_ENCODE_332(2, 4, 0))
  711. write_c0_config6(read_c0_config6() | MIPS_CONF6_SYND);
  712. break;
  713. case PRID_IMP_1074K:
  714. if (rev <= PRID_REV_ENCODE_332(1, 1, 0)) {
  715. c->dcache.flags |= MIPS_CACHE_VTAG;
  716. write_c0_config6(read_c0_config6() | MIPS_CONF6_SYND);
  717. }
  718. break;
  719. default:
  720. BUG();
  721. }
  722. }
  723. static char *way_string[] = { NULL, "direct mapped", "2-way",
  724. "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
  725. };
  726. static void probe_pcache(void)
  727. {
  728. struct cpuinfo_mips *c = &current_cpu_data;
  729. unsigned int config = read_c0_config();
  730. unsigned int prid = read_c0_prid();
  731. unsigned long config1;
  732. unsigned int lsize;
  733. switch (current_cpu_type()) {
  734. case CPU_R4600: /* QED style two way caches? */
  735. case CPU_R4700:
  736. case CPU_R5000:
  737. case CPU_NEVADA:
  738. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  739. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  740. c->icache.ways = 2;
  741. c->icache.waybit = __ffs(icache_size/2);
  742. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  743. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  744. c->dcache.ways = 2;
  745. c->dcache.waybit= __ffs(dcache_size/2);
  746. c->options |= MIPS_CPU_CACHE_CDEX_P;
  747. break;
  748. case CPU_R5432:
  749. case CPU_R5500:
  750. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  751. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  752. c->icache.ways = 2;
  753. c->icache.waybit= 0;
  754. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  755. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  756. c->dcache.ways = 2;
  757. c->dcache.waybit = 0;
  758. c->options |= MIPS_CPU_CACHE_CDEX_P | MIPS_CPU_PREFETCH;
  759. break;
  760. case CPU_TX49XX:
  761. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  762. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  763. c->icache.ways = 4;
  764. c->icache.waybit= 0;
  765. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  766. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  767. c->dcache.ways = 4;
  768. c->dcache.waybit = 0;
  769. c->options |= MIPS_CPU_CACHE_CDEX_P;
  770. c->options |= MIPS_CPU_PREFETCH;
  771. break;
  772. case CPU_R4000PC:
  773. case CPU_R4000SC:
  774. case CPU_R4000MC:
  775. case CPU_R4400PC:
  776. case CPU_R4400SC:
  777. case CPU_R4400MC:
  778. case CPU_R4300:
  779. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  780. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  781. c->icache.ways = 1;
  782. c->icache.waybit = 0; /* doesn't matter */
  783. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  784. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  785. c->dcache.ways = 1;
  786. c->dcache.waybit = 0; /* does not matter */
  787. c->options |= MIPS_CPU_CACHE_CDEX_P;
  788. break;
  789. case CPU_R10000:
  790. case CPU_R12000:
  791. case CPU_R14000:
  792. icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
  793. c->icache.linesz = 64;
  794. c->icache.ways = 2;
  795. c->icache.waybit = 0;
  796. dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
  797. c->dcache.linesz = 32;
  798. c->dcache.ways = 2;
  799. c->dcache.waybit = 0;
  800. c->options |= MIPS_CPU_PREFETCH;
  801. break;
  802. case CPU_VR4133:
  803. write_c0_config(config & ~VR41_CONF_P4K);
  804. case CPU_VR4131:
  805. /* Workaround for cache instruction bug of VR4131 */
  806. if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
  807. c->processor_id == 0x0c82U) {
  808. config |= 0x00400000U;
  809. if (c->processor_id == 0x0c80U)
  810. config |= VR41_CONF_BP;
  811. write_c0_config(config);
  812. } else
  813. c->options |= MIPS_CPU_CACHE_CDEX_P;
  814. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  815. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  816. c->icache.ways = 2;
  817. c->icache.waybit = __ffs(icache_size/2);
  818. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  819. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  820. c->dcache.ways = 2;
  821. c->dcache.waybit = __ffs(dcache_size/2);
  822. break;
  823. case CPU_VR41XX:
  824. case CPU_VR4111:
  825. case CPU_VR4121:
  826. case CPU_VR4122:
  827. case CPU_VR4181:
  828. case CPU_VR4181A:
  829. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  830. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  831. c->icache.ways = 1;
  832. c->icache.waybit = 0; /* doesn't matter */
  833. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  834. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  835. c->dcache.ways = 1;
  836. c->dcache.waybit = 0; /* does not matter */
  837. c->options |= MIPS_CPU_CACHE_CDEX_P;
  838. break;
  839. case CPU_RM7000:
  840. rm7k_erratum31();
  841. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  842. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  843. c->icache.ways = 4;
  844. c->icache.waybit = __ffs(icache_size / c->icache.ways);
  845. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  846. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  847. c->dcache.ways = 4;
  848. c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
  849. c->options |= MIPS_CPU_CACHE_CDEX_P;
  850. c->options |= MIPS_CPU_PREFETCH;
  851. break;
  852. case CPU_LOONGSON2:
  853. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  854. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  855. if (prid & 0x3)
  856. c->icache.ways = 4;
  857. else
  858. c->icache.ways = 2;
  859. c->icache.waybit = 0;
  860. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  861. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  862. if (prid & 0x3)
  863. c->dcache.ways = 4;
  864. else
  865. c->dcache.ways = 2;
  866. c->dcache.waybit = 0;
  867. break;
  868. default:
  869. if (!(config & MIPS_CONF_M))
  870. panic("Don't know how to probe P-caches on this cpu.");
  871. /*
  872. * So we seem to be a MIPS32 or MIPS64 CPU
  873. * So let's probe the I-cache ...
  874. */
  875. config1 = read_c0_config1();
  876. lsize = (config1 >> 19) & 7;
  877. /* IL == 7 is reserved */
  878. if (lsize == 7)
  879. panic("Invalid icache line size");
  880. c->icache.linesz = lsize ? 2 << lsize : 0;
  881. c->icache.sets = 32 << (((config1 >> 22) + 1) & 7);
  882. c->icache.ways = 1 + ((config1 >> 16) & 7);
  883. icache_size = c->icache.sets *
  884. c->icache.ways *
  885. c->icache.linesz;
  886. c->icache.waybit = __ffs(icache_size/c->icache.ways);
  887. if (config & 0x8) /* VI bit */
  888. c->icache.flags |= MIPS_CACHE_VTAG;
  889. /*
  890. * Now probe the MIPS32 / MIPS64 data cache.
  891. */
  892. c->dcache.flags = 0;
  893. lsize = (config1 >> 10) & 7;
  894. /* DL == 7 is reserved */
  895. if (lsize == 7)
  896. panic("Invalid dcache line size");
  897. c->dcache.linesz = lsize ? 2 << lsize : 0;
  898. c->dcache.sets = 32 << (((config1 >> 13) + 1) & 7);
  899. c->dcache.ways = 1 + ((config1 >> 7) & 7);
  900. dcache_size = c->dcache.sets *
  901. c->dcache.ways *
  902. c->dcache.linesz;
  903. c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
  904. c->options |= MIPS_CPU_PREFETCH;
  905. break;
  906. }
  907. /*
  908. * Processor configuration sanity check for the R4000SC erratum
  909. * #5. With page sizes larger than 32kB there is no possibility
  910. * to get a VCE exception anymore so we don't care about this
  911. * misconfiguration. The case is rather theoretical anyway;
  912. * presumably no vendor is shipping his hardware in the "bad"
  913. * configuration.
  914. */
  915. if ((prid & PRID_IMP_MASK) == PRID_IMP_R4000 &&
  916. (prid & PRID_REV_MASK) < PRID_REV_R4400 &&
  917. !(config & CONF_SC) && c->icache.linesz != 16 &&
  918. PAGE_SIZE <= 0x8000)
  919. panic("Improper R4000SC processor configuration detected");
  920. /* compute a couple of other cache variables */
  921. c->icache.waysize = icache_size / c->icache.ways;
  922. c->dcache.waysize = dcache_size / c->dcache.ways;
  923. c->icache.sets = c->icache.linesz ?
  924. icache_size / (c->icache.linesz * c->icache.ways) : 0;
  925. c->dcache.sets = c->dcache.linesz ?
  926. dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
  927. /*
  928. * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
  929. * 2-way virtually indexed so normally would suffer from aliases. So
  930. * normally they'd suffer from aliases but magic in the hardware deals
  931. * with that for us so we don't need to take care ourselves.
  932. */
  933. switch (current_cpu_type()) {
  934. case CPU_20KC:
  935. case CPU_25KF:
  936. case CPU_SB1:
  937. case CPU_SB1A:
  938. case CPU_XLR:
  939. c->dcache.flags |= MIPS_CACHE_PINDEX;
  940. break;
  941. case CPU_R10000:
  942. case CPU_R12000:
  943. case CPU_R14000:
  944. break;
  945. case CPU_M14KC:
  946. case CPU_M14KEC:
  947. case CPU_24K:
  948. case CPU_34K:
  949. case CPU_74K:
  950. case CPU_1004K:
  951. case CPU_INTERAPTIV:
  952. case CPU_PROAPTIV:
  953. if (current_cpu_type() == CPU_74K)
  954. alias_74k_erratum(c);
  955. if ((read_c0_config7() & (1 << 16))) {
  956. /* effectively physically indexed dcache,
  957. thus no virtual aliases. */
  958. c->dcache.flags |= MIPS_CACHE_PINDEX;
  959. break;
  960. }
  961. default:
  962. if (c->dcache.waysize > PAGE_SIZE)
  963. c->dcache.flags |= MIPS_CACHE_ALIASES;
  964. }
  965. switch (current_cpu_type()) {
  966. case CPU_20KC:
  967. /*
  968. * Some older 20Kc chips doesn't have the 'VI' bit in
  969. * the config register.
  970. */
  971. c->icache.flags |= MIPS_CACHE_VTAG;
  972. break;
  973. case CPU_ALCHEMY:
  974. c->icache.flags |= MIPS_CACHE_IC_F_DC;
  975. break;
  976. case CPU_LOONGSON2:
  977. /*
  978. * LOONGSON2 has 4 way icache, but when using indexed cache op,
  979. * one op will act on all 4 ways
  980. */
  981. c->icache.ways = 1;
  982. }
  983. printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
  984. icache_size >> 10,
  985. c->icache.flags & MIPS_CACHE_VTAG ? "VIVT" : "VIPT",
  986. way_string[c->icache.ways], c->icache.linesz);
  987. printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
  988. dcache_size >> 10, way_string[c->dcache.ways],
  989. (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
  990. (c->dcache.flags & MIPS_CACHE_ALIASES) ?
  991. "cache aliases" : "no aliases",
  992. c->dcache.linesz);
  993. }
  994. /*
  995. * If you even _breathe_ on this function, look at the gcc output and make sure
  996. * it does not pop things on and off the stack for the cache sizing loop that
  997. * executes in KSEG1 space or else you will crash and burn badly. You have
  998. * been warned.
  999. */
  1000. static int probe_scache(void)
  1001. {
  1002. unsigned long flags, addr, begin, end, pow2;
  1003. unsigned int config = read_c0_config();
  1004. struct cpuinfo_mips *c = &current_cpu_data;
  1005. if (config & CONF_SC)
  1006. return 0;
  1007. begin = (unsigned long) &_stext;
  1008. begin &= ~((4 * 1024 * 1024) - 1);
  1009. end = begin + (4 * 1024 * 1024);
  1010. /*
  1011. * This is such a bitch, you'd think they would make it easy to do
  1012. * this. Away you daemons of stupidity!
  1013. */
  1014. local_irq_save(flags);
  1015. /* Fill each size-multiple cache line with a valid tag. */
  1016. pow2 = (64 * 1024);
  1017. for (addr = begin; addr < end; addr = (begin + pow2)) {
  1018. unsigned long *p = (unsigned long *) addr;
  1019. __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
  1020. pow2 <<= 1;
  1021. }
  1022. /* Load first line with zero (therefore invalid) tag. */
  1023. write_c0_taglo(0);
  1024. write_c0_taghi(0);
  1025. __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
  1026. cache_op(Index_Store_Tag_I, begin);
  1027. cache_op(Index_Store_Tag_D, begin);
  1028. cache_op(Index_Store_Tag_SD, begin);
  1029. /* Now search for the wrap around point. */
  1030. pow2 = (128 * 1024);
  1031. for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
  1032. cache_op(Index_Load_Tag_SD, addr);
  1033. __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
  1034. if (!read_c0_taglo())
  1035. break;
  1036. pow2 <<= 1;
  1037. }
  1038. local_irq_restore(flags);
  1039. addr -= begin;
  1040. scache_size = addr;
  1041. c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
  1042. c->scache.ways = 1;
  1043. c->dcache.waybit = 0; /* does not matter */
  1044. return 1;
  1045. }
  1046. static void __init loongson2_sc_init(void)
  1047. {
  1048. struct cpuinfo_mips *c = &current_cpu_data;
  1049. scache_size = 512*1024;
  1050. c->scache.linesz = 32;
  1051. c->scache.ways = 4;
  1052. c->scache.waybit = 0;
  1053. c->scache.waysize = scache_size / (c->scache.ways);
  1054. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  1055. pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  1056. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  1057. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1058. }
  1059. extern int r5k_sc_init(void);
  1060. extern int rm7k_sc_init(void);
  1061. extern int mips_sc_init(void);
  1062. static void setup_scache(void)
  1063. {
  1064. struct cpuinfo_mips *c = &current_cpu_data;
  1065. unsigned int config = read_c0_config();
  1066. int sc_present = 0;
  1067. /*
  1068. * Do the probing thing on R4000SC and R4400SC processors. Other
  1069. * processors don't have a S-cache that would be relevant to the
  1070. * Linux memory management.
  1071. */
  1072. switch (current_cpu_type()) {
  1073. case CPU_R4000SC:
  1074. case CPU_R4000MC:
  1075. case CPU_R4400SC:
  1076. case CPU_R4400MC:
  1077. sc_present = run_uncached(probe_scache);
  1078. if (sc_present)
  1079. c->options |= MIPS_CPU_CACHE_CDEX_S;
  1080. break;
  1081. case CPU_R10000:
  1082. case CPU_R12000:
  1083. case CPU_R14000:
  1084. scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
  1085. c->scache.linesz = 64 << ((config >> 13) & 1);
  1086. c->scache.ways = 2;
  1087. c->scache.waybit= 0;
  1088. sc_present = 1;
  1089. break;
  1090. case CPU_R5000:
  1091. case CPU_NEVADA:
  1092. #ifdef CONFIG_R5000_CPU_SCACHE
  1093. r5k_sc_init();
  1094. #endif
  1095. return;
  1096. case CPU_RM7000:
  1097. #ifdef CONFIG_RM7000_CPU_SCACHE
  1098. rm7k_sc_init();
  1099. #endif
  1100. return;
  1101. case CPU_LOONGSON2:
  1102. loongson2_sc_init();
  1103. return;
  1104. case CPU_XLP:
  1105. /* don't need to worry about L2, fully coherent */
  1106. return;
  1107. default:
  1108. if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M32R2 |
  1109. MIPS_CPU_ISA_M64R1 | MIPS_CPU_ISA_M64R2)) {
  1110. #ifdef CONFIG_MIPS_CPU_SCACHE
  1111. if (mips_sc_init ()) {
  1112. scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
  1113. printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
  1114. scache_size >> 10,
  1115. way_string[c->scache.ways], c->scache.linesz);
  1116. }
  1117. #else
  1118. if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
  1119. panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
  1120. #endif
  1121. return;
  1122. }
  1123. sc_present = 0;
  1124. }
  1125. if (!sc_present)
  1126. return;
  1127. /* compute a couple of other cache variables */
  1128. c->scache.waysize = scache_size / c->scache.ways;
  1129. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  1130. printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  1131. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  1132. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1133. }
  1134. void au1x00_fixup_config_od(void)
  1135. {
  1136. /*
  1137. * c0_config.od (bit 19) was write only (and read as 0)
  1138. * on the early revisions of Alchemy SOCs. It disables the bus
  1139. * transaction overlapping and needs to be set to fix various errata.
  1140. */
  1141. switch (read_c0_prid()) {
  1142. case 0x00030100: /* Au1000 DA */
  1143. case 0x00030201: /* Au1000 HA */
  1144. case 0x00030202: /* Au1000 HB */
  1145. case 0x01030200: /* Au1500 AB */
  1146. /*
  1147. * Au1100 errata actually keeps silence about this bit, so we set it
  1148. * just in case for those revisions that require it to be set according
  1149. * to the (now gone) cpu table.
  1150. */
  1151. case 0x02030200: /* Au1100 AB */
  1152. case 0x02030201: /* Au1100 BA */
  1153. case 0x02030202: /* Au1100 BC */
  1154. set_c0_config(1 << 19);
  1155. break;
  1156. }
  1157. }
  1158. /* CP0 hazard avoidance. */
  1159. #define NXP_BARRIER() \
  1160. __asm__ __volatile__( \
  1161. ".set noreorder\n\t" \
  1162. "nop; nop; nop; nop; nop; nop;\n\t" \
  1163. ".set reorder\n\t")
  1164. static void nxp_pr4450_fixup_config(void)
  1165. {
  1166. unsigned long config0;
  1167. config0 = read_c0_config();
  1168. /* clear all three cache coherency fields */
  1169. config0 &= ~(0x7 | (7 << 25) | (7 << 28));
  1170. config0 |= (((_page_cachable_default >> _CACHE_SHIFT) << 0) |
  1171. ((_page_cachable_default >> _CACHE_SHIFT) << 25) |
  1172. ((_page_cachable_default >> _CACHE_SHIFT) << 28));
  1173. write_c0_config(config0);
  1174. NXP_BARRIER();
  1175. }
  1176. static int cca = -1;
  1177. static int __init cca_setup(char *str)
  1178. {
  1179. get_option(&str, &cca);
  1180. return 0;
  1181. }
  1182. early_param("cca", cca_setup);
  1183. static void coherency_setup(void)
  1184. {
  1185. if (cca < 0 || cca > 7)
  1186. cca = read_c0_config() & CONF_CM_CMASK;
  1187. _page_cachable_default = cca << _CACHE_SHIFT;
  1188. pr_debug("Using cache attribute %d\n", cca);
  1189. change_c0_config(CONF_CM_CMASK, cca);
  1190. /*
  1191. * c0_status.cu=0 specifies that updates by the sc instruction use
  1192. * the coherency mode specified by the TLB; 1 means cachable
  1193. * coherent update on write will be used. Not all processors have
  1194. * this bit and; some wire it to zero, others like Toshiba had the
  1195. * silly idea of putting something else there ...
  1196. */
  1197. switch (current_cpu_type()) {
  1198. case CPU_R4000PC:
  1199. case CPU_R4000SC:
  1200. case CPU_R4000MC:
  1201. case CPU_R4400PC:
  1202. case CPU_R4400SC:
  1203. case CPU_R4400MC:
  1204. clear_c0_config(CONF_CU);
  1205. break;
  1206. /*
  1207. * We need to catch the early Alchemy SOCs with
  1208. * the write-only co_config.od bit and set it back to one on:
  1209. * Au1000 rev DA, HA, HB; Au1100 AB, BA, BC, Au1500 AB
  1210. */
  1211. case CPU_ALCHEMY:
  1212. au1x00_fixup_config_od();
  1213. break;
  1214. case PRID_IMP_PR4450:
  1215. nxp_pr4450_fixup_config();
  1216. break;
  1217. }
  1218. }
  1219. static void r4k_cache_error_setup(void)
  1220. {
  1221. extern char __weak except_vec2_generic;
  1222. extern char __weak except_vec2_sb1;
  1223. switch (current_cpu_type()) {
  1224. case CPU_SB1:
  1225. case CPU_SB1A:
  1226. set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
  1227. break;
  1228. default:
  1229. set_uncached_handler(0x100, &except_vec2_generic, 0x80);
  1230. break;
  1231. }
  1232. }
  1233. void r4k_cache_init(void)
  1234. {
  1235. extern void build_clear_page(void);
  1236. extern void build_copy_page(void);
  1237. struct cpuinfo_mips *c = &current_cpu_data;
  1238. probe_pcache();
  1239. setup_scache();
  1240. r4k_blast_dcache_page_setup();
  1241. r4k_blast_dcache_page_indexed_setup();
  1242. r4k_blast_dcache_setup();
  1243. r4k_blast_icache_page_setup();
  1244. r4k_blast_icache_page_indexed_setup();
  1245. r4k_blast_icache_setup();
  1246. r4k_blast_scache_page_setup();
  1247. r4k_blast_scache_page_indexed_setup();
  1248. r4k_blast_scache_setup();
  1249. /*
  1250. * Some MIPS32 and MIPS64 processors have physically indexed caches.
  1251. * This code supports virtually indexed processors and will be
  1252. * unnecessarily inefficient on physically indexed processors.
  1253. */
  1254. if (c->dcache.linesz)
  1255. shm_align_mask = max_t( unsigned long,
  1256. c->dcache.sets * c->dcache.linesz - 1,
  1257. PAGE_SIZE - 1);
  1258. else
  1259. shm_align_mask = PAGE_SIZE-1;
  1260. __flush_cache_vmap = r4k__flush_cache_vmap;
  1261. __flush_cache_vunmap = r4k__flush_cache_vunmap;
  1262. flush_cache_all = cache_noop;
  1263. __flush_cache_all = r4k___flush_cache_all;
  1264. flush_cache_mm = r4k_flush_cache_mm;
  1265. flush_cache_page = r4k_flush_cache_page;
  1266. flush_cache_range = r4k_flush_cache_range;
  1267. __flush_kernel_vmap_range = r4k_flush_kernel_vmap_range;
  1268. flush_cache_sigtramp = r4k_flush_cache_sigtramp;
  1269. flush_icache_all = r4k_flush_icache_all;
  1270. local_flush_data_cache_page = local_r4k_flush_data_cache_page;
  1271. flush_data_cache_page = r4k_flush_data_cache_page;
  1272. flush_icache_range = r4k_flush_icache_range;
  1273. local_flush_icache_range = local_r4k_flush_icache_range;
  1274. #if defined(CONFIG_DMA_NONCOHERENT)
  1275. if (coherentio) {
  1276. _dma_cache_wback_inv = (void *)cache_noop;
  1277. _dma_cache_wback = (void *)cache_noop;
  1278. _dma_cache_inv = (void *)cache_noop;
  1279. } else {
  1280. _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
  1281. _dma_cache_wback = r4k_dma_cache_wback_inv;
  1282. _dma_cache_inv = r4k_dma_cache_inv;
  1283. }
  1284. #endif
  1285. build_clear_page();
  1286. build_copy_page();
  1287. /*
  1288. * We want to run CMP kernels on core with and without coherent
  1289. * caches. Therefore, do not use CONFIG_MIPS_CMP to decide whether
  1290. * or not to flush caches.
  1291. */
  1292. local_r4k___flush_cache_all(NULL);
  1293. coherency_setup();
  1294. board_cache_error_setup = r4k_cache_error_setup;
  1295. }