c-r4k.c 38 KB

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