1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support PCI/PCIe on PowerNV platforms
5 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
10 #include <linux/kernel.h>
11 #include <linux/pci.h>
12 #include <linux/crash_dump.h>
13 #include <linux/delay.h>
14 #include <linux/string.h>
15 #include <linux/init.h>
16 #include <linux/memblock.h>
17 #include <linux/irq.h>
19 #include <linux/msi.h>
20 #include <linux/iommu.h>
21 #include <linux/rculist.h>
22 #include <linux/sizes.h>
24 #include <asm/sections.h>
27 #include <asm/pci-bridge.h>
28 #include <asm/machdep.h>
29 #include <asm/msi_bitmap.h>
30 #include <asm/ppc-pci.h>
32 #include <asm/iommu.h>
35 #include <asm/debugfs.h>
36 #include <asm/firmware.h>
37 #include <asm/pnv-pci.h>
38 #include <asm/mmzone.h>
40 #include <misc/cxl-base.h>
44 #include "../../../../drivers/pci/pci.h"
46 #define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */
47 #define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */
48 #define PNV_IODA1_DMA32_SEGSIZE 0x10000000
50 static const char * const pnv_phb_names
[] = { "IODA1", "IODA2", "NPU_NVLINK",
53 void pe_level_printk(const struct pnv_ioda_pe
*pe
, const char *level
,
65 if (pe
->flags
& PNV_IODA_PE_DEV
)
66 strlcpy(pfix
, dev_name(&pe
->pdev
->dev
), sizeof(pfix
));
67 else if (pe
->flags
& (PNV_IODA_PE_BUS
| PNV_IODA_PE_BUS_ALL
))
68 sprintf(pfix
, "%04x:%02x ",
69 pci_domain_nr(pe
->pbus
), pe
->pbus
->number
);
71 else if (pe
->flags
& PNV_IODA_PE_VF
)
72 sprintf(pfix
, "%04x:%02x:%2x.%d",
73 pci_domain_nr(pe
->parent_dev
->bus
),
74 (pe
->rid
& 0xff00) >> 8,
75 PCI_SLOT(pe
->rid
), PCI_FUNC(pe
->rid
));
76 #endif /* CONFIG_PCI_IOV*/
78 printk("%spci %s: [PE# %.2x] %pV",
79 level
, pfix
, pe
->pe_number
, &vaf
);
84 static bool pnv_iommu_bypass_disabled __read_mostly
;
85 static bool pci_reset_phbs __read_mostly
;
87 static int __init
iommu_setup(char *str
)
93 if (!strncmp(str
, "nobypass", 8)) {
94 pnv_iommu_bypass_disabled
= true;
95 pr_info("PowerNV: IOMMU bypass window disabled.\n");
98 str
+= strcspn(str
, ",");
105 early_param("iommu", iommu_setup
);
107 static int __init
pci_reset_phbs_setup(char *str
)
109 pci_reset_phbs
= true;
113 early_param("ppc_pci_reset_phbs", pci_reset_phbs_setup
);
115 static inline bool pnv_pci_is_m64(struct pnv_phb
*phb
, struct resource
*r
)
118 * WARNING: We cannot rely on the resource flags. The Linux PCI
119 * allocation code sometimes decides to put a 64-bit prefetchable
120 * BAR in the 32-bit window, so we have to compare the addresses.
122 * For simplicity we only test resource start.
124 return (r
->start
>= phb
->ioda
.m64_base
&&
125 r
->start
< (phb
->ioda
.m64_base
+ phb
->ioda
.m64_size
));
128 static inline bool pnv_pci_is_m64_flags(unsigned long resource_flags
)
130 unsigned long flags
= (IORESOURCE_MEM_64
| IORESOURCE_PREFETCH
);
132 return (resource_flags
& flags
) == flags
;
135 static struct pnv_ioda_pe
*pnv_ioda_init_pe(struct pnv_phb
*phb
, int pe_no
)
139 phb
->ioda
.pe_array
[pe_no
].phb
= phb
;
140 phb
->ioda
.pe_array
[pe_no
].pe_number
= pe_no
;
143 * Clear the PE frozen state as it might be put into frozen state
144 * in the last PCI remove path. It's not harmful to do so when the
145 * PE is already in unfrozen state.
147 rc
= opal_pci_eeh_freeze_clear(phb
->opal_id
, pe_no
,
148 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL
);
149 if (rc
!= OPAL_SUCCESS
&& rc
!= OPAL_UNSUPPORTED
)
150 pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n",
151 __func__
, rc
, phb
->hose
->global_number
, pe_no
);
153 return &phb
->ioda
.pe_array
[pe_no
];
156 static void pnv_ioda_reserve_pe(struct pnv_phb
*phb
, int pe_no
)
158 if (!(pe_no
>= 0 && pe_no
< phb
->ioda
.total_pe_num
)) {
159 pr_warn("%s: Invalid PE %x on PHB#%x\n",
160 __func__
, pe_no
, phb
->hose
->global_number
);
164 if (test_and_set_bit(pe_no
, phb
->ioda
.pe_alloc
))
165 pr_debug("%s: PE %x was reserved on PHB#%x\n",
166 __func__
, pe_no
, phb
->hose
->global_number
);
168 pnv_ioda_init_pe(phb
, pe_no
);
171 static struct pnv_ioda_pe
*pnv_ioda_alloc_pe(struct pnv_phb
*phb
)
175 for (pe
= phb
->ioda
.total_pe_num
- 1; pe
>= 0; pe
--) {
176 if (!test_and_set_bit(pe
, phb
->ioda
.pe_alloc
))
177 return pnv_ioda_init_pe(phb
, pe
);
183 static void pnv_ioda_free_pe(struct pnv_ioda_pe
*pe
)
185 struct pnv_phb
*phb
= pe
->phb
;
186 unsigned int pe_num
= pe
->pe_number
;
189 WARN_ON(pe
->npucomp
); /* NPUs are not supposed to be freed */
191 memset(pe
, 0, sizeof(struct pnv_ioda_pe
));
192 clear_bit(pe_num
, phb
->ioda
.pe_alloc
);
195 /* The default M64 BAR is shared by all PEs */
196 static int pnv_ioda2_init_m64(struct pnv_phb
*phb
)
202 /* Configure the default M64 BAR */
203 rc
= opal_pci_set_phb_mem_window(phb
->opal_id
,
204 OPAL_M64_WINDOW_TYPE
,
205 phb
->ioda
.m64_bar_idx
,
209 if (rc
!= OPAL_SUCCESS
) {
210 desc
= "configuring";
214 /* Enable the default M64 BAR */
215 rc
= opal_pci_phb_mmio_enable(phb
->opal_id
,
216 OPAL_M64_WINDOW_TYPE
,
217 phb
->ioda
.m64_bar_idx
,
218 OPAL_ENABLE_M64_SPLIT
);
219 if (rc
!= OPAL_SUCCESS
) {
225 * Exclude the segments for reserved and root bus PE, which
226 * are first or last two PEs.
228 r
= &phb
->hose
->mem_resources
[1];
229 if (phb
->ioda
.reserved_pe_idx
== 0)
230 r
->start
+= (2 * phb
->ioda
.m64_segsize
);
231 else if (phb
->ioda
.reserved_pe_idx
== (phb
->ioda
.total_pe_num
- 1))
232 r
->end
-= (2 * phb
->ioda
.m64_segsize
);
234 pr_warn(" Cannot strip M64 segment for reserved PE#%x\n",
235 phb
->ioda
.reserved_pe_idx
);
240 pr_warn(" Failure %lld %s M64 BAR#%d\n",
241 rc
, desc
, phb
->ioda
.m64_bar_idx
);
242 opal_pci_phb_mmio_enable(phb
->opal_id
,
243 OPAL_M64_WINDOW_TYPE
,
244 phb
->ioda
.m64_bar_idx
,
249 static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev
*pdev
,
250 unsigned long *pe_bitmap
)
252 struct pci_controller
*hose
= pci_bus_to_host(pdev
->bus
);
253 struct pnv_phb
*phb
= hose
->private_data
;
255 resource_size_t base
, sgsz
, start
, end
;
258 base
= phb
->ioda
.m64_base
;
259 sgsz
= phb
->ioda
.m64_segsize
;
260 for (i
= 0; i
<= PCI_ROM_RESOURCE
; i
++) {
261 r
= &pdev
->resource
[i
];
262 if (!r
->parent
|| !pnv_pci_is_m64(phb
, r
))
265 start
= _ALIGN_DOWN(r
->start
- base
, sgsz
);
266 end
= _ALIGN_UP(r
->end
- base
, sgsz
);
267 for (segno
= start
/ sgsz
; segno
< end
/ sgsz
; segno
++) {
269 set_bit(segno
, pe_bitmap
);
271 pnv_ioda_reserve_pe(phb
, segno
);
276 static int pnv_ioda1_init_m64(struct pnv_phb
*phb
)
282 * There are 16 M64 BARs, each of which has 8 segments. So
283 * there are as many M64 segments as the maximum number of
286 for (index
= 0; index
< PNV_IODA1_M64_NUM
; index
++) {
287 unsigned long base
, segsz
= phb
->ioda
.m64_segsize
;
290 base
= phb
->ioda
.m64_base
+
291 index
* PNV_IODA1_M64_SEGS
* segsz
;
292 rc
= opal_pci_set_phb_mem_window(phb
->opal_id
,
293 OPAL_M64_WINDOW_TYPE
, index
, base
, 0,
294 PNV_IODA1_M64_SEGS
* segsz
);
295 if (rc
!= OPAL_SUCCESS
) {
296 pr_warn(" Error %lld setting M64 PHB#%x-BAR#%d\n",
297 rc
, phb
->hose
->global_number
, index
);
301 rc
= opal_pci_phb_mmio_enable(phb
->opal_id
,
302 OPAL_M64_WINDOW_TYPE
, index
,
303 OPAL_ENABLE_M64_SPLIT
);
304 if (rc
!= OPAL_SUCCESS
) {
305 pr_warn(" Error %lld enabling M64 PHB#%x-BAR#%d\n",
306 rc
, phb
->hose
->global_number
, index
);
312 * Exclude the segments for reserved and root bus PE, which
313 * are first or last two PEs.
315 r
= &phb
->hose
->mem_resources
[1];
316 if (phb
->ioda
.reserved_pe_idx
== 0)
317 r
->start
+= (2 * phb
->ioda
.m64_segsize
);
318 else if (phb
->ioda
.reserved_pe_idx
== (phb
->ioda
.total_pe_num
- 1))
319 r
->end
-= (2 * phb
->ioda
.m64_segsize
);
321 WARN(1, "Wrong reserved PE#%x on PHB#%x\n",
322 phb
->ioda
.reserved_pe_idx
, phb
->hose
->global_number
);
327 for ( ; index
>= 0; index
--)
328 opal_pci_phb_mmio_enable(phb
->opal_id
,
329 OPAL_M64_WINDOW_TYPE
, index
, OPAL_DISABLE_M64
);
334 static void pnv_ioda_reserve_m64_pe(struct pci_bus
*bus
,
335 unsigned long *pe_bitmap
,
338 struct pci_dev
*pdev
;
340 list_for_each_entry(pdev
, &bus
->devices
, bus_list
) {
341 pnv_ioda_reserve_dev_m64_pe(pdev
, pe_bitmap
);
343 if (all
&& pdev
->subordinate
)
344 pnv_ioda_reserve_m64_pe(pdev
->subordinate
,
349 static struct pnv_ioda_pe
*pnv_ioda_pick_m64_pe(struct pci_bus
*bus
, bool all
)
351 struct pci_controller
*hose
= pci_bus_to_host(bus
);
352 struct pnv_phb
*phb
= hose
->private_data
;
353 struct pnv_ioda_pe
*master_pe
, *pe
;
354 unsigned long size
, *pe_alloc
;
357 /* Root bus shouldn't use M64 */
358 if (pci_is_root_bus(bus
))
361 /* Allocate bitmap */
362 size
= _ALIGN_UP(phb
->ioda
.total_pe_num
/ 8, sizeof(unsigned long));
363 pe_alloc
= kzalloc(size
, GFP_KERNEL
);
365 pr_warn("%s: Out of memory !\n",
370 /* Figure out reserved PE numbers by the PE */
371 pnv_ioda_reserve_m64_pe(bus
, pe_alloc
, all
);
374 * the current bus might not own M64 window and that's all
375 * contributed by its child buses. For the case, we needn't
376 * pick M64 dependent PE#.
378 if (bitmap_empty(pe_alloc
, phb
->ioda
.total_pe_num
)) {
384 * Figure out the master PE and put all slave PEs to master
385 * PE's list to form compound PE.
389 while ((i
= find_next_bit(pe_alloc
, phb
->ioda
.total_pe_num
, i
+ 1)) <
390 phb
->ioda
.total_pe_num
) {
391 pe
= &phb
->ioda
.pe_array
[i
];
393 phb
->ioda
.m64_segmap
[pe
->pe_number
] = pe
->pe_number
;
395 pe
->flags
|= PNV_IODA_PE_MASTER
;
396 INIT_LIST_HEAD(&pe
->slaves
);
399 pe
->flags
|= PNV_IODA_PE_SLAVE
;
400 pe
->master
= master_pe
;
401 list_add_tail(&pe
->list
, &master_pe
->slaves
);
405 * P7IOC supports M64DT, which helps mapping M64 segment
406 * to one particular PE#. However, PHB3 has fixed mapping
407 * between M64 segment and PE#. In order to have same logic
408 * for P7IOC and PHB3, we enforce fixed mapping between M64
409 * segment and PE# on P7IOC.
411 if (phb
->type
== PNV_PHB_IODA1
) {
414 rc
= opal_pci_map_pe_mmio_window(phb
->opal_id
,
415 pe
->pe_number
, OPAL_M64_WINDOW_TYPE
,
416 pe
->pe_number
/ PNV_IODA1_M64_SEGS
,
417 pe
->pe_number
% PNV_IODA1_M64_SEGS
);
418 if (rc
!= OPAL_SUCCESS
)
419 pr_warn("%s: Error %lld mapping M64 for PHB#%x-PE#%x\n",
420 __func__
, rc
, phb
->hose
->global_number
,
429 static void __init
pnv_ioda_parse_m64_window(struct pnv_phb
*phb
)
431 struct pci_controller
*hose
= phb
->hose
;
432 struct device_node
*dn
= hose
->dn
;
433 struct resource
*res
;
438 if (phb
->type
!= PNV_PHB_IODA1
&& phb
->type
!= PNV_PHB_IODA2
) {
439 pr_info(" Not support M64 window\n");
443 if (!firmware_has_feature(FW_FEATURE_OPAL
)) {
444 pr_info(" Firmware too old to support M64 window\n");
448 r
= of_get_property(dn
, "ibm,opal-m64-window", NULL
);
450 pr_info(" No <ibm,opal-m64-window> on %pOF\n",
456 * Find the available M64 BAR range and pickup the last one for
457 * covering the whole 64-bits space. We support only one range.
459 if (of_property_read_u32_array(dn
, "ibm,opal-available-m64-ranges",
461 /* In absence of the property, assume 0..15 */
465 /* We only support 64 bits in our allocator */
466 if (m64_range
[1] > 63) {
467 pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",
468 __func__
, m64_range
[1], phb
->hose
->global_number
);
471 /* Empty range, no m64 */
472 if (m64_range
[1] <= m64_range
[0]) {
473 pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",
474 __func__
, phb
->hose
->global_number
);
478 /* Configure M64 informations */
479 res
= &hose
->mem_resources
[1];
480 res
->name
= dn
->full_name
;
481 res
->start
= of_translate_address(dn
, r
+ 2);
482 res
->end
= res
->start
+ of_read_number(r
+ 4, 2) - 1;
483 res
->flags
= (IORESOURCE_MEM
| IORESOURCE_MEM_64
| IORESOURCE_PREFETCH
);
484 pci_addr
= of_read_number(r
, 2);
485 hose
->mem_offset
[1] = res
->start
- pci_addr
;
487 phb
->ioda
.m64_size
= resource_size(res
);
488 phb
->ioda
.m64_segsize
= phb
->ioda
.m64_size
/ phb
->ioda
.total_pe_num
;
489 phb
->ioda
.m64_base
= pci_addr
;
491 /* This lines up nicely with the display from processing OF ranges */
492 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",
493 res
->start
, res
->end
, pci_addr
, m64_range
[0],
494 m64_range
[0] + m64_range
[1] - 1);
496 /* Mark all M64 used up by default */
497 phb
->ioda
.m64_bar_alloc
= (unsigned long)-1;
499 /* Use last M64 BAR to cover M64 window */
501 phb
->ioda
.m64_bar_idx
= m64_range
[0] + m64_range
[1];
503 pr_info(" Using M64 #%d as default window\n", phb
->ioda
.m64_bar_idx
);
505 /* Mark remaining ones free */
506 for (i
= m64_range
[0]; i
< m64_range
[1]; i
++)
507 clear_bit(i
, &phb
->ioda
.m64_bar_alloc
);
510 * Setup init functions for M64 based on IODA version, IODA3 uses
513 if (phb
->type
== PNV_PHB_IODA1
)
514 phb
->init_m64
= pnv_ioda1_init_m64
;
516 phb
->init_m64
= pnv_ioda2_init_m64
;
519 static void pnv_ioda_freeze_pe(struct pnv_phb
*phb
, int pe_no
)
521 struct pnv_ioda_pe
*pe
= &phb
->ioda
.pe_array
[pe_no
];
522 struct pnv_ioda_pe
*slave
;
525 /* Fetch master PE */
526 if (pe
->flags
& PNV_IODA_PE_SLAVE
) {
528 if (WARN_ON(!pe
|| !(pe
->flags
& PNV_IODA_PE_MASTER
)))
531 pe_no
= pe
->pe_number
;
534 /* Freeze master PE */
535 rc
= opal_pci_eeh_freeze_set(phb
->opal_id
,
537 OPAL_EEH_ACTION_SET_FREEZE_ALL
);
538 if (rc
!= OPAL_SUCCESS
) {
539 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
540 __func__
, rc
, phb
->hose
->global_number
, pe_no
);
544 /* Freeze slave PEs */
545 if (!(pe
->flags
& PNV_IODA_PE_MASTER
))
548 list_for_each_entry(slave
, &pe
->slaves
, list
) {
549 rc
= opal_pci_eeh_freeze_set(phb
->opal_id
,
551 OPAL_EEH_ACTION_SET_FREEZE_ALL
);
552 if (rc
!= OPAL_SUCCESS
)
553 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
554 __func__
, rc
, phb
->hose
->global_number
,
559 static int pnv_ioda_unfreeze_pe(struct pnv_phb
*phb
, int pe_no
, int opt
)
561 struct pnv_ioda_pe
*pe
, *slave
;
565 pe
= &phb
->ioda
.pe_array
[pe_no
];
566 if (pe
->flags
& PNV_IODA_PE_SLAVE
) {
568 WARN_ON(!pe
|| !(pe
->flags
& PNV_IODA_PE_MASTER
));
569 pe_no
= pe
->pe_number
;
572 /* Clear frozen state for master PE */
573 rc
= opal_pci_eeh_freeze_clear(phb
->opal_id
, pe_no
, opt
);
574 if (rc
!= OPAL_SUCCESS
) {
575 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
576 __func__
, rc
, opt
, phb
->hose
->global_number
, pe_no
);
580 if (!(pe
->flags
& PNV_IODA_PE_MASTER
))
583 /* Clear frozen state for slave PEs */
584 list_for_each_entry(slave
, &pe
->slaves
, list
) {
585 rc
= opal_pci_eeh_freeze_clear(phb
->opal_id
,
588 if (rc
!= OPAL_SUCCESS
) {
589 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
590 __func__
, rc
, opt
, phb
->hose
->global_number
,
599 static int pnv_ioda_get_pe_state(struct pnv_phb
*phb
, int pe_no
)
601 struct pnv_ioda_pe
*slave
, *pe
;
602 u8 fstate
= 0, state
;
606 /* Sanity check on PE number */
607 if (pe_no
< 0 || pe_no
>= phb
->ioda
.total_pe_num
)
608 return OPAL_EEH_STOPPED_PERM_UNAVAIL
;
611 * Fetch the master PE and the PE instance might be
612 * not initialized yet.
614 pe
= &phb
->ioda
.pe_array
[pe_no
];
615 if (pe
->flags
& PNV_IODA_PE_SLAVE
) {
617 WARN_ON(!pe
|| !(pe
->flags
& PNV_IODA_PE_MASTER
));
618 pe_no
= pe
->pe_number
;
621 /* Check the master PE */
622 rc
= opal_pci_eeh_freeze_status(phb
->opal_id
, pe_no
,
623 &state
, &pcierr
, NULL
);
624 if (rc
!= OPAL_SUCCESS
) {
625 pr_warn("%s: Failure %lld getting "
626 "PHB#%x-PE#%x state\n",
628 phb
->hose
->global_number
, pe_no
);
629 return OPAL_EEH_STOPPED_TEMP_UNAVAIL
;
632 /* Check the slave PE */
633 if (!(pe
->flags
& PNV_IODA_PE_MASTER
))
636 list_for_each_entry(slave
, &pe
->slaves
, list
) {
637 rc
= opal_pci_eeh_freeze_status(phb
->opal_id
,
642 if (rc
!= OPAL_SUCCESS
) {
643 pr_warn("%s: Failure %lld getting "
644 "PHB#%x-PE#%x state\n",
646 phb
->hose
->global_number
, slave
->pe_number
);
647 return OPAL_EEH_STOPPED_TEMP_UNAVAIL
;
651 * Override the result based on the ascending
661 struct pnv_ioda_pe
*pnv_ioda_get_pe(struct pci_dev
*dev
)
663 struct pci_controller
*hose
= pci_bus_to_host(dev
->bus
);
664 struct pnv_phb
*phb
= hose
->private_data
;
665 struct pci_dn
*pdn
= pci_get_pdn(dev
);
669 if (pdn
->pe_number
== IODA_INVALID_PE
)
671 return &phb
->ioda
.pe_array
[pdn
->pe_number
];
674 static int pnv_ioda_set_one_peltv(struct pnv_phb
*phb
,
675 struct pnv_ioda_pe
*parent
,
676 struct pnv_ioda_pe
*child
,
679 const char *desc
= is_add
? "adding" : "removing";
680 uint8_t op
= is_add
? OPAL_ADD_PE_TO_DOMAIN
:
681 OPAL_REMOVE_PE_FROM_DOMAIN
;
682 struct pnv_ioda_pe
*slave
;
685 /* Parent PE affects child PE */
686 rc
= opal_pci_set_peltv(phb
->opal_id
, parent
->pe_number
,
687 child
->pe_number
, op
);
688 if (rc
!= OPAL_SUCCESS
) {
689 pe_warn(child
, "OPAL error %ld %s to parent PELTV\n",
694 if (!(child
->flags
& PNV_IODA_PE_MASTER
))
697 /* Compound case: parent PE affects slave PEs */
698 list_for_each_entry(slave
, &child
->slaves
, list
) {
699 rc
= opal_pci_set_peltv(phb
->opal_id
, parent
->pe_number
,
700 slave
->pe_number
, op
);
701 if (rc
!= OPAL_SUCCESS
) {
702 pe_warn(slave
, "OPAL error %ld %s to parent PELTV\n",
711 static int pnv_ioda_set_peltv(struct pnv_phb
*phb
,
712 struct pnv_ioda_pe
*pe
,
715 struct pnv_ioda_pe
*slave
;
716 struct pci_dev
*pdev
= NULL
;
720 * Clear PE frozen state. If it's master PE, we need
721 * clear slave PE frozen state as well.
724 opal_pci_eeh_freeze_clear(phb
->opal_id
, pe
->pe_number
,
725 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL
);
726 if (pe
->flags
& PNV_IODA_PE_MASTER
) {
727 list_for_each_entry(slave
, &pe
->slaves
, list
)
728 opal_pci_eeh_freeze_clear(phb
->opal_id
,
730 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL
);
735 * Associate PE in PELT. We need add the PE into the
736 * corresponding PELT-V as well. Otherwise, the error
737 * originated from the PE might contribute to other
740 ret
= pnv_ioda_set_one_peltv(phb
, pe
, pe
, is_add
);
744 /* For compound PEs, any one affects all of them */
745 if (pe
->flags
& PNV_IODA_PE_MASTER
) {
746 list_for_each_entry(slave
, &pe
->slaves
, list
) {
747 ret
= pnv_ioda_set_one_peltv(phb
, slave
, pe
, is_add
);
753 if (pe
->flags
& (PNV_IODA_PE_BUS_ALL
| PNV_IODA_PE_BUS
))
754 pdev
= pe
->pbus
->self
;
755 else if (pe
->flags
& PNV_IODA_PE_DEV
)
756 pdev
= pe
->pdev
->bus
->self
;
757 #ifdef CONFIG_PCI_IOV
758 else if (pe
->flags
& PNV_IODA_PE_VF
)
759 pdev
= pe
->parent_dev
;
760 #endif /* CONFIG_PCI_IOV */
762 struct pci_dn
*pdn
= pci_get_pdn(pdev
);
763 struct pnv_ioda_pe
*parent
;
765 if (pdn
&& pdn
->pe_number
!= IODA_INVALID_PE
) {
766 parent
= &phb
->ioda
.pe_array
[pdn
->pe_number
];
767 ret
= pnv_ioda_set_one_peltv(phb
, parent
, pe
, is_add
);
772 pdev
= pdev
->bus
->self
;
778 static int pnv_ioda_deconfigure_pe(struct pnv_phb
*phb
, struct pnv_ioda_pe
*pe
)
780 struct pci_dev
*parent
;
781 uint8_t bcomp
, dcomp
, fcomp
;
785 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
789 dcomp
= OPAL_IGNORE_RID_DEVICE_NUMBER
;
790 fcomp
= OPAL_IGNORE_RID_FUNCTION_NUMBER
;
791 parent
= pe
->pbus
->self
;
792 if (pe
->flags
& PNV_IODA_PE_BUS_ALL
)
793 count
= pe
->pbus
->busn_res
.end
- pe
->pbus
->busn_res
.start
+ 1;
798 case 1: bcomp
= OpalPciBusAll
; break;
799 case 2: bcomp
= OpalPciBus7Bits
; break;
800 case 4: bcomp
= OpalPciBus6Bits
; break;
801 case 8: bcomp
= OpalPciBus5Bits
; break;
802 case 16: bcomp
= OpalPciBus4Bits
; break;
803 case 32: bcomp
= OpalPciBus3Bits
; break;
805 dev_err(&pe
->pbus
->dev
, "Number of subordinate buses %d unsupported\n",
807 /* Do an exact match only */
808 bcomp
= OpalPciBusAll
;
810 rid_end
= pe
->rid
+ (count
<< 8);
812 #ifdef CONFIG_PCI_IOV
813 if (pe
->flags
& PNV_IODA_PE_VF
)
814 parent
= pe
->parent_dev
;
817 parent
= pe
->pdev
->bus
->self
;
818 bcomp
= OpalPciBusAll
;
819 dcomp
= OPAL_COMPARE_RID_DEVICE_NUMBER
;
820 fcomp
= OPAL_COMPARE_RID_FUNCTION_NUMBER
;
821 rid_end
= pe
->rid
+ 1;
824 /* Clear the reverse map */
825 for (rid
= pe
->rid
; rid
< rid_end
; rid
++)
826 phb
->ioda
.pe_rmap
[rid
] = IODA_INVALID_PE
;
828 /* Release from all parents PELT-V */
830 struct pci_dn
*pdn
= pci_get_pdn(parent
);
831 if (pdn
&& pdn
->pe_number
!= IODA_INVALID_PE
) {
832 rc
= opal_pci_set_peltv(phb
->opal_id
, pdn
->pe_number
,
833 pe
->pe_number
, OPAL_REMOVE_PE_FROM_DOMAIN
);
834 /* XXX What to do in case of error ? */
836 parent
= parent
->bus
->self
;
839 opal_pci_eeh_freeze_clear(phb
->opal_id
, pe
->pe_number
,
840 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL
);
842 /* Disassociate PE in PELT */
843 rc
= opal_pci_set_peltv(phb
->opal_id
, pe
->pe_number
,
844 pe
->pe_number
, OPAL_REMOVE_PE_FROM_DOMAIN
);
846 pe_warn(pe
, "OPAL error %lld remove self from PELTV\n", rc
);
847 rc
= opal_pci_set_pe(phb
->opal_id
, pe
->pe_number
, pe
->rid
,
848 bcomp
, dcomp
, fcomp
, OPAL_UNMAP_PE
);
850 pe_err(pe
, "OPAL error %lld trying to setup PELT table\n", rc
);
854 #ifdef CONFIG_PCI_IOV
855 pe
->parent_dev
= NULL
;
861 static int pnv_ioda_configure_pe(struct pnv_phb
*phb
, struct pnv_ioda_pe
*pe
)
863 struct pci_dev
*parent
;
864 uint8_t bcomp
, dcomp
, fcomp
;
865 long rc
, rid_end
, rid
;
867 /* Bus validation ? */
871 dcomp
= OPAL_IGNORE_RID_DEVICE_NUMBER
;
872 fcomp
= OPAL_IGNORE_RID_FUNCTION_NUMBER
;
873 parent
= pe
->pbus
->self
;
874 if (pe
->flags
& PNV_IODA_PE_BUS_ALL
)
875 count
= pe
->pbus
->busn_res
.end
- pe
->pbus
->busn_res
.start
+ 1;
880 case 1: bcomp
= OpalPciBusAll
; break;
881 case 2: bcomp
= OpalPciBus7Bits
; break;
882 case 4: bcomp
= OpalPciBus6Bits
; break;
883 case 8: bcomp
= OpalPciBus5Bits
; break;
884 case 16: bcomp
= OpalPciBus4Bits
; break;
885 case 32: bcomp
= OpalPciBus3Bits
; break;
887 dev_err(&pe
->pbus
->dev
, "Number of subordinate buses %d unsupported\n",
889 /* Do an exact match only */
890 bcomp
= OpalPciBusAll
;
892 rid_end
= pe
->rid
+ (count
<< 8);
894 #ifdef CONFIG_PCI_IOV
895 if (pe
->flags
& PNV_IODA_PE_VF
)
896 parent
= pe
->parent_dev
;
898 #endif /* CONFIG_PCI_IOV */
899 parent
= pe
->pdev
->bus
->self
;
900 bcomp
= OpalPciBusAll
;
901 dcomp
= OPAL_COMPARE_RID_DEVICE_NUMBER
;
902 fcomp
= OPAL_COMPARE_RID_FUNCTION_NUMBER
;
903 rid_end
= pe
->rid
+ 1;
907 * Associate PE in PELT. We need add the PE into the
908 * corresponding PELT-V as well. Otherwise, the error
909 * originated from the PE might contribute to other
912 rc
= opal_pci_set_pe(phb
->opal_id
, pe
->pe_number
, pe
->rid
,
913 bcomp
, dcomp
, fcomp
, OPAL_MAP_PE
);
915 pe_err(pe
, "OPAL error %ld trying to setup PELT table\n", rc
);
920 * Configure PELTV. NPUs don't have a PELTV table so skip
921 * configuration on them.
923 if (phb
->type
!= PNV_PHB_NPU_NVLINK
&& phb
->type
!= PNV_PHB_NPU_OCAPI
)
924 pnv_ioda_set_peltv(phb
, pe
, true);
926 /* Setup reverse map */
927 for (rid
= pe
->rid
; rid
< rid_end
; rid
++)
928 phb
->ioda
.pe_rmap
[rid
] = pe
->pe_number
;
930 /* Setup one MVTs on IODA1 */
931 if (phb
->type
!= PNV_PHB_IODA1
) {
936 pe
->mve_number
= pe
->pe_number
;
937 rc
= opal_pci_set_mve(phb
->opal_id
, pe
->mve_number
, pe
->pe_number
);
938 if (rc
!= OPAL_SUCCESS
) {
939 pe_err(pe
, "OPAL error %ld setting up MVE %x\n",
943 rc
= opal_pci_set_mve_enable(phb
->opal_id
,
944 pe
->mve_number
, OPAL_ENABLE_MVE
);
946 pe_err(pe
, "OPAL error %ld enabling MVE %x\n",
956 #ifdef CONFIG_PCI_IOV
957 static int pnv_pci_vf_resource_shift(struct pci_dev
*dev
, int offset
)
959 struct pci_dn
*pdn
= pci_get_pdn(dev
);
961 struct resource
*res
, res2
;
962 resource_size_t size
;
969 * "offset" is in VFs. The M64 windows are sized so that when they
970 * are segmented, each segment is the same size as the IOV BAR.
971 * Each segment is in a separate PE, and the high order bits of the
972 * address are the PE number. Therefore, each VF's BAR is in a
973 * separate PE, and changing the IOV BAR start address changes the
974 * range of PEs the VFs are in.
976 num_vfs
= pdn
->num_vfs
;
977 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
978 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
979 if (!res
->flags
|| !res
->parent
)
983 * The actual IOV BAR range is determined by the start address
984 * and the actual size for num_vfs VFs BAR. This check is to
985 * make sure that after shifting, the range will not overlap
986 * with another device.
988 size
= pci_iov_resource_size(dev
, i
+ PCI_IOV_RESOURCES
);
989 res2
.flags
= res
->flags
;
990 res2
.start
= res
->start
+ (size
* offset
);
991 res2
.end
= res2
.start
+ (size
* num_vfs
) - 1;
993 if (res2
.end
> res
->end
) {
994 dev_err(&dev
->dev
, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
995 i
, &res2
, res
, num_vfs
, offset
);
1001 * Since M64 BAR shares segments among all possible 256 PEs,
1002 * we have to shift the beginning of PF IOV BAR to make it start from
1003 * the segment which belongs to the PE number assigned to the first VF.
1004 * This creates a "hole" in the /proc/iomem which could be used for
1005 * allocating other resources so we reserve this area below and
1006 * release when IOV is released.
1008 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
1009 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
1010 if (!res
->flags
|| !res
->parent
)
1013 size
= pci_iov_resource_size(dev
, i
+ PCI_IOV_RESOURCES
);
1015 res
->start
+= size
* offset
;
1017 dev_info(&dev
->dev
, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
1018 i
, &res2
, res
, (offset
> 0) ? "En" : "Dis",
1022 devm_release_resource(&dev
->dev
, &pdn
->holes
[i
]);
1023 memset(&pdn
->holes
[i
], 0, sizeof(pdn
->holes
[i
]));
1026 pci_update_resource(dev
, i
+ PCI_IOV_RESOURCES
);
1029 pdn
->holes
[i
].start
= res2
.start
;
1030 pdn
->holes
[i
].end
= res2
.start
+ size
* offset
- 1;
1031 pdn
->holes
[i
].flags
= IORESOURCE_BUS
;
1032 pdn
->holes
[i
].name
= "pnv_iov_reserved";
1033 devm_request_resource(&dev
->dev
, res
->parent
,
1039 #endif /* CONFIG_PCI_IOV */
1041 static struct pnv_ioda_pe
*pnv_ioda_setup_dev_PE(struct pci_dev
*dev
)
1043 struct pci_controller
*hose
= pci_bus_to_host(dev
->bus
);
1044 struct pnv_phb
*phb
= hose
->private_data
;
1045 struct pci_dn
*pdn
= pci_get_pdn(dev
);
1046 struct pnv_ioda_pe
*pe
;
1049 pr_err("%s: Device tree node not associated properly\n",
1053 if (pdn
->pe_number
!= IODA_INVALID_PE
)
1056 pe
= pnv_ioda_alloc_pe(phb
);
1058 pr_warn("%s: Not enough PE# available, disabling device\n",
1063 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
1064 * pointer in the PE data structure, both should be destroyed at the
1065 * same time. However, this needs to be looked at more closely again
1066 * once we actually start removing things (Hotplug, SR-IOV, ...)
1068 * At some point we want to remove the PDN completely anyways
1071 pdn
->pe_number
= pe
->pe_number
;
1072 pe
->flags
= PNV_IODA_PE_DEV
;
1075 pe
->mve_number
= -1;
1076 pe
->rid
= dev
->bus
->number
<< 8 | pdn
->devfn
;
1078 pe_info(pe
, "Associated device to PE\n");
1080 if (pnv_ioda_configure_pe(phb
, pe
)) {
1081 /* XXX What do we do here ? */
1082 pnv_ioda_free_pe(pe
);
1083 pdn
->pe_number
= IODA_INVALID_PE
;
1089 /* Put PE to the list */
1090 list_add_tail(&pe
->list
, &phb
->ioda
.pe_list
);
1095 static void pnv_ioda_setup_same_PE(struct pci_bus
*bus
, struct pnv_ioda_pe
*pe
)
1097 struct pci_dev
*dev
;
1099 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1100 struct pci_dn
*pdn
= pci_get_pdn(dev
);
1103 pr_warn("%s: No device node associated with device !\n",
1109 * In partial hotplug case, the PCI device might be still
1110 * associated with the PE and needn't attach it to the PE
1113 if (pdn
->pe_number
!= IODA_INVALID_PE
)
1117 pdn
->pe_number
= pe
->pe_number
;
1118 if ((pe
->flags
& PNV_IODA_PE_BUS_ALL
) && dev
->subordinate
)
1119 pnv_ioda_setup_same_PE(dev
->subordinate
, pe
);
1124 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1125 * single PCI bus. Another one that contains the primary PCI bus and its
1126 * subordinate PCI devices and buses. The second type of PE is normally
1127 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1129 static struct pnv_ioda_pe
*pnv_ioda_setup_bus_PE(struct pci_bus
*bus
, bool all
)
1131 struct pci_controller
*hose
= pci_bus_to_host(bus
);
1132 struct pnv_phb
*phb
= hose
->private_data
;
1133 struct pnv_ioda_pe
*pe
= NULL
;
1134 unsigned int pe_num
;
1137 * In partial hotplug case, the PE instance might be still alive.
1138 * We should reuse it instead of allocating a new one.
1140 pe_num
= phb
->ioda
.pe_rmap
[bus
->number
<< 8];
1141 if (pe_num
!= IODA_INVALID_PE
) {
1142 pe
= &phb
->ioda
.pe_array
[pe_num
];
1143 pnv_ioda_setup_same_PE(bus
, pe
);
1147 /* PE number for root bus should have been reserved */
1148 if (pci_is_root_bus(bus
) &&
1149 phb
->ioda
.root_pe_idx
!= IODA_INVALID_PE
)
1150 pe
= &phb
->ioda
.pe_array
[phb
->ioda
.root_pe_idx
];
1152 /* Check if PE is determined by M64 */
1154 pe
= pnv_ioda_pick_m64_pe(bus
, all
);
1156 /* The PE number isn't pinned by M64 */
1158 pe
= pnv_ioda_alloc_pe(phb
);
1161 pr_warn("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1162 __func__
, pci_domain_nr(bus
), bus
->number
);
1166 pe
->flags
|= (all
? PNV_IODA_PE_BUS_ALL
: PNV_IODA_PE_BUS
);
1169 pe
->mve_number
= -1;
1170 pe
->rid
= bus
->busn_res
.start
<< 8;
1173 pe_info(pe
, "Secondary bus %pad..%pad associated with PE#%x\n",
1174 &bus
->busn_res
.start
, &bus
->busn_res
.end
,
1177 pe_info(pe
, "Secondary bus %pad associated with PE#%x\n",
1178 &bus
->busn_res
.start
, pe
->pe_number
);
1180 if (pnv_ioda_configure_pe(phb
, pe
)) {
1181 /* XXX What do we do here ? */
1182 pnv_ioda_free_pe(pe
);
1187 /* Associate it with all child devices */
1188 pnv_ioda_setup_same_PE(bus
, pe
);
1190 /* Put PE to the list */
1191 list_add_tail(&pe
->list
, &phb
->ioda
.pe_list
);
1196 static struct pnv_ioda_pe
*pnv_ioda_setup_npu_PE(struct pci_dev
*npu_pdev
)
1198 int pe_num
, found_pe
= false, rc
;
1200 struct pnv_ioda_pe
*pe
;
1201 struct pci_dev
*gpu_pdev
;
1202 struct pci_dn
*npu_pdn
;
1203 struct pci_controller
*hose
= pci_bus_to_host(npu_pdev
->bus
);
1204 struct pnv_phb
*phb
= hose
->private_data
;
1207 * Due to a hardware errata PE#0 on the NPU is reserved for
1208 * error handling. This means we only have three PEs remaining
1209 * which need to be assigned to four links, implying some
1210 * links must share PEs.
1212 * To achieve this we assign PEs such that NPUs linking the
1213 * same GPU get assigned the same PE.
1215 gpu_pdev
= pnv_pci_get_gpu_dev(npu_pdev
);
1216 for (pe_num
= 0; pe_num
< phb
->ioda
.total_pe_num
; pe_num
++) {
1217 pe
= &phb
->ioda
.pe_array
[pe_num
];
1221 if (pnv_pci_get_gpu_dev(pe
->pdev
) == gpu_pdev
) {
1223 * This device has the same peer GPU so should
1224 * be assigned the same PE as the existing
1227 dev_info(&npu_pdev
->dev
,
1228 "Associating to existing PE %x\n", pe_num
);
1229 pci_dev_get(npu_pdev
);
1230 npu_pdn
= pci_get_pdn(npu_pdev
);
1231 rid
= npu_pdev
->bus
->number
<< 8 | npu_pdn
->devfn
;
1232 npu_pdn
->pe_number
= pe_num
;
1233 phb
->ioda
.pe_rmap
[rid
] = pe
->pe_number
;
1235 /* Map the PE to this link */
1236 rc
= opal_pci_set_pe(phb
->opal_id
, pe_num
, rid
,
1238 OPAL_COMPARE_RID_DEVICE_NUMBER
,
1239 OPAL_COMPARE_RID_FUNCTION_NUMBER
,
1241 WARN_ON(rc
!= OPAL_SUCCESS
);
1249 * Could not find an existing PE so allocate a new
1252 return pnv_ioda_setup_dev_PE(npu_pdev
);
1257 static void pnv_ioda_setup_npu_PEs(struct pci_bus
*bus
)
1259 struct pci_dev
*pdev
;
1261 list_for_each_entry(pdev
, &bus
->devices
, bus_list
)
1262 pnv_ioda_setup_npu_PE(pdev
);
1265 static void pnv_pci_ioda_setup_PEs(void)
1267 struct pci_controller
*hose
;
1268 struct pnv_phb
*phb
;
1269 struct pci_bus
*bus
;
1270 struct pci_dev
*pdev
;
1271 struct pnv_ioda_pe
*pe
;
1273 list_for_each_entry(hose
, &hose_list
, list_node
) {
1274 phb
= hose
->private_data
;
1275 if (phb
->type
== PNV_PHB_NPU_NVLINK
) {
1276 /* PE#0 is needed for error reporting */
1277 pnv_ioda_reserve_pe(phb
, 0);
1278 pnv_ioda_setup_npu_PEs(hose
->bus
);
1279 if (phb
->model
== PNV_PHB_MODEL_NPU2
)
1280 WARN_ON_ONCE(pnv_npu2_init(hose
));
1282 if (phb
->type
== PNV_PHB_NPU_OCAPI
) {
1284 list_for_each_entry(pdev
, &bus
->devices
, bus_list
)
1285 pnv_ioda_setup_dev_PE(pdev
);
1288 list_for_each_entry(hose
, &hose_list
, list_node
) {
1289 phb
= hose
->private_data
;
1290 if (phb
->type
!= PNV_PHB_IODA2
)
1293 list_for_each_entry(pe
, &phb
->ioda
.pe_list
, list
)
1294 pnv_npu2_map_lpar(pe
, MSR_DR
| MSR_PR
| MSR_HV
);
1298 #ifdef CONFIG_PCI_IOV
1299 static int pnv_pci_vf_release_m64(struct pci_dev
*pdev
, u16 num_vfs
)
1301 struct pci_bus
*bus
;
1302 struct pci_controller
*hose
;
1303 struct pnv_phb
*phb
;
1309 hose
= pci_bus_to_host(bus
);
1310 phb
= hose
->private_data
;
1311 pdn
= pci_get_pdn(pdev
);
1313 if (pdn
->m64_single_mode
)
1318 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++)
1319 for (j
= 0; j
< m64_bars
; j
++) {
1320 if (pdn
->m64_map
[j
][i
] == IODA_INVALID_M64
)
1322 opal_pci_phb_mmio_enable(phb
->opal_id
,
1323 OPAL_M64_WINDOW_TYPE
, pdn
->m64_map
[j
][i
], 0);
1324 clear_bit(pdn
->m64_map
[j
][i
], &phb
->ioda
.m64_bar_alloc
);
1325 pdn
->m64_map
[j
][i
] = IODA_INVALID_M64
;
1328 kfree(pdn
->m64_map
);
1332 static int pnv_pci_vf_assign_m64(struct pci_dev
*pdev
, u16 num_vfs
)
1334 struct pci_bus
*bus
;
1335 struct pci_controller
*hose
;
1336 struct pnv_phb
*phb
;
1339 struct resource
*res
;
1343 resource_size_t size
, start
;
1348 hose
= pci_bus_to_host(bus
);
1349 phb
= hose
->private_data
;
1350 pdn
= pci_get_pdn(pdev
);
1351 total_vfs
= pci_sriov_get_totalvfs(pdev
);
1353 if (pdn
->m64_single_mode
)
1358 pdn
->m64_map
= kmalloc_array(m64_bars
,
1359 sizeof(*pdn
->m64_map
),
1363 /* Initialize the m64_map to IODA_INVALID_M64 */
1364 for (i
= 0; i
< m64_bars
; i
++)
1365 for (j
= 0; j
< PCI_SRIOV_NUM_BARS
; j
++)
1366 pdn
->m64_map
[i
][j
] = IODA_INVALID_M64
;
1369 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
1370 res
= &pdev
->resource
[i
+ PCI_IOV_RESOURCES
];
1371 if (!res
->flags
|| !res
->parent
)
1374 for (j
= 0; j
< m64_bars
; j
++) {
1376 win
= find_next_zero_bit(&phb
->ioda
.m64_bar_alloc
,
1377 phb
->ioda
.m64_bar_idx
+ 1, 0);
1379 if (win
>= phb
->ioda
.m64_bar_idx
+ 1)
1381 } while (test_and_set_bit(win
, &phb
->ioda
.m64_bar_alloc
));
1383 pdn
->m64_map
[j
][i
] = win
;
1385 if (pdn
->m64_single_mode
) {
1386 size
= pci_iov_resource_size(pdev
,
1387 PCI_IOV_RESOURCES
+ i
);
1388 start
= res
->start
+ size
* j
;
1390 size
= resource_size(res
);
1394 /* Map the M64 here */
1395 if (pdn
->m64_single_mode
) {
1396 pe_num
= pdn
->pe_num_map
[j
];
1397 rc
= opal_pci_map_pe_mmio_window(phb
->opal_id
,
1398 pe_num
, OPAL_M64_WINDOW_TYPE
,
1399 pdn
->m64_map
[j
][i
], 0);
1402 rc
= opal_pci_set_phb_mem_window(phb
->opal_id
,
1403 OPAL_M64_WINDOW_TYPE
,
1410 if (rc
!= OPAL_SUCCESS
) {
1411 dev_err(&pdev
->dev
, "Failed to map M64 window #%d: %lld\n",
1416 if (pdn
->m64_single_mode
)
1417 rc
= opal_pci_phb_mmio_enable(phb
->opal_id
,
1418 OPAL_M64_WINDOW_TYPE
, pdn
->m64_map
[j
][i
], 2);
1420 rc
= opal_pci_phb_mmio_enable(phb
->opal_id
,
1421 OPAL_M64_WINDOW_TYPE
, pdn
->m64_map
[j
][i
], 1);
1423 if (rc
!= OPAL_SUCCESS
) {
1424 dev_err(&pdev
->dev
, "Failed to enable M64 window #%d: %llx\n",
1433 pnv_pci_vf_release_m64(pdev
, num_vfs
);
1437 static long pnv_pci_ioda2_unset_window(struct iommu_table_group
*table_group
,
1440 static void pnv_pci_ioda2_release_dma_pe(struct pci_dev
*dev
, struct pnv_ioda_pe
*pe
)
1442 struct iommu_table
*tbl
;
1445 tbl
= pe
->table_group
.tables
[0];
1446 rc
= pnv_pci_ioda2_unset_window(&pe
->table_group
, 0);
1448 pe_warn(pe
, "OPAL error %lld release DMA window\n", rc
);
1450 pnv_pci_ioda2_set_bypass(pe
, false);
1451 if (pe
->table_group
.group
) {
1452 iommu_group_put(pe
->table_group
.group
);
1453 BUG_ON(pe
->table_group
.group
);
1455 iommu_tce_table_put(tbl
);
1458 static void pnv_ioda_release_vf_PE(struct pci_dev
*pdev
)
1460 struct pci_bus
*bus
;
1461 struct pci_controller
*hose
;
1462 struct pnv_phb
*phb
;
1463 struct pnv_ioda_pe
*pe
, *pe_n
;
1467 hose
= pci_bus_to_host(bus
);
1468 phb
= hose
->private_data
;
1469 pdn
= pci_get_pdn(pdev
);
1471 if (!pdev
->is_physfn
)
1474 list_for_each_entry_safe(pe
, pe_n
, &phb
->ioda
.pe_list
, list
) {
1475 if (pe
->parent_dev
!= pdev
)
1478 pnv_pci_ioda2_release_dma_pe(pdev
, pe
);
1480 /* Remove from list */
1481 mutex_lock(&phb
->ioda
.pe_list_mutex
);
1482 list_del(&pe
->list
);
1483 mutex_unlock(&phb
->ioda
.pe_list_mutex
);
1485 pnv_ioda_deconfigure_pe(phb
, pe
);
1487 pnv_ioda_free_pe(pe
);
1491 void pnv_pci_sriov_disable(struct pci_dev
*pdev
)
1493 struct pci_bus
*bus
;
1494 struct pci_controller
*hose
;
1495 struct pnv_phb
*phb
;
1496 struct pnv_ioda_pe
*pe
;
1501 hose
= pci_bus_to_host(bus
);
1502 phb
= hose
->private_data
;
1503 pdn
= pci_get_pdn(pdev
);
1504 num_vfs
= pdn
->num_vfs
;
1506 /* Release VF PEs */
1507 pnv_ioda_release_vf_PE(pdev
);
1509 if (phb
->type
== PNV_PHB_IODA2
) {
1510 if (!pdn
->m64_single_mode
)
1511 pnv_pci_vf_resource_shift(pdev
, -*pdn
->pe_num_map
);
1513 /* Release M64 windows */
1514 pnv_pci_vf_release_m64(pdev
, num_vfs
);
1516 /* Release PE numbers */
1517 if (pdn
->m64_single_mode
) {
1518 for (i
= 0; i
< num_vfs
; i
++) {
1519 if (pdn
->pe_num_map
[i
] == IODA_INVALID_PE
)
1522 pe
= &phb
->ioda
.pe_array
[pdn
->pe_num_map
[i
]];
1523 pnv_ioda_free_pe(pe
);
1526 bitmap_clear(phb
->ioda
.pe_alloc
, *pdn
->pe_num_map
, num_vfs
);
1527 /* Releasing pe_num_map */
1528 kfree(pdn
->pe_num_map
);
1532 static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb
*phb
,
1533 struct pnv_ioda_pe
*pe
);
1534 #ifdef CONFIG_IOMMU_API
1535 static void pnv_ioda_setup_bus_iommu_group(struct pnv_ioda_pe
*pe
,
1536 struct iommu_table_group
*table_group
, struct pci_bus
*bus
);
1539 static void pnv_ioda_setup_vf_PE(struct pci_dev
*pdev
, u16 num_vfs
)
1541 struct pci_bus
*bus
;
1542 struct pci_controller
*hose
;
1543 struct pnv_phb
*phb
;
1544 struct pnv_ioda_pe
*pe
;
1550 hose
= pci_bus_to_host(bus
);
1551 phb
= hose
->private_data
;
1552 pdn
= pci_get_pdn(pdev
);
1554 if (!pdev
->is_physfn
)
1557 /* Reserve PE for each VF */
1558 for (vf_index
= 0; vf_index
< num_vfs
; vf_index
++) {
1559 if (pdn
->m64_single_mode
)
1560 pe_num
= pdn
->pe_num_map
[vf_index
];
1562 pe_num
= *pdn
->pe_num_map
+ vf_index
;
1564 pe
= &phb
->ioda
.pe_array
[pe_num
];
1565 pe
->pe_number
= pe_num
;
1567 pe
->flags
= PNV_IODA_PE_VF
;
1569 pe
->parent_dev
= pdev
;
1570 pe
->mve_number
= -1;
1571 pe
->rid
= (pci_iov_virtfn_bus(pdev
, vf_index
) << 8) |
1572 pci_iov_virtfn_devfn(pdev
, vf_index
);
1574 pe_info(pe
, "VF %04d:%02d:%02d.%d associated with PE#%x\n",
1575 hose
->global_number
, pdev
->bus
->number
,
1576 PCI_SLOT(pci_iov_virtfn_devfn(pdev
, vf_index
)),
1577 PCI_FUNC(pci_iov_virtfn_devfn(pdev
, vf_index
)), pe_num
);
1579 if (pnv_ioda_configure_pe(phb
, pe
)) {
1580 /* XXX What do we do here ? */
1581 pnv_ioda_free_pe(pe
);
1586 /* Put PE to the list */
1587 mutex_lock(&phb
->ioda
.pe_list_mutex
);
1588 list_add_tail(&pe
->list
, &phb
->ioda
.pe_list
);
1589 mutex_unlock(&phb
->ioda
.pe_list_mutex
);
1591 pnv_pci_ioda2_setup_dma_pe(phb
, pe
);
1592 #ifdef CONFIG_IOMMU_API
1593 iommu_register_group(&pe
->table_group
,
1594 pe
->phb
->hose
->global_number
, pe
->pe_number
);
1595 pnv_ioda_setup_bus_iommu_group(pe
, &pe
->table_group
, NULL
);
1600 int pnv_pci_sriov_enable(struct pci_dev
*pdev
, u16 num_vfs
)
1602 struct pci_bus
*bus
;
1603 struct pci_controller
*hose
;
1604 struct pnv_phb
*phb
;
1605 struct pnv_ioda_pe
*pe
;
1611 hose
= pci_bus_to_host(bus
);
1612 phb
= hose
->private_data
;
1613 pdn
= pci_get_pdn(pdev
);
1615 if (phb
->type
== PNV_PHB_IODA2
) {
1616 if (!pdn
->vfs_expanded
) {
1617 dev_info(&pdev
->dev
, "don't support this SRIOV device"
1618 " with non 64bit-prefetchable IOV BAR\n");
1623 * When M64 BARs functions in Single PE mode, the number of VFs
1624 * could be enabled must be less than the number of M64 BARs.
1626 if (pdn
->m64_single_mode
&& num_vfs
> phb
->ioda
.m64_bar_idx
) {
1627 dev_info(&pdev
->dev
, "Not enough M64 BAR for VFs\n");
1631 /* Allocating pe_num_map */
1632 if (pdn
->m64_single_mode
)
1633 pdn
->pe_num_map
= kmalloc_array(num_vfs
,
1634 sizeof(*pdn
->pe_num_map
),
1637 pdn
->pe_num_map
= kmalloc(sizeof(*pdn
->pe_num_map
), GFP_KERNEL
);
1639 if (!pdn
->pe_num_map
)
1642 if (pdn
->m64_single_mode
)
1643 for (i
= 0; i
< num_vfs
; i
++)
1644 pdn
->pe_num_map
[i
] = IODA_INVALID_PE
;
1646 /* Calculate available PE for required VFs */
1647 if (pdn
->m64_single_mode
) {
1648 for (i
= 0; i
< num_vfs
; i
++) {
1649 pe
= pnv_ioda_alloc_pe(phb
);
1655 pdn
->pe_num_map
[i
] = pe
->pe_number
;
1658 mutex_lock(&phb
->ioda
.pe_alloc_mutex
);
1659 *pdn
->pe_num_map
= bitmap_find_next_zero_area(
1660 phb
->ioda
.pe_alloc
, phb
->ioda
.total_pe_num
,
1662 if (*pdn
->pe_num_map
>= phb
->ioda
.total_pe_num
) {
1663 mutex_unlock(&phb
->ioda
.pe_alloc_mutex
);
1664 dev_info(&pdev
->dev
, "Failed to enable VF%d\n", num_vfs
);
1665 kfree(pdn
->pe_num_map
);
1668 bitmap_set(phb
->ioda
.pe_alloc
, *pdn
->pe_num_map
, num_vfs
);
1669 mutex_unlock(&phb
->ioda
.pe_alloc_mutex
);
1671 pdn
->num_vfs
= num_vfs
;
1673 /* Assign M64 window accordingly */
1674 ret
= pnv_pci_vf_assign_m64(pdev
, num_vfs
);
1676 dev_info(&pdev
->dev
, "Not enough M64 window resources\n");
1681 * When using one M64 BAR to map one IOV BAR, we need to shift
1682 * the IOV BAR according to the PE# allocated to the VFs.
1683 * Otherwise, the PE# for the VF will conflict with others.
1685 if (!pdn
->m64_single_mode
) {
1686 ret
= pnv_pci_vf_resource_shift(pdev
, *pdn
->pe_num_map
);
1693 pnv_ioda_setup_vf_PE(pdev
, num_vfs
);
1698 if (pdn
->m64_single_mode
) {
1699 for (i
= 0; i
< num_vfs
; i
++) {
1700 if (pdn
->pe_num_map
[i
] == IODA_INVALID_PE
)
1703 pe
= &phb
->ioda
.pe_array
[pdn
->pe_num_map
[i
]];
1704 pnv_ioda_free_pe(pe
);
1707 bitmap_clear(phb
->ioda
.pe_alloc
, *pdn
->pe_num_map
, num_vfs
);
1709 /* Releasing pe_num_map */
1710 kfree(pdn
->pe_num_map
);
1715 int pnv_pcibios_sriov_disable(struct pci_dev
*pdev
)
1717 pnv_pci_sriov_disable(pdev
);
1719 /* Release PCI data */
1720 remove_dev_pci_data(pdev
);
1724 int pnv_pcibios_sriov_enable(struct pci_dev
*pdev
, u16 num_vfs
)
1726 /* Allocate PCI data */
1727 add_dev_pci_data(pdev
);
1729 return pnv_pci_sriov_enable(pdev
, num_vfs
);
1731 #endif /* CONFIG_PCI_IOV */
1733 static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb
*phb
, struct pci_dev
*pdev
)
1735 struct pci_dn
*pdn
= pci_get_pdn(pdev
);
1736 struct pnv_ioda_pe
*pe
;
1739 * The function can be called while the PE#
1740 * hasn't been assigned. Do nothing for the
1743 if (!pdn
|| pdn
->pe_number
== IODA_INVALID_PE
)
1746 pe
= &phb
->ioda
.pe_array
[pdn
->pe_number
];
1747 WARN_ON(get_dma_ops(&pdev
->dev
) != &dma_iommu_ops
);
1748 pdev
->dev
.archdata
.dma_offset
= pe
->tce_bypass_base
;
1749 set_iommu_table_base(&pdev
->dev
, pe
->table_group
.tables
[0]);
1751 * Note: iommu_add_device() will fail here as
1752 * for physical PE: the device is already added by now;
1753 * for virtual PE: sysfs entries are not ready yet and
1754 * tce_iommu_bus_notifier will add the device to a group later.
1759 * Reconfigure TVE#0 to be usable as 64-bit DMA space.
1761 * The first 4GB of virtual memory for a PE is reserved for 32-bit accesses.
1762 * Devices can only access more than that if bit 59 of the PCI address is set
1763 * by hardware, which indicates TVE#1 should be used instead of TVE#0.
1764 * Many PCI devices are not capable of addressing that many bits, and as a
1765 * result are limited to the 4GB of virtual memory made available to 32-bit
1768 * In order to work around this, reconfigure TVE#0 to be suitable for 64-bit
1769 * devices by configuring the virtual memory past the first 4GB inaccessible
1770 * by 64-bit DMAs. This should only be used by devices that want more than
1771 * 4GB, and only on PEs that have no 32-bit devices.
1773 * Currently this will only work on PHB3 (POWER8).
1775 static int pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe
*pe
)
1777 u64 window_size
, table_size
, tce_count
, addr
;
1778 struct page
*table_pages
;
1779 u64 tce_order
= 28; /* 256MB TCEs */
1784 * Window size needs to be a power of two, but needs to account for
1785 * shifting memory by the 4GB offset required to skip 32bit space.
1787 window_size
= roundup_pow_of_two(memory_hotplug_max() + (1ULL << 32));
1788 tce_count
= window_size
>> tce_order
;
1789 table_size
= tce_count
<< 3;
1791 if (table_size
< PAGE_SIZE
)
1792 table_size
= PAGE_SIZE
;
1794 table_pages
= alloc_pages_node(pe
->phb
->hose
->node
, GFP_KERNEL
,
1795 get_order(table_size
));
1799 tces
= page_address(table_pages
);
1803 memset(tces
, 0, table_size
);
1805 for (addr
= 0; addr
< memory_hotplug_max(); addr
+= (1 << tce_order
)) {
1806 tces
[(addr
+ (1ULL << 32)) >> tce_order
] =
1807 cpu_to_be64(addr
| TCE_PCI_READ
| TCE_PCI_WRITE
);
1810 rc
= opal_pci_map_pe_dma_window(pe
->phb
->opal_id
,
1812 /* reconfigure window 0 */
1813 (pe
->pe_number
<< 1) + 0,
1818 if (rc
== OPAL_SUCCESS
) {
1819 pe_info(pe
, "Using 64-bit DMA iommu bypass (through TVE#0)\n");
1823 pe_err(pe
, "Error configuring 64-bit DMA bypass\n");
1827 static bool pnv_pci_ioda_iommu_bypass_supported(struct pci_dev
*pdev
,
1830 struct pci_controller
*hose
= pci_bus_to_host(pdev
->bus
);
1831 struct pnv_phb
*phb
= hose
->private_data
;
1832 struct pci_dn
*pdn
= pci_get_pdn(pdev
);
1833 struct pnv_ioda_pe
*pe
;
1835 if (WARN_ON(!pdn
|| pdn
->pe_number
== IODA_INVALID_PE
))
1838 pe
= &phb
->ioda
.pe_array
[pdn
->pe_number
];
1839 if (pe
->tce_bypass_enabled
) {
1840 u64 top
= pe
->tce_bypass_base
+ memblock_end_of_DRAM() - 1;
1841 if (dma_mask
>= top
)
1846 * If the device can't set the TCE bypass bit but still wants
1847 * to access 4GB or more, on PHB3 we can reconfigure TVE#0 to
1848 * bypass the 32-bit region and be usable for 64-bit DMAs.
1849 * The device needs to be able to address all of this space.
1851 if (dma_mask
>> 32 &&
1852 dma_mask
> (memory_hotplug_max() + (1ULL << 32)) &&
1853 /* pe->pdev should be set if it's a single device, pe->pbus if not */
1854 (pe
->device_count
== 1 || !pe
->pbus
) &&
1855 phb
->model
== PNV_PHB_MODEL_PHB3
) {
1856 /* Configure the bypass mode */
1857 s64 rc
= pnv_pci_ioda_dma_64bit_bypass(pe
);
1860 /* 4GB offset bypasses 32-bit space */
1861 pdev
->dev
.archdata
.dma_offset
= (1ULL << 32);
1868 static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe
*pe
, struct pci_bus
*bus
)
1870 struct pci_dev
*dev
;
1872 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1873 set_iommu_table_base(&dev
->dev
, pe
->table_group
.tables
[0]);
1874 dev
->dev
.archdata
.dma_offset
= pe
->tce_bypass_base
;
1876 if ((pe
->flags
& PNV_IODA_PE_BUS_ALL
) && dev
->subordinate
)
1877 pnv_ioda_setup_bus_dma(pe
, dev
->subordinate
);
1881 static inline __be64 __iomem
*pnv_ioda_get_inval_reg(struct pnv_phb
*phb
,
1884 return real_mode
? (__be64 __iomem
*)(phb
->regs_phys
+ 0x210) :
1885 (phb
->regs
+ 0x210);
1888 static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table
*tbl
,
1889 unsigned long index
, unsigned long npages
, bool rm
)
1891 struct iommu_table_group_link
*tgl
= list_first_entry_or_null(
1892 &tbl
->it_group_list
, struct iommu_table_group_link
,
1894 struct pnv_ioda_pe
*pe
= container_of(tgl
->table_group
,
1895 struct pnv_ioda_pe
, table_group
);
1896 __be64 __iomem
*invalidate
= pnv_ioda_get_inval_reg(pe
->phb
, rm
);
1897 unsigned long start
, end
, inc
;
1899 start
= __pa(((__be64
*)tbl
->it_base
) + index
- tbl
->it_offset
);
1900 end
= __pa(((__be64
*)tbl
->it_base
) + index
- tbl
->it_offset
+
1903 /* p7ioc-style invalidation, 2 TCEs per write */
1904 start
|= (1ull << 63);
1905 end
|= (1ull << 63);
1907 end
|= inc
- 1; /* round up end to be different than start */
1909 mb(); /* Ensure above stores are visible */
1910 while (start
<= end
) {
1912 __raw_rm_writeq_be(start
, invalidate
);
1914 __raw_writeq_be(start
, invalidate
);
1920 * The iommu layer will do another mb() for us on build()
1921 * and we don't care on free()
1925 static int pnv_ioda1_tce_build(struct iommu_table
*tbl
, long index
,
1926 long npages
, unsigned long uaddr
,
1927 enum dma_data_direction direction
,
1928 unsigned long attrs
)
1930 int ret
= pnv_tce_build(tbl
, index
, npages
, uaddr
, direction
,
1934 pnv_pci_p7ioc_tce_invalidate(tbl
, index
, npages
, false);
1939 #ifdef CONFIG_IOMMU_API
1940 static int pnv_ioda1_tce_xchg(struct iommu_table
*tbl
, long index
,
1941 unsigned long *hpa
, enum dma_data_direction
*direction
)
1943 long ret
= pnv_tce_xchg(tbl
, index
, hpa
, direction
, true);
1946 pnv_pci_p7ioc_tce_invalidate(tbl
, index
, 1, false);
1951 static int pnv_ioda1_tce_xchg_rm(struct iommu_table
*tbl
, long index
,
1952 unsigned long *hpa
, enum dma_data_direction
*direction
)
1954 long ret
= pnv_tce_xchg(tbl
, index
, hpa
, direction
, false);
1957 pnv_pci_p7ioc_tce_invalidate(tbl
, index
, 1, true);
1963 static void pnv_ioda1_tce_free(struct iommu_table
*tbl
, long index
,
1966 pnv_tce_free(tbl
, index
, npages
);
1968 pnv_pci_p7ioc_tce_invalidate(tbl
, index
, npages
, false);
1971 static struct iommu_table_ops pnv_ioda1_iommu_ops
= {
1972 .set
= pnv_ioda1_tce_build
,
1973 #ifdef CONFIG_IOMMU_API
1974 .exchange
= pnv_ioda1_tce_xchg
,
1975 .exchange_rm
= pnv_ioda1_tce_xchg_rm
,
1976 .useraddrptr
= pnv_tce_useraddrptr
,
1978 .clear
= pnv_ioda1_tce_free
,
1982 #define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)
1983 #define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)
1984 #define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)
1986 static void pnv_pci_phb3_tce_invalidate_entire(struct pnv_phb
*phb
, bool rm
)
1988 __be64 __iomem
*invalidate
= pnv_ioda_get_inval_reg(phb
, rm
);
1989 const unsigned long val
= PHB3_TCE_KILL_INVAL_ALL
;
1991 mb(); /* Ensure previous TCE table stores are visible */
1993 __raw_rm_writeq_be(val
, invalidate
);
1995 __raw_writeq_be(val
, invalidate
);
1998 static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe
*pe
)
2000 /* 01xb - invalidate TCEs that match the specified PE# */
2001 __be64 __iomem
*invalidate
= pnv_ioda_get_inval_reg(pe
->phb
, false);
2002 unsigned long val
= PHB3_TCE_KILL_INVAL_PE
| (pe
->pe_number
& 0xFF);
2004 mb(); /* Ensure above stores are visible */
2005 __raw_writeq_be(val
, invalidate
);
2008 static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe
*pe
, bool rm
,
2009 unsigned shift
, unsigned long index
,
2010 unsigned long npages
)
2012 __be64 __iomem
*invalidate
= pnv_ioda_get_inval_reg(pe
->phb
, rm
);
2013 unsigned long start
, end
, inc
;
2015 /* We'll invalidate DMA address in PE scope */
2016 start
= PHB3_TCE_KILL_INVAL_ONE
;
2017 start
|= (pe
->pe_number
& 0xFF);
2020 /* Figure out the start, end and step */
2021 start
|= (index
<< shift
);
2022 end
|= ((index
+ npages
- 1) << shift
);
2023 inc
= (0x1ull
<< shift
);
2026 while (start
<= end
) {
2028 __raw_rm_writeq_be(start
, invalidate
);
2030 __raw_writeq_be(start
, invalidate
);
2035 static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe
*pe
)
2037 struct pnv_phb
*phb
= pe
->phb
;
2039 if (phb
->model
== PNV_PHB_MODEL_PHB3
&& phb
->regs
)
2040 pnv_pci_phb3_tce_invalidate_pe(pe
);
2042 opal_pci_tce_kill(phb
->opal_id
, OPAL_PCI_TCE_KILL_PE
,
2043 pe
->pe_number
, 0, 0, 0);
2046 static void pnv_pci_ioda2_tce_invalidate(struct iommu_table
*tbl
,
2047 unsigned long index
, unsigned long npages
, bool rm
)
2049 struct iommu_table_group_link
*tgl
;
2051 list_for_each_entry_lockless(tgl
, &tbl
->it_group_list
, next
) {
2052 struct pnv_ioda_pe
*pe
= container_of(tgl
->table_group
,
2053 struct pnv_ioda_pe
, table_group
);
2054 struct pnv_phb
*phb
= pe
->phb
;
2055 unsigned int shift
= tbl
->it_page_shift
;
2058 * NVLink1 can use the TCE kill register directly as
2059 * it's the same as PHB3. NVLink2 is different and
2060 * should go via the OPAL call.
2062 if (phb
->model
== PNV_PHB_MODEL_NPU
) {
2064 * The NVLink hardware does not support TCE kill
2065 * per TCE entry so we have to invalidate
2066 * the entire cache for it.
2068 pnv_pci_phb3_tce_invalidate_entire(phb
, rm
);
2071 if (phb
->model
== PNV_PHB_MODEL_PHB3
&& phb
->regs
)
2072 pnv_pci_phb3_tce_invalidate(pe
, rm
, shift
,
2075 opal_pci_tce_kill(phb
->opal_id
,
2076 OPAL_PCI_TCE_KILL_PAGES
,
2077 pe
->pe_number
, 1u << shift
,
2078 index
<< shift
, npages
);
2082 void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_phb
*phb
, bool rm
)
2084 if (phb
->model
== PNV_PHB_MODEL_NPU
|| phb
->model
== PNV_PHB_MODEL_PHB3
)
2085 pnv_pci_phb3_tce_invalidate_entire(phb
, rm
);
2087 opal_pci_tce_kill(phb
->opal_id
, OPAL_PCI_TCE_KILL
, 0, 0, 0, 0);
2090 static int pnv_ioda2_tce_build(struct iommu_table
*tbl
, long index
,
2091 long npages
, unsigned long uaddr
,
2092 enum dma_data_direction direction
,
2093 unsigned long attrs
)
2095 int ret
= pnv_tce_build(tbl
, index
, npages
, uaddr
, direction
,
2099 pnv_pci_ioda2_tce_invalidate(tbl
, index
, npages
, false);
2104 #ifdef CONFIG_IOMMU_API
2105 static int pnv_ioda2_tce_xchg(struct iommu_table
*tbl
, long index
,
2106 unsigned long *hpa
, enum dma_data_direction
*direction
)
2108 long ret
= pnv_tce_xchg(tbl
, index
, hpa
, direction
, true);
2111 pnv_pci_ioda2_tce_invalidate(tbl
, index
, 1, false);
2116 static int pnv_ioda2_tce_xchg_rm(struct iommu_table
*tbl
, long index
,
2117 unsigned long *hpa
, enum dma_data_direction
*direction
)
2119 long ret
= pnv_tce_xchg(tbl
, index
, hpa
, direction
, false);
2122 pnv_pci_ioda2_tce_invalidate(tbl
, index
, 1, true);
2128 static void pnv_ioda2_tce_free(struct iommu_table
*tbl
, long index
,
2131 pnv_tce_free(tbl
, index
, npages
);
2133 pnv_pci_ioda2_tce_invalidate(tbl
, index
, npages
, false);
2136 static struct iommu_table_ops pnv_ioda2_iommu_ops
= {
2137 .set
= pnv_ioda2_tce_build
,
2138 #ifdef CONFIG_IOMMU_API
2139 .exchange
= pnv_ioda2_tce_xchg
,
2140 .exchange_rm
= pnv_ioda2_tce_xchg_rm
,
2141 .useraddrptr
= pnv_tce_useraddrptr
,
2143 .clear
= pnv_ioda2_tce_free
,
2145 .free
= pnv_pci_ioda2_table_free_pages
,
2148 static int pnv_pci_ioda_dev_dma_weight(struct pci_dev
*dev
, void *data
)
2150 unsigned int *weight
= (unsigned int *)data
;
2152 /* This is quite simplistic. The "base" weight of a device
2153 * is 10. 0 means no DMA is to be accounted for it.
2155 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
)
2158 if (dev
->class == PCI_CLASS_SERIAL_USB_UHCI
||
2159 dev
->class == PCI_CLASS_SERIAL_USB_OHCI
||
2160 dev
->class == PCI_CLASS_SERIAL_USB_EHCI
)
2162 else if ((dev
->class >> 8) == PCI_CLASS_STORAGE_RAID
)
2170 static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe
*pe
)
2172 unsigned int weight
= 0;
2174 /* SRIOV VF has same DMA32 weight as its PF */
2175 #ifdef CONFIG_PCI_IOV
2176 if ((pe
->flags
& PNV_IODA_PE_VF
) && pe
->parent_dev
) {
2177 pnv_pci_ioda_dev_dma_weight(pe
->parent_dev
, &weight
);
2182 if ((pe
->flags
& PNV_IODA_PE_DEV
) && pe
->pdev
) {
2183 pnv_pci_ioda_dev_dma_weight(pe
->pdev
, &weight
);
2184 } else if ((pe
->flags
& PNV_IODA_PE_BUS
) && pe
->pbus
) {
2185 struct pci_dev
*pdev
;
2187 list_for_each_entry(pdev
, &pe
->pbus
->devices
, bus_list
)
2188 pnv_pci_ioda_dev_dma_weight(pdev
, &weight
);
2189 } else if ((pe
->flags
& PNV_IODA_PE_BUS_ALL
) && pe
->pbus
) {
2190 pci_walk_bus(pe
->pbus
, pnv_pci_ioda_dev_dma_weight
, &weight
);
2196 static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb
*phb
,
2197 struct pnv_ioda_pe
*pe
)
2200 struct page
*tce_mem
= NULL
;
2201 struct iommu_table
*tbl
;
2202 unsigned int weight
, total_weight
= 0;
2203 unsigned int tce32_segsz
, base
, segs
, avail
, i
;
2207 /* XXX FIXME: Handle 64-bit only DMA devices */
2208 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
2209 /* XXX FIXME: Allocate multi-level tables on PHB3 */
2210 weight
= pnv_pci_ioda_pe_dma_weight(pe
);
2214 pci_walk_bus(phb
->hose
->bus
, pnv_pci_ioda_dev_dma_weight
,
2216 segs
= (weight
* phb
->ioda
.dma32_count
) / total_weight
;
2221 * Allocate contiguous DMA32 segments. We begin with the expected
2222 * number of segments. With one more attempt, the number of DMA32
2223 * segments to be allocated is decreased by one until one segment
2224 * is allocated successfully.
2227 for (base
= 0; base
<= phb
->ioda
.dma32_count
- segs
; base
++) {
2228 for (avail
= 0, i
= base
; i
< base
+ segs
; i
++) {
2229 if (phb
->ioda
.dma32_segmap
[i
] ==
2240 pe_warn(pe
, "No available DMA32 segments\n");
2245 tbl
= pnv_pci_table_alloc(phb
->hose
->node
);
2249 iommu_register_group(&pe
->table_group
, phb
->hose
->global_number
,
2251 pnv_pci_link_table_and_group(phb
->hose
->node
, 0, tbl
, &pe
->table_group
);
2253 /* Grab a 32-bit TCE table */
2254 pe_info(pe
, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
2255 weight
, total_weight
, base
, segs
);
2256 pe_info(pe
, " Setting up 32-bit TCE table at %08x..%08x\n",
2257 base
* PNV_IODA1_DMA32_SEGSIZE
,
2258 (base
+ segs
) * PNV_IODA1_DMA32_SEGSIZE
- 1);
2260 /* XXX Currently, we allocate one big contiguous table for the
2261 * TCEs. We only really need one chunk per 256M of TCE space
2262 * (ie per segment) but that's an optimization for later, it
2263 * requires some added smarts with our get/put_tce implementation
2265 * Each TCE page is 4KB in size and each TCE entry occupies 8
2268 tce32_segsz
= PNV_IODA1_DMA32_SEGSIZE
>> (IOMMU_PAGE_SHIFT_4K
- 3);
2269 tce_mem
= alloc_pages_node(phb
->hose
->node
, GFP_KERNEL
,
2270 get_order(tce32_segsz
* segs
));
2272 pe_err(pe
, " Failed to allocate a 32-bit TCE memory\n");
2275 addr
= page_address(tce_mem
);
2276 memset(addr
, 0, tce32_segsz
* segs
);
2279 for (i
= 0; i
< segs
; i
++) {
2280 rc
= opal_pci_map_pe_dma_window(phb
->opal_id
,
2283 __pa(addr
) + tce32_segsz
* i
,
2284 tce32_segsz
, IOMMU_PAGE_SIZE_4K
);
2286 pe_err(pe
, " Failed to configure 32-bit TCE table, err %lld\n",
2292 /* Setup DMA32 segment mapping */
2293 for (i
= base
; i
< base
+ segs
; i
++)
2294 phb
->ioda
.dma32_segmap
[i
] = pe
->pe_number
;
2296 /* Setup linux iommu table */
2297 pnv_pci_setup_iommu_table(tbl
, addr
, tce32_segsz
* segs
,
2298 base
* PNV_IODA1_DMA32_SEGSIZE
,
2299 IOMMU_PAGE_SHIFT_4K
);
2301 tbl
->it_ops
= &pnv_ioda1_iommu_ops
;
2302 pe
->table_group
.tce32_start
= tbl
->it_offset
<< tbl
->it_page_shift
;
2303 pe
->table_group
.tce32_size
= tbl
->it_size
<< tbl
->it_page_shift
;
2304 iommu_init_table(tbl
, phb
->hose
->node
);
2306 if (pe
->flags
& (PNV_IODA_PE_BUS
| PNV_IODA_PE_BUS_ALL
))
2307 pnv_ioda_setup_bus_dma(pe
, pe
->pbus
);
2311 /* XXX Failure: Try to fallback to 64-bit only ? */
2313 __free_pages(tce_mem
, get_order(tce32_segsz
* segs
));
2315 pnv_pci_unlink_table_and_group(tbl
, &pe
->table_group
);
2316 iommu_tce_table_put(tbl
);
2320 static long pnv_pci_ioda2_set_window(struct iommu_table_group
*table_group
,
2321 int num
, struct iommu_table
*tbl
)
2323 struct pnv_ioda_pe
*pe
= container_of(table_group
, struct pnv_ioda_pe
,
2325 struct pnv_phb
*phb
= pe
->phb
;
2327 const unsigned long size
= tbl
->it_indirect_levels
?
2328 tbl
->it_level_size
: tbl
->it_size
;
2329 const __u64 start_addr
= tbl
->it_offset
<< tbl
->it_page_shift
;
2330 const __u64 win_size
= tbl
->it_size
<< tbl
->it_page_shift
;
2332 pe_info(pe
, "Setting up window#%d %llx..%llx pg=%lx\n",
2333 num
, start_addr
, start_addr
+ win_size
- 1,
2334 IOMMU_PAGE_SIZE(tbl
));
2337 * Map TCE table through TVT. The TVE index is the PE number
2338 * shifted by 1 bit for 32-bits DMA space.
2340 rc
= opal_pci_map_pe_dma_window(phb
->opal_id
,
2342 (pe
->pe_number
<< 1) + num
,
2343 tbl
->it_indirect_levels
+ 1,
2346 IOMMU_PAGE_SIZE(tbl
));
2348 pe_err(pe
, "Failed to configure TCE table, err %lld\n", rc
);
2352 pnv_pci_link_table_and_group(phb
->hose
->node
, num
,
2353 tbl
, &pe
->table_group
);
2354 pnv_pci_ioda2_tce_invalidate_pe(pe
);
2359 void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe
*pe
, bool enable
)
2361 uint16_t window_id
= (pe
->pe_number
<< 1 ) + 1;
2364 pe_info(pe
, "%sabling 64-bit DMA bypass\n", enable
? "En" : "Dis");
2366 phys_addr_t top
= memblock_end_of_DRAM();
2368 top
= roundup_pow_of_two(top
);
2369 rc
= opal_pci_map_pe_dma_window_real(pe
->phb
->opal_id
,
2372 pe
->tce_bypass_base
,
2375 rc
= opal_pci_map_pe_dma_window_real(pe
->phb
->opal_id
,
2378 pe
->tce_bypass_base
,
2382 pe_err(pe
, "OPAL error %lld configuring bypass window\n", rc
);
2384 pe
->tce_bypass_enabled
= enable
;
2387 static long pnv_pci_ioda2_create_table(struct iommu_table_group
*table_group
,
2388 int num
, __u32 page_shift
, __u64 window_size
, __u32 levels
,
2389 bool alloc_userspace_copy
, struct iommu_table
**ptbl
)
2391 struct pnv_ioda_pe
*pe
= container_of(table_group
, struct pnv_ioda_pe
,
2393 int nid
= pe
->phb
->hose
->node
;
2394 __u64 bus_offset
= num
? pe
->tce_bypass_base
: table_group
->tce32_start
;
2396 struct iommu_table
*tbl
;
2398 tbl
= pnv_pci_table_alloc(nid
);
2402 tbl
->it_ops
= &pnv_ioda2_iommu_ops
;
2404 ret
= pnv_pci_ioda2_table_alloc_pages(nid
,
2405 bus_offset
, page_shift
, window_size
,
2406 levels
, alloc_userspace_copy
, tbl
);
2408 iommu_tce_table_put(tbl
);
2417 static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe
*pe
)
2419 struct iommu_table
*tbl
= NULL
;
2423 * crashkernel= specifies the kdump kernel's maximum memory at
2424 * some offset and there is no guaranteed the result is a power
2425 * of 2, which will cause errors later.
2427 const u64 max_memory
= __rounddown_pow_of_two(memory_hotplug_max());
2430 * In memory constrained environments, e.g. kdump kernel, the
2431 * DMA window can be larger than available memory, which will
2432 * cause errors later.
2434 const u64 window_size
= min((u64
)pe
->table_group
.tce32_size
, max_memory
);
2436 rc
= pnv_pci_ioda2_create_table(&pe
->table_group
, 0,
2437 IOMMU_PAGE_SHIFT_4K
,
2439 POWERNV_IOMMU_DEFAULT_LEVELS
, false, &tbl
);
2441 pe_err(pe
, "Failed to create 32-bit TCE table, err %ld",
2446 iommu_init_table(tbl
, pe
->phb
->hose
->node
);
2448 rc
= pnv_pci_ioda2_set_window(&pe
->table_group
, 0, tbl
);
2450 pe_err(pe
, "Failed to configure 32-bit TCE table, err %ld\n",
2452 iommu_tce_table_put(tbl
);
2456 if (!pnv_iommu_bypass_disabled
)
2457 pnv_pci_ioda2_set_bypass(pe
, true);
2462 #if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2463 static long pnv_pci_ioda2_unset_window(struct iommu_table_group
*table_group
,
2466 struct pnv_ioda_pe
*pe
= container_of(table_group
, struct pnv_ioda_pe
,
2468 struct pnv_phb
*phb
= pe
->phb
;
2471 pe_info(pe
, "Removing DMA window #%d\n", num
);
2473 ret
= opal_pci_map_pe_dma_window(phb
->opal_id
, pe
->pe_number
,
2474 (pe
->pe_number
<< 1) + num
,
2475 0/* levels */, 0/* table address */,
2476 0/* table size */, 0/* page size */);
2478 pe_warn(pe
, "Unmapping failed, ret = %ld\n", ret
);
2480 pnv_pci_ioda2_tce_invalidate_pe(pe
);
2482 pnv_pci_unlink_table_and_group(table_group
->tables
[num
], table_group
);
2488 #ifdef CONFIG_IOMMU_API
2489 unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift
,
2490 __u64 window_size
, __u32 levels
)
2492 unsigned long bytes
= 0;
2493 const unsigned window_shift
= ilog2(window_size
);
2494 unsigned entries_shift
= window_shift
- page_shift
;
2495 unsigned table_shift
= entries_shift
+ 3;
2496 unsigned long tce_table_size
= max(0x1000UL
, 1UL << table_shift
);
2497 unsigned long direct_table_size
;
2499 if (!levels
|| (levels
> POWERNV_IOMMU_MAX_LEVELS
) ||
2500 !is_power_of_2(window_size
))
2503 /* Calculate a direct table size from window_size and levels */
2504 entries_shift
= (entries_shift
+ levels
- 1) / levels
;
2505 table_shift
= entries_shift
+ 3;
2506 table_shift
= max_t(unsigned, table_shift
, PAGE_SHIFT
);
2507 direct_table_size
= 1UL << table_shift
;
2509 for ( ; levels
; --levels
) {
2510 bytes
+= _ALIGN_UP(tce_table_size
, direct_table_size
);
2512 tce_table_size
/= direct_table_size
;
2513 tce_table_size
<<= 3;
2514 tce_table_size
= max_t(unsigned long,
2515 tce_table_size
, direct_table_size
);
2518 return bytes
+ bytes
; /* one for HW table, one for userspace copy */
2521 static long pnv_pci_ioda2_create_table_userspace(
2522 struct iommu_table_group
*table_group
,
2523 int num
, __u32 page_shift
, __u64 window_size
, __u32 levels
,
2524 struct iommu_table
**ptbl
)
2526 long ret
= pnv_pci_ioda2_create_table(table_group
,
2527 num
, page_shift
, window_size
, levels
, true, ptbl
);
2530 (*ptbl
)->it_allocated_size
= pnv_pci_ioda2_get_table_size(
2531 page_shift
, window_size
, levels
);
2535 static void pnv_ioda2_take_ownership(struct iommu_table_group
*table_group
)
2537 struct pnv_ioda_pe
*pe
= container_of(table_group
, struct pnv_ioda_pe
,
2539 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2540 struct iommu_table
*tbl
= pe
->table_group
.tables
[0];
2542 pnv_pci_ioda2_set_bypass(pe
, false);
2543 pnv_pci_ioda2_unset_window(&pe
->table_group
, 0);
2545 pnv_ioda_setup_bus_dma(pe
, pe
->pbus
);
2546 iommu_tce_table_put(tbl
);
2549 static void pnv_ioda2_release_ownership(struct iommu_table_group
*table_group
)
2551 struct pnv_ioda_pe
*pe
= container_of(table_group
, struct pnv_ioda_pe
,
2554 pnv_pci_ioda2_setup_default_config(pe
);
2556 pnv_ioda_setup_bus_dma(pe
, pe
->pbus
);
2559 static struct iommu_table_group_ops pnv_pci_ioda2_ops
= {
2560 .get_table_size
= pnv_pci_ioda2_get_table_size
,
2561 .create_table
= pnv_pci_ioda2_create_table_userspace
,
2562 .set_window
= pnv_pci_ioda2_set_window
,
2563 .unset_window
= pnv_pci_ioda2_unset_window
,
2564 .take_ownership
= pnv_ioda2_take_ownership
,
2565 .release_ownership
= pnv_ioda2_release_ownership
,
2568 static void pnv_ioda_setup_bus_iommu_group_add_devices(struct pnv_ioda_pe
*pe
,
2569 struct iommu_table_group
*table_group
,
2570 struct pci_bus
*bus
)
2572 struct pci_dev
*dev
;
2574 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
2575 iommu_add_device(table_group
, &dev
->dev
);
2577 if ((pe
->flags
& PNV_IODA_PE_BUS_ALL
) && dev
->subordinate
)
2578 pnv_ioda_setup_bus_iommu_group_add_devices(pe
,
2579 table_group
, dev
->subordinate
);
2583 static void pnv_ioda_setup_bus_iommu_group(struct pnv_ioda_pe
*pe
,
2584 struct iommu_table_group
*table_group
, struct pci_bus
*bus
)
2587 if (pe
->flags
& PNV_IODA_PE_DEV
)
2588 iommu_add_device(table_group
, &pe
->pdev
->dev
);
2590 if ((pe
->flags
& (PNV_IODA_PE_BUS
| PNV_IODA_PE_BUS_ALL
)) || bus
)
2591 pnv_ioda_setup_bus_iommu_group_add_devices(pe
, table_group
,
2595 static unsigned long pnv_ioda_parse_tce_sizes(struct pnv_phb
*phb
);
2597 static void pnv_pci_ioda_setup_iommu_api(void)
2599 struct pci_controller
*hose
;
2600 struct pnv_phb
*phb
;
2601 struct pnv_ioda_pe
*pe
;
2604 * There are 4 types of PEs:
2605 * - PNV_IODA_PE_BUS: a downstream port with an adapter,
2606 * created from pnv_pci_setup_bridge();
2607 * - PNV_IODA_PE_BUS_ALL: a PCI-PCIX bridge with devices behind it,
2608 * created from pnv_pci_setup_bridge();
2609 * - PNV_IODA_PE_VF: a SRIOV virtual function,
2610 * created from pnv_pcibios_sriov_enable();
2611 * - PNV_IODA_PE_DEV: an NPU or OCAPI device,
2612 * created from pnv_pci_ioda_fixup().
2614 * Normally a PE is represented by an IOMMU group, however for
2615 * devices with side channels the groups need to be more strict.
2617 list_for_each_entry(hose
, &hose_list
, list_node
) {
2618 phb
= hose
->private_data
;
2620 if (phb
->type
== PNV_PHB_NPU_NVLINK
||
2621 phb
->type
== PNV_PHB_NPU_OCAPI
)
2624 list_for_each_entry(pe
, &phb
->ioda
.pe_list
, list
) {
2625 struct iommu_table_group
*table_group
;
2627 table_group
= pnv_try_setup_npu_table_group(pe
);
2629 if (!pnv_pci_ioda_pe_dma_weight(pe
))
2632 table_group
= &pe
->table_group
;
2633 iommu_register_group(&pe
->table_group
,
2634 pe
->phb
->hose
->global_number
,
2637 pnv_ioda_setup_bus_iommu_group(pe
, table_group
,
2643 * Now we have all PHBs discovered, time to add NPU devices to
2644 * the corresponding IOMMU groups.
2646 list_for_each_entry(hose
, &hose_list
, list_node
) {
2647 unsigned long pgsizes
;
2649 phb
= hose
->private_data
;
2651 if (phb
->type
!= PNV_PHB_NPU_NVLINK
)
2654 pgsizes
= pnv_ioda_parse_tce_sizes(phb
);
2655 list_for_each_entry(pe
, &phb
->ioda
.pe_list
, list
) {
2657 * IODA2 bridges get this set up from
2658 * pci_controller_ops::setup_bridge but NPU bridges
2659 * do not have this hook defined so we do it here.
2661 pe
->table_group
.pgsizes
= pgsizes
;
2662 pnv_npu_compound_attach(pe
);
2666 #else /* !CONFIG_IOMMU_API */
2667 static void pnv_pci_ioda_setup_iommu_api(void) { };
2670 static unsigned long pnv_ioda_parse_tce_sizes(struct pnv_phb
*phb
)
2672 struct pci_controller
*hose
= phb
->hose
;
2673 struct device_node
*dn
= hose
->dn
;
2674 unsigned long mask
= 0;
2678 count
= of_property_count_u32_elems(dn
, "ibm,supported-tce-sizes");
2680 mask
= SZ_4K
| SZ_64K
;
2681 /* Add 16M for POWER8 by default */
2682 if (cpu_has_feature(CPU_FTR_ARCH_207S
) &&
2683 !cpu_has_feature(CPU_FTR_ARCH_300
))
2684 mask
|= SZ_16M
| SZ_256M
;
2688 for (i
= 0; i
< count
; i
++) {
2689 rc
= of_property_read_u32_index(dn
, "ibm,supported-tce-sizes",
2692 mask
|= 1ULL << val
;
2698 static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb
*phb
,
2699 struct pnv_ioda_pe
*pe
)
2703 if (!pnv_pci_ioda_pe_dma_weight(pe
))
2706 /* TVE #1 is selected by PCI address bit 59 */
2707 pe
->tce_bypass_base
= 1ull << 59;
2709 /* The PE will reserve all possible 32-bits space */
2710 pe_info(pe
, "Setting up 32-bit TCE table at 0..%08x\n",
2711 phb
->ioda
.m32_pci_base
);
2713 /* Setup linux iommu table */
2714 pe
->table_group
.tce32_start
= 0;
2715 pe
->table_group
.tce32_size
= phb
->ioda
.m32_pci_base
;
2716 pe
->table_group
.max_dynamic_windows_supported
=
2717 IOMMU_TABLE_GROUP_MAX_TABLES
;
2718 pe
->table_group
.max_levels
= POWERNV_IOMMU_MAX_LEVELS
;
2719 pe
->table_group
.pgsizes
= pnv_ioda_parse_tce_sizes(phb
);
2720 #ifdef CONFIG_IOMMU_API
2721 pe
->table_group
.ops
= &pnv_pci_ioda2_ops
;
2724 rc
= pnv_pci_ioda2_setup_default_config(pe
);
2728 if (pe
->flags
& (PNV_IODA_PE_BUS
| PNV_IODA_PE_BUS_ALL
))
2729 pnv_ioda_setup_bus_dma(pe
, pe
->pbus
);
2732 int64_t pnv_opal_pci_msi_eoi(struct irq_chip
*chip
, unsigned int hw_irq
)
2734 struct pnv_phb
*phb
= container_of(chip
, struct pnv_phb
,
2737 return opal_pci_msi_eoi(phb
->opal_id
, hw_irq
);
2740 static void pnv_ioda2_msi_eoi(struct irq_data
*d
)
2743 unsigned int hw_irq
= (unsigned int)irqd_to_hwirq(d
);
2744 struct irq_chip
*chip
= irq_data_get_irq_chip(d
);
2746 rc
= pnv_opal_pci_msi_eoi(chip
, hw_irq
);
2753 void pnv_set_msi_irq_chip(struct pnv_phb
*phb
, unsigned int virq
)
2755 struct irq_data
*idata
;
2756 struct irq_chip
*ichip
;
2758 /* The MSI EOI OPAL call is only needed on PHB3 */
2759 if (phb
->model
!= PNV_PHB_MODEL_PHB3
)
2762 if (!phb
->ioda
.irq_chip_init
) {
2764 * First time we setup an MSI IRQ, we need to setup the
2765 * corresponding IRQ chip to route correctly.
2767 idata
= irq_get_irq_data(virq
);
2768 ichip
= irq_data_get_irq_chip(idata
);
2769 phb
->ioda
.irq_chip_init
= 1;
2770 phb
->ioda
.irq_chip
= *ichip
;
2771 phb
->ioda
.irq_chip
.irq_eoi
= pnv_ioda2_msi_eoi
;
2773 irq_set_chip(virq
, &phb
->ioda
.irq_chip
);
2777 * Returns true iff chip is something that we could call
2778 * pnv_opal_pci_msi_eoi for.
2780 bool is_pnv_opal_msi(struct irq_chip
*chip
)
2782 return chip
->irq_eoi
== pnv_ioda2_msi_eoi
;
2784 EXPORT_SYMBOL_GPL(is_pnv_opal_msi
);
2786 static int pnv_pci_ioda_msi_setup(struct pnv_phb
*phb
, struct pci_dev
*dev
,
2787 unsigned int hwirq
, unsigned int virq
,
2788 unsigned int is_64
, struct msi_msg
*msg
)
2790 struct pnv_ioda_pe
*pe
= pnv_ioda_get_pe(dev
);
2791 unsigned int xive_num
= hwirq
- phb
->msi_base
;
2795 /* No PE assigned ? bail out ... no MSI for you ! */
2799 /* Check if we have an MVE */
2800 if (pe
->mve_number
< 0)
2803 /* Force 32-bit MSI on some broken devices */
2804 if (dev
->no_64bit_msi
)
2807 /* Assign XIVE to PE */
2808 rc
= opal_pci_set_xive_pe(phb
->opal_id
, pe
->pe_number
, xive_num
);
2810 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2811 pci_name(dev
), rc
, xive_num
);
2818 rc
= opal_get_msi_64(phb
->opal_id
, pe
->mve_number
, xive_num
, 1,
2821 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2825 msg
->address_hi
= be64_to_cpu(addr64
) >> 32;
2826 msg
->address_lo
= be64_to_cpu(addr64
) & 0xfffffffful
;
2830 rc
= opal_get_msi_32(phb
->opal_id
, pe
->mve_number
, xive_num
, 1,
2833 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2837 msg
->address_hi
= 0;
2838 msg
->address_lo
= be32_to_cpu(addr32
);
2840 msg
->data
= be32_to_cpu(data
);
2842 pnv_set_msi_irq_chip(phb
, virq
);
2844 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2845 " address=%x_%08x data=%x PE# %x\n",
2846 pci_name(dev
), is_64
? "64" : "32", hwirq
, xive_num
,
2847 msg
->address_hi
, msg
->address_lo
, data
, pe
->pe_number
);
2852 static void pnv_pci_init_ioda_msis(struct pnv_phb
*phb
)
2855 const __be32
*prop
= of_get_property(phb
->hose
->dn
,
2856 "ibm,opal-msi-ranges", NULL
);
2859 prop
= of_get_property(phb
->hose
->dn
, "msi-ranges", NULL
);
2864 phb
->msi_base
= be32_to_cpup(prop
);
2865 count
= be32_to_cpup(prop
+ 1);
2866 if (msi_bitmap_alloc(&phb
->msi_bmp
, count
, phb
->hose
->dn
)) {
2867 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2868 phb
->hose
->global_number
);
2872 phb
->msi_setup
= pnv_pci_ioda_msi_setup
;
2873 phb
->msi32_support
= 1;
2874 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
2875 count
, phb
->msi_base
);
2878 #ifdef CONFIG_PCI_IOV
2879 static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev
*pdev
)
2881 struct pci_controller
*hose
= pci_bus_to_host(pdev
->bus
);
2882 struct pnv_phb
*phb
= hose
->private_data
;
2883 const resource_size_t gate
= phb
->ioda
.m64_segsize
>> 2;
2884 struct resource
*res
;
2886 resource_size_t size
, total_vf_bar_sz
;
2890 if (!pdev
->is_physfn
|| pci_dev_is_added(pdev
))
2893 pdn
= pci_get_pdn(pdev
);
2894 pdn
->vfs_expanded
= 0;
2895 pdn
->m64_single_mode
= false;
2897 total_vfs
= pci_sriov_get_totalvfs(pdev
);
2898 mul
= phb
->ioda
.total_pe_num
;
2899 total_vf_bar_sz
= 0;
2901 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
2902 res
= &pdev
->resource
[i
+ PCI_IOV_RESOURCES
];
2903 if (!res
->flags
|| res
->parent
)
2905 if (!pnv_pci_is_m64_flags(res
->flags
)) {
2906 dev_warn(&pdev
->dev
, "Don't support SR-IOV with"
2907 " non M64 VF BAR%d: %pR. \n",
2912 total_vf_bar_sz
+= pci_iov_resource_size(pdev
,
2913 i
+ PCI_IOV_RESOURCES
);
2916 * If bigger than quarter of M64 segment size, just round up
2919 * Generally, one M64 BAR maps one IOV BAR. To avoid conflict
2920 * with other devices, IOV BAR size is expanded to be
2921 * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64
2922 * segment size , the expanded size would equal to half of the
2923 * whole M64 space size, which will exhaust the M64 Space and
2924 * limit the system flexibility. This is a design decision to
2925 * set the boundary to quarter of the M64 segment size.
2927 if (total_vf_bar_sz
> gate
) {
2928 mul
= roundup_pow_of_two(total_vfs
);
2929 dev_info(&pdev
->dev
,
2930 "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n",
2931 total_vf_bar_sz
, gate
, mul
);
2932 pdn
->m64_single_mode
= true;
2937 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
2938 res
= &pdev
->resource
[i
+ PCI_IOV_RESOURCES
];
2939 if (!res
->flags
|| res
->parent
)
2942 size
= pci_iov_resource_size(pdev
, i
+ PCI_IOV_RESOURCES
);
2944 * On PHB3, the minimum size alignment of M64 BAR in single
2947 if (pdn
->m64_single_mode
&& (size
< SZ_32M
))
2949 dev_dbg(&pdev
->dev
, " Fixing VF BAR%d: %pR to\n", i
, res
);
2950 res
->end
= res
->start
+ size
* mul
- 1;
2951 dev_dbg(&pdev
->dev
, " %pR\n", res
);
2952 dev_info(&pdev
->dev
, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
2955 pdn
->vfs_expanded
= mul
;
2960 /* To save MMIO space, IOV BAR is truncated. */
2961 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
2962 res
= &pdev
->resource
[i
+ PCI_IOV_RESOURCES
];
2964 res
->end
= res
->start
- 1;
2967 #endif /* CONFIG_PCI_IOV */
2969 static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe
*pe
,
2970 struct resource
*res
)
2972 struct pnv_phb
*phb
= pe
->phb
;
2973 struct pci_bus_region region
;
2977 if (!res
|| !res
->flags
|| res
->start
> res
->end
)
2980 if (res
->flags
& IORESOURCE_IO
) {
2981 region
.start
= res
->start
- phb
->ioda
.io_pci_base
;
2982 region
.end
= res
->end
- phb
->ioda
.io_pci_base
;
2983 index
= region
.start
/ phb
->ioda
.io_segsize
;
2985 while (index
< phb
->ioda
.total_pe_num
&&
2986 region
.start
<= region
.end
) {
2987 phb
->ioda
.io_segmap
[index
] = pe
->pe_number
;
2988 rc
= opal_pci_map_pe_mmio_window(phb
->opal_id
,
2989 pe
->pe_number
, OPAL_IO_WINDOW_TYPE
, 0, index
);
2990 if (rc
!= OPAL_SUCCESS
) {
2991 pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",
2992 __func__
, rc
, index
, pe
->pe_number
);
2996 region
.start
+= phb
->ioda
.io_segsize
;
2999 } else if ((res
->flags
& IORESOURCE_MEM
) &&
3000 !pnv_pci_is_m64(phb
, res
)) {
3001 region
.start
= res
->start
-
3002 phb
->hose
->mem_offset
[0] -
3003 phb
->ioda
.m32_pci_base
;
3004 region
.end
= res
->end
-
3005 phb
->hose
->mem_offset
[0] -
3006 phb
->ioda
.m32_pci_base
;
3007 index
= region
.start
/ phb
->ioda
.m32_segsize
;
3009 while (index
< phb
->ioda
.total_pe_num
&&
3010 region
.start
<= region
.end
) {
3011 phb
->ioda
.m32_segmap
[index
] = pe
->pe_number
;
3012 rc
= opal_pci_map_pe_mmio_window(phb
->opal_id
,
3013 pe
->pe_number
, OPAL_M32_WINDOW_TYPE
, 0, index
);
3014 if (rc
!= OPAL_SUCCESS
) {
3015 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",
3016 __func__
, rc
, index
, pe
->pe_number
);
3020 region
.start
+= phb
->ioda
.m32_segsize
;
3027 * This function is supposed to be called on basis of PE from top
3028 * to bottom style. So the the I/O or MMIO segment assigned to
3029 * parent PE could be overridden by its child PEs if necessary.
3031 static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe
*pe
)
3033 struct pci_dev
*pdev
;
3037 * NOTE: We only care PCI bus based PE for now. For PCI
3038 * device based PE, for example SRIOV sensitive VF should
3039 * be figured out later.
3041 BUG_ON(!(pe
->flags
& (PNV_IODA_PE_BUS
| PNV_IODA_PE_BUS_ALL
)));
3043 list_for_each_entry(pdev
, &pe
->pbus
->devices
, bus_list
) {
3044 for (i
= 0; i
<= PCI_ROM_RESOURCE
; i
++)
3045 pnv_ioda_setup_pe_res(pe
, &pdev
->resource
[i
]);
3048 * If the PE contains all subordinate PCI buses, the
3049 * windows of the child bridges should be mapped to
3052 if (!(pe
->flags
& PNV_IODA_PE_BUS_ALL
) || !pci_is_bridge(pdev
))
3054 for (i
= 0; i
< PCI_BRIDGE_RESOURCE_NUM
; i
++)
3055 pnv_ioda_setup_pe_res(pe
,
3056 &pdev
->resource
[PCI_BRIDGE_RESOURCES
+ i
]);
3060 #ifdef CONFIG_DEBUG_FS
3061 static int pnv_pci_diag_data_set(void *data
, u64 val
)
3063 struct pci_controller
*hose
;
3064 struct pnv_phb
*phb
;
3070 hose
= (struct pci_controller
*)data
;
3071 if (!hose
|| !hose
->private_data
)
3074 phb
= hose
->private_data
;
3076 /* Retrieve the diag data from firmware */
3077 ret
= opal_pci_get_phb_diag_data2(phb
->opal_id
, phb
->diag_data
,
3078 phb
->diag_data_size
);
3079 if (ret
!= OPAL_SUCCESS
)
3082 /* Print the diag data to the kernel log */
3083 pnv_pci_dump_phb_diag_data(phb
->hose
, phb
->diag_data
);
3087 DEFINE_SIMPLE_ATTRIBUTE(pnv_pci_diag_data_fops
, NULL
,
3088 pnv_pci_diag_data_set
, "%llu\n");
3090 #endif /* CONFIG_DEBUG_FS */
3092 static void pnv_pci_ioda_create_dbgfs(void)
3094 #ifdef CONFIG_DEBUG_FS
3095 struct pci_controller
*hose
, *tmp
;
3096 struct pnv_phb
*phb
;
3099 list_for_each_entry_safe(hose
, tmp
, &hose_list
, list_node
) {
3100 phb
= hose
->private_data
;
3102 /* Notify initialization of PHB done */
3103 phb
->initialized
= 1;
3105 sprintf(name
, "PCI%04x", hose
->global_number
);
3106 phb
->dbgfs
= debugfs_create_dir(name
, powerpc_debugfs_root
);
3108 pr_warn("%s: Error on creating debugfs on PHB#%x\n",
3109 __func__
, hose
->global_number
);
3113 debugfs_create_file("dump_diag_regs", 0200, phb
->dbgfs
, hose
,
3114 &pnv_pci_diag_data_fops
);
3116 #endif /* CONFIG_DEBUG_FS */
3119 static void pnv_pci_enable_bridge(struct pci_bus
*bus
)
3121 struct pci_dev
*dev
= bus
->self
;
3122 struct pci_bus
*child
;
3124 /* Empty bus ? bail */
3125 if (list_empty(&bus
->devices
))
3129 * If there's a bridge associated with that bus enable it. This works
3130 * around races in the generic code if the enabling is done during
3131 * parallel probing. This can be removed once those races have been
3135 int rc
= pci_enable_device(dev
);
3137 pci_err(dev
, "Error enabling bridge (%d)\n", rc
);
3138 pci_set_master(dev
);
3141 /* Perform the same to child busses */
3142 list_for_each_entry(child
, &bus
->children
, node
)
3143 pnv_pci_enable_bridge(child
);
3146 static void pnv_pci_enable_bridges(void)
3148 struct pci_controller
*hose
;
3150 list_for_each_entry(hose
, &hose_list
, list_node
)
3151 pnv_pci_enable_bridge(hose
->bus
);
3154 static void pnv_pci_ioda_fixup(void)
3156 pnv_pci_ioda_setup_PEs();
3157 pnv_pci_ioda_setup_iommu_api();
3158 pnv_pci_ioda_create_dbgfs();
3160 pnv_pci_enable_bridges();
3163 pnv_eeh_post_init();
3168 * Returns the alignment for I/O or memory windows for P2P
3169 * bridges. That actually depends on how PEs are segmented.
3170 * For now, we return I/O or M32 segment size for PE sensitive
3171 * P2P bridges. Otherwise, the default values (4KiB for I/O,
3172 * 1MiB for memory) will be returned.
3174 * The current PCI bus might be put into one PE, which was
3175 * create against the parent PCI bridge. For that case, we
3176 * needn't enlarge the alignment so that we can save some
3179 static resource_size_t
pnv_pci_window_alignment(struct pci_bus
*bus
,
3182 struct pci_dev
*bridge
;
3183 struct pci_controller
*hose
= pci_bus_to_host(bus
);
3184 struct pnv_phb
*phb
= hose
->private_data
;
3185 int num_pci_bridges
= 0;
3189 if (pci_pcie_type(bridge
) == PCI_EXP_TYPE_PCI_BRIDGE
) {
3191 if (num_pci_bridges
>= 2)
3195 bridge
= bridge
->bus
->self
;
3199 * We fall back to M32 if M64 isn't supported. We enforce the M64
3200 * alignment for any 64-bit resource, PCIe doesn't care and
3201 * bridges only do 64-bit prefetchable anyway.
3203 if (phb
->ioda
.m64_segsize
&& pnv_pci_is_m64_flags(type
))
3204 return phb
->ioda
.m64_segsize
;
3205 if (type
& IORESOURCE_MEM
)
3206 return phb
->ioda
.m32_segsize
;
3208 return phb
->ioda
.io_segsize
;
3212 * We are updating root port or the upstream port of the
3213 * bridge behind the root port with PHB's windows in order
3214 * to accommodate the changes on required resources during
3215 * PCI (slot) hotplug, which is connected to either root
3216 * port or the downstream ports of PCIe switch behind the
3219 static void pnv_pci_fixup_bridge_resources(struct pci_bus
*bus
,
3222 struct pci_controller
*hose
= pci_bus_to_host(bus
);
3223 struct pnv_phb
*phb
= hose
->private_data
;
3224 struct pci_dev
*bridge
= bus
->self
;
3225 struct resource
*r
, *w
;
3226 bool msi_region
= false;
3229 /* Check if we need apply fixup to the bridge's windows */
3230 if (!pci_is_root_bus(bridge
->bus
) &&
3231 !pci_is_root_bus(bridge
->bus
->self
->bus
))
3234 /* Fixup the resources */
3235 for (i
= 0; i
< PCI_BRIDGE_RESOURCE_NUM
; i
++) {
3236 r
= &bridge
->resource
[PCI_BRIDGE_RESOURCES
+ i
];
3237 if (!r
->flags
|| !r
->parent
)
3241 if (r
->flags
& type
& IORESOURCE_IO
)
3242 w
= &hose
->io_resource
;
3243 else if (pnv_pci_is_m64(phb
, r
) &&
3244 (type
& IORESOURCE_PREFETCH
) &&
3245 phb
->ioda
.m64_segsize
)
3246 w
= &hose
->mem_resources
[1];
3247 else if (r
->flags
& type
& IORESOURCE_MEM
) {
3248 w
= &hose
->mem_resources
[0];
3252 r
->start
= w
->start
;
3255 /* The 64KB 32-bits MSI region shouldn't be included in
3256 * the 32-bits bridge window. Otherwise, we can see strange
3257 * issues. One of them is EEH error observed on Garrison.
3259 * Exclude top 1MB region which is the minimal alignment of
3260 * 32-bits bridge window.
3269 static void pnv_pci_setup_bridge(struct pci_bus
*bus
, unsigned long type
)
3271 struct pci_controller
*hose
= pci_bus_to_host(bus
);
3272 struct pnv_phb
*phb
= hose
->private_data
;
3273 struct pci_dev
*bridge
= bus
->self
;
3274 struct pnv_ioda_pe
*pe
;
3275 bool all
= (pci_pcie_type(bridge
) == PCI_EXP_TYPE_PCI_BRIDGE
);
3277 /* Extend bridge's windows if necessary */
3278 pnv_pci_fixup_bridge_resources(bus
, type
);
3280 /* The PE for root bus should be realized before any one else */
3281 if (!phb
->ioda
.root_pe_populated
) {
3282 pe
= pnv_ioda_setup_bus_PE(phb
->hose
->bus
, false);
3284 phb
->ioda
.root_pe_idx
= pe
->pe_number
;
3285 phb
->ioda
.root_pe_populated
= true;
3289 /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
3290 if (list_empty(&bus
->devices
))
3293 /* Reserve PEs according to used M64 resources */
3294 pnv_ioda_reserve_m64_pe(bus
, NULL
, all
);
3297 * Assign PE. We might run here because of partial hotplug.
3298 * For the case, we just pick up the existing PE and should
3299 * not allocate resources again.
3301 pe
= pnv_ioda_setup_bus_PE(bus
, all
);
3305 pnv_ioda_setup_pe_seg(pe
);
3306 switch (phb
->type
) {
3308 pnv_pci_ioda1_setup_dma_pe(phb
, pe
);
3311 pnv_pci_ioda2_setup_dma_pe(phb
, pe
);
3314 pr_warn("%s: No DMA for PHB#%x (type %d)\n",
3315 __func__
, phb
->hose
->global_number
, phb
->type
);
3319 static resource_size_t
pnv_pci_default_alignment(void)
3324 #ifdef CONFIG_PCI_IOV
3325 static resource_size_t
pnv_pci_iov_resource_alignment(struct pci_dev
*pdev
,
3328 struct pci_controller
*hose
= pci_bus_to_host(pdev
->bus
);
3329 struct pnv_phb
*phb
= hose
->private_data
;
3330 struct pci_dn
*pdn
= pci_get_pdn(pdev
);
3331 resource_size_t align
;
3334 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
3335 * SR-IOV. While from hardware perspective, the range mapped by M64
3336 * BAR should be size aligned.
3338 * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra
3339 * powernv-specific hardware restriction is gone. But if just use the
3340 * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with
3341 * in one segment of M64 #15, which introduces the PE conflict between
3342 * PF and VF. Based on this, the minimum alignment of an IOV BAR is
3345 * This function returns the total IOV BAR size if M64 BAR is in
3346 * Shared PE mode or just VF BAR size if not.
3347 * If the M64 BAR is in Single PE mode, return the VF BAR size or
3348 * M64 segment size if IOV BAR size is less.
3350 align
= pci_iov_resource_size(pdev
, resno
);
3351 if (!pdn
->vfs_expanded
)
3353 if (pdn
->m64_single_mode
)
3354 return max(align
, (resource_size_t
)phb
->ioda
.m64_segsize
);
3356 return pdn
->vfs_expanded
* align
;
3358 #endif /* CONFIG_PCI_IOV */
3360 /* Prevent enabling devices for which we couldn't properly
3363 static bool pnv_pci_enable_device_hook(struct pci_dev
*dev
)
3365 struct pci_controller
*hose
= pci_bus_to_host(dev
->bus
);
3366 struct pnv_phb
*phb
= hose
->private_data
;
3369 /* The function is probably called while the PEs have
3370 * not be created yet. For example, resource reassignment
3371 * during PCI probe period. We just skip the check if
3374 if (!phb
->initialized
)
3377 pdn
= pci_get_pdn(dev
);
3378 if (!pdn
|| pdn
->pe_number
== IODA_INVALID_PE
)
3384 static long pnv_pci_ioda1_unset_window(struct iommu_table_group
*table_group
,
3387 struct pnv_ioda_pe
*pe
= container_of(table_group
,
3388 struct pnv_ioda_pe
, table_group
);
3389 struct pnv_phb
*phb
= pe
->phb
;
3393 pe_info(pe
, "Removing DMA window #%d\n", num
);
3394 for (idx
= 0; idx
< phb
->ioda
.dma32_count
; idx
++) {
3395 if (phb
->ioda
.dma32_segmap
[idx
] != pe
->pe_number
)
3398 rc
= opal_pci_map_pe_dma_window(phb
->opal_id
, pe
->pe_number
,
3399 idx
, 0, 0ul, 0ul, 0ul);
3400 if (rc
!= OPAL_SUCCESS
) {
3401 pe_warn(pe
, "Failure %ld unmapping DMA32 segment#%d\n",
3406 phb
->ioda
.dma32_segmap
[idx
] = IODA_INVALID_PE
;
3409 pnv_pci_unlink_table_and_group(table_group
->tables
[num
], table_group
);
3410 return OPAL_SUCCESS
;
3413 static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe
*pe
)
3415 unsigned int weight
= pnv_pci_ioda_pe_dma_weight(pe
);
3416 struct iommu_table
*tbl
= pe
->table_group
.tables
[0];
3422 rc
= pnv_pci_ioda1_unset_window(&pe
->table_group
, 0);
3423 if (rc
!= OPAL_SUCCESS
)
3426 pnv_pci_p7ioc_tce_invalidate(tbl
, tbl
->it_offset
, tbl
->it_size
, false);
3427 if (pe
->table_group
.group
) {
3428 iommu_group_put(pe
->table_group
.group
);
3429 WARN_ON(pe
->table_group
.group
);
3432 free_pages(tbl
->it_base
, get_order(tbl
->it_size
<< 3));
3433 iommu_tce_table_put(tbl
);
3436 static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe
*pe
)
3438 struct iommu_table
*tbl
= pe
->table_group
.tables
[0];
3439 unsigned int weight
= pnv_pci_ioda_pe_dma_weight(pe
);
3440 #ifdef CONFIG_IOMMU_API
3447 #ifdef CONFIG_IOMMU_API
3448 rc
= pnv_pci_ioda2_unset_window(&pe
->table_group
, 0);
3450 pe_warn(pe
, "OPAL error %lld release DMA window\n", rc
);
3453 pnv_pci_ioda2_set_bypass(pe
, false);
3454 if (pe
->table_group
.group
) {
3455 iommu_group_put(pe
->table_group
.group
);
3456 WARN_ON(pe
->table_group
.group
);
3459 iommu_tce_table_put(tbl
);
3462 static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe
*pe
,
3466 struct pnv_phb
*phb
= pe
->phb
;
3470 for (idx
= 0; idx
< phb
->ioda
.total_pe_num
; idx
++) {
3471 if (map
[idx
] != pe
->pe_number
)
3474 if (win
== OPAL_M64_WINDOW_TYPE
)
3475 rc
= opal_pci_map_pe_mmio_window(phb
->opal_id
,
3476 phb
->ioda
.reserved_pe_idx
, win
,
3477 idx
/ PNV_IODA1_M64_SEGS
,
3478 idx
% PNV_IODA1_M64_SEGS
);
3480 rc
= opal_pci_map_pe_mmio_window(phb
->opal_id
,
3481 phb
->ioda
.reserved_pe_idx
, win
, 0, idx
);
3483 if (rc
!= OPAL_SUCCESS
)
3484 pe_warn(pe
, "Error %lld unmapping (%d) segment#%d\n",
3487 map
[idx
] = IODA_INVALID_PE
;
3491 static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe
*pe
)
3493 struct pnv_phb
*phb
= pe
->phb
;
3495 if (phb
->type
== PNV_PHB_IODA1
) {
3496 pnv_ioda_free_pe_seg(pe
, OPAL_IO_WINDOW_TYPE
,
3497 phb
->ioda
.io_segmap
);
3498 pnv_ioda_free_pe_seg(pe
, OPAL_M32_WINDOW_TYPE
,
3499 phb
->ioda
.m32_segmap
);
3500 pnv_ioda_free_pe_seg(pe
, OPAL_M64_WINDOW_TYPE
,
3501 phb
->ioda
.m64_segmap
);
3502 } else if (phb
->type
== PNV_PHB_IODA2
) {
3503 pnv_ioda_free_pe_seg(pe
, OPAL_M32_WINDOW_TYPE
,
3504 phb
->ioda
.m32_segmap
);
3508 static void pnv_ioda_release_pe(struct pnv_ioda_pe
*pe
)
3510 struct pnv_phb
*phb
= pe
->phb
;
3511 struct pnv_ioda_pe
*slave
, *tmp
;
3513 list_del(&pe
->list
);
3514 switch (phb
->type
) {
3516 pnv_pci_ioda1_release_pe_dma(pe
);
3519 pnv_pci_ioda2_release_pe_dma(pe
);
3525 pnv_ioda_release_pe_seg(pe
);
3526 pnv_ioda_deconfigure_pe(pe
->phb
, pe
);
3528 /* Release slave PEs in the compound PE */
3529 if (pe
->flags
& PNV_IODA_PE_MASTER
) {
3530 list_for_each_entry_safe(slave
, tmp
, &pe
->slaves
, list
) {
3531 list_del(&slave
->list
);
3532 pnv_ioda_free_pe(slave
);
3537 * The PE for root bus can be removed because of hotplug in EEH
3538 * recovery for fenced PHB error. We need to mark the PE dead so
3539 * that it can be populated again in PCI hot add path. The PE
3540 * shouldn't be destroyed as it's the global reserved resource.
3542 if (phb
->ioda
.root_pe_populated
&&
3543 phb
->ioda
.root_pe_idx
== pe
->pe_number
)
3544 phb
->ioda
.root_pe_populated
= false;
3546 pnv_ioda_free_pe(pe
);
3549 static void pnv_pci_release_device(struct pci_dev
*pdev
)
3551 struct pci_controller
*hose
= pci_bus_to_host(pdev
->bus
);
3552 struct pnv_phb
*phb
= hose
->private_data
;
3553 struct pci_dn
*pdn
= pci_get_pdn(pdev
);
3554 struct pnv_ioda_pe
*pe
;
3556 if (pdev
->is_virtfn
)
3559 if (!pdn
|| pdn
->pe_number
== IODA_INVALID_PE
)
3563 * PCI hotplug can happen as part of EEH error recovery. The @pdn
3564 * isn't removed and added afterwards in this scenario. We should
3565 * set the PE number in @pdn to an invalid one. Otherwise, the PE's
3566 * device count is decreased on removing devices while failing to
3567 * be increased on adding devices. It leads to unbalanced PE's device
3568 * count and eventually make normal PCI hotplug path broken.
3570 pe
= &phb
->ioda
.pe_array
[pdn
->pe_number
];
3571 pdn
->pe_number
= IODA_INVALID_PE
;
3573 WARN_ON(--pe
->device_count
< 0);
3574 if (pe
->device_count
== 0)
3575 pnv_ioda_release_pe(pe
);
3578 static void pnv_npu_disable_device(struct pci_dev
*pdev
)
3580 struct eeh_dev
*edev
= pci_dev_to_eeh_dev(pdev
);
3581 struct eeh_pe
*eehpe
= edev
? edev
->pe
: NULL
;
3583 if (eehpe
&& eeh_ops
&& eeh_ops
->reset
)
3584 eeh_ops
->reset(eehpe
, EEH_RESET_HOT
);
3587 static void pnv_pci_ioda_shutdown(struct pci_controller
*hose
)
3589 struct pnv_phb
*phb
= hose
->private_data
;
3591 opal_pci_reset(phb
->opal_id
, OPAL_RESET_PCI_IODA_TABLE
,
3595 static const struct pci_controller_ops pnv_pci_ioda_controller_ops
= {
3596 .dma_dev_setup
= pnv_pci_dma_dev_setup
,
3597 .dma_bus_setup
= pnv_pci_dma_bus_setup
,
3598 .iommu_bypass_supported
= pnv_pci_ioda_iommu_bypass_supported
,
3599 .setup_msi_irqs
= pnv_setup_msi_irqs
,
3600 .teardown_msi_irqs
= pnv_teardown_msi_irqs
,
3601 .enable_device_hook
= pnv_pci_enable_device_hook
,
3602 .release_device
= pnv_pci_release_device
,
3603 .window_alignment
= pnv_pci_window_alignment
,
3604 .setup_bridge
= pnv_pci_setup_bridge
,
3605 .reset_secondary_bus
= pnv_pci_reset_secondary_bus
,
3606 .shutdown
= pnv_pci_ioda_shutdown
,
3609 static const struct pci_controller_ops pnv_npu_ioda_controller_ops
= {
3610 .dma_dev_setup
= pnv_pci_dma_dev_setup
,
3611 .setup_msi_irqs
= pnv_setup_msi_irqs
,
3612 .teardown_msi_irqs
= pnv_teardown_msi_irqs
,
3613 .enable_device_hook
= pnv_pci_enable_device_hook
,
3614 .window_alignment
= pnv_pci_window_alignment
,
3615 .reset_secondary_bus
= pnv_pci_reset_secondary_bus
,
3616 .shutdown
= pnv_pci_ioda_shutdown
,
3617 .disable_device
= pnv_npu_disable_device
,
3620 static const struct pci_controller_ops pnv_npu_ocapi_ioda_controller_ops
= {
3621 .enable_device_hook
= pnv_pci_enable_device_hook
,
3622 .window_alignment
= pnv_pci_window_alignment
,
3623 .reset_secondary_bus
= pnv_pci_reset_secondary_bus
,
3624 .shutdown
= pnv_pci_ioda_shutdown
,
3627 static void __init
pnv_pci_init_ioda_phb(struct device_node
*np
,
3628 u64 hub_id
, int ioda_type
)
3630 struct pci_controller
*hose
;
3631 struct pnv_phb
*phb
;
3632 unsigned long size
, m64map_off
, m32map_off
, pemap_off
;
3633 unsigned long iomap_off
= 0, dma32map_off
= 0;
3635 const __be64
*prop64
;
3636 const __be32
*prop32
;
3643 if (!of_device_is_available(np
))
3646 pr_info("Initializing %s PHB (%pOF)\n", pnv_phb_names
[ioda_type
], np
);
3648 prop64
= of_get_property(np
, "ibm,opal-phbid", NULL
);
3650 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3653 phb_id
= be64_to_cpup(prop64
);
3654 pr_debug(" PHB-ID : 0x%016llx\n", phb_id
);
3656 phb
= memblock_alloc(sizeof(*phb
), SMP_CACHE_BYTES
);
3658 panic("%s: Failed to allocate %zu bytes\n", __func__
,
3661 /* Allocate PCI controller */
3662 phb
->hose
= hose
= pcibios_alloc_controller(np
);
3664 pr_err(" Can't allocate PCI controller for %pOF\n",
3666 memblock_free(__pa(phb
), sizeof(struct pnv_phb
));
3670 spin_lock_init(&phb
->lock
);
3671 prop32
= of_get_property(np
, "bus-range", &len
);
3672 if (prop32
&& len
== 8) {
3673 hose
->first_busno
= be32_to_cpu(prop32
[0]);
3674 hose
->last_busno
= be32_to_cpu(prop32
[1]);
3676 pr_warn(" Broken <bus-range> on %pOF\n", np
);
3677 hose
->first_busno
= 0;
3678 hose
->last_busno
= 0xff;
3680 hose
->private_data
= phb
;
3681 phb
->hub_id
= hub_id
;
3682 phb
->opal_id
= phb_id
;
3683 phb
->type
= ioda_type
;
3684 mutex_init(&phb
->ioda
.pe_alloc_mutex
);
3686 /* Detect specific models for error handling */
3687 if (of_device_is_compatible(np
, "ibm,p7ioc-pciex"))
3688 phb
->model
= PNV_PHB_MODEL_P7IOC
;
3689 else if (of_device_is_compatible(np
, "ibm,power8-pciex"))
3690 phb
->model
= PNV_PHB_MODEL_PHB3
;
3691 else if (of_device_is_compatible(np
, "ibm,power8-npu-pciex"))
3692 phb
->model
= PNV_PHB_MODEL_NPU
;
3693 else if (of_device_is_compatible(np
, "ibm,power9-npu-pciex"))
3694 phb
->model
= PNV_PHB_MODEL_NPU2
;
3696 phb
->model
= PNV_PHB_MODEL_UNKNOWN
;
3698 /* Initialize diagnostic data buffer */
3699 prop32
= of_get_property(np
, "ibm,phb-diag-data-size", NULL
);
3701 phb
->diag_data_size
= be32_to_cpup(prop32
);
3703 phb
->diag_data_size
= PNV_PCI_DIAG_BUF_SIZE
;
3705 phb
->diag_data
= memblock_alloc(phb
->diag_data_size
, SMP_CACHE_BYTES
);
3706 if (!phb
->diag_data
)
3707 panic("%s: Failed to allocate %u bytes\n", __func__
,
3708 phb
->diag_data_size
);
3710 /* Parse 32-bit and IO ranges (if any) */
3711 pci_process_bridge_OF_ranges(hose
, np
, !hose
->global_number
);
3714 if (!of_address_to_resource(np
, 0, &r
)) {
3715 phb
->regs_phys
= r
.start
;
3716 phb
->regs
= ioremap(r
.start
, resource_size(&r
));
3717 if (phb
->regs
== NULL
)
3718 pr_err(" Failed to map registers !\n");
3721 /* Initialize more IODA stuff */
3722 phb
->ioda
.total_pe_num
= 1;
3723 prop32
= of_get_property(np
, "ibm,opal-num-pes", NULL
);
3725 phb
->ioda
.total_pe_num
= be32_to_cpup(prop32
);
3726 prop32
= of_get_property(np
, "ibm,opal-reserved-pe", NULL
);
3728 phb
->ioda
.reserved_pe_idx
= be32_to_cpup(prop32
);
3730 /* Invalidate RID to PE# mapping */
3731 for (segno
= 0; segno
< ARRAY_SIZE(phb
->ioda
.pe_rmap
); segno
++)
3732 phb
->ioda
.pe_rmap
[segno
] = IODA_INVALID_PE
;
3734 /* Parse 64-bit MMIO range */
3735 pnv_ioda_parse_m64_window(phb
);
3737 phb
->ioda
.m32_size
= resource_size(&hose
->mem_resources
[0]);
3738 /* FW Has already off top 64k of M32 space (MSI space) */
3739 phb
->ioda
.m32_size
+= 0x10000;
3741 phb
->ioda
.m32_segsize
= phb
->ioda
.m32_size
/ phb
->ioda
.total_pe_num
;
3742 phb
->ioda
.m32_pci_base
= hose
->mem_resources
[0].start
- hose
->mem_offset
[0];
3743 phb
->ioda
.io_size
= hose
->pci_io_size
;
3744 phb
->ioda
.io_segsize
= phb
->ioda
.io_size
/ phb
->ioda
.total_pe_num
;
3745 phb
->ioda
.io_pci_base
= 0; /* XXX calculate this ? */
3747 /* Calculate how many 32-bit TCE segments we have */
3748 phb
->ioda
.dma32_count
= phb
->ioda
.m32_pci_base
/
3749 PNV_IODA1_DMA32_SEGSIZE
;
3751 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
3752 size
= _ALIGN_UP(max_t(unsigned, phb
->ioda
.total_pe_num
, 8) / 8,
3753 sizeof(unsigned long));
3755 size
+= phb
->ioda
.total_pe_num
* sizeof(phb
->ioda
.m64_segmap
[0]);
3757 size
+= phb
->ioda
.total_pe_num
* sizeof(phb
->ioda
.m32_segmap
[0]);
3758 if (phb
->type
== PNV_PHB_IODA1
) {
3760 size
+= phb
->ioda
.total_pe_num
* sizeof(phb
->ioda
.io_segmap
[0]);
3761 dma32map_off
= size
;
3762 size
+= phb
->ioda
.dma32_count
*
3763 sizeof(phb
->ioda
.dma32_segmap
[0]);
3766 size
+= phb
->ioda
.total_pe_num
* sizeof(struct pnv_ioda_pe
);
3767 aux
= memblock_alloc(size
, SMP_CACHE_BYTES
);
3769 panic("%s: Failed to allocate %lu bytes\n", __func__
, size
);
3770 phb
->ioda
.pe_alloc
= aux
;
3771 phb
->ioda
.m64_segmap
= aux
+ m64map_off
;
3772 phb
->ioda
.m32_segmap
= aux
+ m32map_off
;
3773 for (segno
= 0; segno
< phb
->ioda
.total_pe_num
; segno
++) {
3774 phb
->ioda
.m64_segmap
[segno
] = IODA_INVALID_PE
;
3775 phb
->ioda
.m32_segmap
[segno
] = IODA_INVALID_PE
;
3777 if (phb
->type
== PNV_PHB_IODA1
) {
3778 phb
->ioda
.io_segmap
= aux
+ iomap_off
;
3779 for (segno
= 0; segno
< phb
->ioda
.total_pe_num
; segno
++)
3780 phb
->ioda
.io_segmap
[segno
] = IODA_INVALID_PE
;
3782 phb
->ioda
.dma32_segmap
= aux
+ dma32map_off
;
3783 for (segno
= 0; segno
< phb
->ioda
.dma32_count
; segno
++)
3784 phb
->ioda
.dma32_segmap
[segno
] = IODA_INVALID_PE
;
3786 phb
->ioda
.pe_array
= aux
+ pemap_off
;
3789 * Choose PE number for root bus, which shouldn't have
3790 * M64 resources consumed by its child devices. To pick
3791 * the PE number adjacent to the reserved one if possible.
3793 pnv_ioda_reserve_pe(phb
, phb
->ioda
.reserved_pe_idx
);
3794 if (phb
->ioda
.reserved_pe_idx
== 0) {
3795 phb
->ioda
.root_pe_idx
= 1;
3796 pnv_ioda_reserve_pe(phb
, phb
->ioda
.root_pe_idx
);
3797 } else if (phb
->ioda
.reserved_pe_idx
== (phb
->ioda
.total_pe_num
- 1)) {
3798 phb
->ioda
.root_pe_idx
= phb
->ioda
.reserved_pe_idx
- 1;
3799 pnv_ioda_reserve_pe(phb
, phb
->ioda
.root_pe_idx
);
3801 phb
->ioda
.root_pe_idx
= IODA_INVALID_PE
;
3804 INIT_LIST_HEAD(&phb
->ioda
.pe_list
);
3805 mutex_init(&phb
->ioda
.pe_list_mutex
);
3807 /* Calculate how many 32-bit TCE segments we have */
3808 phb
->ioda
.dma32_count
= phb
->ioda
.m32_pci_base
/
3809 PNV_IODA1_DMA32_SEGSIZE
;
3811 #if 0 /* We should really do that ... */
3812 rc
= opal_pci_set_phb_mem_window(opal
->phb_id
,
3815 starting_real_address
,
3816 starting_pci_address
,
3820 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
3821 phb
->ioda
.total_pe_num
, phb
->ioda
.reserved_pe_idx
,
3822 phb
->ioda
.m32_size
, phb
->ioda
.m32_segsize
);
3823 if (phb
->ioda
.m64_size
)
3824 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3825 phb
->ioda
.m64_size
, phb
->ioda
.m64_segsize
);
3826 if (phb
->ioda
.io_size
)
3827 pr_info(" IO: 0x%x [segment=0x%x]\n",
3828 phb
->ioda
.io_size
, phb
->ioda
.io_segsize
);
3831 phb
->hose
->ops
= &pnv_pci_ops
;
3832 phb
->get_pe_state
= pnv_ioda_get_pe_state
;
3833 phb
->freeze_pe
= pnv_ioda_freeze_pe
;
3834 phb
->unfreeze_pe
= pnv_ioda_unfreeze_pe
;
3836 /* Setup MSI support */
3837 pnv_pci_init_ioda_msis(phb
);
3840 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3841 * to let the PCI core do resource assignment. It's supposed
3842 * that the PCI core will do correct I/O and MMIO alignment
3843 * for the P2P bridge bars so that each PCI bus (excluding
3844 * the child P2P bridges) can form individual PE.
3846 ppc_md
.pcibios_fixup
= pnv_pci_ioda_fixup
;
3848 switch (phb
->type
) {
3849 case PNV_PHB_NPU_NVLINK
:
3850 hose
->controller_ops
= pnv_npu_ioda_controller_ops
;
3852 case PNV_PHB_NPU_OCAPI
:
3853 hose
->controller_ops
= pnv_npu_ocapi_ioda_controller_ops
;
3856 phb
->dma_dev_setup
= pnv_pci_ioda_dma_dev_setup
;
3857 hose
->controller_ops
= pnv_pci_ioda_controller_ops
;
3860 ppc_md
.pcibios_default_alignment
= pnv_pci_default_alignment
;
3862 #ifdef CONFIG_PCI_IOV
3863 ppc_md
.pcibios_fixup_sriov
= pnv_pci_ioda_fixup_iov_resources
;
3864 ppc_md
.pcibios_iov_resource_alignment
= pnv_pci_iov_resource_alignment
;
3865 ppc_md
.pcibios_sriov_enable
= pnv_pcibios_sriov_enable
;
3866 ppc_md
.pcibios_sriov_disable
= pnv_pcibios_sriov_disable
;
3869 pci_add_flags(PCI_REASSIGN_ALL_RSRC
);
3871 /* Reset IODA tables to a clean state */
3872 rc
= opal_pci_reset(phb_id
, OPAL_RESET_PCI_IODA_TABLE
, OPAL_ASSERT_RESET
);
3874 pr_warn(" OPAL Error %ld performing IODA table reset !\n", rc
);
3877 * If we're running in kdump kernel, the previous kernel never
3878 * shutdown PCI devices correctly. We already got IODA table
3879 * cleaned out. So we have to issue PHB reset to stop all PCI
3880 * transactions from previous kernel. The ppc_pci_reset_phbs
3881 * kernel parameter will force this reset too. Additionally,
3882 * if the IODA reset above failed then use a bigger hammer.
3883 * This can happen if we get a PHB fatal error in very early
3886 if (is_kdump_kernel() || pci_reset_phbs
|| rc
) {
3887 pr_info(" Issue PHB reset ...\n");
3888 pnv_eeh_phb_reset(hose
, EEH_RESET_FUNDAMENTAL
);
3889 pnv_eeh_phb_reset(hose
, EEH_RESET_DEACTIVATE
);
3892 /* Remove M64 resource if we can't configure it successfully */
3893 if (!phb
->init_m64
|| phb
->init_m64(phb
))
3894 hose
->mem_resources
[1].flags
= 0;
3897 void __init
pnv_pci_init_ioda2_phb(struct device_node
*np
)
3899 pnv_pci_init_ioda_phb(np
, 0, PNV_PHB_IODA2
);
3902 void __init
pnv_pci_init_npu_phb(struct device_node
*np
)
3904 pnv_pci_init_ioda_phb(np
, 0, PNV_PHB_NPU_NVLINK
);
3907 void __init
pnv_pci_init_npu2_opencapi_phb(struct device_node
*np
)
3909 pnv_pci_init_ioda_phb(np
, 0, PNV_PHB_NPU_OCAPI
);
3912 static void pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev
*dev
)
3914 struct pci_controller
*hose
= pci_bus_to_host(dev
->bus
);
3915 struct pnv_phb
*phb
= hose
->private_data
;
3917 if (!machine_is(powernv
))
3920 if (phb
->type
== PNV_PHB_NPU_OCAPI
)
3921 dev
->cfg_size
= PCI_CFG_SPACE_EXP_SIZE
;
3923 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID
, PCI_ANY_ID
, pnv_npu2_opencapi_cfg_size_fixup
);
3925 void __init
pnv_pci_init_ioda_hub(struct device_node
*np
)
3927 struct device_node
*phbn
;
3928 const __be64
*prop64
;
3931 pr_info("Probing IODA IO-Hub %pOF\n", np
);
3933 prop64
= of_get_property(np
, "ibm,opal-hubid", NULL
);
3935 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3938 hub_id
= be64_to_cpup(prop64
);
3939 pr_devel(" HUB-ID : 0x%016llx\n", hub_id
);
3941 /* Count child PHBs */
3942 for_each_child_of_node(np
, phbn
) {
3943 /* Look for IODA1 PHBs */
3944 if (of_device_is_compatible(phbn
, "ibm,ioda-phb"))
3945 pnv_pci_init_ioda_phb(phbn
, hub_id
, PNV_PHB_IODA1
);