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 <linux/slab.h>
15 #include <asm/pgalloc.h>
16 #include <asm/pgtable.h>
17 #include <asm/setup.h>
18 #include <asm/tlbflush.h>
19 #include <asm/sections.h>
21 static DEFINE_MUTEX(vmem_mutex
);
23 struct memory_segment
{
24 struct list_head list
;
29 static LIST_HEAD(mem_segs
);
31 static void __ref
*vmem_alloc_pages(unsigned int order
)
33 if (slab_is_available())
34 return (void *)__get_free_pages(GFP_KERNEL
, order
);
35 return alloc_bootmem_pages((1 << order
) * PAGE_SIZE
);
38 static inline pud_t
*vmem_pud_alloc(void)
43 pud
= vmem_alloc_pages(2);
46 clear_table((unsigned long *) pud
, _REGION3_ENTRY_EMPTY
, PAGE_SIZE
* 4);
51 static inline pmd_t
*vmem_pmd_alloc(void)
56 pmd
= vmem_alloc_pages(2);
59 clear_table((unsigned long *) pmd
, _SEGMENT_ENTRY_EMPTY
, PAGE_SIZE
* 4);
64 static pte_t __ref
*vmem_pte_alloc(unsigned long address
)
68 if (slab_is_available())
69 pte
= (pte_t
*) page_table_alloc(&init_mm
, address
);
71 pte
= alloc_bootmem(PTRS_PER_PTE
* sizeof(pte_t
));
74 clear_table((unsigned long *) pte
, _PAGE_TYPE_EMPTY
,
75 PTRS_PER_PTE
* sizeof(pte_t
));
80 * Add a physical memory range to the 1:1 mapping.
82 static int vmem_add_mem(unsigned long start
, unsigned long size
, int ro
)
84 unsigned long address
;
92 for (address
= start
; address
< start
+ size
; address
+= PAGE_SIZE
) {
93 pg_dir
= pgd_offset_k(address
);
94 if (pgd_none(*pg_dir
)) {
95 pu_dir
= vmem_pud_alloc();
98 pgd_populate(&init_mm
, pg_dir
, pu_dir
);
101 pu_dir
= pud_offset(pg_dir
, address
);
102 if (pud_none(*pu_dir
)) {
103 pm_dir
= vmem_pmd_alloc();
106 pud_populate(&init_mm
, pu_dir
, pm_dir
);
109 pte
= mk_pte_phys(address
, __pgprot(ro
? _PAGE_RO
: 0));
110 pm_dir
= pmd_offset(pu_dir
, address
);
113 if (MACHINE_HAS_HPAGE
&& !(address
& ~HPAGE_MASK
) &&
114 (address
+ HPAGE_SIZE
<= start
+ size
) &&
115 (address
>= HPAGE_SIZE
)) {
116 pte_val(pte
) |= _SEGMENT_ENTRY_LARGE
;
117 pmd_val(*pm_dir
) = pte_val(pte
);
118 address
+= HPAGE_SIZE
- PAGE_SIZE
;
122 if (pmd_none(*pm_dir
)) {
123 pt_dir
= vmem_pte_alloc(address
);
126 pmd_populate(&init_mm
, pm_dir
, pt_dir
);
129 pt_dir
= pte_offset_kernel(pm_dir
, address
);
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
;
151 pte_val(pte
) = _PAGE_TYPE_EMPTY
;
152 for (address
= start
; address
< start
+ size
; address
+= PAGE_SIZE
) {
153 pg_dir
= pgd_offset_k(address
);
154 pu_dir
= pud_offset(pg_dir
, address
);
155 if (pud_none(*pu_dir
))
157 pm_dir
= pmd_offset(pu_dir
, address
);
158 if (pmd_none(*pm_dir
))
161 if (pmd_huge(*pm_dir
)) {
163 address
+= HPAGE_SIZE
- PAGE_SIZE
;
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 int __meminit
vmemmap_populate(struct page
*start
, unsigned long nr
, int node
)
178 unsigned long address
, start_addr
, end_addr
;
186 start_addr
= (unsigned long) start
;
187 end_addr
= (unsigned long) (start
+ nr
);
189 for (address
= start_addr
; address
< end_addr
; address
+= PAGE_SIZE
) {
190 pg_dir
= pgd_offset_k(address
);
191 if (pgd_none(*pg_dir
)) {
192 pu_dir
= vmem_pud_alloc();
195 pgd_populate(&init_mm
, pg_dir
, pu_dir
);
198 pu_dir
= pud_offset(pg_dir
, address
);
199 if (pud_none(*pu_dir
)) {
200 pm_dir
= vmem_pmd_alloc();
203 pud_populate(&init_mm
, pu_dir
, pm_dir
);
206 pm_dir
= pmd_offset(pu_dir
, address
);
207 if (pmd_none(*pm_dir
)) {
208 pt_dir
= vmem_pte_alloc(address
);
211 pmd_populate(&init_mm
, pm_dir
, pt_dir
);
214 pt_dir
= pte_offset_kernel(pm_dir
, address
);
215 if (pte_none(*pt_dir
)) {
216 unsigned long new_page
;
218 new_page
=__pa(vmem_alloc_pages(0));
221 pte
= pfn_pte(new_page
>> PAGE_SHIFT
, PAGE_KERNEL
);
225 memset(start
, 0, nr
* sizeof(struct page
));
228 flush_tlb_kernel_range(start_addr
, end_addr
);
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 (seg
->start
+ seg
->size
> VMEM_MAX_PHYS
||
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 vmem_remove_mapping(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 vmem_add_mapping(unsigned long start
, unsigned long size
)
295 struct memory_segment
*seg
;
298 mutex_lock(&vmem_mutex
);
300 seg
= kzalloc(sizeof(*seg
), GFP_KERNEL
);
306 ret
= insert_memory_segment(seg
);
310 ret
= vmem_add_mem(start
, size
, 0);
316 __remove_shared_memory(seg
);
320 mutex_unlock(&vmem_mutex
);
325 * map whole physical memory to virtual memory (identity mapping)
326 * we reserve enough space in the vmalloc area for vmemmap to hotplug
327 * additional memory segments.
329 void __init
vmem_map_init(void)
331 unsigned long ro_start
, ro_end
;
332 unsigned long start
, end
;
335 ro_start
= ((unsigned long)&_stext
) & PAGE_MASK
;
336 ro_end
= PFN_ALIGN((unsigned long)&_eshared
);
337 for (i
= 0; i
< MEMORY_CHUNKS
&& memory_chunk
[i
].size
> 0; i
++) {
338 start
= memory_chunk
[i
].addr
;
339 end
= memory_chunk
[i
].addr
+ memory_chunk
[i
].size
;
340 if (start
>= ro_end
|| end
<= ro_start
)
341 vmem_add_mem(start
, end
- start
, 0);
342 else if (start
>= ro_start
&& end
<= ro_end
)
343 vmem_add_mem(start
, end
- start
, 1);
344 else if (start
>= ro_start
) {
345 vmem_add_mem(start
, ro_end
- start
, 1);
346 vmem_add_mem(ro_end
, end
- ro_end
, 0);
347 } else if (end
< ro_end
) {
348 vmem_add_mem(start
, ro_start
- start
, 0);
349 vmem_add_mem(ro_start
, end
- ro_start
, 1);
351 vmem_add_mem(start
, ro_start
- start
, 0);
352 vmem_add_mem(ro_start
, ro_end
- ro_start
, 1);
353 vmem_add_mem(ro_end
, end
- ro_end
, 0);
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
; 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
);