srmmu.c 50 KB

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