blob: 23dae6a30ac5fe1cef0547394f4407b164bd4e0a [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
64/* Iterate over each entry in a memranges structure. Ranges cannot
65 * be deleted while processing each entry as the list cannot be safely
66 * traversed after such an operation.
67 * r - range_entry pointer.
68 * ranges - memranges pointer */
69#define memranges_each_entry(r, ranges) \
70 for (r = (ranges)->entries; r != NULL; r = r->next)
71
72/* Initialize and fill a memranges structure according to the
73 * mask and match type for all memory resources. Tag each entry with the
74 * specified type. */
75void memranges_init(struct memranges *ranges,
76 unsigned long mask, unsigned long match,
77 unsigned long tag);
78
79/* Remove and free all entries within the memranges structure. */
80void memranges_teardown(struct memranges *ranges);
81
82/* Add memory resources that match with the corresponding mask and match.
83 * Each entry will be tagged with the provided tag. e.g. To populate
84 * all cacheable memory resources in the range:
85 * memranges_add_resources(range, IORESOURCE_CACHEABLE,
86 * IORESROUCE_CACHEABLE, my_cacheable_tag); */
87void memranges_add_resources(struct memranges *ranges,
88 unsigned long mask, unsigned long match,
89 unsigned long tag);
90
91/* Fill all address ranges up to limit (exclusive) not covered by an entry by
92 * inserting new entries with the provided tag. */
93void memranges_fill_holes_up_to(struct memranges *ranges,
94 resource_t limit, unsigned long tag);
95
96/* Create a hole in the range by deleting/modifying entries that overlap with
97 * the region specified by base and size. */
98void memranges_create_hole(struct memranges *ranges,
99 resource_t base, resource_t size);
100
101/* Insert a resource to the given memranges. All existing ranges
102 * covered by range specified by base and size will be removed before a
103 * new one is added. */
104void memranges_insert(struct memranges *ranges,
105 resource_t base, resource_t size, unsigned long tag);
106
107#endif /* MEMRANGE_H_ */