lmb.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #ifndef _LINUX_LMB_H
  3. #define _LINUX_LMB_H
  4. #ifdef __KERNEL__
  5. #include <asm/types.h>
  6. #include <asm/u-boot.h>
  7. /*
  8. * Logical memory blocks.
  9. *
  10. * Copyright (C) 2001 Peter Bergner, IBM Corp.
  11. */
  12. /**
  13. * enum lmb_flags - definition of memory region attributes
  14. * @LMB_NONE: no special request
  15. * @LMB_NOMAP: don't add to mmu configuration
  16. */
  17. enum lmb_flags {
  18. LMB_NONE = 0x0,
  19. LMB_NOMAP = 0x4,
  20. };
  21. /**
  22. * struct lmb_property - Description of one region.
  23. *
  24. * @base: Base address of the region.
  25. * @size: Size of the region
  26. * @flags: memory region attributes
  27. */
  28. struct lmb_property {
  29. phys_addr_t base;
  30. phys_size_t size;
  31. enum lmb_flags flags;
  32. };
  33. /*
  34. * For regions size management, see LMB configuration in KConfig
  35. * all the #if test are done with CONFIG_LMB_USE_MAX_REGIONS (boolean)
  36. *
  37. * case 1. CONFIG_LMB_USE_MAX_REGIONS is defined (legacy mode)
  38. * => CONFIG_LMB_MAX_REGIONS is used to configure the region size,
  39. * directly in the array lmb_region.region[], with the same
  40. * configuration for memory and reserved regions.
  41. *
  42. * case 2. CONFIG_LMB_USE_MAX_REGIONS is not defined, the size of each
  43. * region is configurated *independently* with
  44. * => CONFIG_LMB_MEMORY_REGIONS: struct lmb.memory_regions
  45. * => CONFIG_LMB_RESERVED_REGIONS: struct lmb.reserved_regions
  46. * lmb_region.region is only a pointer to the correct buffer,
  47. * initialized in lmb_init(). This configuration is useful to manage
  48. * more reserved memory regions with CONFIG_LMB_RESERVED_REGIONS.
  49. */
  50. /**
  51. * struct lmb_region - Description of a set of region.
  52. *
  53. * @cnt: Number of regions.
  54. * @max: Size of the region array, max value of cnt.
  55. * @region: Array of the region properties
  56. */
  57. struct lmb_region {
  58. unsigned long cnt;
  59. unsigned long max;
  60. #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
  61. struct lmb_property region[CONFIG_LMB_MAX_REGIONS];
  62. #else
  63. struct lmb_property *region;
  64. #endif
  65. };
  66. /**
  67. * struct lmb - Logical memory block handle.
  68. *
  69. * Clients provide storage for Logical memory block (lmb) handles.
  70. * The content of the structure is managed by the lmb library.
  71. * A lmb struct is initialized by lmb_init() functions.
  72. * The lmb struct is passed to all other lmb APIs.
  73. *
  74. * @memory: Description of memory regions.
  75. * @reserved: Description of reserved regions.
  76. * @memory_regions: Array of the memory regions (statically allocated)
  77. * @reserved_regions: Array of the reserved regions (statically allocated)
  78. */
  79. struct lmb {
  80. struct lmb_region memory;
  81. struct lmb_region reserved;
  82. #if !IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
  83. struct lmb_property memory_regions[CONFIG_LMB_MEMORY_REGIONS];
  84. struct lmb_property reserved_regions[CONFIG_LMB_RESERVED_REGIONS];
  85. #endif
  86. };
  87. void lmb_init(struct lmb *lmb);
  88. void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob);
  89. void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
  90. phys_size_t size, void *fdt_blob);
  91. long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size);
  92. long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size);
  93. /**
  94. * lmb_reserve_flags - Reserve one region with a specific flags bitfield.
  95. *
  96. * @lmb: the logical memory block struct
  97. * @base: base address of the memory region
  98. * @size: size of the memory region
  99. * @flags: flags for the memory region
  100. * Return: 0 if OK, > 0 for coalesced region or a negative error code.
  101. */
  102. long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base,
  103. phys_size_t size, enum lmb_flags flags);
  104. phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align);
  105. phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align,
  106. phys_addr_t max_addr);
  107. phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align,
  108. phys_addr_t max_addr);
  109. phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size);
  110. phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr);
  111. /**
  112. * lmb_is_reserved() - test if address is in reserved region
  113. *
  114. * The function checks if a reserved region comprising @addr exists.
  115. *
  116. * @lmb: the logical memory block struct
  117. * @addr: address to be tested
  118. * Return: 1 if reservation exists, 0 otherwise
  119. */
  120. int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr);
  121. /**
  122. * lmb_is_reserved_flags() - test if address is in reserved region with flag bits set
  123. *
  124. * The function checks if a reserved region comprising @addr exists which has
  125. * all flag bits set which are set in @flags.
  126. *
  127. * @lmb: the logical memory block struct
  128. * @addr: address to be tested
  129. * @flags: bitmap with bits to be tested
  130. * Return: 1 if matching reservation exists, 0 otherwise
  131. */
  132. int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags);
  133. long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size);
  134. void lmb_dump_all(struct lmb *lmb);
  135. void lmb_dump_all_force(struct lmb *lmb);
  136. void board_lmb_reserve(struct lmb *lmb);
  137. void arch_lmb_reserve(struct lmb *lmb);
  138. void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align);
  139. #endif /* __KERNEL__ */
  140. #endif /* _LINUX_LMB_H */