c-r4k.c 38 KB

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