c-r4k.c 38 KB

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