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