^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * arch/xtensa/mm/init.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Derived from MIPS, PPC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * License. See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2001 - 2005 Tensilica Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (C) 2014 - 2016 Cadence Design Systems Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Chris Zankel <chris@zankel.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Marc Gauthier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Kevin Chea
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/nodemask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/of_fdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/dma-map-ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <asm/bootparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <asm/sysmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * Initialize the bootmem system and give it all low memory we have available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void __init bootmem_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Reserve all memory below PHYS_OFFSET, as memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * accounting doesn't work for pages below that address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * If PHYS_OFFSET is zero reserve page at address 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * successfull allocations should never return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) memblock_reserve(0, PHYS_OFFSET ? PHYS_OFFSET : 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) early_init_fdt_scan_reserved_mem();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!memblock_phys_mem_size())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) panic("No memory found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) min_low_pfn = PFN_UP(memblock_start_of_DRAM());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) min_low_pfn = max(min_low_pfn, PFN_UP(PHYS_OFFSET));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) max_pfn = PFN_DOWN(memblock_end_of_DRAM());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) max_low_pfn = min(max_pfn, MAX_LOW_PFN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) (phys_addr_t)max_low_pfn << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) memblock_set_current_limit(PFN_PHYS(max_low_pfn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) memblock_dump_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) void __init zones_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* All pages are DMA-able, so we put them all in the DMA zone. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned long max_zone_pfn[MAX_NR_ZONES] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) [ZONE_NORMAL] = max_low_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #ifdef CONFIG_HIGHMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) [ZONE_HIGHMEM] = max_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) free_area_init(max_zone_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static void __init free_highpages(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #ifdef CONFIG_HIGHMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) unsigned long max_low = max_low_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) phys_addr_t range_start, range_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u64 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* set highmem page free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) &range_start, &range_end, NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) unsigned long start = PFN_UP(range_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned long end = PFN_DOWN(range_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* Ignore complete lowmem entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (end <= max_low)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* Truncate partial highmem entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (start < max_low)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) start = max_low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) for (; start < end; start++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) free_highmem_page(pfn_to_page(start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * Initialize memory pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) void __init mem_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) free_highpages();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) max_mapnr = max_pfn - ARCH_PFN_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) memblock_free_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) mem_init_print_info(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) pr_info("virtual kernel memory layout:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #ifdef CONFIG_KASAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) " kasan : 0x%08lx - 0x%08lx (%5lu MB)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) " vmalloc : 0x%08lx - 0x%08lx (%5lu MB)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #ifdef CONFIG_HIGHMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) " pkmap : 0x%08lx - 0x%08lx (%5lu kB)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) " fixmap : 0x%08lx - 0x%08lx (%5lu kB)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) " lowmem : 0x%08lx - 0x%08lx (%5lu MB)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) " .text : 0x%08lx - 0x%08lx (%5lu kB)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) " .rodata : 0x%08lx - 0x%08lx (%5lu kB)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) " .data : 0x%08lx - 0x%08lx (%5lu kB)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) " .init : 0x%08lx - 0x%08lx (%5lu kB)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) " .bss : 0x%08lx - 0x%08lx (%5lu kB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #ifdef CONFIG_KASAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) KASAN_SHADOW_START, KASAN_SHADOW_START + KASAN_SHADOW_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) KASAN_SHADOW_SIZE >> 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) VMALLOC_START, VMALLOC_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) (VMALLOC_END - VMALLOC_START) >> 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #ifdef CONFIG_HIGHMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) (LAST_PKMAP*PAGE_SIZE) >> 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) FIXADDR_START, FIXADDR_TOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) (FIXADDR_TOP - FIXADDR_START) >> 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) PAGE_OFFSET, PAGE_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) (max_low_pfn - min_low_pfn) * PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) min_low_pfn * PAGE_SIZE, max_low_pfn * PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ((max_low_pfn - min_low_pfn) * PAGE_SIZE) >> 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) (unsigned long)_text, (unsigned long)_etext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) (unsigned long)(_etext - _text) >> 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) (unsigned long)__start_rodata, (unsigned long)__end_rodata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) (unsigned long)(__end_rodata - __start_rodata) >> 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) (unsigned long)_sdata, (unsigned long)_edata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) (unsigned long)(_edata - _sdata) >> 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) (unsigned long)__init_begin, (unsigned long)__init_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) (unsigned long)(__init_end - __init_begin) >> 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) (unsigned long)__bss_start, (unsigned long)__bss_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) (unsigned long)(__bss_stop - __bss_start) >> 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void __init parse_memmap_one(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) char *oldp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) unsigned long start_at, mem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) oldp = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) mem_size = memparse(p, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (p == oldp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) switch (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) case '@':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) start_at = memparse(p + 1, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) memblock_add(start_at, mem_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) case '$':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) start_at = memparse(p + 1, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) memblock_reserve(start_at, mem_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) memblock_reserve(mem_size, -mem_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) pr_warn("Unrecognized memmap syntax: %s\n", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int __init parse_memmap_opt(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) while (str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) char *k = strchr(str, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) *k++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) parse_memmap_one(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) str = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) early_param("memmap", parse_memmap_opt);