blob: aa33fd67fa412bee2cef32e18c163d626e88bd00 [file] [log] [blame]
Christoph Lameter8f6aac42007-10-16 01:24:13 -07001/*
2 * Virtual Memory Map support
3 *
Christoph Lametercde53532008-07-04 09:59:22 -07004 * (C) 2007 sgi. Christoph Lameter.
Christoph Lameter8f6aac42007-10-16 01:24:13 -07005 *
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
12 * via TLBs. For those arches the virtual memmory map is essentially
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 Whitcroft29c71112007-10-16 01:24:14 -070017 * The architecture is expected to provide a vmemmap_populate() function
18 * to instantiate the mapping.
Christoph Lameter8f6aac42007-10-16 01:24:13 -070019 */
20#include <linux/mm.h>
21#include <linux/mmzone.h>
22#include <linux/bootmem.h>
23#include <linux/highmem.h>
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Christoph Lameter8f6aac42007-10-16 01:24:13 -070026#include <linux/spinlock.h>
27#include <linux/vmalloc.h>
Glauber de Oliveira Costa8bca44b2007-10-29 14:37:19 -070028#include <linux/sched.h>
Christoph Lameter8f6aac42007-10-16 01:24:13 -070029#include <asm/dma.h>
30#include <asm/pgalloc.h>
31#include <asm/pgtable.h>
32
33/*
34 * Allocate a block of memory to be used to back the virtual memory map
35 * or to back the page tables that are used to create the mapping.
36 * Uses the main allocators if they are available, else bootmem.
37 */
KAMEZAWA Hiroyukie0dc3a52007-11-28 16:21:57 -080038
39static void * __init_refok __earlyonly_bootmem_alloc(int node,
40 unsigned long size,
41 unsigned long align,
42 unsigned long goal)
43{
Yinghai Lu08677212010-02-10 01:20:20 -080044 return __alloc_bootmem_node_high(NODE_DATA(node), size, align, goal);
KAMEZAWA Hiroyukie0dc3a52007-11-28 16:21:57 -080045}
46
Yinghai Lu9bdac912010-02-10 01:20:22 -080047static void *vmemmap_buf;
48static void *vmemmap_buf_end;
KAMEZAWA Hiroyukie0dc3a52007-11-28 16:21:57 -080049
Christoph Lameter8f6aac42007-10-16 01:24:13 -070050void * __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 Lif52407c2009-09-21 17:01:19 -070054 struct page *page;
55
56 if (node_state(node, N_HIGH_MEMORY))
57 page = alloc_pages_node(node,
Christoph Lameter8f6aac42007-10-16 01:24:13 -070058 GFP_KERNEL | __GFP_ZERO, get_order(size));
Shaohua Lif52407c2009-09-21 17:01:19 -070059 else
60 page = alloc_pages(GFP_KERNEL | __GFP_ZERO,
61 get_order(size));
Christoph Lameter8f6aac42007-10-16 01:24:13 -070062 if (page)
63 return page_address(page);
64 return NULL;
65 } else
KAMEZAWA Hiroyukie0dc3a52007-11-28 16:21:57 -080066 return __earlyonly_bootmem_alloc(node, size, size,
Christoph Lameter8f6aac42007-10-16 01:24:13 -070067 __pa(MAX_DMA_ADDRESS));
68}
69
Yinghai Lu9bdac912010-02-10 01:20:22 -080070/* need to make sure size is all the same during early stage */
71void * __meminit vmemmap_alloc_block_buf(unsigned long size, int node)
72{
73 void *ptr;
74
75 if (!vmemmap_buf)
76 return vmemmap_alloc_block(size, node);
77
78 /* take the from buf */
79 ptr = (void *)ALIGN((unsigned long)vmemmap_buf, size);
80 if (ptr + size > vmemmap_buf_end)
81 return vmemmap_alloc_block(size, node);
82
83 vmemmap_buf = ptr + size;
84
85 return ptr;
86}
87
Christoph Lameter8f6aac42007-10-16 01:24:13 -070088void __meminit vmemmap_verify(pte_t *pte, int node,
89 unsigned long start, unsigned long end)
90{
91 unsigned long pfn = pte_pfn(*pte);
92 int actual_node = early_pfn_to_nid(pfn);
93
David Rientjesb41ad142008-11-06 12:53:31 -080094 if (node_distance(actual_node, node) > LOCAL_DISTANCE)
Christoph Lameter8f6aac42007-10-16 01:24:13 -070095 printk(KERN_WARNING "[%lx-%lx] potential offnode "
96 "page_structs\n", start, end - 1);
97}
98
Andy Whitcroft29c71112007-10-16 01:24:14 -070099pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node)
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700100{
Andy Whitcroft29c71112007-10-16 01:24:14 -0700101 pte_t *pte = pte_offset_kernel(pmd, addr);
102 if (pte_none(*pte)) {
103 pte_t entry;
Yinghai Lu9bdac912010-02-10 01:20:22 -0800104 void *p = vmemmap_alloc_block_buf(PAGE_SIZE, node);
Andy Whitcroft29c71112007-10-16 01:24:14 -0700105 if (!p)
Al Viro9dce07f2008-03-29 03:07:28 +0000106 return NULL;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700107 entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
108 set_pte_at(&init_mm, addr, pte, entry);
109 }
110 return pte;
111}
112
113pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)
114{
115 pmd_t *pmd = pmd_offset(pud, addr);
116 if (pmd_none(*pmd)) {
117 void *p = vmemmap_alloc_block(PAGE_SIZE, node);
118 if (!p)
Al Viro9dce07f2008-03-29 03:07:28 +0000119 return NULL;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700120 pmd_populate_kernel(&init_mm, pmd, p);
121 }
122 return pmd;
123}
124
125pud_t * __meminit vmemmap_pud_populate(pgd_t *pgd, unsigned long addr, int node)
126{
127 pud_t *pud = pud_offset(pgd, addr);
128 if (pud_none(*pud)) {
129 void *p = vmemmap_alloc_block(PAGE_SIZE, node);
130 if (!p)
Al Viro9dce07f2008-03-29 03:07:28 +0000131 return NULL;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700132 pud_populate(&init_mm, pud, p);
133 }
134 return pud;
135}
136
137pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
138{
139 pgd_t *pgd = pgd_offset_k(addr);
140 if (pgd_none(*pgd)) {
141 void *p = vmemmap_alloc_block(PAGE_SIZE, node);
142 if (!p)
Al Viro9dce07f2008-03-29 03:07:28 +0000143 return NULL;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700144 pgd_populate(&init_mm, pgd, p);
145 }
146 return pgd;
147}
148
149int __meminit vmemmap_populate_basepages(struct page *start_page,
150 unsigned long size, int node)
151{
152 unsigned long addr = (unsigned long)start_page;
153 unsigned long end = (unsigned long)(start_page + size);
154 pgd_t *pgd;
155 pud_t *pud;
156 pmd_t *pmd;
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700157 pte_t *pte;
158
Andy Whitcroft29c71112007-10-16 01:24:14 -0700159 for (; addr < end; addr += PAGE_SIZE) {
160 pgd = vmemmap_pgd_populate(addr, node);
161 if (!pgd)
162 return -ENOMEM;
163 pud = vmemmap_pud_populate(pgd, addr, node);
164 if (!pud)
165 return -ENOMEM;
166 pmd = vmemmap_pmd_populate(pud, addr, node);
167 if (!pmd)
168 return -ENOMEM;
169 pte = vmemmap_pte_populate(pmd, addr, node);
170 if (!pte)
171 return -ENOMEM;
172 vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
173 }
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700174
175 return 0;
176}
177
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700178struct page * __meminit sparse_mem_map_populate(unsigned long pnum, int nid)
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700179{
180 struct page *map = pfn_to_page(pnum * PAGES_PER_SECTION);
181 int error = vmemmap_populate(map, PAGES_PER_SECTION, nid);
182 if (error)
183 return NULL;
184
185 return map;
186}
Yinghai Lu9bdac912010-02-10 01:20:22 -0800187
188void __init sparse_mem_maps_populate_node(struct page **map_map,
189 unsigned long pnum_begin,
190 unsigned long pnum_end,
191 unsigned long map_count, int nodeid)
192{
193 unsigned long pnum;
194 unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
195 void *vmemmap_buf_start;
196
197 size = ALIGN(size, PMD_SIZE);
198 vmemmap_buf_start = __earlyonly_bootmem_alloc(nodeid, size * map_count,
199 PMD_SIZE, __pa(MAX_DMA_ADDRESS));
200
201 if (vmemmap_buf_start) {
202 vmemmap_buf = vmemmap_buf_start;
203 vmemmap_buf_end = vmemmap_buf_start + size * map_count;
204 }
205
206 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
207 struct mem_section *ms;
208
209 if (!present_section_nr(pnum))
210 continue;
211
212 map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
213 if (map_map[pnum])
214 continue;
215 ms = __nr_to_section(pnum);
216 printk(KERN_ERR "%s: sparsemem memory map backing failed "
217 "some memory will not be available.\n", __func__);
218 ms->section_mem_map = 0;
219 }
220
221 if (vmemmap_buf_start) {
222 /* need to free left buf */
223#ifdef CONFIG_NO_BOOTMEM
224 free_early(__pa(vmemmap_buf_start), __pa(vmemmap_buf_end));
225 if (vmemmap_buf_start < vmemmap_buf) {
226 char name[15];
227
228 snprintf(name, sizeof(name), "MEMMAP %d", nodeid);
229 reserve_early_without_check(__pa(vmemmap_buf_start),
230 __pa(vmemmap_buf), name);
231 }
232#else
233 free_bootmem(__pa(vmemmap_buf), vmemmap_buf_end - vmemmap_buf);
234#endif
235 vmemmap_buf = NULL;
236 vmemmap_buf_end = NULL;
237 }
238}