2 * Support PCI/PCIe on PowerNV platforms
4 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/pci.h>
16 #include <linux/crash_dump.h>
17 #include <linux/debugfs.h>
18 #include <linux/delay.h>
19 #include <linux/string.h>
20 #include <linux/init.h>
21 #include <linux/bootmem.h>
22 #include <linux/irq.h>
24 #include <linux/msi.h>
25 #include <linux/memblock.h>
27 #include <asm/sections.h>
30 #include <asm/pci-bridge.h>
31 #include <asm/machdep.h>
32 #include <asm/msi_bitmap.h>
33 #include <asm/ppc-pci.h>
35 #include <asm/iommu.h>
38 #include <asm/debug.h>
39 #include <asm/firmware.h>
44 static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
57 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
59 sprintf(pfix, "%04x:%02x ",
60 pci_domain_nr(pe->pbus), pe->pbus->number);
62 printk("%spci %s: [PE# %.3d] %pV",
63 level, pfix, pe->pe_number, &vaf);
68 #define pe_err(pe, fmt, ...) \
69 pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
70 #define pe_warn(pe, fmt, ...) \
71 pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
72 #define pe_info(pe, fmt, ...) \
73 pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
76 * stdcix is only supposed to be used in hypervisor real mode as per
77 * the architecture spec
79 static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
81 __asm__ __volatile__("stdcix %0,0,%1"
82 : : "r" (val), "r" (paddr) : "memory");
85 static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
87 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
88 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
91 static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
96 pe = find_next_zero_bit(phb->ioda.pe_alloc,
97 phb->ioda.total_pe, 0);
98 if (pe >= phb->ioda.total_pe)
99 return IODA_INVALID_PE;
100 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
102 phb->ioda.pe_array[pe].phb = phb;
103 phb->ioda.pe_array[pe].pe_number = pe;
107 static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
109 WARN_ON(phb->ioda.pe_array[pe].pdev);
111 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
112 clear_bit(pe, phb->ioda.pe_alloc);
115 /* The default M64 BAR is shared by all PEs */
116 static int pnv_ioda2_init_m64(struct pnv_phb *phb)
122 /* Configure the default M64 BAR */
123 rc = opal_pci_set_phb_mem_window(phb->opal_id,
124 OPAL_M64_WINDOW_TYPE,
125 phb->ioda.m64_bar_idx,
129 if (rc != OPAL_SUCCESS) {
130 desc = "configuring";
134 /* Enable the default M64 BAR */
135 rc = opal_pci_phb_mmio_enable(phb->opal_id,
136 OPAL_M64_WINDOW_TYPE,
137 phb->ioda.m64_bar_idx,
138 OPAL_ENABLE_M64_SPLIT);
139 if (rc != OPAL_SUCCESS) {
144 /* Mark the M64 BAR assigned */
145 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
148 * Strip off the segment used by the reserved PE, which is
149 * expected to be 0 or last one of PE capabicity.
151 r = &phb->hose->mem_resources[1];
152 if (phb->ioda.reserved_pe == 0)
153 r->start += phb->ioda.m64_segsize;
154 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
155 r->end -= phb->ioda.m64_segsize;
157 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
158 phb->ioda.reserved_pe);
163 pr_warn(" Failure %lld %s M64 BAR#%d\n",
164 rc, desc, phb->ioda.m64_bar_idx);
165 opal_pci_phb_mmio_enable(phb->opal_id,
166 OPAL_M64_WINDOW_TYPE,
167 phb->ioda.m64_bar_idx,
172 static void pnv_ioda2_alloc_m64_pe(struct pnv_phb *phb)
174 resource_size_t sgsz = phb->ioda.m64_segsize;
175 struct pci_dev *pdev;
180 * Root bus always has full M64 range and root port has
181 * M64 range used in reality. So we're checking root port
182 * instead of root bus.
184 list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
185 for (i = PCI_BRIDGE_RESOURCES;
186 i <= PCI_BRIDGE_RESOURCE_END; i++) {
187 r = &pdev->resource[i];
189 !pnv_pci_is_mem_pref_64(r->flags))
192 base = (r->start - phb->ioda.m64_base) / sgsz;
193 for (step = 0; step < resource_size(r) / sgsz; step++)
194 set_bit(base + step, phb->ioda.pe_alloc);
199 static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
200 struct pci_bus *bus, int all)
202 resource_size_t segsz = phb->ioda.m64_segsize;
203 struct pci_dev *pdev;
205 struct pnv_ioda_pe *master_pe, *pe;
206 unsigned long size, *pe_alloc;
210 /* Root bus shouldn't use M64 */
211 if (pci_is_root_bus(bus))
212 return IODA_INVALID_PE;
214 /* We support only one M64 window on each bus */
216 pci_bus_for_each_resource(bus, r, i) {
217 if (r && r->parent &&
218 pnv_pci_is_mem_pref_64(r->flags)) {
224 /* No M64 window found ? */
226 return IODA_INVALID_PE;
228 /* Allocate bitmap */
229 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
230 pe_alloc = kzalloc(size, GFP_KERNEL);
232 pr_warn("%s: Out of memory !\n",
234 return IODA_INVALID_PE;
238 * Figure out reserved PE numbers by the PE
241 start = (r->start - phb->ioda.m64_base) / segsz;
242 for (i = 0; i < resource_size(r) / segsz; i++)
243 set_bit(start + i, pe_alloc);
249 * If the PE doesn't cover all subordinate buses,
250 * we need subtract from reserved PEs for children.
252 list_for_each_entry(pdev, &bus->devices, bus_list) {
253 if (!pdev->subordinate)
256 pci_bus_for_each_resource(pdev->subordinate, r, i) {
257 if (!r || !r->parent ||
258 !pnv_pci_is_mem_pref_64(r->flags))
261 start = (r->start - phb->ioda.m64_base) / segsz;
262 for (j = 0; j < resource_size(r) / segsz ; j++)
263 clear_bit(start + j, pe_alloc);
268 * the current bus might not own M64 window and that's all
269 * contributed by its child buses. For the case, we needn't
270 * pick M64 dependent PE#.
272 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
274 return IODA_INVALID_PE;
278 * Figure out the master PE and put all slave PEs to master
279 * PE's list to form compound PE.
284 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
285 phb->ioda.total_pe) {
286 pe = &phb->ioda.pe_array[i];
291 pe->flags |= PNV_IODA_PE_MASTER;
292 INIT_LIST_HEAD(&pe->slaves);
295 pe->flags |= PNV_IODA_PE_SLAVE;
296 pe->master = master_pe;
297 list_add_tail(&pe->list, &master_pe->slaves);
302 return master_pe->pe_number;
305 static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
307 struct pci_controller *hose = phb->hose;
308 struct device_node *dn = hose->dn;
309 struct resource *res;
313 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
314 pr_info(" Firmware too old to support M64 window\n");
318 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
320 pr_info(" No <ibm,opal-m64-window> on %s\n",
325 /* FIXME: Support M64 for P7IOC */
326 if (phb->type != PNV_PHB_IODA2) {
327 pr_info(" Not support M64 window\n");
331 res = &hose->mem_resources[1];
332 res->start = of_translate_address(dn, r + 2);
333 res->end = res->start + of_read_number(r + 4, 2) - 1;
334 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
335 pci_addr = of_read_number(r, 2);
336 hose->mem_offset[1] = res->start - pci_addr;
338 phb->ioda.m64_size = resource_size(res);
339 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
340 phb->ioda.m64_base = pci_addr;
342 /* Use last M64 BAR to cover M64 window */
343 phb->ioda.m64_bar_idx = 15;
344 phb->init_m64 = pnv_ioda2_init_m64;
345 phb->alloc_m64_pe = pnv_ioda2_alloc_m64_pe;
346 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
349 static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
351 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
352 struct pnv_ioda_pe *slave;
355 /* Fetch master PE */
356 if (pe->flags & PNV_IODA_PE_SLAVE) {
358 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
359 pe_no = pe->pe_number;
362 /* Freeze master PE */
363 rc = opal_pci_eeh_freeze_set(phb->opal_id,
365 OPAL_EEH_ACTION_SET_FREEZE_ALL);
366 if (rc != OPAL_SUCCESS) {
367 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
368 __func__, rc, phb->hose->global_number, pe_no);
372 /* Freeze slave PEs */
373 if (!(pe->flags & PNV_IODA_PE_MASTER))
376 list_for_each_entry(slave, &pe->slaves, list) {
377 rc = opal_pci_eeh_freeze_set(phb->opal_id,
379 OPAL_EEH_ACTION_SET_FREEZE_ALL);
380 if (rc != OPAL_SUCCESS)
381 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
382 __func__, rc, phb->hose->global_number,
387 static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
389 struct pnv_ioda_pe *pe, *slave;
393 pe = &phb->ioda.pe_array[pe_no];
394 if (pe->flags & PNV_IODA_PE_SLAVE) {
396 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
397 pe_no = pe->pe_number;
400 /* Clear frozen state for master PE */
401 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
402 if (rc != OPAL_SUCCESS) {
403 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
404 __func__, rc, opt, phb->hose->global_number, pe_no);
408 if (!(pe->flags & PNV_IODA_PE_MASTER))
411 /* Clear frozen state for slave PEs */
412 list_for_each_entry(slave, &pe->slaves, list) {
413 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
416 if (rc != OPAL_SUCCESS) {
417 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
418 __func__, rc, opt, phb->hose->global_number,
427 static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
429 struct pnv_ioda_pe *slave, *pe;
434 /* Sanity check on PE number */
435 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
436 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
439 * Fetch the master PE and the PE instance might be
440 * not initialized yet.
442 pe = &phb->ioda.pe_array[pe_no];
443 if (pe->flags & PNV_IODA_PE_SLAVE) {
445 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
446 pe_no = pe->pe_number;
449 /* Check the master PE */
450 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
451 &state, &pcierr, NULL);
452 if (rc != OPAL_SUCCESS) {
453 pr_warn("%s: Failure %lld getting "
454 "PHB#%x-PE#%x state\n",
456 phb->hose->global_number, pe_no);
457 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
460 /* Check the slave PE */
461 if (!(pe->flags & PNV_IODA_PE_MASTER))
464 list_for_each_entry(slave, &pe->slaves, list) {
465 rc = opal_pci_eeh_freeze_status(phb->opal_id,
470 if (rc != OPAL_SUCCESS) {
471 pr_warn("%s: Failure %lld getting "
472 "PHB#%x-PE#%x state\n",
474 phb->hose->global_number, slave->pe_number);
475 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
479 * Override the result based on the ascending
489 /* Currently those 2 are only used when MSIs are enabled, this will change
490 * but in the meantime, we need to protect them to avoid warnings
492 #ifdef CONFIG_PCI_MSI
493 static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
495 struct pci_controller *hose = pci_bus_to_host(dev->bus);
496 struct pnv_phb *phb = hose->private_data;
497 struct pci_dn *pdn = pci_get_pdn(dev);
501 if (pdn->pe_number == IODA_INVALID_PE)
503 return &phb->ioda.pe_array[pdn->pe_number];
505 #endif /* CONFIG_PCI_MSI */
507 static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
509 struct pci_dev *parent;
510 uint8_t bcomp, dcomp, fcomp;
511 long rc, rid_end, rid;
513 /* Bus validation ? */
517 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
518 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
519 parent = pe->pbus->self;
520 if (pe->flags & PNV_IODA_PE_BUS_ALL)
521 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
526 case 1: bcomp = OpalPciBusAll; break;
527 case 2: bcomp = OpalPciBus7Bits; break;
528 case 4: bcomp = OpalPciBus6Bits; break;
529 case 8: bcomp = OpalPciBus5Bits; break;
530 case 16: bcomp = OpalPciBus4Bits; break;
531 case 32: bcomp = OpalPciBus3Bits; break;
533 pr_err("%s: Number of subordinate busses %d"
535 pci_name(pe->pbus->self), count);
536 /* Do an exact match only */
537 bcomp = OpalPciBusAll;
539 rid_end = pe->rid + (count << 8);
541 parent = pe->pdev->bus->self;
542 bcomp = OpalPciBusAll;
543 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
544 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
545 rid_end = pe->rid + 1;
549 * Associate PE in PELT. We need add the PE into the
550 * corresponding PELT-V as well. Otherwise, the error
551 * originated from the PE might contribute to other
554 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
555 bcomp, dcomp, fcomp, OPAL_MAP_PE);
557 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
561 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
562 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
564 pe_warn(pe, "OPAL error %d adding self to PELTV\n", rc);
565 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
566 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
568 /* Add to all parents PELT-V */
570 struct pci_dn *pdn = pci_get_pdn(parent);
571 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
572 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
573 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
574 /* XXX What to do in case of error ? */
576 parent = parent->bus->self;
578 /* Setup reverse map */
579 for (rid = pe->rid; rid < rid_end; rid++)
580 phb->ioda.pe_rmap[rid] = pe->pe_number;
582 /* Setup one MVTs on IODA1 */
583 if (phb->type == PNV_PHB_IODA1) {
584 pe->mve_number = pe->pe_number;
585 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
588 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
592 rc = opal_pci_set_mve_enable(phb->opal_id,
593 pe->mve_number, OPAL_ENABLE_MVE);
595 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
600 } else if (phb->type == PNV_PHB_IODA2)
606 static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
607 struct pnv_ioda_pe *pe)
609 struct pnv_ioda_pe *lpe;
611 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
612 if (lpe->dma_weight < pe->dma_weight) {
613 list_add_tail(&pe->dma_link, &lpe->dma_link);
617 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
620 static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
622 /* This is quite simplistic. The "base" weight of a device
623 * is 10. 0 means no DMA is to be accounted for it.
626 /* If it's a bridge, no DMA */
627 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
630 /* Reduce the weight of slow USB controllers */
631 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
632 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
633 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
636 /* Increase the weight of RAID (includes Obsidian) */
637 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
645 static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
647 struct pci_controller *hose = pci_bus_to_host(dev->bus);
648 struct pnv_phb *phb = hose->private_data;
649 struct pci_dn *pdn = pci_get_pdn(dev);
650 struct pnv_ioda_pe *pe;
654 pr_err("%s: Device tree node not associated properly\n",
658 if (pdn->pe_number != IODA_INVALID_PE)
661 /* PE#0 has been pre-set */
662 if (dev->bus->number == 0)
665 pe_num = pnv_ioda_alloc_pe(phb);
666 if (pe_num == IODA_INVALID_PE) {
667 pr_warning("%s: Not enough PE# available, disabling device\n",
672 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
673 * pointer in the PE data structure, both should be destroyed at the
674 * same time. However, this needs to be looked at more closely again
675 * once we actually start removing things (Hotplug, SR-IOV, ...)
677 * At some point we want to remove the PDN completely anyways
679 pe = &phb->ioda.pe_array[pe_num];
682 pdn->pe_number = pe_num;
687 pe->rid = dev->bus->number << 8 | pdn->devfn;
689 pe_info(pe, "Associated device to PE\n");
691 if (pnv_ioda_configure_pe(phb, pe)) {
692 /* XXX What do we do here ? */
694 pnv_ioda_free_pe(phb, pe_num);
695 pdn->pe_number = IODA_INVALID_PE;
701 /* Assign a DMA weight to the device */
702 pe->dma_weight = pnv_ioda_dma_weight(dev);
703 if (pe->dma_weight != 0) {
704 phb->ioda.dma_weight += pe->dma_weight;
705 phb->ioda.dma_pe_count++;
709 pnv_ioda_link_pe_by_weight(phb, pe);
713 #endif /* Useful for SRIOV case */
715 static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
719 list_for_each_entry(dev, &bus->devices, bus_list) {
720 struct pci_dn *pdn = pci_get_pdn(dev);
723 pr_warn("%s: No device node associated with device !\n",
728 pdn->pe_number = pe->pe_number;
729 pe->dma_weight += pnv_ioda_dma_weight(dev);
730 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
731 pnv_ioda_setup_same_PE(dev->subordinate, pe);
736 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
737 * single PCI bus. Another one that contains the primary PCI bus and its
738 * subordinate PCI devices and buses. The second type of PE is normally
739 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
741 static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
743 struct pci_controller *hose = pci_bus_to_host(bus);
744 struct pnv_phb *phb = hose->private_data;
745 struct pnv_ioda_pe *pe;
746 int pe_num = IODA_INVALID_PE;
748 /* Check if PE is determined by M64 */
749 if (phb->pick_m64_pe)
750 pe_num = phb->pick_m64_pe(phb, bus, all);
752 /* The PE number isn't pinned by M64 */
753 if (pe_num == IODA_INVALID_PE)
754 pe_num = pnv_ioda_alloc_pe(phb);
756 if (pe_num == IODA_INVALID_PE) {
757 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
758 __func__, pci_domain_nr(bus), bus->number);
762 pe = &phb->ioda.pe_array[pe_num];
763 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
768 pe->rid = bus->busn_res.start << 8;
772 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
773 bus->busn_res.start, bus->busn_res.end, pe_num);
775 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
776 bus->busn_res.start, pe_num);
778 if (pnv_ioda_configure_pe(phb, pe)) {
779 /* XXX What do we do here ? */
781 pnv_ioda_free_pe(phb, pe_num);
786 /* Associate it with all child devices */
787 pnv_ioda_setup_same_PE(bus, pe);
789 /* Put PE to the list */
790 list_add_tail(&pe->list, &phb->ioda.pe_list);
792 /* Account for one DMA PE if at least one DMA capable device exist
795 if (pe->dma_weight != 0) {
796 phb->ioda.dma_weight += pe->dma_weight;
797 phb->ioda.dma_pe_count++;
801 pnv_ioda_link_pe_by_weight(phb, pe);
804 static void pnv_ioda_setup_PEs(struct pci_bus *bus)
808 pnv_ioda_setup_bus_PE(bus, 0);
810 list_for_each_entry(dev, &bus->devices, bus_list) {
811 if (dev->subordinate) {
812 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
813 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
815 pnv_ioda_setup_PEs(dev->subordinate);
821 * Configure PEs so that the downstream PCI buses and devices
822 * could have their associated PE#. Unfortunately, we didn't
823 * figure out the way to identify the PLX bridge yet. So we
824 * simply put the PCI bus and the subordinate behind the root
825 * port to PE# here. The game rule here is expected to be changed
826 * as soon as we can detected PLX bridge correctly.
828 static void pnv_pci_ioda_setup_PEs(void)
830 struct pci_controller *hose, *tmp;
833 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
834 phb = hose->private_data;
836 /* M64 layout might affect PE allocation */
837 if (phb->alloc_m64_pe)
838 phb->alloc_m64_pe(phb);
840 pnv_ioda_setup_PEs(hose->bus);
844 static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
846 struct pci_dn *pdn = pci_get_pdn(pdev);
847 struct pnv_ioda_pe *pe;
850 * The function can be called while the PE#
851 * hasn't been assigned. Do nothing for the
854 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
857 pe = &phb->ioda.pe_array[pdn->pe_number];
858 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
859 set_iommu_table_base_and_group(&pdev->dev, &pe->tce32_table);
862 static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
863 struct pci_dev *pdev, u64 dma_mask)
865 struct pci_dn *pdn = pci_get_pdn(pdev);
866 struct pnv_ioda_pe *pe;
870 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
873 pe = &phb->ioda.pe_array[pdn->pe_number];
874 if (pe->tce_bypass_enabled) {
875 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
876 bypass = (dma_mask >= top);
880 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
881 set_dma_ops(&pdev->dev, &dma_direct_ops);
882 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
884 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
885 set_dma_ops(&pdev->dev, &dma_iommu_ops);
886 set_iommu_table_base(&pdev->dev, &pe->tce32_table);
888 *pdev->dev.dma_mask = dma_mask;
892 static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
894 bool add_to_iommu_group)
898 list_for_each_entry(dev, &bus->devices, bus_list) {
899 if (add_to_iommu_group)
900 set_iommu_table_base_and_group(&dev->dev,
903 set_iommu_table_base(&dev->dev, &pe->tce32_table);
905 if (dev->subordinate)
906 pnv_ioda_setup_bus_dma(pe, dev->subordinate,
911 static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
912 struct iommu_table *tbl,
913 __be64 *startp, __be64 *endp, bool rm)
915 __be64 __iomem *invalidate = rm ?
916 (__be64 __iomem *)pe->tce_inval_reg_phys :
917 (__be64 __iomem *)tbl->it_index;
918 unsigned long start, end, inc;
919 const unsigned shift = tbl->it_page_shift;
921 start = __pa(startp);
924 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
928 inc = 128ull << shift;
929 start |= tbl->it_busno;
930 end |= tbl->it_busno;
931 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
932 /* p7ioc-style invalidation, 2 TCEs per write */
933 start |= (1ull << 63);
937 /* Default (older HW) */
941 end |= inc - 1; /* round up end to be different than start */
943 mb(); /* Ensure above stores are visible */
944 while (start <= end) {
946 __raw_rm_writeq(cpu_to_be64(start), invalidate);
948 __raw_writeq(cpu_to_be64(start), invalidate);
953 * The iommu layer will do another mb() for us on build()
954 * and we don't care on free()
958 static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
959 struct iommu_table *tbl,
960 __be64 *startp, __be64 *endp, bool rm)
962 unsigned long start, end, inc;
963 __be64 __iomem *invalidate = rm ?
964 (__be64 __iomem *)pe->tce_inval_reg_phys :
965 (__be64 __iomem *)tbl->it_index;
966 const unsigned shift = tbl->it_page_shift;
968 /* We'll invalidate DMA address in PE scope */
969 start = 0x2ull << 60;
970 start |= (pe->pe_number & 0xFF);
973 /* Figure out the start, end and step */
974 inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
975 start |= (inc << shift);
976 inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
977 end |= (inc << shift);
978 inc = (0x1ull << shift);
981 while (start <= end) {
983 __raw_rm_writeq(cpu_to_be64(start), invalidate);
985 __raw_writeq(cpu_to_be64(start), invalidate);
990 void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
991 __be64 *startp, __be64 *endp, bool rm)
993 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
995 struct pnv_phb *phb = pe->phb;
997 if (phb->type == PNV_PHB_IODA1)
998 pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
1000 pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
1003 static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1004 struct pnv_ioda_pe *pe, unsigned int base,
1008 struct page *tce_mem = NULL;
1009 const __be64 *swinvp;
1010 struct iommu_table *tbl;
1015 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
1016 #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
1018 /* XXX FIXME: Handle 64-bit only DMA devices */
1019 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1020 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1022 /* We shouldn't already have a 32-bit DMA associated */
1023 if (WARN_ON(pe->tce32_seg >= 0))
1026 /* Grab a 32-bit TCE table */
1027 pe->tce32_seg = base;
1028 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1029 (base << 28), ((base + segs) << 28) - 1);
1031 /* XXX Currently, we allocate one big contiguous table for the
1032 * TCEs. We only really need one chunk per 256M of TCE space
1033 * (ie per segment) but that's an optimization for later, it
1034 * requires some added smarts with our get/put_tce implementation
1036 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1037 get_order(TCE32_TABLE_SIZE * segs));
1039 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1042 addr = page_address(tce_mem);
1043 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1046 for (i = 0; i < segs; i++) {
1047 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1050 __pa(addr) + TCE32_TABLE_SIZE * i,
1051 TCE32_TABLE_SIZE, 0x1000);
1053 pe_err(pe, " Failed to configure 32-bit TCE table,"
1059 /* Setup linux iommu table */
1060 tbl = &pe->tce32_table;
1061 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
1062 base << 28, IOMMU_PAGE_SHIFT_4K);
1064 /* OPAL variant of P7IOC SW invalidated TCEs */
1065 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1067 /* We need a couple more fields -- an address and a data
1068 * to or. Since the bus is only printed out on table free
1069 * errors, and on the first pass the data will be a relative
1070 * bus number, print that out instead.
1072 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1073 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1075 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1076 TCE_PCI_SWINV_FREE |
1077 TCE_PCI_SWINV_PAIR);
1079 iommu_init_table(tbl, phb->hose->node);
1080 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
1083 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
1085 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
1089 /* XXX Failure: Try to fallback to 64-bit only ? */
1090 if (pe->tce32_seg >= 0)
1093 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
1096 static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
1098 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
1100 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1103 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1105 phys_addr_t top = memblock_end_of_DRAM();
1107 top = roundup_pow_of_two(top);
1108 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1111 pe->tce_bypass_base,
1114 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1117 pe->tce_bypass_base,
1121 * EEH needs the mapping between IOMMU table and group
1122 * of those VFIO/KVM pass-through devices. We can postpone
1123 * resetting DMA ops until the DMA mask is configured in
1127 set_iommu_table_base(&pe->pdev->dev, tbl);
1129 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
1132 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1134 pe->tce_bypass_enabled = enable;
1137 static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
1138 struct pnv_ioda_pe *pe)
1140 /* TVE #1 is selected by PCI address bit 59 */
1141 pe->tce_bypass_base = 1ull << 59;
1143 /* Install set_bypass callback for VFIO */
1144 pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass;
1146 /* Enable bypass by default */
1147 pnv_pci_ioda2_set_bypass(&pe->tce32_table, true);
1150 static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1151 struct pnv_ioda_pe *pe)
1153 struct page *tce_mem = NULL;
1155 const __be64 *swinvp;
1156 struct iommu_table *tbl;
1157 unsigned int tce_table_size, end;
1160 /* We shouldn't already have a 32-bit DMA associated */
1161 if (WARN_ON(pe->tce32_seg >= 0))
1164 /* The PE will reserve all possible 32-bits space */
1166 end = (1 << ilog2(phb->ioda.m32_pci_base));
1167 tce_table_size = (end / 0x1000) * 8;
1168 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1171 /* Allocate TCE table */
1172 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1173 get_order(tce_table_size));
1175 pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
1178 addr = page_address(tce_mem);
1179 memset(addr, 0, tce_table_size);
1182 * Map TCE table through TVT. The TVE index is the PE number
1183 * shifted by 1 bit for 32-bits DMA space.
1185 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1186 pe->pe_number << 1, 1, __pa(addr),
1187 tce_table_size, 0x1000);
1189 pe_err(pe, "Failed to configure 32-bit TCE table,"
1194 /* Setup linux iommu table */
1195 tbl = &pe->tce32_table;
1196 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
1197 IOMMU_PAGE_SHIFT_4K);
1199 /* OPAL variant of PHB3 invalidated TCEs */
1200 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1202 /* We need a couple more fields -- an address and a data
1203 * to or. Since the bus is only printed out on table free
1204 * errors, and on the first pass the data will be a relative
1205 * bus number, print that out instead.
1207 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1208 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1210 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
1212 iommu_init_table(tbl, phb->hose->node);
1213 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
1216 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
1218 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
1220 /* Also create a bypass window */
1221 pnv_pci_ioda2_setup_bypass_pe(phb, pe);
1224 if (pe->tce32_seg >= 0)
1227 __free_pages(tce_mem, get_order(tce_table_size));
1230 static void pnv_ioda_setup_dma(struct pnv_phb *phb)
1232 struct pci_controller *hose = phb->hose;
1233 unsigned int residual, remaining, segs, tw, base;
1234 struct pnv_ioda_pe *pe;
1236 /* If we have more PE# than segments available, hand out one
1237 * per PE until we run out and let the rest fail. If not,
1238 * then we assign at least one segment per PE, plus more based
1239 * on the amount of devices under that PE
1241 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
1244 residual = phb->ioda.tce32_count -
1245 phb->ioda.dma_pe_count;
1247 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
1248 hose->global_number, phb->ioda.tce32_count);
1249 pr_info("PCI: %d PE# for a total weight of %d\n",
1250 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
1252 /* Walk our PE list and configure their DMA segments, hand them
1253 * out one base segment plus any residual segments based on
1256 remaining = phb->ioda.tce32_count;
1257 tw = phb->ioda.dma_weight;
1259 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
1260 if (!pe->dma_weight)
1263 pe_warn(pe, "No DMA32 resources available\n");
1268 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
1269 if (segs > remaining)
1274 * For IODA2 compliant PHB3, we needn't care about the weight.
1275 * The all available 32-bits DMA space will be assigned to
1278 if (phb->type == PNV_PHB_IODA1) {
1279 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1280 pe->dma_weight, segs);
1281 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1283 pe_info(pe, "Assign DMA32 space\n");
1285 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1293 #ifdef CONFIG_PCI_MSI
1294 static void pnv_ioda2_msi_eoi(struct irq_data *d)
1296 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1297 struct irq_chip *chip = irq_data_get_irq_chip(d);
1298 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
1302 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
1308 static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
1309 unsigned int hwirq, unsigned int virq,
1310 unsigned int is_64, struct msi_msg *msg)
1312 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1313 struct pci_dn *pdn = pci_get_pdn(dev);
1314 struct irq_data *idata;
1315 struct irq_chip *ichip;
1316 unsigned int xive_num = hwirq - phb->msi_base;
1320 /* No PE assigned ? bail out ... no MSI for you ! */
1324 /* Check if we have an MVE */
1325 if (pe->mve_number < 0)
1328 /* Force 32-bit MSI on some broken devices */
1329 if (pdn && pdn->force_32bit_msi)
1332 /* Assign XIVE to PE */
1333 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1335 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1336 pci_name(dev), rc, xive_num);
1343 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1346 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1350 msg->address_hi = be64_to_cpu(addr64) >> 32;
1351 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
1355 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1358 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1362 msg->address_hi = 0;
1363 msg->address_lo = be32_to_cpu(addr32);
1365 msg->data = be32_to_cpu(data);
1368 * Change the IRQ chip for the MSI interrupts on PHB3.
1369 * The corresponding IRQ chip should be populated for
1372 if (phb->type == PNV_PHB_IODA2) {
1373 if (!phb->ioda.irq_chip_init) {
1374 idata = irq_get_irq_data(virq);
1375 ichip = irq_data_get_irq_chip(idata);
1376 phb->ioda.irq_chip_init = 1;
1377 phb->ioda.irq_chip = *ichip;
1378 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
1381 irq_set_chip(virq, &phb->ioda.irq_chip);
1384 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1385 " address=%x_%08x data=%x PE# %d\n",
1386 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
1387 msg->address_hi, msg->address_lo, data, pe->pe_number);
1392 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1395 const __be32 *prop = of_get_property(phb->hose->dn,
1396 "ibm,opal-msi-ranges", NULL);
1399 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1404 phb->msi_base = be32_to_cpup(prop);
1405 count = be32_to_cpup(prop + 1);
1406 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
1407 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1408 phb->hose->global_number);
1412 phb->msi_setup = pnv_pci_ioda_msi_setup;
1413 phb->msi32_support = 1;
1414 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
1415 count, phb->msi_base);
1418 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1419 #endif /* CONFIG_PCI_MSI */
1422 * This function is supposed to be called on basis of PE from top
1423 * to bottom style. So the the I/O or MMIO segment assigned to
1424 * parent PE could be overrided by its child PEs if necessary.
1426 static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
1427 struct pnv_ioda_pe *pe)
1429 struct pnv_phb *phb = hose->private_data;
1430 struct pci_bus_region region;
1431 struct resource *res;
1436 * NOTE: We only care PCI bus based PE for now. For PCI
1437 * device based PE, for example SRIOV sensitive VF should
1438 * be figured out later.
1440 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
1442 pci_bus_for_each_resource(pe->pbus, res, i) {
1443 if (!res || !res->flags ||
1444 res->start > res->end)
1447 if (res->flags & IORESOURCE_IO) {
1448 region.start = res->start - phb->ioda.io_pci_base;
1449 region.end = res->end - phb->ioda.io_pci_base;
1450 index = region.start / phb->ioda.io_segsize;
1452 while (index < phb->ioda.total_pe &&
1453 region.start <= region.end) {
1454 phb->ioda.io_segmap[index] = pe->pe_number;
1455 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1456 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
1457 if (rc != OPAL_SUCCESS) {
1458 pr_err("%s: OPAL error %d when mapping IO "
1459 "segment #%d to PE#%d\n",
1460 __func__, rc, index, pe->pe_number);
1464 region.start += phb->ioda.io_segsize;
1467 } else if (res->flags & IORESOURCE_MEM) {
1468 region.start = res->start -
1469 hose->mem_offset[0] -
1470 phb->ioda.m32_pci_base;
1471 region.end = res->end -
1472 hose->mem_offset[0] -
1473 phb->ioda.m32_pci_base;
1474 index = region.start / phb->ioda.m32_segsize;
1476 while (index < phb->ioda.total_pe &&
1477 region.start <= region.end) {
1478 phb->ioda.m32_segmap[index] = pe->pe_number;
1479 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1480 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
1481 if (rc != OPAL_SUCCESS) {
1482 pr_err("%s: OPAL error %d when mapping M32 "
1483 "segment#%d to PE#%d",
1484 __func__, rc, index, pe->pe_number);
1488 region.start += phb->ioda.m32_segsize;
1495 static void pnv_pci_ioda_setup_seg(void)
1497 struct pci_controller *tmp, *hose;
1498 struct pnv_phb *phb;
1499 struct pnv_ioda_pe *pe;
1501 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1502 phb = hose->private_data;
1503 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
1504 pnv_ioda_setup_pe_seg(hose, pe);
1509 static void pnv_pci_ioda_setup_DMA(void)
1511 struct pci_controller *hose, *tmp;
1512 struct pnv_phb *phb;
1514 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1515 pnv_ioda_setup_dma(hose->private_data);
1517 /* Mark the PHB initialization done */
1518 phb = hose->private_data;
1519 phb->initialized = 1;
1523 static void pnv_pci_ioda_create_dbgfs(void)
1525 #ifdef CONFIG_DEBUG_FS
1526 struct pci_controller *hose, *tmp;
1527 struct pnv_phb *phb;
1530 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1531 phb = hose->private_data;
1533 sprintf(name, "PCI%04x", hose->global_number);
1534 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
1536 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
1537 __func__, hose->global_number);
1539 #endif /* CONFIG_DEBUG_FS */
1542 static void pnv_pci_ioda_fixup(void)
1544 pnv_pci_ioda_setup_PEs();
1545 pnv_pci_ioda_setup_seg();
1546 pnv_pci_ioda_setup_DMA();
1548 pnv_pci_ioda_create_dbgfs();
1552 eeh_addr_cache_build();
1557 * Returns the alignment for I/O or memory windows for P2P
1558 * bridges. That actually depends on how PEs are segmented.
1559 * For now, we return I/O or M32 segment size for PE sensitive
1560 * P2P bridges. Otherwise, the default values (4KiB for I/O,
1561 * 1MiB for memory) will be returned.
1563 * The current PCI bus might be put into one PE, which was
1564 * create against the parent PCI bridge. For that case, we
1565 * needn't enlarge the alignment so that we can save some
1568 static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
1571 struct pci_dev *bridge;
1572 struct pci_controller *hose = pci_bus_to_host(bus);
1573 struct pnv_phb *phb = hose->private_data;
1574 int num_pci_bridges = 0;
1578 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
1580 if (num_pci_bridges >= 2)
1584 bridge = bridge->bus->self;
1587 /* We fail back to M32 if M64 isn't supported */
1588 if (phb->ioda.m64_segsize &&
1589 pnv_pci_is_mem_pref_64(type))
1590 return phb->ioda.m64_segsize;
1591 if (type & IORESOURCE_MEM)
1592 return phb->ioda.m32_segsize;
1594 return phb->ioda.io_segsize;
1597 /* Prevent enabling devices for which we couldn't properly
1600 static int pnv_pci_enable_device_hook(struct pci_dev *dev)
1602 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1603 struct pnv_phb *phb = hose->private_data;
1606 /* The function is probably called while the PEs have
1607 * not be created yet. For example, resource reassignment
1608 * during PCI probe period. We just skip the check if
1611 if (!phb->initialized)
1614 pdn = pci_get_pdn(dev);
1615 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1621 static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1624 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1627 static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
1629 opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET,
1633 static void __init pnv_pci_init_ioda_phb(struct device_node *np,
1634 u64 hub_id, int ioda_type)
1636 struct pci_controller *hose;
1637 struct pnv_phb *phb;
1638 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
1639 const __be64 *prop64;
1640 const __be32 *prop32;
1646 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
1648 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1650 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
1653 phb_id = be64_to_cpup(prop64);
1654 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
1656 phb = alloc_bootmem(sizeof(struct pnv_phb));
1658 pr_err(" Out of memory !\n");
1662 /* Allocate PCI controller */
1663 memset(phb, 0, sizeof(struct pnv_phb));
1664 phb->hose = hose = pcibios_alloc_controller(np);
1666 pr_err(" Can't allocate PCI controller for %s\n",
1668 free_bootmem((unsigned long)phb, sizeof(struct pnv_phb));
1672 spin_lock_init(&phb->lock);
1673 prop32 = of_get_property(np, "bus-range", &len);
1674 if (prop32 && len == 8) {
1675 hose->first_busno = be32_to_cpu(prop32[0]);
1676 hose->last_busno = be32_to_cpu(prop32[1]);
1678 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
1679 hose->first_busno = 0;
1680 hose->last_busno = 0xff;
1682 hose->private_data = phb;
1683 phb->hub_id = hub_id;
1684 phb->opal_id = phb_id;
1685 phb->type = ioda_type;
1687 /* Detect specific models for error handling */
1688 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
1689 phb->model = PNV_PHB_MODEL_P7IOC;
1690 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
1691 phb->model = PNV_PHB_MODEL_PHB3;
1693 phb->model = PNV_PHB_MODEL_UNKNOWN;
1695 /* Parse 32-bit and IO ranges (if any) */
1696 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
1699 phb->regs = of_iomap(np, 0);
1700 if (phb->regs == NULL)
1701 pr_err(" Failed to map registers !\n");
1703 /* Initialize more IODA stuff */
1704 phb->ioda.total_pe = 1;
1705 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
1707 phb->ioda.total_pe = be32_to_cpup(prop32);
1708 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
1710 phb->ioda.reserved_pe = be32_to_cpup(prop32);
1712 /* Parse 64-bit MMIO range */
1713 pnv_ioda_parse_m64_window(phb);
1715 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
1716 /* FW Has already off top 64k of M32 space (MSI space) */
1717 phb->ioda.m32_size += 0x10000;
1719 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
1720 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
1721 phb->ioda.io_size = hose->pci_io_size;
1722 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
1723 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
1725 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
1726 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
1728 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
1729 if (phb->type == PNV_PHB_IODA1) {
1731 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
1734 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
1735 aux = alloc_bootmem(size);
1736 memset(aux, 0, size);
1737 phb->ioda.pe_alloc = aux;
1738 phb->ioda.m32_segmap = aux + m32map_off;
1739 if (phb->type == PNV_PHB_IODA1)
1740 phb->ioda.io_segmap = aux + iomap_off;
1741 phb->ioda.pe_array = aux + pemap_off;
1742 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
1744 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
1745 INIT_LIST_HEAD(&phb->ioda.pe_list);
1747 /* Calculate how many 32-bit TCE segments we have */
1748 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
1750 #if 0 /* We should really do that ... */
1751 rc = opal_pci_set_phb_mem_window(opal->phb_id,
1754 starting_real_address,
1755 starting_pci_address,
1759 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
1760 phb->ioda.total_pe, phb->ioda.reserved_pe,
1761 phb->ioda.m32_size, phb->ioda.m32_segsize);
1762 if (phb->ioda.m64_size)
1763 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
1764 phb->ioda.m64_size, phb->ioda.m64_segsize);
1765 if (phb->ioda.io_size)
1766 pr_info(" IO: 0x%x [segment=0x%x]\n",
1767 phb->ioda.io_size, phb->ioda.io_segsize);
1770 phb->hose->ops = &pnv_pci_ops;
1771 phb->get_pe_state = pnv_ioda_get_pe_state;
1772 phb->freeze_pe = pnv_ioda_freeze_pe;
1773 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
1775 phb->eeh_ops = &ioda_eeh_ops;
1778 /* Setup RID -> PE mapping function */
1779 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
1782 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
1783 phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
1785 /* Setup shutdown function for kexec */
1786 phb->shutdown = pnv_pci_ioda_shutdown;
1788 /* Setup MSI support */
1789 pnv_pci_init_ioda_msis(phb);
1792 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
1793 * to let the PCI core do resource assignment. It's supposed
1794 * that the PCI core will do correct I/O and MMIO alignment
1795 * for the P2P bridge bars so that each PCI bus (excluding
1796 * the child P2P bridges) can form individual PE.
1798 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
1799 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
1800 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
1801 ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
1802 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
1804 /* Reset IODA tables to a clean state */
1805 rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
1807 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
1809 /* If we're running in kdump kerenl, the previous kerenl never
1810 * shutdown PCI devices correctly. We already got IODA table
1811 * cleaned out. So we have to issue PHB reset to stop all PCI
1812 * transactions from previous kerenl.
1814 if (is_kdump_kernel()) {
1815 pr_info(" Issue PHB reset ...\n");
1816 ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
1817 ioda_eeh_phb_reset(hose, OPAL_DEASSERT_RESET);
1820 /* Configure M64 window */
1821 if (phb->init_m64 && phb->init_m64(phb))
1822 hose->mem_resources[1].flags = 0;
1825 void __init pnv_pci_init_ioda2_phb(struct device_node *np)
1827 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
1830 void __init pnv_pci_init_ioda_hub(struct device_node *np)
1832 struct device_node *phbn;
1833 const __be64 *prop64;
1836 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
1838 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
1840 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
1843 hub_id = be64_to_cpup(prop64);
1844 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
1846 /* Count child PHBs */
1847 for_each_child_of_node(np, phbn) {
1848 /* Look for IODA1 PHBs */
1849 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
1850 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);