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