page_alloc: bootmem limit with movablecore_map
[deliverable/linux.git] / mm / memblock.c
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>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/bitops.h>
17 #include <linux/poison.h>
18 #include <linux/pfn.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
21 #include <linux/memblock.h>
22
23 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
24 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
25
26 struct 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 };
37
38 int memblock_debug __initdata_memblock;
39 static int memblock_can_resize __initdata_memblock;
40 static int memblock_memory_in_slab __initdata_memblock = 0;
41 static int memblock_reserved_in_slab __initdata_memblock = 0;
42
43 /* inline so we don't get a warning when pr_debug is compiled out */
44 static __init_memblock const char *
45 memblock_type_name(struct memblock_type *type)
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
55 /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
56 static 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
61 /*
62 * Address comparison utilities
63 */
64 static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
65 phys_addr_t base2, phys_addr_t size2)
66 {
67 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
68 }
69
70 static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
71 phys_addr_t base, phys_addr_t size)
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
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.
97 */
98 phys_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)
101 {
102 phys_addr_t this_start, this_end, cand;
103 u64 i;
104 int curr = movablemem_map.nr_map - 1;
105
106 /* pump up @end */
107 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
108 end = memblock.current_limit;
109
110 /* avoid allocating the first page */
111 start = max_t(phys_addr_t, start, PAGE_SIZE);
112 end = max(start, end);
113
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);
117
118 restart:
119 if (this_end <= this_start || this_end < size)
120 continue;
121
122 for (; curr >= 0; curr--) {
123 if ((movablemem_map.map[curr].start_pfn << PAGE_SHIFT)
124 < this_end)
125 break;
126 }
127
128 cand = round_down(this_end - size, align);
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
136 if (cand >= this_start)
137 return cand;
138 }
139
140 return 0;
141 }
142
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.
154 */
155 phys_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)
158 {
159 return memblock_find_in_range_node(start, end, size, align,
160 MAX_NUMNODES);
161 }
162
163 static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
164 {
165 type->total_size -= type->regions[r].size;
166 memmove(&type->regions[r], &type->regions[r + 1],
167 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
168 type->cnt--;
169
170 /* Special case for empty arrays */
171 if (type->cnt == 0) {
172 WARN_ON(type->total_size != 0);
173 type->cnt = 1;
174 type->regions[0].base = 0;
175 type->regions[0].size = 0;
176 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
177 }
178 }
179
180 phys_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
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 */
207 static int __init_memblock memblock_double_array(struct memblock_type *type,
208 phys_addr_t new_area_start,
209 phys_addr_t new_area_size)
210 {
211 struct memblock_region *new_array, *old_array;
212 phys_addr_t old_alloc_size, new_alloc_size;
213 phys_addr_t old_size, new_size, addr;
214 int use_slab = slab_is_available();
215 int *in_slab;
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
223 /* Calculate new doubled size */
224 old_size = type->max * sizeof(struct memblock_region);
225 new_size = old_size << 1;
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);
232
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
239 /* Try to find some space for it.
240 *
241 * WARNING: We assume that either slab_is_available() and we use it or
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)
245 *
246 * This should however not be an issue for now, as we currently only
247 * call into MEMBLOCK while it's still active, or much later when slab
248 * is active for memory hotplug operations
249 */
250 if (use_slab) {
251 new_array = kmalloc(new_size, GFP_KERNEL);
252 addr = new_array ? __pa(new_array) : 0;
253 } else {
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,
260 new_alloc_size, PAGE_SIZE);
261 if (!addr && new_area_size)
262 addr = memblock_find_in_range(0,
263 min(new_area_start, memblock.current_limit),
264 new_alloc_size, PAGE_SIZE);
265
266 new_array = addr ? __va(addr) : NULL;
267 }
268 if (!addr) {
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 }
273
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);
277
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.
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
289 /* Free old array. We needn't free it if the array is the static one */
290 if (*in_slab)
291 kfree(old_array);
292 else if (old_array != memblock_memory_init_regions &&
293 old_array != memblock_reserved_init_regions)
294 memblock_free(__pa(old_array), old_alloc_size);
295
296 /*
297 * Reserve the new array if that comes from the memblock. Otherwise, we
298 * needn't do it
299 */
300 if (!use_slab)
301 BUG_ON(memblock_reserve(addr, new_alloc_size));
302
303 /* Update slab flag */
304 *in_slab = use_slab;
305
306 return 0;
307 }
308
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 */
315 static void __init_memblock memblock_merge_regions(struct memblock_type *type)
316 {
317 int i = 0;
318
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];
323
324 if (this->base + this->size != next->base ||
325 memblock_get_region_node(this) !=
326 memblock_get_region_node(next)) {
327 BUG_ON(this->base + this->size > next->base);
328 i++;
329 continue;
330 }
331
332 this->size += next->size;
333 /* move forward from next + 1, index of which is i + 2 */
334 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
335 type->cnt--;
336 }
337 }
338
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 */
349 static void __init_memblock memblock_insert_region(struct memblock_type *type,
350 int idx, phys_addr_t base,
351 phys_addr_t size, int nid)
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;
359 memblock_set_region_node(rgn, nid);
360 type->cnt++;
361 type->total_size += size;
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
369 * @nid: nid of the new region
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 */
379 static int __init_memblock memblock_add_region(struct memblock_type *type,
380 phys_addr_t base, phys_addr_t size, int nid)
381 {
382 bool insert = false;
383 phys_addr_t obase = base;
384 phys_addr_t end = base + memblock_cap_size(base, &size);
385 int i, nr_new;
386
387 if (!size)
388 return 0;
389
390 /* special case for empty array */
391 if (type->regions[0].size == 0) {
392 WARN_ON(type->cnt != 1 || type->total_size);
393 type->regions[0].base = base;
394 type->regions[0].size = size;
395 memblock_set_region_node(&type->regions[0], nid);
396 type->total_size = size;
397 return 0;
398 }
399 repeat:
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.
404 */
405 base = obase;
406 nr_new = 0;
407
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)
414 break;
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,
425 rbase - base, nid);
426 }
427 /* area below @rend is dealt with, forget about it */
428 base = min(rend, end);
429 }
430
431 /* insert the remaining portion */
432 if (base < end) {
433 nr_new++;
434 if (insert)
435 memblock_insert_region(type, i, base, end - base, nid);
436 }
437
438 /*
439 * If this was the first round, resize array and repeat for actual
440 * insertions; otherwise, merge and return.
441 */
442 if (!insert) {
443 while (type->cnt + nr_new > type->max)
444 if (memblock_double_array(type, obase, size) < 0)
445 return -ENOMEM;
446 insert = true;
447 goto repeat;
448 } else {
449 memblock_merge_regions(type);
450 return 0;
451 }
452 }
453
454 int __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
460 int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
461 {
462 return memblock_add_region(&memblock.memory, base, size, MAX_NUMNODES);
463 }
464
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 */
481 static 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 {
485 phys_addr_t end = base + memblock_cap_size(base, &size);
486 int i;
487
488 *start_rgn = *end_rgn = 0;
489
490 if (!size)
491 return 0;
492
493 /* we'll create at most two more regions */
494 while (type->cnt + 2 > type->max)
495 if (memblock_double_array(type, base, size) < 0)
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;
514 rgn->size -= base - rbase;
515 type->total_size -= base - rbase;
516 memblock_insert_region(type, i, rbase, base - rbase,
517 memblock_get_region_node(rgn));
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;
524 rgn->size -= end - rbase;
525 type->total_size -= end - rbase;
526 memblock_insert_region(type, i--, rbase, end - rbase,
527 memblock_get_region_node(rgn));
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 }
538
539 static int __init_memblock __memblock_remove(struct memblock_type *type,
540 phys_addr_t base, phys_addr_t size)
541 {
542 int start_rgn, end_rgn;
543 int i, ret;
544
545 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
546 if (ret)
547 return ret;
548
549 for (i = end_rgn - 1; i >= start_rgn; i--)
550 memblock_remove_region(type, i);
551 return 0;
552 }
553
554 int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
555 {
556 return __memblock_remove(&memblock.memory, base, size);
557 }
558
559 int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
560 {
561 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
562 (unsigned long long)base,
563 (unsigned long long)base + size,
564 (void *)_RET_IP_);
565
566 return __memblock_remove(&memblock.reserved, base, size);
567 }
568
569 int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
570 {
571 struct memblock_type *_rgn = &memblock.reserved;
572
573 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
574 (unsigned long long)base,
575 (unsigned long long)base + size,
576 (void *)_RET_IP_);
577
578 return memblock_add_region(_rgn, base, size, MAX_NUMNODES);
579 }
580
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
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
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 */
604 void __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
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
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
664 *
665 * Reverse of __next_free_mem_range().
666 */
667 void __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
721 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
722 /*
723 * Common iterator interface used to define for_each_mem_range().
724 */
725 void __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 */
765 int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
766 int nid)
767 {
768 struct memblock_type *type = &memblock.memory;
769 int start_rgn, end_rgn;
770 int i, ret;
771
772 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
773 if (ret)
774 return ret;
775
776 for (i = start_rgn; i < end_rgn; i++)
777 memblock_set_region_node(&type->regions[i], nid);
778
779 memblock_merge_regions(type);
780 return 0;
781 }
782 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
783
784 static 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)
787 {
788 phys_addr_t found;
789
790 /* align @size to avoid excessive fragmentation on reserved array */
791 size = round_up(size, align);
792
793 found = memblock_find_in_range_node(0, max_addr, size, align, nid);
794 if (found && !memblock_reserve(found, size))
795 return found;
796
797 return 0;
798 }
799
800 phys_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
805 phys_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
810 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
811 {
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;
821 }
822
823 phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
824 {
825 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
826 }
827
828 phys_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;
834 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
835 }
836
837
838 /*
839 * Remaining API functions
840 */
841
842 phys_addr_t __init memblock_phys_mem_size(void)
843 {
844 return memblock.memory.total_size;
845 }
846
847 phys_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
864 /* lowest address */
865 phys_addr_t __init_memblock memblock_start_of_DRAM(void)
866 {
867 return memblock.memory.regions[0].base;
868 }
869
870 phys_addr_t __init_memblock memblock_end_of_DRAM(void)
871 {
872 int idx = memblock.memory.cnt - 1;
873
874 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
875 }
876
877 void __init memblock_enforce_memory_limit(phys_addr_t limit)
878 {
879 unsigned long i;
880 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
881
882 if (!limit)
883 return;
884
885 /* find out max address */
886 for (i = 0; i < memblock.memory.cnt; i++) {
887 struct memblock_region *r = &memblock.memory.regions[i];
888
889 if (limit <= r->size) {
890 max_addr = r->base + limit;
891 break;
892 }
893 limit -= r->size;
894 }
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);
899 }
900
901 static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
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
919 int __init memblock_is_reserved(phys_addr_t addr)
920 {
921 return memblock_search(&memblock.reserved, addr) != -1;
922 }
923
924 int __init_memblock memblock_is_memory(phys_addr_t addr)
925 {
926 return memblock_search(&memblock.memory, addr) != -1;
927 }
928
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 */
939 int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
940 {
941 int idx = memblock_search(&memblock.memory, base);
942 phys_addr_t end = base + memblock_cap_size(base, &size);
943
944 if (idx == -1)
945 return 0;
946 return memblock.memory.regions[idx].base <= base &&
947 (memblock.memory.regions[idx].base +
948 memblock.memory.regions[idx].size) >= end;
949 }
950
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 */
961 int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
962 {
963 memblock_cap_size(base, &size);
964 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
965 }
966
967 void __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 }
991
992 void __init_memblock memblock_set_current_limit(phys_addr_t limit)
993 {
994 memblock.current_limit = limit;
995 }
996
997 static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
998 {
999 unsigned long long base, size;
1000 int i;
1001
1002 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
1003
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);
1017 }
1018 }
1019
1020 void __init_memblock __memblock_dump_all(void)
1021 {
1022 pr_info("MEMBLOCK configuration:\n");
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);
1026
1027 memblock_dump(&memblock.memory, "memory");
1028 memblock_dump(&memblock.reserved, "reserved");
1029 }
1030
1031 void __init memblock_allow_resize(void)
1032 {
1033 memblock_can_resize = 1;
1034 }
1035
1036 static int __init early_memblock(char *p)
1037 {
1038 if (p && strstr(p, "debug"))
1039 memblock_debug = 1;
1040 return 0;
1041 }
1042 early_param("memblock", early_memblock);
1043
1044 #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
1045
1046 static 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
1068 static int memblock_debug_open(struct inode *inode, struct file *file)
1069 {
1070 return single_open(file, memblock_debug_show, inode->i_private);
1071 }
1072
1073 static 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
1080 static 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 */
This page took 0.053064 seconds and 6 git commands to generate.