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> |
| 22 | |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 23 | #include <asm-generic/sections.h> |
| 24 | |
Tejun Heo | fe091c2 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 25 | static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; |
| 26 | static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; |
| 27 | |
| 28 | struct memblock memblock __initdata_memblock = { |
| 29 | .memory.regions = memblock_memory_init_regions, |
| 30 | .memory.cnt = 1, /* empty dummy entry */ |
| 31 | .memory.max = INIT_MEMBLOCK_REGIONS, |
| 32 | |
| 33 | .reserved.regions = memblock_reserved_init_regions, |
| 34 | .reserved.cnt = 1, /* empty dummy entry */ |
| 35 | .reserved.max = INIT_MEMBLOCK_REGIONS, |
| 36 | |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 37 | .bottom_up = false, |
Tejun Heo | fe091c2 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 38 | .current_limit = MEMBLOCK_ALLOC_ANYWHERE, |
| 39 | }; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 40 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 41 | int memblock_debug __initdata_memblock; |
Tang Chen | 55ac590 | 2014-01-21 15:49:35 -0800 | [diff] [blame] | 42 | #ifdef CONFIG_MOVABLE_NODE |
| 43 | bool movable_node_enabled __initdata_memblock = false; |
| 44 | #endif |
Tejun Heo | 1aadc05 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 45 | static int memblock_can_resize __initdata_memblock; |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 46 | static int memblock_memory_in_slab __initdata_memblock = 0; |
| 47 | static int memblock_reserved_in_slab __initdata_memblock = 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 48 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 49 | /* 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] | 50 | static __init_memblock const char * |
| 51 | memblock_type_name(struct memblock_type *type) |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 52 | { |
| 53 | if (type == &memblock.memory) |
| 54 | return "memory"; |
| 55 | else if (type == &memblock.reserved) |
| 56 | return "reserved"; |
| 57 | else |
| 58 | return "unknown"; |
| 59 | } |
| 60 | |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 61 | /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */ |
| 62 | static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size) |
| 63 | { |
| 64 | return *size = min(*size, (phys_addr_t)ULLONG_MAX - base); |
| 65 | } |
| 66 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 67 | /* |
| 68 | * Address comparison utilities |
| 69 | */ |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 70 | 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] | 71 | phys_addr_t base2, phys_addr_t size2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 72 | { |
| 73 | return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); |
| 74 | } |
| 75 | |
H Hartley Sweeten | 2d7d3eb | 2011-10-31 17:09:15 -0700 | [diff] [blame] | 76 | static long __init_memblock memblock_overlaps_region(struct memblock_type *type, |
| 77 | phys_addr_t base, phys_addr_t size) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 78 | { |
| 79 | unsigned long i; |
| 80 | |
| 81 | for (i = 0; i < type->cnt; i++) { |
| 82 | phys_addr_t rgnbase = type->regions[i].base; |
| 83 | phys_addr_t rgnsize = type->regions[i].size; |
| 84 | if (memblock_addrs_overlap(base, size, rgnbase, rgnsize)) |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | return (i < type->cnt) ? i : -1; |
| 89 | } |
| 90 | |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 91 | /* |
| 92 | * __memblock_find_range_bottom_up - find free area utility in bottom-up |
| 93 | * @start: start of candidate range |
| 94 | * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} |
| 95 | * @size: size of free area to find |
| 96 | * @align: alignment of free area to find |
| 97 | * @nid: nid of the free area to find, %MAX_NUMNODES for any node |
| 98 | * |
| 99 | * Utility called from memblock_find_in_range_node(), find free area bottom-up. |
| 100 | * |
| 101 | * RETURNS: |
| 102 | * Found address on success, 0 on failure. |
| 103 | */ |
| 104 | static phys_addr_t __init_memblock |
| 105 | __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end, |
| 106 | phys_addr_t size, phys_addr_t align, int nid) |
| 107 | { |
| 108 | phys_addr_t this_start, this_end, cand; |
| 109 | u64 i; |
| 110 | |
| 111 | for_each_free_mem_range(i, nid, &this_start, &this_end, NULL) { |
| 112 | this_start = clamp(this_start, start, end); |
| 113 | this_end = clamp(this_end, start, end); |
| 114 | |
| 115 | cand = round_up(this_start, align); |
| 116 | if (cand < this_end && this_end - cand >= size) |
| 117 | return cand; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 123 | /** |
Tang Chen | 1402899 | 2013-11-12 15:07:57 -0800 | [diff] [blame] | 124 | * __memblock_find_range_top_down - find free area utility, in top-down |
| 125 | * @start: start of candidate range |
| 126 | * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} |
| 127 | * @size: size of free area to find |
| 128 | * @align: alignment of free area to find |
| 129 | * @nid: nid of the free area to find, %MAX_NUMNODES for any node |
| 130 | * |
| 131 | * Utility called from memblock_find_in_range_node(), find free area top-down. |
| 132 | * |
| 133 | * RETURNS: |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 134 | * Found address on success, 0 on failure. |
Tang Chen | 1402899 | 2013-11-12 15:07:57 -0800 | [diff] [blame] | 135 | */ |
| 136 | static phys_addr_t __init_memblock |
| 137 | __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end, |
| 138 | phys_addr_t size, phys_addr_t align, int nid) |
| 139 | { |
| 140 | phys_addr_t this_start, this_end, cand; |
| 141 | u64 i; |
| 142 | |
| 143 | for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) { |
| 144 | this_start = clamp(this_start, start, end); |
| 145 | this_end = clamp(this_end, start, end); |
| 146 | |
| 147 | if (this_end < size) |
| 148 | continue; |
| 149 | |
| 150 | cand = round_down(this_end - size, align); |
| 151 | if (cand >= this_start) |
| 152 | return cand; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /** |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 159 | * memblock_find_in_range_node - find free area in given range and node |
| 160 | * @start: start of candidate range |
| 161 | * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} |
| 162 | * @size: size of free area to find |
| 163 | * @align: alignment of free area to find |
| 164 | * @nid: nid of the free area to find, %MAX_NUMNODES for any node |
| 165 | * |
| 166 | * Find @size free area aligned to @align in the specified range and node. |
| 167 | * |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 168 | * When allocation direction is bottom-up, the @start should be greater |
| 169 | * than the end of the kernel image. Otherwise, it will be trimmed. The |
| 170 | * reason is that we want the bottom-up allocation just near the kernel |
| 171 | * image so it is highly likely that the allocated memory and the kernel |
| 172 | * will reside in the same node. |
| 173 | * |
| 174 | * If bottom-up allocation failed, will try to allocate memory top-down. |
| 175 | * |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 176 | * RETURNS: |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 177 | * Found address on success, 0 on failure. |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 178 | */ |
Tang Chen | f7210e6 | 2013-02-22 16:33:51 -0800 | [diff] [blame] | 179 | phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start, |
| 180 | phys_addr_t end, phys_addr_t size, |
| 181 | phys_addr_t align, int nid) |
| 182 | { |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 183 | int ret; |
| 184 | phys_addr_t kernel_end; |
| 185 | |
Tang Chen | f7210e6 | 2013-02-22 16:33:51 -0800 | [diff] [blame] | 186 | /* pump up @end */ |
| 187 | if (end == MEMBLOCK_ALLOC_ACCESSIBLE) |
| 188 | end = memblock.current_limit; |
| 189 | |
| 190 | /* avoid allocating the first page */ |
| 191 | start = max_t(phys_addr_t, start, PAGE_SIZE); |
| 192 | end = max(start, end); |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 193 | kernel_end = __pa_symbol(_end); |
| 194 | |
| 195 | /* |
| 196 | * try bottom-up allocation only when bottom-up mode |
| 197 | * is set and @end is above the kernel image. |
| 198 | */ |
| 199 | if (memblock_bottom_up() && end > kernel_end) { |
| 200 | phys_addr_t bottom_up_start; |
| 201 | |
| 202 | /* make sure we will allocate above the kernel */ |
| 203 | bottom_up_start = max(start, kernel_end); |
| 204 | |
| 205 | /* ok, try bottom-up allocation first */ |
| 206 | ret = __memblock_find_range_bottom_up(bottom_up_start, end, |
| 207 | size, align, nid); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | |
| 211 | /* |
| 212 | * we always limit bottom-up allocation above the kernel, |
| 213 | * but top-down allocation doesn't have the limit, so |
| 214 | * retrying top-down allocation may succeed when bottom-up |
| 215 | * allocation failed. |
| 216 | * |
| 217 | * bottom-up allocation is expected to be fail very rarely, |
| 218 | * so we use WARN_ONCE() here to see the stack trace if |
| 219 | * fail happens. |
| 220 | */ |
| 221 | WARN_ONCE(1, "memblock: bottom-up allocation failed, " |
| 222 | "memory hotunplug may be affected\n"); |
| 223 | } |
Tang Chen | f7210e6 | 2013-02-22 16:33:51 -0800 | [diff] [blame] | 224 | |
Tang Chen | 1402899 | 2013-11-12 15:07:57 -0800 | [diff] [blame] | 225 | return __memblock_find_range_top_down(start, end, size, align, nid); |
Tang Chen | f7210e6 | 2013-02-22 16:33:51 -0800 | [diff] [blame] | 226 | } |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 227 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 228 | /** |
| 229 | * memblock_find_in_range - find free area in given range |
| 230 | * @start: start of candidate range |
| 231 | * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} |
| 232 | * @size: size of free area to find |
| 233 | * @align: alignment of free area to find |
| 234 | * |
| 235 | * Find @size free area aligned to @align in the specified range. |
| 236 | * |
| 237 | * RETURNS: |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 238 | * Found address on success, 0 on failure. |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 239 | */ |
| 240 | phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, |
| 241 | phys_addr_t end, phys_addr_t size, |
| 242 | phys_addr_t align) |
| 243 | { |
| 244 | return memblock_find_in_range_node(start, end, size, align, |
| 245 | MAX_NUMNODES); |
| 246 | } |
| 247 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 248 | 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] | 249 | { |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 250 | type->total_size -= type->regions[r].size; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 251 | memmove(&type->regions[r], &type->regions[r + 1], |
| 252 | (type->cnt - (r + 1)) * sizeof(type->regions[r])); |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 253 | type->cnt--; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 254 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 255 | /* Special case for empty arrays */ |
| 256 | if (type->cnt == 0) { |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 257 | WARN_ON(type->total_size != 0); |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 258 | type->cnt = 1; |
| 259 | type->regions[0].base = 0; |
| 260 | type->regions[0].size = 0; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 261 | type->regions[0].flags = 0; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 262 | memblock_set_region_node(&type->regions[0], MAX_NUMNODES); |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 263 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 264 | } |
| 265 | |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 266 | phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info( |
| 267 | phys_addr_t *addr) |
| 268 | { |
| 269 | if (memblock.reserved.regions == memblock_reserved_init_regions) |
| 270 | return 0; |
| 271 | |
Grygorii Strashko | fd615c4 | 2014-01-21 15:50:05 -0800 | [diff] [blame^] | 272 | /* |
| 273 | * Don't allow nobootmem allocator to free reserved memory regions |
| 274 | * array if |
| 275 | * - CONFIG_DEBUG_FS is enabled; |
| 276 | * - CONFIG_ARCH_DISCARD_MEMBLOCK is not enabled; |
| 277 | * - reserved memory regions array have been resized during boot. |
| 278 | * Otherwise debug_fs entry "sys/kernel/debug/memblock/reserved" |
| 279 | * will show garbage instead of state of memory reservations. |
| 280 | */ |
| 281 | if (IS_ENABLED(CONFIG_DEBUG_FS) && |
| 282 | !IS_ENABLED(CONFIG_ARCH_DISCARD_MEMBLOCK)) |
| 283 | return 0; |
| 284 | |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 285 | *addr = __pa(memblock.reserved.regions); |
| 286 | |
| 287 | return PAGE_ALIGN(sizeof(struct memblock_region) * |
| 288 | memblock.reserved.max); |
| 289 | } |
| 290 | |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 291 | /** |
| 292 | * memblock_double_array - double the size of the memblock regions array |
| 293 | * @type: memblock type of the regions array being doubled |
| 294 | * @new_area_start: starting address of memory range to avoid overlap with |
| 295 | * @new_area_size: size of memory range to avoid overlap with |
| 296 | * |
| 297 | * Double the size of the @type regions array. If memblock is being used to |
| 298 | * allocate memory for a new reserved regions array and there is a previously |
| 299 | * allocated memory range [@new_area_start,@new_area_start+@new_area_size] |
| 300 | * waiting to be reserved, ensure the memory used by the new array does |
| 301 | * not overlap. |
| 302 | * |
| 303 | * RETURNS: |
| 304 | * 0 on success, -1 on failure. |
| 305 | */ |
| 306 | static int __init_memblock memblock_double_array(struct memblock_type *type, |
| 307 | phys_addr_t new_area_start, |
| 308 | phys_addr_t new_area_size) |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 309 | { |
| 310 | struct memblock_region *new_array, *old_array; |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 311 | phys_addr_t old_alloc_size, new_alloc_size; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 312 | phys_addr_t old_size, new_size, addr; |
| 313 | int use_slab = slab_is_available(); |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 314 | int *in_slab; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 315 | |
| 316 | /* We don't allow resizing until we know about the reserved regions |
| 317 | * of memory that aren't suitable for allocation |
| 318 | */ |
| 319 | if (!memblock_can_resize) |
| 320 | return -1; |
| 321 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 322 | /* Calculate new doubled size */ |
| 323 | old_size = type->max * sizeof(struct memblock_region); |
| 324 | new_size = old_size << 1; |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 325 | /* |
| 326 | * We need to allocated new one align to PAGE_SIZE, |
| 327 | * so we can free them completely later. |
| 328 | */ |
| 329 | old_alloc_size = PAGE_ALIGN(old_size); |
| 330 | new_alloc_size = PAGE_ALIGN(new_size); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 331 | |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 332 | /* Retrieve the slab flag */ |
| 333 | if (type == &memblock.memory) |
| 334 | in_slab = &memblock_memory_in_slab; |
| 335 | else |
| 336 | in_slab = &memblock_reserved_in_slab; |
| 337 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 338 | /* Try to find some space for it. |
| 339 | * |
| 340 | * 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] | 341 | * we use MEMBLOCK for allocations. That means that this is unsafe to |
| 342 | * use when bootmem is currently active (unless bootmem itself is |
| 343 | * implemented on top of MEMBLOCK which isn't the case yet) |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 344 | * |
| 345 | * 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] | 346 | * call into MEMBLOCK while it's still active, or much later when slab |
| 347 | * is active for memory hotplug operations |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 348 | */ |
| 349 | if (use_slab) { |
| 350 | new_array = kmalloc(new_size, GFP_KERNEL); |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 351 | addr = new_array ? __pa(new_array) : 0; |
Gavin Shan | 4e2f077 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 352 | } else { |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 353 | /* only exclude range when trying to double reserved.regions */ |
| 354 | if (type != &memblock.reserved) |
| 355 | new_area_start = new_area_size = 0; |
| 356 | |
| 357 | addr = memblock_find_in_range(new_area_start + new_area_size, |
| 358 | memblock.current_limit, |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 359 | new_alloc_size, PAGE_SIZE); |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 360 | if (!addr && new_area_size) |
| 361 | addr = memblock_find_in_range(0, |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 362 | min(new_area_start, memblock.current_limit), |
| 363 | new_alloc_size, PAGE_SIZE); |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 364 | |
Sachin Kamat | 1567486 | 2012-09-04 13:55:05 +0530 | [diff] [blame] | 365 | new_array = addr ? __va(addr) : NULL; |
Gavin Shan | 4e2f077 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 366 | } |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 367 | if (!addr) { |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 368 | pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", |
| 369 | memblock_type_name(type), type->max, type->max * 2); |
| 370 | return -1; |
| 371 | } |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 372 | |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 373 | memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]", |
| 374 | memblock_type_name(type), type->max * 2, (u64)addr, |
| 375 | (u64)addr + new_size - 1); |
Yinghai Lu | ea9e437 | 2010-07-28 15:13:22 +1000 | [diff] [blame] | 376 | |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 377 | /* |
| 378 | * Found space, we now need to move the array over before we add the |
| 379 | * reserved region since it may be our reserved array itself that is |
| 380 | * full. |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 381 | */ |
| 382 | memcpy(new_array, type->regions, old_size); |
| 383 | memset(new_array + type->max, 0, old_size); |
| 384 | old_array = type->regions; |
| 385 | type->regions = new_array; |
| 386 | type->max <<= 1; |
| 387 | |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 388 | /* 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] | 389 | if (*in_slab) |
| 390 | kfree(old_array); |
| 391 | else if (old_array != memblock_memory_init_regions && |
| 392 | old_array != memblock_reserved_init_regions) |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 393 | memblock_free(__pa(old_array), old_alloc_size); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 394 | |
Andrew Morton | fd07383 | 2012-07-31 16:42:40 -0700 | [diff] [blame] | 395 | /* |
| 396 | * Reserve the new array if that comes from the memblock. Otherwise, we |
| 397 | * needn't do it |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 398 | */ |
| 399 | if (!use_slab) |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 400 | BUG_ON(memblock_reserve(addr, new_alloc_size)); |
Gavin Shan | 181eb39 | 2012-05-29 15:06:50 -0700 | [diff] [blame] | 401 | |
| 402 | /* Update slab flag */ |
| 403 | *in_slab = use_slab; |
| 404 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 408 | /** |
| 409 | * memblock_merge_regions - merge neighboring compatible regions |
| 410 | * @type: memblock type to scan |
| 411 | * |
| 412 | * Scan @type and merge neighboring compatible regions. |
| 413 | */ |
| 414 | static void __init_memblock memblock_merge_regions(struct memblock_type *type) |
| 415 | { |
| 416 | int i = 0; |
| 417 | |
| 418 | /* cnt never goes below 1 */ |
| 419 | while (i < type->cnt - 1) { |
| 420 | struct memblock_region *this = &type->regions[i]; |
| 421 | struct memblock_region *next = &type->regions[i + 1]; |
| 422 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 423 | if (this->base + this->size != next->base || |
| 424 | memblock_get_region_node(this) != |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 425 | memblock_get_region_node(next) || |
| 426 | this->flags != next->flags) { |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 427 | BUG_ON(this->base + this->size > next->base); |
| 428 | i++; |
| 429 | continue; |
| 430 | } |
| 431 | |
| 432 | this->size += next->size; |
Lin Feng | c0232ae | 2013-01-11 14:31:44 -0800 | [diff] [blame] | 433 | /* move forward from next + 1, index of which is i + 2 */ |
| 434 | memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next)); |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 435 | type->cnt--; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * memblock_insert_region - insert new memblock region |
Tang Chen | 209ff86 | 2013-04-29 15:08:41 -0700 | [diff] [blame] | 441 | * @type: memblock type to insert into |
| 442 | * @idx: index for the insertion point |
| 443 | * @base: base address of the new region |
| 444 | * @size: size of the new region |
| 445 | * @nid: node id of the new region |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 446 | * @flags: flags of the new region |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 447 | * |
| 448 | * Insert new memblock region [@base,@base+@size) into @type at @idx. |
| 449 | * @type must already have extra room to accomodate the new region. |
| 450 | */ |
| 451 | static void __init_memblock memblock_insert_region(struct memblock_type *type, |
| 452 | int idx, phys_addr_t base, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 453 | phys_addr_t size, |
| 454 | int nid, unsigned long flags) |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 455 | { |
| 456 | struct memblock_region *rgn = &type->regions[idx]; |
| 457 | |
| 458 | BUG_ON(type->cnt >= type->max); |
| 459 | memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); |
| 460 | rgn->base = base; |
| 461 | rgn->size = size; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 462 | rgn->flags = flags; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 463 | memblock_set_region_node(rgn, nid); |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 464 | type->cnt++; |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 465 | type->total_size += size; |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /** |
| 469 | * memblock_add_region - add new memblock region |
| 470 | * @type: memblock type to add new region into |
| 471 | * @base: base address of the new region |
| 472 | * @size: size of the new region |
Tejun Heo | 7fb0bc3 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 473 | * @nid: nid of the new region |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 474 | * @flags: flags of the new region |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 475 | * |
| 476 | * Add new memblock region [@base,@base+@size) into @type. The new region |
| 477 | * is allowed to overlap with existing ones - overlaps don't affect already |
| 478 | * existing regions. @type is guaranteed to be minimal (all neighbouring |
| 479 | * compatible regions are merged) after the addition. |
| 480 | * |
| 481 | * RETURNS: |
| 482 | * 0 on success, -errno on failure. |
| 483 | */ |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 484 | static int __init_memblock memblock_add_region(struct memblock_type *type, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 485 | phys_addr_t base, phys_addr_t size, |
| 486 | int nid, unsigned long flags) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 487 | { |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 488 | bool insert = false; |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 489 | phys_addr_t obase = base; |
| 490 | phys_addr_t end = base + memblock_cap_size(base, &size); |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 491 | int i, nr_new; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 492 | |
Tejun Heo | b3dc627 | 2012-04-20 08:31:34 -0700 | [diff] [blame] | 493 | if (!size) |
| 494 | return 0; |
| 495 | |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 496 | /* special case for empty array */ |
| 497 | if (type->regions[0].size == 0) { |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 498 | WARN_ON(type->cnt != 1 || type->total_size); |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 499 | type->regions[0].base = base; |
| 500 | type->regions[0].size = size; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 501 | type->regions[0].flags = flags; |
Tejun Heo | 7fb0bc3 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 502 | memblock_set_region_node(&type->regions[0], nid); |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 503 | type->total_size = size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 504 | return 0; |
| 505 | } |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 506 | repeat: |
| 507 | /* |
| 508 | * The following is executed twice. Once with %false @insert and |
| 509 | * then with %true. The first counts the number of regions needed |
| 510 | * to accomodate the new area. The second actually inserts them. |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 511 | */ |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 512 | base = obase; |
| 513 | nr_new = 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 514 | |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 515 | for (i = 0; i < type->cnt; i++) { |
| 516 | struct memblock_region *rgn = &type->regions[i]; |
| 517 | phys_addr_t rbase = rgn->base; |
| 518 | phys_addr_t rend = rbase + rgn->size; |
| 519 | |
| 520 | if (rbase >= end) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 521 | break; |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 522 | if (rend <= base) |
| 523 | continue; |
| 524 | /* |
| 525 | * @rgn overlaps. If it separates the lower part of new |
| 526 | * area, insert that portion. |
| 527 | */ |
| 528 | if (rbase > base) { |
| 529 | nr_new++; |
| 530 | if (insert) |
| 531 | memblock_insert_region(type, i++, base, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 532 | rbase - base, nid, |
| 533 | flags); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 534 | } |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 535 | /* area below @rend is dealt with, forget about it */ |
| 536 | base = min(rend, end); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 537 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 538 | |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 539 | /* insert the remaining portion */ |
| 540 | if (base < end) { |
| 541 | nr_new++; |
| 542 | if (insert) |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 543 | memblock_insert_region(type, i, base, end - base, |
| 544 | nid, flags); |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* |
| 548 | * If this was the first round, resize array and repeat for actual |
| 549 | * insertions; otherwise, merge and return. |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 550 | */ |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 551 | if (!insert) { |
| 552 | while (type->cnt + nr_new > type->max) |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 553 | if (memblock_double_array(type, obase, size) < 0) |
Tejun Heo | 784656f9 | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 554 | return -ENOMEM; |
| 555 | insert = true; |
| 556 | goto repeat; |
| 557 | } else { |
| 558 | memblock_merge_regions(type); |
| 559 | return 0; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 560 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 561 | } |
| 562 | |
Tejun Heo | 7fb0bc3 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 563 | int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size, |
| 564 | int nid) |
| 565 | { |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 566 | return memblock_add_region(&memblock.memory, base, size, nid, 0); |
Tejun Heo | 7fb0bc3 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 569 | int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 570 | { |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 571 | return memblock_add_region(&memblock.memory, base, size, |
| 572 | MAX_NUMNODES, 0); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 573 | } |
| 574 | |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 575 | /** |
| 576 | * memblock_isolate_range - isolate given range into disjoint memblocks |
| 577 | * @type: memblock type to isolate range for |
| 578 | * @base: base of range to isolate |
| 579 | * @size: size of range to isolate |
| 580 | * @start_rgn: out parameter for the start of isolated region |
| 581 | * @end_rgn: out parameter for the end of isolated region |
| 582 | * |
| 583 | * Walk @type and ensure that regions don't cross the boundaries defined by |
| 584 | * [@base,@base+@size). Crossing regions are split at the boundaries, |
| 585 | * which may create at most two more regions. The index of the first |
| 586 | * region inside the range is returned in *@start_rgn and end in *@end_rgn. |
| 587 | * |
| 588 | * RETURNS: |
| 589 | * 0 on success, -errno on failure. |
| 590 | */ |
| 591 | static int __init_memblock memblock_isolate_range(struct memblock_type *type, |
| 592 | phys_addr_t base, phys_addr_t size, |
| 593 | int *start_rgn, int *end_rgn) |
| 594 | { |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 595 | phys_addr_t end = base + memblock_cap_size(base, &size); |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 596 | int i; |
| 597 | |
| 598 | *start_rgn = *end_rgn = 0; |
| 599 | |
Tejun Heo | b3dc627 | 2012-04-20 08:31:34 -0700 | [diff] [blame] | 600 | if (!size) |
| 601 | return 0; |
| 602 | |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 603 | /* we'll create at most two more regions */ |
| 604 | while (type->cnt + 2 > type->max) |
Greg Pearson | 48c3b58 | 2012-06-20 12:53:05 -0700 | [diff] [blame] | 605 | if (memblock_double_array(type, base, size) < 0) |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 606 | return -ENOMEM; |
| 607 | |
| 608 | for (i = 0; i < type->cnt; i++) { |
| 609 | struct memblock_region *rgn = &type->regions[i]; |
| 610 | phys_addr_t rbase = rgn->base; |
| 611 | phys_addr_t rend = rbase + rgn->size; |
| 612 | |
| 613 | if (rbase >= end) |
| 614 | break; |
| 615 | if (rend <= base) |
| 616 | continue; |
| 617 | |
| 618 | if (rbase < base) { |
| 619 | /* |
| 620 | * @rgn intersects from below. Split and continue |
| 621 | * to process the next region - the new top half. |
| 622 | */ |
| 623 | rgn->base = base; |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 624 | rgn->size -= base - rbase; |
| 625 | type->total_size -= base - rbase; |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 626 | memblock_insert_region(type, i, rbase, base - rbase, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 627 | memblock_get_region_node(rgn), |
| 628 | rgn->flags); |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 629 | } else if (rend > end) { |
| 630 | /* |
| 631 | * @rgn intersects from above. Split and redo the |
| 632 | * current region - the new bottom half. |
| 633 | */ |
| 634 | rgn->base = end; |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 635 | rgn->size -= end - rbase; |
| 636 | type->total_size -= end - rbase; |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 637 | memblock_insert_region(type, i--, rbase, end - rbase, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 638 | memblock_get_region_node(rgn), |
| 639 | rgn->flags); |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 640 | } else { |
| 641 | /* @rgn is fully contained, record it */ |
| 642 | if (!*end_rgn) |
| 643 | *start_rgn = i; |
| 644 | *end_rgn = i + 1; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return 0; |
| 649 | } |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 650 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 651 | static int __init_memblock __memblock_remove(struct memblock_type *type, |
| 652 | phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 653 | { |
Tejun Heo | 7193618 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 654 | int start_rgn, end_rgn; |
| 655 | int i, ret; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 656 | |
Tejun Heo | 7193618 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 657 | ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); |
| 658 | if (ret) |
| 659 | return ret; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 660 | |
Tejun Heo | 7193618 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 661 | for (i = end_rgn - 1; i >= start_rgn; i--) |
| 662 | memblock_remove_region(type, i); |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 663 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 664 | } |
| 665 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 666 | 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] | 667 | { |
| 668 | return __memblock_remove(&memblock.memory, base, size); |
| 669 | } |
| 670 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 671 | 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] | 672 | { |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 673 | memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n", |
H. Peter Anvin | a150439 | 2011-07-14 11:57:10 -0700 | [diff] [blame] | 674 | (unsigned long long)base, |
Grygorii Strashko | 931d13f | 2014-01-21 15:49:17 -0800 | [diff] [blame] | 675 | (unsigned long long)base + size - 1, |
H. Peter Anvin | a150439 | 2011-07-14 11:57:10 -0700 | [diff] [blame] | 676 | (void *)_RET_IP_); |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 677 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 678 | return __memblock_remove(&memblock.reserved, base, size); |
| 679 | } |
| 680 | |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 681 | static int __init_memblock memblock_reserve_region(phys_addr_t base, |
| 682 | phys_addr_t size, |
| 683 | int nid, |
| 684 | unsigned long flags) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 685 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 686 | struct memblock_type *_rgn = &memblock.reserved; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 687 | |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 688 | memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n", |
H. Peter Anvin | a150439 | 2011-07-14 11:57:10 -0700 | [diff] [blame] | 689 | (unsigned long long)base, |
Grygorii Strashko | 931d13f | 2014-01-21 15:49:17 -0800 | [diff] [blame] | 690 | (unsigned long long)base + size - 1, |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 691 | flags, (void *)_RET_IP_); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 692 | |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 693 | return memblock_add_region(_rgn, base, size, nid, flags); |
| 694 | } |
| 695 | |
| 696 | int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) |
| 697 | { |
| 698 | return memblock_reserve_region(base, size, MAX_NUMNODES, 0); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 699 | } |
| 700 | |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 701 | /** |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 702 | * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG. |
| 703 | * @base: the base phys addr of the region |
| 704 | * @size: the size of the region |
| 705 | * |
| 706 | * This function isolates region [@base, @base + @size), and mark it with flag |
| 707 | * MEMBLOCK_HOTPLUG. |
| 708 | * |
| 709 | * Return 0 on succees, -errno on failure. |
| 710 | */ |
| 711 | int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size) |
| 712 | { |
| 713 | struct memblock_type *type = &memblock.memory; |
| 714 | int i, ret, start_rgn, end_rgn; |
| 715 | |
| 716 | ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); |
| 717 | if (ret) |
| 718 | return ret; |
| 719 | |
| 720 | for (i = start_rgn; i < end_rgn; i++) |
| 721 | memblock_set_region_flags(&type->regions[i], MEMBLOCK_HOTPLUG); |
| 722 | |
| 723 | memblock_merge_regions(type); |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region. |
| 729 | * @base: the base phys addr of the region |
| 730 | * @size: the size of the region |
| 731 | * |
| 732 | * This function isolates region [@base, @base + @size), and clear flag |
| 733 | * MEMBLOCK_HOTPLUG for the isolated regions. |
| 734 | * |
| 735 | * Return 0 on succees, -errno on failure. |
| 736 | */ |
| 737 | int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size) |
| 738 | { |
| 739 | struct memblock_type *type = &memblock.memory; |
| 740 | int i, ret, start_rgn, end_rgn; |
| 741 | |
| 742 | ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); |
| 743 | if (ret) |
| 744 | return ret; |
| 745 | |
| 746 | for (i = start_rgn; i < end_rgn; i++) |
| 747 | memblock_clear_region_flags(&type->regions[i], |
| 748 | MEMBLOCK_HOTPLUG); |
| 749 | |
| 750 | memblock_merge_regions(type); |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /** |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 755 | * __next_free_mem_range - next function for for_each_free_mem_range() |
| 756 | * @idx: pointer to u64 loop variable |
Tang Chen | d8bbdd7 | 2013-07-08 16:00:22 -0700 | [diff] [blame] | 757 | * @nid: node selector, %MAX_NUMNODES for all nodes |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 758 | * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 759 | * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 760 | * @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] | 761 | * |
| 762 | * Find the first free area from *@idx which matches @nid, fill the out |
| 763 | * parameters, and update *@idx for the next iteration. The lower 32bit of |
| 764 | * *@idx contains index into memory region and the upper 32bit indexes the |
| 765 | * areas before each reserved region. For example, if reserved regions |
| 766 | * look like the following, |
| 767 | * |
| 768 | * 0:[0-16), 1:[32-48), 2:[128-130) |
| 769 | * |
| 770 | * The upper 32bit indexes the following regions. |
| 771 | * |
| 772 | * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) |
| 773 | * |
| 774 | * As both region arrays are sorted, the function advances the two indices |
| 775 | * in lockstep and returns each intersection. |
| 776 | */ |
| 777 | void __init_memblock __next_free_mem_range(u64 *idx, int nid, |
| 778 | phys_addr_t *out_start, |
| 779 | phys_addr_t *out_end, int *out_nid) |
| 780 | { |
| 781 | struct memblock_type *mem = &memblock.memory; |
| 782 | struct memblock_type *rsv = &memblock.reserved; |
| 783 | int mi = *idx & 0xffffffff; |
| 784 | int ri = *idx >> 32; |
| 785 | |
| 786 | for ( ; mi < mem->cnt; mi++) { |
| 787 | struct memblock_region *m = &mem->regions[mi]; |
| 788 | phys_addr_t m_start = m->base; |
| 789 | phys_addr_t m_end = m->base + m->size; |
| 790 | |
| 791 | /* only memory regions are associated with nodes, check it */ |
| 792 | if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m)) |
| 793 | continue; |
| 794 | |
| 795 | /* scan areas before each reservation for intersection */ |
| 796 | for ( ; ri < rsv->cnt + 1; ri++) { |
| 797 | struct memblock_region *r = &rsv->regions[ri]; |
| 798 | phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0; |
| 799 | phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX; |
| 800 | |
| 801 | /* if ri advanced past mi, break out to advance mi */ |
| 802 | if (r_start >= m_end) |
| 803 | break; |
| 804 | /* if the two regions intersect, we're done */ |
| 805 | if (m_start < r_end) { |
| 806 | if (out_start) |
| 807 | *out_start = max(m_start, r_start); |
| 808 | if (out_end) |
| 809 | *out_end = min(m_end, r_end); |
| 810 | if (out_nid) |
| 811 | *out_nid = memblock_get_region_node(m); |
| 812 | /* |
| 813 | * The region which ends first is advanced |
| 814 | * for the next iteration. |
| 815 | */ |
| 816 | if (m_end <= r_end) |
| 817 | mi++; |
| 818 | else |
| 819 | ri++; |
| 820 | *idx = (u32)mi | (u64)ri << 32; |
| 821 | return; |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /* signal end of iteration */ |
| 827 | *idx = ULLONG_MAX; |
| 828 | } |
| 829 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 830 | /** |
| 831 | * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse() |
| 832 | * @idx: pointer to u64 loop variable |
| 833 | * @nid: nid: node selector, %MAX_NUMNODES for all nodes |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 834 | * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 835 | * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 836 | * @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] | 837 | * |
| 838 | * Reverse of __next_free_mem_range(). |
Tang Chen | 55ac590 | 2014-01-21 15:49:35 -0800 | [diff] [blame] | 839 | * |
| 840 | * Linux kernel cannot migrate pages used by itself. Memory hotplug users won't |
| 841 | * be able to hot-remove hotpluggable memory used by the kernel. So this |
| 842 | * function skip hotpluggable regions if needed when allocating memory for the |
| 843 | * kernel. |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 844 | */ |
| 845 | void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid, |
| 846 | phys_addr_t *out_start, |
| 847 | phys_addr_t *out_end, int *out_nid) |
| 848 | { |
| 849 | struct memblock_type *mem = &memblock.memory; |
| 850 | struct memblock_type *rsv = &memblock.reserved; |
| 851 | int mi = *idx & 0xffffffff; |
| 852 | int ri = *idx >> 32; |
| 853 | |
| 854 | if (*idx == (u64)ULLONG_MAX) { |
| 855 | mi = mem->cnt - 1; |
| 856 | ri = rsv->cnt; |
| 857 | } |
| 858 | |
| 859 | for ( ; mi >= 0; mi--) { |
| 860 | struct memblock_region *m = &mem->regions[mi]; |
| 861 | phys_addr_t m_start = m->base; |
| 862 | phys_addr_t m_end = m->base + m->size; |
| 863 | |
| 864 | /* only memory regions are associated with nodes, check it */ |
| 865 | if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m)) |
| 866 | continue; |
| 867 | |
Tang Chen | 55ac590 | 2014-01-21 15:49:35 -0800 | [diff] [blame] | 868 | /* skip hotpluggable memory regions if needed */ |
| 869 | if (movable_node_is_enabled() && memblock_is_hotpluggable(m)) |
| 870 | continue; |
| 871 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 872 | /* scan areas before each reservation for intersection */ |
| 873 | for ( ; ri >= 0; ri--) { |
| 874 | struct memblock_region *r = &rsv->regions[ri]; |
| 875 | phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0; |
| 876 | phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX; |
| 877 | |
| 878 | /* if ri advanced past mi, break out to advance mi */ |
| 879 | if (r_end <= m_start) |
| 880 | break; |
| 881 | /* if the two regions intersect, we're done */ |
| 882 | if (m_end > r_start) { |
| 883 | if (out_start) |
| 884 | *out_start = max(m_start, r_start); |
| 885 | if (out_end) |
| 886 | *out_end = min(m_end, r_end); |
| 887 | if (out_nid) |
| 888 | *out_nid = memblock_get_region_node(m); |
| 889 | |
| 890 | if (m_start >= r_start) |
| 891 | mi--; |
| 892 | else |
| 893 | ri--; |
| 894 | *idx = (u32)mi | (u64)ri << 32; |
| 895 | return; |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | *idx = ULLONG_MAX; |
| 901 | } |
| 902 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 903 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 904 | /* |
| 905 | * Common iterator interface used to define for_each_mem_range(). |
| 906 | */ |
| 907 | void __init_memblock __next_mem_pfn_range(int *idx, int nid, |
| 908 | unsigned long *out_start_pfn, |
| 909 | unsigned long *out_end_pfn, int *out_nid) |
| 910 | { |
| 911 | struct memblock_type *type = &memblock.memory; |
| 912 | struct memblock_region *r; |
| 913 | |
| 914 | while (++*idx < type->cnt) { |
| 915 | r = &type->regions[*idx]; |
| 916 | |
| 917 | if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) |
| 918 | continue; |
| 919 | if (nid == MAX_NUMNODES || nid == r->nid) |
| 920 | break; |
| 921 | } |
| 922 | if (*idx >= type->cnt) { |
| 923 | *idx = -1; |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | if (out_start_pfn) |
| 928 | *out_start_pfn = PFN_UP(r->base); |
| 929 | if (out_end_pfn) |
| 930 | *out_end_pfn = PFN_DOWN(r->base + r->size); |
| 931 | if (out_nid) |
| 932 | *out_nid = r->nid; |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * memblock_set_node - set node ID on memblock regions |
| 937 | * @base: base of area to set node ID for |
| 938 | * @size: size of area to set node ID for |
Tang Chen | e7e8de5 | 2014-01-21 15:49:26 -0800 | [diff] [blame] | 939 | * @type: memblock type to set node ID for |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 940 | * @nid: node ID to set |
| 941 | * |
Tang Chen | e7e8de5 | 2014-01-21 15:49:26 -0800 | [diff] [blame] | 942 | * 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] | 943 | * Regions which cross the area boundaries are split as necessary. |
| 944 | * |
| 945 | * RETURNS: |
| 946 | * 0 on success, -errno on failure. |
| 947 | */ |
| 948 | 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] | 949 | struct memblock_type *type, int nid) |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 950 | { |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 951 | int start_rgn, end_rgn; |
| 952 | int i, ret; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 953 | |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 954 | ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); |
| 955 | if (ret) |
| 956 | return ret; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 957 | |
Tejun Heo | 6a9ceb3 | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 958 | for (i = start_rgn; i < end_rgn; i++) |
Wanpeng Li | e9d24ad | 2012-10-08 16:32:21 -0700 | [diff] [blame] | 959 | memblock_set_region_node(&type->regions[i], nid); |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 960 | |
| 961 | memblock_merge_regions(type); |
| 962 | return 0; |
| 963 | } |
| 964 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
| 965 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 966 | static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size, |
| 967 | phys_addr_t align, phys_addr_t max_addr, |
| 968 | int nid) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 969 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 970 | phys_addr_t found; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 971 | |
Vineet Gupta | 94f3d3a | 2013-04-29 15:06:15 -0700 | [diff] [blame] | 972 | if (WARN_ON(!align)) |
| 973 | align = __alignof__(long long); |
| 974 | |
Tejun Heo | 847854f | 2012-02-29 05:56:21 +0900 | [diff] [blame] | 975 | /* align @size to avoid excessive fragmentation on reserved array */ |
| 976 | size = round_up(size, align); |
| 977 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 978 | found = memblock_find_in_range_node(0, max_addr, size, align, nid); |
Tejun Heo | 9c8c27e | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 979 | if (found && !memblock_reserve(found, size)) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 980 | return found; |
| 981 | |
| 982 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 983 | } |
| 984 | |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 985 | phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid) |
| 986 | { |
| 987 | return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid); |
| 988 | } |
| 989 | |
| 990 | phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
| 991 | { |
| 992 | return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES); |
| 993 | } |
| 994 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 995 | 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] | 996 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 997 | phys_addr_t alloc; |
| 998 | |
| 999 | alloc = __memblock_alloc_base(size, align, max_addr); |
| 1000 | |
| 1001 | if (alloc == 0) |
| 1002 | panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n", |
| 1003 | (unsigned long long) size, (unsigned long long) max_addr); |
| 1004 | |
| 1005 | return alloc; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1006 | } |
| 1007 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1008 | 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] | 1009 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1010 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1011 | } |
| 1012 | |
Benjamin Herrenschmidt | 9d1e249 | 2010-07-06 15:39:17 -0700 | [diff] [blame] | 1013 | phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) |
| 1014 | { |
| 1015 | phys_addr_t res = memblock_alloc_nid(size, align, nid); |
| 1016 | |
| 1017 | if (res) |
| 1018 | return res; |
Tejun Heo | 15fb097 | 2011-07-12 09:58:07 +0200 | [diff] [blame] | 1019 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1020 | } |
| 1021 | |
Benjamin Herrenschmidt | 9d1e249 | 2010-07-06 15:39:17 -0700 | [diff] [blame] | 1022 | |
| 1023 | /* |
| 1024 | * Remaining API functions |
| 1025 | */ |
| 1026 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 1027 | phys_addr_t __init memblock_phys_mem_size(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1028 | { |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 1029 | return memblock.memory.total_size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1030 | } |
| 1031 | |
Yinghai Lu | 595ad9a | 2013-01-24 12:20:09 -0800 | [diff] [blame] | 1032 | phys_addr_t __init memblock_mem_size(unsigned long limit_pfn) |
| 1033 | { |
| 1034 | unsigned long pages = 0; |
| 1035 | struct memblock_region *r; |
| 1036 | unsigned long start_pfn, end_pfn; |
| 1037 | |
| 1038 | for_each_memblock(memory, r) { |
| 1039 | start_pfn = memblock_region_memory_base_pfn(r); |
| 1040 | end_pfn = memblock_region_memory_end_pfn(r); |
| 1041 | start_pfn = min_t(unsigned long, start_pfn, limit_pfn); |
| 1042 | end_pfn = min_t(unsigned long, end_pfn, limit_pfn); |
| 1043 | pages += end_pfn - start_pfn; |
| 1044 | } |
| 1045 | |
| 1046 | return (phys_addr_t)pages << PAGE_SHIFT; |
| 1047 | } |
| 1048 | |
Sam Ravnborg | 0a93ebe | 2011-10-31 17:08:16 -0700 | [diff] [blame] | 1049 | /* lowest address */ |
| 1050 | phys_addr_t __init_memblock memblock_start_of_DRAM(void) |
| 1051 | { |
| 1052 | return memblock.memory.regions[0].base; |
| 1053 | } |
| 1054 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 1055 | phys_addr_t __init_memblock memblock_end_of_DRAM(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1056 | { |
| 1057 | int idx = memblock.memory.cnt - 1; |
| 1058 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 1059 | return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1060 | } |
| 1061 | |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1062 | void __init memblock_enforce_memory_limit(phys_addr_t limit) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1063 | { |
| 1064 | unsigned long i; |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1065 | phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1066 | |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1067 | if (!limit) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1068 | return; |
| 1069 | |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1070 | /* find out max address */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1071 | for (i = 0; i < memblock.memory.cnt; i++) { |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1072 | struct memblock_region *r = &memblock.memory.regions[i]; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1073 | |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1074 | if (limit <= r->size) { |
| 1075 | max_addr = r->base + limit; |
| 1076 | break; |
| 1077 | } |
| 1078 | limit -= r->size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1079 | } |
| 1080 | |
Tejun Heo | c0ce8fe | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1081 | /* truncate both memory and reserved regions */ |
| 1082 | __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX); |
| 1083 | __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1084 | } |
| 1085 | |
Yinghai Lu | cd79481 | 2010-10-11 12:34:09 -0700 | [diff] [blame] | 1086 | static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 1087 | { |
| 1088 | unsigned int left = 0, right = type->cnt; |
| 1089 | |
| 1090 | do { |
| 1091 | unsigned int mid = (right + left) / 2; |
| 1092 | |
| 1093 | if (addr < type->regions[mid].base) |
| 1094 | right = mid; |
| 1095 | else if (addr >= (type->regions[mid].base + |
| 1096 | type->regions[mid].size)) |
| 1097 | left = mid + 1; |
| 1098 | else |
| 1099 | return mid; |
| 1100 | } while (left < right); |
| 1101 | return -1; |
| 1102 | } |
| 1103 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 1104 | int __init memblock_is_reserved(phys_addr_t addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1105 | { |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 1106 | return memblock_search(&memblock.reserved, addr) != -1; |
| 1107 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1108 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 1109 | int __init_memblock memblock_is_memory(phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 1110 | { |
| 1111 | return memblock_search(&memblock.memory, addr) != -1; |
| 1112 | } |
| 1113 | |
Yinghai Lu | e76b63f | 2013-09-11 14:22:17 -0700 | [diff] [blame] | 1114 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 1115 | int __init_memblock memblock_search_pfn_nid(unsigned long pfn, |
| 1116 | unsigned long *start_pfn, unsigned long *end_pfn) |
| 1117 | { |
| 1118 | struct memblock_type *type = &memblock.memory; |
| 1119 | int mid = memblock_search(type, (phys_addr_t)pfn << PAGE_SHIFT); |
| 1120 | |
| 1121 | if (mid == -1) |
| 1122 | return -1; |
| 1123 | |
| 1124 | *start_pfn = type->regions[mid].base >> PAGE_SHIFT; |
| 1125 | *end_pfn = (type->regions[mid].base + type->regions[mid].size) |
| 1126 | >> PAGE_SHIFT; |
| 1127 | |
| 1128 | return type->regions[mid].nid; |
| 1129 | } |
| 1130 | #endif |
| 1131 | |
Stephen Boyd | eab3094 | 2012-05-24 00:45:21 -0700 | [diff] [blame] | 1132 | /** |
| 1133 | * memblock_is_region_memory - check if a region is a subset of memory |
| 1134 | * @base: base of region to check |
| 1135 | * @size: size of region to check |
| 1136 | * |
| 1137 | * Check if the region [@base, @base+@size) is a subset of a memory block. |
| 1138 | * |
| 1139 | * RETURNS: |
| 1140 | * 0 if false, non-zero if true |
| 1141 | */ |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 1142 | 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] | 1143 | { |
Tomi Valkeinen | abb6527 | 2011-01-20 14:44:20 -0800 | [diff] [blame] | 1144 | int idx = memblock_search(&memblock.memory, base); |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1145 | phys_addr_t end = base + memblock_cap_size(base, &size); |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 1146 | |
| 1147 | if (idx == -1) |
| 1148 | return 0; |
Tomi Valkeinen | abb6527 | 2011-01-20 14:44:20 -0800 | [diff] [blame] | 1149 | return memblock.memory.regions[idx].base <= base && |
| 1150 | (memblock.memory.regions[idx].base + |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1151 | memblock.memory.regions[idx].size) >= end; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1152 | } |
| 1153 | |
Stephen Boyd | eab3094 | 2012-05-24 00:45:21 -0700 | [diff] [blame] | 1154 | /** |
| 1155 | * memblock_is_region_reserved - check if a region intersects reserved memory |
| 1156 | * @base: base of region to check |
| 1157 | * @size: size of region to check |
| 1158 | * |
| 1159 | * Check if the region [@base, @base+@size) intersects a reserved memory block. |
| 1160 | * |
| 1161 | * RETURNS: |
| 1162 | * 0 if false, non-zero if true |
| 1163 | */ |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 1164 | int __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] | 1165 | { |
Tejun Heo | eb18f1b | 2011-12-08 10:22:07 -0800 | [diff] [blame] | 1166 | memblock_cap_size(base, &size); |
Benjamin Herrenschmidt | f1c2c19 | 2010-08-04 14:17:17 +1000 | [diff] [blame] | 1167 | return memblock_overlaps_region(&memblock.reserved, base, size) >= 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1168 | } |
| 1169 | |
Yinghai Lu | 6ede1fd | 2012-10-22 16:35:18 -0700 | [diff] [blame] | 1170 | void __init_memblock memblock_trim_memory(phys_addr_t align) |
| 1171 | { |
| 1172 | int i; |
| 1173 | phys_addr_t start, end, orig_start, orig_end; |
| 1174 | struct memblock_type *mem = &memblock.memory; |
| 1175 | |
| 1176 | for (i = 0; i < mem->cnt; i++) { |
| 1177 | orig_start = mem->regions[i].base; |
| 1178 | orig_end = mem->regions[i].base + mem->regions[i].size; |
| 1179 | start = round_up(orig_start, align); |
| 1180 | end = round_down(orig_end, align); |
| 1181 | |
| 1182 | if (start == orig_start && end == orig_end) |
| 1183 | continue; |
| 1184 | |
| 1185 | if (start < end) { |
| 1186 | mem->regions[i].base = start; |
| 1187 | mem->regions[i].size = end - start; |
| 1188 | } else { |
| 1189 | memblock_remove_region(mem, i); |
| 1190 | i--; |
| 1191 | } |
| 1192 | } |
| 1193 | } |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 1194 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 1195 | void __init_memblock memblock_set_current_limit(phys_addr_t limit) |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 1196 | { |
| 1197 | memblock.current_limit = limit; |
| 1198 | } |
| 1199 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1200 | static void __init_memblock memblock_dump(struct memblock_type *type, char *name) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1201 | { |
| 1202 | unsigned long long base, size; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 1203 | unsigned long flags; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1204 | int i; |
| 1205 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1206 | pr_info(" %s.cnt = 0x%lx\n", name, type->cnt); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1207 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1208 | for (i = 0; i < type->cnt; i++) { |
| 1209 | struct memblock_region *rgn = &type->regions[i]; |
| 1210 | char nid_buf[32] = ""; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1211 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1212 | base = rgn->base; |
| 1213 | size = rgn->size; |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 1214 | flags = rgn->flags; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 1215 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 1216 | if (memblock_get_region_node(rgn) != MAX_NUMNODES) |
| 1217 | snprintf(nid_buf, sizeof(nid_buf), " on node %d", |
| 1218 | memblock_get_region_node(rgn)); |
| 1219 | #endif |
Tang Chen | 66a2075 | 2014-01-21 15:49:20 -0800 | [diff] [blame] | 1220 | pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n", |
| 1221 | name, i, base, base + size - 1, size, nid_buf, flags); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
Tejun Heo | 4ff7b82 | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 1225 | void __init_memblock __memblock_dump_all(void) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1226 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1227 | pr_info("MEMBLOCK configuration:\n"); |
Tejun Heo | 1440c4e | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 1228 | pr_info(" memory size = %#llx reserved size = %#llx\n", |
| 1229 | (unsigned long long)memblock.memory.total_size, |
| 1230 | (unsigned long long)memblock.reserved.total_size); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1231 | |
| 1232 | memblock_dump(&memblock.memory, "memory"); |
| 1233 | memblock_dump(&memblock.reserved, "reserved"); |
| 1234 | } |
| 1235 | |
Tejun Heo | 1aadc05 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 1236 | void __init memblock_allow_resize(void) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1237 | { |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 1238 | memblock_can_resize = 1; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1239 | } |
| 1240 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 1241 | static int __init early_memblock(char *p) |
| 1242 | { |
| 1243 | if (p && strstr(p, "debug")) |
| 1244 | memblock_debug = 1; |
| 1245 | return 0; |
| 1246 | } |
| 1247 | early_param("memblock", early_memblock); |
| 1248 | |
Tejun Heo | c378ddd | 2011-07-14 11:46:03 +0200 | [diff] [blame] | 1249 | #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK) |
Benjamin Herrenschmidt | 6d03b88 | 2010-07-06 15:39:19 -0700 | [diff] [blame] | 1250 | |
| 1251 | static int memblock_debug_show(struct seq_file *m, void *private) |
| 1252 | { |
| 1253 | struct memblock_type *type = m->private; |
| 1254 | struct memblock_region *reg; |
| 1255 | int i; |
| 1256 | |
| 1257 | for (i = 0; i < type->cnt; i++) { |
| 1258 | reg = &type->regions[i]; |
| 1259 | seq_printf(m, "%4d: ", i); |
| 1260 | if (sizeof(phys_addr_t) == 4) |
| 1261 | seq_printf(m, "0x%08lx..0x%08lx\n", |
| 1262 | (unsigned long)reg->base, |
| 1263 | (unsigned long)(reg->base + reg->size - 1)); |
| 1264 | else |
| 1265 | seq_printf(m, "0x%016llx..0x%016llx\n", |
| 1266 | (unsigned long long)reg->base, |
| 1267 | (unsigned long long)(reg->base + reg->size - 1)); |
| 1268 | |
| 1269 | } |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | static int memblock_debug_open(struct inode *inode, struct file *file) |
| 1274 | { |
| 1275 | return single_open(file, memblock_debug_show, inode->i_private); |
| 1276 | } |
| 1277 | |
| 1278 | static const struct file_operations memblock_debug_fops = { |
| 1279 | .open = memblock_debug_open, |
| 1280 | .read = seq_read, |
| 1281 | .llseek = seq_lseek, |
| 1282 | .release = single_release, |
| 1283 | }; |
| 1284 | |
| 1285 | static int __init memblock_init_debugfs(void) |
| 1286 | { |
| 1287 | struct dentry *root = debugfs_create_dir("memblock", NULL); |
| 1288 | if (!root) |
| 1289 | return -ENXIO; |
| 1290 | debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops); |
| 1291 | debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops); |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | __initcall(memblock_init_debugfs); |
| 1296 | |
| 1297 | #endif /* CONFIG_DEBUG_FS */ |