blob: 5d5f037ccb9530958b119372bcf911b185465f67 [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 Ganu291bf9c2024-08-26 17:29:18 +0530127long lmb_add(phys_addr_t base, phys_size_t size);
Sam Protsenkofd042592024-12-10 20:25:50 -0600128
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530129phys_size_t lmb_get_free_size(phys_addr_t addr);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200130
Sam Protsenkofd042592024-12-10 20:25:50 -0600131/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600132 * 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
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200136 *
137 * The function checks if a reserved region comprising @addr exists which has
138 * all flag bits set which are set in @flags.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200139 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600140 * Return: 1 if matching reservation exists, 0 otherwise.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200141 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530142int lmb_is_reserved_flags(phys_addr_t addr, int flags);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200143
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530144/**
Sughosh Ganud80105b2025-06-17 16:13:43 +0530145 * lmb_free() - Free up a region of memory
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530146 * @base: Base Address of region to be freed
147 * @size: Size of the region to be freed
148 * @flags: Memory region attributes
149 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600150 * Return: 0 on success, negative error code on failure.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530151 */
Sughosh Ganud80105b2025-06-17 16:13:43 +0530152long lmb_free(phys_addr_t base, phys_size_t size, u32 flags);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600153
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530154void lmb_dump_all(void);
155void lmb_dump_all_force(void);
156
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530157void lmb_arch_add_memory(void);
158
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530159struct lmb *lmb_get(void);
160int lmb_push(struct lmb *store);
161void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500162
Prasad Kummari940bebc2024-09-13 13:02:52 +0530163static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
164{
Sughosh Ganu9b0765a2025-06-17 16:13:40 +0530165 return lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, len, LMB_NONE);
Prasad Kummari940bebc2024-09-13 13:02:52 +0530166}
167
Janne Grunaue818d3c2024-11-11 07:56:33 +0100168/**
169 * io_lmb_setup() - Initialize LMB struct
Tom Rinid32dddb2024-11-11 07:26:50 -0600170 * @io_lmb: IO LMB to initialize
Janne Grunaue818d3c2024-11-11 07:56:33 +0100171 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600172 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100173 */
174int io_lmb_setup(struct lmb *io_lmb);
175
176/**
177 * io_lmb_teardown() - Tear LMB struct down
Tom Rinid32dddb2024-11-11 07:26:50 -0600178 * @io_lmb: IO LMB to teardown
Janne Grunaue818d3c2024-11-11 07:56:33 +0100179 */
180void 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 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600190 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100191 */
192long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
193
194/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600195 * io_lmb_alloc() - Allocate specified IO memory address with specified
196 * alignment
Janne Grunaue818d3c2024-11-11 07:56:33 +0100197 * @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 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600204 * Return: Base IO address on success, 0 on error.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100205 */
206phys_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 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600214 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100215 */
216long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
217
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600218#endif /* __KERNEL__ */
219
220#endif /* _LINUX_LMB_H */