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/plpar_wrappers.h>
46 #include <asm/pSeries_reconfig.h>
47 #include <asm/systemcfg.h>
52 extern int is_python(struct device_node
*);
54 static void tce_build_pSeries(struct iommu_table
*tbl
, long index
,
55 long npages
, unsigned long uaddr
,
56 enum dma_data_direction direction
)
62 t
.te_rdwr
= 1; // Read allowed
64 if (direction
!= DMA_TO_DEVICE
)
67 tp
= ((union tce_entry
*)tbl
->it_base
) + index
;
70 /* can't move this out since we might cross LMB boundary */
71 t
.te_rpn
= (virt_to_abs(uaddr
)) >> PAGE_SHIFT
;
73 tp
->te_word
= t
.te_word
;
81 static void tce_free_pSeries(struct iommu_table
*tbl
, long index
, long npages
)
87 tp
= ((union tce_entry
*)tbl
->it_base
) + index
;
90 tp
->te_word
= t
.te_word
;
97 static void tce_build_pSeriesLP(struct iommu_table
*tbl
, long tcenum
,
98 long npages
, unsigned long uaddr
,
99 enum dma_data_direction direction
)
105 tce
.te_rpn
= (virt_to_abs(uaddr
)) >> PAGE_SHIFT
;
107 if (direction
!= DMA_TO_DEVICE
)
111 rc
= plpar_tce_put((u64
)tbl
->it_index
,
115 if (rc
&& printk_ratelimit()) {
116 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc
);
117 printk("\tindex = 0x%lx\n", (u64
)tbl
->it_index
);
118 printk("\ttcenum = 0x%lx\n", (u64
)tcenum
);
119 printk("\ttce val = 0x%lx\n", tce
.te_word
);
120 show_stack(current
, (unsigned long *)__get_SP());
128 static DEFINE_PER_CPU(void *, tce_page
) = NULL
;
130 static void tce_buildmulti_pSeriesLP(struct iommu_table
*tbl
, long tcenum
,
131 long npages
, unsigned long uaddr
,
132 enum dma_data_direction direction
)
135 union tce_entry tce
, *tcep
;
139 return tce_build_pSeriesLP(tbl
, tcenum
, npages
, uaddr
,
142 tcep
= __get_cpu_var(tce_page
);
144 /* This is safe to do since interrupts are off when we're called
145 * from iommu_alloc{,_sg}()
148 tcep
= (void *)__get_free_page(GFP_ATOMIC
);
149 /* If allocation fails, fall back to the loop implementation */
151 return tce_build_pSeriesLP(tbl
, tcenum
, npages
,
153 __get_cpu_var(tce_page
) = tcep
;
157 tce
.te_rpn
= (virt_to_abs(uaddr
)) >> PAGE_SHIFT
;
159 if (direction
!= DMA_TO_DEVICE
)
162 /* We can map max one pageful of TCEs at a time */
165 * Set up the page with TCE data, looping through and setting
168 limit
= min_t(long, npages
, PAGE_SIZE
/sizeof(union tce_entry
));
170 for (l
= 0; l
< limit
; l
++) {
175 rc
= plpar_tce_put_indirect((u64
)tbl
->it_index
,
177 (u64
)virt_to_abs(tcep
),
182 } while (npages
> 0 && !rc
);
184 if (rc
&& printk_ratelimit()) {
185 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc
);
186 printk("\tindex = 0x%lx\n", (u64
)tbl
->it_index
);
187 printk("\tnpages = 0x%lx\n", (u64
)npages
);
188 printk("\ttce[0] val = 0x%lx\n", tcep
[0].te_word
);
189 show_stack(current
, (unsigned long *)__get_SP());
193 static void tce_free_pSeriesLP(struct iommu_table
*tbl
, long tcenum
, long npages
)
201 rc
= plpar_tce_put((u64
)tbl
->it_index
,
205 if (rc
&& printk_ratelimit()) {
206 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc
);
207 printk("\tindex = 0x%lx\n", (u64
)tbl
->it_index
);
208 printk("\ttcenum = 0x%lx\n", (u64
)tcenum
);
209 printk("\ttce val = 0x%lx\n", tce
.te_word
);
210 show_stack(current
, (unsigned long *)__get_SP());
218 static void tce_freemulti_pSeriesLP(struct iommu_table
*tbl
, long tcenum
, long npages
)
225 rc
= plpar_tce_stuff((u64
)tbl
->it_index
,
230 if (rc
&& printk_ratelimit()) {
231 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
232 printk("\trc = %ld\n", rc
);
233 printk("\tindex = 0x%lx\n", (u64
)tbl
->it_index
);
234 printk("\tnpages = 0x%lx\n", (u64
)npages
);
235 printk("\ttce val = 0x%lx\n", tce
.te_word
);
236 show_stack(current
, (unsigned long *)__get_SP());
240 static void iommu_table_setparms(struct pci_controller
*phb
,
241 struct device_node
*dn
,
242 struct iommu_table
*tbl
)
244 struct device_node
*node
;
245 unsigned long *basep
;
248 node
= (struct device_node
*)phb
->arch_data
;
250 basep
= (unsigned long *)get_property(node
, "linux,tce-base", NULL
);
251 sizep
= (unsigned int *)get_property(node
, "linux,tce-size", NULL
);
252 if (basep
== NULL
|| sizep
== NULL
) {
253 printk(KERN_ERR
"PCI_DMA: iommu_table_setparms: %s has "
254 "missing tce entries !\n", dn
->full_name
);
258 tbl
->it_base
= (unsigned long)__va(*basep
);
259 memset((void *)tbl
->it_base
, 0, *sizep
);
261 tbl
->it_busno
= phb
->bus
->number
;
263 /* Units of tce entries */
264 tbl
->it_offset
= phb
->dma_window_base_cur
>> PAGE_SHIFT
;
266 /* Test if we are going over 2GB of DMA space */
267 if (phb
->dma_window_base_cur
+ phb
->dma_window_size
> (1L << 31))
268 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
270 phb
->dma_window_base_cur
+= phb
->dma_window_size
;
272 /* Set the tce table size - measured in entries */
273 tbl
->it_size
= phb
->dma_window_size
>> PAGE_SHIFT
;
276 tbl
->it_blocksize
= 16;
277 tbl
->it_type
= TCE_PCI
;
281 * iommu_table_setparms_lpar
283 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
285 * ToDo: properly interpret the ibm,dma-window property. The definition is:
286 * logical-bus-number (1 word)
287 * phys-address (#address-cells words)
288 * size (#cell-size words)
290 * Currently we hard code these sizes (more or less).
292 static void iommu_table_setparms_lpar(struct pci_controller
*phb
,
293 struct device_node
*dn
,
294 struct iommu_table
*tbl
,
295 unsigned int *dma_window
)
297 tbl
->it_busno
= dn
->bussubno
;
299 /* TODO: Parse field size properties properly. */
300 tbl
->it_size
= (((unsigned long)dma_window
[4] << 32) |
301 (unsigned long)dma_window
[5]) >> PAGE_SHIFT
;
302 tbl
->it_offset
= (((unsigned long)dma_window
[2] << 32) |
303 (unsigned long)dma_window
[3]) >> PAGE_SHIFT
;
305 tbl
->it_index
= dma_window
[0];
306 tbl
->it_blocksize
= 16;
307 tbl
->it_type
= TCE_PCI
;
310 static void iommu_bus_setup_pSeries(struct pci_bus
*bus
)
312 struct device_node
*dn
, *pdn
;
313 struct iommu_table
*tbl
;
315 DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus
, bus
->self
);
317 /* For each (root) bus, we carve up the available DMA space in 256MB
318 * pieces. Since each piece is used by one (sub) bus/device, that would
319 * give a maximum of 7 devices per PHB. In most cases, this is plenty.
321 * The exception is on Python PHBs (pre-POWER4). Here we don't have EADS
322 * bridges below the PHB to allocate the sectioned tables to, so instead
323 * we allocate a 1GB table at the PHB level.
326 dn
= pci_bus_to_OF_node(bus
);
331 unsigned int *iohole
;
333 DBG("Python root bus %s\n", bus
->name
);
335 iohole
= (unsigned int *)get_property(dn
, "io-hole", 0);
338 /* On first bus we need to leave room for the
339 * ISA address space. Just skip the first 256MB
340 * alltogether. This leaves 768MB for the window.
342 DBG("PHB has io-hole, reserving 256MB\n");
343 dn
->phb
->dma_window_size
= 3 << 28;
344 dn
->phb
->dma_window_base_cur
= 1 << 28;
346 /* 1GB window by default */
347 dn
->phb
->dma_window_size
= 1 << 30;
348 dn
->phb
->dma_window_base_cur
= 0;
351 tbl
= kmalloc(sizeof(struct iommu_table
), GFP_KERNEL
);
353 iommu_table_setparms(dn
->phb
, dn
, tbl
);
354 dn
->iommu_table
= iommu_init_table(tbl
);
356 /* Do a 128MB table at root. This is used for the IDE
357 * controller on some SMP-mode POWER4 machines. It
358 * doesn't hurt to allocate it on other machines
359 * -- it'll just be unused since new tables are
360 * allocated on the EADS level.
362 * Allocate at offset 128MB to avoid having to deal
363 * with ISA holes; 128MB table for IDE is plenty.
365 dn
->phb
->dma_window_size
= 1 << 27;
366 dn
->phb
->dma_window_base_cur
= 1 << 27;
368 tbl
= kmalloc(sizeof(struct iommu_table
), GFP_KERNEL
);
370 iommu_table_setparms(dn
->phb
, dn
, tbl
);
371 dn
->iommu_table
= iommu_init_table(tbl
);
373 /* All child buses have 256MB tables */
374 dn
->phb
->dma_window_size
= 1 << 28;
377 pdn
= pci_bus_to_OF_node(bus
->parent
);
379 if (!bus
->parent
->self
&& !is_python(pdn
)) {
380 struct iommu_table
*tbl
;
381 /* First child and not python means this is the EADS
382 * level. Allocate new table for this slot with 256MB
386 tbl
= kmalloc(sizeof(struct iommu_table
), GFP_KERNEL
);
388 iommu_table_setparms(dn
->phb
, dn
, tbl
);
390 dn
->iommu_table
= iommu_init_table(tbl
);
392 /* Lower than first child or under python, use parent table */
393 dn
->iommu_table
= pdn
->iommu_table
;
399 static void iommu_bus_setup_pSeriesLP(struct pci_bus
*bus
)
401 struct iommu_table
*tbl
;
402 struct device_node
*dn
, *pdn
;
403 unsigned int *dma_window
= NULL
;
405 DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus
, bus
->self
);
407 dn
= pci_bus_to_OF_node(bus
);
409 /* Find nearest ibm,dma-window, walking up the device tree */
410 for (pdn
= dn
; pdn
!= NULL
; pdn
= pdn
->parent
) {
411 dma_window
= (unsigned int *)get_property(pdn
, "ibm,dma-window", NULL
);
412 if (dma_window
!= NULL
)
416 if (dma_window
== NULL
) {
417 DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn
->full_name
);
421 if (!pdn
->iommu_table
) {
422 /* Bussubno hasn't been copied yet.
423 * Do it now because iommu_table_setparms_lpar needs it.
425 pdn
->bussubno
= bus
->number
;
427 tbl
= (struct iommu_table
*)kmalloc(sizeof(struct iommu_table
),
430 iommu_table_setparms_lpar(pdn
->phb
, pdn
, tbl
, dma_window
);
432 pdn
->iommu_table
= iommu_init_table(tbl
);
436 dn
->iommu_table
= pdn
->iommu_table
;
440 static void iommu_dev_setup_pSeries(struct pci_dev
*dev
)
442 struct device_node
*dn
, *mydn
;
444 DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev
, dev
->pretty_name
);
445 /* Now copy the iommu_table ptr from the bus device down to the
446 * pci device_node. This means get_iommu_table() won't need to search
447 * up the device tree to find it.
449 mydn
= dn
= pci_device_to_OF_node(dev
);
451 while (dn
&& dn
->iommu_table
== NULL
)
455 mydn
->iommu_table
= dn
->iommu_table
;
457 DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev
, dev
->pretty_name
);
461 static int iommu_reconfig_notifier(struct notifier_block
*nb
, unsigned long action
, void *node
)
464 struct device_node
*np
= node
;
467 case PSERIES_RECONFIG_REMOVE
:
468 if (np
->iommu_table
&&
469 get_property(np
, "ibm,dma-window", NULL
))
470 iommu_free_table(np
);
479 static struct notifier_block iommu_reconfig_nb
= {
480 .notifier_call
= iommu_reconfig_notifier
,
483 static void iommu_dev_setup_pSeriesLP(struct pci_dev
*dev
)
485 struct device_node
*pdn
, *dn
;
486 struct iommu_table
*tbl
;
487 int *dma_window
= NULL
;
489 DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev
, dev
->pretty_name
);
491 /* dev setup for LPAR is a little tricky, since the device tree might
492 * contain the dma-window properties per-device and not neccesarily
493 * for the bus. So we need to search upwards in the tree until we
494 * either hit a dma-window property, OR find a parent with a table
497 dn
= pci_device_to_OF_node(dev
);
499 for (pdn
= dn
; pdn
&& !pdn
->iommu_table
; pdn
= pdn
->parent
) {
500 dma_window
= (unsigned int *)get_property(pdn
, "ibm,dma-window", NULL
);
505 /* Check for parent == NULL so we don't try to setup the empty EADS
506 * slots on POWER4 machines.
508 if (dma_window
== NULL
|| pdn
->parent
== NULL
) {
509 /* Fall back to regular (non-LPAR) dev setup */
510 DBG("No dma window for device, falling back to regular setup\n");
511 iommu_dev_setup_pSeries(dev
);
514 DBG("Found DMA window, allocating table\n");
517 if (!pdn
->iommu_table
) {
518 /* iommu_table_setparms_lpar needs bussubno. */
519 pdn
->bussubno
= pdn
->phb
->bus
->number
;
521 tbl
= (struct iommu_table
*)kmalloc(sizeof(struct iommu_table
),
524 iommu_table_setparms_lpar(pdn
->phb
, pdn
, tbl
, dma_window
);
526 pdn
->iommu_table
= iommu_init_table(tbl
);
530 dn
->iommu_table
= pdn
->iommu_table
;
533 static void iommu_bus_setup_null(struct pci_bus
*b
) { }
534 static void iommu_dev_setup_null(struct pci_dev
*d
) { }
536 /* These are called very early. */
537 void iommu_init_early_pSeries(void)
539 if (of_chosen
&& get_property(of_chosen
, "linux,iommu-off", NULL
)) {
540 /* Direct I/O, IOMMU off */
541 ppc_md
.iommu_dev_setup
= iommu_dev_setup_null
;
542 ppc_md
.iommu_bus_setup
= iommu_bus_setup_null
;
543 pci_direct_iommu_init();
548 if (systemcfg
->platform
& PLATFORM_LPAR
) {
549 if (cur_cpu_spec
->firmware_features
& FW_FEATURE_MULTITCE
) {
550 ppc_md
.tce_build
= tce_buildmulti_pSeriesLP
;
551 ppc_md
.tce_free
= tce_freemulti_pSeriesLP
;
553 ppc_md
.tce_build
= tce_build_pSeriesLP
;
554 ppc_md
.tce_free
= tce_free_pSeriesLP
;
556 ppc_md
.iommu_bus_setup
= iommu_bus_setup_pSeriesLP
;
557 ppc_md
.iommu_dev_setup
= iommu_dev_setup_pSeriesLP
;
559 ppc_md
.tce_build
= tce_build_pSeries
;
560 ppc_md
.tce_free
= tce_free_pSeries
;
561 ppc_md
.iommu_bus_setup
= iommu_bus_setup_pSeries
;
562 ppc_md
.iommu_dev_setup
= iommu_dev_setup_pSeries
;
566 pSeries_reconfig_notifier_register(&iommu_reconfig_nb
);