| 1 |
|
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#include <commonlib/bsd/helpers.h> |
| 5 |
#include <console/console.h> |
| 6 |
#include <device/device.h> |
| 7 |
#include <device/pci_def.h> |
| 8 |
#include <device/pci_ids.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <types.h> |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
struct device *dev_find_lapic(unsigned int apic_id) |
| 21 |
{ |
| 22 |
struct device *dev; |
| 23 |
struct device *result = NULL; |
| 24 |
|
| 25 |
for (dev = all_devices; dev; dev = dev->next) { |
| 26 |
if (dev->path.type == DEVICE_PATH_APIC && |
| 27 |
dev->path.apic.apic_id == apic_id) { |
| 28 |
result = dev; |
| 29 |
break; |
| 30 |
} |
| 31 |
} |
| 32 |
return result; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
struct device *dev_find_device(u16 vendor, u16 device, struct device *from) |
| 46 |
{ |
| 47 |
if (!from) |
| 48 |
from = all_devices; |
| 49 |
else |
| 50 |
from = from->next; |
| 51 |
|
| 52 |
while (from && (from->vendor != vendor || from->device != device)) |
| 53 |
from = from->next; |
| 54 |
|
| 55 |
return from; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
struct device *dev_find_class(unsigned int class, struct device *from) |
| 68 |
{ |
| 69 |
if (!from) |
| 70 |
from = all_devices; |
| 71 |
else |
| 72 |
from = from->next; |
| 73 |
|
| 74 |
while (from && (from->class & 0xffffff00) != class) |
| 75 |
from = from->next; |
| 76 |
|
| 77 |
return from; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
u32 dev_path_encode(const struct device *dev) |
| 87 |
{ |
| 88 |
u32 ret; |
| 89 |
|
| 90 |
if (!dev) |
| 91 |
return 0; |
| 92 |
|
| 93 |
|
| 94 |
ret = dev->path.type << 16; |
| 95 |
|
| 96 |
|
| 97 |
switch (dev->path.type) { |
| 98 |
case DEVICE_PATH_ROOT: |
| 99 |
break; |
| 100 |
case DEVICE_PATH_PCI: |
| 101 |
ret |= dev->upstream->segment_group << 16 | dev->upstream->secondary << 8 | dev->path.pci.devfn; |
| 102 |
break; |
| 103 |
case DEVICE_PATH_PNP: |
| 104 |
ret |= dev->path.pnp.port << 8 | dev->path.pnp.device; |
| 105 |
break; |
| 106 |
case DEVICE_PATH_I2C: |
| 107 |
ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device; |
| 108 |
break; |
| 109 |
case DEVICE_PATH_APIC: |
| 110 |
ret |= dev->path.apic.apic_id; |
| 111 |
break; |
| 112 |
case DEVICE_PATH_DOMAIN: |
| 113 |
ret |= dev->path.domain.domain_id; |
| 114 |
break; |
| 115 |
case DEVICE_PATH_CPU_CLUSTER: |
| 116 |
ret |= dev->path.cpu_cluster.cluster; |
| 117 |
break; |
| 118 |
case DEVICE_PATH_CPU: |
| 119 |
ret |= dev->path.cpu.id; |
| 120 |
break; |
| 121 |
case DEVICE_PATH_CPU_BUS: |
| 122 |
ret |= dev->path.cpu_bus.id; |
| 123 |
break; |
| 124 |
case DEVICE_PATH_IOAPIC: |
| 125 |
ret |= dev->path.ioapic.ioapic_id; |
| 126 |
break; |
| 127 |
case DEVICE_PATH_GENERIC: |
| 128 |
ret |= dev->path.generic.subid << 8 | dev->path.generic.id; |
| 129 |
break; |
| 130 |
case DEVICE_PATH_SPI: |
| 131 |
ret |= dev->path.spi.cs; |
| 132 |
break; |
| 133 |
case DEVICE_PATH_USB: |
| 134 |
ret |= dev->path.usb.port_type << 8 | dev->path.usb.port_id; |
| 135 |
break; |
| 136 |
case DEVICE_PATH_GPIO: |
| 137 |
ret |= dev->path.gpio.id; |
| 138 |
break; |
| 139 |
case DEVICE_PATH_MDIO: |
| 140 |
ret |= dev->path.mdio.addr; |
| 141 |
break; |
| 142 |
case DEVICE_PATH_NONE: |
| 143 |
case DEVICE_PATH_MMIO: |
| 144 |
default: |
| 145 |
break; |
| 146 |
} |
| 147 |
|
| 148 |
return ret; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
const char *dev_path(const struct device *dev) |
| 156 |
{ |
| 157 |
static char buffer[DEVICE_PATH_MAX]; |
| 158 |
|
| 159 |
buffer[0] = '\0'; |
| 160 |
if (!dev) { |
| 161 |
strcpy(buffer, "<null>"); |
| 162 |
} else { |
| 163 |
switch (dev->path.type) { |
| 164 |
case DEVICE_PATH_NONE: |
| 165 |
strcpy(buffer, "NONE"); |
| 166 |
break; |
| 167 |
case DEVICE_PATH_ROOT: |
| 168 |
strcpy(buffer, "Root Device"); |
| 169 |
break; |
| 170 |
case DEVICE_PATH_PCI: |
| 171 |
snprintf(buffer, sizeof(buffer), |
| 172 |
"PCI: %02x:%02x:%02x.%01x", |
| 173 |
dev->upstream->segment_group, |
| 174 |
dev->upstream->secondary, |
| 175 |
PCI_SLOT(dev->path.pci.devfn), |
| 176 |
PCI_FUNC(dev->path.pci.devfn)); |
| 177 |
break; |
| 178 |
case DEVICE_PATH_PNP: |
| 179 |
snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x", |
| 180 |
dev->path.pnp.port, dev->path.pnp.device); |
| 181 |
break; |
| 182 |
case DEVICE_PATH_I2C: |
| 183 |
snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x", |
| 184 |
dev->upstream->secondary, |
| 185 |
dev->path.i2c.device); |
| 186 |
break; |
| 187 |
case DEVICE_PATH_APIC: |
| 188 |
snprintf(buffer, sizeof(buffer), "APIC: %02x", |
| 189 |
dev->path.apic.apic_id); |
| 190 |
break; |
| 191 |
case DEVICE_PATH_IOAPIC: |
| 192 |
snprintf(buffer, sizeof(buffer), "IOAPIC: %02x", |
| 193 |
dev->path.ioapic.ioapic_id); |
| 194 |
break; |
| 195 |
case DEVICE_PATH_DOMAIN: |
| 196 |
snprintf(buffer, sizeof(buffer), "DOMAIN: %08x", |
| 197 |
dev->path.domain.domain_id); |
| 198 |
break; |
| 199 |
case DEVICE_PATH_CPU_CLUSTER: |
| 200 |
snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x", |
| 201 |
dev->path.cpu_cluster.cluster); |
| 202 |
break; |
| 203 |
case DEVICE_PATH_CPU: |
| 204 |
snprintf(buffer, sizeof(buffer), |
| 205 |
"CPU: %02x", dev->path.cpu.id); |
| 206 |
break; |
| 207 |
case DEVICE_PATH_CPU_BUS: |
| 208 |
snprintf(buffer, sizeof(buffer), |
| 209 |
"CPU_BUS: %02x", dev->path.cpu_bus.id); |
| 210 |
break; |
| 211 |
case DEVICE_PATH_GENERIC: |
| 212 |
snprintf(buffer, sizeof(buffer), |
| 213 |
"GENERIC: %d.%d", dev->path.generic.id, |
| 214 |
dev->path.generic.subid); |
| 215 |
break; |
| 216 |
case DEVICE_PATH_SPI: |
| 217 |
snprintf(buffer, sizeof(buffer), "SPI: %02x", |
| 218 |
dev->path.spi.cs); |
| 219 |
break; |
| 220 |
case DEVICE_PATH_USB: |
| 221 |
snprintf(buffer, sizeof(buffer), "USB%u port %u", |
| 222 |
dev->path.usb.port_type, dev->path.usb.port_id); |
| 223 |
break; |
| 224 |
case DEVICE_PATH_MMIO: |
| 225 |
snprintf(buffer, sizeof(buffer), "MMIO: %08lx", |
| 226 |
dev->path.mmio.addr); |
| 227 |
break; |
| 228 |
case DEVICE_PATH_GPIO: |
| 229 |
snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id); |
| 230 |
break; |
| 231 |
case DEVICE_PATH_MDIO: |
| 232 |
snprintf(buffer, sizeof(buffer), "MDIO: %02x", dev->path.mdio.addr); |
| 233 |
break; |
| 234 |
case DEVICE_PATH_GICC_V3: |
| 235 |
snprintf(buffer, sizeof(buffer), "GICV3: %02llx", dev->path.gicc_v3.mpidr); |
| 236 |
break; |
| 237 |
default: |
| 238 |
printk(BIOS_ERR, "Unknown device path type: %d\n", |
| 239 |
dev->path.type); |
| 240 |
break; |
| 241 |
} |
| 242 |
} |
| 243 |
return buffer; |
| 244 |
} |
| 245 |
|
| 246 |
const char *dev_name(const struct device *dev) |
| 247 |
{ |
| 248 |
if (dev->name) |
| 249 |
return dev->name; |
| 250 |
else if (dev->chip_ops && dev->chip_ops->name) |
| 251 |
return dev->chip_ops->name; |
| 252 |
else |
| 253 |
return "unknown"; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
const struct device *dev_get_domain(const struct device *dev) |
| 258 |
{ |
| 259 |
|
| 260 |
while (dev && dev->upstream && !is_root_device(dev)) { |
| 261 |
if (dev->path.type == DEVICE_PATH_DOMAIN) |
| 262 |
return dev; |
| 263 |
dev = dev->upstream->dev; |
| 264 |
} |
| 265 |
|
| 266 |
return NULL; |
| 267 |
} |
| 268 |
|
| 269 |
unsigned int dev_get_domain_id(const struct device *dev) |
| 270 |
{ |
| 271 |
const struct device *domain_dev = dev_get_domain(dev); |
| 272 |
|
| 273 |
assert(domain_dev); |
| 274 |
|
| 275 |
if (!domain_dev) { |
| 276 |
printk(BIOS_ERR, "%s: doesn't have a domain device\n", dev_path(dev)); |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
return domain_dev->path.domain.domain_id; |
| 281 |
} |
| 282 |
|
| 283 |
bool is_domain0(const struct device *dev) |
| 284 |
{ |
| 285 |
return dev && dev->path.type == DEVICE_PATH_DOMAIN && dev->path.domain.domain_id == 0; |
| 286 |
} |
| 287 |
|
| 288 |
bool is_dev_on_domain0(const struct device *dev) |
| 289 |
{ |
| 290 |
return is_domain0(dev_get_domain(dev)); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static int allocate_more_resources(void) |
| 299 |
{ |
| 300 |
int i; |
| 301 |
struct resource *new_res_list; |
| 302 |
|
| 303 |
new_res_list = malloc(64 * sizeof(*new_res_list)); |
| 304 |
|
| 305 |
if (new_res_list == NULL) |
| 306 |
return 0; |
| 307 |
|
| 308 |
memset(new_res_list, 0, 64 * sizeof(*new_res_list)); |
| 309 |
|
| 310 |
for (i = 0; i < 64 - 1; i++) |
| 311 |
new_res_list[i].next = &new_res_list[i+1]; |
| 312 |
|
| 313 |
free_resources = new_res_list; |
| 314 |
return 1; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
static void free_resource(struct device *dev, struct resource *res, |
| 325 |
struct resource *prev) |
| 326 |
{ |
| 327 |
if (prev) |
| 328 |
prev->next = res->next; |
| 329 |
else |
| 330 |
dev->resource_list = res->next; |
| 331 |
|
| 332 |
res->next = free_resources; |
| 333 |
free_resources = res; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
void compact_resources(struct device *dev) |
| 344 |
{ |
| 345 |
struct resource *res, *next, *prev = NULL; |
| 346 |
|
| 347 |
|
| 348 |
for (res = dev->resource_list; res; res = next) { |
| 349 |
next = res->next; |
| 350 |
if (!res->flags) |
| 351 |
free_resource(dev, res, prev); |
| 352 |
else |
| 353 |
prev = res; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
struct resource *probe_resource(const struct device *dev, unsigned int index) |
| 365 |
{ |
| 366 |
struct resource *res; |
| 367 |
|
| 368 |
|
| 369 |
for (res = dev->resource_list; res; res = res->next) { |
| 370 |
if (res->index == index) |
| 371 |
break; |
| 372 |
} |
| 373 |
|
| 374 |
return res; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
struct resource *new_resource(struct device *dev, unsigned int index) |
| 388 |
{ |
| 389 |
struct resource *resource, *tail; |
| 390 |
|
| 391 |
|
| 392 |
compact_resources(dev); |
| 393 |
|
| 394 |
|
| 395 |
resource = probe_resource(dev, index); |
| 396 |
if (!resource) { |
| 397 |
if (free_resources == NULL && !allocate_more_resources()) |
| 398 |
die("Couldn't allocate more resources."); |
| 399 |
|
| 400 |
resource = free_resources; |
| 401 |
free_resources = free_resources->next; |
| 402 |
memset(resource, 0, sizeof(*resource)); |
| 403 |
resource->next = NULL; |
| 404 |
tail = dev->resource_list; |
| 405 |
if (tail) { |
| 406 |
while (tail->next) |
| 407 |
tail = tail->next; |
| 408 |
tail->next = resource; |
| 409 |
} else { |
| 410 |
dev->resource_list = resource; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
if (!(resource->flags & IORESOURCE_FIXED)) { |
| 416 |
resource->flags = 0; |
| 417 |
resource->base = 0; |
| 418 |
} |
| 419 |
resource->size = 0; |
| 420 |
resource->limit = 0; |
| 421 |
resource->index = index; |
| 422 |
resource->align = 0; |
| 423 |
resource->gran = 0; |
| 424 |
|
| 425 |
return resource; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
struct resource *find_resource(const struct device *dev, unsigned int index) |
| 436 |
{ |
| 437 |
struct resource *resource; |
| 438 |
|
| 439 |
|
| 440 |
resource = probe_resource(dev, index); |
| 441 |
if (!resource) |
| 442 |
die("%s missing resource: %02x\n", dev_path(dev), index); |
| 443 |
return resource; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
static resource_t align_up(resource_t val, unsigned long gran) |
| 454 |
{ |
| 455 |
resource_t mask; |
| 456 |
mask = (1ULL << gran) - 1ULL; |
| 457 |
val += mask; |
| 458 |
val &= ~mask; |
| 459 |
return val; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
static resource_t align_down(resource_t val, unsigned long gran) |
| 470 |
{ |
| 471 |
resource_t mask; |
| 472 |
mask = (1ULL << gran) - 1ULL; |
| 473 |
val &= ~mask; |
| 474 |
return val; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
resource_t resource_end(const struct resource *resource) |
| 484 |
{ |
| 485 |
resource_t base, end; |
| 486 |
|
| 487 |
|
| 488 |
base = resource->base; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
end = base + align_up(resource->size, resource->gran) - 1; |
| 499 |
|
| 500 |
return end; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
resource_t resource_max(const struct resource *resource) |
| 510 |
{ |
| 511 |
resource_t max; |
| 512 |
|
| 513 |
max = align_down(resource->limit - resource->size + 1, resource->align); |
| 514 |
|
| 515 |
return max; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
const char *resource_type(const struct resource *resource) |
| 525 |
{ |
| 526 |
static char buffer[RESOURCE_TYPE_MAX]; |
| 527 |
snprintf(buffer, sizeof(buffer), "%s%s%s%s", |
| 528 |
((resource->flags & IORESOURCE_READONLY) ? "ro" : ""), |
| 529 |
((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""), |
| 530 |
((resource->flags == 0) ? "unused" : |
| 531 |
(resource->flags & IORESOURCE_IO) ? "io" : |
| 532 |
(resource->flags & IORESOURCE_DRQ) ? "drq" : |
| 533 |
(resource->flags & IORESOURCE_IRQ) ? "irq" : |
| 534 |
(resource->flags & IORESOURCE_MEM) ? "mem" : "??????"), |
| 535 |
((resource->flags & IORESOURCE_PCI64) ? "64" : "")); |
| 536 |
return buffer; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
void report_resource_stored(struct device *dev, const struct resource *resource, |
| 547 |
const char *comment) |
| 548 |
{ |
| 549 |
char buf[16]; |
| 550 |
unsigned long long base, end; |
| 551 |
|
| 552 |
if (!(resource->flags & IORESOURCE_STORED)) |
| 553 |
return; |
| 554 |
|
| 555 |
base = resource->base; |
| 556 |
end = resource_end(resource); |
| 557 |
buf[0] = '\0'; |
| 558 |
|
| 559 |
if (dev->downstream && (resource->flags & IORESOURCE_PCI_BRIDGE)) { |
| 560 |
snprintf(buf, sizeof(buf), |
| 561 |
"seg %02x bus %02x ", dev->downstream->segment_group, |
| 562 |
dev->downstream->secondary); |
| 563 |
} |
| 564 |
printk(BIOS_DEBUG, "%s %02lx <- [0x%016llx - 0x%016llx] size 0x%08llx " |
| 565 |
"gran 0x%02x %s%s%s\n", dev_path(dev), resource->index, |
| 566 |
base, end, resource->size, resource->gran, buf, |
| 567 |
resource_type(resource), comment); |
| 568 |
} |
| 569 |
|
| 570 |
void search_bus_resources(struct bus *bus, unsigned long type_mask, |
| 571 |
unsigned long type, resource_search_t search, |
| 572 |
void *gp) |
| 573 |
{ |
| 574 |
struct device *curdev; |
| 575 |
|
| 576 |
for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
| 577 |
struct resource *res; |
| 578 |
|
| 579 |
|
| 580 |
if (!curdev->enabled) |
| 581 |
continue; |
| 582 |
|
| 583 |
for (res = curdev->resource_list; res; res = res->next) { |
| 584 |
|
| 585 |
if ((res->flags & type_mask) != type) |
| 586 |
continue; |
| 587 |
|
| 588 |
|
| 589 |
if (res->flags & IORESOURCE_SUBTRACTIVE) { |
| 590 |
if (curdev->downstream) |
| 591 |
search_bus_resources(curdev->downstream, type_mask, type, |
| 592 |
search, gp); |
| 593 |
continue; |
| 594 |
} |
| 595 |
search(gp, curdev, res); |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
void search_global_resources(unsigned long type_mask, unsigned long type, |
| 601 |
resource_search_t search, void *gp) |
| 602 |
{ |
| 603 |
struct device *curdev; |
| 604 |
|
| 605 |
for (curdev = all_devices; curdev; curdev = curdev->next) { |
| 606 |
struct resource *res; |
| 607 |
|
| 608 |
|
| 609 |
if (!curdev->enabled) |
| 610 |
continue; |
| 611 |
|
| 612 |
for (res = curdev->resource_list; res; res = res->next) { |
| 613 |
|
| 614 |
if ((res->flags & type_mask) != type) |
| 615 |
continue; |
| 616 |
|
| 617 |
|
| 618 |
if (res->flags & IORESOURCE_SUBTRACTIVE) |
| 619 |
continue; |
| 620 |
|
| 621 |
|
| 622 |
if (!(res->flags & IORESOURCE_ASSIGNED)) |
| 623 |
continue; |
| 624 |
|
| 625 |
search(gp, curdev, res); |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
void dev_set_enabled(struct device *dev, int enable) |
| 631 |
{ |
| 632 |
if (dev->enabled == enable) |
| 633 |
return; |
| 634 |
|
| 635 |
dev->enabled = enable; |
| 636 |
if (dev->ops && dev->ops->enable) |
| 637 |
dev->ops->enable(dev); |
| 638 |
else if (dev->chip_ops && dev->chip_ops->enable_dev) |
| 639 |
dev->chip_ops->enable_dev(dev); |
| 640 |
} |
| 641 |
|
| 642 |
void disable_children(struct bus *bus) |
| 643 |
{ |
| 644 |
struct device *child; |
| 645 |
|
| 646 |
for (child = bus->children; child; child = child->sibling) { |
| 647 |
if (child->downstream) |
| 648 |
disable_children(child->downstream); |
| 649 |
dev_set_enabled(child, 0); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
bool dev_is_active_bridge(const struct device *dev) |
| 658 |
{ |
| 659 |
struct device *child; |
| 660 |
|
| 661 |
if (!dev || !dev->enabled) |
| 662 |
return 0; |
| 663 |
|
| 664 |
if (!dev->downstream || !dev->downstream->children) |
| 665 |
return 0; |
| 666 |
|
| 667 |
for (child = dev->downstream->children; child; child = child->sibling) { |
| 668 |
if (child->path.type == DEVICE_PATH_NONE) |
| 669 |
continue; |
| 670 |
if (child->enabled) |
| 671 |
return 1; |
| 672 |
} |
| 673 |
|
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
static void resource_tree(const struct device *root, int debug_level, int depth) |
| 678 |
{ |
| 679 |
int i = 0; |
| 680 |
struct device *child; |
| 681 |
struct resource *res; |
| 682 |
char indent[30]; |
| 683 |
|
| 684 |
for (i = 0; i < depth + 1 && i < 29; i++) |
| 685 |
indent[i] = ' '; |
| 686 |
indent[i] = '\0'; |
| 687 |
|
| 688 |
printk(BIOS_DEBUG, "%s%s", indent, dev_path(root)); |
| 689 |
if (root->downstream && root->downstream->children) |
| 690 |
printk(BIOS_DEBUG, " child on link 0 %s", |
| 691 |
dev_path(root->downstream->children)); |
| 692 |
printk(BIOS_DEBUG, "\n"); |
| 693 |
|
| 694 |
for (res = root->resource_list; res; res = res->next) { |
| 695 |
printk(debug_level, "%s%s resource base %llx size %llx " |
| 696 |
"align %d gran %d limit %llx flags %lx index %lx\n", |
| 697 |
indent, dev_path(root), res->base, res->size, |
| 698 |
res->align, res->gran, res->limit, res->flags, |
| 699 |
res->index); |
| 700 |
} |
| 701 |
|
| 702 |
if (!root->downstream) |
| 703 |
return; |
| 704 |
|
| 705 |
for (child = root->downstream->children; child; child = child->sibling) |
| 706 |
resource_tree(child, debug_level, depth + 1); |
| 707 |
} |
| 708 |
|
| 709 |
void print_resource_tree(const struct device *root, int debug_level, |
| 710 |
const char *msg) |
| 711 |
{ |
| 712 |
|
| 713 |
if (!root) { |
| 714 |
printk(debug_level, "%s passed NULL for root!\n", __func__); |
| 715 |
return; |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
if (!printk(debug_level, "Show resources in subtree (%s)...%s\n", |
| 720 |
dev_path(root), msg)) |
| 721 |
return; |
| 722 |
|
| 723 |
resource_tree(root, debug_level, 0); |
| 724 |
} |
| 725 |
|
| 726 |
void show_devs_tree(const struct device *dev, int debug_level, int depth) |
| 727 |
{ |
| 728 |
char depth_str[20]; |
| 729 |
int i; |
| 730 |
struct device *sibling; |
| 731 |
|
| 732 |
for (i = 0; i < depth; i++) |
| 733 |
depth_str[i] = ' '; |
| 734 |
depth_str[i] = '\0'; |
| 735 |
|
| 736 |
printk(debug_level, "%s%s: enabled %d\n", |
| 737 |
depth_str, dev_path(dev), dev->enabled); |
| 738 |
|
| 739 |
if (!dev->downstream) |
| 740 |
return; |
| 741 |
|
| 742 |
for (sibling = dev->downstream->children; sibling; sibling = sibling->sibling) |
| 743 |
show_devs_tree(sibling, debug_level, depth + 1); |
| 744 |
} |
| 745 |
|
| 746 |
void show_all_devs_tree(int debug_level, const char *msg) |
| 747 |
{ |
| 748 |
|
| 749 |
if (!printk(debug_level, "Show all devs in tree form... %s\n", msg)) |
| 750 |
return; |
| 751 |
show_devs_tree(all_devices, debug_level, 0); |
| 752 |
} |
| 753 |
|
| 754 |
void show_devs_subtree(struct device *root, int debug_level, const char *msg) |
| 755 |
{ |
| 756 |
|
| 757 |
if (!printk(debug_level, "Show all devs in subtree %s... %s\n", |
| 758 |
dev_path(root), msg)) |
| 759 |
return; |
| 760 |
printk(debug_level, "%s\n", msg); |
| 761 |
show_devs_tree(root, debug_level, 0); |
| 762 |
} |
| 763 |
|
| 764 |
void show_all_devs(int debug_level, const char *msg) |
| 765 |
{ |
| 766 |
struct device *dev; |
| 767 |
|
| 768 |
|
| 769 |
if (!printk(debug_level, "Show all devs... %s\n", msg)) |
| 770 |
return; |
| 771 |
for (dev = all_devices; dev; dev = dev->next) { |
| 772 |
printk(debug_level, "%s: enabled %d\n", |
| 773 |
dev_path(dev), dev->enabled); |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
void show_one_resource(int debug_level, struct device *dev, |
| 778 |
struct resource *resource, const char *comment) |
| 779 |
{ |
| 780 |
char buf[10]; |
| 781 |
unsigned long long base, end; |
| 782 |
base = resource->base; |
| 783 |
end = resource_end(resource); |
| 784 |
buf[0] = '\0'; |
| 785 |
|
| 786 |
printk(debug_level, "%s %02lx <- [0x%016llx - 0x%016llx] " |
| 787 |
"size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev), |
| 788 |
resource->index, base, end, resource->size, resource->gran, |
| 789 |
buf, resource_type(resource), comment); |
| 790 |
} |
| 791 |
|
| 792 |
void show_all_devs_resources(int debug_level, const char *msg) |
| 793 |
{ |
| 794 |
struct device *dev; |
| 795 |
|
| 796 |
if (!printk(debug_level, "Show all devs with resources... %s\n", msg)) |
| 797 |
return; |
| 798 |
|
| 799 |
for (dev = all_devices; dev; dev = dev->next) { |
| 800 |
struct resource *res; |
| 801 |
printk(debug_level, "%s: enabled %d\n", |
| 802 |
dev_path(dev), dev->enabled); |
| 803 |
for (res = dev->resource_list; res; res = res->next) |
| 804 |
show_one_resource(debug_level, dev, res, ""); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
const struct resource *resource_range_idx(struct device *dev, unsigned long index, |
| 809 |
uint64_t base, uint64_t size, unsigned long flags) |
| 810 |
{ |
| 811 |
struct resource *resource; |
| 812 |
if (!size) |
| 813 |
return NULL; |
| 814 |
|
| 815 |
resource = new_resource(dev, index); |
| 816 |
resource->base = base; |
| 817 |
|
| 818 |
if (flags & IORESOURCE_FIXED) |
| 819 |
resource->size = size; |
| 820 |
if (flags & IORESOURCE_BRIDGE) |
| 821 |
resource->limit = base + size - 1; |
| 822 |
|
| 823 |
resource->flags = IORESOURCE_ASSIGNED; |
| 824 |
resource->flags |= flags; |
| 825 |
|
| 826 |
printk(BIOS_SPEW, "dev: %s, index: 0x%lx, base: 0x%llx, size: 0x%llx\n", |
| 827 |
dev_path(dev), resource->index, resource->base, resource->size); |
| 828 |
|
| 829 |
return resource; |
| 830 |
} |
| 831 |
|
| 832 |
const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end) |
| 833 |
{ |
| 834 |
return ram_from_to(dev, index, 0, end); |
| 835 |
} |
| 836 |
|
| 837 |
const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end) |
| 838 |
{ |
| 839 |
if (end <= 4ull * GiB) |
| 840 |
return NULL; |
| 841 |
|
| 842 |
printk(BIOS_INFO, "Available memory above 4GB: %lluM\n", (end - 4ull * GiB) / MiB); |
| 843 |
|
| 844 |
return ram_from_to(dev, index, 4ull * GiB, end); |
| 845 |
} |
| 846 |
|
| 847 |
void mmconf_resource(struct device *dev, unsigned long index) |
| 848 |
{ |
| 849 |
struct resource *resource = new_resource(dev, index); |
| 850 |
resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS; |
| 851 |
resource->size = CONFIG_ECAM_MMCONF_LENGTH; |
| 852 |
resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE | |
| 853 |
IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED; |
| 854 |
|
| 855 |
printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n", |
| 856 |
(unsigned long)(resource->base), |
| 857 |
(unsigned long)(resource->base + resource->size)); |
| 858 |
} |
| 859 |
|
| 860 |
void tolm_test(void *gp, struct device *dev, struct resource *new) |
| 861 |
{ |
| 862 |
struct resource **best_p = gp; |
| 863 |
struct resource *best; |
| 864 |
|
| 865 |
best = *best_p; |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
if (new->size == 0) |
| 872 |
return; |
| 873 |
|
| 874 |
if (!best || (best->base > new->base)) |
| 875 |
best = new; |
| 876 |
|
| 877 |
*best_p = best; |
| 878 |
} |
| 879 |
|
| 880 |
u32 find_pci_tolm(struct bus *bus) |
| 881 |
{ |
| 882 |
struct resource *min = NULL; |
| 883 |
u32 tolm; |
| 884 |
unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED; |
| 885 |
|
| 886 |
search_bus_resources(bus, mask_match, mask_match, tolm_test, &min); |
| 887 |
|
| 888 |
tolm = 0xffffffffUL; |
| 889 |
|
| 890 |
if (min && tolm > min->base) |
| 891 |
tolm = min->base; |
| 892 |
|
| 893 |
return tolm; |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
int dev_count_cpu(void) |
| 898 |
{ |
| 899 |
struct device *cpu; |
| 900 |
int count = 0; |
| 901 |
|
| 902 |
for (cpu = all_devices; cpu; cpu = cpu->next) { |
| 903 |
if (!is_enabled_cpu(cpu)) |
| 904 |
continue; |
| 905 |
count++; |
| 906 |
} |
| 907 |
|
| 908 |
return count; |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
const char *dev_path_name(enum device_path_type type) |
| 913 |
{ |
| 914 |
static const char *const type_names[] = DEVICE_PATH_NAMES; |
| 915 |
const char *type_name = "Unknown"; |
| 916 |
|
| 917 |
|
| 918 |
if (type < ARRAY_SIZE(type_names)) |
| 919 |
type_name = type_names[type]; |
| 920 |
return type_name; |
| 921 |
} |
| 922 |
|
| 923 |
bool dev_path_hotplug(const struct device *dev) |
| 924 |
{ |
| 925 |
for (dev = dev->upstream->dev; dev != dev->upstream->dev; dev = dev->upstream->dev) { |
| 926 |
if (dev->hotplug_port) |
| 927 |
return true; |
| 928 |
} |
| 929 |
return false; |
| 930 |
} |
| 931 |
|
| 932 |
void log_resource(const char *type, const struct device *dev, const struct resource *res, |
| 933 |
const char *srcfile, const int line) |
| 934 |
{ |
| 935 |
printk(BIOS_SPEW, "%s:%d res: %s, dev: %s, index: 0x%lx, base: 0x%llx, " |
| 936 |
"end: 0x%llx, size_kb: 0x%llx\n", |
| 937 |
srcfile, line, type, dev_path(dev), res->index, res->base, |
| 938 |
resource_end(res), res->size / KiB); |
| 939 |
} |
| 940 |
|
| 941 |
bool is_cpu(const struct device *cpu) |
| 942 |
{ |
| 943 |
return cpu->path.type == DEVICE_PATH_APIC && |
| 944 |
cpu->upstream->dev->path.type == DEVICE_PATH_CPU_CLUSTER; |
| 945 |
} |
| 946 |
|
| 947 |
bool is_enabled_cpu(const struct device *cpu) |
| 948 |
{ |
| 949 |
return is_cpu(cpu) && cpu->enabled; |
| 950 |
} |
| 951 |
|
| 952 |
bool is_pci(const struct device *pci) |
| 953 |
{ |
| 954 |
return pci->path.type == DEVICE_PATH_PCI; |
| 955 |
} |
| 956 |
|
| 957 |
bool is_enabled_pci(const struct device *pci) |
| 958 |
{ |
| 959 |
return is_pci(pci) && pci->enabled; |
| 960 |
} |
| 961 |
|
| 962 |
bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus) |
| 963 |
{ |
| 964 |
return is_pci(pci) && pci->upstream->segment_group == 0 |
| 965 |
&& pci->upstream->secondary == bus; |
| 966 |
} |
| 967 |
|
| 968 |
bool is_pci_bridge(const struct device *pci) |
| 969 |
{ |
| 970 |
return is_pci(pci) && ((pci->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE); |
| 971 |
} |
| 972 |
|
| 973 |
bool is_pci_ioapic(const struct device *pci) |
| 974 |
{ |
| 975 |
return is_pci(pci) && ((pci->class >> 16) == PCI_BASE_CLASS_SYSTEM) && |
| 976 |
((pci->class >> 8) == PCI_CLASS_SYSTEM_PIC) && |
| 977 |
((pci->class & 0xff) >= 0x10); |
| 978 |
} |