srmmu.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * srmmu.c: SRMMU specific routines for memory management.
  4. *
  5. * Copyright (C) 1995 David S. Miller ([email protected])
  6. * Copyright (C) 1995,2002 Pete Zaitcev ([email protected])
  7. * Copyright (C) 1996 Eddie C. Dost ([email protected])
  8. * Copyright (C) 1997,1998 Jakub Jelinek ([email protected])
  9. * Copyright (C) 1999,2000 Anton Blanchard ([email protected])
  10. */
  11. #include <linux/seq_file.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/memblock.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/kdebug.h>
  17. #include <linux/export.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/log2.h>
  21. #include <linux/gfp.h>
  22. #include <linux/fs.h>
  23. #include <linux/mm.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/io-unit.h>
  28. #include <asm/pgalloc.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/bitext.h>
  31. #include <asm/vaddrs.h>
  32. #include <asm/cache.h>
  33. #include <asm/traps.h>
  34. #include <asm/oplib.h>
  35. #include <asm/mbus.h>
  36. #include <asm/page.h>
  37. #include <asm/asi.h>
  38. #include <asm/smp.h>
  39. #include <asm/io.h>
  40. /* Now the cpu specific definitions. */
  41. #include <asm/turbosparc.h>
  42. #include <asm/tsunami.h>
  43. #include <asm/viking.h>
  44. #include <asm/swift.h>
  45. #include <asm/leon.h>
  46. #include <asm/mxcc.h>
  47. #include <asm/ross.h>
  48. #include "mm_32.h"
  49. enum mbus_module srmmu_modtype;
  50. static unsigned int hwbug_bitmask;
  51. int vac_cache_size;
  52. EXPORT_SYMBOL(vac_cache_size);
  53. int vac_line_size;
  54. extern struct resource sparc_iomap;
  55. extern unsigned long last_valid_pfn;
  56. static pgd_t *srmmu_swapper_pg_dir;
  57. const struct sparc32_cachetlb_ops *sparc32_cachetlb_ops;
  58. EXPORT_SYMBOL(sparc32_cachetlb_ops);
  59. #ifdef CONFIG_SMP
  60. const struct sparc32_cachetlb_ops *local_ops;
  61. #define FLUSH_BEGIN(mm)
  62. #define FLUSH_END
  63. #else
  64. #define FLUSH_BEGIN(mm) if ((mm)->context != NO_CONTEXT) {
  65. #define FLUSH_END }
  66. #endif
  67. int flush_page_for_dma_global = 1;
  68. char *srmmu_name;
  69. ctxd_t *srmmu_ctx_table_phys;
  70. static ctxd_t *srmmu_context_table;
  71. int viking_mxcc_present;
  72. static DEFINE_SPINLOCK(srmmu_context_spinlock);
  73. static int is_hypersparc;
  74. static int srmmu_cache_pagetables;
  75. /* these will be initialized in srmmu_nocache_calcsize() */
  76. static unsigned long srmmu_nocache_size;
  77. static unsigned long srmmu_nocache_end;
  78. /* 1 bit <=> 256 bytes of nocache <=> 64 PTEs */
  79. #define SRMMU_NOCACHE_BITMAP_SHIFT (PAGE_SHIFT - 4)
  80. /* The context table is a nocache user with the biggest alignment needs. */
  81. #define SRMMU_NOCACHE_ALIGN_MAX (sizeof(ctxd_t)*SRMMU_MAX_CONTEXTS)
  82. void *srmmu_nocache_pool;
  83. static struct bit_map srmmu_nocache_map;
  84. static inline int srmmu_pmd_none(pmd_t pmd)
  85. { return !(pmd_val(pmd) & 0xFFFFFFF); }
  86. /* XXX should we hyper_flush_whole_icache here - Anton */
  87. static inline void srmmu_ctxd_set(ctxd_t *ctxp, pgd_t *pgdp)
  88. {
  89. pte_t pte;
  90. pte = __pte((SRMMU_ET_PTD | (__nocache_pa(pgdp) >> 4)));
  91. set_pte((pte_t *)ctxp, pte);
  92. }
  93. /*
  94. * Locations of MSI Registers.
  95. */
  96. #define MSI_MBUS_ARBEN 0xe0001008 /* MBus Arbiter Enable register */
  97. /*
  98. * Useful bits in the MSI Registers.
  99. */
  100. #define MSI_ASYNC_MODE 0x80000000 /* Operate the MSI asynchronously */
  101. static void msi_set_sync(void)
  102. {
  103. __asm__ __volatile__ ("lda [%0] %1, %%g3\n\t"
  104. "andn %%g3, %2, %%g3\n\t"
  105. "sta %%g3, [%0] %1\n\t" : :
  106. "r" (MSI_MBUS_ARBEN),
  107. "i" (ASI_M_CTL), "r" (MSI_ASYNC_MODE) : "g3");
  108. }
  109. void pmd_set(pmd_t *pmdp, pte_t *ptep)
  110. {
  111. unsigned long ptp = __nocache_pa(ptep) >> 4;
  112. set_pte((pte_t *)&pmd_val(*pmdp), __pte(SRMMU_ET_PTD | ptp));
  113. }
  114. /*
  115. * size: bytes to allocate in the nocache area.
  116. * align: bytes, number to align at.
  117. * Returns the virtual address of the allocated area.
  118. */
  119. static void *__srmmu_get_nocache(int size, int align)
  120. {
  121. int offset, minsz = 1 << SRMMU_NOCACHE_BITMAP_SHIFT;
  122. unsigned long addr;
  123. if (size < minsz) {
  124. printk(KERN_ERR "Size 0x%x too small for nocache request\n",
  125. size);
  126. size = minsz;
  127. }
  128. if (size & (minsz - 1)) {
  129. printk(KERN_ERR "Size 0x%x unaligned in nocache request\n",
  130. size);
  131. size += minsz - 1;
  132. }
  133. BUG_ON(align > SRMMU_NOCACHE_ALIGN_MAX);
  134. offset = bit_map_string_get(&srmmu_nocache_map,
  135. size >> SRMMU_NOCACHE_BITMAP_SHIFT,
  136. align >> SRMMU_NOCACHE_BITMAP_SHIFT);
  137. if (offset == -1) {
  138. printk(KERN_ERR "srmmu: out of nocache %d: %d/%d\n",
  139. size, (int) srmmu_nocache_size,
  140. srmmu_nocache_map.used << SRMMU_NOCACHE_BITMAP_SHIFT);
  141. return NULL;
  142. }
  143. addr = SRMMU_NOCACHE_VADDR + (offset << SRMMU_NOCACHE_BITMAP_SHIFT);
  144. return (void *)addr;
  145. }
  146. void *srmmu_get_nocache(int size, int align)
  147. {
  148. void *tmp;
  149. tmp = __srmmu_get_nocache(size, align);
  150. if (tmp)
  151. memset(tmp, 0, size);
  152. return tmp;
  153. }
  154. void srmmu_free_nocache(void *addr, int size)
  155. {
  156. unsigned long vaddr;
  157. int offset;
  158. vaddr = (unsigned long)addr;
  159. if (vaddr < SRMMU_NOCACHE_VADDR) {
  160. printk("Vaddr %lx is smaller than nocache base 0x%lx\n",
  161. vaddr, (unsigned long)SRMMU_NOCACHE_VADDR);
  162. BUG();
  163. }
  164. if (vaddr + size > srmmu_nocache_end) {
  165. printk("Vaddr %lx is bigger than nocache end 0x%lx\n",
  166. vaddr, srmmu_nocache_end);
  167. BUG();
  168. }
  169. if (!is_power_of_2(size)) {
  170. printk("Size 0x%x is not a power of 2\n", size);
  171. BUG();
  172. }
  173. if (size < SRMMU_NOCACHE_BITMAP_SHIFT) {
  174. printk("Size 0x%x is too small\n", size);
  175. BUG();
  176. }
  177. if (vaddr & (size - 1)) {
  178. printk("Vaddr %lx is not aligned to size 0x%x\n", vaddr, size);
  179. BUG();
  180. }
  181. offset = (vaddr - SRMMU_NOCACHE_VADDR) >> SRMMU_NOCACHE_BITMAP_SHIFT;
  182. size = size >> SRMMU_NOCACHE_BITMAP_SHIFT;
  183. bit_map_clear(&srmmu_nocache_map, offset, size);
  184. }
  185. static void srmmu_early_allocate_ptable_skeleton(unsigned long start,
  186. unsigned long end);
  187. /* Return how much physical memory we have. */
  188. static unsigned long __init probe_memory(void)
  189. {
  190. unsigned long total = 0;
  191. int i;
  192. for (i = 0; sp_banks[i].num_bytes; i++)
  193. total += sp_banks[i].num_bytes;
  194. return total;
  195. }
  196. /*
  197. * Reserve nocache dynamically proportionally to the amount of
  198. * system RAM. -- Tomas Szepe <[email protected]>, June 2002
  199. */
  200. static void __init srmmu_nocache_calcsize(void)
  201. {
  202. unsigned long sysmemavail = probe_memory() / 1024;
  203. int srmmu_nocache_npages;
  204. srmmu_nocache_npages =
  205. sysmemavail / SRMMU_NOCACHE_ALCRATIO / 1024 * 256;
  206. /* P3 XXX The 4x overuse: corroborated by /proc/meminfo. */
  207. // if (srmmu_nocache_npages < 256) srmmu_nocache_npages = 256;
  208. if (srmmu_nocache_npages < SRMMU_MIN_NOCACHE_PAGES)
  209. srmmu_nocache_npages = SRMMU_MIN_NOCACHE_PAGES;
  210. /* anything above 1280 blows up */
  211. if (srmmu_nocache_npages > SRMMU_MAX_NOCACHE_PAGES)
  212. srmmu_nocache_npages = SRMMU_MAX_NOCACHE_PAGES;
  213. srmmu_nocache_size = srmmu_nocache_npages * PAGE_SIZE;
  214. srmmu_nocache_end = SRMMU_NOCACHE_VADDR + srmmu_nocache_size;
  215. }
  216. static void __init srmmu_nocache_init(void)
  217. {
  218. void *srmmu_nocache_bitmap;
  219. unsigned int bitmap_bits;
  220. pgd_t *pgd;
  221. p4d_t *p4d;
  222. pud_t *pud;
  223. pmd_t *pmd;
  224. pte_t *pte;
  225. unsigned long paddr, vaddr;
  226. unsigned long pteval;
  227. bitmap_bits = srmmu_nocache_size >> SRMMU_NOCACHE_BITMAP_SHIFT;
  228. srmmu_nocache_pool = memblock_alloc(srmmu_nocache_size,
  229. SRMMU_NOCACHE_ALIGN_MAX);
  230. if (!srmmu_nocache_pool)
  231. panic("%s: Failed to allocate %lu bytes align=0x%x\n",
  232. __func__, srmmu_nocache_size, SRMMU_NOCACHE_ALIGN_MAX);
  233. memset(srmmu_nocache_pool, 0, srmmu_nocache_size);
  234. srmmu_nocache_bitmap =
  235. memblock_alloc(BITS_TO_LONGS(bitmap_bits) * sizeof(long),
  236. SMP_CACHE_BYTES);
  237. if (!srmmu_nocache_bitmap)
  238. panic("%s: Failed to allocate %zu bytes\n", __func__,
  239. BITS_TO_LONGS(bitmap_bits) * sizeof(long));
  240. bit_map_init(&srmmu_nocache_map, srmmu_nocache_bitmap, bitmap_bits);
  241. srmmu_swapper_pg_dir = __srmmu_get_nocache(SRMMU_PGD_TABLE_SIZE, SRMMU_PGD_TABLE_SIZE);
  242. memset(__nocache_fix(srmmu_swapper_pg_dir), 0, SRMMU_PGD_TABLE_SIZE);
  243. init_mm.pgd = srmmu_swapper_pg_dir;
  244. srmmu_early_allocate_ptable_skeleton(SRMMU_NOCACHE_VADDR, srmmu_nocache_end);
  245. paddr = __pa((unsigned long)srmmu_nocache_pool);
  246. vaddr = SRMMU_NOCACHE_VADDR;
  247. while (vaddr < srmmu_nocache_end) {
  248. pgd = pgd_offset_k(vaddr);
  249. p4d = p4d_offset(pgd, vaddr);
  250. pud = pud_offset(p4d, vaddr);
  251. pmd = pmd_offset(__nocache_fix(pud), vaddr);
  252. pte = pte_offset_kernel(__nocache_fix(pmd), vaddr);
  253. pteval = ((paddr >> 4) | SRMMU_ET_PTE | SRMMU_PRIV);
  254. if (srmmu_cache_pagetables)
  255. pteval |= SRMMU_CACHE;
  256. set_pte(__nocache_fix(pte), __pte(pteval));
  257. vaddr += PAGE_SIZE;
  258. paddr += PAGE_SIZE;
  259. }
  260. flush_cache_all();
  261. flush_tlb_all();
  262. }
  263. pgd_t *get_pgd_fast(void)
  264. {
  265. pgd_t *pgd = NULL;
  266. pgd = __srmmu_get_nocache(SRMMU_PGD_TABLE_SIZE, SRMMU_PGD_TABLE_SIZE);
  267. if (pgd) {
  268. pgd_t *init = pgd_offset_k(0);
  269. memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
  270. memcpy(pgd + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD,
  271. (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
  272. }
  273. return pgd;
  274. }
  275. /*
  276. * Hardware needs alignment to 256 only, but we align to whole page size
  277. * to reduce fragmentation problems due to the buddy principle.
  278. * XXX Provide actual fragmentation statistics in /proc.
  279. *
  280. * Alignments up to the page size are the same for physical and virtual
  281. * addresses of the nocache area.
  282. */
  283. pgtable_t pte_alloc_one(struct mm_struct *mm)
  284. {
  285. pte_t *ptep;
  286. struct page *page;
  287. if (!(ptep = pte_alloc_one_kernel(mm)))
  288. return NULL;
  289. page = pfn_to_page(__nocache_pa((unsigned long)ptep) >> PAGE_SHIFT);
  290. spin_lock(&mm->page_table_lock);
  291. if (page_ref_inc_return(page) == 2 && !pgtable_pte_page_ctor(page)) {
  292. page_ref_dec(page);
  293. ptep = NULL;
  294. }
  295. spin_unlock(&mm->page_table_lock);
  296. return ptep;
  297. }
  298. void pte_free(struct mm_struct *mm, pgtable_t ptep)
  299. {
  300. struct page *page;
  301. page = pfn_to_page(__nocache_pa((unsigned long)ptep) >> PAGE_SHIFT);
  302. spin_lock(&mm->page_table_lock);
  303. if (page_ref_dec_return(page) == 1)
  304. pgtable_pte_page_dtor(page);
  305. spin_unlock(&mm->page_table_lock);
  306. srmmu_free_nocache(ptep, SRMMU_PTE_TABLE_SIZE);
  307. }
  308. /* context handling - a dynamically sized pool is used */
  309. #define NO_CONTEXT -1
  310. struct ctx_list {
  311. struct ctx_list *next;
  312. struct ctx_list *prev;
  313. unsigned int ctx_number;
  314. struct mm_struct *ctx_mm;
  315. };
  316. static struct ctx_list *ctx_list_pool;
  317. static struct ctx_list ctx_free;
  318. static struct ctx_list ctx_used;
  319. /* At boot time we determine the number of contexts */
  320. static int num_contexts;
  321. static inline void remove_from_ctx_list(struct ctx_list *entry)
  322. {
  323. entry->next->prev = entry->prev;
  324. entry->prev->next = entry->next;
  325. }
  326. static inline void add_to_ctx_list(struct ctx_list *head, struct ctx_list *entry)
  327. {
  328. entry->next = head;
  329. (entry->prev = head->prev)->next = entry;
  330. head->prev = entry;
  331. }
  332. #define add_to_free_ctxlist(entry) add_to_ctx_list(&ctx_free, entry)
  333. #define add_to_used_ctxlist(entry) add_to_ctx_list(&ctx_used, entry)
  334. static inline void alloc_context(struct mm_struct *old_mm, struct mm_struct *mm)
  335. {
  336. struct ctx_list *ctxp;
  337. ctxp = ctx_free.next;
  338. if (ctxp != &ctx_free) {
  339. remove_from_ctx_list(ctxp);
  340. add_to_used_ctxlist(ctxp);
  341. mm->context = ctxp->ctx_number;
  342. ctxp->ctx_mm = mm;
  343. return;
  344. }
  345. ctxp = ctx_used.next;
  346. if (ctxp->ctx_mm == old_mm)
  347. ctxp = ctxp->next;
  348. if (ctxp == &ctx_used)
  349. panic("out of mmu contexts");
  350. flush_cache_mm(ctxp->ctx_mm);
  351. flush_tlb_mm(ctxp->ctx_mm);
  352. remove_from_ctx_list(ctxp);
  353. add_to_used_ctxlist(ctxp);
  354. ctxp->ctx_mm->context = NO_CONTEXT;
  355. ctxp->ctx_mm = mm;
  356. mm->context = ctxp->ctx_number;
  357. }
  358. static inline void free_context(int context)
  359. {
  360. struct ctx_list *ctx_old;
  361. ctx_old = ctx_list_pool + context;
  362. remove_from_ctx_list(ctx_old);
  363. add_to_free_ctxlist(ctx_old);
  364. }
  365. static void __init sparc_context_init(int numctx)
  366. {
  367. int ctx;
  368. unsigned long size;
  369. size = numctx * sizeof(struct ctx_list);
  370. ctx_list_pool = memblock_alloc(size, SMP_CACHE_BYTES);
  371. if (!ctx_list_pool)
  372. panic("%s: Failed to allocate %lu bytes\n", __func__, size);
  373. for (ctx = 0; ctx < numctx; ctx++) {
  374. struct ctx_list *clist;
  375. clist = (ctx_list_pool + ctx);
  376. clist->ctx_number = ctx;
  377. clist->ctx_mm = NULL;
  378. }
  379. ctx_free.next = ctx_free.prev = &ctx_free;
  380. ctx_used.next = ctx_used.prev = &ctx_used;
  381. for (ctx = 0; ctx < numctx; ctx++)
  382. add_to_free_ctxlist(ctx_list_pool + ctx);
  383. }
  384. void switch_mm(struct mm_struct *old_mm, struct mm_struct *mm,
  385. struct task_struct *tsk)
  386. {
  387. unsigned long flags;
  388. if (mm->context == NO_CONTEXT) {
  389. spin_lock_irqsave(&srmmu_context_spinlock, flags);
  390. alloc_context(old_mm, mm);
  391. spin_unlock_irqrestore(&srmmu_context_spinlock, flags);
  392. srmmu_ctxd_set(&srmmu_context_table[mm->context], mm->pgd);
  393. }
  394. if (sparc_cpu_model == sparc_leon)
  395. leon_switch_mm();
  396. if (is_hypersparc)
  397. hyper_flush_whole_icache();
  398. srmmu_set_context(mm->context);
  399. }
  400. /* Low level IO area allocation on the SRMMU. */
  401. static inline void srmmu_mapioaddr(unsigned long physaddr,
  402. unsigned long virt_addr, int bus_type)
  403. {
  404. pgd_t *pgdp;
  405. p4d_t *p4dp;
  406. pud_t *pudp;
  407. pmd_t *pmdp;
  408. pte_t *ptep;
  409. unsigned long tmp;
  410. physaddr &= PAGE_MASK;
  411. pgdp = pgd_offset_k(virt_addr);
  412. p4dp = p4d_offset(pgdp, virt_addr);
  413. pudp = pud_offset(p4dp, virt_addr);
  414. pmdp = pmd_offset(pudp, virt_addr);
  415. ptep = pte_offset_kernel(pmdp, virt_addr);
  416. tmp = (physaddr >> 4) | SRMMU_ET_PTE;
  417. /* I need to test whether this is consistent over all
  418. * sun4m's. The bus_type represents the upper 4 bits of
  419. * 36-bit physical address on the I/O space lines...
  420. */
  421. tmp |= (bus_type << 28);
  422. tmp |= SRMMU_PRIV;
  423. __flush_page_to_ram(virt_addr);
  424. set_pte(ptep, __pte(tmp));
  425. }
  426. void srmmu_mapiorange(unsigned int bus, unsigned long xpa,
  427. unsigned long xva, unsigned int len)
  428. {
  429. while (len != 0) {
  430. len -= PAGE_SIZE;
  431. srmmu_mapioaddr(xpa, xva, bus);
  432. xva += PAGE_SIZE;
  433. xpa += PAGE_SIZE;
  434. }
  435. flush_tlb_all();
  436. }
  437. static inline void srmmu_unmapioaddr(unsigned long virt_addr)
  438. {
  439. pgd_t *pgdp;
  440. p4d_t *p4dp;
  441. pud_t *pudp;
  442. pmd_t *pmdp;
  443. pte_t *ptep;
  444. pgdp = pgd_offset_k(virt_addr);
  445. p4dp = p4d_offset(pgdp, virt_addr);
  446. pudp = pud_offset(p4dp, virt_addr);
  447. pmdp = pmd_offset(pudp, virt_addr);
  448. ptep = pte_offset_kernel(pmdp, virt_addr);
  449. /* No need to flush uncacheable page. */
  450. __pte_clear(ptep);
  451. }
  452. void srmmu_unmapiorange(unsigned long virt_addr, unsigned int len)
  453. {
  454. while (len != 0) {
  455. len -= PAGE_SIZE;
  456. srmmu_unmapioaddr(virt_addr);
  457. virt_addr += PAGE_SIZE;
  458. }
  459. flush_tlb_all();
  460. }
  461. /* tsunami.S */
  462. extern void tsunami_flush_cache_all(void);
  463. extern void tsunami_flush_cache_mm(struct mm_struct *mm);
  464. extern void tsunami_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  465. extern void tsunami_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
  466. extern void tsunami_flush_page_to_ram(unsigned long page);
  467. extern void tsunami_flush_page_for_dma(unsigned long page);
  468. extern void tsunami_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr);
  469. extern void tsunami_flush_tlb_all(void);
  470. extern void tsunami_flush_tlb_mm(struct mm_struct *mm);
  471. extern void tsunami_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  472. extern void tsunami_flush_tlb_page(struct vm_area_struct *vma, unsigned long page);
  473. extern void tsunami_setup_blockops(void);
  474. /* swift.S */
  475. extern void swift_flush_cache_all(void);
  476. extern void swift_flush_cache_mm(struct mm_struct *mm);
  477. extern void swift_flush_cache_range(struct vm_area_struct *vma,
  478. unsigned long start, unsigned long end);
  479. extern void swift_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
  480. extern void swift_flush_page_to_ram(unsigned long page);
  481. extern void swift_flush_page_for_dma(unsigned long page);
  482. extern void swift_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr);
  483. extern void swift_flush_tlb_all(void);
  484. extern void swift_flush_tlb_mm(struct mm_struct *mm);
  485. extern void swift_flush_tlb_range(struct vm_area_struct *vma,
  486. unsigned long start, unsigned long end);
  487. extern void swift_flush_tlb_page(struct vm_area_struct *vma, unsigned long page);
  488. #if 0 /* P3: deadwood to debug precise flushes on Swift. */
  489. void swift_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  490. {
  491. int cctx, ctx1;
  492. page &= PAGE_MASK;
  493. if ((ctx1 = vma->vm_mm->context) != -1) {
  494. cctx = srmmu_get_context();
  495. /* Is context # ever different from current context? P3 */
  496. if (cctx != ctx1) {
  497. printk("flush ctx %02x curr %02x\n", ctx1, cctx);
  498. srmmu_set_context(ctx1);
  499. swift_flush_page(page);
  500. __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : :
  501. "r" (page), "i" (ASI_M_FLUSH_PROBE));
  502. srmmu_set_context(cctx);
  503. } else {
  504. /* Rm. prot. bits from virt. c. */
  505. /* swift_flush_cache_all(); */
  506. /* swift_flush_cache_page(vma, page); */
  507. swift_flush_page(page);
  508. __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : :
  509. "r" (page), "i" (ASI_M_FLUSH_PROBE));
  510. /* same as above: srmmu_flush_tlb_page() */
  511. }
  512. }
  513. }
  514. #endif
  515. /*
  516. * The following are all MBUS based SRMMU modules, and therefore could
  517. * be found in a multiprocessor configuration. On the whole, these
  518. * chips seems to be much more touchy about DVMA and page tables
  519. * with respect to cache coherency.
  520. */
  521. /* viking.S */
  522. extern void viking_flush_cache_all(void);
  523. extern void viking_flush_cache_mm(struct mm_struct *mm);
  524. extern void viking_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  525. unsigned long end);
  526. extern void viking_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
  527. extern void viking_flush_page_to_ram(unsigned long page);
  528. extern void viking_flush_page_for_dma(unsigned long page);
  529. extern void viking_flush_sig_insns(struct mm_struct *mm, unsigned long addr);
  530. extern void viking_flush_page(unsigned long page);
  531. extern void viking_mxcc_flush_page(unsigned long page);
  532. extern void viking_flush_tlb_all(void);
  533. extern void viking_flush_tlb_mm(struct mm_struct *mm);
  534. extern void viking_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  535. unsigned long end);
  536. extern void viking_flush_tlb_page(struct vm_area_struct *vma,
  537. unsigned long page);
  538. extern void sun4dsmp_flush_tlb_all(void);
  539. extern void sun4dsmp_flush_tlb_mm(struct mm_struct *mm);
  540. extern void sun4dsmp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  541. unsigned long end);
  542. extern void sun4dsmp_flush_tlb_page(struct vm_area_struct *vma,
  543. unsigned long page);
  544. /* hypersparc.S */
  545. extern void hypersparc_flush_cache_all(void);
  546. extern void hypersparc_flush_cache_mm(struct mm_struct *mm);
  547. extern void hypersparc_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  548. extern void hypersparc_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
  549. extern void hypersparc_flush_page_to_ram(unsigned long page);
  550. extern void hypersparc_flush_page_for_dma(unsigned long page);
  551. extern void hypersparc_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr);
  552. extern void hypersparc_flush_tlb_all(void);
  553. extern void hypersparc_flush_tlb_mm(struct mm_struct *mm);
  554. extern void hypersparc_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  555. extern void hypersparc_flush_tlb_page(struct vm_area_struct *vma, unsigned long page);
  556. extern void hypersparc_setup_blockops(void);
  557. /*
  558. * NOTE: All of this startup code assumes the low 16mb (approx.) of
  559. * kernel mappings are done with one single contiguous chunk of
  560. * ram. On small ram machines (classics mainly) we only get
  561. * around 8mb mapped for us.
  562. */
  563. static void __init early_pgtable_allocfail(char *type)
  564. {
  565. prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type);
  566. prom_halt();
  567. }
  568. static void __init srmmu_early_allocate_ptable_skeleton(unsigned long start,
  569. unsigned long end)
  570. {
  571. pgd_t *pgdp;
  572. p4d_t *p4dp;
  573. pud_t *pudp;
  574. pmd_t *pmdp;
  575. pte_t *ptep;
  576. while (start < end) {
  577. pgdp = pgd_offset_k(start);
  578. p4dp = p4d_offset(pgdp, start);
  579. pudp = pud_offset(p4dp, start);
  580. if (pud_none(*__nocache_fix(pudp))) {
  581. pmdp = __srmmu_get_nocache(
  582. SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE);
  583. if (pmdp == NULL)
  584. early_pgtable_allocfail("pmd");
  585. memset(__nocache_fix(pmdp), 0, SRMMU_PMD_TABLE_SIZE);
  586. pud_set(__nocache_fix(pudp), pmdp);
  587. }
  588. pmdp = pmd_offset(__nocache_fix(pudp), start);
  589. if (srmmu_pmd_none(*__nocache_fix(pmdp))) {
  590. ptep = __srmmu_get_nocache(PTE_SIZE, PTE_SIZE);
  591. if (ptep == NULL)
  592. early_pgtable_allocfail("pte");
  593. memset(__nocache_fix(ptep), 0, PTE_SIZE);
  594. pmd_set(__nocache_fix(pmdp), ptep);
  595. }
  596. if (start > (0xffffffffUL - PMD_SIZE))
  597. break;
  598. start = (start + PMD_SIZE) & PMD_MASK;
  599. }
  600. }
  601. static void __init srmmu_allocate_ptable_skeleton(unsigned long start,
  602. unsigned long end)
  603. {
  604. pgd_t *pgdp;
  605. p4d_t *p4dp;
  606. pud_t *pudp;
  607. pmd_t *pmdp;
  608. pte_t *ptep;
  609. while (start < end) {
  610. pgdp = pgd_offset_k(start);
  611. p4dp = p4d_offset(pgdp, start);
  612. pudp = pud_offset(p4dp, start);
  613. if (pud_none(*pudp)) {
  614. pmdp = __srmmu_get_nocache(SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE);
  615. if (pmdp == NULL)
  616. early_pgtable_allocfail("pmd");
  617. memset(pmdp, 0, SRMMU_PMD_TABLE_SIZE);
  618. pud_set((pud_t *)pgdp, pmdp);
  619. }
  620. pmdp = pmd_offset(pudp, start);
  621. if (srmmu_pmd_none(*pmdp)) {
  622. ptep = __srmmu_get_nocache(PTE_SIZE,
  623. PTE_SIZE);
  624. if (ptep == NULL)
  625. early_pgtable_allocfail("pte");
  626. memset(ptep, 0, PTE_SIZE);
  627. pmd_set(pmdp, ptep);
  628. }
  629. if (start > (0xffffffffUL - PMD_SIZE))
  630. break;
  631. start = (start + PMD_SIZE) & PMD_MASK;
  632. }
  633. }
  634. /* These flush types are not available on all chips... */
  635. static inline unsigned long srmmu_probe(unsigned long vaddr)
  636. {
  637. unsigned long retval;
  638. if (sparc_cpu_model != sparc_leon) {
  639. vaddr &= PAGE_MASK;
  640. __asm__ __volatile__("lda [%1] %2, %0\n\t" :
  641. "=r" (retval) :
  642. "r" (vaddr | 0x400), "i" (ASI_M_FLUSH_PROBE));
  643. } else {
  644. retval = leon_swprobe(vaddr, NULL);
  645. }
  646. return retval;
  647. }
  648. /*
  649. * This is much cleaner than poking around physical address space
  650. * looking at the prom's page table directly which is what most
  651. * other OS's do. Yuck... this is much better.
  652. */
  653. static void __init srmmu_inherit_prom_mappings(unsigned long start,
  654. unsigned long end)
  655. {
  656. unsigned long probed;
  657. unsigned long addr;
  658. pgd_t *pgdp;
  659. p4d_t *p4dp;
  660. pud_t *pudp;
  661. pmd_t *pmdp;
  662. pte_t *ptep;
  663. int what; /* 0 = normal-pte, 1 = pmd-level pte, 2 = pgd-level pte */
  664. while (start <= end) {
  665. if (start == 0)
  666. break; /* probably wrap around */
  667. if (start == 0xfef00000)
  668. start = KADB_DEBUGGER_BEGVM;
  669. probed = srmmu_probe(start);
  670. if (!probed) {
  671. /* continue probing until we find an entry */
  672. start += PAGE_SIZE;
  673. continue;
  674. }
  675. /* A red snapper, see what it really is. */
  676. what = 0;
  677. addr = start - PAGE_SIZE;
  678. if (!(start & ~(PMD_MASK))) {
  679. if (srmmu_probe(addr + PMD_SIZE) == probed)
  680. what = 1;
  681. }
  682. if (!(start & ~(PGDIR_MASK))) {
  683. if (srmmu_probe(addr + PGDIR_SIZE) == probed)
  684. what = 2;
  685. }
  686. pgdp = pgd_offset_k(start);
  687. p4dp = p4d_offset(pgdp, start);
  688. pudp = pud_offset(p4dp, start);
  689. if (what == 2) {
  690. *__nocache_fix(pgdp) = __pgd(probed);
  691. start += PGDIR_SIZE;
  692. continue;
  693. }
  694. if (pud_none(*__nocache_fix(pudp))) {
  695. pmdp = __srmmu_get_nocache(SRMMU_PMD_TABLE_SIZE,
  696. SRMMU_PMD_TABLE_SIZE);
  697. if (pmdp == NULL)
  698. early_pgtable_allocfail("pmd");
  699. memset(__nocache_fix(pmdp), 0, SRMMU_PMD_TABLE_SIZE);
  700. pud_set(__nocache_fix(pudp), pmdp);
  701. }
  702. pmdp = pmd_offset(__nocache_fix(pudp), start);
  703. if (what == 1) {
  704. *(pmd_t *)__nocache_fix(pmdp) = __pmd(probed);
  705. start += PMD_SIZE;
  706. continue;
  707. }
  708. if (srmmu_pmd_none(*__nocache_fix(pmdp))) {
  709. ptep = __srmmu_get_nocache(PTE_SIZE, PTE_SIZE);
  710. if (ptep == NULL)
  711. early_pgtable_allocfail("pte");
  712. memset(__nocache_fix(ptep), 0, PTE_SIZE);
  713. pmd_set(__nocache_fix(pmdp), ptep);
  714. }
  715. ptep = pte_offset_kernel(__nocache_fix(pmdp), start);
  716. *__nocache_fix(ptep) = __pte(probed);
  717. start += PAGE_SIZE;
  718. }
  719. }
  720. #define KERNEL_PTE(page_shifted) ((page_shifted)|SRMMU_CACHE|SRMMU_PRIV|SRMMU_VALID)
  721. /* Create a third-level SRMMU 16MB page mapping. */
  722. static void __init do_large_mapping(unsigned long vaddr, unsigned long phys_base)
  723. {
  724. pgd_t *pgdp = pgd_offset_k(vaddr);
  725. unsigned long big_pte;
  726. big_pte = KERNEL_PTE(phys_base >> 4);
  727. *__nocache_fix(pgdp) = __pgd(big_pte);
  728. }
  729. /* Map sp_bank entry SP_ENTRY, starting at virtual address VBASE. */
  730. static unsigned long __init map_spbank(unsigned long vbase, int sp_entry)
  731. {
  732. unsigned long pstart = (sp_banks[sp_entry].base_addr & PGDIR_MASK);
  733. unsigned long vstart = (vbase & PGDIR_MASK);
  734. unsigned long vend = PGDIR_ALIGN(vbase + sp_banks[sp_entry].num_bytes);
  735. /* Map "low" memory only */
  736. const unsigned long min_vaddr = PAGE_OFFSET;
  737. const unsigned long max_vaddr = PAGE_OFFSET + SRMMU_MAXMEM;
  738. if (vstart < min_vaddr || vstart >= max_vaddr)
  739. return vstart;
  740. if (vend > max_vaddr || vend < min_vaddr)
  741. vend = max_vaddr;
  742. while (vstart < vend) {
  743. do_large_mapping(vstart, pstart);
  744. vstart += PGDIR_SIZE; pstart += PGDIR_SIZE;
  745. }
  746. return vstart;
  747. }
  748. static void __init map_kernel(void)
  749. {
  750. int i;
  751. if (phys_base > 0) {
  752. do_large_mapping(PAGE_OFFSET, phys_base);
  753. }
  754. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  755. map_spbank((unsigned long)__va(sp_banks[i].base_addr), i);
  756. }
  757. }
  758. void (*poke_srmmu)(void) = NULL;
  759. void __init srmmu_paging_init(void)
  760. {
  761. int i;
  762. phandle cpunode;
  763. char node_str[128];
  764. pgd_t *pgd;
  765. p4d_t *p4d;
  766. pud_t *pud;
  767. pmd_t *pmd;
  768. pte_t *pte;
  769. unsigned long pages_avail;
  770. init_mm.context = (unsigned long) NO_CONTEXT;
  771. sparc_iomap.start = SUN4M_IOBASE_VADDR; /* 16MB of IOSPACE on all sun4m's. */
  772. if (sparc_cpu_model == sun4d)
  773. num_contexts = 65536; /* We know it is Viking */
  774. else {
  775. /* Find the number of contexts on the srmmu. */
  776. cpunode = prom_getchild(prom_root_node);
  777. num_contexts = 0;
  778. while (cpunode != 0) {
  779. prom_getstring(cpunode, "device_type", node_str, sizeof(node_str));
  780. if (!strcmp(node_str, "cpu")) {
  781. num_contexts = prom_getintdefault(cpunode, "mmu-nctx", 0x8);
  782. break;
  783. }
  784. cpunode = prom_getsibling(cpunode);
  785. }
  786. }
  787. if (!num_contexts) {
  788. prom_printf("Something wrong, can't find cpu node in paging_init.\n");
  789. prom_halt();
  790. }
  791. pages_avail = 0;
  792. last_valid_pfn = bootmem_init(&pages_avail);
  793. srmmu_nocache_calcsize();
  794. srmmu_nocache_init();
  795. srmmu_inherit_prom_mappings(0xfe400000, (LINUX_OPPROM_ENDVM - PAGE_SIZE));
  796. map_kernel();
  797. /* ctx table has to be physically aligned to its size */
  798. srmmu_context_table = __srmmu_get_nocache(num_contexts * sizeof(ctxd_t), num_contexts * sizeof(ctxd_t));
  799. srmmu_ctx_table_phys = (ctxd_t *)__nocache_pa(srmmu_context_table);
  800. for (i = 0; i < num_contexts; i++)
  801. srmmu_ctxd_set(__nocache_fix(&srmmu_context_table[i]), srmmu_swapper_pg_dir);
  802. flush_cache_all();
  803. srmmu_set_ctable_ptr((unsigned long)srmmu_ctx_table_phys);
  804. #ifdef CONFIG_SMP
  805. /* Stop from hanging here... */
  806. local_ops->tlb_all();
  807. #else
  808. flush_tlb_all();
  809. #endif
  810. poke_srmmu();
  811. srmmu_allocate_ptable_skeleton(sparc_iomap.start, IOBASE_END);
  812. srmmu_allocate_ptable_skeleton(DVMA_VADDR, DVMA_END);
  813. srmmu_allocate_ptable_skeleton(
  814. __fix_to_virt(__end_of_fixed_addresses - 1), FIXADDR_TOP);
  815. srmmu_allocate_ptable_skeleton(PKMAP_BASE, PKMAP_END);
  816. pgd = pgd_offset_k(PKMAP_BASE);
  817. p4d = p4d_offset(pgd, PKMAP_BASE);
  818. pud = pud_offset(p4d, PKMAP_BASE);
  819. pmd = pmd_offset(pud, PKMAP_BASE);
  820. pte = pte_offset_kernel(pmd, PKMAP_BASE);
  821. pkmap_page_table = pte;
  822. flush_cache_all();
  823. flush_tlb_all();
  824. sparc_context_init(num_contexts);
  825. {
  826. unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
  827. max_zone_pfn[ZONE_DMA] = max_low_pfn;
  828. max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
  829. max_zone_pfn[ZONE_HIGHMEM] = highend_pfn;
  830. free_area_init(max_zone_pfn);
  831. }
  832. }
  833. void mmu_info(struct seq_file *m)
  834. {
  835. seq_printf(m,
  836. "MMU type\t: %s\n"
  837. "contexts\t: %d\n"
  838. "nocache total\t: %ld\n"
  839. "nocache used\t: %d\n",
  840. srmmu_name,
  841. num_contexts,
  842. srmmu_nocache_size,
  843. srmmu_nocache_map.used << SRMMU_NOCACHE_BITMAP_SHIFT);
  844. }
  845. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  846. {
  847. mm->context = NO_CONTEXT;
  848. return 0;
  849. }
  850. void destroy_context(struct mm_struct *mm)
  851. {
  852. unsigned long flags;
  853. if (mm->context != NO_CONTEXT) {
  854. flush_cache_mm(mm);
  855. srmmu_ctxd_set(&srmmu_context_table[mm->context], srmmu_swapper_pg_dir);
  856. flush_tlb_mm(mm);
  857. spin_lock_irqsave(&srmmu_context_spinlock, flags);
  858. free_context(mm->context);
  859. spin_unlock_irqrestore(&srmmu_context_spinlock, flags);
  860. mm->context = NO_CONTEXT;
  861. }
  862. }
  863. /* Init various srmmu chip types. */
  864. static void __init srmmu_is_bad(void)
  865. {
  866. prom_printf("Could not determine SRMMU chip type.\n");
  867. prom_halt();
  868. }
  869. static void __init init_vac_layout(void)
  870. {
  871. phandle nd;
  872. int cache_lines;
  873. char node_str[128];
  874. #ifdef CONFIG_SMP
  875. int cpu = 0;
  876. unsigned long max_size = 0;
  877. unsigned long min_line_size = 0x10000000;
  878. #endif
  879. nd = prom_getchild(prom_root_node);
  880. while ((nd = prom_getsibling(nd)) != 0) {
  881. prom_getstring(nd, "device_type", node_str, sizeof(node_str));
  882. if (!strcmp(node_str, "cpu")) {
  883. vac_line_size = prom_getint(nd, "cache-line-size");
  884. if (vac_line_size == -1) {
  885. prom_printf("can't determine cache-line-size, halting.\n");
  886. prom_halt();
  887. }
  888. cache_lines = prom_getint(nd, "cache-nlines");
  889. if (cache_lines == -1) {
  890. prom_printf("can't determine cache-nlines, halting.\n");
  891. prom_halt();
  892. }
  893. vac_cache_size = cache_lines * vac_line_size;
  894. #ifdef CONFIG_SMP
  895. if (vac_cache_size > max_size)
  896. max_size = vac_cache_size;
  897. if (vac_line_size < min_line_size)
  898. min_line_size = vac_line_size;
  899. //FIXME: cpus not contiguous!!
  900. cpu++;
  901. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  902. break;
  903. #else
  904. break;
  905. #endif
  906. }
  907. }
  908. if (nd == 0) {
  909. prom_printf("No CPU nodes found, halting.\n");
  910. prom_halt();
  911. }
  912. #ifdef CONFIG_SMP
  913. vac_cache_size = max_size;
  914. vac_line_size = min_line_size;
  915. #endif
  916. printk("SRMMU: Using VAC size of %d bytes, line size %d bytes.\n",
  917. (int)vac_cache_size, (int)vac_line_size);
  918. }
  919. static void poke_hypersparc(void)
  920. {
  921. volatile unsigned long clear;
  922. unsigned long mreg = srmmu_get_mmureg();
  923. hyper_flush_unconditional_combined();
  924. mreg &= ~(HYPERSPARC_CWENABLE);
  925. mreg |= (HYPERSPARC_CENABLE | HYPERSPARC_WBENABLE);
  926. mreg |= (HYPERSPARC_CMODE);
  927. srmmu_set_mmureg(mreg);
  928. #if 0 /* XXX I think this is bad news... -DaveM */
  929. hyper_clear_all_tags();
  930. #endif
  931. put_ross_icr(HYPERSPARC_ICCR_FTD | HYPERSPARC_ICCR_ICE);
  932. hyper_flush_whole_icache();
  933. clear = srmmu_get_faddr();
  934. clear = srmmu_get_fstatus();
  935. }
  936. static const struct sparc32_cachetlb_ops hypersparc_ops = {
  937. .cache_all = hypersparc_flush_cache_all,
  938. .cache_mm = hypersparc_flush_cache_mm,
  939. .cache_page = hypersparc_flush_cache_page,
  940. .cache_range = hypersparc_flush_cache_range,
  941. .tlb_all = hypersparc_flush_tlb_all,
  942. .tlb_mm = hypersparc_flush_tlb_mm,
  943. .tlb_page = hypersparc_flush_tlb_page,
  944. .tlb_range = hypersparc_flush_tlb_range,
  945. .page_to_ram = hypersparc_flush_page_to_ram,
  946. .sig_insns = hypersparc_flush_sig_insns,
  947. .page_for_dma = hypersparc_flush_page_for_dma,
  948. };
  949. static void __init init_hypersparc(void)
  950. {
  951. srmmu_name = "ROSS HyperSparc";
  952. srmmu_modtype = HyperSparc;
  953. init_vac_layout();
  954. is_hypersparc = 1;
  955. sparc32_cachetlb_ops = &hypersparc_ops;
  956. poke_srmmu = poke_hypersparc;
  957. hypersparc_setup_blockops();
  958. }
  959. static void poke_swift(void)
  960. {
  961. unsigned long mreg;
  962. /* Clear any crap from the cache or else... */
  963. swift_flush_cache_all();
  964. /* Enable I & D caches */
  965. mreg = srmmu_get_mmureg();
  966. mreg |= (SWIFT_IE | SWIFT_DE);
  967. /*
  968. * The Swift branch folding logic is completely broken. At
  969. * trap time, if things are just right, if can mistakenly
  970. * think that a trap is coming from kernel mode when in fact
  971. * it is coming from user mode (it mis-executes the branch in
  972. * the trap code). So you see things like crashme completely
  973. * hosing your machine which is completely unacceptable. Turn
  974. * this shit off... nice job Fujitsu.
  975. */
  976. mreg &= ~(SWIFT_BF);
  977. srmmu_set_mmureg(mreg);
  978. }
  979. static const struct sparc32_cachetlb_ops swift_ops = {
  980. .cache_all = swift_flush_cache_all,
  981. .cache_mm = swift_flush_cache_mm,
  982. .cache_page = swift_flush_cache_page,
  983. .cache_range = swift_flush_cache_range,
  984. .tlb_all = swift_flush_tlb_all,
  985. .tlb_mm = swift_flush_tlb_mm,
  986. .tlb_page = swift_flush_tlb_page,
  987. .tlb_range = swift_flush_tlb_range,
  988. .page_to_ram = swift_flush_page_to_ram,
  989. .sig_insns = swift_flush_sig_insns,
  990. .page_for_dma = swift_flush_page_for_dma,
  991. };
  992. #define SWIFT_MASKID_ADDR 0x10003018
  993. static void __init init_swift(void)
  994. {
  995. unsigned long swift_rev;
  996. __asm__ __volatile__("lda [%1] %2, %0\n\t"
  997. "srl %0, 0x18, %0\n\t" :
  998. "=r" (swift_rev) :
  999. "r" (SWIFT_MASKID_ADDR), "i" (ASI_M_BYPASS));
  1000. srmmu_name = "Fujitsu Swift";
  1001. switch (swift_rev) {
  1002. case 0x11:
  1003. case 0x20:
  1004. case 0x23:
  1005. case 0x30:
  1006. srmmu_modtype = Swift_lots_o_bugs;
  1007. hwbug_bitmask |= (HWBUG_KERN_ACCBROKEN | HWBUG_KERN_CBITBROKEN);
  1008. /*
  1009. * Gee george, I wonder why Sun is so hush hush about
  1010. * this hardware bug... really braindamage stuff going
  1011. * on here. However I think we can find a way to avoid
  1012. * all of the workaround overhead under Linux. Basically,
  1013. * any page fault can cause kernel pages to become user
  1014. * accessible (the mmu gets confused and clears some of
  1015. * the ACC bits in kernel ptes). Aha, sounds pretty
  1016. * horrible eh? But wait, after extensive testing it appears
  1017. * that if you use pgd_t level large kernel pte's (like the
  1018. * 4MB pages on the Pentium) the bug does not get tripped
  1019. * at all. This avoids almost all of the major overhead.
  1020. * Welcome to a world where your vendor tells you to,
  1021. * "apply this kernel patch" instead of "sorry for the
  1022. * broken hardware, send it back and we'll give you
  1023. * properly functioning parts"
  1024. */
  1025. break;
  1026. case 0x25:
  1027. case 0x31:
  1028. srmmu_modtype = Swift_bad_c;
  1029. hwbug_bitmask |= HWBUG_KERN_CBITBROKEN;
  1030. /*
  1031. * You see Sun allude to this hardware bug but never
  1032. * admit things directly, they'll say things like,
  1033. * "the Swift chip cache problems" or similar.
  1034. */
  1035. break;
  1036. default:
  1037. srmmu_modtype = Swift_ok;
  1038. break;
  1039. }
  1040. sparc32_cachetlb_ops = &swift_ops;
  1041. flush_page_for_dma_global = 0;
  1042. /*
  1043. * Are you now convinced that the Swift is one of the
  1044. * biggest VLSI abortions of all time? Bravo Fujitsu!
  1045. * Fujitsu, the !#?!%$'d up processor people. I bet if
  1046. * you examined the microcode of the Swift you'd find
  1047. * XXX's all over the place.
  1048. */
  1049. poke_srmmu = poke_swift;
  1050. }
  1051. static void turbosparc_flush_cache_all(void)
  1052. {
  1053. flush_user_windows();
  1054. turbosparc_idflash_clear();
  1055. }
  1056. static void turbosparc_flush_cache_mm(struct mm_struct *mm)
  1057. {
  1058. FLUSH_BEGIN(mm)
  1059. flush_user_windows();
  1060. turbosparc_idflash_clear();
  1061. FLUSH_END
  1062. }
  1063. static void turbosparc_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  1064. {
  1065. FLUSH_BEGIN(vma->vm_mm)
  1066. flush_user_windows();
  1067. turbosparc_idflash_clear();
  1068. FLUSH_END
  1069. }
  1070. static void turbosparc_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
  1071. {
  1072. FLUSH_BEGIN(vma->vm_mm)
  1073. flush_user_windows();
  1074. if (vma->vm_flags & VM_EXEC)
  1075. turbosparc_flush_icache();
  1076. turbosparc_flush_dcache();
  1077. FLUSH_END
  1078. }
  1079. /* TurboSparc is copy-back, if we turn it on, but this does not work. */
  1080. static void turbosparc_flush_page_to_ram(unsigned long page)
  1081. {
  1082. #ifdef TURBOSPARC_WRITEBACK
  1083. volatile unsigned long clear;
  1084. if (srmmu_probe(page))
  1085. turbosparc_flush_page_cache(page);
  1086. clear = srmmu_get_fstatus();
  1087. #endif
  1088. }
  1089. static void turbosparc_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
  1090. {
  1091. }
  1092. static void turbosparc_flush_page_for_dma(unsigned long page)
  1093. {
  1094. turbosparc_flush_dcache();
  1095. }
  1096. static void turbosparc_flush_tlb_all(void)
  1097. {
  1098. srmmu_flush_whole_tlb();
  1099. }
  1100. static void turbosparc_flush_tlb_mm(struct mm_struct *mm)
  1101. {
  1102. FLUSH_BEGIN(mm)
  1103. srmmu_flush_whole_tlb();
  1104. FLUSH_END
  1105. }
  1106. static void turbosparc_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  1107. {
  1108. FLUSH_BEGIN(vma->vm_mm)
  1109. srmmu_flush_whole_tlb();
  1110. FLUSH_END
  1111. }
  1112. static void turbosparc_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  1113. {
  1114. FLUSH_BEGIN(vma->vm_mm)
  1115. srmmu_flush_whole_tlb();
  1116. FLUSH_END
  1117. }
  1118. static void poke_turbosparc(void)
  1119. {
  1120. unsigned long mreg = srmmu_get_mmureg();
  1121. unsigned long ccreg;
  1122. /* Clear any crap from the cache or else... */
  1123. turbosparc_flush_cache_all();
  1124. /* Temporarily disable I & D caches */
  1125. mreg &= ~(TURBOSPARC_ICENABLE | TURBOSPARC_DCENABLE);
  1126. mreg &= ~(TURBOSPARC_PCENABLE); /* Don't check parity */
  1127. srmmu_set_mmureg(mreg);
  1128. ccreg = turbosparc_get_ccreg();
  1129. #ifdef TURBOSPARC_WRITEBACK
  1130. ccreg |= (TURBOSPARC_SNENABLE); /* Do DVMA snooping in Dcache */
  1131. ccreg &= ~(TURBOSPARC_uS2 | TURBOSPARC_WTENABLE);
  1132. /* Write-back D-cache, emulate VLSI
  1133. * abortion number three, not number one */
  1134. #else
  1135. /* For now let's play safe, optimize later */
  1136. ccreg |= (TURBOSPARC_SNENABLE | TURBOSPARC_WTENABLE);
  1137. /* Do DVMA snooping in Dcache, Write-thru D-cache */
  1138. ccreg &= ~(TURBOSPARC_uS2);
  1139. /* Emulate VLSI abortion number three, not number one */
  1140. #endif
  1141. switch (ccreg & 7) {
  1142. case 0: /* No SE cache */
  1143. case 7: /* Test mode */
  1144. break;
  1145. default:
  1146. ccreg |= (TURBOSPARC_SCENABLE);
  1147. }
  1148. turbosparc_set_ccreg(ccreg);
  1149. mreg |= (TURBOSPARC_ICENABLE | TURBOSPARC_DCENABLE); /* I & D caches on */
  1150. mreg |= (TURBOSPARC_ICSNOOP); /* Icache snooping on */
  1151. srmmu_set_mmureg(mreg);
  1152. }
  1153. static const struct sparc32_cachetlb_ops turbosparc_ops = {
  1154. .cache_all = turbosparc_flush_cache_all,
  1155. .cache_mm = turbosparc_flush_cache_mm,
  1156. .cache_page = turbosparc_flush_cache_page,
  1157. .cache_range = turbosparc_flush_cache_range,
  1158. .tlb_all = turbosparc_flush_tlb_all,
  1159. .tlb_mm = turbosparc_flush_tlb_mm,
  1160. .tlb_page = turbosparc_flush_tlb_page,
  1161. .tlb_range = turbosparc_flush_tlb_range,
  1162. .page_to_ram = turbosparc_flush_page_to_ram,
  1163. .sig_insns = turbosparc_flush_sig_insns,
  1164. .page_for_dma = turbosparc_flush_page_for_dma,
  1165. };
  1166. static void __init init_turbosparc(void)
  1167. {
  1168. srmmu_name = "Fujitsu TurboSparc";
  1169. srmmu_modtype = TurboSparc;
  1170. sparc32_cachetlb_ops = &turbosparc_ops;
  1171. poke_srmmu = poke_turbosparc;
  1172. }
  1173. static void poke_tsunami(void)
  1174. {
  1175. unsigned long mreg = srmmu_get_mmureg();
  1176. tsunami_flush_icache();
  1177. tsunami_flush_dcache();
  1178. mreg &= ~TSUNAMI_ITD;
  1179. mreg |= (TSUNAMI_IENAB | TSUNAMI_DENAB);
  1180. srmmu_set_mmureg(mreg);
  1181. }
  1182. static const struct sparc32_cachetlb_ops tsunami_ops = {
  1183. .cache_all = tsunami_flush_cache_all,
  1184. .cache_mm = tsunami_flush_cache_mm,
  1185. .cache_page = tsunami_flush_cache_page,
  1186. .cache_range = tsunami_flush_cache_range,
  1187. .tlb_all = tsunami_flush_tlb_all,
  1188. .tlb_mm = tsunami_flush_tlb_mm,
  1189. .tlb_page = tsunami_flush_tlb_page,
  1190. .tlb_range = tsunami_flush_tlb_range,
  1191. .page_to_ram = tsunami_flush_page_to_ram,
  1192. .sig_insns = tsunami_flush_sig_insns,
  1193. .page_for_dma = tsunami_flush_page_for_dma,
  1194. };
  1195. static void __init init_tsunami(void)
  1196. {
  1197. /*
  1198. * Tsunami's pretty sane, Sun and TI actually got it
  1199. * somewhat right this time. Fujitsu should have
  1200. * taken some lessons from them.
  1201. */
  1202. srmmu_name = "TI Tsunami";
  1203. srmmu_modtype = Tsunami;
  1204. sparc32_cachetlb_ops = &tsunami_ops;
  1205. poke_srmmu = poke_tsunami;
  1206. tsunami_setup_blockops();
  1207. }
  1208. static void poke_viking(void)
  1209. {
  1210. unsigned long mreg = srmmu_get_mmureg();
  1211. static int smp_catch;
  1212. if (viking_mxcc_present) {
  1213. unsigned long mxcc_control = mxcc_get_creg();
  1214. mxcc_control |= (MXCC_CTL_ECE | MXCC_CTL_PRE | MXCC_CTL_MCE);
  1215. mxcc_control &= ~(MXCC_CTL_RRC);
  1216. mxcc_set_creg(mxcc_control);
  1217. /*
  1218. * We don't need memory parity checks.
  1219. * XXX This is a mess, have to dig out later. ecd.
  1220. viking_mxcc_turn_off_parity(&mreg, &mxcc_control);
  1221. */
  1222. /* We do cache ptables on MXCC. */
  1223. mreg |= VIKING_TCENABLE;
  1224. } else {
  1225. unsigned long bpreg;
  1226. mreg &= ~(VIKING_TCENABLE);
  1227. if (smp_catch++) {
  1228. /* Must disable mixed-cmd mode here for other cpu's. */
  1229. bpreg = viking_get_bpreg();
  1230. bpreg &= ~(VIKING_ACTION_MIX);
  1231. viking_set_bpreg(bpreg);
  1232. /* Just in case PROM does something funny. */
  1233. msi_set_sync();
  1234. }
  1235. }
  1236. mreg |= VIKING_SPENABLE;
  1237. mreg |= (VIKING_ICENABLE | VIKING_DCENABLE);
  1238. mreg |= VIKING_SBENABLE;
  1239. mreg &= ~(VIKING_ACENABLE);
  1240. srmmu_set_mmureg(mreg);
  1241. }
  1242. static struct sparc32_cachetlb_ops viking_ops __ro_after_init = {
  1243. .cache_all = viking_flush_cache_all,
  1244. .cache_mm = viking_flush_cache_mm,
  1245. .cache_page = viking_flush_cache_page,
  1246. .cache_range = viking_flush_cache_range,
  1247. .tlb_all = viking_flush_tlb_all,
  1248. .tlb_mm = viking_flush_tlb_mm,
  1249. .tlb_page = viking_flush_tlb_page,
  1250. .tlb_range = viking_flush_tlb_range,
  1251. .page_to_ram = viking_flush_page_to_ram,
  1252. .sig_insns = viking_flush_sig_insns,
  1253. .page_for_dma = viking_flush_page_for_dma,
  1254. };
  1255. #ifdef CONFIG_SMP
  1256. /* On sun4d the cpu broadcasts local TLB flushes, so we can just
  1257. * perform the local TLB flush and all the other cpus will see it.
  1258. * But, unfortunately, there is a bug in the sun4d XBUS backplane
  1259. * that requires that we add some synchronization to these flushes.
  1260. *
  1261. * The bug is that the fifo which keeps track of all the pending TLB
  1262. * broadcasts in the system is an entry or two too small, so if we
  1263. * have too many going at once we'll overflow that fifo and lose a TLB
  1264. * flush resulting in corruption.
  1265. *
  1266. * Our workaround is to take a global spinlock around the TLB flushes,
  1267. * which guarentees we won't ever have too many pending. It's a big
  1268. * hammer, but a semaphore like system to make sure we only have N TLB
  1269. * flushes going at once will require SMP locking anyways so there's
  1270. * no real value in trying any harder than this.
  1271. */
  1272. static struct sparc32_cachetlb_ops viking_sun4d_smp_ops __ro_after_init = {
  1273. .cache_all = viking_flush_cache_all,
  1274. .cache_mm = viking_flush_cache_mm,
  1275. .cache_page = viking_flush_cache_page,
  1276. .cache_range = viking_flush_cache_range,
  1277. .tlb_all = sun4dsmp_flush_tlb_all,
  1278. .tlb_mm = sun4dsmp_flush_tlb_mm,
  1279. .tlb_page = sun4dsmp_flush_tlb_page,
  1280. .tlb_range = sun4dsmp_flush_tlb_range,
  1281. .page_to_ram = viking_flush_page_to_ram,
  1282. .sig_insns = viking_flush_sig_insns,
  1283. .page_for_dma = viking_flush_page_for_dma,
  1284. };
  1285. #endif
  1286. static void __init init_viking(void)
  1287. {
  1288. unsigned long mreg = srmmu_get_mmureg();
  1289. /* Ahhh, the viking. SRMMU VLSI abortion number two... */
  1290. if (mreg & VIKING_MMODE) {
  1291. srmmu_name = "TI Viking";
  1292. viking_mxcc_present = 0;
  1293. msi_set_sync();
  1294. /*
  1295. * We need this to make sure old viking takes no hits
  1296. * on it's cache for dma snoops to workaround the
  1297. * "load from non-cacheable memory" interrupt bug.
  1298. * This is only necessary because of the new way in
  1299. * which we use the IOMMU.
  1300. */
  1301. viking_ops.page_for_dma = viking_flush_page;
  1302. #ifdef CONFIG_SMP
  1303. viking_sun4d_smp_ops.page_for_dma = viking_flush_page;
  1304. #endif
  1305. flush_page_for_dma_global = 0;
  1306. } else {
  1307. srmmu_name = "TI Viking/MXCC";
  1308. viking_mxcc_present = 1;
  1309. srmmu_cache_pagetables = 1;
  1310. }
  1311. sparc32_cachetlb_ops = (const struct sparc32_cachetlb_ops *)
  1312. &viking_ops;
  1313. #ifdef CONFIG_SMP
  1314. if (sparc_cpu_model == sun4d)
  1315. sparc32_cachetlb_ops = (const struct sparc32_cachetlb_ops *)
  1316. &viking_sun4d_smp_ops;
  1317. #endif
  1318. poke_srmmu = poke_viking;
  1319. }
  1320. /* Probe for the srmmu chip version. */
  1321. static void __init get_srmmu_type(void)
  1322. {
  1323. unsigned long mreg, psr;
  1324. unsigned long mod_typ, mod_rev, psr_typ, psr_vers;
  1325. srmmu_modtype = SRMMU_INVAL_MOD;
  1326. hwbug_bitmask = 0;
  1327. mreg = srmmu_get_mmureg(); psr = get_psr();
  1328. mod_typ = (mreg & 0xf0000000) >> 28;
  1329. mod_rev = (mreg & 0x0f000000) >> 24;
  1330. psr_typ = (psr >> 28) & 0xf;
  1331. psr_vers = (psr >> 24) & 0xf;
  1332. /* First, check for sparc-leon. */
  1333. if (sparc_cpu_model == sparc_leon) {
  1334. init_leon();
  1335. return;
  1336. }
  1337. /* Second, check for HyperSparc or Cypress. */
  1338. if (mod_typ == 1) {
  1339. switch (mod_rev) {
  1340. case 7:
  1341. /* UP or MP Hypersparc */
  1342. init_hypersparc();
  1343. break;
  1344. case 0:
  1345. case 2:
  1346. case 10:
  1347. case 11:
  1348. case 12:
  1349. case 13:
  1350. case 14:
  1351. case 15:
  1352. default:
  1353. prom_printf("Sparc-Linux Cypress support does not longer exit.\n");
  1354. prom_halt();
  1355. break;
  1356. }
  1357. return;
  1358. }
  1359. /* Now Fujitsu TurboSparc. It might happen that it is
  1360. * in Swift emulation mode, so we will check later...
  1361. */
  1362. if (psr_typ == 0 && psr_vers == 5) {
  1363. init_turbosparc();
  1364. return;
  1365. }
  1366. /* Next check for Fujitsu Swift. */
  1367. if (psr_typ == 0 && psr_vers == 4) {
  1368. phandle cpunode;
  1369. char node_str[128];
  1370. /* Look if it is not a TurboSparc emulating Swift... */
  1371. cpunode = prom_getchild(prom_root_node);
  1372. while ((cpunode = prom_getsibling(cpunode)) != 0) {
  1373. prom_getstring(cpunode, "device_type", node_str, sizeof(node_str));
  1374. if (!strcmp(node_str, "cpu")) {
  1375. if (!prom_getintdefault(cpunode, "psr-implementation", 1) &&
  1376. prom_getintdefault(cpunode, "psr-version", 1) == 5) {
  1377. init_turbosparc();
  1378. return;
  1379. }
  1380. break;
  1381. }
  1382. }
  1383. init_swift();
  1384. return;
  1385. }
  1386. /* Now the Viking family of srmmu. */
  1387. if (psr_typ == 4 &&
  1388. ((psr_vers == 0) ||
  1389. ((psr_vers == 1) && (mod_typ == 0) && (mod_rev == 0)))) {
  1390. init_viking();
  1391. return;
  1392. }
  1393. /* Finally the Tsunami. */
  1394. if (psr_typ == 4 && psr_vers == 1 && (mod_typ || mod_rev)) {
  1395. init_tsunami();
  1396. return;
  1397. }
  1398. /* Oh well */
  1399. srmmu_is_bad();
  1400. }
  1401. #ifdef CONFIG_SMP
  1402. /* Local cross-calls. */
  1403. static void smp_flush_page_for_dma(unsigned long page)
  1404. {
  1405. xc1(local_ops->page_for_dma, page);
  1406. local_ops->page_for_dma(page);
  1407. }
  1408. static void smp_flush_cache_all(void)
  1409. {
  1410. xc0(local_ops->cache_all);
  1411. local_ops->cache_all();
  1412. }
  1413. static void smp_flush_tlb_all(void)
  1414. {
  1415. xc0(local_ops->tlb_all);
  1416. local_ops->tlb_all();
  1417. }
  1418. static void smp_flush_cache_mm(struct mm_struct *mm)
  1419. {
  1420. if (mm->context != NO_CONTEXT) {
  1421. cpumask_t cpu_mask;
  1422. cpumask_copy(&cpu_mask, mm_cpumask(mm));
  1423. cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
  1424. if (!cpumask_empty(&cpu_mask))
  1425. xc1(local_ops->cache_mm, (unsigned long)mm);
  1426. local_ops->cache_mm(mm);
  1427. }
  1428. }
  1429. static void smp_flush_tlb_mm(struct mm_struct *mm)
  1430. {
  1431. if (mm->context != NO_CONTEXT) {
  1432. cpumask_t cpu_mask;
  1433. cpumask_copy(&cpu_mask, mm_cpumask(mm));
  1434. cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
  1435. if (!cpumask_empty(&cpu_mask)) {
  1436. xc1(local_ops->tlb_mm, (unsigned long)mm);
  1437. if (atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
  1438. cpumask_copy(mm_cpumask(mm),
  1439. cpumask_of(smp_processor_id()));
  1440. }
  1441. local_ops->tlb_mm(mm);
  1442. }
  1443. }
  1444. static void smp_flush_cache_range(struct vm_area_struct *vma,
  1445. unsigned long start,
  1446. unsigned long end)
  1447. {
  1448. struct mm_struct *mm = vma->vm_mm;
  1449. if (mm->context != NO_CONTEXT) {
  1450. cpumask_t cpu_mask;
  1451. cpumask_copy(&cpu_mask, mm_cpumask(mm));
  1452. cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
  1453. if (!cpumask_empty(&cpu_mask))
  1454. xc3(local_ops->cache_range, (unsigned long)vma, start,
  1455. end);
  1456. local_ops->cache_range(vma, start, end);
  1457. }
  1458. }
  1459. static void smp_flush_tlb_range(struct vm_area_struct *vma,
  1460. unsigned long start,
  1461. unsigned long end)
  1462. {
  1463. struct mm_struct *mm = vma->vm_mm;
  1464. if (mm->context != NO_CONTEXT) {
  1465. cpumask_t cpu_mask;
  1466. cpumask_copy(&cpu_mask, mm_cpumask(mm));
  1467. cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
  1468. if (!cpumask_empty(&cpu_mask))
  1469. xc3(local_ops->tlb_range, (unsigned long)vma, start,
  1470. end);
  1471. local_ops->tlb_range(vma, start, end);
  1472. }
  1473. }
  1474. static void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
  1475. {
  1476. struct mm_struct *mm = vma->vm_mm;
  1477. if (mm->context != NO_CONTEXT) {
  1478. cpumask_t cpu_mask;
  1479. cpumask_copy(&cpu_mask, mm_cpumask(mm));
  1480. cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
  1481. if (!cpumask_empty(&cpu_mask))
  1482. xc2(local_ops->cache_page, (unsigned long)vma, page);
  1483. local_ops->cache_page(vma, page);
  1484. }
  1485. }
  1486. static void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  1487. {
  1488. struct mm_struct *mm = vma->vm_mm;
  1489. if (mm->context != NO_CONTEXT) {
  1490. cpumask_t cpu_mask;
  1491. cpumask_copy(&cpu_mask, mm_cpumask(mm));
  1492. cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
  1493. if (!cpumask_empty(&cpu_mask))
  1494. xc2(local_ops->tlb_page, (unsigned long)vma, page);
  1495. local_ops->tlb_page(vma, page);
  1496. }
  1497. }
  1498. static void smp_flush_page_to_ram(unsigned long page)
  1499. {
  1500. /* Current theory is that those who call this are the one's
  1501. * who have just dirtied their cache with the pages contents
  1502. * in kernel space, therefore we only run this on local cpu.
  1503. *
  1504. * XXX This experiment failed, research further... -DaveM
  1505. */
  1506. #if 1
  1507. xc1(local_ops->page_to_ram, page);
  1508. #endif
  1509. local_ops->page_to_ram(page);
  1510. }
  1511. static void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
  1512. {
  1513. cpumask_t cpu_mask;
  1514. cpumask_copy(&cpu_mask, mm_cpumask(mm));
  1515. cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
  1516. if (!cpumask_empty(&cpu_mask))
  1517. xc2(local_ops->sig_insns, (unsigned long)mm, insn_addr);
  1518. local_ops->sig_insns(mm, insn_addr);
  1519. }
  1520. static struct sparc32_cachetlb_ops smp_cachetlb_ops __ro_after_init = {
  1521. .cache_all = smp_flush_cache_all,
  1522. .cache_mm = smp_flush_cache_mm,
  1523. .cache_page = smp_flush_cache_page,
  1524. .cache_range = smp_flush_cache_range,
  1525. .tlb_all = smp_flush_tlb_all,
  1526. .tlb_mm = smp_flush_tlb_mm,
  1527. .tlb_page = smp_flush_tlb_page,
  1528. .tlb_range = smp_flush_tlb_range,
  1529. .page_to_ram = smp_flush_page_to_ram,
  1530. .sig_insns = smp_flush_sig_insns,
  1531. .page_for_dma = smp_flush_page_for_dma,
  1532. };
  1533. #endif
  1534. /* Load up routines and constants for sun4m and sun4d mmu */
  1535. void __init load_mmu(void)
  1536. {
  1537. /* Functions */
  1538. get_srmmu_type();
  1539. #ifdef CONFIG_SMP
  1540. /* El switcheroo... */
  1541. local_ops = sparc32_cachetlb_ops;
  1542. if (sparc_cpu_model == sun4d || sparc_cpu_model == sparc_leon) {
  1543. smp_cachetlb_ops.tlb_all = local_ops->tlb_all;
  1544. smp_cachetlb_ops.tlb_mm = local_ops->tlb_mm;
  1545. smp_cachetlb_ops.tlb_range = local_ops->tlb_range;
  1546. smp_cachetlb_ops.tlb_page = local_ops->tlb_page;
  1547. }
  1548. if (poke_srmmu == poke_viking) {
  1549. /* Avoid unnecessary cross calls. */
  1550. smp_cachetlb_ops.cache_all = local_ops->cache_all;
  1551. smp_cachetlb_ops.cache_mm = local_ops->cache_mm;
  1552. smp_cachetlb_ops.cache_range = local_ops->cache_range;
  1553. smp_cachetlb_ops.cache_page = local_ops->cache_page;
  1554. smp_cachetlb_ops.page_to_ram = local_ops->page_to_ram;
  1555. smp_cachetlb_ops.sig_insns = local_ops->sig_insns;
  1556. smp_cachetlb_ops.page_for_dma = local_ops->page_for_dma;
  1557. }
  1558. /* It really is const after this point. */
  1559. sparc32_cachetlb_ops = (const struct sparc32_cachetlb_ops *)
  1560. &smp_cachetlb_ops;
  1561. #endif
  1562. if (sparc_cpu_model != sun4d)
  1563. ld_mmu_iommu();
  1564. #ifdef CONFIG_SMP
  1565. if (sparc_cpu_model == sun4d)
  1566. sun4d_init_smp();
  1567. else if (sparc_cpu_model == sparc_leon)
  1568. leon_init_smp();
  1569. else
  1570. sun4m_init_smp();
  1571. #endif
  1572. }