]>
Commit | Line | Data |
---|---|---|
95f72d1e YL |
1 | /* |
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 | ||
95f72d1e YL |
18 | struct memblock memblock; |
19 | ||
20 | static int memblock_debug; | |
21 | ||
22 | static int __init early_memblock(char *p) | |
23 | { | |
24 | if (p && strstr(p, "debug")) | |
25 | memblock_debug = 1; | |
26 | return 0; | |
27 | } | |
28 | early_param("memblock", early_memblock); | |
29 | ||
e3239ff9 | 30 | static void memblock_dump(struct memblock_type *region, char *name) |
95f72d1e YL |
31 | { |
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++) { | |
e3239ff9 BH |
38 | base = region->regions[i].base; |
39 | size = region->regions[i].size; | |
95f72d1e YL |
40 | |
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 | ||
46 | void memblock_dump_all(void) | |
47 | { | |
48 | if (!memblock_debug) | |
49 | return; | |
50 | ||
51 | pr_info("MEMBLOCK configuration:\n"); | |
95f72d1e YL |
52 | 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 | ||
2898cc4c BH |
58 | static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, |
59 | phys_addr_t base2, phys_addr_t size2) | |
95f72d1e YL |
60 | { |
61 | return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); | |
62 | } | |
63 | ||
2898cc4c BH |
64 | static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1, |
65 | phys_addr_t base2, phys_addr_t size2) | |
95f72d1e YL |
66 | { |
67 | if (base2 == base1 + size1) | |
68 | return 1; | |
69 | else if (base1 == base2 + size2) | |
70 | return -1; | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
e3239ff9 | 75 | static long memblock_regions_adjacent(struct memblock_type *type, |
2898cc4c | 76 | unsigned long r1, unsigned long r2) |
95f72d1e | 77 | { |
2898cc4c BH |
78 | 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; | |
95f72d1e YL |
82 | |
83 | return memblock_addrs_adjacent(base1, size1, base2, size2); | |
84 | } | |
85 | ||
e3239ff9 | 86 | static void memblock_remove_region(struct memblock_type *type, unsigned long r) |
95f72d1e YL |
87 | { |
88 | unsigned long i; | |
89 | ||
e3239ff9 BH |
90 | 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; | |
95f72d1e | 93 | } |
e3239ff9 | 94 | type->cnt--; |
95f72d1e YL |
95 | } |
96 | ||
97 | /* Assumption: base addr of region 1 < base addr of region 2 */ | |
e3239ff9 | 98 | static void memblock_coalesce_regions(struct memblock_type *type, |
95f72d1e YL |
99 | unsigned long r1, unsigned long r2) |
100 | { | |
e3239ff9 BH |
101 | type->regions[r1].size += type->regions[r2].size; |
102 | memblock_remove_region(type, r2); | |
95f72d1e YL |
103 | } |
104 | ||
105 | void __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 | */ | |
e3239ff9 BH |
110 | memblock.memory.regions[0].base = 0; |
111 | memblock.memory.regions[0].size = 0; | |
95f72d1e YL |
112 | memblock.memory.cnt = 1; |
113 | ||
114 | /* Ditto. */ | |
e3239ff9 BH |
115 | memblock.reserved.regions[0].base = 0; |
116 | memblock.reserved.regions[0].size = 0; | |
95f72d1e | 117 | memblock.reserved.cnt = 1; |
e63075a3 BH |
118 | |
119 | memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE; | |
95f72d1e YL |
120 | } |
121 | ||
122 | void __init memblock_analyze(void) | |
123 | { | |
124 | int i; | |
125 | ||
126 | memblock.memory.size = 0; | |
127 | ||
128 | for (i = 0; i < memblock.memory.cnt; i++) | |
e3239ff9 | 129 | memblock.memory.size += memblock.memory.regions[i].size; |
95f72d1e YL |
130 | } |
131 | ||
2898cc4c | 132 | static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size) |
95f72d1e YL |
133 | { |
134 | unsigned long coalesced = 0; | |
135 | long adjacent, i; | |
136 | ||
e3239ff9 BH |
137 | if ((type->cnt == 1) && (type->regions[0].size == 0)) { |
138 | type->regions[0].base = base; | |
139 | type->regions[0].size = size; | |
95f72d1e YL |
140 | return 0; |
141 | } | |
142 | ||
143 | /* First try and coalesce this MEMBLOCK with another. */ | |
e3239ff9 | 144 | for (i = 0; i < type->cnt; i++) { |
2898cc4c BH |
145 | phys_addr_t rgnbase = type->regions[i].base; |
146 | phys_addr_t rgnsize = type->regions[i].size; | |
95f72d1e YL |
147 | |
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) { | |
e3239ff9 BH |
154 | type->regions[i].base -= size; |
155 | type->regions[i].size += size; | |
95f72d1e YL |
156 | coalesced++; |
157 | break; | |
158 | } else if (adjacent < 0) { | |
e3239ff9 | 159 | type->regions[i].size += size; |
95f72d1e YL |
160 | coalesced++; |
161 | break; | |
162 | } | |
163 | } | |
164 | ||
e3239ff9 BH |
165 | if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1)) { |
166 | memblock_coalesce_regions(type, i, i+1); | |
95f72d1e YL |
167 | coalesced++; |
168 | } | |
169 | ||
170 | if (coalesced) | |
171 | return coalesced; | |
e3239ff9 | 172 | if (type->cnt >= MAX_MEMBLOCK_REGIONS) |
95f72d1e YL |
173 | return -1; |
174 | ||
175 | /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */ | |
e3239ff9 BH |
176 | 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; | |
95f72d1e | 180 | } else { |
e3239ff9 BH |
181 | type->regions[i+1].base = base; |
182 | type->regions[i+1].size = size; | |
95f72d1e YL |
183 | break; |
184 | } | |
185 | } | |
186 | ||
e3239ff9 BH |
187 | if (base < type->regions[0].base) { |
188 | type->regions[0].base = base; | |
189 | type->regions[0].size = size; | |
95f72d1e | 190 | } |
e3239ff9 | 191 | type->cnt++; |
95f72d1e YL |
192 | |
193 | return 0; | |
194 | } | |
195 | ||
2898cc4c | 196 | long memblock_add(phys_addr_t base, phys_addr_t size) |
95f72d1e | 197 | { |
e3239ff9 | 198 | return memblock_add_region(&memblock.memory, base, size); |
95f72d1e YL |
199 | |
200 | } | |
201 | ||
2898cc4c | 202 | static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size) |
95f72d1e | 203 | { |
2898cc4c BH |
204 | phys_addr_t rgnbegin, rgnend; |
205 | phys_addr_t end = base + size; | |
95f72d1e YL |
206 | int i; |
207 | ||
208 | rgnbegin = rgnend = 0; /* supress gcc warnings */ | |
209 | ||
210 | /* Find the region where (base, size) belongs to */ | |
e3239ff9 BH |
211 | for (i=0; i < type->cnt; i++) { |
212 | rgnbegin = type->regions[i].base; | |
213 | rgnend = rgnbegin + type->regions[i].size; | |
95f72d1e YL |
214 | |
215 | if ((rgnbegin <= base) && (end <= rgnend)) | |
216 | break; | |
217 | } | |
218 | ||
219 | /* Didn't find the region */ | |
e3239ff9 | 220 | if (i == type->cnt) |
95f72d1e YL |
221 | return -1; |
222 | ||
223 | /* Check to see if we are removing entire region */ | |
224 | if ((rgnbegin == base) && (rgnend == end)) { | |
e3239ff9 | 225 | memblock_remove_region(type, i); |
95f72d1e YL |
226 | return 0; |
227 | } | |
228 | ||
229 | /* Check to see if region is matching at the front */ | |
230 | if (rgnbegin == base) { | |
e3239ff9 BH |
231 | type->regions[i].base = end; |
232 | type->regions[i].size -= size; | |
95f72d1e YL |
233 | return 0; |
234 | } | |
235 | ||
236 | /* Check to see if the region is matching at the end */ | |
237 | if (rgnend == end) { | |
e3239ff9 | 238 | type->regions[i].size -= size; |
95f72d1e YL |
239 | 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 | */ | |
e3239ff9 BH |
246 | type->regions[i].size = base - type->regions[i].base; |
247 | return memblock_add_region(type, end, rgnend - end); | |
95f72d1e YL |
248 | } |
249 | ||
2898cc4c | 250 | long memblock_remove(phys_addr_t base, phys_addr_t size) |
95f72d1e YL |
251 | { |
252 | return __memblock_remove(&memblock.memory, base, size); | |
253 | } | |
254 | ||
2898cc4c | 255 | long __init memblock_free(phys_addr_t base, phys_addr_t size) |
95f72d1e YL |
256 | { |
257 | return __memblock_remove(&memblock.reserved, base, size); | |
258 | } | |
259 | ||
2898cc4c | 260 | long __init memblock_reserve(phys_addr_t base, phys_addr_t size) |
95f72d1e | 261 | { |
e3239ff9 | 262 | struct memblock_type *_rgn = &memblock.reserved; |
95f72d1e YL |
263 | |
264 | BUG_ON(0 == size); | |
265 | ||
266 | return memblock_add_region(_rgn, base, size); | |
267 | } | |
268 | ||
2898cc4c | 269 | long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size) |
95f72d1e YL |
270 | { |
271 | unsigned long i; | |
272 | ||
e3239ff9 | 273 | for (i = 0; i < type->cnt; i++) { |
2898cc4c BH |
274 | phys_addr_t rgnbase = type->regions[i].base; |
275 | phys_addr_t rgnsize = type->regions[i].size; | |
95f72d1e YL |
276 | if (memblock_addrs_overlap(base, size, rgnbase, rgnsize)) |
277 | break; | |
278 | } | |
279 | ||
e3239ff9 | 280 | return (i < type->cnt) ? i : -1; |
95f72d1e YL |
281 | } |
282 | ||
2898cc4c | 283 | static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size) |
95f72d1e YL |
284 | { |
285 | return addr & ~(size - 1); | |
286 | } | |
287 | ||
2898cc4c | 288 | static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size) |
95f72d1e YL |
289 | { |
290 | return (addr + (size - 1)) & ~(size - 1); | |
291 | } | |
292 | ||
2898cc4c BH |
293 | static phys_addr_t __init memblock_alloc_region(phys_addr_t start, phys_addr_t end, |
294 | phys_addr_t size, phys_addr_t align) | |
95f72d1e | 295 | { |
2898cc4c | 296 | phys_addr_t base, res_base; |
95f72d1e YL |
297 | 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) | |
2898cc4c | 305 | base = ~(phys_addr_t)0; |
95f72d1e YL |
306 | return base; |
307 | } | |
e3239ff9 | 308 | res_base = memblock.reserved.regions[j].base; |
95f72d1e YL |
309 | if (res_base < size) |
310 | break; | |
311 | base = memblock_align_down(res_base - size, align); | |
312 | } | |
313 | ||
2898cc4c | 314 | return ~(phys_addr_t)0; |
95f72d1e YL |
315 | } |
316 | ||
2898cc4c | 317 | phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid) |
c3f72b57 BH |
318 | { |
319 | *nid = 0; | |
320 | ||
321 | return end; | |
322 | } | |
323 | ||
2898cc4c BH |
324 | static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp, |
325 | phys_addr_t size, | |
326 | phys_addr_t align, int nid) | |
95f72d1e | 327 | { |
2898cc4c | 328 | phys_addr_t start, end; |
95f72d1e YL |
329 | |
330 | start = mp->base; | |
331 | end = start + mp->size; | |
332 | ||
333 | start = memblock_align_up(start, align); | |
334 | while (start < end) { | |
2898cc4c | 335 | phys_addr_t this_end; |
95f72d1e YL |
336 | int this_nid; |
337 | ||
35a1f0bd | 338 | this_end = memblock_nid_range(start, end, &this_nid); |
95f72d1e | 339 | if (this_nid == nid) { |
2898cc4c BH |
340 | phys_addr_t ret = memblock_alloc_region(start, this_end, size, align); |
341 | if (ret != ~(phys_addr_t)0) | |
95f72d1e YL |
342 | return ret; |
343 | } | |
344 | start = this_end; | |
345 | } | |
346 | ||
2898cc4c | 347 | return ~(phys_addr_t)0; |
95f72d1e YL |
348 | } |
349 | ||
2898cc4c | 350 | phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid) |
95f72d1e | 351 | { |
e3239ff9 | 352 | struct memblock_type *mem = &memblock.memory; |
95f72d1e YL |
353 | int i; |
354 | ||
355 | BUG_ON(0 == size); | |
356 | ||
c3f72b57 BH |
357 | /* 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 | */ | |
95f72d1e YL |
361 | size = memblock_align_up(size, align); |
362 | ||
363 | for (i = 0; i < mem->cnt; i++) { | |
2898cc4c | 364 | phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i], |
95f72d1e | 365 | size, align, nid); |
2898cc4c | 366 | if (ret != ~(phys_addr_t)0) |
95f72d1e YL |
367 | return ret; |
368 | } | |
369 | ||
370 | return memblock_alloc(size, align); | |
371 | } | |
372 | ||
2898cc4c | 373 | phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align) |
95f72d1e | 374 | { |
e63075a3 | 375 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
95f72d1e YL |
376 | } |
377 | ||
2898cc4c | 378 | phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
95f72d1e | 379 | { |
2898cc4c | 380 | phys_addr_t alloc; |
95f72d1e YL |
381 | |
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 | ||
2898cc4c | 391 | phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
95f72d1e | 392 | { |
c3f72b57 | 393 | long i; |
2898cc4c BH |
394 | phys_addr_t base = 0; |
395 | phys_addr_t res_base; | |
95f72d1e YL |
396 | |
397 | BUG_ON(0 == size); | |
398 | ||
399 | size = memblock_align_up(size, align); | |
400 | ||
c3f72b57 | 401 | /* Pump up max_addr */ |
e63075a3 BH |
402 | if (max_addr == MEMBLOCK_ALLOC_ACCESSIBLE) |
403 | max_addr = memblock.current_limit; | |
c3f72b57 BH |
404 | |
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 | */ | |
95f72d1e | 409 | for (i = memblock.memory.cnt - 1; i >= 0; i--) { |
2898cc4c BH |
410 | phys_addr_t memblockbase = memblock.memory.regions[i].base; |
411 | phys_addr_t memblocksize = memblock.memory.regions[i].size; | |
95f72d1e YL |
412 | |
413 | if (memblocksize < size) | |
414 | continue; | |
c3f72b57 BH |
415 | base = min(memblockbase + memblocksize, max_addr); |
416 | res_base = memblock_alloc_region(memblockbase, base, size, align); | |
2898cc4c | 417 | if (res_base != ~(phys_addr_t)0) |
c3f72b57 | 418 | return res_base; |
95f72d1e YL |
419 | } |
420 | return 0; | |
421 | } | |
422 | ||
423 | /* You must call memblock_analyze() before this. */ | |
2898cc4c | 424 | phys_addr_t __init memblock_phys_mem_size(void) |
95f72d1e YL |
425 | { |
426 | return memblock.memory.size; | |
427 | } | |
428 | ||
2898cc4c | 429 | phys_addr_t memblock_end_of_DRAM(void) |
95f72d1e YL |
430 | { |
431 | int idx = memblock.memory.cnt - 1; | |
432 | ||
e3239ff9 | 433 | return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); |
95f72d1e YL |
434 | } |
435 | ||
436 | /* You must call memblock_analyze() after this. */ | |
2898cc4c | 437 | void __init memblock_enforce_memory_limit(phys_addr_t memory_limit) |
95f72d1e YL |
438 | { |
439 | unsigned long i; | |
2898cc4c | 440 | phys_addr_t limit; |
e3239ff9 | 441 | struct memblock_region *p; |
95f72d1e YL |
442 | |
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++) { | |
e3239ff9 BH |
449 | if (limit > memblock.memory.regions[i].size) { |
450 | limit -= memblock.memory.regions[i].size; | |
95f72d1e YL |
451 | continue; |
452 | } | |
453 | ||
e3239ff9 | 454 | memblock.memory.regions[i].size = limit; |
95f72d1e YL |
455 | memblock.memory.cnt = i + 1; |
456 | break; | |
457 | } | |
458 | ||
95f72d1e YL |
459 | 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++) { | |
e3239ff9 | 463 | p = &memblock.reserved.regions[i]; |
95f72d1e YL |
464 | |
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 | ||
2898cc4c | 477 | static int memblock_search(struct memblock_type *type, phys_addr_t addr) |
72d4b0b4 BH |
478 | { |
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 | ||
2898cc4c | 495 | int __init memblock_is_reserved(phys_addr_t addr) |
95f72d1e | 496 | { |
72d4b0b4 BH |
497 | return memblock_search(&memblock.reserved, addr) != -1; |
498 | } | |
95f72d1e | 499 | |
2898cc4c | 500 | int memblock_is_memory(phys_addr_t addr) |
72d4b0b4 BH |
501 | { |
502 | return memblock_search(&memblock.memory, addr) != -1; | |
503 | } | |
504 | ||
2898cc4c | 505 | int memblock_is_region_memory(phys_addr_t base, phys_addr_t size) |
72d4b0b4 BH |
506 | { |
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); | |
95f72d1e YL |
514 | } |
515 | ||
2898cc4c | 516 | int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) |
95f72d1e | 517 | { |
f1c2c19c | 518 | return memblock_overlaps_region(&memblock.reserved, base, size) >= 0; |
95f72d1e YL |
519 | } |
520 | ||
e63075a3 | 521 | |
2898cc4c | 522 | void __init memblock_set_current_limit(phys_addr_t limit) |
e63075a3 BH |
523 | { |
524 | memblock.current_limit = limit; | |
525 | } | |
526 |