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 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 41 | /* |
| 42 | * Address comparison utilities |
| 43 | */ |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 44 | 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] | 45 | phys_addr_t base2, phys_addr_t size2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 46 | { |
| 47 | return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); |
| 48 | } |
| 49 | |
H Hartley Sweeten | 2d7d3eb | 2011-10-31 17:09:15 -0700 | [diff] [blame] | 50 | static long __init_memblock memblock_overlaps_region(struct memblock_type *type, |
| 51 | phys_addr_t base, phys_addr_t size) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 52 | { |
| 53 | unsigned long i; |
| 54 | |
| 55 | for (i = 0; i < type->cnt; i++) { |
| 56 | phys_addr_t rgnbase = type->regions[i].base; |
| 57 | phys_addr_t rgnsize = type->regions[i].size; |
| 58 | if (memblock_addrs_overlap(base, size, rgnbase, rgnsize)) |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | return (i < type->cnt) ? i : -1; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Find, allocate, deallocate or reserve unreserved regions. All allocations |
| 67 | * are top-down. |
| 68 | */ |
| 69 | |
Yinghai Lu | cd79481 | 2010-10-11 12:34:09 -0700 | [diff] [blame] | 70 | 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] | 71 | phys_addr_t size, phys_addr_t align) |
| 72 | { |
| 73 | phys_addr_t base, res_base; |
| 74 | long j; |
| 75 | |
Yinghai Lu | f1af98c | 2010-10-04 14:57:39 -0700 | [diff] [blame] | 76 | /* In case, huge size is requested */ |
| 77 | if (end < size) |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 78 | return 0; |
Yinghai Lu | f1af98c | 2010-10-04 14:57:39 -0700 | [diff] [blame] | 79 | |
Tejun Heo | 348968e | 2011-07-12 09:58:08 +0200 | [diff] [blame] | 80 | base = round_down(end - size, align); |
Yinghai Lu | f1af98c | 2010-10-04 14:57:39 -0700 | [diff] [blame] | 81 | |
Benjamin Herrenschmidt | 25818f0 | 2010-07-28 15:25:10 +1000 | [diff] [blame] | 82 | /* Prevent allocations returning 0 as it's also used to |
| 83 | * indicate an allocation failure |
| 84 | */ |
| 85 | if (start == 0) |
| 86 | start = PAGE_SIZE; |
| 87 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 88 | while (start <= base) { |
| 89 | j = memblock_overlaps_region(&memblock.reserved, base, size); |
| 90 | if (j < 0) |
| 91 | return base; |
| 92 | res_base = memblock.reserved.regions[j].base; |
| 93 | if (res_base < size) |
| 94 | break; |
Tejun Heo | 348968e | 2011-07-12 09:58:08 +0200 | [diff] [blame] | 95 | base = round_down(res_base - size, align); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 96 | } |
| 97 | |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 98 | return 0; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 99 | } |
| 100 | |
Tejun Heo | fc769a8 | 2011-07-12 09:58:10 +0200 | [diff] [blame] | 101 | /* |
| 102 | * Find a free area with specified alignment in a specific range. |
| 103 | */ |
| 104 | phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, phys_addr_t end, |
| 105 | phys_addr_t size, phys_addr_t align) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 106 | { |
| 107 | long i; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 108 | |
| 109 | BUG_ON(0 == size); |
| 110 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 111 | /* Pump up max_addr */ |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 112 | if (end == MEMBLOCK_ALLOC_ACCESSIBLE) |
| 113 | end = memblock.current_limit; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 114 | |
| 115 | /* We do a top-down search, this tends to limit memory |
| 116 | * fragmentation by keeping early boot allocs near the |
| 117 | * top of memory |
| 118 | */ |
| 119 | for (i = memblock.memory.cnt - 1; i >= 0; i--) { |
| 120 | phys_addr_t memblockbase = memblock.memory.regions[i].base; |
| 121 | phys_addr_t memblocksize = memblock.memory.regions[i].size; |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 122 | phys_addr_t bottom, top, found; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 123 | |
| 124 | if (memblocksize < size) |
| 125 | continue; |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 126 | if ((memblockbase + memblocksize) <= start) |
| 127 | break; |
| 128 | bottom = max(memblockbase, start); |
| 129 | top = min(memblockbase + memblocksize, end); |
| 130 | if (bottom >= top) |
| 131 | continue; |
| 132 | found = memblock_find_region(bottom, top, size, align); |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 133 | if (found) |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 134 | return found; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 135 | } |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 136 | return 0; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 137 | } |
| 138 | |
Yinghai Lu | 5303b68 | 2010-07-28 15:38:40 +1000 | [diff] [blame] | 139 | /* |
Yinghai Lu | 7950c40 | 2010-08-25 13:39:14 -0700 | [diff] [blame] | 140 | * Free memblock.reserved.regions |
| 141 | */ |
| 142 | int __init_memblock memblock_free_reserved_regions(void) |
| 143 | { |
| 144 | if (memblock.reserved.regions == memblock_reserved_init_regions) |
| 145 | return 0; |
| 146 | |
| 147 | return memblock_free(__pa(memblock.reserved.regions), |
| 148 | sizeof(struct memblock_region) * memblock.reserved.max); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Reserve memblock.reserved.regions |
| 153 | */ |
| 154 | int __init_memblock memblock_reserve_reserved_regions(void) |
| 155 | { |
| 156 | if (memblock.reserved.regions == memblock_reserved_init_regions) |
| 157 | return 0; |
| 158 | |
| 159 | return memblock_reserve(__pa(memblock.reserved.regions), |
| 160 | sizeof(struct memblock_region) * memblock.reserved.max); |
| 161 | } |
| 162 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 163 | 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] | 164 | { |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 165 | memmove(&type->regions[r], &type->regions[r + 1], |
| 166 | (type->cnt - (r + 1)) * sizeof(type->regions[r])); |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 167 | type->cnt--; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 168 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 169 | /* Special case for empty arrays */ |
| 170 | if (type->cnt == 0) { |
| 171 | type->cnt = 1; |
| 172 | type->regions[0].base = 0; |
| 173 | type->regions[0].size = 0; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 174 | memblock_set_region_node(&type->regions[0], MAX_NUMNODES); |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 175 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 176 | } |
| 177 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 178 | /* Defined below but needed now */ |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame^] | 179 | static int memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 180 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 181 | static int __init_memblock memblock_double_array(struct memblock_type *type) |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 182 | { |
| 183 | struct memblock_region *new_array, *old_array; |
| 184 | phys_addr_t old_size, new_size, addr; |
| 185 | int use_slab = slab_is_available(); |
| 186 | |
| 187 | /* We don't allow resizing until we know about the reserved regions |
| 188 | * of memory that aren't suitable for allocation |
| 189 | */ |
| 190 | if (!memblock_can_resize) |
| 191 | return -1; |
| 192 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 193 | /* Calculate new doubled size */ |
| 194 | old_size = type->max * sizeof(struct memblock_region); |
| 195 | new_size = old_size << 1; |
| 196 | |
| 197 | /* Try to find some space for it. |
| 198 | * |
| 199 | * WARNING: We assume that either slab_is_available() and we use it or |
| 200 | * we use MEMBLOCK for allocations. That means that this is unsafe to use |
| 201 | * when bootmem is currently active (unless bootmem itself is implemented |
| 202 | * on top of MEMBLOCK which isn't the case yet) |
| 203 | * |
| 204 | * This should however not be an issue for now, as we currently only |
| 205 | * call into MEMBLOCK while it's still active, or much later when slab is |
| 206 | * active for memory hotplug operations |
| 207 | */ |
| 208 | if (use_slab) { |
| 209 | new_array = kmalloc(new_size, GFP_KERNEL); |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 210 | addr = new_array ? __pa(new_array) : 0; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 211 | } else |
Tejun Heo | fc769a8 | 2011-07-12 09:58:10 +0200 | [diff] [blame] | 212 | addr = memblock_find_in_range(0, MEMBLOCK_ALLOC_ACCESSIBLE, new_size, sizeof(phys_addr_t)); |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 213 | if (!addr) { |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 214 | pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", |
| 215 | memblock_type_name(type), type->max, type->max * 2); |
| 216 | return -1; |
| 217 | } |
| 218 | new_array = __va(addr); |
| 219 | |
Yinghai Lu | ea9e437 | 2010-07-28 15:13:22 +1000 | [diff] [blame] | 220 | memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]", |
| 221 | memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1); |
| 222 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 223 | /* Found space, we now need to move the array over before |
| 224 | * we add the reserved region since it may be our reserved |
| 225 | * array itself that is full. |
| 226 | */ |
| 227 | memcpy(new_array, type->regions, old_size); |
| 228 | memset(new_array + type->max, 0, old_size); |
| 229 | old_array = type->regions; |
| 230 | type->regions = new_array; |
| 231 | type->max <<= 1; |
| 232 | |
| 233 | /* If we use SLAB that's it, we are done */ |
| 234 | if (use_slab) |
| 235 | return 0; |
| 236 | |
| 237 | /* Add the new reserved region now. Should not fail ! */ |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 238 | BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size)); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 239 | |
| 240 | /* If the array wasn't our static init one, then free it. We only do |
| 241 | * that before SLAB is available as later on, we don't know whether |
| 242 | * to use kfree or free_bootmem_pages(). Shouldn't be a big deal |
| 243 | * anyways |
| 244 | */ |
| 245 | if (old_array != memblock_memory_init_regions && |
| 246 | old_array != memblock_reserved_init_regions) |
| 247 | memblock_free(__pa(old_array), old_size); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 252 | /** |
| 253 | * memblock_merge_regions - merge neighboring compatible regions |
| 254 | * @type: memblock type to scan |
| 255 | * |
| 256 | * Scan @type and merge neighboring compatible regions. |
| 257 | */ |
| 258 | static void __init_memblock memblock_merge_regions(struct memblock_type *type) |
| 259 | { |
| 260 | int i = 0; |
| 261 | |
| 262 | /* cnt never goes below 1 */ |
| 263 | while (i < type->cnt - 1) { |
| 264 | struct memblock_region *this = &type->regions[i]; |
| 265 | struct memblock_region *next = &type->regions[i + 1]; |
| 266 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 267 | if (this->base + this->size != next->base || |
| 268 | memblock_get_region_node(this) != |
| 269 | memblock_get_region_node(next)) { |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 270 | BUG_ON(this->base + this->size > next->base); |
| 271 | i++; |
| 272 | continue; |
| 273 | } |
| 274 | |
| 275 | this->size += next->size; |
| 276 | memmove(next, next + 1, (type->cnt - (i + 1)) * sizeof(*next)); |
| 277 | type->cnt--; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * memblock_insert_region - insert new memblock region |
| 283 | * @type: memblock type to insert into |
| 284 | * @idx: index for the insertion point |
| 285 | * @base: base address of the new region |
| 286 | * @size: size of the new region |
| 287 | * |
| 288 | * Insert new memblock region [@base,@base+@size) into @type at @idx. |
| 289 | * @type must already have extra room to accomodate the new region. |
| 290 | */ |
| 291 | static void __init_memblock memblock_insert_region(struct memblock_type *type, |
| 292 | int idx, phys_addr_t base, |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 293 | phys_addr_t size, int nid) |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 294 | { |
| 295 | struct memblock_region *rgn = &type->regions[idx]; |
| 296 | |
| 297 | BUG_ON(type->cnt >= type->max); |
| 298 | memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); |
| 299 | rgn->base = base; |
| 300 | rgn->size = size; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 301 | memblock_set_region_node(rgn, nid); |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 302 | type->cnt++; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * memblock_add_region - add new memblock region |
| 307 | * @type: memblock type to add new region into |
| 308 | * @base: base address of the new region |
| 309 | * @size: size of the new region |
| 310 | * |
| 311 | * Add new memblock region [@base,@base+@size) into @type. The new region |
| 312 | * is allowed to overlap with existing ones - overlaps don't affect already |
| 313 | * existing regions. @type is guaranteed to be minimal (all neighbouring |
| 314 | * compatible regions are merged) after the addition. |
| 315 | * |
| 316 | * RETURNS: |
| 317 | * 0 on success, -errno on failure. |
| 318 | */ |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame^] | 319 | static int __init_memblock memblock_add_region(struct memblock_type *type, |
| 320 | phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 321 | { |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 322 | bool insert = false; |
| 323 | phys_addr_t obase = base, end = base + size; |
| 324 | int i, nr_new; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 325 | |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 326 | /* special case for empty array */ |
| 327 | if (type->regions[0].size == 0) { |
| 328 | WARN_ON(type->cnt != 1); |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 329 | type->regions[0].base = base; |
| 330 | type->regions[0].size = size; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 331 | memblock_set_region_node(&type->regions[0], MAX_NUMNODES); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 332 | return 0; |
| 333 | } |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 334 | repeat: |
| 335 | /* |
| 336 | * The following is executed twice. Once with %false @insert and |
| 337 | * then with %true. The first counts the number of regions needed |
| 338 | * to accomodate the new area. The second actually inserts them. |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 339 | */ |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 340 | base = obase; |
| 341 | nr_new = 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 342 | |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 343 | for (i = 0; i < type->cnt; i++) { |
| 344 | struct memblock_region *rgn = &type->regions[i]; |
| 345 | phys_addr_t rbase = rgn->base; |
| 346 | phys_addr_t rend = rbase + rgn->size; |
| 347 | |
| 348 | if (rbase >= end) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 349 | break; |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 350 | if (rend <= base) |
| 351 | continue; |
| 352 | /* |
| 353 | * @rgn overlaps. If it separates the lower part of new |
| 354 | * area, insert that portion. |
| 355 | */ |
| 356 | if (rbase > base) { |
| 357 | nr_new++; |
| 358 | if (insert) |
| 359 | memblock_insert_region(type, i++, base, |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 360 | rbase - base, MAX_NUMNODES); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 361 | } |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 362 | /* area below @rend is dealt with, forget about it */ |
| 363 | base = min(rend, end); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 364 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 365 | |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 366 | /* insert the remaining portion */ |
| 367 | if (base < end) { |
| 368 | nr_new++; |
| 369 | if (insert) |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 370 | memblock_insert_region(type, i, base, end - base, |
| 371 | MAX_NUMNODES); |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* |
| 375 | * If this was the first round, resize array and repeat for actual |
| 376 | * insertions; otherwise, merge and return. |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 377 | */ |
Tejun Heo | 784656f | 2011-07-12 11:15:55 +0200 | [diff] [blame] | 378 | if (!insert) { |
| 379 | while (type->cnt + nr_new > type->max) |
| 380 | if (memblock_double_array(type) < 0) |
| 381 | return -ENOMEM; |
| 382 | insert = true; |
| 383 | goto repeat; |
| 384 | } else { |
| 385 | memblock_merge_regions(type); |
| 386 | return 0; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 387 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 388 | } |
| 389 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame^] | 390 | int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 391 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 392 | return memblock_add_region(&memblock.memory, base, size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 393 | } |
| 394 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame^] | 395 | static int __init_memblock __memblock_remove(struct memblock_type *type, |
| 396 | phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 397 | { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 398 | phys_addr_t end = base + size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 399 | int i; |
| 400 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 401 | /* Walk through the array for collisions */ |
| 402 | for (i = 0; i < type->cnt; i++) { |
| 403 | struct memblock_region *rgn = &type->regions[i]; |
| 404 | phys_addr_t rend = rgn->base + rgn->size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 405 | |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 406 | /* Nothing more to do, exit */ |
| 407 | if (rgn->base > end || rgn->size == 0) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 408 | break; |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 409 | |
| 410 | /* If we fully enclose the block, drop it */ |
| 411 | if (base <= rgn->base && end >= rend) { |
| 412 | memblock_remove_region(type, i--); |
| 413 | continue; |
| 414 | } |
| 415 | |
| 416 | /* If we are fully enclosed within a block |
| 417 | * then we need to split it and we are done |
| 418 | */ |
| 419 | if (base > rgn->base && end < rend) { |
| 420 | rgn->size = base - rgn->base; |
| 421 | if (!memblock_add_region(type, end, rend - end)) |
| 422 | return 0; |
| 423 | /* Failure to split is bad, we at least |
| 424 | * restore the block before erroring |
| 425 | */ |
| 426 | rgn->size = rend - rgn->base; |
| 427 | WARN_ON(1); |
| 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | /* Check if we need to trim the bottom of a block */ |
| 432 | if (rgn->base < end && rend > end) { |
| 433 | rgn->size -= end - rgn->base; |
| 434 | rgn->base = end; |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | /* And check if we need to trim the top of a block */ |
| 439 | if (base < rend) |
| 440 | rgn->size -= rend - base; |
| 441 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 442 | } |
Benjamin Herrenschmidt | 8f7a660 | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 443 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 444 | } |
| 445 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame^] | 446 | int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 447 | { |
| 448 | return __memblock_remove(&memblock.memory, base, size); |
| 449 | } |
| 450 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame^] | 451 | int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 452 | { |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 453 | memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n", |
H. Peter Anvin | a150439 | 2011-07-14 11:57:10 -0700 | [diff] [blame] | 454 | (unsigned long long)base, |
| 455 | (unsigned long long)base + size, |
| 456 | (void *)_RET_IP_); |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 457 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 458 | return __memblock_remove(&memblock.reserved, base, size); |
| 459 | } |
| 460 | |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame^] | 461 | int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 462 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 463 | struct memblock_type *_rgn = &memblock.reserved; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 464 | |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 465 | memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n", |
H. Peter Anvin | a150439 | 2011-07-14 11:57:10 -0700 | [diff] [blame] | 466 | (unsigned long long)base, |
| 467 | (unsigned long long)base + size, |
| 468 | (void *)_RET_IP_); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 469 | BUG_ON(0 == size); |
| 470 | |
| 471 | return memblock_add_region(_rgn, base, size); |
| 472 | } |
| 473 | |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 474 | /** |
| 475 | * __next_free_mem_range - next function for for_each_free_mem_range() |
| 476 | * @idx: pointer to u64 loop variable |
| 477 | * @nid: nid: node selector, %MAX_NUMNODES for all nodes |
| 478 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 479 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 480 | * @p_nid: ptr to int for nid of the range, can be %NULL |
| 481 | * |
| 482 | * Find the first free area from *@idx which matches @nid, fill the out |
| 483 | * parameters, and update *@idx for the next iteration. The lower 32bit of |
| 484 | * *@idx contains index into memory region and the upper 32bit indexes the |
| 485 | * areas before each reserved region. For example, if reserved regions |
| 486 | * look like the following, |
| 487 | * |
| 488 | * 0:[0-16), 1:[32-48), 2:[128-130) |
| 489 | * |
| 490 | * The upper 32bit indexes the following regions. |
| 491 | * |
| 492 | * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) |
| 493 | * |
| 494 | * As both region arrays are sorted, the function advances the two indices |
| 495 | * in lockstep and returns each intersection. |
| 496 | */ |
| 497 | void __init_memblock __next_free_mem_range(u64 *idx, int nid, |
| 498 | phys_addr_t *out_start, |
| 499 | phys_addr_t *out_end, int *out_nid) |
| 500 | { |
| 501 | struct memblock_type *mem = &memblock.memory; |
| 502 | struct memblock_type *rsv = &memblock.reserved; |
| 503 | int mi = *idx & 0xffffffff; |
| 504 | int ri = *idx >> 32; |
| 505 | |
| 506 | for ( ; mi < mem->cnt; mi++) { |
| 507 | struct memblock_region *m = &mem->regions[mi]; |
| 508 | phys_addr_t m_start = m->base; |
| 509 | phys_addr_t m_end = m->base + m->size; |
| 510 | |
| 511 | /* only memory regions are associated with nodes, check it */ |
| 512 | if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m)) |
| 513 | continue; |
| 514 | |
| 515 | /* scan areas before each reservation for intersection */ |
| 516 | for ( ; ri < rsv->cnt + 1; ri++) { |
| 517 | struct memblock_region *r = &rsv->regions[ri]; |
| 518 | phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0; |
| 519 | phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX; |
| 520 | |
| 521 | /* if ri advanced past mi, break out to advance mi */ |
| 522 | if (r_start >= m_end) |
| 523 | break; |
| 524 | /* if the two regions intersect, we're done */ |
| 525 | if (m_start < r_end) { |
| 526 | if (out_start) |
| 527 | *out_start = max(m_start, r_start); |
| 528 | if (out_end) |
| 529 | *out_end = min(m_end, r_end); |
| 530 | if (out_nid) |
| 531 | *out_nid = memblock_get_region_node(m); |
| 532 | /* |
| 533 | * The region which ends first is advanced |
| 534 | * for the next iteration. |
| 535 | */ |
| 536 | if (m_end <= r_end) |
| 537 | mi++; |
| 538 | else |
| 539 | ri++; |
| 540 | *idx = (u32)mi | (u64)ri << 32; |
| 541 | return; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* signal end of iteration */ |
| 547 | *idx = ULLONG_MAX; |
| 548 | } |
| 549 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 550 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 551 | /* |
| 552 | * Common iterator interface used to define for_each_mem_range(). |
| 553 | */ |
| 554 | void __init_memblock __next_mem_pfn_range(int *idx, int nid, |
| 555 | unsigned long *out_start_pfn, |
| 556 | unsigned long *out_end_pfn, int *out_nid) |
| 557 | { |
| 558 | struct memblock_type *type = &memblock.memory; |
| 559 | struct memblock_region *r; |
| 560 | |
| 561 | while (++*idx < type->cnt) { |
| 562 | r = &type->regions[*idx]; |
| 563 | |
| 564 | if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) |
| 565 | continue; |
| 566 | if (nid == MAX_NUMNODES || nid == r->nid) |
| 567 | break; |
| 568 | } |
| 569 | if (*idx >= type->cnt) { |
| 570 | *idx = -1; |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | if (out_start_pfn) |
| 575 | *out_start_pfn = PFN_UP(r->base); |
| 576 | if (out_end_pfn) |
| 577 | *out_end_pfn = PFN_DOWN(r->base + r->size); |
| 578 | if (out_nid) |
| 579 | *out_nid = r->nid; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * memblock_set_node - set node ID on memblock regions |
| 584 | * @base: base of area to set node ID for |
| 585 | * @size: size of area to set node ID for |
| 586 | * @nid: node ID to set |
| 587 | * |
| 588 | * Set the nid of memblock memory regions in [@base,@base+@size) to @nid. |
| 589 | * Regions which cross the area boundaries are split as necessary. |
| 590 | * |
| 591 | * RETURNS: |
| 592 | * 0 on success, -errno on failure. |
| 593 | */ |
| 594 | int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, |
| 595 | int nid) |
| 596 | { |
| 597 | struct memblock_type *type = &memblock.memory; |
| 598 | phys_addr_t end = base + size; |
| 599 | int i; |
| 600 | |
| 601 | /* we'll create at most two more regions */ |
| 602 | while (type->cnt + 2 > type->max) |
| 603 | if (memblock_double_array(type) < 0) |
| 604 | return -ENOMEM; |
| 605 | |
| 606 | for (i = 0; i < type->cnt; i++) { |
| 607 | struct memblock_region *rgn = &type->regions[i]; |
| 608 | phys_addr_t rbase = rgn->base; |
| 609 | phys_addr_t rend = rbase + rgn->size; |
| 610 | |
| 611 | if (rbase >= end) |
| 612 | break; |
| 613 | if (rend <= base) |
| 614 | continue; |
| 615 | |
| 616 | if (rbase < base) { |
| 617 | /* |
| 618 | * @rgn intersects from below. Split and continue |
| 619 | * to process the next region - the new top half. |
| 620 | */ |
| 621 | rgn->base = base; |
| 622 | rgn->size = rend - rgn->base; |
| 623 | memblock_insert_region(type, i, rbase, base - rbase, |
| 624 | rgn->nid); |
| 625 | } else if (rend > end) { |
| 626 | /* |
| 627 | * @rgn intersects from above. Split and redo the |
| 628 | * current region - the new bottom half. |
| 629 | */ |
| 630 | rgn->base = end; |
| 631 | rgn->size = rend - rgn->base; |
| 632 | memblock_insert_region(type, i--, rbase, end - rbase, |
| 633 | rgn->nid); |
| 634 | } else { |
| 635 | /* @rgn is fully contained, set ->nid */ |
| 636 | rgn->nid = nid; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | memblock_merge_regions(type); |
| 641 | return 0; |
| 642 | } |
| 643 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
| 644 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 645 | 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] | 646 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 647 | phys_addr_t found; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 648 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 649 | /* We align the size to limit fragmentation. Without this, a lot of |
| 650 | * small allocs quickly eat up the whole reserve array on sparc |
| 651 | */ |
Tejun Heo | 348968e | 2011-07-12 09:58:08 +0200 | [diff] [blame] | 652 | size = round_up(size, align); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 653 | |
Tejun Heo | fc769a8 | 2011-07-12 09:58:10 +0200 | [diff] [blame] | 654 | found = memblock_find_in_range(0, max_addr, size, align); |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 655 | if (found && !memblock_add_region(&memblock.reserved, found, size)) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 656 | return found; |
| 657 | |
| 658 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 659 | } |
| 660 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 661 | 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] | 662 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 663 | phys_addr_t alloc; |
| 664 | |
| 665 | alloc = __memblock_alloc_base(size, align, max_addr); |
| 666 | |
| 667 | if (alloc == 0) |
| 668 | panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n", |
| 669 | (unsigned long long) size, (unsigned long long) max_addr); |
| 670 | |
| 671 | return alloc; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 672 | } |
| 673 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 674 | 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] | 675 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 676 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 677 | } |
| 678 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 679 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 680 | /* |
Tejun Heo | 34e1845 | 2011-07-12 10:46:33 +0200 | [diff] [blame] | 681 | * Additional node-local top-down allocators. |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame] | 682 | * |
| 683 | * WARNING: Only available after early_node_map[] has been populated, |
| 684 | * on some architectures, that is after all the calls to add_active_range() |
| 685 | * have been done to populate it. |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 686 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 687 | |
Tejun Heo | 34e1845 | 2011-07-12 10:46:33 +0200 | [diff] [blame] | 688 | static phys_addr_t __init memblock_nid_range_rev(phys_addr_t start, |
| 689 | phys_addr_t end, int *nid) |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 690 | { |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame] | 691 | #ifdef CONFIG_ARCH_POPULATES_NODE_MAP |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame] | 692 | unsigned long start_pfn, end_pfn; |
| 693 | int i; |
| 694 | |
Tejun Heo | b2fea98 | 2011-07-12 10:46:31 +0200 | [diff] [blame] | 695 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, nid) |
Tejun Heo | 34e1845 | 2011-07-12 10:46:33 +0200 | [diff] [blame] | 696 | if (end > PFN_PHYS(start_pfn) && end <= PFN_PHYS(end_pfn)) |
| 697 | return max(start, PFN_PHYS(start_pfn)); |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame] | 698 | #endif |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 699 | *nid = 0; |
Tejun Heo | 34e1845 | 2011-07-12 10:46:33 +0200 | [diff] [blame] | 700 | return start; |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Tejun Heo | e649804 | 2011-07-12 10:46:34 +0200 | [diff] [blame] | 703 | phys_addr_t __init memblock_find_in_range_node(phys_addr_t start, |
| 704 | phys_addr_t end, |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 705 | phys_addr_t size, |
| 706 | phys_addr_t align, int nid) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 707 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 708 | struct memblock_type *mem = &memblock.memory; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 709 | int i; |
| 710 | |
| 711 | BUG_ON(0 == size); |
| 712 | |
Tejun Heo | e649804 | 2011-07-12 10:46:34 +0200 | [diff] [blame] | 713 | /* Pump up max_addr */ |
| 714 | if (end == MEMBLOCK_ALLOC_ACCESSIBLE) |
| 715 | end = memblock.current_limit; |
| 716 | |
| 717 | for (i = mem->cnt - 1; i >= 0; i--) { |
| 718 | struct memblock_region *r = &mem->regions[i]; |
| 719 | phys_addr_t base = max(start, r->base); |
| 720 | phys_addr_t top = min(end, r->base + r->size); |
| 721 | |
| 722 | while (base < top) { |
| 723 | phys_addr_t tbase, ret; |
| 724 | int tnid; |
| 725 | |
| 726 | tbase = memblock_nid_range_rev(base, top, &tnid); |
| 727 | if (nid == MAX_NUMNODES || tnid == nid) { |
| 728 | ret = memblock_find_region(tbase, top, size, align); |
| 729 | if (ret) |
| 730 | return ret; |
| 731 | } |
| 732 | top = tbase; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid) |
| 740 | { |
| 741 | phys_addr_t found; |
| 742 | |
| 743 | /* |
| 744 | * We align the size to limit fragmentation. Without this, a lot of |
Benjamin Herrenschmidt | 7f219c7 | 2010-07-12 14:24:57 +1000 | [diff] [blame] | 745 | * small allocs quickly eat up the whole reserve array on sparc |
| 746 | */ |
Tejun Heo | 348968e | 2011-07-12 09:58:08 +0200 | [diff] [blame] | 747 | size = round_up(size, align); |
Benjamin Herrenschmidt | 7f219c7 | 2010-07-12 14:24:57 +1000 | [diff] [blame] | 748 | |
Tejun Heo | e649804 | 2011-07-12 10:46:34 +0200 | [diff] [blame] | 749 | found = memblock_find_in_range_node(0, MEMBLOCK_ALLOC_ACCESSIBLE, |
| 750 | size, align, nid); |
| 751 | if (found && !memblock_add_region(&memblock.reserved, found, size)) |
| 752 | return found; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 753 | |
Benjamin Herrenschmidt | 9d1e249 | 2010-07-06 15:39:17 -0700 | [diff] [blame] | 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) |
| 758 | { |
| 759 | phys_addr_t res = memblock_alloc_nid(size, align, nid); |
| 760 | |
| 761 | if (res) |
| 762 | return res; |
Tejun Heo | 15fb097 | 2011-07-12 09:58:07 +0200 | [diff] [blame] | 763 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 764 | } |
| 765 | |
Benjamin Herrenschmidt | 9d1e249 | 2010-07-06 15:39:17 -0700 | [diff] [blame] | 766 | |
| 767 | /* |
| 768 | * Remaining API functions |
| 769 | */ |
| 770 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 771 | /* You must call memblock_analyze() before this. */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 772 | phys_addr_t __init memblock_phys_mem_size(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 773 | { |
Benjamin Herrenschmidt | 4734b59 | 2010-07-28 14:31:29 +1000 | [diff] [blame] | 774 | return memblock.memory_size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 775 | } |
| 776 | |
Sam Ravnborg | 0a93ebe | 2011-10-31 17:08:16 -0700 | [diff] [blame] | 777 | /* lowest address */ |
| 778 | phys_addr_t __init_memblock memblock_start_of_DRAM(void) |
| 779 | { |
| 780 | return memblock.memory.regions[0].base; |
| 781 | } |
| 782 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 783 | phys_addr_t __init_memblock memblock_end_of_DRAM(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 784 | { |
| 785 | int idx = memblock.memory.cnt - 1; |
| 786 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 787 | return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* You must call memblock_analyze() after this. */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 791 | void __init memblock_enforce_memory_limit(phys_addr_t memory_limit) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 792 | { |
| 793 | unsigned long i; |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 794 | phys_addr_t limit; |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 795 | struct memblock_region *p; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 796 | |
| 797 | if (!memory_limit) |
| 798 | return; |
| 799 | |
| 800 | /* Truncate the memblock regions to satisfy the memory limit. */ |
| 801 | limit = memory_limit; |
| 802 | for (i = 0; i < memblock.memory.cnt; i++) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 803 | if (limit > memblock.memory.regions[i].size) { |
| 804 | limit -= memblock.memory.regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 805 | continue; |
| 806 | } |
| 807 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 808 | memblock.memory.regions[i].size = limit; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 809 | memblock.memory.cnt = i + 1; |
| 810 | break; |
| 811 | } |
| 812 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 813 | memory_limit = memblock_end_of_DRAM(); |
| 814 | |
| 815 | /* And truncate any reserves above the limit also. */ |
| 816 | for (i = 0; i < memblock.reserved.cnt; i++) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 817 | p = &memblock.reserved.regions[i]; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 818 | |
| 819 | if (p->base > memory_limit) |
| 820 | p->size = 0; |
| 821 | else if ((p->base + p->size) > memory_limit) |
| 822 | p->size = memory_limit - p->base; |
| 823 | |
| 824 | if (p->size == 0) { |
| 825 | memblock_remove_region(&memblock.reserved, i); |
| 826 | i--; |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Yinghai Lu | cd79481 | 2010-10-11 12:34:09 -0700 | [diff] [blame] | 831 | 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] | 832 | { |
| 833 | unsigned int left = 0, right = type->cnt; |
| 834 | |
| 835 | do { |
| 836 | unsigned int mid = (right + left) / 2; |
| 837 | |
| 838 | if (addr < type->regions[mid].base) |
| 839 | right = mid; |
| 840 | else if (addr >= (type->regions[mid].base + |
| 841 | type->regions[mid].size)) |
| 842 | left = mid + 1; |
| 843 | else |
| 844 | return mid; |
| 845 | } while (left < right); |
| 846 | return -1; |
| 847 | } |
| 848 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 849 | int __init memblock_is_reserved(phys_addr_t addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 850 | { |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 851 | return memblock_search(&memblock.reserved, addr) != -1; |
| 852 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 853 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 854 | int __init_memblock memblock_is_memory(phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 855 | { |
| 856 | return memblock_search(&memblock.memory, addr) != -1; |
| 857 | } |
| 858 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 859 | 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] | 860 | { |
Tomi Valkeinen | abb6527 | 2011-01-20 14:44:20 -0800 | [diff] [blame] | 861 | int idx = memblock_search(&memblock.memory, base); |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 862 | |
| 863 | if (idx == -1) |
| 864 | return 0; |
Tomi Valkeinen | abb6527 | 2011-01-20 14:44:20 -0800 | [diff] [blame] | 865 | return memblock.memory.regions[idx].base <= base && |
| 866 | (memblock.memory.regions[idx].base + |
| 867 | memblock.memory.regions[idx].size) >= (base + size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 868 | } |
| 869 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 870 | 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] | 871 | { |
Benjamin Herrenschmidt | f1c2c19 | 2010-08-04 14:17:17 +1000 | [diff] [blame] | 872 | return memblock_overlaps_region(&memblock.reserved, base, size) >= 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 873 | } |
| 874 | |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 875 | |
Yinghai Lu | 3661ca6 | 2010-09-15 13:05:29 -0700 | [diff] [blame] | 876 | void __init_memblock memblock_set_current_limit(phys_addr_t limit) |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 877 | { |
| 878 | memblock.current_limit = limit; |
| 879 | } |
| 880 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 881 | static void __init_memblock memblock_dump(struct memblock_type *type, char *name) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 882 | { |
| 883 | unsigned long long base, size; |
| 884 | int i; |
| 885 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 886 | pr_info(" %s.cnt = 0x%lx\n", name, type->cnt); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 887 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 888 | for (i = 0; i < type->cnt; i++) { |
| 889 | struct memblock_region *rgn = &type->regions[i]; |
| 890 | char nid_buf[32] = ""; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 891 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 892 | base = rgn->base; |
| 893 | size = rgn->size; |
| 894 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 895 | if (memblock_get_region_node(rgn) != MAX_NUMNODES) |
| 896 | snprintf(nid_buf, sizeof(nid_buf), " on node %d", |
| 897 | memblock_get_region_node(rgn)); |
| 898 | #endif |
| 899 | pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n", |
| 900 | name, i, base, base + size - 1, size, nid_buf); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
Yinghai Lu | 10d0643 | 2010-07-28 15:43:02 +1000 | [diff] [blame] | 904 | void __init_memblock memblock_dump_all(void) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 905 | { |
| 906 | if (!memblock_debug) |
| 907 | return; |
| 908 | |
| 909 | pr_info("MEMBLOCK configuration:\n"); |
| 910 | pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size); |
| 911 | |
| 912 | memblock_dump(&memblock.memory, "memory"); |
| 913 | memblock_dump(&memblock.reserved, "reserved"); |
| 914 | } |
| 915 | |
| 916 | void __init memblock_analyze(void) |
| 917 | { |
| 918 | int i; |
| 919 | |
| 920 | /* Check marker in the unused last array entry */ |
| 921 | WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base |
Andrew Morton | c9d8c3d | 2011-07-25 17:12:18 -0700 | [diff] [blame] | 922 | != MEMBLOCK_INACTIVE); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 923 | WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base |
Andrew Morton | c9d8c3d | 2011-07-25 17:12:18 -0700 | [diff] [blame] | 924 | != MEMBLOCK_INACTIVE); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 925 | |
| 926 | memblock.memory_size = 0; |
| 927 | |
| 928 | for (i = 0; i < memblock.memory.cnt; i++) |
| 929 | memblock.memory_size += memblock.memory.regions[i].size; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 930 | |
| 931 | /* We allow resizing from there */ |
| 932 | memblock_can_resize = 1; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 933 | } |
| 934 | |
Benjamin Herrenschmidt | 7590abe | 2010-07-06 15:39:10 -0700 | [diff] [blame] | 935 | void __init memblock_init(void) |
| 936 | { |
Jeremy Fitzhardinge | 236260b | 2010-10-06 15:52:29 -0700 | [diff] [blame] | 937 | static int init_done __initdata = 0; |
| 938 | |
| 939 | if (init_done) |
| 940 | return; |
| 941 | init_done = 1; |
| 942 | |
Benjamin Herrenschmidt | 7590abe | 2010-07-06 15:39:10 -0700 | [diff] [blame] | 943 | /* Hookup the initial arrays */ |
| 944 | memblock.memory.regions = memblock_memory_init_regions; |
| 945 | memblock.memory.max = INIT_MEMBLOCK_REGIONS; |
| 946 | memblock.reserved.regions = memblock_reserved_init_regions; |
| 947 | memblock.reserved.max = INIT_MEMBLOCK_REGIONS; |
| 948 | |
| 949 | /* Write a marker in the unused last array entry */ |
Andrew Morton | c9d8c3d | 2011-07-25 17:12:18 -0700 | [diff] [blame] | 950 | memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = MEMBLOCK_INACTIVE; |
| 951 | memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = MEMBLOCK_INACTIVE; |
Benjamin Herrenschmidt | 7590abe | 2010-07-06 15:39:10 -0700 | [diff] [blame] | 952 | |
| 953 | /* Create a dummy zero size MEMBLOCK which will get coalesced away later. |
| 954 | * This simplifies the memblock_add() code below... |
| 955 | */ |
| 956 | memblock.memory.regions[0].base = 0; |
| 957 | memblock.memory.regions[0].size = 0; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 958 | memblock_set_region_node(&memblock.memory.regions[0], MAX_NUMNODES); |
Benjamin Herrenschmidt | 7590abe | 2010-07-06 15:39:10 -0700 | [diff] [blame] | 959 | memblock.memory.cnt = 1; |
| 960 | |
| 961 | /* Ditto. */ |
| 962 | memblock.reserved.regions[0].base = 0; |
| 963 | memblock.reserved.regions[0].size = 0; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 964 | memblock_set_region_node(&memblock.reserved.regions[0], MAX_NUMNODES); |
Benjamin Herrenschmidt | 7590abe | 2010-07-06 15:39:10 -0700 | [diff] [blame] | 965 | memblock.reserved.cnt = 1; |
| 966 | |
| 967 | memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE; |
| 968 | } |
| 969 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 970 | static int __init early_memblock(char *p) |
| 971 | { |
| 972 | if (p && strstr(p, "debug")) |
| 973 | memblock_debug = 1; |
| 974 | return 0; |
| 975 | } |
| 976 | early_param("memblock", early_memblock); |
| 977 | |
Tejun Heo | c378ddd | 2011-07-14 11:46:03 +0200 | [diff] [blame] | 978 | #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK) |
Benjamin Herrenschmidt | 6d03b88 | 2010-07-06 15:39:19 -0700 | [diff] [blame] | 979 | |
| 980 | static int memblock_debug_show(struct seq_file *m, void *private) |
| 981 | { |
| 982 | struct memblock_type *type = m->private; |
| 983 | struct memblock_region *reg; |
| 984 | int i; |
| 985 | |
| 986 | for (i = 0; i < type->cnt; i++) { |
| 987 | reg = &type->regions[i]; |
| 988 | seq_printf(m, "%4d: ", i); |
| 989 | if (sizeof(phys_addr_t) == 4) |
| 990 | seq_printf(m, "0x%08lx..0x%08lx\n", |
| 991 | (unsigned long)reg->base, |
| 992 | (unsigned long)(reg->base + reg->size - 1)); |
| 993 | else |
| 994 | seq_printf(m, "0x%016llx..0x%016llx\n", |
| 995 | (unsigned long long)reg->base, |
| 996 | (unsigned long long)(reg->base + reg->size - 1)); |
| 997 | |
| 998 | } |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | static int memblock_debug_open(struct inode *inode, struct file *file) |
| 1003 | { |
| 1004 | return single_open(file, memblock_debug_show, inode->i_private); |
| 1005 | } |
| 1006 | |
| 1007 | static const struct file_operations memblock_debug_fops = { |
| 1008 | .open = memblock_debug_open, |
| 1009 | .read = seq_read, |
| 1010 | .llseek = seq_lseek, |
| 1011 | .release = single_release, |
| 1012 | }; |
| 1013 | |
| 1014 | static int __init memblock_init_debugfs(void) |
| 1015 | { |
| 1016 | struct dentry *root = debugfs_create_dir("memblock", NULL); |
| 1017 | if (!root) |
| 1018 | return -ENXIO; |
| 1019 | debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops); |
| 1020 | debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops); |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | __initcall(memblock_init_debugfs); |
| 1025 | |
| 1026 | #endif /* CONFIG_DEBUG_FS */ |