blob: 03d5fac6aa79116d3978bf3a41e4b7e9a73844c4 [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/**
Sam Protsenkofd042592024-12-10 20:25:50 -060022 * enum lmb_flags - Definition of memory region attributes
23 * @LMB_NONE: No special request
24 * @LMB_NOMAP: Don't add to MMU configuration
25 * @LMB_NOOVERWRITE: The memory region cannot be overwritten/re-reserved
26 * @LMB_NONOTIFY: Do not notify other modules of changes to this memory region
Patrick Delaunaye11c9082021-05-07 14:50:29 +020027 */
28enum lmb_flags {
Sughosh Ganu26d7fa12024-08-26 17:29:17 +053029 LMB_NONE = 0,
30 LMB_NOMAP = BIT(1),
Sughosh Ganu50e27272024-08-26 17:29:19 +053031 LMB_NOOVERWRITE = BIT(2),
Sughosh Ganu7ebbdd72024-10-15 21:07:04 +053032 LMB_NONOTIFY = BIT(3),
Patrick Delaunaye11c9082021-05-07 14:50:29 +020033};
34
35/**
Sam Protsenkofd042592024-12-10 20:25:50 -060036 * struct lmb_region - Description of one region
37 * @base: Base address of the region
38 * @size: Size of the region
39 * @flags: Memory region attributes
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010040 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053041struct lmb_region {
Becky Bruced26d67c2008-06-09 20:37:18 -050042 phys_addr_t base;
43 phys_size_t size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +020044 enum lmb_flags flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060045};
46
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010047/**
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053048 * struct lmb - The LMB structure
Sam Protsenkofd042592024-12-10 20:25:50 -060049 * @free_mem: List of free memory regions
50 * @used_mem: List of used/reserved memory regions
51 * @test: Is structure being used for LMB tests
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010052 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053053struct lmb {
54 struct alist free_mem;
55 struct alist used_mem;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +053056 bool test;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060057};
58
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010059/**
Sam Protsenkofd042592024-12-10 20:25:50 -060060 * lmb_init() - Initialise the LMB module.
61 *
62 * Return: 0 on success, negative error code on failure.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010063 *
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053064 * Initialise the LMB lists needed for keeping the memory map. There
Sam Protsenkofd042592024-12-10 20:25:50 -060065 * are two lists, in form of allocated list data structure. One for the
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053066 * available memory, and one for the used memory. Initialise the two
67 * lists as part of board init. Add memory to the available memory
68 * list and reserve common areas by adding them to the used memory
69 * list.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010070 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053071int lmb_init(void);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060072
Sughosh Ganu65597fa2024-08-26 17:29:23 +053073/**
Sam Protsenkofd042592024-12-10 20:25:50 -060074 * lmb_add_memory() - Add memory range for LMB allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +053075 *
76 * Add the entire available memory range to the pool of memory that
77 * can be used by the LMB module for allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +053078 */
79void lmb_add_memory(void);
80
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053081long lmb_add(phys_addr_t base, phys_size_t size);
Sam Protsenkofd042592024-12-10 20:25:50 -060082
83/**
84 * lmb_reserve() - Reserve a memory region (with no special flags)
85 * @base: Base address of the memory region
86 * @size: Size of the memory region
87 *
88 * Return: 0 on success, negative error code on failure.
89 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053090long lmb_reserve(phys_addr_t base, phys_size_t size);
Sam Protsenkofd042592024-12-10 20:25:50 -060091
Patrick Delaunaye11c9082021-05-07 14:50:29 +020092/**
Sam Protsenkofd042592024-12-10 20:25:50 -060093 * lmb_reserve_flags() - Reserve one region with a specific flags bitfield
94 * @base: Base address of the memory region
95 * @size: Size of the memory region
96 * @flags: Flags for the memory region
Patrick Delaunaye11c9082021-05-07 14:50:29 +020097 *
Sam Protsenkofd042592024-12-10 20:25:50 -060098 * Return:
99 * * %0 - Added successfully, or it's already added (only if LMB_NONE)
100 * * %-EEXIST - The region is already added, and flags != LMB_NONE
101 * * %-1 - Failure
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200102 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530103long lmb_reserve_flags(phys_addr_t base, phys_size_t size,
104 enum lmb_flags flags);
Sam Protsenkofd042592024-12-10 20:25:50 -0600105
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530106phys_addr_t lmb_alloc(phys_size_t size, ulong align);
107phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr);
108phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size);
109phys_size_t lmb_get_free_size(phys_addr_t addr);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200110
Sam Protsenkofd042592024-12-10 20:25:50 -0600111/**
112 * lmb_alloc_base_flags() - Allocate specified memory region with specified
113 * attributes
114 * @size: Size of the region requested
115 * @align: Alignment of the memory region requested
116 * @max_addr: Maximum address of the requested region
117 * @flags: Memory region attributes to be set
118 *
119 * Allocate a region of memory with the attributes specified through the
120 * parameter. The max_addr parameter is used to specify the maximum address
121 * below which the requested region should be allocated.
122 *
123 * Return: Base address on success, 0 on error.
124 */
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530125phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
126 phys_addr_t max_addr, uint flags);
127
128/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600129 * lmb_alloc_addr_flags() - Allocate specified memory address with specified
130 * attributes
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530131 * @base: Base Address requested
132 * @size: Size of the region requested
133 * @flags: Memory region attributes to be set
134 *
135 * Allocate a region of memory with the attributes specified through the
136 * parameter. The base parameter is used to specify the base address
137 * of the requested region.
138 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600139 * Return: Base address on success, 0 on error.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530140 */
141phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
142 uint flags);
143
144/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600145 * lmb_is_reserved_flags() - Test if address is in reserved region with flag
146 * bits set
147 * @addr: Address to be tested
148 * @flags: Bitmap with bits to be tested
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200149 *
150 * The function checks if a reserved region comprising @addr exists which has
151 * all flag bits set which are set in @flags.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200152 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600153 * Return: 1 if matching reservation exists, 0 otherwise.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200154 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530155int lmb_is_reserved_flags(phys_addr_t addr, int flags);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200156
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530157/**
158 * lmb_free_flags() - Free up a region of memory
159 * @base: Base Address of region to be freed
160 * @size: Size of the region to be freed
161 * @flags: Memory region attributes
162 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600163 * Return: 0 on success, negative error code on failure.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530164 */
165long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
166
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530167long lmb_free(phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600168
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530169void lmb_dump_all(void);
170void lmb_dump_all_force(void);
171
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530172void lmb_arch_add_memory(void);
173
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530174struct lmb *lmb_get(void);
175int lmb_push(struct lmb *store);
176void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500177
Prasad Kummari940bebc2024-09-13 13:02:52 +0530178static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
179{
180 return lmb_alloc_addr(addr, len) == addr ? 0 : -1;
181}
182
Janne Grunaue818d3c2024-11-11 07:56:33 +0100183/**
184 * io_lmb_setup() - Initialize LMB struct
Tom Rinid32dddb2024-11-11 07:26:50 -0600185 * @io_lmb: IO LMB to initialize
Janne Grunaue818d3c2024-11-11 07:56:33 +0100186 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600187 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100188 */
189int io_lmb_setup(struct lmb *io_lmb);
190
191/**
192 * io_lmb_teardown() - Tear LMB struct down
Tom Rinid32dddb2024-11-11 07:26:50 -0600193 * @io_lmb: IO LMB to teardown
Janne Grunaue818d3c2024-11-11 07:56:33 +0100194 */
195void io_lmb_teardown(struct lmb *io_lmb);
196
197/**
198 * io_lmb_add() - Add an IOVA range for allocations
199 * @io_lmb: LMB to add the space to
200 * @base: Base Address of region to add
201 * @size: Size of the region to add
202 *
203 * Add the IOVA space [base, base + size] to be managed by io_lmb.
204 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600205 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100206 */
207long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
208
209/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600210 * io_lmb_alloc() - Allocate specified IO memory address with specified
211 * alignment
Janne Grunaue818d3c2024-11-11 07:56:33 +0100212 * @io_lmb: LMB to alloc from
213 * @size: Size of the region requested
214 * @align: Required address and size alignment
215 *
216 * Allocate a region of IO memory. The base parameter is used to specify the
217 * base address of the requested region.
218 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600219 * Return: Base IO address on success, 0 on error.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100220 */
221phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align);
222
223/**
224 * io_lmb_free() - Free up a region of IOVA space
225 * @io_lmb: LMB to return the IO address space to
226 * @base: Base Address of region to be freed
227 * @size: Size of the region to be freed
228 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600229 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100230 */
231long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
232
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600233#endif /* __KERNEL__ */
234
235#endif /* _LINUX_LMB_H */