| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <console/console.h> |
| 8 |
#include <device/device.h> |
| 9 |
#include <device/pci_def.h> |
| 10 |
#include <device/pci_ids.h> |
| 11 |
#include <post.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
#include <smp/spinlock.h> |
| 15 |
#include <timer.h> |
| 16 |
|
| 17 |
|
| 18 |
extern struct device *last_dev; |
| 19 |
|
| 20 |
struct resource *free_resources = NULL; |
| 21 |
|
| 22 |
void devfn_disable(const struct bus *bus, unsigned int devfn) |
| 23 |
{ |
| 24 |
struct device *dev = pcidev_path_behind(bus, devfn); |
| 25 |
if (dev) |
| 26 |
dev->enabled = 0; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
void dev_initialize_chips(void) |
| 36 |
{ |
| 37 |
const struct device *dev; |
| 38 |
|
| 39 |
for (dev = all_devices; dev; dev = dev->next) { |
| 40 |
|
| 41 |
if (dev->chip_ops && dev->chip_ops->init && |
| 42 |
!dev->chip_ops->initialized) { |
| 43 |
post_log_path(dev); |
| 44 |
dev->chip_ops->init(dev->chip_info); |
| 45 |
dev->chip_ops->initialized = 1; |
| 46 |
} |
| 47 |
} |
| 48 |
post_log_clear(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
void dev_finalize_chips(void) |
| 58 |
{ |
| 59 |
const struct device *dev; |
| 60 |
|
| 61 |
for (dev = all_devices; dev; dev = dev->next) { |
| 62 |
|
| 63 |
if (dev->chip_ops && dev->chip_ops->final && |
| 64 |
!dev->chip_ops->finalized) { |
| 65 |
dev->chip_ops->final(dev->chip_info); |
| 66 |
dev->chip_ops->finalized = 1; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
DECLARE_SPIN_LOCK(dev_lock) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static struct device *__alloc_dev(struct bus *parent, struct device_path *path) |
| 86 |
{ |
| 87 |
struct device *dev, *child; |
| 88 |
|
| 89 |
|
| 90 |
for (child = parent->children; child && child->sibling; ) |
| 91 |
child = child->sibling; |
| 92 |
|
| 93 |
dev = malloc(sizeof(*dev)); |
| 94 |
if (dev == 0) |
| 95 |
die("alloc_dev(): out of memory.\n"); |
| 96 |
|
| 97 |
memset(dev, 0, sizeof(*dev)); |
| 98 |
memcpy(&dev->path, path, sizeof(*path)); |
| 99 |
|
| 100 |
|
| 101 |
dev->enabled = 1; |
| 102 |
|
| 103 |
|
| 104 |
dev->upstream = parent; |
| 105 |
if (child) |
| 106 |
child->sibling = dev; |
| 107 |
else |
| 108 |
parent->children = dev; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
last_dev->next = dev; |
| 114 |
last_dev = dev; |
| 115 |
|
| 116 |
return dev; |
| 117 |
} |
| 118 |
|
| 119 |
struct device *alloc_dev(struct bus *parent, struct device_path *path) |
| 120 |
{ |
| 121 |
struct device *dev; |
| 122 |
spin_lock(&dev_lock); |
| 123 |
dev = __alloc_dev(parent, path); |
| 124 |
spin_unlock(&dev_lock); |
| 125 |
return dev; |
| 126 |
} |
| 127 |
|
| 128 |
DECLARE_SPIN_LOCK(bus_lock) |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
static struct bus *__alloc_bus(struct device *parent) |
| 141 |
{ |
| 142 |
if (parent->downstream) |
| 143 |
return parent->downstream; |
| 144 |
|
| 145 |
struct bus *bus = calloc(1, sizeof(struct bus)); |
| 146 |
if (!bus) |
| 147 |
die("Couldn't allocate downstream bus!\n"); |
| 148 |
parent->downstream = bus; |
| 149 |
bus->dev = parent; |
| 150 |
|
| 151 |
return bus; |
| 152 |
} |
| 153 |
|
| 154 |
struct bus *alloc_bus(struct device *parent) |
| 155 |
{ |
| 156 |
struct bus *bus; |
| 157 |
spin_lock(&bus_lock); |
| 158 |
bus = __alloc_bus(parent); |
| 159 |
spin_unlock(&bus_lock); |
| 160 |
return bus; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
struct device *alloc_find_dev(struct bus *parent, struct device_path *path) |
| 171 |
{ |
| 172 |
struct device *child; |
| 173 |
spin_lock(&dev_lock); |
| 174 |
child = find_dev_path(parent, path); |
| 175 |
if (!child) |
| 176 |
child = __alloc_dev(parent, path); |
| 177 |
spin_unlock(&dev_lock); |
| 178 |
return child; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
static void read_resources(struct bus *bus) |
| 187 |
{ |
| 188 |
struct device *curdev; |
| 189 |
|
| 190 |
printk(BIOS_SPEW, "%s %s segment group %d bus %d\n", dev_path(bus->dev), |
| 191 |
__func__, bus->segment_group, bus->secondary); |
| 192 |
|
| 193 |
|
| 194 |
for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
| 195 |
if (!curdev->enabled) |
| 196 |
continue; |
| 197 |
|
| 198 |
if (!curdev->ops || !curdev->ops->read_resources) { |
| 199 |
if (curdev->path.type != DEVICE_PATH_APIC) |
| 200 |
printk(BIOS_ERR, "%s missing %s\n", |
| 201 |
dev_path(curdev), __func__); |
| 202 |
continue; |
| 203 |
} |
| 204 |
post_log_path(curdev); |
| 205 |
curdev->ops->read_resources(curdev); |
| 206 |
|
| 207 |
|
| 208 |
if (curdev->downstream) |
| 209 |
read_resources(curdev->downstream); |
| 210 |
} |
| 211 |
post_log_clear(); |
| 212 |
printk(BIOS_SPEW, "%s %s segment group %d bus %d done\n", |
| 213 |
dev_path(bus->dev), __func__, bus->segment_group, bus->secondary); |
| 214 |
} |
| 215 |
|
| 216 |
struct device *vga_pri = NULL; |
| 217 |
static void set_vga_bridge_bits(void) |
| 218 |
{ |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
struct device *dev, *vga, *vga_onboard; |
| 227 |
struct bus *bus; |
| 228 |
|
| 229 |
bus = 0; |
| 230 |
vga = 0; |
| 231 |
vga_onboard = 0; |
| 232 |
|
| 233 |
dev = NULL; |
| 234 |
while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) { |
| 235 |
if (!dev->enabled) |
| 236 |
continue; |
| 237 |
|
| 238 |
printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev)); |
| 239 |
if (dev->upstream->no_vga16) { |
| 240 |
printk(BIOS_WARNING, |
| 241 |
"A bridge on the path doesn't support 16-bit VGA decoding!"); |
| 242 |
} |
| 243 |
|
| 244 |
if (dev->on_mainboard) |
| 245 |
vga_onboard = dev; |
| 246 |
else |
| 247 |
vga = dev; |
| 248 |
|
| 249 |
|
| 250 |
dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
| 251 |
} |
| 252 |
|
| 253 |
if (!vga) |
| 254 |
vga = vga_onboard; |
| 255 |
|
| 256 |
if (CONFIG(ONBOARD_VGA_IS_PRIMARY) && vga_onboard) |
| 257 |
vga = vga_onboard; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
if (!CONFIG(ONBOARD_VGA_IS_PRIMARY) && (vga != vga_onboard) && |
| 262 |
vga_onboard && vga_onboard->ops && vga_onboard->ops->vga_disable) { |
| 263 |
printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n"); |
| 264 |
vga_onboard->ops->vga_disable(vga_onboard); |
| 265 |
} |
| 266 |
|
| 267 |
if (vga) { |
| 268 |
|
| 269 |
printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga)); |
| 270 |
|
| 271 |
vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
| 272 |
vga_pri = vga; |
| 273 |
bus = vga->upstream; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
while (bus) { |
| 278 |
printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n", |
| 279 |
dev_path(bus->dev)); |
| 280 |
bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA | PCI_BRIDGE_CTL_VGA16; |
| 281 |
bus = (bus == bus->dev->upstream) ? 0 : bus->dev->upstream; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
void assign_resources(struct bus *bus) |
| 299 |
{ |
| 300 |
struct device *curdev; |
| 301 |
|
| 302 |
printk(BIOS_SPEW, "%s %s, segment group %d bus %d\n", |
| 303 |
dev_path(bus->dev), __func__, bus->segment_group, bus->secondary); |
| 304 |
|
| 305 |
for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
| 306 |
if (!curdev->enabled || !curdev->resource_list) |
| 307 |
continue; |
| 308 |
|
| 309 |
if (!curdev->ops || !curdev->ops->set_resources) { |
| 310 |
printk(BIOS_ERR, "%s missing set_resources\n", |
| 311 |
dev_path(curdev)); |
| 312 |
continue; |
| 313 |
} |
| 314 |
post_log_path(curdev); |
| 315 |
curdev->ops->set_resources(curdev); |
| 316 |
} |
| 317 |
post_log_clear(); |
| 318 |
printk(BIOS_SPEW, "%s %s, segment group %d bus %d done\n", |
| 319 |
dev_path(bus->dev), __func__, bus->segment_group, bus->secondary); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
static void enable_resources(struct bus *link) |
| 335 |
{ |
| 336 |
struct device *dev; |
| 337 |
|
| 338 |
for (dev = link->children; dev; dev = dev->sibling) { |
| 339 |
if (dev->enabled && dev->ops && dev->ops->enable_resources) { |
| 340 |
post_log_path(dev); |
| 341 |
dev->ops->enable_resources(dev); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
for (dev = link->children; dev; dev = dev->sibling) { |
| 346 |
if (dev->downstream) |
| 347 |
enable_resources(dev->downstream); |
| 348 |
} |
| 349 |
post_log_clear(); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
int reset_bus(struct bus *bus) |
| 359 |
{ |
| 360 |
if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) { |
| 361 |
bus->dev->ops->reset_bus(bus); |
| 362 |
bus->reset_needed = 0; |
| 363 |
return 1; |
| 364 |
} |
| 365 |
return 0; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
static void scan_bus(struct device *busdev) |
| 378 |
{ |
| 379 |
int do_scan_bus; |
| 380 |
struct stopwatch sw; |
| 381 |
long scan_time; |
| 382 |
|
| 383 |
if (!busdev->enabled) |
| 384 |
return; |
| 385 |
|
| 386 |
printk(BIOS_DEBUG, "%s scanning...\n", dev_path(busdev)); |
| 387 |
|
| 388 |
post_log_path(busdev); |
| 389 |
|
| 390 |
stopwatch_init(&sw); |
| 391 |
|
| 392 |
do_scan_bus = 1; |
| 393 |
while (do_scan_bus) { |
| 394 |
struct bus *link = busdev->downstream; |
| 395 |
busdev->ops->scan_bus(busdev); |
| 396 |
do_scan_bus = 0; |
| 397 |
if (!link || !link->reset_needed) |
| 398 |
continue; |
| 399 |
if (reset_bus(link)) |
| 400 |
do_scan_bus = 1; |
| 401 |
else |
| 402 |
busdev->upstream->reset_needed = 1; |
| 403 |
} |
| 404 |
|
| 405 |
scan_time = stopwatch_duration_msecs(&sw); |
| 406 |
printk(BIOS_DEBUG, "%s: bus %s finished in %ld msecs\n", __func__, |
| 407 |
dev_path(busdev), scan_time); |
| 408 |
} |
| 409 |
|
| 410 |
void scan_bridges(struct bus *bus) |
| 411 |
{ |
| 412 |
struct device *child; |
| 413 |
|
| 414 |
for (child = bus->children; child; child = child->sibling) { |
| 415 |
if (!child->ops || !child->ops->scan_bus) |
| 416 |
continue; |
| 417 |
scan_bus(child); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
void dev_enumerate(void) |
| 444 |
{ |
| 445 |
struct device *root; |
| 446 |
|
| 447 |
printk(BIOS_INFO, "Enumerating buses...\n"); |
| 448 |
|
| 449 |
root = &dev_root; |
| 450 |
|
| 451 |
show_all_devs(BIOS_SPEW, "Before device enumeration."); |
| 452 |
printk(BIOS_SPEW, "Compare with tree...\n"); |
| 453 |
show_devs_tree(root, BIOS_SPEW, 0); |
| 454 |
|
| 455 |
if (root->chip_ops && root->chip_ops->enable_dev) |
| 456 |
root->chip_ops->enable_dev(root); |
| 457 |
|
| 458 |
if (!root->ops || !root->ops->scan_bus) { |
| 459 |
printk(BIOS_ERR, "dev_root missing scan_bus operation"); |
| 460 |
return; |
| 461 |
} |
| 462 |
scan_bus(root); |
| 463 |
post_log_clear(); |
| 464 |
printk(BIOS_INFO, "done\n"); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
void dev_configure(void) |
| 481 |
{ |
| 482 |
const struct device *root; |
| 483 |
|
| 484 |
set_vga_bridge_bits(); |
| 485 |
|
| 486 |
printk(BIOS_INFO, "Allocating resources...\n"); |
| 487 |
|
| 488 |
root = &dev_root; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
printk(BIOS_INFO, "Reading resources...\n"); |
| 499 |
read_resources(root->downstream); |
| 500 |
printk(BIOS_INFO, "Done reading resources.\n"); |
| 501 |
|
| 502 |
print_resource_tree(root, BIOS_SPEW, "After reading."); |
| 503 |
|
| 504 |
allocate_resources(root); |
| 505 |
|
| 506 |
assign_resources(root->downstream); |
| 507 |
printk(BIOS_INFO, "Done setting resources.\n"); |
| 508 |
print_resource_tree(root, BIOS_SPEW, "After assigning values."); |
| 509 |
|
| 510 |
printk(BIOS_INFO, "Done allocating resources.\n"); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
void dev_enable(void) |
| 520 |
{ |
| 521 |
printk(BIOS_INFO, "Enabling resources...\n"); |
| 522 |
|
| 523 |
|
| 524 |
if (dev_root.downstream) |
| 525 |
enable_resources(dev_root.downstream); |
| 526 |
|
| 527 |
printk(BIOS_INFO, "done.\n"); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
static void init_dev(struct device *dev) |
| 540 |
{ |
| 541 |
if (!dev->enabled) |
| 542 |
return; |
| 543 |
|
| 544 |
if (!dev->initialized && dev->ops && dev->ops->init) { |
| 545 |
struct stopwatch sw; |
| 546 |
long init_time; |
| 547 |
|
| 548 |
if (dev->path.type == DEVICE_PATH_I2C) { |
| 549 |
printk(BIOS_DEBUG, "smbus: %s->", dev_path(dev->upstream->dev)); |
| 550 |
} |
| 551 |
|
| 552 |
printk(BIOS_DEBUG, "%s init\n", dev_path(dev)); |
| 553 |
|
| 554 |
stopwatch_init(&sw); |
| 555 |
dev->initialized = 1; |
| 556 |
dev->ops->init(dev); |
| 557 |
|
| 558 |
init_time = stopwatch_duration_msecs(&sw); |
| 559 |
printk(BIOS_DEBUG, "%s init finished in %ld msecs\n", dev_path(dev), |
| 560 |
init_time); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
static void init_link(struct bus *link) |
| 565 |
{ |
| 566 |
struct device *dev; |
| 567 |
|
| 568 |
for (dev = link->children; dev; dev = dev->sibling) { |
| 569 |
post_code(POSTCODE_BS_DEV_INIT); |
| 570 |
post_log_path(dev); |
| 571 |
init_dev(dev); |
| 572 |
} |
| 573 |
|
| 574 |
for (dev = link->children; dev; dev = dev->sibling) |
| 575 |
if (dev->downstream) |
| 576 |
init_link(dev->downstream); |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
void dev_initialize(void) |
| 586 |
{ |
| 587 |
printk(BIOS_INFO, "Initializing devices...\n"); |
| 588 |
|
| 589 |
|
| 590 |
init_dev(&dev_root); |
| 591 |
|
| 592 |
|
| 593 |
if (dev_root.downstream) |
| 594 |
init_link(dev_root.downstream); |
| 595 |
post_log_clear(); |
| 596 |
|
| 597 |
printk(BIOS_INFO, "Devices initialized\n"); |
| 598 |
show_all_devs(BIOS_SPEW, "After init."); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
static void final_dev(struct device *dev) |
| 611 |
{ |
| 612 |
if (!dev->enabled) |
| 613 |
return; |
| 614 |
|
| 615 |
if (dev->ops && dev->ops->final) { |
| 616 |
printk(BIOS_DEBUG, "%s final\n", dev_path(dev)); |
| 617 |
dev->ops->final(dev); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
static void final_link(struct bus *link) |
| 622 |
{ |
| 623 |
struct device *dev; |
| 624 |
|
| 625 |
for (dev = link->children; dev; dev = dev->sibling) |
| 626 |
final_dev(dev); |
| 627 |
|
| 628 |
for (dev = link->children; dev; dev = dev->sibling) |
| 629 |
if (dev->downstream) |
| 630 |
final_link(dev->downstream); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
void dev_finalize(void) |
| 639 |
{ |
| 640 |
printk(BIOS_INFO, "Finalize devices...\n"); |
| 641 |
|
| 642 |
|
| 643 |
final_dev(&dev_root); |
| 644 |
|
| 645 |
|
| 646 |
final_link(dev_root.downstream); |
| 647 |
|
| 648 |
printk(BIOS_INFO, "Devices finalized\n"); |
| 649 |
} |