memblock.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. struct memblock_region {
  19. phys_addr_t base;
  20. phys_addr_t size;
  21. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  22. int nid;
  23. #endif
  24. };
  25. struct memblock_type {
  26. unsigned long cnt; /* number of regions */
  27. unsigned long max; /* size of the allocated array */
  28. phys_addr_t total_size; /* size of all regions */
  29. struct memblock_region *regions;
  30. };
  31. struct memblock {
  32. phys_addr_t current_limit;
  33. struct memblock_type memory;
  34. struct memblock_type reserved;
  35. };
  36. extern struct memblock memblock;
  37. extern int memblock_debug;
  38. #define memblock_dbg(fmt, ...) \
  39. if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  40. phys_addr_t memblock_find_in_range_node(phys_addr_t start, phys_addr_t end,
  41. phys_addr_t size, phys_addr_t align, int nid);
  42. phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
  43. phys_addr_t size, phys_addr_t align);
  44. phys_addr_t get_allocated_memblock_reserved_regions_info(phys_addr_t *addr);
  45. void memblock_allow_resize(void);
  46. int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
  47. int memblock_add(phys_addr_t base, phys_addr_t size);
  48. int memblock_remove(phys_addr_t base, phys_addr_t size);
  49. int memblock_free(phys_addr_t base, phys_addr_t size);
  50. int memblock_reserve(phys_addr_t base, phys_addr_t size);
  51. void memblock_trim_memory(phys_addr_t align);
  52. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  53. int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
  54. unsigned long *end_pfn);
  55. void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
  56. unsigned long *out_end_pfn, int *out_nid);
  57. /**
  58. * for_each_mem_pfn_range - early memory pfn range iterator
  59. * @i: an integer used as loop variable
  60. * @nid: node selector, %MAX_NUMNODES for all nodes
  61. * @p_start: ptr to ulong for start pfn of the range, can be %NULL
  62. * @p_end: ptr to ulong for end pfn of the range, can be %NULL
  63. * @p_nid: ptr to int for nid of the range, can be %NULL
  64. *
  65. * Walks over configured memory ranges.
  66. */
  67. #define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
  68. for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
  69. i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
  70. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  71. void __next_free_mem_range(u64 *idx, int nid, phys_addr_t *out_start,
  72. phys_addr_t *out_end, int *out_nid);
  73. /**
  74. * for_each_free_mem_range - iterate through free memblock areas
  75. * @i: u64 used as loop variable
  76. * @nid: node selector, %MAX_NUMNODES for all nodes
  77. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  78. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  79. * @p_nid: ptr to int for nid of the range, can be %NULL
  80. *
  81. * Walks over free (memory && !reserved) areas of memblock. Available as
  82. * soon as memblock is initialized.
  83. */
  84. #define for_each_free_mem_range(i, nid, p_start, p_end, p_nid) \
  85. for (i = 0, \
  86. __next_free_mem_range(&i, nid, p_start, p_end, p_nid); \
  87. i != (u64)ULLONG_MAX; \
  88. __next_free_mem_range(&i, nid, p_start, p_end, p_nid))
  89. void __next_free_mem_range_rev(u64 *idx, int nid, phys_addr_t *out_start,
  90. phys_addr_t *out_end, int *out_nid);
  91. /**
  92. * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
  93. * @i: u64 used as loop variable
  94. * @nid: node selector, %MAX_NUMNODES for all nodes
  95. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  96. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  97. * @p_nid: ptr to int for nid of the range, can be %NULL
  98. *
  99. * Walks over free (memory && !reserved) areas of memblock in reverse
  100. * order. Available as soon as memblock is initialized.
  101. */
  102. #define for_each_free_mem_range_reverse(i, nid, p_start, p_end, p_nid) \
  103. for (i = (u64)ULLONG_MAX, \
  104. __next_free_mem_range_rev(&i, nid, p_start, p_end, p_nid); \
  105. i != (u64)ULLONG_MAX; \
  106. __next_free_mem_range_rev(&i, nid, p_start, p_end, p_nid))
  107. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  108. int memblock_set_node(phys_addr_t base, phys_addr_t size, int nid);
  109. static inline void memblock_set_region_node(struct memblock_region *r, int nid)
  110. {
  111. r->nid = nid;
  112. }
  113. static inline int memblock_get_region_node(const struct memblock_region *r)
  114. {
  115. return r->nid;
  116. }
  117. #else
  118. static inline void memblock_set_region_node(struct memblock_region *r, int nid)
  119. {
  120. }
  121. static inline int memblock_get_region_node(const struct memblock_region *r)
  122. {
  123. return 0;
  124. }
  125. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  126. phys_addr_t memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
  127. phys_addr_t memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
  128. phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align);
  129. /* Flags for memblock_alloc_base() amd __memblock_alloc_base() */
  130. #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
  131. #define MEMBLOCK_ALLOC_ACCESSIBLE 0
  132. phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
  133. phys_addr_t max_addr);
  134. phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align,
  135. phys_addr_t max_addr);
  136. phys_addr_t memblock_phys_mem_size(void);
  137. phys_addr_t memblock_mem_size(unsigned long limit_pfn);
  138. phys_addr_t memblock_start_of_DRAM(void);
  139. phys_addr_t memblock_end_of_DRAM(void);
  140. void memblock_enforce_memory_limit(phys_addr_t memory_limit);
  141. int memblock_is_memory(phys_addr_t addr);
  142. int memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
  143. int memblock_is_reserved(phys_addr_t addr);
  144. int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
  145. extern void __memblock_dump_all(void);
  146. static inline void memblock_dump_all(void)
  147. {
  148. if (memblock_debug)
  149. __memblock_dump_all();
  150. }
  151. /**
  152. * memblock_set_current_limit - Set the current allocation limit to allow
  153. * limiting allocations to what is currently
  154. * accessible during boot
  155. * @limit: New limit value (physical address)
  156. */
  157. void memblock_set_current_limit(phys_addr_t limit);
  158. /*
  159. * pfn conversion functions
  160. *
  161. * While the memory MEMBLOCKs should always be page aligned, the reserved
  162. * MEMBLOCKs may not be. This accessor attempt to provide a very clear
  163. * idea of what they return for such non aligned MEMBLOCKs.
  164. */
  165. /**
  166. * memblock_region_memory_base_pfn - Return the lowest pfn intersecting with the memory region
  167. * @reg: memblock_region structure
  168. */
  169. static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
  170. {
  171. return PFN_UP(reg->base);
  172. }
  173. /**
  174. * memblock_region_memory_end_pfn - Return the end_pfn this region
  175. * @reg: memblock_region structure
  176. */
  177. static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
  178. {
  179. return PFN_DOWN(reg->base + reg->size);
  180. }
  181. /**
  182. * memblock_region_reserved_base_pfn - Return the lowest pfn intersecting with the reserved region
  183. * @reg: memblock_region structure
  184. */
  185. static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
  186. {
  187. return PFN_DOWN(reg->base);
  188. }
  189. /**
  190. * memblock_region_reserved_end_pfn - Return the end_pfn this region
  191. * @reg: memblock_region structure
  192. */
  193. static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
  194. {
  195. return PFN_UP(reg->base + reg->size);
  196. }
  197. #define for_each_memblock(memblock_type, region) \
  198. for (region = memblock.memblock_type.regions; \
  199. region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
  200. region++)
  201. #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
  202. #define __init_memblock __meminit
  203. #define __initdata_memblock __meminitdata
  204. #else
  205. #define __init_memblock
  206. #define __initdata_memblock
  207. #endif
  208. #else
  209. static inline phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align)
  210. {
  211. return 0;
  212. }
  213. #endif /* CONFIG_HAVE_MEMBLOCK */
  214. #endif /* __KERNEL__ */
  215. #endif /* _LINUX_MEMBLOCK_H */