blob: 8906b42181f84e70a07051e8b7d69ffce78099f1 [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
Sughosh Ganu61d7d9e2025-04-23 17:01:23 +053030#define LMB_NOMAP BIT(1)
31#define LMB_NOOVERWRITE BIT(2)
32#define LMB_NONOTIFY BIT(3)
Patrick Delaunaye11c9082021-05-07 14:50:29 +020033
34/**
Sughosh Ganu9b0765a2025-06-17 16:13:40 +053035 * enum lmb_mem_type - type of memory allocation request
36 * @LMB_MEM_ALLOC_ADDR: request for a particular region of memory
37 */
38enum lmb_mem_type {
39 LMB_MEM_ALLOC_ADDR = 1,
40};
41
42/**
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +010043 * enum lmb_map_op - memory map operation
44 */
45enum lmb_map_op {
46 /** @LMB_MAP_OP_RESERVE: reserve memory */
47 LMB_MAP_OP_RESERVE = 1,
48 /** @LMB_MAP_OP_FREE: free memory */
49 LMB_MAP_OP_FREE,
50 /** @LMB_MAP_OP_ADD: add memory */
51 LMB_MAP_OP_ADD,
52};
53
54/**
Sam Protsenkofd042592024-12-10 20:25:50 -060055 * struct lmb_region - Description of one region
56 * @base: Base address of the region
57 * @size: Size of the region
58 * @flags: Memory region attributes
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010059 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053060struct lmb_region {
Becky Bruced26d67c2008-06-09 20:37:18 -050061 phys_addr_t base;
62 phys_size_t size;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +020063 u32 flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060064};
65
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010066/**
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053067 * struct lmb - The LMB structure
Ilias Apalodimas5421c332024-12-18 09:02:33 +020068 * @available_mem: List of memory available to LMB
Sam Protsenkofd042592024-12-10 20:25:50 -060069 * @used_mem: List of used/reserved memory regions
70 * @test: Is structure being used for LMB tests
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010071 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053072struct lmb {
Ilias Apalodimas5421c332024-12-18 09:02:33 +020073 struct alist available_mem;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053074 struct alist used_mem;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +053075 bool test;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060076};
77
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010078/**
Sughosh Ganu9b0765a2025-06-17 16:13:40 +053079 * lmb_alloc_mem() - Request LMB memory
80 * @type: Type of memory allocation request
81 * @align: Alignment of the memory region requested(0 for none)
82 * @addr: Base address of the allocated memory region
83 * @size: Size in bytes of the allocation request
84 * @flags: Memory region attributes to be set
85 *
86 * Allocate a region of memory where the allocation is based on the parameters
87 * that have been passed to the function.The first parameter specifies the
88 * type of allocation that is being requested. The second parameter, @align
89 * is used to specify if the allocation is to be made with a particular
90 * alignment. Use 0 for no alignment requirements.
91 *
92 * The allocated address is returned through the @addr parameter when @type
93 * is @LMB_MEM_ALLOC_ANY or @LMB_MEM_ALLOC_MAX. If @type is
94 * @LMB_MEM_ALLOC_ADDR the @addr parameter would contain the address being
95 * requested.
96 *
97 * The flags parameter is used to specify the memory attributes of the
98 * requested region.
99 *
100 * Return: 0 on success, -ve value on failure
101 *
102 * When the allocation is of type @LMB_MEM_ALLOC_ADDR, the return value can
103 * be -EINVAL if the requested memory region is not part of the LMB memory
104 * map, and -EEXIST if the requested region is already allocated.
105 */
106int lmb_alloc_mem(enum lmb_mem_type type, u64 align, phys_addr_t *addr,
107 phys_size_t size, u32 flags);
108
109/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600110 * lmb_init() - Initialise the LMB module.
111 *
112 * Return: 0 on success, negative error code on failure.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100113 *
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530114 * Initialise the LMB lists needed for keeping the memory map. There
Sam Protsenkofd042592024-12-10 20:25:50 -0600115 * are two lists, in form of allocated list data structure. One for the
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530116 * available memory, and one for the used memory. Initialise the two
117 * lists as part of board init. Add memory to the available memory
118 * list and reserve common areas by adding them to the used memory
119 * list.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100120 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530121int lmb_init(void);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600122
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530123/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600124 * lmb_add_memory() - Add memory range for LMB allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530125 *
126 * Add the entire available memory range to the pool of memory that
127 * can be used by the LMB module for allocations.
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530128 */
129void lmb_add_memory(void);
130
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530131long lmb_add(phys_addr_t base, phys_size_t size);
Sam Protsenkofd042592024-12-10 20:25:50 -0600132
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530133phys_addr_t lmb_alloc(phys_size_t size, ulong align);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530134phys_size_t lmb_get_free_size(phys_addr_t addr);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200135
Sam Protsenkofd042592024-12-10 20:25:50 -0600136/**
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200137 * lmb_alloc_base() - Allocate specified memory region with specified
Sam Protsenkofd042592024-12-10 20:25:50 -0600138 * attributes
139 * @size: Size of the region requested
140 * @align: Alignment of the memory region requested
141 * @max_addr: Maximum address of the requested region
142 * @flags: Memory region attributes to be set
143 *
144 * Allocate a region of memory with the attributes specified through the
145 * parameter. The max_addr parameter is used to specify the maximum address
146 * below which the requested region should be allocated.
147 *
148 * Return: Base address on success, 0 on error.
149 */
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200150phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr,
151 uint flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530152
153/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600154 * lmb_is_reserved_flags() - Test if address is in reserved region with flag
155 * bits set
156 * @addr: Address to be tested
157 * @flags: Bitmap with bits to be tested
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200158 *
159 * The function checks if a reserved region comprising @addr exists which has
160 * all flag bits set which are set in @flags.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200161 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600162 * Return: 1 if matching reservation exists, 0 otherwise.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200163 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530164int lmb_is_reserved_flags(phys_addr_t addr, int flags);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200165
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530166/**
167 * lmb_free_flags() - Free up a region of memory
168 * @base: Base Address of region to be freed
169 * @size: Size of the region to be freed
170 * @flags: Memory region attributes
171 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600172 * Return: 0 on success, negative error code on failure.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530173 */
174long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
175
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530176long lmb_free(phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600177
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530178void lmb_dump_all(void);
179void lmb_dump_all_force(void);
180
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530181void lmb_arch_add_memory(void);
182
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530183struct lmb *lmb_get(void);
184int lmb_push(struct lmb *store);
185void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500186
Prasad Kummari940bebc2024-09-13 13:02:52 +0530187static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
188{
Sughosh Ganu9b0765a2025-06-17 16:13:40 +0530189 return lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, len, LMB_NONE);
Prasad Kummari940bebc2024-09-13 13:02:52 +0530190}
191
Janne Grunaue818d3c2024-11-11 07:56:33 +0100192/**
193 * io_lmb_setup() - Initialize LMB struct
Tom Rinid32dddb2024-11-11 07:26:50 -0600194 * @io_lmb: IO LMB to initialize
Janne Grunaue818d3c2024-11-11 07:56:33 +0100195 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600196 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100197 */
198int io_lmb_setup(struct lmb *io_lmb);
199
200/**
201 * io_lmb_teardown() - Tear LMB struct down
Tom Rinid32dddb2024-11-11 07:26:50 -0600202 * @io_lmb: IO LMB to teardown
Janne Grunaue818d3c2024-11-11 07:56:33 +0100203 */
204void io_lmb_teardown(struct lmb *io_lmb);
205
206/**
207 * io_lmb_add() - Add an IOVA range for allocations
208 * @io_lmb: LMB to add the space to
209 * @base: Base Address of region to add
210 * @size: Size of the region to add
211 *
212 * Add the IOVA space [base, base + size] to be managed by io_lmb.
213 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600214 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100215 */
216long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
217
218/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600219 * io_lmb_alloc() - Allocate specified IO memory address with specified
220 * alignment
Janne Grunaue818d3c2024-11-11 07:56:33 +0100221 * @io_lmb: LMB to alloc from
222 * @size: Size of the region requested
223 * @align: Required address and size alignment
224 *
225 * Allocate a region of IO memory. The base parameter is used to specify the
226 * base address of the requested region.
227 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600228 * Return: Base IO address on success, 0 on error.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100229 */
230phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align);
231
232/**
233 * io_lmb_free() - Free up a region of IOVA space
234 * @io_lmb: LMB to return the IO address space to
235 * @base: Base Address of region to be freed
236 * @size: Size of the region to be freed
237 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600238 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100239 */
240long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
241
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600242#endif /* __KERNEL__ */
243
244#endif /* _LINUX_LMB_H */