blob: b7ab10a2ef4648ebfc25cb75a1b369e0f4bf8184 [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
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100041/*
42 * Address comparison utilities
43 */
44
Yinghai Lu10d06432010-07-28 15:43:02 +100045static phys_addr_t __init_memblock memblock_align_down(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +100046{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100047 return addr & ~(size - 1);
Yinghai Lu95f72d12010-07-12 14:36:09 +100048}
49
Yinghai Lu10d06432010-07-28 15:43:02 +100050static phys_addr_t __init_memblock memblock_align_up(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +100051{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100052 return (addr + (size - 1)) & ~(size - 1);
Yinghai Lu95f72d12010-07-12 14:36:09 +100053}
54
Yinghai Lu10d06432010-07-28 15:43:02 +100055static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100056 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100057{
58 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
59}
60
Yinghai Lu10d06432010-07-28 15:43:02 +100061static long __init_memblock memblock_addrs_adjacent(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 if (base2 == base1 + size1)
65 return 1;
66 else if (base1 == base2 + size2)
67 return -1;
68
69 return 0;
70}
71
Yinghai Lu10d06432010-07-28 15:43:02 +100072static long __init_memblock memblock_regions_adjacent(struct memblock_type *type,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100073 unsigned long r1, unsigned long r2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100074{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100075 phys_addr_t base1 = type->regions[r1].base;
76 phys_addr_t size1 = type->regions[r1].size;
77 phys_addr_t base2 = type->regions[r2].base;
78 phys_addr_t size2 = type->regions[r2].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100079
80 return memblock_addrs_adjacent(base1, size1, base2, size2);
81}
82
Yinghai Lu10d06432010-07-28 15:43:02 +100083long __init_memblock memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100084{
85 unsigned long i;
86
87 for (i = 0; i < type->cnt; i++) {
88 phys_addr_t rgnbase = type->regions[i].base;
89 phys_addr_t rgnsize = type->regions[i].size;
90 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
91 break;
92 }
93
94 return (i < type->cnt) ? i : -1;
95}
96
97/*
98 * Find, allocate, deallocate or reserve unreserved regions. All allocations
99 * are top-down.
100 */
101
102static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end,
103 phys_addr_t size, phys_addr_t align)
104{
105 phys_addr_t base, res_base;
106 long j;
107
Benjamin Herrenschmidt25818f02010-07-28 15:25:10 +1000108 /* Prevent allocations returning 0 as it's also used to
109 * indicate an allocation failure
110 */
111 if (start == 0)
112 start = PAGE_SIZE;
113
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000114 base = memblock_align_down((end - size), align);
115 while (start <= base) {
116 j = memblock_overlaps_region(&memblock.reserved, base, size);
117 if (j < 0)
118 return base;
119 res_base = memblock.reserved.regions[j].base;
120 if (res_base < size)
121 break;
122 base = memblock_align_down(res_base - size, align);
123 }
124
125 return MEMBLOCK_ERROR;
126}
127
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000128static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align,
129 phys_addr_t start, phys_addr_t end)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000130{
131 long i;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000132
133 BUG_ON(0 == size);
134
135 size = memblock_align_up(size, align);
136
137 /* Pump up max_addr */
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000138 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
139 end = memblock.current_limit;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000140
141 /* We do a top-down search, this tends to limit memory
142 * fragmentation by keeping early boot allocs near the
143 * top of memory
144 */
145 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
146 phys_addr_t memblockbase = memblock.memory.regions[i].base;
147 phys_addr_t memblocksize = memblock.memory.regions[i].size;
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000148 phys_addr_t bottom, top, found;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000149
150 if (memblocksize < size)
151 continue;
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000152 if ((memblockbase + memblocksize) <= start)
153 break;
154 bottom = max(memblockbase, start);
155 top = min(memblockbase + memblocksize, end);
156 if (bottom >= top)
157 continue;
158 found = memblock_find_region(bottom, top, size, align);
159 if (found != MEMBLOCK_ERROR)
160 return found;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000161 }
162 return MEMBLOCK_ERROR;
163}
164
Yinghai Lu5303b682010-07-28 15:38:40 +1000165/*
166 * Find a free area with specified alignment in a specific range.
167 */
168u64 __init_memblock memblock_find_in_range(u64 start, u64 end, u64 size, u64 align)
169{
170 return memblock_find_base(size, align, start, end);
171}
172
Yinghai Lu10d06432010-07-28 15:43:02 +1000173static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000174{
175 unsigned long i;
176
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000177 for (i = r; i < type->cnt - 1; i++) {
178 type->regions[i].base = type->regions[i + 1].base;
179 type->regions[i].size = type->regions[i + 1].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000180 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000181 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000182}
183
184/* Assumption: base addr of region 1 < base addr of region 2 */
Yinghai Lu10d06432010-07-28 15:43:02 +1000185static void __init_memblock memblock_coalesce_regions(struct memblock_type *type,
Yinghai Lu95f72d12010-07-12 14:36:09 +1000186 unsigned long r1, unsigned long r2)
187{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000188 type->regions[r1].size += type->regions[r2].size;
189 memblock_remove_region(type, r2);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000190}
191
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700192/* Defined below but needed now */
193static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size);
194
Yinghai Lu10d06432010-07-28 15:43:02 +1000195static int __init_memblock memblock_double_array(struct memblock_type *type)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700196{
197 struct memblock_region *new_array, *old_array;
198 phys_addr_t old_size, new_size, addr;
199 int use_slab = slab_is_available();
200
201 /* We don't allow resizing until we know about the reserved regions
202 * of memory that aren't suitable for allocation
203 */
204 if (!memblock_can_resize)
205 return -1;
206
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700207 /* Calculate new doubled size */
208 old_size = type->max * sizeof(struct memblock_region);
209 new_size = old_size << 1;
210
211 /* Try to find some space for it.
212 *
213 * WARNING: We assume that either slab_is_available() and we use it or
214 * we use MEMBLOCK for allocations. That means that this is unsafe to use
215 * when bootmem is currently active (unless bootmem itself is implemented
216 * on top of MEMBLOCK which isn't the case yet)
217 *
218 * This should however not be an issue for now, as we currently only
219 * call into MEMBLOCK while it's still active, or much later when slab is
220 * active for memory hotplug operations
221 */
222 if (use_slab) {
223 new_array = kmalloc(new_size, GFP_KERNEL);
224 addr = new_array == NULL ? MEMBLOCK_ERROR : __pa(new_array);
225 } else
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000226 addr = memblock_find_base(new_size, sizeof(phys_addr_t), 0, MEMBLOCK_ALLOC_ACCESSIBLE);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700227 if (addr == MEMBLOCK_ERROR) {
228 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
229 memblock_type_name(type), type->max, type->max * 2);
230 return -1;
231 }
232 new_array = __va(addr);
233
Yinghai Luea9e4372010-07-28 15:13:22 +1000234 memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]",
235 memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1);
236
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700237 /* Found space, we now need to move the array over before
238 * we add the reserved region since it may be our reserved
239 * array itself that is full.
240 */
241 memcpy(new_array, type->regions, old_size);
242 memset(new_array + type->max, 0, old_size);
243 old_array = type->regions;
244 type->regions = new_array;
245 type->max <<= 1;
246
247 /* If we use SLAB that's it, we are done */
248 if (use_slab)
249 return 0;
250
251 /* Add the new reserved region now. Should not fail ! */
252 BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size) < 0);
253
254 /* If the array wasn't our static init one, then free it. We only do
255 * that before SLAB is available as later on, we don't know whether
256 * to use kfree or free_bootmem_pages(). Shouldn't be a big deal
257 * anyways
258 */
259 if (old_array != memblock_memory_init_regions &&
260 old_array != memblock_reserved_init_regions)
261 memblock_free(__pa(old_array), old_size);
262
263 return 0;
264}
265
Yinghai Lu10d06432010-07-28 15:43:02 +1000266extern int __init_memblock __weak memblock_memory_can_coalesce(phys_addr_t addr1, phys_addr_t size1,
Benjamin Herrenschmidtd2cd5632010-07-06 15:39:14 -0700267 phys_addr_t addr2, phys_addr_t size2)
268{
269 return 1;
270}
271
Yinghai Lu10d06432010-07-28 15:43:02 +1000272static long __init_memblock memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000273{
274 unsigned long coalesced = 0;
275 long adjacent, i;
276
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000277 if ((type->cnt == 1) && (type->regions[0].size == 0)) {
278 type->regions[0].base = base;
279 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000280 return 0;
281 }
282
283 /* First try and coalesce this MEMBLOCK with another. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000284 for (i = 0; i < type->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000285 phys_addr_t rgnbase = type->regions[i].base;
286 phys_addr_t rgnsize = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000287
288 if ((rgnbase == base) && (rgnsize == size))
289 /* Already have this region, so we're done */
290 return 0;
291
292 adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
Benjamin Herrenschmidtd2cd5632010-07-06 15:39:14 -0700293 /* Check if arch allows coalescing */
294 if (adjacent != 0 && type == &memblock.memory &&
295 !memblock_memory_can_coalesce(base, size, rgnbase, rgnsize))
296 break;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000297 if (adjacent > 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000298 type->regions[i].base -= size;
299 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000300 coalesced++;
301 break;
302 } else if (adjacent < 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000303 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000304 coalesced++;
305 break;
306 }
307 }
308
Benjamin Herrenschmidtd2cd5632010-07-06 15:39:14 -0700309 /* If we plugged a hole, we may want to also coalesce with the
310 * next region
311 */
312 if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1) &&
313 ((type != &memblock.memory || memblock_memory_can_coalesce(type->regions[i].base,
314 type->regions[i].size,
315 type->regions[i+1].base,
316 type->regions[i+1].size)))) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000317 memblock_coalesce_regions(type, i, i+1);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000318 coalesced++;
319 }
320
321 if (coalesced)
322 return coalesced;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700323
324 /* If we are out of space, we fail. It's too late to resize the array
325 * but then this shouldn't have happened in the first place.
326 */
327 if (WARN_ON(type->cnt >= type->max))
Yinghai Lu95f72d12010-07-12 14:36:09 +1000328 return -1;
329
330 /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000331 for (i = type->cnt - 1; i >= 0; i--) {
332 if (base < type->regions[i].base) {
333 type->regions[i+1].base = type->regions[i].base;
334 type->regions[i+1].size = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000335 } else {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000336 type->regions[i+1].base = base;
337 type->regions[i+1].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000338 break;
339 }
340 }
341
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000342 if (base < type->regions[0].base) {
343 type->regions[0].base = base;
344 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000345 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000346 type->cnt++;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000347
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700348 /* The array is full ? Try to resize it. If that fails, we undo
349 * our allocation and return an error
350 */
351 if (type->cnt == type->max && memblock_double_array(type)) {
352 type->cnt--;
353 return -1;
354 }
355
Yinghai Lu95f72d12010-07-12 14:36:09 +1000356 return 0;
357}
358
Yinghai Lu10d06432010-07-28 15:43:02 +1000359long __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000360{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000361 return memblock_add_region(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000362
363}
364
Yinghai Lu10d06432010-07-28 15:43:02 +1000365static long __init_memblock __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000366{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000367 phys_addr_t rgnbegin, rgnend;
368 phys_addr_t end = base + size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000369 int i;
370
371 rgnbegin = rgnend = 0; /* supress gcc warnings */
372
373 /* Find the region where (base, size) belongs to */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000374 for (i=0; i < type->cnt; i++) {
375 rgnbegin = type->regions[i].base;
376 rgnend = rgnbegin + type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000377
378 if ((rgnbegin <= base) && (end <= rgnend))
379 break;
380 }
381
382 /* Didn't find the region */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000383 if (i == type->cnt)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000384 return -1;
385
386 /* Check to see if we are removing entire region */
387 if ((rgnbegin == base) && (rgnend == end)) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000388 memblock_remove_region(type, i);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000389 return 0;
390 }
391
392 /* Check to see if region is matching at the front */
393 if (rgnbegin == base) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000394 type->regions[i].base = end;
395 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000396 return 0;
397 }
398
399 /* Check to see if the region is matching at the end */
400 if (rgnend == end) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000401 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000402 return 0;
403 }
404
405 /*
406 * We need to split the entry - adjust the current one to the
407 * beginging of the hole and add the region after hole.
408 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000409 type->regions[i].size = base - type->regions[i].base;
410 return memblock_add_region(type, end, rgnend - end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000411}
412
Yinghai Lu10d06432010-07-28 15:43:02 +1000413long __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000414{
415 return __memblock_remove(&memblock.memory, base, size);
416}
417
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000418long __init memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000419{
420 return __memblock_remove(&memblock.reserved, base, size);
421}
422
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000423long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000424{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000425 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000426
427 BUG_ON(0 == size);
428
429 return memblock_add_region(_rgn, base, size);
430}
431
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000432phys_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 +1000433{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000434 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000435
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000436 /* We align the size to limit fragmentation. Without this, a lot of
437 * small allocs quickly eat up the whole reserve array on sparc
438 */
439 size = memblock_align_up(size, align);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000440
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000441 found = memblock_find_base(size, align, 0, max_addr);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000442 if (found != MEMBLOCK_ERROR &&
443 memblock_add_region(&memblock.reserved, found, size) >= 0)
444 return found;
445
446 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000447}
448
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000449phys_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 +1000450{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000451 phys_addr_t alloc;
452
453 alloc = __memblock_alloc_base(size, align, max_addr);
454
455 if (alloc == 0)
456 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
457 (unsigned long long) size, (unsigned long long) max_addr);
458
459 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000460}
461
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000462phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000463{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000464 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000465}
466
Yinghai Lu95f72d12010-07-12 14:36:09 +1000467
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000468/*
469 * Additional node-local allocators. Search for node memory is bottom up
470 * and walks memblock regions within that node bottom-up as well, but allocation
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700471 * within an memblock region is top-down. XXX I plan to fix that at some stage
472 *
473 * WARNING: Only available after early_node_map[] has been populated,
474 * on some architectures, that is after all the calls to add_active_range()
475 * have been done to populate it.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000476 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000477
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000478phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700479{
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700480#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
481 /*
482 * This code originates from sparc which really wants use to walk by addresses
483 * and returns the nid. This is not very convenient for early_pfn_map[] users
484 * as the map isn't sorted yet, and it really wants to be walked by nid.
485 *
486 * For now, I implement the inefficient method below which walks the early
487 * map multiple times. Eventually we may want to use an ARCH config option
488 * to implement a completely different method for both case.
489 */
490 unsigned long start_pfn, end_pfn;
491 int i;
492
493 for (i = 0; i < MAX_NUMNODES; i++) {
494 get_pfn_range_for_nid(i, &start_pfn, &end_pfn);
495 if (start < PFN_PHYS(start_pfn) || start >= PFN_PHYS(end_pfn))
496 continue;
497 *nid = i;
498 return min(end, PFN_PHYS(end_pfn));
499 }
500#endif
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700501 *nid = 0;
502
503 return end;
504}
505
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000506static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
507 phys_addr_t size,
508 phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000509{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000510 phys_addr_t start, end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000511
512 start = mp->base;
513 end = start + mp->size;
514
515 start = memblock_align_up(start, align);
516 while (start < end) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000517 phys_addr_t this_end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000518 int this_nid;
519
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700520 this_end = memblock_nid_range(start, end, &this_nid);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000521 if (this_nid == nid) {
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000522 phys_addr_t ret = memblock_find_region(start, this_end, size, align);
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700523 if (ret != MEMBLOCK_ERROR &&
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000524 memblock_add_region(&memblock.reserved, ret, size) >= 0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000525 return ret;
526 }
527 start = this_end;
528 }
529
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700530 return MEMBLOCK_ERROR;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000531}
532
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000533phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000534{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000535 struct memblock_type *mem = &memblock.memory;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000536 int i;
537
538 BUG_ON(0 == size);
539
Benjamin Herrenschmidt7f219c72010-07-12 14:24:57 +1000540 /* We align the size to limit fragmentation. Without this, a lot of
541 * small allocs quickly eat up the whole reserve array on sparc
542 */
543 size = memblock_align_up(size, align);
544
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700545 /* We do a bottom-up search for a region with the right
546 * nid since that's easier considering how memblock_nid_range()
547 * works
548 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000549 for (i = 0; i < mem->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000550 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
Yinghai Lu95f72d12010-07-12 14:36:09 +1000551 size, align, nid);
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700552 if (ret != MEMBLOCK_ERROR)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000553 return ret;
554 }
555
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700556 return 0;
557}
558
559phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
560{
561 phys_addr_t res = memblock_alloc_nid(size, align, nid);
562
563 if (res)
564 return res;
Benjamin Herrenschmidt918fe8d2010-07-06 15:39:18 -0700565 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ANYWHERE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000566}
567
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700568
569/*
570 * Remaining API functions
571 */
572
Yinghai Lu95f72d12010-07-12 14:36:09 +1000573/* You must call memblock_analyze() before this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000574phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000575{
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000576 return memblock.memory_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000577}
578
Yinghai Lu10d06432010-07-28 15:43:02 +1000579phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000580{
581 int idx = memblock.memory.cnt - 1;
582
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000583 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000584}
585
586/* You must call memblock_analyze() after this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000587void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000588{
589 unsigned long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000590 phys_addr_t limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000591 struct memblock_region *p;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000592
593 if (!memory_limit)
594 return;
595
596 /* Truncate the memblock regions to satisfy the memory limit. */
597 limit = memory_limit;
598 for (i = 0; i < memblock.memory.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000599 if (limit > memblock.memory.regions[i].size) {
600 limit -= memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000601 continue;
602 }
603
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000604 memblock.memory.regions[i].size = limit;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000605 memblock.memory.cnt = i + 1;
606 break;
607 }
608
Yinghai Lu95f72d12010-07-12 14:36:09 +1000609 memory_limit = memblock_end_of_DRAM();
610
611 /* And truncate any reserves above the limit also. */
612 for (i = 0; i < memblock.reserved.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000613 p = &memblock.reserved.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +1000614
615 if (p->base > memory_limit)
616 p->size = 0;
617 else if ((p->base + p->size) > memory_limit)
618 p->size = memory_limit - p->base;
619
620 if (p->size == 0) {
621 memblock_remove_region(&memblock.reserved, i);
622 i--;
623 }
624 }
625}
626
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000627static int memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000628{
629 unsigned int left = 0, right = type->cnt;
630
631 do {
632 unsigned int mid = (right + left) / 2;
633
634 if (addr < type->regions[mid].base)
635 right = mid;
636 else if (addr >= (type->regions[mid].base +
637 type->regions[mid].size))
638 left = mid + 1;
639 else
640 return mid;
641 } while (left < right);
642 return -1;
643}
644
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000645int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000646{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000647 return memblock_search(&memblock.reserved, addr) != -1;
648}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000649
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000650int memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000651{
652 return memblock_search(&memblock.memory, addr) != -1;
653}
654
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000655int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000656{
657 int idx = memblock_search(&memblock.reserved, base);
658
659 if (idx == -1)
660 return 0;
661 return memblock.reserved.regions[idx].base <= base &&
662 (memblock.reserved.regions[idx].base +
663 memblock.reserved.regions[idx].size) >= (base + size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000664}
665
Yinghai Lu10d06432010-07-28 15:43:02 +1000666int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000667{
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +1000668 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000669}
670
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700671
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000672void __init memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700673{
674 memblock.current_limit = limit;
675}
676
Yinghai Lu10d06432010-07-28 15:43:02 +1000677static void __init_memblock memblock_dump(struct memblock_type *region, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000678{
679 unsigned long long base, size;
680 int i;
681
682 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
683
684 for (i = 0; i < region->cnt; i++) {
685 base = region->regions[i].base;
686 size = region->regions[i].size;
687
Yinghai Luea9e4372010-07-28 15:13:22 +1000688 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes\n",
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000689 name, i, base, base + size - 1, size);
690 }
691}
692
Yinghai Lu10d06432010-07-28 15:43:02 +1000693void __init_memblock memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000694{
695 if (!memblock_debug)
696 return;
697
698 pr_info("MEMBLOCK configuration:\n");
699 pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
700
701 memblock_dump(&memblock.memory, "memory");
702 memblock_dump(&memblock.reserved, "reserved");
703}
704
705void __init memblock_analyze(void)
706{
707 int i;
708
709 /* Check marker in the unused last array entry */
710 WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
711 != (phys_addr_t)RED_INACTIVE);
712 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
713 != (phys_addr_t)RED_INACTIVE);
714
715 memblock.memory_size = 0;
716
717 for (i = 0; i < memblock.memory.cnt; i++)
718 memblock.memory_size += memblock.memory.regions[i].size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700719
720 /* We allow resizing from there */
721 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000722}
723
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700724void __init memblock_init(void)
725{
726 /* Hookup the initial arrays */
727 memblock.memory.regions = memblock_memory_init_regions;
728 memblock.memory.max = INIT_MEMBLOCK_REGIONS;
729 memblock.reserved.regions = memblock_reserved_init_regions;
730 memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
731
732 /* Write a marker in the unused last array entry */
733 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
734 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
735
736 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
737 * This simplifies the memblock_add() code below...
738 */
739 memblock.memory.regions[0].base = 0;
740 memblock.memory.regions[0].size = 0;
741 memblock.memory.cnt = 1;
742
743 /* Ditto. */
744 memblock.reserved.regions[0].base = 0;
745 memblock.reserved.regions[0].size = 0;
746 memblock.reserved.cnt = 1;
747
748 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
749}
750
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000751static int __init early_memblock(char *p)
752{
753 if (p && strstr(p, "debug"))
754 memblock_debug = 1;
755 return 0;
756}
757early_param("memblock", early_memblock);
758
Yinghai Lu10d06432010-07-28 15:43:02 +1000759#if defined(CONFIG_DEBUG_FS) && !defined(ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -0700760
761static int memblock_debug_show(struct seq_file *m, void *private)
762{
763 struct memblock_type *type = m->private;
764 struct memblock_region *reg;
765 int i;
766
767 for (i = 0; i < type->cnt; i++) {
768 reg = &type->regions[i];
769 seq_printf(m, "%4d: ", i);
770 if (sizeof(phys_addr_t) == 4)
771 seq_printf(m, "0x%08lx..0x%08lx\n",
772 (unsigned long)reg->base,
773 (unsigned long)(reg->base + reg->size - 1));
774 else
775 seq_printf(m, "0x%016llx..0x%016llx\n",
776 (unsigned long long)reg->base,
777 (unsigned long long)(reg->base + reg->size - 1));
778
779 }
780 return 0;
781}
782
783static int memblock_debug_open(struct inode *inode, struct file *file)
784{
785 return single_open(file, memblock_debug_show, inode->i_private);
786}
787
788static const struct file_operations memblock_debug_fops = {
789 .open = memblock_debug_open,
790 .read = seq_read,
791 .llseek = seq_lseek,
792 .release = single_release,
793};
794
795static int __init memblock_init_debugfs(void)
796{
797 struct dentry *root = debugfs_create_dir("memblock", NULL);
798 if (!root)
799 return -ENXIO;
800 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
801 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
802
803 return 0;
804}
805__initcall(memblock_init_debugfs);
806
807#endif /* CONFIG_DEBUG_FS */