memblock.h 14 KB

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