18f6aac41SChristoph Lameter /* 28f6aac41SChristoph Lameter * Virtual Memory Map support 38f6aac41SChristoph Lameter * 48f6aac41SChristoph Lameter * (C) 2007 sgi. Christoph Lameter <clameter@sgi.com>. 58f6aac41SChristoph Lameter * 68f6aac41SChristoph Lameter * Virtual memory maps allow VM primitives pfn_to_page, page_to_pfn, 78f6aac41SChristoph Lameter * virt_to_page, page_address() to be implemented as a base offset 88f6aac41SChristoph Lameter * calculation without memory access. 98f6aac41SChristoph Lameter * 108f6aac41SChristoph Lameter * However, virtual mappings need a page table and TLBs. Many Linux 118f6aac41SChristoph Lameter * architectures already map their physical space using 1-1 mappings 128f6aac41SChristoph Lameter * via TLBs. For those arches the virtual memmory map is essentially 138f6aac41SChristoph Lameter * for free if we use the same page size as the 1-1 mappings. In that 148f6aac41SChristoph Lameter * case the overhead consists of a few additional pages that are 158f6aac41SChristoph Lameter * allocated to create a view of memory for vmemmap. 168f6aac41SChristoph Lameter * 1729c71111SAndy Whitcroft * The architecture is expected to provide a vmemmap_populate() function 1829c71111SAndy Whitcroft * to instantiate the mapping. 198f6aac41SChristoph Lameter */ 208f6aac41SChristoph Lameter #include <linux/mm.h> 218f6aac41SChristoph Lameter #include <linux/mmzone.h> 228f6aac41SChristoph Lameter #include <linux/bootmem.h> 238f6aac41SChristoph Lameter #include <linux/highmem.h> 248f6aac41SChristoph Lameter #include <linux/module.h> 258f6aac41SChristoph Lameter #include <linux/spinlock.h> 268f6aac41SChristoph Lameter #include <linux/vmalloc.h> 278bca44bbSGlauber de Oliveira Costa #include <linux/sched.h> 288f6aac41SChristoph Lameter #include <asm/dma.h> 298f6aac41SChristoph Lameter #include <asm/pgalloc.h> 308f6aac41SChristoph Lameter #include <asm/pgtable.h> 318f6aac41SChristoph Lameter 328f6aac41SChristoph Lameter /* 338f6aac41SChristoph Lameter * Allocate a block of memory to be used to back the virtual memory map 348f6aac41SChristoph Lameter * or to back the page tables that are used to create the mapping. 358f6aac41SChristoph Lameter * Uses the main allocators if they are available, else bootmem. 368f6aac41SChristoph Lameter */ 37e0dc3a53SKAMEZAWA Hiroyuki 38e0dc3a53SKAMEZAWA Hiroyuki static void * __init_refok __earlyonly_bootmem_alloc(int node, 39e0dc3a53SKAMEZAWA Hiroyuki unsigned long size, 40e0dc3a53SKAMEZAWA Hiroyuki unsigned long align, 41e0dc3a53SKAMEZAWA Hiroyuki unsigned long goal) 42e0dc3a53SKAMEZAWA Hiroyuki { 43e0dc3a53SKAMEZAWA Hiroyuki return __alloc_bootmem_node(NODE_DATA(node), size, align, goal); 44e0dc3a53SKAMEZAWA Hiroyuki } 45e0dc3a53SKAMEZAWA Hiroyuki 46e0dc3a53SKAMEZAWA Hiroyuki 478f6aac41SChristoph Lameter void * __meminit vmemmap_alloc_block(unsigned long size, int node) 488f6aac41SChristoph Lameter { 498f6aac41SChristoph Lameter /* If the main allocator is up use that, fallback to bootmem. */ 508f6aac41SChristoph Lameter if (slab_is_available()) { 518f6aac41SChristoph Lameter struct page *page = alloc_pages_node(node, 528f6aac41SChristoph Lameter GFP_KERNEL | __GFP_ZERO, get_order(size)); 538f6aac41SChristoph Lameter if (page) 548f6aac41SChristoph Lameter return page_address(page); 558f6aac41SChristoph Lameter return NULL; 568f6aac41SChristoph Lameter } else 57e0dc3a53SKAMEZAWA Hiroyuki return __earlyonly_bootmem_alloc(node, size, size, 588f6aac41SChristoph Lameter __pa(MAX_DMA_ADDRESS)); 598f6aac41SChristoph Lameter } 608f6aac41SChristoph Lameter 618f6aac41SChristoph Lameter void __meminit vmemmap_verify(pte_t *pte, int node, 628f6aac41SChristoph Lameter unsigned long start, unsigned long end) 638f6aac41SChristoph Lameter { 648f6aac41SChristoph Lameter unsigned long pfn = pte_pfn(*pte); 658f6aac41SChristoph Lameter int actual_node = early_pfn_to_nid(pfn); 668f6aac41SChristoph Lameter 678f6aac41SChristoph Lameter if (actual_node != node) 688f6aac41SChristoph Lameter printk(KERN_WARNING "[%lx-%lx] potential offnode " 698f6aac41SChristoph Lameter "page_structs\n", start, end - 1); 708f6aac41SChristoph Lameter } 718f6aac41SChristoph Lameter 7229c71111SAndy Whitcroft pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node) 738f6aac41SChristoph Lameter { 7429c71111SAndy Whitcroft pte_t *pte = pte_offset_kernel(pmd, addr); 758f6aac41SChristoph Lameter if (pte_none(*pte)) { 768f6aac41SChristoph Lameter pte_t entry; 778f6aac41SChristoph Lameter void *p = vmemmap_alloc_block(PAGE_SIZE, node); 788f6aac41SChristoph Lameter if (!p) 79*9dce07f1SAl Viro return NULL; 8029c71111SAndy Whitcroft entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL); 8129c71111SAndy Whitcroft set_pte_at(&init_mm, addr, pte, entry); 8229c71111SAndy Whitcroft } 8329c71111SAndy Whitcroft return pte; 848f6aac41SChristoph Lameter } 858f6aac41SChristoph Lameter 8629c71111SAndy Whitcroft pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node) 878f6aac41SChristoph Lameter { 8829c71111SAndy Whitcroft pmd_t *pmd = pmd_offset(pud, addr); 898f6aac41SChristoph Lameter if (pmd_none(*pmd)) { 908f6aac41SChristoph Lameter void *p = vmemmap_alloc_block(PAGE_SIZE, node); 918f6aac41SChristoph Lameter if (!p) 92*9dce07f1SAl Viro return NULL; 938f6aac41SChristoph Lameter pmd_populate_kernel(&init_mm, pmd, p); 948f6aac41SChristoph Lameter } 9529c71111SAndy Whitcroft return pmd; 968f6aac41SChristoph Lameter } 978f6aac41SChristoph Lameter 9829c71111SAndy Whitcroft pud_t * __meminit vmemmap_pud_populate(pgd_t *pgd, unsigned long addr, int node) 998f6aac41SChristoph Lameter { 10029c71111SAndy Whitcroft pud_t *pud = pud_offset(pgd, addr); 1018f6aac41SChristoph Lameter if (pud_none(*pud)) { 1028f6aac41SChristoph Lameter void *p = vmemmap_alloc_block(PAGE_SIZE, node); 1038f6aac41SChristoph Lameter if (!p) 104*9dce07f1SAl Viro return NULL; 1058f6aac41SChristoph Lameter pud_populate(&init_mm, pud, p); 1068f6aac41SChristoph Lameter } 10729c71111SAndy Whitcroft return pud; 1088f6aac41SChristoph Lameter } 1098f6aac41SChristoph Lameter 11029c71111SAndy Whitcroft pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node) 1118f6aac41SChristoph Lameter { 11229c71111SAndy Whitcroft pgd_t *pgd = pgd_offset_k(addr); 1138f6aac41SChristoph Lameter if (pgd_none(*pgd)) { 1148f6aac41SChristoph Lameter void *p = vmemmap_alloc_block(PAGE_SIZE, node); 1158f6aac41SChristoph Lameter if (!p) 116*9dce07f1SAl Viro return NULL; 1178f6aac41SChristoph Lameter pgd_populate(&init_mm, pgd, p); 1188f6aac41SChristoph Lameter } 11929c71111SAndy Whitcroft return pgd; 1208f6aac41SChristoph Lameter } 12129c71111SAndy Whitcroft 12229c71111SAndy Whitcroft int __meminit vmemmap_populate_basepages(struct page *start_page, 12329c71111SAndy Whitcroft unsigned long size, int node) 12429c71111SAndy Whitcroft { 12529c71111SAndy Whitcroft unsigned long addr = (unsigned long)start_page; 12629c71111SAndy Whitcroft unsigned long end = (unsigned long)(start_page + size); 12729c71111SAndy Whitcroft pgd_t *pgd; 12829c71111SAndy Whitcroft pud_t *pud; 12929c71111SAndy Whitcroft pmd_t *pmd; 13029c71111SAndy Whitcroft pte_t *pte; 13129c71111SAndy Whitcroft 13229c71111SAndy Whitcroft for (; addr < end; addr += PAGE_SIZE) { 13329c71111SAndy Whitcroft pgd = vmemmap_pgd_populate(addr, node); 13429c71111SAndy Whitcroft if (!pgd) 13529c71111SAndy Whitcroft return -ENOMEM; 13629c71111SAndy Whitcroft pud = vmemmap_pud_populate(pgd, addr, node); 13729c71111SAndy Whitcroft if (!pud) 13829c71111SAndy Whitcroft return -ENOMEM; 13929c71111SAndy Whitcroft pmd = vmemmap_pmd_populate(pud, addr, node); 14029c71111SAndy Whitcroft if (!pmd) 14129c71111SAndy Whitcroft return -ENOMEM; 14229c71111SAndy Whitcroft pte = vmemmap_pte_populate(pmd, addr, node); 14329c71111SAndy Whitcroft if (!pte) 14429c71111SAndy Whitcroft return -ENOMEM; 14529c71111SAndy Whitcroft vmemmap_verify(pte, node, addr, addr + PAGE_SIZE); 1468f6aac41SChristoph Lameter } 14729c71111SAndy Whitcroft 14829c71111SAndy Whitcroft return 0; 14929c71111SAndy Whitcroft } 1508f6aac41SChristoph Lameter 15198f3cfc1SYasunori Goto struct page * __meminit sparse_mem_map_populate(unsigned long pnum, int nid) 1528f6aac41SChristoph Lameter { 1538f6aac41SChristoph Lameter struct page *map = pfn_to_page(pnum * PAGES_PER_SECTION); 1548f6aac41SChristoph Lameter int error = vmemmap_populate(map, PAGES_PER_SECTION, nid); 1558f6aac41SChristoph Lameter if (error) 1568f6aac41SChristoph Lameter return NULL; 1578f6aac41SChristoph Lameter 1588f6aac41SChristoph Lameter return map; 1598f6aac41SChristoph Lameter } 160