c-r4k.c 37 KB

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