c-r4k.c 36 KB

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