c-r4k.c 45 KB

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