c-r4k.c 53 KB

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