c-r4k.c 38 KB

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