memblock.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include <asm/memblock.h>
  18. #define INIT_MEMBLOCK_REGIONS 128
  19. struct memblock_region {
  20. phys_addr_t base;
  21. phys_addr_t size;
  22. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  23. int nid;
  24. #endif
  25. };
  26. struct memblock_type {
  27. unsigned long cnt; /* number of regions */
  28. unsigned long max; /* size of the allocated array */
  29. struct memblock_region *regions;
  30. };
  31. struct memblock {
  32. phys_addr_t current_limit;
  33. phys_addr_t memory_size; /* Updated by memblock_analyze() */
  34. struct memblock_type memory;
  35. struct memblock_type reserved;
  36. };
  37. extern struct memblock memblock;
  38. extern int memblock_debug;
  39. extern int memblock_can_resize;
  40. #define memblock_dbg(fmt, ...) \
  41. if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  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. int memblock_free_reserved_regions(void);
  45. int memblock_reserve_reserved_regions(void);
  46. extern void memblock_init(void);
  47. extern void memblock_analyze(void);
  48. extern long memblock_add(phys_addr_t base, phys_addr_t size);
  49. extern long memblock_remove(phys_addr_t base, phys_addr_t size);
  50. extern long memblock_free(phys_addr_t base, phys_addr_t size);
  51. extern long memblock_reserve(phys_addr_t base, phys_addr_t size);
  52. extern void __next_free_mem_range(u64 *idx, int nid, phys_addr_t *out_start,
  53. phys_addr_t *out_end, int *out_nid);
  54. /**
  55. * for_each_free_mem_range - iterate through free memblock areas
  56. * @i: u64 used as loop variable
  57. * @nid: node selector, %MAX_NUMNODES for all nodes
  58. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  59. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  60. * @p_nid: ptr to int for nid of the range, can be %NULL
  61. *
  62. * Walks over free (memory && !reserved) areas of memblock. Available as
  63. * soon as memblock is initialized.
  64. */
  65. #define for_each_free_mem_range(i, nid, p_start, p_end, p_nid) \
  66. for (i = 0, \
  67. __next_free_mem_range(&i, nid, p_start, p_end, p_nid); \
  68. i != (u64)ULLONG_MAX; \
  69. __next_free_mem_range(&i, nid, p_start, p_end, p_nid))
  70. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  71. extern int memblock_set_node(phys_addr_t base, phys_addr_t size, int nid);
  72. static inline void memblock_set_region_node(struct memblock_region *r, int nid)
  73. {
  74. r->nid = nid;
  75. }
  76. static inline int memblock_get_region_node(const struct memblock_region *r)
  77. {
  78. return r->nid;
  79. }
  80. #else
  81. static inline void memblock_set_region_node(struct memblock_region *r, int nid)
  82. {
  83. }
  84. static inline int memblock_get_region_node(const struct memblock_region *r)
  85. {
  86. return 0;
  87. }
  88. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  89. /* The numa aware allocator is only available if
  90. * CONFIG_ARCH_POPULATES_NODE_MAP is set
  91. */
  92. extern phys_addr_t memblock_find_in_range_node(phys_addr_t start,
  93. phys_addr_t end,
  94. phys_addr_t size,
  95. phys_addr_t align, int nid);
  96. extern phys_addr_t memblock_alloc_nid(phys_addr_t size, phys_addr_t align,
  97. int nid);
  98. extern phys_addr_t memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
  99. int nid);
  100. extern phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align);
  101. /* Flags for memblock_alloc_base() amd __memblock_alloc_base() */
  102. #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
  103. #define MEMBLOCK_ALLOC_ACCESSIBLE 0
  104. extern phys_addr_t memblock_alloc_base(phys_addr_t size,
  105. phys_addr_t align,
  106. phys_addr_t max_addr);
  107. extern phys_addr_t __memblock_alloc_base(phys_addr_t size,
  108. phys_addr_t align,
  109. phys_addr_t max_addr);
  110. extern phys_addr_t memblock_phys_mem_size(void);
  111. extern phys_addr_t memblock_end_of_DRAM(void);
  112. extern void memblock_enforce_memory_limit(phys_addr_t memory_limit);
  113. extern int memblock_is_memory(phys_addr_t addr);
  114. extern int memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
  115. extern int memblock_is_reserved(phys_addr_t addr);
  116. extern int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
  117. extern void memblock_dump_all(void);
  118. /**
  119. * memblock_set_current_limit - Set the current allocation limit to allow
  120. * limiting allocations to what is currently
  121. * accessible during boot
  122. * @limit: New limit value (physical address)
  123. */
  124. extern void memblock_set_current_limit(phys_addr_t limit);
  125. /*
  126. * pfn conversion functions
  127. *
  128. * While the memory MEMBLOCKs should always be page aligned, the reserved
  129. * MEMBLOCKs may not be. This accessor attempt to provide a very clear
  130. * idea of what they return for such non aligned MEMBLOCKs.
  131. */
  132. /**
  133. * memblock_region_memory_base_pfn - Return the lowest pfn intersecting with the memory region
  134. * @reg: memblock_region structure
  135. */
  136. static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
  137. {
  138. return PFN_UP(reg->base);
  139. }
  140. /**
  141. * memblock_region_memory_end_pfn - Return the end_pfn this region
  142. * @reg: memblock_region structure
  143. */
  144. static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
  145. {
  146. return PFN_DOWN(reg->base + reg->size);
  147. }
  148. /**
  149. * memblock_region_reserved_base_pfn - Return the lowest pfn intersecting with the reserved region
  150. * @reg: memblock_region structure
  151. */
  152. static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
  153. {
  154. return PFN_DOWN(reg->base);
  155. }
  156. /**
  157. * memblock_region_reserved_end_pfn - Return the end_pfn this region
  158. * @reg: memblock_region structure
  159. */
  160. static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
  161. {
  162. return PFN_UP(reg->base + reg->size);
  163. }
  164. #define for_each_memblock(memblock_type, region) \
  165. for (region = memblock.memblock_type.regions; \
  166. region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
  167. region++)
  168. #ifdef ARCH_DISCARD_MEMBLOCK
  169. #define __init_memblock __meminit
  170. #define __initdata_memblock __meminitdata
  171. #else
  172. #define __init_memblock
  173. #define __initdata_memblock
  174. #endif
  175. #else
  176. static inline phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align)
  177. {
  178. return 0;
  179. }
  180. #endif /* CONFIG_HAVE_MEMBLOCK */
  181. #endif /* __KERNEL__ */
  182. #endif /* _LINUX_MEMBLOCK_H */