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