| 1 |
|
| 2 |
|
| 3 |
#include <commonlib/bsd/helpers.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <device/device.h> |
| 6 |
#include <memrange.h> |
| 7 |
#include <post.h> |
| 8 |
#include <types.h> |
| 9 |
|
| 10 |
static const char *resource2str(const struct resource *res) |
| 11 |
{ |
| 12 |
if (res->flags & IORESOURCE_IO) |
| 13 |
return "io"; |
| 14 |
if (res->flags & IORESOURCE_PREFETCH) |
| 15 |
return "prefmem"; |
| 16 |
if (res->flags & IORESOURCE_MEM) |
| 17 |
return "mem"; |
| 18 |
return "undefined"; |
| 19 |
} |
| 20 |
|
| 21 |
static void print_domain_res(const struct device *dev, |
| 22 |
const struct resource *res, const char *suffix) |
| 23 |
{ |
| 24 |
printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %u gran: %u limit: %llx%s\n", |
| 25 |
dev_path(dev), resource2str(res), res->base, res->size, |
| 26 |
res->align, res->gran, res->limit, suffix); |
| 27 |
} |
| 28 |
|
| 29 |
#define res_printk(depth, str, ...) printk(BIOS_DEBUG, "%*c"str, depth, ' ', __VA_ARGS__) |
| 30 |
|
| 31 |
static void print_bridge_res(const struct device *dev, const struct resource *res, |
| 32 |
int depth, const char *suffix) |
| 33 |
{ |
| 34 |
res_printk(depth, "%s %s: size: %llx align: %u gran: %u limit: %llx%s\n", dev_path(dev), |
| 35 |
resource2str(res), res->size, res->align, res->gran, res->limit, suffix); |
| 36 |
} |
| 37 |
|
| 38 |
static void print_child_res(const struct device *dev, const struct resource *res, int depth) |
| 39 |
{ |
| 40 |
res_printk(depth + 1, "%s %02lx * [0x%llx - 0x%llx] %s\n", dev_path(dev), |
| 41 |
res->index, res->base, res->base + res->size - 1, resource2str(res)); |
| 42 |
} |
| 43 |
|
| 44 |
static void print_fixed_res(const struct device *dev, |
| 45 |
const struct resource *res, const char *prefix) |
| 46 |
{ |
| 47 |
printk(BIOS_DEBUG, " %s: %s %02lx base %08llx limit %08llx %s (fixed)\n", |
| 48 |
prefix, dev_path(dev), res->index, res->base, res->base + res->size - 1, |
| 49 |
resource2str(res)); |
| 50 |
} |
| 51 |
|
| 52 |
static void print_assigned_res(const struct device *dev, const struct resource *res) |
| 53 |
{ |
| 54 |
printk(BIOS_DEBUG, " %s %02lx * [0x%llx - 0x%llx] limit: %llx %s\n", |
| 55 |
dev_path(dev), res->index, res->base, res->limit, res->limit, resource2str(res)); |
| 56 |
} |
| 57 |
|
| 58 |
static void print_failed_res(const struct device *dev, const struct resource *res) |
| 59 |
{ |
| 60 |
printk(BIOS_DEBUG, " %s %02lx * size: 0x%llx limit: %llx %s\n", |
| 61 |
dev_path(dev), res->index, res->size, res->limit, resource2str(res)); |
| 62 |
} |
| 63 |
|
| 64 |
static void print_resource_ranges(const struct device *dev, const struct memranges *ranges) |
| 65 |
{ |
| 66 |
const struct range_entry *r; |
| 67 |
|
| 68 |
printk(BIOS_INFO, " %s: Resource ranges:\n", dev_path(dev)); |
| 69 |
|
| 70 |
if (memranges_is_empty(ranges)) |
| 71 |
printk(BIOS_INFO, " * EMPTY!!\n"); |
| 72 |
|
| 73 |
memranges_each_entry(r, ranges) { |
| 74 |
printk(BIOS_INFO, " * Base: %llx, Size: %llx, Tag: %lx\n", |
| 75 |
range_entry_base(r), range_entry_size(r), range_entry_tag(r)); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static bool dev_has_children(const struct device *dev) |
| 80 |
{ |
| 81 |
const struct bus *bus = dev->downstream; |
| 82 |
return bus && bus->children; |
| 83 |
} |
| 84 |
|
| 85 |
static resource_t effective_limit(const struct resource *const res) |
| 86 |
{ |
| 87 |
if (CONFIG(ALWAYS_ALLOW_ABOVE_4G_ALLOCATION)) |
| 88 |
return res->limit; |
| 89 |
|
| 90 |
|
| 91 |
if (res->flags & IORESOURCE_BRIDGE) |
| 92 |
return res->limit; |
| 93 |
|
| 94 |
const resource_t quirk_4g_limit = |
| 95 |
res->flags & IORESOURCE_ABOVE_4G ? UINT64_MAX : UINT32_MAX; |
| 96 |
return MIN(res->limit, quirk_4g_limit); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static void update_bridge_resource(const struct device *bridge, struct resource *bridge_res, |
| 118 |
int print_depth) |
| 119 |
{ |
| 120 |
const struct device *child; |
| 121 |
struct resource *child_res; |
| 122 |
resource_t base; |
| 123 |
const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH; |
| 124 |
const unsigned long type_match = bridge_res->flags & type_mask; |
| 125 |
struct bus *bus = bridge->downstream; |
| 126 |
|
| 127 |
child_res = NULL; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
base = 0; |
| 139 |
|
| 140 |
print_bridge_res(bridge, bridge_res, print_depth, ""); |
| 141 |
|
| 142 |
while ((child = largest_resource(bus, &child_res, type_mask, type_match))) { |
| 143 |
|
| 144 |
if (!child_res->size) |
| 145 |
continue; |
| 146 |
|
| 147 |
|
| 148 |
if (!child_res->limit) |
| 149 |
continue; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if (child_res->align > bridge_res->align) |
| 158 |
bridge_res->align = child_res->align; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
if (effective_limit(child_res) < bridge_res->limit) |
| 167 |
bridge_res->limit = effective_limit(child_res); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
base = ALIGN_UP(base, POWER_OF_2(child_res->align)); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
child_res->base = base; |
| 181 |
child_res->flags &= ~(IORESOURCE_ASSIGNED | IORESOURCE_STORED); |
| 182 |
|
| 183 |
print_child_res(child, child_res, print_depth); |
| 184 |
|
| 185 |
base += child_res->size; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
bridge_res->size = ALIGN_UP(base, POWER_OF_2(bridge_res->gran)); |
| 196 |
|
| 197 |
print_bridge_res(bridge, bridge_res, print_depth, " done"); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static void compute_bridge_resources(const struct device *bridge, unsigned long type_match, |
| 206 |
int print_depth) |
| 207 |
{ |
| 208 |
const struct device *child; |
| 209 |
struct resource *res; |
| 210 |
struct bus *bus = bridge->downstream; |
| 211 |
const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH; |
| 212 |
|
| 213 |
for (res = bridge->resource_list; res; res = res->next) { |
| 214 |
if (!(res->flags & IORESOURCE_BRIDGE)) |
| 215 |
continue; |
| 216 |
|
| 217 |
if ((res->flags & type_mask) != type_match) |
| 218 |
continue; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
for (child = bus->children; child; child = child->sibling) { |
| 225 |
if (!dev_has_children(child)) |
| 226 |
continue; |
| 227 |
compute_bridge_resources(child, type_match, print_depth + 1); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
update_bridge_resource(bridge, res, print_depth); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
static void compute_domain_resources(const struct device *domain) |
| 254 |
{ |
| 255 |
const struct device *child; |
| 256 |
const int print_depth = 1; |
| 257 |
|
| 258 |
if (domain->downstream == NULL) |
| 259 |
return; |
| 260 |
|
| 261 |
for (child = domain->downstream->children; child; child = child->sibling) { |
| 262 |
|
| 263 |
if (!dev_has_children(child)) |
| 264 |
continue; |
| 265 |
|
| 266 |
compute_bridge_resources(child, IORESOURCE_IO, print_depth); |
| 267 |
compute_bridge_resources(child, IORESOURCE_MEM, print_depth); |
| 268 |
compute_bridge_resources(child, IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 269 |
print_depth); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
static void avoid_fixed_resources(struct memranges *ranges, const struct device *dev, |
| 285 |
unsigned long mask_match) |
| 286 |
{ |
| 287 |
const struct resource *res; |
| 288 |
const struct device *child; |
| 289 |
const struct bus *bus; |
| 290 |
|
| 291 |
for (res = dev->resource_list; res != NULL; res = res->next) { |
| 292 |
if ((res->flags & mask_match) != mask_match) |
| 293 |
continue; |
| 294 |
if (!res->size) |
| 295 |
continue; |
| 296 |
print_fixed_res(dev, res, __func__); |
| 297 |
memranges_create_hole(ranges, res->base, res->size); |
| 298 |
} |
| 299 |
|
| 300 |
bus = dev->downstream; |
| 301 |
if (bus == NULL) |
| 302 |
return; |
| 303 |
|
| 304 |
for (child = bus->children; child != NULL; child = child->sibling) |
| 305 |
avoid_fixed_resources(ranges, child, mask_match); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static void setup_resource_ranges(const struct device *const domain, |
| 318 |
const unsigned long type, |
| 319 |
struct memranges *const ranges) |
| 320 |
{ |
| 321 |
|
| 322 |
|
| 323 |
const unsigned char alignment = type == IORESOURCE_MEM ? 12 : 0; |
| 324 |
const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_FIXED; |
| 325 |
|
| 326 |
memranges_init_empty_with_alignment(ranges, NULL, 0, alignment); |
| 327 |
|
| 328 |
for (struct resource *res = domain->resource_list; res != NULL; res = res->next) { |
| 329 |
if ((res->flags & type_mask) != type) |
| 330 |
continue; |
| 331 |
print_domain_res(domain, res, ""); |
| 332 |
memranges_insert(ranges, res->base, res->limit - res->base + 1, type); |
| 333 |
} |
| 334 |
|
| 335 |
if (type == IORESOURCE_IO) { |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
memranges_create_hole(ranges, 0x3b0, 0x3df - 0x3b0 + 1); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
avoid_fixed_resources(ranges, domain, type | IORESOURCE_FIXED); |
| 350 |
|
| 351 |
print_resource_ranges(domain, ranges); |
| 352 |
} |
| 353 |
|
| 354 |
static void cleanup_domain_resource_ranges(const struct device *dev, struct memranges *ranges, |
| 355 |
unsigned long type) |
| 356 |
{ |
| 357 |
memranges_teardown(ranges); |
| 358 |
for (struct resource *res = dev->resource_list; res != NULL; res = res->next) { |
| 359 |
if (res->flags & IORESOURCE_FIXED) |
| 360 |
continue; |
| 361 |
if ((res->flags & IORESOURCE_TYPE_MASK) != type) |
| 362 |
continue; |
| 363 |
print_domain_res(dev, res, " done"); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
static void assign_resource(struct resource *const res, const resource_t base, |
| 368 |
const struct device *const dev) |
| 369 |
{ |
| 370 |
res->base = base; |
| 371 |
res->limit = res->base + res->size - 1; |
| 372 |
res->flags |= IORESOURCE_ASSIGNED; |
| 373 |
res->flags &= ~IORESOURCE_STORED; |
| 374 |
|
| 375 |
print_assigned_res(dev, res); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
static void allocate_toplevel_resources(const struct device *const domain, |
| 387 |
const unsigned long type) |
| 388 |
{ |
| 389 |
const unsigned long type_mask = IORESOURCE_TYPE_MASK; |
| 390 |
struct resource *res = NULL; |
| 391 |
const struct device *dev; |
| 392 |
struct memranges ranges; |
| 393 |
resource_t base; |
| 394 |
|
| 395 |
if (!dev_has_children(domain)) |
| 396 |
return; |
| 397 |
|
| 398 |
setup_resource_ranges(domain, type, &ranges); |
| 399 |
|
| 400 |
while ((dev = largest_resource(domain->downstream, &res, type_mask, type))) { |
| 401 |
if (!res->size) |
| 402 |
continue; |
| 403 |
|
| 404 |
if (!memranges_steal(&ranges, effective_limit(res), res->size, res->align, |
| 405 |
type, &base, CONFIG(RESOURCE_ALLOCATION_TOP_DOWN))) { |
| 406 |
printk(BIOS_ERR, "Resource didn't fit!!!\n"); |
| 407 |
print_failed_res(dev, res); |
| 408 |
continue; |
| 409 |
} |
| 410 |
|
| 411 |
assign_resource(res, base, dev); |
| 412 |
} |
| 413 |
|
| 414 |
cleanup_domain_resource_ranges(domain, &ranges, type); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
static void assign_resource_cb(void *param, struct device *dev, struct resource *res) |
| 430 |
{ |
| 431 |
|
| 432 |
if (!res->size || !res->limit) |
| 433 |
return; |
| 434 |
|
| 435 |
assign_resource(res, *(const resource_t *)param + res->base, dev); |
| 436 |
} |
| 437 |
static void allocate_bridge_resources(const struct device *bridge) |
| 438 |
{ |
| 439 |
const unsigned long type_mask = |
| 440 |
IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH | IORESOURCE_FIXED; |
| 441 |
struct bus *const bus = bridge->downstream; |
| 442 |
struct resource *res; |
| 443 |
struct device *child; |
| 444 |
|
| 445 |
for (res = bridge->resource_list; res != NULL; res = res->next) { |
| 446 |
if (!res->size) |
| 447 |
continue; |
| 448 |
|
| 449 |
if (!(res->flags & IORESOURCE_BRIDGE)) |
| 450 |
continue; |
| 451 |
|
| 452 |
if (!(res->flags & IORESOURCE_ASSIGNED)) |
| 453 |
continue; |
| 454 |
|
| 455 |
|
| 456 |
search_bus_resources(bus, type_mask, res->flags & type_mask, |
| 457 |
assign_resource_cb, &res->base); |
| 458 |
} |
| 459 |
|
| 460 |
for (child = bus->children; child != NULL; child = child->sibling) { |
| 461 |
if (!dev_has_children(child)) |
| 462 |
continue; |
| 463 |
|
| 464 |
allocate_bridge_resources(child); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
static void allocate_domain_resources(const struct device *domain) |
| 482 |
{ |
| 483 |
|
| 484 |
allocate_toplevel_resources(domain, IORESOURCE_IO); |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
allocate_toplevel_resources(domain, IORESOURCE_MEM); |
| 494 |
|
| 495 |
struct device *child; |
| 496 |
for (child = domain->downstream->children; child; child = child->sibling) { |
| 497 |
if (!dev_has_children(child)) |
| 498 |
continue; |
| 499 |
|
| 500 |
|
| 501 |
allocate_bridge_resources(child); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
void allocate_resources(const struct device *root) |
| 550 |
{ |
| 551 |
const struct device *child; |
| 552 |
|
| 553 |
if ((root == NULL) || (root->downstream == NULL)) |
| 554 |
return; |
| 555 |
|
| 556 |
for (child = root->downstream->children; child; child = child->sibling) { |
| 557 |
if (child->path.type != DEVICE_PATH_DOMAIN) |
| 558 |
continue; |
| 559 |
|
| 560 |
post_log_path(child); |
| 561 |
|
| 562 |
|
| 563 |
printk(BIOS_INFO, "=== Resource allocator: %s - Pass 1 (relative placement) ===\n", |
| 564 |
dev_path(child)); |
| 565 |
compute_domain_resources(child); |
| 566 |
|
| 567 |
|
| 568 |
printk(BIOS_INFO, "=== Resource allocator: %s - Pass 2 (allocating resources) ===\n", |
| 569 |
dev_path(child)); |
| 570 |
allocate_domain_resources(child); |
| 571 |
|
| 572 |
printk(BIOS_INFO, "=== Resource allocator: %s - resource allocation complete ===\n", |
| 573 |
dev_path(child)); |
| 574 |
} |
| 575 |
} |