Lines Matching +full:memory +full:- +full:region

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Procedures for maintaining information about logical memory blocks.
40 * Memblock is a method of managing memory regions during the early
41 * boot period when the usual kernel memory allocators are not up and
44 * Memblock views the system memory as collections of contiguous
47 * * ``memory`` - describes the physical memory available to the
48 * kernel; this may differ from the actual physical memory installed
49 * in the system, for instance when the memory is restricted with
51 * * ``reserved`` - describes the regions that were allocated
52 * * ``physmem`` - describes the actual physical memory available during
53 * boot regardless of the possible restrictions and memory hot(un)plug;
56 * Each region is represented by struct memblock_region that
57 * defines the region extents, its attributes and NUMA node id on NUMA
58 * systems. Every memory type is described by the struct memblock_type
59 * which contains an array of memory regions along with
60 * the allocator metadata. The "memory" and "reserved" types are nicely
62 * initialized at build time. The region arrays are initially sized to
63 * %INIT_MEMBLOCK_MEMORY_REGIONS for "memory" and
64 * %INIT_MEMBLOCK_RESERVED_REGIONS for "reserved". The region array
66 * The memblock_allow_resize() enables automatic resizing of the region
68 * with care so that memory allocated for the region array will not
72 * memory layout is by using memblock_add() or memblock_add_node()
73 * functions. The first function does not assign the region to a NUMA
75 * use it on NUMA systems as well and assign the region to a NUMA node
79 * Once memblock is setup the memory can be allocated using one of the
82 * * memblock_phys_alloc*() - these functions return the **physical**
83 * address of the allocated memory
84 * * memblock_alloc*() - these functions return the **virtual** address
85 * of the allocated memory.
88 * memory ranges and the fallback methods. Consult the documentation
93 * function frees all the memory to the buddy page allocator.
117 .memory.regions = memblock_memory_init_regions,
118 .memory.max = INIT_MEMBLOCK_MEMORY_REGIONS,
119 .memory.name = "memory",
138 * keep a pointer to &memblock.memory in the text section to use it in
143 static __refdata struct memblock_type *memblock_memory = &memblock.memory;
146 for (i = 0, rgn = &memblock_type->regions[0]; \
147 i < memblock_type->cnt; \
148 i++, rgn = &memblock_type->regions[i])
175 return *size = min(*size, PHYS_ADDR_MAX - base); in memblock_cap_size()
195 for (i = 0; i < type->cnt; i++) in memblock_overlaps_region()
196 if (memblock_addrs_overlap(base, size, type->regions[i].base, in memblock_overlaps_region()
197 type->regions[i].size)) in memblock_overlaps_region()
203 * __memblock_find_range_bottom_up - find free area utility in bottom-up
210 * @flags: pick from blocks based on memory attributes
212 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
230 if (cand < this_end && this_end - cand >= size) in __memblock_find_range_bottom_up()
238 * __memblock_find_range_top_down - find free area utility, in top-down
245 * @flags: pick from blocks based on memory attributes
247 * Utility called from memblock_find_in_range_node(), find free area top-down.
268 cand = round_down(this_end - size, align); in __memblock_find_range_top_down()
277 * memblock_find_in_range_node - find free area in given range and node
284 * @flags: pick from blocks based on memory attributes
314 * memblock_find_in_range - find free area in given range
338 pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n", in memblock_find_in_range()
349 type->total_size -= type->regions[r].size; in memblock_remove_region()
350 memmove(&type->regions[r], &type->regions[r + 1], in memblock_remove_region()
351 (type->cnt - (r + 1)) * sizeof(type->regions[r])); in memblock_remove_region()
352 type->cnt--; in memblock_remove_region()
355 if (type->cnt == 0) { in memblock_remove_region()
356 WARN_ON(type->total_size != 0); in memblock_remove_region()
357 type->regions[0].base = 0; in memblock_remove_region()
358 type->regions[0].size = 0; in memblock_remove_region()
359 type->regions[0].flags = 0; in memblock_remove_region()
360 memblock_set_region_node(&type->regions[0], MAX_NUMNODES); in memblock_remove_region()
366 * memblock_discard - discard memory and reserved arrays if they were allocated
382 if (memblock.memory.regions != memblock_memory_init_regions) { in memblock_discard()
383 addr = __pa(memblock.memory.regions); in memblock_discard()
385 memblock.memory.max); in memblock_discard()
387 kfree(memblock.memory.regions); in memblock_discard()
397 * memblock_double_array - double the size of the memblock regions array
399 * @new_area_start: starting address of memory range to avoid overlap with
400 * @new_area_size: size of memory range to avoid overlap with
403 * allocate memory for a new reserved regions array and there is a previously
404 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
405 * waiting to be reserved, ensure the memory used by the new array does
409 * 0 on success, -1 on failure.
422 * of memory that aren't suitable for allocation in memblock_double_array()
425 panic("memblock: cannot resize %s array\n", type->name); in memblock_double_array()
428 old_size = type->max * sizeof(struct memblock_region); in memblock_double_array()
438 if (type == &memblock.memory) in memblock_double_array()
461 /* The memory may not have been accepted, yet. */ in memblock_double_array()
471 type->name, type->max, type->max * 2); in memblock_double_array()
472 return -1; in memblock_double_array()
475 new_end = addr + new_size - 1; in memblock_double_array()
476 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]", in memblock_double_array()
477 type->name, type->max * 2, &addr, &new_end); in memblock_double_array()
481 * reserved region since it may be our reserved array itself that is in memblock_double_array()
484 memcpy(new_array, type->regions, old_size); in memblock_double_array()
485 memset(new_array + type->max, 0, old_size); in memblock_double_array()
486 old_array = type->regions; in memblock_double_array()
487 type->regions = new_array; in memblock_double_array()
488 type->max <<= 1; in memblock_double_array()
511 * memblock_merge_regions - merge neighboring compatible regions
513 * @start_rgn: start scanning from (@start_rgn - 1)
514 * @end_rgn: end scanning at (@end_rgn - 1)
515 * Scan @type and merge neighboring compatible regions in [@start_rgn - 1, @end_rgn)
523 i = start_rgn - 1; in memblock_merge_regions()
524 end_rgn = min(end_rgn, type->cnt - 1); in memblock_merge_regions()
526 struct memblock_region *this = &type->regions[i]; in memblock_merge_regions()
527 struct memblock_region *next = &type->regions[i + 1]; in memblock_merge_regions()
529 if (this->base + this->size != next->base || in memblock_merge_regions()
532 this->flags != next->flags) { in memblock_merge_regions()
533 BUG_ON(this->base + this->size > next->base); in memblock_merge_regions()
538 this->size += next->size; in memblock_merge_regions()
540 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next)); in memblock_merge_regions()
541 type->cnt--; in memblock_merge_regions()
542 end_rgn--; in memblock_merge_regions()
547 * memblock_insert_region - insert new memblock region
550 * @base: base address of the new region
551 * @size: size of the new region
552 * @nid: node id of the new region
553 * @flags: flags of the new region
555 * Insert new memblock region [@base, @base + @size) into @type at @idx.
556 * @type must already have extra room to accommodate the new region.
564 struct memblock_region *rgn = &type->regions[idx]; in memblock_insert_region()
566 BUG_ON(type->cnt >= type->max); in memblock_insert_region()
567 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); in memblock_insert_region()
568 rgn->base = base; in memblock_insert_region()
569 rgn->size = size; in memblock_insert_region()
570 rgn->flags = flags; in memblock_insert_region()
572 type->cnt++; in memblock_insert_region()
573 type->total_size += size; in memblock_insert_region()
577 * memblock_add_range - add new memblock region
578 * @type: memblock type to add new region into
579 * @base: base address of the new region
580 * @size: size of the new region
581 * @nid: nid of the new region
582 * @flags: flags of the new region
584 * Add new memblock region [@base, @base + @size) into @type. The new region
585 * is allowed to overlap with existing ones - overlaps don't affect already
590 * 0 on success, -errno on failure.
599 int idx, nr_new, start_rgn = -1, end_rgn; in memblock_add_range()
606 if (type->regions[0].size == 0) { in memblock_add_range()
607 WARN_ON(type->cnt != 0 || type->total_size); in memblock_add_range()
608 type->regions[0].base = base; in memblock_add_range()
609 type->regions[0].size = size; in memblock_add_range()
610 type->regions[0].flags = flags; in memblock_add_range()
611 memblock_set_region_node(&type->regions[0], nid); in memblock_add_range()
612 type->total_size = size; in memblock_add_range()
613 type->cnt = 1; in memblock_add_range()
619 * then we'll need type->cnt + 1 empty regions in @type. So if in memblock_add_range()
620 * type->cnt * 2 + 1 is less than or equal to type->max, we know in memblock_add_range()
624 if (type->cnt * 2 + 1 <= type->max) in memblock_add_range()
637 phys_addr_t rbase = rgn->base; in memblock_add_range()
638 phys_addr_t rend = rbase + rgn->size; in memblock_add_range()
652 WARN_ON(flags != rgn->flags); in memblock_add_range()
655 if (start_rgn == -1) in memblock_add_range()
659 rbase - base, nid, in memblock_add_range()
671 if (start_rgn == -1) in memblock_add_range()
674 memblock_insert_region(type, idx, base, end - base, in memblock_add_range()
687 while (type->cnt + nr_new > type->max) in memblock_add_range()
689 return -ENOMEM; in memblock_add_range()
699 * memblock_add_node - add new memblock region within a NUMA node
700 * @base: base address of the new region
701 * @size: size of the new region
702 * @nid: nid of the new region
703 * @flags: flags of the new region
705 * Add new memblock region [@base, @base + @size) to the "memory"
709 * 0 on success, -errno on failure.
714 phys_addr_t end = base + size - 1; in memblock_add_node()
716 memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__, in memblock_add_node()
719 return memblock_add_range(&memblock.memory, base, size, nid, flags); in memblock_add_node()
723 * memblock_add - add new memblock region
724 * @base: base address of the new region
725 * @size: size of the new region
727 * Add new memblock region [@base, @base + @size) to the "memory"
731 * 0 on success, -errno on failure.
735 phys_addr_t end = base + size - 1; in memblock_add()
737 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, in memblock_add()
740 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0); in memblock_add()
744 * memblock_validate_numa_coverage - check if amount of memory with
746 * @threshold_bytes: maximal memory size that can have unassigned node
749 * A buggy firmware may report memory that does not belong to any node.
750 * Check if amount of such memory is below @threshold_bytes.
763 nr_pages += end_pfn - start_pfn; in memblock_validate_numa_coverage()
778 * memblock_isolate_range - isolate given range into disjoint memblocks
782 * @start_rgn: out parameter for the start of isolated region
783 * @end_rgn: out parameter for the end of isolated region
788 * region inside the range is returned in *@start_rgn and the index of the
789 * first region after the range is returned in *@end_rgn.
792 * 0 on success, -errno on failure.
808 while (type->cnt + 2 > type->max) in memblock_isolate_range()
810 return -ENOMEM; in memblock_isolate_range()
813 phys_addr_t rbase = rgn->base; in memblock_isolate_range()
814 phys_addr_t rend = rbase + rgn->size; in memblock_isolate_range()
824 * to process the next region - the new top half. in memblock_isolate_range()
826 rgn->base = base; in memblock_isolate_range()
827 rgn->size -= base - rbase; in memblock_isolate_range()
828 type->total_size -= base - rbase; in memblock_isolate_range()
829 memblock_insert_region(type, idx, rbase, base - rbase, in memblock_isolate_range()
831 rgn->flags); in memblock_isolate_range()
835 * current region - the new bottom half. in memblock_isolate_range()
837 rgn->base = end; in memblock_isolate_range()
838 rgn->size -= end - rbase; in memblock_isolate_range()
839 type->total_size -= end - rbase; in memblock_isolate_range()
840 memblock_insert_region(type, idx--, rbase, end - rbase, in memblock_isolate_range()
842 rgn->flags); in memblock_isolate_range()
864 for (i = end_rgn - 1; i >= start_rgn; i--) in memblock_remove_range()
871 phys_addr_t end = base + size - 1; in memblock_remove()
873 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, in memblock_remove()
876 return memblock_remove_range(&memblock.memory, base, size); in memblock_remove()
880 * memblock_free - free boot memory allocation
881 * @ptr: starting address of the boot memory allocation
882 * @size: size of the boot memory block in bytes
884 * Free boot memory block previously allocated by memblock_alloc_xx() API.
885 * The freeing memory will not be released to the buddy allocator.
894 * memblock_phys_free - free boot memory block
895 * @base: phys starting address of the boot memory block
896 * @size: size of the boot memory block in bytes
898 * Free boot memory block previously allocated by memblock_phys_alloc_xx() API.
899 * The freeing memory will not be released to the buddy allocator.
903 phys_addr_t end = base + size - 1; in memblock_phys_free()
905 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, in memblock_phys_free()
914 phys_addr_t end = base + size - 1; in memblock_reserve()
916 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, in memblock_reserve()
925 phys_addr_t end = base + size - 1; in memblock_physmem_add()
927 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, in memblock_physmem_add()
935 * memblock_setclr_flag - set or clear flag for a memory region
937 * @base: base address of the region
938 * @size: size of the region
942 * This function isolates region [@base, @base + @size), and sets/clears flag
944 * Return: 0 on success, -errno on failure.
956 struct memblock_region *r = &type->regions[i]; in memblock_setclr_flag()
959 r->flags |= flag; in memblock_setclr_flag()
961 r->flags &= ~flag; in memblock_setclr_flag()
969 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
970 * @base: the base phys addr of the region
971 * @size: the size of the region
973 * Return: 0 on success, -errno on failure.
977 return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_HOTPLUG); in memblock_mark_hotplug()
981 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
982 * @base: the base phys addr of the region
983 * @size: the size of the region
985 * Return: 0 on success, -errno on failure.
989 return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_HOTPLUG); in memblock_clear_hotplug()
993 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
994 * @base: the base phys addr of the region
995 * @size: the size of the region
997 * Return: 0 on success, -errno on failure.
1006 return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_MIRROR); in memblock_mark_mirror()
1010 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
1011 * @base: the base phys addr of the region
1012 * @size: the size of the region
1014 * The memory regions marked with %MEMBLOCK_NOMAP will not be added to the
1015 * direct mapping of the physical memory. These regions will still be
1016 * covered by the memory map. The struct page representing NOMAP memory
1017 * frames in the memory map will be PageReserved()
1019 * Note: if the memory being marked %MEMBLOCK_NOMAP was allocated from
1020 * memblock, the caller must inform kmemleak to ignore that memory
1022 * Return: 0 on success, -errno on failure.
1026 return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP); in memblock_mark_nomap()
1030 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
1031 * @base: the base phys addr of the region
1032 * @size: the size of the region
1034 * Return: 0 on success, -errno on failure.
1038 return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_NOMAP); in memblock_clear_nomap()
1042 * memblock_reserved_mark_noinit - Mark a reserved memory region with flag
1044 * for this region.
1045 * @base: the base phys addr of the region
1046 * @size: the size of the region
1048 * struct pages will not be initialized for reserved memory regions marked with
1051 * Return: 0 on success, -errno on failure.
1069 /* only memory regions are associated with nodes, check it */ in should_skip_region()
1073 /* skip hotpluggable memory regions if needed */ in should_skip_region()
1078 /* if we want mirror memory skip non-mirror memory regions */ in should_skip_region()
1082 /* skip nomap memory unless we were asked for it explicitly */ in should_skip_region()
1086 /* skip driver-managed memory unless we were asked for it explicitly */ in should_skip_region()
1094 * __next_mem_range - next function for for_each_free_mem_range() etc.
1097 * @flags: pick from blocks based on memory attributes
1099 * @type_b: pointer to memblock_type which excludes memory from being taken
1107 * areas before each region in type_b. For example, if type_b regions
1110 * 0:[0-16), 1:[32-48), 2:[128-130)
1114 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
1116 * As both region arrays are sorted, the function advances the two indices
1127 for (; idx_a < type_a->cnt; idx_a++) { in __next_mem_range()
1128 struct memblock_region *m = &type_a->regions[idx_a]; in __next_mem_range()
1130 phys_addr_t m_start = m->base; in __next_mem_range()
1131 phys_addr_t m_end = m->base + m->size; in __next_mem_range()
1150 for (; idx_b < type_b->cnt + 1; idx_b++) { in __next_mem_range()
1155 r = &type_b->regions[idx_b]; in __next_mem_range()
1156 r_start = idx_b ? r[-1].base + r[-1].size : 0; in __next_mem_range()
1157 r_end = idx_b < type_b->cnt ? in __next_mem_range()
1158 r->base : PHYS_ADDR_MAX; in __next_mem_range()
1176 * The region which ends first is in __next_mem_range()
1194 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1198 * @flags: pick from blocks based on memory attributes
1200 * @type_b: pointer to memblock_type which excludes memory from being taken
1221 idx_a = type_a->cnt - 1; in __next_mem_range_rev()
1223 idx_b = type_b->cnt; in __next_mem_range_rev()
1228 for (; idx_a >= 0; idx_a--) { in __next_mem_range_rev()
1229 struct memblock_region *m = &type_a->regions[idx_a]; in __next_mem_range_rev()
1231 phys_addr_t m_start = m->base; in __next_mem_range_rev()
1232 phys_addr_t m_end = m->base + m->size; in __next_mem_range_rev()
1245 idx_a--; in __next_mem_range_rev()
1251 for (; idx_b >= 0; idx_b--) { in __next_mem_range_rev()
1256 r = &type_b->regions[idx_b]; in __next_mem_range_rev()
1257 r_start = idx_b ? r[-1].base + r[-1].size : 0; in __next_mem_range_rev()
1258 r_end = idx_b < type_b->cnt ? in __next_mem_range_rev()
1259 r->base : PHYS_ADDR_MAX; in __next_mem_range_rev()
1276 idx_a--; in __next_mem_range_rev()
1278 idx_b--; in __next_mem_range_rev()
1295 struct memblock_type *type = &memblock.memory; in __next_mem_pfn_range()
1299 while (++*idx < type->cnt) { in __next_mem_pfn_range()
1300 r = &type->regions[*idx]; in __next_mem_pfn_range()
1303 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) in __next_mem_pfn_range()
1308 if (*idx >= type->cnt) { in __next_mem_pfn_range()
1309 *idx = -1; in __next_mem_pfn_range()
1314 *out_start_pfn = PFN_UP(r->base); in __next_mem_pfn_range()
1316 *out_end_pfn = PFN_DOWN(r->base + r->size); in __next_mem_pfn_range()
1322 * memblock_set_node - set node ID on memblock regions
1332 * 0 on success, -errno on failure.
1346 memblock_set_region_node(&type->regions[i], nid); in memblock_set_node()
1355 * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1358 * @zone: zone in which all of the memory blocks reside
1364 * deferred memory init routines and as such we were duplicating much of
1377 &memblock.memory, &memblock.reserved, in __next_mem_pfn_range_in_zone()
1388 if (zone->zone_start_pfn < epfn && spfn < epfn) { in __next_mem_pfn_range_in_zone()
1396 *out_spfn = max(zone->zone_start_pfn, spfn); in __next_mem_pfn_range_in_zone()
1404 &memblock.memory, &memblock.reserved, in __next_mem_pfn_range_in_zone()
1418 * memblock_alloc_range_nid - allocate boot memory block
1419 * @size: size of memory block to be allocated in bytes
1420 * @align: alignment of the region and block's size
1421 * @start: the lower bound of the memory region to allocate (phys address)
1422 * @end: the upper bound of the memory region to allocate (phys address)
1426 * The allocation is performed from memory region limited by
1429 * If the specified node can not hold the requested memory and @exact_nid
1432 * For systems with memory mirroring, the allocation is attempted first
1434 * memory region.
1437 * memory block, it is never reported as leaks.
1440 * Physical address of allocated memory block on success, %0 on failure.
1483 pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n", in memblock_alloc_range_nid()
1505 * Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP, in memblock_alloc_range_nid()
1506 * require memory to be accepted before it can be used by the in memblock_alloc_range_nid()
1509 * Accept the memory of the allocated buffer. in memblock_alloc_range_nid()
1517 * memblock_phys_alloc_range - allocate a memory block inside specified range
1518 * @size: size of memory block to be allocated in bytes
1519 * @align: alignment of the region and block's size
1520 * @start: the lower bound of the memory region to allocate (physical address)
1521 * @end: the upper bound of the memory region to allocate (physical address)
1525 * Return: physical address of the allocated memory block on success,
1541 * memblock_phys_alloc_try_nid - allocate a memory block from specified NUMA node
1542 * @size: size of memory block to be allocated in bytes
1543 * @align: alignment of the region and block's size
1546 * Allocates memory block from the specified NUMA node. If the node
1547 * has no available memory, attempts to allocated from any node in the
1550 * Return: physical address of the allocated memory block on success,
1560 * memblock_alloc_internal - allocate boot memory block
1561 * @size: size of memory block to be allocated in bytes
1562 * @align: alignment of the region and block's size
1563 * @min_addr: the lower bound of the memory region to allocate (phys address)
1564 * @max_addr: the upper bound of the memory region to allocate (phys address)
1568 * Allocates memory block using memblock_alloc_range_nid() and
1572 * will fall back to memory below @min_addr. Other constraints, such
1573 * as node and mirrored memory will be handled again in
1577 * Virtual address of allocated memory block on success, NULL on failure.
1605 * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
1606 * without zeroing memory
1607 * @size: size of memory block to be allocated in bytes
1608 * @align: alignment of the region and block's size
1609 * @min_addr: the lower bound of the memory region from where the allocation
1611 * @max_addr: the upper bound of the memory region from where the allocation
1613 * allocate only from memory limited by memblock.current_limit value
1617 * info), if enabled. Does not zero allocated memory.
1620 * Virtual address of allocated memory block on success, NULL on failure.
1636 * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1637 * memory and without panicking
1638 * @size: size of memory block to be allocated in bytes
1639 * @align: alignment of the region and block's size
1640 * @min_addr: the lower bound of the memory region from where the allocation
1642 * @max_addr: the upper bound of the memory region from where the allocation
1644 * allocate only from memory limited by memblock.current_limit value
1648 * info), if enabled. Does not zero allocated memory, does not panic if request
1652 * Virtual address of allocated memory block on success, NULL on failure.
1668 * memblock_alloc_try_nid - allocate boot memory block
1669 * @size: size of memory block to be allocated in bytes
1670 * @align: alignment of the region and block's size
1671 * @min_addr: the lower bound of the memory region from where the allocation
1673 * @max_addr: the upper bound of the memory region from where the allocation
1675 * allocate only from memory limited by memblock.current_limit value
1679 * info), if enabled. This function zeroes the allocated memory.
1682 * Virtual address of allocated memory block on success, NULL on failure.
1703 * __memblock_alloc_or_panic - Try to allocate memory and panic on failure
1704 * @size: size of memory block to be allocated in bytes
1705 * @align: alignment of the region and block's size
1708 * This function attempts to allocate memory using memblock_alloc,
1723 * memblock_free_late - free pages directly to buddy allocator
1724 * @base: phys starting address of the boot memory block
1725 * @size: size of the boot memory block in bytes
1735 end = base + size - 1; in memblock_free_late()
1736 memblock_dbg("%s: [%pa-%pa] %pS\n", in memblock_free_late()
1754 return memblock.memory.total_size; in memblock_phys_mem_size()
1763 * memblock_estimated_nr_free_pages - return estimated number of free pages
1776 return PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size()); in memblock_estimated_nr_free_pages()
1782 return memblock.memory.regions[0].base; in memblock_start_of_DRAM()
1787 int idx = memblock.memory.cnt - 1; in memblock_end_of_DRAM()
1789 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); in memblock_end_of_DRAM()
1798 * translate the memory @limit size into the max address within one of in __find_max_addr()
1799 * the memory memblock regions, if the @limit exceeds the total size in __find_max_addr()
1803 if (limit <= r->size) { in __find_max_addr()
1804 max_addr = r->base + limit; in __find_max_addr()
1807 limit -= r->size; in __find_max_addr()
1822 /* @limit exceeds the total size of the memory, do nothing */ in memblock_enforce_memory_limit()
1826 /* truncate both memory and reserved regions */ in memblock_enforce_memory_limit()
1827 memblock_remove_range(&memblock.memory, max_addr, in memblock_enforce_memory_limit()
1841 if (!memblock_memory->total_size) { in memblock_cap_memory_range()
1842 pr_warn("%s: No memory registered yet\n", __func__); in memblock_cap_memory_range()
1846 ret = memblock_isolate_range(&memblock.memory, base, size, in memblock_cap_memory_range()
1852 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--) in memblock_cap_memory_range()
1853 if (!memblock_is_nomap(&memblock.memory.regions[i])) in memblock_cap_memory_range()
1854 memblock_remove_region(&memblock.memory, i); in memblock_cap_memory_range()
1856 for (i = start_rgn - 1; i >= 0; i--) in memblock_cap_memory_range()
1857 if (!memblock_is_nomap(&memblock.memory.regions[i])) in memblock_cap_memory_range()
1858 memblock_remove_region(&memblock.memory, i); in memblock_cap_memory_range()
1875 /* @limit exceeds the total size of the memory, do nothing */ in memblock_mem_limit_remove_map()
1884 unsigned int left = 0, right = type->cnt; in memblock_search()
1889 if (addr < type->regions[mid].base) in memblock_search()
1891 else if (addr >= (type->regions[mid].base + in memblock_search()
1892 type->regions[mid].size)) in memblock_search()
1897 return -1; in memblock_search()
1902 return memblock_search(&memblock.reserved, addr) != -1; in memblock_is_reserved()
1907 return memblock_search(&memblock.memory, addr) != -1; in memblock_is_memory()
1912 int i = memblock_search(&memblock.memory, addr); in memblock_is_map_memory()
1914 if (i == -1) in memblock_is_map_memory()
1916 return !memblock_is_nomap(&memblock.memory.regions[i]); in memblock_is_map_memory()
1922 struct memblock_type *type = &memblock.memory; in memblock_search_pfn_nid()
1925 if (mid == -1) in memblock_search_pfn_nid()
1928 *start_pfn = PFN_DOWN(type->regions[mid].base); in memblock_search_pfn_nid()
1929 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size); in memblock_search_pfn_nid()
1931 return memblock_get_region_node(&type->regions[mid]); in memblock_search_pfn_nid()
1935 * memblock_is_region_memory - check if a region is a subset of memory
1936 * @base: base of region to check
1937 * @size: size of region to check
1939 * Check if the region [@base, @base + @size) is a subset of a memory block.
1942 * 0 if false, non-zero if true
1946 int idx = memblock_search(&memblock.memory, base); in memblock_is_region_memory()
1949 if (idx == -1) in memblock_is_region_memory()
1951 return (memblock.memory.regions[idx].base + in memblock_is_region_memory()
1952 memblock.memory.regions[idx].size) >= end; in memblock_is_region_memory()
1956 * memblock_is_region_reserved - check if a region intersects reserved memory
1957 * @base: base of region to check
1958 * @size: size of region to check
1960 * Check if the region [@base, @base + @size) intersects a reserved
1961 * memory block.
1977 orig_start = r->base; in memblock_trim_memory()
1978 orig_end = r->base + r->size; in memblock_trim_memory()
1986 r->base = start; in memblock_trim_memory()
1987 r->size = end - start; in memblock_trim_memory()
1989 memblock_remove_region(&memblock.memory, in memblock_trim_memory()
1990 r - memblock.memory.regions); in memblock_trim_memory()
1991 r--; in memblock_trim_memory()
2013 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt); in memblock_dump()
2018 base = rgn->base; in memblock_dump()
2019 size = rgn->size; in memblock_dump()
2020 end = base + size - 1; in memblock_dump()
2021 flags = rgn->flags; in memblock_dump()
2027 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n", in memblock_dump()
2028 type->name, idx, &base, &end, &size, nid_buf, flags); in memblock_dump()
2035 pr_info(" memory size = %pa reserved size = %pa\n", in __memblock_dump_all()
2036 &memblock.memory.total_size, in __memblock_dump_all()
2039 memblock_dump(&memblock.memory); in __memblock_dump_all()
2073 start_pg = pfn_to_page(start_pfn - 1) + 1; in free_memmap()
2074 end_pg = pfn_to_page(end_pfn - 1) + 1; in free_memmap()
2088 memblock_phys_free(pg, pgend - pg); in free_memmap()
2092 * The mem_map array can get very big. Free the unused area of the memory map.
2117 * presume that there are no holes in the memory map inside in free_unused_memmap()
2131 * presume that there are no holes in the memory map inside in free_unused_memmap()
2154 * MAX_PAGE_ORDER-aligned, set order to MAX_PAGE_ORDER for in __free_pages_memory()
2163 order--; in __free_pages_memory()
2185 return end_pfn - start_pfn; in __free_memory_core()
2190 struct memblock_region *region; in memmap_init_reserved_pages() local
2201 for_each_mem_region(region) { in memmap_init_reserved_pages()
2202 nid = memblock_get_region_node(region); in memmap_init_reserved_pages()
2203 start = region->base; in memmap_init_reserved_pages()
2204 end = start + region->size; in memmap_init_reserved_pages()
2206 if (memblock_is_nomap(region)) in memmap_init_reserved_pages()
2209 memblock_set_node(start, region->size, &memblock.reserved, nid); in memmap_init_reserved_pages()
2213 * array, which may result a new reserved region before current in memmap_init_reserved_pages()
2223 for_each_reserved_mem_region(region) { in memmap_init_reserved_pages()
2224 if (!memblock_is_reserved_noinit(region)) { in memmap_init_reserved_pages()
2225 nid = memblock_get_region_node(region); in memmap_init_reserved_pages()
2226 start = region->base; in memmap_init_reserved_pages()
2227 end = start + region->size; in memmap_init_reserved_pages()
2243 memblock_clear_hotplug(0, -1); in free_low_memory_core_early()
2248 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id in free_low_memory_core_early()
2265 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) in reset_node_managed_pages()
2266 atomic_long_set(&z->managed_pages, 0); in reset_node_managed_pages()
2283 * memblock_free_all - release free pages to the buddy allocator
2296 /* Keep a table to reserve named memory */
2308 /* Add wildcard region with a lookup name */
2315 map->start = start; in reserved_mem_add()
2316 map->size = size; in reserved_mem_add()
2317 strscpy(map->name, name); in reserved_mem_add()
2327 if (!map->size) in reserve_mem_find_by_name_nolock()
2329 if (strcmp(name, map->name) == 0) in reserve_mem_find_by_name_nolock()
2336 * reserve_mem_find_by_name - Find reserved memory region with a given name
2337 * @name: The name that is attached to a reserved memory region
2354 *start = map->start; in reserve_mem_find_by_name()
2355 *size = map->size; in reserve_mem_find_by_name()
2361 * reserve_mem_release_by_name - Release reserved memory region with a given name
2362 * @name: The name that is attatched to a reserved memory region
2364 * Forcibly release the pages in the reserved memory region so that those memory
2365 * can be used as free memory. After released the reserved region size becomes 0.
2380 start = phys_to_virt(map->start); in reserve_mem_release_by_name()
2381 end = start + map->size - 1; in reserve_mem_release_by_name()
2384 map->size = 0; in reserve_mem_release_by_name()
2400 return -EINVAL; in reserve_mem()
2402 /* Check if there's room for more reserved memory */ in reserve_mem()
2404 return -EBUSY; in reserve_mem()
2409 return -EINVAL; in reserve_mem()
2412 return -EINVAL; in reserve_mem()
2416 return -EINVAL; in reserve_mem()
2430 return -EINVAL; in reserve_mem()
2438 return -EINVAL; in reserve_mem()
2442 return -EBUSY; in reserve_mem()
2446 return -ENOMEM; in reserve_mem()
2465 struct memblock_type *type = m->private; in memblock_debug_show()
2471 for (i = 0; i < type->cnt; i++) { in memblock_debug_show()
2472 reg = &type->regions[i]; in memblock_debug_show()
2473 end = reg->base + reg->size - 1; in memblock_debug_show()
2477 seq_printf(m, "%pa..%pa ", &reg->base, &end); in memblock_debug_show()
2482 if (reg->flags) { in memblock_debug_show()
2484 if (reg->flags & (1U << j)) { in memblock_debug_show()
2503 debugfs_create_file("memory", 0444, root, in memblock_init_debugfs()
2504 &memblock.memory, &memblock_debug_fops); in memblock_init_debugfs()