blob: 6661739107d7b5cae14d90f9240ee2c99614bf1e [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 Herrenschmidt142b45a2010-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
Mike Rapoport3e039c52018-06-30 17:55:05 +030032/**
33 * DOC: memblock overview
34 *
35 * Memblock is a method of managing memory regions during the early
36 * boot period when the usual kernel memory allocators are not up and
37 * running.
38 *
39 * Memblock views the system memory as collections of contiguous
40 * regions. There are several types of these collections:
41 *
42 * * ``memory`` - describes the physical memory available to the
43 * kernel; this may differ from the actual physical memory installed
44 * in the system, for instance when the memory is restricted with
45 * ``mem=`` command line parameter
46 * * ``reserved`` - describes the regions that were allocated
David Hildenbrand77649902020-07-01 16:18:29 +020047 * * ``physmem`` - describes the actual physical memory available during
48 * boot regardless of the possible restrictions and memory hot(un)plug;
49 * the ``physmem`` type is only available on some architectures.
Mike Rapoport3e039c52018-06-30 17:55:05 +030050 *
Mauro Carvalho Chehab9303c9d2020-09-25 12:01:25 +020051 * Each region is represented by struct memblock_region that
Mike Rapoport3e039c52018-06-30 17:55:05 +030052 * defines the region extents, its attributes and NUMA node id on NUMA
Mauro Carvalho Chehab1bf162e2020-09-28 15:50:33 +020053 * systems. Every memory type is described by the struct memblock_type
54 * which contains an array of memory regions along with
David Hildenbrand77649902020-07-01 16:18:29 +020055 * the allocator metadata. The "memory" and "reserved" types are nicely
Mauro Carvalho Chehab9303c9d2020-09-25 12:01:25 +020056 * wrapped with struct memblock. This structure is statically
David Hildenbrand77649902020-07-01 16:18:29 +020057 * initialized at build time. The region arrays are initially sized to
58 * %INIT_MEMBLOCK_REGIONS for "memory" and %INIT_MEMBLOCK_RESERVED_REGIONS
59 * for "reserved". The region array for "physmem" is initially sized to
60 * %INIT_PHYSMEM_REGIONS.
Cao jin6e5af9a2019-11-30 17:56:21 -080061 * The memblock_allow_resize() enables automatic resizing of the region
62 * arrays during addition of new regions. This feature should be used
63 * with care so that memory allocated for the region array will not
64 * overlap with areas that should be reserved, for example initrd.
Mike Rapoport3e039c52018-06-30 17:55:05 +030065 *
66 * The early architecture setup should tell memblock what the physical
Cao jin6e5af9a2019-11-30 17:56:21 -080067 * memory layout is by using memblock_add() or memblock_add_node()
68 * functions. The first function does not assign the region to a NUMA
69 * node and it is appropriate for UMA systems. Yet, it is possible to
70 * use it on NUMA systems as well and assign the region to a NUMA node
71 * later in the setup process using memblock_set_node(). The
72 * memblock_add_node() performs such an assignment directly.
Mike Rapoport3e039c52018-06-30 17:55:05 +030073 *
Mike Rapoporta2974132019-03-11 23:30:54 -070074 * Once memblock is setup the memory can be allocated using one of the
75 * API variants:
76 *
Cao jin6e5af9a2019-11-30 17:56:21 -080077 * * memblock_phys_alloc*() - these functions return the **physical**
78 * address of the allocated memory
79 * * memblock_alloc*() - these functions return the **virtual** address
80 * of the allocated memory.
Mike Rapoporta2974132019-03-11 23:30:54 -070081 *
Ethon Pauldf1758d2020-06-04 16:49:16 -070082 * Note, that both API variants use implicit assumptions about allowed
Mike Rapoporta2974132019-03-11 23:30:54 -070083 * memory ranges and the fallback methods. Consult the documentation
Cao jin6e5af9a2019-11-30 17:56:21 -080084 * of memblock_alloc_internal() and memblock_alloc_range_nid()
85 * functions for more elaborate description.
Mike Rapoport3e039c52018-06-30 17:55:05 +030086 *
Cao jin6e5af9a2019-11-30 17:56:21 -080087 * As the system boot progresses, the architecture specific mem_init()
88 * function frees all the memory to the buddy page allocator.
Mike Rapoport3e039c52018-06-30 17:55:05 +030089 *
Cao jin6e5af9a2019-11-30 17:56:21 -080090 * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
David Hildenbrand77649902020-07-01 16:18:29 +020091 * memblock data structures (except "physmem") will be discarded after the
92 * system initialization completes.
Mike Rapoport3e039c52018-06-30 17:55:05 +030093 */
94
Mike Rapoportbda49a82018-10-30 15:09:40 -070095#ifndef CONFIG_NEED_MULTIPLE_NODES
96struct pglist_data __refdata contig_page_data;
97EXPORT_SYMBOL(contig_page_data);
98#endif
99
Tao Huang8b1cb4e2022-04-21 18:10:26 +0800100#if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP)
Simon Xueb6cd53a2021-06-21 11:19:42 +0800101static unsigned long defer_start __initdata;
102static unsigned long defer_end __initdata;
103
104#define DEFAULT_DEFER_FREE_BLOCK_SIZE SZ_256M
105static unsigned long defer_free_block_size __initdata =
106 DEFAULT_DEFER_FREE_BLOCK_SIZE;
107
108static int __init early_defer_free_block_size(char *p)
109{
110 defer_free_block_size = memparse(p, &p);
111
112 pr_debug("defer_free_block_size = 0x%lx\n", defer_free_block_size);
113
114 return 0;
115}
116
117early_param("defer_free_block_size", early_defer_free_block_size);
118#endif
119
Mike Rapoportbda49a82018-10-30 15:09:40 -0700120unsigned long max_low_pfn;
121unsigned long min_low_pfn;
122unsigned long max_pfn;
123unsigned long long max_possible_pfn;
124
Tejun Heofe091c22011-12-08 10:22:07 -0800125static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
Ard Biesheuvel8a5b403d2019-02-15 13:33:32 +0100126static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100127#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
David Hildenbrand77649902020-07-01 16:18:29 +0200128static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS];
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100129#endif
Tejun Heofe091c22011-12-08 10:22:07 -0800130
131struct memblock memblock __initdata_memblock = {
132 .memory.regions = memblock_memory_init_regions,
133 .memory.cnt = 1, /* empty dummy entry */
134 .memory.max = INIT_MEMBLOCK_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800135 .memory.name = "memory",
Tejun Heofe091c22011-12-08 10:22:07 -0800136
137 .reserved.regions = memblock_reserved_init_regions,
138 .reserved.cnt = 1, /* empty dummy entry */
Ard Biesheuvel8a5b403d2019-02-15 13:33:32 +0100139 .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800140 .reserved.name = "reserved",
Tejun Heofe091c22011-12-08 10:22:07 -0800141
Tang Chen79442ed2013-11-12 15:07:59 -0800142 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -0800143 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
144};
Yinghai Lu95f72d12010-07-12 14:36:09 +1000145
David Hildenbrand77649902020-07-01 16:18:29 +0200146#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
147struct memblock_type physmem = {
148 .regions = memblock_physmem_init_regions,
149 .cnt = 1, /* empty dummy entry */
150 .max = INIT_PHYSMEM_REGIONS,
151 .name = "physmem",
152};
153#endif
154
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -0700155/*
156 * keep a pointer to &memblock.memory in the text section to use it in
157 * __next_mem_range() and its helpers.
158 * For architectures that do not keep memblock data after init, this
159 * pointer will be reset to NULL at memblock_discard()
160 */
161static __refdata struct memblock_type *memblock_memory = &memblock.memory;
162
Mike Rapoportcd991db2020-10-13 16:57:49 -0700163#define for_each_memblock_type(i, memblock_type, rgn) \
164 for (i = 0, rgn = &memblock_type->regions[0]; \
165 i < memblock_type->cnt; \
166 i++, rgn = &memblock_type->regions[i])
167
Mike Rapoport87c55872020-10-13 16:57:54 -0700168#define memblock_dbg(fmt, ...) \
169 do { \
170 if (memblock_debug) \
171 pr_info(fmt, ##__VA_ARGS__); \
172 } while (0)
173
174static int memblock_debug __initdata_memblock;
Vijayanand Jitta3c2f1072022-08-11 20:05:52 +0530175static bool memblock_nomap_remove __initdata_memblock;
Tony Lucka3f5baf2015-06-24 16:58:12 -0700176static bool system_has_some_mirror __initdata_memblock = false;
Tejun Heo1aadc052011-12-08 10:22:08 -0800177static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -0700178static int memblock_memory_in_slab __initdata_memblock = 0;
179static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000180
Mike Rapoportc366ea82019-03-11 23:29:46 -0700181static enum memblock_flags __init_memblock choose_memblock_flags(void)
Tony Lucka3f5baf2015-06-24 16:58:12 -0700182{
183 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
184}
185
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800186/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
187static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
188{
Stefan Agner1c4bc432018-06-07 17:06:15 -0700189 return *size = min(*size, PHYS_ADDR_MAX - base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800190}
191
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000192/*
193 * Address comparison utilities
194 */
Yinghai Lu10d06432010-07-28 15:43:02 +1000195static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000196 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000197{
198 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
199}
200
Tang Chen95cf82e2015-09-08 15:02:03 -0700201bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -0700202 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000203{
204 unsigned long i;
205
Mike Rapoport6e634c02021-12-13 17:41:33 +0800206 memblock_cap_size(base, &size);
207
Alexander Kuleshovf14516f2016-01-14 15:20:39 -0800208 for (i = 0; i < type->cnt; i++)
209 if (memblock_addrs_overlap(base, size, type->regions[i].base,
210 type->regions[i].size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000211 break;
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700212 return i < type->cnt;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000213}
214
Mike Rapoport47cec442018-06-30 17:55:02 +0300215/**
Tang Chen79442ed2013-11-12 15:07:59 -0800216 * __memblock_find_range_bottom_up - find free area utility in bottom-up
217 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300218 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
219 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen79442ed2013-11-12 15:07:59 -0800220 * @size: size of free area to find
221 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800222 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700223 * @flags: pick from blocks based on memory attributes
Tang Chen79442ed2013-11-12 15:07:59 -0800224 *
225 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
226 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300227 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800228 * Found address on success, 0 on failure.
229 */
230static phys_addr_t __init_memblock
231__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700232 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300233 enum memblock_flags flags)
Tang Chen79442ed2013-11-12 15:07:59 -0800234{
235 phys_addr_t this_start, this_end, cand;
236 u64 i;
237
Tony Luckfc6daaf2015-06-24 16:58:09 -0700238 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
Tang Chen79442ed2013-11-12 15:07:59 -0800239 this_start = clamp(this_start, start, end);
240 this_end = clamp(this_end, start, end);
241
242 cand = round_up(this_start, align);
243 if (cand < this_end && this_end - cand >= size)
244 return cand;
245 }
246
247 return 0;
248}
249
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800250/**
Tang Chen14028992013-11-12 15:07:57 -0800251 * __memblock_find_range_top_down - find free area utility, in top-down
252 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300253 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
254 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen14028992013-11-12 15:07:57 -0800255 * @size: size of free area to find
256 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800257 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700258 * @flags: pick from blocks based on memory attributes
Tang Chen14028992013-11-12 15:07:57 -0800259 *
260 * Utility called from memblock_find_in_range_node(), find free area top-down.
261 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300262 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800263 * Found address on success, 0 on failure.
Tang Chen14028992013-11-12 15:07:57 -0800264 */
265static phys_addr_t __init_memblock
266__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700267 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300268 enum memblock_flags flags)
Tang Chen14028992013-11-12 15:07:57 -0800269{
270 phys_addr_t this_start, this_end, cand;
271 u64 i;
272
Tony Luckfc6daaf2015-06-24 16:58:09 -0700273 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
274 NULL) {
Tang Chen14028992013-11-12 15:07:57 -0800275 this_start = clamp(this_start, start, end);
276 this_end = clamp(this_end, start, end);
277
278 if (this_end < size)
279 continue;
280
281 cand = round_down(this_end - size, align);
282 if (cand >= this_start)
283 return cand;
284 }
285
286 return 0;
287}
288
289/**
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800290 * memblock_find_in_range_node - find free area in given range and node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800291 * @size: size of free area to find
292 * @align: alignment of free area to find
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800293 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300294 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
295 * %MEMBLOCK_ALLOC_ACCESSIBLE
Grygorii Strashkob1154232014-01-21 15:50:16 -0800296 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700297 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800298 *
299 * Find @size free area aligned to @align in the specified range and node.
300 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300301 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800302 * Found address on success, 0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000303 */
Mike Rapoportc366ea82019-03-11 23:29:46 -0700304static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800305 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300306 phys_addr_t end, int nid,
307 enum memblock_flags flags)
Tang Chenf7210e62013-02-22 16:33:51 -0800308{
Tang Chenf7210e62013-02-22 16:33:51 -0800309 /* pump up @end */
Qian Caifed84c72018-12-28 00:36:29 -0800310 if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
311 end == MEMBLOCK_ALLOC_KASAN)
Tang Chenf7210e62013-02-22 16:33:51 -0800312 end = memblock.current_limit;
313
314 /* avoid allocating the first page */
315 start = max_t(phys_addr_t, start, PAGE_SIZE);
316 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800317
Roman Gushchin1897a8f2021-02-04 18:32:36 -0800318 if (memblock_bottom_up())
319 return __memblock_find_range_bottom_up(start, end, size, align,
320 nid, flags);
321 else
322 return __memblock_find_range_top_down(start, end, size, align,
323 nid, flags);
Tang Chenf7210e62013-02-22 16:33:51 -0800324}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000325
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800326/**
327 * memblock_find_in_range - find free area in given range
328 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300329 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
330 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800331 * @size: size of free area to find
332 * @align: alignment of free area to find
333 *
334 * Find @size free area aligned to @align in the specified range.
335 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300336 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800337 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800338 */
339phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
340 phys_addr_t end, phys_addr_t size,
341 phys_addr_t align)
342{
Tony Lucka3f5baf2015-06-24 16:58:12 -0700343 phys_addr_t ret;
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300344 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -0700345
346again:
347 ret = memblock_find_in_range_node(size, align, start, end,
348 NUMA_NO_NODE, flags);
349
350 if (!ret && (flags & MEMBLOCK_MIRROR)) {
351 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
352 &size);
353 flags &= ~MEMBLOCK_MIRROR;
354 goto again;
355 }
356
357 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800358}
359
Yinghai Lu10d06432010-07-28 15:43:02 +1000360static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000361{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800362 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200363 memmove(&type->regions[r], &type->regions[r + 1],
364 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000365 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000366
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700367 /* Special case for empty arrays */
368 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800369 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700370 type->cnt = 1;
371 type->regions[0].base = 0;
372 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800373 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200374 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700375 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000376}
377
Mike Rapoport350e88b2019-05-13 17:22:59 -0700378#ifndef CONFIG_ARCH_KEEP_MEMBLOCK
Pavel Tatashin3010f872017-08-18 15:16:05 -0700379/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300380 * memblock_discard - discard memory and reserved arrays if they were allocated
Pavel Tatashin3010f872017-08-18 15:16:05 -0700381 */
382void __init memblock_discard(void)
Yinghai Lu29f67382012-07-11 14:02:56 -0700383{
Pavel Tatashin3010f872017-08-18 15:16:05 -0700384 phys_addr_t addr, size;
Yinghai Lu29f67382012-07-11 14:02:56 -0700385
Pavel Tatashin3010f872017-08-18 15:16:05 -0700386 if (memblock.reserved.regions != memblock_reserved_init_regions) {
387 addr = __pa(memblock.reserved.regions);
388 size = PAGE_ALIGN(sizeof(struct memblock_region) *
389 memblock.reserved.max);
Miaohe Lin78706b02022-02-17 22:53:27 +0800390 if (memblock_reserved_in_slab)
391 kfree(memblock.reserved.regions);
392 else
393 __memblock_free_late(addr, size);
Pavel Tatashin3010f872017-08-18 15:16:05 -0700394 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700395
Pavel Tatashin91b540f92017-08-25 15:55:46 -0700396 if (memblock.memory.regions != memblock_memory_init_regions) {
Pavel Tatashin3010f872017-08-18 15:16:05 -0700397 addr = __pa(memblock.memory.regions);
398 size = PAGE_ALIGN(sizeof(struct memblock_region) *
399 memblock.memory.max);
Miaohe Lin78706b02022-02-17 22:53:27 +0800400 if (memblock_memory_in_slab)
401 kfree(memblock.memory.regions);
402 else
403 __memblock_free_late(addr, size);
Pavel Tatashin3010f872017-08-18 15:16:05 -0700404 }
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -0700405
406 memblock_memory = NULL;
Yinghai Lu29f67382012-07-11 14:02:56 -0700407}
Philipp Hachtmann5e270e22014-01-23 15:53:11 -0800408#endif
409
Greg Pearson48c3b582012-06-20 12:53:05 -0700410/**
411 * memblock_double_array - double the size of the memblock regions array
412 * @type: memblock type of the regions array being doubled
413 * @new_area_start: starting address of memory range to avoid overlap with
414 * @new_area_size: size of memory range to avoid overlap with
415 *
416 * Double the size of the @type regions array. If memblock is being used to
417 * allocate memory for a new reserved regions array and there is a previously
Mike Rapoport47cec442018-06-30 17:55:02 +0300418 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
Greg Pearson48c3b582012-06-20 12:53:05 -0700419 * waiting to be reserved, ensure the memory used by the new array does
420 * not overlap.
421 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300422 * Return:
Greg Pearson48c3b582012-06-20 12:53:05 -0700423 * 0 on success, -1 on failure.
424 */
425static int __init_memblock memblock_double_array(struct memblock_type *type,
426 phys_addr_t new_area_start,
427 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700428{
429 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700430 phys_addr_t old_alloc_size, new_alloc_size;
Mike Rapoporta36aab82018-08-17 15:47:17 -0700431 phys_addr_t old_size, new_size, addr, new_end;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700432 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700433 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700434
435 /* We don't allow resizing until we know about the reserved regions
436 * of memory that aren't suitable for allocation
437 */
438 if (!memblock_can_resize)
439 return -1;
440
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700441 /* Calculate new doubled size */
442 old_size = type->max * sizeof(struct memblock_region);
443 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700444 /*
445 * We need to allocated new one align to PAGE_SIZE,
446 * so we can free them completely later.
447 */
448 old_alloc_size = PAGE_ALIGN(old_size);
449 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700450
Gavin Shan181eb392012-05-29 15:06:50 -0700451 /* Retrieve the slab flag */
452 if (type == &memblock.memory)
453 in_slab = &memblock_memory_in_slab;
454 else
455 in_slab = &memblock_reserved_in_slab;
456
Mike Rapoporta2974132019-03-11 23:30:54 -0700457 /* Try to find some space for it */
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700458 if (use_slab) {
459 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200460 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700461 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700462 /* only exclude range when trying to double reserved.regions */
463 if (type != &memblock.reserved)
464 new_area_start = new_area_size = 0;
465
466 addr = memblock_find_in_range(new_area_start + new_area_size,
467 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700468 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700469 if (!addr && new_area_size)
470 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700471 min(new_area_start, memblock.current_limit),
472 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700473
Sachin Kamat15674862012-09-04 13:55:05 +0530474 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700475 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200476 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700477 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800478 type->name, type->max, type->max * 2);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700479 return -1;
480 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700481
Mike Rapoporta36aab82018-08-17 15:47:17 -0700482 new_end = addr + new_size - 1;
483 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
484 type->name, type->max * 2, &addr, &new_end);
Yinghai Luea9e4372010-07-28 15:13:22 +1000485
Andrew Mortonfd073832012-07-31 16:42:40 -0700486 /*
487 * Found space, we now need to move the array over before we add the
488 * reserved region since it may be our reserved array itself that is
489 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700490 */
491 memcpy(new_array, type->regions, old_size);
492 memset(new_array + type->max, 0, old_size);
493 old_array = type->regions;
494 type->regions = new_array;
495 type->max <<= 1;
496
Andrew Mortonfd073832012-07-31 16:42:40 -0700497 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700498 if (*in_slab)
499 kfree(old_array);
500 else if (old_array != memblock_memory_init_regions &&
501 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700502 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700503
Andrew Mortonfd073832012-07-31 16:42:40 -0700504 /*
505 * Reserve the new array if that comes from the memblock. Otherwise, we
506 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700507 */
508 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700509 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700510
511 /* Update slab flag */
512 *in_slab = use_slab;
513
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700514 return 0;
515}
516
Tejun Heo784656f92011-07-12 11:15:55 +0200517/**
518 * memblock_merge_regions - merge neighboring compatible regions
519 * @type: memblock type to scan
520 *
521 * Scan @type and merge neighboring compatible regions.
522 */
523static void __init_memblock memblock_merge_regions(struct memblock_type *type)
524{
525 int i = 0;
526
527 /* cnt never goes below 1 */
528 while (i < type->cnt - 1) {
529 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--;
545 }
546}
547
548/**
549 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700550 * @type: memblock type to insert into
551 * @idx: index for the insertion point
552 * @base: base address of the new region
553 * @size: size of the new region
554 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800555 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200556 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300557 * Insert new memblock region [@base, @base + @size) into @type at @idx.
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700558 * @type must already have extra room to accommodate the new region.
Tejun Heo784656f92011-07-12 11:15:55 +0200559 */
560static void __init_memblock memblock_insert_region(struct memblock_type *type,
561 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800562 phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300563 int nid,
564 enum memblock_flags flags)
Tejun Heo784656f92011-07-12 11:15:55 +0200565{
566 struct memblock_region *rgn = &type->regions[idx];
567
568 BUG_ON(type->cnt >= type->max);
569 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
570 rgn->base = base;
571 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800572 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200573 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200574 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800575 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200576}
577
578/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100579 * memblock_add_range - add new memblock region
Tejun Heo784656f92011-07-12 11:15:55 +0200580 * @type: memblock type to add new region into
581 * @base: base address of the new region
582 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800583 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800584 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200585 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300586 * Add new memblock region [@base, @base + @size) into @type. The new region
Tejun Heo784656f92011-07-12 11:15:55 +0200587 * is allowed to overlap with existing ones - overlaps don't affect already
588 * existing regions. @type is guaranteed to be minimal (all neighbouring
589 * compatible regions are merged) after the addition.
590 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300591 * Return:
Tejun Heo784656f92011-07-12 11:15:55 +0200592 * 0 on success, -errno on failure.
593 */
Anshuman Khandual02634a42020-01-30 22:14:20 -0800594static int __init_memblock memblock_add_range(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800595 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300596 int nid, enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000597{
Tejun Heo784656f92011-07-12 11:15:55 +0200598 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800599 phys_addr_t obase = base;
600 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800601 int idx, nr_new;
602 struct memblock_region *rgn;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000603
Tejun Heob3dc6272012-04-20 08:31:34 -0700604 if (!size)
605 return 0;
606
Tejun Heo784656f92011-07-12 11:15:55 +0200607 /* special case for empty array */
608 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800609 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000610 type->regions[0].base = base;
611 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800612 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800613 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800614 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000615 return 0;
616 }
Tejun Heo784656f92011-07-12 11:15:55 +0200617repeat:
618 /*
619 * The following is executed twice. Once with %false @insert and
620 * then with %true. The first counts the number of regions needed
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700621 * to accommodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700622 */
Tejun Heo784656f92011-07-12 11:15:55 +0200623 base = obase;
624 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000625
Gioh Kim66e8b432017-11-15 17:33:42 -0800626 for_each_memblock_type(idx, type, rgn) {
Tejun Heo784656f92011-07-12 11:15:55 +0200627 phys_addr_t rbase = rgn->base;
628 phys_addr_t rend = rbase + rgn->size;
629
630 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000631 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200632 if (rend <= base)
633 continue;
634 /*
635 * @rgn overlaps. If it separates the lower part of new
636 * area, insert that portion.
637 */
638 if (rbase > base) {
Mike Rapoport3f08a302020-06-03 15:57:02 -0700639#ifdef CONFIG_NEED_MULTIPLE_NODES
Wei Yangc0a29492015-09-04 15:47:38 -0700640 WARN_ON(nid != memblock_get_region_node(rgn));
641#endif
Wei Yang4fcab5f2015-09-08 14:59:53 -0700642 WARN_ON(flags != rgn->flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200643 nr_new++;
644 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800645 memblock_insert_region(type, idx++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800646 rbase - base, nid,
647 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000648 }
Tejun Heo784656f92011-07-12 11:15:55 +0200649 /* area below @rend is dealt with, forget about it */
650 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000651 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000652
Tejun Heo784656f92011-07-12 11:15:55 +0200653 /* insert the remaining portion */
654 if (base < end) {
655 nr_new++;
656 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800657 memblock_insert_region(type, idx, base, end - base,
Tang Chen66a20752014-01-21 15:49:20 -0800658 nid, flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200659 }
660
nimisoloef3cc4d2016-07-26 15:24:56 -0700661 if (!nr_new)
662 return 0;
663
Tejun Heo784656f92011-07-12 11:15:55 +0200664 /*
665 * If this was the first round, resize array and repeat for actual
666 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700667 */
Tejun Heo784656f92011-07-12 11:15:55 +0200668 if (!insert) {
669 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700670 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200671 return -ENOMEM;
672 insert = true;
673 goto repeat;
674 } else {
675 memblock_merge_regions(type);
676 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700677 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000678}
679
Mike Rapoport48a833c2018-06-30 17:55:03 +0300680/**
681 * memblock_add_node - add new memblock region within a NUMA node
682 * @base: base address of the new region
683 * @size: size of the new region
684 * @nid: nid of the new region
685 *
686 * Add new memblock region [@base, @base + @size) to the "memory"
687 * type. See memblock_add_range() description for mode details
688 *
689 * Return:
690 * 0 on success, -errno on failure.
691 */
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800692int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
693 int nid)
694{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100695 return memblock_add_range(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800696}
697
Mike Rapoport48a833c2018-06-30 17:55:03 +0300698/**
699 * memblock_add - add new memblock region
700 * @base: base address of the new region
701 * @size: size of the new region
702 *
703 * Add new memblock region [@base, @base + @size) to the "memory"
704 * type. See memblock_add_range() description for mode details
705 *
706 * Return:
707 * 0 on success, -errno on failure.
708 */
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700709int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700710{
Miles Chen5d63f812017-02-22 15:46:42 -0800711 phys_addr_t end = base + size - 1;
712
Anshuman Khanduala090d712020-01-30 22:14:23 -0800713 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
Miles Chen5d63f812017-02-22 15:46:42 -0800714 &base, &end, (void *)_RET_IP_);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700715
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700716 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000717}
718
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800719/**
720 * memblock_isolate_range - isolate given range into disjoint memblocks
721 * @type: memblock type to isolate range for
722 * @base: base of range to isolate
723 * @size: size of range to isolate
724 * @start_rgn: out parameter for the start of isolated region
725 * @end_rgn: out parameter for the end of isolated region
726 *
727 * Walk @type and ensure that regions don't cross the boundaries defined by
Mike Rapoport47cec442018-06-30 17:55:02 +0300728 * [@base, @base + @size). Crossing regions are split at the boundaries,
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800729 * which may create at most two more regions. The index of the first
730 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
731 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300732 * Return:
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800733 * 0 on success, -errno on failure.
734 */
735static int __init_memblock memblock_isolate_range(struct memblock_type *type,
736 phys_addr_t base, phys_addr_t size,
737 int *start_rgn, int *end_rgn)
738{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800739 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800740 int idx;
741 struct memblock_region *rgn;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800742
743 *start_rgn = *end_rgn = 0;
744
Tejun Heob3dc6272012-04-20 08:31:34 -0700745 if (!size)
746 return 0;
747
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800748 /* we'll create at most two more regions */
749 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700750 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800751 return -ENOMEM;
752
Gioh Kim66e8b432017-11-15 17:33:42 -0800753 for_each_memblock_type(idx, type, rgn) {
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800754 phys_addr_t rbase = rgn->base;
755 phys_addr_t rend = rbase + rgn->size;
756
757 if (rbase >= end)
758 break;
759 if (rend <= base)
760 continue;
761
762 if (rbase < base) {
763 /*
764 * @rgn intersects from below. Split and continue
765 * to process the next region - the new top half.
766 */
767 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800768 rgn->size -= base - rbase;
769 type->total_size -= base - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800770 memblock_insert_region(type, idx, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800771 memblock_get_region_node(rgn),
772 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800773 } else if (rend > end) {
774 /*
775 * @rgn intersects from above. Split and redo the
776 * current region - the new bottom half.
777 */
778 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800779 rgn->size -= end - rbase;
780 type->total_size -= end - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800781 memblock_insert_region(type, idx--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800782 memblock_get_region_node(rgn),
783 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800784 } else {
785 /* @rgn is fully contained, record it */
786 if (!*end_rgn)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800787 *start_rgn = idx;
788 *end_rgn = idx + 1;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800789 }
790 }
791
792 return 0;
793}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800794
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800795static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100796 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000797{
Tejun Heo71936182011-12-08 10:22:07 -0800798 int start_rgn, end_rgn;
799 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000800
Tejun Heo71936182011-12-08 10:22:07 -0800801 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
802 if (ret)
803 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000804
Tejun Heo71936182011-12-08 10:22:07 -0800805 for (i = end_rgn - 1; i >= start_rgn; i--)
806 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700807 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000808}
809
Tejun Heo581adcb2011-12-08 10:22:06 -0800810int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000811{
Minchan Kim25cf23d2018-06-07 17:07:35 -0700812 phys_addr_t end = base + size - 1;
813
Anshuman Khanduala090d712020-01-30 22:14:23 -0800814 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
Minchan Kim25cf23d2018-06-07 17:07:35 -0700815 &base, &end, (void *)_RET_IP_);
816
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100817 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000818}
819
Mike Rapoport4d728682018-12-28 00:35:29 -0800820/**
821 * memblock_free - free boot memory block
822 * @base: phys starting address of the boot memory block
823 * @size: size of the boot memory block in bytes
824 *
825 * Free boot memory block previously allocated by memblock_alloc_xx() API.
826 * The freeing memory will not be released to the buddy allocator.
827 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800828int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000829{
Miles Chen5d63f812017-02-22 15:46:42 -0800830 phys_addr_t end = base + size - 1;
831
Anshuman Khanduala090d712020-01-30 22:14:23 -0800832 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
Miles Chen5d63f812017-02-22 15:46:42 -0800833 &base, &end, (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200834
Catalin Marinas9099dae2016-10-11 13:55:11 -0700835 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100836 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000837}
Alistair Delva2eeee9f42020-06-04 09:29:56 -0700838#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
839EXPORT_SYMBOL_GPL(memblock_free);
840#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +1000841
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700842int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000843{
Miles Chen5d63f812017-02-22 15:46:42 -0800844 phys_addr_t end = base + size - 1;
845
Anshuman Khanduala090d712020-01-30 22:14:23 -0800846 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
Miles Chen5d63f812017-02-22 15:46:42 -0800847 &base, &end, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000848
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700849 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000850}
851
Anshuman Khandual02634a42020-01-30 22:14:20 -0800852#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
853int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
854{
855 phys_addr_t end = base + size - 1;
856
857 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
858 &base, &end, (void *)_RET_IP_);
859
David Hildenbrand77649902020-07-01 16:18:29 +0200860 return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0);
Anshuman Khandual02634a42020-01-30 22:14:20 -0800861}
862#endif
863
Tejun Heo35fd0802011-07-12 11:15:59 +0200864/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300865 * memblock_setclr_flag - set or clear flag for a memory region
866 * @base: base address of the region
867 * @size: size of the region
868 * @set: set or clear the flag
869 * @flag: the flag to udpate
Tang Chen66b16ed2014-01-21 15:49:23 -0800870 *
Tony Luck4308ce12014-12-12 16:54:59 -0800871 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800872 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300873 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800874 */
Tony Luck4308ce12014-12-12 16:54:59 -0800875static int __init_memblock memblock_setclr_flag(phys_addr_t base,
876 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800877{
878 struct memblock_type *type = &memblock.memory;
879 int i, ret, start_rgn, end_rgn;
880
881 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
882 if (ret)
883 return ret;
884
Mike Rapoportfe145122019-03-11 23:30:46 -0700885 for (i = start_rgn; i < end_rgn; i++) {
886 struct memblock_region *r = &type->regions[i];
887
Tony Luck4308ce12014-12-12 16:54:59 -0800888 if (set)
Mike Rapoportfe145122019-03-11 23:30:46 -0700889 r->flags |= flag;
Tony Luck4308ce12014-12-12 16:54:59 -0800890 else
Mike Rapoportfe145122019-03-11 23:30:46 -0700891 r->flags &= ~flag;
892 }
Tang Chen66b16ed2014-01-21 15:49:23 -0800893
894 memblock_merge_regions(type);
895 return 0;
896}
897
898/**
Tony Luck4308ce12014-12-12 16:54:59 -0800899 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
900 * @base: the base phys addr of the region
901 * @size: the size of the region
902 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300903 * Return: 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800904 */
905int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
906{
907 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
908}
909
910/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800911 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
912 * @base: the base phys addr of the region
913 * @size: the size of the region
914 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300915 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800916 */
917int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
918{
Tony Luck4308ce12014-12-12 16:54:59 -0800919 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800920}
921
922/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700923 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
924 * @base: the base phys addr of the region
925 * @size: the size of the region
926 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300927 * Return: 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700928 */
929int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
930{
931 system_has_some_mirror = true;
932
933 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
934}
935
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100936/**
937 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
938 * @base: the base phys addr of the region
939 * @size: the size of the region
940 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300941 * Return: 0 on success, -errno on failure.
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100942 */
943int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
944{
945 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
946}
Tony Lucka3f5baf2015-06-24 16:58:12 -0700947
948/**
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900949 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
950 * @base: the base phys addr of the region
951 * @size: the size of the region
952 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300953 * Return: 0 on success, -errno on failure.
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900954 */
955int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
956{
957 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
958}
959
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -0700960static bool should_skip_region(struct memblock_type *type,
961 struct memblock_region *m,
962 int nid, int flags)
Mike Rapoportc9a688a2019-03-11 23:30:50 -0700963{
964 int m_nid = memblock_get_region_node(m);
965
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -0700966 /* we never skip regions when iterating memblock.reserved or physmem */
967 if (type != memblock_memory)
968 return false;
969
Mike Rapoportc9a688a2019-03-11 23:30:50 -0700970 /* only memory regions are associated with nodes, check it */
971 if (nid != NUMA_NO_NODE && nid != m_nid)
972 return true;
973
974 /* skip hotpluggable memory regions if needed */
Mike Rapoport1a25c572021-07-23 15:50:26 -0700975 if (movable_node_is_enabled() && memblock_is_hotpluggable(m) &&
976 !(flags & MEMBLOCK_HOTPLUG))
Mike Rapoportc9a688a2019-03-11 23:30:50 -0700977 return true;
978
979 /* if we want mirror memory skip non-mirror memory regions */
980 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
981 return true;
982
983 /* skip nomap memory unless we were asked for it explicitly */
984 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
985 return true;
986
987 return false;
988}
989
Robin Holt8e7a7f82015-06-30 14:56:41 -0700990/**
Mike Rapoporta2974132019-03-11 23:30:54 -0700991 * __next_mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +0200992 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800993 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700994 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100995 * @type_a: pointer to memblock_type from where the range is taken
996 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700997 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
998 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
999 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +02001000 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001001 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +02001002 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001003 * *@idx contains index into type_a and the upper 32bit indexes the
1004 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +02001005 * look like the following,
1006 *
1007 * 0:[0-16), 1:[32-48), 2:[128-130)
1008 *
1009 * The upper 32bit indexes the following regions.
1010 *
1011 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
1012 *
1013 * As both region arrays are sorted, the function advances the two indices
1014 * in lockstep and returns each intersection.
1015 */
David Hildenbrand77649902020-07-01 16:18:29 +02001016void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
1017 struct memblock_type *type_a,
1018 struct memblock_type *type_b, phys_addr_t *out_start,
1019 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +02001020{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001021 int idx_a = *idx & 0xffffffff;
1022 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001023
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001024 if (WARN_ONCE(nid == MAX_NUMNODES,
1025 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
Grygorii Strashko560dca272014-01-21 15:50:55 -08001026 nid = NUMA_NO_NODE;
Tejun Heo35fd0802011-07-12 11:15:59 +02001027
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001028 for (; idx_a < type_a->cnt; idx_a++) {
1029 struct memblock_region *m = &type_a->regions[idx_a];
1030
Tejun Heo35fd0802011-07-12 11:15:59 +02001031 phys_addr_t m_start = m->base;
1032 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001033 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +02001034
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -07001035 if (should_skip_region(type_a, m, nid, flags))
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001036 continue;
1037
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001038 if (!type_b) {
1039 if (out_start)
1040 *out_start = m_start;
1041 if (out_end)
1042 *out_end = m_end;
1043 if (out_nid)
1044 *out_nid = m_nid;
1045 idx_a++;
1046 *idx = (u32)idx_a | (u64)idx_b << 32;
1047 return;
1048 }
Tejun Heo35fd0802011-07-12 11:15:59 +02001049
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001050 /* scan areas before each reservation */
1051 for (; idx_b < type_b->cnt + 1; idx_b++) {
1052 struct memblock_region *r;
1053 phys_addr_t r_start;
1054 phys_addr_t r_end;
1055
1056 r = &type_b->regions[idx_b];
1057 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1058 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001059 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001060
1061 /*
1062 * if idx_b advanced past idx_a,
1063 * break out to advance idx_a
1064 */
Tejun Heo35fd0802011-07-12 11:15:59 +02001065 if (r_start >= m_end)
1066 break;
1067 /* if the two regions intersect, we're done */
1068 if (m_start < r_end) {
1069 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001070 *out_start =
1071 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +02001072 if (out_end)
1073 *out_end = min(m_end, r_end);
1074 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001075 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +02001076 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001077 * The region which ends first is
1078 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +02001079 */
1080 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001081 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +02001082 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001083 idx_b++;
1084 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +02001085 return;
1086 }
1087 }
1088 }
1089
1090 /* signal end of iteration */
1091 *idx = ULLONG_MAX;
1092}
1093
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001094/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001095 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1096 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001097 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -07001098 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -07001099 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001100 * @type_a: pointer to memblock_type from where the range is taken
1101 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -07001102 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1103 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1104 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001105 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001106 * Finds the next range from type_a which is not marked as unsuitable
1107 * in type_b.
1108 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001109 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001110 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001111void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1112 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001113 struct memblock_type *type_a,
1114 struct memblock_type *type_b,
1115 phys_addr_t *out_start,
1116 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001117{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001118 int idx_a = *idx & 0xffffffff;
1119 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001120
Grygorii Strashko560dca272014-01-21 15:50:55 -08001121 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1122 nid = NUMA_NO_NODE;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001123
1124 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001125 idx_a = type_a->cnt - 1;
zijun_hue47608a2016-08-04 15:32:00 -07001126 if (type_b != NULL)
1127 idx_b = type_b->cnt;
1128 else
1129 idx_b = 0;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001130 }
1131
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001132 for (; idx_a >= 0; idx_a--) {
1133 struct memblock_region *m = &type_a->regions[idx_a];
1134
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001135 phys_addr_t m_start = m->base;
1136 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001137 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001138
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -07001139 if (should_skip_region(type_a, m, nid, flags))
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001140 continue;
1141
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001142 if (!type_b) {
1143 if (out_start)
1144 *out_start = m_start;
1145 if (out_end)
1146 *out_end = m_end;
1147 if (out_nid)
1148 *out_nid = m_nid;
zijun_hufb399b42016-07-28 15:48:56 -07001149 idx_a--;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001150 *idx = (u32)idx_a | (u64)idx_b << 32;
1151 return;
1152 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001153
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001154 /* scan areas before each reservation */
1155 for (; idx_b >= 0; idx_b--) {
1156 struct memblock_region *r;
1157 phys_addr_t r_start;
1158 phys_addr_t r_end;
1159
1160 r = &type_b->regions[idx_b];
1161 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1162 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001163 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001164 /*
1165 * if idx_b advanced past idx_a,
1166 * break out to advance idx_a
1167 */
1168
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001169 if (r_end <= m_start)
1170 break;
1171 /* if the two regions intersect, we're done */
1172 if (m_end > r_start) {
1173 if (out_start)
1174 *out_start = max(m_start, r_start);
1175 if (out_end)
1176 *out_end = min(m_end, r_end);
1177 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001178 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001179 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001180 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001181 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001182 idx_b--;
1183 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001184 return;
1185 }
1186 }
1187 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001188 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001189 *idx = ULLONG_MAX;
1190}
1191
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001192/*
Chen Chang45e79812018-11-16 15:08:57 -08001193 * Common iterator interface used to define for_each_mem_pfn_range().
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001194 */
1195void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1196 unsigned long *out_start_pfn,
1197 unsigned long *out_end_pfn, int *out_nid)
1198{
1199 struct memblock_type *type = &memblock.memory;
1200 struct memblock_region *r;
Mike Rapoportd622abf2020-06-03 15:56:53 -07001201 int r_nid;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001202
1203 while (++*idx < type->cnt) {
1204 r = &type->regions[*idx];
Mike Rapoportd622abf2020-06-03 15:56:53 -07001205 r_nid = memblock_get_region_node(r);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001206
1207 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1208 continue;
Mike Rapoportd622abf2020-06-03 15:56:53 -07001209 if (nid == MAX_NUMNODES || nid == r_nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001210 break;
1211 }
1212 if (*idx >= type->cnt) {
1213 *idx = -1;
1214 return;
1215 }
1216
1217 if (out_start_pfn)
1218 *out_start_pfn = PFN_UP(r->base);
1219 if (out_end_pfn)
1220 *out_end_pfn = PFN_DOWN(r->base + r->size);
1221 if (out_nid)
Mike Rapoportd622abf2020-06-03 15:56:53 -07001222 *out_nid = r_nid;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001223}
1224
1225/**
1226 * memblock_set_node - set node ID on memblock regions
1227 * @base: base of area to set node ID for
1228 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001229 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001230 * @nid: node ID to set
1231 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001232 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001233 * Regions which cross the area boundaries are split as necessary.
1234 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001235 * Return:
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001236 * 0 on success, -errno on failure.
1237 */
1238int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001239 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001240{
Mike Rapoport3f08a302020-06-03 15:57:02 -07001241#ifdef CONFIG_NEED_MULTIPLE_NODES
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001242 int start_rgn, end_rgn;
1243 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001244
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001245 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1246 if (ret)
1247 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001248
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001249 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001250 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001251
1252 memblock_merge_regions(type);
Mike Rapoport3f08a302020-06-03 15:57:02 -07001253#endif
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001254 return 0;
1255}
Mike Rapoport3f08a302020-06-03 15:57:02 -07001256
Alexander Duyck837566e2019-05-13 17:21:17 -07001257#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1258/**
1259 * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1260 *
1261 * @idx: pointer to u64 loop variable
1262 * @zone: zone in which all of the memory blocks reside
1263 * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1264 * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1265 *
1266 * This function is meant to be a zone/pfn specific wrapper for the
1267 * for_each_mem_range type iterators. Specifically they are used in the
1268 * deferred memory init routines and as such we were duplicating much of
1269 * this logic throughout the code. So instead of having it in multiple
1270 * locations it seemed like it would make more sense to centralize this to
1271 * one new iterator that does everything they need.
1272 */
1273void __init_memblock
1274__next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1275 unsigned long *out_spfn, unsigned long *out_epfn)
1276{
1277 int zone_nid = zone_to_nid(zone);
1278 phys_addr_t spa, epa;
1279 int nid;
1280
1281 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1282 &memblock.memory, &memblock.reserved,
1283 &spa, &epa, &nid);
1284
1285 while (*idx != U64_MAX) {
1286 unsigned long epfn = PFN_DOWN(epa);
1287 unsigned long spfn = PFN_UP(spa);
1288
1289 /*
1290 * Verify the end is at least past the start of the zone and
1291 * that we have at least one PFN to initialize.
1292 */
1293 if (zone->zone_start_pfn < epfn && spfn < epfn) {
1294 /* if we went too far just stop searching */
1295 if (zone_end_pfn(zone) <= spfn) {
1296 *idx = U64_MAX;
1297 break;
1298 }
1299
1300 if (out_spfn)
1301 *out_spfn = max(zone->zone_start_pfn, spfn);
1302 if (out_epfn)
1303 *out_epfn = min(zone_end_pfn(zone), epfn);
1304
1305 return;
1306 }
1307
1308 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1309 &memblock.memory, &memblock.reserved,
1310 &spa, &epa, &nid);
1311 }
1312
1313 /* signal end of iteration */
1314 if (out_spfn)
1315 *out_spfn = ULONG_MAX;
1316 if (out_epfn)
1317 *out_epfn = 0;
1318}
1319
1320#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001321
Mike Rapoport92d12f92019-03-11 23:29:41 -07001322/**
1323 * memblock_alloc_range_nid - allocate boot memory block
1324 * @size: size of memory block to be allocated in bytes
1325 * @align: alignment of the region and block's size
1326 * @start: the lower bound of the memory region to allocate (phys address)
1327 * @end: the upper bound of the memory region to allocate (phys address)
1328 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001329 * @exact_nid: control the allocation fall back to other nodes
Mike Rapoport92d12f92019-03-11 23:29:41 -07001330 *
1331 * The allocation is performed from memory region limited by
Cao jin95830662019-11-30 17:56:24 -08001332 * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
Mike Rapoport92d12f92019-03-11 23:29:41 -07001333 *
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001334 * If the specified node can not hold the requested memory and @exact_nid
1335 * is false, the allocation falls back to any node in the system.
Mike Rapoport92d12f92019-03-11 23:29:41 -07001336 *
1337 * For systems with memory mirroring, the allocation is attempted first
1338 * from the regions with mirroring enabled and then retried from any
1339 * memory region.
1340 *
1341 * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
1342 * allocated boot memory block, so that it is never reported as leaks.
1343 *
1344 * Return:
1345 * Physical address of allocated memory block on success, %0 on failure.
1346 */
Aslan Bakirov8676af12020-04-10 14:32:42 -07001347phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001348 phys_addr_t align, phys_addr_t start,
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001349 phys_addr_t end, int nid,
1350 bool exact_nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001351{
Mike Rapoport92d12f92019-03-11 23:29:41 -07001352 enum memblock_flags flags = choose_memblock_flags();
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001353 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001354
Mike Rapoport92d12f92019-03-11 23:29:41 -07001355 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1356 nid = NUMA_NO_NODE;
1357
Mike Rapoport2f770802018-10-30 15:10:01 -07001358 if (!align) {
1359 /* Can't use WARNs this early in boot on powerpc */
1360 dump_stack();
1361 align = SMP_CACHE_BYTES;
1362 }
1363
Mike Rapoport92d12f92019-03-11 23:29:41 -07001364again:
Tony Luckfc6daaf2015-06-24 16:58:09 -07001365 found = memblock_find_in_range_node(size, align, start, end, nid,
1366 flags);
Mike Rapoport92d12f92019-03-11 23:29:41 -07001367 if (found && !memblock_reserve(found, size))
1368 goto done;
1369
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001370 if (nid != NUMA_NO_NODE && !exact_nid) {
Mike Rapoport92d12f92019-03-11 23:29:41 -07001371 found = memblock_find_in_range_node(size, align, start,
1372 end, NUMA_NO_NODE,
1373 flags);
1374 if (found && !memblock_reserve(found, size))
1375 goto done;
1376 }
1377
1378 if (flags & MEMBLOCK_MIRROR) {
1379 flags &= ~MEMBLOCK_MIRROR;
1380 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1381 &size);
1382 goto again;
1383 }
1384
1385 return 0;
1386
1387done:
1388 /* Skip kmemleak for kasan_init() due to high volume. */
1389 if (end != MEMBLOCK_ALLOC_KASAN)
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001390 /*
Mike Rapoport92d12f92019-03-11 23:29:41 -07001391 * The min_count is set to 0 so that memblock allocated
1392 * blocks are never reported as leaks. This is because many
1393 * of these blocks are only referred via the physical
1394 * address which is not looked up by kmemleak.
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001395 */
Catalin Marinas9099dae2016-10-11 13:55:11 -07001396 kmemleak_alloc_phys(found, size, 0, 0);
Mike Rapoport92d12f92019-03-11 23:29:41 -07001397
1398 return found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001399}
1400
Mike Rapoporta2974132019-03-11 23:30:54 -07001401/**
1402 * memblock_phys_alloc_range - allocate a memory block inside specified range
1403 * @size: size of memory block to be allocated in bytes
1404 * @align: alignment of the region and block's size
1405 * @start: the lower bound of the memory region to allocate (physical address)
1406 * @end: the upper bound of the memory region to allocate (physical address)
1407 *
1408 * Allocate @size bytes in the between @start and @end.
1409 *
1410 * Return: physical address of the allocated memory block on success,
1411 * %0 on failure.
1412 */
Mike Rapoport8a770c22019-03-11 23:29:16 -07001413phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
1414 phys_addr_t align,
1415 phys_addr_t start,
1416 phys_addr_t end)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001417{
Faiyaz Mohammedf2f8d732020-11-16 10:14:04 +05301418 memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n",
1419 __func__, (u64)size, (u64)align, &start, &end,
1420 (void *)_RET_IP_);
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001421 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1422 false);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001423}
1424
Mike Rapoporta2974132019-03-11 23:30:54 -07001425/**
1426 * memblock_phys_alloc_try_nid - allocate a memory block from specified MUMA node
1427 * @size: size of memory block to be allocated in bytes
1428 * @align: alignment of the region and block's size
1429 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1430 *
1431 * Allocates memory block from the specified NUMA node. If the node
1432 * has no available memory, attempts to allocated from any node in the
1433 * system.
1434 *
1435 * Return: physical address of the allocated memory block on success,
1436 * %0 on failure.
1437 */
Mike Rapoport9a8dd702018-10-30 15:07:59 -07001438phys_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 -07001439{
Mike Rapoport33755572019-03-11 23:29:21 -07001440 return memblock_alloc_range_nid(size, align, 0,
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001441 MEMBLOCK_ALLOC_ACCESSIBLE, nid, false);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001442}
1443
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001444/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001445 * memblock_alloc_internal - allocate boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001446 * @size: size of memory block to be allocated in bytes
1447 * @align: alignment of the region and block's size
1448 * @min_addr: the lower bound of the memory region to allocate (phys address)
1449 * @max_addr: the upper bound of the memory region to allocate (phys address)
1450 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001451 * @exact_nid: control the allocation fall back to other nodes
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001452 *
Mike Rapoport92d12f92019-03-11 23:29:41 -07001453 * Allocates memory block using memblock_alloc_range_nid() and
1454 * converts the returned physical address to virtual.
1455 *
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001456 * The @min_addr limit is dropped if it can not be satisfied and the allocation
Mike Rapoport92d12f92019-03-11 23:29:41 -07001457 * will fall back to memory below @min_addr. Other constraints, such
1458 * as node and mirrored memory will be handled again in
1459 * memblock_alloc_range_nid().
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001460 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001461 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001462 * Virtual address of allocated memory block on success, NULL on failure.
1463 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001464static void * __init memblock_alloc_internal(
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001465 phys_addr_t size, phys_addr_t align,
1466 phys_addr_t min_addr, phys_addr_t max_addr,
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001467 int nid, bool exact_nid)
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001468{
1469 phys_addr_t alloc;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001470
1471 /*
1472 * Detect any accidental use of these APIs after slab is ready, as at
1473 * this moment memblock may be deinitialized already and its
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -07001474 * internal data may be destroyed (after execution of memblock_free_all)
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001475 */
1476 if (WARN_ON_ONCE(slab_is_available()))
1477 return kzalloc_node(size, GFP_NOWAIT, nid);
1478
Mike Rapoportf3057ad2019-10-18 20:20:01 -07001479 if (max_addr > memblock.current_limit)
1480 max_addr = memblock.current_limit;
1481
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001482 alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid,
1483 exact_nid);
Mike Rapoport2f770802018-10-30 15:10:01 -07001484
Mike Rapoport92d12f92019-03-11 23:29:41 -07001485 /* retry allocation without lower limit */
1486 if (!alloc && min_addr)
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001487 alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid,
1488 exact_nid);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001489
Mike Rapoport92d12f92019-03-11 23:29:41 -07001490 if (!alloc)
1491 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001492
Mike Rapoport92d12f92019-03-11 23:29:41 -07001493 return phys_to_virt(alloc);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001494}
1495
1496/**
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001497 * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
1498 * without zeroing memory
1499 * @size: size of memory block to be allocated in bytes
1500 * @align: alignment of the region and block's size
1501 * @min_addr: the lower bound of the memory region from where the allocation
1502 * is preferred (phys address)
1503 * @max_addr: the upper bound of the memory region from where the allocation
1504 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1505 * allocate only from memory limited by memblock.current_limit value
1506 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1507 *
1508 * Public function, provides additional debug information (including caller
1509 * info), if enabled. Does not zero allocated memory.
1510 *
1511 * Return:
1512 * Virtual address of allocated memory block on success, NULL on failure.
1513 */
1514void * __init memblock_alloc_exact_nid_raw(
1515 phys_addr_t size, phys_addr_t align,
1516 phys_addr_t min_addr, phys_addr_t max_addr,
1517 int nid)
1518{
1519 void *ptr;
1520
1521 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1522 __func__, (u64)size, (u64)align, nid, &min_addr,
1523 &max_addr, (void *)_RET_IP_);
1524
1525 ptr = memblock_alloc_internal(size, align,
1526 min_addr, max_addr, nid, true);
1527 if (ptr && size > 0)
1528 page_init_poison(ptr, size);
1529
1530 return ptr;
1531}
1532
1533/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001534 * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001535 * memory and without panicking
1536 * @size: size of memory block to be allocated in bytes
1537 * @align: alignment of the region and block's size
1538 * @min_addr: the lower bound of the memory region from where the allocation
1539 * is preferred (phys address)
1540 * @max_addr: the upper bound of the memory region from where the allocation
Mike Rapoport97ad1082018-10-30 15:09:44 -07001541 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001542 * allocate only from memory limited by memblock.current_limit value
1543 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1544 *
1545 * Public function, provides additional debug information (including caller
1546 * info), if enabled. Does not zero allocated memory, does not panic if request
1547 * cannot be satisfied.
1548 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001549 * Return:
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001550 * Virtual address of allocated memory block on success, NULL on failure.
1551 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001552void * __init memblock_alloc_try_nid_raw(
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001553 phys_addr_t size, phys_addr_t align,
1554 phys_addr_t min_addr, phys_addr_t max_addr,
1555 int nid)
1556{
1557 void *ptr;
1558
Sakari Ailusd75f7732019-03-25 21:32:28 +02001559 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
Mike Rapoporta36aab82018-08-17 15:47:17 -07001560 __func__, (u64)size, (u64)align, nid, &min_addr,
1561 &max_addr, (void *)_RET_IP_);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001562
Mike Rapoporteb31d552018-10-30 15:08:04 -07001563 ptr = memblock_alloc_internal(size, align,
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001564 min_addr, max_addr, nid, false);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001565 if (ptr && size > 0)
Alexander Duyckf682a972018-10-26 15:07:45 -07001566 page_init_poison(ptr, size);
1567
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001568 return ptr;
1569}
1570
1571/**
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001572 * memblock_alloc_try_nid - allocate boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001573 * @size: size of memory block to be allocated in bytes
1574 * @align: alignment of the region and block's size
1575 * @min_addr: the lower bound of the memory region from where the allocation
1576 * is preferred (phys address)
1577 * @max_addr: the upper bound of the memory region from where the allocation
Mike Rapoport97ad1082018-10-30 15:09:44 -07001578 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001579 * allocate only from memory limited by memblock.current_limit value
1580 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1581 *
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001582 * Public function, provides additional debug information (including caller
1583 * info), if enabled. This function zeroes the allocated memory.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001584 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001585 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001586 * Virtual address of allocated memory block on success, NULL on failure.
1587 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001588void * __init memblock_alloc_try_nid(
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001589 phys_addr_t size, phys_addr_t align,
1590 phys_addr_t min_addr, phys_addr_t max_addr,
1591 int nid)
1592{
1593 void *ptr;
1594
Sakari Ailusd75f7732019-03-25 21:32:28 +02001595 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
Mike Rapoporta36aab82018-08-17 15:47:17 -07001596 __func__, (u64)size, (u64)align, nid, &min_addr,
1597 &max_addr, (void *)_RET_IP_);
Mike Rapoporteb31d552018-10-30 15:08:04 -07001598 ptr = memblock_alloc_internal(size, align,
Yunfeng Ye0ac398b2019-11-30 17:56:27 -08001599 min_addr, max_addr, nid, false);
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001600 if (ptr)
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001601 memset(ptr, 0, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001602
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001603 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001604}
1605
1606/**
Mike Rapoporta2974132019-03-11 23:30:54 -07001607 * __memblock_free_late - free pages directly to buddy allocator
Mike Rapoport48a833c2018-06-30 17:55:03 +03001608 * @base: phys starting address of the boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001609 * @size: size of the boot memory block in bytes
1610 *
Mike Rapoporta2974132019-03-11 23:30:54 -07001611 * This is only useful when the memblock allocator has already been torn
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001612 * down, but we are still initializing the system. Pages are released directly
Mike Rapoporta2974132019-03-11 23:30:54 -07001613 * to the buddy allocator.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001614 */
1615void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1616{
Mike Rapoporta36aab82018-08-17 15:47:17 -07001617 phys_addr_t cursor, end;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001618
Mike Rapoporta36aab82018-08-17 15:47:17 -07001619 end = base + size - 1;
Sakari Ailusd75f7732019-03-25 21:32:28 +02001620 memblock_dbg("%s: [%pa-%pa] %pS\n",
Mike Rapoporta36aab82018-08-17 15:47:17 -07001621 __func__, &base, &end, (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001622 kmemleak_free_part_phys(base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001623 cursor = PFN_UP(base);
1624 end = PFN_DOWN(base + size);
1625
1626 for (; cursor < end; cursor++) {
Mike Rapoport7c2ee342018-10-30 15:09:36 -07001627 memblock_free_pages(pfn_to_page(cursor), cursor, 0);
Arun KSca79b0c2018-12-28 00:34:29 -08001628 totalram_pages_inc();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001629 }
1630}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001631
1632/*
1633 * Remaining API functions
1634 */
1635
David Gibson1f1ffb8a2016-02-05 15:36:19 -08001636phys_addr_t __init_memblock memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001637{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001638 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001639}
1640
Srikar Dronamraju8907de52016-10-07 16:59:18 -07001641phys_addr_t __init_memblock memblock_reserved_size(void)
1642{
1643 return memblock.reserved.total_size;
1644}
1645
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001646/* lowest address */
1647phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1648{
1649 return memblock.memory.regions[0].base;
1650}
1651
Yinghai Lu10d06432010-07-28 15:43:02 +10001652phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001653{
1654 int idx = memblock.memory.cnt - 1;
1655
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001656 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001657}
Sudarshan Rajagopalan6be666e2020-10-28 14:31:09 -07001658EXPORT_SYMBOL_GPL(memblock_end_of_DRAM);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001659
Dennis Chena571d4e2016-07-28 15:48:26 -07001660static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001661{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001662 phys_addr_t max_addr = PHYS_ADDR_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001663 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001664
Dennis Chena571d4e2016-07-28 15:48:26 -07001665 /*
1666 * translate the memory @limit size into the max address within one of
1667 * the memory memblock regions, if the @limit exceeds the total size
Stefan Agner1c4bc432018-06-07 17:06:15 -07001668 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
Dennis Chena571d4e2016-07-28 15:48:26 -07001669 */
Mike Rapoportcc6de162020-10-13 16:58:30 -07001670 for_each_mem_region(r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001671 if (limit <= r->size) {
1672 max_addr = r->base + limit;
1673 break;
1674 }
1675 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001676 }
1677
Dennis Chena571d4e2016-07-28 15:48:26 -07001678 return max_addr;
1679}
1680
1681void __init memblock_enforce_memory_limit(phys_addr_t limit)
1682{
Colin Ian King49aef712020-04-01 21:11:01 -07001683 phys_addr_t max_addr;
Dennis Chena571d4e2016-07-28 15:48:26 -07001684
1685 if (!limit)
1686 return;
1687
1688 max_addr = __find_max_addr(limit);
1689
1690 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001691 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001692 return;
1693
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001694 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001695 memblock_remove_range(&memblock.memory, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001696 PHYS_ADDR_MAX);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001697 memblock_remove_range(&memblock.reserved, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001698 PHYS_ADDR_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001699}
1700
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001701void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1702{
1703 int start_rgn, end_rgn;
1704 int i, ret;
1705
1706 if (!size)
1707 return;
1708
1709 ret = memblock_isolate_range(&memblock.memory, base, size,
1710 &start_rgn, &end_rgn);
1711 if (ret)
1712 return;
1713
1714 /* remove all the MAP regions */
1715 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1716 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1717 memblock_remove_region(&memblock.memory, i);
1718
1719 for (i = start_rgn - 1; i >= 0; i--)
1720 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1721 memblock_remove_region(&memblock.memory, i);
1722
1723 /* truncate the reserved regions */
1724 memblock_remove_range(&memblock.reserved, 0, base);
1725 memblock_remove_range(&memblock.reserved,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001726 base + size, PHYS_ADDR_MAX);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001727}
1728
Dennis Chena571d4e2016-07-28 15:48:26 -07001729void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1730{
Dennis Chena571d4e2016-07-28 15:48:26 -07001731 phys_addr_t max_addr;
Dennis Chena571d4e2016-07-28 15:48:26 -07001732
1733 if (!limit)
1734 return;
1735
1736 max_addr = __find_max_addr(limit);
1737
1738 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001739 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001740 return;
1741
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001742 memblock_cap_memory_range(0, max_addr);
Dennis Chena571d4e2016-07-28 15:48:26 -07001743}
1744
Yinghai Lucd794812010-10-11 12:34:09 -07001745static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001746{
1747 unsigned int left = 0, right = type->cnt;
1748
1749 do {
1750 unsigned int mid = (right + left) / 2;
1751
1752 if (addr < type->regions[mid].base)
1753 right = mid;
1754 else if (addr >= (type->regions[mid].base +
1755 type->regions[mid].size))
1756 left = mid + 1;
1757 else
1758 return mid;
1759 } while (left < right);
1760 return -1;
1761}
1762
Yueyi Lif5a222d2018-12-14 14:17:06 -08001763bool __init_memblock memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001764{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001765 return memblock_search(&memblock.reserved, addr) != -1;
1766}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001767
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001768bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001769{
1770 return memblock_search(&memblock.memory, addr) != -1;
1771}
1772
Yaowei Bai937f0c22018-02-06 15:41:18 -08001773bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001774{
1775 int i = memblock_search(&memblock.memory, addr);
1776
1777 if (i == -1)
1778 return false;
1779 return !memblock_is_nomap(&memblock.memory.regions[i]);
1780}
1781
Yinghai Lue76b63f2013-09-11 14:22:17 -07001782int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1783 unsigned long *start_pfn, unsigned long *end_pfn)
1784{
1785 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001786 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001787
1788 if (mid == -1)
1789 return -1;
1790
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001791 *start_pfn = PFN_DOWN(type->regions[mid].base);
1792 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001793
Mike Rapoportd622abf2020-06-03 15:56:53 -07001794 return memblock_get_region_node(&type->regions[mid]);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001795}
Yinghai Lue76b63f2013-09-11 14:22:17 -07001796
Stephen Boydeab30942012-05-24 00:45:21 -07001797/**
1798 * memblock_is_region_memory - check if a region is a subset of memory
1799 * @base: base of region to check
1800 * @size: size of region to check
1801 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001802 * Check if the region [@base, @base + @size) is a subset of a memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001803 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001804 * Return:
Stephen Boydeab30942012-05-24 00:45:21 -07001805 * 0 if false, non-zero if true
1806 */
Yaowei Bai937f0c22018-02-06 15:41:18 -08001807bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001808{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001809 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001810 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001811
1812 if (idx == -1)
Yaowei Bai937f0c22018-02-06 15:41:18 -08001813 return false;
Wei Yangef415ef2017-02-22 15:45:04 -08001814 return (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001815 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001816}
1817
Stephen Boydeab30942012-05-24 00:45:21 -07001818/**
1819 * memblock_is_region_reserved - check if a region intersects reserved memory
1820 * @base: base of region to check
1821 * @size: size of region to check
1822 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001823 * Check if the region [@base, @base + @size) intersects a reserved
1824 * memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001825 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001826 * Return:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001827 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001828 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001829bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001830{
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001831 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001832}
1833
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001834void __init_memblock memblock_trim_memory(phys_addr_t align)
1835{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001836 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001837 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001838
Mike Rapoportcc6de162020-10-13 16:58:30 -07001839 for_each_mem_region(r) {
Emil Medve136199f2014-04-07 15:37:52 -07001840 orig_start = r->base;
1841 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001842 start = round_up(orig_start, align);
1843 end = round_down(orig_end, align);
1844
1845 if (start == orig_start && end == orig_end)
1846 continue;
1847
1848 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001849 r->base = start;
1850 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001851 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001852 memblock_remove_region(&memblock.memory,
1853 r - memblock.memory.regions);
1854 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001855 }
1856 }
1857}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001858
Yinghai Lu3661ca62010-09-15 13:05:29 -07001859void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001860{
1861 memblock.current_limit = limit;
1862}
1863
Laura Abbottfec51012014-02-27 01:23:43 +01001864phys_addr_t __init_memblock memblock_get_current_limit(void)
1865{
1866 return memblock.current_limit;
1867}
1868
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001869static void __init_memblock memblock_dump(struct memblock_type *type)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001870{
Miles Chen5d63f812017-02-22 15:46:42 -08001871 phys_addr_t base, end, size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001872 enum memblock_flags flags;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001873 int idx;
1874 struct memblock_region *rgn;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001875
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001876 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001877
Gioh Kim66e8b432017-11-15 17:33:42 -08001878 for_each_memblock_type(idx, type, rgn) {
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001879 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001880
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001881 base = rgn->base;
1882 size = rgn->size;
Miles Chen5d63f812017-02-22 15:46:42 -08001883 end = base + size - 1;
Tang Chen66a20752014-01-21 15:49:20 -08001884 flags = rgn->flags;
Mike Rapoport3f08a302020-06-03 15:57:02 -07001885#ifdef CONFIG_NEED_MULTIPLE_NODES
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001886 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1887 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1888 memblock_get_region_node(rgn));
1889#endif
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001890 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001891 type->name, idx, &base, &end, &size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001892 }
1893}
1894
Mike Rapoport87c55872020-10-13 16:57:54 -07001895static void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001896{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001897 pr_info("MEMBLOCK configuration:\n");
Miles Chen5d63f812017-02-22 15:46:42 -08001898 pr_info(" memory size = %pa reserved size = %pa\n",
1899 &memblock.memory.total_size,
1900 &memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001901
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001902 memblock_dump(&memblock.memory);
1903 memblock_dump(&memblock.reserved);
Heiko Carstens409efd42017-02-24 14:55:56 -08001904#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
David Hildenbrand77649902020-07-01 16:18:29 +02001905 memblock_dump(&physmem);
Heiko Carstens409efd42017-02-24 14:55:56 -08001906#endif
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001907}
1908
Mike Rapoport87c55872020-10-13 16:57:54 -07001909void __init_memblock memblock_dump_all(void)
1910{
1911 if (memblock_debug)
1912 __memblock_dump_all();
1913}
1914
Tejun Heo1aadc052011-12-08 10:22:08 -08001915void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001916{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001917 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001918}
1919
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001920static int __init early_memblock(char *p)
1921{
1922 if (p && strstr(p, "debug"))
1923 memblock_debug = 1;
1924 return 0;
1925}
1926early_param("memblock", early_memblock);
1927
Vijayanand Jitta3c2f1072022-08-11 20:05:52 +05301928static int __init early_memblock_nomap(char *str)
1929{
Suren Baghdasaryan425c0f12022-08-24 08:51:27 -07001930 return kstrtobool(str, &memblock_nomap_remove);
Vijayanand Jitta3c2f1072022-08-11 20:05:52 +05301931}
1932early_param("android12_only.will_be_removed_soon.memblock_nomap_remove", early_memblock_nomap);
1933
1934bool __init memblock_is_nomap_remove(void)
1935{
1936 return memblock_nomap_remove;
1937}
1938
Mike Rapoportbda49a82018-10-30 15:09:40 -07001939static void __init __free_pages_memory(unsigned long start, unsigned long end)
1940{
1941 int order;
1942
1943 while (start < end) {
1944 order = min(MAX_ORDER - 1UL, __ffs(start));
1945
1946 while (start + (1UL << order) > end)
1947 order--;
1948
1949 memblock_free_pages(pfn_to_page(start), start, order);
1950
1951 start += (1UL << order);
1952 }
1953}
1954
Tao Huang8b1cb4e2022-04-21 18:10:26 +08001955#if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP)
Simon Xueb6cd53a2021-06-21 11:19:42 +08001956int __init defer_free_memblock(void *unused)
1957{
1958 if (defer_start == 0)
1959 return 0;
1960
1961 pr_debug("start = %ld, end = %ld\n", defer_start, defer_end);
1962
1963 __free_pages_memory(defer_start, defer_end);
1964
1965 totalram_pages_add(defer_end - defer_start);
1966
1967 pr_info("%s: size %luM free %luM [%luM - %luM] total %luM\n", __func__,
1968 defer_free_block_size >> 20,
1969 (defer_end - defer_start) >> (20 - PAGE_SHIFT),
1970 defer_end >> (20 - PAGE_SHIFT),
1971 defer_start >> (20 - PAGE_SHIFT),
1972 totalram_pages() >> (20 - PAGE_SHIFT));
1973 return 0;
1974}
1975#endif
1976
Mike Rapoportbda49a82018-10-30 15:09:40 -07001977static unsigned long __init __free_memory_core(phys_addr_t start,
1978 phys_addr_t end)
1979{
1980 unsigned long start_pfn = PFN_UP(start);
1981 unsigned long end_pfn = min_t(unsigned long,
1982 PFN_DOWN(end), max_low_pfn);
1983
1984 if (start_pfn >= end_pfn)
1985 return 0;
1986
Tao Huang8b1cb4e2022-04-21 18:10:26 +08001987#if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP)
Simon Xueb6cd53a2021-06-21 11:19:42 +08001988 if ((end - start) > defer_free_block_size) {
1989 defer_start = start_pfn;
1990 defer_end = end_pfn;
1991
1992 return 0;
1993 }
1994#endif
1995
Mike Rapoportbda49a82018-10-30 15:09:40 -07001996 __free_pages_memory(start_pfn, end_pfn);
1997
1998 return end_pfn - start_pfn;
1999}
2000
2001static unsigned long __init free_low_memory_core_early(void)
2002{
2003 unsigned long count = 0;
2004 phys_addr_t start, end;
2005 u64 i;
2006
2007 memblock_clear_hotplug(0, -1);
2008
Mike Rapoport9f3d5ea2020-10-13 16:58:25 -07002009 for_each_reserved_mem_range(i, &start, &end)
Mike Rapoportbda49a82018-10-30 15:09:40 -07002010 reserve_bootmem_region(start, end);
2011
2012 /*
2013 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
2014 * because in some case like Node0 doesn't have RAM installed
2015 * low ram will be on Node1
2016 */
2017 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
2018 NULL)
2019 count += __free_memory_core(start, end);
2020
2021 return count;
2022}
2023
2024static int reset_managed_pages_done __initdata;
2025
2026void reset_node_managed_pages(pg_data_t *pgdat)
2027{
2028 struct zone *z;
2029
2030 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
Arun KS9705bea2018-12-28 00:34:24 -08002031 atomic_long_set(&z->managed_pages, 0);
Mike Rapoportbda49a82018-10-30 15:09:40 -07002032}
2033
2034void __init reset_all_zones_managed_pages(void)
2035{
2036 struct pglist_data *pgdat;
2037
2038 if (reset_managed_pages_done)
2039 return;
2040
2041 for_each_online_pgdat(pgdat)
2042 reset_node_managed_pages(pgdat);
2043
2044 reset_managed_pages_done = 1;
2045}
2046
2047/**
2048 * memblock_free_all - release free pages to the buddy allocator
2049 *
2050 * Return: the number of pages actually released.
2051 */
2052unsigned long __init memblock_free_all(void)
2053{
2054 unsigned long pages;
2055
2056 reset_all_zones_managed_pages();
2057
2058 pages = free_low_memory_core_early();
Arun KSca79b0c2018-12-28 00:34:29 -08002059 totalram_pages_add(pages);
Mike Rapoportbda49a82018-10-30 15:09:40 -07002060
2061 return pages;
2062}
2063
Mike Rapoport350e88b2019-05-13 17:22:59 -07002064#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002065
2066static int memblock_debug_show(struct seq_file *m, void *private)
2067{
2068 struct memblock_type *type = m->private;
2069 struct memblock_region *reg;
2070 int i;
Miles Chen5d63f812017-02-22 15:46:42 -08002071 phys_addr_t end;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002072
2073 for (i = 0; i < type->cnt; i++) {
2074 reg = &type->regions[i];
Miles Chen5d63f812017-02-22 15:46:42 -08002075 end = reg->base + reg->size - 1;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002076
Miles Chen5d63f812017-02-22 15:46:42 -08002077 seq_printf(m, "%4d: ", i);
2078 seq_printf(m, "%pa..%pa\n", &reg->base, &end);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002079 }
2080 return 0;
2081}
Andy Shevchenko5ad35092018-04-05 16:23:16 -07002082DEFINE_SHOW_ATTRIBUTE(memblock_debug);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002083
2084static int __init memblock_init_debugfs(void)
2085{
2086 struct dentry *root = debugfs_create_dir("memblock", NULL);
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08002087
Joe Perches0825a6f2018-06-14 15:27:58 -07002088 debugfs_create_file("memory", 0444, root,
2089 &memblock.memory, &memblock_debug_fops);
2090 debugfs_create_file("reserved", 0444, root,
2091 &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01002092#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
David Hildenbrand77649902020-07-01 16:18:29 +02002093 debugfs_create_file("physmem", 0444, root, &physmem,
2094 &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01002095#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07002096
2097 return 0;
2098}
2099__initcall(memblock_init_debugfs);
2100
2101#endif /* CONFIG_DEBUG_FS */