blob: 34dbc25759dd087367ea800c0420c1bae2ee0fc9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Sam Protsenkofd042592024-12-10 20:25:50 -06002/*
3 * Logical memory blocks.
4 *
5 * Copyright (C) 2001 Peter Bergner, IBM Corp.
6 */
7
Kumar Gala6d7bfa82008-02-27 21:51:47 -06008#ifndef _LINUX_LMB_H
9#define _LINUX_LMB_H
Sam Protsenkofd042592024-12-10 20:25:50 -060010
Kumar Gala6d7bfa82008-02-27 21:51:47 -060011#ifdef __KERNEL__
12
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053013#include <alist.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060014#include <asm/types.h>
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +010015#include <asm/u-boot.h>
Sughosh Ganu26d7fa12024-08-26 17:29:17 +053016#include <linux/bitops.h>
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +010017
Sam Protsenkofd042592024-12-10 20:25:50 -060018#define LMB_ALLOC_ANYWHERE 0
19#define LMB_ALIST_INITIAL_SIZE 4
Ilias Apalodimas7d182fb2024-10-23 18:26:36 +030020
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010021/**
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +020022 * 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 Delaunaye11c9082021-05-07 14:50:29 +020028 */
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +020029#define LMB_NONE 0
Sughosh Ganu61d7d9e2025-04-23 17:01:23 +053030#define LMB_NOMAP BIT(1)
31#define LMB_NOOVERWRITE BIT(2)
32#define LMB_NONOTIFY BIT(3)
Patrick Delaunaye11c9082021-05-07 14:50:29 +020033
34/**
Sughosh Ganu9b0765a2025-06-17 16:13:40 +053035 * enum lmb_mem_type - type of memory allocation request
36 * @LMB_MEM_ALLOC_ADDR: request for a particular region of memory
Sughosh Ganu7bdfe122025-06-17 16:13:41 +053037 * @LMB_MEM_ALLOC_ANY: allocate any available memory region
38 * @LMB_MEM_ALLOC_MAX: allocate memory below a particular address
Sughosh Ganu9b0765a2025-06-17 16:13:40 +053039 */
40enum lmb_mem_type {
41 LMB_MEM_ALLOC_ADDR = 1,
Sughosh Ganu7bdfe122025-06-17 16:13:41 +053042 LMB_MEM_ALLOC_ANY,
43 LMB_MEM_ALLOC_MAX,
Sughosh Ganu9b0765a2025-06-17 16:13:40 +053044};
45
46/**
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +010047 * enum lmb_map_op - memory map operation
48 */
49enum 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 Protsenkofd042592024-12-10 20:25:50 -060059 * 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 Delaunay71cc9c52021-03-10 10:16:31 +010063 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053064struct lmb_region {
Becky Bruced26d67c2008-06-09 20:37:18 -050065 phys_addr_t base;
66 phys_size_t size;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +020067 u32 flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060068};
69
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010070/**
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053071 * struct lmb - The LMB structure
Ilias Apalodimas5421c332024-12-18 09:02:33 +020072 * @available_mem: List of memory available to LMB
Sam Protsenkofd042592024-12-10 20:25:50 -060073 * @used_mem: List of used/reserved memory regions
74 * @test: Is structure being used for LMB tests
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010075 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053076struct lmb {
Ilias Apalodimas5421c332024-12-18 09:02:33 +020077 struct alist available_mem;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053078 struct alist used_mem;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +053079 bool test;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060080};
81
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010082/**
Sughosh Ganu9b0765a2025-06-17 16:13:40 +053083 * 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 */
110int lmb_alloc_mem(enum lmb_mem_type type, u64 align, phys_addr_t *addr,
111 phys_size_t size, u32 flags);
112
113/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600114 * lmb_init() - Initialise the LMB module.
115 *
116 * Return: 0 on success, negative error code on failure.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100117 *
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530118 * Initialise the LMB lists needed for keeping the memory map. There
Sam Protsenkofd042592024-12-10 20:25:50 -0600119 * are two lists, in form of allocated list data structure. One for the
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530120 * 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 Delaunay71cc9c52021-03-10 10:16:31 +0100124 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530125int lmb_init(void);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600126
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530127/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600128 * lmb_add_memory() - Add memory range for LMB allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530129 *
130 * Add the entire available memory range to the pool of memory that
131 * can be used by the LMB module for allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530132 */
133void lmb_add_memory(void);
134
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530135long lmb_add(phys_addr_t base, phys_size_t size);
Sam Protsenkofd042592024-12-10 20:25:50 -0600136
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530137phys_size_t lmb_get_free_size(phys_addr_t addr);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200138
Sam Protsenkofd042592024-12-10 20:25:50 -0600139/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600140 * 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 Schuchardt5411fb72023-08-12 19:09:32 +0200144 *
145 * The function checks if a reserved region comprising @addr exists which has
146 * all flag bits set which are set in @flags.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200147 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600148 * Return: 1 if matching reservation exists, 0 otherwise.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200149 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530150int lmb_is_reserved_flags(phys_addr_t addr, int flags);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200151
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530152/**
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 Protsenkofd042592024-12-10 20:25:50 -0600158 * Return: 0 on success, negative error code on failure.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530159 */
160long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
161
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530162long lmb_free(phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600163
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530164void lmb_dump_all(void);
165void lmb_dump_all_force(void);
166
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530167void lmb_arch_add_memory(void);
168
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530169struct lmb *lmb_get(void);
170int lmb_push(struct lmb *store);
171void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500172
Prasad Kummari940bebc2024-09-13 13:02:52 +0530173static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
174{
Sughosh Ganu9b0765a2025-06-17 16:13:40 +0530175 return lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, len, LMB_NONE);
Prasad Kummari940bebc2024-09-13 13:02:52 +0530176}
177
Janne Grunaue818d3c2024-11-11 07:56:33 +0100178/**
179 * io_lmb_setup() - Initialize LMB struct
Tom Rinid32dddb2024-11-11 07:26:50 -0600180 * @io_lmb: IO LMB to initialize
Janne Grunaue818d3c2024-11-11 07:56:33 +0100181 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600182 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100183 */
184int io_lmb_setup(struct lmb *io_lmb);
185
186/**
187 * io_lmb_teardown() - Tear LMB struct down
Tom Rinid32dddb2024-11-11 07:26:50 -0600188 * @io_lmb: IO LMB to teardown
Janne Grunaue818d3c2024-11-11 07:56:33 +0100189 */
190void 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 Protsenkofd042592024-12-10 20:25:50 -0600200 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100201 */
202long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
203
204/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600205 * io_lmb_alloc() - Allocate specified IO memory address with specified
206 * alignment
Janne Grunaue818d3c2024-11-11 07:56:33 +0100207 * @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 Protsenkofd042592024-12-10 20:25:50 -0600214 * Return: Base IO address on success, 0 on error.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100215 */
216phys_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 Protsenkofd042592024-12-10 20:25:50 -0600224 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100225 */
226long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
227
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600228#endif /* __KERNEL__ */
229
230#endif /* _LINUX_LMB_H */