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(p2p_mutex);
42 static DEFINE_MUTEX(tunnel_mutex);
44 int pnv_pci_get_slot_id(struct device_node *np, uint64_t *id)
46 struct device_node *parent = np;
51 ret = of_property_read_u32(np, "reg", &bdfn);
55 bdfn = ((bdfn & 0x00ffff00) >> 8);
56 while ((parent = of_get_parent(parent))) {
57 if (!PCI_DN(parent)) {
62 if (!of_device_is_compatible(parent, "ibm,ioda2-phb")) {
67 ret = of_property_read_u64(parent, "ibm,opal-phbid", &phbid);
73 *id = PCI_SLOT_ID(phbid, bdfn);
79 EXPORT_SYMBOL_GPL(pnv_pci_get_slot_id);
81 int pnv_pci_get_device_tree(uint32_t phandle, void *buf, uint64_t len)
85 if (!opal_check_token(OPAL_GET_DEVICE_TREE))
88 rc = opal_get_device_tree(phandle, (uint64_t)buf, len);
89 if (rc < OPAL_SUCCESS)
94 EXPORT_SYMBOL_GPL(pnv_pci_get_device_tree);
96 int pnv_pci_get_presence_state(uint64_t id, uint8_t *state)
100 if (!opal_check_token(OPAL_PCI_GET_PRESENCE_STATE))
103 rc = opal_pci_get_presence_state(id, (uint64_t)state);
104 if (rc != OPAL_SUCCESS)
109 EXPORT_SYMBOL_GPL(pnv_pci_get_presence_state);
111 int pnv_pci_get_power_state(uint64_t id, uint8_t *state)
115 if (!opal_check_token(OPAL_PCI_GET_POWER_STATE))
118 rc = opal_pci_get_power_state(id, (uint64_t)state);
119 if (rc != OPAL_SUCCESS)
124 EXPORT_SYMBOL_GPL(pnv_pci_get_power_state);
126 int pnv_pci_set_power_state(uint64_t id, uint8_t state, struct opal_msg *msg)
132 if (!opal_check_token(OPAL_PCI_SET_POWER_STATE))
135 token = opal_async_get_token_interruptible();
136 if (unlikely(token < 0))
139 rc = opal_pci_set_power_state(token, id, (uint64_t)&state);
140 if (rc == OPAL_SUCCESS) {
143 } else if (rc != OPAL_ASYNC_COMPLETION) {
148 ret = opal_async_wait_response(token, &m);
154 memcpy(msg, &m, sizeof(m));
158 opal_async_release_token(token);
161 EXPORT_SYMBOL_GPL(pnv_pci_set_power_state);
163 #ifdef CONFIG_PCI_MSI
164 int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
166 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
167 struct pnv_phb *phb = hose->private_data;
168 struct msi_desc *entry;
174 if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
177 if (pdev->no_64bit_msi && !phb->msi32_support)
180 for_each_pci_msi_entry(entry, pdev) {
181 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
182 pr_warn("%s: Supports only 64-bit MSIs\n",
186 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
188 pr_warn("%s: Failed to find a free MSI\n",
192 virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
194 pr_warn("%s: Failed to map MSI to linux irq\n",
196 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
199 rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
200 virq, entry->msi_attrib.is_64, &msg);
202 pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
203 irq_dispose_mapping(virq);
204 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
207 irq_set_msi_desc(virq, entry);
208 pci_write_msi_msg(virq, &msg);
213 void pnv_teardown_msi_irqs(struct pci_dev *pdev)
215 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
216 struct pnv_phb *phb = hose->private_data;
217 struct msi_desc *entry;
218 irq_hw_number_t hwirq;
223 for_each_pci_msi_entry(entry, pdev) {
226 hwirq = virq_to_hw(entry->irq);
227 irq_set_msi_desc(entry->irq, NULL);
228 irq_dispose_mapping(entry->irq);
229 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, 1);
232 #endif /* CONFIG_PCI_MSI */
234 /* Nicely print the contents of the PE State Tables (PEST). */
235 static void pnv_pci_dump_pest(__be64 pestA[], __be64 pestB[], int pest_size)
237 __be64 prevA = ULONG_MAX, prevB = ULONG_MAX;
241 for (i = 0; i < pest_size; i++) {
242 __be64 peA = be64_to_cpu(pestA[i]);
243 __be64 peB = be64_to_cpu(pestB[i]);
245 if (peA != prevA || peB != prevB) {
247 pr_info("PE[..%03x] A/B: as above\n", i-1);
252 if (peA & PNV_IODA_STOPPED_STATE ||
253 peB & PNV_IODA_STOPPED_STATE)
254 pr_info("PE[%03x] A/B: %016llx %016llx\n",
256 } else if (!dup && (peA & PNV_IODA_STOPPED_STATE ||
257 peB & PNV_IODA_STOPPED_STATE)) {
263 static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose,
264 struct OpalIoPhbErrorCommon *common)
266 struct OpalIoP7IOCPhbErrorData *data;
268 data = (struct OpalIoP7IOCPhbErrorData *)common;
269 pr_info("P7IOC PHB#%x Diag-data (Version: %d)\n",
270 hose->global_number, be32_to_cpu(common->version));
273 pr_info("brdgCtl: %08x\n",
274 be32_to_cpu(data->brdgCtl));
275 if (data->portStatusReg || data->rootCmplxStatus ||
276 data->busAgentStatus)
277 pr_info("UtlSts: %08x %08x %08x\n",
278 be32_to_cpu(data->portStatusReg),
279 be32_to_cpu(data->rootCmplxStatus),
280 be32_to_cpu(data->busAgentStatus));
281 if (data->deviceStatus || data->slotStatus ||
282 data->linkStatus || data->devCmdStatus ||
284 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
285 be32_to_cpu(data->deviceStatus),
286 be32_to_cpu(data->slotStatus),
287 be32_to_cpu(data->linkStatus),
288 be32_to_cpu(data->devCmdStatus),
289 be32_to_cpu(data->devSecStatus));
290 if (data->rootErrorStatus || data->uncorrErrorStatus ||
291 data->corrErrorStatus)
292 pr_info("RootErrSts: %08x %08x %08x\n",
293 be32_to_cpu(data->rootErrorStatus),
294 be32_to_cpu(data->uncorrErrorStatus),
295 be32_to_cpu(data->corrErrorStatus));
296 if (data->tlpHdr1 || data->tlpHdr2 ||
297 data->tlpHdr3 || data->tlpHdr4)
298 pr_info("RootErrLog: %08x %08x %08x %08x\n",
299 be32_to_cpu(data->tlpHdr1),
300 be32_to_cpu(data->tlpHdr2),
301 be32_to_cpu(data->tlpHdr3),
302 be32_to_cpu(data->tlpHdr4));
303 if (data->sourceId || data->errorClass ||
305 pr_info("RootErrLog1: %08x %016llx %016llx\n",
306 be32_to_cpu(data->sourceId),
307 be64_to_cpu(data->errorClass),
308 be64_to_cpu(data->correlator));
309 if (data->p7iocPlssr || data->p7iocCsr)
310 pr_info("PhbSts: %016llx %016llx\n",
311 be64_to_cpu(data->p7iocPlssr),
312 be64_to_cpu(data->p7iocCsr));
314 pr_info("Lem: %016llx %016llx %016llx\n",
315 be64_to_cpu(data->lemFir),
316 be64_to_cpu(data->lemErrorMask),
317 be64_to_cpu(data->lemWOF));
318 if (data->phbErrorStatus)
319 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
320 be64_to_cpu(data->phbErrorStatus),
321 be64_to_cpu(data->phbFirstErrorStatus),
322 be64_to_cpu(data->phbErrorLog0),
323 be64_to_cpu(data->phbErrorLog1));
324 if (data->mmioErrorStatus)
325 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
326 be64_to_cpu(data->mmioErrorStatus),
327 be64_to_cpu(data->mmioFirstErrorStatus),
328 be64_to_cpu(data->mmioErrorLog0),
329 be64_to_cpu(data->mmioErrorLog1));
330 if (data->dma0ErrorStatus)
331 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
332 be64_to_cpu(data->dma0ErrorStatus),
333 be64_to_cpu(data->dma0FirstErrorStatus),
334 be64_to_cpu(data->dma0ErrorLog0),
335 be64_to_cpu(data->dma0ErrorLog1));
336 if (data->dma1ErrorStatus)
337 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
338 be64_to_cpu(data->dma1ErrorStatus),
339 be64_to_cpu(data->dma1FirstErrorStatus),
340 be64_to_cpu(data->dma1ErrorLog0),
341 be64_to_cpu(data->dma1ErrorLog1));
343 pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_P7IOC_NUM_PEST_REGS);
346 static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
347 struct OpalIoPhbErrorCommon *common)
349 struct OpalIoPhb3ErrorData *data;
351 data = (struct OpalIoPhb3ErrorData*)common;
352 pr_info("PHB3 PHB#%x Diag-data (Version: %d)\n",
353 hose->global_number, be32_to_cpu(common->version));
355 pr_info("brdgCtl: %08x\n",
356 be32_to_cpu(data->brdgCtl));
357 if (data->portStatusReg || data->rootCmplxStatus ||
358 data->busAgentStatus)
359 pr_info("UtlSts: %08x %08x %08x\n",
360 be32_to_cpu(data->portStatusReg),
361 be32_to_cpu(data->rootCmplxStatus),
362 be32_to_cpu(data->busAgentStatus));
363 if (data->deviceStatus || data->slotStatus ||
364 data->linkStatus || data->devCmdStatus ||
366 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
367 be32_to_cpu(data->deviceStatus),
368 be32_to_cpu(data->slotStatus),
369 be32_to_cpu(data->linkStatus),
370 be32_to_cpu(data->devCmdStatus),
371 be32_to_cpu(data->devSecStatus));
372 if (data->rootErrorStatus || data->uncorrErrorStatus ||
373 data->corrErrorStatus)
374 pr_info("RootErrSts: %08x %08x %08x\n",
375 be32_to_cpu(data->rootErrorStatus),
376 be32_to_cpu(data->uncorrErrorStatus),
377 be32_to_cpu(data->corrErrorStatus));
378 if (data->tlpHdr1 || data->tlpHdr2 ||
379 data->tlpHdr3 || data->tlpHdr4)
380 pr_info("RootErrLog: %08x %08x %08x %08x\n",
381 be32_to_cpu(data->tlpHdr1),
382 be32_to_cpu(data->tlpHdr2),
383 be32_to_cpu(data->tlpHdr3),
384 be32_to_cpu(data->tlpHdr4));
385 if (data->sourceId || data->errorClass ||
387 pr_info("RootErrLog1: %08x %016llx %016llx\n",
388 be32_to_cpu(data->sourceId),
389 be64_to_cpu(data->errorClass),
390 be64_to_cpu(data->correlator));
392 pr_info("nFir: %016llx %016llx %016llx\n",
393 be64_to_cpu(data->nFir),
394 be64_to_cpu(data->nFirMask),
395 be64_to_cpu(data->nFirWOF));
396 if (data->phbPlssr || data->phbCsr)
397 pr_info("PhbSts: %016llx %016llx\n",
398 be64_to_cpu(data->phbPlssr),
399 be64_to_cpu(data->phbCsr));
401 pr_info("Lem: %016llx %016llx %016llx\n",
402 be64_to_cpu(data->lemFir),
403 be64_to_cpu(data->lemErrorMask),
404 be64_to_cpu(data->lemWOF));
405 if (data->phbErrorStatus)
406 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
407 be64_to_cpu(data->phbErrorStatus),
408 be64_to_cpu(data->phbFirstErrorStatus),
409 be64_to_cpu(data->phbErrorLog0),
410 be64_to_cpu(data->phbErrorLog1));
411 if (data->mmioErrorStatus)
412 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
413 be64_to_cpu(data->mmioErrorStatus),
414 be64_to_cpu(data->mmioFirstErrorStatus),
415 be64_to_cpu(data->mmioErrorLog0),
416 be64_to_cpu(data->mmioErrorLog1));
417 if (data->dma0ErrorStatus)
418 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
419 be64_to_cpu(data->dma0ErrorStatus),
420 be64_to_cpu(data->dma0FirstErrorStatus),
421 be64_to_cpu(data->dma0ErrorLog0),
422 be64_to_cpu(data->dma0ErrorLog1));
423 if (data->dma1ErrorStatus)
424 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
425 be64_to_cpu(data->dma1ErrorStatus),
426 be64_to_cpu(data->dma1FirstErrorStatus),
427 be64_to_cpu(data->dma1ErrorLog0),
428 be64_to_cpu(data->dma1ErrorLog1));
430 pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_PHB3_NUM_PEST_REGS);
433 static void pnv_pci_dump_phb4_diag_data(struct pci_controller *hose,
434 struct OpalIoPhbErrorCommon *common)
436 struct OpalIoPhb4ErrorData *data;
438 data = (struct OpalIoPhb4ErrorData*)common;
439 pr_info("PHB4 PHB#%d Diag-data (Version: %d)\n",
440 hose->global_number, be32_to_cpu(common->version));
442 pr_info("brdgCtl: %08x\n",
443 be32_to_cpu(data->brdgCtl));
444 if (data->deviceStatus || data->slotStatus ||
445 data->linkStatus || data->devCmdStatus ||
447 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
448 be32_to_cpu(data->deviceStatus),
449 be32_to_cpu(data->slotStatus),
450 be32_to_cpu(data->linkStatus),
451 be32_to_cpu(data->devCmdStatus),
452 be32_to_cpu(data->devSecStatus));
453 if (data->rootErrorStatus || data->uncorrErrorStatus ||
454 data->corrErrorStatus)
455 pr_info("RootErrSts: %08x %08x %08x\n",
456 be32_to_cpu(data->rootErrorStatus),
457 be32_to_cpu(data->uncorrErrorStatus),
458 be32_to_cpu(data->corrErrorStatus));
459 if (data->tlpHdr1 || data->tlpHdr2 ||
460 data->tlpHdr3 || data->tlpHdr4)
461 pr_info("RootErrLog: %08x %08x %08x %08x\n",
462 be32_to_cpu(data->tlpHdr1),
463 be32_to_cpu(data->tlpHdr2),
464 be32_to_cpu(data->tlpHdr3),
465 be32_to_cpu(data->tlpHdr4));
467 pr_info("sourceId: %08x\n", be32_to_cpu(data->sourceId));
469 pr_info("nFir: %016llx %016llx %016llx\n",
470 be64_to_cpu(data->nFir),
471 be64_to_cpu(data->nFirMask),
472 be64_to_cpu(data->nFirWOF));
473 if (data->phbPlssr || data->phbCsr)
474 pr_info("PhbSts: %016llx %016llx\n",
475 be64_to_cpu(data->phbPlssr),
476 be64_to_cpu(data->phbCsr));
478 pr_info("Lem: %016llx %016llx %016llx\n",
479 be64_to_cpu(data->lemFir),
480 be64_to_cpu(data->lemErrorMask),
481 be64_to_cpu(data->lemWOF));
482 if (data->phbErrorStatus)
483 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
484 be64_to_cpu(data->phbErrorStatus),
485 be64_to_cpu(data->phbFirstErrorStatus),
486 be64_to_cpu(data->phbErrorLog0),
487 be64_to_cpu(data->phbErrorLog1));
488 if (data->phbTxeErrorStatus)
489 pr_info("PhbTxeErr: %016llx %016llx %016llx %016llx\n",
490 be64_to_cpu(data->phbTxeErrorStatus),
491 be64_to_cpu(data->phbTxeFirstErrorStatus),
492 be64_to_cpu(data->phbTxeErrorLog0),
493 be64_to_cpu(data->phbTxeErrorLog1));
494 if (data->phbRxeArbErrorStatus)
495 pr_info("RxeArbErr: %016llx %016llx %016llx %016llx\n",
496 be64_to_cpu(data->phbRxeArbErrorStatus),
497 be64_to_cpu(data->phbRxeArbFirstErrorStatus),
498 be64_to_cpu(data->phbRxeArbErrorLog0),
499 be64_to_cpu(data->phbRxeArbErrorLog1));
500 if (data->phbRxeMrgErrorStatus)
501 pr_info("RxeMrgErr: %016llx %016llx %016llx %016llx\n",
502 be64_to_cpu(data->phbRxeMrgErrorStatus),
503 be64_to_cpu(data->phbRxeMrgFirstErrorStatus),
504 be64_to_cpu(data->phbRxeMrgErrorLog0),
505 be64_to_cpu(data->phbRxeMrgErrorLog1));
506 if (data->phbRxeTceErrorStatus)
507 pr_info("RxeTceErr: %016llx %016llx %016llx %016llx\n",
508 be64_to_cpu(data->phbRxeTceErrorStatus),
509 be64_to_cpu(data->phbRxeTceFirstErrorStatus),
510 be64_to_cpu(data->phbRxeTceErrorLog0),
511 be64_to_cpu(data->phbRxeTceErrorLog1));
513 if (data->phbPblErrorStatus)
514 pr_info("PblErr: %016llx %016llx %016llx %016llx\n",
515 be64_to_cpu(data->phbPblErrorStatus),
516 be64_to_cpu(data->phbPblFirstErrorStatus),
517 be64_to_cpu(data->phbPblErrorLog0),
518 be64_to_cpu(data->phbPblErrorLog1));
519 if (data->phbPcieDlpErrorStatus)
520 pr_info("PcieDlp: %016llx %016llx %016llx\n",
521 be64_to_cpu(data->phbPcieDlpErrorLog1),
522 be64_to_cpu(data->phbPcieDlpErrorLog2),
523 be64_to_cpu(data->phbPcieDlpErrorStatus));
524 if (data->phbRegbErrorStatus)
525 pr_info("RegbErr: %016llx %016llx %016llx %016llx\n",
526 be64_to_cpu(data->phbRegbErrorStatus),
527 be64_to_cpu(data->phbRegbFirstErrorStatus),
528 be64_to_cpu(data->phbRegbErrorLog0),
529 be64_to_cpu(data->phbRegbErrorLog1));
532 pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_PHB4_NUM_PEST_REGS);
535 void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
536 unsigned char *log_buff)
538 struct OpalIoPhbErrorCommon *common;
540 if (!hose || !log_buff)
543 common = (struct OpalIoPhbErrorCommon *)log_buff;
544 switch (be32_to_cpu(common->ioType)) {
545 case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
546 pnv_pci_dump_p7ioc_diag_data(hose, common);
548 case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
549 pnv_pci_dump_phb3_diag_data(hose, common);
551 case OPAL_PHB_ERROR_DATA_TYPE_PHB4:
552 pnv_pci_dump_phb4_diag_data(hose, common);
555 pr_warn("%s: Unrecognized ioType %d\n",
556 __func__, be32_to_cpu(common->ioType));
560 static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
562 unsigned long flags, rc;
563 int has_diag, ret = 0;
565 spin_lock_irqsave(&phb->lock, flags);
567 /* Fetch PHB diag-data */
568 rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
569 phb->diag_data_size);
570 has_diag = (rc == OPAL_SUCCESS);
572 /* If PHB supports compound PE, to handle it */
573 if (phb->unfreeze_pe) {
574 ret = phb->unfreeze_pe(phb,
576 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
578 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
580 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
582 pr_warn("%s: Failure %ld clearing frozen "
584 __func__, rc, phb->hose->global_number,
591 * For now, let's only display the diag buffer when we fail to clear
592 * the EEH status. We'll do more sensible things later when we have
593 * proper EEH support. We need to make sure we don't pollute ourselves
594 * with the normal errors generated when probing empty slots
597 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
599 spin_unlock_irqrestore(&phb->lock, flags);
602 static void pnv_pci_config_check_eeh(struct pci_dn *pdn)
604 struct pnv_phb *phb = pdn->phb->private_data;
611 * Get the PE#. During the PCI probe stage, we might not
612 * setup that yet. So all ER errors should be mapped to
615 pe_no = pdn->pe_number;
616 if (pe_no == IODA_INVALID_PE) {
617 pe_no = phb->ioda.reserved_pe_idx;
621 * Fetch frozen state. If the PHB support compound PE,
622 * we need handle that case.
624 if (phb->get_pe_state) {
625 fstate = phb->get_pe_state(phb, pe_no);
627 rc = opal_pci_eeh_freeze_status(phb->opal_id,
633 pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
634 __func__, rc, phb->hose->global_number, pe_no);
639 pr_devel(" -> EEH check, bdfn=%04x PE#%x fstate=%x\n",
640 (pdn->busno << 8) | (pdn->devfn), pe_no, fstate);
642 /* Clear the frozen state if applicable */
643 if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
644 fstate == OPAL_EEH_STOPPED_DMA_FREEZE ||
645 fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
647 * If PHB supports compound PE, freeze it for
651 phb->freeze_pe(phb, pe_no);
653 pnv_pci_handle_eeh_config(phb, pe_no);
657 int pnv_pci_cfg_read(struct pci_dn *pdn,
658 int where, int size, u32 *val)
660 struct pnv_phb *phb = pdn->phb->private_data;
661 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
667 rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
668 *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
673 rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
675 *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
680 rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
681 *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
685 return PCIBIOS_FUNC_NOT_SUPPORTED;
688 pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
689 __func__, pdn->busno, pdn->devfn, where, size, *val);
690 return PCIBIOS_SUCCESSFUL;
693 int pnv_pci_cfg_write(struct pci_dn *pdn,
694 int where, int size, u32 val)
696 struct pnv_phb *phb = pdn->phb->private_data;
697 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
699 pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
700 __func__, pdn->busno, pdn->devfn, where, size, val);
703 opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
706 opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
709 opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
712 return PCIBIOS_FUNC_NOT_SUPPORTED;
715 return PCIBIOS_SUCCESSFUL;
719 static bool pnv_pci_cfg_check(struct pci_dn *pdn)
721 struct eeh_dev *edev = NULL;
722 struct pnv_phb *phb = pdn->phb->private_data;
724 /* EEH not enabled ? */
725 if (!(phb->flags & PNV_PHB_FLAG_EEH))
728 /* PE reset or device removed ? */
732 (edev->pe->state & EEH_PE_CFG_BLOCKED))
735 if (edev->mode & EEH_DEV_REMOVED)
742 static inline pnv_pci_cfg_check(struct pci_dn *pdn)
746 #endif /* CONFIG_EEH */
748 static int pnv_pci_read_config(struct pci_bus *bus,
750 int where, int size, u32 *val)
757 pdn = pci_get_pdn_by_devfn(bus, devfn);
759 return PCIBIOS_DEVICE_NOT_FOUND;
761 if (!pnv_pci_cfg_check(pdn))
762 return PCIBIOS_DEVICE_NOT_FOUND;
764 ret = pnv_pci_cfg_read(pdn, where, size, val);
765 phb = pdn->phb->private_data;
766 if (phb->flags & PNV_PHB_FLAG_EEH && pdn->edev) {
767 if (*val == EEH_IO_ERROR_VALUE(size) &&
768 eeh_dev_check_failure(pdn->edev))
769 return PCIBIOS_DEVICE_NOT_FOUND;
771 pnv_pci_config_check_eeh(pdn);
777 static int pnv_pci_write_config(struct pci_bus *bus,
779 int where, int size, u32 val)
785 pdn = pci_get_pdn_by_devfn(bus, devfn);
787 return PCIBIOS_DEVICE_NOT_FOUND;
789 if (!pnv_pci_cfg_check(pdn))
790 return PCIBIOS_DEVICE_NOT_FOUND;
792 ret = pnv_pci_cfg_write(pdn, where, size, val);
793 phb = pdn->phb->private_data;
794 if (!(phb->flags & PNV_PHB_FLAG_EEH))
795 pnv_pci_config_check_eeh(pdn);
800 struct pci_ops pnv_pci_ops = {
801 .read = pnv_pci_read_config,
802 .write = pnv_pci_write_config,
805 struct iommu_table *pnv_pci_table_alloc(int nid)
807 struct iommu_table *tbl;
809 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
813 INIT_LIST_HEAD_RCU(&tbl->it_group_list);
814 kref_init(&tbl->it_kref);
819 void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
821 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
822 struct pnv_phb *phb = hose->private_data;
824 if (phb && phb->dma_dev_setup)
825 phb->dma_dev_setup(phb, pdev);
828 void pnv_pci_dma_bus_setup(struct pci_bus *bus)
830 struct pci_controller *hose = bus->sysdata;
831 struct pnv_phb *phb = hose->private_data;
832 struct pnv_ioda_pe *pe;
834 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
835 if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
841 if (bus->number == ((pe->rid >> 8) & 0xFF)) {
848 int pnv_pci_set_p2p(struct pci_dev *initiator, struct pci_dev *target, u64 desc)
850 struct pci_controller *hose;
851 struct pnv_phb *phb_init, *phb_target;
852 struct pnv_ioda_pe *pe_init;
855 if (!opal_check_token(OPAL_PCI_SET_P2P))
858 hose = pci_bus_to_host(initiator->bus);
859 phb_init = hose->private_data;
861 hose = pci_bus_to_host(target->bus);
862 phb_target = hose->private_data;
864 pe_init = pnv_ioda_get_pe(initiator);
869 * Configuring the initiator's PHB requires to adjust its
870 * TVE#1 setting. Since the same device can be an initiator
871 * several times for different target devices, we need to keep
872 * a reference count to know when we can restore the default
873 * bypass setting on its TVE#1 when disabling. Opal is not
874 * tracking PE states, so we add a reference count on the PE
877 * For the target, the configuration is per PHB, so we keep a
878 * target reference count on the PHB.
880 mutex_lock(&p2p_mutex);
882 if (desc & OPAL_PCI_P2P_ENABLE) {
883 /* always go to opal to validate the configuration */
884 rc = opal_pci_set_p2p(phb_init->opal_id, phb_target->opal_id,
885 desc, pe_init->pe_number);
887 if (rc != OPAL_SUCCESS) {
892 pe_init->p2p_initiator_count++;
893 phb_target->p2p_target_count++;
895 if (!pe_init->p2p_initiator_count ||
896 !phb_target->p2p_target_count) {
901 if (--pe_init->p2p_initiator_count == 0)
902 pnv_pci_ioda2_set_bypass(pe_init, true);
904 if (--phb_target->p2p_target_count == 0) {
905 rc = opal_pci_set_p2p(phb_init->opal_id,
906 phb_target->opal_id, desc,
908 if (rc != OPAL_SUCCESS) {
916 mutex_unlock(&p2p_mutex);
919 EXPORT_SYMBOL_GPL(pnv_pci_set_p2p);
921 struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
923 struct pci_controller *hose = pci_bus_to_host(dev->bus);
925 return of_node_get(hose->dn);
927 EXPORT_SYMBOL(pnv_pci_get_phb_node);
929 int pnv_pci_enable_tunnel(struct pci_dev *dev, u64 *asnind)
931 struct device_node *np;
933 struct pnv_ioda_pe *pe;
937 if (!radix_enabled())
940 if (!(np = pnv_pci_get_phb_node(dev)))
943 prop = of_get_property(np, "ibm,phb-indications", NULL);
946 if (!prop || !prop[1])
949 *asnind = (u64)be32_to_cpu(prop[1]);
950 pe = pnv_ioda_get_pe(dev);
954 /* Increase real window size to accept as_notify messages. */
955 window_id = (pe->pe_number << 1 ) + 1;
956 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, pe->pe_number,
957 window_id, pe->tce_bypass_base,
959 return opal_error_code(rc);
961 EXPORT_SYMBOL_GPL(pnv_pci_enable_tunnel);
963 int pnv_pci_disable_tunnel(struct pci_dev *dev)
965 struct pnv_ioda_pe *pe;
967 pe = pnv_ioda_get_pe(dev);
971 /* Restore default real window size. */
972 pnv_pci_ioda2_set_bypass(pe, true);
975 EXPORT_SYMBOL_GPL(pnv_pci_disable_tunnel);
977 int pnv_pci_set_tunnel_bar(struct pci_dev *dev, u64 addr, int enable)
980 struct pci_controller *hose;
985 if (!opal_check_token(OPAL_PCI_GET_PBCQ_TUNNEL_BAR))
987 if (!opal_check_token(OPAL_PCI_SET_PBCQ_TUNNEL_BAR))
990 hose = pci_bus_to_host(dev->bus);
991 phb = hose->private_data;
993 mutex_lock(&tunnel_mutex);
994 rc = opal_pci_get_pbcq_tunnel_bar(phb->opal_id, &val);
995 if (rc != OPAL_SUCCESS) {
999 tunnel_bar = be64_to_cpu(val);
1002 * Only one device per PHB can use atomics.
1003 * Our policy is first-come, first-served.
1006 if (tunnel_bar != addr)
1009 rc = 0; /* Setting same address twice is ok */
1014 * The device that owns atomics and wants to release
1015 * them must pass the same address with enable == 0.
1017 if (tunnel_bar != addr) {
1023 rc = opal_pci_set_pbcq_tunnel_bar(phb->opal_id, addr);
1024 rc = opal_error_code(rc);
1026 mutex_unlock(&tunnel_mutex);
1029 EXPORT_SYMBOL_GPL(pnv_pci_set_tunnel_bar);
1031 #ifdef CONFIG_PPC64 /* for thread.tidr */
1032 int pnv_pci_get_as_notify_info(struct task_struct *task, u32 *lpid, u32 *pid,
1035 struct mm_struct *mm = NULL;
1040 mm = get_task_mm(task);
1044 *pid = mm->context.id;
1047 *tid = task->thread.tidr;
1048 *lpid = mfspr(SPRN_LPID);
1051 EXPORT_SYMBOL_GPL(pnv_pci_get_as_notify_info);
1054 void pnv_pci_shutdown(void)
1056 struct pci_controller *hose;
1058 list_for_each_entry(hose, &hose_list, list_node)
1059 if (hose->controller_ops.shutdown)
1060 hose->controller_ops.shutdown(hose);
1063 /* Fixup wrong class code in p7ioc and p8 root complex */
1064 static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
1066 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
1068 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
1070 void __init pnv_pci_init(void)
1072 struct device_node *np;
1074 pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
1076 /* If we don't have OPAL, eg. in sim, just skip PCI probe */
1077 if (!firmware_has_feature(FW_FEATURE_OPAL))
1080 #ifdef CONFIG_PCIEPORTBUS
1082 * On PowerNV PCIe devices are (currently) managed in cooperation
1083 * with firmware. This isn't *strictly* required, but there's enough
1084 * assumptions baked into both firmware and the platform code that
1085 * it's unwise to allow the portbus services to be used.
1087 * We need to fix this eventually, but for now set this flag to disable
1088 * the portbus driver. The AER service isn't required since that AER
1089 * events are handled via EEH. The pciehp hotplug driver can't work
1090 * without kernel changes (and portbus binding breaks pnv_php). The
1091 * other services also require some thinking about how we're going
1092 * to integrate them.
1094 pcie_ports_disabled = true;
1097 /* Look for IODA IO-Hubs. */
1098 for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
1099 pnv_pci_init_ioda_hub(np);
1102 /* Look for ioda2 built-in PHB3's */
1103 for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
1104 pnv_pci_init_ioda2_phb(np);
1106 /* Look for ioda3 built-in PHB4's, we treat them as IODA2 */
1107 for_each_compatible_node(np, NULL, "ibm,ioda3-phb")
1108 pnv_pci_init_ioda2_phb(np);
1110 /* Look for NPU PHBs */
1111 for_each_compatible_node(np, NULL, "ibm,ioda2-npu-phb")
1112 pnv_pci_init_npu_phb(np);
1115 * Look for NPU2 PHBs which we treat mostly as NPU PHBs with
1116 * the exception of TCE kill which requires an OPAL call.
1118 for_each_compatible_node(np, NULL, "ibm,ioda2-npu2-phb")
1119 pnv_pci_init_npu_phb(np);
1121 /* Look for NPU2 OpenCAPI PHBs */
1122 for_each_compatible_node(np, NULL, "ibm,ioda2-npu2-opencapi-phb")
1123 pnv_pci_init_npu2_opencapi_phb(np);
1125 /* Configure IOMMU DMA hooks */
1126 set_pci_dma_ops(&dma_iommu_ops);
1129 machine_subsys_initcall_sync(powernv, tce_iommu_bus_notifier_init);