c-r4k.c 38 KB

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