12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
261305a96SBenjamin Herrenschmidt /*
361305a96SBenjamin Herrenschmidt  * Support PCI/PCIe on PowerNV platforms
461305a96SBenjamin Herrenschmidt  *
561305a96SBenjamin Herrenschmidt  * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
661305a96SBenjamin Herrenschmidt  */
761305a96SBenjamin Herrenschmidt 
861305a96SBenjamin Herrenschmidt #include <linux/kernel.h>
961305a96SBenjamin Herrenschmidt #include <linux/pci.h>
1061305a96SBenjamin Herrenschmidt #include <linux/delay.h>
1161305a96SBenjamin Herrenschmidt #include <linux/string.h>
1261305a96SBenjamin Herrenschmidt #include <linux/init.h>
1361305a96SBenjamin Herrenschmidt #include <linux/irq.h>
1461305a96SBenjamin Herrenschmidt #include <linux/io.h>
15c1a2562aSBenjamin Herrenschmidt #include <linux/msi.h>
164e13c1acSAlexey Kardashevskiy #include <linux/iommu.h>
17d6a90bb8SPhilippe Bergheaud #include <linux/sched/mm.h>
1861305a96SBenjamin Herrenschmidt 
1961305a96SBenjamin Herrenschmidt #include <asm/sections.h>
2061305a96SBenjamin Herrenschmidt #include <asm/io.h>
2161305a96SBenjamin Herrenschmidt #include <asm/pci-bridge.h>
2261305a96SBenjamin Herrenschmidt #include <asm/machdep.h>
23fb1b55d6SGavin Shan #include <asm/msi_bitmap.h>
2461305a96SBenjamin Herrenschmidt #include <asm/ppc-pci.h>
257e19bf32SGavin Shan #include <asm/pnv-pci.h>
2661305a96SBenjamin Herrenschmidt #include <asm/opal.h>
2761305a96SBenjamin Herrenschmidt #include <asm/iommu.h>
2861305a96SBenjamin Herrenschmidt #include <asm/tce.h>
29f5339277SStephen Rothwell #include <asm/firmware.h>
30be7e7446SGavin Shan #include <asm/eeh_event.h>
31be7e7446SGavin Shan #include <asm/eeh.h>
3261305a96SBenjamin Herrenschmidt 
3361305a96SBenjamin Herrenschmidt #include "powernv.h"
3461305a96SBenjamin Herrenschmidt #include "pci.h"
3561305a96SBenjamin Herrenschmidt 
36d6a90bb8SPhilippe Bergheaud static DEFINE_MUTEX(tunnel_mutex);
3725529100SFrederic Barrat 
pnv_pci_get_slot_id(struct device_node * np,uint64_t * id)387e19bf32SGavin Shan int pnv_pci_get_slot_id(struct device_node *np, uint64_t *id)
397e19bf32SGavin Shan {
40bbb78904SFrederic Barrat 	struct device_node *node = np;
417e19bf32SGavin Shan 	u32 bdfn;
427e19bf32SGavin Shan 	u64 phbid;
437e19bf32SGavin Shan 	int ret;
447e19bf32SGavin Shan 
457e19bf32SGavin Shan 	ret = of_property_read_u32(np, "reg", &bdfn);
467e19bf32SGavin Shan 	if (ret)
477e19bf32SGavin Shan 		return -ENXIO;
487e19bf32SGavin Shan 
497e19bf32SGavin Shan 	bdfn = ((bdfn & 0x00ffff00) >> 8);
50bbb78904SFrederic Barrat 	for (node = np; node; node = of_get_parent(node)) {
51bbb78904SFrederic Barrat 		if (!PCI_DN(node)) {
52bbb78904SFrederic Barrat 			of_node_put(node);
537e19bf32SGavin Shan 			break;
547e19bf32SGavin Shan 		}
557e19bf32SGavin Shan 
56bbb78904SFrederic Barrat 		if (!of_device_is_compatible(node, "ibm,ioda2-phb") &&
57bbb78904SFrederic Barrat 		    !of_device_is_compatible(node, "ibm,ioda3-phb") &&
58bbb78904SFrederic Barrat 		    !of_device_is_compatible(node, "ibm,ioda2-npu2-opencapi-phb")) {
59bbb78904SFrederic Barrat 			of_node_put(node);
607e19bf32SGavin Shan 			continue;
617e19bf32SGavin Shan 		}
627e19bf32SGavin Shan 
63bbb78904SFrederic Barrat 		ret = of_property_read_u64(node, "ibm,opal-phbid", &phbid);
647e19bf32SGavin Shan 		if (ret) {
65bbb78904SFrederic Barrat 			of_node_put(node);
667e19bf32SGavin Shan 			return -ENXIO;
677e19bf32SGavin Shan 		}
687e19bf32SGavin Shan 
69bbb78904SFrederic Barrat 		if (of_device_is_compatible(node, "ibm,ioda2-npu2-opencapi-phb"))
70bbb78904SFrederic Barrat 			*id = PCI_PHB_SLOT_ID(phbid);
71bbb78904SFrederic Barrat 		else
727e19bf32SGavin Shan 			*id = PCI_SLOT_ID(phbid, bdfn);
737e19bf32SGavin Shan 		return 0;
747e19bf32SGavin Shan 	}
757e19bf32SGavin Shan 
767e19bf32SGavin Shan 	return -ENODEV;
777e19bf32SGavin Shan }
787e19bf32SGavin Shan EXPORT_SYMBOL_GPL(pnv_pci_get_slot_id);
797e19bf32SGavin Shan 
pnv_pci_get_device_tree(uint32_t phandle,void * buf,uint64_t len)80ea0d856cSGavin Shan int pnv_pci_get_device_tree(uint32_t phandle, void *buf, uint64_t len)
81ea0d856cSGavin Shan {
82ea0d856cSGavin Shan 	int64_t rc;
83ea0d856cSGavin Shan 
84ea0d856cSGavin Shan 	if (!opal_check_token(OPAL_GET_DEVICE_TREE))
85ea0d856cSGavin Shan 		return -ENXIO;
86ea0d856cSGavin Shan 
87ea0d856cSGavin Shan 	rc = opal_get_device_tree(phandle, (uint64_t)buf, len);
88ea0d856cSGavin Shan 	if (rc < OPAL_SUCCESS)
89ea0d856cSGavin Shan 		return -EIO;
90ea0d856cSGavin Shan 
91ea0d856cSGavin Shan 	return rc;
92ea0d856cSGavin Shan }
93ea0d856cSGavin Shan EXPORT_SYMBOL_GPL(pnv_pci_get_device_tree);
94ea0d856cSGavin Shan 
pnv_pci_get_presence_state(uint64_t id,uint8_t * state)95ea0d856cSGavin Shan int pnv_pci_get_presence_state(uint64_t id, uint8_t *state)
96ea0d856cSGavin Shan {
97ea0d856cSGavin Shan 	int64_t rc;
98ea0d856cSGavin Shan 
99ea0d856cSGavin Shan 	if (!opal_check_token(OPAL_PCI_GET_PRESENCE_STATE))
100ea0d856cSGavin Shan 		return -ENXIO;
101ea0d856cSGavin Shan 
102ea0d856cSGavin Shan 	rc = opal_pci_get_presence_state(id, (uint64_t)state);
103ea0d856cSGavin Shan 	if (rc != OPAL_SUCCESS)
104ea0d856cSGavin Shan 		return -EIO;
105ea0d856cSGavin Shan 
106ea0d856cSGavin Shan 	return 0;
107ea0d856cSGavin Shan }
108ea0d856cSGavin Shan EXPORT_SYMBOL_GPL(pnv_pci_get_presence_state);
109ea0d856cSGavin Shan 
pnv_pci_get_power_state(uint64_t id,uint8_t * state)110ea0d856cSGavin Shan int pnv_pci_get_power_state(uint64_t id, uint8_t *state)
111ea0d856cSGavin Shan {
112ea0d856cSGavin Shan 	int64_t rc;
113ea0d856cSGavin Shan 
114ea0d856cSGavin Shan 	if (!opal_check_token(OPAL_PCI_GET_POWER_STATE))
115ea0d856cSGavin Shan 		return -ENXIO;
116ea0d856cSGavin Shan 
117ea0d856cSGavin Shan 	rc = opal_pci_get_power_state(id, (uint64_t)state);
118ea0d856cSGavin Shan 	if (rc != OPAL_SUCCESS)
119ea0d856cSGavin Shan 		return -EIO;
120ea0d856cSGavin Shan 
121ea0d856cSGavin Shan 	return 0;
122ea0d856cSGavin Shan }
123ea0d856cSGavin Shan EXPORT_SYMBOL_GPL(pnv_pci_get_power_state);
124ea0d856cSGavin Shan 
pnv_pci_set_power_state(uint64_t id,uint8_t state,struct opal_msg * msg)125ea0d856cSGavin Shan int pnv_pci_set_power_state(uint64_t id, uint8_t state, struct opal_msg *msg)
126ea0d856cSGavin Shan {
127ea0d856cSGavin Shan 	struct opal_msg m;
128ea0d856cSGavin Shan 	int token, ret;
129ea0d856cSGavin Shan 	int64_t rc;
130ea0d856cSGavin Shan 
131ea0d856cSGavin Shan 	if (!opal_check_token(OPAL_PCI_SET_POWER_STATE))
132ea0d856cSGavin Shan 		return -ENXIO;
133ea0d856cSGavin Shan 
134ea0d856cSGavin Shan 	token = opal_async_get_token_interruptible();
135ea0d856cSGavin Shan 	if (unlikely(token < 0))
136ea0d856cSGavin Shan 		return token;
137ea0d856cSGavin Shan 
138ea0d856cSGavin Shan 	rc = opal_pci_set_power_state(token, id, (uint64_t)&state);
139ea0d856cSGavin Shan 	if (rc == OPAL_SUCCESS) {
140ea0d856cSGavin Shan 		ret = 0;
141ea0d856cSGavin Shan 		goto exit;
142ea0d856cSGavin Shan 	} else if (rc != OPAL_ASYNC_COMPLETION) {
143ea0d856cSGavin Shan 		ret = -EIO;
144ea0d856cSGavin Shan 		goto exit;
145ea0d856cSGavin Shan 	}
146ea0d856cSGavin Shan 
147ea0d856cSGavin Shan 	ret = opal_async_wait_response(token, &m);
148ea0d856cSGavin Shan 	if (ret < 0)
149ea0d856cSGavin Shan 		goto exit;
150ea0d856cSGavin Shan 
151ea0d856cSGavin Shan 	if (msg) {
152ea0d856cSGavin Shan 		ret = 1;
153ea0d856cSGavin Shan 		memcpy(msg, &m, sizeof(m));
154ea0d856cSGavin Shan 	}
155ea0d856cSGavin Shan 
156ea0d856cSGavin Shan exit:
157ea0d856cSGavin Shan 	opal_async_release_token(token);
158ea0d856cSGavin Shan 	return ret;
159ea0d856cSGavin Shan }
160ea0d856cSGavin Shan EXPORT_SYMBOL_GPL(pnv_pci_set_power_state);
161ea0d856cSGavin Shan 
16231bbd45aSRussell Currey /* Nicely print the contents of the PE State Tables (PEST). */
pnv_pci_dump_pest(__be64 pestA[],__be64 pestB[],int pest_size)16331bbd45aSRussell Currey static void pnv_pci_dump_pest(__be64 pestA[], __be64 pestB[], int pest_size)
16431bbd45aSRussell Currey {
16531bbd45aSRussell Currey 	__be64 prevA = ULONG_MAX, prevB = ULONG_MAX;
16631bbd45aSRussell Currey 	bool dup = false;
16731bbd45aSRussell Currey 	int i;
16831bbd45aSRussell Currey 
16931bbd45aSRussell Currey 	for (i = 0; i < pest_size; i++) {
17031bbd45aSRussell Currey 		__be64 peA = be64_to_cpu(pestA[i]);
17131bbd45aSRussell Currey 		__be64 peB = be64_to_cpu(pestB[i]);
17231bbd45aSRussell Currey 
17331bbd45aSRussell Currey 		if (peA != prevA || peB != prevB) {
17431bbd45aSRussell Currey 			if (dup) {
17531bbd45aSRussell Currey 				pr_info("PE[..%03x] A/B: as above\n", i-1);
17631bbd45aSRussell Currey 				dup = false;
17731bbd45aSRussell Currey 			}
17831bbd45aSRussell Currey 			prevA = peA;
17931bbd45aSRussell Currey 			prevB = peB;
18031bbd45aSRussell Currey 			if (peA & PNV_IODA_STOPPED_STATE ||
18131bbd45aSRussell Currey 			    peB & PNV_IODA_STOPPED_STATE)
18231bbd45aSRussell Currey 				pr_info("PE[%03x] A/B: %016llx %016llx\n",
18331bbd45aSRussell Currey 					i, peA, peB);
18431bbd45aSRussell Currey 		} else if (!dup && (peA & PNV_IODA_STOPPED_STATE ||
18531bbd45aSRussell Currey 				    peB & PNV_IODA_STOPPED_STATE)) {
18631bbd45aSRussell Currey 			dup = true;
18731bbd45aSRussell Currey 		}
18831bbd45aSRussell Currey 	}
18931bbd45aSRussell Currey }
19031bbd45aSRussell Currey 
pnv_pci_dump_p7ioc_diag_data(struct pci_controller * hose,struct OpalIoPhbErrorCommon * common)19193aef2a7SGavin Shan static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose,
19293aef2a7SGavin Shan 					 struct OpalIoPhbErrorCommon *common)
193cee72d5bSBenjamin Herrenschmidt {
19493aef2a7SGavin Shan 	struct OpalIoP7IOCPhbErrorData *data;
195cee72d5bSBenjamin Herrenschmidt 
19693aef2a7SGavin Shan 	data = (struct OpalIoP7IOCPhbErrorData *)common;
1971f52f176SRussell Currey 	pr_info("P7IOC PHB#%x Diag-data (Version: %d)\n",
198f18440fbSGavin Shan 		hose->global_number, be32_to_cpu(common->version));
199cee72d5bSBenjamin Herrenschmidt 
200af87d2feSGavin Shan 	if (data->brdgCtl)
201af87d2feSGavin Shan 		pr_info("brdgCtl:     %08x\n",
202f18440fbSGavin Shan 			be32_to_cpu(data->brdgCtl));
203af87d2feSGavin Shan 	if (data->portStatusReg || data->rootCmplxStatus ||
204af87d2feSGavin Shan 	    data->busAgentStatus)
205af87d2feSGavin Shan 		pr_info("UtlSts:      %08x %08x %08x\n",
206f18440fbSGavin Shan 			be32_to_cpu(data->portStatusReg),
207f18440fbSGavin Shan 			be32_to_cpu(data->rootCmplxStatus),
208f18440fbSGavin Shan 			be32_to_cpu(data->busAgentStatus));
209af87d2feSGavin Shan 	if (data->deviceStatus || data->slotStatus   ||
210af87d2feSGavin Shan 	    data->linkStatus   || data->devCmdStatus ||
211af87d2feSGavin Shan 	    data->devSecStatus)
212af87d2feSGavin Shan 		pr_info("RootSts:     %08x %08x %08x %08x %08x\n",
213f18440fbSGavin Shan 			be32_to_cpu(data->deviceStatus),
214f18440fbSGavin Shan 			be32_to_cpu(data->slotStatus),
215f18440fbSGavin Shan 			be32_to_cpu(data->linkStatus),
216f18440fbSGavin Shan 			be32_to_cpu(data->devCmdStatus),
217f18440fbSGavin Shan 			be32_to_cpu(data->devSecStatus));
218af87d2feSGavin Shan 	if (data->rootErrorStatus   || data->uncorrErrorStatus ||
219af87d2feSGavin Shan 	    data->corrErrorStatus)
220af87d2feSGavin Shan 		pr_info("RootErrSts:  %08x %08x %08x\n",
221f18440fbSGavin Shan 			be32_to_cpu(data->rootErrorStatus),
222f18440fbSGavin Shan 			be32_to_cpu(data->uncorrErrorStatus),
223f18440fbSGavin Shan 			be32_to_cpu(data->corrErrorStatus));
224af87d2feSGavin Shan 	if (data->tlpHdr1 || data->tlpHdr2 ||
225af87d2feSGavin Shan 	    data->tlpHdr3 || data->tlpHdr4)
226af87d2feSGavin Shan 		pr_info("RootErrLog:  %08x %08x %08x %08x\n",
227f18440fbSGavin Shan 			be32_to_cpu(data->tlpHdr1),
228f18440fbSGavin Shan 			be32_to_cpu(data->tlpHdr2),
229f18440fbSGavin Shan 			be32_to_cpu(data->tlpHdr3),
230f18440fbSGavin Shan 			be32_to_cpu(data->tlpHdr4));
231af87d2feSGavin Shan 	if (data->sourceId || data->errorClass ||
232af87d2feSGavin Shan 	    data->correlator)
233af87d2feSGavin Shan 		pr_info("RootErrLog1: %08x %016llx %016llx\n",
234f18440fbSGavin Shan 			be32_to_cpu(data->sourceId),
235f18440fbSGavin Shan 			be64_to_cpu(data->errorClass),
236f18440fbSGavin Shan 			be64_to_cpu(data->correlator));
237af87d2feSGavin Shan 	if (data->p7iocPlssr || data->p7iocCsr)
238af87d2feSGavin Shan 		pr_info("PhbSts:      %016llx %016llx\n",
239f18440fbSGavin Shan 			be64_to_cpu(data->p7iocPlssr),
240f18440fbSGavin Shan 			be64_to_cpu(data->p7iocCsr));
241b34497d1SGavin Shan 	if (data->lemFir)
242af87d2feSGavin Shan 		pr_info("Lem:         %016llx %016llx %016llx\n",
243f18440fbSGavin Shan 			be64_to_cpu(data->lemFir),
244f18440fbSGavin Shan 			be64_to_cpu(data->lemErrorMask),
245f18440fbSGavin Shan 			be64_to_cpu(data->lemWOF));
246b34497d1SGavin Shan 	if (data->phbErrorStatus)
247af87d2feSGavin Shan 		pr_info("PhbErr:      %016llx %016llx %016llx %016llx\n",
248f18440fbSGavin Shan 			be64_to_cpu(data->phbErrorStatus),
249f18440fbSGavin Shan 			be64_to_cpu(data->phbFirstErrorStatus),
250f18440fbSGavin Shan 			be64_to_cpu(data->phbErrorLog0),
251f18440fbSGavin Shan 			be64_to_cpu(data->phbErrorLog1));
252b34497d1SGavin Shan 	if (data->mmioErrorStatus)
253af87d2feSGavin Shan 		pr_info("OutErr:      %016llx %016llx %016llx %016llx\n",
254f18440fbSGavin Shan 			be64_to_cpu(data->mmioErrorStatus),
255f18440fbSGavin Shan 			be64_to_cpu(data->mmioFirstErrorStatus),
256f18440fbSGavin Shan 			be64_to_cpu(data->mmioErrorLog0),
257f18440fbSGavin Shan 			be64_to_cpu(data->mmioErrorLog1));
258b34497d1SGavin Shan 	if (data->dma0ErrorStatus)
259af87d2feSGavin Shan 		pr_info("InAErr:      %016llx %016llx %016llx %016llx\n",
260f18440fbSGavin Shan 			be64_to_cpu(data->dma0ErrorStatus),
261f18440fbSGavin Shan 			be64_to_cpu(data->dma0FirstErrorStatus),
262f18440fbSGavin Shan 			be64_to_cpu(data->dma0ErrorLog0),
263f18440fbSGavin Shan 			be64_to_cpu(data->dma0ErrorLog1));
264b34497d1SGavin Shan 	if (data->dma1ErrorStatus)
265af87d2feSGavin Shan 		pr_info("InBErr:      %016llx %016llx %016llx %016llx\n",
266f18440fbSGavin Shan 			be64_to_cpu(data->dma1ErrorStatus),
267f18440fbSGavin Shan 			be64_to_cpu(data->dma1FirstErrorStatus),
268f18440fbSGavin Shan 			be64_to_cpu(data->dma1ErrorLog0),
269f18440fbSGavin Shan 			be64_to_cpu(data->dma1ErrorLog1));
270cee72d5bSBenjamin Herrenschmidt 
27131bbd45aSRussell Currey 	pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_P7IOC_NUM_PEST_REGS);
272cee72d5bSBenjamin Herrenschmidt }
273cee72d5bSBenjamin Herrenschmidt 
pnv_pci_dump_phb3_diag_data(struct pci_controller * hose,struct OpalIoPhbErrorCommon * common)27493aef2a7SGavin Shan static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
27593aef2a7SGavin Shan 					struct OpalIoPhbErrorCommon *common)
276cee72d5bSBenjamin Herrenschmidt {
27793aef2a7SGavin Shan 	struct OpalIoPhb3ErrorData *data;
27893aef2a7SGavin Shan 
27993aef2a7SGavin Shan 	data = (struct OpalIoPhb3ErrorData*)common;
2801f52f176SRussell Currey 	pr_info("PHB3 PHB#%x Diag-data (Version: %d)\n",
281ddf0322aSGuo Chao 		hose->global_number, be32_to_cpu(common->version));
282af87d2feSGavin Shan 	if (data->brdgCtl)
283af87d2feSGavin Shan 		pr_info("brdgCtl:     %08x\n",
284ddf0322aSGuo Chao 			be32_to_cpu(data->brdgCtl));
285af87d2feSGavin Shan 	if (data->portStatusReg || data->rootCmplxStatus ||
286af87d2feSGavin Shan 	    data->busAgentStatus)
287af87d2feSGavin Shan 		pr_info("UtlSts:      %08x %08x %08x\n",
288ddf0322aSGuo Chao 			be32_to_cpu(data->portStatusReg),
289ddf0322aSGuo Chao 			be32_to_cpu(data->rootCmplxStatus),
290ddf0322aSGuo Chao 			be32_to_cpu(data->busAgentStatus));
291af87d2feSGavin Shan 	if (data->deviceStatus || data->slotStatus   ||
292af87d2feSGavin Shan 	    data->linkStatus   || data->devCmdStatus ||
293af87d2feSGavin Shan 	    data->devSecStatus)
294af87d2feSGavin Shan 		pr_info("RootSts:     %08x %08x %08x %08x %08x\n",
295ddf0322aSGuo Chao 			be32_to_cpu(data->deviceStatus),
296ddf0322aSGuo Chao 			be32_to_cpu(data->slotStatus),
297ddf0322aSGuo Chao 			be32_to_cpu(data->linkStatus),
298ddf0322aSGuo Chao 			be32_to_cpu(data->devCmdStatus),
299ddf0322aSGuo Chao 			be32_to_cpu(data->devSecStatus));
300af87d2feSGavin Shan 	if (data->rootErrorStatus || data->uncorrErrorStatus ||
301af87d2feSGavin Shan 	    data->corrErrorStatus)
302af87d2feSGavin Shan 		pr_info("RootErrSts:  %08x %08x %08x\n",
303ddf0322aSGuo Chao 			be32_to_cpu(data->rootErrorStatus),
304ddf0322aSGuo Chao 			be32_to_cpu(data->uncorrErrorStatus),
305ddf0322aSGuo Chao 			be32_to_cpu(data->corrErrorStatus));
306af87d2feSGavin Shan 	if (data->tlpHdr1 || data->tlpHdr2 ||
307af87d2feSGavin Shan 	    data->tlpHdr3 || data->tlpHdr4)
308af87d2feSGavin Shan 		pr_info("RootErrLog:  %08x %08x %08x %08x\n",
309ddf0322aSGuo Chao 			be32_to_cpu(data->tlpHdr1),
310ddf0322aSGuo Chao 			be32_to_cpu(data->tlpHdr2),
311ddf0322aSGuo Chao 			be32_to_cpu(data->tlpHdr3),
312ddf0322aSGuo Chao 			be32_to_cpu(data->tlpHdr4));
313af87d2feSGavin Shan 	if (data->sourceId || data->errorClass ||
314af87d2feSGavin Shan 	    data->correlator)
315af87d2feSGavin Shan 		pr_info("RootErrLog1: %08x %016llx %016llx\n",
316ddf0322aSGuo Chao 			be32_to_cpu(data->sourceId),
317ddf0322aSGuo Chao 			be64_to_cpu(data->errorClass),
318ddf0322aSGuo Chao 			be64_to_cpu(data->correlator));
319b34497d1SGavin Shan 	if (data->nFir)
320af87d2feSGavin Shan 		pr_info("nFir:        %016llx %016llx %016llx\n",
321ddf0322aSGuo Chao 			be64_to_cpu(data->nFir),
322ddf0322aSGuo Chao 			be64_to_cpu(data->nFirMask),
323ddf0322aSGuo Chao 			be64_to_cpu(data->nFirWOF));
324af87d2feSGavin Shan 	if (data->phbPlssr || data->phbCsr)
325af87d2feSGavin Shan 		pr_info("PhbSts:      %016llx %016llx\n",
326ddf0322aSGuo Chao 			be64_to_cpu(data->phbPlssr),
327ddf0322aSGuo Chao 			be64_to_cpu(data->phbCsr));
328b34497d1SGavin Shan 	if (data->lemFir)
329af87d2feSGavin Shan 		pr_info("Lem:         %016llx %016llx %016llx\n",
330ddf0322aSGuo Chao 			be64_to_cpu(data->lemFir),
331ddf0322aSGuo Chao 			be64_to_cpu(data->lemErrorMask),
332ddf0322aSGuo Chao 			be64_to_cpu(data->lemWOF));
333b34497d1SGavin Shan 	if (data->phbErrorStatus)
334af87d2feSGavin Shan 		pr_info("PhbErr:      %016llx %016llx %016llx %016llx\n",
335ddf0322aSGuo Chao 			be64_to_cpu(data->phbErrorStatus),
336ddf0322aSGuo Chao 			be64_to_cpu(data->phbFirstErrorStatus),
337ddf0322aSGuo Chao 			be64_to_cpu(data->phbErrorLog0),
338ddf0322aSGuo Chao 			be64_to_cpu(data->phbErrorLog1));
339b34497d1SGavin Shan 	if (data->mmioErrorStatus)
340af87d2feSGavin Shan 		pr_info("OutErr:      %016llx %016llx %016llx %016llx\n",
341ddf0322aSGuo Chao 			be64_to_cpu(data->mmioErrorStatus),
342ddf0322aSGuo Chao 			be64_to_cpu(data->mmioFirstErrorStatus),
343ddf0322aSGuo Chao 			be64_to_cpu(data->mmioErrorLog0),
344ddf0322aSGuo Chao 			be64_to_cpu(data->mmioErrorLog1));
345b34497d1SGavin Shan 	if (data->dma0ErrorStatus)
346af87d2feSGavin Shan 		pr_info("InAErr:      %016llx %016llx %016llx %016llx\n",
347ddf0322aSGuo Chao 			be64_to_cpu(data->dma0ErrorStatus),
348ddf0322aSGuo Chao 			be64_to_cpu(data->dma0FirstErrorStatus),
349ddf0322aSGuo Chao 			be64_to_cpu(data->dma0ErrorLog0),
350ddf0322aSGuo Chao 			be64_to_cpu(data->dma0ErrorLog1));
351b34497d1SGavin Shan 	if (data->dma1ErrorStatus)
352af87d2feSGavin Shan 		pr_info("InBErr:      %016llx %016llx %016llx %016llx\n",
353ddf0322aSGuo Chao 			be64_to_cpu(data->dma1ErrorStatus),
354ddf0322aSGuo Chao 			be64_to_cpu(data->dma1FirstErrorStatus),
355ddf0322aSGuo Chao 			be64_to_cpu(data->dma1ErrorLog0),
356ddf0322aSGuo Chao 			be64_to_cpu(data->dma1ErrorLog1));
35793aef2a7SGavin Shan 
35831bbd45aSRussell Currey 	pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_PHB3_NUM_PEST_REGS);
35993aef2a7SGavin Shan }
36093aef2a7SGavin Shan 
pnv_pci_dump_phb4_diag_data(struct pci_controller * hose,struct OpalIoPhbErrorCommon * common)361a4b48ba9SRussell Currey static void pnv_pci_dump_phb4_diag_data(struct pci_controller *hose,
362a4b48ba9SRussell Currey 					struct OpalIoPhbErrorCommon *common)
363a4b48ba9SRussell Currey {
364a4b48ba9SRussell Currey 	struct OpalIoPhb4ErrorData *data;
365a4b48ba9SRussell Currey 
366a4b48ba9SRussell Currey 	data = (struct OpalIoPhb4ErrorData*)common;
367a4b48ba9SRussell Currey 	pr_info("PHB4 PHB#%d Diag-data (Version: %d)\n",
368a4b48ba9SRussell Currey 		hose->global_number, be32_to_cpu(common->version));
369a4b48ba9SRussell Currey 	if (data->brdgCtl)
370a4b48ba9SRussell Currey 		pr_info("brdgCtl:    %08x\n",
371a4b48ba9SRussell Currey 			be32_to_cpu(data->brdgCtl));
372a4b48ba9SRussell Currey 	if (data->deviceStatus || data->slotStatus   ||
373a4b48ba9SRussell Currey 	    data->linkStatus   || data->devCmdStatus ||
374a4b48ba9SRussell Currey 	    data->devSecStatus)
375a4b48ba9SRussell Currey 		pr_info("RootSts:    %08x %08x %08x %08x %08x\n",
376a4b48ba9SRussell Currey 			be32_to_cpu(data->deviceStatus),
377a4b48ba9SRussell Currey 			be32_to_cpu(data->slotStatus),
378a4b48ba9SRussell Currey 			be32_to_cpu(data->linkStatus),
379a4b48ba9SRussell Currey 			be32_to_cpu(data->devCmdStatus),
380a4b48ba9SRussell Currey 			be32_to_cpu(data->devSecStatus));
381a4b48ba9SRussell Currey 	if (data->rootErrorStatus || data->uncorrErrorStatus ||
382a4b48ba9SRussell Currey 	    data->corrErrorStatus)
383a4b48ba9SRussell Currey 		pr_info("RootErrSts: %08x %08x %08x\n",
384a4b48ba9SRussell Currey 			be32_to_cpu(data->rootErrorStatus),
385a4b48ba9SRussell Currey 			be32_to_cpu(data->uncorrErrorStatus),
386a4b48ba9SRussell Currey 			be32_to_cpu(data->corrErrorStatus));
387a4b48ba9SRussell Currey 	if (data->tlpHdr1 || data->tlpHdr2 ||
388a4b48ba9SRussell Currey 	    data->tlpHdr3 || data->tlpHdr4)
389a4b48ba9SRussell Currey 		pr_info("RootErrLog: %08x %08x %08x %08x\n",
390a4b48ba9SRussell Currey 			be32_to_cpu(data->tlpHdr1),
391a4b48ba9SRussell Currey 			be32_to_cpu(data->tlpHdr2),
392a4b48ba9SRussell Currey 			be32_to_cpu(data->tlpHdr3),
393a4b48ba9SRussell Currey 			be32_to_cpu(data->tlpHdr4));
394a4b48ba9SRussell Currey 	if (data->sourceId)
395a4b48ba9SRussell Currey 		pr_info("sourceId:   %08x\n", be32_to_cpu(data->sourceId));
396a4b48ba9SRussell Currey 	if (data->nFir)
397a4b48ba9SRussell Currey 		pr_info("nFir:       %016llx %016llx %016llx\n",
398a4b48ba9SRussell Currey 			be64_to_cpu(data->nFir),
399a4b48ba9SRussell Currey 			be64_to_cpu(data->nFirMask),
400a4b48ba9SRussell Currey 			be64_to_cpu(data->nFirWOF));
401a4b48ba9SRussell Currey 	if (data->phbPlssr || data->phbCsr)
402a4b48ba9SRussell Currey 		pr_info("PhbSts:     %016llx %016llx\n",
403a4b48ba9SRussell Currey 			be64_to_cpu(data->phbPlssr),
404a4b48ba9SRussell Currey 			be64_to_cpu(data->phbCsr));
405a4b48ba9SRussell Currey 	if (data->lemFir)
406a4b48ba9SRussell Currey 		pr_info("Lem:        %016llx %016llx %016llx\n",
407a4b48ba9SRussell Currey 			be64_to_cpu(data->lemFir),
408a4b48ba9SRussell Currey 			be64_to_cpu(data->lemErrorMask),
409a4b48ba9SRussell Currey 			be64_to_cpu(data->lemWOF));
410a4b48ba9SRussell Currey 	if (data->phbErrorStatus)
411a4b48ba9SRussell Currey 		pr_info("PhbErr:     %016llx %016llx %016llx %016llx\n",
412a4b48ba9SRussell Currey 			be64_to_cpu(data->phbErrorStatus),
413a4b48ba9SRussell Currey 			be64_to_cpu(data->phbFirstErrorStatus),
414a4b48ba9SRussell Currey 			be64_to_cpu(data->phbErrorLog0),
415a4b48ba9SRussell Currey 			be64_to_cpu(data->phbErrorLog1));
416a4b48ba9SRussell Currey 	if (data->phbTxeErrorStatus)
417a4b48ba9SRussell Currey 		pr_info("PhbTxeErr:  %016llx %016llx %016llx %016llx\n",
418a4b48ba9SRussell Currey 			be64_to_cpu(data->phbTxeErrorStatus),
419a4b48ba9SRussell Currey 			be64_to_cpu(data->phbTxeFirstErrorStatus),
420a4b48ba9SRussell Currey 			be64_to_cpu(data->phbTxeErrorLog0),
421a4b48ba9SRussell Currey 			be64_to_cpu(data->phbTxeErrorLog1));
422a4b48ba9SRussell Currey 	if (data->phbRxeArbErrorStatus)
423a4b48ba9SRussell Currey 		pr_info("RxeArbErr:  %016llx %016llx %016llx %016llx\n",
424a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeArbErrorStatus),
425a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeArbFirstErrorStatus),
426a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeArbErrorLog0),
427a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeArbErrorLog1));
428a4b48ba9SRussell Currey 	if (data->phbRxeMrgErrorStatus)
429a4b48ba9SRussell Currey 		pr_info("RxeMrgErr:  %016llx %016llx %016llx %016llx\n",
430a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeMrgErrorStatus),
431a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeMrgFirstErrorStatus),
432a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeMrgErrorLog0),
433a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeMrgErrorLog1));
434a4b48ba9SRussell Currey 	if (data->phbRxeTceErrorStatus)
435a4b48ba9SRussell Currey 		pr_info("RxeTceErr:  %016llx %016llx %016llx %016llx\n",
436a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeTceErrorStatus),
437a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeTceFirstErrorStatus),
438a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeTceErrorLog0),
439a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRxeTceErrorLog1));
440a4b48ba9SRussell Currey 
441a4b48ba9SRussell Currey 	if (data->phbPblErrorStatus)
442a4b48ba9SRussell Currey 		pr_info("PblErr:     %016llx %016llx %016llx %016llx\n",
443a4b48ba9SRussell Currey 			be64_to_cpu(data->phbPblErrorStatus),
444a4b48ba9SRussell Currey 			be64_to_cpu(data->phbPblFirstErrorStatus),
445a4b48ba9SRussell Currey 			be64_to_cpu(data->phbPblErrorLog0),
446a4b48ba9SRussell Currey 			be64_to_cpu(data->phbPblErrorLog1));
447a4b48ba9SRussell Currey 	if (data->phbPcieDlpErrorStatus)
448a4b48ba9SRussell Currey 		pr_info("PcieDlp:    %016llx %016llx %016llx\n",
449a4b48ba9SRussell Currey 			be64_to_cpu(data->phbPcieDlpErrorLog1),
450a4b48ba9SRussell Currey 			be64_to_cpu(data->phbPcieDlpErrorLog2),
451a4b48ba9SRussell Currey 			be64_to_cpu(data->phbPcieDlpErrorStatus));
452a4b48ba9SRussell Currey 	if (data->phbRegbErrorStatus)
453a4b48ba9SRussell Currey 		pr_info("RegbErr:    %016llx %016llx %016llx %016llx\n",
454a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRegbErrorStatus),
455a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRegbFirstErrorStatus),
456a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRegbErrorLog0),
457a4b48ba9SRussell Currey 			be64_to_cpu(data->phbRegbErrorLog1));
458a4b48ba9SRussell Currey 
459a4b48ba9SRussell Currey 
460a4b48ba9SRussell Currey 	pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_PHB4_NUM_PEST_REGS);
461a4b48ba9SRussell Currey }
462a4b48ba9SRussell Currey 
pnv_pci_dump_phb_diag_data(struct pci_controller * hose,unsigned char * log_buff)46393aef2a7SGavin Shan void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
46493aef2a7SGavin Shan 				unsigned char *log_buff)
46593aef2a7SGavin Shan {
46693aef2a7SGavin Shan 	struct OpalIoPhbErrorCommon *common;
46793aef2a7SGavin Shan 
46893aef2a7SGavin Shan 	if (!hose || !log_buff)
46993aef2a7SGavin Shan 		return;
47093aef2a7SGavin Shan 
47193aef2a7SGavin Shan 	common = (struct OpalIoPhbErrorCommon *)log_buff;
472ddf0322aSGuo Chao 	switch (be32_to_cpu(common->ioType)) {
47393aef2a7SGavin Shan 	case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
47493aef2a7SGavin Shan 		pnv_pci_dump_p7ioc_diag_data(hose, common);
47593aef2a7SGavin Shan 		break;
47693aef2a7SGavin Shan 	case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
47793aef2a7SGavin Shan 		pnv_pci_dump_phb3_diag_data(hose, common);
478cee72d5bSBenjamin Herrenschmidt 		break;
479a4b48ba9SRussell Currey 	case OPAL_PHB_ERROR_DATA_TYPE_PHB4:
480a4b48ba9SRussell Currey 		pnv_pci_dump_phb4_diag_data(hose, common);
481a4b48ba9SRussell Currey 		break;
482cee72d5bSBenjamin Herrenschmidt 	default:
48393aef2a7SGavin Shan 		pr_warn("%s: Unrecognized ioType %d\n",
484ddf0322aSGuo Chao 			__func__, be32_to_cpu(common->ioType));
485cee72d5bSBenjamin Herrenschmidt 	}
486cee72d5bSBenjamin Herrenschmidt }
487cee72d5bSBenjamin Herrenschmidt 
pnv_pci_handle_eeh_config(struct pnv_phb * phb,u32 pe_no)488cee72d5bSBenjamin Herrenschmidt static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
489cee72d5bSBenjamin Herrenschmidt {
490cee72d5bSBenjamin Herrenschmidt 	unsigned long flags, rc;
49198fd7002SGavin Shan 	int has_diag, ret = 0;
492cee72d5bSBenjamin Herrenschmidt 
493cee72d5bSBenjamin Herrenschmidt 	spin_lock_irqsave(&phb->lock, flags);
494cee72d5bSBenjamin Herrenschmidt 
49598fd7002SGavin Shan 	/* Fetch PHB diag-data */
4965cb1f8fdSRussell Currey 	rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
4975cb1f8fdSRussell Currey 					 phb->diag_data_size);
498cee72d5bSBenjamin Herrenschmidt 	has_diag = (rc == OPAL_SUCCESS);
499cee72d5bSBenjamin Herrenschmidt 
50098fd7002SGavin Shan 	/* If PHB supports compound PE, to handle it */
50198fd7002SGavin Shan 	if (phb->unfreeze_pe) {
50298fd7002SGavin Shan 		ret = phb->unfreeze_pe(phb,
50398fd7002SGavin Shan 				       pe_no,
50498fd7002SGavin Shan 				       OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
50598fd7002SGavin Shan 	} else {
50698fd7002SGavin Shan 		rc = opal_pci_eeh_freeze_clear(phb->opal_id,
50798fd7002SGavin Shan 					     pe_no,
508cee72d5bSBenjamin Herrenschmidt 					     OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
509cee72d5bSBenjamin Herrenschmidt 		if (rc) {
51098fd7002SGavin Shan 			pr_warn("%s: Failure %ld clearing frozen "
51198fd7002SGavin Shan 				"PHB#%x-PE#%x\n",
51298fd7002SGavin Shan 				__func__, rc, phb->hose->global_number,
51398fd7002SGavin Shan 				pe_no);
51498fd7002SGavin Shan 			ret = -EIO;
51598fd7002SGavin Shan 		}
51698fd7002SGavin Shan 	}
517cee72d5bSBenjamin Herrenschmidt 
51898fd7002SGavin Shan 	/*
51998fd7002SGavin Shan 	 * For now, let's only display the diag buffer when we fail to clear
520cee72d5bSBenjamin Herrenschmidt 	 * the EEH status. We'll do more sensible things later when we have
521cee72d5bSBenjamin Herrenschmidt 	 * proper EEH support. We need to make sure we don't pollute ourselves
522cee72d5bSBenjamin Herrenschmidt 	 * with the normal errors generated when probing empty slots
523cee72d5bSBenjamin Herrenschmidt 	 */
52498fd7002SGavin Shan 	if (has_diag && ret)
5255cb1f8fdSRussell Currey 		pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
526cee72d5bSBenjamin Herrenschmidt 
527cee72d5bSBenjamin Herrenschmidt 	spin_unlock_irqrestore(&phb->lock, flags);
528cee72d5bSBenjamin Herrenschmidt }
529cee72d5bSBenjamin Herrenschmidt 
pnv_pci_config_check_eeh(struct pci_dn * pdn)5303532a741SGavin Shan static void pnv_pci_config_check_eeh(struct pci_dn *pdn)
53161305a96SBenjamin Herrenschmidt {
5323532a741SGavin Shan 	struct pnv_phb *phb = pdn->phb->private_data;
533c2057701SAlexey Kardashevskiy 	u8	fstate = 0;
534c2057701SAlexey Kardashevskiy 	__be16	pcierr = 0;
535689ee8c9SGavin Shan 	unsigned int pe_no;
53698fd7002SGavin Shan 	s64	rc;
53761305a96SBenjamin Herrenschmidt 
5389bf41be6SGavin Shan 	/*
5399bf41be6SGavin Shan 	 * Get the PE#. During the PCI probe stage, we might not
5409bf41be6SGavin Shan 	 * setup that yet. So all ER errors should be mapped to
54136954dc7SGavin Shan 	 * reserved PE.
5429bf41be6SGavin Shan 	 */
5433532a741SGavin Shan 	pe_no = pdn->pe_number;
54436954dc7SGavin Shan 	if (pe_no == IODA_INVALID_PE) {
54592b8f137SGavin Shan 		pe_no = phb->ioda.reserved_pe_idx;
54636954dc7SGavin Shan 	}
54761305a96SBenjamin Herrenschmidt 
54898fd7002SGavin Shan 	/*
54998fd7002SGavin Shan 	 * Fetch frozen state. If the PHB support compound PE,
55098fd7002SGavin Shan 	 * we need handle that case.
55198fd7002SGavin Shan 	 */
55298fd7002SGavin Shan 	if (phb->get_pe_state) {
55398fd7002SGavin Shan 		fstate = phb->get_pe_state(phb, pe_no);
55498fd7002SGavin Shan 	} else {
55598fd7002SGavin Shan 		rc = opal_pci_eeh_freeze_status(phb->opal_id,
55698fd7002SGavin Shan 						pe_no,
55798fd7002SGavin Shan 						&fstate,
55898fd7002SGavin Shan 						&pcierr,
55961305a96SBenjamin Herrenschmidt 						NULL);
56061305a96SBenjamin Herrenschmidt 		if (rc) {
56198fd7002SGavin Shan 			pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
56298fd7002SGavin Shan 				__func__, rc, phb->hose->global_number, pe_no);
56361305a96SBenjamin Herrenschmidt 			return;
56461305a96SBenjamin Herrenschmidt 		}
56598fd7002SGavin Shan 	}
56698fd7002SGavin Shan 
5671f52f176SRussell Currey 	pr_devel(" -> EEH check, bdfn=%04x PE#%x fstate=%x\n",
5683532a741SGavin Shan 		 (pdn->busno << 8) | (pdn->devfn), pe_no, fstate);
56998fd7002SGavin Shan 
57098fd7002SGavin Shan 	/* Clear the frozen state if applicable */
57198fd7002SGavin Shan 	if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
57298fd7002SGavin Shan 	    fstate == OPAL_EEH_STOPPED_DMA_FREEZE  ||
57398fd7002SGavin Shan 	    fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
57498fd7002SGavin Shan 		/*
57598fd7002SGavin Shan 		 * If PHB supports compound PE, freeze it for
57698fd7002SGavin Shan 		 * consistency.
57798fd7002SGavin Shan 		 */
57898fd7002SGavin Shan 		if (phb->freeze_pe)
57998fd7002SGavin Shan 			phb->freeze_pe(phb, pe_no);
58098fd7002SGavin Shan 
581cee72d5bSBenjamin Herrenschmidt 		pnv_pci_handle_eeh_config(phb, pe_no);
58261305a96SBenjamin Herrenschmidt 	}
58398fd7002SGavin Shan }
58461305a96SBenjamin Herrenschmidt 
pnv_pci_cfg_read(struct pci_dn * pdn,int where,int size,u32 * val)5853532a741SGavin Shan int pnv_pci_cfg_read(struct pci_dn *pdn,
58661305a96SBenjamin Herrenschmidt 		     int where, int size, u32 *val)
58761305a96SBenjamin Herrenschmidt {
5889bf41be6SGavin Shan 	struct pnv_phb *phb = pdn->phb->private_data;
5899bf41be6SGavin Shan 	u32 bdfn = (pdn->busno << 8) | pdn->devfn;
59061305a96SBenjamin Herrenschmidt 	s64 rc;
59161305a96SBenjamin Herrenschmidt 
59261305a96SBenjamin Herrenschmidt 	switch (size) {
59361305a96SBenjamin Herrenschmidt 	case 1: {
59461305a96SBenjamin Herrenschmidt 		u8 v8;
59561305a96SBenjamin Herrenschmidt 		rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
59661305a96SBenjamin Herrenschmidt 		*val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
59761305a96SBenjamin Herrenschmidt 		break;
59861305a96SBenjamin Herrenschmidt 	}
59961305a96SBenjamin Herrenschmidt 	case 2: {
6003a1a4661SBenjamin Herrenschmidt 		__be16 v16;
60161305a96SBenjamin Herrenschmidt 		rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
60261305a96SBenjamin Herrenschmidt 						   &v16);
6033a1a4661SBenjamin Herrenschmidt 		*val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
60461305a96SBenjamin Herrenschmidt 		break;
60561305a96SBenjamin Herrenschmidt 	}
60661305a96SBenjamin Herrenschmidt 	case 4: {
6073a1a4661SBenjamin Herrenschmidt 		__be32 v32;
60861305a96SBenjamin Herrenschmidt 		rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
6093a1a4661SBenjamin Herrenschmidt 		*val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
61061305a96SBenjamin Herrenschmidt 		break;
61161305a96SBenjamin Herrenschmidt 	}
61261305a96SBenjamin Herrenschmidt 	default:
61361305a96SBenjamin Herrenschmidt 		return PCIBIOS_FUNC_NOT_SUPPORTED;
61461305a96SBenjamin Herrenschmidt 	}
615d0914f50SGavin Shan 
6169e447547SAlexey Kardashevskiy 	pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
6179bf41be6SGavin Shan 		 __func__, pdn->busno, pdn->devfn, where, size, *val);
61861305a96SBenjamin Herrenschmidt 	return PCIBIOS_SUCCESSFUL;
61961305a96SBenjamin Herrenschmidt }
62061305a96SBenjamin Herrenschmidt 
pnv_pci_cfg_write(struct pci_dn * pdn,int where,int size,u32 val)6213532a741SGavin Shan int pnv_pci_cfg_write(struct pci_dn *pdn,
62261305a96SBenjamin Herrenschmidt 		      int where, int size, u32 val)
62361305a96SBenjamin Herrenschmidt {
6249bf41be6SGavin Shan 	struct pnv_phb *phb = pdn->phb->private_data;
6259bf41be6SGavin Shan 	u32 bdfn = (pdn->busno << 8) | pdn->devfn;
62661305a96SBenjamin Herrenschmidt 
6279e447547SAlexey Kardashevskiy 	pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
6289e447547SAlexey Kardashevskiy 		 __func__, pdn->busno, pdn->devfn, where, size, val);
62961305a96SBenjamin Herrenschmidt 	switch (size) {
63061305a96SBenjamin Herrenschmidt 	case 1:
63161305a96SBenjamin Herrenschmidt 		opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
63261305a96SBenjamin Herrenschmidt 		break;
63361305a96SBenjamin Herrenschmidt 	case 2:
63461305a96SBenjamin Herrenschmidt 		opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
63561305a96SBenjamin Herrenschmidt 		break;
63661305a96SBenjamin Herrenschmidt 	case 4:
63761305a96SBenjamin Herrenschmidt 		opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
63861305a96SBenjamin Herrenschmidt 		break;
63961305a96SBenjamin Herrenschmidt 	default:
64061305a96SBenjamin Herrenschmidt 		return PCIBIOS_FUNC_NOT_SUPPORTED;
64161305a96SBenjamin Herrenschmidt 	}
642be7e7446SGavin Shan 
64361305a96SBenjamin Herrenschmidt 	return PCIBIOS_SUCCESSFUL;
64461305a96SBenjamin Herrenschmidt }
64561305a96SBenjamin Herrenschmidt 
64673e6e4e0SNick Desaulniers #ifdef CONFIG_EEH
pnv_pci_cfg_check(struct pci_dn * pdn)6473532a741SGavin Shan static bool pnv_pci_cfg_check(struct pci_dn *pdn)
648d0914f50SGavin Shan {
649d0914f50SGavin Shan 	struct eeh_dev *edev = NULL;
6503532a741SGavin Shan 	struct pnv_phb *phb = pdn->phb->private_data;
651d0914f50SGavin Shan 
652d0914f50SGavin Shan 	/* EEH not enabled ? */
653d0914f50SGavin Shan 	if (!(phb->flags & PNV_PHB_FLAG_EEH))
654d0914f50SGavin Shan 		return true;
655d0914f50SGavin Shan 
656d2b0f6f7SGavin Shan 	/* PE reset or device removed ? */
6573532a741SGavin Shan 	edev = pdn->edev;
658d2b0f6f7SGavin Shan 	if (edev) {
659d2b0f6f7SGavin Shan 		if (edev->pe &&
6608a6b3710SGavin Shan 		    (edev->pe->state & EEH_PE_CFG_BLOCKED))
661d0914f50SGavin Shan 			return false;
662d0914f50SGavin Shan 
663d2b0f6f7SGavin Shan 		if (edev->mode & EEH_DEV_REMOVED)
664d2b0f6f7SGavin Shan 			return false;
665d2b0f6f7SGavin Shan 	}
666d2b0f6f7SGavin Shan 
667d0914f50SGavin Shan 	return true;
668d0914f50SGavin Shan }
669d0914f50SGavin Shan #else
pnv_pci_cfg_check(struct pci_dn * pdn)6703532a741SGavin Shan static inline pnv_pci_cfg_check(struct pci_dn *pdn)
671d0914f50SGavin Shan {
672d0914f50SGavin Shan 	return true;
673d0914f50SGavin Shan }
674d0914f50SGavin Shan #endif /* CONFIG_EEH */
675d0914f50SGavin Shan 
pnv_pci_read_config(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)6769bf41be6SGavin Shan static int pnv_pci_read_config(struct pci_bus *bus,
6779bf41be6SGavin Shan 			       unsigned int devfn,
6789bf41be6SGavin Shan 			       int where, int size, u32 *val)
6799bf41be6SGavin Shan {
6809bf41be6SGavin Shan 	struct pci_dn *pdn;
681d0914f50SGavin Shan 	struct pnv_phb *phb;
682d0914f50SGavin Shan 	int ret;
6839bf41be6SGavin Shan 
6849bf41be6SGavin Shan 	*val = 0xFFFFFFFF;
6853532a741SGavin Shan 	pdn = pci_get_pdn_by_devfn(bus, devfn);
6863532a741SGavin Shan 	if (!pdn)
6879bf41be6SGavin Shan 		return PCIBIOS_DEVICE_NOT_FOUND;
6889bf41be6SGavin Shan 
6893532a741SGavin Shan 	if (!pnv_pci_cfg_check(pdn))
6903532a741SGavin Shan 		return PCIBIOS_DEVICE_NOT_FOUND;
6913532a741SGavin Shan 
6923532a741SGavin Shan 	ret = pnv_pci_cfg_read(pdn, where, size, val);
6933532a741SGavin Shan 	phb = pdn->phb->private_data;
6943532a741SGavin Shan 	if (phb->flags & PNV_PHB_FLAG_EEH && pdn->edev) {
695d0914f50SGavin Shan 		if (*val == EEH_IO_ERROR_VALUE(size) &&
6963532a741SGavin Shan 		    eeh_dev_check_failure(pdn->edev))
697d0914f50SGavin Shan                         return PCIBIOS_DEVICE_NOT_FOUND;
698d0914f50SGavin Shan 	} else {
6993532a741SGavin Shan 		pnv_pci_config_check_eeh(pdn);
700d0914f50SGavin Shan 	}
701d0914f50SGavin Shan 
702d0914f50SGavin Shan 	return ret;
7039bf41be6SGavin Shan }
7049bf41be6SGavin Shan 
pnv_pci_write_config(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)7059bf41be6SGavin Shan static int pnv_pci_write_config(struct pci_bus *bus,
7069bf41be6SGavin Shan 				unsigned int devfn,
7079bf41be6SGavin Shan 				int where, int size, u32 val)
7089bf41be6SGavin Shan {
7099bf41be6SGavin Shan 	struct pci_dn *pdn;
710d0914f50SGavin Shan 	struct pnv_phb *phb;
711d0914f50SGavin Shan 	int ret;
7129bf41be6SGavin Shan 
7133532a741SGavin Shan 	pdn = pci_get_pdn_by_devfn(bus, devfn);
7143532a741SGavin Shan 	if (!pdn)
7159bf41be6SGavin Shan 		return PCIBIOS_DEVICE_NOT_FOUND;
716d0914f50SGavin Shan 
7173532a741SGavin Shan 	if (!pnv_pci_cfg_check(pdn))
7183532a741SGavin Shan 		return PCIBIOS_DEVICE_NOT_FOUND;
7193532a741SGavin Shan 
7203532a741SGavin Shan 	ret = pnv_pci_cfg_write(pdn, where, size, val);
7213532a741SGavin Shan 	phb = pdn->phb->private_data;
722d0914f50SGavin Shan 	if (!(phb->flags & PNV_PHB_FLAG_EEH))
7233532a741SGavin Shan 		pnv_pci_config_check_eeh(pdn);
724d0914f50SGavin Shan 
725d0914f50SGavin Shan 	return ret;
7269bf41be6SGavin Shan }
7279bf41be6SGavin Shan 
72861305a96SBenjamin Herrenschmidt struct pci_ops pnv_pci_ops = {
72961305a96SBenjamin Herrenschmidt 	.read  = pnv_pci_read_config,
73061305a96SBenjamin Herrenschmidt 	.write = pnv_pci_write_config,
73161305a96SBenjamin Herrenschmidt };
73261305a96SBenjamin Herrenschmidt 
pnv_pci_table_alloc(int nid)7330eaf4defSAlexey Kardashevskiy struct iommu_table *pnv_pci_table_alloc(int nid)
7340eaf4defSAlexey Kardashevskiy {
7350eaf4defSAlexey Kardashevskiy 	struct iommu_table *tbl;
7360eaf4defSAlexey Kardashevskiy 
7370eaf4defSAlexey Kardashevskiy 	tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
73882eae1afSAlexey Kardashevskiy 	if (!tbl)
73982eae1afSAlexey Kardashevskiy 		return NULL;
74082eae1afSAlexey Kardashevskiy 
7410eaf4defSAlexey Kardashevskiy 	INIT_LIST_HEAD_RCU(&tbl->it_group_list);
742e5afdf9dSAlexey Kardashevskiy 	kref_init(&tbl->it_kref);
7430eaf4defSAlexey Kardashevskiy 
7440eaf4defSAlexey Kardashevskiy 	return tbl;
7450eaf4defSAlexey Kardashevskiy }
7460eaf4defSAlexey Kardashevskiy 
pnv_pci_get_phb_node(struct pci_dev * dev)747d6a90bb8SPhilippe Bergheaud struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
748d6a90bb8SPhilippe Bergheaud {
749d6a90bb8SPhilippe Bergheaud 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
750d6a90bb8SPhilippe Bergheaud 
751d6a90bb8SPhilippe Bergheaud 	return of_node_get(hose->dn);
752d6a90bb8SPhilippe Bergheaud }
753d6a90bb8SPhilippe Bergheaud EXPORT_SYMBOL(pnv_pci_get_phb_node);
754d6a90bb8SPhilippe Bergheaud 
pnv_pci_set_tunnel_bar(struct pci_dev * dev,u64 addr,int enable)755d6a90bb8SPhilippe Bergheaud int pnv_pci_set_tunnel_bar(struct pci_dev *dev, u64 addr, int enable)
756d6a90bb8SPhilippe Bergheaud {
7575609ffddSOliver O'Halloran 	struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
758d6a90bb8SPhilippe Bergheaud 	u64 tunnel_bar;
7595609ffddSOliver O'Halloran 	__be64 val;
760d6a90bb8SPhilippe Bergheaud 	int rc;
761d6a90bb8SPhilippe Bergheaud 
762d6a90bb8SPhilippe Bergheaud 	if (!opal_check_token(OPAL_PCI_GET_PBCQ_TUNNEL_BAR))
763d6a90bb8SPhilippe Bergheaud 		return -ENXIO;
764d6a90bb8SPhilippe Bergheaud 	if (!opal_check_token(OPAL_PCI_SET_PBCQ_TUNNEL_BAR))
765d6a90bb8SPhilippe Bergheaud 		return -ENXIO;
766d6a90bb8SPhilippe Bergheaud 
767d6a90bb8SPhilippe Bergheaud 	mutex_lock(&tunnel_mutex);
768d6a90bb8SPhilippe Bergheaud 	rc = opal_pci_get_pbcq_tunnel_bar(phb->opal_id, &val);
769d6a90bb8SPhilippe Bergheaud 	if (rc != OPAL_SUCCESS) {
770d6a90bb8SPhilippe Bergheaud 		rc = -EIO;
771d6a90bb8SPhilippe Bergheaud 		goto out;
772d6a90bb8SPhilippe Bergheaud 	}
773d6a90bb8SPhilippe Bergheaud 	tunnel_bar = be64_to_cpu(val);
774d6a90bb8SPhilippe Bergheaud 	if (enable) {
775d6a90bb8SPhilippe Bergheaud 		/*
776d6a90bb8SPhilippe Bergheaud 		* Only one device per PHB can use atomics.
777d6a90bb8SPhilippe Bergheaud 		* Our policy is first-come, first-served.
778d6a90bb8SPhilippe Bergheaud 		*/
779d6a90bb8SPhilippe Bergheaud 		if (tunnel_bar) {
780d6a90bb8SPhilippe Bergheaud 			if (tunnel_bar != addr)
781d6a90bb8SPhilippe Bergheaud 				rc = -EBUSY;
782d6a90bb8SPhilippe Bergheaud 			else
783d6a90bb8SPhilippe Bergheaud 				rc = 0;	/* Setting same address twice is ok */
784d6a90bb8SPhilippe Bergheaud 			goto out;
785d6a90bb8SPhilippe Bergheaud 		}
786d6a90bb8SPhilippe Bergheaud 	} else {
787d6a90bb8SPhilippe Bergheaud 		/*
788d6a90bb8SPhilippe Bergheaud 		* The device that owns atomics and wants to release
789d6a90bb8SPhilippe Bergheaud 		* them must pass the same address with enable == 0.
790d6a90bb8SPhilippe Bergheaud 		*/
791d6a90bb8SPhilippe Bergheaud 		if (tunnel_bar != addr) {
792d6a90bb8SPhilippe Bergheaud 			rc = -EPERM;
793d6a90bb8SPhilippe Bergheaud 			goto out;
794d6a90bb8SPhilippe Bergheaud 		}
795d6a90bb8SPhilippe Bergheaud 		addr = 0x0ULL;
796d6a90bb8SPhilippe Bergheaud 	}
797d6a90bb8SPhilippe Bergheaud 	rc = opal_pci_set_pbcq_tunnel_bar(phb->opal_id, addr);
798d6a90bb8SPhilippe Bergheaud 	rc = opal_error_code(rc);
799d6a90bb8SPhilippe Bergheaud out:
800d6a90bb8SPhilippe Bergheaud 	mutex_unlock(&tunnel_mutex);
801d6a90bb8SPhilippe Bergheaud 	return rc;
802d6a90bb8SPhilippe Bergheaud }
803d6a90bb8SPhilippe Bergheaud EXPORT_SYMBOL_GPL(pnv_pci_set_tunnel_bar);
804d6a90bb8SPhilippe Bergheaud 
pnv_pci_shutdown(void)80573ed148aSBenjamin Herrenschmidt void pnv_pci_shutdown(void)
80673ed148aSBenjamin Herrenschmidt {
80773ed148aSBenjamin Herrenschmidt 	struct pci_controller *hose;
80873ed148aSBenjamin Herrenschmidt 
8097a8e6bbfSMichael Neuling 	list_for_each_entry(hose, &hose_list, list_node)
8107a8e6bbfSMichael Neuling 		if (hose->controller_ops.shutdown)
8117a8e6bbfSMichael Neuling 			hose->controller_ops.shutdown(hose);
81273ed148aSBenjamin Herrenschmidt }
81373ed148aSBenjamin Herrenschmidt 
814aa0c033fSGavin Shan /* Fixup wrong class code in p7ioc and p8 root complex */
pnv_p7ioc_rc_quirk(struct pci_dev * dev)815cad5cef6SGreg Kroah-Hartman static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
816ca45cfe3SBenjamin Herrenschmidt {
817*904b10fbSPali Rohár 	dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
818ca45cfe3SBenjamin Herrenschmidt }
819ca45cfe3SBenjamin Herrenschmidt DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
820ca45cfe3SBenjamin Herrenschmidt 
pnv_pci_init(void)82161305a96SBenjamin Herrenschmidt void __init pnv_pci_init(void)
82261305a96SBenjamin Herrenschmidt {
82361305a96SBenjamin Herrenschmidt 	struct device_node *np;
82461305a96SBenjamin Herrenschmidt 
825673c9756SBjorn Helgaas 	pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
82661305a96SBenjamin Herrenschmidt 
827646b54f2SMichael Ellerman 	/* If we don't have OPAL, eg. in sim, just skip PCI probe */
828646b54f2SMichael Ellerman 	if (!firmware_has_feature(FW_FEATURE_OPAL))
829646b54f2SMichael Ellerman 		return;
830184cd4a3SBenjamin Herrenschmidt 
8319d72dcefSOliver O'Halloran #ifdef CONFIG_PCIEPORTBUS
8329d72dcefSOliver O'Halloran 	/*
8339d72dcefSOliver O'Halloran 	 * On PowerNV PCIe devices are (currently) managed in cooperation
8349d72dcefSOliver O'Halloran 	 * with firmware. This isn't *strictly* required, but there's enough
8359d72dcefSOliver O'Halloran 	 * assumptions baked into both firmware and the platform code that
8369d72dcefSOliver O'Halloran 	 * it's unwise to allow the portbus services to be used.
8379d72dcefSOliver O'Halloran 	 *
8389d72dcefSOliver O'Halloran 	 * We need to fix this eventually, but for now set this flag to disable
8399d72dcefSOliver O'Halloran 	 * the portbus driver. The AER service isn't required since that AER
8409d72dcefSOliver O'Halloran 	 * events are handled via EEH. The pciehp hotplug driver can't work
8419d72dcefSOliver O'Halloran 	 * without kernel changes (and portbus binding breaks pnv_php). The
8429d72dcefSOliver O'Halloran 	 * other services also require some thinking about how we're going
8439d72dcefSOliver O'Halloran 	 * to integrate them.
8449d72dcefSOliver O'Halloran 	 */
8459d72dcefSOliver O'Halloran 	pcie_ports_disabled = true;
8469d72dcefSOliver O'Halloran #endif
8479d72dcefSOliver O'Halloran 
848aa0c033fSGavin Shan 	/* Look for ioda2 built-in PHB3's */
849aa0c033fSGavin Shan 	for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
850aa0c033fSGavin Shan 		pnv_pci_init_ioda2_phb(np);
85161305a96SBenjamin Herrenschmidt 
852fb111334SBenjamin Herrenschmidt 	/* Look for ioda3 built-in PHB4's, we treat them as IODA2 */
853fb111334SBenjamin Herrenschmidt 	for_each_compatible_node(np, NULL, "ibm,ioda3-phb")
854fb111334SBenjamin Herrenschmidt 		pnv_pci_init_ioda2_phb(np);
855fb111334SBenjamin Herrenschmidt 
8567f2c39e9SFrederic Barrat 	/* Look for NPU2 OpenCAPI PHBs */
8577f2c39e9SFrederic Barrat 	for_each_compatible_node(np, NULL, "ibm,ioda2-npu2-opencapi-phb")
8587f2c39e9SFrederic Barrat 		pnv_pci_init_npu2_opencapi_phb(np);
8597f2c39e9SFrederic Barrat 
86061305a96SBenjamin Herrenschmidt 	/* Configure IOMMU DMA hooks */
86161305a96SBenjamin Herrenschmidt 	set_pci_dma_ops(&dma_iommu_ops);
86261305a96SBenjamin Herrenschmidt }
863