Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Support PCI/PCIe on PowerNV platforms |
| 3 | * |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 4 | * Copyright 2011 Benjamin Herrenschmidt, IBM Corp. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/pci.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/init.h> |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 17 | #include <linux/irq.h> |
| 18 | #include <linux/io.h> |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 19 | #include <linux/msi.h> |
Alexey Kardashevskiy | 4e13c1a | 2013-05-21 13:33:09 +1000 | [diff] [blame] | 20 | #include <linux/iommu.h> |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 21 | |
| 22 | #include <asm/sections.h> |
| 23 | #include <asm/io.h> |
| 24 | #include <asm/prom.h> |
| 25 | #include <asm/pci-bridge.h> |
| 26 | #include <asm/machdep.h> |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 27 | #include <asm/msi_bitmap.h> |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 28 | #include <asm/ppc-pci.h> |
| 29 | #include <asm/opal.h> |
| 30 | #include <asm/iommu.h> |
| 31 | #include <asm/tce.h> |
Stephen Rothwell | f533927 | 2012-03-15 18:18:00 +0000 | [diff] [blame] | 32 | #include <asm/firmware.h> |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 33 | #include <asm/eeh_event.h> |
| 34 | #include <asm/eeh.h> |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 35 | |
| 36 | #include "powernv.h" |
| 37 | #include "pci.h" |
| 38 | |
Benjamin Herrenschmidt | 82ba129 | 2011-09-19 17:45:07 +0000 | [diff] [blame] | 39 | /* Delay in usec */ |
| 40 | #define PCI_RESET_DELAY_US 3000000 |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 41 | |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 42 | #ifdef CONFIG_PCI_MSI |
Daniel Axtens | 92ae035 | 2015-04-28 15:12:05 +1000 | [diff] [blame] | 43 | int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 44 | { |
| 45 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 46 | struct pnv_phb *phb = hose->private_data; |
| 47 | struct msi_desc *entry; |
| 48 | struct msi_msg msg; |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 49 | int hwirq; |
| 50 | unsigned int virq; |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 51 | int rc; |
| 52 | |
Alexander Gordeev | 6b2fd7ef | 2014-09-07 20:57:53 +0200 | [diff] [blame] | 53 | if (WARN_ON(!phb) || !phb->msi_bmp.bitmap) |
| 54 | return -ENODEV; |
| 55 | |
Benjamin Herrenschmidt | 3607438 | 2014-10-07 16:12:36 +1100 | [diff] [blame] | 56 | if (pdev->no_64bit_msi && !phb->msi32_support) |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 57 | return -ENODEV; |
| 58 | |
Jiang Liu | 2921d17 | 2015-07-09 16:00:38 +0800 | [diff] [blame] | 59 | for_each_pci_msi_entry(entry, pdev) { |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 60 | if (!entry->msi_attrib.is_64 && !phb->msi32_support) { |
| 61 | pr_warn("%s: Supports only 64-bit MSIs\n", |
| 62 | pci_name(pdev)); |
| 63 | return -ENXIO; |
| 64 | } |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 65 | hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1); |
| 66 | if (hwirq < 0) { |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 67 | pr_warn("%s: Failed to find a free MSI\n", |
| 68 | pci_name(pdev)); |
| 69 | return -ENOSPC; |
| 70 | } |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 71 | virq = irq_create_mapping(NULL, phb->msi_base + hwirq); |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 72 | if (virq == NO_IRQ) { |
| 73 | pr_warn("%s: Failed to map MSI to linux irq\n", |
| 74 | pci_name(pdev)); |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 75 | msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1); |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 76 | return -ENOMEM; |
| 77 | } |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 78 | rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq, |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame] | 79 | virq, entry->msi_attrib.is_64, &msg); |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 80 | if (rc) { |
| 81 | pr_warn("%s: Failed to setup MSI\n", pci_name(pdev)); |
| 82 | irq_dispose_mapping(virq); |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 83 | msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1); |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 84 | return rc; |
| 85 | } |
| 86 | irq_set_msi_desc(virq, entry); |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 87 | pci_write_msi_msg(virq, &msg); |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
Daniel Axtens | 92ae035 | 2015-04-28 15:12:05 +1000 | [diff] [blame] | 92 | void pnv_teardown_msi_irqs(struct pci_dev *pdev) |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 93 | { |
| 94 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 95 | struct pnv_phb *phb = hose->private_data; |
| 96 | struct msi_desc *entry; |
Paul Mackerras | e297c93 | 2015-09-10 14:36:21 +1000 | [diff] [blame] | 97 | irq_hw_number_t hwirq; |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 98 | |
| 99 | if (WARN_ON(!phb)) |
| 100 | return; |
| 101 | |
Jiang Liu | 2921d17 | 2015-07-09 16:00:38 +0800 | [diff] [blame] | 102 | for_each_pci_msi_entry(entry, pdev) { |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 103 | if (entry->irq == NO_IRQ) |
| 104 | continue; |
Paul Mackerras | e297c93 | 2015-09-10 14:36:21 +1000 | [diff] [blame] | 105 | hwirq = virq_to_hw(entry->irq); |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 106 | irq_set_msi_desc(entry->irq, NULL); |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 107 | irq_dispose_mapping(entry->irq); |
Paul Mackerras | e297c93 | 2015-09-10 14:36:21 +1000 | [diff] [blame] | 108 | msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, 1); |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | #endif /* CONFIG_PCI_MSI */ |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 112 | |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 113 | static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose, |
| 114 | struct OpalIoPhbErrorCommon *common) |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 115 | { |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 116 | struct OpalIoP7IOCPhbErrorData *data; |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 117 | int i; |
| 118 | |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 119 | data = (struct OpalIoP7IOCPhbErrorData *)common; |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 120 | pr_info("P7IOC PHB#%d Diag-data (Version: %d)\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 121 | hose->global_number, be32_to_cpu(common->version)); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 122 | |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 123 | if (data->brdgCtl) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 124 | pr_info("brdgCtl: %08x\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 125 | be32_to_cpu(data->brdgCtl)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 126 | if (data->portStatusReg || data->rootCmplxStatus || |
| 127 | data->busAgentStatus) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 128 | pr_info("UtlSts: %08x %08x %08x\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 129 | be32_to_cpu(data->portStatusReg), |
| 130 | be32_to_cpu(data->rootCmplxStatus), |
| 131 | be32_to_cpu(data->busAgentStatus)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 132 | if (data->deviceStatus || data->slotStatus || |
| 133 | data->linkStatus || data->devCmdStatus || |
| 134 | data->devSecStatus) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 135 | pr_info("RootSts: %08x %08x %08x %08x %08x\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 136 | be32_to_cpu(data->deviceStatus), |
| 137 | be32_to_cpu(data->slotStatus), |
| 138 | be32_to_cpu(data->linkStatus), |
| 139 | be32_to_cpu(data->devCmdStatus), |
| 140 | be32_to_cpu(data->devSecStatus)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 141 | if (data->rootErrorStatus || data->uncorrErrorStatus || |
| 142 | data->corrErrorStatus) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 143 | pr_info("RootErrSts: %08x %08x %08x\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 144 | be32_to_cpu(data->rootErrorStatus), |
| 145 | be32_to_cpu(data->uncorrErrorStatus), |
| 146 | be32_to_cpu(data->corrErrorStatus)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 147 | if (data->tlpHdr1 || data->tlpHdr2 || |
| 148 | data->tlpHdr3 || data->tlpHdr4) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 149 | pr_info("RootErrLog: %08x %08x %08x %08x\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 150 | be32_to_cpu(data->tlpHdr1), |
| 151 | be32_to_cpu(data->tlpHdr2), |
| 152 | be32_to_cpu(data->tlpHdr3), |
| 153 | be32_to_cpu(data->tlpHdr4)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 154 | if (data->sourceId || data->errorClass || |
| 155 | data->correlator) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 156 | pr_info("RootErrLog1: %08x %016llx %016llx\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 157 | be32_to_cpu(data->sourceId), |
| 158 | be64_to_cpu(data->errorClass), |
| 159 | be64_to_cpu(data->correlator)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 160 | if (data->p7iocPlssr || data->p7iocCsr) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 161 | pr_info("PhbSts: %016llx %016llx\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 162 | be64_to_cpu(data->p7iocPlssr), |
| 163 | be64_to_cpu(data->p7iocCsr)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 164 | if (data->lemFir) |
| 165 | pr_info("Lem: %016llx %016llx %016llx\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 166 | be64_to_cpu(data->lemFir), |
| 167 | be64_to_cpu(data->lemErrorMask), |
| 168 | be64_to_cpu(data->lemWOF)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 169 | if (data->phbErrorStatus) |
| 170 | pr_info("PhbErr: %016llx %016llx %016llx %016llx\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 171 | be64_to_cpu(data->phbErrorStatus), |
| 172 | be64_to_cpu(data->phbFirstErrorStatus), |
| 173 | be64_to_cpu(data->phbErrorLog0), |
| 174 | be64_to_cpu(data->phbErrorLog1)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 175 | if (data->mmioErrorStatus) |
| 176 | pr_info("OutErr: %016llx %016llx %016llx %016llx\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 177 | be64_to_cpu(data->mmioErrorStatus), |
| 178 | be64_to_cpu(data->mmioFirstErrorStatus), |
| 179 | be64_to_cpu(data->mmioErrorLog0), |
| 180 | be64_to_cpu(data->mmioErrorLog1)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 181 | if (data->dma0ErrorStatus) |
| 182 | pr_info("InAErr: %016llx %016llx %016llx %016llx\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 183 | be64_to_cpu(data->dma0ErrorStatus), |
| 184 | be64_to_cpu(data->dma0FirstErrorStatus), |
| 185 | be64_to_cpu(data->dma0ErrorLog0), |
| 186 | be64_to_cpu(data->dma0ErrorLog1)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 187 | if (data->dma1ErrorStatus) |
| 188 | pr_info("InBErr: %016llx %016llx %016llx %016llx\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 189 | be64_to_cpu(data->dma1ErrorStatus), |
| 190 | be64_to_cpu(data->dma1FirstErrorStatus), |
| 191 | be64_to_cpu(data->dma1ErrorLog0), |
| 192 | be64_to_cpu(data->dma1ErrorLog1)); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 193 | |
| 194 | for (i = 0; i < OPAL_P7IOC_NUM_PEST_REGS; i++) { |
| 195 | if ((data->pestA[i] >> 63) == 0 && |
| 196 | (data->pestB[i] >> 63) == 0) |
| 197 | continue; |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 198 | |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 199 | pr_info("PE[%3d] A/B: %016llx %016llx\n", |
Gavin Shan | f18440f | 2014-07-17 14:41:42 +1000 | [diff] [blame] | 200 | i, be64_to_cpu(data->pestA[i]), |
| 201 | be64_to_cpu(data->pestB[i])); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 205 | static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose, |
| 206 | struct OpalIoPhbErrorCommon *common) |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 207 | { |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 208 | struct OpalIoPhb3ErrorData *data; |
| 209 | int i; |
| 210 | |
| 211 | data = (struct OpalIoPhb3ErrorData*)common; |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 212 | pr_info("PHB3 PHB#%d Diag-data (Version: %d)\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 213 | hose->global_number, be32_to_cpu(common->version)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 214 | if (data->brdgCtl) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 215 | pr_info("brdgCtl: %08x\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 216 | be32_to_cpu(data->brdgCtl)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 217 | if (data->portStatusReg || data->rootCmplxStatus || |
| 218 | data->busAgentStatus) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 219 | pr_info("UtlSts: %08x %08x %08x\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 220 | be32_to_cpu(data->portStatusReg), |
| 221 | be32_to_cpu(data->rootCmplxStatus), |
| 222 | be32_to_cpu(data->busAgentStatus)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 223 | if (data->deviceStatus || data->slotStatus || |
| 224 | data->linkStatus || data->devCmdStatus || |
| 225 | data->devSecStatus) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 226 | pr_info("RootSts: %08x %08x %08x %08x %08x\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 227 | be32_to_cpu(data->deviceStatus), |
| 228 | be32_to_cpu(data->slotStatus), |
| 229 | be32_to_cpu(data->linkStatus), |
| 230 | be32_to_cpu(data->devCmdStatus), |
| 231 | be32_to_cpu(data->devSecStatus)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 232 | if (data->rootErrorStatus || data->uncorrErrorStatus || |
| 233 | data->corrErrorStatus) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 234 | pr_info("RootErrSts: %08x %08x %08x\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 235 | be32_to_cpu(data->rootErrorStatus), |
| 236 | be32_to_cpu(data->uncorrErrorStatus), |
| 237 | be32_to_cpu(data->corrErrorStatus)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 238 | if (data->tlpHdr1 || data->tlpHdr2 || |
| 239 | data->tlpHdr3 || data->tlpHdr4) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 240 | pr_info("RootErrLog: %08x %08x %08x %08x\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 241 | be32_to_cpu(data->tlpHdr1), |
| 242 | be32_to_cpu(data->tlpHdr2), |
| 243 | be32_to_cpu(data->tlpHdr3), |
| 244 | be32_to_cpu(data->tlpHdr4)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 245 | if (data->sourceId || data->errorClass || |
| 246 | data->correlator) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 247 | pr_info("RootErrLog1: %08x %016llx %016llx\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 248 | be32_to_cpu(data->sourceId), |
| 249 | be64_to_cpu(data->errorClass), |
| 250 | be64_to_cpu(data->correlator)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 251 | if (data->nFir) |
| 252 | pr_info("nFir: %016llx %016llx %016llx\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 253 | be64_to_cpu(data->nFir), |
| 254 | be64_to_cpu(data->nFirMask), |
| 255 | be64_to_cpu(data->nFirWOF)); |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 256 | if (data->phbPlssr || data->phbCsr) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 257 | pr_info("PhbSts: %016llx %016llx\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 258 | be64_to_cpu(data->phbPlssr), |
| 259 | be64_to_cpu(data->phbCsr)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 260 | if (data->lemFir) |
| 261 | pr_info("Lem: %016llx %016llx %016llx\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 262 | be64_to_cpu(data->lemFir), |
| 263 | be64_to_cpu(data->lemErrorMask), |
| 264 | be64_to_cpu(data->lemWOF)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 265 | if (data->phbErrorStatus) |
| 266 | pr_info("PhbErr: %016llx %016llx %016llx %016llx\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 267 | be64_to_cpu(data->phbErrorStatus), |
| 268 | be64_to_cpu(data->phbFirstErrorStatus), |
| 269 | be64_to_cpu(data->phbErrorLog0), |
| 270 | be64_to_cpu(data->phbErrorLog1)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 271 | if (data->mmioErrorStatus) |
| 272 | pr_info("OutErr: %016llx %016llx %016llx %016llx\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 273 | be64_to_cpu(data->mmioErrorStatus), |
| 274 | be64_to_cpu(data->mmioFirstErrorStatus), |
| 275 | be64_to_cpu(data->mmioErrorLog0), |
| 276 | be64_to_cpu(data->mmioErrorLog1)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 277 | if (data->dma0ErrorStatus) |
| 278 | pr_info("InAErr: %016llx %016llx %016llx %016llx\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 279 | be64_to_cpu(data->dma0ErrorStatus), |
| 280 | be64_to_cpu(data->dma0FirstErrorStatus), |
| 281 | be64_to_cpu(data->dma0ErrorLog0), |
| 282 | be64_to_cpu(data->dma0ErrorLog1)); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 283 | if (data->dma1ErrorStatus) |
| 284 | pr_info("InBErr: %016llx %016llx %016llx %016llx\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 285 | be64_to_cpu(data->dma1ErrorStatus), |
| 286 | be64_to_cpu(data->dma1FirstErrorStatus), |
| 287 | be64_to_cpu(data->dma1ErrorLog0), |
| 288 | be64_to_cpu(data->dma1ErrorLog1)); |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 289 | |
| 290 | for (i = 0; i < OPAL_PHB3_NUM_PEST_REGS; i++) { |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 291 | if ((be64_to_cpu(data->pestA[i]) >> 63) == 0 && |
| 292 | (be64_to_cpu(data->pestB[i]) >> 63) == 0) |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 293 | continue; |
| 294 | |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame] | 295 | pr_info("PE[%3d] A/B: %016llx %016llx\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 296 | i, be64_to_cpu(data->pestA[i]), |
| 297 | be64_to_cpu(data->pestB[i])); |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | void pnv_pci_dump_phb_diag_data(struct pci_controller *hose, |
| 302 | unsigned char *log_buff) |
| 303 | { |
| 304 | struct OpalIoPhbErrorCommon *common; |
| 305 | |
| 306 | if (!hose || !log_buff) |
| 307 | return; |
| 308 | |
| 309 | common = (struct OpalIoPhbErrorCommon *)log_buff; |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 310 | switch (be32_to_cpu(common->ioType)) { |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 311 | case OPAL_PHB_ERROR_DATA_TYPE_P7IOC: |
| 312 | pnv_pci_dump_p7ioc_diag_data(hose, common); |
| 313 | break; |
| 314 | case OPAL_PHB_ERROR_DATA_TYPE_PHB3: |
| 315 | pnv_pci_dump_phb3_diag_data(hose, common); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 316 | break; |
| 317 | default: |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 318 | pr_warn("%s: Unrecognized ioType %d\n", |
Guo Chao | ddf0322a | 2014-06-09 16:58:51 +0800 | [diff] [blame] | 319 | __func__, be32_to_cpu(common->ioType)); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no) |
| 324 | { |
| 325 | unsigned long flags, rc; |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 326 | int has_diag, ret = 0; |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 327 | |
| 328 | spin_lock_irqsave(&phb->lock, flags); |
| 329 | |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 330 | /* Fetch PHB diag-data */ |
Gavin Shan | 2377323 | 2013-06-20 13:21:05 +0800 | [diff] [blame] | 331 | rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob, |
| 332 | PNV_PCI_DIAG_BUF_SIZE); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 333 | has_diag = (rc == OPAL_SUCCESS); |
| 334 | |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 335 | /* If PHB supports compound PE, to handle it */ |
| 336 | if (phb->unfreeze_pe) { |
| 337 | ret = phb->unfreeze_pe(phb, |
| 338 | pe_no, |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 339 | OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 340 | } else { |
| 341 | rc = opal_pci_eeh_freeze_clear(phb->opal_id, |
| 342 | pe_no, |
| 343 | OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); |
| 344 | if (rc) { |
| 345 | pr_warn("%s: Failure %ld clearing frozen " |
| 346 | "PHB#%x-PE#%x\n", |
| 347 | __func__, rc, phb->hose->global_number, |
| 348 | pe_no); |
| 349 | ret = -EIO; |
| 350 | } |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 353 | /* |
| 354 | * For now, let's only display the diag buffer when we fail to clear |
| 355 | * the EEH status. We'll do more sensible things later when we have |
| 356 | * proper EEH support. We need to make sure we don't pollute ourselves |
| 357 | * with the normal errors generated when probing empty slots |
| 358 | */ |
| 359 | if (has_diag && ret) |
| 360 | pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob); |
| 361 | |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 362 | spin_unlock_irqrestore(&phb->lock, flags); |
| 363 | } |
| 364 | |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 365 | static void pnv_pci_config_check_eeh(struct pci_dn *pdn) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 366 | { |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 367 | struct pnv_phb *phb = pdn->phb->private_data; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 368 | u8 fstate; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 369 | __be16 pcierr; |
Gavin Shan | 689ee8c | 2016-05-03 15:41:25 +1000 | [diff] [blame^] | 370 | unsigned int pe_no; |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 371 | s64 rc; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 372 | |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 373 | /* |
| 374 | * Get the PE#. During the PCI probe stage, we might not |
| 375 | * setup that yet. So all ER errors should be mapped to |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 376 | * reserved PE. |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 377 | */ |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 378 | pe_no = pdn->pe_number; |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 379 | if (pe_no == IODA_INVALID_PE) { |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 380 | pe_no = phb->ioda.reserved_pe_idx; |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 381 | } |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 382 | |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 383 | /* |
| 384 | * Fetch frozen state. If the PHB support compound PE, |
| 385 | * we need handle that case. |
| 386 | */ |
| 387 | if (phb->get_pe_state) { |
| 388 | fstate = phb->get_pe_state(phb, pe_no); |
| 389 | } else { |
| 390 | rc = opal_pci_eeh_freeze_status(phb->opal_id, |
| 391 | pe_no, |
| 392 | &fstate, |
| 393 | &pcierr, |
| 394 | NULL); |
| 395 | if (rc) { |
| 396 | pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n", |
| 397 | __func__, rc, phb->hose->global_number, pe_no); |
| 398 | return; |
| 399 | } |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 400 | } |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 401 | |
Alexey Kardashevskiy | 9e44754 | 2016-05-02 17:06:12 +1000 | [diff] [blame] | 402 | pr_devel(" -> EEH check, bdfn=%04x PE#%d fstate=%x\n", |
| 403 | (pdn->busno << 8) | (pdn->devfn), pe_no, fstate); |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 404 | |
| 405 | /* Clear the frozen state if applicable */ |
| 406 | if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE || |
| 407 | fstate == OPAL_EEH_STOPPED_DMA_FREEZE || |
| 408 | fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) { |
| 409 | /* |
| 410 | * If PHB supports compound PE, freeze it for |
| 411 | * consistency. |
| 412 | */ |
| 413 | if (phb->freeze_pe) |
| 414 | phb->freeze_pe(phb, pe_no); |
| 415 | |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 416 | pnv_pci_handle_eeh_config(phb, pe_no); |
Gavin Shan | 98fd700 | 2014-07-21 14:42:35 +1000 | [diff] [blame] | 417 | } |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 420 | int pnv_pci_cfg_read(struct pci_dn *pdn, |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 421 | int where, int size, u32 *val) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 422 | { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 423 | struct pnv_phb *phb = pdn->phb->private_data; |
| 424 | u32 bdfn = (pdn->busno << 8) | pdn->devfn; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 425 | s64 rc; |
| 426 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 427 | switch (size) { |
| 428 | case 1: { |
| 429 | u8 v8; |
| 430 | rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8); |
| 431 | *val = (rc == OPAL_SUCCESS) ? v8 : 0xff; |
| 432 | break; |
| 433 | } |
| 434 | case 2: { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 435 | __be16 v16; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 436 | rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where, |
| 437 | &v16); |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 438 | *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | case 4: { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 442 | __be32 v32; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 443 | rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32); |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 444 | *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 445 | break; |
| 446 | } |
| 447 | default: |
| 448 | return PCIBIOS_FUNC_NOT_SUPPORTED; |
| 449 | } |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 450 | |
Alexey Kardashevskiy | 9e44754 | 2016-05-02 17:06:12 +1000 | [diff] [blame] | 451 | pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n", |
| 452 | __func__, pdn->busno, pdn->devfn, where, size, *val); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 453 | return PCIBIOS_SUCCESSFUL; |
| 454 | } |
| 455 | |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 456 | int pnv_pci_cfg_write(struct pci_dn *pdn, |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 457 | int where, int size, u32 val) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 458 | { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 459 | struct pnv_phb *phb = pdn->phb->private_data; |
| 460 | u32 bdfn = (pdn->busno << 8) | pdn->devfn; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 461 | |
Alexey Kardashevskiy | 9e44754 | 2016-05-02 17:06:12 +1000 | [diff] [blame] | 462 | pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n", |
| 463 | __func__, pdn->busno, pdn->devfn, where, size, val); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 464 | switch (size) { |
| 465 | case 1: |
| 466 | opal_pci_config_write_byte(phb->opal_id, bdfn, where, val); |
| 467 | break; |
| 468 | case 2: |
| 469 | opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val); |
| 470 | break; |
| 471 | case 4: |
| 472 | opal_pci_config_write_word(phb->opal_id, bdfn, where, val); |
| 473 | break; |
| 474 | default: |
| 475 | return PCIBIOS_FUNC_NOT_SUPPORTED; |
| 476 | } |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 477 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 478 | return PCIBIOS_SUCCESSFUL; |
| 479 | } |
| 480 | |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 481 | #if CONFIG_EEH |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 482 | static bool pnv_pci_cfg_check(struct pci_dn *pdn) |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 483 | { |
| 484 | struct eeh_dev *edev = NULL; |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 485 | struct pnv_phb *phb = pdn->phb->private_data; |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 486 | |
| 487 | /* EEH not enabled ? */ |
| 488 | if (!(phb->flags & PNV_PHB_FLAG_EEH)) |
| 489 | return true; |
| 490 | |
Gavin Shan | d2b0f6f | 2014-04-24 18:00:19 +1000 | [diff] [blame] | 491 | /* PE reset or device removed ? */ |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 492 | edev = pdn->edev; |
Gavin Shan | d2b0f6f | 2014-04-24 18:00:19 +1000 | [diff] [blame] | 493 | if (edev) { |
| 494 | if (edev->pe && |
Gavin Shan | 8a6b371 | 2014-10-01 17:07:50 +1000 | [diff] [blame] | 495 | (edev->pe->state & EEH_PE_CFG_BLOCKED)) |
Gavin Shan | d2b0f6f | 2014-04-24 18:00:19 +1000 | [diff] [blame] | 496 | return false; |
| 497 | |
| 498 | if (edev->mode & EEH_DEV_REMOVED) |
| 499 | return false; |
| 500 | } |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 501 | |
| 502 | return true; |
| 503 | } |
| 504 | #else |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 505 | static inline pnv_pci_cfg_check(struct pci_dn *pdn) |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 506 | { |
| 507 | return true; |
| 508 | } |
| 509 | #endif /* CONFIG_EEH */ |
| 510 | |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 511 | static int pnv_pci_read_config(struct pci_bus *bus, |
| 512 | unsigned int devfn, |
| 513 | int where, int size, u32 *val) |
| 514 | { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 515 | struct pci_dn *pdn; |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 516 | struct pnv_phb *phb; |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 517 | int ret; |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 518 | |
| 519 | *val = 0xFFFFFFFF; |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 520 | pdn = pci_get_pdn_by_devfn(bus, devfn); |
| 521 | if (!pdn) |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 522 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 523 | |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 524 | if (!pnv_pci_cfg_check(pdn)) |
| 525 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 526 | |
| 527 | ret = pnv_pci_cfg_read(pdn, where, size, val); |
| 528 | phb = pdn->phb->private_data; |
| 529 | if (phb->flags & PNV_PHB_FLAG_EEH && pdn->edev) { |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 530 | if (*val == EEH_IO_ERROR_VALUE(size) && |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 531 | eeh_dev_check_failure(pdn->edev)) |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 532 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 533 | } else { |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 534 | pnv_pci_config_check_eeh(pdn); |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | return ret; |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | static int pnv_pci_write_config(struct pci_bus *bus, |
| 541 | unsigned int devfn, |
| 542 | int where, int size, u32 val) |
| 543 | { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 544 | struct pci_dn *pdn; |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 545 | struct pnv_phb *phb; |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 546 | int ret; |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 547 | |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 548 | pdn = pci_get_pdn_by_devfn(bus, devfn); |
| 549 | if (!pdn) |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 550 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 551 | |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 552 | if (!pnv_pci_cfg_check(pdn)) |
| 553 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 554 | |
| 555 | ret = pnv_pci_cfg_write(pdn, where, size, val); |
| 556 | phb = pdn->phb->private_data; |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 557 | if (!(phb->flags & PNV_PHB_FLAG_EEH)) |
Gavin Shan | 3532a741 | 2015-03-17 16:15:03 +1100 | [diff] [blame] | 558 | pnv_pci_config_check_eeh(pdn); |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 559 | |
| 560 | return ret; |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 561 | } |
| 562 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 563 | struct pci_ops pnv_pci_ops = { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 564 | .read = pnv_pci_read_config, |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 565 | .write = pnv_pci_write_config, |
| 566 | }; |
| 567 | |
Alexey Kardashevskiy | c5bb44e | 2015-06-05 16:35:14 +1000 | [diff] [blame] | 568 | static __be64 *pnv_tce(struct iommu_table *tbl, long idx) |
| 569 | { |
| 570 | __be64 *tmp = ((__be64 *)tbl->it_base); |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 571 | int level = tbl->it_indirect_levels; |
| 572 | const long shift = ilog2(tbl->it_level_size); |
| 573 | unsigned long mask = (tbl->it_level_size - 1) << (level * shift); |
| 574 | |
| 575 | while (level) { |
| 576 | int n = (idx & mask) >> (level * shift); |
| 577 | unsigned long tce = be64_to_cpu(tmp[n]); |
| 578 | |
| 579 | tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE)); |
| 580 | idx &= ~mask; |
| 581 | mask >>= shift; |
| 582 | --level; |
| 583 | } |
Alexey Kardashevskiy | c5bb44e | 2015-06-05 16:35:14 +1000 | [diff] [blame] | 584 | |
| 585 | return tmp + idx; |
| 586 | } |
| 587 | |
Alexey Kardashevskiy | da004c3 | 2015-06-05 16:35:06 +1000 | [diff] [blame] | 588 | int pnv_tce_build(struct iommu_table *tbl, long index, long npages, |
| 589 | unsigned long uaddr, enum dma_data_direction direction, |
| 590 | struct dma_attrs *attrs) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 591 | { |
Alexey Kardashevskiy | 10b35b2 | 2015-06-05 16:35:05 +1000 | [diff] [blame] | 592 | u64 proto_tce = iommu_direction_to_tce_perm(direction); |
Alexey Kardashevskiy | c5bb44e | 2015-06-05 16:35:14 +1000 | [diff] [blame] | 593 | u64 rpn = __pa(uaddr) >> tbl->it_page_shift; |
| 594 | long i; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 595 | |
Alexey Kardashevskiy | 6ecad91 | 2016-02-17 18:26:31 +1100 | [diff] [blame] | 596 | if (proto_tce & TCE_PCI_WRITE) |
| 597 | proto_tce |= TCE_PCI_READ; |
| 598 | |
Alexey Kardashevskiy | c5bb44e | 2015-06-05 16:35:14 +1000 | [diff] [blame] | 599 | for (i = 0; i < npages; i++) { |
| 600 | unsigned long newtce = proto_tce | |
| 601 | ((rpn + i) << tbl->it_page_shift); |
| 602 | unsigned long idx = index - tbl->it_offset + i; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 603 | |
Alexey Kardashevskiy | c5bb44e | 2015-06-05 16:35:14 +1000 | [diff] [blame] | 604 | *(pnv_tce(tbl, idx)) = cpu_to_be64(newtce); |
| 605 | } |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 606 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
Alexey Kardashevskiy | 05c6cfb | 2015-06-05 16:35:15 +1000 | [diff] [blame] | 610 | #ifdef CONFIG_IOMMU_API |
| 611 | int pnv_tce_xchg(struct iommu_table *tbl, long index, |
| 612 | unsigned long *hpa, enum dma_data_direction *direction) |
| 613 | { |
| 614 | u64 proto_tce = iommu_direction_to_tce_perm(*direction); |
| 615 | unsigned long newtce = *hpa | proto_tce, oldtce; |
| 616 | unsigned long idx = index - tbl->it_offset; |
| 617 | |
| 618 | BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl)); |
| 619 | |
Alexey Kardashevskiy | 6ecad91 | 2016-02-17 18:26:31 +1100 | [diff] [blame] | 620 | if (newtce & TCE_PCI_WRITE) |
| 621 | newtce |= TCE_PCI_READ; |
| 622 | |
Alexey Kardashevskiy | 05c6cfb | 2015-06-05 16:35:15 +1000 | [diff] [blame] | 623 | oldtce = xchg(pnv_tce(tbl, idx), cpu_to_be64(newtce)); |
| 624 | *hpa = be64_to_cpu(oldtce) & ~(TCE_PCI_READ | TCE_PCI_WRITE); |
| 625 | *direction = iommu_tce_direction(oldtce); |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | #endif |
| 630 | |
Alexey Kardashevskiy | da004c3 | 2015-06-05 16:35:06 +1000 | [diff] [blame] | 631 | void pnv_tce_free(struct iommu_table *tbl, long index, long npages) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 632 | { |
Alexey Kardashevskiy | c5bb44e | 2015-06-05 16:35:14 +1000 | [diff] [blame] | 633 | long i; |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 634 | |
Alexey Kardashevskiy | c5bb44e | 2015-06-05 16:35:14 +1000 | [diff] [blame] | 635 | for (i = 0; i < npages; i++) { |
| 636 | unsigned long idx = index - tbl->it_offset + i; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 637 | |
Alexey Kardashevskiy | c5bb44e | 2015-06-05 16:35:14 +1000 | [diff] [blame] | 638 | *(pnv_tce(tbl, idx)) = cpu_to_be64(0); |
| 639 | } |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 640 | } |
| 641 | |
Alexey Kardashevskiy | da004c3 | 2015-06-05 16:35:06 +1000 | [diff] [blame] | 642 | unsigned long pnv_tce_get(struct iommu_table *tbl, long index) |
Alexey Kardashevskiy | 11f63d3 | 2012-09-04 15:19:35 +0000 | [diff] [blame] | 643 | { |
Alexey Kardashevskiy | c5bb44e | 2015-06-05 16:35:14 +1000 | [diff] [blame] | 644 | return *(pnv_tce(tbl, index - tbl->it_offset)); |
Alexey Kardashevskiy | 11f63d3 | 2012-09-04 15:19:35 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Alexey Kardashevskiy | 0eaf4de | 2015-06-05 16:35:09 +1000 | [diff] [blame] | 647 | struct iommu_table *pnv_pci_table_alloc(int nid) |
| 648 | { |
| 649 | struct iommu_table *tbl; |
| 650 | |
| 651 | tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid); |
| 652 | INIT_LIST_HEAD_RCU(&tbl->it_group_list); |
| 653 | |
| 654 | return tbl; |
| 655 | } |
| 656 | |
| 657 | long pnv_pci_link_table_and_group(int node, int num, |
| 658 | struct iommu_table *tbl, |
| 659 | struct iommu_table_group *table_group) |
| 660 | { |
| 661 | struct iommu_table_group_link *tgl = NULL; |
| 662 | |
| 663 | if (WARN_ON(!tbl || !table_group)) |
| 664 | return -EINVAL; |
| 665 | |
| 666 | tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL, |
| 667 | node); |
| 668 | if (!tgl) |
| 669 | return -ENOMEM; |
| 670 | |
| 671 | tgl->table_group = table_group; |
| 672 | list_add_rcu(&tgl->next, &tbl->it_group_list); |
| 673 | |
| 674 | table_group->tables[num] = tbl; |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | static void pnv_iommu_table_group_link_free(struct rcu_head *head) |
| 680 | { |
| 681 | struct iommu_table_group_link *tgl = container_of(head, |
| 682 | struct iommu_table_group_link, rcu); |
| 683 | |
| 684 | kfree(tgl); |
| 685 | } |
| 686 | |
| 687 | void pnv_pci_unlink_table_and_group(struct iommu_table *tbl, |
| 688 | struct iommu_table_group *table_group) |
| 689 | { |
| 690 | long i; |
| 691 | bool found; |
| 692 | struct iommu_table_group_link *tgl; |
| 693 | |
| 694 | if (!tbl || !table_group) |
| 695 | return; |
| 696 | |
| 697 | /* Remove link to a group from table's list of attached groups */ |
| 698 | found = false; |
| 699 | list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) { |
| 700 | if (tgl->table_group == table_group) { |
| 701 | list_del_rcu(&tgl->next); |
| 702 | call_rcu(&tgl->rcu, pnv_iommu_table_group_link_free); |
| 703 | found = true; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | if (WARN_ON(!found)) |
| 708 | return; |
| 709 | |
| 710 | /* Clean a pointer to iommu_table in iommu_table_group::tables[] */ |
| 711 | found = false; |
| 712 | for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) { |
| 713 | if (table_group->tables[i] == tbl) { |
| 714 | table_group->tables[i] = NULL; |
| 715 | found = true; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | WARN_ON(!found); |
| 720 | } |
| 721 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 722 | void pnv_pci_setup_iommu_table(struct iommu_table *tbl, |
| 723 | void *tce_mem, u64 tce_size, |
Alexey Kardashevskiy | 8fa5d45 | 2014-06-06 18:44:03 +1000 | [diff] [blame] | 724 | u64 dma_offset, unsigned page_shift) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 725 | { |
| 726 | tbl->it_blocksize = 16; |
| 727 | tbl->it_base = (unsigned long)tce_mem; |
Alexey Kardashevskiy | 8fa5d45 | 2014-06-06 18:44:03 +1000 | [diff] [blame] | 728 | tbl->it_page_shift = page_shift; |
Alistair Popple | 3a55317 | 2013-12-09 18:17:02 +1100 | [diff] [blame] | 729 | tbl->it_offset = dma_offset >> tbl->it_page_shift; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 730 | tbl->it_index = 0; |
| 731 | tbl->it_size = tce_size >> 3; |
| 732 | tbl->it_busno = 0; |
| 733 | tbl->it_type = TCE_PCI; |
| 734 | } |
| 735 | |
Daniel Axtens | 92ae035 | 2015-04-28 15:12:05 +1000 | [diff] [blame] | 736 | void pnv_pci_dma_dev_setup(struct pci_dev *pdev) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 737 | { |
| 738 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 739 | struct pnv_phb *phb = hose->private_data; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 740 | #ifdef CONFIG_PCI_IOV |
| 741 | struct pnv_ioda_pe *pe; |
| 742 | struct pci_dn *pdn; |
| 743 | |
| 744 | /* Fix the VF pdn PE number */ |
| 745 | if (pdev->is_virtfn) { |
| 746 | pdn = pci_get_pdn(pdev); |
| 747 | WARN_ON(pdn->pe_number != IODA_INVALID_PE); |
| 748 | list_for_each_entry(pe, &phb->ioda.pe_list, list) { |
| 749 | if (pe->rid == ((pdev->bus->number << 8) | |
| 750 | (pdev->devfn & 0xff))) { |
| 751 | pdn->pe_number = pe->pe_number; |
| 752 | pe->pdev = pdev; |
| 753 | break; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | #endif /* CONFIG_PCI_IOV */ |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 758 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 759 | if (phb && phb->dma_dev_setup) |
| 760 | phb->dma_dev_setup(phb, pdev); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Gavin Shan | 1bc74f1 | 2016-02-09 15:50:22 +1100 | [diff] [blame] | 763 | void pnv_pci_dma_bus_setup(struct pci_bus *bus) |
| 764 | { |
| 765 | struct pci_controller *hose = bus->sysdata; |
| 766 | struct pnv_phb *phb = hose->private_data; |
| 767 | struct pnv_ioda_pe *pe; |
| 768 | |
| 769 | list_for_each_entry(pe, &phb->ioda.pe_list, list) { |
| 770 | if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))) |
| 771 | continue; |
| 772 | |
| 773 | if (!pe->pbus) |
| 774 | continue; |
| 775 | |
| 776 | if (bus->number == ((pe->rid >> 8) & 0xFF)) { |
| 777 | pe->pbus = bus; |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 783 | void pnv_pci_shutdown(void) |
| 784 | { |
| 785 | struct pci_controller *hose; |
| 786 | |
Michael Neuling | 7a8e6bb | 2015-05-27 16:06:59 +1000 | [diff] [blame] | 787 | list_for_each_entry(hose, &hose_list, list_node) |
| 788 | if (hose->controller_ops.shutdown) |
| 789 | hose->controller_ops.shutdown(hose); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 790 | } |
| 791 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 792 | /* Fixup wrong class code in p7ioc and p8 root complex */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 793 | static void pnv_p7ioc_rc_quirk(struct pci_dev *dev) |
Benjamin Herrenschmidt | ca45cfe | 2011-11-06 18:56:00 +0000 | [diff] [blame] | 794 | { |
| 795 | dev->class = PCI_CLASS_BRIDGE_PCI << 8; |
| 796 | } |
| 797 | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk); |
| 798 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 799 | void __init pnv_pci_init(void) |
| 800 | { |
| 801 | struct device_node *np; |
| 802 | |
Bjorn Helgaas | 673c975 | 2012-02-23 20:18:58 -0700 | [diff] [blame] | 803 | pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 804 | |
Michael Ellerman | 646b54f | 2015-03-12 17:27:11 +1100 | [diff] [blame] | 805 | /* If we don't have OPAL, eg. in sim, just skip PCI probe */ |
| 806 | if (!firmware_has_feature(FW_FEATURE_OPAL)) |
| 807 | return; |
| 808 | |
Russell Currey | 2de50e9 | 2016-02-08 15:08:20 +1100 | [diff] [blame] | 809 | /* Look for IODA IO-Hubs. */ |
Michael Ellerman | 646b54f | 2015-03-12 17:27:11 +1100 | [diff] [blame] | 810 | for_each_compatible_node(np, NULL, "ibm,ioda-hub") { |
| 811 | pnv_pci_init_ioda_hub(np); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 812 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 813 | |
Michael Ellerman | 646b54f | 2015-03-12 17:27:11 +1100 | [diff] [blame] | 814 | /* Look for ioda2 built-in PHB3's */ |
| 815 | for_each_compatible_node(np, NULL, "ibm,ioda2-phb") |
| 816 | pnv_pci_init_ioda2_phb(np); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 817 | |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 818 | /* Look for NPU PHBs */ |
| 819 | for_each_compatible_node(np, NULL, "ibm,ioda2-npu-phb") |
| 820 | pnv_pci_init_npu_phb(np); |
| 821 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 822 | /* Setup the linkage between OF nodes and PHBs */ |
| 823 | pci_devs_phb_init(); |
| 824 | |
| 825 | /* Configure IOMMU DMA hooks */ |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 826 | set_pci_dma_ops(&dma_iommu_ops); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 827 | } |
Alexey Kardashevskiy | d905c5d | 2013-11-21 17:43:14 +1100 | [diff] [blame] | 828 | |
Michael Ellerman | b14726c | 2014-07-15 22:22:24 +1000 | [diff] [blame] | 829 | machine_subsys_initcall_sync(powernv, tce_iommu_bus_notifier_init); |