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