arm64: mm: create new fine-grained mappings at boot
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / mm / mmu.c
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>
24 #include <linux/libfdt.h>
25 #include <linux/mman.h>
26 #include <linux/nodemask.h>
27 #include <linux/memblock.h>
28 #include <linux/fs.h>
29 #include <linux/io.h>
30 #include <linux/slab.h>
31 #include <linux/stop_machine.h>
32
33 #include <asm/barrier.h>
34 #include <asm/cputype.h>
35 #include <asm/fixmap.h>
36 #include <asm/kasan.h>
37 #include <asm/kernel-pgtable.h>
38 #include <asm/sections.h>
39 #include <asm/setup.h>
40 #include <asm/sizes.h>
41 #include <asm/tlb.h>
42 #include <asm/memblock.h>
43 #include <asm/mmu_context.h>
44
45 #include "mm.h"
46
47 u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
48
49 /*
50  * Empty_zero_page is a special page that is used for zero-initialized data
51  * and COW.
52  */
53 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
54 EXPORT_SYMBOL(empty_zero_page);
55
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
67 static phys_addr_t __init early_pgtable_alloc(void)
68 {
69         phys_addr_t phys;
70         void *ptr;
71
72         phys = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
73         BUG_ON(!phys);
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
82         memset(ptr, 0, PAGE_SIZE);
83
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;
91 }
92
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
104                  * permissions will be fixed up later
105                  */
106                 set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
107                 pfn++;
108         } while (pte++, i++, i < PTRS_PER_PTE);
109 }
110
111 static void alloc_init_pte(pmd_t *pmd, unsigned long addr,
112                                   unsigned long end, unsigned long pfn,
113                                   pgprot_t prot,
114                                   phys_addr_t (*pgtable_alloc)(void))
115 {
116         pte_t *pte;
117
118         if (pmd_none(*pmd) || pmd_sect(*pmd)) {
119                 phys_addr_t pte_phys = pgtable_alloc();
120                 pte = pte_set_fixmap(pte_phys);
121                 if (pmd_sect(*pmd))
122                         split_pmd(pmd, pte);
123                 __pmd_populate(pmd, pte_phys, PMD_TYPE_TABLE);
124                 flush_tlb_all();
125                 pte_clear_fixmap();
126         }
127         BUG_ON(pmd_bad(*pmd));
128
129         pte = pte_set_fixmap_offset(pmd, addr);
130         do {
131                 set_pte(pte, pfn_pte(pfn, prot));
132                 pfn++;
133         } while (pte++, addr += PAGE_SIZE, addr != end);
134
135         pte_clear_fixmap();
136 }
137
138 static void split_pud(pud_t *old_pud, pmd_t *pmd)
139 {
140         unsigned long addr = pud_pfn(*old_pud) << PAGE_SHIFT;
141         pgprot_t prot = __pgprot(pud_val(*old_pud) ^ addr);
142         int i = 0;
143
144         do {
145                 set_pmd(pmd, __pmd(addr | pgprot_val(prot)));
146                 addr += PMD_SIZE;
147         } while (pmd++, i++, i < PTRS_PER_PMD);
148 }
149
150 static void alloc_init_pmd(pud_t *pud, unsigned long addr, unsigned long end,
151                                   phys_addr_t phys, pgprot_t prot,
152                                   phys_addr_t (*pgtable_alloc)(void))
153 {
154         pmd_t *pmd;
155         unsigned long next;
156
157         /*
158          * Check for initial section mappings in the pgd/pud and remove them.
159          */
160         if (pud_none(*pud) || pud_sect(*pud)) {
161                 phys_addr_t pmd_phys = pgtable_alloc();
162                 pmd = pmd_set_fixmap(pmd_phys);
163                 if (pud_sect(*pud)) {
164                         /*
165                          * need to have the 1G of mappings continue to be
166                          * present
167                          */
168                         split_pud(pud, pmd);
169                 }
170                 __pud_populate(pud, pmd_phys, PUD_TYPE_TABLE);
171                 flush_tlb_all();
172                 pmd_clear_fixmap();
173         }
174         BUG_ON(pud_bad(*pud));
175
176         pmd = pmd_set_fixmap_offset(pud, addr);
177         do {
178                 next = pmd_addr_end(addr, end);
179                 /* try section mapping first */
180                 if (((addr | next | phys) & ~SECTION_MASK) == 0) {
181                         pmd_t old_pmd =*pmd;
182                         set_pmd(pmd, __pmd(phys |
183                                            pgprot_val(mk_sect_prot(prot))));
184                         /*
185                          * Check for previous table entries created during
186                          * boot (__create_page_tables) and flush them.
187                          */
188                         if (!pmd_none(old_pmd)) {
189                                 flush_tlb_all();
190                                 if (pmd_table(old_pmd)) {
191                                         phys_addr_t table = pmd_page_paddr(old_pmd);
192                                         if (!WARN_ON_ONCE(slab_is_available()))
193                                                 memblock_free(table, PAGE_SIZE);
194                                 }
195                         }
196                 } else {
197                         alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys),
198                                        prot, pgtable_alloc);
199                 }
200                 phys += next - addr;
201         } while (pmd++, addr = next, addr != end);
202
203         pmd_clear_fixmap();
204 }
205
206 static inline bool use_1G_block(unsigned long addr, unsigned long next,
207                         unsigned long phys)
208 {
209         if (PAGE_SHIFT != 12)
210                 return false;
211
212         if (((addr | next | phys) & ~PUD_MASK) != 0)
213                 return false;
214
215         return true;
216 }
217
218 static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
219                                   phys_addr_t phys, pgprot_t prot,
220                                   phys_addr_t (*pgtable_alloc)(void))
221 {
222         pud_t *pud;
223         unsigned long next;
224
225         if (pgd_none(*pgd)) {
226                 phys_addr_t pud_phys = pgtable_alloc();
227                 __pgd_populate(pgd, pud_phys, PUD_TYPE_TABLE);
228         }
229         BUG_ON(pgd_bad(*pgd));
230
231         pud = pud_set_fixmap_offset(pgd, addr);
232         do {
233                 next = pud_addr_end(addr, end);
234
235                 /*
236                  * For 4K granule only, attempt to put down a 1GB block
237                  */
238                 if (use_1G_block(addr, next, phys)) {
239                         pud_t old_pud = *pud;
240                         set_pud(pud, __pud(phys |
241                                            pgprot_val(mk_sect_prot(prot))));
242
243                         /*
244                          * If we have an old value for a pud, it will
245                          * be pointing to a pmd table that we no longer
246                          * need (from swapper_pg_dir).
247                          *
248                          * Look up the old pmd table and free it.
249                          */
250                         if (!pud_none(old_pud)) {
251                                 flush_tlb_all();
252                                 if (pud_table(old_pud)) {
253                                         phys_addr_t table = pud_page_paddr(old_pud);
254                                         if (!WARN_ON_ONCE(slab_is_available()))
255                                                 memblock_free(table, PAGE_SIZE);
256                                 }
257                         }
258                 } else {
259                         alloc_init_pmd(pud, addr, next, phys, prot,
260                                        pgtable_alloc);
261                 }
262                 phys += next - addr;
263         } while (pud++, addr = next, addr != end);
264
265         pud_clear_fixmap();
266 }
267
268 /*
269  * Create the page directory entries and any necessary page tables for the
270  * mapping specified by 'md'.
271  */
272 static void init_pgd(pgd_t *pgd, phys_addr_t phys, unsigned long virt,
273                                     phys_addr_t size, pgprot_t prot,
274                                     phys_addr_t (*pgtable_alloc)(void))
275 {
276         unsigned long addr, length, end, next;
277
278         addr = virt & PAGE_MASK;
279         length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
280
281         end = addr + length;
282         do {
283                 next = pgd_addr_end(addr, end);
284                 alloc_init_pud(pgd, addr, next, phys, prot, pgtable_alloc);
285                 phys += next - addr;
286         } while (pgd++, addr = next, addr != end);
287 }
288
289 static phys_addr_t late_pgtable_alloc(void)
290 {
291         void *ptr = (void *)__get_free_page(PGALLOC_GFP);
292         BUG_ON(!ptr);
293
294         /* Ensure the zeroed page is visible to the page table walker */
295         dsb(ishst);
296         return __pa(ptr);
297 }
298
299 static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
300                                  unsigned long virt, phys_addr_t size,
301                                  pgprot_t prot,
302                                  phys_addr_t (*alloc)(void))
303 {
304         init_pgd(pgd_offset_raw(pgdir, virt), phys, virt, size, prot, alloc);
305 }
306
307 static void __init create_mapping(phys_addr_t phys, unsigned long virt,
308                                   phys_addr_t size, pgprot_t prot)
309 {
310         if (virt < VMALLOC_START) {
311                 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
312                         &phys, virt);
313                 return;
314         }
315         __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
316                              early_pgtable_alloc);
317 }
318
319 void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
320                                unsigned long virt, phys_addr_t size,
321                                pgprot_t prot)
322 {
323         __create_pgd_mapping(mm->pgd, phys, virt, size, prot,
324                              late_pgtable_alloc);
325 }
326
327 static void create_mapping_late(phys_addr_t phys, unsigned long virt,
328                                   phys_addr_t size, pgprot_t prot)
329 {
330         if (virt < VMALLOC_START) {
331                 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
332                         &phys, virt);
333                 return;
334         }
335
336         __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
337                              late_pgtable_alloc);
338 }
339
340 static void __init __map_memblock(pgd_t *pgd, phys_addr_t start, phys_addr_t end)
341 {
342
343         unsigned long kernel_start = __pa(_stext);
344         unsigned long kernel_end = __pa(_end);
345
346         /*
347          * The kernel itself is mapped at page granularity. Map all other
348          * memory, making sure we don't overwrite the existing kernel mappings.
349          */
350
351         /* No overlap with the kernel. */
352         if (end < kernel_start || start >= kernel_end) {
353                 __create_pgd_mapping(pgd, start, __phys_to_virt(start),
354                                      end - start, PAGE_KERNEL,
355                                      early_pgtable_alloc);
356                 return;
357         }
358
359         /*
360          * This block overlaps the kernel mapping. Map the portion(s) which
361          * don't overlap.
362          */
363         if (start < kernel_start)
364                 __create_pgd_mapping(pgd, start,
365                                      __phys_to_virt(start),
366                                      kernel_start - start, PAGE_KERNEL,
367                                      early_pgtable_alloc);
368         if (kernel_end < end)
369                 __create_pgd_mapping(pgd, kernel_end,
370                                      __phys_to_virt(kernel_end),
371                                      end - kernel_end, PAGE_KERNEL,
372                                      early_pgtable_alloc);
373 }
374
375 static void __init map_mem(pgd_t *pgd)
376 {
377         struct memblock_region *reg;
378
379         /* map all the memory banks */
380         for_each_memblock(memory, reg) {
381                 phys_addr_t start = reg->base;
382                 phys_addr_t end = start + reg->size;
383
384                 if (start >= end)
385                         break;
386
387                 __map_memblock(pgd, start, end);
388         }
389 }
390
391 #ifdef CONFIG_DEBUG_RODATA
392 void mark_rodata_ro(void)
393 {
394         create_mapping_late(__pa(_stext), (unsigned long)_stext,
395                                 (unsigned long)_etext - (unsigned long)_stext,
396                                 PAGE_KERNEL_ROX);
397
398 }
399 #endif
400
401 void fixup_init(void)
402 {
403         create_mapping_late(__pa(__init_begin), (unsigned long)__init_begin,
404                         (unsigned long)__init_end - (unsigned long)__init_begin,
405                         PAGE_KERNEL);
406 }
407
408 static void __init map_kernel_chunk(pgd_t *pgd, void *va_start, void *va_end,
409                                     pgprot_t prot)
410 {
411         phys_addr_t pa_start = __pa(va_start);
412         unsigned long size = va_end - va_start;
413
414         BUG_ON(!PAGE_ALIGNED(pa_start));
415         BUG_ON(!PAGE_ALIGNED(size));
416
417         __create_pgd_mapping(pgd, pa_start, (unsigned long)va_start, size, prot,
418                              early_pgtable_alloc);
419 }
420
421 /*
422  * Create fine-grained mappings for the kernel.
423  */
424 static void __init map_kernel(pgd_t *pgd)
425 {
426
427         map_kernel_chunk(pgd, _stext, _etext, PAGE_KERNEL_EXEC);
428         map_kernel_chunk(pgd, __init_begin, __init_end, PAGE_KERNEL_EXEC);
429         map_kernel_chunk(pgd, _data, _end, PAGE_KERNEL);
430
431         /*
432          * The fixmap falls in a separate pgd to the kernel, and doesn't live
433          * in the carveout for the swapper_pg_dir. We can simply re-use the
434          * existing dir for the fixmap.
435          */
436         set_pgd(pgd_offset_raw(pgd, FIXADDR_START), *pgd_offset_k(FIXADDR_START));
437
438         kasan_copy_shadow(pgd);
439 }
440
441 /*
442  * paging_init() sets up the page tables, initialises the zone memory
443  * maps and sets up the zero page.
444  */
445 void __init paging_init(void)
446 {
447         phys_addr_t pgd_phys = early_pgtable_alloc();
448         pgd_t *pgd = pgd_set_fixmap(pgd_phys);
449
450         map_kernel(pgd);
451         map_mem(pgd);
452
453         /*
454          * We want to reuse the original swapper_pg_dir so we don't have to
455          * communicate the new address to non-coherent secondaries in
456          * secondary_entry, and so cpu_switch_mm can generate the address with
457          * adrp+add rather than a load from some global variable.
458          *
459          * To do this we need to go via a temporary pgd.
460          */
461         cpu_replace_ttbr1(__va(pgd_phys));
462         memcpy(swapper_pg_dir, pgd, PAGE_SIZE);
463         cpu_replace_ttbr1(swapper_pg_dir);
464
465         pgd_clear_fixmap();
466         memblock_free(pgd_phys, PAGE_SIZE);
467
468         /*
469          * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd
470          * allocated with it.
471          */
472         memblock_free(__pa(swapper_pg_dir) + PAGE_SIZE,
473                       SWAPPER_DIR_SIZE - PAGE_SIZE);
474
475         bootmem_init();
476 }
477
478 /*
479  * Check whether a kernel address is valid (derived from arch/x86/).
480  */
481 int kern_addr_valid(unsigned long addr)
482 {
483         pgd_t *pgd;
484         pud_t *pud;
485         pmd_t *pmd;
486         pte_t *pte;
487
488         if ((((long)addr) >> VA_BITS) != -1UL)
489                 return 0;
490
491         pgd = pgd_offset_k(addr);
492         if (pgd_none(*pgd))
493                 return 0;
494
495         pud = pud_offset(pgd, addr);
496         if (pud_none(*pud))
497                 return 0;
498
499         if (pud_sect(*pud))
500                 return pfn_valid(pud_pfn(*pud));
501
502         pmd = pmd_offset(pud, addr);
503         if (pmd_none(*pmd))
504                 return 0;
505
506         if (pmd_sect(*pmd))
507                 return pfn_valid(pmd_pfn(*pmd));
508
509         pte = pte_offset_kernel(pmd, addr);
510         if (pte_none(*pte))
511                 return 0;
512
513         return pfn_valid(pte_pfn(*pte));
514 }
515 #ifdef CONFIG_SPARSEMEM_VMEMMAP
516 #if !ARM64_SWAPPER_USES_SECTION_MAPS
517 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
518 {
519         return vmemmap_populate_basepages(start, end, node);
520 }
521 #else   /* !ARM64_SWAPPER_USES_SECTION_MAPS */
522 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
523 {
524         unsigned long addr = start;
525         unsigned long next;
526         pgd_t *pgd;
527         pud_t *pud;
528         pmd_t *pmd;
529
530         do {
531                 next = pmd_addr_end(addr, end);
532
533                 pgd = vmemmap_pgd_populate(addr, node);
534                 if (!pgd)
535                         return -ENOMEM;
536
537                 pud = vmemmap_pud_populate(pgd, addr, node);
538                 if (!pud)
539                         return -ENOMEM;
540
541                 pmd = pmd_offset(pud, addr);
542                 if (pmd_none(*pmd)) {
543                         void *p = NULL;
544
545                         p = vmemmap_alloc_block_buf(PMD_SIZE, node);
546                         if (!p)
547                                 return -ENOMEM;
548
549                         set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL));
550                 } else
551                         vmemmap_verify((pte_t *)pmd, node, addr, next);
552         } while (addr = next, addr != end);
553
554         return 0;
555 }
556 #endif  /* CONFIG_ARM64_64K_PAGES */
557 void vmemmap_free(unsigned long start, unsigned long end)
558 {
559 }
560 #endif  /* CONFIG_SPARSEMEM_VMEMMAP */
561
562 static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
563 #if CONFIG_PGTABLE_LEVELS > 2
564 static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss;
565 #endif
566 #if CONFIG_PGTABLE_LEVELS > 3
567 static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss;
568 #endif
569
570 static inline pud_t * fixmap_pud(unsigned long addr)
571 {
572         pgd_t *pgd = pgd_offset_k(addr);
573
574         BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
575
576         return pud_offset(pgd, addr);
577 }
578
579 static inline pmd_t * fixmap_pmd(unsigned long addr)
580 {
581         pud_t *pud = fixmap_pud(addr);
582
583         BUG_ON(pud_none(*pud) || pud_bad(*pud));
584
585         return pmd_offset(pud, addr);
586 }
587
588 static inline pte_t * fixmap_pte(unsigned long addr)
589 {
590         pmd_t *pmd = fixmap_pmd(addr);
591
592         BUG_ON(pmd_none(*pmd) || pmd_bad(*pmd));
593
594         return pte_offset_kernel(pmd, addr);
595 }
596
597 void __init early_fixmap_init(void)
598 {
599         pgd_t *pgd;
600         pud_t *pud;
601         pmd_t *pmd;
602         unsigned long addr = FIXADDR_START;
603
604         pgd = pgd_offset_k(addr);
605         pgd_populate(&init_mm, pgd, bm_pud);
606         pud = pud_offset(pgd, addr);
607         pud_populate(&init_mm, pud, bm_pmd);
608         pmd = pmd_offset(pud, addr);
609         pmd_populate_kernel(&init_mm, pmd, bm_pte);
610
611         /*
612          * The boot-ioremap range spans multiple pmds, for which
613          * we are not preparted:
614          */
615         BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
616                      != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
617
618         if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
619              || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
620                 WARN_ON(1);
621                 pr_warn("pmd %p != %p, %p\n",
622                         pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
623                         fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
624                 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
625                         fix_to_virt(FIX_BTMAP_BEGIN));
626                 pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
627                         fix_to_virt(FIX_BTMAP_END));
628
629                 pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
630                 pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
631         }
632 }
633
634 void __set_fixmap(enum fixed_addresses idx,
635                                phys_addr_t phys, pgprot_t flags)
636 {
637         unsigned long addr = __fix_to_virt(idx);
638         pte_t *pte;
639
640         BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
641
642         pte = fixmap_pte(addr);
643
644         if (pgprot_val(flags)) {
645                 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
646         } else {
647                 pte_clear(&init_mm, addr, pte);
648                 flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
649         }
650 }
651
652 void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
653 {
654         const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
655         pgprot_t prot = PAGE_KERNEL_RO;
656         int size, offset;
657         void *dt_virt;
658
659         /*
660          * Check whether the physical FDT address is set and meets the minimum
661          * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
662          * at least 8 bytes so that we can always access the magic and size
663          * fields of the FDT header after mapping the first chunk, double check
664          * here if that is indeed the case.
665          */
666         BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
667         if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
668                 return NULL;
669
670         /*
671          * Make sure that the FDT region can be mapped without the need to
672          * allocate additional translation table pages, so that it is safe
673          * to call create_mapping() this early.
674          *
675          * On 64k pages, the FDT will be mapped using PTEs, so we need to
676          * be in the same PMD as the rest of the fixmap.
677          * On 4k pages, we'll use section mappings for the FDT so we only
678          * have to be in the same PUD.
679          */
680         BUILD_BUG_ON(dt_virt_base % SZ_2M);
681
682         BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
683                      __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
684
685         offset = dt_phys % SWAPPER_BLOCK_SIZE;
686         dt_virt = (void *)dt_virt_base + offset;
687
688         /* map the first chunk so we can read the size from the header */
689         create_mapping(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
690                        SWAPPER_BLOCK_SIZE, prot);
691
692         if (fdt_magic(dt_virt) != FDT_MAGIC)
693                 return NULL;
694
695         size = fdt_totalsize(dt_virt);
696         if (size > MAX_FDT_SIZE)
697                 return NULL;
698
699         if (offset + size > SWAPPER_BLOCK_SIZE)
700                 create_mapping(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
701                                round_up(offset + size, SWAPPER_BLOCK_SIZE), prot);
702
703         memblock_reserve(dt_phys, size);
704
705         return dt_virt;
706 }