blob: b555ebc57ef5a33010ea4becbf992d3deb2dcbad [file] [log] [blame]
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +00001/*
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 Herrenschmidtc1a25622011-09-19 17:45:06 +000022#include <linux/msi.h>
Alexey Kardashevskiy4e13c1a2013-05-21 13:33:09 +100023#include <linux/iommu.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000024
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 Shanfb1b55d2013-03-05 21:12:37 +000030#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000031#include <asm/ppc-pci.h>
32#include <asm/opal.h>
33#include <asm/iommu.h>
34#include <asm/tce.h>
Stephen Rothwellf5339272012-03-15 18:18:00 +000035#include <asm/firmware.h>
Gavin Shanbe7e7442013-06-20 13:21:15 +080036#include <asm/eeh_event.h>
37#include <asm/eeh.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000038
39#include "powernv.h"
40#include "pci.h"
41
Benjamin Herrenschmidt82ba1292011-09-19 17:45:07 +000042/* Delay in usec */
43#define PCI_RESET_DELAY_US 3000000
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000044
45#define cfg_dbg(fmt...) do { } while(0)
46//#define cfg_dbg(fmt...) printk(fmt)
47
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000048#ifdef CONFIG_PCI_MSI
49static 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 Herrenschmidtb72c1f62013-05-21 22:58:21 +000053 struct pci_dn *pdn = pci_get_pdn(pdev);
54
55 if (pdn && pdn->force_32bit_msi && !phb->msi32_support)
56 return -ENODEV;
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000057
Gavin Shanfb1b55d2013-03-05 21:12:37 +000058 return (phb && phb->msi_bmp.bitmap) ? 0 : -ENODEV;
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000059}
60
61static 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 Shanfb1b55d2013-03-05 21:12:37 +000067 int hwirq;
68 unsigned int virq;
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000069 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 Shanfb1b55d2013-03-05 21:12:37 +000080 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
81 if (hwirq < 0) {
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000082 pr_warn("%s: Failed to find a free MSI\n",
83 pci_name(pdev));
84 return -ENOSPC;
85 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +000086 virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000087 if (virq == NO_IRQ) {
88 pr_warn("%s: Failed to map MSI to linux irq\n",
89 pci_name(pdev));
Gavin Shanfb1b55d2013-03-05 21:12:37 +000090 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000091 return -ENOMEM;
92 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +000093 rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
Gavin Shan137436c2013-04-25 19:20:59 +000094 virq, entry->msi_attrib.is_64, &msg);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000095 if (rc) {
96 pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
97 irq_dispose_mapping(virq);
Gavin Shanfb1b55d2013-03-05 21:12:37 +000098 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000099 return rc;
100 }
101 irq_set_msi_desc(virq, entry);
102 write_msi_msg(virq, &msg);
103 }
104 return 0;
105}
106
107static 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 Shanfb1b55d2013-03-05 21:12:37 +0000120 msi_bitmap_free_hwirqs(&phb->msi_bmp,
121 virq_to_hw(entry->irq) - phb->msi_base, 1);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000122 irq_dispose_mapping(entry->irq);
123 }
124}
125#endif /* CONFIG_PCI_MSI */
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000126
Gavin Shan93aef2a2013-11-22 16:28:45 +0800127static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose,
128 struct OpalIoPhbErrorCommon *common)
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000129{
Gavin Shan93aef2a2013-11-22 16:28:45 +0800130 struct OpalIoP7IOCPhbErrorData *data;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000131 int i;
132
Gavin Shan93aef2a2013-11-22 16:28:45 +0800133 data = (struct OpalIoP7IOCPhbErrorData *)common;
134 pr_info("P7IOC PHB#%d Diag-data (Version: %d)\n\n",
135 hose->global_number, common->version);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000136
Gavin Shan93aef2a2013-11-22 16:28:45 +0800137 pr_info(" brdgCtl: %08x\n", data->brdgCtl);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000138
Gavin Shan93aef2a2013-11-22 16:28:45 +0800139 pr_info(" portStatusReg: %08x\n", data->portStatusReg);
140 pr_info(" rootCmplxStatus: %08x\n", data->rootCmplxStatus);
141 pr_info(" busAgentStatus: %08x\n", data->busAgentStatus);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000142
Gavin Shan93aef2a2013-11-22 16:28:45 +0800143 pr_info(" deviceStatus: %08x\n", data->deviceStatus);
144 pr_info(" slotStatus: %08x\n", data->slotStatus);
145 pr_info(" linkStatus: %08x\n", data->linkStatus);
146 pr_info(" devCmdStatus: %08x\n", data->devCmdStatus);
147 pr_info(" devSecStatus: %08x\n", data->devSecStatus);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000148
Gavin Shan93aef2a2013-11-22 16:28:45 +0800149 pr_info(" rootErrorStatus: %08x\n", data->rootErrorStatus);
150 pr_info(" uncorrErrorStatus: %08x\n", data->uncorrErrorStatus);
151 pr_info(" corrErrorStatus: %08x\n", data->corrErrorStatus);
152 pr_info(" tlpHdr1: %08x\n", data->tlpHdr1);
153 pr_info(" tlpHdr2: %08x\n", data->tlpHdr2);
154 pr_info(" tlpHdr3: %08x\n", data->tlpHdr3);
155 pr_info(" tlpHdr4: %08x\n", data->tlpHdr4);
156 pr_info(" sourceId: %08x\n", data->sourceId);
157 pr_info(" errorClass: %016llx\n", data->errorClass);
158 pr_info(" correlator: %016llx\n", data->correlator);
159 pr_info(" p7iocPlssr: %016llx\n", data->p7iocPlssr);
160 pr_info(" p7iocCsr: %016llx\n", data->p7iocCsr);
161 pr_info(" lemFir: %016llx\n", data->lemFir);
162 pr_info(" lemErrorMask: %016llx\n", data->lemErrorMask);
163 pr_info(" lemWOF: %016llx\n", data->lemWOF);
164 pr_info(" phbErrorStatus: %016llx\n", data->phbErrorStatus);
165 pr_info(" phbFirstErrorStatus: %016llx\n", data->phbFirstErrorStatus);
166 pr_info(" phbErrorLog0: %016llx\n", data->phbErrorLog0);
167 pr_info(" phbErrorLog1: %016llx\n", data->phbErrorLog1);
168 pr_info(" mmioErrorStatus: %016llx\n", data->mmioErrorStatus);
169 pr_info(" mmioFirstErrorStatus: %016llx\n", data->mmioFirstErrorStatus);
170 pr_info(" mmioErrorLog0: %016llx\n", data->mmioErrorLog0);
171 pr_info(" mmioErrorLog1: %016llx\n", data->mmioErrorLog1);
172 pr_info(" dma0ErrorStatus: %016llx\n", data->dma0ErrorStatus);
173 pr_info(" dma0FirstErrorStatus: %016llx\n", data->dma0FirstErrorStatus);
174 pr_info(" dma0ErrorLog0: %016llx\n", data->dma0ErrorLog0);
175 pr_info(" dma0ErrorLog1: %016llx\n", data->dma0ErrorLog1);
176 pr_info(" dma1ErrorStatus: %016llx\n", data->dma1ErrorStatus);
177 pr_info(" dma1FirstErrorStatus: %016llx\n", data->dma1FirstErrorStatus);
178 pr_info(" dma1ErrorLog0: %016llx\n", data->dma1ErrorLog0);
179 pr_info(" dma1ErrorLog1: %016llx\n", data->dma1ErrorLog1);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000180
181 for (i = 0; i < OPAL_P7IOC_NUM_PEST_REGS; i++) {
182 if ((data->pestA[i] >> 63) == 0 &&
183 (data->pestB[i] >> 63) == 0)
184 continue;
Gavin Shan93aef2a2013-11-22 16:28:45 +0800185
186 pr_info(" PE[%3d] PESTA: %016llx\n", i, data->pestA[i]);
187 pr_info(" PESTB: %016llx\n", data->pestB[i]);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000188 }
189}
190
Gavin Shan93aef2a2013-11-22 16:28:45 +0800191static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
192 struct OpalIoPhbErrorCommon *common)
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000193{
Gavin Shan93aef2a2013-11-22 16:28:45 +0800194 struct OpalIoPhb3ErrorData *data;
195 int i;
196
197 data = (struct OpalIoPhb3ErrorData*)common;
198 pr_info("PHB3 PHB#%d Diag-data (Version: %d)\n\n",
199 hose->global_number, common->version);
200
201 pr_info(" brdgCtl: %08x\n", data->brdgCtl);
202
203 pr_info(" portStatusReg: %08x\n", data->portStatusReg);
204 pr_info(" rootCmplxStatus: %08x\n", data->rootCmplxStatus);
205 pr_info(" busAgentStatus: %08x\n", data->busAgentStatus);
206
207 pr_info(" deviceStatus: %08x\n", data->deviceStatus);
208 pr_info(" slotStatus: %08x\n", data->slotStatus);
209 pr_info(" linkStatus: %08x\n", data->linkStatus);
210 pr_info(" devCmdStatus: %08x\n", data->devCmdStatus);
211 pr_info(" devSecStatus: %08x\n", data->devSecStatus);
212
213 pr_info(" rootErrorStatus: %08x\n", data->rootErrorStatus);
214 pr_info(" uncorrErrorStatus: %08x\n", data->uncorrErrorStatus);
215 pr_info(" corrErrorStatus: %08x\n", data->corrErrorStatus);
216 pr_info(" tlpHdr1: %08x\n", data->tlpHdr1);
217 pr_info(" tlpHdr2: %08x\n", data->tlpHdr2);
218 pr_info(" tlpHdr3: %08x\n", data->tlpHdr3);
219 pr_info(" tlpHdr4: %08x\n", data->tlpHdr4);
220 pr_info(" sourceId: %08x\n", data->sourceId);
221 pr_info(" errorClass: %016llx\n", data->errorClass);
222 pr_info(" correlator: %016llx\n", data->correlator);
223
224 pr_info(" nFir: %016llx\n", data->nFir);
225 pr_info(" nFirMask: %016llx\n", data->nFirMask);
226 pr_info(" nFirWOF: %016llx\n", data->nFirWOF);
227 pr_info(" PhbPlssr: %016llx\n", data->phbPlssr);
228 pr_info(" PhbCsr: %016llx\n", data->phbCsr);
229 pr_info(" lemFir: %016llx\n", data->lemFir);
230 pr_info(" lemErrorMask: %016llx\n", data->lemErrorMask);
231 pr_info(" lemWOF: %016llx\n", data->lemWOF);
232 pr_info(" phbErrorStatus: %016llx\n", data->phbErrorStatus);
233 pr_info(" phbFirstErrorStatus: %016llx\n", data->phbFirstErrorStatus);
234 pr_info(" phbErrorLog0: %016llx\n", data->phbErrorLog0);
235 pr_info(" phbErrorLog1: %016llx\n", data->phbErrorLog1);
236 pr_info(" mmioErrorStatus: %016llx\n", data->mmioErrorStatus);
237 pr_info(" mmioFirstErrorStatus: %016llx\n", data->mmioFirstErrorStatus);
238 pr_info(" mmioErrorLog0: %016llx\n", data->mmioErrorLog0);
239 pr_info(" mmioErrorLog1: %016llx\n", data->mmioErrorLog1);
240 pr_info(" dma0ErrorStatus: %016llx\n", data->dma0ErrorStatus);
241 pr_info(" dma0FirstErrorStatus: %016llx\n", data->dma0FirstErrorStatus);
242 pr_info(" dma0ErrorLog0: %016llx\n", data->dma0ErrorLog0);
243 pr_info(" dma0ErrorLog1: %016llx\n", data->dma0ErrorLog1);
244 pr_info(" dma1ErrorStatus: %016llx\n", data->dma1ErrorStatus);
245 pr_info(" dma1FirstErrorStatus: %016llx\n", data->dma1FirstErrorStatus);
246 pr_info(" dma1ErrorLog0: %016llx\n", data->dma1ErrorLog0);
247 pr_info(" dma1ErrorLog1: %016llx\n", data->dma1ErrorLog1);
248
249 for (i = 0; i < OPAL_PHB3_NUM_PEST_REGS; i++) {
250 if ((data->pestA[i] >> 63) == 0 &&
251 (data->pestB[i] >> 63) == 0)
252 continue;
253
254 pr_info(" PE[%3d] PESTA: %016llx\n", i, data->pestA[i]);
255 pr_info(" PESTB: %016llx\n", data->pestB[i]);
256 }
257}
258
259void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
260 unsigned char *log_buff)
261{
262 struct OpalIoPhbErrorCommon *common;
263
264 if (!hose || !log_buff)
265 return;
266
267 common = (struct OpalIoPhbErrorCommon *)log_buff;
268 switch (common->ioType) {
269 case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
270 pnv_pci_dump_p7ioc_diag_data(hose, common);
271 break;
272 case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
273 pnv_pci_dump_phb3_diag_data(hose, common);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000274 break;
275 default:
Gavin Shan93aef2a2013-11-22 16:28:45 +0800276 pr_warn("%s: Unrecognized ioType %d\n",
277 __func__, common->ioType);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000278 }
279}
280
281static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
282{
283 unsigned long flags, rc;
284 int has_diag;
285
286 spin_lock_irqsave(&phb->lock, flags);
287
Gavin Shan23773232013-06-20 13:21:05 +0800288 rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob,
289 PNV_PCI_DIAG_BUF_SIZE);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000290 has_diag = (rc == OPAL_SUCCESS);
291
292 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
293 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
294 if (rc) {
295 pr_warning("PCI %d: Failed to clear EEH freeze state"
296 " for PE#%d, err %ld\n",
297 phb->hose->global_number, pe_no, rc);
298
299 /* For now, let's only display the diag buffer when we fail to clear
300 * the EEH status. We'll do more sensible things later when we have
301 * proper EEH support. We need to make sure we don't pollute ourselves
302 * with the normal errors generated when probing empty slots
303 */
304 if (has_diag)
Gavin Shan93aef2a2013-11-22 16:28:45 +0800305 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000306 else
307 pr_warning("PCI %d: No diag data available\n",
308 phb->hose->global_number);
309 }
310
311 spin_unlock_irqrestore(&phb->lock, flags);
312}
313
Gavin Shan9bf41be2013-06-27 13:46:48 +0800314static void pnv_pci_config_check_eeh(struct pnv_phb *phb,
315 struct device_node *dn)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000316{
317 s64 rc;
318 u8 fstate;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000319 __be16 pcierr;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000320 u32 pe_no;
321
Gavin Shan9bf41be2013-06-27 13:46:48 +0800322 /*
323 * Get the PE#. During the PCI probe stage, we might not
324 * setup that yet. So all ER errors should be mapped to
Gavin Shan36954dc2013-11-04 16:32:47 +0800325 * reserved PE.
Gavin Shan9bf41be2013-06-27 13:46:48 +0800326 */
327 pe_no = PCI_DN(dn)->pe_number;
Gavin Shan36954dc2013-11-04 16:32:47 +0800328 if (pe_no == IODA_INVALID_PE) {
329 if (phb->type == PNV_PHB_P5IOC2)
330 pe_no = 0;
331 else
332 pe_no = phb->ioda.reserved_pe;
333 }
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000334
335 /* Read freeze status */
336 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, &fstate, &pcierr,
337 NULL);
338 if (rc) {
Gavin Shan9bf41be2013-06-27 13:46:48 +0800339 pr_warning("%s: Can't read EEH status (PE#%d) for "
340 "%s, err %lld\n",
341 __func__, pe_no, dn->full_name, rc);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000342 return;
343 }
Gavin Shan9bf41be2013-06-27 13:46:48 +0800344 cfg_dbg(" -> EEH check, bdfn=%04x PE#%d fstate=%x\n",
345 (PCI_DN(dn)->busno << 8) | (PCI_DN(dn)->devfn),
346 pe_no, fstate);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000347 if (fstate != 0)
348 pnv_pci_handle_eeh_config(phb, pe_no);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000349}
350
Gavin Shan9bf41be2013-06-27 13:46:48 +0800351int pnv_pci_cfg_read(struct device_node *dn,
352 int where, int size, u32 *val)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000353{
Gavin Shan9bf41be2013-06-27 13:46:48 +0800354 struct pci_dn *pdn = PCI_DN(dn);
355 struct pnv_phb *phb = pdn->phb->private_data;
356 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
Gavin Shanbe7e7442013-06-20 13:21:15 +0800357#ifdef CONFIG_EEH
Gavin Shanbe7e7442013-06-20 13:21:15 +0800358 struct eeh_pe *phb_pe = NULL;
359#endif
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000360 s64 rc;
361
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000362 switch (size) {
363 case 1: {
364 u8 v8;
365 rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
366 *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
367 break;
368 }
369 case 2: {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000370 __be16 v16;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000371 rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
372 &v16);
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000373 *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000374 break;
375 }
376 case 4: {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000377 __be32 v32;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000378 rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000379 *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000380 break;
381 }
382 default:
383 return PCIBIOS_FUNC_NOT_SUPPORTED;
384 }
Gavin Shan9bf41be2013-06-27 13:46:48 +0800385 cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
386 __func__, pdn->busno, pdn->devfn, where, size, *val);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000387
Gavin Shanbe7e7442013-06-20 13:21:15 +0800388 /*
389 * Check if the specified PE has been put into frozen
390 * state. On the other hand, we needn't do that while
391 * the PHB has been put into frozen state because of
392 * PHB-fatal errors.
393 */
394#ifdef CONFIG_EEH
Gavin Shan9bf41be2013-06-27 13:46:48 +0800395 phb_pe = eeh_phb_pe_get(pdn->phb);
Gavin Shanbe7e7442013-06-20 13:21:15 +0800396 if (phb_pe && (phb_pe->state & EEH_PE_ISOLATED))
397 return PCIBIOS_SUCCESSFUL;
398
Gavin Shan0b9e2672013-06-27 13:46:44 +0800399 if (phb->eeh_state & PNV_EEH_STATE_ENABLED) {
Gavin Shan9bf41be2013-06-27 13:46:48 +0800400 if (*val == EEH_IO_ERROR_VALUE(size) &&
401 eeh_dev_check_failure(of_node_to_eeh_dev(dn)))
402 return PCIBIOS_DEVICE_NOT_FOUND;
Gavin Shanbe7e7442013-06-20 13:21:15 +0800403 } else {
Gavin Shan9bf41be2013-06-27 13:46:48 +0800404 pnv_pci_config_check_eeh(phb, dn);
Gavin Shanbe7e7442013-06-20 13:21:15 +0800405 }
406#else
Gavin Shan9bf41be2013-06-27 13:46:48 +0800407 pnv_pci_config_check_eeh(phb, dn);
Gavin Shanbe7e7442013-06-20 13:21:15 +0800408#endif
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000409
410 return PCIBIOS_SUCCESSFUL;
411}
412
Gavin Shan9bf41be2013-06-27 13:46:48 +0800413int pnv_pci_cfg_write(struct device_node *dn,
414 int where, int size, u32 val)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000415{
Gavin Shan9bf41be2013-06-27 13:46:48 +0800416 struct pci_dn *pdn = PCI_DN(dn);
417 struct pnv_phb *phb = pdn->phb->private_data;
418 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000419
Gavin Shan9bf41be2013-06-27 13:46:48 +0800420 cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
421 pdn->busno, pdn->devfn, where, size, val);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000422 switch (size) {
423 case 1:
424 opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
425 break;
426 case 2:
427 opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
428 break;
429 case 4:
430 opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
431 break;
432 default:
433 return PCIBIOS_FUNC_NOT_SUPPORTED;
434 }
Gavin Shanbe7e7442013-06-20 13:21:15 +0800435
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000436 /* Check if the PHB got frozen due to an error (no response) */
Gavin Shanbe7e7442013-06-20 13:21:15 +0800437#ifdef CONFIG_EEH
Gavin Shan0b9e2672013-06-27 13:46:44 +0800438 if (!(phb->eeh_state & PNV_EEH_STATE_ENABLED))
Gavin Shan9bf41be2013-06-27 13:46:48 +0800439 pnv_pci_config_check_eeh(phb, dn);
Gavin Shanbe7e7442013-06-20 13:21:15 +0800440#else
Gavin Shan9bf41be2013-06-27 13:46:48 +0800441 pnv_pci_config_check_eeh(phb, dn);
Gavin Shanbe7e7442013-06-20 13:21:15 +0800442#endif
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000443
444 return PCIBIOS_SUCCESSFUL;
445}
446
Gavin Shan9bf41be2013-06-27 13:46:48 +0800447static int pnv_pci_read_config(struct pci_bus *bus,
448 unsigned int devfn,
449 int where, int size, u32 *val)
450{
451 struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
452 struct pci_dn *pdn;
453
454 for (dn = busdn->child; dn; dn = dn->sibling) {
455 pdn = PCI_DN(dn);
456 if (pdn && pdn->devfn == devfn)
457 return pnv_pci_cfg_read(dn, where, size, val);
458 }
459
460 *val = 0xFFFFFFFF;
461 return PCIBIOS_DEVICE_NOT_FOUND;
462
463}
464
465static int pnv_pci_write_config(struct pci_bus *bus,
466 unsigned int devfn,
467 int where, int size, u32 val)
468{
469 struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
470 struct pci_dn *pdn;
471
472 for (dn = busdn->child; dn; dn = dn->sibling) {
473 pdn = PCI_DN(dn);
474 if (pdn && pdn->devfn == devfn)
475 return pnv_pci_cfg_write(dn, where, size, val);
476 }
477
478 return PCIBIOS_DEVICE_NOT_FOUND;
479}
480
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000481struct pci_ops pnv_pci_ops = {
Gavin Shan9bf41be2013-06-27 13:46:48 +0800482 .read = pnv_pci_read_config,
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000483 .write = pnv_pci_write_config,
484};
485
486static int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
487 unsigned long uaddr, enum dma_data_direction direction,
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000488 struct dma_attrs *attrs, bool rm)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000489{
490 u64 proto_tce;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000491 __be64 *tcep, *tces;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000492 u64 rpn;
493
494 proto_tce = TCE_PCI_READ; // Read allowed
495
496 if (direction != DMA_TO_DEVICE)
497 proto_tce |= TCE_PCI_WRITE;
498
Anton Blanchard5e4da532013-09-23 12:05:06 +1000499 tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000500 rpn = __pa(uaddr) >> TCE_SHIFT;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000501
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000502 while (npages--)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000503 *(tcep++) = cpu_to_be64(proto_tce | (rpn++ << TCE_RPN_SHIFT));
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000504
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000505 /* Some implementations won't cache invalid TCEs and thus may not
506 * need that flush. We'll probably turn it_type into a bit mask
507 * of flags if that becomes the case
508 */
509 if (tbl->it_type & TCE_PCI_SWINV_CREATE)
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000510 pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000511
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000512 return 0;
513}
514
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000515static int pnv_tce_build_vm(struct iommu_table *tbl, long index, long npages,
516 unsigned long uaddr,
517 enum dma_data_direction direction,
518 struct dma_attrs *attrs)
519{
520 return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs,
521 false);
522}
523
524static void pnv_tce_free(struct iommu_table *tbl, long index, long npages,
525 bool rm)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000526{
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000527 __be64 *tcep, *tces;
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000528
Anton Blanchard5e4da532013-09-23 12:05:06 +1000529 tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000530
531 while (npages--)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000532 *(tcep++) = cpu_to_be64(0);
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000533
Benjamin Herrenschmidt605e44d2013-05-20 17:25:15 +0000534 if (tbl->it_type & TCE_PCI_SWINV_FREE)
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000535 pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
536}
537
538static void pnv_tce_free_vm(struct iommu_table *tbl, long index, long npages)
539{
540 pnv_tce_free(tbl, index, npages, false);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000541}
542
Alexey Kardashevskiy11f63d32012-09-04 15:19:35 +0000543static unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
544{
545 return ((u64 *)tbl->it_base)[index - tbl->it_offset];
546}
547
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000548static int pnv_tce_build_rm(struct iommu_table *tbl, long index, long npages,
549 unsigned long uaddr,
550 enum dma_data_direction direction,
551 struct dma_attrs *attrs)
552{
553 return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs, true);
554}
555
556static void pnv_tce_free_rm(struct iommu_table *tbl, long index, long npages)
557{
558 pnv_tce_free(tbl, index, npages, true);
559}
560
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000561void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
562 void *tce_mem, u64 tce_size,
563 u64 dma_offset)
564{
565 tbl->it_blocksize = 16;
566 tbl->it_base = (unsigned long)tce_mem;
Alistair Popple3a553172013-12-09 18:17:02 +1100567 tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
568 tbl->it_offset = dma_offset >> tbl->it_page_shift;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000569 tbl->it_index = 0;
570 tbl->it_size = tce_size >> 3;
571 tbl->it_busno = 0;
572 tbl->it_type = TCE_PCI;
573}
574
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800575static struct iommu_table *pnv_pci_setup_bml_iommu(struct pci_controller *hose)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000576{
577 struct iommu_table *tbl;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000578 const __be64 *basep, *swinvp;
579 const __be32 *sizep;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000580
581 basep = of_get_property(hose->dn, "linux,tce-base", NULL);
582 sizep = of_get_property(hose->dn, "linux,tce-size", NULL);
583 if (basep == NULL || sizep == NULL) {
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000584 pr_err("PCI: %s has missing tce entries !\n",
585 hose->dn->full_name);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000586 return NULL;
587 }
588 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node);
589 if (WARN_ON(!tbl))
590 return NULL;
591 pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)),
592 be32_to_cpup(sizep), 0);
593 iommu_init_table(tbl, hose->node);
Alexey Kardashevskiy4e13c1a2013-05-21 13:33:09 +1000594 iommu_register_group(tbl, pci_domain_nr(hose->bus), 0);
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000595
596 /* Deal with SW invalidated TCEs when needed (BML way) */
597 swinvp = of_get_property(hose->dn, "linux,tce-sw-invalidate-info",
598 NULL);
599 if (swinvp) {
Anton Blanchard5e4da532013-09-23 12:05:06 +1000600 tbl->it_busno = be64_to_cpu(swinvp[1]);
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000601 tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000602 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
603 }
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000604 return tbl;
605}
606
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800607static void pnv_pci_dma_fallback_setup(struct pci_controller *hose,
608 struct pci_dev *pdev)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000609{
610 struct device_node *np = pci_bus_to_OF_node(hose->bus);
611 struct pci_dn *pdn;
612
613 if (np == NULL)
614 return;
615 pdn = PCI_DN(np);
616 if (!pdn->iommu_table)
617 pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
618 if (!pdn->iommu_table)
619 return;
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +1100620 set_iommu_table_base_and_group(&pdev->dev, pdn->iommu_table);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000621}
622
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800623static void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000624{
625 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
626 struct pnv_phb *phb = hose->private_data;
627
628 /* If we have no phb structure, try to setup a fallback based on
629 * the device-tree (RTAS PCI for example)
630 */
631 if (phb && phb->dma_dev_setup)
632 phb->dma_dev_setup(phb, pdev);
633 else
634 pnv_pci_dma_fallback_setup(hose, pdev);
635}
636
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000637void pnv_pci_shutdown(void)
638{
639 struct pci_controller *hose;
640
641 list_for_each_entry(hose, &hose_list, list_node) {
642 struct pnv_phb *phb = hose->private_data;
643
644 if (phb && phb->shutdown)
645 phb->shutdown(phb);
646 }
647}
648
Gavin Shanaa0c0332013-04-25 19:20:57 +0000649/* Fixup wrong class code in p7ioc and p8 root complex */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800650static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
Benjamin Herrenschmidtca45cfe2011-11-06 18:56:00 +0000651{
652 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
653}
654DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
655
Benjamin Herrenschmidt82ba1292011-09-19 17:45:07 +0000656static int pnv_pci_probe_mode(struct pci_bus *bus)
657{
658 struct pci_controller *hose = pci_bus_to_host(bus);
659 const __be64 *tstamp;
660 u64 now, target;
661
662
663 /* We hijack this as a way to ensure we have waited long
664 * enough since the reset was lifted on the PCI bus
665 */
666 if (bus != hose->bus)
667 return PCI_PROBE_NORMAL;
668 tstamp = of_get_property(hose->dn, "reset-clear-timestamp", NULL);
669 if (!tstamp || !*tstamp)
670 return PCI_PROBE_NORMAL;
671
672 now = mftb() / tb_ticks_per_usec;
673 target = (be64_to_cpup(tstamp) / tb_ticks_per_usec)
674 + PCI_RESET_DELAY_US;
675
676 pr_devel("pci %04d: Reset target: 0x%llx now: 0x%llx\n",
677 hose->global_number, target, now);
678
679 if (now < target)
680 msleep((target - now + 999) / 1000);
681
682 return PCI_PROBE_NORMAL;
683}
684
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000685void __init pnv_pci_init(void)
686{
687 struct device_node *np;
688
Bjorn Helgaas673c9752012-02-23 20:18:58 -0700689 pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000690
691 /* OPAL absent, try POPAL first then RTAS detection of PHBs */
692 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
693#ifdef CONFIG_PPC_POWERNV_RTAS
694 init_pci_config_tokens();
695 find_and_init_phbs();
696#endif /* CONFIG_PPC_POWERNV_RTAS */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000697 }
698 /* OPAL is here, do our normal stuff */
699 else {
700 int found_ioda = 0;
701
702 /* Look for IODA IO-Hubs. We don't support mixing IODA
703 * and p5ioc2 due to the need to change some global
704 * probing flags
705 */
706 for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
707 pnv_pci_init_ioda_hub(np);
708 found_ioda = 1;
709 }
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000710
711 /* Look for p5ioc2 IO-Hubs */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000712 if (!found_ioda)
713 for_each_compatible_node(np, NULL, "ibm,p5ioc2")
714 pnv_pci_init_p5ioc2_hub(np);
Gavin Shanaa0c0332013-04-25 19:20:57 +0000715
716 /* Look for ioda2 built-in PHB3's */
717 for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
718 pnv_pci_init_ioda2_phb(np);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000719 }
720
721 /* Setup the linkage between OF nodes and PHBs */
722 pci_devs_phb_init();
723
724 /* Configure IOMMU DMA hooks */
725 ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000726 ppc_md.tce_build = pnv_tce_build_vm;
727 ppc_md.tce_free = pnv_tce_free_vm;
728 ppc_md.tce_build_rm = pnv_tce_build_rm;
729 ppc_md.tce_free_rm = pnv_tce_free_rm;
Alexey Kardashevskiy11f63d32012-09-04 15:19:35 +0000730 ppc_md.tce_get = pnv_tce_get;
Benjamin Herrenschmidt82ba1292011-09-19 17:45:07 +0000731 ppc_md.pci_probe_mode = pnv_pci_probe_mode;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000732 set_pci_dma_ops(&dma_iommu_ops);
733
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000734 /* Configure MSIs */
735#ifdef CONFIG_PCI_MSI
736 ppc_md.msi_check_device = pnv_msi_check_device;
737 ppc_md.setup_msi_irqs = pnv_setup_msi_irqs;
738 ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs;
739#endif
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000740}
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +1100741
742static int tce_iommu_bus_notifier(struct notifier_block *nb,
743 unsigned long action, void *data)
744{
745 struct device *dev = data;
746
747 switch (action) {
748 case BUS_NOTIFY_ADD_DEVICE:
749 return iommu_add_device(dev);
750 case BUS_NOTIFY_DEL_DEVICE:
751 if (dev->iommu_group)
752 iommu_del_device(dev);
753 return 0;
754 default:
755 return 0;
756 }
757}
758
759static struct notifier_block tce_iommu_bus_nb = {
760 .notifier_call = tce_iommu_bus_notifier,
761};
762
763static int __init tce_iommu_bus_notifier_init(void)
764{
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +1100765 bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb);
766 return 0;
767}
768
769subsys_initcall_sync(tce_iommu_bus_notifier_init);