14 #include <linux/kernel.h>
15 #include <linux/pci.h>
17 #include <linux/string.h>
24 #include <asm/sections.h>
27 #include <asm/pci-bridge.h>
28 #include <asm/machdep.h>
31 #include <asm/iommu.h>
39 #define PCI_RESET_DELAY_US 3000000
41 #define cfg_dbg(fmt...) do { } while(0)
45 static int pnv_msi_check_device(
struct pci_dev* pdev,
int nvec,
int type)
48 struct pnv_phb *phb = hose->private_data;
50 return (phb && phb->msi_map) ? 0 : -
ENODEV;
53 static unsigned int pnv_get_one_msi(
struct pnv_phb *phb)
61 if (
id >= phb->msi_count && phb->msi_next)
63 if (
id >= phb->msi_count) {
68 rc =
id + phb->msi_base;
70 spin_unlock_irqrestore(&phb->
lock, flags);
74 static void pnv_put_msi(
struct pnv_phb *phb,
unsigned int hwirq)
79 if (
WARN_ON(hwirq < phb->msi_base ||
80 hwirq >= (phb->msi_base + phb->msi_count)))
82 id = hwirq - phb->msi_base;
86 spin_unlock_irqrestore(&phb->
lock, flags);
89 static int pnv_setup_msi_irqs(
struct pci_dev *pdev,
int nvec,
int type)
92 struct pnv_phb *phb = hose->private_data;
95 unsigned int hwirq, virq;
102 if (!entry->
msi_attrib.is_64 && !phb->msi32_support) {
103 pr_warn(
"%s: Supports only 64-bit MSIs\n",
107 hwirq = pnv_get_one_msi(phb);
109 pr_warn(
"%s: Failed to find a free MSI\n",
115 pr_warn(
"%s: Failed to map MSI to linux irq\n",
117 pnv_put_msi(phb, hwirq);
123 pr_warn(
"%s: Failed to setup MSI\n", pci_name(pdev));
125 pnv_put_msi(phb, hwirq);
134 static void pnv_teardown_msi_irqs(
struct pci_dev *pdev)
137 struct pnv_phb *phb = hose->private_data;
153 static void pnv_pci_dump_p7ioc_diag_data(
struct pnv_phb *phb)
158 pr_info(
"PHB %d diagnostic data:\n", phb->
hose->global_number);
207 if ((data->
pestA[i] >> 63) == 0 &&
208 (data->
pestB[i] >> 63) == 0)
210 pr_info(
" PE[%3d] PESTA = 0x%016llx\n", i, data->
pestA[i]);
215 static void pnv_pci_dump_phb_diag_data(
struct pnv_phb *phb)
219 pnv_pci_dump_p7ioc_diag_data(phb);
222 pr_warning(
"PCI %d: Can't decode this PHB diag data\n",
223 phb->
hose->global_number);
227 static void pnv_pci_handle_eeh_config(
struct pnv_phb *phb,
u32 pe_no)
240 pr_warning(
"PCI %d: Failed to clear EEH freeze state"
241 " for PE#%d, err %ld\n",
242 phb->
hose->global_number, pe_no, rc);
250 pnv_pci_dump_phb_diag_data(phb);
252 pr_warning(
"PCI %d: No diag data available\n",
253 phb->
hose->global_number);
256 spin_unlock_irqrestore(&phb->
lock, flags);
274 pr_warning(
"PCI %d: Failed to read EEH status for PE#%d,"
275 " err %lld\n", phb->
hose->global_number, pe_no, rc);
278 cfg_dbg(
" -> EEH check, bdfn=%04x PE%d fstate=%x\n",
279 bdfn, pe_no, fstate);
281 pnv_pci_handle_eeh_config(phb, pe_no);
284 static int pnv_pci_read_config(
struct pci_bus *bus,
289 struct pnv_phb *phb = hose->private_data;
319 cfg_dbg(
"pnv_pci_read_config bus: %x devfn: %x +%x/%x -> %08x\n",
320 bus->
number, devfn, where, size, *val);
323 pnv_pci_config_check_eeh(phb, bus, bdfn);
328 static int pnv_pci_write_config(
struct pci_bus *bus,
330 int where,
int size,
u32 val)
333 struct pnv_phb *phb = hose->private_data;
339 cfg_dbg(
"pnv_pci_write_config bus: %x devfn: %x +%x/%x -> %08x\n",
340 bus->
number, devfn, where, size, val);
355 pnv_pci_config_check_eeh(phb, bus, bdfn);
361 .read = pnv_pci_read_config,
362 .write = pnv_pci_write_config,
366 static void pnv_tce_invalidate(
struct iommu_table *tbl,
372 start =
__pa(startp);
385 else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
386 start |= (1ull << 63);
397 while (start <=
end) {
420 tces = tcep = ((
u64 *)tbl->
it_base) + index - tbl->it_offset;
430 if (tbl->it_type & TCE_PCI_SWINV_CREATE)
431 pnv_tce_invalidate(tbl, tces, tcep - 1);
436 static void pnv_tce_free(
struct iommu_table *tbl,
long index,
long npages)
440 tces = tcep = ((
u64 *)tbl->
it_base) + index - tbl->it_offset;
445 if (tbl->it_type & TCE_PCI_SWINV_FREE)
446 pnv_tce_invalidate(tbl, tces, tcep - 1);
449 static unsigned long pnv_tce_get(
struct iommu_table *tbl,
long index)
451 return ((
u64 *)tbl->
it_base)[index - tbl->it_offset];
455 void *tce_mem,
u64 tce_size,
458 tbl->it_blocksize = 16;
464 tbl->it_type = TCE_PCI;
471 const __be64 *basep, *swinvp;
476 if (basep ==
NULL || sizep ==
NULL) {
477 pr_err(
"PCI: %s has missing tce entries !\n",
478 hose->dn->full_name);
493 tbl->it_index = (
unsigned long)
ioremap(swinvp[0], 8);
494 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
502 struct device_node *np = pci_bus_to_OF_node(hose->bus);
508 if (!pdn->iommu_table)
509 pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
510 if (!pdn->iommu_table)
512 set_iommu_table_base(&pdev->
dev, pdn->iommu_table);
518 struct pnv_phb *phb = hose->private_data;
526 pnv_pci_dma_fallback_setup(hose, pdev);
536 static int pnv_pci_probe_mode(
struct pci_bus *bus)
546 if (bus != hose->bus)
547 return PCI_PROBE_NORMAL;
549 if (!tstamp || !*tstamp)
550 return PCI_PROBE_NORMAL;
556 pr_devel(
"pci %04d: Reset target: 0x%llx now: 0x%llx\n",
557 hose->global_number, target, now);
560 msleep((target - now + 999) / 1000);
562 return PCI_PROBE_NORMAL;
572 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
573 #ifdef CONFIG_PPC_POWERNV_RTAS
586 for_each_compatible_node(np,
NULL,
"ibm,ioda-hub") {
593 for_each_compatible_node(np,
NULL,
"ibm,p5ioc2")
601 ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
602 ppc_md.tce_build = pnv_tce_build;
603 ppc_md.tce_free = pnv_tce_free;
604 ppc_md.tce_get = pnv_tce_get;
605 ppc_md.pci_probe_mode = pnv_pci_probe_mode;
609 #ifdef CONFIG_PCI_MSI
610 ppc_md.msi_check_device = pnv_msi_check_device;
611 ppc_md.setup_msi_irqs = pnv_setup_msi_irqs;
612 ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs;