blob: 2f55c86df703554bfd9541a44f8ba26bec072729 [file] [log] [blame]
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +00001/*
2 * Support PCI/PCIe on PowerNV platforms
3 *
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +00004 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/pci.h>
14#include <linux/delay.h>
15#include <linux/string.h>
16#include <linux/init.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000017#include <linux/irq.h>
18#include <linux/io.h>
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000019#include <linux/msi.h>
Alexey Kardashevskiy4e13c1a2013-05-21 13:33:09 +100020#include <linux/iommu.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000021
22#include <asm/sections.h>
23#include <asm/io.h>
24#include <asm/prom.h>
25#include <asm/pci-bridge.h>
26#include <asm/machdep.h>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000027#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000028#include <asm/ppc-pci.h>
29#include <asm/opal.h>
30#include <asm/iommu.h>
31#include <asm/tce.h>
Stephen Rothwellf5339272012-03-15 18:18:00 +000032#include <asm/firmware.h>
Gavin Shanbe7e7442013-06-20 13:21:15 +080033#include <asm/eeh_event.h>
34#include <asm/eeh.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000035
36#include "powernv.h"
37#include "pci.h"
38
Benjamin Herrenschmidt82ba1292011-09-19 17:45:07 +000039/* Delay in usec */
40#define PCI_RESET_DELAY_US 3000000
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000041
42#define cfg_dbg(fmt...) do { } while(0)
43//#define cfg_dbg(fmt...) printk(fmt)
44
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000045#ifdef CONFIG_PCI_MSI
Daniel Axtens92ae0352015-04-28 15:12:05 +100046int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000047{
48 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
49 struct pnv_phb *phb = hose->private_data;
50 struct msi_desc *entry;
51 struct msi_msg msg;
Gavin Shanfb1b55d2013-03-05 21:12:37 +000052 int hwirq;
53 unsigned int virq;
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000054 int rc;
55
Alexander Gordeev6b2fd7ef2014-09-07 20:57:53 +020056 if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
57 return -ENODEV;
58
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +110059 if (pdev->no_64bit_msi && !phb->msi32_support)
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000060 return -ENODEV;
61
Jiang Liu2921d172015-07-09 16:00:38 +080062 for_each_pci_msi_entry(entry, pdev) {
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000063 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
64 pr_warn("%s: Supports only 64-bit MSIs\n",
65 pci_name(pdev));
66 return -ENXIO;
67 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +000068 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
69 if (hwirq < 0) {
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000070 pr_warn("%s: Failed to find a free MSI\n",
71 pci_name(pdev));
72 return -ENOSPC;
73 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +000074 virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000075 if (virq == NO_IRQ) {
76 pr_warn("%s: Failed to map MSI to linux irq\n",
77 pci_name(pdev));
Gavin Shanfb1b55d2013-03-05 21:12:37 +000078 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000079 return -ENOMEM;
80 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +000081 rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
Gavin Shan137436c2013-04-25 19:20:59 +000082 virq, entry->msi_attrib.is_64, &msg);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000083 if (rc) {
84 pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
85 irq_dispose_mapping(virq);
Gavin Shanfb1b55d2013-03-05 21:12:37 +000086 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000087 return rc;
88 }
89 irq_set_msi_desc(virq, entry);
Jiang Liu83a18912014-11-09 23:10:34 +080090 pci_write_msi_msg(virq, &msg);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000091 }
92 return 0;
93}
94
Daniel Axtens92ae0352015-04-28 15:12:05 +100095void pnv_teardown_msi_irqs(struct pci_dev *pdev)
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000096{
97 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
98 struct pnv_phb *phb = hose->private_data;
99 struct msi_desc *entry;
Paul Mackerrase297c932015-09-10 14:36:21 +1000100 irq_hw_number_t hwirq;
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000101
102 if (WARN_ON(!phb))
103 return;
104
Jiang Liu2921d172015-07-09 16:00:38 +0800105 for_each_pci_msi_entry(entry, pdev) {
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000106 if (entry->irq == NO_IRQ)
107 continue;
Paul Mackerrase297c932015-09-10 14:36:21 +1000108 hwirq = virq_to_hw(entry->irq);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000109 irq_set_msi_desc(entry->irq, NULL);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000110 irq_dispose_mapping(entry->irq);
Paul Mackerrase297c932015-09-10 14:36:21 +1000111 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, 1);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000112 }
113}
114#endif /* CONFIG_PCI_MSI */
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000115
Gavin Shan93aef2a2013-11-22 16:28:45 +0800116static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose,
117 struct OpalIoPhbErrorCommon *common)
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000118{
Gavin Shan93aef2a2013-11-22 16:28:45 +0800119 struct OpalIoP7IOCPhbErrorData *data;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000120 int i;
121
Gavin Shan93aef2a2013-11-22 16:28:45 +0800122 data = (struct OpalIoP7IOCPhbErrorData *)common;
Gavin Shanb34497d2014-04-24 18:00:10 +1000123 pr_info("P7IOC PHB#%d Diag-data (Version: %d)\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000124 hose->global_number, be32_to_cpu(common->version));
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000125
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800126 if (data->brdgCtl)
Gavin Shanb34497d2014-04-24 18:00:10 +1000127 pr_info("brdgCtl: %08x\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000128 be32_to_cpu(data->brdgCtl));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800129 if (data->portStatusReg || data->rootCmplxStatus ||
130 data->busAgentStatus)
Gavin Shanb34497d2014-04-24 18:00:10 +1000131 pr_info("UtlSts: %08x %08x %08x\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000132 be32_to_cpu(data->portStatusReg),
133 be32_to_cpu(data->rootCmplxStatus),
134 be32_to_cpu(data->busAgentStatus));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800135 if (data->deviceStatus || data->slotStatus ||
136 data->linkStatus || data->devCmdStatus ||
137 data->devSecStatus)
Gavin Shanb34497d2014-04-24 18:00:10 +1000138 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000139 be32_to_cpu(data->deviceStatus),
140 be32_to_cpu(data->slotStatus),
141 be32_to_cpu(data->linkStatus),
142 be32_to_cpu(data->devCmdStatus),
143 be32_to_cpu(data->devSecStatus));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800144 if (data->rootErrorStatus || data->uncorrErrorStatus ||
145 data->corrErrorStatus)
Gavin Shanb34497d2014-04-24 18:00:10 +1000146 pr_info("RootErrSts: %08x %08x %08x\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000147 be32_to_cpu(data->rootErrorStatus),
148 be32_to_cpu(data->uncorrErrorStatus),
149 be32_to_cpu(data->corrErrorStatus));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800150 if (data->tlpHdr1 || data->tlpHdr2 ||
151 data->tlpHdr3 || data->tlpHdr4)
Gavin Shanb34497d2014-04-24 18:00:10 +1000152 pr_info("RootErrLog: %08x %08x %08x %08x\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000153 be32_to_cpu(data->tlpHdr1),
154 be32_to_cpu(data->tlpHdr2),
155 be32_to_cpu(data->tlpHdr3),
156 be32_to_cpu(data->tlpHdr4));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800157 if (data->sourceId || data->errorClass ||
158 data->correlator)
Gavin Shanb34497d2014-04-24 18:00:10 +1000159 pr_info("RootErrLog1: %08x %016llx %016llx\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000160 be32_to_cpu(data->sourceId),
161 be64_to_cpu(data->errorClass),
162 be64_to_cpu(data->correlator));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800163 if (data->p7iocPlssr || data->p7iocCsr)
Gavin Shanb34497d2014-04-24 18:00:10 +1000164 pr_info("PhbSts: %016llx %016llx\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000165 be64_to_cpu(data->p7iocPlssr),
166 be64_to_cpu(data->p7iocCsr));
Gavin Shanb34497d2014-04-24 18:00:10 +1000167 if (data->lemFir)
168 pr_info("Lem: %016llx %016llx %016llx\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000169 be64_to_cpu(data->lemFir),
170 be64_to_cpu(data->lemErrorMask),
171 be64_to_cpu(data->lemWOF));
Gavin Shanb34497d2014-04-24 18:00:10 +1000172 if (data->phbErrorStatus)
173 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000174 be64_to_cpu(data->phbErrorStatus),
175 be64_to_cpu(data->phbFirstErrorStatus),
176 be64_to_cpu(data->phbErrorLog0),
177 be64_to_cpu(data->phbErrorLog1));
Gavin Shanb34497d2014-04-24 18:00:10 +1000178 if (data->mmioErrorStatus)
179 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000180 be64_to_cpu(data->mmioErrorStatus),
181 be64_to_cpu(data->mmioFirstErrorStatus),
182 be64_to_cpu(data->mmioErrorLog0),
183 be64_to_cpu(data->mmioErrorLog1));
Gavin Shanb34497d2014-04-24 18:00:10 +1000184 if (data->dma0ErrorStatus)
185 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000186 be64_to_cpu(data->dma0ErrorStatus),
187 be64_to_cpu(data->dma0FirstErrorStatus),
188 be64_to_cpu(data->dma0ErrorLog0),
189 be64_to_cpu(data->dma0ErrorLog1));
Gavin Shanb34497d2014-04-24 18:00:10 +1000190 if (data->dma1ErrorStatus)
191 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000192 be64_to_cpu(data->dma1ErrorStatus),
193 be64_to_cpu(data->dma1FirstErrorStatus),
194 be64_to_cpu(data->dma1ErrorLog0),
195 be64_to_cpu(data->dma1ErrorLog1));
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000196
197 for (i = 0; i < OPAL_P7IOC_NUM_PEST_REGS; i++) {
198 if ((data->pestA[i] >> 63) == 0 &&
199 (data->pestB[i] >> 63) == 0)
200 continue;
Gavin Shan93aef2a2013-11-22 16:28:45 +0800201
Gavin Shanb34497d2014-04-24 18:00:10 +1000202 pr_info("PE[%3d] A/B: %016llx %016llx\n",
Gavin Shanf18440f2014-07-17 14:41:42 +1000203 i, be64_to_cpu(data->pestA[i]),
204 be64_to_cpu(data->pestB[i]));
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000205 }
206}
207
Gavin Shan93aef2a2013-11-22 16:28:45 +0800208static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
209 struct OpalIoPhbErrorCommon *common)
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000210{
Gavin Shan93aef2a2013-11-22 16:28:45 +0800211 struct OpalIoPhb3ErrorData *data;
212 int i;
213
214 data = (struct OpalIoPhb3ErrorData*)common;
Gavin Shanb34497d2014-04-24 18:00:10 +1000215 pr_info("PHB3 PHB#%d Diag-data (Version: %d)\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800216 hose->global_number, be32_to_cpu(common->version));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800217 if (data->brdgCtl)
Gavin Shanb34497d2014-04-24 18:00:10 +1000218 pr_info("brdgCtl: %08x\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800219 be32_to_cpu(data->brdgCtl));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800220 if (data->portStatusReg || data->rootCmplxStatus ||
221 data->busAgentStatus)
Gavin Shanb34497d2014-04-24 18:00:10 +1000222 pr_info("UtlSts: %08x %08x %08x\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800223 be32_to_cpu(data->portStatusReg),
224 be32_to_cpu(data->rootCmplxStatus),
225 be32_to_cpu(data->busAgentStatus));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800226 if (data->deviceStatus || data->slotStatus ||
227 data->linkStatus || data->devCmdStatus ||
228 data->devSecStatus)
Gavin Shanb34497d2014-04-24 18:00:10 +1000229 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800230 be32_to_cpu(data->deviceStatus),
231 be32_to_cpu(data->slotStatus),
232 be32_to_cpu(data->linkStatus),
233 be32_to_cpu(data->devCmdStatus),
234 be32_to_cpu(data->devSecStatus));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800235 if (data->rootErrorStatus || data->uncorrErrorStatus ||
236 data->corrErrorStatus)
Gavin Shanb34497d2014-04-24 18:00:10 +1000237 pr_info("RootErrSts: %08x %08x %08x\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800238 be32_to_cpu(data->rootErrorStatus),
239 be32_to_cpu(data->uncorrErrorStatus),
240 be32_to_cpu(data->corrErrorStatus));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800241 if (data->tlpHdr1 || data->tlpHdr2 ||
242 data->tlpHdr3 || data->tlpHdr4)
Gavin Shanb34497d2014-04-24 18:00:10 +1000243 pr_info("RootErrLog: %08x %08x %08x %08x\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800244 be32_to_cpu(data->tlpHdr1),
245 be32_to_cpu(data->tlpHdr2),
246 be32_to_cpu(data->tlpHdr3),
247 be32_to_cpu(data->tlpHdr4));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800248 if (data->sourceId || data->errorClass ||
249 data->correlator)
Gavin Shanb34497d2014-04-24 18:00:10 +1000250 pr_info("RootErrLog1: %08x %016llx %016llx\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800251 be32_to_cpu(data->sourceId),
252 be64_to_cpu(data->errorClass),
253 be64_to_cpu(data->correlator));
Gavin Shanb34497d2014-04-24 18:00:10 +1000254 if (data->nFir)
255 pr_info("nFir: %016llx %016llx %016llx\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800256 be64_to_cpu(data->nFir),
257 be64_to_cpu(data->nFirMask),
258 be64_to_cpu(data->nFirWOF));
Gavin Shanaf87d2f2014-02-25 15:28:38 +0800259 if (data->phbPlssr || data->phbCsr)
Gavin Shanb34497d2014-04-24 18:00:10 +1000260 pr_info("PhbSts: %016llx %016llx\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800261 be64_to_cpu(data->phbPlssr),
262 be64_to_cpu(data->phbCsr));
Gavin Shanb34497d2014-04-24 18:00:10 +1000263 if (data->lemFir)
264 pr_info("Lem: %016llx %016llx %016llx\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800265 be64_to_cpu(data->lemFir),
266 be64_to_cpu(data->lemErrorMask),
267 be64_to_cpu(data->lemWOF));
Gavin Shanb34497d2014-04-24 18:00:10 +1000268 if (data->phbErrorStatus)
269 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800270 be64_to_cpu(data->phbErrorStatus),
271 be64_to_cpu(data->phbFirstErrorStatus),
272 be64_to_cpu(data->phbErrorLog0),
273 be64_to_cpu(data->phbErrorLog1));
Gavin Shanb34497d2014-04-24 18:00:10 +1000274 if (data->mmioErrorStatus)
275 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800276 be64_to_cpu(data->mmioErrorStatus),
277 be64_to_cpu(data->mmioFirstErrorStatus),
278 be64_to_cpu(data->mmioErrorLog0),
279 be64_to_cpu(data->mmioErrorLog1));
Gavin Shanb34497d2014-04-24 18:00:10 +1000280 if (data->dma0ErrorStatus)
281 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800282 be64_to_cpu(data->dma0ErrorStatus),
283 be64_to_cpu(data->dma0FirstErrorStatus),
284 be64_to_cpu(data->dma0ErrorLog0),
285 be64_to_cpu(data->dma0ErrorLog1));
Gavin Shanb34497d2014-04-24 18:00:10 +1000286 if (data->dma1ErrorStatus)
287 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800288 be64_to_cpu(data->dma1ErrorStatus),
289 be64_to_cpu(data->dma1FirstErrorStatus),
290 be64_to_cpu(data->dma1ErrorLog0),
291 be64_to_cpu(data->dma1ErrorLog1));
Gavin Shan93aef2a2013-11-22 16:28:45 +0800292
293 for (i = 0; i < OPAL_PHB3_NUM_PEST_REGS; i++) {
Guo Chaoddf0322a2014-06-09 16:58:51 +0800294 if ((be64_to_cpu(data->pestA[i]) >> 63) == 0 &&
295 (be64_to_cpu(data->pestB[i]) >> 63) == 0)
Gavin Shan93aef2a2013-11-22 16:28:45 +0800296 continue;
297
Gavin Shanb34497d2014-04-24 18:00:10 +1000298 pr_info("PE[%3d] A/B: %016llx %016llx\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800299 i, be64_to_cpu(data->pestA[i]),
300 be64_to_cpu(data->pestB[i]));
Gavin Shan93aef2a2013-11-22 16:28:45 +0800301 }
302}
303
304void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
305 unsigned char *log_buff)
306{
307 struct OpalIoPhbErrorCommon *common;
308
309 if (!hose || !log_buff)
310 return;
311
312 common = (struct OpalIoPhbErrorCommon *)log_buff;
Guo Chaoddf0322a2014-06-09 16:58:51 +0800313 switch (be32_to_cpu(common->ioType)) {
Gavin Shan93aef2a2013-11-22 16:28:45 +0800314 case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
315 pnv_pci_dump_p7ioc_diag_data(hose, common);
316 break;
317 case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
318 pnv_pci_dump_phb3_diag_data(hose, common);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000319 break;
320 default:
Gavin Shan93aef2a2013-11-22 16:28:45 +0800321 pr_warn("%s: Unrecognized ioType %d\n",
Guo Chaoddf0322a2014-06-09 16:58:51 +0800322 __func__, be32_to_cpu(common->ioType));
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000323 }
324}
325
326static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
327{
328 unsigned long flags, rc;
Gavin Shan98fd7002014-07-21 14:42:35 +1000329 int has_diag, ret = 0;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000330
331 spin_lock_irqsave(&phb->lock, flags);
332
Gavin Shan98fd7002014-07-21 14:42:35 +1000333 /* Fetch PHB diag-data */
Gavin Shan23773232013-06-20 13:21:05 +0800334 rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob,
335 PNV_PCI_DIAG_BUF_SIZE);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000336 has_diag = (rc == OPAL_SUCCESS);
337
Gavin Shan98fd7002014-07-21 14:42:35 +1000338 /* If PHB supports compound PE, to handle it */
339 if (phb->unfreeze_pe) {
340 ret = phb->unfreeze_pe(phb,
341 pe_no,
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000342 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
Gavin Shan98fd7002014-07-21 14:42:35 +1000343 } else {
344 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
345 pe_no,
346 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
347 if (rc) {
348 pr_warn("%s: Failure %ld clearing frozen "
349 "PHB#%x-PE#%x\n",
350 __func__, rc, phb->hose->global_number,
351 pe_no);
352 ret = -EIO;
353 }
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000354 }
355
Gavin Shan98fd7002014-07-21 14:42:35 +1000356 /*
357 * For now, let's only display the diag buffer when we fail to clear
358 * the EEH status. We'll do more sensible things later when we have
359 * proper EEH support. We need to make sure we don't pollute ourselves
360 * with the normal errors generated when probing empty slots
361 */
362 if (has_diag && ret)
363 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob);
364
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000365 spin_unlock_irqrestore(&phb->lock, flags);
366}
367
Gavin Shan3532a7412015-03-17 16:15:03 +1100368static void pnv_pci_config_check_eeh(struct pci_dn *pdn)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000369{
Gavin Shan3532a7412015-03-17 16:15:03 +1100370 struct pnv_phb *phb = pdn->phb->private_data;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000371 u8 fstate;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000372 __be16 pcierr;
Gavin Shan98fd7002014-07-21 14:42:35 +1000373 int pe_no;
374 s64 rc;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000375
Gavin Shan9bf41be2013-06-27 13:46:48 +0800376 /*
377 * Get the PE#. During the PCI probe stage, we might not
378 * setup that yet. So all ER errors should be mapped to
Gavin Shan36954dc2013-11-04 16:32:47 +0800379 * reserved PE.
Gavin Shan9bf41be2013-06-27 13:46:48 +0800380 */
Gavin Shan3532a7412015-03-17 16:15:03 +1100381 pe_no = pdn->pe_number;
Gavin Shan36954dc2013-11-04 16:32:47 +0800382 if (pe_no == IODA_INVALID_PE) {
383 if (phb->type == PNV_PHB_P5IOC2)
384 pe_no = 0;
385 else
386 pe_no = phb->ioda.reserved_pe;
387 }
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000388
Gavin Shan98fd7002014-07-21 14:42:35 +1000389 /*
390 * Fetch frozen state. If the PHB support compound PE,
391 * we need handle that case.
392 */
393 if (phb->get_pe_state) {
394 fstate = phb->get_pe_state(phb, pe_no);
395 } else {
396 rc = opal_pci_eeh_freeze_status(phb->opal_id,
397 pe_no,
398 &fstate,
399 &pcierr,
400 NULL);
401 if (rc) {
402 pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
403 __func__, rc, phb->hose->global_number, pe_no);
404 return;
405 }
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000406 }
Gavin Shan98fd7002014-07-21 14:42:35 +1000407
Gavin Shan9bf41be2013-06-27 13:46:48 +0800408 cfg_dbg(" -> EEH check, bdfn=%04x PE#%d fstate=%x\n",
Gavin Shan3532a7412015-03-17 16:15:03 +1100409 (pdn->busno << 8) | (pdn->devfn), pe_no, fstate);
Gavin Shan98fd7002014-07-21 14:42:35 +1000410
411 /* Clear the frozen state if applicable */
412 if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
413 fstate == OPAL_EEH_STOPPED_DMA_FREEZE ||
414 fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
415 /*
416 * If PHB supports compound PE, freeze it for
417 * consistency.
418 */
419 if (phb->freeze_pe)
420 phb->freeze_pe(phb, pe_no);
421
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000422 pnv_pci_handle_eeh_config(phb, pe_no);
Gavin Shan98fd7002014-07-21 14:42:35 +1000423 }
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000424}
425
Gavin Shan3532a7412015-03-17 16:15:03 +1100426int pnv_pci_cfg_read(struct pci_dn *pdn,
Gavin Shan9bf41be2013-06-27 13:46:48 +0800427 int where, int size, u32 *val)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000428{
Gavin Shan9bf41be2013-06-27 13:46:48 +0800429 struct pnv_phb *phb = pdn->phb->private_data;
430 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000431 s64 rc;
432
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000433 switch (size) {
434 case 1: {
435 u8 v8;
436 rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
437 *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
438 break;
439 }
440 case 2: {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000441 __be16 v16;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000442 rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
443 &v16);
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000444 *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000445 break;
446 }
447 case 4: {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000448 __be32 v32;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000449 rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000450 *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000451 break;
452 }
453 default:
454 return PCIBIOS_FUNC_NOT_SUPPORTED;
455 }
Gavin Shand0914f52014-04-24 18:00:12 +1000456
Gavin Shan9bf41be2013-06-27 13:46:48 +0800457 cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
458 __func__, pdn->busno, pdn->devfn, where, size, *val);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000459 return PCIBIOS_SUCCESSFUL;
460}
461
Gavin Shan3532a7412015-03-17 16:15:03 +1100462int pnv_pci_cfg_write(struct pci_dn *pdn,
Gavin Shan9bf41be2013-06-27 13:46:48 +0800463 int where, int size, u32 val)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000464{
Gavin Shan9bf41be2013-06-27 13:46:48 +0800465 struct pnv_phb *phb = pdn->phb->private_data;
466 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000467
Gavin Shan9bf41be2013-06-27 13:46:48 +0800468 cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
469 pdn->busno, pdn->devfn, where, size, val);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000470 switch (size) {
471 case 1:
472 opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
473 break;
474 case 2:
475 opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
476 break;
477 case 4:
478 opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
479 break;
480 default:
481 return PCIBIOS_FUNC_NOT_SUPPORTED;
482 }
Gavin Shanbe7e7442013-06-20 13:21:15 +0800483
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000484 return PCIBIOS_SUCCESSFUL;
485}
486
Gavin Shand0914f52014-04-24 18:00:12 +1000487#if CONFIG_EEH
Gavin Shan3532a7412015-03-17 16:15:03 +1100488static bool pnv_pci_cfg_check(struct pci_dn *pdn)
Gavin Shand0914f52014-04-24 18:00:12 +1000489{
490 struct eeh_dev *edev = NULL;
Gavin Shan3532a7412015-03-17 16:15:03 +1100491 struct pnv_phb *phb = pdn->phb->private_data;
Gavin Shand0914f52014-04-24 18:00:12 +1000492
493 /* EEH not enabled ? */
494 if (!(phb->flags & PNV_PHB_FLAG_EEH))
495 return true;
496
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000497 /* PE reset or device removed ? */
Gavin Shan3532a7412015-03-17 16:15:03 +1100498 edev = pdn->edev;
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000499 if (edev) {
500 if (edev->pe &&
Gavin Shan8a6b3712014-10-01 17:07:50 +1000501 (edev->pe->state & EEH_PE_CFG_BLOCKED))
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000502 return false;
503
504 if (edev->mode & EEH_DEV_REMOVED)
505 return false;
506 }
Gavin Shand0914f52014-04-24 18:00:12 +1000507
508 return true;
509}
510#else
Gavin Shan3532a7412015-03-17 16:15:03 +1100511static inline pnv_pci_cfg_check(struct pci_dn *pdn)
Gavin Shand0914f52014-04-24 18:00:12 +1000512{
513 return true;
514}
515#endif /* CONFIG_EEH */
516
Gavin Shan9bf41be2013-06-27 13:46:48 +0800517static int pnv_pci_read_config(struct pci_bus *bus,
518 unsigned int devfn,
519 int where, int size, u32 *val)
520{
Gavin Shan9bf41be2013-06-27 13:46:48 +0800521 struct pci_dn *pdn;
Gavin Shand0914f52014-04-24 18:00:12 +1000522 struct pnv_phb *phb;
Gavin Shand0914f52014-04-24 18:00:12 +1000523 int ret;
Gavin Shan9bf41be2013-06-27 13:46:48 +0800524
525 *val = 0xFFFFFFFF;
Gavin Shan3532a7412015-03-17 16:15:03 +1100526 pdn = pci_get_pdn_by_devfn(bus, devfn);
527 if (!pdn)
Gavin Shand0914f52014-04-24 18:00:12 +1000528 return PCIBIOS_DEVICE_NOT_FOUND;
529
Gavin Shan3532a7412015-03-17 16:15:03 +1100530 if (!pnv_pci_cfg_check(pdn))
531 return PCIBIOS_DEVICE_NOT_FOUND;
532
533 ret = pnv_pci_cfg_read(pdn, where, size, val);
534 phb = pdn->phb->private_data;
535 if (phb->flags & PNV_PHB_FLAG_EEH && pdn->edev) {
Gavin Shand0914f52014-04-24 18:00:12 +1000536 if (*val == EEH_IO_ERROR_VALUE(size) &&
Gavin Shan3532a7412015-03-17 16:15:03 +1100537 eeh_dev_check_failure(pdn->edev))
Gavin Shand0914f52014-04-24 18:00:12 +1000538 return PCIBIOS_DEVICE_NOT_FOUND;
539 } else {
Gavin Shan3532a7412015-03-17 16:15:03 +1100540 pnv_pci_config_check_eeh(pdn);
Gavin Shand0914f52014-04-24 18:00:12 +1000541 }
542
543 return ret;
Gavin Shan9bf41be2013-06-27 13:46:48 +0800544}
545
546static int pnv_pci_write_config(struct pci_bus *bus,
547 unsigned int devfn,
548 int where, int size, u32 val)
549{
Gavin Shan9bf41be2013-06-27 13:46:48 +0800550 struct pci_dn *pdn;
Gavin Shand0914f52014-04-24 18:00:12 +1000551 struct pnv_phb *phb;
Gavin Shand0914f52014-04-24 18:00:12 +1000552 int ret;
Gavin Shan9bf41be2013-06-27 13:46:48 +0800553
Gavin Shan3532a7412015-03-17 16:15:03 +1100554 pdn = pci_get_pdn_by_devfn(bus, devfn);
555 if (!pdn)
Gavin Shand0914f52014-04-24 18:00:12 +1000556 return PCIBIOS_DEVICE_NOT_FOUND;
557
Gavin Shan3532a7412015-03-17 16:15:03 +1100558 if (!pnv_pci_cfg_check(pdn))
559 return PCIBIOS_DEVICE_NOT_FOUND;
560
561 ret = pnv_pci_cfg_write(pdn, where, size, val);
562 phb = pdn->phb->private_data;
Gavin Shand0914f52014-04-24 18:00:12 +1000563 if (!(phb->flags & PNV_PHB_FLAG_EEH))
Gavin Shan3532a7412015-03-17 16:15:03 +1100564 pnv_pci_config_check_eeh(pdn);
Gavin Shand0914f52014-04-24 18:00:12 +1000565
566 return ret;
Gavin Shan9bf41be2013-06-27 13:46:48 +0800567}
568
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000569struct pci_ops pnv_pci_ops = {
Gavin Shan9bf41be2013-06-27 13:46:48 +0800570 .read = pnv_pci_read_config,
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000571 .write = pnv_pci_write_config,
572};
573
Alexey Kardashevskiyc5bb44e2015-06-05 16:35:14 +1000574static __be64 *pnv_tce(struct iommu_table *tbl, long idx)
575{
576 __be64 *tmp = ((__be64 *)tbl->it_base);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +1000577 int level = tbl->it_indirect_levels;
578 const long shift = ilog2(tbl->it_level_size);
579 unsigned long mask = (tbl->it_level_size - 1) << (level * shift);
580
581 while (level) {
582 int n = (idx & mask) >> (level * shift);
583 unsigned long tce = be64_to_cpu(tmp[n]);
584
585 tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE));
586 idx &= ~mask;
587 mask >>= shift;
588 --level;
589 }
Alexey Kardashevskiyc5bb44e2015-06-05 16:35:14 +1000590
591 return tmp + idx;
592}
593
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000594int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
595 unsigned long uaddr, enum dma_data_direction direction,
596 struct dma_attrs *attrs)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000597{
Alexey Kardashevskiy10b35b22015-06-05 16:35:05 +1000598 u64 proto_tce = iommu_direction_to_tce_perm(direction);
Alexey Kardashevskiyc5bb44e2015-06-05 16:35:14 +1000599 u64 rpn = __pa(uaddr) >> tbl->it_page_shift;
600 long i;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000601
Alexey Kardashevskiyc5bb44e2015-06-05 16:35:14 +1000602 for (i = 0; i < npages; i++) {
603 unsigned long newtce = proto_tce |
604 ((rpn + i) << tbl->it_page_shift);
605 unsigned long idx = index - tbl->it_offset + i;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000606
Alexey Kardashevskiyc5bb44e2015-06-05 16:35:14 +1000607 *(pnv_tce(tbl, idx)) = cpu_to_be64(newtce);
608 }
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000609
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000610 return 0;
611}
612
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000613#ifdef CONFIG_IOMMU_API
614int pnv_tce_xchg(struct iommu_table *tbl, long index,
615 unsigned long *hpa, enum dma_data_direction *direction)
616{
617 u64 proto_tce = iommu_direction_to_tce_perm(*direction);
618 unsigned long newtce = *hpa | proto_tce, oldtce;
619 unsigned long idx = index - tbl->it_offset;
620
621 BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl));
622
623 oldtce = xchg(pnv_tce(tbl, idx), cpu_to_be64(newtce));
624 *hpa = be64_to_cpu(oldtce) & ~(TCE_PCI_READ | TCE_PCI_WRITE);
625 *direction = iommu_tce_direction(oldtce);
626
627 return 0;
628}
629#endif
630
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000631void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000632{
Alexey Kardashevskiyc5bb44e2015-06-05 16:35:14 +1000633 long i;
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000634
Alexey Kardashevskiyc5bb44e2015-06-05 16:35:14 +1000635 for (i = 0; i < npages; i++) {
636 unsigned long idx = index - tbl->it_offset + i;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000637
Alexey Kardashevskiyc5bb44e2015-06-05 16:35:14 +1000638 *(pnv_tce(tbl, idx)) = cpu_to_be64(0);
639 }
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000640}
641
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000642unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
Alexey Kardashevskiy11f63d32012-09-04 15:19:35 +0000643{
Alexey Kardashevskiyc5bb44e2015-06-05 16:35:14 +1000644 return *(pnv_tce(tbl, index - tbl->it_offset));
Alexey Kardashevskiy11f63d32012-09-04 15:19:35 +0000645}
646
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000647struct iommu_table *pnv_pci_table_alloc(int nid)
648{
649 struct iommu_table *tbl;
650
651 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
652 INIT_LIST_HEAD_RCU(&tbl->it_group_list);
653
654 return tbl;
655}
656
657long pnv_pci_link_table_and_group(int node, int num,
658 struct iommu_table *tbl,
659 struct iommu_table_group *table_group)
660{
661 struct iommu_table_group_link *tgl = NULL;
662
663 if (WARN_ON(!tbl || !table_group))
664 return -EINVAL;
665
666 tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
667 node);
668 if (!tgl)
669 return -ENOMEM;
670
671 tgl->table_group = table_group;
672 list_add_rcu(&tgl->next, &tbl->it_group_list);
673
674 table_group->tables[num] = tbl;
675
676 return 0;
677}
678
679static void pnv_iommu_table_group_link_free(struct rcu_head *head)
680{
681 struct iommu_table_group_link *tgl = container_of(head,
682 struct iommu_table_group_link, rcu);
683
684 kfree(tgl);
685}
686
687void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
688 struct iommu_table_group *table_group)
689{
690 long i;
691 bool found;
692 struct iommu_table_group_link *tgl;
693
694 if (!tbl || !table_group)
695 return;
696
697 /* Remove link to a group from table's list of attached groups */
698 found = false;
699 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
700 if (tgl->table_group == table_group) {
701 list_del_rcu(&tgl->next);
702 call_rcu(&tgl->rcu, pnv_iommu_table_group_link_free);
703 found = true;
704 break;
705 }
706 }
707 if (WARN_ON(!found))
708 return;
709
710 /* Clean a pointer to iommu_table in iommu_table_group::tables[] */
711 found = false;
712 for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
713 if (table_group->tables[i] == tbl) {
714 table_group->tables[i] = NULL;
715 found = true;
716 break;
717 }
718 }
719 WARN_ON(!found);
720}
721
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000722void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
723 void *tce_mem, u64 tce_size,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +1000724 u64 dma_offset, unsigned page_shift)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000725{
726 tbl->it_blocksize = 16;
727 tbl->it_base = (unsigned long)tce_mem;
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +1000728 tbl->it_page_shift = page_shift;
Alistair Popple3a553172013-12-09 18:17:02 +1100729 tbl->it_offset = dma_offset >> tbl->it_page_shift;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000730 tbl->it_index = 0;
731 tbl->it_size = tce_size >> 3;
732 tbl->it_busno = 0;
733 tbl->it_type = TCE_PCI;
734}
735
Daniel Axtens92ae0352015-04-28 15:12:05 +1000736void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000737{
738 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
739 struct pnv_phb *phb = hose->private_data;
Wei Yang781a8682015-03-25 16:23:57 +0800740#ifdef CONFIG_PCI_IOV
741 struct pnv_ioda_pe *pe;
742 struct pci_dn *pdn;
743
744 /* Fix the VF pdn PE number */
745 if (pdev->is_virtfn) {
746 pdn = pci_get_pdn(pdev);
747 WARN_ON(pdn->pe_number != IODA_INVALID_PE);
748 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
749 if (pe->rid == ((pdev->bus->number << 8) |
750 (pdev->devfn & 0xff))) {
751 pdn->pe_number = pe->pe_number;
752 pe->pdev = pdev;
753 break;
754 }
755 }
756 }
757#endif /* CONFIG_PCI_IOV */
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000758
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000759 if (phb && phb->dma_dev_setup)
760 phb->dma_dev_setup(phb, pdev);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000761}
762
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000763void pnv_pci_shutdown(void)
764{
765 struct pci_controller *hose;
766
Michael Neuling7a8e6bb2015-05-27 16:06:59 +1000767 list_for_each_entry(hose, &hose_list, list_node)
768 if (hose->controller_ops.shutdown)
769 hose->controller_ops.shutdown(hose);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000770}
771
Gavin Shanaa0c0332013-04-25 19:20:57 +0000772/* Fixup wrong class code in p7ioc and p8 root complex */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800773static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
Benjamin Herrenschmidtca45cfe2011-11-06 18:56:00 +0000774{
775 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
776}
777DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
778
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000779void __init pnv_pci_init(void)
780{
781 struct device_node *np;
Michael Ellerman646b54f2015-03-12 17:27:11 +1100782 bool found_ioda = false;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000783
Bjorn Helgaas673c9752012-02-23 20:18:58 -0700784 pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000785
Michael Ellerman646b54f2015-03-12 17:27:11 +1100786 /* If we don't have OPAL, eg. in sim, just skip PCI probe */
787 if (!firmware_has_feature(FW_FEATURE_OPAL))
788 return;
789
790 /* Look for IODA IO-Hubs. We don't support mixing IODA
791 * and p5ioc2 due to the need to change some global
792 * probing flags
793 */
794 for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
795 pnv_pci_init_ioda_hub(np);
796 found_ioda = true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000797 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000798
Michael Ellerman646b54f2015-03-12 17:27:11 +1100799 /* Look for p5ioc2 IO-Hubs */
800 if (!found_ioda)
801 for_each_compatible_node(np, NULL, "ibm,p5ioc2")
802 pnv_pci_init_p5ioc2_hub(np);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000803
Michael Ellerman646b54f2015-03-12 17:27:11 +1100804 /* Look for ioda2 built-in PHB3's */
805 for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
806 pnv_pci_init_ioda2_phb(np);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000807
Alistair Popple5d2aa712015-12-17 13:43:13 +1100808 /* Look for NPU PHBs */
809 for_each_compatible_node(np, NULL, "ibm,ioda2-npu-phb")
810 pnv_pci_init_npu_phb(np);
811
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000812 /* Setup the linkage between OF nodes and PHBs */
813 pci_devs_phb_init();
814
815 /* Configure IOMMU DMA hooks */
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000816 set_pci_dma_ops(&dma_iommu_ops);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000817}
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +1100818
Michael Ellermanb14726c2014-07-15 22:22:24 +1000819machine_subsys_initcall_sync(powernv, tce_iommu_bus_notifier_init);