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