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