c-r4k.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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 mips3\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 char *way_string[] = { NULL, "direct mapped", "2-way",
  795. "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
  796. };
  797. static void probe_pcache(void)
  798. {
  799. struct cpuinfo_mips *c = &current_cpu_data;
  800. unsigned int config = read_c0_config();
  801. unsigned int prid = read_c0_prid();
  802. unsigned long config1;
  803. unsigned int lsize;
  804. switch (current_cpu_type()) {
  805. case CPU_R4600: /* QED style two way caches? */
  806. case CPU_R4700:
  807. case CPU_R5000:
  808. case CPU_NEVADA:
  809. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  810. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  811. c->icache.ways = 2;
  812. c->icache.waybit = __ffs(icache_size/2);
  813. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  814. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  815. c->dcache.ways = 2;
  816. c->dcache.waybit= __ffs(dcache_size/2);
  817. c->options |= MIPS_CPU_CACHE_CDEX_P;
  818. break;
  819. case CPU_R5432:
  820. case CPU_R5500:
  821. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  822. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  823. c->icache.ways = 2;
  824. c->icache.waybit= 0;
  825. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  826. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  827. c->dcache.ways = 2;
  828. c->dcache.waybit = 0;
  829. c->options |= MIPS_CPU_CACHE_CDEX_P | MIPS_CPU_PREFETCH;
  830. break;
  831. case CPU_TX49XX:
  832. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  833. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  834. c->icache.ways = 4;
  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 = 4;
  839. c->dcache.waybit = 0;
  840. c->options |= MIPS_CPU_CACHE_CDEX_P;
  841. c->options |= MIPS_CPU_PREFETCH;
  842. break;
  843. case CPU_R4000PC:
  844. case CPU_R4000SC:
  845. case CPU_R4000MC:
  846. case CPU_R4400PC:
  847. case CPU_R4400SC:
  848. case CPU_R4400MC:
  849. case CPU_R4300:
  850. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  851. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  852. c->icache.ways = 1;
  853. c->icache.waybit = 0; /* doesn't matter */
  854. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  855. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  856. c->dcache.ways = 1;
  857. c->dcache.waybit = 0; /* does not matter */
  858. c->options |= MIPS_CPU_CACHE_CDEX_P;
  859. break;
  860. case CPU_R10000:
  861. case CPU_R12000:
  862. case CPU_R14000:
  863. icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
  864. c->icache.linesz = 64;
  865. c->icache.ways = 2;
  866. c->icache.waybit = 0;
  867. dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
  868. c->dcache.linesz = 32;
  869. c->dcache.ways = 2;
  870. c->dcache.waybit = 0;
  871. c->options |= MIPS_CPU_PREFETCH;
  872. break;
  873. case CPU_VR4133:
  874. write_c0_config(config & ~VR41_CONF_P4K);
  875. case CPU_VR4131:
  876. /* Workaround for cache instruction bug of VR4131 */
  877. if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
  878. c->processor_id == 0x0c82U) {
  879. config |= 0x00400000U;
  880. if (c->processor_id == 0x0c80U)
  881. config |= VR41_CONF_BP;
  882. write_c0_config(config);
  883. } else
  884. c->options |= MIPS_CPU_CACHE_CDEX_P;
  885. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  886. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  887. c->icache.ways = 2;
  888. c->icache.waybit = __ffs(icache_size/2);
  889. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  890. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  891. c->dcache.ways = 2;
  892. c->dcache.waybit = __ffs(dcache_size/2);
  893. break;
  894. case CPU_VR41XX:
  895. case CPU_VR4111:
  896. case CPU_VR4121:
  897. case CPU_VR4122:
  898. case CPU_VR4181:
  899. case CPU_VR4181A:
  900. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  901. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  902. c->icache.ways = 1;
  903. c->icache.waybit = 0; /* doesn't matter */
  904. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  905. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  906. c->dcache.ways = 1;
  907. c->dcache.waybit = 0; /* does not matter */
  908. c->options |= MIPS_CPU_CACHE_CDEX_P;
  909. break;
  910. case CPU_RM7000:
  911. rm7k_erratum31();
  912. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  913. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  914. c->icache.ways = 4;
  915. c->icache.waybit = __ffs(icache_size / c->icache.ways);
  916. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  917. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  918. c->dcache.ways = 4;
  919. c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
  920. c->options |= MIPS_CPU_CACHE_CDEX_P;
  921. c->options |= MIPS_CPU_PREFETCH;
  922. break;
  923. case CPU_LOONGSON2:
  924. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  925. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  926. if (prid & 0x3)
  927. c->icache.ways = 4;
  928. else
  929. c->icache.ways = 2;
  930. c->icache.waybit = 0;
  931. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  932. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  933. if (prid & 0x3)
  934. c->dcache.ways = 4;
  935. else
  936. c->dcache.ways = 2;
  937. c->dcache.waybit = 0;
  938. break;
  939. case CPU_LOONGSON3:
  940. config1 = read_c0_config1();
  941. lsize = (config1 >> 19) & 7;
  942. if (lsize)
  943. c->icache.linesz = 2 << lsize;
  944. else
  945. c->icache.linesz = 0;
  946. c->icache.sets = 64 << ((config1 >> 22) & 7);
  947. c->icache.ways = 1 + ((config1 >> 16) & 7);
  948. icache_size = c->icache.sets *
  949. c->icache.ways *
  950. c->icache.linesz;
  951. c->icache.waybit = 0;
  952. lsize = (config1 >> 10) & 7;
  953. if (lsize)
  954. c->dcache.linesz = 2 << lsize;
  955. else
  956. c->dcache.linesz = 0;
  957. c->dcache.sets = 64 << ((config1 >> 13) & 7);
  958. c->dcache.ways = 1 + ((config1 >> 7) & 7);
  959. dcache_size = c->dcache.sets *
  960. c->dcache.ways *
  961. c->dcache.linesz;
  962. c->dcache.waybit = 0;
  963. break;
  964. case CPU_CAVIUM_OCTEON3:
  965. /* For now lie about the number of ways. */
  966. c->icache.linesz = 128;
  967. c->icache.sets = 16;
  968. c->icache.ways = 8;
  969. c->icache.flags |= MIPS_CACHE_VTAG;
  970. icache_size = c->icache.sets * c->icache.ways * c->icache.linesz;
  971. c->dcache.linesz = 128;
  972. c->dcache.ways = 8;
  973. c->dcache.sets = 8;
  974. dcache_size = c->dcache.sets * c->dcache.ways * c->dcache.linesz;
  975. c->options |= MIPS_CPU_PREFETCH;
  976. break;
  977. default:
  978. if (!(config & MIPS_CONF_M))
  979. panic("Don't know how to probe P-caches on this cpu.");
  980. /*
  981. * So we seem to be a MIPS32 or MIPS64 CPU
  982. * So let's probe the I-cache ...
  983. */
  984. config1 = read_c0_config1();
  985. lsize = (config1 >> 19) & 7;
  986. /* IL == 7 is reserved */
  987. if (lsize == 7)
  988. panic("Invalid icache line size");
  989. c->icache.linesz = lsize ? 2 << lsize : 0;
  990. c->icache.sets = 32 << (((config1 >> 22) + 1) & 7);
  991. c->icache.ways = 1 + ((config1 >> 16) & 7);
  992. icache_size = c->icache.sets *
  993. c->icache.ways *
  994. c->icache.linesz;
  995. c->icache.waybit = __ffs(icache_size/c->icache.ways);
  996. if (config & 0x8) /* VI bit */
  997. c->icache.flags |= MIPS_CACHE_VTAG;
  998. /*
  999. * Now probe the MIPS32 / MIPS64 data cache.
  1000. */
  1001. c->dcache.flags = 0;
  1002. lsize = (config1 >> 10) & 7;
  1003. /* DL == 7 is reserved */
  1004. if (lsize == 7)
  1005. panic("Invalid dcache line size");
  1006. c->dcache.linesz = lsize ? 2 << lsize : 0;
  1007. c->dcache.sets = 32 << (((config1 >> 13) + 1) & 7);
  1008. c->dcache.ways = 1 + ((config1 >> 7) & 7);
  1009. dcache_size = c->dcache.sets *
  1010. c->dcache.ways *
  1011. c->dcache.linesz;
  1012. c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
  1013. c->options |= MIPS_CPU_PREFETCH;
  1014. break;
  1015. }
  1016. /*
  1017. * Processor configuration sanity check for the R4000SC erratum
  1018. * #5. With page sizes larger than 32kB there is no possibility
  1019. * to get a VCE exception anymore so we don't care about this
  1020. * misconfiguration. The case is rather theoretical anyway;
  1021. * presumably no vendor is shipping his hardware in the "bad"
  1022. * configuration.
  1023. */
  1024. if ((prid & PRID_IMP_MASK) == PRID_IMP_R4000 &&
  1025. (prid & PRID_REV_MASK) < PRID_REV_R4400 &&
  1026. !(config & CONF_SC) && c->icache.linesz != 16 &&
  1027. PAGE_SIZE <= 0x8000)
  1028. panic("Improper R4000SC processor configuration detected");
  1029. /* compute a couple of other cache variables */
  1030. c->icache.waysize = icache_size / c->icache.ways;
  1031. c->dcache.waysize = dcache_size / c->dcache.ways;
  1032. c->icache.sets = c->icache.linesz ?
  1033. icache_size / (c->icache.linesz * c->icache.ways) : 0;
  1034. c->dcache.sets = c->dcache.linesz ?
  1035. dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
  1036. /*
  1037. * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
  1038. * 2-way virtually indexed so normally would suffer from aliases. So
  1039. * normally they'd suffer from aliases but magic in the hardware deals
  1040. * with that for us so we don't need to take care ourselves.
  1041. */
  1042. switch (current_cpu_type()) {
  1043. case CPU_20KC:
  1044. case CPU_25KF:
  1045. case CPU_SB1:
  1046. case CPU_SB1A:
  1047. case CPU_XLR:
  1048. c->dcache.flags |= MIPS_CACHE_PINDEX;
  1049. break;
  1050. case CPU_R10000:
  1051. case CPU_R12000:
  1052. case CPU_R14000:
  1053. break;
  1054. case CPU_M14KC:
  1055. case CPU_M14KEC:
  1056. case CPU_24K:
  1057. case CPU_34K:
  1058. case CPU_74K:
  1059. case CPU_1004K:
  1060. case CPU_1074K:
  1061. case CPU_INTERAPTIV:
  1062. case CPU_P5600:
  1063. case CPU_PROAPTIV:
  1064. case CPU_M5150:
  1065. if ((c->cputype == CPU_74K) || (c->cputype == CPU_1074K))
  1066. alias_74k_erratum(c);
  1067. if (!(read_c0_config7() & MIPS_CONF7_IAR) &&
  1068. (c->icache.waysize > PAGE_SIZE))
  1069. c->icache.flags |= MIPS_CACHE_ALIASES;
  1070. if (read_c0_config7() & MIPS_CONF7_AR) {
  1071. /*
  1072. * Effectively physically indexed dcache,
  1073. * thus no virtual aliases.
  1074. */
  1075. c->dcache.flags |= MIPS_CACHE_PINDEX;
  1076. break;
  1077. }
  1078. default:
  1079. if (c->dcache.waysize > PAGE_SIZE)
  1080. c->dcache.flags |= MIPS_CACHE_ALIASES;
  1081. }
  1082. switch (current_cpu_type()) {
  1083. case CPU_20KC:
  1084. /*
  1085. * Some older 20Kc chips doesn't have the 'VI' bit in
  1086. * the config register.
  1087. */
  1088. c->icache.flags |= MIPS_CACHE_VTAG;
  1089. break;
  1090. case CPU_ALCHEMY:
  1091. c->icache.flags |= MIPS_CACHE_IC_F_DC;
  1092. break;
  1093. case CPU_LOONGSON2:
  1094. /*
  1095. * LOONGSON2 has 4 way icache, but when using indexed cache op,
  1096. * one op will act on all 4 ways
  1097. */
  1098. c->icache.ways = 1;
  1099. }
  1100. printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
  1101. icache_size >> 10,
  1102. c->icache.flags & MIPS_CACHE_VTAG ? "VIVT" : "VIPT",
  1103. way_string[c->icache.ways], c->icache.linesz);
  1104. printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
  1105. dcache_size >> 10, way_string[c->dcache.ways],
  1106. (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
  1107. (c->dcache.flags & MIPS_CACHE_ALIASES) ?
  1108. "cache aliases" : "no aliases",
  1109. c->dcache.linesz);
  1110. }
  1111. /*
  1112. * If you even _breathe_ on this function, look at the gcc output and make sure
  1113. * it does not pop things on and off the stack for the cache sizing loop that
  1114. * executes in KSEG1 space or else you will crash and burn badly. You have
  1115. * been warned.
  1116. */
  1117. static int probe_scache(void)
  1118. {
  1119. unsigned long flags, addr, begin, end, pow2;
  1120. unsigned int config = read_c0_config();
  1121. struct cpuinfo_mips *c = &current_cpu_data;
  1122. if (config & CONF_SC)
  1123. return 0;
  1124. begin = (unsigned long) &_stext;
  1125. begin &= ~((4 * 1024 * 1024) - 1);
  1126. end = begin + (4 * 1024 * 1024);
  1127. /*
  1128. * This is such a bitch, you'd think they would make it easy to do
  1129. * this. Away you daemons of stupidity!
  1130. */
  1131. local_irq_save(flags);
  1132. /* Fill each size-multiple cache line with a valid tag. */
  1133. pow2 = (64 * 1024);
  1134. for (addr = begin; addr < end; addr = (begin + pow2)) {
  1135. unsigned long *p = (unsigned long *) addr;
  1136. __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
  1137. pow2 <<= 1;
  1138. }
  1139. /* Load first line with zero (therefore invalid) tag. */
  1140. write_c0_taglo(0);
  1141. write_c0_taghi(0);
  1142. __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
  1143. cache_op(Index_Store_Tag_I, begin);
  1144. cache_op(Index_Store_Tag_D, begin);
  1145. cache_op(Index_Store_Tag_SD, begin);
  1146. /* Now search for the wrap around point. */
  1147. pow2 = (128 * 1024);
  1148. for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
  1149. cache_op(Index_Load_Tag_SD, addr);
  1150. __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
  1151. if (!read_c0_taglo())
  1152. break;
  1153. pow2 <<= 1;
  1154. }
  1155. local_irq_restore(flags);
  1156. addr -= begin;
  1157. scache_size = addr;
  1158. c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
  1159. c->scache.ways = 1;
  1160. c->dcache.waybit = 0; /* does not matter */
  1161. return 1;
  1162. }
  1163. static void __init loongson2_sc_init(void)
  1164. {
  1165. struct cpuinfo_mips *c = &current_cpu_data;
  1166. scache_size = 512*1024;
  1167. c->scache.linesz = 32;
  1168. c->scache.ways = 4;
  1169. c->scache.waybit = 0;
  1170. c->scache.waysize = scache_size / (c->scache.ways);
  1171. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  1172. pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  1173. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  1174. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1175. }
  1176. static void __init loongson3_sc_init(void)
  1177. {
  1178. struct cpuinfo_mips *c = &current_cpu_data;
  1179. unsigned int config2, lsize;
  1180. config2 = read_c0_config2();
  1181. lsize = (config2 >> 4) & 15;
  1182. if (lsize)
  1183. c->scache.linesz = 2 << lsize;
  1184. else
  1185. c->scache.linesz = 0;
  1186. c->scache.sets = 64 << ((config2 >> 8) & 15);
  1187. c->scache.ways = 1 + (config2 & 15);
  1188. scache_size = c->scache.sets *
  1189. c->scache.ways *
  1190. c->scache.linesz;
  1191. /* Loongson-3 has 4 cores, 1MB scache for each. scaches are shared */
  1192. scache_size *= 4;
  1193. c->scache.waybit = 0;
  1194. pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  1195. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  1196. if (scache_size)
  1197. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1198. return;
  1199. }
  1200. extern int r5k_sc_init(void);
  1201. extern int rm7k_sc_init(void);
  1202. extern int mips_sc_init(void);
  1203. static void setup_scache(void)
  1204. {
  1205. struct cpuinfo_mips *c = &current_cpu_data;
  1206. unsigned int config = read_c0_config();
  1207. int sc_present = 0;
  1208. /*
  1209. * Do the probing thing on R4000SC and R4400SC processors. Other
  1210. * processors don't have a S-cache that would be relevant to the
  1211. * Linux memory management.
  1212. */
  1213. switch (current_cpu_type()) {
  1214. case CPU_R4000SC:
  1215. case CPU_R4000MC:
  1216. case CPU_R4400SC:
  1217. case CPU_R4400MC:
  1218. sc_present = run_uncached(probe_scache);
  1219. if (sc_present)
  1220. c->options |= MIPS_CPU_CACHE_CDEX_S;
  1221. break;
  1222. case CPU_R10000:
  1223. case CPU_R12000:
  1224. case CPU_R14000:
  1225. scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
  1226. c->scache.linesz = 64 << ((config >> 13) & 1);
  1227. c->scache.ways = 2;
  1228. c->scache.waybit= 0;
  1229. sc_present = 1;
  1230. break;
  1231. case CPU_R5000:
  1232. case CPU_NEVADA:
  1233. #ifdef CONFIG_R5000_CPU_SCACHE
  1234. r5k_sc_init();
  1235. #endif
  1236. return;
  1237. case CPU_RM7000:
  1238. #ifdef CONFIG_RM7000_CPU_SCACHE
  1239. rm7k_sc_init();
  1240. #endif
  1241. return;
  1242. case CPU_LOONGSON2:
  1243. loongson2_sc_init();
  1244. return;
  1245. case CPU_LOONGSON3:
  1246. loongson3_sc_init();
  1247. return;
  1248. case CPU_CAVIUM_OCTEON3:
  1249. case CPU_XLP:
  1250. /* don't need to worry about L2, fully coherent */
  1251. return;
  1252. default:
  1253. if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M32R2 |
  1254. MIPS_CPU_ISA_M64R1 | MIPS_CPU_ISA_M64R2)) {
  1255. #ifdef CONFIG_MIPS_CPU_SCACHE
  1256. if (mips_sc_init ()) {
  1257. scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
  1258. printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
  1259. scache_size >> 10,
  1260. way_string[c->scache.ways], c->scache.linesz);
  1261. }
  1262. #else
  1263. if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
  1264. panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
  1265. #endif
  1266. return;
  1267. }
  1268. sc_present = 0;
  1269. }
  1270. if (!sc_present)
  1271. return;
  1272. /* compute a couple of other cache variables */
  1273. c->scache.waysize = scache_size / c->scache.ways;
  1274. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  1275. printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  1276. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  1277. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1278. }
  1279. void au1x00_fixup_config_od(void)
  1280. {
  1281. /*
  1282. * c0_config.od (bit 19) was write only (and read as 0)
  1283. * on the early revisions of Alchemy SOCs. It disables the bus
  1284. * transaction overlapping and needs to be set to fix various errata.
  1285. */
  1286. switch (read_c0_prid()) {
  1287. case 0x00030100: /* Au1000 DA */
  1288. case 0x00030201: /* Au1000 HA */
  1289. case 0x00030202: /* Au1000 HB */
  1290. case 0x01030200: /* Au1500 AB */
  1291. /*
  1292. * Au1100 errata actually keeps silence about this bit, so we set it
  1293. * just in case for those revisions that require it to be set according
  1294. * to the (now gone) cpu table.
  1295. */
  1296. case 0x02030200: /* Au1100 AB */
  1297. case 0x02030201: /* Au1100 BA */
  1298. case 0x02030202: /* Au1100 BC */
  1299. set_c0_config(1 << 19);
  1300. break;
  1301. }
  1302. }
  1303. /* CP0 hazard avoidance. */
  1304. #define NXP_BARRIER() \
  1305. __asm__ __volatile__( \
  1306. ".set noreorder\n\t" \
  1307. "nop; nop; nop; nop; nop; nop;\n\t" \
  1308. ".set reorder\n\t")
  1309. static void nxp_pr4450_fixup_config(void)
  1310. {
  1311. unsigned long config0;
  1312. config0 = read_c0_config();
  1313. /* clear all three cache coherency fields */
  1314. config0 &= ~(0x7 | (7 << 25) | (7 << 28));
  1315. config0 |= (((_page_cachable_default >> _CACHE_SHIFT) << 0) |
  1316. ((_page_cachable_default >> _CACHE_SHIFT) << 25) |
  1317. ((_page_cachable_default >> _CACHE_SHIFT) << 28));
  1318. write_c0_config(config0);
  1319. NXP_BARRIER();
  1320. }
  1321. static int cca = -1;
  1322. static int __init cca_setup(char *str)
  1323. {
  1324. get_option(&str, &cca);
  1325. return 0;
  1326. }
  1327. early_param("cca", cca_setup);
  1328. static void coherency_setup(void)
  1329. {
  1330. if (cca < 0 || cca > 7)
  1331. cca = read_c0_config() & CONF_CM_CMASK;
  1332. _page_cachable_default = cca << _CACHE_SHIFT;
  1333. pr_debug("Using cache attribute %d\n", cca);
  1334. change_c0_config(CONF_CM_CMASK, cca);
  1335. /*
  1336. * c0_status.cu=0 specifies that updates by the sc instruction use
  1337. * the coherency mode specified by the TLB; 1 means cachable
  1338. * coherent update on write will be used. Not all processors have
  1339. * this bit and; some wire it to zero, others like Toshiba had the
  1340. * silly idea of putting something else there ...
  1341. */
  1342. switch (current_cpu_type()) {
  1343. case CPU_R4000PC:
  1344. case CPU_R4000SC:
  1345. case CPU_R4000MC:
  1346. case CPU_R4400PC:
  1347. case CPU_R4400SC:
  1348. case CPU_R4400MC:
  1349. clear_c0_config(CONF_CU);
  1350. break;
  1351. /*
  1352. * We need to catch the early Alchemy SOCs with
  1353. * the write-only co_config.od bit and set it back to one on:
  1354. * Au1000 rev DA, HA, HB; Au1100 AB, BA, BC, Au1500 AB
  1355. */
  1356. case CPU_ALCHEMY:
  1357. au1x00_fixup_config_od();
  1358. break;
  1359. case PRID_IMP_PR4450:
  1360. nxp_pr4450_fixup_config();
  1361. break;
  1362. }
  1363. }
  1364. static void r4k_cache_error_setup(void)
  1365. {
  1366. extern char __weak except_vec2_generic;
  1367. extern char __weak except_vec2_sb1;
  1368. switch (current_cpu_type()) {
  1369. case CPU_SB1:
  1370. case CPU_SB1A:
  1371. set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
  1372. break;
  1373. default:
  1374. set_uncached_handler(0x100, &except_vec2_generic, 0x80);
  1375. break;
  1376. }
  1377. }
  1378. void r4k_cache_init(void)
  1379. {
  1380. extern void build_clear_page(void);
  1381. extern void build_copy_page(void);
  1382. struct cpuinfo_mips *c = &current_cpu_data;
  1383. probe_pcache();
  1384. setup_scache();
  1385. r4k_blast_dcache_page_setup();
  1386. r4k_blast_dcache_page_indexed_setup();
  1387. r4k_blast_dcache_setup();
  1388. r4k_blast_icache_page_setup();
  1389. r4k_blast_icache_page_indexed_setup();
  1390. r4k_blast_icache_setup();
  1391. r4k_blast_scache_page_setup();
  1392. r4k_blast_scache_page_indexed_setup();
  1393. r4k_blast_scache_setup();
  1394. #ifdef CONFIG_EVA
  1395. r4k_blast_dcache_user_page_setup();
  1396. r4k_blast_icache_user_page_setup();
  1397. #endif
  1398. /*
  1399. * Some MIPS32 and MIPS64 processors have physically indexed caches.
  1400. * This code supports virtually indexed processors and will be
  1401. * unnecessarily inefficient on physically indexed processors.
  1402. */
  1403. if (c->dcache.linesz)
  1404. shm_align_mask = max_t( unsigned long,
  1405. c->dcache.sets * c->dcache.linesz - 1,
  1406. PAGE_SIZE - 1);
  1407. else
  1408. shm_align_mask = PAGE_SIZE-1;
  1409. __flush_cache_vmap = r4k__flush_cache_vmap;
  1410. __flush_cache_vunmap = r4k__flush_cache_vunmap;
  1411. flush_cache_all = cache_noop;
  1412. __flush_cache_all = r4k___flush_cache_all;
  1413. flush_cache_mm = r4k_flush_cache_mm;
  1414. flush_cache_page = r4k_flush_cache_page;
  1415. flush_cache_range = r4k_flush_cache_range;
  1416. __flush_kernel_vmap_range = r4k_flush_kernel_vmap_range;
  1417. flush_cache_sigtramp = r4k_flush_cache_sigtramp;
  1418. flush_icache_all = r4k_flush_icache_all;
  1419. local_flush_data_cache_page = local_r4k_flush_data_cache_page;
  1420. flush_data_cache_page = r4k_flush_data_cache_page;
  1421. flush_icache_range = r4k_flush_icache_range;
  1422. local_flush_icache_range = local_r4k_flush_icache_range;
  1423. #if defined(CONFIG_DMA_NONCOHERENT) || defined(CONFIG_DMA_MAYBE_COHERENT)
  1424. if (coherentio) {
  1425. _dma_cache_wback_inv = (void *)cache_noop;
  1426. _dma_cache_wback = (void *)cache_noop;
  1427. _dma_cache_inv = (void *)cache_noop;
  1428. } else {
  1429. _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
  1430. _dma_cache_wback = r4k_dma_cache_wback_inv;
  1431. _dma_cache_inv = r4k_dma_cache_inv;
  1432. }
  1433. #endif
  1434. build_clear_page();
  1435. build_copy_page();
  1436. /*
  1437. * We want to run CMP kernels on core with and without coherent
  1438. * caches. Therefore, do not use CONFIG_MIPS_CMP to decide whether
  1439. * or not to flush caches.
  1440. */
  1441. local_r4k___flush_cache_all(NULL);
  1442. coherency_setup();
  1443. board_cache_error_setup = r4k_cache_error_setup;
  1444. }
  1445. static int r4k_cache_pm_notifier(struct notifier_block *self, unsigned long cmd,
  1446. void *v)
  1447. {
  1448. switch (cmd) {
  1449. case CPU_PM_ENTER_FAILED:
  1450. case CPU_PM_EXIT:
  1451. coherency_setup();
  1452. break;
  1453. }
  1454. return NOTIFY_OK;
  1455. }
  1456. static struct notifier_block r4k_cache_pm_notifier_block = {
  1457. .notifier_call = r4k_cache_pm_notifier,
  1458. };
  1459. int __init r4k_cache_init_pm(void)
  1460. {
  1461. return cpu_pm_register_notifier(&r4k_cache_pm_notifier_block);
  1462. }
  1463. arch_initcall(r4k_cache_init_pm);