c-r4k.c 38 KB

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