srmmu.c 49 KB

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