Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 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> |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/bitops.h> |
Benjamin Herrenschmidt | 449e8df | 2010-07-06 15:39:07 -0700 | [diff] [blame] | 17 | #include <linux/poison.h> |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame] | 18 | #include <linux/pfn.h> |
Benjamin Herrenschmidt | 6d03b88 | 2010-07-06 15:39:19 -0700 | [diff] [blame] | 19 | #include <linux/debugfs.h> |
| 20 | #include <linux/seq_file.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 21 | #include <linux/memblock.h> |
| 22 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 23 | struct memblock memblock __initdata_memblock; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 24 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 25 | int memblock_debug __initdata_memblock; |
| 26 | int memblock_can_resize __initdata_memblock; |
| 27 | static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1] __initdata_memblock; |
| 28 | static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1] __initdata_memblock; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 29 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 30 | /* inline so we don't get a warning when pr_debug is compiled out */ |
| 31 | static inline const char *memblock_type_name(struct memblock_type *type) |
| 32 | { |
| 33 | if (type == &memblock.memory) |
| 34 | return "memory"; |
| 35 | else if (type == &memblock.reserved) |
| 36 | return "reserved"; |
| 37 | else |
| 38 | return "unknown"; |
| 39 | } |
| 40 | |
Tejun Heo | 0ce6929 | 2011-07-28 10:57:00 +0200 | [diff] [blame] | 41 | /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */ |
| 42 | static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size) |
| 43 | { |
| 44 | return *size = min(*size, (phys_addr_t)ULLONG_MAX - base); |
| 45 | } |
| 46 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 47 | /* |
| 48 | * Address comparison utilities |
| 49 | */ |
| 50 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 51 | static phys_addr_t __init_memblock memblock_align_down(phys_addr_t addr, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 52 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 53 | return addr & ~(size - 1); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 54 | } |
| 55 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 56 | static phys_addr_t __init_memblock memblock_align_up(phys_addr_t addr, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 57 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 58 | return (addr + (size - 1)) & ~(size - 1); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 59 | } |
| 60 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 61 | static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 62 | phys_addr_t base2, phys_addr_t size2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 63 | { |
| 64 | return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); |
| 65 | } |
| 66 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 67 | long __init_memblock memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 68 | { |
| 69 | unsigned long i; |
| 70 | |
| 71 | for (i = 0; i < type->cnt; i++) { |
| 72 | phys_addr_t rgnbase = type->regions[i].base; |
| 73 | phys_addr_t rgnsize = type->regions[i].size; |
| 74 | if (memblock_addrs_overlap(base, size, rgnbase, rgnsize)) |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | return (i < type->cnt) ? i : -1; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Find, allocate, deallocate or reserve unreserved regions. All allocations |
| 83 | * are top-down. |
| 84 | */ |
| 85 | |
Yinghai Lu | cd79481 | 2010-10-11 12:34:09 -0700 | [diff] [blame] | 86 | static phys_addr_t __init_memblock memblock_find_region(phys_addr_t start, phys_addr_t end, |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 87 | phys_addr_t size, phys_addr_t align) |
| 88 | { |
| 89 | phys_addr_t base, res_base; |
| 90 | long j; |
| 91 | |
Yinghai Lu | f1af98c | 2010-10-04 14:57:39 -0700 | [diff] [blame] | 92 | /* In case, huge size is requested */ |
| 93 | if (end < size) |
| 94 | return MEMBLOCK_ERROR; |
| 95 | |
| 96 | base = memblock_align_down((end - size), align); |
| 97 | |
Benjamin Herrenschmidt | 25818f0 | 2010-07-28 15:25:10 +1000 | [diff] [blame] | 98 | /* Prevent allocations returning 0 as it's also used to |
| 99 | * indicate an allocation failure |
| 100 | */ |
| 101 | if (start == 0) |
| 102 | start = PAGE_SIZE; |
| 103 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 104 | while (start <= base) { |
| 105 | j = memblock_overlaps_region(&memblock.reserved, base, size); |
| 106 | if (j < 0) |
| 107 | return base; |
| 108 | res_base = memblock.reserved.regions[j].base; |
| 109 | if (res_base < size) |
| 110 | break; |
| 111 | base = memblock_align_down(res_base - size, align); |
| 112 | } |
| 113 | |
| 114 | return MEMBLOCK_ERROR; |
| 115 | } |
| 116 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 117 | static phys_addr_t __init_memblock memblock_find_base(phys_addr_t size, |
| 118 | phys_addr_t align, phys_addr_t start, phys_addr_t end) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 119 | { |
| 120 | long i; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 121 | |
| 122 | BUG_ON(0 == size); |
| 123 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 124 | /* Pump up max_addr */ |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 125 | if (end == MEMBLOCK_ALLOC_ACCESSIBLE) |
| 126 | end = memblock.current_limit; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 127 | |
| 128 | /* We do a top-down search, this tends to limit memory |
| 129 | * fragmentation by keeping early boot allocs near the |
| 130 | * top of memory |
| 131 | */ |
| 132 | for (i = memblock.memory.cnt - 1; i >= 0; i--) { |
| 133 | phys_addr_t memblockbase = memblock.memory.regions[i].base; |
| 134 | phys_addr_t memblocksize = memblock.memory.regions[i].size; |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 135 | phys_addr_t bottom, top, found; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 136 | |
| 137 | if (memblocksize < size) |
| 138 | continue; |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 139 | if ((memblockbase + memblocksize) <= start) |
| 140 | break; |
| 141 | bottom = max(memblockbase, start); |
| 142 | top = min(memblockbase + memblocksize, end); |
| 143 | if (bottom >= top) |
| 144 | continue; |
| 145 | found = memblock_find_region(bottom, top, size, align); |
| 146 | if (found != MEMBLOCK_ERROR) |
| 147 | return found; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 148 | } |
| 149 | return MEMBLOCK_ERROR; |
| 150 | } |
| 151 | |
Yinghai Lu | 5303b68 | 2010-07-28 15:38:40 +1000 | [diff] [blame] | 152 | /* |
| 153 | * Find a free area with specified alignment in a specific range. |
| 154 | */ |
| 155 | u64 __init_memblock memblock_find_in_range(u64 start, u64 end, u64 size, u64 align) |
| 156 | { |
| 157 | return memblock_find_base(size, align, start, end); |
| 158 | } |
| 159 | |
Yinghai Lu | 7950c40 | 2010-08-25 13:39:14 -0700 | [diff] [blame] | 160 | /* |
| 161 | * Free memblock.reserved.regions |
| 162 | */ |
| 163 | int __init_memblock memblock_free_reserved_regions(void) |
| 164 | { |
| 165 | if (memblock.reserved.regions == memblock_reserved_init_regions) |
| 166 | return 0; |
| 167 | |
| 168 | return memblock_free(__pa(memblock.reserved.regions), |
| 169 | sizeof(struct memblock_region) * memblock.reserved.max); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Reserve memblock.reserved.regions |
| 174 | */ |
| 175 | int __init_memblock memblock_reserve_reserved_regions(void) |
| 176 | { |
| 177 | if (memblock.reserved.regions == memblock_reserved_init_regions) |
| 178 | return 0; |
| 179 | |
| 180 | return memblock_reserve(__pa(memblock.reserved.regions), |
| 181 | sizeof(struct memblock_region) * memblock.reserved.max); |
| 182 | } |
| 183 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 184 | static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 185 | { |
| 186 | unsigned long i; |
| 187 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 188 | for (i = r; i < type->cnt - 1; i++) { |
| 189 | type->regions[i].base = type->regions[i + 1].base; |
| 190 | type->regions[i].size = type->regions[i + 1].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 191 | } |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 192 | type->cnt--; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 193 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 194 | /* Special case for empty arrays */ |
| 195 | if (type->cnt == 0) { |
| 196 | type->cnt = 1; |
| 197 | type->regions[0].base = 0; |
| 198 | type->regions[0].size = 0; |
| 199 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 200 | } |
| 201 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 202 | /* Defined below but needed now */ |
| 203 | static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size); |
| 204 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 205 | static int __init_memblock memblock_double_array(struct memblock_type *type) |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 206 | { |
| 207 | struct memblock_region *new_array, *old_array; |
| 208 | phys_addr_t old_size, new_size, addr; |
| 209 | int use_slab = slab_is_available(); |
| 210 | |
| 211 | /* We don't allow resizing until we know about the reserved regions |
| 212 | * of memory that aren't suitable for allocation |
| 213 | */ |
| 214 | if (!memblock_can_resize) |
| 215 | return -1; |
| 216 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 217 | /* Calculate new doubled size */ |
| 218 | old_size = type->max * sizeof(struct memblock_region); |
| 219 | new_size = old_size << 1; |
| 220 | |
| 221 | /* Try to find some space for it. |
| 222 | * |
| 223 | * WARNING: We assume that either slab_is_available() and we use it or |
| 224 | * we use MEMBLOCK for allocations. That means that this is unsafe to use |
| 225 | * when bootmem is currently active (unless bootmem itself is implemented |
| 226 | * on top of MEMBLOCK which isn't the case yet) |
| 227 | * |
| 228 | * This should however not be an issue for now, as we currently only |
| 229 | * call into MEMBLOCK while it's still active, or much later when slab is |
| 230 | * active for memory hotplug operations |
| 231 | */ |
| 232 | if (use_slab) { |
| 233 | new_array = kmalloc(new_size, GFP_KERNEL); |
| 234 | addr = new_array == NULL ? MEMBLOCK_ERROR : __pa(new_array); |
| 235 | } else |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 236 | addr = memblock_find_base(new_size, sizeof(phys_addr_t), 0, MEMBLOCK_ALLOC_ACCESSIBLE); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 237 | if (addr == MEMBLOCK_ERROR) { |
| 238 | pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", |
| 239 | memblock_type_name(type), type->max, type->max * 2); |
| 240 | return -1; |
| 241 | } |
| 242 | new_array = __va(addr); |
| 243 | |
Yinghai Lu | ea9e437 | 2010-07-28 15:13:22 +1000 | [diff] [blame] | 244 | memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]", |
| 245 | memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1); |
| 246 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 247 | /* Found space, we now need to move the array over before |
| 248 | * we add the reserved region since it may be our reserved |
| 249 | * array itself that is full. |
| 250 | */ |
| 251 | memcpy(new_array, type->regions, old_size); |
| 252 | memset(new_array + type->max, 0, old_size); |
| 253 | old_array = type->regions; |
| 254 | type->regions = new_array; |
| 255 | type->max <<= 1; |
| 256 | |
| 257 | /* If we use SLAB that's it, we are done */ |
| 258 | if (use_slab) |
| 259 | return 0; |
| 260 | |
| 261 | /* Add the new reserved region now. Should not fail ! */ |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 262 | BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size)); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 263 | |
| 264 | /* If the array wasn't our static init one, then free it. We only do |
| 265 | * that before SLAB is available as later on, we don't know whether |
| 266 | * to use kfree or free_bootmem_pages(). Shouldn't be a big deal |
| 267 | * anyways |
| 268 | */ |
| 269 | if (old_array != memblock_memory_init_regions && |
| 270 | old_array != memblock_reserved_init_regions) |
| 271 | memblock_free(__pa(old_array), old_size); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 276 | extern int __init_memblock __weak memblock_memory_can_coalesce(phys_addr_t addr1, phys_addr_t size1, |
Benjamin Herrenschmidt | d2cd563 | 2010-07-06 15:39:14 -0700 | [diff] [blame] | 277 | phys_addr_t addr2, phys_addr_t size2) |
| 278 | { |
| 279 | return 1; |
| 280 | } |
| 281 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 282 | static long __init_memblock memblock_add_region(struct memblock_type *type, |
| 283 | phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 284 | { |
Tejun Heo | 0ce6929 | 2011-07-28 10:57:00 +0200 | [diff] [blame] | 285 | phys_addr_t end = base + memblock_cap_size(base, &size); |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 286 | int i, slot = -1; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 287 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 288 | /* First try and coalesce this MEMBLOCK with others */ |
| 289 | for (i = 0; i < type->cnt; i++) { |
| 290 | struct memblock_region *rgn = &type->regions[i]; |
| 291 | phys_addr_t rend = rgn->base + rgn->size; |
| 292 | |
| 293 | /* Exit if there's no possible hits */ |
| 294 | if (rgn->base > end || rgn->size == 0) |
| 295 | break; |
| 296 | |
| 297 | /* Check if we are fully enclosed within an existing |
| 298 | * block |
| 299 | */ |
| 300 | if (rgn->base <= base && rend >= end) |
| 301 | return 0; |
| 302 | |
| 303 | /* Check if we overlap or are adjacent with the bottom |
| 304 | * of a block. |
| 305 | */ |
| 306 | if (base < rgn->base && end >= rgn->base) { |
| 307 | /* If we can't coalesce, create a new block */ |
| 308 | if (!memblock_memory_can_coalesce(base, size, |
| 309 | rgn->base, |
| 310 | rgn->size)) { |
| 311 | /* Overlap & can't coalesce are mutually |
| 312 | * exclusive, if you do that, be prepared |
| 313 | * for trouble |
| 314 | */ |
| 315 | WARN_ON(end != rgn->base); |
| 316 | goto new_block; |
| 317 | } |
| 318 | /* We extend the bottom of the block down to our |
| 319 | * base |
| 320 | */ |
| 321 | rgn->base = base; |
| 322 | rgn->size = rend - base; |
| 323 | |
| 324 | /* Return if we have nothing else to allocate |
| 325 | * (fully coalesced) |
| 326 | */ |
| 327 | if (rend >= end) |
| 328 | return 0; |
| 329 | |
| 330 | /* We continue processing from the end of the |
| 331 | * coalesced block. |
| 332 | */ |
| 333 | base = rend; |
| 334 | size = end - base; |
| 335 | } |
| 336 | |
| 337 | /* Now check if we overlap or are adjacent with the |
| 338 | * top of a block |
| 339 | */ |
| 340 | if (base <= rend && end >= rend) { |
| 341 | /* If we can't coalesce, create a new block */ |
| 342 | if (!memblock_memory_can_coalesce(rgn->base, |
| 343 | rgn->size, |
| 344 | base, size)) { |
| 345 | /* Overlap & can't coalesce are mutually |
| 346 | * exclusive, if you do that, be prepared |
| 347 | * for trouble |
| 348 | */ |
| 349 | WARN_ON(rend != base); |
| 350 | goto new_block; |
| 351 | } |
| 352 | /* We adjust our base down to enclose the |
| 353 | * original block and destroy it. It will be |
| 354 | * part of our new allocation. Since we've |
| 355 | * freed an entry, we know we won't fail |
| 356 | * to allocate one later, so we won't risk |
| 357 | * losing the original block allocation. |
| 358 | */ |
| 359 | size += (base - rgn->base); |
| 360 | base = rgn->base; |
| 361 | memblock_remove_region(type, i--); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /* If the array is empty, special case, replace the fake |
| 366 | * filler region and return |
| 367 | */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 368 | if ((type->cnt == 1) && (type->regions[0].size == 0)) { |
| 369 | type->regions[0].base = base; |
| 370 | type->regions[0].size = size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 374 | new_block: |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 375 | /* If we are out of space, we fail. It's too late to resize the array |
| 376 | * but then this shouldn't have happened in the first place. |
| 377 | */ |
| 378 | if (WARN_ON(type->cnt >= type->max)) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 379 | return -1; |
| 380 | |
| 381 | /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 382 | for (i = type->cnt - 1; i >= 0; i--) { |
| 383 | if (base < type->regions[i].base) { |
| 384 | type->regions[i+1].base = type->regions[i].base; |
| 385 | type->regions[i+1].size = type->regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 386 | } else { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 387 | type->regions[i+1].base = base; |
| 388 | type->regions[i+1].size = size; |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 389 | slot = i + 1; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | } |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 393 | if (base < type->regions[0].base) { |
| 394 | type->regions[0].base = base; |
| 395 | type->regions[0].size = size; |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 396 | slot = 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 397 | } |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 398 | type->cnt++; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 399 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 400 | /* The array is full ? Try to resize it. If that fails, we undo |
| 401 | * our allocation and return an error |
| 402 | */ |
| 403 | if (type->cnt == type->max && memblock_double_array(type)) { |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 404 | BUG_ON(slot < 0); |
| 405 | memblock_remove_region(type, slot); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 406 | return -1; |
| 407 | } |
| 408 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 412 | long __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 413 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 414 | return memblock_add_region(&memblock.memory, base, size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 415 | |
| 416 | } |
| 417 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 418 | static long __init_memblock __memblock_remove(struct memblock_type *type, |
| 419 | phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 420 | { |
Tejun Heo | 0ce6929 | 2011-07-28 10:57:00 +0200 | [diff] [blame] | 421 | phys_addr_t end = base + memblock_cap_size(base, &size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 422 | int i; |
| 423 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 424 | /* Walk through the array for collisions */ |
| 425 | for (i = 0; i < type->cnt; i++) { |
| 426 | struct memblock_region *rgn = &type->regions[i]; |
| 427 | phys_addr_t rend = rgn->base + rgn->size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 428 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 429 | /* Nothing more to do, exit */ |
| 430 | if (rgn->base > end || rgn->size == 0) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 431 | break; |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 432 | |
| 433 | /* If we fully enclose the block, drop it */ |
| 434 | if (base <= rgn->base && end >= rend) { |
| 435 | memblock_remove_region(type, i--); |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | /* If we are fully enclosed within a block |
| 440 | * then we need to split it and we are done |
| 441 | */ |
| 442 | if (base > rgn->base && end < rend) { |
| 443 | rgn->size = base - rgn->base; |
| 444 | if (!memblock_add_region(type, end, rend - end)) |
| 445 | return 0; |
| 446 | /* Failure to split is bad, we at least |
| 447 | * restore the block before erroring |
| 448 | */ |
| 449 | rgn->size = rend - rgn->base; |
| 450 | WARN_ON(1); |
| 451 | return -1; |
| 452 | } |
| 453 | |
| 454 | /* Check if we need to trim the bottom of a block */ |
| 455 | if (rgn->base < end && rend > end) { |
| 456 | rgn->size -= end - rgn->base; |
| 457 | rgn->base = end; |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | /* And check if we need to trim the top of a block */ |
| 462 | if (base < rend) |
| 463 | rgn->size -= rend - base; |
| 464 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 465 | } |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 466 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 467 | } |
| 468 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 469 | long __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 470 | { |
| 471 | return __memblock_remove(&memblock.memory, base, size); |
| 472 | } |
| 473 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 474 | long __init_memblock memblock_free(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 475 | { |
| 476 | return __memblock_remove(&memblock.reserved, base, size); |
| 477 | } |
| 478 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 479 | long __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 480 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 481 | struct memblock_type *_rgn = &memblock.reserved; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 482 | |
| 483 | BUG_ON(0 == size); |
| 484 | |
| 485 | return memblock_add_region(_rgn, base, size); |
| 486 | } |
| 487 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 488 | phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 489 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 490 | phys_addr_t found; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 491 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 492 | /* We align the size to limit fragmentation. Without this, a lot of |
| 493 | * small allocs quickly eat up the whole reserve array on sparc |
| 494 | */ |
| 495 | size = memblock_align_up(size, align); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 496 | |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 497 | found = memblock_find_base(size, align, 0, max_addr); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 498 | if (found != MEMBLOCK_ERROR && |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 499 | !memblock_add_region(&memblock.reserved, found, size)) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 500 | return found; |
| 501 | |
| 502 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 503 | } |
| 504 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 505 | phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 506 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 507 | phys_addr_t alloc; |
| 508 | |
| 509 | alloc = __memblock_alloc_base(size, align, max_addr); |
| 510 | |
| 511 | if (alloc == 0) |
| 512 | panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n", |
| 513 | (unsigned long long) size, (unsigned long long) max_addr); |
| 514 | |
| 515 | return alloc; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 516 | } |
| 517 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 518 | phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 519 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 520 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 521 | } |
| 522 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 523 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 524 | /* |
| 525 | * Additional node-local allocators. Search for node memory is bottom up |
| 526 | * and walks memblock regions within that node bottom-up as well, but allocation |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame] | 527 | * within an memblock region is top-down. XXX I plan to fix that at some stage |
| 528 | * |
| 529 | * WARNING: Only available after early_node_map[] has been populated, |
| 530 | * on some architectures, that is after all the calls to add_active_range() |
| 531 | * have been done to populate it. |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 532 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 533 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 534 | phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid) |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 535 | { |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame] | 536 | #ifdef CONFIG_ARCH_POPULATES_NODE_MAP |
| 537 | /* |
| 538 | * This code originates from sparc which really wants use to walk by addresses |
| 539 | * and returns the nid. This is not very convenient for early_pfn_map[] users |
| 540 | * as the map isn't sorted yet, and it really wants to be walked by nid. |
| 541 | * |
| 542 | * For now, I implement the inefficient method below which walks the early |
| 543 | * map multiple times. Eventually we may want to use an ARCH config option |
| 544 | * to implement a completely different method for both case. |
| 545 | */ |
| 546 | unsigned long start_pfn, end_pfn; |
| 547 | int i; |
| 548 | |
| 549 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 550 | get_pfn_range_for_nid(i, &start_pfn, &end_pfn); |
| 551 | if (start < PFN_PHYS(start_pfn) || start >= PFN_PHYS(end_pfn)) |
| 552 | continue; |
| 553 | *nid = i; |
| 554 | return min(end, PFN_PHYS(end_pfn)); |
| 555 | } |
| 556 | #endif |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 557 | *nid = 0; |
| 558 | |
| 559 | return end; |
| 560 | } |
| 561 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 562 | static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp, |
| 563 | phys_addr_t size, |
| 564 | phys_addr_t align, int nid) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 565 | { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 566 | phys_addr_t start, end; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 567 | |
| 568 | start = mp->base; |
| 569 | end = start + mp->size; |
| 570 | |
| 571 | start = memblock_align_up(start, align); |
| 572 | while (start < end) { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 573 | phys_addr_t this_end; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 574 | int this_nid; |
| 575 | |
Benjamin Herrenschmidt | 35a1f0b | 2010-07-06 15:38:58 -0700 | [diff] [blame] | 576 | this_end = memblock_nid_range(start, end, &this_nid); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 577 | if (this_nid == nid) { |
Benjamin Herrenschmidt | 3a9c2c8 | 2010-07-12 13:28:15 +1000 | [diff] [blame] | 578 | phys_addr_t ret = memblock_find_region(start, this_end, size, align); |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 579 | if (ret != MEMBLOCK_ERROR && |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 580 | !memblock_add_region(&memblock.reserved, ret, size)) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 581 | return ret; |
| 582 | } |
| 583 | start = this_end; |
| 584 | } |
| 585 | |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 586 | return MEMBLOCK_ERROR; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 587 | } |
| 588 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 589 | phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 590 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 591 | struct memblock_type *mem = &memblock.memory; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 592 | int i; |
| 593 | |
| 594 | BUG_ON(0 == size); |
| 595 | |
Benjamin Herrenschmidt | 7f219c7 | 2010-07-12 14:24:57 +1000 | [diff] [blame] | 596 | /* We align the size to limit fragmentation. Without this, a lot of |
| 597 | * small allocs quickly eat up the whole reserve array on sparc |
| 598 | */ |
| 599 | size = memblock_align_up(size, align); |
| 600 | |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 601 | /* We do a bottom-up search for a region with the right |
| 602 | * nid since that's easier considering how memblock_nid_range() |
| 603 | * works |
| 604 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 605 | for (i = 0; i < mem->cnt; i++) { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 606 | phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i], |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 607 | size, align, nid); |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 608 | if (ret != MEMBLOCK_ERROR) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 609 | return ret; |
| 610 | } |
| 611 | |
Benjamin Herrenschmidt | 9d1e249 | 2010-07-06 15:39:17 -0700 | [diff] [blame] | 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) |
| 616 | { |
| 617 | phys_addr_t res = memblock_alloc_nid(size, align, nid); |
| 618 | |
| 619 | if (res) |
| 620 | return res; |
Benjamin Herrenschmidt | 918fe8d | 2010-07-06 15:39:18 -0700 | [diff] [blame] | 621 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ANYWHERE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 622 | } |
| 623 | |
Benjamin Herrenschmidt | 9d1e249 | 2010-07-06 15:39:17 -0700 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * Remaining API functions |
| 627 | */ |
| 628 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 629 | /* You must call memblock_analyze() before this. */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 630 | phys_addr_t __init memblock_phys_mem_size(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 631 | { |
Benjamin Herrenschmidt | 4734b59 | 2010-07-28 14:31:29 +1000 | [diff] [blame] | 632 | return memblock.memory_size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 633 | } |
| 634 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 635 | phys_addr_t __init_memblock memblock_end_of_DRAM(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 636 | { |
| 637 | int idx = memblock.memory.cnt - 1; |
| 638 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 639 | return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* You must call memblock_analyze() after this. */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 643 | void __init memblock_enforce_memory_limit(phys_addr_t memory_limit) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 644 | { |
| 645 | unsigned long i; |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 646 | phys_addr_t limit; |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 647 | struct memblock_region *p; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 648 | |
| 649 | if (!memory_limit) |
| 650 | return; |
| 651 | |
| 652 | /* Truncate the memblock regions to satisfy the memory limit. */ |
| 653 | limit = memory_limit; |
| 654 | for (i = 0; i < memblock.memory.cnt; i++) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 655 | if (limit > memblock.memory.regions[i].size) { |
| 656 | limit -= memblock.memory.regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 657 | continue; |
| 658 | } |
| 659 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 660 | memblock.memory.regions[i].size = limit; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 661 | memblock.memory.cnt = i + 1; |
| 662 | break; |
| 663 | } |
| 664 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 665 | memory_limit = memblock_end_of_DRAM(); |
| 666 | |
| 667 | /* And truncate any reserves above the limit also. */ |
| 668 | for (i = 0; i < memblock.reserved.cnt; i++) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 669 | p = &memblock.reserved.regions[i]; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 670 | |
| 671 | if (p->base > memory_limit) |
| 672 | p->size = 0; |
| 673 | else if ((p->base + p->size) > memory_limit) |
| 674 | p->size = memory_limit - p->base; |
| 675 | |
| 676 | if (p->size == 0) { |
| 677 | memblock_remove_region(&memblock.reserved, i); |
| 678 | i--; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
Yinghai Lu | cd79481 | 2010-10-11 12:34:09 -0700 | [diff] [blame] | 683 | static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 684 | { |
| 685 | unsigned int left = 0, right = type->cnt; |
| 686 | |
| 687 | do { |
| 688 | unsigned int mid = (right + left) / 2; |
| 689 | |
| 690 | if (addr < type->regions[mid].base) |
| 691 | right = mid; |
| 692 | else if (addr >= (type->regions[mid].base + |
| 693 | type->regions[mid].size)) |
| 694 | left = mid + 1; |
| 695 | else |
| 696 | return mid; |
| 697 | } while (left < right); |
| 698 | return -1; |
| 699 | } |
| 700 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 701 | int __init memblock_is_reserved(phys_addr_t addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 702 | { |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 703 | return memblock_search(&memblock.reserved, addr) != -1; |
| 704 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 705 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 706 | int __init_memblock memblock_is_memory(phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 707 | { |
| 708 | return memblock_search(&memblock.memory, addr) != -1; |
| 709 | } |
| 710 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 711 | int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 712 | { |
Tomi Valkeinen | abb6527 | 2011-01-20 14:44:20 -0800 | [diff] [blame] | 713 | int idx = memblock_search(&memblock.memory, base); |
Tejun Heo | 0ce6929 | 2011-07-28 10:57:00 +0200 | [diff] [blame] | 714 | phys_addr_t end = base + memblock_cap_size(base, &size); |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 715 | |
| 716 | if (idx == -1) |
| 717 | return 0; |
Tomi Valkeinen | abb6527 | 2011-01-20 14:44:20 -0800 | [diff] [blame] | 718 | return memblock.memory.regions[idx].base <= base && |
| 719 | (memblock.memory.regions[idx].base + |
Tejun Heo | 0ce6929 | 2011-07-28 10:57:00 +0200 | [diff] [blame] | 720 | memblock.memory.regions[idx].size) >= end; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 721 | } |
| 722 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 723 | int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 724 | { |
Tejun Heo | 0ce6929 | 2011-07-28 10:57:00 +0200 | [diff] [blame] | 725 | memblock_cap_size(base, &size); |
Benjamin Herrenschmidt | f1c2c19 | 2010-08-04 14:17:17 +1000 | [diff] [blame] | 726 | return memblock_overlaps_region(&memblock.reserved, base, size) >= 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 727 | } |
| 728 | |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 729 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 730 | void __init_memblock memblock_set_current_limit(phys_addr_t limit) |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 731 | { |
| 732 | memblock.current_limit = limit; |
| 733 | } |
| 734 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 735 | static void __init_memblock memblock_dump(struct memblock_type *region, char *name) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 736 | { |
| 737 | unsigned long long base, size; |
| 738 | int i; |
| 739 | |
| 740 | pr_info(" %s.cnt = 0x%lx\n", name, region->cnt); |
| 741 | |
| 742 | for (i = 0; i < region->cnt; i++) { |
| 743 | base = region->regions[i].base; |
| 744 | size = region->regions[i].size; |
| 745 | |
Yinghai Lu | ea9e437 | 2010-07-28 15:13:22 +1000 | [diff] [blame] | 746 | pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes\n", |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 747 | name, i, base, base + size - 1, size); |
| 748 | } |
| 749 | } |
| 750 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 751 | void __init_memblock memblock_dump_all(void) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 752 | { |
| 753 | if (!memblock_debug) |
| 754 | return; |
| 755 | |
| 756 | pr_info("MEMBLOCK configuration:\n"); |
| 757 | pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size); |
| 758 | |
| 759 | memblock_dump(&memblock.memory, "memory"); |
| 760 | memblock_dump(&memblock.reserved, "reserved"); |
| 761 | } |
| 762 | |
| 763 | void __init memblock_analyze(void) |
| 764 | { |
| 765 | int i; |
| 766 | |
| 767 | /* Check marker in the unused last array entry */ |
| 768 | WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base |
| 769 | != (phys_addr_t)RED_INACTIVE); |
| 770 | WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base |
| 771 | != (phys_addr_t)RED_INACTIVE); |
| 772 | |
| 773 | memblock.memory_size = 0; |
| 774 | |
| 775 | for (i = 0; i < memblock.memory.cnt; i++) |
| 776 | memblock.memory_size += memblock.memory.regions[i].size; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 777 | |
| 778 | /* We allow resizing from there */ |
| 779 | memblock_can_resize = 1; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 780 | } |
| 781 | |
Benjamin Herrenschmidt | 7590abe | 2010-07-06 15:39:10 -0700 | [diff] [blame] | 782 | void __init memblock_init(void) |
| 783 | { |
Jeremy Fitzhardinge | 236260b | 2010-10-06 15:52:29 -0700 | [diff] [blame] | 784 | static int init_done __initdata = 0; |
| 785 | |
| 786 | if (init_done) |
| 787 | return; |
| 788 | init_done = 1; |
| 789 | |
Benjamin Herrenschmidt | 7590abe | 2010-07-06 15:39:10 -0700 | [diff] [blame] | 790 | /* Hookup the initial arrays */ |
| 791 | memblock.memory.regions = memblock_memory_init_regions; |
| 792 | memblock.memory.max = INIT_MEMBLOCK_REGIONS; |
| 793 | memblock.reserved.regions = memblock_reserved_init_regions; |
| 794 | memblock.reserved.max = INIT_MEMBLOCK_REGIONS; |
| 795 | |
| 796 | /* Write a marker in the unused last array entry */ |
| 797 | memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE; |
| 798 | memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE; |
| 799 | |
| 800 | /* Create a dummy zero size MEMBLOCK which will get coalesced away later. |
| 801 | * This simplifies the memblock_add() code below... |
| 802 | */ |
| 803 | memblock.memory.regions[0].base = 0; |
| 804 | memblock.memory.regions[0].size = 0; |
| 805 | memblock.memory.cnt = 1; |
| 806 | |
| 807 | /* Ditto. */ |
| 808 | memblock.reserved.regions[0].base = 0; |
| 809 | memblock.reserved.regions[0].size = 0; |
| 810 | memblock.reserved.cnt = 1; |
| 811 | |
| 812 | memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE; |
| 813 | } |
| 814 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 815 | static int __init early_memblock(char *p) |
| 816 | { |
| 817 | if (p && strstr(p, "debug")) |
| 818 | memblock_debug = 1; |
| 819 | return 0; |
| 820 | } |
| 821 | early_param("memblock", early_memblock); |
| 822 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 823 | #if defined(CONFIG_DEBUG_FS) && !defined(ARCH_DISCARD_MEMBLOCK) |
Benjamin Herrenschmidt | 6d03b88 | 2010-07-06 15:39:19 -0700 | [diff] [blame] | 824 | |
| 825 | static int memblock_debug_show(struct seq_file *m, void *private) |
| 826 | { |
| 827 | struct memblock_type *type = m->private; |
| 828 | struct memblock_region *reg; |
| 829 | int i; |
| 830 | |
| 831 | for (i = 0; i < type->cnt; i++) { |
| 832 | reg = &type->regions[i]; |
| 833 | seq_printf(m, "%4d: ", i); |
| 834 | if (sizeof(phys_addr_t) == 4) |
| 835 | seq_printf(m, "0x%08lx..0x%08lx\n", |
| 836 | (unsigned long)reg->base, |
| 837 | (unsigned long)(reg->base + reg->size - 1)); |
| 838 | else |
| 839 | seq_printf(m, "0x%016llx..0x%016llx\n", |
| 840 | (unsigned long long)reg->base, |
| 841 | (unsigned long long)(reg->base + reg->size - 1)); |
| 842 | |
| 843 | } |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | static int memblock_debug_open(struct inode *inode, struct file *file) |
| 848 | { |
| 849 | return single_open(file, memblock_debug_show, inode->i_private); |
| 850 | } |
| 851 | |
| 852 | static const struct file_operations memblock_debug_fops = { |
| 853 | .open = memblock_debug_open, |
| 854 | .read = seq_read, |
| 855 | .llseek = seq_lseek, |
| 856 | .release = single_release, |
| 857 | }; |
| 858 | |
| 859 | static int __init memblock_init_debugfs(void) |
| 860 | { |
| 861 | struct dentry *root = debugfs_create_dir("memblock", NULL); |
| 862 | if (!root) |
| 863 | return -ENXIO; |
| 864 | debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops); |
| 865 | debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops); |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | __initcall(memblock_init_debugfs); |
| 870 | |
| 871 | #endif /* CONFIG_DEBUG_FS */ |