blob: af7e4d9cf400b1109a48c2f6b4f082969569d38a [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>
Yinghai Lu95f72d12010-07-12 14:36:09 +100019#include <linux/memblock.h>
20
Yinghai Lu95f72d12010-07-12 14:36:09 +100021struct memblock memblock;
22
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070023static int memblock_debug, memblock_can_resize;
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -070024static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1];
25static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1];
Yinghai Lu95f72d12010-07-12 14:36:09 +100026
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -070027#define MEMBLOCK_ERROR (~(phys_addr_t)0)
28
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070029/* inline so we don't get a warning when pr_debug is compiled out */
30static inline const char *memblock_type_name(struct memblock_type *type)
31{
32 if (type == &memblock.memory)
33 return "memory";
34 else if (type == &memblock.reserved)
35 return "reserved";
36 else
37 return "unknown";
38}
39
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100040/*
41 * Address comparison utilities
42 */
43
44static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +100045{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100046 return addr & ~(size - 1);
Yinghai Lu95f72d12010-07-12 14:36:09 +100047}
48
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100049static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +100050{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100051 return (addr + (size - 1)) & ~(size - 1);
Yinghai Lu95f72d12010-07-12 14:36:09 +100052}
53
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100054static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
55 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100056{
57 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
58}
59
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100060static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
61 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100062{
63 if (base2 == base1 + size1)
64 return 1;
65 else if (base1 == base2 + size2)
66 return -1;
67
68 return 0;
69}
70
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100071static long memblock_regions_adjacent(struct memblock_type *type,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100072 unsigned long r1, unsigned long r2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100073{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100074 phys_addr_t base1 = type->regions[r1].base;
75 phys_addr_t size1 = type->regions[r1].size;
76 phys_addr_t base2 = type->regions[r2].base;
77 phys_addr_t size2 = type->regions[r2].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100078
79 return memblock_addrs_adjacent(base1, size1, base2, size2);
80}
81
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100082long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
83{
84 unsigned long i;
85
86 for (i = 0; i < type->cnt; i++) {
87 phys_addr_t rgnbase = type->regions[i].base;
88 phys_addr_t rgnsize = type->regions[i].size;
89 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
90 break;
91 }
92
93 return (i < type->cnt) ? i : -1;
94}
95
96/*
97 * Find, allocate, deallocate or reserve unreserved regions. All allocations
98 * are top-down.
99 */
100
101static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end,
102 phys_addr_t size, phys_addr_t align)
103{
104 phys_addr_t base, res_base;
105 long j;
106
107 base = memblock_align_down((end - size), align);
108 while (start <= base) {
109 j = memblock_overlaps_region(&memblock.reserved, base, size);
110 if (j < 0)
111 return base;
112 res_base = memblock.reserved.regions[j].base;
113 if (res_base < size)
114 break;
115 base = memblock_align_down(res_base - size, align);
116 }
117
118 return MEMBLOCK_ERROR;
119}
120
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000121static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align,
122 phys_addr_t start, phys_addr_t end)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000123{
124 long i;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000125
126 BUG_ON(0 == size);
127
128 size = memblock_align_up(size, align);
129
130 /* Pump up max_addr */
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000131 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
132 end = memblock.current_limit;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000133
134 /* We do a top-down search, this tends to limit memory
135 * fragmentation by keeping early boot allocs near the
136 * top of memory
137 */
138 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
139 phys_addr_t memblockbase = memblock.memory.regions[i].base;
140 phys_addr_t memblocksize = memblock.memory.regions[i].size;
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000141 phys_addr_t bottom, top, found;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000142
143 if (memblocksize < size)
144 continue;
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000145 if ((memblockbase + memblocksize) <= start)
146 break;
147 bottom = max(memblockbase, start);
148 top = min(memblockbase + memblocksize, end);
149 if (bottom >= top)
150 continue;
151 found = memblock_find_region(bottom, top, size, align);
152 if (found != MEMBLOCK_ERROR)
153 return found;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000154 }
155 return MEMBLOCK_ERROR;
156}
157
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000158static void memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000159{
160 unsigned long i;
161
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000162 for (i = r; i < type->cnt - 1; i++) {
163 type->regions[i].base = type->regions[i + 1].base;
164 type->regions[i].size = type->regions[i + 1].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000165 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000166 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000167}
168
169/* Assumption: base addr of region 1 < base addr of region 2 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000170static void memblock_coalesce_regions(struct memblock_type *type,
Yinghai Lu95f72d12010-07-12 14:36:09 +1000171 unsigned long r1, unsigned long r2)
172{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000173 type->regions[r1].size += type->regions[r2].size;
174 memblock_remove_region(type, r2);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000175}
176
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700177/* Defined below but needed now */
178static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size);
179
180static int memblock_double_array(struct memblock_type *type)
181{
182 struct memblock_region *new_array, *old_array;
183 phys_addr_t old_size, new_size, addr;
184 int use_slab = slab_is_available();
185
186 /* We don't allow resizing until we know about the reserved regions
187 * of memory that aren't suitable for allocation
188 */
189 if (!memblock_can_resize)
190 return -1;
191
192 pr_debug("memblock: %s array full, doubling...", memblock_type_name(type));
193
194 /* Calculate new doubled size */
195 old_size = type->max * sizeof(struct memblock_region);
196 new_size = old_size << 1;
197
198 /* Try to find some space for it.
199 *
200 * WARNING: We assume that either slab_is_available() and we use it or
201 * we use MEMBLOCK for allocations. That means that this is unsafe to use
202 * when bootmem is currently active (unless bootmem itself is implemented
203 * on top of MEMBLOCK which isn't the case yet)
204 *
205 * This should however not be an issue for now, as we currently only
206 * call into MEMBLOCK while it's still active, or much later when slab is
207 * active for memory hotplug operations
208 */
209 if (use_slab) {
210 new_array = kmalloc(new_size, GFP_KERNEL);
211 addr = new_array == NULL ? MEMBLOCK_ERROR : __pa(new_array);
212 } else
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000213 addr = memblock_find_base(new_size, sizeof(phys_addr_t), 0, MEMBLOCK_ALLOC_ACCESSIBLE);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700214 if (addr == MEMBLOCK_ERROR) {
215 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
216 memblock_type_name(type), type->max, type->max * 2);
217 return -1;
218 }
219 new_array = __va(addr);
220
221 /* Found space, we now need to move the array over before
222 * we add the reserved region since it may be our reserved
223 * array itself that is full.
224 */
225 memcpy(new_array, type->regions, old_size);
226 memset(new_array + type->max, 0, old_size);
227 old_array = type->regions;
228 type->regions = new_array;
229 type->max <<= 1;
230
231 /* If we use SLAB that's it, we are done */
232 if (use_slab)
233 return 0;
234
235 /* Add the new reserved region now. Should not fail ! */
236 BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size) < 0);
237
238 /* If the array wasn't our static init one, then free it. We only do
239 * that before SLAB is available as later on, we don't know whether
240 * to use kfree or free_bootmem_pages(). Shouldn't be a big deal
241 * anyways
242 */
243 if (old_array != memblock_memory_init_regions &&
244 old_array != memblock_reserved_init_regions)
245 memblock_free(__pa(old_array), old_size);
246
247 return 0;
248}
249
Benjamin Herrenschmidtd2cd5632010-07-06 15:39:14 -0700250extern int __weak memblock_memory_can_coalesce(phys_addr_t addr1, phys_addr_t size1,
251 phys_addr_t addr2, phys_addr_t size2)
252{
253 return 1;
254}
255
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000256static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000257{
258 unsigned long coalesced = 0;
259 long adjacent, i;
260
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000261 if ((type->cnt == 1) && (type->regions[0].size == 0)) {
262 type->regions[0].base = base;
263 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000264 return 0;
265 }
266
267 /* First try and coalesce this MEMBLOCK with another. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000268 for (i = 0; i < type->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000269 phys_addr_t rgnbase = type->regions[i].base;
270 phys_addr_t rgnsize = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000271
272 if ((rgnbase == base) && (rgnsize == size))
273 /* Already have this region, so we're done */
274 return 0;
275
276 adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
Benjamin Herrenschmidtd2cd5632010-07-06 15:39:14 -0700277 /* Check if arch allows coalescing */
278 if (adjacent != 0 && type == &memblock.memory &&
279 !memblock_memory_can_coalesce(base, size, rgnbase, rgnsize))
280 break;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000281 if (adjacent > 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000282 type->regions[i].base -= size;
283 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000284 coalesced++;
285 break;
286 } else if (adjacent < 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000287 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000288 coalesced++;
289 break;
290 }
291 }
292
Benjamin Herrenschmidtd2cd5632010-07-06 15:39:14 -0700293 /* If we plugged a hole, we may want to also coalesce with the
294 * next region
295 */
296 if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1) &&
297 ((type != &memblock.memory || memblock_memory_can_coalesce(type->regions[i].base,
298 type->regions[i].size,
299 type->regions[i+1].base,
300 type->regions[i+1].size)))) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000301 memblock_coalesce_regions(type, i, i+1);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000302 coalesced++;
303 }
304
305 if (coalesced)
306 return coalesced;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700307
308 /* If we are out of space, we fail. It's too late to resize the array
309 * but then this shouldn't have happened in the first place.
310 */
311 if (WARN_ON(type->cnt >= type->max))
Yinghai Lu95f72d12010-07-12 14:36:09 +1000312 return -1;
313
314 /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000315 for (i = type->cnt - 1; i >= 0; i--) {
316 if (base < type->regions[i].base) {
317 type->regions[i+1].base = type->regions[i].base;
318 type->regions[i+1].size = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000319 } else {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000320 type->regions[i+1].base = base;
321 type->regions[i+1].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000322 break;
323 }
324 }
325
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000326 if (base < type->regions[0].base) {
327 type->regions[0].base = base;
328 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000329 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000330 type->cnt++;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000331
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700332 /* The array is full ? Try to resize it. If that fails, we undo
333 * our allocation and return an error
334 */
335 if (type->cnt == type->max && memblock_double_array(type)) {
336 type->cnt--;
337 return -1;
338 }
339
Yinghai Lu95f72d12010-07-12 14:36:09 +1000340 return 0;
341}
342
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000343long memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000344{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000345 return memblock_add_region(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000346
347}
348
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000349static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000350{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000351 phys_addr_t rgnbegin, rgnend;
352 phys_addr_t end = base + size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000353 int i;
354
355 rgnbegin = rgnend = 0; /* supress gcc warnings */
356
357 /* Find the region where (base, size) belongs to */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000358 for (i=0; i < type->cnt; i++) {
359 rgnbegin = type->regions[i].base;
360 rgnend = rgnbegin + type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000361
362 if ((rgnbegin <= base) && (end <= rgnend))
363 break;
364 }
365
366 /* Didn't find the region */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000367 if (i == type->cnt)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000368 return -1;
369
370 /* Check to see if we are removing entire region */
371 if ((rgnbegin == base) && (rgnend == end)) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000372 memblock_remove_region(type, i);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000373 return 0;
374 }
375
376 /* Check to see if region is matching at the front */
377 if (rgnbegin == base) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000378 type->regions[i].base = end;
379 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000380 return 0;
381 }
382
383 /* Check to see if the region is matching at the end */
384 if (rgnend == end) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000385 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000386 return 0;
387 }
388
389 /*
390 * We need to split the entry - adjust the current one to the
391 * beginging of the hole and add the region after hole.
392 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000393 type->regions[i].size = base - type->regions[i].base;
394 return memblock_add_region(type, end, rgnend - end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000395}
396
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000397long memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000398{
399 return __memblock_remove(&memblock.memory, base, size);
400}
401
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000402long __init memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000403{
404 return __memblock_remove(&memblock.reserved, base, size);
405}
406
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000407long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000408{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000409 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000410
411 BUG_ON(0 == size);
412
413 return memblock_add_region(_rgn, base, size);
414}
415
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000416phys_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 +1000417{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000418 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000419
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000420 /* We align the size to limit fragmentation. Without this, a lot of
421 * small allocs quickly eat up the whole reserve array on sparc
422 */
423 size = memblock_align_up(size, align);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000424
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000425 found = memblock_find_base(size, align, 0, max_addr);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000426 if (found != MEMBLOCK_ERROR &&
427 memblock_add_region(&memblock.reserved, found, size) >= 0)
428 return found;
429
430 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000431}
432
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000433phys_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 +1000434{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000435 phys_addr_t alloc;
436
437 alloc = __memblock_alloc_base(size, align, max_addr);
438
439 if (alloc == 0)
440 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
441 (unsigned long long) size, (unsigned long long) max_addr);
442
443 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000444}
445
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000446phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000447{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000448 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000449}
450
Yinghai Lu95f72d12010-07-12 14:36:09 +1000451
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000452/*
453 * Additional node-local allocators. Search for node memory is bottom up
454 * and walks memblock regions within that node bottom-up as well, but allocation
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700455 * within an memblock region is top-down. XXX I plan to fix that at some stage
456 *
457 * WARNING: Only available after early_node_map[] has been populated,
458 * on some architectures, that is after all the calls to add_active_range()
459 * have been done to populate it.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000460 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000461
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000462phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700463{
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700464#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
465 /*
466 * This code originates from sparc which really wants use to walk by addresses
467 * and returns the nid. This is not very convenient for early_pfn_map[] users
468 * as the map isn't sorted yet, and it really wants to be walked by nid.
469 *
470 * For now, I implement the inefficient method below which walks the early
471 * map multiple times. Eventually we may want to use an ARCH config option
472 * to implement a completely different method for both case.
473 */
474 unsigned long start_pfn, end_pfn;
475 int i;
476
477 for (i = 0; i < MAX_NUMNODES; i++) {
478 get_pfn_range_for_nid(i, &start_pfn, &end_pfn);
479 if (start < PFN_PHYS(start_pfn) || start >= PFN_PHYS(end_pfn))
480 continue;
481 *nid = i;
482 return min(end, PFN_PHYS(end_pfn));
483 }
484#endif
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700485 *nid = 0;
486
487 return end;
488}
489
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000490static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
491 phys_addr_t size,
492 phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000493{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000494 phys_addr_t start, end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000495
496 start = mp->base;
497 end = start + mp->size;
498
499 start = memblock_align_up(start, align);
500 while (start < end) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000501 phys_addr_t this_end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000502 int this_nid;
503
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700504 this_end = memblock_nid_range(start, end, &this_nid);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000505 if (this_nid == nid) {
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000506 phys_addr_t ret = memblock_find_region(start, this_end, size, align);
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700507 if (ret != MEMBLOCK_ERROR &&
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000508 memblock_add_region(&memblock.reserved, ret, size) >= 0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000509 return ret;
510 }
511 start = this_end;
512 }
513
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700514 return MEMBLOCK_ERROR;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000515}
516
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000517phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000518{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000519 struct memblock_type *mem = &memblock.memory;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000520 int i;
521
522 BUG_ON(0 == size);
523
Benjamin Herrenschmidt7f219c72010-07-12 14:24:57 +1000524 /* We align the size to limit fragmentation. Without this, a lot of
525 * small allocs quickly eat up the whole reserve array on sparc
526 */
527 size = memblock_align_up(size, align);
528
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700529 /* We do a bottom-up search for a region with the right
530 * nid since that's easier considering how memblock_nid_range()
531 * works
532 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000533 for (i = 0; i < mem->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000534 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
Yinghai Lu95f72d12010-07-12 14:36:09 +1000535 size, align, nid);
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700536 if (ret != MEMBLOCK_ERROR)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000537 return ret;
538 }
539
540 return memblock_alloc(size, align);
541}
542
Yinghai Lu95f72d12010-07-12 14:36:09 +1000543/* You must call memblock_analyze() before this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000544phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000545{
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000546 return memblock.memory_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000547}
548
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000549phys_addr_t memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000550{
551 int idx = memblock.memory.cnt - 1;
552
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000553 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000554}
555
556/* You must call memblock_analyze() after this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000557void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000558{
559 unsigned long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000560 phys_addr_t limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000561 struct memblock_region *p;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000562
563 if (!memory_limit)
564 return;
565
566 /* Truncate the memblock regions to satisfy the memory limit. */
567 limit = memory_limit;
568 for (i = 0; i < memblock.memory.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000569 if (limit > memblock.memory.regions[i].size) {
570 limit -= memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000571 continue;
572 }
573
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000574 memblock.memory.regions[i].size = limit;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000575 memblock.memory.cnt = i + 1;
576 break;
577 }
578
Yinghai Lu95f72d12010-07-12 14:36:09 +1000579 memory_limit = memblock_end_of_DRAM();
580
581 /* And truncate any reserves above the limit also. */
582 for (i = 0; i < memblock.reserved.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000583 p = &memblock.reserved.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +1000584
585 if (p->base > memory_limit)
586 p->size = 0;
587 else if ((p->base + p->size) > memory_limit)
588 p->size = memory_limit - p->base;
589
590 if (p->size == 0) {
591 memblock_remove_region(&memblock.reserved, i);
592 i--;
593 }
594 }
595}
596
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000597static int memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000598{
599 unsigned int left = 0, right = type->cnt;
600
601 do {
602 unsigned int mid = (right + left) / 2;
603
604 if (addr < type->regions[mid].base)
605 right = mid;
606 else if (addr >= (type->regions[mid].base +
607 type->regions[mid].size))
608 left = mid + 1;
609 else
610 return mid;
611 } while (left < right);
612 return -1;
613}
614
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000615int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000616{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000617 return memblock_search(&memblock.reserved, addr) != -1;
618}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000619
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000620int memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000621{
622 return memblock_search(&memblock.memory, addr) != -1;
623}
624
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000625int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000626{
627 int idx = memblock_search(&memblock.reserved, base);
628
629 if (idx == -1)
630 return 0;
631 return memblock.reserved.regions[idx].base <= base &&
632 (memblock.reserved.regions[idx].base +
633 memblock.reserved.regions[idx].size) >= (base + size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000634}
635
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000636int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000637{
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +1000638 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000639}
640
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700641
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000642void __init memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700643{
644 memblock.current_limit = limit;
645}
646
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000647static void memblock_dump(struct memblock_type *region, char *name)
648{
649 unsigned long long base, size;
650 int i;
651
652 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
653
654 for (i = 0; i < region->cnt; i++) {
655 base = region->regions[i].base;
656 size = region->regions[i].size;
657
658 pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n",
659 name, i, base, base + size - 1, size);
660 }
661}
662
663void memblock_dump_all(void)
664{
665 if (!memblock_debug)
666 return;
667
668 pr_info("MEMBLOCK configuration:\n");
669 pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
670
671 memblock_dump(&memblock.memory, "memory");
672 memblock_dump(&memblock.reserved, "reserved");
673}
674
675void __init memblock_analyze(void)
676{
677 int i;
678
679 /* Check marker in the unused last array entry */
680 WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
681 != (phys_addr_t)RED_INACTIVE);
682 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
683 != (phys_addr_t)RED_INACTIVE);
684
685 memblock.memory_size = 0;
686
687 for (i = 0; i < memblock.memory.cnt; i++)
688 memblock.memory_size += memblock.memory.regions[i].size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700689
690 /* We allow resizing from there */
691 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000692}
693
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700694void __init memblock_init(void)
695{
696 /* Hookup the initial arrays */
697 memblock.memory.regions = memblock_memory_init_regions;
698 memblock.memory.max = INIT_MEMBLOCK_REGIONS;
699 memblock.reserved.regions = memblock_reserved_init_regions;
700 memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
701
702 /* Write a marker in the unused last array entry */
703 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
704 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
705
706 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
707 * This simplifies the memblock_add() code below...
708 */
709 memblock.memory.regions[0].base = 0;
710 memblock.memory.regions[0].size = 0;
711 memblock.memory.cnt = 1;
712
713 /* Ditto. */
714 memblock.reserved.regions[0].base = 0;
715 memblock.reserved.regions[0].size = 0;
716 memblock.reserved.cnt = 1;
717
718 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
719}
720
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000721static int __init early_memblock(char *p)
722{
723 if (p && strstr(p, "debug"))
724 memblock_debug = 1;
725 return 0;
726}
727early_param("memblock", early_memblock);
728