blob: 11f6ae37d669982408fb22a54f5aa73cb18c8060 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Procedures for maintaining information about logical memory blocks.
4 *
5 * Peter Bergner, IBM Corp. June 2001.
6 * Copyright (C) 2001 Peter Bergner.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <linux/init.h>
12#include <linux/bitops.h>
13#include <linux/poison.h>
14#include <linux/pfn.h>
15#include <linux/debugfs.h>
16#include <linux/kmemleak.h>
17#include <linux/seq_file.h>
18#include <linux/memblock.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019
20#include <asm/sections.h>
21#include <linux/io.h>
22
23#include "internal.h"
24
David Brazdil0f672f62019-12-10 10:32:29 +000025#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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032/**
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
47 * * ``physmap`` - describes the actual physical memory regardless of
48 * the possible restrictions; the ``physmap`` type is only available
49 * on some architectures.
50 *
51 * Each region is represented by :c:type:`struct memblock_region` that
52 * defines the region extents, its attributes and NUMA node id on NUMA
53 * systems. Every memory type is described by the :c:type:`struct
54 * memblock_type` which contains an array of memory regions along with
55 * the allocator metadata. The memory types are nicely wrapped with
56 * :c:type:`struct memblock`. This structure is statically initialzed
57 * at build time. The region arrays for the "memory" and "reserved"
58 * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the
59 * "physmap" type to %INIT_PHYSMEM_REGIONS.
60 * The :c:func:`memblock_allow_resize` enables automatic resizing of
61 * the region arrays during addition of new regions. This feature
62 * should be used with care so that memory allocated for the region
63 * array will not overlap with areas that should be reserved, for
64 * example initrd.
65 *
66 * The early architecture setup should tell memblock what the physical
67 * memory layout is by using :c:func:`memblock_add` or
68 * :c:func:`memblock_add_node` functions. The first function does not
69 * assign the region to a NUMA node and it is appropriate for UMA
70 * systems. Yet, it is possible to use it on NUMA systems as well and
71 * assign the region to a NUMA node later in the setup process using
72 * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node`
73 * performs such an assignment directly.
74 *
David Brazdil0f672f62019-12-10 10:32:29 +000075 * Once memblock is setup the memory can be allocated using one of the
76 * API variants:
77 *
78 * * :c:func:`memblock_phys_alloc*` - these functions return the
79 * **physical** address of the allocated memory
80 * * :c:func:`memblock_alloc*` - these functions return the **virtual**
81 * address of the allocated memory.
82 *
83 * Note, that both API variants use implict assumptions about allowed
84 * memory ranges and the fallback methods. Consult the documentation
85 * of :c:func:`memblock_alloc_internal` and
86 * :c:func:`memblock_alloc_range_nid` functions for more elaboarte
87 * description.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088 *
89 * As the system boot progresses, the architecture specific
90 * :c:func:`mem_init` function frees all the memory to the buddy page
91 * allocator.
92 *
David Brazdil0f672f62019-12-10 10:32:29 +000093 * Unless an architecure enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094 * memblock data structures will be discarded after the system
95 * initialization compltes.
96 */
97
David Brazdil0f672f62019-12-10 10:32:29 +000098#ifndef CONFIG_NEED_MULTIPLE_NODES
99struct pglist_data __refdata contig_page_data;
100EXPORT_SYMBOL(contig_page_data);
101#endif
102
103unsigned long max_low_pfn;
104unsigned long min_low_pfn;
105unsigned long max_pfn;
106unsigned long long max_possible_pfn;
107
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
David Brazdil0f672f62019-12-10 10:32:29 +0000109static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
111static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
112#endif
113
114struct memblock memblock __initdata_memblock = {
115 .memory.regions = memblock_memory_init_regions,
116 .memory.cnt = 1, /* empty dummy entry */
117 .memory.max = INIT_MEMBLOCK_REGIONS,
118 .memory.name = "memory",
119
120 .reserved.regions = memblock_reserved_init_regions,
121 .reserved.cnt = 1, /* empty dummy entry */
David Brazdil0f672f62019-12-10 10:32:29 +0000122 .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123 .reserved.name = "reserved",
124
125#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
126 .physmem.regions = memblock_physmem_init_regions,
127 .physmem.cnt = 1, /* empty dummy entry */
128 .physmem.max = INIT_PHYSMEM_REGIONS,
129 .physmem.name = "physmem",
130#endif
131
132 .bottom_up = false,
133 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
134};
135
136int memblock_debug __initdata_memblock;
137static bool system_has_some_mirror __initdata_memblock = false;
138static int memblock_can_resize __initdata_memblock;
139static int memblock_memory_in_slab __initdata_memblock = 0;
140static int memblock_reserved_in_slab __initdata_memblock = 0;
141
David Brazdil0f672f62019-12-10 10:32:29 +0000142static enum memblock_flags __init_memblock choose_memblock_flags(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143{
144 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
145}
146
147/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
148static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
149{
150 return *size = min(*size, PHYS_ADDR_MAX - base);
151}
152
153/*
154 * Address comparison utilities
155 */
156static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
157 phys_addr_t base2, phys_addr_t size2)
158{
159 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
160}
161
162bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
163 phys_addr_t base, phys_addr_t size)
164{
165 unsigned long i;
166
167 for (i = 0; i < type->cnt; i++)
168 if (memblock_addrs_overlap(base, size, type->regions[i].base,
169 type->regions[i].size))
170 break;
171 return i < type->cnt;
172}
173
174/**
175 * __memblock_find_range_bottom_up - find free area utility in bottom-up
176 * @start: start of candidate range
177 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
178 * %MEMBLOCK_ALLOC_ACCESSIBLE
179 * @size: size of free area to find
180 * @align: alignment of free area to find
181 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
182 * @flags: pick from blocks based on memory attributes
183 *
184 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
185 *
186 * Return:
187 * Found address on success, 0 on failure.
188 */
189static phys_addr_t __init_memblock
190__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
191 phys_addr_t size, phys_addr_t align, int nid,
192 enum memblock_flags flags)
193{
194 phys_addr_t this_start, this_end, cand;
195 u64 i;
196
197 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
198 this_start = clamp(this_start, start, end);
199 this_end = clamp(this_end, start, end);
200
201 cand = round_up(this_start, align);
202 if (cand < this_end && this_end - cand >= size)
203 return cand;
204 }
205
206 return 0;
207}
208
209/**
210 * __memblock_find_range_top_down - find free area utility, in top-down
211 * @start: start of candidate range
212 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
213 * %MEMBLOCK_ALLOC_ACCESSIBLE
214 * @size: size of free area to find
215 * @align: alignment of free area to find
216 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
217 * @flags: pick from blocks based on memory attributes
218 *
219 * Utility called from memblock_find_in_range_node(), find free area top-down.
220 *
221 * Return:
222 * Found address on success, 0 on failure.
223 */
224static phys_addr_t __init_memblock
225__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
226 phys_addr_t size, phys_addr_t align, int nid,
227 enum memblock_flags flags)
228{
229 phys_addr_t this_start, this_end, cand;
230 u64 i;
231
232 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
233 NULL) {
234 this_start = clamp(this_start, start, end);
235 this_end = clamp(this_end, start, end);
236
237 if (this_end < size)
238 continue;
239
240 cand = round_down(this_end - size, align);
241 if (cand >= this_start)
242 return cand;
243 }
244
245 return 0;
246}
247
248/**
249 * memblock_find_in_range_node - find free area in given range and node
250 * @size: size of free area to find
251 * @align: alignment of free area to find
252 * @start: start of candidate range
253 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
254 * %MEMBLOCK_ALLOC_ACCESSIBLE
255 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
256 * @flags: pick from blocks based on memory attributes
257 *
258 * Find @size free area aligned to @align in the specified range and node.
259 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260 * Return:
261 * Found address on success, 0 on failure.
262 */
David Brazdil0f672f62019-12-10 10:32:29 +0000263static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000264 phys_addr_t align, phys_addr_t start,
265 phys_addr_t end, int nid,
266 enum memblock_flags flags)
267{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268 /* pump up @end */
David Brazdil0f672f62019-12-10 10:32:29 +0000269 if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
270 end == MEMBLOCK_ALLOC_KASAN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271 end = memblock.current_limit;
272
273 /* avoid allocating the first page */
274 start = max_t(phys_addr_t, start, PAGE_SIZE);
275 end = max(start, end);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000276
Olivier Deprez0e641232021-09-23 10:07:05 +0200277 if (memblock_bottom_up())
278 return __memblock_find_range_bottom_up(start, end, size, align,
279 nid, flags);
280 else
281 return __memblock_find_range_top_down(start, end, size, align,
282 nid, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283}
284
285/**
286 * memblock_find_in_range - find free area in given range
287 * @start: start of candidate range
288 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
289 * %MEMBLOCK_ALLOC_ACCESSIBLE
290 * @size: size of free area to find
291 * @align: alignment of free area to find
292 *
293 * Find @size free area aligned to @align in the specified range.
294 *
295 * Return:
296 * Found address on success, 0 on failure.
297 */
298phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
299 phys_addr_t end, phys_addr_t size,
300 phys_addr_t align)
301{
302 phys_addr_t ret;
303 enum memblock_flags flags = choose_memblock_flags();
304
305again:
306 ret = memblock_find_in_range_node(size, align, start, end,
307 NUMA_NO_NODE, flags);
308
309 if (!ret && (flags & MEMBLOCK_MIRROR)) {
310 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
311 &size);
312 flags &= ~MEMBLOCK_MIRROR;
313 goto again;
314 }
315
316 return ret;
317}
318
319static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
320{
321 type->total_size -= type->regions[r].size;
322 memmove(&type->regions[r], &type->regions[r + 1],
323 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
324 type->cnt--;
325
326 /* Special case for empty arrays */
327 if (type->cnt == 0) {
328 WARN_ON(type->total_size != 0);
329 type->cnt = 1;
330 type->regions[0].base = 0;
331 type->regions[0].size = 0;
332 type->regions[0].flags = 0;
333 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
334 }
335}
336
David Brazdil0f672f62019-12-10 10:32:29 +0000337#ifndef CONFIG_ARCH_KEEP_MEMBLOCK
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000338/**
339 * memblock_discard - discard memory and reserved arrays if they were allocated
340 */
341void __init memblock_discard(void)
342{
343 phys_addr_t addr, size;
344
345 if (memblock.reserved.regions != memblock_reserved_init_regions) {
346 addr = __pa(memblock.reserved.regions);
347 size = PAGE_ALIGN(sizeof(struct memblock_region) *
348 memblock.reserved.max);
349 __memblock_free_late(addr, size);
350 }
351
352 if (memblock.memory.regions != memblock_memory_init_regions) {
353 addr = __pa(memblock.memory.regions);
354 size = PAGE_ALIGN(sizeof(struct memblock_region) *
355 memblock.memory.max);
356 __memblock_free_late(addr, size);
357 }
358}
359#endif
360
361/**
362 * memblock_double_array - double the size of the memblock regions array
363 * @type: memblock type of the regions array being doubled
364 * @new_area_start: starting address of memory range to avoid overlap with
365 * @new_area_size: size of memory range to avoid overlap with
366 *
367 * Double the size of the @type regions array. If memblock is being used to
368 * allocate memory for a new reserved regions array and there is a previously
369 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
370 * waiting to be reserved, ensure the memory used by the new array does
371 * not overlap.
372 *
373 * Return:
374 * 0 on success, -1 on failure.
375 */
376static int __init_memblock memblock_double_array(struct memblock_type *type,
377 phys_addr_t new_area_start,
378 phys_addr_t new_area_size)
379{
380 struct memblock_region *new_array, *old_array;
381 phys_addr_t old_alloc_size, new_alloc_size;
382 phys_addr_t old_size, new_size, addr, new_end;
383 int use_slab = slab_is_available();
384 int *in_slab;
385
386 /* We don't allow resizing until we know about the reserved regions
387 * of memory that aren't suitable for allocation
388 */
389 if (!memblock_can_resize)
390 return -1;
391
392 /* Calculate new doubled size */
393 old_size = type->max * sizeof(struct memblock_region);
394 new_size = old_size << 1;
395 /*
396 * We need to allocated new one align to PAGE_SIZE,
397 * so we can free them completely later.
398 */
399 old_alloc_size = PAGE_ALIGN(old_size);
400 new_alloc_size = PAGE_ALIGN(new_size);
401
402 /* Retrieve the slab flag */
403 if (type == &memblock.memory)
404 in_slab = &memblock_memory_in_slab;
405 else
406 in_slab = &memblock_reserved_in_slab;
407
David Brazdil0f672f62019-12-10 10:32:29 +0000408 /* Try to find some space for it */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000409 if (use_slab) {
410 new_array = kmalloc(new_size, GFP_KERNEL);
411 addr = new_array ? __pa(new_array) : 0;
412 } else {
413 /* only exclude range when trying to double reserved.regions */
414 if (type != &memblock.reserved)
415 new_area_start = new_area_size = 0;
416
417 addr = memblock_find_in_range(new_area_start + new_area_size,
418 memblock.current_limit,
419 new_alloc_size, PAGE_SIZE);
420 if (!addr && new_area_size)
421 addr = memblock_find_in_range(0,
422 min(new_area_start, memblock.current_limit),
423 new_alloc_size, PAGE_SIZE);
424
425 new_array = addr ? __va(addr) : NULL;
426 }
427 if (!addr) {
428 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
429 type->name, type->max, type->max * 2);
430 return -1;
431 }
432
433 new_end = addr + new_size - 1;
434 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
435 type->name, type->max * 2, &addr, &new_end);
436
437 /*
438 * Found space, we now need to move the array over before we add the
439 * reserved region since it may be our reserved array itself that is
440 * full.
441 */
442 memcpy(new_array, type->regions, old_size);
443 memset(new_array + type->max, 0, old_size);
444 old_array = type->regions;
445 type->regions = new_array;
446 type->max <<= 1;
447
448 /* Free old array. We needn't free it if the array is the static one */
449 if (*in_slab)
450 kfree(old_array);
451 else if (old_array != memblock_memory_init_regions &&
452 old_array != memblock_reserved_init_regions)
453 memblock_free(__pa(old_array), old_alloc_size);
454
455 /*
456 * Reserve the new array if that comes from the memblock. Otherwise, we
457 * needn't do it
458 */
459 if (!use_slab)
460 BUG_ON(memblock_reserve(addr, new_alloc_size));
461
462 /* Update slab flag */
463 *in_slab = use_slab;
464
465 return 0;
466}
467
468/**
469 * memblock_merge_regions - merge neighboring compatible regions
470 * @type: memblock type to scan
471 *
472 * Scan @type and merge neighboring compatible regions.
473 */
474static void __init_memblock memblock_merge_regions(struct memblock_type *type)
475{
476 int i = 0;
477
478 /* cnt never goes below 1 */
479 while (i < type->cnt - 1) {
480 struct memblock_region *this = &type->regions[i];
481 struct memblock_region *next = &type->regions[i + 1];
482
483 if (this->base + this->size != next->base ||
484 memblock_get_region_node(this) !=
485 memblock_get_region_node(next) ||
486 this->flags != next->flags) {
487 BUG_ON(this->base + this->size > next->base);
488 i++;
489 continue;
490 }
491
492 this->size += next->size;
493 /* move forward from next + 1, index of which is i + 2 */
494 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
495 type->cnt--;
496 }
497}
498
499/**
500 * memblock_insert_region - insert new memblock region
501 * @type: memblock type to insert into
502 * @idx: index for the insertion point
503 * @base: base address of the new region
504 * @size: size of the new region
505 * @nid: node id of the new region
506 * @flags: flags of the new region
507 *
508 * Insert new memblock region [@base, @base + @size) into @type at @idx.
509 * @type must already have extra room to accommodate the new region.
510 */
511static void __init_memblock memblock_insert_region(struct memblock_type *type,
512 int idx, phys_addr_t base,
513 phys_addr_t size,
514 int nid,
515 enum memblock_flags flags)
516{
517 struct memblock_region *rgn = &type->regions[idx];
518
519 BUG_ON(type->cnt >= type->max);
520 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
521 rgn->base = base;
522 rgn->size = size;
523 rgn->flags = flags;
524 memblock_set_region_node(rgn, nid);
525 type->cnt++;
526 type->total_size += size;
527}
528
529/**
530 * memblock_add_range - add new memblock region
531 * @type: memblock type to add new region into
532 * @base: base address of the new region
533 * @size: size of the new region
534 * @nid: nid of the new region
535 * @flags: flags of the new region
536 *
537 * Add new memblock region [@base, @base + @size) into @type. The new region
538 * is allowed to overlap with existing ones - overlaps don't affect already
539 * existing regions. @type is guaranteed to be minimal (all neighbouring
540 * compatible regions are merged) after the addition.
541 *
542 * Return:
543 * 0 on success, -errno on failure.
544 */
545int __init_memblock memblock_add_range(struct memblock_type *type,
546 phys_addr_t base, phys_addr_t size,
547 int nid, enum memblock_flags flags)
548{
549 bool insert = false;
550 phys_addr_t obase = base;
551 phys_addr_t end = base + memblock_cap_size(base, &size);
552 int idx, nr_new;
553 struct memblock_region *rgn;
554
555 if (!size)
556 return 0;
557
558 /* special case for empty array */
559 if (type->regions[0].size == 0) {
560 WARN_ON(type->cnt != 1 || type->total_size);
561 type->regions[0].base = base;
562 type->regions[0].size = size;
563 type->regions[0].flags = flags;
564 memblock_set_region_node(&type->regions[0], nid);
565 type->total_size = size;
566 return 0;
567 }
568repeat:
569 /*
570 * The following is executed twice. Once with %false @insert and
571 * then with %true. The first counts the number of regions needed
572 * to accommodate the new area. The second actually inserts them.
573 */
574 base = obase;
575 nr_new = 0;
576
577 for_each_memblock_type(idx, type, rgn) {
578 phys_addr_t rbase = rgn->base;
579 phys_addr_t rend = rbase + rgn->size;
580
581 if (rbase >= end)
582 break;
583 if (rend <= base)
584 continue;
585 /*
586 * @rgn overlaps. If it separates the lower part of new
587 * area, insert that portion.
588 */
589 if (rbase > base) {
590#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
591 WARN_ON(nid != memblock_get_region_node(rgn));
592#endif
593 WARN_ON(flags != rgn->flags);
594 nr_new++;
595 if (insert)
596 memblock_insert_region(type, idx++, base,
597 rbase - base, nid,
598 flags);
599 }
600 /* area below @rend is dealt with, forget about it */
601 base = min(rend, end);
602 }
603
604 /* insert the remaining portion */
605 if (base < end) {
606 nr_new++;
607 if (insert)
608 memblock_insert_region(type, idx, base, end - base,
609 nid, flags);
610 }
611
612 if (!nr_new)
613 return 0;
614
615 /*
616 * If this was the first round, resize array and repeat for actual
617 * insertions; otherwise, merge and return.
618 */
619 if (!insert) {
620 while (type->cnt + nr_new > type->max)
621 if (memblock_double_array(type, obase, size) < 0)
622 return -ENOMEM;
623 insert = true;
624 goto repeat;
625 } else {
626 memblock_merge_regions(type);
627 return 0;
628 }
629}
630
631/**
632 * memblock_add_node - add new memblock region within a NUMA node
633 * @base: base address of the new region
634 * @size: size of the new region
635 * @nid: nid of the new region
636 *
637 * Add new memblock region [@base, @base + @size) to the "memory"
638 * type. See memblock_add_range() description for mode details
639 *
640 * Return:
641 * 0 on success, -errno on failure.
642 */
643int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
644 int nid)
645{
646 return memblock_add_range(&memblock.memory, base, size, nid, 0);
647}
648
649/**
650 * memblock_add - add new memblock region
651 * @base: base address of the new region
652 * @size: size of the new region
653 *
654 * Add new memblock region [@base, @base + @size) to the "memory"
655 * type. See memblock_add_range() description for mode details
656 *
657 * Return:
658 * 0 on success, -errno on failure.
659 */
660int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
661{
662 phys_addr_t end = base + size - 1;
663
David Brazdil0f672f62019-12-10 10:32:29 +0000664 memblock_dbg("memblock_add: [%pa-%pa] %pS\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000665 &base, &end, (void *)_RET_IP_);
666
667 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
668}
669
670/**
671 * memblock_isolate_range - isolate given range into disjoint memblocks
672 * @type: memblock type to isolate range for
673 * @base: base of range to isolate
674 * @size: size of range to isolate
675 * @start_rgn: out parameter for the start of isolated region
676 * @end_rgn: out parameter for the end of isolated region
677 *
678 * Walk @type and ensure that regions don't cross the boundaries defined by
679 * [@base, @base + @size). Crossing regions are split at the boundaries,
680 * which may create at most two more regions. The index of the first
681 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
682 *
683 * Return:
684 * 0 on success, -errno on failure.
685 */
686static int __init_memblock memblock_isolate_range(struct memblock_type *type,
687 phys_addr_t base, phys_addr_t size,
688 int *start_rgn, int *end_rgn)
689{
690 phys_addr_t end = base + memblock_cap_size(base, &size);
691 int idx;
692 struct memblock_region *rgn;
693
694 *start_rgn = *end_rgn = 0;
695
696 if (!size)
697 return 0;
698
699 /* we'll create at most two more regions */
700 while (type->cnt + 2 > type->max)
701 if (memblock_double_array(type, base, size) < 0)
702 return -ENOMEM;
703
704 for_each_memblock_type(idx, type, rgn) {
705 phys_addr_t rbase = rgn->base;
706 phys_addr_t rend = rbase + rgn->size;
707
708 if (rbase >= end)
709 break;
710 if (rend <= base)
711 continue;
712
713 if (rbase < base) {
714 /*
715 * @rgn intersects from below. Split and continue
716 * to process the next region - the new top half.
717 */
718 rgn->base = base;
719 rgn->size -= base - rbase;
720 type->total_size -= base - rbase;
721 memblock_insert_region(type, idx, rbase, base - rbase,
722 memblock_get_region_node(rgn),
723 rgn->flags);
724 } else if (rend > end) {
725 /*
726 * @rgn intersects from above. Split and redo the
727 * current region - the new bottom half.
728 */
729 rgn->base = end;
730 rgn->size -= end - rbase;
731 type->total_size -= end - rbase;
732 memblock_insert_region(type, idx--, rbase, end - rbase,
733 memblock_get_region_node(rgn),
734 rgn->flags);
735 } else {
736 /* @rgn is fully contained, record it */
737 if (!*end_rgn)
738 *start_rgn = idx;
739 *end_rgn = idx + 1;
740 }
741 }
742
743 return 0;
744}
745
746static int __init_memblock memblock_remove_range(struct memblock_type *type,
747 phys_addr_t base, phys_addr_t size)
748{
749 int start_rgn, end_rgn;
750 int i, ret;
751
752 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
753 if (ret)
754 return ret;
755
756 for (i = end_rgn - 1; i >= start_rgn; i--)
757 memblock_remove_region(type, i);
758 return 0;
759}
760
761int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
762{
763 phys_addr_t end = base + size - 1;
764
765 memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
766 &base, &end, (void *)_RET_IP_);
767
768 return memblock_remove_range(&memblock.memory, base, size);
769}
770
David Brazdil0f672f62019-12-10 10:32:29 +0000771/**
772 * memblock_free - free boot memory block
773 * @base: phys starting address of the boot memory block
774 * @size: size of the boot memory block in bytes
775 *
776 * Free boot memory block previously allocated by memblock_alloc_xx() API.
777 * The freeing memory will not be released to the buddy allocator.
778 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000779int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
780{
781 phys_addr_t end = base + size - 1;
782
David Brazdil0f672f62019-12-10 10:32:29 +0000783 memblock_dbg(" memblock_free: [%pa-%pa] %pS\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000784 &base, &end, (void *)_RET_IP_);
785
786 kmemleak_free_part_phys(base, size);
787 return memblock_remove_range(&memblock.reserved, base, size);
788}
789
790int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
791{
792 phys_addr_t end = base + size - 1;
793
David Brazdil0f672f62019-12-10 10:32:29 +0000794 memblock_dbg("memblock_reserve: [%pa-%pa] %pS\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795 &base, &end, (void *)_RET_IP_);
796
797 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
798}
799
800/**
801 * memblock_setclr_flag - set or clear flag for a memory region
802 * @base: base address of the region
803 * @size: size of the region
804 * @set: set or clear the flag
805 * @flag: the flag to udpate
806 *
807 * This function isolates region [@base, @base + @size), and sets/clears flag
808 *
809 * Return: 0 on success, -errno on failure.
810 */
811static int __init_memblock memblock_setclr_flag(phys_addr_t base,
812 phys_addr_t size, int set, int flag)
813{
814 struct memblock_type *type = &memblock.memory;
815 int i, ret, start_rgn, end_rgn;
816
817 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
818 if (ret)
819 return ret;
820
David Brazdil0f672f62019-12-10 10:32:29 +0000821 for (i = start_rgn; i < end_rgn; i++) {
822 struct memblock_region *r = &type->regions[i];
823
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000824 if (set)
David Brazdil0f672f62019-12-10 10:32:29 +0000825 r->flags |= flag;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000826 else
David Brazdil0f672f62019-12-10 10:32:29 +0000827 r->flags &= ~flag;
828 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000829
830 memblock_merge_regions(type);
831 return 0;
832}
833
834/**
835 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
836 * @base: the base phys addr of the region
837 * @size: the size of the region
838 *
839 * Return: 0 on success, -errno on failure.
840 */
841int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
842{
843 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
844}
845
846/**
847 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
848 * @base: the base phys addr of the region
849 * @size: the size of the region
850 *
851 * Return: 0 on success, -errno on failure.
852 */
853int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
854{
855 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
856}
857
858/**
859 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
860 * @base: the base phys addr of the region
861 * @size: the size of the region
862 *
863 * Return: 0 on success, -errno on failure.
864 */
865int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
866{
867 system_has_some_mirror = true;
868
869 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
870}
871
872/**
873 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
874 * @base: the base phys addr of the region
875 * @size: the size of the region
876 *
877 * Return: 0 on success, -errno on failure.
878 */
879int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
880{
881 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
882}
883
884/**
885 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
886 * @base: the base phys addr of the region
887 * @size: the size of the region
888 *
889 * Return: 0 on success, -errno on failure.
890 */
891int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
892{
893 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
894}
895
896/**
897 * __next_reserved_mem_region - next function for for_each_reserved_region()
898 * @idx: pointer to u64 loop variable
899 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
900 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
901 *
902 * Iterate over all reserved memory regions.
903 */
904void __init_memblock __next_reserved_mem_region(u64 *idx,
905 phys_addr_t *out_start,
906 phys_addr_t *out_end)
907{
908 struct memblock_type *type = &memblock.reserved;
909
910 if (*idx < type->cnt) {
911 struct memblock_region *r = &type->regions[*idx];
912 phys_addr_t base = r->base;
913 phys_addr_t size = r->size;
914
915 if (out_start)
916 *out_start = base;
917 if (out_end)
918 *out_end = base + size - 1;
919
920 *idx += 1;
921 return;
922 }
923
924 /* signal end of iteration */
925 *idx = ULLONG_MAX;
926}
927
David Brazdil0f672f62019-12-10 10:32:29 +0000928static bool should_skip_region(struct memblock_region *m, int nid, int flags)
929{
930 int m_nid = memblock_get_region_node(m);
931
932 /* only memory regions are associated with nodes, check it */
933 if (nid != NUMA_NO_NODE && nid != m_nid)
934 return true;
935
936 /* skip hotpluggable memory regions if needed */
937 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
938 return true;
939
940 /* if we want mirror memory skip non-mirror memory regions */
941 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
942 return true;
943
944 /* skip nomap memory unless we were asked for it explicitly */
945 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
946 return true;
947
948 return false;
949}
950
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951/**
David Brazdil0f672f62019-12-10 10:32:29 +0000952 * __next_mem_range - next function for for_each_free_mem_range() etc.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000953 * @idx: pointer to u64 loop variable
954 * @nid: node selector, %NUMA_NO_NODE for all nodes
955 * @flags: pick from blocks based on memory attributes
956 * @type_a: pointer to memblock_type from where the range is taken
957 * @type_b: pointer to memblock_type which excludes memory from being taken
958 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
959 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
960 * @out_nid: ptr to int for nid of the range, can be %NULL
961 *
962 * Find the first area from *@idx which matches @nid, fill the out
963 * parameters, and update *@idx for the next iteration. The lower 32bit of
964 * *@idx contains index into type_a and the upper 32bit indexes the
965 * areas before each region in type_b. For example, if type_b regions
966 * look like the following,
967 *
968 * 0:[0-16), 1:[32-48), 2:[128-130)
969 *
970 * The upper 32bit indexes the following regions.
971 *
972 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
973 *
974 * As both region arrays are sorted, the function advances the two indices
975 * in lockstep and returns each intersection.
976 */
977void __init_memblock __next_mem_range(u64 *idx, int nid,
978 enum memblock_flags flags,
979 struct memblock_type *type_a,
980 struct memblock_type *type_b,
981 phys_addr_t *out_start,
982 phys_addr_t *out_end, int *out_nid)
983{
984 int idx_a = *idx & 0xffffffff;
985 int idx_b = *idx >> 32;
986
987 if (WARN_ONCE(nid == MAX_NUMNODES,
988 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
989 nid = NUMA_NO_NODE;
990
991 for (; idx_a < type_a->cnt; idx_a++) {
992 struct memblock_region *m = &type_a->regions[idx_a];
993
994 phys_addr_t m_start = m->base;
995 phys_addr_t m_end = m->base + m->size;
996 int m_nid = memblock_get_region_node(m);
997
David Brazdil0f672f62019-12-10 10:32:29 +0000998 if (should_skip_region(m, nid, flags))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000999 continue;
1000
1001 if (!type_b) {
1002 if (out_start)
1003 *out_start = m_start;
1004 if (out_end)
1005 *out_end = m_end;
1006 if (out_nid)
1007 *out_nid = m_nid;
1008 idx_a++;
1009 *idx = (u32)idx_a | (u64)idx_b << 32;
1010 return;
1011 }
1012
1013 /* scan areas before each reservation */
1014 for (; idx_b < type_b->cnt + 1; idx_b++) {
1015 struct memblock_region *r;
1016 phys_addr_t r_start;
1017 phys_addr_t r_end;
1018
1019 r = &type_b->regions[idx_b];
1020 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1021 r_end = idx_b < type_b->cnt ?
1022 r->base : PHYS_ADDR_MAX;
1023
1024 /*
1025 * if idx_b advanced past idx_a,
1026 * break out to advance idx_a
1027 */
1028 if (r_start >= m_end)
1029 break;
1030 /* if the two regions intersect, we're done */
1031 if (m_start < r_end) {
1032 if (out_start)
1033 *out_start =
1034 max(m_start, r_start);
1035 if (out_end)
1036 *out_end = min(m_end, r_end);
1037 if (out_nid)
1038 *out_nid = m_nid;
1039 /*
1040 * The region which ends first is
1041 * advanced for the next iteration.
1042 */
1043 if (m_end <= r_end)
1044 idx_a++;
1045 else
1046 idx_b++;
1047 *idx = (u32)idx_a | (u64)idx_b << 32;
1048 return;
1049 }
1050 }
1051 }
1052
1053 /* signal end of iteration */
1054 *idx = ULLONG_MAX;
1055}
1056
1057/**
1058 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1059 *
1060 * @idx: pointer to u64 loop variable
1061 * @nid: node selector, %NUMA_NO_NODE for all nodes
1062 * @flags: pick from blocks based on memory attributes
1063 * @type_a: pointer to memblock_type from where the range is taken
1064 * @type_b: pointer to memblock_type which excludes memory from being taken
1065 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1066 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1067 * @out_nid: ptr to int for nid of the range, can be %NULL
1068 *
1069 * Finds the next range from type_a which is not marked as unsuitable
1070 * in type_b.
1071 *
1072 * Reverse of __next_mem_range().
1073 */
1074void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1075 enum memblock_flags flags,
1076 struct memblock_type *type_a,
1077 struct memblock_type *type_b,
1078 phys_addr_t *out_start,
1079 phys_addr_t *out_end, int *out_nid)
1080{
1081 int idx_a = *idx & 0xffffffff;
1082 int idx_b = *idx >> 32;
1083
1084 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1085 nid = NUMA_NO_NODE;
1086
1087 if (*idx == (u64)ULLONG_MAX) {
1088 idx_a = type_a->cnt - 1;
1089 if (type_b != NULL)
1090 idx_b = type_b->cnt;
1091 else
1092 idx_b = 0;
1093 }
1094
1095 for (; idx_a >= 0; idx_a--) {
1096 struct memblock_region *m = &type_a->regions[idx_a];
1097
1098 phys_addr_t m_start = m->base;
1099 phys_addr_t m_end = m->base + m->size;
1100 int m_nid = memblock_get_region_node(m);
1101
David Brazdil0f672f62019-12-10 10:32:29 +00001102 if (should_skip_region(m, nid, flags))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001103 continue;
1104
1105 if (!type_b) {
1106 if (out_start)
1107 *out_start = m_start;
1108 if (out_end)
1109 *out_end = m_end;
1110 if (out_nid)
1111 *out_nid = m_nid;
1112 idx_a--;
1113 *idx = (u32)idx_a | (u64)idx_b << 32;
1114 return;
1115 }
1116
1117 /* scan areas before each reservation */
1118 for (; idx_b >= 0; idx_b--) {
1119 struct memblock_region *r;
1120 phys_addr_t r_start;
1121 phys_addr_t r_end;
1122
1123 r = &type_b->regions[idx_b];
1124 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1125 r_end = idx_b < type_b->cnt ?
1126 r->base : PHYS_ADDR_MAX;
1127 /*
1128 * if idx_b advanced past idx_a,
1129 * break out to advance idx_a
1130 */
1131
1132 if (r_end <= m_start)
1133 break;
1134 /* if the two regions intersect, we're done */
1135 if (m_end > r_start) {
1136 if (out_start)
1137 *out_start = max(m_start, r_start);
1138 if (out_end)
1139 *out_end = min(m_end, r_end);
1140 if (out_nid)
1141 *out_nid = m_nid;
1142 if (m_start >= r_start)
1143 idx_a--;
1144 else
1145 idx_b--;
1146 *idx = (u32)idx_a | (u64)idx_b << 32;
1147 return;
1148 }
1149 }
1150 }
1151 /* signal end of iteration */
1152 *idx = ULLONG_MAX;
1153}
1154
1155#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1156/*
David Brazdil0f672f62019-12-10 10:32:29 +00001157 * Common iterator interface used to define for_each_mem_pfn_range().
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001158 */
1159void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1160 unsigned long *out_start_pfn,
1161 unsigned long *out_end_pfn, int *out_nid)
1162{
1163 struct memblock_type *type = &memblock.memory;
1164 struct memblock_region *r;
1165
1166 while (++*idx < type->cnt) {
1167 r = &type->regions[*idx];
1168
1169 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1170 continue;
1171 if (nid == MAX_NUMNODES || nid == r->nid)
1172 break;
1173 }
1174 if (*idx >= type->cnt) {
1175 *idx = -1;
1176 return;
1177 }
1178
1179 if (out_start_pfn)
1180 *out_start_pfn = PFN_UP(r->base);
1181 if (out_end_pfn)
1182 *out_end_pfn = PFN_DOWN(r->base + r->size);
1183 if (out_nid)
1184 *out_nid = r->nid;
1185}
1186
1187/**
1188 * memblock_set_node - set node ID on memblock regions
1189 * @base: base of area to set node ID for
1190 * @size: size of area to set node ID for
1191 * @type: memblock type to set node ID for
1192 * @nid: node ID to set
1193 *
1194 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
1195 * Regions which cross the area boundaries are split as necessary.
1196 *
1197 * Return:
1198 * 0 on success, -errno on failure.
1199 */
1200int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1201 struct memblock_type *type, int nid)
1202{
1203 int start_rgn, end_rgn;
1204 int i, ret;
1205
1206 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1207 if (ret)
1208 return ret;
1209
1210 for (i = start_rgn; i < end_rgn; i++)
1211 memblock_set_region_node(&type->regions[i], nid);
1212
1213 memblock_merge_regions(type);
1214 return 0;
1215}
1216#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
David Brazdil0f672f62019-12-10 10:32:29 +00001217#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001218/**
David Brazdil0f672f62019-12-10 10:32:29 +00001219 * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1220 *
1221 * @idx: pointer to u64 loop variable
1222 * @zone: zone in which all of the memory blocks reside
1223 * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1224 * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1225 *
1226 * This function is meant to be a zone/pfn specific wrapper for the
1227 * for_each_mem_range type iterators. Specifically they are used in the
1228 * deferred memory init routines and as such we were duplicating much of
1229 * this logic throughout the code. So instead of having it in multiple
1230 * locations it seemed like it would make more sense to centralize this to
1231 * one new iterator that does everything they need.
1232 */
1233void __init_memblock
1234__next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1235 unsigned long *out_spfn, unsigned long *out_epfn)
1236{
1237 int zone_nid = zone_to_nid(zone);
1238 phys_addr_t spa, epa;
1239 int nid;
1240
1241 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1242 &memblock.memory, &memblock.reserved,
1243 &spa, &epa, &nid);
1244
1245 while (*idx != U64_MAX) {
1246 unsigned long epfn = PFN_DOWN(epa);
1247 unsigned long spfn = PFN_UP(spa);
1248
1249 /*
1250 * Verify the end is at least past the start of the zone and
1251 * that we have at least one PFN to initialize.
1252 */
1253 if (zone->zone_start_pfn < epfn && spfn < epfn) {
1254 /* if we went too far just stop searching */
1255 if (zone_end_pfn(zone) <= spfn) {
1256 *idx = U64_MAX;
1257 break;
1258 }
1259
1260 if (out_spfn)
1261 *out_spfn = max(zone->zone_start_pfn, spfn);
1262 if (out_epfn)
1263 *out_epfn = min(zone_end_pfn(zone), epfn);
1264
1265 return;
1266 }
1267
1268 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1269 &memblock.memory, &memblock.reserved,
1270 &spa, &epa, &nid);
1271 }
1272
1273 /* signal end of iteration */
1274 if (out_spfn)
1275 *out_spfn = ULONG_MAX;
1276 if (out_epfn)
1277 *out_epfn = 0;
1278}
1279
1280#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1281
1282/**
1283 * memblock_alloc_range_nid - allocate boot memory block
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001284 * @size: size of memory block to be allocated in bytes
1285 * @align: alignment of the region and block's size
David Brazdil0f672f62019-12-10 10:32:29 +00001286 * @start: the lower bound of the memory region to allocate (phys address)
1287 * @end: the upper bound of the memory region to allocate (phys address)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001288 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1289 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001290 * The allocation is performed from memory region limited by
David Brazdil0f672f62019-12-10 10:32:29 +00001291 * memblock.current_limit if @max_addr == %MEMBLOCK_ALLOC_ACCESSIBLE.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001292 *
David Brazdil0f672f62019-12-10 10:32:29 +00001293 * If the specified node can not hold the requested memory the
1294 * allocation falls back to any node in the system
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001295 *
David Brazdil0f672f62019-12-10 10:32:29 +00001296 * For systems with memory mirroring, the allocation is attempted first
1297 * from the regions with mirroring enabled and then retried from any
1298 * memory region.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001299 *
David Brazdil0f672f62019-12-10 10:32:29 +00001300 * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001301 * allocated boot memory block, so that it is never reported as leaks.
1302 *
1303 * Return:
David Brazdil0f672f62019-12-10 10:32:29 +00001304 * Physical address of allocated memory block on success, %0 on failure.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001305 */
David Brazdil0f672f62019-12-10 10:32:29 +00001306static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1307 phys_addr_t align, phys_addr_t start,
1308 phys_addr_t end, int nid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001309{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001310 enum memblock_flags flags = choose_memblock_flags();
David Brazdil0f672f62019-12-10 10:32:29 +00001311 phys_addr_t found;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001312
1313 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1314 nid = NUMA_NO_NODE;
1315
David Brazdil0f672f62019-12-10 10:32:29 +00001316 if (!align) {
1317 /* Can't use WARNs this early in boot on powerpc */
1318 dump_stack();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001319 align = SMP_CACHE_BYTES;
David Brazdil0f672f62019-12-10 10:32:29 +00001320 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001321
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001322again:
David Brazdil0f672f62019-12-10 10:32:29 +00001323 found = memblock_find_in_range_node(size, align, start, end, nid,
1324 flags);
1325 if (found && !memblock_reserve(found, size))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001326 goto done;
1327
1328 if (nid != NUMA_NO_NODE) {
David Brazdil0f672f62019-12-10 10:32:29 +00001329 found = memblock_find_in_range_node(size, align, start,
1330 end, NUMA_NO_NODE,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001331 flags);
David Brazdil0f672f62019-12-10 10:32:29 +00001332 if (found && !memblock_reserve(found, size))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001333 goto done;
1334 }
1335
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001336 if (flags & MEMBLOCK_MIRROR) {
1337 flags &= ~MEMBLOCK_MIRROR;
1338 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1339 &size);
1340 goto again;
1341 }
1342
David Brazdil0f672f62019-12-10 10:32:29 +00001343 return 0;
1344
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001345done:
David Brazdil0f672f62019-12-10 10:32:29 +00001346 /* Skip kmemleak for kasan_init() due to high volume. */
1347 if (end != MEMBLOCK_ALLOC_KASAN)
1348 /*
1349 * The min_count is set to 0 so that memblock allocated
1350 * blocks are never reported as leaks. This is because many
1351 * of these blocks are only referred via the physical
1352 * address which is not looked up by kmemleak.
1353 */
1354 kmemleak_alloc_phys(found, size, 0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001355
David Brazdil0f672f62019-12-10 10:32:29 +00001356 return found;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001357}
1358
1359/**
David Brazdil0f672f62019-12-10 10:32:29 +00001360 * memblock_phys_alloc_range - allocate a memory block inside specified range
1361 * @size: size of memory block to be allocated in bytes
1362 * @align: alignment of the region and block's size
1363 * @start: the lower bound of the memory region to allocate (physical address)
1364 * @end: the upper bound of the memory region to allocate (physical address)
1365 *
1366 * Allocate @size bytes in the between @start and @end.
1367 *
1368 * Return: physical address of the allocated memory block on success,
1369 * %0 on failure.
1370 */
1371phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
1372 phys_addr_t align,
1373 phys_addr_t start,
1374 phys_addr_t end)
1375{
1376 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE);
1377}
1378
1379/**
1380 * memblock_phys_alloc_try_nid - allocate a memory block from specified MUMA node
1381 * @size: size of memory block to be allocated in bytes
1382 * @align: alignment of the region and block's size
1383 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1384 *
1385 * Allocates memory block from the specified NUMA node. If the node
1386 * has no available memory, attempts to allocated from any node in the
1387 * system.
1388 *
1389 * Return: physical address of the allocated memory block on success,
1390 * %0 on failure.
1391 */
1392phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1393{
1394 return memblock_alloc_range_nid(size, align, 0,
1395 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
1396}
1397
1398/**
1399 * memblock_alloc_internal - allocate boot memory block
1400 * @size: size of memory block to be allocated in bytes
1401 * @align: alignment of the region and block's size
1402 * @min_addr: the lower bound of the memory region to allocate (phys address)
1403 * @max_addr: the upper bound of the memory region to allocate (phys address)
1404 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1405 *
1406 * Allocates memory block using memblock_alloc_range_nid() and
1407 * converts the returned physical address to virtual.
1408 *
1409 * The @min_addr limit is dropped if it can not be satisfied and the allocation
1410 * will fall back to memory below @min_addr. Other constraints, such
1411 * as node and mirrored memory will be handled again in
1412 * memblock_alloc_range_nid().
1413 *
1414 * Return:
1415 * Virtual address of allocated memory block on success, NULL on failure.
1416 */
1417static void * __init memblock_alloc_internal(
1418 phys_addr_t size, phys_addr_t align,
1419 phys_addr_t min_addr, phys_addr_t max_addr,
1420 int nid)
1421{
1422 phys_addr_t alloc;
1423
1424 /*
1425 * Detect any accidental use of these APIs after slab is ready, as at
1426 * this moment memblock may be deinitialized already and its
1427 * internal data may be destroyed (after execution of memblock_free_all)
1428 */
1429 if (WARN_ON_ONCE(slab_is_available()))
1430 return kzalloc_node(size, GFP_NOWAIT, nid);
1431
1432 if (max_addr > memblock.current_limit)
1433 max_addr = memblock.current_limit;
1434
1435 alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid);
1436
1437 /* retry allocation without lower limit */
1438 if (!alloc && min_addr)
1439 alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid);
1440
1441 if (!alloc)
1442 return NULL;
1443
1444 return phys_to_virt(alloc);
1445}
1446
1447/**
1448 * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001449 * memory and without panicking
1450 * @size: size of memory block to be allocated in bytes
1451 * @align: alignment of the region and block's size
1452 * @min_addr: the lower bound of the memory region from where the allocation
1453 * is preferred (phys address)
1454 * @max_addr: the upper bound of the memory region from where the allocation
David Brazdil0f672f62019-12-10 10:32:29 +00001455 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001456 * allocate only from memory limited by memblock.current_limit value
1457 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1458 *
1459 * Public function, provides additional debug information (including caller
1460 * info), if enabled. Does not zero allocated memory, does not panic if request
1461 * cannot be satisfied.
1462 *
1463 * Return:
1464 * Virtual address of allocated memory block on success, NULL on failure.
1465 */
David Brazdil0f672f62019-12-10 10:32:29 +00001466void * __init memblock_alloc_try_nid_raw(
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001467 phys_addr_t size, phys_addr_t align,
1468 phys_addr_t min_addr, phys_addr_t max_addr,
1469 int nid)
1470{
1471 void *ptr;
1472
David Brazdil0f672f62019-12-10 10:32:29 +00001473 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001474 __func__, (u64)size, (u64)align, nid, &min_addr,
1475 &max_addr, (void *)_RET_IP_);
1476
David Brazdil0f672f62019-12-10 10:32:29 +00001477 ptr = memblock_alloc_internal(size, align,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001478 min_addr, max_addr, nid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001479 if (ptr && size > 0)
David Brazdil0f672f62019-12-10 10:32:29 +00001480 page_init_poison(ptr, size);
1481
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001482 return ptr;
1483}
1484
1485/**
David Brazdil0f672f62019-12-10 10:32:29 +00001486 * memblock_alloc_try_nid - allocate boot memory block
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001487 * @size: size of memory block to be allocated in bytes
1488 * @align: alignment of the region and block's size
1489 * @min_addr: the lower bound of the memory region from where the allocation
1490 * is preferred (phys address)
1491 * @max_addr: the upper bound of the memory region from where the allocation
David Brazdil0f672f62019-12-10 10:32:29 +00001492 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001493 * allocate only from memory limited by memblock.current_limit value
1494 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1495 *
1496 * Public function, provides additional debug information (including caller
1497 * info), if enabled. This function zeroes the allocated memory.
1498 *
1499 * Return:
1500 * Virtual address of allocated memory block on success, NULL on failure.
1501 */
David Brazdil0f672f62019-12-10 10:32:29 +00001502void * __init memblock_alloc_try_nid(
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001503 phys_addr_t size, phys_addr_t align,
1504 phys_addr_t min_addr, phys_addr_t max_addr,
1505 int nid)
1506{
1507 void *ptr;
1508
David Brazdil0f672f62019-12-10 10:32:29 +00001509 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001510 __func__, (u64)size, (u64)align, nid, &min_addr,
1511 &max_addr, (void *)_RET_IP_);
David Brazdil0f672f62019-12-10 10:32:29 +00001512 ptr = memblock_alloc_internal(size, align,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001513 min_addr, max_addr, nid);
David Brazdil0f672f62019-12-10 10:32:29 +00001514 if (ptr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001515 memset(ptr, 0, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001516
David Brazdil0f672f62019-12-10 10:32:29 +00001517 return ptr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001518}
1519
1520/**
David Brazdil0f672f62019-12-10 10:32:29 +00001521 * __memblock_free_late - free pages directly to buddy allocator
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001522 * @base: phys starting address of the boot memory block
1523 * @size: size of the boot memory block in bytes
1524 *
David Brazdil0f672f62019-12-10 10:32:29 +00001525 * This is only useful when the memblock allocator has already been torn
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001526 * down, but we are still initializing the system. Pages are released directly
David Brazdil0f672f62019-12-10 10:32:29 +00001527 * to the buddy allocator.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001528 */
1529void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1530{
1531 phys_addr_t cursor, end;
1532
1533 end = base + size - 1;
David Brazdil0f672f62019-12-10 10:32:29 +00001534 memblock_dbg("%s: [%pa-%pa] %pS\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001535 __func__, &base, &end, (void *)_RET_IP_);
1536 kmemleak_free_part_phys(base, size);
1537 cursor = PFN_UP(base);
1538 end = PFN_DOWN(base + size);
1539
1540 for (; cursor < end; cursor++) {
David Brazdil0f672f62019-12-10 10:32:29 +00001541 memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1542 totalram_pages_inc();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001543 }
1544}
1545
1546/*
1547 * Remaining API functions
1548 */
1549
1550phys_addr_t __init_memblock memblock_phys_mem_size(void)
1551{
1552 return memblock.memory.total_size;
1553}
1554
1555phys_addr_t __init_memblock memblock_reserved_size(void)
1556{
1557 return memblock.reserved.total_size;
1558}
1559
1560phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1561{
1562 unsigned long pages = 0;
1563 struct memblock_region *r;
1564 unsigned long start_pfn, end_pfn;
1565
1566 for_each_memblock(memory, r) {
1567 start_pfn = memblock_region_memory_base_pfn(r);
1568 end_pfn = memblock_region_memory_end_pfn(r);
1569 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1570 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1571 pages += end_pfn - start_pfn;
1572 }
1573
1574 return PFN_PHYS(pages);
1575}
1576
1577/* lowest address */
1578phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1579{
1580 return memblock.memory.regions[0].base;
1581}
1582
1583phys_addr_t __init_memblock memblock_end_of_DRAM(void)
1584{
1585 int idx = memblock.memory.cnt - 1;
1586
1587 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
1588}
1589
1590static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
1591{
1592 phys_addr_t max_addr = PHYS_ADDR_MAX;
1593 struct memblock_region *r;
1594
1595 /*
1596 * translate the memory @limit size into the max address within one of
1597 * the memory memblock regions, if the @limit exceeds the total size
1598 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1599 */
1600 for_each_memblock(memory, r) {
1601 if (limit <= r->size) {
1602 max_addr = r->base + limit;
1603 break;
1604 }
1605 limit -= r->size;
1606 }
1607
1608 return max_addr;
1609}
1610
1611void __init memblock_enforce_memory_limit(phys_addr_t limit)
1612{
1613 phys_addr_t max_addr = PHYS_ADDR_MAX;
1614
1615 if (!limit)
1616 return;
1617
1618 max_addr = __find_max_addr(limit);
1619
1620 /* @limit exceeds the total size of the memory, do nothing */
1621 if (max_addr == PHYS_ADDR_MAX)
1622 return;
1623
1624 /* truncate both memory and reserved regions */
1625 memblock_remove_range(&memblock.memory, max_addr,
1626 PHYS_ADDR_MAX);
1627 memblock_remove_range(&memblock.reserved, max_addr,
1628 PHYS_ADDR_MAX);
1629}
1630
1631void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1632{
1633 int start_rgn, end_rgn;
1634 int i, ret;
1635
1636 if (!size)
1637 return;
1638
1639 ret = memblock_isolate_range(&memblock.memory, base, size,
1640 &start_rgn, &end_rgn);
1641 if (ret)
1642 return;
1643
1644 /* remove all the MAP regions */
1645 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1646 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1647 memblock_remove_region(&memblock.memory, i);
1648
1649 for (i = start_rgn - 1; i >= 0; i--)
1650 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1651 memblock_remove_region(&memblock.memory, i);
1652
1653 /* truncate the reserved regions */
1654 memblock_remove_range(&memblock.reserved, 0, base);
1655 memblock_remove_range(&memblock.reserved,
1656 base + size, PHYS_ADDR_MAX);
1657}
1658
1659void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1660{
1661 phys_addr_t max_addr;
1662
1663 if (!limit)
1664 return;
1665
1666 max_addr = __find_max_addr(limit);
1667
1668 /* @limit exceeds the total size of the memory, do nothing */
1669 if (max_addr == PHYS_ADDR_MAX)
1670 return;
1671
1672 memblock_cap_memory_range(0, max_addr);
1673}
1674
1675static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
1676{
1677 unsigned int left = 0, right = type->cnt;
1678
1679 do {
1680 unsigned int mid = (right + left) / 2;
1681
1682 if (addr < type->regions[mid].base)
1683 right = mid;
1684 else if (addr >= (type->regions[mid].base +
1685 type->regions[mid].size))
1686 left = mid + 1;
1687 else
1688 return mid;
1689 } while (left < right);
1690 return -1;
1691}
1692
David Brazdil0f672f62019-12-10 10:32:29 +00001693bool __init_memblock memblock_is_reserved(phys_addr_t addr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001694{
1695 return memblock_search(&memblock.reserved, addr) != -1;
1696}
1697
1698bool __init_memblock memblock_is_memory(phys_addr_t addr)
1699{
1700 return memblock_search(&memblock.memory, addr) != -1;
1701}
1702
1703bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1704{
1705 int i = memblock_search(&memblock.memory, addr);
1706
1707 if (i == -1)
1708 return false;
1709 return !memblock_is_nomap(&memblock.memory.regions[i]);
1710}
1711
1712#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1713int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1714 unsigned long *start_pfn, unsigned long *end_pfn)
1715{
1716 struct memblock_type *type = &memblock.memory;
1717 int mid = memblock_search(type, PFN_PHYS(pfn));
1718
1719 if (mid == -1)
1720 return -1;
1721
1722 *start_pfn = PFN_DOWN(type->regions[mid].base);
1723 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1724
1725 return type->regions[mid].nid;
1726}
1727#endif
1728
1729/**
1730 * memblock_is_region_memory - check if a region is a subset of memory
1731 * @base: base of region to check
1732 * @size: size of region to check
1733 *
1734 * Check if the region [@base, @base + @size) is a subset of a memory block.
1735 *
1736 * Return:
1737 * 0 if false, non-zero if true
1738 */
1739bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
1740{
1741 int idx = memblock_search(&memblock.memory, base);
1742 phys_addr_t end = base + memblock_cap_size(base, &size);
1743
1744 if (idx == -1)
1745 return false;
1746 return (memblock.memory.regions[idx].base +
1747 memblock.memory.regions[idx].size) >= end;
1748}
1749
1750/**
1751 * memblock_is_region_reserved - check if a region intersects reserved memory
1752 * @base: base of region to check
1753 * @size: size of region to check
1754 *
1755 * Check if the region [@base, @base + @size) intersects a reserved
1756 * memory block.
1757 *
1758 * Return:
1759 * True if they intersect, false if not.
1760 */
1761bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
1762{
1763 memblock_cap_size(base, &size);
1764 return memblock_overlaps_region(&memblock.reserved, base, size);
1765}
1766
1767void __init_memblock memblock_trim_memory(phys_addr_t align)
1768{
1769 phys_addr_t start, end, orig_start, orig_end;
1770 struct memblock_region *r;
1771
1772 for_each_memblock(memory, r) {
1773 orig_start = r->base;
1774 orig_end = r->base + r->size;
1775 start = round_up(orig_start, align);
1776 end = round_down(orig_end, align);
1777
1778 if (start == orig_start && end == orig_end)
1779 continue;
1780
1781 if (start < end) {
1782 r->base = start;
1783 r->size = end - start;
1784 } else {
1785 memblock_remove_region(&memblock.memory,
1786 r - memblock.memory.regions);
1787 r--;
1788 }
1789 }
1790}
1791
1792void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1793{
1794 memblock.current_limit = limit;
1795}
1796
1797phys_addr_t __init_memblock memblock_get_current_limit(void)
1798{
1799 return memblock.current_limit;
1800}
1801
1802static void __init_memblock memblock_dump(struct memblock_type *type)
1803{
1804 phys_addr_t base, end, size;
1805 enum memblock_flags flags;
1806 int idx;
1807 struct memblock_region *rgn;
1808
1809 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
1810
1811 for_each_memblock_type(idx, type, rgn) {
1812 char nid_buf[32] = "";
1813
1814 base = rgn->base;
1815 size = rgn->size;
1816 end = base + size - 1;
1817 flags = rgn->flags;
1818#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1819 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1820 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1821 memblock_get_region_node(rgn));
1822#endif
1823 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
1824 type->name, idx, &base, &end, &size, nid_buf, flags);
1825 }
1826}
1827
1828void __init_memblock __memblock_dump_all(void)
1829{
1830 pr_info("MEMBLOCK configuration:\n");
1831 pr_info(" memory size = %pa reserved size = %pa\n",
1832 &memblock.memory.total_size,
1833 &memblock.reserved.total_size);
1834
1835 memblock_dump(&memblock.memory);
1836 memblock_dump(&memblock.reserved);
1837#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
1838 memblock_dump(&memblock.physmem);
1839#endif
1840}
1841
1842void __init memblock_allow_resize(void)
1843{
1844 memblock_can_resize = 1;
1845}
1846
1847static int __init early_memblock(char *p)
1848{
1849 if (p && strstr(p, "debug"))
1850 memblock_debug = 1;
1851 return 0;
1852}
1853early_param("memblock", early_memblock);
1854
David Brazdil0f672f62019-12-10 10:32:29 +00001855static void __init __free_pages_memory(unsigned long start, unsigned long end)
1856{
1857 int order;
1858
1859 while (start < end) {
1860 order = min(MAX_ORDER - 1UL, __ffs(start));
1861
1862 while (start + (1UL << order) > end)
1863 order--;
1864
1865 memblock_free_pages(pfn_to_page(start), start, order);
1866
1867 start += (1UL << order);
1868 }
1869}
1870
1871static unsigned long __init __free_memory_core(phys_addr_t start,
1872 phys_addr_t end)
1873{
1874 unsigned long start_pfn = PFN_UP(start);
1875 unsigned long end_pfn = min_t(unsigned long,
1876 PFN_DOWN(end), max_low_pfn);
1877
1878 if (start_pfn >= end_pfn)
1879 return 0;
1880
1881 __free_pages_memory(start_pfn, end_pfn);
1882
1883 return end_pfn - start_pfn;
1884}
1885
1886static unsigned long __init free_low_memory_core_early(void)
1887{
1888 unsigned long count = 0;
1889 phys_addr_t start, end;
1890 u64 i;
1891
1892 memblock_clear_hotplug(0, -1);
1893
1894 for_each_reserved_mem_region(i, &start, &end)
1895 reserve_bootmem_region(start, end);
1896
1897 /*
1898 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
1899 * because in some case like Node0 doesn't have RAM installed
1900 * low ram will be on Node1
1901 */
1902 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
1903 NULL)
1904 count += __free_memory_core(start, end);
1905
1906 return count;
1907}
1908
1909static int reset_managed_pages_done __initdata;
1910
1911void reset_node_managed_pages(pg_data_t *pgdat)
1912{
1913 struct zone *z;
1914
1915 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1916 atomic_long_set(&z->managed_pages, 0);
1917}
1918
1919void __init reset_all_zones_managed_pages(void)
1920{
1921 struct pglist_data *pgdat;
1922
1923 if (reset_managed_pages_done)
1924 return;
1925
1926 for_each_online_pgdat(pgdat)
1927 reset_node_managed_pages(pgdat);
1928
1929 reset_managed_pages_done = 1;
1930}
1931
1932/**
1933 * memblock_free_all - release free pages to the buddy allocator
1934 *
1935 * Return: the number of pages actually released.
1936 */
1937unsigned long __init memblock_free_all(void)
1938{
1939 unsigned long pages;
1940
1941 reset_all_zones_managed_pages();
1942
1943 pages = free_low_memory_core_early();
1944 totalram_pages_add(pages);
1945
1946 return pages;
1947}
1948
1949#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001950
1951static int memblock_debug_show(struct seq_file *m, void *private)
1952{
1953 struct memblock_type *type = m->private;
1954 struct memblock_region *reg;
1955 int i;
1956 phys_addr_t end;
1957
1958 for (i = 0; i < type->cnt; i++) {
1959 reg = &type->regions[i];
1960 end = reg->base + reg->size - 1;
1961
1962 seq_printf(m, "%4d: ", i);
1963 seq_printf(m, "%pa..%pa\n", &reg->base, &end);
1964 }
1965 return 0;
1966}
1967DEFINE_SHOW_ATTRIBUTE(memblock_debug);
1968
1969static int __init memblock_init_debugfs(void)
1970{
1971 struct dentry *root = debugfs_create_dir("memblock", NULL);
David Brazdil0f672f62019-12-10 10:32:29 +00001972
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001973 debugfs_create_file("memory", 0444, root,
1974 &memblock.memory, &memblock_debug_fops);
1975 debugfs_create_file("reserved", 0444, root,
1976 &memblock.reserved, &memblock_debug_fops);
1977#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
1978 debugfs_create_file("physmem", 0444, root,
1979 &memblock.physmem, &memblock_debug_fops);
1980#endif
1981
1982 return 0;
1983}
1984__initcall(memblock_init_debugfs);
1985
1986#endif /* CONFIG_DEBUG_FS */