c-r4k.c 46 KB

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