1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Procedures for maintaining information about logical memory blocks. 4 * 5 * Peter Bergner, IBM Corp. June 2001. 6 * Copyright (C) 2001 Peter Bergner. 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/slab.h> 11 #include <linux/init.h> 12 #include <linux/bitops.h> 13 #include <linux/poison.h> 14 #include <linux/pfn.h> 15 #include <linux/debugfs.h> 16 #include <linux/kmemleak.h> 17 #include <linux/seq_file.h> 18 #include <linux/memblock.h> 19 #include <linux/mutex.h> 20 21 #ifdef CONFIG_KEXEC_HANDOVER 22 #include <linux/libfdt.h> 23 #include <linux/kexec_handover.h> 24 #include <linux/kho/abi/memblock.h> 25 #endif /* CONFIG_KEXEC_HANDOVER */ 26 27 #include <asm/sections.h> 28 #include <linux/io.h> 29 30 #include "internal.h" 31 32 #define INIT_MEMBLOCK_REGIONS 128 33 #define INIT_PHYSMEM_REGIONS 4 34 35 #ifndef INIT_MEMBLOCK_RESERVED_REGIONS 36 # define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS 37 #endif 38 39 #ifndef INIT_MEMBLOCK_MEMORY_REGIONS 40 #define INIT_MEMBLOCK_MEMORY_REGIONS INIT_MEMBLOCK_REGIONS 41 #endif 42 43 /** 44 * DOC: memblock overview 45 * 46 * Memblock is a method of managing memory regions during the early 47 * boot period when the usual kernel memory allocators are not up and 48 * running. 49 * 50 * Memblock views the system memory as collections of contiguous 51 * regions. There are several types of these collections: 52 * 53 * * ``memory`` - describes the physical memory available to the 54 * kernel; this may differ from the actual physical memory installed 55 * in the system, for instance when the memory is restricted with 56 * ``mem=`` command line parameter 57 * * ``reserved`` - describes the regions that were allocated 58 * * ``physmem`` - describes the actual physical memory available during 59 * boot regardless of the possible restrictions and memory hot(un)plug; 60 * the ``physmem`` type is only available on some architectures. 61 * 62 * Each region is represented by struct memblock_region that 63 * defines the region extents, its attributes and NUMA node id on NUMA 64 * systems. Every memory type is described by the struct memblock_type 65 * which contains an array of memory regions along with 66 * the allocator metadata. The "memory" and "reserved" types are nicely 67 * wrapped with struct memblock. This structure is statically 68 * initialized at build time. The region arrays are initially sized to 69 * %INIT_MEMBLOCK_MEMORY_REGIONS for "memory" and 70 * %INIT_MEMBLOCK_RESERVED_REGIONS for "reserved". The region array 71 * for "physmem" is initially sized to %INIT_PHYSMEM_REGIONS. 72 * The memblock_allow_resize() enables automatic resizing of the region 73 * arrays during addition of new regions. This feature should be used 74 * with care so that memory allocated for the region array will not 75 * overlap with areas that should be reserved, for example initrd. 76 * 77 * The early architecture setup should tell memblock what the physical 78 * memory layout is by using memblock_add() or memblock_add_node() 79 * functions. The first function does not assign the region to a NUMA 80 * node and it is appropriate for UMA systems. Yet, it is possible to 81 * use it on NUMA systems as well and assign the region to a NUMA node 82 * later in the setup process using memblock_set_node(). The 83 * memblock_add_node() performs such an assignment directly. 84 * 85 * Once memblock is setup the memory can be allocated using one of the 86 * API variants: 87 * 88 * * memblock_phys_alloc*() - these functions return the **physical** 89 * address of the allocated memory 90 * * memblock_alloc*() - these functions return the **virtual** address 91 * of the allocated memory. 92 * 93 * Note, that both API variants use implicit assumptions about allowed 94 * memory ranges and the fallback methods. Consult the documentation 95 * of memblock_alloc_internal() and memblock_alloc_range_nid() 96 * functions for more elaborate description. 97 * 98 * As the system boot progresses, the architecture specific mem_init() 99 * function frees all the memory to the buddy page allocator. 100 * 101 * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the 102 * memblock data structures (except "physmem") will be discarded after the 103 * system initialization completes. 104 */ 105 106 #ifndef CONFIG_NUMA 107 struct pglist_data __refdata contig_page_data; 108 EXPORT_SYMBOL(contig_page_data); 109 #endif 110 111 unsigned long max_low_pfn; 112 unsigned long min_low_pfn; 113 unsigned long max_pfn; 114 unsigned long long max_possible_pfn; 115 116 #ifdef CONFIG_MEMBLOCK_KHO_SCRATCH 117 /* When set to true, only allocate from MEMBLOCK_KHO_SCRATCH ranges */ 118 static bool kho_scratch_only; 119 #else 120 #define kho_scratch_only false 121 #endif 122 123 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_MEMORY_REGIONS] __initdata_memblock; 124 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock; 125 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 126 static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS]; 127 #endif 128 129 struct memblock memblock __initdata_memblock = { 130 .memory.regions = memblock_memory_init_regions, 131 .memory.max = INIT_MEMBLOCK_MEMORY_REGIONS, 132 .memory.name = "memory", 133 134 .reserved.regions = memblock_reserved_init_regions, 135 .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS, 136 .reserved.name = "reserved", 137 138 .bottom_up = false, 139 .current_limit = MEMBLOCK_ALLOC_ANYWHERE, 140 }; 141 142 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 143 struct memblock_type physmem = { 144 .regions = memblock_physmem_init_regions, 145 .max = INIT_PHYSMEM_REGIONS, 146 .name = "physmem", 147 }; 148 #endif 149 150 /* 151 * keep a pointer to &memblock.memory in the text section to use it in 152 * __next_mem_range() and its helpers. 153 * For architectures that do not keep memblock data after init, this 154 * pointer will be reset to NULL at memblock_discard() 155 */ 156 static __refdata struct memblock_type *memblock_memory = &memblock.memory; 157 158 #define for_each_memblock_type(i, memblock_type, rgn) \ 159 for (i = 0, rgn = &memblock_type->regions[0]; \ 160 i < memblock_type->cnt; \ 161 i++, rgn = &memblock_type->regions[i]) 162 163 #define memblock_dbg(fmt, ...) \ 164 do { \ 165 if (memblock_debug) \ 166 pr_info(fmt, ##__VA_ARGS__); \ 167 } while (0) 168 169 static int memblock_debug __initdata_memblock; 170 static bool system_has_some_mirror __initdata_memblock; 171 static int memblock_can_resize __initdata_memblock; 172 static int memblock_memory_in_slab __initdata_memblock; 173 static int memblock_reserved_in_slab __initdata_memblock; 174 175 bool __init_memblock memblock_has_mirror(void) 176 { 177 return system_has_some_mirror; 178 } 179 180 static enum memblock_flags __init_memblock choose_memblock_flags(void) 181 { 182 /* skip non-scratch memory for kho early boot allocations */ 183 if (kho_scratch_only) 184 return MEMBLOCK_KHO_SCRATCH; 185 186 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE; 187 } 188 189 /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */ 190 static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size) 191 { 192 return *size = min(*size, PHYS_ADDR_MAX - base); 193 } 194 195 /* 196 * Address comparison utilities 197 */ 198 unsigned long __init_memblock 199 memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, phys_addr_t base2, 200 phys_addr_t size2) 201 { 202 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); 203 } 204 205 bool __init_memblock memblock_overlaps_region(struct memblock_type *type, 206 phys_addr_t base, phys_addr_t size) 207 { 208 unsigned long i; 209 210 memblock_cap_size(base, &size); 211 212 for (i = 0; i < type->cnt; i++) 213 if (memblock_addrs_overlap(base, size, type->regions[i].base, 214 type->regions[i].size)) 215 return true; 216 return false; 217 } 218 219 /** 220 * __memblock_find_range_bottom_up - find free area utility in bottom-up 221 * @start: start of candidate range 222 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 223 * %MEMBLOCK_ALLOC_ACCESSIBLE 224 * @size: size of free area to find 225 * @align: alignment of free area to find 226 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 227 * @flags: pick from blocks based on memory attributes 228 * 229 * Utility called from memblock_find_in_range_node(), find free area bottom-up. 230 * 231 * Return: 232 * Found address on success, 0 on failure. 233 */ 234 static phys_addr_t __init_memblock 235 __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end, 236 phys_addr_t size, phys_addr_t align, int nid, 237 enum memblock_flags flags) 238 { 239 phys_addr_t this_start, this_end, cand; 240 u64 i; 241 242 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) { 243 this_start = clamp(this_start, start, end); 244 this_end = clamp(this_end, start, end); 245 246 cand = round_up(this_start, align); 247 if (cand < this_end && this_end - cand >= size) 248 return cand; 249 } 250 251 return 0; 252 } 253 254 /** 255 * __memblock_find_range_top_down - find free area utility, in top-down 256 * @start: start of candidate range 257 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 258 * %MEMBLOCK_ALLOC_ACCESSIBLE 259 * @size: size of free area to find 260 * @align: alignment of free area to find 261 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 262 * @flags: pick from blocks based on memory attributes 263 * 264 * Utility called from memblock_find_in_range_node(), find free area top-down. 265 * 266 * Return: 267 * Found address on success, 0 on failure. 268 */ 269 static phys_addr_t __init_memblock 270 __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end, 271 phys_addr_t size, phys_addr_t align, int nid, 272 enum memblock_flags flags) 273 { 274 phys_addr_t this_start, this_end, cand; 275 u64 i; 276 277 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end, 278 NULL) { 279 this_start = clamp(this_start, start, end); 280 this_end = clamp(this_end, start, end); 281 282 if (this_end < size) 283 continue; 284 285 cand = round_down(this_end - size, align); 286 if (cand >= this_start) 287 return cand; 288 } 289 290 return 0; 291 } 292 293 /** 294 * memblock_find_in_range_node - find free area in given range and node 295 * @size: size of free area to find 296 * @align: alignment of free area to find 297 * @start: start of candidate range 298 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 299 * %MEMBLOCK_ALLOC_ACCESSIBLE 300 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 301 * @flags: pick from blocks based on memory attributes 302 * 303 * Find @size free area aligned to @align in the specified range and node. 304 * 305 * Return: 306 * Found address on success, 0 on failure. 307 */ 308 static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size, 309 phys_addr_t align, phys_addr_t start, 310 phys_addr_t end, int nid, 311 enum memblock_flags flags) 312 { 313 /* pump up @end */ 314 if (end == MEMBLOCK_ALLOC_ACCESSIBLE || 315 end == MEMBLOCK_ALLOC_NOLEAKTRACE) 316 end = memblock.current_limit; 317 318 /* avoid allocating the first page */ 319 start = max_t(phys_addr_t, start, PAGE_SIZE); 320 end = max(start, end); 321 322 if (memblock_bottom_up()) 323 return __memblock_find_range_bottom_up(start, end, size, align, 324 nid, flags); 325 else 326 return __memblock_find_range_top_down(start, end, size, align, 327 nid, flags); 328 } 329 330 /** 331 * memblock_find_in_range - find free area in given range 332 * @start: start of candidate range 333 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 334 * %MEMBLOCK_ALLOC_ACCESSIBLE 335 * @size: size of free area to find 336 * @align: alignment of free area to find 337 * 338 * Find @size free area aligned to @align in the specified range. 339 * 340 * Return: 341 * Found address on success, 0 on failure. 342 */ 343 static phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, 344 phys_addr_t end, phys_addr_t size, 345 phys_addr_t align) 346 { 347 phys_addr_t ret; 348 enum memblock_flags flags = choose_memblock_flags(); 349 350 again: 351 ret = memblock_find_in_range_node(size, align, start, end, 352 NUMA_NO_NODE, flags); 353 354 if (!ret && (flags & MEMBLOCK_MIRROR)) { 355 pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n", 356 &size); 357 flags &= ~MEMBLOCK_MIRROR; 358 goto again; 359 } 360 361 return ret; 362 } 363 364 static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r) 365 { 366 type->total_size -= type->regions[r].size; 367 memmove(&type->regions[r], &type->regions[r + 1], 368 (type->cnt - (r + 1)) * sizeof(type->regions[r])); 369 type->cnt--; 370 371 /* Special case for empty arrays */ 372 if (type->cnt == 0) { 373 WARN_ON(type->total_size != 0); 374 type->regions[0].base = 0; 375 type->regions[0].size = 0; 376 type->regions[0].flags = 0; 377 memblock_set_region_node(&type->regions[0], MAX_NUMNODES); 378 } 379 } 380 381 #ifndef CONFIG_ARCH_KEEP_MEMBLOCK 382 /** 383 * memblock_discard - discard memory and reserved arrays if they were allocated 384 */ 385 void __init memblock_discard(void) 386 { 387 phys_addr_t addr, size; 388 389 if (memblock.reserved.regions != memblock_reserved_init_regions) { 390 addr = __pa(memblock.reserved.regions); 391 size = PAGE_ALIGN(sizeof(struct memblock_region) * 392 memblock.reserved.max); 393 if (memblock_reserved_in_slab) 394 kfree(memblock.reserved.regions); 395 else 396 memblock_free_late(addr, size); 397 } 398 399 if (memblock.memory.regions != memblock_memory_init_regions) { 400 addr = __pa(memblock.memory.regions); 401 size = PAGE_ALIGN(sizeof(struct memblock_region) * 402 memblock.memory.max); 403 if (memblock_memory_in_slab) 404 kfree(memblock.memory.regions); 405 else 406 memblock_free_late(addr, size); 407 } 408 409 memblock_memory = NULL; 410 } 411 #endif 412 413 /** 414 * memblock_double_array - double the size of the memblock regions array 415 * @type: memblock type of the regions array being doubled 416 * @new_area_start: starting address of memory range to avoid overlap with 417 * @new_area_size: size of memory range to avoid overlap with 418 * 419 * Double the size of the @type regions array. If memblock is being used to 420 * allocate memory for a new reserved regions array and there is a previously 421 * allocated memory range [@new_area_start, @new_area_start + @new_area_size] 422 * waiting to be reserved, ensure the memory used by the new array does 423 * not overlap. 424 * 425 * Return: 426 * 0 on success, -1 on failure. 427 */ 428 static int __init_memblock memblock_double_array(struct memblock_type *type, 429 phys_addr_t new_area_start, 430 phys_addr_t new_area_size) 431 { 432 struct memblock_region *new_array, *old_array; 433 phys_addr_t old_alloc_size, new_alloc_size; 434 phys_addr_t old_size, new_size, addr, new_end; 435 int use_slab = slab_is_available(); 436 int *in_slab; 437 438 /* We don't allow resizing until we know about the reserved regions 439 * of memory that aren't suitable for allocation 440 */ 441 if (!memblock_can_resize) 442 panic("memblock: cannot resize %s array\n", type->name); 443 444 /* Calculate new doubled size */ 445 old_size = type->max * sizeof(struct memblock_region); 446 new_size = old_size << 1; 447 /* 448 * We need to allocated new one align to PAGE_SIZE, 449 * so we can free them completely later. 450 */ 451 old_alloc_size = PAGE_ALIGN(old_size); 452 new_alloc_size = PAGE_ALIGN(new_size); 453 454 /* Retrieve the slab flag */ 455 if (type == &memblock.memory) 456 in_slab = &memblock_memory_in_slab; 457 else 458 in_slab = &memblock_reserved_in_slab; 459 460 /* Try to find some space for it */ 461 if (use_slab) { 462 new_array = kmalloc(new_size, GFP_KERNEL); 463 addr = new_array ? __pa(new_array) : 0; 464 } else { 465 /* only exclude range when trying to double reserved.regions */ 466 if (type != &memblock.reserved) 467 new_area_start = new_area_size = 0; 468 469 addr = memblock_find_in_range(new_area_start + new_area_size, 470 memblock.current_limit, 471 new_alloc_size, PAGE_SIZE); 472 if (!addr && new_area_size) 473 addr = memblock_find_in_range(0, 474 min(new_area_start, memblock.current_limit), 475 new_alloc_size, PAGE_SIZE); 476 477 if (addr) { 478 /* The memory may not have been accepted, yet. */ 479 accept_memory(addr, new_alloc_size); 480 481 new_array = __va(addr); 482 } else { 483 new_array = NULL; 484 } 485 } 486 if (!addr) { 487 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", 488 type->name, type->max, type->max * 2); 489 return -1; 490 } 491 492 new_end = addr + new_size - 1; 493 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]", 494 type->name, type->max * 2, &addr, &new_end); 495 496 /* 497 * Found space, we now need to move the array over before we add the 498 * reserved region since it may be our reserved array itself that is 499 * full. 500 */ 501 memcpy(new_array, type->regions, old_size); 502 memset(new_array + type->max, 0, old_size); 503 old_array = type->regions; 504 type->regions = new_array; 505 type->max <<= 1; 506 507 /* Free old array. We needn't free it if the array is the static one */ 508 if (*in_slab) 509 kfree(old_array); 510 else if (old_array != memblock_memory_init_regions && 511 old_array != memblock_reserved_init_regions) 512 memblock_free(old_array, old_alloc_size); 513 514 /* 515 * Reserve the new array if that comes from the memblock. Otherwise, we 516 * needn't do it 517 */ 518 if (!use_slab) 519 BUG_ON(memblock_reserve_kern(addr, new_alloc_size)); 520 521 /* Update slab flag */ 522 *in_slab = use_slab; 523 524 return 0; 525 } 526 527 /** 528 * memblock_merge_regions - merge neighboring compatible regions 529 * @type: memblock type to scan 530 * @start_rgn: start scanning from (@start_rgn - 1) 531 * @end_rgn: end scanning at (@end_rgn - 1) 532 * Scan @type and merge neighboring compatible regions in [@start_rgn - 1, @end_rgn) 533 */ 534 static void __init_memblock memblock_merge_regions(struct memblock_type *type, 535 unsigned long start_rgn, 536 unsigned long end_rgn) 537 { 538 int i = 0; 539 if (start_rgn) 540 i = start_rgn - 1; 541 end_rgn = min(end_rgn, type->cnt - 1); 542 while (i < end_rgn) { 543 struct memblock_region *this = &type->regions[i]; 544 struct memblock_region *next = &type->regions[i + 1]; 545 546 if (this->base + this->size != next->base || 547 memblock_get_region_node(this) != 548 memblock_get_region_node(next) || 549 this->flags != next->flags) { 550 BUG_ON(this->base + this->size > next->base); 551 i++; 552 continue; 553 } 554 555 this->size += next->size; 556 /* move forward from next + 1, index of which is i + 2 */ 557 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next)); 558 type->cnt--; 559 end_rgn--; 560 } 561 } 562 563 /** 564 * memblock_insert_region - insert new memblock region 565 * @type: memblock type to insert into 566 * @idx: index for the insertion point 567 * @base: base address of the new region 568 * @size: size of the new region 569 * @nid: node id of the new region 570 * @flags: flags of the new region 571 * 572 * Insert new memblock region [@base, @base + @size) into @type at @idx. 573 * @type must already have extra room to accommodate the new region. 574 */ 575 static void __init_memblock memblock_insert_region(struct memblock_type *type, 576 int idx, phys_addr_t base, 577 phys_addr_t size, 578 int nid, 579 enum memblock_flags flags) 580 { 581 struct memblock_region *rgn = &type->regions[idx]; 582 583 BUG_ON(type->cnt >= type->max); 584 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); 585 rgn->base = base; 586 rgn->size = size; 587 rgn->flags = flags; 588 memblock_set_region_node(rgn, nid); 589 type->cnt++; 590 type->total_size += size; 591 } 592 593 /** 594 * memblock_add_range - add new memblock region 595 * @type: memblock type to add new region into 596 * @base: base address of the new region 597 * @size: size of the new region 598 * @nid: nid of the new region 599 * @flags: flags of the new region 600 * 601 * Add new memblock region [@base, @base + @size) into @type. The new region 602 * is allowed to overlap with existing ones - overlaps don't affect already 603 * existing regions. @type is guaranteed to be minimal (all neighbouring 604 * compatible regions are merged) after the addition. 605 * 606 * Return: 607 * 0 on success, -errno on failure. 608 */ 609 static int __init_memblock memblock_add_range(struct memblock_type *type, 610 phys_addr_t base, phys_addr_t size, 611 int nid, enum memblock_flags flags) 612 { 613 bool insert = false; 614 phys_addr_t obase = base; 615 phys_addr_t end = base + memblock_cap_size(base, &size); 616 int idx, nr_new, start_rgn = -1, end_rgn; 617 struct memblock_region *rgn; 618 619 if (!size) 620 return 0; 621 622 /* special case for empty array */ 623 if (type->regions[0].size == 0) { 624 WARN_ON(type->cnt != 0 || type->total_size); 625 type->regions[0].base = base; 626 type->regions[0].size = size; 627 type->regions[0].flags = flags; 628 memblock_set_region_node(&type->regions[0], nid); 629 type->total_size = size; 630 type->cnt = 1; 631 return 0; 632 } 633 634 /* 635 * The worst case is when new range overlaps all existing regions, 636 * then we'll need type->cnt + 1 empty regions in @type. So if 637 * type->cnt * 2 + 1 is less than or equal to type->max, we know 638 * that there is enough empty regions in @type, and we can insert 639 * regions directly. 640 */ 641 if (type->cnt * 2 + 1 <= type->max) 642 insert = true; 643 644 repeat: 645 /* 646 * The following is executed twice. Once with %false @insert and 647 * then with %true. The first counts the number of regions needed 648 * to accommodate the new area. The second actually inserts them. 649 */ 650 base = obase; 651 nr_new = 0; 652 653 for_each_memblock_type(idx, type, rgn) { 654 phys_addr_t rbase = rgn->base; 655 phys_addr_t rend = rbase + rgn->size; 656 657 if (rbase >= end) 658 break; 659 if (rend <= base) 660 continue; 661 /* 662 * @rgn overlaps. If it separates the lower part of new 663 * area, insert that portion. 664 */ 665 if (rbase > base) { 666 #ifdef CONFIG_NUMA 667 WARN_ON(nid != memblock_get_region_node(rgn)); 668 #endif 669 WARN_ON(flags != MEMBLOCK_NONE && flags != rgn->flags); 670 nr_new++; 671 if (insert) { 672 if (start_rgn == -1) 673 start_rgn = idx; 674 end_rgn = idx + 1; 675 memblock_insert_region(type, idx++, base, 676 rbase - base, nid, 677 flags); 678 } 679 } 680 /* area below @rend is dealt with, forget about it */ 681 base = min(rend, end); 682 } 683 684 /* insert the remaining portion */ 685 if (base < end) { 686 nr_new++; 687 if (insert) { 688 if (start_rgn == -1) 689 start_rgn = idx; 690 end_rgn = idx + 1; 691 memblock_insert_region(type, idx, base, end - base, 692 nid, flags); 693 } 694 } 695 696 if (!nr_new) 697 return 0; 698 699 /* 700 * If this was the first round, resize array and repeat for actual 701 * insertions; otherwise, merge and return. 702 */ 703 if (!insert) { 704 while (type->cnt + nr_new > type->max) 705 if (memblock_double_array(type, obase, size) < 0) 706 return -ENOMEM; 707 insert = true; 708 goto repeat; 709 } else { 710 memblock_merge_regions(type, start_rgn, end_rgn); 711 return 0; 712 } 713 } 714 715 /** 716 * memblock_add_node - add new memblock region within a NUMA node 717 * @base: base address of the new region 718 * @size: size of the new region 719 * @nid: nid of the new region 720 * @flags: flags of the new region 721 * 722 * Add new memblock region [@base, @base + @size) to the "memory" 723 * type. See memblock_add_range() description for mode details 724 * 725 * Return: 726 * 0 on success, -errno on failure. 727 */ 728 int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size, 729 int nid, enum memblock_flags flags) 730 { 731 phys_addr_t end = base + size - 1; 732 733 memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__, 734 &base, &end, nid, flags, (void *)_RET_IP_); 735 736 return memblock_add_range(&memblock.memory, base, size, nid, flags); 737 } 738 739 /** 740 * memblock_add - add new memblock region 741 * @base: base address of the new region 742 * @size: size of the new region 743 * 744 * Add new memblock region [@base, @base + @size) to the "memory" 745 * type. See memblock_add_range() description for mode details 746 * 747 * Return: 748 * 0 on success, -errno on failure. 749 */ 750 int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) 751 { 752 phys_addr_t end = base + size - 1; 753 754 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 755 &base, &end, (void *)_RET_IP_); 756 757 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0); 758 } 759 760 /** 761 * memblock_validate_numa_coverage - check if amount of memory with 762 * no node ID assigned is less than a threshold 763 * @threshold_bytes: maximal memory size that can have unassigned node 764 * ID (in bytes). 765 * 766 * A buggy firmware may report memory that does not belong to any node. 767 * Check if amount of such memory is below @threshold_bytes. 768 * 769 * Return: true on success, false on failure. 770 */ 771 bool __init_memblock memblock_validate_numa_coverage(unsigned long threshold_bytes) 772 { 773 unsigned long nr_pages = 0; 774 unsigned long start_pfn, end_pfn, mem_size_mb; 775 int nid, i; 776 777 /* calculate lost page */ 778 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { 779 if (!numa_valid_node(nid)) 780 nr_pages += end_pfn - start_pfn; 781 } 782 783 if ((nr_pages << PAGE_SHIFT) > threshold_bytes) { 784 mem_size_mb = memblock_phys_mem_size() / SZ_1M; 785 pr_err("NUMA: no nodes coverage for %luMB of %luMB RAM\n", 786 (nr_pages << PAGE_SHIFT) / SZ_1M, mem_size_mb); 787 return false; 788 } 789 790 return true; 791 } 792 793 794 /** 795 * memblock_isolate_range - isolate given range into disjoint memblocks 796 * @type: memblock type to isolate range for 797 * @base: base of range to isolate 798 * @size: size of range to isolate 799 * @start_rgn: out parameter for the start of isolated region 800 * @end_rgn: out parameter for the end of isolated region 801 * 802 * Walk @type and ensure that regions don't cross the boundaries defined by 803 * [@base, @base + @size). Crossing regions are split at the boundaries, 804 * which may create at most two more regions. The index of the first 805 * region inside the range is returned in *@start_rgn and the index of the 806 * first region after the range is returned in *@end_rgn. 807 * 808 * Return: 809 * 0 on success, -errno on failure. 810 */ 811 static int __init_memblock memblock_isolate_range(struct memblock_type *type, 812 phys_addr_t base, phys_addr_t size, 813 int *start_rgn, int *end_rgn) 814 { 815 phys_addr_t end = base + memblock_cap_size(base, &size); 816 int idx; 817 struct memblock_region *rgn; 818 819 *start_rgn = *end_rgn = 0; 820 821 if (!size) 822 return 0; 823 824 /* we'll create at most two more regions */ 825 while (type->cnt + 2 > type->max) 826 if (memblock_double_array(type, base, size) < 0) 827 return -ENOMEM; 828 829 for_each_memblock_type(idx, type, rgn) { 830 phys_addr_t rbase = rgn->base; 831 phys_addr_t rend = rbase + rgn->size; 832 833 if (rbase >= end) 834 break; 835 if (rend <= base) 836 continue; 837 838 if (rbase < base) { 839 /* 840 * @rgn intersects from below. Split and continue 841 * to process the next region - the new top half. 842 */ 843 rgn->base = base; 844 rgn->size -= base - rbase; 845 type->total_size -= base - rbase; 846 memblock_insert_region(type, idx, rbase, base - rbase, 847 memblock_get_region_node(rgn), 848 rgn->flags); 849 } else if (rend > end) { 850 /* 851 * @rgn intersects from above. Split and redo the 852 * current region - the new bottom half. 853 */ 854 rgn->base = end; 855 rgn->size -= end - rbase; 856 type->total_size -= end - rbase; 857 memblock_insert_region(type, idx--, rbase, end - rbase, 858 memblock_get_region_node(rgn), 859 rgn->flags); 860 } else { 861 /* @rgn is fully contained, record it */ 862 if (!*end_rgn) 863 *start_rgn = idx; 864 *end_rgn = idx + 1; 865 } 866 } 867 868 return 0; 869 } 870 871 static int __init_memblock memblock_remove_range(struct memblock_type *type, 872 phys_addr_t base, phys_addr_t size) 873 { 874 int start_rgn, end_rgn; 875 int i, ret; 876 877 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 878 if (ret) 879 return ret; 880 881 for (i = end_rgn - 1; i >= start_rgn; i--) 882 memblock_remove_region(type, i); 883 return 0; 884 } 885 886 int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size) 887 { 888 phys_addr_t end = base + size - 1; 889 890 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 891 &base, &end, (void *)_RET_IP_); 892 893 return memblock_remove_range(&memblock.memory, base, size); 894 } 895 896 /** 897 * memblock_free - free boot memory allocation 898 * @ptr: starting address of the boot memory allocation 899 * @size: size of the boot memory block in bytes 900 * 901 * Free boot memory block previously allocated by memblock_alloc_xx() API. 902 * The freeing memory will not be released to the buddy allocator. 903 */ 904 void __init_memblock memblock_free(void *ptr, size_t size) 905 { 906 if (ptr) 907 memblock_phys_free(__pa(ptr), size); 908 } 909 910 /** 911 * memblock_phys_free - free boot memory block 912 * @base: phys starting address of the boot memory block 913 * @size: size of the boot memory block in bytes 914 * 915 * Free boot memory block previously allocated by memblock_phys_alloc_xx() API. 916 * The freeing memory will not be released to the buddy allocator. 917 */ 918 int __init_memblock memblock_phys_free(phys_addr_t base, phys_addr_t size) 919 { 920 phys_addr_t end = base + size - 1; 921 922 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 923 &base, &end, (void *)_RET_IP_); 924 925 kmemleak_free_part_phys(base, size); 926 return memblock_remove_range(&memblock.reserved, base, size); 927 } 928 929 int __init_memblock __memblock_reserve(phys_addr_t base, phys_addr_t size, 930 int nid, enum memblock_flags flags) 931 { 932 phys_addr_t end = base + size - 1; 933 934 memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__, 935 &base, &end, nid, flags, (void *)_RET_IP_); 936 937 return memblock_add_range(&memblock.reserved, base, size, nid, flags); 938 } 939 940 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 941 int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size) 942 { 943 phys_addr_t end = base + size - 1; 944 945 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 946 &base, &end, (void *)_RET_IP_); 947 948 return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0); 949 } 950 #endif 951 952 #ifdef CONFIG_MEMBLOCK_KHO_SCRATCH 953 __init void memblock_set_kho_scratch_only(void) 954 { 955 kho_scratch_only = true; 956 } 957 958 __init void memblock_clear_kho_scratch_only(void) 959 { 960 kho_scratch_only = false; 961 } 962 963 __init void memmap_init_kho_scratch_pages(void) 964 { 965 phys_addr_t start, end; 966 unsigned long pfn; 967 int nid; 968 u64 i; 969 970 if (!IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT)) 971 return; 972 973 /* 974 * Initialize struct pages for free scratch memory. 975 * The struct pages for reserved scratch memory will be set up in 976 * reserve_bootmem_region() 977 */ 978 __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE, 979 MEMBLOCK_KHO_SCRATCH, &start, &end, &nid) { 980 for (pfn = PFN_UP(start); pfn < PFN_DOWN(end); pfn++) 981 init_deferred_page(pfn, nid); 982 } 983 } 984 #endif 985 986 /** 987 * memblock_setclr_flag - set or clear flag for a memory region 988 * @type: memblock type to set/clear flag for 989 * @base: base address of the region 990 * @size: size of the region 991 * @set: set or clear the flag 992 * @flag: the flag to update 993 * 994 * This function isolates region [@base, @base + @size), and sets/clears flag 995 * 996 * Return: 0 on success, -errno on failure. 997 */ 998 static int __init_memblock memblock_setclr_flag(struct memblock_type *type, 999 phys_addr_t base, phys_addr_t size, int set, int flag) 1000 { 1001 int i, ret, start_rgn, end_rgn; 1002 1003 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 1004 if (ret) 1005 return ret; 1006 1007 for (i = start_rgn; i < end_rgn; i++) { 1008 struct memblock_region *r = &type->regions[i]; 1009 1010 if (set) 1011 r->flags |= flag; 1012 else 1013 r->flags &= ~flag; 1014 } 1015 1016 memblock_merge_regions(type, start_rgn, end_rgn); 1017 return 0; 1018 } 1019 1020 /** 1021 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG. 1022 * @base: the base phys addr of the region 1023 * @size: the size of the region 1024 * 1025 * Return: 0 on success, -errno on failure. 1026 */ 1027 int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size) 1028 { 1029 return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_HOTPLUG); 1030 } 1031 1032 /** 1033 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region. 1034 * @base: the base phys addr of the region 1035 * @size: the size of the region 1036 * 1037 * Return: 0 on success, -errno on failure. 1038 */ 1039 int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size) 1040 { 1041 return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_HOTPLUG); 1042 } 1043 1044 /** 1045 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR. 1046 * @base: the base phys addr of the region 1047 * @size: the size of the region 1048 * 1049 * Return: 0 on success, -errno on failure. 1050 */ 1051 int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size) 1052 { 1053 if (!mirrored_kernelcore) 1054 return 0; 1055 1056 system_has_some_mirror = true; 1057 1058 return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_MIRROR); 1059 } 1060 1061 /** 1062 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP. 1063 * @base: the base phys addr of the region 1064 * @size: the size of the region 1065 * 1066 * The memory regions marked with %MEMBLOCK_NOMAP will not be added to the 1067 * direct mapping of the physical memory. These regions will still be 1068 * covered by the memory map. The struct page representing NOMAP memory 1069 * frames in the memory map will be PageReserved() 1070 * 1071 * Note: if the memory being marked %MEMBLOCK_NOMAP was allocated from 1072 * memblock, the caller must inform kmemleak to ignore that memory 1073 * 1074 * Return: 0 on success, -errno on failure. 1075 */ 1076 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size) 1077 { 1078 return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP); 1079 } 1080 1081 /** 1082 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region. 1083 * @base: the base phys addr of the region 1084 * @size: the size of the region 1085 * 1086 * Return: 0 on success, -errno on failure. 1087 */ 1088 int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size) 1089 { 1090 return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_NOMAP); 1091 } 1092 1093 /** 1094 * memblock_reserved_mark_noinit - Mark a reserved memory region with flag 1095 * MEMBLOCK_RSRV_NOINIT 1096 * 1097 * @base: the base phys addr of the region 1098 * @size: the size of the region 1099 * 1100 * The struct pages for the reserved regions marked %MEMBLOCK_RSRV_NOINIT will 1101 * not be fully initialized to allow the caller optimize their initialization. 1102 * 1103 * When %CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, setting this flag 1104 * completely bypasses the initialization of struct pages for such region. 1105 * 1106 * When %CONFIG_DEFERRED_STRUCT_PAGE_INIT is disabled, struct pages in this 1107 * region will be initialized with default values but won't be marked as 1108 * reserved. 1109 * 1110 * Return: 0 on success, -errno on failure. 1111 */ 1112 int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size) 1113 { 1114 return memblock_setclr_flag(&memblock.reserved, base, size, 1, 1115 MEMBLOCK_RSRV_NOINIT); 1116 } 1117 1118 /** 1119 * memblock_reserved_mark_kern - Mark a reserved memory region with flag 1120 * MEMBLOCK_RSRV_KERN 1121 * 1122 * @base: the base phys addr of the region 1123 * @size: the size of the region 1124 * 1125 * Return: 0 on success, -errno on failure. 1126 */ 1127 int __init_memblock memblock_reserved_mark_kern(phys_addr_t base, phys_addr_t size) 1128 { 1129 return memblock_setclr_flag(&memblock.reserved, base, size, 1, 1130 MEMBLOCK_RSRV_KERN); 1131 } 1132 1133 /** 1134 * memblock_mark_kho_scratch - Mark a memory region as MEMBLOCK_KHO_SCRATCH. 1135 * @base: the base phys addr of the region 1136 * @size: the size of the region 1137 * 1138 * Only memory regions marked with %MEMBLOCK_KHO_SCRATCH will be considered 1139 * for allocations during early boot with kexec handover. 1140 * 1141 * Return: 0 on success, -errno on failure. 1142 */ 1143 __init int memblock_mark_kho_scratch(phys_addr_t base, phys_addr_t size) 1144 { 1145 return memblock_setclr_flag(&memblock.memory, base, size, 1, 1146 MEMBLOCK_KHO_SCRATCH); 1147 } 1148 1149 /** 1150 * memblock_clear_kho_scratch - Clear MEMBLOCK_KHO_SCRATCH flag for a 1151 * specified region. 1152 * @base: the base phys addr of the region 1153 * @size: the size of the region 1154 * 1155 * Return: 0 on success, -errno on failure. 1156 */ 1157 __init int memblock_clear_kho_scratch(phys_addr_t base, phys_addr_t size) 1158 { 1159 return memblock_setclr_flag(&memblock.memory, base, size, 0, 1160 MEMBLOCK_KHO_SCRATCH); 1161 } 1162 1163 static bool should_skip_region(struct memblock_type *type, 1164 struct memblock_region *m, 1165 int nid, int flags) 1166 { 1167 int m_nid = memblock_get_region_node(m); 1168 1169 /* we never skip regions when iterating memblock.reserved or physmem */ 1170 if (type != memblock_memory) 1171 return false; 1172 1173 /* only memory regions are associated with nodes, check it */ 1174 if (numa_valid_node(nid) && nid != m_nid) 1175 return true; 1176 1177 /* skip hotpluggable memory regions if needed */ 1178 if (movable_node_is_enabled() && memblock_is_hotpluggable(m) && 1179 !(flags & MEMBLOCK_HOTPLUG)) 1180 return true; 1181 1182 /* if we want mirror memory skip non-mirror memory regions */ 1183 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) 1184 return true; 1185 1186 /* skip nomap memory unless we were asked for it explicitly */ 1187 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) 1188 return true; 1189 1190 /* skip driver-managed memory unless we were asked for it explicitly */ 1191 if (!(flags & MEMBLOCK_DRIVER_MANAGED) && memblock_is_driver_managed(m)) 1192 return true; 1193 1194 /* 1195 * In early alloc during kexec handover, we can only consider 1196 * MEMBLOCK_KHO_SCRATCH regions for the allocations 1197 */ 1198 if ((flags & MEMBLOCK_KHO_SCRATCH) && !memblock_is_kho_scratch(m)) 1199 return true; 1200 1201 return false; 1202 } 1203 1204 /** 1205 * __next_mem_range - next function for for_each_free_mem_range() etc. 1206 * @idx: pointer to u64 loop variable 1207 * @nid: node selector, %NUMA_NO_NODE for all nodes 1208 * @flags: pick from blocks based on memory attributes 1209 * @type_a: pointer to memblock_type from where the range is taken 1210 * @type_b: pointer to memblock_type which excludes memory from being taken 1211 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 1212 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 1213 * @out_nid: ptr to int for nid of the range, can be %NULL 1214 * 1215 * Find the first area from *@idx which matches @nid, fill the out 1216 * parameters, and update *@idx for the next iteration. The lower 32bit of 1217 * *@idx contains index into type_a and the upper 32bit indexes the 1218 * areas before each region in type_b. For example, if type_b regions 1219 * look like the following, 1220 * 1221 * 0:[0-16), 1:[32-48), 2:[128-130) 1222 * 1223 * The upper 32bit indexes the following regions. 1224 * 1225 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) 1226 * 1227 * As both region arrays are sorted, the function advances the two indices 1228 * in lockstep and returns each intersection. 1229 */ 1230 void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags, 1231 struct memblock_type *type_a, 1232 struct memblock_type *type_b, phys_addr_t *out_start, 1233 phys_addr_t *out_end, int *out_nid) 1234 { 1235 int idx_a = *idx & 0xffffffff; 1236 int idx_b = *idx >> 32; 1237 1238 for (; idx_a < type_a->cnt; idx_a++) { 1239 struct memblock_region *m = &type_a->regions[idx_a]; 1240 1241 phys_addr_t m_start = m->base; 1242 phys_addr_t m_end = m->base + m->size; 1243 int m_nid = memblock_get_region_node(m); 1244 1245 if (should_skip_region(type_a, m, nid, flags)) 1246 continue; 1247 1248 if (!type_b) { 1249 if (out_start) 1250 *out_start = m_start; 1251 if (out_end) 1252 *out_end = m_end; 1253 if (out_nid) 1254 *out_nid = m_nid; 1255 idx_a++; 1256 *idx = (u32)idx_a | (u64)idx_b << 32; 1257 return; 1258 } 1259 1260 /* scan areas before each reservation */ 1261 for (; idx_b < type_b->cnt + 1; idx_b++) { 1262 struct memblock_region *r; 1263 phys_addr_t r_start; 1264 phys_addr_t r_end; 1265 1266 r = &type_b->regions[idx_b]; 1267 r_start = idx_b ? r[-1].base + r[-1].size : 0; 1268 r_end = idx_b < type_b->cnt ? 1269 r->base : PHYS_ADDR_MAX; 1270 1271 /* 1272 * if idx_b advanced past idx_a, 1273 * break out to advance idx_a 1274 */ 1275 if (r_start >= m_end) 1276 break; 1277 /* if the two regions intersect, we're done */ 1278 if (m_start < r_end) { 1279 if (out_start) 1280 *out_start = 1281 max(m_start, r_start); 1282 if (out_end) 1283 *out_end = min(m_end, r_end); 1284 if (out_nid) 1285 *out_nid = m_nid; 1286 /* 1287 * The region which ends first is 1288 * advanced for the next iteration. 1289 */ 1290 if (m_end <= r_end) 1291 idx_a++; 1292 else 1293 idx_b++; 1294 *idx = (u32)idx_a | (u64)idx_b << 32; 1295 return; 1296 } 1297 } 1298 } 1299 1300 /* signal end of iteration */ 1301 *idx = ULLONG_MAX; 1302 } 1303 1304 /** 1305 * __next_mem_range_rev - generic next function for for_each_*_range_rev() 1306 * 1307 * @idx: pointer to u64 loop variable 1308 * @nid: node selector, %NUMA_NO_NODE for all nodes 1309 * @flags: pick from blocks based on memory attributes 1310 * @type_a: pointer to memblock_type from where the range is taken 1311 * @type_b: pointer to memblock_type which excludes memory from being taken 1312 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 1313 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 1314 * @out_nid: ptr to int for nid of the range, can be %NULL 1315 * 1316 * Finds the next range from type_a which is not marked as unsuitable 1317 * in type_b. 1318 * 1319 * Reverse of __next_mem_range(). 1320 */ 1321 void __init_memblock __next_mem_range_rev(u64 *idx, int nid, 1322 enum memblock_flags flags, 1323 struct memblock_type *type_a, 1324 struct memblock_type *type_b, 1325 phys_addr_t *out_start, 1326 phys_addr_t *out_end, int *out_nid) 1327 { 1328 int idx_a = *idx & 0xffffffff; 1329 int idx_b = *idx >> 32; 1330 1331 if (*idx == (u64)ULLONG_MAX) { 1332 idx_a = type_a->cnt - 1; 1333 if (type_b != NULL) 1334 idx_b = type_b->cnt; 1335 else 1336 idx_b = 0; 1337 } 1338 1339 for (; idx_a >= 0; idx_a--) { 1340 struct memblock_region *m = &type_a->regions[idx_a]; 1341 1342 phys_addr_t m_start = m->base; 1343 phys_addr_t m_end = m->base + m->size; 1344 int m_nid = memblock_get_region_node(m); 1345 1346 if (should_skip_region(type_a, m, nid, flags)) 1347 continue; 1348 1349 if (!type_b) { 1350 if (out_start) 1351 *out_start = m_start; 1352 if (out_end) 1353 *out_end = m_end; 1354 if (out_nid) 1355 *out_nid = m_nid; 1356 idx_a--; 1357 *idx = (u32)idx_a | (u64)idx_b << 32; 1358 return; 1359 } 1360 1361 /* scan areas before each reservation */ 1362 for (; idx_b >= 0; idx_b--) { 1363 struct memblock_region *r; 1364 phys_addr_t r_start; 1365 phys_addr_t r_end; 1366 1367 r = &type_b->regions[idx_b]; 1368 r_start = idx_b ? r[-1].base + r[-1].size : 0; 1369 r_end = idx_b < type_b->cnt ? 1370 r->base : PHYS_ADDR_MAX; 1371 /* 1372 * if idx_b advanced past idx_a, 1373 * break out to advance idx_a 1374 */ 1375 1376 if (r_end <= m_start) 1377 break; 1378 /* if the two regions intersect, we're done */ 1379 if (m_end > r_start) { 1380 if (out_start) 1381 *out_start = max(m_start, r_start); 1382 if (out_end) 1383 *out_end = min(m_end, r_end); 1384 if (out_nid) 1385 *out_nid = m_nid; 1386 if (m_start >= r_start) 1387 idx_a--; 1388 else 1389 idx_b--; 1390 *idx = (u32)idx_a | (u64)idx_b << 32; 1391 return; 1392 } 1393 } 1394 } 1395 /* signal end of iteration */ 1396 *idx = ULLONG_MAX; 1397 } 1398 1399 /* 1400 * Common iterator interface used to define for_each_mem_pfn_range(). 1401 */ 1402 void __init_memblock __next_mem_pfn_range(int *idx, int nid, 1403 unsigned long *out_start_pfn, 1404 unsigned long *out_end_pfn, int *out_nid) 1405 { 1406 struct memblock_type *type = &memblock.memory; 1407 struct memblock_region *r; 1408 int r_nid; 1409 1410 while (++*idx < type->cnt) { 1411 r = &type->regions[*idx]; 1412 r_nid = memblock_get_region_node(r); 1413 1414 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) 1415 continue; 1416 if (!numa_valid_node(nid) || nid == r_nid) 1417 break; 1418 } 1419 if (*idx >= type->cnt) { 1420 *idx = -1; 1421 return; 1422 } 1423 1424 if (out_start_pfn) 1425 *out_start_pfn = PFN_UP(r->base); 1426 if (out_end_pfn) 1427 *out_end_pfn = PFN_DOWN(r->base + r->size); 1428 if (out_nid) 1429 *out_nid = r_nid; 1430 } 1431 1432 /** 1433 * memblock_set_node - set node ID on memblock regions 1434 * @base: base of area to set node ID for 1435 * @size: size of area to set node ID for 1436 * @type: memblock type to set node ID for 1437 * @nid: node ID to set 1438 * 1439 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid. 1440 * Regions which cross the area boundaries are split as necessary. 1441 * 1442 * Return: 1443 * 0 on success, -errno on failure. 1444 */ 1445 int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, 1446 struct memblock_type *type, int nid) 1447 { 1448 #ifdef CONFIG_NUMA 1449 int start_rgn, end_rgn; 1450 int i, ret; 1451 1452 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 1453 if (ret) 1454 return ret; 1455 1456 for (i = start_rgn; i < end_rgn; i++) 1457 memblock_set_region_node(&type->regions[i], nid); 1458 1459 memblock_merge_regions(type, start_rgn, end_rgn); 1460 #endif 1461 return 0; 1462 } 1463 1464 /** 1465 * memblock_alloc_range_nid - allocate boot memory block 1466 * @size: size of memory block to be allocated in bytes 1467 * @align: alignment of the region and block's size 1468 * @start: the lower bound of the memory region to allocate (phys address) 1469 * @end: the upper bound of the memory region to allocate (phys address) 1470 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1471 * @exact_nid: control the allocation fall back to other nodes 1472 * 1473 * The allocation is performed from memory region limited by 1474 * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE. 1475 * 1476 * If the specified node can not hold the requested memory and @exact_nid 1477 * is false, the allocation falls back to any node in the system. 1478 * 1479 * For systems with memory mirroring, the allocation is attempted first 1480 * from the regions with mirroring enabled and then retried from any 1481 * memory region. 1482 * 1483 * In addition, function using kmemleak_alloc_phys for allocated boot 1484 * memory block, it is never reported as leaks. 1485 * 1486 * Return: 1487 * Physical address of allocated memory block on success, %0 on failure. 1488 */ 1489 phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, 1490 phys_addr_t align, phys_addr_t start, 1491 phys_addr_t end, int nid, 1492 bool exact_nid) 1493 { 1494 enum memblock_flags flags = choose_memblock_flags(); 1495 phys_addr_t found; 1496 1497 /* 1498 * Detect any accidental use of these APIs after slab is ready, as at 1499 * this moment memblock may be deinitialized already and its 1500 * internal data may be destroyed (after execution of memblock_free_all) 1501 */ 1502 if (WARN_ON_ONCE(slab_is_available())) { 1503 void *vaddr = kzalloc_node(size, GFP_NOWAIT, nid); 1504 1505 return vaddr ? virt_to_phys(vaddr) : 0; 1506 } 1507 1508 if (!align) { 1509 /* Can't use WARNs this early in boot on powerpc */ 1510 dump_stack(); 1511 align = SMP_CACHE_BYTES; 1512 } 1513 1514 again: 1515 found = memblock_find_in_range_node(size, align, start, end, nid, 1516 flags); 1517 if (found && !__memblock_reserve(found, size, nid, MEMBLOCK_RSRV_KERN)) 1518 goto done; 1519 1520 if (numa_valid_node(nid) && !exact_nid) { 1521 found = memblock_find_in_range_node(size, align, start, 1522 end, NUMA_NO_NODE, 1523 flags); 1524 if (found && !memblock_reserve_kern(found, size)) 1525 goto done; 1526 } 1527 1528 if (flags & MEMBLOCK_MIRROR) { 1529 flags &= ~MEMBLOCK_MIRROR; 1530 pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n", 1531 &size); 1532 goto again; 1533 } 1534 1535 return 0; 1536 1537 done: 1538 /* 1539 * Skip kmemleak for those places like kasan_init() and 1540 * early_pgtable_alloc() due to high volume. 1541 */ 1542 if (end != MEMBLOCK_ALLOC_NOLEAKTRACE) 1543 /* 1544 * Memblock allocated blocks are never reported as 1545 * leaks. This is because many of these blocks are 1546 * only referred via the physical address which is 1547 * not looked up by kmemleak. 1548 */ 1549 kmemleak_alloc_phys(found, size, 0); 1550 1551 /* 1552 * Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP, 1553 * require memory to be accepted before it can be used by the 1554 * guest. 1555 * 1556 * Accept the memory of the allocated buffer. 1557 */ 1558 accept_memory(found, size); 1559 1560 return found; 1561 } 1562 1563 /** 1564 * memblock_phys_alloc_range - allocate a memory block inside specified range 1565 * @size: size of memory block to be allocated in bytes 1566 * @align: alignment of the region and block's size 1567 * @start: the lower bound of the memory region to allocate (physical address) 1568 * @end: the upper bound of the memory region to allocate (physical address) 1569 * 1570 * Allocate @size bytes in the between @start and @end. 1571 * 1572 * Return: physical address of the allocated memory block on success, 1573 * %0 on failure. 1574 */ 1575 phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size, 1576 phys_addr_t align, 1577 phys_addr_t start, 1578 phys_addr_t end) 1579 { 1580 memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n", 1581 __func__, (u64)size, (u64)align, &start, &end, 1582 (void *)_RET_IP_); 1583 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE, 1584 false); 1585 } 1586 1587 /** 1588 * memblock_phys_alloc_try_nid - allocate a memory block from specified NUMA node 1589 * @size: size of memory block to be allocated in bytes 1590 * @align: alignment of the region and block's size 1591 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1592 * 1593 * Allocates memory block from the specified NUMA node. If the node 1594 * has no available memory, attempts to allocated from any node in the 1595 * system. 1596 * 1597 * Return: physical address of the allocated memory block on success, 1598 * %0 on failure. 1599 */ 1600 phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) 1601 { 1602 return memblock_alloc_range_nid(size, align, 0, 1603 MEMBLOCK_ALLOC_ACCESSIBLE, nid, false); 1604 } 1605 1606 /** 1607 * memblock_alloc_internal - allocate boot memory block 1608 * @size: size of memory block to be allocated in bytes 1609 * @align: alignment of the region and block's size 1610 * @min_addr: the lower bound of the memory region to allocate (phys address) 1611 * @max_addr: the upper bound of the memory region to allocate (phys address) 1612 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1613 * @exact_nid: control the allocation fall back to other nodes 1614 * 1615 * Allocates memory block using memblock_alloc_range_nid() and 1616 * converts the returned physical address to virtual. 1617 * 1618 * The @min_addr limit is dropped if it can not be satisfied and the allocation 1619 * will fall back to memory below @min_addr. Other constraints, such 1620 * as node and mirrored memory will be handled again in 1621 * memblock_alloc_range_nid(). 1622 * 1623 * Return: 1624 * Virtual address of allocated memory block on success, NULL on failure. 1625 */ 1626 static void * __init memblock_alloc_internal( 1627 phys_addr_t size, phys_addr_t align, 1628 phys_addr_t min_addr, phys_addr_t max_addr, 1629 int nid, bool exact_nid) 1630 { 1631 phys_addr_t alloc; 1632 1633 1634 if (max_addr > memblock.current_limit) 1635 max_addr = memblock.current_limit; 1636 1637 alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid, 1638 exact_nid); 1639 1640 /* retry allocation without lower limit */ 1641 if (!alloc && min_addr) 1642 alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid, 1643 exact_nid); 1644 1645 if (!alloc) 1646 return NULL; 1647 1648 return phys_to_virt(alloc); 1649 } 1650 1651 /** 1652 * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node 1653 * without zeroing memory 1654 * @size: size of memory block to be allocated in bytes 1655 * @align: alignment of the region and block's size 1656 * @min_addr: the lower bound of the memory region from where the allocation 1657 * is preferred (phys address) 1658 * @max_addr: the upper bound of the memory region from where the allocation 1659 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 1660 * allocate only from memory limited by memblock.current_limit value 1661 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1662 * 1663 * Public function, provides additional debug information (including caller 1664 * info), if enabled. Does not zero allocated memory. 1665 * 1666 * Return: 1667 * Virtual address of allocated memory block on success, NULL on failure. 1668 */ 1669 void * __init memblock_alloc_exact_nid_raw( 1670 phys_addr_t size, phys_addr_t align, 1671 phys_addr_t min_addr, phys_addr_t max_addr, 1672 int nid) 1673 { 1674 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1675 __func__, (u64)size, (u64)align, nid, &min_addr, 1676 &max_addr, (void *)_RET_IP_); 1677 1678 return memblock_alloc_internal(size, align, min_addr, max_addr, nid, 1679 true); 1680 } 1681 1682 /** 1683 * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing 1684 * memory and without panicking 1685 * @size: size of memory block to be allocated in bytes 1686 * @align: alignment of the region and block's size 1687 * @min_addr: the lower bound of the memory region from where the allocation 1688 * is preferred (phys address) 1689 * @max_addr: the upper bound of the memory region from where the allocation 1690 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 1691 * allocate only from memory limited by memblock.current_limit value 1692 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1693 * 1694 * Public function, provides additional debug information (including caller 1695 * info), if enabled. Does not zero allocated memory, does not panic if request 1696 * cannot be satisfied. 1697 * 1698 * Return: 1699 * Virtual address of allocated memory block on success, NULL on failure. 1700 */ 1701 void * __init memblock_alloc_try_nid_raw( 1702 phys_addr_t size, phys_addr_t align, 1703 phys_addr_t min_addr, phys_addr_t max_addr, 1704 int nid) 1705 { 1706 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1707 __func__, (u64)size, (u64)align, nid, &min_addr, 1708 &max_addr, (void *)_RET_IP_); 1709 1710 return memblock_alloc_internal(size, align, min_addr, max_addr, nid, 1711 false); 1712 } 1713 1714 /** 1715 * memblock_alloc_try_nid - allocate boot memory block 1716 * @size: size of memory block to be allocated in bytes 1717 * @align: alignment of the region and block's size 1718 * @min_addr: the lower bound of the memory region from where the allocation 1719 * is preferred (phys address) 1720 * @max_addr: the upper bound of the memory region from where the allocation 1721 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 1722 * allocate only from memory limited by memblock.current_limit value 1723 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1724 * 1725 * Public function, provides additional debug information (including caller 1726 * info), if enabled. This function zeroes the allocated memory. 1727 * 1728 * Return: 1729 * Virtual address of allocated memory block on success, NULL on failure. 1730 */ 1731 void * __init memblock_alloc_try_nid( 1732 phys_addr_t size, phys_addr_t align, 1733 phys_addr_t min_addr, phys_addr_t max_addr, 1734 int nid) 1735 { 1736 void *ptr; 1737 1738 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1739 __func__, (u64)size, (u64)align, nid, &min_addr, 1740 &max_addr, (void *)_RET_IP_); 1741 ptr = memblock_alloc_internal(size, align, 1742 min_addr, max_addr, nid, false); 1743 if (ptr) 1744 memset(ptr, 0, size); 1745 1746 return ptr; 1747 } 1748 1749 /** 1750 * __memblock_alloc_or_panic - Try to allocate memory and panic on failure 1751 * @size: size of memory block to be allocated in bytes 1752 * @align: alignment of the region and block's size 1753 * @func: caller func name 1754 * 1755 * This function attempts to allocate memory using memblock_alloc, 1756 * and in case of failure, it calls panic with the formatted message. 1757 * This function should not be used directly, please use the macro memblock_alloc_or_panic. 1758 */ 1759 void *__init __memblock_alloc_or_panic(phys_addr_t size, phys_addr_t align, 1760 const char *func) 1761 { 1762 void *addr = memblock_alloc(size, align); 1763 1764 if (unlikely(!addr)) 1765 panic("%s: Failed to allocate %pap bytes\n", func, &size); 1766 return addr; 1767 } 1768 1769 /** 1770 * memblock_free_late - free pages directly to buddy allocator 1771 * @base: phys starting address of the boot memory block 1772 * @size: size of the boot memory block in bytes 1773 * 1774 * This is only useful when the memblock allocator has already been torn 1775 * down, but we are still initializing the system. Pages are released directly 1776 * to the buddy allocator. 1777 */ 1778 void __init memblock_free_late(phys_addr_t base, phys_addr_t size) 1779 { 1780 phys_addr_t cursor, end; 1781 1782 end = base + size - 1; 1783 memblock_dbg("%s: [%pa-%pa] %pS\n", 1784 __func__, &base, &end, (void *)_RET_IP_); 1785 kmemleak_free_part_phys(base, size); 1786 cursor = PFN_UP(base); 1787 end = PFN_DOWN(base + size); 1788 1789 for (; cursor < end; cursor++) { 1790 memblock_free_pages(cursor, 0); 1791 totalram_pages_inc(); 1792 } 1793 } 1794 1795 /* 1796 * Remaining API functions 1797 */ 1798 1799 phys_addr_t __init_memblock memblock_phys_mem_size(void) 1800 { 1801 return memblock.memory.total_size; 1802 } 1803 1804 phys_addr_t __init_memblock memblock_reserved_size(void) 1805 { 1806 return memblock.reserved.total_size; 1807 } 1808 1809 phys_addr_t __init_memblock memblock_reserved_kern_size(phys_addr_t limit, int nid) 1810 { 1811 struct memblock_region *r; 1812 phys_addr_t total = 0; 1813 1814 for_each_reserved_mem_region(r) { 1815 phys_addr_t size = r->size; 1816 1817 if (r->base > limit) 1818 break; 1819 1820 if (r->base + r->size > limit) 1821 size = limit - r->base; 1822 1823 if (nid == memblock_get_region_node(r) || !numa_valid_node(nid)) 1824 if (r->flags & MEMBLOCK_RSRV_KERN) 1825 total += size; 1826 } 1827 1828 return total; 1829 } 1830 1831 /** 1832 * memblock_estimated_nr_free_pages - return estimated number of free pages 1833 * from memblock point of view 1834 * 1835 * During bootup, subsystems might need a rough estimate of the number of free 1836 * pages in the whole system, before precise numbers are available from the 1837 * buddy. Especially with CONFIG_DEFERRED_STRUCT_PAGE_INIT, the numbers 1838 * obtained from the buddy might be very imprecise during bootup. 1839 * 1840 * Return: 1841 * An estimated number of free pages from memblock point of view. 1842 */ 1843 unsigned long __init memblock_estimated_nr_free_pages(void) 1844 { 1845 return PHYS_PFN(memblock_phys_mem_size() - 1846 memblock_reserved_kern_size(MEMBLOCK_ALLOC_ANYWHERE, NUMA_NO_NODE)); 1847 } 1848 1849 /* lowest address */ 1850 phys_addr_t __init_memblock memblock_start_of_DRAM(void) 1851 { 1852 return memblock.memory.regions[0].base; 1853 } 1854 1855 phys_addr_t __init_memblock memblock_end_of_DRAM(void) 1856 { 1857 int idx = memblock.memory.cnt - 1; 1858 1859 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); 1860 } 1861 1862 static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit) 1863 { 1864 phys_addr_t max_addr = PHYS_ADDR_MAX; 1865 struct memblock_region *r; 1866 1867 /* 1868 * translate the memory @limit size into the max address within one of 1869 * the memory memblock regions, if the @limit exceeds the total size 1870 * of those regions, max_addr will keep original value PHYS_ADDR_MAX 1871 */ 1872 for_each_mem_region(r) { 1873 if (limit <= r->size) { 1874 max_addr = r->base + limit; 1875 break; 1876 } 1877 limit -= r->size; 1878 } 1879 1880 return max_addr; 1881 } 1882 1883 void __init memblock_enforce_memory_limit(phys_addr_t limit) 1884 { 1885 phys_addr_t max_addr; 1886 1887 if (!limit) 1888 return; 1889 1890 max_addr = __find_max_addr(limit); 1891 1892 /* @limit exceeds the total size of the memory, do nothing */ 1893 if (max_addr == PHYS_ADDR_MAX) 1894 return; 1895 1896 /* truncate both memory and reserved regions */ 1897 memblock_remove_range(&memblock.memory, max_addr, 1898 PHYS_ADDR_MAX); 1899 memblock_remove_range(&memblock.reserved, max_addr, 1900 PHYS_ADDR_MAX); 1901 } 1902 1903 void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size) 1904 { 1905 int start_rgn, end_rgn; 1906 int i, ret; 1907 1908 if (!size) 1909 return; 1910 1911 if (!memblock_memory->total_size) { 1912 pr_warn("%s: No memory registered yet\n", __func__); 1913 return; 1914 } 1915 1916 ret = memblock_isolate_range(&memblock.memory, base, size, 1917 &start_rgn, &end_rgn); 1918 if (ret) 1919 return; 1920 1921 /* remove all the MAP regions */ 1922 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--) 1923 if (!memblock_is_nomap(&memblock.memory.regions[i])) 1924 memblock_remove_region(&memblock.memory, i); 1925 1926 for (i = start_rgn - 1; i >= 0; i--) 1927 if (!memblock_is_nomap(&memblock.memory.regions[i])) 1928 memblock_remove_region(&memblock.memory, i); 1929 1930 /* truncate the reserved regions */ 1931 memblock_remove_range(&memblock.reserved, 0, base); 1932 memblock_remove_range(&memblock.reserved, 1933 base + size, PHYS_ADDR_MAX); 1934 } 1935 1936 void __init memblock_mem_limit_remove_map(phys_addr_t limit) 1937 { 1938 phys_addr_t max_addr; 1939 1940 if (!limit) 1941 return; 1942 1943 max_addr = __find_max_addr(limit); 1944 1945 /* @limit exceeds the total size of the memory, do nothing */ 1946 if (max_addr == PHYS_ADDR_MAX) 1947 return; 1948 1949 memblock_cap_memory_range(0, max_addr); 1950 } 1951 1952 static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr) 1953 { 1954 unsigned int left = 0, right = type->cnt; 1955 1956 do { 1957 unsigned int mid = (right + left) / 2; 1958 1959 if (addr < type->regions[mid].base) 1960 right = mid; 1961 else if (addr >= (type->regions[mid].base + 1962 type->regions[mid].size)) 1963 left = mid + 1; 1964 else 1965 return mid; 1966 } while (left < right); 1967 return -1; 1968 } 1969 1970 bool __init_memblock memblock_is_reserved(phys_addr_t addr) 1971 { 1972 return memblock_search(&memblock.reserved, addr) != -1; 1973 } 1974 1975 bool __init_memblock memblock_is_memory(phys_addr_t addr) 1976 { 1977 return memblock_search(&memblock.memory, addr) != -1; 1978 } 1979 1980 bool __init_memblock memblock_is_map_memory(phys_addr_t addr) 1981 { 1982 int i = memblock_search(&memblock.memory, addr); 1983 1984 if (i == -1) 1985 return false; 1986 return !memblock_is_nomap(&memblock.memory.regions[i]); 1987 } 1988 1989 int __init_memblock memblock_search_pfn_nid(unsigned long pfn, 1990 unsigned long *start_pfn, unsigned long *end_pfn) 1991 { 1992 struct memblock_type *type = &memblock.memory; 1993 int mid = memblock_search(type, PFN_PHYS(pfn)); 1994 1995 if (mid == -1) 1996 return NUMA_NO_NODE; 1997 1998 *start_pfn = PFN_DOWN(type->regions[mid].base); 1999 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size); 2000 2001 return memblock_get_region_node(&type->regions[mid]); 2002 } 2003 2004 /** 2005 * memblock_is_region_memory - check if a region is a subset of memory 2006 * @base: base of region to check 2007 * @size: size of region to check 2008 * 2009 * Check if the region [@base, @base + @size) is a subset of a memory block. 2010 * 2011 * Return: 2012 * 0 if false, non-zero if true 2013 */ 2014 bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size) 2015 { 2016 int idx = memblock_search(&memblock.memory, base); 2017 phys_addr_t end = base + memblock_cap_size(base, &size); 2018 2019 if (idx == -1) 2020 return false; 2021 return (memblock.memory.regions[idx].base + 2022 memblock.memory.regions[idx].size) >= end; 2023 } 2024 2025 /** 2026 * memblock_is_region_reserved - check if a region intersects reserved memory 2027 * @base: base of region to check 2028 * @size: size of region to check 2029 * 2030 * Check if the region [@base, @base + @size) intersects a reserved 2031 * memory block. 2032 * 2033 * Return: 2034 * True if they intersect, false if not. 2035 */ 2036 bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) 2037 { 2038 return memblock_overlaps_region(&memblock.reserved, base, size); 2039 } 2040 2041 void __init_memblock memblock_trim_memory(phys_addr_t align) 2042 { 2043 phys_addr_t start, end, orig_start, orig_end; 2044 struct memblock_region *r; 2045 2046 for_each_mem_region(r) { 2047 orig_start = r->base; 2048 orig_end = r->base + r->size; 2049 start = round_up(orig_start, align); 2050 end = round_down(orig_end, align); 2051 2052 if (start == orig_start && end == orig_end) 2053 continue; 2054 2055 if (start < end) { 2056 r->base = start; 2057 r->size = end - start; 2058 } else { 2059 memblock_remove_region(&memblock.memory, 2060 r - memblock.memory.regions); 2061 r--; 2062 } 2063 } 2064 } 2065 2066 void __init_memblock memblock_set_current_limit(phys_addr_t limit) 2067 { 2068 memblock.current_limit = limit; 2069 } 2070 2071 phys_addr_t __init_memblock memblock_get_current_limit(void) 2072 { 2073 return memblock.current_limit; 2074 } 2075 2076 static void __init_memblock memblock_dump(struct memblock_type *type) 2077 { 2078 phys_addr_t base, end, size; 2079 enum memblock_flags flags; 2080 int idx; 2081 struct memblock_region *rgn; 2082 2083 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt); 2084 2085 for_each_memblock_type(idx, type, rgn) { 2086 char nid_buf[32] = ""; 2087 2088 base = rgn->base; 2089 size = rgn->size; 2090 end = base + size - 1; 2091 flags = rgn->flags; 2092 #ifdef CONFIG_NUMA 2093 if (numa_valid_node(memblock_get_region_node(rgn))) 2094 snprintf(nid_buf, sizeof(nid_buf), " on node %d", 2095 memblock_get_region_node(rgn)); 2096 #endif 2097 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n", 2098 type->name, idx, &base, &end, &size, nid_buf, flags); 2099 } 2100 } 2101 2102 static void __init_memblock __memblock_dump_all(void) 2103 { 2104 pr_info("MEMBLOCK configuration:\n"); 2105 pr_info(" memory size = %pa reserved size = %pa\n", 2106 &memblock.memory.total_size, 2107 &memblock.reserved.total_size); 2108 2109 memblock_dump(&memblock.memory); 2110 memblock_dump(&memblock.reserved); 2111 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 2112 memblock_dump(&physmem); 2113 #endif 2114 } 2115 2116 void __init_memblock memblock_dump_all(void) 2117 { 2118 if (memblock_debug) 2119 __memblock_dump_all(); 2120 } 2121 2122 void __init memblock_allow_resize(void) 2123 { 2124 memblock_can_resize = 1; 2125 } 2126 2127 static int __init early_memblock(char *p) 2128 { 2129 if (p && strstr(p, "debug")) 2130 memblock_debug = 1; 2131 return 0; 2132 } 2133 early_param("memblock", early_memblock); 2134 2135 static void __init free_memmap(unsigned long start_pfn, unsigned long end_pfn) 2136 { 2137 struct page *start_pg, *end_pg; 2138 phys_addr_t pg, pgend; 2139 2140 /* 2141 * Convert start_pfn/end_pfn to a struct page pointer. 2142 */ 2143 start_pg = pfn_to_page(start_pfn - 1) + 1; 2144 end_pg = pfn_to_page(end_pfn - 1) + 1; 2145 2146 /* 2147 * Convert to physical addresses, and round start upwards and end 2148 * downwards. 2149 */ 2150 pg = PAGE_ALIGN(__pa(start_pg)); 2151 pgend = PAGE_ALIGN_DOWN(__pa(end_pg)); 2152 2153 /* 2154 * If there are free pages between these, free the section of the 2155 * memmap array. 2156 */ 2157 if (pg < pgend) 2158 memblock_phys_free(pg, pgend - pg); 2159 } 2160 2161 /* 2162 * The mem_map array can get very big. Free the unused area of the memory map. 2163 */ 2164 static void __init free_unused_memmap(void) 2165 { 2166 unsigned long start, end, prev_end = 0; 2167 int i; 2168 2169 if (!IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) || 2170 IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) 2171 return; 2172 2173 /* 2174 * This relies on each bank being in address order. 2175 * The banks are sorted previously in bootmem_init(). 2176 */ 2177 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) { 2178 #ifdef CONFIG_SPARSEMEM 2179 /* 2180 * Take care not to free memmap entries that don't exist 2181 * due to SPARSEMEM sections which aren't present. 2182 */ 2183 start = min(start, ALIGN(prev_end, PAGES_PER_SECTION)); 2184 #endif 2185 /* 2186 * Align down here since many operations in VM subsystem 2187 * presume that there are no holes in the memory map inside 2188 * a pageblock 2189 */ 2190 start = pageblock_start_pfn(start); 2191 2192 /* 2193 * If we had a previous bank, and there is a space 2194 * between the current bank and the previous, free it. 2195 */ 2196 if (prev_end && prev_end < start) 2197 free_memmap(prev_end, start); 2198 2199 /* 2200 * Align up here since many operations in VM subsystem 2201 * presume that there are no holes in the memory map inside 2202 * a pageblock 2203 */ 2204 prev_end = pageblock_align(end); 2205 } 2206 2207 #ifdef CONFIG_SPARSEMEM 2208 if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) { 2209 prev_end = pageblock_align(end); 2210 free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION)); 2211 } 2212 #endif 2213 } 2214 2215 static void __init __free_pages_memory(unsigned long start, unsigned long end) 2216 { 2217 int order; 2218 2219 while (start < end) { 2220 /* 2221 * Free the pages in the largest chunks alignment allows. 2222 * 2223 * __ffs() behaviour is undefined for 0. start == 0 is 2224 * MAX_PAGE_ORDER-aligned, set order to MAX_PAGE_ORDER for 2225 * the case. 2226 */ 2227 if (start) 2228 order = min_t(int, MAX_PAGE_ORDER, __ffs(start)); 2229 else 2230 order = MAX_PAGE_ORDER; 2231 2232 while (start + (1UL << order) > end) 2233 order--; 2234 2235 memblock_free_pages(start, order); 2236 2237 start += (1UL << order); 2238 } 2239 } 2240 2241 static unsigned long __init __free_memory_core(phys_addr_t start, 2242 phys_addr_t end) 2243 { 2244 unsigned long start_pfn = PFN_UP(start); 2245 unsigned long end_pfn = PFN_DOWN(end); 2246 2247 if (!IS_ENABLED(CONFIG_HIGHMEM) && end_pfn > max_low_pfn) 2248 end_pfn = max_low_pfn; 2249 2250 if (start_pfn >= end_pfn) 2251 return 0; 2252 2253 __free_pages_memory(start_pfn, end_pfn); 2254 2255 return end_pfn - start_pfn; 2256 } 2257 2258 static void __init memmap_init_reserved_pages(void) 2259 { 2260 struct memblock_region *region; 2261 phys_addr_t start, end; 2262 int nid; 2263 unsigned long max_reserved; 2264 2265 /* 2266 * set nid on all reserved pages and also treat struct 2267 * pages for the NOMAP regions as PageReserved 2268 */ 2269 repeat: 2270 max_reserved = memblock.reserved.max; 2271 for_each_mem_region(region) { 2272 nid = memblock_get_region_node(region); 2273 start = region->base; 2274 end = start + region->size; 2275 2276 if (memblock_is_nomap(region)) 2277 reserve_bootmem_region(start, end, nid); 2278 2279 memblock_set_node(start, region->size, &memblock.reserved, nid); 2280 } 2281 /* 2282 * 'max' is changed means memblock.reserved has been doubled its 2283 * array, which may result a new reserved region before current 2284 * 'start'. Now we should repeat the procedure to set its node id. 2285 */ 2286 if (max_reserved != memblock.reserved.max) 2287 goto repeat; 2288 2289 /* 2290 * initialize struct pages for reserved regions that don't have 2291 * the MEMBLOCK_RSRV_NOINIT flag set 2292 */ 2293 for_each_reserved_mem_region(region) { 2294 if (!memblock_is_reserved_noinit(region)) { 2295 nid = memblock_get_region_node(region); 2296 start = region->base; 2297 end = start + region->size; 2298 2299 if (!numa_valid_node(nid)) 2300 nid = early_pfn_to_nid(PFN_DOWN(start)); 2301 2302 reserve_bootmem_region(start, end, nid); 2303 } 2304 } 2305 } 2306 2307 static unsigned long __init free_low_memory_core_early(void) 2308 { 2309 unsigned long count = 0; 2310 phys_addr_t start, end; 2311 u64 i; 2312 2313 memblock_clear_hotplug(0, -1); 2314 2315 memmap_init_reserved_pages(); 2316 2317 /* 2318 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id 2319 * because in some case like Node0 doesn't have RAM installed 2320 * low ram will be on Node1 2321 */ 2322 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end, 2323 NULL) 2324 count += __free_memory_core(start, end); 2325 2326 return count; 2327 } 2328 2329 static int reset_managed_pages_done __initdata; 2330 2331 static void __init reset_node_managed_pages(pg_data_t *pgdat) 2332 { 2333 struct zone *z; 2334 2335 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 2336 atomic_long_set(&z->managed_pages, 0); 2337 } 2338 2339 void __init reset_all_zones_managed_pages(void) 2340 { 2341 struct pglist_data *pgdat; 2342 2343 if (reset_managed_pages_done) 2344 return; 2345 2346 for_each_online_pgdat(pgdat) 2347 reset_node_managed_pages(pgdat); 2348 2349 reset_managed_pages_done = 1; 2350 } 2351 2352 /** 2353 * memblock_free_all - release free pages to the buddy allocator 2354 */ 2355 void __init memblock_free_all(void) 2356 { 2357 unsigned long pages; 2358 2359 free_unused_memmap(); 2360 reset_all_zones_managed_pages(); 2361 2362 memblock_clear_kho_scratch_only(); 2363 pages = free_low_memory_core_early(); 2364 totalram_pages_add(pages); 2365 } 2366 2367 /* Keep a table to reserve named memory */ 2368 #define RESERVE_MEM_MAX_ENTRIES 8 2369 #define RESERVE_MEM_NAME_SIZE 16 2370 struct reserve_mem_table { 2371 char name[RESERVE_MEM_NAME_SIZE]; 2372 phys_addr_t start; 2373 phys_addr_t size; 2374 }; 2375 static struct reserve_mem_table reserved_mem_table[RESERVE_MEM_MAX_ENTRIES]; 2376 static int reserved_mem_count; 2377 static DEFINE_MUTEX(reserve_mem_lock); 2378 2379 /* Add wildcard region with a lookup name */ 2380 static void __init reserved_mem_add(phys_addr_t start, phys_addr_t size, 2381 const char *name) 2382 { 2383 struct reserve_mem_table *map; 2384 2385 map = &reserved_mem_table[reserved_mem_count++]; 2386 map->start = start; 2387 map->size = size; 2388 strscpy(map->name, name); 2389 } 2390 2391 static struct reserve_mem_table *reserve_mem_find_by_name_nolock(const char *name) 2392 { 2393 struct reserve_mem_table *map; 2394 int i; 2395 2396 for (i = 0; i < reserved_mem_count; i++) { 2397 map = &reserved_mem_table[i]; 2398 if (!map->size) 2399 continue; 2400 if (strcmp(name, map->name) == 0) 2401 return map; 2402 } 2403 return NULL; 2404 } 2405 2406 /** 2407 * reserve_mem_find_by_name - Find reserved memory region with a given name 2408 * @name: The name that is attached to a reserved memory region 2409 * @start: If found, holds the start address 2410 * @size: If found, holds the size of the address. 2411 * 2412 * @start and @size are only updated if @name is found. 2413 * 2414 * Returns: 1 if found or 0 if not found. 2415 */ 2416 int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t *size) 2417 { 2418 struct reserve_mem_table *map; 2419 2420 guard(mutex)(&reserve_mem_lock); 2421 map = reserve_mem_find_by_name_nolock(name); 2422 if (!map) 2423 return 0; 2424 2425 *start = map->start; 2426 *size = map->size; 2427 return 1; 2428 } 2429 EXPORT_SYMBOL_GPL(reserve_mem_find_by_name); 2430 2431 /** 2432 * reserve_mem_release_by_name - Release reserved memory region with a given name 2433 * @name: The name that is attached to a reserved memory region 2434 * 2435 * Forcibly release the pages in the reserved memory region so that those memory 2436 * can be used as free memory. After released the reserved region size becomes 0. 2437 * 2438 * Returns: 1 if released or 0 if not found. 2439 */ 2440 int reserve_mem_release_by_name(const char *name) 2441 { 2442 char buf[RESERVE_MEM_NAME_SIZE + 12]; 2443 struct reserve_mem_table *map; 2444 void *start, *end; 2445 2446 guard(mutex)(&reserve_mem_lock); 2447 map = reserve_mem_find_by_name_nolock(name); 2448 if (!map) 2449 return 0; 2450 2451 start = phys_to_virt(map->start); 2452 end = start + map->size - 1; 2453 snprintf(buf, sizeof(buf), "reserve_mem:%s", name); 2454 free_reserved_area(start, end, 0, buf); 2455 map->size = 0; 2456 2457 return 1; 2458 } 2459 2460 #ifdef CONFIG_KEXEC_HANDOVER 2461 2462 static int __init reserved_mem_preserve(void) 2463 { 2464 unsigned int nr_preserved = 0; 2465 int err; 2466 2467 for (unsigned int i = 0; i < reserved_mem_count; i++, nr_preserved++) { 2468 struct reserve_mem_table *map = &reserved_mem_table[i]; 2469 struct page *page = phys_to_page(map->start); 2470 unsigned int nr_pages = map->size >> PAGE_SHIFT; 2471 2472 err = kho_preserve_pages(page, nr_pages); 2473 if (err) 2474 goto err_unpreserve; 2475 } 2476 2477 return 0; 2478 2479 err_unpreserve: 2480 for (unsigned int i = 0; i < nr_preserved; i++) { 2481 struct reserve_mem_table *map = &reserved_mem_table[i]; 2482 struct page *page = phys_to_page(map->start); 2483 unsigned int nr_pages = map->size >> PAGE_SHIFT; 2484 2485 kho_unpreserve_pages(page, nr_pages); 2486 } 2487 2488 return err; 2489 } 2490 2491 static int __init prepare_kho_fdt(void) 2492 { 2493 struct page *fdt_page; 2494 void *fdt; 2495 int err; 2496 2497 fdt_page = alloc_page(GFP_KERNEL); 2498 if (!fdt_page) { 2499 err = -ENOMEM; 2500 goto err_report; 2501 } 2502 2503 fdt = page_to_virt(fdt_page); 2504 err = kho_preserve_pages(fdt_page, 1); 2505 if (err) 2506 goto err_free_fdt; 2507 2508 err |= fdt_create(fdt, PAGE_SIZE); 2509 err |= fdt_finish_reservemap(fdt); 2510 err |= fdt_begin_node(fdt, ""); 2511 err |= fdt_property_string(fdt, "compatible", MEMBLOCK_KHO_NODE_COMPATIBLE); 2512 2513 for (unsigned int i = 0; !err && i < reserved_mem_count; i++) { 2514 struct reserve_mem_table *map = &reserved_mem_table[i]; 2515 2516 err |= fdt_begin_node(fdt, map->name); 2517 err |= fdt_property_string(fdt, "compatible", RESERVE_MEM_KHO_NODE_COMPATIBLE); 2518 err |= fdt_property(fdt, "start", &map->start, sizeof(map->start)); 2519 err |= fdt_property(fdt, "size", &map->size, sizeof(map->size)); 2520 err |= fdt_end_node(fdt); 2521 } 2522 err |= fdt_end_node(fdt); 2523 err |= fdt_finish(fdt); 2524 2525 if (err) 2526 goto err_unpreserve_fdt; 2527 2528 err = kho_add_subtree(MEMBLOCK_KHO_FDT, fdt); 2529 if (err) 2530 goto err_unpreserve_fdt; 2531 2532 err = reserved_mem_preserve(); 2533 if (err) 2534 goto err_remove_subtree; 2535 2536 return 0; 2537 2538 err_remove_subtree: 2539 kho_remove_subtree(fdt); 2540 err_unpreserve_fdt: 2541 kho_unpreserve_pages(fdt_page, 1); 2542 err_free_fdt: 2543 put_page(fdt_page); 2544 err_report: 2545 pr_err("failed to prepare memblock FDT for KHO: %d\n", err); 2546 2547 return err; 2548 } 2549 2550 static int __init reserve_mem_init(void) 2551 { 2552 int err; 2553 2554 if (!kho_is_enabled() || !reserved_mem_count) 2555 return 0; 2556 2557 err = prepare_kho_fdt(); 2558 if (err) 2559 return err; 2560 return err; 2561 } 2562 late_initcall(reserve_mem_init); 2563 2564 static void *__init reserve_mem_kho_retrieve_fdt(void) 2565 { 2566 phys_addr_t fdt_phys; 2567 static void *fdt; 2568 int err; 2569 2570 if (fdt) 2571 return fdt; 2572 2573 err = kho_retrieve_subtree(MEMBLOCK_KHO_FDT, &fdt_phys); 2574 if (err) { 2575 if (err != -ENOENT) 2576 pr_warn("failed to retrieve FDT '%s' from KHO: %d\n", 2577 MEMBLOCK_KHO_FDT, err); 2578 return NULL; 2579 } 2580 2581 fdt = phys_to_virt(fdt_phys); 2582 2583 err = fdt_node_check_compatible(fdt, 0, MEMBLOCK_KHO_NODE_COMPATIBLE); 2584 if (err) { 2585 pr_warn("FDT '%s' is incompatible with '%s': %d\n", 2586 MEMBLOCK_KHO_FDT, MEMBLOCK_KHO_NODE_COMPATIBLE, err); 2587 fdt = NULL; 2588 } 2589 2590 return fdt; 2591 } 2592 2593 static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size, 2594 phys_addr_t align) 2595 { 2596 int err, len_start, len_size, offset; 2597 const phys_addr_t *p_start, *p_size; 2598 const void *fdt; 2599 2600 fdt = reserve_mem_kho_retrieve_fdt(); 2601 if (!fdt) 2602 return false; 2603 2604 offset = fdt_subnode_offset(fdt, 0, name); 2605 if (offset < 0) { 2606 pr_warn("FDT '%s' has no child '%s': %d\n", 2607 MEMBLOCK_KHO_FDT, name, offset); 2608 return false; 2609 } 2610 err = fdt_node_check_compatible(fdt, offset, RESERVE_MEM_KHO_NODE_COMPATIBLE); 2611 if (err) { 2612 pr_warn("Node '%s' is incompatible with '%s': %d\n", 2613 name, RESERVE_MEM_KHO_NODE_COMPATIBLE, err); 2614 return false; 2615 } 2616 2617 p_start = fdt_getprop(fdt, offset, "start", &len_start); 2618 p_size = fdt_getprop(fdt, offset, "size", &len_size); 2619 if (!p_start || len_start != sizeof(*p_start) || !p_size || 2620 len_size != sizeof(*p_size)) { 2621 return false; 2622 } 2623 2624 if (*p_start & (align - 1)) { 2625 pr_warn("KHO reserve-mem '%s' has wrong alignment (0x%lx, 0x%lx)\n", 2626 name, (long)align, (long)*p_start); 2627 return false; 2628 } 2629 2630 if (*p_size != size) { 2631 pr_warn("KHO reserve-mem '%s' has wrong size (0x%lx != 0x%lx)\n", 2632 name, (long)*p_size, (long)size); 2633 return false; 2634 } 2635 2636 reserved_mem_add(*p_start, size, name); 2637 pr_info("Revived memory reservation '%s' from KHO\n", name); 2638 2639 return true; 2640 } 2641 #else 2642 static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size, 2643 phys_addr_t align) 2644 { 2645 return false; 2646 } 2647 #endif /* CONFIG_KEXEC_HANDOVER */ 2648 2649 /* 2650 * Parse reserve_mem=nn:align:name 2651 */ 2652 static int __init reserve_mem(char *p) 2653 { 2654 phys_addr_t start, size, align, tmp; 2655 char *name; 2656 char *oldp; 2657 int len; 2658 2659 if (!p) 2660 return -EINVAL; 2661 2662 /* Check if there's room for more reserved memory */ 2663 if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES) 2664 return -EBUSY; 2665 2666 oldp = p; 2667 size = memparse(p, &p); 2668 if (!size || p == oldp) 2669 return -EINVAL; 2670 2671 if (*p != ':') 2672 return -EINVAL; 2673 2674 align = memparse(p+1, &p); 2675 if (*p != ':') 2676 return -EINVAL; 2677 2678 /* 2679 * memblock_phys_alloc() doesn't like a zero size align, 2680 * but it is OK for this command to have it. 2681 */ 2682 if (align < SMP_CACHE_BYTES) 2683 align = SMP_CACHE_BYTES; 2684 2685 name = p + 1; 2686 len = strlen(name); 2687 2688 /* name needs to have length but not too big */ 2689 if (!len || len >= RESERVE_MEM_NAME_SIZE) 2690 return -EINVAL; 2691 2692 /* Make sure that name has text */ 2693 for (p = name; *p; p++) { 2694 if (!isspace(*p)) 2695 break; 2696 } 2697 if (!*p) 2698 return -EINVAL; 2699 2700 /* Make sure the name is not already used */ 2701 if (reserve_mem_find_by_name(name, &start, &tmp)) 2702 return -EBUSY; 2703 2704 /* Pick previous allocations up from KHO if available */ 2705 if (reserve_mem_kho_revive(name, size, align)) 2706 return 1; 2707 2708 /* TODO: Allocation must be outside of scratch region */ 2709 start = memblock_phys_alloc(size, align); 2710 if (!start) 2711 return -ENOMEM; 2712 2713 reserved_mem_add(start, size, name); 2714 2715 return 1; 2716 } 2717 __setup("reserve_mem=", reserve_mem); 2718 2719 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK) 2720 static const char * const flagname[] = { 2721 [ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG", 2722 [ilog2(MEMBLOCK_MIRROR)] = "MIRROR", 2723 [ilog2(MEMBLOCK_NOMAP)] = "NOMAP", 2724 [ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG", 2725 [ilog2(MEMBLOCK_RSRV_NOINIT)] = "RSV_NIT", 2726 [ilog2(MEMBLOCK_RSRV_KERN)] = "RSV_KERN", 2727 [ilog2(MEMBLOCK_KHO_SCRATCH)] = "KHO_SCRATCH", 2728 }; 2729 2730 static int memblock_debug_show(struct seq_file *m, void *private) 2731 { 2732 struct memblock_type *type = m->private; 2733 struct memblock_region *reg; 2734 int i, j, nid; 2735 unsigned int count = ARRAY_SIZE(flagname); 2736 phys_addr_t end; 2737 2738 for (i = 0; i < type->cnt; i++) { 2739 reg = &type->regions[i]; 2740 end = reg->base + reg->size - 1; 2741 nid = memblock_get_region_node(reg); 2742 2743 seq_printf(m, "%4d: ", i); 2744 seq_printf(m, "%pa..%pa ", ®->base, &end); 2745 if (numa_valid_node(nid)) 2746 seq_printf(m, "%4d ", nid); 2747 else 2748 seq_printf(m, "%4c ", 'x'); 2749 if (reg->flags) { 2750 for (j = 0; j < count; j++) { 2751 if (reg->flags & (1U << j)) { 2752 seq_printf(m, "%s\n", flagname[j]); 2753 break; 2754 } 2755 } 2756 if (j == count) 2757 seq_printf(m, "%s\n", "UNKNOWN"); 2758 } else { 2759 seq_printf(m, "%s\n", "NONE"); 2760 } 2761 } 2762 return 0; 2763 } 2764 DEFINE_SHOW_ATTRIBUTE(memblock_debug); 2765 2766 static int __init memblock_init_debugfs(void) 2767 { 2768 struct dentry *root = debugfs_create_dir("memblock", NULL); 2769 2770 debugfs_create_file("memory", 0444, root, 2771 &memblock.memory, &memblock_debug_fops); 2772 debugfs_create_file("reserved", 0444, root, 2773 &memblock.reserved, &memblock_debug_fops); 2774 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 2775 debugfs_create_file("physmem", 0444, root, &physmem, 2776 &memblock_debug_fops); 2777 #endif 2778 2779 return 0; 2780 } 2781 __initcall(memblock_init_debugfs); 2782 2783 #endif /* CONFIG_DEBUG_FS */ 2784