ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / pSeries_iommu.c
1 /*
2  * arch/ppc64/kernel/pSeries_iommu.c
3  *
4  * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
5  *
6  * Rewrite, cleanup: 
7  *
8  * Copyright (C) 2004 Olof Johansson <olof@austin.ibm.com>, IBM Corporation
9  *
10  * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR.
11  *
12  * 
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.
17  * 
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.
22  * 
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
26  */
27
28 #include <linux/config.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/mm.h>
33 #include <linux/spinlock.h>
34 #include <linux/string.h>
35 #include <linux/pci.h>
36 #include <linux/dma-mapping.h>
37 #include <asm/io.h>
38 #include <asm/prom.h>
39 #include <asm/rtas.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 "pci.h"
46
47
48 static void tce_build_pSeries(struct iommu_table *tbl, long index, 
49                               long npages, unsigned long uaddr, 
50                               enum dma_data_direction direction)
51 {
52         union tce_entry t;
53         union tce_entry *tp;
54
55         t.te_word = 0;
56         t.te_rdwr = 1; // Read allowed 
57
58         if (direction != DMA_TO_DEVICE)
59                 t.te_pciwr = 1;
60
61         tp = ((union tce_entry *)tbl->it_base) + index;
62
63         while (npages--) {
64                 /* can't move this out since we might cross LMB boundary */
65                 t.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
66         
67                 tp->te_word = t.te_word;
68
69                 uaddr += PAGE_SIZE;
70                 tp++;
71         }
72 }
73
74
75 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
76 {
77         union tce_entry t;
78         union tce_entry *tp;
79
80         t.te_word = 0;
81         tp  = ((union tce_entry *)tbl->it_base) + index;
82                 
83         while (npages--) {
84                 tp->te_word = t.te_word;
85                 
86                 tp++;
87         }
88 }
89
90
91 static void iommu_buses_init(void)
92 {
93         struct pci_controller* phb;
94         struct device_node *dn, *first_dn;
95         int num_slots, num_slots_ilog2;
96         int first_phb = 1;
97         unsigned long tcetable_ilog2;
98
99         /*
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)
103          */
104         if (io_hole_start < 0x80000000UL)
105                 tcetable_ilog2 = 21;
106         else
107                 tcetable_ilog2 = 22;
108
109         /* XXX Should we be using pci_root_buses instead?  -ojn 
110          */
111
112         for (phb=hose_head; phb; phb=phb->next) {
113                 first_dn = ((struct device_node *)phb->arch_data)->child;
114
115                 /* Carve 2GB into the largest dma_window_size possible */
116                 for (dn = first_dn, num_slots = 0; dn != NULL; dn = dn->sibling)
117                         num_slots++;
118                 num_slots_ilog2 = __ilog2(num_slots);
119
120                 if ((1<<num_slots_ilog2) != num_slots)
121                         num_slots_ilog2++;
122
123                 phb->dma_window_size = 1 << (tcetable_ilog2 - num_slots_ilog2);
124
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.
129                  */
130                 phb->dma_window_base_cur = first_phb ? (1 << 12) : 0;
131                 first_phb = 0;
132
133                 for (dn = first_dn; dn != NULL; dn = dn->sibling)
134                         iommu_devnode_init(dn);
135         }
136 }
137
138
139 static void iommu_buses_init_lpar(struct list_head *bus_list)
140 {
141         struct list_head *ln;
142         struct pci_bus *bus;
143         struct device_node *busdn;
144         unsigned int *dma_window;
145
146         for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
147                 bus = pci_bus_b(ln);
148                 busdn = PCI_GET_DN(bus);
149
150                 dma_window = (unsigned int *)get_property(busdn, "ibm,dma-window", 0);
151                 if (dma_window) {
152                         /* Bussubno hasn't been copied yet.
153                          * Do it now because iommu_table_setparms_lpar needs it.
154                          */
155                         busdn->bussubno = bus->number;
156                         iommu_devnode_init(busdn);
157                 }
158
159                 /* look for a window on a bridge even if the PHB had one */
160                 iommu_buses_init_lpar(&bus->children);
161         }
162 }
163
164
165 static void iommu_table_setparms(struct pci_controller *phb,
166                                  struct device_node *dn,
167                                  struct iommu_table *tbl) 
168 {
169         phandle node;
170         unsigned long i;
171         struct of_tce_table *oft;
172
173         node = ((struct device_node *)(phb->arch_data))->node;
174
175         oft = NULL;
176
177         for (i=0; of_tce_table[i].node; i++)
178                 if(of_tce_table[i].node == node) {
179                         oft = &of_tce_table[i];
180                         break;
181                 }
182
183         if (!oft)
184                 panic("PCI_DMA: iommu_table_setparms: Can't find phb named '%s' in of_tce_table\n", dn->full_name);
185
186         memset((void *)oft->base, 0, oft->size);
187
188         tbl->it_busno = phb->bus->number;
189         
190         /* Units of tce entries */
191         tbl->it_offset = phb->dma_window_base_cur;
192         
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.
197          */
198         phb->dma_window_base_cur += (phb->dma_window_size>>3);
199         phb->dma_window_base_cur &= 
200                 ~((phb->dma_window_size>>3)-1);
201         
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;
205         
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"); 
209         
210         tbl->it_base = oft->base;
211         tbl->it_index = 0;
212         tbl->it_entrysize = sizeof(union tce_entry);
213         tbl->it_blocksize = 16;
214 }
215
216 /*
217  * iommu_table_setparms_lpar
218  *
219  * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
220  *
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)
225  *
226  * Currently we hard code these sizes (more or less).
227  */
228 static void iommu_table_setparms_lpar(struct pci_controller *phb,
229                                       struct device_node *dn,
230                                       struct iommu_table *tbl)
231 {
232         unsigned int *dma_window;
233
234         dma_window = (unsigned int *)get_property(dn, "ibm,dma-window", 0);
235
236         if (!dma_window)
237                 panic("iommu_table_setparms_lpar: device %s has no"
238                       " ibm,dma-window property!\n", dn->full_name);
239
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);
245         tbl->it_base   = 0;
246         tbl->it_index  = dma_window[0];
247         tbl->it_entrysize = sizeof(union tce_entry);
248         tbl->it_blocksize  = 16;
249 }
250
251
252 void iommu_devnode_init(struct device_node *dn)
253 {
254         struct iommu_table *tbl;
255
256         tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table), 
257                                             GFP_KERNEL);
258         
259         if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
260                 iommu_table_setparms_lpar(dn->phb, dn, tbl);
261         else
262                 iommu_table_setparms(dn->phb, dn, tbl);
263         
264         dn->iommu_table = iommu_init_table(tbl);
265 }
266
267
268 void iommu_setup_pSeries(void)
269 {
270         struct pci_dev *dev = NULL;
271         struct device_node *dn, *mydn;
272
273         if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
274                 iommu_buses_init_lpar(&pci_root_buses);
275         else
276                 iommu_buses_init();
277
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.
281          */
282         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
283                 mydn = dn = PCI_GET_DN(dev);
284
285                 while (dn && dn->iommu_table == NULL)
286                         dn = dn->parent;
287                 if (dn)
288                         mydn->iommu_table = dn->iommu_table;
289         }
290 }
291
292
293 /* These are called very early. */
294 void tce_init_pSeries(void)
295 {
296         ppc_md.tce_build = tce_build_pSeries;
297         ppc_md.tce_free  = tce_free_pSeries;
298
299         pci_iommu_init();
300 }
301