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