1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (c) 2014 Google, Inc 4 * Written by Simon Glass <sjg@chromium.org> 5 */ 6 7 #include <common.h> 8 #include <dm.h> 9 #include <errno.h> 10 #include <pci.h> 11 #include <asm/io.h> 12 #include <dm/device-internal.h> 13 #include <dm/lists.h> 14 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP) 15 #include <asm/fsp/fsp_support.h> 16 #endif 17 #include "pci_internal.h" 18 19 DECLARE_GLOBAL_DATA_PTR; 20 21 int pci_get_bus(int busnum, struct udevice **busp) 22 { 23 int ret; 24 25 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp); 26 27 /* Since buses may not be numbered yet try a little harder with bus 0 */ 28 if (ret == -ENODEV) { 29 ret = uclass_first_device_err(UCLASS_PCI, busp); 30 if (ret) 31 return ret; 32 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp); 33 } 34 35 return ret; 36 } 37 38 struct udevice *pci_get_controller(struct udevice *dev) 39 { 40 while (device_is_on_pci_bus(dev)) 41 dev = dev->parent; 42 43 return dev; 44 } 45 46 pci_dev_t dm_pci_get_bdf(struct udevice *dev) 47 { 48 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev); 49 struct udevice *bus = dev->parent; 50 51 return PCI_ADD_BUS(bus->seq, pplat->devfn); 52 } 53 54 /** 55 * pci_get_bus_max() - returns the bus number of the last active bus 56 * 57 * @return last bus number, or -1 if no active buses 58 */ 59 static int pci_get_bus_max(void) 60 { 61 struct udevice *bus; 62 struct uclass *uc; 63 int ret = -1; 64 65 ret = uclass_get(UCLASS_PCI, &uc); 66 uclass_foreach_dev(bus, uc) { 67 if (bus->seq > ret) 68 ret = bus->seq; 69 } 70 71 debug("%s: ret=%d\n", __func__, ret); 72 73 return ret; 74 } 75 76 int pci_last_busno(void) 77 { 78 return pci_get_bus_max(); 79 } 80 81 int pci_get_ff(enum pci_size_t size) 82 { 83 switch (size) { 84 case PCI_SIZE_8: 85 return 0xff; 86 case PCI_SIZE_16: 87 return 0xffff; 88 default: 89 return 0xffffffff; 90 } 91 } 92 93 int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn, 94 struct udevice **devp) 95 { 96 struct udevice *dev; 97 98 for (device_find_first_child(bus, &dev); 99 dev; 100 device_find_next_child(&dev)) { 101 struct pci_child_platdata *pplat; 102 103 pplat = dev_get_parent_platdata(dev); 104 if (pplat && pplat->devfn == find_devfn) { 105 *devp = dev; 106 return 0; 107 } 108 } 109 110 return -ENODEV; 111 } 112 113 int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp) 114 { 115 struct udevice *bus; 116 int ret; 117 118 ret = pci_get_bus(PCI_BUS(bdf), &bus); 119 if (ret) 120 return ret; 121 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp); 122 } 123 124 static int pci_device_matches_ids(struct udevice *dev, 125 struct pci_device_id *ids) 126 { 127 struct pci_child_platdata *pplat; 128 int i; 129 130 pplat = dev_get_parent_platdata(dev); 131 if (!pplat) 132 return -EINVAL; 133 for (i = 0; ids[i].vendor != 0; i++) { 134 if (pplat->vendor == ids[i].vendor && 135 pplat->device == ids[i].device) 136 return i; 137 } 138 139 return -EINVAL; 140 } 141 142 int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids, 143 int *indexp, struct udevice **devp) 144 { 145 struct udevice *dev; 146 147 /* Scan all devices on this bus */ 148 for (device_find_first_child(bus, &dev); 149 dev; 150 device_find_next_child(&dev)) { 151 if (pci_device_matches_ids(dev, ids) >= 0) { 152 if ((*indexp)-- <= 0) { 153 *devp = dev; 154 return 0; 155 } 156 } 157 } 158 159 return -ENODEV; 160 } 161 162 int pci_find_device_id(struct pci_device_id *ids, int index, 163 struct udevice **devp) 164 { 165 struct udevice *bus; 166 167 /* Scan all known buses */ 168 for (uclass_first_device(UCLASS_PCI, &bus); 169 bus; 170 uclass_next_device(&bus)) { 171 if (!pci_bus_find_devices(bus, ids, &index, devp)) 172 return 0; 173 } 174 *devp = NULL; 175 176 return -ENODEV; 177 } 178 179 static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor, 180 unsigned int device, int *indexp, 181 struct udevice **devp) 182 { 183 struct pci_child_platdata *pplat; 184 struct udevice *dev; 185 186 for (device_find_first_child(bus, &dev); 187 dev; 188 device_find_next_child(&dev)) { 189 pplat = dev_get_parent_platdata(dev); 190 if (pplat->vendor == vendor && pplat->device == device) { 191 if (!(*indexp)--) { 192 *devp = dev; 193 return 0; 194 } 195 } 196 } 197 198 return -ENODEV; 199 } 200 201 int dm_pci_find_device(unsigned int vendor, unsigned int device, int index, 202 struct udevice **devp) 203 { 204 struct udevice *bus; 205 206 /* Scan all known buses */ 207 for (uclass_first_device(UCLASS_PCI, &bus); 208 bus; 209 uclass_next_device(&bus)) { 210 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp)) 211 return device_probe(*devp); 212 } 213 *devp = NULL; 214 215 return -ENODEV; 216 } 217 218 int dm_pci_find_class(uint find_class, int index, struct udevice **devp) 219 { 220 struct udevice *dev; 221 222 /* Scan all known buses */ 223 for (pci_find_first_device(&dev); 224 dev; 225 pci_find_next_device(&dev)) { 226 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev); 227 228 if (pplat->class == find_class && !index--) { 229 *devp = dev; 230 return device_probe(*devp); 231 } 232 } 233 *devp = NULL; 234 235 return -ENODEV; 236 } 237 238 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset, 239 unsigned long value, enum pci_size_t size) 240 { 241 struct dm_pci_ops *ops; 242 243 ops = pci_get_ops(bus); 244 if (!ops->write_config) 245 return -ENOSYS; 246 return ops->write_config(bus, bdf, offset, value, size); 247 } 248 249 int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset, 250 u32 clr, u32 set) 251 { 252 ulong val; 253 int ret; 254 255 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32); 256 if (ret) 257 return ret; 258 val &= ~clr; 259 val |= set; 260 261 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32); 262 } 263 264 int pci_write_config(pci_dev_t bdf, int offset, unsigned long value, 265 enum pci_size_t size) 266 { 267 struct udevice *bus; 268 int ret; 269 270 ret = pci_get_bus(PCI_BUS(bdf), &bus); 271 if (ret) 272 return ret; 273 274 return pci_bus_write_config(bus, bdf, offset, value, size); 275 } 276 277 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value, 278 enum pci_size_t size) 279 { 280 struct udevice *bus; 281 282 for (bus = dev; device_is_on_pci_bus(bus);) 283 bus = bus->parent; 284 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value, 285 size); 286 } 287 288 int pci_write_config32(pci_dev_t bdf, int offset, u32 value) 289 { 290 return pci_write_config(bdf, offset, value, PCI_SIZE_32); 291 } 292 293 int pci_write_config16(pci_dev_t bdf, int offset, u16 value) 294 { 295 return pci_write_config(bdf, offset, value, PCI_SIZE_16); 296 } 297 298 int pci_write_config8(pci_dev_t bdf, int offset, u8 value) 299 { 300 return pci_write_config(bdf, offset, value, PCI_SIZE_8); 301 } 302 303 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value) 304 { 305 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8); 306 } 307 308 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value) 309 { 310 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16); 311 } 312 313 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value) 314 { 315 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32); 316 } 317 318 int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset, 319 unsigned long *valuep, enum pci_size_t size) 320 { 321 struct dm_pci_ops *ops; 322 323 ops = pci_get_ops(bus); 324 if (!ops->read_config) 325 return -ENOSYS; 326 return ops->read_config(bus, bdf, offset, valuep, size); 327 } 328 329 int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep, 330 enum pci_size_t size) 331 { 332 struct udevice *bus; 333 int ret; 334 335 ret = pci_get_bus(PCI_BUS(bdf), &bus); 336 if (ret) 337 return ret; 338 339 return pci_bus_read_config(bus, bdf, offset, valuep, size); 340 } 341 342 int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep, 343 enum pci_size_t size) 344 { 345 struct udevice *bus; 346 347 for (bus = dev; device_is_on_pci_bus(bus);) 348 bus = bus->parent; 349 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep, 350 size); 351 } 352 353 int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep) 354 { 355 unsigned long value; 356 int ret; 357 358 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32); 359 if (ret) 360 return ret; 361 *valuep = value; 362 363 return 0; 364 } 365 366 int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep) 367 { 368 unsigned long value; 369 int ret; 370 371 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16); 372 if (ret) 373 return ret; 374 *valuep = value; 375 376 return 0; 377 } 378 379 int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep) 380 { 381 unsigned long value; 382 int ret; 383 384 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8); 385 if (ret) 386 return ret; 387 *valuep = value; 388 389 return 0; 390 } 391 392 int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep) 393 { 394 unsigned long value; 395 int ret; 396 397 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8); 398 if (ret) 399 return ret; 400 *valuep = value; 401 402 return 0; 403 } 404 405 int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep) 406 { 407 unsigned long value; 408 int ret; 409 410 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16); 411 if (ret) 412 return ret; 413 *valuep = value; 414 415 return 0; 416 } 417 418 int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep) 419 { 420 unsigned long value; 421 int ret; 422 423 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32); 424 if (ret) 425 return ret; 426 *valuep = value; 427 428 return 0; 429 } 430 431 int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set) 432 { 433 u8 val; 434 int ret; 435 436 ret = dm_pci_read_config8(dev, offset, &val); 437 if (ret) 438 return ret; 439 val &= ~clr; 440 val |= set; 441 442 return dm_pci_write_config8(dev, offset, val); 443 } 444 445 int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set) 446 { 447 u16 val; 448 int ret; 449 450 ret = dm_pci_read_config16(dev, offset, &val); 451 if (ret) 452 return ret; 453 val &= ~clr; 454 val |= set; 455 456 return dm_pci_write_config16(dev, offset, val); 457 } 458 459 int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set) 460 { 461 u32 val; 462 int ret; 463 464 ret = dm_pci_read_config32(dev, offset, &val); 465 if (ret) 466 return ret; 467 val &= ~clr; 468 val |= set; 469 470 return dm_pci_write_config32(dev, offset, val); 471 } 472 473 static void set_vga_bridge_bits(struct udevice *dev) 474 { 475 struct udevice *parent = dev->parent; 476 u16 bc; 477 478 while (parent->seq != 0) { 479 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc); 480 bc |= PCI_BRIDGE_CTL_VGA; 481 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc); 482 parent = parent->parent; 483 } 484 } 485 486 int pci_auto_config_devices(struct udevice *bus) 487 { 488 struct pci_controller *hose = bus->uclass_priv; 489 struct pci_child_platdata *pplat; 490 unsigned int sub_bus; 491 struct udevice *dev; 492 int ret; 493 494 sub_bus = bus->seq; 495 debug("%s: start\n", __func__); 496 pciauto_config_init(hose); 497 for (ret = device_find_first_child(bus, &dev); 498 !ret && dev; 499 ret = device_find_next_child(&dev)) { 500 unsigned int max_bus; 501 int ret; 502 503 debug("%s: device %s\n", __func__, dev->name); 504 ret = dm_pciauto_config_device(dev); 505 if (ret < 0) 506 return ret; 507 max_bus = ret; 508 sub_bus = max(sub_bus, max_bus); 509 510 pplat = dev_get_parent_platdata(dev); 511 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8)) 512 set_vga_bridge_bits(dev); 513 } 514 debug("%s: done\n", __func__); 515 516 return sub_bus; 517 } 518 519 int pci_generic_mmap_write_config( 520 struct udevice *bus, 521 int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp), 522 pci_dev_t bdf, 523 uint offset, 524 ulong value, 525 enum pci_size_t size) 526 { 527 void *address; 528 529 if (addr_f(bus, bdf, offset, &address) < 0) 530 return 0; 531 532 switch (size) { 533 case PCI_SIZE_8: 534 writeb(value, address); 535 return 0; 536 case PCI_SIZE_16: 537 writew(value, address); 538 return 0; 539 case PCI_SIZE_32: 540 writel(value, address); 541 return 0; 542 default: 543 return -EINVAL; 544 } 545 } 546 547 int pci_generic_mmap_read_config( 548 struct udevice *bus, 549 int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp), 550 pci_dev_t bdf, 551 uint offset, 552 ulong *valuep, 553 enum pci_size_t size) 554 { 555 void *address; 556 557 if (addr_f(bus, bdf, offset, &address) < 0) { 558 *valuep = pci_get_ff(size); 559 return 0; 560 } 561 562 switch (size) { 563 case PCI_SIZE_8: 564 *valuep = readb(address); 565 return 0; 566 case PCI_SIZE_16: 567 *valuep = readw(address); 568 return 0; 569 case PCI_SIZE_32: 570 *valuep = readl(address); 571 return 0; 572 default: 573 return -EINVAL; 574 } 575 } 576 577 int dm_pci_hose_probe_bus(struct udevice *bus) 578 { 579 int sub_bus; 580 int ret; 581 582 debug("%s\n", __func__); 583 584 sub_bus = pci_get_bus_max() + 1; 585 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name); 586 dm_pciauto_prescan_setup_bridge(bus, sub_bus); 587 588 ret = device_probe(bus); 589 if (ret) { 590 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name, 591 ret); 592 return ret; 593 } 594 if (sub_bus != bus->seq) { 595 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n", 596 __func__, bus->name, bus->seq, sub_bus); 597 return -EPIPE; 598 } 599 sub_bus = pci_get_bus_max(); 600 dm_pciauto_postscan_setup_bridge(bus, sub_bus); 601 602 return sub_bus; 603 } 604 605 /** 606 * pci_match_one_device - Tell if a PCI device structure has a matching 607 * PCI device id structure 608 * @id: single PCI device id structure to match 609 * @find: the PCI device id structure to match against 610 * 611 * Returns true if the finding pci_device_id structure matched or false if 612 * there is no match. 613 */ 614 static bool pci_match_one_id(const struct pci_device_id *id, 615 const struct pci_device_id *find) 616 { 617 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) && 618 (id->device == PCI_ANY_ID || id->device == find->device) && 619 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) && 620 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) && 621 !((id->class ^ find->class) & id->class_mask)) 622 return true; 623 624 return false; 625 } 626 627 /** 628 * pci_find_and_bind_driver() - Find and bind the right PCI driver 629 * 630 * This only looks at certain fields in the descriptor. 631 * 632 * @parent: Parent bus 633 * @find_id: Specification of the driver to find 634 * @bdf: Bus/device/function addreess - see PCI_BDF() 635 * @devp: Returns a pointer to the device created 636 * @return 0 if OK, -EPERM if the device is not needed before relocation and 637 * therefore was not created, other -ve value on error 638 */ 639 static int pci_find_and_bind_driver(struct udevice *parent, 640 struct pci_device_id *find_id, 641 pci_dev_t bdf, struct udevice **devp) 642 { 643 struct pci_driver_entry *start, *entry; 644 const char *drv; 645 int n_ents; 646 int ret; 647 char name[30], *str; 648 bool bridge; 649 650 *devp = NULL; 651 652 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__, 653 find_id->vendor, find_id->device); 654 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry); 655 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry); 656 for (entry = start; entry != start + n_ents; entry++) { 657 const struct pci_device_id *id; 658 struct udevice *dev; 659 const struct driver *drv; 660 661 for (id = entry->match; 662 id->vendor || id->subvendor || id->class_mask; 663 id++) { 664 if (!pci_match_one_id(id, find_id)) 665 continue; 666 667 drv = entry->driver; 668 669 /* 670 * In the pre-relocation phase, we only bind devices 671 * whose driver has the DM_FLAG_PRE_RELOC set, to save 672 * precious memory space as on some platforms as that 673 * space is pretty limited (ie: using Cache As RAM). 674 */ 675 if (!(gd->flags & GD_FLG_RELOC) && 676 !(drv->flags & DM_FLAG_PRE_RELOC)) 677 return -EPERM; 678 679 /* 680 * We could pass the descriptor to the driver as 681 * platdata (instead of NULL) and allow its bind() 682 * method to return -ENOENT if it doesn't support this 683 * device. That way we could continue the search to 684 * find another driver. For now this doesn't seem 685 * necesssary, so just bind the first match. 686 */ 687 ret = device_bind(parent, drv, drv->name, NULL, -1, 688 &dev); 689 if (ret) 690 goto error; 691 debug("%s: Match found: %s\n", __func__, drv->name); 692 dev->driver_data = id->driver_data; 693 *devp = dev; 694 return 0; 695 } 696 } 697 698 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI; 699 /* 700 * In the pre-relocation phase, we only bind bridge devices to save 701 * precious memory space as on some platforms as that space is pretty 702 * limited (ie: using Cache As RAM). 703 */ 704 if (!(gd->flags & GD_FLG_RELOC) && !bridge) 705 return -EPERM; 706 707 /* Bind a generic driver so that the device can be used */ 708 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf), 709 PCI_FUNC(bdf)); 710 str = strdup(name); 711 if (!str) 712 return -ENOMEM; 713 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv"; 714 715 ret = device_bind_driver(parent, drv, str, devp); 716 if (ret) { 717 debug("%s: Failed to bind generic driver: %d\n", __func__, ret); 718 free(str); 719 return ret; 720 } 721 debug("%s: No match found: bound generic driver instead\n", __func__); 722 723 return 0; 724 725 error: 726 debug("%s: No match found: error %d\n", __func__, ret); 727 return ret; 728 } 729 730 int pci_bind_bus_devices(struct udevice *bus) 731 { 732 ulong vendor, device; 733 ulong header_type; 734 pci_dev_t bdf, end; 735 bool found_multi; 736 int ret; 737 738 found_multi = false; 739 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1, 740 PCI_MAX_PCI_FUNCTIONS - 1); 741 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end; 742 bdf += PCI_BDF(0, 0, 1)) { 743 struct pci_child_platdata *pplat; 744 struct udevice *dev; 745 ulong class; 746 747 if (!PCI_FUNC(bdf)) 748 found_multi = false; 749 if (PCI_FUNC(bdf) && !found_multi) 750 continue; 751 /* Check only the first access, we don't expect problems */ 752 ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE, 753 &header_type, PCI_SIZE_8); 754 if (ret) 755 goto error; 756 pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor, 757 PCI_SIZE_16); 758 if (vendor == 0xffff || vendor == 0x0000) 759 continue; 760 761 if (!PCI_FUNC(bdf)) 762 found_multi = header_type & 0x80; 763 764 debug("%s: bus %d/%s: found device %x, function %d\n", __func__, 765 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf)); 766 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device, 767 PCI_SIZE_16); 768 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class, 769 PCI_SIZE_32); 770 class >>= 8; 771 772 /* Find this device in the device tree */ 773 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev); 774 775 /* If nothing in the device tree, bind a device */ 776 if (ret == -ENODEV) { 777 struct pci_device_id find_id; 778 ulong val; 779 780 memset(&find_id, '\0', sizeof(find_id)); 781 find_id.vendor = vendor; 782 find_id.device = device; 783 find_id.class = class; 784 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) { 785 pci_bus_read_config(bus, bdf, 786 PCI_SUBSYSTEM_VENDOR_ID, 787 &val, PCI_SIZE_32); 788 find_id.subvendor = val & 0xffff; 789 find_id.subdevice = val >> 16; 790 } 791 ret = pci_find_and_bind_driver(bus, &find_id, bdf, 792 &dev); 793 } 794 if (ret == -EPERM) 795 continue; 796 else if (ret) 797 return ret; 798 799 /* Update the platform data */ 800 pplat = dev_get_parent_platdata(dev); 801 pplat->devfn = PCI_MASK_BUS(bdf); 802 pplat->vendor = vendor; 803 pplat->device = device; 804 pplat->class = class; 805 } 806 807 return 0; 808 error: 809 printf("Cannot read bus configuration: %d\n", ret); 810 811 return ret; 812 } 813 814 static void decode_regions(struct pci_controller *hose, ofnode parent_node, 815 ofnode node) 816 { 817 int pci_addr_cells, addr_cells, size_cells; 818 int cells_per_record; 819 const u32 *prop; 820 int len; 821 int i; 822 823 prop = ofnode_get_property(node, "ranges", &len); 824 if (!prop) { 825 debug("%s: Cannot decode regions\n", __func__); 826 return; 827 } 828 829 pci_addr_cells = ofnode_read_simple_addr_cells(node); 830 addr_cells = ofnode_read_simple_addr_cells(parent_node); 831 size_cells = ofnode_read_simple_size_cells(node); 832 833 /* PCI addresses are always 3-cells */ 834 len /= sizeof(u32); 835 cells_per_record = pci_addr_cells + addr_cells + size_cells; 836 hose->region_count = 0; 837 debug("%s: len=%d, cells_per_record=%d\n", __func__, len, 838 cells_per_record); 839 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) { 840 u64 pci_addr, addr, size; 841 int space_code; 842 u32 flags; 843 int type; 844 int pos; 845 846 if (len < cells_per_record) 847 break; 848 flags = fdt32_to_cpu(prop[0]); 849 space_code = (flags >> 24) & 3; 850 pci_addr = fdtdec_get_number(prop + 1, 2); 851 prop += pci_addr_cells; 852 addr = fdtdec_get_number(prop, addr_cells); 853 prop += addr_cells; 854 size = fdtdec_get_number(prop, size_cells); 855 prop += size_cells; 856 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n", 857 __func__, hose->region_count, pci_addr, addr, size, space_code); 858 if (space_code & 2) { 859 type = flags & (1U << 30) ? PCI_REGION_PREFETCH : 860 PCI_REGION_MEM; 861 } else if (space_code & 1) { 862 type = PCI_REGION_IO; 863 } else { 864 continue; 865 } 866 867 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) && 868 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) { 869 debug(" - beyond the 32-bit boundary, ignoring\n"); 870 continue; 871 } 872 873 pos = -1; 874 for (i = 0; i < hose->region_count; i++) { 875 if (hose->regions[i].flags == type) 876 pos = i; 877 } 878 if (pos == -1) 879 pos = hose->region_count++; 880 debug(" - type=%d, pos=%d\n", type, pos); 881 pci_set_region(hose->regions + pos, pci_addr, addr, size, type); 882 } 883 884 /* Add a region for our local memory */ 885 #ifdef CONFIG_NR_DRAM_BANKS 886 bd_t *bd = gd->bd; 887 888 if (!bd) 889 return; 890 891 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) { 892 if (bd->bi_dram[i].size) { 893 pci_set_region(hose->regions + hose->region_count++, 894 bd->bi_dram[i].start, 895 bd->bi_dram[i].start, 896 bd->bi_dram[i].size, 897 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); 898 } 899 } 900 #else 901 phys_addr_t base = 0, size; 902 903 size = gd->ram_size; 904 #ifdef CONFIG_SYS_SDRAM_BASE 905 base = CONFIG_SYS_SDRAM_BASE; 906 #endif 907 if (gd->pci_ram_top && gd->pci_ram_top < base + size) 908 size = gd->pci_ram_top - base; 909 if (size) 910 pci_set_region(hose->regions + hose->region_count++, base, 911 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); 912 #endif 913 914 return; 915 } 916 917 static int pci_uclass_pre_probe(struct udevice *bus) 918 { 919 struct pci_controller *hose; 920 921 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name, 922 bus->parent->name); 923 hose = bus->uclass_priv; 924 925 /* For bridges, use the top-level PCI controller */ 926 if (!device_is_on_pci_bus(bus)) { 927 hose->ctlr = bus; 928 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus)); 929 } else { 930 struct pci_controller *parent_hose; 931 932 parent_hose = dev_get_uclass_priv(bus->parent); 933 hose->ctlr = parent_hose->bus; 934 } 935 hose->bus = bus; 936 hose->first_busno = bus->seq; 937 hose->last_busno = bus->seq; 938 939 return 0; 940 } 941 942 static int pci_uclass_post_probe(struct udevice *bus) 943 { 944 int ret; 945 946 debug("%s: probing bus %d\n", __func__, bus->seq); 947 ret = pci_bind_bus_devices(bus); 948 if (ret) 949 return ret; 950 951 #ifdef CONFIG_PCI_PNP 952 ret = pci_auto_config_devices(bus); 953 if (ret < 0) 954 return ret; 955 #endif 956 957 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP) 958 /* 959 * Per Intel FSP specification, we should call FSP notify API to 960 * inform FSP that PCI enumeration has been done so that FSP will 961 * do any necessary initialization as required by the chipset's 962 * BIOS Writer's Guide (BWG). 963 * 964 * Unfortunately we have to put this call here as with driver model, 965 * the enumeration is all done on a lazy basis as needed, so until 966 * something is touched on PCI it won't happen. 967 * 968 * Note we only call this 1) after U-Boot is relocated, and 2) 969 * root bus has finished probing. 970 */ 971 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) { 972 ret = fsp_init_phase_pci(); 973 if (ret) 974 return ret; 975 } 976 #endif 977 978 return 0; 979 } 980 981 static int pci_uclass_child_post_bind(struct udevice *dev) 982 { 983 struct pci_child_platdata *pplat; 984 struct fdt_pci_addr addr; 985 int ret; 986 987 if (!dev_of_valid(dev)) 988 return 0; 989 990 pplat = dev_get_parent_platdata(dev); 991 992 /* Extract vendor id and device id if available */ 993 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device); 994 995 /* Extract the devfn from fdt_pci_addr */ 996 ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_CONFIG, "reg", 997 &addr); 998 if (ret) { 999 if (ret != -ENOENT) 1000 return -EINVAL; 1001 } else { 1002 pplat->devfn = addr.phys_hi & 0xff00; 1003 } 1004 1005 return 0; 1006 } 1007 1008 static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf, 1009 uint offset, ulong *valuep, 1010 enum pci_size_t size) 1011 { 1012 struct pci_controller *hose = bus->uclass_priv; 1013 1014 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size); 1015 } 1016 1017 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf, 1018 uint offset, ulong value, 1019 enum pci_size_t size) 1020 { 1021 struct pci_controller *hose = bus->uclass_priv; 1022 1023 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size); 1024 } 1025 1026 static int skip_to_next_device(struct udevice *bus, struct udevice **devp) 1027 { 1028 struct udevice *dev; 1029 int ret = 0; 1030 1031 /* 1032 * Scan through all the PCI controllers. On x86 there will only be one 1033 * but that is not necessarily true on other hardware. 1034 */ 1035 do { 1036 device_find_first_child(bus, &dev); 1037 if (dev) { 1038 *devp = dev; 1039 return 0; 1040 } 1041 ret = uclass_next_device(&bus); 1042 if (ret) 1043 return ret; 1044 } while (bus); 1045 1046 return 0; 1047 } 1048 1049 int pci_find_next_device(struct udevice **devp) 1050 { 1051 struct udevice *child = *devp; 1052 struct udevice *bus = child->parent; 1053 int ret; 1054 1055 /* First try all the siblings */ 1056 *devp = NULL; 1057 while (child) { 1058 device_find_next_child(&child); 1059 if (child) { 1060 *devp = child; 1061 return 0; 1062 } 1063 } 1064 1065 /* We ran out of siblings. Try the next bus */ 1066 ret = uclass_next_device(&bus); 1067 if (ret) 1068 return ret; 1069 1070 return bus ? skip_to_next_device(bus, devp) : 0; 1071 } 1072 1073 int pci_find_first_device(struct udevice **devp) 1074 { 1075 struct udevice *bus; 1076 int ret; 1077 1078 *devp = NULL; 1079 ret = uclass_first_device(UCLASS_PCI, &bus); 1080 if (ret) 1081 return ret; 1082 1083 return skip_to_next_device(bus, devp); 1084 } 1085 1086 ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size) 1087 { 1088 switch (size) { 1089 case PCI_SIZE_8: 1090 return (value >> ((offset & 3) * 8)) & 0xff; 1091 case PCI_SIZE_16: 1092 return (value >> ((offset & 2) * 8)) & 0xffff; 1093 default: 1094 return value; 1095 } 1096 } 1097 1098 ulong pci_conv_size_to_32(ulong old, ulong value, uint offset, 1099 enum pci_size_t size) 1100 { 1101 uint off_mask; 1102 uint val_mask, shift; 1103 ulong ldata, mask; 1104 1105 switch (size) { 1106 case PCI_SIZE_8: 1107 off_mask = 3; 1108 val_mask = 0xff; 1109 break; 1110 case PCI_SIZE_16: 1111 off_mask = 2; 1112 val_mask = 0xffff; 1113 break; 1114 default: 1115 return value; 1116 } 1117 shift = (offset & off_mask) * 8; 1118 ldata = (value & val_mask) << shift; 1119 mask = val_mask << shift; 1120 value = (old & ~mask) | ldata; 1121 1122 return value; 1123 } 1124 1125 int pci_get_regions(struct udevice *dev, struct pci_region **iop, 1126 struct pci_region **memp, struct pci_region **prefp) 1127 { 1128 struct udevice *bus = pci_get_controller(dev); 1129 struct pci_controller *hose = dev_get_uclass_priv(bus); 1130 int i; 1131 1132 *iop = NULL; 1133 *memp = NULL; 1134 *prefp = NULL; 1135 for (i = 0; i < hose->region_count; i++) { 1136 switch (hose->regions[i].flags) { 1137 case PCI_REGION_IO: 1138 if (!*iop || (*iop)->size < hose->regions[i].size) 1139 *iop = hose->regions + i; 1140 break; 1141 case PCI_REGION_MEM: 1142 if (!*memp || (*memp)->size < hose->regions[i].size) 1143 *memp = hose->regions + i; 1144 break; 1145 case (PCI_REGION_MEM | PCI_REGION_PREFETCH): 1146 if (!*prefp || (*prefp)->size < hose->regions[i].size) 1147 *prefp = hose->regions + i; 1148 break; 1149 } 1150 } 1151 1152 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL); 1153 } 1154 1155 u32 dm_pci_read_bar32(struct udevice *dev, int barnum) 1156 { 1157 u32 addr; 1158 int bar; 1159 1160 bar = PCI_BASE_ADDRESS_0 + barnum * 4; 1161 dm_pci_read_config32(dev, bar, &addr); 1162 if (addr & PCI_BASE_ADDRESS_SPACE_IO) 1163 return addr & PCI_BASE_ADDRESS_IO_MASK; 1164 else 1165 return addr & PCI_BASE_ADDRESS_MEM_MASK; 1166 } 1167 1168 void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr) 1169 { 1170 int bar; 1171 1172 bar = PCI_BASE_ADDRESS_0 + barnum * 4; 1173 dm_pci_write_config32(dev, bar, addr); 1174 } 1175 1176 static int _dm_pci_bus_to_phys(struct udevice *ctlr, 1177 pci_addr_t bus_addr, unsigned long flags, 1178 unsigned long skip_mask, phys_addr_t *pa) 1179 { 1180 struct pci_controller *hose = dev_get_uclass_priv(ctlr); 1181 struct pci_region *res; 1182 int i; 1183 1184 if (hose->region_count == 0) { 1185 *pa = bus_addr; 1186 return 0; 1187 } 1188 1189 for (i = 0; i < hose->region_count; i++) { 1190 res = &hose->regions[i]; 1191 1192 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) 1193 continue; 1194 1195 if (res->flags & skip_mask) 1196 continue; 1197 1198 if (bus_addr >= res->bus_start && 1199 (bus_addr - res->bus_start) < res->size) { 1200 *pa = (bus_addr - res->bus_start + res->phys_start); 1201 return 0; 1202 } 1203 } 1204 1205 return 1; 1206 } 1207 1208 phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr, 1209 unsigned long flags) 1210 { 1211 phys_addr_t phys_addr = 0; 1212 struct udevice *ctlr; 1213 int ret; 1214 1215 /* The root controller has the region information */ 1216 ctlr = pci_get_controller(dev); 1217 1218 /* 1219 * if PCI_REGION_MEM is set we do a two pass search with preference 1220 * on matches that don't have PCI_REGION_SYS_MEMORY set 1221 */ 1222 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) { 1223 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, 1224 flags, PCI_REGION_SYS_MEMORY, 1225 &phys_addr); 1226 if (!ret) 1227 return phys_addr; 1228 } 1229 1230 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr); 1231 1232 if (ret) 1233 puts("pci_hose_bus_to_phys: invalid physical address\n"); 1234 1235 return phys_addr; 1236 } 1237 1238 int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, 1239 unsigned long flags, unsigned long skip_mask, 1240 pci_addr_t *ba) 1241 { 1242 struct pci_region *res; 1243 struct udevice *ctlr; 1244 pci_addr_t bus_addr; 1245 int i; 1246 struct pci_controller *hose; 1247 1248 /* The root controller has the region information */ 1249 ctlr = pci_get_controller(dev); 1250 hose = dev_get_uclass_priv(ctlr); 1251 1252 if (hose->region_count == 0) { 1253 *ba = phys_addr; 1254 return 0; 1255 } 1256 1257 for (i = 0; i < hose->region_count; i++) { 1258 res = &hose->regions[i]; 1259 1260 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) 1261 continue; 1262 1263 if (res->flags & skip_mask) 1264 continue; 1265 1266 bus_addr = phys_addr - res->phys_start + res->bus_start; 1267 1268 if (bus_addr >= res->bus_start && 1269 (bus_addr - res->bus_start) < res->size) { 1270 *ba = bus_addr; 1271 return 0; 1272 } 1273 } 1274 1275 return 1; 1276 } 1277 1278 pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, 1279 unsigned long flags) 1280 { 1281 pci_addr_t bus_addr = 0; 1282 int ret; 1283 1284 /* 1285 * if PCI_REGION_MEM is set we do a two pass search with preference 1286 * on matches that don't have PCI_REGION_SYS_MEMORY set 1287 */ 1288 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) { 1289 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 1290 PCI_REGION_SYS_MEMORY, &bus_addr); 1291 if (!ret) 1292 return bus_addr; 1293 } 1294 1295 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr); 1296 1297 if (ret) 1298 puts("pci_hose_phys_to_bus: invalid physical address\n"); 1299 1300 return bus_addr; 1301 } 1302 1303 void *dm_pci_map_bar(struct udevice *dev, int bar, int flags) 1304 { 1305 pci_addr_t pci_bus_addr; 1306 u32 bar_response; 1307 1308 /* read BAR address */ 1309 dm_pci_read_config32(dev, bar, &bar_response); 1310 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf); 1311 1312 /* 1313 * Pass "0" as the length argument to pci_bus_to_virt. The arg 1314 * isn't actualy used on any platform because u-boot assumes a static 1315 * linear mapping. In the future, this could read the BAR size 1316 * and pass that as the size if needed. 1317 */ 1318 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE); 1319 } 1320 1321 int dm_pci_find_capability(struct udevice *dev, int cap) 1322 { 1323 u16 status; 1324 u8 header_type; 1325 int ttl = PCI_FIND_CAP_TTL; 1326 u8 id; 1327 u16 ent; 1328 u8 pos; 1329 1330 dm_pci_read_config16(dev, PCI_STATUS, &status); 1331 if (!(status & PCI_STATUS_CAP_LIST)) 1332 return 0; 1333 1334 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type); 1335 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS) 1336 pos = PCI_CB_CAPABILITY_LIST; 1337 else 1338 pos = PCI_CAPABILITY_LIST; 1339 1340 dm_pci_read_config8(dev, pos, &pos); 1341 while (ttl--) { 1342 if (pos < PCI_STD_HEADER_SIZEOF) 1343 break; 1344 pos &= ~3; 1345 dm_pci_read_config16(dev, pos, &ent); 1346 1347 id = ent & 0xff; 1348 if (id == 0xff) 1349 break; 1350 if (id == cap) 1351 return pos; 1352 pos = (ent >> 8); 1353 } 1354 1355 return 0; 1356 } 1357 1358 int dm_pci_find_ext_capability(struct udevice *dev, int cap) 1359 { 1360 u32 header; 1361 int ttl; 1362 int pos = PCI_CFG_SPACE_SIZE; 1363 1364 /* minimum 8 bytes per capability */ 1365 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; 1366 1367 dm_pci_read_config32(dev, pos, &header); 1368 /* 1369 * If we have no capabilities, this is indicated by cap ID, 1370 * cap version and next pointer all being 0. 1371 */ 1372 if (header == 0) 1373 return 0; 1374 1375 while (ttl--) { 1376 if (PCI_EXT_CAP_ID(header) == cap) 1377 return pos; 1378 1379 pos = PCI_EXT_CAP_NEXT(header); 1380 if (pos < PCI_CFG_SPACE_SIZE) 1381 break; 1382 1383 dm_pci_read_config32(dev, pos, &header); 1384 } 1385 1386 return 0; 1387 } 1388 1389 UCLASS_DRIVER(pci) = { 1390 .id = UCLASS_PCI, 1391 .name = "pci", 1392 .flags = DM_UC_FLAG_SEQ_ALIAS, 1393 .post_bind = dm_scan_fdt_dev, 1394 .pre_probe = pci_uclass_pre_probe, 1395 .post_probe = pci_uclass_post_probe, 1396 .child_post_bind = pci_uclass_child_post_bind, 1397 .per_device_auto_alloc_size = sizeof(struct pci_controller), 1398 .per_child_platdata_auto_alloc_size = 1399 sizeof(struct pci_child_platdata), 1400 }; 1401 1402 static const struct dm_pci_ops pci_bridge_ops = { 1403 .read_config = pci_bridge_read_config, 1404 .write_config = pci_bridge_write_config, 1405 }; 1406 1407 static const struct udevice_id pci_bridge_ids[] = { 1408 { .compatible = "pci-bridge" }, 1409 { } 1410 }; 1411 1412 U_BOOT_DRIVER(pci_bridge_drv) = { 1413 .name = "pci_bridge_drv", 1414 .id = UCLASS_PCI, 1415 .of_match = pci_bridge_ids, 1416 .ops = &pci_bridge_ops, 1417 }; 1418 1419 UCLASS_DRIVER(pci_generic) = { 1420 .id = UCLASS_PCI_GENERIC, 1421 .name = "pci_generic", 1422 }; 1423 1424 static const struct udevice_id pci_generic_ids[] = { 1425 { .compatible = "pci-generic" }, 1426 { } 1427 }; 1428 1429 U_BOOT_DRIVER(pci_generic_drv) = { 1430 .name = "pci_generic_drv", 1431 .id = UCLASS_PCI_GENERIC, 1432 .of_match = pci_generic_ids, 1433 }; 1434 1435 void pci_init(void) 1436 { 1437 struct udevice *bus; 1438 1439 /* 1440 * Enumerate all known controller devices. Enumeration has the side- 1441 * effect of probing them, so PCIe devices will be enumerated too. 1442 */ 1443 for (uclass_first_device(UCLASS_PCI, &bus); 1444 bus; 1445 uclass_next_device(&bus)) { 1446 ; 1447 } 1448 } 1449