4 * Copyright IBM Corp. 2006
5 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
8 #include <linux/bootmem.h>
11 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <asm/pgalloc.h>
14 #include <asm/pgtable.h>
15 #include <asm/setup.h>
16 #include <asm/tlbflush.h>
18 static DEFINE_MUTEX(vmem_mutex);
20 struct memory_segment {
21 struct list_head list;
26 static LIST_HEAD(mem_segs);
28 void __meminit memmap_init(unsigned long size, int nid, unsigned long zone,
29 unsigned long start_pfn)
31 struct page *start, *end;
32 struct page *map_start, *map_end;
35 start = pfn_to_page(start_pfn);
38 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
39 unsigned long cstart, cend;
41 cstart = PFN_DOWN(memory_chunk[i].addr);
42 cend = cstart + PFN_DOWN(memory_chunk[i].size);
44 map_start = mem_map + cstart;
45 map_end = mem_map + cend;
47 if (map_start < start)
52 map_start -= ((unsigned long) map_start & (PAGE_SIZE - 1))
53 / sizeof(struct page);
54 map_end += ((PFN_ALIGN((unsigned long) map_end)
55 - (unsigned long) map_end)
56 / sizeof(struct page));
58 if (map_start < map_end)
59 memmap_init_zone((unsigned long)(map_end - map_start),
60 nid, zone, page_to_pfn(map_start),
65 static void __ref *vmem_alloc_pages(unsigned int order)
67 if (slab_is_available())
68 return (void *)__get_free_pages(GFP_KERNEL, order);
69 return alloc_bootmem_pages((1 << order) * PAGE_SIZE);
72 static inline pud_t *vmem_pud_alloc(void)
77 pud = vmem_alloc_pages(2);
80 pud_val(*pud) = _REGION3_ENTRY_EMPTY;
81 memcpy(pud + 1, pud, (PTRS_PER_PUD - 1)*sizeof(pud_t));
86 static inline pmd_t *vmem_pmd_alloc(void)
91 pmd = vmem_alloc_pages(2);
94 clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE*4);
99 static pte_t __init_refok *vmem_pte_alloc(void)
103 if (slab_is_available())
104 pte = (pte_t *) page_table_alloc(&init_mm);
106 pte = alloc_bootmem(PTRS_PER_PTE * sizeof(pte_t));
109 clear_table((unsigned long *) pte, _PAGE_TYPE_EMPTY,
110 PTRS_PER_PTE * sizeof(pte_t));
115 * Add a physical memory range to the 1:1 mapping.
117 static int vmem_add_range(unsigned long start, unsigned long size)
119 unsigned long address;
127 for (address = start; address < start + size; address += PAGE_SIZE) {
128 pg_dir = pgd_offset_k(address);
129 if (pgd_none(*pg_dir)) {
130 pu_dir = vmem_pud_alloc();
133 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
136 pu_dir = pud_offset(pg_dir, address);
137 if (pud_none(*pu_dir)) {
138 pm_dir = vmem_pmd_alloc();
141 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
144 pm_dir = pmd_offset(pu_dir, address);
145 if (pmd_none(*pm_dir)) {
146 pt_dir = vmem_pte_alloc();
149 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
152 pt_dir = pte_offset_kernel(pm_dir, address);
153 pte = pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL);
158 flush_tlb_kernel_range(start, start + size);
163 * Remove a physical memory range from the 1:1 mapping.
164 * Currently only invalidates page table entries.
166 static void vmem_remove_range(unsigned long start, unsigned long size)
168 unsigned long address;
175 pte_val(pte) = _PAGE_TYPE_EMPTY;
176 for (address = start; address < start + size; address += PAGE_SIZE) {
177 pg_dir = pgd_offset_k(address);
178 pu_dir = pud_offset(pg_dir, address);
179 if (pud_none(*pu_dir))
181 pm_dir = pmd_offset(pu_dir, address);
182 if (pmd_none(*pm_dir))
184 pt_dir = pte_offset_kernel(pm_dir, address);
187 flush_tlb_kernel_range(start, start + size);
191 * Add a backed mem_map array to the virtual mem_map array.
193 static int vmem_add_mem_map(unsigned long start, unsigned long size)
195 unsigned long address, start_addr, end_addr;
196 struct page *map_start, *map_end;
204 map_start = VMEM_MAP + PFN_DOWN(start);
205 map_end = VMEM_MAP + PFN_DOWN(start + size);
207 start_addr = (unsigned long) map_start & PAGE_MASK;
208 end_addr = PFN_ALIGN((unsigned long) map_end);
210 for (address = start_addr; address < end_addr; address += PAGE_SIZE) {
211 pg_dir = pgd_offset_k(address);
212 if (pgd_none(*pg_dir)) {
213 pu_dir = vmem_pud_alloc();
216 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
219 pu_dir = pud_offset(pg_dir, address);
220 if (pud_none(*pu_dir)) {
221 pm_dir = vmem_pmd_alloc();
224 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
227 pm_dir = pmd_offset(pu_dir, address);
228 if (pmd_none(*pm_dir)) {
229 pt_dir = vmem_pte_alloc();
232 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
235 pt_dir = pte_offset_kernel(pm_dir, address);
236 if (pte_none(*pt_dir)) {
237 unsigned long new_page;
239 new_page =__pa(vmem_alloc_pages(0));
242 pte = pfn_pte(new_page >> PAGE_SHIFT, PAGE_KERNEL);
248 flush_tlb_kernel_range(start_addr, end_addr);
252 static int vmem_add_mem(unsigned long start, unsigned long size)
256 ret = vmem_add_mem_map(start, size);
259 return vmem_add_range(start, size);
263 * Add memory segment to the segment list if it doesn't overlap with
264 * an already present segment.
266 static int insert_memory_segment(struct memory_segment *seg)
268 struct memory_segment *tmp;
270 if (seg->start + seg->size >= VMEM_MAX_PHYS ||
271 seg->start + seg->size < seg->start)
274 list_for_each_entry(tmp, &mem_segs, list) {
275 if (seg->start >= tmp->start + tmp->size)
277 if (seg->start + seg->size <= tmp->start)
281 list_add(&seg->list, &mem_segs);
286 * Remove memory segment from the segment list.
288 static void remove_memory_segment(struct memory_segment *seg)
290 list_del(&seg->list);
293 static void __remove_shared_memory(struct memory_segment *seg)
295 remove_memory_segment(seg);
296 vmem_remove_range(seg->start, seg->size);
299 int remove_shared_memory(unsigned long start, unsigned long size)
301 struct memory_segment *seg;
304 mutex_lock(&vmem_mutex);
307 list_for_each_entry(seg, &mem_segs, list) {
308 if (seg->start == start && seg->size == size)
312 if (seg->start != start || seg->size != size)
316 __remove_shared_memory(seg);
319 mutex_unlock(&vmem_mutex);
323 int add_shared_memory(unsigned long start, unsigned long size)
325 struct memory_segment *seg;
327 unsigned long pfn, num_pfn, end_pfn;
330 mutex_lock(&vmem_mutex);
332 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
338 ret = insert_memory_segment(seg);
342 ret = vmem_add_mem(start, size);
346 pfn = PFN_DOWN(start);
347 num_pfn = PFN_DOWN(size);
348 end_pfn = pfn + num_pfn;
350 page = pfn_to_page(pfn);
351 memset(page, 0, num_pfn * sizeof(struct page));
353 for (; pfn < end_pfn; pfn++) {
354 page = pfn_to_page(pfn);
355 init_page_count(page);
356 reset_page_mapcount(page);
357 SetPageReserved(page);
358 INIT_LIST_HEAD(&page->lru);
363 __remove_shared_memory(seg);
367 mutex_unlock(&vmem_mutex);
372 * map whole physical memory to virtual memory (identity mapping)
373 * we reserve enough space in the vmalloc area for vmemmap to hotplug
374 * additional memory segments.
376 void __init vmem_map_init(void)
380 INIT_LIST_HEAD(&init_mm.context.crst_list);
381 INIT_LIST_HEAD(&init_mm.context.pgtable_list);
382 init_mm.context.noexec = 0;
383 NODE_DATA(0)->node_mem_map = VMEM_MAP;
384 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++)
385 vmem_add_mem(memory_chunk[i].addr, memory_chunk[i].size);
389 * Convert memory chunk array to a memory segment list so there is a single
390 * list that contains both r/w memory and shared memory segments.
392 static int __init vmem_convert_memory_chunk(void)
394 struct memory_segment *seg;
397 mutex_lock(&vmem_mutex);
398 for (i = 0; i < MEMORY_CHUNKS; i++) {
399 if (!memory_chunk[i].size)
401 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
403 panic("Out of memory...\n");
404 seg->start = memory_chunk[i].addr;
405 seg->size = memory_chunk[i].size;
406 insert_memory_segment(seg);
408 mutex_unlock(&vmem_mutex);
412 core_initcall(vmem_convert_memory_chunk);