]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - mm/memblock.c
acpi, memory-hotplug: support getting hotplug info from SRAT
[thirdparty/kernel/linux.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
fe091c20
TH
23static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
24static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
25
26struct memblock memblock __initdata_memblock = {
27 .memory.regions = memblock_memory_init_regions,
28 .memory.cnt = 1, /* empty dummy entry */
29 .memory.max = INIT_MEMBLOCK_REGIONS,
30
31 .reserved.regions = memblock_reserved_init_regions,
32 .reserved.cnt = 1, /* empty dummy entry */
33 .reserved.max = INIT_MEMBLOCK_REGIONS,
34
35 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
36};
95f72d1e 37
10d06439 38int memblock_debug __initdata_memblock;
1aadc056 39static int memblock_can_resize __initdata_memblock;
181eb394
GS
40static int memblock_memory_in_slab __initdata_memblock = 0;
41static int memblock_reserved_in_slab __initdata_memblock = 0;
95f72d1e 42
142b45a7 43/* inline so we don't get a warning when pr_debug is compiled out */
c2233116
RP
44static __init_memblock const char *
45memblock_type_name(struct memblock_type *type)
142b45a7
BH
46{
47 if (type == &memblock.memory)
48 return "memory";
49 else if (type == &memblock.reserved)
50 return "reserved";
51 else
52 return "unknown";
53}
54
eb18f1b5
TH
55/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
56static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
57{
58 return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
59}
60
6ed311b2
BH
61/*
62 * Address comparison utilities
63 */
10d06439 64static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
2898cc4c 65 phys_addr_t base2, phys_addr_t size2)
95f72d1e
YL
66{
67 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
68}
69
2d7d3eb2
HS
70static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
71 phys_addr_t base, phys_addr_t size)
6ed311b2
BH
72{
73 unsigned long i;
74
75 for (i = 0; i < type->cnt; i++) {
76 phys_addr_t rgnbase = type->regions[i].base;
77 phys_addr_t rgnsize = type->regions[i].size;
78 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
79 break;
80 }
81
82 return (i < type->cnt) ? i : -1;
83}
84
7bd0b0f0
TH
85/**
86 * memblock_find_in_range_node - find free area in given range and node
87 * @start: start of candidate range
88 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
89 * @size: size of free area to find
90 * @align: alignment of free area to find
91 * @nid: nid of the free area to find, %MAX_NUMNODES for any node
92 *
93 * Find @size free area aligned to @align in the specified range and node.
94 *
95 * RETURNS:
96 * Found address on success, %0 on failure.
6ed311b2 97 */
7bd0b0f0
TH
98phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start,
99 phys_addr_t end, phys_addr_t size,
100 phys_addr_t align, int nid)
6ed311b2 101{
7bd0b0f0
TH
102 phys_addr_t this_start, this_end, cand;
103 u64 i;
fb06bc8e 104 int curr = movablemem_map.nr_map - 1;
6ed311b2 105
7bd0b0f0
TH
106 /* pump up @end */
107 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
108 end = memblock.current_limit;
f1af98c7 109
5d53cb27
TH
110 /* avoid allocating the first page */
111 start = max_t(phys_addr_t, start, PAGE_SIZE);
7bd0b0f0 112 end = max(start, end);
f1af98c7 113
7bd0b0f0
TH
114 for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
115 this_start = clamp(this_start, start, end);
116 this_end = clamp(this_end, start, end);
6ed311b2 117
fb06bc8e
TC
118restart:
119 if (this_end <= this_start || this_end < size)
5d53cb27
TH
120 continue;
121
fb06bc8e
TC
122 for (; curr >= 0; curr--) {
123 if ((movablemem_map.map[curr].start_pfn << PAGE_SHIFT)
124 < this_end)
125 break;
126 }
127
7bd0b0f0 128 cand = round_down(this_end - size, align);
fb06bc8e
TC
129 if (curr >= 0 &&
130 cand < movablemem_map.map[curr].end_pfn << PAGE_SHIFT) {
131 this_end = movablemem_map.map[curr].start_pfn
132 << PAGE_SHIFT;
133 goto restart;
134 }
135
7bd0b0f0
TH
136 if (cand >= this_start)
137 return cand;
138 }
fb06bc8e 139
1f5026a7 140 return 0;
6ed311b2
BH
141}
142
7bd0b0f0
TH
143/**
144 * memblock_find_in_range - find free area in given range
145 * @start: start of candidate range
146 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
147 * @size: size of free area to find
148 * @align: alignment of free area to find
149 *
150 * Find @size free area aligned to @align in the specified range.
151 *
152 * RETURNS:
153 * Found address on success, %0 on failure.
fc769a8e 154 */
7bd0b0f0
TH
155phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
156 phys_addr_t end, phys_addr_t size,
157 phys_addr_t align)
6ed311b2 158{
7bd0b0f0
TH
159 return memblock_find_in_range_node(start, end, size, align,
160 MAX_NUMNODES);
6ed311b2
BH
161}
162
10d06439 163static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
95f72d1e 164{
1440c4e2 165 type->total_size -= type->regions[r].size;
7c0caeb8
TH
166 memmove(&type->regions[r], &type->regions[r + 1],
167 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
e3239ff9 168 type->cnt--;
95f72d1e 169
8f7a6605
BH
170 /* Special case for empty arrays */
171 if (type->cnt == 0) {
1440c4e2 172 WARN_ON(type->total_size != 0);
8f7a6605
BH
173 type->cnt = 1;
174 type->regions[0].base = 0;
175 type->regions[0].size = 0;
7c0caeb8 176 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
8f7a6605 177 }
95f72d1e
YL
178}
179
29f67386
YL
180phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
181 phys_addr_t *addr)
182{
183 if (memblock.reserved.regions == memblock_reserved_init_regions)
184 return 0;
185
186 *addr = __pa(memblock.reserved.regions);
187
188 return PAGE_ALIGN(sizeof(struct memblock_region) *
189 memblock.reserved.max);
190}
191
48c3b583
GP
192/**
193 * memblock_double_array - double the size of the memblock regions array
194 * @type: memblock type of the regions array being doubled
195 * @new_area_start: starting address of memory range to avoid overlap with
196 * @new_area_size: size of memory range to avoid overlap with
197 *
198 * Double the size of the @type regions array. If memblock is being used to
199 * allocate memory for a new reserved regions array and there is a previously
200 * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
201 * waiting to be reserved, ensure the memory used by the new array does
202 * not overlap.
203 *
204 * RETURNS:
205 * 0 on success, -1 on failure.
206 */
207static int __init_memblock memblock_double_array(struct memblock_type *type,
208 phys_addr_t new_area_start,
209 phys_addr_t new_area_size)
142b45a7
BH
210{
211 struct memblock_region *new_array, *old_array;
29f67386 212 phys_addr_t old_alloc_size, new_alloc_size;
142b45a7
BH
213 phys_addr_t old_size, new_size, addr;
214 int use_slab = slab_is_available();
181eb394 215 int *in_slab;
142b45a7
BH
216
217 /* We don't allow resizing until we know about the reserved regions
218 * of memory that aren't suitable for allocation
219 */
220 if (!memblock_can_resize)
221 return -1;
222
142b45a7
BH
223 /* Calculate new doubled size */
224 old_size = type->max * sizeof(struct memblock_region);
225 new_size = old_size << 1;
29f67386
YL
226 /*
227 * We need to allocated new one align to PAGE_SIZE,
228 * so we can free them completely later.
229 */
230 old_alloc_size = PAGE_ALIGN(old_size);
231 new_alloc_size = PAGE_ALIGN(new_size);
142b45a7 232
181eb394
GS
233 /* Retrieve the slab flag */
234 if (type == &memblock.memory)
235 in_slab = &memblock_memory_in_slab;
236 else
237 in_slab = &memblock_reserved_in_slab;
238
142b45a7
BH
239 /* Try to find some space for it.
240 *
241 * WARNING: We assume that either slab_is_available() and we use it or
fd07383b
AM
242 * we use MEMBLOCK for allocations. That means that this is unsafe to
243 * use when bootmem is currently active (unless bootmem itself is
244 * implemented on top of MEMBLOCK which isn't the case yet)
142b45a7
BH
245 *
246 * This should however not be an issue for now, as we currently only
fd07383b
AM
247 * call into MEMBLOCK while it's still active, or much later when slab
248 * is active for memory hotplug operations
142b45a7
BH
249 */
250 if (use_slab) {
251 new_array = kmalloc(new_size, GFP_KERNEL);
1f5026a7 252 addr = new_array ? __pa(new_array) : 0;
4e2f0775 253 } else {
48c3b583
GP
254 /* only exclude range when trying to double reserved.regions */
255 if (type != &memblock.reserved)
256 new_area_start = new_area_size = 0;
257
258 addr = memblock_find_in_range(new_area_start + new_area_size,
259 memblock.current_limit,
29f67386 260 new_alloc_size, PAGE_SIZE);
48c3b583
GP
261 if (!addr && new_area_size)
262 addr = memblock_find_in_range(0,
fd07383b
AM
263 min(new_area_start, memblock.current_limit),
264 new_alloc_size, PAGE_SIZE);
48c3b583 265
15674868 266 new_array = addr ? __va(addr) : NULL;
4e2f0775 267 }
1f5026a7 268 if (!addr) {
142b45a7
BH
269 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
270 memblock_type_name(type), type->max, type->max * 2);
271 return -1;
272 }
142b45a7 273
fd07383b
AM
274 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
275 memblock_type_name(type), type->max * 2, (u64)addr,
276 (u64)addr + new_size - 1);
ea9e4376 277
fd07383b
AM
278 /*
279 * Found space, we now need to move the array over before we add the
280 * reserved region since it may be our reserved array itself that is
281 * full.
142b45a7
BH
282 */
283 memcpy(new_array, type->regions, old_size);
284 memset(new_array + type->max, 0, old_size);
285 old_array = type->regions;
286 type->regions = new_array;
287 type->max <<= 1;
288
fd07383b 289 /* Free old array. We needn't free it if the array is the static one */
181eb394
GS
290 if (*in_slab)
291 kfree(old_array);
292 else if (old_array != memblock_memory_init_regions &&
293 old_array != memblock_reserved_init_regions)
29f67386 294 memblock_free(__pa(old_array), old_alloc_size);
142b45a7 295
fd07383b
AM
296 /*
297 * Reserve the new array if that comes from the memblock. Otherwise, we
298 * needn't do it
181eb394
GS
299 */
300 if (!use_slab)
29f67386 301 BUG_ON(memblock_reserve(addr, new_alloc_size));
181eb394
GS
302
303 /* Update slab flag */
304 *in_slab = use_slab;
305
142b45a7
BH
306 return 0;
307}
308
784656f9
TH
309/**
310 * memblock_merge_regions - merge neighboring compatible regions
311 * @type: memblock type to scan
312 *
313 * Scan @type and merge neighboring compatible regions.
314 */
315static void __init_memblock memblock_merge_regions(struct memblock_type *type)
95f72d1e 316{
784656f9 317 int i = 0;
95f72d1e 318
784656f9
TH
319 /* cnt never goes below 1 */
320 while (i < type->cnt - 1) {
321 struct memblock_region *this = &type->regions[i];
322 struct memblock_region *next = &type->regions[i + 1];
95f72d1e 323
7c0caeb8
TH
324 if (this->base + this->size != next->base ||
325 memblock_get_region_node(this) !=
326 memblock_get_region_node(next)) {
784656f9
TH
327 BUG_ON(this->base + this->size > next->base);
328 i++;
329 continue;
8f7a6605
BH
330 }
331
784656f9 332 this->size += next->size;
c0232ae8
LF
333 /* move forward from next + 1, index of which is i + 2 */
334 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
784656f9 335 type->cnt--;
95f72d1e 336 }
784656f9 337}
95f72d1e 338
784656f9
TH
339/**
340 * memblock_insert_region - insert new memblock region
341 * @type: memblock type to insert into
342 * @idx: index for the insertion point
343 * @base: base address of the new region
344 * @size: size of the new region
345 *
346 * Insert new memblock region [@base,@base+@size) into @type at @idx.
347 * @type must already have extra room to accomodate the new region.
348 */
349static void __init_memblock memblock_insert_region(struct memblock_type *type,
350 int idx, phys_addr_t base,
7c0caeb8 351 phys_addr_t size, int nid)
784656f9
TH
352{
353 struct memblock_region *rgn = &type->regions[idx];
354
355 BUG_ON(type->cnt >= type->max);
356 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
357 rgn->base = base;
358 rgn->size = size;
7c0caeb8 359 memblock_set_region_node(rgn, nid);
784656f9 360 type->cnt++;
1440c4e2 361 type->total_size += size;
784656f9
TH
362}
363
364/**
365 * memblock_add_region - add new memblock region
366 * @type: memblock type to add new region into
367 * @base: base address of the new region
368 * @size: size of the new region
7fb0bc3f 369 * @nid: nid of the new region
784656f9
TH
370 *
371 * Add new memblock region [@base,@base+@size) into @type. The new region
372 * is allowed to overlap with existing ones - overlaps don't affect already
373 * existing regions. @type is guaranteed to be minimal (all neighbouring
374 * compatible regions are merged) after the addition.
375 *
376 * RETURNS:
377 * 0 on success, -errno on failure.
378 */
581adcbe 379static int __init_memblock memblock_add_region(struct memblock_type *type,
7fb0bc3f 380 phys_addr_t base, phys_addr_t size, int nid)
784656f9
TH
381{
382 bool insert = false;
eb18f1b5
TH
383 phys_addr_t obase = base;
384 phys_addr_t end = base + memblock_cap_size(base, &size);
784656f9
TH
385 int i, nr_new;
386
b3dc627c
TH
387 if (!size)
388 return 0;
389
784656f9
TH
390 /* special case for empty array */
391 if (type->regions[0].size == 0) {
1440c4e2 392 WARN_ON(type->cnt != 1 || type->total_size);
8f7a6605
BH
393 type->regions[0].base = base;
394 type->regions[0].size = size;
7fb0bc3f 395 memblock_set_region_node(&type->regions[0], nid);
1440c4e2 396 type->total_size = size;
8f7a6605 397 return 0;
95f72d1e 398 }
784656f9
TH
399repeat:
400 /*
401 * The following is executed twice. Once with %false @insert and
402 * then with %true. The first counts the number of regions needed
403 * to accomodate the new area. The second actually inserts them.
142b45a7 404 */
784656f9
TH
405 base = obase;
406 nr_new = 0;
95f72d1e 407
784656f9
TH
408 for (i = 0; i < type->cnt; i++) {
409 struct memblock_region *rgn = &type->regions[i];
410 phys_addr_t rbase = rgn->base;
411 phys_addr_t rend = rbase + rgn->size;
412
413 if (rbase >= end)
95f72d1e 414 break;
784656f9
TH
415 if (rend <= base)
416 continue;
417 /*
418 * @rgn overlaps. If it separates the lower part of new
419 * area, insert that portion.
420 */
421 if (rbase > base) {
422 nr_new++;
423 if (insert)
424 memblock_insert_region(type, i++, base,
7fb0bc3f 425 rbase - base, nid);
95f72d1e 426 }
784656f9
TH
427 /* area below @rend is dealt with, forget about it */
428 base = min(rend, end);
95f72d1e 429 }
784656f9
TH
430
431 /* insert the remaining portion */
432 if (base < end) {
433 nr_new++;
434 if (insert)
7fb0bc3f 435 memblock_insert_region(type, i, base, end - base, nid);
95f72d1e 436 }
95f72d1e 437
784656f9
TH
438 /*
439 * If this was the first round, resize array and repeat for actual
440 * insertions; otherwise, merge and return.
142b45a7 441 */
784656f9
TH
442 if (!insert) {
443 while (type->cnt + nr_new > type->max)
48c3b583 444 if (memblock_double_array(type, obase, size) < 0)
784656f9
TH
445 return -ENOMEM;
446 insert = true;
447 goto repeat;
448 } else {
449 memblock_merge_regions(type);
450 return 0;
142b45a7 451 }
95f72d1e
YL
452}
453
7fb0bc3f
TH
454int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
455 int nid)
456{
457 return memblock_add_region(&memblock.memory, base, size, nid);
458}
459
581adcbe 460int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
95f72d1e 461{
7fb0bc3f 462 return memblock_add_region(&memblock.memory, base, size, MAX_NUMNODES);
95f72d1e
YL
463}
464
6a9ceb31
TH
465/**
466 * memblock_isolate_range - isolate given range into disjoint memblocks
467 * @type: memblock type to isolate range for
468 * @base: base of range to isolate
469 * @size: size of range to isolate
470 * @start_rgn: out parameter for the start of isolated region
471 * @end_rgn: out parameter for the end of isolated region
472 *
473 * Walk @type and ensure that regions don't cross the boundaries defined by
474 * [@base,@base+@size). Crossing regions are split at the boundaries,
475 * which may create at most two more regions. The index of the first
476 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
477 *
478 * RETURNS:
479 * 0 on success, -errno on failure.
480 */
481static int __init_memblock memblock_isolate_range(struct memblock_type *type,
482 phys_addr_t base, phys_addr_t size,
483 int *start_rgn, int *end_rgn)
484{
eb18f1b5 485 phys_addr_t end = base + memblock_cap_size(base, &size);
6a9ceb31
TH
486 int i;
487
488 *start_rgn = *end_rgn = 0;
489
b3dc627c
TH
490 if (!size)
491 return 0;
492
6a9ceb31
TH
493 /* we'll create at most two more regions */
494 while (type->cnt + 2 > type->max)
48c3b583 495 if (memblock_double_array(type, base, size) < 0)
6a9ceb31
TH
496 return -ENOMEM;
497
498 for (i = 0; i < type->cnt; i++) {
499 struct memblock_region *rgn = &type->regions[i];
500 phys_addr_t rbase = rgn->base;
501 phys_addr_t rend = rbase + rgn->size;
502
503 if (rbase >= end)
504 break;
505 if (rend <= base)
506 continue;
507
508 if (rbase < base) {
509 /*
510 * @rgn intersects from below. Split and continue
511 * to process the next region - the new top half.
512 */
513 rgn->base = base;
1440c4e2
TH
514 rgn->size -= base - rbase;
515 type->total_size -= base - rbase;
6a9ceb31 516 memblock_insert_region(type, i, rbase, base - rbase,
71936180 517 memblock_get_region_node(rgn));
6a9ceb31
TH
518 } else if (rend > end) {
519 /*
520 * @rgn intersects from above. Split and redo the
521 * current region - the new bottom half.
522 */
523 rgn->base = end;
1440c4e2
TH
524 rgn->size -= end - rbase;
525 type->total_size -= end - rbase;
6a9ceb31 526 memblock_insert_region(type, i--, rbase, end - rbase,
71936180 527 memblock_get_region_node(rgn));
6a9ceb31
TH
528 } else {
529 /* @rgn is fully contained, record it */
530 if (!*end_rgn)
531 *start_rgn = i;
532 *end_rgn = i + 1;
533 }
534 }
535
536 return 0;
537}
6a9ceb31 538
581adcbe
TH
539static int __init_memblock __memblock_remove(struct memblock_type *type,
540 phys_addr_t base, phys_addr_t size)
95f72d1e 541{
71936180
TH
542 int start_rgn, end_rgn;
543 int i, ret;
95f72d1e 544
71936180
TH
545 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
546 if (ret)
547 return ret;
95f72d1e 548
71936180
TH
549 for (i = end_rgn - 1; i >= start_rgn; i--)
550 memblock_remove_region(type, i);
8f7a6605 551 return 0;
95f72d1e
YL
552}
553
581adcbe 554int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
95f72d1e
YL
555{
556 return __memblock_remove(&memblock.memory, base, size);
557}
558
581adcbe 559int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
95f72d1e 560{
24aa0788 561 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
a150439c
PA
562 (unsigned long long)base,
563 (unsigned long long)base + size,
564 (void *)_RET_IP_);
24aa0788 565
95f72d1e
YL
566 return __memblock_remove(&memblock.reserved, base, size);
567}
568
581adcbe 569int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
95f72d1e 570{
e3239ff9 571 struct memblock_type *_rgn = &memblock.reserved;
95f72d1e 572
24aa0788 573 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
a150439c
PA
574 (unsigned long long)base,
575 (unsigned long long)base + size,
576 (void *)_RET_IP_);
95f72d1e 577
7fb0bc3f 578 return memblock_add_region(_rgn, base, size, MAX_NUMNODES);
95f72d1e
YL
579}
580
35fd0808
TH
581/**
582 * __next_free_mem_range - next function for for_each_free_mem_range()
583 * @idx: pointer to u64 loop variable
584 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
dad7557e
WL
585 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
586 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
587 * @out_nid: ptr to int for nid of the range, can be %NULL
35fd0808
TH
588 *
589 * Find the first free area from *@idx which matches @nid, fill the out
590 * parameters, and update *@idx for the next iteration. The lower 32bit of
591 * *@idx contains index into memory region and the upper 32bit indexes the
592 * areas before each reserved region. For example, if reserved regions
593 * look like the following,
594 *
595 * 0:[0-16), 1:[32-48), 2:[128-130)
596 *
597 * The upper 32bit indexes the following regions.
598 *
599 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
600 *
601 * As both region arrays are sorted, the function advances the two indices
602 * in lockstep and returns each intersection.
603 */
604void __init_memblock __next_free_mem_range(u64 *idx, int nid,
605 phys_addr_t *out_start,
606 phys_addr_t *out_end, int *out_nid)
607{
608 struct memblock_type *mem = &memblock.memory;
609 struct memblock_type *rsv = &memblock.reserved;
610 int mi = *idx & 0xffffffff;
611 int ri = *idx >> 32;
612
613 for ( ; mi < mem->cnt; mi++) {
614 struct memblock_region *m = &mem->regions[mi];
615 phys_addr_t m_start = m->base;
616 phys_addr_t m_end = m->base + m->size;
617
618 /* only memory regions are associated with nodes, check it */
619 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
620 continue;
621
622 /* scan areas before each reservation for intersection */
623 for ( ; ri < rsv->cnt + 1; ri++) {
624 struct memblock_region *r = &rsv->regions[ri];
625 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
626 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
627
628 /* if ri advanced past mi, break out to advance mi */
629 if (r_start >= m_end)
630 break;
631 /* if the two regions intersect, we're done */
632 if (m_start < r_end) {
633 if (out_start)
634 *out_start = max(m_start, r_start);
635 if (out_end)
636 *out_end = min(m_end, r_end);
637 if (out_nid)
638 *out_nid = memblock_get_region_node(m);
639 /*
640 * The region which ends first is advanced
641 * for the next iteration.
642 */
643 if (m_end <= r_end)
644 mi++;
645 else
646 ri++;
647 *idx = (u32)mi | (u64)ri << 32;
648 return;
649 }
650 }
651 }
652
653 /* signal end of iteration */
654 *idx = ULLONG_MAX;
655}
656
7bd0b0f0
TH
657/**
658 * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
659 * @idx: pointer to u64 loop variable
660 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
dad7557e
WL
661 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
662 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
663 * @out_nid: ptr to int for nid of the range, can be %NULL
7bd0b0f0
TH
664 *
665 * Reverse of __next_free_mem_range().
666 */
667void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
668 phys_addr_t *out_start,
669 phys_addr_t *out_end, int *out_nid)
670{
671 struct memblock_type *mem = &memblock.memory;
672 struct memblock_type *rsv = &memblock.reserved;
673 int mi = *idx & 0xffffffff;
674 int ri = *idx >> 32;
675
676 if (*idx == (u64)ULLONG_MAX) {
677 mi = mem->cnt - 1;
678 ri = rsv->cnt;
679 }
680
681 for ( ; mi >= 0; mi--) {
682 struct memblock_region *m = &mem->regions[mi];
683 phys_addr_t m_start = m->base;
684 phys_addr_t m_end = m->base + m->size;
685
686 /* only memory regions are associated with nodes, check it */
687 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
688 continue;
689
690 /* scan areas before each reservation for intersection */
691 for ( ; ri >= 0; ri--) {
692 struct memblock_region *r = &rsv->regions[ri];
693 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
694 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
695
696 /* if ri advanced past mi, break out to advance mi */
697 if (r_end <= m_start)
698 break;
699 /* if the two regions intersect, we're done */
700 if (m_end > r_start) {
701 if (out_start)
702 *out_start = max(m_start, r_start);
703 if (out_end)
704 *out_end = min(m_end, r_end);
705 if (out_nid)
706 *out_nid = memblock_get_region_node(m);
707
708 if (m_start >= r_start)
709 mi--;
710 else
711 ri--;
712 *idx = (u32)mi | (u64)ri << 32;
713 return;
714 }
715 }
716 }
717
718 *idx = ULLONG_MAX;
719}
720
7c0caeb8
TH
721#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
722/*
723 * Common iterator interface used to define for_each_mem_range().
724 */
725void __init_memblock __next_mem_pfn_range(int *idx, int nid,
726 unsigned long *out_start_pfn,
727 unsigned long *out_end_pfn, int *out_nid)
728{
729 struct memblock_type *type = &memblock.memory;
730 struct memblock_region *r;
731
732 while (++*idx < type->cnt) {
733 r = &type->regions[*idx];
734
735 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
736 continue;
737 if (nid == MAX_NUMNODES || nid == r->nid)
738 break;
739 }
740 if (*idx >= type->cnt) {
741 *idx = -1;
742 return;
743 }
744
745 if (out_start_pfn)
746 *out_start_pfn = PFN_UP(r->base);
747 if (out_end_pfn)
748 *out_end_pfn = PFN_DOWN(r->base + r->size);
749 if (out_nid)
750 *out_nid = r->nid;
751}
752
753/**
754 * memblock_set_node - set node ID on memblock regions
755 * @base: base of area to set node ID for
756 * @size: size of area to set node ID for
757 * @nid: node ID to set
758 *
759 * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
760 * Regions which cross the area boundaries are split as necessary.
761 *
762 * RETURNS:
763 * 0 on success, -errno on failure.
764 */
765int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
766 int nid)
767{
768 struct memblock_type *type = &memblock.memory;
6a9ceb31
TH
769 int start_rgn, end_rgn;
770 int i, ret;
7c0caeb8 771
6a9ceb31
TH
772 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
773 if (ret)
774 return ret;
7c0caeb8 775
6a9ceb31 776 for (i = start_rgn; i < end_rgn; i++)
e9d24ad3 777 memblock_set_region_node(&type->regions[i], nid);
7c0caeb8
TH
778
779 memblock_merge_regions(type);
780 return 0;
781}
782#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
783
7bd0b0f0
TH
784static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
785 phys_addr_t align, phys_addr_t max_addr,
786 int nid)
95f72d1e 787{
6ed311b2 788 phys_addr_t found;
95f72d1e 789
847854f5
TH
790 /* align @size to avoid excessive fragmentation on reserved array */
791 size = round_up(size, align);
792
7bd0b0f0 793 found = memblock_find_in_range_node(0, max_addr, size, align, nid);
9c8c27e2 794 if (found && !memblock_reserve(found, size))
6ed311b2 795 return found;
95f72d1e 796
6ed311b2 797 return 0;
95f72d1e
YL
798}
799
7bd0b0f0
TH
800phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
801{
802 return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
803}
804
805phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
806{
807 return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES);
808}
809
6ed311b2 810phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
95f72d1e 811{
6ed311b2
BH
812 phys_addr_t alloc;
813
814 alloc = __memblock_alloc_base(size, align, max_addr);
815
816 if (alloc == 0)
817 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
818 (unsigned long long) size, (unsigned long long) max_addr);
819
820 return alloc;
95f72d1e
YL
821}
822
6ed311b2 823phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
95f72d1e 824{
6ed311b2
BH
825 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
826}
95f72d1e 827
9d1e2492
BH
828phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
829{
830 phys_addr_t res = memblock_alloc_nid(size, align, nid);
831
832 if (res)
833 return res;
15fb0972 834 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
95f72d1e
YL
835}
836
9d1e2492
BH
837
838/*
839 * Remaining API functions
840 */
841
2898cc4c 842phys_addr_t __init memblock_phys_mem_size(void)
95f72d1e 843{
1440c4e2 844 return memblock.memory.total_size;
95f72d1e
YL
845}
846
595ad9af
YL
847phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
848{
849 unsigned long pages = 0;
850 struct memblock_region *r;
851 unsigned long start_pfn, end_pfn;
852
853 for_each_memblock(memory, r) {
854 start_pfn = memblock_region_memory_base_pfn(r);
855 end_pfn = memblock_region_memory_end_pfn(r);
856 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
857 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
858 pages += end_pfn - start_pfn;
859 }
860
861 return (phys_addr_t)pages << PAGE_SHIFT;
862}
863
0a93ebef
SR
864/* lowest address */
865phys_addr_t __init_memblock memblock_start_of_DRAM(void)
866{
867 return memblock.memory.regions[0].base;
868}
869
10d06439 870phys_addr_t __init_memblock memblock_end_of_DRAM(void)
95f72d1e
YL
871{
872 int idx = memblock.memory.cnt - 1;
873
e3239ff9 874 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
95f72d1e
YL
875}
876
c0ce8fef 877void __init memblock_enforce_memory_limit(phys_addr_t limit)
95f72d1e
YL
878{
879 unsigned long i;
c0ce8fef 880 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
95f72d1e 881
c0ce8fef 882 if (!limit)
95f72d1e
YL
883 return;
884
c0ce8fef 885 /* find out max address */
95f72d1e 886 for (i = 0; i < memblock.memory.cnt; i++) {
c0ce8fef 887 struct memblock_region *r = &memblock.memory.regions[i];
95f72d1e 888
c0ce8fef
TH
889 if (limit <= r->size) {
890 max_addr = r->base + limit;
891 break;
95f72d1e 892 }
c0ce8fef 893 limit -= r->size;
95f72d1e 894 }
c0ce8fef
TH
895
896 /* truncate both memory and reserved regions */
897 __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
898 __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
95f72d1e
YL
899}
900
cd79481d 901static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
72d4b0b4
BH
902{
903 unsigned int left = 0, right = type->cnt;
904
905 do {
906 unsigned int mid = (right + left) / 2;
907
908 if (addr < type->regions[mid].base)
909 right = mid;
910 else if (addr >= (type->regions[mid].base +
911 type->regions[mid].size))
912 left = mid + 1;
913 else
914 return mid;
915 } while (left < right);
916 return -1;
917}
918
2898cc4c 919int __init memblock_is_reserved(phys_addr_t addr)
95f72d1e 920{
72d4b0b4
BH
921 return memblock_search(&memblock.reserved, addr) != -1;
922}
95f72d1e 923
3661ca66 924int __init_memblock memblock_is_memory(phys_addr_t addr)
72d4b0b4
BH
925{
926 return memblock_search(&memblock.memory, addr) != -1;
927}
928
eab30949
SB
929/**
930 * memblock_is_region_memory - check if a region is a subset of memory
931 * @base: base of region to check
932 * @size: size of region to check
933 *
934 * Check if the region [@base, @base+@size) is a subset of a memory block.
935 *
936 * RETURNS:
937 * 0 if false, non-zero if true
938 */
3661ca66 939int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
72d4b0b4 940{
abb65272 941 int idx = memblock_search(&memblock.memory, base);
eb18f1b5 942 phys_addr_t end = base + memblock_cap_size(base, &size);
72d4b0b4
BH
943
944 if (idx == -1)
945 return 0;
abb65272
TV
946 return memblock.memory.regions[idx].base <= base &&
947 (memblock.memory.regions[idx].base +
eb18f1b5 948 memblock.memory.regions[idx].size) >= end;
95f72d1e
YL
949}
950
eab30949
SB
951/**
952 * memblock_is_region_reserved - check if a region intersects reserved memory
953 * @base: base of region to check
954 * @size: size of region to check
955 *
956 * Check if the region [@base, @base+@size) intersects a reserved memory block.
957 *
958 * RETURNS:
959 * 0 if false, non-zero if true
960 */
10d06439 961int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
95f72d1e 962{
eb18f1b5 963 memblock_cap_size(base, &size);
f1c2c19c 964 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
95f72d1e
YL
965}
966
6ede1fd3
YL
967void __init_memblock memblock_trim_memory(phys_addr_t align)
968{
969 int i;
970 phys_addr_t start, end, orig_start, orig_end;
971 struct memblock_type *mem = &memblock.memory;
972
973 for (i = 0; i < mem->cnt; i++) {
974 orig_start = mem->regions[i].base;
975 orig_end = mem->regions[i].base + mem->regions[i].size;
976 start = round_up(orig_start, align);
977 end = round_down(orig_end, align);
978
979 if (start == orig_start && end == orig_end)
980 continue;
981
982 if (start < end) {
983 mem->regions[i].base = start;
984 mem->regions[i].size = end - start;
985 } else {
986 memblock_remove_region(mem, i);
987 i--;
988 }
989 }
990}
e63075a3 991
3661ca66 992void __init_memblock memblock_set_current_limit(phys_addr_t limit)
e63075a3
BH
993{
994 memblock.current_limit = limit;
995}
996
7c0caeb8 997static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
6ed311b2
BH
998{
999 unsigned long long base, size;
1000 int i;
1001
7c0caeb8 1002 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
6ed311b2 1003
7c0caeb8
TH
1004 for (i = 0; i < type->cnt; i++) {
1005 struct memblock_region *rgn = &type->regions[i];
1006 char nid_buf[32] = "";
1007
1008 base = rgn->base;
1009 size = rgn->size;
1010#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1011 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1012 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1013 memblock_get_region_node(rgn));
1014#endif
1015 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
1016 name, i, base, base + size - 1, size, nid_buf);
6ed311b2
BH
1017 }
1018}
1019
4ff7b82f 1020void __init_memblock __memblock_dump_all(void)
6ed311b2 1021{
6ed311b2 1022 pr_info("MEMBLOCK configuration:\n");
1440c4e2
TH
1023 pr_info(" memory size = %#llx reserved size = %#llx\n",
1024 (unsigned long long)memblock.memory.total_size,
1025 (unsigned long long)memblock.reserved.total_size);
6ed311b2
BH
1026
1027 memblock_dump(&memblock.memory, "memory");
1028 memblock_dump(&memblock.reserved, "reserved");
1029}
1030
1aadc056 1031void __init memblock_allow_resize(void)
6ed311b2 1032{
142b45a7 1033 memblock_can_resize = 1;
6ed311b2
BH
1034}
1035
6ed311b2
BH
1036static int __init early_memblock(char *p)
1037{
1038 if (p && strstr(p, "debug"))
1039 memblock_debug = 1;
1040 return 0;
1041}
1042early_param("memblock", early_memblock);
1043
c378ddd5 1044#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
6d03b885
BH
1045
1046static int memblock_debug_show(struct seq_file *m, void *private)
1047{
1048 struct memblock_type *type = m->private;
1049 struct memblock_region *reg;
1050 int i;
1051
1052 for (i = 0; i < type->cnt; i++) {
1053 reg = &type->regions[i];
1054 seq_printf(m, "%4d: ", i);
1055 if (sizeof(phys_addr_t) == 4)
1056 seq_printf(m, "0x%08lx..0x%08lx\n",
1057 (unsigned long)reg->base,
1058 (unsigned long)(reg->base + reg->size - 1));
1059 else
1060 seq_printf(m, "0x%016llx..0x%016llx\n",
1061 (unsigned long long)reg->base,
1062 (unsigned long long)(reg->base + reg->size - 1));
1063
1064 }
1065 return 0;
1066}
1067
1068static int memblock_debug_open(struct inode *inode, struct file *file)
1069{
1070 return single_open(file, memblock_debug_show, inode->i_private);
1071}
1072
1073static const struct file_operations memblock_debug_fops = {
1074 .open = memblock_debug_open,
1075 .read = seq_read,
1076 .llseek = seq_lseek,
1077 .release = single_release,
1078};
1079
1080static int __init memblock_init_debugfs(void)
1081{
1082 struct dentry *root = debugfs_create_dir("memblock", NULL);
1083 if (!root)
1084 return -ENXIO;
1085 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1086 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
1087
1088 return 0;
1089}
1090__initcall(memblock_init_debugfs);
1091
1092#endif /* CONFIG_DEBUG_FS */