memblock.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #ifndef _LINUX_MEMBLOCK_H
  2. #define _LINUX_MEMBLOCK_H
  3. #ifdef __KERNEL__
  4. #ifdef CONFIG_HAVE_MEMBLOCK
  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. #include <linux/init.h>
  16. #include <linux/mm.h>
  17. #define INIT_MEMBLOCK_REGIONS 128
  18. #define INIT_PHYSMEM_REGIONS 4
  19. /* Definition of memblock flags. */
  20. enum {
  21. MEMBLOCK_NONE = 0x0, /* No special request */
  22. MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
  23. MEMBLOCK_MIRROR = 0x2, /* mirrored region */
  24. };
  25. struct memblock_region {
  26. phys_addr_t base;
  27. phys_addr_t size;
  28. unsigned long flags;
  29. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  30. int nid;
  31. #endif
  32. };
  33. struct memblock_type {
  34. unsigned long cnt; /* number of regions */
  35. unsigned long max; /* size of the allocated array */
  36. phys_addr_t total_size; /* size of all regions */
  37. struct memblock_region *regions;
  38. };
  39. struct memblock {
  40. bool bottom_up; /* is bottom up direction? */
  41. phys_addr_t current_limit;
  42. struct memblock_type memory;
  43. struct memblock_type reserved;
  44. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  45. struct memblock_type physmem;
  46. #endif
  47. };
  48. extern struct memblock memblock;
  49. extern int memblock_debug;
  50. #ifdef CONFIG_MOVABLE_NODE
  51. /* If movable_node boot option specified */
  52. extern bool movable_node_enabled;
  53. #endif /* CONFIG_MOVABLE_NODE */
  54. #define memblock_dbg(fmt, ...) \
  55. if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  56. phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align,
  57. phys_addr_t start, phys_addr_t end,
  58. int nid, ulong flags);
  59. phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
  60. phys_addr_t size, phys_addr_t align);
  61. phys_addr_t get_allocated_memblock_reserved_regions_info(phys_addr_t *addr);
  62. phys_addr_t get_allocated_memblock_memory_regions_info(phys_addr_t *addr);
  63. void memblock_allow_resize(void);
  64. int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
  65. int memblock_add(phys_addr_t base, phys_addr_t size);
  66. int memblock_remove(phys_addr_t base, phys_addr_t size);
  67. int memblock_free(phys_addr_t base, phys_addr_t size);
  68. int memblock_reserve(phys_addr_t base, phys_addr_t size);
  69. void memblock_trim_memory(phys_addr_t align);
  70. bool memblock_overlaps_region(struct memblock_type *type,
  71. phys_addr_t base, phys_addr_t size);
  72. int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
  73. int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
  74. int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
  75. ulong choose_memblock_flags(void);
  76. /* Low level functions */
  77. int memblock_add_range(struct memblock_type *type,
  78. phys_addr_t base, phys_addr_t size,
  79. int nid, unsigned long flags);
  80. void __next_mem_range(u64 *idx, int nid, ulong flags,
  81. struct memblock_type *type_a,
  82. struct memblock_type *type_b, phys_addr_t *out_start,
  83. phys_addr_t *out_end, int *out_nid);
  84. void __next_mem_range_rev(u64 *idx, int nid, ulong flags,
  85. struct memblock_type *type_a,
  86. struct memblock_type *type_b, phys_addr_t *out_start,
  87. phys_addr_t *out_end, int *out_nid);
  88. void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
  89. phys_addr_t *out_end);
  90. /**
  91. * for_each_mem_range - iterate through memblock areas from type_a and not
  92. * included in type_b. Or just type_a if type_b is NULL.
  93. * @i: u64 used as loop variable
  94. * @type_a: ptr to memblock_type to iterate
  95. * @type_b: ptr to memblock_type which excludes from the iteration
  96. * @nid: node selector, %NUMA_NO_NODE for all nodes
  97. * @flags: pick from blocks based on memory attributes
  98. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  99. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  100. * @p_nid: ptr to int for nid of the range, can be %NULL
  101. */
  102. #define for_each_mem_range(i, type_a, type_b, nid, flags, \
  103. p_start, p_end, p_nid) \
  104. for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
  105. p_start, p_end, p_nid); \
  106. i != (u64)ULLONG_MAX; \
  107. __next_mem_range(&i, nid, flags, type_a, type_b, \
  108. p_start, p_end, p_nid))
  109. /**
  110. * for_each_mem_range_rev - reverse iterate through memblock areas from
  111. * type_a and not included in type_b. Or just type_a if type_b is NULL.
  112. * @i: u64 used as loop variable
  113. * @type_a: ptr to memblock_type to iterate
  114. * @type_b: ptr to memblock_type which excludes from the iteration
  115. * @nid: node selector, %NUMA_NO_NODE for all nodes
  116. * @flags: pick from blocks based on memory attributes
  117. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  118. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  119. * @p_nid: ptr to int for nid of the range, can be %NULL
  120. */
  121. #define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
  122. p_start, p_end, p_nid) \
  123. for (i = (u64)ULLONG_MAX, \
  124. __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
  125. p_start, p_end, p_nid); \
  126. i != (u64)ULLONG_MAX; \
  127. __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
  128. p_start, p_end, p_nid))
  129. /**
  130. * for_each_reserved_mem_region - iterate over all reserved memblock areas
  131. * @i: u64 used as loop variable
  132. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  133. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  134. *
  135. * Walks over reserved areas of memblock. Available as soon as memblock
  136. * is initialized.
  137. */
  138. #define for_each_reserved_mem_region(i, p_start, p_end) \
  139. for (i = 0UL, \
  140. __next_reserved_mem_region(&i, p_start, p_end); \
  141. i != (u64)ULLONG_MAX; \
  142. __next_reserved_mem_region(&i, p_start, p_end))
  143. #ifdef CONFIG_MOVABLE_NODE
  144. static inline bool memblock_is_hotpluggable(struct memblock_region *m)
  145. {
  146. return m->flags & MEMBLOCK_HOTPLUG;
  147. }
  148. static inline bool movable_node_is_enabled(void)
  149. {
  150. return movable_node_enabled;
  151. }
  152. #else
  153. static inline bool memblock_is_hotpluggable(struct memblock_region *m)
  154. {
  155. return false;
  156. }
  157. static inline bool movable_node_is_enabled(void)
  158. {
  159. return false;
  160. }
  161. #endif
  162. static inline bool memblock_is_mirror(struct memblock_region *m)
  163. {
  164. return m->flags & MEMBLOCK_MIRROR;
  165. }
  166. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  167. int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
  168. unsigned long *end_pfn);
  169. void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
  170. unsigned long *out_end_pfn, int *out_nid);
  171. /**
  172. * for_each_mem_pfn_range - early memory pfn range iterator
  173. * @i: an integer used as loop variable
  174. * @nid: node selector, %MAX_NUMNODES for all nodes
  175. * @p_start: ptr to ulong for start pfn of the range, can be %NULL
  176. * @p_end: ptr to ulong for end pfn of the range, can be %NULL
  177. * @p_nid: ptr to int for nid of the range, can be %NULL
  178. *
  179. * Walks over configured memory ranges.
  180. */
  181. #define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
  182. for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
  183. i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
  184. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  185. /**
  186. * for_each_free_mem_range - iterate through free memblock areas
  187. * @i: u64 used as loop variable
  188. * @nid: node selector, %NUMA_NO_NODE for all nodes
  189. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  190. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  191. * @p_nid: ptr to int for nid of the range, can be %NULL
  192. * @flags: pick from blocks based on memory attributes
  193. *
  194. * Walks over free (memory && !reserved) areas of memblock. Available as
  195. * soon as memblock is initialized.
  196. */
  197. #define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
  198. for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
  199. nid, flags, p_start, p_end, p_nid)
  200. /**
  201. * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
  202. * @i: u64 used as loop variable
  203. * @nid: node selector, %NUMA_NO_NODE for all nodes
  204. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  205. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  206. * @p_nid: ptr to int for nid of the range, can be %NULL
  207. * @flags: pick from blocks based on memory attributes
  208. *
  209. * Walks over free (memory && !reserved) areas of memblock in reverse
  210. * order. Available as soon as memblock is initialized.
  211. */
  212. #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
  213. p_nid) \
  214. for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
  215. nid, flags, p_start, p_end, p_nid)
  216. static inline void memblock_set_region_flags(struct memblock_region *r,
  217. unsigned long flags)
  218. {
  219. r->flags |= flags;
  220. }
  221. static inline void memblock_clear_region_flags(struct memblock_region *r,
  222. unsigned long flags)
  223. {
  224. r->flags &= ~flags;
  225. }
  226. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  227. int memblock_set_node(phys_addr_t base, phys_addr_t size,
  228. struct memblock_type *type, int nid);
  229. static inline void memblock_set_region_node(struct memblock_region *r, int nid)
  230. {
  231. r->nid = nid;
  232. }
  233. static inline int memblock_get_region_node(const struct memblock_region *r)
  234. {
  235. return r->nid;
  236. }
  237. #else
  238. static inline void memblock_set_region_node(struct memblock_region *r, int nid)
  239. {
  240. }
  241. static inline int memblock_get_region_node(const struct memblock_region *r)
  242. {
  243. return 0;
  244. }
  245. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  246. phys_addr_t memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
  247. phys_addr_t memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
  248. phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align);
  249. #ifdef CONFIG_MOVABLE_NODE
  250. /*
  251. * Set the allocation direction to bottom-up or top-down.
  252. */
  253. static inline void __init memblock_set_bottom_up(bool enable)
  254. {
  255. memblock.bottom_up = enable;
  256. }
  257. /*
  258. * Check if the allocation direction is bottom-up or not.
  259. * if this is true, that said, memblock will allocate memory
  260. * in bottom-up direction.
  261. */
  262. static inline bool memblock_bottom_up(void)
  263. {
  264. return memblock.bottom_up;
  265. }
  266. #else
  267. static inline void __init memblock_set_bottom_up(bool enable) {}
  268. static inline bool memblock_bottom_up(void) { return false; }
  269. #endif
  270. /* Flags for memblock_alloc_base() amd __memblock_alloc_base() */
  271. #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
  272. #define MEMBLOCK_ALLOC_ACCESSIBLE 0
  273. phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
  274. phys_addr_t start, phys_addr_t end,
  275. ulong flags);
  276. phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
  277. phys_addr_t max_addr);
  278. phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align,
  279. phys_addr_t max_addr);
  280. phys_addr_t memblock_phys_mem_size(void);
  281. phys_addr_t memblock_mem_size(unsigned long limit_pfn);
  282. phys_addr_t memblock_start_of_DRAM(void);
  283. phys_addr_t memblock_end_of_DRAM(void);
  284. void memblock_enforce_memory_limit(phys_addr_t memory_limit);
  285. int memblock_is_memory(phys_addr_t addr);
  286. int memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
  287. int memblock_is_reserved(phys_addr_t addr);
  288. bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
  289. extern void __memblock_dump_all(void);
  290. static inline void memblock_dump_all(void)
  291. {
  292. if (memblock_debug)
  293. __memblock_dump_all();
  294. }
  295. /**
  296. * memblock_set_current_limit - Set the current allocation limit to allow
  297. * limiting allocations to what is currently
  298. * accessible during boot
  299. * @limit: New limit value (physical address)
  300. */
  301. void memblock_set_current_limit(phys_addr_t limit);
  302. phys_addr_t memblock_get_current_limit(void);
  303. /*
  304. * pfn conversion functions
  305. *
  306. * While the memory MEMBLOCKs should always be page aligned, the reserved
  307. * MEMBLOCKs may not be. This accessor attempt to provide a very clear
  308. * idea of what they return for such non aligned MEMBLOCKs.
  309. */
  310. /**
  311. * memblock_region_memory_base_pfn - Return the lowest pfn intersecting with the memory region
  312. * @reg: memblock_region structure
  313. */
  314. static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
  315. {
  316. return PFN_UP(reg->base);
  317. }
  318. /**
  319. * memblock_region_memory_end_pfn - Return the end_pfn this region
  320. * @reg: memblock_region structure
  321. */
  322. static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
  323. {
  324. return PFN_DOWN(reg->base + reg->size);
  325. }
  326. /**
  327. * memblock_region_reserved_base_pfn - Return the lowest pfn intersecting with the reserved region
  328. * @reg: memblock_region structure
  329. */
  330. static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
  331. {
  332. return PFN_DOWN(reg->base);
  333. }
  334. /**
  335. * memblock_region_reserved_end_pfn - Return the end_pfn this region
  336. * @reg: memblock_region structure
  337. */
  338. static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
  339. {
  340. return PFN_UP(reg->base + reg->size);
  341. }
  342. #define for_each_memblock(memblock_type, region) \
  343. for (region = memblock.memblock_type.regions; \
  344. region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
  345. region++)
  346. #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
  347. #define __init_memblock __meminit
  348. #define __initdata_memblock __meminitdata
  349. #else
  350. #define __init_memblock
  351. #define __initdata_memblock
  352. #endif
  353. #ifdef CONFIG_MEMTEST
  354. extern void early_memtest(phys_addr_t start, phys_addr_t end);
  355. #else
  356. static inline void early_memtest(phys_addr_t start, phys_addr_t end)
  357. {
  358. }
  359. #endif
  360. extern unsigned long memblock_reserved_memory_within(phys_addr_t start_addr,
  361. phys_addr_t end_addr);
  362. #else
  363. static inline phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align)
  364. {
  365. return 0;
  366. }
  367. static inline unsigned long memblock_reserved_memory_within(phys_addr_t start_addr,
  368. phys_addr_t end_addr)
  369. {
  370. return 0;
  371. }
  372. #endif /* CONFIG_HAVE_MEMBLOCK */
  373. #endif /* __KERNEL__ */
  374. #endif /* _LINUX_MEMBLOCK_H */