1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Logical memory blocks.
4  *
5  * Copyright (C) 2001 Peter Bergner, IBM Corp.
6  */
7 
8 #ifndef _LINUX_LMB_H
9 #define _LINUX_LMB_H
10 
11 #ifdef __KERNEL__
12 
13 #include <alist.h>
14 #include <asm/types.h>
15 #include <asm/u-boot.h>
16 #include <linux/bitops.h>
17 
18 #define LMB_ALLOC_ANYWHERE	0
19 #define LMB_ALIST_INITIAL_SIZE	4
20 
21 /**
22  * DOC: Memory region attribute flags.
23  *
24  * %LMB_NONE: No special request
25  * %LMB_NOMAP: Don't add to MMU configuration
26  * %LMB_NOOVERWRITE: The memory region cannot be overwritten/re-reserved
27  * %LMB_NONOTIFY: Do not notify other modules of changes to this memory region
28  */
29 #define LMB_NONE 0
30 #define LMB_NOMAP BIT(1)
31 #define LMB_NOOVERWRITE BIT(2)
32 #define LMB_NONOTIFY BIT(3)
33 
34 /**
35  * enum lmb_mem_type - type of memory allocation request
36  * @LMB_MEM_ALLOC_ADDR:	request for a particular region of memory
37  * @LMB_MEM_ALLOC_ANY:	allocate any available memory region
38  * @LMB_MEM_ALLOC_MAX:	allocate memory below a particular address
39  */
40 enum lmb_mem_type {
41 	LMB_MEM_ALLOC_ADDR = 1,
42 	LMB_MEM_ALLOC_ANY,
43 	LMB_MEM_ALLOC_MAX,
44 };
45 
46 /**
47  * enum lmb_map_op - memory map operation
48  */
49 enum lmb_map_op {
50 	/** @LMB_MAP_OP_RESERVE:	reserve memory */
51 	LMB_MAP_OP_RESERVE = 1,
52 	/** @LMB_MAP_OP_FREE:		free memory */
53 	LMB_MAP_OP_FREE,
54 	/** @LMB_MAP_OP_ADD:		add memory */
55 	LMB_MAP_OP_ADD,
56 };
57 
58 /**
59  * struct lmb_region - Description of one region
60  * @base: Base address of the region
61  * @size: Size of the region
62  * @flags: Memory region attributes
63  */
64 struct lmb_region {
65 	phys_addr_t base;
66 	phys_size_t size;
67 	u32 flags;
68 };
69 
70 /**
71  * struct lmb - The LMB structure
72  * @available_mem: List of memory available to LMB
73  * @used_mem: List of used/reserved memory regions
74  * @test: Is structure being used for LMB tests
75  */
76 struct lmb {
77 	struct alist available_mem;
78 	struct alist used_mem;
79 	bool test;
80 };
81 
82 /**
83  * lmb_alloc_mem() - Request LMB memory
84  * @type:		Type of memory allocation request
85  * @align:		Alignment of the memory region requested(0 for none)
86  * @addr:		Base address of the allocated memory region
87  * @size:		Size in bytes of the allocation request
88  * @flags:		Memory region attributes to be set
89  *
90  * Allocate a region of memory where the allocation is based on the parameters
91  * that have been passed to the function.The first parameter specifies the
92  * type of allocation that is being requested. The second parameter, @align
93  * is used to specify if the allocation is to be made with a particular
94  * alignment. Use 0 for no alignment requirements.
95  *
96  * The allocated address is returned through the @addr parameter when @type
97  * is @LMB_MEM_ALLOC_ANY or @LMB_MEM_ALLOC_MAX. If @type is
98  * @LMB_MEM_ALLOC_ADDR the @addr parameter would contain the address being
99  * requested.
100  *
101  * The flags parameter is used to specify the memory attributes of the
102  * requested region.
103  *
104  * Return: 0 on success, -ve value on failure
105  *
106  * When the allocation is of type @LMB_MEM_ALLOC_ADDR, the return value can
107  * be -EINVAL if the requested memory region is not part of the LMB memory
108  * map, and -EEXIST if the requested region is already allocated.
109  */
110 int lmb_alloc_mem(enum lmb_mem_type type, u64 align, phys_addr_t *addr,
111 		  phys_size_t size, u32 flags);
112 
113 /**
114  * lmb_init() - Initialise the LMB module.
115  *
116  * Return: 0 on success, negative error code on failure.
117  *
118  * Initialise the LMB lists needed for keeping the memory map. There
119  * are two lists, in form of allocated list data structure. One for the
120  * available memory, and one for the used memory. Initialise the two
121  * lists as part of board init. Add memory to the available memory
122  * list and reserve common areas by adding them to the used memory
123  * list.
124  */
125 int lmb_init(void);
126 
127 long lmb_add(phys_addr_t base, phys_size_t size);
128 
129 phys_size_t lmb_get_free_size(phys_addr_t addr);
130 
131 /**
132  * lmb_is_reserved_flags() - Test if address is in reserved region with flag
133  *			     bits set
134  * @addr: Address to be tested
135  * @flags: Bitmap with bits to be tested
136  *
137  * The function checks if a reserved region comprising @addr exists which has
138  * all flag bits set which are set in @flags.
139  *
140  * Return: 1 if matching reservation exists, 0 otherwise.
141  */
142 int lmb_is_reserved_flags(phys_addr_t addr, int flags);
143 
144 /**
145  * lmb_free() - Free up a region of memory
146  * @base: Base Address of region to be freed
147  * @size: Size of the region to be freed
148  * @flags: Memory region attributes
149  *
150  * Return: 0 on success, negative error code on failure.
151  */
152 long lmb_free(phys_addr_t base, phys_size_t size, u32 flags);
153 
154 void lmb_dump_all(void);
155 void lmb_dump_all_force(void);
156 
157 void lmb_arch_add_memory(void);
158 
159 struct lmb *lmb_get(void);
160 int lmb_push(struct lmb *store);
161 void lmb_pop(struct lmb *store);
162 
lmb_read_check(phys_addr_t addr,phys_size_t len)163 static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
164 {
165 	return lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, len, LMB_NONE);
166 }
167 
168 /**
169  * io_lmb_setup() - Initialize LMB struct
170  * @io_lmb: IO LMB to initialize
171  *
172  * Return: 0 on success, negative error code on failure.
173  */
174 int io_lmb_setup(struct lmb *io_lmb);
175 
176 /**
177  * io_lmb_teardown() - Tear LMB struct down
178  * @io_lmb: IO LMB to teardown
179  */
180 void io_lmb_teardown(struct lmb *io_lmb);
181 
182 /**
183  * io_lmb_add() - Add an IOVA range for allocations
184  * @io_lmb: LMB to add the space to
185  * @base: Base Address of region to add
186  * @size: Size of the region to add
187  *
188  * Add the IOVA space [base, base + size] to be managed by io_lmb.
189  *
190  * Return: 0 on success, negative error code on failure.
191  */
192 long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
193 
194 /**
195  * io_lmb_alloc() - Allocate specified IO memory address with specified
196  *		    alignment
197  * @io_lmb: LMB to alloc from
198  * @size: Size of the region requested
199  * @align: Required address and size alignment
200  *
201  * Allocate a region of IO memory. The base parameter is used to specify the
202  * base address of the requested region.
203  *
204  * Return: Base IO address on success, 0 on error.
205  */
206 phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align);
207 
208 /**
209  * io_lmb_free() - Free up a region of IOVA space
210  * @io_lmb: LMB to return the IO address space to
211  * @base: Base Address of region to be freed
212  * @size: Size of the region to be freed
213  *
214  * Return: 0 on success, negative error code on failure.
215  */
216 long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
217 
218 #endif /* __KERNEL__ */
219 
220 #endif /* _LINUX_LMB_H */
221