by the linker, since the locations of such sections can change between linker
          versions.
 
+config HAVE_ARCH_PFN_VALID
+       bool
+
 source "kernel/gcov/Kconfig"
 
 source "scripts/gcc-plugins/Kconfig"
 
        select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU
        select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
        select HAVE_ARCH_MMAP_RND_BITS if MMU
+       select HAVE_ARCH_PFN_VALID
        select HAVE_ARCH_SECCOMP
        select HAVE_ARCH_SECCOMP_FILTER if AEABI && !OABI_COMPAT
        select HAVE_ARCH_THREAD_STRUCT_WHITELIST
        bool
        select SPARSEMEM_STATIC if SPARSEMEM
 
-config HAVE_ARCH_PFN_VALID
-       def_bool y
-
 config HIGHMEM
        bool "High Memory Support"
        depends on MMU
 
                *p++ = 0xe7fddef0;
 }
 
-static inline void __init
-free_memmap(unsigned long start_pfn, unsigned long end_pfn)
-{
-       struct page *start_pg, *end_pg;
-       phys_addr_t pg, pgend;
-
-       /*
-        * Convert start_pfn/end_pfn to a struct page pointer.
-        */
-       start_pg = pfn_to_page(start_pfn - 1) + 1;
-       end_pg = pfn_to_page(end_pfn - 1) + 1;
-
-       /*
-        * Convert to physical addresses, and
-        * round start upwards and end downwards.
-        */
-       pg = PAGE_ALIGN(__pa(start_pg));
-       pgend = __pa(end_pg) & PAGE_MASK;
-
-       /*
-        * If there are free pages between these,
-        * free the section of the memmap array.
-        */
-       if (pg < pgend)
-               memblock_free_early(pg, pgend - pg);
-}
-
-/*
- * The mem_map array can get very big.  Free the unused area of the memory map.
- */
-static void __init free_unused_memmap(void)
-{
-       unsigned long start, end, prev_end = 0;
-       int i;
-
-       /*
-        * This relies on each bank being in address order.
-        * The banks are sorted previously in bootmem_init().
-        */
-       for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
-#ifdef CONFIG_SPARSEMEM
-               /*
-                * Take care not to free memmap entries that don't exist
-                * due to SPARSEMEM sections which aren't present.
-                */
-               start = min(start,
-                                ALIGN(prev_end, PAGES_PER_SECTION));
-#else
-               /*
-                * Align down here since the VM subsystem insists that the
-                * memmap entries are valid from the bank start aligned to
-                * MAX_ORDER_NR_PAGES.
-                */
-               start = round_down(start, MAX_ORDER_NR_PAGES);
-#endif
-               /*
-                * If we had a previous bank, and there is a space
-                * between the current bank and the previous, free it.
-                */
-               if (prev_end && prev_end < start)
-                       free_memmap(prev_end, start);
-
-               /*
-                * Align up here since the VM subsystem insists that the
-                * memmap entries are valid from the bank end aligned to
-                * MAX_ORDER_NR_PAGES.
-                */
-               prev_end = ALIGN(end, MAX_ORDER_NR_PAGES);
-       }
-
-#ifdef CONFIG_SPARSEMEM
-       if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
-               free_memmap(prev_end,
-                           ALIGN(prev_end, PAGES_PER_SECTION));
-#endif
-}
-
 static void __init free_highpages(void)
 {
 #ifdef CONFIG_HIGHMEM
        set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
 
        /* this will put all unused low memory onto the freelists */
-       free_unused_memmap();
        memblock_free_all();
 
 #ifdef CONFIG_SA1111
 
        select HAVE_ARCH_KGDB
        select HAVE_ARCH_MMAP_RND_BITS
        select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT
+       select HAVE_ARCH_PFN_VALID
        select HAVE_ARCH_PREL32_RELOCATIONS
        select HAVE_ARCH_SECCOMP_FILTER
        select HAVE_ARCH_STACKLEAK
 config ARCH_FLATMEM_ENABLE
        def_bool !NUMA
 
-config HAVE_ARCH_PFN_VALID
-       def_bool y
-
 config HW_PERF_EVENTS
        def_bool y
        depends on ARM_PMU
 
        memblock_dump_all();
 }
 
