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