blob: 0695284232f3c617022d4694059c7532e1cf0d48 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Yinghai Lu95f72d12010-07-12 14:36:09 +10002/*
3 * Procedures for maintaining information about logical memory blocks.
4 *
5 * Peter Bergner, IBM Corp. June 2001.
6 * Copyright (C) 2001 Peter Bergner.
Yinghai Lu95f72d12010-07-12 14:36:09 +10007 */
8
9#include <linux/kernel.h>
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -070010#include <linux/slab.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100011#include <linux/init.h>
12#include <linux/bitops.h>
Benjamin Herrenschmidt449e8df2010-07-06 15:39:07 -070013#include <linux/poison.h>
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -070014#include <linux/pfn.h>
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -070015#include <linux/debugfs.h>
Randy Dunlap514c6032018-04-05 16:25:34 -070016#include <linux/kmemleak.h>
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -070017#include <linux/seq_file.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100018#include <linux/memblock.h>
19
Christoph Hellwigc4c5ad62016-07-28 15:48:06 -070020#include <asm/sections.h>
Santosh Shilimkar26f09e92014-01-21 15:50:19 -080021#include <linux/io.h>
22
23#include "internal.h"
Tang Chen79442ed2013-11-12 15:07:59 -080024
Ard Biesheuvel8a5b403d2019-02-15 13:33:32 +010025#define INIT_MEMBLOCK_REGIONS 128
26#define INIT_PHYSMEM_REGIONS 4
27
28#ifndef INIT_MEMBLOCK_RESERVED_REGIONS
29# define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
30#endif
31
Zhou Guanghui450d0e72022-06-15 10:27:42 +000032#ifndef INIT_MEMBLOCK_MEMORY_REGIONS
33#define INIT_MEMBLOCK_MEMORY_REGIONS INIT_MEMBLOCK_REGIONS
34#endif
35
Mike Rapoport3e039c52018-06-30 17:55:05 +030036/**
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
David Hildenbrand77649902020-07-01 16:18:29 +020051 * * ``physmem`` - describes the actual physical memory available during
52 * boot regardless of the possible restrictions and memory hot(un)plug;
53 * the ``physmem`` type is only available on some architectures.
Mike Rapoport3e039c52018-06-30 17:55:05 +030054 *
Mauro Carvalho Chehab9303c9d2020-09-25 12:01:25 +020055 * Each region is represented by struct memblock_region that
Mike Rapoport3e039c52018-06-30 17:55:05 +030056 * defines the region extents, its attributes and NUMA node id on NUMA
Mauro Carvalho Chehab1bf162e2020-09-28 15:50:33 +020057 * systems. Every memory type is described by the struct memblock_type
58 * which contains an array of memory regions along with
David Hildenbrand77649902020-07-01 16:18:29 +020059 * the allocator metadata. The "memory" and "reserved" types are nicely
Mauro Carvalho Chehab9303c9d2020-09-25 12:01:25 +020060 * wrapped with struct memblock. This structure is statically
David Hildenbrand77649902020-07-01 16:18:29 +020061 * initialized at build time. The region arrays are initially sized to
Zhou Guanghui450d0e72022-06-15 10:27:42 +000062 * %INIT_MEMBLOCK_MEMORY_REGIONS for "memory" and
63 * %INIT_MEMBLOCK_RESERVED_REGIONS for "reserved". The region array
64 * for "physmem" is initially sized to %INIT_PHYSMEM_REGIONS.
Cao jin6e5af9a2019-11-30 17:56:21 -080065 * The memblock_allow_resize() enables automatic resizing of the region
66 * arrays during addition of new regions. This feature should be used
67 * with care so that memory allocated for the region array will not
68 * overlap with areas that should be reserved, for example initrd.
Mike Rapoport3e039c52018-06-30 17:55:05 +030069 *
70 * The early architecture setup should tell memblock what the physical
Cao jin6e5af9a2019-11-30 17:56:21 -080071 * memory layout is by using memblock_add() or memblock_add_node()
72 * functions. The first function does not assign the region to a NUMA
73 * node and it is appropriate for UMA systems. Yet, it is possible to
74 * use it on NUMA systems as well and assign the region to a NUMA node
75 * later in the setup process using memblock_set_node(). The
76 * memblock_add_node() performs such an assignment directly.
Mike Rapoport3e039c52018-06-30 17:55:05 +030077 *
Mike Rapoporta2974132019-03-11 23:30:54 -070078 * Once memblock is setup the memory can be allocated using one of the
79 * API variants:
80 *
Cao jin6e5af9a2019-11-30 17:56:21 -080081 * * memblock_phys_alloc*() - these functions return the **physical**
82 * address of the allocated memory
83 * * memblock_alloc*() - these functions return the **virtual** address
84 * of the allocated memory.
Mike Rapoporta2974132019-03-11 23:30:54 -070085 *
Ethon Pauldf1758d2020-06-04 16:49:16 -070086 * Note, that both API variants use implicit assumptions about allowed
Mike Rapoporta2974132019-03-11 23:30:54 -070087 * memory ranges and the fallback methods. Consult the documentation
Cao jin6e5af9a2019-11-30 17:56:21 -080088 * of memblock_alloc_internal() and memblock_alloc_range_nid()
89 * functions for more elaborate description.
Mike Rapoport3e039c52018-06-30 17:55:05 +030090 *
Cao jin6e5af9a2019-11-30 17:56:21 -080091 * As the system boot progresses, the architecture specific mem_init()
92 * function frees all the memory to the buddy page allocator.
Mike Rapoport3e039c52018-06-30 17:55:05 +030093 *
Cao jin6e5af9a2019-11-30 17:56:21 -080094 * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
David Hildenbrand77649902020-07-01 16:18:29 +020095 * memblock data structures (except "physmem") will be discarded after the
96 * system initialization completes.
Mike Rapoport3e039c52018-06-30 17:55:05 +030097 */
98
Mike Rapoporta9ee6cf2021-06-28 19:43:01 -070099#ifndef CONFIG_NUMA
Mike Rapoportbda49a82018-10-30 15:09:40 -0700100struct pglist_data __refdata contig_page_data;
101EXPORT_SYMBOL(contig_page_data);
102#endif
103
104unsigned long max_low_pfn;
105unsigned long min_low_pfn;
106unsigned long max_pfn;
107unsigned long long max_possible_pfn;
108
Zhou Guanghui450d0e72022-06-15 10:27:42 +0000109static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_MEMORY_REGIONS] __initdata_memblock;
Ard Biesheuvel8a5b403d2019-02-15 13:33:32 +0100110static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100111#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
David Hildenbrand77649902020-07-01 16:18:29 +0200112static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS];
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100113#endif
Tejun Heofe091c22011-12-08 10:22:07 -0800114
115struct memblock memblock __initdata_memblock = {
116 .memory.regions = memblock_memory_init_regions,
117 .memory.cnt = 1, /* empty dummy entry */
Zhou Guanghui450d0e72022-06-15 10:27:42 +0000118 .memory.max = INIT_MEMBLOCK_MEMORY_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800119 .memory.name = "memory",
Tejun Heofe091c22011-12-08 10:22:07 -0800120
121 .reserved.regions = memblock_reserved_init_regions,
122 .reserved.cnt = 1, /* empty dummy entry */
Ard Biesheuvel8a5b403d2019-02-15 13:33:32 +0100123 .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800124 .reserved.name = "reserved",
Tejun Heofe091c22011-12-08 10:22:07 -0800125
Tang Chen79442ed2013-11-12 15:07:59 -0800126 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -0800127 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
128};
Yinghai Lu95f72d12010-07-12 14:36:09 +1000129
David Hildenbrand77649902020-07-01 16:18:29 +0200130#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
131struct memblock_type physmem = {
132 .regions = memblock_physmem_init_regions,
133 .cnt = 1, /* empty dummy entry */
134 .max = INIT_PHYSMEM_REGIONS,
135 .name = "physmem",
136};
137#endif
138
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -0700139/*
140 * keep a pointer to &memblock.memory in the text section to use it in
141 * __next_mem_range() and its helpers.
142 * For architectures that do not keep memblock data after init, this
143 * pointer will be reset to NULL at memblock_discard()
144 */
145static __refdata struct memblock_type *memblock_memory = &memblock.memory;
146
Mike Rapoportcd991db2020-10-13 16:57:49 -0700147#define for_each_memblock_type(i, memblock_type, rgn) \
148 for (i = 0, rgn = &memblock_type->regions[0]; \
149 i < memblock_type->cnt; \
150 i++, rgn = &memblock_type->regions[i])
151
Mike Rapoport87c55872020-10-13 16:57:54 -0700152#define memblock_dbg(fmt, ...) \
153 do { \
154 if (memblock_debug) \
155 pr_info(fmt, ##__VA_ARGS__); \
156 } while (0)
157
158static int memblock_debug __initdata_memblock;
Claudio Migliorellifc493f82023-04-23 15:29:35 +0200159static bool system_has_some_mirror __initdata_memblock;
Tejun Heo1aadc052011-12-08 10:22:08 -0800160static int memblock_can_resize __initdata_memblock;
Claudio Migliorellifc493f82023-04-23 15:29:35 +0200161static int memblock_memory_in_slab __initdata_memblock;
162static int memblock_reserved_in_slab __initdata_memblock;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000163
Ma Wupeng0db31d62023-08-02 15:23:28 +0800164bool __init_memblock memblock_has_mirror(void)
165{
166 return system_has_some_mirror;
167}
168
Mike Rapoportc366ea82019-03-11 23:29:46 -0700169static enum memblock_flags __init_memblock choose_memblock_flags(void)
Tony Lucka3f5baf2015-06-24 16:58:12 -0700170{
171 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
172}
173
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800174/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
175static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
176{
Stefan Agner1c4bc432018-06-07 17:06:15 -0700177 return *size = min(*size, PHYS_ADDR_MAX - base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800178}
179
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000180/*
181 * Address comparison utilities
182 */
Alison Schofieldb5bf39c2024-01-12 12:09:50 -0800183unsigned long __init_memblock
184memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, phys_addr_t base2,
185 phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000186{
187 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
188}
189
Tang Chen95cf82e2015-09-08 15:02:03 -0700190bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -0700191 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000192{
193 unsigned long i;
194
Mike Rapoport023accf2021-06-30 09:12:13 +0300195 memblock_cap_size(base, &size);
196
Alexander Kuleshovf14516f2016-01-14 15:20:39 -0800197 for (i = 0; i < type->cnt; i++)
198 if (memblock_addrs_overlap(base, size, type->regions[i].base,
199 type->regions[i].size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000200 break;
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700201 return i < type->cnt;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000202}
203
Mike Rapoport47cec442018-06-30 17:55:02 +0300204/**
Tang Chen79442ed2013-11-12 15:07:59 -0800205 * __memblock_find_range_bottom_up - find free area utility in bottom-up
206 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300207 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
208 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen79442ed2013-11-12 15:07:59 -0800209 * @size: size of free area to find
210 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800211 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700212 * @flags: pick from blocks based on memory attributes
Tang Chen79442ed2013-11-12 15:07:59 -0800213 *
214 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
215 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300216 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800217 * Found address on success, 0 on failure.
218 */
219static phys_addr_t __init_memblock
220__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700221 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300222 enum memblock_flags flags)
Tang Chen79442ed2013-11-12 15:07:59 -0800223{
224 phys_addr_t this_start, this_end, cand;
225 u64 i;
226
Tony Luckfc6daaf2015-06-24 16:58:09 -0700227 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
Tang Chen79442ed2013-11-12 15:07:59 -0800228 this_start = clamp(this_start, start, end);
229 this_end = clamp(this_end, start, end);
230
231 cand = round_up(this_start, align);
232 if (cand < this_end && this_end - cand >= size)
233 return cand;
234 }
235
236 return 0;
237}
238
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800239/**
Tang Chen14028992013-11-12 15:07:57 -0800240 * __memblock_find_range_top_down - find free area utility, in top-down
241 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300242 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
243 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen14028992013-11-12 15:07:57 -0800244 * @size: size of free area to find
245 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800246 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700247 * @flags: pick from blocks based on memory attributes
Tang Chen14028992013-11-12 15:07:57 -0800248 *
249 * Utility called from memblock_find_in_range_node(), find free area top-down.
250 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300251 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800252 * Found address on success, 0 on failure.
Tang Chen14028992013-11-12 15:07:57 -0800253 */
254static phys_addr_t __init_memblock
255__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700256 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300257 enum memblock_flags flags)
Tang Chen14028992013-11-12 15:07:57 -0800258{
259 phys_addr_t this_start, this_end, cand;
260 u64 i;
261
Tony Luckfc6daaf2015-06-24 16:58:09 -0700262 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
263 NULL) {
Tang Chen14028992013-11-12 15:07:57 -0800264 this_start = clamp(this_start, start, end);
265 this_end = clamp(this_end, start, end);
266
267 if (this_end < size)
268 continue;
269
270 cand = round_down(this_end - size, align);
271 if (cand >= this_start)
272 return cand;
273 }
274
275 return 0;
276}
277
278/**
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800279 * memblock_find_in_range_node - find free area in given range and node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800280 * @size: size of free area to find
281 * @align: alignment of free area to find
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800282 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300283 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
284 * %MEMBLOCK_ALLOC_ACCESSIBLE
Grygorii Strashkob1154232014-01-21 15:50:16 -0800285 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700286 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800287 *
288 * Find @size free area aligned to @align in the specified range and node.
289 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300290 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800291 * Found address on success, 0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000292 */
Mike Rapoportc366ea82019-03-11 23:29:46 -0700293static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800294 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300295 phys_addr_t end, int nid,
296 enum memblock_flags flags)
Tang Chenf7210e62013-02-22 16:33:51 -0800297{
Tang Chenf7210e62013-02-22 16:33:51 -0800298 /* pump up @end */
Qian Caifed84c72018-12-28 00:36:29 -0800299 if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
Qian Caic6975d72021-11-05 11:05:09 -0400300 end == MEMBLOCK_ALLOC_NOLEAKTRACE)
Tang Chenf7210e62013-02-22 16:33:51 -0800301 end = memblock.current_limit;
302
303 /* avoid allocating the first page */
304 start = max_t(phys_addr_t, start, PAGE_SIZE);
305 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800306
Roman Gushchin2dcb3962021-02-04 18:32:36 -0800307 if (memblock_bottom_up())
308 return __memblock_find_range_bottom_up(start, end, size, align,
309 nid, flags);
310 else
311 return __memblock_find_range_top_down(start, end, size, align,
312 nid, flags);
Tang Chenf7210e62013-02-22 16:33:51 -0800313}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000314
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800315/**
316 * memblock_find_in_range - find free area in given range
317 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300318 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
319 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800320 * @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 Rapoport47cec442018-06-30 17:55:02 +0300325 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800326 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800327 */
Mike Rapoporta7259df2021-09-02 15:00:26 -0700328static phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800329 phys_addr_t end, phys_addr_t size,
330 phys_addr_t align)
331{
Tony Lucka3f5baf2015-06-24 16:58:12 -0700332 phys_addr_t ret;
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300333 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -0700334
335again:
336 ret = memblock_find_in_range_node(size, align, start, end,
337 NUMA_NO_NODE, flags);
338
339 if (!ret && (flags & MEMBLOCK_MIRROR)) {
Ma Wupeng14d9a672022-06-14 17:21:53 +0800340 pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n",
Tony Lucka3f5baf2015-06-24 16:58:12 -0700341 &size);
342 flags &= ~MEMBLOCK_MIRROR;
343 goto again;
344 }
345
346 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800347}
348
Yinghai Lu10d06432010-07-28 15:43:02 +1000349static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000350{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800351 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200352 memmove(&type->regions[r], &type->regions[r + 1],
353 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000354 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000355
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700356 /* Special case for empty arrays */
357 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800358 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700359 type->cnt = 1;
360 type->regions[0].base = 0;
361 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800362 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200363 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700364 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000365}
366
Mike Rapoport350e88b2019-05-13 17:22:59 -0700367#ifndef CONFIG_ARCH_KEEP_MEMBLOCK
Pavel Tatashin3010f872017-08-18 15:16:05 -0700368/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300369 * memblock_discard - discard memory and reserved arrays if they were allocated
Pavel Tatashin3010f872017-08-18 15:16:05 -0700370 */
371void __init memblock_discard(void)
Yinghai Lu29f67382012-07-11 14:02:56 -0700372{
Pavel Tatashin3010f872017-08-18 15:16:05 -0700373 phys_addr_t addr, size;
Yinghai Lu29f67382012-07-11 14:02:56 -0700374
Pavel Tatashin3010f872017-08-18 15:16:05 -0700375 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);
Miaohe Linc94afc42022-02-17 22:53:27 +0800379 if (memblock_reserved_in_slab)
380 kfree(memblock.reserved.regions);
381 else
382 memblock_free_late(addr, size);
Pavel Tatashin3010f872017-08-18 15:16:05 -0700383 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700384
Pavel Tatashin91b540f92017-08-25 15:55:46 -0700385 if (memblock.memory.regions != memblock_memory_init_regions) {
Pavel Tatashin3010f872017-08-18 15:16:05 -0700386 addr = __pa(memblock.memory.regions);
387 size = PAGE_ALIGN(sizeof(struct memblock_region) *
388 memblock.memory.max);
Miaohe Linc94afc42022-02-17 22:53:27 +0800389 if (memblock_memory_in_slab)
390 kfree(memblock.memory.regions);
391 else
392 memblock_free_late(addr, size);
Pavel Tatashin3010f872017-08-18 15:16:05 -0700393 }
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -0700394
395 memblock_memory = NULL;
Yinghai Lu29f67382012-07-11 14:02:56 -0700396}
Philipp Hachtmann5e270e22014-01-23 15:53:11 -0800397#endif
398
Greg Pearson48c3b582012-06-20 12:53:05 -0700399/**
400 * memblock_double_array - double the size of the memblock regions array
401 * @type: memblock type of the regions array being doubled
402 * @new_area_start: starting address of memory range to avoid overlap with
403 * @new_area_size: size of memory range to avoid overlap with
404 *
405 * Double the size of the @type regions array. If memblock is being used to
406 * allocate memory for a new reserved regions array and there is a previously
Mike Rapoport47cec442018-06-30 17:55:02 +0300407 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
Greg Pearson48c3b582012-06-20 12:53:05 -0700408 * waiting to be reserved, ensure the memory used by the new array does
409 * not overlap.
410 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300411 * Return:
Greg Pearson48c3b582012-06-20 12:53:05 -0700412 * 0 on success, -1 on failure.
413 */
414static int __init_memblock memblock_double_array(struct memblock_type *type,
415 phys_addr_t new_area_start,
416 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700417{
418 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700419 phys_addr_t old_alloc_size, new_alloc_size;
Mike Rapoporta36aab82018-08-17 15:47:17 -0700420 phys_addr_t old_size, new_size, addr, new_end;
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700421 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700422 int *in_slab;
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700423
424 /* We don't allow resizing until we know about the reserved regions
425 * of memory that aren't suitable for allocation
426 */
427 if (!memblock_can_resize)
428 return -1;
429
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700430 /* Calculate new doubled size */
431 old_size = type->max * sizeof(struct memblock_region);
432 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700433 /*
434 * We need to allocated new one align to PAGE_SIZE,
435 * so we can free them completely later.
436 */
437 old_alloc_size = PAGE_ALIGN(old_size);
438 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700439
Gavin Shan181eb392012-05-29 15:06:50 -0700440 /* Retrieve the slab flag */
441 if (type == &memblock.memory)
442 in_slab = &memblock_memory_in_slab;
443 else
444 in_slab = &memblock_reserved_in_slab;
445
Mike Rapoporta2974132019-03-11 23:30:54 -0700446 /* Try to find some space for it */
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700447 if (use_slab) {
448 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200449 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700450 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700451 /* only exclude range when trying to double reserved.regions */
452 if (type != &memblock.reserved)
453 new_area_start = new_area_size = 0;
454
455 addr = memblock_find_in_range(new_area_start + new_area_size,
456 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700457 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700458 if (!addr && new_area_size)
459 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700460 min(new_area_start, memblock.current_limit),
461 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700462
Tom Lendacky7bcd2912025-05-08 12:24:10 -0500463 if (addr) {
464 /* The memory may not have been accepted, yet. */
465 accept_memory(addr, addr + new_alloc_size);
466
467 new_array = __va(addr);
468 } else {
469 new_array = NULL;
470 }
Gavin Shan4e2f0772012-05-29 15:06:50 -0700471 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200472 if (!addr) {
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700473 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800474 type->name, type->max, type->max * 2);
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700475 return -1;
476 }
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700477
Mike Rapoporta36aab82018-08-17 15:47:17 -0700478 new_end = addr + new_size - 1;
479 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
480 type->name, type->max * 2, &addr, &new_end);
Yinghai Luea9e4372010-07-28 15:13:22 +1000481
Andrew Mortonfd073832012-07-31 16:42:40 -0700482 /*
483 * Found space, we now need to move the array over before we add the
484 * reserved region since it may be our reserved array itself that is
485 * full.
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700486 */
487 memcpy(new_array, type->regions, old_size);
488 memset(new_array + type->max, 0, old_size);
489 old_array = type->regions;
490 type->regions = new_array;
491 type->max <<= 1;
492
Andrew Mortonfd073832012-07-31 16:42:40 -0700493 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700494 if (*in_slab)
495 kfree(old_array);
496 else if (old_array != memblock_memory_init_regions &&
497 old_array != memblock_reserved_init_regions)
Mike Rapoport4421cca02021-11-05 13:43:22 -0700498 memblock_free(old_array, old_alloc_size);
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700499
Andrew Mortonfd073832012-07-31 16:42:40 -0700500 /*
501 * Reserve the new array if that comes from the memblock. Otherwise, we
502 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700503 */
504 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700505 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700506
507 /* Update slab flag */
508 *in_slab = use_slab;
509
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700510 return 0;
511}
512
Tejun Heo784656f92011-07-12 11:15:55 +0200513/**
514 * memblock_merge_regions - merge neighboring compatible regions
515 * @type: memblock type to scan
Peng Zhang2fe03412023-01-29 17:00:34 +0800516 * @start_rgn: start scanning from (@start_rgn - 1)
517 * @end_rgn: end scanning at (@end_rgn - 1)
518 * Scan @type and merge neighboring compatible regions in [@start_rgn - 1, @end_rgn)
Tejun Heo784656f92011-07-12 11:15:55 +0200519 */
Peng Zhang2fe03412023-01-29 17:00:34 +0800520static void __init_memblock memblock_merge_regions(struct memblock_type *type,
521 unsigned long start_rgn,
522 unsigned long end_rgn)
Tejun Heo784656f92011-07-12 11:15:55 +0200523{
524 int i = 0;
Peng Zhang2fe03412023-01-29 17:00:34 +0800525 if (start_rgn)
526 i = start_rgn - 1;
527 end_rgn = min(end_rgn, type->cnt - 1);
528 while (i < end_rgn) {
Tejun Heo784656f92011-07-12 11:15:55 +0200529 struct memblock_region *this = &type->regions[i];
530 struct memblock_region *next = &type->regions[i + 1];
531
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200532 if (this->base + this->size != next->base ||
533 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800534 memblock_get_region_node(next) ||
535 this->flags != next->flags) {
Tejun Heo784656f92011-07-12 11:15:55 +0200536 BUG_ON(this->base + this->size > next->base);
537 i++;
538 continue;
539 }
540
541 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800542 /* move forward from next + 1, index of which is i + 2 */
543 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f92011-07-12 11:15:55 +0200544 type->cnt--;
Peng Zhang2fe03412023-01-29 17:00:34 +0800545 end_rgn--;
Tejun Heo784656f92011-07-12 11:15:55 +0200546 }
547}
548
549/**
550 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700551 * @type: memblock type to insert into
552 * @idx: index for the insertion point
553 * @base: base address of the new region
554 * @size: size of the new region
555 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800556 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200557 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300558 * Insert new memblock region [@base, @base + @size) into @type at @idx.
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700559 * @type must already have extra room to accommodate the new region.
Tejun Heo784656f92011-07-12 11:15:55 +0200560 */
561static void __init_memblock memblock_insert_region(struct memblock_type *type,
562 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800563 phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300564 int nid,
565 enum memblock_flags flags)
Tejun Heo784656f92011-07-12 11:15:55 +0200566{
567 struct memblock_region *rgn = &type->regions[idx];
568
569 BUG_ON(type->cnt >= type->max);
570 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
571 rgn->base = base;
572 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800573 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200574 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200575 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800576 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200577}
578
579/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100580 * memblock_add_range - add new memblock region
Tejun Heo784656f92011-07-12 11:15:55 +0200581 * @type: memblock type to add new region into
582 * @base: base address of the new region
583 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800584 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800585 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200586 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300587 * Add new memblock region [@base, @base + @size) into @type. The new region
Tejun Heo784656f92011-07-12 11:15:55 +0200588 * is allowed to overlap with existing ones - overlaps don't affect already
589 * existing regions. @type is guaranteed to be minimal (all neighbouring
590 * compatible regions are merged) after the addition.
591 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300592 * Return:
Tejun Heo784656f92011-07-12 11:15:55 +0200593 * 0 on success, -errno on failure.
594 */
Anshuman Khandual02634a42020-01-30 22:14:20 -0800595static int __init_memblock memblock_add_range(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800596 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300597 int nid, enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000598{
Tejun Heo784656f92011-07-12 11:15:55 +0200599 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800600 phys_addr_t obase = base;
601 phys_addr_t end = base + memblock_cap_size(base, &size);
Peng Zhang2fe03412023-01-29 17:00:34 +0800602 int idx, nr_new, start_rgn = -1, end_rgn;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800603 struct memblock_region *rgn;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000604
Tejun Heob3dc6272012-04-20 08:31:34 -0700605 if (!size)
606 return 0;
607
Tejun Heo784656f92011-07-12 11:15:55 +0200608 /* special case for empty array */
609 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800610 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000611 type->regions[0].base = base;
612 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800613 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800614 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800615 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000616 return 0;
617 }
Jinyu Tang28e1a8f2022-06-15 17:40:15 +0800618
619 /*
620 * The worst case is when new range overlaps all existing regions,
621 * then we'll need type->cnt + 1 empty regions in @type. So if
Peng Zhangad500fb2023-01-29 17:00:33 +0800622 * type->cnt * 2 + 1 is less than or equal to type->max, we know
Jinyu Tang28e1a8f2022-06-15 17:40:15 +0800623 * that there is enough empty regions in @type, and we can insert
624 * regions directly.
625 */
Peng Zhangad500fb2023-01-29 17:00:33 +0800626 if (type->cnt * 2 + 1 <= type->max)
Jinyu Tang28e1a8f2022-06-15 17:40:15 +0800627 insert = true;
628
Tejun Heo784656f92011-07-12 11:15:55 +0200629repeat:
630 /*
631 * The following is executed twice. Once with %false @insert and
632 * then with %true. The first counts the number of regions needed
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700633 * to accommodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700634 */
Tejun Heo784656f92011-07-12 11:15:55 +0200635 base = obase;
636 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000637
Gioh Kim66e8b432017-11-15 17:33:42 -0800638 for_each_memblock_type(idx, type, rgn) {
Tejun Heo784656f92011-07-12 11:15:55 +0200639 phys_addr_t rbase = rgn->base;
640 phys_addr_t rend = rbase + rgn->size;
641
642 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000643 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200644 if (rend <= base)
645 continue;
646 /*
647 * @rgn overlaps. If it separates the lower part of new
648 * area, insert that portion.
649 */
650 if (rbase > base) {
Mike Rapoporta9ee6cf2021-06-28 19:43:01 -0700651#ifdef CONFIG_NUMA
Wei Yangc0a29492015-09-04 15:47:38 -0700652 WARN_ON(nid != memblock_get_region_node(rgn));
653#endif
Wei Yang4fcab5f2015-09-08 14:59:53 -0700654 WARN_ON(flags != rgn->flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200655 nr_new++;
Peng Zhang2fe03412023-01-29 17:00:34 +0800656 if (insert) {
657 if (start_rgn == -1)
658 start_rgn = idx;
659 end_rgn = idx + 1;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800660 memblock_insert_region(type, idx++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800661 rbase - base, nid,
662 flags);
Peng Zhang2fe03412023-01-29 17:00:34 +0800663 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000664 }
Tejun Heo784656f92011-07-12 11:15:55 +0200665 /* area below @rend is dealt with, forget about it */
666 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000667 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000668
Tejun Heo784656f92011-07-12 11:15:55 +0200669 /* insert the remaining portion */
670 if (base < end) {
671 nr_new++;
Peng Zhang2fe03412023-01-29 17:00:34 +0800672 if (insert) {
673 if (start_rgn == -1)
674 start_rgn = idx;
675 end_rgn = idx + 1;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800676 memblock_insert_region(type, idx, base, end - base,
Tang Chen66a20752014-01-21 15:49:20 -0800677 nid, flags);
Peng Zhang2fe03412023-01-29 17:00:34 +0800678 }
Tejun Heo784656f92011-07-12 11:15:55 +0200679 }
680
nimisoloef3cc4d2016-07-26 15:24:56 -0700681 if (!nr_new)
682 return 0;
683
Tejun Heo784656f92011-07-12 11:15:55 +0200684 /*
685 * If this was the first round, resize array and repeat for actual
686 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700687 */
Tejun Heo784656f92011-07-12 11:15:55 +0200688 if (!insert) {
689 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700690 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200691 return -ENOMEM;
692 insert = true;
693 goto repeat;
694 } else {
Peng Zhang2fe03412023-01-29 17:00:34 +0800695 memblock_merge_regions(type, start_rgn, end_rgn);
Tejun Heo784656f92011-07-12 11:15:55 +0200696 return 0;
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -0700697 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000698}
699
Mike Rapoport48a833c2018-06-30 17:55:03 +0300700/**
701 * memblock_add_node - add new memblock region within a NUMA node
702 * @base: base address of the new region
703 * @size: size of the new region
704 * @nid: nid of the new region
David Hildenbrand952eea92021-11-05 13:44:49 -0700705 * @flags: flags of the new region
Mike Rapoport48a833c2018-06-30 17:55:03 +0300706 *
707 * Add new memblock region [@base, @base + @size) to the "memory"
708 * type. See memblock_add_range() description for mode details
709 *
710 * Return:
711 * 0 on success, -errno on failure.
712 */
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800713int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
David Hildenbrand952eea92021-11-05 13:44:49 -0700714 int nid, enum memblock_flags flags)
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800715{
Geert Uytterhoeven00974b92021-08-11 10:54:36 +0200716 phys_addr_t end = base + size - 1;
717
David Hildenbrand952eea92021-11-05 13:44:49 -0700718 memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__,
719 &base, &end, nid, flags, (void *)_RET_IP_);
Geert Uytterhoeven00974b92021-08-11 10:54:36 +0200720
David Hildenbrand952eea92021-11-05 13:44:49 -0700721 return memblock_add_range(&memblock.memory, base, size, nid, flags);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800722}
723
Mike Rapoport48a833c2018-06-30 17:55:03 +0300724/**
725 * memblock_add - add new memblock region
726 * @base: base address of the new region
727 * @size: size of the new region
728 *
729 * Add new memblock region [@base, @base + @size) to the "memory"
730 * type. See memblock_add_range() description for mode details
731 *
732 * Return:
733 * 0 on success, -errno on failure.
734 */
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700735int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700736{
Miles Chen5d63f812017-02-22 15:46:42 -0800737 phys_addr_t end = base + size - 1;
738
Anshuman Khanduala090d712020-01-30 22:14:23 -0800739 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
Miles Chen5d63f812017-02-22 15:46:42 -0800740 &base, &end, (void *)_RET_IP_);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700741
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700742 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000743}
744
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800745/**
Liam Ni6fdc7702023-10-26 10:03:29 +0800746 * memblock_validate_numa_coverage - check if amount of memory with
747 * no node ID assigned is less than a threshold
Mike Rapoport (Microsoft)1864d472024-11-29 11:13:47 +0200748 * @threshold_bytes: maximal memory size that can have unassigned node
Liam Ni6fdc7702023-10-26 10:03:29 +0800749 * ID (in bytes).
750 *
751 * A buggy firmware may report memory that does not belong to any node.
752 * Check if amount of such memory is below @threshold_bytes.
753 *
754 * Return: true on success, false on failure.
755 */
756bool __init_memblock memblock_validate_numa_coverage(unsigned long threshold_bytes)
757{
758 unsigned long nr_pages = 0;
759 unsigned long start_pfn, end_pfn, mem_size_mb;
760 int nid, i;
761
762 /* calculate lose page */
763 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
Mike Rapoport (IBM)fdebee52024-06-14 11:05:43 +0300764 if (!numa_valid_node(nid))
Liam Ni6fdc7702023-10-26 10:03:29 +0800765 nr_pages += end_pfn - start_pfn;
766 }
767
Mike Rapoport (Microsoft)1864d472024-11-29 11:13:47 +0200768 if ((nr_pages << PAGE_SHIFT) > threshold_bytes) {
Liam Ni6fdc7702023-10-26 10:03:29 +0800769 mem_size_mb = memblock_phys_mem_size() >> 20;
770 pr_err("NUMA: no nodes coverage for %luMB of %luMB RAM\n",
771 (nr_pages << PAGE_SHIFT) >> 20, mem_size_mb);
772 return false;
773 }
774
775 return true;
776}
777
778
779/**
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800780 * memblock_isolate_range - isolate given range into disjoint memblocks
781 * @type: memblock type to isolate range for
782 * @base: base of range to isolate
783 * @size: size of range to isolate
784 * @start_rgn: out parameter for the start of isolated region
785 * @end_rgn: out parameter for the end of isolated region
786 *
787 * Walk @type and ensure that regions don't cross the boundaries defined by
Mike Rapoport47cec442018-06-30 17:55:02 +0300788 * [@base, @base + @size). Crossing regions are split at the boundaries,
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800789 * which may create at most two more regions. The index of the first
790 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
791 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300792 * Return:
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800793 * 0 on success, -errno on failure.
794 */
795static int __init_memblock memblock_isolate_range(struct memblock_type *type,
796 phys_addr_t base, phys_addr_t size,
797 int *start_rgn, int *end_rgn)
798{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800799 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800800 int idx;
801 struct memblock_region *rgn;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800802
803 *start_rgn = *end_rgn = 0;
804
Tejun Heob3dc6272012-04-20 08:31:34 -0700805 if (!size)
806 return 0;
807
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800808 /* we'll create at most two more regions */
809 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700810 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800811 return -ENOMEM;
812
Gioh Kim66e8b432017-11-15 17:33:42 -0800813 for_each_memblock_type(idx, type, rgn) {
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800814 phys_addr_t rbase = rgn->base;
815 phys_addr_t rend = rbase + rgn->size;
816
817 if (rbase >= end)
818 break;
819 if (rend <= base)
820 continue;
821
822 if (rbase < base) {
823 /*
824 * @rgn intersects from below. Split and continue
825 * to process the next region - the new top half.
826 */
827 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800828 rgn->size -= base - rbase;
829 type->total_size -= base - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800830 memblock_insert_region(type, idx, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800831 memblock_get_region_node(rgn),
832 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800833 } else if (rend > end) {
834 /*
835 * @rgn intersects from above. Split and redo the
836 * current region - the new bottom half.
837 */
838 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800839 rgn->size -= end - rbase;
840 type->total_size -= end - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800841 memblock_insert_region(type, idx--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800842 memblock_get_region_node(rgn),
843 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800844 } else {
845 /* @rgn is fully contained, record it */
846 if (!*end_rgn)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800847 *start_rgn = idx;
848 *end_rgn = idx + 1;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800849 }
850 }
851
852 return 0;
853}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800854
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800855static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100856 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000857{
Tejun Heo71936182011-12-08 10:22:07 -0800858 int start_rgn, end_rgn;
859 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000860
Tejun Heo71936182011-12-08 10:22:07 -0800861 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
862 if (ret)
863 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000864
Tejun Heo71936182011-12-08 10:22:07 -0800865 for (i = end_rgn - 1; i >= start_rgn; i--)
866 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700867 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000868}
869
Tejun Heo581adcb2011-12-08 10:22:06 -0800870int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000871{
Minchan Kim25cf23d2018-06-07 17:07:35 -0700872 phys_addr_t end = base + size - 1;
873
Anshuman Khanduala090d712020-01-30 22:14:23 -0800874 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
Minchan Kim25cf23d2018-06-07 17:07:35 -0700875 &base, &end, (void *)_RET_IP_);
876
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100877 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000878}
879
Mike Rapoport4d728682018-12-28 00:35:29 -0800880/**
Mike Rapoport4421cca02021-11-05 13:43:22 -0700881 * memblock_free - free boot memory allocation
Linus Torvalds77e02cf2021-09-14 13:23:22 -0700882 * @ptr: starting address of the boot memory allocation
883 * @size: size of the boot memory block in bytes
884 *
885 * Free boot memory block previously allocated by memblock_alloc_xx() API.
886 * The freeing memory will not be released to the buddy allocator.
887 */
Mike Rapoport4421cca02021-11-05 13:43:22 -0700888void __init_memblock memblock_free(void *ptr, size_t size)
Linus Torvalds77e02cf2021-09-14 13:23:22 -0700889{
890 if (ptr)
Mike Rapoport3ecc6832021-11-05 13:43:19 -0700891 memblock_phys_free(__pa(ptr), size);
Linus Torvalds77e02cf2021-09-14 13:23:22 -0700892}
893
894/**
Mike Rapoport3ecc6832021-11-05 13:43:19 -0700895 * memblock_phys_free - free boot memory block
Mike Rapoport4d728682018-12-28 00:35:29 -0800896 * @base: phys starting address of the boot memory block
897 * @size: size of the boot memory block in bytes
898 *
Miaoqian Linfa81ab42022-12-16 14:03:03 +0400899 * Free boot memory block previously allocated by memblock_phys_alloc_xx() API.
Mike Rapoport4d728682018-12-28 00:35:29 -0800900 * The freeing memory will not be released to the buddy allocator.
901 */
Mike Rapoport3ecc6832021-11-05 13:43:19 -0700902int __init_memblock memblock_phys_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000903{
Miles Chen5d63f812017-02-22 15:46:42 -0800904 phys_addr_t end = base + size - 1;
905
Anshuman Khanduala090d712020-01-30 22:14:23 -0800906 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
Miles Chen5d63f812017-02-22 15:46:42 -0800907 &base, &end, (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200908
Catalin Marinas9099dae2016-10-11 13:55:11 -0700909 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100910 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000911}
912
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700913int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000914{
Miles Chen5d63f812017-02-22 15:46:42 -0800915 phys_addr_t end = base + size - 1;
916
Anshuman Khanduala090d712020-01-30 22:14:23 -0800917 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
Miles Chen5d63f812017-02-22 15:46:42 -0800918 &base, &end, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000919
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700920 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000921}
922
Anshuman Khandual02634a42020-01-30 22:14:20 -0800923#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
924int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
925{
926 phys_addr_t end = base + size - 1;
927
928 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
929 &base, &end, (void *)_RET_IP_);
930
David Hildenbrand77649902020-07-01 16:18:29 +0200931 return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0);
Anshuman Khandual02634a42020-01-30 22:14:20 -0800932}
933#endif
934
Tejun Heo35fd0802011-07-12 11:15:59 +0200935/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300936 * memblock_setclr_flag - set or clear flag for a memory region
937 * @base: base address of the region
938 * @size: size of the region
939 * @set: set or clear the flag
Haitao Shi8958b242020-12-15 20:47:26 -0800940 * @flag: the flag to update
Tang Chen66b16ed2014-01-21 15:49:23 -0800941 *
Tony Luck4308ce12014-12-12 16:54:59 -0800942 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800943 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300944 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800945 */
Tony Luck4308ce12014-12-12 16:54:59 -0800946static int __init_memblock memblock_setclr_flag(phys_addr_t base,
947 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800948{
949 struct memblock_type *type = &memblock.memory;
950 int i, ret, start_rgn, end_rgn;
951
952 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
953 if (ret)
954 return ret;
955
Mike Rapoportfe145122019-03-11 23:30:46 -0700956 for (i = start_rgn; i < end_rgn; i++) {
957 struct memblock_region *r = &type->regions[i];
958
Tony Luck4308ce12014-12-12 16:54:59 -0800959 if (set)
Mike Rapoportfe145122019-03-11 23:30:46 -0700960 r->flags |= flag;
Tony Luck4308ce12014-12-12 16:54:59 -0800961 else
Mike Rapoportfe145122019-03-11 23:30:46 -0700962 r->flags &= ~flag;
963 }
Tang Chen66b16ed2014-01-21 15:49:23 -0800964
Peng Zhang2fe03412023-01-29 17:00:34 +0800965 memblock_merge_regions(type, start_rgn, end_rgn);
Tang Chen66b16ed2014-01-21 15:49:23 -0800966 return 0;
967}
968
969/**
Tony Luck4308ce12014-12-12 16:54:59 -0800970 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
971 * @base: the base phys addr of the region
972 * @size: the size of the region
973 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300974 * Return: 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800975 */
976int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
977{
978 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
979}
980
981/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800982 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
983 * @base: the base phys addr of the region
984 * @size: the size of the region
985 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300986 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800987 */
988int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
989{
Tony Luck4308ce12014-12-12 16:54:59 -0800990 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800991}
992
993/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700994 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
995 * @base: the base phys addr of the region
996 * @size: the size of the region
997 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300998 * Return: 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700999 */
1000int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
1001{
Ma Wupeng902c2d92022-06-14 17:21:56 +08001002 if (!mirrored_kernelcore)
1003 return 0;
1004
Tony Lucka3f5baf2015-06-24 16:58:12 -07001005 system_has_some_mirror = true;
1006
1007 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
1008}
1009
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001010/**
1011 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
1012 * @base: the base phys addr of the region
1013 * @size: the size of the region
1014 *
Mike Rapoport9092d4f2021-06-30 18:51:16 -07001015 * The memory regions marked with %MEMBLOCK_NOMAP will not be added to the
1016 * direct mapping of the physical memory. These regions will still be
1017 * covered by the memory map. The struct page representing NOMAP memory
1018 * frames in the memory map will be PageReserved()
1019 *
Mike Rapoport658aafc2021-10-21 10:09:29 +03001020 * Note: if the memory being marked %MEMBLOCK_NOMAP was allocated from
1021 * memblock, the caller must inform kmemleak to ignore that memory
1022 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001023 * Return: 0 on success, -errno on failure.
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001024 */
1025int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
1026{
Mike Rapoport6c9a5452021-10-21 10:09:28 +03001027 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001028}
Tony Lucka3f5baf2015-06-24 16:58:12 -07001029
1030/**
AKASHI Takahiro4c546b82017-04-03 11:23:54 +09001031 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
1032 * @base: the base phys addr of the region
1033 * @size: the size of the region
1034 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001035 * Return: 0 on success, -errno on failure.
AKASHI Takahiro4c546b82017-04-03 11:23:54 +09001036 */
1037int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
1038{
1039 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
1040}
1041
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -07001042static bool should_skip_region(struct memblock_type *type,
1043 struct memblock_region *m,
1044 int nid, int flags)
Mike Rapoportc9a688a2019-03-11 23:30:50 -07001045{
1046 int m_nid = memblock_get_region_node(m);
1047
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -07001048 /* we never skip regions when iterating memblock.reserved or physmem */
1049 if (type != memblock_memory)
1050 return false;
1051
Mike Rapoportc9a688a2019-03-11 23:30:50 -07001052 /* only memory regions are associated with nodes, check it */
Mike Rapoport (IBM)fdebee52024-06-14 11:05:43 +03001053 if (numa_valid_node(nid) && nid != m_nid)
Mike Rapoportc9a688a2019-03-11 23:30:50 -07001054 return true;
1055
1056 /* skip hotpluggable memory regions if needed */
Mike Rapoport79e482e2021-07-23 15:50:26 -07001057 if (movable_node_is_enabled() && memblock_is_hotpluggable(m) &&
1058 !(flags & MEMBLOCK_HOTPLUG))
Mike Rapoportc9a688a2019-03-11 23:30:50 -07001059 return true;
1060
1061 /* if we want mirror memory skip non-mirror memory regions */
1062 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1063 return true;
1064
1065 /* skip nomap memory unless we were asked for it explicitly */
1066 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1067 return true;
1068
David Hildenbrandf7892d82021-11-05 13:44:53 -07001069 /* skip driver-managed memory unless we were asked for it explicitly */
1070 if (!(flags & MEMBLOCK_DRIVER_MANAGED) && memblock_is_driver_managed(m))
1071 return true;
1072
Mike Rapoportc9a688a2019-03-11 23:30:50 -07001073 return false;
1074}
1075
Robin Holt8e7a7f82015-06-30 14:56:41 -07001076/**
Mike Rapoporta2974132019-03-11 23:30:54 -07001077 * __next_mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +02001078 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -08001079 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -07001080 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001081 * @type_a: pointer to memblock_type from where the range is taken
1082 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -07001083 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1084 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1085 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +02001086 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001087 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +02001088 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001089 * *@idx contains index into type_a and the upper 32bit indexes the
1090 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +02001091 * look like the following,
1092 *
1093 * 0:[0-16), 1:[32-48), 2:[128-130)
1094 *
1095 * The upper 32bit indexes the following regions.
1096 *
1097 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
1098 *
1099 * As both region arrays are sorted, the function advances the two indices
1100 * in lockstep and returns each intersection.
1101 */
David Hildenbrand77649902020-07-01 16:18:29 +02001102void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
1103 struct memblock_type *type_a,
1104 struct memblock_type *type_b, phys_addr_t *out_start,
1105 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +02001106{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001107 int idx_a = *idx & 0xffffffff;
1108 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001109
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001110 for (; idx_a < type_a->cnt; idx_a++) {
1111 struct memblock_region *m = &type_a->regions[idx_a];
1112
Tejun Heo35fd0802011-07-12 11:15:59 +02001113 phys_addr_t m_start = m->base;
1114 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001115 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +02001116
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -07001117 if (should_skip_region(type_a, m, nid, flags))
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001118 continue;
1119
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001120 if (!type_b) {
1121 if (out_start)
1122 *out_start = m_start;
1123 if (out_end)
1124 *out_end = m_end;
1125 if (out_nid)
1126 *out_nid = m_nid;
1127 idx_a++;
1128 *idx = (u32)idx_a | (u64)idx_b << 32;
1129 return;
1130 }
Tejun Heo35fd0802011-07-12 11:15:59 +02001131
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001132 /* scan areas before each reservation */
1133 for (; idx_b < type_b->cnt + 1; idx_b++) {
1134 struct memblock_region *r;
1135 phys_addr_t r_start;
1136 phys_addr_t r_end;
1137
1138 r = &type_b->regions[idx_b];
1139 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1140 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001141 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001142
1143 /*
1144 * if idx_b advanced past idx_a,
1145 * break out to advance idx_a
1146 */
Tejun Heo35fd0802011-07-12 11:15:59 +02001147 if (r_start >= m_end)
1148 break;
1149 /* if the two regions intersect, we're done */
1150 if (m_start < r_end) {
1151 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001152 *out_start =
1153 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +02001154 if (out_end)
1155 *out_end = min(m_end, r_end);
1156 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001157 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +02001158 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001159 * The region which ends first is
1160 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +02001161 */
1162 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001163 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +02001164 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001165 idx_b++;
1166 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +02001167 return;
1168 }
1169 }
1170 }
1171
1172 /* signal end of iteration */
1173 *idx = ULLONG_MAX;
1174}
1175
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001176/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001177 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1178 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001179 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -07001180 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -07001181 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001182 * @type_a: pointer to memblock_type from where the range is taken
1183 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -07001184 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1185 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1186 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001187 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001188 * Finds the next range from type_a which is not marked as unsuitable
1189 * in type_b.
1190 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001191 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001192 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001193void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1194 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001195 struct memblock_type *type_a,
1196 struct memblock_type *type_b,
1197 phys_addr_t *out_start,
1198 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001199{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001200 int idx_a = *idx & 0xffffffff;
1201 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001202
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001203 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001204 idx_a = type_a->cnt - 1;
zijun_hue47608a2016-08-04 15:32:00 -07001205 if (type_b != NULL)
1206 idx_b = type_b->cnt;
1207 else
1208 idx_b = 0;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001209 }
1210
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001211 for (; idx_a >= 0; idx_a--) {
1212 struct memblock_region *m = &type_a->regions[idx_a];
1213
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001214 phys_addr_t m_start = m->base;
1215 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001216 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001217
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -07001218 if (should_skip_region(type_a, m, nid, flags))
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001219 continue;
1220
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001221 if (!type_b) {
1222 if (out_start)
1223 *out_start = m_start;
1224 if (out_end)
1225 *out_end = m_end;
1226 if (out_nid)
1227 *out_nid = m_nid;
zijun_hufb399b42016-07-28 15:48:56 -07001228 idx_a--;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001229 *idx = (u32)idx_a | (u64)idx_b << 32;
1230 return;
1231 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001232
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001233 /* scan areas before each reservation */
1234 for (; idx_b >= 0; idx_b--) {
1235 struct memblock_region *r;
1236 phys_addr_t r_start;
1237 phys_addr_t r_end;
1238
1239 r = &type_b->regions[idx_b];
1240 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1241 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001242 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001243 /*
1244 * if idx_b advanced past idx_a,
1245 * break out to advance idx_a
1246 */
1247
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001248 if (r_end <= m_start)
1249 break;
1250 /* if the two regions intersect, we're done */
1251 if (m_end > r_start) {
1252 if (out_start)
1253 *out_start = max(m_start, r_start);
1254 if (out_end)
1255 *out_end = min(m_end, r_end);
1256 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001257 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001258 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001259 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001260 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001261 idx_b--;
1262 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001263 return;
1264 }
1265 }
1266 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001267 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001268 *idx = ULLONG_MAX;
1269}
1270
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001271/*
Chen Chang45e79812018-11-16 15:08:57 -08001272 * Common iterator interface used to define for_each_mem_pfn_range().
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001273 */
1274void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1275 unsigned long *out_start_pfn,
1276 unsigned long *out_end_pfn, int *out_nid)
1277{
1278 struct memblock_type *type = &memblock.memory;
1279 struct memblock_region *r;
Mike Rapoportd622abf2020-06-03 15:56:53 -07001280 int r_nid;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001281
1282 while (++*idx < type->cnt) {
1283 r = &type->regions[*idx];
Mike Rapoportd622abf2020-06-03 15:56:53 -07001284 r_nid = memblock_get_region_node(r);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001285
1286 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1287 continue;
Mike Rapoport (IBM)fdebee52024-06-14 11:05:43 +03001288 if (!numa_valid_node(nid) || nid == r_nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001289 break;
1290 }
1291 if (*idx >= type->cnt) {
1292 *idx = -1;
1293 return;
1294 }
1295
1296 if (out_start_pfn)
1297 *out_start_pfn = PFN_UP(r->base);
1298 if (out_end_pfn)
1299 *out_end_pfn = PFN_DOWN(r->base + r->size);
1300 if (out_nid)
Mike Rapoportd622abf2020-06-03 15:56:53 -07001301 *out_nid = r_nid;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001302}
1303
1304/**
1305 * memblock_set_node - set node ID on memblock regions
1306 * @base: base of area to set node ID for
1307 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001308 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001309 * @nid: node ID to set
1310 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001311 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001312 * Regions which cross the area boundaries are split as necessary.
1313 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001314 * Return:
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001315 * 0 on success, -errno on failure.
1316 */
1317int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001318 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001319{
Mike Rapoporta9ee6cf2021-06-28 19:43:01 -07001320#ifdef CONFIG_NUMA
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001321 int start_rgn, end_rgn;
1322 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001323
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001324 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1325 if (ret)
1326 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001327
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001328 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001329 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001330
Peng Zhang2fe03412023-01-29 17:00:34 +08001331 memblock_merge_regions(type, start_rgn, end_rgn);
Mike Rapoport3f08a302020-06-03 15:57:02 -07001332#endif
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001333 return 0;
1334}
Mike Rapoport3f08a302020-06-03 15:57:02 -07001335
Alexander Duyck837566e2019-05-13 17:21:17 -07001336#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1337/**
1338 * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1339 *
1340 * @idx: pointer to u64 loop variable
1341 * @zone: zone in which all of the memory blocks reside
1342 * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1343 * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1344 *
1345 * This function is meant to be a zone/pfn specific wrapper for the
1346 * for_each_mem_range type iterators. Specifically they are used in the
1347 * deferred memory init routines and as such we were duplicating much of
1348 * this logic throughout the code. So instead of having it in multiple
1349 * locations it seemed like it would make more sense to centralize this to
1350 * one new iterator that does everything they need.
1351 */
1352void __init_memblock
1353__next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1354 unsigned long *out_spfn, unsigned long *out_epfn)
1355{
1356 int zone_nid = zone_to_nid(zone);
1357 phys_addr_t spa, epa;
Alexander Duyck837566e2019-05-13 17:21:17 -07001358
1359 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1360 &memblock.memory, &memblock.reserved,
Miaohe Linf30b0022022-02-17 22:07:54 +08001361 &spa, &epa, NULL);
Alexander Duyck837566e2019-05-13 17:21:17 -07001362
1363 while (*idx != U64_MAX) {
1364 unsigned long epfn = PFN_DOWN(epa);
1365 unsigned long spfn = PFN_UP(spa);
1366
1367 /*
1368 * Verify the end is at least past the start of the zone and
1369 * that we have at least one PFN to initialize.
1370 */
1371 if (zone->zone_start_pfn < epfn && spfn < epfn) {
1372 /* if we went too far just stop searching */
1373 if (zone_end_pfn(zone) <= spfn) {
1374 *idx = U64_MAX;
1375 break;
1376 }
1377
1378 if (out_spfn)
1379 *out_spfn = max(zone->zone_start_pfn, spfn);
1380 if (out_epfn)
1381 *out_epfn = min(zone_end_pfn(zone), epfn);
1382
1383 return;
1384 }
1385
1386 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1387 &memblock.memory, &memblock.reserved,
Miaohe Linf30b0022022-02-17 22:07:54 +08001388 &spa, &epa, NULL);
Alexander Duyck837566e2019-05-13 17:21:17 -07001389 }
1390
1391 /* signal end of iteration */
1392 if (out_spfn)
1393 *out_spfn = ULONG_MAX;
1394 if (out_epfn)
1395 *out_epfn = 0;
1396}
1397
1398#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001399
Mike Rapoport92d12f92019-03-11 23:29:41 -07001400/**
1401 * memblock_alloc_range_nid - allocate boot memory block
1402 * @size: size of memory block to be allocated in bytes
1403 * @align: alignment of the region and block's size
1404 * @start: the lower bound of the memory region to allocate (phys address)
1405 * @end: the upper bound of the memory region to allocate (phys address)
1406 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001407 * @exact_nid: control the allocation fall back to other nodes
Mike Rapoport92d12f92019-03-11 23:29:41 -07001408 *
1409 * The allocation is performed from memory region limited by
Cao jin95830662019-11-30 17:56:24 -08001410 * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
Mike Rapoport92d12f92019-03-11 23:29:41 -07001411 *
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001412 * If the specified node can not hold the requested memory and @exact_nid
1413 * is false, the allocation falls back to any node in the system.
Mike Rapoport92d12f92019-03-11 23:29:41 -07001414 *
1415 * For systems with memory mirroring, the allocation is attempted first
1416 * from the regions with mirroring enabled and then retried from any
1417 * memory region.
1418 *
Patrick Wangc200d9002022-06-11 11:55:48 +08001419 * In addition, function using kmemleak_alloc_phys for allocated boot
1420 * memory block, it is never reported as leaks.
Mike Rapoport92d12f92019-03-11 23:29:41 -07001421 *
1422 * Return:
1423 * Physical address of allocated memory block on success, %0 on failure.
1424 */
Aslan Bakirov8676af12020-04-10 14:32:42 -07001425phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001426 phys_addr_t align, phys_addr_t start,
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001427 phys_addr_t end, int nid,
1428 bool exact_nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001429{
Mike Rapoport92d12f92019-03-11 23:29:41 -07001430 enum memblock_flags flags = choose_memblock_flags();
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001431 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001432
Mike Rapoport2f770802018-10-30 15:10:01 -07001433 if (!align) {
1434 /* Can't use WARNs this early in boot on powerpc */
1435 dump_stack();
1436 align = SMP_CACHE_BYTES;
1437 }
1438
Mike Rapoport92d12f92019-03-11 23:29:41 -07001439again:
Tony Luckfc6daaf2015-06-24 16:58:09 -07001440 found = memblock_find_in_range_node(size, align, start, end, nid,
1441 flags);
Mike Rapoport92d12f92019-03-11 23:29:41 -07001442 if (found && !memblock_reserve(found, size))
1443 goto done;
1444
Mike Rapoport (IBM)fdebee52024-06-14 11:05:43 +03001445 if (numa_valid_node(nid) && !exact_nid) {
Mike Rapoport92d12f92019-03-11 23:29:41 -07001446 found = memblock_find_in_range_node(size, align, start,
1447 end, NUMA_NO_NODE,
1448 flags);
1449 if (found && !memblock_reserve(found, size))
1450 goto done;
1451 }
1452
1453 if (flags & MEMBLOCK_MIRROR) {
1454 flags &= ~MEMBLOCK_MIRROR;
Ma Wupeng14d9a672022-06-14 17:21:53 +08001455 pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n",
Mike Rapoport92d12f92019-03-11 23:29:41 -07001456 &size);
1457 goto again;
1458 }
1459
1460 return 0;
1461
1462done:
Qian Caic6975d72021-11-05 11:05:09 -04001463 /*
1464 * Skip kmemleak for those places like kasan_init() and
1465 * early_pgtable_alloc() due to high volume.
1466 */
1467 if (end != MEMBLOCK_ALLOC_NOLEAKTRACE)
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001468 /*
Patrick Wangc200d9002022-06-11 11:55:48 +08001469 * Memblock allocated blocks are never reported as
1470 * leaks. This is because many of these blocks are
1471 * only referred via the physical address which is
1472 * not looked up by kmemleak.
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001473 */
Patrick Wangc200d9002022-06-11 11:55:48 +08001474 kmemleak_alloc_phys(found, size, 0);
Mike Rapoport92d12f92019-03-11 23:29:41 -07001475
Kirill A. Shutemovdcdfdd42023-06-06 17:26:29 +03001476 /*
1477 * Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP,
1478 * require memory to be accepted before it can be used by the
1479 * guest.
1480 *
1481 * Accept the memory of the allocated buffer.
1482 */
1483 accept_memory(found, found + size);
1484
Mike Rapoport92d12f92019-03-11 23:29:41 -07001485 return found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001486}
1487
Mike Rapoporta2974132019-03-11 23:30:54 -07001488/**
1489 * memblock_phys_alloc_range - allocate a memory block inside specified range
1490 * @size: size of memory block to be allocated in bytes
1491 * @align: alignment of the region and block's size
1492 * @start: the lower bound of the memory region to allocate (physical address)
1493 * @end: the upper bound of the memory region to allocate (physical address)
1494 *
1495 * Allocate @size bytes in the between @start and @end.
1496 *
1497 * Return: physical address of the allocated memory block on success,
1498 * %0 on failure.
1499 */
Mike Rapoport8a770c22019-03-11 23:29:16 -07001500phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
1501 phys_addr_t align,
1502 phys_addr_t start,
1503 phys_addr_t end)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001504{
Faiyaz Mohammedb5cf2d62020-11-16 10:14:04 +05301505 memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n",
1506 __func__, (u64)size, (u64)align, &start, &end,
1507 (void *)_RET_IP_);
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001508 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1509 false);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001510}
1511
Mike Rapoporta2974132019-03-11 23:30:54 -07001512/**
Levi Yun17cbe032021-01-20 21:28:18 +09001513 * memblock_phys_alloc_try_nid - allocate a memory block from specified NUMA node
Mike Rapoporta2974132019-03-11 23:30:54 -07001514 * @size: size of memory block to be allocated in bytes
1515 * @align: alignment of the region and block's size
1516 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1517 *
1518 * Allocates memory block from the specified NUMA node. If the node
1519 * has no available memory, attempts to allocated from any node in the
1520 * system.
1521 *
1522 * Return: physical address of the allocated memory block on success,
1523 * %0 on failure.
1524 */
Mike Rapoport9a8dd702018-10-30 15:07:59 -07001525phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001526{
Mike Rapoport33755572019-03-11 23:29:21 -07001527 return memblock_alloc_range_nid(size, align, 0,
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001528 MEMBLOCK_ALLOC_ACCESSIBLE, nid, false);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001529}
1530
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001531/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001532 * memblock_alloc_internal - allocate boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001533 * @size: size of memory block to be allocated in bytes
1534 * @align: alignment of the region and block's size
1535 * @min_addr: the lower bound of the memory region to allocate (phys address)
1536 * @max_addr: the upper bound of the memory region to allocate (phys address)
1537 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001538 * @exact_nid: control the allocation fall back to other nodes
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001539 *
Mike Rapoport92d12f92019-03-11 23:29:41 -07001540 * Allocates memory block using memblock_alloc_range_nid() and
1541 * converts the returned physical address to virtual.
1542 *
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001543 * The @min_addr limit is dropped if it can not be satisfied and the allocation
Mike Rapoport92d12f92019-03-11 23:29:41 -07001544 * will fall back to memory below @min_addr. Other constraints, such
1545 * as node and mirrored memory will be handled again in
1546 * memblock_alloc_range_nid().
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001547 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001548 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001549 * Virtual address of allocated memory block on success, NULL on failure.
1550 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001551static void * __init memblock_alloc_internal(
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001552 phys_addr_t size, phys_addr_t align,
1553 phys_addr_t min_addr, phys_addr_t max_addr,
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001554 int nid, bool exact_nid)
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001555{
1556 phys_addr_t alloc;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001557
1558 /*
1559 * Detect any accidental use of these APIs after slab is ready, as at
1560 * this moment memblock may be deinitialized already and its
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -07001561 * internal data may be destroyed (after execution of memblock_free_all)
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001562 */
1563 if (WARN_ON_ONCE(slab_is_available()))
1564 return kzalloc_node(size, GFP_NOWAIT, nid);
1565
Mike Rapoportf3057ad2019-10-18 20:20:01 -07001566 if (max_addr > memblock.current_limit)
1567 max_addr = memblock.current_limit;
1568
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001569 alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid,
1570 exact_nid);
Mike Rapoport2f770802018-10-30 15:10:01 -07001571
Mike Rapoport92d12f92019-03-11 23:29:41 -07001572 /* retry allocation without lower limit */
1573 if (!alloc && min_addr)
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001574 alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid,
1575 exact_nid);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001576
Mike Rapoport92d12f92019-03-11 23:29:41 -07001577 if (!alloc)
1578 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001579
Mike Rapoport92d12f92019-03-11 23:29:41 -07001580 return phys_to_virt(alloc);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001581}
1582
1583/**
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001584 * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
1585 * without zeroing memory
1586 * @size: size of memory block to be allocated in bytes
1587 * @align: alignment of the region and block's size
1588 * @min_addr: the lower bound of the memory region from where the allocation
1589 * is preferred (phys address)
1590 * @max_addr: the upper bound of the memory region from where the allocation
1591 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1592 * allocate only from memory limited by memblock.current_limit value
1593 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1594 *
1595 * Public function, provides additional debug information (including caller
1596 * info), if enabled. Does not zero allocated memory.
1597 *
1598 * Return:
1599 * Virtual address of allocated memory block on success, NULL on failure.
1600 */
1601void * __init memblock_alloc_exact_nid_raw(
1602 phys_addr_t size, phys_addr_t align,
1603 phys_addr_t min_addr, phys_addr_t max_addr,
1604 int nid)
1605{
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001606 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1607 __func__, (u64)size, (u64)align, nid, &min_addr,
1608 &max_addr, (void *)_RET_IP_);
1609
Mike Rapoport08678802021-09-02 14:58:05 -07001610 return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
1611 true);
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001612}
1613
1614/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001615 * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001616 * memory and without panicking
1617 * @size: size of memory block to be allocated in bytes
1618 * @align: alignment of the region and block's size
1619 * @min_addr: the lower bound of the memory region from where the allocation
1620 * is preferred (phys address)
1621 * @max_addr: the upper bound of the memory region from where the allocation
Mike Rapoport97ad1082018-10-30 15:09:44 -07001622 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001623 * allocate only from memory limited by memblock.current_limit value
1624 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1625 *
1626 * Public function, provides additional debug information (including caller
1627 * info), if enabled. Does not zero allocated memory, does not panic if request
1628 * cannot be satisfied.
1629 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001630 * Return:
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001631 * Virtual address of allocated memory block on success, NULL on failure.
1632 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001633void * __init memblock_alloc_try_nid_raw(
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001634 phys_addr_t size, phys_addr_t align,
1635 phys_addr_t min_addr, phys_addr_t max_addr,
1636 int nid)
1637{
Sakari Ailusd75f7732019-03-25 21:32:28 +02001638 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
Mike Rapoporta36aab82018-08-17 15:47:17 -07001639 __func__, (u64)size, (u64)align, nid, &min_addr,
1640 &max_addr, (void *)_RET_IP_);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001641
Mike Rapoport08678802021-09-02 14:58:05 -07001642 return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
1643 false);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001644}
1645
1646/**
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001647 * memblock_alloc_try_nid - allocate boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001648 * @size: size of memory block to be allocated in bytes
1649 * @align: alignment of the region and block's size
1650 * @min_addr: the lower bound of the memory region from where the allocation
1651 * is preferred (phys address)
1652 * @max_addr: the upper bound of the memory region from where the allocation
Mike Rapoport97ad1082018-10-30 15:09:44 -07001653 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001654 * allocate only from memory limited by memblock.current_limit value
1655 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1656 *
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001657 * Public function, provides additional debug information (including caller
1658 * info), if enabled. This function zeroes the allocated memory.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001659 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001660 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001661 * Virtual address of allocated memory block on success, NULL on failure.
1662 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001663void * __init memblock_alloc_try_nid(
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001664 phys_addr_t size, phys_addr_t align,
1665 phys_addr_t min_addr, phys_addr_t max_addr,
1666 int nid)
1667{
1668 void *ptr;
1669
Sakari Ailusd75f7732019-03-25 21:32:28 +02001670 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
Mike Rapoporta36aab82018-08-17 15:47:17 -07001671 __func__, (u64)size, (u64)align, nid, &min_addr,
1672 &max_addr, (void *)_RET_IP_);
Mike Rapoporteb31d552018-10-30 15:08:04 -07001673 ptr = memblock_alloc_internal(size, align,
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001674 min_addr, max_addr, nid, false);
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001675 if (ptr)
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001676 memset(ptr, 0, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001677
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001678 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001679}
1680
1681/**
Mike Rapoport621d9732021-11-05 13:43:16 -07001682 * memblock_free_late - free pages directly to buddy allocator
Mike Rapoport48a833c2018-06-30 17:55:03 +03001683 * @base: phys starting address of the boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001684 * @size: size of the boot memory block in bytes
1685 *
Mike Rapoporta2974132019-03-11 23:30:54 -07001686 * This is only useful when the memblock allocator has already been torn
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001687 * down, but we are still initializing the system. Pages are released directly
Mike Rapoporta2974132019-03-11 23:30:54 -07001688 * to the buddy allocator.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001689 */
Mike Rapoport621d9732021-11-05 13:43:16 -07001690void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001691{
Mike Rapoporta36aab82018-08-17 15:47:17 -07001692 phys_addr_t cursor, end;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001693
Mike Rapoporta36aab82018-08-17 15:47:17 -07001694 end = base + size - 1;
Sakari Ailusd75f7732019-03-25 21:32:28 +02001695 memblock_dbg("%s: [%pa-%pa] %pS\n",
Mike Rapoporta36aab82018-08-17 15:47:17 -07001696 __func__, &base, &end, (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001697 kmemleak_free_part_phys(base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001698 cursor = PFN_UP(base);
1699 end = PFN_DOWN(base + size);
1700
1701 for (; cursor < end; cursor++) {
Aaron Thompson647037a2023-02-07 08:21:51 +00001702 memblock_free_pages(pfn_to_page(cursor), cursor, 0);
Arun KSca79b0c2018-12-28 00:34:29 -08001703 totalram_pages_inc();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001704 }
1705}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001706
1707/*
1708 * Remaining API functions
1709 */
1710
David Gibson1f1ffb8a2016-02-05 15:36:19 -08001711phys_addr_t __init_memblock memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001712{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001713 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001714}
1715
Srikar Dronamraju8907de52016-10-07 16:59:18 -07001716phys_addr_t __init_memblock memblock_reserved_size(void)
1717{
1718 return memblock.reserved.total_size;
1719}
1720
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001721/* lowest address */
1722phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1723{
1724 return memblock.memory.regions[0].base;
1725}
1726
Yinghai Lu10d06432010-07-28 15:43:02 +10001727phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001728{
1729 int idx = memblock.memory.cnt - 1;
1730
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001731 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001732}
1733
Dennis Chena571d4e2016-07-28 15:48:26 -07001734static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001735{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001736 phys_addr_t max_addr = PHYS_ADDR_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001737 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001738
Dennis Chena571d4e2016-07-28 15:48:26 -07001739 /*
1740 * translate the memory @limit size into the max address within one of
1741 * the memory memblock regions, if the @limit exceeds the total size
Stefan Agner1c4bc432018-06-07 17:06:15 -07001742 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
Dennis Chena571d4e2016-07-28 15:48:26 -07001743 */
Mike Rapoportcc6de1682020-10-13 16:58:30 -07001744 for_each_mem_region(r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001745 if (limit <= r->size) {
1746 max_addr = r->base + limit;
1747 break;
1748 }
1749 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001750 }
1751
Dennis Chena571d4e2016-07-28 15:48:26 -07001752 return max_addr;
1753}
1754
1755void __init memblock_enforce_memory_limit(phys_addr_t limit)
1756{
Colin Ian King49aef712020-04-01 21:11:01 -07001757 phys_addr_t max_addr;
Dennis Chena571d4e2016-07-28 15:48:26 -07001758
1759 if (!limit)
1760 return;
1761
1762 max_addr = __find_max_addr(limit);
1763
1764 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001765 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001766 return;
1767
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001768 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001769 memblock_remove_range(&memblock.memory, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001770 PHYS_ADDR_MAX);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001771 memblock_remove_range(&memblock.reserved, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001772 PHYS_ADDR_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001773}
1774
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001775void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1776{
1777 int start_rgn, end_rgn;
1778 int i, ret;
1779
1780 if (!size)
1781 return;
1782
Peng Fan5173ed72021-10-18 15:15:45 -07001783 if (!memblock_memory->total_size) {
Geert Uytterhoevene888fa72021-08-11 10:55:18 +02001784 pr_warn("%s: No memory registered yet\n", __func__);
1785 return;
1786 }
1787
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001788 ret = memblock_isolate_range(&memblock.memory, base, size,
1789 &start_rgn, &end_rgn);
1790 if (ret)
1791 return;
1792
1793 /* remove all the MAP regions */
1794 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1795 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1796 memblock_remove_region(&memblock.memory, i);
1797
1798 for (i = start_rgn - 1; i >= 0; i--)
1799 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1800 memblock_remove_region(&memblock.memory, i);
1801
1802 /* truncate the reserved regions */
1803 memblock_remove_range(&memblock.reserved, 0, base);
1804 memblock_remove_range(&memblock.reserved,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001805 base + size, PHYS_ADDR_MAX);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001806}
1807
Dennis Chena571d4e2016-07-28 15:48:26 -07001808void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1809{
Dennis Chena571d4e2016-07-28 15:48:26 -07001810 phys_addr_t max_addr;
Dennis Chena571d4e2016-07-28 15:48:26 -07001811
1812 if (!limit)
1813 return;
1814
1815 max_addr = __find_max_addr(limit);
1816
1817 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001818 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001819 return;
1820
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001821 memblock_cap_memory_range(0, max_addr);
Dennis Chena571d4e2016-07-28 15:48:26 -07001822}
1823
Yinghai Lucd794812010-10-11 12:34:09 -07001824static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001825{
1826 unsigned int left = 0, right = type->cnt;
1827
1828 do {
1829 unsigned int mid = (right + left) / 2;
1830
1831 if (addr < type->regions[mid].base)
1832 right = mid;
1833 else if (addr >= (type->regions[mid].base +
1834 type->regions[mid].size))
1835 left = mid + 1;
1836 else
1837 return mid;
1838 } while (left < right);
1839 return -1;
1840}
1841
Yueyi Lif5a222d2018-12-14 14:17:06 -08001842bool __init_memblock memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001843{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001844 return memblock_search(&memblock.reserved, addr) != -1;
1845}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001846
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001847bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001848{
1849 return memblock_search(&memblock.memory, addr) != -1;
1850}
1851
Yaowei Bai937f0c22018-02-06 15:41:18 -08001852bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001853{
1854 int i = memblock_search(&memblock.memory, addr);
1855
1856 if (i == -1)
1857 return false;
1858 return !memblock_is_nomap(&memblock.memory.regions[i]);
1859}
1860
Yinghai Lue76b63f2013-09-11 14:22:17 -07001861int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1862 unsigned long *start_pfn, unsigned long *end_pfn)
1863{
1864 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001865 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001866
1867 if (mid == -1)
1868 return -1;
1869
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001870 *start_pfn = PFN_DOWN(type->regions[mid].base);
1871 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001872
Mike Rapoportd622abf2020-06-03 15:56:53 -07001873 return memblock_get_region_node(&type->regions[mid]);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001874}
Yinghai Lue76b63f2013-09-11 14:22:17 -07001875
Stephen Boydeab30942012-05-24 00:45:21 -07001876/**
1877 * memblock_is_region_memory - check if a region is a subset of memory
1878 * @base: base of region to check
1879 * @size: size of region to check
1880 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001881 * Check if the region [@base, @base + @size) is a subset of a memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001882 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001883 * Return:
Stephen Boydeab30942012-05-24 00:45:21 -07001884 * 0 if false, non-zero if true
1885 */
Yaowei Bai937f0c22018-02-06 15:41:18 -08001886bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001887{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001888 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001889 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001890
1891 if (idx == -1)
Yaowei Bai937f0c22018-02-06 15:41:18 -08001892 return false;
Wei Yangef415ef2017-02-22 15:45:04 -08001893 return (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001894 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001895}
1896
Stephen Boydeab30942012-05-24 00:45:21 -07001897/**
1898 * memblock_is_region_reserved - check if a region intersects reserved memory
1899 * @base: base of region to check
1900 * @size: size of region to check
1901 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001902 * Check if the region [@base, @base + @size) intersects a reserved
1903 * memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001904 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001905 * Return:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001906 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001907 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001908bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001909{
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001910 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001911}
1912
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001913void __init_memblock memblock_trim_memory(phys_addr_t align)
1914{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001915 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001916 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001917
Mike Rapoportcc6de1682020-10-13 16:58:30 -07001918 for_each_mem_region(r) {
Emil Medve136199f2014-04-07 15:37:52 -07001919 orig_start = r->base;
1920 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001921 start = round_up(orig_start, align);
1922 end = round_down(orig_end, align);
1923
1924 if (start == orig_start && end == orig_end)
1925 continue;
1926
1927 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001928 r->base = start;
1929 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001930 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001931 memblock_remove_region(&memblock.memory,
1932 r - memblock.memory.regions);
1933 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001934 }
1935 }
1936}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001937
Yinghai Lu3661ca62010-09-15 13:05:29 -07001938void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001939{
1940 memblock.current_limit = limit;
1941}
1942
Laura Abbottfec51012014-02-27 01:23:43 +01001943phys_addr_t __init_memblock memblock_get_current_limit(void)
1944{
1945 return memblock.current_limit;
1946}
1947
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001948static void __init_memblock memblock_dump(struct memblock_type *type)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001949{
Miles Chen5d63f812017-02-22 15:46:42 -08001950 phys_addr_t base, end, size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001951 enum memblock_flags flags;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001952 int idx;
1953 struct memblock_region *rgn;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001954
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001955 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001956
Gioh Kim66e8b432017-11-15 17:33:42 -08001957 for_each_memblock_type(idx, type, rgn) {
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001958 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001959
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001960 base = rgn->base;
1961 size = rgn->size;
Miles Chen5d63f812017-02-22 15:46:42 -08001962 end = base + size - 1;
Tang Chen66a20752014-01-21 15:49:20 -08001963 flags = rgn->flags;
Mike Rapoporta9ee6cf2021-06-28 19:43:01 -07001964#ifdef CONFIG_NUMA
Mike Rapoport (IBM)fdebee52024-06-14 11:05:43 +03001965 if (numa_valid_node(memblock_get_region_node(rgn)))
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001966 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1967 memblock_get_region_node(rgn));
1968#endif
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001969 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001970 type->name, idx, &base, &end, &size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001971 }
1972}
1973
Mike Rapoport87c55872020-10-13 16:57:54 -07001974static void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001975{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001976 pr_info("MEMBLOCK configuration:\n");
Miles Chen5d63f812017-02-22 15:46:42 -08001977 pr_info(" memory size = %pa reserved size = %pa\n",
1978 &memblock.memory.total_size,
1979 &memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001980
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001981 memblock_dump(&memblock.memory);
1982 memblock_dump(&memblock.reserved);
Heiko Carstens409efd42017-02-24 14:55:56 -08001983#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
David Hildenbrand77649902020-07-01 16:18:29 +02001984 memblock_dump(&physmem);
Heiko Carstens409efd42017-02-24 14:55:56 -08001985#endif
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001986}
1987
Mike Rapoport87c55872020-10-13 16:57:54 -07001988void __init_memblock memblock_dump_all(void)
1989{
1990 if (memblock_debug)
1991 __memblock_dump_all();
1992}
1993
Tejun Heo1aadc052011-12-08 10:22:08 -08001994void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001995{
Benjamin Herrenschmidt142b45a72010-07-06 15:39:13 -07001996 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001997}
1998
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001999static int __init early_memblock(char *p)
2000{
2001 if (p && strstr(p, "debug"))
2002 memblock_debug = 1;
2003 return 0;
2004}
2005early_param("memblock", early_memblock);
2006
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002007static void __init free_memmap(unsigned long start_pfn, unsigned long end_pfn)
2008{
2009 struct page *start_pg, *end_pg;
2010 phys_addr_t pg, pgend;
2011
2012 /*
2013 * Convert start_pfn/end_pfn to a struct page pointer.
2014 */
2015 start_pg = pfn_to_page(start_pfn - 1) + 1;
2016 end_pg = pfn_to_page(end_pfn - 1) + 1;
2017
2018 /*
2019 * Convert to physical addresses, and round start upwards and end
2020 * downwards.
2021 */
2022 pg = PAGE_ALIGN(__pa(start_pg));
2023 pgend = __pa(end_pg) & PAGE_MASK;
2024
2025 /*
2026 * If there are free pages between these, free the section of the
2027 * memmap array.
2028 */
2029 if (pg < pgend)
Mike Rapoport3ecc6832021-11-05 13:43:19 -07002030 memblock_phys_free(pg, pgend - pg);
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002031}
2032
2033/*
2034 * The mem_map array can get very big. Free the unused area of the memory map.
2035 */
2036static void __init free_unused_memmap(void)
2037{
2038 unsigned long start, end, prev_end = 0;
2039 int i;
2040
2041 if (!IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) ||
2042 IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
2043 return;
2044
2045 /*
2046 * This relies on each bank being in address order.
2047 * The banks are sorted previously in bootmem_init().
2048 */
2049 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
2050#ifdef CONFIG_SPARSEMEM
2051 /*
2052 * Take care not to free memmap entries that don't exist
2053 * due to SPARSEMEM sections which aren't present.
2054 */
2055 start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002056#endif
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002057 /*
Mike Rapoporte2a86802021-05-17 21:15:15 +03002058 * Align down here since many operations in VM subsystem
2059 * presume that there are no holes in the memory map inside
2060 * a pageblock
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002061 */
Kefeng Wang4f9bc692022-09-07 14:08:42 +08002062 start = pageblock_start_pfn(start);
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002063
2064 /*
2065 * If we had a previous bank, and there is a space
2066 * between the current bank and the previous, free it.
2067 */
2068 if (prev_end && prev_end < start)
2069 free_memmap(prev_end, start);
2070
2071 /*
Mike Rapoporte2a86802021-05-17 21:15:15 +03002072 * Align up here since many operations in VM subsystem
2073 * presume that there are no holes in the memory map inside
2074 * a pageblock
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002075 */
Kefeng Wang5f7fa132022-09-07 14:08:43 +08002076 prev_end = pageblock_align(end);
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002077 }
2078
2079#ifdef CONFIG_SPARSEMEM
Mike Rapoportf921f532021-05-17 21:31:59 +03002080 if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) {
Kefeng Wang5f7fa132022-09-07 14:08:43 +08002081 prev_end = pageblock_align(end);
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002082 free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
Mike Rapoportf921f532021-05-17 21:31:59 +03002083 }
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002084#endif
2085}
2086
Mike Rapoportbda49a82018-10-30 15:09:40 -07002087static void __init __free_pages_memory(unsigned long start, unsigned long end)
2088{
2089 int order;
2090
2091 while (start < end) {
Kirill A. Shutemov59f876f2023-04-06 10:25:29 +03002092 /*
2093 * Free the pages in the largest chunks alignment allows.
2094 *
2095 * __ffs() behaviour is undefined for 0. start == 0 is
2096 * MAX_ORDER-aligned, set order to MAX_ORDER for the case.
2097 */
2098 if (start)
2099 order = min_t(int, MAX_ORDER, __ffs(start));
2100 else
2101 order = MAX_ORDER;
Mike Rapoportbda49a82018-10-30 15:09:40 -07002102
2103 while (start + (1UL << order) > end)
2104 order--;
2105
2106 memblock_free_pages(pfn_to_page(start), start, order);
2107
2108 start += (1UL << order);
2109 }
2110}
2111
2112static unsigned long __init __free_memory_core(phys_addr_t start,
2113 phys_addr_t end)
2114{
2115 unsigned long start_pfn = PFN_UP(start);
2116 unsigned long end_pfn = min_t(unsigned long,
2117 PFN_DOWN(end), max_low_pfn);
2118
2119 if (start_pfn >= end_pfn)
2120 return 0;
2121
2122 __free_pages_memory(start_pfn, end_pfn);
2123
2124 return end_pfn - start_pfn;
2125}
2126
Mike Rapoport9092d4f2021-06-30 18:51:16 -07002127static void __init memmap_init_reserved_pages(void)
2128{
2129 struct memblock_region *region;
2130 phys_addr_t start, end;
Yajun Deng61167ad2023-06-19 10:34:06 +08002131 int nid;
Wei Yang4d1a2d12025-03-18 07:19:47 +00002132 unsigned long max_reserved;
Yajun Deng61167ad2023-06-19 10:34:06 +08002133
2134 /*
2135 * set nid on all reserved pages and also treat struct
2136 * pages for the NOMAP regions as PageReserved
2137 */
Wei Yang4d1a2d12025-03-18 07:19:47 +00002138repeat:
2139 max_reserved = memblock.reserved.max;
Yajun Deng61167ad2023-06-19 10:34:06 +08002140 for_each_mem_region(region) {
2141 nid = memblock_get_region_node(region);
2142 start = region->base;
2143 end = start + region->size;
2144
2145 if (memblock_is_nomap(region))
2146 reserve_bootmem_region(start, end, nid);
2147
Wei Yangc0fabec2025-03-18 07:19:46 +00002148 memblock_set_node(start, region->size, &memblock.reserved, nid);
Yajun Deng61167ad2023-06-19 10:34:06 +08002149 }
Wei Yang4d1a2d12025-03-18 07:19:47 +00002150 /*
2151 * 'max' is changed means memblock.reserved has been doubled its
2152 * array, which may result a new reserved region before current
2153 * 'start'. Now we should repeat the procedure to set its node id.
2154 */
2155 if (max_reserved != memblock.reserved.max)
2156 goto repeat;
Mike Rapoport9092d4f2021-06-30 18:51:16 -07002157
2158 /* initialize struct pages for the reserved regions */
Yajun Deng61167ad2023-06-19 10:34:06 +08002159 for_each_reserved_mem_region(region) {
2160 nid = memblock_get_region_node(region);
2161 start = region->base;
2162 end = start + region->size;
Mike Rapoport9092d4f2021-06-30 18:51:16 -07002163
Mike Rapoport (IBM)fdebee52024-06-14 11:05:43 +03002164 if (!numa_valid_node(nid))
Yajun Denge791a342024-01-18 14:18:53 +08002165 nid = early_pfn_to_nid(PFN_DOWN(start));
2166
Yajun Deng61167ad2023-06-19 10:34:06 +08002167 reserve_bootmem_region(start, end, nid);
Mike Rapoport9092d4f2021-06-30 18:51:16 -07002168 }
2169}
2170
Mike Rapoportbda49a82018-10-30 15:09:40 -07002171static unsigned long __init free_low_memory_core_early(void)
2172{
2173 unsigned long count = 0;
2174 phys_addr_t start, end;
2175 u64 i;
2176
2177 memblock_clear_hotplug(0, -1);
2178
Mike Rapoport9092d4f2021-06-30 18:51:16 -07002179 memmap_init_reserved_pages();
Mike Rapoportbda49a82018-10-30 15:09:40 -07002180
2181 /*
2182 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
2183 * because in some case like Node0 doesn't have RAM installed
2184 * low ram will be on Node1
2185 */
2186 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
2187 NULL)
2188 count += __free_memory_core(start, end);
2189
2190 return count;
2191}
2192
2193static int reset_managed_pages_done __initdata;
2194
Haifeng Xua6689682023-06-07 02:45:48 +00002195static void __init reset_node_managed_pages(pg_data_t *pgdat)
Mike Rapoportbda49a82018-10-30 15:09:40 -07002196{
2197 struct zone *z;
2198
2199 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
Arun KS9705bea2018-12-28 00:34:24 -08002200 atomic_long_set(&z->managed_pages, 0);
Mike Rapoportbda49a82018-10-30 15:09:40 -07002201}
2202
2203void __init reset_all_zones_managed_pages(void)
2204{
2205 struct pglist_data *pgdat;
2206
2207 if (reset_managed_pages_done)
2208 return;
2209
2210 for_each_online_pgdat(pgdat)
2211 reset_node_managed_pages(pgdat);
2212
2213 reset_managed_pages_done = 1;
2214}
2215
2216/**
2217 * memblock_free_all - release free pages to the buddy allocator
Mike Rapoportbda49a82018-10-30 15:09:40 -07002218 */
Daeseok Youn097d43d2021-01-14 16:08:17 +09002219void __init memblock_free_all(void)
Mike Rapoportbda49a82018-10-30 15:09:40 -07002220{
2221 unsigned long pages;
2222
Mike Rapoport4f5b0c12020-12-14 19:09:59 -08002223 free_unused_memmap();
Mike Rapoportbda49a82018-10-30 15:09:40 -07002224 reset_all_zones_managed_pages();
2225
2226 pages = free_low_memory_core_early();
Arun KSca79b0c2018-12-28 00:34:29 -08002227 totalram_pages_add(pages);
Mike Rapoportbda49a82018-10-30 15:09:40 -07002228}
2229
Mike Rapoport350e88b2019-05-13 17:22:59 -07002230#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
Yuwei Guan493f3492023-05-19 18:53:21 +08002231static const char * const flagname[] = {
2232 [ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
2233 [ilog2(MEMBLOCK_MIRROR)] = "MIRROR",
2234 [ilog2(MEMBLOCK_NOMAP)] = "NOMAP",
2235 [ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG",
2236};
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002237
2238static int memblock_debug_show(struct seq_file *m, void *private)
2239{
2240 struct memblock_type *type = m->private;
2241 struct memblock_region *reg;
Yuwei Guande649e72023-06-01 21:31:49 +08002242 int i, j, nid;
Yuwei Guan493f3492023-05-19 18:53:21 +08002243 unsigned int count = ARRAY_SIZE(flagname);
Miles Chen5d63f812017-02-22 15:46:42 -08002244 phys_addr_t end;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002245
2246 for (i = 0; i < type->cnt; i++) {
2247 reg = &type->regions[i];
Miles Chen5d63f812017-02-22 15:46:42 -08002248 end = reg->base + reg->size - 1;
Yuwei Guande649e72023-06-01 21:31:49 +08002249 nid = memblock_get_region_node(reg);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002250
Miles Chen5d63f812017-02-22 15:46:42 -08002251 seq_printf(m, "%4d: ", i);
Yuwei Guan493f3492023-05-19 18:53:21 +08002252 seq_printf(m, "%pa..%pa ", &reg->base, &end);
Mike Rapoport (IBM)fdebee52024-06-14 11:05:43 +03002253 if (numa_valid_node(nid))
Yuwei Guande649e72023-06-01 21:31:49 +08002254 seq_printf(m, "%4d ", nid);
2255 else
2256 seq_printf(m, "%4c ", 'x');
Yuwei Guan493f3492023-05-19 18:53:21 +08002257 if (reg->flags) {
2258 for (j = 0; j < count; j++) {
2259 if (reg->flags & (1U << j)) {
2260 seq_printf(m, "%s\n", flagname[j]);
2261 break;
2262 }
2263 }
2264 if (j == count)
2265 seq_printf(m, "%s\n", "UNKNOWN");
2266 } else {
2267 seq_printf(m, "%s\n", "NONE");
2268 }
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002269 }
2270 return 0;
2271}
Andy Shevchenko5ad35092018-04-05 16:23:16 -07002272DEFINE_SHOW_ATTRIBUTE(memblock_debug);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002273
2274static int __init memblock_init_debugfs(void)
2275{
2276 struct dentry *root = debugfs_create_dir("memblock", NULL);
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08002277
Joe Perches0825a6f2018-06-14 15:27:58 -07002278 debugfs_create_file("memory", 0444, root,
2279 &memblock.memory, &memblock_debug_fops);
2280 debugfs_create_file("reserved", 0444, root,
2281 &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01002282#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
David Hildenbrand77649902020-07-01 16:18:29 +02002283 debugfs_create_file("physmem", 0444, root, &physmem,
2284 &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01002285#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002286
2287 return 0;
2288}
2289__initcall(memblock_init_debugfs);
2290
2291#endif /* CONFIG_DEBUG_FS */