blob: ae856d4e25a37ea71fdc658a667b897508f865e4 [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>
14#include <linux/init.h>
15#include <linux/bitops.h>
Benjamin Herrenschmidt449e8df2010-07-06 15:39:07 -070016#include <linux/poison.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100017#include <linux/memblock.h>
18
Yinghai Lu95f72d12010-07-12 14:36:09 +100019struct memblock memblock;
20
21static int memblock_debug;
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -070022static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1];
23static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1];
Yinghai Lu95f72d12010-07-12 14:36:09 +100024
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -070025#define MEMBLOCK_ERROR (~(phys_addr_t)0)
26
Yinghai Lu95f72d12010-07-12 14:36:09 +100027static int __init early_memblock(char *p)
28{
29 if (p && strstr(p, "debug"))
30 memblock_debug = 1;
31 return 0;
32}
33early_param("memblock", early_memblock);
34
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100035static void memblock_dump(struct memblock_type *region, char *name)
Yinghai Lu95f72d12010-07-12 14:36:09 +100036{
37 unsigned long long base, size;
38 int i;
39
40 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
41
42 for (i = 0; i < region->cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100043 base = region->regions[i].base;
44 size = region->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100045
46 pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n",
47 name, i, base, base + size - 1, size);
48 }
49}
50
51void memblock_dump_all(void)
52{
53 if (!memblock_debug)
54 return;
55
56 pr_info("MEMBLOCK configuration:\n");
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +100057 pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
Yinghai Lu95f72d12010-07-12 14:36:09 +100058
59 memblock_dump(&memblock.memory, "memory");
60 memblock_dump(&memblock.reserved, "reserved");
61}
62
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100063static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
64 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100065{
66 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
67}
68
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100069static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
70 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100071{
72 if (base2 == base1 + size1)
73 return 1;
74 else if (base1 == base2 + size2)
75 return -1;
76
77 return 0;
78}
79
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100080static long memblock_regions_adjacent(struct memblock_type *type,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100081 unsigned long r1, unsigned long r2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100082{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100083 phys_addr_t base1 = type->regions[r1].base;
84 phys_addr_t size1 = type->regions[r1].size;
85 phys_addr_t base2 = type->regions[r2].base;
86 phys_addr_t size2 = type->regions[r2].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100087
88 return memblock_addrs_adjacent(base1, size1, base2, size2);
89}
90
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100091static void memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +100092{
93 unsigned long i;
94
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100095 for (i = r; i < type->cnt - 1; i++) {
96 type->regions[i].base = type->regions[i + 1].base;
97 type->regions[i].size = type->regions[i + 1].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100098 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100099 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000100}
101
102/* Assumption: base addr of region 1 < base addr of region 2 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000103static void memblock_coalesce_regions(struct memblock_type *type,
Yinghai Lu95f72d12010-07-12 14:36:09 +1000104 unsigned long r1, unsigned long r2)
105{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000106 type->regions[r1].size += type->regions[r2].size;
107 memblock_remove_region(type, r2);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000108}
109
Yinghai Lu95f72d12010-07-12 14:36:09 +1000110void __init memblock_analyze(void)
111{
112 int i;
113
Benjamin Herrenschmidt449e8df2010-07-06 15:39:07 -0700114 /* Check marker in the unused last array entry */
115 WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
116 != (phys_addr_t)RED_INACTIVE);
117 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
118 != (phys_addr_t)RED_INACTIVE);
119
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000120 memblock.memory_size = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000121
122 for (i = 0; i < memblock.memory.cnt; i++)
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000123 memblock.memory_size += memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000124}
125
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000126static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000127{
128 unsigned long coalesced = 0;
129 long adjacent, i;
130
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000131 if ((type->cnt == 1) && (type->regions[0].size == 0)) {
132 type->regions[0].base = base;
133 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000134 return 0;
135 }
136
137 /* First try and coalesce this MEMBLOCK with another. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000138 for (i = 0; i < type->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000139 phys_addr_t rgnbase = type->regions[i].base;
140 phys_addr_t rgnsize = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000141
142 if ((rgnbase == base) && (rgnsize == size))
143 /* Already have this region, so we're done */
144 return 0;
145
146 adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
147 if (adjacent > 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000148 type->regions[i].base -= size;
149 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000150 coalesced++;
151 break;
152 } else if (adjacent < 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000153 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000154 coalesced++;
155 break;
156 }
157 }
158
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000159 if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1)) {
160 memblock_coalesce_regions(type, i, i+1);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000161 coalesced++;
162 }
163
164 if (coalesced)
165 return coalesced;
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -0700166 if (type->cnt >= type->max)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000167 return -1;
168
169 /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000170 for (i = type->cnt - 1; i >= 0; i--) {
171 if (base < type->regions[i].base) {
172 type->regions[i+1].base = type->regions[i].base;
173 type->regions[i+1].size = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000174 } else {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000175 type->regions[i+1].base = base;
176 type->regions[i+1].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000177 break;
178 }
179 }
180
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000181 if (base < type->regions[0].base) {
182 type->regions[0].base = base;
183 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000184 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000185 type->cnt++;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000186
187 return 0;
188}
189
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000190long memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000191{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000192 return memblock_add_region(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000193
194}
195
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000196static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000197{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000198 phys_addr_t rgnbegin, rgnend;
199 phys_addr_t end = base + size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000200 int i;
201
202 rgnbegin = rgnend = 0; /* supress gcc warnings */
203
204 /* Find the region where (base, size) belongs to */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000205 for (i=0; i < type->cnt; i++) {
206 rgnbegin = type->regions[i].base;
207 rgnend = rgnbegin + type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000208
209 if ((rgnbegin <= base) && (end <= rgnend))
210 break;
211 }
212
213 /* Didn't find the region */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000214 if (i == type->cnt)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000215 return -1;
216
217 /* Check to see if we are removing entire region */
218 if ((rgnbegin == base) && (rgnend == end)) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000219 memblock_remove_region(type, i);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000220 return 0;
221 }
222
223 /* Check to see if region is matching at the front */
224 if (rgnbegin == base) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000225 type->regions[i].base = end;
226 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000227 return 0;
228 }
229
230 /* Check to see if the region is matching at the end */
231 if (rgnend == end) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000232 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000233 return 0;
234 }
235
236 /*
237 * We need to split the entry - adjust the current one to the
238 * beginging of the hole and add the region after hole.
239 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000240 type->regions[i].size = base - type->regions[i].base;
241 return memblock_add_region(type, end, rgnend - end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000242}
243
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000244long memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000245{
246 return __memblock_remove(&memblock.memory, base, size);
247}
248
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000249long __init memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000250{
251 return __memblock_remove(&memblock.reserved, base, size);
252}
253
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000254long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000255{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000256 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000257
258 BUG_ON(0 == size);
259
260 return memblock_add_region(_rgn, base, size);
261}
262
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000263long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000264{
265 unsigned long i;
266
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000267 for (i = 0; i < type->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000268 phys_addr_t rgnbase = type->regions[i].base;
269 phys_addr_t rgnsize = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000270 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
271 break;
272 }
273
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000274 return (i < type->cnt) ? i : -1;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000275}
276
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000277static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000278{
279 return addr & ~(size - 1);
280}
281
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000282static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000283{
284 return (addr + (size - 1)) & ~(size - 1);
285}
286
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000287static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end,
288 phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000289{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000290 phys_addr_t base, res_base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000291 long j;
292
293 base = memblock_align_down((end - size), align);
294 while (start <= base) {
295 j = memblock_overlaps_region(&memblock.reserved, base, size);
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000296 if (j < 0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000297 return base;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000298 res_base = memblock.reserved.regions[j].base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000299 if (res_base < size)
300 break;
301 base = memblock_align_down(res_base - size, align);
302 }
303
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700304 return MEMBLOCK_ERROR;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000305}
306
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000307phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700308{
309 *nid = 0;
310
311 return end;
312}
313
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000314static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
315 phys_addr_t size,
316 phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000317{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000318 phys_addr_t start, end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000319
320 start = mp->base;
321 end = start + mp->size;
322
323 start = memblock_align_up(start, align);
324 while (start < end) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000325 phys_addr_t this_end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000326 int this_nid;
327
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700328 this_end = memblock_nid_range(start, end, &this_nid);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000329 if (this_nid == nid) {
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000330 phys_addr_t ret = memblock_find_region(start, this_end, size, align);
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700331 if (ret != MEMBLOCK_ERROR &&
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000332 memblock_add_region(&memblock.reserved, ret, size) >= 0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000333 return ret;
334 }
335 start = this_end;
336 }
337
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700338 return MEMBLOCK_ERROR;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000339}
340
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000341phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000342{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000343 struct memblock_type *mem = &memblock.memory;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000344 int i;
345
346 BUG_ON(0 == size);
347
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700348 /* We do a bottom-up search for a region with the right
349 * nid since that's easier considering how memblock_nid_range()
350 * works
351 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000352 size = memblock_align_up(size, align);
353
354 for (i = 0; i < mem->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000355 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
Yinghai Lu95f72d12010-07-12 14:36:09 +1000356 size, align, nid);
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700357 if (ret != MEMBLOCK_ERROR)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000358 return ret;
359 }
360
361 return memblock_alloc(size, align);
362}
363
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000364phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000365{
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700366 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000367}
368
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000369phys_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 +1000370{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000371 phys_addr_t alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000372
373 alloc = __memblock_alloc_base(size, align, max_addr);
374
375 if (alloc == 0)
376 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
377 (unsigned long long) size, (unsigned long long) max_addr);
378
379 return alloc;
380}
381
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000382phys_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 +1000383{
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700384 long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000385 phys_addr_t base = 0;
386 phys_addr_t res_base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000387
388 BUG_ON(0 == size);
389
390 size = memblock_align_up(size, align);
391
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700392 /* Pump up max_addr */
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700393 if (max_addr == MEMBLOCK_ALLOC_ACCESSIBLE)
394 max_addr = memblock.current_limit;
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700395
396 /* We do a top-down search, this tends to limit memory
397 * fragmentation by keeping early boot allocs near the
398 * top of memory
399 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000400 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000401 phys_addr_t memblockbase = memblock.memory.regions[i].base;
402 phys_addr_t memblocksize = memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000403
404 if (memblocksize < size)
405 continue;
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700406 base = min(memblockbase + memblocksize, max_addr);
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000407 res_base = memblock_find_region(memblockbase, base, size, align);
Benjamin Herrenschmidt4d629f92010-07-06 15:39:09 -0700408 if (res_base != MEMBLOCK_ERROR &&
Benjamin Herrenschmidt3a9c2c82010-07-12 13:28:15 +1000409 memblock_add_region(&memblock.reserved, res_base, size) >= 0)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700410 return res_base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000411 }
412 return 0;
413}
414
415/* You must call memblock_analyze() before this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000416phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000417{
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000418 return memblock.memory_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000419}
420
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000421phys_addr_t memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000422{
423 int idx = memblock.memory.cnt - 1;
424
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000425 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000426}
427
428/* You must call memblock_analyze() after this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000429void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000430{
431 unsigned long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000432 phys_addr_t limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000433 struct memblock_region *p;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000434
435 if (!memory_limit)
436 return;
437
438 /* Truncate the memblock regions to satisfy the memory limit. */
439 limit = memory_limit;
440 for (i = 0; i < memblock.memory.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000441 if (limit > memblock.memory.regions[i].size) {
442 limit -= memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000443 continue;
444 }
445
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000446 memblock.memory.regions[i].size = limit;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000447 memblock.memory.cnt = i + 1;
448 break;
449 }
450
Yinghai Lu95f72d12010-07-12 14:36:09 +1000451 memory_limit = memblock_end_of_DRAM();
452
453 /* And truncate any reserves above the limit also. */
454 for (i = 0; i < memblock.reserved.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000455 p = &memblock.reserved.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +1000456
457 if (p->base > memory_limit)
458 p->size = 0;
459 else if ((p->base + p->size) > memory_limit)
460 p->size = memory_limit - p->base;
461
462 if (p->size == 0) {
463 memblock_remove_region(&memblock.reserved, i);
464 i--;
465 }
466 }
467}
468
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000469static int memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000470{
471 unsigned int left = 0, right = type->cnt;
472
473 do {
474 unsigned int mid = (right + left) / 2;
475
476 if (addr < type->regions[mid].base)
477 right = mid;
478 else if (addr >= (type->regions[mid].base +
479 type->regions[mid].size))
480 left = mid + 1;
481 else
482 return mid;
483 } while (left < right);
484 return -1;
485}
486
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000487int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000488{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000489 return memblock_search(&memblock.reserved, addr) != -1;
490}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000491
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000492int memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000493{
494 return memblock_search(&memblock.memory, addr) != -1;
495}
496
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000497int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000498{
499 int idx = memblock_search(&memblock.reserved, base);
500
501 if (idx == -1)
502 return 0;
503 return memblock.reserved.regions[idx].base <= base &&
504 (memblock.reserved.regions[idx].base +
505 memblock.reserved.regions[idx].size) >= (base + size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000506}
507
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000508int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000509{
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +1000510 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000511}
512
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700513
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000514void __init memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700515{
516 memblock.current_limit = limit;
517}
518
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700519void __init memblock_init(void)
520{
521 /* Hookup the initial arrays */
522 memblock.memory.regions = memblock_memory_init_regions;
523 memblock.memory.max = INIT_MEMBLOCK_REGIONS;
524 memblock.reserved.regions = memblock_reserved_init_regions;
525 memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
526
527 /* Write a marker in the unused last array entry */
528 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
529 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
530
531 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
532 * This simplifies the memblock_add() code below...
533 */
534 memblock.memory.regions[0].base = 0;
535 memblock.memory.regions[0].size = 0;
536 memblock.memory.cnt = 1;
537
538 /* Ditto. */
539 memblock.reserved.regions[0].base = 0;
540 memblock.reserved.regions[0].size = 0;
541 memblock.reserved.cnt = 1;
542
543 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
544}
545