srmmu.c 49 KB

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