memblock: Move functions around into a more sensible order
[cascardo/linux.git] / mm / memblock.c
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/poison.h>
17 #include <linux/memblock.h>
18
19 struct memblock memblock;
20
21 static int memblock_debug;
22 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1];
23 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1];
24
25 #define MEMBLOCK_ERROR  (~(phys_addr_t)0)
26
27 /*
28  * Address comparison utilities
29  */
30
31 static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size)
32 {
33         return addr & ~(size - 1);
34 }
35
36 static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size)
37 {
38         return (addr + (size - 1)) & ~(size - 1);
39 }
40
41 static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
42                                        phys_addr_t base2, phys_addr_t size2)
43 {
44         return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
45 }
46
47 static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
48                                phys_addr_t base2, phys_addr_t size2)
49 {
50         if (base2 == base1 + size1)
51                 return 1;
52         else if (base1 == base2 + size2)
53                 return -1;
54
55         return 0;
56 }
57
58 static long memblock_regions_adjacent(struct memblock_type *type,
59                                  unsigned long r1, unsigned long r2)
60 {
61         phys_addr_t base1 = type->regions[r1].base;
62         phys_addr_t size1 = type->regions[r1].size;
63         phys_addr_t base2 = type->regions[r2].base;
64         phys_addr_t size2 = type->regions[r2].size;
65
66         return memblock_addrs_adjacent(base1, size1, base2, size2);
67 }
68
69 long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
70 {
71         unsigned long i;
72
73         for (i = 0; i < type->cnt; i++) {
74                 phys_addr_t rgnbase = type->regions[i].base;
75                 phys_addr_t rgnsize = type->regions[i].size;
76                 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
77                         break;
78         }
79
80         return (i < type->cnt) ? i : -1;
81 }
82
83 /*
84  * Find, allocate, deallocate or reserve unreserved regions. All allocations
85  * are top-down.
86  */
87
88 static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end,
89                                           phys_addr_t size, phys_addr_t align)
90 {
91         phys_addr_t base, res_base;
92         long j;
93
94         base = memblock_align_down((end - size), align);
95         while (start <= base) {
96                 j = memblock_overlaps_region(&memblock.reserved, base, size);
97                 if (j < 0)
98                         return base;
99                 res_base = memblock.reserved.regions[j].base;
100                 if (res_base < size)
101                         break;
102                 base = memblock_align_down(res_base - size, align);
103         }
104
105         return MEMBLOCK_ERROR;
106 }
107
108 static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
109 {
110         long i;
111         phys_addr_t base = 0;
112         phys_addr_t res_base;
113
114         BUG_ON(0 == size);
115
116         size = memblock_align_up(size, align);
117
118         /* Pump up max_addr */
119         if (max_addr == MEMBLOCK_ALLOC_ACCESSIBLE)
120                 max_addr = memblock.current_limit;
121
122         /* We do a top-down search, this tends to limit memory
123          * fragmentation by keeping early boot allocs near the
124          * top of memory
125          */
126         for (i = memblock.memory.cnt - 1; i >= 0; i--) {
127                 phys_addr_t memblockbase = memblock.memory.regions[i].base;
128                 phys_addr_t memblocksize = memblock.memory.regions[i].size;
129
130                 if (memblocksize < size)
131                         continue;
132                 base = min(memblockbase + memblocksize, max_addr);
133                 res_base = memblock_find_region(memblockbase, base, size, align);
134                 if (res_base != MEMBLOCK_ERROR)
135                         return res_base;
136         }
137         return MEMBLOCK_ERROR;
138 }
139
140 static void memblock_remove_region(struct memblock_type *type, unsigned long r)
141 {
142         unsigned long i;
143
144         for (i = r; i < type->cnt - 1; i++) {
145                 type->regions[i].base = type->regions[i + 1].base;
146                 type->regions[i].size = type->regions[i + 1].size;
147         }
148         type->cnt--;
149 }
150
151 /* Assumption: base addr of region 1 < base addr of region 2 */
152 static void memblock_coalesce_regions(struct memblock_type *type,
153                 unsigned long r1, unsigned long r2)
154 {
155         type->regions[r1].size += type->regions[r2].size;
156         memblock_remove_region(type, r2);
157 }
158
159 static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
160 {
161         unsigned long coalesced = 0;
162         long adjacent, i;
163
164         if ((type->cnt == 1) && (type->regions[0].size == 0)) {
165                 type->regions[0].base = base;
166                 type->regions[0].size = size;
167                 return 0;
168         }
169
170         /* First try and coalesce this MEMBLOCK with another. */
171         for (i = 0; i < type->cnt; i++) {
172                 phys_addr_t rgnbase = type->regions[i].base;
173                 phys_addr_t rgnsize = type->regions[i].size;
174
175                 if ((rgnbase == base) && (rgnsize == size))
176                         /* Already have this region, so we're done */
177                         return 0;
178
179                 adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
180                 if (adjacent > 0) {
181                         type->regions[i].base -= size;
182                         type->regions[i].size += size;
183                         coalesced++;
184                         break;
185                 } else if (adjacent < 0) {
186                         type->regions[i].size += size;
187                         coalesced++;
188                         break;
189                 }
190         }
191
192         if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1)) {
193                 memblock_coalesce_regions(type, i, i+1);
194                 coalesced++;
195         }
196
197         if (coalesced)
198                 return coalesced;
199         if (type->cnt >= type->max)
200                 return -1;
201
202         /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
203         for (i = type->cnt - 1; i >= 0; i--) {
204                 if (base < type->regions[i].base) {
205                         type->regions[i+1].base = type->regions[i].base;
206                         type->regions[i+1].size = type->regions[i].size;
207                 } else {
208                         type->regions[i+1].base = base;
209                         type->regions[i+1].size = size;
210                         break;
211                 }
212         }
213
214         if (base < type->regions[0].base) {
215                 type->regions[0].base = base;
216                 type->regions[0].size = size;
217         }
218         type->cnt++;
219
220         return 0;
221 }
222
223 long memblock_add(phys_addr_t base, phys_addr_t size)
224 {
225         return memblock_add_region(&memblock.memory, base, size);
226
227 }
228
229 static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
230 {
231         phys_addr_t rgnbegin, rgnend;
232         phys_addr_t end = base + size;
233         int i;
234
235         rgnbegin = rgnend = 0; /* supress gcc warnings */
236
237         /* Find the region where (base, size) belongs to */
238         for (i=0; i < type->cnt; i++) {
239                 rgnbegin = type->regions[i].base;
240                 rgnend = rgnbegin + type->regions[i].size;
241
242                 if ((rgnbegin <= base) && (end <= rgnend))
243                         break;
244         }
245
246         /* Didn't find the region */
247         if (i == type->cnt)
248                 return -1;
249
250         /* Check to see if we are removing entire region */
251         if ((rgnbegin == base) && (rgnend == end)) {
252                 memblock_remove_region(type, i);
253                 return 0;
254         }
255
256         /* Check to see if region is matching at the front */
257         if (rgnbegin == base) {
258                 type->regions[i].base = end;
259                 type->regions[i].size -= size;
260                 return 0;
261         }
262
263         /* Check to see if the region is matching at the end */
264         if (rgnend == end) {
265                 type->regions[i].size -= size;
266                 return 0;
267         }
268
269         /*
270          * We need to split the entry -  adjust the current one to the
271          * beginging of the hole and add the region after hole.
272          */
273         type->regions[i].size = base - type->regions[i].base;
274         return memblock_add_region(type, end, rgnend - end);
275 }
276
277 long memblock_remove(phys_addr_t base, phys_addr_t size)
278 {
279         return __memblock_remove(&memblock.memory, base, size);
280 }
281
282 long __init memblock_free(phys_addr_t base, phys_addr_t size)
283 {
284         return __memblock_remove(&memblock.reserved, base, size);
285 }
286
287 long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
288 {
289         struct memblock_type *_rgn = &memblock.reserved;
290
291         BUG_ON(0 == size);
292
293         return memblock_add_region(_rgn, base, size);
294 }
295
296 phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
297 {
298         phys_addr_t found;
299
300         /* We align the size to limit fragmentation. Without this, a lot of
301          * small allocs quickly eat up the whole reserve array on sparc
302          */
303         size = memblock_align_up(size, align);
304
305         found = memblock_find_base(size, align, max_addr);
306         if (found != MEMBLOCK_ERROR &&
307             memblock_add_region(&memblock.reserved, found, size) >= 0)
308                 return found;
309
310         return 0;
311 }
312
313 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
314 {
315         phys_addr_t alloc;
316
317         alloc = __memblock_alloc_base(size, align, max_addr);
318
319         if (alloc == 0)
320                 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
321                       (unsigned long long) size, (unsigned long long) max_addr);
322
323         return alloc;
324 }
325
326 phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
327 {
328         return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
329 }
330
331
332 /*
333  * Additional node-local allocators. Search for node memory is bottom up
334  * and walks memblock regions within that node bottom-up as well, but allocation
335  * within an memblock region is top-down.
336  */
337
338 phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
339 {
340         *nid = 0;
341
342         return end;
343 }
344
345 static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
346                                                phys_addr_t size,
347                                                phys_addr_t align, int nid)
348 {
349         phys_addr_t start, end;
350
351         start = mp->base;
352         end = start + mp->size;
353
354         start = memblock_align_up(start, align);
355         while (start < end) {
356                 phys_addr_t this_end;
357                 int this_nid;
358
359                 this_end = memblock_nid_range(start, end, &this_nid);
360                 if (this_nid == nid) {
361                         phys_addr_t ret = memblock_find_region(start, this_end, size, align);
362                         if (ret != MEMBLOCK_ERROR &&
363                             memblock_add_region(&memblock.reserved, ret, size) >= 0)
364                                 return ret;
365                 }
366                 start = this_end;
367         }
368
369         return MEMBLOCK_ERROR;
370 }
371
372 phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
373 {
374         struct memblock_type *mem = &memblock.memory;
375         int i;
376
377         BUG_ON(0 == size);
378
379         /* We align the size to limit fragmentation. Without this, a lot of
380          * small allocs quickly eat up the whole reserve array on sparc
381          */
382         size = memblock_align_up(size, align);
383
384         /* We do a bottom-up search for a region with the right
385          * nid since that's easier considering how memblock_nid_range()
386          * works
387          */
388         for (i = 0; i < mem->cnt; i++) {
389                 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
390                                                size, align, nid);
391                 if (ret != MEMBLOCK_ERROR)
392                         return ret;
393         }
394
395         return memblock_alloc(size, align);
396 }
397
398 /* You must call memblock_analyze() before this. */
399 phys_addr_t __init memblock_phys_mem_size(void)
400 {
401         return memblock.memory_size;
402 }
403
404 phys_addr_t memblock_end_of_DRAM(void)
405 {
406         int idx = memblock.memory.cnt - 1;
407
408         return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
409 }
410
411 /* You must call memblock_analyze() after this. */
412 void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
413 {
414         unsigned long i;
415         phys_addr_t limit;
416         struct memblock_region *p;
417
418         if (!memory_limit)
419                 return;
420
421         /* Truncate the memblock regions to satisfy the memory limit. */
422         limit = memory_limit;
423         for (i = 0; i < memblock.memory.cnt; i++) {
424                 if (limit > memblock.memory.regions[i].size) {
425                         limit -= memblock.memory.regions[i].size;
426                         continue;
427                 }
428
429                 memblock.memory.regions[i].size = limit;
430                 memblock.memory.cnt = i + 1;
431                 break;
432         }
433
434         memory_limit = memblock_end_of_DRAM();
435
436         /* And truncate any reserves above the limit also. */
437         for (i = 0; i < memblock.reserved.cnt; i++) {
438                 p = &memblock.reserved.regions[i];
439
440                 if (p->base > memory_limit)
441                         p->size = 0;
442                 else if ((p->base + p->size) > memory_limit)
443                         p->size = memory_limit - p->base;
444
445                 if (p->size == 0) {
446                         memblock_remove_region(&memblock.reserved, i);
447                         i--;
448                 }
449         }
450 }
451
452 static int memblock_search(struct memblock_type *type, phys_addr_t addr)
453 {
454         unsigned int left = 0, right = type->cnt;
455
456         do {
457                 unsigned int mid = (right + left) / 2;
458
459                 if (addr < type->regions[mid].base)
460                         right = mid;
461                 else if (addr >= (type->regions[mid].base +
462                                   type->regions[mid].size))
463                         left = mid + 1;
464                 else
465                         return mid;
466         } while (left < right);
467         return -1;
468 }
469
470 int __init memblock_is_reserved(phys_addr_t addr)
471 {
472         return memblock_search(&memblock.reserved, addr) != -1;
473 }
474
475 int memblock_is_memory(phys_addr_t addr)
476 {
477         return memblock_search(&memblock.memory, addr) != -1;
478 }
479
480 int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
481 {
482         int idx = memblock_search(&memblock.reserved, base);
483
484         if (idx == -1)
485                 return 0;
486         return memblock.reserved.regions[idx].base <= base &&
487                 (memblock.reserved.regions[idx].base +
488                  memblock.reserved.regions[idx].size) >= (base + size);
489 }
490
491 int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
492 {
493         return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
494 }
495
496
497 void __init memblock_set_current_limit(phys_addr_t limit)
498 {
499         memblock.current_limit = limit;
500 }
501
502 static void memblock_dump(struct memblock_type *region, char *name)
503 {
504         unsigned long long base, size;
505         int i;
506
507         pr_info(" %s.cnt  = 0x%lx\n", name, region->cnt);
508
509         for (i = 0; i < region->cnt; i++) {
510                 base = region->regions[i].base;
511                 size = region->regions[i].size;
512
513                 pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n",
514                     name, i, base, base + size - 1, size);
515         }
516 }
517
518 void memblock_dump_all(void)
519 {
520         if (!memblock_debug)
521                 return;
522
523         pr_info("MEMBLOCK configuration:\n");
524         pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
525
526         memblock_dump(&memblock.memory, "memory");
527         memblock_dump(&memblock.reserved, "reserved");
528 }
529
530 void __init memblock_analyze(void)
531 {
532         int i;
533
534         /* Check marker in the unused last array entry */
535         WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
536                 != (phys_addr_t)RED_INACTIVE);
537         WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
538                 != (phys_addr_t)RED_INACTIVE);
539
540         memblock.memory_size = 0;
541
542         for (i = 0; i < memblock.memory.cnt; i++)
543                 memblock.memory_size += memblock.memory.regions[i].size;
544 }
545
546 void __init memblock_init(void)
547 {
548         /* Hookup the initial arrays */
549         memblock.memory.regions = memblock_memory_init_regions;
550         memblock.memory.max             = INIT_MEMBLOCK_REGIONS;
551         memblock.reserved.regions       = memblock_reserved_init_regions;
552         memblock.reserved.max   = INIT_MEMBLOCK_REGIONS;
553
554         /* Write a marker in the unused last array entry */
555         memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
556         memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
557
558         /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
559          * This simplifies the memblock_add() code below...
560          */
561         memblock.memory.regions[0].base = 0;
562         memblock.memory.regions[0].size = 0;
563         memblock.memory.cnt = 1;
564
565         /* Ditto. */
566         memblock.reserved.regions[0].base = 0;
567         memblock.reserved.regions[0].size = 0;
568         memblock.reserved.cnt = 1;
569
570         memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
571 }
572
573 static int __init early_memblock(char *p)
574 {
575         if (p && strstr(p, "debug"))
576                 memblock_debug = 1;
577         return 0;
578 }
579 early_param("memblock", early_memblock);
580