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 |
| 30 | #define LMB_NOMAP BIT(0) |
| 31 | #define LMB_NOOVERWRITE BIT(1) |
| 32 | #define LMB_NONOTIFY BIT(2) |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 33 | |
| 34 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 35 | * struct lmb_region - Description of one region |
| 36 | * @base: Base address of the region |
| 37 | * @size: Size of the region |
| 38 | * @flags: Memory region attributes |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 39 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 40 | struct lmb_region { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 41 | phys_addr_t base; |
| 42 | phys_size_t size; |
Ilias Apalodimas | d8462bf | 2024-12-18 09:02:31 +0200 | [diff] [blame] | 43 | u32 flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 44 | }; |
| 45 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 46 | /** |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 47 | * struct lmb - The LMB structure |
Ilias Apalodimas | 5421c33 | 2024-12-18 09:02:33 +0200 | [diff] [blame] | 48 | * @available_mem: List of memory available to LMB |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 49 | * @used_mem: List of used/reserved memory regions |
| 50 | * @test: Is structure being used for LMB tests |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 51 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 52 | struct lmb { |
Ilias Apalodimas | 5421c33 | 2024-12-18 09:02:33 +0200 | [diff] [blame] | 53 | struct alist available_mem; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 54 | struct alist used_mem; |
Sughosh Ganu | a3af5ba | 2024-10-15 21:07:07 +0530 | [diff] [blame] | 55 | bool test; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 56 | }; |
| 57 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 58 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 59 | * lmb_init() - Initialise the LMB module. |
| 60 | * |
| 61 | * Return: 0 on success, negative error code on failure. |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 62 | * |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 63 | * Initialise the LMB lists needed for keeping the memory map. There |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 64 | * 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] | 65 | * available memory, and one for the used memory. Initialise the two |
| 66 | * lists as part of board init. Add memory to the available memory |
| 67 | * list and reserve common areas by adding them to the used memory |
| 68 | * list. |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 69 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 70 | int lmb_init(void); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 71 | |
Sughosh Ganu | 65597fa | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 72 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 73 | * lmb_add_memory() - Add memory range for LMB allocations. |
Sughosh Ganu | 65597fa | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 74 | * |
| 75 | * Add the entire available memory range to the pool of memory that |
| 76 | * can be used by the LMB module for allocations. |
Sughosh Ganu | 65597fa | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 77 | */ |
| 78 | void lmb_add_memory(void); |
| 79 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 80 | long lmb_add(phys_addr_t base, phys_size_t size); |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 81 | |
| 82 | /** |
Ilias Apalodimas | f72c55e | 2024-12-18 09:02:32 +0200 | [diff] [blame] | 83 | * lmb_reserve() - Reserve one region with a specific flags bitfield |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 84 | * @base: Base address of the memory region |
| 85 | * @size: Size of the memory region |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 86 | * @flags: Flags for the memory region |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 87 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 88 | * Return: |
| 89 | * * %0 - Added successfully, or it's already added (only if LMB_NONE) |
| 90 | * * %-EEXIST - The region is already added, and flags != LMB_NONE |
| 91 | * * %-1 - Failure |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 92 | */ |
Ilias Apalodimas | f72c55e | 2024-12-18 09:02:32 +0200 | [diff] [blame] | 93 | long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags); |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 94 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 95 | phys_addr_t lmb_alloc(phys_size_t size, ulong align); |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 96 | phys_size_t lmb_get_free_size(phys_addr_t addr); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 97 | |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 98 | /** |
Ilias Apalodimas | d1e9a26 | 2024-12-18 09:02:36 +0200 | [diff] [blame] | 99 | * lmb_alloc_base() - Allocate specified memory region with specified |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 100 | * attributes |
| 101 | * @size: Size of the region requested |
| 102 | * @align: Alignment of the memory region requested |
| 103 | * @max_addr: Maximum address of the requested region |
| 104 | * @flags: Memory region attributes to be set |
| 105 | * |
| 106 | * Allocate a region of memory with the attributes specified through the |
| 107 | * parameter. The max_addr parameter is used to specify the maximum address |
| 108 | * below which the requested region should be allocated. |
| 109 | * |
| 110 | * Return: Base address on success, 0 on error. |
| 111 | */ |
Ilias Apalodimas | d1e9a26 | 2024-12-18 09:02:36 +0200 | [diff] [blame] | 112 | phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr, |
| 113 | uint flags); |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 114 | |
| 115 | /** |
Ilias Apalodimas | cc2ed3d | 2024-12-18 09:02:35 +0200 | [diff] [blame] | 116 | * lmb_alloc_addr() - Allocate specified memory address with specified attributes |
| 117 | * |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 118 | * @base: Base Address requested |
| 119 | * @size: Size of the region requested |
| 120 | * @flags: Memory region attributes to be set |
| 121 | * |
| 122 | * Allocate a region of memory with the attributes specified through the |
| 123 | * parameter. The base parameter is used to specify the base address |
| 124 | * of the requested region. |
| 125 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 126 | * Return: Base address on success, 0 on error. |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 127 | */ |
Ilias Apalodimas | 31bf8f1 | 2024-12-18 09:02:37 +0200 | [diff] [blame] | 128 | phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags); |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 129 | |
| 130 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 131 | * lmb_is_reserved_flags() - Test if address is in reserved region with flag |
| 132 | * bits set |
| 133 | * @addr: Address to be tested |
| 134 | * @flags: Bitmap with bits to be tested |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 135 | * |
| 136 | * The function checks if a reserved region comprising @addr exists which has |
| 137 | * all flag bits set which are set in @flags. |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 138 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 139 | * Return: 1 if matching reservation exists, 0 otherwise. |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 140 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 141 | int lmb_is_reserved_flags(phys_addr_t addr, int flags); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 142 | |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 143 | /** |
| 144 | * lmb_free_flags() - Free up a region of memory |
| 145 | * @base: Base Address of region to be freed |
| 146 | * @size: Size of the region to be freed |
| 147 | * @flags: Memory region attributes |
| 148 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 149 | * Return: 0 on success, negative error code on failure. |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 150 | */ |
| 151 | long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags); |
| 152 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 153 | long lmb_free(phys_addr_t base, phys_size_t size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 154 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 155 | void lmb_dump_all(void); |
| 156 | void lmb_dump_all_force(void); |
| 157 | |
Sughosh Ganu | 1a36d44 | 2024-10-15 21:07:11 +0530 | [diff] [blame] | 158 | void lmb_arch_add_memory(void); |
| 159 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 160 | struct lmb *lmb_get(void); |
| 161 | int lmb_push(struct lmb *store); |
| 162 | void lmb_pop(struct lmb *store); |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 163 | |
Prasad Kummari | 940bebc | 2024-09-13 13:02:52 +0530 | [diff] [blame] | 164 | static inline int lmb_read_check(phys_addr_t addr, phys_size_t len) |
| 165 | { |
Ilias Apalodimas | cc2ed3d | 2024-12-18 09:02:35 +0200 | [diff] [blame] | 166 | return lmb_alloc_addr(addr, len, LMB_NONE) == addr ? 0 : -1; |
Prasad Kummari | 940bebc | 2024-09-13 13:02:52 +0530 | [diff] [blame] | 167 | } |
| 168 | |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 169 | /** |
| 170 | * io_lmb_setup() - Initialize LMB struct |
Tom Rini | d32dddb | 2024-11-11 07:26:50 -0600 | [diff] [blame] | 171 | * @io_lmb: IO LMB to initialize |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 172 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 173 | * Return: 0 on success, negative error code on failure. |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 174 | */ |
| 175 | int io_lmb_setup(struct lmb *io_lmb); |
| 176 | |
| 177 | /** |
| 178 | * io_lmb_teardown() - Tear LMB struct down |
Tom Rini | d32dddb | 2024-11-11 07:26:50 -0600 | [diff] [blame] | 179 | * @io_lmb: IO LMB to teardown |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 180 | */ |
| 181 | void io_lmb_teardown(struct lmb *io_lmb); |
| 182 | |
| 183 | /** |
| 184 | * io_lmb_add() - Add an IOVA range for allocations |
| 185 | * @io_lmb: LMB to add the space to |
| 186 | * @base: Base Address of region to add |
| 187 | * @size: Size of the region to add |
| 188 | * |
| 189 | * Add the IOVA space [base, base + size] to be managed by io_lmb. |
| 190 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 191 | * Return: 0 on success, negative error code on failure. |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 192 | */ |
| 193 | long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size); |
| 194 | |
| 195 | /** |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 196 | * io_lmb_alloc() - Allocate specified IO memory address with specified |
| 197 | * alignment |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 198 | * @io_lmb: LMB to alloc from |
| 199 | * @size: Size of the region requested |
| 200 | * @align: Required address and size alignment |
| 201 | * |
| 202 | * Allocate a region of IO memory. The base parameter is used to specify the |
| 203 | * base address of the requested region. |
| 204 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 205 | * Return: Base IO address on success, 0 on error. |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 206 | */ |
| 207 | phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align); |
| 208 | |
| 209 | /** |
| 210 | * io_lmb_free() - Free up a region of IOVA space |
| 211 | * @io_lmb: LMB to return the IO address space to |
| 212 | * @base: Base Address of region to be freed |
| 213 | * @size: Size of the region to be freed |
| 214 | * |
Sam Protsenko | fd04259 | 2024-12-10 20:25:50 -0600 | [diff] [blame] | 215 | * Return: 0 on success, negative error code on failure. |
Janne Grunau | e818d3c | 2024-11-11 07:56:33 +0100 | [diff] [blame] | 216 | */ |
| 217 | long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size); |
| 218 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 219 | #endif /* __KERNEL__ */ |
| 220 | |
| 221 | #endif /* _LINUX_LMB_H */ |