195f72d1eSYinghai Lu /* 295f72d1eSYinghai Lu * Procedures for maintaining information about logical memory blocks. 395f72d1eSYinghai Lu * 495f72d1eSYinghai Lu * Peter Bergner, IBM Corp. June 2001. 595f72d1eSYinghai Lu * Copyright (C) 2001 Peter Bergner. 695f72d1eSYinghai Lu * 795f72d1eSYinghai Lu * This program is free software; you can redistribute it and/or 895f72d1eSYinghai Lu * modify it under the terms of the GNU General Public License 995f72d1eSYinghai Lu * as published by the Free Software Foundation; either version 1095f72d1eSYinghai Lu * 2 of the License, or (at your option) any later version. 1195f72d1eSYinghai Lu */ 1295f72d1eSYinghai Lu 1395f72d1eSYinghai Lu #include <linux/kernel.h> 14142b45a7SBenjamin Herrenschmidt #include <linux/slab.h> 1595f72d1eSYinghai Lu #include <linux/init.h> 1695f72d1eSYinghai Lu #include <linux/bitops.h> 17449e8df3SBenjamin Herrenschmidt #include <linux/poison.h> 18c196f76fSBenjamin Herrenschmidt #include <linux/pfn.h> 196d03b885SBenjamin Herrenschmidt #include <linux/debugfs.h> 20514c6032SRandy Dunlap #include <linux/kmemleak.h> 216d03b885SBenjamin Herrenschmidt #include <linux/seq_file.h> 2295f72d1eSYinghai Lu #include <linux/memblock.h> 2395f72d1eSYinghai Lu 24c4c5ad6bSChristoph Hellwig #include <asm/sections.h> 2526f09e9bSSantosh Shilimkar #include <linux/io.h> 2626f09e9bSSantosh Shilimkar 2726f09e9bSSantosh Shilimkar #include "internal.h" 2879442ed1STang Chen 29fe091c20STejun Heo static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; 30fe091c20STejun Heo static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; 3170210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 3270210ed9SPhilipp Hachtmann static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock; 3370210ed9SPhilipp Hachtmann #endif 34fe091c20STejun Heo 35fe091c20STejun Heo struct memblock memblock __initdata_memblock = { 36fe091c20STejun Heo .memory.regions = memblock_memory_init_regions, 37fe091c20STejun Heo .memory.cnt = 1, /* empty dummy entry */ 38fe091c20STejun Heo .memory.max = INIT_MEMBLOCK_REGIONS, 390262d9c8SHeiko Carstens .memory.name = "memory", 40fe091c20STejun Heo 41fe091c20STejun Heo .reserved.regions = memblock_reserved_init_regions, 42fe091c20STejun Heo .reserved.cnt = 1, /* empty dummy entry */ 43fe091c20STejun Heo .reserved.max = INIT_MEMBLOCK_REGIONS, 440262d9c8SHeiko Carstens .reserved.name = "reserved", 45fe091c20STejun Heo 4670210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 4770210ed9SPhilipp Hachtmann .physmem.regions = memblock_physmem_init_regions, 4870210ed9SPhilipp Hachtmann .physmem.cnt = 1, /* empty dummy entry */ 4970210ed9SPhilipp Hachtmann .physmem.max = INIT_PHYSMEM_REGIONS, 500262d9c8SHeiko Carstens .physmem.name = "physmem", 5170210ed9SPhilipp Hachtmann #endif 5270210ed9SPhilipp Hachtmann 5379442ed1STang Chen .bottom_up = false, 54fe091c20STejun Heo .current_limit = MEMBLOCK_ALLOC_ANYWHERE, 55fe091c20STejun Heo }; 5695f72d1eSYinghai Lu 5710d06439SYinghai Lu int memblock_debug __initdata_memblock; 58a3f5bafcSTony Luck static bool system_has_some_mirror __initdata_memblock = false; 591aadc056STejun Heo static int memblock_can_resize __initdata_memblock; 60181eb394SGavin Shan static int memblock_memory_in_slab __initdata_memblock = 0; 61181eb394SGavin Shan static int memblock_reserved_in_slab __initdata_memblock = 0; 6295f72d1eSYinghai Lu 63e1720feeSMike Rapoport enum memblock_flags __init_memblock choose_memblock_flags(void) 64a3f5bafcSTony Luck { 65a3f5bafcSTony Luck return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE; 66a3f5bafcSTony Luck } 67a3f5bafcSTony Luck 68eb18f1b5STejun Heo /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */ 69eb18f1b5STejun Heo static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size) 70eb18f1b5STejun Heo { 711c4bc43dSStefan Agner return *size = min(*size, PHYS_ADDR_MAX - base); 72eb18f1b5STejun Heo } 73eb18f1b5STejun Heo 746ed311b2SBenjamin Herrenschmidt /* 756ed311b2SBenjamin Herrenschmidt * Address comparison utilities 766ed311b2SBenjamin Herrenschmidt */ 7710d06439SYinghai Lu static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, 782898cc4cSBenjamin Herrenschmidt phys_addr_t base2, phys_addr_t size2) 7995f72d1eSYinghai Lu { 8095f72d1eSYinghai Lu return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); 8195f72d1eSYinghai Lu } 8295f72d1eSYinghai Lu 8395cf82ecSTang Chen bool __init_memblock memblock_overlaps_region(struct memblock_type *type, 842d7d3eb2SH Hartley Sweeten phys_addr_t base, phys_addr_t size) 856ed311b2SBenjamin Herrenschmidt { 866ed311b2SBenjamin Herrenschmidt unsigned long i; 876ed311b2SBenjamin Herrenschmidt 88f14516fbSAlexander Kuleshov for (i = 0; i < type->cnt; i++) 89f14516fbSAlexander Kuleshov if (memblock_addrs_overlap(base, size, type->regions[i].base, 90f14516fbSAlexander Kuleshov type->regions[i].size)) 916ed311b2SBenjamin Herrenschmidt break; 92c5c5c9d1STang Chen return i < type->cnt; 936ed311b2SBenjamin Herrenschmidt } 946ed311b2SBenjamin Herrenschmidt 9547cec443SMike Rapoport /** 9679442ed1STang Chen * __memblock_find_range_bottom_up - find free area utility in bottom-up 9779442ed1STang Chen * @start: start of candidate range 9847cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 9947cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 10079442ed1STang Chen * @size: size of free area to find 10179442ed1STang Chen * @align: alignment of free area to find 102b1154233SGrygorii Strashko * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 103fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 10479442ed1STang Chen * 10579442ed1STang Chen * Utility called from memblock_find_in_range_node(), find free area bottom-up. 10679442ed1STang Chen * 10747cec443SMike Rapoport * Return: 10879442ed1STang Chen * Found address on success, 0 on failure. 10979442ed1STang Chen */ 11079442ed1STang Chen static phys_addr_t __init_memblock 11179442ed1STang Chen __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end, 112fc6daaf9STony Luck phys_addr_t size, phys_addr_t align, int nid, 113e1720feeSMike Rapoport enum memblock_flags flags) 11479442ed1STang Chen { 11579442ed1STang Chen phys_addr_t this_start, this_end, cand; 11679442ed1STang Chen u64 i; 11779442ed1STang Chen 118fc6daaf9STony Luck for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) { 11979442ed1STang Chen this_start = clamp(this_start, start, end); 12079442ed1STang Chen this_end = clamp(this_end, start, end); 12179442ed1STang Chen 12279442ed1STang Chen cand = round_up(this_start, align); 12379442ed1STang Chen if (cand < this_end && this_end - cand >= size) 12479442ed1STang Chen return cand; 12579442ed1STang Chen } 12679442ed1STang Chen 12779442ed1STang Chen return 0; 12879442ed1STang Chen } 12979442ed1STang Chen 1307bd0b0f0STejun Heo /** 1311402899eSTang Chen * __memblock_find_range_top_down - find free area utility, in top-down 1321402899eSTang Chen * @start: start of candidate range 13347cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 13447cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 1351402899eSTang Chen * @size: size of free area to find 1361402899eSTang Chen * @align: alignment of free area to find 137b1154233SGrygorii Strashko * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 138fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 1391402899eSTang Chen * 1401402899eSTang Chen * Utility called from memblock_find_in_range_node(), find free area top-down. 1411402899eSTang Chen * 14247cec443SMike Rapoport * Return: 14379442ed1STang Chen * Found address on success, 0 on failure. 1441402899eSTang Chen */ 1451402899eSTang Chen static phys_addr_t __init_memblock 1461402899eSTang Chen __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end, 147fc6daaf9STony Luck phys_addr_t size, phys_addr_t align, int nid, 148e1720feeSMike Rapoport enum memblock_flags flags) 1491402899eSTang Chen { 1501402899eSTang Chen phys_addr_t this_start, this_end, cand; 1511402899eSTang Chen u64 i; 1521402899eSTang Chen 153fc6daaf9STony Luck for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end, 154fc6daaf9STony Luck NULL) { 1551402899eSTang Chen this_start = clamp(this_start, start, end); 1561402899eSTang Chen this_end = clamp(this_end, start, end); 1571402899eSTang Chen 1581402899eSTang Chen if (this_end < size) 1591402899eSTang Chen continue; 1601402899eSTang Chen 1611402899eSTang Chen cand = round_down(this_end - size, align); 1621402899eSTang Chen if (cand >= this_start) 1631402899eSTang Chen return cand; 1641402899eSTang Chen } 1651402899eSTang Chen 1661402899eSTang Chen return 0; 1671402899eSTang Chen } 1681402899eSTang Chen 1691402899eSTang Chen /** 1707bd0b0f0STejun Heo * memblock_find_in_range_node - find free area in given range and node 1717bd0b0f0STejun Heo * @size: size of free area to find 1727bd0b0f0STejun Heo * @align: alignment of free area to find 17387029ee9SGrygorii Strashko * @start: start of candidate range 17447cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 17547cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 176b1154233SGrygorii Strashko * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 177fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 1787bd0b0f0STejun Heo * 1797bd0b0f0STejun Heo * Find @size free area aligned to @align in the specified range and node. 1807bd0b0f0STejun Heo * 18179442ed1STang Chen * When allocation direction is bottom-up, the @start should be greater 18279442ed1STang Chen * than the end of the kernel image. Otherwise, it will be trimmed. The 18379442ed1STang Chen * reason is that we want the bottom-up allocation just near the kernel 18479442ed1STang Chen * image so it is highly likely that the allocated memory and the kernel 18579442ed1STang Chen * will reside in the same node. 18679442ed1STang Chen * 18779442ed1STang Chen * If bottom-up allocation failed, will try to allocate memory top-down. 18879442ed1STang Chen * 18947cec443SMike Rapoport * Return: 19079442ed1STang Chen * Found address on success, 0 on failure. 1916ed311b2SBenjamin Herrenschmidt */ 19287029ee9SGrygorii Strashko phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size, 19387029ee9SGrygorii Strashko phys_addr_t align, phys_addr_t start, 194e1720feeSMike Rapoport phys_addr_t end, int nid, 195e1720feeSMike Rapoport enum memblock_flags flags) 196f7210e6cSTang Chen { 1970cfb8f0cSTang Chen phys_addr_t kernel_end, ret; 19879442ed1STang Chen 199f7210e6cSTang Chen /* pump up @end */ 200f7210e6cSTang Chen if (end == MEMBLOCK_ALLOC_ACCESSIBLE) 201f7210e6cSTang Chen end = memblock.current_limit; 202f7210e6cSTang Chen 203f7210e6cSTang Chen /* avoid allocating the first page */ 204f7210e6cSTang Chen start = max_t(phys_addr_t, start, PAGE_SIZE); 205f7210e6cSTang Chen end = max(start, end); 20679442ed1STang Chen kernel_end = __pa_symbol(_end); 20779442ed1STang Chen 20879442ed1STang Chen /* 20979442ed1STang Chen * try bottom-up allocation only when bottom-up mode 21079442ed1STang Chen * is set and @end is above the kernel image. 21179442ed1STang Chen */ 21279442ed1STang Chen if (memblock_bottom_up() && end > kernel_end) { 21379442ed1STang Chen phys_addr_t bottom_up_start; 21479442ed1STang Chen 21579442ed1STang Chen /* make sure we will allocate above the kernel */ 21679442ed1STang Chen bottom_up_start = max(start, kernel_end); 21779442ed1STang Chen 21879442ed1STang Chen /* ok, try bottom-up allocation first */ 21979442ed1STang Chen ret = __memblock_find_range_bottom_up(bottom_up_start, end, 220fc6daaf9STony Luck size, align, nid, flags); 22179442ed1STang Chen if (ret) 22279442ed1STang Chen return ret; 22379442ed1STang Chen 22479442ed1STang Chen /* 22579442ed1STang Chen * we always limit bottom-up allocation above the kernel, 22679442ed1STang Chen * but top-down allocation doesn't have the limit, so 22779442ed1STang Chen * retrying top-down allocation may succeed when bottom-up 22879442ed1STang Chen * allocation failed. 22979442ed1STang Chen * 23079442ed1STang Chen * bottom-up allocation is expected to be fail very rarely, 23179442ed1STang Chen * so we use WARN_ONCE() here to see the stack trace if 23279442ed1STang Chen * fail happens. 23379442ed1STang Chen */ 234756a025fSJoe Perches WARN_ONCE(1, "memblock: bottom-up allocation failed, memory hotunplug may be affected\n"); 23579442ed1STang Chen } 236f7210e6cSTang Chen 237fc6daaf9STony Luck return __memblock_find_range_top_down(start, end, size, align, nid, 238fc6daaf9STony Luck flags); 239f7210e6cSTang Chen } 2406ed311b2SBenjamin Herrenschmidt 2417bd0b0f0STejun Heo /** 2427bd0b0f0STejun Heo * memblock_find_in_range - find free area in given range 2437bd0b0f0STejun Heo * @start: start of candidate range 24447cec443SMike Rapoport * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 24547cec443SMike Rapoport * %MEMBLOCK_ALLOC_ACCESSIBLE 2467bd0b0f0STejun Heo * @size: size of free area to find 2477bd0b0f0STejun Heo * @align: alignment of free area to find 2487bd0b0f0STejun Heo * 2497bd0b0f0STejun Heo * Find @size free area aligned to @align in the specified range. 2507bd0b0f0STejun Heo * 25147cec443SMike Rapoport * Return: 25279442ed1STang Chen * Found address on success, 0 on failure. 2537bd0b0f0STejun Heo */ 2547bd0b0f0STejun Heo phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, 2557bd0b0f0STejun Heo phys_addr_t end, phys_addr_t size, 2567bd0b0f0STejun Heo phys_addr_t align) 2577bd0b0f0STejun Heo { 258a3f5bafcSTony Luck phys_addr_t ret; 259e1720feeSMike Rapoport enum memblock_flags flags = choose_memblock_flags(); 260a3f5bafcSTony Luck 261a3f5bafcSTony Luck again: 262a3f5bafcSTony Luck ret = memblock_find_in_range_node(size, align, start, end, 263a3f5bafcSTony Luck NUMA_NO_NODE, flags); 264a3f5bafcSTony Luck 265a3f5bafcSTony Luck if (!ret && (flags & MEMBLOCK_MIRROR)) { 266a3f5bafcSTony Luck pr_warn("Could not allocate %pap bytes of mirrored memory\n", 267a3f5bafcSTony Luck &size); 268a3f5bafcSTony Luck flags &= ~MEMBLOCK_MIRROR; 269a3f5bafcSTony Luck goto again; 270a3f5bafcSTony Luck } 271a3f5bafcSTony Luck 272a3f5bafcSTony Luck return ret; 2737bd0b0f0STejun Heo } 2747bd0b0f0STejun Heo 27510d06439SYinghai Lu static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r) 27695f72d1eSYinghai Lu { 2771440c4e2STejun Heo type->total_size -= type->regions[r].size; 2787c0caeb8STejun Heo memmove(&type->regions[r], &type->regions[r + 1], 2797c0caeb8STejun Heo (type->cnt - (r + 1)) * sizeof(type->regions[r])); 280e3239ff9SBenjamin Herrenschmidt type->cnt--; 28195f72d1eSYinghai Lu 2828f7a6605SBenjamin Herrenschmidt /* Special case for empty arrays */ 2838f7a6605SBenjamin Herrenschmidt if (type->cnt == 0) { 2841440c4e2STejun Heo WARN_ON(type->total_size != 0); 2858f7a6605SBenjamin Herrenschmidt type->cnt = 1; 2868f7a6605SBenjamin Herrenschmidt type->regions[0].base = 0; 2878f7a6605SBenjamin Herrenschmidt type->regions[0].size = 0; 28866a20757STang Chen type->regions[0].flags = 0; 2897c0caeb8STejun Heo memblock_set_region_node(&type->regions[0], MAX_NUMNODES); 2908f7a6605SBenjamin Herrenschmidt } 29195f72d1eSYinghai Lu } 29295f72d1eSYinghai Lu 293354f17e1SPhilipp Hachtmann #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK 2943010f876SPavel Tatashin /** 29547cec443SMike Rapoport * memblock_discard - discard memory and reserved arrays if they were allocated 2963010f876SPavel Tatashin */ 2973010f876SPavel Tatashin void __init memblock_discard(void) 29829f67386SYinghai Lu { 2993010f876SPavel Tatashin phys_addr_t addr, size; 30029f67386SYinghai Lu 3013010f876SPavel Tatashin if (memblock.reserved.regions != memblock_reserved_init_regions) { 3023010f876SPavel Tatashin addr = __pa(memblock.reserved.regions); 3033010f876SPavel Tatashin size = PAGE_ALIGN(sizeof(struct memblock_region) * 30429f67386SYinghai Lu memblock.reserved.max); 3053010f876SPavel Tatashin __memblock_free_late(addr, size); 30629f67386SYinghai Lu } 30729f67386SYinghai Lu 30891b540f9SPavel Tatashin if (memblock.memory.regions != memblock_memory_init_regions) { 3093010f876SPavel Tatashin addr = __pa(memblock.memory.regions); 3103010f876SPavel Tatashin size = PAGE_ALIGN(sizeof(struct memblock_region) * 3115e270e25SPhilipp Hachtmann memblock.memory.max); 3123010f876SPavel Tatashin __memblock_free_late(addr, size); 3135e270e25SPhilipp Hachtmann } 3143010f876SPavel Tatashin } 3155e270e25SPhilipp Hachtmann #endif 3165e270e25SPhilipp Hachtmann 31748c3b583SGreg Pearson /** 31848c3b583SGreg Pearson * memblock_double_array - double the size of the memblock regions array 31948c3b583SGreg Pearson * @type: memblock type of the regions array being doubled 32048c3b583SGreg Pearson * @new_area_start: starting address of memory range to avoid overlap with 32148c3b583SGreg Pearson * @new_area_size: size of memory range to avoid overlap with 32248c3b583SGreg Pearson * 32348c3b583SGreg Pearson * Double the size of the @type regions array. If memblock is being used to 32448c3b583SGreg Pearson * allocate memory for a new reserved regions array and there is a previously 32548c3b583SGreg Pearson * allocated memory range [@new_area_start, @new_area_start + @new_area_size] 32648c3b583SGreg Pearson * waiting to be reserved, ensure the memory used by the new array does 32748c3b583SGreg Pearson * not overlap. 32848c3b583SGreg Pearson * 32947cec443SMike Rapoport * Return: 33048c3b583SGreg Pearson * 0 on success, -1 on failure. 33148c3b583SGreg Pearson */ 33248c3b583SGreg Pearson static int __init_memblock memblock_double_array(struct memblock_type *type, 33348c3b583SGreg Pearson phys_addr_t new_area_start, 33448c3b583SGreg Pearson phys_addr_t new_area_size) 335142b45a7SBenjamin Herrenschmidt { 336142b45a7SBenjamin Herrenschmidt struct memblock_region *new_array, *old_array; 33729f67386SYinghai Lu phys_addr_t old_alloc_size, new_alloc_size; 338142b45a7SBenjamin Herrenschmidt phys_addr_t old_size, new_size, addr; 339142b45a7SBenjamin Herrenschmidt int use_slab = slab_is_available(); 340181eb394SGavin Shan int *in_slab; 341142b45a7SBenjamin Herrenschmidt 342142b45a7SBenjamin Herrenschmidt /* We don't allow resizing until we know about the reserved regions 343142b45a7SBenjamin Herrenschmidt * of memory that aren't suitable for allocation 344142b45a7SBenjamin Herrenschmidt */ 345142b45a7SBenjamin Herrenschmidt if (!memblock_can_resize) 346142b45a7SBenjamin Herrenschmidt return -1; 347142b45a7SBenjamin Herrenschmidt 348142b45a7SBenjamin Herrenschmidt /* Calculate new doubled size */ 349142b45a7SBenjamin Herrenschmidt old_size = type->max * sizeof(struct memblock_region); 350142b45a7SBenjamin Herrenschmidt new_size = old_size << 1; 35129f67386SYinghai Lu /* 35229f67386SYinghai Lu * We need to allocated new one align to PAGE_SIZE, 35329f67386SYinghai Lu * so we can free them completely later. 35429f67386SYinghai Lu */ 35529f67386SYinghai Lu old_alloc_size = PAGE_ALIGN(old_size); 35629f67386SYinghai Lu new_alloc_size = PAGE_ALIGN(new_size); 357142b45a7SBenjamin Herrenschmidt 358181eb394SGavin Shan /* Retrieve the slab flag */ 359181eb394SGavin Shan if (type == &memblock.memory) 360181eb394SGavin Shan in_slab = &memblock_memory_in_slab; 361181eb394SGavin Shan else 362181eb394SGavin Shan in_slab = &memblock_reserved_in_slab; 363181eb394SGavin Shan 364142b45a7SBenjamin Herrenschmidt /* Try to find some space for it. 365142b45a7SBenjamin Herrenschmidt * 366142b45a7SBenjamin Herrenschmidt * WARNING: We assume that either slab_is_available() and we use it or 367fd07383bSAndrew Morton * we use MEMBLOCK for allocations. That means that this is unsafe to 368fd07383bSAndrew Morton * use when bootmem is currently active (unless bootmem itself is 369fd07383bSAndrew Morton * implemented on top of MEMBLOCK which isn't the case yet) 370142b45a7SBenjamin Herrenschmidt * 371142b45a7SBenjamin Herrenschmidt * This should however not be an issue for now, as we currently only 372fd07383bSAndrew Morton * call into MEMBLOCK while it's still active, or much later when slab 373fd07383bSAndrew Morton * is active for memory hotplug operations 374142b45a7SBenjamin Herrenschmidt */ 375142b45a7SBenjamin Herrenschmidt if (use_slab) { 376142b45a7SBenjamin Herrenschmidt new_array = kmalloc(new_size, GFP_KERNEL); 3771f5026a7STejun Heo addr = new_array ? __pa(new_array) : 0; 3784e2f0775SGavin Shan } else { 37948c3b583SGreg Pearson /* only exclude range when trying to double reserved.regions */ 38048c3b583SGreg Pearson if (type != &memblock.reserved) 38148c3b583SGreg Pearson new_area_start = new_area_size = 0; 38248c3b583SGreg Pearson 38348c3b583SGreg Pearson addr = memblock_find_in_range(new_area_start + new_area_size, 38448c3b583SGreg Pearson memblock.current_limit, 38529f67386SYinghai Lu new_alloc_size, PAGE_SIZE); 38648c3b583SGreg Pearson if (!addr && new_area_size) 38748c3b583SGreg Pearson addr = memblock_find_in_range(0, 38848c3b583SGreg Pearson min(new_area_start, memblock.current_limit), 38929f67386SYinghai Lu new_alloc_size, PAGE_SIZE); 39048c3b583SGreg Pearson 39115674868SSachin Kamat new_array = addr ? __va(addr) : NULL; 3924e2f0775SGavin Shan } 3931f5026a7STejun Heo if (!addr) { 394142b45a7SBenjamin Herrenschmidt pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", 3950262d9c8SHeiko Carstens type->name, type->max, type->max * 2); 396142b45a7SBenjamin Herrenschmidt return -1; 397142b45a7SBenjamin Herrenschmidt } 398142b45a7SBenjamin Herrenschmidt 399fd07383bSAndrew Morton memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]", 4000262d9c8SHeiko Carstens type->name, type->max * 2, (u64)addr, 401fd07383bSAndrew Morton (u64)addr + new_size - 1); 402ea9e4376SYinghai Lu 403fd07383bSAndrew Morton /* 404fd07383bSAndrew Morton * Found space, we now need to move the array over before we add the 405fd07383bSAndrew Morton * reserved region since it may be our reserved array itself that is 406fd07383bSAndrew Morton * full. 407142b45a7SBenjamin Herrenschmidt */ 408142b45a7SBenjamin Herrenschmidt memcpy(new_array, type->regions, old_size); 409142b45a7SBenjamin Herrenschmidt memset(new_array + type->max, 0, old_size); 410142b45a7SBenjamin Herrenschmidt old_array = type->regions; 411142b45a7SBenjamin Herrenschmidt type->regions = new_array; 412142b45a7SBenjamin Herrenschmidt type->max <<= 1; 413142b45a7SBenjamin Herrenschmidt 414fd07383bSAndrew Morton /* Free old array. We needn't free it if the array is the static one */ 415181eb394SGavin Shan if (*in_slab) 416181eb394SGavin Shan kfree(old_array); 417181eb394SGavin Shan else if (old_array != memblock_memory_init_regions && 418142b45a7SBenjamin Herrenschmidt old_array != memblock_reserved_init_regions) 41929f67386SYinghai Lu memblock_free(__pa(old_array), old_alloc_size); 420142b45a7SBenjamin Herrenschmidt 421fd07383bSAndrew Morton /* 422fd07383bSAndrew Morton * Reserve the new array if that comes from the memblock. Otherwise, we 423fd07383bSAndrew Morton * needn't do it 424181eb394SGavin Shan */ 425181eb394SGavin Shan if (!use_slab) 42629f67386SYinghai Lu BUG_ON(memblock_reserve(addr, new_alloc_size)); 427181eb394SGavin Shan 428181eb394SGavin Shan /* Update slab flag */ 429181eb394SGavin Shan *in_slab = use_slab; 430181eb394SGavin Shan 431142b45a7SBenjamin Herrenschmidt return 0; 432142b45a7SBenjamin Herrenschmidt } 433142b45a7SBenjamin Herrenschmidt 434784656f9STejun Heo /** 435784656f9STejun Heo * memblock_merge_regions - merge neighboring compatible regions 436784656f9STejun Heo * @type: memblock type to scan 437784656f9STejun Heo * 438784656f9STejun Heo * Scan @type and merge neighboring compatible regions. 439784656f9STejun Heo */ 440784656f9STejun Heo static void __init_memblock memblock_merge_regions(struct memblock_type *type) 441784656f9STejun Heo { 442784656f9STejun Heo int i = 0; 443784656f9STejun Heo 444784656f9STejun Heo /* cnt never goes below 1 */ 445784656f9STejun Heo while (i < type->cnt - 1) { 446784656f9STejun Heo struct memblock_region *this = &type->regions[i]; 447784656f9STejun Heo struct memblock_region *next = &type->regions[i + 1]; 448784656f9STejun Heo 4497c0caeb8STejun Heo if (this->base + this->size != next->base || 4507c0caeb8STejun Heo memblock_get_region_node(this) != 45166a20757STang Chen memblock_get_region_node(next) || 45266a20757STang Chen this->flags != next->flags) { 453784656f9STejun Heo BUG_ON(this->base + this->size > next->base); 454784656f9STejun Heo i++; 455784656f9STejun Heo continue; 456784656f9STejun Heo } 457784656f9STejun Heo 458784656f9STejun Heo this->size += next->size; 459c0232ae8SLin Feng /* move forward from next + 1, index of which is i + 2 */ 460c0232ae8SLin Feng memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next)); 461784656f9STejun Heo type->cnt--; 462784656f9STejun Heo } 463784656f9STejun Heo } 464784656f9STejun Heo 465784656f9STejun Heo /** 466784656f9STejun Heo * memblock_insert_region - insert new memblock region 467784656f9STejun Heo * @type: memblock type to insert into 468784656f9STejun Heo * @idx: index for the insertion point 469784656f9STejun Heo * @base: base address of the new region 470784656f9STejun Heo * @size: size of the new region 471209ff86dSTang Chen * @nid: node id of the new region 47266a20757STang Chen * @flags: flags of the new region 473784656f9STejun Heo * 474784656f9STejun Heo * Insert new memblock region [@base, @base + @size) into @type at @idx. 475412d0008SAlexander Kuleshov * @type must already have extra room to accommodate the new region. 476784656f9STejun Heo */ 477784656f9STejun Heo static void __init_memblock memblock_insert_region(struct memblock_type *type, 478784656f9STejun Heo int idx, phys_addr_t base, 47966a20757STang Chen phys_addr_t size, 480e1720feeSMike Rapoport int nid, 481e1720feeSMike Rapoport enum memblock_flags flags) 482784656f9STejun Heo { 483784656f9STejun Heo struct memblock_region *rgn = &type->regions[idx]; 484784656f9STejun Heo 485784656f9STejun Heo BUG_ON(type->cnt >= type->max); 486784656f9STejun Heo memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); 487784656f9STejun Heo rgn->base = base; 488784656f9STejun Heo rgn->size = size; 48966a20757STang Chen rgn->flags = flags; 4907c0caeb8STejun Heo memblock_set_region_node(rgn, nid); 491784656f9STejun Heo type->cnt++; 4921440c4e2STejun Heo type->total_size += size; 493784656f9STejun Heo } 494784656f9STejun Heo 495784656f9STejun Heo /** 496f1af9d3aSPhilipp Hachtmann * memblock_add_range - add new memblock region 497784656f9STejun Heo * @type: memblock type to add new region into 498784656f9STejun Heo * @base: base address of the new region 499784656f9STejun Heo * @size: size of the new region 5007fb0bc3fSTejun Heo * @nid: nid of the new region 50166a20757STang Chen * @flags: flags of the new region 502784656f9STejun Heo * 503784656f9STejun Heo * Add new memblock region [@base, @base + @size) into @type. The new region 504784656f9STejun Heo * is allowed to overlap with existing ones - overlaps don't affect already 505784656f9STejun Heo * existing regions. @type is guaranteed to be minimal (all neighbouring 506784656f9STejun Heo * compatible regions are merged) after the addition. 507784656f9STejun Heo * 50847cec443SMike Rapoport * Return: 509784656f9STejun Heo * 0 on success, -errno on failure. 510784656f9STejun Heo */ 511f1af9d3aSPhilipp Hachtmann int __init_memblock memblock_add_range(struct memblock_type *type, 51266a20757STang Chen phys_addr_t base, phys_addr_t size, 513e1720feeSMike Rapoport int nid, enum memblock_flags flags) 51495f72d1eSYinghai Lu { 515784656f9STejun Heo bool insert = false; 516eb18f1b5STejun Heo phys_addr_t obase = base; 517eb18f1b5STejun Heo phys_addr_t end = base + memblock_cap_size(base, &size); 5188c9c1701SAlexander Kuleshov int idx, nr_new; 5198c9c1701SAlexander Kuleshov struct memblock_region *rgn; 52095f72d1eSYinghai Lu 521b3dc627cSTejun Heo if (!size) 522b3dc627cSTejun Heo return 0; 523b3dc627cSTejun Heo 524784656f9STejun Heo /* special case for empty array */ 525784656f9STejun Heo if (type->regions[0].size == 0) { 5261440c4e2STejun Heo WARN_ON(type->cnt != 1 || type->total_size); 527784656f9STejun Heo type->regions[0].base = base; 528784656f9STejun Heo type->regions[0].size = size; 52966a20757STang Chen type->regions[0].flags = flags; 5307fb0bc3fSTejun Heo memblock_set_region_node(&type->regions[0], nid); 5311440c4e2STejun Heo type->total_size = size; 532784656f9STejun Heo return 0; 533784656f9STejun Heo } 534784656f9STejun Heo repeat: 535784656f9STejun Heo /* 536784656f9STejun Heo * The following is executed twice. Once with %false @insert and 537784656f9STejun Heo * then with %true. The first counts the number of regions needed 538412d0008SAlexander Kuleshov * to accommodate the new area. The second actually inserts them. 539784656f9STejun Heo */ 540784656f9STejun Heo base = obase; 541784656f9STejun Heo nr_new = 0; 542784656f9STejun Heo 54366e8b438SGioh Kim for_each_memblock_type(idx, type, rgn) { 544784656f9STejun Heo phys_addr_t rbase = rgn->base; 545784656f9STejun Heo phys_addr_t rend = rbase + rgn->size; 5468f7a6605SBenjamin Herrenschmidt 547784656f9STejun Heo if (rbase >= end) 5488f7a6605SBenjamin Herrenschmidt break; 549784656f9STejun Heo if (rend <= base) 550784656f9STejun Heo continue; 551784656f9STejun Heo /* 552784656f9STejun Heo * @rgn overlaps. If it separates the lower part of new 553784656f9STejun Heo * area, insert that portion. 5548f7a6605SBenjamin Herrenschmidt */ 555784656f9STejun Heo if (rbase > base) { 556c0a29498SWei Yang #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 557c0a29498SWei Yang WARN_ON(nid != memblock_get_region_node(rgn)); 558c0a29498SWei Yang #endif 5594fcab5f4SWei Yang WARN_ON(flags != rgn->flags); 560784656f9STejun Heo nr_new++; 561784656f9STejun Heo if (insert) 5628c9c1701SAlexander Kuleshov memblock_insert_region(type, idx++, base, 56366a20757STang Chen rbase - base, nid, 56466a20757STang Chen flags); 565784656f9STejun Heo } 566784656f9STejun Heo /* area below @rend is dealt with, forget about it */ 567784656f9STejun Heo base = min(rend, end); 5688f7a6605SBenjamin Herrenschmidt } 5698f7a6605SBenjamin Herrenschmidt 570784656f9STejun Heo /* insert the remaining portion */ 571784656f9STejun Heo if (base < end) { 572784656f9STejun Heo nr_new++; 573784656f9STejun Heo if (insert) 5748c9c1701SAlexander Kuleshov memblock_insert_region(type, idx, base, end - base, 57566a20757STang Chen nid, flags); 5768f7a6605SBenjamin Herrenschmidt } 5778f7a6605SBenjamin Herrenschmidt 578ef3cc4dbSnimisolo if (!nr_new) 579ef3cc4dbSnimisolo return 0; 580ef3cc4dbSnimisolo 581784656f9STejun Heo /* 582784656f9STejun Heo * If this was the first round, resize array and repeat for actual 583784656f9STejun Heo * insertions; otherwise, merge and return. 5848f7a6605SBenjamin Herrenschmidt */ 585784656f9STejun Heo if (!insert) { 586784656f9STejun Heo while (type->cnt + nr_new > type->max) 58748c3b583SGreg Pearson if (memblock_double_array(type, obase, size) < 0) 588784656f9STejun Heo return -ENOMEM; 589784656f9STejun Heo insert = true; 590784656f9STejun Heo goto repeat; 59195f72d1eSYinghai Lu } else { 592784656f9STejun Heo memblock_merge_regions(type); 59395f72d1eSYinghai Lu return 0; 59495f72d1eSYinghai Lu } 595784656f9STejun Heo } 59695f72d1eSYinghai Lu 597*48a833ccSMike Rapoport /** 598*48a833ccSMike Rapoport * memblock_add_node - add new memblock region within a NUMA node 599*48a833ccSMike Rapoport * @base: base address of the new region 600*48a833ccSMike Rapoport * @size: size of the new region 601*48a833ccSMike Rapoport * @nid: nid of the new region 602*48a833ccSMike Rapoport * 603*48a833ccSMike Rapoport * Add new memblock region [@base, @base + @size) to the "memory" 604*48a833ccSMike Rapoport * type. See memblock_add_range() description for mode details 605*48a833ccSMike Rapoport * 606*48a833ccSMike Rapoport * Return: 607*48a833ccSMike Rapoport * 0 on success, -errno on failure. 608*48a833ccSMike Rapoport */ 6097fb0bc3fSTejun Heo int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size, 6107fb0bc3fSTejun Heo int nid) 6117fb0bc3fSTejun Heo { 612f1af9d3aSPhilipp Hachtmann return memblock_add_range(&memblock.memory, base, size, nid, 0); 6137fb0bc3fSTejun Heo } 6147fb0bc3fSTejun Heo 615*48a833ccSMike Rapoport /** 616*48a833ccSMike Rapoport * memblock_add - add new memblock region 617*48a833ccSMike Rapoport * @base: base address of the new region 618*48a833ccSMike Rapoport * @size: size of the new region 619*48a833ccSMike Rapoport * 620*48a833ccSMike Rapoport * Add new memblock region [@base, @base + @size) to the "memory" 621*48a833ccSMike Rapoport * type. See memblock_add_range() description for mode details 622*48a833ccSMike Rapoport * 623*48a833ccSMike Rapoport * Return: 624*48a833ccSMike Rapoport * 0 on success, -errno on failure. 625*48a833ccSMike Rapoport */ 626f705ac4bSAlexander Kuleshov int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) 6276a4055bcSAlexander Kuleshov { 6285d63f81cSMiles Chen phys_addr_t end = base + size - 1; 6295d63f81cSMiles Chen 6305d63f81cSMiles Chen memblock_dbg("memblock_add: [%pa-%pa] %pF\n", 6315d63f81cSMiles Chen &base, &end, (void *)_RET_IP_); 6326a4055bcSAlexander Kuleshov 633f705ac4bSAlexander Kuleshov return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0); 63495f72d1eSYinghai Lu } 63595f72d1eSYinghai Lu 6366a9ceb31STejun Heo /** 6376a9ceb31STejun Heo * memblock_isolate_range - isolate given range into disjoint memblocks 6386a9ceb31STejun Heo * @type: memblock type to isolate range for 6396a9ceb31STejun Heo * @base: base of range to isolate 6406a9ceb31STejun Heo * @size: size of range to isolate 6416a9ceb31STejun Heo * @start_rgn: out parameter for the start of isolated region 6426a9ceb31STejun Heo * @end_rgn: out parameter for the end of isolated region 6436a9ceb31STejun Heo * 6446a9ceb31STejun Heo * Walk @type and ensure that regions don't cross the boundaries defined by 6456a9ceb31STejun Heo * [@base, @base + @size). Crossing regions are split at the boundaries, 6466a9ceb31STejun Heo * which may create at most two more regions. The index of the first 6476a9ceb31STejun Heo * region inside the range is returned in *@start_rgn and end in *@end_rgn. 6486a9ceb31STejun Heo * 64947cec443SMike Rapoport * Return: 6506a9ceb31STejun Heo * 0 on success, -errno on failure. 6516a9ceb31STejun Heo */ 6526a9ceb31STejun Heo static int __init_memblock memblock_isolate_range(struct memblock_type *type, 6536a9ceb31STejun Heo phys_addr_t base, phys_addr_t size, 6546a9ceb31STejun Heo int *start_rgn, int *end_rgn) 6556a9ceb31STejun Heo { 656eb18f1b5STejun Heo phys_addr_t end = base + memblock_cap_size(base, &size); 6578c9c1701SAlexander Kuleshov int idx; 6588c9c1701SAlexander Kuleshov struct memblock_region *rgn; 6596a9ceb31STejun Heo 6606a9ceb31STejun Heo *start_rgn = *end_rgn = 0; 6616a9ceb31STejun Heo 662b3dc627cSTejun Heo if (!size) 663b3dc627cSTejun Heo return 0; 664b3dc627cSTejun Heo 6656a9ceb31STejun Heo /* we'll create at most two more regions */ 6666a9ceb31STejun Heo while (type->cnt + 2 > type->max) 66748c3b583SGreg Pearson if (memblock_double_array(type, base, size) < 0) 6686a9ceb31STejun Heo return -ENOMEM; 6696a9ceb31STejun Heo 67066e8b438SGioh Kim for_each_memblock_type(idx, type, rgn) { 6716a9ceb31STejun Heo phys_addr_t rbase = rgn->base; 6726a9ceb31STejun Heo phys_addr_t rend = rbase + rgn->size; 6736a9ceb31STejun Heo 6746a9ceb31STejun Heo if (rbase >= end) 6756a9ceb31STejun Heo break; 6766a9ceb31STejun Heo if (rend <= base) 6776a9ceb31STejun Heo continue; 6786a9ceb31STejun Heo 6796a9ceb31STejun Heo if (rbase < base) { 6806a9ceb31STejun Heo /* 6816a9ceb31STejun Heo * @rgn intersects from below. Split and continue 6826a9ceb31STejun Heo * to process the next region - the new top half. 6836a9ceb31STejun Heo */ 6846a9ceb31STejun Heo rgn->base = base; 6851440c4e2STejun Heo rgn->size -= base - rbase; 6861440c4e2STejun Heo type->total_size -= base - rbase; 6878c9c1701SAlexander Kuleshov memblock_insert_region(type, idx, rbase, base - rbase, 68866a20757STang Chen memblock_get_region_node(rgn), 68966a20757STang Chen rgn->flags); 6906a9ceb31STejun Heo } else if (rend > end) { 6916a9ceb31STejun Heo /* 6926a9ceb31STejun Heo * @rgn intersects from above. Split and redo the 6936a9ceb31STejun Heo * current region - the new bottom half. 6946a9ceb31STejun Heo */ 6956a9ceb31STejun Heo rgn->base = end; 6961440c4e2STejun Heo rgn->size -= end - rbase; 6971440c4e2STejun Heo type->total_size -= end - rbase; 6988c9c1701SAlexander Kuleshov memblock_insert_region(type, idx--, rbase, end - rbase, 69966a20757STang Chen memblock_get_region_node(rgn), 70066a20757STang Chen rgn->flags); 7016a9ceb31STejun Heo } else { 7026a9ceb31STejun Heo /* @rgn is fully contained, record it */ 7036a9ceb31STejun Heo if (!*end_rgn) 7048c9c1701SAlexander Kuleshov *start_rgn = idx; 7058c9c1701SAlexander Kuleshov *end_rgn = idx + 1; 7066a9ceb31STejun Heo } 7076a9ceb31STejun Heo } 7086a9ceb31STejun Heo 7096a9ceb31STejun Heo return 0; 7106a9ceb31STejun Heo } 7116a9ceb31STejun Heo 71235bd16a2SAlexander Kuleshov static int __init_memblock memblock_remove_range(struct memblock_type *type, 7138f7a6605SBenjamin Herrenschmidt phys_addr_t base, phys_addr_t size) 71495f72d1eSYinghai Lu { 71571936180STejun Heo int start_rgn, end_rgn; 71671936180STejun Heo int i, ret; 71795f72d1eSYinghai Lu 71871936180STejun Heo ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 71971936180STejun Heo if (ret) 72071936180STejun Heo return ret; 72195f72d1eSYinghai Lu 72271936180STejun Heo for (i = end_rgn - 1; i >= start_rgn; i--) 72371936180STejun Heo memblock_remove_region(type, i); 72495f72d1eSYinghai Lu return 0; 72595f72d1eSYinghai Lu } 72695f72d1eSYinghai Lu 727581adcbeSTejun Heo int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size) 72895f72d1eSYinghai Lu { 72925cf23d7SMinchan Kim phys_addr_t end = base + size - 1; 73025cf23d7SMinchan Kim 73125cf23d7SMinchan Kim memblock_dbg("memblock_remove: [%pa-%pa] %pS\n", 73225cf23d7SMinchan Kim &base, &end, (void *)_RET_IP_); 73325cf23d7SMinchan Kim 734f1af9d3aSPhilipp Hachtmann return memblock_remove_range(&memblock.memory, base, size); 73595f72d1eSYinghai Lu } 73695f72d1eSYinghai Lu 737f1af9d3aSPhilipp Hachtmann 738581adcbeSTejun Heo int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size) 73995f72d1eSYinghai Lu { 7405d63f81cSMiles Chen phys_addr_t end = base + size - 1; 7415d63f81cSMiles Chen 7425d63f81cSMiles Chen memblock_dbg(" memblock_free: [%pa-%pa] %pF\n", 7435d63f81cSMiles Chen &base, &end, (void *)_RET_IP_); 74424aa0788STejun Heo 7459099daedSCatalin Marinas kmemleak_free_part_phys(base, size); 746f1af9d3aSPhilipp Hachtmann return memblock_remove_range(&memblock.reserved, base, size); 74795f72d1eSYinghai Lu } 74895f72d1eSYinghai Lu 749f705ac4bSAlexander Kuleshov int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) 75095f72d1eSYinghai Lu { 7515d63f81cSMiles Chen phys_addr_t end = base + size - 1; 7525d63f81cSMiles Chen 7535d63f81cSMiles Chen memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n", 7545d63f81cSMiles Chen &base, &end, (void *)_RET_IP_); 75595f72d1eSYinghai Lu 756f705ac4bSAlexander Kuleshov return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0); 75795f72d1eSYinghai Lu } 75895f72d1eSYinghai Lu 75935fd0808STejun Heo /** 76047cec443SMike Rapoport * memblock_setclr_flag - set or clear flag for a memory region 76147cec443SMike Rapoport * @base: base address of the region 76247cec443SMike Rapoport * @size: size of the region 76347cec443SMike Rapoport * @set: set or clear the flag 76447cec443SMike Rapoport * @flag: the flag to udpate 76566b16edfSTang Chen * 7664308ce17STony Luck * This function isolates region [@base, @base + @size), and sets/clears flag 76766b16edfSTang Chen * 76847cec443SMike Rapoport * Return: 0 on success, -errno on failure. 76966b16edfSTang Chen */ 7704308ce17STony Luck static int __init_memblock memblock_setclr_flag(phys_addr_t base, 7714308ce17STony Luck phys_addr_t size, int set, int flag) 77266b16edfSTang Chen { 77366b16edfSTang Chen struct memblock_type *type = &memblock.memory; 77466b16edfSTang Chen int i, ret, start_rgn, end_rgn; 77566b16edfSTang Chen 77666b16edfSTang Chen ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 77766b16edfSTang Chen if (ret) 77866b16edfSTang Chen return ret; 77966b16edfSTang Chen 78066b16edfSTang Chen for (i = start_rgn; i < end_rgn; i++) 7814308ce17STony Luck if (set) 7824308ce17STony Luck memblock_set_region_flags(&type->regions[i], flag); 7834308ce17STony Luck else 7844308ce17STony Luck memblock_clear_region_flags(&type->regions[i], flag); 78566b16edfSTang Chen 78666b16edfSTang Chen memblock_merge_regions(type); 78766b16edfSTang Chen return 0; 78866b16edfSTang Chen } 78966b16edfSTang Chen 79066b16edfSTang Chen /** 7914308ce17STony Luck * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG. 7924308ce17STony Luck * @base: the base phys addr of the region 7934308ce17STony Luck * @size: the size of the region 7944308ce17STony Luck * 79547cec443SMike Rapoport * Return: 0 on success, -errno on failure. 7964308ce17STony Luck */ 7974308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size) 7984308ce17STony Luck { 7994308ce17STony Luck return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG); 8004308ce17STony Luck } 8014308ce17STony Luck 8024308ce17STony Luck /** 80366b16edfSTang Chen * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region. 80466b16edfSTang Chen * @base: the base phys addr of the region 80566b16edfSTang Chen * @size: the size of the region 80666b16edfSTang Chen * 80747cec443SMike Rapoport * Return: 0 on success, -errno on failure. 80866b16edfSTang Chen */ 80966b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size) 81066b16edfSTang Chen { 8114308ce17STony Luck return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG); 81266b16edfSTang Chen } 81366b16edfSTang Chen 81466b16edfSTang Chen /** 815a3f5bafcSTony Luck * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR. 816a3f5bafcSTony Luck * @base: the base phys addr of the region 817a3f5bafcSTony Luck * @size: the size of the region 818a3f5bafcSTony Luck * 81947cec443SMike Rapoport * Return: 0 on success, -errno on failure. 820a3f5bafcSTony Luck */ 821a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size) 822a3f5bafcSTony Luck { 823a3f5bafcSTony Luck system_has_some_mirror = true; 824a3f5bafcSTony Luck 825a3f5bafcSTony Luck return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR); 826a3f5bafcSTony Luck } 827a3f5bafcSTony Luck 828bf3d3cc5SArd Biesheuvel /** 829bf3d3cc5SArd Biesheuvel * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP. 830bf3d3cc5SArd Biesheuvel * @base: the base phys addr of the region 831bf3d3cc5SArd Biesheuvel * @size: the size of the region 832bf3d3cc5SArd Biesheuvel * 83347cec443SMike Rapoport * Return: 0 on success, -errno on failure. 834bf3d3cc5SArd Biesheuvel */ 835bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size) 836bf3d3cc5SArd Biesheuvel { 837bf3d3cc5SArd Biesheuvel return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP); 838bf3d3cc5SArd Biesheuvel } 839a3f5bafcSTony Luck 840a3f5bafcSTony Luck /** 8414c546b8aSAKASHI Takahiro * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region. 8424c546b8aSAKASHI Takahiro * @base: the base phys addr of the region 8434c546b8aSAKASHI Takahiro * @size: the size of the region 8444c546b8aSAKASHI Takahiro * 84547cec443SMike Rapoport * Return: 0 on success, -errno on failure. 8464c546b8aSAKASHI Takahiro */ 8474c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size) 8484c546b8aSAKASHI Takahiro { 8494c546b8aSAKASHI Takahiro return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP); 8504c546b8aSAKASHI Takahiro } 8514c546b8aSAKASHI Takahiro 8524c546b8aSAKASHI Takahiro /** 8538e7a7f86SRobin Holt * __next_reserved_mem_region - next function for for_each_reserved_region() 8548e7a7f86SRobin Holt * @idx: pointer to u64 loop variable 8558e7a7f86SRobin Holt * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL 8568e7a7f86SRobin Holt * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL 8578e7a7f86SRobin Holt * 8588e7a7f86SRobin Holt * Iterate over all reserved memory regions. 8598e7a7f86SRobin Holt */ 8608e7a7f86SRobin Holt void __init_memblock __next_reserved_mem_region(u64 *idx, 8618e7a7f86SRobin Holt phys_addr_t *out_start, 8628e7a7f86SRobin Holt phys_addr_t *out_end) 8638e7a7f86SRobin Holt { 864567d117bSAlexander Kuleshov struct memblock_type *type = &memblock.reserved; 8658e7a7f86SRobin Holt 866cd33a76bSRichard Leitner if (*idx < type->cnt) { 867567d117bSAlexander Kuleshov struct memblock_region *r = &type->regions[*idx]; 8688e7a7f86SRobin Holt phys_addr_t base = r->base; 8698e7a7f86SRobin Holt phys_addr_t size = r->size; 8708e7a7f86SRobin Holt 8718e7a7f86SRobin Holt if (out_start) 8728e7a7f86SRobin Holt *out_start = base; 8738e7a7f86SRobin Holt if (out_end) 8748e7a7f86SRobin Holt *out_end = base + size - 1; 8758e7a7f86SRobin Holt 8768e7a7f86SRobin Holt *idx += 1; 8778e7a7f86SRobin Holt return; 8788e7a7f86SRobin Holt } 8798e7a7f86SRobin Holt 8808e7a7f86SRobin Holt /* signal end of iteration */ 8818e7a7f86SRobin Holt *idx = ULLONG_MAX; 8828e7a7f86SRobin Holt } 8838e7a7f86SRobin Holt 8848e7a7f86SRobin Holt /** 885f1af9d3aSPhilipp Hachtmann * __next__mem_range - next function for for_each_free_mem_range() etc. 88635fd0808STejun Heo * @idx: pointer to u64 loop variable 887b1154233SGrygorii Strashko * @nid: node selector, %NUMA_NO_NODE for all nodes 888fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 889f1af9d3aSPhilipp Hachtmann * @type_a: pointer to memblock_type from where the range is taken 890f1af9d3aSPhilipp Hachtmann * @type_b: pointer to memblock_type which excludes memory from being taken 891dad7557eSWanpeng Li * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 892dad7557eSWanpeng Li * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 893dad7557eSWanpeng Li * @out_nid: ptr to int for nid of the range, can be %NULL 89435fd0808STejun Heo * 895f1af9d3aSPhilipp Hachtmann * Find the first area from *@idx which matches @nid, fill the out 89635fd0808STejun Heo * parameters, and update *@idx for the next iteration. The lower 32bit of 897f1af9d3aSPhilipp Hachtmann * *@idx contains index into type_a and the upper 32bit indexes the 898f1af9d3aSPhilipp Hachtmann * areas before each region in type_b. For example, if type_b regions 89935fd0808STejun Heo * look like the following, 90035fd0808STejun Heo * 90135fd0808STejun Heo * 0:[0-16), 1:[32-48), 2:[128-130) 90235fd0808STejun Heo * 90335fd0808STejun Heo * The upper 32bit indexes the following regions. 90435fd0808STejun Heo * 90535fd0808STejun Heo * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) 90635fd0808STejun Heo * 90735fd0808STejun Heo * As both region arrays are sorted, the function advances the two indices 90835fd0808STejun Heo * in lockstep and returns each intersection. 90935fd0808STejun Heo */ 910e1720feeSMike Rapoport void __init_memblock __next_mem_range(u64 *idx, int nid, 911e1720feeSMike Rapoport enum memblock_flags flags, 912f1af9d3aSPhilipp Hachtmann struct memblock_type *type_a, 913f1af9d3aSPhilipp Hachtmann struct memblock_type *type_b, 91435fd0808STejun Heo phys_addr_t *out_start, 91535fd0808STejun Heo phys_addr_t *out_end, int *out_nid) 91635fd0808STejun Heo { 917f1af9d3aSPhilipp Hachtmann int idx_a = *idx & 0xffffffff; 918f1af9d3aSPhilipp Hachtmann int idx_b = *idx >> 32; 919b1154233SGrygorii Strashko 920f1af9d3aSPhilipp Hachtmann if (WARN_ONCE(nid == MAX_NUMNODES, 921f1af9d3aSPhilipp Hachtmann "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) 922560dca27SGrygorii Strashko nid = NUMA_NO_NODE; 92335fd0808STejun Heo 924f1af9d3aSPhilipp Hachtmann for (; idx_a < type_a->cnt; idx_a++) { 925f1af9d3aSPhilipp Hachtmann struct memblock_region *m = &type_a->regions[idx_a]; 926f1af9d3aSPhilipp Hachtmann 92735fd0808STejun Heo phys_addr_t m_start = m->base; 92835fd0808STejun Heo phys_addr_t m_end = m->base + m->size; 929f1af9d3aSPhilipp Hachtmann int m_nid = memblock_get_region_node(m); 93035fd0808STejun Heo 93135fd0808STejun Heo /* only memory regions are associated with nodes, check it */ 932f1af9d3aSPhilipp Hachtmann if (nid != NUMA_NO_NODE && nid != m_nid) 93335fd0808STejun Heo continue; 93435fd0808STejun Heo 9350a313a99SXishi Qiu /* skip hotpluggable memory regions if needed */ 9360a313a99SXishi Qiu if (movable_node_is_enabled() && memblock_is_hotpluggable(m)) 9370a313a99SXishi Qiu continue; 9380a313a99SXishi Qiu 939a3f5bafcSTony Luck /* if we want mirror memory skip non-mirror memory regions */ 940a3f5bafcSTony Luck if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) 941a3f5bafcSTony Luck continue; 942a3f5bafcSTony Luck 943bf3d3cc5SArd Biesheuvel /* skip nomap memory unless we were asked for it explicitly */ 944bf3d3cc5SArd Biesheuvel if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) 945bf3d3cc5SArd Biesheuvel continue; 946bf3d3cc5SArd Biesheuvel 947f1af9d3aSPhilipp Hachtmann if (!type_b) { 948f1af9d3aSPhilipp Hachtmann if (out_start) 949f1af9d3aSPhilipp Hachtmann *out_start = m_start; 950f1af9d3aSPhilipp Hachtmann if (out_end) 951f1af9d3aSPhilipp Hachtmann *out_end = m_end; 952f1af9d3aSPhilipp Hachtmann if (out_nid) 953f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 954f1af9d3aSPhilipp Hachtmann idx_a++; 955f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 956f1af9d3aSPhilipp Hachtmann return; 957f1af9d3aSPhilipp Hachtmann } 95835fd0808STejun Heo 959f1af9d3aSPhilipp Hachtmann /* scan areas before each reservation */ 960f1af9d3aSPhilipp Hachtmann for (; idx_b < type_b->cnt + 1; idx_b++) { 961f1af9d3aSPhilipp Hachtmann struct memblock_region *r; 962f1af9d3aSPhilipp Hachtmann phys_addr_t r_start; 963f1af9d3aSPhilipp Hachtmann phys_addr_t r_end; 964f1af9d3aSPhilipp Hachtmann 965f1af9d3aSPhilipp Hachtmann r = &type_b->regions[idx_b]; 966f1af9d3aSPhilipp Hachtmann r_start = idx_b ? r[-1].base + r[-1].size : 0; 967f1af9d3aSPhilipp Hachtmann r_end = idx_b < type_b->cnt ? 9681c4bc43dSStefan Agner r->base : PHYS_ADDR_MAX; 969f1af9d3aSPhilipp Hachtmann 970f1af9d3aSPhilipp Hachtmann /* 971f1af9d3aSPhilipp Hachtmann * if idx_b advanced past idx_a, 972f1af9d3aSPhilipp Hachtmann * break out to advance idx_a 973f1af9d3aSPhilipp Hachtmann */ 97435fd0808STejun Heo if (r_start >= m_end) 97535fd0808STejun Heo break; 97635fd0808STejun Heo /* if the two regions intersect, we're done */ 97735fd0808STejun Heo if (m_start < r_end) { 97835fd0808STejun Heo if (out_start) 979f1af9d3aSPhilipp Hachtmann *out_start = 980f1af9d3aSPhilipp Hachtmann max(m_start, r_start); 98135fd0808STejun Heo if (out_end) 98235fd0808STejun Heo *out_end = min(m_end, r_end); 98335fd0808STejun Heo if (out_nid) 984f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 98535fd0808STejun Heo /* 986f1af9d3aSPhilipp Hachtmann * The region which ends first is 987f1af9d3aSPhilipp Hachtmann * advanced for the next iteration. 98835fd0808STejun Heo */ 98935fd0808STejun Heo if (m_end <= r_end) 990f1af9d3aSPhilipp Hachtmann idx_a++; 99135fd0808STejun Heo else 992f1af9d3aSPhilipp Hachtmann idx_b++; 993f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 99435fd0808STejun Heo return; 99535fd0808STejun Heo } 99635fd0808STejun Heo } 99735fd0808STejun Heo } 99835fd0808STejun Heo 99935fd0808STejun Heo /* signal end of iteration */ 100035fd0808STejun Heo *idx = ULLONG_MAX; 100135fd0808STejun Heo } 100235fd0808STejun Heo 10037bd0b0f0STejun Heo /** 1004f1af9d3aSPhilipp Hachtmann * __next_mem_range_rev - generic next function for for_each_*_range_rev() 1005f1af9d3aSPhilipp Hachtmann * 10067bd0b0f0STejun Heo * @idx: pointer to u64 loop variable 1007ad5ea8cdSAlexander Kuleshov * @nid: node selector, %NUMA_NO_NODE for all nodes 1008fc6daaf9STony Luck * @flags: pick from blocks based on memory attributes 1009f1af9d3aSPhilipp Hachtmann * @type_a: pointer to memblock_type from where the range is taken 1010f1af9d3aSPhilipp Hachtmann * @type_b: pointer to memblock_type which excludes memory from being taken 1011dad7557eSWanpeng Li * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 1012dad7557eSWanpeng Li * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 1013dad7557eSWanpeng Li * @out_nid: ptr to int for nid of the range, can be %NULL 10147bd0b0f0STejun Heo * 101547cec443SMike Rapoport * Finds the next range from type_a which is not marked as unsuitable 101647cec443SMike Rapoport * in type_b. 101747cec443SMike Rapoport * 1018f1af9d3aSPhilipp Hachtmann * Reverse of __next_mem_range(). 10197bd0b0f0STejun Heo */ 1020e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid, 1021e1720feeSMike Rapoport enum memblock_flags flags, 1022f1af9d3aSPhilipp Hachtmann struct memblock_type *type_a, 1023f1af9d3aSPhilipp Hachtmann struct memblock_type *type_b, 10247bd0b0f0STejun Heo phys_addr_t *out_start, 10257bd0b0f0STejun Heo phys_addr_t *out_end, int *out_nid) 10267bd0b0f0STejun Heo { 1027f1af9d3aSPhilipp Hachtmann int idx_a = *idx & 0xffffffff; 1028f1af9d3aSPhilipp Hachtmann int idx_b = *idx >> 32; 1029b1154233SGrygorii Strashko 1030560dca27SGrygorii Strashko if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) 1031560dca27SGrygorii Strashko nid = NUMA_NO_NODE; 10327bd0b0f0STejun Heo 10337bd0b0f0STejun Heo if (*idx == (u64)ULLONG_MAX) { 1034f1af9d3aSPhilipp Hachtmann idx_a = type_a->cnt - 1; 1035e47608abSzijun_hu if (type_b != NULL) 1036f1af9d3aSPhilipp Hachtmann idx_b = type_b->cnt; 1037e47608abSzijun_hu else 1038e47608abSzijun_hu idx_b = 0; 10397bd0b0f0STejun Heo } 10407bd0b0f0STejun Heo 1041f1af9d3aSPhilipp Hachtmann for (; idx_a >= 0; idx_a--) { 1042f1af9d3aSPhilipp Hachtmann struct memblock_region *m = &type_a->regions[idx_a]; 1043f1af9d3aSPhilipp Hachtmann 10447bd0b0f0STejun Heo phys_addr_t m_start = m->base; 10457bd0b0f0STejun Heo phys_addr_t m_end = m->base + m->size; 1046f1af9d3aSPhilipp Hachtmann int m_nid = memblock_get_region_node(m); 10477bd0b0f0STejun Heo 10487bd0b0f0STejun Heo /* only memory regions are associated with nodes, check it */ 1049f1af9d3aSPhilipp Hachtmann if (nid != NUMA_NO_NODE && nid != m_nid) 10507bd0b0f0STejun Heo continue; 10517bd0b0f0STejun Heo 105255ac590cSTang Chen /* skip hotpluggable memory regions if needed */ 105355ac590cSTang Chen if (movable_node_is_enabled() && memblock_is_hotpluggable(m)) 105455ac590cSTang Chen continue; 105555ac590cSTang Chen 1056a3f5bafcSTony Luck /* if we want mirror memory skip non-mirror memory regions */ 1057a3f5bafcSTony Luck if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) 1058a3f5bafcSTony Luck continue; 1059a3f5bafcSTony Luck 1060bf3d3cc5SArd Biesheuvel /* skip nomap memory unless we were asked for it explicitly */ 1061bf3d3cc5SArd Biesheuvel if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) 1062bf3d3cc5SArd Biesheuvel continue; 1063bf3d3cc5SArd Biesheuvel 1064f1af9d3aSPhilipp Hachtmann if (!type_b) { 1065f1af9d3aSPhilipp Hachtmann if (out_start) 1066f1af9d3aSPhilipp Hachtmann *out_start = m_start; 1067f1af9d3aSPhilipp Hachtmann if (out_end) 1068f1af9d3aSPhilipp Hachtmann *out_end = m_end; 1069f1af9d3aSPhilipp Hachtmann if (out_nid) 1070f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 1071fb399b48Szijun_hu idx_a--; 1072f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 1073f1af9d3aSPhilipp Hachtmann return; 1074f1af9d3aSPhilipp Hachtmann } 10757bd0b0f0STejun Heo 1076f1af9d3aSPhilipp Hachtmann /* scan areas before each reservation */ 1077f1af9d3aSPhilipp Hachtmann for (; idx_b >= 0; idx_b--) { 1078f1af9d3aSPhilipp Hachtmann struct memblock_region *r; 1079f1af9d3aSPhilipp Hachtmann phys_addr_t r_start; 1080f1af9d3aSPhilipp Hachtmann phys_addr_t r_end; 1081f1af9d3aSPhilipp Hachtmann 1082f1af9d3aSPhilipp Hachtmann r = &type_b->regions[idx_b]; 1083f1af9d3aSPhilipp Hachtmann r_start = idx_b ? r[-1].base + r[-1].size : 0; 1084f1af9d3aSPhilipp Hachtmann r_end = idx_b < type_b->cnt ? 10851c4bc43dSStefan Agner r->base : PHYS_ADDR_MAX; 1086f1af9d3aSPhilipp Hachtmann /* 1087f1af9d3aSPhilipp Hachtmann * if idx_b advanced past idx_a, 1088f1af9d3aSPhilipp Hachtmann * break out to advance idx_a 1089f1af9d3aSPhilipp Hachtmann */ 1090f1af9d3aSPhilipp Hachtmann 10917bd0b0f0STejun Heo if (r_end <= m_start) 10927bd0b0f0STejun Heo break; 10937bd0b0f0STejun Heo /* if the two regions intersect, we're done */ 10947bd0b0f0STejun Heo if (m_end > r_start) { 10957bd0b0f0STejun Heo if (out_start) 10967bd0b0f0STejun Heo *out_start = max(m_start, r_start); 10977bd0b0f0STejun Heo if (out_end) 10987bd0b0f0STejun Heo *out_end = min(m_end, r_end); 10997bd0b0f0STejun Heo if (out_nid) 1100f1af9d3aSPhilipp Hachtmann *out_nid = m_nid; 11017bd0b0f0STejun Heo if (m_start >= r_start) 1102f1af9d3aSPhilipp Hachtmann idx_a--; 11037bd0b0f0STejun Heo else 1104f1af9d3aSPhilipp Hachtmann idx_b--; 1105f1af9d3aSPhilipp Hachtmann *idx = (u32)idx_a | (u64)idx_b << 32; 11067bd0b0f0STejun Heo return; 11077bd0b0f0STejun Heo } 11087bd0b0f0STejun Heo } 11097bd0b0f0STejun Heo } 1110f1af9d3aSPhilipp Hachtmann /* signal end of iteration */ 11117bd0b0f0STejun Heo *idx = ULLONG_MAX; 11127bd0b0f0STejun Heo } 11137bd0b0f0STejun Heo 11147c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 11157c0caeb8STejun Heo /* 11167c0caeb8STejun Heo * Common iterator interface used to define for_each_mem_range(). 11177c0caeb8STejun Heo */ 11187c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid, 11197c0caeb8STejun Heo unsigned long *out_start_pfn, 11207c0caeb8STejun Heo unsigned long *out_end_pfn, int *out_nid) 11217c0caeb8STejun Heo { 11227c0caeb8STejun Heo struct memblock_type *type = &memblock.memory; 11237c0caeb8STejun Heo struct memblock_region *r; 11247c0caeb8STejun Heo 11257c0caeb8STejun Heo while (++*idx < type->cnt) { 11267c0caeb8STejun Heo r = &type->regions[*idx]; 11277c0caeb8STejun Heo 11287c0caeb8STejun Heo if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) 11297c0caeb8STejun Heo continue; 11307c0caeb8STejun Heo if (nid == MAX_NUMNODES || nid == r->nid) 11317c0caeb8STejun Heo break; 11327c0caeb8STejun Heo } 11337c0caeb8STejun Heo if (*idx >= type->cnt) { 11347c0caeb8STejun Heo *idx = -1; 11357c0caeb8STejun Heo return; 11367c0caeb8STejun Heo } 11377c0caeb8STejun Heo 11387c0caeb8STejun Heo if (out_start_pfn) 11397c0caeb8STejun Heo *out_start_pfn = PFN_UP(r->base); 11407c0caeb8STejun Heo if (out_end_pfn) 11417c0caeb8STejun Heo *out_end_pfn = PFN_DOWN(r->base + r->size); 11427c0caeb8STejun Heo if (out_nid) 11437c0caeb8STejun Heo *out_nid = r->nid; 11447c0caeb8STejun Heo } 11457c0caeb8STejun Heo 11467c0caeb8STejun Heo /** 11477c0caeb8STejun Heo * memblock_set_node - set node ID on memblock regions 11487c0caeb8STejun Heo * @base: base of area to set node ID for 11497c0caeb8STejun Heo * @size: size of area to set node ID for 1150e7e8de59STang Chen * @type: memblock type to set node ID for 11517c0caeb8STejun Heo * @nid: node ID to set 11527c0caeb8STejun Heo * 1153e7e8de59STang Chen * Set the nid of memblock @type regions in [@base, @base + @size) to @nid. 11547c0caeb8STejun Heo * Regions which cross the area boundaries are split as necessary. 11557c0caeb8STejun Heo * 115647cec443SMike Rapoport * Return: 11577c0caeb8STejun Heo * 0 on success, -errno on failure. 11587c0caeb8STejun Heo */ 11597c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, 1160e7e8de59STang Chen struct memblock_type *type, int nid) 11617c0caeb8STejun Heo { 11626a9ceb31STejun Heo int start_rgn, end_rgn; 11636a9ceb31STejun Heo int i, ret; 11647c0caeb8STejun Heo 11656a9ceb31STejun Heo ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 11666a9ceb31STejun Heo if (ret) 11676a9ceb31STejun Heo return ret; 11687c0caeb8STejun Heo 11696a9ceb31STejun Heo for (i = start_rgn; i < end_rgn; i++) 1170e9d24ad3SWanpeng Li memblock_set_region_node(&type->regions[i], nid); 11717c0caeb8STejun Heo 11727c0caeb8STejun Heo memblock_merge_regions(type); 11737c0caeb8STejun Heo return 0; 11747c0caeb8STejun Heo } 11757c0caeb8STejun Heo #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ 11767c0caeb8STejun Heo 11772bfc2862SAkinobu Mita static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, 11782bfc2862SAkinobu Mita phys_addr_t align, phys_addr_t start, 1179e1720feeSMike Rapoport phys_addr_t end, int nid, 1180e1720feeSMike Rapoport enum memblock_flags flags) 118195f72d1eSYinghai Lu { 11826ed311b2SBenjamin Herrenschmidt phys_addr_t found; 118395f72d1eSYinghai Lu 118479f40fabSGrygorii Strashko if (!align) 118579f40fabSGrygorii Strashko align = SMP_CACHE_BYTES; 118694f3d3afSVineet Gupta 1187fc6daaf9STony Luck found = memblock_find_in_range_node(size, align, start, end, nid, 1188fc6daaf9STony Luck flags); 1189aedf95eaSCatalin Marinas if (found && !memblock_reserve(found, size)) { 1190aedf95eaSCatalin Marinas /* 1191aedf95eaSCatalin Marinas * The min_count is set to 0 so that memblock allocations are 1192aedf95eaSCatalin Marinas * never reported as leaks. 1193aedf95eaSCatalin Marinas */ 11949099daedSCatalin Marinas kmemleak_alloc_phys(found, size, 0, 0); 11956ed311b2SBenjamin Herrenschmidt return found; 1196aedf95eaSCatalin Marinas } 11976ed311b2SBenjamin Herrenschmidt return 0; 119895f72d1eSYinghai Lu } 119995f72d1eSYinghai Lu 12002bfc2862SAkinobu Mita phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align, 1201fc6daaf9STony Luck phys_addr_t start, phys_addr_t end, 1202e1720feeSMike Rapoport enum memblock_flags flags) 12032bfc2862SAkinobu Mita { 1204fc6daaf9STony Luck return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE, 1205fc6daaf9STony Luck flags); 12062bfc2862SAkinobu Mita } 12072bfc2862SAkinobu Mita 1208b575454fSNicholas Piggin phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size, 12092bfc2862SAkinobu Mita phys_addr_t align, phys_addr_t max_addr, 1210e1720feeSMike Rapoport int nid, enum memblock_flags flags) 12112bfc2862SAkinobu Mita { 1212fc6daaf9STony Luck return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags); 12132bfc2862SAkinobu Mita } 12142bfc2862SAkinobu Mita 12157bd0b0f0STejun Heo phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid) 12167bd0b0f0STejun Heo { 1217e1720feeSMike Rapoport enum memblock_flags flags = choose_memblock_flags(); 1218a3f5bafcSTony Luck phys_addr_t ret; 1219a3f5bafcSTony Luck 1220a3f5bafcSTony Luck again: 1221a3f5bafcSTony Luck ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, 1222a3f5bafcSTony Luck nid, flags); 1223a3f5bafcSTony Luck 1224a3f5bafcSTony Luck if (!ret && (flags & MEMBLOCK_MIRROR)) { 1225a3f5bafcSTony Luck flags &= ~MEMBLOCK_MIRROR; 1226a3f5bafcSTony Luck goto again; 1227a3f5bafcSTony Luck } 1228a3f5bafcSTony Luck return ret; 12297bd0b0f0STejun Heo } 12307bd0b0f0STejun Heo 12317bd0b0f0STejun Heo phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) 12327bd0b0f0STejun Heo { 1233fc6daaf9STony Luck return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE, 1234fc6daaf9STony Luck MEMBLOCK_NONE); 12357bd0b0f0STejun Heo } 12367bd0b0f0STejun Heo 12376ed311b2SBenjamin Herrenschmidt phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) 123895f72d1eSYinghai Lu { 12396ed311b2SBenjamin Herrenschmidt phys_addr_t alloc; 12406ed311b2SBenjamin Herrenschmidt 12416ed311b2SBenjamin Herrenschmidt alloc = __memblock_alloc_base(size, align, max_addr); 12426ed311b2SBenjamin Herrenschmidt 12436ed311b2SBenjamin Herrenschmidt if (alloc == 0) 12445d63f81cSMiles Chen panic("ERROR: Failed to allocate %pa bytes below %pa.\n", 12455d63f81cSMiles Chen &size, &max_addr); 12466ed311b2SBenjamin Herrenschmidt 12476ed311b2SBenjamin Herrenschmidt return alloc; 124895f72d1eSYinghai Lu } 124995f72d1eSYinghai Lu 12506ed311b2SBenjamin Herrenschmidt phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align) 125195f72d1eSYinghai Lu { 12526ed311b2SBenjamin Herrenschmidt return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); 125395f72d1eSYinghai Lu } 125495f72d1eSYinghai Lu 12559d1e2492SBenjamin Herrenschmidt phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) 12569d1e2492SBenjamin Herrenschmidt { 12579d1e2492SBenjamin Herrenschmidt phys_addr_t res = memblock_alloc_nid(size, align, nid); 12589d1e2492SBenjamin Herrenschmidt 12599d1e2492SBenjamin Herrenschmidt if (res) 12609d1e2492SBenjamin Herrenschmidt return res; 126115fb0972STejun Heo return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); 126295f72d1eSYinghai Lu } 126395f72d1eSYinghai Lu 126426f09e9bSSantosh Shilimkar /** 126526f09e9bSSantosh Shilimkar * memblock_virt_alloc_internal - allocate boot memory block 126626f09e9bSSantosh Shilimkar * @size: size of memory block to be allocated in bytes 126726f09e9bSSantosh Shilimkar * @align: alignment of the region and block's size 126826f09e9bSSantosh Shilimkar * @min_addr: the lower bound of the memory region to allocate (phys address) 126926f09e9bSSantosh Shilimkar * @max_addr: the upper bound of the memory region to allocate (phys address) 127026f09e9bSSantosh Shilimkar * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 127126f09e9bSSantosh Shilimkar * 127226f09e9bSSantosh Shilimkar * The @min_addr limit is dropped if it can not be satisfied and the allocation 127326f09e9bSSantosh Shilimkar * will fall back to memory below @min_addr. Also, allocation may fall back 127426f09e9bSSantosh Shilimkar * to any node in the system if the specified node can not 127526f09e9bSSantosh Shilimkar * hold the requested memory. 127626f09e9bSSantosh Shilimkar * 127726f09e9bSSantosh Shilimkar * The allocation is performed from memory region limited by 127826f09e9bSSantosh Shilimkar * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE. 127926f09e9bSSantosh Shilimkar * 128047cec443SMike Rapoport * The memory block is aligned on %SMP_CACHE_BYTES if @align == 0. 128126f09e9bSSantosh Shilimkar * 128226f09e9bSSantosh Shilimkar * The phys address of allocated boot memory block is converted to virtual and 128326f09e9bSSantosh Shilimkar * allocated memory is reset to 0. 128426f09e9bSSantosh Shilimkar * 128526f09e9bSSantosh Shilimkar * In addition, function sets the min_count to 0 using kmemleak_alloc for 128626f09e9bSSantosh Shilimkar * allocated boot memory block, so that it is never reported as leaks. 128726f09e9bSSantosh Shilimkar * 128847cec443SMike Rapoport * Return: 128926f09e9bSSantosh Shilimkar * Virtual address of allocated memory block on success, NULL on failure. 129026f09e9bSSantosh Shilimkar */ 129126f09e9bSSantosh Shilimkar static void * __init memblock_virt_alloc_internal( 129226f09e9bSSantosh Shilimkar phys_addr_t size, phys_addr_t align, 129326f09e9bSSantosh Shilimkar phys_addr_t min_addr, phys_addr_t max_addr, 129426f09e9bSSantosh Shilimkar int nid) 129526f09e9bSSantosh Shilimkar { 129626f09e9bSSantosh Shilimkar phys_addr_t alloc; 129726f09e9bSSantosh Shilimkar void *ptr; 1298e1720feeSMike Rapoport enum memblock_flags flags = choose_memblock_flags(); 129926f09e9bSSantosh Shilimkar 1300560dca27SGrygorii Strashko if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) 1301560dca27SGrygorii Strashko nid = NUMA_NO_NODE; 130226f09e9bSSantosh Shilimkar 130326f09e9bSSantosh Shilimkar /* 130426f09e9bSSantosh Shilimkar * Detect any accidental use of these APIs after slab is ready, as at 130526f09e9bSSantosh Shilimkar * this moment memblock may be deinitialized already and its 130626f09e9bSSantosh Shilimkar * internal data may be destroyed (after execution of free_all_bootmem) 130726f09e9bSSantosh Shilimkar */ 130826f09e9bSSantosh Shilimkar if (WARN_ON_ONCE(slab_is_available())) 130926f09e9bSSantosh Shilimkar return kzalloc_node(size, GFP_NOWAIT, nid); 131026f09e9bSSantosh Shilimkar 131126f09e9bSSantosh Shilimkar if (!align) 131226f09e9bSSantosh Shilimkar align = SMP_CACHE_BYTES; 131326f09e9bSSantosh Shilimkar 1314f544e14fSYinghai Lu if (max_addr > memblock.current_limit) 1315f544e14fSYinghai Lu max_addr = memblock.current_limit; 131626f09e9bSSantosh Shilimkar again: 131726f09e9bSSantosh Shilimkar alloc = memblock_find_in_range_node(size, align, min_addr, max_addr, 1318a3f5bafcSTony Luck nid, flags); 13197d41c03eSWei Yang if (alloc && !memblock_reserve(alloc, size)) 132026f09e9bSSantosh Shilimkar goto done; 132126f09e9bSSantosh Shilimkar 132226f09e9bSSantosh Shilimkar if (nid != NUMA_NO_NODE) { 132326f09e9bSSantosh Shilimkar alloc = memblock_find_in_range_node(size, align, min_addr, 1324fc6daaf9STony Luck max_addr, NUMA_NO_NODE, 1325a3f5bafcSTony Luck flags); 13267d41c03eSWei Yang if (alloc && !memblock_reserve(alloc, size)) 132726f09e9bSSantosh Shilimkar goto done; 132826f09e9bSSantosh Shilimkar } 132926f09e9bSSantosh Shilimkar 133026f09e9bSSantosh Shilimkar if (min_addr) { 133126f09e9bSSantosh Shilimkar min_addr = 0; 133226f09e9bSSantosh Shilimkar goto again; 133326f09e9bSSantosh Shilimkar } 133426f09e9bSSantosh Shilimkar 1335a3f5bafcSTony Luck if (flags & MEMBLOCK_MIRROR) { 1336a3f5bafcSTony Luck flags &= ~MEMBLOCK_MIRROR; 1337a3f5bafcSTony Luck pr_warn("Could not allocate %pap bytes of mirrored memory\n", 1338a3f5bafcSTony Luck &size); 1339a3f5bafcSTony Luck goto again; 1340a3f5bafcSTony Luck } 1341a3f5bafcSTony Luck 1342a3f5bafcSTony Luck return NULL; 134326f09e9bSSantosh Shilimkar done: 134426f09e9bSSantosh Shilimkar ptr = phys_to_virt(alloc); 134526f09e9bSSantosh Shilimkar 134626f09e9bSSantosh Shilimkar /* 134726f09e9bSSantosh Shilimkar * The min_count is set to 0 so that bootmem allocated blocks 134826f09e9bSSantosh Shilimkar * are never reported as leaks. This is because many of these blocks 134926f09e9bSSantosh Shilimkar * are only referred via the physical address which is not 135026f09e9bSSantosh Shilimkar * looked up by kmemleak. 135126f09e9bSSantosh Shilimkar */ 135226f09e9bSSantosh Shilimkar kmemleak_alloc(ptr, size, 0, 0); 135326f09e9bSSantosh Shilimkar 135426f09e9bSSantosh Shilimkar return ptr; 135526f09e9bSSantosh Shilimkar } 135626f09e9bSSantosh Shilimkar 135726f09e9bSSantosh Shilimkar /** 1358ea1f5f37SPavel Tatashin * memblock_virt_alloc_try_nid_raw - allocate boot memory block without zeroing 1359ea1f5f37SPavel Tatashin * memory and without panicking 1360ea1f5f37SPavel Tatashin * @size: size of memory block to be allocated in bytes 1361ea1f5f37SPavel Tatashin * @align: alignment of the region and block's size 1362ea1f5f37SPavel Tatashin * @min_addr: the lower bound of the memory region from where the allocation 1363ea1f5f37SPavel Tatashin * is preferred (phys address) 1364ea1f5f37SPavel Tatashin * @max_addr: the upper bound of the memory region from where the allocation 1365ea1f5f37SPavel Tatashin * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to 1366ea1f5f37SPavel Tatashin * allocate only from memory limited by memblock.current_limit value 1367ea1f5f37SPavel Tatashin * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1368ea1f5f37SPavel Tatashin * 1369ea1f5f37SPavel Tatashin * Public function, provides additional debug information (including caller 1370ea1f5f37SPavel Tatashin * info), if enabled. Does not zero allocated memory, does not panic if request 1371ea1f5f37SPavel Tatashin * cannot be satisfied. 1372ea1f5f37SPavel Tatashin * 137347cec443SMike Rapoport * Return: 1374ea1f5f37SPavel Tatashin * Virtual address of allocated memory block on success, NULL on failure. 1375ea1f5f37SPavel Tatashin */ 1376ea1f5f37SPavel Tatashin void * __init memblock_virt_alloc_try_nid_raw( 1377ea1f5f37SPavel Tatashin phys_addr_t size, phys_addr_t align, 1378ea1f5f37SPavel Tatashin phys_addr_t min_addr, phys_addr_t max_addr, 1379ea1f5f37SPavel Tatashin int nid) 1380ea1f5f37SPavel Tatashin { 1381ea1f5f37SPavel Tatashin void *ptr; 1382ea1f5f37SPavel Tatashin 1383ea1f5f37SPavel Tatashin memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n", 1384ea1f5f37SPavel Tatashin __func__, (u64)size, (u64)align, nid, (u64)min_addr, 1385ea1f5f37SPavel Tatashin (u64)max_addr, (void *)_RET_IP_); 1386ea1f5f37SPavel Tatashin 1387ea1f5f37SPavel Tatashin ptr = memblock_virt_alloc_internal(size, align, 1388ea1f5f37SPavel Tatashin min_addr, max_addr, nid); 1389ea1f5f37SPavel Tatashin #ifdef CONFIG_DEBUG_VM 1390ea1f5f37SPavel Tatashin if (ptr && size > 0) 1391f165b378SPavel Tatashin memset(ptr, PAGE_POISON_PATTERN, size); 1392ea1f5f37SPavel Tatashin #endif 1393ea1f5f37SPavel Tatashin return ptr; 1394ea1f5f37SPavel Tatashin } 1395ea1f5f37SPavel Tatashin 1396ea1f5f37SPavel Tatashin /** 139726f09e9bSSantosh Shilimkar * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block 139826f09e9bSSantosh Shilimkar * @size: size of memory block to be allocated in bytes 139926f09e9bSSantosh Shilimkar * @align: alignment of the region and block's size 140026f09e9bSSantosh Shilimkar * @min_addr: the lower bound of the memory region from where the allocation 140126f09e9bSSantosh Shilimkar * is preferred (phys address) 140226f09e9bSSantosh Shilimkar * @max_addr: the upper bound of the memory region from where the allocation 140326f09e9bSSantosh Shilimkar * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to 140426f09e9bSSantosh Shilimkar * allocate only from memory limited by memblock.current_limit value 140526f09e9bSSantosh Shilimkar * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 140626f09e9bSSantosh Shilimkar * 1407ea1f5f37SPavel Tatashin * Public function, provides additional debug information (including caller 1408ea1f5f37SPavel Tatashin * info), if enabled. This function zeroes the allocated memory. 140926f09e9bSSantosh Shilimkar * 141047cec443SMike Rapoport * Return: 141126f09e9bSSantosh Shilimkar * Virtual address of allocated memory block on success, NULL on failure. 141226f09e9bSSantosh Shilimkar */ 141326f09e9bSSantosh Shilimkar void * __init memblock_virt_alloc_try_nid_nopanic( 141426f09e9bSSantosh Shilimkar phys_addr_t size, phys_addr_t align, 141526f09e9bSSantosh Shilimkar phys_addr_t min_addr, phys_addr_t max_addr, 141626f09e9bSSantosh Shilimkar int nid) 141726f09e9bSSantosh Shilimkar { 1418ea1f5f37SPavel Tatashin void *ptr; 1419ea1f5f37SPavel Tatashin 142026f09e9bSSantosh Shilimkar memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n", 142126f09e9bSSantosh Shilimkar __func__, (u64)size, (u64)align, nid, (u64)min_addr, 142226f09e9bSSantosh Shilimkar (u64)max_addr, (void *)_RET_IP_); 1423ea1f5f37SPavel Tatashin 1424ea1f5f37SPavel Tatashin ptr = memblock_virt_alloc_internal(size, align, 1425ea1f5f37SPavel Tatashin min_addr, max_addr, nid); 1426ea1f5f37SPavel Tatashin if (ptr) 1427ea1f5f37SPavel Tatashin memset(ptr, 0, size); 1428ea1f5f37SPavel Tatashin return ptr; 142926f09e9bSSantosh Shilimkar } 143026f09e9bSSantosh Shilimkar 143126f09e9bSSantosh Shilimkar /** 143226f09e9bSSantosh Shilimkar * memblock_virt_alloc_try_nid - allocate boot memory block with panicking 143326f09e9bSSantosh Shilimkar * @size: size of memory block to be allocated in bytes 143426f09e9bSSantosh Shilimkar * @align: alignment of the region and block's size 143526f09e9bSSantosh Shilimkar * @min_addr: the lower bound of the memory region from where the allocation 143626f09e9bSSantosh Shilimkar * is preferred (phys address) 143726f09e9bSSantosh Shilimkar * @max_addr: the upper bound of the memory region from where the allocation 143826f09e9bSSantosh Shilimkar * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to 143926f09e9bSSantosh Shilimkar * allocate only from memory limited by memblock.current_limit value 144026f09e9bSSantosh Shilimkar * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 144126f09e9bSSantosh Shilimkar * 1442ea1f5f37SPavel Tatashin * Public panicking version of memblock_virt_alloc_try_nid_nopanic() 144326f09e9bSSantosh Shilimkar * which provides debug information (including caller info), if enabled, 144426f09e9bSSantosh Shilimkar * and panics if the request can not be satisfied. 144526f09e9bSSantosh Shilimkar * 144647cec443SMike Rapoport * Return: 144726f09e9bSSantosh Shilimkar * Virtual address of allocated memory block on success, NULL on failure. 144826f09e9bSSantosh Shilimkar */ 144926f09e9bSSantosh Shilimkar void * __init memblock_virt_alloc_try_nid( 145026f09e9bSSantosh Shilimkar phys_addr_t size, phys_addr_t align, 145126f09e9bSSantosh Shilimkar phys_addr_t min_addr, phys_addr_t max_addr, 145226f09e9bSSantosh Shilimkar int nid) 145326f09e9bSSantosh Shilimkar { 145426f09e9bSSantosh Shilimkar void *ptr; 145526f09e9bSSantosh Shilimkar 145626f09e9bSSantosh Shilimkar memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n", 145726f09e9bSSantosh Shilimkar __func__, (u64)size, (u64)align, nid, (u64)min_addr, 145826f09e9bSSantosh Shilimkar (u64)max_addr, (void *)_RET_IP_); 145926f09e9bSSantosh Shilimkar ptr = memblock_virt_alloc_internal(size, align, 146026f09e9bSSantosh Shilimkar min_addr, max_addr, nid); 1461ea1f5f37SPavel Tatashin if (ptr) { 1462ea1f5f37SPavel Tatashin memset(ptr, 0, size); 146326f09e9bSSantosh Shilimkar return ptr; 1464ea1f5f37SPavel Tatashin } 146526f09e9bSSantosh Shilimkar 146626f09e9bSSantosh Shilimkar panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx\n", 146726f09e9bSSantosh Shilimkar __func__, (u64)size, (u64)align, nid, (u64)min_addr, 146826f09e9bSSantosh Shilimkar (u64)max_addr); 146926f09e9bSSantosh Shilimkar return NULL; 147026f09e9bSSantosh Shilimkar } 147126f09e9bSSantosh Shilimkar 147226f09e9bSSantosh Shilimkar /** 147326f09e9bSSantosh Shilimkar * __memblock_free_early - free boot memory block 147426f09e9bSSantosh Shilimkar * @base: phys starting address of the boot memory block 147526f09e9bSSantosh Shilimkar * @size: size of the boot memory block in bytes 147626f09e9bSSantosh Shilimkar * 147726f09e9bSSantosh Shilimkar * Free boot memory block previously allocated by memblock_virt_alloc_xx() API. 147826f09e9bSSantosh Shilimkar * The freeing memory will not be released to the buddy allocator. 147926f09e9bSSantosh Shilimkar */ 148026f09e9bSSantosh Shilimkar void __init __memblock_free_early(phys_addr_t base, phys_addr_t size) 148126f09e9bSSantosh Shilimkar { 148226f09e9bSSantosh Shilimkar memblock_dbg("%s: [%#016llx-%#016llx] %pF\n", 148326f09e9bSSantosh Shilimkar __func__, (u64)base, (u64)base + size - 1, 148426f09e9bSSantosh Shilimkar (void *)_RET_IP_); 14859099daedSCatalin Marinas kmemleak_free_part_phys(base, size); 1486f1af9d3aSPhilipp Hachtmann memblock_remove_range(&memblock.reserved, base, size); 148726f09e9bSSantosh Shilimkar } 148826f09e9bSSantosh Shilimkar 1489*48a833ccSMike Rapoport /** 149026f09e9bSSantosh Shilimkar * __memblock_free_late - free bootmem block pages directly to buddy allocator 1491*48a833ccSMike Rapoport * @base: phys starting address of the boot memory block 149226f09e9bSSantosh Shilimkar * @size: size of the boot memory block in bytes 149326f09e9bSSantosh Shilimkar * 149426f09e9bSSantosh Shilimkar * This is only useful when the bootmem allocator has already been torn 149526f09e9bSSantosh Shilimkar * down, but we are still initializing the system. Pages are released directly 149626f09e9bSSantosh Shilimkar * to the buddy allocator, no bootmem metadata is updated because it is gone. 149726f09e9bSSantosh Shilimkar */ 149826f09e9bSSantosh Shilimkar void __init __memblock_free_late(phys_addr_t base, phys_addr_t size) 149926f09e9bSSantosh Shilimkar { 150026f09e9bSSantosh Shilimkar u64 cursor, end; 150126f09e9bSSantosh Shilimkar 150226f09e9bSSantosh Shilimkar memblock_dbg("%s: [%#016llx-%#016llx] %pF\n", 150326f09e9bSSantosh Shilimkar __func__, (u64)base, (u64)base + size - 1, 150426f09e9bSSantosh Shilimkar (void *)_RET_IP_); 15059099daedSCatalin Marinas kmemleak_free_part_phys(base, size); 150626f09e9bSSantosh Shilimkar cursor = PFN_UP(base); 150726f09e9bSSantosh Shilimkar end = PFN_DOWN(base + size); 150826f09e9bSSantosh Shilimkar 150926f09e9bSSantosh Shilimkar for (; cursor < end; cursor++) { 1510d70ddd7aSMel Gorman __free_pages_bootmem(pfn_to_page(cursor), cursor, 0); 151126f09e9bSSantosh Shilimkar totalram_pages++; 151226f09e9bSSantosh Shilimkar } 151326f09e9bSSantosh Shilimkar } 15149d1e2492SBenjamin Herrenschmidt 15159d1e2492SBenjamin Herrenschmidt /* 15169d1e2492SBenjamin Herrenschmidt * Remaining API functions 15179d1e2492SBenjamin Herrenschmidt */ 15189d1e2492SBenjamin Herrenschmidt 15191f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void) 152095f72d1eSYinghai Lu { 15211440c4e2STejun Heo return memblock.memory.total_size; 152295f72d1eSYinghai Lu } 152395f72d1eSYinghai Lu 15248907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void) 15258907de5dSSrikar Dronamraju { 15268907de5dSSrikar Dronamraju return memblock.reserved.total_size; 15278907de5dSSrikar Dronamraju } 15288907de5dSSrikar Dronamraju 1529595ad9afSYinghai Lu phys_addr_t __init memblock_mem_size(unsigned long limit_pfn) 1530595ad9afSYinghai Lu { 1531595ad9afSYinghai Lu unsigned long pages = 0; 1532595ad9afSYinghai Lu struct memblock_region *r; 1533595ad9afSYinghai Lu unsigned long start_pfn, end_pfn; 1534595ad9afSYinghai Lu 1535595ad9afSYinghai Lu for_each_memblock(memory, r) { 1536595ad9afSYinghai Lu start_pfn = memblock_region_memory_base_pfn(r); 1537595ad9afSYinghai Lu end_pfn = memblock_region_memory_end_pfn(r); 1538595ad9afSYinghai Lu start_pfn = min_t(unsigned long, start_pfn, limit_pfn); 1539595ad9afSYinghai Lu end_pfn = min_t(unsigned long, end_pfn, limit_pfn); 1540595ad9afSYinghai Lu pages += end_pfn - start_pfn; 1541595ad9afSYinghai Lu } 1542595ad9afSYinghai Lu 154316763230SFabian Frederick return PFN_PHYS(pages); 1544595ad9afSYinghai Lu } 1545595ad9afSYinghai Lu 15460a93ebefSSam Ravnborg /* lowest address */ 15470a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void) 15480a93ebefSSam Ravnborg { 15490a93ebefSSam Ravnborg return memblock.memory.regions[0].base; 15500a93ebefSSam Ravnborg } 15510a93ebefSSam Ravnborg 155210d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void) 155395f72d1eSYinghai Lu { 155495f72d1eSYinghai Lu int idx = memblock.memory.cnt - 1; 155595f72d1eSYinghai Lu 1556e3239ff9SBenjamin Herrenschmidt return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); 155795f72d1eSYinghai Lu } 155895f72d1eSYinghai Lu 1559a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit) 156095f72d1eSYinghai Lu { 15611c4bc43dSStefan Agner phys_addr_t max_addr = PHYS_ADDR_MAX; 1562136199f0SEmil Medve struct memblock_region *r; 156395f72d1eSYinghai Lu 1564a571d4ebSDennis Chen /* 1565a571d4ebSDennis Chen * translate the memory @limit size into the max address within one of 1566a571d4ebSDennis Chen * the memory memblock regions, if the @limit exceeds the total size 15671c4bc43dSStefan Agner * of those regions, max_addr will keep original value PHYS_ADDR_MAX 1568a571d4ebSDennis Chen */ 1569136199f0SEmil Medve for_each_memblock(memory, r) { 1570c0ce8fefSTejun Heo if (limit <= r->size) { 1571c0ce8fefSTejun Heo max_addr = r->base + limit; 157295f72d1eSYinghai Lu break; 157395f72d1eSYinghai Lu } 1574c0ce8fefSTejun Heo limit -= r->size; 157595f72d1eSYinghai Lu } 1576c0ce8fefSTejun Heo 1577a571d4ebSDennis Chen return max_addr; 1578a571d4ebSDennis Chen } 1579a571d4ebSDennis Chen 1580a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit) 1581a571d4ebSDennis Chen { 15821c4bc43dSStefan Agner phys_addr_t max_addr = PHYS_ADDR_MAX; 1583a571d4ebSDennis Chen 1584a571d4ebSDennis Chen if (!limit) 1585a571d4ebSDennis Chen return; 1586a571d4ebSDennis Chen 1587a571d4ebSDennis Chen max_addr = __find_max_addr(limit); 1588a571d4ebSDennis Chen 1589a571d4ebSDennis Chen /* @limit exceeds the total size of the memory, do nothing */ 15901c4bc43dSStefan Agner if (max_addr == PHYS_ADDR_MAX) 1591a571d4ebSDennis Chen return; 1592a571d4ebSDennis Chen 1593c0ce8fefSTejun Heo /* truncate both memory and reserved regions */ 1594f1af9d3aSPhilipp Hachtmann memblock_remove_range(&memblock.memory, max_addr, 15951c4bc43dSStefan Agner PHYS_ADDR_MAX); 1596f1af9d3aSPhilipp Hachtmann memblock_remove_range(&memblock.reserved, max_addr, 15971c4bc43dSStefan Agner PHYS_ADDR_MAX); 159895f72d1eSYinghai Lu } 159995f72d1eSYinghai Lu 1600c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size) 1601c9ca9b4eSAKASHI Takahiro { 1602c9ca9b4eSAKASHI Takahiro int start_rgn, end_rgn; 1603c9ca9b4eSAKASHI Takahiro int i, ret; 1604c9ca9b4eSAKASHI Takahiro 1605c9ca9b4eSAKASHI Takahiro if (!size) 1606c9ca9b4eSAKASHI Takahiro return; 1607c9ca9b4eSAKASHI Takahiro 1608c9ca9b4eSAKASHI Takahiro ret = memblock_isolate_range(&memblock.memory, base, size, 1609c9ca9b4eSAKASHI Takahiro &start_rgn, &end_rgn); 1610c9ca9b4eSAKASHI Takahiro if (ret) 1611c9ca9b4eSAKASHI Takahiro return; 1612c9ca9b4eSAKASHI Takahiro 1613c9ca9b4eSAKASHI Takahiro /* remove all the MAP regions */ 1614c9ca9b4eSAKASHI Takahiro for (i = memblock.memory.cnt - 1; i >= end_rgn; i--) 1615c9ca9b4eSAKASHI Takahiro if (!memblock_is_nomap(&memblock.memory.regions[i])) 1616c9ca9b4eSAKASHI Takahiro memblock_remove_region(&memblock.memory, i); 1617c9ca9b4eSAKASHI Takahiro 1618c9ca9b4eSAKASHI Takahiro for (i = start_rgn - 1; i >= 0; i--) 1619c9ca9b4eSAKASHI Takahiro if (!memblock_is_nomap(&memblock.memory.regions[i])) 1620c9ca9b4eSAKASHI Takahiro memblock_remove_region(&memblock.memory, i); 1621c9ca9b4eSAKASHI Takahiro 1622c9ca9b4eSAKASHI Takahiro /* truncate the reserved regions */ 1623c9ca9b4eSAKASHI Takahiro memblock_remove_range(&memblock.reserved, 0, base); 1624c9ca9b4eSAKASHI Takahiro memblock_remove_range(&memblock.reserved, 16251c4bc43dSStefan Agner base + size, PHYS_ADDR_MAX); 1626c9ca9b4eSAKASHI Takahiro } 1627c9ca9b4eSAKASHI Takahiro 1628a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit) 1629a571d4ebSDennis Chen { 1630a571d4ebSDennis Chen phys_addr_t max_addr; 1631a571d4ebSDennis Chen 1632a571d4ebSDennis Chen if (!limit) 1633a571d4ebSDennis Chen return; 1634a571d4ebSDennis Chen 1635a571d4ebSDennis Chen max_addr = __find_max_addr(limit); 1636a571d4ebSDennis Chen 1637a571d4ebSDennis Chen /* @limit exceeds the total size of the memory, do nothing */ 16381c4bc43dSStefan Agner if (max_addr == PHYS_ADDR_MAX) 1639a571d4ebSDennis Chen return; 1640a571d4ebSDennis Chen 1641c9ca9b4eSAKASHI Takahiro memblock_cap_memory_range(0, max_addr); 1642a571d4ebSDennis Chen } 1643a571d4ebSDennis Chen 1644cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr) 164572d4b0b4SBenjamin Herrenschmidt { 164672d4b0b4SBenjamin Herrenschmidt unsigned int left = 0, right = type->cnt; 164772d4b0b4SBenjamin Herrenschmidt 164872d4b0b4SBenjamin Herrenschmidt do { 164972d4b0b4SBenjamin Herrenschmidt unsigned int mid = (right + left) / 2; 165072d4b0b4SBenjamin Herrenschmidt 165172d4b0b4SBenjamin Herrenschmidt if (addr < type->regions[mid].base) 165272d4b0b4SBenjamin Herrenschmidt right = mid; 165372d4b0b4SBenjamin Herrenschmidt else if (addr >= (type->regions[mid].base + 165472d4b0b4SBenjamin Herrenschmidt type->regions[mid].size)) 165572d4b0b4SBenjamin Herrenschmidt left = mid + 1; 165672d4b0b4SBenjamin Herrenschmidt else 165772d4b0b4SBenjamin Herrenschmidt return mid; 165872d4b0b4SBenjamin Herrenschmidt } while (left < right); 165972d4b0b4SBenjamin Herrenschmidt return -1; 166072d4b0b4SBenjamin Herrenschmidt } 166172d4b0b4SBenjamin Herrenschmidt 1662b4ad0c7eSYaowei Bai bool __init memblock_is_reserved(phys_addr_t addr) 166395f72d1eSYinghai Lu { 166472d4b0b4SBenjamin Herrenschmidt return memblock_search(&memblock.reserved, addr) != -1; 166595f72d1eSYinghai Lu } 166672d4b0b4SBenjamin Herrenschmidt 1667b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr) 166872d4b0b4SBenjamin Herrenschmidt { 166972d4b0b4SBenjamin Herrenschmidt return memblock_search(&memblock.memory, addr) != -1; 167072d4b0b4SBenjamin Herrenschmidt } 167172d4b0b4SBenjamin Herrenschmidt 1672937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr) 1673bf3d3cc5SArd Biesheuvel { 1674bf3d3cc5SArd Biesheuvel int i = memblock_search(&memblock.memory, addr); 1675bf3d3cc5SArd Biesheuvel 1676bf3d3cc5SArd Biesheuvel if (i == -1) 1677bf3d3cc5SArd Biesheuvel return false; 1678bf3d3cc5SArd Biesheuvel return !memblock_is_nomap(&memblock.memory.regions[i]); 1679bf3d3cc5SArd Biesheuvel } 1680bf3d3cc5SArd Biesheuvel 1681e76b63f8SYinghai Lu #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 1682e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn, 1683e76b63f8SYinghai Lu unsigned long *start_pfn, unsigned long *end_pfn) 1684e76b63f8SYinghai Lu { 1685e76b63f8SYinghai Lu struct memblock_type *type = &memblock.memory; 168616763230SFabian Frederick int mid = memblock_search(type, PFN_PHYS(pfn)); 1687e76b63f8SYinghai Lu 1688e76b63f8SYinghai Lu if (mid == -1) 1689e76b63f8SYinghai Lu return -1; 1690e76b63f8SYinghai Lu 1691f7e2f7e8SFabian Frederick *start_pfn = PFN_DOWN(type->regions[mid].base); 1692f7e2f7e8SFabian Frederick *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size); 1693e76b63f8SYinghai Lu 1694e76b63f8SYinghai Lu return type->regions[mid].nid; 1695e76b63f8SYinghai Lu } 1696e76b63f8SYinghai Lu #endif 1697e76b63f8SYinghai Lu 1698eab30949SStephen Boyd /** 1699eab30949SStephen Boyd * memblock_is_region_memory - check if a region is a subset of memory 1700eab30949SStephen Boyd * @base: base of region to check 1701eab30949SStephen Boyd * @size: size of region to check 1702eab30949SStephen Boyd * 1703eab30949SStephen Boyd * Check if the region [@base, @base + @size) is a subset of a memory block. 1704eab30949SStephen Boyd * 170547cec443SMike Rapoport * Return: 1706eab30949SStephen Boyd * 0 if false, non-zero if true 1707eab30949SStephen Boyd */ 1708937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size) 170972d4b0b4SBenjamin Herrenschmidt { 1710abb65272STomi Valkeinen int idx = memblock_search(&memblock.memory, base); 1711eb18f1b5STejun Heo phys_addr_t end = base + memblock_cap_size(base, &size); 171272d4b0b4SBenjamin Herrenschmidt 171372d4b0b4SBenjamin Herrenschmidt if (idx == -1) 1714937f0c26SYaowei Bai return false; 1715ef415ef4SWei Yang return (memblock.memory.regions[idx].base + 1716eb18f1b5STejun Heo memblock.memory.regions[idx].size) >= end; 171795f72d1eSYinghai Lu } 171895f72d1eSYinghai Lu 1719eab30949SStephen Boyd /** 1720eab30949SStephen Boyd * memblock_is_region_reserved - check if a region intersects reserved memory 1721eab30949SStephen Boyd * @base: base of region to check 1722eab30949SStephen Boyd * @size: size of region to check 1723eab30949SStephen Boyd * 172447cec443SMike Rapoport * Check if the region [@base, @base + @size) intersects a reserved 172547cec443SMike Rapoport * memory block. 1726eab30949SStephen Boyd * 172747cec443SMike Rapoport * Return: 1728c5c5c9d1STang Chen * True if they intersect, false if not. 1729eab30949SStephen Boyd */ 1730c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) 173195f72d1eSYinghai Lu { 1732eb18f1b5STejun Heo memblock_cap_size(base, &size); 1733c5c5c9d1STang Chen return memblock_overlaps_region(&memblock.reserved, base, size); 173495f72d1eSYinghai Lu } 173595f72d1eSYinghai Lu 17366ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align) 17376ede1fd3SYinghai Lu { 17386ede1fd3SYinghai Lu phys_addr_t start, end, orig_start, orig_end; 1739136199f0SEmil Medve struct memblock_region *r; 17406ede1fd3SYinghai Lu 1741136199f0SEmil Medve for_each_memblock(memory, r) { 1742136199f0SEmil Medve orig_start = r->base; 1743136199f0SEmil Medve orig_end = r->base + r->size; 17446ede1fd3SYinghai Lu start = round_up(orig_start, align); 17456ede1fd3SYinghai Lu end = round_down(orig_end, align); 17466ede1fd3SYinghai Lu 17476ede1fd3SYinghai Lu if (start == orig_start && end == orig_end) 17486ede1fd3SYinghai Lu continue; 17496ede1fd3SYinghai Lu 17506ede1fd3SYinghai Lu if (start < end) { 1751136199f0SEmil Medve r->base = start; 1752136199f0SEmil Medve r->size = end - start; 17536ede1fd3SYinghai Lu } else { 1754136199f0SEmil Medve memblock_remove_region(&memblock.memory, 1755136199f0SEmil Medve r - memblock.memory.regions); 1756136199f0SEmil Medve r--; 17576ede1fd3SYinghai Lu } 17586ede1fd3SYinghai Lu } 17596ede1fd3SYinghai Lu } 1760e63075a3SBenjamin Herrenschmidt 17613661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit) 1762e63075a3SBenjamin Herrenschmidt { 1763e63075a3SBenjamin Herrenschmidt memblock.current_limit = limit; 1764e63075a3SBenjamin Herrenschmidt } 1765e63075a3SBenjamin Herrenschmidt 1766fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void) 1767fec51014SLaura Abbott { 1768fec51014SLaura Abbott return memblock.current_limit; 1769fec51014SLaura Abbott } 1770fec51014SLaura Abbott 17710262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type) 17726ed311b2SBenjamin Herrenschmidt { 17735d63f81cSMiles Chen phys_addr_t base, end, size; 1774e1720feeSMike Rapoport enum memblock_flags flags; 17758c9c1701SAlexander Kuleshov int idx; 17768c9c1701SAlexander Kuleshov struct memblock_region *rgn; 17776ed311b2SBenjamin Herrenschmidt 17780262d9c8SHeiko Carstens pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt); 17796ed311b2SBenjamin Herrenschmidt 178066e8b438SGioh Kim for_each_memblock_type(idx, type, rgn) { 17817c0caeb8STejun Heo char nid_buf[32] = ""; 17826ed311b2SBenjamin Herrenschmidt 17837c0caeb8STejun Heo base = rgn->base; 17847c0caeb8STejun Heo size = rgn->size; 17855d63f81cSMiles Chen end = base + size - 1; 178666a20757STang Chen flags = rgn->flags; 17877c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 17887c0caeb8STejun Heo if (memblock_get_region_node(rgn) != MAX_NUMNODES) 17897c0caeb8STejun Heo snprintf(nid_buf, sizeof(nid_buf), " on node %d", 17907c0caeb8STejun Heo memblock_get_region_node(rgn)); 17917c0caeb8STejun Heo #endif 1792e1720feeSMike Rapoport pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n", 17930262d9c8SHeiko Carstens type->name, idx, &base, &end, &size, nid_buf, flags); 17946ed311b2SBenjamin Herrenschmidt } 17956ed311b2SBenjamin Herrenschmidt } 17966ed311b2SBenjamin Herrenschmidt 17974ff7b82fSTejun Heo void __init_memblock __memblock_dump_all(void) 17986ed311b2SBenjamin Herrenschmidt { 17996ed311b2SBenjamin Herrenschmidt pr_info("MEMBLOCK configuration:\n"); 18005d63f81cSMiles Chen pr_info(" memory size = %pa reserved size = %pa\n", 18015d63f81cSMiles Chen &memblock.memory.total_size, 18025d63f81cSMiles Chen &memblock.reserved.total_size); 18036ed311b2SBenjamin Herrenschmidt 18040262d9c8SHeiko Carstens memblock_dump(&memblock.memory); 18050262d9c8SHeiko Carstens memblock_dump(&memblock.reserved); 1806409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 18070262d9c8SHeiko Carstens memblock_dump(&memblock.physmem); 1808409efd4cSHeiko Carstens #endif 18096ed311b2SBenjamin Herrenschmidt } 18106ed311b2SBenjamin Herrenschmidt 18111aadc056STejun Heo void __init memblock_allow_resize(void) 18126ed311b2SBenjamin Herrenschmidt { 1813142b45a7SBenjamin Herrenschmidt memblock_can_resize = 1; 18146ed311b2SBenjamin Herrenschmidt } 18156ed311b2SBenjamin Herrenschmidt 18166ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p) 18176ed311b2SBenjamin Herrenschmidt { 18186ed311b2SBenjamin Herrenschmidt if (p && strstr(p, "debug")) 18196ed311b2SBenjamin Herrenschmidt memblock_debug = 1; 18206ed311b2SBenjamin Herrenschmidt return 0; 18216ed311b2SBenjamin Herrenschmidt } 18226ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock); 18236ed311b2SBenjamin Herrenschmidt 1824c378ddd5STejun Heo #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK) 18256d03b885SBenjamin Herrenschmidt 18266d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private) 18276d03b885SBenjamin Herrenschmidt { 18286d03b885SBenjamin Herrenschmidt struct memblock_type *type = m->private; 18296d03b885SBenjamin Herrenschmidt struct memblock_region *reg; 18306d03b885SBenjamin Herrenschmidt int i; 18315d63f81cSMiles Chen phys_addr_t end; 18326d03b885SBenjamin Herrenschmidt 18336d03b885SBenjamin Herrenschmidt for (i = 0; i < type->cnt; i++) { 18346d03b885SBenjamin Herrenschmidt reg = &type->regions[i]; 18355d63f81cSMiles Chen end = reg->base + reg->size - 1; 18366d03b885SBenjamin Herrenschmidt 18375d63f81cSMiles Chen seq_printf(m, "%4d: ", i); 18385d63f81cSMiles Chen seq_printf(m, "%pa..%pa\n", ®->base, &end); 18396d03b885SBenjamin Herrenschmidt } 18406d03b885SBenjamin Herrenschmidt return 0; 18416d03b885SBenjamin Herrenschmidt } 18425ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug); 18436d03b885SBenjamin Herrenschmidt 18446d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void) 18456d03b885SBenjamin Herrenschmidt { 18466d03b885SBenjamin Herrenschmidt struct dentry *root = debugfs_create_dir("memblock", NULL); 18476d03b885SBenjamin Herrenschmidt if (!root) 18486d03b885SBenjamin Herrenschmidt return -ENXIO; 18490825a6f9SJoe Perches debugfs_create_file("memory", 0444, root, 18500825a6f9SJoe Perches &memblock.memory, &memblock_debug_fops); 18510825a6f9SJoe Perches debugfs_create_file("reserved", 0444, root, 18520825a6f9SJoe Perches &memblock.reserved, &memblock_debug_fops); 185370210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 18540825a6f9SJoe Perches debugfs_create_file("physmem", 0444, root, 18550825a6f9SJoe Perches &memblock.physmem, &memblock_debug_fops); 185670210ed9SPhilipp Hachtmann #endif 18576d03b885SBenjamin Herrenschmidt 18586d03b885SBenjamin Herrenschmidt return 0; 18596d03b885SBenjamin Herrenschmidt } 18606d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs); 18616d03b885SBenjamin Herrenschmidt 18626d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */ 1863