2 * This file is part of the coreboot project.
4 * Copyright (C) 2013 Google, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
18 #include <device/resource.h>
20 /* A memranges structure consists of a list of range_entry(s). The structure
21 * is exposed so that a memranges can be used on the stack if needed. */
23 struct range_entry
*entries
;
24 /* Coreboot doesn't have a free() function. Therefore, keep a cache of
26 struct range_entry
*free_list
;
29 /* Each region within a memranges structure is represented by a
30 * range_entry structure. Use the associated range_entry_(base|end|size|tag)
31 * functions to interrogate its properties. i.e. don't rely on one's own
32 * interpretation of the fields. */
37 struct range_entry
*next
;
40 /* Initialize a range_entry with inclusive beginning address and exclusive
41 * end address along with the appropriate tag. */
42 static inline void range_entry_init(struct range_entry
*re
,
43 resource_t incl_begin
, resource_t excl_end
,
46 re
->begin
= incl_begin
;
47 re
->end
= excl_end
- 1;
52 /* Return inclusive base address of memory range. */
53 static inline resource_t
range_entry_base(const struct range_entry
*r
)
58 /* Return exclusive end address of memory range. */
59 static inline resource_t
range_entry_end(const struct range_entry
*r
)
64 /* Return size of of memory range. */
65 static inline resource_t
range_entry_size(const struct range_entry
*r
)
67 return r
->end
- r
->begin
+ 1;
70 static inline unsigned long range_entry_tag(const struct range_entry
*r
)
75 static inline void range_entry_update_tag(struct range_entry
*r
,
76 unsigned long new_tag
)
81 /* Iterate over each entry in a memranges structure. Ranges cannot
82 * be deleted while processing each entry as the list cannot be safely
83 * traversed after such an operation.
84 * r - range_entry pointer.
85 * ranges - memranges pointer */
86 #define memranges_each_entry(r, ranges) \
87 for (r = (ranges)->entries; r != NULL; r = r->next)
89 /* Initialize memranges structure providing an optional array of range_entry
90 * to use as the free list. */
91 void memranges_init_empty(struct memranges
*ranges
, struct range_entry
*free
,
94 /* Initialize and fill a memranges structure according to the
95 * mask and match type for all memory resources. Tag each entry with the
97 void memranges_init(struct memranges
*ranges
,
98 unsigned long mask
, unsigned long match
,
101 /* Remove and free all entries within the memranges structure. */
102 void memranges_teardown(struct memranges
*ranges
);
104 /* Add memory resources that match with the corresponding mask and match.
105 * Each entry will be tagged with the provided tag. e.g. To populate
106 * all cacheable memory resources in the range:
107 * memranges_add_resources(range, IORESOURCE_CACHEABLE,
108 * IORESROUCE_CACHEABLE, my_cacheable_tag); */
109 void memranges_add_resources(struct memranges
*ranges
,
110 unsigned long mask
, unsigned long match
,
113 /* Add memory resources that match with the corresponding mask and match but
114 * also provide filter as additional check. The filter will return non-zero
115 * to add the resource or zero to not add the resource. Each entry will be
116 * tagged with the provided tag. e.g. To populate all cacheable memory
117 * resources in the range with a filter:
118 * memranges_add_resources_filter(range, IORESOURCE_CACHEABLE,
119 * IORESROUCE_CACHEABLE, my_cacheable_tag, filter); */
120 typedef int (*memrange_filter_t
)(struct device
*dev
, struct resource
*res
);
121 void memranges_add_resources_filter(struct memranges
*ranges
,
122 unsigned long mask
, unsigned long match
,
124 memrange_filter_t filter
);
126 /* Fill all address ranges up to limit (exclusive) not covered by an entry by
127 * inserting new entries with the provided tag. */
128 void memranges_fill_holes_up_to(struct memranges
*ranges
,
129 resource_t limit
, unsigned long tag
);
131 /* Create a hole in the range by deleting/modifying entries that overlap with
132 * the region specified by base and size. */
133 void memranges_create_hole(struct memranges
*ranges
,
134 resource_t base
, resource_t size
);
136 /* Insert a resource to the given memranges. All existing ranges
137 * covered by range specified by base and size will be removed before a
138 * new one is added. */
139 void memranges_insert(struct memranges
*ranges
,
140 resource_t base
, resource_t size
, unsigned long tag
);
142 /* Update all entries with old_tag to new_tag. */
143 void memranges_update_tag(struct memranges
*ranges
, unsigned long old_tag
,
144 unsigned long new_tag
);
146 /* Returns next entry after the provided entry. NULL if r is last. */
147 struct range_entry
*memranges_next_entry(struct memranges
*ranges
,
148 const struct range_entry
*r
);
149 #endif /* MEMRANGE_H_ */