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>
48 static void tce_build_pSeries(struct iommu_table *tbl, long index,
49 long npages, unsigned long uaddr,
50 enum dma_data_direction direction)
56 t.te_rdwr = 1; // Read allowed
58 if (direction != DMA_TO_DEVICE)
61 tp = ((union tce_entry *)tbl->it_base) + index;
64 /* can't move this out since we might cross LMB boundary */
65 t.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
67 tp->te_word = t.te_word;
75 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
81 tp = ((union tce_entry *)tbl->it_base) + index;
84 tp->te_word = t.te_word;
91 static void iommu_buses_init(void)
93 struct pci_controller* phb;
94 struct device_node *dn, *first_dn;
95 int num_slots, num_slots_ilog2;
97 unsigned long tcetable_ilog2;
100 * We default to a TCE table that maps 2GB (4MB table, 22 bits),
101 * however some machines have a 3GB IO hole and for these we
102 * create a table that maps 1GB (2MB table, 21 bits)
104 if (io_hole_start < 0x80000000UL)
109 /* XXX Should we be using pci_root_buses instead? -ojn
112 for (phb=hose_head; phb; phb=phb->next) {
113 first_dn = ((struct device_node *)phb->arch_data)->child;
115 /* Carve 2GB into the largest dma_window_size possible */
116 for (dn = first_dn, num_slots = 0; dn != NULL; dn = dn->sibling)
118 num_slots_ilog2 = __ilog2(num_slots);
120 if ((1<<num_slots_ilog2) != num_slots)
123 phb->dma_window_size = 1 << (tcetable_ilog2 - num_slots_ilog2);
125 /* Reserve 16MB of DMA space on the first PHB.
126 * We should probably be more careful and use firmware props.
127 * In reality this space is remapped, not lost. But we don't
128 * want to get that smart to handle it -- too much work.
130 phb->dma_window_base_cur = first_phb ? (1 << 12) : 0;
133 for (dn = first_dn; dn != NULL; dn = dn->sibling)
134 iommu_devnode_init(dn);
139 static void iommu_buses_init_lpar(struct list_head *bus_list)
141 struct list_head *ln;
143 struct device_node *busdn;
144 unsigned int *dma_window;
146 for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
148 busdn = PCI_GET_DN(bus);
150 dma_window = (unsigned int *)get_property(busdn, "ibm,dma-window", 0);
152 /* Bussubno hasn't been copied yet.
153 * Do it now because iommu_table_setparms_lpar needs it.
155 busdn->bussubno = bus->number;
156 iommu_devnode_init(busdn);
159 /* look for a window on a bridge even if the PHB had one */
160 iommu_buses_init_lpar(&bus->children);
165 static void iommu_table_setparms(struct pci_controller *phb,
166 struct device_node *dn,
167 struct iommu_table *tbl)
171 struct of_tce_table *oft;
173 node = ((struct device_node *)(phb->arch_data))->node;
177 for (i=0; of_tce_table[i].node; i++)
178 if(of_tce_table[i].node == node) {
179 oft = &of_tce_table[i];
184 panic("PCI_DMA: iommu_table_setparms: Can't find phb named '%s' in of_tce_table\n", dn->full_name);
186 memset((void *)oft->base, 0, oft->size);
188 tbl->it_busno = phb->bus->number;
190 /* Units of tce entries */
191 tbl->it_offset = phb->dma_window_base_cur;
193 /* Adjust the current table offset to the next
194 * region. Measured in TCE entries. Force an
195 * alignment to the size allotted per IOA. This
196 * makes it easier to remove the 1st 16MB.
198 phb->dma_window_base_cur += (phb->dma_window_size>>3);
199 phb->dma_window_base_cur &=
200 ~((phb->dma_window_size>>3)-1);
202 /* Set the tce table size - measured in pages */
203 tbl->it_size = ((phb->dma_window_base_cur -
204 tbl->it_offset) << 3) >> PAGE_SHIFT;
206 /* Test if we are going over 2GB of DMA space */
207 if (phb->dma_window_base_cur > (1 << 19))
208 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
210 tbl->it_base = oft->base;
212 tbl->it_entrysize = sizeof(union tce_entry);
213 tbl->it_blocksize = 16;
217 * iommu_table_setparms_lpar
219 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
221 * ToDo: properly interpret the ibm,dma-window property. The definition is:
222 * logical-bus-number (1 word)
223 * phys-address (#address-cells words)
224 * size (#cell-size words)
226 * Currently we hard code these sizes (more or less).
228 static void iommu_table_setparms_lpar(struct pci_controller *phb,
229 struct device_node *dn,
230 struct iommu_table *tbl)
232 unsigned int *dma_window;
234 dma_window = (unsigned int *)get_property(dn, "ibm,dma-window", 0);
237 panic("iommu_table_setparms_lpar: device %s has no"
238 " ibm,dma-window property!\n", dn->full_name);
240 tbl->it_busno = dn->bussubno;
241 tbl->it_size = (((((unsigned long)dma_window[4] << 32) |
242 (unsigned long)dma_window[5]) >> PAGE_SHIFT) << 3) >> PAGE_SHIFT;
243 tbl->it_offset = ((((unsigned long)dma_window[2] << 32) |
244 (unsigned long)dma_window[3]) >> 12);
246 tbl->it_index = dma_window[0];
247 tbl->it_entrysize = sizeof(union tce_entry);
248 tbl->it_blocksize = 16;
252 void iommu_devnode_init(struct device_node *dn)
254 struct iommu_table *tbl;
256 tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
259 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
260 iommu_table_setparms_lpar(dn->phb, dn, tbl);
262 iommu_table_setparms(dn->phb, dn, tbl);
264 dn->iommu_table = iommu_init_table(tbl);
268 void iommu_setup_pSeries(void)
270 struct pci_dev *dev = NULL;
271 struct device_node *dn, *mydn;
273 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
274 iommu_buses_init_lpar(&pci_root_buses);
278 /* Now copy the iommu_table ptr from the bus devices down to every
279 * pci device_node. This means get_iommu_table() won't need to search
280 * up the device tree to find it.
282 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
283 mydn = dn = PCI_GET_DN(dev);
285 while (dn && dn->iommu_table == NULL)
288 mydn->iommu_table = dn->iommu_table;
293 /* These are called very early. */
294 void tce_init_pSeries(void)
296 ppc_md.tce_build = tce_build_pSeries;
297 ppc_md.tce_free = tce_free_pSeries;