c-r4k.c 37 KB

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