blob: e38af036a0d0a02f37fbb9cd1e91247df2d4cd43 [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
Ilias Apalodimas5421c332024-12-18 09:02:33 +020048 * @available_mem: List of memory available to LMB
Sam Protsenkofd042592024-12-10 20:25:50 -060049 * @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 {
Ilias Apalodimas5421c332024-12-18 09:02:33 +020053 struct alist available_mem;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053054 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/**
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +020083 * lmb_reserve() - Reserve one region with a specific flags bitfield
Sam Protsenkofd042592024-12-10 20:25:50 -060084 * @base: Base address of the memory region
85 * @size: Size of the memory region
Sam Protsenkofd042592024-12-10 20:25:50 -060086 * @flags: Flags for the memory region
Patrick Delaunaye11c9082021-05-07 14:50:29 +020087 *
Sam Protsenkofd042592024-12-10 20:25:50 -060088 * Return:
89 * * %0 - Added successfully, or it's already added (only if LMB_NONE)
90 * * %-EEXIST - The region is already added, and flags != LMB_NONE
91 * * %-1 - Failure
Patrick Delaunaye11c9082021-05-07 14:50:29 +020092 */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +020093long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags);
Sam Protsenkofd042592024-12-10 20:25:50 -060094
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053095phys_addr_t lmb_alloc(phys_size_t size, ulong align);
96phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053097phys_size_t lmb_get_free_size(phys_addr_t addr);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +020098
Sam Protsenkofd042592024-12-10 20:25:50 -060099/**
100 * lmb_alloc_base_flags() - Allocate specified memory region with specified
101 * attributes
102 * @size: Size of the region requested
103 * @align: Alignment of the memory region requested
104 * @max_addr: Maximum address of the requested region
105 * @flags: Memory region attributes to be set
106 *
107 * Allocate a region of memory with the attributes specified through the
108 * parameter. The max_addr parameter is used to specify the maximum address
109 * below which the requested region should be allocated.
110 *
111 * Return: Base address on success, 0 on error.
112 */
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530113phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
114 phys_addr_t max_addr, uint flags);
115
116/**
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200117 * lmb_alloc_addr() - Allocate specified memory address with specified attributes
118 *
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530119 * @base: Base Address requested
120 * @size: Size of the region requested
121 * @flags: Memory region attributes to be set
122 *
123 * Allocate a region of memory with the attributes specified through the
124 * parameter. The base parameter is used to specify the base address
125 * of the requested region.
126 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600127 * Return: Base address on success, 0 on error.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530128 */
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200129phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, uint flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530130
131/**
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/**
145 * lmb_free_flags() - Free up a region of memory
146 * @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 */
152long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
153
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530154long lmb_free(phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600155
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530156void lmb_dump_all(void);
157void lmb_dump_all_force(void);
158
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530159void lmb_arch_add_memory(void);
160
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530161struct lmb *lmb_get(void);
162int lmb_push(struct lmb *store);
163void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500164
Prasad Kummari940bebc2024-09-13 13:02:52 +0530165static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
166{
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200167 return lmb_alloc_addr(addr, len, LMB_NONE) == addr ? 0 : -1;
Prasad Kummari940bebc2024-09-13 13:02:52 +0530168}
169
Janne Grunaue818d3c2024-11-11 07:56:33 +0100170/**
171 * io_lmb_setup() - Initialize LMB struct
Tom Rinid32dddb2024-11-11 07:26:50 -0600172 * @io_lmb: IO LMB to initialize
Janne Grunaue818d3c2024-11-11 07:56:33 +0100173 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600174 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100175 */
176int io_lmb_setup(struct lmb *io_lmb);
177
178/**
179 * io_lmb_teardown() - Tear LMB struct down
Tom Rinid32dddb2024-11-11 07:26:50 -0600180 * @io_lmb: IO LMB to teardown
Janne Grunaue818d3c2024-11-11 07:56:33 +0100181 */
182void io_lmb_teardown(struct lmb *io_lmb);
183
184/**
185 * io_lmb_add() - Add an IOVA range for allocations
186 * @io_lmb: LMB to add the space to
187 * @base: Base Address of region to add
188 * @size: Size of the region to add
189 *
190 * Add the IOVA space [base, base + size] to be managed by io_lmb.
191 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600192 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100193 */
194long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
195
196/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600197 * io_lmb_alloc() - Allocate specified IO memory address with specified
198 * alignment
Janne Grunaue818d3c2024-11-11 07:56:33 +0100199 * @io_lmb: LMB to alloc from
200 * @size: Size of the region requested
201 * @align: Required address and size alignment
202 *
203 * Allocate a region of IO memory. The base parameter is used to specify the
204 * base address of the requested region.
205 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600206 * Return: Base IO address on success, 0 on error.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100207 */
208phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align);
209
210/**
211 * io_lmb_free() - Free up a region of IOVA space
212 * @io_lmb: LMB to return the IO address space to
213 * @base: Base Address of region to be freed
214 * @size: Size of the region to be freed
215 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600216 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100217 */
218long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
219
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600220#endif /* __KERNEL__ */
221
222#endif /* _LINUX_LMB_H */