c-r4k.c 42 KB

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