srmmu.c 57 KB

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