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/delay.h>
17 #include <linux/string.h>
18 #include <linux/init.h>
19 #include <linux/bootmem.h>
20 #include <linux/irq.h>
22 #include <linux/msi.h>
24 #include <asm/sections.h>
27 #include <asm/pci-bridge.h>
28 #include <asm/machdep.h>
29 #include <asm/ppc-pci.h>
31 #include <asm/iommu.h>
37 #define define_pe_printk_level(func, kern_level) \
38 static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
40 struct va_format vaf; \
45 va_start(args, fmt); \
51 strlcpy(pfix, dev_name(&pe->pdev->dev), \
54 sprintf(pfix, "%04x:%02x ", \
55 pci_domain_nr(pe->pbus), \
57 r = printk(kern_level "pci %s: [PE# %.3d] %pV", \
58 pfix, pe->pe_number, &vaf); \
65 define_pe_printk_level(pe_err, KERN_ERR);
66 define_pe_printk_level(pe_warn, KERN_WARNING);
67 define_pe_printk_level(pe_info, KERN_INFO);
69 static struct pci_dn *pnv_ioda_get_pdn(struct pci_dev *dev)
71 struct device_node *np;
73 np = pci_device_to_OF_node(dev);
79 static int __devinit pnv_ioda_alloc_pe(struct pnv_phb *phb)
84 pe = find_next_zero_bit(phb->ioda.pe_alloc,
85 phb->ioda.total_pe, 0);
86 if (pe >= phb->ioda.total_pe)
87 return IODA_INVALID_PE;
88 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
90 phb->ioda.pe_array[pe].pe_number = pe;
94 static void __devinit pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
96 WARN_ON(phb->ioda.pe_array[pe].pdev);
98 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
99 clear_bit(pe, phb->ioda.pe_alloc);
102 /* Currently those 2 are only used when MSIs are enabled, this will change
103 * but in the meantime, we need to protect them to avoid warnings
105 #ifdef CONFIG_PCI_MSI
106 static struct pnv_ioda_pe * __devinit pnv_ioda_get_pe(struct pci_dev *dev)
108 struct pci_controller *hose = pci_bus_to_host(dev->bus);
109 struct pnv_phb *phb = hose->private_data;
110 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
114 if (pdn->pe_number == IODA_INVALID_PE)
116 return &phb->ioda.pe_array[pdn->pe_number];
118 #endif /* CONFIG_PCI_MSI */
120 static int __devinit pnv_ioda_configure_pe(struct pnv_phb *phb,
121 struct pnv_ioda_pe *pe)
123 struct pci_dev *parent;
124 uint8_t bcomp, dcomp, fcomp;
125 long rc, rid_end, rid;
127 /* Bus validation ? */
131 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
132 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
133 parent = pe->pbus->self;
134 if (pe->flags & PNV_IODA_PE_BUS_ALL)
135 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
140 case 1: bcomp = OpalPciBusAll; break;
141 case 2: bcomp = OpalPciBus7Bits; break;
142 case 4: bcomp = OpalPciBus6Bits; break;
143 case 8: bcomp = OpalPciBus5Bits; break;
144 case 16: bcomp = OpalPciBus4Bits; break;
145 case 32: bcomp = OpalPciBus3Bits; break;
147 pr_err("%s: Number of subordinate busses %d"
149 pci_name(pe->pbus->self), count);
150 /* Do an exact match only */
151 bcomp = OpalPciBusAll;
153 rid_end = pe->rid + (count << 8);
155 parent = pe->pdev->bus->self;
156 bcomp = OpalPciBusAll;
157 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
158 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
159 rid_end = pe->rid + 1;
162 /* Associate PE in PELT */
163 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
164 bcomp, dcomp, fcomp, OPAL_MAP_PE);
166 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
169 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
170 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
172 /* Add to all parents PELT-V */
174 struct pci_dn *pdn = pnv_ioda_get_pdn(parent);
175 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
176 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
177 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
178 /* XXX What to do in case of error ? */
180 parent = parent->bus->self;
182 /* Setup reverse map */
183 for (rid = pe->rid; rid < rid_end; rid++)
184 phb->ioda.pe_rmap[rid] = pe->pe_number;
186 /* Setup one MVTs on IODA1 */
187 if (phb->type == PNV_PHB_IODA1) {
188 pe->mve_number = pe->pe_number;
189 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
192 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
196 rc = opal_pci_set_mve_enable(phb->opal_id,
197 pe->mve_number, OPAL_ENABLE_MVE);
199 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
204 } else if (phb->type == PNV_PHB_IODA2)
210 static void __devinit pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
211 struct pnv_ioda_pe *pe)
213 struct pnv_ioda_pe *lpe;
215 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
216 if (lpe->dma_weight < pe->dma_weight) {
217 list_add_tail(&pe->dma_link, &lpe->dma_link);
221 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
224 static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
226 /* This is quite simplistic. The "base" weight of a device
227 * is 10. 0 means no DMA is to be accounted for it.
230 /* If it's a bridge, no DMA */
231 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
234 /* Reduce the weight of slow USB controllers */
235 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
236 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
237 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
240 /* Increase the weight of RAID (includes Obsidian) */
241 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
249 static struct pnv_ioda_pe * __devinit pnv_ioda_setup_dev_PE(struct pci_dev *dev)
251 struct pci_controller *hose = pci_bus_to_host(dev->bus);
252 struct pnv_phb *phb = hose->private_data;
253 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
254 struct pnv_ioda_pe *pe;
258 pr_err("%s: Device tree node not associated properly\n",
262 if (pdn->pe_number != IODA_INVALID_PE)
265 /* PE#0 has been pre-set */
266 if (dev->bus->number == 0)
269 pe_num = pnv_ioda_alloc_pe(phb);
270 if (pe_num == IODA_INVALID_PE) {
271 pr_warning("%s: Not enough PE# available, disabling device\n",
276 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
277 * pointer in the PE data structure, both should be destroyed at the
278 * same time. However, this needs to be looked at more closely again
279 * once we actually start removing things (Hotplug, SR-IOV, ...)
281 * At some point we want to remove the PDN completely anyways
283 pe = &phb->ioda.pe_array[pe_num];
286 pdn->pe_number = pe_num;
291 pe->rid = dev->bus->number << 8 | pdn->devfn;
293 pe_info(pe, "Associated device to PE\n");
295 if (pnv_ioda_configure_pe(phb, pe)) {
296 /* XXX What do we do here ? */
298 pnv_ioda_free_pe(phb, pe_num);
299 pdn->pe_number = IODA_INVALID_PE;
305 /* Assign a DMA weight to the device */
306 pe->dma_weight = pnv_ioda_dma_weight(dev);
307 if (pe->dma_weight != 0) {
308 phb->ioda.dma_weight += pe->dma_weight;
309 phb->ioda.dma_pe_count++;
313 pnv_ioda_link_pe_by_weight(phb, pe);
317 #endif /* Useful for SRIOV case */
319 static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
323 list_for_each_entry(dev, &bus->devices, bus_list) {
324 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
327 pr_warn("%s: No device node associated with device !\n",
333 pdn->pe_number = pe->pe_number;
334 pe->dma_weight += pnv_ioda_dma_weight(dev);
335 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
336 pnv_ioda_setup_same_PE(dev->subordinate, pe);
341 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
342 * single PCI bus. Another one that contains the primary PCI bus and its
343 * subordinate PCI devices and buses. The second type of PE is normally
344 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
346 static void __devinit pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
348 struct pci_controller *hose = pci_bus_to_host(bus);
349 struct pnv_phb *phb = hose->private_data;
350 struct pnv_ioda_pe *pe;
353 pe_num = pnv_ioda_alloc_pe(phb);
354 if (pe_num == IODA_INVALID_PE) {
355 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
356 __func__, pci_domain_nr(bus), bus->number);
360 pe = &phb->ioda.pe_array[pe_num];
361 pe->flags = (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
366 pe->rid = bus->busn_res.start << 8;
370 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
371 bus->busn_res.start, bus->busn_res.end, pe_num);
373 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
374 bus->busn_res.start, pe_num);
376 if (pnv_ioda_configure_pe(phb, pe)) {
377 /* XXX What do we do here ? */
379 pnv_ioda_free_pe(phb, pe_num);
384 /* Associate it with all child devices */
385 pnv_ioda_setup_same_PE(bus, pe);
387 /* Put PE to the list */
388 list_add_tail(&pe->list, &phb->ioda.pe_list);
390 /* Account for one DMA PE if at least one DMA capable device exist
393 if (pe->dma_weight != 0) {
394 phb->ioda.dma_weight += pe->dma_weight;
395 phb->ioda.dma_pe_count++;
399 pnv_ioda_link_pe_by_weight(phb, pe);
402 static void __devinit pnv_ioda_setup_PEs(struct pci_bus *bus)
406 pnv_ioda_setup_bus_PE(bus, 0);
408 list_for_each_entry(dev, &bus->devices, bus_list) {
409 if (dev->subordinate) {
410 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
411 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
413 pnv_ioda_setup_PEs(dev->subordinate);
419 * Configure PEs so that the downstream PCI buses and devices
420 * could have their associated PE#. Unfortunately, we didn't
421 * figure out the way to identify the PLX bridge yet. So we
422 * simply put the PCI bus and the subordinate behind the root
423 * port to PE# here. The game rule here is expected to be changed
424 * as soon as we can detected PLX bridge correctly.
426 static void __devinit pnv_pci_ioda_setup_PEs(void)
428 struct pci_controller *hose, *tmp;
430 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
431 pnv_ioda_setup_PEs(hose->bus);
435 static void __devinit pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb,
438 /* We delay DMA setup after we have assigned all PE# */
441 static void __devinit pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
446 list_for_each_entry(dev, &bus->devices, bus_list) {
447 set_iommu_table_base(&dev->dev, &pe->tce32_table);
448 if (dev->subordinate)
449 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
453 static void __devinit pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
454 struct pnv_ioda_pe *pe,
459 struct page *tce_mem = NULL;
460 const __be64 *swinvp;
461 struct iommu_table *tbl;
466 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
467 #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
469 /* XXX FIXME: Handle 64-bit only DMA devices */
470 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
471 /* XXX FIXME: Allocate multi-level tables on PHB3 */
473 /* We shouldn't already have a 32-bit DMA associated */
474 if (WARN_ON(pe->tce32_seg >= 0))
477 /* Grab a 32-bit TCE table */
478 pe->tce32_seg = base;
479 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
480 (base << 28), ((base + segs) << 28) - 1);
482 /* XXX Currently, we allocate one big contiguous table for the
483 * TCEs. We only really need one chunk per 256M of TCE space
484 * (ie per segment) but that's an optimization for later, it
485 * requires some added smarts with our get/put_tce implementation
487 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
488 get_order(TCE32_TABLE_SIZE * segs));
490 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
493 addr = page_address(tce_mem);
494 memset(addr, 0, TCE32_TABLE_SIZE * segs);
497 for (i = 0; i < segs; i++) {
498 rc = opal_pci_map_pe_dma_window(phb->opal_id,
501 __pa(addr) + TCE32_TABLE_SIZE * i,
502 TCE32_TABLE_SIZE, 0x1000);
504 pe_err(pe, " Failed to configure 32-bit TCE table,"
510 /* Setup linux iommu table */
511 tbl = &pe->tce32_table;
512 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
515 /* OPAL variant of P7IOC SW invalidated TCEs */
516 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
518 /* We need a couple more fields -- an address and a data
519 * to or. Since the bus is only printed out on table free
520 * errors, and on the first pass the data will be a relative
521 * bus number, print that out instead.
524 tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
525 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE
526 | TCE_PCI_SWINV_PAIR;
528 iommu_init_table(tbl, phb->hose->node);
531 set_iommu_table_base(&pe->pdev->dev, tbl);
533 pnv_ioda_setup_bus_dma(pe, pe->pbus);
537 /* XXX Failure: Try to fallback to 64-bit only ? */
538 if (pe->tce32_seg >= 0)
541 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
544 static void __devinit pnv_ioda_setup_dma(struct pnv_phb *phb)
546 struct pci_controller *hose = phb->hose;
547 unsigned int residual, remaining, segs, tw, base;
548 struct pnv_ioda_pe *pe;
550 /* If we have more PE# than segments available, hand out one
551 * per PE until we run out and let the rest fail. If not,
552 * then we assign at least one segment per PE, plus more based
553 * on the amount of devices under that PE
555 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
558 residual = phb->ioda.tce32_count -
559 phb->ioda.dma_pe_count;
561 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
562 hose->global_number, phb->ioda.tce32_count);
563 pr_info("PCI: %d PE# for a total weight of %d\n",
564 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
566 /* Walk our PE list and configure their DMA segments, hand them
567 * out one base segment plus any residual segments based on
570 remaining = phb->ioda.tce32_count;
571 tw = phb->ioda.dma_weight;
573 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
577 pe_warn(pe, "No DMA32 resources available\n");
582 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
583 if (segs > remaining)
586 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
587 pe->dma_weight, segs);
588 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
594 #ifdef CONFIG_PCI_MSI
595 static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
596 unsigned int hwirq, unsigned int is_64,
599 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
600 unsigned int xive_num = hwirq - phb->msi_base;
602 uint32_t addr32, data;
605 /* No PE assigned ? bail out ... no MSI for you ! */
609 /* Check if we have an MVE */
610 if (pe->mve_number < 0)
613 /* Assign XIVE to PE */
614 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
616 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
617 pci_name(dev), rc, xive_num);
622 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
625 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
629 msg->address_hi = addr64 >> 32;
630 msg->address_lo = addr64 & 0xfffffffful;
632 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
635 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
640 msg->address_lo = addr32;
644 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
645 " address=%x_%08x data=%x PE# %d\n",
646 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
647 msg->address_hi, msg->address_lo, data, pe->pe_number);
652 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
654 unsigned int bmap_size;
655 const __be32 *prop = of_get_property(phb->hose->dn,
656 "ibm,opal-msi-ranges", NULL);
659 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
664 phb->msi_base = be32_to_cpup(prop);
665 phb->msi_count = be32_to_cpup(prop + 1);
666 bmap_size = BITS_TO_LONGS(phb->msi_count) * sizeof(unsigned long);
667 phb->msi_map = zalloc_maybe_bootmem(bmap_size, GFP_KERNEL);
669 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
670 phb->hose->global_number);
673 phb->msi_setup = pnv_pci_ioda_msi_setup;
674 phb->msi32_support = 1;
675 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
676 phb->msi_count, phb->msi_base);
679 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
680 #endif /* CONFIG_PCI_MSI */
683 * This function is supposed to be called on basis of PE from top
684 * to bottom style. So the the I/O or MMIO segment assigned to
685 * parent PE could be overrided by its child PEs if necessary.
687 static void __devinit pnv_ioda_setup_pe_seg(struct pci_controller *hose,
688 struct pnv_ioda_pe *pe)
690 struct pnv_phb *phb = hose->private_data;
691 struct pci_bus_region region;
692 struct resource *res;
697 * NOTE: We only care PCI bus based PE for now. For PCI
698 * device based PE, for example SRIOV sensitive VF should
699 * be figured out later.
701 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
703 pci_bus_for_each_resource(pe->pbus, res, i) {
704 if (!res || !res->flags ||
705 res->start > res->end)
708 if (res->flags & IORESOURCE_IO) {
709 region.start = res->start - phb->ioda.io_pci_base;
710 region.end = res->end - phb->ioda.io_pci_base;
711 index = region.start / phb->ioda.io_segsize;
713 while (index < phb->ioda.total_pe &&
714 region.start <= region.end) {
715 phb->ioda.io_segmap[index] = pe->pe_number;
716 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
717 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
718 if (rc != OPAL_SUCCESS) {
719 pr_err("%s: OPAL error %d when mapping IO "
720 "segment #%d to PE#%d\n",
721 __func__, rc, index, pe->pe_number);
725 region.start += phb->ioda.io_segsize;
728 } else if (res->flags & IORESOURCE_MEM) {
729 region.start = res->start -
730 hose->pci_mem_offset -
731 phb->ioda.m32_pci_base;
732 region.end = res->end -
733 hose->pci_mem_offset -
734 phb->ioda.m32_pci_base;
735 index = region.start / phb->ioda.m32_segsize;
737 while (index < phb->ioda.total_pe &&
738 region.start <= region.end) {
739 phb->ioda.m32_segmap[index] = pe->pe_number;
740 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
741 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
742 if (rc != OPAL_SUCCESS) {
743 pr_err("%s: OPAL error %d when mapping M32 "
744 "segment#%d to PE#%d",
745 __func__, rc, index, pe->pe_number);
749 region.start += phb->ioda.m32_segsize;
756 static void __devinit pnv_pci_ioda_setup_seg(void)
758 struct pci_controller *tmp, *hose;
760 struct pnv_ioda_pe *pe;
762 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
763 phb = hose->private_data;
764 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
765 pnv_ioda_setup_pe_seg(hose, pe);
770 static void __devinit pnv_pci_ioda_setup_DMA(void)
772 struct pci_controller *hose, *tmp;
775 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
776 pnv_ioda_setup_dma(hose->private_data);
778 /* Mark the PHB initialization done */
779 phb = hose->private_data;
780 phb->initialized = 1;
784 static void __devinit pnv_pci_ioda_fixup(void)
786 pnv_pci_ioda_setup_PEs();
787 pnv_pci_ioda_setup_seg();
788 pnv_pci_ioda_setup_DMA();
792 * Returns the alignment for I/O or memory windows for P2P
793 * bridges. That actually depends on how PEs are segmented.
794 * For now, we return I/O or M32 segment size for PE sensitive
795 * P2P bridges. Otherwise, the default values (4KiB for I/O,
796 * 1MiB for memory) will be returned.
798 * The current PCI bus might be put into one PE, which was
799 * create against the parent PCI bridge. For that case, we
800 * needn't enlarge the alignment so that we can save some
803 static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
806 struct pci_dev *bridge;
807 struct pci_controller *hose = pci_bus_to_host(bus);
808 struct pnv_phb *phb = hose->private_data;
809 int num_pci_bridges = 0;
813 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
815 if (num_pci_bridges >= 2)
819 bridge = bridge->bus->self;
822 /* We need support prefetchable memory window later */
823 if (type & IORESOURCE_MEM)
824 return phb->ioda.m32_segsize;
826 return phb->ioda.io_segsize;
829 /* Prevent enabling devices for which we couldn't properly
832 static int __devinit pnv_pci_enable_device_hook(struct pci_dev *dev)
834 struct pci_controller *hose = pci_bus_to_host(dev->bus);
835 struct pnv_phb *phb = hose->private_data;
838 /* The function is probably called while the PEs have
839 * not be created yet. For example, resource reassignment
840 * during PCI probe period. We just skip the check if
843 if (!phb->initialized)
846 pdn = pnv_ioda_get_pdn(dev);
847 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
853 static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
856 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
859 void __init pnv_pci_init_ioda1_phb(struct device_node *np)
861 struct pci_controller *hose;
862 static int primary = 1;
864 unsigned long size, m32map_off, iomap_off, pemap_off;
870 pr_info(" Initializing IODA OPAL PHB %s\n", np->full_name);
872 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
874 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
877 phb_id = be64_to_cpup(prop64);
878 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
880 phb = alloc_bootmem(sizeof(struct pnv_phb));
882 memset(phb, 0, sizeof(struct pnv_phb));
883 phb->hose = hose = pcibios_alloc_controller(np);
885 if (!phb || !phb->hose) {
886 pr_err("PCI: Failed to allocate PCI controller for %s\n",
891 spin_lock_init(&phb->lock);
892 /* XXX Use device-tree */
893 hose->first_busno = 0;
894 hose->last_busno = 0xff;
895 hose->private_data = phb;
896 phb->opal_id = phb_id;
897 phb->type = PNV_PHB_IODA1;
899 /* Detect specific models for error handling */
900 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
901 phb->model = PNV_PHB_MODEL_P7IOC;
903 phb->model = PNV_PHB_MODEL_UNKNOWN;
905 /* We parse "ranges" now since we need to deduce the register base
908 pci_process_bridge_OF_ranges(phb->hose, np, primary);
911 /* Magic formula from Milton */
912 phb->regs = of_iomap(np, 0);
913 if (phb->regs == NULL)
914 pr_err(" Failed to map registers !\n");
917 /* XXX This is hack-a-thon. This needs to be changed so that:
918 * - we obtain stuff like PE# etc... from device-tree
919 * - we properly re-allocate M32 ourselves
920 * (the OFW one isn't very good)
923 /* Initialize more IODA stuff */
924 phb->ioda.total_pe = 128;
926 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
927 /* OFW Has already off top 64k of M32 space (MSI space) */
928 phb->ioda.m32_size += 0x10000;
930 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
931 phb->ioda.m32_pci_base = hose->mem_resources[0].start -
932 hose->pci_mem_offset;
933 phb->ioda.io_size = hose->pci_io_size;
934 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
935 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
937 /* Allocate aux data & arrays */
938 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
940 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
942 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
944 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
945 aux = alloc_bootmem(size);
946 memset(aux, 0, size);
947 phb->ioda.pe_alloc = aux;
948 phb->ioda.m32_segmap = aux + m32map_off;
949 phb->ioda.io_segmap = aux + iomap_off;
950 phb->ioda.pe_array = aux + pemap_off;
951 set_bit(0, phb->ioda.pe_alloc);
953 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
954 INIT_LIST_HEAD(&phb->ioda.pe_list);
956 /* Calculate how many 32-bit TCE segments we have */
957 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
959 /* Clear unusable m64 */
960 hose->mem_resources[1].flags = 0;
961 hose->mem_resources[1].start = 0;
962 hose->mem_resources[1].end = 0;
963 hose->mem_resources[2].flags = 0;
964 hose->mem_resources[2].start = 0;
965 hose->mem_resources[2].end = 0;
968 rc = opal_pci_set_phb_mem_window(opal->phb_id,
971 starting_real_address,
972 starting_pci_address,
976 pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
978 phb->ioda.m32_size, phb->ioda.m32_segsize,
979 phb->ioda.io_size, phb->ioda.io_segsize);
982 pr_devel(" BUID = 0x%016llx\n", in_be64(phb->regs + 0x100));
983 pr_devel(" PHB2_CR = 0x%016llx\n", in_be64(phb->regs + 0x160));
984 pr_devel(" IO_BAR = 0x%016llx\n", in_be64(phb->regs + 0x170));
985 pr_devel(" IO_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x178));
986 pr_devel(" IO_SAR = 0x%016llx\n", in_be64(phb->regs + 0x180));
987 pr_devel(" M32_BAR = 0x%016llx\n", in_be64(phb->regs + 0x190));
988 pr_devel(" M32_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x198));
989 pr_devel(" M32_SAR = 0x%016llx\n", in_be64(phb->regs + 0x1a0));
991 phb->hose->ops = &pnv_pci_ops;
993 /* Setup RID -> PE mapping function */
994 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
997 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
999 /* Setup MSI support */
1000 pnv_pci_init_ioda_msis(phb);
1003 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
1004 * to let the PCI core do resource assignment. It's supposed
1005 * that the PCI core will do correct I/O and MMIO alignment
1006 * for the P2P bridge bars so that each PCI bus (excluding
1007 * the child P2P bridges) can form individual PE.
1009 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
1010 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
1011 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
1012 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
1014 /* Reset IODA tables to a clean state */
1015 rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
1017 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
1018 opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
1021 void __init pnv_pci_init_ioda_hub(struct device_node *np)
1023 struct device_node *phbn;
1027 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
1029 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
1031 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
1034 hub_id = be64_to_cpup(prop64);
1035 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
1037 /* Count child PHBs */
1038 for_each_child_of_node(np, phbn) {
1039 /* Look for IODA1 PHBs */
1040 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
1041 pnv_pci_init_ioda1_phb(phbn);