Merge tag 'for-4.1' of git://git.kernel.org/pub/scm/linux/kernel/git/kishon/linux...
[deliverable/linux.git] / arch / powerpc / platforms / powernv / pci.c
CommitLineData
61305a96
BH
1/*
2 * Support PCI/PCIe on PowerNV platforms
3 *
4 * Currently supports only P5IOC2
5 *
6 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/pci.h>
16#include <linux/delay.h>
17#include <linux/string.h>
18#include <linux/init.h>
61305a96
BH
19#include <linux/irq.h>
20#include <linux/io.h>
c1a2562a 21#include <linux/msi.h>
4e13c1ac 22#include <linux/iommu.h>
61305a96
BH
23
24#include <asm/sections.h>
25#include <asm/io.h>
26#include <asm/prom.h>
27#include <asm/pci-bridge.h>
28#include <asm/machdep.h>
fb1b55d6 29#include <asm/msi_bitmap.h>
61305a96
BH
30#include <asm/ppc-pci.h>
31#include <asm/opal.h>
32#include <asm/iommu.h>
33#include <asm/tce.h>
f5339277 34#include <asm/firmware.h>
be7e7446
GS
35#include <asm/eeh_event.h>
36#include <asm/eeh.h>
61305a96
BH
37
38#include "powernv.h"
39#include "pci.h"
40
82ba129b
BH
41/* Delay in usec */
42#define PCI_RESET_DELAY_US 3000000
61305a96
BH
43
44#define cfg_dbg(fmt...) do { } while(0)
45//#define cfg_dbg(fmt...) printk(fmt)
46
c1a2562a 47#ifdef CONFIG_PCI_MSI
c1a2562a
BH
48static int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
49{
50 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
51 struct pnv_phb *phb = hose->private_data;
52 struct msi_desc *entry;
53 struct msi_msg msg;
fb1b55d6
GS
54 int hwirq;
55 unsigned int virq;
c1a2562a
BH
56 int rc;
57
6b2fd7ef
AG
58 if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
59 return -ENODEV;
60
36074381 61 if (pdev->no_64bit_msi && !phb->msi32_support)
c1a2562a
BH
62 return -ENODEV;
63
64 list_for_each_entry(entry, &pdev->msi_list, list) {
65 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
66 pr_warn("%s: Supports only 64-bit MSIs\n",
67 pci_name(pdev));
68 return -ENXIO;
69 }
fb1b55d6
GS
70 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
71 if (hwirq < 0) {
c1a2562a
BH
72 pr_warn("%s: Failed to find a free MSI\n",
73 pci_name(pdev));
74 return -ENOSPC;
75 }
fb1b55d6 76 virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
c1a2562a
BH
77 if (virq == NO_IRQ) {
78 pr_warn("%s: Failed to map MSI to linux irq\n",
79 pci_name(pdev));
fb1b55d6 80 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
c1a2562a
BH
81 return -ENOMEM;
82 }
fb1b55d6 83 rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
137436c9 84 virq, entry->msi_attrib.is_64, &msg);
c1a2562a
BH
85 if (rc) {
86 pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
87 irq_dispose_mapping(virq);
fb1b55d6 88 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
c1a2562a
BH
89 return rc;
90 }
91 irq_set_msi_desc(virq, entry);
83a18912 92 pci_write_msi_msg(virq, &msg);
c1a2562a
BH
93 }
94 return 0;
95}
96
97static void pnv_teardown_msi_irqs(struct pci_dev *pdev)
98{
99 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
100 struct pnv_phb *phb = hose->private_data;
101 struct msi_desc *entry;
102
103 if (WARN_ON(!phb))
104 return;
105
106 list_for_each_entry(entry, &pdev->msi_list, list) {
107 if (entry->irq == NO_IRQ)
108 continue;
109 irq_set_msi_desc(entry->irq, NULL);
fb1b55d6
GS
110 msi_bitmap_free_hwirqs(&phb->msi_bmp,
111 virq_to_hw(entry->irq) - phb->msi_base, 1);
c1a2562a
BH
112 irq_dispose_mapping(entry->irq);
113 }
114}
115#endif /* CONFIG_PCI_MSI */
61305a96 116
93aef2a7
GS
117static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose,
118 struct OpalIoPhbErrorCommon *common)
cee72d5b 119{
93aef2a7 120 struct OpalIoP7IOCPhbErrorData *data;
cee72d5b
BH
121 int i;
122
93aef2a7 123 data = (struct OpalIoP7IOCPhbErrorData *)common;
b34497d1 124 pr_info("P7IOC PHB#%d Diag-data (Version: %d)\n",
f18440fb 125 hose->global_number, be32_to_cpu(common->version));
93aef2a7 126
af87d2fe 127 if (data->brdgCtl)
b34497d1 128 pr_info("brdgCtl: %08x\n",
f18440fb 129 be32_to_cpu(data->brdgCtl));
af87d2fe
GS
130 if (data->portStatusReg || data->rootCmplxStatus ||
131 data->busAgentStatus)
b34497d1 132 pr_info("UtlSts: %08x %08x %08x\n",
f18440fb
GS
133 be32_to_cpu(data->portStatusReg),
134 be32_to_cpu(data->rootCmplxStatus),
135 be32_to_cpu(data->busAgentStatus));
af87d2fe
GS
136 if (data->deviceStatus || data->slotStatus ||
137 data->linkStatus || data->devCmdStatus ||
138 data->devSecStatus)
b34497d1 139 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
f18440fb
GS
140 be32_to_cpu(data->deviceStatus),
141 be32_to_cpu(data->slotStatus),
142 be32_to_cpu(data->linkStatus),
143 be32_to_cpu(data->devCmdStatus),
144 be32_to_cpu(data->devSecStatus));
af87d2fe
GS
145 if (data->rootErrorStatus || data->uncorrErrorStatus ||
146 data->corrErrorStatus)
b34497d1 147 pr_info("RootErrSts: %08x %08x %08x\n",
f18440fb
GS
148 be32_to_cpu(data->rootErrorStatus),
149 be32_to_cpu(data->uncorrErrorStatus),
150 be32_to_cpu(data->corrErrorStatus));
af87d2fe
GS
151 if (data->tlpHdr1 || data->tlpHdr2 ||
152 data->tlpHdr3 || data->tlpHdr4)
b34497d1 153 pr_info("RootErrLog: %08x %08x %08x %08x\n",
f18440fb
GS
154 be32_to_cpu(data->tlpHdr1),
155 be32_to_cpu(data->tlpHdr2),
156 be32_to_cpu(data->tlpHdr3),
157 be32_to_cpu(data->tlpHdr4));
af87d2fe
GS
158 if (data->sourceId || data->errorClass ||
159 data->correlator)
b34497d1 160 pr_info("RootErrLog1: %08x %016llx %016llx\n",
f18440fb
GS
161 be32_to_cpu(data->sourceId),
162 be64_to_cpu(data->errorClass),
163 be64_to_cpu(data->correlator));
af87d2fe 164 if (data->p7iocPlssr || data->p7iocCsr)
b34497d1 165 pr_info("PhbSts: %016llx %016llx\n",
f18440fb
GS
166 be64_to_cpu(data->p7iocPlssr),
167 be64_to_cpu(data->p7iocCsr));
b34497d1
GS
168 if (data->lemFir)
169 pr_info("Lem: %016llx %016llx %016llx\n",
f18440fb
GS
170 be64_to_cpu(data->lemFir),
171 be64_to_cpu(data->lemErrorMask),
172 be64_to_cpu(data->lemWOF));
b34497d1
GS
173 if (data->phbErrorStatus)
174 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
f18440fb
GS
175 be64_to_cpu(data->phbErrorStatus),
176 be64_to_cpu(data->phbFirstErrorStatus),
177 be64_to_cpu(data->phbErrorLog0),
178 be64_to_cpu(data->phbErrorLog1));
b34497d1
GS
179 if (data->mmioErrorStatus)
180 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
f18440fb
GS
181 be64_to_cpu(data->mmioErrorStatus),
182 be64_to_cpu(data->mmioFirstErrorStatus),
183 be64_to_cpu(data->mmioErrorLog0),
184 be64_to_cpu(data->mmioErrorLog1));
b34497d1
GS
185 if (data->dma0ErrorStatus)
186 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
f18440fb
GS
187 be64_to_cpu(data->dma0ErrorStatus),
188 be64_to_cpu(data->dma0FirstErrorStatus),
189 be64_to_cpu(data->dma0ErrorLog0),
190 be64_to_cpu(data->dma0ErrorLog1));
b34497d1
GS
191 if (data->dma1ErrorStatus)
192 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
f18440fb
GS
193 be64_to_cpu(data->dma1ErrorStatus),
194 be64_to_cpu(data->dma1FirstErrorStatus),
195 be64_to_cpu(data->dma1ErrorLog0),
196 be64_to_cpu(data->dma1ErrorLog1));
cee72d5b
BH
197
198 for (i = 0; i < OPAL_P7IOC_NUM_PEST_REGS; i++) {
199 if ((data->pestA[i] >> 63) == 0 &&
200 (data->pestB[i] >> 63) == 0)
201 continue;
93aef2a7 202
b34497d1 203 pr_info("PE[%3d] A/B: %016llx %016llx\n",
f18440fb
GS
204 i, be64_to_cpu(data->pestA[i]),
205 be64_to_cpu(data->pestB[i]));
cee72d5b
BH
206 }
207}
208
93aef2a7
GS
209static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
210 struct OpalIoPhbErrorCommon *common)
cee72d5b 211{
93aef2a7
GS
212 struct OpalIoPhb3ErrorData *data;
213 int i;
214
215 data = (struct OpalIoPhb3ErrorData*)common;
b34497d1 216 pr_info("PHB3 PHB#%d Diag-data (Version: %d)\n",
ddf0322a 217 hose->global_number, be32_to_cpu(common->version));
af87d2fe 218 if (data->brdgCtl)
b34497d1 219 pr_info("brdgCtl: %08x\n",
ddf0322a 220 be32_to_cpu(data->brdgCtl));
af87d2fe
GS
221 if (data->portStatusReg || data->rootCmplxStatus ||
222 data->busAgentStatus)
b34497d1 223 pr_info("UtlSts: %08x %08x %08x\n",
ddf0322a
GC
224 be32_to_cpu(data->portStatusReg),
225 be32_to_cpu(data->rootCmplxStatus),
226 be32_to_cpu(data->busAgentStatus));
af87d2fe
GS
227 if (data->deviceStatus || data->slotStatus ||
228 data->linkStatus || data->devCmdStatus ||
229 data->devSecStatus)
b34497d1 230 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
ddf0322a
GC
231 be32_to_cpu(data->deviceStatus),
232 be32_to_cpu(data->slotStatus),
233 be32_to_cpu(data->linkStatus),
234 be32_to_cpu(data->devCmdStatus),
235 be32_to_cpu(data->devSecStatus));
af87d2fe
GS
236 if (data->rootErrorStatus || data->uncorrErrorStatus ||
237 data->corrErrorStatus)
b34497d1 238 pr_info("RootErrSts: %08x %08x %08x\n",
ddf0322a
GC
239 be32_to_cpu(data->rootErrorStatus),
240 be32_to_cpu(data->uncorrErrorStatus),
241 be32_to_cpu(data->corrErrorStatus));
af87d2fe
GS
242 if (data->tlpHdr1 || data->tlpHdr2 ||
243 data->tlpHdr3 || data->tlpHdr4)
b34497d1 244 pr_info("RootErrLog: %08x %08x %08x %08x\n",
ddf0322a
GC
245 be32_to_cpu(data->tlpHdr1),
246 be32_to_cpu(data->tlpHdr2),
247 be32_to_cpu(data->tlpHdr3),
248 be32_to_cpu(data->tlpHdr4));
af87d2fe
GS
249 if (data->sourceId || data->errorClass ||
250 data->correlator)
b34497d1 251 pr_info("RootErrLog1: %08x %016llx %016llx\n",
ddf0322a
GC
252 be32_to_cpu(data->sourceId),
253 be64_to_cpu(data->errorClass),
254 be64_to_cpu(data->correlator));
b34497d1
GS
255 if (data->nFir)
256 pr_info("nFir: %016llx %016llx %016llx\n",
ddf0322a
GC
257 be64_to_cpu(data->nFir),
258 be64_to_cpu(data->nFirMask),
259 be64_to_cpu(data->nFirWOF));
af87d2fe 260 if (data->phbPlssr || data->phbCsr)
b34497d1 261 pr_info("PhbSts: %016llx %016llx\n",
ddf0322a
GC
262 be64_to_cpu(data->phbPlssr),
263 be64_to_cpu(data->phbCsr));
b34497d1
GS
264 if (data->lemFir)
265 pr_info("Lem: %016llx %016llx %016llx\n",
ddf0322a
GC
266 be64_to_cpu(data->lemFir),
267 be64_to_cpu(data->lemErrorMask),
268 be64_to_cpu(data->lemWOF));
b34497d1
GS
269 if (data->phbErrorStatus)
270 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
ddf0322a
GC
271 be64_to_cpu(data->phbErrorStatus),
272 be64_to_cpu(data->phbFirstErrorStatus),
273 be64_to_cpu(data->phbErrorLog0),
274 be64_to_cpu(data->phbErrorLog1));
b34497d1
GS
275 if (data->mmioErrorStatus)
276 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
ddf0322a
GC
277 be64_to_cpu(data->mmioErrorStatus),
278 be64_to_cpu(data->mmioFirstErrorStatus),
279 be64_to_cpu(data->mmioErrorLog0),
280 be64_to_cpu(data->mmioErrorLog1));
b34497d1
GS
281 if (data->dma0ErrorStatus)
282 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
ddf0322a
GC
283 be64_to_cpu(data->dma0ErrorStatus),
284 be64_to_cpu(data->dma0FirstErrorStatus),
285 be64_to_cpu(data->dma0ErrorLog0),
286 be64_to_cpu(data->dma0ErrorLog1));
b34497d1
GS
287 if (data->dma1ErrorStatus)
288 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
ddf0322a
GC
289 be64_to_cpu(data->dma1ErrorStatus),
290 be64_to_cpu(data->dma1FirstErrorStatus),
291 be64_to_cpu(data->dma1ErrorLog0),
292 be64_to_cpu(data->dma1ErrorLog1));
93aef2a7
GS
293
294 for (i = 0; i < OPAL_PHB3_NUM_PEST_REGS; i++) {
ddf0322a
GC
295 if ((be64_to_cpu(data->pestA[i]) >> 63) == 0 &&
296 (be64_to_cpu(data->pestB[i]) >> 63) == 0)
93aef2a7
GS
297 continue;
298
b34497d1 299 pr_info("PE[%3d] A/B: %016llx %016llx\n",
ddf0322a
GC
300 i, be64_to_cpu(data->pestA[i]),
301 be64_to_cpu(data->pestB[i]));
93aef2a7
GS
302 }
303}
304
305void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
306 unsigned char *log_buff)
307{
308 struct OpalIoPhbErrorCommon *common;
309
310 if (!hose || !log_buff)
311 return;
312
313 common = (struct OpalIoPhbErrorCommon *)log_buff;
ddf0322a 314 switch (be32_to_cpu(common->ioType)) {
93aef2a7
GS
315 case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
316 pnv_pci_dump_p7ioc_diag_data(hose, common);
317 break;
318 case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
319 pnv_pci_dump_phb3_diag_data(hose, common);
cee72d5b
BH
320 break;
321 default:
93aef2a7 322 pr_warn("%s: Unrecognized ioType %d\n",
ddf0322a 323 __func__, be32_to_cpu(common->ioType));
cee72d5b
BH
324 }
325}
326
327static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
328{
329 unsigned long flags, rc;
98fd7002 330 int has_diag, ret = 0;
cee72d5b
BH
331
332 spin_lock_irqsave(&phb->lock, flags);
333
98fd7002 334 /* Fetch PHB diag-data */
23773230
GS
335 rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob,
336 PNV_PCI_DIAG_BUF_SIZE);
cee72d5b
BH
337 has_diag = (rc == OPAL_SUCCESS);
338
98fd7002
GS
339 /* If PHB supports compound PE, to handle it */
340 if (phb->unfreeze_pe) {
341 ret = phb->unfreeze_pe(phb,
342 pe_no,
cee72d5b 343 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
98fd7002
GS
344 } else {
345 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
346 pe_no,
347 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
348 if (rc) {
349 pr_warn("%s: Failure %ld clearing frozen "
350 "PHB#%x-PE#%x\n",
351 __func__, rc, phb->hose->global_number,
352 pe_no);
353 ret = -EIO;
354 }
cee72d5b
BH
355 }
356
98fd7002
GS
357 /*
358 * For now, let's only display the diag buffer when we fail to clear
359 * the EEH status. We'll do more sensible things later when we have
360 * proper EEH support. We need to make sure we don't pollute ourselves
361 * with the normal errors generated when probing empty slots
362 */
363 if (has_diag && ret)
364 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob);
365
cee72d5b
BH
366 spin_unlock_irqrestore(&phb->lock, flags);
367}
368
9bf41be6
GS
369static void pnv_pci_config_check_eeh(struct pnv_phb *phb,
370 struct device_node *dn)
61305a96 371{
61305a96 372 u8 fstate;
3a1a4661 373 __be16 pcierr;
98fd7002
GS
374 int pe_no;
375 s64 rc;
61305a96 376
9bf41be6
GS
377 /*
378 * Get the PE#. During the PCI probe stage, we might not
379 * setup that yet. So all ER errors should be mapped to
36954dc7 380 * reserved PE.
9bf41be6
GS
381 */
382 pe_no = PCI_DN(dn)->pe_number;
36954dc7
GS
383 if (pe_no == IODA_INVALID_PE) {
384 if (phb->type == PNV_PHB_P5IOC2)
385 pe_no = 0;
386 else
387 pe_no = phb->ioda.reserved_pe;
388 }
61305a96 389
98fd7002
GS
390 /*
391 * Fetch frozen state. If the PHB support compound PE,
392 * we need handle that case.
393 */
394 if (phb->get_pe_state) {
395 fstate = phb->get_pe_state(phb, pe_no);
396 } else {
397 rc = opal_pci_eeh_freeze_status(phb->opal_id,
398 pe_no,
399 &fstate,
400 &pcierr,
401 NULL);
402 if (rc) {
403 pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
404 __func__, rc, phb->hose->global_number, pe_no);
405 return;
406 }
61305a96 407 }
98fd7002 408
9bf41be6
GS
409 cfg_dbg(" -> EEH check, bdfn=%04x PE#%d fstate=%x\n",
410 (PCI_DN(dn)->busno << 8) | (PCI_DN(dn)->devfn),
411 pe_no, fstate);
98fd7002
GS
412
413 /* Clear the frozen state if applicable */
414 if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
415 fstate == OPAL_EEH_STOPPED_DMA_FREEZE ||
416 fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
417 /*
418 * If PHB supports compound PE, freeze it for
419 * consistency.
420 */
421 if (phb->freeze_pe)
422 phb->freeze_pe(phb, pe_no);
423
cee72d5b 424 pnv_pci_handle_eeh_config(phb, pe_no);
98fd7002 425 }
61305a96
BH
426}
427
9bf41be6
GS
428int pnv_pci_cfg_read(struct device_node *dn,
429 int where, int size, u32 *val)
61305a96 430{
9bf41be6
GS
431 struct pci_dn *pdn = PCI_DN(dn);
432 struct pnv_phb *phb = pdn->phb->private_data;
433 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
61305a96
BH
434 s64 rc;
435
61305a96
BH
436 switch (size) {
437 case 1: {
438 u8 v8;
439 rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
440 *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
441 break;
442 }
443 case 2: {
3a1a4661 444 __be16 v16;
61305a96
BH
445 rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
446 &v16);
3a1a4661 447 *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
61305a96
BH
448 break;
449 }
450 case 4: {
3a1a4661 451 __be32 v32;
61305a96 452 rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
3a1a4661 453 *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
61305a96
BH
454 break;
455 }
456 default:
457 return PCIBIOS_FUNC_NOT_SUPPORTED;
458 }
d0914f50 459
9bf41be6
GS
460 cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
461 __func__, pdn->busno, pdn->devfn, where, size, *val);
61305a96
BH
462 return PCIBIOS_SUCCESSFUL;
463}
464
9bf41be6
GS
465int pnv_pci_cfg_write(struct device_node *dn,
466 int where, int size, u32 val)
61305a96 467{
9bf41be6
GS
468 struct pci_dn *pdn = PCI_DN(dn);
469 struct pnv_phb *phb = pdn->phb->private_data;
470 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
61305a96 471
9bf41be6
GS
472 cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
473 pdn->busno, pdn->devfn, where, size, val);
61305a96
BH
474 switch (size) {
475 case 1:
476 opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
477 break;
478 case 2:
479 opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
480 break;
481 case 4:
482 opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
483 break;
484 default:
485 return PCIBIOS_FUNC_NOT_SUPPORTED;
486 }
be7e7446 487
d0914f50
GS
488 return PCIBIOS_SUCCESSFUL;
489}
490
491#if CONFIG_EEH
492static bool pnv_pci_cfg_check(struct pci_controller *hose,
493 struct device_node *dn)
494{
495 struct eeh_dev *edev = NULL;
496 struct pnv_phb *phb = hose->private_data;
497
498 /* EEH not enabled ? */
f5bc6b70 499 if (!(phb->flags & PNV_PHB_FLAG_EEH))
d0914f50 500 return true;
61305a96 501
d2b0f6f7 502 /* PE reset or device removed ? */
d0914f50 503 edev = of_node_to_eeh_dev(dn);
d2b0f6f7
GS
504 if (edev) {
505 if (edev->pe &&
8a6b3710 506 (edev->pe->state & EEH_PE_CFG_BLOCKED))
d2b0f6f7
GS
507 return false;
508
509 if (edev->mode & EEH_DEV_REMOVED)
510 return false;
511 }
d0914f50
GS
512
513 return true;
514}
515#else
516static inline pnv_pci_cfg_check(struct pci_controller *hose,
517 struct device_node *dn)
518{
519 return true;
61305a96 520}
d0914f50 521#endif /* CONFIG_EEH */
61305a96 522
9bf41be6
GS
523static int pnv_pci_read_config(struct pci_bus *bus,
524 unsigned int devfn,
525 int where, int size, u32 *val)
526{
527 struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
528 struct pci_dn *pdn;
d0914f50
GS
529 struct pnv_phb *phb;
530 bool found = false;
531 int ret;
9bf41be6 532
d0914f50 533 *val = 0xFFFFFFFF;
9bf41be6
GS
534 for (dn = busdn->child; dn; dn = dn->sibling) {
535 pdn = PCI_DN(dn);
d0914f50
GS
536 if (pdn && pdn->devfn == devfn) {
537 phb = pdn->phb->private_data;
538 found = true;
539 break;
540 }
9bf41be6
GS
541 }
542
d0914f50
GS
543 if (!found || !pnv_pci_cfg_check(pdn->phb, dn))
544 return PCIBIOS_DEVICE_NOT_FOUND;
545
546 ret = pnv_pci_cfg_read(dn, where, size, val);
547 if (phb->flags & PNV_PHB_FLAG_EEH) {
548 if (*val == EEH_IO_ERROR_VALUE(size) &&
549 eeh_dev_check_failure(of_node_to_eeh_dev(dn)))
550 return PCIBIOS_DEVICE_NOT_FOUND;
551 } else {
552 pnv_pci_config_check_eeh(phb, dn);
553 }
9bf41be6 554
d0914f50 555 return ret;
9bf41be6
GS
556}
557
558static int pnv_pci_write_config(struct pci_bus *bus,
559 unsigned int devfn,
560 int where, int size, u32 val)
561{
562 struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
563 struct pci_dn *pdn;
d0914f50
GS
564 struct pnv_phb *phb;
565 bool found = false;
566 int ret;
9bf41be6
GS
567
568 for (dn = busdn->child; dn; dn = dn->sibling) {
569 pdn = PCI_DN(dn);
d0914f50
GS
570 if (pdn && pdn->devfn == devfn) {
571 phb = pdn->phb->private_data;
572 found = true;
573 break;
574 }
9bf41be6
GS
575 }
576
d0914f50
GS
577 if (!found || !pnv_pci_cfg_check(pdn->phb, dn))
578 return PCIBIOS_DEVICE_NOT_FOUND;
579
580 ret = pnv_pci_cfg_write(dn, where, size, val);
581 if (!(phb->flags & PNV_PHB_FLAG_EEH))
582 pnv_pci_config_check_eeh(phb, dn);
583
584 return ret;
9bf41be6
GS
585}
586
61305a96 587struct pci_ops pnv_pci_ops = {
9bf41be6 588 .read = pnv_pci_read_config,
61305a96
BH
589 .write = pnv_pci_write_config,
590};
591
592static int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
593 unsigned long uaddr, enum dma_data_direction direction,
8e0a1611 594 struct dma_attrs *attrs, bool rm)
61305a96
BH
595{
596 u64 proto_tce;
3a1a4661 597 __be64 *tcep, *tces;
61305a96
BH
598 u64 rpn;
599
600 proto_tce = TCE_PCI_READ; // Read allowed
601
602 if (direction != DMA_TO_DEVICE)
603 proto_tce |= TCE_PCI_WRITE;
604
5e4da530 605 tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
bc32057e 606 rpn = __pa(uaddr) >> tbl->it_page_shift;
61305a96 607
1f1616e8 608 while (npages--)
bc32057e
AK
609 *(tcep++) = cpu_to_be64(proto_tce |
610 (rpn++ << tbl->it_page_shift));
1f1616e8
BH
611
612 /* Some implementations won't cache invalid TCEs and thus may not
613 * need that flush. We'll probably turn it_type into a bit mask
614 * of flags if that becomes the case
615 */
616 if (tbl->it_type & TCE_PCI_SWINV_CREATE)
8e0a1611 617 pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
61305a96 618
61305a96
BH
619 return 0;
620}
621
8e0a1611
AK
622static int pnv_tce_build_vm(struct iommu_table *tbl, long index, long npages,
623 unsigned long uaddr,
624 enum dma_data_direction direction,
625 struct dma_attrs *attrs)
626{
627 return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs,
628 false);
629}
630
631static void pnv_tce_free(struct iommu_table *tbl, long index, long npages,
632 bool rm)
61305a96 633{
3a1a4661 634 __be64 *tcep, *tces;
1f1616e8 635
5e4da530 636 tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
61305a96
BH
637
638 while (npages--)
3a1a4661 639 *(tcep++) = cpu_to_be64(0);
1f1616e8 640
605e44d6 641 if (tbl->it_type & TCE_PCI_SWINV_FREE)
8e0a1611
AK
642 pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
643}
644
645static void pnv_tce_free_vm(struct iommu_table *tbl, long index, long npages)
646{
647 pnv_tce_free(tbl, index, npages, false);
61305a96
BH
648}
649
11f63d3f
AK
650static unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
651{
652 return ((u64 *)tbl->it_base)[index - tbl->it_offset];
653}
654
8e0a1611
AK
655static int pnv_tce_build_rm(struct iommu_table *tbl, long index, long npages,
656 unsigned long uaddr,
657 enum dma_data_direction direction,
658 struct dma_attrs *attrs)
659{
660 return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs, true);
661}
662
663static void pnv_tce_free_rm(struct iommu_table *tbl, long index, long npages)
664{
665 pnv_tce_free(tbl, index, npages, true);
666}
667
61305a96
BH
668void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
669 void *tce_mem, u64 tce_size,
8fa5d454 670 u64 dma_offset, unsigned page_shift)
61305a96
BH
671{
672 tbl->it_blocksize = 16;
673 tbl->it_base = (unsigned long)tce_mem;
8fa5d454 674 tbl->it_page_shift = page_shift;
3a553170 675 tbl->it_offset = dma_offset >> tbl->it_page_shift;
61305a96
BH
676 tbl->it_index = 0;
677 tbl->it_size = tce_size >> 3;
678 tbl->it_busno = 0;
679 tbl->it_type = TCE_PCI;
680}
681
cad5cef6 682static struct iommu_table *pnv_pci_setup_bml_iommu(struct pci_controller *hose)
61305a96
BH
683{
684 struct iommu_table *tbl;
3a1a4661
BH
685 const __be64 *basep, *swinvp;
686 const __be32 *sizep;
61305a96
BH
687
688 basep = of_get_property(hose->dn, "linux,tce-base", NULL);
689 sizep = of_get_property(hose->dn, "linux,tce-size", NULL);
690 if (basep == NULL || sizep == NULL) {
1f1616e8
BH
691 pr_err("PCI: %s has missing tce entries !\n",
692 hose->dn->full_name);
61305a96
BH
693 return NULL;
694 }
695 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node);
696 if (WARN_ON(!tbl))
697 return NULL;
698 pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)),
8fa5d454 699 be32_to_cpup(sizep), 0, IOMMU_PAGE_SHIFT_4K);
61305a96 700 iommu_init_table(tbl, hose->node);
4e13c1ac 701 iommu_register_group(tbl, pci_domain_nr(hose->bus), 0);
1f1616e8
BH
702
703 /* Deal with SW invalidated TCEs when needed (BML way) */
704 swinvp = of_get_property(hose->dn, "linux,tce-sw-invalidate-info",
705 NULL);
706 if (swinvp) {
5e4da530 707 tbl->it_busno = be64_to_cpu(swinvp[1]);
3a1a4661 708 tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
1f1616e8
BH
709 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
710 }
61305a96
BH
711 return tbl;
712}
713
cad5cef6
GKH
714static void pnv_pci_dma_fallback_setup(struct pci_controller *hose,
715 struct pci_dev *pdev)
61305a96
BH
716{
717 struct device_node *np = pci_bus_to_OF_node(hose->bus);
718 struct pci_dn *pdn;
719
720 if (np == NULL)
721 return;
722 pdn = PCI_DN(np);
723 if (!pdn->iommu_table)
724 pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
725 if (!pdn->iommu_table)
726 return;
d905c5df 727 set_iommu_table_base_and_group(&pdev->dev, pdn->iommu_table);
61305a96
BH
728}
729
cad5cef6 730static void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
61305a96
BH
731{
732 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
733 struct pnv_phb *phb = hose->private_data;
734
735 /* If we have no phb structure, try to setup a fallback based on
736 * the device-tree (RTAS PCI for example)
737 */
738 if (phb && phb->dma_dev_setup)
739 phb->dma_dev_setup(phb, pdev);
740 else
741 pnv_pci_dma_fallback_setup(hose, pdev);
742}
743
cd15b048
BH
744int pnv_pci_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
745{
746 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
747 struct pnv_phb *phb = hose->private_data;
748
749 if (phb && phb->dma_set_mask)
750 return phb->dma_set_mask(phb, pdev, dma_mask);
751 return __dma_set_mask(&pdev->dev, dma_mask);
752}
753
fe7e85c6
GS
754u64 pnv_pci_dma_get_required_mask(struct pci_dev *pdev)
755{
756 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
757 struct pnv_phb *phb = hose->private_data;
758
759 if (phb && phb->dma_get_required_mask)
760 return phb->dma_get_required_mask(phb, pdev);
761
762 return __dma_get_required_mask(&pdev->dev);
763}
764
73ed148a
BH
765void pnv_pci_shutdown(void)
766{
767 struct pci_controller *hose;
768
769 list_for_each_entry(hose, &hose_list, list_node) {
770 struct pnv_phb *phb = hose->private_data;
771
772 if (phb && phb->shutdown)
773 phb->shutdown(phb);
774 }
775}
776
aa0c033f 777/* Fixup wrong class code in p7ioc and p8 root complex */
cad5cef6 778static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
ca45cfe3
BH
779{
780 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
781}
782DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
783
61305a96
BH
784void __init pnv_pci_init(void)
785{
786 struct device_node *np;
787
673c9756 788 pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
61305a96
BH
789
790 /* OPAL absent, try POPAL first then RTAS detection of PHBs */
791 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
792#ifdef CONFIG_PPC_POWERNV_RTAS
793 init_pci_config_tokens();
794 find_and_init_phbs();
795#endif /* CONFIG_PPC_POWERNV_RTAS */
184cd4a3
BH
796 }
797 /* OPAL is here, do our normal stuff */
798 else {
799 int found_ioda = 0;
800
801 /* Look for IODA IO-Hubs. We don't support mixing IODA
802 * and p5ioc2 due to the need to change some global
803 * probing flags
804 */
805 for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
806 pnv_pci_init_ioda_hub(np);
807 found_ioda = 1;
808 }
61305a96
BH
809
810 /* Look for p5ioc2 IO-Hubs */
184cd4a3
BH
811 if (!found_ioda)
812 for_each_compatible_node(np, NULL, "ibm,p5ioc2")
813 pnv_pci_init_p5ioc2_hub(np);
aa0c033f
GS
814
815 /* Look for ioda2 built-in PHB3's */
816 for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
817 pnv_pci_init_ioda2_phb(np);
61305a96
BH
818 }
819
820 /* Setup the linkage between OF nodes and PHBs */
821 pci_devs_phb_init();
822
823 /* Configure IOMMU DMA hooks */
824 ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
8e0a1611
AK
825 ppc_md.tce_build = pnv_tce_build_vm;
826 ppc_md.tce_free = pnv_tce_free_vm;
827 ppc_md.tce_build_rm = pnv_tce_build_rm;
828 ppc_md.tce_free_rm = pnv_tce_free_rm;
11f63d3f 829 ppc_md.tce_get = pnv_tce_get;
61305a96
BH
830 set_pci_dma_ops(&dma_iommu_ops);
831
c1a2562a
BH
832 /* Configure MSIs */
833#ifdef CONFIG_PCI_MSI
c1a2562a
BH
834 ppc_md.setup_msi_irqs = pnv_setup_msi_irqs;
835 ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs;
836#endif
61305a96 837}
d905c5df 838
b14726c5 839machine_subsys_initcall_sync(powernv, tce_iommu_bus_notifier_init);
This page took 0.225748 seconds and 5 git commands to generate.