blob: ec477c1f51d2217002527c54f020bccda6b7f519 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Kumar Gala6d7bfa82008-02-27 21:51:47 -06002#ifndef _LINUX_LMB_H
3#define _LINUX_LMB_H
4#ifdef __KERNEL__
5
Sughosh Ganu291bf9c2024-08-26 17:29:18 +05306#include <alist.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -06007#include <asm/types.h>
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +01008#include <asm/u-boot.h>
Sughosh Ganu26d7fa12024-08-26 17:29:17 +05309#include <linux/bitops.h>
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +010010
Kumar Gala6d7bfa82008-02-27 21:51:47 -060011/*
12 * Logical memory blocks.
13 *
14 * Copyright (C) 2001 Peter Bergner, IBM Corp.
Kumar Gala6d7bfa82008-02-27 21:51:47 -060015 */
16
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010017/**
Patrick Delaunaye11c9082021-05-07 14:50:29 +020018 * enum lmb_flags - definition of memory region attributes
19 * @LMB_NONE: no special request
20 * @LMB_NOMAP: don't add to mmu configuration
Sughosh Ganu7ebbdd72024-10-15 21:07:04 +053021 * @LMB_NOOVERWRITE: the memory region cannot be overwritten/re-reserved
22 * @LMB_NONOTIFY: do not notify other modules of changes to this memory region
Patrick Delaunaye11c9082021-05-07 14:50:29 +020023 */
24enum lmb_flags {
Sughosh Ganu26d7fa12024-08-26 17:29:17 +053025 LMB_NONE = 0,
26 LMB_NOMAP = BIT(1),
Sughosh Ganu50e27272024-08-26 17:29:19 +053027 LMB_NOOVERWRITE = BIT(2),
Sughosh Ganu7ebbdd72024-10-15 21:07:04 +053028 LMB_NONOTIFY = BIT(3),
Patrick Delaunaye11c9082021-05-07 14:50:29 +020029};
30
31/**
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053032 * struct lmb_region - Description of one region.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010033 *
Heinrich Schuchardte6923f02021-11-14 08:43:07 +010034 * @base: Base address of the region.
35 * @size: Size of the region
36 * @flags: memory region attributes
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010037 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053038struct lmb_region {
Becky Bruced26d67c2008-06-09 20:37:18 -050039 phys_addr_t base;
40 phys_size_t size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +020041 enum lmb_flags flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060042};
43
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010044/**
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053045 * struct lmb - The LMB structure
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010046 *
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053047 * @free_mem: List of free memory regions
48 * @used_mem: List of used/reserved memory regions
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010049 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053050struct lmb {
51 struct alist free_mem;
52 struct alist used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060053};
54
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010055/**
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053056 * lmb_init() - Initialise the LMB module
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010057 *
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053058 * Initialise the LMB lists needed for keeping the memory map. There
59 * are two lists, in form of alloced list data structure. One for the
60 * available memory, and one for the used memory. Initialise the two
61 * lists as part of board init. Add memory to the available memory
62 * list and reserve common areas by adding them to the used memory
63 * list.
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010064 *
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053065 * Return: 0 on success, -ve on error
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010066 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053067int lmb_init(void);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060068
Sughosh Ganu65597fa2024-08-26 17:29:23 +053069/**
70 * lmb_add_memory() - Add memory range for LMB allocations
71 *
72 * Add the entire available memory range to the pool of memory that
73 * can be used by the LMB module for allocations.
74 *
75 * Return: None
76 */
77void lmb_add_memory(void);
78
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053079long lmb_add(phys_addr_t base, phys_size_t size);
80long lmb_reserve(phys_addr_t base, phys_size_t size);
Patrick Delaunaye11c9082021-05-07 14:50:29 +020081/**
82 * lmb_reserve_flags - Reserve one region with a specific flags bitfield.
83 *
Heinrich Schuchardte6923f02021-11-14 08:43:07 +010084 * @base: base address of the memory region
85 * @size: size of the memory region
86 * @flags: flags for the memory region
87 * Return: 0 if OK, > 0 for coalesced region or a negative error code.
Patrick Delaunaye11c9082021-05-07 14:50:29 +020088 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053089long lmb_reserve_flags(phys_addr_t base, phys_size_t size,
90 enum lmb_flags flags);
91phys_addr_t lmb_alloc(phys_size_t size, ulong align);
92phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr);
93phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size);
94phys_size_t lmb_get_free_size(phys_addr_t addr);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +020095
96/**
Sughosh Ganu7f15d322024-10-15 21:07:03 +053097 * lmb_alloc_flags() - Allocate memory region with specified attributes
98 * @size: Size of the region requested
99 * @align: Alignment of the memory region requested
100 * @flags: Memory region attributes to be set
101 *
102 * Allocate a region of memory with the attributes specified through the
103 * parameter.
104 *
105 * Return: base address on success, 0 on error
106 */
107phys_addr_t lmb_alloc_flags(phys_size_t size, ulong align, uint flags);
108
109/**
110 * lmb_alloc_base_flags() - Allocate specified memory region with specified attributes
111 * @size: Size of the region requested
112 * @align: Alignment of the memory region requested
113 * @max_addr: Maximum address of the requested region
114 * @flags: Memory region attributes to be set
115 *
116 * Allocate a region of memory with the attributes specified through the
117 * parameter. The max_addr parameter is used to specify the maximum address
118 * below which the requested region should be allocated.
119 *
120 * Return: base address on success, 0 on error
121 */
122phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
123 phys_addr_t max_addr, uint flags);
124
125/**
126 * lmb_alloc_addr_flags() - Allocate specified memory address with specified attributes
127 * @base: Base Address requested
128 * @size: Size of the region requested
129 * @flags: Memory region attributes to be set
130 *
131 * Allocate a region of memory with the attributes specified through the
132 * parameter. The base parameter is used to specify the base address
133 * of the requested region.
134 *
135 * Return: base address on success, 0 on error
136 */
137phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
138 uint flags);
139
140/**
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200141 * lmb_is_reserved_flags() - test if address is in reserved region with flag bits set
142 *
143 * The function checks if a reserved region comprising @addr exists which has
144 * all flag bits set which are set in @flags.
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200145 *
Heinrich Schuchardte6923f02021-11-14 08:43:07 +0100146 * @addr: address to be tested
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200147 * @flags: bitmap with bits to be tested
148 * Return: 1 if matching reservation exists, 0 otherwise
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200149 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530150int lmb_is_reserved_flags(phys_addr_t addr, int flags);
Heinrich Schuchardt5411fb72023-08-12 19:09:32 +0200151
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530152/**
153 * lmb_free_flags() - Free up a region of memory
154 * @base: Base Address of region to be freed
155 * @size: Size of the region to be freed
156 * @flags: Memory region attributes
157 *
158 * Free up a region of memory.
159 *
160 * Return: 0 if successful, -1 on failure
161 */
162long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags);
163
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530164long lmb_free(phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600165
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530166void lmb_dump_all(void);
167void lmb_dump_all_force(void);
168
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530169struct lmb *lmb_get(void);
170int lmb_push(struct lmb *store);
171void lmb_pop(struct lmb *store);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500172
Prasad Kummari940bebc2024-09-13 13:02:52 +0530173static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
174{
175 return lmb_alloc_addr(addr, len) == addr ? 0 : -1;
176}
177
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600178#endif /* __KERNEL__ */
179
180#endif /* _LINUX_LMB_H */