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 <linux/hugetlb.h>
14 #include <asm/pgalloc.h>
15 #include <asm/pgtable.h>
16 #include <asm/setup.h>
17 #include <asm/tlbflush.h>
18 #include <asm/sections.h>
20 static DEFINE_MUTEX(vmem_mutex
);
22 struct memory_segment
{
23 struct list_head list
;
28 static LIST_HEAD(mem_segs
);
30 static void __ref
*vmem_alloc_pages(unsigned int order
)
32 if (slab_is_available())
33 return (void *)__get_free_pages(GFP_KERNEL
, order
);
34 return alloc_bootmem_pages((1 << order
) * PAGE_SIZE
);
37 static inline pud_t
*vmem_pud_alloc(void)
42 pud
= vmem_alloc_pages(2);
45 clear_table((unsigned long *) pud
, _REGION3_ENTRY_EMPTY
, PAGE_SIZE
* 4);
50 static inline pmd_t
*vmem_pmd_alloc(void)
55 pmd
= vmem_alloc_pages(2);
58 clear_table((unsigned long *) pmd
, _SEGMENT_ENTRY_EMPTY
, PAGE_SIZE
* 4);
63 static pte_t __init_refok
*vmem_pte_alloc(void)
67 if (slab_is_available())
68 pte
= (pte_t
*) page_table_alloc(&init_mm
);
70 pte
= alloc_bootmem(PTRS_PER_PTE
* sizeof(pte_t
));
73 clear_table((unsigned long *) pte
, _PAGE_TYPE_EMPTY
,
74 PTRS_PER_PTE
* sizeof(pte_t
));
79 * Add a physical memory range to the 1:1 mapping.
81 static int vmem_add_mem(unsigned long start
, unsigned long size
, int ro
)
83 unsigned long address
;
91 for (address
= start
; address
< start
+ size
; address
+= PAGE_SIZE
) {
92 pg_dir
= pgd_offset_k(address
);
93 if (pgd_none(*pg_dir
)) {
94 pu_dir
= vmem_pud_alloc();
97 pgd_populate_kernel(&init_mm
, pg_dir
, pu_dir
);
100 pu_dir
= pud_offset(pg_dir
, address
);
101 if (pud_none(*pu_dir
)) {
102 pm_dir
= vmem_pmd_alloc();
105 pud_populate_kernel(&init_mm
, pu_dir
, pm_dir
);
108 pte
= mk_pte_phys(address
, __pgprot(ro
? _PAGE_RO
: 0));
109 pm_dir
= pmd_offset(pu_dir
, address
);
112 if (MACHINE_HAS_HPAGE
&& !(address
& ~HPAGE_MASK
) &&
113 (address
+ HPAGE_SIZE
<= start
+ size
) &&
114 (address
>= HPAGE_SIZE
)) {
115 pte_val(pte
) |= _SEGMENT_ENTRY_LARGE
;
116 pmd_val(*pm_dir
) = pte_val(pte
);
117 address
+= HPAGE_SIZE
- PAGE_SIZE
;
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
);
133 flush_tlb_kernel_range(start
, start
+ size
);
138 * Remove a physical memory range from the 1:1 mapping.
139 * Currently only invalidates page table entries.
141 static void vmem_remove_range(unsigned long start
, unsigned long size
)
143 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 pu_dir
= pud_offset(pg_dir
, address
);
154 if (pud_none(*pu_dir
))
156 pm_dir
= pmd_offset(pu_dir
, address
);
157 if (pmd_none(*pm_dir
))
160 if (pmd_huge(*pm_dir
)) {
161 pmd_clear_kernel(pm_dir
);
162 address
+= HPAGE_SIZE
- PAGE_SIZE
;
166 pt_dir
= pte_offset_kernel(pm_dir
, address
);
169 flush_tlb_kernel_range(start
, start
+ size
);
173 * Add a backed mem_map array to the virtual mem_map array.
175 int __meminit
vmemmap_populate(struct page
*start
, unsigned long nr
, int node
)
177 unsigned long address
, start_addr
, end_addr
;
185 start_addr
= (unsigned long) start
;
186 end_addr
= (unsigned long) (start
+ nr
);
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 pu_dir
= vmem_pud_alloc();
194 pgd_populate_kernel(&init_mm
, pg_dir
, pu_dir
);
197 pu_dir
= pud_offset(pg_dir
, address
);
198 if (pud_none(*pu_dir
)) {
199 pm_dir
= vmem_pmd_alloc();
202 pud_populate_kernel(&init_mm
, pu_dir
, pm_dir
);
205 pm_dir
= pmd_offset(pu_dir
, address
);
206 if (pmd_none(*pm_dir
)) {
207 pt_dir
= vmem_pte_alloc();
210 pmd_populate_kernel(&init_mm
, pm_dir
, pt_dir
);
213 pt_dir
= pte_offset_kernel(pm_dir
, address
);
214 if (pte_none(*pt_dir
)) {
215 unsigned long new_page
;
217 new_page
=__pa(vmem_alloc_pages(0));
220 pte
= pfn_pte(new_page
>> PAGE_SHIFT
, PAGE_KERNEL
);
226 flush_tlb_kernel_range(start_addr
, end_addr
);
231 * Add memory segment to the segment list if it doesn't overlap with
232 * an already present segment.
234 static int insert_memory_segment(struct memory_segment
*seg
)
236 struct memory_segment
*tmp
;
238 if (seg
->start
+ seg
->size
>= VMEM_MAX_PHYS
||
239 seg
->start
+ seg
->size
< seg
->start
)
242 list_for_each_entry(tmp
, &mem_segs
, list
) {
243 if (seg
->start
>= tmp
->start
+ tmp
->size
)
245 if (seg
->start
+ seg
->size
<= tmp
->start
)
249 list_add(&seg
->list
, &mem_segs
);
254 * Remove memory segment from the segment list.
256 static void remove_memory_segment(struct memory_segment
*seg
)
258 list_del(&seg
->list
);
261 static void __remove_shared_memory(struct memory_segment
*seg
)
263 remove_memory_segment(seg
);
264 vmem_remove_range(seg
->start
, seg
->size
);
267 int vmem_remove_mapping(unsigned long start
, unsigned long size
)
269 struct memory_segment
*seg
;
272 mutex_lock(&vmem_mutex
);
275 list_for_each_entry(seg
, &mem_segs
, list
) {
276 if (seg
->start
== start
&& seg
->size
== size
)
280 if (seg
->start
!= start
|| seg
->size
!= size
)
284 __remove_shared_memory(seg
);
287 mutex_unlock(&vmem_mutex
);
291 int vmem_add_mapping(unsigned long start
, unsigned long size
)
293 struct memory_segment
*seg
;
296 mutex_lock(&vmem_mutex
);
298 seg
= kzalloc(sizeof(*seg
), GFP_KERNEL
);
304 ret
= insert_memory_segment(seg
);
308 ret
= vmem_add_mem(start
, size
, 0);
314 __remove_shared_memory(seg
);
318 mutex_unlock(&vmem_mutex
);
323 * map whole physical memory to virtual memory (identity mapping)
324 * we reserve enough space in the vmalloc area for vmemmap to hotplug
325 * additional memory segments.
327 void __init
vmem_map_init(void)
329 unsigned long ro_start
, ro_end
;
330 unsigned long start
, end
;
333 INIT_LIST_HEAD(&init_mm
.context
.crst_list
);
334 INIT_LIST_HEAD(&init_mm
.context
.pgtable_list
);
335 init_mm
.context
.noexec
= 0;
336 ro_start
= ((unsigned long)&_stext
) & PAGE_MASK
;
337 ro_end
= PFN_ALIGN((unsigned long)&_eshared
);
338 for (i
= 0; i
< MEMORY_CHUNKS
&& memory_chunk
[i
].size
> 0; i
++) {
339 start
= memory_chunk
[i
].addr
;
340 end
= memory_chunk
[i
].addr
+ memory_chunk
[i
].size
;
341 if (start
>= ro_end
|| end
<= ro_start
)
342 vmem_add_mem(start
, end
- start
, 0);
343 else if (start
>= ro_start
&& end
<= ro_end
)
344 vmem_add_mem(start
, end
- start
, 1);
345 else if (start
>= ro_start
) {
346 vmem_add_mem(start
, ro_end
- start
, 1);
347 vmem_add_mem(ro_end
, end
- ro_end
, 0);
348 } else if (end
< ro_end
) {
349 vmem_add_mem(start
, ro_start
- start
, 0);
350 vmem_add_mem(ro_start
, end
- ro_start
, 1);
352 vmem_add_mem(start
, ro_start
- start
, 0);
353 vmem_add_mem(ro_start
, ro_end
- ro_start
, 1);
354 vmem_add_mem(ro_end
, end
- ro_end
, 0);
360 * Convert memory chunk array to a memory segment list so there is a single
361 * list that contains both r/w memory and shared memory segments.
363 static int __init
vmem_convert_memory_chunk(void)
365 struct memory_segment
*seg
;
368 mutex_lock(&vmem_mutex
);
369 for (i
= 0; i
< MEMORY_CHUNKS
; i
++) {
370 if (!memory_chunk
[i
].size
)
372 seg
= kzalloc(sizeof(*seg
), GFP_KERNEL
);
374 panic("Out of memory...\n");
375 seg
->start
= memory_chunk
[i
].addr
;
376 seg
->size
= memory_chunk
[i
].size
;
377 insert_memory_segment(seg
);
379 mutex_unlock(&vmem_mutex
);
383 core_initcall(vmem_convert_memory_chunk
);