c-r4k.c 38 KB

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