1 | #ifndef _LINUX_MEMBLOCK_H |
2 | #define _LINUX_MEMBLOCK_H |
3 | #ifdef __KERNEL__ |
4 | |
5 | /* |
6 | * Logical memory blocks. |
7 | * |
8 | * Copyright (C) 2001 Peter Bergner, IBM Corp. |
9 | * |
10 | * This program is free software; you can redistribute it and/or |
11 | * modify it under the terms of the GNU General Public License |
12 | * as published by the Free Software Foundation; either version |
13 | * 2 of the License, or (at your option) any later version. |
14 | */ |
15 | |
16 | #include <linux/init.h> |
17 | #include <linux/mm.h> |
18 | #include <asm/dma.h> |
19 | |
20 | extern unsigned long max_low_pfn; |
21 | extern unsigned long min_low_pfn; |
22 | |
23 | /* |
24 | * highest page |
25 | */ |
26 | extern unsigned long max_pfn; |
27 | /* |
28 | * highest possible page |
29 | */ |
30 | extern unsigned long long max_possible_pfn; |
31 | |
32 | /** |
33 | * enum memblock_flags - definition of memory region attributes |
34 | * @MEMBLOCK_NONE: no special request |
35 | * @MEMBLOCK_HOTPLUG: hotpluggable region |
36 | * @MEMBLOCK_MIRROR: mirrored region |
37 | * @MEMBLOCK_NOMAP: don't add to kernel direct mapping |
38 | */ |
39 | enum memblock_flags { |
40 | MEMBLOCK_NONE = 0x0, /* No special request */ |
41 | MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */ |
42 | MEMBLOCK_MIRROR = 0x2, /* mirrored region */ |
43 | MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */ |
44 | }; |
45 | |
46 | /** |
47 | * struct memblock_region - represents a memory region |
48 | * @base: physical address of the region |
49 | * @size: size of the region |
50 | * @flags: memory region attributes |
51 | * @nid: NUMA node id |
52 | */ |
53 | struct memblock_region { |
54 | phys_addr_t base; |
55 | phys_addr_t size; |
56 | enum memblock_flags flags; |
57 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
58 | int nid; |
59 | #endif |
60 | }; |
61 | |
62 | /** |
63 | * struct memblock_type - collection of memory regions of certain type |
64 | * @cnt: number of regions |
65 | * @max: size of the allocated array |
66 | * @total_size: size of all regions |
67 | * @regions: array of regions |
68 | * @name: the memory type symbolic name |
69 | */ |
70 | struct memblock_type { |
71 | unsigned long cnt; |
72 | unsigned long max; |
73 | phys_addr_t total_size; |
74 | struct memblock_region *regions; |
75 | char *name; |
76 | }; |
77 | |
78 | /** |
79 | * struct memblock - memblock allocator metadata |
80 | * @bottom_up: is bottom up direction? |
81 | * @current_limit: physical address of the current allocation limit |
82 | * @memory: usabe memory regions |
83 | * @reserved: reserved memory regions |
84 | * @physmem: all physical memory |
85 | */ |
86 | struct memblock { |
87 | bool bottom_up; /* is bottom up direction? */ |
88 | phys_addr_t current_limit; |
89 | struct memblock_type memory; |
90 | struct memblock_type reserved; |
91 | #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
92 | struct memblock_type physmem; |
93 | #endif |
94 | }; |
95 | |
96 | extern struct memblock memblock; |
97 | extern int memblock_debug; |
98 | |
99 | #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK |
100 | #define __init_memblock __meminit |
101 | #define __initdata_memblock __meminitdata |
102 | void memblock_discard(void); |
103 | #else |
104 | #define __init_memblock |
105 | #define __initdata_memblock |
106 | #endif |
107 | |
108 | #define memblock_dbg(fmt, ...) \ |
109 | if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) |
110 | |
111 | phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end, |
112 | phys_addr_t size, phys_addr_t align); |
113 | void memblock_allow_resize(void); |
114 | int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid); |
115 | int memblock_add(phys_addr_t base, phys_addr_t size); |
116 | int memblock_remove(phys_addr_t base, phys_addr_t size); |
117 | int memblock_free(phys_addr_t base, phys_addr_t size); |
118 | int memblock_reserve(phys_addr_t base, phys_addr_t size); |
119 | void memblock_trim_memory(phys_addr_t align); |
120 | bool memblock_overlaps_region(struct memblock_type *type, |
121 | phys_addr_t base, phys_addr_t size); |
122 | int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size); |
123 | int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size); |
124 | int memblock_mark_mirror(phys_addr_t base, phys_addr_t size); |
125 | int memblock_mark_nomap(phys_addr_t base, phys_addr_t size); |
126 | int memblock_clear_nomap(phys_addr_t base, phys_addr_t size); |
127 | |
128 | unsigned long memblock_free_all(void); |
129 | void reset_node_managed_pages(pg_data_t *pgdat); |
130 | void reset_all_zones_managed_pages(void); |
131 | |
132 | /* Low level functions */ |
133 | int memblock_add_range(struct memblock_type *type, |
134 | phys_addr_t base, phys_addr_t size, |
135 | int nid, enum memblock_flags flags); |
136 | |
137 | void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags, |
138 | struct memblock_type *type_a, |
139 | struct memblock_type *type_b, phys_addr_t *out_start, |
140 | phys_addr_t *out_end, int *out_nid); |
141 | |
142 | void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags, |
143 | struct memblock_type *type_a, |
144 | struct memblock_type *type_b, phys_addr_t *out_start, |
145 | phys_addr_t *out_end, int *out_nid); |
146 | |
147 | void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start, |
148 | phys_addr_t *out_end); |
149 | |
150 | void __memblock_free_late(phys_addr_t base, phys_addr_t size); |
151 | |
152 | /** |
153 | * for_each_mem_range - iterate through memblock areas from type_a and not |
154 | * included in type_b. Or just type_a if type_b is NULL. |
155 | * @i: u64 used as loop variable |
156 | * @type_a: ptr to memblock_type to iterate |
157 | * @type_b: ptr to memblock_type which excludes from the iteration |
158 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
159 | * @flags: pick from blocks based on memory attributes |
160 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
161 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
162 | * @p_nid: ptr to int for nid of the range, can be %NULL |
163 | */ |
164 | #define for_each_mem_range(i, type_a, type_b, nid, flags, \ |
165 | p_start, p_end, p_nid) \ |
166 | for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \ |
167 | p_start, p_end, p_nid); \ |
168 | i != (u64)ULLONG_MAX; \ |
169 | __next_mem_range(&i, nid, flags, type_a, type_b, \ |
170 | p_start, p_end, p_nid)) |
171 | |
172 | /** |
173 | * for_each_mem_range_rev - reverse iterate through memblock areas from |
174 | * type_a and not included in type_b. Or just type_a if type_b is NULL. |
175 | * @i: u64 used as loop variable |
176 | * @type_a: ptr to memblock_type to iterate |
177 | * @type_b: ptr to memblock_type which excludes from the iteration |
178 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
179 | * @flags: pick from blocks based on memory attributes |
180 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
181 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
182 | * @p_nid: ptr to int for nid of the range, can be %NULL |
183 | */ |
184 | #define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \ |
185 | p_start, p_end, p_nid) \ |
186 | for (i = (u64)ULLONG_MAX, \ |
187 | __next_mem_range_rev(&i, nid, flags, type_a, type_b,\ |
188 | p_start, p_end, p_nid); \ |
189 | i != (u64)ULLONG_MAX; \ |
190 | __next_mem_range_rev(&i, nid, flags, type_a, type_b, \ |
191 | p_start, p_end, p_nid)) |
192 | |
193 | /** |
194 | * for_each_reserved_mem_region - iterate over all reserved memblock areas |
195 | * @i: u64 used as loop variable |
196 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
197 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
198 | * |
199 | * Walks over reserved areas of memblock. Available as soon as memblock |
200 | * is initialized. |
201 | */ |
202 | #define for_each_reserved_mem_region(i, p_start, p_end) \ |
203 | for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \ |
204 | i != (u64)ULLONG_MAX; \ |
205 | __next_reserved_mem_region(&i, p_start, p_end)) |
206 | |
207 | static inline bool memblock_is_hotpluggable(struct memblock_region *m) |
208 | { |
209 | return m->flags & MEMBLOCK_HOTPLUG; |
210 | } |
211 | |
212 | static inline bool memblock_is_mirror(struct memblock_region *m) |
213 | { |
214 | return m->flags & MEMBLOCK_MIRROR; |
215 | } |
216 | |
217 | static inline bool memblock_is_nomap(struct memblock_region *m) |
218 | { |
219 | return m->flags & MEMBLOCK_NOMAP; |
220 | } |
221 | |
222 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
223 | int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn, |
224 | unsigned long *end_pfn); |
225 | void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn, |
226 | unsigned long *out_end_pfn, int *out_nid); |
227 | |
228 | /** |
229 | * for_each_mem_pfn_range - early memory pfn range iterator |
230 | * @i: an integer used as loop variable |
231 | * @nid: node selector, %MAX_NUMNODES for all nodes |
232 | * @p_start: ptr to ulong for start pfn of the range, can be %NULL |
233 | * @p_end: ptr to ulong for end pfn of the range, can be %NULL |
234 | * @p_nid: ptr to int for nid of the range, can be %NULL |
235 | * |
236 | * Walks over configured memory ranges. |
237 | */ |
238 | #define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \ |
239 | for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \ |
240 | i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid)) |
241 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
242 | |
243 | /** |
244 | * for_each_free_mem_range - iterate through free memblock areas |
245 | * @i: u64 used as loop variable |
246 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
247 | * @flags: pick from blocks based on memory attributes |
248 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
249 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
250 | * @p_nid: ptr to int for nid of the range, can be %NULL |
251 | * |
252 | * Walks over free (memory && !reserved) areas of memblock. Available as |
253 | * soon as memblock is initialized. |
254 | */ |
255 | #define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \ |
256 | for_each_mem_range(i, &memblock.memory, &memblock.reserved, \ |
257 | nid, flags, p_start, p_end, p_nid) |
258 | |
259 | /** |
260 | * for_each_free_mem_range_reverse - rev-iterate through free memblock areas |
261 | * @i: u64 used as loop variable |
262 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
263 | * @flags: pick from blocks based on memory attributes |
264 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
265 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
266 | * @p_nid: ptr to int for nid of the range, can be %NULL |
267 | * |
268 | * Walks over free (memory && !reserved) areas of memblock in reverse |
269 | * order. Available as soon as memblock is initialized. |
270 | */ |
271 | #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \ |
272 | p_nid) \ |
273 | for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \ |
274 | nid, flags, p_start, p_end, p_nid) |
275 | |
276 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
277 | int memblock_set_node(phys_addr_t base, phys_addr_t size, |
278 | struct memblock_type *type, int nid); |
279 | |
280 | static inline void memblock_set_region_node(struct memblock_region *r, int nid) |
281 | { |
282 | r->nid = nid; |
283 | } |
284 | |
285 | static inline int memblock_get_region_node(const struct memblock_region *r) |
286 | { |
287 | return r->nid; |
288 | } |
289 | #else |
290 | static inline void memblock_set_region_node(struct memblock_region *r, int nid) |
291 | { |
292 | } |
293 | |
294 | static inline int memblock_get_region_node(const struct memblock_region *r) |
295 | { |
296 | return 0; |
297 | } |
298 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
299 | |
300 | /* Flags for memblock allocation APIs */ |
301 | #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0) |
302 | #define MEMBLOCK_ALLOC_ACCESSIBLE 0 |
303 | #define MEMBLOCK_ALLOC_KASAN 1 |
304 | |
305 | /* We are using top down, so it is safe to use 0 here */ |
306 | #define MEMBLOCK_LOW_LIMIT 0 |
307 | |
308 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
309 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL |
310 | #endif |
311 | |
312 | phys_addr_t memblock_phys_alloc_range(phys_addr_t size, phys_addr_t align, |
313 | phys_addr_t start, phys_addr_t end); |
314 | phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid); |
315 | |
316 | static inline phys_addr_t memblock_phys_alloc(phys_addr_t size, |
317 | phys_addr_t align) |
318 | { |
319 | return memblock_phys_alloc_range(size, align, 0, |
320 | MEMBLOCK_ALLOC_ACCESSIBLE); |
321 | } |
322 | |
323 | void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align, |
324 | phys_addr_t min_addr, phys_addr_t max_addr, |
325 | int nid); |
326 | void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, |
327 | phys_addr_t min_addr, phys_addr_t max_addr, |
328 | int nid); |
329 | |
330 | static inline void * __init memblock_alloc(phys_addr_t size, phys_addr_t align) |
331 | { |
332 | return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT, |
333 | MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE); |
334 | } |
335 | |
336 | static inline void * __init memblock_alloc_raw(phys_addr_t size, |
337 | phys_addr_t align) |
338 | { |
339 | return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT, |
340 | MEMBLOCK_ALLOC_ACCESSIBLE, |
341 | NUMA_NO_NODE); |
342 | } |
343 | |
344 | static inline void * __init memblock_alloc_from(phys_addr_t size, |
345 | phys_addr_t align, |
346 | phys_addr_t min_addr) |
347 | { |
348 | return memblock_alloc_try_nid(size, align, min_addr, |
349 | MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE); |
350 | } |
351 | |
352 | static inline void * __init memblock_alloc_low(phys_addr_t size, |
353 | phys_addr_t align) |
354 | { |
355 | return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT, |
356 | ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE); |
357 | } |
358 | |
359 | static inline void * __init memblock_alloc_node(phys_addr_t size, |
360 | phys_addr_t align, int nid) |
361 | { |
362 | return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT, |
363 | MEMBLOCK_ALLOC_ACCESSIBLE, nid); |
364 | } |
365 | |
366 | static inline void __init memblock_free_early(phys_addr_t base, |
367 | phys_addr_t size) |
368 | { |
369 | memblock_free(base, size); |
370 | } |
371 | |
372 | static inline void __init memblock_free_early_nid(phys_addr_t base, |
373 | phys_addr_t size, int nid) |
374 | { |
375 | memblock_free(base, size); |
376 | } |
377 | |
378 | static inline void __init memblock_free_late(phys_addr_t base, phys_addr_t size) |
379 | { |
380 | __memblock_free_late(base, size); |
381 | } |
382 | |
383 | /* |
384 | * Set the allocation direction to bottom-up or top-down. |
385 | */ |
386 | static inline void __init memblock_set_bottom_up(bool enable) |
387 | { |
388 | memblock.bottom_up = enable; |
389 | } |
390 | |
391 | /* |
392 | * Check if the allocation direction is bottom-up or not. |
393 | * if this is true, that said, memblock will allocate memory |
394 | * in bottom-up direction. |
395 | */ |
396 | static inline bool memblock_bottom_up(void) |
397 | { |
398 | return memblock.bottom_up; |
399 | } |
400 | |
401 | phys_addr_t memblock_phys_mem_size(void); |
402 | phys_addr_t memblock_reserved_size(void); |
403 | phys_addr_t memblock_mem_size(unsigned long limit_pfn); |
404 | phys_addr_t memblock_start_of_DRAM(void); |
405 | phys_addr_t memblock_end_of_DRAM(void); |
406 | void memblock_enforce_memory_limit(phys_addr_t memory_limit); |
407 | void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size); |
408 | void memblock_mem_limit_remove_map(phys_addr_t limit); |
409 | bool memblock_is_memory(phys_addr_t addr); |
410 | bool memblock_is_map_memory(phys_addr_t addr); |
411 | bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size); |
412 | bool memblock_is_reserved(phys_addr_t addr); |
413 | bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size); |
414 | |
415 | extern void __memblock_dump_all(void); |
416 | |
417 | static inline void memblock_dump_all(void) |
418 | { |
419 | if (memblock_debug) |
420 | __memblock_dump_all(); |
421 | } |
422 | |
423 | /** |
424 | * memblock_set_current_limit - Set the current allocation limit to allow |
425 | * limiting allocations to what is currently |
426 | * accessible during boot |
427 | * @limit: New limit value (physical address) |
428 | */ |
429 | void memblock_set_current_limit(phys_addr_t limit); |
430 | |
431 | |
432 | phys_addr_t memblock_get_current_limit(void); |
433 | |
434 | /* |
435 | * pfn conversion functions |
436 | * |
437 | * While the memory MEMBLOCKs should always be page aligned, the reserved |
438 | * MEMBLOCKs may not be. This accessor attempt to provide a very clear |
439 | * idea of what they return for such non aligned MEMBLOCKs. |
440 | */ |
441 | |
442 | /** |
443 | * memblock_region_memory_base_pfn - get the lowest pfn of the memory region |
444 | * @reg: memblock_region structure |
445 | * |
446 | * Return: the lowest pfn intersecting with the memory region |
447 | */ |
448 | static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg) |
449 | { |
450 | return PFN_UP(reg->base); |
451 | } |
452 | |
453 | /** |
454 | * memblock_region_memory_end_pfn - get the end pfn of the memory region |
455 | * @reg: memblock_region structure |
456 | * |
457 | * Return: the end_pfn of the reserved region |
458 | */ |
459 | static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg) |
460 | { |
461 | return PFN_DOWN(reg->base + reg->size); |
462 | } |
463 | |
464 | /** |
465 | * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region |
466 | * @reg: memblock_region structure |
467 | * |
468 | * Return: the lowest pfn intersecting with the reserved region |
469 | */ |
470 | static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg) |
471 | { |
472 | return PFN_DOWN(reg->base); |
473 | } |
474 | |
475 | /** |
476 | * memblock_region_reserved_end_pfn - get the end pfn of the reserved region |
477 | * @reg: memblock_region structure |
478 | * |
479 | * Return: the end_pfn of the reserved region |
480 | */ |
481 | static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg) |
482 | { |
483 | return PFN_UP(reg->base + reg->size); |
484 | } |
485 | |
486 | #define for_each_memblock(memblock_type, region) \ |
487 | for (region = memblock.memblock_type.regions; \ |
488 | region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \ |
489 | region++) |
490 | |
491 | #define for_each_memblock_type(i, memblock_type, rgn) \ |
492 | for (i = 0, rgn = &memblock_type->regions[0]; \ |
493 | i < memblock_type->cnt; \ |
494 | i++, rgn = &memblock_type->regions[i]) |
495 | |
496 | extern void *alloc_large_system_hash(const char *tablename, |
497 | unsigned long bucketsize, |
498 | unsigned long numentries, |
499 | int scale, |
500 | int flags, |
501 | unsigned int *_hash_shift, |
502 | unsigned int *_hash_mask, |
503 | unsigned long low_limit, |
504 | unsigned long high_limit); |
505 | |
506 | #define HASH_EARLY 0x00000001 /* Allocating during early boot? */ |
507 | #define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min |
508 | * shift passed via *_hash_shift */ |
509 | #define HASH_ZERO 0x00000004 /* Zero allocated hash table */ |
510 | |
511 | /* Only NUMA needs hash distribution. 64bit NUMA architectures have |
512 | * sufficient vmalloc space. |
513 | */ |
514 | #ifdef CONFIG_NUMA |
515 | #define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT) |
516 | extern int hashdist; /* Distribute hashes across NUMA nodes? */ |
517 | #else |
518 | #define hashdist (0) |
519 | #endif |
520 | |
521 | #ifdef CONFIG_MEMTEST |
522 | extern void early_memtest(phys_addr_t start, phys_addr_t end); |
523 | #else |
524 | static inline void early_memtest(phys_addr_t start, phys_addr_t end) |
525 | { |
526 | } |
527 | #endif |
528 | |
529 | #endif /* __KERNEL__ */ |
530 | |
531 | #endif /* _LINUX_MEMBLOCK_H */ |
532 | |