2 * arch/ppc64/kernel/pSeries_iommu.c
4 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
8 * Copyright (C) 2004 Olof Johansson <olof@austin.ibm.com>, IBM Corporation
10 * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/config.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/string.h>
35 #include <linux/pci.h>
36 #include <linux/dma-mapping.h>
40 #include <asm/ppcdebug.h>
41 #include <asm/iommu.h>
42 #include <asm/pci-bridge.h>
43 #include <asm/machdep.h>
44 #include <asm/abs_addr.h>
45 #include <asm/pSeries_reconfig.h>
46 #include <asm/systemcfg.h>
47 #include <asm/firmware.h>
49 #include <asm/ppc-pci.h>
51 #include "plpar_wrappers.h"
55 extern int is_python(struct device_node *);
57 static void tce_build_pSeries(struct iommu_table *tbl, long index,
58 long npages, unsigned long uaddr,
59 enum dma_data_direction direction)
64 index <<= TCE_PAGE_FACTOR;
65 npages <<= TCE_PAGE_FACTOR;
68 t.te_rdwr = 1; // Read allowed
70 if (direction != DMA_TO_DEVICE)
73 tp = ((union tce_entry *)tbl->it_base) + index;
76 /* can't move this out since we might cross LMB boundary */
77 t.te_rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
79 tp->te_word = t.te_word;
81 uaddr += TCE_PAGE_SIZE;
87 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
92 npages <<= TCE_PAGE_FACTOR;
93 index <<= TCE_PAGE_FACTOR;
96 tp = ((union tce_entry *)tbl->it_base) + index;
99 tp->te_word = t.te_word;
106 static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
107 long npages, unsigned long uaddr,
108 enum dma_data_direction direction)
114 tce.te_rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
116 if (direction != DMA_TO_DEVICE)
120 rc = plpar_tce_put((u64)tbl->it_index,
124 if (rc && printk_ratelimit()) {
125 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
126 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
127 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
128 printk("\ttce val = 0x%lx\n", tce.te_word );
129 show_stack(current, (unsigned long *)__get_SP());
137 static DEFINE_PER_CPU(void *, tce_page) = NULL;
139 static void tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
140 long npages, unsigned long uaddr,
141 enum dma_data_direction direction)
144 union tce_entry tce, *tcep;
147 tcenum <<= TCE_PAGE_FACTOR;
148 npages <<= TCE_PAGE_FACTOR;
151 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
154 tcep = __get_cpu_var(tce_page);
156 /* This is safe to do since interrupts are off when we're called
157 * from iommu_alloc{,_sg}()
160 tcep = (void *)__get_free_page(GFP_ATOMIC);
161 /* If allocation fails, fall back to the loop implementation */
163 return tce_build_pSeriesLP(tbl, tcenum, npages,
165 __get_cpu_var(tce_page) = tcep;
169 tce.te_rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
171 if (direction != DMA_TO_DEVICE)
174 /* We can map max one pageful of TCEs at a time */
177 * Set up the page with TCE data, looping through and setting
180 limit = min_t(long, npages, 4096/sizeof(union tce_entry));
182 for (l = 0; l < limit; l++) {
187 rc = plpar_tce_put_indirect((u64)tbl->it_index,
189 (u64)virt_to_abs(tcep),
194 } while (npages > 0 && !rc);
196 if (rc && printk_ratelimit()) {
197 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
198 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
199 printk("\tnpages = 0x%lx\n", (u64)npages);
200 printk("\ttce[0] val = 0x%lx\n", tcep[0].te_word);
201 show_stack(current, (unsigned long *)__get_SP());
205 static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
210 tcenum <<= TCE_PAGE_FACTOR;
211 npages <<= TCE_PAGE_FACTOR;
216 rc = plpar_tce_put((u64)tbl->it_index,
220 if (rc && printk_ratelimit()) {
221 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
222 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
223 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
224 printk("\ttce val = 0x%lx\n", tce.te_word );
225 show_stack(current, (unsigned long *)__get_SP());
233 static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
238 tcenum <<= TCE_PAGE_FACTOR;
239 npages <<= TCE_PAGE_FACTOR;
243 rc = plpar_tce_stuff((u64)tbl->it_index,
248 if (rc && printk_ratelimit()) {
249 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
250 printk("\trc = %ld\n", rc);
251 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
252 printk("\tnpages = 0x%lx\n", (u64)npages);
253 printk("\ttce val = 0x%lx\n", tce.te_word );
254 show_stack(current, (unsigned long *)__get_SP());
258 static void iommu_table_setparms(struct pci_controller *phb,
259 struct device_node *dn,
260 struct iommu_table *tbl)
262 struct device_node *node;
263 unsigned long *basep;
266 node = (struct device_node *)phb->arch_data;
268 basep = (unsigned long *)get_property(node, "linux,tce-base", NULL);
269 sizep = (unsigned int *)get_property(node, "linux,tce-size", NULL);
270 if (basep == NULL || sizep == NULL) {
271 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
272 "missing tce entries !\n", dn->full_name);
276 tbl->it_base = (unsigned long)__va(*basep);
277 memset((void *)tbl->it_base, 0, *sizep);
279 tbl->it_busno = phb->bus->number;
281 /* Units of tce entries */
282 tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT;
284 /* Test if we are going over 2GB of DMA space */
285 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
286 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
287 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
290 phb->dma_window_base_cur += phb->dma_window_size;
292 /* Set the tce table size - measured in entries */
293 tbl->it_size = phb->dma_window_size >> PAGE_SHIFT;
296 tbl->it_blocksize = 16;
297 tbl->it_type = TCE_PCI;
301 * iommu_table_setparms_lpar
303 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
305 * ToDo: properly interpret the ibm,dma-window property. The definition is:
306 * logical-bus-number (1 word)
307 * phys-address (#address-cells words)
308 * size (#cell-size words)
310 * Currently we hard code these sizes (more or less).
312 static void iommu_table_setparms_lpar(struct pci_controller *phb,
313 struct device_node *dn,
314 struct iommu_table *tbl,
315 unsigned int *dma_window)
317 tbl->it_busno = PCI_DN(dn)->bussubno;
319 /* TODO: Parse field size properties properly. */
320 tbl->it_size = (((unsigned long)dma_window[4] << 32) |
321 (unsigned long)dma_window[5]) >> PAGE_SHIFT;
322 tbl->it_offset = (((unsigned long)dma_window[2] << 32) |
323 (unsigned long)dma_window[3]) >> PAGE_SHIFT;
325 tbl->it_index = dma_window[0];
326 tbl->it_blocksize = 16;
327 tbl->it_type = TCE_PCI;
330 static void iommu_bus_setup_pSeries(struct pci_bus *bus)
332 struct device_node *dn;
333 struct iommu_table *tbl;
334 struct device_node *isa_dn, *isa_dn_orig;
335 struct device_node *tmp;
339 DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self);
341 dn = pci_bus_to_OF_node(bus);
345 /* This is not a root bus, any setup will be done for the
346 * device-side of the bridge in iommu_dev_setup_pSeries().
351 /* Check if the ISA bus on the system is under
354 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
356 while (isa_dn && isa_dn != dn)
357 isa_dn = isa_dn->parent;
360 of_node_put(isa_dn_orig);
362 /* Count number of direct PCI children of the PHB.
363 * All PCI device nodes have class-code property, so it's
364 * an easy way to find them.
366 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
367 if (get_property(tmp, "class-code", NULL))
370 DBG("Children: %d\n", children);
372 /* Calculate amount of DMA window per slot. Each window must be
373 * a power of two (due to pci_alloc_consistent requirements).
375 * Keep 256MB aside for PHBs with ISA.
379 /* No ISA/IDE - just set window size and return */
380 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
382 while (pci->phb->dma_window_size * children > 0x80000000ul)
383 pci->phb->dma_window_size >>= 1;
384 DBG("No ISA/IDE, window size is 0x%lx\n",
385 pci->phb->dma_window_size);
386 pci->phb->dma_window_base_cur = 0;
391 /* If we have ISA, then we probably have an IDE
392 * controller too. Allocate a 128MB table but
393 * skip the first 128MB to avoid stepping on ISA
396 pci->phb->dma_window_size = 0x8000000ul;
397 pci->phb->dma_window_base_cur = 0x8000000ul;
399 tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
401 iommu_table_setparms(pci->phb, dn, tbl);
402 pci->iommu_table = iommu_init_table(tbl);
404 /* Divide the rest (1.75GB) among the children */
405 pci->phb->dma_window_size = 0x80000000ul;
406 while (pci->phb->dma_window_size * children > 0x70000000ul)
407 pci->phb->dma_window_size >>= 1;
409 DBG("ISA/IDE, window size is 0x%lx\n", pci->phb->dma_window_size);
414 static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus)
416 struct iommu_table *tbl;
417 struct device_node *dn, *pdn;
419 unsigned int *dma_window = NULL;
421 DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self);
423 dn = pci_bus_to_OF_node(bus);
425 /* Find nearest ibm,dma-window, walking up the device tree */
426 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
427 dma_window = (unsigned int *)get_property(pdn, "ibm,dma-window", NULL);
428 if (dma_window != NULL)
432 if (dma_window == NULL) {
433 DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name);
438 if (!ppci->iommu_table) {
439 /* Bussubno hasn't been copied yet.
440 * Do it now because iommu_table_setparms_lpar needs it.
443 ppci->bussubno = bus->number;
445 tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
448 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
450 ppci->iommu_table = iommu_init_table(tbl);
454 PCI_DN(dn)->iommu_table = ppci->iommu_table;
458 static void iommu_dev_setup_pSeries(struct pci_dev *dev)
460 struct device_node *dn, *mydn;
461 struct iommu_table *tbl;
463 DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, pci_name(dev));
465 mydn = dn = pci_device_to_OF_node(dev);
467 /* If we're the direct child of a root bus, then we need to allocate
468 * an iommu table ourselves. The bus setup code should have setup
469 * the window sizes already.
471 if (!dev->bus->self) {
472 DBG(" --> first child, no bridge. Allocating iommu table.\n");
473 tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
474 iommu_table_setparms(PCI_DN(dn)->phb, dn, tbl);
475 PCI_DN(mydn)->iommu_table = iommu_init_table(tbl);
480 /* If this device is further down the bus tree, search upwards until
481 * an already allocated iommu table is found and use that.
484 while (dn && dn->data && PCI_DN(dn)->iommu_table == NULL)
487 if (dn && dn->data) {
488 PCI_DN(mydn)->iommu_table = PCI_DN(dn)->iommu_table;
490 DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, pci_name(dev));
494 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
497 struct device_node *np = node;
498 struct pci_dn *pci = np->data;
501 case PSERIES_RECONFIG_REMOVE:
502 if (pci && pci->iommu_table &&
503 get_property(np, "ibm,dma-window", NULL))
504 iommu_free_table(np);
513 static struct notifier_block iommu_reconfig_nb = {
514 .notifier_call = iommu_reconfig_notifier,
517 static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev)
519 struct device_node *pdn, *dn;
520 struct iommu_table *tbl;
521 int *dma_window = NULL;
524 DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, pci_name(dev));
526 /* dev setup for LPAR is a little tricky, since the device tree might
527 * contain the dma-window properties per-device and not neccesarily
528 * for the bus. So we need to search upwards in the tree until we
529 * either hit a dma-window property, OR find a parent with a table
532 dn = pci_device_to_OF_node(dev);
534 for (pdn = dn; pdn && pdn->data && !PCI_DN(pdn)->iommu_table;
536 dma_window = (unsigned int *)
537 get_property(pdn, "ibm,dma-window", NULL);
542 /* Check for parent == NULL so we don't try to setup the empty EADS
543 * slots on POWER4 machines.
545 if (dma_window == NULL || pdn->parent == NULL) {
546 DBG("No dma window for device, linking to parent\n");
547 PCI_DN(dn)->iommu_table = PCI_DN(pdn)->iommu_table;
550 DBG("Found DMA window, allocating table\n");
554 if (!pci->iommu_table) {
555 /* iommu_table_setparms_lpar needs bussubno. */
556 pci->bussubno = pci->phb->bus->number;
558 tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
561 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
563 pci->iommu_table = iommu_init_table(tbl);
567 PCI_DN(dn)->iommu_table = pci->iommu_table;
570 static void iommu_bus_setup_null(struct pci_bus *b) { }
571 static void iommu_dev_setup_null(struct pci_dev *d) { }
573 /* These are called very early. */
574 void iommu_init_early_pSeries(void)
576 if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) {
577 /* Direct I/O, IOMMU off */
578 ppc_md.iommu_dev_setup = iommu_dev_setup_null;
579 ppc_md.iommu_bus_setup = iommu_bus_setup_null;
580 pci_direct_iommu_init();
585 if (systemcfg->platform & PLATFORM_LPAR) {
586 if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
587 ppc_md.tce_build = tce_buildmulti_pSeriesLP;
588 ppc_md.tce_free = tce_freemulti_pSeriesLP;
590 ppc_md.tce_build = tce_build_pSeriesLP;
591 ppc_md.tce_free = tce_free_pSeriesLP;
593 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP;
594 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP;
596 ppc_md.tce_build = tce_build_pSeries;
597 ppc_md.tce_free = tce_free_pSeries;
598 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries;
599 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries;
603 pSeries_reconfig_notifier_register(&iommu_reconfig_nb);