c-r4k.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  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/export.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-cps.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 void (*r4k_blast_scache_node)(long node);
  401. static void r4k_blast_scache_node_setup(void)
  402. {
  403. unsigned long sc_lsize = cpu_scache_line_size();
  404. if (current_cpu_type() != CPU_LOONGSON3)
  405. r4k_blast_scache_node = (void *)cache_noop;
  406. else if (sc_lsize == 16)
  407. r4k_blast_scache_node = blast_scache16_node;
  408. else if (sc_lsize == 32)
  409. r4k_blast_scache_node = blast_scache32_node;
  410. else if (sc_lsize == 64)
  411. r4k_blast_scache_node = blast_scache64_node;
  412. else if (sc_lsize == 128)
  413. r4k_blast_scache_node = blast_scache128_node;
  414. }
  415. static inline void local_r4k___flush_cache_all(void * args)
  416. {
  417. switch (current_cpu_type()) {
  418. case CPU_LOONGSON2:
  419. case CPU_R4000SC:
  420. case CPU_R4000MC:
  421. case CPU_R4400SC:
  422. case CPU_R4400MC:
  423. case CPU_R10000:
  424. case CPU_R12000:
  425. case CPU_R14000:
  426. case CPU_R16000:
  427. /*
  428. * These caches are inclusive caches, that is, if something
  429. * is not cached in the S-cache, we know it also won't be
  430. * in one of the primary caches.
  431. */
  432. r4k_blast_scache();
  433. break;
  434. case CPU_LOONGSON3:
  435. /* Use get_ebase_cpunum() for both NUMA=y/n */
  436. r4k_blast_scache_node(get_ebase_cpunum() >> 2);
  437. break;
  438. case CPU_BMIPS5000:
  439. r4k_blast_scache();
  440. __sync();
  441. break;
  442. default:
  443. r4k_blast_dcache();
  444. r4k_blast_icache();
  445. break;
  446. }
  447. }
  448. static void r4k___flush_cache_all(void)
  449. {
  450. r4k_on_each_cpu(R4K_INDEX, local_r4k___flush_cache_all, NULL);
  451. }
  452. /**
  453. * has_valid_asid() - Determine if an mm already has an ASID.
  454. * @mm: Memory map.
  455. * @type: R4K_HIT or R4K_INDEX, type of cache op.
  456. *
  457. * Determines whether @mm already has an ASID on any of the CPUs which cache ops
  458. * of type @type within an r4k_on_each_cpu() call will affect. If
  459. * r4k_on_each_cpu() does an SMP call to a single VPE in each core, then the
  460. * scope of the operation is confined to sibling CPUs, otherwise all online CPUs
  461. * will need to be checked.
  462. *
  463. * Must be called in non-preemptive context.
  464. *
  465. * Returns: 1 if the CPUs affected by @type cache ops have an ASID for @mm.
  466. * 0 otherwise.
  467. */
  468. static inline int has_valid_asid(const struct mm_struct *mm, unsigned int type)
  469. {
  470. unsigned int i;
  471. const cpumask_t *mask = cpu_present_mask;
  472. /* cpu_sibling_map[] undeclared when !CONFIG_SMP */
  473. #ifdef CONFIG_SMP
  474. /*
  475. * If r4k_on_each_cpu does SMP calls, it does them to a single VPE in
  476. * each foreign core, so we only need to worry about siblings.
  477. * Otherwise we need to worry about all present CPUs.
  478. */
  479. if (r4k_op_needs_ipi(type))
  480. mask = &cpu_sibling_map[smp_processor_id()];
  481. #endif
  482. for_each_cpu(i, mask)
  483. if (cpu_context(i, mm))
  484. return 1;
  485. return 0;
  486. }
  487. static void r4k__flush_cache_vmap(void)
  488. {
  489. r4k_blast_dcache();
  490. }
  491. static void r4k__flush_cache_vunmap(void)
  492. {
  493. r4k_blast_dcache();
  494. }
  495. /*
  496. * Note: flush_tlb_range() assumes flush_cache_range() sufficiently flushes
  497. * whole caches when vma is executable.
  498. */
  499. static inline void local_r4k_flush_cache_range(void * args)
  500. {
  501. struct vm_area_struct *vma = args;
  502. int exec = vma->vm_flags & VM_EXEC;
  503. if (!has_valid_asid(vma->vm_mm, R4K_INDEX))
  504. return;
  505. /*
  506. * If dcache can alias, we must blast it since mapping is changing.
  507. * If executable, we must ensure any dirty lines are written back far
  508. * enough to be visible to icache.
  509. */
  510. if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
  511. r4k_blast_dcache();
  512. /* If executable, blast stale lines from icache */
  513. if (exec)
  514. r4k_blast_icache();
  515. }
  516. static void r4k_flush_cache_range(struct vm_area_struct *vma,
  517. unsigned long start, unsigned long end)
  518. {
  519. int exec = vma->vm_flags & VM_EXEC;
  520. if (cpu_has_dc_aliases || exec)
  521. r4k_on_each_cpu(R4K_INDEX, local_r4k_flush_cache_range, vma);
  522. }
  523. static inline void local_r4k_flush_cache_mm(void * args)
  524. {
  525. struct mm_struct *mm = args;
  526. if (!has_valid_asid(mm, R4K_INDEX))
  527. return;
  528. /*
  529. * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
  530. * only flush the primary caches but R1x000 behave sane ...
  531. * R4000SC and R4400SC indexed S-cache ops also invalidate primary
  532. * caches, so we can bail out early.
  533. */
  534. if (current_cpu_type() == CPU_R4000SC ||
  535. current_cpu_type() == CPU_R4000MC ||
  536. current_cpu_type() == CPU_R4400SC ||
  537. current_cpu_type() == CPU_R4400MC) {
  538. r4k_blast_scache();
  539. return;
  540. }
  541. r4k_blast_dcache();
  542. }
  543. static void r4k_flush_cache_mm(struct mm_struct *mm)
  544. {
  545. if (!cpu_has_dc_aliases)
  546. return;
  547. r4k_on_each_cpu(R4K_INDEX, local_r4k_flush_cache_mm, mm);
  548. }
  549. struct flush_cache_page_args {
  550. struct vm_area_struct *vma;
  551. unsigned long addr;
  552. unsigned long pfn;
  553. };
  554. static inline void local_r4k_flush_cache_page(void *args)
  555. {
  556. struct flush_cache_page_args *fcp_args = args;
  557. struct vm_area_struct *vma = fcp_args->vma;
  558. unsigned long addr = fcp_args->addr;
  559. struct page *page = pfn_to_page(fcp_args->pfn);
  560. int exec = vma->vm_flags & VM_EXEC;
  561. struct mm_struct *mm = vma->vm_mm;
  562. int map_coherent = 0;
  563. pgd_t *pgdp;
  564. pud_t *pudp;
  565. pmd_t *pmdp;
  566. pte_t *ptep;
  567. void *vaddr;
  568. /*
  569. * If owns no valid ASID yet, cannot possibly have gotten
  570. * this page into the cache.
  571. */
  572. if (!has_valid_asid(mm, R4K_HIT))
  573. return;
  574. addr &= PAGE_MASK;
  575. pgdp = pgd_offset(mm, addr);
  576. pudp = pud_offset(pgdp, addr);
  577. pmdp = pmd_offset(pudp, addr);
  578. ptep = pte_offset(pmdp, addr);
  579. /*
  580. * If the page isn't marked valid, the page cannot possibly be
  581. * in the cache.
  582. */
  583. if (!(pte_present(*ptep)))
  584. return;
  585. if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
  586. vaddr = NULL;
  587. else {
  588. /*
  589. * Use kmap_coherent or kmap_atomic to do flushes for
  590. * another ASID than the current one.
  591. */
  592. map_coherent = (cpu_has_dc_aliases &&
  593. page_mapcount(page) &&
  594. !Page_dcache_dirty(page));
  595. if (map_coherent)
  596. vaddr = kmap_coherent(page, addr);
  597. else
  598. vaddr = kmap_atomic(page);
  599. addr = (unsigned long)vaddr;
  600. }
  601. if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
  602. vaddr ? r4k_blast_dcache_page(addr) :
  603. r4k_blast_dcache_user_page(addr);
  604. if (exec && !cpu_icache_snoops_remote_store)
  605. r4k_blast_scache_page(addr);
  606. }
  607. if (exec) {
  608. if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
  609. int cpu = smp_processor_id();
  610. if (cpu_context(cpu, mm) != 0)
  611. drop_mmu_context(mm, cpu);
  612. } else
  613. vaddr ? r4k_blast_icache_page(addr) :
  614. r4k_blast_icache_user_page(addr);
  615. }
  616. if (vaddr) {
  617. if (map_coherent)
  618. kunmap_coherent();
  619. else
  620. kunmap_atomic(vaddr);
  621. }
  622. }
  623. static void r4k_flush_cache_page(struct vm_area_struct *vma,
  624. unsigned long addr, unsigned long pfn)
  625. {
  626. struct flush_cache_page_args args;
  627. args.vma = vma;
  628. args.addr = addr;
  629. args.pfn = pfn;
  630. r4k_on_each_cpu(R4K_HIT, local_r4k_flush_cache_page, &args);
  631. }
  632. static inline void local_r4k_flush_data_cache_page(void * addr)
  633. {
  634. r4k_blast_dcache_page((unsigned long) addr);
  635. }
  636. static void r4k_flush_data_cache_page(unsigned long addr)
  637. {
  638. if (in_atomic())
  639. local_r4k_flush_data_cache_page((void *)addr);
  640. else
  641. r4k_on_each_cpu(R4K_HIT, local_r4k_flush_data_cache_page,
  642. (void *) addr);
  643. }
  644. struct flush_icache_range_args {
  645. unsigned long start;
  646. unsigned long end;
  647. unsigned int type;
  648. bool user;
  649. };
  650. static inline void __local_r4k_flush_icache_range(unsigned long start,
  651. unsigned long end,
  652. unsigned int type,
  653. bool user)
  654. {
  655. if (!cpu_has_ic_fills_f_dc) {
  656. if (type == R4K_INDEX ||
  657. (type & R4K_INDEX && end - start >= dcache_size)) {
  658. r4k_blast_dcache();
  659. } else {
  660. R4600_HIT_CACHEOP_WAR_IMPL;
  661. if (user)
  662. protected_blast_dcache_range(start, end);
  663. else
  664. blast_dcache_range(start, end);
  665. }
  666. }
  667. if (type == R4K_INDEX ||
  668. (type & R4K_INDEX && end - start > icache_size))
  669. r4k_blast_icache();
  670. else {
  671. switch (boot_cpu_type()) {
  672. case CPU_LOONGSON2:
  673. protected_loongson2_blast_icache_range(start, end);
  674. break;
  675. default:
  676. if (user)
  677. protected_blast_icache_range(start, end);
  678. else
  679. blast_icache_range(start, end);
  680. break;
  681. }
  682. }
  683. }
  684. static inline void local_r4k_flush_icache_range(unsigned long start,
  685. unsigned long end)
  686. {
  687. __local_r4k_flush_icache_range(start, end, R4K_HIT | R4K_INDEX, false);
  688. }
  689. static inline void local_r4k_flush_icache_user_range(unsigned long start,
  690. unsigned long end)
  691. {
  692. __local_r4k_flush_icache_range(start, end, R4K_HIT | R4K_INDEX, true);
  693. }
  694. static inline void local_r4k_flush_icache_range_ipi(void *args)
  695. {
  696. struct flush_icache_range_args *fir_args = args;
  697. unsigned long start = fir_args->start;
  698. unsigned long end = fir_args->end;
  699. unsigned int type = fir_args->type;
  700. bool user = fir_args->user;
  701. __local_r4k_flush_icache_range(start, end, type, user);
  702. }
  703. static void __r4k_flush_icache_range(unsigned long start, unsigned long end,
  704. bool user)
  705. {
  706. struct flush_icache_range_args args;
  707. unsigned long size, cache_size;
  708. args.start = start;
  709. args.end = end;
  710. args.type = R4K_HIT | R4K_INDEX;
  711. args.user = user;
  712. /*
  713. * Indexed cache ops require an SMP call.
  714. * Consider if that can or should be avoided.
  715. */
  716. preempt_disable();
  717. if (r4k_op_needs_ipi(R4K_INDEX) && !r4k_op_needs_ipi(R4K_HIT)) {
  718. /*
  719. * If address-based cache ops don't require an SMP call, then
  720. * use them exclusively for small flushes.
  721. */
  722. size = end - start;
  723. cache_size = icache_size;
  724. if (!cpu_has_ic_fills_f_dc) {
  725. size *= 2;
  726. cache_size += dcache_size;
  727. }
  728. if (size <= cache_size)
  729. args.type &= ~R4K_INDEX;
  730. }
  731. r4k_on_each_cpu(args.type, local_r4k_flush_icache_range_ipi, &args);
  732. preempt_enable();
  733. instruction_hazard();
  734. }
  735. static void r4k_flush_icache_range(unsigned long start, unsigned long end)
  736. {
  737. return __r4k_flush_icache_range(start, end, false);
  738. }
  739. static void r4k_flush_icache_user_range(unsigned long start, unsigned long end)
  740. {
  741. return __r4k_flush_icache_range(start, end, true);
  742. }
  743. #ifdef CONFIG_DMA_NONCOHERENT
  744. static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
  745. {
  746. /* Catch bad driver code */
  747. if (WARN_ON(size == 0))
  748. return;
  749. preempt_disable();
  750. if (cpu_has_inclusive_pcaches) {
  751. if (size >= scache_size) {
  752. if (current_cpu_type() != CPU_LOONGSON3)
  753. r4k_blast_scache();
  754. else
  755. r4k_blast_scache_node(pa_to_nid(addr));
  756. } else {
  757. blast_scache_range(addr, addr + size);
  758. }
  759. preempt_enable();
  760. __sync();
  761. return;
  762. }
  763. /*
  764. * Either no secondary cache or the available caches don't have the
  765. * subset property so we have to flush the primary caches
  766. * explicitly.
  767. * If we would need IPI to perform an INDEX-type operation, then
  768. * we have to use the HIT-type alternative as IPI cannot be used
  769. * here due to interrupts possibly being disabled.
  770. */
  771. if (!r4k_op_needs_ipi(R4K_INDEX) && size >= dcache_size) {
  772. r4k_blast_dcache();
  773. } else {
  774. R4600_HIT_CACHEOP_WAR_IMPL;
  775. blast_dcache_range(addr, addr + size);
  776. }
  777. preempt_enable();
  778. bc_wback_inv(addr, size);
  779. __sync();
  780. }
  781. static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
  782. {
  783. /* Catch bad driver code */
  784. if (WARN_ON(size == 0))
  785. return;
  786. preempt_disable();
  787. if (cpu_has_inclusive_pcaches) {
  788. if (size >= scache_size) {
  789. if (current_cpu_type() != CPU_LOONGSON3)
  790. r4k_blast_scache();
  791. else
  792. r4k_blast_scache_node(pa_to_nid(addr));
  793. } else {
  794. /*
  795. * There is no clearly documented alignment requirement
  796. * for the cache instruction on MIPS processors and
  797. * some processors, among them the RM5200 and RM7000
  798. * QED processors will throw an address error for cache
  799. * hit ops with insufficient alignment. Solved by
  800. * aligning the address to cache line size.
  801. */
  802. blast_inv_scache_range(addr, addr + size);
  803. }
  804. preempt_enable();
  805. __sync();
  806. return;
  807. }
  808. if (!r4k_op_needs_ipi(R4K_INDEX) && size >= dcache_size) {
  809. r4k_blast_dcache();
  810. } else {
  811. R4600_HIT_CACHEOP_WAR_IMPL;
  812. blast_inv_dcache_range(addr, addr + size);
  813. }
  814. preempt_enable();
  815. bc_inv(addr, size);
  816. __sync();
  817. }
  818. #endif /* CONFIG_DMA_NONCOHERENT */
  819. struct flush_cache_sigtramp_args {
  820. struct mm_struct *mm;
  821. struct page *page;
  822. unsigned long addr;
  823. };
  824. /*
  825. * While we're protected against bad userland addresses we don't care
  826. * very much about what happens in that case. Usually a segmentation
  827. * fault will dump the process later on anyway ...
  828. */
  829. static void local_r4k_flush_cache_sigtramp(void *args)
  830. {
  831. struct flush_cache_sigtramp_args *fcs_args = args;
  832. unsigned long addr = fcs_args->addr;
  833. struct page *page = fcs_args->page;
  834. struct mm_struct *mm = fcs_args->mm;
  835. int map_coherent = 0;
  836. void *vaddr;
  837. unsigned long ic_lsize = cpu_icache_line_size();
  838. unsigned long dc_lsize = cpu_dcache_line_size();
  839. unsigned long sc_lsize = cpu_scache_line_size();
  840. /*
  841. * If owns no valid ASID yet, cannot possibly have gotten
  842. * this page into the cache.
  843. */
  844. if (!has_valid_asid(mm, R4K_HIT))
  845. return;
  846. if (mm == current->active_mm) {
  847. vaddr = NULL;
  848. } else {
  849. /*
  850. * Use kmap_coherent or kmap_atomic to do flushes for
  851. * another ASID than the current one.
  852. */
  853. map_coherent = (cpu_has_dc_aliases &&
  854. page_mapcount(page) &&
  855. !Page_dcache_dirty(page));
  856. if (map_coherent)
  857. vaddr = kmap_coherent(page, addr);
  858. else
  859. vaddr = kmap_atomic(page);
  860. addr = (unsigned long)vaddr + (addr & ~PAGE_MASK);
  861. }
  862. R4600_HIT_CACHEOP_WAR_IMPL;
  863. if (!cpu_has_ic_fills_f_dc) {
  864. if (dc_lsize)
  865. vaddr ? flush_dcache_line(addr & ~(dc_lsize - 1))
  866. : protected_writeback_dcache_line(
  867. addr & ~(dc_lsize - 1));
  868. if (!cpu_icache_snoops_remote_store && scache_size)
  869. vaddr ? flush_scache_line(addr & ~(sc_lsize - 1))
  870. : protected_writeback_scache_line(
  871. addr & ~(sc_lsize - 1));
  872. }
  873. if (ic_lsize)
  874. vaddr ? flush_icache_line(addr & ~(ic_lsize - 1))
  875. : protected_flush_icache_line(addr & ~(ic_lsize - 1));
  876. if (vaddr) {
  877. if (map_coherent)
  878. kunmap_coherent();
  879. else
  880. kunmap_atomic(vaddr);
  881. }
  882. if (MIPS4K_ICACHE_REFILL_WAR) {
  883. __asm__ __volatile__ (
  884. ".set push\n\t"
  885. ".set noat\n\t"
  886. ".set "MIPS_ISA_LEVEL"\n\t"
  887. #ifdef CONFIG_32BIT
  888. "la $at,1f\n\t"
  889. #endif
  890. #ifdef CONFIG_64BIT
  891. "dla $at,1f\n\t"
  892. #endif
  893. "cache %0,($at)\n\t"
  894. "nop; nop; nop\n"
  895. "1:\n\t"
  896. ".set pop"
  897. :
  898. : "i" (Hit_Invalidate_I));
  899. }
  900. if (MIPS_CACHE_SYNC_WAR)
  901. __asm__ __volatile__ ("sync");
  902. }
  903. static void r4k_flush_cache_sigtramp(unsigned long addr)
  904. {
  905. struct flush_cache_sigtramp_args args;
  906. int npages;
  907. down_read(&current->mm->mmap_sem);
  908. npages = get_user_pages_fast(addr, 1, 0, &args.page);
  909. if (npages < 1)
  910. goto out;
  911. args.mm = current->mm;
  912. args.addr = addr;
  913. r4k_on_each_cpu(R4K_HIT, local_r4k_flush_cache_sigtramp, &args);
  914. put_page(args.page);
  915. out:
  916. up_read(&current->mm->mmap_sem);
  917. }
  918. static void r4k_flush_icache_all(void)
  919. {
  920. if (cpu_has_vtag_icache)
  921. r4k_blast_icache();
  922. }
  923. struct flush_kernel_vmap_range_args {
  924. unsigned long vaddr;
  925. int size;
  926. };
  927. static inline void local_r4k_flush_kernel_vmap_range_index(void *args)
  928. {
  929. /*
  930. * Aliases only affect the primary caches so don't bother with
  931. * S-caches or T-caches.
  932. */
  933. r4k_blast_dcache();
  934. }
  935. static inline void local_r4k_flush_kernel_vmap_range(void *args)
  936. {
  937. struct flush_kernel_vmap_range_args *vmra = args;
  938. unsigned long vaddr = vmra->vaddr;
  939. int size = vmra->size;
  940. /*
  941. * Aliases only affect the primary caches so don't bother with
  942. * S-caches or T-caches.
  943. */
  944. R4600_HIT_CACHEOP_WAR_IMPL;
  945. blast_dcache_range(vaddr, vaddr + size);
  946. }
  947. static void r4k_flush_kernel_vmap_range(unsigned long vaddr, int size)
  948. {
  949. struct flush_kernel_vmap_range_args args;
  950. args.vaddr = (unsigned long) vaddr;
  951. args.size = size;
  952. if (size >= dcache_size)
  953. r4k_on_each_cpu(R4K_INDEX,
  954. local_r4k_flush_kernel_vmap_range_index, NULL);
  955. else
  956. r4k_on_each_cpu(R4K_HIT, local_r4k_flush_kernel_vmap_range,
  957. &args);
  958. }
  959. static inline void rm7k_erratum31(void)
  960. {
  961. const unsigned long ic_lsize = 32;
  962. unsigned long addr;
  963. /* RM7000 erratum #31. The icache is screwed at startup. */
  964. write_c0_taglo(0);
  965. write_c0_taghi(0);
  966. for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
  967. __asm__ __volatile__ (
  968. ".set push\n\t"
  969. ".set noreorder\n\t"
  970. ".set mips3\n\t"
  971. "cache\t%1, 0(%0)\n\t"
  972. "cache\t%1, 0x1000(%0)\n\t"
  973. "cache\t%1, 0x2000(%0)\n\t"
  974. "cache\t%1, 0x3000(%0)\n\t"
  975. "cache\t%2, 0(%0)\n\t"
  976. "cache\t%2, 0x1000(%0)\n\t"
  977. "cache\t%2, 0x2000(%0)\n\t"
  978. "cache\t%2, 0x3000(%0)\n\t"
  979. "cache\t%1, 0(%0)\n\t"
  980. "cache\t%1, 0x1000(%0)\n\t"
  981. "cache\t%1, 0x2000(%0)\n\t"
  982. "cache\t%1, 0x3000(%0)\n\t"
  983. ".set pop\n"
  984. :
  985. : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
  986. }
  987. }
  988. static inline int alias_74k_erratum(struct cpuinfo_mips *c)
  989. {
  990. unsigned int imp = c->processor_id & PRID_IMP_MASK;
  991. unsigned int rev = c->processor_id & PRID_REV_MASK;
  992. int present = 0;
  993. /*
  994. * Early versions of the 74K do not update the cache tags on a
  995. * vtag miss/ptag hit which can occur in the case of KSEG0/KUSEG
  996. * aliases. In this case it is better to treat the cache as always
  997. * having aliases. Also disable the synonym tag update feature
  998. * where available. In this case no opportunistic tag update will
  999. * happen where a load causes a virtual address miss but a physical
  1000. * address hit during a D-cache look-up.
  1001. */
  1002. switch (imp) {
  1003. case PRID_IMP_74K:
  1004. if (rev <= PRID_REV_ENCODE_332(2, 4, 0))
  1005. present = 1;
  1006. if (rev == PRID_REV_ENCODE_332(2, 4, 0))
  1007. write_c0_config6(read_c0_config6() | MIPS_CONF6_SYND);
  1008. break;
  1009. case PRID_IMP_1074K:
  1010. if (rev <= PRID_REV_ENCODE_332(1, 1, 0)) {
  1011. present = 1;
  1012. write_c0_config6(read_c0_config6() | MIPS_CONF6_SYND);
  1013. }
  1014. break;
  1015. default:
  1016. BUG();
  1017. }
  1018. return present;
  1019. }
  1020. static void b5k_instruction_hazard(void)
  1021. {
  1022. __sync();
  1023. __sync();
  1024. __asm__ __volatile__(
  1025. " nop; nop; nop; nop; nop; nop; nop; nop\n"
  1026. " nop; nop; nop; nop; nop; nop; nop; nop\n"
  1027. " nop; nop; nop; nop; nop; nop; nop; nop\n"
  1028. " nop; nop; nop; nop; nop; nop; nop; nop\n"
  1029. : : : "memory");
  1030. }
  1031. static char *way_string[] = { NULL, "direct mapped", "2-way",
  1032. "3-way", "4-way", "5-way", "6-way", "7-way", "8-way",
  1033. "9-way", "10-way", "11-way", "12-way",
  1034. "13-way", "14-way", "15-way", "16-way",
  1035. };
  1036. static void probe_pcache(void)
  1037. {
  1038. struct cpuinfo_mips *c = &current_cpu_data;
  1039. unsigned int config = read_c0_config();
  1040. unsigned int prid = read_c0_prid();
  1041. int has_74k_erratum = 0;
  1042. unsigned long config1;
  1043. unsigned int lsize;
  1044. switch (current_cpu_type()) {
  1045. case CPU_R4600: /* QED style two way caches? */
  1046. case CPU_R4700:
  1047. case CPU_R5000:
  1048. case CPU_NEVADA:
  1049. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  1050. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  1051. c->icache.ways = 2;
  1052. c->icache.waybit = __ffs(icache_size/2);
  1053. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  1054. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  1055. c->dcache.ways = 2;
  1056. c->dcache.waybit= __ffs(dcache_size/2);
  1057. c->options |= MIPS_CPU_CACHE_CDEX_P;
  1058. break;
  1059. case CPU_R5432:
  1060. case CPU_R5500:
  1061. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  1062. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  1063. c->icache.ways = 2;
  1064. c->icache.waybit= 0;
  1065. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  1066. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  1067. c->dcache.ways = 2;
  1068. c->dcache.waybit = 0;
  1069. c->options |= MIPS_CPU_CACHE_CDEX_P | MIPS_CPU_PREFETCH;
  1070. break;
  1071. case CPU_TX49XX:
  1072. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  1073. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  1074. c->icache.ways = 4;
  1075. c->icache.waybit= 0;
  1076. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  1077. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  1078. c->dcache.ways = 4;
  1079. c->dcache.waybit = 0;
  1080. c->options |= MIPS_CPU_CACHE_CDEX_P;
  1081. c->options |= MIPS_CPU_PREFETCH;
  1082. break;
  1083. case CPU_R4000PC:
  1084. case CPU_R4000SC:
  1085. case CPU_R4000MC:
  1086. case CPU_R4400PC:
  1087. case CPU_R4400SC:
  1088. case CPU_R4400MC:
  1089. case CPU_R4300:
  1090. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  1091. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  1092. c->icache.ways = 1;
  1093. c->icache.waybit = 0; /* doesn't matter */
  1094. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  1095. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  1096. c->dcache.ways = 1;
  1097. c->dcache.waybit = 0; /* does not matter */
  1098. c->options |= MIPS_CPU_CACHE_CDEX_P;
  1099. break;
  1100. case CPU_R10000:
  1101. case CPU_R12000:
  1102. case CPU_R14000:
  1103. case CPU_R16000:
  1104. icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
  1105. c->icache.linesz = 64;
  1106. c->icache.ways = 2;
  1107. c->icache.waybit = 0;
  1108. dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
  1109. c->dcache.linesz = 32;
  1110. c->dcache.ways = 2;
  1111. c->dcache.waybit = 0;
  1112. c->options |= MIPS_CPU_PREFETCH;
  1113. break;
  1114. case CPU_VR4133:
  1115. write_c0_config(config & ~VR41_CONF_P4K);
  1116. case CPU_VR4131:
  1117. /* Workaround for cache instruction bug of VR4131 */
  1118. if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
  1119. c->processor_id == 0x0c82U) {
  1120. config |= 0x00400000U;
  1121. if (c->processor_id == 0x0c80U)
  1122. config |= VR41_CONF_BP;
  1123. write_c0_config(config);
  1124. } else
  1125. c->options |= MIPS_CPU_CACHE_CDEX_P;
  1126. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  1127. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  1128. c->icache.ways = 2;
  1129. c->icache.waybit = __ffs(icache_size/2);
  1130. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  1131. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  1132. c->dcache.ways = 2;
  1133. c->dcache.waybit = __ffs(dcache_size/2);
  1134. break;
  1135. case CPU_VR41XX:
  1136. case CPU_VR4111:
  1137. case CPU_VR4121:
  1138. case CPU_VR4122:
  1139. case CPU_VR4181:
  1140. case CPU_VR4181A:
  1141. icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
  1142. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  1143. c->icache.ways = 1;
  1144. c->icache.waybit = 0; /* doesn't matter */
  1145. dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
  1146. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  1147. c->dcache.ways = 1;
  1148. c->dcache.waybit = 0; /* does not matter */
  1149. c->options |= MIPS_CPU_CACHE_CDEX_P;
  1150. break;
  1151. case CPU_RM7000:
  1152. rm7k_erratum31();
  1153. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  1154. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  1155. c->icache.ways = 4;
  1156. c->icache.waybit = __ffs(icache_size / c->icache.ways);
  1157. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  1158. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  1159. c->dcache.ways = 4;
  1160. c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
  1161. c->options |= MIPS_CPU_CACHE_CDEX_P;
  1162. c->options |= MIPS_CPU_PREFETCH;
  1163. break;
  1164. case CPU_LOONGSON2:
  1165. icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
  1166. c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
  1167. if (prid & 0x3)
  1168. c->icache.ways = 4;
  1169. else
  1170. c->icache.ways = 2;
  1171. c->icache.waybit = 0;
  1172. dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
  1173. c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
  1174. if (prid & 0x3)
  1175. c->dcache.ways = 4;
  1176. else
  1177. c->dcache.ways = 2;
  1178. c->dcache.waybit = 0;
  1179. break;
  1180. case CPU_LOONGSON3:
  1181. config1 = read_c0_config1();
  1182. lsize = (config1 >> 19) & 7;
  1183. if (lsize)
  1184. c->icache.linesz = 2 << lsize;
  1185. else
  1186. c->icache.linesz = 0;
  1187. c->icache.sets = 64 << ((config1 >> 22) & 7);
  1188. c->icache.ways = 1 + ((config1 >> 16) & 7);
  1189. icache_size = c->icache.sets *
  1190. c->icache.ways *
  1191. c->icache.linesz;
  1192. c->icache.waybit = 0;
  1193. lsize = (config1 >> 10) & 7;
  1194. if (lsize)
  1195. c->dcache.linesz = 2 << lsize;
  1196. else
  1197. c->dcache.linesz = 0;
  1198. c->dcache.sets = 64 << ((config1 >> 13) & 7);
  1199. c->dcache.ways = 1 + ((config1 >> 7) & 7);
  1200. dcache_size = c->dcache.sets *
  1201. c->dcache.ways *
  1202. c->dcache.linesz;
  1203. c->dcache.waybit = 0;
  1204. if ((prid & PRID_REV_MASK) >= PRID_REV_LOONGSON3A_R2)
  1205. c->options |= MIPS_CPU_PREFETCH;
  1206. break;
  1207. case CPU_CAVIUM_OCTEON3:
  1208. /* For now lie about the number of ways. */
  1209. c->icache.linesz = 128;
  1210. c->icache.sets = 16;
  1211. c->icache.ways = 8;
  1212. c->icache.flags |= MIPS_CACHE_VTAG;
  1213. icache_size = c->icache.sets * c->icache.ways * c->icache.linesz;
  1214. c->dcache.linesz = 128;
  1215. c->dcache.ways = 8;
  1216. c->dcache.sets = 8;
  1217. dcache_size = c->dcache.sets * c->dcache.ways * c->dcache.linesz;
  1218. c->options |= MIPS_CPU_PREFETCH;
  1219. break;
  1220. default:
  1221. if (!(config & MIPS_CONF_M))
  1222. panic("Don't know how to probe P-caches on this cpu.");
  1223. /*
  1224. * So we seem to be a MIPS32 or MIPS64 CPU
  1225. * So let's probe the I-cache ...
  1226. */
  1227. config1 = read_c0_config1();
  1228. lsize = (config1 >> 19) & 7;
  1229. /* IL == 7 is reserved */
  1230. if (lsize == 7)
  1231. panic("Invalid icache line size");
  1232. c->icache.linesz = lsize ? 2 << lsize : 0;
  1233. c->icache.sets = 32 << (((config1 >> 22) + 1) & 7);
  1234. c->icache.ways = 1 + ((config1 >> 16) & 7);
  1235. icache_size = c->icache.sets *
  1236. c->icache.ways *
  1237. c->icache.linesz;
  1238. c->icache.waybit = __ffs(icache_size/c->icache.ways);
  1239. if (config & MIPS_CONF_VI)
  1240. c->icache.flags |= MIPS_CACHE_VTAG;
  1241. /*
  1242. * Now probe the MIPS32 / MIPS64 data cache.
  1243. */
  1244. c->dcache.flags = 0;
  1245. lsize = (config1 >> 10) & 7;
  1246. /* DL == 7 is reserved */
  1247. if (lsize == 7)
  1248. panic("Invalid dcache line size");
  1249. c->dcache.linesz = lsize ? 2 << lsize : 0;
  1250. c->dcache.sets = 32 << (((config1 >> 13) + 1) & 7);
  1251. c->dcache.ways = 1 + ((config1 >> 7) & 7);
  1252. dcache_size = c->dcache.sets *
  1253. c->dcache.ways *
  1254. c->dcache.linesz;
  1255. c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
  1256. c->options |= MIPS_CPU_PREFETCH;
  1257. break;
  1258. }
  1259. /*
  1260. * Processor configuration sanity check for the R4000SC erratum
  1261. * #5. With page sizes larger than 32kB there is no possibility
  1262. * to get a VCE exception anymore so we don't care about this
  1263. * misconfiguration. The case is rather theoretical anyway;
  1264. * presumably no vendor is shipping his hardware in the "bad"
  1265. * configuration.
  1266. */
  1267. if ((prid & PRID_IMP_MASK) == PRID_IMP_R4000 &&
  1268. (prid & PRID_REV_MASK) < PRID_REV_R4400 &&
  1269. !(config & CONF_SC) && c->icache.linesz != 16 &&
  1270. PAGE_SIZE <= 0x8000)
  1271. panic("Improper R4000SC processor configuration detected");
  1272. /* compute a couple of other cache variables */
  1273. c->icache.waysize = icache_size / c->icache.ways;
  1274. c->dcache.waysize = dcache_size / c->dcache.ways;
  1275. c->icache.sets = c->icache.linesz ?
  1276. icache_size / (c->icache.linesz * c->icache.ways) : 0;
  1277. c->dcache.sets = c->dcache.linesz ?
  1278. dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
  1279. /*
  1280. * R1x000 P-caches are odd in a positive way. They're 32kB 2-way
  1281. * virtually indexed so normally would suffer from aliases. So
  1282. * normally they'd suffer from aliases but magic in the hardware deals
  1283. * with that for us so we don't need to take care ourselves.
  1284. */
  1285. switch (current_cpu_type()) {
  1286. case CPU_20KC:
  1287. case CPU_25KF:
  1288. case CPU_I6400:
  1289. case CPU_I6500:
  1290. case CPU_SB1:
  1291. case CPU_SB1A:
  1292. case CPU_XLR:
  1293. c->dcache.flags |= MIPS_CACHE_PINDEX;
  1294. break;
  1295. case CPU_R10000:
  1296. case CPU_R12000:
  1297. case CPU_R14000:
  1298. case CPU_R16000:
  1299. break;
  1300. case CPU_74K:
  1301. case CPU_1074K:
  1302. has_74k_erratum = alias_74k_erratum(c);
  1303. /* Fall through. */
  1304. case CPU_M14KC:
  1305. case CPU_M14KEC:
  1306. case CPU_24K:
  1307. case CPU_34K:
  1308. case CPU_1004K:
  1309. case CPU_INTERAPTIV:
  1310. case CPU_P5600:
  1311. case CPU_PROAPTIV:
  1312. case CPU_M5150:
  1313. case CPU_QEMU_GENERIC:
  1314. case CPU_P6600:
  1315. case CPU_M6250:
  1316. if (!(read_c0_config7() & MIPS_CONF7_IAR) &&
  1317. (c->icache.waysize > PAGE_SIZE))
  1318. c->icache.flags |= MIPS_CACHE_ALIASES;
  1319. if (!has_74k_erratum && (read_c0_config7() & MIPS_CONF7_AR)) {
  1320. /*
  1321. * Effectively physically indexed dcache,
  1322. * thus no virtual aliases.
  1323. */
  1324. c->dcache.flags |= MIPS_CACHE_PINDEX;
  1325. break;
  1326. }
  1327. default:
  1328. if (has_74k_erratum || c->dcache.waysize > PAGE_SIZE)
  1329. c->dcache.flags |= MIPS_CACHE_ALIASES;
  1330. }
  1331. /* Physically indexed caches don't suffer from virtual aliasing */
  1332. if (c->dcache.flags & MIPS_CACHE_PINDEX)
  1333. c->dcache.flags &= ~MIPS_CACHE_ALIASES;
  1334. /*
  1335. * In systems with CM the icache fills from L2 or closer caches, and
  1336. * thus sees remote stores without needing to write them back any
  1337. * further than that.
  1338. */
  1339. if (mips_cm_present())
  1340. c->icache.flags |= MIPS_IC_SNOOPS_REMOTE;
  1341. switch (current_cpu_type()) {
  1342. case CPU_20KC:
  1343. /*
  1344. * Some older 20Kc chips doesn't have the 'VI' bit in
  1345. * the config register.
  1346. */
  1347. c->icache.flags |= MIPS_CACHE_VTAG;
  1348. break;
  1349. case CPU_ALCHEMY:
  1350. case CPU_I6400:
  1351. case CPU_I6500:
  1352. c->icache.flags |= MIPS_CACHE_IC_F_DC;
  1353. break;
  1354. case CPU_BMIPS5000:
  1355. c->icache.flags |= MIPS_CACHE_IC_F_DC;
  1356. /* Cache aliases are handled in hardware; allow HIGHMEM */
  1357. c->dcache.flags &= ~MIPS_CACHE_ALIASES;
  1358. break;
  1359. case CPU_LOONGSON2:
  1360. /*
  1361. * LOONGSON2 has 4 way icache, but when using indexed cache op,
  1362. * one op will act on all 4 ways
  1363. */
  1364. c->icache.ways = 1;
  1365. }
  1366. printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
  1367. icache_size >> 10,
  1368. c->icache.flags & MIPS_CACHE_VTAG ? "VIVT" : "VIPT",
  1369. way_string[c->icache.ways], c->icache.linesz);
  1370. printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
  1371. dcache_size >> 10, way_string[c->dcache.ways],
  1372. (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
  1373. (c->dcache.flags & MIPS_CACHE_ALIASES) ?
  1374. "cache aliases" : "no aliases",
  1375. c->dcache.linesz);
  1376. }
  1377. static void probe_vcache(void)
  1378. {
  1379. struct cpuinfo_mips *c = &current_cpu_data;
  1380. unsigned int config2, lsize;
  1381. if (current_cpu_type() != CPU_LOONGSON3)
  1382. return;
  1383. config2 = read_c0_config2();
  1384. if ((lsize = ((config2 >> 20) & 15)))
  1385. c->vcache.linesz = 2 << lsize;
  1386. else
  1387. c->vcache.linesz = lsize;
  1388. c->vcache.sets = 64 << ((config2 >> 24) & 15);
  1389. c->vcache.ways = 1 + ((config2 >> 16) & 15);
  1390. vcache_size = c->vcache.sets * c->vcache.ways * c->vcache.linesz;
  1391. c->vcache.waybit = 0;
  1392. c->vcache.waysize = vcache_size / c->vcache.ways;
  1393. pr_info("Unified victim cache %ldkB %s, linesize %d bytes.\n",
  1394. vcache_size >> 10, way_string[c->vcache.ways], c->vcache.linesz);
  1395. }
  1396. /*
  1397. * If you even _breathe_ on this function, look at the gcc output and make sure
  1398. * it does not pop things on and off the stack for the cache sizing loop that
  1399. * executes in KSEG1 space or else you will crash and burn badly. You have
  1400. * been warned.
  1401. */
  1402. static int probe_scache(void)
  1403. {
  1404. unsigned long flags, addr, begin, end, pow2;
  1405. unsigned int config = read_c0_config();
  1406. struct cpuinfo_mips *c = &current_cpu_data;
  1407. if (config & CONF_SC)
  1408. return 0;
  1409. begin = (unsigned long) &_stext;
  1410. begin &= ~((4 * 1024 * 1024) - 1);
  1411. end = begin + (4 * 1024 * 1024);
  1412. /*
  1413. * This is such a bitch, you'd think they would make it easy to do
  1414. * this. Away you daemons of stupidity!
  1415. */
  1416. local_irq_save(flags);
  1417. /* Fill each size-multiple cache line with a valid tag. */
  1418. pow2 = (64 * 1024);
  1419. for (addr = begin; addr < end; addr = (begin + pow2)) {
  1420. unsigned long *p = (unsigned long *) addr;
  1421. __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
  1422. pow2 <<= 1;
  1423. }
  1424. /* Load first line with zero (therefore invalid) tag. */
  1425. write_c0_taglo(0);
  1426. write_c0_taghi(0);
  1427. __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
  1428. cache_op(Index_Store_Tag_I, begin);
  1429. cache_op(Index_Store_Tag_D, begin);
  1430. cache_op(Index_Store_Tag_SD, begin);
  1431. /* Now search for the wrap around point. */
  1432. pow2 = (128 * 1024);
  1433. for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
  1434. cache_op(Index_Load_Tag_SD, addr);
  1435. __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
  1436. if (!read_c0_taglo())
  1437. break;
  1438. pow2 <<= 1;
  1439. }
  1440. local_irq_restore(flags);
  1441. addr -= begin;
  1442. scache_size = addr;
  1443. c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
  1444. c->scache.ways = 1;
  1445. c->scache.waybit = 0; /* does not matter */
  1446. return 1;
  1447. }
  1448. static void loongson2_sc_init(void)
  1449. {
  1450. struct cpuinfo_mips *c = &current_cpu_data;
  1451. scache_size = 512*1024;
  1452. c->scache.linesz = 32;
  1453. c->scache.ways = 4;
  1454. c->scache.waybit = 0;
  1455. c->scache.waysize = scache_size / (c->scache.ways);
  1456. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  1457. pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  1458. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  1459. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1460. }
  1461. static void __init loongson3_sc_init(void)
  1462. {
  1463. struct cpuinfo_mips *c = &current_cpu_data;
  1464. unsigned int config2, lsize;
  1465. config2 = read_c0_config2();
  1466. lsize = (config2 >> 4) & 15;
  1467. if (lsize)
  1468. c->scache.linesz = 2 << lsize;
  1469. else
  1470. c->scache.linesz = 0;
  1471. c->scache.sets = 64 << ((config2 >> 8) & 15);
  1472. c->scache.ways = 1 + (config2 & 15);
  1473. scache_size = c->scache.sets *
  1474. c->scache.ways *
  1475. c->scache.linesz;
  1476. /* Loongson-3 has 4 cores, 1MB scache for each. scaches are shared */
  1477. scache_size *= 4;
  1478. c->scache.waybit = 0;
  1479. c->scache.waysize = scache_size / c->scache.ways;
  1480. pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  1481. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  1482. if (scache_size)
  1483. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1484. return;
  1485. }
  1486. extern int r5k_sc_init(void);
  1487. extern int rm7k_sc_init(void);
  1488. extern int mips_sc_init(void);
  1489. static void setup_scache(void)
  1490. {
  1491. struct cpuinfo_mips *c = &current_cpu_data;
  1492. unsigned int config = read_c0_config();
  1493. int sc_present = 0;
  1494. /*
  1495. * Do the probing thing on R4000SC and R4400SC processors. Other
  1496. * processors don't have a S-cache that would be relevant to the
  1497. * Linux memory management.
  1498. */
  1499. switch (current_cpu_type()) {
  1500. case CPU_R4000SC:
  1501. case CPU_R4000MC:
  1502. case CPU_R4400SC:
  1503. case CPU_R4400MC:
  1504. sc_present = run_uncached(probe_scache);
  1505. if (sc_present)
  1506. c->options |= MIPS_CPU_CACHE_CDEX_S;
  1507. break;
  1508. case CPU_R10000:
  1509. case CPU_R12000:
  1510. case CPU_R14000:
  1511. case CPU_R16000:
  1512. scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
  1513. c->scache.linesz = 64 << ((config >> 13) & 1);
  1514. c->scache.ways = 2;
  1515. c->scache.waybit= 0;
  1516. sc_present = 1;
  1517. break;
  1518. case CPU_R5000:
  1519. case CPU_NEVADA:
  1520. #ifdef CONFIG_R5000_CPU_SCACHE
  1521. r5k_sc_init();
  1522. #endif
  1523. return;
  1524. case CPU_RM7000:
  1525. #ifdef CONFIG_RM7000_CPU_SCACHE
  1526. rm7k_sc_init();
  1527. #endif
  1528. return;
  1529. case CPU_LOONGSON2:
  1530. loongson2_sc_init();
  1531. return;
  1532. case CPU_LOONGSON3:
  1533. loongson3_sc_init();
  1534. return;
  1535. case CPU_CAVIUM_OCTEON3:
  1536. case CPU_XLP:
  1537. /* don't need to worry about L2, fully coherent */
  1538. return;
  1539. default:
  1540. if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M32R2 |
  1541. MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R1 |
  1542. MIPS_CPU_ISA_M64R2 | MIPS_CPU_ISA_M64R6)) {
  1543. #ifdef CONFIG_MIPS_CPU_SCACHE
  1544. if (mips_sc_init ()) {
  1545. scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
  1546. printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
  1547. scache_size >> 10,
  1548. way_string[c->scache.ways], c->scache.linesz);
  1549. if (current_cpu_type() == CPU_BMIPS5000)
  1550. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1551. }
  1552. #else
  1553. if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
  1554. panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
  1555. #endif
  1556. return;
  1557. }
  1558. sc_present = 0;
  1559. }
  1560. if (!sc_present)
  1561. return;
  1562. /* compute a couple of other cache variables */
  1563. c->scache.waysize = scache_size / c->scache.ways;
  1564. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  1565. printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
  1566. scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
  1567. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  1568. }
  1569. void au1x00_fixup_config_od(void)
  1570. {
  1571. /*
  1572. * c0_config.od (bit 19) was write only (and read as 0)
  1573. * on the early revisions of Alchemy SOCs. It disables the bus
  1574. * transaction overlapping and needs to be set to fix various errata.
  1575. */
  1576. switch (read_c0_prid()) {
  1577. case 0x00030100: /* Au1000 DA */
  1578. case 0x00030201: /* Au1000 HA */
  1579. case 0x00030202: /* Au1000 HB */
  1580. case 0x01030200: /* Au1500 AB */
  1581. /*
  1582. * Au1100 errata actually keeps silence about this bit, so we set it
  1583. * just in case for those revisions that require it to be set according
  1584. * to the (now gone) cpu table.
  1585. */
  1586. case 0x02030200: /* Au1100 AB */
  1587. case 0x02030201: /* Au1100 BA */
  1588. case 0x02030202: /* Au1100 BC */
  1589. set_c0_config(1 << 19);
  1590. break;
  1591. }
  1592. }
  1593. /* CP0 hazard avoidance. */
  1594. #define NXP_BARRIER() \
  1595. __asm__ __volatile__( \
  1596. ".set noreorder\n\t" \
  1597. "nop; nop; nop; nop; nop; nop;\n\t" \
  1598. ".set reorder\n\t")
  1599. static void nxp_pr4450_fixup_config(void)
  1600. {
  1601. unsigned long config0;
  1602. config0 = read_c0_config();
  1603. /* clear all three cache coherency fields */
  1604. config0 &= ~(0x7 | (7 << 25) | (7 << 28));
  1605. config0 |= (((_page_cachable_default >> _CACHE_SHIFT) << 0) |
  1606. ((_page_cachable_default >> _CACHE_SHIFT) << 25) |
  1607. ((_page_cachable_default >> _CACHE_SHIFT) << 28));
  1608. write_c0_config(config0);
  1609. NXP_BARRIER();
  1610. }
  1611. static int cca = -1;
  1612. static int __init cca_setup(char *str)
  1613. {
  1614. get_option(&str, &cca);
  1615. return 0;
  1616. }
  1617. early_param("cca", cca_setup);
  1618. static void coherency_setup(void)
  1619. {
  1620. if (cca < 0 || cca > 7)
  1621. cca = read_c0_config() & CONF_CM_CMASK;
  1622. _page_cachable_default = cca << _CACHE_SHIFT;
  1623. pr_debug("Using cache attribute %d\n", cca);
  1624. change_c0_config(CONF_CM_CMASK, cca);
  1625. /*
  1626. * c0_status.cu=0 specifies that updates by the sc instruction use
  1627. * the coherency mode specified by the TLB; 1 means cachable
  1628. * coherent update on write will be used. Not all processors have
  1629. * this bit and; some wire it to zero, others like Toshiba had the
  1630. * silly idea of putting something else there ...
  1631. */
  1632. switch (current_cpu_type()) {
  1633. case CPU_R4000PC:
  1634. case CPU_R4000SC:
  1635. case CPU_R4000MC:
  1636. case CPU_R4400PC:
  1637. case CPU_R4400SC:
  1638. case CPU_R4400MC:
  1639. clear_c0_config(CONF_CU);
  1640. break;
  1641. /*
  1642. * We need to catch the early Alchemy SOCs with
  1643. * the write-only co_config.od bit and set it back to one on:
  1644. * Au1000 rev DA, HA, HB; Au1100 AB, BA, BC, Au1500 AB
  1645. */
  1646. case CPU_ALCHEMY:
  1647. au1x00_fixup_config_od();
  1648. break;
  1649. case PRID_IMP_PR4450:
  1650. nxp_pr4450_fixup_config();
  1651. break;
  1652. }
  1653. }
  1654. static void r4k_cache_error_setup(void)
  1655. {
  1656. extern char __weak except_vec2_generic;
  1657. extern char __weak except_vec2_sb1;
  1658. switch (current_cpu_type()) {
  1659. case CPU_SB1:
  1660. case CPU_SB1A:
  1661. set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
  1662. break;
  1663. default:
  1664. set_uncached_handler(0x100, &except_vec2_generic, 0x80);
  1665. break;
  1666. }
  1667. }
  1668. void r4k_cache_init(void)
  1669. {
  1670. extern void build_clear_page(void);
  1671. extern void build_copy_page(void);
  1672. struct cpuinfo_mips *c = &current_cpu_data;
  1673. probe_pcache();
  1674. probe_vcache();
  1675. setup_scache();
  1676. r4k_blast_dcache_page_setup();
  1677. r4k_blast_dcache_page_indexed_setup();
  1678. r4k_blast_dcache_setup();
  1679. r4k_blast_icache_page_setup();
  1680. r4k_blast_icache_page_indexed_setup();
  1681. r4k_blast_icache_setup();
  1682. r4k_blast_scache_page_setup();
  1683. r4k_blast_scache_page_indexed_setup();
  1684. r4k_blast_scache_setup();
  1685. r4k_blast_scache_node_setup();
  1686. #ifdef CONFIG_EVA
  1687. r4k_blast_dcache_user_page_setup();
  1688. r4k_blast_icache_user_page_setup();
  1689. #endif
  1690. /*
  1691. * Some MIPS32 and MIPS64 processors have physically indexed caches.
  1692. * This code supports virtually indexed processors and will be
  1693. * unnecessarily inefficient on physically indexed processors.
  1694. */
  1695. if (c->dcache.linesz && cpu_has_dc_aliases)
  1696. shm_align_mask = max_t( unsigned long,
  1697. c->dcache.sets * c->dcache.linesz - 1,
  1698. PAGE_SIZE - 1);
  1699. else
  1700. shm_align_mask = PAGE_SIZE-1;
  1701. __flush_cache_vmap = r4k__flush_cache_vmap;
  1702. __flush_cache_vunmap = r4k__flush_cache_vunmap;
  1703. flush_cache_all = cache_noop;
  1704. __flush_cache_all = r4k___flush_cache_all;
  1705. flush_cache_mm = r4k_flush_cache_mm;
  1706. flush_cache_page = r4k_flush_cache_page;
  1707. flush_cache_range = r4k_flush_cache_range;
  1708. __flush_kernel_vmap_range = r4k_flush_kernel_vmap_range;
  1709. flush_cache_sigtramp = r4k_flush_cache_sigtramp;
  1710. flush_icache_all = r4k_flush_icache_all;
  1711. local_flush_data_cache_page = local_r4k_flush_data_cache_page;
  1712. flush_data_cache_page = r4k_flush_data_cache_page;
  1713. flush_icache_range = r4k_flush_icache_range;
  1714. local_flush_icache_range = local_r4k_flush_icache_range;
  1715. __flush_icache_user_range = r4k_flush_icache_user_range;
  1716. __local_flush_icache_user_range = local_r4k_flush_icache_user_range;
  1717. #if defined(CONFIG_DMA_NONCOHERENT) || defined(CONFIG_DMA_MAYBE_COHERENT)
  1718. # if defined(CONFIG_DMA_PERDEV_COHERENT)
  1719. if (0) {
  1720. # else
  1721. if ((coherentio == IO_COHERENCE_ENABLED) ||
  1722. ((coherentio == IO_COHERENCE_DEFAULT) && hw_coherentio)) {
  1723. # endif
  1724. _dma_cache_wback_inv = (void *)cache_noop;
  1725. _dma_cache_wback = (void *)cache_noop;
  1726. _dma_cache_inv = (void *)cache_noop;
  1727. } else {
  1728. _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
  1729. _dma_cache_wback = r4k_dma_cache_wback_inv;
  1730. _dma_cache_inv = r4k_dma_cache_inv;
  1731. }
  1732. #endif
  1733. build_clear_page();
  1734. build_copy_page();
  1735. /*
  1736. * We want to run CMP kernels on core with and without coherent
  1737. * caches. Therefore, do not use CONFIG_MIPS_CMP to decide whether
  1738. * or not to flush caches.
  1739. */
  1740. local_r4k___flush_cache_all(NULL);
  1741. coherency_setup();
  1742. board_cache_error_setup = r4k_cache_error_setup;
  1743. /*
  1744. * Per-CPU overrides
  1745. */
  1746. switch (current_cpu_type()) {
  1747. case CPU_BMIPS4350:
  1748. case CPU_BMIPS4380:
  1749. /* No IPI is needed because all CPUs share the same D$ */
  1750. flush_data_cache_page = r4k_blast_dcache_page;
  1751. break;
  1752. case CPU_BMIPS5000:
  1753. /* We lose our superpowers if L2 is disabled */
  1754. if (c->scache.flags & MIPS_CACHE_NOT_PRESENT)
  1755. break;
  1756. /* I$ fills from D$ just by emptying the write buffers */
  1757. flush_cache_page = (void *)b5k_instruction_hazard;
  1758. flush_cache_range = (void *)b5k_instruction_hazard;
  1759. flush_cache_sigtramp = (void *)b5k_instruction_hazard;
  1760. local_flush_data_cache_page = (void *)b5k_instruction_hazard;
  1761. flush_data_cache_page = (void *)b5k_instruction_hazard;
  1762. flush_icache_range = (void *)b5k_instruction_hazard;
  1763. local_flush_icache_range = (void *)b5k_instruction_hazard;
  1764. /* Optimization: an L2 flush implicitly flushes the L1 */
  1765. current_cpu_data.options |= MIPS_CPU_INCLUSIVE_CACHES;
  1766. break;
  1767. case CPU_LOONGSON3:
  1768. /* Loongson-3 maintains cache coherency by hardware */
  1769. __flush_cache_all = cache_noop;
  1770. __flush_cache_vmap = cache_noop;
  1771. __flush_cache_vunmap = cache_noop;
  1772. __flush_kernel_vmap_range = (void *)cache_noop;
  1773. flush_cache_mm = (void *)cache_noop;
  1774. flush_cache_page = (void *)cache_noop;
  1775. flush_cache_range = (void *)cache_noop;
  1776. flush_cache_sigtramp = (void *)cache_noop;
  1777. flush_icache_all = (void *)cache_noop;
  1778. flush_data_cache_page = (void *)cache_noop;
  1779. local_flush_data_cache_page = (void *)cache_noop;
  1780. break;
  1781. }
  1782. }
  1783. static int r4k_cache_pm_notifier(struct notifier_block *self, unsigned long cmd,
  1784. void *v)
  1785. {
  1786. switch (cmd) {
  1787. case CPU_PM_ENTER_FAILED:
  1788. case CPU_PM_EXIT:
  1789. coherency_setup();
  1790. break;
  1791. }
  1792. return NOTIFY_OK;
  1793. }
  1794. static struct notifier_block r4k_cache_pm_notifier_block = {
  1795. .notifier_call = r4k_cache_pm_notifier,
  1796. };
  1797. int __init r4k_cache_init_pm(void)
  1798. {
  1799. return cpu_pm_register_notifier(&r4k_cache_pm_notifier_block);
  1800. }
  1801. arch_initcall(r4k_cache_init_pm);