Commit | Line | Data |
---|---|---|
c1cc1552 CM |
1 | /* |
2 | * Based on arch/arm/mm/mmu.c | |
3 | * | |
4 | * Copyright (C) 1995-2005 Russell King | |
5 | * Copyright (C) 2012 ARM Ltd. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <linux/export.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/errno.h> | |
23 | #include <linux/init.h> | |
61bd93ce | 24 | #include <linux/libfdt.h> |
c1cc1552 CM |
25 | #include <linux/mman.h> |
26 | #include <linux/nodemask.h> | |
27 | #include <linux/memblock.h> | |
28 | #include <linux/fs.h> | |
2475ff9d | 29 | #include <linux/io.h> |
41089357 | 30 | #include <linux/slab.h> |
da141706 | 31 | #include <linux/stop_machine.h> |
c1cc1552 | 32 | |
21ab99c2 | 33 | #include <asm/barrier.h> |
c1cc1552 | 34 | #include <asm/cputype.h> |
af86e597 | 35 | #include <asm/fixmap.h> |
068a17a5 | 36 | #include <asm/kasan.h> |
b433dce0 | 37 | #include <asm/kernel-pgtable.h> |
c1cc1552 CM |
38 | #include <asm/sections.h> |
39 | #include <asm/setup.h> | |
40 | #include <asm/sizes.h> | |
41 | #include <asm/tlb.h> | |
c79b954b | 42 | #include <asm/memblock.h> |
c1cc1552 CM |
43 | #include <asm/mmu_context.h> |
44 | ||
45 | #include "mm.h" | |
46 | ||
dd006da2 AB |
47 | u64 idmap_t0sz = TCR_T0SZ(VA_BITS); |
48 | ||
c1cc1552 CM |
49 | /* |
50 | * Empty_zero_page is a special page that is used for zero-initialized data | |
51 | * and COW. | |
52 | */ | |
5227cfa7 | 53 | unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; |
c1cc1552 CM |
54 | EXPORT_SYMBOL(empty_zero_page); |
55 | ||
c1cc1552 CM |
56 | pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, |
57 | unsigned long size, pgprot_t vma_prot) | |
58 | { | |
59 | if (!pfn_valid(pfn)) | |
60 | return pgprot_noncached(vma_prot); | |
61 | else if (file->f_flags & O_SYNC) | |
62 | return pgprot_writecombine(vma_prot); | |
63 | return vma_prot; | |
64 | } | |
65 | EXPORT_SYMBOL(phys_mem_access_prot); | |
66 | ||
f4710445 | 67 | static phys_addr_t __init early_pgtable_alloc(void) |
c1cc1552 | 68 | { |
7142392d SP |
69 | phys_addr_t phys; |
70 | void *ptr; | |
71 | ||
21ab99c2 | 72 | phys = memblock_alloc(PAGE_SIZE, PAGE_SIZE); |
7142392d | 73 | BUG_ON(!phys); |
f4710445 MR |
74 | |
75 | /* | |
76 | * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE | |
77 | * slot will be free, so we can (ab)use the FIX_PTE slot to initialise | |
78 | * any level of table. | |
79 | */ | |
80 | ptr = pte_set_fixmap(phys); | |
81 | ||
21ab99c2 MR |
82 | memset(ptr, 0, PAGE_SIZE); |
83 | ||
f4710445 MR |
84 | /* |
85 | * Implicit barriers also ensure the zeroed page is visible to the page | |
86 | * table walker | |
87 | */ | |
88 | pte_clear_fixmap(); | |
89 | ||
90 | return phys; | |
c1cc1552 CM |
91 | } |
92 | ||
da141706 LA |
93 | /* |
94 | * remap a PMD into pages | |
95 | */ | |
96 | static void split_pmd(pmd_t *pmd, pte_t *pte) | |
97 | { | |
98 | unsigned long pfn = pmd_pfn(*pmd); | |
99 | int i = 0; | |
100 | ||
101 | do { | |
102 | /* | |
103 | * Need to have the least restrictive permissions available | |
667c2759 | 104 | * permissions will be fixed up later |
da141706 | 105 | */ |
667c2759 | 106 | set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC)); |
da141706 LA |
107 | pfn++; |
108 | } while (pte++, i++, i < PTRS_PER_PTE); | |
109 | } | |
110 | ||
111 | static void alloc_init_pte(pmd_t *pmd, unsigned long addr, | |
667c2759 | 112 | unsigned long end, unsigned long pfn, |
da141706 | 113 | pgprot_t prot, |
f4710445 | 114 | phys_addr_t (*pgtable_alloc)(void)) |
c1cc1552 CM |
115 | { |
116 | pte_t *pte; | |
117 | ||
a1c76574 | 118 | if (pmd_none(*pmd) || pmd_sect(*pmd)) { |
132233a7 LA |
119 | phys_addr_t pte_phys; |
120 | BUG_ON(!pgtable_alloc); | |
121 | pte_phys = pgtable_alloc(); | |
f4710445 | 122 | pte = pte_set_fixmap(pte_phys); |
da141706 LA |
123 | if (pmd_sect(*pmd)) |
124 | split_pmd(pmd, pte); | |
f4710445 | 125 | __pmd_populate(pmd, pte_phys, PMD_TYPE_TABLE); |
da141706 | 126 | flush_tlb_all(); |
f4710445 | 127 | pte_clear_fixmap(); |
c1cc1552 | 128 | } |
a1c76574 | 129 | BUG_ON(pmd_bad(*pmd)); |
c1cc1552 | 130 | |
f4710445 | 131 | pte = pte_set_fixmap_offset(pmd, addr); |
c1cc1552 | 132 | do { |
667c2759 CM |
133 | set_pte(pte, pfn_pte(pfn, prot)); |
134 | pfn++; | |
135 | } while (pte++, addr += PAGE_SIZE, addr != end); | |
f4710445 MR |
136 | |
137 | pte_clear_fixmap(); | |
c1cc1552 CM |
138 | } |
139 | ||
9a17a213 | 140 | static void split_pud(pud_t *old_pud, pmd_t *pmd) |
da141706 LA |
141 | { |
142 | unsigned long addr = pud_pfn(*old_pud) << PAGE_SHIFT; | |
143 | pgprot_t prot = __pgprot(pud_val(*old_pud) ^ addr); | |
144 | int i = 0; | |
145 | ||
146 | do { | |
1e43ba9c | 147 | set_pmd(pmd, __pmd(addr | pgprot_val(prot))); |
da141706 LA |
148 | addr += PMD_SIZE; |
149 | } while (pmd++, i++, i < PTRS_PER_PMD); | |
150 | } | |
151 | ||
83863f25 LA |
152 | #ifdef CONFIG_DEBUG_PAGEALLOC |
153 | static bool block_mappings_allowed(phys_addr_t (*pgtable_alloc)(void)) | |
154 | { | |
155 | ||
156 | /* | |
157 | * If debug_page_alloc is enabled we must map the linear map | |
158 | * using pages. However, other mappings created by | |
159 | * create_mapping_noalloc must use sections in some cases. Allow | |
160 | * sections to be used in those cases, where no pgtable_alloc | |
161 | * function is provided. | |
162 | */ | |
163 | return !pgtable_alloc || !debug_pagealloc_enabled(); | |
164 | } | |
165 | #else | |
166 | static bool block_mappings_allowed(phys_addr_t (*pgtable_alloc)(void)) | |
167 | { | |
168 | return true; | |
169 | } | |
170 | #endif | |
171 | ||
11509a30 | 172 | static void alloc_init_pmd(pud_t *pud, unsigned long addr, unsigned long end, |
da141706 | 173 | phys_addr_t phys, pgprot_t prot, |
f4710445 | 174 | phys_addr_t (*pgtable_alloc)(void)) |
c1cc1552 CM |
175 | { |
176 | pmd_t *pmd; | |
177 | unsigned long next; | |
178 | ||
179 | /* | |
180 | * Check for initial section mappings in the pgd/pud and remove them. | |
181 | */ | |
a1c76574 | 182 | if (pud_none(*pud) || pud_sect(*pud)) { |
132233a7 LA |
183 | phys_addr_t pmd_phys; |
184 | BUG_ON(!pgtable_alloc); | |
185 | pmd_phys = pgtable_alloc(); | |
f4710445 | 186 | pmd = pmd_set_fixmap(pmd_phys); |
da141706 LA |
187 | if (pud_sect(*pud)) { |
188 | /* | |
189 | * need to have the 1G of mappings continue to be | |
190 | * present | |
191 | */ | |
192 | split_pud(pud, pmd); | |
193 | } | |
f4710445 | 194 | __pud_populate(pud, pmd_phys, PUD_TYPE_TABLE); |
da141706 | 195 | flush_tlb_all(); |
f4710445 | 196 | pmd_clear_fixmap(); |
c1cc1552 | 197 | } |
a1c76574 | 198 | BUG_ON(pud_bad(*pud)); |
c1cc1552 | 199 | |
f4710445 | 200 | pmd = pmd_set_fixmap_offset(pud, addr); |
c1cc1552 CM |
201 | do { |
202 | next = pmd_addr_end(addr, end); | |
203 | /* try section mapping first */ | |
83863f25 LA |
204 | if (((addr | next | phys) & ~SECTION_MASK) == 0 && |
205 | block_mappings_allowed(pgtable_alloc)) { | |
a55f9929 | 206 | pmd_t old_pmd =*pmd; |
8ce837ce AB |
207 | set_pmd(pmd, __pmd(phys | |
208 | pgprot_val(mk_sect_prot(prot)))); | |
a55f9929 CM |
209 | /* |
210 | * Check for previous table entries created during | |
211 | * boot (__create_page_tables) and flush them. | |
212 | */ | |
523d6e9f | 213 | if (!pmd_none(old_pmd)) { |
a55f9929 | 214 | flush_tlb_all(); |
523d6e9f | 215 | if (pmd_table(old_pmd)) { |
316b39db | 216 | phys_addr_t table = pmd_page_paddr(old_pmd); |
41089357 CM |
217 | if (!WARN_ON_ONCE(slab_is_available())) |
218 | memblock_free(table, PAGE_SIZE); | |
523d6e9f | 219 | } |
220 | } | |
a55f9929 | 221 | } else { |
667c2759 | 222 | alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys), |
21ab99c2 | 223 | prot, pgtable_alloc); |
a55f9929 | 224 | } |
c1cc1552 CM |
225 | phys += next - addr; |
226 | } while (pmd++, addr = next, addr != end); | |
f4710445 MR |
227 | |
228 | pmd_clear_fixmap(); | |
c1cc1552 CM |
229 | } |
230 | ||
da141706 LA |
231 | static inline bool use_1G_block(unsigned long addr, unsigned long next, |
232 | unsigned long phys) | |
233 | { | |
234 | if (PAGE_SHIFT != 12) | |
235 | return false; | |
236 | ||
237 | if (((addr | next | phys) & ~PUD_MASK) != 0) | |
238 | return false; | |
239 | ||
240 | return true; | |
241 | } | |
242 | ||
11509a30 | 243 | static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end, |
da141706 | 244 | phys_addr_t phys, pgprot_t prot, |
f4710445 | 245 | phys_addr_t (*pgtable_alloc)(void)) |
c1cc1552 | 246 | { |
c79b954b | 247 | pud_t *pud; |
c1cc1552 CM |
248 | unsigned long next; |
249 | ||
c79b954b | 250 | if (pgd_none(*pgd)) { |
132233a7 LA |
251 | phys_addr_t pud_phys; |
252 | BUG_ON(!pgtable_alloc); | |
253 | pud_phys = pgtable_alloc(); | |
f4710445 | 254 | __pgd_populate(pgd, pud_phys, PUD_TYPE_TABLE); |
c79b954b JL |
255 | } |
256 | BUG_ON(pgd_bad(*pgd)); | |
257 | ||
f4710445 | 258 | pud = pud_set_fixmap_offset(pgd, addr); |
c1cc1552 CM |
259 | do { |
260 | next = pud_addr_end(addr, end); | |
206a2a73 SC |
261 | |
262 | /* | |
263 | * For 4K granule only, attempt to put down a 1GB block | |
264 | */ | |
83863f25 LA |
265 | if (use_1G_block(addr, next, phys) && |
266 | block_mappings_allowed(pgtable_alloc)) { | |
206a2a73 | 267 | pud_t old_pud = *pud; |
8ce837ce AB |
268 | set_pud(pud, __pud(phys | |
269 | pgprot_val(mk_sect_prot(prot)))); | |
206a2a73 SC |
270 | |
271 | /* | |
272 | * If we have an old value for a pud, it will | |
273 | * be pointing to a pmd table that we no longer | |
274 | * need (from swapper_pg_dir). | |
275 | * | |
276 | * Look up the old pmd table and free it. | |
277 | */ | |
278 | if (!pud_none(old_pud)) { | |
206a2a73 | 279 | flush_tlb_all(); |
523d6e9f | 280 | if (pud_table(old_pud)) { |
316b39db | 281 | phys_addr_t table = pud_page_paddr(old_pud); |
41089357 CM |
282 | if (!WARN_ON_ONCE(slab_is_available())) |
283 | memblock_free(table, PAGE_SIZE); | |
523d6e9f | 284 | } |
206a2a73 SC |
285 | } |
286 | } else { | |
11509a30 | 287 | alloc_init_pmd(pud, addr, next, phys, prot, |
21ab99c2 | 288 | pgtable_alloc); |
206a2a73 | 289 | } |
c1cc1552 CM |
290 | phys += next - addr; |
291 | } while (pud++, addr = next, addr != end); | |
f4710445 MR |
292 | |
293 | pud_clear_fixmap(); | |
c1cc1552 CM |
294 | } |
295 | ||
296 | /* | |
297 | * Create the page directory entries and any necessary page tables for the | |
298 | * mapping specified by 'md'. | |
299 | */ | |
11509a30 | 300 | static void init_pgd(pgd_t *pgd, phys_addr_t phys, unsigned long virt, |
da141706 | 301 | phys_addr_t size, pgprot_t prot, |
f4710445 | 302 | phys_addr_t (*pgtable_alloc)(void)) |
c1cc1552 CM |
303 | { |
304 | unsigned long addr, length, end, next; | |
c1cc1552 | 305 | |
cc5d2b3b MR |
306 | /* |
307 | * If the virtual and physical address don't have the same offset | |
308 | * within a page, we cannot map the region as the caller expects. | |
309 | */ | |
310 | if (WARN_ON((phys ^ virt) & ~PAGE_MASK)) | |
311 | return; | |
312 | ||
9c4e08a3 | 313 | phys &= PAGE_MASK; |
c1cc1552 CM |
314 | addr = virt & PAGE_MASK; |
315 | length = PAGE_ALIGN(size + (virt & ~PAGE_MASK)); | |
316 | ||
c1cc1552 CM |
317 | end = addr + length; |
318 | do { | |
319 | next = pgd_addr_end(addr, end); | |
11509a30 | 320 | alloc_init_pud(pgd, addr, next, phys, prot, pgtable_alloc); |
c1cc1552 CM |
321 | phys += next - addr; |
322 | } while (pgd++, addr = next, addr != end); | |
323 | } | |
324 | ||
f4710445 | 325 | static phys_addr_t late_pgtable_alloc(void) |
da141706 | 326 | { |
21ab99c2 | 327 | void *ptr = (void *)__get_free_page(PGALLOC_GFP); |
da141706 | 328 | BUG_ON(!ptr); |
21ab99c2 MR |
329 | |
330 | /* Ensure the zeroed page is visible to the page table walker */ | |
331 | dsb(ishst); | |
f4710445 | 332 | return __pa(ptr); |
da141706 LA |
333 | } |
334 | ||
11509a30 MR |
335 | static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, |
336 | unsigned long virt, phys_addr_t size, | |
337 | pgprot_t prot, | |
338 | phys_addr_t (*alloc)(void)) | |
339 | { | |
340 | init_pgd(pgd_offset_raw(pgdir, virt), phys, virt, size, prot, alloc); | |
341 | } | |
342 | ||
132233a7 LA |
343 | /* |
344 | * This function can only be used to modify existing table entries, | |
345 | * without allocating new levels of table. Note that this permits the | |
346 | * creation of new section or page entries. | |
347 | */ | |
348 | static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt, | |
da141706 | 349 | phys_addr_t size, pgprot_t prot) |
d7ecbddf MS |
350 | { |
351 | if (virt < VMALLOC_START) { | |
352 | pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", | |
353 | &phys, virt); | |
354 | return; | |
355 | } | |
11509a30 | 356 | __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, |
132233a7 | 357 | NULL); |
d7ecbddf MS |
358 | } |
359 | ||
8ce837ce AB |
360 | void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, |
361 | unsigned long virt, phys_addr_t size, | |
362 | pgprot_t prot) | |
363 | { | |
11509a30 MR |
364 | __create_pgd_mapping(mm->pgd, phys, virt, size, prot, |
365 | late_pgtable_alloc); | |
d7ecbddf MS |
366 | } |
367 | ||
da141706 LA |
368 | static void create_mapping_late(phys_addr_t phys, unsigned long virt, |
369 | phys_addr_t size, pgprot_t prot) | |
370 | { | |
371 | if (virt < VMALLOC_START) { | |
372 | pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", | |
373 | &phys, virt); | |
374 | return; | |
375 | } | |
376 | ||
11509a30 MR |
377 | __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, |
378 | late_pgtable_alloc); | |
da141706 LA |
379 | } |
380 | ||
068a17a5 | 381 | static void __init __map_memblock(pgd_t *pgd, phys_addr_t start, phys_addr_t end) |
da141706 | 382 | { |
068a17a5 MR |
383 | |
384 | unsigned long kernel_start = __pa(_stext); | |
385 | unsigned long kernel_end = __pa(_end); | |
386 | ||
da141706 | 387 | /* |
068a17a5 MR |
388 | * The kernel itself is mapped at page granularity. Map all other |
389 | * memory, making sure we don't overwrite the existing kernel mappings. | |
da141706 | 390 | */ |
068a17a5 MR |
391 | |
392 | /* No overlap with the kernel. */ | |
393 | if (end < kernel_start || start >= kernel_end) { | |
394 | __create_pgd_mapping(pgd, start, __phys_to_virt(start), | |
395 | end - start, PAGE_KERNEL, | |
396 | early_pgtable_alloc); | |
397 | return; | |
da141706 LA |
398 | } |
399 | ||
068a17a5 MR |
400 | /* |
401 | * This block overlaps the kernel mapping. Map the portion(s) which | |
402 | * don't overlap. | |
403 | */ | |
404 | if (start < kernel_start) | |
405 | __create_pgd_mapping(pgd, start, | |
406 | __phys_to_virt(start), | |
407 | kernel_start - start, PAGE_KERNEL, | |
408 | early_pgtable_alloc); | |
409 | if (kernel_end < end) | |
410 | __create_pgd_mapping(pgd, kernel_end, | |
411 | __phys_to_virt(kernel_end), | |
412 | end - kernel_end, PAGE_KERNEL, | |
413 | early_pgtable_alloc); | |
da141706 | 414 | } |
da141706 | 415 | |
068a17a5 | 416 | static void __init map_mem(pgd_t *pgd) |
c1cc1552 CM |
417 | { |
418 | struct memblock_region *reg; | |
f6bc87c3 | 419 | |
c1cc1552 CM |
420 | /* map all the memory banks */ |
421 | for_each_memblock(memory, reg) { | |
422 | phys_addr_t start = reg->base; | |
423 | phys_addr_t end = start + reg->size; | |
424 | ||
425 | if (start >= end) | |
426 | break; | |
68709f45 AB |
427 | if (memblock_is_nomap(reg)) |
428 | continue; | |
c1cc1552 | 429 | |
068a17a5 | 430 | __map_memblock(pgd, start, end); |
c1cc1552 CM |
431 | } |
432 | } | |
433 | ||
da141706 LA |
434 | #ifdef CONFIG_DEBUG_RODATA |
435 | void mark_rodata_ro(void) | |
436 | { | |
437 | create_mapping_late(__pa(_stext), (unsigned long)_stext, | |
438 | (unsigned long)_etext - (unsigned long)_stext, | |
0b2aa5b8 | 439 | PAGE_KERNEL_ROX); |
da141706 LA |
440 | |
441 | } | |
442 | #endif | |
443 | ||
444 | void fixup_init(void) | |
445 | { | |
446 | create_mapping_late(__pa(__init_begin), (unsigned long)__init_begin, | |
447 | (unsigned long)__init_end - (unsigned long)__init_begin, | |
448 | PAGE_KERNEL); | |
449 | } | |
450 | ||
068a17a5 MR |
451 | static void __init map_kernel_chunk(pgd_t *pgd, void *va_start, void *va_end, |
452 | pgprot_t prot) | |
453 | { | |
454 | phys_addr_t pa_start = __pa(va_start); | |
455 | unsigned long size = va_end - va_start; | |
456 | ||
457 | BUG_ON(!PAGE_ALIGNED(pa_start)); | |
458 | BUG_ON(!PAGE_ALIGNED(size)); | |
459 | ||
460 | __create_pgd_mapping(pgd, pa_start, (unsigned long)va_start, size, prot, | |
461 | early_pgtable_alloc); | |
462 | } | |
463 | ||
464 | /* | |
465 | * Create fine-grained mappings for the kernel. | |
466 | */ | |
467 | static void __init map_kernel(pgd_t *pgd) | |
468 | { | |
469 | ||
470 | map_kernel_chunk(pgd, _stext, _etext, PAGE_KERNEL_EXEC); | |
471 | map_kernel_chunk(pgd, __init_begin, __init_end, PAGE_KERNEL_EXEC); | |
472 | map_kernel_chunk(pgd, _data, _end, PAGE_KERNEL); | |
473 | ||
474 | /* | |
475 | * The fixmap falls in a separate pgd to the kernel, and doesn't live | |
476 | * in the carveout for the swapper_pg_dir. We can simply re-use the | |
477 | * existing dir for the fixmap. | |
478 | */ | |
479 | set_pgd(pgd_offset_raw(pgd, FIXADDR_START), *pgd_offset_k(FIXADDR_START)); | |
480 | ||
481 | kasan_copy_shadow(pgd); | |
482 | } | |
483 | ||
c1cc1552 CM |
484 | /* |
485 | * paging_init() sets up the page tables, initialises the zone memory | |
486 | * maps and sets up the zero page. | |
487 | */ | |
488 | void __init paging_init(void) | |
489 | { | |
068a17a5 MR |
490 | phys_addr_t pgd_phys = early_pgtable_alloc(); |
491 | pgd_t *pgd = pgd_set_fixmap(pgd_phys); | |
492 | ||
493 | map_kernel(pgd); | |
494 | map_mem(pgd); | |
495 | ||
496 | /* | |
497 | * We want to reuse the original swapper_pg_dir so we don't have to | |
498 | * communicate the new address to non-coherent secondaries in | |
499 | * secondary_entry, and so cpu_switch_mm can generate the address with | |
500 | * adrp+add rather than a load from some global variable. | |
501 | * | |
502 | * To do this we need to go via a temporary pgd. | |
503 | */ | |
504 | cpu_replace_ttbr1(__va(pgd_phys)); | |
505 | memcpy(swapper_pg_dir, pgd, PAGE_SIZE); | |
506 | cpu_replace_ttbr1(swapper_pg_dir); | |
507 | ||
508 | pgd_clear_fixmap(); | |
509 | memblock_free(pgd_phys, PAGE_SIZE); | |
510 | ||
511 | /* | |
512 | * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd | |
513 | * allocated with it. | |
514 | */ | |
515 | memblock_free(__pa(swapper_pg_dir) + PAGE_SIZE, | |
516 | SWAPPER_DIR_SIZE - PAGE_SIZE); | |
c1cc1552 | 517 | |
c1cc1552 | 518 | bootmem_init(); |
c1cc1552 CM |
519 | } |
520 | ||
c1cc1552 CM |
521 | /* |
522 | * Check whether a kernel address is valid (derived from arch/x86/). | |
523 | */ | |
524 | int kern_addr_valid(unsigned long addr) | |
525 | { | |
526 | pgd_t *pgd; | |
527 | pud_t *pud; | |
528 | pmd_t *pmd; | |
529 | pte_t *pte; | |
530 | ||
531 | if ((((long)addr) >> VA_BITS) != -1UL) | |
532 | return 0; | |
533 | ||
534 | pgd = pgd_offset_k(addr); | |
535 | if (pgd_none(*pgd)) | |
536 | return 0; | |
537 | ||
538 | pud = pud_offset(pgd, addr); | |
539 | if (pud_none(*pud)) | |
540 | return 0; | |
541 | ||
206a2a73 SC |
542 | if (pud_sect(*pud)) |
543 | return pfn_valid(pud_pfn(*pud)); | |
544 | ||
c1cc1552 CM |
545 | pmd = pmd_offset(pud, addr); |
546 | if (pmd_none(*pmd)) | |
547 | return 0; | |
548 | ||
da6e4cb6 DA |
549 | if (pmd_sect(*pmd)) |
550 | return pfn_valid(pmd_pfn(*pmd)); | |
551 | ||
c1cc1552 CM |
552 | pte = pte_offset_kernel(pmd, addr); |
553 | if (pte_none(*pte)) | |
554 | return 0; | |
555 | ||
556 | return pfn_valid(pte_pfn(*pte)); | |
557 | } | |
558 | #ifdef CONFIG_SPARSEMEM_VMEMMAP | |
b433dce0 | 559 | #if !ARM64_SWAPPER_USES_SECTION_MAPS |
0aad818b | 560 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node) |
c1cc1552 | 561 | { |
0aad818b | 562 | return vmemmap_populate_basepages(start, end, node); |
c1cc1552 | 563 | } |
b433dce0 | 564 | #else /* !ARM64_SWAPPER_USES_SECTION_MAPS */ |
0aad818b | 565 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node) |
c1cc1552 | 566 | { |
0aad818b | 567 | unsigned long addr = start; |
c1cc1552 CM |
568 | unsigned long next; |
569 | pgd_t *pgd; | |
570 | pud_t *pud; | |
571 | pmd_t *pmd; | |
572 | ||
573 | do { | |
574 | next = pmd_addr_end(addr, end); | |
575 | ||
576 | pgd = vmemmap_pgd_populate(addr, node); | |
577 | if (!pgd) | |
578 | return -ENOMEM; | |
579 | ||
580 | pud = vmemmap_pud_populate(pgd, addr, node); | |
581 | if (!pud) | |
582 | return -ENOMEM; | |
583 | ||
584 | pmd = pmd_offset(pud, addr); | |
585 | if (pmd_none(*pmd)) { | |
586 | void *p = NULL; | |
587 | ||
588 | p = vmemmap_alloc_block_buf(PMD_SIZE, node); | |
589 | if (!p) | |
590 | return -ENOMEM; | |
591 | ||
a501e324 | 592 | set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL)); |
c1cc1552 CM |
593 | } else |
594 | vmemmap_verify((pte_t *)pmd, node, addr, next); | |
595 | } while (addr = next, addr != end); | |
596 | ||
597 | return 0; | |
598 | } | |
599 | #endif /* CONFIG_ARM64_64K_PAGES */ | |
0aad818b | 600 | void vmemmap_free(unsigned long start, unsigned long end) |
0197518c TC |
601 | { |
602 | } | |
c1cc1552 | 603 | #endif /* CONFIG_SPARSEMEM_VMEMMAP */ |
af86e597 LA |
604 | |
605 | static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss; | |
9f25e6ad | 606 | #if CONFIG_PGTABLE_LEVELS > 2 |
af86e597 LA |
607 | static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss; |
608 | #endif | |
9f25e6ad | 609 | #if CONFIG_PGTABLE_LEVELS > 3 |
af86e597 LA |
610 | static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss; |
611 | #endif | |
612 | ||
613 | static inline pud_t * fixmap_pud(unsigned long addr) | |
614 | { | |
615 | pgd_t *pgd = pgd_offset_k(addr); | |
616 | ||
617 | BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd)); | |
618 | ||
619 | return pud_offset(pgd, addr); | |
620 | } | |
621 | ||
622 | static inline pmd_t * fixmap_pmd(unsigned long addr) | |
623 | { | |
624 | pud_t *pud = fixmap_pud(addr); | |
625 | ||
626 | BUG_ON(pud_none(*pud) || pud_bad(*pud)); | |
627 | ||
628 | return pmd_offset(pud, addr); | |
629 | } | |
630 | ||
631 | static inline pte_t * fixmap_pte(unsigned long addr) | |
632 | { | |
633 | pmd_t *pmd = fixmap_pmd(addr); | |
634 | ||
635 | BUG_ON(pmd_none(*pmd) || pmd_bad(*pmd)); | |
636 | ||
637 | return pte_offset_kernel(pmd, addr); | |
638 | } | |
639 | ||
640 | void __init early_fixmap_init(void) | |
641 | { | |
642 | pgd_t *pgd; | |
643 | pud_t *pud; | |
644 | pmd_t *pmd; | |
645 | unsigned long addr = FIXADDR_START; | |
646 | ||
647 | pgd = pgd_offset_k(addr); | |
648 | pgd_populate(&init_mm, pgd, bm_pud); | |
649 | pud = pud_offset(pgd, addr); | |
650 | pud_populate(&init_mm, pud, bm_pmd); | |
651 | pmd = pmd_offset(pud, addr); | |
652 | pmd_populate_kernel(&init_mm, pmd, bm_pte); | |
653 | ||
654 | /* | |
655 | * The boot-ioremap range spans multiple pmds, for which | |
656 | * we are not preparted: | |
657 | */ | |
658 | BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) | |
659 | != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); | |
660 | ||
661 | if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN))) | |
662 | || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) { | |
663 | WARN_ON(1); | |
664 | pr_warn("pmd %p != %p, %p\n", | |
665 | pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)), | |
666 | fixmap_pmd(fix_to_virt(FIX_BTMAP_END))); | |
667 | pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", | |
668 | fix_to_virt(FIX_BTMAP_BEGIN)); | |
669 | pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n", | |
670 | fix_to_virt(FIX_BTMAP_END)); | |
671 | ||
672 | pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END); | |
673 | pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN); | |
674 | } | |
675 | } | |
676 | ||
677 | void __set_fixmap(enum fixed_addresses idx, | |
678 | phys_addr_t phys, pgprot_t flags) | |
679 | { | |
680 | unsigned long addr = __fix_to_virt(idx); | |
681 | pte_t *pte; | |
682 | ||
b63dbef9 | 683 | BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); |
af86e597 LA |
684 | |
685 | pte = fixmap_pte(addr); | |
686 | ||
687 | if (pgprot_val(flags)) { | |
688 | set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags)); | |
689 | } else { | |
690 | pte_clear(&init_mm, addr, pte); | |
691 | flush_tlb_kernel_range(addr, addr+PAGE_SIZE); | |
692 | } | |
693 | } | |
61bd93ce AB |
694 | |
695 | void *__init fixmap_remap_fdt(phys_addr_t dt_phys) | |
696 | { | |
697 | const u64 dt_virt_base = __fix_to_virt(FIX_FDT); | |
fb226c3d | 698 | pgprot_t prot = PAGE_KERNEL_RO; |
b433dce0 | 699 | int size, offset; |
61bd93ce AB |
700 | void *dt_virt; |
701 | ||
702 | /* | |
703 | * Check whether the physical FDT address is set and meets the minimum | |
704 | * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be | |
705 | * at least 8 bytes so that we can always access the size field of the | |
706 | * FDT header after mapping the first chunk, double check here if that | |
707 | * is indeed the case. | |
708 | */ | |
709 | BUILD_BUG_ON(MIN_FDT_ALIGN < 8); | |
710 | if (!dt_phys || dt_phys % MIN_FDT_ALIGN) | |
711 | return NULL; | |
712 | ||
713 | /* | |
714 | * Make sure that the FDT region can be mapped without the need to | |
715 | * allocate additional translation table pages, so that it is safe | |
132233a7 | 716 | * to call create_mapping_noalloc() this early. |
61bd93ce AB |
717 | * |
718 | * On 64k pages, the FDT will be mapped using PTEs, so we need to | |
719 | * be in the same PMD as the rest of the fixmap. | |
720 | * On 4k pages, we'll use section mappings for the FDT so we only | |
721 | * have to be in the same PUD. | |
722 | */ | |
723 | BUILD_BUG_ON(dt_virt_base % SZ_2M); | |
724 | ||
b433dce0 SP |
725 | BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT != |
726 | __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT); | |
61bd93ce | 727 | |
b433dce0 | 728 | offset = dt_phys % SWAPPER_BLOCK_SIZE; |
61bd93ce AB |
729 | dt_virt = (void *)dt_virt_base + offset; |
730 | ||
731 | /* map the first chunk so we can read the size from the header */ | |
132233a7 LA |
732 | create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), |
733 | dt_virt_base, SWAPPER_BLOCK_SIZE, prot); | |
61bd93ce AB |
734 | |
735 | if (fdt_check_header(dt_virt) != 0) | |
736 | return NULL; | |
737 | ||
738 | size = fdt_totalsize(dt_virt); | |
739 | if (size > MAX_FDT_SIZE) | |
740 | return NULL; | |
741 | ||
b433dce0 | 742 | if (offset + size > SWAPPER_BLOCK_SIZE) |
132233a7 | 743 | create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base, |
b433dce0 | 744 | round_up(offset + size, SWAPPER_BLOCK_SIZE), prot); |
61bd93ce AB |
745 | |
746 | memblock_reserve(dt_phys, size); | |
747 | ||
748 | return dt_virt; | |
749 | } | |
324420bf AB |
750 | |
751 | int __init arch_ioremap_pud_supported(void) | |
752 | { | |
753 | /* only 4k granule supports level 1 block mappings */ | |
754 | return IS_ENABLED(CONFIG_ARM64_4K_PAGES); | |
755 | } | |
756 | ||
757 | int __init arch_ioremap_pmd_supported(void) | |
758 | { | |
759 | return 1; | |
760 | } | |
761 | ||
762 | int pud_set_huge(pud_t *pud, phys_addr_t phys, pgprot_t prot) | |
763 | { | |
764 | BUG_ON(phys & ~PUD_MASK); | |
765 | set_pud(pud, __pud(phys | PUD_TYPE_SECT | pgprot_val(mk_sect_prot(prot)))); | |
766 | return 1; | |
767 | } | |
768 | ||
769 | int pmd_set_huge(pmd_t *pmd, phys_addr_t phys, pgprot_t prot) | |
770 | { | |
771 | BUG_ON(phys & ~PMD_MASK); | |
772 | set_pmd(pmd, __pmd(phys | PMD_TYPE_SECT | pgprot_val(mk_sect_prot(prot)))); | |
773 | return 1; | |
774 | } | |
775 | ||
776 | int pud_clear_huge(pud_t *pud) | |
777 | { | |
778 | if (!pud_sect(*pud)) | |
779 | return 0; | |
780 | pud_clear(pud); | |
781 | return 1; | |
782 | } | |
783 | ||
784 | int pmd_clear_huge(pmd_t *pmd) | |
785 | { | |
786 | if (!pmd_sect(*pmd)) | |
787 | return 0; | |
788 | pmd_clear(pmd); | |
789 | return 1; | |
790 | } |