init.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. /* $Id: init.c,v 1.209 2002/02/09 19:49:31 davem Exp $
  2. * arch/sparc64/mm/init.c
  3. *
  4. * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. */
  7. #include <linux/config.h>
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/string.h>
  12. #include <linux/init.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/mm.h>
  15. #include <linux/hugetlb.h>
  16. #include <linux/slab.h>
  17. #include <linux/initrd.h>
  18. #include <linux/swap.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/cache.h>
  24. #include <linux/sort.h>
  25. #include <asm/head.h>
  26. #include <asm/system.h>
  27. #include <asm/page.h>
  28. #include <asm/pgalloc.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/oplib.h>
  31. #include <asm/iommu.h>
  32. #include <asm/io.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/dma.h>
  37. #include <asm/starfire.h>
  38. #include <asm/tlb.h>
  39. #include <asm/spitfire.h>
  40. #include <asm/sections.h>
  41. #include <asm/tsb.h>
  42. #include <asm/hypervisor.h>
  43. extern void device_scan(void);
  44. #define MAX_PHYS_ADDRESS (1UL << 42UL)
  45. #define KPTE_BITMAP_CHUNK_SZ (256UL * 1024UL * 1024UL)
  46. #define KPTE_BITMAP_BYTES \
  47. ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
  48. unsigned long kern_linear_pte_xor[2] __read_mostly;
  49. /* A bitmap, one bit for every 256MB of physical memory. If the bit
  50. * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
  51. * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
  52. */
  53. unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
  54. /* A special kernel TSB for 4MB and 256MB linear mappings. */
  55. struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
  56. #define MAX_BANKS 32
  57. static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
  58. static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
  59. static int pavail_ents __initdata;
  60. static int pavail_rescan_ents __initdata;
  61. static int cmp_p64(const void *a, const void *b)
  62. {
  63. const struct linux_prom64_registers *x = a, *y = b;
  64. if (x->phys_addr > y->phys_addr)
  65. return 1;
  66. if (x->phys_addr < y->phys_addr)
  67. return -1;
  68. return 0;
  69. }
  70. static void __init read_obp_memory(const char *property,
  71. struct linux_prom64_registers *regs,
  72. int *num_ents)
  73. {
  74. int node = prom_finddevice("/memory");
  75. int prop_size = prom_getproplen(node, property);
  76. int ents, ret, i;
  77. ents = prop_size / sizeof(struct linux_prom64_registers);
  78. if (ents > MAX_BANKS) {
  79. prom_printf("The machine has more %s property entries than "
  80. "this kernel can support (%d).\n",
  81. property, MAX_BANKS);
  82. prom_halt();
  83. }
  84. ret = prom_getproperty(node, property, (char *) regs, prop_size);
  85. if (ret == -1) {
  86. prom_printf("Couldn't get %s property from /memory.\n");
  87. prom_halt();
  88. }
  89. *num_ents = ents;
  90. /* Sanitize what we got from the firmware, by page aligning
  91. * everything.
  92. */
  93. for (i = 0; i < ents; i++) {
  94. unsigned long base, size;
  95. base = regs[i].phys_addr;
  96. size = regs[i].reg_size;
  97. size &= PAGE_MASK;
  98. if (base & ~PAGE_MASK) {
  99. unsigned long new_base = PAGE_ALIGN(base);
  100. size -= new_base - base;
  101. if ((long) size < 0L)
  102. size = 0UL;
  103. base = new_base;
  104. }
  105. regs[i].phys_addr = base;
  106. regs[i].reg_size = size;
  107. }
  108. sort(regs, ents, sizeof(struct linux_prom64_registers),
  109. cmp_p64, NULL);
  110. }
  111. unsigned long *sparc64_valid_addr_bitmap __read_mostly;
  112. /* Kernel physical address base and size in bytes. */
  113. unsigned long kern_base __read_mostly;
  114. unsigned long kern_size __read_mostly;
  115. /* get_new_mmu_context() uses "cache + 1". */
  116. DEFINE_SPINLOCK(ctx_alloc_lock);
  117. unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
  118. #define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
  119. unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
  120. /* References to special section boundaries */
  121. extern char _start[], _end[];
  122. /* Initial ramdisk setup */
  123. extern unsigned long sparc_ramdisk_image64;
  124. extern unsigned int sparc_ramdisk_image;
  125. extern unsigned int sparc_ramdisk_size;
  126. struct page *mem_map_zero __read_mostly;
  127. unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
  128. unsigned long sparc64_kern_pri_context __read_mostly;
  129. unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
  130. unsigned long sparc64_kern_sec_context __read_mostly;
  131. int bigkernel = 0;
  132. kmem_cache_t *pgtable_cache __read_mostly;
  133. static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
  134. {
  135. clear_page(addr);
  136. }
  137. extern void tsb_cache_init(void);
  138. void pgtable_cache_init(void)
  139. {
  140. pgtable_cache = kmem_cache_create("pgtable_cache",
  141. PAGE_SIZE, PAGE_SIZE,
  142. SLAB_HWCACHE_ALIGN |
  143. SLAB_MUST_HWCACHE_ALIGN,
  144. zero_ctor,
  145. NULL);
  146. if (!pgtable_cache) {
  147. prom_printf("Could not create pgtable_cache\n");
  148. prom_halt();
  149. }
  150. tsb_cache_init();
  151. }
  152. #ifdef CONFIG_DEBUG_DCFLUSH
  153. atomic_t dcpage_flushes = ATOMIC_INIT(0);
  154. #ifdef CONFIG_SMP
  155. atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
  156. #endif
  157. #endif
  158. inline void flush_dcache_page_impl(struct page *page)
  159. {
  160. BUG_ON(tlb_type == hypervisor);
  161. #ifdef CONFIG_DEBUG_DCFLUSH
  162. atomic_inc(&dcpage_flushes);
  163. #endif
  164. #ifdef DCACHE_ALIASING_POSSIBLE
  165. __flush_dcache_page(page_address(page),
  166. ((tlb_type == spitfire) &&
  167. page_mapping(page) != NULL));
  168. #else
  169. if (page_mapping(page) != NULL &&
  170. tlb_type == spitfire)
  171. __flush_icache_page(__pa(page_address(page)));
  172. #endif
  173. }
  174. #define PG_dcache_dirty PG_arch_1
  175. #define PG_dcache_cpu_shift 24UL
  176. #define PG_dcache_cpu_mask (256UL - 1UL)
  177. #if NR_CPUS > 256
  178. #error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
  179. #endif
  180. #define dcache_dirty_cpu(page) \
  181. (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
  182. static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
  183. {
  184. unsigned long mask = this_cpu;
  185. unsigned long non_cpu_bits;
  186. non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
  187. mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
  188. __asm__ __volatile__("1:\n\t"
  189. "ldx [%2], %%g7\n\t"
  190. "and %%g7, %1, %%g1\n\t"
  191. "or %%g1, %0, %%g1\n\t"
  192. "casx [%2], %%g7, %%g1\n\t"
  193. "cmp %%g7, %%g1\n\t"
  194. "membar #StoreLoad | #StoreStore\n\t"
  195. "bne,pn %%xcc, 1b\n\t"
  196. " nop"
  197. : /* no outputs */
  198. : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
  199. : "g1", "g7");
  200. }
  201. static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
  202. {
  203. unsigned long mask = (1UL << PG_dcache_dirty);
  204. __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
  205. "1:\n\t"
  206. "ldx [%2], %%g7\n\t"
  207. "srlx %%g7, %4, %%g1\n\t"
  208. "and %%g1, %3, %%g1\n\t"
  209. "cmp %%g1, %0\n\t"
  210. "bne,pn %%icc, 2f\n\t"
  211. " andn %%g7, %1, %%g1\n\t"
  212. "casx [%2], %%g7, %%g1\n\t"
  213. "cmp %%g7, %%g1\n\t"
  214. "membar #StoreLoad | #StoreStore\n\t"
  215. "bne,pn %%xcc, 1b\n\t"
  216. " nop\n"
  217. "2:"
  218. : /* no outputs */
  219. : "r" (cpu), "r" (mask), "r" (&page->flags),
  220. "i" (PG_dcache_cpu_mask),
  221. "i" (PG_dcache_cpu_shift)
  222. : "g1", "g7");
  223. }
  224. static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
  225. {
  226. unsigned long tsb_addr = (unsigned long) ent;
  227. if (tlb_type == cheetah_plus || tlb_type == hypervisor)
  228. tsb_addr = __pa(tsb_addr);
  229. __tsb_insert(tsb_addr, tag, pte);
  230. }
  231. unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
  232. unsigned long _PAGE_SZBITS __read_mostly;
  233. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  234. {
  235. struct mm_struct *mm;
  236. struct tsb *tsb;
  237. unsigned long tag, flags;
  238. if (tlb_type != hypervisor) {
  239. unsigned long pfn = pte_pfn(pte);
  240. unsigned long pg_flags;
  241. struct page *page;
  242. if (pfn_valid(pfn) &&
  243. (page = pfn_to_page(pfn), page_mapping(page)) &&
  244. ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
  245. int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
  246. PG_dcache_cpu_mask);
  247. int this_cpu = get_cpu();
  248. /* This is just to optimize away some function calls
  249. * in the SMP case.
  250. */
  251. if (cpu == this_cpu)
  252. flush_dcache_page_impl(page);
  253. else
  254. smp_flush_dcache_page_impl(page, cpu);
  255. clear_dcache_dirty_cpu(page, cpu);
  256. put_cpu();
  257. }
  258. }
  259. mm = vma->vm_mm;
  260. spin_lock_irqsave(&mm->context.lock, flags);
  261. tsb = &mm->context.tsb[(address >> PAGE_SHIFT) &
  262. (mm->context.tsb_nentries - 1UL)];
  263. tag = (address >> 22UL);
  264. tsb_insert(tsb, tag, pte_val(pte));
  265. spin_unlock_irqrestore(&mm->context.lock, flags);
  266. }
  267. void flush_dcache_page(struct page *page)
  268. {
  269. struct address_space *mapping;
  270. int this_cpu;
  271. if (tlb_type == hypervisor)
  272. return;
  273. /* Do not bother with the expensive D-cache flush if it
  274. * is merely the zero page. The 'bigcore' testcase in GDB
  275. * causes this case to run millions of times.
  276. */
  277. if (page == ZERO_PAGE(0))
  278. return;
  279. this_cpu = get_cpu();
  280. mapping = page_mapping(page);
  281. if (mapping && !mapping_mapped(mapping)) {
  282. int dirty = test_bit(PG_dcache_dirty, &page->flags);
  283. if (dirty) {
  284. int dirty_cpu = dcache_dirty_cpu(page);
  285. if (dirty_cpu == this_cpu)
  286. goto out;
  287. smp_flush_dcache_page_impl(page, dirty_cpu);
  288. }
  289. set_dcache_dirty(page, this_cpu);
  290. } else {
  291. /* We could delay the flush for the !page_mapping
  292. * case too. But that case is for exec env/arg
  293. * pages and those are %99 certainly going to get
  294. * faulted into the tlb (and thus flushed) anyways.
  295. */
  296. flush_dcache_page_impl(page);
  297. }
  298. out:
  299. put_cpu();
  300. }
  301. void __kprobes flush_icache_range(unsigned long start, unsigned long end)
  302. {
  303. /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
  304. if (tlb_type == spitfire) {
  305. unsigned long kaddr;
  306. for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
  307. __flush_icache_page(__get_phys(kaddr));
  308. }
  309. }
  310. void show_mem(void)
  311. {
  312. printk("Mem-info:\n");
  313. show_free_areas();
  314. printk("Free swap: %6ldkB\n",
  315. nr_swap_pages << (PAGE_SHIFT-10));
  316. printk("%ld pages of RAM\n", num_physpages);
  317. printk("%d free pages\n", nr_free_pages());
  318. }
  319. void mmu_info(struct seq_file *m)
  320. {
  321. if (tlb_type == cheetah)
  322. seq_printf(m, "MMU Type\t: Cheetah\n");
  323. else if (tlb_type == cheetah_plus)
  324. seq_printf(m, "MMU Type\t: Cheetah+\n");
  325. else if (tlb_type == spitfire)
  326. seq_printf(m, "MMU Type\t: Spitfire\n");
  327. else if (tlb_type == hypervisor)
  328. seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
  329. else
  330. seq_printf(m, "MMU Type\t: ???\n");
  331. #ifdef CONFIG_DEBUG_DCFLUSH
  332. seq_printf(m, "DCPageFlushes\t: %d\n",
  333. atomic_read(&dcpage_flushes));
  334. #ifdef CONFIG_SMP
  335. seq_printf(m, "DCPageFlushesXC\t: %d\n",
  336. atomic_read(&dcpage_flushes_xcall));
  337. #endif /* CONFIG_SMP */
  338. #endif /* CONFIG_DEBUG_DCFLUSH */
  339. }
  340. struct linux_prom_translation {
  341. unsigned long virt;
  342. unsigned long size;
  343. unsigned long data;
  344. };
  345. /* Exported for kernel TLB miss handling in ktlb.S */
  346. struct linux_prom_translation prom_trans[512] __read_mostly;
  347. unsigned int prom_trans_ents __read_mostly;
  348. /* Exported for SMP bootup purposes. */
  349. unsigned long kern_locked_tte_data;
  350. /* The obp translations are saved based on 8k pagesize, since obp can
  351. * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
  352. * HI_OBP_ADDRESS range are handled in ktlb.S.
  353. */
  354. static inline int in_obp_range(unsigned long vaddr)
  355. {
  356. return (vaddr >= LOW_OBP_ADDRESS &&
  357. vaddr < HI_OBP_ADDRESS);
  358. }
  359. static int cmp_ptrans(const void *a, const void *b)
  360. {
  361. const struct linux_prom_translation *x = a, *y = b;
  362. if (x->virt > y->virt)
  363. return 1;
  364. if (x->virt < y->virt)
  365. return -1;
  366. return 0;
  367. }
  368. /* Read OBP translations property into 'prom_trans[]'. */
  369. static void __init read_obp_translations(void)
  370. {
  371. int n, node, ents, first, last, i;
  372. node = prom_finddevice("/virtual-memory");
  373. n = prom_getproplen(node, "translations");
  374. if (unlikely(n == 0 || n == -1)) {
  375. prom_printf("prom_mappings: Couldn't get size.\n");
  376. prom_halt();
  377. }
  378. if (unlikely(n > sizeof(prom_trans))) {
  379. prom_printf("prom_mappings: Size %Zd is too big.\n", n);
  380. prom_halt();
  381. }
  382. if ((n = prom_getproperty(node, "translations",
  383. (char *)&prom_trans[0],
  384. sizeof(prom_trans))) == -1) {
  385. prom_printf("prom_mappings: Couldn't get property.\n");
  386. prom_halt();
  387. }
  388. n = n / sizeof(struct linux_prom_translation);
  389. ents = n;
  390. sort(prom_trans, ents, sizeof(struct linux_prom_translation),
  391. cmp_ptrans, NULL);
  392. /* Now kick out all the non-OBP entries. */
  393. for (i = 0; i < ents; i++) {
  394. if (in_obp_range(prom_trans[i].virt))
  395. break;
  396. }
  397. first = i;
  398. for (; i < ents; i++) {
  399. if (!in_obp_range(prom_trans[i].virt))
  400. break;
  401. }
  402. last = i;
  403. for (i = 0; i < (last - first); i++) {
  404. struct linux_prom_translation *src = &prom_trans[i + first];
  405. struct linux_prom_translation *dest = &prom_trans[i];
  406. *dest = *src;
  407. }
  408. for (; i < ents; i++) {
  409. struct linux_prom_translation *dest = &prom_trans[i];
  410. dest->virt = dest->size = dest->data = 0x0UL;
  411. }
  412. prom_trans_ents = last - first;
  413. if (tlb_type == spitfire) {
  414. /* Clear diag TTE bits. */
  415. for (i = 0; i < prom_trans_ents; i++)
  416. prom_trans[i].data &= ~0x0003fe0000000000UL;
  417. }
  418. }
  419. static void __init hypervisor_tlb_lock(unsigned long vaddr,
  420. unsigned long pte,
  421. unsigned long mmu)
  422. {
  423. register unsigned long func asm("%o5");
  424. register unsigned long arg0 asm("%o0");
  425. register unsigned long arg1 asm("%o1");
  426. register unsigned long arg2 asm("%o2");
  427. register unsigned long arg3 asm("%o3");
  428. func = HV_FAST_MMU_MAP_PERM_ADDR;
  429. arg0 = vaddr;
  430. arg1 = 0;
  431. arg2 = pte;
  432. arg3 = mmu;
  433. __asm__ __volatile__("ta 0x80"
  434. : "=&r" (func), "=&r" (arg0),
  435. "=&r" (arg1), "=&r" (arg2),
  436. "=&r" (arg3)
  437. : "0" (func), "1" (arg0), "2" (arg1),
  438. "3" (arg2), "4" (arg3));
  439. if (arg0 != 0) {
  440. prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
  441. "errors with %lx\n", vaddr, 0, pte, mmu, arg0);
  442. prom_halt();
  443. }
  444. }
  445. static unsigned long kern_large_tte(unsigned long paddr);
  446. static void __init remap_kernel(void)
  447. {
  448. unsigned long phys_page, tte_vaddr, tte_data;
  449. int tlb_ent = sparc64_highest_locked_tlbent();
  450. tte_vaddr = (unsigned long) KERNBASE;
  451. phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  452. tte_data = kern_large_tte(phys_page);
  453. kern_locked_tte_data = tte_data;
  454. /* Now lock us into the TLBs via Hypervisor or OBP. */
  455. if (tlb_type == hypervisor) {
  456. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
  457. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
  458. if (bigkernel) {
  459. tte_vaddr += 0x400000;
  460. tte_data += 0x400000;
  461. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
  462. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
  463. }
  464. } else {
  465. prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
  466. prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
  467. if (bigkernel) {
  468. tlb_ent -= 1;
  469. prom_dtlb_load(tlb_ent,
  470. tte_data + 0x400000,
  471. tte_vaddr + 0x400000);
  472. prom_itlb_load(tlb_ent,
  473. tte_data + 0x400000,
  474. tte_vaddr + 0x400000);
  475. }
  476. sparc64_highest_unlocked_tlb_ent = tlb_ent - 1;
  477. }
  478. if (tlb_type == cheetah_plus) {
  479. sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
  480. CTX_CHEETAH_PLUS_NUC);
  481. sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
  482. sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
  483. }
  484. }
  485. static void __init inherit_prom_mappings(void)
  486. {
  487. read_obp_translations();
  488. /* Now fixup OBP's idea about where we really are mapped. */
  489. prom_printf("Remapping the kernel... ");
  490. remap_kernel();
  491. prom_printf("done.\n");
  492. }
  493. void prom_world(int enter)
  494. {
  495. if (!enter)
  496. set_fs((mm_segment_t) { get_thread_current_ds() });
  497. __asm__ __volatile__("flushw");
  498. }
  499. #ifdef DCACHE_ALIASING_POSSIBLE
  500. void __flush_dcache_range(unsigned long start, unsigned long end)
  501. {
  502. unsigned long va;
  503. if (tlb_type == spitfire) {
  504. int n = 0;
  505. for (va = start; va < end; va += 32) {
  506. spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
  507. if (++n >= 512)
  508. break;
  509. }
  510. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  511. start = __pa(start);
  512. end = __pa(end);
  513. for (va = start; va < end; va += 32)
  514. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  515. "membar #Sync"
  516. : /* no outputs */
  517. : "r" (va),
  518. "i" (ASI_DCACHE_INVALIDATE));
  519. }
  520. }
  521. #endif /* DCACHE_ALIASING_POSSIBLE */
  522. /* Caller does TLB context flushing on local CPU if necessary.
  523. * The caller also ensures that CTX_VALID(mm->context) is false.
  524. *
  525. * We must be careful about boundary cases so that we never
  526. * let the user have CTX 0 (nucleus) or we ever use a CTX
  527. * version of zero (and thus NO_CONTEXT would not be caught
  528. * by version mis-match tests in mmu_context.h).
  529. *
  530. * Always invoked with interrupts disabled.
  531. */
  532. void get_new_mmu_context(struct mm_struct *mm)
  533. {
  534. unsigned long ctx, new_ctx;
  535. unsigned long orig_pgsz_bits;
  536. unsigned long flags;
  537. int new_version;
  538. spin_lock_irqsave(&ctx_alloc_lock, flags);
  539. orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
  540. ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
  541. new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
  542. new_version = 0;
  543. if (new_ctx >= (1 << CTX_NR_BITS)) {
  544. new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
  545. if (new_ctx >= ctx) {
  546. int i;
  547. new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
  548. CTX_FIRST_VERSION;
  549. if (new_ctx == 1)
  550. new_ctx = CTX_FIRST_VERSION;
  551. /* Don't call memset, for 16 entries that's just
  552. * plain silly...
  553. */
  554. mmu_context_bmap[0] = 3;
  555. mmu_context_bmap[1] = 0;
  556. mmu_context_bmap[2] = 0;
  557. mmu_context_bmap[3] = 0;
  558. for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
  559. mmu_context_bmap[i + 0] = 0;
  560. mmu_context_bmap[i + 1] = 0;
  561. mmu_context_bmap[i + 2] = 0;
  562. mmu_context_bmap[i + 3] = 0;
  563. }
  564. new_version = 1;
  565. goto out;
  566. }
  567. }
  568. mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
  569. new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
  570. out:
  571. tlb_context_cache = new_ctx;
  572. mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
  573. spin_unlock_irqrestore(&ctx_alloc_lock, flags);
  574. if (unlikely(new_version))
  575. smp_new_mmu_context_version();
  576. }
  577. void sparc_ultra_dump_itlb(void)
  578. {
  579. int slot;
  580. if (tlb_type == spitfire) {
  581. printk ("Contents of itlb: ");
  582. for (slot = 0; slot < 14; slot++) printk (" ");
  583. printk ("%2x:%016lx,%016lx\n",
  584. 0,
  585. spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
  586. for (slot = 1; slot < 64; slot+=3) {
  587. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  588. slot,
  589. spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
  590. slot+1,
  591. spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
  592. slot+2,
  593. spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
  594. }
  595. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  596. printk ("Contents of itlb0:\n");
  597. for (slot = 0; slot < 16; slot+=2) {
  598. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  599. slot,
  600. cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
  601. slot+1,
  602. cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
  603. }
  604. printk ("Contents of itlb2:\n");
  605. for (slot = 0; slot < 128; slot+=2) {
  606. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  607. slot,
  608. cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
  609. slot+1,
  610. cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
  611. }
  612. }
  613. }
  614. void sparc_ultra_dump_dtlb(void)
  615. {
  616. int slot;
  617. if (tlb_type == spitfire) {
  618. printk ("Contents of dtlb: ");
  619. for (slot = 0; slot < 14; slot++) printk (" ");
  620. printk ("%2x:%016lx,%016lx\n", 0,
  621. spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
  622. for (slot = 1; slot < 64; slot+=3) {
  623. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  624. slot,
  625. spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
  626. slot+1,
  627. spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
  628. slot+2,
  629. spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
  630. }
  631. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  632. printk ("Contents of dtlb0:\n");
  633. for (slot = 0; slot < 16; slot+=2) {
  634. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  635. slot,
  636. cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
  637. slot+1,
  638. cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
  639. }
  640. printk ("Contents of dtlb2:\n");
  641. for (slot = 0; slot < 512; slot+=2) {
  642. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  643. slot,
  644. cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
  645. slot+1,
  646. cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
  647. }
  648. if (tlb_type == cheetah_plus) {
  649. printk ("Contents of dtlb3:\n");
  650. for (slot = 0; slot < 512; slot+=2) {
  651. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  652. slot,
  653. cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
  654. slot+1,
  655. cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
  656. }
  657. }
  658. }
  659. }
  660. extern unsigned long cmdline_memory_size;
  661. /* Find a free area for the bootmem map, avoiding the kernel image
  662. * and the initial ramdisk.
  663. */
  664. static unsigned long __init choose_bootmap_pfn(unsigned long start_pfn,
  665. unsigned long end_pfn)
  666. {
  667. unsigned long avoid_start, avoid_end, bootmap_size;
  668. int i;
  669. bootmap_size = ((end_pfn - start_pfn) + 7) / 8;
  670. bootmap_size = ALIGN(bootmap_size, sizeof(long));
  671. avoid_start = avoid_end = 0;
  672. #ifdef CONFIG_BLK_DEV_INITRD
  673. avoid_start = initrd_start;
  674. avoid_end = PAGE_ALIGN(initrd_end);
  675. #endif
  676. #ifdef CONFIG_DEBUG_BOOTMEM
  677. prom_printf("choose_bootmap_pfn: kern[%lx:%lx] avoid[%lx:%lx]\n",
  678. kern_base, PAGE_ALIGN(kern_base + kern_size),
  679. avoid_start, avoid_end);
  680. #endif
  681. for (i = 0; i < pavail_ents; i++) {
  682. unsigned long start, end;
  683. start = pavail[i].phys_addr;
  684. end = start + pavail[i].reg_size;
  685. while (start < end) {
  686. if (start >= kern_base &&
  687. start < PAGE_ALIGN(kern_base + kern_size)) {
  688. start = PAGE_ALIGN(kern_base + kern_size);
  689. continue;
  690. }
  691. if (start >= avoid_start && start < avoid_end) {
  692. start = avoid_end;
  693. continue;
  694. }
  695. if ((end - start) < bootmap_size)
  696. break;
  697. if (start < kern_base &&
  698. (start + bootmap_size) > kern_base) {
  699. start = PAGE_ALIGN(kern_base + kern_size);
  700. continue;
  701. }
  702. if (start < avoid_start &&
  703. (start + bootmap_size) > avoid_start) {
  704. start = avoid_end;
  705. continue;
  706. }
  707. /* OK, it doesn't overlap anything, use it. */
  708. #ifdef CONFIG_DEBUG_BOOTMEM
  709. prom_printf("choose_bootmap_pfn: Using %lx [%lx]\n",
  710. start >> PAGE_SHIFT, start);
  711. #endif
  712. return start >> PAGE_SHIFT;
  713. }
  714. }
  715. prom_printf("Cannot find free area for bootmap, aborting.\n");
  716. prom_halt();
  717. }
  718. static unsigned long __init bootmem_init(unsigned long *pages_avail,
  719. unsigned long phys_base)
  720. {
  721. unsigned long bootmap_size, end_pfn;
  722. unsigned long end_of_phys_memory = 0UL;
  723. unsigned long bootmap_pfn, bytes_avail, size;
  724. int i;
  725. #ifdef CONFIG_DEBUG_BOOTMEM
  726. prom_printf("bootmem_init: Scan pavail, ");
  727. #endif
  728. bytes_avail = 0UL;
  729. for (i = 0; i < pavail_ents; i++) {
  730. end_of_phys_memory = pavail[i].phys_addr +
  731. pavail[i].reg_size;
  732. bytes_avail += pavail[i].reg_size;
  733. if (cmdline_memory_size) {
  734. if (bytes_avail > cmdline_memory_size) {
  735. unsigned long slack = bytes_avail - cmdline_memory_size;
  736. bytes_avail -= slack;
  737. end_of_phys_memory -= slack;
  738. pavail[i].reg_size -= slack;
  739. if ((long)pavail[i].reg_size <= 0L) {
  740. pavail[i].phys_addr = 0xdeadbeefUL;
  741. pavail[i].reg_size = 0UL;
  742. pavail_ents = i;
  743. } else {
  744. pavail[i+1].reg_size = 0Ul;
  745. pavail[i+1].phys_addr = 0xdeadbeefUL;
  746. pavail_ents = i + 1;
  747. }
  748. break;
  749. }
  750. }
  751. }
  752. *pages_avail = bytes_avail >> PAGE_SHIFT;
  753. end_pfn = end_of_phys_memory >> PAGE_SHIFT;
  754. #ifdef CONFIG_BLK_DEV_INITRD
  755. /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
  756. if (sparc_ramdisk_image || sparc_ramdisk_image64) {
  757. unsigned long ramdisk_image = sparc_ramdisk_image ?
  758. sparc_ramdisk_image : sparc_ramdisk_image64;
  759. if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
  760. ramdisk_image -= KERNBASE;
  761. initrd_start = ramdisk_image + phys_base;
  762. initrd_end = initrd_start + sparc_ramdisk_size;
  763. if (initrd_end > end_of_phys_memory) {
  764. printk(KERN_CRIT "initrd extends beyond end of memory "
  765. "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
  766. initrd_end, end_of_phys_memory);
  767. initrd_start = 0;
  768. initrd_end = 0;
  769. }
  770. }
  771. #endif
  772. /* Initialize the boot-time allocator. */
  773. max_pfn = max_low_pfn = end_pfn;
  774. min_low_pfn = (phys_base >> PAGE_SHIFT);
  775. bootmap_pfn = choose_bootmap_pfn(min_low_pfn, end_pfn);
  776. #ifdef CONFIG_DEBUG_BOOTMEM
  777. prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
  778. min_low_pfn, bootmap_pfn, max_low_pfn);
  779. #endif
  780. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn,
  781. min_low_pfn, end_pfn);
  782. /* Now register the available physical memory with the
  783. * allocator.
  784. */
  785. for (i = 0; i < pavail_ents; i++) {
  786. #ifdef CONFIG_DEBUG_BOOTMEM
  787. prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
  788. i, pavail[i].phys_addr, pavail[i].reg_size);
  789. #endif
  790. free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
  791. }
  792. #ifdef CONFIG_BLK_DEV_INITRD
  793. if (initrd_start) {
  794. size = initrd_end - initrd_start;
  795. /* Resert the initrd image area. */
  796. #ifdef CONFIG_DEBUG_BOOTMEM
  797. prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
  798. initrd_start, initrd_end);
  799. #endif
  800. reserve_bootmem(initrd_start, size);
  801. *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
  802. initrd_start += PAGE_OFFSET;
  803. initrd_end += PAGE_OFFSET;
  804. }
  805. #endif
  806. /* Reserve the kernel text/data/bss. */
  807. #ifdef CONFIG_DEBUG_BOOTMEM
  808. prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
  809. #endif
  810. reserve_bootmem(kern_base, kern_size);
  811. *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
  812. /* Reserve the bootmem map. We do not account for it
  813. * in pages_avail because we will release that memory
  814. * in free_all_bootmem.
  815. */
  816. size = bootmap_size;
  817. #ifdef CONFIG_DEBUG_BOOTMEM
  818. prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
  819. (bootmap_pfn << PAGE_SHIFT), size);
  820. #endif
  821. reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
  822. *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
  823. for (i = 0; i < pavail_ents; i++) {
  824. unsigned long start_pfn, end_pfn;
  825. start_pfn = pavail[i].phys_addr >> PAGE_SHIFT;
  826. end_pfn = (start_pfn + (pavail[i].reg_size >> PAGE_SHIFT));
  827. #ifdef CONFIG_DEBUG_BOOTMEM
  828. prom_printf("memory_present(0, %lx, %lx)\n",
  829. start_pfn, end_pfn);
  830. #endif
  831. memory_present(0, start_pfn, end_pfn);
  832. }
  833. sparse_init();
  834. return end_pfn;
  835. }
  836. static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
  837. static int pall_ents __initdata;
  838. #ifdef CONFIG_DEBUG_PAGEALLOC
  839. static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
  840. {
  841. unsigned long vstart = PAGE_OFFSET + pstart;
  842. unsigned long vend = PAGE_OFFSET + pend;
  843. unsigned long alloc_bytes = 0UL;
  844. if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
  845. prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
  846. vstart, vend);
  847. prom_halt();
  848. }
  849. while (vstart < vend) {
  850. unsigned long this_end, paddr = __pa(vstart);
  851. pgd_t *pgd = pgd_offset_k(vstart);
  852. pud_t *pud;
  853. pmd_t *pmd;
  854. pte_t *pte;
  855. pud = pud_offset(pgd, vstart);
  856. if (pud_none(*pud)) {
  857. pmd_t *new;
  858. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  859. alloc_bytes += PAGE_SIZE;
  860. pud_populate(&init_mm, pud, new);
  861. }
  862. pmd = pmd_offset(pud, vstart);
  863. if (!pmd_present(*pmd)) {
  864. pte_t *new;
  865. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  866. alloc_bytes += PAGE_SIZE;
  867. pmd_populate_kernel(&init_mm, pmd, new);
  868. }
  869. pte = pte_offset_kernel(pmd, vstart);
  870. this_end = (vstart + PMD_SIZE) & PMD_MASK;
  871. if (this_end > vend)
  872. this_end = vend;
  873. while (vstart < this_end) {
  874. pte_val(*pte) = (paddr | pgprot_val(prot));
  875. vstart += PAGE_SIZE;
  876. paddr += PAGE_SIZE;
  877. pte++;
  878. }
  879. }
  880. return alloc_bytes;
  881. }
  882. extern unsigned int kvmap_linear_patch[1];
  883. #endif /* CONFIG_DEBUG_PAGEALLOC */
  884. static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
  885. {
  886. const unsigned long shift_256MB = 28;
  887. const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
  888. const unsigned long size_256MB = (1UL << shift_256MB);
  889. while (start < end) {
  890. long remains;
  891. remains = end - start;
  892. if (remains < size_256MB)
  893. break;
  894. if (start & mask_256MB) {
  895. start = (start + size_256MB) & ~mask_256MB;
  896. continue;
  897. }
  898. while (remains >= size_256MB) {
  899. unsigned long index = start >> shift_256MB;
  900. __set_bit(index, kpte_linear_bitmap);
  901. start += size_256MB;
  902. remains -= size_256MB;
  903. }
  904. }
  905. }
  906. static void __init kernel_physical_mapping_init(void)
  907. {
  908. unsigned long i;
  909. #ifdef CONFIG_DEBUG_PAGEALLOC
  910. unsigned long mem_alloced = 0UL;
  911. #endif
  912. read_obp_memory("reg", &pall[0], &pall_ents);
  913. for (i = 0; i < pall_ents; i++) {
  914. unsigned long phys_start, phys_end;
  915. phys_start = pall[i].phys_addr;
  916. phys_end = phys_start + pall[i].reg_size;
  917. mark_kpte_bitmap(phys_start, phys_end);
  918. #ifdef CONFIG_DEBUG_PAGEALLOC
  919. mem_alloced += kernel_map_range(phys_start, phys_end,
  920. PAGE_KERNEL);
  921. #endif
  922. }
  923. #ifdef CONFIG_DEBUG_PAGEALLOC
  924. printk("Allocated %ld bytes for kernel page tables.\n",
  925. mem_alloced);
  926. kvmap_linear_patch[0] = 0x01000000; /* nop */
  927. flushi(&kvmap_linear_patch[0]);
  928. __flush_tlb_all();
  929. #endif
  930. }
  931. #ifdef CONFIG_DEBUG_PAGEALLOC
  932. void kernel_map_pages(struct page *page, int numpages, int enable)
  933. {
  934. unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
  935. unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
  936. kernel_map_range(phys_start, phys_end,
  937. (enable ? PAGE_KERNEL : __pgprot(0)));
  938. flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
  939. PAGE_OFFSET + phys_end);
  940. /* we should perform an IPI and flush all tlbs,
  941. * but that can deadlock->flush only current cpu.
  942. */
  943. __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
  944. PAGE_OFFSET + phys_end);
  945. }
  946. #endif
  947. unsigned long __init find_ecache_flush_span(unsigned long size)
  948. {
  949. int i;
  950. for (i = 0; i < pavail_ents; i++) {
  951. if (pavail[i].reg_size >= size)
  952. return pavail[i].phys_addr;
  953. }
  954. return ~0UL;
  955. }
  956. static void __init tsb_phys_patch(void)
  957. {
  958. struct tsb_ldquad_phys_patch_entry *pquad;
  959. struct tsb_phys_patch_entry *p;
  960. pquad = &__tsb_ldquad_phys_patch;
  961. while (pquad < &__tsb_ldquad_phys_patch_end) {
  962. unsigned long addr = pquad->addr;
  963. if (tlb_type == hypervisor)
  964. *(unsigned int *) addr = pquad->sun4v_insn;
  965. else
  966. *(unsigned int *) addr = pquad->sun4u_insn;
  967. wmb();
  968. __asm__ __volatile__("flush %0"
  969. : /* no outputs */
  970. : "r" (addr));
  971. pquad++;
  972. }
  973. p = &__tsb_phys_patch;
  974. while (p < &__tsb_phys_patch_end) {
  975. unsigned long addr = p->addr;
  976. *(unsigned int *) addr = p->insn;
  977. wmb();
  978. __asm__ __volatile__("flush %0"
  979. : /* no outputs */
  980. : "r" (addr));
  981. p++;
  982. }
  983. }
  984. /* Don't mark as init, we give this to the Hypervisor. */
  985. static struct hv_tsb_descr ktsb_descr[2];
  986. extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
  987. static void __init sun4v_ktsb_init(void)
  988. {
  989. unsigned long ktsb_pa;
  990. /* First KTSB for PAGE_SIZE mappings. */
  991. ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
  992. switch (PAGE_SIZE) {
  993. case 8 * 1024:
  994. default:
  995. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
  996. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
  997. break;
  998. case 64 * 1024:
  999. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
  1000. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
  1001. break;
  1002. case 512 * 1024:
  1003. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
  1004. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
  1005. break;
  1006. case 4 * 1024 * 1024:
  1007. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
  1008. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
  1009. break;
  1010. };
  1011. ktsb_descr[0].assoc = 1;
  1012. ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
  1013. ktsb_descr[0].ctx_idx = 0;
  1014. ktsb_descr[0].tsb_base = ktsb_pa;
  1015. ktsb_descr[0].resv = 0;
  1016. /* Second KTSB for 4MB/256MB mappings. */
  1017. ktsb_pa = (kern_base +
  1018. ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
  1019. ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
  1020. ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
  1021. HV_PGSZ_MASK_256MB);
  1022. ktsb_descr[1].assoc = 1;
  1023. ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
  1024. ktsb_descr[1].ctx_idx = 0;
  1025. ktsb_descr[1].tsb_base = ktsb_pa;
  1026. ktsb_descr[1].resv = 0;
  1027. }
  1028. void __cpuinit sun4v_ktsb_register(void)
  1029. {
  1030. register unsigned long func asm("%o5");
  1031. register unsigned long arg0 asm("%o0");
  1032. register unsigned long arg1 asm("%o1");
  1033. unsigned long pa;
  1034. pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
  1035. func = HV_FAST_MMU_TSB_CTX0;
  1036. arg0 = 2;
  1037. arg1 = pa;
  1038. __asm__ __volatile__("ta %6"
  1039. : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
  1040. : "0" (func), "1" (arg0), "2" (arg1),
  1041. "i" (HV_FAST_TRAP));
  1042. }
  1043. /* paging_init() sets up the page tables */
  1044. extern void cheetah_ecache_flush_init(void);
  1045. extern void sun4v_patch_tlb_handlers(void);
  1046. static unsigned long last_valid_pfn;
  1047. pgd_t swapper_pg_dir[2048];
  1048. static void sun4u_pgprot_init(void);
  1049. static void sun4v_pgprot_init(void);
  1050. void __init paging_init(void)
  1051. {
  1052. unsigned long end_pfn, pages_avail, shift, phys_base;
  1053. unsigned long real_end, i;
  1054. kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  1055. kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
  1056. /* Invalidate both kernel TSBs. */
  1057. memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
  1058. memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
  1059. if (tlb_type == hypervisor)
  1060. sun4v_pgprot_init();
  1061. else
  1062. sun4u_pgprot_init();
  1063. if (tlb_type == cheetah_plus ||
  1064. tlb_type == hypervisor)
  1065. tsb_phys_patch();
  1066. if (tlb_type == hypervisor) {
  1067. sun4v_patch_tlb_handlers();
  1068. sun4v_ktsb_init();
  1069. }
  1070. /* Find available physical memory... */
  1071. read_obp_memory("available", &pavail[0], &pavail_ents);
  1072. phys_base = 0xffffffffffffffffUL;
  1073. for (i = 0; i < pavail_ents; i++)
  1074. phys_base = min(phys_base, pavail[i].phys_addr);
  1075. set_bit(0, mmu_context_bmap);
  1076. shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
  1077. real_end = (unsigned long)_end;
  1078. if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
  1079. bigkernel = 1;
  1080. if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
  1081. prom_printf("paging_init: Kernel > 8MB, too large.\n");
  1082. prom_halt();
  1083. }
  1084. /* Set kernel pgd to upper alias so physical page computations
  1085. * work.
  1086. */
  1087. init_mm.pgd += ((shift) / (sizeof(pgd_t)));
  1088. memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
  1089. /* Now can init the kernel/bad page tables. */
  1090. pud_set(pud_offset(&swapper_pg_dir[0], 0),
  1091. swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
  1092. inherit_prom_mappings();
  1093. /* Ok, we can use our TLB miss and window trap handlers safely. */
  1094. setup_tba();
  1095. __flush_tlb_all();
  1096. if (tlb_type == hypervisor)
  1097. sun4v_ktsb_register();
  1098. /* Setup bootmem... */
  1099. pages_avail = 0;
  1100. last_valid_pfn = end_pfn = bootmem_init(&pages_avail, phys_base);
  1101. max_mapnr = last_valid_pfn;
  1102. kernel_physical_mapping_init();
  1103. {
  1104. unsigned long zones_size[MAX_NR_ZONES];
  1105. unsigned long zholes_size[MAX_NR_ZONES];
  1106. int znum;
  1107. for (znum = 0; znum < MAX_NR_ZONES; znum++)
  1108. zones_size[znum] = zholes_size[znum] = 0;
  1109. zones_size[ZONE_DMA] = end_pfn;
  1110. zholes_size[ZONE_DMA] = end_pfn - pages_avail;
  1111. free_area_init_node(0, &contig_page_data, zones_size,
  1112. __pa(PAGE_OFFSET) >> PAGE_SHIFT,
  1113. zholes_size);
  1114. }
  1115. device_scan();
  1116. }
  1117. static void __init taint_real_pages(void)
  1118. {
  1119. int i;
  1120. read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
  1121. /* Find changes discovered in the physmem available rescan and
  1122. * reserve the lost portions in the bootmem maps.
  1123. */
  1124. for (i = 0; i < pavail_ents; i++) {
  1125. unsigned long old_start, old_end;
  1126. old_start = pavail[i].phys_addr;
  1127. old_end = old_start +
  1128. pavail[i].reg_size;
  1129. while (old_start < old_end) {
  1130. int n;
  1131. for (n = 0; pavail_rescan_ents; n++) {
  1132. unsigned long new_start, new_end;
  1133. new_start = pavail_rescan[n].phys_addr;
  1134. new_end = new_start +
  1135. pavail_rescan[n].reg_size;
  1136. if (new_start <= old_start &&
  1137. new_end >= (old_start + PAGE_SIZE)) {
  1138. set_bit(old_start >> 22,
  1139. sparc64_valid_addr_bitmap);
  1140. goto do_next_page;
  1141. }
  1142. }
  1143. reserve_bootmem(old_start, PAGE_SIZE);
  1144. do_next_page:
  1145. old_start += PAGE_SIZE;
  1146. }
  1147. }
  1148. }
  1149. void __init mem_init(void)
  1150. {
  1151. unsigned long codepages, datapages, initpages;
  1152. unsigned long addr, last;
  1153. int i;
  1154. i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
  1155. i += 1;
  1156. sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
  1157. if (sparc64_valid_addr_bitmap == NULL) {
  1158. prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
  1159. prom_halt();
  1160. }
  1161. memset(sparc64_valid_addr_bitmap, 0, i << 3);
  1162. addr = PAGE_OFFSET + kern_base;
  1163. last = PAGE_ALIGN(kern_size) + addr;
  1164. while (addr < last) {
  1165. set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
  1166. addr += PAGE_SIZE;
  1167. }
  1168. taint_real_pages();
  1169. high_memory = __va(last_valid_pfn << PAGE_SHIFT);
  1170. #ifdef CONFIG_DEBUG_BOOTMEM
  1171. prom_printf("mem_init: Calling free_all_bootmem().\n");
  1172. #endif
  1173. totalram_pages = num_physpages = free_all_bootmem() - 1;
  1174. /*
  1175. * Set up the zero page, mark it reserved, so that page count
  1176. * is not manipulated when freeing the page from user ptes.
  1177. */
  1178. mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
  1179. if (mem_map_zero == NULL) {
  1180. prom_printf("paging_init: Cannot alloc zero page.\n");
  1181. prom_halt();
  1182. }
  1183. SetPageReserved(mem_map_zero);
  1184. codepages = (((unsigned long) _etext) - ((unsigned long) _start));
  1185. codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
  1186. datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
  1187. datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
  1188. initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
  1189. initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
  1190. printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
  1191. nr_free_pages() << (PAGE_SHIFT-10),
  1192. codepages << (PAGE_SHIFT-10),
  1193. datapages << (PAGE_SHIFT-10),
  1194. initpages << (PAGE_SHIFT-10),
  1195. PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
  1196. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1197. cheetah_ecache_flush_init();
  1198. }
  1199. void free_initmem(void)
  1200. {
  1201. unsigned long addr, initend;
  1202. /*
  1203. * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
  1204. */
  1205. addr = PAGE_ALIGN((unsigned long)(__init_begin));
  1206. initend = (unsigned long)(__init_end) & PAGE_MASK;
  1207. for (; addr < initend; addr += PAGE_SIZE) {
  1208. unsigned long page;
  1209. struct page *p;
  1210. page = (addr +
  1211. ((unsigned long) __va(kern_base)) -
  1212. ((unsigned long) KERNBASE));
  1213. memset((void *)addr, 0xcc, PAGE_SIZE);
  1214. p = virt_to_page(page);
  1215. ClearPageReserved(p);
  1216. init_page_count(p);
  1217. __free_page(p);
  1218. num_physpages++;
  1219. totalram_pages++;
  1220. }
  1221. }
  1222. #ifdef CONFIG_BLK_DEV_INITRD
  1223. void free_initrd_mem(unsigned long start, unsigned long end)
  1224. {
  1225. if (start < end)
  1226. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  1227. for (; start < end; start += PAGE_SIZE) {
  1228. struct page *p = virt_to_page(start);
  1229. ClearPageReserved(p);
  1230. init_page_count(p);
  1231. __free_page(p);
  1232. num_physpages++;
  1233. totalram_pages++;
  1234. }
  1235. }
  1236. #endif
  1237. #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
  1238. #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
  1239. #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
  1240. #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
  1241. #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
  1242. #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
  1243. pgprot_t PAGE_KERNEL __read_mostly;
  1244. EXPORT_SYMBOL(PAGE_KERNEL);
  1245. pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
  1246. pgprot_t PAGE_COPY __read_mostly;
  1247. pgprot_t PAGE_SHARED __read_mostly;
  1248. EXPORT_SYMBOL(PAGE_SHARED);
  1249. pgprot_t PAGE_EXEC __read_mostly;
  1250. unsigned long pg_iobits __read_mostly;
  1251. unsigned long _PAGE_IE __read_mostly;
  1252. unsigned long _PAGE_E __read_mostly;
  1253. EXPORT_SYMBOL(_PAGE_E);
  1254. unsigned long _PAGE_CACHE __read_mostly;
  1255. EXPORT_SYMBOL(_PAGE_CACHE);
  1256. static void prot_init_common(unsigned long page_none,
  1257. unsigned long page_shared,
  1258. unsigned long page_copy,
  1259. unsigned long page_readonly,
  1260. unsigned long page_exec_bit)
  1261. {
  1262. PAGE_COPY = __pgprot(page_copy);
  1263. PAGE_SHARED = __pgprot(page_shared);
  1264. protection_map[0x0] = __pgprot(page_none);
  1265. protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
  1266. protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
  1267. protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
  1268. protection_map[0x4] = __pgprot(page_readonly);
  1269. protection_map[0x5] = __pgprot(page_readonly);
  1270. protection_map[0x6] = __pgprot(page_copy);
  1271. protection_map[0x7] = __pgprot(page_copy);
  1272. protection_map[0x8] = __pgprot(page_none);
  1273. protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
  1274. protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
  1275. protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
  1276. protection_map[0xc] = __pgprot(page_readonly);
  1277. protection_map[0xd] = __pgprot(page_readonly);
  1278. protection_map[0xe] = __pgprot(page_shared);
  1279. protection_map[0xf] = __pgprot(page_shared);
  1280. }
  1281. static void __init sun4u_pgprot_init(void)
  1282. {
  1283. unsigned long page_none, page_shared, page_copy, page_readonly;
  1284. unsigned long page_exec_bit;
  1285. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1286. _PAGE_CACHE_4U | _PAGE_P_4U |
  1287. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1288. _PAGE_EXEC_4U);
  1289. PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1290. _PAGE_CACHE_4U | _PAGE_P_4U |
  1291. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1292. _PAGE_EXEC_4U | _PAGE_L_4U);
  1293. PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
  1294. _PAGE_IE = _PAGE_IE_4U;
  1295. _PAGE_E = _PAGE_E_4U;
  1296. _PAGE_CACHE = _PAGE_CACHE_4U;
  1297. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
  1298. __ACCESS_BITS_4U | _PAGE_E_4U);
  1299. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
  1300. 0xfffff80000000000;
  1301. kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
  1302. _PAGE_P_4U | _PAGE_W_4U);
  1303. /* XXX Should use 256MB on Panther. XXX */
  1304. kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
  1305. _PAGE_SZBITS = _PAGE_SZBITS_4U;
  1306. _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
  1307. _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
  1308. _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
  1309. page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
  1310. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1311. __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
  1312. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1313. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1314. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1315. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1316. page_exec_bit = _PAGE_EXEC_4U;
  1317. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1318. page_exec_bit);
  1319. }
  1320. static void __init sun4v_pgprot_init(void)
  1321. {
  1322. unsigned long page_none, page_shared, page_copy, page_readonly;
  1323. unsigned long page_exec_bit;
  1324. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
  1325. _PAGE_CACHE_4V | _PAGE_P_4V |
  1326. __ACCESS_BITS_4V | __DIRTY_BITS_4V |
  1327. _PAGE_EXEC_4V);
  1328. PAGE_KERNEL_LOCKED = PAGE_KERNEL;
  1329. PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
  1330. _PAGE_IE = _PAGE_IE_4V;
  1331. _PAGE_E = _PAGE_E_4V;
  1332. _PAGE_CACHE = _PAGE_CACHE_4V;
  1333. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
  1334. 0xfffff80000000000;
  1335. kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1336. _PAGE_P_4V | _PAGE_W_4V);
  1337. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
  1338. 0xfffff80000000000;
  1339. kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1340. _PAGE_P_4V | _PAGE_W_4V);
  1341. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
  1342. __ACCESS_BITS_4V | _PAGE_E_4V);
  1343. _PAGE_SZBITS = _PAGE_SZBITS_4V;
  1344. _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
  1345. _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
  1346. _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
  1347. _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
  1348. page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
  1349. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1350. __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
  1351. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1352. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1353. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1354. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1355. page_exec_bit = _PAGE_EXEC_4V;
  1356. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1357. page_exec_bit);
  1358. }
  1359. unsigned long pte_sz_bits(unsigned long sz)
  1360. {
  1361. if (tlb_type == hypervisor) {
  1362. switch (sz) {
  1363. case 8 * 1024:
  1364. default:
  1365. return _PAGE_SZ8K_4V;
  1366. case 64 * 1024:
  1367. return _PAGE_SZ64K_4V;
  1368. case 512 * 1024:
  1369. return _PAGE_SZ512K_4V;
  1370. case 4 * 1024 * 1024:
  1371. return _PAGE_SZ4MB_4V;
  1372. };
  1373. } else {
  1374. switch (sz) {
  1375. case 8 * 1024:
  1376. default:
  1377. return _PAGE_SZ8K_4U;
  1378. case 64 * 1024:
  1379. return _PAGE_SZ64K_4U;
  1380. case 512 * 1024:
  1381. return _PAGE_SZ512K_4U;
  1382. case 4 * 1024 * 1024:
  1383. return _PAGE_SZ4MB_4U;
  1384. };
  1385. }
  1386. }
  1387. pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
  1388. {
  1389. pte_t pte;
  1390. pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
  1391. pte_val(pte) |= (((unsigned long)space) << 32);
  1392. pte_val(pte) |= pte_sz_bits(page_size);
  1393. return pte;
  1394. }
  1395. static unsigned long kern_large_tte(unsigned long paddr)
  1396. {
  1397. unsigned long val;
  1398. val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1399. _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
  1400. _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
  1401. if (tlb_type == hypervisor)
  1402. val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1403. _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
  1404. _PAGE_EXEC_4V | _PAGE_W_4V);
  1405. return val | paddr;
  1406. }
  1407. /*
  1408. * Translate PROM's mapping we capture at boot time into physical address.
  1409. * The second parameter is only set from prom_callback() invocations.
  1410. */
  1411. unsigned long prom_virt_to_phys(unsigned long promva, int *error)
  1412. {
  1413. unsigned long mask;
  1414. int i;
  1415. mask = _PAGE_PADDR_4U;
  1416. if (tlb_type == hypervisor)
  1417. mask = _PAGE_PADDR_4V;
  1418. for (i = 0; i < prom_trans_ents; i++) {
  1419. struct linux_prom_translation *p = &prom_trans[i];
  1420. if (promva >= p->virt &&
  1421. promva < (p->virt + p->size)) {
  1422. unsigned long base = p->data & mask;
  1423. if (error)
  1424. *error = 0;
  1425. return base + (promva & (8192 - 1));
  1426. }
  1427. }
  1428. if (error)
  1429. *error = 1;
  1430. return 0UL;
  1431. }
  1432. /* XXX We should kill off this ugly thing at so me point. XXX */
  1433. unsigned long sun4u_get_pte(unsigned long addr)
  1434. {
  1435. pgd_t *pgdp;
  1436. pud_t *pudp;
  1437. pmd_t *pmdp;
  1438. pte_t *ptep;
  1439. unsigned long mask = _PAGE_PADDR_4U;
  1440. if (tlb_type == hypervisor)
  1441. mask = _PAGE_PADDR_4V;
  1442. if (addr >= PAGE_OFFSET)
  1443. return addr & mask;
  1444. if ((addr >= LOW_OBP_ADDRESS) && (addr < HI_OBP_ADDRESS))
  1445. return prom_virt_to_phys(addr, NULL);
  1446. pgdp = pgd_offset_k(addr);
  1447. pudp = pud_offset(pgdp, addr);
  1448. pmdp = pmd_offset(pudp, addr);
  1449. ptep = pte_offset_kernel(pmdp, addr);
  1450. return pte_val(*ptep) & mask;
  1451. }
  1452. /* If not locked, zap it. */
  1453. void __flush_tlb_all(void)
  1454. {
  1455. unsigned long pstate;
  1456. int i;
  1457. __asm__ __volatile__("flushw\n\t"
  1458. "rdpr %%pstate, %0\n\t"
  1459. "wrpr %0, %1, %%pstate"
  1460. : "=r" (pstate)
  1461. : "i" (PSTATE_IE));
  1462. if (tlb_type == spitfire) {
  1463. for (i = 0; i < 64; i++) {
  1464. /* Spitfire Errata #32 workaround */
  1465. /* NOTE: Always runs on spitfire, so no
  1466. * cheetah+ page size encodings.
  1467. */
  1468. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1469. "flush %%g6"
  1470. : /* No outputs */
  1471. : "r" (0),
  1472. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1473. if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
  1474. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1475. "membar #Sync"
  1476. : /* no outputs */
  1477. : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
  1478. spitfire_put_dtlb_data(i, 0x0UL);
  1479. }
  1480. /* Spitfire Errata #32 workaround */
  1481. /* NOTE: Always runs on spitfire, so no
  1482. * cheetah+ page size encodings.
  1483. */
  1484. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1485. "flush %%g6"
  1486. : /* No outputs */
  1487. : "r" (0),
  1488. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1489. if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
  1490. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1491. "membar #Sync"
  1492. : /* no outputs */
  1493. : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
  1494. spitfire_put_itlb_data(i, 0x0UL);
  1495. }
  1496. }
  1497. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1498. cheetah_flush_dtlb_all();
  1499. cheetah_flush_itlb_all();
  1500. }
  1501. __asm__ __volatile__("wrpr %0, 0, %%pstate"
  1502. : : "r" (pstate));
  1503. }
  1504. #ifdef CONFIG_MEMORY_HOTPLUG
  1505. void online_page(struct page *page)
  1506. {
  1507. ClearPageReserved(page);
  1508. set_page_count(page, 0);
  1509. free_cold_page(page);
  1510. totalram_pages++;
  1511. num_physpages++;
  1512. }
  1513. int remove_memory(u64 start, u64 size)
  1514. {
  1515. return -EINVAL;
  1516. }
  1517. #endif /* CONFIG_MEMORY_HOTPLUG */