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
),
69 static inline void *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(PMD_ALLOC_ORDER
);
84 for (i
= 0; i
< PTRS_PER_PMD
; i
++)
85 pmd_clear_kernel(pmd
+ i
);
89 static inline pte_t
*vmem_pte_alloc(void)
95 pte
= vmem_alloc_pages(PTE_ALLOC_ORDER
);
98 pte_val(empty_pte
) = _PAGE_TYPE_EMPTY
;
99 for (i
= 0; i
< PTRS_PER_PTE
; i
++)
105 * Add a physical memory range to the 1:1 mapping.
107 static int vmem_add_range(unsigned long start
, unsigned long size
)
109 unsigned long address
;
116 for (address
= start
; address
< start
+ size
; address
+= PAGE_SIZE
) {
117 pg_dir
= pgd_offset_k(address
);
118 if (pgd_none(*pg_dir
)) {
119 pm_dir
= vmem_pmd_alloc();
122 pgd_populate_kernel(&init_mm
, pg_dir
, pm_dir
);
125 pm_dir
= pmd_offset(pg_dir
, address
);
126 if (pmd_none(*pm_dir
)) {
127 pt_dir
= vmem_pte_alloc();
130 pmd_populate_kernel(&init_mm
, pm_dir
, pt_dir
);
133 pt_dir
= pte_offset_kernel(pm_dir
, address
);
134 pte
= pfn_pte(address
>> PAGE_SHIFT
, PAGE_KERNEL
);
139 flush_tlb_kernel_range(start
, start
+ size
);
144 * Remove a physical memory range from the 1:1 mapping.
145 * Currently only invalidates page table entries.
147 static void vmem_remove_range(unsigned long start
, unsigned long size
)
149 unsigned long address
;
155 pte_val(pte
) = _PAGE_TYPE_EMPTY
;
156 for (address
= start
; address
< start
+ size
; address
+= PAGE_SIZE
) {
157 pg_dir
= pgd_offset_k(address
);
158 if (pgd_none(*pg_dir
))
160 pm_dir
= pmd_offset(pg_dir
, address
);
161 if (pmd_none(*pm_dir
))
163 pt_dir
= pte_offset_kernel(pm_dir
, address
);
166 flush_tlb_kernel_range(start
, start
+ size
);
170 * Add a backed mem_map array to the virtual mem_map array.
172 static int vmem_add_mem_map(unsigned long start
, unsigned long size
)
174 unsigned long address
, start_addr
, end_addr
;
175 struct page
*map_start
, *map_end
;
182 map_start
= vmem_map
+ PFN_DOWN(start
);
183 map_end
= vmem_map
+ PFN_DOWN(start
+ size
);
185 start_addr
= (unsigned long) map_start
& PAGE_MASK
;
186 end_addr
= PFN_ALIGN((unsigned long) map_end
);
188 for (address
= start_addr
; address
< end_addr
; address
+= PAGE_SIZE
) {
189 pg_dir
= pgd_offset_k(address
);
190 if (pgd_none(*pg_dir
)) {
191 pm_dir
= vmem_pmd_alloc();
194 pgd_populate_kernel(&init_mm
, pg_dir
, pm_dir
);
197 pm_dir
= pmd_offset(pg_dir
, address
);
198 if (pmd_none(*pm_dir
)) {
199 pt_dir
= vmem_pte_alloc();
202 pmd_populate_kernel(&init_mm
, pm_dir
, pt_dir
);
205 pt_dir
= pte_offset_kernel(pm_dir
, address
);
206 if (pte_none(*pt_dir
)) {
207 unsigned long new_page
;
209 new_page
=__pa(vmem_alloc_pages(0));
212 pte
= pfn_pte(new_page
>> PAGE_SHIFT
, PAGE_KERNEL
);
218 flush_tlb_kernel_range(start_addr
, end_addr
);
222 static int vmem_add_mem(unsigned long start
, unsigned long size
)
226 ret
= vmem_add_range(start
, size
);
229 return vmem_add_mem_map(start
, size
);
233 * Add memory segment to the segment list if it doesn't overlap with
234 * an already present segment.
236 static int insert_memory_segment(struct memory_segment
*seg
)
238 struct memory_segment
*tmp
;
240 if (PFN_DOWN(seg
->start
+ seg
->size
) > max_pfn
||
241 seg
->start
+ seg
->size
< seg
->start
)
244 list_for_each_entry(tmp
, &mem_segs
, list
) {
245 if (seg
->start
>= tmp
->start
+ tmp
->size
)
247 if (seg
->start
+ seg
->size
<= tmp
->start
)
251 list_add(&seg
->list
, &mem_segs
);
256 * Remove memory segment from the segment list.
258 static void remove_memory_segment(struct memory_segment
*seg
)
260 list_del(&seg
->list
);
263 static void __remove_shared_memory(struct memory_segment
*seg
)
265 remove_memory_segment(seg
);
266 vmem_remove_range(seg
->start
, seg
->size
);
269 int remove_shared_memory(unsigned long start
, unsigned long size
)
271 struct memory_segment
*seg
;
274 mutex_lock(&vmem_mutex
);
277 list_for_each_entry(seg
, &mem_segs
, list
) {
278 if (seg
->start
== start
&& seg
->size
== size
)
282 if (seg
->start
!= start
|| seg
->size
!= size
)
286 __remove_shared_memory(seg
);
289 mutex_unlock(&vmem_mutex
);
293 int add_shared_memory(unsigned long start
, unsigned long size
)
295 struct memory_segment
*seg
;
297 unsigned long pfn
, num_pfn
, end_pfn
;
300 mutex_lock(&vmem_mutex
);
302 seg
= kzalloc(sizeof(*seg
), GFP_KERNEL
);
308 ret
= insert_memory_segment(seg
);
312 ret
= vmem_add_mem(start
, size
);
316 pfn
= PFN_DOWN(start
);
317 num_pfn
= PFN_DOWN(size
);
318 end_pfn
= pfn
+ num_pfn
;
320 page
= pfn_to_page(pfn
);
321 memset(page
, 0, num_pfn
* sizeof(struct page
));
323 for (; pfn
< end_pfn
; pfn
++) {
324 page
= pfn_to_page(pfn
);
325 init_page_count(page
);
326 reset_page_mapcount(page
);
327 SetPageReserved(page
);
328 INIT_LIST_HEAD(&page
->lru
);
333 __remove_shared_memory(seg
);
337 mutex_unlock(&vmem_mutex
);
342 * map whole physical memory to virtual memory (identity mapping)
344 void __init
vmem_map_init(void)
346 unsigned long map_size
;
349 map_size
= ALIGN(max_low_pfn
, MAX_ORDER_NR_PAGES
) * sizeof(struct page
);
350 vmalloc_end
= PFN_ALIGN(VMALLOC_END_INIT
) - PFN_ALIGN(map_size
);
351 vmem_map
= (struct page
*) vmalloc_end
;
352 NODE_DATA(0)->node_mem_map
= vmem_map
;
354 for (i
= 0; i
< MEMORY_CHUNKS
&& memory_chunk
[i
].size
> 0; i
++)
355 vmem_add_mem(memory_chunk
[i
].addr
, memory_chunk
[i
].size
);
359 * Convert memory chunk array to a memory segment list so there is a single
360 * list that contains both r/w memory and shared memory segments.
362 static int __init
vmem_convert_memory_chunk(void)
364 struct memory_segment
*seg
;
367 mutex_lock(&vmem_mutex
);
368 for (i
= 0; i
< MEMORY_CHUNKS
&& memory_chunk
[i
].size
> 0; i
++) {
369 if (!memory_chunk
[i
].size
)
371 seg
= kzalloc(sizeof(*seg
), GFP_KERNEL
);
373 panic("Out of memory...\n");
374 seg
->start
= memory_chunk
[i
].addr
;
375 seg
->size
= memory_chunk
[i
].size
;
376 insert_memory_segment(seg
);
378 mutex_unlock(&vmem_mutex
);
382 core_initcall(vmem_convert_memory_chunk
);