blob: efe0b0775c1da514753995f6322c0b95a6c1e6a0 [file] [log] [blame]
Yinghai Lu95f72d12010-07-12 14:36:09 +10001/*
2 * Procedures for maintaining information about logical memory blocks.
3 *
4 * Peter Bergner, IBM Corp. June 2001.
5 * Copyright (C) 2001 Peter Bergner.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/kernel.h>
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070014#include <linux/slab.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100015#include <linux/init.h>
16#include <linux/bitops.h>
Benjamin Herrenschmidt449e8df2010-07-06 15:39:07 -070017#include <linux/poison.h>
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -070018#include <linux/pfn.h>
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -070019#include <linux/debugfs.h>
20#include <linux/seq_file.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100021#include <linux/memblock.h>
Shiraz Hashime1525702016-02-04 14:53:33 +053022#include <linux/preempt.h>
23#include <linux/seqlock.h>
Shiraz Hashim3eaf6e62016-04-16 11:43:02 +053024#include <linux/irqflags.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100025
Christoph Hellwigc4c5ad62016-07-28 15:48:06 -070026#include <asm/sections.h>
Santosh Shilimkar26f09e92014-01-21 15:50:19 -080027#include <linux/io.h>
28
29#include "internal.h"
Tang Chen79442ed2013-11-12 15:07:59 -080030
Tejun Heofe091c22011-12-08 10:22:07 -080031static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
32static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010033#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
34static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
35#endif
Tejun Heofe091c22011-12-08 10:22:07 -080036
Shiraz Hashime1525702016-02-04 14:53:33 +053037static seqcount_t memblock_seq;
Tejun Heofe091c22011-12-08 10:22:07 -080038struct memblock memblock __initdata_memblock = {
39 .memory.regions = memblock_memory_init_regions,
40 .memory.cnt = 1, /* empty dummy entry */
41 .memory.max = INIT_MEMBLOCK_REGIONS,
42
43 .reserved.regions = memblock_reserved_init_regions,
44 .reserved.cnt = 1, /* empty dummy entry */
45 .reserved.max = INIT_MEMBLOCK_REGIONS,
46
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010047#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
48 .physmem.regions = memblock_physmem_init_regions,
49 .physmem.cnt = 1, /* empty dummy entry */
50 .physmem.max = INIT_PHYSMEM_REGIONS,
51#endif
52
Tang Chen79442ed2013-11-12 15:07:59 -080053 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -080054 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
55};
Yinghai Lu95f72d12010-07-12 14:36:09 +100056
Yinghai Lu10d06432010-07-28 15:43:02 +100057int memblock_debug __initdata_memblock;
Tang Chen55ac5902014-01-21 15:49:35 -080058#ifdef CONFIG_MOVABLE_NODE
59bool movable_node_enabled __initdata_memblock = false;
60#endif
Tony Lucka3f5baf2015-06-24 16:58:12 -070061static bool system_has_some_mirror __initdata_memblock = false;
Tejun Heo1aadc052011-12-08 10:22:08 -080062static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -070063static int memblock_memory_in_slab __initdata_memblock = 0;
64static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +100065
Tony Lucka3f5baf2015-06-24 16:58:12 -070066ulong __init_memblock choose_memblock_flags(void)
67{
68 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
69}
70
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070071/* inline so we don't get a warning when pr_debug is compiled out */
Raghavendra D Prabhuc2233112012-10-08 16:33:55 -070072static __init_memblock const char *
73memblock_type_name(struct memblock_type *type)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070074{
75 if (type == &memblock.memory)
76 return "memory";
77 else if (type == &memblock.reserved)
78 return "reserved";
79 else
80 return "unknown";
81}
82
Tejun Heoeb18f1b2011-12-08 10:22:07 -080083/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
84static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
85{
86 return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
87}
88
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100089/*
90 * Address comparison utilities
91 */
Yinghai Lu10d06432010-07-28 15:43:02 +100092static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100093 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100094{
95 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
96}
97
Tang Chen95cf82e2015-09-08 15:02:03 -070098bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -070099 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000100{
101 unsigned long i;
102
Alexander Kuleshovf14516f2016-01-14 15:20:39 -0800103 for (i = 0; i < type->cnt; i++)
104 if (memblock_addrs_overlap(base, size, type->regions[i].base,
105 type->regions[i].size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000106 break;
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700107 return i < type->cnt;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000108}
109
Tang Chen79442ed2013-11-12 15:07:59 -0800110/*
111 * __memblock_find_range_bottom_up - find free area utility in bottom-up
112 * @start: start of candidate range
113 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
114 * @size: size of free area to find
115 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800116 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700117 * @flags: pick from blocks based on memory attributes
Tang Chen79442ed2013-11-12 15:07:59 -0800118 *
119 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
120 *
121 * RETURNS:
122 * Found address on success, 0 on failure.
123 */
124static phys_addr_t __init_memblock
125__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700126 phys_addr_t size, phys_addr_t align, int nid,
127 ulong flags)
Tang Chen79442ed2013-11-12 15:07:59 -0800128{
129 phys_addr_t this_start, this_end, cand;
130 u64 i;
131
Tony Luckfc6daaf2015-06-24 16:58:09 -0700132 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
Tang Chen79442ed2013-11-12 15:07:59 -0800133 this_start = clamp(this_start, start, end);
134 this_end = clamp(this_end, start, end);
135
136 cand = round_up(this_start, align);
137 if (cand < this_end && this_end - cand >= size)
138 return cand;
139 }
140
141 return 0;
142}
143
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800144/**
Tang Chen14028992013-11-12 15:07:57 -0800145 * __memblock_find_range_top_down - find free area utility, in top-down
146 * @start: start of candidate range
147 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
148 * @size: size of free area to find
149 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800150 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700151 * @flags: pick from blocks based on memory attributes
Tang Chen14028992013-11-12 15:07:57 -0800152 *
153 * Utility called from memblock_find_in_range_node(), find free area top-down.
154 *
155 * RETURNS:
Tang Chen79442ed2013-11-12 15:07:59 -0800156 * Found address on success, 0 on failure.
Tang Chen14028992013-11-12 15:07:57 -0800157 */
158static phys_addr_t __init_memblock
159__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700160 phys_addr_t size, phys_addr_t align, int nid,
161 ulong flags)
Tang Chen14028992013-11-12 15:07:57 -0800162{
163 phys_addr_t this_start, this_end, cand;
164 u64 i;
165
Tony Luckfc6daaf2015-06-24 16:58:09 -0700166 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
167 NULL) {
Tang Chen14028992013-11-12 15:07:57 -0800168 this_start = clamp(this_start, start, end);
169 this_end = clamp(this_end, start, end);
170
171 if (this_end < size)
172 continue;
173
174 cand = round_down(this_end - size, align);
175 if (cand >= this_start)
176 return cand;
177 }
178
179 return 0;
180}
181
182/**
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800183 * memblock_find_in_range_node - find free area in given range and node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800184 * @size: size of free area to find
185 * @align: alignment of free area to find
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800186 * @start: start of candidate range
187 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
Grygorii Strashkob1154232014-01-21 15:50:16 -0800188 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700189 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800190 *
191 * Find @size free area aligned to @align in the specified range and node.
192 *
193 * RETURNS:
Tang Chen79442ed2013-11-12 15:07:59 -0800194 * Found address on success, 0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000195 */
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800196phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
197 phys_addr_t align, phys_addr_t start,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700198 phys_addr_t end, int nid, ulong flags)
Tang Chenf7210e62013-02-22 16:33:51 -0800199{
Tang Chenf7210e62013-02-22 16:33:51 -0800200 /* pump up @end */
201 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
202 end = memblock.current_limit;
203
204 /* avoid allocating the first page */
205 start = max_t(phys_addr_t, start, PAGE_SIZE);
206 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800207
Roman Gushchin3fcc1c32021-02-04 18:32:36 -0800208 if (memblock_bottom_up())
209 return __memblock_find_range_bottom_up(start, end, size, align,
210 nid, flags);
211 else
212 return __memblock_find_range_top_down(start, end, size, align,
213 nid, flags);
Tang Chenf7210e62013-02-22 16:33:51 -0800214}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000215
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800216/**
217 * memblock_find_in_range - find free area in given range
218 * @start: start of candidate range
219 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
220 * @size: size of free area to find
221 * @align: alignment of free area to find
222 *
223 * Find @size free area aligned to @align in the specified range.
224 *
225 * RETURNS:
Tang Chen79442ed2013-11-12 15:07:59 -0800226 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800227 */
228phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
229 phys_addr_t end, phys_addr_t size,
230 phys_addr_t align)
231{
Tony Lucka3f5baf2015-06-24 16:58:12 -0700232 phys_addr_t ret;
233 ulong flags = choose_memblock_flags();
234
235again:
236 ret = memblock_find_in_range_node(size, align, start, end,
237 NUMA_NO_NODE, flags);
238
239 if (!ret && (flags & MEMBLOCK_MIRROR)) {
240 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
241 &size);
242 flags &= ~MEMBLOCK_MIRROR;
243 goto again;
244 }
245
246 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800247}
248
Yinghai Lu10d06432010-07-28 15:43:02 +1000249static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000250{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800251 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200252 memmove(&type->regions[r], &type->regions[r + 1],
253 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000254 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000255
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700256 /* Special case for empty arrays */
257 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800258 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700259 type->cnt = 1;
260 type->regions[0].base = 0;
261 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800262 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200263 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700264 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000265}
266
Philipp Hachtmann354f17e2014-01-23 15:53:24 -0800267#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
Pavel Tatashin87395ee2017-08-18 15:16:05 -0700268/**
269 * Discard memory and reserved arrays if they were allocated
270 */
271void __init memblock_discard(void)
Yinghai Lu29f67382012-07-11 14:02:56 -0700272{
Pavel Tatashin87395ee2017-08-18 15:16:05 -0700273 phys_addr_t addr, size;
Yinghai Lu29f67382012-07-11 14:02:56 -0700274
Pavel Tatashin87395ee2017-08-18 15:16:05 -0700275 if (memblock.reserved.regions != memblock_reserved_init_regions) {
276 addr = __pa(memblock.reserved.regions);
277 size = PAGE_ALIGN(sizeof(struct memblock_region) *
278 memblock.reserved.max);
Miaohe Lin670d6e52022-02-17 22:53:27 +0800279 if (memblock_reserved_in_slab)
280 kfree(memblock.reserved.regions);
281 else
282 __memblock_free_late(addr, size);
Pavel Tatashin87395ee2017-08-18 15:16:05 -0700283 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700284
Pavel Tatashin9d263322017-08-25 15:55:46 -0700285 if (memblock.memory.regions != memblock_memory_init_regions) {
Pavel Tatashin87395ee2017-08-18 15:16:05 -0700286 addr = __pa(memblock.memory.regions);
287 size = PAGE_ALIGN(sizeof(struct memblock_region) *
288 memblock.memory.max);
Miaohe Lin670d6e52022-02-17 22:53:27 +0800289 if (memblock_memory_in_slab)
290 kfree(memblock.memory.regions);
291 else
292 __memblock_free_late(addr, size);
Pavel Tatashin87395ee2017-08-18 15:16:05 -0700293 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700294}
Philipp Hachtmann5e270e22014-01-23 15:53:11 -0800295#endif
296
Greg Pearson48c3b582012-06-20 12:53:05 -0700297/**
298 * memblock_double_array - double the size of the memblock regions array
299 * @type: memblock type of the regions array being doubled
300 * @new_area_start: starting address of memory range to avoid overlap with
301 * @new_area_size: size of memory range to avoid overlap with
302 *
303 * Double the size of the @type regions array. If memblock is being used to
304 * allocate memory for a new reserved regions array and there is a previously
305 * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
306 * waiting to be reserved, ensure the memory used by the new array does
307 * not overlap.
308 *
309 * RETURNS:
310 * 0 on success, -1 on failure.
311 */
312static int __init_memblock memblock_double_array(struct memblock_type *type,
313 phys_addr_t new_area_start,
314 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700315{
316 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700317 phys_addr_t old_alloc_size, new_alloc_size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700318 phys_addr_t old_size, new_size, addr;
319 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700320 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700321
322 /* We don't allow resizing until we know about the reserved regions
323 * of memory that aren't suitable for allocation
324 */
325 if (!memblock_can_resize)
326 return -1;
327
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700328 /* Calculate new doubled size */
329 old_size = type->max * sizeof(struct memblock_region);
330 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700331 /*
332 * We need to allocated new one align to PAGE_SIZE,
333 * so we can free them completely later.
334 */
335 old_alloc_size = PAGE_ALIGN(old_size);
336 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700337
Gavin Shan181eb392012-05-29 15:06:50 -0700338 /* Retrieve the slab flag */
339 if (type == &memblock.memory)
340 in_slab = &memblock_memory_in_slab;
341 else
342 in_slab = &memblock_reserved_in_slab;
343
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700344 /* Try to find some space for it.
345 *
346 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700347 * we use MEMBLOCK for allocations. That means that this is unsafe to
348 * use when bootmem is currently active (unless bootmem itself is
349 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700350 *
351 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700352 * call into MEMBLOCK while it's still active, or much later when slab
353 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700354 */
355 if (use_slab) {
356 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200357 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700358 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700359 /* only exclude range when trying to double reserved.regions */
360 if (type != &memblock.reserved)
361 new_area_start = new_area_size = 0;
362
363 addr = memblock_find_in_range(new_area_start + new_area_size,
364 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700365 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700366 if (!addr && new_area_size)
367 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700368 min(new_area_start, memblock.current_limit),
369 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700370
Sachin Kamat15674862012-09-04 13:55:05 +0530371 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700372 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200373 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700374 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
375 memblock_type_name(type), type->max, type->max * 2);
376 return -1;
377 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700378
Andrew Mortonfd073832012-07-31 16:42:40 -0700379 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
380 memblock_type_name(type), type->max * 2, (u64)addr,
381 (u64)addr + new_size - 1);
Yinghai Luea9e4372010-07-28 15:13:22 +1000382
Andrew Mortonfd073832012-07-31 16:42:40 -0700383 /*
384 * Found space, we now need to move the array over before we add the
385 * reserved region since it may be our reserved array itself that is
386 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700387 */
388 memcpy(new_array, type->regions, old_size);
389 memset(new_array + type->max, 0, old_size);
390 old_array = type->regions;
391 type->regions = new_array;
392 type->max <<= 1;
393
Andrew Mortonfd073832012-07-31 16:42:40 -0700394 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700395 if (*in_slab)
396 kfree(old_array);
397 else if (old_array != memblock_memory_init_regions &&
398 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700399 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700400
Andrew Mortonfd073832012-07-31 16:42:40 -0700401 /*
402 * Reserve the new array if that comes from the memblock. Otherwise, we
403 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700404 */
405 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700406 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700407
408 /* Update slab flag */
409 *in_slab = use_slab;
410
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700411 return 0;
412}
413
Tejun Heo784656f2011-07-12 11:15:55 +0200414/**
415 * memblock_merge_regions - merge neighboring compatible regions
416 * @type: memblock type to scan
417 *
418 * Scan @type and merge neighboring compatible regions.
419 */
420static void __init_memblock memblock_merge_regions(struct memblock_type *type)
421{
422 int i = 0;
423
424 /* cnt never goes below 1 */
425 while (i < type->cnt - 1) {
426 struct memblock_region *this = &type->regions[i];
427 struct memblock_region *next = &type->regions[i + 1];
428
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200429 if (this->base + this->size != next->base ||
430 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800431 memblock_get_region_node(next) ||
432 this->flags != next->flags) {
Tejun Heo784656f2011-07-12 11:15:55 +0200433 BUG_ON(this->base + this->size > next->base);
434 i++;
435 continue;
436 }
437
438 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800439 /* move forward from next + 1, index of which is i + 2 */
440 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f2011-07-12 11:15:55 +0200441 type->cnt--;
442 }
443}
444
445/**
446 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700447 * @type: memblock type to insert into
448 * @idx: index for the insertion point
449 * @base: base address of the new region
450 * @size: size of the new region
451 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800452 * @flags: flags of the new region
Tejun Heo784656f2011-07-12 11:15:55 +0200453 *
454 * Insert new memblock region [@base,@base+@size) into @type at @idx.
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700455 * @type must already have extra room to accommodate the new region.
Tejun Heo784656f2011-07-12 11:15:55 +0200456 */
457static void __init_memblock memblock_insert_region(struct memblock_type *type,
458 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800459 phys_addr_t size,
460 int nid, unsigned long flags)
Tejun Heo784656f2011-07-12 11:15:55 +0200461{
462 struct memblock_region *rgn = &type->regions[idx];
463
464 BUG_ON(type->cnt >= type->max);
465 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
466 rgn->base = base;
467 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800468 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200469 memblock_set_region_node(rgn, nid);
Tejun Heo784656f2011-07-12 11:15:55 +0200470 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800471 type->total_size += size;
Tejun Heo784656f2011-07-12 11:15:55 +0200472}
473
474/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100475 * memblock_add_range - add new memblock region
Tejun Heo784656f2011-07-12 11:15:55 +0200476 * @type: memblock type to add new region into
477 * @base: base address of the new region
478 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800479 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800480 * @flags: flags of the new region
Tejun Heo784656f2011-07-12 11:15:55 +0200481 *
482 * Add new memblock region [@base,@base+@size) into @type. The new region
483 * is allowed to overlap with existing ones - overlaps don't affect already
484 * existing regions. @type is guaranteed to be minimal (all neighbouring
485 * compatible regions are merged) after the addition.
486 *
487 * RETURNS:
488 * 0 on success, -errno on failure.
489 */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100490int __init_memblock memblock_add_range(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800491 phys_addr_t base, phys_addr_t size,
492 int nid, unsigned long flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000493{
Tejun Heo784656f2011-07-12 11:15:55 +0200494 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800495 phys_addr_t obase = base;
496 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800497 int idx, nr_new;
498 struct memblock_region *rgn;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000499
Tejun Heob3dc6272012-04-20 08:31:34 -0700500 if (!size)
501 return 0;
502
Tejun Heo784656f2011-07-12 11:15:55 +0200503 /* special case for empty array */
504 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800505 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000506 type->regions[0].base = base;
507 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800508 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800509 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800510 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000511 return 0;
512 }
Tejun Heo784656f2011-07-12 11:15:55 +0200513repeat:
514 /*
515 * The following is executed twice. Once with %false @insert and
516 * then with %true. The first counts the number of regions needed
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700517 * to accommodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700518 */
Tejun Heo784656f2011-07-12 11:15:55 +0200519 base = obase;
520 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000521
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800522 for_each_memblock_type(type, rgn) {
Tejun Heo784656f2011-07-12 11:15:55 +0200523 phys_addr_t rbase = rgn->base;
524 phys_addr_t rend = rbase + rgn->size;
525
526 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000527 break;
Tejun Heo784656f2011-07-12 11:15:55 +0200528 if (rend <= base)
529 continue;
530 /*
531 * @rgn overlaps. If it separates the lower part of new
532 * area, insert that portion.
533 */
534 if (rbase > base) {
Wei Yangc0a29492015-09-04 15:47:38 -0700535#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
536 WARN_ON(nid != memblock_get_region_node(rgn));
537#endif
Wei Yang4fcab5f2015-09-08 14:59:53 -0700538 WARN_ON(flags != rgn->flags);
Tejun Heo784656f2011-07-12 11:15:55 +0200539 nr_new++;
540 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800541 memblock_insert_region(type, idx++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800542 rbase - base, nid,
543 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000544 }
Tejun Heo784656f2011-07-12 11:15:55 +0200545 /* area below @rend is dealt with, forget about it */
546 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000547 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000548
Tejun Heo784656f2011-07-12 11:15:55 +0200549 /* insert the remaining portion */
550 if (base < end) {
551 nr_new++;
552 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800553 memblock_insert_region(type, idx, base, end - base,
Tang Chen66a20752014-01-21 15:49:20 -0800554 nid, flags);
Tejun Heo784656f2011-07-12 11:15:55 +0200555 }
556
nimisoloef3cc4d2016-07-26 15:24:56 -0700557 if (!nr_new)
558 return 0;
559
Tejun Heo784656f2011-07-12 11:15:55 +0200560 /*
561 * If this was the first round, resize array and repeat for actual
562 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700563 */
Tejun Heo784656f2011-07-12 11:15:55 +0200564 if (!insert) {
565 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700566 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f2011-07-12 11:15:55 +0200567 return -ENOMEM;
568 insert = true;
569 goto repeat;
570 } else {
571 memblock_merge_regions(type);
572 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700573 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000574}
575
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800576int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
577 int nid)
578{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100579 return memblock_add_range(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800580}
581
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700582int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700583{
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700584 memblock_dbg("memblock_add: [%#016llx-%#016llx] flags %#02lx %pF\n",
585 (unsigned long long)base,
586 (unsigned long long)base + size - 1,
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700587 0UL, (void *)_RET_IP_);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700588
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700589 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000590}
591
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800592/**
593 * memblock_isolate_range - isolate given range into disjoint memblocks
594 * @type: memblock type to isolate range for
595 * @base: base of range to isolate
596 * @size: size of range to isolate
597 * @start_rgn: out parameter for the start of isolated region
598 * @end_rgn: out parameter for the end of isolated region
599 *
600 * Walk @type and ensure that regions don't cross the boundaries defined by
601 * [@base,@base+@size). Crossing regions are split at the boundaries,
602 * which may create at most two more regions. The index of the first
603 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
604 *
605 * RETURNS:
606 * 0 on success, -errno on failure.
607 */
608static int __init_memblock memblock_isolate_range(struct memblock_type *type,
609 phys_addr_t base, phys_addr_t size,
610 int *start_rgn, int *end_rgn)
611{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800612 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800613 int idx;
614 struct memblock_region *rgn;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800615
616 *start_rgn = *end_rgn = 0;
617
Tejun Heob3dc6272012-04-20 08:31:34 -0700618 if (!size)
619 return 0;
620
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800621 /* we'll create at most two more regions */
622 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700623 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800624 return -ENOMEM;
625
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800626 for_each_memblock_type(type, rgn) {
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800627 phys_addr_t rbase = rgn->base;
628 phys_addr_t rend = rbase + rgn->size;
629
630 if (rbase >= end)
631 break;
632 if (rend <= base)
633 continue;
634
635 if (rbase < base) {
636 /*
637 * @rgn intersects from below. Split and continue
638 * to process the next region - the new top half.
639 */
640 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800641 rgn->size -= base - rbase;
642 type->total_size -= base - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800643 memblock_insert_region(type, idx, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800644 memblock_get_region_node(rgn),
645 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800646 } else if (rend > end) {
647 /*
648 * @rgn intersects from above. Split and redo the
649 * current region - the new bottom half.
650 */
651 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800652 rgn->size -= end - rbase;
653 type->total_size -= end - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800654 memblock_insert_region(type, idx--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800655 memblock_get_region_node(rgn),
656 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800657 } else {
658 /* @rgn is fully contained, record it */
659 if (!*end_rgn)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800660 *start_rgn = idx;
661 *end_rgn = idx + 1;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800662 }
663 }
664
665 return 0;
666}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800667
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800668static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100669 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000670{
Tejun Heo71936182011-12-08 10:22:07 -0800671 int start_rgn, end_rgn;
672 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000673
Tejun Heo71936182011-12-08 10:22:07 -0800674 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
675 if (ret)
676 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000677
Tejun Heo71936182011-12-08 10:22:07 -0800678 for (i = end_rgn - 1; i >= start_rgn; i--)
679 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700680 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000681}
682
Tejun Heo581adcb2011-12-08 10:22:06 -0800683int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000684{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100685 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000686}
687
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100688
Tejun Heo581adcb2011-12-08 10:22:06 -0800689int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000690{
Tejun Heo24aa0782011-07-12 11:16:06 +0200691 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700692 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800693 (unsigned long long)base + size - 1,
H. Peter Anvina1504392011-07-14 11:57:10 -0700694 (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200695
Shiraz Hashimcb3a9222016-01-19 15:11:10 +0530696 if (base < memblock.current_limit)
Kyle Yan65be4a52016-10-31 15:05:00 -0700697 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100698 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000699}
700
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700701int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000702{
Tang Chen66a20752014-01-21 15:49:20 -0800703 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700704 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800705 (unsigned long long)base + size - 1,
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700706 0UL, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000707
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700708 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000709}
710
Tejun Heo35fd0802011-07-12 11:15:59 +0200711/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800712 *
Tony Luck4308ce12014-12-12 16:54:59 -0800713 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800714 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700715 * Return 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800716 */
Tony Luck4308ce12014-12-12 16:54:59 -0800717static int __init_memblock memblock_setclr_flag(phys_addr_t base,
718 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800719{
720 struct memblock_type *type = &memblock.memory;
721 int i, ret, start_rgn, end_rgn;
722
723 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
724 if (ret)
725 return ret;
726
727 for (i = start_rgn; i < end_rgn; i++)
Tony Luck4308ce12014-12-12 16:54:59 -0800728 if (set)
729 memblock_set_region_flags(&type->regions[i], flag);
730 else
731 memblock_clear_region_flags(&type->regions[i], flag);
Tang Chen66b16ed2014-01-21 15:49:23 -0800732
733 memblock_merge_regions(type);
734 return 0;
735}
736
737/**
Tony Luck4308ce12014-12-12 16:54:59 -0800738 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
739 * @base: the base phys addr of the region
740 * @size: the size of the region
741 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700742 * Return 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800743 */
744int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
745{
746 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
747}
748
749/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800750 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
751 * @base: the base phys addr of the region
752 * @size: the size of the region
753 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700754 * Return 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800755 */
756int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
757{
Tony Luck4308ce12014-12-12 16:54:59 -0800758 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800759}
760
761/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700762 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
763 * @base: the base phys addr of the region
764 * @size: the size of the region
765 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700766 * Return 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700767 */
768int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
769{
770 system_has_some_mirror = true;
771
772 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
773}
774
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100775/**
776 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
777 * @base: the base phys addr of the region
778 * @size: the size of the region
779 *
780 * Return 0 on success, -errno on failure.
781 */
782int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
783{
784 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
785}
Tony Lucka3f5baf2015-06-24 16:58:12 -0700786
787/**
Robin Holt8e7a7f82015-06-30 14:56:41 -0700788 * __next_reserved_mem_region - next function for for_each_reserved_region()
789 * @idx: pointer to u64 loop variable
790 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
791 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
792 *
793 * Iterate over all reserved memory regions.
794 */
795void __init_memblock __next_reserved_mem_region(u64 *idx,
796 phys_addr_t *out_start,
797 phys_addr_t *out_end)
798{
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700799 struct memblock_type *type = &memblock.reserved;
Robin Holt8e7a7f82015-06-30 14:56:41 -0700800
Richard Leitnercd33a762016-05-20 16:58:33 -0700801 if (*idx < type->cnt) {
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700802 struct memblock_region *r = &type->regions[*idx];
Robin Holt8e7a7f82015-06-30 14:56:41 -0700803 phys_addr_t base = r->base;
804 phys_addr_t size = r->size;
805
806 if (out_start)
807 *out_start = base;
808 if (out_end)
809 *out_end = base + size - 1;
810
811 *idx += 1;
812 return;
813 }
814
815 /* signal end of iteration */
816 *idx = ULLONG_MAX;
817}
818
819/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100820 * __next__mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +0200821 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800822 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700823 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100824 * @type_a: pointer to memblock_type from where the range is taken
825 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700826 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
827 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
828 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200829 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100830 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +0200831 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100832 * *@idx contains index into type_a and the upper 32bit indexes the
833 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +0200834 * look like the following,
835 *
836 * 0:[0-16), 1:[32-48), 2:[128-130)
837 *
838 * The upper 32bit indexes the following regions.
839 *
840 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
841 *
842 * As both region arrays are sorted, the function advances the two indices
843 * in lockstep and returns each intersection.
844 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700845void __init_memblock __next_mem_range(u64 *idx, int nid, ulong flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100846 struct memblock_type *type_a,
847 struct memblock_type *type_b,
848 phys_addr_t *out_start,
849 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200850{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100851 int idx_a = *idx & 0xffffffff;
852 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800853
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100854 if (WARN_ONCE(nid == MAX_NUMNODES,
855 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
Grygorii Strashko560dca272014-01-21 15:50:55 -0800856 nid = NUMA_NO_NODE;
Tejun Heo35fd0802011-07-12 11:15:59 +0200857
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100858 for (; idx_a < type_a->cnt; idx_a++) {
859 struct memblock_region *m = &type_a->regions[idx_a];
860
Tejun Heo35fd0802011-07-12 11:15:59 +0200861 phys_addr_t m_start = m->base;
862 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100863 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +0200864
865 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100866 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200867 continue;
868
Xishi Qiu0a313a92014-09-09 14:50:46 -0700869 /* skip hotpluggable memory regions if needed */
870 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
871 continue;
872
Tony Lucka3f5baf2015-06-24 16:58:12 -0700873 /* if we want mirror memory skip non-mirror memory regions */
874 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
875 continue;
876
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100877 /* skip nomap memory unless we were asked for it explicitly */
878 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
879 continue;
880
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100881 if (!type_b) {
882 if (out_start)
883 *out_start = m_start;
884 if (out_end)
885 *out_end = m_end;
886 if (out_nid)
887 *out_nid = m_nid;
888 idx_a++;
889 *idx = (u32)idx_a | (u64)idx_b << 32;
890 return;
891 }
Tejun Heo35fd0802011-07-12 11:15:59 +0200892
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100893 /* scan areas before each reservation */
894 for (; idx_b < type_b->cnt + 1; idx_b++) {
895 struct memblock_region *r;
896 phys_addr_t r_start;
897 phys_addr_t r_end;
898
899 r = &type_b->regions[idx_b];
900 r_start = idx_b ? r[-1].base + r[-1].size : 0;
901 r_end = idx_b < type_b->cnt ?
Stefan Agner15c3e892018-04-05 16:25:38 -0700902 r->base : (phys_addr_t)ULLONG_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100903
904 /*
905 * if idx_b advanced past idx_a,
906 * break out to advance idx_a
907 */
Tejun Heo35fd0802011-07-12 11:15:59 +0200908 if (r_start >= m_end)
909 break;
910 /* if the two regions intersect, we're done */
911 if (m_start < r_end) {
912 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100913 *out_start =
914 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +0200915 if (out_end)
916 *out_end = min(m_end, r_end);
917 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100918 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +0200919 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100920 * The region which ends first is
921 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +0200922 */
923 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100924 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +0200925 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100926 idx_b++;
927 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +0200928 return;
929 }
930 }
931 }
932
933 /* signal end of iteration */
934 *idx = ULLONG_MAX;
935}
936
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800937/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100938 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
939 *
940 * Finds the next range from type_a which is not marked as unsuitable
941 * in type_b.
942 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800943 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -0700944 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700945 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100946 * @type_a: pointer to memblock_type from where the range is taken
947 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700948 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
949 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
950 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800951 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100952 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800953 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700954void __init_memblock __next_mem_range_rev(u64 *idx, int nid, ulong flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100955 struct memblock_type *type_a,
956 struct memblock_type *type_b,
957 phys_addr_t *out_start,
958 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800959{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100960 int idx_a = *idx & 0xffffffff;
961 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800962
Grygorii Strashko560dca272014-01-21 15:50:55 -0800963 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
964 nid = NUMA_NO_NODE;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800965
966 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100967 idx_a = type_a->cnt - 1;
zijun_hue47608a2016-08-04 15:32:00 -0700968 if (type_b != NULL)
969 idx_b = type_b->cnt;
970 else
971 idx_b = 0;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800972 }
973
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100974 for (; idx_a >= 0; idx_a--) {
975 struct memblock_region *m = &type_a->regions[idx_a];
976
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800977 phys_addr_t m_start = m->base;
978 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100979 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800980
981 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100982 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800983 continue;
984
Tang Chen55ac5902014-01-21 15:49:35 -0800985 /* skip hotpluggable memory regions if needed */
986 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
987 continue;
988
Tony Lucka3f5baf2015-06-24 16:58:12 -0700989 /* if we want mirror memory skip non-mirror memory regions */
990 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
991 continue;
992
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100993 /* skip nomap memory unless we were asked for it explicitly */
994 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
995 continue;
996
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100997 if (!type_b) {
998 if (out_start)
999 *out_start = m_start;
1000 if (out_end)
1001 *out_end = m_end;
1002 if (out_nid)
1003 *out_nid = m_nid;
zijun_hufb399b42016-07-28 15:48:56 -07001004 idx_a--;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001005 *idx = (u32)idx_a | (u64)idx_b << 32;
1006 return;
1007 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001008
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001009 /* scan areas before each reservation */
1010 for (; idx_b >= 0; idx_b--) {
1011 struct memblock_region *r;
1012 phys_addr_t r_start;
1013 phys_addr_t r_end;
1014
1015 r = &type_b->regions[idx_b];
1016 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1017 r_end = idx_b < type_b->cnt ?
Stefan Agner15c3e892018-04-05 16:25:38 -07001018 r->base : (phys_addr_t)ULLONG_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001019 /*
1020 * if idx_b advanced past idx_a,
1021 * break out to advance idx_a
1022 */
1023
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001024 if (r_end <= m_start)
1025 break;
1026 /* if the two regions intersect, we're done */
1027 if (m_end > r_start) {
1028 if (out_start)
1029 *out_start = max(m_start, r_start);
1030 if (out_end)
1031 *out_end = min(m_end, r_end);
1032 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001033 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001034 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001035 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001036 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001037 idx_b--;
1038 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001039 return;
1040 }
1041 }
1042 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001043 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001044 *idx = ULLONG_MAX;
1045}
1046
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001047#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1048/*
1049 * Common iterator interface used to define for_each_mem_range().
1050 */
1051void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1052 unsigned long *out_start_pfn,
1053 unsigned long *out_end_pfn, int *out_nid)
1054{
1055 struct memblock_type *type = &memblock.memory;
1056 struct memblock_region *r;
1057
1058 while (++*idx < type->cnt) {
1059 r = &type->regions[*idx];
1060
1061 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1062 continue;
1063 if (nid == MAX_NUMNODES || nid == r->nid)
1064 break;
1065 }
1066 if (*idx >= type->cnt) {
1067 *idx = -1;
1068 return;
1069 }
1070
1071 if (out_start_pfn)
1072 *out_start_pfn = PFN_UP(r->base);
1073 if (out_end_pfn)
1074 *out_end_pfn = PFN_DOWN(r->base + r->size);
1075 if (out_nid)
1076 *out_nid = r->nid;
1077}
1078
1079/**
1080 * memblock_set_node - set node ID on memblock regions
1081 * @base: base of area to set node ID for
1082 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001083 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001084 * @nid: node ID to set
1085 *
Tang Chene7e8de52014-01-21 15:49:26 -08001086 * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001087 * Regions which cross the area boundaries are split as necessary.
1088 *
1089 * RETURNS:
1090 * 0 on success, -errno on failure.
1091 */
1092int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001093 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001094{
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001095 int start_rgn, end_rgn;
1096 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001097
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001098 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1099 if (ret)
1100 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001101
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001102 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001103 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001104
1105 memblock_merge_regions(type);
1106 return 0;
1107}
1108#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1109
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001110static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1111 phys_addr_t align, phys_addr_t start,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001112 phys_addr_t end, int nid, ulong flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001113{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001114 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001115
Grygorii Strashko79f40fa2014-01-21 15:50:12 -08001116 if (!align)
1117 align = SMP_CACHE_BYTES;
Vineet Gupta94f3d3a2013-04-29 15:06:15 -07001118
Tony Luckfc6daaf2015-06-24 16:58:09 -07001119 found = memblock_find_in_range_node(size, align, start, end, nid,
1120 flags);
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001121 if (found && !memblock_reserve(found, size)) {
1122 /*
1123 * The min_count is set to 0 so that memblock allocations are
1124 * never reported as leaks.
1125 */
Shiraz Hashimcb3a9222016-01-19 15:11:10 +05301126 if (found < memblock.current_limit)
Kyle Yan65be4a52016-10-31 15:05:00 -07001127 kmemleak_alloc_phys(found, size, 0, 0);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001128 return found;
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001129 }
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001130 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001131}
1132
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001133phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001134 phys_addr_t start, phys_addr_t end,
1135 ulong flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001136{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001137 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1138 flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001139}
1140
1141static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
1142 phys_addr_t align, phys_addr_t max_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001143 int nid, ulong flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001144{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001145 return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001146}
1147
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001148phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
1149{
Tony Lucka3f5baf2015-06-24 16:58:12 -07001150 ulong flags = choose_memblock_flags();
1151 phys_addr_t ret;
1152
1153again:
1154 ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
1155 nid, flags);
1156
1157 if (!ret && (flags & MEMBLOCK_MIRROR)) {
1158 flags &= ~MEMBLOCK_MIRROR;
1159 goto again;
1160 }
1161 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001162}
1163
1164phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1165{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001166 return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
1167 MEMBLOCK_NONE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001168}
1169
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001170phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001171{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001172 phys_addr_t alloc;
1173
1174 alloc = __memblock_alloc_base(size, align, max_addr);
1175
1176 if (alloc == 0)
1177 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
1178 (unsigned long long) size, (unsigned long long) max_addr);
1179
1180 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001181}
1182
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001183phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001184{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001185 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001186}
1187
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001188phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1189{
1190 phys_addr_t res = memblock_alloc_nid(size, align, nid);
1191
1192 if (res)
1193 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001194 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001195}
1196
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001197/**
1198 * memblock_virt_alloc_internal - allocate boot memory block
1199 * @size: size of memory block to be allocated in bytes
1200 * @align: alignment of the region and block's size
1201 * @min_addr: the lower bound of the memory region to allocate (phys address)
1202 * @max_addr: the upper bound of the memory region to allocate (phys address)
1203 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1204 *
1205 * The @min_addr limit is dropped if it can not be satisfied and the allocation
1206 * will fall back to memory below @min_addr. Also, allocation may fall back
1207 * to any node in the system if the specified node can not
1208 * hold the requested memory.
1209 *
1210 * The allocation is performed from memory region limited by
1211 * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE.
1212 *
1213 * The memory block is aligned on SMP_CACHE_BYTES if @align == 0.
1214 *
1215 * The phys address of allocated boot memory block is converted to virtual and
1216 * allocated memory is reset to 0.
1217 *
1218 * In addition, function sets the min_count to 0 using kmemleak_alloc for
1219 * allocated boot memory block, so that it is never reported as leaks.
1220 *
1221 * RETURNS:
1222 * Virtual address of allocated memory block on success, NULL on failure.
1223 */
1224static void * __init memblock_virt_alloc_internal(
1225 phys_addr_t size, phys_addr_t align,
1226 phys_addr_t min_addr, phys_addr_t max_addr,
1227 int nid)
1228{
1229 phys_addr_t alloc;
1230 void *ptr;
Tony Lucka3f5baf2015-06-24 16:58:12 -07001231 ulong flags = choose_memblock_flags();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001232
Grygorii Strashko560dca272014-01-21 15:50:55 -08001233 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1234 nid = NUMA_NO_NODE;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001235
1236 /*
1237 * Detect any accidental use of these APIs after slab is ready, as at
1238 * this moment memblock may be deinitialized already and its
1239 * internal data may be destroyed (after execution of free_all_bootmem)
1240 */
1241 if (WARN_ON_ONCE(slab_is_available()))
1242 return kzalloc_node(size, GFP_NOWAIT, nid);
1243
1244 if (!align)
1245 align = SMP_CACHE_BYTES;
1246
Yinghai Luf544e142014-01-29 14:05:52 -08001247 if (max_addr > memblock.current_limit)
1248 max_addr = memblock.current_limit;
1249
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001250again:
1251 alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001252 nid, flags);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001253 if (alloc)
1254 goto done;
1255
1256 if (nid != NUMA_NO_NODE) {
1257 alloc = memblock_find_in_range_node(size, align, min_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001258 max_addr, NUMA_NO_NODE,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001259 flags);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001260 if (alloc)
1261 goto done;
1262 }
1263
1264 if (min_addr) {
1265 min_addr = 0;
1266 goto again;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001267 }
1268
Tony Lucka3f5baf2015-06-24 16:58:12 -07001269 if (flags & MEMBLOCK_MIRROR) {
1270 flags &= ~MEMBLOCK_MIRROR;
1271 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1272 &size);
1273 goto again;
1274 }
1275
1276 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001277done:
1278 memblock_reserve(alloc, size);
1279 ptr = phys_to_virt(alloc);
1280 memset(ptr, 0, size);
1281
1282 /*
1283 * The min_count is set to 0 so that bootmem allocated blocks
1284 * are never reported as leaks. This is because many of these blocks
1285 * are only referred via the physical address which is not
1286 * looked up by kmemleak.
1287 */
1288 kmemleak_alloc(ptr, size, 0, 0);
1289
1290 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001291}
1292
1293/**
1294 * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block
1295 * @size: size of memory block to be allocated in bytes
1296 * @align: alignment of the region and block's size
1297 * @min_addr: the lower bound of the memory region from where the allocation
1298 * is preferred (phys address)
1299 * @max_addr: the upper bound of the memory region from where the allocation
1300 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1301 * allocate only from memory limited by memblock.current_limit value
1302 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1303 *
1304 * Public version of _memblock_virt_alloc_try_nid_nopanic() which provides
1305 * additional debug information (including caller info), if enabled.
1306 *
1307 * RETURNS:
1308 * Virtual address of allocated memory block on success, NULL on failure.
1309 */
1310void * __init memblock_virt_alloc_try_nid_nopanic(
1311 phys_addr_t size, phys_addr_t align,
1312 phys_addr_t min_addr, phys_addr_t max_addr,
1313 int nid)
1314{
1315 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1316 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1317 (u64)max_addr, (void *)_RET_IP_);
1318 return memblock_virt_alloc_internal(size, align, min_addr,
1319 max_addr, nid);
1320}
1321
1322/**
1323 * memblock_virt_alloc_try_nid - allocate boot memory block with panicking
1324 * @size: size of memory block to be allocated in bytes
1325 * @align: alignment of the region and block's size
1326 * @min_addr: the lower bound of the memory region from where the allocation
1327 * is preferred (phys address)
1328 * @max_addr: the upper bound of the memory region from where the allocation
1329 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1330 * allocate only from memory limited by memblock.current_limit value
1331 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1332 *
1333 * Public panicking version of _memblock_virt_alloc_try_nid_nopanic()
1334 * which provides debug information (including caller info), if enabled,
1335 * and panics if the request can not be satisfied.
1336 *
1337 * RETURNS:
1338 * Virtual address of allocated memory block on success, NULL on failure.
1339 */
1340void * __init memblock_virt_alloc_try_nid(
1341 phys_addr_t size, phys_addr_t align,
1342 phys_addr_t min_addr, phys_addr_t max_addr,
1343 int nid)
1344{
1345 void *ptr;
1346
1347 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1348 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1349 (u64)max_addr, (void *)_RET_IP_);
1350 ptr = memblock_virt_alloc_internal(size, align,
1351 min_addr, max_addr, nid);
1352 if (ptr)
1353 return ptr;
1354
1355 panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx\n",
1356 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1357 (u64)max_addr);
1358 return NULL;
1359}
1360
1361/**
1362 * __memblock_free_early - free boot memory block
1363 * @base: phys starting address of the boot memory block
1364 * @size: size of the boot memory block in bytes
1365 *
1366 * Free boot memory block previously allocated by memblock_virt_alloc_xx() API.
1367 * The freeing memory will not be released to the buddy allocator.
1368 */
1369void __init __memblock_free_early(phys_addr_t base, phys_addr_t size)
1370{
1371 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1372 __func__, (u64)base, (u64)base + size - 1,
1373 (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001374 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001375 memblock_remove_range(&memblock.reserved, base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001376}
1377
1378/*
1379 * __memblock_free_late - free bootmem block pages directly to buddy allocator
1380 * @addr: phys starting address of the boot memory block
1381 * @size: size of the boot memory block in bytes
1382 *
1383 * This is only useful when the bootmem allocator has already been torn
1384 * down, but we are still initializing the system. Pages are released directly
1385 * to the buddy allocator, no bootmem metadata is updated because it is gone.
1386 */
1387void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1388{
1389 u64 cursor, end;
1390
1391 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1392 __func__, (u64)base, (u64)base + size - 1,
1393 (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001394 kmemleak_free_part_phys(base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001395 cursor = PFN_UP(base);
1396 end = PFN_DOWN(base + size);
1397
1398 for (; cursor < end; cursor++) {
Mel Gormand70ddd72015-06-30 14:56:52 -07001399 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001400 totalram_pages++;
1401 }
1402}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001403
1404/*
1405 * Remaining API functions
1406 */
1407
David Gibson1f1ffb8a2016-02-05 15:36:19 -08001408phys_addr_t __init_memblock memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001409{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001410 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001411}
1412
Srikar Dronamraju8907de52016-10-07 16:59:18 -07001413phys_addr_t __init_memblock memblock_reserved_size(void)
1414{
1415 return memblock.reserved.total_size;
1416}
1417
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001418phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1419{
1420 unsigned long pages = 0;
1421 struct memblock_region *r;
1422 unsigned long start_pfn, end_pfn;
1423
1424 for_each_memblock(memory, r) {
1425 start_pfn = memblock_region_memory_base_pfn(r);
1426 end_pfn = memblock_region_memory_end_pfn(r);
1427 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1428 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1429 pages += end_pfn - start_pfn;
1430 }
1431
Fabian Frederick16763232014-04-07 15:37:53 -07001432 return PFN_PHYS(pages);
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001433}
1434
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001435/* lowest address */
1436phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1437{
1438 return memblock.memory.regions[0].base;
1439}
1440
Yinghai Lu10d06432010-07-28 15:43:02 +10001441phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001442{
1443 int idx = memblock.memory.cnt - 1;
1444
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001445 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001446}
1447
Dennis Chena571d4e2016-07-28 15:48:26 -07001448static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001449{
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001450 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001451 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001452
Dennis Chena571d4e2016-07-28 15:48:26 -07001453 /*
1454 * translate the memory @limit size into the max address within one of
1455 * the memory memblock regions, if the @limit exceeds the total size
1456 * of those regions, max_addr will keep original value ULLONG_MAX
1457 */
Emil Medve136199f2014-04-07 15:37:52 -07001458 for_each_memblock(memory, r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001459 if (limit <= r->size) {
1460 max_addr = r->base + limit;
1461 break;
1462 }
1463 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001464 }
1465
Dennis Chena571d4e2016-07-28 15:48:26 -07001466 return max_addr;
1467}
1468
1469void __init memblock_enforce_memory_limit(phys_addr_t limit)
1470{
1471 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
1472
1473 if (!limit)
1474 return;
1475
1476 max_addr = __find_max_addr(limit);
1477
1478 /* @limit exceeds the total size of the memory, do nothing */
1479 if (max_addr == (phys_addr_t)ULLONG_MAX)
1480 return;
1481
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001482 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001483 memblock_remove_range(&memblock.memory, max_addr,
1484 (phys_addr_t)ULLONG_MAX);
1485 memblock_remove_range(&memblock.reserved, max_addr,
1486 (phys_addr_t)ULLONG_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001487}
1488
Dennis Chena571d4e2016-07-28 15:48:26 -07001489void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1490{
1491 struct memblock_type *type = &memblock.memory;
1492 phys_addr_t max_addr;
1493 int i, ret, start_rgn, end_rgn;
1494
1495 if (!limit)
1496 return;
1497
1498 max_addr = __find_max_addr(limit);
1499
1500 /* @limit exceeds the total size of the memory, do nothing */
1501 if (max_addr == (phys_addr_t)ULLONG_MAX)
1502 return;
1503
1504 ret = memblock_isolate_range(type, max_addr, (phys_addr_t)ULLONG_MAX,
1505 &start_rgn, &end_rgn);
1506 if (ret)
1507 return;
1508
1509 /* remove all the MAP regions above the limit */
1510 for (i = end_rgn - 1; i >= start_rgn; i--) {
1511 if (!memblock_is_nomap(&type->regions[i]))
1512 memblock_remove_region(type, i);
1513 }
1514 /* truncate the reserved regions */
1515 memblock_remove_range(&memblock.reserved, max_addr,
1516 (phys_addr_t)ULLONG_MAX);
1517}
1518
Shiraz Hashime1525702016-02-04 14:53:33 +05301519static int __init_memblock __memblock_search(struct memblock_type *type,
1520 phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001521{
1522 unsigned int left = 0, right = type->cnt;
1523
1524 do {
1525 unsigned int mid = (right + left) / 2;
1526
1527 if (addr < type->regions[mid].base)
1528 right = mid;
1529 else if (addr >= (type->regions[mid].base +
1530 type->regions[mid].size))
1531 left = mid + 1;
1532 else
1533 return mid;
1534 } while (left < right);
1535 return -1;
1536}
1537
Shiraz Hashime1525702016-02-04 14:53:33 +05301538static int __init_memblock memblock_search(struct memblock_type *type,
1539 phys_addr_t addr)
1540{
1541 int ret;
1542 unsigned long seq;
1543
1544 do {
1545 seq = raw_read_seqcount_begin(&memblock_seq);
1546 ret = __memblock_search(type, addr);
1547 } while (unlikely(read_seqcount_retry(&memblock_seq, seq)));
1548
1549 return ret;
1550}
1551
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001552bool __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001553{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001554 return memblock_search(&memblock.reserved, addr) != -1;
1555}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001556
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001557bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001558{
1559 return memblock_search(&memblock.memory, addr) != -1;
1560}
1561
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001562int __init_memblock memblock_is_map_memory(phys_addr_t addr)
1563{
1564 int i = memblock_search(&memblock.memory, addr);
1565
1566 if (i == -1)
1567 return false;
1568 return !memblock_is_nomap(&memblock.memory.regions[i]);
1569}
1570
Yinghai Lue76b63f2013-09-11 14:22:17 -07001571#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1572int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1573 unsigned long *start_pfn, unsigned long *end_pfn)
1574{
1575 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001576 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001577
1578 if (mid == -1)
1579 return -1;
1580
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001581 *start_pfn = PFN_DOWN(type->regions[mid].base);
1582 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001583
1584 return type->regions[mid].nid;
1585}
1586#endif
1587
Stephen Boydeab30942012-05-24 00:45:21 -07001588/**
1589 * memblock_is_region_memory - check if a region is a subset of memory
1590 * @base: base of region to check
1591 * @size: size of region to check
1592 *
1593 * Check if the region [@base, @base+@size) is a subset of a memory block.
1594 *
1595 * RETURNS:
1596 * 0 if false, non-zero if true
1597 */
Yinghai Lu3661ca62010-09-15 13:05:29 -07001598int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001599{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001600 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001601 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001602
1603 if (idx == -1)
1604 return 0;
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001605 return memblock.memory.regions[idx].base <= base &&
1606 (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001607 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001608}
1609
Stephen Boyd48cc5402017-03-01 16:42:02 -08001610bool __init_memblock memblock_overlaps_memory(phys_addr_t base,
1611 phys_addr_t size)
Stephen Boyd39ea1a32012-05-14 18:55:50 -07001612{
1613 memblock_cap_size(base, &size);
1614
Stephen Boyd48cc5402017-03-01 16:42:02 -08001615 return memblock_overlaps_region(&memblock.memory, base, size);
Stephen Boyd39ea1a32012-05-14 18:55:50 -07001616}
1617
Stephen Boydeab30942012-05-24 00:45:21 -07001618/**
1619 * memblock_is_region_reserved - check if a region intersects reserved memory
1620 * @base: base of region to check
1621 * @size: size of region to check
1622 *
1623 * Check if the region [@base, @base+@size) intersects a reserved memory block.
1624 *
1625 * RETURNS:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001626 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001627 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001628bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001629{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001630 memblock_cap_size(base, &size);
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001631 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001632}
1633
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001634void __init_memblock memblock_trim_memory(phys_addr_t align)
1635{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001636 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001637 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001638
Emil Medve136199f2014-04-07 15:37:52 -07001639 for_each_memblock(memory, r) {
1640 orig_start = r->base;
1641 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001642 start = round_up(orig_start, align);
1643 end = round_down(orig_end, align);
1644
1645 if (start == orig_start && end == orig_end)
1646 continue;
1647
1648 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001649 r->base = start;
1650 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001651 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001652 memblock_remove_region(&memblock.memory,
1653 r - memblock.memory.regions);
1654 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001655 }
1656 }
1657}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001658
Yinghai Lu3661ca62010-09-15 13:05:29 -07001659void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001660{
1661 memblock.current_limit = limit;
1662}
1663
Laura Abbottfec51012014-02-27 01:23:43 +01001664phys_addr_t __init_memblock memblock_get_current_limit(void)
1665{
1666 return memblock.current_limit;
1667}
1668
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001669static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001670{
1671 unsigned long long base, size;
Tang Chen66a20752014-01-21 15:49:20 -08001672 unsigned long flags;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001673 int idx;
1674 struct memblock_region *rgn;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001675
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001676 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001677
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001678 for_each_memblock_type(type, rgn) {
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001679 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001680
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001681 base = rgn->base;
1682 size = rgn->size;
Tang Chen66a20752014-01-21 15:49:20 -08001683 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001684#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1685 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1686 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1687 memblock_get_region_node(rgn));
1688#endif
Tang Chen66a20752014-01-21 15:49:20 -08001689 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001690 name, idx, base, base + size - 1, size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001691 }
1692}
1693
Michal Hocko292f70c2017-06-02 14:46:49 -07001694extern unsigned long __init_memblock
1695memblock_reserved_memory_within(phys_addr_t start_addr, phys_addr_t end_addr)
1696{
1697 struct memblock_region *rgn;
1698 unsigned long size = 0;
1699 int idx;
1700
1701 for_each_memblock_type((&memblock.reserved), rgn) {
1702 phys_addr_t start, end;
1703
1704 if (rgn->base + rgn->size < start_addr)
1705 continue;
1706 if (rgn->base > end_addr)
1707 continue;
1708
1709 start = rgn->base;
1710 end = start + rgn->size;
1711 size += end - start;
1712 }
1713
1714 return size;
1715}
1716
Tejun Heo4ff7b822011-12-08 10:22:06 -08001717void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001718{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001719 pr_info("MEMBLOCK configuration:\n");
Tejun Heo1440c4e2011-12-08 10:22:08 -08001720 pr_info(" memory size = %#llx reserved size = %#llx\n",
1721 (unsigned long long)memblock.memory.total_size,
1722 (unsigned long long)memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001723
1724 memblock_dump(&memblock.memory, "memory");
1725 memblock_dump(&memblock.reserved, "reserved");
1726}
1727
Tejun Heo1aadc052011-12-08 10:22:08 -08001728void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001729{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001730 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001731}
1732
Shiraz Hashim3eaf6e62016-04-16 11:43:02 +05301733static unsigned long __init_memblock
1734memblock_resize_late(int begin, unsigned long flags)
Shiraz Hashime1525702016-02-04 14:53:33 +05301735{
1736 static int memblock_can_resize_old;
1737
1738 if (begin) {
1739 preempt_disable();
Shiraz Hashim3eaf6e62016-04-16 11:43:02 +05301740 local_irq_save(flags);
Shiraz Hashime1525702016-02-04 14:53:33 +05301741 memblock_can_resize_old = memblock_can_resize;
1742 memblock_can_resize = 0;
1743 raw_write_seqcount_begin(&memblock_seq);
1744 } else {
1745 raw_write_seqcount_end(&memblock_seq);
1746 memblock_can_resize = memblock_can_resize_old;
Shiraz Hashim3eaf6e62016-04-16 11:43:02 +05301747 local_irq_restore(flags);
Shiraz Hashime1525702016-02-04 14:53:33 +05301748 preempt_enable();
1749 }
Shiraz Hashim3eaf6e62016-04-16 11:43:02 +05301750
1751 return flags;
Shiraz Hashime1525702016-02-04 14:53:33 +05301752}
1753
Shiraz Hashim3eaf6e62016-04-16 11:43:02 +05301754unsigned long __init_memblock memblock_region_resize_late_begin(void)
Shiraz Hashime1525702016-02-04 14:53:33 +05301755{
Shiraz Hashim3eaf6e62016-04-16 11:43:02 +05301756 return memblock_resize_late(1, 0);
Shiraz Hashime1525702016-02-04 14:53:33 +05301757}
1758
Shiraz Hashim3eaf6e62016-04-16 11:43:02 +05301759void __init_memblock memblock_region_resize_late_end(unsigned long flags)
Shiraz Hashime1525702016-02-04 14:53:33 +05301760{
Shiraz Hashim3eaf6e62016-04-16 11:43:02 +05301761 memblock_resize_late(0, flags);
Shiraz Hashime1525702016-02-04 14:53:33 +05301762}
1763
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001764static int __init early_memblock(char *p)
1765{
1766 if (p && strstr(p, "debug"))
1767 memblock_debug = 1;
1768 return 0;
1769}
1770early_param("memblock", early_memblock);
1771
Tejun Heoc378ddd2011-07-14 11:46:03 +02001772#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001773
1774static int memblock_debug_show(struct seq_file *m, void *private)
1775{
1776 struct memblock_type *type = m->private;
1777 struct memblock_region *reg;
1778 int i;
1779
1780 for (i = 0; i < type->cnt; i++) {
1781 reg = &type->regions[i];
1782 seq_printf(m, "%4d: ", i);
1783 if (sizeof(phys_addr_t) == 4)
1784 seq_printf(m, "0x%08lx..0x%08lx\n",
1785 (unsigned long)reg->base,
1786 (unsigned long)(reg->base + reg->size - 1));
1787 else
1788 seq_printf(m, "0x%016llx..0x%016llx\n",
1789 (unsigned long long)reg->base,
1790 (unsigned long long)(reg->base + reg->size - 1));
1791
1792 }
1793 return 0;
1794}
1795
1796static int memblock_debug_open(struct inode *inode, struct file *file)
1797{
1798 return single_open(file, memblock_debug_show, inode->i_private);
1799}
1800
1801static const struct file_operations memblock_debug_fops = {
1802 .open = memblock_debug_open,
1803 .read = seq_read,
1804 .llseek = seq_lseek,
1805 .release = single_release,
1806};
1807
1808static int __init memblock_init_debugfs(void)
1809{
1810 struct dentry *root = debugfs_create_dir("memblock", NULL);
1811 if (!root)
1812 return -ENXIO;
1813 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1814 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001815#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
1816 debugfs_create_file("physmem", S_IRUGO, root, &memblock.physmem, &memblock_debug_fops);
1817#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001818
1819 return 0;
1820}
1821__initcall(memblock_init_debugfs);
1822
1823#endif /* CONFIG_DEBUG_FS */