2 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
6 * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
8 * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/config.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/string.h>
33 #include <linux/pci.h>
34 #include <linux/dma-mapping.h>
38 #include <asm/iommu.h>
39 #include <asm/pci-bridge.h>
40 #include <asm/machdep.h>
41 #include <asm/abs_addr.h>
42 #include <asm/pSeries_reconfig.h>
43 #include <asm/firmware.h>
45 #include <asm/ppc-pci.h>
48 #include "plpar_wrappers.h"
52 static void tce_build_pSeries(struct iommu_table *tbl, long index,
53 long npages, unsigned long uaddr,
54 enum dma_data_direction direction)
59 index <<= TCE_PAGE_FACTOR;
60 npages <<= TCE_PAGE_FACTOR;
63 t.te_rdwr = 1; // Read allowed
65 if (direction != DMA_TO_DEVICE)
68 tp = ((union tce_entry *)tbl->it_base) + index;
71 /* can't move this out since we might cross LMB boundary */
72 t.te_rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
74 tp->te_word = t.te_word;
76 uaddr += TCE_PAGE_SIZE;
82 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
87 npages <<= TCE_PAGE_FACTOR;
88 index <<= TCE_PAGE_FACTOR;
91 tp = ((union tce_entry *)tbl->it_base) + index;
94 tp->te_word = t.te_word;
101 static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
102 long npages, unsigned long uaddr,
103 enum dma_data_direction direction)
108 tcenum <<= TCE_PAGE_FACTOR;
109 npages <<= TCE_PAGE_FACTOR;
112 tce.te_rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
114 if (direction != DMA_TO_DEVICE)
118 rc = plpar_tce_put((u64)tbl->it_index,
122 if (rc && printk_ratelimit()) {
123 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
124 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
125 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
126 printk("\ttce val = 0x%lx\n", tce.te_word );
127 show_stack(current, (unsigned long *)__get_SP());
135 static DEFINE_PER_CPU(void *, tce_page) = NULL;
137 static void tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
138 long npages, unsigned long uaddr,
139 enum dma_data_direction direction)
142 union tce_entry tce, *tcep;
145 if (TCE_PAGE_FACTOR == 0 && npages == 1)
146 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
149 tcep = __get_cpu_var(tce_page);
151 /* This is safe to do since interrupts are off when we're called
152 * from iommu_alloc{,_sg}()
155 tcep = (void *)__get_free_page(GFP_ATOMIC);
156 /* If allocation fails, fall back to the loop implementation */
158 return tce_build_pSeriesLP(tbl, tcenum, npages,
160 __get_cpu_var(tce_page) = tcep;
163 tcenum <<= TCE_PAGE_FACTOR;
164 npages <<= TCE_PAGE_FACTOR;
167 tce.te_rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
169 if (direction != DMA_TO_DEVICE)
172 /* We can map max one pageful of TCEs at a time */
175 * Set up the page with TCE data, looping through and setting
178 limit = min_t(long, npages, 4096/sizeof(union tce_entry));
180 for (l = 0; l < limit; l++) {
185 rc = plpar_tce_put_indirect((u64)tbl->it_index,
187 (u64)virt_to_abs(tcep),
192 } while (npages > 0 && !rc);
194 if (rc && printk_ratelimit()) {
195 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
196 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
197 printk("\tnpages = 0x%lx\n", (u64)npages);
198 printk("\ttce[0] val = 0x%lx\n", tcep[0].te_word);
199 show_stack(current, (unsigned long *)__get_SP());
203 static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
208 tcenum <<= TCE_PAGE_FACTOR;
209 npages <<= TCE_PAGE_FACTOR;
214 rc = plpar_tce_put((u64)tbl->it_index,
218 if (rc && printk_ratelimit()) {
219 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
220 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
221 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
222 printk("\ttce val = 0x%lx\n", tce.te_word );
223 show_stack(current, (unsigned long *)__get_SP());
231 static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
236 tcenum <<= TCE_PAGE_FACTOR;
237 npages <<= TCE_PAGE_FACTOR;
241 rc = plpar_tce_stuff((u64)tbl->it_index,
246 if (rc && printk_ratelimit()) {
247 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
248 printk("\trc = %ld\n", rc);
249 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
250 printk("\tnpages = 0x%lx\n", (u64)npages);
251 printk("\ttce val = 0x%lx\n", tce.te_word );
252 show_stack(current, (unsigned long *)__get_SP());
256 static void iommu_table_setparms(struct pci_controller *phb,
257 struct device_node *dn,
258 struct iommu_table *tbl)
260 struct device_node *node;
261 unsigned long *basep;
264 node = (struct device_node *)phb->arch_data;
266 basep = (unsigned long *)get_property(node, "linux,tce-base", NULL);
267 sizep = (unsigned int *)get_property(node, "linux,tce-size", NULL);
268 if (basep == NULL || sizep == NULL) {
269 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
270 "missing tce entries !\n", dn->full_name);
274 tbl->it_base = (unsigned long)__va(*basep);
275 memset((void *)tbl->it_base, 0, *sizep);
277 tbl->it_busno = phb->bus->number;
279 /* Units of tce entries */
280 tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT;
282 /* Test if we are going over 2GB of DMA space */
283 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
284 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
285 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
288 phb->dma_window_base_cur += phb->dma_window_size;
290 /* Set the tce table size - measured in entries */
291 tbl->it_size = phb->dma_window_size >> PAGE_SHIFT;
294 tbl->it_blocksize = 16;
295 tbl->it_type = TCE_PCI;
299 * iommu_table_setparms_lpar
301 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
303 * ToDo: properly interpret the ibm,dma-window property. The definition is:
304 * logical-bus-number (1 word)
305 * phys-address (#address-cells words)
306 * size (#cell-size words)
308 * Currently we hard code these sizes (more or less).
310 static void iommu_table_setparms_lpar(struct pci_controller *phb,
311 struct device_node *dn,
312 struct iommu_table *tbl,
313 unsigned int *dma_window)
315 tbl->it_busno = PCI_DN(dn)->bussubno;
317 /* TODO: Parse field size properties properly. */
318 tbl->it_size = (((unsigned long)dma_window[4] << 32) |
319 (unsigned long)dma_window[5]) >> PAGE_SHIFT;
320 tbl->it_offset = (((unsigned long)dma_window[2] << 32) |
321 (unsigned long)dma_window[3]) >> PAGE_SHIFT;
323 tbl->it_index = dma_window[0];
324 tbl->it_blocksize = 16;
325 tbl->it_type = TCE_PCI;
328 static void iommu_bus_setup_pSeries(struct pci_bus *bus)
330 struct device_node *dn;
331 struct iommu_table *tbl;
332 struct device_node *isa_dn, *isa_dn_orig;
333 struct device_node *tmp;
337 DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self);
339 dn = pci_bus_to_OF_node(bus);
343 /* This is not a root bus, any setup will be done for the
344 * device-side of the bridge in iommu_dev_setup_pSeries().
349 /* Check if the ISA bus on the system is under
352 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
354 while (isa_dn && isa_dn != dn)
355 isa_dn = isa_dn->parent;
358 of_node_put(isa_dn_orig);
360 /* Count number of direct PCI children of the PHB.
361 * All PCI device nodes have class-code property, so it's
362 * an easy way to find them.
364 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
365 if (get_property(tmp, "class-code", NULL))
368 DBG("Children: %d\n", children);
370 /* Calculate amount of DMA window per slot. Each window must be
371 * a power of two (due to pci_alloc_consistent requirements).
373 * Keep 256MB aside for PHBs with ISA.
377 /* No ISA/IDE - just set window size and return */
378 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
380 while (pci->phb->dma_window_size * children > 0x80000000ul)
381 pci->phb->dma_window_size >>= 1;
382 DBG("No ISA/IDE, window size is 0x%lx\n",
383 pci->phb->dma_window_size);
384 pci->phb->dma_window_base_cur = 0;
389 /* If we have ISA, then we probably have an IDE
390 * controller too. Allocate a 128MB table but
391 * skip the first 128MB to avoid stepping on ISA
394 pci->phb->dma_window_size = 0x8000000ul;
395 pci->phb->dma_window_base_cur = 0x8000000ul;
397 tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
399 iommu_table_setparms(pci->phb, dn, tbl);
400 pci->iommu_table = iommu_init_table(tbl);
402 /* Divide the rest (1.75GB) among the children */
403 pci->phb->dma_window_size = 0x80000000ul;
404 while (pci->phb->dma_window_size * children > 0x70000000ul)
405 pci->phb->dma_window_size >>= 1;
407 DBG("ISA/IDE, window size is 0x%lx\n", pci->phb->dma_window_size);
412 static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus)
414 struct iommu_table *tbl;
415 struct device_node *dn, *pdn;
417 unsigned int *dma_window = NULL;
419 DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self);
421 dn = pci_bus_to_OF_node(bus);
423 /* Find nearest ibm,dma-window, walking up the device tree */
424 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
425 dma_window = (unsigned int *)get_property(pdn, "ibm,dma-window", NULL);
426 if (dma_window != NULL)
430 if (dma_window == NULL) {
431 DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name);
436 if (!ppci->iommu_table) {
437 /* Bussubno hasn't been copied yet.
438 * Do it now because iommu_table_setparms_lpar needs it.
441 ppci->bussubno = bus->number;
443 tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
446 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
448 ppci->iommu_table = iommu_init_table(tbl);
452 PCI_DN(dn)->iommu_table = ppci->iommu_table;
456 static void iommu_dev_setup_pSeries(struct pci_dev *dev)
458 struct device_node *dn, *mydn;
459 struct iommu_table *tbl;
461 DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, pci_name(dev));
463 mydn = dn = pci_device_to_OF_node(dev);
465 /* If we're the direct child of a root bus, then we need to allocate
466 * an iommu table ourselves. The bus setup code should have setup
467 * the window sizes already.
469 if (!dev->bus->self) {
470 DBG(" --> first child, no bridge. Allocating iommu table.\n");
471 tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
472 iommu_table_setparms(PCI_DN(dn)->phb, dn, tbl);
473 PCI_DN(mydn)->iommu_table = iommu_init_table(tbl);
478 /* If this device is further down the bus tree, search upwards until
479 * an already allocated iommu table is found and use that.
482 while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
485 if (dn && PCI_DN(dn)) {
486 PCI_DN(mydn)->iommu_table = PCI_DN(dn)->iommu_table;
488 DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, pci_name(dev));
492 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
495 struct device_node *np = node;
496 struct pci_dn *pci = PCI_DN(np);
499 case PSERIES_RECONFIG_REMOVE:
500 if (pci && pci->iommu_table &&
501 get_property(np, "ibm,dma-window", NULL))
502 iommu_free_table(np);
511 static struct notifier_block iommu_reconfig_nb = {
512 .notifier_call = iommu_reconfig_notifier,
515 static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev)
517 struct device_node *pdn, *dn;
518 struct iommu_table *tbl;
519 int *dma_window = NULL;
522 DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, pci_name(dev));
524 /* dev setup for LPAR is a little tricky, since the device tree might
525 * contain the dma-window properties per-device and not neccesarily
526 * for the bus. So we need to search upwards in the tree until we
527 * either hit a dma-window property, OR find a parent with a table
530 dn = pci_device_to_OF_node(dev);
532 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
534 dma_window = (unsigned int *)
535 get_property(pdn, "ibm,dma-window", NULL);
540 /* Check for parent == NULL so we don't try to setup the empty EADS
541 * slots on POWER4 machines.
543 if (dma_window == NULL || pdn->parent == NULL) {
544 DBG("No dma window for device, linking to parent\n");
545 PCI_DN(dn)->iommu_table = PCI_DN(pdn)->iommu_table;
548 DBG("Found DMA window, allocating table\n");
552 if (!pci->iommu_table) {
553 /* iommu_table_setparms_lpar needs bussubno. */
554 pci->bussubno = pci->phb->bus->number;
556 tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
559 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
561 pci->iommu_table = iommu_init_table(tbl);
565 PCI_DN(dn)->iommu_table = pci->iommu_table;
568 static void iommu_bus_setup_null(struct pci_bus *b) { }
569 static void iommu_dev_setup_null(struct pci_dev *d) { }
571 /* These are called very early. */
572 void iommu_init_early_pSeries(void)
574 if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) {
575 /* Direct I/O, IOMMU off */
576 ppc_md.iommu_dev_setup = iommu_dev_setup_null;
577 ppc_md.iommu_bus_setup = iommu_bus_setup_null;
578 pci_direct_iommu_init();
583 if (firmware_has_feature(FW_FEATURE_LPAR)) {
584 if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
585 ppc_md.tce_build = tce_buildmulti_pSeriesLP;
586 ppc_md.tce_free = tce_freemulti_pSeriesLP;
588 ppc_md.tce_build = tce_build_pSeriesLP;
589 ppc_md.tce_free = tce_free_pSeriesLP;
591 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP;
592 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP;
594 ppc_md.tce_build = tce_build_pSeries;
595 ppc_md.tce_free = tce_free_pSeries;
596 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries;
597 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries;
601 pSeries_reconfig_notifier_register(&iommu_reconfig_nb);