c-r4k.c 54 KB

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