srmmu.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  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. /*
  437. * Workaround, until we find what's going on with Swift. When low on memory,
  438. * it sometimes loops in fault/handle_mm_fault incl. flush_tlb_page to find
  439. * out it is already in page tables/ fault again on the same instruction.
  440. * I really don't understand it, have checked it and contexts
  441. * are right, flush_tlb_all is done as well, and it faults again...
  442. * Strange. -jj
  443. *
  444. * The following code is a deadwood that may be necessary when
  445. * we start to make precise page flushes again. --zaitcev
  446. */
  447. static void swift_update_mmu_cache(struct vm_area_struct * vma, unsigned long address, pte_t *ptep)
  448. {
  449. #if 0
  450. static unsigned long last;
  451. unsigned int val;
  452. /* unsigned int n; */
  453. if (address == last) {
  454. val = srmmu_hwprobe(address);
  455. if (val != 0 && pte_val(*ptep) != val) {
  456. printk("swift_update_mmu_cache: "
  457. "addr %lx put %08x probed %08x from %pf\n",
  458. address, pte_val(*ptep), val,
  459. __builtin_return_address(0));
  460. srmmu_flush_whole_tlb();
  461. }
  462. }
  463. last = address;
  464. #endif
  465. }
  466. /* swift.S */
  467. extern void swift_flush_cache_all(void);
  468. extern void swift_flush_cache_mm(struct mm_struct *mm);
  469. extern void swift_flush_cache_range(struct vm_area_struct *vma,
  470. unsigned long start, unsigned long end);
  471. extern void swift_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
  472. extern void swift_flush_page_to_ram(unsigned long page);
  473. extern void swift_flush_page_for_dma(unsigned long page);
  474. extern void swift_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr);
  475. extern void swift_flush_tlb_all(void);
  476. extern void swift_flush_tlb_mm(struct mm_struct *mm);
  477. extern void swift_flush_tlb_range(struct vm_area_struct *vma,
  478. unsigned long start, unsigned long end);
  479. extern void swift_flush_tlb_page(struct vm_area_struct *vma, unsigned long page);
  480. #if 0 /* P3: deadwood to debug precise flushes on Swift. */
  481. void swift_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  482. {
  483. int cctx, ctx1;
  484. page &= PAGE_MASK;
  485. if ((ctx1 = vma->vm_mm->context) != -1) {
  486. cctx = srmmu_get_context();
  487. /* Is context # ever different from current context? P3 */
  488. if (cctx != ctx1) {
  489. printk("flush ctx %02x curr %02x\n", ctx1, cctx);
  490. srmmu_set_context(ctx1);
  491. swift_flush_page(page);
  492. __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : :
  493. "r" (page), "i" (ASI_M_FLUSH_PROBE));
  494. srmmu_set_context(cctx);
  495. } else {
  496. /* Rm. prot. bits from virt. c. */
  497. /* swift_flush_cache_all(); */
  498. /* swift_flush_cache_page(vma, page); */
  499. swift_flush_page(page);
  500. __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : :
  501. "r" (page), "i" (ASI_M_FLUSH_PROBE));
  502. /* same as above: srmmu_flush_tlb_page() */
  503. }
  504. }
  505. }
  506. #endif
  507. /*
  508. * The following are all MBUS based SRMMU modules, and therefore could
  509. * be found in a multiprocessor configuration. On the whole, these
  510. * chips seems to be much more touchy about DVMA and page tables
  511. * with respect to cache coherency.
  512. */
  513. /* Cypress flushes. */
  514. static void cypress_flush_cache_all(void)
  515. {
  516. volatile unsigned long cypress_sucks;
  517. unsigned long faddr, tagval;
  518. flush_user_windows();
  519. for(faddr = 0; faddr < 0x10000; faddr += 0x20) {
  520. __asm__ __volatile__("lda [%1 + %2] %3, %0\n\t" :
  521. "=r" (tagval) :
  522. "r" (faddr), "r" (0x40000),
  523. "i" (ASI_M_DATAC_TAG));
  524. /* If modified and valid, kick it. */
  525. if((tagval & 0x60) == 0x60)
  526. cypress_sucks = *(unsigned long *)(0xf0020000 + faddr);
  527. }
  528. }
  529. static void cypress_flush_cache_mm(struct mm_struct *mm)
  530. {
  531. register unsigned long a, b, c, d, e, f, g;
  532. unsigned long flags, faddr;
  533. int octx;
  534. FLUSH_BEGIN(mm)
  535. flush_user_windows();
  536. local_irq_save(flags);
  537. octx = srmmu_get_context();
  538. srmmu_set_context(mm->context);
  539. a = 0x20; b = 0x40; c = 0x60;
  540. d = 0x80; e = 0xa0; f = 0xc0; g = 0xe0;
  541. faddr = (0x10000 - 0x100);
  542. goto inside;
  543. do {
  544. faddr -= 0x100;
  545. inside:
  546. __asm__ __volatile__("sta %%g0, [%0] %1\n\t"
  547. "sta %%g0, [%0 + %2] %1\n\t"
  548. "sta %%g0, [%0 + %3] %1\n\t"
  549. "sta %%g0, [%0 + %4] %1\n\t"
  550. "sta %%g0, [%0 + %5] %1\n\t"
  551. "sta %%g0, [%0 + %6] %1\n\t"
  552. "sta %%g0, [%0 + %7] %1\n\t"
  553. "sta %%g0, [%0 + %8] %1\n\t" : :
  554. "r" (faddr), "i" (ASI_M_FLUSH_CTX),
  555. "r" (a), "r" (b), "r" (c), "r" (d),
  556. "r" (e), "r" (f), "r" (g));
  557. } while(faddr);
  558. srmmu_set_context(octx);
  559. local_irq_restore(flags);
  560. FLUSH_END
  561. }
  562. static void cypress_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  563. {
  564. struct mm_struct *mm = vma->vm_mm;
  565. register unsigned long a, b, c, d, e, f, g;
  566. unsigned long flags, faddr;
  567. int octx;
  568. FLUSH_BEGIN(mm)
  569. flush_user_windows();
  570. local_irq_save(flags);
  571. octx = srmmu_get_context();
  572. srmmu_set_context(mm->context);
  573. a = 0x20; b = 0x40; c = 0x60;
  574. d = 0x80; e = 0xa0; f = 0xc0; g = 0xe0;
  575. start &= SRMMU_REAL_PMD_MASK;
  576. while(start < end) {
  577. faddr = (start + (0x10000 - 0x100));
  578. goto inside;
  579. do {
  580. faddr -= 0x100;
  581. inside:
  582. __asm__ __volatile__("sta %%g0, [%0] %1\n\t"
  583. "sta %%g0, [%0 + %2] %1\n\t"
  584. "sta %%g0, [%0 + %3] %1\n\t"
  585. "sta %%g0, [%0 + %4] %1\n\t"
  586. "sta %%g0, [%0 + %5] %1\n\t"
  587. "sta %%g0, [%0 + %6] %1\n\t"
  588. "sta %%g0, [%0 + %7] %1\n\t"
  589. "sta %%g0, [%0 + %8] %1\n\t" : :
  590. "r" (faddr),
  591. "i" (ASI_M_FLUSH_SEG),
  592. "r" (a), "r" (b), "r" (c), "r" (d),
  593. "r" (e), "r" (f), "r" (g));
  594. } while (faddr != start);
  595. start += SRMMU_REAL_PMD_SIZE;
  596. }
  597. srmmu_set_context(octx);
  598. local_irq_restore(flags);
  599. FLUSH_END
  600. }
  601. static void cypress_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
  602. {
  603. register unsigned long a, b, c, d, e, f, g;
  604. struct mm_struct *mm = vma->vm_mm;
  605. unsigned long flags, line;
  606. int octx;
  607. FLUSH_BEGIN(mm)
  608. flush_user_windows();
  609. local_irq_save(flags);
  610. octx = srmmu_get_context();
  611. srmmu_set_context(mm->context);
  612. a = 0x20; b = 0x40; c = 0x60;
  613. d = 0x80; e = 0xa0; f = 0xc0; g = 0xe0;
  614. page &= PAGE_MASK;
  615. line = (page + PAGE_SIZE) - 0x100;
  616. goto inside;
  617. do {
  618. line -= 0x100;
  619. inside:
  620. __asm__ __volatile__("sta %%g0, [%0] %1\n\t"
  621. "sta %%g0, [%0 + %2] %1\n\t"
  622. "sta %%g0, [%0 + %3] %1\n\t"
  623. "sta %%g0, [%0 + %4] %1\n\t"
  624. "sta %%g0, [%0 + %5] %1\n\t"
  625. "sta %%g0, [%0 + %6] %1\n\t"
  626. "sta %%g0, [%0 + %7] %1\n\t"
  627. "sta %%g0, [%0 + %8] %1\n\t" : :
  628. "r" (line),
  629. "i" (ASI_M_FLUSH_PAGE),
  630. "r" (a), "r" (b), "r" (c), "r" (d),
  631. "r" (e), "r" (f), "r" (g));
  632. } while(line != page);
  633. srmmu_set_context(octx);
  634. local_irq_restore(flags);
  635. FLUSH_END
  636. }
  637. /* Cypress is copy-back, at least that is how we configure it. */
  638. static void cypress_flush_page_to_ram(unsigned long page)
  639. {
  640. register unsigned long a, b, c, d, e, f, g;
  641. unsigned long line;
  642. a = 0x20; b = 0x40; c = 0x60; d = 0x80; e = 0xa0; f = 0xc0; g = 0xe0;
  643. page &= PAGE_MASK;
  644. line = (page + PAGE_SIZE) - 0x100;
  645. goto inside;
  646. do {
  647. line -= 0x100;
  648. inside:
  649. __asm__ __volatile__("sta %%g0, [%0] %1\n\t"
  650. "sta %%g0, [%0 + %2] %1\n\t"
  651. "sta %%g0, [%0 + %3] %1\n\t"
  652. "sta %%g0, [%0 + %4] %1\n\t"
  653. "sta %%g0, [%0 + %5] %1\n\t"
  654. "sta %%g0, [%0 + %6] %1\n\t"
  655. "sta %%g0, [%0 + %7] %1\n\t"
  656. "sta %%g0, [%0 + %8] %1\n\t" : :
  657. "r" (line),
  658. "i" (ASI_M_FLUSH_PAGE),
  659. "r" (a), "r" (b), "r" (c), "r" (d),
  660. "r" (e), "r" (f), "r" (g));
  661. } while(line != page);
  662. }
  663. /* Cypress is also IO cache coherent. */
  664. static void cypress_flush_page_for_dma(unsigned long page)
  665. {
  666. }
  667. /* Cypress has unified L2 VIPT, from which both instructions and data
  668. * are stored. It does not have an onboard icache of any sort, therefore
  669. * no flush is necessary.
  670. */
  671. static void cypress_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
  672. {
  673. }
  674. static void cypress_flush_tlb_all(void)
  675. {
  676. srmmu_flush_whole_tlb();
  677. }
  678. static void cypress_flush_tlb_mm(struct mm_struct *mm)
  679. {
  680. FLUSH_BEGIN(mm)
  681. __asm__ __volatile__(
  682. "lda [%0] %3, %%g5\n\t"
  683. "sta %2, [%0] %3\n\t"
  684. "sta %%g0, [%1] %4\n\t"
  685. "sta %%g5, [%0] %3\n"
  686. : /* no outputs */
  687. : "r" (SRMMU_CTX_REG), "r" (0x300), "r" (mm->context),
  688. "i" (ASI_M_MMUREGS), "i" (ASI_M_FLUSH_PROBE)
  689. : "g5");
  690. FLUSH_END
  691. }
  692. static void cypress_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  693. {
  694. struct mm_struct *mm = vma->vm_mm;
  695. unsigned long size;
  696. FLUSH_BEGIN(mm)
  697. start &= SRMMU_PGDIR_MASK;
  698. size = SRMMU_PGDIR_ALIGN(end) - start;
  699. __asm__ __volatile__(
  700. "lda [%0] %5, %%g5\n\t"
  701. "sta %1, [%0] %5\n"
  702. "1:\n\t"
  703. "subcc %3, %4, %3\n\t"
  704. "bne 1b\n\t"
  705. " sta %%g0, [%2 + %3] %6\n\t"
  706. "sta %%g5, [%0] %5\n"
  707. : /* no outputs */
  708. : "r" (SRMMU_CTX_REG), "r" (mm->context), "r" (start | 0x200),
  709. "r" (size), "r" (SRMMU_PGDIR_SIZE), "i" (ASI_M_MMUREGS),
  710. "i" (ASI_M_FLUSH_PROBE)
  711. : "g5", "cc");
  712. FLUSH_END
  713. }
  714. static void cypress_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  715. {
  716. struct mm_struct *mm = vma->vm_mm;
  717. FLUSH_BEGIN(mm)
  718. __asm__ __volatile__(
  719. "lda [%0] %3, %%g5\n\t"
  720. "sta %1, [%0] %3\n\t"
  721. "sta %%g0, [%2] %4\n\t"
  722. "sta %%g5, [%0] %3\n"
  723. : /* no outputs */
  724. : "r" (SRMMU_CTX_REG), "r" (mm->context), "r" (page & PAGE_MASK),
  725. "i" (ASI_M_MMUREGS), "i" (ASI_M_FLUSH_PROBE)
  726. : "g5");
  727. FLUSH_END
  728. }
  729. /* viking.S */
  730. extern void viking_flush_cache_all(void);
  731. extern void viking_flush_cache_mm(struct mm_struct *mm);
  732. extern void viking_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  733. unsigned long end);
  734. extern void viking_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
  735. extern void viking_flush_page_to_ram(unsigned long page);
  736. extern void viking_flush_page_for_dma(unsigned long page);
  737. extern void viking_flush_sig_insns(struct mm_struct *mm, unsigned long addr);
  738. extern void viking_flush_page(unsigned long page);
  739. extern void viking_mxcc_flush_page(unsigned long page);
  740. extern void viking_flush_tlb_all(void);
  741. extern void viking_flush_tlb_mm(struct mm_struct *mm);
  742. extern void viking_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  743. unsigned long end);
  744. extern void viking_flush_tlb_page(struct vm_area_struct *vma,
  745. unsigned long page);
  746. extern void sun4dsmp_flush_tlb_all(void);
  747. extern void sun4dsmp_flush_tlb_mm(struct mm_struct *mm);
  748. extern void sun4dsmp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  749. unsigned long end);
  750. extern void sun4dsmp_flush_tlb_page(struct vm_area_struct *vma,
  751. unsigned long page);
  752. /* hypersparc.S */
  753. extern void hypersparc_flush_cache_all(void);
  754. extern void hypersparc_flush_cache_mm(struct mm_struct *mm);
  755. extern void hypersparc_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  756. extern void hypersparc_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
  757. extern void hypersparc_flush_page_to_ram(unsigned long page);
  758. extern void hypersparc_flush_page_for_dma(unsigned long page);
  759. extern void hypersparc_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr);
  760. extern void hypersparc_flush_tlb_all(void);
  761. extern void hypersparc_flush_tlb_mm(struct mm_struct *mm);
  762. extern void hypersparc_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  763. extern void hypersparc_flush_tlb_page(struct vm_area_struct *vma, unsigned long page);
  764. extern void hypersparc_setup_blockops(void);
  765. /*
  766. * NOTE: All of this startup code assumes the low 16mb (approx.) of
  767. * kernel mappings are done with one single contiguous chunk of
  768. * ram. On small ram machines (classics mainly) we only get
  769. * around 8mb mapped for us.
  770. */
  771. static void __init early_pgtable_allocfail(char *type)
  772. {
  773. prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type);
  774. prom_halt();
  775. }
  776. static void __init srmmu_early_allocate_ptable_skeleton(unsigned long start,
  777. unsigned long end)
  778. {
  779. pgd_t *pgdp;
  780. pmd_t *pmdp;
  781. pte_t *ptep;
  782. while(start < end) {
  783. pgdp = pgd_offset_k(start);
  784. if (pgd_none(*(pgd_t *)__nocache_fix(pgdp))) {
  785. pmdp = (pmd_t *) __srmmu_get_nocache(
  786. SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE);
  787. if (pmdp == NULL)
  788. early_pgtable_allocfail("pmd");
  789. memset(__nocache_fix(pmdp), 0, SRMMU_PMD_TABLE_SIZE);
  790. pgd_set(__nocache_fix(pgdp), pmdp);
  791. }
  792. pmdp = pmd_offset(__nocache_fix(pgdp), start);
  793. if(srmmu_pmd_none(*(pmd_t *)__nocache_fix(pmdp))) {
  794. ptep = (pte_t *)__srmmu_get_nocache(PTE_SIZE, PTE_SIZE);
  795. if (ptep == NULL)
  796. early_pgtable_allocfail("pte");
  797. memset(__nocache_fix(ptep), 0, PTE_SIZE);
  798. pmd_set(__nocache_fix(pmdp), ptep);
  799. }
  800. if (start > (0xffffffffUL - PMD_SIZE))
  801. break;
  802. start = (start + PMD_SIZE) & PMD_MASK;
  803. }
  804. }
  805. static void __init srmmu_allocate_ptable_skeleton(unsigned long start,
  806. unsigned long end)
  807. {
  808. pgd_t *pgdp;
  809. pmd_t *pmdp;
  810. pte_t *ptep;
  811. while(start < end) {
  812. pgdp = pgd_offset_k(start);
  813. if (pgd_none(*pgdp)) {
  814. pmdp = (pmd_t *)__srmmu_get_nocache(SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE);
  815. if (pmdp == NULL)
  816. early_pgtable_allocfail("pmd");
  817. memset(pmdp, 0, SRMMU_PMD_TABLE_SIZE);
  818. pgd_set(pgdp, pmdp);
  819. }
  820. pmdp = pmd_offset(pgdp, start);
  821. if(srmmu_pmd_none(*pmdp)) {
  822. ptep = (pte_t *) __srmmu_get_nocache(PTE_SIZE,
  823. PTE_SIZE);
  824. if (ptep == NULL)
  825. early_pgtable_allocfail("pte");
  826. memset(ptep, 0, PTE_SIZE);
  827. pmd_set(pmdp, ptep);
  828. }
  829. if (start > (0xffffffffUL - PMD_SIZE))
  830. break;
  831. start = (start + PMD_SIZE) & PMD_MASK;
  832. }
  833. }
  834. /*
  835. * This is much cleaner than poking around physical address space
  836. * looking at the prom's page table directly which is what most
  837. * other OS's do. Yuck... this is much better.
  838. */
  839. static void __init srmmu_inherit_prom_mappings(unsigned long start,
  840. unsigned long end)
  841. {
  842. pgd_t *pgdp;
  843. pmd_t *pmdp;
  844. pte_t *ptep;
  845. int what = 0; /* 0 = normal-pte, 1 = pmd-level pte, 2 = pgd-level pte */
  846. unsigned long prompte;
  847. while(start <= end) {
  848. if (start == 0)
  849. break; /* probably wrap around */
  850. if(start == 0xfef00000)
  851. start = KADB_DEBUGGER_BEGVM;
  852. if(!(prompte = srmmu_hwprobe(start))) {
  853. start += PAGE_SIZE;
  854. continue;
  855. }
  856. /* A red snapper, see what it really is. */
  857. what = 0;
  858. if(!(start & ~(SRMMU_REAL_PMD_MASK))) {
  859. if(srmmu_hwprobe((start-PAGE_SIZE) + SRMMU_REAL_PMD_SIZE) == prompte)
  860. what = 1;
  861. }
  862. if(!(start & ~(SRMMU_PGDIR_MASK))) {
  863. if(srmmu_hwprobe((start-PAGE_SIZE) + SRMMU_PGDIR_SIZE) ==
  864. prompte)
  865. what = 2;
  866. }
  867. pgdp = pgd_offset_k(start);
  868. if(what == 2) {
  869. *(pgd_t *)__nocache_fix(pgdp) = __pgd(prompte);
  870. start += SRMMU_PGDIR_SIZE;
  871. continue;
  872. }
  873. if (pgd_none(*(pgd_t *)__nocache_fix(pgdp))) {
  874. pmdp = (pmd_t *)__srmmu_get_nocache(SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE);
  875. if (pmdp == NULL)
  876. early_pgtable_allocfail("pmd");
  877. memset(__nocache_fix(pmdp), 0, SRMMU_PMD_TABLE_SIZE);
  878. pgd_set(__nocache_fix(pgdp), pmdp);
  879. }
  880. pmdp = pmd_offset(__nocache_fix(pgdp), start);
  881. if(srmmu_pmd_none(*(pmd_t *)__nocache_fix(pmdp))) {
  882. ptep = (pte_t *) __srmmu_get_nocache(PTE_SIZE,
  883. PTE_SIZE);
  884. if (ptep == NULL)
  885. early_pgtable_allocfail("pte");
  886. memset(__nocache_fix(ptep), 0, PTE_SIZE);
  887. pmd_set(__nocache_fix(pmdp), ptep);
  888. }
  889. if(what == 1) {
  890. /*
  891. * We bend the rule where all 16 PTPs in a pmd_t point
  892. * inside the same PTE page, and we leak a perfectly
  893. * good hardware PTE piece. Alternatives seem worse.
  894. */
  895. unsigned int x; /* Index of HW PMD in soft cluster */
  896. x = (start >> PMD_SHIFT) & 15;
  897. *(unsigned long *)__nocache_fix(&pmdp->pmdv[x]) = prompte;
  898. start += SRMMU_REAL_PMD_SIZE;
  899. continue;
  900. }
  901. ptep = pte_offset_kernel(__nocache_fix(pmdp), start);
  902. *(pte_t *)__nocache_fix(ptep) = __pte(prompte);
  903. start += PAGE_SIZE;
  904. }
  905. }
  906. #define KERNEL_PTE(page_shifted) ((page_shifted)|SRMMU_CACHE|SRMMU_PRIV|SRMMU_VALID)
  907. /* Create a third-level SRMMU 16MB page mapping. */
  908. static void __init do_large_mapping(unsigned long vaddr, unsigned long phys_base)
  909. {
  910. pgd_t *pgdp = pgd_offset_k(vaddr);
  911. unsigned long big_pte;
  912. big_pte = KERNEL_PTE(phys_base >> 4);
  913. *(pgd_t *)__nocache_fix(pgdp) = __pgd(big_pte);
  914. }
  915. /* Map sp_bank entry SP_ENTRY, starting at virtual address VBASE. */
  916. static unsigned long __init map_spbank(unsigned long vbase, int sp_entry)
  917. {
  918. unsigned long pstart = (sp_banks[sp_entry].base_addr & SRMMU_PGDIR_MASK);
  919. unsigned long vstart = (vbase & SRMMU_PGDIR_MASK);
  920. unsigned long vend = SRMMU_PGDIR_ALIGN(vbase + sp_banks[sp_entry].num_bytes);
  921. /* Map "low" memory only */
  922. const unsigned long min_vaddr = PAGE_OFFSET;
  923. const unsigned long max_vaddr = PAGE_OFFSET + SRMMU_MAXMEM;
  924. if (vstart < min_vaddr || vstart >= max_vaddr)
  925. return vstart;
  926. if (vend > max_vaddr || vend < min_vaddr)
  927. vend = max_vaddr;
  928. while(vstart < vend) {
  929. do_large_mapping(vstart, pstart);
  930. vstart += SRMMU_PGDIR_SIZE; pstart += SRMMU_PGDIR_SIZE;
  931. }
  932. return vstart;
  933. }
  934. static inline void memprobe_error(char *msg)
  935. {
  936. prom_printf(msg);
  937. prom_printf("Halting now...\n");
  938. prom_halt();
  939. }
  940. static inline void map_kernel(void)
  941. {
  942. int i;
  943. if (phys_base > 0) {
  944. do_large_mapping(PAGE_OFFSET, phys_base);
  945. }
  946. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  947. map_spbank((unsigned long)__va(sp_banks[i].base_addr), i);
  948. }
  949. }
  950. /* Paging initialization on the Sparc Reference MMU. */
  951. extern void sparc_context_init(int);
  952. void (*poke_srmmu)(void) __cpuinitdata = NULL;
  953. extern unsigned long bootmem_init(unsigned long *pages_avail);
  954. void __init srmmu_paging_init(void)
  955. {
  956. int i;
  957. phandle cpunode;
  958. char node_str[128];
  959. pgd_t *pgd;
  960. pmd_t *pmd;
  961. pte_t *pte;
  962. unsigned long pages_avail;
  963. sparc_iomap.start = SUN4M_IOBASE_VADDR; /* 16MB of IOSPACE on all sun4m's. */
  964. if (sparc_cpu_model == sun4d)
  965. num_contexts = 65536; /* We know it is Viking */
  966. else {
  967. /* Find the number of contexts on the srmmu. */
  968. cpunode = prom_getchild(prom_root_node);
  969. num_contexts = 0;
  970. while(cpunode != 0) {
  971. prom_getstring(cpunode, "device_type", node_str, sizeof(node_str));
  972. if(!strcmp(node_str, "cpu")) {
  973. num_contexts = prom_getintdefault(cpunode, "mmu-nctx", 0x8);
  974. break;
  975. }
  976. cpunode = prom_getsibling(cpunode);
  977. }
  978. }
  979. if(!num_contexts) {
  980. prom_printf("Something wrong, can't find cpu node in paging_init.\n");
  981. prom_halt();
  982. }
  983. pages_avail = 0;
  984. last_valid_pfn = bootmem_init(&pages_avail);
  985. srmmu_nocache_calcsize();
  986. srmmu_nocache_init();
  987. srmmu_inherit_prom_mappings(0xfe400000,(LINUX_OPPROM_ENDVM-PAGE_SIZE));
  988. map_kernel();
  989. /* ctx table has to be physically aligned to its size */
  990. srmmu_context_table = (ctxd_t *)__srmmu_get_nocache(num_contexts*sizeof(ctxd_t), num_contexts*sizeof(ctxd_t));
  991. srmmu_ctx_table_phys = (ctxd_t *)__nocache_pa((unsigned long)srmmu_context_table);
  992. for(i = 0; i < num_contexts; i++)
  993. srmmu_ctxd_set((ctxd_t *)__nocache_fix(&srmmu_context_table[i]), srmmu_swapper_pg_dir);
  994. flush_cache_all();
  995. srmmu_set_ctable_ptr((unsigned long)srmmu_ctx_table_phys);
  996. #ifdef CONFIG_SMP
  997. /* Stop from hanging here... */
  998. local_flush_tlb_all();
  999. #else
  1000. flush_tlb_all();
  1001. #endif
  1002. poke_srmmu();
  1003. srmmu_allocate_ptable_skeleton(sparc_iomap.start, IOBASE_END);
  1004. srmmu_allocate_ptable_skeleton(DVMA_VADDR, DVMA_END);
  1005. srmmu_allocate_ptable_skeleton(
  1006. __fix_to_virt(__end_of_fixed_addresses - 1), FIXADDR_TOP);
  1007. srmmu_allocate_ptable_skeleton(PKMAP_BASE, PKMAP_END);
  1008. pgd = pgd_offset_k(PKMAP_BASE);
  1009. pmd = pmd_offset(pgd, PKMAP_BASE);
  1010. pte = pte_offset_kernel(pmd, PKMAP_BASE);
  1011. pkmap_page_table = pte;
  1012. flush_cache_all();
  1013. flush_tlb_all();
  1014. sparc_context_init(num_contexts);
  1015. kmap_init();
  1016. {
  1017. unsigned long zones_size[MAX_NR_ZONES];
  1018. unsigned long zholes_size[MAX_NR_ZONES];
  1019. unsigned long npages;
  1020. int znum;
  1021. for (znum = 0; znum < MAX_NR_ZONES; znum++)
  1022. zones_size[znum] = zholes_size[znum] = 0;
  1023. npages = max_low_pfn - pfn_base;
  1024. zones_size[ZONE_DMA] = npages;
  1025. zholes_size[ZONE_DMA] = npages - pages_avail;
  1026. npages = highend_pfn - max_low_pfn;
  1027. zones_size[ZONE_HIGHMEM] = npages;
  1028. zholes_size[ZONE_HIGHMEM] = npages - calc_highpages();
  1029. free_area_init_node(0, zones_size, pfn_base, zholes_size);
  1030. }
  1031. }
  1032. void mmu_info(struct seq_file *m)
  1033. {
  1034. seq_printf(m,
  1035. "MMU type\t: %s\n"
  1036. "contexts\t: %d\n"
  1037. "nocache total\t: %ld\n"
  1038. "nocache used\t: %d\n",
  1039. srmmu_name,
  1040. num_contexts,
  1041. srmmu_nocache_size,
  1042. srmmu_nocache_map.used << SRMMU_NOCACHE_BITMAP_SHIFT);
  1043. }
  1044. static void srmmu_update_mmu_cache(struct vm_area_struct * vma, unsigned long address, pte_t pte)
  1045. {
  1046. }
  1047. void destroy_context(struct mm_struct *mm)
  1048. {
  1049. if(mm->context != NO_CONTEXT) {
  1050. flush_cache_mm(mm);
  1051. srmmu_ctxd_set(&srmmu_context_table[mm->context], srmmu_swapper_pg_dir);
  1052. flush_tlb_mm(mm);
  1053. spin_lock(&srmmu_context_spinlock);
  1054. free_context(mm->context);
  1055. spin_unlock(&srmmu_context_spinlock);
  1056. mm->context = NO_CONTEXT;
  1057. }
  1058. }
  1059. /* Init various srmmu chip types. */
  1060. static void __init srmmu_is_bad(void)
  1061. {
  1062. prom_printf("Could not determine SRMMU chip type.\n");
  1063. prom_halt();
  1064. }
  1065. static void __init init_vac_layout(void)
  1066. {
  1067. phandle nd;
  1068. int cache_lines;
  1069. char node_str[128];
  1070. #ifdef CONFIG_SMP
  1071. int cpu = 0;
  1072. unsigned long max_size = 0;
  1073. unsigned long min_line_size = 0x10000000;
  1074. #endif
  1075. nd = prom_getchild(prom_root_node);
  1076. while((nd = prom_getsibling(nd)) != 0) {
  1077. prom_getstring(nd, "device_type", node_str, sizeof(node_str));
  1078. if(!strcmp(node_str, "cpu")) {
  1079. vac_line_size = prom_getint(nd, "cache-line-size");
  1080. if (vac_line_size == -1) {
  1081. prom_printf("can't determine cache-line-size, "
  1082. "halting.\n");
  1083. prom_halt();
  1084. }
  1085. cache_lines = prom_getint(nd, "cache-nlines");
  1086. if (cache_lines == -1) {
  1087. prom_printf("can't determine cache-nlines, halting.\n");
  1088. prom_halt();
  1089. }
  1090. vac_cache_size = cache_lines * vac_line_size;
  1091. #ifdef CONFIG_SMP
  1092. if(vac_cache_size > max_size)
  1093. max_size = vac_cache_size;
  1094. if(vac_line_size < min_line_size)
  1095. min_line_size = vac_line_size;
  1096. //FIXME: cpus not contiguous!!
  1097. cpu++;
  1098. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  1099. break;
  1100. #else
  1101. break;
  1102. #endif
  1103. }
  1104. }
  1105. if(nd == 0) {
  1106. prom_printf("No CPU nodes found, halting.\n");
  1107. prom_halt();
  1108. }
  1109. #ifdef CONFIG_SMP
  1110. vac_cache_size = max_size;
  1111. vac_line_size = min_line_size;
  1112. #endif
  1113. printk("SRMMU: Using VAC size of %d bytes, line size %d bytes.\n",
  1114. (int)vac_cache_size, (int)vac_line_size);
  1115. }
  1116. static void __cpuinit poke_hypersparc(void)
  1117. {
  1118. volatile unsigned long clear;
  1119. unsigned long mreg = srmmu_get_mmureg();
  1120. hyper_flush_unconditional_combined();
  1121. mreg &= ~(HYPERSPARC_CWENABLE);
  1122. mreg |= (HYPERSPARC_CENABLE | HYPERSPARC_WBENABLE);
  1123. mreg |= (HYPERSPARC_CMODE);
  1124. srmmu_set_mmureg(mreg);
  1125. #if 0 /* XXX I think this is bad news... -DaveM */
  1126. hyper_clear_all_tags();
  1127. #endif
  1128. put_ross_icr(HYPERSPARC_ICCR_FTD | HYPERSPARC_ICCR_ICE);
  1129. hyper_flush_whole_icache();
  1130. clear = srmmu_get_faddr();
  1131. clear = srmmu_get_fstatus();
  1132. }
  1133. static void __init init_hypersparc(void)
  1134. {
  1135. srmmu_name = "ROSS HyperSparc";
  1136. srmmu_modtype = HyperSparc;
  1137. init_vac_layout();
  1138. is_hypersparc = 1;
  1139. BTFIXUPSET_CALL(flush_cache_all, hypersparc_flush_cache_all, BTFIXUPCALL_NORM);
  1140. BTFIXUPSET_CALL(flush_cache_mm, hypersparc_flush_cache_mm, BTFIXUPCALL_NORM);
  1141. BTFIXUPSET_CALL(flush_cache_range, hypersparc_flush_cache_range, BTFIXUPCALL_NORM);
  1142. BTFIXUPSET_CALL(flush_cache_page, hypersparc_flush_cache_page, BTFIXUPCALL_NORM);
  1143. BTFIXUPSET_CALL(flush_tlb_all, hypersparc_flush_tlb_all, BTFIXUPCALL_NORM);
  1144. BTFIXUPSET_CALL(flush_tlb_mm, hypersparc_flush_tlb_mm, BTFIXUPCALL_NORM);
  1145. BTFIXUPSET_CALL(flush_tlb_range, hypersparc_flush_tlb_range, BTFIXUPCALL_NORM);
  1146. BTFIXUPSET_CALL(flush_tlb_page, hypersparc_flush_tlb_page, BTFIXUPCALL_NORM);
  1147. BTFIXUPSET_CALL(__flush_page_to_ram, hypersparc_flush_page_to_ram, BTFIXUPCALL_NORM);
  1148. BTFIXUPSET_CALL(flush_sig_insns, hypersparc_flush_sig_insns, BTFIXUPCALL_NORM);
  1149. BTFIXUPSET_CALL(flush_page_for_dma, hypersparc_flush_page_for_dma, BTFIXUPCALL_NOP);
  1150. poke_srmmu = poke_hypersparc;
  1151. hypersparc_setup_blockops();
  1152. }
  1153. static void __cpuinit poke_cypress(void)
  1154. {
  1155. unsigned long mreg = srmmu_get_mmureg();
  1156. unsigned long faddr, tagval;
  1157. volatile unsigned long cypress_sucks;
  1158. volatile unsigned long clear;
  1159. clear = srmmu_get_faddr();
  1160. clear = srmmu_get_fstatus();
  1161. if (!(mreg & CYPRESS_CENABLE)) {
  1162. for(faddr = 0x0; faddr < 0x10000; faddr += 20) {
  1163. __asm__ __volatile__("sta %%g0, [%0 + %1] %2\n\t"
  1164. "sta %%g0, [%0] %2\n\t" : :
  1165. "r" (faddr), "r" (0x40000),
  1166. "i" (ASI_M_DATAC_TAG));
  1167. }
  1168. } else {
  1169. for(faddr = 0; faddr < 0x10000; faddr += 0x20) {
  1170. __asm__ __volatile__("lda [%1 + %2] %3, %0\n\t" :
  1171. "=r" (tagval) :
  1172. "r" (faddr), "r" (0x40000),
  1173. "i" (ASI_M_DATAC_TAG));
  1174. /* If modified and valid, kick it. */
  1175. if((tagval & 0x60) == 0x60)
  1176. cypress_sucks = *(unsigned long *)
  1177. (0xf0020000 + faddr);
  1178. }
  1179. }
  1180. /* And one more, for our good neighbor, Mr. Broken Cypress. */
  1181. clear = srmmu_get_faddr();
  1182. clear = srmmu_get_fstatus();
  1183. mreg |= (CYPRESS_CENABLE | CYPRESS_CMODE);
  1184. srmmu_set_mmureg(mreg);
  1185. }
  1186. static void __init init_cypress_common(void)
  1187. {
  1188. init_vac_layout();
  1189. BTFIXUPSET_CALL(flush_cache_all, cypress_flush_cache_all, BTFIXUPCALL_NORM);
  1190. BTFIXUPSET_CALL(flush_cache_mm, cypress_flush_cache_mm, BTFIXUPCALL_NORM);
  1191. BTFIXUPSET_CALL(flush_cache_range, cypress_flush_cache_range, BTFIXUPCALL_NORM);
  1192. BTFIXUPSET_CALL(flush_cache_page, cypress_flush_cache_page, BTFIXUPCALL_NORM);
  1193. BTFIXUPSET_CALL(flush_tlb_all, cypress_flush_tlb_all, BTFIXUPCALL_NORM);
  1194. BTFIXUPSET_CALL(flush_tlb_mm, cypress_flush_tlb_mm, BTFIXUPCALL_NORM);
  1195. BTFIXUPSET_CALL(flush_tlb_page, cypress_flush_tlb_page, BTFIXUPCALL_NORM);
  1196. BTFIXUPSET_CALL(flush_tlb_range, cypress_flush_tlb_range, BTFIXUPCALL_NORM);
  1197. BTFIXUPSET_CALL(__flush_page_to_ram, cypress_flush_page_to_ram, BTFIXUPCALL_NORM);
  1198. BTFIXUPSET_CALL(flush_sig_insns, cypress_flush_sig_insns, BTFIXUPCALL_NOP);
  1199. BTFIXUPSET_CALL(flush_page_for_dma, cypress_flush_page_for_dma, BTFIXUPCALL_NOP);
  1200. poke_srmmu = poke_cypress;
  1201. }
  1202. static void __init init_cypress_604(void)
  1203. {
  1204. srmmu_name = "ROSS Cypress-604(UP)";
  1205. srmmu_modtype = Cypress;
  1206. init_cypress_common();
  1207. }
  1208. static void __init init_cypress_605(unsigned long mrev)
  1209. {
  1210. srmmu_name = "ROSS Cypress-605(MP)";
  1211. if(mrev == 0xe) {
  1212. srmmu_modtype = Cypress_vE;
  1213. hwbug_bitmask |= HWBUG_COPYBACK_BROKEN;
  1214. } else {
  1215. if(mrev == 0xd) {
  1216. srmmu_modtype = Cypress_vD;
  1217. hwbug_bitmask |= HWBUG_ASIFLUSH_BROKEN;
  1218. } else {
  1219. srmmu_modtype = Cypress;
  1220. }
  1221. }
  1222. init_cypress_common();
  1223. }
  1224. static void __cpuinit poke_swift(void)
  1225. {
  1226. unsigned long mreg;
  1227. /* Clear any crap from the cache or else... */
  1228. swift_flush_cache_all();
  1229. /* Enable I & D caches */
  1230. mreg = srmmu_get_mmureg();
  1231. mreg |= (SWIFT_IE | SWIFT_DE);
  1232. /*
  1233. * The Swift branch folding logic is completely broken. At
  1234. * trap time, if things are just right, if can mistakenly
  1235. * think that a trap is coming from kernel mode when in fact
  1236. * it is coming from user mode (it mis-executes the branch in
  1237. * the trap code). So you see things like crashme completely
  1238. * hosing your machine which is completely unacceptable. Turn
  1239. * this shit off... nice job Fujitsu.
  1240. */
  1241. mreg &= ~(SWIFT_BF);
  1242. srmmu_set_mmureg(mreg);
  1243. }
  1244. #define SWIFT_MASKID_ADDR 0x10003018
  1245. static void __init init_swift(void)
  1246. {
  1247. unsigned long swift_rev;
  1248. __asm__ __volatile__("lda [%1] %2, %0\n\t"
  1249. "srl %0, 0x18, %0\n\t" :
  1250. "=r" (swift_rev) :
  1251. "r" (SWIFT_MASKID_ADDR), "i" (ASI_M_BYPASS));
  1252. srmmu_name = "Fujitsu Swift";
  1253. switch(swift_rev) {
  1254. case 0x11:
  1255. case 0x20:
  1256. case 0x23:
  1257. case 0x30:
  1258. srmmu_modtype = Swift_lots_o_bugs;
  1259. hwbug_bitmask |= (HWBUG_KERN_ACCBROKEN | HWBUG_KERN_CBITBROKEN);
  1260. /*
  1261. * Gee george, I wonder why Sun is so hush hush about
  1262. * this hardware bug... really braindamage stuff going
  1263. * on here. However I think we can find a way to avoid
  1264. * all of the workaround overhead under Linux. Basically,
  1265. * any page fault can cause kernel pages to become user
  1266. * accessible (the mmu gets confused and clears some of
  1267. * the ACC bits in kernel ptes). Aha, sounds pretty
  1268. * horrible eh? But wait, after extensive testing it appears
  1269. * that if you use pgd_t level large kernel pte's (like the
  1270. * 4MB pages on the Pentium) the bug does not get tripped
  1271. * at all. This avoids almost all of the major overhead.
  1272. * Welcome to a world where your vendor tells you to,
  1273. * "apply this kernel patch" instead of "sorry for the
  1274. * broken hardware, send it back and we'll give you
  1275. * properly functioning parts"
  1276. */
  1277. break;
  1278. case 0x25:
  1279. case 0x31:
  1280. srmmu_modtype = Swift_bad_c;
  1281. hwbug_bitmask |= HWBUG_KERN_CBITBROKEN;
  1282. /*
  1283. * You see Sun allude to this hardware bug but never
  1284. * admit things directly, they'll say things like,
  1285. * "the Swift chip cache problems" or similar.
  1286. */
  1287. break;
  1288. default:
  1289. srmmu_modtype = Swift_ok;
  1290. break;
  1291. }
  1292. BTFIXUPSET_CALL(flush_cache_all, swift_flush_cache_all, BTFIXUPCALL_NORM);
  1293. BTFIXUPSET_CALL(flush_cache_mm, swift_flush_cache_mm, BTFIXUPCALL_NORM);
  1294. BTFIXUPSET_CALL(flush_cache_page, swift_flush_cache_page, BTFIXUPCALL_NORM);
  1295. BTFIXUPSET_CALL(flush_cache_range, swift_flush_cache_range, BTFIXUPCALL_NORM);
  1296. BTFIXUPSET_CALL(flush_tlb_all, swift_flush_tlb_all, BTFIXUPCALL_NORM);
  1297. BTFIXUPSET_CALL(flush_tlb_mm, swift_flush_tlb_mm, BTFIXUPCALL_NORM);
  1298. BTFIXUPSET_CALL(flush_tlb_page, swift_flush_tlb_page, BTFIXUPCALL_NORM);
  1299. BTFIXUPSET_CALL(flush_tlb_range, swift_flush_tlb_range, BTFIXUPCALL_NORM);
  1300. BTFIXUPSET_CALL(__flush_page_to_ram, swift_flush_page_to_ram, BTFIXUPCALL_NORM);
  1301. BTFIXUPSET_CALL(flush_sig_insns, swift_flush_sig_insns, BTFIXUPCALL_NORM);
  1302. BTFIXUPSET_CALL(flush_page_for_dma, swift_flush_page_for_dma, BTFIXUPCALL_NORM);
  1303. BTFIXUPSET_CALL(update_mmu_cache, swift_update_mmu_cache, BTFIXUPCALL_NORM);
  1304. flush_page_for_dma_global = 0;
  1305. /*
  1306. * Are you now convinced that the Swift is one of the
  1307. * biggest VLSI abortions of all time? Bravo Fujitsu!
  1308. * Fujitsu, the !#?!%$'d up processor people. I bet if
  1309. * you examined the microcode of the Swift you'd find
  1310. * XXX's all over the place.
  1311. */
  1312. poke_srmmu = poke_swift;
  1313. }
  1314. static void turbosparc_flush_cache_all(void)
  1315. {
  1316. flush_user_windows();
  1317. turbosparc_idflash_clear();
  1318. }
  1319. static void turbosparc_flush_cache_mm(struct mm_struct *mm)
  1320. {
  1321. FLUSH_BEGIN(mm)
  1322. flush_user_windows();
  1323. turbosparc_idflash_clear();
  1324. FLUSH_END
  1325. }
  1326. static void turbosparc_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  1327. {
  1328. FLUSH_BEGIN(vma->vm_mm)
  1329. flush_user_windows();
  1330. turbosparc_idflash_clear();
  1331. FLUSH_END
  1332. }
  1333. static void turbosparc_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
  1334. {
  1335. FLUSH_BEGIN(vma->vm_mm)
  1336. flush_user_windows();
  1337. if (vma->vm_flags & VM_EXEC)
  1338. turbosparc_flush_icache();
  1339. turbosparc_flush_dcache();
  1340. FLUSH_END
  1341. }
  1342. /* TurboSparc is copy-back, if we turn it on, but this does not work. */
  1343. static void turbosparc_flush_page_to_ram(unsigned long page)
  1344. {
  1345. #ifdef TURBOSPARC_WRITEBACK
  1346. volatile unsigned long clear;
  1347. if (srmmu_hwprobe(page))
  1348. turbosparc_flush_page_cache(page);
  1349. clear = srmmu_get_fstatus();
  1350. #endif
  1351. }
  1352. static void turbosparc_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
  1353. {
  1354. }
  1355. static void turbosparc_flush_page_for_dma(unsigned long page)
  1356. {
  1357. turbosparc_flush_dcache();
  1358. }
  1359. static void turbosparc_flush_tlb_all(void)
  1360. {
  1361. srmmu_flush_whole_tlb();
  1362. }
  1363. static void turbosparc_flush_tlb_mm(struct mm_struct *mm)
  1364. {
  1365. FLUSH_BEGIN(mm)
  1366. srmmu_flush_whole_tlb();
  1367. FLUSH_END
  1368. }
  1369. static void turbosparc_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  1370. {
  1371. FLUSH_BEGIN(vma->vm_mm)
  1372. srmmu_flush_whole_tlb();
  1373. FLUSH_END
  1374. }
  1375. static void turbosparc_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  1376. {
  1377. FLUSH_BEGIN(vma->vm_mm)
  1378. srmmu_flush_whole_tlb();
  1379. FLUSH_END
  1380. }
  1381. static void __cpuinit poke_turbosparc(void)
  1382. {
  1383. unsigned long mreg = srmmu_get_mmureg();
  1384. unsigned long ccreg;
  1385. /* Clear any crap from the cache or else... */
  1386. turbosparc_flush_cache_all();
  1387. mreg &= ~(TURBOSPARC_ICENABLE | TURBOSPARC_DCENABLE); /* Temporarily disable I & D caches */
  1388. mreg &= ~(TURBOSPARC_PCENABLE); /* Don't check parity */
  1389. srmmu_set_mmureg(mreg);
  1390. ccreg = turbosparc_get_ccreg();
  1391. #ifdef TURBOSPARC_WRITEBACK
  1392. ccreg |= (TURBOSPARC_SNENABLE); /* Do DVMA snooping in Dcache */
  1393. ccreg &= ~(TURBOSPARC_uS2 | TURBOSPARC_WTENABLE);
  1394. /* Write-back D-cache, emulate VLSI
  1395. * abortion number three, not number one */
  1396. #else
  1397. /* For now let's play safe, optimize later */
  1398. ccreg |= (TURBOSPARC_SNENABLE | TURBOSPARC_WTENABLE);
  1399. /* Do DVMA snooping in Dcache, Write-thru D-cache */
  1400. ccreg &= ~(TURBOSPARC_uS2);
  1401. /* Emulate VLSI abortion number three, not number one */
  1402. #endif
  1403. switch (ccreg & 7) {
  1404. case 0: /* No SE cache */
  1405. case 7: /* Test mode */
  1406. break;
  1407. default:
  1408. ccreg |= (TURBOSPARC_SCENABLE);
  1409. }
  1410. turbosparc_set_ccreg (ccreg);
  1411. mreg |= (TURBOSPARC_ICENABLE | TURBOSPARC_DCENABLE); /* I & D caches on */
  1412. mreg |= (TURBOSPARC_ICSNOOP); /* Icache snooping on */
  1413. srmmu_set_mmureg(mreg);
  1414. }
  1415. static void __init init_turbosparc(void)
  1416. {
  1417. srmmu_name = "Fujitsu TurboSparc";
  1418. srmmu_modtype = TurboSparc;
  1419. BTFIXUPSET_CALL(flush_cache_all, turbosparc_flush_cache_all, BTFIXUPCALL_NORM);
  1420. BTFIXUPSET_CALL(flush_cache_mm, turbosparc_flush_cache_mm, BTFIXUPCALL_NORM);
  1421. BTFIXUPSET_CALL(flush_cache_page, turbosparc_flush_cache_page, BTFIXUPCALL_NORM);
  1422. BTFIXUPSET_CALL(flush_cache_range, turbosparc_flush_cache_range, BTFIXUPCALL_NORM);
  1423. BTFIXUPSET_CALL(flush_tlb_all, turbosparc_flush_tlb_all, BTFIXUPCALL_NORM);
  1424. BTFIXUPSET_CALL(flush_tlb_mm, turbosparc_flush_tlb_mm, BTFIXUPCALL_NORM);
  1425. BTFIXUPSET_CALL(flush_tlb_page, turbosparc_flush_tlb_page, BTFIXUPCALL_NORM);
  1426. BTFIXUPSET_CALL(flush_tlb_range, turbosparc_flush_tlb_range, BTFIXUPCALL_NORM);
  1427. BTFIXUPSET_CALL(__flush_page_to_ram, turbosparc_flush_page_to_ram, BTFIXUPCALL_NORM);
  1428. BTFIXUPSET_CALL(flush_sig_insns, turbosparc_flush_sig_insns, BTFIXUPCALL_NOP);
  1429. BTFIXUPSET_CALL(flush_page_for_dma, turbosparc_flush_page_for_dma, BTFIXUPCALL_NORM);
  1430. poke_srmmu = poke_turbosparc;
  1431. }
  1432. static void __cpuinit poke_tsunami(void)
  1433. {
  1434. unsigned long mreg = srmmu_get_mmureg();
  1435. tsunami_flush_icache();
  1436. tsunami_flush_dcache();
  1437. mreg &= ~TSUNAMI_ITD;
  1438. mreg |= (TSUNAMI_IENAB | TSUNAMI_DENAB);
  1439. srmmu_set_mmureg(mreg);
  1440. }
  1441. static void __init init_tsunami(void)
  1442. {
  1443. /*
  1444. * Tsunami's pretty sane, Sun and TI actually got it
  1445. * somewhat right this time. Fujitsu should have
  1446. * taken some lessons from them.
  1447. */
  1448. srmmu_name = "TI Tsunami";
  1449. srmmu_modtype = Tsunami;
  1450. BTFIXUPSET_CALL(flush_cache_all, tsunami_flush_cache_all, BTFIXUPCALL_NORM);
  1451. BTFIXUPSET_CALL(flush_cache_mm, tsunami_flush_cache_mm, BTFIXUPCALL_NORM);
  1452. BTFIXUPSET_CALL(flush_cache_page, tsunami_flush_cache_page, BTFIXUPCALL_NORM);
  1453. BTFIXUPSET_CALL(flush_cache_range, tsunami_flush_cache_range, BTFIXUPCALL_NORM);
  1454. BTFIXUPSET_CALL(flush_tlb_all, tsunami_flush_tlb_all, BTFIXUPCALL_NORM);
  1455. BTFIXUPSET_CALL(flush_tlb_mm, tsunami_flush_tlb_mm, BTFIXUPCALL_NORM);
  1456. BTFIXUPSET_CALL(flush_tlb_page, tsunami_flush_tlb_page, BTFIXUPCALL_NORM);
  1457. BTFIXUPSET_CALL(flush_tlb_range, tsunami_flush_tlb_range, BTFIXUPCALL_NORM);
  1458. BTFIXUPSET_CALL(__flush_page_to_ram, tsunami_flush_page_to_ram, BTFIXUPCALL_NOP);
  1459. BTFIXUPSET_CALL(flush_sig_insns, tsunami_flush_sig_insns, BTFIXUPCALL_NORM);
  1460. BTFIXUPSET_CALL(flush_page_for_dma, tsunami_flush_page_for_dma, BTFIXUPCALL_NORM);
  1461. poke_srmmu = poke_tsunami;
  1462. tsunami_setup_blockops();
  1463. }
  1464. static void __cpuinit poke_viking(void)
  1465. {
  1466. unsigned long mreg = srmmu_get_mmureg();
  1467. static int smp_catch;
  1468. if(viking_mxcc_present) {
  1469. unsigned long mxcc_control = mxcc_get_creg();
  1470. mxcc_control |= (MXCC_CTL_ECE | MXCC_CTL_PRE | MXCC_CTL_MCE);
  1471. mxcc_control &= ~(MXCC_CTL_RRC);
  1472. mxcc_set_creg(mxcc_control);
  1473. /*
  1474. * We don't need memory parity checks.
  1475. * XXX This is a mess, have to dig out later. ecd.
  1476. viking_mxcc_turn_off_parity(&mreg, &mxcc_control);
  1477. */
  1478. /* We do cache ptables on MXCC. */
  1479. mreg |= VIKING_TCENABLE;
  1480. } else {
  1481. unsigned long bpreg;
  1482. mreg &= ~(VIKING_TCENABLE);
  1483. if(smp_catch++) {
  1484. /* Must disable mixed-cmd mode here for other cpu's. */
  1485. bpreg = viking_get_bpreg();
  1486. bpreg &= ~(VIKING_ACTION_MIX);
  1487. viking_set_bpreg(bpreg);
  1488. /* Just in case PROM does something funny. */
  1489. msi_set_sync();
  1490. }
  1491. }
  1492. mreg |= VIKING_SPENABLE;
  1493. mreg |= (VIKING_ICENABLE | VIKING_DCENABLE);
  1494. mreg |= VIKING_SBENABLE;
  1495. mreg &= ~(VIKING_ACENABLE);
  1496. srmmu_set_mmureg(mreg);
  1497. }
  1498. static void __init init_viking(void)
  1499. {
  1500. unsigned long mreg = srmmu_get_mmureg();
  1501. /* Ahhh, the viking. SRMMU VLSI abortion number two... */
  1502. if(mreg & VIKING_MMODE) {
  1503. srmmu_name = "TI Viking";
  1504. viking_mxcc_present = 0;
  1505. msi_set_sync();
  1506. /*
  1507. * We need this to make sure old viking takes no hits
  1508. * on it's cache for dma snoops to workaround the
  1509. * "load from non-cacheable memory" interrupt bug.
  1510. * This is only necessary because of the new way in
  1511. * which we use the IOMMU.
  1512. */
  1513. BTFIXUPSET_CALL(flush_page_for_dma, viking_flush_page, BTFIXUPCALL_NORM);
  1514. flush_page_for_dma_global = 0;
  1515. } else {
  1516. srmmu_name = "TI Viking/MXCC";
  1517. viking_mxcc_present = 1;
  1518. srmmu_cache_pagetables = 1;
  1519. /* MXCC vikings lack the DMA snooping bug. */
  1520. BTFIXUPSET_CALL(flush_page_for_dma, viking_flush_page_for_dma, BTFIXUPCALL_NOP);
  1521. }
  1522. BTFIXUPSET_CALL(flush_cache_all, viking_flush_cache_all, BTFIXUPCALL_NORM);
  1523. BTFIXUPSET_CALL(flush_cache_mm, viking_flush_cache_mm, BTFIXUPCALL_NORM);
  1524. BTFIXUPSET_CALL(flush_cache_page, viking_flush_cache_page, BTFIXUPCALL_NORM);
  1525. BTFIXUPSET_CALL(flush_cache_range, viking_flush_cache_range, BTFIXUPCALL_NORM);
  1526. #ifdef CONFIG_SMP
  1527. if (sparc_cpu_model == sun4d) {
  1528. BTFIXUPSET_CALL(flush_tlb_all, sun4dsmp_flush_tlb_all, BTFIXUPCALL_NORM);
  1529. BTFIXUPSET_CALL(flush_tlb_mm, sun4dsmp_flush_tlb_mm, BTFIXUPCALL_NORM);
  1530. BTFIXUPSET_CALL(flush_tlb_page, sun4dsmp_flush_tlb_page, BTFIXUPCALL_NORM);
  1531. BTFIXUPSET_CALL(flush_tlb_range, sun4dsmp_flush_tlb_range, BTFIXUPCALL_NORM);
  1532. } else
  1533. #endif
  1534. {
  1535. BTFIXUPSET_CALL(flush_tlb_all, viking_flush_tlb_all, BTFIXUPCALL_NORM);
  1536. BTFIXUPSET_CALL(flush_tlb_mm, viking_flush_tlb_mm, BTFIXUPCALL_NORM);
  1537. BTFIXUPSET_CALL(flush_tlb_page, viking_flush_tlb_page, BTFIXUPCALL_NORM);
  1538. BTFIXUPSET_CALL(flush_tlb_range, viking_flush_tlb_range, BTFIXUPCALL_NORM);
  1539. }
  1540. BTFIXUPSET_CALL(__flush_page_to_ram, viking_flush_page_to_ram, BTFIXUPCALL_NOP);
  1541. BTFIXUPSET_CALL(flush_sig_insns, viking_flush_sig_insns, BTFIXUPCALL_NOP);
  1542. poke_srmmu = poke_viking;
  1543. }
  1544. #ifdef CONFIG_SPARC_LEON
  1545. void __init poke_leonsparc(void)
  1546. {
  1547. }
  1548. void __init init_leon(void)
  1549. {
  1550. srmmu_name = "LEON";
  1551. BTFIXUPSET_CALL(flush_cache_all, leon_flush_cache_all,
  1552. BTFIXUPCALL_NORM);
  1553. BTFIXUPSET_CALL(flush_cache_mm, leon_flush_cache_all,
  1554. BTFIXUPCALL_NORM);
  1555. BTFIXUPSET_CALL(flush_cache_page, leon_flush_pcache_all,
  1556. BTFIXUPCALL_NORM);
  1557. BTFIXUPSET_CALL(flush_cache_range, leon_flush_cache_all,
  1558. BTFIXUPCALL_NORM);
  1559. BTFIXUPSET_CALL(flush_page_for_dma, leon_flush_dcache_all,
  1560. BTFIXUPCALL_NORM);
  1561. BTFIXUPSET_CALL(flush_tlb_all, leon_flush_tlb_all, BTFIXUPCALL_NORM);
  1562. BTFIXUPSET_CALL(flush_tlb_mm, leon_flush_tlb_all, BTFIXUPCALL_NORM);
  1563. BTFIXUPSET_CALL(flush_tlb_page, leon_flush_tlb_all, BTFIXUPCALL_NORM);
  1564. BTFIXUPSET_CALL(flush_tlb_range, leon_flush_tlb_all, BTFIXUPCALL_NORM);
  1565. BTFIXUPSET_CALL(__flush_page_to_ram, leon_flush_cache_all,
  1566. BTFIXUPCALL_NOP);
  1567. BTFIXUPSET_CALL(flush_sig_insns, leon_flush_cache_all, BTFIXUPCALL_NOP);
  1568. poke_srmmu = poke_leonsparc;
  1569. srmmu_cache_pagetables = 0;
  1570. leon_flush_during_switch = leon_flush_needed();
  1571. }
  1572. #endif
  1573. /* Probe for the srmmu chip version. */
  1574. static void __init get_srmmu_type(void)
  1575. {
  1576. unsigned long mreg, psr;
  1577. unsigned long mod_typ, mod_rev, psr_typ, psr_vers;
  1578. srmmu_modtype = SRMMU_INVAL_MOD;
  1579. hwbug_bitmask = 0;
  1580. mreg = srmmu_get_mmureg(); psr = get_psr();
  1581. mod_typ = (mreg & 0xf0000000) >> 28;
  1582. mod_rev = (mreg & 0x0f000000) >> 24;
  1583. psr_typ = (psr >> 28) & 0xf;
  1584. psr_vers = (psr >> 24) & 0xf;
  1585. /* First, check for sparc-leon. */
  1586. if (sparc_cpu_model == sparc_leon) {
  1587. init_leon();
  1588. return;
  1589. }
  1590. /* Second, check for HyperSparc or Cypress. */
  1591. if(mod_typ == 1) {
  1592. switch(mod_rev) {
  1593. case 7:
  1594. /* UP or MP Hypersparc */
  1595. init_hypersparc();
  1596. break;
  1597. case 0:
  1598. case 2:
  1599. /* Uniprocessor Cypress */
  1600. init_cypress_604();
  1601. break;
  1602. case 10:
  1603. case 11:
  1604. case 12:
  1605. /* _REALLY OLD_ Cypress MP chips... */
  1606. case 13:
  1607. case 14:
  1608. case 15:
  1609. /* MP Cypress mmu/cache-controller */
  1610. init_cypress_605(mod_rev);
  1611. break;
  1612. default:
  1613. /* Some other Cypress revision, assume a 605. */
  1614. init_cypress_605(mod_rev);
  1615. break;
  1616. }
  1617. return;
  1618. }
  1619. /*
  1620. * Now Fujitsu TurboSparc. It might happen that it is
  1621. * in Swift emulation mode, so we will check later...
  1622. */
  1623. if (psr_typ == 0 && psr_vers == 5) {
  1624. init_turbosparc();
  1625. return;
  1626. }
  1627. /* Next check for Fujitsu Swift. */
  1628. if(psr_typ == 0 && psr_vers == 4) {
  1629. phandle cpunode;
  1630. char node_str[128];
  1631. /* Look if it is not a TurboSparc emulating Swift... */
  1632. cpunode = prom_getchild(prom_root_node);
  1633. while((cpunode = prom_getsibling(cpunode)) != 0) {
  1634. prom_getstring(cpunode, "device_type", node_str, sizeof(node_str));
  1635. if(!strcmp(node_str, "cpu")) {
  1636. if (!prom_getintdefault(cpunode, "psr-implementation", 1) &&
  1637. prom_getintdefault(cpunode, "psr-version", 1) == 5) {
  1638. init_turbosparc();
  1639. return;
  1640. }
  1641. break;
  1642. }
  1643. }
  1644. init_swift();
  1645. return;
  1646. }
  1647. /* Now the Viking family of srmmu. */
  1648. if(psr_typ == 4 &&
  1649. ((psr_vers == 0) ||
  1650. ((psr_vers == 1) && (mod_typ == 0) && (mod_rev == 0)))) {
  1651. init_viking();
  1652. return;
  1653. }
  1654. /* Finally the Tsunami. */
  1655. if(psr_typ == 4 && psr_vers == 1 && (mod_typ || mod_rev)) {
  1656. init_tsunami();
  1657. return;
  1658. }
  1659. /* Oh well */
  1660. srmmu_is_bad();
  1661. }
  1662. extern unsigned long spwin_mmu_patchme, fwin_mmu_patchme,
  1663. tsetup_mmu_patchme, rtrap_mmu_patchme;
  1664. extern unsigned long spwin_srmmu_stackchk, srmmu_fwin_stackchk,
  1665. tsetup_srmmu_stackchk, srmmu_rett_stackchk;
  1666. #ifdef CONFIG_SMP
  1667. /* Local cross-calls. */
  1668. static void smp_flush_page_for_dma(unsigned long page)
  1669. {
  1670. xc1((smpfunc_t) BTFIXUP_CALL(local_flush_page_for_dma), page);
  1671. local_flush_page_for_dma(page);
  1672. }
  1673. #endif
  1674. /* Load up routines and constants for sun4m and sun4d mmu */
  1675. void __init load_mmu(void)
  1676. {
  1677. extern void ld_mmu_iommu(void);
  1678. extern void ld_mmu_iounit(void);
  1679. extern void ___xchg32_sun4md(void);
  1680. /* Functions */
  1681. #ifndef CONFIG_SMP
  1682. BTFIXUPSET_CALL(___xchg32, ___xchg32_sun4md, BTFIXUPCALL_SWAPG1G2);
  1683. #endif
  1684. BTFIXUPSET_CALL(update_mmu_cache, srmmu_update_mmu_cache, BTFIXUPCALL_NOP);
  1685. get_srmmu_type();
  1686. #ifdef CONFIG_SMP
  1687. /* El switcheroo... */
  1688. BTFIXUPCOPY_CALL(local_flush_cache_all, flush_cache_all);
  1689. BTFIXUPCOPY_CALL(local_flush_cache_mm, flush_cache_mm);
  1690. BTFIXUPCOPY_CALL(local_flush_cache_range, flush_cache_range);
  1691. BTFIXUPCOPY_CALL(local_flush_cache_page, flush_cache_page);
  1692. BTFIXUPCOPY_CALL(local_flush_tlb_all, flush_tlb_all);
  1693. BTFIXUPCOPY_CALL(local_flush_tlb_mm, flush_tlb_mm);
  1694. BTFIXUPCOPY_CALL(local_flush_tlb_range, flush_tlb_range);
  1695. BTFIXUPCOPY_CALL(local_flush_tlb_page, flush_tlb_page);
  1696. BTFIXUPCOPY_CALL(local_flush_page_to_ram, __flush_page_to_ram);
  1697. BTFIXUPCOPY_CALL(local_flush_sig_insns, flush_sig_insns);
  1698. BTFIXUPCOPY_CALL(local_flush_page_for_dma, flush_page_for_dma);
  1699. BTFIXUPSET_CALL(flush_cache_all, smp_flush_cache_all, BTFIXUPCALL_NORM);
  1700. BTFIXUPSET_CALL(flush_cache_mm, smp_flush_cache_mm, BTFIXUPCALL_NORM);
  1701. BTFIXUPSET_CALL(flush_cache_range, smp_flush_cache_range, BTFIXUPCALL_NORM);
  1702. BTFIXUPSET_CALL(flush_cache_page, smp_flush_cache_page, BTFIXUPCALL_NORM);
  1703. if (sparc_cpu_model != sun4d &&
  1704. sparc_cpu_model != sparc_leon) {
  1705. BTFIXUPSET_CALL(flush_tlb_all, smp_flush_tlb_all, BTFIXUPCALL_NORM);
  1706. BTFIXUPSET_CALL(flush_tlb_mm, smp_flush_tlb_mm, BTFIXUPCALL_NORM);
  1707. BTFIXUPSET_CALL(flush_tlb_range, smp_flush_tlb_range, BTFIXUPCALL_NORM);
  1708. BTFIXUPSET_CALL(flush_tlb_page, smp_flush_tlb_page, BTFIXUPCALL_NORM);
  1709. }
  1710. BTFIXUPSET_CALL(__flush_page_to_ram, smp_flush_page_to_ram, BTFIXUPCALL_NORM);
  1711. BTFIXUPSET_CALL(flush_sig_insns, smp_flush_sig_insns, BTFIXUPCALL_NORM);
  1712. BTFIXUPSET_CALL(flush_page_for_dma, smp_flush_page_for_dma, BTFIXUPCALL_NORM);
  1713. if (poke_srmmu == poke_viking) {
  1714. /* Avoid unnecessary cross calls. */
  1715. BTFIXUPCOPY_CALL(flush_cache_all, local_flush_cache_all);
  1716. BTFIXUPCOPY_CALL(flush_cache_mm, local_flush_cache_mm);
  1717. BTFIXUPCOPY_CALL(flush_cache_range, local_flush_cache_range);
  1718. BTFIXUPCOPY_CALL(flush_cache_page, local_flush_cache_page);
  1719. BTFIXUPCOPY_CALL(__flush_page_to_ram, local_flush_page_to_ram);
  1720. BTFIXUPCOPY_CALL(flush_sig_insns, local_flush_sig_insns);
  1721. BTFIXUPCOPY_CALL(flush_page_for_dma, local_flush_page_for_dma);
  1722. }
  1723. #endif
  1724. if (sparc_cpu_model == sun4d)
  1725. ld_mmu_iounit();
  1726. else
  1727. ld_mmu_iommu();
  1728. #ifdef CONFIG_SMP
  1729. if (sparc_cpu_model == sun4d)
  1730. sun4d_init_smp();
  1731. else if (sparc_cpu_model == sparc_leon)
  1732. leon_init_smp();
  1733. else
  1734. sun4m_init_smp();
  1735. #endif
  1736. btfixup();
  1737. }