1 // SPDX-License-Identifier: GPL-2.0
3 * arch/sparc64/mm/init.c
5 * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9 #include <linux/extable.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/init.h>
14 #include <linux/memblock.h>
16 #include <linux/hugetlb.h>
17 #include <linux/initrd.h>
18 #include <linux/swap.h>
19 #include <linux/pagemap.h>
20 #include <linux/poison.h>
22 #include <linux/seq_file.h>
23 #include <linux/kprobes.h>
24 #include <linux/cache.h>
25 #include <linux/sort.h>
26 #include <linux/ioport.h>
27 #include <linux/percpu.h>
28 #include <linux/mmzone.h>
29 #include <linux/gfp.h>
33 #include <asm/pgalloc.h>
34 #include <asm/pgtable.h>
35 #include <asm/oplib.h>
36 #include <asm/iommu.h>
38 #include <linux/uaccess.h>
39 #include <asm/mmu_context.h>
40 #include <asm/tlbflush.h>
42 #include <asm/starfire.h>
44 #include <asm/spitfire.h>
45 #include <asm/sections.h>
47 #include <asm/hypervisor.h>
49 #include <asm/mdesc.h>
50 #include <asm/cpudata.h>
51 #include <asm/setup.h>
56 unsigned long kern_linear_pte_xor[4] __read_mostly;
57 static unsigned long page_cache4v_flag;
59 /* A bitmap, two bits for every 256MB of physical memory. These two
60 * bits determine what page size we use for kernel linear
61 * translations. They form an index into kern_linear_pte_xor[]. The
62 * value in the indexed slot is XOR'd with the TLB miss virtual
63 * address to form the resulting TTE. The mapping is:
70 * All sun4v chips support 256MB pages. Only SPARC-T4 and later
71 * support 2GB pages, and hopefully future cpus will support the 16GB
72 * pages as well. For slots 2 and 3, we encode a 256MB TTE xor there
73 * if these larger page sizes are not supported by the cpu.
75 * It would be nice to determine this from the machine description
76 * 'cpu' properties, but we need to have this table setup before the
77 * MDESC is initialized.
80 #ifndef CONFIG_DEBUG_PAGEALLOC
81 /* A special kernel TSB for 4MB, 256MB, 2GB and 16GB linear mappings.
82 * Space is allocated for this right after the trap table in
83 * arch/sparc64/kernel/head.S
85 extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
87 extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
89 static unsigned long cpu_pgsz_mask;
91 #define MAX_BANKS 1024
93 static struct linux_prom64_registers pavail[MAX_BANKS];
94 static int pavail_ents;
96 u64 numa_latency[MAX_NUMNODES][MAX_NUMNODES];
98 static int cmp_p64(const void *a, const void *b)
100 const struct linux_prom64_registers *x = a, *y = b;
102 if (x->phys_addr > y->phys_addr)
104 if (x->phys_addr < y->phys_addr)
109 static void __init read_obp_memory(const char *property,
110 struct linux_prom64_registers *regs,
113 phandle node = prom_finddevice("/memory");
114 int prop_size = prom_getproplen(node, property);
117 ents = prop_size / sizeof(struct linux_prom64_registers);
118 if (ents > MAX_BANKS) {
119 prom_printf("The machine has more %s property entries than "
120 "this kernel can support (%d).\n",
121 property, MAX_BANKS);
125 ret = prom_getproperty(node, property, (char *) regs, prop_size);
127 prom_printf("Couldn't get %s property from /memory.\n",
132 /* Sanitize what we got from the firmware, by page aligning
135 for (i = 0; i < ents; i++) {
136 unsigned long base, size;
138 base = regs[i].phys_addr;
139 size = regs[i].reg_size;
142 if (base & ~PAGE_MASK) {
143 unsigned long new_base = PAGE_ALIGN(base);
145 size -= new_base - base;
146 if ((long) size < 0L)
151 /* If it is empty, simply get rid of it.
152 * This simplifies the logic of the other
153 * functions that process these arrays.
155 memmove(®s[i], ®s[i + 1],
156 (ents - i - 1) * sizeof(regs[0]));
161 regs[i].phys_addr = base;
162 regs[i].reg_size = size;
167 sort(regs, ents, sizeof(struct linux_prom64_registers),
171 /* Kernel physical address base and size in bytes. */
172 unsigned long kern_base __read_mostly;
173 unsigned long kern_size __read_mostly;
175 /* Initial ramdisk setup */
176 extern unsigned long sparc_ramdisk_image64;
177 extern unsigned int sparc_ramdisk_image;
178 extern unsigned int sparc_ramdisk_size;
180 struct page *mem_map_zero __read_mostly;
181 EXPORT_SYMBOL(mem_map_zero);
183 unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
185 unsigned long sparc64_kern_pri_context __read_mostly;
186 unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
187 unsigned long sparc64_kern_sec_context __read_mostly;
189 int num_kernel_image_mappings;
191 #ifdef CONFIG_DEBUG_DCFLUSH
192 atomic_t dcpage_flushes = ATOMIC_INIT(0);
194 atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
198 inline void flush_dcache_page_impl(struct page *page)
200 BUG_ON(tlb_type == hypervisor);
201 #ifdef CONFIG_DEBUG_DCFLUSH
202 atomic_inc(&dcpage_flushes);
205 #ifdef DCACHE_ALIASING_POSSIBLE
206 __flush_dcache_page(page_address(page),
207 ((tlb_type == spitfire) &&
208 page_mapping_file(page) != NULL));
210 if (page_mapping_file(page) != NULL &&
211 tlb_type == spitfire)
212 __flush_icache_page(__pa(page_address(page)));
216 #define PG_dcache_dirty PG_arch_1
217 #define PG_dcache_cpu_shift 32UL
218 #define PG_dcache_cpu_mask \
219 ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
221 #define dcache_dirty_cpu(page) \
222 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
224 static inline void set_dcache_dirty(struct page *page, int this_cpu)
226 unsigned long mask = this_cpu;
227 unsigned long non_cpu_bits;
229 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
230 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
232 __asm__ __volatile__("1:\n\t"
234 "and %%g7, %1, %%g1\n\t"
235 "or %%g1, %0, %%g1\n\t"
236 "casx [%2], %%g7, %%g1\n\t"
238 "bne,pn %%xcc, 1b\n\t"
241 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
245 static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
247 unsigned long mask = (1UL << PG_dcache_dirty);
249 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
252 "srlx %%g7, %4, %%g1\n\t"
253 "and %%g1, %3, %%g1\n\t"
255 "bne,pn %%icc, 2f\n\t"
256 " andn %%g7, %1, %%g1\n\t"
257 "casx [%2], %%g7, %%g1\n\t"
259 "bne,pn %%xcc, 1b\n\t"
263 : "r" (cpu), "r" (mask), "r" (&page->flags),
264 "i" (PG_dcache_cpu_mask),
265 "i" (PG_dcache_cpu_shift)
269 static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
271 unsigned long tsb_addr = (unsigned long) ent;
273 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
274 tsb_addr = __pa(tsb_addr);
276 __tsb_insert(tsb_addr, tag, pte);
279 unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
281 static void flush_dcache(unsigned long pfn)
285 page = pfn_to_page(pfn);
287 unsigned long pg_flags;
289 pg_flags = page->flags;
290 if (pg_flags & (1UL << PG_dcache_dirty)) {
291 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
293 int this_cpu = get_cpu();
295 /* This is just to optimize away some function calls
299 flush_dcache_page_impl(page);
301 smp_flush_dcache_page_impl(page, cpu);
303 clear_dcache_dirty_cpu(page, cpu);
310 /* mm->context.lock must be held */
311 static void __update_mmu_tsb_insert(struct mm_struct *mm, unsigned long tsb_index,
312 unsigned long tsb_hash_shift, unsigned long address,
315 struct tsb *tsb = mm->context.tsb_block[tsb_index].tsb;
321 tsb += ((address >> tsb_hash_shift) &
322 (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
323 tag = (address >> 22UL);
324 tsb_insert(tsb, tag, tte);
327 #ifdef CONFIG_HUGETLB_PAGE
328 static void __init add_huge_page_size(unsigned long size)
332 if (size_to_hstate(size))
335 order = ilog2(size) - PAGE_SHIFT;
336 hugetlb_add_hstate(order);
339 static int __init hugetlbpage_init(void)
341 add_huge_page_size(1UL << HPAGE_64K_SHIFT);
342 add_huge_page_size(1UL << HPAGE_SHIFT);
343 add_huge_page_size(1UL << HPAGE_256MB_SHIFT);
344 add_huge_page_size(1UL << HPAGE_2GB_SHIFT);
349 arch_initcall(hugetlbpage_init);
351 static void __init pud_huge_patch(void)
353 struct pud_huge_patch_entry *p;
356 p = &__pud_huge_patch;
358 *(unsigned int *)addr = p->insn;
360 __asm__ __volatile__("flush %0" : : "r" (addr));
363 bool __init arch_hugetlb_valid_size(unsigned long size)
365 unsigned int hugepage_shift = ilog2(size);
366 unsigned short hv_pgsz_idx;
367 unsigned int hv_pgsz_mask;
369 switch (hugepage_shift) {
370 case HPAGE_16GB_SHIFT:
371 hv_pgsz_mask = HV_PGSZ_MASK_16GB;
372 hv_pgsz_idx = HV_PGSZ_IDX_16GB;
375 case HPAGE_2GB_SHIFT:
376 hv_pgsz_mask = HV_PGSZ_MASK_2GB;
377 hv_pgsz_idx = HV_PGSZ_IDX_2GB;
379 case HPAGE_256MB_SHIFT:
380 hv_pgsz_mask = HV_PGSZ_MASK_256MB;
381 hv_pgsz_idx = HV_PGSZ_IDX_256MB;
384 hv_pgsz_mask = HV_PGSZ_MASK_4MB;
385 hv_pgsz_idx = HV_PGSZ_IDX_4MB;
387 case HPAGE_64K_SHIFT:
388 hv_pgsz_mask = HV_PGSZ_MASK_64K;
389 hv_pgsz_idx = HV_PGSZ_IDX_64K;
395 if ((hv_pgsz_mask & cpu_pgsz_mask) == 0U)
400 #endif /* CONFIG_HUGETLB_PAGE */
402 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
404 struct mm_struct *mm;
409 if (tlb_type != hypervisor) {
410 unsigned long pfn = pte_pfn(pte);
418 /* Don't insert a non-valid PTE into the TSB, we'll deadlock. */
419 if (!pte_accessible(mm, pte))
422 spin_lock_irqsave(&mm->context.lock, flags);
425 #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
426 if (mm->context.hugetlb_pte_count || mm->context.thp_pte_count) {
427 unsigned long hugepage_size = PAGE_SIZE;
429 if (is_vm_hugetlb_page(vma))
430 hugepage_size = huge_page_size(hstate_vma(vma));
432 if (hugepage_size >= PUD_SIZE) {
433 unsigned long mask = 0x1ffc00000UL;
435 /* Transfer bits [32:22] from address to resolve
438 pte_val(pte) &= ~mask;
439 pte_val(pte) |= (address & mask);
440 } else if (hugepage_size >= PMD_SIZE) {
441 /* We are fabricating 8MB pages using 4MB
444 pte_val(pte) |= (address & (1UL << REAL_HPAGE_SHIFT));
447 if (hugepage_size >= PMD_SIZE) {
448 __update_mmu_tsb_insert(mm, MM_TSB_HUGE,
449 REAL_HPAGE_SHIFT, address, pte_val(pte));
455 __update_mmu_tsb_insert(mm, MM_TSB_BASE, PAGE_SHIFT,
456 address, pte_val(pte));
458 spin_unlock_irqrestore(&mm->context.lock, flags);
461 void flush_dcache_page(struct page *page)
463 struct address_space *mapping;
466 if (tlb_type == hypervisor)
469 /* Do not bother with the expensive D-cache flush if it
470 * is merely the zero page. The 'bigcore' testcase in GDB
471 * causes this case to run millions of times.
473 if (page == ZERO_PAGE(0))
476 this_cpu = get_cpu();
478 mapping = page_mapping_file(page);
479 if (mapping && !mapping_mapped(mapping)) {
480 int dirty = test_bit(PG_dcache_dirty, &page->flags);
482 int dirty_cpu = dcache_dirty_cpu(page);
484 if (dirty_cpu == this_cpu)
486 smp_flush_dcache_page_impl(page, dirty_cpu);
488 set_dcache_dirty(page, this_cpu);
490 /* We could delay the flush for the !page_mapping
491 * case too. But that case is for exec env/arg
492 * pages and those are %99 certainly going to get
493 * faulted into the tlb (and thus flushed) anyways.
495 flush_dcache_page_impl(page);
501 EXPORT_SYMBOL(flush_dcache_page);
503 void __kprobes flush_icache_range(unsigned long start, unsigned long end)
505 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
506 if (tlb_type == spitfire) {
509 /* This code only runs on Spitfire cpus so this is
510 * why we can assume _PAGE_PADDR_4U.
512 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
513 unsigned long paddr, mask = _PAGE_PADDR_4U;
515 if (kaddr >= PAGE_OFFSET)
516 paddr = kaddr & mask;
518 pgd_t *pgdp = pgd_offset_k(kaddr);
519 p4d_t *p4dp = p4d_offset(pgdp, kaddr);
520 pud_t *pudp = pud_offset(p4dp, kaddr);
521 pmd_t *pmdp = pmd_offset(pudp, kaddr);
522 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
524 paddr = pte_val(*ptep) & mask;
526 __flush_icache_page(paddr);
530 EXPORT_SYMBOL(flush_icache_range);
532 void mmu_info(struct seq_file *m)
534 static const char *pgsz_strings[] = {
535 "8K", "64K", "512K", "4MB", "32MB",
536 "256MB", "2GB", "16GB",
540 if (tlb_type == cheetah)
541 seq_printf(m, "MMU Type\t: Cheetah\n");
542 else if (tlb_type == cheetah_plus)
543 seq_printf(m, "MMU Type\t: Cheetah+\n");
544 else if (tlb_type == spitfire)
545 seq_printf(m, "MMU Type\t: Spitfire\n");
546 else if (tlb_type == hypervisor)
547 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
549 seq_printf(m, "MMU Type\t: ???\n");
551 seq_printf(m, "MMU PGSZs\t: ");
553 for (i = 0; i < ARRAY_SIZE(pgsz_strings); i++) {
554 if (cpu_pgsz_mask & (1UL << i)) {
555 seq_printf(m, "%s%s",
556 printed ? "," : "", pgsz_strings[i]);
562 #ifdef CONFIG_DEBUG_DCFLUSH
563 seq_printf(m, "DCPageFlushes\t: %d\n",
564 atomic_read(&dcpage_flushes));
566 seq_printf(m, "DCPageFlushesXC\t: %d\n",
567 atomic_read(&dcpage_flushes_xcall));
568 #endif /* CONFIG_SMP */
569 #endif /* CONFIG_DEBUG_DCFLUSH */
572 struct linux_prom_translation prom_trans[512] __read_mostly;
573 unsigned int prom_trans_ents __read_mostly;
575 unsigned long kern_locked_tte_data;
577 /* The obp translations are saved based on 8k pagesize, since obp can
578 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
579 * HI_OBP_ADDRESS range are handled in ktlb.S.
581 static inline int in_obp_range(unsigned long vaddr)
583 return (vaddr >= LOW_OBP_ADDRESS &&
584 vaddr < HI_OBP_ADDRESS);
587 static int cmp_ptrans(const void *a, const void *b)
589 const struct linux_prom_translation *x = a, *y = b;
591 if (x->virt > y->virt)
593 if (x->virt < y->virt)
598 /* Read OBP translations property into 'prom_trans[]'. */
599 static void __init read_obp_translations(void)
601 int n, node, ents, first, last, i;
603 node = prom_finddevice("/virtual-memory");
604 n = prom_getproplen(node, "translations");
605 if (unlikely(n == 0 || n == -1)) {
606 prom_printf("prom_mappings: Couldn't get size.\n");
609 if (unlikely(n > sizeof(prom_trans))) {
610 prom_printf("prom_mappings: Size %d is too big.\n", n);
614 if ((n = prom_getproperty(node, "translations",
615 (char *)&prom_trans[0],
616 sizeof(prom_trans))) == -1) {
617 prom_printf("prom_mappings: Couldn't get property.\n");
621 n = n / sizeof(struct linux_prom_translation);
625 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
628 /* Now kick out all the non-OBP entries. */
629 for (i = 0; i < ents; i++) {
630 if (in_obp_range(prom_trans[i].virt))
634 for (; i < ents; i++) {
635 if (!in_obp_range(prom_trans[i].virt))
640 for (i = 0; i < (last - first); i++) {
641 struct linux_prom_translation *src = &prom_trans[i + first];
642 struct linux_prom_translation *dest = &prom_trans[i];
646 for (; i < ents; i++) {
647 struct linux_prom_translation *dest = &prom_trans[i];
648 dest->virt = dest->size = dest->data = 0x0UL;
651 prom_trans_ents = last - first;
653 if (tlb_type == spitfire) {
654 /* Clear diag TTE bits. */
655 for (i = 0; i < prom_trans_ents; i++)
656 prom_trans[i].data &= ~0x0003fe0000000000UL;
659 /* Force execute bit on. */
660 for (i = 0; i < prom_trans_ents; i++)
661 prom_trans[i].data |= (tlb_type == hypervisor ?
662 _PAGE_EXEC_4V : _PAGE_EXEC_4U);
665 static void __init hypervisor_tlb_lock(unsigned long vaddr,
669 unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
672 prom_printf("hypervisor_tlb_lock[%lx:%x:%lx:%lx]: "
673 "errors with %lx\n", vaddr, 0, pte, mmu, ret);
678 static unsigned long kern_large_tte(unsigned long paddr);
680 static void __init remap_kernel(void)
682 unsigned long phys_page, tte_vaddr, tte_data;
683 int i, tlb_ent = sparc64_highest_locked_tlbent();
685 tte_vaddr = (unsigned long) KERNBASE;
686 phys_page = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
687 tte_data = kern_large_tte(phys_page);
689 kern_locked_tte_data = tte_data;
691 /* Now lock us into the TLBs via Hypervisor or OBP. */
692 if (tlb_type == hypervisor) {
693 for (i = 0; i < num_kernel_image_mappings; i++) {
694 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
695 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
696 tte_vaddr += 0x400000;
697 tte_data += 0x400000;
700 for (i = 0; i < num_kernel_image_mappings; i++) {
701 prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
702 prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
703 tte_vaddr += 0x400000;
704 tte_data += 0x400000;
706 sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
708 if (tlb_type == cheetah_plus) {
709 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
710 CTX_CHEETAH_PLUS_NUC);
711 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
712 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
717 static void __init inherit_prom_mappings(void)
719 /* Now fixup OBP's idea about where we really are mapped. */
720 printk("Remapping the kernel... ");
725 void prom_world(int enter)
730 __asm__ __volatile__("flushw");
733 void __flush_dcache_range(unsigned long start, unsigned long end)
737 if (tlb_type == spitfire) {
740 for (va = start; va < end; va += 32) {
741 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
745 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
748 for (va = start; va < end; va += 32)
749 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
753 "i" (ASI_DCACHE_INVALIDATE));
756 EXPORT_SYMBOL(__flush_dcache_range);
758 /* get_new_mmu_context() uses "cache + 1". */
759 DEFINE_SPINLOCK(ctx_alloc_lock);
760 unsigned long tlb_context_cache = CTX_FIRST_VERSION;
761 #define MAX_CTX_NR (1UL << CTX_NR_BITS)
762 #define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
763 DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
764 DEFINE_PER_CPU(struct mm_struct *, per_cpu_secondary_mm) = {0};
766 static void mmu_context_wrap(void)
768 unsigned long old_ver = tlb_context_cache & CTX_VERSION_MASK;
769 unsigned long new_ver, new_ctx, old_ctx;
770 struct mm_struct *mm;
773 bitmap_zero(mmu_context_bmap, 1 << CTX_NR_BITS);
775 /* Reserve kernel context */
776 set_bit(0, mmu_context_bmap);
778 new_ver = (tlb_context_cache & CTX_VERSION_MASK) + CTX_FIRST_VERSION;
779 if (unlikely(new_ver == 0))
780 new_ver = CTX_FIRST_VERSION;
781 tlb_context_cache = new_ver;
784 * Make sure that any new mm that are added into per_cpu_secondary_mm,
785 * are going to go through get_new_mmu_context() path.
790 * Updated versions to current on those CPUs that had valid secondary
793 for_each_online_cpu(cpu) {
795 * If a new mm is stored after we took this mm from the array,
796 * it will go into get_new_mmu_context() path, because we
797 * already bumped the version in tlb_context_cache.
799 mm = per_cpu(per_cpu_secondary_mm, cpu);
801 if (unlikely(!mm || mm == &init_mm))
804 old_ctx = mm->context.sparc64_ctx_val;
805 if (likely((old_ctx & CTX_VERSION_MASK) == old_ver)) {
806 new_ctx = (old_ctx & ~CTX_VERSION_MASK) | new_ver;
807 set_bit(new_ctx & CTX_NR_MASK, mmu_context_bmap);
808 mm->context.sparc64_ctx_val = new_ctx;
813 /* Caller does TLB context flushing on local CPU if necessary.
814 * The caller also ensures that CTX_VALID(mm->context) is false.
816 * We must be careful about boundary cases so that we never
817 * let the user have CTX 0 (nucleus) or we ever use a CTX
818 * version of zero (and thus NO_CONTEXT would not be caught
819 * by version mis-match tests in mmu_context.h).
821 * Always invoked with interrupts disabled.
823 void get_new_mmu_context(struct mm_struct *mm)
825 unsigned long ctx, new_ctx;
826 unsigned long orig_pgsz_bits;
828 spin_lock(&ctx_alloc_lock);
830 /* wrap might have happened, test again if our context became valid */
831 if (unlikely(CTX_VALID(mm->context)))
833 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
834 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
835 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
836 if (new_ctx >= (1 << CTX_NR_BITS)) {
837 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
838 if (new_ctx >= ctx) {
843 if (mm->context.sparc64_ctx_val)
844 cpumask_clear(mm_cpumask(mm));
845 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
846 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
847 tlb_context_cache = new_ctx;
848 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
850 spin_unlock(&ctx_alloc_lock);
853 static int numa_enabled = 1;
854 static int numa_debug;
856 static int __init early_numa(char *p)
861 if (strstr(p, "off"))
864 if (strstr(p, "debug"))
869 early_param("numa", early_numa);
871 #define numadbg(f, a...) \
872 do { if (numa_debug) \
873 printk(KERN_INFO f, ## a); \
876 static void __init find_ramdisk(unsigned long phys_base)
878 #ifdef CONFIG_BLK_DEV_INITRD
879 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
880 unsigned long ramdisk_image;
882 /* Older versions of the bootloader only supported a
883 * 32-bit physical address for the ramdisk image
884 * location, stored at sparc_ramdisk_image. Newer
885 * SILO versions set sparc_ramdisk_image to zero and
886 * provide a full 64-bit physical address at
887 * sparc_ramdisk_image64.
889 ramdisk_image = sparc_ramdisk_image;
891 ramdisk_image = sparc_ramdisk_image64;
893 /* Another bootloader quirk. The bootloader normalizes
894 * the physical address to KERNBASE, so we have to
895 * factor that back out and add in the lowest valid
896 * physical page address to get the true physical address.
898 ramdisk_image -= KERNBASE;
899 ramdisk_image += phys_base;
901 numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
902 ramdisk_image, sparc_ramdisk_size);
904 initrd_start = ramdisk_image;
905 initrd_end = ramdisk_image + sparc_ramdisk_size;
907 memblock_reserve(initrd_start, sparc_ramdisk_size);
909 initrd_start += PAGE_OFFSET;
910 initrd_end += PAGE_OFFSET;
915 struct node_mem_mask {
919 static struct node_mem_mask node_masks[MAX_NUMNODES];
920 static int num_node_masks;
922 #ifdef CONFIG_NEED_MULTIPLE_NODES
924 struct mdesc_mlgroup {
931 static struct mdesc_mlgroup *mlgroups;
932 static int num_mlgroups;
934 int numa_cpu_lookup_table[NR_CPUS];
935 cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
937 struct mdesc_mblock {
940 u64 offset; /* RA-to-PA */
942 static struct mdesc_mblock *mblocks;
943 static int num_mblocks;
945 static struct mdesc_mblock * __init addr_to_mblock(unsigned long addr)
947 struct mdesc_mblock *m = NULL;
950 for (i = 0; i < num_mblocks; i++) {
953 if (addr >= m->base &&
954 addr < (m->base + m->size)) {
962 static u64 __init memblock_nid_range_sun4u(u64 start, u64 end, int *nid)
964 int prev_nid, new_nid;
966 prev_nid = NUMA_NO_NODE;
967 for ( ; start < end; start += PAGE_SIZE) {
968 for (new_nid = 0; new_nid < num_node_masks; new_nid++) {
969 struct node_mem_mask *p = &node_masks[new_nid];
971 if ((start & p->mask) == p->match) {
972 if (prev_nid == NUMA_NO_NODE)
978 if (new_nid == num_node_masks) {
980 WARN_ONCE(1, "addr[%Lx] doesn't match a NUMA node rule. Some memory will be owned by node 0.",
985 if (prev_nid != new_nid)
990 return start > end ? end : start;
993 static u64 __init memblock_nid_range(u64 start, u64 end, int *nid)
995 u64 ret_end, pa_start, m_mask, m_match, m_end;
996 struct mdesc_mblock *mblock;
999 if (tlb_type != hypervisor)
1000 return memblock_nid_range_sun4u(start, end, nid);
1002 mblock = addr_to_mblock(start);
1004 WARN_ONCE(1, "memblock_nid_range: Can't find mblock addr[%Lx]",
1012 pa_start = start + mblock->offset;
1016 for (_nid = 0; _nid < num_node_masks; _nid++) {
1017 struct node_mem_mask *const m = &node_masks[_nid];
1019 if ((pa_start & m->mask) == m->match) {
1026 if (num_node_masks == _nid) {
1027 /* We could not find NUMA group, so default to 0, but lets
1028 * search for latency group, so we could calculate the correct
1029 * end address that we return
1033 for (i = 0; i < num_mlgroups; i++) {
1034 struct mdesc_mlgroup *const m = &mlgroups[i];
1036 if ((pa_start & m->mask) == m->match) {
1043 if (i == num_mlgroups) {
1044 WARN_ONCE(1, "memblock_nid_range: Can't find latency group addr[%Lx]",
1053 * Each latency group has match and mask, and each memory block has an
1054 * offset. An address belongs to a latency group if its address matches
1055 * the following formula: ((addr + offset) & mask) == match
1056 * It is, however, slow to check every single page if it matches a
1057 * particular latency group. As optimization we calculate end value by
1058 * using bit arithmetics.
1060 m_end = m_match + (1ul << __ffs(m_mask)) - mblock->offset;
1061 m_end += pa_start & ~((1ul << fls64(m_mask)) - 1);
1062 ret_end = m_end > end ? end : m_end;
1070 /* This must be invoked after performing all of the necessary
1071 * memblock_set_node() calls for 'nid'. We need to be able to get
1072 * correct data from get_pfn_range_for_nid().
1074 static void __init allocate_node_data(int nid)
1076 struct pglist_data *p;
1077 unsigned long start_pfn, end_pfn;
1078 #ifdef CONFIG_NEED_MULTIPLE_NODES
1080 NODE_DATA(nid) = memblock_alloc_node(sizeof(struct pglist_data),
1081 SMP_CACHE_BYTES, nid);
1082 if (!NODE_DATA(nid)) {
1083 prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
1087 NODE_DATA(nid)->node_id = nid;
1092 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1093 p->node_start_pfn = start_pfn;
1094 p->node_spanned_pages = end_pfn - start_pfn;
1097 static void init_node_masks_nonnuma(void)
1099 #ifdef CONFIG_NEED_MULTIPLE_NODES
1103 numadbg("Initializing tables for non-numa.\n");
1105 node_masks[0].mask = 0;
1106 node_masks[0].match = 0;
1109 #ifdef CONFIG_NEED_MULTIPLE_NODES
1110 for (i = 0; i < NR_CPUS; i++)
1111 numa_cpu_lookup_table[i] = 0;
1113 cpumask_setall(&numa_cpumask_lookup_table[0]);
1117 #ifdef CONFIG_NEED_MULTIPLE_NODES
1118 struct pglist_data *node_data[MAX_NUMNODES];
1120 EXPORT_SYMBOL(numa_cpu_lookup_table);
1121 EXPORT_SYMBOL(numa_cpumask_lookup_table);
1122 EXPORT_SYMBOL(node_data);
1124 static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
1129 mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
1130 u64 target = mdesc_arc_target(md, arc);
1133 val = mdesc_get_property(md, target,
1134 "cfg-handle", NULL);
1135 if (val && *val == cfg_handle)
1141 static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
1144 u64 arc, candidate, best_latency = ~(u64)0;
1146 candidate = MDESC_NODE_NULL;
1147 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1148 u64 target = mdesc_arc_target(md, arc);
1149 const char *name = mdesc_node_name(md, target);
1152 if (strcmp(name, "pio-latency-group"))
1155 val = mdesc_get_property(md, target, "latency", NULL);
1159 if (*val < best_latency) {
1161 best_latency = *val;
1165 if (candidate == MDESC_NODE_NULL)
1168 return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
1171 int of_node_to_nid(struct device_node *dp)
1173 const struct linux_prom64_registers *regs;
1174 struct mdesc_handle *md;
1179 /* This is the right thing to do on currently supported
1180 * SUN4U NUMA platforms as well, as the PCI controller does
1181 * not sit behind any particular memory controller.
1186 regs = of_get_property(dp, "reg", NULL);
1190 cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
1196 mdesc_for_each_node_by_name(md, grp, "group") {
1197 if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
1209 static void __init add_node_ranges(void)
1211 struct memblock_region *reg;
1212 unsigned long prev_max;
1215 prev_max = memblock.memory.max;
1217 for_each_memblock(memory, reg) {
1218 unsigned long size = reg->size;
1219 unsigned long start, end;
1223 while (start < end) {
1224 unsigned long this_end;
1227 this_end = memblock_nid_range(start, end, &nid);
1229 numadbg("Setting memblock NUMA node nid[%d] "
1230 "start[%lx] end[%lx]\n",
1231 nid, start, this_end);
1233 memblock_set_node(start, this_end - start,
1234 &memblock.memory, nid);
1235 if (memblock.memory.max != prev_max)
1236 goto memblock_resized;
1242 static int __init grab_mlgroups(struct mdesc_handle *md)
1244 unsigned long paddr;
1248 mdesc_for_each_node_by_name(md, node, "memory-latency-group")
1253 paddr = memblock_phys_alloc(count * sizeof(struct mdesc_mlgroup),
1258 mlgroups = __va(paddr);
1259 num_mlgroups = count;
1262 mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
1263 struct mdesc_mlgroup *m = &mlgroups[count++];
1268 val = mdesc_get_property(md, node, "latency", NULL);
1270 val = mdesc_get_property(md, node, "address-match", NULL);
1272 val = mdesc_get_property(md, node, "address-mask", NULL);
1275 numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
1276 "match[%llx] mask[%llx]\n",
1277 count - 1, m->node, m->latency, m->match, m->mask);
1283 static int __init grab_mblocks(struct mdesc_handle *md)
1285 unsigned long paddr;
1289 mdesc_for_each_node_by_name(md, node, "mblock")
1294 paddr = memblock_phys_alloc(count * sizeof(struct mdesc_mblock),
1299 mblocks = __va(paddr);
1300 num_mblocks = count;
1303 mdesc_for_each_node_by_name(md, node, "mblock") {
1304 struct mdesc_mblock *m = &mblocks[count++];
1307 val = mdesc_get_property(md, node, "base", NULL);
1309 val = mdesc_get_property(md, node, "size", NULL);
1311 val = mdesc_get_property(md, node,
1312 "address-congruence-offset", NULL);
1314 /* The address-congruence-offset property is optional.
1315 * Explicity zero it be identifty this.
1322 numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
1323 count - 1, m->base, m->size, m->offset);
1329 static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
1330 u64 grp, cpumask_t *mask)
1334 cpumask_clear(mask);
1336 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
1337 u64 target = mdesc_arc_target(md, arc);
1338 const char *name = mdesc_node_name(md, target);
1341 if (strcmp(name, "cpu"))
1343 id = mdesc_get_property(md, target, "id", NULL);
1344 if (*id < nr_cpu_ids)
1345 cpumask_set_cpu(*id, mask);
1349 static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
1353 for (i = 0; i < num_mlgroups; i++) {
1354 struct mdesc_mlgroup *m = &mlgroups[i];
1355 if (m->node == node)
1361 int __node_distance(int from, int to)
1363 if ((from >= MAX_NUMNODES) || (to >= MAX_NUMNODES)) {
1364 pr_warn("Returning default NUMA distance value for %d->%d\n",
1366 return (from == to) ? LOCAL_DISTANCE : REMOTE_DISTANCE;
1368 return numa_latency[from][to];
1370 EXPORT_SYMBOL(__node_distance);
1372 static int __init find_best_numa_node_for_mlgroup(struct mdesc_mlgroup *grp)
1376 for (i = 0; i < MAX_NUMNODES; i++) {
1377 struct node_mem_mask *n = &node_masks[i];
1379 if ((grp->mask == n->mask) && (grp->match == n->match))
1385 static void __init find_numa_latencies_for_group(struct mdesc_handle *md,
1390 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1392 u64 target = mdesc_arc_target(md, arc);
1393 struct mdesc_mlgroup *m = find_mlgroup(target);
1397 tnode = find_best_numa_node_for_mlgroup(m);
1398 if (tnode == MAX_NUMNODES)
1400 numa_latency[index][tnode] = m->latency;
1404 static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
1407 struct mdesc_mlgroup *candidate = NULL;
1408 u64 arc, best_latency = ~(u64)0;
1409 struct node_mem_mask *n;
1411 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1412 u64 target = mdesc_arc_target(md, arc);
1413 struct mdesc_mlgroup *m = find_mlgroup(target);
1416 if (m->latency < best_latency) {
1418 best_latency = m->latency;
1424 if (num_node_masks != index) {
1425 printk(KERN_ERR "Inconsistent NUMA state, "
1426 "index[%d] != num_node_masks[%d]\n",
1427 index, num_node_masks);
1431 n = &node_masks[num_node_masks++];
1433 n->mask = candidate->mask;
1434 n->match = candidate->match;
1436 numadbg("NUMA NODE[%d]: mask[%lx] match[%lx] (latency[%llx])\n",
1437 index, n->mask, n->match, candidate->latency);
1442 static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
1448 numa_parse_mdesc_group_cpus(md, grp, &mask);
1450 for_each_cpu(cpu, &mask)
1451 numa_cpu_lookup_table[cpu] = index;
1452 cpumask_copy(&numa_cpumask_lookup_table[index], &mask);
1455 printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
1456 for_each_cpu(cpu, &mask)
1461 return numa_attach_mlgroup(md, grp, index);
1464 static int __init numa_parse_mdesc(void)
1466 struct mdesc_handle *md = mdesc_grab();
1467 int i, j, err, count;
1470 node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
1471 if (node == MDESC_NODE_NULL) {
1476 err = grab_mblocks(md);
1480 err = grab_mlgroups(md);
1485 mdesc_for_each_node_by_name(md, node, "group") {
1486 err = numa_parse_mdesc_group(md, node, count);
1493 mdesc_for_each_node_by_name(md, node, "group") {
1494 find_numa_latencies_for_group(md, node, count);
1498 /* Normalize numa latency matrix according to ACPI SLIT spec. */
1499 for (i = 0; i < MAX_NUMNODES; i++) {
1500 u64 self_latency = numa_latency[i][i];
1502 for (j = 0; j < MAX_NUMNODES; j++) {
1503 numa_latency[i][j] =
1504 (numa_latency[i][j] * LOCAL_DISTANCE) /
1511 for (i = 0; i < num_node_masks; i++) {
1512 allocate_node_data(i);
1522 static int __init numa_parse_jbus(void)
1524 unsigned long cpu, index;
1526 /* NUMA node id is encoded in bits 36 and higher, and there is
1527 * a 1-to-1 mapping from CPU ID to NUMA node ID.
1530 for_each_present_cpu(cpu) {
1531 numa_cpu_lookup_table[cpu] = index;
1532 cpumask_copy(&numa_cpumask_lookup_table[index], cpumask_of(cpu));
1533 node_masks[index].mask = ~((1UL << 36UL) - 1UL);
1534 node_masks[index].match = cpu << 36UL;
1538 num_node_masks = index;
1542 for (index = 0; index < num_node_masks; index++) {
1543 allocate_node_data(index);
1544 node_set_online(index);
1550 static int __init numa_parse_sun4u(void)
1552 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1555 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
1556 if ((ver >> 32UL) == __JALAPENO_ID ||
1557 (ver >> 32UL) == __SERRANO_ID)
1558 return numa_parse_jbus();
1563 static int __init bootmem_init_numa(void)
1568 numadbg("bootmem_init_numa()\n");
1570 /* Some sane defaults for numa latency values */
1571 for (i = 0; i < MAX_NUMNODES; i++) {
1572 for (j = 0; j < MAX_NUMNODES; j++)
1573 numa_latency[i][j] = (i == j) ?
1574 LOCAL_DISTANCE : REMOTE_DISTANCE;
1578 if (tlb_type == hypervisor)
1579 err = numa_parse_mdesc();
1581 err = numa_parse_sun4u();
1588 static int bootmem_init_numa(void)
1595 static void __init bootmem_init_nonnuma(void)
1597 unsigned long top_of_ram = memblock_end_of_DRAM();
1598 unsigned long total_ram = memblock_phys_mem_size();
1600 numadbg("bootmem_init_nonnuma()\n");
1602 printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1603 top_of_ram, total_ram);
1604 printk(KERN_INFO "Memory hole size: %ldMB\n",
1605 (top_of_ram - total_ram) >> 20);
1607 init_node_masks_nonnuma();
1608 memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
1609 allocate_node_data(0);
1613 static unsigned long __init bootmem_init(unsigned long phys_base)
1615 unsigned long end_pfn;
1617 end_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
1618 max_pfn = max_low_pfn = end_pfn;
1619 min_low_pfn = (phys_base >> PAGE_SHIFT);
1621 if (bootmem_init_numa() < 0)
1622 bootmem_init_nonnuma();
1624 /* Dump memblock with node info. */
1625 memblock_dump_all();
1627 /* XXX cpu notifier XXX */
1629 sparse_memory_present_with_active_regions(MAX_NUMNODES);
1635 static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1636 static int pall_ents __initdata;
1638 static unsigned long max_phys_bits = 40;
1640 bool kern_addr_valid(unsigned long addr)
1648 if ((long)addr < 0L) {
1649 unsigned long pa = __pa(addr);
1651 if ((pa >> max_phys_bits) != 0UL)
1654 return pfn_valid(pa >> PAGE_SHIFT);
1657 if (addr >= (unsigned long) KERNBASE &&
1658 addr < (unsigned long)&_end)
1661 pgd = pgd_offset_k(addr);
1665 p4d = p4d_offset(pgd, addr);
1669 pud = pud_offset(p4d, addr);
1673 if (pud_large(*pud))
1674 return pfn_valid(pud_pfn(*pud));
1676 pmd = pmd_offset(pud, addr);
1680 if (pmd_large(*pmd))
1681 return pfn_valid(pmd_pfn(*pmd));
1683 pte = pte_offset_kernel(pmd, addr);
1687 return pfn_valid(pte_pfn(*pte));
1689 EXPORT_SYMBOL(kern_addr_valid);
1691 static unsigned long __ref kernel_map_hugepud(unsigned long vstart,
1695 const unsigned long mask16gb = (1UL << 34) - 1UL;
1696 u64 pte_val = vstart;
1698 /* Each PUD is 8GB */
1699 if ((vstart & mask16gb) ||
1700 (vend - vstart <= mask16gb)) {
1701 pte_val ^= kern_linear_pte_xor[2];
1702 pud_val(*pud) = pte_val | _PAGE_PUD_HUGE;
1704 return vstart + PUD_SIZE;
1707 pte_val ^= kern_linear_pte_xor[3];
1708 pte_val |= _PAGE_PUD_HUGE;
1710 vend = vstart + mask16gb + 1UL;
1711 while (vstart < vend) {
1712 pud_val(*pud) = pte_val;
1714 pte_val += PUD_SIZE;
1721 static bool kernel_can_map_hugepud(unsigned long vstart, unsigned long vend,
1724 if (guard && !(vstart & ~PUD_MASK) && (vend - vstart) >= PUD_SIZE)
1730 static unsigned long __ref kernel_map_hugepmd(unsigned long vstart,
1734 const unsigned long mask256mb = (1UL << 28) - 1UL;
1735 const unsigned long mask2gb = (1UL << 31) - 1UL;
1736 u64 pte_val = vstart;
1738 /* Each PMD is 8MB */
1739 if ((vstart & mask256mb) ||
1740 (vend - vstart <= mask256mb)) {
1741 pte_val ^= kern_linear_pte_xor[0];
1742 pmd_val(*pmd) = pte_val | _PAGE_PMD_HUGE;
1744 return vstart + PMD_SIZE;
1747 if ((vstart & mask2gb) ||
1748 (vend - vstart <= mask2gb)) {
1749 pte_val ^= kern_linear_pte_xor[1];
1750 pte_val |= _PAGE_PMD_HUGE;
1751 vend = vstart + mask256mb + 1UL;
1753 pte_val ^= kern_linear_pte_xor[2];
1754 pte_val |= _PAGE_PMD_HUGE;
1755 vend = vstart + mask2gb + 1UL;
1758 while (vstart < vend) {
1759 pmd_val(*pmd) = pte_val;
1761 pte_val += PMD_SIZE;
1769 static bool kernel_can_map_hugepmd(unsigned long vstart, unsigned long vend,
1772 if (guard && !(vstart & ~PMD_MASK) && (vend - vstart) >= PMD_SIZE)
1778 static unsigned long __ref kernel_map_range(unsigned long pstart,
1779 unsigned long pend, pgprot_t prot,
1782 unsigned long vstart = PAGE_OFFSET + pstart;
1783 unsigned long vend = PAGE_OFFSET + pend;
1784 unsigned long alloc_bytes = 0UL;
1786 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
1787 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
1792 while (vstart < vend) {
1793 unsigned long this_end, paddr = __pa(vstart);
1794 pgd_t *pgd = pgd_offset_k(vstart);
1800 if (pgd_none(*pgd)) {
1803 new = memblock_alloc_from(PAGE_SIZE, PAGE_SIZE,
1807 alloc_bytes += PAGE_SIZE;
1808 pgd_populate(&init_mm, pgd, new);
1811 p4d = p4d_offset(pgd, vstart);
1812 if (p4d_none(*p4d)) {
1815 new = memblock_alloc_from(PAGE_SIZE, PAGE_SIZE,
1819 alloc_bytes += PAGE_SIZE;
1820 p4d_populate(&init_mm, p4d, new);
1823 pud = pud_offset(p4d, vstart);
1824 if (pud_none(*pud)) {
1827 if (kernel_can_map_hugepud(vstart, vend, use_huge)) {
1828 vstart = kernel_map_hugepud(vstart, vend, pud);
1831 new = memblock_alloc_from(PAGE_SIZE, PAGE_SIZE,
1835 alloc_bytes += PAGE_SIZE;
1836 pud_populate(&init_mm, pud, new);
1839 pmd = pmd_offset(pud, vstart);
1840 if (pmd_none(*pmd)) {
1843 if (kernel_can_map_hugepmd(vstart, vend, use_huge)) {
1844 vstart = kernel_map_hugepmd(vstart, vend, pmd);
1847 new = memblock_alloc_from(PAGE_SIZE, PAGE_SIZE,
1851 alloc_bytes += PAGE_SIZE;
1852 pmd_populate_kernel(&init_mm, pmd, new);
1855 pte = pte_offset_kernel(pmd, vstart);
1856 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1857 if (this_end > vend)
1860 while (vstart < this_end) {
1861 pte_val(*pte) = (paddr | pgprot_val(prot));
1863 vstart += PAGE_SIZE;
1872 panic("%s: Failed to allocate %lu bytes align=%lx from=%lx\n",
1873 __func__, PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1877 static void __init flush_all_kernel_tsbs(void)
1881 for (i = 0; i < KERNEL_TSB_NENTRIES; i++) {
1882 struct tsb *ent = &swapper_tsb[i];
1884 ent->tag = (1UL << TSB_TAG_INVALID_BIT);
1886 #ifndef CONFIG_DEBUG_PAGEALLOC
1887 for (i = 0; i < KERNEL_TSB4M_NENTRIES; i++) {
1888 struct tsb *ent = &swapper_4m_tsb[i];
1890 ent->tag = (1UL << TSB_TAG_INVALID_BIT);
1895 extern unsigned int kvmap_linear_patch[1];
1897 static void __init kernel_physical_mapping_init(void)
1899 unsigned long i, mem_alloced = 0UL;
1900 bool use_huge = true;
1902 #ifdef CONFIG_DEBUG_PAGEALLOC
1905 for (i = 0; i < pall_ents; i++) {
1906 unsigned long phys_start, phys_end;
1908 phys_start = pall[i].phys_addr;
1909 phys_end = phys_start + pall[i].reg_size;
1911 mem_alloced += kernel_map_range(phys_start, phys_end,
1912 PAGE_KERNEL, use_huge);
1915 printk("Allocated %ld bytes for kernel page tables.\n",
1918 kvmap_linear_patch[0] = 0x01000000; /* nop */
1919 flushi(&kvmap_linear_patch[0]);
1921 flush_all_kernel_tsbs();
1926 #ifdef CONFIG_DEBUG_PAGEALLOC
1927 void __kernel_map_pages(struct page *page, int numpages, int enable)
1929 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1930 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1932 kernel_map_range(phys_start, phys_end,
1933 (enable ? PAGE_KERNEL : __pgprot(0)), false);
1935 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1936 PAGE_OFFSET + phys_end);
1938 /* we should perform an IPI and flush all tlbs,
1939 * but that can deadlock->flush only current cpu.
1941 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1942 PAGE_OFFSET + phys_end);
1946 unsigned long __init find_ecache_flush_span(unsigned long size)
1950 for (i = 0; i < pavail_ents; i++) {
1951 if (pavail[i].reg_size >= size)
1952 return pavail[i].phys_addr;
1958 unsigned long PAGE_OFFSET;
1959 EXPORT_SYMBOL(PAGE_OFFSET);
1961 unsigned long VMALLOC_END = 0x0000010000000000UL;
1962 EXPORT_SYMBOL(VMALLOC_END);
1964 unsigned long sparc64_va_hole_top = 0xfffff80000000000UL;
1965 unsigned long sparc64_va_hole_bottom = 0x0000080000000000UL;
1967 static void __init setup_page_offset(void)
1969 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1970 /* Cheetah/Panther support a full 64-bit virtual
1971 * address, so we can use all that our page tables
1974 sparc64_va_hole_top = 0xfff0000000000000UL;
1975 sparc64_va_hole_bottom = 0x0010000000000000UL;
1978 } else if (tlb_type == hypervisor) {
1979 switch (sun4v_chip_type) {
1980 case SUN4V_CHIP_NIAGARA1:
1981 case SUN4V_CHIP_NIAGARA2:
1982 /* T1 and T2 support 48-bit virtual addresses. */
1983 sparc64_va_hole_top = 0xffff800000000000UL;
1984 sparc64_va_hole_bottom = 0x0000800000000000UL;
1988 case SUN4V_CHIP_NIAGARA3:
1989 /* T3 supports 48-bit virtual addresses. */
1990 sparc64_va_hole_top = 0xffff800000000000UL;
1991 sparc64_va_hole_bottom = 0x0000800000000000UL;
1995 case SUN4V_CHIP_NIAGARA4:
1996 case SUN4V_CHIP_NIAGARA5:
1997 case SUN4V_CHIP_SPARC64X:
1998 case SUN4V_CHIP_SPARC_M6:
1999 /* T4 and later support 52-bit virtual addresses. */
2000 sparc64_va_hole_top = 0xfff8000000000000UL;
2001 sparc64_va_hole_bottom = 0x0008000000000000UL;
2004 case SUN4V_CHIP_SPARC_M7:
2005 case SUN4V_CHIP_SPARC_SN:
2006 /* M7 and later support 52-bit virtual addresses. */
2007 sparc64_va_hole_top = 0xfff8000000000000UL;
2008 sparc64_va_hole_bottom = 0x0008000000000000UL;
2011 case SUN4V_CHIP_SPARC_M8:
2013 /* M8 and later support 54-bit virtual addresses.
2014 * However, restricting M8 and above VA bits to 53
2015 * as 4-level page table cannot support more than
2018 sparc64_va_hole_top = 0xfff0000000000000UL;
2019 sparc64_va_hole_bottom = 0x0010000000000000UL;
2025 if (max_phys_bits > MAX_PHYS_ADDRESS_BITS) {
2026 prom_printf("MAX_PHYS_ADDRESS_BITS is too small, need %lu\n",
2031 PAGE_OFFSET = sparc64_va_hole_top;
2032 VMALLOC_END = ((sparc64_va_hole_bottom >> 1) +
2033 (sparc64_va_hole_bottom >> 2));
2035 pr_info("MM: PAGE_OFFSET is 0x%016lx (max_phys_bits == %lu)\n",
2036 PAGE_OFFSET, max_phys_bits);
2037 pr_info("MM: VMALLOC [0x%016lx --> 0x%016lx]\n",
2038 VMALLOC_START, VMALLOC_END);
2039 pr_info("MM: VMEMMAP [0x%016lx --> 0x%016lx]\n",
2040 VMEMMAP_BASE, VMEMMAP_BASE << 1);
2043 static void __init tsb_phys_patch(void)
2045 struct tsb_ldquad_phys_patch_entry *pquad;
2046 struct tsb_phys_patch_entry *p;
2048 pquad = &__tsb_ldquad_phys_patch;
2049 while (pquad < &__tsb_ldquad_phys_patch_end) {
2050 unsigned long addr = pquad->addr;
2052 if (tlb_type == hypervisor)
2053 *(unsigned int *) addr = pquad->sun4v_insn;
2055 *(unsigned int *) addr = pquad->sun4u_insn;
2057 __asm__ __volatile__("flush %0"
2064 p = &__tsb_phys_patch;
2065 while (p < &__tsb_phys_patch_end) {
2066 unsigned long addr = p->addr;
2068 *(unsigned int *) addr = p->insn;
2070 __asm__ __volatile__("flush %0"
2078 /* Don't mark as init, we give this to the Hypervisor. */
2079 #ifndef CONFIG_DEBUG_PAGEALLOC
2080 #define NUM_KTSB_DESCR 2
2082 #define NUM_KTSB_DESCR 1
2084 static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
2086 /* The swapper TSBs are loaded with a base sequence of:
2088 * sethi %uhi(SYMBOL), REG1
2089 * sethi %hi(SYMBOL), REG2
2090 * or REG1, %ulo(SYMBOL), REG1
2091 * or REG2, %lo(SYMBOL), REG2
2092 * sllx REG1, 32, REG1
2093 * or REG1, REG2, REG1
2095 * When we use physical addressing for the TSB accesses, we patch the
2096 * first four instructions in the above sequence.
2099 static void patch_one_ktsb_phys(unsigned int *start, unsigned int *end, unsigned long pa)
2101 unsigned long high_bits, low_bits;
2103 high_bits = (pa >> 32) & 0xffffffff;
2104 low_bits = (pa >> 0) & 0xffffffff;
2106 while (start < end) {
2107 unsigned int *ia = (unsigned int *)(unsigned long)*start;
2109 ia[0] = (ia[0] & ~0x3fffff) | (high_bits >> 10);
2110 __asm__ __volatile__("flush %0" : : "r" (ia));
2112 ia[1] = (ia[1] & ~0x3fffff) | (low_bits >> 10);
2113 __asm__ __volatile__("flush %0" : : "r" (ia + 1));
2115 ia[2] = (ia[2] & ~0x1fff) | (high_bits & 0x3ff);
2116 __asm__ __volatile__("flush %0" : : "r" (ia + 2));
2118 ia[3] = (ia[3] & ~0x1fff) | (low_bits & 0x3ff);
2119 __asm__ __volatile__("flush %0" : : "r" (ia + 3));
2125 static void ktsb_phys_patch(void)
2127 extern unsigned int __swapper_tsb_phys_patch;
2128 extern unsigned int __swapper_tsb_phys_patch_end;
2129 unsigned long ktsb_pa;
2131 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
2132 patch_one_ktsb_phys(&__swapper_tsb_phys_patch,
2133 &__swapper_tsb_phys_patch_end, ktsb_pa);
2134 #ifndef CONFIG_DEBUG_PAGEALLOC
2136 extern unsigned int __swapper_4m_tsb_phys_patch;
2137 extern unsigned int __swapper_4m_tsb_phys_patch_end;
2138 ktsb_pa = (kern_base +
2139 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
2140 patch_one_ktsb_phys(&__swapper_4m_tsb_phys_patch,
2141 &__swapper_4m_tsb_phys_patch_end, ktsb_pa);
2146 static void __init sun4v_ktsb_init(void)
2148 unsigned long ktsb_pa;
2150 /* First KTSB for PAGE_SIZE mappings. */
2151 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
2153 switch (PAGE_SIZE) {
2156 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
2157 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
2161 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
2162 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
2166 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
2167 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
2170 case 4 * 1024 * 1024:
2171 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
2172 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
2176 ktsb_descr[0].assoc = 1;
2177 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
2178 ktsb_descr[0].ctx_idx = 0;
2179 ktsb_descr[0].tsb_base = ktsb_pa;
2180 ktsb_descr[0].resv = 0;
2182 #ifndef CONFIG_DEBUG_PAGEALLOC
2183 /* Second KTSB for 4MB/256MB/2GB/16GB mappings. */
2184 ktsb_pa = (kern_base +
2185 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
2187 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
2188 ktsb_descr[1].pgsz_mask = ((HV_PGSZ_MASK_4MB |
2189 HV_PGSZ_MASK_256MB |
2191 HV_PGSZ_MASK_16GB) &
2193 ktsb_descr[1].assoc = 1;
2194 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
2195 ktsb_descr[1].ctx_idx = 0;
2196 ktsb_descr[1].tsb_base = ktsb_pa;
2197 ktsb_descr[1].resv = 0;
2201 void sun4v_ktsb_register(void)
2203 unsigned long pa, ret;
2205 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
2207 ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
2209 prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
2210 "errors with %lx\n", pa, ret);
2215 static void __init sun4u_linear_pte_xor_finalize(void)
2217 #ifndef CONFIG_DEBUG_PAGEALLOC
2218 /* This is where we would add Panther support for
2219 * 32MB and 256MB pages.
2224 static void __init sun4v_linear_pte_xor_finalize(void)
2226 unsigned long pagecv_flag;
2228 /* Bit 9 of TTE is no longer CV bit on M7 processor and it instead
2229 * enables MCD error. Do not set bit 9 on M7 processor.
2231 switch (sun4v_chip_type) {
2232 case SUN4V_CHIP_SPARC_M7:
2233 case SUN4V_CHIP_SPARC_M8:
2234 case SUN4V_CHIP_SPARC_SN:
2238 pagecv_flag = _PAGE_CV_4V;
2241 #ifndef CONFIG_DEBUG_PAGEALLOC
2242 if (cpu_pgsz_mask & HV_PGSZ_MASK_256MB) {
2243 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
2245 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | pagecv_flag |
2246 _PAGE_P_4V | _PAGE_W_4V);
2248 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
2251 if (cpu_pgsz_mask & HV_PGSZ_MASK_2GB) {
2252 kern_linear_pte_xor[2] = (_PAGE_VALID | _PAGE_SZ2GB_4V) ^
2254 kern_linear_pte_xor[2] |= (_PAGE_CP_4V | pagecv_flag |
2255 _PAGE_P_4V | _PAGE_W_4V);
2257 kern_linear_pte_xor[2] = kern_linear_pte_xor[1];
2260 if (cpu_pgsz_mask & HV_PGSZ_MASK_16GB) {
2261 kern_linear_pte_xor[3] = (_PAGE_VALID | _PAGE_SZ16GB_4V) ^
2263 kern_linear_pte_xor[3] |= (_PAGE_CP_4V | pagecv_flag |
2264 _PAGE_P_4V | _PAGE_W_4V);
2266 kern_linear_pte_xor[3] = kern_linear_pte_xor[2];
2271 /* paging_init() sets up the page tables */
2273 static unsigned long last_valid_pfn;
2275 static void sun4u_pgprot_init(void);
2276 static void sun4v_pgprot_init(void);
2278 #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
2279 #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
2280 #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
2281 #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
2282 #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
2283 #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
2285 /* We need to exclude reserved regions. This exclusion will include
2286 * vmlinux and initrd. To be more precise the initrd size could be used to
2287 * compute a new lower limit because it is freed later during initialization.
2289 static void __init reduce_memory(phys_addr_t limit_ram)
2291 limit_ram += memblock_reserved_size();
2292 memblock_enforce_memory_limit(limit_ram);
2295 void __init paging_init(void)
2297 unsigned long end_pfn, shift, phys_base;
2298 unsigned long real_end, i;
2300 setup_page_offset();
2302 /* These build time checkes make sure that the dcache_dirty_cpu()
2303 * page->flags usage will work.
2305 * When a page gets marked as dcache-dirty, we store the
2306 * cpu number starting at bit 32 in the page->flags. Also,
2307 * functions like clear_dcache_dirty_cpu use the cpu mask
2308 * in 13-bit signed-immediate instruction fields.
2312 * Page flags must not reach into upper 32 bits that are used
2313 * for the cpu number
2315 BUILD_BUG_ON(NR_PAGEFLAGS > 32);
2318 * The bit fields placed in the high range must not reach below
2319 * the 32 bit boundary. Otherwise we cannot place the cpu field
2320 * at the 32 bit boundary.
2322 BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
2323 ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
2325 BUILD_BUG_ON(NR_CPUS > 4096);
2327 kern_base = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
2328 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
2330 /* Invalidate both kernel TSBs. */
2331 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
2332 #ifndef CONFIG_DEBUG_PAGEALLOC
2333 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
2336 /* TTE.cv bit on sparc v9 occupies the same position as TTE.mcde
2337 * bit on M7 processor. This is a conflicting usage of the same
2338 * bit. Enabling TTE.cv on M7 would turn on Memory Corruption
2339 * Detection error on all pages and this will lead to problems
2340 * later. Kernel does not run with MCD enabled and hence rest
2341 * of the required steps to fully configure memory corruption
2342 * detection are not taken. We need to ensure TTE.mcde is not
2343 * set on M7 processor. Compute the value of cacheability
2344 * flag for use later taking this into consideration.
2346 switch (sun4v_chip_type) {
2347 case SUN4V_CHIP_SPARC_M7:
2348 case SUN4V_CHIP_SPARC_M8:
2349 case SUN4V_CHIP_SPARC_SN:
2350 page_cache4v_flag = _PAGE_CP_4V;
2353 page_cache4v_flag = _PAGE_CACHE_4V;
2357 if (tlb_type == hypervisor)
2358 sun4v_pgprot_init();
2360 sun4u_pgprot_init();
2362 if (tlb_type == cheetah_plus ||
2363 tlb_type == hypervisor) {
2368 if (tlb_type == hypervisor)
2369 sun4v_patch_tlb_handlers();
2371 /* Find available physical memory...
2373 * Read it twice in order to work around a bug in openfirmware.
2374 * The call to grab this table itself can cause openfirmware to
2375 * allocate memory, which in turn can take away some space from
2376 * the list of available memory. Reading it twice makes sure
2377 * we really do get the final value.
2379 read_obp_translations();
2380 read_obp_memory("reg", &pall[0], &pall_ents);
2381 read_obp_memory("available", &pavail[0], &pavail_ents);
2382 read_obp_memory("available", &pavail[0], &pavail_ents);
2384 phys_base = 0xffffffffffffffffUL;
2385 for (i = 0; i < pavail_ents; i++) {
2386 phys_base = min(phys_base, pavail[i].phys_addr);
2387 memblock_add(pavail[i].phys_addr, pavail[i].reg_size);
2390 memblock_reserve(kern_base, kern_size);
2392 find_ramdisk(phys_base);
2394 if (cmdline_memory_size)
2395 reduce_memory(cmdline_memory_size);
2397 memblock_allow_resize();
2398 memblock_dump_all();
2400 set_bit(0, mmu_context_bmap);
2402 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
2404 real_end = (unsigned long)_end;
2405 num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << ILOG2_4MB);
2406 printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
2407 num_kernel_image_mappings);
2409 /* Set kernel pgd to upper alias so physical page computations
2412 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
2414 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
2416 inherit_prom_mappings();
2418 /* Ok, we can use our TLB miss and window trap handlers safely. */
2423 prom_build_devicetree();
2424 of_populate_present_mask();
2426 of_fill_in_cpu_data();
2429 if (tlb_type == hypervisor) {
2431 mdesc_populate_present_mask(cpu_all_mask);
2433 mdesc_fill_in_cpu_data(cpu_all_mask);
2435 mdesc_get_page_sizes(cpu_all_mask, &cpu_pgsz_mask);
2437 sun4v_linear_pte_xor_finalize();
2440 sun4v_ktsb_register();
2442 unsigned long impl, ver;
2444 cpu_pgsz_mask = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
2445 HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
2447 __asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
2448 impl = ((ver >> 32) & 0xffff);
2449 if (impl == PANTHER_IMPL)
2450 cpu_pgsz_mask |= (HV_PGSZ_MASK_32MB |
2451 HV_PGSZ_MASK_256MB);
2453 sun4u_linear_pte_xor_finalize();
2456 /* Flush the TLBs and the 4M TSB so that the updated linear
2457 * pte XOR settings are realized for all mappings.
2460 #ifndef CONFIG_DEBUG_PAGEALLOC
2461 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
2465 /* Setup bootmem... */
2466 last_valid_pfn = end_pfn = bootmem_init(phys_base);
2468 kernel_physical_mapping_init();
2471 unsigned long max_zone_pfns[MAX_NR_ZONES];
2473 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
2475 max_zone_pfns[ZONE_NORMAL] = end_pfn;
2477 free_area_init(max_zone_pfns);
2480 printk("Booting Linux...\n");
2483 int page_in_phys_avail(unsigned long paddr)
2489 for (i = 0; i < pavail_ents; i++) {
2490 unsigned long start, end;
2492 start = pavail[i].phys_addr;
2493 end = start + pavail[i].reg_size;
2495 if (paddr >= start && paddr < end)
2498 if (paddr >= kern_base && paddr < (kern_base + kern_size))
2500 #ifdef CONFIG_BLK_DEV_INITRD
2501 if (paddr >= __pa(initrd_start) &&
2502 paddr < __pa(PAGE_ALIGN(initrd_end)))
2509 static void __init register_page_bootmem_info(void)
2511 #ifdef CONFIG_NEED_MULTIPLE_NODES
2514 for_each_online_node(i)
2515 if (NODE_DATA(i)->node_spanned_pages)
2516 register_page_bootmem_info_node(NODE_DATA(i));
2519 void __init mem_init(void)
2521 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
2523 memblock_free_all();
2526 * Must be done after boot memory is put on freelist, because here we
2527 * might set fields in deferred struct pages that have not yet been
2528 * initialized, and memblock_free_all() initializes all the reserved
2529 * deferred pages for us.
2531 register_page_bootmem_info();
2534 * Set up the zero page, mark it reserved, so that page count
2535 * is not manipulated when freeing the page from user ptes.
2537 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
2538 if (mem_map_zero == NULL) {
2539 prom_printf("paging_init: Cannot alloc zero page.\n");
2542 mark_page_reserved(mem_map_zero);
2544 mem_init_print_info(NULL);
2546 if (tlb_type == cheetah || tlb_type == cheetah_plus)
2547 cheetah_ecache_flush_init();
2550 void free_initmem(void)
2552 unsigned long addr, initend;
2555 /* If the physical memory maps were trimmed by kernel command
2556 * line options, don't even try freeing this initmem stuff up.
2557 * The kernel image could have been in the trimmed out region
2558 * and if so the freeing below will free invalid page structs.
2560 if (cmdline_memory_size)
2564 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
2566 addr = PAGE_ALIGN((unsigned long)(__init_begin));
2567 initend = (unsigned long)(__init_end) & PAGE_MASK;
2568 for (; addr < initend; addr += PAGE_SIZE) {
2572 ((unsigned long) __va(kern_base)) -
2573 ((unsigned long) KERNBASE));
2574 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
2577 free_reserved_page(virt_to_page(page));
2581 pgprot_t PAGE_KERNEL __read_mostly;
2582 EXPORT_SYMBOL(PAGE_KERNEL);
2584 pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
2585 pgprot_t PAGE_COPY __read_mostly;
2587 pgprot_t PAGE_SHARED __read_mostly;
2588 EXPORT_SYMBOL(PAGE_SHARED);
2590 unsigned long pg_iobits __read_mostly;
2592 unsigned long _PAGE_IE __read_mostly;
2593 EXPORT_SYMBOL(_PAGE_IE);
2595 unsigned long _PAGE_E __read_mostly;
2596 EXPORT_SYMBOL(_PAGE_E);
2598 unsigned long _PAGE_CACHE __read_mostly;
2599 EXPORT_SYMBOL(_PAGE_CACHE);
2601 #ifdef CONFIG_SPARSEMEM_VMEMMAP
2602 int __meminit vmemmap_populate(unsigned long vstart, unsigned long vend,
2603 int node, struct vmem_altmap *altmap)
2605 unsigned long pte_base;
2607 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2608 _PAGE_CP_4U | _PAGE_CV_4U |
2609 _PAGE_P_4U | _PAGE_W_4U);
2610 if (tlb_type == hypervisor)
2611 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2612 page_cache4v_flag | _PAGE_P_4V | _PAGE_W_4V);
2614 pte_base |= _PAGE_PMD_HUGE;
2616 vstart = vstart & PMD_MASK;
2617 vend = ALIGN(vend, PMD_SIZE);
2618 for (; vstart < vend; vstart += PMD_SIZE) {
2619 pgd_t *pgd = vmemmap_pgd_populate(vstart, node);
2628 p4d = vmemmap_p4d_populate(pgd, vstart, node);
2632 pud = vmemmap_pud_populate(p4d, vstart, node);
2636 pmd = pmd_offset(pud, vstart);
2637 pte = pmd_val(*pmd);
2638 if (!(pte & _PAGE_VALID)) {
2639 void *block = vmemmap_alloc_block(PMD_SIZE, node);
2644 pmd_val(*pmd) = pte_base | __pa(block);
2651 void vmemmap_free(unsigned long start, unsigned long end,
2652 struct vmem_altmap *altmap)
2655 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
2657 static void prot_init_common(unsigned long page_none,
2658 unsigned long page_shared,
2659 unsigned long page_copy,
2660 unsigned long page_readonly,
2661 unsigned long page_exec_bit)
2663 PAGE_COPY = __pgprot(page_copy);
2664 PAGE_SHARED = __pgprot(page_shared);
2666 protection_map[0x0] = __pgprot(page_none);
2667 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
2668 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
2669 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
2670 protection_map[0x4] = __pgprot(page_readonly);
2671 protection_map[0x5] = __pgprot(page_readonly);
2672 protection_map[0x6] = __pgprot(page_copy);
2673 protection_map[0x7] = __pgprot(page_copy);
2674 protection_map[0x8] = __pgprot(page_none);
2675 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
2676 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
2677 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
2678 protection_map[0xc] = __pgprot(page_readonly);
2679 protection_map[0xd] = __pgprot(page_readonly);
2680 protection_map[0xe] = __pgprot(page_shared);
2681 protection_map[0xf] = __pgprot(page_shared);
2684 static void __init sun4u_pgprot_init(void)
2686 unsigned long page_none, page_shared, page_copy, page_readonly;
2687 unsigned long page_exec_bit;
2690 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2691 _PAGE_CACHE_4U | _PAGE_P_4U |
2692 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2694 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2695 _PAGE_CACHE_4U | _PAGE_P_4U |
2696 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2697 _PAGE_EXEC_4U | _PAGE_L_4U);
2699 _PAGE_IE = _PAGE_IE_4U;
2700 _PAGE_E = _PAGE_E_4U;
2701 _PAGE_CACHE = _PAGE_CACHE_4U;
2703 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
2704 __ACCESS_BITS_4U | _PAGE_E_4U);
2706 #ifdef CONFIG_DEBUG_PAGEALLOC
2707 kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
2709 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
2712 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
2713 _PAGE_P_4U | _PAGE_W_4U);
2715 for (i = 1; i < 4; i++)
2716 kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
2718 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
2719 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
2720 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
2723 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
2724 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2725 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
2726 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2727 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2728 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2729 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2731 page_exec_bit = _PAGE_EXEC_4U;
2733 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2737 static void __init sun4v_pgprot_init(void)
2739 unsigned long page_none, page_shared, page_copy, page_readonly;
2740 unsigned long page_exec_bit;
2743 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
2744 page_cache4v_flag | _PAGE_P_4V |
2745 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
2747 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
2749 _PAGE_IE = _PAGE_IE_4V;
2750 _PAGE_E = _PAGE_E_4V;
2751 _PAGE_CACHE = page_cache4v_flag;
2753 #ifdef CONFIG_DEBUG_PAGEALLOC
2754 kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
2756 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
2759 kern_linear_pte_xor[0] |= (page_cache4v_flag | _PAGE_P_4V |
2762 for (i = 1; i < 4; i++)
2763 kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
2765 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
2766 __ACCESS_BITS_4V | _PAGE_E_4V);
2768 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
2769 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
2770 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
2771 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
2773 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | page_cache4v_flag;
2774 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
2775 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
2776 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
2777 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2778 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
2779 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2781 page_exec_bit = _PAGE_EXEC_4V;
2783 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2787 unsigned long pte_sz_bits(unsigned long sz)
2789 if (tlb_type == hypervisor) {
2793 return _PAGE_SZ8K_4V;
2795 return _PAGE_SZ64K_4V;
2797 return _PAGE_SZ512K_4V;
2798 case 4 * 1024 * 1024:
2799 return _PAGE_SZ4MB_4V;
2805 return _PAGE_SZ8K_4U;
2807 return _PAGE_SZ64K_4U;
2809 return _PAGE_SZ512K_4U;
2810 case 4 * 1024 * 1024:
2811 return _PAGE_SZ4MB_4U;
2816 pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
2820 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
2821 pte_val(pte) |= (((unsigned long)space) << 32);
2822 pte_val(pte) |= pte_sz_bits(page_size);
2827 static unsigned long kern_large_tte(unsigned long paddr)
2831 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2832 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
2833 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
2834 if (tlb_type == hypervisor)
2835 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2836 page_cache4v_flag | _PAGE_P_4V |
2837 _PAGE_EXEC_4V | _PAGE_W_4V);
2842 /* If not locked, zap it. */
2843 void __flush_tlb_all(void)
2845 unsigned long pstate;
2848 __asm__ __volatile__("flushw\n\t"
2849 "rdpr %%pstate, %0\n\t"
2850 "wrpr %0, %1, %%pstate"
2853 if (tlb_type == hypervisor) {
2854 sun4v_mmu_demap_all();
2855 } else if (tlb_type == spitfire) {
2856 for (i = 0; i < 64; i++) {
2857 /* Spitfire Errata #32 workaround */
2858 /* NOTE: Always runs on spitfire, so no
2859 * cheetah+ page size encodings.
2861 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2865 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2867 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
2868 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2871 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
2872 spitfire_put_dtlb_data(i, 0x0UL);
2875 /* Spitfire Errata #32 workaround */
2876 /* NOTE: Always runs on spitfire, so no
2877 * cheetah+ page size encodings.
2879 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2883 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2885 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
2886 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2889 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
2890 spitfire_put_itlb_data(i, 0x0UL);
2893 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
2894 cheetah_flush_dtlb_all();
2895 cheetah_flush_itlb_all();
2897 __asm__ __volatile__("wrpr %0, 0, %%pstate"
2901 pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
2903 struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2907 pte = (pte_t *) page_address(page);
2912 pgtable_t pte_alloc_one(struct mm_struct *mm)
2914 struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2917 if (!pgtable_pte_page_ctor(page)) {
2918 free_unref_page(page);
2921 return (pte_t *) page_address(page);
2924 void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
2926 free_page((unsigned long)pte);
2929 static void __pte_free(pgtable_t pte)
2931 struct page *page = virt_to_page(pte);
2933 pgtable_pte_page_dtor(page);
2937 void pte_free(struct mm_struct *mm, pgtable_t pte)
2942 void pgtable_free(void *table, bool is_page)
2947 kmem_cache_free(pgtable_cache, table);
2950 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2951 void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
2954 unsigned long pte, flags;
2955 struct mm_struct *mm;
2958 if (!pmd_large(entry) || !pmd_young(entry))
2961 pte = pmd_val(entry);
2963 /* Don't insert a non-valid PMD into the TSB, we'll deadlock. */
2964 if (!(pte & _PAGE_VALID))
2967 /* We are fabricating 8MB pages using 4MB real hw pages. */
2968 pte |= (addr & (1UL << REAL_HPAGE_SHIFT));
2972 spin_lock_irqsave(&mm->context.lock, flags);
2974 if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL)
2975 __update_mmu_tsb_insert(mm, MM_TSB_HUGE, REAL_HPAGE_SHIFT,
2978 spin_unlock_irqrestore(&mm->context.lock, flags);
2980 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
2982 #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
2983 static void context_reload(void *__data)
2985 struct mm_struct *mm = __data;
2987 if (mm == current->mm)
2988 load_secondary_context(mm);
2991 void hugetlb_setup(struct pt_regs *regs)
2993 struct mm_struct *mm = current->mm;
2994 struct tsb_config *tp;
2996 if (faulthandler_disabled() || !mm) {
2997 const struct exception_table_entry *entry;
2999 entry = search_exception_tables(regs->tpc);
3001 regs->tpc = entry->fixup;
3002 regs->tnpc = regs->tpc + 4;
3005 pr_alert("Unexpected HugeTLB setup in atomic context.\n");
3006 die_if_kernel("HugeTSB in atomic", regs);
3009 tp = &mm->context.tsb_block[MM_TSB_HUGE];
3010 if (likely(tp->tsb == NULL))
3011 tsb_grow(mm, MM_TSB_HUGE, 0);
3013 tsb_context_switch(mm);
3016 /* On UltraSPARC-III+ and later, configure the second half of
3017 * the Data-TLB for huge pages.
3019 if (tlb_type == cheetah_plus) {
3020 bool need_context_reload = false;
3023 spin_lock_irq(&ctx_alloc_lock);
3024 ctx = mm->context.sparc64_ctx_val;
3025 ctx &= ~CTX_PGSZ_MASK;
3026 ctx |= CTX_PGSZ_BASE << CTX_PGSZ0_SHIFT;
3027 ctx |= CTX_PGSZ_HUGE << CTX_PGSZ1_SHIFT;
3029 if (ctx != mm->context.sparc64_ctx_val) {
3030 /* When changing the page size fields, we
3031 * must perform a context flush so that no
3032 * stale entries match. This flush must
3033 * occur with the original context register
3036 do_flush_tlb_mm(mm);
3038 /* Reload the context register of all processors
3039 * also executing in this address space.
3041 mm->context.sparc64_ctx_val = ctx;
3042 need_context_reload = true;
3044 spin_unlock_irq(&ctx_alloc_lock);
3046 if (need_context_reload)
3047 on_each_cpu(context_reload, mm, 0);
3052 static struct resource code_resource = {
3053 .name = "Kernel code",
3054 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
3057 static struct resource data_resource = {
3058 .name = "Kernel data",
3059 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
3062 static struct resource bss_resource = {
3063 .name = "Kernel bss",
3064 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
3067 static inline resource_size_t compute_kern_paddr(void *addr)
3069 return (resource_size_t) (addr - KERNBASE + kern_base);
3072 static void __init kernel_lds_init(void)
3074 code_resource.start = compute_kern_paddr(_text);
3075 code_resource.end = compute_kern_paddr(_etext - 1);
3076 data_resource.start = compute_kern_paddr(_etext);
3077 data_resource.end = compute_kern_paddr(_edata - 1);
3078 bss_resource.start = compute_kern_paddr(__bss_start);
3079 bss_resource.end = compute_kern_paddr(_end - 1);
3082 static int __init report_memory(void)
3085 struct resource *res;
3089 for (i = 0; i < pavail_ents; i++) {
3090 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
3093 pr_warn("Failed to allocate source.\n");
3097 res->name = "System RAM";
3098 res->start = pavail[i].phys_addr;
3099 res->end = pavail[i].phys_addr + pavail[i].reg_size - 1;
3100 res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
3102 if (insert_resource(&iomem_resource, res) < 0) {
3103 pr_warn("Resource insertion failed.\n");
3107 insert_resource(res, &code_resource);
3108 insert_resource(res, &data_resource);
3109 insert_resource(res, &bss_resource);
3114 arch_initcall(report_memory);
3117 #define do_flush_tlb_kernel_range smp_flush_tlb_kernel_range
3119 #define do_flush_tlb_kernel_range __flush_tlb_kernel_range
3122 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
3124 if (start < HI_OBP_ADDRESS && end > LOW_OBP_ADDRESS) {
3125 if (start < LOW_OBP_ADDRESS) {
3126 flush_tsb_kernel_range(start, LOW_OBP_ADDRESS);
3127 do_flush_tlb_kernel_range(start, LOW_OBP_ADDRESS);
3129 if (end > HI_OBP_ADDRESS) {
3130 flush_tsb_kernel_range(HI_OBP_ADDRESS, end);
3131 do_flush_tlb_kernel_range(HI_OBP_ADDRESS, end);
3134 flush_tsb_kernel_range(start, end);
3135 do_flush_tlb_kernel_range(start, end);
3139 void copy_user_highpage(struct page *to, struct page *from,
3140 unsigned long vaddr, struct vm_area_struct *vma)
3144 vfrom = kmap_atomic(from);
3145 vto = kmap_atomic(to);
3146 copy_user_page(vto, vfrom, vaddr, to);
3148 kunmap_atomic(vfrom);
3150 /* If this page has ADI enabled, copy over any ADI tags
3153 if (vma->vm_flags & VM_SPARC_ADI) {
3154 unsigned long pfrom, pto, i, adi_tag;
3156 pfrom = page_to_phys(from);
3157 pto = page_to_phys(to);
3159 for (i = pfrom; i < (pfrom + PAGE_SIZE); i += adi_blksize()) {
3160 asm volatile("ldxa [%1] %2, %0\n\t"
3162 : "r" (i), "i" (ASI_MCD_REAL));
3163 asm volatile("stxa %0, [%1] %2\n\t"
3165 : "r" (adi_tag), "r" (pto),
3166 "i" (ASI_MCD_REAL));
3167 pto += adi_blksize();
3169 asm volatile("membar #Sync\n\t");
3172 EXPORT_SYMBOL(copy_user_highpage);
3174 void copy_highpage(struct page *to, struct page *from)
3178 vfrom = kmap_atomic(from);
3179 vto = kmap_atomic(to);
3180 copy_page(vto, vfrom);
3182 kunmap_atomic(vfrom);
3184 /* If this platform is ADI enabled, copy any ADI tags
3187 if (adi_capable()) {
3188 unsigned long pfrom, pto, i, adi_tag;
3190 pfrom = page_to_phys(from);
3191 pto = page_to_phys(to);
3193 for (i = pfrom; i < (pfrom + PAGE_SIZE); i += adi_blksize()) {
3194 asm volatile("ldxa [%1] %2, %0\n\t"
3196 : "r" (i), "i" (ASI_MCD_REAL));
3197 asm volatile("stxa %0, [%1] %2\n\t"
3199 : "r" (adi_tag), "r" (pto),
3200 "i" (ASI_MCD_REAL));
3201 pto += adi_blksize();
3203 asm volatile("membar #Sync\n\t");
3206 EXPORT_SYMBOL(copy_highpage);