2 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
6 * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
7 * Copyright (C) 2006 Olof Johansson <olof@lixom.net>
9 * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
31 #include <linux/memblock.h>
32 #include <linux/spinlock.h>
33 #include <linux/string.h>
34 #include <linux/pci.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/crash_dump.h>
37 #include <linux/memory.h>
39 #include <linux/iommu.h>
43 #include <asm/iommu.h>
44 #include <asm/pci-bridge.h>
45 #include <asm/machdep.h>
46 #include <asm/firmware.h>
48 #include <asm/ppc-pci.h>
50 #include <asm/mmzone.h>
51 #include <asm/plpar_wrappers.h>
55 static struct iommu_table_group *iommu_pseries_alloc_group(int node)
57 struct iommu_table_group *table_group = NULL;
58 struct iommu_table *tbl = NULL;
60 table_group = kzalloc_node(sizeof(struct iommu_table_group), GFP_KERNEL,
65 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, node);
69 tbl->it_table_group = table_group;
70 table_group->tables[0] = tbl;
81 static void iommu_pseries_free_group(struct iommu_table_group *table_group,
82 const char *node_name)
84 struct iommu_table *tbl;
89 #ifdef CONFIG_IOMMU_API
90 if (table_group->group) {
91 iommu_group_put(table_group->group);
92 BUG_ON(table_group->group);
96 tbl = table_group->tables[0];
97 iommu_free_table(tbl, node_name);
102 static void tce_invalidate_pSeries_sw(struct iommu_table *tbl,
103 __be64 *startp, __be64 *endp)
105 u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
106 unsigned long start, end, inc;
108 start = __pa(startp);
110 inc = L1_CACHE_BYTES; /* invalidate a cacheline of TCEs at a time */
112 /* If this is non-zero, change the format. We shift the
113 * address and or in the magic from the device tree. */
118 start |= tbl->it_busno;
119 end |= tbl->it_busno;
122 end |= inc - 1; /* round up end to be different than start */
124 mb(); /* Make sure TCEs in memory are written */
125 while (start <= end) {
126 out_be64(invalidate, start);
131 static int tce_build_pSeries(struct iommu_table *tbl, long index,
132 long npages, unsigned long uaddr,
133 enum dma_data_direction direction,
134 struct dma_attrs *attrs)
140 proto_tce = TCE_PCI_READ; // Read allowed
142 if (direction != DMA_TO_DEVICE)
143 proto_tce |= TCE_PCI_WRITE;
145 tces = tcep = ((__be64 *)tbl->it_base) + index;
148 /* can't move this out since we might cross MEMBLOCK boundary */
149 rpn = __pa(uaddr) >> TCE_SHIFT;
150 *tcep = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
152 uaddr += TCE_PAGE_SIZE;
156 if (tbl->it_type & TCE_PCI_SWINV_CREATE)
157 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
162 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
166 tces = tcep = ((__be64 *)tbl->it_base) + index;
171 if (tbl->it_type & TCE_PCI_SWINV_FREE)
172 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
175 static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
179 tcep = ((__be64 *)tbl->it_base) + index;
181 return be64_to_cpu(*tcep);
184 static void tce_free_pSeriesLP(struct iommu_table*, long, long);
185 static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
187 static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
188 long npages, unsigned long uaddr,
189 enum dma_data_direction direction,
190 struct dma_attrs *attrs)
196 long tcenum_start = tcenum, npages_start = npages;
198 rpn = __pa(uaddr) >> TCE_SHIFT;
199 proto_tce = TCE_PCI_READ;
200 if (direction != DMA_TO_DEVICE)
201 proto_tce |= TCE_PCI_WRITE;
204 tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
205 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
207 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
209 tce_free_pSeriesLP(tbl, tcenum_start,
210 (npages_start - (npages + 1)));
214 if (rc && printk_ratelimit()) {
215 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
216 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
217 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
218 printk("\ttce val = 0x%llx\n", tce );
228 static DEFINE_PER_CPU(__be64 *, tce_page);
230 static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
231 long npages, unsigned long uaddr,
232 enum dma_data_direction direction,
233 struct dma_attrs *attrs)
240 long tcenum_start = tcenum, npages_start = npages;
244 if ((npages == 1) || !firmware_has_feature(FW_FEATURE_MULTITCE)) {
245 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
249 local_irq_save(flags); /* to protect tcep and the page behind it */
251 tcep = __this_cpu_read(tce_page);
253 /* This is safe to do since interrupts are off when we're called
254 * from iommu_alloc{,_sg}()
257 tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
258 /* If allocation fails, fall back to the loop implementation */
260 local_irq_restore(flags);
261 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
264 __this_cpu_write(tce_page, tcep);
267 rpn = __pa(uaddr) >> TCE_SHIFT;
268 proto_tce = TCE_PCI_READ;
269 if (direction != DMA_TO_DEVICE)
270 proto_tce |= TCE_PCI_WRITE;
272 /* We can map max one pageful of TCEs at a time */
275 * Set up the page with TCE data, looping through and setting
278 limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
280 for (l = 0; l < limit; l++) {
281 tcep[l] = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
285 rc = plpar_tce_put_indirect((u64)tbl->it_index,
292 } while (npages > 0 && !rc);
294 local_irq_restore(flags);
296 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
298 tce_freemulti_pSeriesLP(tbl, tcenum_start,
299 (npages_start - (npages + limit)));
303 if (rc && printk_ratelimit()) {
304 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
305 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
306 printk("\tnpages = 0x%llx\n", (u64)npages);
307 printk("\ttce[0] val = 0x%llx\n", tcep[0]);
313 static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
318 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
320 if (rc && printk_ratelimit()) {
321 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
322 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
323 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
332 static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
336 if (!firmware_has_feature(FW_FEATURE_MULTITCE))
337 return tce_free_pSeriesLP(tbl, tcenum, npages);
339 rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
341 if (rc && printk_ratelimit()) {
342 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
343 printk("\trc = %lld\n", rc);
344 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
345 printk("\tnpages = 0x%llx\n", (u64)npages);
350 static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
353 unsigned long tce_ret;
355 rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
357 if (rc && printk_ratelimit()) {
358 printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
359 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
360 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
367 /* this is compatible with cells for the device tree property */
368 struct dynamic_dma_window_prop {
369 __be32 liobn; /* tce table number */
370 __be64 dma_base; /* address hi,lo */
371 __be32 tce_shift; /* ilog2(tce_page_size) */
372 __be32 window_shift; /* ilog2(tce_window_size) */
375 struct direct_window {
376 struct device_node *device;
377 const struct dynamic_dma_window_prop *prop;
378 struct list_head list;
381 /* Dynamic DMA Window support */
382 struct ddw_query_response {
383 u32 windows_available;
384 u32 largest_available_block;
386 u32 migration_capable;
389 struct ddw_create_response {
395 static LIST_HEAD(direct_window_list);
396 /* prevents races between memory on/offline and window creation */
397 static DEFINE_SPINLOCK(direct_window_list_lock);
398 /* protects initializing window twice for same device */
399 static DEFINE_MUTEX(direct_window_init_mutex);
400 #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
402 static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,
403 unsigned long num_pfn, const void *arg)
405 const struct dynamic_dma_window_prop *maprange = arg;
407 u64 tce_size, num_tce, dma_offset, next;
411 tce_shift = be32_to_cpu(maprange->tce_shift);
412 tce_size = 1ULL << tce_shift;
413 next = start_pfn << PAGE_SHIFT;
414 num_tce = num_pfn << PAGE_SHIFT;
416 /* round back to the beginning of the tce page size */
417 num_tce += next & (tce_size - 1);
418 next &= ~(tce_size - 1);
420 /* covert to number of tces */
421 num_tce |= tce_size - 1;
422 num_tce >>= tce_shift;
426 * Set up the page with TCE data, looping through and setting
429 limit = min_t(long, num_tce, 512);
430 dma_offset = next + be64_to_cpu(maprange->dma_base);
432 rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn),
435 next += limit * tce_size;
437 } while (num_tce > 0 && !rc);
442 static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
443 unsigned long num_pfn, const void *arg)
445 const struct dynamic_dma_window_prop *maprange = arg;
446 u64 tce_size, num_tce, dma_offset, next, proto_tce, liobn;
452 local_irq_disable(); /* to protect tcep and the page behind it */
453 tcep = __this_cpu_read(tce_page);
456 tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
461 __this_cpu_write(tce_page, tcep);
464 proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
466 liobn = (u64)be32_to_cpu(maprange->liobn);
467 tce_shift = be32_to_cpu(maprange->tce_shift);
468 tce_size = 1ULL << tce_shift;
469 next = start_pfn << PAGE_SHIFT;
470 num_tce = num_pfn << PAGE_SHIFT;
472 /* round back to the beginning of the tce page size */
473 num_tce += next & (tce_size - 1);
474 next &= ~(tce_size - 1);
476 /* covert to number of tces */
477 num_tce |= tce_size - 1;
478 num_tce >>= tce_shift;
480 /* We can map max one pageful of TCEs at a time */
483 * Set up the page with TCE data, looping through and setting
486 limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
487 dma_offset = next + be64_to_cpu(maprange->dma_base);
489 for (l = 0; l < limit; l++) {
490 tcep[l] = cpu_to_be64(proto_tce | next);
494 rc = plpar_tce_put_indirect(liobn,
500 } while (num_tce > 0 && !rc);
502 /* error cleanup: caller will clear whole range */
508 static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
509 unsigned long num_pfn, void *arg)
511 return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
515 static void iommu_table_setparms(struct pci_controller *phb,
516 struct device_node *dn,
517 struct iommu_table *tbl)
519 struct device_node *node;
520 const unsigned long *basep, *sw_inval;
525 basep = of_get_property(node, "linux,tce-base", NULL);
526 sizep = of_get_property(node, "linux,tce-size", NULL);
527 if (basep == NULL || sizep == NULL) {
528 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
529 "missing tce entries !\n", dn->full_name);
533 tbl->it_base = (unsigned long)__va(*basep);
535 if (!is_kdump_kernel())
536 memset((void *)tbl->it_base, 0, *sizep);
538 tbl->it_busno = phb->bus->number;
539 tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
541 /* Units of tce entries */
542 tbl->it_offset = phb->dma_window_base_cur >> tbl->it_page_shift;
544 /* Test if we are going over 2GB of DMA space */
545 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
546 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
547 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
550 phb->dma_window_base_cur += phb->dma_window_size;
552 /* Set the tce table size - measured in entries */
553 tbl->it_size = phb->dma_window_size >> tbl->it_page_shift;
556 tbl->it_blocksize = 16;
557 tbl->it_type = TCE_PCI;
559 sw_inval = of_get_property(node, "linux,tce-sw-invalidate-info", NULL);
562 * This property contains information on how to
563 * invalidate the TCE entry. The first property is
564 * the base MMIO address used to invalidate entries.
565 * The second property tells us the format of the TCE
566 * invalidate (whether it needs to be shifted) and
567 * some magic routing info to add to our invalidate
570 tbl->it_index = (unsigned long) ioremap(sw_inval[0], 8);
571 tbl->it_busno = sw_inval[1]; /* overload this with magic */
572 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
577 * iommu_table_setparms_lpar
579 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
581 static void iommu_table_setparms_lpar(struct pci_controller *phb,
582 struct device_node *dn,
583 struct iommu_table *tbl,
584 const __be32 *dma_window)
586 unsigned long offset, size;
588 of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
590 tbl->it_busno = phb->bus->number;
591 tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
593 tbl->it_blocksize = 16;
594 tbl->it_type = TCE_PCI;
595 tbl->it_offset = offset >> tbl->it_page_shift;
596 tbl->it_size = size >> tbl->it_page_shift;
599 struct iommu_table_ops iommu_table_pseries_ops = {
600 .set = tce_build_pSeries,
601 .clear = tce_free_pSeries,
602 .get = tce_get_pseries
605 static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
607 struct device_node *dn;
608 struct iommu_table *tbl;
609 struct device_node *isa_dn, *isa_dn_orig;
610 struct device_node *tmp;
614 dn = pci_bus_to_OF_node(bus);
616 pr_debug("pci_dma_bus_setup_pSeries: setting up bus %s\n", dn->full_name);
619 /* This is not a root bus, any setup will be done for the
620 * device-side of the bridge in iommu_dev_setup_pSeries().
626 /* Check if the ISA bus on the system is under
629 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
631 while (isa_dn && isa_dn != dn)
632 isa_dn = isa_dn->parent;
634 of_node_put(isa_dn_orig);
636 /* Count number of direct PCI children of the PHB. */
637 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
640 pr_debug("Children: %d\n", children);
642 /* Calculate amount of DMA window per slot. Each window must be
643 * a power of two (due to pci_alloc_consistent requirements).
645 * Keep 256MB aside for PHBs with ISA.
649 /* No ISA/IDE - just set window size and return */
650 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
652 while (pci->phb->dma_window_size * children > 0x80000000ul)
653 pci->phb->dma_window_size >>= 1;
654 pr_debug("No ISA/IDE, window size is 0x%llx\n",
655 pci->phb->dma_window_size);
656 pci->phb->dma_window_base_cur = 0;
661 /* If we have ISA, then we probably have an IDE
662 * controller too. Allocate a 128MB table but
663 * skip the first 128MB to avoid stepping on ISA
666 pci->phb->dma_window_size = 0x8000000ul;
667 pci->phb->dma_window_base_cur = 0x8000000ul;
669 pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
670 tbl = pci->table_group->tables[0];
672 iommu_table_setparms(pci->phb, dn, tbl);
673 tbl->it_ops = &iommu_table_pseries_ops;
674 iommu_init_table(tbl, pci->phb->node);
675 iommu_register_group(pci->table_group, pci_domain_nr(bus), 0);
677 /* Divide the rest (1.75GB) among the children */
678 pci->phb->dma_window_size = 0x80000000ul;
679 while (pci->phb->dma_window_size * children > 0x70000000ul)
680 pci->phb->dma_window_size >>= 1;
682 pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
685 struct iommu_table_ops iommu_table_lpar_multi_ops = {
686 .set = tce_buildmulti_pSeriesLP,
687 .clear = tce_freemulti_pSeriesLP,
688 .get = tce_get_pSeriesLP
691 static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
693 struct iommu_table *tbl;
694 struct device_node *dn, *pdn;
696 const __be32 *dma_window = NULL;
698 dn = pci_bus_to_OF_node(bus);
700 pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
703 /* Find nearest ibm,dma-window, walking up the device tree */
704 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
705 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
706 if (dma_window != NULL)
710 if (dma_window == NULL) {
711 pr_debug(" no ibm,dma-window property !\n");
717 pr_debug(" parent is %s, iommu_table: 0x%p\n",
718 pdn->full_name, ppci->table_group);
720 if (!ppci->table_group) {
721 ppci->table_group = iommu_pseries_alloc_group(ppci->phb->node);
722 tbl = ppci->table_group->tables[0];
723 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
724 tbl->it_ops = &iommu_table_lpar_multi_ops;
725 iommu_init_table(tbl, ppci->phb->node);
726 iommu_register_group(ppci->table_group,
727 pci_domain_nr(bus), 0);
728 pr_debug(" created table: %p\n", ppci->table_group);
733 static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
735 struct device_node *dn;
736 struct iommu_table *tbl;
738 pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
740 dn = dev->dev.of_node;
742 /* If we're the direct child of a root bus, then we need to allocate
743 * an iommu table ourselves. The bus setup code should have setup
744 * the window sizes already.
746 if (!dev->bus->self) {
747 struct pci_controller *phb = PCI_DN(dn)->phb;
749 pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
750 PCI_DN(dn)->table_group = iommu_pseries_alloc_group(phb->node);
751 tbl = PCI_DN(dn)->table_group->tables[0];
752 iommu_table_setparms(phb, dn, tbl);
753 tbl->it_ops = &iommu_table_pseries_ops;
754 iommu_init_table(tbl, phb->node);
755 iommu_register_group(PCI_DN(dn)->table_group,
756 pci_domain_nr(phb->bus), 0);
757 set_iommu_table_base(&dev->dev, tbl);
758 iommu_add_device(&dev->dev);
762 /* If this device is further down the bus tree, search upwards until
763 * an already allocated iommu table is found and use that.
766 while (dn && PCI_DN(dn) && PCI_DN(dn)->table_group == NULL)
769 if (dn && PCI_DN(dn)) {
770 set_iommu_table_base(&dev->dev,
771 PCI_DN(dn)->table_group->tables[0]);
772 iommu_add_device(&dev->dev);
774 printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
778 static int __read_mostly disable_ddw;
780 static int __init disable_ddw_setup(char *str)
783 printk(KERN_INFO "ppc iommu: disabling ddw.\n");
788 early_param("disable_ddw", disable_ddw_setup);
790 static void remove_ddw(struct device_node *np, bool remove_prop)
792 struct dynamic_dma_window_prop *dwp;
793 struct property *win64;
798 ret = of_property_read_u32_array(np, "ibm,ddw-applicable",
801 win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
805 if (ret || win64->length < sizeof(*dwp))
809 liobn = (u64)be32_to_cpu(dwp->liobn);
811 /* clear the whole window, note the arg is in kernel pages */
812 ret = tce_clearrange_multi_pSeriesLP(0,
813 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
815 pr_warning("%s failed to clear tces in window.\n",
818 pr_debug("%s successfully cleared tces in window.\n",
821 ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
823 pr_warning("%s: failed to remove direct window: rtas returned "
824 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
825 np->full_name, ret, ddw_avail[2], liobn);
827 pr_debug("%s: successfully removed direct window: rtas returned "
828 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
829 np->full_name, ret, ddw_avail[2], liobn);
833 ret = of_remove_property(np, win64);
835 pr_warning("%s: failed to remove direct window property: %d\n",
839 static u64 find_existing_ddw(struct device_node *pdn)
841 struct direct_window *window;
842 const struct dynamic_dma_window_prop *direct64;
845 spin_lock(&direct_window_list_lock);
846 /* check if we already created a window and dupe that config if so */
847 list_for_each_entry(window, &direct_window_list, list) {
848 if (window->device == pdn) {
849 direct64 = window->prop;
850 dma_addr = be64_to_cpu(direct64->dma_base);
854 spin_unlock(&direct_window_list_lock);
859 static int find_existing_ddw_windows(void)
862 struct device_node *pdn;
863 struct direct_window *window;
864 const struct dynamic_dma_window_prop *direct64;
866 if (!firmware_has_feature(FW_FEATURE_LPAR))
869 for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
870 direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
874 window = kzalloc(sizeof(*window), GFP_KERNEL);
875 if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
877 remove_ddw(pdn, true);
881 window->device = pdn;
882 window->prop = direct64;
883 spin_lock(&direct_window_list_lock);
884 list_add(&window->list, &direct_window_list);
885 spin_unlock(&direct_window_list_lock);
890 machine_arch_initcall(pseries, find_existing_ddw_windows);
892 static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
893 struct ddw_query_response *query)
895 struct eeh_dev *edev;
901 * Get the config address and phb buid of the PE window.
902 * Rely on eeh to retrieve this for us.
903 * Retrieve them from the pci device, not the node with the
904 * dma-window property
906 edev = pci_dev_to_eeh_dev(dev);
907 cfg_addr = edev->config_addr;
908 if (edev->pe_config_addr)
909 cfg_addr = edev->pe_config_addr;
910 buid = edev->phb->buid;
912 ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
913 cfg_addr, BUID_HI(buid), BUID_LO(buid));
914 dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
915 " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
920 static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
921 struct ddw_create_response *create, int page_shift,
924 struct eeh_dev *edev;
930 * Get the config address and phb buid of the PE window.
931 * Rely on eeh to retrieve this for us.
932 * Retrieve them from the pci device, not the node with the
933 * dma-window property
935 edev = pci_dev_to_eeh_dev(dev);
936 cfg_addr = edev->config_addr;
937 if (edev->pe_config_addr)
938 cfg_addr = edev->pe_config_addr;
939 buid = edev->phb->buid;
942 /* extra outputs are LIOBN and dma-addr (hi, lo) */
943 ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create,
944 cfg_addr, BUID_HI(buid), BUID_LO(buid),
945 page_shift, window_shift);
946 } while (rtas_busy_delay(ret));
948 "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
949 "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
950 cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
951 window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
956 struct failed_ddw_pdn {
957 struct device_node *pdn;
958 struct list_head list;
961 static LIST_HEAD(failed_ddw_pdn_list);
964 * If the PE supports dynamic dma windows, and there is space for a table
965 * that can map all pages in a linear offset, then setup such a table,
966 * and record the dma-offset in the struct device.
968 * dev: the pci device we are checking
969 * pdn: the parent pe node with the ibm,dma_window property
970 * Future: also check if we can remap the base window for our base page size
972 * returns the dma offset for use by dma_set_mask
974 static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
977 struct ddw_query_response query;
978 struct ddw_create_response create;
980 u64 dma_addr, max_addr;
981 struct device_node *dn;
983 struct direct_window *window;
984 struct property *win64;
985 struct dynamic_dma_window_prop *ddwprop;
986 struct failed_ddw_pdn *fpdn;
988 mutex_lock(&direct_window_init_mutex);
990 dma_addr = find_existing_ddw(pdn);
995 * If we already went through this for a previous function of
996 * the same device and failed, we don't want to muck with the
997 * DMA window again, as it will race with in-flight operations
998 * and can lead to EEHs. The above mutex protects access to the
1001 list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) {
1002 if (!strcmp(fpdn->pdn->full_name, pdn->full_name))
1007 * the ibm,ddw-applicable property holds the tokens for:
1008 * ibm,query-pe-dma-window
1009 * ibm,create-pe-dma-window
1010 * ibm,remove-pe-dma-window
1011 * for the given node in that order.
1012 * the property is actually in the parent, not the PE
1014 ret = of_property_read_u32_array(pdn, "ibm,ddw-applicable",
1020 * Query if there is a second window of size to map the
1021 * whole partition. Query returns number of windows, largest
1022 * block assigned to PE (partition endpoint), and two bitmasks
1023 * of page sizes: supported and supported for migrate-dma.
1025 dn = pci_device_to_OF_node(dev);
1026 ret = query_ddw(dev, ddw_avail, &query);
1030 if (query.windows_available == 0) {
1032 * no additional windows are available for this device.
1033 * We might be able to reallocate the existing window,
1034 * trading in for a larger page size.
1036 dev_dbg(&dev->dev, "no free dynamic windows");
1039 if (query.page_size & 4) {
1040 page_shift = 24; /* 16MB */
1041 } else if (query.page_size & 2) {
1042 page_shift = 16; /* 64kB */
1043 } else if (query.page_size & 1) {
1044 page_shift = 12; /* 4kB */
1046 dev_dbg(&dev->dev, "no supported direct page size in mask %x",
1050 /* verify the window * number of ptes will map the partition */
1051 /* check largest block * page size > max memory hotplug addr */
1052 max_addr = memory_hotplug_max();
1053 if (query.largest_available_block < (max_addr >> page_shift)) {
1054 dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
1055 "%llu-sized pages\n", max_addr, query.largest_available_block,
1056 1ULL << page_shift);
1059 len = order_base_2(max_addr);
1060 win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
1063 "couldn't allocate property for 64bit dma window\n");
1066 win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
1067 win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
1068 win64->length = sizeof(*ddwprop);
1069 if (!win64->name || !win64->value) {
1071 "couldn't allocate property name and value\n");
1075 ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
1079 ddwprop->liobn = cpu_to_be32(create.liobn);
1080 ddwprop->dma_base = cpu_to_be64(((u64)create.addr_hi << 32) |
1082 ddwprop->tce_shift = cpu_to_be32(page_shift);
1083 ddwprop->window_shift = cpu_to_be32(len);
1085 dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
1086 create.liobn, dn->full_name);
1088 window = kzalloc(sizeof(*window), GFP_KERNEL);
1090 goto out_clear_window;
1092 ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
1093 win64->value, tce_setrange_multi_pSeriesLP_walk);
1095 dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
1096 dn->full_name, ret);
1097 goto out_free_window;
1100 ret = of_add_property(pdn, win64);
1102 dev_err(&dev->dev, "unable to add dma window property for %s: %d",
1103 pdn->full_name, ret);
1104 goto out_free_window;
1107 window->device = pdn;
1108 window->prop = ddwprop;
1109 spin_lock(&direct_window_list_lock);
1110 list_add(&window->list, &direct_window_list);
1111 spin_unlock(&direct_window_list_lock);
1113 dma_addr = be64_to_cpu(ddwprop->dma_base);
1120 remove_ddw(pdn, true);
1124 kfree(win64->value);
1129 fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
1133 list_add(&fpdn->list, &failed_ddw_pdn_list);
1136 mutex_unlock(&direct_window_init_mutex);
1140 static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
1142 struct device_node *pdn, *dn;
1143 struct iommu_table *tbl;
1144 const __be32 *dma_window = NULL;
1147 pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
1149 /* dev setup for LPAR is a little tricky, since the device tree might
1150 * contain the dma-window properties per-device and not necessarily
1151 * for the bus. So we need to search upwards in the tree until we
1152 * either hit a dma-window property, OR find a parent with a table
1153 * already allocated.
1155 dn = pci_device_to_OF_node(dev);
1156 pr_debug(" node is %s\n", dn->full_name);
1158 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1159 pdn = pdn->parent) {
1160 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1165 if (!pdn || !PCI_DN(pdn)) {
1166 printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
1167 "no DMA window found for pci dev=%s dn=%s\n",
1168 pci_name(dev), of_node_full_name(dn));
1171 pr_debug(" parent is %s\n", pdn->full_name);
1174 if (!pci->table_group) {
1175 pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
1176 tbl = pci->table_group->tables[0];
1177 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
1178 tbl->it_ops = &iommu_table_lpar_multi_ops;
1179 iommu_init_table(tbl, pci->phb->node);
1180 iommu_register_group(pci->table_group,
1181 pci_domain_nr(pci->phb->bus), 0);
1182 pr_debug(" created table: %p\n", pci->table_group);
1184 pr_debug(" found DMA window, table: %p\n", pci->table_group);
1187 set_iommu_table_base(&dev->dev, pci->table_group->tables[0]);
1188 iommu_add_device(&dev->dev);
1191 static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
1193 bool ddw_enabled = false;
1194 struct device_node *pdn, *dn;
1195 struct pci_dev *pdev;
1196 const __be32 *dma_window = NULL;
1202 if (!dev_is_pci(dev))
1205 pdev = to_pci_dev(dev);
1207 /* only attempt to use a new window if 64-bit DMA is requested */
1208 if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
1209 dn = pci_device_to_OF_node(pdev);
1210 dev_dbg(dev, "node is %s\n", dn->full_name);
1213 * the device tree might contain the dma-window properties
1214 * per-device and not necessarily for the bus. So we need to
1215 * search upwards in the tree until we either hit a dma-window
1216 * property, OR find a parent with a table already allocated.
1218 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1219 pdn = pdn->parent) {
1220 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1224 if (pdn && PCI_DN(pdn)) {
1225 dma_offset = enable_ddw(pdev, pdn);
1226 if (dma_offset != 0) {
1227 dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
1228 set_dma_offset(dev, dma_offset);
1229 set_dma_ops(dev, &dma_direct_ops);
1235 /* fall back on iommu ops, restore table pointer with ops */
1236 if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
1237 dev_info(dev, "Restoring 32-bit DMA via iommu\n");
1238 set_dma_ops(dev, &dma_iommu_ops);
1239 pci_dma_dev_setup_pSeriesLP(pdev);
1243 if (!dma_supported(dev, dma_mask))
1246 *dev->dma_mask = dma_mask;
1250 static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
1255 if (!disable_ddw && dev_is_pci(dev)) {
1256 struct pci_dev *pdev = to_pci_dev(dev);
1257 struct device_node *dn;
1259 dn = pci_device_to_OF_node(pdev);
1261 /* search upwards for ibm,dma-window */
1262 for (; dn && PCI_DN(dn) && !PCI_DN(dn)->table_group;
1264 if (of_get_property(dn, "ibm,dma-window", NULL))
1266 /* if there is a ibm,ddw-applicable property require 64 bits */
1267 if (dn && PCI_DN(dn) &&
1268 of_get_property(dn, "ibm,ddw-applicable", NULL))
1269 return DMA_BIT_MASK(64);
1272 return dma_iommu_ops.get_required_mask(dev);
1275 #else /* CONFIG_PCI */
1276 #define pci_dma_bus_setup_pSeries NULL
1277 #define pci_dma_dev_setup_pSeries NULL
1278 #define pci_dma_bus_setup_pSeriesLP NULL
1279 #define pci_dma_dev_setup_pSeriesLP NULL
1280 #define dma_set_mask_pSeriesLP NULL
1281 #define dma_get_required_mask_pSeriesLP NULL
1282 #endif /* !CONFIG_PCI */
1284 static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
1287 struct direct_window *window;
1288 struct memory_notify *arg = data;
1292 case MEM_GOING_ONLINE:
1293 spin_lock(&direct_window_list_lock);
1294 list_for_each_entry(window, &direct_window_list, list) {
1295 ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
1296 arg->nr_pages, window->prop);
1299 spin_unlock(&direct_window_list_lock);
1301 case MEM_CANCEL_ONLINE:
1303 spin_lock(&direct_window_list_lock);
1304 list_for_each_entry(window, &direct_window_list, list) {
1305 ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
1306 arg->nr_pages, window->prop);
1309 spin_unlock(&direct_window_list_lock);
1314 if (ret && action != MEM_CANCEL_ONLINE)
1320 static struct notifier_block iommu_mem_nb = {
1321 .notifier_call = iommu_mem_notifier,
1324 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
1326 int err = NOTIFY_OK;
1327 struct of_reconfig_data *rd = data;
1328 struct device_node *np = rd->dn;
1329 struct pci_dn *pci = PCI_DN(np);
1330 struct direct_window *window;
1333 case OF_RECONFIG_DETACH_NODE:
1335 * Removing the property will invoke the reconfig
1336 * notifier again, which causes dead-lock on the
1337 * read-write semaphore of the notifier chain. So
1338 * we have to remove the property when releasing
1341 remove_ddw(np, false);
1342 if (pci && pci->table_group)
1343 iommu_pseries_free_group(pci->table_group,
1346 spin_lock(&direct_window_list_lock);
1347 list_for_each_entry(window, &direct_window_list, list) {
1348 if (window->device == np) {
1349 list_del(&window->list);
1354 spin_unlock(&direct_window_list_lock);
1363 static struct notifier_block iommu_reconfig_nb = {
1364 .notifier_call = iommu_reconfig_notifier,
1367 /* These are called very early. */
1368 void iommu_init_early_pSeries(void)
1370 if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
1373 if (firmware_has_feature(FW_FEATURE_LPAR)) {
1374 pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
1375 pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
1376 ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
1377 ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
1379 pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeries;
1380 pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeries;
1384 of_reconfig_notifier_register(&iommu_reconfig_nb);
1385 register_memory_notifier(&iommu_mem_nb);
1387 set_pci_dma_ops(&dma_iommu_ops);
1390 static int __init disable_multitce(char *str)
1392 if (strcmp(str, "off") == 0 &&
1393 firmware_has_feature(FW_FEATURE_LPAR) &&
1394 firmware_has_feature(FW_FEATURE_MULTITCE)) {
1395 printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
1396 powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
1401 __setup("multitce=", disable_multitce);
1403 machine_subsys_initcall_sync(pseries, tce_iommu_bus_notifier_init);