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