arm64: add support for ioremap() block mappings
[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;
120                 BUG_ON(!pgtable_alloc);
121                 pte_phys = pgtable_alloc();
122                 pte = pte_set_fixmap(pte_phys);
123                 if (pmd_sect(*pmd))
124                         split_pmd(pmd, pte);
125                 __pmd_populate(pmd, pte_phys, PMD_TYPE_TABLE);
126                 flush_tlb_all();
127                 pte_clear_fixmap();
128         }
129         BUG_ON(pmd_bad(*pmd));
130
131         pte = pte_set_fixmap_offset(pmd, addr);
132         do {
133                 set_pte(pte, pfn_pte(pfn, prot));
134                 pfn++;
135         } while (pte++, addr += PAGE_SIZE, addr != end);
136
137         pte_clear_fixmap();
138 }
139
140 static void split_pud(pud_t *old_pud, pmd_t *pmd)
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 {
147                 set_pmd(pmd, __pmd(addr | pgprot_val(prot)));
148                 addr += PMD_SIZE;
149         } while (pmd++, i++, i < PTRS_PER_PMD);
150 }
151
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
172 static void alloc_init_pmd(pud_t *pud, unsigned long addr, unsigned long end,
173                                   phys_addr_t phys, pgprot_t prot,
174                                   phys_addr_t (*pgtable_alloc)(void))
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          */
182         if (pud_none(*pud) || pud_sect(*pud)) {
183                 phys_addr_t pmd_phys;
184                 BUG_ON(!pgtable_alloc);
185                 pmd_phys = pgtable_alloc();
186                 pmd = pmd_set_fixmap(pmd_phys);
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                 }
194                 __pud_populate(pud, pmd_phys, PUD_TYPE_TABLE);
195                 flush_tlb_all();
196                 pmd_clear_fixmap();
197         }
198         BUG_ON(pud_bad(*pud));
199
200         pmd = pmd_set_fixmap_offset(pud, addr);
201         do {
202                 next = pmd_addr_end(addr, end);
203                 /* try section mapping first */
204                 if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
205                       block_mappings_allowed(pgtable_alloc)) {
206                         pmd_t old_pmd =*pmd;
207                         set_pmd(pmd, __pmd(phys |
208                                            pgprot_val(mk_sect_prot(prot))));
209                         /*
210                          * Check for previous table entries created during
211                          * boot (__create_page_tables) and flush them.
212                          */
213                         if (!pmd_none(old_pmd)) {
214                                 flush_tlb_all();
215                                 if (pmd_table(old_pmd)) {
216                                         phys_addr_t table = pmd_page_paddr(old_pmd);
217                                         if (!WARN_ON_ONCE(slab_is_available()))
218                                                 memblock_free(table, PAGE_SIZE);
219                                 }
220                         }
221                 } else {
222                         alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys),
223                                        prot, pgtable_alloc);
224                 }
225                 phys += next - addr;
226         } while (pmd++, addr = next, addr != end);
227
228         pmd_clear_fixmap();
229 }
230
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
243 static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
244                                   phys_addr_t phys, pgprot_t prot,
245                                   phys_addr_t (*pgtable_alloc)(void))
246 {
247         pud_t *pud;
248         unsigned long next;
249
250         if (pgd_none(*pgd)) {
251                 phys_addr_t pud_phys;
252                 BUG_ON(!pgtable_alloc);
253                 pud_phys = pgtable_alloc();
254                 __pgd_populate(pgd, pud_phys, PUD_TYPE_TABLE);
255         }
256         BUG_ON(pgd_bad(*pgd));
257
258         pud = pud_set_fixmap_offset(pgd, addr);
259         do {
260                 next = pud_addr_end(addr, end);
261
262                 /*
263                  * For 4K granule only, attempt to put down a 1GB block
264                  */
265                 if (use_1G_block(addr, next, phys) &&
266                     block_mappings_allowed(pgtable_alloc)) {
267                         pud_t old_pud = *pud;
268                         set_pud(pud, __pud(phys |
269                                            pgprot_val(mk_sect_prot(prot))));
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)) {
279                                 flush_tlb_all();
280                                 if (pud_table(old_pud)) {
281                                         phys_addr_t table = pud_page_paddr(old_pud);
282                                         if (!WARN_ON_ONCE(slab_is_available()))
283                                                 memblock_free(table, PAGE_SIZE);
284                                 }
285                         }
286                 } else {
287                         alloc_init_pmd(pud, addr, next, phys, prot,
288                                        pgtable_alloc);
289                 }
290                 phys += next - addr;
291         } while (pud++, addr = next, addr != end);
292
293         pud_clear_fixmap();
294 }
295
296 /*
297  * Create the page directory entries and any necessary page tables for the
298  * mapping specified by 'md'.
299  */
300 static void init_pgd(pgd_t *pgd, phys_addr_t phys, unsigned long virt,
301                                     phys_addr_t size, pgprot_t prot,
302                                     phys_addr_t (*pgtable_alloc)(void))
303 {
304         unsigned long addr, length, end, next;
305
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
313         phys &= PAGE_MASK;
314         addr = virt & PAGE_MASK;
315         length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
316
317         end = addr + length;
318         do {
319                 next = pgd_addr_end(addr, end);
320                 alloc_init_pud(pgd, addr, next, phys, prot, pgtable_alloc);
321                 phys += next - addr;
322         } while (pgd++, addr = next, addr != end);
323 }
324
325 static phys_addr_t late_pgtable_alloc(void)
326 {
327         void *ptr = (void *)__get_free_page(PGALLOC_GFP);
328         BUG_ON(!ptr);
329
330         /* Ensure the zeroed page is visible to the page table walker */
331         dsb(ishst);
332         return __pa(ptr);
333 }
334
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
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,
349                                   phys_addr_t size, pgprot_t prot)
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         }
356         __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
357                              NULL);
358 }
359
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 {
364         __create_pgd_mapping(mm->pgd, phys, virt, size, prot,
365                              late_pgtable_alloc);
366 }
367
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
377         __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
378                              late_pgtable_alloc);
379 }
380
381 static void __init __map_memblock(pgd_t *pgd, phys_addr_t start, phys_addr_t end)
382 {
383
384         unsigned long kernel_start = __pa(_stext);
385         unsigned long kernel_end = __pa(_end);
386
387         /*
388          * The kernel itself is mapped at page granularity. Map all other
389          * memory, making sure we don't overwrite the existing kernel mappings.
390          */
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;
398         }
399
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);
414 }
415
416 static void __init map_mem(pgd_t *pgd)
417 {
418         struct memblock_region *reg;
419
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;
427
428                 __map_memblock(pgd, start, end);
429         }
430 }
431
432 #ifdef CONFIG_DEBUG_RODATA
433 void mark_rodata_ro(void)
434 {
435         create_mapping_late(__pa(_stext), (unsigned long)_stext,
436                                 (unsigned long)_etext - (unsigned long)_stext,
437                                 PAGE_KERNEL_ROX);
438
439 }
440 #endif
441
442 void fixup_init(void)
443 {
444         create_mapping_late(__pa(__init_begin), (unsigned long)__init_begin,
445                         (unsigned long)__init_end - (unsigned long)__init_begin,
446                         PAGE_KERNEL);
447 }
448
449 static void __init map_kernel_chunk(pgd_t *pgd, void *va_start, void *va_end,
450                                     pgprot_t prot)
451 {
452         phys_addr_t pa_start = __pa(va_start);
453         unsigned long size = va_end - va_start;
454
455         BUG_ON(!PAGE_ALIGNED(pa_start));
456         BUG_ON(!PAGE_ALIGNED(size));
457
458         __create_pgd_mapping(pgd, pa_start, (unsigned long)va_start, size, prot,
459                              early_pgtable_alloc);
460 }
461
462 /*
463  * Create fine-grained mappings for the kernel.
464  */
465 static void __init map_kernel(pgd_t *pgd)
466 {
467
468         map_kernel_chunk(pgd, _stext, _etext, PAGE_KERNEL_EXEC);
469         map_kernel_chunk(pgd, __init_begin, __init_end, PAGE_KERNEL_EXEC);
470         map_kernel_chunk(pgd, _data, _end, PAGE_KERNEL);
471
472         /*
473          * The fixmap falls in a separate pgd to the kernel, and doesn't live
474          * in the carveout for the swapper_pg_dir. We can simply re-use the
475          * existing dir for the fixmap.
476          */
477         set_pgd(pgd_offset_raw(pgd, FIXADDR_START), *pgd_offset_k(FIXADDR_START));
478
479         kasan_copy_shadow(pgd);
480 }
481
482 /*
483  * paging_init() sets up the page tables, initialises the zone memory
484  * maps and sets up the zero page.
485  */
486 void __init paging_init(void)
487 {
488         phys_addr_t pgd_phys = early_pgtable_alloc();
489         pgd_t *pgd = pgd_set_fixmap(pgd_phys);
490
491         map_kernel(pgd);
492         map_mem(pgd);
493
494         /*
495          * We want to reuse the original swapper_pg_dir so we don't have to
496          * communicate the new address to non-coherent secondaries in
497          * secondary_entry, and so cpu_switch_mm can generate the address with
498          * adrp+add rather than a load from some global variable.
499          *
500          * To do this we need to go via a temporary pgd.
501          */
502         cpu_replace_ttbr1(__va(pgd_phys));
503         memcpy(swapper_pg_dir, pgd, PAGE_SIZE);
504         cpu_replace_ttbr1(swapper_pg_dir);
505
506         pgd_clear_fixmap();
507         memblock_free(pgd_phys, PAGE_SIZE);
508
509         /*
510          * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd
511          * allocated with it.
512          */
513         memblock_free(__pa(swapper_pg_dir) + PAGE_SIZE,
514                       SWAPPER_DIR_SIZE - PAGE_SIZE);
515
516         bootmem_init();
517 }
518
519 /*
520  * Check whether a kernel address is valid (derived from arch/x86/).
521  */
522 int kern_addr_valid(unsigned long addr)
523 {
524         pgd_t *pgd;
525         pud_t *pud;
526         pmd_t *pmd;
527         pte_t *pte;
528
529         if ((((long)addr) >> VA_BITS) != -1UL)
530                 return 0;
531
532         pgd = pgd_offset_k(addr);
533         if (pgd_none(*pgd))
534                 return 0;
535
536         pud = pud_offset(pgd, addr);
537         if (pud_none(*pud))
538                 return 0;
539
540         if (pud_sect(*pud))
541                 return pfn_valid(pud_pfn(*pud));
542
543         pmd = pmd_offset(pud, addr);
544         if (pmd_none(*pmd))
545                 return 0;
546
547         if (pmd_sect(*pmd))
548                 return pfn_valid(pmd_pfn(*pmd));
549
550         pte = pte_offset_kernel(pmd, addr);
551         if (pte_none(*pte))
552                 return 0;
553
554         return pfn_valid(pte_pfn(*pte));
555 }
556 #ifdef CONFIG_SPARSEMEM_VMEMMAP
557 #if !ARM64_SWAPPER_USES_SECTION_MAPS
558 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
559 {
560         return vmemmap_populate_basepages(start, end, node);
561 }
562 #else   /* !ARM64_SWAPPER_USES_SECTION_MAPS */
563 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
564 {
565         unsigned long addr = start;
566         unsigned long next;
567         pgd_t *pgd;
568         pud_t *pud;
569         pmd_t *pmd;
570
571         do {
572                 next = pmd_addr_end(addr, end);
573
574                 pgd = vmemmap_pgd_populate(addr, node);
575                 if (!pgd)
576                         return -ENOMEM;
577
578                 pud = vmemmap_pud_populate(pgd, addr, node);
579                 if (!pud)
580                         return -ENOMEM;
581
582                 pmd = pmd_offset(pud, addr);
583                 if (pmd_none(*pmd)) {
584                         void *p = NULL;
585
586                         p = vmemmap_alloc_block_buf(PMD_SIZE, node);
587                         if (!p)
588                                 return -ENOMEM;
589
590                         set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL));
591                 } else
592                         vmemmap_verify((pte_t *)pmd, node, addr, next);
593         } while (addr = next, addr != end);
594
595         return 0;
596 }
597 #endif  /* CONFIG_ARM64_64K_PAGES */
598 void vmemmap_free(unsigned long start, unsigned long end)
599 {
600 }
601 #endif  /* CONFIG_SPARSEMEM_VMEMMAP */
602
603 static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
604 #if CONFIG_PGTABLE_LEVELS > 2
605 static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss;
606 #endif
607 #if CONFIG_PGTABLE_LEVELS > 3
608 static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss;
609 #endif
610
611 static inline pud_t * fixmap_pud(unsigned long addr)
612 {
613         pgd_t *pgd = pgd_offset_k(addr);
614
615         BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
616
617         return pud_offset(pgd, addr);
618 }
619
620 static inline pmd_t * fixmap_pmd(unsigned long addr)
621 {
622         pud_t *pud = fixmap_pud(addr);
623
624         BUG_ON(pud_none(*pud) || pud_bad(*pud));
625
626         return pmd_offset(pud, addr);
627 }
628
629 static inline pte_t * fixmap_pte(unsigned long addr)
630 {
631         pmd_t *pmd = fixmap_pmd(addr);
632
633         BUG_ON(pmd_none(*pmd) || pmd_bad(*pmd));
634
635         return pte_offset_kernel(pmd, addr);
636 }
637
638 void __init early_fixmap_init(void)
639 {
640         pgd_t *pgd;
641         pud_t *pud;
642         pmd_t *pmd;
643         unsigned long addr = FIXADDR_START;
644
645         pgd = pgd_offset_k(addr);
646         pgd_populate(&init_mm, pgd, bm_pud);
647         pud = pud_offset(pgd, addr);
648         pud_populate(&init_mm, pud, bm_pmd);
649         pmd = pmd_offset(pud, addr);
650         pmd_populate_kernel(&init_mm, pmd, bm_pte);
651
652         /*
653          * The boot-ioremap range spans multiple pmds, for which
654          * we are not preparted:
655          */
656         BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
657                      != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
658
659         if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
660              || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
661                 WARN_ON(1);
662                 pr_warn("pmd %p != %p, %p\n",
663                         pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
664                         fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
665                 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
666                         fix_to_virt(FIX_BTMAP_BEGIN));
667                 pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
668                         fix_to_virt(FIX_BTMAP_END));
669
670                 pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
671                 pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
672         }
673 }
674
675 void __set_fixmap(enum fixed_addresses idx,
676                                phys_addr_t phys, pgprot_t flags)
677 {
678         unsigned long addr = __fix_to_virt(idx);
679         pte_t *pte;
680
681         BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
682
683         pte = fixmap_pte(addr);
684
685         if (pgprot_val(flags)) {
686                 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
687         } else {
688                 pte_clear(&init_mm, addr, pte);
689                 flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
690         }
691 }
692
693 void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
694 {
695         const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
696         pgprot_t prot = PAGE_KERNEL_RO;
697         int size, offset;
698         void *dt_virt;
699
700         /*
701          * Check whether the physical FDT address is set and meets the minimum
702          * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
703          * at least 8 bytes so that we can always access the size field of the
704          * FDT header after mapping the first chunk, double check here if that
705          * is indeed the case.
706          */
707         BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
708         if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
709                 return NULL;
710
711         /*
712          * Make sure that the FDT region can be mapped without the need to
713          * allocate additional translation table pages, so that it is safe
714          * to call create_mapping_noalloc() this early.
715          *
716          * On 64k pages, the FDT will be mapped using PTEs, so we need to
717          * be in the same PMD as the rest of the fixmap.
718          * On 4k pages, we'll use section mappings for the FDT so we only
719          * have to be in the same PUD.
720          */
721         BUILD_BUG_ON(dt_virt_base % SZ_2M);
722
723         BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
724                      __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
725
726         offset = dt_phys % SWAPPER_BLOCK_SIZE;
727         dt_virt = (void *)dt_virt_base + offset;
728
729         /* map the first chunk so we can read the size from the header */
730         create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
731                         dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
732
733         if (fdt_check_header(dt_virt) != 0)
734                 return NULL;
735
736         size = fdt_totalsize(dt_virt);
737         if (size > MAX_FDT_SIZE)
738                 return NULL;
739
740         if (offset + size > SWAPPER_BLOCK_SIZE)
741                 create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
742                                round_up(offset + size, SWAPPER_BLOCK_SIZE), prot);
743
744         memblock_reserve(dt_phys, size);
745
746         return dt_virt;
747 }
748
749 int __init arch_ioremap_pud_supported(void)
750 {
751         /* only 4k granule supports level 1 block mappings */
752         return IS_ENABLED(CONFIG_ARM64_4K_PAGES);
753 }
754
755 int __init arch_ioremap_pmd_supported(void)
756 {
757         return 1;
758 }
759
760 int pud_set_huge(pud_t *pud, phys_addr_t phys, pgprot_t prot)
761 {
762         BUG_ON(phys & ~PUD_MASK);
763         set_pud(pud, __pud(phys | PUD_TYPE_SECT | pgprot_val(mk_sect_prot(prot))));
764         return 1;
765 }
766
767 int pmd_set_huge(pmd_t *pmd, phys_addr_t phys, pgprot_t prot)
768 {
769         BUG_ON(phys & ~PMD_MASK);
770         set_pmd(pmd, __pmd(phys | PMD_TYPE_SECT | pgprot_val(mk_sect_prot(prot))));
771         return 1;
772 }
773
774 int pud_clear_huge(pud_t *pud)
775 {
776         if (!pud_sect(*pud))
777                 return 0;
778         pud_clear(pud);
779         return 1;
780 }
781
782 int pmd_clear_huge(pmd_t *pmd)
783 {
784         if (!pmd_sect(*pmd))
785                 return 0;
786         pmd_clear(pmd);
787         return 1;
788 }