blob: 3abe24deb56e2eccf4d87b7f6de2d6cf08dc4736 [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/**
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);
97phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size);
98phys_size_t lmb_get_free_size(phys_addr_t addr);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +020099
Sam Protsenkofd042592024-12-10 20:25:50 -0600100/**
101 * lmb_alloc_base_flags() - Allocate specified memory region with specified
102 * attributes
103 * @size: Size of the region requested
104 * @align: Alignment of the memory region requested
105 * @max_addr: Maximum address of the requested region
106 * @flags: Memory region attributes to be set
107 *
108 * Allocate a region of memory with the attributes specified through the
109 * parameter. The max_addr parameter is used to specify the maximum address
110 * below which the requested region should be allocated.
111 *
112 * Return: Base address on success, 0 on error.
113 */
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530114phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
115 phys_addr_t max_addr, uint flags);
116
117/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600118 * lmb_alloc_addr_flags() - Allocate specified memory address with specified
119 * attributes
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530120 * @base: Base Address requested
121 * @size: Size of the region requested
122 * @flags: Memory region attributes to be set
123 *
124 * Allocate a region of memory with the attributes specified through the
125 * parameter. The base parameter is used to specify the base address
126 * of the requested region.
127 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600128 * Return: Base address on success, 0 on error.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530129 */
130phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
131 uint flags);
132
133/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600134 * lmb_is_reserved_flags() - Test if address is in reserved region with flag
135 * bits set
136 * @addr: Address to be tested
137 * @flags: Bitmap with bits to be tested
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200138 *
139 * The function checks if a reserved region comprising @addr exists which has
140 * all flag bits set which are set in @flags.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200141 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600142 * Return: 1 if matching reservation exists, 0 otherwise.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200143 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530144int lmb_is_reserved_flags(phys_addr_t addr, int flags);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200145
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530146/**
147 * lmb_free_flags() - Free up a region of memory
148 * @base: Base Address of region to be freed
149 * @size: Size of the region to be freed
150 * @flags: Memory region attributes
151 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600152 * Return: 0 on success, negative error code on failure.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530153 */
154long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
155
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530156long lmb_free(phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600157
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530158void lmb_dump_all(void);
159void lmb_dump_all_force(void);
160
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530161void lmb_arch_add_memory(void);
162
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530163struct lmb *lmb_get(void);
164int lmb_push(struct lmb *store);
165void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500166
Prasad Kummari940bebc2024-09-13 13:02:52 +0530167static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
168{
169 return lmb_alloc_addr(addr, len) == addr ? 0 : -1;
170}
171
Janne Grunaue818d3c2024-11-11 07:56:33 +0100172/**
173 * io_lmb_setup() - Initialize LMB struct
Tom Rinid32dddb2024-11-11 07:26:50 -0600174 * @io_lmb: IO LMB to initialize
Janne Grunaue818d3c2024-11-11 07:56:33 +0100175 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600176 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100177 */
178int io_lmb_setup(struct lmb *io_lmb);
179
180/**
181 * io_lmb_teardown() - Tear LMB struct down
Tom Rinid32dddb2024-11-11 07:26:50 -0600182 * @io_lmb: IO LMB to teardown
Janne Grunaue818d3c2024-11-11 07:56:33 +0100183 */
184void io_lmb_teardown(struct lmb *io_lmb);
185
186/**
187 * io_lmb_add() - Add an IOVA range for allocations
188 * @io_lmb: LMB to add the space to
189 * @base: Base Address of region to add
190 * @size: Size of the region to add
191 *
192 * Add the IOVA space [base, base + size] to be managed by io_lmb.
193 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600194 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100195 */
196long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
197
198/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600199 * io_lmb_alloc() - Allocate specified IO memory address with specified
200 * alignment
Janne Grunaue818d3c2024-11-11 07:56:33 +0100201 * @io_lmb: LMB to alloc from
202 * @size: Size of the region requested
203 * @align: Required address and size alignment
204 *
205 * Allocate a region of IO memory. The base parameter is used to specify the
206 * base address of the requested region.
207 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600208 * Return: Base IO address on success, 0 on error.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100209 */
210phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align);
211
212/**
213 * io_lmb_free() - Free up a region of IOVA space
214 * @io_lmb: LMB to return the IO address space to
215 * @base: Base Address of region to be freed
216 * @size: Size of the region to be freed
217 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600218 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100219 */
220long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
221
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600222#endif /* __KERNEL__ */
223
224#endif /* _LINUX_LMB_H */