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 static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller
*hose
,
231 struct OpalIoPhbErrorCommon
*common
)
233 struct OpalIoP7IOCPhbErrorData
*data
;
236 data
= (struct OpalIoP7IOCPhbErrorData
*)common
;
237 pr_info("P7IOC PHB#%x Diag-data (Version: %d)\n",
238 hose
->global_number
, be32_to_cpu(common
->version
));
241 pr_info("brdgCtl: %08x\n",
242 be32_to_cpu(data
->brdgCtl
));
243 if (data
->portStatusReg
|| data
->rootCmplxStatus
||
244 data
->busAgentStatus
)
245 pr_info("UtlSts: %08x %08x %08x\n",
246 be32_to_cpu(data
->portStatusReg
),
247 be32_to_cpu(data
->rootCmplxStatus
),
248 be32_to_cpu(data
->busAgentStatus
));
249 if (data
->deviceStatus
|| data
->slotStatus
||
250 data
->linkStatus
|| data
->devCmdStatus
||
252 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
253 be32_to_cpu(data
->deviceStatus
),
254 be32_to_cpu(data
->slotStatus
),
255 be32_to_cpu(data
->linkStatus
),
256 be32_to_cpu(data
->devCmdStatus
),
257 be32_to_cpu(data
->devSecStatus
));
258 if (data
->rootErrorStatus
|| data
->uncorrErrorStatus
||
259 data
->corrErrorStatus
)
260 pr_info("RootErrSts: %08x %08x %08x\n",
261 be32_to_cpu(data
->rootErrorStatus
),
262 be32_to_cpu(data
->uncorrErrorStatus
),
263 be32_to_cpu(data
->corrErrorStatus
));
264 if (data
->tlpHdr1
|| data
->tlpHdr2
||
265 data
->tlpHdr3
|| data
->tlpHdr4
)
266 pr_info("RootErrLog: %08x %08x %08x %08x\n",
267 be32_to_cpu(data
->tlpHdr1
),
268 be32_to_cpu(data
->tlpHdr2
),
269 be32_to_cpu(data
->tlpHdr3
),
270 be32_to_cpu(data
->tlpHdr4
));
271 if (data
->sourceId
|| data
->errorClass
||
273 pr_info("RootErrLog1: %08x %016llx %016llx\n",
274 be32_to_cpu(data
->sourceId
),
275 be64_to_cpu(data
->errorClass
),
276 be64_to_cpu(data
->correlator
));
277 if (data
->p7iocPlssr
|| data
->p7iocCsr
)
278 pr_info("PhbSts: %016llx %016llx\n",
279 be64_to_cpu(data
->p7iocPlssr
),
280 be64_to_cpu(data
->p7iocCsr
));
282 pr_info("Lem: %016llx %016llx %016llx\n",
283 be64_to_cpu(data
->lemFir
),
284 be64_to_cpu(data
->lemErrorMask
),
285 be64_to_cpu(data
->lemWOF
));
286 if (data
->phbErrorStatus
)
287 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
288 be64_to_cpu(data
->phbErrorStatus
),
289 be64_to_cpu(data
->phbFirstErrorStatus
),
290 be64_to_cpu(data
->phbErrorLog0
),
291 be64_to_cpu(data
->phbErrorLog1
));
292 if (data
->mmioErrorStatus
)
293 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
294 be64_to_cpu(data
->mmioErrorStatus
),
295 be64_to_cpu(data
->mmioFirstErrorStatus
),
296 be64_to_cpu(data
->mmioErrorLog0
),
297 be64_to_cpu(data
->mmioErrorLog1
));
298 if (data
->dma0ErrorStatus
)
299 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
300 be64_to_cpu(data
->dma0ErrorStatus
),
301 be64_to_cpu(data
->dma0FirstErrorStatus
),
302 be64_to_cpu(data
->dma0ErrorLog0
),
303 be64_to_cpu(data
->dma0ErrorLog1
));
304 if (data
->dma1ErrorStatus
)
305 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
306 be64_to_cpu(data
->dma1ErrorStatus
),
307 be64_to_cpu(data
->dma1FirstErrorStatus
),
308 be64_to_cpu(data
->dma1ErrorLog0
),
309 be64_to_cpu(data
->dma1ErrorLog1
));
311 for (i
= 0; i
< OPAL_P7IOC_NUM_PEST_REGS
; i
++) {
312 if ((be64_to_cpu(data
->pestA
[i
]) >> 63) == 0 &&
313 (be64_to_cpu(data
->pestB
[i
]) >> 63) == 0)
316 pr_info("PE[%3d] A/B: %016llx %016llx\n",
317 i
, be64_to_cpu(data
->pestA
[i
]),
318 be64_to_cpu(data
->pestB
[i
]));
322 static void pnv_pci_dump_phb3_diag_data(struct pci_controller
*hose
,
323 struct OpalIoPhbErrorCommon
*common
)
325 struct OpalIoPhb3ErrorData
*data
;
328 data
= (struct OpalIoPhb3ErrorData
*)common
;
329 pr_info("PHB3 PHB#%x Diag-data (Version: %d)\n",
330 hose
->global_number
, be32_to_cpu(common
->version
));
332 pr_info("brdgCtl: %08x\n",
333 be32_to_cpu(data
->brdgCtl
));
334 if (data
->portStatusReg
|| data
->rootCmplxStatus
||
335 data
->busAgentStatus
)
336 pr_info("UtlSts: %08x %08x %08x\n",
337 be32_to_cpu(data
->portStatusReg
),
338 be32_to_cpu(data
->rootCmplxStatus
),
339 be32_to_cpu(data
->busAgentStatus
));
340 if (data
->deviceStatus
|| data
->slotStatus
||
341 data
->linkStatus
|| data
->devCmdStatus
||
343 pr_info("RootSts: %08x %08x %08x %08x %08x\n",
344 be32_to_cpu(data
->deviceStatus
),
345 be32_to_cpu(data
->slotStatus
),
346 be32_to_cpu(data
->linkStatus
),
347 be32_to_cpu(data
->devCmdStatus
),
348 be32_to_cpu(data
->devSecStatus
));
349 if (data
->rootErrorStatus
|| data
->uncorrErrorStatus
||
350 data
->corrErrorStatus
)
351 pr_info("RootErrSts: %08x %08x %08x\n",
352 be32_to_cpu(data
->rootErrorStatus
),
353 be32_to_cpu(data
->uncorrErrorStatus
),
354 be32_to_cpu(data
->corrErrorStatus
));
355 if (data
->tlpHdr1
|| data
->tlpHdr2
||
356 data
->tlpHdr3
|| data
->tlpHdr4
)
357 pr_info("RootErrLog: %08x %08x %08x %08x\n",
358 be32_to_cpu(data
->tlpHdr1
),
359 be32_to_cpu(data
->tlpHdr2
),
360 be32_to_cpu(data
->tlpHdr3
),
361 be32_to_cpu(data
->tlpHdr4
));
362 if (data
->sourceId
|| data
->errorClass
||
364 pr_info("RootErrLog1: %08x %016llx %016llx\n",
365 be32_to_cpu(data
->sourceId
),
366 be64_to_cpu(data
->errorClass
),
367 be64_to_cpu(data
->correlator
));
369 pr_info("nFir: %016llx %016llx %016llx\n",
370 be64_to_cpu(data
->nFir
),
371 be64_to_cpu(data
->nFirMask
),
372 be64_to_cpu(data
->nFirWOF
));
373 if (data
->phbPlssr
|| data
->phbCsr
)
374 pr_info("PhbSts: %016llx %016llx\n",
375 be64_to_cpu(data
->phbPlssr
),
376 be64_to_cpu(data
->phbCsr
));
378 pr_info("Lem: %016llx %016llx %016llx\n",
379 be64_to_cpu(data
->lemFir
),
380 be64_to_cpu(data
->lemErrorMask
),
381 be64_to_cpu(data
->lemWOF
));
382 if (data
->phbErrorStatus
)
383 pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
384 be64_to_cpu(data
->phbErrorStatus
),
385 be64_to_cpu(data
->phbFirstErrorStatus
),
386 be64_to_cpu(data
->phbErrorLog0
),
387 be64_to_cpu(data
->phbErrorLog1
));
388 if (data
->mmioErrorStatus
)
389 pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
390 be64_to_cpu(data
->mmioErrorStatus
),
391 be64_to_cpu(data
->mmioFirstErrorStatus
),
392 be64_to_cpu(data
->mmioErrorLog0
),
393 be64_to_cpu(data
->mmioErrorLog1
));
394 if (data
->dma0ErrorStatus
)
395 pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
396 be64_to_cpu(data
->dma0ErrorStatus
),
397 be64_to_cpu(data
->dma0FirstErrorStatus
),
398 be64_to_cpu(data
->dma0ErrorLog0
),
399 be64_to_cpu(data
->dma0ErrorLog1
));
400 if (data
->dma1ErrorStatus
)
401 pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
402 be64_to_cpu(data
->dma1ErrorStatus
),
403 be64_to_cpu(data
->dma1FirstErrorStatus
),
404 be64_to_cpu(data
->dma1ErrorLog0
),
405 be64_to_cpu(data
->dma1ErrorLog1
));
407 for (i
= 0; i
< OPAL_PHB3_NUM_PEST_REGS
; i
++) {
408 if ((be64_to_cpu(data
->pestA
[i
]) >> 63) == 0 &&
409 (be64_to_cpu(data
->pestB
[i
]) >> 63) == 0)
412 pr_info("PE[%3d] A/B: %016llx %016llx\n",
413 i
, be64_to_cpu(data
->pestA
[i
]),
414 be64_to_cpu(data
->pestB
[i
]));
418 void pnv_pci_dump_phb_diag_data(struct pci_controller
*hose
,
419 unsigned char *log_buff
)
421 struct OpalIoPhbErrorCommon
*common
;
423 if (!hose
|| !log_buff
)
426 common
= (struct OpalIoPhbErrorCommon
*)log_buff
;
427 switch (be32_to_cpu(common
->ioType
)) {
428 case OPAL_PHB_ERROR_DATA_TYPE_P7IOC
:
429 pnv_pci_dump_p7ioc_diag_data(hose
, common
);
431 case OPAL_PHB_ERROR_DATA_TYPE_PHB3
:
432 pnv_pci_dump_phb3_diag_data(hose
, common
);
435 pr_warn("%s: Unrecognized ioType %d\n",
436 __func__
, be32_to_cpu(common
->ioType
));
440 static void pnv_pci_handle_eeh_config(struct pnv_phb
*phb
, u32 pe_no
)
442 unsigned long flags
, rc
;
443 int has_diag
, ret
= 0;
445 spin_lock_irqsave(&phb
->lock
, flags
);
447 /* Fetch PHB diag-data */
448 rc
= opal_pci_get_phb_diag_data2(phb
->opal_id
, phb
->diag
.blob
,
449 PNV_PCI_DIAG_BUF_SIZE
);
450 has_diag
= (rc
== OPAL_SUCCESS
);
452 /* If PHB supports compound PE, to handle it */
453 if (phb
->unfreeze_pe
) {
454 ret
= phb
->unfreeze_pe(phb
,
456 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL
);
458 rc
= opal_pci_eeh_freeze_clear(phb
->opal_id
,
460 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL
);
462 pr_warn("%s: Failure %ld clearing frozen "
464 __func__
, rc
, phb
->hose
->global_number
,
471 * For now, let's only display the diag buffer when we fail to clear
472 * the EEH status. We'll do more sensible things later when we have
473 * proper EEH support. We need to make sure we don't pollute ourselves
474 * with the normal errors generated when probing empty slots
477 pnv_pci_dump_phb_diag_data(phb
->hose
, phb
->diag
.blob
);
479 spin_unlock_irqrestore(&phb
->lock
, flags
);
482 static void pnv_pci_config_check_eeh(struct pci_dn
*pdn
)
484 struct pnv_phb
*phb
= pdn
->phb
->private_data
;
491 * Get the PE#. During the PCI probe stage, we might not
492 * setup that yet. So all ER errors should be mapped to
495 pe_no
= pdn
->pe_number
;
496 if (pe_no
== IODA_INVALID_PE
) {
497 pe_no
= phb
->ioda
.reserved_pe_idx
;
501 * Fetch frozen state. If the PHB support compound PE,
502 * we need handle that case.
504 if (phb
->get_pe_state
) {
505 fstate
= phb
->get_pe_state(phb
, pe_no
);
507 rc
= opal_pci_eeh_freeze_status(phb
->opal_id
,
513 pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
514 __func__
, rc
, phb
->hose
->global_number
, pe_no
);
519 pr_devel(" -> EEH check, bdfn=%04x PE#%x fstate=%x\n",
520 (pdn
->busno
<< 8) | (pdn
->devfn
), pe_no
, fstate
);
522 /* Clear the frozen state if applicable */
523 if (fstate
== OPAL_EEH_STOPPED_MMIO_FREEZE
||
524 fstate
== OPAL_EEH_STOPPED_DMA_FREEZE
||
525 fstate
== OPAL_EEH_STOPPED_MMIO_DMA_FREEZE
) {
527 * If PHB supports compound PE, freeze it for
531 phb
->freeze_pe(phb
, pe_no
);
533 pnv_pci_handle_eeh_config(phb
, pe_no
);
537 int pnv_pci_cfg_read(struct pci_dn
*pdn
,
538 int where
, int size
, u32
*val
)
540 struct pnv_phb
*phb
= pdn
->phb
->private_data
;
541 u32 bdfn
= (pdn
->busno
<< 8) | pdn
->devfn
;
547 rc
= opal_pci_config_read_byte(phb
->opal_id
, bdfn
, where
, &v8
);
548 *val
= (rc
== OPAL_SUCCESS
) ? v8
: 0xff;
553 rc
= opal_pci_config_read_half_word(phb
->opal_id
, bdfn
, where
,
555 *val
= (rc
== OPAL_SUCCESS
) ? be16_to_cpu(v16
) : 0xffff;
560 rc
= opal_pci_config_read_word(phb
->opal_id
, bdfn
, where
, &v32
);
561 *val
= (rc
== OPAL_SUCCESS
) ? be32_to_cpu(v32
) : 0xffffffff;
565 return PCIBIOS_FUNC_NOT_SUPPORTED
;
568 pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
569 __func__
, pdn
->busno
, pdn
->devfn
, where
, size
, *val
);
570 return PCIBIOS_SUCCESSFUL
;
573 int pnv_pci_cfg_write(struct pci_dn
*pdn
,
574 int where
, int size
, u32 val
)
576 struct pnv_phb
*phb
= pdn
->phb
->private_data
;
577 u32 bdfn
= (pdn
->busno
<< 8) | pdn
->devfn
;
579 pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
580 __func__
, pdn
->busno
, pdn
->devfn
, where
, size
, val
);
583 opal_pci_config_write_byte(phb
->opal_id
, bdfn
, where
, val
);
586 opal_pci_config_write_half_word(phb
->opal_id
, bdfn
, where
, val
);
589 opal_pci_config_write_word(phb
->opal_id
, bdfn
, where
, val
);
592 return PCIBIOS_FUNC_NOT_SUPPORTED
;
595 return PCIBIOS_SUCCESSFUL
;
599 static bool pnv_pci_cfg_check(struct pci_dn
*pdn
)
601 struct eeh_dev
*edev
= NULL
;
602 struct pnv_phb
*phb
= pdn
->phb
->private_data
;
604 /* EEH not enabled ? */
605 if (!(phb
->flags
& PNV_PHB_FLAG_EEH
))
608 /* PE reset or device removed ? */
612 (edev
->pe
->state
& EEH_PE_CFG_BLOCKED
))
615 if (edev
->mode
& EEH_DEV_REMOVED
)
622 static inline pnv_pci_cfg_check(struct pci_dn
*pdn
)
626 #endif /* CONFIG_EEH */
628 static int pnv_pci_read_config(struct pci_bus
*bus
,
630 int where
, int size
, u32
*val
)
637 pdn
= pci_get_pdn_by_devfn(bus
, devfn
);
639 return PCIBIOS_DEVICE_NOT_FOUND
;
641 if (!pnv_pci_cfg_check(pdn
))
642 return PCIBIOS_DEVICE_NOT_FOUND
;
644 ret
= pnv_pci_cfg_read(pdn
, where
, size
, val
);
645 phb
= pdn
->phb
->private_data
;
646 if (phb
->flags
& PNV_PHB_FLAG_EEH
&& pdn
->edev
) {
647 if (*val
== EEH_IO_ERROR_VALUE(size
) &&
648 eeh_dev_check_failure(pdn
->edev
))
649 return PCIBIOS_DEVICE_NOT_FOUND
;
651 pnv_pci_config_check_eeh(pdn
);
657 static int pnv_pci_write_config(struct pci_bus
*bus
,
659 int where
, int size
, u32 val
)
665 pdn
= pci_get_pdn_by_devfn(bus
, devfn
);
667 return PCIBIOS_DEVICE_NOT_FOUND
;
669 if (!pnv_pci_cfg_check(pdn
))
670 return PCIBIOS_DEVICE_NOT_FOUND
;
672 ret
= pnv_pci_cfg_write(pdn
, where
, size
, val
);
673 phb
= pdn
->phb
->private_data
;
674 if (!(phb
->flags
& PNV_PHB_FLAG_EEH
))
675 pnv_pci_config_check_eeh(pdn
);
680 struct pci_ops pnv_pci_ops
= {
681 .read
= pnv_pci_read_config
,
682 .write
= pnv_pci_write_config
,
685 static __be64
*pnv_tce(struct iommu_table
*tbl
, long idx
)
687 __be64
*tmp
= ((__be64
*)tbl
->it_base
);
688 int level
= tbl
->it_indirect_levels
;
689 const long shift
= ilog2(tbl
->it_level_size
);
690 unsigned long mask
= (tbl
->it_level_size
- 1) << (level
* shift
);
693 int n
= (idx
& mask
) >> (level
* shift
);
694 unsigned long tce
= be64_to_cpu(tmp
[n
]);
696 tmp
= __va(tce
& ~(TCE_PCI_READ
| TCE_PCI_WRITE
));
705 int pnv_tce_build(struct iommu_table
*tbl
, long index
, long npages
,
706 unsigned long uaddr
, enum dma_data_direction direction
,
709 u64 proto_tce
= iommu_direction_to_tce_perm(direction
);
710 u64 rpn
= __pa(uaddr
) >> tbl
->it_page_shift
;
713 if (proto_tce
& TCE_PCI_WRITE
)
714 proto_tce
|= TCE_PCI_READ
;
716 for (i
= 0; i
< npages
; i
++) {
717 unsigned long newtce
= proto_tce
|
718 ((rpn
+ i
) << tbl
->it_page_shift
);
719 unsigned long idx
= index
- tbl
->it_offset
+ i
;
721 *(pnv_tce(tbl
, idx
)) = cpu_to_be64(newtce
);
727 #ifdef CONFIG_IOMMU_API
728 int pnv_tce_xchg(struct iommu_table
*tbl
, long index
,
729 unsigned long *hpa
, enum dma_data_direction
*direction
)
731 u64 proto_tce
= iommu_direction_to_tce_perm(*direction
);
732 unsigned long newtce
= *hpa
| proto_tce
, oldtce
;
733 unsigned long idx
= index
- tbl
->it_offset
;
735 BUG_ON(*hpa
& ~IOMMU_PAGE_MASK(tbl
));
737 if (newtce
& TCE_PCI_WRITE
)
738 newtce
|= TCE_PCI_READ
;
740 oldtce
= be64_to_cpu(xchg(pnv_tce(tbl
, idx
), cpu_to_be64(newtce
)));
741 *hpa
= oldtce
& ~(TCE_PCI_READ
| TCE_PCI_WRITE
);
742 *direction
= iommu_tce_direction(oldtce
);
748 void pnv_tce_free(struct iommu_table
*tbl
, long index
, long npages
)
752 for (i
= 0; i
< npages
; i
++) {
753 unsigned long idx
= index
- tbl
->it_offset
+ i
;
755 *(pnv_tce(tbl
, idx
)) = cpu_to_be64(0);
759 unsigned long pnv_tce_get(struct iommu_table
*tbl
, long index
)
761 return *(pnv_tce(tbl
, index
- tbl
->it_offset
));
764 struct iommu_table
*pnv_pci_table_alloc(int nid
)
766 struct iommu_table
*tbl
;
768 tbl
= kzalloc_node(sizeof(struct iommu_table
), GFP_KERNEL
, nid
);
769 INIT_LIST_HEAD_RCU(&tbl
->it_group_list
);
774 long pnv_pci_link_table_and_group(int node
, int num
,
775 struct iommu_table
*tbl
,
776 struct iommu_table_group
*table_group
)
778 struct iommu_table_group_link
*tgl
= NULL
;
780 if (WARN_ON(!tbl
|| !table_group
))
783 tgl
= kzalloc_node(sizeof(struct iommu_table_group_link
), GFP_KERNEL
,
788 tgl
->table_group
= table_group
;
789 list_add_rcu(&tgl
->next
, &tbl
->it_group_list
);
791 table_group
->tables
[num
] = tbl
;
796 static void pnv_iommu_table_group_link_free(struct rcu_head
*head
)
798 struct iommu_table_group_link
*tgl
= container_of(head
,
799 struct iommu_table_group_link
, rcu
);
804 void pnv_pci_unlink_table_and_group(struct iommu_table
*tbl
,
805 struct iommu_table_group
*table_group
)
809 struct iommu_table_group_link
*tgl
;
811 if (!tbl
|| !table_group
)
814 /* Remove link to a group from table's list of attached groups */
816 list_for_each_entry_rcu(tgl
, &tbl
->it_group_list
, next
) {
817 if (tgl
->table_group
== table_group
) {
818 list_del_rcu(&tgl
->next
);
819 call_rcu(&tgl
->rcu
, pnv_iommu_table_group_link_free
);
827 /* Clean a pointer to iommu_table in iommu_table_group::tables[] */
829 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
) {
830 if (table_group
->tables
[i
] == tbl
) {
831 table_group
->tables
[i
] = NULL
;
839 void pnv_pci_setup_iommu_table(struct iommu_table
*tbl
,
840 void *tce_mem
, u64 tce_size
,
841 u64 dma_offset
, unsigned page_shift
)
843 tbl
->it_blocksize
= 16;
844 tbl
->it_base
= (unsigned long)tce_mem
;
845 tbl
->it_page_shift
= page_shift
;
846 tbl
->it_offset
= dma_offset
>> tbl
->it_page_shift
;
848 tbl
->it_size
= tce_size
>> 3;
850 tbl
->it_type
= TCE_PCI
;
853 void pnv_pci_dma_dev_setup(struct pci_dev
*pdev
)
855 struct pci_controller
*hose
= pci_bus_to_host(pdev
->bus
);
856 struct pnv_phb
*phb
= hose
->private_data
;
857 #ifdef CONFIG_PCI_IOV
858 struct pnv_ioda_pe
*pe
;
861 /* Fix the VF pdn PE number */
862 if (pdev
->is_virtfn
) {
863 pdn
= pci_get_pdn(pdev
);
864 WARN_ON(pdn
->pe_number
!= IODA_INVALID_PE
);
865 list_for_each_entry(pe
, &phb
->ioda
.pe_list
, list
) {
866 if (pe
->rid
== ((pdev
->bus
->number
<< 8) |
867 (pdev
->devfn
& 0xff))) {
868 pdn
->pe_number
= pe
->pe_number
;
874 #endif /* CONFIG_PCI_IOV */
876 if (phb
&& phb
->dma_dev_setup
)
877 phb
->dma_dev_setup(phb
, pdev
);
880 void pnv_pci_dma_bus_setup(struct pci_bus
*bus
)
882 struct pci_controller
*hose
= bus
->sysdata
;
883 struct pnv_phb
*phb
= hose
->private_data
;
884 struct pnv_ioda_pe
*pe
;
886 list_for_each_entry(pe
, &phb
->ioda
.pe_list
, list
) {
887 if (!(pe
->flags
& (PNV_IODA_PE_BUS
| PNV_IODA_PE_BUS_ALL
)))
893 if (bus
->number
== ((pe
->rid
>> 8) & 0xFF)) {
900 void pnv_pci_shutdown(void)
902 struct pci_controller
*hose
;
904 list_for_each_entry(hose
, &hose_list
, list_node
)
905 if (hose
->controller_ops
.shutdown
)
906 hose
->controller_ops
.shutdown(hose
);
909 /* Fixup wrong class code in p7ioc and p8 root complex */
910 static void pnv_p7ioc_rc_quirk(struct pci_dev
*dev
)
912 dev
->class = PCI_CLASS_BRIDGE_PCI
<< 8;
914 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM
, 0x3b9, pnv_p7ioc_rc_quirk
);
916 void __init
pnv_pci_init(void)
918 struct device_node
*np
;
920 pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN
);
922 /* If we don't have OPAL, eg. in sim, just skip PCI probe */
923 if (!firmware_has_feature(FW_FEATURE_OPAL
))
926 /* Look for IODA IO-Hubs. */
927 for_each_compatible_node(np
, NULL
, "ibm,ioda-hub") {
928 pnv_pci_init_ioda_hub(np
);
931 /* Look for ioda2 built-in PHB3's */
932 for_each_compatible_node(np
, NULL
, "ibm,ioda2-phb")
933 pnv_pci_init_ioda2_phb(np
);
935 /* Look for ioda3 built-in PHB4's, we treat them as IODA2 */
936 for_each_compatible_node(np
, NULL
, "ibm,ioda3-phb")
937 pnv_pci_init_ioda2_phb(np
);
939 /* Look for NPU PHBs */
940 for_each_compatible_node(np
, NULL
, "ibm,ioda2-npu-phb")
941 pnv_pci_init_npu_phb(np
);
943 /* Configure IOMMU DMA hooks */
944 set_pci_dma_ops(&dma_iommu_ops
);
947 machine_subsys_initcall_sync(powernv
, tce_iommu_bus_notifier_init
);