blob: 09297a4f530a7fc551bf5754e22fe8a9cea98aa3 [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/**
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +010035 * enum lmb_map_op - memory map operation
36 */
37enum lmb_map_op {
38 /** @LMB_MAP_OP_RESERVE: reserve memory */
39 LMB_MAP_OP_RESERVE = 1,
40 /** @LMB_MAP_OP_FREE: free memory */
41 LMB_MAP_OP_FREE,
42 /** @LMB_MAP_OP_ADD: add memory */
43 LMB_MAP_OP_ADD,
44};
45
46/**
Sam Protsenkofd042592024-12-10 20:25:50 -060047 * struct lmb_region - Description of one region
48 * @base: Base address of the region
49 * @size: Size of the region
50 * @flags: Memory region attributes
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010051 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053052struct lmb_region {
Becky Bruced26d67c2008-06-09 20:37:18 -050053 phys_addr_t base;
54 phys_size_t size;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +020055 u32 flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060056};
57
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010058/**
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053059 * struct lmb - The LMB structure
Ilias Apalodimas5421c332024-12-18 09:02:33 +020060 * @available_mem: List of memory available to LMB
Sam Protsenkofd042592024-12-10 20:25:50 -060061 * @used_mem: List of used/reserved memory regions
62 * @test: Is structure being used for LMB tests
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010063 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053064struct lmb {
Ilias Apalodimas5421c332024-12-18 09:02:33 +020065 struct alist available_mem;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053066 struct alist used_mem;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +053067 bool test;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060068};
69
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010070/**
Sam Protsenkofd042592024-12-10 20:25:50 -060071 * lmb_init() - Initialise the LMB module.
72 *
73 * Return: 0 on success, negative error code on failure.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010074 *
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053075 * Initialise the LMB lists needed for keeping the memory map. There
Sam Protsenkofd042592024-12-10 20:25:50 -060076 * are two lists, in form of allocated list data structure. One for the
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053077 * available memory, and one for the used memory. Initialise the two
78 * lists as part of board init. Add memory to the available memory
79 * list and reserve common areas by adding them to the used memory
80 * list.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010081 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053082int lmb_init(void);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060083
Sughosh Ganu65597fa2024-08-26 17:29:23 +053084/**
Sam Protsenkofd042592024-12-10 20:25:50 -060085 * lmb_add_memory() - Add memory range for LMB allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +053086 *
87 * Add the entire available memory range to the pool of memory that
88 * can be used by the LMB module for allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +053089 */
90void lmb_add_memory(void);
91
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053092long lmb_add(phys_addr_t base, phys_size_t size);
Sam Protsenkofd042592024-12-10 20:25:50 -060093
94/**
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +020095 * lmb_reserve() - Reserve one region with a specific flags bitfield
Sam Protsenkofd042592024-12-10 20:25:50 -060096 * @base: Base address of the memory region
97 * @size: Size of the memory region
Sam Protsenkofd042592024-12-10 20:25:50 -060098 * @flags: Flags for the memory region
Patrick Delaunaye11c9082021-05-07 14:50:29 +020099 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600100 * Return:
101 * * %0 - Added successfully, or it's already added (only if LMB_NONE)
102 * * %-EEXIST - The region is already added, and flags != LMB_NONE
103 * * %-1 - Failure
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200104 */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200105long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags);
Sam Protsenkofd042592024-12-10 20:25:50 -0600106
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530107phys_addr_t lmb_alloc(phys_size_t size, ulong align);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530108phys_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/**
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200111 * lmb_alloc_base() - Allocate specified memory region with specified
Sam Protsenkofd042592024-12-10 20:25:50 -0600112 * 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 */
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200124phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr,
125 uint flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530126
127/**
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200128 * lmb_alloc_addr() - Allocate specified memory address with specified attributes
129 *
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 */
Ilias Apalodimas31bf8f12024-12-18 09:02:37 +0200140phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530141
142/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600143 * lmb_is_reserved_flags() - Test if address is in reserved region with flag
144 * bits set
145 * @addr: Address to be tested
146 * @flags: Bitmap with bits to be tested
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200147 *
148 * The function checks if a reserved region comprising @addr exists which has
149 * all flag bits set which are set in @flags.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200150 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600151 * Return: 1 if matching reservation exists, 0 otherwise.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200152 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530153int lmb_is_reserved_flags(phys_addr_t addr, int flags);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200154
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530155/**
156 * lmb_free_flags() - Free up a region of memory
157 * @base: Base Address of region to be freed
158 * @size: Size of the region to be freed
159 * @flags: Memory region attributes
160 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600161 * Return: 0 on success, negative error code on failure.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530162 */
163long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
164
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530165long lmb_free(phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600166
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530167void lmb_dump_all(void);
168void lmb_dump_all_force(void);
169
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530170void lmb_arch_add_memory(void);
171
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530172struct lmb *lmb_get(void);
173int lmb_push(struct lmb *store);
174void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500175
Prasad Kummari940bebc2024-09-13 13:02:52 +0530176static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
177{
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200178 return lmb_alloc_addr(addr, len, LMB_NONE) == addr ? 0 : -1;
Prasad Kummari940bebc2024-09-13 13:02:52 +0530179}
180
Janne Grunaue818d3c2024-11-11 07:56:33 +0100181/**
182 * io_lmb_setup() - Initialize LMB struct
Tom Rinid32dddb2024-11-11 07:26:50 -0600183 * @io_lmb: IO LMB to initialize
Janne Grunaue818d3c2024-11-11 07:56:33 +0100184 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600185 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100186 */
187int io_lmb_setup(struct lmb *io_lmb);
188
189/**
190 * io_lmb_teardown() - Tear LMB struct down
Tom Rinid32dddb2024-11-11 07:26:50 -0600191 * @io_lmb: IO LMB to teardown
Janne Grunaue818d3c2024-11-11 07:56:33 +0100192 */
193void io_lmb_teardown(struct lmb *io_lmb);
194
195/**
196 * io_lmb_add() - Add an IOVA range for allocations
197 * @io_lmb: LMB to add the space to
198 * @base: Base Address of region to add
199 * @size: Size of the region to add
200 *
201 * Add the IOVA space [base, base + size] to be managed by io_lmb.
202 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600203 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100204 */
205long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
206
207/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600208 * io_lmb_alloc() - Allocate specified IO memory address with specified
209 * alignment
Janne Grunaue818d3c2024-11-11 07:56:33 +0100210 * @io_lmb: LMB to alloc from
211 * @size: Size of the region requested
212 * @align: Required address and size alignment
213 *
214 * Allocate a region of IO memory. The base parameter is used to specify the
215 * base address of the requested region.
216 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600217 * Return: Base IO address on success, 0 on error.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100218 */
219phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align);
220
221/**
222 * io_lmb_free() - Free up a region of IOVA space
223 * @io_lmb: LMB to return the IO address space to
224 * @base: Base Address of region to be freed
225 * @size: Size of the region to be freed
226 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600227 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100228 */
229long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
230
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600231#endif /* __KERNEL__ */
232
233#endif /* _LINUX_LMB_H */