@@ -57,7 +57,6 @@ unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
5757EXPORT_SYMBOL (empty_zero_page );
5858
5959extern char _start [];
60- #define DTB_EARLY_BASE_VA PGDIR_SIZE
6160void * _dtb_early_va __initdata ;
6261uintptr_t _dtb_early_pa __initdata ;
6362
@@ -236,31 +235,22 @@ static void __init setup_bootmem(void)
236235 set_max_mapnr (max_low_pfn - ARCH_PFN_OFFSET );
237236
238237 reserve_initrd_mem ();
238+
239+ /*
240+ * No allocation should be done before reserving the memory as defined
241+ * in the device tree, otherwise the allocation could end up in a
242+ * reserved region.
243+ */
244+ early_init_fdt_scan_reserved_mem ();
245+
239246 /*
240247 * If DTB is built in, no need to reserve its memblock.
241248 * Otherwise, do reserve it but avoid using
242249 * early_init_fdt_reserve_self() since __pa() does
243250 * not work for DTB pointers that are fixmap addresses
244251 */
245- if (!IS_ENABLED (CONFIG_BUILTIN_DTB )) {
246- /*
247- * In case the DTB is not located in a memory region we won't
248- * be able to locate it later on via the linear mapping and
249- * get a segfault when accessing it via __va(dtb_early_pa).
250- * To avoid this situation copy DTB to a memory region.
251- * Note that memblock_phys_alloc will also reserve DTB region.
252- */
253- if (!memblock_is_memory (dtb_early_pa )) {
254- size_t fdt_size = fdt_totalsize (dtb_early_va );
255- phys_addr_t new_dtb_early_pa = memblock_phys_alloc (fdt_size , PAGE_SIZE );
256- void * new_dtb_early_va = early_memremap (new_dtb_early_pa , fdt_size );
257-
258- memcpy (new_dtb_early_va , dtb_early_va , fdt_size );
259- early_memunmap (new_dtb_early_va , fdt_size );
260- _dtb_early_pa = new_dtb_early_pa ;
261- } else
262- memblock_reserve (dtb_early_pa , fdt_totalsize (dtb_early_va ));
263- }
252+ if (!IS_ENABLED (CONFIG_BUILTIN_DTB ))
253+ memblock_reserve (dtb_early_pa , fdt_totalsize (dtb_early_va ));
264254
265255 dma_contiguous_reserve (dma32_phys_limit );
266256 if (IS_ENABLED (CONFIG_64BIT ))
@@ -279,9 +269,6 @@ pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
279269static pte_t fixmap_pte [PTRS_PER_PTE ] __page_aligned_bss ;
280270
281271pgd_t early_pg_dir [PTRS_PER_PGD ] __initdata __aligned (PAGE_SIZE );
282- static p4d_t __maybe_unused early_dtb_p4d [PTRS_PER_P4D ] __initdata __aligned (PAGE_SIZE );
283- static pud_t __maybe_unused early_dtb_pud [PTRS_PER_PUD ] __initdata __aligned (PAGE_SIZE );
284- static pmd_t __maybe_unused early_dtb_pmd [PTRS_PER_PMD ] __initdata __aligned (PAGE_SIZE );
285272
286273#ifdef CONFIG_XIP_KERNEL
287274#define pt_ops (*(struct pt_alloc_ops *)XIP_FIXUP(&pt_ops))
@@ -626,17 +613,13 @@ static void __init create_p4d_mapping(p4d_t *p4dp,
626613#define trampoline_pgd_next (pgtable_l5_enabled ? \
627614 (uintptr_t)trampoline_p4d : (pgtable_l4_enabled ? \
628615 (uintptr_t)trampoline_pud : (uintptr_t)trampoline_pmd))
629- #define early_dtb_pgd_next (pgtable_l5_enabled ? \
630- (uintptr_t)early_dtb_p4d : (pgtable_l4_enabled ? \
631- (uintptr_t)early_dtb_pud : (uintptr_t)early_dtb_pmd))
632616#else
633617#define pgd_next_t pte_t
634618#define alloc_pgd_next (__va ) pt_ops.alloc_pte(__va)
635619#define get_pgd_next_virt (__pa ) pt_ops.get_pte_virt(__pa)
636620#define create_pgd_next_mapping (__nextp , __va , __pa , __sz , __prot ) \
637621 create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
638622#define fixmap_pgd_next ((uintptr_t)fixmap_pte)
639- #define early_dtb_pgd_next ((uintptr_t)early_dtb_pmd)
640623#define create_p4d_mapping (__pmdp , __va , __pa , __sz , __prot ) do {} while(0)
641624#define create_pud_mapping (__pmdp , __va , __pa , __sz , __prot ) do {} while(0)
642625#define create_pmd_mapping (__pmdp , __va , __pa , __sz , __prot ) do {} while(0)
@@ -860,32 +843,28 @@ static void __init create_kernel_page_table(pgd_t *pgdir, bool early)
860843 * this means 2 PMD entries whereas for 32-bit kernel, this is only 1 PGDIR
861844 * entry.
862845 */
863- static void __init create_fdt_early_page_table (pgd_t * pgdir , uintptr_t dtb_pa )
846+ static void __init create_fdt_early_page_table (pgd_t * pgdir ,
847+ uintptr_t fix_fdt_va ,
848+ uintptr_t dtb_pa )
864849{
865- #ifndef CONFIG_BUILTIN_DTB
866850 uintptr_t pa = dtb_pa & ~(PMD_SIZE - 1 );
867851
868- create_pgd_mapping (early_pg_dir , DTB_EARLY_BASE_VA ,
869- IS_ENABLED (CONFIG_64BIT ) ? early_dtb_pgd_next : pa ,
870- PGDIR_SIZE ,
871- IS_ENABLED (CONFIG_64BIT ) ? PAGE_TABLE : PAGE_KERNEL );
872-
873- if (pgtable_l5_enabled )
874- create_p4d_mapping (early_dtb_p4d , DTB_EARLY_BASE_VA ,
875- (uintptr_t )early_dtb_pud , P4D_SIZE , PAGE_TABLE );
876-
877- if (pgtable_l4_enabled )
878- create_pud_mapping (early_dtb_pud , DTB_EARLY_BASE_VA ,
879- (uintptr_t )early_dtb_pmd , PUD_SIZE , PAGE_TABLE );
852+ #ifndef CONFIG_BUILTIN_DTB
853+ /* Make sure the fdt fixmap address is always aligned on PMD size */
854+ BUILD_BUG_ON (FIX_FDT % (PMD_SIZE / PAGE_SIZE ));
880855
881- if (IS_ENABLED (CONFIG_64BIT )) {
882- create_pmd_mapping (early_dtb_pmd , DTB_EARLY_BASE_VA ,
856+ /* In 32-bit only, the fdt lies in its own PGD */
857+ if (!IS_ENABLED (CONFIG_64BIT )) {
858+ create_pgd_mapping (early_pg_dir , fix_fdt_va ,
859+ pa , MAX_FDT_SIZE , PAGE_KERNEL );
860+ } else {
861+ create_pmd_mapping (fixmap_pmd , fix_fdt_va ,
883862 pa , PMD_SIZE , PAGE_KERNEL );
884- create_pmd_mapping (early_dtb_pmd , DTB_EARLY_BASE_VA + PMD_SIZE ,
863+ create_pmd_mapping (fixmap_pmd , fix_fdt_va + PMD_SIZE ,
885864 pa + PMD_SIZE , PMD_SIZE , PAGE_KERNEL );
886865 }
887866
888- dtb_early_va = (void * )DTB_EARLY_BASE_VA + (dtb_pa & (PMD_SIZE - 1 ));
867+ dtb_early_va = (void * )fix_fdt_va + (dtb_pa & (PMD_SIZE - 1 ));
889868#else
890869 /*
891870 * For 64-bit kernel, __va can't be used since it would return a linear
@@ -1055,7 +1034,8 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
10551034 create_kernel_page_table (early_pg_dir , true);
10561035
10571036 /* Setup early mapping for FDT early scan */
1058- create_fdt_early_page_table (early_pg_dir , dtb_pa );
1037+ create_fdt_early_page_table (early_pg_dir ,
1038+ __fix_to_virt (FIX_FDT ), dtb_pa );
10591039
10601040 /*
10611041 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
@@ -1097,6 +1077,16 @@ static void __init setup_vm_final(void)
10971077 u64 i ;
10981078
10991079 /* Setup swapper PGD for fixmap */
1080+ #if !defined(CONFIG_64BIT )
1081+ /*
1082+ * In 32-bit, the device tree lies in a pgd entry, so it must be copied
1083+ * directly in swapper_pg_dir in addition to the pgd entry that points
1084+ * to fixmap_pte.
1085+ */
1086+ unsigned long idx = pgd_index (__fix_to_virt (FIX_FDT ));
1087+
1088+ set_pgd (& swapper_pg_dir [idx ], early_pg_dir [idx ]);
1089+ #endif
11001090 create_pgd_mapping (swapper_pg_dir , FIXADDR_START ,
11011091 __pa_symbol (fixmap_pgd_next ),
11021092 PGDIR_SIZE , PAGE_TABLE );
0 commit comments