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