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