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>
33 #include <asm/abs_addr.h>
38 struct resource_wrap {
39 struct list_head link;
41 resource_size_t align;
42 struct pci_dev *dev; /* Set if it's a device */
43 struct pci_bus *bus; /* Set if it's a bridge */
46 static int __pe_printk(const char *level, const struct pnv_ioda_pe *pe,
47 struct va_format *vaf)
52 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
54 sprintf(pfix, "%04x:%02x ",
55 pci_domain_nr(pe->pbus), pe->pbus->number);
56 return printk("pci %s%s: [PE# %.3d] %pV", level, pfix, pe->pe_number, vaf);
59 #define define_pe_printk_level(func, kern_level) \
60 static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
62 struct va_format vaf; \
66 va_start(args, fmt); \
71 r = __pe_printk(kern_level, pe, &vaf); \
77 define_pe_printk_level(pe_err, KERN_ERR);
78 define_pe_printk_level(pe_warn, KERN_WARNING);
79 define_pe_printk_level(pe_info, KERN_INFO);
82 /* Calculate resource usage & alignment requirement of a single
83 * device. This will also assign all resources within the device
84 * for a given type starting at 0 for the biggest one and then
85 * assigning in decreasing order of size.
87 static void __devinit pnv_ioda_calc_dev(struct pci_dev *dev, unsigned int flags,
88 resource_size_t *size,
89 resource_size_t *align)
91 resource_size_t start;
95 pr_devel(" -> CDR %s\n", pci_name(dev));
99 /* Clear the resources out and mark them all unset */
100 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
101 r = &dev->resource[i];
102 if (!(r->flags & flags))
108 r->flags |= IORESOURCE_UNSET;
111 /* We currently keep all memory resources together, we
112 * will handle prefetch & 64-bit separately in the future
113 * but for now we stick everybody in M32
117 resource_size_t max_size = 0;
120 /* Find next biggest resource */
121 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
122 r = &dev->resource[i];
123 if (!(r->flags & IORESOURCE_UNSET) ||
126 if (resource_size(r) > max_size) {
127 max_size = resource_size(r);
133 r = &dev->resource[max_no];
134 if (max_size > *align)
139 r->end = r->start + max_size - 1;
140 r->flags &= ~IORESOURCE_UNSET;
141 pr_devel(" -> R%d %016llx..%016llx\n",
142 max_no, r->start, r->end);
144 pr_devel(" <- CDR %s size=%llx align=%llx\n",
145 pci_name(dev), *size, *align);
148 /* Allocate a resource "wrap" for a given device or bridge and
149 * insert it at the right position in the sorted list
151 static void __devinit pnv_ioda_add_wrap(struct list_head *list,
154 resource_size_t size,
155 resource_size_t align)
157 struct resource_wrap *w1, *w = kzalloc(sizeof(*w), GFP_KERNEL);
164 list_for_each_entry(w1, list, link) {
165 if (w1->align < align) {
166 list_add_tail(&w->link, &w1->link);
170 list_add_tail(&w->link, list);
173 /* Offset device resources of a given type */
174 static void __devinit pnv_ioda_offset_dev(struct pci_dev *dev,
176 resource_size_t offset)
181 pr_devel(" -> ODR %s [%x] +%016llx\n", pci_name(dev), flags, offset);
183 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
184 r = &dev->resource[i];
185 if (r->flags & flags) {
186 dev->resource[i].start += offset;
187 dev->resource[i].end += offset;
191 pr_devel(" <- ODR %s [%x] +%016llx\n", pci_name(dev), flags, offset);
194 /* Offset bus resources (& all children) of a given type */
195 static void __devinit pnv_ioda_offset_bus(struct pci_bus *bus,
197 resource_size_t offset)
201 struct pci_bus *cbus;
204 pr_devel(" -> OBR %s [%x] +%016llx\n",
205 bus->self ? pci_name(bus->self) : "root", flags, offset);
207 for (i = 0; i < 2; i++) {
208 r = bus->resource[i];
209 if (r && (r->flags & flags)) {
210 bus->resource[i]->start += offset;
211 bus->resource[i]->end += offset;
214 list_for_each_entry(dev, &bus->devices, bus_list)
215 pnv_ioda_offset_dev(dev, flags, offset);
216 list_for_each_entry(cbus, &bus->children, node)
217 pnv_ioda_offset_bus(cbus, flags, offset);
219 pr_devel(" <- OBR %s [%x]\n",
220 bus->self ? pci_name(bus->self) : "root", flags);
223 /* This is the guts of our IODA resource allocation. This is called
224 * recursively for each bus in the system. It calculates all the
225 * necessary size and requirements for children and assign them
226 * resources such that:
228 * - Each function fits in it's own contiguous set of IO/M32
231 * - All segments behind a P2P bridge are contiguous and obey
232 * alignment constraints of those bridges
234 static void __devinit pnv_ioda_calc_bus(struct pci_bus *bus, unsigned int flags,
235 resource_size_t *size,
236 resource_size_t *align)
238 struct pci_controller *hose = pci_bus_to_host(bus);
239 struct pnv_phb *phb = hose->private_data;
240 resource_size_t dev_size, dev_align, start;
241 resource_size_t min_align, min_balign;
242 struct pci_dev *cdev;
243 struct pci_bus *cbus;
244 struct list_head head;
245 struct resource_wrap *w;
250 pr_devel("-> CBR %s [%x]\n",
251 bus->self ? pci_name(bus->self) : "root", flags);
253 /* Calculate alignment requirements based on the type
254 * of resource we are working on
256 if (flags & IORESOURCE_IO) {
258 min_align = phb->ioda.io_segsize;
262 min_align = phb->ioda.m32_segsize;
263 min_balign = 0x100000;
266 /* Gather all our children resources ordered by alignment */
267 INIT_LIST_HEAD(&head);
270 list_for_each_entry(cbus, &bus->children, node) {
271 pnv_ioda_calc_bus(cbus, flags, &dev_size, &dev_align);
272 pnv_ioda_add_wrap(&head, cbus, NULL, dev_size, dev_align);
276 list_for_each_entry(cdev, &bus->devices, bus_list) {
277 pnv_ioda_calc_dev(cdev, flags, &dev_size, &dev_align);
278 /* Align them to segment size */
279 if (dev_align < min_align)
280 dev_align = min_align;
281 pnv_ioda_add_wrap(&head, NULL, cdev, dev_size, dev_align);
283 if (list_empty(&head))
286 /* Now we can do two things: assign offsets to them within that
287 * level and get our total alignment & size requirements. The
288 * assignment algorithm is going to be uber-trivial for now, we
289 * can try to be smarter later at filling out holes.
291 start = bus->self ? 0 : bus->resource[bres]->start;
293 /* Don't hand out IO 0 */
294 if ((flags & IORESOURCE_IO) && !bus->self)
297 while(!list_empty(&head)) {
298 w = list_first_entry(&head, struct resource_wrap, link);
302 start = ALIGN(start, w->align);
304 pnv_ioda_offset_dev(w->dev,flags,start);
306 pnv_ioda_offset_bus(w->bus,flags,start);
308 if (w->align > *align)
316 /* Align and setup bridge resources */
317 *align = max_t(resource_size_t, *align,
318 max_t(resource_size_t, min_align, min_balign));
320 max_t(resource_size_t, min_align, min_balign));
322 /* Only setup P2P's, not the PHB itself */
324 WARN_ON(bus->resource[bres] == NULL);
325 bus->resource[bres]->start = 0;
326 bus->resource[bres]->flags = (*size) ? flags : 0;
327 bus->resource[bres]->end = (*size) ? (*size - 1) : 0;
329 /* Clear prefetch bus resources for now */
330 bus->resource[2]->flags = 0;
333 pr_devel("<- CBR %s [%x] *size=%016llx *align=%016llx\n",
334 bus->self ? pci_name(bus->self) : "root", flags,*size,*align);
337 static struct pci_dn *pnv_ioda_get_pdn(struct pci_dev *dev)
339 struct device_node *np;
341 np = pci_device_to_OF_node(dev);
347 static void __devinit pnv_ioda_setup_pe_segments(struct pci_dev *dev)
349 struct pci_controller *hose = pci_bus_to_host(dev->bus);
350 struct pnv_phb *phb = hose->private_data;
351 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
354 struct resource io_res;
355 struct resource m32_res;
356 struct pci_bus_region region;
359 /* Anything not referenced in the device-tree gets PE#0 */
360 pe = pdn ? pdn->pe_number : 0;
362 /* Calculate the device min/max */
363 io_res.start = m32_res.start = (resource_size_t)-1;
364 io_res.end = m32_res.end = 0;
365 io_res.flags = IORESOURCE_IO;
366 m32_res.flags = IORESOURCE_MEM;
368 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
369 struct resource *r = NULL;
370 if (dev->resource[i].flags & IORESOURCE_IO)
372 if (dev->resource[i].flags & IORESOURCE_MEM)
376 if (dev->resource[i].start < r->start)
377 r->start = dev->resource[i].start;
378 if (dev->resource[i].end > r->end)
379 r->end = dev->resource[i].end;
382 /* Setup IO segments */
383 if (io_res.start < io_res.end) {
384 pcibios_resource_to_bus(dev, ®ion, &io_res);
386 i = pos / phb->ioda.io_segsize;
387 while(i < phb->ioda.total_pe && pos <= region.end) {
388 if (phb->ioda.io_segmap[i]) {
389 pr_err("%s: Trying to use IO seg #%d which is"
390 " already used by PE# %d\n",
392 phb->ioda.io_segmap[i]);
393 /* XXX DO SOMETHING TO DISABLE DEVICE ? */
396 phb->ioda.io_segmap[i] = pe;
397 rc = opal_pci_map_pe_mmio_window(phb->opal_id, pe,
400 if (rc != OPAL_SUCCESS) {
401 pr_err("%s: OPAL error %d setting up mapping"
403 pci_name(dev), rc, i);
404 /* XXX DO SOMETHING TO DISABLE DEVICE ? */
407 pos += phb->ioda.io_segsize;
412 /* Setup M32 segments */
413 if (m32_res.start < m32_res.end) {
414 pcibios_resource_to_bus(dev, ®ion, &m32_res);
416 i = pos / phb->ioda.m32_segsize;
417 while(i < phb->ioda.total_pe && pos <= region.end) {
418 if (phb->ioda.m32_segmap[i]) {
419 pr_err("%s: Trying to use M32 seg #%d which is"
420 " already used by PE# %d\n",
422 phb->ioda.m32_segmap[i]);
423 /* XXX DO SOMETHING TO DISABLE DEVICE ? */
426 phb->ioda.m32_segmap[i] = pe;
427 rc = opal_pci_map_pe_mmio_window(phb->opal_id, pe,
428 OPAL_M32_WINDOW_TYPE,
430 if (rc != OPAL_SUCCESS) {
431 pr_err("%s: OPAL error %d setting up mapping"
432 " for M32 seg# %d\n",
433 pci_name(dev), rc, i);
434 /* XXX DO SOMETHING TO DISABLE DEVICE ? */
437 pos += phb->ioda.m32_segsize;
443 /* Check if a resource still fits in the total IO or M32 range
446 static int __devinit pnv_ioda_resource_fit(struct pci_controller *hose,
449 struct resource *bounds;
451 if (r->flags & IORESOURCE_IO)
452 bounds = &hose->io_resource;
453 else if (r->flags & IORESOURCE_MEM)
454 bounds = &hose->mem_resources[0];
458 if (r->start >= bounds->start && r->end <= bounds->end)
464 static void __devinit pnv_ioda_update_resources(struct pci_bus *bus)
466 struct pci_controller *hose = pci_bus_to_host(bus);
467 struct pci_bus *cbus;
468 struct pci_dev *cdev;
471 /* We used to clear all device enables here. However it looks like
472 * clearing MEM enable causes Obsidian (IPR SCS) to go bonkers,
473 * and shoot fatal errors to the PHB which in turns fences itself
474 * and we can't recover from that ... yet. So for now, let's leave
475 * the enables as-is and hope for the best.
478 /* Check if bus resources fit in our IO or M32 range */
479 for (i = 0; bus->self && (i < 2); i++) {
480 struct resource *r = bus->resource[i];
481 if (r && !pnv_ioda_resource_fit(hose, r))
482 pr_err("%s: Bus %d resource %d disabled, no room\n",
483 pci_name(bus->self), bus->number, i);
486 /* Update self if it's not a PHB */
488 pci_setup_bridge(bus);
490 /* Update child devices */
491 list_for_each_entry(cdev, &bus->devices, bus_list) {
492 /* Check if resource fits, if not, disabled it */
493 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
494 struct resource *r = &cdev->resource[i];
495 if (!pnv_ioda_resource_fit(hose, r))
496 pr_err("%s: Resource %d disabled, no room\n",
500 /* Assign segments */
501 pnv_ioda_setup_pe_segments(cdev);
504 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
505 pci_update_resource(cdev, i);
508 /* Update child busses */
509 list_for_each_entry(cbus, &bus->children, node)
510 pnv_ioda_update_resources(cbus);
513 static int __devinit pnv_ioda_alloc_pe(struct pnv_phb *phb)
518 pe = find_next_zero_bit(phb->ioda.pe_alloc,
519 phb->ioda.total_pe, 0);
520 if (pe >= phb->ioda.total_pe)
521 return IODA_INVALID_PE;
522 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
524 phb->ioda.pe_array[pe].pe_number = pe;
528 static void __devinit pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
530 WARN_ON(phb->ioda.pe_array[pe].pdev);
532 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
533 clear_bit(pe, phb->ioda.pe_alloc);
536 /* Currently those 2 are only used when MSIs are enabled, this will change
537 * but in the meantime, we need to protect them to avoid warnings
539 #ifdef CONFIG_PCI_MSI
540 static struct pnv_ioda_pe * __devinit __pnv_ioda_get_one_pe(struct pci_dev *dev)
542 struct pci_controller *hose = pci_bus_to_host(dev->bus);
543 struct pnv_phb *phb = hose->private_data;
544 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
548 if (pdn->pe_number == IODA_INVALID_PE)
550 return &phb->ioda.pe_array[pdn->pe_number];
553 static struct pnv_ioda_pe * __devinit pnv_ioda_get_pe(struct pci_dev *dev)
555 struct pnv_ioda_pe *pe = __pnv_ioda_get_one_pe(dev);
557 while (!pe && dev->bus->self) {
558 dev = dev->bus->self;
559 pe = __pnv_ioda_get_one_pe(dev);
565 #endif /* CONFIG_PCI_MSI */
567 static int __devinit pnv_ioda_configure_pe(struct pnv_phb *phb,
568 struct pnv_ioda_pe *pe)
570 struct pci_dev *parent;
571 uint8_t bcomp, dcomp, fcomp;
572 long rc, rid_end, rid;
574 /* Bus validation ? */
578 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
579 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
580 parent = pe->pbus->self;
581 count = pe->pbus->subordinate - pe->pbus->secondary + 1;
583 case 1: bcomp = OpalPciBusAll; break;
584 case 2: bcomp = OpalPciBus7Bits; break;
585 case 4: bcomp = OpalPciBus6Bits; break;
586 case 8: bcomp = OpalPciBus5Bits; break;
587 case 16: bcomp = OpalPciBus4Bits; break;
588 case 32: bcomp = OpalPciBus3Bits; break;
590 pr_err("%s: Number of subordinate busses %d"
592 pci_name(pe->pbus->self), count);
593 /* Do an exact match only */
594 bcomp = OpalPciBusAll;
596 rid_end = pe->rid + (count << 8);
598 parent = pe->pdev->bus->self;
599 bcomp = OpalPciBusAll;
600 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
601 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
602 rid_end = pe->rid + 1;
605 /* Associate PE in PELT */
606 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
607 bcomp, dcomp, fcomp, OPAL_MAP_PE);
609 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
612 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
613 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
615 /* Add to all parents PELT-V */
617 struct pci_dn *pdn = pnv_ioda_get_pdn(parent);
618 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
619 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
620 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
621 /* XXX What to do in case of error ? */
623 parent = parent->bus->self;
625 /* Setup reverse map */
626 for (rid = pe->rid; rid < rid_end; rid++)
627 phb->ioda.pe_rmap[rid] = pe->pe_number;
629 /* Setup one MVTs on IODA1 */
630 if (phb->type == PNV_PHB_IODA1) {
631 pe->mve_number = pe->pe_number;
632 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
635 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
639 rc = opal_pci_set_mve_enable(phb->opal_id,
640 pe->mve_number, OPAL_ENABLE_MVE);
642 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
647 } else if (phb->type == PNV_PHB_IODA2)
653 static void __devinit pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
654 struct pnv_ioda_pe *pe)
656 struct pnv_ioda_pe *lpe;
658 list_for_each_entry(lpe, &phb->ioda.pe_list, link) {
659 if (lpe->dma_weight < pe->dma_weight) {
660 list_add_tail(&pe->link, &lpe->link);
664 list_add_tail(&pe->link, &phb->ioda.pe_list);
667 static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
669 /* This is quite simplistic. The "base" weight of a device
670 * is 10. 0 means no DMA is to be accounted for it.
673 /* If it's a bridge, no DMA */
674 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
677 /* Reduce the weight of slow USB controllers */
678 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
679 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
680 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
683 /* Increase the weight of RAID (includes Obsidian) */
684 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
691 static struct pnv_ioda_pe * __devinit pnv_ioda_setup_dev_PE(struct pci_dev *dev)
693 struct pci_controller *hose = pci_bus_to_host(dev->bus);
694 struct pnv_phb *phb = hose->private_data;
695 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
696 struct pnv_ioda_pe *pe;
700 pr_err("%s: Device tree node not associated properly\n",
704 if (pdn->pe_number != IODA_INVALID_PE)
707 /* PE#0 has been pre-set */
708 if (dev->bus->number == 0)
711 pe_num = pnv_ioda_alloc_pe(phb);
712 if (pe_num == IODA_INVALID_PE) {
713 pr_warning("%s: Not enough PE# available, disabling device\n",
718 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
719 * pointer in the PE data structure, both should be destroyed at the
720 * same time. However, this needs to be looked at more closely again
721 * once we actually start removing things (Hotplug, SR-IOV, ...)
723 * At some point we want to remove the PDN completely anyways
725 pe = &phb->ioda.pe_array[pe_num];
728 pdn->pe_number = pe_num;
733 pe->rid = dev->bus->number << 8 | pdn->devfn;
735 pe_info(pe, "Associated device to PE\n");
737 if (pnv_ioda_configure_pe(phb, pe)) {
738 /* XXX What do we do here ? */
740 pnv_ioda_free_pe(phb, pe_num);
741 pdn->pe_number = IODA_INVALID_PE;
747 /* Assign a DMA weight to the device */
748 pe->dma_weight = pnv_ioda_dma_weight(dev);
749 if (pe->dma_weight != 0) {
750 phb->ioda.dma_weight += pe->dma_weight;
751 phb->ioda.dma_pe_count++;
755 pnv_ioda_link_pe_by_weight(phb, pe);
760 static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
764 list_for_each_entry(dev, &bus->devices, bus_list) {
765 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
768 pr_warn("%s: No device node associated with device !\n",
774 pdn->pe_number = pe->pe_number;
775 pe->dma_weight += pnv_ioda_dma_weight(dev);
776 if (dev->subordinate)
777 pnv_ioda_setup_same_PE(dev->subordinate, pe);
781 static void __devinit pnv_ioda_setup_bus_PE(struct pci_dev *dev,
782 struct pnv_ioda_pe *ppe)
784 struct pci_controller *hose = pci_bus_to_host(dev->bus);
785 struct pnv_phb *phb = hose->private_data;
786 struct pci_bus *bus = dev->subordinate;
787 struct pnv_ioda_pe *pe;
791 pr_warning("%s: Bridge without a subordinate bus !\n",
795 pe_num = pnv_ioda_alloc_pe(phb);
796 if (pe_num == IODA_INVALID_PE) {
797 pr_warning("%s: Not enough PE# available, disabling bus\n",
802 pe = &phb->ioda.pe_array[pe_num];
808 pe->rid = bus->secondary << 8;
811 pe_info(pe, "Secondary busses %d..%d associated with PE\n",
812 bus->secondary, bus->subordinate);
814 if (pnv_ioda_configure_pe(phb, pe)) {
815 /* XXX What do we do here ? */
817 pnv_ioda_free_pe(phb, pe_num);
822 /* Associate it with all child devices */
823 pnv_ioda_setup_same_PE(bus, pe);
825 /* Account for one DMA PE if at least one DMA capable device exist
828 if (pe->dma_weight != 0) {
829 phb->ioda.dma_weight += pe->dma_weight;
830 phb->ioda.dma_pe_count++;
834 pnv_ioda_link_pe_by_weight(phb, pe);
837 static void __devinit pnv_ioda_setup_PEs(struct pci_bus *bus)
840 struct pnv_ioda_pe *pe;
842 list_for_each_entry(dev, &bus->devices, bus_list) {
843 pe = pnv_ioda_setup_dev_PE(dev);
846 /* Leaving the PCIe domain ... single PE# */
847 if (dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
848 pnv_ioda_setup_bus_PE(dev, pe);
849 else if (dev->subordinate)
850 pnv_ioda_setup_PEs(dev->subordinate);
854 static void __devinit pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb,
857 /* We delay DMA setup after we have assigned all PE# */
860 static void __devinit pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
865 list_for_each_entry(dev, &bus->devices, bus_list) {
866 set_iommu_table_base(&dev->dev, &pe->tce32_table);
867 if (dev->subordinate)
868 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
872 static void __devinit pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
873 struct pnv_ioda_pe *pe,
878 struct page *tce_mem = NULL;
879 const __be64 *swinvp;
880 struct iommu_table *tbl;
885 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
886 #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
888 /* XXX FIXME: Handle 64-bit only DMA devices */
889 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
890 /* XXX FIXME: Allocate multi-level tables on PHB3 */
892 /* We shouldn't already have a 32-bit DMA associated */
893 if (WARN_ON(pe->tce32_seg >= 0))
896 /* Grab a 32-bit TCE table */
897 pe->tce32_seg = base;
898 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
899 (base << 28), ((base + segs) << 28) - 1);
901 /* XXX Currently, we allocate one big contiguous table for the
902 * TCEs. We only really need one chunk per 256M of TCE space
903 * (ie per segment) but that's an optimization for later, it
904 * requires some added smarts with our get/put_tce implementation
906 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
907 get_order(TCE32_TABLE_SIZE * segs));
909 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
912 addr = page_address(tce_mem);
913 memset(addr, 0, TCE32_TABLE_SIZE * segs);
916 for (i = 0; i < segs; i++) {
917 rc = opal_pci_map_pe_dma_window(phb->opal_id,
920 __pa(addr) + TCE32_TABLE_SIZE * i,
921 TCE32_TABLE_SIZE, 0x1000);
923 pe_err(pe, " Failed to configure 32-bit TCE table,"
929 /* Setup linux iommu table */
930 tbl = &pe->tce32_table;
931 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
934 /* OPAL variant of P7IOC SW invalidated TCEs */
935 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
937 /* We need a couple more fields -- an address and a data
938 * to or. Since the bus is only printed out on table free
939 * errors, and on the first pass the data will be a relative
940 * bus number, print that out instead.
943 tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
944 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE
945 | TCE_PCI_SWINV_PAIR;
947 iommu_init_table(tbl, phb->hose->node);
950 set_iommu_table_base(&pe->pdev->dev, tbl);
952 pnv_ioda_setup_bus_dma(pe, pe->pbus);
956 /* XXX Failure: Try to fallback to 64-bit only ? */
957 if (pe->tce32_seg >= 0)
960 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
963 static void __devinit pnv_ioda_setup_dma(struct pnv_phb *phb)
965 struct pci_controller *hose = phb->hose;
966 unsigned int residual, remaining, segs, tw, base;
967 struct pnv_ioda_pe *pe;
969 /* If we have more PE# than segments available, hand out one
970 * per PE until we run out and let the rest fail. If not,
971 * then we assign at least one segment per PE, plus more based
972 * on the amount of devices under that PE
974 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
977 residual = phb->ioda.tce32_count -
978 phb->ioda.dma_pe_count;
980 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
981 hose->global_number, phb->ioda.tce32_count);
982 pr_info("PCI: %d PE# for a total weight of %d\n",
983 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
985 /* Walk our PE list and configure their DMA segments, hand them
986 * out one base segment plus any residual segments based on
989 remaining = phb->ioda.tce32_count;
990 tw = phb->ioda.dma_weight;
992 list_for_each_entry(pe, &phb->ioda.pe_list, link) {
996 pe_warn(pe, "No DMA32 resources available\n");
1001 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
1002 if (segs > remaining)
1005 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1006 pe->dma_weight, segs);
1007 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1013 #ifdef CONFIG_PCI_MSI
1014 static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
1015 unsigned int hwirq, unsigned int is_64,
1016 struct msi_msg *msg)
1018 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1019 unsigned int xive_num = hwirq - phb->msi_base;
1021 uint32_t addr32, data;
1024 /* No PE assigned ? bail out ... no MSI for you ! */
1028 /* Check if we have an MVE */
1029 if (pe->mve_number < 0)
1032 /* Assign XIVE to PE */
1033 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1035 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1036 pci_name(dev), rc, xive_num);
1041 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1044 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1048 msg->address_hi = addr64 >> 32;
1049 msg->address_lo = addr64 & 0xfffffffful;
1051 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1054 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1058 msg->address_hi = 0;
1059 msg->address_lo = addr32;
1063 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1064 " address=%x_%08x data=%x PE# %d\n",
1065 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
1066 msg->address_hi, msg->address_lo, data, pe->pe_number);
1071 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1073 unsigned int bmap_size;
1074 const __be32 *prop = of_get_property(phb->hose->dn,
1075 "ibm,opal-msi-ranges", NULL);
1078 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1083 phb->msi_base = be32_to_cpup(prop);
1084 phb->msi_count = be32_to_cpup(prop + 1);
1085 bmap_size = BITS_TO_LONGS(phb->msi_count) * sizeof(unsigned long);
1086 phb->msi_map = zalloc_maybe_bootmem(bmap_size, GFP_KERNEL);
1087 if (!phb->msi_map) {
1088 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1089 phb->hose->global_number);
1092 phb->msi_setup = pnv_pci_ioda_msi_setup;
1093 phb->msi32_support = 1;
1094 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
1095 phb->msi_count, phb->msi_base);
1098 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1099 #endif /* CONFIG_PCI_MSI */
1101 /* This is the starting point of our IODA specific resource
1102 * allocation process
1104 static void __devinit pnv_pci_ioda_fixup_phb(struct pci_controller *hose)
1106 resource_size_t size, align;
1107 struct pci_bus *child;
1109 /* Associate PEs per functions */
1110 pnv_ioda_setup_PEs(hose->bus);
1112 /* Calculate all resources */
1113 pnv_ioda_calc_bus(hose->bus, IORESOURCE_IO, &size, &align);
1114 pnv_ioda_calc_bus(hose->bus, IORESOURCE_MEM, &size, &align);
1116 /* Apply then to HW */
1117 pnv_ioda_update_resources(hose->bus);
1120 pnv_ioda_setup_dma(hose->private_data);
1122 /* Configure PCI Express settings */
1123 list_for_each_entry(child, &hose->bus->children, node) {
1124 struct pci_dev *self = child->self;
1127 pcie_bus_configure_settings(child, self->pcie_mpss);
1131 /* Prevent enabling devices for which we couldn't properly
1134 static int __devinit pnv_pci_enable_device_hook(struct pci_dev *dev)
1136 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
1138 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1143 static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1146 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1149 void __init pnv_pci_init_ioda1_phb(struct device_node *np)
1151 struct pci_controller *hose;
1152 static int primary = 1;
1153 struct pnv_phb *phb;
1154 unsigned long size, m32map_off, iomap_off, pemap_off;
1160 pr_info(" Initializing IODA OPAL PHB %s\n", np->full_name);
1162 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1164 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
1167 phb_id = be64_to_cpup(prop64);
1168 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
1170 phb = alloc_bootmem(sizeof(struct pnv_phb));
1172 memset(phb, 0, sizeof(struct pnv_phb));
1173 phb->hose = hose = pcibios_alloc_controller(np);
1175 if (!phb || !phb->hose) {
1176 pr_err("PCI: Failed to allocate PCI controller for %s\n",
1181 spin_lock_init(&phb->lock);
1182 /* XXX Use device-tree */
1183 hose->first_busno = 0;
1184 hose->last_busno = 0xff;
1185 hose->private_data = phb;
1186 phb->opal_id = phb_id;
1187 phb->type = PNV_PHB_IODA1;
1189 /* Detect specific models for error handling */
1190 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
1191 phb->model = PNV_PHB_MODEL_P7IOC;
1193 phb->model = PNV_PHB_MODEL_UNKNOWN;
1195 /* We parse "ranges" now since we need to deduce the register base
1198 pci_process_bridge_OF_ranges(phb->hose, np, primary);
1201 /* Magic formula from Milton */
1202 phb->regs = of_iomap(np, 0);
1203 if (phb->regs == NULL)
1204 pr_err(" Failed to map registers !\n");
1207 /* XXX This is hack-a-thon. This needs to be changed so that:
1208 * - we obtain stuff like PE# etc... from device-tree
1209 * - we properly re-allocate M32 ourselves
1210 * (the OFW one isn't very good)
1213 /* Initialize more IODA stuff */
1214 phb->ioda.total_pe = 128;
1216 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
1217 /* OFW Has already off top 64k of M32 space (MSI space) */
1218 phb->ioda.m32_size += 0x10000;
1220 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
1221 phb->ioda.m32_pci_base = hose->mem_resources[0].start -
1222 hose->pci_mem_offset;
1223 phb->ioda.io_size = hose->pci_io_size;
1224 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
1225 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
1227 /* Allocate aux data & arrays */
1228 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
1230 size += phb->ioda.total_pe;
1232 size += phb->ioda.total_pe;
1234 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
1235 aux = alloc_bootmem(size);
1236 memset(aux, 0, size);
1237 phb->ioda.pe_alloc = aux;
1238 phb->ioda.m32_segmap = aux + m32map_off;
1239 phb->ioda.io_segmap = aux + iomap_off;
1240 phb->ioda.pe_array = aux + pemap_off;
1241 set_bit(0, phb->ioda.pe_alloc);
1243 INIT_LIST_HEAD(&phb->ioda.pe_list);
1245 /* Calculate how many 32-bit TCE segments we have */
1246 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
1248 /* Clear unusable m64 */
1249 hose->mem_resources[1].flags = 0;
1250 hose->mem_resources[1].start = 0;
1251 hose->mem_resources[1].end = 0;
1252 hose->mem_resources[2].flags = 0;
1253 hose->mem_resources[2].start = 0;
1254 hose->mem_resources[2].end = 0;
1257 rc = opal_pci_set_phb_mem_window(opal->phb_id,
1260 starting_real_address,
1261 starting_pci_address,
1265 pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
1267 phb->ioda.m32_size, phb->ioda.m32_segsize,
1268 phb->ioda.io_size, phb->ioda.io_segsize);
1271 pr_devel(" BUID = 0x%016llx\n", in_be64(phb->regs + 0x100));
1272 pr_devel(" PHB2_CR = 0x%016llx\n", in_be64(phb->regs + 0x160));
1273 pr_devel(" IO_BAR = 0x%016llx\n", in_be64(phb->regs + 0x170));
1274 pr_devel(" IO_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x178));
1275 pr_devel(" IO_SAR = 0x%016llx\n", in_be64(phb->regs + 0x180));
1276 pr_devel(" M32_BAR = 0x%016llx\n", in_be64(phb->regs + 0x190));
1277 pr_devel(" M32_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x198));
1278 pr_devel(" M32_SAR = 0x%016llx\n", in_be64(phb->regs + 0x1a0));
1280 phb->hose->ops = &pnv_pci_ops;
1282 /* Setup RID -> PE mapping function */
1283 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
1286 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
1288 /* Setup MSI support */
1289 pnv_pci_init_ioda_msis(phb);
1291 /* We set both probe_only and PCI_REASSIGN_ALL_RSRC. This is an
1292 * odd combination which essentially means that we skip all resource
1293 * fixups and assignments in the generic code, and do it all
1297 ppc_md.pcibios_fixup_phb = pnv_pci_ioda_fixup_phb;
1298 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
1299 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
1301 /* Reset IODA tables to a clean state */
1302 rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
1304 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
1305 opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
1308 void __init pnv_pci_init_ioda_hub(struct device_node *np)
1310 struct device_node *phbn;
1314 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
1316 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
1318 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
1321 hub_id = be64_to_cpup(prop64);
1322 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
1324 /* Count child PHBs */
1325 for_each_child_of_node(np, phbn) {
1326 /* Look for IODA1 PHBs */
1327 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
1328 pnv_pci_init_ioda1_phb(phbn);