blob: d9d7435a431e39b2b0a456fa3baaae6f0ac23b3b [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);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053096phys_size_t lmb_get_free_size(phys_addr_t addr);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +020097
Sam Protsenkofd042592024-12-10 20:25:50 -060098/**
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +020099 * lmb_alloc_base() - Allocate specified memory region with specified
Sam Protsenkofd042592024-12-10 20:25:50 -0600100 * attributes
101 * @size: Size of the region requested
102 * @align: Alignment of the memory region requested
103 * @max_addr: Maximum address of the requested region
104 * @flags: Memory region attributes to be set
105 *
106 * Allocate a region of memory with the attributes specified through the
107 * parameter. The max_addr parameter is used to specify the maximum address
108 * below which the requested region should be allocated.
109 *
110 * Return: Base address on success, 0 on error.
111 */
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200112phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr,
113 uint flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530114
115/**
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200116 * lmb_alloc_addr() - Allocate specified memory address with specified attributes
117 *
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530118 * @base: Base Address requested
119 * @size: Size of the region requested
120 * @flags: Memory region attributes to be set
121 *
122 * Allocate a region of memory with the attributes specified through the
123 * parameter. The base parameter is used to specify the base address
124 * of the requested region.
125 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600126 * Return: Base address on success, 0 on error.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530127 */
Ilias Apalodimas31bf8f12024-12-18 09:02:37 +0200128phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530129
130/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600131 * lmb_is_reserved_flags() - Test if address is in reserved region with flag
132 * bits set
133 * @addr: Address to be tested
134 * @flags: Bitmap with bits to be tested
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200135 *
136 * The function checks if a reserved region comprising @addr exists which has
137 * all flag bits set which are set in @flags.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200138 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600139 * Return: 1 if matching reservation exists, 0 otherwise.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200140 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530141int lmb_is_reserved_flags(phys_addr_t addr, int flags);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200142
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530143/**
144 * lmb_free_flags() - Free up a region of memory
145 * @base: Base Address of region to be freed
146 * @size: Size of the region to be freed
147 * @flags: Memory region attributes
148 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600149 * Return: 0 on success, negative error code on failure.
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530150 */
151long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
152
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530153long lmb_free(phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600154
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530155void lmb_dump_all(void);
156void lmb_dump_all_force(void);
157
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530158void lmb_arch_add_memory(void);
159
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530160struct lmb *lmb_get(void);
161int lmb_push(struct lmb *store);
162void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500163
Prasad Kummari940bebc2024-09-13 13:02:52 +0530164static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
165{
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200166 return lmb_alloc_addr(addr, len, LMB_NONE) == addr ? 0 : -1;
Prasad Kummari940bebc2024-09-13 13:02:52 +0530167}
168
Janne Grunaue818d3c2024-11-11 07:56:33 +0100169/**
170 * io_lmb_setup() - Initialize LMB struct
Tom Rinid32dddb2024-11-11 07:26:50 -0600171 * @io_lmb: IO LMB to initialize
Janne Grunaue818d3c2024-11-11 07:56:33 +0100172 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600173 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100174 */
175int io_lmb_setup(struct lmb *io_lmb);
176
177/**
178 * io_lmb_teardown() - Tear LMB struct down
Tom Rinid32dddb2024-11-11 07:26:50 -0600179 * @io_lmb: IO LMB to teardown
Janne Grunaue818d3c2024-11-11 07:56:33 +0100180 */
181void io_lmb_teardown(struct lmb *io_lmb);
182
183/**
184 * io_lmb_add() - Add an IOVA range for allocations
185 * @io_lmb: LMB to add the space to
186 * @base: Base Address of region to add
187 * @size: Size of the region to add
188 *
189 * Add the IOVA space [base, base + size] to be managed by io_lmb.
190 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600191 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100192 */
193long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
194
195/**
Sam Protsenkofd042592024-12-10 20:25:50 -0600196 * io_lmb_alloc() - Allocate specified IO memory address with specified
197 * alignment
Janne Grunaue818d3c2024-11-11 07:56:33 +0100198 * @io_lmb: LMB to alloc from
199 * @size: Size of the region requested
200 * @align: Required address and size alignment
201 *
202 * Allocate a region of IO memory. The base parameter is used to specify the
203 * base address of the requested region.
204 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600205 * Return: Base IO address on success, 0 on error.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100206 */
207phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align);
208
209/**
210 * io_lmb_free() - Free up a region of IOVA space
211 * @io_lmb: LMB to return the IO address space to
212 * @base: Base Address of region to be freed
213 * @size: Size of the region to be freed
214 *
Sam Protsenkofd042592024-12-10 20:25:50 -0600215 * Return: 0 on success, negative error code on failure.
Janne Grunaue818d3c2024-11-11 07:56:33 +0100216 */
217long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size);
218
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600219#endif /* __KERNEL__ */
220
221#endif /* _LINUX_LMB_H */