c-r4k.c 38 KB

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