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 | |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 233 | /* The addresses are aligned to 4096 bytes: the begin address is |
| 234 | * aligned down while the end address is aligned up to be conservative |
| 235 | * about the full range covered. */ |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 236 | begin = ALIGN_DOWN(base, ranges->align); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 237 | end = begin + size + (base - begin); |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 238 | end = ALIGN_UP(end, 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, |
| 297 | size_t num_free, size_t 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 | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 301 | /* Alignment must be a power of 2. */ |
| 302 | assert(IS_POWER_OF_2(align)); |
| 303 | |
Furquan Shaikh | 196ee2b | 2014-07-18 10:25:54 -0700 | [diff] [blame] | 304 | ranges->entries = NULL; |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 305 | ranges->free_list = NULL; |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 306 | ranges->align = align; |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 307 | |
| 308 | for (i = 0; i < num_free; i++) |
Aaron Durbin | a7141c5 | 2016-02-24 18:49:25 -0600 | [diff] [blame] | 309 | range_entry_link(&ranges->free_list, &to_free[i]); |
Furquan Shaikh | 196ee2b | 2014-07-18 10:25:54 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 312 | void memranges_init_with_alignment(struct memranges *ranges, |
| 313 | unsigned long mask, unsigned long match, |
| 314 | unsigned long tag, size_t align) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 315 | { |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 316 | memranges_init_empty_with_alignment(ranges, NULL, 0, align); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 317 | memranges_add_resources(ranges, mask, match, tag); |
| 318 | } |
| 319 | |
Patrick Rudolph | d67a4bd | 2018-04-10 09:31:10 +0200 | [diff] [blame] | 320 | /* Clone a memrange. The new memrange has the same entries as the old one. */ |
| 321 | void memranges_clone(struct memranges *newranges, struct memranges *oldranges) |
| 322 | { |
| 323 | struct range_entry *r, *cur; |
| 324 | struct range_entry **prev_ptr; |
| 325 | |
Furquan Shaikh | 1429092 | 2020-03-11 14:35:35 -0700 | [diff] [blame] | 326 | memranges_init_empty_with_alignment(newranges, NULL, 0, oldranges->align); |
Patrick Rudolph | d67a4bd | 2018-04-10 09:31:10 +0200 | [diff] [blame] | 327 | |
| 328 | prev_ptr = &newranges->entries; |
| 329 | memranges_each_entry(r, oldranges) { |
| 330 | cur = range_list_add(newranges, prev_ptr, r->begin, r->end, |
| 331 | r->tag); |
| 332 | prev_ptr = &cur->next; |
| 333 | } |
| 334 | } |
| 335 | |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 336 | void memranges_teardown(struct memranges *ranges) |
| 337 | { |
| 338 | while (ranges->entries != NULL) { |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 339 | range_entry_unlink_and_free(ranges, &ranges->entries, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 340 | ranges->entries); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | void memranges_fill_holes_up_to(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 345 | resource_t limit, unsigned long tag) |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 346 | { |
| 347 | struct range_entry *cur; |
| 348 | struct range_entry *prev; |
| 349 | |
| 350 | prev = NULL; |
| 351 | for (cur = ranges->entries; cur != NULL; cur = cur->next) { |
| 352 | /* First entry. Just set prev. */ |
| 353 | if (prev == NULL) { |
| 354 | prev = cur; |
| 355 | continue; |
| 356 | } |
| 357 | |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 358 | /* If the previous entry does not directly precede the current |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 359 | * entry then add a new entry just after the previous one. */ |
| 360 | if (range_entry_end(prev) != cur->begin) { |
| 361 | resource_t end; |
| 362 | |
| 363 | end = cur->begin - 1; |
| 364 | if (end >= limit) |
| 365 | end = limit - 1; |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 366 | range_list_add(ranges, &prev->next, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 367 | range_entry_end(prev), end, tag); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | prev = cur; |
| 371 | |
| 372 | /* Hit the requested range limit. No other entries after this |
| 373 | * are affected. */ |
| 374 | if (cur->begin >= limit) |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | /* Handle the case where the limit was never reached. A new entry needs |
| 379 | * to be added to cover the range up to the limit. */ |
| 380 | if (prev != NULL && range_entry_end(prev) < limit) |
Aaron Durbin | 1b915b8 | 2016-01-15 21:59:37 -0600 | [diff] [blame] | 381 | range_list_add(ranges, &prev->next, range_entry_end(prev), |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 382 | limit - 1, tag); |
Aaron Durbin | a05a852 | 2013-03-22 20:44:46 -0500 | [diff] [blame] | 383 | |
| 384 | /* Merge all entries that were newly added. */ |
| 385 | merge_neighbor_entries(ranges); |
| 386 | } |
Aaron Durbin | f6f6e13 | 2013-03-26 21:22:42 -0500 | [diff] [blame] | 387 | |
| 388 | struct range_entry *memranges_next_entry(struct memranges *ranges, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 389 | const struct range_entry *r) |
Aaron Durbin | f6f6e13 | 2013-03-26 21:22:42 -0500 | [diff] [blame] | 390 | { |
| 391 | return r->next; |
| 392 | } |
Furquan Shaikh | 9c6274c | 2020-03-11 19:06:24 -0700 | [diff] [blame] | 393 | |
| 394 | /* Find a range entry that satisfies the given constraints to fit a hole that matches the |
| 395 | * required alignment, is big enough, does not exceed the limit and has a matching tag. */ |
| 396 | static const struct range_entry *memranges_find_entry(struct memranges *ranges, |
| 397 | resource_t limit, resource_t size, |
| 398 | size_t align, unsigned long tag) |
| 399 | { |
| 400 | const struct range_entry *r; |
| 401 | resource_t base, end; |
| 402 | |
| 403 | if (size == 0) |
| 404 | return NULL; |
| 405 | |
| 406 | if (!IS_POWER_OF_2(align)) |
| 407 | return NULL; |
| 408 | |
| 409 | if (!IS_ALIGNED(align, ranges->align)) |
| 410 | return NULL; |
| 411 | |
| 412 | memranges_each_entry(r, ranges) { |
| 413 | |
| 414 | if (r->tag != tag) |
| 415 | continue; |
| 416 | |
| 417 | base = ALIGN_UP(r->begin, align); |
| 418 | end = base + size - 1; |
| 419 | |
| 420 | if (end > r->end) |
| 421 | continue; |
| 422 | |
| 423 | if (end > limit) |
| 424 | continue; |
| 425 | |
| 426 | return r; |
| 427 | } |
| 428 | |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | bool memranges_steal(struct memranges *ranges, resource_t limit, resource_t size, size_t align, |
| 433 | unsigned long tag, resource_t *stolen_base) |
| 434 | { |
| 435 | resource_t base; |
| 436 | const struct range_entry *r = memranges_find_entry(ranges, limit, size, align, tag); |
| 437 | |
| 438 | if (r == NULL) |
| 439 | return false; |
| 440 | |
| 441 | base = ALIGN_UP(r->begin, align); |
| 442 | |
| 443 | memranges_create_hole(ranges, base, size); |
| 444 | *stolen_base = base; |
| 445 | |
| 446 | return true; |
| 447 | } |