c-r4k.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
  7. * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Ralf Baechle (ralf@gnu.org)
  8. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  9. */
  10. #include <linux/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/sched.h>
  16. #include <linux/smp.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/bitops.h>
  20. #include <asm/bcache.h>
  21. #include <asm/bootinfo.h>
  22. #include <asm/cache.h>
  23. #include <asm/cacheops.h>
  24. #include <asm/cpu.h>
  25. #include <asm/cpu-features.h>
  26. #include <asm/io.h>
  27. #include <asm/page.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/r4kcache.h>
  30. #include <asm/sections.h>
  31. #include <asm/system.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. */
  43. static inline void r4k_on_each_cpu(void (*func) (void *info), void *info,
  44. int wait)
  45. {
  46. preempt_disable();
  47. #if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
  48. smp_call_function(func, info, wait);
  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, 1);
  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, 1);
  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, 1);
  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, KM_USER0);
  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, KM_USER0);
  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, 1);
  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. 1);
  469. }
  470. struct flush_icache_range_args {
  471. unsigned long start;
  472. unsigned long end;
  473. };
  474. static inline void local_r4k_flush_icache_range(unsigned long start, unsigned long end)
  475. {
  476. if (!cpu_has_ic_fills_f_dc) {
  477. if (end - start >= dcache_size) {
  478. r4k_blast_dcache();
  479. } else {
  480. R4600_HIT_CACHEOP_WAR_IMPL;
  481. protected_blast_dcache_range(start, end);
  482. }
  483. }
  484. if (end - start > icache_size)
  485. r4k_blast_icache();
  486. else
  487. protected_blast_icache_range(start, end);
  488. }
  489. static inline void local_r4k_flush_icache_range_ipi(void *args)
  490. {
  491. struct flush_icache_range_args *fir_args = args;
  492. unsigned long start = fir_args->start;
  493. unsigned long end = fir_args->end;
  494. local_r4k_flush_icache_range(start, end);
  495. }
  496. static void r4k_flush_icache_range(unsigned long start, unsigned long end)
  497. {
  498. struct flush_icache_range_args args;
  499. args.start = start;
  500. args.end = end;
  501. r4k_on_each_cpu(local_r4k_flush_icache_range_ipi, &args, 1);
  502. instruction_hazard();
  503. }
  504. #ifdef CONFIG_DMA_NONCOHERENT
  505. static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
  506. {
  507. /* Catch bad driver code */
  508. BUG_ON(size == 0);
  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. return;
  515. }
  516. /*
  517. * Either no secondary cache or the available caches don't have the
  518. * subset property so we have to flush the primary caches
  519. * explicitly
  520. */
  521. if (cpu_has_safe_index_cacheops && size >= dcache_size) {
  522. r4k_blast_dcache();
  523. } else {
  524. R4600_HIT_CACHEOP_WAR_IMPL;
  525. blast_dcache_range(addr, addr + size);
  526. }
  527. bc_wback_inv(addr, size);
  528. }
  529. static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
  530. {
  531. /* Catch bad driver code */
  532. BUG_ON(size == 0);
  533. if (cpu_has_inclusive_pcaches) {
  534. if (size >= scache_size)
  535. r4k_blast_scache();
  536. else {
  537. unsigned long lsize = cpu_scache_line_size();
  538. unsigned long almask = ~(lsize - 1);
  539. /*
  540. * There is no clearly documented alignment requirement
  541. * for the cache instruction on MIPS processors and
  542. * some processors, among them the RM5200 and RM7000
  543. * QED processors will throw an address error for cache
  544. * hit ops with insufficient alignment. Solved by
  545. * aligning the address to cache line size.
  546. */
  547. cache_op(Hit_Writeback_Inv_SD, addr & almask);
  548. cache_op(Hit_Writeback_Inv_SD,
  549. (addr + size - 1) & almask);
  550. blast_inv_scache_range(addr, addr + size);
  551. }
  552. return;
  553. }
  554. if (cpu_has_safe_index_cacheops && size >= dcache_size) {
  555. r4k_blast_dcache();
  556. } else {
  557. unsigned long lsize = cpu_dcache_line_size();
  558. unsigned long almask = ~(lsize - 1);
  559. R4600_HIT_CACHEOP_WAR_IMPL;
  560. cache_op(Hit_Writeback_Inv_D, addr & almask);
  561. cache_op(Hit_Writeback_Inv_D, (addr + size - 1) & almask);
  562. blast_inv_dcache_range(addr, addr + size);
  563. }
  564. bc_inv(addr, size);
  565. }
  566. #endif /* CONFIG_DMA_NONCOHERENT */
  567. /*
  568. * While we're protected against bad userland addresses we don't care
  569. * very much about what happens in that case. Usually a segmentation
  570. * fault will dump the process later on anyway ...
  571. */
  572. static void local_r4k_flush_cache_sigtramp(void * arg)
  573. {
  574. unsigned long ic_lsize = cpu_icache_line_size();
  575. unsigned long dc_lsize = cpu_dcache_line_size();
  576. unsigned long sc_lsize = cpu_scache_line_size();
  577. unsigned long addr = (unsigned long) arg;
  578. R4600_HIT_CACHEOP_WAR_IMPL;
  579. if (dc_lsize)
  580. protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
  581. if (!cpu_icache_snoops_remote_store && scache_size)
  582. protected_writeback_scache_line(addr & ~(sc_lsize - 1));
  583. if (ic_lsize)
  584. protected_flush_icache_line(addr & ~(ic_lsize - 1));
  585. if (MIPS4K_ICACHE_REFILL_WAR) {
  586. __asm__ __volatile__ (
  587. ".set push\n\t"
  588. ".set noat\n\t"
  589. ".set mips3\n\t"
  590. #ifdef CONFIG_32BIT
  591. "la $at,1f\n\t"
  592. #endif
  593. #ifdef CONFIG_64BIT
  594. "dla $at,1f\n\t"
  595. #endif
  596. "cache %0,($at)\n\t"
  597. "nop; nop; nop\n"
  598. "1:\n\t"
  599. ".set pop"
  600. :
  601. : "i" (Hit_Invalidate_I));
  602. }
  603. if (MIPS_CACHE_SYNC_WAR)
  604. __asm__ __volatile__ ("sync");
  605. }
  606. static void r4k_flush_cache_sigtramp(unsigned long addr)
  607. {
  608. r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1);
  609. }
  610. static void r4k_flush_icache_all(void)
  611. {
  612. if (cpu_has_vtag_icache)
  613. r4k_blast_icache();
  614. }
  615. static inline void rm7k_erratum31(void)
  616. {
  617. const unsigned long ic_lsize = 32;
  618. unsigned long addr;
  619. /* RM7000 erratum #31. The icache is screwed at startup. */
  620. write_c0_taglo(0);
  621. write_c0_taghi(0);
  622. for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
  623. __asm__ __volatile__ (
  624. ".set push\n\t"
  625. ".set noreorder\n\t"
  626. ".set mips3\n\t"
  627. "cache\t%1, 0(%0)\n\t"
  628. "cache\t%1, 0x1000(%0)\n\t"
  629. "cache\t%1, 0x2000(%0)\n\t"
  630. "cache\t%1, 0x3000(%0)\n\t"
  631. "cache\t%2, 0(%0)\n\t"
  632. "cache\t%2, 0x1000(%0)\n\t"
  633. "cache\t%2, 0x2000(%0)\n\t"
  634. "cache\t%2, 0x3000(%0)\n\t"
  635. "cache\t%1, 0(%0)\n\t"
  636. "cache\t%1, 0x1000(%0)\n\t"
  637. "cache\t%1, 0x2000(%0)\n\t"
  638. "cache\t%1, 0x3000(%0)\n\t"
  639. ".set pop\n"
  640. :
  641. : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
  642. }
  643. }
  644. static char *way_string[] __cpuinitdata = { NULL, "direct mapped", "2-way",
  645. "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
  646. };
  647. static void __cpuinit probe_pcache(void)
  648. {
  649. struct cpuinfo_mips *c = &current_cpu_data;
  650. unsigned int config = read_c0_config();
  651. unsigned int prid = read_c0_prid();
  652. unsigned long config1;
  653. unsigned int lsize;
  654. switch (c->cputype) {
  655. case CPU_R4600: /* QED style two way caches? */
  656. case CPU_R4700:
  657. case CPU_R5000:
  658. case CPU_NEVADA:
  659. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  660. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  661. c->icache.ways = 2;
  662. c->icache.waybit = __ffs(icache_size/2);
  663. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  664. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  665. c->dcache.ways = 2;
  666. c->dcache.waybit= __ffs(dcache_size/2);
  667. c->options |= MIPS_CPU_CACHE_CDEX_P;
  668. break;
  669. case CPU_R5432:
  670. case CPU_R5500:
  671. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  672. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  673. c->icache.ways = 2;
  674. c->icache.waybit= 0;
  675. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  676. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  677. c->dcache.ways = 2;
  678. c->dcache.waybit = 0;
  679. c->options |= MIPS_CPU_CACHE_CDEX_P | MIPS_CPU_PREFETCH;
  680. break;
  681. case CPU_TX49XX:
  682. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  683. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  684. c->icache.ways = 4;
  685. c->icache.waybit= 0;
  686. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  687. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  688. c->dcache.ways = 4;
  689. c->dcache.waybit = 0;
  690. c->options |= MIPS_CPU_CACHE_CDEX_P;
  691. c->options |= MIPS_CPU_PREFETCH;
  692. break;
  693. case CPU_R4000PC:
  694. case CPU_R4000SC:
  695. case CPU_R4000MC:
  696. case CPU_R4400PC:
  697. case CPU_R4400SC:
  698. case CPU_R4400MC:
  699. case CPU_R4300:
  700. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  701. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  702. c->icache.ways = 1;
  703. c->icache.waybit = 0; /* doesn't matter */
  704. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  705. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  706. c->dcache.ways = 1;
  707. c->dcache.waybit = 0; /* does not matter */
  708. c->options |= MIPS_CPU_CACHE_CDEX_P;
  709. break;
  710. case CPU_R10000:
  711. case CPU_R12000:
  712. case CPU_R14000:
  713. icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
  714. c->icache.linesz = 64;
  715. c->icache.ways = 2;
  716. c->icache.waybit = 0;
  717. dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
  718. c->dcache.linesz = 32;
  719. c->dcache.ways = 2;
  720. c->dcache.waybit = 0;
  721. c->options |= MIPS_CPU_PREFETCH;
  722. break;
  723. case CPU_VR4133:
  724. write_c0_config(config & ~VR41_CONF_P4K);
  725. case CPU_VR4131:
  726. /* Workaround for cache instruction bug of VR4131 */
  727. if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
  728. c->processor_id == 0x0c82U) {
  729. config |= 0x00400000U;
  730. if (c->processor_id == 0x0c80U)
  731. config |= VR41_CONF_BP;
  732. write_c0_config(config);
  733. } else
  734. c->options |= MIPS_CPU_CACHE_CDEX_P;
  735. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  736. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  737. c->icache.ways = 2;
  738. c->icache.waybit = __ffs(icache_size/2);
  739. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  740. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  741. c->dcache.ways = 2;
  742. c->dcache.waybit = __ffs(dcache_size/2);
  743. break;
  744. case CPU_VR41XX:
  745. case CPU_VR4111:
  746. case CPU_VR4121:
  747. case CPU_VR4122:
  748. case CPU_VR4181:
  749. case CPU_VR4181A:
  750. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  751. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  752. c->icache.ways = 1;
  753. c->icache.waybit = 0; /* doesn't matter */
  754. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  755. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  756. c->dcache.ways = 1;
  757. c->dcache.waybit = 0; /* does not matter */
  758. c->options |= MIPS_CPU_CACHE_CDEX_P;
  759. break;
  760. case CPU_RM7000:
  761. rm7k_erratum31();
  762. case CPU_RM9000:
  763. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  764. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  765. c->icache.ways = 4;
  766. c->icache.waybit = __ffs(icache_size / c->icache.ways);
  767. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  768. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  769. c->dcache.ways = 4;
  770. c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
  771. #if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
  772. c->options |= MIPS_CPU_CACHE_CDEX_P;
  773. #endif
  774. c->options |= MIPS_CPU_PREFETCH;
  775. break;
  776. case CPU_LOONGSON2:
  777. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  778. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  779. if (prid & 0x3)
  780. c->icache.ways = 4;
  781. else
  782. c->icache.ways = 2;
  783. c->icache.waybit = 0;
  784. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  785. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  786. if (prid & 0x3)
  787. c->dcache.ways = 4;
  788. else
  789. c->dcache.ways = 2;
  790. c->dcache.waybit = 0;
  791. break;
  792. default:
  793. if (!(config & MIPS_CONF_M))
  794. panic("Don't know how to probe P-caches on this cpu.");
  795. /*
  796. * So we seem to be a MIPS32 or MIPS64 CPU
  797. * So let's probe the I-cache ...
  798. */
  799. config1 = read_c0_config1();
  800. if ((lsize = ((config1 >> 19) & 7)))
  801. c->icache.linesz = 2 << lsize;
  802. else
  803. c->icache.linesz = lsize;
  804. c->icache.sets = 64 << ((config1 >> 22) & 7);
  805. c->icache.ways = 1 + ((config1 >> 16) & 7);
  806. icache_size = c->icache.sets *
  807. c->icache.ways *
  808. c->icache.linesz;
  809. c->icache.waybit = __ffs(icache_size/c->icache.ways);
  810. if (config & 0x8) /* VI bit */
  811. c->icache.flags |= MIPS_CACHE_VTAG;
  812. /*
  813. * Now probe the MIPS32 / MIPS64 data cache.
  814. */
  815. c->dcache.flags = 0;
  816. if ((lsize = ((config1 >> 10) & 7)))
  817. c->dcache.linesz = 2 << lsize;
  818. else
  819. c->dcache.linesz= lsize;
  820. c->dcache.sets = 64 << ((config1 >> 13) & 7);
  821. c->dcache.ways = 1 + ((config1 >> 7) & 7);
  822. dcache_size = c->dcache.sets *
  823. c->dcache.ways *
  824. c->dcache.linesz;
  825. c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
  826. c->options |= MIPS_CPU_PREFETCH;
  827. break;
  828. }
  829. /*
  830. * Processor configuration sanity check for the R4000SC erratum
  831. * #5. With page sizes larger than 32kB there is no possibility
  832. * to get a VCE exception anymore so we don't care about this
  833. * misconfiguration. The case is rather theoretical anyway;
  834. * presumably no vendor is shipping his hardware in the "bad"
  835. * configuration.
  836. */
  837. if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
  838. !(config & CONF_SC) && c->icache.linesz != 16 &&
  839. PAGE_SIZE <= 0x8000)
  840. panic("Improper R4000SC processor configuration detected");
  841. /* compute a couple of other cache variables */
  842. c->icache.waysize = icache_size / c->icache.ways;
  843. c->dcache.waysize = dcache_size / c->dcache.ways;
  844. c->icache.sets = c->icache.linesz ?
  845. icache_size / (c->icache.linesz * c->icache.ways) : 0;
  846. c->dcache.sets = c->dcache.linesz ?
  847. dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
  848. /*
  849. * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
  850. * 2-way virtually indexed so normally would suffer from aliases. So
  851. * normally they'd suffer from aliases but magic in the hardware deals
  852. * with that for us so we don't need to take care ourselves.
  853. */
  854. switch (c->cputype) {
  855. case CPU_20KC:
  856. case CPU_25KF:
  857. case CPU_SB1:
  858. case CPU_SB1A:
  859. c->dcache.flags |= MIPS_CACHE_PINDEX;
  860. break;
  861. case CPU_R10000:
  862. case CPU_R12000:
  863. case CPU_R14000:
  864. break;
  865. case CPU_24K:
  866. case CPU_34K:
  867. case CPU_74K:
  868. case CPU_1004K:
  869. if ((read_c0_config7() & (1 << 16))) {
  870. /* effectively physically indexed dcache,
  871. thus no virtual aliases. */
  872. c->dcache.flags |= MIPS_CACHE_PINDEX;
  873. break;
  874. }
  875. default:
  876. if (c->dcache.waysize > PAGE_SIZE)
  877. c->dcache.flags |= MIPS_CACHE_ALIASES;
  878. }
  879. switch (c->cputype) {
  880. case CPU_20KC:
  881. /*
  882. * Some older 20Kc chips doesn't have the 'VI' bit in
  883. * the config register.
  884. */
  885. c->icache.flags |= MIPS_CACHE_VTAG;
  886. break;
  887. case CPU_ALCHEMY:
  888. c->icache.flags |= MIPS_CACHE_IC_F_DC;
  889. break;
  890. }
  891. #ifdef CONFIG_CPU_LOONGSON2
  892. /*
  893. * LOONGSON2 has 4 way icache, but when using indexed cache op,
  894. * one op will act on all 4 ways
  895. */
  896. c->icache.ways = 1;
  897. #endif
  898. printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
  899. icache_size >> 10,
  900. c->icache.flags & MIPS_CACHE_VTAG ? "VIVT" : "VIPT",
  901. way_string[c->icache.ways], c->icache.linesz);
  902. printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
  903. dcache_size >> 10, way_string[c->dcache.ways],
  904. (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
  905. (c->dcache.flags & MIPS_CACHE_ALIASES) ?
  906. "cache aliases" : "no aliases",
  907. c->dcache.linesz);
  908. }
  909. /*
  910. * If you even _breathe_ on this function, look at the gcc output and make sure
  911. * it does not pop things on and off the stack for the cache sizing loop that
  912. * executes in KSEG1 space or else you will crash and burn badly. You have
  913. * been warned.
  914. */
  915. static int __cpuinit probe_scache(void)
  916. {
  917. unsigned long flags, addr, begin, end, pow2;
  918. unsigned int config = read_c0_config();
  919. struct cpuinfo_mips *c = &current_cpu_data;
  920. int tmp;
  921. if (config & CONF_SC)
  922. return 0;
  923. begin = (unsigned long) &_stext;
  924. begin &= ~((4 * 1024 * 1024) - 1);
  925. end = begin + (4 * 1024 * 1024);
  926. /*
  927. * This is such a bitch, you'd think they would make it easy to do
  928. * this. Away you daemons of stupidity!
  929. */
  930. local_irq_save(flags);
  931. /* Fill each size-multiple cache line with a valid tag. */
  932. pow2 = (64 * 1024);
  933. for (addr = begin; addr < end; addr = (begin + pow2)) {
  934. unsigned long *p = (unsigned long *) addr;
  935. __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
  936. pow2 <<= 1;
  937. }
  938. /* Load first line with zero (therefore invalid) tag. */
  939. write_c0_taglo(0);
  940. write_c0_taghi(0);
  941. __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
  942. cache_op(Index_Store_Tag_I, begin);
  943. cache_op(Index_Store_Tag_D, begin);
  944. cache_op(Index_Store_Tag_SD, begin);
  945. /* Now search for the wrap around point. */
  946. pow2 = (128 * 1024);
  947. tmp = 0;
  948. for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
  949. cache_op(Index_Load_Tag_SD, addr);
  950. __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
  951. if (!read_c0_taglo())
  952. break;
  953. pow2 <<= 1;
  954. }
  955. local_irq_restore(flags);
  956. addr -= begin;
  957. scache_size = addr;
  958. c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
  959. c->scache.ways = 1;
  960. c->dcache.waybit = 0; /* does not matter */
  961. return 1;
  962. }
  963. #if defined(CONFIG_CPU_LOONGSON2)
  964. static void __init loongson2_sc_init(void)
  965. {
  966. struct cpuinfo_mips *c = &current_cpu_data;
  967. scache_size = 512*1024;
  968. c->scache.linesz = 32;
  969. c->scache.ways = 4;
  970. c->scache.waybit = 0;
  971. c->scache.waysize = scache_size / (c->scache.ways);
  972. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  973. pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  974. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  975. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  976. }
  977. #endif
  978. extern int r5k_sc_init(void);
  979. extern int rm7k_sc_init(void);
  980. extern int mips_sc_init(void);
  981. static void __cpuinit setup_scache(void)
  982. {
  983. struct cpuinfo_mips *c = &current_cpu_data;
  984. unsigned int config = read_c0_config();
  985. int sc_present = 0;
  986. /*
  987. * Do the probing thing on R4000SC and R4400SC processors. Other
  988. * processors don't have a S-cache that would be relevant to the
  989. * Linux memory management.
  990. */
  991. switch (c->cputype) {
  992. case CPU_R4000SC:
  993. case CPU_R4000MC:
  994. case CPU_R4400SC:
  995. case CPU_R4400MC:
  996. sc_present = run_uncached(probe_scache);
  997. if (sc_present)
  998. c->options |= MIPS_CPU_CACHE_CDEX_S;
  999. break;
  1000. case CPU_R10000:
  1001. case CPU_R12000:
  1002. case CPU_R14000:
  1003. scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
  1004. c->scache.linesz = 64 << ((config >> 13) & 1);
  1005. c->scache.ways = 2;
  1006. c->scache.waybit= 0;
  1007. sc_present = 1;
  1008. break;
  1009. case CPU_R5000:
  1010. case CPU_NEVADA:
  1011. #ifdef CONFIG_R5000_CPU_SCACHE
  1012. r5k_sc_init();
  1013. #endif
  1014. return;
  1015. case CPU_RM7000:
  1016. case CPU_RM9000:
  1017. #ifdef CONFIG_RM7000_CPU_SCACHE
  1018. rm7k_sc_init();
  1019. #endif
  1020. return;
  1021. #if defined(CONFIG_CPU_LOONGSON2)
  1022. case CPU_LOONGSON2:
  1023. loongson2_sc_init();
  1024. return;
  1025. #endif
  1026. default:
  1027. if (c->isa_level == MIPS_CPU_ISA_M32R1 ||
  1028. c->isa_level == MIPS_CPU_ISA_M32R2 ||
  1029. c->isa_level == MIPS_CPU_ISA_M64R1 ||
  1030. c->isa_level == MIPS_CPU_ISA_M64R2) {
  1031. #ifdef CONFIG_MIPS_CPU_SCACHE
  1032. if (mips_sc_init ()) {
  1033. scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
  1034. printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
  1035. scache_size >> 10,
  1036. way_string[c->scache.ways], c->scache.linesz);
  1037. }
  1038. #else
  1039. if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
  1040. panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
  1041. #endif
  1042. return;
  1043. }
  1044. sc_present = 0;
  1045. }
  1046. if (!sc_present)
  1047. return;
  1048. /* compute a couple of other cache variables */
  1049. c->scache.waysize = scache_size / c->scache.ways;
  1050. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  1051. printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  1052. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  1053. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1054. }
  1055. void au1x00_fixup_config_od(void)
  1056. {
  1057. /*
  1058. * c0_config.od (bit 19) was write only (and read as 0)
  1059. * on the early revisions of Alchemy SOCs. It disables the bus
  1060. * transaction overlapping and needs to be set to fix various errata.
  1061. */
  1062. switch (read_c0_prid()) {
  1063. case 0x00030100: /* Au1000 DA */
  1064. case 0x00030201: /* Au1000 HA */
  1065. case 0x00030202: /* Au1000 HB */
  1066. case 0x01030200: /* Au1500 AB */
  1067. /*
  1068. * Au1100 errata actually keeps silence about this bit, so we set it
  1069. * just in case for those revisions that require it to be set according
  1070. * to the (now gone) cpu table.
  1071. */
  1072. case 0x02030200: /* Au1100 AB */
  1073. case 0x02030201: /* Au1100 BA */
  1074. case 0x02030202: /* Au1100 BC */
  1075. set_c0_config(1 << 19);
  1076. break;
  1077. }
  1078. }
  1079. /* CP0 hazard avoidance. */
  1080. #define NXP_BARRIER() \
  1081. __asm__ __volatile__( \
  1082. ".set noreorder\n\t" \
  1083. "nop; nop; nop; nop; nop; nop;\n\t" \
  1084. ".set reorder\n\t")
  1085. static void nxp_pr4450_fixup_config(void)
  1086. {
  1087. unsigned long config0;
  1088. config0 = read_c0_config();
  1089. /* clear all three cache coherency fields */
  1090. config0 &= ~(0x7 | (7 << 25) | (7 << 28));
  1091. config0 |= (((_page_cachable_default >> _CACHE_SHIFT) << 0) |
  1092. ((_page_cachable_default >> _CACHE_SHIFT) << 25) |
  1093. ((_page_cachable_default >> _CACHE_SHIFT) << 28));
  1094. write_c0_config(config0);
  1095. NXP_BARRIER();
  1096. }
  1097. static int __cpuinitdata cca = -1;
  1098. static int __init cca_setup(char *str)
  1099. {
  1100. get_option(&str, &cca);
  1101. return 1;
  1102. }
  1103. __setup("cca=", cca_setup);
  1104. static void __cpuinit coherency_setup(void)
  1105. {
  1106. if (cca < 0 || cca > 7)
  1107. cca = read_c0_config() & CONF_CM_CMASK;
  1108. _page_cachable_default = cca << _CACHE_SHIFT;
  1109. pr_debug("Using cache attribute %d\n", cca);
  1110. change_c0_config(CONF_CM_CMASK, cca);
  1111. /*
  1112. * c0_status.cu=0 specifies that updates by the sc instruction use
  1113. * the coherency mode specified by the TLB; 1 means cachable
  1114. * coherent update on write will be used. Not all processors have
  1115. * this bit and; some wire it to zero, others like Toshiba had the
  1116. * silly idea of putting something else there ...
  1117. */
  1118. switch (current_cpu_type()) {
  1119. case CPU_R4000PC:
  1120. case CPU_R4000SC:
  1121. case CPU_R4000MC:
  1122. case CPU_R4400PC:
  1123. case CPU_R4400SC:
  1124. case CPU_R4400MC:
  1125. clear_c0_config(CONF_CU);
  1126. break;
  1127. /*
  1128. * We need to catch the early Alchemy SOCs with
  1129. * the write-only co_config.od bit and set it back to one on:
  1130. * Au1000 rev DA, HA, HB; Au1100 AB, BA, BC, Au1500 AB
  1131. */
  1132. case CPU_ALCHEMY:
  1133. au1x00_fixup_config_od();
  1134. break;
  1135. case PRID_IMP_PR4450:
  1136. nxp_pr4450_fixup_config();
  1137. break;
  1138. }
  1139. }
  1140. #if defined(CONFIG_DMA_NONCOHERENT)
  1141. static int __cpuinitdata coherentio;
  1142. static int __init setcoherentio(char *str)
  1143. {
  1144. coherentio = 1;
  1145. return 1;
  1146. }
  1147. __setup("coherentio", setcoherentio);
  1148. #endif
  1149. void __cpuinit r4k_cache_init(void)
  1150. {
  1151. extern void build_clear_page(void);
  1152. extern void build_copy_page(void);
  1153. extern char __weak except_vec2_generic;
  1154. extern char __weak except_vec2_sb1;
  1155. struct cpuinfo_mips *c = &current_cpu_data;
  1156. switch (c->cputype) {
  1157. case CPU_SB1:
  1158. case CPU_SB1A:
  1159. set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
  1160. break;
  1161. default:
  1162. set_uncached_handler(0x100, &except_vec2_generic, 0x80);
  1163. break;
  1164. }
  1165. probe_pcache();
  1166. setup_scache();
  1167. r4k_blast_dcache_page_setup();
  1168. r4k_blast_dcache_page_indexed_setup();
  1169. r4k_blast_dcache_setup();
  1170. r4k_blast_icache_page_setup();
  1171. r4k_blast_icache_page_indexed_setup();
  1172. r4k_blast_icache_setup();
  1173. r4k_blast_scache_page_setup();
  1174. r4k_blast_scache_page_indexed_setup();
  1175. r4k_blast_scache_setup();
  1176. /*
  1177. * Some MIPS32 and MIPS64 processors have physically indexed caches.
  1178. * This code supports virtually indexed processors and will be
  1179. * unnecessarily inefficient on physically indexed processors.
  1180. */
  1181. if (c->dcache.linesz)
  1182. shm_align_mask = max_t( unsigned long,
  1183. c->dcache.sets * c->dcache.linesz - 1,
  1184. PAGE_SIZE - 1);
  1185. else
  1186. shm_align_mask = PAGE_SIZE-1;
  1187. __flush_cache_vmap = r4k__flush_cache_vmap;
  1188. __flush_cache_vunmap = r4k__flush_cache_vunmap;
  1189. flush_cache_all = cache_noop;
  1190. __flush_cache_all = r4k___flush_cache_all;
  1191. flush_cache_mm = r4k_flush_cache_mm;
  1192. flush_cache_page = r4k_flush_cache_page;
  1193. flush_cache_range = r4k_flush_cache_range;
  1194. flush_cache_sigtramp = r4k_flush_cache_sigtramp;
  1195. flush_icache_all = r4k_flush_icache_all;
  1196. local_flush_data_cache_page = local_r4k_flush_data_cache_page;
  1197. flush_data_cache_page = r4k_flush_data_cache_page;
  1198. flush_icache_range = r4k_flush_icache_range;
  1199. local_flush_icache_range = local_r4k_flush_icache_range;
  1200. #if defined(CONFIG_DMA_NONCOHERENT)
  1201. if (coherentio) {
  1202. _dma_cache_wback_inv = (void *)cache_noop;
  1203. _dma_cache_wback = (void *)cache_noop;
  1204. _dma_cache_inv = (void *)cache_noop;
  1205. } else {
  1206. _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
  1207. _dma_cache_wback = r4k_dma_cache_wback_inv;
  1208. _dma_cache_inv = r4k_dma_cache_inv;
  1209. }
  1210. #endif
  1211. build_clear_page();
  1212. build_copy_page();
  1213. #if !defined(CONFIG_MIPS_CMP)
  1214. local_r4k___flush_cache_all(NULL);
  1215. #endif
  1216. coherency_setup();
  1217. }