at v5.0 19 kB view raw
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 20extern unsigned long max_low_pfn; 21extern unsigned long min_low_pfn; 22 23/* 24 * highest page 25 */ 26extern unsigned long max_pfn; 27/* 28 * highest possible page 29 */ 30extern 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 */ 39enum 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 */ 53struct 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 */ 70struct 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 */ 86struct 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 96extern struct memblock memblock; 97extern int memblock_debug; 98 99#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK 100#define __init_memblock __meminit 101#define __initdata_memblock __meminitdata 102void 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 111phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align, 112 phys_addr_t start, phys_addr_t end, 113 int nid, enum memblock_flags flags); 114phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end, 115 phys_addr_t size, phys_addr_t align); 116void memblock_allow_resize(void); 117int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid); 118int memblock_add(phys_addr_t base, phys_addr_t size); 119int memblock_remove(phys_addr_t base, phys_addr_t size); 120int memblock_free(phys_addr_t base, phys_addr_t size); 121int memblock_reserve(phys_addr_t base, phys_addr_t size); 122void memblock_trim_memory(phys_addr_t align); 123bool memblock_overlaps_region(struct memblock_type *type, 124 phys_addr_t base, phys_addr_t size); 125int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size); 126int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size); 127int memblock_mark_mirror(phys_addr_t base, phys_addr_t size); 128int memblock_mark_nomap(phys_addr_t base, phys_addr_t size); 129int memblock_clear_nomap(phys_addr_t base, phys_addr_t size); 130enum memblock_flags choose_memblock_flags(void); 131 132unsigned long memblock_free_all(void); 133void reset_node_managed_pages(pg_data_t *pgdat); 134void reset_all_zones_managed_pages(void); 135 136/* Low level functions */ 137int memblock_add_range(struct memblock_type *type, 138 phys_addr_t base, phys_addr_t size, 139 int nid, enum memblock_flags flags); 140 141void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags, 142 struct memblock_type *type_a, 143 struct memblock_type *type_b, phys_addr_t *out_start, 144 phys_addr_t *out_end, int *out_nid); 145 146void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags, 147 struct memblock_type *type_a, 148 struct memblock_type *type_b, phys_addr_t *out_start, 149 phys_addr_t *out_end, int *out_nid); 150 151void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start, 152 phys_addr_t *out_end); 153 154void __memblock_free_late(phys_addr_t base, phys_addr_t size); 155 156/** 157 * for_each_mem_range - iterate through memblock areas from type_a and not 158 * included in type_b. Or just type_a if type_b is NULL. 159 * @i: u64 used as loop variable 160 * @type_a: ptr to memblock_type to iterate 161 * @type_b: ptr to memblock_type which excludes from the iteration 162 * @nid: node selector, %NUMA_NO_NODE for all nodes 163 * @flags: pick from blocks based on memory attributes 164 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL 165 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL 166 * @p_nid: ptr to int for nid of the range, can be %NULL 167 */ 168#define for_each_mem_range(i, type_a, type_b, nid, flags, \ 169 p_start, p_end, p_nid) \ 170 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \ 171 p_start, p_end, p_nid); \ 172 i != (u64)ULLONG_MAX; \ 173 __next_mem_range(&i, nid, flags, type_a, type_b, \ 174 p_start, p_end, p_nid)) 175 176/** 177 * for_each_mem_range_rev - reverse iterate through memblock areas from 178 * type_a and not included in type_b. Or just type_a if type_b is NULL. 179 * @i: u64 used as loop variable 180 * @type_a: ptr to memblock_type to iterate 181 * @type_b: ptr to memblock_type which excludes from the iteration 182 * @nid: node selector, %NUMA_NO_NODE for all nodes 183 * @flags: pick from blocks based on memory attributes 184 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL 185 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL 186 * @p_nid: ptr to int for nid of the range, can be %NULL 187 */ 188#define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \ 189 p_start, p_end, p_nid) \ 190 for (i = (u64)ULLONG_MAX, \ 191 __next_mem_range_rev(&i, nid, flags, type_a, type_b,\ 192 p_start, p_end, p_nid); \ 193 i != (u64)ULLONG_MAX; \ 194 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \ 195 p_start, p_end, p_nid)) 196 197/** 198 * for_each_reserved_mem_region - iterate over all reserved memblock areas 199 * @i: u64 used as loop variable 200 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL 201 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL 202 * 203 * Walks over reserved areas of memblock. Available as soon as memblock 204 * is initialized. 205 */ 206#define for_each_reserved_mem_region(i, p_start, p_end) \ 207 for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \ 208 i != (u64)ULLONG_MAX; \ 209 __next_reserved_mem_region(&i, p_start, p_end)) 210 211static inline bool memblock_is_hotpluggable(struct memblock_region *m) 212{ 213 return m->flags & MEMBLOCK_HOTPLUG; 214} 215 216static inline bool memblock_is_mirror(struct memblock_region *m) 217{ 218 return m->flags & MEMBLOCK_MIRROR; 219} 220 221static inline bool memblock_is_nomap(struct memblock_region *m) 222{ 223 return m->flags & MEMBLOCK_NOMAP; 224} 225 226#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 227int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn, 228 unsigned long *end_pfn); 229void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn, 230 unsigned long *out_end_pfn, int *out_nid); 231 232/** 233 * for_each_mem_pfn_range - early memory pfn range iterator 234 * @i: an integer used as loop variable 235 * @nid: node selector, %MAX_NUMNODES for all nodes 236 * @p_start: ptr to ulong for start pfn of the range, can be %NULL 237 * @p_end: ptr to ulong for end pfn of the range, can be %NULL 238 * @p_nid: ptr to int for nid of the range, can be %NULL 239 * 240 * Walks over configured memory ranges. 241 */ 242#define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \ 243 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \ 244 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid)) 245#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ 246 247/** 248 * for_each_free_mem_range - iterate through free memblock areas 249 * @i: u64 used as loop variable 250 * @nid: node selector, %NUMA_NO_NODE for all nodes 251 * @flags: pick from blocks based on memory attributes 252 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL 253 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL 254 * @p_nid: ptr to int for nid of the range, can be %NULL 255 * 256 * Walks over free (memory && !reserved) areas of memblock. Available as 257 * soon as memblock is initialized. 258 */ 259#define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \ 260 for_each_mem_range(i, &memblock.memory, &memblock.reserved, \ 261 nid, flags, p_start, p_end, p_nid) 262 263/** 264 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas 265 * @i: u64 used as loop variable 266 * @nid: node selector, %NUMA_NO_NODE for all nodes 267 * @flags: pick from blocks based on memory attributes 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 * @p_nid: ptr to int for nid of the range, can be %NULL 271 * 272 * Walks over free (memory && !reserved) areas of memblock in reverse 273 * order. Available as soon as memblock is initialized. 274 */ 275#define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \ 276 p_nid) \ 277 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \ 278 nid, flags, p_start, p_end, p_nid) 279 280static inline void memblock_set_region_flags(struct memblock_region *r, 281 enum memblock_flags flags) 282{ 283 r->flags |= flags; 284} 285 286static inline void memblock_clear_region_flags(struct memblock_region *r, 287 enum memblock_flags flags) 288{ 289 r->flags &= ~flags; 290} 291 292#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 293int memblock_set_node(phys_addr_t base, phys_addr_t size, 294 struct memblock_type *type, int nid); 295 296static inline void memblock_set_region_node(struct memblock_region *r, int nid) 297{ 298 r->nid = nid; 299} 300 301static inline int memblock_get_region_node(const struct memblock_region *r) 302{ 303 return r->nid; 304} 305#else 306static inline void memblock_set_region_node(struct memblock_region *r, int nid) 307{ 308} 309 310static inline int memblock_get_region_node(const struct memblock_region *r) 311{ 312 return 0; 313} 314#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ 315 316/* Flags for memblock allocation APIs */ 317#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0) 318#define MEMBLOCK_ALLOC_ACCESSIBLE 0 319#define MEMBLOCK_ALLOC_KASAN 1 320 321/* We are using top down, so it is safe to use 0 here */ 322#define MEMBLOCK_LOW_LIMIT 0 323 324#ifndef ARCH_LOW_ADDRESS_LIMIT 325#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL 326#endif 327 328phys_addr_t memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid); 329phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid); 330 331phys_addr_t memblock_phys_alloc(phys_addr_t size, phys_addr_t align); 332 333void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align, 334 phys_addr_t min_addr, phys_addr_t max_addr, 335 int nid); 336void *memblock_alloc_try_nid_nopanic(phys_addr_t size, phys_addr_t align, 337 phys_addr_t min_addr, phys_addr_t max_addr, 338 int nid); 339void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, 340 phys_addr_t min_addr, phys_addr_t max_addr, 341 int nid); 342 343static inline void * __init memblock_alloc(phys_addr_t size, phys_addr_t align) 344{ 345 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT, 346 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE); 347} 348 349static inline void * __init memblock_alloc_raw(phys_addr_t size, 350 phys_addr_t align) 351{ 352 return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT, 353 MEMBLOCK_ALLOC_ACCESSIBLE, 354 NUMA_NO_NODE); 355} 356 357static inline void * __init memblock_alloc_from(phys_addr_t size, 358 phys_addr_t align, 359 phys_addr_t min_addr) 360{ 361 return memblock_alloc_try_nid(size, align, min_addr, 362 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE); 363} 364 365static inline void * __init memblock_alloc_nopanic(phys_addr_t size, 366 phys_addr_t align) 367{ 368 return memblock_alloc_try_nid_nopanic(size, align, MEMBLOCK_LOW_LIMIT, 369 MEMBLOCK_ALLOC_ACCESSIBLE, 370 NUMA_NO_NODE); 371} 372 373static inline void * __init memblock_alloc_low(phys_addr_t size, 374 phys_addr_t align) 375{ 376 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT, 377 ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE); 378} 379static inline void * __init memblock_alloc_low_nopanic(phys_addr_t size, 380 phys_addr_t align) 381{ 382 return memblock_alloc_try_nid_nopanic(size, align, MEMBLOCK_LOW_LIMIT, 383 ARCH_LOW_ADDRESS_LIMIT, 384 NUMA_NO_NODE); 385} 386 387static inline void * __init memblock_alloc_from_nopanic(phys_addr_t size, 388 phys_addr_t align, 389 phys_addr_t min_addr) 390{ 391 return memblock_alloc_try_nid_nopanic(size, align, min_addr, 392 MEMBLOCK_ALLOC_ACCESSIBLE, 393 NUMA_NO_NODE); 394} 395 396static inline void * __init memblock_alloc_node(phys_addr_t size, 397 phys_addr_t align, int nid) 398{ 399 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT, 400 MEMBLOCK_ALLOC_ACCESSIBLE, nid); 401} 402 403static inline void * __init memblock_alloc_node_nopanic(phys_addr_t size, 404 int nid) 405{ 406 return memblock_alloc_try_nid_nopanic(size, SMP_CACHE_BYTES, 407 MEMBLOCK_LOW_LIMIT, 408 MEMBLOCK_ALLOC_ACCESSIBLE, nid); 409} 410 411static inline void __init memblock_free_early(phys_addr_t base, 412 phys_addr_t size) 413{ 414 memblock_free(base, size); 415} 416 417static inline void __init memblock_free_early_nid(phys_addr_t base, 418 phys_addr_t size, int nid) 419{ 420 memblock_free(base, size); 421} 422 423static inline void __init memblock_free_late(phys_addr_t base, phys_addr_t size) 424{ 425 __memblock_free_late(base, size); 426} 427 428/* 429 * Set the allocation direction to bottom-up or top-down. 430 */ 431static inline void __init memblock_set_bottom_up(bool enable) 432{ 433 memblock.bottom_up = enable; 434} 435 436/* 437 * Check if the allocation direction is bottom-up or not. 438 * if this is true, that said, memblock will allocate memory 439 * in bottom-up direction. 440 */ 441static inline bool memblock_bottom_up(void) 442{ 443 return memblock.bottom_up; 444} 445 446phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align, 447 phys_addr_t start, phys_addr_t end, 448 enum memblock_flags flags); 449phys_addr_t memblock_alloc_base_nid(phys_addr_t size, 450 phys_addr_t align, phys_addr_t max_addr, 451 int nid, enum memblock_flags flags); 452phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align, 453 phys_addr_t max_addr); 454phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align, 455 phys_addr_t max_addr); 456phys_addr_t memblock_phys_mem_size(void); 457phys_addr_t memblock_reserved_size(void); 458phys_addr_t memblock_mem_size(unsigned long limit_pfn); 459phys_addr_t memblock_start_of_DRAM(void); 460phys_addr_t memblock_end_of_DRAM(void); 461void memblock_enforce_memory_limit(phys_addr_t memory_limit); 462void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size); 463void memblock_mem_limit_remove_map(phys_addr_t limit); 464bool memblock_is_memory(phys_addr_t addr); 465bool memblock_is_map_memory(phys_addr_t addr); 466bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size); 467bool memblock_is_reserved(phys_addr_t addr); 468bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size); 469 470extern void __memblock_dump_all(void); 471 472static inline void memblock_dump_all(void) 473{ 474 if (memblock_debug) 475 __memblock_dump_all(); 476} 477 478/** 479 * memblock_set_current_limit - Set the current allocation limit to allow 480 * limiting allocations to what is currently 481 * accessible during boot 482 * @limit: New limit value (physical address) 483 */ 484void memblock_set_current_limit(phys_addr_t limit); 485 486 487phys_addr_t memblock_get_current_limit(void); 488 489/* 490 * pfn conversion functions 491 * 492 * While the memory MEMBLOCKs should always be page aligned, the reserved 493 * MEMBLOCKs may not be. This accessor attempt to provide a very clear 494 * idea of what they return for such non aligned MEMBLOCKs. 495 */ 496 497/** 498 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region 499 * @reg: memblock_region structure 500 * 501 * Return: the lowest pfn intersecting with the memory region 502 */ 503static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg) 504{ 505 return PFN_UP(reg->base); 506} 507 508/** 509 * memblock_region_memory_end_pfn - get the end pfn of the memory region 510 * @reg: memblock_region structure 511 * 512 * Return: the end_pfn of the reserved region 513 */ 514static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg) 515{ 516 return PFN_DOWN(reg->base + reg->size); 517} 518 519/** 520 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region 521 * @reg: memblock_region structure 522 * 523 * Return: the lowest pfn intersecting with the reserved region 524 */ 525static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg) 526{ 527 return PFN_DOWN(reg->base); 528} 529 530/** 531 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region 532 * @reg: memblock_region structure 533 * 534 * Return: the end_pfn of the reserved region 535 */ 536static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg) 537{ 538 return PFN_UP(reg->base + reg->size); 539} 540 541#define for_each_memblock(memblock_type, region) \ 542 for (region = memblock.memblock_type.regions; \ 543 region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \ 544 region++) 545 546#define for_each_memblock_type(i, memblock_type, rgn) \ 547 for (i = 0, rgn = &memblock_type->regions[0]; \ 548 i < memblock_type->cnt; \ 549 i++, rgn = &memblock_type->regions[i]) 550 551extern void *alloc_large_system_hash(const char *tablename, 552 unsigned long bucketsize, 553 unsigned long numentries, 554 int scale, 555 int flags, 556 unsigned int *_hash_shift, 557 unsigned int *_hash_mask, 558 unsigned long low_limit, 559 unsigned long high_limit); 560 561#define HASH_EARLY 0x00000001 /* Allocating during early boot? */ 562#define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min 563 * shift passed via *_hash_shift */ 564#define HASH_ZERO 0x00000004 /* Zero allocated hash table */ 565 566/* Only NUMA needs hash distribution. 64bit NUMA architectures have 567 * sufficient vmalloc space. 568 */ 569#ifdef CONFIG_NUMA 570#define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT) 571extern int hashdist; /* Distribute hashes across NUMA nodes? */ 572#else 573#define hashdist (0) 574#endif 575 576#ifdef CONFIG_MEMTEST 577extern void early_memtest(phys_addr_t start, phys_addr_t end); 578#else 579static inline void early_memtest(phys_addr_t start, phys_addr_t end) 580{ 581} 582#endif 583 584#endif /* __KERNEL__ */ 585 586#endif /* _LINUX_MEMBLOCK_H */