c-r4k.c 45 KB

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