c-r4k.c 46 KB

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