Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 1 | /* |
| 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. */ |
| 26 | struct 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. */ |
| 34 | struct 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. */ |
| 42 | static 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. */ |
| 48 | static 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. */ |
| 54 | static inline resource_t range_entry_size(const struct range_entry *r) |
| 55 | { |
| 56 | return r->end - r->begin + 1; |
| 57 | } |
| 58 | |
| 59 | static inline unsigned long range_entry_tag(const struct range_entry *r) |
| 60 | { |
| 61 | return r->tag; |
| 62 | } |
| 63 | |
Aaron Durbin | f6f6e13 | 2013-03-26 21:22:42 -0500 | [diff] [blame] | 64 | static inline void range_entry_update_tag(struct range_entry *r, |
| 65 | unsigned long new_tag) |
| 66 | { |
| 67 | r->tag = new_tag; |
| 68 | } |
| 69 | |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 70 | /* 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. */ |
| 81 | void 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. */ |
| 86 | void 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); */ |
| 93 | void 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. */ |
| 99 | void 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. */ |
| 104 | void 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. */ |
| 110 | void memranges_insert(struct memranges *ranges, |
| 111 | resource_t base, resource_t size, unsigned long tag); |
| 112 | |
Aaron Durbin | f6f6e13 | 2013-03-26 21:22:42 -0500 | [diff] [blame] | 113 | /* Returns next entry after the provided entry. NULL if r is last. */ |
| 114 | struct range_entry *memranges_next_entry(struct memranges *ranges, |
| 115 | const struct range_entry *r); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 116 | #endif /* MEMRANGE_H_ */ |