2 * arch/sparc64/mm/init.c
4 * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
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>
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/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/percpu.h>
27 #include <linux/lmb.h>
28 #include <linux/mmzone.h>
31 #include <asm/system.h>
33 #include <asm/pgalloc.h>
34 #include <asm/pgtable.h>
35 #include <asm/oplib.h>
36 #include <asm/iommu.h>
38 #include <asm/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>
55 unsigned long kern_linear_pte_xor[2] __read_mostly;
57 /* A bitmap, one bit for every 256MB of physical memory. If the bit
58 * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
59 * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
61 unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
63 #ifndef CONFIG_DEBUG_PAGEALLOC
64 /* A special kernel TSB for 4MB and 256MB linear mappings.
65 * Space is allocated for this right after the trap table
66 * in arch/sparc64/kernel/head.S
68 extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
73 static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
74 static int pavail_ents __initdata;
76 static int cmp_p64(const void *a, const void *b)
78 const struct linux_prom64_registers *x = a, *y = b;
80 if (x->phys_addr > y->phys_addr)
82 if (x->phys_addr < y->phys_addr)
87 static void __init read_obp_memory(const char *property,
88 struct linux_prom64_registers *regs,
91 int node = prom_finddevice("/memory");
92 int prop_size = prom_getproplen(node, property);
95 ents = prop_size / sizeof(struct linux_prom64_registers);
96 if (ents > MAX_BANKS) {
97 prom_printf("The machine has more %s property entries than "
98 "this kernel can support (%d).\n",
103 ret = prom_getproperty(node, property, (char *) regs, prop_size);
105 prom_printf("Couldn't get %s property from /memory.\n");
109 /* Sanitize what we got from the firmware, by page aligning
112 for (i = 0; i < ents; i++) {
113 unsigned long base, size;
115 base = regs[i].phys_addr;
116 size = regs[i].reg_size;
119 if (base & ~PAGE_MASK) {
120 unsigned long new_base = PAGE_ALIGN(base);
122 size -= new_base - base;
123 if ((long) size < 0L)
128 /* If it is empty, simply get rid of it.
129 * This simplifies the logic of the other
130 * functions that process these arrays.
132 memmove(®s[i], ®s[i + 1],
133 (ents - i - 1) * sizeof(regs[0]));
138 regs[i].phys_addr = base;
139 regs[i].reg_size = size;
144 sort(regs, ents, sizeof(struct linux_prom64_registers),
148 unsigned long *sparc64_valid_addr_bitmap __read_mostly;
150 /* Kernel physical address base and size in bytes. */
151 unsigned long kern_base __read_mostly;
152 unsigned long kern_size __read_mostly;
154 /* Initial ramdisk setup */
155 extern unsigned long sparc_ramdisk_image64;
156 extern unsigned int sparc_ramdisk_image;
157 extern unsigned int sparc_ramdisk_size;
159 struct page *mem_map_zero __read_mostly;
160 EXPORT_SYMBOL(mem_map_zero);
162 unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
164 unsigned long sparc64_kern_pri_context __read_mostly;
165 unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
166 unsigned long sparc64_kern_sec_context __read_mostly;
168 int num_kernel_image_mappings;
170 #ifdef CONFIG_DEBUG_DCFLUSH
171 atomic_t dcpage_flushes = ATOMIC_INIT(0);
173 atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
177 inline void flush_dcache_page_impl(struct page *page)
179 BUG_ON(tlb_type == hypervisor);
180 #ifdef CONFIG_DEBUG_DCFLUSH
181 atomic_inc(&dcpage_flushes);
184 #ifdef DCACHE_ALIASING_POSSIBLE
185 __flush_dcache_page(page_address(page),
186 ((tlb_type == spitfire) &&
187 page_mapping(page) != NULL));
189 if (page_mapping(page) != NULL &&
190 tlb_type == spitfire)
191 __flush_icache_page(__pa(page_address(page)));
195 #define PG_dcache_dirty PG_arch_1
196 #define PG_dcache_cpu_shift 32UL
197 #define PG_dcache_cpu_mask \
198 ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
200 #define dcache_dirty_cpu(page) \
201 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
203 static inline void set_dcache_dirty(struct page *page, int this_cpu)
205 unsigned long mask = this_cpu;
206 unsigned long non_cpu_bits;
208 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
209 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
211 __asm__ __volatile__("1:\n\t"
213 "and %%g7, %1, %%g1\n\t"
214 "or %%g1, %0, %%g1\n\t"
215 "casx [%2], %%g7, %%g1\n\t"
217 "bne,pn %%xcc, 1b\n\t"
220 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
224 static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
226 unsigned long mask = (1UL << PG_dcache_dirty);
228 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
231 "srlx %%g7, %4, %%g1\n\t"
232 "and %%g1, %3, %%g1\n\t"
234 "bne,pn %%icc, 2f\n\t"
235 " andn %%g7, %1, %%g1\n\t"
236 "casx [%2], %%g7, %%g1\n\t"
238 "bne,pn %%xcc, 1b\n\t"
242 : "r" (cpu), "r" (mask), "r" (&page->flags),
243 "i" (PG_dcache_cpu_mask),
244 "i" (PG_dcache_cpu_shift)
248 static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
250 unsigned long tsb_addr = (unsigned long) ent;
252 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
253 tsb_addr = __pa(tsb_addr);
255 __tsb_insert(tsb_addr, tag, pte);
258 unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
259 unsigned long _PAGE_SZBITS __read_mostly;
261 static void flush_dcache(unsigned long pfn)
265 page = pfn_to_page(pfn);
266 if (page && page_mapping(page)) {
267 unsigned long pg_flags;
269 pg_flags = page->flags;
270 if (pg_flags & (1UL << PG_dcache_dirty)) {
271 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
273 int this_cpu = get_cpu();
275 /* This is just to optimize away some function calls
279 flush_dcache_page_impl(page);
281 smp_flush_dcache_page_impl(page, cpu);
283 clear_dcache_dirty_cpu(page, cpu);
290 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
292 struct mm_struct *mm;
294 unsigned long tag, flags;
295 unsigned long tsb_index, tsb_hash_shift;
297 if (tlb_type != hypervisor) {
298 unsigned long pfn = pte_pfn(pte);
306 tsb_index = MM_TSB_BASE;
307 tsb_hash_shift = PAGE_SHIFT;
309 spin_lock_irqsave(&mm->context.lock, flags);
311 #ifdef CONFIG_HUGETLB_PAGE
312 if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
313 if ((tlb_type == hypervisor &&
314 (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
315 (tlb_type != hypervisor &&
316 (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
317 tsb_index = MM_TSB_HUGE;
318 tsb_hash_shift = HPAGE_SHIFT;
323 tsb = mm->context.tsb_block[tsb_index].tsb;
324 tsb += ((address >> tsb_hash_shift) &
325 (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
326 tag = (address >> 22UL);
327 tsb_insert(tsb, tag, pte_val(pte));
329 spin_unlock_irqrestore(&mm->context.lock, flags);
332 void flush_dcache_page(struct page *page)
334 struct address_space *mapping;
337 if (tlb_type == hypervisor)
340 /* Do not bother with the expensive D-cache flush if it
341 * is merely the zero page. The 'bigcore' testcase in GDB
342 * causes this case to run millions of times.
344 if (page == ZERO_PAGE(0))
347 this_cpu = get_cpu();
349 mapping = page_mapping(page);
350 if (mapping && !mapping_mapped(mapping)) {
351 int dirty = test_bit(PG_dcache_dirty, &page->flags);
353 int dirty_cpu = dcache_dirty_cpu(page);
355 if (dirty_cpu == this_cpu)
357 smp_flush_dcache_page_impl(page, dirty_cpu);
359 set_dcache_dirty(page, this_cpu);
361 /* We could delay the flush for the !page_mapping
362 * case too. But that case is for exec env/arg
363 * pages and those are %99 certainly going to get
364 * faulted into the tlb (and thus flushed) anyways.
366 flush_dcache_page_impl(page);
373 void __kprobes flush_icache_range(unsigned long start, unsigned long end)
375 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
376 if (tlb_type == spitfire) {
379 /* This code only runs on Spitfire cpus so this is
380 * why we can assume _PAGE_PADDR_4U.
382 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
383 unsigned long paddr, mask = _PAGE_PADDR_4U;
385 if (kaddr >= PAGE_OFFSET)
386 paddr = kaddr & mask;
388 pgd_t *pgdp = pgd_offset_k(kaddr);
389 pud_t *pudp = pud_offset(pgdp, kaddr);
390 pmd_t *pmdp = pmd_offset(pudp, kaddr);
391 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
393 paddr = pte_val(*ptep) & mask;
395 __flush_icache_page(paddr);
400 void mmu_info(struct seq_file *m)
402 if (tlb_type == cheetah)
403 seq_printf(m, "MMU Type\t: Cheetah\n");
404 else if (tlb_type == cheetah_plus)
405 seq_printf(m, "MMU Type\t: Cheetah+\n");
406 else if (tlb_type == spitfire)
407 seq_printf(m, "MMU Type\t: Spitfire\n");
408 else if (tlb_type == hypervisor)
409 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
411 seq_printf(m, "MMU Type\t: ???\n");
413 #ifdef CONFIG_DEBUG_DCFLUSH
414 seq_printf(m, "DCPageFlushes\t: %d\n",
415 atomic_read(&dcpage_flushes));
417 seq_printf(m, "DCPageFlushesXC\t: %d\n",
418 atomic_read(&dcpage_flushes_xcall));
419 #endif /* CONFIG_SMP */
420 #endif /* CONFIG_DEBUG_DCFLUSH */
423 struct linux_prom_translation prom_trans[512] __read_mostly;
424 unsigned int prom_trans_ents __read_mostly;
426 unsigned long kern_locked_tte_data;
428 /* The obp translations are saved based on 8k pagesize, since obp can
429 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
430 * HI_OBP_ADDRESS range are handled in ktlb.S.
432 static inline int in_obp_range(unsigned long vaddr)
434 return (vaddr >= LOW_OBP_ADDRESS &&
435 vaddr < HI_OBP_ADDRESS);
438 static int cmp_ptrans(const void *a, const void *b)
440 const struct linux_prom_translation *x = a, *y = b;
442 if (x->virt > y->virt)
444 if (x->virt < y->virt)
449 /* Read OBP translations property into 'prom_trans[]'. */
450 static void __init read_obp_translations(void)
452 int n, node, ents, first, last, i;
454 node = prom_finddevice("/virtual-memory");
455 n = prom_getproplen(node, "translations");
456 if (unlikely(n == 0 || n == -1)) {
457 prom_printf("prom_mappings: Couldn't get size.\n");
460 if (unlikely(n > sizeof(prom_trans))) {
461 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
465 if ((n = prom_getproperty(node, "translations",
466 (char *)&prom_trans[0],
467 sizeof(prom_trans))) == -1) {
468 prom_printf("prom_mappings: Couldn't get property.\n");
472 n = n / sizeof(struct linux_prom_translation);
476 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
479 /* Now kick out all the non-OBP entries. */
480 for (i = 0; i < ents; i++) {
481 if (in_obp_range(prom_trans[i].virt))
485 for (; i < ents; i++) {
486 if (!in_obp_range(prom_trans[i].virt))
491 for (i = 0; i < (last - first); i++) {
492 struct linux_prom_translation *src = &prom_trans[i + first];
493 struct linux_prom_translation *dest = &prom_trans[i];
497 for (; i < ents; i++) {
498 struct linux_prom_translation *dest = &prom_trans[i];
499 dest->virt = dest->size = dest->data = 0x0UL;
502 prom_trans_ents = last - first;
504 if (tlb_type == spitfire) {
505 /* Clear diag TTE bits. */
506 for (i = 0; i < prom_trans_ents; i++)
507 prom_trans[i].data &= ~0x0003fe0000000000UL;
511 static void __init hypervisor_tlb_lock(unsigned long vaddr,
515 unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
518 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
519 "errors with %lx\n", vaddr, 0, pte, mmu, ret);
524 static unsigned long kern_large_tte(unsigned long paddr);
526 static void __init remap_kernel(void)
528 unsigned long phys_page, tte_vaddr, tte_data;
529 int i, tlb_ent = sparc64_highest_locked_tlbent();
531 tte_vaddr = (unsigned long) KERNBASE;
532 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
533 tte_data = kern_large_tte(phys_page);
535 kern_locked_tte_data = tte_data;
537 /* Now lock us into the TLBs via Hypervisor or OBP. */
538 if (tlb_type == hypervisor) {
539 for (i = 0; i < num_kernel_image_mappings; i++) {
540 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
541 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
542 tte_vaddr += 0x400000;
543 tte_data += 0x400000;
546 for (i = 0; i < num_kernel_image_mappings; i++) {
547 prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
548 prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
549 tte_vaddr += 0x400000;
550 tte_data += 0x400000;
552 sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
554 if (tlb_type == cheetah_plus) {
555 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
556 CTX_CHEETAH_PLUS_NUC);
557 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
558 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
563 static void __init inherit_prom_mappings(void)
565 /* Now fixup OBP's idea about where we really are mapped. */
566 printk("Remapping the kernel... ");
571 void prom_world(int enter)
574 set_fs((mm_segment_t) { get_thread_current_ds() });
576 __asm__ __volatile__("flushw");
579 void __flush_dcache_range(unsigned long start, unsigned long end)
583 if (tlb_type == spitfire) {
586 for (va = start; va < end; va += 32) {
587 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
591 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
594 for (va = start; va < end; va += 32)
595 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
599 "i" (ASI_DCACHE_INVALIDATE));
603 /* get_new_mmu_context() uses "cache + 1". */
604 DEFINE_SPINLOCK(ctx_alloc_lock);
605 unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
606 #define MAX_CTX_NR (1UL << CTX_NR_BITS)
607 #define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
608 DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
610 /* Caller does TLB context flushing on local CPU if necessary.
611 * The caller also ensures that CTX_VALID(mm->context) is false.
613 * We must be careful about boundary cases so that we never
614 * let the user have CTX 0 (nucleus) or we ever use a CTX
615 * version of zero (and thus NO_CONTEXT would not be caught
616 * by version mis-match tests in mmu_context.h).
618 * Always invoked with interrupts disabled.
620 void get_new_mmu_context(struct mm_struct *mm)
622 unsigned long ctx, new_ctx;
623 unsigned long orig_pgsz_bits;
627 spin_lock_irqsave(&ctx_alloc_lock, flags);
628 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
629 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
630 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
632 if (new_ctx >= (1 << CTX_NR_BITS)) {
633 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
634 if (new_ctx >= ctx) {
636 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
639 new_ctx = CTX_FIRST_VERSION;
641 /* Don't call memset, for 16 entries that's just
644 mmu_context_bmap[0] = 3;
645 mmu_context_bmap[1] = 0;
646 mmu_context_bmap[2] = 0;
647 mmu_context_bmap[3] = 0;
648 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
649 mmu_context_bmap[i + 0] = 0;
650 mmu_context_bmap[i + 1] = 0;
651 mmu_context_bmap[i + 2] = 0;
652 mmu_context_bmap[i + 3] = 0;
658 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
659 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
661 tlb_context_cache = new_ctx;
662 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
663 spin_unlock_irqrestore(&ctx_alloc_lock, flags);
665 if (unlikely(new_version))
666 smp_new_mmu_context_version();
669 static int numa_enabled = 1;
670 static int numa_debug;
672 static int __init early_numa(char *p)
677 if (strstr(p, "off"))
680 if (strstr(p, "debug"))
685 early_param("numa", early_numa);
687 #define numadbg(f, a...) \
688 do { if (numa_debug) \
689 printk(KERN_INFO f, ## a); \
692 static void __init find_ramdisk(unsigned long phys_base)
694 #ifdef CONFIG_BLK_DEV_INITRD
695 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
696 unsigned long ramdisk_image;
698 /* Older versions of the bootloader only supported a
699 * 32-bit physical address for the ramdisk image
700 * location, stored at sparc_ramdisk_image. Newer
701 * SILO versions set sparc_ramdisk_image to zero and
702 * provide a full 64-bit physical address at
703 * sparc_ramdisk_image64.
705 ramdisk_image = sparc_ramdisk_image;
707 ramdisk_image = sparc_ramdisk_image64;
709 /* Another bootloader quirk. The bootloader normalizes
710 * the physical address to KERNBASE, so we have to
711 * factor that back out and add in the lowest valid
712 * physical page address to get the true physical address.
714 ramdisk_image -= KERNBASE;
715 ramdisk_image += phys_base;
717 numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
718 ramdisk_image, sparc_ramdisk_size);
720 initrd_start = ramdisk_image;
721 initrd_end = ramdisk_image + sparc_ramdisk_size;
723 lmb_reserve(initrd_start, sparc_ramdisk_size);
725 initrd_start += PAGE_OFFSET;
726 initrd_end += PAGE_OFFSET;
731 struct node_mem_mask {
734 unsigned long bootmem_paddr;
736 static struct node_mem_mask node_masks[MAX_NUMNODES];
737 static int num_node_masks;
739 int numa_cpu_lookup_table[NR_CPUS];
740 cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
742 #ifdef CONFIG_NEED_MULTIPLE_NODES
744 struct mdesc_mblock {
747 u64 offset; /* RA-to-PA */
749 static struct mdesc_mblock *mblocks;
750 static int num_mblocks;
752 static unsigned long ra_to_pa(unsigned long addr)
756 for (i = 0; i < num_mblocks; i++) {
757 struct mdesc_mblock *m = &mblocks[i];
759 if (addr >= m->base &&
760 addr < (m->base + m->size)) {
768 static int find_node(unsigned long addr)
772 addr = ra_to_pa(addr);
773 for (i = 0; i < num_node_masks; i++) {
774 struct node_mem_mask *p = &node_masks[i];
776 if ((addr & p->mask) == p->val)
782 static unsigned long long nid_range(unsigned long long start,
783 unsigned long long end, int *nid)
785 *nid = find_node(start);
787 while (start < end) {
788 int n = find_node(start);
801 static unsigned long long nid_range(unsigned long long start,
802 unsigned long long end, int *nid)
809 /* This must be invoked after performing all of the necessary
810 * add_active_range() calls for 'nid'. We need to be able to get
811 * correct data from get_pfn_range_for_nid().
813 static void __init allocate_node_data(int nid)
815 unsigned long paddr, num_pages, start_pfn, end_pfn;
816 struct pglist_data *p;
818 #ifdef CONFIG_NEED_MULTIPLE_NODES
819 paddr = lmb_alloc_nid(sizeof(struct pglist_data),
820 SMP_CACHE_BYTES, nid, nid_range);
822 prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
825 NODE_DATA(nid) = __va(paddr);
826 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
828 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
833 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
834 p->node_start_pfn = start_pfn;
835 p->node_spanned_pages = end_pfn - start_pfn;
837 if (p->node_spanned_pages) {
838 num_pages = bootmem_bootmap_pages(p->node_spanned_pages);
840 paddr = lmb_alloc_nid(num_pages << PAGE_SHIFT, PAGE_SIZE, nid,
843 prom_printf("Cannot allocate bootmap for nid[%d]\n",
847 node_masks[nid].bootmem_paddr = paddr;
851 static void init_node_masks_nonnuma(void)
855 numadbg("Initializing tables for non-numa.\n");
857 node_masks[0].mask = node_masks[0].val = 0;
860 for (i = 0; i < NR_CPUS; i++)
861 numa_cpu_lookup_table[i] = 0;
863 numa_cpumask_lookup_table[0] = CPU_MASK_ALL;
866 #ifdef CONFIG_NEED_MULTIPLE_NODES
867 struct pglist_data *node_data[MAX_NUMNODES];
869 EXPORT_SYMBOL(numa_cpu_lookup_table);
870 EXPORT_SYMBOL(numa_cpumask_lookup_table);
871 EXPORT_SYMBOL(node_data);
873 struct mdesc_mlgroup {
879 static struct mdesc_mlgroup *mlgroups;
880 static int num_mlgroups;
882 static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
887 mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
888 u64 target = mdesc_arc_target(md, arc);
891 val = mdesc_get_property(md, target,
893 if (val && *val == cfg_handle)
899 static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
902 u64 arc, candidate, best_latency = ~(u64)0;
904 candidate = MDESC_NODE_NULL;
905 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
906 u64 target = mdesc_arc_target(md, arc);
907 const char *name = mdesc_node_name(md, target);
910 if (strcmp(name, "pio-latency-group"))
913 val = mdesc_get_property(md, target, "latency", NULL);
917 if (*val < best_latency) {
923 if (candidate == MDESC_NODE_NULL)
926 return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
929 int of_node_to_nid(struct device_node *dp)
931 const struct linux_prom64_registers *regs;
932 struct mdesc_handle *md;
937 /* This is the right thing to do on currently supported
938 * SUN4U NUMA platforms as well, as the PCI controller does
939 * not sit behind any particular memory controller.
944 regs = of_get_property(dp, "reg", NULL);
948 cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
954 mdesc_for_each_node_by_name(md, grp, "group") {
955 if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
967 static void add_node_ranges(void)
971 for (i = 0; i < lmb.memory.cnt; i++) {
972 unsigned long size = lmb_size_bytes(&lmb.memory, i);
973 unsigned long start, end;
975 start = lmb.memory.region[i].base;
977 while (start < end) {
978 unsigned long this_end;
981 this_end = nid_range(start, end, &nid);
983 numadbg("Adding active range nid[%d] "
984 "start[%lx] end[%lx]\n",
985 nid, start, this_end);
987 add_active_range(nid,
989 this_end >> PAGE_SHIFT);
996 static int __init grab_mlgroups(struct mdesc_handle *md)
1002 mdesc_for_each_node_by_name(md, node, "memory-latency-group")
1007 paddr = lmb_alloc(count * sizeof(struct mdesc_mlgroup),
1012 mlgroups = __va(paddr);
1013 num_mlgroups = count;
1016 mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
1017 struct mdesc_mlgroup *m = &mlgroups[count++];
1022 val = mdesc_get_property(md, node, "latency", NULL);
1024 val = mdesc_get_property(md, node, "address-match", NULL);
1026 val = mdesc_get_property(md, node, "address-mask", NULL);
1029 numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
1030 "match[%llx] mask[%llx]\n",
1031 count - 1, m->node, m->latency, m->match, m->mask);
1037 static int __init grab_mblocks(struct mdesc_handle *md)
1039 unsigned long paddr;
1043 mdesc_for_each_node_by_name(md, node, "mblock")
1048 paddr = lmb_alloc(count * sizeof(struct mdesc_mblock),
1053 mblocks = __va(paddr);
1054 num_mblocks = count;
1057 mdesc_for_each_node_by_name(md, node, "mblock") {
1058 struct mdesc_mblock *m = &mblocks[count++];
1061 val = mdesc_get_property(md, node, "base", NULL);
1063 val = mdesc_get_property(md, node, "size", NULL);
1065 val = mdesc_get_property(md, node,
1066 "address-congruence-offset", NULL);
1069 numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
1070 count - 1, m->base, m->size, m->offset);
1076 static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
1077 u64 grp, cpumask_t *mask)
1083 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
1084 u64 target = mdesc_arc_target(md, arc);
1085 const char *name = mdesc_node_name(md, target);
1088 if (strcmp(name, "cpu"))
1090 id = mdesc_get_property(md, target, "id", NULL);
1092 cpu_set(*id, *mask);
1096 static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
1100 for (i = 0; i < num_mlgroups; i++) {
1101 struct mdesc_mlgroup *m = &mlgroups[i];
1102 if (m->node == node)
1108 static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
1111 struct mdesc_mlgroup *candidate = NULL;
1112 u64 arc, best_latency = ~(u64)0;
1113 struct node_mem_mask *n;
1115 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1116 u64 target = mdesc_arc_target(md, arc);
1117 struct mdesc_mlgroup *m = find_mlgroup(target);
1120 if (m->latency < best_latency) {
1122 best_latency = m->latency;
1128 if (num_node_masks != index) {
1129 printk(KERN_ERR "Inconsistent NUMA state, "
1130 "index[%d] != num_node_masks[%d]\n",
1131 index, num_node_masks);
1135 n = &node_masks[num_node_masks++];
1137 n->mask = candidate->mask;
1138 n->val = candidate->match;
1140 numadbg("NUMA NODE[%d]: mask[%lx] val[%lx] (latency[%llx])\n",
1141 index, n->mask, n->val, candidate->latency);
1146 static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
1152 numa_parse_mdesc_group_cpus(md, grp, &mask);
1154 for_each_cpu_mask(cpu, mask)
1155 numa_cpu_lookup_table[cpu] = index;
1156 numa_cpumask_lookup_table[index] = mask;
1159 printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
1160 for_each_cpu_mask(cpu, mask)
1165 return numa_attach_mlgroup(md, grp, index);
1168 static int __init numa_parse_mdesc(void)
1170 struct mdesc_handle *md = mdesc_grab();
1174 node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
1175 if (node == MDESC_NODE_NULL) {
1180 err = grab_mblocks(md);
1184 err = grab_mlgroups(md);
1189 mdesc_for_each_node_by_name(md, node, "group") {
1190 err = numa_parse_mdesc_group(md, node, count);
1198 for (i = 0; i < num_node_masks; i++) {
1199 allocate_node_data(i);
1209 static int __init numa_parse_jbus(void)
1211 unsigned long cpu, index;
1213 /* NUMA node id is encoded in bits 36 and higher, and there is
1214 * a 1-to-1 mapping from CPU ID to NUMA node ID.
1217 for_each_present_cpu(cpu) {
1218 numa_cpu_lookup_table[cpu] = index;
1219 numa_cpumask_lookup_table[index] = cpumask_of_cpu(cpu);
1220 node_masks[index].mask = ~((1UL << 36UL) - 1UL);
1221 node_masks[index].val = cpu << 36UL;
1225 num_node_masks = index;
1229 for (index = 0; index < num_node_masks; index++) {
1230 allocate_node_data(index);
1231 node_set_online(index);
1237 static int __init numa_parse_sun4u(void)
1239 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1242 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
1243 if ((ver >> 32UL) == __JALAPENO_ID ||
1244 (ver >> 32UL) == __SERRANO_ID)
1245 return numa_parse_jbus();
1250 static int __init bootmem_init_numa(void)
1254 numadbg("bootmem_init_numa()\n");
1257 if (tlb_type == hypervisor)
1258 err = numa_parse_mdesc();
1260 err = numa_parse_sun4u();
1267 static int bootmem_init_numa(void)
1274 static void __init bootmem_init_nonnuma(void)
1276 unsigned long top_of_ram = lmb_end_of_DRAM();
1277 unsigned long total_ram = lmb_phys_mem_size();
1280 numadbg("bootmem_init_nonnuma()\n");
1282 printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1283 top_of_ram, total_ram);
1284 printk(KERN_INFO "Memory hole size: %ldMB\n",
1285 (top_of_ram - total_ram) >> 20);
1287 init_node_masks_nonnuma();
1289 for (i = 0; i < lmb.memory.cnt; i++) {
1290 unsigned long size = lmb_size_bytes(&lmb.memory, i);
1291 unsigned long start_pfn, end_pfn;
1296 start_pfn = lmb.memory.region[i].base >> PAGE_SHIFT;
1297 end_pfn = start_pfn + lmb_size_pages(&lmb.memory, i);
1298 add_active_range(0, start_pfn, end_pfn);
1301 allocate_node_data(0);
1306 static void __init reserve_range_in_node(int nid, unsigned long start,
1309 numadbg(" reserve_range_in_node(nid[%d],start[%lx],end[%lx]\n",
1311 while (start < end) {
1312 unsigned long this_end;
1315 this_end = nid_range(start, end, &n);
1317 numadbg(" MATCH reserving range [%lx:%lx]\n",
1319 reserve_bootmem_node(NODE_DATA(nid), start,
1320 (this_end - start), BOOTMEM_DEFAULT);
1322 numadbg(" NO MATCH, advancing start to %lx\n",
1329 static void __init trim_reserved_in_node(int nid)
1333 numadbg(" trim_reserved_in_node(%d)\n", nid);
1335 for (i = 0; i < lmb.reserved.cnt; i++) {
1336 unsigned long start = lmb.reserved.region[i].base;
1337 unsigned long size = lmb_size_bytes(&lmb.reserved, i);
1338 unsigned long end = start + size;
1340 reserve_range_in_node(nid, start, end);
1344 static void __init bootmem_init_one_node(int nid)
1346 struct pglist_data *p;
1348 numadbg("bootmem_init_one_node(%d)\n", nid);
1352 if (p->node_spanned_pages) {
1353 unsigned long paddr = node_masks[nid].bootmem_paddr;
1354 unsigned long end_pfn;
1356 end_pfn = p->node_start_pfn + p->node_spanned_pages;
1358 numadbg(" init_bootmem_node(%d, %lx, %lx, %lx)\n",
1359 nid, paddr >> PAGE_SHIFT, p->node_start_pfn, end_pfn);
1361 init_bootmem_node(p, paddr >> PAGE_SHIFT,
1362 p->node_start_pfn, end_pfn);
1364 numadbg(" free_bootmem_with_active_regions(%d, %lx)\n",
1366 free_bootmem_with_active_regions(nid, end_pfn);
1368 trim_reserved_in_node(nid);
1370 numadbg(" sparse_memory_present_with_active_regions(%d)\n",
1372 sparse_memory_present_with_active_regions(nid);
1376 static unsigned long __init bootmem_init(unsigned long phys_base)
1378 unsigned long end_pfn;
1381 end_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
1382 max_pfn = max_low_pfn = end_pfn;
1383 min_low_pfn = (phys_base >> PAGE_SHIFT);
1385 if (bootmem_init_numa() < 0)
1386 bootmem_init_nonnuma();
1388 /* XXX cpu notifier XXX */
1390 for_each_online_node(nid)
1391 bootmem_init_one_node(nid);
1398 static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1399 static int pall_ents __initdata;
1401 #ifdef CONFIG_DEBUG_PAGEALLOC
1402 static unsigned long __ref kernel_map_range(unsigned long pstart,
1403 unsigned long pend, pgprot_t prot)
1405 unsigned long vstart = PAGE_OFFSET + pstart;
1406 unsigned long vend = PAGE_OFFSET + pend;
1407 unsigned long alloc_bytes = 0UL;
1409 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
1410 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
1415 while (vstart < vend) {
1416 unsigned long this_end, paddr = __pa(vstart);
1417 pgd_t *pgd = pgd_offset_k(vstart);
1422 pud = pud_offset(pgd, vstart);
1423 if (pud_none(*pud)) {
1426 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1427 alloc_bytes += PAGE_SIZE;
1428 pud_populate(&init_mm, pud, new);
1431 pmd = pmd_offset(pud, vstart);
1432 if (!pmd_present(*pmd)) {
1435 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1436 alloc_bytes += PAGE_SIZE;
1437 pmd_populate_kernel(&init_mm, pmd, new);
1440 pte = pte_offset_kernel(pmd, vstart);
1441 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1442 if (this_end > vend)
1445 while (vstart < this_end) {
1446 pte_val(*pte) = (paddr | pgprot_val(prot));
1448 vstart += PAGE_SIZE;
1457 extern unsigned int kvmap_linear_patch[1];
1458 #endif /* CONFIG_DEBUG_PAGEALLOC */
1460 static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
1462 const unsigned long shift_256MB = 28;
1463 const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
1464 const unsigned long size_256MB = (1UL << shift_256MB);
1466 while (start < end) {
1469 remains = end - start;
1470 if (remains < size_256MB)
1473 if (start & mask_256MB) {
1474 start = (start + size_256MB) & ~mask_256MB;
1478 while (remains >= size_256MB) {
1479 unsigned long index = start >> shift_256MB;
1481 __set_bit(index, kpte_linear_bitmap);
1483 start += size_256MB;
1484 remains -= size_256MB;
1489 static void __init init_kpte_bitmap(void)
1493 for (i = 0; i < pall_ents; i++) {
1494 unsigned long phys_start, phys_end;
1496 phys_start = pall[i].phys_addr;
1497 phys_end = phys_start + pall[i].reg_size;
1499 mark_kpte_bitmap(phys_start, phys_end);
1503 static void __init kernel_physical_mapping_init(void)
1505 #ifdef CONFIG_DEBUG_PAGEALLOC
1506 unsigned long i, mem_alloced = 0UL;
1508 for (i = 0; i < pall_ents; i++) {
1509 unsigned long phys_start, phys_end;
1511 phys_start = pall[i].phys_addr;
1512 phys_end = phys_start + pall[i].reg_size;
1514 mem_alloced += kernel_map_range(phys_start, phys_end,
1518 printk("Allocated %ld bytes for kernel page tables.\n",
1521 kvmap_linear_patch[0] = 0x01000000; /* nop */
1522 flushi(&kvmap_linear_patch[0]);
1528 #ifdef CONFIG_DEBUG_PAGEALLOC
1529 void kernel_map_pages(struct page *page, int numpages, int enable)
1531 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1532 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1534 kernel_map_range(phys_start, phys_end,
1535 (enable ? PAGE_KERNEL : __pgprot(0)));
1537 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1538 PAGE_OFFSET + phys_end);
1540 /* we should perform an IPI and flush all tlbs,
1541 * but that can deadlock->flush only current cpu.
1543 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1544 PAGE_OFFSET + phys_end);
1548 unsigned long __init find_ecache_flush_span(unsigned long size)
1552 for (i = 0; i < pavail_ents; i++) {
1553 if (pavail[i].reg_size >= size)
1554 return pavail[i].phys_addr;
1560 static void __init tsb_phys_patch(void)
1562 struct tsb_ldquad_phys_patch_entry *pquad;
1563 struct tsb_phys_patch_entry *p;
1565 pquad = &__tsb_ldquad_phys_patch;
1566 while (pquad < &__tsb_ldquad_phys_patch_end) {
1567 unsigned long addr = pquad->addr;
1569 if (tlb_type == hypervisor)
1570 *(unsigned int *) addr = pquad->sun4v_insn;
1572 *(unsigned int *) addr = pquad->sun4u_insn;
1574 __asm__ __volatile__("flush %0"
1581 p = &__tsb_phys_patch;
1582 while (p < &__tsb_phys_patch_end) {
1583 unsigned long addr = p->addr;
1585 *(unsigned int *) addr = p->insn;
1587 __asm__ __volatile__("flush %0"
1595 /* Don't mark as init, we give this to the Hypervisor. */
1596 #ifndef CONFIG_DEBUG_PAGEALLOC
1597 #define NUM_KTSB_DESCR 2
1599 #define NUM_KTSB_DESCR 1
1601 static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
1602 extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1604 static void __init sun4v_ktsb_init(void)
1606 unsigned long ktsb_pa;
1608 /* First KTSB for PAGE_SIZE mappings. */
1609 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1611 switch (PAGE_SIZE) {
1614 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1615 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1619 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1620 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1624 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1625 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1628 case 4 * 1024 * 1024:
1629 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1630 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1634 ktsb_descr[0].assoc = 1;
1635 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1636 ktsb_descr[0].ctx_idx = 0;
1637 ktsb_descr[0].tsb_base = ktsb_pa;
1638 ktsb_descr[0].resv = 0;
1640 #ifndef CONFIG_DEBUG_PAGEALLOC
1641 /* Second KTSB for 4MB/256MB mappings. */
1642 ktsb_pa = (kern_base +
1643 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1645 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
1646 ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
1647 HV_PGSZ_MASK_256MB);
1648 ktsb_descr[1].assoc = 1;
1649 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
1650 ktsb_descr[1].ctx_idx = 0;
1651 ktsb_descr[1].tsb_base = ktsb_pa;
1652 ktsb_descr[1].resv = 0;
1656 void __cpuinit sun4v_ktsb_register(void)
1658 unsigned long pa, ret;
1660 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1662 ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
1664 prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
1665 "errors with %lx\n", pa, ret);
1670 /* paging_init() sets up the page tables */
1672 static unsigned long last_valid_pfn;
1673 pgd_t swapper_pg_dir[2048];
1675 static void sun4u_pgprot_init(void);
1676 static void sun4v_pgprot_init(void);
1678 /* Dummy function */
1679 void __init setup_per_cpu_areas(void)
1683 void __init paging_init(void)
1685 unsigned long end_pfn, shift, phys_base;
1686 unsigned long real_end, i;
1688 /* These build time checkes make sure that the dcache_dirty_cpu()
1689 * page->flags usage will work.
1691 * When a page gets marked as dcache-dirty, we store the
1692 * cpu number starting at bit 32 in the page->flags. Also,
1693 * functions like clear_dcache_dirty_cpu use the cpu mask
1694 * in 13-bit signed-immediate instruction fields.
1698 * Page flags must not reach into upper 32 bits that are used
1699 * for the cpu number
1701 BUILD_BUG_ON(NR_PAGEFLAGS > 32);
1704 * The bit fields placed in the high range must not reach below
1705 * the 32 bit boundary. Otherwise we cannot place the cpu field
1706 * at the 32 bit boundary.
1708 BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
1709 ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
1711 BUILD_BUG_ON(NR_CPUS > 4096);
1713 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1714 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1716 /* Invalidate both kernel TSBs. */
1717 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
1718 #ifndef CONFIG_DEBUG_PAGEALLOC
1719 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
1722 if (tlb_type == hypervisor)
1723 sun4v_pgprot_init();
1725 sun4u_pgprot_init();
1727 if (tlb_type == cheetah_plus ||
1728 tlb_type == hypervisor)
1731 if (tlb_type == hypervisor) {
1732 sun4v_patch_tlb_handlers();
1738 /* Find available physical memory...
1740 * Read it twice in order to work around a bug in openfirmware.
1741 * The call to grab this table itself can cause openfirmware to
1742 * allocate memory, which in turn can take away some space from
1743 * the list of available memory. Reading it twice makes sure
1744 * we really do get the final value.
1746 read_obp_translations();
1747 read_obp_memory("reg", &pall[0], &pall_ents);
1748 read_obp_memory("available", &pavail[0], &pavail_ents);
1749 read_obp_memory("available", &pavail[0], &pavail_ents);
1751 phys_base = 0xffffffffffffffffUL;
1752 for (i = 0; i < pavail_ents; i++) {
1753 phys_base = min(phys_base, pavail[i].phys_addr);
1754 lmb_add(pavail[i].phys_addr, pavail[i].reg_size);
1757 lmb_reserve(kern_base, kern_size);
1759 find_ramdisk(phys_base);
1761 lmb_enforce_memory_limit(cmdline_memory_size);
1766 set_bit(0, mmu_context_bmap);
1768 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1770 real_end = (unsigned long)_end;
1771 num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << 22);
1772 printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
1773 num_kernel_image_mappings);
1775 /* Set kernel pgd to upper alias so physical page computations
1778 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1780 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
1782 /* Now can init the kernel/bad page tables. */
1783 pud_set(pud_offset(&swapper_pg_dir[0], 0),
1784 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
1786 inherit_prom_mappings();
1790 /* Ok, we can use our TLB miss and window trap handlers safely. */
1795 if (tlb_type == hypervisor)
1796 sun4v_ktsb_register();
1798 /* We must setup the per-cpu areas before we pull in the
1799 * PROM and the MDESC. The code there fills in cpu and
1800 * other information into per-cpu data structures.
1802 real_setup_per_cpu_areas();
1804 prom_build_devicetree();
1806 if (tlb_type == hypervisor)
1809 /* Once the OF device tree and MDESC have been setup, we know
1810 * the list of possible cpus. Therefore we can allocate the
1813 for_each_possible_cpu(i) {
1814 /* XXX Use node local allocations... XXX */
1815 softirq_stack[i] = __va(lmb_alloc(THREAD_SIZE, THREAD_SIZE));
1816 hardirq_stack[i] = __va(lmb_alloc(THREAD_SIZE, THREAD_SIZE));
1819 /* Setup bootmem... */
1820 last_valid_pfn = end_pfn = bootmem_init(phys_base);
1822 #ifndef CONFIG_NEED_MULTIPLE_NODES
1823 max_mapnr = last_valid_pfn;
1825 kernel_physical_mapping_init();
1828 unsigned long max_zone_pfns[MAX_NR_ZONES];
1830 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
1832 max_zone_pfns[ZONE_NORMAL] = end_pfn;
1834 free_area_init_nodes(max_zone_pfns);
1837 printk("Booting Linux...\n");
1840 int __init page_in_phys_avail(unsigned long paddr)
1846 for (i = 0; i < pavail_ents; i++) {
1847 unsigned long start, end;
1849 start = pavail[i].phys_addr;
1850 end = start + pavail[i].reg_size;
1852 if (paddr >= start && paddr < end)
1855 if (paddr >= kern_base && paddr < (kern_base + kern_size))
1857 #ifdef CONFIG_BLK_DEV_INITRD
1858 if (paddr >= __pa(initrd_start) &&
1859 paddr < __pa(PAGE_ALIGN(initrd_end)))
1866 static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
1867 static int pavail_rescan_ents __initdata;
1869 /* Certain OBP calls, such as fetching "available" properties, can
1870 * claim physical memory. So, along with initializing the valid
1871 * address bitmap, what we do here is refetch the physical available
1872 * memory list again, and make sure it provides at least as much
1873 * memory as 'pavail' does.
1875 static void __init setup_valid_addr_bitmap_from_pavail(void)
1879 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
1881 for (i = 0; i < pavail_ents; i++) {
1882 unsigned long old_start, old_end;
1884 old_start = pavail[i].phys_addr;
1885 old_end = old_start + pavail[i].reg_size;
1886 while (old_start < old_end) {
1889 for (n = 0; n < pavail_rescan_ents; n++) {
1890 unsigned long new_start, new_end;
1892 new_start = pavail_rescan[n].phys_addr;
1893 new_end = new_start +
1894 pavail_rescan[n].reg_size;
1896 if (new_start <= old_start &&
1897 new_end >= (old_start + PAGE_SIZE)) {
1898 set_bit(old_start >> 22,
1899 sparc64_valid_addr_bitmap);
1904 prom_printf("mem_init: Lost memory in pavail\n");
1905 prom_printf("mem_init: OLD start[%lx] size[%lx]\n",
1906 pavail[i].phys_addr,
1907 pavail[i].reg_size);
1908 prom_printf("mem_init: NEW start[%lx] size[%lx]\n",
1909 pavail_rescan[i].phys_addr,
1910 pavail_rescan[i].reg_size);
1911 prom_printf("mem_init: Cannot continue, aborting.\n");
1915 old_start += PAGE_SIZE;
1920 void __init mem_init(void)
1922 unsigned long codepages, datapages, initpages;
1923 unsigned long addr, last;
1926 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1928 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
1929 if (sparc64_valid_addr_bitmap == NULL) {
1930 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1933 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1935 addr = PAGE_OFFSET + kern_base;
1936 last = PAGE_ALIGN(kern_size) + addr;
1937 while (addr < last) {
1938 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1942 setup_valid_addr_bitmap_from_pavail();
1944 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1946 #ifdef CONFIG_NEED_MULTIPLE_NODES
1947 for_each_online_node(i) {
1948 if (NODE_DATA(i)->node_spanned_pages != 0) {
1950 free_all_bootmem_node(NODE_DATA(i));
1954 totalram_pages = free_all_bootmem();
1957 /* We subtract one to account for the mem_map_zero page
1960 totalram_pages -= 1;
1961 num_physpages = totalram_pages;
1964 * Set up the zero page, mark it reserved, so that page count
1965 * is not manipulated when freeing the page from user ptes.
1967 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1968 if (mem_map_zero == NULL) {
1969 prom_printf("paging_init: Cannot alloc zero page.\n");
1972 SetPageReserved(mem_map_zero);
1974 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1975 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1976 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1977 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1978 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1979 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1981 printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1982 nr_free_pages() << (PAGE_SHIFT-10),
1983 codepages << (PAGE_SHIFT-10),
1984 datapages << (PAGE_SHIFT-10),
1985 initpages << (PAGE_SHIFT-10),
1986 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1988 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1989 cheetah_ecache_flush_init();
1992 void free_initmem(void)
1994 unsigned long addr, initend;
1997 /* If the physical memory maps were trimmed by kernel command
1998 * line options, don't even try freeing this initmem stuff up.
1999 * The kernel image could have been in the trimmed out region
2000 * and if so the freeing below will free invalid page structs.
2002 if (cmdline_memory_size)
2006 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
2008 addr = PAGE_ALIGN((unsigned long)(__init_begin));
2009 initend = (unsigned long)(__init_end) & PAGE_MASK;
2010 for (; addr < initend; addr += PAGE_SIZE) {
2015 ((unsigned long) __va(kern_base)) -
2016 ((unsigned long) KERNBASE));
2017 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
2020 p = virt_to_page(page);
2022 ClearPageReserved(p);
2031 #ifdef CONFIG_BLK_DEV_INITRD
2032 void free_initrd_mem(unsigned long start, unsigned long end)
2035 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
2036 for (; start < end; start += PAGE_SIZE) {
2037 struct page *p = virt_to_page(start);
2039 ClearPageReserved(p);
2048 #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
2049 #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
2050 #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
2051 #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
2052 #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
2053 #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
2055 pgprot_t PAGE_KERNEL __read_mostly;
2056 EXPORT_SYMBOL(PAGE_KERNEL);
2058 pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
2059 pgprot_t PAGE_COPY __read_mostly;
2061 pgprot_t PAGE_SHARED __read_mostly;
2062 EXPORT_SYMBOL(PAGE_SHARED);
2064 unsigned long pg_iobits __read_mostly;
2066 unsigned long _PAGE_IE __read_mostly;
2067 EXPORT_SYMBOL(_PAGE_IE);
2069 unsigned long _PAGE_E __read_mostly;
2070 EXPORT_SYMBOL(_PAGE_E);
2072 unsigned long _PAGE_CACHE __read_mostly;
2073 EXPORT_SYMBOL(_PAGE_CACHE);
2075 #ifdef CONFIG_SPARSEMEM_VMEMMAP
2076 unsigned long vmemmap_table[VMEMMAP_SIZE];
2078 int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
2080 unsigned long vstart = (unsigned long) start;
2081 unsigned long vend = (unsigned long) (start + nr);
2082 unsigned long phys_start = (vstart - VMEMMAP_BASE);
2083 unsigned long phys_end = (vend - VMEMMAP_BASE);
2084 unsigned long addr = phys_start & VMEMMAP_CHUNK_MASK;
2085 unsigned long end = VMEMMAP_ALIGN(phys_end);
2086 unsigned long pte_base;
2088 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2089 _PAGE_CP_4U | _PAGE_CV_4U |
2090 _PAGE_P_4U | _PAGE_W_4U);
2091 if (tlb_type == hypervisor)
2092 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2093 _PAGE_CP_4V | _PAGE_CV_4V |
2094 _PAGE_P_4V | _PAGE_W_4V);
2096 for (; addr < end; addr += VMEMMAP_CHUNK) {
2097 unsigned long *vmem_pp =
2098 vmemmap_table + (addr >> VMEMMAP_CHUNK_SHIFT);
2101 if (!(*vmem_pp & _PAGE_VALID)) {
2102 block = vmemmap_alloc_block(1UL << 22, node);
2106 *vmem_pp = pte_base | __pa(block);
2108 printk(KERN_INFO "[%p-%p] page_structs=%lu "
2109 "node=%d entry=%lu/%lu\n", start, block, nr,
2111 addr >> VMEMMAP_CHUNK_SHIFT,
2112 VMEMMAP_SIZE >> VMEMMAP_CHUNK_SHIFT);
2117 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
2119 static void prot_init_common(unsigned long page_none,
2120 unsigned long page_shared,
2121 unsigned long page_copy,
2122 unsigned long page_readonly,
2123 unsigned long page_exec_bit)
2125 PAGE_COPY = __pgprot(page_copy);
2126 PAGE_SHARED = __pgprot(page_shared);
2128 protection_map[0x0] = __pgprot(page_none);
2129 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
2130 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
2131 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
2132 protection_map[0x4] = __pgprot(page_readonly);
2133 protection_map[0x5] = __pgprot(page_readonly);
2134 protection_map[0x6] = __pgprot(page_copy);
2135 protection_map[0x7] = __pgprot(page_copy);
2136 protection_map[0x8] = __pgprot(page_none);
2137 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
2138 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
2139 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
2140 protection_map[0xc] = __pgprot(page_readonly);
2141 protection_map[0xd] = __pgprot(page_readonly);
2142 protection_map[0xe] = __pgprot(page_shared);
2143 protection_map[0xf] = __pgprot(page_shared);
2146 static void __init sun4u_pgprot_init(void)
2148 unsigned long page_none, page_shared, page_copy, page_readonly;
2149 unsigned long page_exec_bit;
2151 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2152 _PAGE_CACHE_4U | _PAGE_P_4U |
2153 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2155 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2156 _PAGE_CACHE_4U | _PAGE_P_4U |
2157 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2158 _PAGE_EXEC_4U | _PAGE_L_4U);
2160 _PAGE_IE = _PAGE_IE_4U;
2161 _PAGE_E = _PAGE_E_4U;
2162 _PAGE_CACHE = _PAGE_CACHE_4U;
2164 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
2165 __ACCESS_BITS_4U | _PAGE_E_4U);
2167 #ifdef CONFIG_DEBUG_PAGEALLOC
2168 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
2169 0xfffff80000000000UL;
2171 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
2172 0xfffff80000000000UL;
2174 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
2175 _PAGE_P_4U | _PAGE_W_4U);
2177 /* XXX Should use 256MB on Panther. XXX */
2178 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
2180 _PAGE_SZBITS = _PAGE_SZBITS_4U;
2181 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
2182 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
2183 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
2186 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
2187 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2188 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
2189 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2190 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2191 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2192 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2194 page_exec_bit = _PAGE_EXEC_4U;
2196 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2200 static void __init sun4v_pgprot_init(void)
2202 unsigned long page_none, page_shared, page_copy, page_readonly;
2203 unsigned long page_exec_bit;
2205 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
2206 _PAGE_CACHE_4V | _PAGE_P_4V |
2207 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
2209 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
2211 _PAGE_IE = _PAGE_IE_4V;
2212 _PAGE_E = _PAGE_E_4V;
2213 _PAGE_CACHE = _PAGE_CACHE_4V;
2215 #ifdef CONFIG_DEBUG_PAGEALLOC
2216 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
2217 0xfffff80000000000UL;
2219 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
2220 0xfffff80000000000UL;
2222 kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
2223 _PAGE_P_4V | _PAGE_W_4V);
2225 #ifdef CONFIG_DEBUG_PAGEALLOC
2226 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
2227 0xfffff80000000000UL;
2229 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
2230 0xfffff80000000000UL;
2232 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
2233 _PAGE_P_4V | _PAGE_W_4V);
2235 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
2236 __ACCESS_BITS_4V | _PAGE_E_4V);
2238 _PAGE_SZBITS = _PAGE_SZBITS_4V;
2239 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
2240 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
2241 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
2242 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
2244 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
2245 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2246 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
2247 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2248 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2249 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2250 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2252 page_exec_bit = _PAGE_EXEC_4V;
2254 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2258 unsigned long pte_sz_bits(unsigned long sz)
2260 if (tlb_type == hypervisor) {
2264 return _PAGE_SZ8K_4V;
2266 return _PAGE_SZ64K_4V;
2268 return _PAGE_SZ512K_4V;
2269 case 4 * 1024 * 1024:
2270 return _PAGE_SZ4MB_4V;
2276 return _PAGE_SZ8K_4U;
2278 return _PAGE_SZ64K_4U;
2280 return _PAGE_SZ512K_4U;
2281 case 4 * 1024 * 1024:
2282 return _PAGE_SZ4MB_4U;
2287 pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
2291 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
2292 pte_val(pte) |= (((unsigned long)space) << 32);
2293 pte_val(pte) |= pte_sz_bits(page_size);
2298 static unsigned long kern_large_tte(unsigned long paddr)
2302 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2303 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
2304 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
2305 if (tlb_type == hypervisor)
2306 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2307 _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
2308 _PAGE_EXEC_4V | _PAGE_W_4V);
2313 /* If not locked, zap it. */
2314 void __flush_tlb_all(void)
2316 unsigned long pstate;
2319 __asm__ __volatile__("flushw\n\t"
2320 "rdpr %%pstate, %0\n\t"
2321 "wrpr %0, %1, %%pstate"
2324 if (tlb_type == hypervisor) {
2325 sun4v_mmu_demap_all();
2326 } else if (tlb_type == spitfire) {
2327 for (i = 0; i < 64; i++) {
2328 /* Spitfire Errata #32 workaround */
2329 /* NOTE: Always runs on spitfire, so no
2330 * cheetah+ page size encodings.
2332 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2336 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2338 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
2339 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2342 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
2343 spitfire_put_dtlb_data(i, 0x0UL);
2346 /* Spitfire Errata #32 workaround */
2347 /* NOTE: Always runs on spitfire, so no
2348 * cheetah+ page size encodings.
2350 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2354 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2356 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
2357 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2360 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
2361 spitfire_put_itlb_data(i, 0x0UL);
2364 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
2365 cheetah_flush_dtlb_all();
2366 cheetah_flush_itlb_all();
2368 __asm__ __volatile__("wrpr %0, 0, %%pstate"