12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 295f72d1eSYinghai Lu /* 395f72d1eSYinghai Lu * Procedures for maintaining information about logical memory blocks. 495f72d1eSYinghai Lu * 595f72d1eSYinghai Lu * Peter Bergner, IBM Corp. June 2001. 695f72d1eSYinghai Lu * Copyright (C) 2001 Peter Bergner. 795f72d1eSYinghai Lu */ 895f72d1eSYinghai Lu 995f72d1eSYinghai Lu #include <linux/kernel.h> 10142b45a7SBenjamin Herrenschmidt #include <linux/slab.h> 1195f72d1eSYinghai Lu #include <linux/init.h> 1295f72d1eSYinghai Lu #include <linux/bitops.h> 13449e8df3SBenjamin Herrenschmidt #include <linux/poison.h> 14c196f76fSBenjamin Herrenschmidt #include <linux/pfn.h> 156d03b885SBenjamin Herrenschmidt #include <linux/debugfs.h> 16514c6032SRandy Dunlap #include <linux/kmemleak.h> 176d03b885SBenjamin Herrenschmidt #include <linux/seq_file.h> 1895f72d1eSYinghai Lu #include <linux/memblock.h> 1995f72d1eSYinghai Lu 20c4c5ad6bSChristoph Hellwig #include <asm/sections.h> 2126f09e9bSSantosh Shilimkar #include <linux/io.h> 2226f09e9bSSantosh Shilimkar 2326f09e9bSSantosh Shilimkar #include "internal.h" 2479442ed1STang Chen 258a5b403dSArd Biesheuvel #define INIT_MEMBLOCK_REGIONS 128 268a5b403dSArd Biesheuvel #define INIT_PHYSMEM_REGIONS 4 278a5b403dSArd Biesheuvel 288a5b403dSArd Biesheuvel #ifndef INIT_MEMBLOCK_RESERVED_REGIONS 298a5b403dSArd Biesheuvel # define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS 308a5b403dSArd Biesheuvel #endif 318a5b403dSArd Biesheuvel 32450d0e74SZhou Guanghui #ifndef INIT_MEMBLOCK_MEMORY_REGIONS 33450d0e74SZhou Guanghui #define INIT_MEMBLOCK_MEMORY_REGIONS INIT_MEMBLOCK_REGIONS 34450d0e74SZhou Guanghui #endif 35450d0e74SZhou Guanghui 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 5177649905SDavid Hildenbrand * * ``physmem`` - describes the actual physical memory available during 5277649905SDavid Hildenbrand * boot regardless of the possible restrictions and memory hot(un)plug; 5377649905SDavid Hildenbrand * the ``physmem`` type is only available on some architectures. 543e039c5cSMike Rapoport * 559303c9d5SMauro Carvalho Chehab * Each region is represented by struct memblock_region that 563e039c5cSMike Rapoport * defines the region extents, its attributes and NUMA node id on NUMA 571bf162e4SMauro Carvalho Chehab * systems. Every memory type is described by the struct memblock_type 581bf162e4SMauro Carvalho Chehab * which contains an array of memory regions along with 5977649905SDavid Hildenbrand * the allocator metadata. The "memory" and "reserved" types are nicely 609303c9d5SMauro Carvalho Chehab * wrapped with struct memblock. This structure is statically 6177649905SDavid Hildenbrand * initialized at build time. The region arrays are initially sized to 62450d0e74SZhou Guanghui * %INIT_MEMBLOCK_MEMORY_REGIONS for "memory" and 63450d0e74SZhou Guanghui * %INIT_MEMBLOCK_RESERVED_REGIONS for "reserved". The region array 64450d0e74SZhou Guanghui * for "physmem" is initially sized to %INIT_PHYSMEM_REGIONS. 656e5af9a8SCao jin * The memblock_allow_resize() enables automatic resizing of the region 666e5af9a8SCao jin * arrays during addition of new regions. This feature should be used 676e5af9a8SCao jin * with care so that memory allocated for the region array will not 686e5af9a8SCao jin * overlap with areas that should be reserved, for example initrd. 693e039c5cSMike Rapoport * 703e039c5cSMike Rapoport * The early architecture setup should tell memblock what the physical 716e5af9a8SCao jin * memory layout is by using memblock_add() or memblock_add_node() 726e5af9a8SCao jin * functions. The first function does not assign the region to a NUMA 736e5af9a8SCao jin * node and it is appropriate for UMA systems. Yet, it is possible to 746e5af9a8SCao jin * use it on NUMA systems as well and assign the region to a NUMA node 756e5af9a8SCao jin * later in the setup process using memblock_set_node(). The 766e5af9a8SCao jin * memblock_add_node() performs such an assignment directly. 773e039c5cSMike Rapoport * 78a2974133SMike Rapoport * Once memblock is setup the memory can be allocated using one of the 79a2974133SMike Rapoport * API variants: 80a2974133SMike Rapoport * 816e5af9a8SCao jin * * memblock_phys_alloc*() - these functions return the **physical** 826e5af9a8SCao jin * address of the allocated memory 836e5af9a8SCao jin * * memblock_alloc*() - these functions return the **virtual** address 846e5af9a8SCao jin * of the allocated memory. 85a2974133SMike Rapoport * 86df1758d9SEthon Paul * Note, that both API variants use implicit assumptions about allowed 87a2974133SMike Rapoport * memory ranges and the fallback methods. Consult the documentation 886e5af9a8SCao jin * of memblock_alloc_internal() and memblock_alloc_range_nid() 896e5af9a8SCao jin * functions for more elaborate description. 903e039c5cSMike Rapoport * 916e5af9a8SCao jin * As the system boot progresses, the architecture specific mem_init() 926e5af9a8SCao jin * function frees all the memory to the buddy page allocator. 933e039c5cSMike Rapoport * 946e5af9a8SCao jin * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the 9577649905SDavid Hildenbrand * memblock data structures (except "physmem") will be discarded after the 9677649905SDavid Hildenbrand * system initialization completes. 973e039c5cSMike Rapoport */ 983e039c5cSMike Rapoport 99a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA 100bda49a81SMike Rapoport struct pglist_data __refdata contig_page_data; 101bda49a81SMike Rapoport EXPORT_SYMBOL(contig_page_data); 102bda49a81SMike Rapoport #endif 103bda49a81SMike Rapoport 104bda49a81SMike Rapoport unsigned long max_low_pfn; 105bda49a81SMike Rapoport unsigned long min_low_pfn; 106bda49a81SMike Rapoport unsigned long max_pfn; 107bda49a81SMike Rapoport unsigned long long max_possible_pfn; 108bda49a81SMike Rapoport 109450d0e74SZhou Guanghui static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_MEMORY_REGIONS] __initdata_memblock; 1108a5b403dSArd Biesheuvel static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock; 11170210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 11277649905SDavid Hildenbrand static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS]; 11370210ed9SPhilipp Hachtmann #endif 114fe091c20STejun Heo 115fe091c20STejun Heo struct memblock memblock __initdata_memblock = { 116fe091c20STejun Heo .memory.regions = memblock_memory_init_regions, 117fe091c20STejun Heo .memory.cnt = 1, /* empty dummy entry */ 118450d0e74SZhou Guanghui .memory.max = INIT_MEMBLOCK_MEMORY_REGIONS, 1190262d9c8SHeiko Carstens .memory.name = "memory", 120fe091c20STejun Heo 121fe091c20STejun Heo .reserved.regions = memblock_reserved_init_regions, 122fe091c20STejun Heo .reserved.cnt = 1, /* empty dummy entry */ 1238a5b403dSArd Biesheuvel .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS, 1240262d9c8SHeiko Carstens .reserved.name = "reserved", 125fe091c20STejun Heo 12679442ed1STang Chen .bottom_up = false, 127fe091c20STejun Heo .current_limit = MEMBLOCK_ALLOC_ANYWHERE, 128fe091c20STejun Heo }; 12995f72d1eSYinghai Lu 13077649905SDavid Hildenbrand #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 13177649905SDavid Hildenbrand struct memblock_type physmem = { 13277649905SDavid Hildenbrand .regions = memblock_physmem_init_regions, 13377649905SDavid Hildenbrand .cnt = 1, /* empty dummy entry */ 13477649905SDavid Hildenbrand .max = INIT_PHYSMEM_REGIONS, 13577649905SDavid Hildenbrand .name = "physmem", 13677649905SDavid Hildenbrand }; 13777649905SDavid Hildenbrand #endif 13877649905SDavid Hildenbrand 1399f3d5eaaSMike Rapoport /* 1409f3d5eaaSMike Rapoport * keep a pointer to &memblock.memory in the text section to use it in 1419f3d5eaaSMike Rapoport * __next_mem_range() and its helpers. 1429f3d5eaaSMike Rapoport * For architectures that do not keep memblock data after init, this 1439f3d5eaaSMike Rapoport * pointer will be reset to NULL at memblock_discard() 1449f3d5eaaSMike Rapoport */ 1459f3d5eaaSMike Rapoport static __refdata struct memblock_type *memblock_memory = &memblock.memory; 1469f3d5eaaSMike Rapoport 147cd991db8SMike Rapoport #define for_each_memblock_type(i, memblock_type, rgn) \ 148cd991db8SMike Rapoport for (i = 0, rgn = &memblock_type->regions[0]; \ 149cd991db8SMike Rapoport i < memblock_type->cnt; \ 150cd991db8SMike Rapoport i++, rgn = &memblock_type->regions[i]) 151cd991db8SMike Rapoport 15287c55870SMike Rapoport #define memblock_dbg(fmt, ...) \ 15387c55870SMike Rapoport do { \ 15487c55870SMike Rapoport if (memblock_debug) \ 15587c55870SMike Rapoport pr_info(fmt, ##__VA_ARGS__); \ 15687c55870SMike Rapoport } while (0) 15787c55870SMike Rapoport 15887c55870SMike Rapoport static int memblock_debug __initdata_memblock; 159fc493f83SClaudio Migliorelli static bool system_has_some_mirror __initdata_memblock; 1601aadc056STejun Heo static int memblock_can_resize __initdata_memblock; 161fc493f83SClaudio Migliorelli static int memblock_memory_in_slab __initdata_memblock; 162fc493f83SClaudio Migliorelli static int memblock_reserved_in_slab __initdata_memblock; 16395f72d1eSYinghai Lu 1640db31d63SMa Wupeng bool __init_memblock memblock_has_mirror(void) 1650db31d63SMa Wupeng { 1660db31d63SMa Wupeng return system_has_some_mirror; 1670db31d63SMa Wupeng } 1680db31d63SMa Wupeng 169c366ea89SMike Rapoport static enum memblock_flags __init_memblock choose_memblock_flags(void) 170a3f5bafcSTony Luck { 171a3f5bafcSTony Luck return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE; 172a3f5bafcSTony Luck } 173a3f5bafcSTony Luck 174eb18f1b5STejun Heo /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */ 175eb18f1b5STejun Heo static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size) 176eb18f1b5STejun Heo { 1771c4bc43dSStefan Agner return *size = min(*size, PHYS_ADDR_MAX - base); 178eb18f1b5STejun Heo } 179eb18f1b5STejun Heo 1806ed311b2SBenjamin Herrenschmidt /* 1816ed311b2SBenjamin Herrenschmidt * Address comparison utilities 1826ed311b2SBenjamin Herrenschmidt */ 183*9b99c17fSAlison Schofield unsigned long __init_memblock 184*9b99c17fSAlison Schofield memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, phys_addr_t base2, 185*9b99c17fSAlison Schofield phys_addr_t size2) 18695f72d1eSYinghai Lu { 18795f72d1eSYinghai Lu return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); 18895f72d1eSYinghai Lu } 18995f72d1eSYinghai Lu 19095cf82ecSTang Chen bool __init_memblock memblock_overlaps_region(struct memblock_type *type, 1912d7d3eb2SH Hartley Sweeten phys_addr_t base, phys_addr_t size) 1926ed311b2SBenjamin Herrenschmidt { 1936ed311b2SBenjamin Herrenschmidt unsigned long i; 1946ed311b2SBenjamin Herrenschmidt 195023accf5SMike Rapoport memblock_cap_size(base, &size); 196023accf5SMike Rapoport 197f14516fbSAlexander Kuleshov for (i = 0; i < type->cnt; i++) 198f14516fbSAlexander Kuleshov if (memblock_addrs_overlap(base, size, type->regions[i].base, 199f14516fbSAlexander Kuleshov type->regions[i].size)) 2006ed311b2SBenjamin Herrenschmidt break; 201c5c5c9d1STang Chen return i < type->cnt; 2026ed311b2SBenjamin Herrenschmidt } 2036ed311b2SBenjamin Herrenschmidt 20447cec443SMike Rapoport /** 20579442ed1STang Chen * __memblock_find_range_bottom_up - find free area utility in bottom-up 20679442ed1STang Chen * @start: start of candidate range 20747cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 20847cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 20979442ed1STang Chen * @size: size of free area to find 21079442ed1STang Chen * @align: alignment of free area to find 211b1154233SGrygorii Strashko * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 212fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 21379442ed1STang Chen * 21479442ed1STang Chen * Utility called from memblock_find_in_range_node(), find free area bottom-up. 21579442ed1STang Chen * 21647cec443SMike Rapoport * Return: 21779442ed1STang Chen * Found address on success, 0 on failure. 21879442ed1STang Chen */ 21979442ed1STang Chen static phys_addr_t __init_memblock 22079442ed1STang Chen __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end, 221fc6daaf9STony Luck phys_addr_t size, phys_addr_t align, int nid, 222e1720feeSMike Rapoport enum memblock_flags flags) 22379442ed1STang Chen { 22479442ed1STang Chen phys_addr_t this_start, this_end, cand; 22579442ed1STang Chen u64 i; 22679442ed1STang Chen 227fc6daaf9STony Luck for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) { 22879442ed1STang Chen this_start = clamp(this_start, start, end); 22979442ed1STang Chen this_end = clamp(this_end, start, end); 23079442ed1STang Chen 23179442ed1STang Chen cand = round_up(this_start, align); 23279442ed1STang Chen if (cand < this_end && this_end - cand >= size) 23379442ed1STang Chen return cand; 23479442ed1STang Chen } 23579442ed1STang Chen 23679442ed1STang Chen return 0; 23779442ed1STang Chen } 23879442ed1STang Chen 2397bd0b0f0STejun Heo /** 2401402899eSTang Chen * __memblock_find_range_top_down - find free area utility, in top-down 2411402899eSTang Chen * @start: start of candidate range 24247cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 24347cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 2441402899eSTang Chen * @size: size of free area to find 2451402899eSTang Chen * @align: alignment of free area to find 246b1154233SGrygorii Strashko * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 247fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 2481402899eSTang Chen * 2491402899eSTang Chen * Utility called from memblock_find_in_range_node(), find free area top-down. 2501402899eSTang Chen * 25147cec443SMike Rapoport * Return: 25279442ed1STang Chen * Found address on success, 0 on failure. 2531402899eSTang Chen */ 2541402899eSTang Chen static phys_addr_t __init_memblock 2551402899eSTang Chen __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end, 256fc6daaf9STony Luck phys_addr_t size, phys_addr_t align, int nid, 257e1720feeSMike Rapoport enum memblock_flags flags) 2581402899eSTang Chen { 2591402899eSTang Chen phys_addr_t this_start, this_end, cand; 2601402899eSTang Chen u64 i; 2611402899eSTang Chen 262fc6daaf9STony Luck for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end, 263fc6daaf9STony Luck NULL) { 2641402899eSTang Chen this_start = clamp(this_start, start, end); 2651402899eSTang Chen this_end = clamp(this_end, start, end); 2661402899eSTang Chen 2671402899eSTang Chen if (this_end < size) 2681402899eSTang Chen continue; 2691402899eSTang Chen 2701402899eSTang Chen cand = round_down(this_end - size, align); 2711402899eSTang Chen if (cand >= this_start) 2721402899eSTang Chen return cand; 2731402899eSTang Chen } 2741402899eSTang Chen 2751402899eSTang Chen return 0; 2761402899eSTang Chen } 2771402899eSTang Chen 2781402899eSTang Chen /** 2797bd0b0f0STejun Heo * memblock_find_in_range_node - find free area in given range and node 2807bd0b0f0STejun Heo * @size: size of free area to find 2817bd0b0f0STejun Heo * @align: alignment of free area to find 28287029ee9SGrygorii Strashko * @start: start of candidate range 28347cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 28447cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 285b1154233SGrygorii Strashko * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 286fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 2877bd0b0f0STejun Heo * 2887bd0b0f0STejun Heo * Find @size free area aligned to @align in the specified range and node. 2897bd0b0f0STejun Heo * 29047cec443SMike Rapoport * Return: 29179442ed1STang Chen * Found address on success, 0 on failure. 2926ed311b2SBenjamin Herrenschmidt */ 293c366ea89SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size, 29487029ee9SGrygorii Strashko phys_addr_t align, phys_addr_t start, 295e1720feeSMike Rapoport phys_addr_t end, int nid, 296e1720feeSMike Rapoport enum memblock_flags flags) 297f7210e6cSTang Chen { 298f7210e6cSTang Chen /* pump up @end */ 299fed84c78SQian Cai if (end == MEMBLOCK_ALLOC_ACCESSIBLE || 300c6975d7cSQian Cai end == MEMBLOCK_ALLOC_NOLEAKTRACE) 301f7210e6cSTang Chen end = memblock.current_limit; 302f7210e6cSTang Chen 303f7210e6cSTang Chen /* avoid allocating the first page */ 304f7210e6cSTang Chen start = max_t(phys_addr_t, start, PAGE_SIZE); 305f7210e6cSTang Chen end = max(start, end); 30679442ed1STang Chen 3072dcb3964SRoman Gushchin if (memblock_bottom_up()) 3082dcb3964SRoman Gushchin return __memblock_find_range_bottom_up(start, end, size, align, 3092dcb3964SRoman Gushchin nid, flags); 3102dcb3964SRoman Gushchin else 3112dcb3964SRoman Gushchin return __memblock_find_range_top_down(start, end, size, align, 3122dcb3964SRoman Gushchin nid, flags); 313f7210e6cSTang Chen } 3146ed311b2SBenjamin Herrenschmidt 3157bd0b0f0STejun Heo /** 3167bd0b0f0STejun Heo * memblock_find_in_range - find free area in given range 3177bd0b0f0STejun Heo * @start: start of candidate range 31847cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 31947cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 3207bd0b0f0STejun Heo * @size: size of free area to find 3217bd0b0f0STejun Heo * @align: alignment of free area to find 3227bd0b0f0STejun Heo * 3237bd0b0f0STejun Heo * Find @size free area aligned to @align in the specified range. 3247bd0b0f0STejun Heo * 32547cec443SMike Rapoport * Return: 32679442ed1STang Chen * Found address on success, 0 on failure. 3277bd0b0f0STejun Heo */ 328a7259df7SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, 3297bd0b0f0STejun Heo phys_addr_t end, phys_addr_t size, 3307bd0b0f0STejun Heo phys_addr_t align) 3317bd0b0f0STejun Heo { 332a3f5bafcSTony Luck phys_addr_t ret; 333e1720feeSMike Rapoport enum memblock_flags flags = choose_memblock_flags(); 334a3f5bafcSTony Luck 335a3f5bafcSTony Luck again: 336a3f5bafcSTony Luck ret = memblock_find_in_range_node(size, align, start, end, 337a3f5bafcSTony Luck NUMA_NO_NODE, flags); 338a3f5bafcSTony Luck 339a3f5bafcSTony Luck if (!ret && (flags & MEMBLOCK_MIRROR)) { 34014d9a675SMa Wupeng pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n", 341a3f5bafcSTony Luck &size); 342a3f5bafcSTony Luck flags &= ~MEMBLOCK_MIRROR; 343a3f5bafcSTony Luck goto again; 344a3f5bafcSTony Luck } 345a3f5bafcSTony Luck 346a3f5bafcSTony Luck return ret; 3477bd0b0f0STejun Heo } 3487bd0b0f0STejun Heo 34910d06439SYinghai Lu static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r) 35095f72d1eSYinghai Lu { 3511440c4e2STejun Heo type->total_size -= type->regions[r].size; 3527c0caeb8STejun Heo memmove(&type->regions[r], &type->regions[r + 1], 3537c0caeb8STejun Heo (type->cnt - (r + 1)) * sizeof(type->regions[r])); 354e3239ff9SBenjamin Herrenschmidt type->cnt--; 35595f72d1eSYinghai Lu 3568f7a6605SBenjamin Herrenschmidt /* Special case for empty arrays */ 3578f7a6605SBenjamin Herrenschmidt if (type->cnt == 0) { 3581440c4e2STejun Heo WARN_ON(type->total_size != 0); 3598f7a6605SBenjamin Herrenschmidt type->cnt = 1; 3608f7a6605SBenjamin Herrenschmidt type->regions[0].base = 0; 3618f7a6605SBenjamin Herrenschmidt type->regions[0].size = 0; 36266a20757STang Chen type->regions[0].flags = 0; 3637c0caeb8STejun Heo memblock_set_region_node(&type->regions[0], MAX_NUMNODES); 3648f7a6605SBenjamin Herrenschmidt } 36595f72d1eSYinghai Lu } 36695f72d1eSYinghai Lu 367350e88baSMike Rapoport #ifndef CONFIG_ARCH_KEEP_MEMBLOCK 3683010f876SPavel Tatashin /** 36947cec443SMike Rapoport * memblock_discard - discard memory and reserved arrays if they were allocated 3703010f876SPavel Tatashin */ 3713010f876SPavel Tatashin void __init memblock_discard(void) 37229f67386SYinghai Lu { 3733010f876SPavel Tatashin phys_addr_t addr, size; 37429f67386SYinghai Lu 3753010f876SPavel Tatashin if (memblock.reserved.regions != memblock_reserved_init_regions) { 3763010f876SPavel Tatashin addr = __pa(memblock.reserved.regions); 3773010f876SPavel Tatashin size = PAGE_ALIGN(sizeof(struct memblock_region) * 37829f67386SYinghai Lu memblock.reserved.max); 379c94afc46SMiaohe Lin if (memblock_reserved_in_slab) 380c94afc46SMiaohe Lin kfree(memblock.reserved.regions); 381c94afc46SMiaohe Lin else 382621d9739SMike Rapoport memblock_free_late(addr, size); 38329f67386SYinghai Lu } 38429f67386SYinghai Lu 38591b540f9SPavel Tatashin if (memblock.memory.regions != memblock_memory_init_regions) { 3863010f876SPavel Tatashin addr = __pa(memblock.memory.regions); 3873010f876SPavel Tatashin size = PAGE_ALIGN(sizeof(struct memblock_region) * 3885e270e25SPhilipp Hachtmann memblock.memory.max); 389c94afc46SMiaohe Lin if (memblock_memory_in_slab) 390c94afc46SMiaohe Lin kfree(memblock.memory.regions); 391c94afc46SMiaohe Lin else 392621d9739SMike Rapoport memblock_free_late(addr, size); 3935e270e25SPhilipp Hachtmann } 3949f3d5eaaSMike Rapoport 3959f3d5eaaSMike Rapoport memblock_memory = NULL; 3963010f876SPavel Tatashin } 3975e270e25SPhilipp Hachtmann #endif 3985e270e25SPhilipp Hachtmann 39948c3b583SGreg Pearson /** 40048c3b583SGreg Pearson * memblock_double_array - double the size of the memblock regions array 40148c3b583SGreg Pearson * @type: memblock type of the regions array being doubled 40248c3b583SGreg Pearson * @new_area_start: starting address of memory range to avoid overlap with 40348c3b583SGreg Pearson * @new_area_size: size of memory range to avoid overlap with 40448c3b583SGreg Pearson * 40548c3b583SGreg Pearson * Double the size of the @type regions array. If memblock is being used to 40648c3b583SGreg Pearson * allocate memory for a new reserved regions array and there is a previously 40748c3b583SGreg Pearson * allocated memory range [@new_area_start, @new_area_start + @new_area_size] 40848c3b583SGreg Pearson * waiting to be reserved, ensure the memory used by the new array does 40948c3b583SGreg Pearson * not overlap. 41048c3b583SGreg Pearson * 41147cec443SMike Rapoport * Return: 41248c3b583SGreg Pearson * 0 on success, -1 on failure. 41348c3b583SGreg Pearson */ 41448c3b583SGreg Pearson static int __init_memblock memblock_double_array(struct memblock_type *type, 41548c3b583SGreg Pearson phys_addr_t new_area_start, 41648c3b583SGreg Pearson phys_addr_t new_area_size) 417142b45a7SBenjamin Herrenschmidt { 418142b45a7SBenjamin Herrenschmidt struct memblock_region *new_array, *old_array; 41929f67386SYinghai Lu phys_addr_t old_alloc_size, new_alloc_size; 420a36aab89SMike Rapoport phys_addr_t old_size, new_size, addr, new_end; 421142b45a7SBenjamin Herrenschmidt int use_slab = slab_is_available(); 422181eb394SGavin Shan int *in_slab; 423142b45a7SBenjamin Herrenschmidt 424142b45a7SBenjamin Herrenschmidt /* We don't allow resizing until we know about the reserved regions 425142b45a7SBenjamin Herrenschmidt * of memory that aren't suitable for allocation 426142b45a7SBenjamin Herrenschmidt */ 427142b45a7SBenjamin Herrenschmidt if (!memblock_can_resize) 428e96c6b8fSSong Shuai panic("memblock: cannot resize %s array\n", type->name); 429142b45a7SBenjamin Herrenschmidt 430142b45a7SBenjamin Herrenschmidt /* Calculate new doubled size */ 431142b45a7SBenjamin Herrenschmidt old_size = type->max * sizeof(struct memblock_region); 432142b45a7SBenjamin Herrenschmidt new_size = old_size << 1; 43329f67386SYinghai Lu /* 43429f67386SYinghai Lu * We need to allocated new one align to PAGE_SIZE, 43529f67386SYinghai Lu * so we can free them completely later. 43629f67386SYinghai Lu */ 43729f67386SYinghai Lu old_alloc_size = PAGE_ALIGN(old_size); 43829f67386SYinghai Lu new_alloc_size = PAGE_ALIGN(new_size); 439142b45a7SBenjamin Herrenschmidt 440181eb394SGavin Shan /* Retrieve the slab flag */ 441181eb394SGavin Shan if (type == &memblock.memory) 442181eb394SGavin Shan in_slab = &memblock_memory_in_slab; 443181eb394SGavin Shan else 444181eb394SGavin Shan in_slab = &memblock_reserved_in_slab; 445181eb394SGavin Shan 446a2974133SMike Rapoport /* Try to find some space for it */ 447142b45a7SBenjamin Herrenschmidt if (use_slab) { 448142b45a7SBenjamin Herrenschmidt new_array = kmalloc(new_size, GFP_KERNEL); 4491f5026a7STejun Heo addr = new_array ? __pa(new_array) : 0; 4504e2f0775SGavin Shan } else { 45148c3b583SGreg Pearson /* only exclude range when trying to double reserved.regions */ 45248c3b583SGreg Pearson if (type != &memblock.reserved) 45348c3b583SGreg Pearson new_area_start = new_area_size = 0; 45448c3b583SGreg Pearson 45548c3b583SGreg Pearson addr = memblock_find_in_range(new_area_start + new_area_size, 45648c3b583SGreg Pearson memblock.current_limit, 45729f67386SYinghai Lu new_alloc_size, PAGE_SIZE); 45848c3b583SGreg Pearson if (!addr && new_area_size) 45948c3b583SGreg Pearson addr = memblock_find_in_range(0, 46048c3b583SGreg Pearson min(new_area_start, memblock.current_limit), 46129f67386SYinghai Lu new_alloc_size, PAGE_SIZE); 46248c3b583SGreg Pearson 46315674868SSachin Kamat new_array = addr ? __va(addr) : NULL; 4644e2f0775SGavin Shan } 4651f5026a7STejun Heo if (!addr) { 466142b45a7SBenjamin Herrenschmidt pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", 4670262d9c8SHeiko Carstens type->name, type->max, type->max * 2); 468142b45a7SBenjamin Herrenschmidt return -1; 469142b45a7SBenjamin Herrenschmidt } 470142b45a7SBenjamin Herrenschmidt 471a36aab89SMike Rapoport new_end = addr + new_size - 1; 472a36aab89SMike Rapoport memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]", 473a36aab89SMike Rapoport type->name, type->max * 2, &addr, &new_end); 474ea9e4376SYinghai Lu 475fd07383bSAndrew Morton /* 476fd07383bSAndrew Morton * Found space, we now need to move the array over before we add the 477fd07383bSAndrew Morton * reserved region since it may be our reserved array itself that is 478fd07383bSAndrew Morton * full. 479142b45a7SBenjamin Herrenschmidt */ 480142b45a7SBenjamin Herrenschmidt memcpy(new_array, type->regions, old_size); 481142b45a7SBenjamin Herrenschmidt memset(new_array + type->max, 0, old_size); 482142b45a7SBenjamin Herrenschmidt old_array = type->regions; 483142b45a7SBenjamin Herrenschmidt type->regions = new_array; 484142b45a7SBenjamin Herrenschmidt type->max <<= 1; 485142b45a7SBenjamin Herrenschmidt 486fd07383bSAndrew Morton /* Free old array. We needn't free it if the array is the static one */ 487181eb394SGavin Shan if (*in_slab) 488181eb394SGavin Shan kfree(old_array); 489181eb394SGavin Shan else if (old_array != memblock_memory_init_regions && 490142b45a7SBenjamin Herrenschmidt old_array != memblock_reserved_init_regions) 4914421cca0SMike Rapoport memblock_free(old_array, old_alloc_size); 492142b45a7SBenjamin Herrenschmidt 493fd07383bSAndrew Morton /* 494fd07383bSAndrew Morton * Reserve the new array if that comes from the memblock. Otherwise, we 495fd07383bSAndrew Morton * needn't do it 496181eb394SGavin Shan */ 497181eb394SGavin Shan if (!use_slab) 49829f67386SYinghai Lu BUG_ON(memblock_reserve(addr, new_alloc_size)); 499181eb394SGavin Shan 500181eb394SGavin Shan /* Update slab flag */ 501181eb394SGavin Shan *in_slab = use_slab; 502181eb394SGavin Shan 503142b45a7SBenjamin Herrenschmidt return 0; 504142b45a7SBenjamin Herrenschmidt } 505142b45a7SBenjamin Herrenschmidt 506784656f9STejun Heo /** 507784656f9STejun Heo * memblock_merge_regions - merge neighboring compatible regions 508784656f9STejun Heo * @type: memblock type to scan 5092fe03412SPeng Zhang * @start_rgn: start scanning from (@start_rgn - 1) 5102fe03412SPeng Zhang * @end_rgn: end scanning at (@end_rgn - 1) 5112fe03412SPeng Zhang * Scan @type and merge neighboring compatible regions in [@start_rgn - 1, @end_rgn) 512784656f9STejun Heo */ 5132fe03412SPeng Zhang static void __init_memblock memblock_merge_regions(struct memblock_type *type, 5142fe03412SPeng Zhang unsigned long start_rgn, 5152fe03412SPeng Zhang unsigned long end_rgn) 516784656f9STejun Heo { 517784656f9STejun Heo int i = 0; 5182fe03412SPeng Zhang if (start_rgn) 5192fe03412SPeng Zhang i = start_rgn - 1; 5202fe03412SPeng Zhang end_rgn = min(end_rgn, type->cnt - 1); 5212fe03412SPeng Zhang while (i < end_rgn) { 522784656f9STejun Heo struct memblock_region *this = &type->regions[i]; 523784656f9STejun Heo struct memblock_region *next = &type->regions[i + 1]; 524784656f9STejun Heo 5257c0caeb8STejun Heo if (this->base + this->size != next->base || 5267c0caeb8STejun Heo memblock_get_region_node(this) != 52766a20757STang Chen memblock_get_region_node(next) || 52866a20757STang Chen this->flags != next->flags) { 529784656f9STejun Heo BUG_ON(this->base + this->size > next->base); 530784656f9STejun Heo i++; 531784656f9STejun Heo continue; 532784656f9STejun Heo } 533784656f9STejun Heo 534784656f9STejun Heo this->size += next->size; 535c0232ae8SLin Feng /* move forward from next + 1, index of which is i + 2 */ 536c0232ae8SLin Feng memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next)); 537784656f9STejun Heo type->cnt--; 5382fe03412SPeng Zhang end_rgn--; 539784656f9STejun Heo } 540784656f9STejun Heo } 541784656f9STejun Heo 542784656f9STejun Heo /** 543784656f9STejun Heo * memblock_insert_region - insert new memblock region 544784656f9STejun Heo * @type: memblock type to insert into 545784656f9STejun Heo * @idx: index for the insertion point 546784656f9STejun Heo * @base: base address of the new region 547784656f9STejun Heo * @size: size of the new region 548209ff86dSTang Chen * @nid: node id of the new region 54966a20757STang Chen * @flags: flags of the new region 550784656f9STejun Heo * 551784656f9STejun Heo * Insert new memblock region [@base, @base + @size) into @type at @idx. 552412d0008SAlexander Kuleshov * @type must already have extra room to accommodate the new region. 553784656f9STejun Heo */ 554784656f9STejun Heo static void __init_memblock memblock_insert_region(struct memblock_type *type, 555784656f9STejun Heo int idx, phys_addr_t base, 55666a20757STang Chen phys_addr_t size, 557e1720feeSMike Rapoport int nid, 558e1720feeSMike Rapoport enum memblock_flags flags) 559784656f9STejun Heo { 560784656f9STejun Heo struct memblock_region *rgn = &type->regions[idx]; 561784656f9STejun Heo 562784656f9STejun Heo BUG_ON(type->cnt >= type->max); 563784656f9STejun Heo memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); 564784656f9STejun Heo rgn->base = base; 565784656f9STejun Heo rgn->size = size; 56666a20757STang Chen rgn->flags = flags; 5677c0caeb8STejun Heo memblock_set_region_node(rgn, nid); 568784656f9STejun Heo type->cnt++; 5691440c4e2STejun Heo type->total_size += size; 570784656f9STejun Heo } 571784656f9STejun Heo 572784656f9STejun Heo /** 573f1af9d3aSPhilipp Hachtmann * memblock_add_range - add new memblock region 574784656f9STejun Heo * @type: memblock type to add new region into 575784656f9STejun Heo * @base: base address of the new region 576784656f9STejun Heo * @size: size of the new region 5777fb0bc3fSTejun Heo * @nid: nid of the new region 57866a20757STang Chen * @flags: flags of the new region 579784656f9STejun Heo * 580784656f9STejun Heo * Add new memblock region [@base, @base + @size) into @type. The new region 581784656f9STejun Heo * is allowed to overlap with existing ones - overlaps don't affect already 582784656f9STejun Heo * existing regions. @type is guaranteed to be minimal (all neighbouring 583784656f9STejun Heo * compatible regions are merged) after the addition. 584784656f9STejun Heo * 58547cec443SMike Rapoport * Return: 586784656f9STejun Heo * 0 on success, -errno on failure. 587784656f9STejun Heo */ 58802634a44SAnshuman Khandual static int __init_memblock memblock_add_range(struct memblock_type *type, 58966a20757STang Chen phys_addr_t base, phys_addr_t size, 590e1720feeSMike Rapoport int nid, enum memblock_flags flags) 59195f72d1eSYinghai Lu { 592784656f9STejun Heo bool insert = false; 593eb18f1b5STejun Heo phys_addr_t obase = base; 594eb18f1b5STejun Heo phys_addr_t end = base + memblock_cap_size(base, &size); 5952fe03412SPeng Zhang int idx, nr_new, start_rgn = -1, end_rgn; 5968c9c1701SAlexander Kuleshov struct memblock_region *rgn; 59795f72d1eSYinghai Lu 598b3dc627cSTejun Heo if (!size) 599b3dc627cSTejun Heo return 0; 600b3dc627cSTejun Heo 601784656f9STejun Heo /* special case for empty array */ 602784656f9STejun Heo if (type->regions[0].size == 0) { 6031440c4e2STejun Heo WARN_ON(type->cnt != 1 || type->total_size); 604784656f9STejun Heo type->regions[0].base = base; 605784656f9STejun Heo type->regions[0].size = size; 60666a20757STang Chen type->regions[0].flags = flags; 6077fb0bc3fSTejun Heo memblock_set_region_node(&type->regions[0], nid); 6081440c4e2STejun Heo type->total_size = size; 609784656f9STejun Heo return 0; 610784656f9STejun Heo } 61128e1a8f4SJinyu Tang 61228e1a8f4SJinyu Tang /* 61328e1a8f4SJinyu Tang * The worst case is when new range overlaps all existing regions, 61428e1a8f4SJinyu Tang * then we'll need type->cnt + 1 empty regions in @type. So if 615ad500fb2SPeng Zhang * type->cnt * 2 + 1 is less than or equal to type->max, we know 61628e1a8f4SJinyu Tang * that there is enough empty regions in @type, and we can insert 61728e1a8f4SJinyu Tang * regions directly. 61828e1a8f4SJinyu Tang */ 619ad500fb2SPeng Zhang if (type->cnt * 2 + 1 <= type->max) 62028e1a8f4SJinyu Tang insert = true; 62128e1a8f4SJinyu Tang 622784656f9STejun Heo repeat: 623784656f9STejun Heo /* 624784656f9STejun Heo * The following is executed twice. Once with %false @insert and 625784656f9STejun Heo * then with %true. The first counts the number of regions needed 626412d0008SAlexander Kuleshov * to accommodate the new area. The second actually inserts them. 627784656f9STejun Heo */ 628784656f9STejun Heo base = obase; 629784656f9STejun Heo nr_new = 0; 630784656f9STejun Heo 63166e8b438SGioh Kim for_each_memblock_type(idx, type, rgn) { 632784656f9STejun Heo phys_addr_t rbase = rgn->base; 633784656f9STejun Heo phys_addr_t rend = rbase + rgn->size; 6348f7a6605SBenjamin Herrenschmidt 635784656f9STejun Heo if (rbase >= end) 6368f7a6605SBenjamin Herrenschmidt break; 637784656f9STejun Heo if (rend <= base) 638784656f9STejun Heo continue; 639784656f9STejun Heo /* 640784656f9STejun Heo * @rgn overlaps. If it separates the lower part of new 641784656f9STejun Heo * area, insert that portion. 6428f7a6605SBenjamin Herrenschmidt */ 643784656f9STejun Heo if (rbase > base) { 644a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA 645c0a29498SWei Yang WARN_ON(nid != memblock_get_region_node(rgn)); 646c0a29498SWei Yang #endif 6474fcab5f4SWei Yang WARN_ON(flags != rgn->flags); 648784656f9STejun Heo nr_new++; 6492fe03412SPeng Zhang if (insert) { 6502fe03412SPeng Zhang if (start_rgn == -1) 6512fe03412SPeng Zhang start_rgn = idx; 6522fe03412SPeng Zhang end_rgn = idx + 1; 6538c9c1701SAlexander Kuleshov memblock_insert_region(type, idx++, base, 65466a20757STang Chen rbase - base, nid, 65566a20757STang Chen flags); 656784656f9STejun Heo } 6572fe03412SPeng Zhang } 658784656f9STejun Heo /* area below @rend is dealt with, forget about it */ 659784656f9STejun Heo base = min(rend, end); 6608f7a6605SBenjamin Herrenschmidt } 6618f7a6605SBenjamin Herrenschmidt 662784656f9STejun Heo /* insert the remaining portion */ 663784656f9STejun Heo if (base < end) { 664784656f9STejun Heo nr_new++; 6652fe03412SPeng Zhang if (insert) { 6662fe03412SPeng Zhang if (start_rgn == -1) 6672fe03412SPeng Zhang start_rgn = idx; 6682fe03412SPeng Zhang end_rgn = idx + 1; 6698c9c1701SAlexander Kuleshov memblock_insert_region(type, idx, base, end - base, 67066a20757STang Chen nid, flags); 6718f7a6605SBenjamin Herrenschmidt } 6722fe03412SPeng Zhang } 6738f7a6605SBenjamin Herrenschmidt 674ef3cc4dbSnimisolo if (!nr_new) 675ef3cc4dbSnimisolo return 0; 676ef3cc4dbSnimisolo 677784656f9STejun Heo /* 678784656f9STejun Heo * If this was the first round, resize array and repeat for actual 679784656f9STejun Heo * insertions; otherwise, merge and return. 6808f7a6605SBenjamin Herrenschmidt */ 681784656f9STejun Heo if (!insert) { 682784656f9STejun Heo while (type->cnt + nr_new > type->max) 68348c3b583SGreg Pearson if (memblock_double_array(type, obase, size) < 0) 684784656f9STejun Heo return -ENOMEM; 685784656f9STejun Heo insert = true; 686784656f9STejun Heo goto repeat; 68795f72d1eSYinghai Lu } else { 6882fe03412SPeng Zhang memblock_merge_regions(type, start_rgn, end_rgn); 68995f72d1eSYinghai Lu return 0; 69095f72d1eSYinghai Lu } 691784656f9STejun Heo } 69295f72d1eSYinghai Lu 69348a833ccSMike Rapoport /** 69448a833ccSMike Rapoport * memblock_add_node - add new memblock region within a NUMA node 69548a833ccSMike Rapoport * @base: base address of the new region 69648a833ccSMike Rapoport * @size: size of the new region 69748a833ccSMike Rapoport * @nid: nid of the new region 698952eea9bSDavid Hildenbrand * @flags: flags of the new region 69948a833ccSMike Rapoport * 70048a833ccSMike Rapoport * Add new memblock region [@base, @base + @size) to the "memory" 70148a833ccSMike Rapoport * type. See memblock_add_range() description for mode details 70248a833ccSMike Rapoport * 70348a833ccSMike Rapoport * Return: 70448a833ccSMike Rapoport * 0 on success, -errno on failure. 70548a833ccSMike Rapoport */ 7067fb0bc3fSTejun Heo int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size, 707952eea9bSDavid Hildenbrand int nid, enum memblock_flags flags) 7087fb0bc3fSTejun Heo { 70900974b9aSGeert Uytterhoeven phys_addr_t end = base + size - 1; 71000974b9aSGeert Uytterhoeven 711952eea9bSDavid Hildenbrand memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__, 712952eea9bSDavid Hildenbrand &base, &end, nid, flags, (void *)_RET_IP_); 71300974b9aSGeert Uytterhoeven 714952eea9bSDavid Hildenbrand return memblock_add_range(&memblock.memory, base, size, nid, flags); 7157fb0bc3fSTejun Heo } 7167fb0bc3fSTejun Heo 71748a833ccSMike Rapoport /** 71848a833ccSMike Rapoport * memblock_add - add new memblock region 71948a833ccSMike Rapoport * @base: base address of the new region 72048a833ccSMike Rapoport * @size: size of the new region 72148a833ccSMike Rapoport * 72248a833ccSMike Rapoport * Add new memblock region [@base, @base + @size) to the "memory" 72348a833ccSMike Rapoport * type. See memblock_add_range() description for mode details 72448a833ccSMike Rapoport * 72548a833ccSMike Rapoport * Return: 72648a833ccSMike Rapoport * 0 on success, -errno on failure. 72748a833ccSMike Rapoport */ 728f705ac4bSAlexander Kuleshov int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) 7296a4055bcSAlexander Kuleshov { 7305d63f81cSMiles Chen phys_addr_t end = base + size - 1; 7315d63f81cSMiles Chen 732a090d711SAnshuman Khandual memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 7335d63f81cSMiles Chen &base, &end, (void *)_RET_IP_); 7346a4055bcSAlexander Kuleshov 735f705ac4bSAlexander Kuleshov return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0); 73695f72d1eSYinghai Lu } 73795f72d1eSYinghai Lu 7386a9ceb31STejun Heo /** 739ff6c3d81SLiam Ni * memblock_validate_numa_coverage - check if amount of memory with 740ff6c3d81SLiam Ni * no node ID assigned is less than a threshold 741ff6c3d81SLiam Ni * @threshold_bytes: maximal number of pages that can have unassigned node 742ff6c3d81SLiam Ni * ID (in bytes). 743ff6c3d81SLiam Ni * 744ff6c3d81SLiam Ni * A buggy firmware may report memory that does not belong to any node. 745ff6c3d81SLiam Ni * Check if amount of such memory is below @threshold_bytes. 746ff6c3d81SLiam Ni * 747ff6c3d81SLiam Ni * Return: true on success, false on failure. 748ff6c3d81SLiam Ni */ 749ff6c3d81SLiam Ni bool __init_memblock memblock_validate_numa_coverage(unsigned long threshold_bytes) 750ff6c3d81SLiam Ni { 751ff6c3d81SLiam Ni unsigned long nr_pages = 0; 752ff6c3d81SLiam Ni unsigned long start_pfn, end_pfn, mem_size_mb; 753ff6c3d81SLiam Ni int nid, i; 754ff6c3d81SLiam Ni 755ff6c3d81SLiam Ni /* calculate lose page */ 756ff6c3d81SLiam Ni for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { 757ff6c3d81SLiam Ni if (nid == NUMA_NO_NODE) 758ff6c3d81SLiam Ni nr_pages += end_pfn - start_pfn; 759ff6c3d81SLiam Ni } 760ff6c3d81SLiam Ni 761ff6c3d81SLiam Ni if ((nr_pages << PAGE_SHIFT) >= threshold_bytes) { 762ff6c3d81SLiam Ni mem_size_mb = memblock_phys_mem_size() >> 20; 763ff6c3d81SLiam Ni pr_err("NUMA: no nodes coverage for %luMB of %luMB RAM\n", 764ff6c3d81SLiam Ni (nr_pages << PAGE_SHIFT) >> 20, mem_size_mb); 765ff6c3d81SLiam Ni return false; 766ff6c3d81SLiam Ni } 767ff6c3d81SLiam Ni 768ff6c3d81SLiam Ni return true; 769ff6c3d81SLiam Ni } 770ff6c3d81SLiam Ni 771ff6c3d81SLiam Ni 772ff6c3d81SLiam Ni /** 7736a9ceb31STejun Heo * memblock_isolate_range - isolate given range into disjoint memblocks 7746a9ceb31STejun Heo * @type: memblock type to isolate range for 7756a9ceb31STejun Heo * @base: base of range to isolate 7766a9ceb31STejun Heo * @size: size of range to isolate 7776a9ceb31STejun Heo * @start_rgn: out parameter for the start of isolated region 7786a9ceb31STejun Heo * @end_rgn: out parameter for the end of isolated region 7796a9ceb31STejun Heo * 7806a9ceb31STejun Heo * Walk @type and ensure that regions don't cross the boundaries defined by 7816a9ceb31STejun Heo * [@base, @base + @size). Crossing regions are split at the boundaries, 7826a9ceb31STejun Heo * which may create at most two more regions. The index of the first 7836a9ceb31STejun Heo * region inside the range is returned in *@start_rgn and end in *@end_rgn. 7846a9ceb31STejun Heo * 78547cec443SMike Rapoport * Return: 7866a9ceb31STejun Heo * 0 on success, -errno on failure. 7876a9ceb31STejun Heo */ 7886a9ceb31STejun Heo static int __init_memblock memblock_isolate_range(struct memblock_type *type, 7896a9ceb31STejun Heo phys_addr_t base, phys_addr_t size, 7906a9ceb31STejun Heo int *start_rgn, int *end_rgn) 7916a9ceb31STejun Heo { 792eb18f1b5STejun Heo phys_addr_t end = base + memblock_cap_size(base, &size); 7938c9c1701SAlexander Kuleshov int idx; 7948c9c1701SAlexander Kuleshov struct memblock_region *rgn; 7956a9ceb31STejun Heo 7966a9ceb31STejun Heo *start_rgn = *end_rgn = 0; 7976a9ceb31STejun Heo 798b3dc627cSTejun Heo if (!size) 799b3dc627cSTejun Heo return 0; 800b3dc627cSTejun Heo 8016a9ceb31STejun Heo /* we'll create at most two more regions */ 8026a9ceb31STejun Heo while (type->cnt + 2 > type->max) 80348c3b583SGreg Pearson if (memblock_double_array(type, base, size) < 0) 8046a9ceb31STejun Heo return -ENOMEM; 8056a9ceb31STejun Heo 80666e8b438SGioh Kim for_each_memblock_type(idx, type, rgn) { 8076a9ceb31STejun Heo phys_addr_t rbase = rgn->base; 8086a9ceb31STejun Heo phys_addr_t rend = rbase + rgn->size; 8096a9ceb31STejun Heo 8106a9ceb31STejun Heo if (rbase >= end) 8116a9ceb31STejun Heo break; 8126a9ceb31STejun Heo if (rend <= base) 8136a9ceb31STejun Heo continue; 8146a9ceb31STejun Heo 8156a9ceb31STejun Heo if (rbase < base) { 8166a9ceb31STejun Heo /* 8176a9ceb31STejun Heo * @rgn intersects from below. Split and continue 8186a9ceb31STejun Heo * to process the next region - the new top half. 8196a9ceb31STejun Heo */ 8206a9ceb31STejun Heo rgn->base = base; 8211440c4e2STejun Heo rgn->size -= base - rbase; 8221440c4e2STejun Heo type->total_size -= base - rbase; 8238c9c1701SAlexander Kuleshov memblock_insert_region(type, idx, rbase, base - rbase, 82466a20757STang Chen memblock_get_region_node(rgn), 82566a20757STang Chen rgn->flags); 8266a9ceb31STejun Heo } else if (rend > end) { 8276a9ceb31STejun Heo /* 8286a9ceb31STejun Heo * @rgn intersects from above. Split and redo the 8296a9ceb31STejun Heo * current region - the new bottom half. 8306a9ceb31STejun Heo */ 8316a9ceb31STejun Heo rgn->base = end; 8321440c4e2STejun Heo rgn->size -= end - rbase; 8331440c4e2STejun Heo type->total_size -= end - rbase; 8348c9c1701SAlexander Kuleshov memblock_insert_region(type, idx--, rbase, end - rbase, 83566a20757STang Chen memblock_get_region_node(rgn), 83666a20757STang Chen rgn->flags); 8376a9ceb31STejun Heo } else { 8386a9ceb31STejun Heo /* @rgn is fully contained, record it */ 8396a9ceb31STejun Heo if (!*end_rgn) 8408c9c1701SAlexander Kuleshov *start_rgn = idx; 8418c9c1701SAlexander Kuleshov *end_rgn = idx + 1; 8426a9ceb31STejun Heo } 8436a9ceb31STejun Heo } 8446a9ceb31STejun Heo 8456a9ceb31STejun Heo return 0; 8466a9ceb31STejun Heo } 8476a9ceb31STejun Heo 84835bd16a2SAlexander Kuleshov static int __init_memblock memblock_remove_range(struct memblock_type *type, 8498f7a6605SBenjamin Herrenschmidt phys_addr_t base, phys_addr_t size) 85095f72d1eSYinghai Lu { 85171936180STejun Heo int start_rgn, end_rgn; 85271936180STejun Heo int i, ret; 85395f72d1eSYinghai Lu 85471936180STejun Heo ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 85571936180STejun Heo if (ret) 85671936180STejun Heo return ret; 85795f72d1eSYinghai Lu 85871936180STejun Heo for (i = end_rgn - 1; i >= start_rgn; i--) 85971936180STejun Heo memblock_remove_region(type, i); 86095f72d1eSYinghai Lu return 0; 86195f72d1eSYinghai Lu } 86295f72d1eSYinghai Lu 863581adcbeSTejun Heo int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size) 86495f72d1eSYinghai Lu { 86525cf23d7SMinchan Kim phys_addr_t end = base + size - 1; 86625cf23d7SMinchan Kim 867a090d711SAnshuman Khandual memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 86825cf23d7SMinchan Kim &base, &end, (void *)_RET_IP_); 86925cf23d7SMinchan Kim 870f1af9d3aSPhilipp Hachtmann return memblock_remove_range(&memblock.memory, base, size); 87195f72d1eSYinghai Lu } 87295f72d1eSYinghai Lu 8734d72868cSMike Rapoport /** 8744421cca0SMike Rapoport * memblock_free - free boot memory allocation 87577e02cf5SLinus Torvalds * @ptr: starting address of the boot memory allocation 87677e02cf5SLinus Torvalds * @size: size of the boot memory block in bytes 87777e02cf5SLinus Torvalds * 87877e02cf5SLinus Torvalds * Free boot memory block previously allocated by memblock_alloc_xx() API. 87977e02cf5SLinus Torvalds * The freeing memory will not be released to the buddy allocator. 88077e02cf5SLinus Torvalds */ 8814421cca0SMike Rapoport void __init_memblock memblock_free(void *ptr, size_t size) 88277e02cf5SLinus Torvalds { 88377e02cf5SLinus Torvalds if (ptr) 8843ecc6834SMike Rapoport memblock_phys_free(__pa(ptr), size); 88577e02cf5SLinus Torvalds } 88677e02cf5SLinus Torvalds 88777e02cf5SLinus Torvalds /** 8883ecc6834SMike Rapoport * memblock_phys_free - free boot memory block 8894d72868cSMike Rapoport * @base: phys starting address of the boot memory block 8904d72868cSMike Rapoport * @size: size of the boot memory block in bytes 8914d72868cSMike Rapoport * 892fa81ab49SMiaoqian Lin * Free boot memory block previously allocated by memblock_phys_alloc_xx() API. 8934d72868cSMike Rapoport * The freeing memory will not be released to the buddy allocator. 8944d72868cSMike Rapoport */ 8953ecc6834SMike Rapoport int __init_memblock memblock_phys_free(phys_addr_t base, phys_addr_t size) 89695f72d1eSYinghai Lu { 8975d63f81cSMiles Chen phys_addr_t end = base + size - 1; 8985d63f81cSMiles Chen 899a090d711SAnshuman Khandual memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 9005d63f81cSMiles Chen &base, &end, (void *)_RET_IP_); 90124aa0788STejun Heo 9029099daedSCatalin Marinas kmemleak_free_part_phys(base, size); 903f1af9d3aSPhilipp Hachtmann return memblock_remove_range(&memblock.reserved, base, size); 90495f72d1eSYinghai Lu } 90595f72d1eSYinghai Lu 906f705ac4bSAlexander Kuleshov int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) 90795f72d1eSYinghai Lu { 9085d63f81cSMiles Chen phys_addr_t end = base + size - 1; 9095d63f81cSMiles Chen 910a090d711SAnshuman Khandual memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 9115d63f81cSMiles Chen &base, &end, (void *)_RET_IP_); 91295f72d1eSYinghai Lu 913f705ac4bSAlexander Kuleshov return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0); 91495f72d1eSYinghai Lu } 91595f72d1eSYinghai Lu 91602634a44SAnshuman Khandual #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 91702634a44SAnshuman Khandual int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size) 91802634a44SAnshuman Khandual { 91902634a44SAnshuman Khandual phys_addr_t end = base + size - 1; 92002634a44SAnshuman Khandual 92102634a44SAnshuman Khandual memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 92202634a44SAnshuman Khandual &base, &end, (void *)_RET_IP_); 92302634a44SAnshuman Khandual 92477649905SDavid Hildenbrand return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0); 92502634a44SAnshuman Khandual } 92602634a44SAnshuman Khandual #endif 92702634a44SAnshuman Khandual 92835fd0808STejun Heo /** 92947cec443SMike Rapoport * memblock_setclr_flag - set or clear flag for a memory region 930ee8d2071SUsama Arif * @type: memblock type to set/clear flag for 93147cec443SMike Rapoport * @base: base address of the region 93247cec443SMike Rapoport * @size: size of the region 93347cec443SMike Rapoport * @set: set or clear the flag 9348958b249SHaitao Shi * @flag: the flag to update 93566b16edfSTang Chen * 9364308ce17STony Luck * This function isolates region [@base, @base + @size), and sets/clears flag 93766b16edfSTang Chen * 93847cec443SMike Rapoport * Return: 0 on success, -errno on failure. 93966b16edfSTang Chen */ 940ee8d2071SUsama Arif static int __init_memblock memblock_setclr_flag(struct memblock_type *type, 941ee8d2071SUsama Arif phys_addr_t base, phys_addr_t size, int set, int flag) 94266b16edfSTang Chen { 94366b16edfSTang Chen int i, ret, start_rgn, end_rgn; 94466b16edfSTang Chen 94566b16edfSTang Chen ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 94666b16edfSTang Chen if (ret) 94766b16edfSTang Chen return ret; 94866b16edfSTang Chen 949fe145124SMike Rapoport for (i = start_rgn; i < end_rgn; i++) { 950fe145124SMike Rapoport struct memblock_region *r = &type->regions[i]; 951fe145124SMike Rapoport 9524308ce17STony Luck if (set) 953fe145124SMike Rapoport r->flags |= flag; 9544308ce17STony Luck else 955fe145124SMike Rapoport r->flags &= ~flag; 956fe145124SMike Rapoport } 95766b16edfSTang Chen 9582fe03412SPeng Zhang memblock_merge_regions(type, start_rgn, end_rgn); 95966b16edfSTang Chen return 0; 96066b16edfSTang Chen } 96166b16edfSTang Chen 96266b16edfSTang Chen /** 9634308ce17STony Luck * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG. 9644308ce17STony Luck * @base: the base phys addr of the region 9654308ce17STony Luck * @size: the size of the region 9664308ce17STony Luck * 96747cec443SMike Rapoport * Return: 0 on success, -errno on failure. 9684308ce17STony Luck */ 9694308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size) 9704308ce17STony Luck { 971ee8d2071SUsama Arif return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_HOTPLUG); 9724308ce17STony Luck } 9734308ce17STony Luck 9744308ce17STony Luck /** 97566b16edfSTang Chen * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region. 97666b16edfSTang Chen * @base: the base phys addr of the region 97766b16edfSTang Chen * @size: the size of the region 97866b16edfSTang Chen * 97947cec443SMike Rapoport * Return: 0 on success, -errno on failure. 98066b16edfSTang Chen */ 98166b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size) 98266b16edfSTang Chen { 983ee8d2071SUsama Arif return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_HOTPLUG); 98466b16edfSTang Chen } 98566b16edfSTang Chen 98666b16edfSTang Chen /** 987a3f5bafcSTony Luck * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR. 988a3f5bafcSTony Luck * @base: the base phys addr of the region 989a3f5bafcSTony Luck * @size: the size of the region 990a3f5bafcSTony Luck * 99147cec443SMike Rapoport * Return: 0 on success, -errno on failure. 992a3f5bafcSTony Luck */ 993a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size) 994a3f5bafcSTony Luck { 995902c2d91SMa Wupeng if (!mirrored_kernelcore) 996902c2d91SMa Wupeng return 0; 997902c2d91SMa Wupeng 998a3f5bafcSTony Luck system_has_some_mirror = true; 999a3f5bafcSTony Luck 1000ee8d2071SUsama Arif return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_MIRROR); 1001a3f5bafcSTony Luck } 1002a3f5bafcSTony Luck 1003bf3d3cc5SArd Biesheuvel /** 1004bf3d3cc5SArd Biesheuvel * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP. 1005bf3d3cc5SArd Biesheuvel * @base: the base phys addr of the region 1006bf3d3cc5SArd Biesheuvel * @size: the size of the region 1007bf3d3cc5SArd Biesheuvel * 10089092d4f7SMike Rapoport * The memory regions marked with %MEMBLOCK_NOMAP will not be added to the 10099092d4f7SMike Rapoport * direct mapping of the physical memory. These regions will still be 10109092d4f7SMike Rapoport * covered by the memory map. The struct page representing NOMAP memory 10119092d4f7SMike Rapoport * frames in the memory map will be PageReserved() 10129092d4f7SMike Rapoport * 1013658aafc8SMike Rapoport * Note: if the memory being marked %MEMBLOCK_NOMAP was allocated from 1014658aafc8SMike Rapoport * memblock, the caller must inform kmemleak to ignore that memory 1015658aafc8SMike Rapoport * 101647cec443SMike Rapoport * Return: 0 on success, -errno on failure. 1017bf3d3cc5SArd Biesheuvel */ 1018bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size) 1019bf3d3cc5SArd Biesheuvel { 1020ee8d2071SUsama Arif return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP); 1021bf3d3cc5SArd Biesheuvel } 1022a3f5bafcSTony Luck 1023a3f5bafcSTony Luck /** 10244c546b8aSAKASHI Takahiro * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region. 10254c546b8aSAKASHI Takahiro * @base: the base phys addr of the region 10264c546b8aSAKASHI Takahiro * @size: the size of the region 10274c546b8aSAKASHI Takahiro * 102847cec443SMike Rapoport * Return: 0 on success, -errno on failure. 10294c546b8aSAKASHI Takahiro */ 10304c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size) 10314c546b8aSAKASHI Takahiro { 1032ee8d2071SUsama Arif return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_NOMAP); 10334c546b8aSAKASHI Takahiro } 10344c546b8aSAKASHI Takahiro 103577e6c43eSUsama Arif /** 103677e6c43eSUsama Arif * memblock_reserved_mark_noinit - Mark a reserved memory region with flag 103777e6c43eSUsama Arif * MEMBLOCK_RSRV_NOINIT which results in the struct pages not being initialized 103877e6c43eSUsama Arif * for this region. 103977e6c43eSUsama Arif * @base: the base phys addr of the region 104077e6c43eSUsama Arif * @size: the size of the region 104177e6c43eSUsama Arif * 104277e6c43eSUsama Arif * struct pages will not be initialized for reserved memory regions marked with 104377e6c43eSUsama Arif * %MEMBLOCK_RSRV_NOINIT. 104477e6c43eSUsama Arif * 104577e6c43eSUsama Arif * Return: 0 on success, -errno on failure. 104677e6c43eSUsama Arif */ 104777e6c43eSUsama Arif int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size) 104877e6c43eSUsama Arif { 104977e6c43eSUsama Arif return memblock_setclr_flag(&memblock.reserved, base, size, 1, 105077e6c43eSUsama Arif MEMBLOCK_RSRV_NOINIT); 105177e6c43eSUsama Arif } 105277e6c43eSUsama Arif 10539f3d5eaaSMike Rapoport static bool should_skip_region(struct memblock_type *type, 10549f3d5eaaSMike Rapoport struct memblock_region *m, 10559f3d5eaaSMike Rapoport int nid, int flags) 1056c9a688a3SMike Rapoport { 1057c9a688a3SMike Rapoport int m_nid = memblock_get_region_node(m); 1058c9a688a3SMike Rapoport 10599f3d5eaaSMike Rapoport /* we never skip regions when iterating memblock.reserved or physmem */ 10609f3d5eaaSMike Rapoport if (type != memblock_memory) 10619f3d5eaaSMike Rapoport return false; 10629f3d5eaaSMike Rapoport 1063c9a688a3SMike Rapoport /* only memory regions are associated with nodes, check it */ 1064c9a688a3SMike Rapoport if (nid != NUMA_NO_NODE && nid != m_nid) 1065c9a688a3SMike Rapoport return true; 1066c9a688a3SMike Rapoport 1067c9a688a3SMike Rapoport /* skip hotpluggable memory regions if needed */ 106879e482e9SMike Rapoport if (movable_node_is_enabled() && memblock_is_hotpluggable(m) && 106979e482e9SMike Rapoport !(flags & MEMBLOCK_HOTPLUG)) 1070c9a688a3SMike Rapoport return true; 1071c9a688a3SMike Rapoport 1072c9a688a3SMike Rapoport /* if we want mirror memory skip non-mirror memory regions */ 1073c9a688a3SMike Rapoport if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) 1074c9a688a3SMike Rapoport return true; 1075c9a688a3SMike Rapoport 1076c9a688a3SMike Rapoport /* skip nomap memory unless we were asked for it explicitly */ 1077c9a688a3SMike Rapoport if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) 1078c9a688a3SMike Rapoport return true; 1079c9a688a3SMike Rapoport 1080f7892d8eSDavid Hildenbrand /* skip driver-managed memory unless we were asked for it explicitly */ 1081f7892d8eSDavid Hildenbrand if (!(flags & MEMBLOCK_DRIVER_MANAGED) && memblock_is_driver_managed(m)) 1082f7892d8eSDavid Hildenbrand return true; 1083f7892d8eSDavid Hildenbrand 1084c9a688a3SMike Rapoport return false; 1085c9a688a3SMike Rapoport } 1086c9a688a3SMike Rapoport 10878e7a7f86SRobin Holt /** 1088a2974133SMike Rapoport * __next_mem_range - next function for for_each_free_mem_range() etc. 108935fd0808STejun Heo * @idx: pointer to u64 loop variable 1090b1154233SGrygorii Strashko * @nid: node selector, %NUMA_NO_NODE for all nodes 1091fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 1092f1af9d3aSPhilipp Hachtmann * @type_a: pointer to memblock_type from where the range is taken 1093f1af9d3aSPhilipp Hachtmann * @type_b: pointer to memblock_type which excludes memory from being taken 1094dad7557eSWanpeng Li * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 1095dad7557eSWanpeng Li * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 1096dad7557eSWanpeng Li * @out_nid: ptr to int for nid of the range, can be %NULL 109735fd0808STejun Heo * 1098f1af9d3aSPhilipp Hachtmann * Find the first area from *@idx which matches @nid, fill the out 109935fd0808STejun Heo * parameters, and update *@idx for the next iteration. The lower 32bit of 1100f1af9d3aSPhilipp Hachtmann * *@idx contains index into type_a and the upper 32bit indexes the 1101f1af9d3aSPhilipp Hachtmann * areas before each region in type_b. For example, if type_b regions 110235fd0808STejun Heo * look like the following, 110335fd0808STejun Heo * 110435fd0808STejun Heo * 0:[0-16), 1:[32-48), 2:[128-130) 110535fd0808STejun Heo * 110635fd0808STejun Heo * The upper 32bit indexes the following regions. 110735fd0808STejun Heo * 110835fd0808STejun Heo * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) 110935fd0808STejun Heo * 111035fd0808STejun Heo * As both region arrays are sorted, the function advances the two indices 111135fd0808STejun Heo * in lockstep and returns each intersection. 111235fd0808STejun Heo */ 111377649905SDavid Hildenbrand void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags, 1114f1af9d3aSPhilipp Hachtmann struct memblock_type *type_a, 111577649905SDavid Hildenbrand struct memblock_type *type_b, phys_addr_t *out_start, 111635fd0808STejun Heo phys_addr_t *out_end, int *out_nid) 111735fd0808STejun Heo { 1118f1af9d3aSPhilipp Hachtmann int idx_a = *idx & 0xffffffff; 1119f1af9d3aSPhilipp Hachtmann int idx_b = *idx >> 32; 1120b1154233SGrygorii Strashko 1121f1af9d3aSPhilipp Hachtmann if (WARN_ONCE(nid == MAX_NUMNODES, 1122f1af9d3aSPhilipp Hachtmann "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) 1123560dca27SGrygorii Strashko nid = NUMA_NO_NODE; 112435fd0808STejun Heo 1125f1af9d3aSPhilipp Hachtmann for (; idx_a < type_a->cnt; idx_a++) { 1126f1af9d3aSPhilipp Hachtmann struct memblock_region *m = &type_a->regions[idx_a]; 1127f1af9d3aSPhilipp Hachtmann 112835fd0808STejun Heo phys_addr_t m_start = m->base; 112935fd0808STejun Heo phys_addr_t m_end = m->base + m->size; 1130f1af9d3aSPhilipp Hachtmann int m_nid = memblock_get_region_node(m); 113135fd0808STejun Heo 11329f3d5eaaSMike Rapoport if (should_skip_region(type_a, m, nid, flags)) 1133bf3d3cc5SArd Biesheuvel continue; 1134bf3d3cc5SArd Biesheuvel 1135f1af9d3aSPhilipp Hachtmann if (!type_b) { 1136f1af9d3aSPhilipp Hachtmann if (out_start) 1137f1af9d3aSPhilipp Hachtmann *out_start = m_start; 1138f1af9d3aSPhilipp Hachtmann if (out_end) 1139f1af9d3aSPhilipp Hachtmann *out_end = m_end; 1140f1af9d3aSPhilipp Hachtmann if (out_nid) 1141f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 1142f1af9d3aSPhilipp Hachtmann idx_a++; 1143f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 1144f1af9d3aSPhilipp Hachtmann return; 1145f1af9d3aSPhilipp Hachtmann } 114635fd0808STejun Heo 1147f1af9d3aSPhilipp Hachtmann /* scan areas before each reservation */ 1148f1af9d3aSPhilipp Hachtmann for (; idx_b < type_b->cnt + 1; idx_b++) { 1149f1af9d3aSPhilipp Hachtmann struct memblock_region *r; 1150f1af9d3aSPhilipp Hachtmann phys_addr_t r_start; 1151f1af9d3aSPhilipp Hachtmann phys_addr_t r_end; 1152f1af9d3aSPhilipp Hachtmann 1153f1af9d3aSPhilipp Hachtmann r = &type_b->regions[idx_b]; 1154f1af9d3aSPhilipp Hachtmann r_start = idx_b ? r[-1].base + r[-1].size : 0; 1155f1af9d3aSPhilipp Hachtmann r_end = idx_b < type_b->cnt ? 11561c4bc43dSStefan Agner r->base : PHYS_ADDR_MAX; 1157f1af9d3aSPhilipp Hachtmann 1158f1af9d3aSPhilipp Hachtmann /* 1159f1af9d3aSPhilipp Hachtmann * if idx_b advanced past idx_a, 1160f1af9d3aSPhilipp Hachtmann * break out to advance idx_a 1161f1af9d3aSPhilipp Hachtmann */ 116235fd0808STejun Heo if (r_start >= m_end) 116335fd0808STejun Heo break; 116435fd0808STejun Heo /* if the two regions intersect, we're done */ 116535fd0808STejun Heo if (m_start < r_end) { 116635fd0808STejun Heo if (out_start) 1167f1af9d3aSPhilipp Hachtmann *out_start = 1168f1af9d3aSPhilipp Hachtmann max(m_start, r_start); 116935fd0808STejun Heo if (out_end) 117035fd0808STejun Heo *out_end = min(m_end, r_end); 117135fd0808STejun Heo if (out_nid) 1172f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 117335fd0808STejun Heo /* 1174f1af9d3aSPhilipp Hachtmann * The region which ends first is 1175f1af9d3aSPhilipp Hachtmann * advanced for the next iteration. 117635fd0808STejun Heo */ 117735fd0808STejun Heo if (m_end <= r_end) 1178f1af9d3aSPhilipp Hachtmann idx_a++; 117935fd0808STejun Heo else 1180f1af9d3aSPhilipp Hachtmann idx_b++; 1181f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 118235fd0808STejun Heo return; 118335fd0808STejun Heo } 118435fd0808STejun Heo } 118535fd0808STejun Heo } 118635fd0808STejun Heo 118735fd0808STejun Heo /* signal end of iteration */ 118835fd0808STejun Heo *idx = ULLONG_MAX; 118935fd0808STejun Heo } 119035fd0808STejun Heo 11917bd0b0f0STejun Heo /** 1192f1af9d3aSPhilipp Hachtmann * __next_mem_range_rev - generic next function for for_each_*_range_rev() 1193f1af9d3aSPhilipp Hachtmann * 11947bd0b0f0STejun Heo * @idx: pointer to u64 loop variable 1195ad5ea8cdSAlexander Kuleshov * @nid: node selector, %NUMA_NO_NODE for all nodes 1196fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 1197f1af9d3aSPhilipp Hachtmann * @type_a: pointer to memblock_type from where the range is taken 1198f1af9d3aSPhilipp Hachtmann * @type_b: pointer to memblock_type which excludes memory from being taken 1199dad7557eSWanpeng Li * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 1200dad7557eSWanpeng Li * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 1201dad7557eSWanpeng Li * @out_nid: ptr to int for nid of the range, can be %NULL 12027bd0b0f0STejun Heo * 120347cec443SMike Rapoport * Finds the next range from type_a which is not marked as unsuitable 120447cec443SMike Rapoport * in type_b. 120547cec443SMike Rapoport * 1206f1af9d3aSPhilipp Hachtmann * Reverse of __next_mem_range(). 12077bd0b0f0STejun Heo */ 1208e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid, 1209e1720feeSMike Rapoport enum memblock_flags flags, 1210f1af9d3aSPhilipp Hachtmann struct memblock_type *type_a, 1211f1af9d3aSPhilipp Hachtmann struct memblock_type *type_b, 12127bd0b0f0STejun Heo phys_addr_t *out_start, 12137bd0b0f0STejun Heo phys_addr_t *out_end, int *out_nid) 12147bd0b0f0STejun Heo { 1215f1af9d3aSPhilipp Hachtmann int idx_a = *idx & 0xffffffff; 1216f1af9d3aSPhilipp Hachtmann int idx_b = *idx >> 32; 1217b1154233SGrygorii Strashko 1218560dca27SGrygorii Strashko if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) 1219560dca27SGrygorii Strashko nid = NUMA_NO_NODE; 12207bd0b0f0STejun Heo 12217bd0b0f0STejun Heo if (*idx == (u64)ULLONG_MAX) { 1222f1af9d3aSPhilipp Hachtmann idx_a = type_a->cnt - 1; 1223e47608abSzijun_hu if (type_b != NULL) 1224f1af9d3aSPhilipp Hachtmann idx_b = type_b->cnt; 1225e47608abSzijun_hu else 1226e47608abSzijun_hu idx_b = 0; 12277bd0b0f0STejun Heo } 12287bd0b0f0STejun Heo 1229f1af9d3aSPhilipp Hachtmann for (; idx_a >= 0; idx_a--) { 1230f1af9d3aSPhilipp Hachtmann struct memblock_region *m = &type_a->regions[idx_a]; 1231f1af9d3aSPhilipp Hachtmann 12327bd0b0f0STejun Heo phys_addr_t m_start = m->base; 12337bd0b0f0STejun Heo phys_addr_t m_end = m->base + m->size; 1234f1af9d3aSPhilipp Hachtmann int m_nid = memblock_get_region_node(m); 12357bd0b0f0STejun Heo 12369f3d5eaaSMike Rapoport if (should_skip_region(type_a, m, nid, flags)) 1237bf3d3cc5SArd Biesheuvel continue; 1238bf3d3cc5SArd Biesheuvel 1239f1af9d3aSPhilipp Hachtmann if (!type_b) { 1240f1af9d3aSPhilipp Hachtmann if (out_start) 1241f1af9d3aSPhilipp Hachtmann *out_start = m_start; 1242f1af9d3aSPhilipp Hachtmann if (out_end) 1243f1af9d3aSPhilipp Hachtmann *out_end = m_end; 1244f1af9d3aSPhilipp Hachtmann if (out_nid) 1245f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 1246fb399b48Szijun_hu idx_a--; 1247f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 1248f1af9d3aSPhilipp Hachtmann return; 1249f1af9d3aSPhilipp Hachtmann } 12507bd0b0f0STejun Heo 1251f1af9d3aSPhilipp Hachtmann /* scan areas before each reservation */ 1252f1af9d3aSPhilipp Hachtmann for (; idx_b >= 0; idx_b--) { 1253f1af9d3aSPhilipp Hachtmann struct memblock_region *r; 1254f1af9d3aSPhilipp Hachtmann phys_addr_t r_start; 1255f1af9d3aSPhilipp Hachtmann phys_addr_t r_end; 1256f1af9d3aSPhilipp Hachtmann 1257f1af9d3aSPhilipp Hachtmann r = &type_b->regions[idx_b]; 1258f1af9d3aSPhilipp Hachtmann r_start = idx_b ? r[-1].base + r[-1].size : 0; 1259f1af9d3aSPhilipp Hachtmann r_end = idx_b < type_b->cnt ? 12601c4bc43dSStefan Agner r->base : PHYS_ADDR_MAX; 1261f1af9d3aSPhilipp Hachtmann /* 1262f1af9d3aSPhilipp Hachtmann * if idx_b advanced past idx_a, 1263f1af9d3aSPhilipp Hachtmann * break out to advance idx_a 1264f1af9d3aSPhilipp Hachtmann */ 1265f1af9d3aSPhilipp Hachtmann 12667bd0b0f0STejun Heo if (r_end <= m_start) 12677bd0b0f0STejun Heo break; 12687bd0b0f0STejun Heo /* if the two regions intersect, we're done */ 12697bd0b0f0STejun Heo if (m_end > r_start) { 12707bd0b0f0STejun Heo if (out_start) 12717bd0b0f0STejun Heo *out_start = max(m_start, r_start); 12727bd0b0f0STejun Heo if (out_end) 12737bd0b0f0STejun Heo *out_end = min(m_end, r_end); 12747bd0b0f0STejun Heo if (out_nid) 1275f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 12767bd0b0f0STejun Heo if (m_start >= r_start) 1277f1af9d3aSPhilipp Hachtmann idx_a--; 12787bd0b0f0STejun Heo else 1279f1af9d3aSPhilipp Hachtmann idx_b--; 1280f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 12817bd0b0f0STejun Heo return; 12827bd0b0f0STejun Heo } 12837bd0b0f0STejun Heo } 12847bd0b0f0STejun Heo } 1285f1af9d3aSPhilipp Hachtmann /* signal end of iteration */ 12867bd0b0f0STejun Heo *idx = ULLONG_MAX; 12877bd0b0f0STejun Heo } 12887bd0b0f0STejun Heo 12897c0caeb8STejun Heo /* 129045e79815SChen Chang * Common iterator interface used to define for_each_mem_pfn_range(). 12917c0caeb8STejun Heo */ 12927c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid, 12937c0caeb8STejun Heo unsigned long *out_start_pfn, 12947c0caeb8STejun Heo unsigned long *out_end_pfn, int *out_nid) 12957c0caeb8STejun Heo { 12967c0caeb8STejun Heo struct memblock_type *type = &memblock.memory; 12977c0caeb8STejun Heo struct memblock_region *r; 1298d622abf7SMike Rapoport int r_nid; 12997c0caeb8STejun Heo 13007c0caeb8STejun Heo while (++*idx < type->cnt) { 13017c0caeb8STejun Heo r = &type->regions[*idx]; 1302d622abf7SMike Rapoport r_nid = memblock_get_region_node(r); 13037c0caeb8STejun Heo 13047c0caeb8STejun Heo if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) 13057c0caeb8STejun Heo continue; 1306d622abf7SMike Rapoport if (nid == MAX_NUMNODES || nid == r_nid) 13077c0caeb8STejun Heo break; 13087c0caeb8STejun Heo } 13097c0caeb8STejun Heo if (*idx >= type->cnt) { 13107c0caeb8STejun Heo *idx = -1; 13117c0caeb8STejun Heo return; 13127c0caeb8STejun Heo } 13137c0caeb8STejun Heo 13147c0caeb8STejun Heo if (out_start_pfn) 13157c0caeb8STejun Heo *out_start_pfn = PFN_UP(r->base); 13167c0caeb8STejun Heo if (out_end_pfn) 13177c0caeb8STejun Heo *out_end_pfn = PFN_DOWN(r->base + r->size); 13187c0caeb8STejun Heo if (out_nid) 1319d622abf7SMike Rapoport *out_nid = r_nid; 13207c0caeb8STejun Heo } 13217c0caeb8STejun Heo 13227c0caeb8STejun Heo /** 13237c0caeb8STejun Heo * memblock_set_node - set node ID on memblock regions 13247c0caeb8STejun Heo * @base: base of area to set node ID for 13257c0caeb8STejun Heo * @size: size of area to set node ID for 1326e7e8de59STang Chen * @type: memblock type to set node ID for 13277c0caeb8STejun Heo * @nid: node ID to set 13287c0caeb8STejun Heo * 1329e7e8de59STang Chen * Set the nid of memblock @type regions in [@base, @base + @size) to @nid. 13307c0caeb8STejun Heo * Regions which cross the area boundaries are split as necessary. 13317c0caeb8STejun Heo * 133247cec443SMike Rapoport * Return: 13337c0caeb8STejun Heo * 0 on success, -errno on failure. 13347c0caeb8STejun Heo */ 13357c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, 1336e7e8de59STang Chen struct memblock_type *type, int nid) 13377c0caeb8STejun Heo { 1338a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA 13396a9ceb31STejun Heo int start_rgn, end_rgn; 13406a9ceb31STejun Heo int i, ret; 13417c0caeb8STejun Heo 13426a9ceb31STejun Heo ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 13436a9ceb31STejun Heo if (ret) 13446a9ceb31STejun Heo return ret; 13457c0caeb8STejun Heo 13466a9ceb31STejun Heo for (i = start_rgn; i < end_rgn; i++) 1347e9d24ad3SWanpeng Li memblock_set_region_node(&type->regions[i], nid); 13487c0caeb8STejun Heo 13492fe03412SPeng Zhang memblock_merge_regions(type, start_rgn, end_rgn); 13503f08a302SMike Rapoport #endif 13517c0caeb8STejun Heo return 0; 13527c0caeb8STejun Heo } 13533f08a302SMike Rapoport 1354837566e7SAlexander Duyck #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 1355837566e7SAlexander Duyck /** 1356837566e7SAlexander Duyck * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone() 1357837566e7SAlexander Duyck * 1358837566e7SAlexander Duyck * @idx: pointer to u64 loop variable 1359837566e7SAlexander Duyck * @zone: zone in which all of the memory blocks reside 1360837566e7SAlexander Duyck * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL 1361837566e7SAlexander Duyck * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL 1362837566e7SAlexander Duyck * 1363837566e7SAlexander Duyck * This function is meant to be a zone/pfn specific wrapper for the 1364837566e7SAlexander Duyck * for_each_mem_range type iterators. Specifically they are used in the 1365837566e7SAlexander Duyck * deferred memory init routines and as such we were duplicating much of 1366837566e7SAlexander Duyck * this logic throughout the code. So instead of having it in multiple 1367837566e7SAlexander Duyck * locations it seemed like it would make more sense to centralize this to 1368837566e7SAlexander Duyck * one new iterator that does everything they need. 1369837566e7SAlexander Duyck */ 1370837566e7SAlexander Duyck void __init_memblock 1371837566e7SAlexander Duyck __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone, 1372837566e7SAlexander Duyck unsigned long *out_spfn, unsigned long *out_epfn) 1373837566e7SAlexander Duyck { 1374837566e7SAlexander Duyck int zone_nid = zone_to_nid(zone); 1375837566e7SAlexander Duyck phys_addr_t spa, epa; 1376837566e7SAlexander Duyck 1377837566e7SAlexander Duyck __next_mem_range(idx, zone_nid, MEMBLOCK_NONE, 1378837566e7SAlexander Duyck &memblock.memory, &memblock.reserved, 1379f30b002cSMiaohe Lin &spa, &epa, NULL); 1380837566e7SAlexander Duyck 1381837566e7SAlexander Duyck while (*idx != U64_MAX) { 1382837566e7SAlexander Duyck unsigned long epfn = PFN_DOWN(epa); 1383837566e7SAlexander Duyck unsigned long spfn = PFN_UP(spa); 1384837566e7SAlexander Duyck 1385837566e7SAlexander Duyck /* 1386837566e7SAlexander Duyck * Verify the end is at least past the start of the zone and 1387837566e7SAlexander Duyck * that we have at least one PFN to initialize. 1388837566e7SAlexander Duyck */ 1389837566e7SAlexander Duyck if (zone->zone_start_pfn < epfn && spfn < epfn) { 1390837566e7SAlexander Duyck /* if we went too far just stop searching */ 1391837566e7SAlexander Duyck if (zone_end_pfn(zone) <= spfn) { 1392837566e7SAlexander Duyck *idx = U64_MAX; 1393837566e7SAlexander Duyck break; 1394837566e7SAlexander Duyck } 1395837566e7SAlexander Duyck 1396837566e7SAlexander Duyck if (out_spfn) 1397837566e7SAlexander Duyck *out_spfn = max(zone->zone_start_pfn, spfn); 1398837566e7SAlexander Duyck if (out_epfn) 1399837566e7SAlexander Duyck *out_epfn = min(zone_end_pfn(zone), epfn); 1400837566e7SAlexander Duyck 1401837566e7SAlexander Duyck return; 1402837566e7SAlexander Duyck } 1403837566e7SAlexander Duyck 1404837566e7SAlexander Duyck __next_mem_range(idx, zone_nid, MEMBLOCK_NONE, 1405837566e7SAlexander Duyck &memblock.memory, &memblock.reserved, 1406f30b002cSMiaohe Lin &spa, &epa, NULL); 1407837566e7SAlexander Duyck } 1408837566e7SAlexander Duyck 1409837566e7SAlexander Duyck /* signal end of iteration */ 1410837566e7SAlexander Duyck if (out_spfn) 1411837566e7SAlexander Duyck *out_spfn = ULONG_MAX; 1412837566e7SAlexander Duyck if (out_epfn) 1413837566e7SAlexander Duyck *out_epfn = 0; 1414837566e7SAlexander Duyck } 1415837566e7SAlexander Duyck 1416837566e7SAlexander Duyck #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 14177c0caeb8STejun Heo 141892d12f95SMike Rapoport /** 141992d12f95SMike Rapoport * memblock_alloc_range_nid - allocate boot memory block 142092d12f95SMike Rapoport * @size: size of memory block to be allocated in bytes 142192d12f95SMike Rapoport * @align: alignment of the region and block's size 142292d12f95SMike Rapoport * @start: the lower bound of the memory region to allocate (phys address) 142392d12f95SMike Rapoport * @end: the upper bound of the memory region to allocate (phys address) 142492d12f95SMike Rapoport * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 14250ac398b1SYunfeng Ye * @exact_nid: control the allocation fall back to other nodes 142692d12f95SMike Rapoport * 142792d12f95SMike Rapoport * The allocation is performed from memory region limited by 142895830666SCao jin * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE. 142992d12f95SMike Rapoport * 14300ac398b1SYunfeng Ye * If the specified node can not hold the requested memory and @exact_nid 14310ac398b1SYunfeng Ye * is false, the allocation falls back to any node in the system. 143292d12f95SMike Rapoport * 143392d12f95SMike Rapoport * For systems with memory mirroring, the allocation is attempted first 143492d12f95SMike Rapoport * from the regions with mirroring enabled and then retried from any 143592d12f95SMike Rapoport * memory region. 143692d12f95SMike Rapoport * 1437c200d900SPatrick Wang * In addition, function using kmemleak_alloc_phys for allocated boot 1438c200d900SPatrick Wang * memory block, it is never reported as leaks. 143992d12f95SMike Rapoport * 144092d12f95SMike Rapoport * Return: 144192d12f95SMike Rapoport * Physical address of allocated memory block on success, %0 on failure. 144292d12f95SMike Rapoport */ 14438676af1fSAslan Bakirov phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, 14442bfc2862SAkinobu Mita phys_addr_t align, phys_addr_t start, 14450ac398b1SYunfeng Ye phys_addr_t end, int nid, 14460ac398b1SYunfeng Ye bool exact_nid) 144795f72d1eSYinghai Lu { 144892d12f95SMike Rapoport enum memblock_flags flags = choose_memblock_flags(); 14496ed311b2SBenjamin Herrenschmidt phys_addr_t found; 145095f72d1eSYinghai Lu 145192d12f95SMike Rapoport if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) 145292d12f95SMike Rapoport nid = NUMA_NO_NODE; 145392d12f95SMike Rapoport 14542f770806SMike Rapoport if (!align) { 14552f770806SMike Rapoport /* Can't use WARNs this early in boot on powerpc */ 14562f770806SMike Rapoport dump_stack(); 14572f770806SMike Rapoport align = SMP_CACHE_BYTES; 14582f770806SMike Rapoport } 14592f770806SMike Rapoport 146092d12f95SMike Rapoport again: 1461fc6daaf9STony Luck found = memblock_find_in_range_node(size, align, start, end, nid, 1462fc6daaf9STony Luck flags); 146392d12f95SMike Rapoport if (found && !memblock_reserve(found, size)) 146492d12f95SMike Rapoport goto done; 146592d12f95SMike Rapoport 14660ac398b1SYunfeng Ye if (nid != NUMA_NO_NODE && !exact_nid) { 146792d12f95SMike Rapoport found = memblock_find_in_range_node(size, align, start, 146892d12f95SMike Rapoport end, NUMA_NO_NODE, 146992d12f95SMike Rapoport flags); 147092d12f95SMike Rapoport if (found && !memblock_reserve(found, size)) 147192d12f95SMike Rapoport goto done; 147292d12f95SMike Rapoport } 147392d12f95SMike Rapoport 147492d12f95SMike Rapoport if (flags & MEMBLOCK_MIRROR) { 147592d12f95SMike Rapoport flags &= ~MEMBLOCK_MIRROR; 147614d9a675SMa Wupeng pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n", 147792d12f95SMike Rapoport &size); 147892d12f95SMike Rapoport goto again; 147992d12f95SMike Rapoport } 148092d12f95SMike Rapoport 148192d12f95SMike Rapoport return 0; 148292d12f95SMike Rapoport 148392d12f95SMike Rapoport done: 1484c6975d7cSQian Cai /* 1485c6975d7cSQian Cai * Skip kmemleak for those places like kasan_init() and 1486c6975d7cSQian Cai * early_pgtable_alloc() due to high volume. 1487c6975d7cSQian Cai */ 1488c6975d7cSQian Cai if (end != MEMBLOCK_ALLOC_NOLEAKTRACE) 1489aedf95eaSCatalin Marinas /* 1490c200d900SPatrick Wang * Memblock allocated blocks are never reported as 1491c200d900SPatrick Wang * leaks. This is because many of these blocks are 1492c200d900SPatrick Wang * only referred via the physical address which is 1493c200d900SPatrick Wang * not looked up by kmemleak. 1494aedf95eaSCatalin Marinas */ 1495c200d900SPatrick Wang kmemleak_alloc_phys(found, size, 0); 149692d12f95SMike Rapoport 1497dcdfdd40SKirill A. Shutemov /* 1498dcdfdd40SKirill A. Shutemov * Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP, 1499dcdfdd40SKirill A. Shutemov * require memory to be accepted before it can be used by the 1500dcdfdd40SKirill A. Shutemov * guest. 1501dcdfdd40SKirill A. Shutemov * 1502dcdfdd40SKirill A. Shutemov * Accept the memory of the allocated buffer. 1503dcdfdd40SKirill A. Shutemov */ 1504dcdfdd40SKirill A. Shutemov accept_memory(found, found + size); 1505dcdfdd40SKirill A. Shutemov 15066ed311b2SBenjamin Herrenschmidt return found; 1507aedf95eaSCatalin Marinas } 150895f72d1eSYinghai Lu 1509a2974133SMike Rapoport /** 1510a2974133SMike Rapoport * memblock_phys_alloc_range - allocate a memory block inside specified range 1511a2974133SMike Rapoport * @size: size of memory block to be allocated in bytes 1512a2974133SMike Rapoport * @align: alignment of the region and block's size 1513a2974133SMike Rapoport * @start: the lower bound of the memory region to allocate (physical address) 1514a2974133SMike Rapoport * @end: the upper bound of the memory region to allocate (physical address) 1515a2974133SMike Rapoport * 1516a2974133SMike Rapoport * Allocate @size bytes in the between @start and @end. 1517a2974133SMike Rapoport * 1518a2974133SMike Rapoport * Return: physical address of the allocated memory block on success, 1519a2974133SMike Rapoport * %0 on failure. 1520a2974133SMike Rapoport */ 15218a770c2aSMike Rapoport phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size, 15228a770c2aSMike Rapoport phys_addr_t align, 15238a770c2aSMike Rapoport phys_addr_t start, 15248a770c2aSMike Rapoport phys_addr_t end) 15252bfc2862SAkinobu Mita { 1526b5cf2d6cSFaiyaz Mohammed memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n", 1527b5cf2d6cSFaiyaz Mohammed __func__, (u64)size, (u64)align, &start, &end, 1528b5cf2d6cSFaiyaz Mohammed (void *)_RET_IP_); 15290ac398b1SYunfeng Ye return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE, 15300ac398b1SYunfeng Ye false); 15317bd0b0f0STejun Heo } 15327bd0b0f0STejun Heo 1533a2974133SMike Rapoport /** 153417cbe038SLevi Yun * memblock_phys_alloc_try_nid - allocate a memory block from specified NUMA node 1535a2974133SMike Rapoport * @size: size of memory block to be allocated in bytes 1536a2974133SMike Rapoport * @align: alignment of the region and block's size 1537a2974133SMike Rapoport * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1538a2974133SMike Rapoport * 1539a2974133SMike Rapoport * Allocates memory block from the specified NUMA node. If the node 1540a2974133SMike Rapoport * has no available memory, attempts to allocated from any node in the 1541a2974133SMike Rapoport * system. 1542a2974133SMike Rapoport * 1543a2974133SMike Rapoport * Return: physical address of the allocated memory block on success, 1544a2974133SMike Rapoport * %0 on failure. 1545a2974133SMike Rapoport */ 15469a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) 15479d1e2492SBenjamin Herrenschmidt { 154833755574SMike Rapoport return memblock_alloc_range_nid(size, align, 0, 15490ac398b1SYunfeng Ye MEMBLOCK_ALLOC_ACCESSIBLE, nid, false); 155095f72d1eSYinghai Lu } 155195f72d1eSYinghai Lu 155226f09e9bSSantosh Shilimkar /** 1553eb31d559SMike Rapoport * memblock_alloc_internal - allocate boot memory block 155426f09e9bSSantosh Shilimkar * @size: size of memory block to be allocated in bytes 155526f09e9bSSantosh Shilimkar * @align: alignment of the region and block's size 155626f09e9bSSantosh Shilimkar * @min_addr: the lower bound of the memory region to allocate (phys address) 155726f09e9bSSantosh Shilimkar * @max_addr: the upper bound of the memory region to allocate (phys address) 155826f09e9bSSantosh Shilimkar * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 15590ac398b1SYunfeng Ye * @exact_nid: control the allocation fall back to other nodes 156026f09e9bSSantosh Shilimkar * 156192d12f95SMike Rapoport * Allocates memory block using memblock_alloc_range_nid() and 156292d12f95SMike Rapoport * converts the returned physical address to virtual. 156392d12f95SMike Rapoport * 156426f09e9bSSantosh Shilimkar * The @min_addr limit is dropped if it can not be satisfied and the allocation 156592d12f95SMike Rapoport * will fall back to memory below @min_addr. Other constraints, such 156692d12f95SMike Rapoport * as node and mirrored memory will be handled again in 156792d12f95SMike Rapoport * memblock_alloc_range_nid(). 156826f09e9bSSantosh Shilimkar * 156947cec443SMike Rapoport * Return: 157026f09e9bSSantosh Shilimkar * Virtual address of allocated memory block on success, NULL on failure. 157126f09e9bSSantosh Shilimkar */ 1572eb31d559SMike Rapoport static void * __init memblock_alloc_internal( 157326f09e9bSSantosh Shilimkar phys_addr_t size, phys_addr_t align, 157426f09e9bSSantosh Shilimkar phys_addr_t min_addr, phys_addr_t max_addr, 15750ac398b1SYunfeng Ye int nid, bool exact_nid) 157626f09e9bSSantosh Shilimkar { 157726f09e9bSSantosh Shilimkar phys_addr_t alloc; 157826f09e9bSSantosh Shilimkar 157926f09e9bSSantosh Shilimkar /* 158026f09e9bSSantosh Shilimkar * Detect any accidental use of these APIs after slab is ready, as at 158126f09e9bSSantosh Shilimkar * this moment memblock may be deinitialized already and its 1582c6ffc5caSMike Rapoport * internal data may be destroyed (after execution of memblock_free_all) 158326f09e9bSSantosh Shilimkar */ 158426f09e9bSSantosh Shilimkar if (WARN_ON_ONCE(slab_is_available())) 158526f09e9bSSantosh Shilimkar return kzalloc_node(size, GFP_NOWAIT, nid); 158626f09e9bSSantosh Shilimkar 1587f3057ad7SMike Rapoport if (max_addr > memblock.current_limit) 1588f3057ad7SMike Rapoport max_addr = memblock.current_limit; 1589f3057ad7SMike Rapoport 15900ac398b1SYunfeng Ye alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid, 15910ac398b1SYunfeng Ye exact_nid); 15922f770806SMike Rapoport 159392d12f95SMike Rapoport /* retry allocation without lower limit */ 159492d12f95SMike Rapoport if (!alloc && min_addr) 15950ac398b1SYunfeng Ye alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid, 15960ac398b1SYunfeng Ye exact_nid); 159726f09e9bSSantosh Shilimkar 159892d12f95SMike Rapoport if (!alloc) 1599a3f5bafcSTony Luck return NULL; 160026f09e9bSSantosh Shilimkar 160192d12f95SMike Rapoport return phys_to_virt(alloc); 160226f09e9bSSantosh Shilimkar } 160326f09e9bSSantosh Shilimkar 160426f09e9bSSantosh Shilimkar /** 16050ac398b1SYunfeng Ye * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node 16060ac398b1SYunfeng Ye * without zeroing memory 16070ac398b1SYunfeng Ye * @size: size of memory block to be allocated in bytes 16080ac398b1SYunfeng Ye * @align: alignment of the region and block's size 16090ac398b1SYunfeng Ye * @min_addr: the lower bound of the memory region from where the allocation 16100ac398b1SYunfeng Ye * is preferred (phys address) 16110ac398b1SYunfeng Ye * @max_addr: the upper bound of the memory region from where the allocation 16120ac398b1SYunfeng Ye * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 16130ac398b1SYunfeng Ye * allocate only from memory limited by memblock.current_limit value 16140ac398b1SYunfeng Ye * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 16150ac398b1SYunfeng Ye * 16160ac398b1SYunfeng Ye * Public function, provides additional debug information (including caller 16170ac398b1SYunfeng Ye * info), if enabled. Does not zero allocated memory. 16180ac398b1SYunfeng Ye * 16190ac398b1SYunfeng Ye * Return: 16200ac398b1SYunfeng Ye * Virtual address of allocated memory block on success, NULL on failure. 16210ac398b1SYunfeng Ye */ 16220ac398b1SYunfeng Ye void * __init memblock_alloc_exact_nid_raw( 16230ac398b1SYunfeng Ye phys_addr_t size, phys_addr_t align, 16240ac398b1SYunfeng Ye phys_addr_t min_addr, phys_addr_t max_addr, 16250ac398b1SYunfeng Ye int nid) 16260ac398b1SYunfeng Ye { 16270ac398b1SYunfeng Ye memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 16280ac398b1SYunfeng Ye __func__, (u64)size, (u64)align, nid, &min_addr, 16290ac398b1SYunfeng Ye &max_addr, (void *)_RET_IP_); 16300ac398b1SYunfeng Ye 163108678804SMike Rapoport return memblock_alloc_internal(size, align, min_addr, max_addr, nid, 163208678804SMike Rapoport true); 16330ac398b1SYunfeng Ye } 16340ac398b1SYunfeng Ye 16350ac398b1SYunfeng Ye /** 1636eb31d559SMike Rapoport * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing 1637ea1f5f37SPavel Tatashin * memory and without panicking 1638ea1f5f37SPavel Tatashin * @size: size of memory block to be allocated in bytes 1639ea1f5f37SPavel Tatashin * @align: alignment of the region and block's size 1640ea1f5f37SPavel Tatashin * @min_addr: the lower bound of the memory region from where the allocation 1641ea1f5f37SPavel Tatashin * is preferred (phys address) 1642ea1f5f37SPavel Tatashin * @max_addr: the upper bound of the memory region from where the allocation 164397ad1087SMike Rapoport * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 1644ea1f5f37SPavel Tatashin * allocate only from memory limited by memblock.current_limit value 1645ea1f5f37SPavel Tatashin * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1646ea1f5f37SPavel Tatashin * 1647ea1f5f37SPavel Tatashin * Public function, provides additional debug information (including caller 1648ea1f5f37SPavel Tatashin * info), if enabled. Does not zero allocated memory, does not panic if request 1649ea1f5f37SPavel Tatashin * cannot be satisfied. 1650ea1f5f37SPavel Tatashin * 165147cec443SMike Rapoport * Return: 1652ea1f5f37SPavel Tatashin * Virtual address of allocated memory block on success, NULL on failure. 1653ea1f5f37SPavel Tatashin */ 1654eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw( 1655ea1f5f37SPavel Tatashin phys_addr_t size, phys_addr_t align, 1656ea1f5f37SPavel Tatashin phys_addr_t min_addr, phys_addr_t max_addr, 1657ea1f5f37SPavel Tatashin int nid) 1658ea1f5f37SPavel Tatashin { 1659d75f773cSSakari Ailus memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1660a36aab89SMike Rapoport __func__, (u64)size, (u64)align, nid, &min_addr, 1661a36aab89SMike Rapoport &max_addr, (void *)_RET_IP_); 1662ea1f5f37SPavel Tatashin 166308678804SMike Rapoport return memblock_alloc_internal(size, align, min_addr, max_addr, nid, 166408678804SMike Rapoport false); 1665ea1f5f37SPavel Tatashin } 1666ea1f5f37SPavel Tatashin 1667ea1f5f37SPavel Tatashin /** 1668c0dbe825SMike Rapoport * memblock_alloc_try_nid - allocate boot memory block 166926f09e9bSSantosh Shilimkar * @size: size of memory block to be allocated in bytes 167026f09e9bSSantosh Shilimkar * @align: alignment of the region and block's size 167126f09e9bSSantosh Shilimkar * @min_addr: the lower bound of the memory region from where the allocation 167226f09e9bSSantosh Shilimkar * is preferred (phys address) 167326f09e9bSSantosh Shilimkar * @max_addr: the upper bound of the memory region from where the allocation 167497ad1087SMike Rapoport * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 167526f09e9bSSantosh Shilimkar * allocate only from memory limited by memblock.current_limit value 167626f09e9bSSantosh Shilimkar * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 167726f09e9bSSantosh Shilimkar * 1678c0dbe825SMike Rapoport * Public function, provides additional debug information (including caller 1679c0dbe825SMike Rapoport * info), if enabled. This function zeroes the allocated memory. 168026f09e9bSSantosh Shilimkar * 168147cec443SMike Rapoport * Return: 168226f09e9bSSantosh Shilimkar * Virtual address of allocated memory block on success, NULL on failure. 168326f09e9bSSantosh Shilimkar */ 1684eb31d559SMike Rapoport void * __init memblock_alloc_try_nid( 168526f09e9bSSantosh Shilimkar phys_addr_t size, phys_addr_t align, 168626f09e9bSSantosh Shilimkar phys_addr_t min_addr, phys_addr_t max_addr, 168726f09e9bSSantosh Shilimkar int nid) 168826f09e9bSSantosh Shilimkar { 168926f09e9bSSantosh Shilimkar void *ptr; 169026f09e9bSSantosh Shilimkar 1691d75f773cSSakari Ailus memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1692a36aab89SMike Rapoport __func__, (u64)size, (u64)align, nid, &min_addr, 1693a36aab89SMike Rapoport &max_addr, (void *)_RET_IP_); 1694eb31d559SMike Rapoport ptr = memblock_alloc_internal(size, align, 16950ac398b1SYunfeng Ye min_addr, max_addr, nid, false); 1696c0dbe825SMike Rapoport if (ptr) 1697ea1f5f37SPavel Tatashin memset(ptr, 0, size); 169826f09e9bSSantosh Shilimkar 1699c0dbe825SMike Rapoport return ptr; 170026f09e9bSSantosh Shilimkar } 170126f09e9bSSantosh Shilimkar 170226f09e9bSSantosh Shilimkar /** 1703621d9739SMike Rapoport * memblock_free_late - free pages directly to buddy allocator 170448a833ccSMike Rapoport * @base: phys starting address of the boot memory block 170526f09e9bSSantosh Shilimkar * @size: size of the boot memory block in bytes 170626f09e9bSSantosh Shilimkar * 1707a2974133SMike Rapoport * This is only useful when the memblock allocator has already been torn 170826f09e9bSSantosh Shilimkar * down, but we are still initializing the system. Pages are released directly 1709a2974133SMike Rapoport * to the buddy allocator. 171026f09e9bSSantosh Shilimkar */ 1711621d9739SMike Rapoport void __init memblock_free_late(phys_addr_t base, phys_addr_t size) 171226f09e9bSSantosh Shilimkar { 1713a36aab89SMike Rapoport phys_addr_t cursor, end; 171426f09e9bSSantosh Shilimkar 1715a36aab89SMike Rapoport end = base + size - 1; 1716d75f773cSSakari Ailus memblock_dbg("%s: [%pa-%pa] %pS\n", 1717a36aab89SMike Rapoport __func__, &base, &end, (void *)_RET_IP_); 17189099daedSCatalin Marinas kmemleak_free_part_phys(base, size); 171926f09e9bSSantosh Shilimkar cursor = PFN_UP(base); 172026f09e9bSSantosh Shilimkar end = PFN_DOWN(base + size); 172126f09e9bSSantosh Shilimkar 172226f09e9bSSantosh Shilimkar for (; cursor < end; cursor++) { 1723647037adSAaron Thompson memblock_free_pages(pfn_to_page(cursor), cursor, 0); 1724ca79b0c2SArun KS totalram_pages_inc(); 172526f09e9bSSantosh Shilimkar } 172626f09e9bSSantosh Shilimkar } 17279d1e2492SBenjamin Herrenschmidt 17289d1e2492SBenjamin Herrenschmidt /* 17299d1e2492SBenjamin Herrenschmidt * Remaining API functions 17309d1e2492SBenjamin Herrenschmidt */ 17319d1e2492SBenjamin Herrenschmidt 17321f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void) 173395f72d1eSYinghai Lu { 17341440c4e2STejun Heo return memblock.memory.total_size; 173595f72d1eSYinghai Lu } 173695f72d1eSYinghai Lu 17378907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void) 17388907de5dSSrikar Dronamraju { 17398907de5dSSrikar Dronamraju return memblock.reserved.total_size; 17408907de5dSSrikar Dronamraju } 17418907de5dSSrikar Dronamraju 17420a93ebefSSam Ravnborg /* lowest address */ 17430a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void) 17440a93ebefSSam Ravnborg { 17450a93ebefSSam Ravnborg return memblock.memory.regions[0].base; 17460a93ebefSSam Ravnborg } 17470a93ebefSSam Ravnborg 174810d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void) 174995f72d1eSYinghai Lu { 175095f72d1eSYinghai Lu int idx = memblock.memory.cnt - 1; 175195f72d1eSYinghai Lu 1752e3239ff9SBenjamin Herrenschmidt return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); 175395f72d1eSYinghai Lu } 175495f72d1eSYinghai Lu 1755a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit) 175695f72d1eSYinghai Lu { 17571c4bc43dSStefan Agner phys_addr_t max_addr = PHYS_ADDR_MAX; 1758136199f0SEmil Medve struct memblock_region *r; 175995f72d1eSYinghai Lu 1760a571d4ebSDennis Chen /* 1761a571d4ebSDennis Chen * translate the memory @limit size into the max address within one of 1762a571d4ebSDennis Chen * the memory memblock regions, if the @limit exceeds the total size 17631c4bc43dSStefan Agner * of those regions, max_addr will keep original value PHYS_ADDR_MAX 1764a571d4ebSDennis Chen */ 1765cc6de168SMike Rapoport for_each_mem_region(r) { 1766c0ce8fefSTejun Heo if (limit <= r->size) { 1767c0ce8fefSTejun Heo max_addr = r->base + limit; 176895f72d1eSYinghai Lu break; 176995f72d1eSYinghai Lu } 1770c0ce8fefSTejun Heo limit -= r->size; 177195f72d1eSYinghai Lu } 1772c0ce8fefSTejun Heo 1773a571d4ebSDennis Chen return max_addr; 1774a571d4ebSDennis Chen } 1775a571d4ebSDennis Chen 1776a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit) 1777a571d4ebSDennis Chen { 177849aef717SColin Ian King phys_addr_t max_addr; 1779a571d4ebSDennis Chen 1780a571d4ebSDennis Chen if (!limit) 1781a571d4ebSDennis Chen return; 1782a571d4ebSDennis Chen 1783a571d4ebSDennis Chen max_addr = __find_max_addr(limit); 1784a571d4ebSDennis Chen 1785a571d4ebSDennis Chen /* @limit exceeds the total size of the memory, do nothing */ 17861c4bc43dSStefan Agner if (max_addr == PHYS_ADDR_MAX) 1787a571d4ebSDennis Chen return; 1788a571d4ebSDennis Chen 1789c0ce8fefSTejun Heo /* truncate both memory and reserved regions */ 1790f1af9d3aSPhilipp Hachtmann memblock_remove_range(&memblock.memory, max_addr, 17911c4bc43dSStefan Agner PHYS_ADDR_MAX); 1792f1af9d3aSPhilipp Hachtmann memblock_remove_range(&memblock.reserved, max_addr, 17931c4bc43dSStefan Agner PHYS_ADDR_MAX); 179495f72d1eSYinghai Lu } 179595f72d1eSYinghai Lu 1796c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size) 1797c9ca9b4eSAKASHI Takahiro { 1798c9ca9b4eSAKASHI Takahiro int start_rgn, end_rgn; 1799c9ca9b4eSAKASHI Takahiro int i, ret; 1800c9ca9b4eSAKASHI Takahiro 1801c9ca9b4eSAKASHI Takahiro if (!size) 1802c9ca9b4eSAKASHI Takahiro return; 1803c9ca9b4eSAKASHI Takahiro 18045173ed72SPeng Fan if (!memblock_memory->total_size) { 1805e888fa7bSGeert Uytterhoeven pr_warn("%s: No memory registered yet\n", __func__); 1806e888fa7bSGeert Uytterhoeven return; 1807e888fa7bSGeert Uytterhoeven } 1808e888fa7bSGeert Uytterhoeven 1809c9ca9b4eSAKASHI Takahiro ret = memblock_isolate_range(&memblock.memory, base, size, 1810c9ca9b4eSAKASHI Takahiro &start_rgn, &end_rgn); 1811c9ca9b4eSAKASHI Takahiro if (ret) 1812c9ca9b4eSAKASHI Takahiro return; 1813c9ca9b4eSAKASHI Takahiro 1814c9ca9b4eSAKASHI Takahiro /* remove all the MAP regions */ 1815c9ca9b4eSAKASHI Takahiro for (i = memblock.memory.cnt - 1; i >= end_rgn; i--) 1816c9ca9b4eSAKASHI Takahiro if (!memblock_is_nomap(&memblock.memory.regions[i])) 1817c9ca9b4eSAKASHI Takahiro memblock_remove_region(&memblock.memory, i); 1818c9ca9b4eSAKASHI Takahiro 1819c9ca9b4eSAKASHI Takahiro for (i = start_rgn - 1; i >= 0; i--) 1820c9ca9b4eSAKASHI Takahiro if (!memblock_is_nomap(&memblock.memory.regions[i])) 1821c9ca9b4eSAKASHI Takahiro memblock_remove_region(&memblock.memory, i); 1822c9ca9b4eSAKASHI Takahiro 1823c9ca9b4eSAKASHI Takahiro /* truncate the reserved regions */ 1824c9ca9b4eSAKASHI Takahiro memblock_remove_range(&memblock.reserved, 0, base); 1825c9ca9b4eSAKASHI Takahiro memblock_remove_range(&memblock.reserved, 18261c4bc43dSStefan Agner base + size, PHYS_ADDR_MAX); 1827c9ca9b4eSAKASHI Takahiro } 1828c9ca9b4eSAKASHI Takahiro 1829a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit) 1830a571d4ebSDennis Chen { 1831a571d4ebSDennis Chen phys_addr_t max_addr; 1832a571d4ebSDennis Chen 1833a571d4ebSDennis Chen if (!limit) 1834a571d4ebSDennis Chen return; 1835a571d4ebSDennis Chen 1836a571d4ebSDennis Chen max_addr = __find_max_addr(limit); 1837a571d4ebSDennis Chen 1838a571d4ebSDennis Chen /* @limit exceeds the total size of the memory, do nothing */ 18391c4bc43dSStefan Agner if (max_addr == PHYS_ADDR_MAX) 1840a571d4ebSDennis Chen return; 1841a571d4ebSDennis Chen 1842c9ca9b4eSAKASHI Takahiro memblock_cap_memory_range(0, max_addr); 1843a571d4ebSDennis Chen } 1844a571d4ebSDennis Chen 1845cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr) 184672d4b0b4SBenjamin Herrenschmidt { 184772d4b0b4SBenjamin Herrenschmidt unsigned int left = 0, right = type->cnt; 184872d4b0b4SBenjamin Herrenschmidt 184972d4b0b4SBenjamin Herrenschmidt do { 185072d4b0b4SBenjamin Herrenschmidt unsigned int mid = (right + left) / 2; 185172d4b0b4SBenjamin Herrenschmidt 185272d4b0b4SBenjamin Herrenschmidt if (addr < type->regions[mid].base) 185372d4b0b4SBenjamin Herrenschmidt right = mid; 185472d4b0b4SBenjamin Herrenschmidt else if (addr >= (type->regions[mid].base + 185572d4b0b4SBenjamin Herrenschmidt type->regions[mid].size)) 185672d4b0b4SBenjamin Herrenschmidt left = mid + 1; 185772d4b0b4SBenjamin Herrenschmidt else 185872d4b0b4SBenjamin Herrenschmidt return mid; 185972d4b0b4SBenjamin Herrenschmidt } while (left < right); 186072d4b0b4SBenjamin Herrenschmidt return -1; 186172d4b0b4SBenjamin Herrenschmidt } 186272d4b0b4SBenjamin Herrenschmidt 1863f5a222dcSYueyi Li bool __init_memblock memblock_is_reserved(phys_addr_t addr) 186495f72d1eSYinghai Lu { 186572d4b0b4SBenjamin Herrenschmidt return memblock_search(&memblock.reserved, addr) != -1; 186695f72d1eSYinghai Lu } 186772d4b0b4SBenjamin Herrenschmidt 1868b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr) 186972d4b0b4SBenjamin Herrenschmidt { 187072d4b0b4SBenjamin Herrenschmidt return memblock_search(&memblock.memory, addr) != -1; 187172d4b0b4SBenjamin Herrenschmidt } 187272d4b0b4SBenjamin Herrenschmidt 1873937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr) 1874bf3d3cc5SArd Biesheuvel { 1875bf3d3cc5SArd Biesheuvel int i = memblock_search(&memblock.memory, addr); 1876bf3d3cc5SArd Biesheuvel 1877bf3d3cc5SArd Biesheuvel if (i == -1) 1878bf3d3cc5SArd Biesheuvel return false; 1879bf3d3cc5SArd Biesheuvel return !memblock_is_nomap(&memblock.memory.regions[i]); 1880bf3d3cc5SArd Biesheuvel } 1881bf3d3cc5SArd Biesheuvel 1882e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn, 1883e76b63f8SYinghai Lu unsigned long *start_pfn, unsigned long *end_pfn) 1884e76b63f8SYinghai Lu { 1885e76b63f8SYinghai Lu struct memblock_type *type = &memblock.memory; 188616763230SFabian Frederick int mid = memblock_search(type, PFN_PHYS(pfn)); 1887e76b63f8SYinghai Lu 1888e76b63f8SYinghai Lu if (mid == -1) 18892159bd4eSYuntao Wang return NUMA_NO_NODE; 1890e76b63f8SYinghai Lu 1891f7e2f7e8SFabian Frederick *start_pfn = PFN_DOWN(type->regions[mid].base); 1892f7e2f7e8SFabian Frederick *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size); 1893e76b63f8SYinghai Lu 1894d622abf7SMike Rapoport return memblock_get_region_node(&type->regions[mid]); 1895e76b63f8SYinghai Lu } 1896e76b63f8SYinghai Lu 1897eab30949SStephen Boyd /** 1898eab30949SStephen Boyd * memblock_is_region_memory - check if a region is a subset of memory 1899eab30949SStephen Boyd * @base: base of region to check 1900eab30949SStephen Boyd * @size: size of region to check 1901eab30949SStephen Boyd * 1902eab30949SStephen Boyd * Check if the region [@base, @base + @size) is a subset of a memory block. 1903eab30949SStephen Boyd * 190447cec443SMike Rapoport * Return: 1905eab30949SStephen Boyd * 0 if false, non-zero if true 1906eab30949SStephen Boyd */ 1907937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size) 190872d4b0b4SBenjamin Herrenschmidt { 1909abb65272STomi Valkeinen int idx = memblock_search(&memblock.memory, base); 1910eb18f1b5STejun Heo phys_addr_t end = base + memblock_cap_size(base, &size); 191172d4b0b4SBenjamin Herrenschmidt 191272d4b0b4SBenjamin Herrenschmidt if (idx == -1) 1913937f0c26SYaowei Bai return false; 1914ef415ef4SWei Yang return (memblock.memory.regions[idx].base + 1915eb18f1b5STejun Heo memblock.memory.regions[idx].size) >= end; 191695f72d1eSYinghai Lu } 191795f72d1eSYinghai Lu 1918eab30949SStephen Boyd /** 1919eab30949SStephen Boyd * memblock_is_region_reserved - check if a region intersects reserved memory 1920eab30949SStephen Boyd * @base: base of region to check 1921eab30949SStephen Boyd * @size: size of region to check 1922eab30949SStephen Boyd * 192347cec443SMike Rapoport * Check if the region [@base, @base + @size) intersects a reserved 192447cec443SMike Rapoport * memory block. 1925eab30949SStephen Boyd * 192647cec443SMike Rapoport * Return: 1927c5c5c9d1STang Chen * True if they intersect, false if not. 1928eab30949SStephen Boyd */ 1929c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) 193095f72d1eSYinghai Lu { 1931c5c5c9d1STang Chen return memblock_overlaps_region(&memblock.reserved, base, size); 193295f72d1eSYinghai Lu } 193395f72d1eSYinghai Lu 19346ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align) 19356ede1fd3SYinghai Lu { 19366ede1fd3SYinghai Lu phys_addr_t start, end, orig_start, orig_end; 1937136199f0SEmil Medve struct memblock_region *r; 19386ede1fd3SYinghai Lu 1939cc6de168SMike Rapoport for_each_mem_region(r) { 1940136199f0SEmil Medve orig_start = r->base; 1941136199f0SEmil Medve orig_end = r->base + r->size; 19426ede1fd3SYinghai Lu start = round_up(orig_start, align); 19436ede1fd3SYinghai Lu end = round_down(orig_end, align); 19446ede1fd3SYinghai Lu 19456ede1fd3SYinghai Lu if (start == orig_start && end == orig_end) 19466ede1fd3SYinghai Lu continue; 19476ede1fd3SYinghai Lu 19486ede1fd3SYinghai Lu if (start < end) { 1949136199f0SEmil Medve r->base = start; 1950136199f0SEmil Medve r->size = end - start; 19516ede1fd3SYinghai Lu } else { 1952136199f0SEmil Medve memblock_remove_region(&memblock.memory, 1953136199f0SEmil Medve r - memblock.memory.regions); 1954136199f0SEmil Medve r--; 19556ede1fd3SYinghai Lu } 19566ede1fd3SYinghai Lu } 19576ede1fd3SYinghai Lu } 1958e63075a3SBenjamin Herrenschmidt 19593661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit) 1960e63075a3SBenjamin Herrenschmidt { 1961e63075a3SBenjamin Herrenschmidt memblock.current_limit = limit; 1962e63075a3SBenjamin Herrenschmidt } 1963e63075a3SBenjamin Herrenschmidt 1964fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void) 1965fec51014SLaura Abbott { 1966fec51014SLaura Abbott return memblock.current_limit; 1967fec51014SLaura Abbott } 1968fec51014SLaura Abbott 19690262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type) 19706ed311b2SBenjamin Herrenschmidt { 19715d63f81cSMiles Chen phys_addr_t base, end, size; 1972e1720feeSMike Rapoport enum memblock_flags flags; 19738c9c1701SAlexander Kuleshov int idx; 19748c9c1701SAlexander Kuleshov struct memblock_region *rgn; 19756ed311b2SBenjamin Herrenschmidt 19760262d9c8SHeiko Carstens pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt); 19776ed311b2SBenjamin Herrenschmidt 197866e8b438SGioh Kim for_each_memblock_type(idx, type, rgn) { 19797c0caeb8STejun Heo char nid_buf[32] = ""; 19806ed311b2SBenjamin Herrenschmidt 19817c0caeb8STejun Heo base = rgn->base; 19827c0caeb8STejun Heo size = rgn->size; 19835d63f81cSMiles Chen end = base + size - 1; 198466a20757STang Chen flags = rgn->flags; 1985a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA 19867c0caeb8STejun Heo if (memblock_get_region_node(rgn) != MAX_NUMNODES) 19877c0caeb8STejun Heo snprintf(nid_buf, sizeof(nid_buf), " on node %d", 19887c0caeb8STejun Heo memblock_get_region_node(rgn)); 19897c0caeb8STejun Heo #endif 1990e1720feeSMike Rapoport pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n", 19910262d9c8SHeiko Carstens type->name, idx, &base, &end, &size, nid_buf, flags); 19926ed311b2SBenjamin Herrenschmidt } 19936ed311b2SBenjamin Herrenschmidt } 19946ed311b2SBenjamin Herrenschmidt 199587c55870SMike Rapoport static void __init_memblock __memblock_dump_all(void) 19966ed311b2SBenjamin Herrenschmidt { 19976ed311b2SBenjamin Herrenschmidt pr_info("MEMBLOCK configuration:\n"); 19985d63f81cSMiles Chen pr_info(" memory size = %pa reserved size = %pa\n", 19995d63f81cSMiles Chen &memblock.memory.total_size, 20005d63f81cSMiles Chen &memblock.reserved.total_size); 20016ed311b2SBenjamin Herrenschmidt 20020262d9c8SHeiko Carstens memblock_dump(&memblock.memory); 20030262d9c8SHeiko Carstens memblock_dump(&memblock.reserved); 2004409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 200577649905SDavid Hildenbrand memblock_dump(&physmem); 2006409efd4cSHeiko Carstens #endif 20076ed311b2SBenjamin Herrenschmidt } 20086ed311b2SBenjamin Herrenschmidt 200987c55870SMike Rapoport void __init_memblock memblock_dump_all(void) 201087c55870SMike Rapoport { 201187c55870SMike Rapoport if (memblock_debug) 201287c55870SMike Rapoport __memblock_dump_all(); 201387c55870SMike Rapoport } 201487c55870SMike Rapoport 20151aadc056STejun Heo void __init memblock_allow_resize(void) 20166ed311b2SBenjamin Herrenschmidt { 2017142b45a7SBenjamin Herrenschmidt memblock_can_resize = 1; 20186ed311b2SBenjamin Herrenschmidt } 20196ed311b2SBenjamin Herrenschmidt 20206ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p) 20216ed311b2SBenjamin Herrenschmidt { 20226ed311b2SBenjamin Herrenschmidt if (p && strstr(p, "debug")) 20236ed311b2SBenjamin Herrenschmidt memblock_debug = 1; 20246ed311b2SBenjamin Herrenschmidt return 0; 20256ed311b2SBenjamin Herrenschmidt } 20266ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock); 20276ed311b2SBenjamin Herrenschmidt 20284f5b0c17SMike Rapoport static void __init free_memmap(unsigned long start_pfn, unsigned long end_pfn) 20294f5b0c17SMike Rapoport { 20304f5b0c17SMike Rapoport struct page *start_pg, *end_pg; 20314f5b0c17SMike Rapoport phys_addr_t pg, pgend; 20324f5b0c17SMike Rapoport 20334f5b0c17SMike Rapoport /* 20344f5b0c17SMike Rapoport * Convert start_pfn/end_pfn to a struct page pointer. 20354f5b0c17SMike Rapoport */ 20364f5b0c17SMike Rapoport start_pg = pfn_to_page(start_pfn - 1) + 1; 20374f5b0c17SMike Rapoport end_pg = pfn_to_page(end_pfn - 1) + 1; 20384f5b0c17SMike Rapoport 20394f5b0c17SMike Rapoport /* 20404f5b0c17SMike Rapoport * Convert to physical addresses, and round start upwards and end 20414f5b0c17SMike Rapoport * downwards. 20424f5b0c17SMike Rapoport */ 20434f5b0c17SMike Rapoport pg = PAGE_ALIGN(__pa(start_pg)); 20444f5b0c17SMike Rapoport pgend = __pa(end_pg) & PAGE_MASK; 20454f5b0c17SMike Rapoport 20464f5b0c17SMike Rapoport /* 20474f5b0c17SMike Rapoport * If there are free pages between these, free the section of the 20484f5b0c17SMike Rapoport * memmap array. 20494f5b0c17SMike Rapoport */ 20504f5b0c17SMike Rapoport if (pg < pgend) 20513ecc6834SMike Rapoport memblock_phys_free(pg, pgend - pg); 20524f5b0c17SMike Rapoport } 20534f5b0c17SMike Rapoport 20544f5b0c17SMike Rapoport /* 20554f5b0c17SMike Rapoport * The mem_map array can get very big. Free the unused area of the memory map. 20564f5b0c17SMike Rapoport */ 20574f5b0c17SMike Rapoport static void __init free_unused_memmap(void) 20584f5b0c17SMike Rapoport { 20594f5b0c17SMike Rapoport unsigned long start, end, prev_end = 0; 20604f5b0c17SMike Rapoport int i; 20614f5b0c17SMike Rapoport 20624f5b0c17SMike Rapoport if (!IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) || 20634f5b0c17SMike Rapoport IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) 20644f5b0c17SMike Rapoport return; 20654f5b0c17SMike Rapoport 20664f5b0c17SMike Rapoport /* 20674f5b0c17SMike Rapoport * This relies on each bank being in address order. 20684f5b0c17SMike Rapoport * The banks are sorted previously in bootmem_init(). 20694f5b0c17SMike Rapoport */ 20704f5b0c17SMike Rapoport for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) { 20714f5b0c17SMike Rapoport #ifdef CONFIG_SPARSEMEM 20724f5b0c17SMike Rapoport /* 20734f5b0c17SMike Rapoport * Take care not to free memmap entries that don't exist 20744f5b0c17SMike Rapoport * due to SPARSEMEM sections which aren't present. 20754f5b0c17SMike Rapoport */ 20764f5b0c17SMike Rapoport start = min(start, ALIGN(prev_end, PAGES_PER_SECTION)); 20774f5b0c17SMike Rapoport #endif 20784f5b0c17SMike Rapoport /* 2079e2a86800SMike Rapoport * Align down here since many operations in VM subsystem 2080e2a86800SMike Rapoport * presume that there are no holes in the memory map inside 2081e2a86800SMike Rapoport * a pageblock 20824f5b0c17SMike Rapoport */ 20834f9bc69aSKefeng Wang start = pageblock_start_pfn(start); 20844f5b0c17SMike Rapoport 20854f5b0c17SMike Rapoport /* 20864f5b0c17SMike Rapoport * If we had a previous bank, and there is a space 20874f5b0c17SMike Rapoport * between the current bank and the previous, free it. 20884f5b0c17SMike Rapoport */ 20894f5b0c17SMike Rapoport if (prev_end && prev_end < start) 20904f5b0c17SMike Rapoport free_memmap(prev_end, start); 20914f5b0c17SMike Rapoport 20924f5b0c17SMike Rapoport /* 2093e2a86800SMike Rapoport * Align up here since many operations in VM subsystem 2094e2a86800SMike Rapoport * presume that there are no holes in the memory map inside 2095e2a86800SMike Rapoport * a pageblock 20964f5b0c17SMike Rapoport */ 20975f7fa13fSKefeng Wang prev_end = pageblock_align(end); 20984f5b0c17SMike Rapoport } 20994f5b0c17SMike Rapoport 21004f5b0c17SMike Rapoport #ifdef CONFIG_SPARSEMEM 2101f921f53eSMike Rapoport if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) { 21025f7fa13fSKefeng Wang prev_end = pageblock_align(end); 21034f5b0c17SMike Rapoport free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION)); 2104f921f53eSMike Rapoport } 21054f5b0c17SMike Rapoport #endif 21064f5b0c17SMike Rapoport } 21074f5b0c17SMike Rapoport 2108bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end) 2109bda49a81SMike Rapoport { 2110bda49a81SMike Rapoport int order; 2111bda49a81SMike Rapoport 2112bda49a81SMike Rapoport while (start < end) { 211359f876fbSKirill A. Shutemov /* 211459f876fbSKirill A. Shutemov * Free the pages in the largest chunks alignment allows. 211559f876fbSKirill A. Shutemov * 211659f876fbSKirill A. Shutemov * __ffs() behaviour is undefined for 0. start == 0 is 21175e0a760bSKirill A. Shutemov * MAX_PAGE_ORDER-aligned, set order to MAX_PAGE_ORDER for 21185e0a760bSKirill A. Shutemov * the case. 211959f876fbSKirill A. Shutemov */ 212059f876fbSKirill A. Shutemov if (start) 21215e0a760bSKirill A. Shutemov order = min_t(int, MAX_PAGE_ORDER, __ffs(start)); 212259f876fbSKirill A. Shutemov else 21235e0a760bSKirill A. Shutemov order = MAX_PAGE_ORDER; 2124bda49a81SMike Rapoport 2125bda49a81SMike Rapoport while (start + (1UL << order) > end) 2126bda49a81SMike Rapoport order--; 2127bda49a81SMike Rapoport 2128bda49a81SMike Rapoport memblock_free_pages(pfn_to_page(start), start, order); 2129bda49a81SMike Rapoport 2130bda49a81SMike Rapoport start += (1UL << order); 2131bda49a81SMike Rapoport } 2132bda49a81SMike Rapoport } 2133bda49a81SMike Rapoport 2134bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start, 2135bda49a81SMike Rapoport phys_addr_t end) 2136bda49a81SMike Rapoport { 2137bda49a81SMike Rapoport unsigned long start_pfn = PFN_UP(start); 2138bda49a81SMike Rapoport unsigned long end_pfn = min_t(unsigned long, 2139bda49a81SMike Rapoport PFN_DOWN(end), max_low_pfn); 2140bda49a81SMike Rapoport 2141bda49a81SMike Rapoport if (start_pfn >= end_pfn) 2142bda49a81SMike Rapoport return 0; 2143bda49a81SMike Rapoport 2144bda49a81SMike Rapoport __free_pages_memory(start_pfn, end_pfn); 2145bda49a81SMike Rapoport 2146bda49a81SMike Rapoport return end_pfn - start_pfn; 2147bda49a81SMike Rapoport } 2148bda49a81SMike Rapoport 21499092d4f7SMike Rapoport static void __init memmap_init_reserved_pages(void) 21509092d4f7SMike Rapoport { 21519092d4f7SMike Rapoport struct memblock_region *region; 21529092d4f7SMike Rapoport phys_addr_t start, end; 215361167ad5SYajun Deng int nid; 21549092d4f7SMike Rapoport 215561167ad5SYajun Deng /* 215661167ad5SYajun Deng * set nid on all reserved pages and also treat struct 215761167ad5SYajun Deng * pages for the NOMAP regions as PageReserved 215861167ad5SYajun Deng */ 21599092d4f7SMike Rapoport for_each_mem_region(region) { 216061167ad5SYajun Deng nid = memblock_get_region_node(region); 21619092d4f7SMike Rapoport start = region->base; 21629092d4f7SMike Rapoport end = start + region->size; 216361167ad5SYajun Deng 216461167ad5SYajun Deng if (memblock_is_nomap(region)) 216561167ad5SYajun Deng reserve_bootmem_region(start, end, nid); 216661167ad5SYajun Deng 216761167ad5SYajun Deng memblock_set_node(start, end, &memblock.reserved, nid); 21689092d4f7SMike Rapoport } 216961167ad5SYajun Deng 217077e6c43eSUsama Arif /* 217177e6c43eSUsama Arif * initialize struct pages for reserved regions that don't have 217277e6c43eSUsama Arif * the MEMBLOCK_RSRV_NOINIT flag set 217377e6c43eSUsama Arif */ 217461167ad5SYajun Deng for_each_reserved_mem_region(region) { 217577e6c43eSUsama Arif if (!memblock_is_reserved_noinit(region)) { 217661167ad5SYajun Deng nid = memblock_get_region_node(region); 217761167ad5SYajun Deng start = region->base; 217861167ad5SYajun Deng end = start + region->size; 217961167ad5SYajun Deng 21806a9531c3SYajun Deng if (nid == NUMA_NO_NODE || nid >= MAX_NUMNODES) 21816a9531c3SYajun Deng nid = early_pfn_to_nid(PFN_DOWN(start)); 21826a9531c3SYajun Deng 218361167ad5SYajun Deng reserve_bootmem_region(start, end, nid); 21849092d4f7SMike Rapoport } 21859092d4f7SMike Rapoport } 218677e6c43eSUsama Arif } 21879092d4f7SMike Rapoport 2188bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void) 2189bda49a81SMike Rapoport { 2190bda49a81SMike Rapoport unsigned long count = 0; 2191bda49a81SMike Rapoport phys_addr_t start, end; 2192bda49a81SMike Rapoport u64 i; 2193bda49a81SMike Rapoport 2194bda49a81SMike Rapoport memblock_clear_hotplug(0, -1); 2195bda49a81SMike Rapoport 21969092d4f7SMike Rapoport memmap_init_reserved_pages(); 2197bda49a81SMike Rapoport 2198bda49a81SMike Rapoport /* 2199bda49a81SMike Rapoport * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id 2200bda49a81SMike Rapoport * because in some case like Node0 doesn't have RAM installed 2201bda49a81SMike Rapoport * low ram will be on Node1 2202bda49a81SMike Rapoport */ 2203bda49a81SMike Rapoport for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end, 2204bda49a81SMike Rapoport NULL) 2205bda49a81SMike Rapoport count += __free_memory_core(start, end); 2206bda49a81SMike Rapoport 2207bda49a81SMike Rapoport return count; 2208bda49a81SMike Rapoport } 2209bda49a81SMike Rapoport 2210bda49a81SMike Rapoport static int reset_managed_pages_done __initdata; 2211bda49a81SMike Rapoport 2212a668968fSHaifeng Xu static void __init reset_node_managed_pages(pg_data_t *pgdat) 2213bda49a81SMike Rapoport { 2214bda49a81SMike Rapoport struct zone *z; 2215bda49a81SMike Rapoport 2216bda49a81SMike Rapoport for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 22179705bea5SArun KS atomic_long_set(&z->managed_pages, 0); 2218bda49a81SMike Rapoport } 2219bda49a81SMike Rapoport 2220bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void) 2221bda49a81SMike Rapoport { 2222bda49a81SMike Rapoport struct pglist_data *pgdat; 2223bda49a81SMike Rapoport 2224bda49a81SMike Rapoport if (reset_managed_pages_done) 2225bda49a81SMike Rapoport return; 2226bda49a81SMike Rapoport 2227bda49a81SMike Rapoport for_each_online_pgdat(pgdat) 2228bda49a81SMike Rapoport reset_node_managed_pages(pgdat); 2229bda49a81SMike Rapoport 2230bda49a81SMike Rapoport reset_managed_pages_done = 1; 2231bda49a81SMike Rapoport } 2232bda49a81SMike Rapoport 2233bda49a81SMike Rapoport /** 2234bda49a81SMike Rapoport * memblock_free_all - release free pages to the buddy allocator 2235bda49a81SMike Rapoport */ 2236097d43d8SDaeseok Youn void __init memblock_free_all(void) 2237bda49a81SMike Rapoport { 2238bda49a81SMike Rapoport unsigned long pages; 2239bda49a81SMike Rapoport 22404f5b0c17SMike Rapoport free_unused_memmap(); 2241bda49a81SMike Rapoport reset_all_zones_managed_pages(); 2242bda49a81SMike Rapoport 2243bda49a81SMike Rapoport pages = free_low_memory_core_early(); 2244ca79b0c2SArun KS totalram_pages_add(pages); 2245bda49a81SMike Rapoport } 2246bda49a81SMike Rapoport 2247350e88baSMike Rapoport #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK) 2248493f349eSYuwei Guan static const char * const flagname[] = { 2249493f349eSYuwei Guan [ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG", 2250493f349eSYuwei Guan [ilog2(MEMBLOCK_MIRROR)] = "MIRROR", 2251493f349eSYuwei Guan [ilog2(MEMBLOCK_NOMAP)] = "NOMAP", 2252493f349eSYuwei Guan [ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG", 22534f155af0SAnshuman Khandual [ilog2(MEMBLOCK_RSRV_NOINIT)] = "RSV_NIT", 2254493f349eSYuwei Guan }; 22556d03b885SBenjamin Herrenschmidt 22566d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private) 22576d03b885SBenjamin Herrenschmidt { 22586d03b885SBenjamin Herrenschmidt struct memblock_type *type = m->private; 22596d03b885SBenjamin Herrenschmidt struct memblock_region *reg; 2260de649e7fSYuwei Guan int i, j, nid; 2261493f349eSYuwei Guan unsigned int count = ARRAY_SIZE(flagname); 22625d63f81cSMiles Chen phys_addr_t end; 22636d03b885SBenjamin Herrenschmidt 22646d03b885SBenjamin Herrenschmidt for (i = 0; i < type->cnt; i++) { 22656d03b885SBenjamin Herrenschmidt reg = &type->regions[i]; 22665d63f81cSMiles Chen end = reg->base + reg->size - 1; 2267de649e7fSYuwei Guan nid = memblock_get_region_node(reg); 22686d03b885SBenjamin Herrenschmidt 22695d63f81cSMiles Chen seq_printf(m, "%4d: ", i); 2270493f349eSYuwei Guan seq_printf(m, "%pa..%pa ", ®->base, &end); 2271de649e7fSYuwei Guan if (nid != MAX_NUMNODES) 2272de649e7fSYuwei Guan seq_printf(m, "%4d ", nid); 2273de649e7fSYuwei Guan else 2274de649e7fSYuwei Guan seq_printf(m, "%4c ", 'x'); 2275493f349eSYuwei Guan if (reg->flags) { 2276493f349eSYuwei Guan for (j = 0; j < count; j++) { 2277493f349eSYuwei Guan if (reg->flags & (1U << j)) { 2278493f349eSYuwei Guan seq_printf(m, "%s\n", flagname[j]); 2279493f349eSYuwei Guan break; 2280493f349eSYuwei Guan } 2281493f349eSYuwei Guan } 2282493f349eSYuwei Guan if (j == count) 2283493f349eSYuwei Guan seq_printf(m, "%s\n", "UNKNOWN"); 2284493f349eSYuwei Guan } else { 2285493f349eSYuwei Guan seq_printf(m, "%s\n", "NONE"); 2286493f349eSYuwei Guan } 22876d03b885SBenjamin Herrenschmidt } 22886d03b885SBenjamin Herrenschmidt return 0; 22896d03b885SBenjamin Herrenschmidt } 22905ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug); 22916d03b885SBenjamin Herrenschmidt 22926d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void) 22936d03b885SBenjamin Herrenschmidt { 22946d03b885SBenjamin Herrenschmidt struct dentry *root = debugfs_create_dir("memblock", NULL); 2295d9f7979cSGreg Kroah-Hartman 22960825a6f9SJoe Perches debugfs_create_file("memory", 0444, root, 22970825a6f9SJoe Perches &memblock.memory, &memblock_debug_fops); 22980825a6f9SJoe Perches debugfs_create_file("reserved", 0444, root, 22990825a6f9SJoe Perches &memblock.reserved, &memblock_debug_fops); 230070210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 230177649905SDavid Hildenbrand debugfs_create_file("physmem", 0444, root, &physmem, 230277649905SDavid Hildenbrand &memblock_debug_fops); 230370210ed9SPhilipp Hachtmann #endif 23046d03b885SBenjamin Herrenschmidt 23056d03b885SBenjamin Herrenschmidt return 0; 23066d03b885SBenjamin Herrenschmidt } 23076d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs); 23086d03b885SBenjamin Herrenschmidt 23096d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */ 2310