1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Logical memory blocks.
5 * Copyright (C) 2001 Peter Bergner, IBM Corp.
14 #include <asm/types.h>
15 #include <asm/u-boot.h>
16 #include <linux/bitops.h>
18 #define LMB_ALLOC_ANYWHERE 0
19 #define LMB_ALIST_INITIAL_SIZE 4
22 * enum lmb_flags - Definition of memory region attributes
23 * @LMB_NONE: No special request
24 * @LMB_NOMAP: Don't add to MMU configuration
25 * @LMB_NOOVERWRITE: The memory region cannot be overwritten/re-reserved
26 * @LMB_NONOTIFY: Do not notify other modules of changes to this memory region
31 LMB_NOOVERWRITE = BIT(2),
32 LMB_NONOTIFY = BIT(3),
36 * struct lmb_region - Description of one region
37 * @base: Base address of the region
38 * @size: Size of the region
39 * @flags: Memory region attributes
48 * struct lmb - The LMB structure
49 * @free_mem: List of free memory regions
50 * @used_mem: List of used/reserved memory regions
51 * @test: Is structure being used for LMB tests
54 struct alist free_mem;
55 struct alist used_mem;
60 * lmb_init() - Initialise the LMB module.
62 * Return: 0 on success, negative error code on failure.
64 * Initialise the LMB lists needed for keeping the memory map. There
65 * are two lists, in form of allocated list data structure. One for the
66 * available memory, and one for the used memory. Initialise the two
67 * lists as part of board init. Add memory to the available memory
68 * list and reserve common areas by adding them to the used memory
74 * lmb_add_memory() - Add memory range for LMB allocations.
76 * Add the entire available memory range to the pool of memory that
77 * can be used by the LMB module for allocations.
79 void lmb_add_memory(void);
81 long lmb_add(phys_addr_t base, phys_size_t size);
84 * lmb_reserve() - Reserve a memory region (with no special flags)
85 * @base: Base address of the memory region
86 * @size: Size of the memory region
88 * Return: 0 on success, negative error code on failure.
90 long lmb_reserve(phys_addr_t base, phys_size_t size);
93 * lmb_reserve_flags() - Reserve one region with a specific flags bitfield
94 * @base: Base address of the memory region
95 * @size: Size of the memory region
96 * @flags: Flags for the memory region
99 * * %0 - Added successfully, or it's already added (only if LMB_NONE)
100 * * %-EEXIST - The region is already added, and flags != LMB_NONE
103 long lmb_reserve_flags(phys_addr_t base, phys_size_t size,
104 enum lmb_flags flags);
106 phys_addr_t lmb_alloc(phys_size_t size, ulong align);
107 phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr);
108 phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size);
109 phys_size_t lmb_get_free_size(phys_addr_t addr);
112 * lmb_alloc_base_flags() - Allocate specified memory region with specified
114 * @size: Size of the region requested
115 * @align: Alignment of the memory region requested
116 * @max_addr: Maximum address of the requested region
117 * @flags: Memory region attributes to be set
119 * Allocate a region of memory with the attributes specified through the
120 * parameter. The max_addr parameter is used to specify the maximum address
121 * below which the requested region should be allocated.
123 * Return: Base address on success, 0 on error.
125 phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
126 phys_addr_t max_addr, uint flags);
129 * lmb_alloc_addr_flags() - Allocate specified memory address with specified
131 * @base: Base Address requested
132 * @size: Size of the region requested
133 * @flags: Memory region attributes to be set
135 * Allocate a region of memory with the attributes specified through the
136 * parameter. The base parameter is used to specify the base address
137 * of the requested region.
139 * Return: Base address on success, 0 on error.
141 phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
145 * lmb_is_reserved_flags() - Test if address is in reserved region with flag
147 * @addr: Address to be tested
148 * @flags: Bitmap with bits to be tested
150 * The function checks if a reserved region comprising @addr exists which has
151 * all flag bits set which are set in @flags.
153 * Return: 1 if matching reservation exists, 0 otherwise.
155 int lmb_is_reserved_flags(phys_addr_t addr, int flags);
158 * lmb_free_flags() - Free up a region of memory
159 * @base: Base Address of region to be freed
160 * @size: Size of the region to be freed
161 * @flags: Memory region attributes
163 * Return: 0 on success, negative error code on failure.
165 long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
167 long lmb_free(phys_addr_t base, phys_size_t size);
169 void lmb_dump_all(void);
170 void lmb_dump_all_force(void);
172 void lmb_arch_add_memory(void);
174 struct lmb *lmb_get(void);
175 int lmb_push(struct lmb *store);
176 void lmb_pop(struct lmb *store);
178 static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
180 return lmb_alloc_addr(addr, len) == addr ? 0 : -1;
184 * io_lmb_setup() - Initialize LMB struct
185 * @io_lmb: IO LMB to initialize
187 * Return: 0 on success, negative error code on failure.
189 int io_lmb_setup(struct lmb *io_lmb);
192 * io_lmb_teardown() - Tear LMB struct down
193 * @io_lmb: IO LMB to teardown
195 void io_lmb_teardown(struct lmb *io_lmb);
198 * io_lmb_add() - Add an IOVA range for allocations
199 * @io_lmb: LMB to add the space to
200 * @base: Base Address of region to add
201 * @size: Size of the region to add
203 * Add the IOVA space [base, base + size] to be managed by io_lmb.
205 * Return: 0 on success, negative error code on failure.
207 long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
210 * io_lmb_alloc() - Allocate specified IO memory address with specified
212 * @io_lmb: LMB to alloc from
213 * @size: Size of the region requested
214 * @align: Required address and size alignment
216 * Allocate a region of IO memory. The base parameter is used to specify the
217 * base address of the requested region.
219 * Return: Base IO address on success, 0 on error.
221 phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align);
224 * io_lmb_free() - Free up a region of IOVA space
225 * @io_lmb: LMB to return the IO address space to
226 * @base: Base Address of region to be freed
227 * @size: Size of the region to be freed
229 * Return: 0 on success, negative error code on failure.
231 long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
233 #endif /* __KERNEL__ */
235 #endif /* _LINUX_LMB_H */