Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 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 | |
Yinghai Lu | 37d8d4b | 2010-07-28 15:20:58 +1000 | [diff] [blame] | 19 | #define INIT_MEMBLOCK_REGIONS 128 |
Philipp Hachtmann | 70210ed | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 20 | #define INIT_PHYSMEM_REGIONS 4 |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 21 | |
Mike Rapoport | 9a0de1b | 2018-06-30 17:55:04 +0300 | [diff] [blame] | 22 | /** |
| 23 | * enum memblock_flags - definition of memory region attributes |
| 24 | * @MEMBLOCK_NONE: no special request |
| 25 | * @MEMBLOCK_HOTPLUG: hotpluggable region |
| 26 | * @MEMBLOCK_MIRROR: mirrored region |
| 27 | * @MEMBLOCK_NOMAP: don't add to kernel direct mapping |
| 28 | */ |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 29 | enum memblock_flags { |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 30 | MEMBLOCK_NONE = 0x0, /* No special request */ |
| 31 | MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */ |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 32 | MEMBLOCK_MIRROR = 0x2, /* mirrored region */ |
Ard Biesheuvel | bf3d3cc | 2015-11-30 13:28:15 +0100 | [diff] [blame] | 33 | MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 34 | }; |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 35 | |
Mike Rapoport | 9a0de1b | 2018-06-30 17:55:04 +0300 | [diff] [blame] | 36 | /** |
| 37 | * struct memblock_region - represents a memory region |
| 38 | * @base: physical address of the region |
| 39 | * @size: size of the region |
| 40 | * @flags: memory region attributes |
| 41 | * @nid: NUMA node id |
| 42 | */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 43 | struct memblock_region { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 44 | phys_addr_t base; |
| 45 | phys_addr_t size; |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 46 | enum memblock_flags flags; |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 47 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 48 | int nid; |
| 49 | #endif |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 50 | }; |
| 51 | |
Mike Rapoport | 9a0de1b | 2018-06-30 17:55:04 +0300 | [diff] [blame] | 52 | /** |
| 53 | * struct memblock_type - collection of memory regions of certain type |
| 54 | * @cnt: number of regions |
| 55 | * @max: size of the allocated array |
| 56 | * @total_size: size of all regions |
| 57 | * @regions: array of regions |
| 58 | * @name: the memory type symbolic name |
| 59 | */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 60 | struct memblock_type { |
Mike Rapoport | 9a0de1b | 2018-06-30 17:55:04 +0300 | [diff] [blame] | 61 | unsigned long cnt; |
| 62 | unsigned long max; |
| 63 | phys_addr_t total_size; |
Benjamin Herrenschmidt | bf23c51 | 2010-07-06 15:39:06 -0700 | [diff] [blame] | 64 | struct memblock_region *regions; |
Heiko Carstens | 0262d9c | 2017-02-24 14:55:59 -0800 | [diff] [blame] | 65 | char *name; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 66 | }; |
| 67 | |
Mike Rapoport | 9a0de1b | 2018-06-30 17:55:04 +0300 | [diff] [blame] | 68 | /** |
| 69 | * struct memblock - memblock allocator metadata |
| 70 | * @bottom_up: is bottom up direction? |
| 71 | * @current_limit: physical address of the current allocation limit |
| 72 | * @memory: usabe memory regions |
| 73 | * @reserved: reserved memory regions |
| 74 | * @physmem: all physical memory |
| 75 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 76 | struct memblock { |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 77 | bool bottom_up; /* is bottom up direction? */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 78 | phys_addr_t current_limit; |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 79 | struct memblock_type memory; |
| 80 | struct memblock_type reserved; |
Philipp Hachtmann | 70210ed | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 81 | #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
| 82 | struct memblock_type physmem; |
| 83 | #endif |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | extern struct memblock memblock; |
Yinghai Lu | 5e63cf4 | 2010-07-28 15:07:21 +1000 | [diff] [blame] | 87 | extern int memblock_debug; |
Yinghai Lu | 5e63cf4 | 2010-07-28 15:07:21 +1000 | [diff] [blame] | 88 | |
Kirill A. Shutemov | 036fbb2 | 2016-01-15 16:57:11 -0800 | [diff] [blame] | 89 | #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK |
| 90 | #define __init_memblock __meminit |
| 91 | #define __initdata_memblock __meminitdata |
Pavel Tatashin | 3010f87 | 2017-08-18 15:16:05 -0700 | [diff] [blame] | 92 | void memblock_discard(void); |
Kirill A. Shutemov | 036fbb2 | 2016-01-15 16:57:11 -0800 | [diff] [blame] | 93 | #else |
| 94 | #define __init_memblock |
| 95 | #define __initdata_memblock |
| 96 | #endif |
| 97 | |
Yinghai Lu | 5e63cf4 | 2010-07-28 15:07:21 +1000 | [diff] [blame] | 98 | #define memblock_dbg(fmt, ...) \ |
| 99 | if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 100 | |
Grygorii Strashko | 87029ee | 2014-01-21 15:50:14 -0800 | [diff] [blame] | 101 | phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align, |
Chen Gang | ba6c19f | 2016-07-26 15:24:47 -0700 | [diff] [blame] | 102 | phys_addr_t start, phys_addr_t end, |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 103 | int nid, enum memblock_flags flags); |
Tejun Heo | fc769a8 | 2011-07-12 09:58:10 +0200 | [diff] [blame] | 104 | phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end, |
| 105 | phys_addr_t size, phys_addr_t align); |
Tejun Heo | 1aadc05 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 106 | void memblock_allow_resize(void); |
Tejun Heo | 7fb0bc3 | 2011-12-08 10:22:08 -0800 | [diff] [blame] | 107 | int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid); |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 108 | int memblock_add(phys_addr_t base, phys_addr_t size); |
| 109 | int memblock_remove(phys_addr_t base, phys_addr_t size); |
| 110 | int memblock_free(phys_addr_t base, phys_addr_t size); |
| 111 | int memblock_reserve(phys_addr_t base, phys_addr_t size); |
Yinghai Lu | 6ede1fd | 2012-10-22 16:35:18 -0700 | [diff] [blame] | 112 | void memblock_trim_memory(phys_addr_t align); |
Tang Chen | 95cf82e | 2015-09-08 15:02:03 -0700 | [diff] [blame] | 113 | bool memblock_overlaps_region(struct memblock_type *type, |
| 114 | phys_addr_t base, phys_addr_t size); |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 115 | int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size); |
| 116 | int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size); |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 117 | int memblock_mark_mirror(phys_addr_t base, phys_addr_t size); |
Ard Biesheuvel | bf3d3cc | 2015-11-30 13:28:15 +0100 | [diff] [blame] | 118 | int memblock_mark_nomap(phys_addr_t base, phys_addr_t size); |
AKASHI Takahiro | 4c546b8 | 2017-04-03 11:23:54 +0900 | [diff] [blame] | 119 | int memblock_clear_nomap(phys_addr_t base, phys_addr_t size); |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 120 | enum memblock_flags choose_memblock_flags(void); |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 121 | |
| 122 | /* Low level functions */ |
| 123 | int memblock_add_range(struct memblock_type *type, |
| 124 | phys_addr_t base, phys_addr_t size, |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 125 | int nid, enum memblock_flags flags); |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 126 | |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 127 | void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 128 | struct memblock_type *type_a, |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 129 | struct memblock_type *type_b, phys_addr_t *out_start, |
| 130 | phys_addr_t *out_end, int *out_nid); |
| 131 | |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 132 | void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 133 | struct memblock_type *type_a, |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 134 | struct memblock_type *type_b, phys_addr_t *out_start, |
| 135 | phys_addr_t *out_end, int *out_nid); |
| 136 | |
Robin Holt | 8e7a7f8 | 2015-06-30 14:56:41 -0700 | [diff] [blame] | 137 | void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start, |
Chen Gang | ba6c19f | 2016-07-26 15:24:47 -0700 | [diff] [blame] | 138 | phys_addr_t *out_end); |
Robin Holt | 8e7a7f8 | 2015-06-30 14:56:41 -0700 | [diff] [blame] | 139 | |
Pavel Tatashin | 3010f87 | 2017-08-18 15:16:05 -0700 | [diff] [blame] | 140 | void __memblock_free_early(phys_addr_t base, phys_addr_t size); |
| 141 | void __memblock_free_late(phys_addr_t base, phys_addr_t size); |
| 142 | |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 143 | /** |
| 144 | * for_each_mem_range - iterate through memblock areas from type_a and not |
| 145 | * included in type_b. Or just type_a if type_b is NULL. |
| 146 | * @i: u64 used as loop variable |
| 147 | * @type_a: ptr to memblock_type to iterate |
| 148 | * @type_b: ptr to memblock_type which excludes from the iteration |
| 149 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 150 | * @flags: pick from blocks based on memory attributes |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 151 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 152 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 153 | * @p_nid: ptr to int for nid of the range, can be %NULL |
| 154 | */ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 155 | #define for_each_mem_range(i, type_a, type_b, nid, flags, \ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 156 | p_start, p_end, p_nid) \ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 157 | for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 158 | p_start, p_end, p_nid); \ |
| 159 | i != (u64)ULLONG_MAX; \ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 160 | __next_mem_range(&i, nid, flags, type_a, type_b, \ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 161 | p_start, p_end, p_nid)) |
| 162 | |
| 163 | /** |
| 164 | * for_each_mem_range_rev - reverse iterate through memblock areas from |
| 165 | * type_a and not included in type_b. Or just type_a if type_b is NULL. |
| 166 | * @i: u64 used as loop variable |
| 167 | * @type_a: ptr to memblock_type to iterate |
| 168 | * @type_b: ptr to memblock_type which excludes from the iteration |
| 169 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 170 | * @flags: pick from blocks based on memory attributes |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 171 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 172 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 173 | * @p_nid: ptr to int for nid of the range, can be %NULL |
| 174 | */ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 175 | #define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 176 | p_start, p_end, p_nid) \ |
| 177 | for (i = (u64)ULLONG_MAX, \ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 178 | __next_mem_range_rev(&i, nid, flags, type_a, type_b,\ |
Chen Gang | ba6c19f | 2016-07-26 15:24:47 -0700 | [diff] [blame] | 179 | p_start, p_end, p_nid); \ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 180 | i != (u64)ULLONG_MAX; \ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 181 | __next_mem_range_rev(&i, nid, flags, type_a, type_b, \ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 182 | p_start, p_end, p_nid)) |
| 183 | |
Robin Holt | 8e7a7f8 | 2015-06-30 14:56:41 -0700 | [diff] [blame] | 184 | /** |
| 185 | * for_each_reserved_mem_region - iterate over all reserved memblock areas |
| 186 | * @i: u64 used as loop variable |
| 187 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 188 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 189 | * |
| 190 | * Walks over reserved areas of memblock. Available as soon as memblock |
| 191 | * is initialized. |
| 192 | */ |
| 193 | #define for_each_reserved_mem_region(i, p_start, p_end) \ |
Chen Gang | ba6c19f | 2016-07-26 15:24:47 -0700 | [diff] [blame] | 194 | for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \ |
Robin Holt | 8e7a7f8 | 2015-06-30 14:56:41 -0700 | [diff] [blame] | 195 | i != (u64)ULLONG_MAX; \ |
| 196 | __next_reserved_mem_region(&i, p_start, p_end)) |
| 197 | |
Tang Chen | 55ac590 | 2014-01-21 15:49:35 -0800 | [diff] [blame] | 198 | static inline bool memblock_is_hotpluggable(struct memblock_region *m) |
| 199 | { |
| 200 | return m->flags & MEMBLOCK_HOTPLUG; |
| 201 | } |
| 202 | |
Tony Luck | a3f5baf | 2015-06-24 16:58:12 -0700 | [diff] [blame] | 203 | static inline bool memblock_is_mirror(struct memblock_region *m) |
| 204 | { |
| 205 | return m->flags & MEMBLOCK_MIRROR; |
| 206 | } |
| 207 | |
Ard Biesheuvel | bf3d3cc | 2015-11-30 13:28:15 +0100 | [diff] [blame] | 208 | static inline bool memblock_is_nomap(struct memblock_region *m) |
| 209 | { |
| 210 | return m->flags & MEMBLOCK_NOMAP; |
| 211 | } |
| 212 | |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 213 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
Yinghai Lu | e76b63f | 2013-09-11 14:22:17 -0700 | [diff] [blame] | 214 | int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn, |
| 215 | unsigned long *end_pfn); |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 216 | void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn, |
| 217 | unsigned long *out_end_pfn, int *out_nid); |
| 218 | |
| 219 | /** |
| 220 | * for_each_mem_pfn_range - early memory pfn range iterator |
| 221 | * @i: an integer used as loop variable |
| 222 | * @nid: node selector, %MAX_NUMNODES for all nodes |
| 223 | * @p_start: ptr to ulong for start pfn of the range, can be %NULL |
| 224 | * @p_end: ptr to ulong for end pfn of the range, can be %NULL |
| 225 | * @p_nid: ptr to int for nid of the range, can be %NULL |
| 226 | * |
Wanpeng Li | f2d52fe | 2012-10-08 16:32:24 -0700 | [diff] [blame] | 227 | * Walks over configured memory ranges. |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 228 | */ |
| 229 | #define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \ |
| 230 | for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \ |
| 231 | i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid)) |
| 232 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
| 233 | |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 234 | /** |
| 235 | * for_each_free_mem_range - iterate through free memblock areas |
| 236 | * @i: u64 used as loop variable |
Grygorii Strashko | b115423 | 2014-01-21 15:50:16 -0800 | [diff] [blame] | 237 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
Florian Fainelli | d30b554 | 2016-01-14 15:22:04 -0800 | [diff] [blame] | 238 | * @flags: pick from blocks based on memory attributes |
Tejun Heo | 35fd080 | 2011-07-12 11:15:59 +0200 | [diff] [blame] | 239 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 240 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 241 | * @p_nid: ptr to int for nid of the range, can be %NULL |
| 242 | * |
| 243 | * Walks over free (memory && !reserved) areas of memblock. Available as |
| 244 | * soon as memblock is initialized. |
| 245 | */ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 246 | #define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 247 | for_each_mem_range(i, &memblock.memory, &memblock.reserved, \ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 248 | nid, flags, p_start, p_end, p_nid) |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * for_each_free_mem_range_reverse - rev-iterate through free memblock areas |
| 252 | * @i: u64 used as loop variable |
Grygorii Strashko | b115423 | 2014-01-21 15:50:16 -0800 | [diff] [blame] | 253 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
Florian Fainelli | d30b554 | 2016-01-14 15:22:04 -0800 | [diff] [blame] | 254 | * @flags: pick from blocks based on memory attributes |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 255 | * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL |
| 256 | * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL |
| 257 | * @p_nid: ptr to int for nid of the range, can be %NULL |
| 258 | * |
| 259 | * Walks over free (memory && !reserved) areas of memblock in reverse |
| 260 | * order. Available as soon as memblock is initialized. |
| 261 | */ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 262 | #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \ |
| 263 | p_nid) \ |
Philipp Hachtmann | f1af9d3 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 264 | for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \ |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 265 | nid, flags, p_start, p_end, p_nid) |
Tejun Heo | 7bd0b0f | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 266 | |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 267 | static inline void memblock_set_region_flags(struct memblock_region *r, |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 268 | enum memblock_flags flags) |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 269 | { |
| 270 | r->flags |= flags; |
| 271 | } |
| 272 | |
| 273 | static inline void memblock_clear_region_flags(struct memblock_region *r, |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 274 | enum memblock_flags flags) |
Tang Chen | 66b16ed | 2014-01-21 15:49:23 -0800 | [diff] [blame] | 275 | { |
| 276 | r->flags &= ~flags; |
| 277 | } |
| 278 | |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 279 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
Tang Chen | e7e8de5 | 2014-01-21 15:49:26 -0800 | [diff] [blame] | 280 | int memblock_set_node(phys_addr_t base, phys_addr_t size, |
| 281 | struct memblock_type *type, int nid); |
Tejun Heo | 7c0caeb | 2011-07-14 11:43:42 +0200 | [diff] [blame] | 282 | |
| 283 | static inline void memblock_set_region_node(struct memblock_region *r, int nid) |
| 284 | { |
| 285 | r->nid = nid; |
| 286 | } |
| 287 | |
| 288 | static inline int memblock_get_region_node(const struct memblock_region *r) |
| 289 | { |
| 290 | return r->nid; |
| 291 | } |
| 292 | #else |
| 293 | static inline void memblock_set_region_node(struct memblock_region *r, int nid) |
| 294 | { |
| 295 | } |
| 296 | |
| 297 | static inline int memblock_get_region_node(const struct memblock_region *r) |
| 298 | { |
| 299 | return 0; |
| 300 | } |
| 301 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
| 302 | |
Mike Rapoport | 9a8dd70 | 2018-10-30 15:07:59 -0700 | [diff] [blame] | 303 | phys_addr_t memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid); |
| 304 | phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid); |
Benjamin Herrenschmidt | 9d1e249 | 2010-07-06 15:39:17 -0700 | [diff] [blame] | 305 | |
Mike Rapoport | 9a8dd70 | 2018-10-30 15:07:59 -0700 | [diff] [blame] | 306 | phys_addr_t memblock_phys_alloc(phys_addr_t size, phys_addr_t align); |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 307 | |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 308 | /* |
| 309 | * Set the allocation direction to bottom-up or top-down. |
| 310 | */ |
Fabian Frederick | 2cfb366 | 2014-08-06 16:05:03 -0700 | [diff] [blame] | 311 | static inline void __init memblock_set_bottom_up(bool enable) |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 312 | { |
| 313 | memblock.bottom_up = enable; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Check if the allocation direction is bottom-up or not. |
| 318 | * if this is true, that said, memblock will allocate memory |
| 319 | * in bottom-up direction. |
| 320 | */ |
| 321 | static inline bool memblock_bottom_up(void) |
| 322 | { |
| 323 | return memblock.bottom_up; |
| 324 | } |
Tang Chen | 79442ed | 2013-11-12 15:07:59 -0800 | [diff] [blame] | 325 | |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 326 | /* Flags for memblock_alloc_base() amd __memblock_alloc_base() */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 327 | #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0) |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 328 | #define MEMBLOCK_ALLOC_ACCESSIBLE 0 |
| 329 | |
Akinobu Mita | 2bfc286 | 2014-06-04 16:06:53 -0700 | [diff] [blame] | 330 | phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 331 | phys_addr_t start, phys_addr_t end, |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 332 | enum memblock_flags flags); |
Nicholas Piggin | b575454f | 2018-02-14 01:08:15 +1000 | [diff] [blame] | 333 | phys_addr_t memblock_alloc_base_nid(phys_addr_t size, |
| 334 | phys_addr_t align, phys_addr_t max_addr, |
Mike Rapoport | e1720fe | 2018-06-30 17:55:01 +0300 | [diff] [blame] | 335 | int nid, enum memblock_flags flags); |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 336 | phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align, |
| 337 | phys_addr_t max_addr); |
| 338 | phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align, |
| 339 | phys_addr_t max_addr); |
| 340 | phys_addr_t memblock_phys_mem_size(void); |
Srikar Dronamraju | 8907de5 | 2016-10-07 16:59:18 -0700 | [diff] [blame] | 341 | phys_addr_t memblock_reserved_size(void); |
Yinghai Lu | 595ad9a | 2013-01-24 12:20:09 -0800 | [diff] [blame] | 342 | phys_addr_t memblock_mem_size(unsigned long limit_pfn); |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 343 | phys_addr_t memblock_start_of_DRAM(void); |
| 344 | phys_addr_t memblock_end_of_DRAM(void); |
| 345 | void memblock_enforce_memory_limit(phys_addr_t memory_limit); |
AKASHI Takahiro | c9ca9b4 | 2017-04-03 11:23:55 +0900 | [diff] [blame] | 346 | void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size); |
Dennis Chen | a571d4e | 2016-07-28 15:48:26 -0700 | [diff] [blame] | 347 | void memblock_mem_limit_remove_map(phys_addr_t limit); |
Yaowei Bai | b4ad0c7 | 2016-01-14 15:18:54 -0800 | [diff] [blame] | 348 | bool memblock_is_memory(phys_addr_t addr); |
Yaowei Bai | 937f0c2 | 2018-02-06 15:41:18 -0800 | [diff] [blame] | 349 | bool memblock_is_map_memory(phys_addr_t addr); |
| 350 | bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size); |
Yaowei Bai | b4ad0c7 | 2016-01-14 15:18:54 -0800 | [diff] [blame] | 351 | bool memblock_is_reserved(phys_addr_t addr); |
Tang Chen | c5c5c9d | 2015-09-08 15:02:00 -0700 | [diff] [blame] | 352 | bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 353 | |
Tejun Heo | 4ff7b82 | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 354 | extern void __memblock_dump_all(void); |
| 355 | |
| 356 | static inline void memblock_dump_all(void) |
| 357 | { |
| 358 | if (memblock_debug) |
| 359 | __memblock_dump_all(); |
| 360 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 361 | |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 362 | /** |
| 363 | * memblock_set_current_limit - Set the current allocation limit to allow |
| 364 | * limiting allocations to what is currently |
| 365 | * accessible during boot |
| 366 | * @limit: New limit value (physical address) |
| 367 | */ |
Tejun Heo | 581adcb | 2011-12-08 10:22:06 -0800 | [diff] [blame] | 368 | void memblock_set_current_limit(phys_addr_t limit); |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 369 | |
Benjamin Herrenschmidt | 35a1f0b | 2010-07-06 15:38:58 -0700 | [diff] [blame] | 370 | |
Laura Abbott | fec5101 | 2014-02-27 01:23:43 +0100 | [diff] [blame] | 371 | phys_addr_t memblock_get_current_limit(void); |
| 372 | |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 373 | /* |
| 374 | * pfn conversion functions |
| 375 | * |
| 376 | * While the memory MEMBLOCKs should always be page aligned, the reserved |
| 377 | * MEMBLOCKs may not be. This accessor attempt to provide a very clear |
| 378 | * idea of what they return for such non aligned MEMBLOCKs. |
| 379 | */ |
| 380 | |
| 381 | /** |
Mike Rapoport | 47cec44 | 2018-06-30 17:55:02 +0300 | [diff] [blame] | 382 | * memblock_region_memory_base_pfn - get the lowest pfn of the memory region |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 383 | * @reg: memblock_region structure |
Mike Rapoport | 47cec44 | 2018-06-30 17:55:02 +0300 | [diff] [blame] | 384 | * |
| 385 | * Return: the lowest pfn intersecting with the memory region |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 386 | */ |
Yinghai Lu | c7fc2de | 2010-10-12 14:07:09 -0700 | [diff] [blame] | 387 | static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg) |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 388 | { |
Yinghai Lu | c7fc2de | 2010-10-12 14:07:09 -0700 | [diff] [blame] | 389 | return PFN_UP(reg->base); |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
Mike Rapoport | 47cec44 | 2018-06-30 17:55:02 +0300 | [diff] [blame] | 393 | * memblock_region_memory_end_pfn - get the end pfn of the memory region |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 394 | * @reg: memblock_region structure |
Mike Rapoport | 47cec44 | 2018-06-30 17:55:02 +0300 | [diff] [blame] | 395 | * |
| 396 | * Return: the end_pfn of the reserved region |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 397 | */ |
Yinghai Lu | c7fc2de | 2010-10-12 14:07:09 -0700 | [diff] [blame] | 398 | static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg) |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 399 | { |
Yinghai Lu | c7fc2de | 2010-10-12 14:07:09 -0700 | [diff] [blame] | 400 | return PFN_DOWN(reg->base + reg->size); |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** |
Mike Rapoport | 47cec44 | 2018-06-30 17:55:02 +0300 | [diff] [blame] | 404 | * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 405 | * @reg: memblock_region structure |
Mike Rapoport | 47cec44 | 2018-06-30 17:55:02 +0300 | [diff] [blame] | 406 | * |
| 407 | * Return: the lowest pfn intersecting with the reserved region |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 408 | */ |
Yinghai Lu | c7fc2de | 2010-10-12 14:07:09 -0700 | [diff] [blame] | 409 | static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg) |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 410 | { |
Yinghai Lu | c7fc2de | 2010-10-12 14:07:09 -0700 | [diff] [blame] | 411 | return PFN_DOWN(reg->base); |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
Mike Rapoport | 47cec44 | 2018-06-30 17:55:02 +0300 | [diff] [blame] | 415 | * memblock_region_reserved_end_pfn - get the end pfn of the reserved region |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 416 | * @reg: memblock_region structure |
Mike Rapoport | 47cec44 | 2018-06-30 17:55:02 +0300 | [diff] [blame] | 417 | * |
| 418 | * Return: the end_pfn of the reserved region |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 419 | */ |
Yinghai Lu | c7fc2de | 2010-10-12 14:07:09 -0700 | [diff] [blame] | 420 | static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg) |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 421 | { |
Yinghai Lu | c7fc2de | 2010-10-12 14:07:09 -0700 | [diff] [blame] | 422 | return PFN_UP(reg->base + reg->size); |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | #define for_each_memblock(memblock_type, region) \ |
Chen Gang | ba6c19f | 2016-07-26 15:24:47 -0700 | [diff] [blame] | 426 | for (region = memblock.memblock_type.regions; \ |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 427 | region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \ |
| 428 | region++) |
| 429 | |
Gioh Kim | 66e8b43 | 2017-11-15 17:33:42 -0800 | [diff] [blame] | 430 | #define for_each_memblock_type(i, memblock_type, rgn) \ |
| 431 | for (i = 0, rgn = &memblock_type->regions[0]; \ |
| 432 | i < memblock_type->cnt; \ |
| 433 | i++, rgn = &memblock_type->regions[i]) |
Benjamin Herrenschmidt | 5b385f2 | 2010-08-04 13:40:38 +1000 | [diff] [blame] | 434 | |
Vladimir Murzin | 4a20799 | 2015-04-14 15:48:27 -0700 | [diff] [blame] | 435 | #ifdef CONFIG_MEMTEST |
Vladimir Murzin | 7f70bae | 2015-04-14 15:48:30 -0700 | [diff] [blame] | 436 | extern void early_memtest(phys_addr_t start, phys_addr_t end); |
Vladimir Murzin | 4a20799 | 2015-04-14 15:48:27 -0700 | [diff] [blame] | 437 | #else |
Vladimir Murzin | 7f70bae | 2015-04-14 15:48:30 -0700 | [diff] [blame] | 438 | static inline void early_memtest(phys_addr_t start, phys_addr_t end) |
Vladimir Murzin | 4a20799 | 2015-04-14 15:48:27 -0700 | [diff] [blame] | 439 | { |
| 440 | } |
| 441 | #endif |
Yinghai Lu | f0b37fad | 2010-07-28 15:28:21 +1000 | [diff] [blame] | 442 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 443 | #endif /* __KERNEL__ */ |
| 444 | |
| 445 | #endif /* _LINUX_MEMBLOCK_H */ |