blob: a925866e1455151989e8fc2f1dd54d37d91b693f [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
25static int __init early_memblock(char *p)
26{
27 if (p && strstr(p, "debug"))
28 memblock_debug = 1;
29 return 0;
30}
31early_param("memblock", early_memblock);
32
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100033static void memblock_dump(struct memblock_type *region, char *name)
Yinghai Lu95f72d12010-07-12 14:36:09 +100034{
35 unsigned long long base, size;
36 int i;
37
38 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
39
40 for (i = 0; i < region->cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100041 base = region->regions[i].base;
42 size = region->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100043
44 pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n",
45 name, i, base, base + size - 1, size);
46 }
47}
48
49void memblock_dump_all(void)
50{
51 if (!memblock_debug)
52 return;
53
54 pr_info("MEMBLOCK configuration:\n");
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +100055 pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
Yinghai Lu95f72d12010-07-12 14:36:09 +100056
57 memblock_dump(&memblock.memory, "memory");
58 memblock_dump(&memblock.reserved, "reserved");
59}
60
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100061static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
62 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100063{
64 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
65}
66
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100067static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
68 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100069{
70 if (base2 == base1 + size1)
71 return 1;
72 else if (base1 == base2 + size2)
73 return -1;
74
75 return 0;
76}
77
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100078static long memblock_regions_adjacent(struct memblock_type *type,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100079 unsigned long r1, unsigned long r2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100080{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100081 phys_addr_t base1 = type->regions[r1].base;
82 phys_addr_t size1 = type->regions[r1].size;
83 phys_addr_t base2 = type->regions[r2].base;
84 phys_addr_t size2 = type->regions[r2].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100085
86 return memblock_addrs_adjacent(base1, size1, base2, size2);
87}
88
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100089static void memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +100090{
91 unsigned long i;
92
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100093 for (i = r; i < type->cnt - 1; i++) {
94 type->regions[i].base = type->regions[i + 1].base;
95 type->regions[i].size = type->regions[i + 1].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +100096 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100097 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +100098}
99
100/* Assumption: base addr of region 1 < base addr of region 2 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000101static void memblock_coalesce_regions(struct memblock_type *type,
Yinghai Lu95f72d12010-07-12 14:36:09 +1000102 unsigned long r1, unsigned long r2)
103{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000104 type->regions[r1].size += type->regions[r2].size;
105 memblock_remove_region(type, r2);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000106}
107
108void __init memblock_init(void)
109{
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -0700110 /* Hookup the initial arrays */
111 memblock.memory.regions = memblock_memory_init_regions;
112 memblock.memory.max = INIT_MEMBLOCK_REGIONS;
113 memblock.reserved.regions = memblock_reserved_init_regions;
114 memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
115
Benjamin Herrenschmidt449e8df2010-07-06 15:39:07 -0700116 /* Write a marker in the unused last array entry */
117 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
118 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
119
Yinghai Lu95f72d12010-07-12 14:36:09 +1000120 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
121 * This simplifies the memblock_add() code below...
122 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000123 memblock.memory.regions[0].base = 0;
124 memblock.memory.regions[0].size = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000125 memblock.memory.cnt = 1;
126
127 /* Ditto. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000128 memblock.reserved.regions[0].base = 0;
129 memblock.reserved.regions[0].size = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000130 memblock.reserved.cnt = 1;
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700131
132 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000133}
134
135void __init memblock_analyze(void)
136{
137 int i;
138
Benjamin Herrenschmidt449e8df2010-07-06 15:39:07 -0700139 /* Check marker in the unused last array entry */
140 WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
141 != (phys_addr_t)RED_INACTIVE);
142 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
143 != (phys_addr_t)RED_INACTIVE);
144
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000145 memblock.memory_size = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000146
147 for (i = 0; i < memblock.memory.cnt; i++)
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000148 memblock.memory_size += memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000149}
150
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000151static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000152{
153 unsigned long coalesced = 0;
154 long adjacent, i;
155
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000156 if ((type->cnt == 1) && (type->regions[0].size == 0)) {
157 type->regions[0].base = base;
158 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000159 return 0;
160 }
161
162 /* First try and coalesce this MEMBLOCK with another. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000163 for (i = 0; i < type->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000164 phys_addr_t rgnbase = type->regions[i].base;
165 phys_addr_t rgnsize = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000166
167 if ((rgnbase == base) && (rgnsize == size))
168 /* Already have this region, so we're done */
169 return 0;
170
171 adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
172 if (adjacent > 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000173 type->regions[i].base -= size;
174 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000175 coalesced++;
176 break;
177 } else if (adjacent < 0) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000178 type->regions[i].size += size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000179 coalesced++;
180 break;
181 }
182 }
183
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000184 if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1)) {
185 memblock_coalesce_regions(type, i, i+1);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000186 coalesced++;
187 }
188
189 if (coalesced)
190 return coalesced;
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -0700191 if (type->cnt >= type->max)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000192 return -1;
193
194 /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000195 for (i = type->cnt - 1; i >= 0; i--) {
196 if (base < type->regions[i].base) {
197 type->regions[i+1].base = type->regions[i].base;
198 type->regions[i+1].size = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000199 } else {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000200 type->regions[i+1].base = base;
201 type->regions[i+1].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000202 break;
203 }
204 }
205
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000206 if (base < type->regions[0].base) {
207 type->regions[0].base = base;
208 type->regions[0].size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000209 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000210 type->cnt++;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000211
212 return 0;
213}
214
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000215long memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000216{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000217 return memblock_add_region(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000218
219}
220
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000221static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000222{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000223 phys_addr_t rgnbegin, rgnend;
224 phys_addr_t end = base + size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000225 int i;
226
227 rgnbegin = rgnend = 0; /* supress gcc warnings */
228
229 /* Find the region where (base, size) belongs to */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000230 for (i=0; i < type->cnt; i++) {
231 rgnbegin = type->regions[i].base;
232 rgnend = rgnbegin + type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000233
234 if ((rgnbegin <= base) && (end <= rgnend))
235 break;
236 }
237
238 /* Didn't find the region */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000239 if (i == type->cnt)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000240 return -1;
241
242 /* Check to see if we are removing entire region */
243 if ((rgnbegin == base) && (rgnend == end)) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000244 memblock_remove_region(type, i);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000245 return 0;
246 }
247
248 /* Check to see if region is matching at the front */
249 if (rgnbegin == base) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000250 type->regions[i].base = end;
251 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000252 return 0;
253 }
254
255 /* Check to see if the region is matching at the end */
256 if (rgnend == end) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000257 type->regions[i].size -= size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000258 return 0;
259 }
260
261 /*
262 * We need to split the entry - adjust the current one to the
263 * beginging of the hole and add the region after hole.
264 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000265 type->regions[i].size = base - type->regions[i].base;
266 return memblock_add_region(type, end, rgnend - end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000267}
268
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000269long memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000270{
271 return __memblock_remove(&memblock.memory, base, size);
272}
273
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000274long __init memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000275{
276 return __memblock_remove(&memblock.reserved, base, size);
277}
278
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000279long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000280{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000281 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000282
283 BUG_ON(0 == size);
284
285 return memblock_add_region(_rgn, base, size);
286}
287
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000288long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000289{
290 unsigned long i;
291
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000292 for (i = 0; i < type->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000293 phys_addr_t rgnbase = type->regions[i].base;
294 phys_addr_t rgnsize = type->regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000295 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
296 break;
297 }
298
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000299 return (i < type->cnt) ? i : -1;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000300}
301
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000302static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000303{
304 return addr & ~(size - 1);
305}
306
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000307static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000308{
309 return (addr + (size - 1)) & ~(size - 1);
310}
311
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000312static phys_addr_t __init memblock_alloc_region(phys_addr_t start, phys_addr_t end,
313 phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000314{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000315 phys_addr_t base, res_base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000316 long j;
317
318 base = memblock_align_down((end - size), align);
319 while (start <= base) {
320 j = memblock_overlaps_region(&memblock.reserved, base, size);
321 if (j < 0) {
322 /* this area isn't reserved, take it */
323 if (memblock_add_region(&memblock.reserved, base, size) < 0)
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000324 base = ~(phys_addr_t)0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000325 return base;
326 }
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000327 res_base = memblock.reserved.regions[j].base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000328 if (res_base < size)
329 break;
330 base = memblock_align_down(res_base - size, align);
331 }
332
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000333 return ~(phys_addr_t)0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000334}
335
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000336phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700337{
338 *nid = 0;
339
340 return end;
341}
342
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000343static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
344 phys_addr_t size,
345 phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000346{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000347 phys_addr_t start, end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000348
349 start = mp->base;
350 end = start + mp->size;
351
352 start = memblock_align_up(start, align);
353 while (start < end) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000354 phys_addr_t this_end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000355 int this_nid;
356
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700357 this_end = memblock_nid_range(start, end, &this_nid);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000358 if (this_nid == nid) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000359 phys_addr_t ret = memblock_alloc_region(start, this_end, size, align);
360 if (ret != ~(phys_addr_t)0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000361 return ret;
362 }
363 start = this_end;
364 }
365
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000366 return ~(phys_addr_t)0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000367}
368
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000369phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000370{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000371 struct memblock_type *mem = &memblock.memory;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000372 int i;
373
374 BUG_ON(0 == size);
375
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700376 /* We do a bottom-up search for a region with the right
377 * nid since that's easier considering how memblock_nid_range()
378 * works
379 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000380 size = memblock_align_up(size, align);
381
382 for (i = 0; i < mem->cnt; i++) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000383 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
Yinghai Lu95f72d12010-07-12 14:36:09 +1000384 size, align, nid);
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000385 if (ret != ~(phys_addr_t)0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000386 return ret;
387 }
388
389 return memblock_alloc(size, align);
390}
391
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000392phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000393{
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700394 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000395}
396
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000397phys_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 +1000398{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000399 phys_addr_t alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000400
401 alloc = __memblock_alloc_base(size, align, max_addr);
402
403 if (alloc == 0)
404 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
405 (unsigned long long) size, (unsigned long long) max_addr);
406
407 return alloc;
408}
409
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000410phys_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 +1000411{
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700412 long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000413 phys_addr_t base = 0;
414 phys_addr_t res_base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000415
416 BUG_ON(0 == size);
417
418 size = memblock_align_up(size, align);
419
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700420 /* Pump up max_addr */
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700421 if (max_addr == MEMBLOCK_ALLOC_ACCESSIBLE)
422 max_addr = memblock.current_limit;
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700423
424 /* We do a top-down search, this tends to limit memory
425 * fragmentation by keeping early boot allocs near the
426 * top of memory
427 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000428 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000429 phys_addr_t memblockbase = memblock.memory.regions[i].base;
430 phys_addr_t memblocksize = memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000431
432 if (memblocksize < size)
433 continue;
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700434 base = min(memblockbase + memblocksize, max_addr);
435 res_base = memblock_alloc_region(memblockbase, base, size, align);
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000436 if (res_base != ~(phys_addr_t)0)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700437 return res_base;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000438 }
439 return 0;
440}
441
442/* You must call memblock_analyze() before this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000443phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000444{
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000445 return memblock.memory_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000446}
447
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000448phys_addr_t memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000449{
450 int idx = memblock.memory.cnt - 1;
451
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000452 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000453}
454
455/* You must call memblock_analyze() after this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000456void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000457{
458 unsigned long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000459 phys_addr_t limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000460 struct memblock_region *p;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000461
462 if (!memory_limit)
463 return;
464
465 /* Truncate the memblock regions to satisfy the memory limit. */
466 limit = memory_limit;
467 for (i = 0; i < memblock.memory.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000468 if (limit > memblock.memory.regions[i].size) {
469 limit -= memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000470 continue;
471 }
472
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000473 memblock.memory.regions[i].size = limit;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000474 memblock.memory.cnt = i + 1;
475 break;
476 }
477
Yinghai Lu95f72d12010-07-12 14:36:09 +1000478 memory_limit = memblock_end_of_DRAM();
479
480 /* And truncate any reserves above the limit also. */
481 for (i = 0; i < memblock.reserved.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000482 p = &memblock.reserved.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +1000483
484 if (p->base > memory_limit)
485 p->size = 0;
486 else if ((p->base + p->size) > memory_limit)
487 p->size = memory_limit - p->base;
488
489 if (p->size == 0) {
490 memblock_remove_region(&memblock.reserved, i);
491 i--;
492 }
493 }
494}
495
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000496static int memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000497{
498 unsigned int left = 0, right = type->cnt;
499
500 do {
501 unsigned int mid = (right + left) / 2;
502
503 if (addr < type->regions[mid].base)
504 right = mid;
505 else if (addr >= (type->regions[mid].base +
506 type->regions[mid].size))
507 left = mid + 1;
508 else
509 return mid;
510 } while (left < right);
511 return -1;
512}
513
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000514int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000515{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000516 return memblock_search(&memblock.reserved, addr) != -1;
517}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000518
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000519int memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000520{
521 return memblock_search(&memblock.memory, addr) != -1;
522}
523
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000524int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000525{
526 int idx = memblock_search(&memblock.reserved, base);
527
528 if (idx == -1)
529 return 0;
530 return memblock.reserved.regions[idx].base <= base &&
531 (memblock.reserved.regions[idx].base +
532 memblock.reserved.regions[idx].size) >= (base + size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000533}
534
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000535int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000536{
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +1000537 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000538}
539
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700540
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000541void __init memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700542{
543 memblock.current_limit = limit;
544}
545