195f72d1eSYinghai Lu /* 295f72d1eSYinghai Lu * Procedures for maintaining information about logical memory blocks. 395f72d1eSYinghai Lu * 495f72d1eSYinghai Lu * Peter Bergner, IBM Corp. June 2001. 595f72d1eSYinghai Lu * Copyright (C) 2001 Peter Bergner. 695f72d1eSYinghai Lu * 795f72d1eSYinghai Lu * This program is free software; you can redistribute it and/or 895f72d1eSYinghai Lu * modify it under the terms of the GNU General Public License 995f72d1eSYinghai Lu * as published by the Free Software Foundation; either version 1095f72d1eSYinghai Lu * 2 of the License, or (at your option) any later version. 1195f72d1eSYinghai Lu */ 1295f72d1eSYinghai Lu 1395f72d1eSYinghai Lu #include <linux/kernel.h> 14142b45a7SBenjamin Herrenschmidt #include <linux/slab.h> 1595f72d1eSYinghai Lu #include <linux/init.h> 1695f72d1eSYinghai Lu #include <linux/bitops.h> 17449e8df3SBenjamin Herrenschmidt #include <linux/poison.h> 18c196f76fSBenjamin Herrenschmidt #include <linux/pfn.h> 196d03b885SBenjamin Herrenschmidt #include <linux/debugfs.h> 20514c6032SRandy Dunlap #include <linux/kmemleak.h> 216d03b885SBenjamin Herrenschmidt #include <linux/seq_file.h> 2295f72d1eSYinghai Lu #include <linux/memblock.h> 2395f72d1eSYinghai Lu 24c4c5ad6bSChristoph Hellwig #include <asm/sections.h> 2526f09e9bSSantosh Shilimkar #include <linux/io.h> 2626f09e9bSSantosh Shilimkar 2726f09e9bSSantosh Shilimkar #include "internal.h" 2879442ed1STang Chen 298a5b403dSArd Biesheuvel #define INIT_MEMBLOCK_REGIONS 128 308a5b403dSArd Biesheuvel #define INIT_PHYSMEM_REGIONS 4 318a5b403dSArd Biesheuvel 328a5b403dSArd Biesheuvel #ifndef INIT_MEMBLOCK_RESERVED_REGIONS 338a5b403dSArd Biesheuvel # define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS 348a5b403dSArd Biesheuvel #endif 358a5b403dSArd Biesheuvel 363e039c5cSMike Rapoport /** 373e039c5cSMike Rapoport * DOC: memblock overview 383e039c5cSMike Rapoport * 393e039c5cSMike Rapoport * Memblock is a method of managing memory regions during the early 403e039c5cSMike Rapoport * boot period when the usual kernel memory allocators are not up and 413e039c5cSMike Rapoport * running. 423e039c5cSMike Rapoport * 433e039c5cSMike Rapoport * Memblock views the system memory as collections of contiguous 443e039c5cSMike Rapoport * regions. There are several types of these collections: 453e039c5cSMike Rapoport * 463e039c5cSMike Rapoport * * ``memory`` - describes the physical memory available to the 473e039c5cSMike Rapoport * kernel; this may differ from the actual physical memory installed 483e039c5cSMike Rapoport * in the system, for instance when the memory is restricted with 493e039c5cSMike Rapoport * ``mem=`` command line parameter 503e039c5cSMike Rapoport * * ``reserved`` - describes the regions that were allocated 513e039c5cSMike Rapoport * * ``physmap`` - describes the actual physical memory regardless of 523e039c5cSMike Rapoport * the possible restrictions; the ``physmap`` type is only available 533e039c5cSMike Rapoport * on some architectures. 543e039c5cSMike Rapoport * 553e039c5cSMike Rapoport * Each region is represented by :c:type:`struct memblock_region` that 563e039c5cSMike Rapoport * defines the region extents, its attributes and NUMA node id on NUMA 573e039c5cSMike Rapoport * systems. Every memory type is described by the :c:type:`struct 583e039c5cSMike Rapoport * memblock_type` which contains an array of memory regions along with 593e039c5cSMike Rapoport * the allocator metadata. The memory types are nicely wrapped with 603e039c5cSMike Rapoport * :c:type:`struct memblock`. This structure is statically initialzed 613e039c5cSMike Rapoport * at build time. The region arrays for the "memory" and "reserved" 623e039c5cSMike Rapoport * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the 633e039c5cSMike Rapoport * "physmap" type to %INIT_PHYSMEM_REGIONS. 643e039c5cSMike Rapoport * The :c:func:`memblock_allow_resize` enables automatic resizing of 653e039c5cSMike Rapoport * the region arrays during addition of new regions. This feature 663e039c5cSMike Rapoport * should be used with care so that memory allocated for the region 673e039c5cSMike Rapoport * array will not overlap with areas that should be reserved, for 683e039c5cSMike Rapoport * example initrd. 693e039c5cSMike Rapoport * 703e039c5cSMike Rapoport * The early architecture setup should tell memblock what the physical 713e039c5cSMike Rapoport * memory layout is by using :c:func:`memblock_add` or 723e039c5cSMike Rapoport * :c:func:`memblock_add_node` functions. The first function does not 733e039c5cSMike Rapoport * assign the region to a NUMA node and it is appropriate for UMA 743e039c5cSMike Rapoport * systems. Yet, it is possible to use it on NUMA systems as well and 753e039c5cSMike Rapoport * assign the region to a NUMA node later in the setup process using 763e039c5cSMike Rapoport * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node` 773e039c5cSMike Rapoport * performs such an assignment directly. 783e039c5cSMike Rapoport * 79a2974133SMike Rapoport * Once memblock is setup the memory can be allocated using one of the 80a2974133SMike Rapoport * API variants: 81a2974133SMike Rapoport * 82a2974133SMike Rapoport * * :c:func:`memblock_phys_alloc*` - these functions return the 83a2974133SMike Rapoport * **physical** address of the allocated memory 84a2974133SMike Rapoport * * :c:func:`memblock_alloc*` - these functions return the **virtual** 85a2974133SMike Rapoport * address of the allocated memory. 86a2974133SMike Rapoport * 87a2974133SMike Rapoport * Note, that both API variants use implict assumptions about allowed 88a2974133SMike Rapoport * memory ranges and the fallback methods. Consult the documentation 89a2974133SMike Rapoport * of :c:func:`memblock_alloc_internal` and 90a2974133SMike Rapoport * :c:func:`memblock_alloc_range_nid` functions for more elaboarte 91a2974133SMike Rapoport * description. 923e039c5cSMike Rapoport * 933e039c5cSMike Rapoport * As the system boot progresses, the architecture specific 943e039c5cSMike Rapoport * :c:func:`mem_init` function frees all the memory to the buddy page 953e039c5cSMike Rapoport * allocator. 963e039c5cSMike Rapoport * 97*350e88baSMike Rapoport * Unless an architecure enables %CONFIG_ARCH_KEEP_MEMBLOCK, the 983e039c5cSMike Rapoport * memblock data structures will be discarded after the system 993e039c5cSMike Rapoport * initialization compltes. 1003e039c5cSMike Rapoport */ 1013e039c5cSMike Rapoport 102bda49a81SMike Rapoport #ifndef CONFIG_NEED_MULTIPLE_NODES 103bda49a81SMike Rapoport struct pglist_data __refdata contig_page_data; 104bda49a81SMike Rapoport EXPORT_SYMBOL(contig_page_data); 105bda49a81SMike Rapoport #endif 106bda49a81SMike Rapoport 107bda49a81SMike Rapoport unsigned long max_low_pfn; 108bda49a81SMike Rapoport unsigned long min_low_pfn; 109bda49a81SMike Rapoport unsigned long max_pfn; 110bda49a81SMike Rapoport unsigned long long max_possible_pfn; 111bda49a81SMike Rapoport 112fe091c20STejun Heo static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; 1138a5b403dSArd Biesheuvel static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock; 11470210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 11570210ed9SPhilipp Hachtmann static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock; 11670210ed9SPhilipp Hachtmann #endif 117fe091c20STejun Heo 118fe091c20STejun Heo struct memblock memblock __initdata_memblock = { 119fe091c20STejun Heo .memory.regions = memblock_memory_init_regions, 120fe091c20STejun Heo .memory.cnt = 1, /* empty dummy entry */ 121fe091c20STejun Heo .memory.max = INIT_MEMBLOCK_REGIONS, 1220262d9c8SHeiko Carstens .memory.name = "memory", 123fe091c20STejun Heo 124fe091c20STejun Heo .reserved.regions = memblock_reserved_init_regions, 125fe091c20STejun Heo .reserved.cnt = 1, /* empty dummy entry */ 1268a5b403dSArd Biesheuvel .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS, 1270262d9c8SHeiko Carstens .reserved.name = "reserved", 128fe091c20STejun Heo 12970210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 13070210ed9SPhilipp Hachtmann .physmem.regions = memblock_physmem_init_regions, 13170210ed9SPhilipp Hachtmann .physmem.cnt = 1, /* empty dummy entry */ 13270210ed9SPhilipp Hachtmann .physmem.max = INIT_PHYSMEM_REGIONS, 1330262d9c8SHeiko Carstens .physmem.name = "physmem", 13470210ed9SPhilipp Hachtmann #endif 13570210ed9SPhilipp Hachtmann 13679442ed1STang Chen .bottom_up = false, 137fe091c20STejun Heo .current_limit = MEMBLOCK_ALLOC_ANYWHERE, 138fe091c20STejun Heo }; 13995f72d1eSYinghai Lu 14010d06439SYinghai Lu int memblock_debug __initdata_memblock; 141a3f5bafcSTony Luck static bool system_has_some_mirror __initdata_memblock = false; 1421aadc056STejun Heo static int memblock_can_resize __initdata_memblock; 143181eb394SGavin Shan static int memblock_memory_in_slab __initdata_memblock = 0; 144181eb394SGavin Shan static int memblock_reserved_in_slab __initdata_memblock = 0; 14595f72d1eSYinghai Lu 146c366ea89SMike Rapoport static enum memblock_flags __init_memblock choose_memblock_flags(void) 147a3f5bafcSTony Luck { 148a3f5bafcSTony Luck return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE; 149a3f5bafcSTony Luck } 150a3f5bafcSTony Luck 151eb18f1b5STejun Heo /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */ 152eb18f1b5STejun Heo static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size) 153eb18f1b5STejun Heo { 1541c4bc43dSStefan Agner return *size = min(*size, PHYS_ADDR_MAX - base); 155eb18f1b5STejun Heo } 156eb18f1b5STejun Heo 1576ed311b2SBenjamin Herrenschmidt /* 1586ed311b2SBenjamin Herrenschmidt * Address comparison utilities 1596ed311b2SBenjamin Herrenschmidt */ 16010d06439SYinghai Lu static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, 1612898cc4cSBenjamin Herrenschmidt phys_addr_t base2, phys_addr_t size2) 16295f72d1eSYinghai Lu { 16395f72d1eSYinghai Lu return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); 16495f72d1eSYinghai Lu } 16595f72d1eSYinghai Lu 16695cf82ecSTang Chen bool __init_memblock memblock_overlaps_region(struct memblock_type *type, 1672d7d3eb2SH Hartley Sweeten phys_addr_t base, phys_addr_t size) 1686ed311b2SBenjamin Herrenschmidt { 1696ed311b2SBenjamin Herrenschmidt unsigned long i; 1706ed311b2SBenjamin Herrenschmidt 171f14516fbSAlexander Kuleshov for (i = 0; i < type->cnt; i++) 172f14516fbSAlexander Kuleshov if (memblock_addrs_overlap(base, size, type->regions[i].base, 173f14516fbSAlexander Kuleshov type->regions[i].size)) 1746ed311b2SBenjamin Herrenschmidt break; 175c5c5c9d1STang Chen return i < type->cnt; 1766ed311b2SBenjamin Herrenschmidt } 1776ed311b2SBenjamin Herrenschmidt 17847cec443SMike Rapoport /** 17979442ed1STang Chen * __memblock_find_range_bottom_up - find free area utility in bottom-up 18079442ed1STang Chen * @start: start of candidate range 18147cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 18247cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 18379442ed1STang Chen * @size: size of free area to find 18479442ed1STang Chen * @align: alignment of free area to find 185b1154233SGrygorii Strashko * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 186fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 18779442ed1STang Chen * 18879442ed1STang Chen * Utility called from memblock_find_in_range_node(), find free area bottom-up. 18979442ed1STang Chen * 19047cec443SMike Rapoport * Return: 19179442ed1STang Chen * Found address on success, 0 on failure. 19279442ed1STang Chen */ 19379442ed1STang Chen static phys_addr_t __init_memblock 19479442ed1STang Chen __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end, 195fc6daaf9STony Luck phys_addr_t size, phys_addr_t align, int nid, 196e1720feeSMike Rapoport enum memblock_flags flags) 19779442ed1STang Chen { 19879442ed1STang Chen phys_addr_t this_start, this_end, cand; 19979442ed1STang Chen u64 i; 20079442ed1STang Chen 201fc6daaf9STony Luck for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) { 20279442ed1STang Chen this_start = clamp(this_start, start, end); 20379442ed1STang Chen this_end = clamp(this_end, start, end); 20479442ed1STang Chen 20579442ed1STang Chen cand = round_up(this_start, align); 20679442ed1STang Chen if (cand < this_end && this_end - cand >= size) 20779442ed1STang Chen return cand; 20879442ed1STang Chen } 20979442ed1STang Chen 21079442ed1STang Chen return 0; 21179442ed1STang Chen } 21279442ed1STang Chen 2137bd0b0f0STejun Heo /** 2141402899eSTang Chen * __memblock_find_range_top_down - find free area utility, in top-down 2151402899eSTang Chen * @start: start of candidate range 21647cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 21747cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 2181402899eSTang Chen * @size: size of free area to find 2191402899eSTang Chen * @align: alignment of free area to find 220b1154233SGrygorii Strashko * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 221fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 2221402899eSTang Chen * 2231402899eSTang Chen * Utility called from memblock_find_in_range_node(), find free area top-down. 2241402899eSTang Chen * 22547cec443SMike Rapoport * Return: 22679442ed1STang Chen * Found address on success, 0 on failure. 2271402899eSTang Chen */ 2281402899eSTang Chen static phys_addr_t __init_memblock 2291402899eSTang Chen __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end, 230fc6daaf9STony Luck phys_addr_t size, phys_addr_t align, int nid, 231e1720feeSMike Rapoport enum memblock_flags flags) 2321402899eSTang Chen { 2331402899eSTang Chen phys_addr_t this_start, this_end, cand; 2341402899eSTang Chen u64 i; 2351402899eSTang Chen 236fc6daaf9STony Luck for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end, 237fc6daaf9STony Luck NULL) { 2381402899eSTang Chen this_start = clamp(this_start, start, end); 2391402899eSTang Chen this_end = clamp(this_end, start, end); 2401402899eSTang Chen 2411402899eSTang Chen if (this_end < size) 2421402899eSTang Chen continue; 2431402899eSTang Chen 2441402899eSTang Chen cand = round_down(this_end - size, align); 2451402899eSTang Chen if (cand >= this_start) 2461402899eSTang Chen return cand; 2471402899eSTang Chen } 2481402899eSTang Chen 2491402899eSTang Chen return 0; 2501402899eSTang Chen } 2511402899eSTang Chen 2521402899eSTang Chen /** 2537bd0b0f0STejun Heo * memblock_find_in_range_node - find free area in given range and node 2547bd0b0f0STejun Heo * @size: size of free area to find 2557bd0b0f0STejun Heo * @align: alignment of free area to find 25687029ee9SGrygorii Strashko * @start: start of candidate range 25747cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 25847cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 259b1154233SGrygorii Strashko * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 260fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 2617bd0b0f0STejun Heo * 2627bd0b0f0STejun Heo * Find @size free area aligned to @align in the specified range and node. 2637bd0b0f0STejun Heo * 26479442ed1STang Chen * When allocation direction is bottom-up, the @start should be greater 26579442ed1STang Chen * than the end of the kernel image. Otherwise, it will be trimmed. The 26679442ed1STang Chen * reason is that we want the bottom-up allocation just near the kernel 26779442ed1STang Chen * image so it is highly likely that the allocated memory and the kernel 26879442ed1STang Chen * will reside in the same node. 26979442ed1STang Chen * 27079442ed1STang Chen * If bottom-up allocation failed, will try to allocate memory top-down. 27179442ed1STang Chen * 27247cec443SMike Rapoport * Return: 27379442ed1STang Chen * Found address on success, 0 on failure. 2746ed311b2SBenjamin Herrenschmidt */ 275c366ea89SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size, 27687029ee9SGrygorii Strashko phys_addr_t align, phys_addr_t start, 277e1720feeSMike Rapoport phys_addr_t end, int nid, 278e1720feeSMike Rapoport enum memblock_flags flags) 279f7210e6cSTang Chen { 2800cfb8f0cSTang Chen phys_addr_t kernel_end, ret; 28179442ed1STang Chen 282f7210e6cSTang Chen /* pump up @end */ 283fed84c78SQian Cai if (end == MEMBLOCK_ALLOC_ACCESSIBLE || 284fed84c78SQian Cai end == MEMBLOCK_ALLOC_KASAN) 285f7210e6cSTang Chen end = memblock.current_limit; 286f7210e6cSTang Chen 287f7210e6cSTang Chen /* avoid allocating the first page */ 288f7210e6cSTang Chen start = max_t(phys_addr_t, start, PAGE_SIZE); 289f7210e6cSTang Chen end = max(start, end); 29079442ed1STang Chen kernel_end = __pa_symbol(_end); 29179442ed1STang Chen 29279442ed1STang Chen /* 29379442ed1STang Chen * try bottom-up allocation only when bottom-up mode 29479442ed1STang Chen * is set and @end is above the kernel image. 29579442ed1STang Chen */ 29679442ed1STang Chen if (memblock_bottom_up() && end > kernel_end) { 29779442ed1STang Chen phys_addr_t bottom_up_start; 29879442ed1STang Chen 29979442ed1STang Chen /* make sure we will allocate above the kernel */ 30079442ed1STang Chen bottom_up_start = max(start, kernel_end); 30179442ed1STang Chen 30279442ed1STang Chen /* ok, try bottom-up allocation first */ 30379442ed1STang Chen ret = __memblock_find_range_bottom_up(bottom_up_start, end, 304fc6daaf9STony Luck size, align, nid, flags); 30579442ed1STang Chen if (ret) 30679442ed1STang Chen return ret; 30779442ed1STang Chen 30879442ed1STang Chen /* 30979442ed1STang Chen * we always limit bottom-up allocation above the kernel, 31079442ed1STang Chen * but top-down allocation doesn't have the limit, so 31179442ed1STang Chen * retrying top-down allocation may succeed when bottom-up 31279442ed1STang Chen * allocation failed. 31379442ed1STang Chen * 31479442ed1STang Chen * bottom-up allocation is expected to be fail very rarely, 31579442ed1STang Chen * so we use WARN_ONCE() here to see the stack trace if 31679442ed1STang Chen * fail happens. 31779442ed1STang Chen */ 318e3d301caSMichal Hocko WARN_ONCE(IS_ENABLED(CONFIG_MEMORY_HOTREMOVE), 319e3d301caSMichal Hocko "memblock: bottom-up allocation failed, memory hotremove may be affected\n"); 32079442ed1STang Chen } 321f7210e6cSTang Chen 322fc6daaf9STony Luck return __memblock_find_range_top_down(start, end, size, align, nid, 323fc6daaf9STony Luck flags); 324f7210e6cSTang Chen } 3256ed311b2SBenjamin Herrenschmidt 3267bd0b0f0STejun Heo /** 3277bd0b0f0STejun Heo * memblock_find_in_range - find free area in given range 3287bd0b0f0STejun Heo * @start: start of candidate range 32947cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 33047cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 3317bd0b0f0STejun Heo * @size: size of free area to find 3327bd0b0f0STejun Heo * @align: alignment of free area to find 3337bd0b0f0STejun Heo * 3347bd0b0f0STejun Heo * Find @size free area aligned to @align in the specified range. 3357bd0b0f0STejun Heo * 33647cec443SMike Rapoport * Return: 33779442ed1STang Chen * Found address on success, 0 on failure. 3387bd0b0f0STejun Heo */ 3397bd0b0f0STejun Heo phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, 3407bd0b0f0STejun Heo phys_addr_t end, phys_addr_t size, 3417bd0b0f0STejun Heo phys_addr_t align) 3427bd0b0f0STejun Heo { 343a3f5bafcSTony Luck phys_addr_t ret; 344e1720feeSMike Rapoport enum memblock_flags flags = choose_memblock_flags(); 345a3f5bafcSTony Luck 346a3f5bafcSTony Luck again: 347a3f5bafcSTony Luck ret = memblock_find_in_range_node(size, align, start, end, 348a3f5bafcSTony Luck NUMA_NO_NODE, flags); 349a3f5bafcSTony Luck 350a3f5bafcSTony Luck if (!ret && (flags & MEMBLOCK_MIRROR)) { 351a3f5bafcSTony Luck pr_warn("Could not allocate %pap bytes of mirrored memory\n", 352a3f5bafcSTony Luck &size); 353a3f5bafcSTony Luck flags &= ~MEMBLOCK_MIRROR; 354a3f5bafcSTony Luck goto again; 355a3f5bafcSTony Luck } 356a3f5bafcSTony Luck 357a3f5bafcSTony Luck return ret; 3587bd0b0f0STejun Heo } 3597bd0b0f0STejun Heo 36010d06439SYinghai Lu static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r) 36195f72d1eSYinghai Lu { 3621440c4e2STejun Heo type->total_size -= type->regions[r].size; 3637c0caeb8STejun Heo memmove(&type->regions[r], &type->regions[r + 1], 3647c0caeb8STejun Heo (type->cnt - (r + 1)) * sizeof(type->regions[r])); 365e3239ff9SBenjamin Herrenschmidt type->cnt--; 36695f72d1eSYinghai Lu 3678f7a6605SBenjamin Herrenschmidt /* Special case for empty arrays */ 3688f7a6605SBenjamin Herrenschmidt if (type->cnt == 0) { 3691440c4e2STejun Heo WARN_ON(type->total_size != 0); 3708f7a6605SBenjamin Herrenschmidt type->cnt = 1; 3718f7a6605SBenjamin Herrenschmidt type->regions[0].base = 0; 3728f7a6605SBenjamin Herrenschmidt type->regions[0].size = 0; 37366a20757STang Chen type->regions[0].flags = 0; 3747c0caeb8STejun Heo memblock_set_region_node(&type->regions[0], MAX_NUMNODES); 3758f7a6605SBenjamin Herrenschmidt } 37695f72d1eSYinghai Lu } 37795f72d1eSYinghai Lu 378*350e88baSMike Rapoport #ifndef CONFIG_ARCH_KEEP_MEMBLOCK 3793010f876SPavel Tatashin /** 38047cec443SMike Rapoport * memblock_discard - discard memory and reserved arrays if they were allocated 3813010f876SPavel Tatashin */ 3823010f876SPavel Tatashin void __init memblock_discard(void) 38329f67386SYinghai Lu { 3843010f876SPavel Tatashin phys_addr_t addr, size; 38529f67386SYinghai Lu 3863010f876SPavel Tatashin if (memblock.reserved.regions != memblock_reserved_init_regions) { 3873010f876SPavel Tatashin addr = __pa(memblock.reserved.regions); 3883010f876SPavel Tatashin size = PAGE_ALIGN(sizeof(struct memblock_region) * 38929f67386SYinghai Lu memblock.reserved.max); 3903010f876SPavel Tatashin __memblock_free_late(addr, size); 39129f67386SYinghai Lu } 39229f67386SYinghai Lu 39391b540f9SPavel Tatashin if (memblock.memory.regions != memblock_memory_init_regions) { 3943010f876SPavel Tatashin addr = __pa(memblock.memory.regions); 3953010f876SPavel Tatashin size = PAGE_ALIGN(sizeof(struct memblock_region) * 3965e270e25SPhilipp Hachtmann memblock.memory.max); 3973010f876SPavel Tatashin __memblock_free_late(addr, size); 3985e270e25SPhilipp Hachtmann } 3993010f876SPavel Tatashin } 4005e270e25SPhilipp Hachtmann #endif 4015e270e25SPhilipp Hachtmann 40248c3b583SGreg Pearson /** 40348c3b583SGreg Pearson * memblock_double_array - double the size of the memblock regions array 40448c3b583SGreg Pearson * @type: memblock type of the regions array being doubled 40548c3b583SGreg Pearson * @new_area_start: starting address of memory range to avoid overlap with 40648c3b583SGreg Pearson * @new_area_size: size of memory range to avoid overlap with 40748c3b583SGreg Pearson * 40848c3b583SGreg Pearson * Double the size of the @type regions array. If memblock is being used to 40948c3b583SGreg Pearson * allocate memory for a new reserved regions array and there is a previously 41048c3b583SGreg Pearson * allocated memory range [@new_area_start, @new_area_start + @new_area_size] 41148c3b583SGreg Pearson * waiting to be reserved, ensure the memory used by the new array does 41248c3b583SGreg Pearson * not overlap. 41348c3b583SGreg Pearson * 41447cec443SMike Rapoport * Return: 41548c3b583SGreg Pearson * 0 on success, -1 on failure. 41648c3b583SGreg Pearson */ 41748c3b583SGreg Pearson static int __init_memblock memblock_double_array(struct memblock_type *type, 41848c3b583SGreg Pearson phys_addr_t new_area_start, 41948c3b583SGreg Pearson phys_addr_t new_area_size) 420142b45a7SBenjamin Herrenschmidt { 421142b45a7SBenjamin Herrenschmidt struct memblock_region *new_array, *old_array; 42229f67386SYinghai Lu phys_addr_t old_alloc_size, new_alloc_size; 423a36aab89SMike Rapoport phys_addr_t old_size, new_size, addr, new_end; 424142b45a7SBenjamin Herrenschmidt int use_slab = slab_is_available(); 425181eb394SGavin Shan int *in_slab; 426142b45a7SBenjamin Herrenschmidt 427142b45a7SBenjamin Herrenschmidt /* We don't allow resizing until we know about the reserved regions 428142b45a7SBenjamin Herrenschmidt * of memory that aren't suitable for allocation 429142b45a7SBenjamin Herrenschmidt */ 430142b45a7SBenjamin Herrenschmidt if (!memblock_can_resize) 431142b45a7SBenjamin Herrenschmidt return -1; 432142b45a7SBenjamin Herrenschmidt 433142b45a7SBenjamin Herrenschmidt /* Calculate new doubled size */ 434142b45a7SBenjamin Herrenschmidt old_size = type->max * sizeof(struct memblock_region); 435142b45a7SBenjamin Herrenschmidt new_size = old_size << 1; 43629f67386SYinghai Lu /* 43729f67386SYinghai Lu * We need to allocated new one align to PAGE_SIZE, 43829f67386SYinghai Lu * so we can free them completely later. 43929f67386SYinghai Lu */ 44029f67386SYinghai Lu old_alloc_size = PAGE_ALIGN(old_size); 44129f67386SYinghai Lu new_alloc_size = PAGE_ALIGN(new_size); 442142b45a7SBenjamin Herrenschmidt 443181eb394SGavin Shan /* Retrieve the slab flag */ 444181eb394SGavin Shan if (type == &memblock.memory) 445181eb394SGavin Shan in_slab = &memblock_memory_in_slab; 446181eb394SGavin Shan else 447181eb394SGavin Shan in_slab = &memblock_reserved_in_slab; 448181eb394SGavin Shan 449a2974133SMike Rapoport /* Try to find some space for it */ 450142b45a7SBenjamin Herrenschmidt if (use_slab) { 451142b45a7SBenjamin Herrenschmidt new_array = kmalloc(new_size, GFP_KERNEL); 4521f5026a7STejun Heo addr = new_array ? __pa(new_array) : 0; 4534e2f0775SGavin Shan } else { 45448c3b583SGreg Pearson /* only exclude range when trying to double reserved.regions */ 45548c3b583SGreg Pearson if (type != &memblock.reserved) 45648c3b583SGreg Pearson new_area_start = new_area_size = 0; 45748c3b583SGreg Pearson 45848c3b583SGreg Pearson addr = memblock_find_in_range(new_area_start + new_area_size, 45948c3b583SGreg Pearson memblock.current_limit, 46029f67386SYinghai Lu new_alloc_size, PAGE_SIZE); 46148c3b583SGreg Pearson if (!addr && new_area_size) 46248c3b583SGreg Pearson addr = memblock_find_in_range(0, 46348c3b583SGreg Pearson min(new_area_start, memblock.current_limit), 46429f67386SYinghai Lu new_alloc_size, PAGE_SIZE); 46548c3b583SGreg Pearson 46615674868SSachin Kamat new_array = addr ? __va(addr) : NULL; 4674e2f0775SGavin Shan } 4681f5026a7STejun Heo if (!addr) { 469142b45a7SBenjamin Herrenschmidt pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", 4700262d9c8SHeiko Carstens type->name, type->max, type->max * 2); 471142b45a7SBenjamin Herrenschmidt return -1; 472142b45a7SBenjamin Herrenschmidt } 473142b45a7SBenjamin Herrenschmidt 474a36aab89SMike Rapoport new_end = addr + new_size - 1; 475a36aab89SMike Rapoport memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]", 476a36aab89SMike Rapoport type->name, type->max * 2, &addr, &new_end); 477ea9e4376SYinghai Lu 478fd07383bSAndrew Morton /* 479fd07383bSAndrew Morton * Found space, we now need to move the array over before we add the 480fd07383bSAndrew Morton * reserved region since it may be our reserved array itself that is 481fd07383bSAndrew Morton * full. 482142b45a7SBenjamin Herrenschmidt */ 483142b45a7SBenjamin Herrenschmidt memcpy(new_array, type->regions, old_size); 484142b45a7SBenjamin Herrenschmidt memset(new_array + type->max, 0, old_size); 485142b45a7SBenjamin Herrenschmidt old_array = type->regions; 486142b45a7SBenjamin Herrenschmidt type->regions = new_array; 487142b45a7SBenjamin Herrenschmidt type->max <<= 1; 488142b45a7SBenjamin Herrenschmidt 489fd07383bSAndrew Morton /* Free old array. We needn't free it if the array is the static one */ 490181eb394SGavin Shan if (*in_slab) 491181eb394SGavin Shan kfree(old_array); 492181eb394SGavin Shan else if (old_array != memblock_memory_init_regions && 493142b45a7SBenjamin Herrenschmidt old_array != memblock_reserved_init_regions) 49429f67386SYinghai Lu memblock_free(__pa(old_array), old_alloc_size); 495142b45a7SBenjamin Herrenschmidt 496fd07383bSAndrew Morton /* 497fd07383bSAndrew Morton * Reserve the new array if that comes from the memblock. Otherwise, we 498fd07383bSAndrew Morton * needn't do it 499181eb394SGavin Shan */ 500181eb394SGavin Shan if (!use_slab) 50129f67386SYinghai Lu BUG_ON(memblock_reserve(addr, new_alloc_size)); 502181eb394SGavin Shan 503181eb394SGavin Shan /* Update slab flag */ 504181eb394SGavin Shan *in_slab = use_slab; 505181eb394SGavin Shan 506142b45a7SBenjamin Herrenschmidt return 0; 507142b45a7SBenjamin Herrenschmidt } 508142b45a7SBenjamin Herrenschmidt 509784656f9STejun Heo /** 510784656f9STejun Heo * memblock_merge_regions - merge neighboring compatible regions 511784656f9STejun Heo * @type: memblock type to scan 512784656f9STejun Heo * 513784656f9STejun Heo * Scan @type and merge neighboring compatible regions. 514784656f9STejun Heo */ 515784656f9STejun Heo static void __init_memblock memblock_merge_regions(struct memblock_type *type) 516784656f9STejun Heo { 517784656f9STejun Heo int i = 0; 518784656f9STejun Heo 519784656f9STejun Heo /* cnt never goes below 1 */ 520784656f9STejun Heo while (i < type->cnt - 1) { 521784656f9STejun Heo struct memblock_region *this = &type->regions[i]; 522784656f9STejun Heo struct memblock_region *next = &type->regions[i + 1]; 523784656f9STejun Heo 5247c0caeb8STejun Heo if (this->base + this->size != next->base || 5257c0caeb8STejun Heo memblock_get_region_node(this) != 52666a20757STang Chen memblock_get_region_node(next) || 52766a20757STang Chen this->flags != next->flags) { 528784656f9STejun Heo BUG_ON(this->base + this->size > next->base); 529784656f9STejun Heo i++; 530784656f9STejun Heo continue; 531784656f9STejun Heo } 532784656f9STejun Heo 533784656f9STejun Heo this->size += next->size; 534c0232ae8SLin Feng /* move forward from next + 1, index of which is i + 2 */ 535c0232ae8SLin Feng memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next)); 536784656f9STejun Heo type->cnt--; 537784656f9STejun Heo } 538784656f9STejun Heo } 539784656f9STejun Heo 540784656f9STejun Heo /** 541784656f9STejun Heo * memblock_insert_region - insert new memblock region 542784656f9STejun Heo * @type: memblock type to insert into 543784656f9STejun Heo * @idx: index for the insertion point 544784656f9STejun Heo * @base: base address of the new region 545784656f9STejun Heo * @size: size of the new region 546209ff86dSTang Chen * @nid: node id of the new region 54766a20757STang Chen * @flags: flags of the new region 548784656f9STejun Heo * 549784656f9STejun Heo * Insert new memblock region [@base, @base + @size) into @type at @idx. 550412d0008SAlexander Kuleshov * @type must already have extra room to accommodate the new region. 551784656f9STejun Heo */ 552784656f9STejun Heo static void __init_memblock memblock_insert_region(struct memblock_type *type, 553784656f9STejun Heo int idx, phys_addr_t base, 55466a20757STang Chen phys_addr_t size, 555e1720feeSMike Rapoport int nid, 556e1720feeSMike Rapoport enum memblock_flags flags) 557784656f9STejun Heo { 558784656f9STejun Heo struct memblock_region *rgn = &type->regions[idx]; 559784656f9STejun Heo 560784656f9STejun Heo BUG_ON(type->cnt >= type->max); 561784656f9STejun Heo memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); 562784656f9STejun Heo rgn->base = base; 563784656f9STejun Heo rgn->size = size; 56466a20757STang Chen rgn->flags = flags; 5657c0caeb8STejun Heo memblock_set_region_node(rgn, nid); 566784656f9STejun Heo type->cnt++; 5671440c4e2STejun Heo type->total_size += size; 568784656f9STejun Heo } 569784656f9STejun Heo 570784656f9STejun Heo /** 571f1af9d3aSPhilipp Hachtmann * memblock_add_range - add new memblock region 572784656f9STejun Heo * @type: memblock type to add new region into 573784656f9STejun Heo * @base: base address of the new region 574784656f9STejun Heo * @size: size of the new region 5757fb0bc3fSTejun Heo * @nid: nid of the new region 57666a20757STang Chen * @flags: flags of the new region 577784656f9STejun Heo * 578784656f9STejun Heo * Add new memblock region [@base, @base + @size) into @type. The new region 579784656f9STejun Heo * is allowed to overlap with existing ones - overlaps don't affect already 580784656f9STejun Heo * existing regions. @type is guaranteed to be minimal (all neighbouring 581784656f9STejun Heo * compatible regions are merged) after the addition. 582784656f9STejun Heo * 58347cec443SMike Rapoport * Return: 584784656f9STejun Heo * 0 on success, -errno on failure. 585784656f9STejun Heo */ 586f1af9d3aSPhilipp Hachtmann int __init_memblock memblock_add_range(struct memblock_type *type, 58766a20757STang Chen phys_addr_t base, phys_addr_t size, 588e1720feeSMike Rapoport int nid, enum memblock_flags flags) 58995f72d1eSYinghai Lu { 590784656f9STejun Heo bool insert = false; 591eb18f1b5STejun Heo phys_addr_t obase = base; 592eb18f1b5STejun Heo phys_addr_t end = base + memblock_cap_size(base, &size); 5938c9c1701SAlexander Kuleshov int idx, nr_new; 5948c9c1701SAlexander Kuleshov struct memblock_region *rgn; 59595f72d1eSYinghai Lu 596b3dc627cSTejun Heo if (!size) 597b3dc627cSTejun Heo return 0; 598b3dc627cSTejun Heo 599784656f9STejun Heo /* special case for empty array */ 600784656f9STejun Heo if (type->regions[0].size == 0) { 6011440c4e2STejun Heo WARN_ON(type->cnt != 1 || type->total_size); 602784656f9STejun Heo type->regions[0].base = base; 603784656f9STejun Heo type->regions[0].size = size; 60466a20757STang Chen type->regions[0].flags = flags; 6057fb0bc3fSTejun Heo memblock_set_region_node(&type->regions[0], nid); 6061440c4e2STejun Heo type->total_size = size; 607784656f9STejun Heo return 0; 608784656f9STejun Heo } 609784656f9STejun Heo repeat: 610784656f9STejun Heo /* 611784656f9STejun Heo * The following is executed twice. Once with %false @insert and 612784656f9STejun Heo * then with %true. The first counts the number of regions needed 613412d0008SAlexander Kuleshov * to accommodate the new area. The second actually inserts them. 614784656f9STejun Heo */ 615784656f9STejun Heo base = obase; 616784656f9STejun Heo nr_new = 0; 617784656f9STejun Heo 61866e8b438SGioh Kim for_each_memblock_type(idx, type, rgn) { 619784656f9STejun Heo phys_addr_t rbase = rgn->base; 620784656f9STejun Heo phys_addr_t rend = rbase + rgn->size; 6218f7a6605SBenjamin Herrenschmidt 622784656f9STejun Heo if (rbase >= end) 6238f7a6605SBenjamin Herrenschmidt break; 624784656f9STejun Heo if (rend <= base) 625784656f9STejun Heo continue; 626784656f9STejun Heo /* 627784656f9STejun Heo * @rgn overlaps. If it separates the lower part of new 628784656f9STejun Heo * area, insert that portion. 6298f7a6605SBenjamin Herrenschmidt */ 630784656f9STejun Heo if (rbase > base) { 631c0a29498SWei Yang #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 632c0a29498SWei Yang WARN_ON(nid != memblock_get_region_node(rgn)); 633c0a29498SWei Yang #endif 6344fcab5f4SWei Yang WARN_ON(flags != rgn->flags); 635784656f9STejun Heo nr_new++; 636784656f9STejun Heo if (insert) 6378c9c1701SAlexander Kuleshov memblock_insert_region(type, idx++, base, 63866a20757STang Chen rbase - base, nid, 63966a20757STang Chen flags); 640784656f9STejun Heo } 641784656f9STejun Heo /* area below @rend is dealt with, forget about it */ 642784656f9STejun Heo base = min(rend, end); 6438f7a6605SBenjamin Herrenschmidt } 6448f7a6605SBenjamin Herrenschmidt 645784656f9STejun Heo /* insert the remaining portion */ 646784656f9STejun Heo if (base < end) { 647784656f9STejun Heo nr_new++; 648784656f9STejun Heo if (insert) 6498c9c1701SAlexander Kuleshov memblock_insert_region(type, idx, base, end - base, 65066a20757STang Chen nid, flags); 6518f7a6605SBenjamin Herrenschmidt } 6528f7a6605SBenjamin Herrenschmidt 653ef3cc4dbSnimisolo if (!nr_new) 654ef3cc4dbSnimisolo return 0; 655ef3cc4dbSnimisolo 656784656f9STejun Heo /* 657784656f9STejun Heo * If this was the first round, resize array and repeat for actual 658784656f9STejun Heo * insertions; otherwise, merge and return. 6598f7a6605SBenjamin Herrenschmidt */ 660784656f9STejun Heo if (!insert) { 661784656f9STejun Heo while (type->cnt + nr_new > type->max) 66248c3b583SGreg Pearson if (memblock_double_array(type, obase, size) < 0) 663784656f9STejun Heo return -ENOMEM; 664784656f9STejun Heo insert = true; 665784656f9STejun Heo goto repeat; 66695f72d1eSYinghai Lu } else { 667784656f9STejun Heo memblock_merge_regions(type); 66895f72d1eSYinghai Lu return 0; 66995f72d1eSYinghai Lu } 670784656f9STejun Heo } 67195f72d1eSYinghai Lu 67248a833ccSMike Rapoport /** 67348a833ccSMike Rapoport * memblock_add_node - add new memblock region within a NUMA node 67448a833ccSMike Rapoport * @base: base address of the new region 67548a833ccSMike Rapoport * @size: size of the new region 67648a833ccSMike Rapoport * @nid: nid of the new region 67748a833ccSMike Rapoport * 67848a833ccSMike Rapoport * Add new memblock region [@base, @base + @size) to the "memory" 67948a833ccSMike Rapoport * type. See memblock_add_range() description for mode details 68048a833ccSMike Rapoport * 68148a833ccSMike Rapoport * Return: 68248a833ccSMike Rapoport * 0 on success, -errno on failure. 68348a833ccSMike Rapoport */ 6847fb0bc3fSTejun Heo int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size, 6857fb0bc3fSTejun Heo int nid) 6867fb0bc3fSTejun Heo { 687f1af9d3aSPhilipp Hachtmann return memblock_add_range(&memblock.memory, base, size, nid, 0); 6887fb0bc3fSTejun Heo } 6897fb0bc3fSTejun Heo 69048a833ccSMike Rapoport /** 69148a833ccSMike Rapoport * memblock_add - add new memblock region 69248a833ccSMike Rapoport * @base: base address of the new region 69348a833ccSMike Rapoport * @size: size of the new region 69448a833ccSMike Rapoport * 69548a833ccSMike Rapoport * Add new memblock region [@base, @base + @size) to the "memory" 69648a833ccSMike Rapoport * type. See memblock_add_range() description for mode details 69748a833ccSMike Rapoport * 69848a833ccSMike Rapoport * Return: 69948a833ccSMike Rapoport * 0 on success, -errno on failure. 70048a833ccSMike Rapoport */ 701f705ac4bSAlexander Kuleshov int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) 7026a4055bcSAlexander Kuleshov { 7035d63f81cSMiles Chen phys_addr_t end = base + size - 1; 7045d63f81cSMiles Chen 705d75f773cSSakari Ailus memblock_dbg("memblock_add: [%pa-%pa] %pS\n", 7065d63f81cSMiles Chen &base, &end, (void *)_RET_IP_); 7076a4055bcSAlexander Kuleshov 708f705ac4bSAlexander Kuleshov return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0); 70995f72d1eSYinghai Lu } 71095f72d1eSYinghai Lu 7116a9ceb31STejun Heo /** 7126a9ceb31STejun Heo * memblock_isolate_range - isolate given range into disjoint memblocks 7136a9ceb31STejun Heo * @type: memblock type to isolate range for 7146a9ceb31STejun Heo * @base: base of range to isolate 7156a9ceb31STejun Heo * @size: size of range to isolate 7166a9ceb31STejun Heo * @start_rgn: out parameter for the start of isolated region 7176a9ceb31STejun Heo * @end_rgn: out parameter for the end of isolated region 7186a9ceb31STejun Heo * 7196a9ceb31STejun Heo * Walk @type and ensure that regions don't cross the boundaries defined by 7206a9ceb31STejun Heo * [@base, @base + @size). Crossing regions are split at the boundaries, 7216a9ceb31STejun Heo * which may create at most two more regions. The index of the first 7226a9ceb31STejun Heo * region inside the range is returned in *@start_rgn and end in *@end_rgn. 7236a9ceb31STejun Heo * 72447cec443SMike Rapoport * Return: 7256a9ceb31STejun Heo * 0 on success, -errno on failure. 7266a9ceb31STejun Heo */ 7276a9ceb31STejun Heo static int __init_memblock memblock_isolate_range(struct memblock_type *type, 7286a9ceb31STejun Heo phys_addr_t base, phys_addr_t size, 7296a9ceb31STejun Heo int *start_rgn, int *end_rgn) 7306a9ceb31STejun Heo { 731eb18f1b5STejun Heo phys_addr_t end = base + memblock_cap_size(base, &size); 7328c9c1701SAlexander Kuleshov int idx; 7338c9c1701SAlexander Kuleshov struct memblock_region *rgn; 7346a9ceb31STejun Heo 7356a9ceb31STejun Heo *start_rgn = *end_rgn = 0; 7366a9ceb31STejun Heo 737b3dc627cSTejun Heo if (!size) 738b3dc627cSTejun Heo return 0; 739b3dc627cSTejun Heo 7406a9ceb31STejun Heo /* we'll create at most two more regions */ 7416a9ceb31STejun Heo while (type->cnt + 2 > type->max) 74248c3b583SGreg Pearson if (memblock_double_array(type, base, size) < 0) 7436a9ceb31STejun Heo return -ENOMEM; 7446a9ceb31STejun Heo 74566e8b438SGioh Kim for_each_memblock_type(idx, type, rgn) { 7466a9ceb31STejun Heo phys_addr_t rbase = rgn->base; 7476a9ceb31STejun Heo phys_addr_t rend = rbase + rgn->size; 7486a9ceb31STejun Heo 7496a9ceb31STejun Heo if (rbase >= end) 7506a9ceb31STejun Heo break; 7516a9ceb31STejun Heo if (rend <= base) 7526a9ceb31STejun Heo continue; 7536a9ceb31STejun Heo 7546a9ceb31STejun Heo if (rbase < base) { 7556a9ceb31STejun Heo /* 7566a9ceb31STejun Heo * @rgn intersects from below. Split and continue 7576a9ceb31STejun Heo * to process the next region - the new top half. 7586a9ceb31STejun Heo */ 7596a9ceb31STejun Heo rgn->base = base; 7601440c4e2STejun Heo rgn->size -= base - rbase; 7611440c4e2STejun Heo type->total_size -= base - rbase; 7628c9c1701SAlexander Kuleshov memblock_insert_region(type, idx, rbase, base - rbase, 76366a20757STang Chen memblock_get_region_node(rgn), 76466a20757STang Chen rgn->flags); 7656a9ceb31STejun Heo } else if (rend > end) { 7666a9ceb31STejun Heo /* 7676a9ceb31STejun Heo * @rgn intersects from above. Split and redo the 7686a9ceb31STejun Heo * current region - the new bottom half. 7696a9ceb31STejun Heo */ 7706a9ceb31STejun Heo rgn->base = end; 7711440c4e2STejun Heo rgn->size -= end - rbase; 7721440c4e2STejun Heo type->total_size -= end - rbase; 7738c9c1701SAlexander Kuleshov memblock_insert_region(type, idx--, rbase, end - rbase, 77466a20757STang Chen memblock_get_region_node(rgn), 77566a20757STang Chen rgn->flags); 7766a9ceb31STejun Heo } else { 7776a9ceb31STejun Heo /* @rgn is fully contained, record it */ 7786a9ceb31STejun Heo if (!*end_rgn) 7798c9c1701SAlexander Kuleshov *start_rgn = idx; 7808c9c1701SAlexander Kuleshov *end_rgn = idx + 1; 7816a9ceb31STejun Heo } 7826a9ceb31STejun Heo } 7836a9ceb31STejun Heo 7846a9ceb31STejun Heo return 0; 7856a9ceb31STejun Heo } 7866a9ceb31STejun Heo 78735bd16a2SAlexander Kuleshov static int __init_memblock memblock_remove_range(struct memblock_type *type, 7888f7a6605SBenjamin Herrenschmidt phys_addr_t base, phys_addr_t size) 78995f72d1eSYinghai Lu { 79071936180STejun Heo int start_rgn, end_rgn; 79171936180STejun Heo int i, ret; 79295f72d1eSYinghai Lu 79371936180STejun Heo ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 79471936180STejun Heo if (ret) 79571936180STejun Heo return ret; 79695f72d1eSYinghai Lu 79771936180STejun Heo for (i = end_rgn - 1; i >= start_rgn; i--) 79871936180STejun Heo memblock_remove_region(type, i); 79995f72d1eSYinghai Lu return 0; 80095f72d1eSYinghai Lu } 80195f72d1eSYinghai Lu 802581adcbeSTejun Heo int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size) 80395f72d1eSYinghai Lu { 80425cf23d7SMinchan Kim phys_addr_t end = base + size - 1; 80525cf23d7SMinchan Kim 80625cf23d7SMinchan Kim memblock_dbg("memblock_remove: [%pa-%pa] %pS\n", 80725cf23d7SMinchan Kim &base, &end, (void *)_RET_IP_); 80825cf23d7SMinchan Kim 809f1af9d3aSPhilipp Hachtmann return memblock_remove_range(&memblock.memory, base, size); 81095f72d1eSYinghai Lu } 81195f72d1eSYinghai Lu 8124d72868cSMike Rapoport /** 8134d72868cSMike Rapoport * memblock_free - free boot memory block 8144d72868cSMike Rapoport * @base: phys starting address of the boot memory block 8154d72868cSMike Rapoport * @size: size of the boot memory block in bytes 8164d72868cSMike Rapoport * 8174d72868cSMike Rapoport * Free boot memory block previously allocated by memblock_alloc_xx() API. 8184d72868cSMike Rapoport * The freeing memory will not be released to the buddy allocator. 8194d72868cSMike Rapoport */ 820581adcbeSTejun Heo int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size) 82195f72d1eSYinghai Lu { 8225d63f81cSMiles Chen phys_addr_t end = base + size - 1; 8235d63f81cSMiles Chen 824d75f773cSSakari Ailus memblock_dbg(" memblock_free: [%pa-%pa] %pS\n", 8255d63f81cSMiles Chen &base, &end, (void *)_RET_IP_); 82624aa0788STejun Heo 8279099daedSCatalin Marinas kmemleak_free_part_phys(base, size); 828f1af9d3aSPhilipp Hachtmann return memblock_remove_range(&memblock.reserved, base, size); 82995f72d1eSYinghai Lu } 83095f72d1eSYinghai Lu 831f705ac4bSAlexander Kuleshov int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) 83295f72d1eSYinghai Lu { 8335d63f81cSMiles Chen phys_addr_t end = base + size - 1; 8345d63f81cSMiles Chen 835d75f773cSSakari Ailus memblock_dbg("memblock_reserve: [%pa-%pa] %pS\n", 8365d63f81cSMiles Chen &base, &end, (void *)_RET_IP_); 83795f72d1eSYinghai Lu 838f705ac4bSAlexander Kuleshov return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0); 83995f72d1eSYinghai Lu } 84095f72d1eSYinghai Lu 84135fd0808STejun Heo /** 84247cec443SMike Rapoport * memblock_setclr_flag - set or clear flag for a memory region 84347cec443SMike Rapoport * @base: base address of the region 84447cec443SMike Rapoport * @size: size of the region 84547cec443SMike Rapoport * @set: set or clear the flag 84647cec443SMike Rapoport * @flag: the flag to udpate 84766b16edfSTang Chen * 8484308ce17STony Luck * This function isolates region [@base, @base + @size), and sets/clears flag 84966b16edfSTang Chen * 85047cec443SMike Rapoport * Return: 0 on success, -errno on failure. 85166b16edfSTang Chen */ 8524308ce17STony Luck static int __init_memblock memblock_setclr_flag(phys_addr_t base, 8534308ce17STony Luck phys_addr_t size, int set, int flag) 85466b16edfSTang Chen { 85566b16edfSTang Chen struct memblock_type *type = &memblock.memory; 85666b16edfSTang Chen int i, ret, start_rgn, end_rgn; 85766b16edfSTang Chen 85866b16edfSTang Chen ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 85966b16edfSTang Chen if (ret) 86066b16edfSTang Chen return ret; 86166b16edfSTang Chen 862fe145124SMike Rapoport for (i = start_rgn; i < end_rgn; i++) { 863fe145124SMike Rapoport struct memblock_region *r = &type->regions[i]; 864fe145124SMike Rapoport 8654308ce17STony Luck if (set) 866fe145124SMike Rapoport r->flags |= flag; 8674308ce17STony Luck else 868fe145124SMike Rapoport r->flags &= ~flag; 869fe145124SMike Rapoport } 87066b16edfSTang Chen 87166b16edfSTang Chen memblock_merge_regions(type); 87266b16edfSTang Chen return 0; 87366b16edfSTang Chen } 87466b16edfSTang Chen 87566b16edfSTang Chen /** 8764308ce17STony Luck * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG. 8774308ce17STony Luck * @base: the base phys addr of the region 8784308ce17STony Luck * @size: the size of the region 8794308ce17STony Luck * 88047cec443SMike Rapoport * Return: 0 on success, -errno on failure. 8814308ce17STony Luck */ 8824308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size) 8834308ce17STony Luck { 8844308ce17STony Luck return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG); 8854308ce17STony Luck } 8864308ce17STony Luck 8874308ce17STony Luck /** 88866b16edfSTang Chen * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region. 88966b16edfSTang Chen * @base: the base phys addr of the region 89066b16edfSTang Chen * @size: the size of the region 89166b16edfSTang Chen * 89247cec443SMike Rapoport * Return: 0 on success, -errno on failure. 89366b16edfSTang Chen */ 89466b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size) 89566b16edfSTang Chen { 8964308ce17STony Luck return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG); 89766b16edfSTang Chen } 89866b16edfSTang Chen 89966b16edfSTang Chen /** 900a3f5bafcSTony Luck * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR. 901a3f5bafcSTony Luck * @base: the base phys addr of the region 902a3f5bafcSTony Luck * @size: the size of the region 903a3f5bafcSTony Luck * 90447cec443SMike Rapoport * Return: 0 on success, -errno on failure. 905a3f5bafcSTony Luck */ 906a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size) 907a3f5bafcSTony Luck { 908a3f5bafcSTony Luck system_has_some_mirror = true; 909a3f5bafcSTony Luck 910a3f5bafcSTony Luck return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR); 911a3f5bafcSTony Luck } 912a3f5bafcSTony Luck 913bf3d3cc5SArd Biesheuvel /** 914bf3d3cc5SArd Biesheuvel * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP. 915bf3d3cc5SArd Biesheuvel * @base: the base phys addr of the region 916bf3d3cc5SArd Biesheuvel * @size: the size of the region 917bf3d3cc5SArd Biesheuvel * 91847cec443SMike Rapoport * Return: 0 on success, -errno on failure. 919bf3d3cc5SArd Biesheuvel */ 920bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size) 921bf3d3cc5SArd Biesheuvel { 922bf3d3cc5SArd Biesheuvel return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP); 923bf3d3cc5SArd Biesheuvel } 924a3f5bafcSTony Luck 925a3f5bafcSTony Luck /** 9264c546b8aSAKASHI Takahiro * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region. 9274c546b8aSAKASHI Takahiro * @base: the base phys addr of the region 9284c546b8aSAKASHI Takahiro * @size: the size of the region 9294c546b8aSAKASHI Takahiro * 93047cec443SMike Rapoport * Return: 0 on success, -errno on failure. 9314c546b8aSAKASHI Takahiro */ 9324c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size) 9334c546b8aSAKASHI Takahiro { 9344c546b8aSAKASHI Takahiro return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP); 9354c546b8aSAKASHI Takahiro } 9364c546b8aSAKASHI Takahiro 9374c546b8aSAKASHI Takahiro /** 9388e7a7f86SRobin Holt * __next_reserved_mem_region - next function for for_each_reserved_region() 9398e7a7f86SRobin Holt * @idx: pointer to u64 loop variable 9408e7a7f86SRobin Holt * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL 9418e7a7f86SRobin Holt * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL 9428e7a7f86SRobin Holt * 9438e7a7f86SRobin Holt * Iterate over all reserved memory regions. 9448e7a7f86SRobin Holt */ 9458e7a7f86SRobin Holt void __init_memblock __next_reserved_mem_region(u64 *idx, 9468e7a7f86SRobin Holt phys_addr_t *out_start, 9478e7a7f86SRobin Holt phys_addr_t *out_end) 9488e7a7f86SRobin Holt { 949567d117bSAlexander Kuleshov struct memblock_type *type = &memblock.reserved; 9508e7a7f86SRobin Holt 951cd33a76bSRichard Leitner if (*idx < type->cnt) { 952567d117bSAlexander Kuleshov struct memblock_region *r = &type->regions[*idx]; 9538e7a7f86SRobin Holt phys_addr_t base = r->base; 9548e7a7f86SRobin Holt phys_addr_t size = r->size; 9558e7a7f86SRobin Holt 9568e7a7f86SRobin Holt if (out_start) 9578e7a7f86SRobin Holt *out_start = base; 9588e7a7f86SRobin Holt if (out_end) 9598e7a7f86SRobin Holt *out_end = base + size - 1; 9608e7a7f86SRobin Holt 9618e7a7f86SRobin Holt *idx += 1; 9628e7a7f86SRobin Holt return; 9638e7a7f86SRobin Holt } 9648e7a7f86SRobin Holt 9658e7a7f86SRobin Holt /* signal end of iteration */ 9668e7a7f86SRobin Holt *idx = ULLONG_MAX; 9678e7a7f86SRobin Holt } 9688e7a7f86SRobin Holt 969c9a688a3SMike Rapoport static bool should_skip_region(struct memblock_region *m, int nid, int flags) 970c9a688a3SMike Rapoport { 971c9a688a3SMike Rapoport int m_nid = memblock_get_region_node(m); 972c9a688a3SMike Rapoport 973c9a688a3SMike Rapoport /* only memory regions are associated with nodes, check it */ 974c9a688a3SMike Rapoport if (nid != NUMA_NO_NODE && nid != m_nid) 975c9a688a3SMike Rapoport return true; 976c9a688a3SMike Rapoport 977c9a688a3SMike Rapoport /* skip hotpluggable memory regions if needed */ 978c9a688a3SMike Rapoport if (movable_node_is_enabled() && memblock_is_hotpluggable(m)) 979c9a688a3SMike Rapoport return true; 980c9a688a3SMike Rapoport 981c9a688a3SMike Rapoport /* if we want mirror memory skip non-mirror memory regions */ 982c9a688a3SMike Rapoport if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) 983c9a688a3SMike Rapoport return true; 984c9a688a3SMike Rapoport 985c9a688a3SMike Rapoport /* skip nomap memory unless we were asked for it explicitly */ 986c9a688a3SMike Rapoport if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) 987c9a688a3SMike Rapoport return true; 988c9a688a3SMike Rapoport 989c9a688a3SMike Rapoport return false; 990c9a688a3SMike Rapoport } 991c9a688a3SMike Rapoport 9928e7a7f86SRobin Holt /** 993a2974133SMike Rapoport * __next_mem_range - next function for for_each_free_mem_range() etc. 99435fd0808STejun Heo * @idx: pointer to u64 loop variable 995b1154233SGrygorii Strashko * @nid: node selector, %NUMA_NO_NODE for all nodes 996fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 997f1af9d3aSPhilipp Hachtmann * @type_a: pointer to memblock_type from where the range is taken 998f1af9d3aSPhilipp Hachtmann * @type_b: pointer to memblock_type which excludes memory from being taken 999dad7557eSWanpeng Li * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 1000dad7557eSWanpeng Li * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 1001dad7557eSWanpeng Li * @out_nid: ptr to int for nid of the range, can be %NULL 100235fd0808STejun Heo * 1003f1af9d3aSPhilipp Hachtmann * Find the first area from *@idx which matches @nid, fill the out 100435fd0808STejun Heo * parameters, and update *@idx for the next iteration. The lower 32bit of 1005f1af9d3aSPhilipp Hachtmann * *@idx contains index into type_a and the upper 32bit indexes the 1006f1af9d3aSPhilipp Hachtmann * areas before each region in type_b. For example, if type_b regions 100735fd0808STejun Heo * look like the following, 100835fd0808STejun Heo * 100935fd0808STejun Heo * 0:[0-16), 1:[32-48), 2:[128-130) 101035fd0808STejun Heo * 101135fd0808STejun Heo * The upper 32bit indexes the following regions. 101235fd0808STejun Heo * 101335fd0808STejun Heo * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) 101435fd0808STejun Heo * 101535fd0808STejun Heo * As both region arrays are sorted, the function advances the two indices 101635fd0808STejun Heo * in lockstep and returns each intersection. 101735fd0808STejun Heo */ 1018e1720feeSMike Rapoport void __init_memblock __next_mem_range(u64 *idx, int nid, 1019e1720feeSMike Rapoport enum memblock_flags flags, 1020f1af9d3aSPhilipp Hachtmann struct memblock_type *type_a, 1021f1af9d3aSPhilipp Hachtmann struct memblock_type *type_b, 102235fd0808STejun Heo phys_addr_t *out_start, 102335fd0808STejun Heo phys_addr_t *out_end, int *out_nid) 102435fd0808STejun Heo { 1025f1af9d3aSPhilipp Hachtmann int idx_a = *idx & 0xffffffff; 1026f1af9d3aSPhilipp Hachtmann int idx_b = *idx >> 32; 1027b1154233SGrygorii Strashko 1028f1af9d3aSPhilipp Hachtmann if (WARN_ONCE(nid == MAX_NUMNODES, 1029f1af9d3aSPhilipp Hachtmann "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) 1030560dca27SGrygorii Strashko nid = NUMA_NO_NODE; 103135fd0808STejun Heo 1032f1af9d3aSPhilipp Hachtmann for (; idx_a < type_a->cnt; idx_a++) { 1033f1af9d3aSPhilipp Hachtmann struct memblock_region *m = &type_a->regions[idx_a]; 1034f1af9d3aSPhilipp Hachtmann 103535fd0808STejun Heo phys_addr_t m_start = m->base; 103635fd0808STejun Heo phys_addr_t m_end = m->base + m->size; 1037f1af9d3aSPhilipp Hachtmann int m_nid = memblock_get_region_node(m); 103835fd0808STejun Heo 1039c9a688a3SMike Rapoport if (should_skip_region(m, nid, flags)) 1040bf3d3cc5SArd Biesheuvel continue; 1041bf3d3cc5SArd Biesheuvel 1042f1af9d3aSPhilipp Hachtmann if (!type_b) { 1043f1af9d3aSPhilipp Hachtmann if (out_start) 1044f1af9d3aSPhilipp Hachtmann *out_start = m_start; 1045f1af9d3aSPhilipp Hachtmann if (out_end) 1046f1af9d3aSPhilipp Hachtmann *out_end = m_end; 1047f1af9d3aSPhilipp Hachtmann if (out_nid) 1048f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 1049f1af9d3aSPhilipp Hachtmann idx_a++; 1050f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 1051f1af9d3aSPhilipp Hachtmann return; 1052f1af9d3aSPhilipp Hachtmann } 105335fd0808STejun Heo 1054f1af9d3aSPhilipp Hachtmann /* scan areas before each reservation */ 1055f1af9d3aSPhilipp Hachtmann for (; idx_b < type_b->cnt + 1; idx_b++) { 1056f1af9d3aSPhilipp Hachtmann struct memblock_region *r; 1057f1af9d3aSPhilipp Hachtmann phys_addr_t r_start; 1058f1af9d3aSPhilipp Hachtmann phys_addr_t r_end; 1059f1af9d3aSPhilipp Hachtmann 1060f1af9d3aSPhilipp Hachtmann r = &type_b->regions[idx_b]; 1061f1af9d3aSPhilipp Hachtmann r_start = idx_b ? r[-1].base + r[-1].size : 0; 1062f1af9d3aSPhilipp Hachtmann r_end = idx_b < type_b->cnt ? 10631c4bc43dSStefan Agner r->base : PHYS_ADDR_MAX; 1064f1af9d3aSPhilipp Hachtmann 1065f1af9d3aSPhilipp Hachtmann /* 1066f1af9d3aSPhilipp Hachtmann * if idx_b advanced past idx_a, 1067f1af9d3aSPhilipp Hachtmann * break out to advance idx_a 1068f1af9d3aSPhilipp Hachtmann */ 106935fd0808STejun Heo if (r_start >= m_end) 107035fd0808STejun Heo break; 107135fd0808STejun Heo /* if the two regions intersect, we're done */ 107235fd0808STejun Heo if (m_start < r_end) { 107335fd0808STejun Heo if (out_start) 1074f1af9d3aSPhilipp Hachtmann *out_start = 1075f1af9d3aSPhilipp Hachtmann max(m_start, r_start); 107635fd0808STejun Heo if (out_end) 107735fd0808STejun Heo *out_end = min(m_end, r_end); 107835fd0808STejun Heo if (out_nid) 1079f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 108035fd0808STejun Heo /* 1081f1af9d3aSPhilipp Hachtmann * The region which ends first is 1082f1af9d3aSPhilipp Hachtmann * advanced for the next iteration. 108335fd0808STejun Heo */ 108435fd0808STejun Heo if (m_end <= r_end) 1085f1af9d3aSPhilipp Hachtmann idx_a++; 108635fd0808STejun Heo else 1087f1af9d3aSPhilipp Hachtmann idx_b++; 1088f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 108935fd0808STejun Heo return; 109035fd0808STejun Heo } 109135fd0808STejun Heo } 109235fd0808STejun Heo } 109335fd0808STejun Heo 109435fd0808STejun Heo /* signal end of iteration */ 109535fd0808STejun Heo *idx = ULLONG_MAX; 109635fd0808STejun Heo } 109735fd0808STejun Heo 10987bd0b0f0STejun Heo /** 1099f1af9d3aSPhilipp Hachtmann * __next_mem_range_rev - generic next function for for_each_*_range_rev() 1100f1af9d3aSPhilipp Hachtmann * 11017bd0b0f0STejun Heo * @idx: pointer to u64 loop variable 1102ad5ea8cdSAlexander Kuleshov * @nid: node selector, %NUMA_NO_NODE for all nodes 1103fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 1104f1af9d3aSPhilipp Hachtmann * @type_a: pointer to memblock_type from where the range is taken 1105f1af9d3aSPhilipp Hachtmann * @type_b: pointer to memblock_type which excludes memory from being taken 1106dad7557eSWanpeng Li * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 1107dad7557eSWanpeng Li * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 1108dad7557eSWanpeng Li * @out_nid: ptr to int for nid of the range, can be %NULL 11097bd0b0f0STejun Heo * 111047cec443SMike Rapoport * Finds the next range from type_a which is not marked as unsuitable 111147cec443SMike Rapoport * in type_b. 111247cec443SMike Rapoport * 1113f1af9d3aSPhilipp Hachtmann * Reverse of __next_mem_range(). 11147bd0b0f0STejun Heo */ 1115e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid, 1116e1720feeSMike Rapoport enum memblock_flags flags, 1117f1af9d3aSPhilipp Hachtmann struct memblock_type *type_a, 1118f1af9d3aSPhilipp Hachtmann struct memblock_type *type_b, 11197bd0b0f0STejun Heo phys_addr_t *out_start, 11207bd0b0f0STejun Heo phys_addr_t *out_end, int *out_nid) 11217bd0b0f0STejun Heo { 1122f1af9d3aSPhilipp Hachtmann int idx_a = *idx & 0xffffffff; 1123f1af9d3aSPhilipp Hachtmann int idx_b = *idx >> 32; 1124b1154233SGrygorii Strashko 1125560dca27SGrygorii Strashko if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) 1126560dca27SGrygorii Strashko nid = NUMA_NO_NODE; 11277bd0b0f0STejun Heo 11287bd0b0f0STejun Heo if (*idx == (u64)ULLONG_MAX) { 1129f1af9d3aSPhilipp Hachtmann idx_a = type_a->cnt - 1; 1130e47608abSzijun_hu if (type_b != NULL) 1131f1af9d3aSPhilipp Hachtmann idx_b = type_b->cnt; 1132e47608abSzijun_hu else 1133e47608abSzijun_hu idx_b = 0; 11347bd0b0f0STejun Heo } 11357bd0b0f0STejun Heo 1136f1af9d3aSPhilipp Hachtmann for (; idx_a >= 0; idx_a--) { 1137f1af9d3aSPhilipp Hachtmann struct memblock_region *m = &type_a->regions[idx_a]; 1138f1af9d3aSPhilipp Hachtmann 11397bd0b0f0STejun Heo phys_addr_t m_start = m->base; 11407bd0b0f0STejun Heo phys_addr_t m_end = m->base + m->size; 1141f1af9d3aSPhilipp Hachtmann int m_nid = memblock_get_region_node(m); 11427bd0b0f0STejun Heo 1143c9a688a3SMike Rapoport if (should_skip_region(m, nid, flags)) 1144bf3d3cc5SArd Biesheuvel continue; 1145bf3d3cc5SArd Biesheuvel 1146f1af9d3aSPhilipp Hachtmann if (!type_b) { 1147f1af9d3aSPhilipp Hachtmann if (out_start) 1148f1af9d3aSPhilipp Hachtmann *out_start = m_start; 1149f1af9d3aSPhilipp Hachtmann if (out_end) 1150f1af9d3aSPhilipp Hachtmann *out_end = m_end; 1151f1af9d3aSPhilipp Hachtmann if (out_nid) 1152f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 1153fb399b48Szijun_hu idx_a--; 1154f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 1155f1af9d3aSPhilipp Hachtmann return; 1156f1af9d3aSPhilipp Hachtmann } 11577bd0b0f0STejun Heo 1158f1af9d3aSPhilipp Hachtmann /* scan areas before each reservation */ 1159f1af9d3aSPhilipp Hachtmann for (; idx_b >= 0; idx_b--) { 1160f1af9d3aSPhilipp Hachtmann struct memblock_region *r; 1161f1af9d3aSPhilipp Hachtmann phys_addr_t r_start; 1162f1af9d3aSPhilipp Hachtmann phys_addr_t r_end; 1163f1af9d3aSPhilipp Hachtmann 1164f1af9d3aSPhilipp Hachtmann r = &type_b->regions[idx_b]; 1165f1af9d3aSPhilipp Hachtmann r_start = idx_b ? r[-1].base + r[-1].size : 0; 1166f1af9d3aSPhilipp Hachtmann r_end = idx_b < type_b->cnt ? 11671c4bc43dSStefan Agner r->base : PHYS_ADDR_MAX; 1168f1af9d3aSPhilipp Hachtmann /* 1169f1af9d3aSPhilipp Hachtmann * if idx_b advanced past idx_a, 1170f1af9d3aSPhilipp Hachtmann * break out to advance idx_a 1171f1af9d3aSPhilipp Hachtmann */ 1172f1af9d3aSPhilipp Hachtmann 11737bd0b0f0STejun Heo if (r_end <= m_start) 11747bd0b0f0STejun Heo break; 11757bd0b0f0STejun Heo /* if the two regions intersect, we're done */ 11767bd0b0f0STejun Heo if (m_end > r_start) { 11777bd0b0f0STejun Heo if (out_start) 11787bd0b0f0STejun Heo *out_start = max(m_start, r_start); 11797bd0b0f0STejun Heo if (out_end) 11807bd0b0f0STejun Heo *out_end = min(m_end, r_end); 11817bd0b0f0STejun Heo if (out_nid) 1182f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 11837bd0b0f0STejun Heo if (m_start >= r_start) 1184f1af9d3aSPhilipp Hachtmann idx_a--; 11857bd0b0f0STejun Heo else 1186f1af9d3aSPhilipp Hachtmann idx_b--; 1187f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 11887bd0b0f0STejun Heo return; 11897bd0b0f0STejun Heo } 11907bd0b0f0STejun Heo } 11917bd0b0f0STejun Heo } 1192f1af9d3aSPhilipp Hachtmann /* signal end of iteration */ 11937bd0b0f0STejun Heo *idx = ULLONG_MAX; 11947bd0b0f0STejun Heo } 11957bd0b0f0STejun Heo 11967c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 11977c0caeb8STejun Heo /* 119845e79815SChen Chang * Common iterator interface used to define for_each_mem_pfn_range(). 11997c0caeb8STejun Heo */ 12007c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid, 12017c0caeb8STejun Heo unsigned long *out_start_pfn, 12027c0caeb8STejun Heo unsigned long *out_end_pfn, int *out_nid) 12037c0caeb8STejun Heo { 12047c0caeb8STejun Heo struct memblock_type *type = &memblock.memory; 12057c0caeb8STejun Heo struct memblock_region *r; 12067c0caeb8STejun Heo 12077c0caeb8STejun Heo while (++*idx < type->cnt) { 12087c0caeb8STejun Heo r = &type->regions[*idx]; 12097c0caeb8STejun Heo 12107c0caeb8STejun Heo if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) 12117c0caeb8STejun Heo continue; 12127c0caeb8STejun Heo if (nid == MAX_NUMNODES || nid == r->nid) 12137c0caeb8STejun Heo break; 12147c0caeb8STejun Heo } 12157c0caeb8STejun Heo if (*idx >= type->cnt) { 12167c0caeb8STejun Heo *idx = -1; 12177c0caeb8STejun Heo return; 12187c0caeb8STejun Heo } 12197c0caeb8STejun Heo 12207c0caeb8STejun Heo if (out_start_pfn) 12217c0caeb8STejun Heo *out_start_pfn = PFN_UP(r->base); 12227c0caeb8STejun Heo if (out_end_pfn) 12237c0caeb8STejun Heo *out_end_pfn = PFN_DOWN(r->base + r->size); 12247c0caeb8STejun Heo if (out_nid) 12257c0caeb8STejun Heo *out_nid = r->nid; 12267c0caeb8STejun Heo } 12277c0caeb8STejun Heo 12287c0caeb8STejun Heo /** 12297c0caeb8STejun Heo * memblock_set_node - set node ID on memblock regions 12307c0caeb8STejun Heo * @base: base of area to set node ID for 12317c0caeb8STejun Heo * @size: size of area to set node ID for 1232e7e8de59STang Chen * @type: memblock type to set node ID for 12337c0caeb8STejun Heo * @nid: node ID to set 12347c0caeb8STejun Heo * 1235e7e8de59STang Chen * Set the nid of memblock @type regions in [@base, @base + @size) to @nid. 12367c0caeb8STejun Heo * Regions which cross the area boundaries are split as necessary. 12377c0caeb8STejun Heo * 123847cec443SMike Rapoport * Return: 12397c0caeb8STejun Heo * 0 on success, -errno on failure. 12407c0caeb8STejun Heo */ 12417c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, 1242e7e8de59STang Chen struct memblock_type *type, int nid) 12437c0caeb8STejun Heo { 12446a9ceb31STejun Heo int start_rgn, end_rgn; 12456a9ceb31STejun Heo int i, ret; 12467c0caeb8STejun Heo 12476a9ceb31STejun Heo ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 12486a9ceb31STejun Heo if (ret) 12496a9ceb31STejun Heo return ret; 12507c0caeb8STejun Heo 12516a9ceb31STejun Heo for (i = start_rgn; i < end_rgn; i++) 1252e9d24ad3SWanpeng Li memblock_set_region_node(&type->regions[i], nid); 12537c0caeb8STejun Heo 12547c0caeb8STejun Heo memblock_merge_regions(type); 12557c0caeb8STejun Heo return 0; 12567c0caeb8STejun Heo } 12577c0caeb8STejun Heo #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ 1258837566e7SAlexander Duyck #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 1259837566e7SAlexander Duyck /** 1260837566e7SAlexander Duyck * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone() 1261837566e7SAlexander Duyck * 1262837566e7SAlexander Duyck * @idx: pointer to u64 loop variable 1263837566e7SAlexander Duyck * @zone: zone in which all of the memory blocks reside 1264837566e7SAlexander Duyck * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL 1265837566e7SAlexander Duyck * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL 1266837566e7SAlexander Duyck * 1267837566e7SAlexander Duyck * This function is meant to be a zone/pfn specific wrapper for the 1268837566e7SAlexander Duyck * for_each_mem_range type iterators. Specifically they are used in the 1269837566e7SAlexander Duyck * deferred memory init routines and as such we were duplicating much of 1270837566e7SAlexander Duyck * this logic throughout the code. So instead of having it in multiple 1271837566e7SAlexander Duyck * locations it seemed like it would make more sense to centralize this to 1272837566e7SAlexander Duyck * one new iterator that does everything they need. 1273837566e7SAlexander Duyck */ 1274837566e7SAlexander Duyck void __init_memblock 1275837566e7SAlexander Duyck __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone, 1276837566e7SAlexander Duyck unsigned long *out_spfn, unsigned long *out_epfn) 1277837566e7SAlexander Duyck { 1278837566e7SAlexander Duyck int zone_nid = zone_to_nid(zone); 1279837566e7SAlexander Duyck phys_addr_t spa, epa; 1280837566e7SAlexander Duyck int nid; 1281837566e7SAlexander Duyck 1282837566e7SAlexander Duyck __next_mem_range(idx, zone_nid, MEMBLOCK_NONE, 1283837566e7SAlexander Duyck &memblock.memory, &memblock.reserved, 1284837566e7SAlexander Duyck &spa, &epa, &nid); 1285837566e7SAlexander Duyck 1286837566e7SAlexander Duyck while (*idx != U64_MAX) { 1287837566e7SAlexander Duyck unsigned long epfn = PFN_DOWN(epa); 1288837566e7SAlexander Duyck unsigned long spfn = PFN_UP(spa); 1289837566e7SAlexander Duyck 1290837566e7SAlexander Duyck /* 1291837566e7SAlexander Duyck * Verify the end is at least past the start of the zone and 1292837566e7SAlexander Duyck * that we have at least one PFN to initialize. 1293837566e7SAlexander Duyck */ 1294837566e7SAlexander Duyck if (zone->zone_start_pfn < epfn && spfn < epfn) { 1295837566e7SAlexander Duyck /* if we went too far just stop searching */ 1296837566e7SAlexander Duyck if (zone_end_pfn(zone) <= spfn) { 1297837566e7SAlexander Duyck *idx = U64_MAX; 1298837566e7SAlexander Duyck break; 1299837566e7SAlexander Duyck } 1300837566e7SAlexander Duyck 1301837566e7SAlexander Duyck if (out_spfn) 1302837566e7SAlexander Duyck *out_spfn = max(zone->zone_start_pfn, spfn); 1303837566e7SAlexander Duyck if (out_epfn) 1304837566e7SAlexander Duyck *out_epfn = min(zone_end_pfn(zone), epfn); 1305837566e7SAlexander Duyck 1306837566e7SAlexander Duyck return; 1307837566e7SAlexander Duyck } 1308837566e7SAlexander Duyck 1309837566e7SAlexander Duyck __next_mem_range(idx, zone_nid, MEMBLOCK_NONE, 1310837566e7SAlexander Duyck &memblock.memory, &memblock.reserved, 1311837566e7SAlexander Duyck &spa, &epa, &nid); 1312837566e7SAlexander Duyck } 1313837566e7SAlexander Duyck 1314837566e7SAlexander Duyck /* signal end of iteration */ 1315837566e7SAlexander Duyck if (out_spfn) 1316837566e7SAlexander Duyck *out_spfn = ULONG_MAX; 1317837566e7SAlexander Duyck if (out_epfn) 1318837566e7SAlexander Duyck *out_epfn = 0; 1319837566e7SAlexander Duyck } 1320837566e7SAlexander Duyck 1321837566e7SAlexander Duyck #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 13227c0caeb8STejun Heo 132392d12f95SMike Rapoport /** 132492d12f95SMike Rapoport * memblock_alloc_range_nid - allocate boot memory block 132592d12f95SMike Rapoport * @size: size of memory block to be allocated in bytes 132692d12f95SMike Rapoport * @align: alignment of the region and block's size 132792d12f95SMike Rapoport * @start: the lower bound of the memory region to allocate (phys address) 132892d12f95SMike Rapoport * @end: the upper bound of the memory region to allocate (phys address) 132992d12f95SMike Rapoport * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 133092d12f95SMike Rapoport * 133192d12f95SMike Rapoport * The allocation is performed from memory region limited by 133292d12f95SMike Rapoport * memblock.current_limit if @max_addr == %MEMBLOCK_ALLOC_ACCESSIBLE. 133392d12f95SMike Rapoport * 133492d12f95SMike Rapoport * If the specified node can not hold the requested memory the 133592d12f95SMike Rapoport * allocation falls back to any node in the system 133692d12f95SMike Rapoport * 133792d12f95SMike Rapoport * For systems with memory mirroring, the allocation is attempted first 133892d12f95SMike Rapoport * from the regions with mirroring enabled and then retried from any 133992d12f95SMike Rapoport * memory region. 134092d12f95SMike Rapoport * 134192d12f95SMike Rapoport * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for 134292d12f95SMike Rapoport * allocated boot memory block, so that it is never reported as leaks. 134392d12f95SMike Rapoport * 134492d12f95SMike Rapoport * Return: 134592d12f95SMike Rapoport * Physical address of allocated memory block on success, %0 on failure. 134692d12f95SMike Rapoport */ 13472bfc2862SAkinobu Mita static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, 13482bfc2862SAkinobu Mita phys_addr_t align, phys_addr_t start, 134992d12f95SMike Rapoport phys_addr_t end, int nid) 135095f72d1eSYinghai Lu { 135192d12f95SMike Rapoport enum memblock_flags flags = choose_memblock_flags(); 13526ed311b2SBenjamin Herrenschmidt phys_addr_t found; 135395f72d1eSYinghai Lu 135492d12f95SMike Rapoport if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) 135592d12f95SMike Rapoport nid = NUMA_NO_NODE; 135692d12f95SMike Rapoport 13572f770806SMike Rapoport if (!align) { 13582f770806SMike Rapoport /* Can't use WARNs this early in boot on powerpc */ 13592f770806SMike Rapoport dump_stack(); 13602f770806SMike Rapoport align = SMP_CACHE_BYTES; 13612f770806SMike Rapoport } 13622f770806SMike Rapoport 136392d12f95SMike Rapoport if (end > memblock.current_limit) 136492d12f95SMike Rapoport end = memblock.current_limit; 136592d12f95SMike Rapoport 136692d12f95SMike Rapoport again: 1367fc6daaf9STony Luck found = memblock_find_in_range_node(size, align, start, end, nid, 1368fc6daaf9STony Luck flags); 136992d12f95SMike Rapoport if (found && !memblock_reserve(found, size)) 137092d12f95SMike Rapoport goto done; 137192d12f95SMike Rapoport 137292d12f95SMike Rapoport if (nid != NUMA_NO_NODE) { 137392d12f95SMike Rapoport found = memblock_find_in_range_node(size, align, start, 137492d12f95SMike Rapoport end, NUMA_NO_NODE, 137592d12f95SMike Rapoport flags); 137692d12f95SMike Rapoport if (found && !memblock_reserve(found, size)) 137792d12f95SMike Rapoport goto done; 137892d12f95SMike Rapoport } 137992d12f95SMike Rapoport 138092d12f95SMike Rapoport if (flags & MEMBLOCK_MIRROR) { 138192d12f95SMike Rapoport flags &= ~MEMBLOCK_MIRROR; 138292d12f95SMike Rapoport pr_warn("Could not allocate %pap bytes of mirrored memory\n", 138392d12f95SMike Rapoport &size); 138492d12f95SMike Rapoport goto again; 138592d12f95SMike Rapoport } 138692d12f95SMike Rapoport 138792d12f95SMike Rapoport return 0; 138892d12f95SMike Rapoport 138992d12f95SMike Rapoport done: 139092d12f95SMike Rapoport /* Skip kmemleak for kasan_init() due to high volume. */ 139192d12f95SMike Rapoport if (end != MEMBLOCK_ALLOC_KASAN) 1392aedf95eaSCatalin Marinas /* 139392d12f95SMike Rapoport * The min_count is set to 0 so that memblock allocated 139492d12f95SMike Rapoport * blocks are never reported as leaks. This is because many 139592d12f95SMike Rapoport * of these blocks are only referred via the physical 139692d12f95SMike Rapoport * address which is not looked up by kmemleak. 1397aedf95eaSCatalin Marinas */ 13989099daedSCatalin Marinas kmemleak_alloc_phys(found, size, 0, 0); 139992d12f95SMike Rapoport 14006ed311b2SBenjamin Herrenschmidt return found; 1401aedf95eaSCatalin Marinas } 140295f72d1eSYinghai Lu 1403a2974133SMike Rapoport /** 1404a2974133SMike Rapoport * memblock_phys_alloc_range - allocate a memory block inside specified range 1405a2974133SMike Rapoport * @size: size of memory block to be allocated in bytes 1406a2974133SMike Rapoport * @align: alignment of the region and block's size 1407a2974133SMike Rapoport * @start: the lower bound of the memory region to allocate (physical address) 1408a2974133SMike Rapoport * @end: the upper bound of the memory region to allocate (physical address) 1409a2974133SMike Rapoport * 1410a2974133SMike Rapoport * Allocate @size bytes in the between @start and @end. 1411a2974133SMike Rapoport * 1412a2974133SMike Rapoport * Return: physical address of the allocated memory block on success, 1413a2974133SMike Rapoport * %0 on failure. 1414a2974133SMike Rapoport */ 14158a770c2aSMike Rapoport phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size, 14168a770c2aSMike Rapoport phys_addr_t align, 14178a770c2aSMike Rapoport phys_addr_t start, 14188a770c2aSMike Rapoport phys_addr_t end) 14192bfc2862SAkinobu Mita { 142092d12f95SMike Rapoport return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE); 14217bd0b0f0STejun Heo } 14227bd0b0f0STejun Heo 1423a2974133SMike Rapoport /** 1424a2974133SMike Rapoport * memblock_phys_alloc_try_nid - allocate a memory block from specified MUMA node 1425a2974133SMike Rapoport * @size: size of memory block to be allocated in bytes 1426a2974133SMike Rapoport * @align: alignment of the region and block's size 1427a2974133SMike Rapoport * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1428a2974133SMike Rapoport * 1429a2974133SMike Rapoport * Allocates memory block from the specified NUMA node. If the node 1430a2974133SMike Rapoport * has no available memory, attempts to allocated from any node in the 1431a2974133SMike Rapoport * system. 1432a2974133SMike Rapoport * 1433a2974133SMike Rapoport * Return: physical address of the allocated memory block on success, 1434a2974133SMike Rapoport * %0 on failure. 1435a2974133SMike Rapoport */ 14369a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) 14379d1e2492SBenjamin Herrenschmidt { 143833755574SMike Rapoport return memblock_alloc_range_nid(size, align, 0, 143992d12f95SMike Rapoport MEMBLOCK_ALLOC_ACCESSIBLE, nid); 144095f72d1eSYinghai Lu } 144195f72d1eSYinghai Lu 144226f09e9bSSantosh Shilimkar /** 1443eb31d559SMike Rapoport * memblock_alloc_internal - allocate boot memory block 144426f09e9bSSantosh Shilimkar * @size: size of memory block to be allocated in bytes 144526f09e9bSSantosh Shilimkar * @align: alignment of the region and block's size 144626f09e9bSSantosh Shilimkar * @min_addr: the lower bound of the memory region to allocate (phys address) 144726f09e9bSSantosh Shilimkar * @max_addr: the upper bound of the memory region to allocate (phys address) 144826f09e9bSSantosh Shilimkar * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 144926f09e9bSSantosh Shilimkar * 145092d12f95SMike Rapoport * Allocates memory block using memblock_alloc_range_nid() and 145192d12f95SMike Rapoport * converts the returned physical address to virtual. 145292d12f95SMike Rapoport * 145326f09e9bSSantosh Shilimkar * The @min_addr limit is dropped if it can not be satisfied and the allocation 145492d12f95SMike Rapoport * will fall back to memory below @min_addr. Other constraints, such 145592d12f95SMike Rapoport * as node and mirrored memory will be handled again in 145692d12f95SMike Rapoport * memblock_alloc_range_nid(). 145726f09e9bSSantosh Shilimkar * 145847cec443SMike Rapoport * Return: 145926f09e9bSSantosh Shilimkar * Virtual address of allocated memory block on success, NULL on failure. 146026f09e9bSSantosh Shilimkar */ 1461eb31d559SMike Rapoport static void * __init memblock_alloc_internal( 146226f09e9bSSantosh Shilimkar phys_addr_t size, phys_addr_t align, 146326f09e9bSSantosh Shilimkar phys_addr_t min_addr, phys_addr_t max_addr, 146426f09e9bSSantosh Shilimkar int nid) 146526f09e9bSSantosh Shilimkar { 146626f09e9bSSantosh Shilimkar phys_addr_t alloc; 146726f09e9bSSantosh Shilimkar 146826f09e9bSSantosh Shilimkar /* 146926f09e9bSSantosh Shilimkar * Detect any accidental use of these APIs after slab is ready, as at 147026f09e9bSSantosh Shilimkar * this moment memblock may be deinitialized already and its 1471c6ffc5caSMike Rapoport * internal data may be destroyed (after execution of memblock_free_all) 147226f09e9bSSantosh Shilimkar */ 147326f09e9bSSantosh Shilimkar if (WARN_ON_ONCE(slab_is_available())) 147426f09e9bSSantosh Shilimkar return kzalloc_node(size, GFP_NOWAIT, nid); 147526f09e9bSSantosh Shilimkar 147692d12f95SMike Rapoport alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid); 14772f770806SMike Rapoport 147892d12f95SMike Rapoport /* retry allocation without lower limit */ 147992d12f95SMike Rapoport if (!alloc && min_addr) 148092d12f95SMike Rapoport alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid); 148126f09e9bSSantosh Shilimkar 148292d12f95SMike Rapoport if (!alloc) 1483a3f5bafcSTony Luck return NULL; 148426f09e9bSSantosh Shilimkar 148592d12f95SMike Rapoport return phys_to_virt(alloc); 148626f09e9bSSantosh Shilimkar } 148726f09e9bSSantosh Shilimkar 148826f09e9bSSantosh Shilimkar /** 1489eb31d559SMike Rapoport * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing 1490ea1f5f37SPavel Tatashin * memory and without panicking 1491ea1f5f37SPavel Tatashin * @size: size of memory block to be allocated in bytes 1492ea1f5f37SPavel Tatashin * @align: alignment of the region and block's size 1493ea1f5f37SPavel Tatashin * @min_addr: the lower bound of the memory region from where the allocation 1494ea1f5f37SPavel Tatashin * is preferred (phys address) 1495ea1f5f37SPavel Tatashin * @max_addr: the upper bound of the memory region from where the allocation 149697ad1087SMike Rapoport * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 1497ea1f5f37SPavel Tatashin * allocate only from memory limited by memblock.current_limit value 1498ea1f5f37SPavel Tatashin * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1499ea1f5f37SPavel Tatashin * 1500ea1f5f37SPavel Tatashin * Public function, provides additional debug information (including caller 1501ea1f5f37SPavel Tatashin * info), if enabled. Does not zero allocated memory, does not panic if request 1502ea1f5f37SPavel Tatashin * cannot be satisfied. 1503ea1f5f37SPavel Tatashin * 150447cec443SMike Rapoport * Return: 1505ea1f5f37SPavel Tatashin * Virtual address of allocated memory block on success, NULL on failure. 1506ea1f5f37SPavel Tatashin */ 1507eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw( 1508ea1f5f37SPavel Tatashin phys_addr_t size, phys_addr_t align, 1509ea1f5f37SPavel Tatashin phys_addr_t min_addr, phys_addr_t max_addr, 1510ea1f5f37SPavel Tatashin int nid) 1511ea1f5f37SPavel Tatashin { 1512ea1f5f37SPavel Tatashin void *ptr; 1513ea1f5f37SPavel Tatashin 1514d75f773cSSakari Ailus memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1515a36aab89SMike Rapoport __func__, (u64)size, (u64)align, nid, &min_addr, 1516a36aab89SMike Rapoport &max_addr, (void *)_RET_IP_); 1517ea1f5f37SPavel Tatashin 1518eb31d559SMike Rapoport ptr = memblock_alloc_internal(size, align, 1519ea1f5f37SPavel Tatashin min_addr, max_addr, nid); 1520ea1f5f37SPavel Tatashin if (ptr && size > 0) 1521f682a97aSAlexander Duyck page_init_poison(ptr, size); 1522f682a97aSAlexander Duyck 1523ea1f5f37SPavel Tatashin return ptr; 1524ea1f5f37SPavel Tatashin } 1525ea1f5f37SPavel Tatashin 1526ea1f5f37SPavel Tatashin /** 1527c0dbe825SMike Rapoport * memblock_alloc_try_nid - allocate boot memory block 152826f09e9bSSantosh Shilimkar * @size: size of memory block to be allocated in bytes 152926f09e9bSSantosh Shilimkar * @align: alignment of the region and block's size 153026f09e9bSSantosh Shilimkar * @min_addr: the lower bound of the memory region from where the allocation 153126f09e9bSSantosh Shilimkar * is preferred (phys address) 153226f09e9bSSantosh Shilimkar * @max_addr: the upper bound of the memory region from where the allocation 153397ad1087SMike Rapoport * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 153426f09e9bSSantosh Shilimkar * allocate only from memory limited by memblock.current_limit value 153526f09e9bSSantosh Shilimkar * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 153626f09e9bSSantosh Shilimkar * 1537c0dbe825SMike Rapoport * Public function, provides additional debug information (including caller 1538c0dbe825SMike Rapoport * info), if enabled. This function zeroes the allocated memory. 153926f09e9bSSantosh Shilimkar * 154047cec443SMike Rapoport * Return: 154126f09e9bSSantosh Shilimkar * Virtual address of allocated memory block on success, NULL on failure. 154226f09e9bSSantosh Shilimkar */ 1543eb31d559SMike Rapoport void * __init memblock_alloc_try_nid( 154426f09e9bSSantosh Shilimkar phys_addr_t size, phys_addr_t align, 154526f09e9bSSantosh Shilimkar phys_addr_t min_addr, phys_addr_t max_addr, 154626f09e9bSSantosh Shilimkar int nid) 154726f09e9bSSantosh Shilimkar { 154826f09e9bSSantosh Shilimkar void *ptr; 154926f09e9bSSantosh Shilimkar 1550d75f773cSSakari Ailus memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1551a36aab89SMike Rapoport __func__, (u64)size, (u64)align, nid, &min_addr, 1552a36aab89SMike Rapoport &max_addr, (void *)_RET_IP_); 1553eb31d559SMike Rapoport ptr = memblock_alloc_internal(size, align, 155426f09e9bSSantosh Shilimkar min_addr, max_addr, nid); 1555c0dbe825SMike Rapoport if (ptr) 1556ea1f5f37SPavel Tatashin memset(ptr, 0, size); 155726f09e9bSSantosh Shilimkar 1558c0dbe825SMike Rapoport return ptr; 155926f09e9bSSantosh Shilimkar } 156026f09e9bSSantosh Shilimkar 156126f09e9bSSantosh Shilimkar /** 1562a2974133SMike Rapoport * __memblock_free_late - free pages directly to buddy allocator 156348a833ccSMike Rapoport * @base: phys starting address of the boot memory block 156426f09e9bSSantosh Shilimkar * @size: size of the boot memory block in bytes 156526f09e9bSSantosh Shilimkar * 1566a2974133SMike Rapoport * This is only useful when the memblock allocator has already been torn 156726f09e9bSSantosh Shilimkar * down, but we are still initializing the system. Pages are released directly 1568a2974133SMike Rapoport * to the buddy allocator. 156926f09e9bSSantosh Shilimkar */ 157026f09e9bSSantosh Shilimkar void __init __memblock_free_late(phys_addr_t base, phys_addr_t size) 157126f09e9bSSantosh Shilimkar { 1572a36aab89SMike Rapoport phys_addr_t cursor, end; 157326f09e9bSSantosh Shilimkar 1574a36aab89SMike Rapoport end = base + size - 1; 1575d75f773cSSakari Ailus memblock_dbg("%s: [%pa-%pa] %pS\n", 1576a36aab89SMike Rapoport __func__, &base, &end, (void *)_RET_IP_); 15779099daedSCatalin Marinas kmemleak_free_part_phys(base, size); 157826f09e9bSSantosh Shilimkar cursor = PFN_UP(base); 157926f09e9bSSantosh Shilimkar end = PFN_DOWN(base + size); 158026f09e9bSSantosh Shilimkar 158126f09e9bSSantosh Shilimkar for (; cursor < end; cursor++) { 15827c2ee349SMike Rapoport memblock_free_pages(pfn_to_page(cursor), cursor, 0); 1583ca79b0c2SArun KS totalram_pages_inc(); 158426f09e9bSSantosh Shilimkar } 158526f09e9bSSantosh Shilimkar } 15869d1e2492SBenjamin Herrenschmidt 15879d1e2492SBenjamin Herrenschmidt /* 15889d1e2492SBenjamin Herrenschmidt * Remaining API functions 15899d1e2492SBenjamin Herrenschmidt */ 15909d1e2492SBenjamin Herrenschmidt 15911f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void) 159295f72d1eSYinghai Lu { 15931440c4e2STejun Heo return memblock.memory.total_size; 159495f72d1eSYinghai Lu } 159595f72d1eSYinghai Lu 15968907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void) 15978907de5dSSrikar Dronamraju { 15988907de5dSSrikar Dronamraju return memblock.reserved.total_size; 15998907de5dSSrikar Dronamraju } 16008907de5dSSrikar Dronamraju 1601595ad9afSYinghai Lu phys_addr_t __init memblock_mem_size(unsigned long limit_pfn) 1602595ad9afSYinghai Lu { 1603595ad9afSYinghai Lu unsigned long pages = 0; 1604595ad9afSYinghai Lu struct memblock_region *r; 1605595ad9afSYinghai Lu unsigned long start_pfn, end_pfn; 1606595ad9afSYinghai Lu 1607595ad9afSYinghai Lu for_each_memblock(memory, r) { 1608595ad9afSYinghai Lu start_pfn = memblock_region_memory_base_pfn(r); 1609595ad9afSYinghai Lu end_pfn = memblock_region_memory_end_pfn(r); 1610595ad9afSYinghai Lu start_pfn = min_t(unsigned long, start_pfn, limit_pfn); 1611595ad9afSYinghai Lu end_pfn = min_t(unsigned long, end_pfn, limit_pfn); 1612595ad9afSYinghai Lu pages += end_pfn - start_pfn; 1613595ad9afSYinghai Lu } 1614595ad9afSYinghai Lu 161516763230SFabian Frederick return PFN_PHYS(pages); 1616595ad9afSYinghai Lu } 1617595ad9afSYinghai Lu 16180a93ebefSSam Ravnborg /* lowest address */ 16190a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void) 16200a93ebefSSam Ravnborg { 16210a93ebefSSam Ravnborg return memblock.memory.regions[0].base; 16220a93ebefSSam Ravnborg } 16230a93ebefSSam Ravnborg 162410d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void) 162595f72d1eSYinghai Lu { 162695f72d1eSYinghai Lu int idx = memblock.memory.cnt - 1; 162795f72d1eSYinghai Lu 1628e3239ff9SBenjamin Herrenschmidt return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); 162995f72d1eSYinghai Lu } 163095f72d1eSYinghai Lu 1631a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit) 163295f72d1eSYinghai Lu { 16331c4bc43dSStefan Agner phys_addr_t max_addr = PHYS_ADDR_MAX; 1634136199f0SEmil Medve struct memblock_region *r; 163595f72d1eSYinghai Lu 1636a571d4ebSDennis Chen /* 1637a571d4ebSDennis Chen * translate the memory @limit size into the max address within one of 1638a571d4ebSDennis Chen * the memory memblock regions, if the @limit exceeds the total size 16391c4bc43dSStefan Agner * of those regions, max_addr will keep original value PHYS_ADDR_MAX 1640a571d4ebSDennis Chen */ 1641136199f0SEmil Medve for_each_memblock(memory, r) { 1642c0ce8fefSTejun Heo if (limit <= r->size) { 1643c0ce8fefSTejun Heo max_addr = r->base + limit; 164495f72d1eSYinghai Lu break; 164595f72d1eSYinghai Lu } 1646c0ce8fefSTejun Heo limit -= r->size; 164795f72d1eSYinghai Lu } 1648c0ce8fefSTejun Heo 1649a571d4ebSDennis Chen return max_addr; 1650a571d4ebSDennis Chen } 1651a571d4ebSDennis Chen 1652a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit) 1653a571d4ebSDennis Chen { 16541c4bc43dSStefan Agner phys_addr_t max_addr = PHYS_ADDR_MAX; 1655a571d4ebSDennis Chen 1656a571d4ebSDennis Chen if (!limit) 1657a571d4ebSDennis Chen return; 1658a571d4ebSDennis Chen 1659a571d4ebSDennis Chen max_addr = __find_max_addr(limit); 1660a571d4ebSDennis Chen 1661a571d4ebSDennis Chen /* @limit exceeds the total size of the memory, do nothing */ 16621c4bc43dSStefan Agner if (max_addr == PHYS_ADDR_MAX) 1663a571d4ebSDennis Chen return; 1664a571d4ebSDennis Chen 1665c0ce8fefSTejun Heo /* truncate both memory and reserved regions */ 1666f1af9d3aSPhilipp Hachtmann memblock_remove_range(&memblock.memory, max_addr, 16671c4bc43dSStefan Agner PHYS_ADDR_MAX); 1668f1af9d3aSPhilipp Hachtmann memblock_remove_range(&memblock.reserved, max_addr, 16691c4bc43dSStefan Agner PHYS_ADDR_MAX); 167095f72d1eSYinghai Lu } 167195f72d1eSYinghai Lu 1672c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size) 1673c9ca9b4eSAKASHI Takahiro { 1674c9ca9b4eSAKASHI Takahiro int start_rgn, end_rgn; 1675c9ca9b4eSAKASHI Takahiro int i, ret; 1676c9ca9b4eSAKASHI Takahiro 1677c9ca9b4eSAKASHI Takahiro if (!size) 1678c9ca9b4eSAKASHI Takahiro return; 1679c9ca9b4eSAKASHI Takahiro 1680c9ca9b4eSAKASHI Takahiro ret = memblock_isolate_range(&memblock.memory, base, size, 1681c9ca9b4eSAKASHI Takahiro &start_rgn, &end_rgn); 1682c9ca9b4eSAKASHI Takahiro if (ret) 1683c9ca9b4eSAKASHI Takahiro return; 1684c9ca9b4eSAKASHI Takahiro 1685c9ca9b4eSAKASHI Takahiro /* remove all the MAP regions */ 1686c9ca9b4eSAKASHI Takahiro for (i = memblock.memory.cnt - 1; i >= end_rgn; i--) 1687c9ca9b4eSAKASHI Takahiro if (!memblock_is_nomap(&memblock.memory.regions[i])) 1688c9ca9b4eSAKASHI Takahiro memblock_remove_region(&memblock.memory, i); 1689c9ca9b4eSAKASHI Takahiro 1690c9ca9b4eSAKASHI Takahiro for (i = start_rgn - 1; i >= 0; i--) 1691c9ca9b4eSAKASHI Takahiro if (!memblock_is_nomap(&memblock.memory.regions[i])) 1692c9ca9b4eSAKASHI Takahiro memblock_remove_region(&memblock.memory, i); 1693c9ca9b4eSAKASHI Takahiro 1694c9ca9b4eSAKASHI Takahiro /* truncate the reserved regions */ 1695c9ca9b4eSAKASHI Takahiro memblock_remove_range(&memblock.reserved, 0, base); 1696c9ca9b4eSAKASHI Takahiro memblock_remove_range(&memblock.reserved, 16971c4bc43dSStefan Agner base + size, PHYS_ADDR_MAX); 1698c9ca9b4eSAKASHI Takahiro } 1699c9ca9b4eSAKASHI Takahiro 1700a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit) 1701a571d4ebSDennis Chen { 1702a571d4ebSDennis Chen phys_addr_t max_addr; 1703a571d4ebSDennis Chen 1704a571d4ebSDennis Chen if (!limit) 1705a571d4ebSDennis Chen return; 1706a571d4ebSDennis Chen 1707a571d4ebSDennis Chen max_addr = __find_max_addr(limit); 1708a571d4ebSDennis Chen 1709a571d4ebSDennis Chen /* @limit exceeds the total size of the memory, do nothing */ 17101c4bc43dSStefan Agner if (max_addr == PHYS_ADDR_MAX) 1711a571d4ebSDennis Chen return; 1712a571d4ebSDennis Chen 1713c9ca9b4eSAKASHI Takahiro memblock_cap_memory_range(0, max_addr); 1714a571d4ebSDennis Chen } 1715a571d4ebSDennis Chen 1716cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr) 171772d4b0b4SBenjamin Herrenschmidt { 171872d4b0b4SBenjamin Herrenschmidt unsigned int left = 0, right = type->cnt; 171972d4b0b4SBenjamin Herrenschmidt 172072d4b0b4SBenjamin Herrenschmidt do { 172172d4b0b4SBenjamin Herrenschmidt unsigned int mid = (right + left) / 2; 172272d4b0b4SBenjamin Herrenschmidt 172372d4b0b4SBenjamin Herrenschmidt if (addr < type->regions[mid].base) 172472d4b0b4SBenjamin Herrenschmidt right = mid; 172572d4b0b4SBenjamin Herrenschmidt else if (addr >= (type->regions[mid].base + 172672d4b0b4SBenjamin Herrenschmidt type->regions[mid].size)) 172772d4b0b4SBenjamin Herrenschmidt left = mid + 1; 172872d4b0b4SBenjamin Herrenschmidt else 172972d4b0b4SBenjamin Herrenschmidt return mid; 173072d4b0b4SBenjamin Herrenschmidt } while (left < right); 173172d4b0b4SBenjamin Herrenschmidt return -1; 173272d4b0b4SBenjamin Herrenschmidt } 173372d4b0b4SBenjamin Herrenschmidt 1734f5a222dcSYueyi Li bool __init_memblock memblock_is_reserved(phys_addr_t addr) 173595f72d1eSYinghai Lu { 173672d4b0b4SBenjamin Herrenschmidt return memblock_search(&memblock.reserved, addr) != -1; 173795f72d1eSYinghai Lu } 173872d4b0b4SBenjamin Herrenschmidt 1739b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr) 174072d4b0b4SBenjamin Herrenschmidt { 174172d4b0b4SBenjamin Herrenschmidt return memblock_search(&memblock.memory, addr) != -1; 174272d4b0b4SBenjamin Herrenschmidt } 174372d4b0b4SBenjamin Herrenschmidt 1744937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr) 1745bf3d3cc5SArd Biesheuvel { 1746bf3d3cc5SArd Biesheuvel int i = memblock_search(&memblock.memory, addr); 1747bf3d3cc5SArd Biesheuvel 1748bf3d3cc5SArd Biesheuvel if (i == -1) 1749bf3d3cc5SArd Biesheuvel return false; 1750bf3d3cc5SArd Biesheuvel return !memblock_is_nomap(&memblock.memory.regions[i]); 1751bf3d3cc5SArd Biesheuvel } 1752bf3d3cc5SArd Biesheuvel 1753e76b63f8SYinghai Lu #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 1754e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn, 1755e76b63f8SYinghai Lu unsigned long *start_pfn, unsigned long *end_pfn) 1756e76b63f8SYinghai Lu { 1757e76b63f8SYinghai Lu struct memblock_type *type = &memblock.memory; 175816763230SFabian Frederick int mid = memblock_search(type, PFN_PHYS(pfn)); 1759e76b63f8SYinghai Lu 1760e76b63f8SYinghai Lu if (mid == -1) 1761e76b63f8SYinghai Lu return -1; 1762e76b63f8SYinghai Lu 1763f7e2f7e8SFabian Frederick *start_pfn = PFN_DOWN(type->regions[mid].base); 1764f7e2f7e8SFabian Frederick *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size); 1765e76b63f8SYinghai Lu 1766e76b63f8SYinghai Lu return type->regions[mid].nid; 1767e76b63f8SYinghai Lu } 1768e76b63f8SYinghai Lu #endif 1769e76b63f8SYinghai Lu 1770eab30949SStephen Boyd /** 1771eab30949SStephen Boyd * memblock_is_region_memory - check if a region is a subset of memory 1772eab30949SStephen Boyd * @base: base of region to check 1773eab30949SStephen Boyd * @size: size of region to check 1774eab30949SStephen Boyd * 1775eab30949SStephen Boyd * Check if the region [@base, @base + @size) is a subset of a memory block. 1776eab30949SStephen Boyd * 177747cec443SMike Rapoport * Return: 1778eab30949SStephen Boyd * 0 if false, non-zero if true 1779eab30949SStephen Boyd */ 1780937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size) 178172d4b0b4SBenjamin Herrenschmidt { 1782abb65272STomi Valkeinen int idx = memblock_search(&memblock.memory, base); 1783eb18f1b5STejun Heo phys_addr_t end = base + memblock_cap_size(base, &size); 178472d4b0b4SBenjamin Herrenschmidt 178572d4b0b4SBenjamin Herrenschmidt if (idx == -1) 1786937f0c26SYaowei Bai return false; 1787ef415ef4SWei Yang return (memblock.memory.regions[idx].base + 1788eb18f1b5STejun Heo memblock.memory.regions[idx].size) >= end; 178995f72d1eSYinghai Lu } 179095f72d1eSYinghai Lu 1791eab30949SStephen Boyd /** 1792eab30949SStephen Boyd * memblock_is_region_reserved - check if a region intersects reserved memory 1793eab30949SStephen Boyd * @base: base of region to check 1794eab30949SStephen Boyd * @size: size of region to check 1795eab30949SStephen Boyd * 179647cec443SMike Rapoport * Check if the region [@base, @base + @size) intersects a reserved 179747cec443SMike Rapoport * memory block. 1798eab30949SStephen Boyd * 179947cec443SMike Rapoport * Return: 1800c5c5c9d1STang Chen * True if they intersect, false if not. 1801eab30949SStephen Boyd */ 1802c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) 180395f72d1eSYinghai Lu { 1804eb18f1b5STejun Heo memblock_cap_size(base, &size); 1805c5c5c9d1STang Chen return memblock_overlaps_region(&memblock.reserved, base, size); 180695f72d1eSYinghai Lu } 180795f72d1eSYinghai Lu 18086ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align) 18096ede1fd3SYinghai Lu { 18106ede1fd3SYinghai Lu phys_addr_t start, end, orig_start, orig_end; 1811136199f0SEmil Medve struct memblock_region *r; 18126ede1fd3SYinghai Lu 1813136199f0SEmil Medve for_each_memblock(memory, r) { 1814136199f0SEmil Medve orig_start = r->base; 1815136199f0SEmil Medve orig_end = r->base + r->size; 18166ede1fd3SYinghai Lu start = round_up(orig_start, align); 18176ede1fd3SYinghai Lu end = round_down(orig_end, align); 18186ede1fd3SYinghai Lu 18196ede1fd3SYinghai Lu if (start == orig_start && end == orig_end) 18206ede1fd3SYinghai Lu continue; 18216ede1fd3SYinghai Lu 18226ede1fd3SYinghai Lu if (start < end) { 1823136199f0SEmil Medve r->base = start; 1824136199f0SEmil Medve r->size = end - start; 18256ede1fd3SYinghai Lu } else { 1826136199f0SEmil Medve memblock_remove_region(&memblock.memory, 1827136199f0SEmil Medve r - memblock.memory.regions); 1828136199f0SEmil Medve r--; 18296ede1fd3SYinghai Lu } 18306ede1fd3SYinghai Lu } 18316ede1fd3SYinghai Lu } 1832e63075a3SBenjamin Herrenschmidt 18333661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit) 1834e63075a3SBenjamin Herrenschmidt { 1835e63075a3SBenjamin Herrenschmidt memblock.current_limit = limit; 1836e63075a3SBenjamin Herrenschmidt } 1837e63075a3SBenjamin Herrenschmidt 1838fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void) 1839fec51014SLaura Abbott { 1840fec51014SLaura Abbott return memblock.current_limit; 1841fec51014SLaura Abbott } 1842fec51014SLaura Abbott 18430262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type) 18446ed311b2SBenjamin Herrenschmidt { 18455d63f81cSMiles Chen phys_addr_t base, end, size; 1846e1720feeSMike Rapoport enum memblock_flags flags; 18478c9c1701SAlexander Kuleshov int idx; 18488c9c1701SAlexander Kuleshov struct memblock_region *rgn; 18496ed311b2SBenjamin Herrenschmidt 18500262d9c8SHeiko Carstens pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt); 18516ed311b2SBenjamin Herrenschmidt 185266e8b438SGioh Kim for_each_memblock_type(idx, type, rgn) { 18537c0caeb8STejun Heo char nid_buf[32] = ""; 18546ed311b2SBenjamin Herrenschmidt 18557c0caeb8STejun Heo base = rgn->base; 18567c0caeb8STejun Heo size = rgn->size; 18575d63f81cSMiles Chen end = base + size - 1; 185866a20757STang Chen flags = rgn->flags; 18597c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 18607c0caeb8STejun Heo if (memblock_get_region_node(rgn) != MAX_NUMNODES) 18617c0caeb8STejun Heo snprintf(nid_buf, sizeof(nid_buf), " on node %d", 18627c0caeb8STejun Heo memblock_get_region_node(rgn)); 18637c0caeb8STejun Heo #endif 1864e1720feeSMike Rapoport pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n", 18650262d9c8SHeiko Carstens type->name, idx, &base, &end, &size, nid_buf, flags); 18666ed311b2SBenjamin Herrenschmidt } 18676ed311b2SBenjamin Herrenschmidt } 18686ed311b2SBenjamin Herrenschmidt 18694ff7b82fSTejun Heo void __init_memblock __memblock_dump_all(void) 18706ed311b2SBenjamin Herrenschmidt { 18716ed311b2SBenjamin Herrenschmidt pr_info("MEMBLOCK configuration:\n"); 18725d63f81cSMiles Chen pr_info(" memory size = %pa reserved size = %pa\n", 18735d63f81cSMiles Chen &memblock.memory.total_size, 18745d63f81cSMiles Chen &memblock.reserved.total_size); 18756ed311b2SBenjamin Herrenschmidt 18760262d9c8SHeiko Carstens memblock_dump(&memblock.memory); 18770262d9c8SHeiko Carstens memblock_dump(&memblock.reserved); 1878409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 18790262d9c8SHeiko Carstens memblock_dump(&memblock.physmem); 1880409efd4cSHeiko Carstens #endif 18816ed311b2SBenjamin Herrenschmidt } 18826ed311b2SBenjamin Herrenschmidt 18831aadc056STejun Heo void __init memblock_allow_resize(void) 18846ed311b2SBenjamin Herrenschmidt { 1885142b45a7SBenjamin Herrenschmidt memblock_can_resize = 1; 18866ed311b2SBenjamin Herrenschmidt } 18876ed311b2SBenjamin Herrenschmidt 18886ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p) 18896ed311b2SBenjamin Herrenschmidt { 18906ed311b2SBenjamin Herrenschmidt if (p && strstr(p, "debug")) 18916ed311b2SBenjamin Herrenschmidt memblock_debug = 1; 18926ed311b2SBenjamin Herrenschmidt return 0; 18936ed311b2SBenjamin Herrenschmidt } 18946ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock); 18956ed311b2SBenjamin Herrenschmidt 1896bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end) 1897bda49a81SMike Rapoport { 1898bda49a81SMike Rapoport int order; 1899bda49a81SMike Rapoport 1900bda49a81SMike Rapoport while (start < end) { 1901bda49a81SMike Rapoport order = min(MAX_ORDER - 1UL, __ffs(start)); 1902bda49a81SMike Rapoport 1903bda49a81SMike Rapoport while (start + (1UL << order) > end) 1904bda49a81SMike Rapoport order--; 1905bda49a81SMike Rapoport 1906bda49a81SMike Rapoport memblock_free_pages(pfn_to_page(start), start, order); 1907bda49a81SMike Rapoport 1908bda49a81SMike Rapoport start += (1UL << order); 1909bda49a81SMike Rapoport } 1910bda49a81SMike Rapoport } 1911bda49a81SMike Rapoport 1912bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start, 1913bda49a81SMike Rapoport phys_addr_t end) 1914bda49a81SMike Rapoport { 1915bda49a81SMike Rapoport unsigned long start_pfn = PFN_UP(start); 1916bda49a81SMike Rapoport unsigned long end_pfn = min_t(unsigned long, 1917bda49a81SMike Rapoport PFN_DOWN(end), max_low_pfn); 1918bda49a81SMike Rapoport 1919bda49a81SMike Rapoport if (start_pfn >= end_pfn) 1920bda49a81SMike Rapoport return 0; 1921bda49a81SMike Rapoport 1922bda49a81SMike Rapoport __free_pages_memory(start_pfn, end_pfn); 1923bda49a81SMike Rapoport 1924bda49a81SMike Rapoport return end_pfn - start_pfn; 1925bda49a81SMike Rapoport } 1926bda49a81SMike Rapoport 1927bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void) 1928bda49a81SMike Rapoport { 1929bda49a81SMike Rapoport unsigned long count = 0; 1930bda49a81SMike Rapoport phys_addr_t start, end; 1931bda49a81SMike Rapoport u64 i; 1932bda49a81SMike Rapoport 1933bda49a81SMike Rapoport memblock_clear_hotplug(0, -1); 1934bda49a81SMike Rapoport 1935bda49a81SMike Rapoport for_each_reserved_mem_region(i, &start, &end) 1936bda49a81SMike Rapoport reserve_bootmem_region(start, end); 1937bda49a81SMike Rapoport 1938bda49a81SMike Rapoport /* 1939bda49a81SMike Rapoport * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id 1940bda49a81SMike Rapoport * because in some case like Node0 doesn't have RAM installed 1941bda49a81SMike Rapoport * low ram will be on Node1 1942bda49a81SMike Rapoport */ 1943bda49a81SMike Rapoport for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end, 1944bda49a81SMike Rapoport NULL) 1945bda49a81SMike Rapoport count += __free_memory_core(start, end); 1946bda49a81SMike Rapoport 1947bda49a81SMike Rapoport return count; 1948bda49a81SMike Rapoport } 1949bda49a81SMike Rapoport 1950bda49a81SMike Rapoport static int reset_managed_pages_done __initdata; 1951bda49a81SMike Rapoport 1952bda49a81SMike Rapoport void reset_node_managed_pages(pg_data_t *pgdat) 1953bda49a81SMike Rapoport { 1954bda49a81SMike Rapoport struct zone *z; 1955bda49a81SMike Rapoport 1956bda49a81SMike Rapoport for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 19579705bea5SArun KS atomic_long_set(&z->managed_pages, 0); 1958bda49a81SMike Rapoport } 1959bda49a81SMike Rapoport 1960bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void) 1961bda49a81SMike Rapoport { 1962bda49a81SMike Rapoport struct pglist_data *pgdat; 1963bda49a81SMike Rapoport 1964bda49a81SMike Rapoport if (reset_managed_pages_done) 1965bda49a81SMike Rapoport return; 1966bda49a81SMike Rapoport 1967bda49a81SMike Rapoport for_each_online_pgdat(pgdat) 1968bda49a81SMike Rapoport reset_node_managed_pages(pgdat); 1969bda49a81SMike Rapoport 1970bda49a81SMike Rapoport reset_managed_pages_done = 1; 1971bda49a81SMike Rapoport } 1972bda49a81SMike Rapoport 1973bda49a81SMike Rapoport /** 1974bda49a81SMike Rapoport * memblock_free_all - release free pages to the buddy allocator 1975bda49a81SMike Rapoport * 1976bda49a81SMike Rapoport * Return: the number of pages actually released. 1977bda49a81SMike Rapoport */ 1978bda49a81SMike Rapoport unsigned long __init memblock_free_all(void) 1979bda49a81SMike Rapoport { 1980bda49a81SMike Rapoport unsigned long pages; 1981bda49a81SMike Rapoport 1982bda49a81SMike Rapoport reset_all_zones_managed_pages(); 1983bda49a81SMike Rapoport 1984bda49a81SMike Rapoport pages = free_low_memory_core_early(); 1985ca79b0c2SArun KS totalram_pages_add(pages); 1986bda49a81SMike Rapoport 1987bda49a81SMike Rapoport return pages; 1988bda49a81SMike Rapoport } 1989bda49a81SMike Rapoport 1990*350e88baSMike Rapoport #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK) 19916d03b885SBenjamin Herrenschmidt 19926d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private) 19936d03b885SBenjamin Herrenschmidt { 19946d03b885SBenjamin Herrenschmidt struct memblock_type *type = m->private; 19956d03b885SBenjamin Herrenschmidt struct memblock_region *reg; 19966d03b885SBenjamin Herrenschmidt int i; 19975d63f81cSMiles Chen phys_addr_t end; 19986d03b885SBenjamin Herrenschmidt 19996d03b885SBenjamin Herrenschmidt for (i = 0; i < type->cnt; i++) { 20006d03b885SBenjamin Herrenschmidt reg = &type->regions[i]; 20015d63f81cSMiles Chen end = reg->base + reg->size - 1; 20026d03b885SBenjamin Herrenschmidt 20035d63f81cSMiles Chen seq_printf(m, "%4d: ", i); 20045d63f81cSMiles Chen seq_printf(m, "%pa..%pa\n", ®->base, &end); 20056d03b885SBenjamin Herrenschmidt } 20066d03b885SBenjamin Herrenschmidt return 0; 20076d03b885SBenjamin Herrenschmidt } 20085ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug); 20096d03b885SBenjamin Herrenschmidt 20106d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void) 20116d03b885SBenjamin Herrenschmidt { 20126d03b885SBenjamin Herrenschmidt struct dentry *root = debugfs_create_dir("memblock", NULL); 2013d9f7979cSGreg Kroah-Hartman 20140825a6f9SJoe Perches debugfs_create_file("memory", 0444, root, 20150825a6f9SJoe Perches &memblock.memory, &memblock_debug_fops); 20160825a6f9SJoe Perches debugfs_create_file("reserved", 0444, root, 20170825a6f9SJoe Perches &memblock.reserved, &memblock_debug_fops); 201870210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 20190825a6f9SJoe Perches debugfs_create_file("physmem", 0444, root, 20200825a6f9SJoe Perches &memblock.physmem, &memblock_debug_fops); 202170210ed9SPhilipp Hachtmann #endif 20226d03b885SBenjamin Herrenschmidt 20236d03b885SBenjamin Herrenschmidt return 0; 20246d03b885SBenjamin Herrenschmidt } 20256d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs); 20266d03b885SBenjamin Herrenschmidt 20276d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */ 2028