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 static inline pmd_t *vmem_pmd_alloc(void)
81 pmd = vmem_alloc_pages(2);
84 clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE*4);
89 static inline pte_t *vmem_pte_alloc(void)
91 pte_t *pte = vmem_alloc_pages(0);
95 clear_table((unsigned long *) pte, _PAGE_TYPE_EMPTY, PAGE_SIZE);
100 * Add a physical memory range to the 1:1 mapping.
102 static int vmem_add_range(unsigned long start, unsigned long size)
104 unsigned long address;
111 for (address = start; address < start + size; address += PAGE_SIZE) {
112 pg_dir = pgd_offset_k(address);
113 if (pgd_none(*pg_dir)) {
114 pm_dir = vmem_pmd_alloc();
117 pgd_populate_kernel(&init_mm, pg_dir, pm_dir);
120 pm_dir = pmd_offset(pg_dir, address);
121 if (pmd_none(*pm_dir)) {
122 pt_dir = vmem_pte_alloc();
125 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
128 pt_dir = pte_offset_kernel(pm_dir, address);
129 pte = pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL);
134 flush_tlb_kernel_range(start, start + size);
139 * Remove a physical memory range from the 1:1 mapping.
140 * Currently only invalidates page table entries.
142 static void vmem_remove_range(unsigned long start, unsigned long size)
144 unsigned long address;
150 pte_val(pte) = _PAGE_TYPE_EMPTY;
151 for (address = start; address < start + size; address += PAGE_SIZE) {
152 pg_dir = pgd_offset_k(address);
153 if (pgd_none(*pg_dir))
155 pm_dir = pmd_offset(pg_dir, address);
156 if (pmd_none(*pm_dir))
158 pt_dir = pte_offset_kernel(pm_dir, address);
161 flush_tlb_kernel_range(start, start + size);
165 * Add a backed mem_map array to the virtual mem_map array.
167 static int vmem_add_mem_map(unsigned long start, unsigned long size)
169 unsigned long address, start_addr, end_addr;
170 struct page *map_start, *map_end;
177 map_start = vmem_map + PFN_DOWN(start);
178 map_end = vmem_map + PFN_DOWN(start + size);
180 start_addr = (unsigned long) map_start & PAGE_MASK;
181 end_addr = PFN_ALIGN((unsigned long) map_end);
183 for (address = start_addr; address < end_addr; address += PAGE_SIZE) {
184 pg_dir = pgd_offset_k(address);
185 if (pgd_none(*pg_dir)) {
186 pm_dir = vmem_pmd_alloc();
189 pgd_populate_kernel(&init_mm, pg_dir, pm_dir);
192 pm_dir = pmd_offset(pg_dir, address);
193 if (pmd_none(*pm_dir)) {
194 pt_dir = vmem_pte_alloc();
197 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
200 pt_dir = pte_offset_kernel(pm_dir, address);
201 if (pte_none(*pt_dir)) {
202 unsigned long new_page;
204 new_page =__pa(vmem_alloc_pages(0));
207 pte = pfn_pte(new_page >> PAGE_SHIFT, PAGE_KERNEL);
213 flush_tlb_kernel_range(start_addr, end_addr);
217 static int vmem_add_mem(unsigned long start, unsigned long size)
221 ret = vmem_add_range(start, size);
224 return vmem_add_mem_map(start, size);
228 * Add memory segment to the segment list if it doesn't overlap with
229 * an already present segment.
231 static int insert_memory_segment(struct memory_segment *seg)
233 struct memory_segment *tmp;
235 if (PFN_DOWN(seg->start + seg->size) > max_pfn ||
236 seg->start + seg->size < seg->start)
239 list_for_each_entry(tmp, &mem_segs, list) {
240 if (seg->start >= tmp->start + tmp->size)
242 if (seg->start + seg->size <= tmp->start)
246 list_add(&seg->list, &mem_segs);
251 * Remove memory segment from the segment list.
253 static void remove_memory_segment(struct memory_segment *seg)
255 list_del(&seg->list);
258 static void __remove_shared_memory(struct memory_segment *seg)
260 remove_memory_segment(seg);
261 vmem_remove_range(seg->start, seg->size);
264 int remove_shared_memory(unsigned long start, unsigned long size)
266 struct memory_segment *seg;
269 mutex_lock(&vmem_mutex);
272 list_for_each_entry(seg, &mem_segs, list) {
273 if (seg->start == start && seg->size == size)
277 if (seg->start != start || seg->size != size)
281 __remove_shared_memory(seg);
284 mutex_unlock(&vmem_mutex);
288 int add_shared_memory(unsigned long start, unsigned long size)
290 struct memory_segment *seg;
292 unsigned long pfn, num_pfn, end_pfn;
295 mutex_lock(&vmem_mutex);
297 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
303 ret = insert_memory_segment(seg);
307 ret = vmem_add_mem(start, size);
311 pfn = PFN_DOWN(start);
312 num_pfn = PFN_DOWN(size);
313 end_pfn = pfn + num_pfn;
315 page = pfn_to_page(pfn);
316 memset(page, 0, num_pfn * sizeof(struct page));
318 for (; pfn < end_pfn; pfn++) {
319 page = pfn_to_page(pfn);
320 init_page_count(page);
321 reset_page_mapcount(page);
322 SetPageReserved(page);
323 INIT_LIST_HEAD(&page->lru);
328 __remove_shared_memory(seg);
332 mutex_unlock(&vmem_mutex);
337 * map whole physical memory to virtual memory (identity mapping)
339 void __init vmem_map_init(void)
341 unsigned long map_size;
344 map_size = ALIGN(max_low_pfn, MAX_ORDER_NR_PAGES) * sizeof(struct page);
345 vmalloc_end = PFN_ALIGN(VMALLOC_END_INIT) - PFN_ALIGN(map_size);
346 vmem_map = (struct page *) vmalloc_end;
347 NODE_DATA(0)->node_mem_map = vmem_map;
349 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++)
350 vmem_add_mem(memory_chunk[i].addr, memory_chunk[i].size);
354 * Convert memory chunk array to a memory segment list so there is a single
355 * list that contains both r/w memory and shared memory segments.
357 static int __init vmem_convert_memory_chunk(void)
359 struct memory_segment *seg;
362 mutex_lock(&vmem_mutex);
363 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
364 if (!memory_chunk[i].size)
366 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
368 panic("Out of memory...\n");
369 seg->start = memory_chunk[i].addr;
370 seg->size = memory_chunk[i].size;
371 insert_memory_segment(seg);
373 mutex_unlock(&vmem_mutex);
377 core_initcall(vmem_convert_memory_chunk);