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