blob: b7abce597e2f7493338a25fcd30a1e23dbe4c2e7 [file] [log] [blame]
Yinghai Lu95f72d12010-07-12 14:36:09 +10001/*
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>
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070014#include <linux/slab.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100015#include <linux/init.h>
16#include <linux/bitops.h>
Benjamin Herrenschmidt449e8df2010-07-06 15:39:07 -070017#include <linux/poison.h>
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -070018#include <linux/pfn.h>
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -070019#include <linux/debugfs.h>
20#include <linux/seq_file.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100021#include <linux/memblock.h>
22
Yinghai Lu10d06432010-07-28 15:43:02 +100023struct memblock memblock __initdata_memblock;
Yinghai Lu95f72d12010-07-12 14:36:09 +100024
Yinghai Lu10d06432010-07-28 15:43:02 +100025int memblock_debug __initdata_memblock;
26int memblock_can_resize __initdata_memblock;
27static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1] __initdata_memblock;
28static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1] __initdata_memblock;
Yinghai Lu95f72d12010-07-12 14:36:09 +100029
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070030/* inline so we don't get a warning when pr_debug is compiled out */
31static inline const char *memblock_type_name(struct memblock_type *type)
32{
33 if (type == &memblock.memory)
34 return "memory";
35 else if (type == &memblock.reserved)
36 return "reserved";
37 else
38 return "unknown";
39}
40
Tejun Heo0ce69292011-07-28 10:57:00 +020041/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
42static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
43{
44 return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
45}
46
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100047/*
48 * Address comparison utilities
49 */
50
Yinghai Lu10d06432010-07-28 15:43:02 +100051static phys_addr_t __init_memblock memblock_align_down(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +100052{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100053 return addr & ~(size - 1);
Yinghai Lu95f72d12010-07-12 14:36:09 +100054}
55
Yinghai Lu10d06432010-07-28 15:43:02 +100056static phys_addr_t __init_memblock memblock_align_up(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +100057{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100058 return (addr + (size - 1)) & ~(size - 1);
Yinghai Lu95f72d12010-07-12 14:36:09 +100059}
60
Yinghai Lu10d06432010-07-28 15:43:02 +100061static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100062 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100063{
64 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
65}
66
Yinghai Lu10d06432010-07-28 15:43:02 +100067long __init_memblock memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100068{
69 unsigned long i;
70
71 for (i = 0; i < type->cnt; i++) {
72 phys_addr_t rgnbase = type->regions[i].base;
73 phys_addr_t rgnsize = type->regions[i].size;
74 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
75 break;
76 }
77
78 return (i < type->cnt) ? i : -1;
79}
80
81/*
82 * Find, allocate, deallocate or reserve unreserved regions. All allocations
83 * are top-down.
84 */
85
Yinghai Lucd794812010-10-11 12:34:09 -070086static phys_addr_t __init_memblock memblock_find_region(phys_addr_t start, phys_addr_t end,
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100087 phys_addr_t size, phys_addr_t align)
88{
89 phys_addr_t base, res_base;
90 long j;
91
Yinghai Luf1af98c2010-10-04 14:57:39 -070092 /* In case, huge size is requested */
93 if (end < size)
94 return MEMBLOCK_ERROR;
95
96 base = memblock_align_down((end - size), align);
97
Benjamin Herrenschmidt25818f02010-07-28 15:25:10 +100098 /* Prevent allocations returning 0 as it's also used to
99 * indicate an allocation failure
100 */
101 if (start == 0)
102 start = PAGE_SIZE;
103
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000104 while (start <= base) {
105 j = memblock_overlaps_region(&memblock.reserved, base, size);
106 if (j < 0)
107 return base;
108 res_base = memblock.reserved.regions[j].base;
109 if (res_base < size)
110 break;
111 base = memblock_align_down(res_base - size, align);
112 }
113
114 return MEMBLOCK_ERROR;
115}
116
Yinghai Lu3661ca62010-09-15 13:05:29 -0700117static phys_addr_t __init_memblock memblock_find_base(phys_addr_t size,
118 phys_addr_t align, phys_addr_t start, phys_addr_t end)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000119{
120 long i;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000121
122 BUG_ON(0 == size);
123
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000124 /* Pump up max_addr */
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000125 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
126 end = memblock.current_limit;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000127
128 /* We do a top-down search, this tends to limit memory
129 * fragmentation by keeping early boot allocs near the
130 * top of memory
131 */
132 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
133 phys_addr_t memblockbase = memblock.memory.regions[i].base;
134 phys_addr_t memblocksize = memblock.memory.regions[i].size;
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000135 phys_addr_t bottom, top, found;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000136
137 if (memblocksize < size)
138 continue;
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000139 if ((memblockbase + memblocksize) <= start)
140 break;
141 bottom = max(memblockbase, start);
142 top = min(memblockbase + memblocksize, end);
143 if (bottom >= top)
144 continue;
145 found = memblock_find_region(bottom, top, size, align);
146 if (found != MEMBLOCK_ERROR)
147 return found;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000148 }
149 return MEMBLOCK_ERROR;
150}
151
Yinghai Lu5303b682010-07-28 15:38:40 +1000152/*
153 * Find a free area with specified alignment in a specific range.
154 */
155u64 __init_memblock memblock_find_in_range(u64 start, u64 end, u64 size, u64 align)
156{
157 return memblock_find_base(size, align, start, end);
158}
159
Yinghai Lu7950c402010-08-25 13:39:14 -0700160/*
161 * Free memblock.reserved.regions
162 */
163int __init_memblock memblock_free_reserved_regions(void)
164{
165 if (memblock.reserved.regions == memblock_reserved_init_regions)
166 return 0;
167
168 return memblock_free(__pa(memblock.reserved.regions),
169 sizeof(struct memblock_region) * memblock.reserved.max);
170}
171
172/*
173 * Reserve memblock.reserved.regions
174 */
175int __init_memblock memblock_reserve_reserved_regions(void)
176{
177 if (memblock.reserved.regions == memblock_reserved_init_regions)
178 return 0;
179
180 return memblock_reserve(__pa(memblock.reserved.regions),
181 sizeof(struct memblock_region) * memblock.reserved.max);
182}
183
Yinghai Lu10d06432010-07-28 15:43:02 +1000184static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000185{
186 unsigned long i;
187
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000188 for (i = r; i < type->cnt - 1; i++) {
189 type->regions[i].base = type->regions[i + 1].base;
190 type->regions[i].size = type->regions[i + 1].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000191 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000192 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000193
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700194 /* Special case for empty arrays */
195 if (type->cnt == 0) {
196 type->cnt = 1;
197 type->regions[0].base = 0;
198 type->regions[0].size = 0;
199 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000200}
201
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700202/* Defined below but needed now */
203static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size);
204
Yinghai Lu10d06432010-07-28 15:43:02 +1000205static int __init_memblock memblock_double_array(struct memblock_type *type)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700206{
207 struct memblock_region *new_array, *old_array;
208 phys_addr_t old_size, new_size, addr;
209 int use_slab = slab_is_available();
210
211 /* We don't allow resizing until we know about the reserved regions
212 * of memory that aren't suitable for allocation
213 */
214 if (!memblock_can_resize)
215 return -1;
216
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700217 /* Calculate new doubled size */
218 old_size = type->max * sizeof(struct memblock_region);
219 new_size = old_size << 1;
220
221 /* Try to find some space for it.
222 *
223 * WARNING: We assume that either slab_is_available() and we use it or
224 * we use MEMBLOCK for allocations. That means that this is unsafe to use
225 * when bootmem is currently active (unless bootmem itself is implemented
226 * on top of MEMBLOCK which isn't the case yet)
227 *
228 * This should however not be an issue for now, as we currently only
229 * call into MEMBLOCK while it's still active, or much later when slab is
230 * active for memory hotplug operations
231 */
232 if (use_slab) {
233 new_array = kmalloc(new_size, GFP_KERNEL);
234 addr = new_array == NULL ? MEMBLOCK_ERROR : __pa(new_array);
235 } else
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000236 addr = memblock_find_base(new_size, sizeof(phys_addr_t), 0, MEMBLOCK_ALLOC_ACCESSIBLE);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700237 if (addr == MEMBLOCK_ERROR) {
238 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
239 memblock_type_name(type), type->max, type->max * 2);
240 return -1;
241 }
242 new_array = __va(addr);
243
Yinghai Luea9e4372010-07-28 15:13:22 +1000244 memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]",
245 memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1);
246
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700247 /* Found space, we now need to move the array over before
248 * we add the reserved region since it may be our reserved
249 * array itself that is full.
250 */
251 memcpy(new_array, type->regions, old_size);
252 memset(new_array + type->max, 0, old_size);
253 old_array = type->regions;
254 type->regions = new_array;
255 type->max <<= 1;
256
257 /* If we use SLAB that's it, we are done */
258 if (use_slab)
259 return 0;
260
261 /* Add the new reserved region now. Should not fail ! */
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700262 BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size));
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700263
264 /* If the array wasn't our static init one, then free it. We only do
265 * that before SLAB is available as later on, we don't know whether
266 * to use kfree or free_bootmem_pages(). Shouldn't be a big deal
267 * anyways
268 */
269 if (old_array != memblock_memory_init_regions &&
270 old_array != memblock_reserved_init_regions)
271 memblock_free(__pa(old_array), old_size);
272
273 return 0;
274}
275
Yinghai Lu10d06432010-07-28 15:43:02 +1000276extern int __init_memblock __weak memblock_memory_can_coalesce(phys_addr_t addr1, phys_addr_t size1,
Benjamin Herrenschmidtd2cd5632010-07-06 15:39:14 -0700277 phys_addr_t addr2, phys_addr_t size2)
278{
279 return 1;
280}
281
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700282static long __init_memblock memblock_add_region(struct memblock_type *type,
283 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000284{
Tejun Heo0ce69292011-07-28 10:57:00 +0200285 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700286 int i, slot = -1;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000287
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700288 /* First try and coalesce this MEMBLOCK with others */
289 for (i = 0; i < type->cnt; i++) {
290 struct memblock_region *rgn = &type->regions[i];
291 phys_addr_t rend = rgn->base + rgn->size;
292
293 /* Exit if there's no possible hits */
294 if (rgn->base > end || rgn->size == 0)
295 break;
296
297 /* Check if we are fully enclosed within an existing
298 * block
299 */
300 if (rgn->base <= base && rend >= end)
301 return 0;
302
303 /* Check if we overlap or are adjacent with the bottom
304 * of a block.
305 */
306 if (base < rgn->base && end >= rgn->base) {
307 /* If we can't coalesce, create a new block */
308 if (!memblock_memory_can_coalesce(base, size,
309 rgn->base,
310 rgn->size)) {
311 /* Overlap & can't coalesce are mutually
312 * exclusive, if you do that, be prepared
313 * for trouble
314 */
315 WARN_ON(end != rgn->base);
316 goto new_block;
317 }
318 /* We extend the bottom of the block down to our
319 * base
320 */
321 rgn->base = base;
322 rgn->size = rend - base;
323
324 /* Return if we have nothing else to allocate
325 * (fully coalesced)
326 */
327 if (rend >= end)
328 return 0;
329
330 /* We continue processing from the end of the
331 * coalesced block.
332 */
333 base = rend;
334 size = end - base;
335 }
336
337 /* Now check if we overlap or are adjacent with the
338 * top of a block
339 */
340 if (base <= rend && end >= rend) {
341 /* If we can't coalesce, create a new block */
342 if (!memblock_memory_can_coalesce(rgn->base,
343 rgn->size,
344 base, size)) {
345 /* Overlap & can't coalesce are mutually
346 * exclusive, if you do that, be prepared
347 * for trouble
348 */
349 WARN_ON(rend != base);
350 goto new_block;
351 }
352 /* We adjust our base down to enclose the
353 * original block and destroy it. It will be
354 * part of our new allocation. Since we've
355 * freed an entry, we know we won't fail
356 * to allocate one later, so we won't risk
357 * losing the original block allocation.
358 */
359 size += (base - rgn->base);
360 base = rgn->base;
361 memblock_remove_region(type, i--);
362 }
363 }
364
365 /* If the array is empty, special case, replace the fake
366 * filler region and return
367 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000368 if ((type->cnt == 1) && (type->regions[0].size == 0)) {
369 type->regions[0].base = base;
370 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000371 return 0;
372 }
373
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700374 new_block:
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700375 /* If we are out of space, we fail. It's too late to resize the array
376 * but then this shouldn't have happened in the first place.
377 */
378 if (WARN_ON(type->cnt >= type->max))
Yinghai Lu95f72d12010-07-12 14:36:09 +1000379 return -1;
380
381 /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000382 for (i = type->cnt - 1; i >= 0; i--) {
383 if (base < type->regions[i].base) {
384 type->regions[i+1].base = type->regions[i].base;
385 type->regions[i+1].size = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000386 } else {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000387 type->regions[i+1].base = base;
388 type->regions[i+1].size = size;
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700389 slot = i + 1;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000390 break;
391 }
392 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000393 if (base < type->regions[0].base) {
394 type->regions[0].base = base;
395 type->regions[0].size = size;
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700396 slot = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000397 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000398 type->cnt++;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000399
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700400 /* The array is full ? Try to resize it. If that fails, we undo
401 * our allocation and return an error
402 */
403 if (type->cnt == type->max && memblock_double_array(type)) {
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700404 BUG_ON(slot < 0);
405 memblock_remove_region(type, slot);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700406 return -1;
407 }
408
Yinghai Lu95f72d12010-07-12 14:36:09 +1000409 return 0;
410}
411
Yinghai Lu10d06432010-07-28 15:43:02 +1000412long __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000413{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000414 return memblock_add_region(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000415
416}
417
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700418static long __init_memblock __memblock_remove(struct memblock_type *type,
419 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000420{
Tejun Heo0ce69292011-07-28 10:57:00 +0200421 phys_addr_t end = base + memblock_cap_size(base, &size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000422 int i;
423
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700424 /* Walk through the array for collisions */
425 for (i = 0; i < type->cnt; i++) {
426 struct memblock_region *rgn = &type->regions[i];
427 phys_addr_t rend = rgn->base + rgn->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000428
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700429 /* Nothing more to do, exit */
430 if (rgn->base > end || rgn->size == 0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000431 break;
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700432
433 /* If we fully enclose the block, drop it */
434 if (base <= rgn->base && end >= rend) {
435 memblock_remove_region(type, i--);
436 continue;
437 }
438
439 /* If we are fully enclosed within a block
440 * then we need to split it and we are done
441 */
442 if (base > rgn->base && end < rend) {
443 rgn->size = base - rgn->base;
444 if (!memblock_add_region(type, end, rend - end))
445 return 0;
446 /* Failure to split is bad, we at least
447 * restore the block before erroring
448 */
449 rgn->size = rend - rgn->base;
450 WARN_ON(1);
451 return -1;
452 }
453
454 /* Check if we need to trim the bottom of a block */
455 if (rgn->base < end && rend > end) {
456 rgn->size -= end - rgn->base;
457 rgn->base = end;
458 break;
459 }
460
461 /* And check if we need to trim the top of a block */
462 if (base < rend)
463 rgn->size -= rend - base;
464
Yinghai Lu95f72d12010-07-12 14:36:09 +1000465 }
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700466 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000467}
468
Yinghai Lu10d06432010-07-28 15:43:02 +1000469long __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000470{
471 return __memblock_remove(&memblock.memory, base, size);
472}
473
Yinghai Lu3661ca62010-09-15 13:05:29 -0700474long __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000475{
476 return __memblock_remove(&memblock.reserved, base, size);
477}
478
Yinghai Lu3661ca62010-09-15 13:05:29 -0700479long __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000480{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000481 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000482
483 BUG_ON(0 == size);
484
485 return memblock_add_region(_rgn, base, size);
486}
487
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000488phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000489{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000490 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000491
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000492 /* We align the size to limit fragmentation. Without this, a lot of
493 * small allocs quickly eat up the whole reserve array on sparc
494 */
495 size = memblock_align_up(size, align);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000496
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000497 found = memblock_find_base(size, align, 0, max_addr);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000498 if (found != MEMBLOCK_ERROR &&
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700499 !memblock_add_region(&memblock.reserved, found, size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000500 return found;
501
502 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000503}
504
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000505phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000506{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000507 phys_addr_t alloc;
508
509 alloc = __memblock_alloc_base(size, align, max_addr);
510
511 if (alloc == 0)
512 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
513 (unsigned long long) size, (unsigned long long) max_addr);
514
515 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000516}
517
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000518phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000519{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000520 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000521}
522
Yinghai Lu95f72d12010-07-12 14:36:09 +1000523
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000524/*
525 * Additional node-local allocators. Search for node memory is bottom up
526 * and walks memblock regions within that node bottom-up as well, but allocation
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700527 * within an memblock region is top-down. XXX I plan to fix that at some stage
528 *
529 * WARNING: Only available after early_node_map[] has been populated,
530 * on some architectures, that is after all the calls to add_active_range()
531 * have been done to populate it.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000532 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000533
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000534phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700535{
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700536#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
537 /*
538 * This code originates from sparc which really wants use to walk by addresses
539 * and returns the nid. This is not very convenient for early_pfn_map[] users
540 * as the map isn't sorted yet, and it really wants to be walked by nid.
541 *
542 * For now, I implement the inefficient method below which walks the early
543 * map multiple times. Eventually we may want to use an ARCH config option
544 * to implement a completely different method for both case.
545 */
546 unsigned long start_pfn, end_pfn;
547 int i;
548
549 for (i = 0; i < MAX_NUMNODES; i++) {
550 get_pfn_range_for_nid(i, &start_pfn, &end_pfn);
551 if (start < PFN_PHYS(start_pfn) || start >= PFN_PHYS(end_pfn))
552 continue;
553 *nid = i;
554 return min(end, PFN_PHYS(end_pfn));
555 }
556#endif
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700557 *nid = 0;
558
559 return end;
560}
561
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000562static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
563 phys_addr_t size,
564 phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000565{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000566 phys_addr_t start, end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000567
568 start = mp->base;
569 end = start + mp->size;
570
571 start = memblock_align_up(start, align);
572 while (start < end) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000573 phys_addr_t this_end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000574 int this_nid;
575
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700576 this_end = memblock_nid_range(start, end, &this_nid);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000577 if (this_nid == nid) {
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000578 phys_addr_t ret = memblock_find_region(start, this_end, size, align);
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700579 if (ret != MEMBLOCK_ERROR &&
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700580 !memblock_add_region(&memblock.reserved, ret, size))
Yinghai Lu95f72d12010-07-12 14:36:09 +1000581 return ret;
582 }
583 start = this_end;
584 }
585
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700586 return MEMBLOCK_ERROR;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000587}
588
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000589phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000590{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000591 struct memblock_type *mem = &memblock.memory;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000592 int i;
593
594 BUG_ON(0 == size);
595
Benjamin Herrenschmidt7f219c72010-07-12 14:24:57 +1000596 /* We align the size to limit fragmentation. Without this, a lot of
597 * small allocs quickly eat up the whole reserve array on sparc
598 */
599 size = memblock_align_up(size, align);
600
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700601 /* We do a bottom-up search for a region with the right
602 * nid since that's easier considering how memblock_nid_range()
603 * works
604 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000605 for (i = 0; i < mem->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000606 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
Yinghai Lu95f72d12010-07-12 14:36:09 +1000607 size, align, nid);
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700608 if (ret != MEMBLOCK_ERROR)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000609 return ret;
610 }
611
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700612 return 0;
613}
614
615phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
616{
617 phys_addr_t res = memblock_alloc_nid(size, align, nid);
618
619 if (res)
620 return res;
Benjamin Herrenschmidt918fe8d2010-07-06 15:39:18 -0700621 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ANYWHERE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000622}
623
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700624
625/*
626 * Remaining API functions
627 */
628
Yinghai Lu95f72d12010-07-12 14:36:09 +1000629/* You must call memblock_analyze() before this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000630phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000631{
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000632 return memblock.memory_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000633}
634
Yinghai Lu10d06432010-07-28 15:43:02 +1000635phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000636{
637 int idx = memblock.memory.cnt - 1;
638
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000639 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000640}
641
642/* You must call memblock_analyze() after this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000643void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000644{
645 unsigned long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000646 phys_addr_t limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000647 struct memblock_region *p;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000648
649 if (!memory_limit)
650 return;
651
652 /* Truncate the memblock regions to satisfy the memory limit. */
653 limit = memory_limit;
654 for (i = 0; i < memblock.memory.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000655 if (limit > memblock.memory.regions[i].size) {
656 limit -= memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000657 continue;
658 }
659
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000660 memblock.memory.regions[i].size = limit;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000661 memblock.memory.cnt = i + 1;
662 break;
663 }
664
Yinghai Lu95f72d12010-07-12 14:36:09 +1000665 memory_limit = memblock_end_of_DRAM();
666
667 /* And truncate any reserves above the limit also. */
668 for (i = 0; i < memblock.reserved.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000669 p = &memblock.reserved.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +1000670
671 if (p->base > memory_limit)
672 p->size = 0;
673 else if ((p->base + p->size) > memory_limit)
674 p->size = memory_limit - p->base;
675
676 if (p->size == 0) {
677 memblock_remove_region(&memblock.reserved, i);
678 i--;
679 }
680 }
681}
682
Yinghai Lucd794812010-10-11 12:34:09 -0700683static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000684{
685 unsigned int left = 0, right = type->cnt;
686
687 do {
688 unsigned int mid = (right + left) / 2;
689
690 if (addr < type->regions[mid].base)
691 right = mid;
692 else if (addr >= (type->regions[mid].base +
693 type->regions[mid].size))
694 left = mid + 1;
695 else
696 return mid;
697 } while (left < right);
698 return -1;
699}
700
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000701int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000702{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000703 return memblock_search(&memblock.reserved, addr) != -1;
704}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000705
Yinghai Lu3661ca62010-09-15 13:05:29 -0700706int __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000707{
708 return memblock_search(&memblock.memory, addr) != -1;
709}
710
Yinghai Lu3661ca62010-09-15 13:05:29 -0700711int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000712{
Tomi Valkeinenabb65272011-01-20 14:44:20 -0800713 int idx = memblock_search(&memblock.memory, base);
Tejun Heo0ce69292011-07-28 10:57:00 +0200714 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000715
716 if (idx == -1)
717 return 0;
Tomi Valkeinenabb65272011-01-20 14:44:20 -0800718 return memblock.memory.regions[idx].base <= base &&
719 (memblock.memory.regions[idx].base +
Tejun Heo0ce69292011-07-28 10:57:00 +0200720 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000721}
722
Stephen Boyd1aa4e5a2012-05-14 18:55:50 -0700723int __init_memblock memblock_overlaps_memory(phys_addr_t base, phys_addr_t size)
724{
725 memblock_cap_size(base, &size);
726 return memblock_overlaps_region(&memblock.memory, base, size) >= 0;
727}
728
Yinghai Lu10d06432010-07-28 15:43:02 +1000729int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000730{
Tejun Heo0ce69292011-07-28 10:57:00 +0200731 memblock_cap_size(base, &size);
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +1000732 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000733}
734
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700735
Yinghai Lu3661ca62010-09-15 13:05:29 -0700736void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700737{
738 memblock.current_limit = limit;
739}
740
Yinghai Lu10d06432010-07-28 15:43:02 +1000741static void __init_memblock memblock_dump(struct memblock_type *region, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000742{
743 unsigned long long base, size;
744 int i;
745
746 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
747
748 for (i = 0; i < region->cnt; i++) {
749 base = region->regions[i].base;
750 size = region->regions[i].size;
751
Yinghai Luea9e4372010-07-28 15:13:22 +1000752 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes\n",
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000753 name, i, base, base + size - 1, size);
754 }
755}
756
Yinghai Lu10d06432010-07-28 15:43:02 +1000757void __init_memblock memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000758{
759 if (!memblock_debug)
760 return;
761
762 pr_info("MEMBLOCK configuration:\n");
763 pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
764
765 memblock_dump(&memblock.memory, "memory");
766 memblock_dump(&memblock.reserved, "reserved");
767}
768
769void __init memblock_analyze(void)
770{
771 int i;
772
773 /* Check marker in the unused last array entry */
774 WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
775 != (phys_addr_t)RED_INACTIVE);
776 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
777 != (phys_addr_t)RED_INACTIVE);
778
779 memblock.memory_size = 0;
780
781 for (i = 0; i < memblock.memory.cnt; i++)
782 memblock.memory_size += memblock.memory.regions[i].size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700783
784 /* We allow resizing from there */
785 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000786}
787
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700788void __init memblock_init(void)
789{
Jeremy Fitzhardinge236260b2010-10-06 15:52:29 -0700790 static int init_done __initdata = 0;
791
792 if (init_done)
793 return;
794 init_done = 1;
795
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700796 /* Hookup the initial arrays */
797 memblock.memory.regions = memblock_memory_init_regions;
798 memblock.memory.max = INIT_MEMBLOCK_REGIONS;
799 memblock.reserved.regions = memblock_reserved_init_regions;
800 memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
801
802 /* Write a marker in the unused last array entry */
803 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
804 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
805
806 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
807 * This simplifies the memblock_add() code below...
808 */
809 memblock.memory.regions[0].base = 0;
810 memblock.memory.regions[0].size = 0;
811 memblock.memory.cnt = 1;
812
813 /* Ditto. */
814 memblock.reserved.regions[0].base = 0;
815 memblock.reserved.regions[0].size = 0;
816 memblock.reserved.cnt = 1;
817
818 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
819}
820
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000821static int __init early_memblock(char *p)
822{
823 if (p && strstr(p, "debug"))
824 memblock_debug = 1;
825 return 0;
826}
827early_param("memblock", early_memblock);
828
Yinghai Lu10d06432010-07-28 15:43:02 +1000829#if defined(CONFIG_DEBUG_FS) && !defined(ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -0700830
831static int memblock_debug_show(struct seq_file *m, void *private)
832{
833 struct memblock_type *type = m->private;
834 struct memblock_region *reg;
835 int i;
836
837 for (i = 0; i < type->cnt; i++) {
838 reg = &type->regions[i];
839 seq_printf(m, "%4d: ", i);
840 if (sizeof(phys_addr_t) == 4)
841 seq_printf(m, "0x%08lx..0x%08lx\n",
842 (unsigned long)reg->base,
843 (unsigned long)(reg->base + reg->size - 1));
844 else
845 seq_printf(m, "0x%016llx..0x%016llx\n",
846 (unsigned long long)reg->base,
847 (unsigned long long)(reg->base + reg->size - 1));
848
849 }
850 return 0;
851}
852
853static int memblock_debug_open(struct inode *inode, struct file *file)
854{
855 return single_open(file, memblock_debug_show, inode->i_private);
856}
857
858static const struct file_operations memblock_debug_fops = {
859 .open = memblock_debug_open,
860 .read = seq_read,
861 .llseek = seq_lseek,
862 .release = single_release,
863};
864
865static int __init memblock_init_debugfs(void)
866{
867 struct dentry *root = debugfs_create_dir("memblock", NULL);
868 if (!root)
869 return -ENXIO;
870 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
871 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
872
873 return 0;
874}
875__initcall(memblock_init_debugfs);
876
877#endif /* CONFIG_DEBUG_FS */