memblock.h 12 KB

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