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", |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 209 | hose->global_number, 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", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 212 | data->brdgCtl); |
| 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", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 216 | data->portStatusReg, data->rootCmplxStatus, |
| 217 | data->busAgentStatus); |
| 218 | if (data->deviceStatus || data->slotStatus || |
| 219 | data->linkStatus || data->devCmdStatus || |
| 220 | data->devSecStatus) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 221 | pr_info("RootSts: %08x %08x %08x %08x %08x\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 222 | data->deviceStatus, data->slotStatus, |
| 223 | data->linkStatus, data->devCmdStatus, |
| 224 | data->devSecStatus); |
| 225 | if (data->rootErrorStatus || data->uncorrErrorStatus || |
| 226 | data->corrErrorStatus) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 227 | pr_info("RootErrSts: %08x %08x %08x\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 228 | data->rootErrorStatus, data->uncorrErrorStatus, |
| 229 | data->corrErrorStatus); |
| 230 | if (data->tlpHdr1 || data->tlpHdr2 || |
| 231 | data->tlpHdr3 || data->tlpHdr4) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 232 | pr_info("RootErrLog: %08x %08x %08x %08x\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 233 | data->tlpHdr1, data->tlpHdr2, |
| 234 | data->tlpHdr3, data->tlpHdr4); |
| 235 | if (data->sourceId || data->errorClass || |
| 236 | data->correlator) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 237 | pr_info("RootErrLog1: %08x %016llx %016llx\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 238 | data->sourceId, data->errorClass, |
| 239 | data->correlator); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 240 | if (data->nFir) |
| 241 | pr_info("nFir: %016llx %016llx %016llx\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 242 | data->nFir, data->nFirMask, |
| 243 | data->nFirWOF); |
| 244 | if (data->phbPlssr || data->phbCsr) |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 245 | pr_info("PhbSts: %016llx %016llx\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 246 | data->phbPlssr, data->phbCsr); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 247 | if (data->lemFir) |
| 248 | pr_info("Lem: %016llx %016llx %016llx\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 249 | data->lemFir, data->lemErrorMask, |
| 250 | data->lemWOF); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 251 | if (data->phbErrorStatus) |
| 252 | pr_info("PhbErr: %016llx %016llx %016llx %016llx\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 253 | data->phbErrorStatus, data->phbFirstErrorStatus, |
| 254 | data->phbErrorLog0, data->phbErrorLog1); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 255 | if (data->mmioErrorStatus) |
| 256 | pr_info("OutErr: %016llx %016llx %016llx %016llx\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 257 | data->mmioErrorStatus, data->mmioFirstErrorStatus, |
| 258 | data->mmioErrorLog0, data->mmioErrorLog1); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 259 | if (data->dma0ErrorStatus) |
| 260 | pr_info("InAErr: %016llx %016llx %016llx %016llx\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 261 | data->dma0ErrorStatus, data->dma0FirstErrorStatus, |
| 262 | data->dma0ErrorLog0, data->dma0ErrorLog1); |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 263 | if (data->dma1ErrorStatus) |
| 264 | pr_info("InBErr: %016llx %016llx %016llx %016llx\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 265 | data->dma1ErrorStatus, data->dma1FirstErrorStatus, |
| 266 | data->dma1ErrorLog0, data->dma1ErrorLog1); |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 267 | |
| 268 | for (i = 0; i < OPAL_PHB3_NUM_PEST_REGS; i++) { |
| 269 | if ((data->pestA[i] >> 63) == 0 && |
| 270 | (data->pestB[i] >> 63) == 0) |
| 271 | continue; |
| 272 | |
Gavin Shan | b34497d | 2014-04-24 18:00:10 +1000 | [diff] [blame^] | 273 | pr_info("PE[%3d] A/B: %016llx %016llx\n", |
Gavin Shan | af87d2f | 2014-02-25 15:28:38 +0800 | [diff] [blame] | 274 | i, data->pestA[i], data->pestB[i]); |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | void pnv_pci_dump_phb_diag_data(struct pci_controller *hose, |
| 279 | unsigned char *log_buff) |
| 280 | { |
| 281 | struct OpalIoPhbErrorCommon *common; |
| 282 | |
| 283 | if (!hose || !log_buff) |
| 284 | return; |
| 285 | |
| 286 | common = (struct OpalIoPhbErrorCommon *)log_buff; |
| 287 | switch (common->ioType) { |
| 288 | case OPAL_PHB_ERROR_DATA_TYPE_P7IOC: |
| 289 | pnv_pci_dump_p7ioc_diag_data(hose, common); |
| 290 | break; |
| 291 | case OPAL_PHB_ERROR_DATA_TYPE_PHB3: |
| 292 | pnv_pci_dump_phb3_diag_data(hose, common); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 293 | break; |
| 294 | default: |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 295 | pr_warn("%s: Unrecognized ioType %d\n", |
| 296 | __func__, common->ioType); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no) |
| 301 | { |
| 302 | unsigned long flags, rc; |
| 303 | int has_diag; |
| 304 | |
| 305 | spin_lock_irqsave(&phb->lock, flags); |
| 306 | |
Gavin Shan | 2377323 | 2013-06-20 13:21:05 +0800 | [diff] [blame] | 307 | rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob, |
| 308 | PNV_PCI_DIAG_BUF_SIZE); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 309 | has_diag = (rc == OPAL_SUCCESS); |
| 310 | |
| 311 | rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, |
| 312 | OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); |
| 313 | if (rc) { |
| 314 | pr_warning("PCI %d: Failed to clear EEH freeze state" |
| 315 | " for PE#%d, err %ld\n", |
| 316 | phb->hose->global_number, pe_no, rc); |
| 317 | |
| 318 | /* For now, let's only display the diag buffer when we fail to clear |
| 319 | * the EEH status. We'll do more sensible things later when we have |
| 320 | * proper EEH support. We need to make sure we don't pollute ourselves |
| 321 | * with the normal errors generated when probing empty slots |
| 322 | */ |
| 323 | if (has_diag) |
Gavin Shan | 93aef2a | 2013-11-22 16:28:45 +0800 | [diff] [blame] | 324 | pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 325 | else |
| 326 | pr_warning("PCI %d: No diag data available\n", |
| 327 | phb->hose->global_number); |
| 328 | } |
| 329 | |
| 330 | spin_unlock_irqrestore(&phb->lock, flags); |
| 331 | } |
| 332 | |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 333 | static void pnv_pci_config_check_eeh(struct pnv_phb *phb, |
| 334 | struct device_node *dn) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 335 | { |
| 336 | s64 rc; |
| 337 | u8 fstate; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 338 | __be16 pcierr; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 339 | u32 pe_no; |
| 340 | |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 341 | /* |
| 342 | * Get the PE#. During the PCI probe stage, we might not |
| 343 | * setup that yet. So all ER errors should be mapped to |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 344 | * reserved PE. |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 345 | */ |
| 346 | pe_no = PCI_DN(dn)->pe_number; |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 347 | if (pe_no == IODA_INVALID_PE) { |
| 348 | if (phb->type == PNV_PHB_P5IOC2) |
| 349 | pe_no = 0; |
| 350 | else |
| 351 | pe_no = phb->ioda.reserved_pe; |
| 352 | } |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 353 | |
| 354 | /* Read freeze status */ |
| 355 | rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, &fstate, &pcierr, |
| 356 | NULL); |
| 357 | if (rc) { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 358 | pr_warning("%s: Can't read EEH status (PE#%d) for " |
| 359 | "%s, err %lld\n", |
| 360 | __func__, pe_no, dn->full_name, rc); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 361 | return; |
| 362 | } |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 363 | cfg_dbg(" -> EEH check, bdfn=%04x PE#%d fstate=%x\n", |
| 364 | (PCI_DN(dn)->busno << 8) | (PCI_DN(dn)->devfn), |
| 365 | pe_no, fstate); |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 366 | if (fstate != 0) |
| 367 | pnv_pci_handle_eeh_config(phb, pe_no); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 370 | int pnv_pci_cfg_read(struct device_node *dn, |
| 371 | int where, int size, u32 *val) |
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 | struct pci_dn *pdn = PCI_DN(dn); |
| 374 | struct pnv_phb *phb = pdn->phb->private_data; |
| 375 | u32 bdfn = (pdn->busno << 8) | pdn->devfn; |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 376 | #ifdef CONFIG_EEH |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 377 | struct eeh_pe *phb_pe = NULL; |
| 378 | #endif |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 379 | s64 rc; |
| 380 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 381 | switch (size) { |
| 382 | case 1: { |
| 383 | u8 v8; |
| 384 | rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8); |
| 385 | *val = (rc == OPAL_SUCCESS) ? v8 : 0xff; |
| 386 | break; |
| 387 | } |
| 388 | case 2: { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 389 | __be16 v16; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 390 | rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where, |
| 391 | &v16); |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 392 | *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 393 | break; |
| 394 | } |
| 395 | case 4: { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 396 | __be32 v32; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 397 | rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32); |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 398 | *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 399 | break; |
| 400 | } |
| 401 | default: |
| 402 | return PCIBIOS_FUNC_NOT_SUPPORTED; |
| 403 | } |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 404 | cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n", |
| 405 | __func__, pdn->busno, pdn->devfn, where, size, *val); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 406 | |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 407 | /* |
| 408 | * Check if the specified PE has been put into frozen |
| 409 | * state. On the other hand, we needn't do that while |
| 410 | * the PHB has been put into frozen state because of |
| 411 | * PHB-fatal errors. |
| 412 | */ |
| 413 | #ifdef CONFIG_EEH |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 414 | phb_pe = eeh_phb_pe_get(pdn->phb); |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 415 | if (phb_pe && (phb_pe->state & EEH_PE_ISOLATED)) |
| 416 | return PCIBIOS_SUCCESSFUL; |
| 417 | |
Gavin Shan | f5bc6b7 | 2014-04-24 18:00:09 +1000 | [diff] [blame] | 418 | if (phb->flags & PNV_PHB_FLAG_EEH) { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 419 | if (*val == EEH_IO_ERROR_VALUE(size) && |
| 420 | eeh_dev_check_failure(of_node_to_eeh_dev(dn))) |
| 421 | return PCIBIOS_DEVICE_NOT_FOUND; |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 422 | } else { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 423 | pnv_pci_config_check_eeh(phb, dn); |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 424 | } |
| 425 | #else |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 426 | pnv_pci_config_check_eeh(phb, dn); |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 427 | #endif |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 428 | |
| 429 | return PCIBIOS_SUCCESSFUL; |
| 430 | } |
| 431 | |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 432 | int pnv_pci_cfg_write(struct device_node *dn, |
| 433 | int where, int size, u32 val) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 434 | { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 435 | struct pci_dn *pdn = PCI_DN(dn); |
| 436 | struct pnv_phb *phb = pdn->phb->private_data; |
| 437 | u32 bdfn = (pdn->busno << 8) | pdn->devfn; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 438 | |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 439 | cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n", |
| 440 | pdn->busno, pdn->devfn, where, size, val); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 441 | switch (size) { |
| 442 | case 1: |
| 443 | opal_pci_config_write_byte(phb->opal_id, bdfn, where, val); |
| 444 | break; |
| 445 | case 2: |
| 446 | opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val); |
| 447 | break; |
| 448 | case 4: |
| 449 | opal_pci_config_write_word(phb->opal_id, bdfn, where, val); |
| 450 | break; |
| 451 | default: |
| 452 | return PCIBIOS_FUNC_NOT_SUPPORTED; |
| 453 | } |
Gavin Shan | be7e744 | 2013-06-20 13:21:15 +0800 | [diff] [blame] | 454 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 455 | /* Check if the PHB got frozen due to an error (no response) */ |
Gavin Shan | f5bc6b7 | 2014-04-24 18:00:09 +1000 | [diff] [blame] | 456 | if (!(phb->flags & PNV_PHB_FLAG_EEH)) |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 457 | pnv_pci_config_check_eeh(phb, dn); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 458 | |
| 459 | return PCIBIOS_SUCCESSFUL; |
| 460 | } |
| 461 | |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 462 | static int pnv_pci_read_config(struct pci_bus *bus, |
| 463 | unsigned int devfn, |
| 464 | int where, int size, u32 *val) |
| 465 | { |
| 466 | struct device_node *dn, *busdn = pci_bus_to_OF_node(bus); |
| 467 | struct pci_dn *pdn; |
| 468 | |
| 469 | for (dn = busdn->child; dn; dn = dn->sibling) { |
| 470 | pdn = PCI_DN(dn); |
| 471 | if (pdn && pdn->devfn == devfn) |
| 472 | return pnv_pci_cfg_read(dn, where, size, val); |
| 473 | } |
| 474 | |
| 475 | *val = 0xFFFFFFFF; |
| 476 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 477 | |
| 478 | } |
| 479 | |
| 480 | static int pnv_pci_write_config(struct pci_bus *bus, |
| 481 | unsigned int devfn, |
| 482 | int where, int size, u32 val) |
| 483 | { |
| 484 | struct device_node *dn, *busdn = pci_bus_to_OF_node(bus); |
| 485 | struct pci_dn *pdn; |
| 486 | |
| 487 | for (dn = busdn->child; dn; dn = dn->sibling) { |
| 488 | pdn = PCI_DN(dn); |
| 489 | if (pdn && pdn->devfn == devfn) |
| 490 | return pnv_pci_cfg_write(dn, where, size, val); |
| 491 | } |
| 492 | |
| 493 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 494 | } |
| 495 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 496 | struct pci_ops pnv_pci_ops = { |
Gavin Shan | 9bf41be | 2013-06-27 13:46:48 +0800 | [diff] [blame] | 497 | .read = pnv_pci_read_config, |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 498 | .write = pnv_pci_write_config, |
| 499 | }; |
| 500 | |
| 501 | static int pnv_tce_build(struct iommu_table *tbl, long index, long npages, |
| 502 | unsigned long uaddr, enum dma_data_direction direction, |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 503 | struct dma_attrs *attrs, bool rm) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 504 | { |
| 505 | u64 proto_tce; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 506 | __be64 *tcep, *tces; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 507 | u64 rpn; |
| 508 | |
| 509 | proto_tce = TCE_PCI_READ; // Read allowed |
| 510 | |
| 511 | if (direction != DMA_TO_DEVICE) |
| 512 | proto_tce |= TCE_PCI_WRITE; |
| 513 | |
Anton Blanchard | 5e4da53 | 2013-09-23 12:05:06 +1000 | [diff] [blame] | 514 | tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset; |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 515 | rpn = __pa(uaddr) >> TCE_SHIFT; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 516 | |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 517 | while (npages--) |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 518 | *(tcep++) = cpu_to_be64(proto_tce | (rpn++ << TCE_RPN_SHIFT)); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 519 | |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 520 | /* Some implementations won't cache invalid TCEs and thus may not |
| 521 | * need that flush. We'll probably turn it_type into a bit mask |
| 522 | * of flags if that becomes the case |
| 523 | */ |
| 524 | if (tbl->it_type & TCE_PCI_SWINV_CREATE) |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 525 | pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm); |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 526 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 530 | static int pnv_tce_build_vm(struct iommu_table *tbl, long index, long npages, |
| 531 | unsigned long uaddr, |
| 532 | enum dma_data_direction direction, |
| 533 | struct dma_attrs *attrs) |
| 534 | { |
| 535 | return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs, |
| 536 | false); |
| 537 | } |
| 538 | |
| 539 | static void pnv_tce_free(struct iommu_table *tbl, long index, long npages, |
| 540 | bool rm) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 541 | { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 542 | __be64 *tcep, *tces; |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 543 | |
Anton Blanchard | 5e4da53 | 2013-09-23 12:05:06 +1000 | [diff] [blame] | 544 | tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 545 | |
| 546 | while (npages--) |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 547 | *(tcep++) = cpu_to_be64(0); |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 548 | |
Benjamin Herrenschmidt | 605e44d | 2013-05-20 17:25:15 +0000 | [diff] [blame] | 549 | if (tbl->it_type & TCE_PCI_SWINV_FREE) |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 550 | pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm); |
| 551 | } |
| 552 | |
| 553 | static void pnv_tce_free_vm(struct iommu_table *tbl, long index, long npages) |
| 554 | { |
| 555 | pnv_tce_free(tbl, index, npages, false); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Alexey Kardashevskiy | 11f63d3 | 2012-09-04 15:19:35 +0000 | [diff] [blame] | 558 | static unsigned long pnv_tce_get(struct iommu_table *tbl, long index) |
| 559 | { |
| 560 | return ((u64 *)tbl->it_base)[index - tbl->it_offset]; |
| 561 | } |
| 562 | |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 563 | static int pnv_tce_build_rm(struct iommu_table *tbl, long index, long npages, |
| 564 | unsigned long uaddr, |
| 565 | enum dma_data_direction direction, |
| 566 | struct dma_attrs *attrs) |
| 567 | { |
| 568 | return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs, true); |
| 569 | } |
| 570 | |
| 571 | static void pnv_tce_free_rm(struct iommu_table *tbl, long index, long npages) |
| 572 | { |
| 573 | pnv_tce_free(tbl, index, npages, true); |
| 574 | } |
| 575 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 576 | void pnv_pci_setup_iommu_table(struct iommu_table *tbl, |
| 577 | void *tce_mem, u64 tce_size, |
| 578 | u64 dma_offset) |
| 579 | { |
| 580 | tbl->it_blocksize = 16; |
| 581 | tbl->it_base = (unsigned long)tce_mem; |
Alistair Popple | 3a55317 | 2013-12-09 18:17:02 +1100 | [diff] [blame] | 582 | tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K; |
| 583 | tbl->it_offset = dma_offset >> tbl->it_page_shift; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 584 | tbl->it_index = 0; |
| 585 | tbl->it_size = tce_size >> 3; |
| 586 | tbl->it_busno = 0; |
| 587 | tbl->it_type = TCE_PCI; |
| 588 | } |
| 589 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 590 | 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] | 591 | { |
| 592 | struct iommu_table *tbl; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 593 | const __be64 *basep, *swinvp; |
| 594 | const __be32 *sizep; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 595 | |
| 596 | basep = of_get_property(hose->dn, "linux,tce-base", NULL); |
| 597 | sizep = of_get_property(hose->dn, "linux,tce-size", NULL); |
| 598 | if (basep == NULL || sizep == NULL) { |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 599 | pr_err("PCI: %s has missing tce entries !\n", |
| 600 | hose->dn->full_name); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 601 | return NULL; |
| 602 | } |
| 603 | tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node); |
| 604 | if (WARN_ON(!tbl)) |
| 605 | return NULL; |
| 606 | pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)), |
| 607 | be32_to_cpup(sizep), 0); |
| 608 | iommu_init_table(tbl, hose->node); |
Alexey Kardashevskiy | 4e13c1a | 2013-05-21 13:33:09 +1000 | [diff] [blame] | 609 | iommu_register_group(tbl, pci_domain_nr(hose->bus), 0); |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 610 | |
| 611 | /* Deal with SW invalidated TCEs when needed (BML way) */ |
| 612 | swinvp = of_get_property(hose->dn, "linux,tce-sw-invalidate-info", |
| 613 | NULL); |
| 614 | if (swinvp) { |
Anton Blanchard | 5e4da53 | 2013-09-23 12:05:06 +1000 | [diff] [blame] | 615 | tbl->it_busno = be64_to_cpu(swinvp[1]); |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 616 | tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8); |
Benjamin Herrenschmidt | 1f1616e | 2011-11-06 18:55:59 +0000 | [diff] [blame] | 617 | tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE; |
| 618 | } |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 619 | return tbl; |
| 620 | } |
| 621 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 622 | static void pnv_pci_dma_fallback_setup(struct pci_controller *hose, |
| 623 | struct pci_dev *pdev) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 624 | { |
| 625 | struct device_node *np = pci_bus_to_OF_node(hose->bus); |
| 626 | struct pci_dn *pdn; |
| 627 | |
| 628 | if (np == NULL) |
| 629 | return; |
| 630 | pdn = PCI_DN(np); |
| 631 | if (!pdn->iommu_table) |
| 632 | pdn->iommu_table = pnv_pci_setup_bml_iommu(hose); |
| 633 | if (!pdn->iommu_table) |
| 634 | return; |
Alexey Kardashevskiy | d905c5d | 2013-11-21 17:43:14 +1100 | [diff] [blame] | 635 | set_iommu_table_base_and_group(&pdev->dev, pdn->iommu_table); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 638 | static void pnv_pci_dma_dev_setup(struct pci_dev *pdev) |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 639 | { |
| 640 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 641 | struct pnv_phb *phb = hose->private_data; |
| 642 | |
| 643 | /* If we have no phb structure, try to setup a fallback based on |
| 644 | * the device-tree (RTAS PCI for example) |
| 645 | */ |
| 646 | if (phb && phb->dma_dev_setup) |
| 647 | phb->dma_dev_setup(phb, pdev); |
| 648 | else |
| 649 | pnv_pci_dma_fallback_setup(hose, pdev); |
| 650 | } |
| 651 | |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 652 | int pnv_pci_dma_set_mask(struct pci_dev *pdev, u64 dma_mask) |
| 653 | { |
| 654 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 655 | struct pnv_phb *phb = hose->private_data; |
| 656 | |
| 657 | if (phb && phb->dma_set_mask) |
| 658 | return phb->dma_set_mask(phb, pdev, dma_mask); |
| 659 | return __dma_set_mask(&pdev->dev, dma_mask); |
| 660 | } |
| 661 | |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 662 | void pnv_pci_shutdown(void) |
| 663 | { |
| 664 | struct pci_controller *hose; |
| 665 | |
| 666 | list_for_each_entry(hose, &hose_list, list_node) { |
| 667 | struct pnv_phb *phb = hose->private_data; |
| 668 | |
| 669 | if (phb && phb->shutdown) |
| 670 | phb->shutdown(phb); |
| 671 | } |
| 672 | } |
| 673 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 674 | /* Fixup wrong class code in p7ioc and p8 root complex */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 675 | static void pnv_p7ioc_rc_quirk(struct pci_dev *dev) |
Benjamin Herrenschmidt | ca45cfe | 2011-11-06 18:56:00 +0000 | [diff] [blame] | 676 | { |
| 677 | dev->class = PCI_CLASS_BRIDGE_PCI << 8; |
| 678 | } |
| 679 | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk); |
| 680 | |
Benjamin Herrenschmidt | 82ba129 | 2011-09-19 17:45:07 +0000 | [diff] [blame] | 681 | static int pnv_pci_probe_mode(struct pci_bus *bus) |
| 682 | { |
| 683 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 684 | const __be64 *tstamp; |
| 685 | u64 now, target; |
| 686 | |
| 687 | |
| 688 | /* We hijack this as a way to ensure we have waited long |
| 689 | * enough since the reset was lifted on the PCI bus |
| 690 | */ |
| 691 | if (bus != hose->bus) |
| 692 | return PCI_PROBE_NORMAL; |
| 693 | tstamp = of_get_property(hose->dn, "reset-clear-timestamp", NULL); |
| 694 | if (!tstamp || !*tstamp) |
| 695 | return PCI_PROBE_NORMAL; |
| 696 | |
| 697 | now = mftb() / tb_ticks_per_usec; |
| 698 | target = (be64_to_cpup(tstamp) / tb_ticks_per_usec) |
| 699 | + PCI_RESET_DELAY_US; |
| 700 | |
| 701 | pr_devel("pci %04d: Reset target: 0x%llx now: 0x%llx\n", |
| 702 | hose->global_number, target, now); |
| 703 | |
| 704 | if (now < target) |
| 705 | msleep((target - now + 999) / 1000); |
| 706 | |
| 707 | return PCI_PROBE_NORMAL; |
| 708 | } |
| 709 | |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 710 | void __init pnv_pci_init(void) |
| 711 | { |
| 712 | struct device_node *np; |
| 713 | |
Bjorn Helgaas | 673c975 | 2012-02-23 20:18:58 -0700 | [diff] [blame] | 714 | pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 715 | |
| 716 | /* OPAL absent, try POPAL first then RTAS detection of PHBs */ |
| 717 | if (!firmware_has_feature(FW_FEATURE_OPAL)) { |
| 718 | #ifdef CONFIG_PPC_POWERNV_RTAS |
| 719 | init_pci_config_tokens(); |
| 720 | find_and_init_phbs(); |
| 721 | #endif /* CONFIG_PPC_POWERNV_RTAS */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 722 | } |
| 723 | /* OPAL is here, do our normal stuff */ |
| 724 | else { |
| 725 | int found_ioda = 0; |
| 726 | |
| 727 | /* Look for IODA IO-Hubs. We don't support mixing IODA |
| 728 | * and p5ioc2 due to the need to change some global |
| 729 | * probing flags |
| 730 | */ |
| 731 | for_each_compatible_node(np, NULL, "ibm,ioda-hub") { |
| 732 | pnv_pci_init_ioda_hub(np); |
| 733 | found_ioda = 1; |
| 734 | } |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 735 | |
| 736 | /* Look for p5ioc2 IO-Hubs */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 737 | if (!found_ioda) |
| 738 | for_each_compatible_node(np, NULL, "ibm,p5ioc2") |
| 739 | pnv_pci_init_p5ioc2_hub(np); |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 740 | |
| 741 | /* Look for ioda2 built-in PHB3's */ |
| 742 | for_each_compatible_node(np, NULL, "ibm,ioda2-phb") |
| 743 | pnv_pci_init_ioda2_phb(np); |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* Setup the linkage between OF nodes and PHBs */ |
| 747 | pci_devs_phb_init(); |
| 748 | |
| 749 | /* Configure IOMMU DMA hooks */ |
| 750 | ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup; |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 751 | ppc_md.tce_build = pnv_tce_build_vm; |
| 752 | ppc_md.tce_free = pnv_tce_free_vm; |
| 753 | ppc_md.tce_build_rm = pnv_tce_build_rm; |
| 754 | ppc_md.tce_free_rm = pnv_tce_free_rm; |
Alexey Kardashevskiy | 11f63d3 | 2012-09-04 15:19:35 +0000 | [diff] [blame] | 755 | ppc_md.tce_get = pnv_tce_get; |
Benjamin Herrenschmidt | 82ba129 | 2011-09-19 17:45:07 +0000 | [diff] [blame] | 756 | ppc_md.pci_probe_mode = pnv_pci_probe_mode; |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 757 | set_pci_dma_ops(&dma_iommu_ops); |
| 758 | |
Benjamin Herrenschmidt | c1a2562 | 2011-09-19 17:45:06 +0000 | [diff] [blame] | 759 | /* Configure MSIs */ |
| 760 | #ifdef CONFIG_PCI_MSI |
| 761 | ppc_md.msi_check_device = pnv_msi_check_device; |
| 762 | ppc_md.setup_msi_irqs = pnv_setup_msi_irqs; |
| 763 | ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs; |
| 764 | #endif |
Benjamin Herrenschmidt | 61305a9 | 2011-09-19 17:45:05 +0000 | [diff] [blame] | 765 | } |
Alexey Kardashevskiy | d905c5d | 2013-11-21 17:43:14 +1100 | [diff] [blame] | 766 | |
| 767 | static int tce_iommu_bus_notifier(struct notifier_block *nb, |
| 768 | unsigned long action, void *data) |
| 769 | { |
| 770 | struct device *dev = data; |
| 771 | |
| 772 | switch (action) { |
| 773 | case BUS_NOTIFY_ADD_DEVICE: |
| 774 | return iommu_add_device(dev); |
| 775 | case BUS_NOTIFY_DEL_DEVICE: |
| 776 | if (dev->iommu_group) |
| 777 | iommu_del_device(dev); |
| 778 | return 0; |
| 779 | default: |
| 780 | return 0; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | static struct notifier_block tce_iommu_bus_nb = { |
| 785 | .notifier_call = tce_iommu_bus_notifier, |
| 786 | }; |
| 787 | |
| 788 | static int __init tce_iommu_bus_notifier_init(void) |
| 789 | { |
Alexey Kardashevskiy | d905c5d | 2013-11-21 17:43:14 +1100 | [diff] [blame] | 790 | bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb); |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | subsys_initcall_sync(tce_iommu_bus_notifier_init); |