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>
22 #include <asm/sections.h>
25 #include <asm/pci-bridge.h>
26 #include <asm/machdep.h>
27 #include <asm/msi_bitmap.h>
28 #include <asm/ppc-pci.h>
29 #include <asm/pnv-pci.h>
31 #include <asm/iommu.h>
33 #include <asm/firmware.h>
34 #include <asm/eeh_event.h>
40 int pnv_pci_get_slot_id(struct device_node *np, uint64_t *id)
42 struct device_node *parent = np;
47 ret = of_property_read_u32(np, "reg", &bdfn);
51 bdfn = ((bdfn & 0x00ffff00) >> 8);
52 while ((parent = of_get_parent(parent))) {
53 if (!PCI_DN(parent)) {
58 if (!of_device_is_compatible(parent, "ibm,ioda2-phb")) {
63 ret = of_property_read_u64(parent, "ibm,opal-phbid", &phbid);
69 *id = PCI_SLOT_ID(phbid, bdfn);
75 EXPORT_SYMBOL_GPL(pnv_pci_get_slot_id);
77 int pnv_pci_get_device_tree(uint32_t phandle, void *buf, uint64_t len)
81 if (!opal_check_token(OPAL_GET_DEVICE_TREE))
84 rc = opal_get_device_tree(phandle, (uint64_t)buf, len);
85 if (rc < OPAL_SUCCESS)
90 EXPORT_SYMBOL_GPL(pnv_pci_get_device_tree);
92 int pnv_pci_get_presence_state(uint64_t id, uint8_t *state)
96 if (!opal_check_token(OPAL_PCI_GET_PRESENCE_STATE))
99 rc = opal_pci_get_presence_state(id, (uint64_t)state);
100 if (rc != OPAL_SUCCESS)
105 EXPORT_SYMBOL_GPL(pnv_pci_get_presence_state);
107 int pnv_pci_get_power_state(uint64_t id, uint8_t *state)
111 if (!opal_check_token(OPAL_PCI_GET_POWER_STATE))
114 rc = opal_pci_get_power_state(id, (uint64_t)state);
115 if (rc != OPAL_SUCCESS)
120 EXPORT_SYMBOL_GPL(pnv_pci_get_power_state);
122 int pnv_pci_set_power_state(uint64_t id, uint8_t state, struct opal_msg *msg)
128 if (!opal_check_token(OPAL_PCI_SET_POWER_STATE))
131 token = opal_async_get_token_interruptible();
132 if (unlikely(token < 0))
135 rc = opal_pci_set_power_state(token, id, (uint64_t)&state);
136 if (rc == OPAL_SUCCESS) {
139 } else if (rc != OPAL_ASYNC_COMPLETION) {
144 ret = opal_async_wait_response(token, &m);
150 memcpy(msg, &m, sizeof(m));
154 opal_async_release_token(token);
157 EXPORT_SYMBOL_GPL(pnv_pci_set_power_state);
159 #ifdef CONFIG_PCI_MSI
160 int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
162 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
163 struct pnv_phb *phb = hose->private_data;
164 struct msi_desc *entry;
170 if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
173 if (pdev->no_64bit_msi && !phb->msi32_support)
176 for_each_pci_msi_entry(entry, pdev) {
177 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
178 pr_warn("%s: Supports only 64-bit MSIs\n",
182 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
184 pr_warn("%s: Failed to find a free MSI\n",
188 virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
190 pr_warn("%s: Failed to map MSI to linux irq\n",
192 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
195 rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
196 virq, entry->msi_attrib.is_64, &msg);
198 pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
199 irq_dispose_mapping(virq);
200 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
203 irq_set_msi_desc(virq, entry);
204 pci_write_msi_msg(virq, &msg);
209 void pnv_teardown_msi_irqs(struct pci_dev *pdev)
211 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
212 struct pnv_phb *phb = hose->private_data;
213 struct msi_desc *entry;
214 irq_hw_number_t hwirq;
219 for_each_pci_msi_entry(entry, pdev) {
222 hwirq = virq_to_hw(entry->irq);
223 irq_set_msi_desc(entry->irq, NULL);
224 irq_dispose_mapping(entry->irq);
225 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, 1);
228 #endif /* CONFIG_PCI_MSI */
230 /* Nicely print the contents of the PE State Tables (PEST). */
231 static void pnv_pci_dump_pest(__be64 pestA[], __be64 pestB[], int pest_size)
233 __be64 prevA = ULONG_MAX, prevB = ULONG_MAX;
237 for (i = 0; i < pest_size; i++) {
238 __be64 peA = be64_to_cpu(pestA[i]);
239 __be64 peB = be64_to_cpu(pestB[i]);
241 if (peA != prevA || peB != prevB) {
243 pr_info("PE[..%03x] A/B: as above\n", i-1);
248 if (peA & PNV_IODA_STOPPED_STATE ||
249 peB & PNV_IODA_STOPPED_STATE)
250 pr_info("PE[%03x] A/B: %016llx %016llx\n",
252 } else if (!dup && (peA & PNV_IODA_STOPPED_STATE ||
253 peB & PNV_IODA_STOPPED_STATE)) {
259 static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose,
260 struct OpalIoPhbErrorCommon *common)
262 struct OpalIoP7IOCPhbErrorData *data;
264 data = (struct OpalIoP7IOCPhbErrorData *)common;
265 pr_info("P7IOC PHB#%x Diag-data (Version: %d)\n",
266 hose->global_number, be32_to_cpu(common->version));
269 pr_info("brdgCtl: %08x\n",
270 be32_to_cpu(data->brdgCtl));
271 if (data->portStatusReg || data->rootCmplxStatus ||
272 data->busAgentStatus)
273 pr_info("UtlSts: %08x %08x %08x\n",
274 be32_to_cpu(data->portStatusReg),
275 be32_to_cpu(data->rootCmplxStatus),
276 be32_to_cpu(data->busAgentStatus));
277 if (data->deviceStatus || data->slotStatus ||
278 data->linkStatus || data->devCmdStatus ||
280 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
281 be32_to_cpu(data->deviceStatus),
282 be32_to_cpu(data->slotStatus),
283 be32_to_cpu(data->linkStatus),
284 be32_to_cpu(data->devCmdStatus),
285 be32_to_cpu(data->devSecStatus));
286 if (data->rootErrorStatus || data->uncorrErrorStatus ||
287 data->corrErrorStatus)
288 pr_info("RootErrSts: %08x %08x %08x\n",
289 be32_to_cpu(data->rootErrorStatus),
290 be32_to_cpu(data->uncorrErrorStatus),
291 be32_to_cpu(data->corrErrorStatus));
292 if (data->tlpHdr1 || data->tlpHdr2 ||
293 data->tlpHdr3 || data->tlpHdr4)
294 pr_info("RootErrLog: %08x %08x %08x %08x\n",
295 be32_to_cpu(data->tlpHdr1),
296 be32_to_cpu(data->tlpHdr2),
297 be32_to_cpu(data->tlpHdr3),
298 be32_to_cpu(data->tlpHdr4));
299 if (data->sourceId || data->errorClass ||
301 pr_info("RootErrLog1: %08x %016llx %016llx\n",
302 be32_to_cpu(data->sourceId),
303 be64_to_cpu(data->errorClass),
304 be64_to_cpu(data->correlator));
305 if (data->p7iocPlssr || data->p7iocCsr)
306 pr_info("PhbSts: %016llx %016llx\n",
307 be64_to_cpu(data->p7iocPlssr),
308 be64_to_cpu(data->p7iocCsr));
310 pr_info("Lem: %016llx %016llx %016llx\n",
311 be64_to_cpu(data->lemFir),
312 be64_to_cpu(data->lemErrorMask),
313 be64_to_cpu(data->lemWOF));
314 if (data->phbErrorStatus)
315 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
316 be64_to_cpu(data->phbErrorStatus),
317 be64_to_cpu(data->phbFirstErrorStatus),
318 be64_to_cpu(data->phbErrorLog0),
319 be64_to_cpu(data->phbErrorLog1));
320 if (data->mmioErrorStatus)
321 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
322 be64_to_cpu(data->mmioErrorStatus),
323 be64_to_cpu(data->mmioFirstErrorStatus),
324 be64_to_cpu(data->mmioErrorLog0),
325 be64_to_cpu(data->mmioErrorLog1));
326 if (data->dma0ErrorStatus)
327 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
328 be64_to_cpu(data->dma0ErrorStatus),
329 be64_to_cpu(data->dma0FirstErrorStatus),
330 be64_to_cpu(data->dma0ErrorLog0),
331 be64_to_cpu(data->dma0ErrorLog1));
332 if (data->dma1ErrorStatus)
333 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
334 be64_to_cpu(data->dma1ErrorStatus),
335 be64_to_cpu(data->dma1FirstErrorStatus),
336 be64_to_cpu(data->dma1ErrorLog0),
337 be64_to_cpu(data->dma1ErrorLog1));
339 pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_P7IOC_NUM_PEST_REGS);
342 static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
343 struct OpalIoPhbErrorCommon *common)
345 struct OpalIoPhb3ErrorData *data;
347 data = (struct OpalIoPhb3ErrorData*)common;
348 pr_info("PHB3 PHB#%x Diag-data (Version: %d)\n",
349 hose->global_number, be32_to_cpu(common->version));
351 pr_info("brdgCtl: %08x\n",
352 be32_to_cpu(data->brdgCtl));
353 if (data->portStatusReg || data->rootCmplxStatus ||
354 data->busAgentStatus)
355 pr_info("UtlSts: %08x %08x %08x\n",
356 be32_to_cpu(data->portStatusReg),
357 be32_to_cpu(data->rootCmplxStatus),
358 be32_to_cpu(data->busAgentStatus));
359 if (data->deviceStatus || data->slotStatus ||
360 data->linkStatus || data->devCmdStatus ||
362 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
363 be32_to_cpu(data->deviceStatus),
364 be32_to_cpu(data->slotStatus),
365 be32_to_cpu(data->linkStatus),
366 be32_to_cpu(data->devCmdStatus),
367 be32_to_cpu(data->devSecStatus));
368 if (data->rootErrorStatus || data->uncorrErrorStatus ||
369 data->corrErrorStatus)
370 pr_info("RootErrSts: %08x %08x %08x\n",
371 be32_to_cpu(data->rootErrorStatus),
372 be32_to_cpu(data->uncorrErrorStatus),
373 be32_to_cpu(data->corrErrorStatus));
374 if (data->tlpHdr1 || data->tlpHdr2 ||
375 data->tlpHdr3 || data->tlpHdr4)
376 pr_info("RootErrLog: %08x %08x %08x %08x\n",
377 be32_to_cpu(data->tlpHdr1),
378 be32_to_cpu(data->tlpHdr2),
379 be32_to_cpu(data->tlpHdr3),
380 be32_to_cpu(data->tlpHdr4));
381 if (data->sourceId || data->errorClass ||
383 pr_info("RootErrLog1: %08x %016llx %016llx\n",
384 be32_to_cpu(data->sourceId),
385 be64_to_cpu(data->errorClass),
386 be64_to_cpu(data->correlator));
388 pr_info("nFir: %016llx %016llx %016llx\n",
389 be64_to_cpu(data->nFir),
390 be64_to_cpu(data->nFirMask),
391 be64_to_cpu(data->nFirWOF));
392 if (data->phbPlssr || data->phbCsr)
393 pr_info("PhbSts: %016llx %016llx\n",
394 be64_to_cpu(data->phbPlssr),
395 be64_to_cpu(data->phbCsr));
397 pr_info("Lem: %016llx %016llx %016llx\n",
398 be64_to_cpu(data->lemFir),
399 be64_to_cpu(data->lemErrorMask),
400 be64_to_cpu(data->lemWOF));
401 if (data->phbErrorStatus)
402 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
403 be64_to_cpu(data->phbErrorStatus),
404 be64_to_cpu(data->phbFirstErrorStatus),
405 be64_to_cpu(data->phbErrorLog0),
406 be64_to_cpu(data->phbErrorLog1));
407 if (data->mmioErrorStatus)
408 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
409 be64_to_cpu(data->mmioErrorStatus),
410 be64_to_cpu(data->mmioFirstErrorStatus),
411 be64_to_cpu(data->mmioErrorLog0),
412 be64_to_cpu(data->mmioErrorLog1));
413 if (data->dma0ErrorStatus)
414 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
415 be64_to_cpu(data->dma0ErrorStatus),
416 be64_to_cpu(data->dma0FirstErrorStatus),
417 be64_to_cpu(data->dma0ErrorLog0),
418 be64_to_cpu(data->dma0ErrorLog1));
419 if (data->dma1ErrorStatus)
420 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
421 be64_to_cpu(data->dma1ErrorStatus),
422 be64_to_cpu(data->dma1FirstErrorStatus),
423 be64_to_cpu(data->dma1ErrorLog0),
424 be64_to_cpu(data->dma1ErrorLog1));
426 pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_PHB3_NUM_PEST_REGS);
429 static void pnv_pci_dump_phb4_diag_data(struct pci_controller *hose,
430 struct OpalIoPhbErrorCommon *common)
432 struct OpalIoPhb4ErrorData *data;
434 data = (struct OpalIoPhb4ErrorData*)common;
435 pr_info("PHB4 PHB#%d Diag-data (Version: %d)\n",
436 hose->global_number, be32_to_cpu(common->version));
438 pr_info("brdgCtl: %08x\n",
439 be32_to_cpu(data->brdgCtl));
440 if (data->deviceStatus || data->slotStatus ||
441 data->linkStatus || data->devCmdStatus ||
443 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
444 be32_to_cpu(data->deviceStatus),
445 be32_to_cpu(data->slotStatus),
446 be32_to_cpu(data->linkStatus),
447 be32_to_cpu(data->devCmdStatus),
448 be32_to_cpu(data->devSecStatus));
449 if (data->rootErrorStatus || data->uncorrErrorStatus ||
450 data->corrErrorStatus)
451 pr_info("RootErrSts: %08x %08x %08x\n",
452 be32_to_cpu(data->rootErrorStatus),
453 be32_to_cpu(data->uncorrErrorStatus),
454 be32_to_cpu(data->corrErrorStatus));
455 if (data->tlpHdr1 || data->tlpHdr2 ||
456 data->tlpHdr3 || data->tlpHdr4)
457 pr_info("RootErrLog: %08x %08x %08x %08x\n",
458 be32_to_cpu(data->tlpHdr1),
459 be32_to_cpu(data->tlpHdr2),
460 be32_to_cpu(data->tlpHdr3),
461 be32_to_cpu(data->tlpHdr4));
463 pr_info("sourceId: %08x\n", be32_to_cpu(data->sourceId));
465 pr_info("nFir: %016llx %016llx %016llx\n",
466 be64_to_cpu(data->nFir),
467 be64_to_cpu(data->nFirMask),
468 be64_to_cpu(data->nFirWOF));
469 if (data->phbPlssr || data->phbCsr)
470 pr_info("PhbSts: %016llx %016llx\n",
471 be64_to_cpu(data->phbPlssr),
472 be64_to_cpu(data->phbCsr));
474 pr_info("Lem: %016llx %016llx %016llx\n",
475 be64_to_cpu(data->lemFir),
476 be64_to_cpu(data->lemErrorMask),
477 be64_to_cpu(data->lemWOF));
478 if (data->phbErrorStatus)
479 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
480 be64_to_cpu(data->phbErrorStatus),
481 be64_to_cpu(data->phbFirstErrorStatus),
482 be64_to_cpu(data->phbErrorLog0),
483 be64_to_cpu(data->phbErrorLog1));
484 if (data->phbTxeErrorStatus)
485 pr_info("PhbTxeErr: %016llx %016llx %016llx %016llx\n",
486 be64_to_cpu(data->phbTxeErrorStatus),
487 be64_to_cpu(data->phbTxeFirstErrorStatus),
488 be64_to_cpu(data->phbTxeErrorLog0),
489 be64_to_cpu(data->phbTxeErrorLog1));
490 if (data->phbRxeArbErrorStatus)
491 pr_info("RxeArbErr: %016llx %016llx %016llx %016llx\n",
492 be64_to_cpu(data->phbRxeArbErrorStatus),
493 be64_to_cpu(data->phbRxeArbFirstErrorStatus),
494 be64_to_cpu(data->phbRxeArbErrorLog0),
495 be64_to_cpu(data->phbRxeArbErrorLog1));
496 if (data->phbRxeMrgErrorStatus)
497 pr_info("RxeMrgErr: %016llx %016llx %016llx %016llx\n",
498 be64_to_cpu(data->phbRxeMrgErrorStatus),
499 be64_to_cpu(data->phbRxeMrgFirstErrorStatus),
500 be64_to_cpu(data->phbRxeMrgErrorLog0),
501 be64_to_cpu(data->phbRxeMrgErrorLog1));
502 if (data->phbRxeTceErrorStatus)
503 pr_info("RxeTceErr: %016llx %016llx %016llx %016llx\n",
504 be64_to_cpu(data->phbRxeTceErrorStatus),
505 be64_to_cpu(data->phbRxeTceFirstErrorStatus),
506 be64_to_cpu(data->phbRxeTceErrorLog0),
507 be64_to_cpu(data->phbRxeTceErrorLog1));
509 if (data->phbPblErrorStatus)
510 pr_info("PblErr: %016llx %016llx %016llx %016llx\n",
511 be64_to_cpu(data->phbPblErrorStatus),
512 be64_to_cpu(data->phbPblFirstErrorStatus),
513 be64_to_cpu(data->phbPblErrorLog0),
514 be64_to_cpu(data->phbPblErrorLog1));
515 if (data->phbPcieDlpErrorStatus)
516 pr_info("PcieDlp: %016llx %016llx %016llx\n",
517 be64_to_cpu(data->phbPcieDlpErrorLog1),
518 be64_to_cpu(data->phbPcieDlpErrorLog2),
519 be64_to_cpu(data->phbPcieDlpErrorStatus));
520 if (data->phbRegbErrorStatus)
521 pr_info("RegbErr: %016llx %016llx %016llx %016llx\n",
522 be64_to_cpu(data->phbRegbErrorStatus),
523 be64_to_cpu(data->phbRegbFirstErrorStatus),
524 be64_to_cpu(data->phbRegbErrorLog0),
525 be64_to_cpu(data->phbRegbErrorLog1));
528 pnv_pci_dump_pest(data->pestA, data->pestB, OPAL_PHB4_NUM_PEST_REGS);
531 void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
532 unsigned char *log_buff)
534 struct OpalIoPhbErrorCommon *common;
536 if (!hose || !log_buff)
539 common = (struct OpalIoPhbErrorCommon *)log_buff;
540 switch (be32_to_cpu(common->ioType)) {
541 case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
542 pnv_pci_dump_p7ioc_diag_data(hose, common);
544 case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
545 pnv_pci_dump_phb3_diag_data(hose, common);
547 case OPAL_PHB_ERROR_DATA_TYPE_PHB4:
548 pnv_pci_dump_phb4_diag_data(hose, common);
551 pr_warn("%s: Unrecognized ioType %d\n",
552 __func__, be32_to_cpu(common->ioType));
556 static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
558 unsigned long flags, rc;
559 int has_diag, ret = 0;
561 spin_lock_irqsave(&phb->lock, flags);
563 /* Fetch PHB diag-data */
564 rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
565 phb->diag_data_size);
566 has_diag = (rc == OPAL_SUCCESS);
568 /* If PHB supports compound PE, to handle it */
569 if (phb->unfreeze_pe) {
570 ret = phb->unfreeze_pe(phb,
572 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
574 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
576 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
578 pr_warn("%s: Failure %ld clearing frozen "
580 __func__, rc, phb->hose->global_number,
587 * For now, let's only display the diag buffer when we fail to clear
588 * the EEH status. We'll do more sensible things later when we have
589 * proper EEH support. We need to make sure we don't pollute ourselves
590 * with the normal errors generated when probing empty slots
593 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
595 spin_unlock_irqrestore(&phb->lock, flags);
598 static void pnv_pci_config_check_eeh(struct pci_dn *pdn)
600 struct pnv_phb *phb = pdn->phb->private_data;
607 * Get the PE#. During the PCI probe stage, we might not
608 * setup that yet. So all ER errors should be mapped to
611 pe_no = pdn->pe_number;
612 if (pe_no == IODA_INVALID_PE) {
613 pe_no = phb->ioda.reserved_pe_idx;
617 * Fetch frozen state. If the PHB support compound PE,
618 * we need handle that case.
620 if (phb->get_pe_state) {
621 fstate = phb->get_pe_state(phb, pe_no);
623 rc = opal_pci_eeh_freeze_status(phb->opal_id,
629 pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
630 __func__, rc, phb->hose->global_number, pe_no);
635 pr_devel(" -> EEH check, bdfn=%04x PE#%x fstate=%x\n",
636 (pdn->busno << 8) | (pdn->devfn), pe_no, fstate);
638 /* Clear the frozen state if applicable */
639 if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
640 fstate == OPAL_EEH_STOPPED_DMA_FREEZE ||
641 fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
643 * If PHB supports compound PE, freeze it for
647 phb->freeze_pe(phb, pe_no);
649 pnv_pci_handle_eeh_config(phb, pe_no);
653 int pnv_pci_cfg_read(struct pci_dn *pdn,
654 int where, int size, u32 *val)
656 struct pnv_phb *phb = pdn->phb->private_data;
657 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
663 rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
664 *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
669 rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
671 *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
676 rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
677 *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
681 return PCIBIOS_FUNC_NOT_SUPPORTED;
684 pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
685 __func__, pdn->busno, pdn->devfn, where, size, *val);
686 return PCIBIOS_SUCCESSFUL;
689 int pnv_pci_cfg_write(struct pci_dn *pdn,
690 int where, int size, u32 val)
692 struct pnv_phb *phb = pdn->phb->private_data;
693 u32 bdfn = (pdn->busno << 8) | pdn->devfn;
695 pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
696 __func__, pdn->busno, pdn->devfn, where, size, val);
699 opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
702 opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
705 opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
708 return PCIBIOS_FUNC_NOT_SUPPORTED;
711 return PCIBIOS_SUCCESSFUL;
715 static bool pnv_pci_cfg_check(struct pci_dn *pdn)
717 struct eeh_dev *edev = NULL;
718 struct pnv_phb *phb = pdn->phb->private_data;
720 /* EEH not enabled ? */
721 if (!(phb->flags & PNV_PHB_FLAG_EEH))
724 /* PE reset or device removed ? */
728 (edev->pe->state & EEH_PE_CFG_BLOCKED))
731 if (edev->mode & EEH_DEV_REMOVED)
738 static inline pnv_pci_cfg_check(struct pci_dn *pdn)
742 #endif /* CONFIG_EEH */
744 static int pnv_pci_read_config(struct pci_bus *bus,
746 int where, int size, u32 *val)
753 pdn = pci_get_pdn_by_devfn(bus, devfn);
755 return PCIBIOS_DEVICE_NOT_FOUND;
757 if (!pnv_pci_cfg_check(pdn))
758 return PCIBIOS_DEVICE_NOT_FOUND;
760 ret = pnv_pci_cfg_read(pdn, where, size, val);
761 phb = pdn->phb->private_data;
762 if (phb->flags & PNV_PHB_FLAG_EEH && pdn->edev) {
763 if (*val == EEH_IO_ERROR_VALUE(size) &&
764 eeh_dev_check_failure(pdn->edev))
765 return PCIBIOS_DEVICE_NOT_FOUND;
767 pnv_pci_config_check_eeh(pdn);
773 static int pnv_pci_write_config(struct pci_bus *bus,
775 int where, int size, u32 val)
781 pdn = pci_get_pdn_by_devfn(bus, devfn);
783 return PCIBIOS_DEVICE_NOT_FOUND;
785 if (!pnv_pci_cfg_check(pdn))
786 return PCIBIOS_DEVICE_NOT_FOUND;
788 ret = pnv_pci_cfg_write(pdn, where, size, val);
789 phb = pdn->phb->private_data;
790 if (!(phb->flags & PNV_PHB_FLAG_EEH))
791 pnv_pci_config_check_eeh(pdn);
796 struct pci_ops pnv_pci_ops = {
797 .read = pnv_pci_read_config,
798 .write = pnv_pci_write_config,
801 static __be64 *pnv_tce(struct iommu_table *tbl, long idx)
803 __be64 *tmp = ((__be64 *)tbl->it_base);
804 int level = tbl->it_indirect_levels;
805 const long shift = ilog2(tbl->it_level_size);
806 unsigned long mask = (tbl->it_level_size - 1) << (level * shift);
809 int n = (idx & mask) >> (level * shift);
810 unsigned long tce = be64_to_cpu(tmp[n]);
812 tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE));
821 int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
822 unsigned long uaddr, enum dma_data_direction direction,
825 u64 proto_tce = iommu_direction_to_tce_perm(direction);
826 u64 rpn = __pa(uaddr) >> tbl->it_page_shift;
829 if (proto_tce & TCE_PCI_WRITE)
830 proto_tce |= TCE_PCI_READ;
832 for (i = 0; i < npages; i++) {
833 unsigned long newtce = proto_tce |
834 ((rpn + i) << tbl->it_page_shift);
835 unsigned long idx = index - tbl->it_offset + i;
837 *(pnv_tce(tbl, idx)) = cpu_to_be64(newtce);
843 #ifdef CONFIG_IOMMU_API
844 int pnv_tce_xchg(struct iommu_table *tbl, long index,
845 unsigned long *hpa, enum dma_data_direction *direction)
847 u64 proto_tce = iommu_direction_to_tce_perm(*direction);
848 unsigned long newtce = *hpa | proto_tce, oldtce;
849 unsigned long idx = index - tbl->it_offset;
851 BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl));
853 if (newtce & TCE_PCI_WRITE)
854 newtce |= TCE_PCI_READ;
856 oldtce = be64_to_cpu(xchg(pnv_tce(tbl, idx), cpu_to_be64(newtce)));
857 *hpa = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
858 *direction = iommu_tce_direction(oldtce);
864 void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
868 for (i = 0; i < npages; i++) {
869 unsigned long idx = index - tbl->it_offset + i;
871 *(pnv_tce(tbl, idx)) = cpu_to_be64(0);
875 unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
877 return be64_to_cpu(*(pnv_tce(tbl, index - tbl->it_offset)));
880 struct iommu_table *pnv_pci_table_alloc(int nid)
882 struct iommu_table *tbl;
884 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
888 INIT_LIST_HEAD_RCU(&tbl->it_group_list);
889 kref_init(&tbl->it_kref);
894 long pnv_pci_link_table_and_group(int node, int num,
895 struct iommu_table *tbl,
896 struct iommu_table_group *table_group)
898 struct iommu_table_group_link *tgl = NULL;
900 if (WARN_ON(!tbl || !table_group))
903 tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
908 tgl->table_group = table_group;
909 list_add_rcu(&tgl->next, &tbl->it_group_list);
911 table_group->tables[num] = tbl;
916 static void pnv_iommu_table_group_link_free(struct rcu_head *head)
918 struct iommu_table_group_link *tgl = container_of(head,
919 struct iommu_table_group_link, rcu);
924 void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
925 struct iommu_table_group *table_group)
929 struct iommu_table_group_link *tgl;
931 if (!tbl || !table_group)
934 /* Remove link to a group from table's list of attached groups */
936 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
937 if (tgl->table_group == table_group) {
938 list_del_rcu(&tgl->next);
939 call_rcu(&tgl->rcu, pnv_iommu_table_group_link_free);
947 /* Clean a pointer to iommu_table in iommu_table_group::tables[] */
949 for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
950 if (table_group->tables[i] == tbl) {
951 table_group->tables[i] = NULL;
959 void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
960 void *tce_mem, u64 tce_size,
961 u64 dma_offset, unsigned page_shift)
963 tbl->it_blocksize = 16;
964 tbl->it_base = (unsigned long)tce_mem;
965 tbl->it_page_shift = page_shift;
966 tbl->it_offset = dma_offset >> tbl->it_page_shift;
968 tbl->it_size = tce_size >> 3;
970 tbl->it_type = TCE_PCI;
973 void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
975 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
976 struct pnv_phb *phb = hose->private_data;
977 #ifdef CONFIG_PCI_IOV
978 struct pnv_ioda_pe *pe;
981 /* Fix the VF pdn PE number */
982 if (pdev->is_virtfn) {
983 pdn = pci_get_pdn(pdev);
984 WARN_ON(pdn->pe_number != IODA_INVALID_PE);
985 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
986 if (pe->rid == ((pdev->bus->number << 8) |
987 (pdev->devfn & 0xff))) {
988 pdn->pe_number = pe->pe_number;
994 #endif /* CONFIG_PCI_IOV */
996 if (phb && phb->dma_dev_setup)
997 phb->dma_dev_setup(phb, pdev);
1000 void pnv_pci_dma_bus_setup(struct pci_bus *bus)
1002 struct pci_controller *hose = bus->sysdata;
1003 struct pnv_phb *phb = hose->private_data;
1004 struct pnv_ioda_pe *pe;
1006 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
1007 if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
1013 if (bus->number == ((pe->rid >> 8) & 0xFF)) {
1020 void pnv_pci_shutdown(void)
1022 struct pci_controller *hose;
1024 list_for_each_entry(hose, &hose_list, list_node)
1025 if (hose->controller_ops.shutdown)
1026 hose->controller_ops.shutdown(hose);
1029 /* Fixup wrong class code in p7ioc and p8 root complex */
1030 static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
1032 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
1034 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
1036 void __init pnv_pci_init(void)
1038 struct device_node *np;
1040 pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
1042 /* If we don't have OPAL, eg. in sim, just skip PCI probe */
1043 if (!firmware_has_feature(FW_FEATURE_OPAL))
1046 /* Look for IODA IO-Hubs. */
1047 for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
1048 pnv_pci_init_ioda_hub(np);
1051 /* Look for ioda2 built-in PHB3's */
1052 for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
1053 pnv_pci_init_ioda2_phb(np);
1055 /* Look for ioda3 built-in PHB4's, we treat them as IODA2 */
1056 for_each_compatible_node(np, NULL, "ibm,ioda3-phb")
1057 pnv_pci_init_ioda2_phb(np);
1059 /* Look for NPU PHBs */
1060 for_each_compatible_node(np, NULL, "ibm,ioda2-npu-phb")
1061 pnv_pci_init_npu_phb(np);
1064 * Look for NPU2 PHBs which we treat mostly as NPU PHBs with
1065 * the exception of TCE kill which requires an OPAL call.
1067 for_each_compatible_node(np, NULL, "ibm,ioda2-npu2-phb")
1068 pnv_pci_init_npu_phb(np);
1070 /* Configure IOMMU DMA hooks */
1071 set_pci_dma_ops(&dma_iommu_ops);
1074 machine_subsys_initcall_sync(powernv, tce_iommu_bus_notifier_init);