srmmu.c 59 KB

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