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