c-r4k.c 52 KB

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