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