blob: 0e69b2ffebe4b8a20f7bc6d7769162a9df8195bd [file] [log] [blame]
Aaron Durbina05a8522013-03-22 20:44:46 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google, Inc.
5 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#ifndef MEMRANGE_H_
20#define MEMRANGE_H_
21
22#include <device/resource.h>
23
24/* A memranges structure consists of a list of range_entry(s). The structure
25 * is exposed so that a memranges can be used on the stack if needed. */
26struct memranges {
27 struct range_entry *entries;
28};
29
30/* Each region within a memranges structure is represented by a
31 * range_entry structure. Use the associated range_entry_(base|end|size|tag)
32 * functions to interrogate its properties. i.e. don't rely on one's own
33 * interpretation of the fields. */
34struct range_entry {
35 resource_t begin;
36 resource_t end;
37 unsigned long tag;
38 struct range_entry *next;
39};
40
41/* Return inclusive base address of memory range. */
42static inline resource_t range_entry_base(const struct range_entry *r)
43{
44 return r->begin;
45}
46
47/* Return exclusive end address of memory range. */
48static inline resource_t range_entry_end(const struct range_entry *r)
49{
50 return r->end + 1;
51}
52
53/* Return size of of memory range. */
54static inline resource_t range_entry_size(const struct range_entry *r)
55{
56 return r->end - r->begin + 1;
57}
58
59static inline unsigned long range_entry_tag(const struct range_entry *r)
60{
61 return r->tag;
62}
63
Aaron Durbinf6f6e132013-03-26 21:22:42 -050064static inline void range_entry_update_tag(struct range_entry *r,
65 unsigned long new_tag)
66{
67 r->tag = new_tag;
68}
69
Aaron Durbina05a8522013-03-22 20:44:46 -050070/* Iterate over each entry in a memranges structure. Ranges cannot
71 * be deleted while processing each entry as the list cannot be safely
72 * traversed after such an operation.
73 * r - range_entry pointer.
74 * ranges - memranges pointer */
75#define memranges_each_entry(r, ranges) \
76 for (r = (ranges)->entries; r != NULL; r = r->next)
77
78/* Initialize and fill a memranges structure according to the
79 * mask and match type for all memory resources. Tag each entry with the
80 * specified type. */
81void memranges_init(struct memranges *ranges,
82 unsigned long mask, unsigned long match,
83 unsigned long tag);
84
85/* Remove and free all entries within the memranges structure. */
86void memranges_teardown(struct memranges *ranges);
87
88/* Add memory resources that match with the corresponding mask and match.
89 * Each entry will be tagged with the provided tag. e.g. To populate
90 * all cacheable memory resources in the range:
91 * memranges_add_resources(range, IORESOURCE_CACHEABLE,
92 * IORESROUCE_CACHEABLE, my_cacheable_tag); */
93void memranges_add_resources(struct memranges *ranges,
94 unsigned long mask, unsigned long match,
95 unsigned long tag);
96
97/* Fill all address ranges up to limit (exclusive) not covered by an entry by
98 * inserting new entries with the provided tag. */
99void memranges_fill_holes_up_to(struct memranges *ranges,
100 resource_t limit, unsigned long tag);
101
102/* Create a hole in the range by deleting/modifying entries that overlap with
103 * the region specified by base and size. */
104void memranges_create_hole(struct memranges *ranges,
105 resource_t base, resource_t size);
106
107/* Insert a resource to the given memranges. All existing ranges
108 * covered by range specified by base and size will be removed before a
109 * new one is added. */
110void memranges_insert(struct memranges *ranges,
111 resource_t base, resource_t size, unsigned long tag);
112
Aaron Durbinf6f6e132013-03-26 21:22:42 -0500113/* Returns next entry after the provided entry. NULL if r is last. */
114struct range_entry *memranges_next_entry(struct memranges *ranges,
115 const struct range_entry *r);
Aaron Durbina05a8522013-03-22 20:44:46 -0500116#endif /* MEMRANGE_H_ */