Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Virtual Memory Map support |
| 3 | * |
Christoph Lameter | cde5353 | 2008-07-04 09:59:22 -0700 | [diff] [blame] | 4 | * (C) 2007 sgi. Christoph Lameter. |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 5 | * |
| 6 | * Virtual memory maps allow VM primitives pfn_to_page, page_to_pfn, |
| 7 | * virt_to_page, page_address() to be implemented as a base offset |
| 8 | * calculation without memory access. |
| 9 | * |
| 10 | * However, virtual mappings need a page table and TLBs. Many Linux |
| 11 | * architectures already map their physical space using 1-1 mappings |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 12 | * via TLBs. For those arches the virtual memory map is essentially |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 13 | * for free if we use the same page size as the 1-1 mappings. In that |
| 14 | * case the overhead consists of a few additional pages that are |
| 15 | * allocated to create a view of memory for vmemmap. |
| 16 | * |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 17 | * The architecture is expected to provide a vmemmap_populate() function |
| 18 | * to instantiate the mapping. |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 19 | */ |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/mmzone.h> |
| 22 | #include <linux/bootmem.h> |
| 23 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/vmalloc.h> |
Glauber de Oliveira Costa | 8bca44b | 2007-10-29 14:37:19 -0700 | [diff] [blame] | 27 | #include <linux/sched.h> |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 28 | #include <asm/dma.h> |
| 29 | #include <asm/pgalloc.h> |
| 30 | #include <asm/pgtable.h> |
| 31 | |
| 32 | /* |
| 33 | * Allocate a block of memory to be used to back the virtual memory map |
| 34 | * or to back the page tables that are used to create the mapping. |
| 35 | * Uses the main allocators if they are available, else bootmem. |
| 36 | */ |
KAMEZAWA Hiroyuki | e0dc3a5 | 2007-11-28 16:21:57 -0800 | [diff] [blame] | 37 | |
| 38 | static void * __init_refok __earlyonly_bootmem_alloc(int node, |
| 39 | unsigned long size, |
| 40 | unsigned long align, |
| 41 | unsigned long goal) |
| 42 | { |
Santosh Shilimkar | bb016b8 | 2014-01-21 15:50:34 -0800 | [diff] [blame] | 43 | return memblock_virt_alloc_try_nid(size, align, goal, |
| 44 | BOOTMEM_ALLOC_ACCESSIBLE, node); |
KAMEZAWA Hiroyuki | e0dc3a5 | 2007-11-28 16:21:57 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Yinghai Lu | 9bdac91 | 2010-02-10 01:20:22 -0800 | [diff] [blame] | 47 | static void *vmemmap_buf; |
| 48 | static void *vmemmap_buf_end; |
KAMEZAWA Hiroyuki | e0dc3a5 | 2007-11-28 16:21:57 -0800 | [diff] [blame] | 49 | |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 50 | void * __meminit vmemmap_alloc_block(unsigned long size, int node) |
| 51 | { |
| 52 | /* If the main allocator is up use that, fallback to bootmem. */ |
| 53 | if (slab_is_available()) { |
Shaohua Li | f52407c | 2009-09-21 17:01:19 -0700 | [diff] [blame] | 54 | struct page *page; |
| 55 | |
| 56 | if (node_state(node, N_HIGH_MEMORY)) |
Ben Hutchings | 055e4fd | 2013-04-29 15:07:49 -0700 | [diff] [blame] | 57 | page = alloc_pages_node( |
| 58 | node, GFP_KERNEL | __GFP_ZERO | __GFP_REPEAT, |
| 59 | get_order(size)); |
Shaohua Li | f52407c | 2009-09-21 17:01:19 -0700 | [diff] [blame] | 60 | else |
Ben Hutchings | 055e4fd | 2013-04-29 15:07:49 -0700 | [diff] [blame] | 61 | page = alloc_pages( |
| 62 | GFP_KERNEL | __GFP_ZERO | __GFP_REPEAT, |
Shaohua Li | f52407c | 2009-09-21 17:01:19 -0700 | [diff] [blame] | 63 | get_order(size)); |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 64 | if (page) |
| 65 | return page_address(page); |
| 66 | return NULL; |
| 67 | } else |
KAMEZAWA Hiroyuki | e0dc3a5 | 2007-11-28 16:21:57 -0800 | [diff] [blame] | 68 | return __earlyonly_bootmem_alloc(node, size, size, |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 69 | __pa(MAX_DMA_ADDRESS)); |
| 70 | } |
| 71 | |
Yinghai Lu | 9bdac91 | 2010-02-10 01:20:22 -0800 | [diff] [blame] | 72 | /* need to make sure size is all the same during early stage */ |
| 73 | void * __meminit vmemmap_alloc_block_buf(unsigned long size, int node) |
| 74 | { |
| 75 | void *ptr; |
| 76 | |
| 77 | if (!vmemmap_buf) |
| 78 | return vmemmap_alloc_block(size, node); |
| 79 | |
| 80 | /* take the from buf */ |
| 81 | ptr = (void *)ALIGN((unsigned long)vmemmap_buf, size); |
| 82 | if (ptr + size > vmemmap_buf_end) |
| 83 | return vmemmap_alloc_block(size, node); |
| 84 | |
| 85 | vmemmap_buf = ptr + size; |
| 86 | |
| 87 | return ptr; |
| 88 | } |
| 89 | |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 90 | void __meminit vmemmap_verify(pte_t *pte, int node, |
| 91 | unsigned long start, unsigned long end) |
| 92 | { |
| 93 | unsigned long pfn = pte_pfn(*pte); |
| 94 | int actual_node = early_pfn_to_nid(pfn); |
| 95 | |
David Rientjes | b41ad14 | 2008-11-06 12:53:31 -0800 | [diff] [blame] | 96 | if (node_distance(actual_node, node) > LOCAL_DISTANCE) |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 97 | printk(KERN_WARNING "[%lx-%lx] potential offnode " |
| 98 | "page_structs\n", start, end - 1); |
| 99 | } |
| 100 | |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 101 | pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node) |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 102 | { |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 103 | pte_t *pte = pte_offset_kernel(pmd, addr); |
| 104 | if (pte_none(*pte)) { |
| 105 | pte_t entry; |
Yinghai Lu | 9bdac91 | 2010-02-10 01:20:22 -0800 | [diff] [blame] | 106 | void *p = vmemmap_alloc_block_buf(PAGE_SIZE, node); |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 107 | if (!p) |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 108 | return NULL; |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 109 | entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL); |
| 110 | set_pte_at(&init_mm, addr, pte, entry); |
| 111 | } |
| 112 | return pte; |
| 113 | } |
| 114 | |
| 115 | pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node) |
| 116 | { |
| 117 | pmd_t *pmd = pmd_offset(pud, addr); |
| 118 | if (pmd_none(*pmd)) { |
| 119 | void *p = vmemmap_alloc_block(PAGE_SIZE, node); |
| 120 | if (!p) |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 121 | return NULL; |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 122 | pmd_populate_kernel(&init_mm, pmd, p); |
| 123 | } |
| 124 | return pmd; |
| 125 | } |
| 126 | |
| 127 | pud_t * __meminit vmemmap_pud_populate(pgd_t *pgd, unsigned long addr, int node) |
| 128 | { |
| 129 | pud_t *pud = pud_offset(pgd, addr); |
| 130 | if (pud_none(*pud)) { |
| 131 | void *p = vmemmap_alloc_block(PAGE_SIZE, node); |
| 132 | if (!p) |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 133 | return NULL; |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 134 | pud_populate(&init_mm, pud, p); |
| 135 | } |
| 136 | return pud; |
| 137 | } |
| 138 | |
| 139 | pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node) |
| 140 | { |
| 141 | pgd_t *pgd = pgd_offset_k(addr); |
| 142 | if (pgd_none(*pgd)) { |
| 143 | void *p = vmemmap_alloc_block(PAGE_SIZE, node); |
| 144 | if (!p) |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 145 | return NULL; |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 146 | pgd_populate(&init_mm, pgd, p); |
| 147 | } |
| 148 | return pgd; |
| 149 | } |
| 150 | |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 151 | int __meminit vmemmap_populate_basepages(unsigned long start, |
| 152 | unsigned long end, int node) |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 153 | { |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 154 | unsigned long addr = start; |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 155 | pgd_t *pgd; |
| 156 | pud_t *pud; |
| 157 | pmd_t *pmd; |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 158 | pte_t *pte; |
| 159 | |
Andy Whitcroft | 29c7111 | 2007-10-16 01:24:14 -0700 | [diff] [blame] | 160 | for (; addr < end; addr += PAGE_SIZE) { |
| 161 | pgd = vmemmap_pgd_populate(addr, node); |
| 162 | if (!pgd) |
| 163 | return -ENOMEM; |
| 164 | pud = vmemmap_pud_populate(pgd, addr, node); |
| 165 | if (!pud) |
| 166 | return -ENOMEM; |
| 167 | pmd = vmemmap_pmd_populate(pud, addr, node); |
| 168 | if (!pmd) |
| 169 | return -ENOMEM; |
| 170 | pte = vmemmap_pte_populate(pmd, addr, node); |
| 171 | if (!pte) |
| 172 | return -ENOMEM; |
| 173 | vmemmap_verify(pte, node, addr, addr + PAGE_SIZE); |
| 174 | } |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 179 | struct page * __meminit sparse_mem_map_populate(unsigned long pnum, int nid) |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 180 | { |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 181 | unsigned long start; |
| 182 | unsigned long end; |
| 183 | struct page *map; |
| 184 | |
| 185 | map = pfn_to_page(pnum * PAGES_PER_SECTION); |
| 186 | start = (unsigned long)map; |
| 187 | end = (unsigned long)(map + PAGES_PER_SECTION); |
| 188 | |
| 189 | if (vmemmap_populate(start, end, nid)) |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 190 | return NULL; |
| 191 | |
| 192 | return map; |
| 193 | } |
Yinghai Lu | 9bdac91 | 2010-02-10 01:20:22 -0800 | [diff] [blame] | 194 | |
| 195 | void __init sparse_mem_maps_populate_node(struct page **map_map, |
| 196 | unsigned long pnum_begin, |
| 197 | unsigned long pnum_end, |
| 198 | unsigned long map_count, int nodeid) |
| 199 | { |
| 200 | unsigned long pnum; |
| 201 | unsigned long size = sizeof(struct page) * PAGES_PER_SECTION; |
| 202 | void *vmemmap_buf_start; |
| 203 | |
| 204 | size = ALIGN(size, PMD_SIZE); |
| 205 | vmemmap_buf_start = __earlyonly_bootmem_alloc(nodeid, size * map_count, |
| 206 | PMD_SIZE, __pa(MAX_DMA_ADDRESS)); |
| 207 | |
| 208 | if (vmemmap_buf_start) { |
| 209 | vmemmap_buf = vmemmap_buf_start; |
| 210 | vmemmap_buf_end = vmemmap_buf_start + size * map_count; |
| 211 | } |
| 212 | |
| 213 | for (pnum = pnum_begin; pnum < pnum_end; pnum++) { |
| 214 | struct mem_section *ms; |
| 215 | |
| 216 | if (!present_section_nr(pnum)) |
| 217 | continue; |
| 218 | |
| 219 | map_map[pnum] = sparse_mem_map_populate(pnum, nodeid); |
| 220 | if (map_map[pnum]) |
| 221 | continue; |
| 222 | ms = __nr_to_section(pnum); |
| 223 | printk(KERN_ERR "%s: sparsemem memory map backing failed " |
| 224 | "some memory will not be available.\n", __func__); |
| 225 | ms->section_mem_map = 0; |
| 226 | } |
| 227 | |
| 228 | if (vmemmap_buf_start) { |
| 229 | /* need to free left buf */ |
Santosh Shilimkar | bb016b8 | 2014-01-21 15:50:34 -0800 | [diff] [blame] | 230 | memblock_free_early(__pa(vmemmap_buf), |
| 231 | vmemmap_buf_end - vmemmap_buf); |
Yinghai Lu | 9bdac91 | 2010-02-10 01:20:22 -0800 | [diff] [blame] | 232 | vmemmap_buf = NULL; |
| 233 | vmemmap_buf_end = NULL; |
| 234 | } |
| 235 | } |