-#ifndef CONFIG_SPARSEMEM_VMEMMAP
-static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn)
-{
-       struct page *start_pg, *end_pg;
-       unsigned long pg, pgend;
-
-       /*
-        * Convert start_pfn/end_pfn to a struct page pointer.
-        */
-       start_pg = pfn_to_page(start_pfn - 1) + 1;
-       end_pg = pfn_to_page(end_pfn - 1) + 1;
-
-       /*
-        * Convert to physical addresses, and round start upwards and end
-        * downwards.
-        */
-       pg = (unsigned long)PAGE_ALIGN(__pa(start_pg));
-       pgend = (unsigned long)__pa(end_pg) & PAGE_MASK;
-
-       /*
-        * If there are free pages between these, free the section of the
-        * memmap array.
-        */
-       if (pg < pgend)
-               memblock_free(pg, pgend - pg);
-}
-
-/*
- * The mem_map array can get very big. Free the unused area of the memory map.
- */
-static void __init free_unused_memmap(void)
-{
-       unsigned long start, end, prev_end = 0;
-       int i;
-
-       for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
-#ifdef CONFIG_SPARSEMEM
-               /*
-                * Take care not to free memmap entries that don't exist due
-                * to SPARSEMEM sections which aren't present.
-                */
-               start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
-#endif
-               /*
-                * If we had a previous bank, and there is a space between the
-                * current bank and the previous, free it.
-                */
-               if (prev_end && prev_end < start)
-                       free_memmap(prev_end, start);
-
-               /*
-                * Align up here since the VM subsystem insists that the
-                * memmap entries are valid from the bank end aligned to
-                * MAX_ORDER_NR_PAGES.
-                */
-               prev_end = ALIGN(end, MAX_ORDER_NR_PAGES);
-       }
-
-#ifdef CONFIG_SPARSEMEM
-       if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
-               free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
-#endif
-}
-#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
-
 /*
  * mem_init() marks the free areas in the mem_map and tells us how much memory
  * is free.  This is done after various parts of the system have claimed their
 
        set_max_mapnr(max_pfn - PHYS_PFN_OFFSET);
 
-#ifndef CONFIG_SPARSEMEM_VMEMMAP
-       free_unused_memmap();
-#endif
        /* this will put all unused low memory onto the freelists */
        memblock_free_all();
 
 
 }
 early_param("memblock", early_memblock);
 
+static void __init free_memmap(unsigned long start_pfn, unsigned long end_pfn)
+{
+       struct page *start_pg, *end_pg;
+       phys_addr_t pg, pgend;
+
+       /*
+        * Convert start_pfn/end_pfn to a struct page pointer.
+        */
+       start_pg = pfn_to_page(start_pfn - 1) + 1;
+       end_pg = pfn_to_page(end_pfn - 1) + 1;
+
+       /*
+        * Convert to physical addresses, and round start upwards and end
+        * downwards.
+        */
+       pg = PAGE_ALIGN(__pa(start_pg));
+       pgend = __pa(end_pg) & PAGE_MASK;
+
+       /*
+        * If there are free pages between these, free the section of the
+        * memmap array.
+        */
+       if (pg < pgend)
+               memblock_free(pg, pgend - pg);
+}
+
+/*
+ * The mem_map array can get very big.  Free the unused area of the memory map.
+ */
+static void __init free_unused_memmap(void)
+{
+       unsigned long start, end, prev_end = 0;
+       int i;
+
+       if (!IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) ||
+           IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
+               return;
+
+       /*
+        * This relies on each bank being in address order.
+        * The banks are sorted previously in bootmem_init().
+        */
+       for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
+#ifdef CONFIG_SPARSEMEM
+               /*
+                * Take care not to free memmap entries that don't exist
+                * due to SPARSEMEM sections which aren't present.
+                */
+               start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
+#else
+               /*
+                * Align down here since the VM subsystem insists that the
+                * memmap entries are valid from the bank start aligned to
+                * MAX_ORDER_NR_PAGES.
+                */
+               start = round_down(start, MAX_ORDER_NR_PAGES);
+#endif
+
+               /*
+                * If we had a previous bank, and there is a space
+                * between the current bank and the previous, free it.
+                */
+               if (prev_end && prev_end < start)
+                       free_memmap(prev_end, start);
+
+               /*
+                * Align up here since the VM subsystem insists that the
+                * memmap entries are valid from the bank end aligned to
+                * MAX_ORDER_NR_PAGES.
+                */
+               prev_end = ALIGN(end, MAX_ORDER_NR_PAGES);
+       }
+
+#ifdef CONFIG_SPARSEMEM
+       if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
+               free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
+#endif
+}
+
 static void __init __free_pages_memory(unsigned long start, unsigned long end)
 {
        int order;
 {
        unsigned long pages;
 
+       free_unused_memmap();
        reset_all_zones_managed_pages();
 
        pages = free_low_memory_core_early();