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 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));
68 static inline void *vmem_alloc_pages(unsigned int order)
70 if (slab_is_available())
71 return (void *)__get_free_pages(GFP_KERNEL, order);
72 return alloc_bootmem_pages((1 << order) * PAGE_SIZE);
75 static inline pmd_t *vmem_pmd_alloc(void)
80 pmd = vmem_alloc_pages(PMD_ALLOC_ORDER);
83 for (i = 0; i < PTRS_PER_PMD; i++)
88 static inline pte_t *vmem_pte_alloc(void)
94 pte = vmem_alloc_pages(PTE_ALLOC_ORDER);
97 pte_val(empty_pte) = _PAGE_TYPE_EMPTY;
98 for (i = 0; i < PTRS_PER_PTE; i++)
99 set_pte(pte + i, empty_pte);
104 * Add a physical memory range to the 1:1 mapping.
106 static int vmem_add_range(unsigned long start, unsigned long size)
108 unsigned long address;
115 for (address = start; address < start + size; address += PAGE_SIZE) {
116 pg_dir = pgd_offset_k(address);
117 if (pgd_none(*pg_dir)) {
118 pm_dir = vmem_pmd_alloc();
121 pgd_populate(&init_mm, pg_dir, pm_dir);
124 pm_dir = pmd_offset(pg_dir, address);
125 if (pmd_none(*pm_dir)) {
126 pt_dir = vmem_pte_alloc();
129 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
132 pt_dir = pte_offset_kernel(pm_dir, address);
133 pte = pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL);
134 set_pte(pt_dir, pte);
138 flush_tlb_kernel_range(start, start + size);
143 * Remove a physical memory range from the 1:1 mapping.
144 * Currently only invalidates page table entries.
146 static void vmem_remove_range(unsigned long start, unsigned long size)
148 unsigned long address;
154 pte_val(pte) = _PAGE_TYPE_EMPTY;
155 for (address = start; address < start + size; address += PAGE_SIZE) {
156 pg_dir = pgd_offset_k(address);
157 if (pgd_none(*pg_dir))
159 pm_dir = pmd_offset(pg_dir, address);
160 if (pmd_none(*pm_dir))
162 pt_dir = pte_offset_kernel(pm_dir, address);
163 set_pte(pt_dir, pte);
165 flush_tlb_kernel_range(start, start + size);
169 * Add a backed mem_map array to the virtual mem_map array.
171 static int vmem_add_mem_map(unsigned long start, unsigned long size)
173 unsigned long address, start_addr, end_addr;
174 struct page *map_start, *map_end;
181 map_start = vmem_map + PFN_DOWN(start);
182 map_end = vmem_map + PFN_DOWN(start + size);
184 start_addr = (unsigned long) map_start & PAGE_MASK;
185 end_addr = PFN_ALIGN((unsigned long) map_end);
187 for (address = start_addr; address < end_addr; address += PAGE_SIZE) {
188 pg_dir = pgd_offset_k(address);
189 if (pgd_none(*pg_dir)) {
190 pm_dir = vmem_pmd_alloc();
193 pgd_populate(&init_mm, pg_dir, pm_dir);
196 pm_dir = pmd_offset(pg_dir, address);
197 if (pmd_none(*pm_dir)) {
198 pt_dir = vmem_pte_alloc();
201 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
204 pt_dir = pte_offset_kernel(pm_dir, address);
205 if (pte_none(*pt_dir)) {
206 unsigned long new_page;
208 new_page =__pa(vmem_alloc_pages(0));
211 pte = pfn_pte(new_page >> PAGE_SHIFT, PAGE_KERNEL);
212 set_pte(pt_dir, pte);
217 flush_tlb_kernel_range(start_addr, end_addr);
221 static int vmem_add_mem(unsigned long start, unsigned long size)
225 ret = vmem_add_range(start, size);
228 return vmem_add_mem_map(start, size);
232 * Add memory segment to the segment list if it doesn't overlap with
233 * an already present segment.
235 static int insert_memory_segment(struct memory_segment *seg)
237 struct memory_segment *tmp;
239 if (PFN_DOWN(seg->start + seg->size) > max_pfn ||
240 seg->start + seg->size < seg->start)
243 list_for_each_entry(tmp, &mem_segs, list) {
244 if (seg->start >= tmp->start + tmp->size)
246 if (seg->start + seg->size <= tmp->start)
250 list_add(&seg->list, &mem_segs);
255 * Remove memory segment from the segment list.
257 static void remove_memory_segment(struct memory_segment *seg)
259 list_del(&seg->list);
262 static void __remove_shared_memory(struct memory_segment *seg)
264 remove_memory_segment(seg);
265 vmem_remove_range(seg->start, seg->size);
268 int remove_shared_memory(unsigned long start, unsigned long size)
270 struct memory_segment *seg;
273 mutex_lock(&vmem_mutex);
276 list_for_each_entry(seg, &mem_segs, list) {
277 if (seg->start == start && seg->size == size)
281 if (seg->start != start || seg->size != size)
285 __remove_shared_memory(seg);
288 mutex_unlock(&vmem_mutex);
292 int add_shared_memory(unsigned long start, unsigned long size)
294 struct memory_segment *seg;
296 unsigned long pfn, num_pfn, end_pfn;
299 mutex_lock(&vmem_mutex);
301 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
307 ret = insert_memory_segment(seg);
311 ret = vmem_add_mem(start, size);
315 pfn = PFN_DOWN(start);
316 num_pfn = PFN_DOWN(size);
317 end_pfn = pfn + num_pfn;
319 page = pfn_to_page(pfn);
320 memset(page, 0, num_pfn * sizeof(struct page));
322 for (; pfn < end_pfn; pfn++) {
323 page = pfn_to_page(pfn);
324 init_page_count(page);
325 reset_page_mapcount(page);
326 SetPageReserved(page);
327 INIT_LIST_HEAD(&page->lru);
332 __remove_shared_memory(seg);
336 mutex_unlock(&vmem_mutex);
341 * map whole physical memory to virtual memory (identity mapping)
343 void __init vmem_map_init(void)
345 unsigned long map_size;
348 map_size = ALIGN(max_low_pfn, MAX_ORDER_NR_PAGES) * sizeof(struct page);
349 vmalloc_end = PFN_ALIGN(VMALLOC_END_INIT) - PFN_ALIGN(map_size);
350 vmem_map = (struct page *) vmalloc_end;
351 NODE_DATA(0)->node_mem_map = vmem_map;
353 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++)
354 vmem_add_mem(memory_chunk[i].addr, memory_chunk[i].size);
358 * Convert memory chunk array to a memory segment list so there is a single
359 * list that contains both r/w memory and shared memory segments.
361 static int __init vmem_convert_memory_chunk(void)
363 struct memory_segment *seg;
366 mutex_lock(&vmem_mutex);
367 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
368 if (!memory_chunk[i].size)
370 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
372 panic("Out of memory...\n");
373 seg->start = memory_chunk[i].addr;
374 seg->size = memory_chunk[i].size;
375 insert_memory_segment(seg);
377 mutex_unlock(&vmem_mutex);
381 core_initcall(vmem_convert_memory_chunk);