blob: 81da63592a68f8e34731db5d571a8482a92d3cda [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>
16#include <linux/memblock.h>
17
Yinghai Lu95f72d12010-07-12 14:36:09 +100018struct memblock memblock;
19
20static int memblock_debug;
21
22static int __init early_memblock(char *p)
23{
24 if (p && strstr(p, "debug"))
25 memblock_debug = 1;
26 return 0;
27}
28early_param("memblock", early_memblock);
29
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100030static void memblock_dump(struct memblock_type *region, char *name)
Yinghai Lu95f72d12010-07-12 14:36:09 +100031{
32 unsigned long long base, size;
33 int i;
34
35 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
36
37 for (i = 0; i < region->cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100038 base = region->regions[i].base;
39 size = region->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100040
41 pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n",
42 name, i, base, base + size - 1, size);
43 }
44}
45
46void memblock_dump_all(void)
47{
48 if (!memblock_debug)
49 return;
50
51 pr_info("MEMBLOCK configuration:\n");
Yinghai Lu95f72d12010-07-12 14:36:09 +100052 pr_info(" memory.size = 0x%llx\n", (unsigned long long)memblock.memory.size);
53
54 memblock_dump(&memblock.memory, "memory");
55 memblock_dump(&memblock.reserved, "reserved");
56}
57
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100058static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
59 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100060{
61 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
62}
63
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100064static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
65 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100066{
67 if (base2 == base1 + size1)
68 return 1;
69 else if (base1 == base2 + size2)
70 return -1;
71
72 return 0;
73}
74
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100075static long memblock_regions_adjacent(struct memblock_type *type,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100076 unsigned long r1, unsigned long r2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100077{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100078 phys_addr_t base1 = type->regions[r1].base;
79 phys_addr_t size1 = type->regions[r1].size;
80 phys_addr_t base2 = type->regions[r2].base;
81 phys_addr_t size2 = type->regions[r2].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100082
83 return memblock_addrs_adjacent(base1, size1, base2, size2);
84}
85
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100086static void memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +100087{
88 unsigned long i;
89
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100090 for (i = r; i < type->cnt - 1; i++) {
91 type->regions[i].base = type->regions[i + 1].base;
92 type->regions[i].size = type->regions[i + 1].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100093 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100094 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +100095}
96
97/* Assumption: base addr of region 1 < base addr of region 2 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100098static void memblock_coalesce_regions(struct memblock_type *type,
Yinghai Lu95f72d12010-07-12 14:36:09 +100099 unsigned long r1, unsigned long r2)
100{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000101 type->regions[r1].size += type->regions[r2].size;
102 memblock_remove_region(type, r2);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000103}
104
105void __init memblock_init(void)
106{
107 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
108 * This simplifies the memblock_add() code below...
109 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000110 memblock.memory.regions[0].base = 0;
111 memblock.memory.regions[0].size = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000112 memblock.memory.cnt = 1;
113
114 /* Ditto. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000115 memblock.reserved.regions[0].base = 0;
116 memblock.reserved.regions[0].size = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000117 memblock.reserved.cnt = 1;
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700118
119 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000120}
121
122void __init memblock_analyze(void)
123{
124 int i;
125
126 memblock.memory.size = 0;
127
128 for (i = 0; i < memblock.memory.cnt; i++)
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000129 memblock.memory.size += memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000130}
131
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000132static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000133{
134 unsigned long coalesced = 0;
135 long adjacent, i;
136
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000137 if ((type->cnt == 1) && (type->regions[0].size == 0)) {
138 type->regions[0].base = base;
139 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000140 return 0;
141 }
142
143 /* First try and coalesce this MEMBLOCK with another. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000144 for (i = 0; i < type->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000145 phys_addr_t rgnbase = type->regions[i].base;
146 phys_addr_t rgnsize = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000147
148 if ((rgnbase == base) && (rgnsize == size))
149 /* Already have this region, so we're done */
150 return 0;
151
152 adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
153 if (adjacent > 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000154 type->regions[i].base -= size;
155 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000156 coalesced++;
157 break;
158 } else if (adjacent < 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000159 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000160 coalesced++;
161 break;
162 }
163 }
164
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000165 if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1)) {
166 memblock_coalesce_regions(type, i, i+1);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000167 coalesced++;
168 }
169
170 if (coalesced)
171 return coalesced;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000172 if (type->cnt >= MAX_MEMBLOCK_REGIONS)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000173 return -1;
174
175 /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000176 for (i = type->cnt - 1; i >= 0; i--) {
177 if (base < type->regions[i].base) {
178 type->regions[i+1].base = type->regions[i].base;
179 type->regions[i+1].size = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000180 } else {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000181 type->regions[i+1].base = base;
182 type->regions[i+1].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000183 break;
184 }
185 }
186
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000187 if (base < type->regions[0].base) {
188 type->regions[0].base = base;
189 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000190 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000191 type->cnt++;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000192
193 return 0;
194}
195
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000196long memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000197{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000198 return memblock_add_region(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000199
200}
201
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000202static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000203{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000204 phys_addr_t rgnbegin, rgnend;
205 phys_addr_t end = base + size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000206 int i;
207
208 rgnbegin = rgnend = 0; /* supress gcc warnings */
209
210 /* Find the region where (base, size) belongs to */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000211 for (i=0; i < type->cnt; i++) {
212 rgnbegin = type->regions[i].base;
213 rgnend = rgnbegin + type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000214
215 if ((rgnbegin <= base) && (end <= rgnend))
216 break;
217 }
218
219 /* Didn't find the region */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000220 if (i == type->cnt)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000221 return -1;
222
223 /* Check to see if we are removing entire region */
224 if ((rgnbegin == base) && (rgnend == end)) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000225 memblock_remove_region(type, i);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000226 return 0;
227 }
228
229 /* Check to see if region is matching at the front */
230 if (rgnbegin == base) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000231 type->regions[i].base = end;
232 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000233 return 0;
234 }
235
236 /* Check to see if the region is matching at the end */
237 if (rgnend == end) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000238 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000239 return 0;
240 }
241
242 /*
243 * We need to split the entry - adjust the current one to the
244 * beginging of the hole and add the region after hole.
245 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000246 type->regions[i].size = base - type->regions[i].base;
247 return memblock_add_region(type, end, rgnend - end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000248}
249
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000250long memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000251{
252 return __memblock_remove(&memblock.memory, base, size);
253}
254
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000255long __init memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000256{
257 return __memblock_remove(&memblock.reserved, base, size);
258}
259
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000260long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000261{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000262 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000263
264 BUG_ON(0 == size);
265
266 return memblock_add_region(_rgn, base, size);
267}
268
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000269long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000270{
271 unsigned long i;
272
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000273 for (i = 0; i < type->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000274 phys_addr_t rgnbase = type->regions[i].base;
275 phys_addr_t rgnsize = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000276 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
277 break;
278 }
279
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000280 return (i < type->cnt) ? i : -1;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000281}
282
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000283static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000284{
285 return addr & ~(size - 1);
286}
287
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000288static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000289{
290 return (addr + (size - 1)) & ~(size - 1);
291}
292
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000293static phys_addr_t __init memblock_alloc_region(phys_addr_t start, phys_addr_t end,
294 phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000295{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000296 phys_addr_t base, res_base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000297 long j;
298
299 base = memblock_align_down((end - size), align);
300 while (start <= base) {
301 j = memblock_overlaps_region(&memblock.reserved, base, size);
302 if (j < 0) {
303 /* this area isn't reserved, take it */
304 if (memblock_add_region(&memblock.reserved, base, size) < 0)
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000305 base = ~(phys_addr_t)0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000306 return base;
307 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000308 res_base = memblock.reserved.regions[j].base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000309 if (res_base < size)
310 break;
311 base = memblock_align_down(res_base - size, align);
312 }
313
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000314 return ~(phys_addr_t)0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000315}
316
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000317phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700318{
319 *nid = 0;
320
321 return end;
322}
323
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000324static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
325 phys_addr_t size,
326 phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000327{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000328 phys_addr_t start, end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000329
330 start = mp->base;
331 end = start + mp->size;
332
333 start = memblock_align_up(start, align);
334 while (start < end) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000335 phys_addr_t this_end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000336 int this_nid;
337
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700338 this_end = memblock_nid_range(start, end, &this_nid);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000339 if (this_nid == nid) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000340 phys_addr_t ret = memblock_alloc_region(start, this_end, size, align);
341 if (ret != ~(phys_addr_t)0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000342 return ret;
343 }
344 start = this_end;
345 }
346
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000347 return ~(phys_addr_t)0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000348}
349
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000350phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000351{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000352 struct memblock_type *mem = &memblock.memory;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000353 int i;
354
355 BUG_ON(0 == size);
356
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700357 /* We do a bottom-up search for a region with the right
358 * nid since that's easier considering how memblock_nid_range()
359 * works
360 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000361 size = memblock_align_up(size, align);
362
363 for (i = 0; i < mem->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000364 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
Yinghai Lu95f72d12010-07-12 14:36:09 +1000365 size, align, nid);
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000366 if (ret != ~(phys_addr_t)0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000367 return ret;
368 }
369
370 return memblock_alloc(size, align);
371}
372
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000373phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000374{
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700375 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000376}
377
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000378phys_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 +1000379{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000380 phys_addr_t alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000381
382 alloc = __memblock_alloc_base(size, align, max_addr);
383
384 if (alloc == 0)
385 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
386 (unsigned long long) size, (unsigned long long) max_addr);
387
388 return alloc;
389}
390
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000391phys_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 +1000392{
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700393 long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000394 phys_addr_t base = 0;
395 phys_addr_t res_base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000396
397 BUG_ON(0 == size);
398
399 size = memblock_align_up(size, align);
400
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700401 /* Pump up max_addr */
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700402 if (max_addr == MEMBLOCK_ALLOC_ACCESSIBLE)
403 max_addr = memblock.current_limit;
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700404
405 /* We do a top-down search, this tends to limit memory
406 * fragmentation by keeping early boot allocs near the
407 * top of memory
408 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000409 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000410 phys_addr_t memblockbase = memblock.memory.regions[i].base;
411 phys_addr_t memblocksize = memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000412
413 if (memblocksize < size)
414 continue;
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700415 base = min(memblockbase + memblocksize, max_addr);
416 res_base = memblock_alloc_region(memblockbase, base, size, align);
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000417 if (res_base != ~(phys_addr_t)0)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700418 return res_base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000419 }
420 return 0;
421}
422
423/* You must call memblock_analyze() before this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000424phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000425{
426 return memblock.memory.size;
427}
428
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000429phys_addr_t memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000430{
431 int idx = memblock.memory.cnt - 1;
432
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000433 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000434}
435
436/* You must call memblock_analyze() after this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000437void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000438{
439 unsigned long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000440 phys_addr_t limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000441 struct memblock_region *p;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000442
443 if (!memory_limit)
444 return;
445
446 /* Truncate the memblock regions to satisfy the memory limit. */
447 limit = memory_limit;
448 for (i = 0; i < memblock.memory.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000449 if (limit > memblock.memory.regions[i].size) {
450 limit -= memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000451 continue;
452 }
453
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000454 memblock.memory.regions[i].size = limit;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000455 memblock.memory.cnt = i + 1;
456 break;
457 }
458
Yinghai Lu95f72d12010-07-12 14:36:09 +1000459 memory_limit = memblock_end_of_DRAM();
460
461 /* And truncate any reserves above the limit also. */
462 for (i = 0; i < memblock.reserved.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000463 p = &memblock.reserved.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +1000464
465 if (p->base > memory_limit)
466 p->size = 0;
467 else if ((p->base + p->size) > memory_limit)
468 p->size = memory_limit - p->base;
469
470 if (p->size == 0) {
471 memblock_remove_region(&memblock.reserved, i);
472 i--;
473 }
474 }
475}
476
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000477static int memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000478{
479 unsigned int left = 0, right = type->cnt;
480
481 do {
482 unsigned int mid = (right + left) / 2;
483
484 if (addr < type->regions[mid].base)
485 right = mid;
486 else if (addr >= (type->regions[mid].base +
487 type->regions[mid].size))
488 left = mid + 1;
489 else
490 return mid;
491 } while (left < right);
492 return -1;
493}
494
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000495int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000496{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000497 return memblock_search(&memblock.reserved, addr) != -1;
498}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000499
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000500int memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000501{
502 return memblock_search(&memblock.memory, addr) != -1;
503}
504
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000505int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000506{
507 int idx = memblock_search(&memblock.reserved, base);
508
509 if (idx == -1)
510 return 0;
511 return memblock.reserved.regions[idx].base <= base &&
512 (memblock.reserved.regions[idx].base +
513 memblock.reserved.regions[idx].size) >= (base + size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000514}
515
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000516int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000517{
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +1000518 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000519}
520
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700521
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000522void __init memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700523{
524 memblock.current_limit = limit;
525}
526