Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/xtensa/mm/init.c |
| 3 | * |
| 4 | * Derived from MIPS, PPC. |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | * |
| 10 | * Copyright (C) 2001 - 2005 Tensilica Inc. |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 11 | * Copyright (C) 2014 - 2016 Cadence Design Systems Inc. |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 12 | * |
| 13 | * Chris Zankel <chris@zankel.net> |
| 14 | * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com> |
| 15 | * Marc Gauthier |
| 16 | * Kevin Chea |
| 17 | */ |
| 18 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | #include <linux/errno.h> |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 21 | #include <linux/bootmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/gfp.h> |
Max Filippov | 6555910 | 2014-02-04 02:17:09 +0400 | [diff] [blame] | 23 | #include <linux/highmem.h> |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 24 | #include <linux/swap.h> |
Chris Zankel | 6656920 | 2007-08-22 10:14:51 -0700 | [diff] [blame] | 25 | #include <linux/mman.h> |
| 26 | #include <linux/nodemask.h> |
| 27 | #include <linux/mm.h> |
Max Filippov | 4e7c84e | 2016-07-19 00:37:05 +0300 | [diff] [blame] | 28 | #include <linux/of_fdt.h> |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 29 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 30 | #include <asm/bootparam.h> |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 31 | #include <asm/page.h> |
Geert Uytterhoeven | f022d0f | 2012-06-20 12:52:59 -0700 | [diff] [blame] | 32 | #include <asm/sections.h> |
Max Filippov | 9ba067f | 2014-03-23 03:17:43 +0400 | [diff] [blame] | 33 | #include <asm/sysmem.h> |
| 34 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 35 | /* |
Max Filippov | e9d6dca | 2014-01-31 05:38:21 +0400 | [diff] [blame] | 36 | * Initialize the bootmem system and give it all low memory we have available. |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | void __init bootmem_init(void) |
| 40 | { |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 41 | /* Reserve all memory below PHYS_OFFSET, as memory |
Max Filippov | 566fb58 | 2014-10-04 03:46:34 +0400 | [diff] [blame] | 42 | * accounting doesn't work for pages below that address. |
| 43 | * |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 44 | * If PHYS_OFFSET is zero reserve page at address 0: |
Max Filippov | 566fb58 | 2014-10-04 03:46:34 +0400 | [diff] [blame] | 45 | * successfull allocations should never return NULL. |
| 46 | */ |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 47 | if (PHYS_OFFSET) |
| 48 | memblock_reserve(0, PHYS_OFFSET); |
Max Filippov | 566fb58 | 2014-10-04 03:46:34 +0400 | [diff] [blame] | 49 | else |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 50 | memblock_reserve(0, 1); |
Max Filippov | 566fb58 | 2014-10-04 03:46:34 +0400 | [diff] [blame] | 51 | |
Max Filippov | 4e7c84e | 2016-07-19 00:37:05 +0300 | [diff] [blame] | 52 | early_init_fdt_scan_reserved_mem(); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 53 | |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 54 | if (!memblock_phys_mem_size()) |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 55 | panic("No memory found!\n"); |
| 56 | |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 57 | min_low_pfn = PFN_UP(memblock_start_of_DRAM()); |
| 58 | min_low_pfn = max(min_low_pfn, PFN_UP(PHYS_OFFSET)); |
| 59 | max_pfn = PFN_DOWN(memblock_end_of_DRAM()); |
Max Filippov | d39af90 | 2016-04-11 21:14:17 +0300 | [diff] [blame] | 60 | max_low_pfn = min(max_pfn, MAX_LOW_PFN); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 61 | |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 62 | memblock_set_current_limit(PFN_PHYS(max_low_pfn)); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 63 | |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 64 | memblock_dump_all(); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
Johannes Weiner | e5083a6 | 2009-03-04 16:21:31 +0100 | [diff] [blame] | 68 | void __init zones_init(void) |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 69 | { |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 70 | /* All pages are DMA-able, so we put them all in the DMA zone. */ |
Max Filippov | 6555910 | 2014-02-04 02:17:09 +0400 | [diff] [blame] | 71 | unsigned long zones_size[MAX_NR_ZONES] = { |
| 72 | [ZONE_DMA] = max_low_pfn - ARCH_PFN_OFFSET, |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 73 | #ifdef CONFIG_HIGHMEM |
Max Filippov | 6555910 | 2014-02-04 02:17:09 +0400 | [diff] [blame] | 74 | [ZONE_HIGHMEM] = max_pfn - max_low_pfn, |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 75 | #endif |
Max Filippov | 6555910 | 2014-02-04 02:17:09 +0400 | [diff] [blame] | 76 | }; |
Johannes Weiner | c947a58 | 2009-03-04 16:21:30 +0100 | [diff] [blame] | 77 | free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Max Filippov | a5ecf56 | 2018-02-13 15:31:05 -0800 | [diff] [blame] | 80 | #ifdef CONFIG_HIGHMEM |
| 81 | static void __init free_area_high(unsigned long pfn, unsigned long end) |
| 82 | { |
| 83 | for (; pfn < end; pfn++) |
| 84 | free_highmem_page(pfn_to_page(pfn)); |
| 85 | } |
| 86 | |
| 87 | static void __init free_highpages(void) |
| 88 | { |
| 89 | unsigned long max_low = max_low_pfn; |
| 90 | struct memblock_region *mem, *res; |
| 91 | |
| 92 | reset_all_zones_managed_pages(); |
| 93 | /* set highmem page free */ |
| 94 | for_each_memblock(memory, mem) { |
| 95 | unsigned long start = memblock_region_memory_base_pfn(mem); |
| 96 | unsigned long end = memblock_region_memory_end_pfn(mem); |
| 97 | |
| 98 | /* Ignore complete lowmem entries */ |
| 99 | if (end <= max_low) |
| 100 | continue; |
| 101 | |
| 102 | if (memblock_is_nomap(mem)) |
| 103 | continue; |
| 104 | |
| 105 | /* Truncate partial highmem entries */ |
| 106 | if (start < max_low) |
| 107 | start = max_low; |
| 108 | |
| 109 | /* Find and exclude any reserved regions */ |
| 110 | for_each_memblock(reserved, res) { |
| 111 | unsigned long res_start, res_end; |
| 112 | |
| 113 | res_start = memblock_region_reserved_base_pfn(res); |
| 114 | res_end = memblock_region_reserved_end_pfn(res); |
| 115 | |
| 116 | if (res_end < start) |
| 117 | continue; |
| 118 | if (res_start < start) |
| 119 | res_start = start; |
| 120 | if (res_start > end) |
| 121 | res_start = end; |
| 122 | if (res_end > end) |
| 123 | res_end = end; |
| 124 | if (res_start != start) |
| 125 | free_area_high(start, res_start); |
| 126 | start = res_end; |
| 127 | if (start == end) |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | /* And now free anything which remains */ |
| 132 | if (start < end) |
| 133 | free_area_high(start, end); |
| 134 | } |
| 135 | } |
| 136 | #else |
| 137 | static void __init free_highpages(void) |
| 138 | { |
| 139 | } |
| 140 | #endif |
| 141 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 142 | /* |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 143 | * Initialize memory pages. |
| 144 | */ |
| 145 | |
| 146 | void __init mem_init(void) |
| 147 | { |
Max Filippov | a5ecf56 | 2018-02-13 15:31:05 -0800 | [diff] [blame] | 148 | free_highpages(); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 149 | |
Max Filippov | 6555910 | 2014-02-04 02:17:09 +0400 | [diff] [blame] | 150 | max_mapnr = max_pfn - ARCH_PFN_OFFSET; |
| 151 | high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT); |
| 152 | |
Jiang Liu | 0c98853 | 2013-07-03 15:03:24 -0700 | [diff] [blame] | 153 | free_all_bootmem(); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 154 | |
Jiang Liu | 808c2c3 | 2013-07-03 15:04:20 -0700 | [diff] [blame] | 155 | mem_init_print_info(NULL); |
Max Filippov | 6555910 | 2014-02-04 02:17:09 +0400 | [diff] [blame] | 156 | pr_info("virtual kernel memory layout:\n" |
| 157 | #ifdef CONFIG_HIGHMEM |
| 158 | " pkmap : 0x%08lx - 0x%08lx (%5lu kB)\n" |
| 159 | " fixmap : 0x%08lx - 0x%08lx (%5lu kB)\n" |
| 160 | #endif |
Max Filippov | 5a0b1d7 | 2014-10-04 03:50:33 +0400 | [diff] [blame] | 161 | #ifdef CONFIG_MMU |
Max Filippov | f1883aa | 2016-04-11 21:07:30 +0300 | [diff] [blame] | 162 | " vmalloc : 0x%08lx - 0x%08lx (%5lu MB)\n" |
Max Filippov | 5a0b1d7 | 2014-10-04 03:50:33 +0400 | [diff] [blame] | 163 | #endif |
| 164 | " lowmem : 0x%08lx - 0x%08lx (%5lu MB)\n", |
Max Filippov | 6555910 | 2014-02-04 02:17:09 +0400 | [diff] [blame] | 165 | #ifdef CONFIG_HIGHMEM |
| 166 | PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE, |
| 167 | (LAST_PKMAP*PAGE_SIZE) >> 10, |
| 168 | FIXADDR_START, FIXADDR_TOP, |
| 169 | (FIXADDR_TOP - FIXADDR_START) >> 10, |
| 170 | #endif |
Max Filippov | 5a0b1d7 | 2014-10-04 03:50:33 +0400 | [diff] [blame] | 171 | #ifdef CONFIG_MMU |
Max Filippov | 6555910 | 2014-02-04 02:17:09 +0400 | [diff] [blame] | 172 | VMALLOC_START, VMALLOC_END, |
| 173 | (VMALLOC_END - VMALLOC_START) >> 20, |
| 174 | PAGE_OFFSET, PAGE_OFFSET + |
| 175 | (max_low_pfn - min_low_pfn) * PAGE_SIZE, |
Max Filippov | 5a0b1d7 | 2014-10-04 03:50:33 +0400 | [diff] [blame] | 176 | #else |
| 177 | min_low_pfn * PAGE_SIZE, max_low_pfn * PAGE_SIZE, |
| 178 | #endif |
Max Filippov | 6555910 | 2014-02-04 02:17:09 +0400 | [diff] [blame] | 179 | ((max_low_pfn - min_low_pfn) * PAGE_SIZE) >> 20); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 182 | #ifdef CONFIG_BLK_DEV_INITRD |
| 183 | extern int initrd_is_mapped; |
| 184 | |
| 185 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 186 | { |
Jiang Liu | 7acb2c2 | 2013-04-29 15:06:55 -0700 | [diff] [blame] | 187 | if (initrd_is_mapped) |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 188 | free_reserved_area((void *)start, (void *)end, -1, "initrd"); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 189 | } |
| 190 | #endif |
| 191 | |
| 192 | void free_initmem(void) |
| 193 | { |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 194 | free_initmem_default(-1); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 195 | } |
Max Filippov | 06bd282 | 2014-03-21 21:04:40 +0400 | [diff] [blame] | 196 | |
| 197 | static void __init parse_memmap_one(char *p) |
| 198 | { |
| 199 | char *oldp; |
| 200 | unsigned long start_at, mem_size; |
| 201 | |
| 202 | if (!p) |
| 203 | return; |
| 204 | |
| 205 | oldp = p; |
| 206 | mem_size = memparse(p, &p); |
| 207 | if (p == oldp) |
| 208 | return; |
| 209 | |
| 210 | switch (*p) { |
| 211 | case '@': |
| 212 | start_at = memparse(p + 1, &p); |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 213 | memblock_add(start_at, mem_size); |
Max Filippov | 06bd282 | 2014-03-21 21:04:40 +0400 | [diff] [blame] | 214 | break; |
| 215 | |
| 216 | case '$': |
| 217 | start_at = memparse(p + 1, &p); |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 218 | memblock_reserve(start_at, mem_size); |
Max Filippov | 06bd282 | 2014-03-21 21:04:40 +0400 | [diff] [blame] | 219 | break; |
| 220 | |
| 221 | case 0: |
Max Filippov | 0e46c11 | 2016-04-25 22:08:20 +0300 | [diff] [blame] | 222 | memblock_reserve(mem_size, -mem_size); |
Max Filippov | 06bd282 | 2014-03-21 21:04:40 +0400 | [diff] [blame] | 223 | break; |
| 224 | |
| 225 | default: |
| 226 | pr_warn("Unrecognized memmap syntax: %s\n", p); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | static int __init parse_memmap_opt(char *str) |
| 232 | { |
| 233 | while (str) { |
| 234 | char *k = strchr(str, ','); |
| 235 | |
| 236 | if (k) |
| 237 | *k++ = 0; |
| 238 | |
| 239 | parse_memmap_one(str); |
| 240 | str = k; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | early_param("memmap", parse_memmap_opt); |