2 * Support PCI/PCIe on PowerNV platforms
4 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/pci.h>
14 #include <linux/delay.h>
15 #include <linux/string.h>
16 #include <linux/init.h>
17 #include <linux/irq.h>
19 #include <linux/msi.h>
20 #include <linux/iommu.h>
21 #include <linux/sched/mm.h>
23 #include <asm/sections.h>
26 #include <asm/pci-bridge.h>
27 #include <asm/machdep.h>
28 #include <asm/msi_bitmap.h>
29 #include <asm/ppc-pci.h>
30 #include <asm/pnv-pci.h>
32 #include <asm/iommu.h>
34 #include <asm/firmware.h>
35 #include <asm/eeh_event.h>
41 static DEFINE_MUTEX(tunnel_mutex);
43 int pnv_pci_get_slot_id(struct device_node *np, uint64_t *id)
45 struct device_node *parent = np;
50 ret = of_property_read_u32(np, "reg", &bdfn);
54 bdfn = ((bdfn & 0x00ffff00) >> 8);
55 while ((parent = of_get_parent(parent))) {
56 if (!PCI_DN(parent)) {
61 if (!of_device_is_compatible(parent, "ibm,ioda2-phb")) {
66 ret = of_property_read_u64(parent, "ibm,opal-phbid", &phbid);
72 *id = PCI_SLOT_ID(phbid, bdfn);
78 EXPORT_SYMBOL_GPL(pnv_pci_get_slot_id);
80 int pnv_pci_get_device_tree(uint32_t phandle, void *buf, uint64_t len)
84 if (!opal_check_token(OPAL_GET_DEVICE_TREE))
87 rc = opal_get_device_tree(phandle, (uint64_t)buf, len);
88 if (rc < OPAL_SUCCESS)
93 EXPORT_SYMBOL_GPL(pnv_pci_get_device_tree);
95 int pnv_pci_get_presence_state(uint64_t id, uint8_t *state)
99 if (!opal_check_token(OPAL_PCI_GET_PRESENCE_STATE))
102 rc = opal_pci_get_presence_state(id, (uint64_t)state);
103 if (rc != OPAL_SUCCESS)
108 EXPORT_SYMBOL_GPL(pnv_pci_get_presence_state);
110 int pnv_pci_get_power_state(uint64_t id, uint8_t *state)
114 if (!opal_check_token(OPAL_PCI_GET_POWER_STATE))
117 rc = opal_pci_get_power_state(id, (uint64_t)state);
118 if (rc != OPAL_SUCCESS)
123 EXPORT_SYMBOL_GPL(pnv_pci_get_power_state);
125 int pnv_pci_set_power_state(uint64_t id, uint8_t state, struct opal_msg *msg)
131 if (!opal_check_token(OPAL_PCI_SET_POWER_STATE))
134 token = opal_async_get_token_interruptible();
135 if (unlikely(token < 0))
138 rc = opal_pci_set_power_state(token, id, (uint64_t)&state);
139 if (rc == OPAL_SUCCESS) {
142 } else if (rc != OPAL_ASYNC_COMPLETION) {
147 ret = opal_async_wait_response(token, &m);
153 memcpy(msg, &m, sizeof(m));
157 opal_async_release_token(token);
160 EXPORT_SYMBOL_GPL(pnv_pci_set_power_state);
162 int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
164 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
165 struct pnv_phb *phb = hose->private_data;
166 struct msi_desc *entry;
172 if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
175 if (pdev->no_64bit_msi && !phb->msi32_support)
178 for_each_pci_msi_entry(entry, pdev) {
179 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
180 pr_warn("%s: Supports only 64-bit MSIs\n",
184 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
186 pr_warn("%s: Failed to find a free MSI\n",
190 virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
192 pr_warn("%s: Failed to map MSI to linux irq\n",
194 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
197 rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
198 virq, entry->msi_attrib.is_64, &msg);
200 pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
201 irq_dispose_mapping(virq);
202 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
205 irq_set_msi_desc(virq, entry);
206 pci_write_msi_msg(virq, &msg);
211 void pnv_teardown_msi_irqs(struct pci_dev *pdev)
213 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
214 struct pnv_phb *phb = hose->private_data;
215 struct msi_desc *entry;
216 irq_hw_number_t hwirq;
221 for_each_pci_msi_entry(entry, pdev) {
224 hwirq = virq_to_hw(entry->irq);
225 irq_set_msi_desc(entry->irq, NULL);
226 irq_dispose_mapping(entry->irq);
227 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, 1);
231 /* Nicely print the contents of the PE State Tables (PEST). */
232 static void pnv_pci_dump_pest(__be64 pestA[], __be64 pestB[], int pest_size)
234 __be64 prevA = ULONG_MAX, prevB = ULONG_MAX;
238 for (i = 0; i < pest_size; i++) {
239 __be64 peA = be64_to_cpu(pestA[i]);
240 __be64 peB = be64_to_cpu(pestB[i]);
242 if (peA != prevA || peB != prevB) {
244 pr_info("PE[..%03x] A/B: as above\n", i-1);
249 if (peA & PNV_IODA_STOPPED_STATE ||
250 peB & PNV_IODA_STOPPED_STATE)
251 pr_info("PE[%03x] A/B: %016llx %016llx\n",
253 } else if (!dup && (peA & PNV_IODA_STOPPED_STATE ||
254 peB & PNV_IODA_STOPPED_STATE)) {
260 static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose,
261 struct OpalIoPhbErrorCommon *common)
263 struct OpalIoP7IOCPhbErrorData *data;
265 data = (struct OpalIoP7IOCPhbErrorData *)common;
266 pr_info("P7IOC PHB#%x Diag-data (Version: %d)\n",
267 hose->global_number, be32_to_cpu(common->version));
270 pr_info("brdgCtl: %08x\n",
271 be32_to_cpu(data->brdgCtl));
272 if (data->portStatusReg || data->rootCmplxStatus ||
273 data->busAgentStatus)
274 pr_info("UtlSts: %08x %08x %08x\n",
275 be32_to_cpu(data->portStatusReg),
276 be32_to_cpu(data->rootCmplxStatus),
277 be32_to_cpu(data->busAgentStatus));
278 if (data->deviceStatus || data->slotStatus ||
279 data->linkStatus || data->devCmdStatus ||
281 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
282 be32_to_cpu(data->deviceStatus),
283 be32_to_cpu(data->slotStatus),
284 be32_to_cpu(data->linkStatus),
285 be32_to_cpu(data->devCmdStatus),
286 be32_to_cpu(data->devSecStatus));
287 if (data->rootErrorStatus || data->uncorrErrorStatus ||
288 data->corrErrorStatus)
289 pr_info("RootErrSts: %08x %08x %08x\n",
290 be32_to_cpu(data->rootErrorStatus),
291 be32_to_cpu(data->uncorrErrorStatus),
292 be32_to_cpu(data->corrErrorStatus));
293 if (data->tlpHdr1 || data->tlpHdr2 ||
294 data->tlpHdr3 || data->tlpHdr4)
295 pr_info("RootErrLog: %08x %08x %08x %08x\n",
296 be32_to_cpu(data->tlpHdr1),
297 be32_to_cpu(data->tlpHdr2),
298 be32_to_cpu(data->tlpHdr3),
299 be32_to_cpu(data->tlpHdr4));
300 if (data->sourceId || data->errorClass ||
302 pr_info("RootErrLog1: %08x %016llx %016llx\n",
303 be32_to_cpu(data->sourceId),
304 be64_to_cpu(data->errorClass),
305 be64_to_cpu(data->correlator));
306 if (data->p7iocPlssr || data->p7iocCsr)
307 pr_info("PhbSts: %016llx %016llx\n",
308 be64_to_cpu(data->p7iocPlssr),
309 be64_to_cpu(data->p7iocCsr));
311 pr_info("Lem: %016llx %016llx %016llx\n",
312 be64_to_cpu(data->lemFir),
313 be64_to_cpu(data->lemErrorMask),
314 be64_to_cpu(data->lemWOF));
315 if (data->phbErrorStatus)
316 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
317 be64_to_cpu(data->phbErrorStatus),
318 be64_to_cpu(data->phbFirstErrorStatus),
319 be64_to_cpu(data->phbErrorLog0),
320 be64_to_cpu(data->phbErrorLog1));
321 if (data->mmioErrorStatus)
322 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
323 be64_to_cpu(data->mmioErrorStatus),
324 be64_to_cpu(data->mmioFirstErrorStatus),
325 be64_to_cpu(data->mmioErrorLog0),
326 be64_to_cpu(data->mmioErrorLog1));
327 if (data->dma0ErrorStatus)
328 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
329 be64_to_cpu(data->dma0ErrorStatus),
330 be64_to_cpu(data->dma0FirstErrorStatus),
331 be64_to_cpu(data->dma0ErrorLog0),
332 be64_to_cpu(data->dma0ErrorLog1));
333 if (data->dma1ErrorStatus)
334 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
335 be64_to_cpu(data->dma1ErrorStatus),
336 be64_to_cpu(data->dma1FirstErrorStatus),
337 be64_to_cpu(data->dma1ErrorLog0),
338 be64_to_cpu(data->dma1ErrorLog1));
340 pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_P7IOC_NUM_PEST_REGS);
343 static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
344 struct OpalIoPhbErrorCommon *common)
346 struct OpalIoPhb3ErrorData *data;
348 data = (struct OpalIoPhb3ErrorData*)common;
349 pr_info("PHB3 PHB#%x Diag-data (Version: %d)\n",
350 hose->global_number, be32_to_cpu(common->version));
352 pr_info("brdgCtl: %08x\n",
353 be32_to_cpu(data->brdgCtl));
354 if (data->portStatusReg || data->rootCmplxStatus ||
355 data->busAgentStatus)
356 pr_info("UtlSts: %08x %08x %08x\n",
357 be32_to_cpu(data->portStatusReg),
358 be32_to_cpu(data->rootCmplxStatus),
359 be32_to_cpu(data->busAgentStatus));
360 if (data->deviceStatus || data->slotStatus ||
361 data->linkStatus || data->devCmdStatus ||
363 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
364 be32_to_cpu(data->deviceStatus),
365 be32_to_cpu(data->slotStatus),
366 be32_to_cpu(data->linkStatus),
367 be32_to_cpu(data->devCmdStatus),
368 be32_to_cpu(data->devSecStatus));
369 if (data->rootErrorStatus || data->uncorrErrorStatus ||
370 data->corrErrorStatus)
371 pr_info("RootErrSts: %08x %08x %08x\n",
372 be32_to_cpu(data->rootErrorStatus),
373 be32_to_cpu(data->uncorrErrorStatus),
374 be32_to_cpu(data->corrErrorStatus));
375 if (data->tlpHdr1 || data->tlpHdr2 ||
376 data->tlpHdr3 || data->tlpHdr4)
377 pr_info("RootErrLog: %08x %08x %08x %08x\n",
378 be32_to_cpu(data->tlpHdr1),
379 be32_to_cpu(data->tlpHdr2),
380 be32_to_cpu(data->tlpHdr3),
381 be32_to_cpu(data->tlpHdr4));
382 if (data->sourceId || data->errorClass ||
384 pr_info("RootErrLog1: %08x %016llx %016llx\n",
385 be32_to_cpu(data->sourceId),
386 be64_to_cpu(data->errorClass),
387 be64_to_cpu(data->correlator));
389 pr_info("nFir: %016llx %016llx %016llx\n",
390 be64_to_cpu(data->nFir),
391 be64_to_cpu(data->nFirMask),
392 be64_to_cpu(data->nFirWOF));
393 if (data->phbPlssr || data->phbCsr)
394 pr_info("PhbSts: %016llx %016llx\n",
395 be64_to_cpu(data->phbPlssr),
396 be64_to_cpu(data->phbCsr));
398 pr_info("Lem: %016llx %016llx %016llx\n",
399 be64_to_cpu(data->lemFir),
400 be64_to_cpu(data->lemErrorMask),
401 be64_to_cpu(data->lemWOF));
402 if (data->phbErrorStatus)
403 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
404 be64_to_cpu(data->phbErrorStatus),
405 be64_to_cpu(data->phbFirstErrorStatus),
406 be64_to_cpu(data->phbErrorLog0),
407 be64_to_cpu(data->phbErrorLog1));
408 if (data->mmioErrorStatus)
409 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
410 be64_to_cpu(data->mmioErrorStatus),
411 be64_to_cpu(data->mmioFirstErrorStatus),
412 be64_to_cpu(data->mmioErrorLog0),
413 be64_to_cpu(data->mmioErrorLog1));
414 if (data->dma0ErrorStatus)
415 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
416 be64_to_cpu(data->dma0ErrorStatus),
417 be64_to_cpu(data->dma0FirstErrorStatus),
418 be64_to_cpu(data->dma0ErrorLog0),
419 be64_to_cpu(data->dma0ErrorLog1));
420 if (data->dma1ErrorStatus)
421 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
422 be64_to_cpu(data->dma1ErrorStatus),
423 be64_to_cpu(data->dma1FirstErrorStatus),
424 be64_to_cpu(data->dma1ErrorLog0),
425 be64_to_cpu(data->dma1ErrorLog1));
427 pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_PHB3_NUM_PEST_REGS);
430 static void pnv_pci_dump_phb4_diag_data(struct pci_controller *hose,
431 struct OpalIoPhbErrorCommon *common)
433 struct OpalIoPhb4ErrorData *data;
435 data = (struct OpalIoPhb4ErrorData*)common;
436 pr_info("PHB4 PHB#%d Diag-data (Version: %d)\n",
437 hose->global_number, be32_to_cpu(common->version));
439 pr_info("brdgCtl: %08x\n",
440 be32_to_cpu(data->brdgCtl));
441 if (data->deviceStatus || data->slotStatus ||
442 data->linkStatus || data->devCmdStatus ||
444 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
445 be32_to_cpu(data->deviceStatus),
446 be32_to_cpu(data->slotStatus),
447 be32_to_cpu(data->linkStatus),
448 be32_to_cpu(data->devCmdStatus),
449 be32_to_cpu(data->devSecStatus));
450 if (data->rootErrorStatus || data->uncorrErrorStatus ||
451 data->corrErrorStatus)
452 pr_info("RootErrSts: %08x %08x %08x\n",
453 be32_to_cpu(data->rootErrorStatus),
454 be32_to_cpu(data->uncorrErrorStatus),
455 be32_to_cpu(data->corrErrorStatus));
456 if (data->tlpHdr1 || data->tlpHdr2 ||
457 data->tlpHdr3 || data->tlpHdr4)
458 pr_info("RootErrLog: %08x %08x %08x %08x\n",
459 be32_to_cpu(data->tlpHdr1),
460 be32_to_cpu(data->tlpHdr2),
461 be32_to_cpu(data->tlpHdr3),
462 be32_to_cpu(data->tlpHdr4));
464 pr_info("sourceId: %08x\n", be32_to_cpu(data->sourceId));
466 pr_info("nFir: %016llx %016llx %016llx\n",
467 be64_to_cpu(data->nFir),
468 be64_to_cpu(data->nFirMask),
469 be64_to_cpu(data->nFirWOF));
470 if (data->phbPlssr || data->phbCsr)
471 pr_info("PhbSts: %016llx %016llx\n",
472 be64_to_cpu(data->phbPlssr),
473 be64_to_cpu(data->phbCsr));
475 pr_info("Lem: %016llx %016llx %016llx\n",
476 be64_to_cpu(data->lemFir),
477 be64_to_cpu(data->lemErrorMask),
478 be64_to_cpu(data->lemWOF));
479 if (data->phbErrorStatus)
480 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
481 be64_to_cpu(data->phbErrorStatus),
482 be64_to_cpu(data->phbFirstErrorStatus),
483 be64_to_cpu(data->phbErrorLog0),
484 be64_to_cpu(data->phbErrorLog1));
485 if (data->phbTxeErrorStatus)
486 pr_info("PhbTxeErr: %016llx %016llx %016llx %016llx\n",
487 be64_to_cpu(data->phbTxeErrorStatus),
488 be64_to_cpu(data->phbTxeFirstErrorStatus),
489 be64_to_cpu(data->phbTxeErrorLog0),
490 be64_to_cpu(data->phbTxeErrorLog1));
491 if (data->phbRxeArbErrorStatus)
492 pr_info("RxeArbErr: %016llx %016llx %016llx %016llx\n",
493 be64_to_cpu(data->phbRxeArbErrorStatus),
494 be64_to_cpu(data->phbRxeArbFirstErrorStatus),
495 be64_to_cpu(data->phbRxeArbErrorLog0),
496 be64_to_cpu(data->phbRxeArbErrorLog1));
497 if (data->phbRxeMrgErrorStatus)
498 pr_info("RxeMrgErr: %016llx %016llx %016llx %016llx\n",
499 be64_to_cpu(data->phbRxeMrgErrorStatus),
500 be64_to_cpu(data->phbRxeMrgFirstErrorStatus),
501 be64_to_cpu(data->phbRxeMrgErrorLog0),
502 be64_to_cpu(data->phbRxeMrgErrorLog1));
503 if (data->phbRxeTceErrorStatus)
504 pr_info("RxeTceErr: %016llx %016llx %016llx %016llx\n",
505 be64_to_cpu(data->phbRxeTceErrorStatus),
506 be64_to_cpu(data->phbRxeTceFirstErrorStatus),
507 be64_to_cpu(data->phbRxeTceErrorLog0),
508 be64_to_cpu(data->phbRxeTceErrorLog1));
510 if (data->phbPblErrorStatus)
511 pr_info("PblErr: %016llx %016llx %016llx %016llx\n",
512 be64_to_cpu(data->phbPblErrorStatus),
513 be64_to_cpu(data->phbPblFirstErrorStatus),
514 be64_to_cpu(data->phbPblErrorLog0),
515 be64_to_cpu(data->phbPblErrorLog1));
516 if (data->phbPcieDlpErrorStatus)
517 pr_info("PcieDlp: %016llx %016llx %016llx\n",
518 be64_to_cpu(data->phbPcieDlpErrorLog1),
519 be64_to_cpu(data->phbPcieDlpErrorLog2),
520 be64_to_cpu(data->phbPcieDlpErrorStatus));
521 if (data->phbRegbErrorStatus)
522 pr_info("RegbErr: %016llx %016llx %016llx %016llx\n",
523 be64_to_cpu(data->phbRegbErrorStatus),
524 be64_to_cpu(data->phbRegbFirstErrorStatus),
525 be64_to_cpu(data->phbRegbErrorLog0),
526 be64_to_cpu(data->phbRegbErrorLog1));
529 pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_PHB4_NUM_PEST_REGS);
532 void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
533 unsigned char *log_buff)
535 struct OpalIoPhbErrorCommon *common;
537 if (!hose || !log_buff)
540 common = (struct OpalIoPhbErrorCommon *)log_buff;
541 switch (be32_to_cpu(common->ioType)) {
542 case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
543 pnv_pci_dump_p7ioc_diag_data(hose, common);
545 case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
546 pnv_pci_dump_phb3_diag_data(hose, common);
548 case OPAL_PHB_ERROR_DATA_TYPE_PHB4:
549 pnv_pci_dump_phb4_diag_data(hose, common);
552 pr_warn("%s: Unrecognized ioType %d\n",
553 __func__, be32_to_cpu(common->ioType));
557 static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
559 unsigned long flags, rc;
560 int has_diag, ret = 0;
562 spin_lock_irqsave(&phb->lock, flags);
564 /* Fetch PHB diag-data */
565 rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
566 phb->diag_data_size);
567 has_diag = (rc == OPAL_SUCCESS);
569 /* If PHB supports compound PE, to handle it */
570 if (phb->unfreeze_pe) {
571 ret = phb->unfreeze_pe(phb,
573 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
575 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
577 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
579 pr_warn("%s: Failure %ld clearing frozen "
581 __func__, rc, phb->hose->global_number,
588 * For now, let's only display the diag buffer when we fail to clear
589 * the EEH status. We'll do more sensible things later when we have
590 * proper EEH support. We need to make sure we don't pollute ourselves
591 * with the normal errors generated when probing empty slots
594 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
596 spin_unlock_irqrestore(&phb->lock, flags);
599 static void pnv_pci_config_check_eeh(struct pci_dn *pdn)
601 struct pnv_phb *phb = pdn->phb->private_data;
608 * Get the PE#. During the PCI probe stage, we might not
609 * setup that yet. So all ER errors should be mapped to
612 pe_no = pdn->pe_number;
613 if (pe_no == IODA_INVALID_PE) {
614 pe_no = phb->ioda.reserved_pe_idx;
618 * Fetch frozen state. If the PHB support compound PE,
619 * we need handle that case.
621 if (phb->get_pe_state) {
622 fstate = phb->get_pe_state(phb, pe_no);
624 rc = opal_pci_eeh_freeze_status(phb->opal_id,
630 pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
631 __func__, rc, phb->hose->global_number, pe_no);
636 pr_devel(" -> EEH check, bdfn=%04x PE#%x fstate=%x\n",
637 (pdn->busno << 8) | (pdn->devfn), pe_no, fstate);
639 /* Clear the frozen state if applicable */
640 if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
641 fstate == OPAL_EEH_STOPPED_DMA_FREEZE ||
642 fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
644 * If PHB supports compound PE, freeze it for
648 phb->freeze_pe(phb, pe_no);
650 pnv_pci_handle_eeh_config(phb, pe_no);
654 int pnv_pci_cfg_read(struct pci_dn *pdn,
655 int where, int size, u32 *val)
657 struct pnv_phb *phb = pdn->phb->private_data;
658 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
664 rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
665 *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
670 rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
672 *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
677 rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
678 *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
682 return PCIBIOS_FUNC_NOT_SUPPORTED;
685 pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
686 __func__, pdn->busno, pdn->devfn, where, size, *val);
687 return PCIBIOS_SUCCESSFUL;
690 int pnv_pci_cfg_write(struct pci_dn *pdn,
691 int where, int size, u32 val)
693 struct pnv_phb *phb = pdn->phb->private_data;
694 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
696 pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
697 __func__, pdn->busno, pdn->devfn, where, size, val);
700 opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
703 opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
706 opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
709 return PCIBIOS_FUNC_NOT_SUPPORTED;
712 return PCIBIOS_SUCCESSFUL;
716 static bool pnv_pci_cfg_check(struct pci_dn *pdn)
718 struct eeh_dev *edev = NULL;
719 struct pnv_phb *phb = pdn->phb->private_data;
721 /* EEH not enabled ? */
722 if (!(phb->flags & PNV_PHB_FLAG_EEH))
725 /* PE reset or device removed ? */
729 (edev->pe->state & EEH_PE_CFG_BLOCKED))
732 if (edev->mode & EEH_DEV_REMOVED)
739 static inline pnv_pci_cfg_check(struct pci_dn *pdn)
743 #endif /* CONFIG_EEH */
745 static int pnv_pci_read_config(struct pci_bus *bus,
747 int where, int size, u32 *val)
754 pdn = pci_get_pdn_by_devfn(bus, devfn);
756 return PCIBIOS_DEVICE_NOT_FOUND;
758 if (!pnv_pci_cfg_check(pdn))
759 return PCIBIOS_DEVICE_NOT_FOUND;
761 ret = pnv_pci_cfg_read(pdn, where, size, val);
762 phb = pdn->phb->private_data;
763 if (phb->flags & PNV_PHB_FLAG_EEH && pdn->edev) {
764 if (*val == EEH_IO_ERROR_VALUE(size) &&
765 eeh_dev_check_failure(pdn->edev))
766 return PCIBIOS_DEVICE_NOT_FOUND;
768 pnv_pci_config_check_eeh(pdn);
774 static int pnv_pci_write_config(struct pci_bus *bus,
776 int where, int size, u32 val)
782 pdn = pci_get_pdn_by_devfn(bus, devfn);
784 return PCIBIOS_DEVICE_NOT_FOUND;
786 if (!pnv_pci_cfg_check(pdn))
787 return PCIBIOS_DEVICE_NOT_FOUND;
789 ret = pnv_pci_cfg_write(pdn, where, size, val);
790 phb = pdn->phb->private_data;
791 if (!(phb->flags & PNV_PHB_FLAG_EEH))
792 pnv_pci_config_check_eeh(pdn);
797 struct pci_ops pnv_pci_ops = {
798 .read = pnv_pci_read_config,
799 .write = pnv_pci_write_config,
802 struct iommu_table *pnv_pci_table_alloc(int nid)
804 struct iommu_table *tbl;
806 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
810 INIT_LIST_HEAD_RCU(&tbl->it_group_list);
811 kref_init(&tbl->it_kref);
816 void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
818 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
819 struct pnv_phb *phb = hose->private_data;
820 #ifdef CONFIG_PCI_IOV
821 struct pnv_ioda_pe *pe;
824 /* Fix the VF pdn PE number */
825 if (pdev->is_virtfn) {
826 pdn = pci_get_pdn(pdev);
827 WARN_ON(pdn->pe_number != IODA_INVALID_PE);
828 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
829 if (pe->rid == ((pdev->bus->number << 8) |
830 (pdev->devfn & 0xff))) {
831 pdn->pe_number = pe->pe_number;
837 #endif /* CONFIG_PCI_IOV */
839 if (phb && phb->dma_dev_setup)
840 phb->dma_dev_setup(phb, pdev);
843 void pnv_pci_dma_bus_setup(struct pci_bus *bus)
845 struct pci_controller *hose = bus->sysdata;
846 struct pnv_phb *phb = hose->private_data;
847 struct pnv_ioda_pe *pe;
849 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
850 if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
856 if (bus->number == ((pe->rid >> 8) & 0xFF)) {
863 struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
865 struct pci_controller *hose = pci_bus_to_host(dev->bus);
867 return of_node_get(hose->dn);
869 EXPORT_SYMBOL(pnv_pci_get_phb_node);
871 int pnv_pci_enable_tunnel(struct pci_dev *dev, u64 *asnind)
873 struct device_node *np;
875 struct pnv_ioda_pe *pe;
879 if (!radix_enabled())
882 if (!(np = pnv_pci_get_phb_node(dev)))
885 prop = of_get_property(np, "ibm,phb-indications", NULL);
888 if (!prop || !prop[1])
891 *asnind = (u64)be32_to_cpu(prop[1]);
892 pe = pnv_ioda_get_pe(dev);
896 /* Increase real window size to accept as_notify messages. */
897 window_id = (pe->pe_number << 1 ) + 1;
898 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, pe->pe_number,
899 window_id, pe->tce_bypass_base,
901 return opal_error_code(rc);
903 EXPORT_SYMBOL_GPL(pnv_pci_enable_tunnel);
905 int pnv_pci_disable_tunnel(struct pci_dev *dev)
907 struct pnv_ioda_pe *pe;
909 pe = pnv_ioda_get_pe(dev);
913 /* Restore default real window size. */
914 pnv_pci_ioda2_set_bypass(pe, true);
917 EXPORT_SYMBOL_GPL(pnv_pci_disable_tunnel);
919 int pnv_pci_set_tunnel_bar(struct pci_dev *dev, u64 addr, int enable)
922 struct pci_controller *hose;
927 if (!opal_check_token(OPAL_PCI_GET_PBCQ_TUNNEL_BAR))
929 if (!opal_check_token(OPAL_PCI_SET_PBCQ_TUNNEL_BAR))
932 hose = pci_bus_to_host(dev->bus);
933 phb = hose->private_data;
935 mutex_lock(&tunnel_mutex);
936 rc = opal_pci_get_pbcq_tunnel_bar(phb->opal_id, &val);
937 if (rc != OPAL_SUCCESS) {
941 tunnel_bar = be64_to_cpu(val);
944 * Only one device per PHB can use atomics.
945 * Our policy is first-come, first-served.
948 if (tunnel_bar != addr)
951 rc = 0; /* Setting same address twice is ok */
956 * The device that owns atomics and wants to release
957 * them must pass the same address with enable == 0.
959 if (tunnel_bar != addr) {
965 rc = opal_pci_set_pbcq_tunnel_bar(phb->opal_id, addr);
966 rc = opal_error_code(rc);
968 mutex_unlock(&tunnel_mutex);
971 EXPORT_SYMBOL_GPL(pnv_pci_set_tunnel_bar);
973 #ifdef CONFIG_PPC64 /* for thread.tidr */
974 int pnv_pci_get_as_notify_info(struct task_struct *task, u32 *lpid, u32 *pid,
977 struct mm_struct *mm = NULL;
982 mm = get_task_mm(task);
986 *pid = mm->context.id;
989 *tid = task->thread.tidr;
990 *lpid = mfspr(SPRN_LPID);
993 EXPORT_SYMBOL_GPL(pnv_pci_get_as_notify_info);
996 void pnv_pci_shutdown(void)
998 struct pci_controller *hose;
1000 list_for_each_entry(hose, &hose_list, list_node)
1001 if (hose->controller_ops.shutdown)
1002 hose->controller_ops.shutdown(hose);
1005 /* Fixup wrong class code in p7ioc and p8 root complex */
1006 static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
1008 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
1010 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
1012 void __init pnv_pci_init(void)
1014 struct device_node *np;
1016 pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
1018 /* If we don't have OPAL, eg. in sim, just skip PCI probe */
1019 if (!firmware_has_feature(FW_FEATURE_OPAL))
1022 /* Look for IODA IO-Hubs. */
1023 for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
1024 pnv_pci_init_ioda_hub(np);
1027 /* Look for ioda2 built-in PHB3's */
1028 for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
1029 pnv_pci_init_ioda2_phb(np);
1031 /* Look for ioda3 built-in PHB4's, we treat them as IODA2 */
1032 for_each_compatible_node(np, NULL, "ibm,ioda3-phb")
1033 pnv_pci_init_ioda2_phb(np);
1035 /* Look for NPU PHBs */
1036 for_each_compatible_node(np, NULL, "ibm,ioda2-npu-phb")
1037 pnv_pci_init_npu_phb(np);
1040 * Look for NPU2 PHBs which we treat mostly as NPU PHBs with
1041 * the exception of TCE kill which requires an OPAL call.
1043 for_each_compatible_node(np, NULL, "ibm,ioda2-npu2-phb")
1044 pnv_pci_init_npu_phb(np);
1046 /* Look for NPU2 OpenCAPI PHBs */
1047 for_each_compatible_node(np, NULL, "ibm,ioda2-npu2-opencapi-phb")
1048 pnv_pci_init_npu2_opencapi_phb(np);
1050 /* Configure IOMMU DMA hooks */
1051 set_pci_dma_ops(&dma_iommu_ops);
1054 static int pnv_tce_iommu_bus_notifier(struct notifier_block *nb,
1055 unsigned long action, void *data)
1057 struct device *dev = data;
1058 struct pci_dev *pdev;
1060 struct pnv_ioda_pe *pe;
1061 struct pci_controller *hose;
1062 struct pnv_phb *phb;
1065 case BUS_NOTIFY_ADD_DEVICE:
1066 pdev = to_pci_dev(dev);
1067 pdn = pci_get_pdn(pdev);
1068 hose = pci_bus_to_host(pdev->bus);
1069 phb = hose->private_data;
1072 if (!pdn || pdn->pe_number == IODA_INVALID_PE || !phb)
1075 pe = &phb->ioda.pe_array[pdn->pe_number];
1076 if (!pe->table_group.group)
1078 iommu_add_device(&pe->table_group, dev);
1080 case BUS_NOTIFY_DEL_DEVICE:
1081 iommu_del_device(dev);
1088 static struct notifier_block pnv_tce_iommu_bus_nb = {
1089 .notifier_call = pnv_tce_iommu_bus_notifier,
1092 static int __init pnv_tce_iommu_bus_notifier_init(void)
1094 bus_register_notifier(&pci_bus_type, &pnv_tce_iommu_bus_nb);
1097 machine_subsys_initcall_sync(powernv, pnv_tce_iommu_bus_notifier_init);