Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Logical memory blocks. |
| 4 | * |
| 5 | * Copyright (C) 2001 Peter Bergner, IBM Corp. |
| 6 | */ |
| 7 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 8 | #ifndef _LINUX_LMB_H |
| 9 | #define _LINUX_LMB_H |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 10 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 11 | #ifdef __KERNEL__ |
| 12 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 13 | #include <alist.h> |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 14 | #include <asm/types.h> |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 15 | #include <asm/u-boot.h> |
Sughosh Ganu | 26d7fa1 | 2024-08-26 17:29:17 +0530 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 17 | |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 18 | #define LMB_ALLOC_ANYWHERE 0 |
| 19 | #define LMB_ALIST_INITIAL_SIZE 4 |
Ilias Apalodimas | 7d182fb | 2024-10-23 18:26:36 +0300 | [diff] [blame] | 20 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 21 | /** |
Ilias Apalodimas | d8462bf | 2024-12-18 09:02:31 +0200 | [diff] [blame] | 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 |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 28 | */ |
Ilias Apalodimas | d8462bf | 2024-12-18 09:02:31 +0200 | [diff] [blame] | 29 | #define LMB_NONE 0 |
Sughosh Ganu | 61d7d9e | 2025-04-23 17:01:23 +0530 | [diff] [blame] | 30 | #define LMB_NOMAP BIT(1) |
| 31 | #define LMB_NOOVERWRITE BIT(2) |
| 32 | #define LMB_NONOTIFY BIT(3) |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 33 | |
| 34 | /** |
Sughosh Ganu | 9b0765a | 2025-06-17 16:13:40 +0530 | [diff] [blame] | 35 | * enum lmb_mem_type - type of memory allocation request |
| 36 | * @LMB_MEM_ALLOC_ADDR: request for a particular region of memory |
Sughosh Ganu | 7bdfe12 | 2025-06-17 16:13:41 +0530 | [diff] [blame^] | 37 | * @LMB_MEM_ALLOC_ANY: allocate any available memory region |
| 38 | * @LMB_MEM_ALLOC_MAX: allocate memory below a particular address |
Sughosh Ganu | 9b0765a | 2025-06-17 16:13:40 +0530 | [diff] [blame] | 39 | */ |
| 40 | enum lmb_mem_type { |
| 41 | LMB_MEM_ALLOC_ADDR = 1, |
Sughosh Ganu | 7bdfe12 | 2025-06-17 16:13:41 +0530 | [diff] [blame^] | 42 | LMB_MEM_ALLOC_ANY, |
| 43 | LMB_MEM_ALLOC_MAX, |
Sughosh Ganu | 9b0765a | 2025-06-17 16:13:40 +0530 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /** |
Heinrich Schuchardt | 63bb30b | 2025-02-16 12:12:39 +0100 | [diff] [blame] | 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 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 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 |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 63 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 64 | struct lmb_region { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 65 | phys_addr_t base; |
| 66 | phys_size_t size; |
Ilias Apalodimas | d8462bf | 2024-12-18 09:02:31 +0200 | [diff] [blame] | 67 | u32 flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 68 | }; |
| 69 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 70 | /** |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 71 | * struct lmb - The LMB structure |
Ilias Apalodimas | 5421c33 | 2024-12-18 09:02:33 +0200 | [diff] [blame] | 72 | * @available_mem: List of memory available to LMB |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 73 | * @used_mem: List of used/reserved memory regions |
| 74 | * @test: Is structure being used for LMB tests |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 75 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 76 | struct lmb { |
Ilias Apalodimas | 5421c33 | 2024-12-18 09:02:33 +0200 | [diff] [blame] | 77 | struct alist available_mem; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 78 | struct alist used_mem; |
Sughosh Ganu | a3af5ba | 2024-10-15 21:07:07 +0530 | [diff] [blame] | 79 | bool test; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 80 | }; |
| 81 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 82 | /** |
Sughosh Ganu | 9b0765a | 2025-06-17 16:13:40 +0530 | [diff] [blame] | 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 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 114 | * lmb_init() - Initialise the LMB module. |
| 115 | * |
| 116 | * Return: 0 on success, negative error code on failure. |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 117 | * |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 118 | * Initialise the LMB lists needed for keeping the memory map. There |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 119 | * are two lists, in form of allocated list data structure. One for the |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 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. |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 124 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 125 | int lmb_init(void); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 126 | |
Sughosh Ganu | 65597fa | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 127 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 128 | * lmb_add_memory() - Add memory range for LMB allocations. |
Sughosh Ganu | 65597fa | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 129 | * |
| 130 | * Add the entire available memory range to the pool of memory that |
| 131 | * can be used by the LMB module for allocations. |
Sughosh Ganu | 65597fa | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 132 | */ |
| 133 | void lmb_add_memory(void); |
| 134 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 135 | long lmb_add(phys_addr_t base, phys_size_t size); |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 136 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 137 | phys_size_t lmb_get_free_size(phys_addr_t addr); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 138 | |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 139 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 140 | * lmb_is_reserved_flags() - Test if address is in reserved region with flag |
| 141 | * bits set |
| 142 | * @addr: Address to be tested |
| 143 | * @flags: Bitmap with bits to be tested |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 144 | * |
| 145 | * The function checks if a reserved region comprising @addr exists which has |
| 146 | * all flag bits set which are set in @flags. |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 147 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 148 | * Return: 1 if matching reservation exists, 0 otherwise. |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 149 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 150 | int lmb_is_reserved_flags(phys_addr_t addr, int flags); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 151 | |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 152 | /** |
| 153 | * lmb_free_flags() - Free up a region of memory |
| 154 | * @base: Base Address of region to be freed |
| 155 | * @size: Size of the region to be freed |
| 156 | * @flags: Memory region attributes |
| 157 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 158 | * Return: 0 on success, negative error code on failure. |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 159 | */ |
| 160 | long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags); |
| 161 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 162 | long lmb_free(phys_addr_t base, phys_size_t size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 163 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 164 | void lmb_dump_all(void); |
| 165 | void lmb_dump_all_force(void); |
| 166 | |
Sughosh Ganu | 1a36d44 | 2024-10-15 21:07:11 +0530 | [diff] [blame] | 167 | void lmb_arch_add_memory(void); |
| 168 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 169 | struct lmb *lmb_get(void); |
| 170 | int lmb_push(struct lmb *store); |
| 171 | void lmb_pop(struct lmb *store); |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 172 | |
Prasad Kummari | 940bebc | 2024-09-13 13:02:52 +0530 | [diff] [blame] | 173 | static inline int lmb_read_check(phys_addr_t addr, phys_size_t len) |
| 174 | { |
Sughosh Ganu | 9b0765a | 2025-06-17 16:13:40 +0530 | [diff] [blame] | 175 | return lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, len, LMB_NONE); |
Prasad Kummari | 940bebc | 2024-09-13 13:02:52 +0530 | [diff] [blame] | 176 | } |
| 177 | |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 178 | /** |
| 179 | * io_lmb_setup() - Initialize LMB struct |
Tom Rini | d32dddb | 2024-11-11 07:26:50 -0600 | [diff] [blame] | 180 | * @io_lmb: IO LMB to initialize |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 181 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 182 | * Return: 0 on success, negative error code on failure. |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 183 | */ |
| 184 | int io_lmb_setup(struct lmb *io_lmb); |
| 185 | |
| 186 | /** |
| 187 | * io_lmb_teardown() - Tear LMB struct down |
Tom Rini | d32dddb | 2024-11-11 07:26:50 -0600 | [diff] [blame] | 188 | * @io_lmb: IO LMB to teardown |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 189 | */ |
| 190 | void io_lmb_teardown(struct lmb *io_lmb); |
| 191 | |
| 192 | /** |
| 193 | * io_lmb_add() - Add an IOVA range for allocations |
| 194 | * @io_lmb: LMB to add the space to |
| 195 | * @base: Base Address of region to add |
| 196 | * @size: Size of the region to add |
| 197 | * |
| 198 | * Add the IOVA space [base, base + size] to be managed by io_lmb. |
| 199 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 200 | * Return: 0 on success, negative error code on failure. |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 201 | */ |
| 202 | long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size); |
| 203 | |
| 204 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 205 | * io_lmb_alloc() - Allocate specified IO memory address with specified |
| 206 | * alignment |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 207 | * @io_lmb: LMB to alloc from |
| 208 | * @size: Size of the region requested |
| 209 | * @align: Required address and size alignment |
| 210 | * |
| 211 | * Allocate a region of IO memory. The base parameter is used to specify the |
| 212 | * base address of the requested region. |
| 213 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 214 | * Return: Base IO address on success, 0 on error. |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 215 | */ |
| 216 | phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align); |
| 217 | |
| 218 | /** |
| 219 | * io_lmb_free() - Free up a region of IOVA space |
| 220 | * @io_lmb: LMB to return the IO address space to |
| 221 | * @base: Base Address of region to be freed |
| 222 | * @size: Size of the region to be freed |
| 223 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 224 | * Return: 0 on success, negative error code on failure. |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 225 | */ |
| 226 | long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size); |
| 227 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 228 | #endif /* __KERNEL__ */ |
| 229 | |
| 230 | #endif /* _LINUX_LMB_H */ |