Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 2 | #ifndef _LINUX_LMB_H |
| 3 | #define _LINUX_LMB_H |
| 4 | #ifdef __KERNEL__ |
| 5 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 6 | #include <alist.h> |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 7 | #include <asm/types.h> |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 8 | #include <asm/u-boot.h> |
Sughosh Ganu | 26d7fa1 | 2024-08-26 17:29:17 +0530 | [diff] [blame] | 9 | #include <linux/bitops.h> |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 10 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 11 | /* |
| 12 | * Logical memory blocks. |
| 13 | * |
| 14 | * Copyright (C) 2001 Peter Bergner, IBM Corp. |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 15 | */ |
| 16 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 17 | /** |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 18 | * enum lmb_flags - definition of memory region attributes |
| 19 | * @LMB_NONE: no special request |
| 20 | * @LMB_NOMAP: don't add to mmu configuration |
Sughosh Ganu | 7ebbdd7 | 2024-10-15 21:07:04 +0530 | [diff] [blame] | 21 | * @LMB_NOOVERWRITE: the memory region cannot be overwritten/re-reserved |
| 22 | * @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] | 23 | */ |
| 24 | enum lmb_flags { |
Sughosh Ganu | 26d7fa1 | 2024-08-26 17:29:17 +0530 | [diff] [blame] | 25 | LMB_NONE = 0, |
| 26 | LMB_NOMAP = BIT(1), |
Sughosh Ganu | 50e2727 | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 27 | LMB_NOOVERWRITE = BIT(2), |
Sughosh Ganu | 7ebbdd7 | 2024-10-15 21:07:04 +0530 | [diff] [blame] | 28 | LMB_NONOTIFY = BIT(3), |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | /** |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 32 | * struct lmb_region - Description of one region. |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 33 | * |
Heinrich Schuchardt | e6923f0 | 2021-11-14 08:43:07 +0100 | [diff] [blame] | 34 | * @base: Base address of the region. |
| 35 | * @size: Size of the region |
| 36 | * @flags: memory region attributes |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 37 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 38 | struct lmb_region { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 39 | phys_addr_t base; |
| 40 | phys_size_t size; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 41 | enum lmb_flags flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 42 | }; |
| 43 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 44 | /** |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 45 | * struct lmb - The LMB structure |
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 | * @free_mem: List of free memory regions |
| 48 | * @used_mem: List of used/reserved memory regions |
Sughosh Ganu | a3af5ba | 2024-10-15 21:07:07 +0530 | [diff] [blame] | 49 | * @test: Is structure being used for LMB tests |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 50 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 51 | struct lmb { |
| 52 | struct alist free_mem; |
| 53 | struct alist used_mem; |
Sughosh Ganu | a3af5ba | 2024-10-15 21:07:07 +0530 | [diff] [blame] | 54 | bool test; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 55 | }; |
| 56 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 57 | /** |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 58 | * lmb_init() - Initialise the LMB module |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 59 | * |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 60 | * Initialise the LMB lists needed for keeping the memory map. There |
| 61 | * are two lists, in form of alloced list data structure. One for the |
| 62 | * available memory, and one for the used memory. Initialise the two |
| 63 | * lists as part of board init. Add memory to the available memory |
| 64 | * list and reserve common areas by adding them to the used memory |
| 65 | * list. |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 66 | * |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 67 | * Return: 0 on success, -ve on error |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 68 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 69 | int lmb_init(void); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 70 | |
Sughosh Ganu | 65597fa | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 71 | /** |
| 72 | * lmb_add_memory() - Add memory range for LMB allocations |
| 73 | * |
| 74 | * Add the entire available memory range to the pool of memory that |
| 75 | * can be used by the LMB module for allocations. |
| 76 | * |
| 77 | * Return: None |
| 78 | */ |
| 79 | void lmb_add_memory(void); |
| 80 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 81 | long lmb_add(phys_addr_t base, phys_size_t size); |
| 82 | long lmb_reserve(phys_addr_t base, phys_size_t size); |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 83 | /** |
| 84 | * lmb_reserve_flags - Reserve one region with a specific flags bitfield. |
| 85 | * |
Heinrich Schuchardt | e6923f0 | 2021-11-14 08:43:07 +0100 | [diff] [blame] | 86 | * @base: base address of the memory region |
| 87 | * @size: size of the memory region |
| 88 | * @flags: flags for the memory region |
| 89 | * Return: 0 if OK, > 0 for coalesced region or a negative error code. |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 90 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 91 | long lmb_reserve_flags(phys_addr_t base, phys_size_t size, |
| 92 | enum lmb_flags flags); |
| 93 | phys_addr_t lmb_alloc(phys_size_t size, ulong align); |
| 94 | phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr); |
| 95 | phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size); |
| 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 | |
| 98 | /** |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 99 | * lmb_alloc_flags() - Allocate memory region with specified attributes |
| 100 | * @size: Size of the region requested |
| 101 | * @align: Alignment of the memory region requested |
| 102 | * @flags: Memory region attributes to be set |
| 103 | * |
| 104 | * Allocate a region of memory with the attributes specified through the |
| 105 | * parameter. |
| 106 | * |
| 107 | * Return: base address on success, 0 on error |
| 108 | */ |
| 109 | phys_addr_t lmb_alloc_flags(phys_size_t size, ulong align, uint flags); |
| 110 | |
| 111 | /** |
| 112 | * lmb_alloc_base_flags() - Allocate specified memory region with specified attributes |
| 113 | * @size: Size of the region requested |
| 114 | * @align: Alignment of the memory region requested |
| 115 | * @max_addr: Maximum address of the requested region |
| 116 | * @flags: Memory region attributes to be set |
| 117 | * |
| 118 | * Allocate a region of memory with the attributes specified through the |
| 119 | * parameter. The max_addr parameter is used to specify the maximum address |
| 120 | * below which the requested region should be allocated. |
| 121 | * |
| 122 | * Return: base address on success, 0 on error |
| 123 | */ |
| 124 | phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align, |
| 125 | phys_addr_t max_addr, uint flags); |
| 126 | |
| 127 | /** |
| 128 | * lmb_alloc_addr_flags() - Allocate specified memory address with specified attributes |
| 129 | * @base: Base Address requested |
| 130 | * @size: Size of the region requested |
| 131 | * @flags: Memory region attributes to be set |
| 132 | * |
| 133 | * Allocate a region of memory with the attributes specified through the |
| 134 | * parameter. The base parameter is used to specify the base address |
| 135 | * of the requested region. |
| 136 | * |
| 137 | * Return: base address on success, 0 on error |
| 138 | */ |
| 139 | phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size, |
| 140 | uint flags); |
| 141 | |
| 142 | /** |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 143 | * lmb_is_reserved_flags() - test if address is in reserved region with flag bits set |
| 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 | * |
Heinrich Schuchardt | e6923f0 | 2021-11-14 08:43:07 +0100 | [diff] [blame] | 148 | * @addr: address to be tested |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 149 | * @flags: bitmap with bits to be tested |
| 150 | * Return: 1 if matching reservation exists, 0 otherwise |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 151 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 152 | int lmb_is_reserved_flags(phys_addr_t addr, int flags); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 153 | |
Sughosh Ganu | 7f15d32 | 2024-10-15 21:07:03 +0530 | [diff] [blame] | 154 | /** |
| 155 | * lmb_free_flags() - Free up a region of memory |
| 156 | * @base: Base Address of region to be freed |
| 157 | * @size: Size of the region to be freed |
| 158 | * @flags: Memory region attributes |
| 159 | * |
| 160 | * Free up a region of memory. |
| 161 | * |
| 162 | * Return: 0 if successful, -1 on failure |
| 163 | */ |
| 164 | long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags); |
| 165 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 166 | long lmb_free(phys_addr_t base, phys_size_t size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 167 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 168 | void lmb_dump_all(void); |
| 169 | void lmb_dump_all_force(void); |
| 170 | |
Sughosh Ganu | 1a36d44 | 2024-10-15 21:07:11 +0530 | [diff] [blame] | 171 | void lmb_arch_add_memory(void); |
| 172 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 173 | struct lmb *lmb_get(void); |
| 174 | int lmb_push(struct lmb *store); |
| 175 | void lmb_pop(struct lmb *store); |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 176 | |
Prasad Kummari | 940bebc | 2024-09-13 13:02:52 +0530 | [diff] [blame] | 177 | static inline int lmb_read_check(phys_addr_t addr, phys_size_t len) |
| 178 | { |
| 179 | return lmb_alloc_addr(addr, len) == addr ? 0 : -1; |
| 180 | } |
| 181 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 182 | #endif /* __KERNEL__ */ |
| 183 | |
| 184 | #endif /* _LINUX_LMB_H */ |