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 unsigned long vmalloc_end;
19 EXPORT_SYMBOL(vmalloc_end);
21 static struct page *vmem_map;
22 static DEFINE_MUTEX(vmem_mutex);
24 struct memory_segment {
25 struct list_head list;
30 static LIST_HEAD(mem_segs);
32 void __meminit memmap_init(unsigned long size, int nid, unsigned long zone,
33 unsigned long start_pfn)
35 struct page *start, *end;
36 struct page *map_start, *map_end;
39 start = pfn_to_page(start_pfn);
42 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
43 unsigned long cstart, cend;
45 cstart = PFN_DOWN(memory_chunk[i].addr);
46 cend = cstart + PFN_DOWN(memory_chunk[i].size);
48 map_start = mem_map + cstart;
49 map_end = mem_map + cend;
51 if (map_start < start)
56 map_start -= ((unsigned long) map_start & (PAGE_SIZE - 1))
57 / sizeof(struct page);
58 map_end += ((PFN_ALIGN((unsigned long) map_end)
59 - (unsigned long) map_end)
60 / sizeof(struct page));
62 if (map_start < map_end)
63 memmap_init_zone((unsigned long)(map_end - map_start),
64 nid, zone, page_to_pfn(map_start),
69 static void __init_refok *vmem_alloc_pages(unsigned int order)
71 if (slab_is_available())
72 return (void *)__get_free_pages(GFP_KERNEL, order);
73 return alloc_bootmem_pages((1 << order) * PAGE_SIZE);
76 #define vmem_pud_alloc() ({ BUG(); ((pud_t *) NULL); })
78 static inline pmd_t *vmem_pmd_alloc(void)
83 pmd = vmem_alloc_pages(2);
86 clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE*4);
91 static inline pte_t *vmem_pte_alloc(void)
93 pte_t *pte = vmem_alloc_pages(0);
97 clear_table((unsigned long *) pte, _PAGE_TYPE_EMPTY, PAGE_SIZE);
102 * Add a physical memory range to the 1:1 mapping.
104 static int vmem_add_range(unsigned long start, unsigned long size)
106 unsigned long address;
114 for (address = start; address < start + size; address += PAGE_SIZE) {
115 pg_dir = pgd_offset_k(address);
116 if (pgd_none(*pg_dir)) {
117 pu_dir = vmem_pud_alloc();
120 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
123 pu_dir = pud_offset(pg_dir, address);
124 if (pud_none(*pu_dir)) {
125 pm_dir = vmem_pmd_alloc();
128 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
131 pm_dir = pmd_offset(pu_dir, address);
132 if (pmd_none(*pm_dir)) {
133 pt_dir = vmem_pte_alloc();
136 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
139 pt_dir = pte_offset_kernel(pm_dir, address);
140 pte = pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL);
145 flush_tlb_kernel_range(start, start + size);
150 * Remove a physical memory range from the 1:1 mapping.
151 * Currently only invalidates page table entries.
153 static void vmem_remove_range(unsigned long start, unsigned long size)
155 unsigned long address;
162 pte_val(pte) = _PAGE_TYPE_EMPTY;
163 for (address = start; address < start + size; address += PAGE_SIZE) {
164 pg_dir = pgd_offset_k(address);
165 pu_dir = pud_offset(pg_dir, address);
166 if (pud_none(*pu_dir))
168 pm_dir = pmd_offset(pu_dir, address);
169 if (pmd_none(*pm_dir))
171 pt_dir = pte_offset_kernel(pm_dir, address);
174 flush_tlb_kernel_range(start, start + size);
178 * Add a backed mem_map array to the virtual mem_map array.
180 static int vmem_add_mem_map(unsigned long start, unsigned long size)
182 unsigned long address, start_addr, end_addr;
183 struct page *map_start, *map_end;
191 map_start = vmem_map + PFN_DOWN(start);
192 map_end = vmem_map + PFN_DOWN(start + size);
194 start_addr = (unsigned long) map_start & PAGE_MASK;
195 end_addr = PFN_ALIGN((unsigned long) map_end);
197 for (address = start_addr; address < end_addr; address += PAGE_SIZE) {
198 pg_dir = pgd_offset_k(address);
199 if (pgd_none(*pg_dir)) {
200 pu_dir = vmem_pud_alloc();
203 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
206 pu_dir = pud_offset(pg_dir, address);
207 if (pud_none(*pu_dir)) {
208 pm_dir = vmem_pmd_alloc();
211 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
214 pm_dir = pmd_offset(pu_dir, address);
215 if (pmd_none(*pm_dir)) {
216 pt_dir = vmem_pte_alloc();
219 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
222 pt_dir = pte_offset_kernel(pm_dir, address);
223 if (pte_none(*pt_dir)) {
224 unsigned long new_page;
226 new_page =__pa(vmem_alloc_pages(0));
229 pte = pfn_pte(new_page >> PAGE_SHIFT, PAGE_KERNEL);
235 flush_tlb_kernel_range(start_addr, end_addr);
239 static int vmem_add_mem(unsigned long start, unsigned long size)
243 ret = vmem_add_range(start, size);
246 return vmem_add_mem_map(start, size);
250 * Add memory segment to the segment list if it doesn't overlap with
251 * an already present segment.
253 static int insert_memory_segment(struct memory_segment *seg)
255 struct memory_segment *tmp;
257 if (PFN_DOWN(seg->start + seg->size) > max_pfn ||
258 seg->start + seg->size < seg->start)
261 list_for_each_entry(tmp, &mem_segs, list) {
262 if (seg->start >= tmp->start + tmp->size)
264 if (seg->start + seg->size <= tmp->start)
268 list_add(&seg->list, &mem_segs);
273 * Remove memory segment from the segment list.
275 static void remove_memory_segment(struct memory_segment *seg)
277 list_del(&seg->list);
280 static void __remove_shared_memory(struct memory_segment *seg)
282 remove_memory_segment(seg);
283 vmem_remove_range(seg->start, seg->size);
286 int remove_shared_memory(unsigned long start, unsigned long size)
288 struct memory_segment *seg;
291 mutex_lock(&vmem_mutex);
294 list_for_each_entry(seg, &mem_segs, list) {
295 if (seg->start == start && seg->size == size)
299 if (seg->start != start || seg->size != size)
303 __remove_shared_memory(seg);
306 mutex_unlock(&vmem_mutex);
310 int add_shared_memory(unsigned long start, unsigned long size)
312 struct memory_segment *seg;
314 unsigned long pfn, num_pfn, end_pfn;
317 mutex_lock(&vmem_mutex);
319 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
325 ret = insert_memory_segment(seg);
329 ret = vmem_add_mem(start, size);
333 pfn = PFN_DOWN(start);
334 num_pfn = PFN_DOWN(size);
335 end_pfn = pfn + num_pfn;
337 page = pfn_to_page(pfn);
338 memset(page, 0, num_pfn * sizeof(struct page));
340 for (; pfn < end_pfn; pfn++) {
341 page = pfn_to_page(pfn);
342 init_page_count(page);
343 reset_page_mapcount(page);
344 SetPageReserved(page);
345 INIT_LIST_HEAD(&page->lru);
350 __remove_shared_memory(seg);
354 mutex_unlock(&vmem_mutex);
359 * map whole physical memory to virtual memory (identity mapping)
361 void __init vmem_map_init(void)
363 unsigned long map_size;
366 map_size = ALIGN(max_low_pfn, MAX_ORDER_NR_PAGES) * sizeof(struct page);
367 vmalloc_end = PFN_ALIGN(VMALLOC_END_INIT) - PFN_ALIGN(map_size);
368 vmem_map = (struct page *) vmalloc_end;
369 NODE_DATA(0)->node_mem_map = vmem_map;
371 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++)
372 vmem_add_mem(memory_chunk[i].addr, memory_chunk[i].size);
376 * Convert memory chunk array to a memory segment list so there is a single
377 * list that contains both r/w memory and shared memory segments.
379 static int __init vmem_convert_memory_chunk(void)
381 struct memory_segment *seg;
384 mutex_lock(&vmem_mutex);
385 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
386 if (!memory_chunk[i].size)
388 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
390 panic("Out of memory...\n");
391 seg->start = memory_chunk[i].addr;
392 seg->size = memory_chunk[i].size;
393 insert_memory_segment(seg);
395 mutex_unlock(&vmem_mutex);
399 core_initcall(vmem_convert_memory_chunk);