Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Procedures for maintaining information about logical memory blocks. |
| 3 | * |
| 4 | * Peter Bergner, IBM Corp. June 2001. |
| 5 | * Copyright (C) 2001 Peter Bergner. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/bitops.h> |
Benjamin Herrenschmidt | 449e8df | 2010-07-06 15:39:07 -0700 | [diff] [blame] | 17 | #include <linux/poison.h> |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame] | 18 | #include <linux/pfn.h> |
Benjamin Herrenschmidt | 6d03b88 | 2010-07-06 15:39:19 -0700 | [diff] [blame] | 19 | #include <linux/debugfs.h> |
| 20 | #include <linux/seq_file.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 21 | #include <linux/memblock.h> |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 22 | #include <linux/preempt.h> |
| 23 | #include <linux/seqlock.h> |
Shiraz Hashim | 3eaf6e6 | 2016-04-16 11:43:02 +0530 | [diff] [blame] | 24 | #include <linux/irqflags.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 25 | |
Christoph Hellwig | c4c5ad6 | 2016-07-28 15:48:06 -0700 | [diff] [blame] | 26 | #include <asm/sections.h> |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 27 | #include <linux/io.h> |
| 28 | |
| 29 | #include "internal.h" |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 30 | |
Tejun Heo | fe091c2 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 31 | static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; |
| 32 | static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; |
Philipp Hachtmann | 70210ed | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 33 | #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
| 34 | static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock; |
| 35 | #endif |
Tejun Heo | fe091c2 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 36 | |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 37 | static seqcount_t memblock_seq; |
Tejun Heo | fe091c2 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 38 | struct memblock memblock __initdata_memblock = { |
| 39 | .memory.regions = memblock_memory_init_regions, |
| 40 | .memory.cnt = 1, /* empty dummy entry */ |
| 41 | .memory.max = INIT_MEMBLOCK_REGIONS, |
| 42 | |
| 43 | .reserved.regions = memblock_reserved_init_regions, |
| 44 | .reserved.cnt = 1, /* empty dummy entry */ |
| 45 | .reserved.max = INIT_MEMBLOCK_REGIONS, |
| 46 | |
Philipp Hachtmann | 70210ed | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 47 | #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
| 48 | .physmem.regions = memblock_physmem_init_regions, |
| 49 | .physmem.cnt = 1, /* empty dummy entry */ |
| 50 | .physmem.max = INIT_PHYSMEM_REGIONS, |
| 51 | #endif |
| 52 | |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 53 | .bottom_up = false, |
Tejun Heo | fe091c2 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 54 | .current_limit = MEMBLOCK_ALLOC_ANYWHERE, |
| 55 | }; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 56 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 57 | int memblock_debug __initdata_memblock; |
Tang Chen | 55ac590 | 2014-01-21 15:49:35 -0800 | [diff] [blame] | 58 | #ifdef CONFIG_MOVABLE_NODE |
| 59 | bool movable_node_enabled __initdata_memblock = false; |
| 60 | #endif |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 61 | static bool system_has_some_mirror __initdata_memblock = false; |
Tejun Heo | 1aadc05 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 62 | static int memblock_can_resize __initdata_memblock; |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 63 | static int memblock_memory_in_slab __initdata_memblock = 0; |
| 64 | static int memblock_reserved_in_slab __initdata_memblock = 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 65 | |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 66 | ulong __init_memblock choose_memblock_flags(void) |
| 67 | { |
| 68 | return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE; |
| 69 | } |
| 70 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 71 | /* inline so we don't get a warning when pr_debug is compiled out */ |
Raghavendra D Prabhu | c223311 | 2012-10-08 16:33:55 -0700 | [diff] [blame] | 72 | static __init_memblock const char * |
| 73 | memblock_type_name(struct memblock_type *type) |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 74 | { |
| 75 | if (type == &memblock.memory) |
| 76 | return "memory"; |
| 77 | else if (type == &memblock.reserved) |
| 78 | return "reserved"; |
| 79 | else |
| 80 | return "unknown"; |
| 81 | } |
| 82 | |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 83 | /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */ |
| 84 | static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size) |
| 85 | { |
| 86 | return *size = min(*size, (phys_addr_t)ULLONG_MAX - base); |
| 87 | } |
| 88 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 89 | /* |
| 90 | * Address comparison utilities |
| 91 | */ |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 92 | static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 93 | phys_addr_t base2, phys_addr_t size2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 94 | { |
| 95 | return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); |
| 96 | } |
| 97 | |
Tang Chen | 95cf82e | 2015-09-08 15:02:03 -0700 | [diff] [blame] | 98 | bool __init_memblock memblock_overlaps_region(struct memblock_type *type, |
H Hartley Sweeten | 2d7d3eb | 2011-10-31 17:09:15 -0700 | [diff] [blame] | 99 | phys_addr_t base, phys_addr_t size) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 100 | { |
| 101 | unsigned long i; |
| 102 | |
Alexander Kuleshov | f14516f | 2016-01-14 15:20:39 -0800 | [diff] [blame] | 103 | for (i = 0; i < type->cnt; i++) |
| 104 | if (memblock_addrs_overlap(base, size, type->regions[i].base, |
| 105 | type->regions[i].size)) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 106 | break; |
Tang Chen | c5c5c9d | 2015-09-08 15:02:00 -0700 | [diff] [blame] | 107 | return i < type->cnt; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 108 | } |
| 109 | |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 110 | /* |
| 111 | * __memblock_find_range_bottom_up - find free area utility in bottom-up |
| 112 | * @start: start of candidate range |
| 113 | * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} |
| 114 | * @size: size of free area to find |
| 115 | * @align: alignment of free area to find |
Grygorii Strashko | b115423 | 2014-01-21 15:50:16 -0800 | [diff] [blame] | 116 | * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 117 | * @flags: pick from blocks based on memory attributes |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 118 | * |
| 119 | * Utility called from memblock_find_in_range_node(), find free area bottom-up. |
| 120 | * |
| 121 | * RETURNS: |
| 122 | * Found address on success, 0 on failure. |
| 123 | */ |
| 124 | static phys_addr_t __init_memblock |
| 125 | __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 126 | phys_addr_t size, phys_addr_t align, int nid, |
| 127 | ulong flags) |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 128 | { |
| 129 | phys_addr_t this_start, this_end, cand; |
| 130 | u64 i; |
| 131 | |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 132 | for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) { |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 133 | this_start = clamp(this_start, start, end); |
| 134 | this_end = clamp(this_end, start, end); |
| 135 | |
| 136 | cand = round_up(this_start, align); |
| 137 | if (cand < this_end && this_end - cand >= size) |
| 138 | return cand; |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 144 | /** |
Tang Chen | 1402899 | 2013-11-12 15:07:57 -0800 | [diff] [blame] | 145 | * __memblock_find_range_top_down - find free area utility, in top-down |
| 146 | * @start: start of candidate range |
| 147 | * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} |
| 148 | * @size: size of free area to find |
| 149 | * @align: alignment of free area to find |
Grygorii Strashko | b115423 | 2014-01-21 15:50:16 -0800 | [diff] [blame] | 150 | * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 151 | * @flags: pick from blocks based on memory attributes |
Tang Chen | 1402899 | 2013-11-12 15:07:57 -0800 | [diff] [blame] | 152 | * |
| 153 | * Utility called from memblock_find_in_range_node(), find free area top-down. |
| 154 | * |
| 155 | * RETURNS: |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 156 | * Found address on success, 0 on failure. |
Tang Chen | 1402899 | 2013-11-12 15:07:57 -0800 | [diff] [blame] | 157 | */ |
| 158 | static phys_addr_t __init_memblock |
| 159 | __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 160 | phys_addr_t size, phys_addr_t align, int nid, |
| 161 | ulong flags) |
Tang Chen | 1402899 | 2013-11-12 15:07:57 -0800 | [diff] [blame] | 162 | { |
| 163 | phys_addr_t this_start, this_end, cand; |
| 164 | u64 i; |
| 165 | |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 166 | for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end, |
| 167 | NULL) { |
Tang Chen | 1402899 | 2013-11-12 15:07:57 -0800 | [diff] [blame] | 168 | this_start = clamp(this_start, start, end); |
| 169 | this_end = clamp(this_end, start, end); |
| 170 | |
| 171 | if (this_end < size) |
| 172 | continue; |
| 173 | |
| 174 | cand = round_down(this_end - size, align); |
| 175 | if (cand >= this_start) |
| 176 | return cand; |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /** |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 183 | * memblock_find_in_range_node - find free area in given range and node |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 184 | * @size: size of free area to find |
| 185 | * @align: alignment of free area to find |
Grygorii Strashko | 87029ee | 2014-01-21 15:50:14 -0800 | [diff] [blame] | 186 | * @start: start of candidate range |
| 187 | * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} |
Grygorii Strashko | b115423 | 2014-01-21 15:50:16 -0800 | [diff] [blame] | 188 | * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 189 | * @flags: pick from blocks based on memory attributes |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 190 | * |
| 191 | * Find @size free area aligned to @align in the specified range and node. |
| 192 | * |
| 193 | * RETURNS: |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 194 | * Found address on success, 0 on failure. |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 195 | */ |
Grygorii Strashko | 87029ee | 2014-01-21 15:50:14 -0800 | [diff] [blame] | 196 | phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size, |
| 197 | phys_addr_t align, phys_addr_t start, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 198 | phys_addr_t end, int nid, ulong flags) |
Tang Chen | f7210e6 | 2013-02-22 16:33:51 -0800 | [diff] [blame] | 199 | { |
Tang Chen | f7210e6 | 2013-02-22 16:33:51 -0800 | [diff] [blame] | 200 | /* pump up @end */ |
| 201 | if (end == MEMBLOCK_ALLOC_ACCESSIBLE) |
| 202 | end = memblock.current_limit; |
| 203 | |
| 204 | /* avoid allocating the first page */ |
| 205 | start = max_t(phys_addr_t, start, PAGE_SIZE); |
| 206 | end = max(start, end); |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 207 | |
Roman Gushchin | 3fcc1c3 | 2021-02-04 18:32:36 -0800 | [diff] [blame] | 208 | if (memblock_bottom_up()) |
| 209 | return __memblock_find_range_bottom_up(start, end, size, align, |
| 210 | nid, flags); |
| 211 | else |
| 212 | return __memblock_find_range_top_down(start, end, size, align, |
| 213 | nid, flags); |
Tang Chen | f7210e6 | 2013-02-22 16:33:51 -0800 | [diff] [blame] | 214 | } |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 215 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 216 | /** |
| 217 | * memblock_find_in_range - find free area in given range |
| 218 | * @start: start of candidate range |
| 219 | * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} |
| 220 | * @size: size of free area to find |
| 221 | * @align: alignment of free area to find |
| 222 | * |
| 223 | * Find @size free area aligned to @align in the specified range. |
| 224 | * |
| 225 | * RETURNS: |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 226 | * Found address on success, 0 on failure. |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 227 | */ |
| 228 | phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, |
| 229 | phys_addr_t end, phys_addr_t size, |
| 230 | phys_addr_t align) |
| 231 | { |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 232 | phys_addr_t ret; |
| 233 | ulong flags = choose_memblock_flags(); |
| 234 | |
| 235 | again: |
| 236 | ret = memblock_find_in_range_node(size, align, start, end, |
| 237 | NUMA_NO_NODE, flags); |
| 238 | |
| 239 | if (!ret && (flags & MEMBLOCK_MIRROR)) { |
| 240 | pr_warn("Could not allocate %pap bytes of mirrored memory\n", |
| 241 | &size); |
| 242 | flags &= ~MEMBLOCK_MIRROR; |
| 243 | goto again; |
| 244 | } |
| 245 | |
| 246 | return ret; |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 249 | static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 250 | { |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 251 | type->total_size -= type->regions[r].size; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 252 | memmove(&type->regions[r], &type->regions[r + 1], |
| 253 | (type->cnt - (r + 1)) * sizeof(type->regions[r])); |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 254 | type->cnt--; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 255 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 256 | /* Special case for empty arrays */ |
| 257 | if (type->cnt == 0) { |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 258 | WARN_ON(type->total_size != 0); |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 259 | type->cnt = 1; |
| 260 | type->regions[0].base = 0; |
| 261 | type->regions[0].size = 0; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 262 | type->regions[0].flags = 0; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 263 | memblock_set_region_node(&type->regions[0], MAX_NUMNODES); |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 264 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 265 | } |
| 266 | |
Philipp Hachtmann | 354f17e | 2014-01-23 15:53:24 -0800 | [diff] [blame] | 267 | #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK |
Pavel Tatashin | 87395ee | 2017-08-18 15:16:05 -0700 | [diff] [blame] | 268 | /** |
| 269 | * Discard memory and reserved arrays if they were allocated |
| 270 | */ |
| 271 | void __init memblock_discard(void) |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 272 | { |
Pavel Tatashin | 87395ee | 2017-08-18 15:16:05 -0700 | [diff] [blame] | 273 | phys_addr_t addr, size; |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 274 | |
Pavel Tatashin | 87395ee | 2017-08-18 15:16:05 -0700 | [diff] [blame] | 275 | if (memblock.reserved.regions != memblock_reserved_init_regions) { |
| 276 | addr = __pa(memblock.reserved.regions); |
| 277 | size = PAGE_ALIGN(sizeof(struct memblock_region) * |
| 278 | memblock.reserved.max); |
Miaohe Lin | 670d6e5 | 2022-02-17 22:53:27 +0800 | [diff] [blame] | 279 | if (memblock_reserved_in_slab) |
| 280 | kfree(memblock.reserved.regions); |
| 281 | else |
| 282 | __memblock_free_late(addr, size); |
Pavel Tatashin | 87395ee | 2017-08-18 15:16:05 -0700 | [diff] [blame] | 283 | } |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 284 | |
Pavel Tatashin | 9d26332 | 2017-08-25 15:55:46 -0700 | [diff] [blame] | 285 | if (memblock.memory.regions != memblock_memory_init_regions) { |
Pavel Tatashin | 87395ee | 2017-08-18 15:16:05 -0700 | [diff] [blame] | 286 | addr = __pa(memblock.memory.regions); |
| 287 | size = PAGE_ALIGN(sizeof(struct memblock_region) * |
| 288 | memblock.memory.max); |
Miaohe Lin | 670d6e5 | 2022-02-17 22:53:27 +0800 | [diff] [blame] | 289 | if (memblock_memory_in_slab) |
| 290 | kfree(memblock.memory.regions); |
| 291 | else |
| 292 | __memblock_free_late(addr, size); |
Pavel Tatashin | 87395ee | 2017-08-18 15:16:05 -0700 | [diff] [blame] | 293 | } |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 294 | } |
Philipp Hachtmann | 5e270e2 | 2014-01-23 15:53:11 -0800 | [diff] [blame] | 295 | #endif |
| 296 | |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 297 | /** |
| 298 | * memblock_double_array - double the size of the memblock regions array |
| 299 | * @type: memblock type of the regions array being doubled |
| 300 | * @new_area_start: starting address of memory range to avoid overlap with |
| 301 | * @new_area_size: size of memory range to avoid overlap with |
| 302 | * |
| 303 | * Double the size of the @type regions array. If memblock is being used to |
| 304 | * allocate memory for a new reserved regions array and there is a previously |
| 305 | * allocated memory range [@new_area_start,@new_area_start+@new_area_size] |
| 306 | * waiting to be reserved, ensure the memory used by the new array does |
| 307 | * not overlap. |
| 308 | * |
| 309 | * RETURNS: |
| 310 | * 0 on success, -1 on failure. |
| 311 | */ |
| 312 | static int __init_memblock memblock_double_array(struct memblock_type *type, |
| 313 | phys_addr_t new_area_start, |
| 314 | phys_addr_t new_area_size) |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 315 | { |
| 316 | struct memblock_region *new_array, *old_array; |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 317 | phys_addr_t old_alloc_size, new_alloc_size; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 318 | phys_addr_t old_size, new_size, addr; |
| 319 | int use_slab = slab_is_available(); |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 320 | int *in_slab; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 321 | |
| 322 | /* We don't allow resizing until we know about the reserved regions |
| 323 | * of memory that aren't suitable for allocation |
| 324 | */ |
| 325 | if (!memblock_can_resize) |
| 326 | return -1; |
| 327 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 328 | /* Calculate new doubled size */ |
| 329 | old_size = type->max * sizeof(struct memblock_region); |
| 330 | new_size = old_size << 1; |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 331 | /* |
| 332 | * We need to allocated new one align to PAGE_SIZE, |
| 333 | * so we can free them completely later. |
| 334 | */ |
| 335 | old_alloc_size = PAGE_ALIGN(old_size); |
| 336 | new_alloc_size = PAGE_ALIGN(new_size); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 337 | |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 338 | /* Retrieve the slab flag */ |
| 339 | if (type == &memblock.memory) |
| 340 | in_slab = &memblock_memory_in_slab; |
| 341 | else |
| 342 | in_slab = &memblock_reserved_in_slab; |
| 343 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 344 | /* Try to find some space for it. |
| 345 | * |
| 346 | * WARNING: We assume that either slab_is_available() and we use it or |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 347 | * we use MEMBLOCK for allocations. That means that this is unsafe to |
| 348 | * use when bootmem is currently active (unless bootmem itself is |
| 349 | * implemented on top of MEMBLOCK which isn't the case yet) |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 350 | * |
| 351 | * This should however not be an issue for now, as we currently only |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 352 | * call into MEMBLOCK while it's still active, or much later when slab |
| 353 | * is active for memory hotplug operations |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 354 | */ |
| 355 | if (use_slab) { |
| 356 | new_array = kmalloc(new_size, GFP_KERNEL); |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 357 | addr = new_array ? __pa(new_array) : 0; |
Gavin Shan | 4e2f077 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 358 | } else { |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 359 | /* only exclude range when trying to double reserved.regions */ |
| 360 | if (type != &memblock.reserved) |
| 361 | new_area_start = new_area_size = 0; |
| 362 | |
| 363 | addr = memblock_find_in_range(new_area_start + new_area_size, |
| 364 | memblock.current_limit, |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 365 | new_alloc_size, PAGE_SIZE); |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 366 | if (!addr && new_area_size) |
| 367 | addr = memblock_find_in_range(0, |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 368 | min(new_area_start, memblock.current_limit), |
| 369 | new_alloc_size, PAGE_SIZE); |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 370 | |
Sachin Kamat | 1567486 | 2012-09-04 13:55:05 +0530 | [diff] [blame] | 371 | new_array = addr ? __va(addr) : NULL; |
Gavin Shan | 4e2f077 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 372 | } |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 373 | if (!addr) { |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 374 | pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", |
| 375 | memblock_type_name(type), type->max, type->max * 2); |
| 376 | return -1; |
| 377 | } |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 378 | |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 379 | memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]", |
| 380 | memblock_type_name(type), type->max * 2, (u64)addr, |
| 381 | (u64)addr + new_size - 1); |
Yinghai Lu | ea9e437 | 2010-07-28 15:13:22 +1000 | [diff] [blame] | 382 | |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 383 | /* |
| 384 | * Found space, we now need to move the array over before we add the |
| 385 | * reserved region since it may be our reserved array itself that is |
| 386 | * full. |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 387 | */ |
| 388 | memcpy(new_array, type->regions, old_size); |
| 389 | memset(new_array + type->max, 0, old_size); |
| 390 | old_array = type->regions; |
| 391 | type->regions = new_array; |
| 392 | type->max <<= 1; |
| 393 | |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 394 | /* Free old array. We needn't free it if the array is the static one */ |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 395 | if (*in_slab) |
| 396 | kfree(old_array); |
| 397 | else if (old_array != memblock_memory_init_regions && |
| 398 | old_array != memblock_reserved_init_regions) |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 399 | memblock_free(__pa(old_array), old_alloc_size); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 400 | |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 401 | /* |
| 402 | * Reserve the new array if that comes from the memblock. Otherwise, we |
| 403 | * needn't do it |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 404 | */ |
| 405 | if (!use_slab) |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 406 | BUG_ON(memblock_reserve(addr, new_alloc_size)); |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 407 | |
| 408 | /* Update slab flag */ |
| 409 | *in_slab = use_slab; |
| 410 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 414 | /** |
| 415 | * memblock_merge_regions - merge neighboring compatible regions |
| 416 | * @type: memblock type to scan |
| 417 | * |
| 418 | * Scan @type and merge neighboring compatible regions. |
| 419 | */ |
| 420 | static void __init_memblock memblock_merge_regions(struct memblock_type *type) |
| 421 | { |
| 422 | int i = 0; |
| 423 | |
| 424 | /* cnt never goes below 1 */ |
| 425 | while (i < type->cnt - 1) { |
| 426 | struct memblock_region *this = &type->regions[i]; |
| 427 | struct memblock_region *next = &type->regions[i + 1]; |
| 428 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 429 | if (this->base + this->size != next->base || |
| 430 | memblock_get_region_node(this) != |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 431 | memblock_get_region_node(next) || |
| 432 | this->flags != next->flags) { |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 433 | BUG_ON(this->base + this->size > next->base); |
| 434 | i++; |
| 435 | continue; |
| 436 | } |
| 437 | |
| 438 | this->size += next->size; |
Lin Feng | c0232ae | 2013-01-11 14:31:44 -0800 | [diff] [blame] | 439 | /* move forward from next + 1, index of which is i + 2 */ |
| 440 | memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next)); |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 441 | type->cnt--; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * memblock_insert_region - insert new memblock region |
Tang Chen | 209ff86 | 2013-04-29 15:08:41 -0700 | [diff] [blame] | 447 | * @type: memblock type to insert into |
| 448 | * @idx: index for the insertion point |
| 449 | * @base: base address of the new region |
| 450 | * @size: size of the new region |
| 451 | * @nid: node id of the new region |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 452 | * @flags: flags of the new region |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 453 | * |
| 454 | * Insert new memblock region [@base,@base+@size) into @type at @idx. |
Alexander Kuleshov | 412d000 | 2016-08-04 15:31:46 -0700 | [diff] [blame] | 455 | * @type must already have extra room to accommodate the new region. |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 456 | */ |
| 457 | static void __init_memblock memblock_insert_region(struct memblock_type *type, |
| 458 | int idx, phys_addr_t base, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 459 | phys_addr_t size, |
| 460 | int nid, unsigned long flags) |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 461 | { |
| 462 | struct memblock_region *rgn = &type->regions[idx]; |
| 463 | |
| 464 | BUG_ON(type->cnt >= type->max); |
| 465 | memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); |
| 466 | rgn->base = base; |
| 467 | rgn->size = size; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 468 | rgn->flags = flags; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 469 | memblock_set_region_node(rgn, nid); |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 470 | type->cnt++; |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 471 | type->total_size += size; |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /** |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 475 | * memblock_add_range - add new memblock region |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 476 | * @type: memblock type to add new region into |
| 477 | * @base: base address of the new region |
| 478 | * @size: size of the new region |
Tejun Heo | 7fb0bc3 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 479 | * @nid: nid of the new region |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 480 | * @flags: flags of the new region |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 481 | * |
| 482 | * Add new memblock region [@base,@base+@size) into @type. The new region |
| 483 | * is allowed to overlap with existing ones - overlaps don't affect already |
| 484 | * existing regions. @type is guaranteed to be minimal (all neighbouring |
| 485 | * compatible regions are merged) after the addition. |
| 486 | * |
| 487 | * RETURNS: |
| 488 | * 0 on success, -errno on failure. |
| 489 | */ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 490 | int __init_memblock memblock_add_range(struct memblock_type *type, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 491 | phys_addr_t base, phys_addr_t size, |
| 492 | int nid, unsigned long flags) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 493 | { |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 494 | bool insert = false; |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 495 | phys_addr_t obase = base; |
| 496 | phys_addr_t end = base + memblock_cap_size(base, &size); |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 497 | int idx, nr_new; |
| 498 | struct memblock_region *rgn; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 499 | |
Tejun Heo | b3dc627 | 2012-04-20 08:31:34 -0700 | [diff] [blame] | 500 | if (!size) |
| 501 | return 0; |
| 502 | |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 503 | /* special case for empty array */ |
| 504 | if (type->regions[0].size == 0) { |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 505 | WARN_ON(type->cnt != 1 || type->total_size); |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 506 | type->regions[0].base = base; |
| 507 | type->regions[0].size = size; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 508 | type->regions[0].flags = flags; |
Tejun Heo | 7fb0bc3 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 509 | memblock_set_region_node(&type->regions[0], nid); |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 510 | type->total_size = size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 511 | return 0; |
| 512 | } |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 513 | repeat: |
| 514 | /* |
| 515 | * The following is executed twice. Once with %false @insert and |
| 516 | * then with %true. The first counts the number of regions needed |
Alexander Kuleshov | 412d000 | 2016-08-04 15:31:46 -0700 | [diff] [blame] | 517 | * to accommodate the new area. The second actually inserts them. |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 518 | */ |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 519 | base = obase; |
| 520 | nr_new = 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 521 | |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 522 | for_each_memblock_type(type, rgn) { |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 523 | phys_addr_t rbase = rgn->base; |
| 524 | phys_addr_t rend = rbase + rgn->size; |
| 525 | |
| 526 | if (rbase >= end) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 527 | break; |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 528 | if (rend <= base) |
| 529 | continue; |
| 530 | /* |
| 531 | * @rgn overlaps. If it separates the lower part of new |
| 532 | * area, insert that portion. |
| 533 | */ |
| 534 | if (rbase > base) { |
Wei Yang | c0a2949 | 2015-09-04 15:47:38 -0700 | [diff] [blame] | 535 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 536 | WARN_ON(nid != memblock_get_region_node(rgn)); |
| 537 | #endif |
Wei Yang | 4fcab5f | 2015-09-08 14:59:53 -0700 | [diff] [blame] | 538 | WARN_ON(flags != rgn->flags); |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 539 | nr_new++; |
| 540 | if (insert) |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 541 | memblock_insert_region(type, idx++, base, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 542 | rbase - base, nid, |
| 543 | flags); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 544 | } |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 545 | /* area below @rend is dealt with, forget about it */ |
| 546 | base = min(rend, end); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 547 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 548 | |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 549 | /* insert the remaining portion */ |
| 550 | if (base < end) { |
| 551 | nr_new++; |
| 552 | if (insert) |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 553 | memblock_insert_region(type, idx, base, end - base, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 554 | nid, flags); |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 555 | } |
| 556 | |
nimisolo | ef3cc4d | 2016-07-26 15:24:56 -0700 | [diff] [blame] | 557 | if (!nr_new) |
| 558 | return 0; |
| 559 | |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 560 | /* |
| 561 | * If this was the first round, resize array and repeat for actual |
| 562 | * insertions; otherwise, merge and return. |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 563 | */ |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 564 | if (!insert) { |
| 565 | while (type->cnt + nr_new > type->max) |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 566 | if (memblock_double_array(type, obase, size) < 0) |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 567 | return -ENOMEM; |
| 568 | insert = true; |
| 569 | goto repeat; |
| 570 | } else { |
| 571 | memblock_merge_regions(type); |
| 572 | return 0; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 573 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 574 | } |
| 575 | |
Tejun Heo | 7fb0bc3 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 576 | int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size, |
| 577 | int nid) |
| 578 | { |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 579 | return memblock_add_range(&memblock.memory, base, size, nid, 0); |
Tejun Heo | 7fb0bc3 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Alexander Kuleshov | f705ac4 | 2016-05-20 16:57:35 -0700 | [diff] [blame] | 582 | int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) |
Alexander Kuleshov | 6a4055b | 2015-04-15 16:14:44 -0700 | [diff] [blame] | 583 | { |
Alexander Kuleshov | 6a4055b | 2015-04-15 16:14:44 -0700 | [diff] [blame] | 584 | memblock_dbg("memblock_add: [%#016llx-%#016llx] flags %#02lx %pF\n", |
| 585 | (unsigned long long)base, |
| 586 | (unsigned long long)base + size - 1, |
Alexander Kuleshov | f705ac4 | 2016-05-20 16:57:35 -0700 | [diff] [blame] | 587 | 0UL, (void *)_RET_IP_); |
Alexander Kuleshov | 6a4055b | 2015-04-15 16:14:44 -0700 | [diff] [blame] | 588 | |
Alexander Kuleshov | f705ac4 | 2016-05-20 16:57:35 -0700 | [diff] [blame] | 589 | return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 590 | } |
| 591 | |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 592 | /** |
| 593 | * memblock_isolate_range - isolate given range into disjoint memblocks |
| 594 | * @type: memblock type to isolate range for |
| 595 | * @base: base of range to isolate |
| 596 | * @size: size of range to isolate |
| 597 | * @start_rgn: out parameter for the start of isolated region |
| 598 | * @end_rgn: out parameter for the end of isolated region |
| 599 | * |
| 600 | * Walk @type and ensure that regions don't cross the boundaries defined by |
| 601 | * [@base,@base+@size). Crossing regions are split at the boundaries, |
| 602 | * which may create at most two more regions. The index of the first |
| 603 | * region inside the range is returned in *@start_rgn and end in *@end_rgn. |
| 604 | * |
| 605 | * RETURNS: |
| 606 | * 0 on success, -errno on failure. |
| 607 | */ |
| 608 | static int __init_memblock memblock_isolate_range(struct memblock_type *type, |
| 609 | phys_addr_t base, phys_addr_t size, |
| 610 | int *start_rgn, int *end_rgn) |
| 611 | { |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 612 | phys_addr_t end = base + memblock_cap_size(base, &size); |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 613 | int idx; |
| 614 | struct memblock_region *rgn; |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 615 | |
| 616 | *start_rgn = *end_rgn = 0; |
| 617 | |
Tejun Heo | b3dc627 | 2012-04-20 08:31:34 -0700 | [diff] [blame] | 618 | if (!size) |
| 619 | return 0; |
| 620 | |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 621 | /* we'll create at most two more regions */ |
| 622 | while (type->cnt + 2 > type->max) |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 623 | if (memblock_double_array(type, base, size) < 0) |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 624 | return -ENOMEM; |
| 625 | |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 626 | for_each_memblock_type(type, rgn) { |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 627 | phys_addr_t rbase = rgn->base; |
| 628 | phys_addr_t rend = rbase + rgn->size; |
| 629 | |
| 630 | if (rbase >= end) |
| 631 | break; |
| 632 | if (rend <= base) |
| 633 | continue; |
| 634 | |
| 635 | if (rbase < base) { |
| 636 | /* |
| 637 | * @rgn intersects from below. Split and continue |
| 638 | * to process the next region - the new top half. |
| 639 | */ |
| 640 | rgn->base = base; |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 641 | rgn->size -= base - rbase; |
| 642 | type->total_size -= base - rbase; |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 643 | memblock_insert_region(type, idx, rbase, base - rbase, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 644 | memblock_get_region_node(rgn), |
| 645 | rgn->flags); |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 646 | } else if (rend > end) { |
| 647 | /* |
| 648 | * @rgn intersects from above. Split and redo the |
| 649 | * current region - the new bottom half. |
| 650 | */ |
| 651 | rgn->base = end; |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 652 | rgn->size -= end - rbase; |
| 653 | type->total_size -= end - rbase; |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 654 | memblock_insert_region(type, idx--, rbase, end - rbase, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 655 | memblock_get_region_node(rgn), |
| 656 | rgn->flags); |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 657 | } else { |
| 658 | /* @rgn is fully contained, record it */ |
| 659 | if (!*end_rgn) |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 660 | *start_rgn = idx; |
| 661 | *end_rgn = idx + 1; |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | return 0; |
| 666 | } |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 667 | |
Alexander Kuleshov | 35bd16a | 2015-11-05 18:47:00 -0800 | [diff] [blame] | 668 | static int __init_memblock memblock_remove_range(struct memblock_type *type, |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 669 | phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 670 | { |
Tejun Heo | 7193618 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 671 | int start_rgn, end_rgn; |
| 672 | int i, ret; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 673 | |
Tejun Heo | 7193618 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 674 | ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); |
| 675 | if (ret) |
| 676 | return ret; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 677 | |
Tejun Heo | 7193618 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 678 | for (i = end_rgn - 1; i >= start_rgn; i--) |
| 679 | memblock_remove_region(type, i); |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 680 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 681 | } |
| 682 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 683 | int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 684 | { |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 685 | return memblock_remove_range(&memblock.memory, base, size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 686 | } |
| 687 | |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 688 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 689 | int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 690 | { |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 691 | memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n", |
H. Peter Anvin | a150439 | 2011-07-14 11:57:10 -0700 | [diff] [blame] | 692 | (unsigned long long)base, |
Grygorii Strashko | 931d13f | 2014-01-21 15:49:17 -0800 | [diff] [blame] | 693 | (unsigned long long)base + size - 1, |
H. Peter Anvin | a150439 | 2011-07-14 11:57:10 -0700 | [diff] [blame] | 694 | (void *)_RET_IP_); |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 695 | |
Shiraz Hashim | cb3a922 | 2016-01-19 15:11:10 +0530 | [diff] [blame] | 696 | if (base < memblock.current_limit) |
Kyle Yan | 65be4a5 | 2016-10-31 15:05:00 -0700 | [diff] [blame] | 697 | kmemleak_free_part_phys(base, size); |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 698 | return memblock_remove_range(&memblock.reserved, base, size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 699 | } |
| 700 | |
Alexander Kuleshov | f705ac4 | 2016-05-20 16:57:35 -0700 | [diff] [blame] | 701 | int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 702 | { |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 703 | memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n", |
H. Peter Anvin | a150439 | 2011-07-14 11:57:10 -0700 | [diff] [blame] | 704 | (unsigned long long)base, |
Grygorii Strashko | 931d13f | 2014-01-21 15:49:17 -0800 | [diff] [blame] | 705 | (unsigned long long)base + size - 1, |
Alexander Kuleshov | f705ac4 | 2016-05-20 16:57:35 -0700 | [diff] [blame] | 706 | 0UL, (void *)_RET_IP_); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 707 | |
Alexander Kuleshov | f705ac4 | 2016-05-20 16:57:35 -0700 | [diff] [blame] | 708 | return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 709 | } |
| 710 | |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 711 | /** |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 712 | * |
Tony Luck | 4308ce1 | 2014-12-12 16:54:59 -0800 | [diff] [blame] | 713 | * This function isolates region [@base, @base + @size), and sets/clears flag |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 714 | * |
Alexander Kuleshov | c115393 | 2015-09-08 15:04:02 -0700 | [diff] [blame] | 715 | * Return 0 on success, -errno on failure. |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 716 | */ |
Tony Luck | 4308ce1 | 2014-12-12 16:54:59 -0800 | [diff] [blame] | 717 | static int __init_memblock memblock_setclr_flag(phys_addr_t base, |
| 718 | phys_addr_t size, int set, int flag) |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 719 | { |
| 720 | struct memblock_type *type = &memblock.memory; |
| 721 | int i, ret, start_rgn, end_rgn; |
| 722 | |
| 723 | ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); |
| 724 | if (ret) |
| 725 | return ret; |
| 726 | |
| 727 | for (i = start_rgn; i < end_rgn; i++) |
Tony Luck | 4308ce1 | 2014-12-12 16:54:59 -0800 | [diff] [blame] | 728 | if (set) |
| 729 | memblock_set_region_flags(&type->regions[i], flag); |
| 730 | else |
| 731 | memblock_clear_region_flags(&type->regions[i], flag); |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 732 | |
| 733 | memblock_merge_regions(type); |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | /** |
Tony Luck | 4308ce1 | 2014-12-12 16:54:59 -0800 | [diff] [blame] | 738 | * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG. |
| 739 | * @base: the base phys addr of the region |
| 740 | * @size: the size of the region |
| 741 | * |
Alexander Kuleshov | c115393 | 2015-09-08 15:04:02 -0700 | [diff] [blame] | 742 | * Return 0 on success, -errno on failure. |
Tony Luck | 4308ce1 | 2014-12-12 16:54:59 -0800 | [diff] [blame] | 743 | */ |
| 744 | int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size) |
| 745 | { |
| 746 | return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG); |
| 747 | } |
| 748 | |
| 749 | /** |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 750 | * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region. |
| 751 | * @base: the base phys addr of the region |
| 752 | * @size: the size of the region |
| 753 | * |
Alexander Kuleshov | c115393 | 2015-09-08 15:04:02 -0700 | [diff] [blame] | 754 | * Return 0 on success, -errno on failure. |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 755 | */ |
| 756 | int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size) |
| 757 | { |
Tony Luck | 4308ce1 | 2014-12-12 16:54:59 -0800 | [diff] [blame] | 758 | return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG); |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /** |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 762 | * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR. |
| 763 | * @base: the base phys addr of the region |
| 764 | * @size: the size of the region |
| 765 | * |
Alexander Kuleshov | c115393 | 2015-09-08 15:04:02 -0700 | [diff] [blame] | 766 | * Return 0 on success, -errno on failure. |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 767 | */ |
| 768 | int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size) |
| 769 | { |
| 770 | system_has_some_mirror = true; |
| 771 | |
| 772 | return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR); |
| 773 | } |
| 774 | |
Ard Biesheuvel | bf3d3cc | 2015-11-30 13:28:15 +0100 | [diff] [blame] | 775 | /** |
| 776 | * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP. |
| 777 | * @base: the base phys addr of the region |
| 778 | * @size: the size of the region |
| 779 | * |
| 780 | * Return 0 on success, -errno on failure. |
| 781 | */ |
| 782 | int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size) |
| 783 | { |
| 784 | return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP); |
| 785 | } |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 786 | |
| 787 | /** |
Robin Holt | 8e7a7f8 | 2015-06-30 14:56:41 -0700 | [diff] [blame] | 788 | * __next_reserved_mem_region - next function for for_each_reserved_region() |
| 789 | * @idx: pointer to u64 loop variable |
| 790 | * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL |
| 791 | * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL |
| 792 | * |
| 793 | * Iterate over all reserved memory regions. |
| 794 | */ |
| 795 | void __init_memblock __next_reserved_mem_region(u64 *idx, |
| 796 | phys_addr_t *out_start, |
| 797 | phys_addr_t *out_end) |
| 798 | { |
Alexander Kuleshov | 567d117 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 799 | struct memblock_type *type = &memblock.reserved; |
Robin Holt | 8e7a7f8 | 2015-06-30 14:56:41 -0700 | [diff] [blame] | 800 | |
Richard Leitner | cd33a76 | 2016-05-20 16:58:33 -0700 | [diff] [blame] | 801 | if (*idx < type->cnt) { |
Alexander Kuleshov | 567d117 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 802 | struct memblock_region *r = &type->regions[*idx]; |
Robin Holt | 8e7a7f8 | 2015-06-30 14:56:41 -0700 | [diff] [blame] | 803 | phys_addr_t base = r->base; |
| 804 | phys_addr_t size = r->size; |
| 805 | |
| 806 | if (out_start) |
| 807 | *out_start = base; |
| 808 | if (out_end) |
| 809 | *out_end = base + size - 1; |
| 810 | |
| 811 | *idx += 1; |
| 812 | return; |
| 813 | } |
| 814 | |
| 815 | /* signal end of iteration */ |
| 816 | *idx = ULLONG_MAX; |
| 817 | } |
| 818 | |
| 819 | /** |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 820 | * __next__mem_range - next function for for_each_free_mem_range() etc. |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 821 | * @idx: pointer to u64 loop variable |
Grygorii Strashko | b115423 | 2014-01-21 15:50:16 -0800 | [diff] [blame] | 822 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 823 | * @flags: pick from blocks based on memory attributes |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 824 | * @type_a: pointer to memblock_type from where the range is taken |
| 825 | * @type_b: pointer to memblock_type which excludes memory from being taken |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 826 | * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 827 | * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 828 | * @out_nid: ptr to int for nid of the range, can be %NULL |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 829 | * |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 830 | * Find the first area from *@idx which matches @nid, fill the out |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 831 | * parameters, and update *@idx for the next iteration. The lower 32bit of |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 832 | * *@idx contains index into type_a and the upper 32bit indexes the |
| 833 | * areas before each region in type_b. For example, if type_b regions |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 834 | * look like the following, |
| 835 | * |
| 836 | * 0:[0-16), 1:[32-48), 2:[128-130) |
| 837 | * |
| 838 | * The upper 32bit indexes the following regions. |
| 839 | * |
| 840 | * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) |
| 841 | * |
| 842 | * As both region arrays are sorted, the function advances the two indices |
| 843 | * in lockstep and returns each intersection. |
| 844 | */ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 845 | void __init_memblock __next_mem_range(u64 *idx, int nid, ulong flags, |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 846 | struct memblock_type *type_a, |
| 847 | struct memblock_type *type_b, |
| 848 | phys_addr_t *out_start, |
| 849 | phys_addr_t *out_end, int *out_nid) |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 850 | { |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 851 | int idx_a = *idx & 0xffffffff; |
| 852 | int idx_b = *idx >> 32; |
Grygorii Strashko | b115423 | 2014-01-21 15:50:16 -0800 | [diff] [blame] | 853 | |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 854 | if (WARN_ONCE(nid == MAX_NUMNODES, |
| 855 | "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) |
Grygorii Strashko | 560dca27 | 2014-01-21 15:50:55 -0800 | [diff] [blame] | 856 | nid = NUMA_NO_NODE; |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 857 | |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 858 | for (; idx_a < type_a->cnt; idx_a++) { |
| 859 | struct memblock_region *m = &type_a->regions[idx_a]; |
| 860 | |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 861 | phys_addr_t m_start = m->base; |
| 862 | phys_addr_t m_end = m->base + m->size; |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 863 | int m_nid = memblock_get_region_node(m); |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 864 | |
| 865 | /* only memory regions are associated with nodes, check it */ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 866 | if (nid != NUMA_NO_NODE && nid != m_nid) |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 867 | continue; |
| 868 | |
Xishi Qiu | 0a313a9 | 2014-09-09 14:50:46 -0700 | [diff] [blame] | 869 | /* skip hotpluggable memory regions if needed */ |
| 870 | if (movable_node_is_enabled() && memblock_is_hotpluggable(m)) |
| 871 | continue; |
| 872 | |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 873 | /* if we want mirror memory skip non-mirror memory regions */ |
| 874 | if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) |
| 875 | continue; |
| 876 | |
Ard Biesheuvel | bf3d3cc | 2015-11-30 13:28:15 +0100 | [diff] [blame] | 877 | /* skip nomap memory unless we were asked for it explicitly */ |
| 878 | if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) |
| 879 | continue; |
| 880 | |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 881 | if (!type_b) { |
| 882 | if (out_start) |
| 883 | *out_start = m_start; |
| 884 | if (out_end) |
| 885 | *out_end = m_end; |
| 886 | if (out_nid) |
| 887 | *out_nid = m_nid; |
| 888 | idx_a++; |
| 889 | *idx = (u32)idx_a | (u64)idx_b << 32; |
| 890 | return; |
| 891 | } |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 892 | |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 893 | /* scan areas before each reservation */ |
| 894 | for (; idx_b < type_b->cnt + 1; idx_b++) { |
| 895 | struct memblock_region *r; |
| 896 | phys_addr_t r_start; |
| 897 | phys_addr_t r_end; |
| 898 | |
| 899 | r = &type_b->regions[idx_b]; |
| 900 | r_start = idx_b ? r[-1].base + r[-1].size : 0; |
| 901 | r_end = idx_b < type_b->cnt ? |
Stefan Agner | 15c3e89 | 2018-04-05 16:25:38 -0700 | [diff] [blame] | 902 | r->base : (phys_addr_t)ULLONG_MAX; |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 903 | |
| 904 | /* |
| 905 | * if idx_b advanced past idx_a, |
| 906 | * break out to advance idx_a |
| 907 | */ |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 908 | if (r_start >= m_end) |
| 909 | break; |
| 910 | /* if the two regions intersect, we're done */ |
| 911 | if (m_start < r_end) { |
| 912 | if (out_start) |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 913 | *out_start = |
| 914 | max(m_start, r_start); |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 915 | if (out_end) |
| 916 | *out_end = min(m_end, r_end); |
| 917 | if (out_nid) |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 918 | *out_nid = m_nid; |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 919 | /* |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 920 | * The region which ends first is |
| 921 | * advanced for the next iteration. |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 922 | */ |
| 923 | if (m_end <= r_end) |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 924 | idx_a++; |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 925 | else |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 926 | idx_b++; |
| 927 | *idx = (u32)idx_a | (u64)idx_b << 32; |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | /* signal end of iteration */ |
| 934 | *idx = ULLONG_MAX; |
| 935 | } |
| 936 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 937 | /** |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 938 | * __next_mem_range_rev - generic next function for for_each_*_range_rev() |
| 939 | * |
| 940 | * Finds the next range from type_a which is not marked as unsuitable |
| 941 | * in type_b. |
| 942 | * |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 943 | * @idx: pointer to u64 loop variable |
Alexander Kuleshov | ad5ea8c | 2015-09-08 15:04:22 -0700 | [diff] [blame] | 944 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 945 | * @flags: pick from blocks based on memory attributes |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 946 | * @type_a: pointer to memblock_type from where the range is taken |
| 947 | * @type_b: pointer to memblock_type which excludes memory from being taken |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 948 | * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 949 | * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 950 | * @out_nid: ptr to int for nid of the range, can be %NULL |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 951 | * |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 952 | * Reverse of __next_mem_range(). |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 953 | */ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 954 | void __init_memblock __next_mem_range_rev(u64 *idx, int nid, ulong flags, |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 955 | struct memblock_type *type_a, |
| 956 | struct memblock_type *type_b, |
| 957 | phys_addr_t *out_start, |
| 958 | phys_addr_t *out_end, int *out_nid) |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 959 | { |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 960 | int idx_a = *idx & 0xffffffff; |
| 961 | int idx_b = *idx >> 32; |
Grygorii Strashko | b115423 | 2014-01-21 15:50:16 -0800 | [diff] [blame] | 962 | |
Grygorii Strashko | 560dca27 | 2014-01-21 15:50:55 -0800 | [diff] [blame] | 963 | if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) |
| 964 | nid = NUMA_NO_NODE; |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 965 | |
| 966 | if (*idx == (u64)ULLONG_MAX) { |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 967 | idx_a = type_a->cnt - 1; |
zijun_hu | e47608a | 2016-08-04 15:32:00 -0700 | [diff] [blame] | 968 | if (type_b != NULL) |
| 969 | idx_b = type_b->cnt; |
| 970 | else |
| 971 | idx_b = 0; |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 974 | for (; idx_a >= 0; idx_a--) { |
| 975 | struct memblock_region *m = &type_a->regions[idx_a]; |
| 976 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 977 | phys_addr_t m_start = m->base; |
| 978 | phys_addr_t m_end = m->base + m->size; |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 979 | int m_nid = memblock_get_region_node(m); |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 980 | |
| 981 | /* only memory regions are associated with nodes, check it */ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 982 | if (nid != NUMA_NO_NODE && nid != m_nid) |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 983 | continue; |
| 984 | |
Tang Chen | 55ac590 | 2014-01-21 15:49:35 -0800 | [diff] [blame] | 985 | /* skip hotpluggable memory regions if needed */ |
| 986 | if (movable_node_is_enabled() && memblock_is_hotpluggable(m)) |
| 987 | continue; |
| 988 | |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 989 | /* if we want mirror memory skip non-mirror memory regions */ |
| 990 | if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) |
| 991 | continue; |
| 992 | |
Ard Biesheuvel | bf3d3cc | 2015-11-30 13:28:15 +0100 | [diff] [blame] | 993 | /* skip nomap memory unless we were asked for it explicitly */ |
| 994 | if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) |
| 995 | continue; |
| 996 | |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 997 | if (!type_b) { |
| 998 | if (out_start) |
| 999 | *out_start = m_start; |
| 1000 | if (out_end) |
| 1001 | *out_end = m_end; |
| 1002 | if (out_nid) |
| 1003 | *out_nid = m_nid; |
zijun_hu | fb399b4 | 2016-07-28 15:48:56 -0700 | [diff] [blame] | 1004 | idx_a--; |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1005 | *idx = (u32)idx_a | (u64)idx_b << 32; |
| 1006 | return; |
| 1007 | } |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 1008 | |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1009 | /* scan areas before each reservation */ |
| 1010 | for (; idx_b >= 0; idx_b--) { |
| 1011 | struct memblock_region *r; |
| 1012 | phys_addr_t r_start; |
| 1013 | phys_addr_t r_end; |
| 1014 | |
| 1015 | r = &type_b->regions[idx_b]; |
| 1016 | r_start = idx_b ? r[-1].base + r[-1].size : 0; |
| 1017 | r_end = idx_b < type_b->cnt ? |
Stefan Agner | 15c3e89 | 2018-04-05 16:25:38 -0700 | [diff] [blame] | 1018 | r->base : (phys_addr_t)ULLONG_MAX; |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1019 | /* |
| 1020 | * if idx_b advanced past idx_a, |
| 1021 | * break out to advance idx_a |
| 1022 | */ |
| 1023 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 1024 | if (r_end <= m_start) |
| 1025 | break; |
| 1026 | /* if the two regions intersect, we're done */ |
| 1027 | if (m_end > r_start) { |
| 1028 | if (out_start) |
| 1029 | *out_start = max(m_start, r_start); |
| 1030 | if (out_end) |
| 1031 | *out_end = min(m_end, r_end); |
| 1032 | if (out_nid) |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1033 | *out_nid = m_nid; |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 1034 | if (m_start >= r_start) |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1035 | idx_a--; |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 1036 | else |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1037 | idx_b--; |
| 1038 | *idx = (u32)idx_a | (u64)idx_b << 32; |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 1039 | return; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1043 | /* signal end of iteration */ |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 1044 | *idx = ULLONG_MAX; |
| 1045 | } |
| 1046 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1047 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 1048 | /* |
| 1049 | * Common iterator interface used to define for_each_mem_range(). |
| 1050 | */ |
| 1051 | void __init_memblock __next_mem_pfn_range(int *idx, int nid, |
| 1052 | unsigned long *out_start_pfn, |
| 1053 | unsigned long *out_end_pfn, int *out_nid) |
| 1054 | { |
| 1055 | struct memblock_type *type = &memblock.memory; |
| 1056 | struct memblock_region *r; |
| 1057 | |
| 1058 | while (++*idx < type->cnt) { |
| 1059 | r = &type->regions[*idx]; |
| 1060 | |
| 1061 | if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) |
| 1062 | continue; |
| 1063 | if (nid == MAX_NUMNODES || nid == r->nid) |
| 1064 | break; |
| 1065 | } |
| 1066 | if (*idx >= type->cnt) { |
| 1067 | *idx = -1; |
| 1068 | return; |
| 1069 | } |
| 1070 | |
| 1071 | if (out_start_pfn) |
| 1072 | *out_start_pfn = PFN_UP(r->base); |
| 1073 | if (out_end_pfn) |
| 1074 | *out_end_pfn = PFN_DOWN(r->base + r->size); |
| 1075 | if (out_nid) |
| 1076 | *out_nid = r->nid; |
| 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * memblock_set_node - set node ID on memblock regions |
| 1081 | * @base: base of area to set node ID for |
| 1082 | * @size: size of area to set node ID for |
Tang Chen | e7e8de5 | 2014-01-21 15:49:26 -0800 | [diff] [blame] | 1083 | * @type: memblock type to set node ID for |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1084 | * @nid: node ID to set |
| 1085 | * |
Tang Chen | e7e8de5 | 2014-01-21 15:49:26 -0800 | [diff] [blame] | 1086 | * Set the nid of memblock @type regions in [@base,@base+@size) to @nid. |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1087 | * Regions which cross the area boundaries are split as necessary. |
| 1088 | * |
| 1089 | * RETURNS: |
| 1090 | * 0 on success, -errno on failure. |
| 1091 | */ |
| 1092 | int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, |
Tang Chen | e7e8de5 | 2014-01-21 15:49:26 -0800 | [diff] [blame] | 1093 | struct memblock_type *type, int nid) |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1094 | { |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1095 | int start_rgn, end_rgn; |
| 1096 | int i, ret; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1097 | |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1098 | ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); |
| 1099 | if (ret) |
| 1100 | return ret; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1101 | |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1102 | for (i = start_rgn; i < end_rgn; i++) |
Wanpeng Li | e9d24ad | 2012-10-08 16:32:21 -0700 | [diff] [blame] | 1103 | memblock_set_region_node(&type->regions[i], nid); |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1104 | |
| 1105 | memblock_merge_regions(type); |
| 1106 | return 0; |
| 1107 | } |
| 1108 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
| 1109 | |
Akinobu Mita | 2bfc286 | 2014-06-04 16:06:53 -0700 | [diff] [blame] | 1110 | static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, |
| 1111 | phys_addr_t align, phys_addr_t start, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 1112 | phys_addr_t end, int nid, ulong flags) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1113 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1114 | phys_addr_t found; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1115 | |
Grygorii Strashko | 79f40fa | 2014-01-21 15:50:12 -0800 | [diff] [blame] | 1116 | if (!align) |
| 1117 | align = SMP_CACHE_BYTES; |
Vineet Gupta | 94f3d3a | 2013-04-29 15:06:15 -0700 | [diff] [blame] | 1118 | |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 1119 | found = memblock_find_in_range_node(size, align, start, end, nid, |
| 1120 | flags); |
Catalin Marinas | aedf95e | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 1121 | if (found && !memblock_reserve(found, size)) { |
| 1122 | /* |
| 1123 | * The min_count is set to 0 so that memblock allocations are |
| 1124 | * never reported as leaks. |
| 1125 | */ |
Shiraz Hashim | cb3a922 | 2016-01-19 15:11:10 +0530 | [diff] [blame] | 1126 | if (found < memblock.current_limit) |
Kyle Yan | 65be4a5 | 2016-10-31 15:05:00 -0700 | [diff] [blame] | 1127 | kmemleak_alloc_phys(found, size, 0, 0); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1128 | return found; |
Catalin Marinas | aedf95e | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 1129 | } |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1130 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1131 | } |
| 1132 | |
Akinobu Mita | 2bfc286 | 2014-06-04 16:06:53 -0700 | [diff] [blame] | 1133 | phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 1134 | phys_addr_t start, phys_addr_t end, |
| 1135 | ulong flags) |
Akinobu Mita | 2bfc286 | 2014-06-04 16:06:53 -0700 | [diff] [blame] | 1136 | { |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 1137 | return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE, |
| 1138 | flags); |
Akinobu Mita | 2bfc286 | 2014-06-04 16:06:53 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size, |
| 1142 | phys_addr_t align, phys_addr_t max_addr, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 1143 | int nid, ulong flags) |
Akinobu Mita | 2bfc286 | 2014-06-04 16:06:53 -0700 | [diff] [blame] | 1144 | { |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 1145 | return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags); |
Akinobu Mita | 2bfc286 | 2014-06-04 16:06:53 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 1148 | phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid) |
| 1149 | { |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 1150 | ulong flags = choose_memblock_flags(); |
| 1151 | phys_addr_t ret; |
| 1152 | |
| 1153 | again: |
| 1154 | ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, |
| 1155 | nid, flags); |
| 1156 | |
| 1157 | if (!ret && (flags & MEMBLOCK_MIRROR)) { |
| 1158 | flags &= ~MEMBLOCK_MIRROR; |
| 1159 | goto again; |
| 1160 | } |
| 1161 | return ret; |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
| 1165 | { |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 1166 | return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE, |
| 1167 | MEMBLOCK_NONE); |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1170 | phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1171 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1172 | phys_addr_t alloc; |
| 1173 | |
| 1174 | alloc = __memblock_alloc_base(size, align, max_addr); |
| 1175 | |
| 1176 | if (alloc == 0) |
| 1177 | panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n", |
| 1178 | (unsigned long long) size, (unsigned long long) max_addr); |
| 1179 | |
| 1180 | return alloc; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1181 | } |
| 1182 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1183 | phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1184 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1185 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1186 | } |
| 1187 | |
Benjamin Herrenschmidt | 9d1e249 | 2010-07-06 15:39:17 -0700 | [diff] [blame] | 1188 | phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) |
| 1189 | { |
| 1190 | phys_addr_t res = memblock_alloc_nid(size, align, nid); |
| 1191 | |
| 1192 | if (res) |
| 1193 | return res; |
Tejun Heo | 15fb097 | 2011-07-12 09:58:07 +0200 | [diff] [blame] | 1194 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1195 | } |
| 1196 | |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1197 | /** |
| 1198 | * memblock_virt_alloc_internal - allocate boot memory block |
| 1199 | * @size: size of memory block to be allocated in bytes |
| 1200 | * @align: alignment of the region and block's size |
| 1201 | * @min_addr: the lower bound of the memory region to allocate (phys address) |
| 1202 | * @max_addr: the upper bound of the memory region to allocate (phys address) |
| 1203 | * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
| 1204 | * |
| 1205 | * The @min_addr limit is dropped if it can not be satisfied and the allocation |
| 1206 | * will fall back to memory below @min_addr. Also, allocation may fall back |
| 1207 | * to any node in the system if the specified node can not |
| 1208 | * hold the requested memory. |
| 1209 | * |
| 1210 | * The allocation is performed from memory region limited by |
| 1211 | * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE. |
| 1212 | * |
| 1213 | * The memory block is aligned on SMP_CACHE_BYTES if @align == 0. |
| 1214 | * |
| 1215 | * The phys address of allocated boot memory block is converted to virtual and |
| 1216 | * allocated memory is reset to 0. |
| 1217 | * |
| 1218 | * In addition, function sets the min_count to 0 using kmemleak_alloc for |
| 1219 | * allocated boot memory block, so that it is never reported as leaks. |
| 1220 | * |
| 1221 | * RETURNS: |
| 1222 | * Virtual address of allocated memory block on success, NULL on failure. |
| 1223 | */ |
| 1224 | static void * __init memblock_virt_alloc_internal( |
| 1225 | phys_addr_t size, phys_addr_t align, |
| 1226 | phys_addr_t min_addr, phys_addr_t max_addr, |
| 1227 | int nid) |
| 1228 | { |
| 1229 | phys_addr_t alloc; |
| 1230 | void *ptr; |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 1231 | ulong flags = choose_memblock_flags(); |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1232 | |
Grygorii Strashko | 560dca27 | 2014-01-21 15:50:55 -0800 | [diff] [blame] | 1233 | if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) |
| 1234 | nid = NUMA_NO_NODE; |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1235 | |
| 1236 | /* |
| 1237 | * Detect any accidental use of these APIs after slab is ready, as at |
| 1238 | * this moment memblock may be deinitialized already and its |
| 1239 | * internal data may be destroyed (after execution of free_all_bootmem) |
| 1240 | */ |
| 1241 | if (WARN_ON_ONCE(slab_is_available())) |
| 1242 | return kzalloc_node(size, GFP_NOWAIT, nid); |
| 1243 | |
| 1244 | if (!align) |
| 1245 | align = SMP_CACHE_BYTES; |
| 1246 | |
Yinghai Lu | f544e14 | 2014-01-29 14:05:52 -0800 | [diff] [blame] | 1247 | if (max_addr > memblock.current_limit) |
| 1248 | max_addr = memblock.current_limit; |
| 1249 | |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1250 | again: |
| 1251 | alloc = memblock_find_in_range_node(size, align, min_addr, max_addr, |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 1252 | nid, flags); |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1253 | if (alloc) |
| 1254 | goto done; |
| 1255 | |
| 1256 | if (nid != NUMA_NO_NODE) { |
| 1257 | alloc = memblock_find_in_range_node(size, align, min_addr, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 1258 | max_addr, NUMA_NO_NODE, |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 1259 | flags); |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1260 | if (alloc) |
| 1261 | goto done; |
| 1262 | } |
| 1263 | |
| 1264 | if (min_addr) { |
| 1265 | min_addr = 0; |
| 1266 | goto again; |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 1269 | if (flags & MEMBLOCK_MIRROR) { |
| 1270 | flags &= ~MEMBLOCK_MIRROR; |
| 1271 | pr_warn("Could not allocate %pap bytes of mirrored memory\n", |
| 1272 | &size); |
| 1273 | goto again; |
| 1274 | } |
| 1275 | |
| 1276 | return NULL; |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1277 | done: |
| 1278 | memblock_reserve(alloc, size); |
| 1279 | ptr = phys_to_virt(alloc); |
| 1280 | memset(ptr, 0, size); |
| 1281 | |
| 1282 | /* |
| 1283 | * The min_count is set to 0 so that bootmem allocated blocks |
| 1284 | * are never reported as leaks. This is because many of these blocks |
| 1285 | * are only referred via the physical address which is not |
| 1286 | * looked up by kmemleak. |
| 1287 | */ |
| 1288 | kmemleak_alloc(ptr, size, 0, 0); |
| 1289 | |
| 1290 | return ptr; |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block |
| 1295 | * @size: size of memory block to be allocated in bytes |
| 1296 | * @align: alignment of the region and block's size |
| 1297 | * @min_addr: the lower bound of the memory region from where the allocation |
| 1298 | * is preferred (phys address) |
| 1299 | * @max_addr: the upper bound of the memory region from where the allocation |
| 1300 | * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to |
| 1301 | * allocate only from memory limited by memblock.current_limit value |
| 1302 | * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
| 1303 | * |
| 1304 | * Public version of _memblock_virt_alloc_try_nid_nopanic() which provides |
| 1305 | * additional debug information (including caller info), if enabled. |
| 1306 | * |
| 1307 | * RETURNS: |
| 1308 | * Virtual address of allocated memory block on success, NULL on failure. |
| 1309 | */ |
| 1310 | void * __init memblock_virt_alloc_try_nid_nopanic( |
| 1311 | phys_addr_t size, phys_addr_t align, |
| 1312 | phys_addr_t min_addr, phys_addr_t max_addr, |
| 1313 | int nid) |
| 1314 | { |
| 1315 | memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n", |
| 1316 | __func__, (u64)size, (u64)align, nid, (u64)min_addr, |
| 1317 | (u64)max_addr, (void *)_RET_IP_); |
| 1318 | return memblock_virt_alloc_internal(size, align, min_addr, |
| 1319 | max_addr, nid); |
| 1320 | } |
| 1321 | |
| 1322 | /** |
| 1323 | * memblock_virt_alloc_try_nid - allocate boot memory block with panicking |
| 1324 | * @size: size of memory block to be allocated in bytes |
| 1325 | * @align: alignment of the region and block's size |
| 1326 | * @min_addr: the lower bound of the memory region from where the allocation |
| 1327 | * is preferred (phys address) |
| 1328 | * @max_addr: the upper bound of the memory region from where the allocation |
| 1329 | * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to |
| 1330 | * allocate only from memory limited by memblock.current_limit value |
| 1331 | * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
| 1332 | * |
| 1333 | * Public panicking version of _memblock_virt_alloc_try_nid_nopanic() |
| 1334 | * which provides debug information (including caller info), if enabled, |
| 1335 | * and panics if the request can not be satisfied. |
| 1336 | * |
| 1337 | * RETURNS: |
| 1338 | * Virtual address of allocated memory block on success, NULL on failure. |
| 1339 | */ |
| 1340 | void * __init memblock_virt_alloc_try_nid( |
| 1341 | phys_addr_t size, phys_addr_t align, |
| 1342 | phys_addr_t min_addr, phys_addr_t max_addr, |
| 1343 | int nid) |
| 1344 | { |
| 1345 | void *ptr; |
| 1346 | |
| 1347 | memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n", |
| 1348 | __func__, (u64)size, (u64)align, nid, (u64)min_addr, |
| 1349 | (u64)max_addr, (void *)_RET_IP_); |
| 1350 | ptr = memblock_virt_alloc_internal(size, align, |
| 1351 | min_addr, max_addr, nid); |
| 1352 | if (ptr) |
| 1353 | return ptr; |
| 1354 | |
| 1355 | panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx\n", |
| 1356 | __func__, (u64)size, (u64)align, nid, (u64)min_addr, |
| 1357 | (u64)max_addr); |
| 1358 | return NULL; |
| 1359 | } |
| 1360 | |
| 1361 | /** |
| 1362 | * __memblock_free_early - free boot memory block |
| 1363 | * @base: phys starting address of the boot memory block |
| 1364 | * @size: size of the boot memory block in bytes |
| 1365 | * |
| 1366 | * Free boot memory block previously allocated by memblock_virt_alloc_xx() API. |
| 1367 | * The freeing memory will not be released to the buddy allocator. |
| 1368 | */ |
| 1369 | void __init __memblock_free_early(phys_addr_t base, phys_addr_t size) |
| 1370 | { |
| 1371 | memblock_dbg("%s: [%#016llx-%#016llx] %pF\n", |
| 1372 | __func__, (u64)base, (u64)base + size - 1, |
| 1373 | (void *)_RET_IP_); |
Catalin Marinas | 9099dae | 2016-10-11 13:55:11 -0700 | [diff] [blame] | 1374 | kmemleak_free_part_phys(base, size); |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1375 | memblock_remove_range(&memblock.reserved, base, size); |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | /* |
| 1379 | * __memblock_free_late - free bootmem block pages directly to buddy allocator |
| 1380 | * @addr: phys starting address of the boot memory block |
| 1381 | * @size: size of the boot memory block in bytes |
| 1382 | * |
| 1383 | * This is only useful when the bootmem allocator has already been torn |
| 1384 | * down, but we are still initializing the system. Pages are released directly |
| 1385 | * to the buddy allocator, no bootmem metadata is updated because it is gone. |
| 1386 | */ |
| 1387 | void __init __memblock_free_late(phys_addr_t base, phys_addr_t size) |
| 1388 | { |
| 1389 | u64 cursor, end; |
| 1390 | |
| 1391 | memblock_dbg("%s: [%#016llx-%#016llx] %pF\n", |
| 1392 | __func__, (u64)base, (u64)base + size - 1, |
| 1393 | (void *)_RET_IP_); |
Catalin Marinas | 9099dae | 2016-10-11 13:55:11 -0700 | [diff] [blame] | 1394 | kmemleak_free_part_phys(base, size); |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1395 | cursor = PFN_UP(base); |
| 1396 | end = PFN_DOWN(base + size); |
| 1397 | |
| 1398 | for (; cursor < end; cursor++) { |
Mel Gorman | d70ddd7 | 2015-06-30 14:56:52 -0700 | [diff] [blame] | 1399 | __free_pages_bootmem(pfn_to_page(cursor), cursor, 0); |
Santosh Shilimkar | 26f09e9 | 2014-01-21 15:50:19 -0800 | [diff] [blame] | 1400 | totalram_pages++; |
| 1401 | } |
| 1402 | } |
Benjamin Herrenschmidt | 9d1e249 | 2010-07-06 15:39:17 -0700 | [diff] [blame] | 1403 | |
| 1404 | /* |
| 1405 | * Remaining API functions |
| 1406 | */ |
| 1407 | |
David Gibson | 1f1ffb8a | 2016-02-05 15:36:19 -0800 | [diff] [blame] | 1408 | phys_addr_t __init_memblock memblock_phys_mem_size(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1409 | { |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 1410 | return memblock.memory.total_size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1411 | } |
| 1412 | |
Srikar Dronamraju | 8907de5 | 2016-10-07 16:59:18 -0700 | [diff] [blame] | 1413 | phys_addr_t __init_memblock memblock_reserved_size(void) |
| 1414 | { |
| 1415 | return memblock.reserved.total_size; |
| 1416 | } |
| 1417 | |
Yinghai Lu | 595ad9a | 2013-01-24 12:20:09 -0800 | [diff] [blame] | 1418 | phys_addr_t __init memblock_mem_size(unsigned long limit_pfn) |
| 1419 | { |
| 1420 | unsigned long pages = 0; |
| 1421 | struct memblock_region *r; |
| 1422 | unsigned long start_pfn, end_pfn; |
| 1423 | |
| 1424 | for_each_memblock(memory, r) { |
| 1425 | start_pfn = memblock_region_memory_base_pfn(r); |
| 1426 | end_pfn = memblock_region_memory_end_pfn(r); |
| 1427 | start_pfn = min_t(unsigned long, start_pfn, limit_pfn); |
| 1428 | end_pfn = min_t(unsigned long, end_pfn, limit_pfn); |
| 1429 | pages += end_pfn - start_pfn; |
| 1430 | } |
| 1431 | |
Fabian Frederick | 1676323 | 2014-04-07 15:37:53 -0700 | [diff] [blame] | 1432 | return PFN_PHYS(pages); |
Yinghai Lu | 595ad9a | 2013-01-24 12:20:09 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
Sam Ravnborg | 0a93ebe | 2011-10-31 17:08:16 -0700 | [diff] [blame] | 1435 | /* lowest address */ |
| 1436 | phys_addr_t __init_memblock memblock_start_of_DRAM(void) |
| 1437 | { |
| 1438 | return memblock.memory.regions[0].base; |
| 1439 | } |
| 1440 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 1441 | phys_addr_t __init_memblock memblock_end_of_DRAM(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1442 | { |
| 1443 | int idx = memblock.memory.cnt - 1; |
| 1444 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 1445 | return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1446 | } |
| 1447 | |
Dennis Chen | a571d4e | 2016-07-28 15:48:26 -0700 | [diff] [blame] | 1448 | static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1449 | { |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1450 | phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX; |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 1451 | struct memblock_region *r; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1452 | |
Dennis Chen | a571d4e | 2016-07-28 15:48:26 -0700 | [diff] [blame] | 1453 | /* |
| 1454 | * translate the memory @limit size into the max address within one of |
| 1455 | * the memory memblock regions, if the @limit exceeds the total size |
| 1456 | * of those regions, max_addr will keep original value ULLONG_MAX |
| 1457 | */ |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 1458 | for_each_memblock(memory, r) { |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1459 | if (limit <= r->size) { |
| 1460 | max_addr = r->base + limit; |
| 1461 | break; |
| 1462 | } |
| 1463 | limit -= r->size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1464 | } |
| 1465 | |
Dennis Chen | a571d4e | 2016-07-28 15:48:26 -0700 | [diff] [blame] | 1466 | return max_addr; |
| 1467 | } |
| 1468 | |
| 1469 | void __init memblock_enforce_memory_limit(phys_addr_t limit) |
| 1470 | { |
| 1471 | phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX; |
| 1472 | |
| 1473 | if (!limit) |
| 1474 | return; |
| 1475 | |
| 1476 | max_addr = __find_max_addr(limit); |
| 1477 | |
| 1478 | /* @limit exceeds the total size of the memory, do nothing */ |
| 1479 | if (max_addr == (phys_addr_t)ULLONG_MAX) |
| 1480 | return; |
| 1481 | |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1482 | /* truncate both memory and reserved regions */ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1483 | memblock_remove_range(&memblock.memory, max_addr, |
| 1484 | (phys_addr_t)ULLONG_MAX); |
| 1485 | memblock_remove_range(&memblock.reserved, max_addr, |
| 1486 | (phys_addr_t)ULLONG_MAX); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1487 | } |
| 1488 | |
Dennis Chen | a571d4e | 2016-07-28 15:48:26 -0700 | [diff] [blame] | 1489 | void __init memblock_mem_limit_remove_map(phys_addr_t limit) |
| 1490 | { |
| 1491 | struct memblock_type *type = &memblock.memory; |
| 1492 | phys_addr_t max_addr; |
| 1493 | int i, ret, start_rgn, end_rgn; |
| 1494 | |
| 1495 | if (!limit) |
| 1496 | return; |
| 1497 | |
| 1498 | max_addr = __find_max_addr(limit); |
| 1499 | |
| 1500 | /* @limit exceeds the total size of the memory, do nothing */ |
| 1501 | if (max_addr == (phys_addr_t)ULLONG_MAX) |
| 1502 | return; |
| 1503 | |
| 1504 | ret = memblock_isolate_range(type, max_addr, (phys_addr_t)ULLONG_MAX, |
| 1505 | &start_rgn, &end_rgn); |
| 1506 | if (ret) |
| 1507 | return; |
| 1508 | |
| 1509 | /* remove all the MAP regions above the limit */ |
| 1510 | for (i = end_rgn - 1; i >= start_rgn; i--) { |
| 1511 | if (!memblock_is_nomap(&type->regions[i])) |
| 1512 | memblock_remove_region(type, i); |
| 1513 | } |
| 1514 | /* truncate the reserved regions */ |
| 1515 | memblock_remove_range(&memblock.reserved, max_addr, |
| 1516 | (phys_addr_t)ULLONG_MAX); |
| 1517 | } |
| 1518 | |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1519 | static int __init_memblock __memblock_search(struct memblock_type *type, |
| 1520 | phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 1521 | { |
| 1522 | unsigned int left = 0, right = type->cnt; |
| 1523 | |
| 1524 | do { |
| 1525 | unsigned int mid = (right + left) / 2; |
| 1526 | |
| 1527 | if (addr < type->regions[mid].base) |
| 1528 | right = mid; |
| 1529 | else if (addr >= (type->regions[mid].base + |
| 1530 | type->regions[mid].size)) |
| 1531 | left = mid + 1; |
| 1532 | else |
| 1533 | return mid; |
| 1534 | } while (left < right); |
| 1535 | return -1; |
| 1536 | } |
| 1537 | |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1538 | static int __init_memblock memblock_search(struct memblock_type *type, |
| 1539 | phys_addr_t addr) |
| 1540 | { |
| 1541 | int ret; |
| 1542 | unsigned long seq; |
| 1543 | |
| 1544 | do { |
| 1545 | seq = raw_read_seqcount_begin(&memblock_seq); |
| 1546 | ret = __memblock_search(type, addr); |
| 1547 | } while (unlikely(read_seqcount_retry(&memblock_seq, seq))); |
| 1548 | |
| 1549 | return ret; |
| 1550 | } |
| 1551 | |
Yaowei Bai | b4ad0c7 | 2016-01-14 15:18:54 -0800 | [diff] [blame] | 1552 | bool __init memblock_is_reserved(phys_addr_t addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1553 | { |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 1554 | return memblock_search(&memblock.reserved, addr) != -1; |
| 1555 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1556 | |
Yaowei Bai | b4ad0c7 | 2016-01-14 15:18:54 -0800 | [diff] [blame] | 1557 | bool __init_memblock memblock_is_memory(phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 1558 | { |
| 1559 | return memblock_search(&memblock.memory, addr) != -1; |
| 1560 | } |
| 1561 | |
Ard Biesheuvel | bf3d3cc | 2015-11-30 13:28:15 +0100 | [diff] [blame] | 1562 | int __init_memblock memblock_is_map_memory(phys_addr_t addr) |
| 1563 | { |
| 1564 | int i = memblock_search(&memblock.memory, addr); |
| 1565 | |
| 1566 | if (i == -1) |
| 1567 | return false; |
| 1568 | return !memblock_is_nomap(&memblock.memory.regions[i]); |
| 1569 | } |
| 1570 | |
Yinghai Lu | e76b63f | 2013-09-11 14:22:17 -0700 | [diff] [blame] | 1571 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 1572 | int __init_memblock memblock_search_pfn_nid(unsigned long pfn, |
| 1573 | unsigned long *start_pfn, unsigned long *end_pfn) |
| 1574 | { |
| 1575 | struct memblock_type *type = &memblock.memory; |
Fabian Frederick | 1676323 | 2014-04-07 15:37:53 -0700 | [diff] [blame] | 1576 | int mid = memblock_search(type, PFN_PHYS(pfn)); |
Yinghai Lu | e76b63f | 2013-09-11 14:22:17 -0700 | [diff] [blame] | 1577 | |
| 1578 | if (mid == -1) |
| 1579 | return -1; |
| 1580 | |
Fabian Frederick | f7e2f7e | 2014-06-04 16:07:51 -0700 | [diff] [blame] | 1581 | *start_pfn = PFN_DOWN(type->regions[mid].base); |
| 1582 | *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size); |
Yinghai Lu | e76b63f | 2013-09-11 14:22:17 -0700 | [diff] [blame] | 1583 | |
| 1584 | return type->regions[mid].nid; |
| 1585 | } |
| 1586 | #endif |
| 1587 | |
Stephen Boyd | eab3094 | 2012-05-24 00:45:21 -0700 | [diff] [blame] | 1588 | /** |
| 1589 | * memblock_is_region_memory - check if a region is a subset of memory |
| 1590 | * @base: base of region to check |
| 1591 | * @size: size of region to check |
| 1592 | * |
| 1593 | * Check if the region [@base, @base+@size) is a subset of a memory block. |
| 1594 | * |
| 1595 | * RETURNS: |
| 1596 | * 0 if false, non-zero if true |
| 1597 | */ |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 1598 | int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 1599 | { |
Tomi Valkeinen | abb6527 | 2011-01-20 14:44:20 -0800 | [diff] [blame] | 1600 | int idx = memblock_search(&memblock.memory, base); |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1601 | phys_addr_t end = base + memblock_cap_size(base, &size); |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 1602 | |
| 1603 | if (idx == -1) |
| 1604 | return 0; |
Tomi Valkeinen | abb6527 | 2011-01-20 14:44:20 -0800 | [diff] [blame] | 1605 | return memblock.memory.regions[idx].base <= base && |
| 1606 | (memblock.memory.regions[idx].base + |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1607 | memblock.memory.regions[idx].size) >= end; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1608 | } |
| 1609 | |
Stephen Boyd | 48cc540 | 2017-03-01 16:42:02 -0800 | [diff] [blame] | 1610 | bool __init_memblock memblock_overlaps_memory(phys_addr_t base, |
| 1611 | phys_addr_t size) |
Stephen Boyd | 39ea1a3 | 2012-05-14 18:55:50 -0700 | [diff] [blame] | 1612 | { |
| 1613 | memblock_cap_size(base, &size); |
| 1614 | |
Stephen Boyd | 48cc540 | 2017-03-01 16:42:02 -0800 | [diff] [blame] | 1615 | return memblock_overlaps_region(&memblock.memory, base, size); |
Stephen Boyd | 39ea1a3 | 2012-05-14 18:55:50 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Stephen Boyd | eab3094 | 2012-05-24 00:45:21 -0700 | [diff] [blame] | 1618 | /** |
| 1619 | * memblock_is_region_reserved - check if a region intersects reserved memory |
| 1620 | * @base: base of region to check |
| 1621 | * @size: size of region to check |
| 1622 | * |
| 1623 | * Check if the region [@base, @base+@size) intersects a reserved memory block. |
| 1624 | * |
| 1625 | * RETURNS: |
Tang Chen | c5c5c9d | 2015-09-08 15:02:00 -0700 | [diff] [blame] | 1626 | * True if they intersect, false if not. |
Stephen Boyd | eab3094 | 2012-05-24 00:45:21 -0700 | [diff] [blame] | 1627 | */ |
Tang Chen | c5c5c9d | 2015-09-08 15:02:00 -0700 | [diff] [blame] | 1628 | bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1629 | { |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1630 | memblock_cap_size(base, &size); |
Tang Chen | c5c5c9d | 2015-09-08 15:02:00 -0700 | [diff] [blame] | 1631 | return memblock_overlaps_region(&memblock.reserved, base, size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1632 | } |
| 1633 | |
Yinghai Lu | 6ede1fd | 2012-10-22 16:35:18 -0700 | [diff] [blame] | 1634 | void __init_memblock memblock_trim_memory(phys_addr_t align) |
| 1635 | { |
Yinghai Lu | 6ede1fd | 2012-10-22 16:35:18 -0700 | [diff] [blame] | 1636 | phys_addr_t start, end, orig_start, orig_end; |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 1637 | struct memblock_region *r; |
Yinghai Lu | 6ede1fd | 2012-10-22 16:35:18 -0700 | [diff] [blame] | 1638 | |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 1639 | for_each_memblock(memory, r) { |
| 1640 | orig_start = r->base; |
| 1641 | orig_end = r->base + r->size; |
Yinghai Lu | 6ede1fd | 2012-10-22 16:35:18 -0700 | [diff] [blame] | 1642 | start = round_up(orig_start, align); |
| 1643 | end = round_down(orig_end, align); |
| 1644 | |
| 1645 | if (start == orig_start && end == orig_end) |
| 1646 | continue; |
| 1647 | |
| 1648 | if (start < end) { |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 1649 | r->base = start; |
| 1650 | r->size = end - start; |
Yinghai Lu | 6ede1fd | 2012-10-22 16:35:18 -0700 | [diff] [blame] | 1651 | } else { |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 1652 | memblock_remove_region(&memblock.memory, |
| 1653 | r - memblock.memory.regions); |
| 1654 | r--; |
Yinghai Lu | 6ede1fd | 2012-10-22 16:35:18 -0700 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | } |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 1658 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 1659 | void __init_memblock memblock_set_current_limit(phys_addr_t limit) |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 1660 | { |
| 1661 | memblock.current_limit = limit; |
| 1662 | } |
| 1663 | |
Laura Abbott | fec5101 | 2014-02-27 01:23:43 +0100 | [diff] [blame] | 1664 | phys_addr_t __init_memblock memblock_get_current_limit(void) |
| 1665 | { |
| 1666 | return memblock.current_limit; |
| 1667 | } |
| 1668 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1669 | static void __init_memblock memblock_dump(struct memblock_type *type, char *name) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1670 | { |
| 1671 | unsigned long long base, size; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 1672 | unsigned long flags; |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 1673 | int idx; |
| 1674 | struct memblock_region *rgn; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1675 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1676 | pr_info(" %s.cnt = 0x%lx\n", name, type->cnt); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1677 | |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 1678 | for_each_memblock_type(type, rgn) { |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1679 | char nid_buf[32] = ""; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1680 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1681 | base = rgn->base; |
| 1682 | size = rgn->size; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 1683 | flags = rgn->flags; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1684 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 1685 | if (memblock_get_region_node(rgn) != MAX_NUMNODES) |
| 1686 | snprintf(nid_buf, sizeof(nid_buf), " on node %d", |
| 1687 | memblock_get_region_node(rgn)); |
| 1688 | #endif |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 1689 | pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n", |
Alexander Kuleshov | 8c9c170 | 2016-01-14 15:20:42 -0800 | [diff] [blame] | 1690 | name, idx, base, base + size - 1, size, nid_buf, flags); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | |
Michal Hocko | 292f70c | 2017-06-02 14:46:49 -0700 | [diff] [blame] | 1694 | extern unsigned long __init_memblock |
| 1695 | memblock_reserved_memory_within(phys_addr_t start_addr, phys_addr_t end_addr) |
| 1696 | { |
| 1697 | struct memblock_region *rgn; |
| 1698 | unsigned long size = 0; |
| 1699 | int idx; |
| 1700 | |
| 1701 | for_each_memblock_type((&memblock.reserved), rgn) { |
| 1702 | phys_addr_t start, end; |
| 1703 | |
| 1704 | if (rgn->base + rgn->size < start_addr) |
| 1705 | continue; |
| 1706 | if (rgn->base > end_addr) |
| 1707 | continue; |
| 1708 | |
| 1709 | start = rgn->base; |
| 1710 | end = start + rgn->size; |
| 1711 | size += end - start; |
| 1712 | } |
| 1713 | |
| 1714 | return size; |
| 1715 | } |
| 1716 | |
Tejun Heo | 4ff7b82 | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 1717 | void __init_memblock __memblock_dump_all(void) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1718 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1719 | pr_info("MEMBLOCK configuration:\n"); |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 1720 | pr_info(" memory size = %#llx reserved size = %#llx\n", |
| 1721 | (unsigned long long)memblock.memory.total_size, |
| 1722 | (unsigned long long)memblock.reserved.total_size); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1723 | |
| 1724 | memblock_dump(&memblock.memory, "memory"); |
| 1725 | memblock_dump(&memblock.reserved, "reserved"); |
| 1726 | } |
| 1727 | |
Tejun Heo | 1aadc05 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 1728 | void __init memblock_allow_resize(void) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1729 | { |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 1730 | memblock_can_resize = 1; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1731 | } |
| 1732 | |
Shiraz Hashim | 3eaf6e6 | 2016-04-16 11:43:02 +0530 | [diff] [blame] | 1733 | static unsigned long __init_memblock |
| 1734 | memblock_resize_late(int begin, unsigned long flags) |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1735 | { |
| 1736 | static int memblock_can_resize_old; |
| 1737 | |
| 1738 | if (begin) { |
| 1739 | preempt_disable(); |
Shiraz Hashim | 3eaf6e6 | 2016-04-16 11:43:02 +0530 | [diff] [blame] | 1740 | local_irq_save(flags); |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1741 | memblock_can_resize_old = memblock_can_resize; |
| 1742 | memblock_can_resize = 0; |
| 1743 | raw_write_seqcount_begin(&memblock_seq); |
| 1744 | } else { |
| 1745 | raw_write_seqcount_end(&memblock_seq); |
| 1746 | memblock_can_resize = memblock_can_resize_old; |
Shiraz Hashim | 3eaf6e6 | 2016-04-16 11:43:02 +0530 | [diff] [blame] | 1747 | local_irq_restore(flags); |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1748 | preempt_enable(); |
| 1749 | } |
Shiraz Hashim | 3eaf6e6 | 2016-04-16 11:43:02 +0530 | [diff] [blame] | 1750 | |
| 1751 | return flags; |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1752 | } |
| 1753 | |
Shiraz Hashim | 3eaf6e6 | 2016-04-16 11:43:02 +0530 | [diff] [blame] | 1754 | unsigned long __init_memblock memblock_region_resize_late_begin(void) |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1755 | { |
Shiraz Hashim | 3eaf6e6 | 2016-04-16 11:43:02 +0530 | [diff] [blame] | 1756 | return memblock_resize_late(1, 0); |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1757 | } |
| 1758 | |
Shiraz Hashim | 3eaf6e6 | 2016-04-16 11:43:02 +0530 | [diff] [blame] | 1759 | void __init_memblock memblock_region_resize_late_end(unsigned long flags) |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1760 | { |
Shiraz Hashim | 3eaf6e6 | 2016-04-16 11:43:02 +0530 | [diff] [blame] | 1761 | memblock_resize_late(0, flags); |
Shiraz Hashim | e152570 | 2016-02-04 14:53:33 +0530 | [diff] [blame] | 1762 | } |
| 1763 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1764 | static int __init early_memblock(char *p) |
| 1765 | { |
| 1766 | if (p && strstr(p, "debug")) |
| 1767 | memblock_debug = 1; |
| 1768 | return 0; |
| 1769 | } |
| 1770 | early_param("memblock", early_memblock); |
| 1771 | |
Tejun Heo | c378ddd | 2011-07-14 11:46:03 +0200 | [diff] [blame] | 1772 | #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK) |
Benjamin Herrenschmidt | 6d03b88 | 2010-07-06 15:39:19 -0700 | [diff] [blame] | 1773 | |
| 1774 | static int memblock_debug_show(struct seq_file *m, void *private) |
| 1775 | { |
| 1776 | struct memblock_type *type = m->private; |
| 1777 | struct memblock_region *reg; |
| 1778 | int i; |
| 1779 | |
| 1780 | for (i = 0; i < type->cnt; i++) { |
| 1781 | reg = &type->regions[i]; |
| 1782 | seq_printf(m, "%4d: ", i); |
| 1783 | if (sizeof(phys_addr_t) == 4) |
| 1784 | seq_printf(m, "0x%08lx..0x%08lx\n", |
| 1785 | (unsigned long)reg->base, |
| 1786 | (unsigned long)(reg->base + reg->size - 1)); |
| 1787 | else |
| 1788 | seq_printf(m, "0x%016llx..0x%016llx\n", |
| 1789 | (unsigned long long)reg->base, |
| 1790 | (unsigned long long)(reg->base + reg->size - 1)); |
| 1791 | |
| 1792 | } |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
| 1796 | static int memblock_debug_open(struct inode *inode, struct file *file) |
| 1797 | { |
| 1798 | return single_open(file, memblock_debug_show, inode->i_private); |
| 1799 | } |
| 1800 | |
| 1801 | static const struct file_operations memblock_debug_fops = { |
| 1802 | .open = memblock_debug_open, |
| 1803 | .read = seq_read, |
| 1804 | .llseek = seq_lseek, |
| 1805 | .release = single_release, |
| 1806 | }; |
| 1807 | |
| 1808 | static int __init memblock_init_debugfs(void) |
| 1809 | { |
| 1810 | struct dentry *root = debugfs_create_dir("memblock", NULL); |
| 1811 | if (!root) |
| 1812 | return -ENXIO; |
| 1813 | debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops); |
| 1814 | debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops); |
Philipp Hachtmann | 70210ed | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 1815 | #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
| 1816 | debugfs_create_file("physmem", S_IRUGO, root, &memblock.physmem, &memblock_debug_fops); |
| 1817 | #endif |
Benjamin Herrenschmidt | 6d03b88 | 2010-07-06 15:39:19 -0700 | [diff] [blame] | 1818 | |
| 1819 | return 0; |
| 1820 | } |
| 1821 | __initcall(memblock_init_debugfs); |
| 1822 | |
| 1823 | #endif /* CONFIG_DEBUG_FS */ |