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