1 /* SPDX-License-Identifier: GPL-2.0-only */
5 #include <device/resource.h>
9 /* A memranges structure consists of a list of range_entry(s). The structure
10 * is exposed so that a memranges can be used on the stack if needed. */
12 struct range_entry
*entries
;
13 /* coreboot doesn't have a free() function. Therefore, keep a cache of
15 struct range_entry
*free_list
;
16 /* Alignment(log 2) for base and end addresses of the range. */
20 /* Each region within a memranges structure is represented by a
21 * range_entry structure. Use the associated range_entry_(base|end|size|tag)
22 * functions to interrogate its properties. i.e. don't rely on one's own
23 * interpretation of the fields. */
28 struct range_entry
*next
;
31 /* Initialize a range_entry with inclusive beginning address and exclusive
32 * end address along with the appropriate tag. */
33 static inline void range_entry_init(struct range_entry
*re
,
34 resource_t incl_begin
, resource_t excl_end
,
37 re
->begin
= incl_begin
;
38 re
->end
= excl_end
- 1;
43 /* Return inclusive base address of memory range. */
44 static inline resource_t
range_entry_base(const struct range_entry
*r
)
49 /* Return exclusive end address of memory range. */
50 static inline resource_t
range_entry_end(const struct range_entry
*r
)
55 /* Return size of memory range. */
56 static inline resource_t
range_entry_size(const struct range_entry
*r
)
58 return r
->end
- r
->begin
+ 1;
61 static inline unsigned long range_entry_tag(const struct range_entry
*r
)
66 static inline void range_entry_update_tag(struct range_entry
*r
,
67 unsigned long new_tag
)
72 static inline bool memranges_is_empty(const struct memranges
*ranges
)
74 return ranges
->entries
== NULL
;
77 /* Iterate over each entry in a memranges structure. Ranges cannot
78 * be deleted while processing each entry as the list cannot be safely
79 * traversed after such an operation.
80 * r - range_entry pointer.
81 * ranges - memranges pointer */
82 #define memranges_each_entry(r, ranges) \
83 for (r = (ranges)->entries; r != NULL; r = r->next)
85 /* Initialize memranges structure providing an optional array of range_entry
86 * to use as the free list. Additionally, it accepts an align parameter that
87 * represents the required alignment(log 2) of addresses. */
88 void memranges_init_empty_with_alignment(struct memranges
*ranges
,
89 struct range_entry
*free
,
90 size_t num_free
, unsigned char align
);
92 /* Initialize and fill a memranges structure according to the
93 * mask and match type for all memory resources. Tag each entry with the
94 * specified type. Additionally, it accepts an align parameter that
95 * represents the required alignment(log 2) of addresses. */
96 void memranges_init_with_alignment(struct memranges
*ranges
,
97 unsigned long mask
, unsigned long match
,
98 unsigned long tag
, unsigned char align
);
100 /* Initialize memranges structure providing an optional array of range_entry
101 * to use as the free list. Addresses are default aligned to 4KiB(2^12). */
102 #define memranges_init_empty(__ranges, __free, __num_free) \
103 memranges_init_empty_with_alignment(__ranges, __free, __num_free, 12);
105 /* Initialize and fill a memranges structure according to the
106 * mask and match type for all memory resources. Tag each entry with the
107 * specified type. Addresses are default aligned to 4KiB(2^12). */
108 #define memranges_init(__ranges, __mask, __match, __tag) \
109 memranges_init_with_alignment(__ranges, __mask, __match, __tag, 12);
111 /* Clone a memrange. The new memrange has the same entries as the old one. */
112 void memranges_clone(struct memranges
*newranges
, struct memranges
*oldranges
);
114 /* Remove and free all entries within the memranges structure. */
115 void memranges_teardown(struct memranges
*ranges
);
117 /* Add memory resources that match with the corresponding mask and match.
118 * Each entry will be tagged with the provided tag. e.g. To populate
119 * all cacheable memory resources in the range:
120 * memranges_add_resources(range, IORESOURCE_CACHEABLE,
121 * IORESROUCE_CACHEABLE, my_cacheable_tag); */
122 void memranges_add_resources(struct memranges
*ranges
,
123 unsigned long mask
, unsigned long match
,
126 /* Add memory resources that match with the corresponding mask and match but
127 * also provide filter as additional check. The filter will return non-zero
128 * to add the resource or zero to not add the resource. Each entry will be
129 * tagged with the provided tag. e.g. To populate all cacheable memory
130 * resources in the range with a filter:
131 * memranges_add_resources_filter(range, IORESOURCE_CACHEABLE,
132 * IORESROUCE_CACHEABLE, my_cacheable_tag, filter); */
133 typedef int (*memrange_filter_t
)(struct device
*dev
, struct resource
*res
);
134 void memranges_add_resources_filter(struct memranges
*ranges
,
135 unsigned long mask
, unsigned long match
,
137 memrange_filter_t filter
);
139 /* Fill all address ranges up to limit (exclusive) not covered by an entry by
140 * inserting new entries with the provided tag. */
141 void memranges_fill_holes_up_to(struct memranges
*ranges
,
142 resource_t limit
, unsigned long tag
);
144 /* Create a hole in the range by deleting/modifying entries that overlap with
145 * the region specified by base and size. */
146 void memranges_create_hole(struct memranges
*ranges
,
147 resource_t base
, resource_t size
);
149 /* Insert a resource to the given memranges. All existing ranges
150 * covered by range specified by base and size will be removed before a
151 * new one is added. */
152 void memranges_insert(struct memranges
*ranges
,
153 resource_t base
, resource_t size
, unsigned long tag
);
155 /* Update all entries with old_tag to new_tag. */
156 void memranges_update_tag(struct memranges
*ranges
, unsigned long old_tag
,
157 unsigned long new_tag
);
159 /* Returns next entry after the provided entry. NULL if r is last. */
160 struct range_entry
*memranges_next_entry(struct memranges
*ranges
,
161 const struct range_entry
*r
);
163 /* Steals memory from the available list in given ranges as per the constraints:
164 * limit = Upper bound for the memory range to steal (Inclusive).
165 * size = Requested size for the stolen memory.
166 * align = Required alignment(log 2) for the starting address of the stolen memory.
167 * tag = Use a range that matches the given tag.
169 * If the constraints can be satisfied, this function creates a hole in the memrange,
170 * writes the base address of that hole to stolen_base and returns true. Otherwise it returns
172 bool memranges_steal(struct memranges
*ranges
, resource_t limit
, resource_t size
,
173 unsigned char align
, unsigned long tag
, resource_t
*stolen_base
);
175 #endif /* MEMRANGE_H_ */