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