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>
23 #include <linux/debugfs.h>
25 #include <asm/sections.h>
28 #include <asm/pci-bridge.h>
29 #include <asm/machdep.h>
30 #include <asm/msi_bitmap.h>
31 #include <asm/ppc-pci.h>
33 #include <asm/iommu.h>
36 #include <asm/firmware.h>
37 #include <asm/pnv-pci.h>
38 #include <asm/mmzone.h>
41 #include <misc/cxl-base.h>
45 #include "../../../../drivers/pci/pci.h"
47 #define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */
48 #define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */
49 #define PNV_IODA1_DMA32_SEGSIZE 0x10000000
51 static const char * const pnv_phb_names[] = { "IODA1", "IODA2", "NPU_OCAPI" };
53 static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
54 static void pnv_pci_configure_bus(struct pci_bus *bus);
56 void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
68 if (pe->flags & PNV_IODA_PE_DEV)
69 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
70 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
71 sprintf(pfix, "%04x:%02x ",
72 pci_domain_nr(pe->pbus), pe->pbus->number);
74 else if (pe->flags & PNV_IODA_PE_VF)
75 sprintf(pfix, "%04x:%02x:%2x.%d",
76 pci_domain_nr(pe->parent_dev->bus),
77 (pe->rid & 0xff00) >> 8,
78 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
79 #endif /* CONFIG_PCI_IOV*/
81 printk("%spci %s: [PE# %.2x] %pV",
82 level, pfix, pe->pe_number, &vaf);
87 static bool pnv_iommu_bypass_disabled __read_mostly;
88 static bool pci_reset_phbs __read_mostly;
90 static int __init iommu_setup(char *str)
96 if (!strncmp(str, "nobypass", 8)) {
97 pnv_iommu_bypass_disabled = true;
98 pr_info("PowerNV: IOMMU bypass window disabled.\n");
101 str += strcspn(str, ",");
108 early_param("iommu", iommu_setup);
110 static int __init pci_reset_phbs_setup(char *str)
112 pci_reset_phbs = true;
116 early_param("ppc_pci_reset_phbs", pci_reset_phbs_setup);
118 static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
122 phb->ioda.pe_array[pe_no].phb = phb;
123 phb->ioda.pe_array[pe_no].pe_number = pe_no;
124 phb->ioda.pe_array[pe_no].dma_setup_done = false;
127 * Clear the PE frozen state as it might be put into frozen state
128 * in the last PCI remove path. It's not harmful to do so when the
129 * PE is already in unfrozen state.
131 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
132 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
133 if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
134 pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n",
135 __func__, rc, phb->hose->global_number, pe_no);
137 return &phb->ioda.pe_array[pe_no];
140 static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
142 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
143 pr_warn("%s: Invalid PE %x on PHB#%x\n",
144 __func__, pe_no, phb->hose->global_number);
148 mutex_lock(&phb->ioda.pe_alloc_mutex);
149 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
150 pr_debug("%s: PE %x was reserved on PHB#%x\n",
151 __func__, pe_no, phb->hose->global_number);
152 mutex_unlock(&phb->ioda.pe_alloc_mutex);
154 pnv_ioda_init_pe(phb, pe_no);
157 struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb, int count)
159 struct pnv_ioda_pe *ret = NULL;
162 mutex_lock(&phb->ioda.pe_alloc_mutex);
164 /* scan backwards for a run of @count cleared bits */
165 for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
166 if (test_bit(pe, phb->ioda.pe_alloc)) {
178 for (i = pe; i < pe + count; i++) {
179 set_bit(i, phb->ioda.pe_alloc);
180 pnv_ioda_init_pe(phb, i);
182 ret = &phb->ioda.pe_array[pe];
185 mutex_unlock(&phb->ioda.pe_alloc_mutex);
189 void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
191 struct pnv_phb *phb = pe->phb;
192 unsigned int pe_num = pe->pe_number;
195 memset(pe, 0, sizeof(struct pnv_ioda_pe));
197 mutex_lock(&phb->ioda.pe_alloc_mutex);
198 clear_bit(pe_num, phb->ioda.pe_alloc);
199 mutex_unlock(&phb->ioda.pe_alloc_mutex);
202 /* The default M64 BAR is shared by all PEs */
203 static int pnv_ioda2_init_m64(struct pnv_phb *phb)
209 /* Configure the default M64 BAR */
210 rc = opal_pci_set_phb_mem_window(phb->opal_id,
211 OPAL_M64_WINDOW_TYPE,
212 phb->ioda.m64_bar_idx,
216 if (rc != OPAL_SUCCESS) {
217 desc = "configuring";
221 /* Enable the default M64 BAR */
222 rc = opal_pci_phb_mmio_enable(phb->opal_id,
223 OPAL_M64_WINDOW_TYPE,
224 phb->ioda.m64_bar_idx,
225 OPAL_ENABLE_M64_SPLIT);
226 if (rc != OPAL_SUCCESS) {
232 * Exclude the segments for reserved and root bus PE, which
233 * are first or last two PEs.
235 r = &phb->hose->mem_resources[1];
236 if (phb->ioda.reserved_pe_idx == 0)
237 r->start += (2 * phb->ioda.m64_segsize);
238 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
239 r->end -= (2 * phb->ioda.m64_segsize);
241 pr_warn(" Cannot strip M64 segment for reserved PE#%x\n",
242 phb->ioda.reserved_pe_idx);
247 pr_warn(" Failure %lld %s M64 BAR#%d\n",
248 rc, desc, phb->ioda.m64_bar_idx);
249 opal_pci_phb_mmio_enable(phb->opal_id,
250 OPAL_M64_WINDOW_TYPE,
251 phb->ioda.m64_bar_idx,
256 static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
257 unsigned long *pe_bitmap)
259 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
261 resource_size_t base, sgsz, start, end;
264 base = phb->ioda.m64_base;
265 sgsz = phb->ioda.m64_segsize;
266 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
267 r = &pdev->resource[i];
268 if (!r->parent || !pnv_pci_is_m64(phb, r))
271 start = ALIGN_DOWN(r->start - base, sgsz);
272 end = ALIGN(r->end - base, sgsz);
273 for (segno = start / sgsz; segno < end / sgsz; segno++) {
275 set_bit(segno, pe_bitmap);
277 pnv_ioda_reserve_pe(phb, segno);
282 static int pnv_ioda1_init_m64(struct pnv_phb *phb)
288 * There are 16 M64 BARs, each of which has 8 segments. So
289 * there are as many M64 segments as the maximum number of
292 for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
293 unsigned long base, segsz = phb->ioda.m64_segsize;
296 base = phb->ioda.m64_base +
297 index * PNV_IODA1_M64_SEGS * segsz;
298 rc = opal_pci_set_phb_mem_window(phb->opal_id,
299 OPAL_M64_WINDOW_TYPE, index, base, 0,
300 PNV_IODA1_M64_SEGS * segsz);
301 if (rc != OPAL_SUCCESS) {
302 pr_warn(" Error %lld setting M64 PHB#%x-BAR#%d\n",
303 rc, phb->hose->global_number, index);
307 rc = opal_pci_phb_mmio_enable(phb->opal_id,
308 OPAL_M64_WINDOW_TYPE, index,
309 OPAL_ENABLE_M64_SPLIT);
310 if (rc != OPAL_SUCCESS) {
311 pr_warn(" Error %lld enabling M64 PHB#%x-BAR#%d\n",
312 rc, phb->hose->global_number, index);
317 for (index = 0; index < phb->ioda.total_pe_num; index++) {
321 * P7IOC supports M64DT, which helps mapping M64 segment
322 * to one particular PE#. However, PHB3 has fixed mapping
323 * between M64 segment and PE#. In order to have same logic
324 * for P7IOC and PHB3, we enforce fixed mapping between M64
325 * segment and PE# on P7IOC.
327 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
328 index, OPAL_M64_WINDOW_TYPE,
329 index / PNV_IODA1_M64_SEGS,
330 index % PNV_IODA1_M64_SEGS);
331 if (rc != OPAL_SUCCESS) {
332 pr_warn("%s: Error %lld mapping M64 for PHB#%x-PE#%x\n",
333 __func__, rc, phb->hose->global_number,
340 * Exclude the segments for reserved and root bus PE, which
341 * are first or last two PEs.
343 r = &phb->hose->mem_resources[1];
344 if (phb->ioda.reserved_pe_idx == 0)
345 r->start += (2 * phb->ioda.m64_segsize);
346 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
347 r->end -= (2 * phb->ioda.m64_segsize);
349 WARN(1, "Wrong reserved PE#%x on PHB#%x\n",
350 phb->ioda.reserved_pe_idx, phb->hose->global_number);
355 for ( ; index >= 0; index--)
356 opal_pci_phb_mmio_enable(phb->opal_id,
357 OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
362 static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
363 unsigned long *pe_bitmap,
366 struct pci_dev *pdev;
368 list_for_each_entry(pdev, &bus->devices, bus_list) {
369 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
371 if (all && pdev->subordinate)
372 pnv_ioda_reserve_m64_pe(pdev->subordinate,
377 static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
379 struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
380 struct pnv_ioda_pe *master_pe, *pe;
381 unsigned long size, *pe_alloc;
384 /* Root bus shouldn't use M64 */
385 if (pci_is_root_bus(bus))
388 /* Allocate bitmap */
389 size = ALIGN(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
390 pe_alloc = kzalloc(size, GFP_KERNEL);
392 pr_warn("%s: Out of memory !\n",
397 /* Figure out reserved PE numbers by the PE */
398 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
401 * the current bus might not own M64 window and that's all
402 * contributed by its child buses. For the case, we needn't
403 * pick M64 dependent PE#.
405 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
411 * Figure out the master PE and put all slave PEs to master
412 * PE's list to form compound PE.
416 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
417 phb->ioda.total_pe_num) {
418 pe = &phb->ioda.pe_array[i];
420 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
422 pe->flags |= PNV_IODA_PE_MASTER;
423 INIT_LIST_HEAD(&pe->slaves);
426 pe->flags |= PNV_IODA_PE_SLAVE;
427 pe->master = master_pe;
428 list_add_tail(&pe->list, &master_pe->slaves);
436 static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
438 struct pci_controller *hose = phb->hose;
439 struct device_node *dn = hose->dn;
440 struct resource *res;
445 if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
446 pr_info(" Not support M64 window\n");
450 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
451 pr_info(" Firmware too old to support M64 window\n");
455 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
457 pr_info(" No <ibm,opal-m64-window> on %pOF\n",
463 * Find the available M64 BAR range and pickup the last one for
464 * covering the whole 64-bits space. We support only one range.
466 if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges",
468 /* In absence of the property, assume 0..15 */
472 /* We only support 64 bits in our allocator */
473 if (m64_range[1] > 63) {
474 pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",
475 __func__, m64_range[1], phb->hose->global_number);
478 /* Empty range, no m64 */
479 if (m64_range[1] <= m64_range[0]) {
480 pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",
481 __func__, phb->hose->global_number);
485 /* Configure M64 informations */
486 res = &hose->mem_resources[1];
487 res->name = dn->full_name;
488 res->start = of_translate_address(dn, r + 2);
489 res->end = res->start + of_read_number(r + 4, 2) - 1;
490 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
491 pci_addr = of_read_number(r, 2);
492 hose->mem_offset[1] = res->start - pci_addr;
494 phb->ioda.m64_size = resource_size(res);
495 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
496 phb->ioda.m64_base = pci_addr;
498 /* This lines up nicely with the display from processing OF ranges */
499 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",
500 res->start, res->end, pci_addr, m64_range[0],
501 m64_range[0] + m64_range[1] - 1);
503 /* Mark all M64 used up by default */
504 phb->ioda.m64_bar_alloc = (unsigned long)-1;
506 /* Use last M64 BAR to cover M64 window */
508 phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1];
510 pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx);
512 /* Mark remaining ones free */
513 for (i = m64_range[0]; i < m64_range[1]; i++)
514 clear_bit(i, &phb->ioda.m64_bar_alloc);
517 * Setup init functions for M64 based on IODA version, IODA3 uses
520 if (phb->type == PNV_PHB_IODA1)
521 phb->init_m64 = pnv_ioda1_init_m64;
523 phb->init_m64 = pnv_ioda2_init_m64;
526 static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
528 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
529 struct pnv_ioda_pe *slave;
532 /* Fetch master PE */
533 if (pe->flags & PNV_IODA_PE_SLAVE) {
535 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
538 pe_no = pe->pe_number;
541 /* Freeze master PE */
542 rc = opal_pci_eeh_freeze_set(phb->opal_id,
544 OPAL_EEH_ACTION_SET_FREEZE_ALL);
545 if (rc != OPAL_SUCCESS) {
546 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
547 __func__, rc, phb->hose->global_number, pe_no);
551 /* Freeze slave PEs */
552 if (!(pe->flags & PNV_IODA_PE_MASTER))
555 list_for_each_entry(slave, &pe->slaves, list) {
556 rc = opal_pci_eeh_freeze_set(phb->opal_id,
558 OPAL_EEH_ACTION_SET_FREEZE_ALL);
559 if (rc != OPAL_SUCCESS)
560 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
561 __func__, rc, phb->hose->global_number,
566 static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
568 struct pnv_ioda_pe *pe, *slave;
572 pe = &phb->ioda.pe_array[pe_no];
573 if (pe->flags & PNV_IODA_PE_SLAVE) {
575 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
576 pe_no = pe->pe_number;
579 /* Clear frozen state for master PE */
580 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
581 if (rc != OPAL_SUCCESS) {
582 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
583 __func__, rc, opt, phb->hose->global_number, pe_no);
587 if (!(pe->flags & PNV_IODA_PE_MASTER))
590 /* Clear frozen state for slave PEs */
591 list_for_each_entry(slave, &pe->slaves, list) {
592 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
595 if (rc != OPAL_SUCCESS) {
596 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
597 __func__, rc, opt, phb->hose->global_number,
606 static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
608 struct pnv_ioda_pe *slave, *pe;
609 u8 fstate = 0, state;
613 /* Sanity check on PE number */
614 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
615 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
618 * Fetch the master PE and the PE instance might be
619 * not initialized yet.
621 pe = &phb->ioda.pe_array[pe_no];
622 if (pe->flags & PNV_IODA_PE_SLAVE) {
624 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
625 pe_no = pe->pe_number;
628 /* Check the master PE */
629 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
630 &state, &pcierr, NULL);
631 if (rc != OPAL_SUCCESS) {
632 pr_warn("%s: Failure %lld getting "
633 "PHB#%x-PE#%x state\n",
635 phb->hose->global_number, pe_no);
636 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
639 /* Check the slave PE */
640 if (!(pe->flags & PNV_IODA_PE_MASTER))
643 list_for_each_entry(slave, &pe->slaves, list) {
644 rc = opal_pci_eeh_freeze_status(phb->opal_id,
649 if (rc != OPAL_SUCCESS) {
650 pr_warn("%s: Failure %lld getting "
651 "PHB#%x-PE#%x state\n",
653 phb->hose->global_number, slave->pe_number);
654 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
658 * Override the result based on the ascending
668 struct pnv_ioda_pe *pnv_pci_bdfn_to_pe(struct pnv_phb *phb, u16 bdfn)
670 int pe_number = phb->ioda.pe_rmap[bdfn];
672 if (pe_number == IODA_INVALID_PE)
675 return &phb->ioda.pe_array[pe_number];
678 struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
680 struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
681 struct pci_dn *pdn = pci_get_pdn(dev);
685 if (pdn->pe_number == IODA_INVALID_PE)
687 return &phb->ioda.pe_array[pdn->pe_number];
690 static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
691 struct pnv_ioda_pe *parent,
692 struct pnv_ioda_pe *child,
695 const char *desc = is_add ? "adding" : "removing";
696 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
697 OPAL_REMOVE_PE_FROM_DOMAIN;
698 struct pnv_ioda_pe *slave;
701 /* Parent PE affects child PE */
702 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
703 child->pe_number, op);
704 if (rc != OPAL_SUCCESS) {
705 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
710 if (!(child->flags & PNV_IODA_PE_MASTER))
713 /* Compound case: parent PE affects slave PEs */
714 list_for_each_entry(slave, &child->slaves, list) {
715 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
716 slave->pe_number, op);
717 if (rc != OPAL_SUCCESS) {
718 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
727 static int pnv_ioda_set_peltv(struct pnv_phb *phb,
728 struct pnv_ioda_pe *pe,
731 struct pnv_ioda_pe *slave;
732 struct pci_dev *pdev = NULL;
736 * Clear PE frozen state. If it's master PE, we need
737 * clear slave PE frozen state as well.
740 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
741 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
742 if (pe->flags & PNV_IODA_PE_MASTER) {
743 list_for_each_entry(slave, &pe->slaves, list)
744 opal_pci_eeh_freeze_clear(phb->opal_id,
746 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
751 * Associate PE in PELT. We need add the PE into the
752 * corresponding PELT-V as well. Otherwise, the error
753 * originated from the PE might contribute to other
756 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
760 /* For compound PEs, any one affects all of them */
761 if (pe->flags & PNV_IODA_PE_MASTER) {
762 list_for_each_entry(slave, &pe->slaves, list) {
763 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
769 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
770 pdev = pe->pbus->self;
771 else if (pe->flags & PNV_IODA_PE_DEV)
772 pdev = pe->pdev->bus->self;
773 #ifdef CONFIG_PCI_IOV
774 else if (pe->flags & PNV_IODA_PE_VF)
775 pdev = pe->parent_dev;
776 #endif /* CONFIG_PCI_IOV */
778 struct pci_dn *pdn = pci_get_pdn(pdev);
779 struct pnv_ioda_pe *parent;
781 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
782 parent = &phb->ioda.pe_array[pdn->pe_number];
783 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
788 pdev = pdev->bus->self;
794 static void pnv_ioda_unset_peltv(struct pnv_phb *phb,
795 struct pnv_ioda_pe *pe,
796 struct pci_dev *parent)
801 struct pci_dn *pdn = pci_get_pdn(parent);
803 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
804 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
806 OPAL_REMOVE_PE_FROM_DOMAIN);
807 /* XXX What to do in case of error ? */
809 parent = parent->bus->self;
812 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
813 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
815 /* Disassociate PE in PELT */
816 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
817 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
819 pe_warn(pe, "OPAL error %lld remove self from PELTV\n", rc);
822 int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
824 struct pci_dev *parent;
825 uint8_t bcomp, dcomp, fcomp;
829 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
833 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
834 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
835 parent = pe->pbus->self;
836 if (pe->flags & PNV_IODA_PE_BUS_ALL)
837 count = resource_size(&pe->pbus->busn_res);
842 case 1: bcomp = OpalPciBusAll; break;
843 case 2: bcomp = OpalPciBus7Bits; break;
844 case 4: bcomp = OpalPciBus6Bits; break;
845 case 8: bcomp = OpalPciBus5Bits; break;
846 case 16: bcomp = OpalPciBus4Bits; break;
847 case 32: bcomp = OpalPciBus3Bits; break;
849 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
851 /* Do an exact match only */
852 bcomp = OpalPciBusAll;
854 rid_end = pe->rid + (count << 8);
856 #ifdef CONFIG_PCI_IOV
857 if (pe->flags & PNV_IODA_PE_VF)
858 parent = pe->parent_dev;
861 parent = pe->pdev->bus->self;
862 bcomp = OpalPciBusAll;
863 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
864 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
865 rid_end = pe->rid + 1;
868 /* Clear the reverse map */
869 for (rid = pe->rid; rid < rid_end; rid++)
870 phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
873 * Release from all parents PELT-V. NPUs don't have a PELTV
876 if (phb->type != PNV_PHB_NPU_OCAPI)
877 pnv_ioda_unset_peltv(phb, pe, parent);
879 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
880 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
882 pe_err(pe, "OPAL error %lld trying to setup PELT table\n", rc);
886 #ifdef CONFIG_PCI_IOV
887 pe->parent_dev = NULL;
893 int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
895 uint8_t bcomp, dcomp, fcomp;
896 long rc, rid_end, rid;
898 /* Bus validation ? */
902 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
903 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
904 if (pe->flags & PNV_IODA_PE_BUS_ALL)
905 count = resource_size(&pe->pbus->busn_res);
910 case 1: bcomp = OpalPciBusAll; break;
911 case 2: bcomp = OpalPciBus7Bits; break;
912 case 4: bcomp = OpalPciBus6Bits; break;
913 case 8: bcomp = OpalPciBus5Bits; break;
914 case 16: bcomp = OpalPciBus4Bits; break;
915 case 32: bcomp = OpalPciBus3Bits; break;
917 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
919 /* Do an exact match only */
920 bcomp = OpalPciBusAll;
922 rid_end = pe->rid + (count << 8);
924 bcomp = OpalPciBusAll;
925 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
926 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
927 rid_end = pe->rid + 1;
931 * Associate PE in PELT. We need add the PE into the
932 * corresponding PELT-V as well. Otherwise, the error
933 * originated from the PE might contribute to other
936 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
937 bcomp, dcomp, fcomp, OPAL_MAP_PE);
939 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
944 * Configure PELTV. NPUs don't have a PELTV table so skip
945 * configuration on them.
947 if (phb->type != PNV_PHB_NPU_OCAPI)
948 pnv_ioda_set_peltv(phb, pe, true);
950 /* Setup reverse map */
951 for (rid = pe->rid; rid < rid_end; rid++)
952 phb->ioda.pe_rmap[rid] = pe->pe_number;
954 /* Setup one MVTs on IODA1 */
955 if (phb->type != PNV_PHB_IODA1) {
960 pe->mve_number = pe->pe_number;
961 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
962 if (rc != OPAL_SUCCESS) {
963 pe_err(pe, "OPAL error %ld setting up MVE %x\n",
967 rc = opal_pci_set_mve_enable(phb->opal_id,
968 pe->mve_number, OPAL_ENABLE_MVE);
970 pe_err(pe, "OPAL error %ld enabling MVE %x\n",
980 static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
982 struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
983 struct pci_dn *pdn = pci_get_pdn(dev);
984 struct pnv_ioda_pe *pe;
987 pr_err("%s: Device tree node not associated properly\n",
991 if (pdn->pe_number != IODA_INVALID_PE)
994 pe = pnv_ioda_alloc_pe(phb, 1);
996 pr_warn("%s: Not enough PE# available, disabling device\n",
1001 /* NOTE: We don't get a reference for the pointer in the PE
1002 * data structure, both the device and PE structures should be
1003 * destroyed at the same time.
1005 * At some point we want to remove the PDN completely anyways
1007 pdn->pe_number = pe->pe_number;
1008 pe->flags = PNV_IODA_PE_DEV;
1011 pe->mve_number = -1;
1012 pe->rid = dev->bus->number << 8 | pdn->devfn;
1015 pe_info(pe, "Associated device to PE\n");
1017 if (pnv_ioda_configure_pe(phb, pe)) {
1018 /* XXX What do we do here ? */
1019 pnv_ioda_free_pe(pe);
1020 pdn->pe_number = IODA_INVALID_PE;
1025 /* Put PE to the list */
1026 mutex_lock(&phb->ioda.pe_list_mutex);
1027 list_add_tail(&pe->list, &phb->ioda.pe_list);
1028 mutex_unlock(&phb->ioda.pe_list_mutex);
1033 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1034 * single PCI bus. Another one that contains the primary PCI bus and its
1035 * subordinate PCI devices and buses. The second type of PE is normally
1036 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1038 static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
1040 struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
1041 struct pnv_ioda_pe *pe = NULL;
1042 unsigned int pe_num;
1045 * In partial hotplug case, the PE instance might be still alive.
1046 * We should reuse it instead of allocating a new one.
1048 pe_num = phb->ioda.pe_rmap[bus->number << 8];
1049 if (WARN_ON(pe_num != IODA_INVALID_PE)) {
1050 pe = &phb->ioda.pe_array[pe_num];
1054 /* PE number for root bus should have been reserved */
1055 if (pci_is_root_bus(bus))
1056 pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
1058 /* Check if PE is determined by M64 */
1060 pe = pnv_ioda_pick_m64_pe(bus, all);
1062 /* The PE number isn't pinned by M64 */
1064 pe = pnv_ioda_alloc_pe(phb, 1);
1067 pr_warn("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1068 __func__, pci_domain_nr(bus), bus->number);
1072 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
1075 pe->mve_number = -1;
1076 pe->rid = bus->busn_res.start << 8;
1079 pe_info(pe, "Secondary bus %pad..%pad associated with PE#%x\n",
1080 &bus->busn_res.start, &bus->busn_res.end,
1083 pe_info(pe, "Secondary bus %pad associated with PE#%x\n",
1084 &bus->busn_res.start, pe->pe_number);
1086 if (pnv_ioda_configure_pe(phb, pe)) {
1087 /* XXX What do we do here ? */
1088 pnv_ioda_free_pe(pe);
1093 /* Put PE to the list */
1094 list_add_tail(&pe->list, &phb->ioda.pe_list);
1099 static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
1100 struct pnv_ioda_pe *pe);
1102 static void pnv_pci_ioda_dma_dev_setup(struct pci_dev *pdev)
1104 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
1105 struct pci_dn *pdn = pci_get_pdn(pdev);
1106 struct pnv_ioda_pe *pe;
1108 /* Check if the BDFN for this device is associated with a PE yet */
1109 pe = pnv_pci_bdfn_to_pe(phb, pdev->devfn | (pdev->bus->number << 8));
1111 /* VF PEs should be pre-configured in pnv_pci_sriov_enable() */
1112 if (WARN_ON(pdev->is_virtfn))
1115 pnv_pci_configure_bus(pdev->bus);
1116 pe = pnv_pci_bdfn_to_pe(phb, pdev->devfn | (pdev->bus->number << 8));
1117 pci_info(pdev, "Configured PE#%x\n", pe ? pe->pe_number : 0xfffff);
1121 * If we can't setup the IODA PE something has gone horribly
1122 * wrong and we can't enable DMA for the device.
1127 pci_info(pdev, "Added to existing PE#%x\n", pe->pe_number);
1131 * We assume that bridges *probably* don't need to do any DMA so we can
1132 * skip allocating a TCE table, etc unless we get a non-bridge device.
1134 if (!pe->dma_setup_done && !pci_is_bridge(pdev)) {
1135 switch (phb->type) {
1137 pnv_pci_ioda1_setup_dma_pe(phb, pe);
1140 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1143 pr_warn("%s: No DMA for PHB#%x (type %d)\n",
1144 __func__, phb->hose->global_number, phb->type);
1149 pdn->pe_number = pe->pe_number;
1152 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
1153 pdev->dev.archdata.dma_offset = pe->tce_bypass_base;
1154 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
1156 /* PEs with a DMA weight of zero won't have a group */
1157 if (pe->table_group.group)
1158 iommu_add_device(&pe->table_group, &pdev->dev);
1162 * Reconfigure TVE#0 to be usable as 64-bit DMA space.
1164 * The first 4GB of virtual memory for a PE is reserved for 32-bit accesses.
1165 * Devices can only access more than that if bit 59 of the PCI address is set
1166 * by hardware, which indicates TVE#1 should be used instead of TVE#0.
1167 * Many PCI devices are not capable of addressing that many bits, and as a
1168 * result are limited to the 4GB of virtual memory made available to 32-bit
1171 * In order to work around this, reconfigure TVE#0 to be suitable for 64-bit
1172 * devices by configuring the virtual memory past the first 4GB inaccessible
1173 * by 64-bit DMAs. This should only be used by devices that want more than
1174 * 4GB, and only on PEs that have no 32-bit devices.
1176 * Currently this will only work on PHB3 (POWER8).
1178 static int pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe *pe)
1180 u64 window_size, table_size, tce_count, addr;
1181 struct page *table_pages;
1182 u64 tce_order = 28; /* 256MB TCEs */
1187 * Window size needs to be a power of two, but needs to account for
1188 * shifting memory by the 4GB offset required to skip 32bit space.
1190 window_size = roundup_pow_of_two(memory_hotplug_max() + (1ULL << 32));
1191 tce_count = window_size >> tce_order;
1192 table_size = tce_count << 3;
1194 if (table_size < PAGE_SIZE)
1195 table_size = PAGE_SIZE;
1197 table_pages = alloc_pages_node(pe->phb->hose->node, GFP_KERNEL,
1198 get_order(table_size));
1202 tces = page_address(table_pages);
1206 memset(tces, 0, table_size);
1208 for (addr = 0; addr < memory_hotplug_max(); addr += (1 << tce_order)) {
1209 tces[(addr + (1ULL << 32)) >> tce_order] =
1210 cpu_to_be64(addr | TCE_PCI_READ | TCE_PCI_WRITE);
1213 rc = opal_pci_map_pe_dma_window(pe->phb->opal_id,
1215 /* reconfigure window 0 */
1216 (pe->pe_number << 1) + 0,
1221 if (rc == OPAL_SUCCESS) {
1222 pe_info(pe, "Using 64-bit DMA iommu bypass (through TVE#0)\n");
1226 pe_err(pe, "Error configuring 64-bit DMA bypass\n");
1230 static bool pnv_pci_ioda_iommu_bypass_supported(struct pci_dev *pdev,
1233 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
1234 struct pci_dn *pdn = pci_get_pdn(pdev);
1235 struct pnv_ioda_pe *pe;
1237 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1240 pe = &phb->ioda.pe_array[pdn->pe_number];
1241 if (pe->tce_bypass_enabled) {
1242 u64 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1243 if (dma_mask >= top)
1248 * If the device can't set the TCE bypass bit but still wants
1249 * to access 4GB or more, on PHB3 we can reconfigure TVE#0 to
1250 * bypass the 32-bit region and be usable for 64-bit DMAs.
1251 * The device needs to be able to address all of this space.
1253 if (dma_mask >> 32 &&
1254 dma_mask > (memory_hotplug_max() + (1ULL << 32)) &&
1255 /* pe->pdev should be set if it's a single device, pe->pbus if not */
1256 (pe->device_count == 1 || !pe->pbus) &&
1257 phb->model == PNV_PHB_MODEL_PHB3) {
1258 /* Configure the bypass mode */
1259 s64 rc = pnv_pci_ioda_dma_64bit_bypass(pe);
1262 /* 4GB offset bypasses 32-bit space */
1263 pdev->dev.archdata.dma_offset = (1ULL << 32);
1270 static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb,
1273 return real_mode ? (__be64 __iomem *)(phb->regs_phys + 0x210) :
1274 (phb->regs + 0x210);
1277 static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table *tbl,
1278 unsigned long index, unsigned long npages, bool rm)
1280 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1281 &tbl->it_group_list, struct iommu_table_group_link,
1283 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1284 struct pnv_ioda_pe, table_group);
1285 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
1286 unsigned long start, end, inc;
1288 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1289 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1292 /* p7ioc-style invalidation, 2 TCEs per write */
1293 start |= (1ull << 63);
1294 end |= (1ull << 63);
1296 end |= inc - 1; /* round up end to be different than start */
1298 mb(); /* Ensure above stores are visible */
1299 while (start <= end) {
1301 __raw_rm_writeq_be(start, invalidate);
1303 __raw_writeq_be(start, invalidate);
1309 * The iommu layer will do another mb() for us on build()
1310 * and we don't care on free()
1314 static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1315 long npages, unsigned long uaddr,
1316 enum dma_data_direction direction,
1317 unsigned long attrs)
1319 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1323 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
1328 #ifdef CONFIG_IOMMU_API
1329 /* Common for IODA1 and IODA2 */
1330 static int pnv_ioda_tce_xchg_no_kill(struct iommu_table *tbl, long index,
1331 unsigned long *hpa, enum dma_data_direction *direction,
1334 return pnv_tce_xchg(tbl, index, hpa, direction, !realmode);
1338 static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1341 pnv_tce_free(tbl, index, npages);
1343 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
1346 static struct iommu_table_ops pnv_ioda1_iommu_ops = {
1347 .set = pnv_ioda1_tce_build,
1348 #ifdef CONFIG_IOMMU_API
1349 .xchg_no_kill = pnv_ioda_tce_xchg_no_kill,
1350 .tce_kill = pnv_pci_p7ioc_tce_invalidate,
1351 .useraddrptr = pnv_tce_useraddrptr,
1353 .clear = pnv_ioda1_tce_free,
1357 #define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)
1358 #define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)
1359 #define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)
1361 static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)
1363 /* 01xb - invalidate TCEs that match the specified PE# */
1364 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, false);
1365 unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
1367 mb(); /* Ensure above stores are visible */
1368 __raw_writeq_be(val, invalidate);
1371 static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe, bool rm,
1372 unsigned shift, unsigned long index,
1373 unsigned long npages)
1375 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
1376 unsigned long start, end, inc;
1378 /* We'll invalidate DMA address in PE scope */
1379 start = PHB3_TCE_KILL_INVAL_ONE;
1380 start |= (pe->pe_number & 0xFF);
1383 /* Figure out the start, end and step */
1384 start |= (index << shift);
1385 end |= ((index + npages - 1) << shift);
1386 inc = (0x1ull << shift);
1389 while (start <= end) {
1391 __raw_rm_writeq_be(start, invalidate);
1393 __raw_writeq_be(start, invalidate);
1398 static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
1400 struct pnv_phb *phb = pe->phb;
1402 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1403 pnv_pci_phb3_tce_invalidate_pe(pe);
1405 opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE,
1406 pe->pe_number, 0, 0, 0);
1409 static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1410 unsigned long index, unsigned long npages, bool rm)
1412 struct iommu_table_group_link *tgl;
1414 list_for_each_entry_lockless(tgl, &tbl->it_group_list, next) {
1415 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1416 struct pnv_ioda_pe, table_group);
1417 struct pnv_phb *phb = pe->phb;
1418 unsigned int shift = tbl->it_page_shift;
1420 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1421 pnv_pci_phb3_tce_invalidate(pe, rm, shift,
1424 opal_pci_tce_kill(phb->opal_id,
1425 OPAL_PCI_TCE_KILL_PAGES,
1426 pe->pe_number, 1u << shift,
1427 index << shift, npages);
1431 static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1432 long npages, unsigned long uaddr,
1433 enum dma_data_direction direction,
1434 unsigned long attrs)
1436 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1440 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1445 static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1448 pnv_tce_free(tbl, index, npages);
1450 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1453 static struct iommu_table_ops pnv_ioda2_iommu_ops = {
1454 .set = pnv_ioda2_tce_build,
1455 #ifdef CONFIG_IOMMU_API
1456 .xchg_no_kill = pnv_ioda_tce_xchg_no_kill,
1457 .tce_kill = pnv_pci_ioda2_tce_invalidate,
1458 .useraddrptr = pnv_tce_useraddrptr,
1460 .clear = pnv_ioda2_tce_free,
1462 .free = pnv_pci_ioda2_table_free_pages,
1465 static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data)
1467 unsigned int *weight = (unsigned int *)data;
1469 /* This is quite simplistic. The "base" weight of a device
1470 * is 10. 0 means no DMA is to be accounted for it.
1472 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
1475 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
1476 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
1477 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
1479 else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
1487 static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe)
1489 unsigned int weight = 0;
1491 /* SRIOV VF has same DMA32 weight as its PF */
1492 #ifdef CONFIG_PCI_IOV
1493 if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) {
1494 pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight);
1499 if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) {
1500 pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight);
1501 } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) {
1502 struct pci_dev *pdev;
1504 list_for_each_entry(pdev, &pe->pbus->devices, bus_list)
1505 pnv_pci_ioda_dev_dma_weight(pdev, &weight);
1506 } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) {
1507 pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight);
1513 static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
1514 struct pnv_ioda_pe *pe)
1517 struct page *tce_mem = NULL;
1518 struct iommu_table *tbl;
1519 unsigned int weight, total_weight = 0;
1520 unsigned int tce32_segsz, base, segs, avail, i;
1524 /* XXX FIXME: Handle 64-bit only DMA devices */
1525 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1526 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1527 weight = pnv_pci_ioda_pe_dma_weight(pe);
1531 pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight,
1533 segs = (weight * phb->ioda.dma32_count) / total_weight;
1538 * Allocate contiguous DMA32 segments. We begin with the expected
1539 * number of segments. With one more attempt, the number of DMA32
1540 * segments to be allocated is decreased by one until one segment
1541 * is allocated successfully.
1544 for (base = 0; base <= phb->ioda.dma32_count - segs; base++) {
1545 for (avail = 0, i = base; i < base + segs; i++) {
1546 if (phb->ioda.dma32_segmap[i] ==
1557 pe_warn(pe, "No available DMA32 segments\n");
1562 tbl = pnv_pci_table_alloc(phb->hose->node);
1566 iommu_register_group(&pe->table_group, phb->hose->global_number,
1568 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
1570 /* Grab a 32-bit TCE table */
1571 pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
1572 weight, total_weight, base, segs);
1573 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1574 base * PNV_IODA1_DMA32_SEGSIZE,
1575 (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1);
1577 /* XXX Currently, we allocate one big contiguous table for the
1578 * TCEs. We only really need one chunk per 256M of TCE space
1579 * (ie per segment) but that's an optimization for later, it
1580 * requires some added smarts with our get/put_tce implementation
1582 * Each TCE page is 4KB in size and each TCE entry occupies 8
1585 tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3);
1586 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1587 get_order(tce32_segsz * segs));
1589 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1592 addr = page_address(tce_mem);
1593 memset(addr, 0, tce32_segsz * segs);
1596 for (i = 0; i < segs; i++) {
1597 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1600 __pa(addr) + tce32_segsz * i,
1601 tce32_segsz, IOMMU_PAGE_SIZE_4K);
1603 pe_err(pe, " Failed to configure 32-bit TCE table, err %lld\n",
1609 /* Setup DMA32 segment mapping */
1610 for (i = base; i < base + segs; i++)
1611 phb->ioda.dma32_segmap[i] = pe->pe_number;
1613 /* Setup linux iommu table */
1614 pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs,
1615 base * PNV_IODA1_DMA32_SEGSIZE,
1616 IOMMU_PAGE_SHIFT_4K);
1618 tbl->it_ops = &pnv_ioda1_iommu_ops;
1619 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
1620 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
1621 tbl->it_index = (phb->hose->global_number << 16) | pe->pe_number;
1622 if (!iommu_init_table(tbl, phb->hose->node, 0, 0))
1623 panic("Failed to initialize iommu table");
1625 pe->dma_setup_done = true;
1628 /* XXX Failure: Try to fallback to 64-bit only ? */
1630 __free_pages(tce_mem, get_order(tce32_segsz * segs));
1632 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
1633 iommu_tce_table_put(tbl);
1637 static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
1638 int num, struct iommu_table *tbl)
1640 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1642 struct pnv_phb *phb = pe->phb;
1644 const unsigned long size = tbl->it_indirect_levels ?
1645 tbl->it_level_size : tbl->it_size;
1646 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
1647 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
1649 pe_info(pe, "Setting up window#%d %llx..%llx pg=%lx\n",
1650 num, start_addr, start_addr + win_size - 1,
1651 IOMMU_PAGE_SIZE(tbl));
1654 * Map TCE table through TVT. The TVE index is the PE number
1655 * shifted by 1 bit for 32-bits DMA space.
1657 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1659 (pe->pe_number << 1) + num,
1660 tbl->it_indirect_levels + 1,
1663 IOMMU_PAGE_SIZE(tbl));
1665 pe_err(pe, "Failed to configure TCE table, err %lld\n", rc);
1669 pnv_pci_link_table_and_group(phb->hose->node, num,
1670 tbl, &pe->table_group);
1671 pnv_pci_ioda2_tce_invalidate_pe(pe);
1676 static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
1678 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1681 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1683 phys_addr_t top = memblock_end_of_DRAM();
1685 top = roundup_pow_of_two(top);
1686 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1689 pe->tce_bypass_base,
1692 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1695 pe->tce_bypass_base,
1699 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1701 pe->tce_bypass_enabled = enable;
1704 static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
1705 int num, __u32 page_shift, __u64 window_size, __u32 levels,
1706 bool alloc_userspace_copy, struct iommu_table **ptbl)
1708 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1710 int nid = pe->phb->hose->node;
1711 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
1713 struct iommu_table *tbl;
1715 tbl = pnv_pci_table_alloc(nid);
1719 tbl->it_ops = &pnv_ioda2_iommu_ops;
1721 ret = pnv_pci_ioda2_table_alloc_pages(nid,
1722 bus_offset, page_shift, window_size,
1723 levels, alloc_userspace_copy, tbl);
1725 iommu_tce_table_put(tbl);
1734 static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
1736 struct iommu_table *tbl = NULL;
1738 unsigned long res_start, res_end;
1741 * crashkernel= specifies the kdump kernel's maximum memory at
1742 * some offset and there is no guaranteed the result is a power
1743 * of 2, which will cause errors later.
1745 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
1748 * In memory constrained environments, e.g. kdump kernel, the
1749 * DMA window can be larger than available memory, which will
1750 * cause errors later.
1752 const u64 maxblock = 1UL << (PAGE_SHIFT + MAX_ORDER - 1);
1755 * We create the default window as big as we can. The constraint is
1756 * the max order of allocation possible. The TCE table is likely to
1757 * end up being multilevel and with on-demand allocation in place,
1758 * the initial use is not going to be huge as the default window aims
1759 * to support crippled devices (i.e. not fully 64bit DMAble) only.
1761 /* iommu_table::it_map uses 1 bit per IOMMU page, hence 8 */
1762 const u64 window_size = min((maxblock * 8) << PAGE_SHIFT, max_memory);
1763 /* Each TCE level cannot exceed maxblock so go multilevel if needed */
1764 unsigned long tces_order = ilog2(window_size >> PAGE_SHIFT);
1765 unsigned long tcelevel_order = ilog2(maxblock >> 3);
1766 unsigned int levels = tces_order / tcelevel_order;
1768 if (tces_order % tcelevel_order)
1771 * We try to stick to default levels (which is >1 at the moment) in
1772 * order to save memory by relying on on-demain TCE level allocation.
1774 levels = max_t(unsigned int, levels, POWERNV_IOMMU_DEFAULT_LEVELS);
1776 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0, PAGE_SHIFT,
1777 window_size, levels, false, &tbl);
1779 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
1784 /* We use top part of 32bit space for MMIO so exclude it from DMA */
1787 if (window_size > pe->phb->ioda.m32_pci_base) {
1788 res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift;
1789 res_end = min(window_size, SZ_4G) >> tbl->it_page_shift;
1792 tbl->it_index = (pe->phb->hose->global_number << 16) | pe->pe_number;
1793 if (iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end))
1794 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
1798 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", rc);
1799 iommu_tce_table_put(tbl);
1800 tbl = NULL; /* This clears iommu_table_base below */
1802 if (!pnv_iommu_bypass_disabled)
1803 pnv_pci_ioda2_set_bypass(pe, true);
1806 * Set table base for the case of IOMMU DMA use. Usually this is done
1807 * from dma_dev_setup() which is not called when a device is returned
1808 * from VFIO so do it here.
1811 set_iommu_table_base(&pe->pdev->dev, tbl);
1816 static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1819 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1821 struct pnv_phb *phb = pe->phb;
1824 pe_info(pe, "Removing DMA window #%d\n", num);
1826 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1827 (pe->pe_number << 1) + num,
1828 0/* levels */, 0/* table address */,
1829 0/* table size */, 0/* page size */);
1831 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
1833 pnv_pci_ioda2_tce_invalidate_pe(pe);
1835 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
1840 #ifdef CONFIG_IOMMU_API
1841 unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
1842 __u64 window_size, __u32 levels)
1844 unsigned long bytes = 0;
1845 const unsigned window_shift = ilog2(window_size);
1846 unsigned entries_shift = window_shift - page_shift;
1847 unsigned table_shift = entries_shift + 3;
1848 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
1849 unsigned long direct_table_size;
1851 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
1852 !is_power_of_2(window_size))
1855 /* Calculate a direct table size from window_size and levels */
1856 entries_shift = (entries_shift + levels - 1) / levels;
1857 table_shift = entries_shift + 3;
1858 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
1859 direct_table_size = 1UL << table_shift;
1861 for ( ; levels; --levels) {
1862 bytes += ALIGN(tce_table_size, direct_table_size);
1864 tce_table_size /= direct_table_size;
1865 tce_table_size <<= 3;
1866 tce_table_size = max_t(unsigned long,
1867 tce_table_size, direct_table_size);
1870 return bytes + bytes; /* one for HW table, one for userspace copy */
1873 static long pnv_pci_ioda2_create_table_userspace(
1874 struct iommu_table_group *table_group,
1875 int num, __u32 page_shift, __u64 window_size, __u32 levels,
1876 struct iommu_table **ptbl)
1878 long ret = pnv_pci_ioda2_create_table(table_group,
1879 num, page_shift, window_size, levels, true, ptbl);
1882 (*ptbl)->it_allocated_size = pnv_pci_ioda2_get_table_size(
1883 page_shift, window_size, levels);
1887 static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus)
1889 struct pci_dev *dev;
1891 list_for_each_entry(dev, &bus->devices, bus_list) {
1892 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
1893 dev->dev.archdata.dma_offset = pe->tce_bypass_base;
1895 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
1896 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
1900 static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
1902 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1904 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
1905 struct iommu_table *tbl = pe->table_group.tables[0];
1907 pnv_pci_ioda2_set_bypass(pe, false);
1908 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
1910 pnv_ioda_setup_bus_dma(pe, pe->pbus);
1912 set_iommu_table_base(&pe->pdev->dev, NULL);
1913 iommu_tce_table_put(tbl);
1916 static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
1918 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1921 pnv_pci_ioda2_setup_default_config(pe);
1923 pnv_ioda_setup_bus_dma(pe, pe->pbus);
1926 static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
1927 .get_table_size = pnv_pci_ioda2_get_table_size,
1928 .create_table = pnv_pci_ioda2_create_table_userspace,
1929 .set_window = pnv_pci_ioda2_set_window,
1930 .unset_window = pnv_pci_ioda2_unset_window,
1931 .take_ownership = pnv_ioda2_take_ownership,
1932 .release_ownership = pnv_ioda2_release_ownership,
1936 void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1937 struct pnv_ioda_pe *pe)
1941 /* TVE #1 is selected by PCI address bit 59 */
1942 pe->tce_bypass_base = 1ull << 59;
1944 /* The PE will reserve all possible 32-bits space */
1945 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1946 phb->ioda.m32_pci_base);
1948 /* Setup linux iommu table */
1949 pe->table_group.tce32_start = 0;
1950 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
1951 pe->table_group.max_dynamic_windows_supported =
1952 IOMMU_TABLE_GROUP_MAX_TABLES;
1953 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
1954 pe->table_group.pgsizes = pnv_ioda_parse_tce_sizes(phb);
1956 rc = pnv_pci_ioda2_setup_default_config(pe);
1960 #ifdef CONFIG_IOMMU_API
1961 pe->table_group.ops = &pnv_pci_ioda2_ops;
1962 iommu_register_group(&pe->table_group, phb->hose->global_number,
1965 pe->dma_setup_done = true;
1969 * Called from KVM in real mode to EOI passthru interrupts. The ICP
1970 * EOI is handled directly in KVM in kvmppc_deliver_irq_passthru().
1972 * The IRQ data is mapped in the PCI-MSI domain and the EOI OPAL call
1973 * needs an HW IRQ number mapped in the XICS IRQ domain. The HW IRQ
1974 * numbers of the in-the-middle MSI domain are vector numbers and it's
1975 * good enough for OPAL. Use that.
1977 int64_t pnv_opal_pci_msi_eoi(struct irq_data *d)
1979 struct pci_controller *hose = irq_data_get_irq_chip_data(d->parent_data);
1980 struct pnv_phb *phb = hose->private_data;
1982 return opal_pci_msi_eoi(phb->opal_id, d->parent_data->hwirq);
1986 * The IRQ data is mapped in the XICS domain, with OPAL HW IRQ numbers
1988 static void pnv_ioda2_msi_eoi(struct irq_data *d)
1991 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1992 struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1993 struct pnv_phb *phb = hose->private_data;
1995 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2002 void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2004 struct irq_data *idata;
2005 struct irq_chip *ichip;
2007 /* The MSI EOI OPAL call is only needed on PHB3 */
2008 if (phb->model != PNV_PHB_MODEL_PHB3)
2011 if (!phb->ioda.irq_chip_init) {
2013 * First time we setup an MSI IRQ, we need to setup the
2014 * corresponding IRQ chip to route correctly.
2016 idata = irq_get_irq_data(virq);
2017 ichip = irq_data_get_irq_chip(idata);
2018 phb->ioda.irq_chip_init = 1;
2019 phb->ioda.irq_chip = *ichip;
2020 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2022 irq_set_chip(virq, &phb->ioda.irq_chip);
2023 irq_set_chip_data(virq, phb->hose);
2026 static struct irq_chip pnv_pci_msi_irq_chip;
2029 * Returns true iff chip is something that we could call
2030 * pnv_opal_pci_msi_eoi for.
2032 bool is_pnv_opal_msi(struct irq_chip *chip)
2034 return chip == &pnv_pci_msi_irq_chip;
2036 EXPORT_SYMBOL_GPL(is_pnv_opal_msi);
2038 static int __pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
2039 unsigned int xive_num,
2040 unsigned int is_64, struct msi_msg *msg)
2042 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2046 dev_dbg(&dev->dev, "%s: setup %s-bit MSI for vector #%d\n", __func__,
2047 is_64 ? "64" : "32", xive_num);
2049 /* No PE assigned ? bail out ... no MSI for you ! */
2053 /* Check if we have an MVE */
2054 if (pe->mve_number < 0)
2057 /* Force 32-bit MSI on some broken devices */
2058 if (dev->no_64bit_msi)
2061 /* Assign XIVE to PE */
2062 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2064 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2065 pci_name(dev), rc, xive_num);
2072 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2075 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2079 msg->address_hi = be64_to_cpu(addr64) >> 32;
2080 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
2084 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2087 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2091 msg->address_hi = 0;
2092 msg->address_lo = be32_to_cpu(addr32);
2094 msg->data = be32_to_cpu(data);
2100 * The msi_free() op is called before irq_domain_free_irqs_top() when
2101 * the handler data is still available. Use that to clear the XIVE
2104 static void pnv_msi_ops_msi_free(struct irq_domain *domain,
2105 struct msi_domain_info *info,
2109 xive_irq_free_data(irq);
2112 static struct msi_domain_ops pnv_pci_msi_domain_ops = {
2113 .msi_free = pnv_msi_ops_msi_free,
2116 static void pnv_msi_shutdown(struct irq_data *d)
2119 if (d->chip->irq_shutdown)
2120 d->chip->irq_shutdown(d);
2123 static void pnv_msi_mask(struct irq_data *d)
2125 pci_msi_mask_irq(d);
2126 irq_chip_mask_parent(d);
2129 static void pnv_msi_unmask(struct irq_data *d)
2131 pci_msi_unmask_irq(d);
2132 irq_chip_unmask_parent(d);
2135 static struct irq_chip pnv_pci_msi_irq_chip = {
2136 .name = "PNV-PCI-MSI",
2137 .irq_shutdown = pnv_msi_shutdown,
2138 .irq_mask = pnv_msi_mask,
2139 .irq_unmask = pnv_msi_unmask,
2140 .irq_eoi = irq_chip_eoi_parent,
2143 static struct msi_domain_info pnv_msi_domain_info = {
2144 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
2145 MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
2146 .ops = &pnv_pci_msi_domain_ops,
2147 .chip = &pnv_pci_msi_irq_chip,
2150 static void pnv_msi_compose_msg(struct irq_data *d, struct msi_msg *msg)
2152 struct msi_desc *entry = irq_data_get_msi_desc(d);
2153 struct pci_dev *pdev = msi_desc_to_pci_dev(entry);
2154 struct pci_controller *hose = irq_data_get_irq_chip_data(d);
2155 struct pnv_phb *phb = hose->private_data;
2158 rc = __pnv_pci_ioda_msi_setup(phb, pdev, d->hwirq,
2159 entry->msi_attrib.is_64, msg);
2161 dev_err(&pdev->dev, "Failed to setup %s-bit MSI #%ld : %d\n",
2162 entry->msi_attrib.is_64 ? "64" : "32", d->hwirq, rc);
2166 * The IRQ data is mapped in the MSI domain in which HW IRQ numbers
2167 * correspond to vector numbers.
2169 static void pnv_msi_eoi(struct irq_data *d)
2171 struct pci_controller *hose = irq_data_get_irq_chip_data(d);
2172 struct pnv_phb *phb = hose->private_data;
2174 if (phb->model == PNV_PHB_MODEL_PHB3) {
2176 * The EOI OPAL call takes an OPAL HW IRQ number but
2177 * since it is translated into a vector number in
2178 * OPAL, use that directly.
2180 WARN_ON_ONCE(opal_pci_msi_eoi(phb->opal_id, d->hwirq));
2183 irq_chip_eoi_parent(d);
2186 static struct irq_chip pnv_msi_irq_chip = {
2188 .irq_shutdown = pnv_msi_shutdown,
2189 .irq_mask = irq_chip_mask_parent,
2190 .irq_unmask = irq_chip_unmask_parent,
2191 .irq_eoi = pnv_msi_eoi,
2192 .irq_set_affinity = irq_chip_set_affinity_parent,
2193 .irq_compose_msi_msg = pnv_msi_compose_msg,
2196 static int pnv_irq_parent_domain_alloc(struct irq_domain *domain,
2197 unsigned int virq, int hwirq)
2199 struct irq_fwspec parent_fwspec;
2202 parent_fwspec.fwnode = domain->parent->fwnode;
2203 parent_fwspec.param_count = 2;
2204 parent_fwspec.param[0] = hwirq;
2205 parent_fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
2207 ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
2214 static int pnv_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2215 unsigned int nr_irqs, void *arg)
2217 struct pci_controller *hose = domain->host_data;
2218 struct pnv_phb *phb = hose->private_data;
2219 msi_alloc_info_t *info = arg;
2220 struct pci_dev *pdev = msi_desc_to_pci_dev(info->desc);
2224 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, nr_irqs);
2226 dev_warn(&pdev->dev, "failed to find a free MSI\n");
2230 dev_dbg(&pdev->dev, "%s bridge %pOF %d/%x #%d\n", __func__,
2231 hose->dn, virq, hwirq, nr_irqs);
2233 for (i = 0; i < nr_irqs; i++) {
2234 ret = pnv_irq_parent_domain_alloc(domain, virq + i,
2235 phb->msi_base + hwirq + i);
2239 irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
2240 &pnv_msi_irq_chip, hose);
2246 irq_domain_free_irqs_parent(domain, virq, i - 1);
2247 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, nr_irqs);
2251 static void pnv_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2252 unsigned int nr_irqs)
2254 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2255 struct pci_controller *hose = irq_data_get_irq_chip_data(d);
2256 struct pnv_phb *phb = hose->private_data;
2258 pr_debug("%s bridge %pOF %d/%lx #%d\n", __func__, hose->dn,
2259 virq, d->hwirq, nr_irqs);
2261 msi_bitmap_free_hwirqs(&phb->msi_bmp, d->hwirq, nr_irqs);
2262 /* XIVE domain is cleared through ->msi_free() */
2265 static const struct irq_domain_ops pnv_irq_domain_ops = {
2266 .alloc = pnv_irq_domain_alloc,
2267 .free = pnv_irq_domain_free,
2270 static int pnv_msi_allocate_domains(struct pci_controller *hose, unsigned int count)
2272 struct pnv_phb *phb = hose->private_data;
2273 struct irq_domain *parent = irq_get_default_host();
2275 hose->fwnode = irq_domain_alloc_named_id_fwnode("PNV-MSI", phb->opal_id);
2279 hose->dev_domain = irq_domain_create_hierarchy(parent, 0, count,
2281 &pnv_irq_domain_ops, hose);
2282 if (!hose->dev_domain) {
2283 pr_err("PCI: failed to create IRQ domain bridge %pOF (domain %d)\n",
2284 hose->dn, hose->global_number);
2285 irq_domain_free_fwnode(hose->fwnode);
2289 hose->msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(hose->dn),
2290 &pnv_msi_domain_info,
2292 if (!hose->msi_domain) {
2293 pr_err("PCI: failed to create MSI IRQ domain bridge %pOF (domain %d)\n",
2294 hose->dn, hose->global_number);
2295 irq_domain_free_fwnode(hose->fwnode);
2296 irq_domain_remove(hose->dev_domain);
2303 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2306 const __be32 *prop = of_get_property(phb->hose->dn,
2307 "ibm,opal-msi-ranges", NULL);
2310 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2315 phb->msi_base = be32_to_cpup(prop);
2316 count = be32_to_cpup(prop + 1);
2317 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
2318 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2319 phb->hose->global_number);
2323 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
2324 count, phb->msi_base);
2326 pnv_msi_allocate_domains(phb->hose, count);
2329 static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
2330 struct resource *res)
2332 struct pnv_phb *phb = pe->phb;
2333 struct pci_bus_region region;
2337 if (!res || !res->flags || res->start > res->end)
2340 if (res->flags & IORESOURCE_IO) {
2341 region.start = res->start - phb->ioda.io_pci_base;
2342 region.end = res->end - phb->ioda.io_pci_base;
2343 index = region.start / phb->ioda.io_segsize;
2345 while (index < phb->ioda.total_pe_num &&
2346 region.start <= region.end) {
2347 phb->ioda.io_segmap[index] = pe->pe_number;
2348 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2349 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2350 if (rc != OPAL_SUCCESS) {
2351 pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",
2352 __func__, rc, index, pe->pe_number);
2356 region.start += phb->ioda.io_segsize;
2359 } else if ((res->flags & IORESOURCE_MEM) &&
2360 !pnv_pci_is_m64(phb, res)) {
2361 region.start = res->start -
2362 phb->hose->mem_offset[0] -
2363 phb->ioda.m32_pci_base;
2364 region.end = res->end -
2365 phb->hose->mem_offset[0] -
2366 phb->ioda.m32_pci_base;
2367 index = region.start / phb->ioda.m32_segsize;
2369 while (index < phb->ioda.total_pe_num &&
2370 region.start <= region.end) {
2371 phb->ioda.m32_segmap[index] = pe->pe_number;
2372 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2373 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2374 if (rc != OPAL_SUCCESS) {
2375 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",
2376 __func__, rc, index, pe->pe_number);
2380 region.start += phb->ioda.m32_segsize;
2387 * This function is supposed to be called on basis of PE from top
2388 * to bottom style. So the the I/O or MMIO segment assigned to
2389 * parent PE could be overridden by its child PEs if necessary.
2391 static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
2393 struct pci_dev *pdev;
2397 * NOTE: We only care PCI bus based PE for now. For PCI
2398 * device based PE, for example SRIOV sensitive VF should
2399 * be figured out later.
2401 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2403 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
2404 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
2405 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
2408 * If the PE contains all subordinate PCI buses, the
2409 * windows of the child bridges should be mapped to
2412 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
2414 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
2415 pnv_ioda_setup_pe_res(pe,
2416 &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
2420 #ifdef CONFIG_DEBUG_FS
2421 static int pnv_pci_diag_data_set(void *data, u64 val)
2423 struct pnv_phb *phb = data;
2426 /* Retrieve the diag data from firmware */
2427 ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
2428 phb->diag_data_size);
2429 if (ret != OPAL_SUCCESS)
2432 /* Print the diag data to the kernel log */
2433 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
2437 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_diag_data_fops, NULL, pnv_pci_diag_data_set,
2440 static int pnv_pci_ioda_pe_dump(void *data, u64 val)
2442 struct pnv_phb *phb = data;
2445 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
2446 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_num];
2448 if (!test_bit(pe_num, phb->ioda.pe_alloc))
2451 pe_warn(pe, "rid: %04x dev count: %2d flags: %s%s%s%s%s%s\n",
2452 pe->rid, pe->device_count,
2453 (pe->flags & PNV_IODA_PE_DEV) ? "dev " : "",
2454 (pe->flags & PNV_IODA_PE_BUS) ? "bus " : "",
2455 (pe->flags & PNV_IODA_PE_BUS_ALL) ? "all " : "",
2456 (pe->flags & PNV_IODA_PE_MASTER) ? "master " : "",
2457 (pe->flags & PNV_IODA_PE_SLAVE) ? "slave " : "",
2458 (pe->flags & PNV_IODA_PE_VF) ? "vf " : "");
2464 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_ioda_pe_dump_fops, NULL,
2465 pnv_pci_ioda_pe_dump, "%llu\n");
2467 #endif /* CONFIG_DEBUG_FS */
2469 static void pnv_pci_ioda_create_dbgfs(void)
2471 #ifdef CONFIG_DEBUG_FS
2472 struct pci_controller *hose, *tmp;
2473 struct pnv_phb *phb;
2476 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2477 phb = hose->private_data;
2479 sprintf(name, "PCI%04x", hose->global_number);
2480 phb->dbgfs = debugfs_create_dir(name, arch_debugfs_dir);
2482 debugfs_create_file_unsafe("dump_diag_regs", 0200, phb->dbgfs,
2483 phb, &pnv_pci_diag_data_fops);
2484 debugfs_create_file_unsafe("dump_ioda_pe_state", 0200, phb->dbgfs,
2485 phb, &pnv_pci_ioda_pe_dump_fops);
2487 #endif /* CONFIG_DEBUG_FS */
2490 static void pnv_pci_enable_bridge(struct pci_bus *bus)
2492 struct pci_dev *dev = bus->self;
2493 struct pci_bus *child;
2495 /* Empty bus ? bail */
2496 if (list_empty(&bus->devices))
2500 * If there's a bridge associated with that bus enable it. This works
2501 * around races in the generic code if the enabling is done during
2502 * parallel probing. This can be removed once those races have been
2506 int rc = pci_enable_device(dev);
2508 pci_err(dev, "Error enabling bridge (%d)\n", rc);
2509 pci_set_master(dev);
2512 /* Perform the same to child busses */
2513 list_for_each_entry(child, &bus->children, node)
2514 pnv_pci_enable_bridge(child);
2517 static void pnv_pci_enable_bridges(void)
2519 struct pci_controller *hose;
2521 list_for_each_entry(hose, &hose_list, list_node)
2522 pnv_pci_enable_bridge(hose->bus);
2525 static void pnv_pci_ioda_fixup(void)
2527 pnv_pci_ioda_create_dbgfs();
2529 pnv_pci_enable_bridges();
2532 pnv_eeh_post_init();
2537 * Returns the alignment for I/O or memory windows for P2P
2538 * bridges. That actually depends on how PEs are segmented.
2539 * For now, we return I/O or M32 segment size for PE sensitive
2540 * P2P bridges. Otherwise, the default values (4KiB for I/O,
2541 * 1MiB for memory) will be returned.
2543 * The current PCI bus might be put into one PE, which was
2544 * create against the parent PCI bridge. For that case, we
2545 * needn't enlarge the alignment so that we can save some
2548 static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2551 struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
2552 int num_pci_bridges = 0;
2553 struct pci_dev *bridge;
2557 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2559 if (num_pci_bridges >= 2)
2563 bridge = bridge->bus->self;
2567 * We fall back to M32 if M64 isn't supported. We enforce the M64
2568 * alignment for any 64-bit resource, PCIe doesn't care and
2569 * bridges only do 64-bit prefetchable anyway.
2571 if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type))
2572 return phb->ioda.m64_segsize;
2573 if (type & IORESOURCE_MEM)
2574 return phb->ioda.m32_segsize;
2576 return phb->ioda.io_segsize;
2580 * We are updating root port or the upstream port of the
2581 * bridge behind the root port with PHB's windows in order
2582 * to accommodate the changes on required resources during
2583 * PCI (slot) hotplug, which is connected to either root
2584 * port or the downstream ports of PCIe switch behind the
2587 static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
2590 struct pci_controller *hose = pci_bus_to_host(bus);
2591 struct pnv_phb *phb = hose->private_data;
2592 struct pci_dev *bridge = bus->self;
2593 struct resource *r, *w;
2594 bool msi_region = false;
2597 /* Check if we need apply fixup to the bridge's windows */
2598 if (!pci_is_root_bus(bridge->bus) &&
2599 !pci_is_root_bus(bridge->bus->self->bus))
2602 /* Fixup the resources */
2603 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
2604 r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
2605 if (!r->flags || !r->parent)
2609 if (r->flags & type & IORESOURCE_IO)
2610 w = &hose->io_resource;
2611 else if (pnv_pci_is_m64(phb, r) &&
2612 (type & IORESOURCE_PREFETCH) &&
2613 phb->ioda.m64_segsize)
2614 w = &hose->mem_resources[1];
2615 else if (r->flags & type & IORESOURCE_MEM) {
2616 w = &hose->mem_resources[0];
2620 r->start = w->start;
2623 /* The 64KB 32-bits MSI region shouldn't be included in
2624 * the 32-bits bridge window. Otherwise, we can see strange
2625 * issues. One of them is EEH error observed on Garrison.
2627 * Exclude top 1MB region which is the minimal alignment of
2628 * 32-bits bridge window.
2637 static void pnv_pci_configure_bus(struct pci_bus *bus)
2639 struct pci_dev *bridge = bus->self;
2640 struct pnv_ioda_pe *pe;
2641 bool all = (bridge && pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
2643 dev_info(&bus->dev, "Configuring PE for bus\n");
2645 /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
2646 if (WARN_ON(list_empty(&bus->devices)))
2649 /* Reserve PEs according to used M64 resources */
2650 pnv_ioda_reserve_m64_pe(bus, NULL, all);
2653 * Assign PE. We might run here because of partial hotplug.
2654 * For the case, we just pick up the existing PE and should
2655 * not allocate resources again.
2657 pe = pnv_ioda_setup_bus_PE(bus, all);
2661 pnv_ioda_setup_pe_seg(pe);
2664 static resource_size_t pnv_pci_default_alignment(void)
2669 /* Prevent enabling devices for which we couldn't properly
2672 static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
2676 pdn = pci_get_pdn(dev);
2677 if (!pdn || pdn->pe_number == IODA_INVALID_PE) {
2678 pci_err(dev, "pci_enable_device() blocked, no PE assigned.\n");
2685 static bool pnv_ocapi_enable_device_hook(struct pci_dev *dev)
2688 struct pnv_ioda_pe *pe;
2690 pdn = pci_get_pdn(dev);
2694 if (pdn->pe_number == IODA_INVALID_PE) {
2695 pe = pnv_ioda_setup_dev_PE(dev);
2702 static long pnv_pci_ioda1_unset_window(struct iommu_table_group *table_group,
2705 struct pnv_ioda_pe *pe = container_of(table_group,
2706 struct pnv_ioda_pe, table_group);
2707 struct pnv_phb *phb = pe->phb;
2711 pe_info(pe, "Removing DMA window #%d\n", num);
2712 for (idx = 0; idx < phb->ioda.dma32_count; idx++) {
2713 if (phb->ioda.dma32_segmap[idx] != pe->pe_number)
2716 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2717 idx, 0, 0ul, 0ul, 0ul);
2718 if (rc != OPAL_SUCCESS) {
2719 pe_warn(pe, "Failure %ld unmapping DMA32 segment#%d\n",
2724 phb->ioda.dma32_segmap[idx] = IODA_INVALID_PE;
2727 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2728 return OPAL_SUCCESS;
2731 static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe)
2733 struct iommu_table *tbl = pe->table_group.tables[0];
2736 if (!pe->dma_setup_done)
2739 rc = pnv_pci_ioda1_unset_window(&pe->table_group, 0);
2740 if (rc != OPAL_SUCCESS)
2743 pnv_pci_p7ioc_tce_invalidate(tbl, tbl->it_offset, tbl->it_size, false);
2744 if (pe->table_group.group) {
2745 iommu_group_put(pe->table_group.group);
2746 WARN_ON(pe->table_group.group);
2749 free_pages(tbl->it_base, get_order(tbl->it_size << 3));
2750 iommu_tce_table_put(tbl);
2753 void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
2755 struct iommu_table *tbl = pe->table_group.tables[0];
2758 if (!pe->dma_setup_done)
2761 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2763 pe_warn(pe, "OPAL error %lld release DMA window\n", rc);
2765 pnv_pci_ioda2_set_bypass(pe, false);
2766 if (pe->table_group.group) {
2767 iommu_group_put(pe->table_group.group);
2768 WARN_ON(pe->table_group.group);
2771 iommu_tce_table_put(tbl);
2774 static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
2778 struct pnv_phb *phb = pe->phb;
2782 for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
2783 if (map[idx] != pe->pe_number)
2786 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2787 phb->ioda.reserved_pe_idx, win, 0, idx);
2789 if (rc != OPAL_SUCCESS)
2790 pe_warn(pe, "Error %lld unmapping (%d) segment#%d\n",
2793 map[idx] = IODA_INVALID_PE;
2797 static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
2799 struct pnv_phb *phb = pe->phb;
2801 if (phb->type == PNV_PHB_IODA1) {
2802 pnv_ioda_free_pe_seg(pe, OPAL_IO_WINDOW_TYPE,
2803 phb->ioda.io_segmap);
2804 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
2805 phb->ioda.m32_segmap);
2806 /* M64 is pre-configured by pnv_ioda1_init_m64() */
2807 } else if (phb->type == PNV_PHB_IODA2) {
2808 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
2809 phb->ioda.m32_segmap);
2813 static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
2815 struct pnv_phb *phb = pe->phb;
2816 struct pnv_ioda_pe *slave, *tmp;
2818 pe_info(pe, "Releasing PE\n");
2820 mutex_lock(&phb->ioda.pe_list_mutex);
2821 list_del(&pe->list);
2822 mutex_unlock(&phb->ioda.pe_list_mutex);
2824 switch (phb->type) {
2826 pnv_pci_ioda1_release_pe_dma(pe);
2829 pnv_pci_ioda2_release_pe_dma(pe);
2831 case PNV_PHB_NPU_OCAPI:
2837 pnv_ioda_release_pe_seg(pe);
2838 pnv_ioda_deconfigure_pe(pe->phb, pe);
2840 /* Release slave PEs in the compound PE */
2841 if (pe->flags & PNV_IODA_PE_MASTER) {
2842 list_for_each_entry_safe(slave, tmp, &pe->slaves, list) {
2843 list_del(&slave->list);
2844 pnv_ioda_free_pe(slave);
2849 * The PE for root bus can be removed because of hotplug in EEH
2850 * recovery for fenced PHB error. We need to mark the PE dead so
2851 * that it can be populated again in PCI hot add path. The PE
2852 * shouldn't be destroyed as it's the global reserved resource.
2854 if (phb->ioda.root_pe_idx == pe->pe_number)
2857 pnv_ioda_free_pe(pe);
2860 static void pnv_pci_release_device(struct pci_dev *pdev)
2862 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
2863 struct pci_dn *pdn = pci_get_pdn(pdev);
2864 struct pnv_ioda_pe *pe;
2866 /* The VF PE state is torn down when sriov_disable() is called */
2867 if (pdev->is_virtfn)
2870 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2873 #ifdef CONFIG_PCI_IOV
2875 * FIXME: Try move this to sriov_disable(). It's here since we allocate
2876 * the iov state at probe time since we need to fiddle with the IOV
2879 if (pdev->is_physfn)
2880 kfree(pdev->dev.archdata.iov_data);
2884 * PCI hotplug can happen as part of EEH error recovery. The @pdn
2885 * isn't removed and added afterwards in this scenario. We should
2886 * set the PE number in @pdn to an invalid one. Otherwise, the PE's
2887 * device count is decreased on removing devices while failing to
2888 * be increased on adding devices. It leads to unbalanced PE's device
2889 * count and eventually make normal PCI hotplug path broken.
2891 pe = &phb->ioda.pe_array[pdn->pe_number];
2892 pdn->pe_number = IODA_INVALID_PE;
2894 WARN_ON(--pe->device_count < 0);
2895 if (pe->device_count == 0)
2896 pnv_ioda_release_pe(pe);
2899 static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
2901 struct pnv_phb *phb = hose->private_data;
2903 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
2907 static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus)
2909 struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
2910 struct pnv_ioda_pe *pe;
2912 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2913 if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
2919 if (bus->number == ((pe->rid >> 8) & 0xFF)) {
2926 static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
2927 .dma_dev_setup = pnv_pci_ioda_dma_dev_setup,
2928 .dma_bus_setup = pnv_pci_ioda_dma_bus_setup,
2929 .iommu_bypass_supported = pnv_pci_ioda_iommu_bypass_supported,
2930 .enable_device_hook = pnv_pci_enable_device_hook,
2931 .release_device = pnv_pci_release_device,
2932 .window_alignment = pnv_pci_window_alignment,
2933 .setup_bridge = pnv_pci_fixup_bridge_resources,
2934 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
2935 .shutdown = pnv_pci_ioda_shutdown,
2938 static const struct pci_controller_ops pnv_npu_ocapi_ioda_controller_ops = {
2939 .enable_device_hook = pnv_ocapi_enable_device_hook,
2940 .release_device = pnv_pci_release_device,
2941 .window_alignment = pnv_pci_window_alignment,
2942 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
2943 .shutdown = pnv_pci_ioda_shutdown,
2946 static void __init pnv_pci_init_ioda_phb(struct device_node *np,
2947 u64 hub_id, int ioda_type)
2949 struct pci_controller *hose;
2950 struct pnv_phb *phb;
2951 unsigned long size, m64map_off, m32map_off, pemap_off;
2952 unsigned long iomap_off = 0, dma32map_off = 0;
2953 struct pnv_ioda_pe *root_pe;
2955 const __be64 *prop64;
2956 const __be32 *prop32;
2963 if (!of_device_is_available(np))
2966 pr_info("Initializing %s PHB (%pOF)\n", pnv_phb_names[ioda_type], np);
2968 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
2970 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
2973 phb_id = be64_to_cpup(prop64);
2974 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
2976 phb = kzalloc(sizeof(*phb), GFP_KERNEL);
2978 panic("%s: Failed to allocate %zu bytes\n", __func__,
2981 /* Allocate PCI controller */
2982 phb->hose = hose = pcibios_alloc_controller(np);
2984 pr_err(" Can't allocate PCI controller for %pOF\n",
2986 memblock_free(__pa(phb), sizeof(struct pnv_phb));
2990 spin_lock_init(&phb->lock);
2991 prop32 = of_get_property(np, "bus-range", &len);
2992 if (prop32 && len == 8) {
2993 hose->first_busno = be32_to_cpu(prop32[0]);
2994 hose->last_busno = be32_to_cpu(prop32[1]);
2996 pr_warn(" Broken <bus-range> on %pOF\n", np);
2997 hose->first_busno = 0;
2998 hose->last_busno = 0xff;
3000 hose->private_data = phb;
3001 phb->hub_id = hub_id;
3002 phb->opal_id = phb_id;
3003 phb->type = ioda_type;
3004 mutex_init(&phb->ioda.pe_alloc_mutex);
3006 /* Detect specific models for error handling */
3007 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3008 phb->model = PNV_PHB_MODEL_P7IOC;
3009 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
3010 phb->model = PNV_PHB_MODEL_PHB3;
3012 phb->model = PNV_PHB_MODEL_UNKNOWN;
3014 /* Initialize diagnostic data buffer */
3015 prop32 = of_get_property(np, "ibm,phb-diag-data-size", NULL);
3017 phb->diag_data_size = be32_to_cpup(prop32);
3019 phb->diag_data_size = PNV_PCI_DIAG_BUF_SIZE;
3021 phb->diag_data = kzalloc(phb->diag_data_size, GFP_KERNEL);
3022 if (!phb->diag_data)
3023 panic("%s: Failed to allocate %u bytes\n", __func__,
3024 phb->diag_data_size);
3026 /* Parse 32-bit and IO ranges (if any) */
3027 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
3030 if (!of_address_to_resource(np, 0, &r)) {
3031 phb->regs_phys = r.start;
3032 phb->regs = ioremap(r.start, resource_size(&r));
3033 if (phb->regs == NULL)
3034 pr_err(" Failed to map registers !\n");
3037 /* Initialize more IODA stuff */
3038 phb->ioda.total_pe_num = 1;
3039 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
3041 phb->ioda.total_pe_num = be32_to_cpup(prop32);
3042 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3044 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
3046 /* Invalidate RID to PE# mapping */
3047 for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
3048 phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
3050 /* Parse 64-bit MMIO range */
3051 pnv_ioda_parse_m64_window(phb);
3053 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
3054 /* FW Has already off top 64k of M32 space (MSI space) */
3055 phb->ioda.m32_size += 0x10000;
3057 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
3058 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
3059 phb->ioda.io_size = hose->pci_io_size;
3060 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
3061 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3063 /* Calculate how many 32-bit TCE segments we have */
3064 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
3065 PNV_IODA1_DMA32_SEGSIZE;
3067 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
3068 size = ALIGN(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
3069 sizeof(unsigned long));
3071 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
3073 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
3074 if (phb->type == PNV_PHB_IODA1) {
3076 size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
3077 dma32map_off = size;
3078 size += phb->ioda.dma32_count *
3079 sizeof(phb->ioda.dma32_segmap[0]);
3082 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
3083 aux = kzalloc(size, GFP_KERNEL);
3085 panic("%s: Failed to allocate %lu bytes\n", __func__, size);
3087 phb->ioda.pe_alloc = aux;
3088 phb->ioda.m64_segmap = aux + m64map_off;
3089 phb->ioda.m32_segmap = aux + m32map_off;
3090 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
3091 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
3092 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
3094 if (phb->type == PNV_PHB_IODA1) {
3095 phb->ioda.io_segmap = aux + iomap_off;
3096 for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
3097 phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
3099 phb->ioda.dma32_segmap = aux + dma32map_off;
3100 for (segno = 0; segno < phb->ioda.dma32_count; segno++)
3101 phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE;
3103 phb->ioda.pe_array = aux + pemap_off;
3106 * Choose PE number for root bus, which shouldn't have
3107 * M64 resources consumed by its child devices. To pick
3108 * the PE number adjacent to the reserved one if possible.
3110 pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
3111 if (phb->ioda.reserved_pe_idx == 0) {
3112 phb->ioda.root_pe_idx = 1;
3113 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3114 } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
3115 phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
3116 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3118 /* otherwise just allocate one */
3119 root_pe = pnv_ioda_alloc_pe(phb, 1);
3120 phb->ioda.root_pe_idx = root_pe->pe_number;
3123 INIT_LIST_HEAD(&phb->ioda.pe_list);
3124 mutex_init(&phb->ioda.pe_list_mutex);
3126 /* Calculate how many 32-bit TCE segments we have */
3127 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
3128 PNV_IODA1_DMA32_SEGSIZE;
3130 #if 0 /* We should really do that ... */
3131 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3134 starting_real_address,
3135 starting_pci_address,
3139 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
3140 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
3141 phb->ioda.m32_size, phb->ioda.m32_segsize);
3142 if (phb->ioda.m64_size)
3143 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3144 phb->ioda.m64_size, phb->ioda.m64_segsize);
3145 if (phb->ioda.io_size)
3146 pr_info(" IO: 0x%x [segment=0x%x]\n",
3147 phb->ioda.io_size, phb->ioda.io_segsize);
3150 phb->hose->ops = &pnv_pci_ops;
3151 phb->get_pe_state = pnv_ioda_get_pe_state;
3152 phb->freeze_pe = pnv_ioda_freeze_pe;
3153 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
3155 /* Setup MSI support */
3156 pnv_pci_init_ioda_msis(phb);
3159 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3160 * to let the PCI core do resource assignment. It's supposed
3161 * that the PCI core will do correct I/O and MMIO alignment
3162 * for the P2P bridge bars so that each PCI bus (excluding
3163 * the child P2P bridges) can form individual PE.
3165 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
3167 switch (phb->type) {
3168 case PNV_PHB_NPU_OCAPI:
3169 hose->controller_ops = pnv_npu_ocapi_ioda_controller_ops;
3172 hose->controller_ops = pnv_pci_ioda_controller_ops;
3175 ppc_md.pcibios_default_alignment = pnv_pci_default_alignment;
3177 #ifdef CONFIG_PCI_IOV
3178 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov;
3179 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
3180 ppc_md.pcibios_sriov_enable = pnv_pcibios_sriov_enable;
3181 ppc_md.pcibios_sriov_disable = pnv_pcibios_sriov_disable;
3184 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
3186 /* Reset IODA tables to a clean state */
3187 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
3189 pr_warn(" OPAL Error %ld performing IODA table reset !\n", rc);
3192 * If we're running in kdump kernel, the previous kernel never
3193 * shutdown PCI devices correctly. We already got IODA table
3194 * cleaned out. So we have to issue PHB reset to stop all PCI
3195 * transactions from previous kernel. The ppc_pci_reset_phbs
3196 * kernel parameter will force this reset too. Additionally,
3197 * if the IODA reset above failed then use a bigger hammer.
3198 * This can happen if we get a PHB fatal error in very early
3201 if (is_kdump_kernel() || pci_reset_phbs || rc) {
3202 pr_info(" Issue PHB reset ...\n");
3203 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3204 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
3207 /* Remove M64 resource if we can't configure it successfully */
3208 if (!phb->init_m64 || phb->init_m64(phb))
3209 hose->mem_resources[1].flags = 0;
3211 /* create pci_dn's for DT nodes under this PHB */
3212 pci_devs_phb_init_dynamic(hose);
3215 void __init pnv_pci_init_ioda2_phb(struct device_node *np)
3217 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
3220 void __init pnv_pci_init_npu2_opencapi_phb(struct device_node *np)
3222 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_OCAPI);
3225 static void pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev *dev)
3227 struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
3229 if (!machine_is(powernv))
3232 if (phb->type == PNV_PHB_NPU_OCAPI)
3233 dev->cfg_size = PCI_CFG_SPACE_EXP_SIZE;
3235 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pnv_npu2_opencapi_cfg_size_fixup);
3237 void __init pnv_pci_init_ioda_hub(struct device_node *np)
3239 struct device_node *phbn;
3240 const __be64 *prop64;
3243 pr_info("Probing IODA IO-Hub %pOF\n", np);
3245 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3247 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3250 hub_id = be64_to_cpup(prop64);
3251 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3253 /* Count child PHBs */
3254 for_each_child_of_node(np, phbn) {
3255 /* Look for IODA1 PHBs */
3256 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
3257 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);