Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 13 | */ |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 14 | |
| 15 | #include <assert.h> |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 16 | #include <stdlib.h> |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 17 | #include <commonlib/helpers.h> |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 18 | #include <console/console.h> |
| 19 | #include <memrange.h> |
| 20 | |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 21 | static inline void range_entry_link(struct range_entry **prev_ptr, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 22 | struct range_entry *r) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 23 | { |
| 24 | r->next = *prev_ptr; |
| 25 | *prev_ptr = r; |
| 26 | } |
| 27 | |
| 28 | static inline void range_entry_unlink(struct range_entry **prev_ptr, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 29 | struct range_entry *r) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 30 | { |
| 31 | *prev_ptr = r->next; |
| 32 | r->next = NULL; |
| 33 | } |
| 34 | |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 35 | static inline void range_entry_unlink_and_free(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 36 | struct range_entry **prev_ptr, |
| 37 | struct range_entry *r) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 38 | { |
| 39 | range_entry_unlink(prev_ptr, r); |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 40 | range_entry_link(&ranges->free_list, r); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 43 | static struct range_entry *alloc_range(struct memranges *ranges) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 44 | { |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 45 | if (ranges->free_list != NULL) { |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 46 | struct range_entry *r; |
| 47 | |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 48 | r = ranges->free_list; |
| 49 | range_entry_unlink(&ranges->free_list, r); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 50 | return r; |
| 51 | } |
Subrata Banik | 42c44c2 | 2019-05-15 20:27:04 +0530 | [diff] [blame] | 52 | if (ENV_PAYLOAD_LOADER) |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 53 | return malloc(sizeof(struct range_entry)); |
| 54 | return NULL; |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static inline struct range_entry * |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 58 | range_list_add(struct memranges *ranges, struct range_entry **prev_ptr, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 59 | resource_t begin, resource_t end, unsigned long tag) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 60 | { |
| 61 | struct range_entry *new_entry; |
| 62 | |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 63 | new_entry = alloc_range(ranges); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 64 | if (new_entry == NULL) { |
| 65 | printk(BIOS_ERR, "Could not allocate range_entry!\n"); |
| 66 | return NULL; |
| 67 | } |
| 68 | new_entry->begin = begin; |
| 69 | new_entry->end = end; |
| 70 | new_entry->tag = tag; |
| 71 | range_entry_link(prev_ptr, new_entry); |
| 72 | |
| 73 | return new_entry; |
| 74 | } |
| 75 | |
| 76 | static void merge_neighbor_entries(struct memranges *ranges) |
| 77 | { |
| 78 | struct range_entry *cur; |
| 79 | struct range_entry *prev; |
| 80 | |
| 81 | prev = NULL; |
| 82 | /* Merge all neighbors and delete/free the leftover entry. */ |
| 83 | for (cur = ranges->entries; cur != NULL; cur = cur->next) { |
| 84 | /* First entry. Just set prev. */ |
| 85 | if (prev == NULL) { |
| 86 | prev = cur; |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | /* If the previous entry merges with the current update the |
| 91 | * previous entry to cover full range and delete current from |
| 92 | * the list. */ |
| 93 | if (prev->end + 1 >= cur->begin && prev->tag == cur->tag) { |
| 94 | prev->end = cur->end; |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 95 | range_entry_unlink_and_free(ranges, &prev->next, cur); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 96 | /* Set cur to prev so cur->next is valid since cur |
| 97 | * was just unlinked and free. */ |
| 98 | cur = prev; |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | prev = cur; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void remove_memranges(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 107 | resource_t begin, resource_t end, |
| 108 | unsigned long unused) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 109 | { |
| 110 | struct range_entry *cur; |
| 111 | struct range_entry *next; |
| 112 | struct range_entry **prev_ptr; |
| 113 | |
| 114 | prev_ptr = &ranges->entries; |
| 115 | for (cur = ranges->entries; cur != NULL; cur = next) { |
| 116 | resource_t tmp_end; |
| 117 | |
| 118 | /* Cache the next value to handle unlinks. */ |
| 119 | next = cur->next; |
| 120 | |
| 121 | /* No other ranges are affected. */ |
| 122 | if (end < cur->begin) |
| 123 | break; |
| 124 | |
| 125 | /* The removal range starts after this one. */ |
| 126 | if (begin > cur->end) { |
| 127 | prev_ptr = &cur->next; |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | /* The removal range overlaps with the current entry either |
| 132 | * partially or fully. However, we need to adjust the removal |
| 133 | * range for any holes. */ |
| 134 | if (begin <= cur->begin) { |
| 135 | begin = cur->begin; |
| 136 | |
| 137 | /* Full removal. */ |
| 138 | if (end >= cur->end) { |
| 139 | begin = cur->end + 1; |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 140 | range_entry_unlink_and_free(ranges, prev_ptr, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 141 | cur); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 142 | continue; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* prev_ptr can be set now that the unlink path wasn't taken. */ |
| 147 | prev_ptr = &cur->next; |
| 148 | |
| 149 | /* Clip the end fragment to do proper splitting. */ |
| 150 | tmp_end = end; |
| 151 | if (end > cur->end) |
| 152 | tmp_end = cur->end; |
| 153 | |
| 154 | /* Hole punched in middle of entry. */ |
| 155 | if (begin > cur->begin && tmp_end < cur->end) { |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 156 | range_list_add(ranges, &cur->next, end + 1, cur->end, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 157 | cur->tag); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 158 | cur->end = begin - 1; |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | /* Removal at beginning. */ |
| 163 | if (begin == cur->begin) |
| 164 | cur->begin = tmp_end + 1; |
| 165 | |
| 166 | /* Removal at end. */ |
| 167 | if (tmp_end == cur->end) |
| 168 | cur->end = begin - 1; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static void merge_add_memranges(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 173 | resource_t begin, resource_t end, |
| 174 | unsigned long tag) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 175 | { |
| 176 | struct range_entry *cur; |
| 177 | struct range_entry **prev_ptr; |
| 178 | |
| 179 | prev_ptr = &ranges->entries; |
| 180 | |
| 181 | /* Remove all existing entries covered by the range. */ |
| 182 | remove_memranges(ranges, begin, end, -1); |
| 183 | |
| 184 | /* Find the entry to place the new entry after. Since |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 185 | * remove_memranges() was called above there is a guaranteed |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 186 | * spot for this new entry. */ |
| 187 | for (cur = ranges->entries; cur != NULL; cur = cur->next) { |
| 188 | /* Found insertion spot before current entry. */ |
| 189 | if (end < cur->begin) |
| 190 | break; |
| 191 | |
| 192 | /* Keep track of previous entry to insert new entry after it. */ |
| 193 | prev_ptr = &cur->next; |
| 194 | |
| 195 | /* The new entry starts after this one. */ |
| 196 | if (begin > cur->end) |
| 197 | continue; |
| 198 | |
| 199 | } |
| 200 | |
| 201 | /* Add new entry and merge with neighbors. */ |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 202 | range_list_add(ranges, prev_ptr, begin, end, tag); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 203 | merge_neighbor_entries(ranges); |
| 204 | } |
| 205 | |
Aaron Durbin | ed9307d | 2014-02-05 15:44:30 -0600 | [diff] [blame] | 206 | void memranges_update_tag(struct memranges *ranges, unsigned long old_tag, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 207 | unsigned long new_tag) |
Aaron Durbin | ed9307d | 2014-02-05 15:44:30 -0600 | [diff] [blame] | 208 | { |
| 209 | struct range_entry *r; |
| 210 | |
| 211 | memranges_each_entry(r, ranges) { |
| 212 | if (range_entry_tag(r) == old_tag) |
| 213 | range_entry_update_tag(r, new_tag); |
| 214 | } |
| 215 | |
| 216 | merge_neighbor_entries(ranges); |
| 217 | } |
| 218 | |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 219 | typedef void (*range_action_t)(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 220 | resource_t begin, resource_t end, |
| 221 | unsigned long tag); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 222 | |
| 223 | static void do_action(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 224 | resource_t base, resource_t size, unsigned long tag, |
| 225 | range_action_t action) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 226 | { |
| 227 | resource_t end; |
| 228 | resource_t begin; |
| 229 | |
Furquan Shaikh | 196ee2b | 2014-07-18 10:25:54 -0700 | [diff] [blame] | 230 | if (size == 0) |
| 231 | return; |
| 232 | |
Furquan Shaikh | 1908340 | 2020-03-24 14:56:38 -0700 | [diff] [blame^] | 233 | /* The addresses are aligned to (1ULL << ranges->align): the begin address is |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 234 | * aligned down while the end address is aligned up to be conservative |
| 235 | * about the full range covered. */ |
Furquan Shaikh | 1908340 | 2020-03-24 14:56:38 -0700 | [diff] [blame^] | 236 | begin = ALIGN_DOWN(base, POWER_OF_2(ranges->align)); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 237 | end = begin + size + (base - begin); |
Furquan Shaikh | 1908340 | 2020-03-24 14:56:38 -0700 | [diff] [blame^] | 238 | end = ALIGN_UP(end, POWER_OF_2(ranges->align)) - 1; |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 239 | action(ranges, begin, end, tag); |
| 240 | } |
| 241 | |
| 242 | void memranges_create_hole(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 243 | resource_t base, resource_t size) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 244 | { |
| 245 | do_action(ranges, base, size, -1, remove_memranges); |
| 246 | } |
| 247 | |
| 248 | void memranges_insert(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 249 | resource_t base, resource_t size, unsigned long tag) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 250 | { |
| 251 | do_action(ranges, base, size, tag, merge_add_memranges); |
| 252 | } |
| 253 | |
| 254 | struct collect_context { |
| 255 | struct memranges *ranges; |
| 256 | unsigned long tag; |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 257 | memrange_filter_t filter; |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | static void collect_ranges(void *gp, struct device *dev, struct resource *res) |
| 261 | { |
| 262 | struct collect_context *ctx = gp; |
| 263 | |
Vladimir Serbinenko | 7a4fa0a | 2014-02-05 23:38:29 +0100 | [diff] [blame] | 264 | if (res->size == 0) |
| 265 | return; |
| 266 | |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 267 | if (ctx->filter == NULL || ctx->filter(dev, res)) |
| 268 | memranges_insert(ctx->ranges, res->base, res->size, ctx->tag); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 271 | void memranges_add_resources_filter(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 272 | unsigned long mask, unsigned long match, |
| 273 | unsigned long tag, |
| 274 | memrange_filter_t filter) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 275 | { |
| 276 | struct collect_context context; |
| 277 | |
| 278 | /* Only deal with MEM resources. */ |
| 279 | mask |= IORESOURCE_MEM; |
| 280 | match |= IORESOURCE_MEM; |
| 281 | |
| 282 | context.ranges = ranges; |
| 283 | context.tag = tag; |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 284 | context.filter = filter; |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 285 | search_global_resources(mask, match, collect_ranges, &context); |
| 286 | } |
| 287 | |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 288 | void memranges_add_resources(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 289 | unsigned long mask, unsigned long match, |
| 290 | unsigned long tag) |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 291 | { |
| 292 | memranges_add_resources_filter(ranges, mask, match, tag, NULL); |
| 293 | } |
| 294 | |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 295 | void memranges_init_empty_with_alignment(struct memranges *ranges, |
| 296 | struct range_entry *to_free, |
Furquan Shaikh | 1908340 | 2020-03-24 14:56:38 -0700 | [diff] [blame^] | 297 | size_t num_free, unsigned char align) |
Furquan Shaikh | 196ee2b | 2014-07-18 10:25:54 -0700 | [diff] [blame] | 298 | { |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 299 | size_t i; |
| 300 | |
Furquan Shaikh | 196ee2b | 2014-07-18 10:25:54 -0700 | [diff] [blame] | 301 | ranges->entries = NULL; |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 302 | ranges->free_list = NULL; |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 303 | ranges->align = align; |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 304 | |
| 305 | for (i = 0; i < num_free; i++) |
Aaron Durbin | a7141c5 | 2016-02-24 18:49:25 -0600 | [diff] [blame] | 306 | range_entry_link(&ranges->free_list, &to_free[i]); |
Furquan Shaikh | 196ee2b | 2014-07-18 10:25:54 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 309 | void memranges_init_with_alignment(struct memranges *ranges, |
| 310 | unsigned long mask, unsigned long match, |
Furquan Shaikh | 1908340 | 2020-03-24 14:56:38 -0700 | [diff] [blame^] | 311 | unsigned long tag, unsigned char align) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 312 | { |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 313 | memranges_init_empty_with_alignment(ranges, NULL, 0, align); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 314 | memranges_add_resources(ranges, mask, match, tag); |
| 315 | } |
| 316 | |
Patrick Rudolph | d67a4bd | 2018-04-10 09:31:10 +0200 | [diff] [blame] | 317 | /* Clone a memrange. The new memrange has the same entries as the old one. */ |
| 318 | void memranges_clone(struct memranges *newranges, struct memranges *oldranges) |
| 319 | { |
| 320 | struct range_entry *r, *cur; |
| 321 | struct range_entry **prev_ptr; |
| 322 | |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 323 | memranges_init_empty_with_alignment(newranges, NULL, 0, oldranges->align); |
Patrick Rudolph | d67a4bd | 2018-04-10 09:31:10 +0200 | [diff] [blame] | 324 | |
| 325 | prev_ptr = &newranges->entries; |
| 326 | memranges_each_entry(r, oldranges) { |
| 327 | cur = range_list_add(newranges, prev_ptr, r->begin, r->end, |
| 328 | r->tag); |
| 329 | prev_ptr = &cur->next; |
| 330 | } |
| 331 | } |
| 332 | |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 333 | void memranges_teardown(struct memranges *ranges) |
| 334 | { |
| 335 | while (ranges->entries != NULL) { |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 336 | range_entry_unlink_and_free(ranges, &ranges->entries, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 337 | ranges->entries); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | void memranges_fill_holes_up_to(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 342 | resource_t limit, unsigned long tag) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 343 | { |
| 344 | struct range_entry *cur; |
| 345 | struct range_entry *prev; |
| 346 | |
| 347 | prev = NULL; |
| 348 | for (cur = ranges->entries; cur != NULL; cur = cur->next) { |
| 349 | /* First entry. Just set prev. */ |
| 350 | if (prev == NULL) { |
| 351 | prev = cur; |
| 352 | continue; |
| 353 | } |
| 354 | |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 355 | /* If the previous entry does not directly precede the current |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 356 | * entry then add a new entry just after the previous one. */ |
| 357 | if (range_entry_end(prev) != cur->begin) { |
| 358 | resource_t end; |
| 359 | |
| 360 | end = cur->begin - 1; |
| 361 | if (end >= limit) |
| 362 | end = limit - 1; |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 363 | range_list_add(ranges, &prev->next, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 364 | range_entry_end(prev), end, tag); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | prev = cur; |
| 368 | |
| 369 | /* Hit the requested range limit. No other entries after this |
| 370 | * are affected. */ |
| 371 | if (cur->begin >= limit) |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | /* Handle the case where the limit was never reached. A new entry needs |
| 376 | * to be added to cover the range up to the limit. */ |
| 377 | if (prev != NULL && range_entry_end(prev) < limit) |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 378 | range_list_add(ranges, &prev->next, range_entry_end(prev), |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 379 | limit - 1, tag); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 380 | |
| 381 | /* Merge all entries that were newly added. */ |
| 382 | merge_neighbor_entries(ranges); |
| 383 | } |
Aaron Durbin | f6f6e13 | 2013-03-26 21:22:42 -0500 | [diff] [blame] | 384 | |
| 385 | struct range_entry *memranges_next_entry(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 386 | const struct range_entry *r) |
Aaron Durbin | f6f6e13 | 2013-03-26 21:22:42 -0500 | [diff] [blame] | 387 | { |
| 388 | return r->next; |
| 389 | } |
Furquan Shaikh | 9c6274c | 2020-03-11 19:06:24 -0700 | [diff] [blame] | 390 | |
| 391 | /* Find a range entry that satisfies the given constraints to fit a hole that matches the |
| 392 | * required alignment, is big enough, does not exceed the limit and has a matching tag. */ |
| 393 | static const struct range_entry *memranges_find_entry(struct memranges *ranges, |
| 394 | resource_t limit, resource_t size, |
Furquan Shaikh | 1908340 | 2020-03-24 14:56:38 -0700 | [diff] [blame^] | 395 | unsigned char align, unsigned long tag) |
Furquan Shaikh | 9c6274c | 2020-03-11 19:06:24 -0700 | [diff] [blame] | 396 | { |
| 397 | const struct range_entry *r; |
| 398 | resource_t base, end; |
| 399 | |
| 400 | if (size == 0) |
| 401 | return NULL; |
| 402 | |
Furquan Shaikh | 9c6274c | 2020-03-11 19:06:24 -0700 | [diff] [blame] | 403 | memranges_each_entry(r, ranges) { |
| 404 | |
| 405 | if (r->tag != tag) |
| 406 | continue; |
| 407 | |
Furquan Shaikh | 1908340 | 2020-03-24 14:56:38 -0700 | [diff] [blame^] | 408 | base = ALIGN_UP(r->begin, POWER_OF_2(align)); |
Furquan Shaikh | 9c6274c | 2020-03-11 19:06:24 -0700 | [diff] [blame] | 409 | end = base + size - 1; |
| 410 | |
| 411 | if (end > r->end) |
| 412 | continue; |
| 413 | |
| 414 | if (end > limit) |
| 415 | continue; |
| 416 | |
| 417 | return r; |
| 418 | } |
| 419 | |
| 420 | return NULL; |
| 421 | } |
| 422 | |
Furquan Shaikh | 1908340 | 2020-03-24 14:56:38 -0700 | [diff] [blame^] | 423 | bool memranges_steal(struct memranges *ranges, resource_t limit, resource_t size, |
| 424 | unsigned char align, unsigned long tag, resource_t *stolen_base) |
Furquan Shaikh | 9c6274c | 2020-03-11 19:06:24 -0700 | [diff] [blame] | 425 | { |
| 426 | resource_t base; |
| 427 | const struct range_entry *r = memranges_find_entry(ranges, limit, size, align, tag); |
| 428 | |
| 429 | if (r == NULL) |
| 430 | return false; |
| 431 | |
Furquan Shaikh | 1908340 | 2020-03-24 14:56:38 -0700 | [diff] [blame^] | 432 | base = ALIGN_UP(r->begin, POWER_OF_2(align)); |
Furquan Shaikh | 9c6274c | 2020-03-11 19:06:24 -0700 | [diff] [blame] | 433 | |
| 434 | memranges_create_hole(ranges, base, size); |
| 435 | *stolen_base = base; |
| 436 | |
| 437 | return true; |
| 438 | } |