srmmu.c 49 KB

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