memblock.h 14 KB

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