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