blob: 3b911d5d8391d07ad2c2c0a78a155e0d0ffe0ba9 [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
30#define LMB_NOMAP BIT(0)
31#define LMB_NOOVERWRITE BIT(1)
32#define LMB_NONOTIFY BIT(2)
Patrick Delaunaye11c9082021-05-07 14:50:29 +020033
34/**
Sam Protsenkofd042592024-12-10 20:25:50 -060035 * 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 Delaunay71cc9c52021-03-10 10:16:31 +010039 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053040struct lmb_region {
Becky Bruced26d67c2008-06-09 20:37:18 -050041 phys_addr_t base;
42 phys_size_t size;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +020043 u32 flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060044};
45
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010046/**
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053047 * struct lmb - The LMB structure
Sam Protsenkofd042592024-12-10 20:25:50 -060048 * @free_mem: List of free memory regions
49 * @used_mem: List of used/reserved memory regions
50 * @test: Is structure being used for LMB tests
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010051 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053052struct lmb {
53 struct alist free_mem;
54 struct alist used_mem;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +053055 bool test;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060056};
57
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010058/**
Sam Protsenkofd042592024-12-10 20:25:50 -060059 * lmb_init() - Initialise the LMB module.
60 *
61 * Return: 0 on success, negative error code on failure.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010062 *
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053063 * Initialise the LMB lists needed for keeping the memory map. There
Sam Protsenkofd042592024-12-10 20:25:50 -060064 * are two lists, in form of allocated list data structure. One for the
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053065 * 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 Delaunay71cc9c52021-03-10 10:16:31 +010069 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053070int lmb_init(void);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060071
Sughosh Ganu65597fa2024-08-26 17:29:23 +053072/**
Sam Protsenkofd042592024-12-10 20:25:50 -060073 * lmb_add_memory() - Add memory range for LMB allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +053074 *
75 * Add the entire available memory range to the pool of memory that
76 * can be used by the LMB module for allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +053077 */
78void lmb_add_memory(void);
79
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053080long lmb_add(phys_addr_t base, phys_size_t size);
Sam Protsenkofd042592024-12-10 20:25:50 -060081
82/**
83 * lmb_reserve() - Reserve a memory region (with no special flags)
84 * @base: Base address of the memory region
85 * @size: Size of the memory region
86 *
87 * Return: 0 on success, negative error code on failure.
88 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053089long lmb_reserve(phys_addr_t base, phys_size_t size);
Sam Protsenkofd042592024-12-10 20:25:50 -060090
Patrick Delaunaye11c9082021-05-07 14:50:29 +020091/**
Sam Protsenkofd042592024-12-10 20:25:50 -060092 * lmb_reserve_flags() - Reserve one region with a specific flags bitfield
93 * @base: Base address of the memory region
94 * @size: Size of the memory region
95 * @flags: Flags for the memory region
Patrick Delaunaye11c9082021-05-07 14:50:29 +020096 *
Sam Protsenkofd042592024-12-10 20:25:50 -060097 * Return:
98 * * %0 - Added successfully, or it's already added (only if LMB_NONE)
99 * * %-EEXIST - The region is already added, and flags != LMB_NONE
100 * * %-1 - Failure
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200101 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530102long lmb_reserve_flags(phys_addr_t base, phys_size_t size,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200103 u32 flags);
Sam Protsenkofd042592024-12-10 20:25:50 -0600104
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530105phys_addr_t lmb_alloc(phys_size_t size, ulong align);
106phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr);
107phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size);
108phys_size_t lmb_get_free_size(phys_addr_t addr);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200109
Sam Protsenkofd042592024-12-10 20:25:50 -0600110/**
111 * lmb_alloc_base_flags() - Allocate specified memory region with specified
112 * 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 */
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530124phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
125 phys_addr_t max_addr, uint flags);
126
127/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600128 * lmb_alloc_addr_flags() - Allocate specified memory address with specified
129 * attributes
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530130 * @base: Base Address requested
131 * @size: Size of the region requested
132 * @flags: Memory region attributes to be set
133 *
134 * Allocate a region of memory with the attributes specified through the
135 * parameter. The base parameter is used to specify the base address
136 * of the requested region.
137 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600138 * Return: Base address on success, 0 on error.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530139 */
140phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
141 uint flags);
142
143/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600144 * lmb_is_reserved_flags() - Test if address is in reserved region with flag
145 * bits set
146 * @addr: Address to be tested
147 * @flags: Bitmap with bits to be tested
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200148 *
149 * The function checks if a reserved region comprising @addr exists which has
150 * all flag bits set which are set in @flags.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200151 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600152 * Return: 1 if matching reservation exists, 0 otherwise.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200153 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530154int lmb_is_reserved_flags(phys_addr_t addr, int flags);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200155
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530156/**
157 * lmb_free_flags() - Free up a region of memory
158 * @base: Base Address of region to be freed
159 * @size: Size of the region to be freed
160 * @flags: Memory region attributes
161 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600162 * Return: 0 on success, negative error code on failure.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530163 */
164long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
165
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530166long lmb_free(phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600167
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530168void lmb_dump_all(void);
169void lmb_dump_all_force(void);
170
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530171void lmb_arch_add_memory(void);
172
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530173struct lmb *lmb_get(void);
174int lmb_push(struct lmb *store);
175void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500176
Prasad Kummari940bebc2024-09-13 13:02:52 +0530177static 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
Janne Grunaue818d3c2024-11-11 07:56:33 +0100182/**
183 * io_lmb_setup() - Initialize LMB struct
Tom Rinid32dddb2024-11-11 07:26:50 -0600184 * @io_lmb: IO LMB to initialize
Janne Grunaue818d3c2024-11-11 07:56:33 +0100185 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600186 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100187 */
188int io_lmb_setup(struct lmb *io_lmb);
189
190/**
191 * io_lmb_teardown() - Tear LMB struct down
Tom Rinid32dddb2024-11-11 07:26:50 -0600192 * @io_lmb: IO LMB to teardown
Janne Grunaue818d3c2024-11-11 07:56:33 +0100193 */
194void io_lmb_teardown(struct lmb *io_lmb);
195
196/**
197 * io_lmb_add() - Add an IOVA range for allocations
198 * @io_lmb: LMB to add the space to
199 * @base: Base Address of region to add
200 * @size: Size of the region to add
201 *
202 * Add the IOVA space [base, base + size] to be managed by io_lmb.
203 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600204 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100205 */
206long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
207
208/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600209 * io_lmb_alloc() - Allocate specified IO memory address with specified
210 * alignment
Janne Grunaue818d3c2024-11-11 07:56:33 +0100211 * @io_lmb: LMB to alloc from
212 * @size: Size of the region requested
213 * @align: Required address and size alignment
214 *
215 * Allocate a region of IO memory. The base parameter is used to specify the
216 * base address of the requested region.
217 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600218 * Return: Base IO address on success, 0 on error.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100219 */
220phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align);
221
222/**
223 * io_lmb_free() - Free up a region of IOVA space
224 * @io_lmb: LMB to return the IO address space to
225 * @base: Base Address of region to be freed
226 * @size: Size of the region to be freed
227 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600228 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100229 */
230long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
231
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600232#endif /* __KERNEL__ */
233
234#endif /* _LINUX_LMB_H */