]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/ppc64/kernel/pSeries_iommu.c
25d6c7eebfd06753611b6a326a3dd1c7e13a6985
[karo-tx-linux.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 <asm/plpar_wrappers.h>
46 #include <asm/pSeries_reconfig.h>
47 #include <asm/systemcfg.h>
48 #include <asm/firmware.h>
49 #include "pci.h"
50
51 #define DBG(fmt...)
52
53 extern int is_python(struct device_node *);
54
55 static void tce_build_pSeries(struct iommu_table *tbl, long index, 
56                               long npages, unsigned long uaddr, 
57                               enum dma_data_direction direction)
58 {
59         union tce_entry t;
60         union tce_entry *tp;
61
62         t.te_word = 0;
63         t.te_rdwr = 1; // Read allowed 
64
65         if (direction != DMA_TO_DEVICE)
66                 t.te_pciwr = 1;
67
68         tp = ((union tce_entry *)tbl->it_base) + index;
69
70         while (npages--) {
71                 /* can't move this out since we might cross LMB boundary */
72                 t.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
73         
74                 tp->te_word = t.te_word;
75
76                 uaddr += PAGE_SIZE;
77                 tp++;
78         }
79 }
80
81
82 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
83 {
84         union tce_entry t;
85         union tce_entry *tp;
86
87         t.te_word = 0;
88         tp  = ((union tce_entry *)tbl->it_base) + index;
89                 
90         while (npages--) {
91                 tp->te_word = t.te_word;
92                 
93                 tp++;
94         }
95 }
96
97
98 static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
99                                 long npages, unsigned long uaddr,
100                                 enum dma_data_direction direction)
101 {
102         u64 rc;
103         union tce_entry tce;
104
105         tce.te_word = 0;
106         tce.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
107         tce.te_rdwr = 1;
108         if (direction != DMA_TO_DEVICE)
109                 tce.te_pciwr = 1;
110
111         while (npages--) {
112                 rc = plpar_tce_put((u64)tbl->it_index, 
113                                    (u64)tcenum << 12, 
114                                    tce.te_word );
115                 
116                 if (rc && printk_ratelimit()) {
117                         printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
118                         printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
119                         printk("\ttcenum  = 0x%lx\n", (u64)tcenum);
120                         printk("\ttce val = 0x%lx\n", tce.te_word );
121                         show_stack(current, (unsigned long *)__get_SP());
122                 }
123                         
124                 tcenum++;
125                 tce.te_rpn++;
126         }
127 }
128
129 static DEFINE_PER_CPU(void *, tce_page) = NULL;
130
131 static void tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
132                                      long npages, unsigned long uaddr,
133                                      enum dma_data_direction direction)
134 {
135         u64 rc;
136         union tce_entry tce, *tcep;
137         long l, limit;
138
139         if (npages == 1)
140                 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
141                                            direction);
142
143         tcep = __get_cpu_var(tce_page);
144
145         /* This is safe to do since interrupts are off when we're called
146          * from iommu_alloc{,_sg}()
147          */
148         if (!tcep) {
149                 tcep = (void *)__get_free_page(GFP_ATOMIC);
150                 /* If allocation fails, fall back to the loop implementation */
151                 if (!tcep)
152                         return tce_build_pSeriesLP(tbl, tcenum, npages,
153                                                    uaddr, direction);
154                 __get_cpu_var(tce_page) = tcep;
155         }
156
157         tce.te_word = 0;
158         tce.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
159         tce.te_rdwr = 1;
160         if (direction != DMA_TO_DEVICE)
161                 tce.te_pciwr = 1;
162
163         /* We can map max one pageful of TCEs at a time */
164         do {
165                 /*
166                  * Set up the page with TCE data, looping through and setting
167                  * the values.
168                  */
169                 limit = min_t(long, npages, PAGE_SIZE/sizeof(union tce_entry));
170
171                 for (l = 0; l < limit; l++) {
172                         tcep[l] = tce;
173                         tce.te_rpn++;
174                 }
175
176                 rc = plpar_tce_put_indirect((u64)tbl->it_index,
177                                             (u64)tcenum << 12,
178                                             (u64)virt_to_abs(tcep),
179                                             limit);
180
181                 npages -= limit;
182                 tcenum += limit;
183         } while (npages > 0 && !rc);
184
185         if (rc && printk_ratelimit()) {
186                 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
187                 printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
188                 printk("\tnpages  = 0x%lx\n", (u64)npages);
189                 printk("\ttce[0] val = 0x%lx\n", tcep[0].te_word);
190                 show_stack(current, (unsigned long *)__get_SP());
191         }
192 }
193
194 static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
195 {
196         u64 rc;
197         union tce_entry tce;
198
199         tce.te_word = 0;
200
201         while (npages--) {
202                 rc = plpar_tce_put((u64)tbl->it_index,
203                                    (u64)tcenum << 12,
204                                    tce.te_word);
205
206                 if (rc && printk_ratelimit()) {
207                         printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
208                         printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
209                         printk("\ttcenum  = 0x%lx\n", (u64)tcenum);
210                         printk("\ttce val = 0x%lx\n", tce.te_word );
211                         show_stack(current, (unsigned long *)__get_SP());
212                 }
213
214                 tcenum++;
215         }
216 }
217
218
219 static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
220 {
221         u64 rc;
222         union tce_entry tce;
223
224         tce.te_word = 0;
225
226         rc = plpar_tce_stuff((u64)tbl->it_index,
227                            (u64)tcenum << 12,
228                            tce.te_word,
229                            npages);
230
231         if (rc && printk_ratelimit()) {
232                 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
233                 printk("\trc      = %ld\n", rc);
234                 printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
235                 printk("\tnpages  = 0x%lx\n", (u64)npages);
236                 printk("\ttce val = 0x%lx\n", tce.te_word );
237                 show_stack(current, (unsigned long *)__get_SP());
238         }
239 }
240
241 static void iommu_table_setparms(struct pci_controller *phb,
242                                  struct device_node *dn,
243                                  struct iommu_table *tbl) 
244 {
245         struct device_node *node;
246         unsigned long *basep;
247         unsigned int *sizep;
248
249         node = (struct device_node *)phb->arch_data;
250
251         basep = (unsigned long *)get_property(node, "linux,tce-base", NULL);
252         sizep = (unsigned int *)get_property(node, "linux,tce-size", NULL);
253         if (basep == NULL || sizep == NULL) {
254                 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
255                                 "missing tce entries !\n", dn->full_name);
256                 return;
257         }
258
259         tbl->it_base = (unsigned long)__va(*basep);
260         memset((void *)tbl->it_base, 0, *sizep);
261
262         tbl->it_busno = phb->bus->number;
263         
264         /* Units of tce entries */
265         tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT;
266         
267         /* Test if we are going over 2GB of DMA space */
268         if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
269                 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
270                 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n"); 
271         }
272         
273         phb->dma_window_base_cur += phb->dma_window_size;
274
275         /* Set the tce table size - measured in entries */
276         tbl->it_size = phb->dma_window_size >> PAGE_SHIFT;
277
278         tbl->it_index = 0;
279         tbl->it_blocksize = 16;
280         tbl->it_type = TCE_PCI;
281 }
282
283 /*
284  * iommu_table_setparms_lpar
285  *
286  * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
287  *
288  * ToDo: properly interpret the ibm,dma-window property.  The definition is:
289  *      logical-bus-number      (1 word)
290  *      phys-address            (#address-cells words)
291  *      size                    (#cell-size words)
292  *
293  * Currently we hard code these sizes (more or less).
294  */
295 static void iommu_table_setparms_lpar(struct pci_controller *phb,
296                                       struct device_node *dn,
297                                       struct iommu_table *tbl,
298                                       unsigned int *dma_window)
299 {
300         tbl->it_busno  = PCI_DN(dn)->bussubno;
301
302         /* TODO: Parse field size properties properly. */
303         tbl->it_size   = (((unsigned long)dma_window[4] << 32) |
304                            (unsigned long)dma_window[5]) >> PAGE_SHIFT;
305         tbl->it_offset = (((unsigned long)dma_window[2] << 32) |
306                            (unsigned long)dma_window[3]) >> PAGE_SHIFT;
307         tbl->it_base   = 0;
308         tbl->it_index  = dma_window[0];
309         tbl->it_blocksize  = 16;
310         tbl->it_type = TCE_PCI;
311 }
312
313 static void iommu_bus_setup_pSeries(struct pci_bus *bus)
314 {
315         struct device_node *dn;
316         struct iommu_table *tbl;
317         struct device_node *isa_dn, *isa_dn_orig;
318         struct device_node *tmp;
319         struct pci_dn *pci;
320         int children;
321
322         DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self);
323
324         dn = pci_bus_to_OF_node(bus);
325         pci = PCI_DN(dn);
326
327         if (bus->self) {
328                 /* This is not a root bus, any setup will be done for the
329                  * device-side of the bridge in iommu_dev_setup_pSeries().
330                  */
331                 return;
332         }
333
334         /* Check if the ISA bus on the system is under
335          * this PHB.
336          */
337         isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
338
339         while (isa_dn && isa_dn != dn)
340                 isa_dn = isa_dn->parent;
341
342         if (isa_dn_orig)
343                 of_node_put(isa_dn_orig);
344
345         /* Count number of direct PCI children of the PHB.
346          * All PCI device nodes have class-code property, so it's
347          * an easy way to find them.
348          */
349         for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
350                 if (get_property(tmp, "class-code", NULL))
351                         children++;
352
353         DBG("Children: %d\n", children);
354
355         /* Calculate amount of DMA window per slot. Each window must be
356          * a power of two (due to pci_alloc_consistent requirements).
357          *
358          * Keep 256MB aside for PHBs with ISA.
359          */
360
361         if (!isa_dn) {
362                 /* No ISA/IDE - just set window size and return */
363                 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
364
365                 while (pci->phb->dma_window_size * children > 0x80000000ul)
366                         pci->phb->dma_window_size >>= 1;
367                 DBG("No ISA/IDE, window size is %x\n", pci->phb->dma_window_size);
368                 pci->phb->dma_window_base_cur = 0;
369
370                 return;
371         }
372
373         /* If we have ISA, then we probably have an IDE
374          * controller too. Allocate a 128MB table but
375          * skip the first 128MB to avoid stepping on ISA
376          * space.
377          */
378         pci->phb->dma_window_size = 0x8000000ul;
379         pci->phb->dma_window_base_cur = 0x8000000ul;
380
381         tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
382
383         iommu_table_setparms(pci->phb, dn, tbl);
384         pci->iommu_table = iommu_init_table(tbl);
385
386         /* Divide the rest (1.75GB) among the children */
387         pci->phb->dma_window_size = 0x80000000ul;
388         while (pci->phb->dma_window_size * children > 0x70000000ul)
389                 pci->phb->dma_window_size >>= 1;
390
391         DBG("ISA/IDE, window size is %x\n", pci->phb->dma_window_size);
392
393 }
394
395
396 static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus)
397 {
398         struct iommu_table *tbl;
399         struct device_node *dn, *pdn;
400         struct pci_dn *ppci;
401         unsigned int *dma_window = NULL;
402
403         DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self);
404
405         dn = pci_bus_to_OF_node(bus);
406
407         /* Find nearest ibm,dma-window, walking up the device tree */
408         for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
409                 dma_window = (unsigned int *)get_property(pdn, "ibm,dma-window", NULL);
410                 if (dma_window != NULL)
411                         break;
412         }
413
414         if (dma_window == NULL) {
415                 DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name);
416                 return;
417         }
418
419         ppci = pdn->data;
420         if (!ppci->iommu_table) {
421                 /* Bussubno hasn't been copied yet.
422                  * Do it now because iommu_table_setparms_lpar needs it.
423                  */
424
425                 ppci->bussubno = bus->number;
426
427                 tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
428                                                     GFP_KERNEL);
429         
430                 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
431
432                 ppci->iommu_table = iommu_init_table(tbl);
433         }
434
435         if (pdn != dn)
436                 PCI_DN(dn)->iommu_table = ppci->iommu_table;
437 }
438
439
440 static void iommu_dev_setup_pSeries(struct pci_dev *dev)
441 {
442         struct device_node *dn, *mydn;
443         struct iommu_table *tbl;
444
445         DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, dev->pretty_name);
446
447         mydn = dn = pci_device_to_OF_node(dev);
448
449         /* If we're the direct child of a root bus, then we need to allocate
450          * an iommu table ourselves. The bus setup code should have setup
451          * the window sizes already.
452          */
453         if (!dev->bus->self) {
454                 DBG(" --> first child, no bridge. Allocating iommu table.\n");
455                 tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
456                 iommu_table_setparms(PCI_DN(dn)->phb, dn, tbl);
457                 PCI_DN(mydn)->iommu_table = iommu_init_table(tbl);
458
459                 return;
460         }
461
462         /* If this device is further down the bus tree, search upwards until
463          * an already allocated iommu table is found and use that.
464          */
465
466         while (dn && dn->data && PCI_DN(dn)->iommu_table == NULL)
467                 dn = dn->parent;
468
469         if (dn && dn->data) {
470                 PCI_DN(mydn)->iommu_table = PCI_DN(dn)->iommu_table;
471         } else {
472                 DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, dev->pretty_name);
473         }
474 }
475
476 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
477 {
478         int err = NOTIFY_OK;
479         struct device_node *np = node;
480         struct pci_dn *pci = np->data;
481
482         switch (action) {
483         case PSERIES_RECONFIG_REMOVE:
484                 if (pci->iommu_table &&
485                     get_property(np, "ibm,dma-window", NULL))
486                         iommu_free_table(np);
487                 break;
488         default:
489                 err = NOTIFY_DONE;
490                 break;
491         }
492         return err;
493 }
494
495 static struct notifier_block iommu_reconfig_nb = {
496         .notifier_call = iommu_reconfig_notifier,
497 };
498
499 static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev)
500 {
501         struct device_node *pdn, *dn;
502         struct iommu_table *tbl;
503         int *dma_window = NULL;
504         struct pci_dn *pci;
505
506         DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, dev->pretty_name);
507
508         /* dev setup for LPAR is a little tricky, since the device tree might
509          * contain the dma-window properties per-device and not neccesarily
510          * for the bus. So we need to search upwards in the tree until we
511          * either hit a dma-window property, OR find a parent with a table
512          * already allocated.
513          */
514         dn = pci_device_to_OF_node(dev);
515
516         for (pdn = dn; pdn && pdn->data && !PCI_DN(pdn)->iommu_table;
517              pdn = pdn->parent) {
518                 dma_window = (unsigned int *)
519                         get_property(pdn, "ibm,dma-window", NULL);
520                 if (dma_window)
521                         break;
522         }
523
524         /* Check for parent == NULL so we don't try to setup the empty EADS
525          * slots on POWER4 machines.
526          */
527         if (dma_window == NULL || pdn->parent == NULL) {
528                 DBG("No dma window for device, linking to parent\n");
529                 PCI_DN(dn)->iommu_table = PCI_DN(pdn)->iommu_table;
530                 return;
531         } else {
532                 DBG("Found DMA window, allocating table\n");
533         }
534
535         pci = pdn->data;
536         if (!pci->iommu_table) {
537                 /* iommu_table_setparms_lpar needs bussubno. */
538                 pci->bussubno = pci->phb->bus->number;
539
540                 tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
541                                                     GFP_KERNEL);
542
543                 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
544
545                 pci->iommu_table = iommu_init_table(tbl);
546         }
547
548         if (pdn != dn)
549                 PCI_DN(dn)->iommu_table = pci->iommu_table;
550 }
551
552 static void iommu_bus_setup_null(struct pci_bus *b) { }
553 static void iommu_dev_setup_null(struct pci_dev *d) { }
554
555 /* These are called very early. */
556 void iommu_init_early_pSeries(void)
557 {
558         if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) {
559                 /* Direct I/O, IOMMU off */
560                 ppc_md.iommu_dev_setup = iommu_dev_setup_null;
561                 ppc_md.iommu_bus_setup = iommu_bus_setup_null;
562                 pci_direct_iommu_init();
563
564                 return;
565         }
566
567         if (systemcfg->platform & PLATFORM_LPAR) {
568                 if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
569                         ppc_md.tce_build = tce_buildmulti_pSeriesLP;
570                         ppc_md.tce_free  = tce_freemulti_pSeriesLP;
571                 } else {
572                         ppc_md.tce_build = tce_build_pSeriesLP;
573                         ppc_md.tce_free  = tce_free_pSeriesLP;
574                 }
575                 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP;
576                 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP;
577         } else {
578                 ppc_md.tce_build = tce_build_pSeries;
579                 ppc_md.tce_free  = tce_free_pSeries;
580                 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries;
581                 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries;
582         }
583
584
585         pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
586
587         pci_iommu_init();
588 }
589