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 #define vmem_pud_alloc() ({ BUG(); ((pud_t *) NULL); })
74 static inline pmd_t *vmem_pmd_alloc(void)
79 pmd = vmem_alloc_pages(2);
82 clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE*4);
87 static inline pte_t *vmem_pte_alloc(void)
89 pte_t *pte = vmem_alloc_pages(0);
93 clear_table((unsigned long *) pte, _PAGE_TYPE_EMPTY, PAGE_SIZE);
98 * Add a physical memory range to the 1:1 mapping.
100 static int vmem_add_range(unsigned long start, unsigned long size)
102 unsigned long address;
110 for (address = start; address < start + size; address += PAGE_SIZE) {
111 pg_dir = pgd_offset_k(address);
112 if (pgd_none(*pg_dir)) {
113 pu_dir = vmem_pud_alloc();
116 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
119 pu_dir = pud_offset(pg_dir, address);
120 if (pud_none(*pu_dir)) {
121 pm_dir = vmem_pmd_alloc();
124 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
127 pm_dir = pmd_offset(pu_dir, address);
128 if (pmd_none(*pm_dir)) {
129 pt_dir = vmem_pte_alloc();
132 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
135 pt_dir = pte_offset_kernel(pm_dir, address);
136 pte = pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL);
141 flush_tlb_kernel_range(start, start + size);
146 * Remove a physical memory range from the 1:1 mapping.
147 * Currently only invalidates page table entries.
149 static void vmem_remove_range(unsigned long start, unsigned long size)
151 unsigned long address;
158 pte_val(pte) = _PAGE_TYPE_EMPTY;
159 for (address = start; address < start + size; address += PAGE_SIZE) {
160 pg_dir = pgd_offset_k(address);
161 pu_dir = pud_offset(pg_dir, address);
162 if (pud_none(*pu_dir))
164 pm_dir = pmd_offset(pu_dir, address);
165 if (pmd_none(*pm_dir))
167 pt_dir = pte_offset_kernel(pm_dir, address);
170 flush_tlb_kernel_range(start, start + size);
174 * Add a backed mem_map array to the virtual mem_map array.
176 static int vmem_add_mem_map(unsigned long start, unsigned long size)
178 unsigned long address, start_addr, end_addr;
179 struct page *map_start, *map_end;
187 map_start = VMEM_MAP + PFN_DOWN(start);
188 map_end = VMEM_MAP + PFN_DOWN(start + size);
190 start_addr = (unsigned long) map_start & PAGE_MASK;
191 end_addr = PFN_ALIGN((unsigned long) map_end);
193 for (address = start_addr; address < end_addr; address += PAGE_SIZE) {
194 pg_dir = pgd_offset_k(address);
195 if (pgd_none(*pg_dir)) {
196 pu_dir = vmem_pud_alloc();
199 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
202 pu_dir = pud_offset(pg_dir, address);
203 if (pud_none(*pu_dir)) {
204 pm_dir = vmem_pmd_alloc();
207 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
210 pm_dir = pmd_offset(pu_dir, address);
211 if (pmd_none(*pm_dir)) {
212 pt_dir = vmem_pte_alloc();
215 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
218 pt_dir = pte_offset_kernel(pm_dir, address);
219 if (pte_none(*pt_dir)) {
220 unsigned long new_page;
222 new_page =__pa(vmem_alloc_pages(0));
225 pte = pfn_pte(new_page >> PAGE_SHIFT, PAGE_KERNEL);
231 flush_tlb_kernel_range(start_addr, end_addr);
235 static int vmem_add_mem(unsigned long start, unsigned long size)
239 ret = vmem_add_mem_map(start, size);
242 return vmem_add_range(start, size);
246 * Add memory segment to the segment list if it doesn't overlap with
247 * an already present segment.
249 static int insert_memory_segment(struct memory_segment *seg)
251 struct memory_segment *tmp;
253 if (seg->start + seg->size >= VMALLOC_START ||
254 seg->start + seg->size < seg->start)
257 list_for_each_entry(tmp, &mem_segs, list) {
258 if (seg->start >= tmp->start + tmp->size)
260 if (seg->start + seg->size <= tmp->start)
264 list_add(&seg->list, &mem_segs);
269 * Remove memory segment from the segment list.
271 static void remove_memory_segment(struct memory_segment *seg)
273 list_del(&seg->list);
276 static void __remove_shared_memory(struct memory_segment *seg)
278 remove_memory_segment(seg);
279 vmem_remove_range(seg->start, seg->size);
282 int remove_shared_memory(unsigned long start, unsigned long size)
284 struct memory_segment *seg;
287 mutex_lock(&vmem_mutex);
290 list_for_each_entry(seg, &mem_segs, list) {
291 if (seg->start == start && seg->size == size)
295 if (seg->start != start || seg->size != size)
299 __remove_shared_memory(seg);
302 mutex_unlock(&vmem_mutex);
306 int add_shared_memory(unsigned long start, unsigned long size)
308 struct memory_segment *seg;
310 unsigned long pfn, num_pfn, end_pfn;
313 mutex_lock(&vmem_mutex);
315 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
321 ret = insert_memory_segment(seg);
325 ret = vmem_add_mem(start, size);
329 pfn = PFN_DOWN(start);
330 num_pfn = PFN_DOWN(size);
331 end_pfn = pfn + num_pfn;
333 page = pfn_to_page(pfn);
334 memset(page, 0, num_pfn * sizeof(struct page));
336 for (; pfn < end_pfn; pfn++) {
337 page = pfn_to_page(pfn);
338 init_page_count(page);
339 reset_page_mapcount(page);
340 SetPageReserved(page);
341 INIT_LIST_HEAD(&page->lru);
346 __remove_shared_memory(seg);
350 mutex_unlock(&vmem_mutex);
355 * map whole physical memory to virtual memory (identity mapping)
356 * we reserve enough space in the vmalloc area for vmemmap to hotplug
357 * additional memory segments.
359 void __init vmem_map_init(void)
363 BUILD_BUG_ON((unsigned long)VMEM_MAP + VMEM_MAP_SIZE > VMEM_MAP_MAX);
364 NODE_DATA(0)->node_mem_map = VMEM_MAP;
365 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++)
366 vmem_add_mem(memory_chunk[i].addr, memory_chunk[i].size);
370 * Convert memory chunk array to a memory segment list so there is a single
371 * list that contains both r/w memory and shared memory segments.
373 static int __init vmem_convert_memory_chunk(void)
375 struct memory_segment *seg;
378 mutex_lock(&vmem_mutex);
379 for (i = 0; i < MEMORY_CHUNKS; i++) {
380 if (!memory_chunk[i].size)
382 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
384 panic("Out of memory...\n");
385 seg->start = memory_chunk[i].addr;
386 seg->size = memory_chunk[i].size;
387 insert_memory_segment(seg);
389 mutex_unlock(&vmem_mutex);
393 core_initcall(vmem_convert_memory_chunk);