1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2006
4 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
7 #include <linux/bootmem.h>
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/hugetlb.h>
13 #include <linux/slab.h>
14 #include <linux/memblock.h>
15 #include <asm/cacheflush.h>
16 #include <asm/pgalloc.h>
17 #include <asm/pgtable.h>
18 #include <asm/setup.h>
19 #include <asm/tlbflush.h>
20 #include <asm/sections.h>
21 #include <asm/set_memory.h>
23 static DEFINE_MUTEX(vmem_mutex
);
25 struct memory_segment
{
26 struct list_head list
;
31 static LIST_HEAD(mem_segs
);
33 static void __ref
*vmem_alloc_pages(unsigned int order
)
35 unsigned long size
= PAGE_SIZE
<< order
;
37 if (slab_is_available())
38 return (void *)__get_free_pages(GFP_KERNEL
, order
);
39 return (void *) memblock_alloc(size
, size
);
42 void *vmem_crst_alloc(unsigned long val
)
46 table
= vmem_alloc_pages(CRST_ALLOC_ORDER
);
48 crst_table_init(table
, val
);
52 pte_t __ref
*vmem_pte_alloc(void)
54 unsigned long size
= PTRS_PER_PTE
* sizeof(pte_t
);
57 if (slab_is_available())
58 pte
= (pte_t
*) page_table_alloc(&init_mm
);
60 pte
= (pte_t
*) memblock_alloc(size
, size
);
63 memset64((u64
*)pte
, _PAGE_INVALID
, PTRS_PER_PTE
);
68 * Add a physical memory range to the 1:1 mapping.
70 static int vmem_add_mem(unsigned long start
, unsigned long size
)
72 unsigned long pgt_prot
, sgt_prot
, r3_prot
;
73 unsigned long pages4k
, pages1m
, pages2g
;
74 unsigned long end
= start
+ size
;
75 unsigned long address
= start
;
83 pgt_prot
= pgprot_val(PAGE_KERNEL
);
84 sgt_prot
= pgprot_val(SEGMENT_KERNEL
);
85 r3_prot
= pgprot_val(REGION3_KERNEL
);
86 if (!MACHINE_HAS_NX
) {
87 pgt_prot
&= ~_PAGE_NOEXEC
;
88 sgt_prot
&= ~_SEGMENT_ENTRY_NOEXEC
;
89 r3_prot
&= ~_REGION_ENTRY_NOEXEC
;
91 pages4k
= pages1m
= pages2g
= 0;
92 while (address
< end
) {
93 pg_dir
= pgd_offset_k(address
);
94 if (pgd_none(*pg_dir
)) {
95 p4_dir
= vmem_crst_alloc(_REGION2_ENTRY_EMPTY
);
98 pgd_populate(&init_mm
, pg_dir
, p4_dir
);
100 p4_dir
= p4d_offset(pg_dir
, address
);
101 if (p4d_none(*p4_dir
)) {
102 pu_dir
= vmem_crst_alloc(_REGION3_ENTRY_EMPTY
);
105 p4d_populate(&init_mm
, p4_dir
, pu_dir
);
107 pu_dir
= pud_offset(p4_dir
, address
);
108 if (MACHINE_HAS_EDAT2
&& pud_none(*pu_dir
) && address
&&
109 !(address
& ~PUD_MASK
) && (address
+ PUD_SIZE
<= end
) &&
110 !debug_pagealloc_enabled()) {
111 pud_val(*pu_dir
) = address
| r3_prot
;
116 if (pud_none(*pu_dir
)) {
117 pm_dir
= vmem_crst_alloc(_SEGMENT_ENTRY_EMPTY
);
120 pud_populate(&init_mm
, pu_dir
, pm_dir
);
122 pm_dir
= pmd_offset(pu_dir
, address
);
123 if (MACHINE_HAS_EDAT1
&& pmd_none(*pm_dir
) && address
&&
124 !(address
& ~PMD_MASK
) && (address
+ PMD_SIZE
<= end
) &&
125 !debug_pagealloc_enabled()) {
126 pmd_val(*pm_dir
) = address
| sgt_prot
;
131 if (pmd_none(*pm_dir
)) {
132 pt_dir
= vmem_pte_alloc();
135 pmd_populate(&init_mm
, pm_dir
, pt_dir
);
138 pt_dir
= pte_offset_kernel(pm_dir
, address
);
139 pte_val(*pt_dir
) = address
| pgt_prot
;
140 address
+= PAGE_SIZE
;
145 update_page_count(PG_DIRECT_MAP_4K
, pages4k
);
146 update_page_count(PG_DIRECT_MAP_1M
, pages1m
);
147 update_page_count(PG_DIRECT_MAP_2G
, pages2g
);
152 * Remove a physical memory range from the 1:1 mapping.
153 * Currently only invalidates page table entries.
155 static void vmem_remove_range(unsigned long start
, unsigned long size
)
157 unsigned long pages4k
, pages1m
, pages2g
;
158 unsigned long end
= start
+ size
;
159 unsigned long address
= start
;
166 pages4k
= pages1m
= pages2g
= 0;
167 while (address
< end
) {
168 pg_dir
= pgd_offset_k(address
);
169 if (pgd_none(*pg_dir
)) {
170 address
+= PGDIR_SIZE
;
173 p4_dir
= p4d_offset(pg_dir
, address
);
174 if (p4d_none(*p4_dir
)) {
178 pu_dir
= pud_offset(p4_dir
, address
);
179 if (pud_none(*pu_dir
)) {
183 if (pud_large(*pu_dir
)) {
189 pm_dir
= pmd_offset(pu_dir
, address
);
190 if (pmd_none(*pm_dir
)) {
194 if (pmd_large(*pm_dir
)) {
200 pt_dir
= pte_offset_kernel(pm_dir
, address
);
201 pte_clear(&init_mm
, address
, pt_dir
);
202 address
+= PAGE_SIZE
;
205 flush_tlb_kernel_range(start
, end
);
206 update_page_count(PG_DIRECT_MAP_4K
, -pages4k
);
207 update_page_count(PG_DIRECT_MAP_1M
, -pages1m
);
208 update_page_count(PG_DIRECT_MAP_2G
, -pages2g
);
212 * Add a backed mem_map array to the virtual mem_map array.
214 int __meminit
vmemmap_populate(unsigned long start
, unsigned long end
, int node
,
215 struct vmem_altmap
*altmap
)
217 unsigned long pgt_prot
, sgt_prot
;
218 unsigned long address
= start
;
226 pgt_prot
= pgprot_val(PAGE_KERNEL
);
227 sgt_prot
= pgprot_val(SEGMENT_KERNEL
);
228 if (!MACHINE_HAS_NX
) {
229 pgt_prot
&= ~_PAGE_NOEXEC
;
230 sgt_prot
&= ~_SEGMENT_ENTRY_NOEXEC
;
232 for (address
= start
; address
< end
;) {
233 pg_dir
= pgd_offset_k(address
);
234 if (pgd_none(*pg_dir
)) {
235 p4_dir
= vmem_crst_alloc(_REGION2_ENTRY_EMPTY
);
238 pgd_populate(&init_mm
, pg_dir
, p4_dir
);
241 p4_dir
= p4d_offset(pg_dir
, address
);
242 if (p4d_none(*p4_dir
)) {
243 pu_dir
= vmem_crst_alloc(_REGION3_ENTRY_EMPTY
);
246 p4d_populate(&init_mm
, p4_dir
, pu_dir
);
249 pu_dir
= pud_offset(p4_dir
, address
);
250 if (pud_none(*pu_dir
)) {
251 pm_dir
= vmem_crst_alloc(_SEGMENT_ENTRY_EMPTY
);
254 pud_populate(&init_mm
, pu_dir
, pm_dir
);
257 pm_dir
= pmd_offset(pu_dir
, address
);
258 if (pmd_none(*pm_dir
)) {
259 /* Use 1MB frames for vmemmap if available. We always
260 * use large frames even if they are only partially
262 * Otherwise we would have also page tables since
263 * vmemmap_populate gets called for each section
265 if (MACHINE_HAS_EDAT1
) {
268 new_page
= vmemmap_alloc_block(PMD_SIZE
, node
);
271 pmd_val(*pm_dir
) = __pa(new_page
) | sgt_prot
;
272 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
275 pt_dir
= vmem_pte_alloc();
278 pmd_populate(&init_mm
, pm_dir
, pt_dir
);
279 } else if (pmd_large(*pm_dir
)) {
280 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
284 pt_dir
= pte_offset_kernel(pm_dir
, address
);
285 if (pte_none(*pt_dir
)) {
288 new_page
= vmemmap_alloc_block(PAGE_SIZE
, node
);
291 pte_val(*pt_dir
) = __pa(new_page
) | pgt_prot
;
293 address
+= PAGE_SIZE
;
300 void vmemmap_free(unsigned long start
, unsigned long end
,
301 struct vmem_altmap
*altmap
)
306 * Add memory segment to the segment list if it doesn't overlap with
307 * an already present segment.
309 static int insert_memory_segment(struct memory_segment
*seg
)
311 struct memory_segment
*tmp
;
313 if (seg
->start
+ seg
->size
> VMEM_MAX_PHYS
||
314 seg
->start
+ seg
->size
< seg
->start
)
317 list_for_each_entry(tmp
, &mem_segs
, list
) {
318 if (seg
->start
>= tmp
->start
+ tmp
->size
)
320 if (seg
->start
+ seg
->size
<= tmp
->start
)
324 list_add(&seg
->list
, &mem_segs
);
329 * Remove memory segment from the segment list.
331 static void remove_memory_segment(struct memory_segment
*seg
)
333 list_del(&seg
->list
);
336 static void __remove_shared_memory(struct memory_segment
*seg
)
338 remove_memory_segment(seg
);
339 vmem_remove_range(seg
->start
, seg
->size
);
342 int vmem_remove_mapping(unsigned long start
, unsigned long size
)
344 struct memory_segment
*seg
;
347 mutex_lock(&vmem_mutex
);
350 list_for_each_entry(seg
, &mem_segs
, list
) {
351 if (seg
->start
== start
&& seg
->size
== size
)
355 if (seg
->start
!= start
|| seg
->size
!= size
)
359 __remove_shared_memory(seg
);
362 mutex_unlock(&vmem_mutex
);
366 int vmem_add_mapping(unsigned long start
, unsigned long size
)
368 struct memory_segment
*seg
;
371 mutex_lock(&vmem_mutex
);
373 seg
= kzalloc(sizeof(*seg
), GFP_KERNEL
);
379 ret
= insert_memory_segment(seg
);
383 ret
= vmem_add_mem(start
, size
);
389 __remove_shared_memory(seg
);
393 mutex_unlock(&vmem_mutex
);
398 * map whole physical memory to virtual memory (identity mapping)
399 * we reserve enough space in the vmalloc area for vmemmap to hotplug
400 * additional memory segments.
402 void __init
vmem_map_init(void)
404 struct memblock_region
*reg
;
406 for_each_memblock(memory
, reg
)
407 vmem_add_mem(reg
->base
, reg
->size
);
408 __set_memory((unsigned long)_stext
,
409 (unsigned long)(_etext
- _stext
) >> PAGE_SHIFT
,
410 SET_MEMORY_RO
| SET_MEMORY_X
);
411 __set_memory((unsigned long)_etext
,
412 (unsigned long)(__end_rodata
- _etext
) >> PAGE_SHIFT
,
414 __set_memory((unsigned long)_sinittext
,
415 (unsigned long)(_einittext
- _sinittext
) >> PAGE_SHIFT
,
416 SET_MEMORY_RO
| SET_MEMORY_X
);
417 pr_info("Write protected kernel read-only data: %luk\n",
418 (unsigned long)(__end_rodata
- _stext
) >> 10);
422 * Convert memblock.memory to a memory segment list so there is a single
423 * list that contains all memory segments.
425 static int __init
vmem_convert_memory_chunk(void)
427 struct memblock_region
*reg
;
428 struct memory_segment
*seg
;
430 mutex_lock(&vmem_mutex
);
431 for_each_memblock(memory
, reg
) {
432 seg
= kzalloc(sizeof(*seg
), GFP_KERNEL
);
434 panic("Out of memory...\n");
435 seg
->start
= reg
->base
;
436 seg
->size
= reg
->size
;
437 insert_memory_segment(seg
);
439 mutex_unlock(&vmem_mutex
);
443 core_initcall(vmem_convert_memory_chunk
);