aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pci/pci.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/pci/pci.c')
-rw-r--r--drivers/pci/pci.c513
1 files changed, 406 insertions, 107 deletions
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 061d1ee0046..c12f6c79069 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -56,6 +56,22 @@ unsigned char pci_bus_max_busnr(struct pci_bus* bus)
56} 56}
57EXPORT_SYMBOL_GPL(pci_bus_max_busnr); 57EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
58 58
59#ifdef CONFIG_HAS_IOMEM
60void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
61{
62 /*
63 * Make sure the BAR is actually a memory resource, not an IO resource
64 */
65 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
66 WARN_ON(1);
67 return NULL;
68 }
69 return ioremap_nocache(pci_resource_start(pdev, bar),
70 pci_resource_len(pdev, bar));
71}
72EXPORT_SYMBOL_GPL(pci_ioremap_bar);
73#endif
74
59#if 0 75#if 0
60/** 76/**
61 * pci_max_busnr - returns maximum PCI bus number 77 * pci_max_busnr - returns maximum PCI bus number
@@ -360,25 +376,10 @@ pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
360static void 376static void
361pci_restore_bars(struct pci_dev *dev) 377pci_restore_bars(struct pci_dev *dev)
362{ 378{
363 int i, numres; 379 int i;
364
365 switch (dev->hdr_type) {
366 case PCI_HEADER_TYPE_NORMAL:
367 numres = 6;
368 break;
369 case PCI_HEADER_TYPE_BRIDGE:
370 numres = 2;
371 break;
372 case PCI_HEADER_TYPE_CARDBUS:
373 numres = 1;
374 break;
375 default:
376 /* Should never get here, but just in case... */
377 return;
378 }
379 380
380 for (i = 0; i < numres; i ++) 381 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
381 pci_update_resource(dev, &dev->resource[i], i); 382 pci_update_resource(dev, i);
382} 383}
383 384
384static struct pci_platform_pm_ops *pci_platform_pm; 385static struct pci_platform_pm_ops *pci_platform_pm;
@@ -524,14 +525,17 @@ pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
524 * pci_update_current_state - Read PCI power state of given device from its 525 * pci_update_current_state - Read PCI power state of given device from its
525 * PCI PM registers and cache it 526 * PCI PM registers and cache it
526 * @dev: PCI device to handle. 527 * @dev: PCI device to handle.
528 * @state: State to cache in case the device doesn't have the PM capability
527 */ 529 */
528static void pci_update_current_state(struct pci_dev *dev) 530void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
529{ 531{
530 if (dev->pm_cap) { 532 if (dev->pm_cap) {
531 u16 pmcsr; 533 u16 pmcsr;
532 534
533 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 535 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
534 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK); 536 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
537 } else {
538 dev->current_state = state;
535 } 539 }
536} 540}
537 541
@@ -574,7 +578,7 @@ int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
574 */ 578 */
575 int ret = platform_pci_set_power_state(dev, PCI_D0); 579 int ret = platform_pci_set_power_state(dev, PCI_D0);
576 if (!ret) 580 if (!ret)
577 pci_update_current_state(dev); 581 pci_update_current_state(dev, PCI_D0);
578 } 582 }
579 /* This device is quirked not to be put into D3, so 583 /* This device is quirked not to be put into D3, so
580 don't put it in D3 */ 584 don't put it in D3 */
@@ -587,7 +591,7 @@ int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
587 /* Allow the platform to finalize the transition */ 591 /* Allow the platform to finalize the transition */
588 int ret = platform_pci_set_power_state(dev, state); 592 int ret = platform_pci_set_power_state(dev, state);
589 if (!ret) { 593 if (!ret) {
590 pci_update_current_state(dev); 594 pci_update_current_state(dev, state);
591 error = 0; 595 error = 0;
592 } 596 }
593 } 597 }
@@ -640,19 +644,14 @@ static int pci_save_pcie_state(struct pci_dev *dev)
640 int pos, i = 0; 644 int pos, i = 0;
641 struct pci_cap_saved_state *save_state; 645 struct pci_cap_saved_state *save_state;
642 u16 *cap; 646 u16 *cap;
643 int found = 0;
644 647
645 pos = pci_find_capability(dev, PCI_CAP_ID_EXP); 648 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
646 if (pos <= 0) 649 if (pos <= 0)
647 return 0; 650 return 0;
648 651
649 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP); 652 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
650 if (!save_state)
651 save_state = kzalloc(sizeof(*save_state) + sizeof(u16) * 4, GFP_KERNEL);
652 else
653 found = 1;
654 if (!save_state) { 653 if (!save_state) {
655 dev_err(&dev->dev, "out of memory in pci_save_pcie_state\n"); 654 dev_err(&dev->dev, "buffer not found in %s\n", __FUNCTION__);
656 return -ENOMEM; 655 return -ENOMEM;
657 } 656 }
658 cap = (u16 *)&save_state->data[0]; 657 cap = (u16 *)&save_state->data[0];
@@ -661,9 +660,7 @@ static int pci_save_pcie_state(struct pci_dev *dev)
661 pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]); 660 pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
662 pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]); 661 pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
663 pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]); 662 pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
664 save_state->cap_nr = PCI_CAP_ID_EXP; 663
665 if (!found)
666 pci_add_saved_cap(dev, save_state);
667 return 0; 664 return 0;
668} 665}
669 666
@@ -688,30 +685,21 @@ static void pci_restore_pcie_state(struct pci_dev *dev)
688 685
689static int pci_save_pcix_state(struct pci_dev *dev) 686static int pci_save_pcix_state(struct pci_dev *dev)
690{ 687{
691 int pos, i = 0; 688 int pos;
692 struct pci_cap_saved_state *save_state; 689 struct pci_cap_saved_state *save_state;
693 u16 *cap;
694 int found = 0;
695 690
696 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); 691 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
697 if (pos <= 0) 692 if (pos <= 0)
698 return 0; 693 return 0;
699 694
700 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX); 695 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
701 if (!save_state)
702 save_state = kzalloc(sizeof(*save_state) + sizeof(u16), GFP_KERNEL);
703 else
704 found = 1;
705 if (!save_state) { 696 if (!save_state) {
706 dev_err(&dev->dev, "out of memory in pci_save_pcie_state\n"); 697 dev_err(&dev->dev, "buffer not found in %s\n", __FUNCTION__);
707 return -ENOMEM; 698 return -ENOMEM;
708 } 699 }
709 cap = (u16 *)&save_state->data[0];
710 700
711 pci_read_config_word(dev, pos + PCI_X_CMD, &cap[i++]); 701 pci_read_config_word(dev, pos + PCI_X_CMD, (u16 *)save_state->data);
712 save_state->cap_nr = PCI_CAP_ID_PCIX; 702
713 if (!found)
714 pci_add_saved_cap(dev, save_state);
715 return 0; 703 return 0;
716} 704}
717 705
@@ -982,6 +970,32 @@ void pcim_pin_device(struct pci_dev *pdev)
982 */ 970 */
983void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {} 971void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
984 972
973static void do_pci_disable_device(struct pci_dev *dev)
974{
975 u16 pci_command;
976
977 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
978 if (pci_command & PCI_COMMAND_MASTER) {
979 pci_command &= ~PCI_COMMAND_MASTER;
980 pci_write_config_word(dev, PCI_COMMAND, pci_command);
981 }
982
983 pcibios_disable_device(dev);
984}
985
986/**
987 * pci_disable_enabled_device - Disable device without updating enable_cnt
988 * @dev: PCI device to disable
989 *
990 * NOTE: This function is a backend of PCI power management routines and is
991 * not supposed to be called drivers.
992 */
993void pci_disable_enabled_device(struct pci_dev *dev)
994{
995 if (atomic_read(&dev->enable_cnt))
996 do_pci_disable_device(dev);
997}
998
985/** 999/**
986 * pci_disable_device - Disable PCI device after use 1000 * pci_disable_device - Disable PCI device after use
987 * @dev: PCI device to be disabled 1001 * @dev: PCI device to be disabled
@@ -996,7 +1010,6 @@ void
996pci_disable_device(struct pci_dev *dev) 1010pci_disable_device(struct pci_dev *dev)
997{ 1011{
998 struct pci_devres *dr; 1012 struct pci_devres *dr;
999 u16 pci_command;
1000 1013
1001 dr = find_pci_dr(dev); 1014 dr = find_pci_dr(dev);
1002 if (dr) 1015 if (dr)
@@ -1005,14 +1018,9 @@ pci_disable_device(struct pci_dev *dev)
1005 if (atomic_sub_return(1, &dev->enable_cnt) != 0) 1018 if (atomic_sub_return(1, &dev->enable_cnt) != 0)
1006 return; 1019 return;
1007 1020
1008 pci_read_config_word(dev, PCI_COMMAND, &pci_command); 1021 do_pci_disable_device(dev);
1009 if (pci_command & PCI_COMMAND_MASTER) {
1010 pci_command &= ~PCI_COMMAND_MASTER;
1011 pci_write_config_word(dev, PCI_COMMAND, pci_command);
1012 }
1013 dev->is_busmaster = 0;
1014 1022
1015 pcibios_disable_device(dev); 1023 dev->is_busmaster = 0;
1016} 1024}
1017 1025
1018/** 1026/**
@@ -1107,7 +1115,7 @@ int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
1107 int error = 0; 1115 int error = 0;
1108 bool pme_done = false; 1116 bool pme_done = false;
1109 1117
1110 if (!device_may_wakeup(&dev->dev)) 1118 if (enable && !device_may_wakeup(&dev->dev))
1111 return -EINVAL; 1119 return -EINVAL;
1112 1120
1113 /* 1121 /*
@@ -1252,14 +1260,15 @@ void pci_pm_init(struct pci_dev *dev)
1252 /* find PCI PM capability in list */ 1260 /* find PCI PM capability in list */
1253 pm = pci_find_capability(dev, PCI_CAP_ID_PM); 1261 pm = pci_find_capability(dev, PCI_CAP_ID_PM);
1254 if (!pm) 1262 if (!pm)
1255 return; 1263 goto Exit;
1264
1256 /* Check device's ability to generate PME# */ 1265 /* Check device's ability to generate PME# */
1257 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc); 1266 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
1258 1267
1259 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) { 1268 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
1260 dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n", 1269 dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
1261 pmc & PCI_PM_CAP_VER_MASK); 1270 pmc & PCI_PM_CAP_VER_MASK);
1262 return; 1271 goto Exit;
1263 } 1272 }
1264 1273
1265 dev->pm_cap = pm; 1274 dev->pm_cap = pm;
@@ -1298,6 +1307,74 @@ void pci_pm_init(struct pci_dev *dev)
1298 } else { 1307 } else {
1299 dev->pme_support = 0; 1308 dev->pme_support = 0;
1300 } 1309 }
1310
1311 Exit:
1312 pci_update_current_state(dev, PCI_D0);
1313}
1314
1315/**
1316 * platform_pci_wakeup_init - init platform wakeup if present
1317 * @dev: PCI device
1318 *
1319 * Some devices don't have PCI PM caps but can still generate wakeup
1320 * events through platform methods (like ACPI events). If @dev supports
1321 * platform wakeup events, set the device flag to indicate as much. This
1322 * may be redundant if the device also supports PCI PM caps, but double
1323 * initialization should be safe in that case.
1324 */
1325void platform_pci_wakeup_init(struct pci_dev *dev)
1326{
1327 if (!platform_pci_can_wakeup(dev))
1328 return;
1329
1330 device_set_wakeup_capable(&dev->dev, true);
1331 device_set_wakeup_enable(&dev->dev, false);
1332 platform_pci_sleep_wake(dev, false);
1333}
1334
1335/**
1336 * pci_add_save_buffer - allocate buffer for saving given capability registers
1337 * @dev: the PCI device
1338 * @cap: the capability to allocate the buffer for
1339 * @size: requested size of the buffer
1340 */
1341static int pci_add_cap_save_buffer(
1342 struct pci_dev *dev, char cap, unsigned int size)
1343{
1344 int pos;
1345 struct pci_cap_saved_state *save_state;
1346
1347 pos = pci_find_capability(dev, cap);
1348 if (pos <= 0)
1349 return 0;
1350
1351 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
1352 if (!save_state)
1353 return -ENOMEM;
1354
1355 save_state->cap_nr = cap;
1356 pci_add_saved_cap(dev, save_state);
1357
1358 return 0;
1359}
1360
1361/**
1362 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
1363 * @dev: the PCI device
1364 */
1365void pci_allocate_cap_save_buffers(struct pci_dev *dev)
1366{
1367 int error;
1368
1369 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP, 4 * sizeof(u16));
1370 if (error)
1371 dev_err(&dev->dev,
1372 "unable to preallocate PCI Express save buffer\n");
1373
1374 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
1375 if (error)
1376 dev_err(&dev->dev,
1377 "unable to preallocate PCI-X save buffer\n");
1301} 1378}
1302 1379
1303/** 1380/**
@@ -1337,6 +1414,20 @@ void pci_enable_ari(struct pci_dev *dev)
1337 bridge->ari_enabled = 1; 1414 bridge->ari_enabled = 1;
1338} 1415}
1339 1416
1417/**
1418 * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
1419 * @dev: the PCI device
1420 * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTD, 4=INTD)
1421 *
1422 * Perform INTx swizzling for a device behind one level of bridge. This is
1423 * required by section 9.1 of the PCI-to-PCI bridge specification for devices
1424 * behind bridges on add-in cards.
1425 */
1426u8 pci_swizzle_interrupt_pin(struct pci_dev *dev, u8 pin)
1427{
1428 return (((pin - 1) + PCI_SLOT(dev->devfn)) % 4) + 1;
1429}
1430
1340int 1431int
1341pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge) 1432pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
1342{ 1433{
@@ -1345,9 +1436,9 @@ pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
1345 pin = dev->pin; 1436 pin = dev->pin;
1346 if (!pin) 1437 if (!pin)
1347 return -1; 1438 return -1;
1348 pin--; 1439
1349 while (dev->bus->self) { 1440 while (dev->bus->self) {
1350 pin = (pin + PCI_SLOT(dev->devfn)) % 4; 1441 pin = pci_swizzle_interrupt_pin(dev, pin);
1351 dev = dev->bus->self; 1442 dev = dev->bus->self;
1352 } 1443 }
1353 *bridge = dev; 1444 *bridge = dev;
@@ -1355,6 +1446,26 @@ pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
1355} 1446}
1356 1447
1357/** 1448/**
1449 * pci_common_swizzle - swizzle INTx all the way to root bridge
1450 * @dev: the PCI device
1451 * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
1452 *
1453 * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
1454 * bridges all the way up to a PCI root bus.
1455 */
1456u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
1457{
1458 u8 pin = *pinp;
1459
1460 while (dev->bus->self) {
1461 pin = pci_swizzle_interrupt_pin(dev, pin);
1462 dev = dev->bus->self;
1463 }
1464 *pinp = pin;
1465 return PCI_SLOT(dev->devfn);
1466}
1467
1468/**
1358 * pci_release_region - Release a PCI bar 1469 * pci_release_region - Release a PCI bar
1359 * @pdev: PCI device whose resources were previously reserved by pci_request_region 1470 * @pdev: PCI device whose resources were previously reserved by pci_request_region
1360 * @bar: BAR to release 1471 * @bar: BAR to release
@@ -1395,7 +1506,8 @@ void pci_release_region(struct pci_dev *pdev, int bar)
1395 * Returns 0 on success, or %EBUSY on error. A warning 1506 * Returns 0 on success, or %EBUSY on error. A warning
1396 * message is also printed on failure. 1507 * message is also printed on failure.
1397 */ 1508 */
1398int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name) 1509static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
1510 int exclusive)
1399{ 1511{
1400 struct pci_devres *dr; 1512 struct pci_devres *dr;
1401 1513
@@ -1408,8 +1520,9 @@ int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
1408 goto err_out; 1520 goto err_out;
1409 } 1521 }
1410 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) { 1522 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
1411 if (!request_mem_region(pci_resource_start(pdev, bar), 1523 if (!__request_mem_region(pci_resource_start(pdev, bar),
1412 pci_resource_len(pdev, bar), res_name)) 1524 pci_resource_len(pdev, bar), res_name,
1525 exclusive))
1413 goto err_out; 1526 goto err_out;
1414 } 1527 }
1415 1528
@@ -1428,6 +1541,47 @@ err_out:
1428} 1541}
1429 1542
1430/** 1543/**
1544 * pci_request_region - Reserved PCI I/O and memory resource
1545 * @pdev: PCI device whose resources are to be reserved
1546 * @bar: BAR to be reserved
1547 * @res_name: Name to be associated with resource.
1548 *
1549 * Mark the PCI region associated with PCI device @pdev BR @bar as
1550 * being reserved by owner @res_name. Do not access any
1551 * address inside the PCI regions unless this call returns
1552 * successfully.
1553 *
1554 * Returns 0 on success, or %EBUSY on error. A warning
1555 * message is also printed on failure.
1556 */
1557int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
1558{
1559 return __pci_request_region(pdev, bar, res_name, 0);
1560}
1561
1562/**
1563 * pci_request_region_exclusive - Reserved PCI I/O and memory resource
1564 * @pdev: PCI device whose resources are to be reserved
1565 * @bar: BAR to be reserved
1566 * @res_name: Name to be associated with resource.
1567 *
1568 * Mark the PCI region associated with PCI device @pdev BR @bar as
1569 * being reserved by owner @res_name. Do not access any
1570 * address inside the PCI regions unless this call returns
1571 * successfully.
1572 *
1573 * Returns 0 on success, or %EBUSY on error. A warning
1574 * message is also printed on failure.
1575 *
1576 * The key difference that _exclusive makes it that userspace is
1577 * explicitly not allowed to map the resource via /dev/mem or
1578 * sysfs.
1579 */
1580int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
1581{
1582 return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
1583}
1584/**
1431 * pci_release_selected_regions - Release selected PCI I/O and memory resources 1585 * pci_release_selected_regions - Release selected PCI I/O and memory resources
1432 * @pdev: PCI device whose resources were previously reserved 1586 * @pdev: PCI device whose resources were previously reserved
1433 * @bars: Bitmask of BARs to be released 1587 * @bars: Bitmask of BARs to be released
@@ -1444,20 +1598,14 @@ void pci_release_selected_regions(struct pci_dev *pdev, int bars)
1444 pci_release_region(pdev, i); 1598 pci_release_region(pdev, i);
1445} 1599}
1446 1600
1447/** 1601int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
1448 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources 1602 const char *res_name, int excl)
1449 * @pdev: PCI device whose resources are to be reserved
1450 * @bars: Bitmask of BARs to be requested
1451 * @res_name: Name to be associated with resource
1452 */
1453int pci_request_selected_regions(struct pci_dev *pdev, int bars,
1454 const char *res_name)
1455{ 1603{
1456 int i; 1604 int i;
1457 1605
1458 for (i = 0; i < 6; i++) 1606 for (i = 0; i < 6; i++)
1459 if (bars & (1 << i)) 1607 if (bars & (1 << i))
1460 if(pci_request_region(pdev, i, res_name)) 1608 if (__pci_request_region(pdev, i, res_name, excl))
1461 goto err_out; 1609 goto err_out;
1462 return 0; 1610 return 0;
1463 1611
@@ -1469,6 +1617,26 @@ err_out:
1469 return -EBUSY; 1617 return -EBUSY;
1470} 1618}
1471 1619
1620
1621/**
1622 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
1623 * @pdev: PCI device whose resources are to be reserved
1624 * @bars: Bitmask of BARs to be requested
1625 * @res_name: Name to be associated with resource
1626 */
1627int pci_request_selected_regions(struct pci_dev *pdev, int bars,
1628 const char *res_name)
1629{
1630 return __pci_request_selected_regions(pdev, bars, res_name, 0);
1631}
1632
1633int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
1634 int bars, const char *res_name)
1635{
1636 return __pci_request_selected_regions(pdev, bars, res_name,
1637 IORESOURCE_EXCLUSIVE);
1638}
1639
1472/** 1640/**
1473 * pci_release_regions - Release reserved PCI I/O and memory resources 1641 * pci_release_regions - Release reserved PCI I/O and memory resources
1474 * @pdev: PCI device whose resources were previously reserved by pci_request_regions 1642 * @pdev: PCI device whose resources were previously reserved by pci_request_regions
@@ -1502,27 +1670,66 @@ int pci_request_regions(struct pci_dev *pdev, const char *res_name)
1502} 1670}
1503 1671
1504/** 1672/**
1673 * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
1674 * @pdev: PCI device whose resources are to be reserved
1675 * @res_name: Name to be associated with resource.
1676 *
1677 * Mark all PCI regions associated with PCI device @pdev as
1678 * being reserved by owner @res_name. Do not access any
1679 * address inside the PCI regions unless this call returns
1680 * successfully.
1681 *
1682 * pci_request_regions_exclusive() will mark the region so that
1683 * /dev/mem and the sysfs MMIO access will not be allowed.
1684 *
1685 * Returns 0 on success, or %EBUSY on error. A warning
1686 * message is also printed on failure.
1687 */
1688int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
1689{
1690 return pci_request_selected_regions_exclusive(pdev,
1691 ((1 << 6) - 1), res_name);
1692}
1693
1694static void __pci_set_master(struct pci_dev *dev, bool enable)
1695{
1696 u16 old_cmd, cmd;
1697
1698 pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
1699 if (enable)
1700 cmd = old_cmd | PCI_COMMAND_MASTER;
1701 else
1702 cmd = old_cmd & ~PCI_COMMAND_MASTER;
1703 if (cmd != old_cmd) {
1704 dev_dbg(&dev->dev, "%s bus mastering\n",
1705 enable ? "enabling" : "disabling");
1706 pci_write_config_word(dev, PCI_COMMAND, cmd);
1707 }
1708 dev->is_busmaster = enable;
1709}
1710
1711/**
1505 * pci_set_master - enables bus-mastering for device dev 1712 * pci_set_master - enables bus-mastering for device dev
1506 * @dev: the PCI device to enable 1713 * @dev: the PCI device to enable
1507 * 1714 *
1508 * Enables bus-mastering on the device and calls pcibios_set_master() 1715 * Enables bus-mastering on the device and calls pcibios_set_master()
1509 * to do the needed arch specific settings. 1716 * to do the needed arch specific settings.
1510 */ 1717 */
1511void 1718void pci_set_master(struct pci_dev *dev)
1512pci_set_master(struct pci_dev *dev)
1513{ 1719{
1514 u16 cmd; 1720 __pci_set_master(dev, true);
1515
1516 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1517 if (! (cmd & PCI_COMMAND_MASTER)) {
1518 dev_dbg(&dev->dev, "enabling bus mastering\n");
1519 cmd |= PCI_COMMAND_MASTER;
1520 pci_write_config_word(dev, PCI_COMMAND, cmd);
1521 }
1522 dev->is_busmaster = 1;
1523 pcibios_set_master(dev); 1721 pcibios_set_master(dev);
1524} 1722}
1525 1723
1724/**
1725 * pci_clear_master - disables bus-mastering for device dev
1726 * @dev: the PCI device to disable
1727 */
1728void pci_clear_master(struct pci_dev *dev)
1729{
1730 __pci_set_master(dev, false);
1731}
1732
1526#ifdef PCI_DISABLE_MWI 1733#ifdef PCI_DISABLE_MWI
1527int pci_set_mwi(struct pci_dev *dev) 1734int pci_set_mwi(struct pci_dev *dev)
1528{ 1735{
@@ -1751,24 +1958,7 @@ int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
1751EXPORT_SYMBOL(pci_set_dma_seg_boundary); 1958EXPORT_SYMBOL(pci_set_dma_seg_boundary);
1752#endif 1959#endif
1753 1960
1754/** 1961static int __pcie_flr(struct pci_dev *dev, int probe)
1755 * pci_execute_reset_function() - Reset a PCI device function
1756 * @dev: Device function to reset
1757 *
1758 * Some devices allow an individual function to be reset without affecting
1759 * other functions in the same device. The PCI device must be responsive
1760 * to PCI config space in order to use this function.
1761 *
1762 * The device function is presumed to be unused when this function is called.
1763 * Resetting the device will make the contents of PCI configuration space
1764 * random, so any caller of this must be prepared to reinitialise the
1765 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
1766 * etc.
1767 *
1768 * Returns 0 if the device function was successfully reset or -ENOTTY if the
1769 * device doesn't support resetting a single function.
1770 */
1771int pci_execute_reset_function(struct pci_dev *dev)
1772{ 1962{
1773 u16 status; 1963 u16 status;
1774 u32 cap; 1964 u32 cap;
@@ -1780,6 +1970,9 @@ int pci_execute_reset_function(struct pci_dev *dev)
1780 if (!(cap & PCI_EXP_DEVCAP_FLR)) 1970 if (!(cap & PCI_EXP_DEVCAP_FLR))
1781 return -ENOTTY; 1971 return -ENOTTY;
1782 1972
1973 if (probe)
1974 return 0;
1975
1783 pci_block_user_cfg_access(dev); 1976 pci_block_user_cfg_access(dev);
1784 1977
1785 /* Wait for Transaction Pending bit clean */ 1978 /* Wait for Transaction Pending bit clean */
@@ -1802,6 +1995,80 @@ int pci_execute_reset_function(struct pci_dev *dev)
1802 pci_unblock_user_cfg_access(dev); 1995 pci_unblock_user_cfg_access(dev);
1803 return 0; 1996 return 0;
1804} 1997}
1998
1999static int __pci_af_flr(struct pci_dev *dev, int probe)
2000{
2001 int cappos = pci_find_capability(dev, PCI_CAP_ID_AF);
2002 u8 status;
2003 u8 cap;
2004
2005 if (!cappos)
2006 return -ENOTTY;
2007 pci_read_config_byte(dev, cappos + PCI_AF_CAP, &cap);
2008 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
2009 return -ENOTTY;
2010
2011 if (probe)
2012 return 0;
2013
2014 pci_block_user_cfg_access(dev);
2015
2016 /* Wait for Transaction Pending bit clean */
2017 msleep(100);
2018 pci_read_config_byte(dev, cappos + PCI_AF_STATUS, &status);
2019 if (status & PCI_AF_STATUS_TP) {
2020 dev_info(&dev->dev, "Busy after 100ms while trying to"
2021 " reset; sleeping for 1 second\n");
2022 ssleep(1);
2023 pci_read_config_byte(dev,
2024 cappos + PCI_AF_STATUS, &status);
2025 if (status & PCI_AF_STATUS_TP)
2026 dev_info(&dev->dev, "Still busy after 1s; "
2027 "proceeding with reset anyway\n");
2028 }
2029 pci_write_config_byte(dev, cappos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
2030 mdelay(100);
2031
2032 pci_unblock_user_cfg_access(dev);
2033 return 0;
2034}
2035
2036static int __pci_reset_function(struct pci_dev *pdev, int probe)
2037{
2038 int res;
2039
2040 res = __pcie_flr(pdev, probe);
2041 if (res != -ENOTTY)
2042 return res;
2043
2044 res = __pci_af_flr(pdev, probe);
2045 if (res != -ENOTTY)
2046 return res;
2047
2048 return res;
2049}
2050
2051/**
2052 * pci_execute_reset_function() - Reset a PCI device function
2053 * @dev: Device function to reset
2054 *
2055 * Some devices allow an individual function to be reset without affecting
2056 * other functions in the same device. The PCI device must be responsive
2057 * to PCI config space in order to use this function.
2058 *
2059 * The device function is presumed to be unused when this function is called.
2060 * Resetting the device will make the contents of PCI configuration space
2061 * random, so any caller of this must be prepared to reinitialise the
2062 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
2063 * etc.
2064 *
2065 * Returns 0 if the device function was successfully reset or -ENOTTY if the
2066 * device doesn't support resetting a single function.
2067 */
2068int pci_execute_reset_function(struct pci_dev *dev)
2069{
2070 return __pci_reset_function(dev, 0);
2071}
1805EXPORT_SYMBOL_GPL(pci_execute_reset_function); 2072EXPORT_SYMBOL_GPL(pci_execute_reset_function);
1806 2073
1807/** 2074/**
@@ -1822,15 +2089,10 @@ EXPORT_SYMBOL_GPL(pci_execute_reset_function);
1822 */ 2089 */
1823int pci_reset_function(struct pci_dev *dev) 2090int pci_reset_function(struct pci_dev *dev)
1824{ 2091{
1825 u32 cap; 2092 int r = __pci_reset_function(dev, 1);
1826 int exppos = pci_find_capability(dev, PCI_CAP_ID_EXP);
1827 int r;
1828 2093
1829 if (!exppos) 2094 if (r < 0)
1830 return -ENOTTY; 2095 return r;
1831 pci_read_config_dword(dev, exppos + PCI_EXP_DEVCAP, &cap);
1832 if (!(cap & PCI_EXP_DEVCAP_FLR))
1833 return -ENOTTY;
1834 2096
1835 if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0) 2097 if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0)
1836 disable_irq(dev->irq); 2098 disable_irq(dev->irq);
@@ -2022,6 +2284,28 @@ int pci_select_bars(struct pci_dev *dev, unsigned long flags)
2022 return bars; 2284 return bars;
2023} 2285}
2024 2286
2287/**
2288 * pci_resource_bar - get position of the BAR associated with a resource
2289 * @dev: the PCI device
2290 * @resno: the resource number
2291 * @type: the BAR type to be filled in
2292 *
2293 * Returns BAR position in config space, or 0 if the BAR is invalid.
2294 */
2295int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
2296{
2297 if (resno < PCI_ROM_RESOURCE) {
2298 *type = pci_bar_unknown;
2299 return PCI_BASE_ADDRESS_0 + 4 * resno;
2300 } else if (resno == PCI_ROM_RESOURCE) {
2301 *type = pci_bar_mem32;
2302 return dev->rom_base_reg;
2303 }
2304
2305 dev_err(&dev->dev, "BAR: invalid resource #%d\n", resno);
2306 return 0;
2307}
2308
2025static void __devinit pci_no_domains(void) 2309static void __devinit pci_no_domains(void)
2026{ 2310{
2027#ifdef CONFIG_PCI_DOMAINS 2311#ifdef CONFIG_PCI_DOMAINS
@@ -2029,6 +2313,19 @@ static void __devinit pci_no_domains(void)
2029#endif 2313#endif
2030} 2314}
2031 2315
2316/**
2317 * pci_ext_cfg_enabled - can we access extended PCI config space?
2318 * @dev: The PCI device of the root bridge.
2319 *
2320 * Returns 1 if we can access PCI extended config space (offsets
2321 * greater than 0xff). This is the default implementation. Architecture
2322 * implementations can override this.
2323 */
2324int __attribute__ ((weak)) pci_ext_cfg_avail(struct pci_dev *dev)
2325{
2326 return 1;
2327}
2328
2032static int __devinit pci_init(void) 2329static int __devinit pci_init(void)
2033{ 2330{
2034 struct pci_dev *dev = NULL; 2331 struct pci_dev *dev = NULL;
@@ -2037,8 +2334,6 @@ static int __devinit pci_init(void)
2037 pci_fixup_device(pci_fixup_final, dev); 2334 pci_fixup_device(pci_fixup_final, dev);
2038 } 2335 }
2039 2336
2040 msi_init();
2041
2042 return 0; 2337 return 0;
2043} 2338}
2044 2339
@@ -2083,11 +2378,15 @@ EXPORT_SYMBOL(pci_find_capability);
2083EXPORT_SYMBOL(pci_bus_find_capability); 2378EXPORT_SYMBOL(pci_bus_find_capability);
2084EXPORT_SYMBOL(pci_release_regions); 2379EXPORT_SYMBOL(pci_release_regions);
2085EXPORT_SYMBOL(pci_request_regions); 2380EXPORT_SYMBOL(pci_request_regions);
2381EXPORT_SYMBOL(pci_request_regions_exclusive);
2086EXPORT_SYMBOL(pci_release_region); 2382EXPORT_SYMBOL(pci_release_region);
2087EXPORT_SYMBOL(pci_request_region); 2383EXPORT_SYMBOL(pci_request_region);
2384EXPORT_SYMBOL(pci_request_region_exclusive);
2088EXPORT_SYMBOL(pci_release_selected_regions); 2385EXPORT_SYMBOL(pci_release_selected_regions);
2089EXPORT_SYMBOL(pci_request_selected_regions); 2386EXPORT_SYMBOL(pci_request_selected_regions);
2387EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
2090EXPORT_SYMBOL(pci_set_master); 2388EXPORT_SYMBOL(pci_set_master);
2389EXPORT_SYMBOL(pci_clear_master);
2091EXPORT_SYMBOL(pci_set_mwi); 2390EXPORT_SYMBOL(pci_set_mwi);
2092EXPORT_SYMBOL(pci_try_set_mwi); 2391EXPORT_SYMBOL(pci_try_set_mwi);
2093EXPORT_SYMBOL(pci_clear_mwi); 2392EXPORT_SYMBOL(pci_clear_mwi);