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/slab.h> 15 #include <linux/init.h> 16 #include <linux/bitops.h> 17 #include <linux/poison.h> 18 #include <linux/pfn.h> 19 #include <linux/debugfs.h> 20 #include <linux/seq_file.h> 21 #include <linux/memblock.h> 22 23 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; 24 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; 25 26 struct memblock memblock __initdata_memblock = { 27 .memory.regions = memblock_memory_init_regions, 28 .memory.cnt = 1, /* empty dummy entry */ 29 .memory.max = INIT_MEMBLOCK_REGIONS, 30 31 .reserved.regions = memblock_reserved_init_regions, 32 .reserved.cnt = 1, /* empty dummy entry */ 33 .reserved.max = INIT_MEMBLOCK_REGIONS, 34 35 .current_limit = MEMBLOCK_ALLOC_ANYWHERE, 36 }; 37 38 int memblock_debug __initdata_memblock; 39 static int memblock_can_resize __initdata_memblock; 40 static int memblock_memory_in_slab __initdata_memblock = 0; 41 static int memblock_reserved_in_slab __initdata_memblock = 0; 42 43 /* inline so we don't get a warning when pr_debug is compiled out */ 44 static __init_memblock const char * 45 memblock_type_name(struct memblock_type *type) 46 { 47 if (type == &memblock.memory) 48 return "memory"; 49 else if (type == &memblock.reserved) 50 return "reserved"; 51 else 52 return "unknown"; 53 } 54 55 /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */ 56 static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size) 57 { 58 return *size = min(*size, (phys_addr_t)ULLONG_MAX - base); 59 } 60 61 /* 62 * Address comparison utilities 63 */ 64 static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, 65 phys_addr_t base2, phys_addr_t size2) 66 { 67 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); 68 } 69 70 static long __init_memblock memblock_overlaps_region(struct memblock_type *type, 71 phys_addr_t base, phys_addr_t size) 72 { 73 unsigned long i; 74 75 for (i = 0; i < type->cnt; i++) { 76 phys_addr_t rgnbase = type->regions[i].base; 77 phys_addr_t rgnsize = type->regions[i].size; 78 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize)) 79 break; 80 } 81 82 return (i < type->cnt) ? i : -1; 83 } 84 85 /** 86 * memblock_find_in_range_node - find free area in given range and node 87 * @start: start of candidate range 88 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} 89 * @size: size of free area to find 90 * @align: alignment of free area to find 91 * @nid: nid of the free area to find, %MAX_NUMNODES for any node 92 * 93 * Find @size free area aligned to @align in the specified range and node. 94 * 95 * If we have CONFIG_HAVE_MEMBLOCK_NODE_MAP defined, we need to check if the 96 * memory we found if not in hotpluggable ranges. 97 * 98 * RETURNS: 99 * Found address on success, %0 on failure. 100 */ 101 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 102 phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start, 103 phys_addr_t end, phys_addr_t size, 104 phys_addr_t align, int nid) 105 { 106 phys_addr_t this_start, this_end, cand; 107 u64 i; 108 int curr = movablemem_map.nr_map - 1; 109 110 /* pump up @end */ 111 if (end == MEMBLOCK_ALLOC_ACCESSIBLE) 112 end = memblock.current_limit; 113 114 /* avoid allocating the first page */ 115 start = max_t(phys_addr_t, start, PAGE_SIZE); 116 end = max(start, end); 117 118 for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) { 119 this_start = clamp(this_start, start, end); 120 this_end = clamp(this_end, start, end); 121 122 restart: 123 if (this_end <= this_start || this_end < size) 124 continue; 125 126 for (; curr >= 0; curr--) { 127 if ((movablemem_map.map[curr].start_pfn << PAGE_SHIFT) 128 < this_end) 129 break; 130 } 131 132 cand = round_down(this_end - size, align); 133 if (curr >= 0 && 134 cand < movablemem_map.map[curr].end_pfn << PAGE_SHIFT) { 135 this_end = movablemem_map.map[curr].start_pfn 136 << PAGE_SHIFT; 137 goto restart; 138 } 139 140 if (cand >= this_start) 141 return cand; 142 } 143 144 return 0; 145 } 146 #else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ 147 phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start, 148 phys_addr_t end, phys_addr_t size, 149 phys_addr_t align, int nid) 150 { 151 phys_addr_t this_start, this_end, cand; 152 u64 i; 153 154 /* pump up @end */ 155 if (end == MEMBLOCK_ALLOC_ACCESSIBLE) 156 end = memblock.current_limit; 157 158 /* avoid allocating the first page */ 159 start = max_t(phys_addr_t, start, PAGE_SIZE); 160 end = max(start, end); 161 162 for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) { 163 this_start = clamp(this_start, start, end); 164 this_end = clamp(this_end, start, end); 165 166 if (this_end < size) 167 continue; 168 169 cand = round_down(this_end - size, align); 170 if (cand >= this_start) 171 return cand; 172 } 173 return 0; 174 } 175 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ 176 177 /** 178 * memblock_find_in_range - find free area in given range 179 * @start: start of candidate range 180 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} 181 * @size: size of free area to find 182 * @align: alignment of free area to find 183 * 184 * Find @size free area aligned to @align in the specified range. 185 * 186 * RETURNS: 187 * Found address on success, %0 on failure. 188 */ 189 phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, 190 phys_addr_t end, phys_addr_t size, 191 phys_addr_t align) 192 { 193 return memblock_find_in_range_node(start, end, size, align, 194 MAX_NUMNODES); 195 } 196 197 static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r) 198 { 199 type->total_size -= type->regions[r].size; 200 memmove(&type->regions[r], &type->regions[r + 1], 201 (type->cnt - (r + 1)) * sizeof(type->regions[r])); 202 type->cnt--; 203 204 /* Special case for empty arrays */ 205 if (type->cnt == 0) { 206 WARN_ON(type->total_size != 0); 207 type->cnt = 1; 208 type->regions[0].base = 0; 209 type->regions[0].size = 0; 210 memblock_set_region_node(&type->regions[0], MAX_NUMNODES); 211 } 212 } 213 214 phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info( 215 phys_addr_t *addr) 216 { 217 if (memblock.reserved.regions == memblock_reserved_init_regions) 218 return 0; 219 220 *addr = __pa(memblock.reserved.regions); 221 222 return PAGE_ALIGN(sizeof(struct memblock_region) * 223 memblock.reserved.max); 224 } 225 226 /** 227 * memblock_double_array - double the size of the memblock regions array 228 * @type: memblock type of the regions array being doubled 229 * @new_area_start: starting address of memory range to avoid overlap with 230 * @new_area_size: size of memory range to avoid overlap with 231 * 232 * Double the size of the @type regions array. If memblock is being used to 233 * allocate memory for a new reserved regions array and there is a previously 234 * allocated memory range [@new_area_start,@new_area_start+@new_area_size] 235 * waiting to be reserved, ensure the memory used by the new array does 236 * not overlap. 237 * 238 * RETURNS: 239 * 0 on success, -1 on failure. 240 */ 241 static int __init_memblock memblock_double_array(struct memblock_type *type, 242 phys_addr_t new_area_start, 243 phys_addr_t new_area_size) 244 { 245 struct memblock_region *new_array, *old_array; 246 phys_addr_t old_alloc_size, new_alloc_size; 247 phys_addr_t old_size, new_size, addr; 248 int use_slab = slab_is_available(); 249 int *in_slab; 250 251 /* We don't allow resizing until we know about the reserved regions 252 * of memory that aren't suitable for allocation 253 */ 254 if (!memblock_can_resize) 255 return -1; 256 257 /* Calculate new doubled size */ 258 old_size = type->max * sizeof(struct memblock_region); 259 new_size = old_size << 1; 260 /* 261 * We need to allocated new one align to PAGE_SIZE, 262 * so we can free them completely later. 263 */ 264 old_alloc_size = PAGE_ALIGN(old_size); 265 new_alloc_size = PAGE_ALIGN(new_size); 266 267 /* Retrieve the slab flag */ 268 if (type == &memblock.memory) 269 in_slab = &memblock_memory_in_slab; 270 else 271 in_slab = &memblock_reserved_in_slab; 272 273 /* Try to find some space for it. 274 * 275 * WARNING: We assume that either slab_is_available() and we use it or 276 * we use MEMBLOCK for allocations. That means that this is unsafe to 277 * use when bootmem is currently active (unless bootmem itself is 278 * implemented on top of MEMBLOCK which isn't the case yet) 279 * 280 * This should however not be an issue for now, as we currently only 281 * call into MEMBLOCK while it's still active, or much later when slab 282 * is active for memory hotplug operations 283 */ 284 if (use_slab) { 285 new_array = kmalloc(new_size, GFP_KERNEL); 286 addr = new_array ? __pa(new_array) : 0; 287 } else { 288 /* only exclude range when trying to double reserved.regions */ 289 if (type != &memblock.reserved) 290 new_area_start = new_area_size = 0; 291 292 addr = memblock_find_in_range(new_area_start + new_area_size, 293 memblock.current_limit, 294 new_alloc_size, PAGE_SIZE); 295 if (!addr && new_area_size) 296 addr = memblock_find_in_range(0, 297 min(new_area_start, memblock.current_limit), 298 new_alloc_size, PAGE_SIZE); 299 300 new_array = addr ? __va(addr) : NULL; 301 } 302 if (!addr) { 303 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", 304 memblock_type_name(type), type->max, type->max * 2); 305 return -1; 306 } 307 308 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]", 309 memblock_type_name(type), type->max * 2, (u64)addr, 310 (u64)addr + new_size - 1); 311 312 /* 313 * Found space, we now need to move the array over before we add the 314 * reserved region since it may be our reserved array itself that is 315 * full. 316 */ 317 memcpy(new_array, type->regions, old_size); 318 memset(new_array + type->max, 0, old_size); 319 old_array = type->regions; 320 type->regions = new_array; 321 type->max <<= 1; 322 323 /* Free old array. We needn't free it if the array is the static one */ 324 if (*in_slab) 325 kfree(old_array); 326 else if (old_array != memblock_memory_init_regions && 327 old_array != memblock_reserved_init_regions) 328 memblock_free(__pa(old_array), old_alloc_size); 329 330 /* 331 * Reserve the new array if that comes from the memblock. Otherwise, we 332 * needn't do it 333 */ 334 if (!use_slab) 335 BUG_ON(memblock_reserve(addr, new_alloc_size)); 336 337 /* Update slab flag */ 338 *in_slab = use_slab; 339 340 return 0; 341 } 342 343 /** 344 * memblock_merge_regions - merge neighboring compatible regions 345 * @type: memblock type to scan 346 * 347 * Scan @type and merge neighboring compatible regions. 348 */ 349 static void __init_memblock memblock_merge_regions(struct memblock_type *type) 350 { 351 int i = 0; 352 353 /* cnt never goes below 1 */ 354 while (i < type->cnt - 1) { 355 struct memblock_region *this = &type->regions[i]; 356 struct memblock_region *next = &type->regions[i + 1]; 357 358 if (this->base + this->size != next->base || 359 memblock_get_region_node(this) != 360 memblock_get_region_node(next)) { 361 BUG_ON(this->base + this->size > next->base); 362 i++; 363 continue; 364 } 365 366 this->size += next->size; 367 /* move forward from next + 1, index of which is i + 2 */ 368 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next)); 369 type->cnt--; 370 } 371 } 372 373 /** 374 * memblock_insert_region - insert new memblock region 375 * @type: memblock type to insert into 376 * @idx: index for the insertion point 377 * @base: base address of the new region 378 * @size: size of the new region 379 * 380 * Insert new memblock region [@base,@base+@size) into @type at @idx. 381 * @type must already have extra room to accomodate the new region. 382 */ 383 static void __init_memblock memblock_insert_region(struct memblock_type *type, 384 int idx, phys_addr_t base, 385 phys_addr_t size, int nid) 386 { 387 struct memblock_region *rgn = &type->regions[idx]; 388 389 BUG_ON(type->cnt >= type->max); 390 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); 391 rgn->base = base; 392 rgn->size = size; 393 memblock_set_region_node(rgn, nid); 394 type->cnt++; 395 type->total_size += size; 396 } 397 398 /** 399 * memblock_add_region - add new memblock region 400 * @type: memblock type to add new region into 401 * @base: base address of the new region 402 * @size: size of the new region 403 * @nid: nid of the new region 404 * 405 * Add new memblock region [@base,@base+@size) into @type. The new region 406 * is allowed to overlap with existing ones - overlaps don't affect already 407 * existing regions. @type is guaranteed to be minimal (all neighbouring 408 * compatible regions are merged) after the addition. 409 * 410 * RETURNS: 411 * 0 on success, -errno on failure. 412 */ 413 static int __init_memblock memblock_add_region(struct memblock_type *type, 414 phys_addr_t base, phys_addr_t size, int nid) 415 { 416 bool insert = false; 417 phys_addr_t obase = base; 418 phys_addr_t end = base + memblock_cap_size(base, &size); 419 int i, nr_new; 420 421 if (!size) 422 return 0; 423 424 /* special case for empty array */ 425 if (type->regions[0].size == 0) { 426 WARN_ON(type->cnt != 1 || type->total_size); 427 type->regions[0].base = base; 428 type->regions[0].size = size; 429 memblock_set_region_node(&type->regions[0], nid); 430 type->total_size = size; 431 return 0; 432 } 433 repeat: 434 /* 435 * The following is executed twice. Once with %false @insert and 436 * then with %true. The first counts the number of regions needed 437 * to accomodate the new area. The second actually inserts them. 438 */ 439 base = obase; 440 nr_new = 0; 441 442 for (i = 0; i < type->cnt; i++) { 443 struct memblock_region *rgn = &type->regions[i]; 444 phys_addr_t rbase = rgn->base; 445 phys_addr_t rend = rbase + rgn->size; 446 447 if (rbase >= end) 448 break; 449 if (rend <= base) 450 continue; 451 /* 452 * @rgn overlaps. If it separates the lower part of new 453 * area, insert that portion. 454 */ 455 if (rbase > base) { 456 nr_new++; 457 if (insert) 458 memblock_insert_region(type, i++, base, 459 rbase - base, nid); 460 } 461 /* area below @rend is dealt with, forget about it */ 462 base = min(rend, end); 463 } 464 465 /* insert the remaining portion */ 466 if (base < end) { 467 nr_new++; 468 if (insert) 469 memblock_insert_region(type, i, base, end - base, nid); 470 } 471 472 /* 473 * If this was the first round, resize array and repeat for actual 474 * insertions; otherwise, merge and return. 475 */ 476 if (!insert) { 477 while (type->cnt + nr_new > type->max) 478 if (memblock_double_array(type, obase, size) < 0) 479 return -ENOMEM; 480 insert = true; 481 goto repeat; 482 } else { 483 memblock_merge_regions(type); 484 return 0; 485 } 486 } 487 488 int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size, 489 int nid) 490 { 491 return memblock_add_region(&memblock.memory, base, size, nid); 492 } 493 494 int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) 495 { 496 return memblock_add_region(&memblock.memory, base, size, MAX_NUMNODES); 497 } 498 499 /** 500 * memblock_isolate_range - isolate given range into disjoint memblocks 501 * @type: memblock type to isolate range for 502 * @base: base of range to isolate 503 * @size: size of range to isolate 504 * @start_rgn: out parameter for the start of isolated region 505 * @end_rgn: out parameter for the end of isolated region 506 * 507 * Walk @type and ensure that regions don't cross the boundaries defined by 508 * [@base,@base+@size). Crossing regions are split at the boundaries, 509 * which may create at most two more regions. The index of the first 510 * region inside the range is returned in *@start_rgn and end in *@end_rgn. 511 * 512 * RETURNS: 513 * 0 on success, -errno on failure. 514 */ 515 static int __init_memblock memblock_isolate_range(struct memblock_type *type, 516 phys_addr_t base, phys_addr_t size, 517 int *start_rgn, int *end_rgn) 518 { 519 phys_addr_t end = base + memblock_cap_size(base, &size); 520 int i; 521 522 *start_rgn = *end_rgn = 0; 523 524 if (!size) 525 return 0; 526 527 /* we'll create at most two more regions */ 528 while (type->cnt + 2 > type->max) 529 if (memblock_double_array(type, base, size) < 0) 530 return -ENOMEM; 531 532 for (i = 0; i < type->cnt; i++) { 533 struct memblock_region *rgn = &type->regions[i]; 534 phys_addr_t rbase = rgn->base; 535 phys_addr_t rend = rbase + rgn->size; 536 537 if (rbase >= end) 538 break; 539 if (rend <= base) 540 continue; 541 542 if (rbase < base) { 543 /* 544 * @rgn intersects from below. Split and continue 545 * to process the next region - the new top half. 546 */ 547 rgn->base = base; 548 rgn->size -= base - rbase; 549 type->total_size -= base - rbase; 550 memblock_insert_region(type, i, rbase, base - rbase, 551 memblock_get_region_node(rgn)); 552 } else if (rend > end) { 553 /* 554 * @rgn intersects from above. Split and redo the 555 * current region - the new bottom half. 556 */ 557 rgn->base = end; 558 rgn->size -= end - rbase; 559 type->total_size -= end - rbase; 560 memblock_insert_region(type, i--, rbase, end - rbase, 561 memblock_get_region_node(rgn)); 562 } else { 563 /* @rgn is fully contained, record it */ 564 if (!*end_rgn) 565 *start_rgn = i; 566 *end_rgn = i + 1; 567 } 568 } 569 570 return 0; 571 } 572 573 static int __init_memblock __memblock_remove(struct memblock_type *type, 574 phys_addr_t base, phys_addr_t size) 575 { 576 int start_rgn, end_rgn; 577 int i, ret; 578 579 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 580 if (ret) 581 return ret; 582 583 for (i = end_rgn - 1; i >= start_rgn; i--) 584 memblock_remove_region(type, i); 585 return 0; 586 } 587 588 int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size) 589 { 590 return __memblock_remove(&memblock.memory, base, size); 591 } 592 593 int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size) 594 { 595 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n", 596 (unsigned long long)base, 597 (unsigned long long)base + size, 598 (void *)_RET_IP_); 599 600 return __memblock_remove(&memblock.reserved, base, size); 601 } 602 603 int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) 604 { 605 struct memblock_type *_rgn = &memblock.reserved; 606 607 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n", 608 (unsigned long long)base, 609 (unsigned long long)base + size, 610 (void *)_RET_IP_); 611 612 return memblock_add_region(_rgn, base, size, MAX_NUMNODES); 613 } 614 615 /** 616 * __next_free_mem_range - next function for for_each_free_mem_range() 617 * @idx: pointer to u64 loop variable 618 * @nid: nid: node selector, %MAX_NUMNODES for all nodes 619 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 620 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 621 * @out_nid: ptr to int for nid of the range, can be %NULL 622 * 623 * Find the first free area from *@idx which matches @nid, fill the out 624 * parameters, and update *@idx for the next iteration. The lower 32bit of 625 * *@idx contains index into memory region and the upper 32bit indexes the 626 * areas before each reserved region. For example, if reserved regions 627 * look like the following, 628 * 629 * 0:[0-16), 1:[32-48), 2:[128-130) 630 * 631 * The upper 32bit indexes the following regions. 632 * 633 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) 634 * 635 * As both region arrays are sorted, the function advances the two indices 636 * in lockstep and returns each intersection. 637 */ 638 void __init_memblock __next_free_mem_range(u64 *idx, int nid, 639 phys_addr_t *out_start, 640 phys_addr_t *out_end, int *out_nid) 641 { 642 struct memblock_type *mem = &memblock.memory; 643 struct memblock_type *rsv = &memblock.reserved; 644 int mi = *idx & 0xffffffff; 645 int ri = *idx >> 32; 646 647 for ( ; mi < mem->cnt; mi++) { 648 struct memblock_region *m = &mem->regions[mi]; 649 phys_addr_t m_start = m->base; 650 phys_addr_t m_end = m->base + m->size; 651 652 /* only memory regions are associated with nodes, check it */ 653 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m)) 654 continue; 655 656 /* scan areas before each reservation for intersection */ 657 for ( ; ri < rsv->cnt + 1; ri++) { 658 struct memblock_region *r = &rsv->regions[ri]; 659 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0; 660 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX; 661 662 /* if ri advanced past mi, break out to advance mi */ 663 if (r_start >= m_end) 664 break; 665 /* if the two regions intersect, we're done */ 666 if (m_start < r_end) { 667 if (out_start) 668 *out_start = max(m_start, r_start); 669 if (out_end) 670 *out_end = min(m_end, r_end); 671 if (out_nid) 672 *out_nid = memblock_get_region_node(m); 673 /* 674 * The region which ends first is advanced 675 * for the next iteration. 676 */ 677 if (m_end <= r_end) 678 mi++; 679 else 680 ri++; 681 *idx = (u32)mi | (u64)ri << 32; 682 return; 683 } 684 } 685 } 686 687 /* signal end of iteration */ 688 *idx = ULLONG_MAX; 689 } 690 691 /** 692 * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse() 693 * @idx: pointer to u64 loop variable 694 * @nid: nid: node selector, %MAX_NUMNODES for all nodes 695 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 696 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 697 * @out_nid: ptr to int for nid of the range, can be %NULL 698 * 699 * Reverse of __next_free_mem_range(). 700 */ 701 void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid, 702 phys_addr_t *out_start, 703 phys_addr_t *out_end, int *out_nid) 704 { 705 struct memblock_type *mem = &memblock.memory; 706 struct memblock_type *rsv = &memblock.reserved; 707 int mi = *idx & 0xffffffff; 708 int ri = *idx >> 32; 709 710 if (*idx == (u64)ULLONG_MAX) { 711 mi = mem->cnt - 1; 712 ri = rsv->cnt; 713 } 714 715 for ( ; mi >= 0; mi--) { 716 struct memblock_region *m = &mem->regions[mi]; 717 phys_addr_t m_start = m->base; 718 phys_addr_t m_end = m->base + m->size; 719 720 /* only memory regions are associated with nodes, check it */ 721 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m)) 722 continue; 723 724 /* scan areas before each reservation for intersection */ 725 for ( ; ri >= 0; ri--) { 726 struct memblock_region *r = &rsv->regions[ri]; 727 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0; 728 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX; 729 730 /* if ri advanced past mi, break out to advance mi */ 731 if (r_end <= m_start) 732 break; 733 /* if the two regions intersect, we're done */ 734 if (m_end > r_start) { 735 if (out_start) 736 *out_start = max(m_start, r_start); 737 if (out_end) 738 *out_end = min(m_end, r_end); 739 if (out_nid) 740 *out_nid = memblock_get_region_node(m); 741 742 if (m_start >= r_start) 743 mi--; 744 else 745 ri--; 746 *idx = (u32)mi | (u64)ri << 32; 747 return; 748 } 749 } 750 } 751 752 *idx = ULLONG_MAX; 753 } 754 755 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 756 /* 757 * Common iterator interface used to define for_each_mem_range(). 758 */ 759 void __init_memblock __next_mem_pfn_range(int *idx, int nid, 760 unsigned long *out_start_pfn, 761 unsigned long *out_end_pfn, int *out_nid) 762 { 763 struct memblock_type *type = &memblock.memory; 764 struct memblock_region *r; 765 766 while (++*idx < type->cnt) { 767 r = &type->regions[*idx]; 768 769 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) 770 continue; 771 if (nid == MAX_NUMNODES || nid == r->nid) 772 break; 773 } 774 if (*idx >= type->cnt) { 775 *idx = -1; 776 return; 777 } 778 779 if (out_start_pfn) 780 *out_start_pfn = PFN_UP(r->base); 781 if (out_end_pfn) 782 *out_end_pfn = PFN_DOWN(r->base + r->size); 783 if (out_nid) 784 *out_nid = r->nid; 785 } 786 787 /** 788 * memblock_set_node - set node ID on memblock regions 789 * @base: base of area to set node ID for 790 * @size: size of area to set node ID for 791 * @nid: node ID to set 792 * 793 * Set the nid of memblock memory regions in [@base,@base+@size) to @nid. 794 * Regions which cross the area boundaries are split as necessary. 795 * 796 * RETURNS: 797 * 0 on success, -errno on failure. 798 */ 799 int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, 800 int nid) 801 { 802 struct memblock_type *type = &memblock.memory; 803 int start_rgn, end_rgn; 804 int i, ret; 805 806 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 807 if (ret) 808 return ret; 809 810 for (i = start_rgn; i < end_rgn; i++) 811 memblock_set_region_node(&type->regions[i], nid); 812 813 memblock_merge_regions(type); 814 return 0; 815 } 816 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ 817 818 static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size, 819 phys_addr_t align, phys_addr_t max_addr, 820 int nid) 821 { 822 phys_addr_t found; 823 824 /* align @size to avoid excessive fragmentation on reserved array */ 825 size = round_up(size, align); 826 827 found = memblock_find_in_range_node(0, max_addr, size, align, nid); 828 if (found && !memblock_reserve(found, size)) 829 return found; 830 831 return 0; 832 } 833 834 phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid) 835 { 836 return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid); 837 } 838 839 phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) 840 { 841 return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES); 842 } 843 844 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) 845 { 846 phys_addr_t alloc; 847 848 alloc = __memblock_alloc_base(size, align, max_addr); 849 850 if (alloc == 0) 851 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n", 852 (unsigned long long) size, (unsigned long long) max_addr); 853 854 return alloc; 855 } 856 857 phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align) 858 { 859 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); 860 } 861 862 phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) 863 { 864 phys_addr_t res = memblock_alloc_nid(size, align, nid); 865 866 if (res) 867 return res; 868 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); 869 } 870 871 872 /* 873 * Remaining API functions 874 */ 875 876 phys_addr_t __init memblock_phys_mem_size(void) 877 { 878 return memblock.memory.total_size; 879 } 880 881 phys_addr_t __init memblock_mem_size(unsigned long limit_pfn) 882 { 883 unsigned long pages = 0; 884 struct memblock_region *r; 885 unsigned long start_pfn, end_pfn; 886 887 for_each_memblock(memory, r) { 888 start_pfn = memblock_region_memory_base_pfn(r); 889 end_pfn = memblock_region_memory_end_pfn(r); 890 start_pfn = min_t(unsigned long, start_pfn, limit_pfn); 891 end_pfn = min_t(unsigned long, end_pfn, limit_pfn); 892 pages += end_pfn - start_pfn; 893 } 894 895 return (phys_addr_t)pages << PAGE_SHIFT; 896 } 897 898 /* lowest address */ 899 phys_addr_t __init_memblock memblock_start_of_DRAM(void) 900 { 901 return memblock.memory.regions[0].base; 902 } 903 904 phys_addr_t __init_memblock memblock_end_of_DRAM(void) 905 { 906 int idx = memblock.memory.cnt - 1; 907 908 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); 909 } 910 911 void __init memblock_enforce_memory_limit(phys_addr_t limit) 912 { 913 unsigned long i; 914 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX; 915 916 if (!limit) 917 return; 918 919 /* find out max address */ 920 for (i = 0; i < memblock.memory.cnt; i++) { 921 struct memblock_region *r = &memblock.memory.regions[i]; 922 923 if (limit <= r->size) { 924 max_addr = r->base + limit; 925 break; 926 } 927 limit -= r->size; 928 } 929 930 /* truncate both memory and reserved regions */ 931 __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX); 932 __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX); 933 } 934 935 static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr) 936 { 937 unsigned int left = 0, right = type->cnt; 938 939 do { 940 unsigned int mid = (right + left) / 2; 941 942 if (addr < type->regions[mid].base) 943 right = mid; 944 else if (addr >= (type->regions[mid].base + 945 type->regions[mid].size)) 946 left = mid + 1; 947 else 948 return mid; 949 } while (left < right); 950 return -1; 951 } 952 953 int __init memblock_is_reserved(phys_addr_t addr) 954 { 955 return memblock_search(&memblock.reserved, addr) != -1; 956 } 957 958 int __init_memblock memblock_is_memory(phys_addr_t addr) 959 { 960 return memblock_search(&memblock.memory, addr) != -1; 961 } 962 963 /** 964 * memblock_is_region_memory - check if a region is a subset of memory 965 * @base: base of region to check 966 * @size: size of region to check 967 * 968 * Check if the region [@base, @base+@size) is a subset of a memory block. 969 * 970 * RETURNS: 971 * 0 if false, non-zero if true 972 */ 973 int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size) 974 { 975 int idx = memblock_search(&memblock.memory, base); 976 phys_addr_t end = base + memblock_cap_size(base, &size); 977 978 if (idx == -1) 979 return 0; 980 return memblock.memory.regions[idx].base <= base && 981 (memblock.memory.regions[idx].base + 982 memblock.memory.regions[idx].size) >= end; 983 } 984 985 /** 986 * memblock_is_region_reserved - check if a region intersects reserved memory 987 * @base: base of region to check 988 * @size: size of region to check 989 * 990 * Check if the region [@base, @base+@size) intersects a reserved memory block. 991 * 992 * RETURNS: 993 * 0 if false, non-zero if true 994 */ 995 int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) 996 { 997 memblock_cap_size(base, &size); 998 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0; 999 } 1000 1001 void __init_memblock memblock_trim_memory(phys_addr_t align) 1002 { 1003 int i; 1004 phys_addr_t start, end, orig_start, orig_end; 1005 struct memblock_type *mem = &memblock.memory; 1006 1007 for (i = 0; i < mem->cnt; i++) { 1008 orig_start = mem->regions[i].base; 1009 orig_end = mem->regions[i].base + mem->regions[i].size; 1010 start = round_up(orig_start, align); 1011 end = round_down(orig_end, align); 1012 1013 if (start == orig_start && end == orig_end) 1014 continue; 1015 1016 if (start < end) { 1017 mem->regions[i].base = start; 1018 mem->regions[i].size = end - start; 1019 } else { 1020 memblock_remove_region(mem, i); 1021 i--; 1022 } 1023 } 1024 } 1025 1026 void __init_memblock memblock_set_current_limit(phys_addr_t limit) 1027 { 1028 memblock.current_limit = limit; 1029 } 1030 1031 static void __init_memblock memblock_dump(struct memblock_type *type, char *name) 1032 { 1033 unsigned long long base, size; 1034 int i; 1035 1036 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt); 1037 1038 for (i = 0; i < type->cnt; i++) { 1039 struct memblock_region *rgn = &type->regions[i]; 1040 char nid_buf[32] = ""; 1041 1042 base = rgn->base; 1043 size = rgn->size; 1044 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 1045 if (memblock_get_region_node(rgn) != MAX_NUMNODES) 1046 snprintf(nid_buf, sizeof(nid_buf), " on node %d", 1047 memblock_get_region_node(rgn)); 1048 #endif 1049 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n", 1050 name, i, base, base + size - 1, size, nid_buf); 1051 } 1052 } 1053 1054 void __init_memblock __memblock_dump_all(void) 1055 { 1056 pr_info("MEMBLOCK configuration:\n"); 1057 pr_info(" memory size = %#llx reserved size = %#llx\n", 1058 (unsigned long long)memblock.memory.total_size, 1059 (unsigned long long)memblock.reserved.total_size); 1060 1061 memblock_dump(&memblock.memory, "memory"); 1062 memblock_dump(&memblock.reserved, "reserved"); 1063 } 1064 1065 void __init memblock_allow_resize(void) 1066 { 1067 memblock_can_resize = 1; 1068 } 1069 1070 static int __init early_memblock(char *p) 1071 { 1072 if (p && strstr(p, "debug")) 1073 memblock_debug = 1; 1074 return 0; 1075 } 1076 early_param("memblock", early_memblock); 1077 1078 #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK) 1079 1080 static int memblock_debug_show(struct seq_file *m, void *private) 1081 { 1082 struct memblock_type *type = m->private; 1083 struct memblock_region *reg; 1084 int i; 1085 1086 for (i = 0; i < type->cnt; i++) { 1087 reg = &type->regions[i]; 1088 seq_printf(m, "%4d: ", i); 1089 if (sizeof(phys_addr_t) == 4) 1090 seq_printf(m, "0x%08lx..0x%08lx\n", 1091 (unsigned long)reg->base, 1092 (unsigned long)(reg->base + reg->size - 1)); 1093 else 1094 seq_printf(m, "0x%016llx..0x%016llx\n", 1095 (unsigned long long)reg->base, 1096 (unsigned long long)(reg->base + reg->size - 1)); 1097 1098 } 1099 return 0; 1100 } 1101 1102 static int memblock_debug_open(struct inode *inode, struct file *file) 1103 { 1104 return single_open(file, memblock_debug_show, inode->i_private); 1105 } 1106 1107 static const struct file_operations memblock_debug_fops = { 1108 .open = memblock_debug_open, 1109 .read = seq_read, 1110 .llseek = seq_lseek, 1111 .release = single_release, 1112 }; 1113 1114 static int __init memblock_init_debugfs(void) 1115 { 1116 struct dentry *root = debugfs_create_dir("memblock", NULL); 1117 if (!root) 1118 return -ENXIO; 1119 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops); 1120 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops); 1121 1122 return 0; 1123 } 1124 __initcall(memblock_init_debugfs); 1125 1126 #endif /* CONFIG_DEBUG_FS */ 1127