2 * Virtual Memory Map support
4 * (C) 2007 sgi. Christoph Lameter.
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.
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 memory 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.
17 * The architecture is expected to provide a vmemmap_populate() function
18 * to instantiate the mapping.
21 #include <linux/mmzone.h>
22 #include <linux/bootmem.h>
23 #include <linux/highmem.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/vmalloc.h>
27 #include <linux/sched.h>
29 #include <asm/pgalloc.h>
30 #include <asm/pgtable.h>
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.
38 static void * __init_refok
__earlyonly_bootmem_alloc(int node
,
43 return memblock_virt_alloc_try_nid(size
, align
, goal
,
44 BOOTMEM_ALLOC_ACCESSIBLE
, node
);
47 static void *vmemmap_buf
;
48 static void *vmemmap_buf_end
;
50 void * __meminit
vmemmap_alloc_block(unsigned long size
, int node
)
52 /* If the main allocator is up use that, fallback to bootmem. */
53 if (slab_is_available()) {
56 if (node_state(node
, N_HIGH_MEMORY
))
57 page
= alloc_pages_node(
58 node
, GFP_KERNEL
| __GFP_ZERO
| __GFP_REPEAT
,
62 GFP_KERNEL
| __GFP_ZERO
| __GFP_REPEAT
,
65 return page_address(page
);
68 return __earlyonly_bootmem_alloc(node
, size
, size
,
69 __pa(MAX_DMA_ADDRESS
));
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
)
78 return vmemmap_alloc_block(size
, node
);
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
);
85 vmemmap_buf
= ptr
+ size
;
90 void __meminit
vmemmap_verify(pte_t
*pte
, int node
,
91 unsigned long start
, unsigned long end
)
93 unsigned long pfn
= pte_pfn(*pte
);
94 int actual_node
= early_pfn_to_nid(pfn
);
96 if (node_distance(actual_node
, node
) > LOCAL_DISTANCE
)
97 printk(KERN_WARNING
"[%lx-%lx] potential offnode "
98 "page_structs\n", start
, end
- 1);
101 pte_t
* __meminit
vmemmap_pte_populate(pmd_t
*pmd
, unsigned long addr
, int node
)
103 pte_t
*pte
= pte_offset_kernel(pmd
, addr
);
104 if (pte_none(*pte
)) {
106 void *p
= vmemmap_alloc_block_buf(PAGE_SIZE
, node
);
109 entry
= pfn_pte(__pa(p
) >> PAGE_SHIFT
, PAGE_KERNEL
);
110 set_pte_at(&init_mm
, addr
, pte
, entry
);
115 pmd_t
* __meminit
vmemmap_pmd_populate(pud_t
*pud
, unsigned long addr
, int node
)
117 pmd_t
*pmd
= pmd_offset(pud
, addr
);
118 if (pmd_none(*pmd
)) {
119 void *p
= vmemmap_alloc_block(PAGE_SIZE
, node
);
122 pmd_populate_kernel(&init_mm
, pmd
, p
);
127 pud_t
* __meminit
vmemmap_pud_populate(pgd_t
*pgd
, unsigned long addr
, int node
)
129 pud_t
*pud
= pud_offset(pgd
, addr
);
130 if (pud_none(*pud
)) {
131 void *p
= vmemmap_alloc_block(PAGE_SIZE
, node
);
134 pud_populate(&init_mm
, pud
, p
);
139 pgd_t
* __meminit
vmemmap_pgd_populate(unsigned long addr
, int node
)
141 pgd_t
*pgd
= pgd_offset_k(addr
);
142 if (pgd_none(*pgd
)) {
143 void *p
= vmemmap_alloc_block(PAGE_SIZE
, node
);
146 pgd_populate(&init_mm
, pgd
, p
);
151 int __meminit
vmemmap_populate_basepages(unsigned long start
,
152 unsigned long end
, int node
)
154 unsigned long addr
= start
;
160 for (; addr
< end
; addr
+= PAGE_SIZE
) {
161 pgd
= vmemmap_pgd_populate(addr
, node
);
164 pud
= vmemmap_pud_populate(pgd
, addr
, node
);
167 pmd
= vmemmap_pmd_populate(pud
, addr
, node
);
170 pte
= vmemmap_pte_populate(pmd
, addr
, node
);
173 vmemmap_verify(pte
, node
, addr
, addr
+ PAGE_SIZE
);
179 struct page
* __meminit
sparse_mem_map_populate(unsigned long pnum
, int nid
)
185 map
= pfn_to_page(pnum
* PAGES_PER_SECTION
);
186 start
= (unsigned long)map
;
187 end
= (unsigned long)(map
+ PAGES_PER_SECTION
);
189 if (vmemmap_populate(start
, end
, nid
))
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
)
201 unsigned long size
= sizeof(struct page
) * PAGES_PER_SECTION
;
202 void *vmemmap_buf_start
;
204 size
= ALIGN(size
, PMD_SIZE
);
205 vmemmap_buf_start
= __earlyonly_bootmem_alloc(nodeid
, size
* map_count
,
206 PMD_SIZE
, __pa(MAX_DMA_ADDRESS
));
208 if (vmemmap_buf_start
) {
209 vmemmap_buf
= vmemmap_buf_start
;
210 vmemmap_buf_end
= vmemmap_buf_start
+ size
* map_count
;
213 for (pnum
= pnum_begin
; pnum
< pnum_end
; pnum
++) {
214 struct mem_section
*ms
;
216 if (!present_section_nr(pnum
))
219 map_map
[pnum
] = sparse_mem_map_populate(pnum
, nodeid
);
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;
228 if (vmemmap_buf_start
) {
229 /* need to free left buf */
230 memblock_free_early(__pa(vmemmap_buf
),
231 vmemmap_buf_end
- vmemmap_buf
);
233 vmemmap_buf_end
= NULL
;