]> rtime.felk.cvut.cz Git - linux-imx.git/blob - arch/powerpc/kernel/iommu.c
7bc94da1a837229591d180eb04a61d263304cd10
[linux-imx.git] / arch / powerpc / kernel / iommu.c
1 /*
2  * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
3  * 
4  * Rewrite, cleanup, new allocation schemes, virtual merging: 
5  * Copyright (C) 2004 Olof Johansson, IBM Corporation
6  *               and  Ben. Herrenschmidt, IBM Corporation
7  *
8  * Dynamic DMA mapping support, bus-independent parts.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24
25
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/mm.h>
30 #include <linux/spinlock.h>
31 #include <linux/string.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/bitmap.h>
34 #include <linux/iommu-helper.h>
35 #include <linux/crash_dump.h>
36 #include <linux/hash.h>
37 #include <asm/io.h>
38 #include <asm/prom.h>
39 #include <asm/iommu.h>
40 #include <asm/pci-bridge.h>
41 #include <asm/machdep.h>
42 #include <asm/kdump.h>
43 #include <asm/fadump.h>
44
45 #define DBG(...)
46
47 static int novmerge;
48
49 static void __iommu_free(struct iommu_table *, dma_addr_t, unsigned int);
50
51 static int __init setup_iommu(char *str)
52 {
53         if (!strcmp(str, "novmerge"))
54                 novmerge = 1;
55         else if (!strcmp(str, "vmerge"))
56                 novmerge = 0;
57         return 1;
58 }
59
60 __setup("iommu=", setup_iommu);
61
62 static DEFINE_PER_CPU(unsigned int, iommu_pool_hash);
63
64 /*
65  * We precalculate the hash to avoid doing it on every allocation.
66  *
67  * The hash is important to spread CPUs across all the pools. For example,
68  * on a POWER7 with 4 way SMT we want interrupts on the primary threads and
69  * with 4 pools all primary threads would map to the same pool.
70  */
71 static int __init setup_iommu_pool_hash(void)
72 {
73         unsigned int i;
74
75         for_each_possible_cpu(i)
76                 per_cpu(iommu_pool_hash, i) = hash_32(i, IOMMU_POOL_HASHBITS);
77
78         return 0;
79 }
80 subsys_initcall(setup_iommu_pool_hash);
81
82 static unsigned long iommu_range_alloc(struct device *dev,
83                                        struct iommu_table *tbl,
84                                        unsigned long npages,
85                                        unsigned long *handle,
86                                        unsigned long mask,
87                                        unsigned int align_order)
88
89         unsigned long n, end, start;
90         unsigned long limit;
91         int largealloc = npages > 15;
92         int pass = 0;
93         unsigned long align_mask;
94         unsigned long boundary_size;
95         unsigned long flags;
96         unsigned int pool_nr;
97         struct iommu_pool *pool;
98
99         align_mask = 0xffffffffffffffffl >> (64 - align_order);
100
101         /* This allocator was derived from x86_64's bit string search */
102
103         /* Sanity check */
104         if (unlikely(npages == 0)) {
105                 if (printk_ratelimit())
106                         WARN_ON(1);
107                 return DMA_ERROR_CODE;
108         }
109
110         /*
111          * We don't need to disable preemption here because any CPU can
112          * safely use any IOMMU pool.
113          */
114         pool_nr = __raw_get_cpu_var(iommu_pool_hash) & (tbl->nr_pools - 1);
115
116         if (largealloc)
117                 pool = &(tbl->large_pool);
118         else
119                 pool = &(tbl->pools[pool_nr]);
120
121         spin_lock_irqsave(&(pool->lock), flags);
122
123 again:
124         if ((pass == 0) && handle && *handle)
125                 start = *handle;
126         else
127                 start = pool->hint;
128
129         limit = pool->end;
130
131         /* The case below can happen if we have a small segment appended
132          * to a large, or when the previous alloc was at the very end of
133          * the available space. If so, go back to the initial start.
134          */
135         if (start >= limit)
136                 start = pool->start;
137
138         if (limit + tbl->it_offset > mask) {
139                 limit = mask - tbl->it_offset + 1;
140                 /* If we're constrained on address range, first try
141                  * at the masked hint to avoid O(n) search complexity,
142                  * but on second pass, start at 0 in pool 0.
143                  */
144                 if ((start & mask) >= limit || pass > 0) {
145                         pool = &(tbl->pools[0]);
146                         start = pool->start;
147                 } else {
148                         start &= mask;
149                 }
150         }
151
152         if (dev)
153                 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
154                                       1 << IOMMU_PAGE_SHIFT);
155         else
156                 boundary_size = ALIGN(1UL << 32, 1 << IOMMU_PAGE_SHIFT);
157         /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
158
159         n = iommu_area_alloc(tbl->it_map, limit, start, npages,
160                              tbl->it_offset, boundary_size >> IOMMU_PAGE_SHIFT,
161                              align_mask);
162         if (n == -1) {
163                 if (likely(pass == 0)) {
164                         /* First try the pool from the start */
165                         pool->hint = pool->start;
166                         pass++;
167                         goto again;
168
169                 } else if (pass <= tbl->nr_pools) {
170                         /* Now try scanning all the other pools */
171                         spin_unlock(&(pool->lock));
172                         pool_nr = (pool_nr + 1) & (tbl->nr_pools - 1);
173                         pool = &tbl->pools[pool_nr];
174                         spin_lock(&(pool->lock));
175                         pool->hint = pool->start;
176                         pass++;
177                         goto again;
178
179                 } else {
180                         /* Give up */
181                         spin_unlock_irqrestore(&(pool->lock), flags);
182                         return DMA_ERROR_CODE;
183                 }
184         }
185
186         end = n + npages;
187
188         /* Bump the hint to a new block for small allocs. */
189         if (largealloc) {
190                 /* Don't bump to new block to avoid fragmentation */
191                 pool->hint = end;
192         } else {
193                 /* Overflow will be taken care of at the next allocation */
194                 pool->hint = (end + tbl->it_blocksize - 1) &
195                                 ~(tbl->it_blocksize - 1);
196         }
197
198         /* Update handle for SG allocations */
199         if (handle)
200                 *handle = end;
201
202         spin_unlock_irqrestore(&(pool->lock), flags);
203
204         return n;
205 }
206
207 static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
208                               void *page, unsigned int npages,
209                               enum dma_data_direction direction,
210                               unsigned long mask, unsigned int align_order,
211                               struct dma_attrs *attrs)
212 {
213         unsigned long entry;
214         dma_addr_t ret = DMA_ERROR_CODE;
215         int build_fail;
216
217         entry = iommu_range_alloc(dev, tbl, npages, NULL, mask, align_order);
218
219         if (unlikely(entry == DMA_ERROR_CODE))
220                 return DMA_ERROR_CODE;
221
222         entry += tbl->it_offset;        /* Offset into real TCE table */
223         ret = entry << IOMMU_PAGE_SHIFT;        /* Set the return dma address */
224
225         /* Put the TCEs in the HW table */
226         build_fail = ppc_md.tce_build(tbl, entry, npages,
227                                       (unsigned long)page & IOMMU_PAGE_MASK,
228                                       direction, attrs);
229
230         /* ppc_md.tce_build() only returns non-zero for transient errors.
231          * Clean up the table bitmap in this case and return
232          * DMA_ERROR_CODE. For all other errors the functionality is
233          * not altered.
234          */
235         if (unlikely(build_fail)) {
236                 __iommu_free(tbl, ret, npages);
237                 return DMA_ERROR_CODE;
238         }
239
240         /* Flush/invalidate TLB caches if necessary */
241         if (ppc_md.tce_flush)
242                 ppc_md.tce_flush(tbl);
243
244         /* Make sure updates are seen by hardware */
245         mb();
246
247         return ret;
248 }
249
250 static bool iommu_free_check(struct iommu_table *tbl, dma_addr_t dma_addr,
251                              unsigned int npages)
252 {
253         unsigned long entry, free_entry;
254
255         entry = dma_addr >> IOMMU_PAGE_SHIFT;
256         free_entry = entry - tbl->it_offset;
257
258         if (((free_entry + npages) > tbl->it_size) ||
259             (entry < tbl->it_offset)) {
260                 if (printk_ratelimit()) {
261                         printk(KERN_INFO "iommu_free: invalid entry\n");
262                         printk(KERN_INFO "\tentry     = 0x%lx\n", entry); 
263                         printk(KERN_INFO "\tdma_addr  = 0x%llx\n", (u64)dma_addr);
264                         printk(KERN_INFO "\tTable     = 0x%llx\n", (u64)tbl);
265                         printk(KERN_INFO "\tbus#      = 0x%llx\n", (u64)tbl->it_busno);
266                         printk(KERN_INFO "\tsize      = 0x%llx\n", (u64)tbl->it_size);
267                         printk(KERN_INFO "\tstartOff  = 0x%llx\n", (u64)tbl->it_offset);
268                         printk(KERN_INFO "\tindex     = 0x%llx\n", (u64)tbl->it_index);
269                         WARN_ON(1);
270                 }
271
272                 return false;
273         }
274
275         return true;
276 }
277
278 static struct iommu_pool *get_pool(struct iommu_table *tbl,
279                                    unsigned long entry)
280 {
281         struct iommu_pool *p;
282         unsigned long largepool_start = tbl->large_pool.start;
283
284         /* The large pool is the last pool at the top of the table */
285         if (entry >= largepool_start) {
286                 p = &tbl->large_pool;
287         } else {
288                 unsigned int pool_nr = entry / tbl->poolsize;
289
290                 BUG_ON(pool_nr > tbl->nr_pools);
291                 p = &tbl->pools[pool_nr];
292         }
293
294         return p;
295 }
296
297 static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
298                          unsigned int npages)
299 {
300         unsigned long entry, free_entry;
301         unsigned long flags;
302         struct iommu_pool *pool;
303
304         entry = dma_addr >> IOMMU_PAGE_SHIFT;
305         free_entry = entry - tbl->it_offset;
306
307         pool = get_pool(tbl, free_entry);
308
309         if (!iommu_free_check(tbl, dma_addr, npages))
310                 return;
311
312         ppc_md.tce_free(tbl, entry, npages);
313
314         spin_lock_irqsave(&(pool->lock), flags);
315         bitmap_clear(tbl->it_map, free_entry, npages);
316         spin_unlock_irqrestore(&(pool->lock), flags);
317 }
318
319 static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
320                 unsigned int npages)
321 {
322         __iommu_free(tbl, dma_addr, npages);
323
324         /* Make sure TLB cache is flushed if the HW needs it. We do
325          * not do an mb() here on purpose, it is not needed on any of
326          * the current platforms.
327          */
328         if (ppc_md.tce_flush)
329                 ppc_md.tce_flush(tbl);
330 }
331
332 int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
333                  struct scatterlist *sglist, int nelems,
334                  unsigned long mask, enum dma_data_direction direction,
335                  struct dma_attrs *attrs)
336 {
337         dma_addr_t dma_next = 0, dma_addr;
338         struct scatterlist *s, *outs, *segstart;
339         int outcount, incount, i, build_fail = 0;
340         unsigned int align;
341         unsigned long handle;
342         unsigned int max_seg_size;
343
344         BUG_ON(direction == DMA_NONE);
345
346         if ((nelems == 0) || !tbl)
347                 return 0;
348
349         outs = s = segstart = &sglist[0];
350         outcount = 1;
351         incount = nelems;
352         handle = 0;
353
354         /* Init first segment length for backout at failure */
355         outs->dma_length = 0;
356
357         DBG("sg mapping %d elements:\n", nelems);
358
359         max_seg_size = dma_get_max_seg_size(dev);
360         for_each_sg(sglist, s, nelems, i) {
361                 unsigned long vaddr, npages, entry, slen;
362
363                 slen = s->length;
364                 /* Sanity check */
365                 if (slen == 0) {
366                         dma_next = 0;
367                         continue;
368                 }
369                 /* Allocate iommu entries for that segment */
370                 vaddr = (unsigned long) sg_virt(s);
371                 npages = iommu_num_pages(vaddr, slen, IOMMU_PAGE_SIZE);
372                 align = 0;
373                 if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && slen >= PAGE_SIZE &&
374                     (vaddr & ~PAGE_MASK) == 0)
375                         align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
376                 entry = iommu_range_alloc(dev, tbl, npages, &handle,
377                                           mask >> IOMMU_PAGE_SHIFT, align);
378
379                 DBG("  - vaddr: %lx, size: %lx\n", vaddr, slen);
380
381                 /* Handle failure */
382                 if (unlikely(entry == DMA_ERROR_CODE)) {
383                         if (printk_ratelimit())
384                                 dev_info(dev, "iommu_alloc failed, tbl %p "
385                                          "vaddr %lx npages %lu\n", tbl, vaddr,
386                                          npages);
387                         goto failure;
388                 }
389
390                 /* Convert entry to a dma_addr_t */
391                 entry += tbl->it_offset;
392                 dma_addr = entry << IOMMU_PAGE_SHIFT;
393                 dma_addr |= (s->offset & ~IOMMU_PAGE_MASK);
394
395                 DBG("  - %lu pages, entry: %lx, dma_addr: %lx\n",
396                             npages, entry, dma_addr);
397
398                 /* Insert into HW table */
399                 build_fail = ppc_md.tce_build(tbl, entry, npages,
400                                               vaddr & IOMMU_PAGE_MASK,
401                                               direction, attrs);
402                 if(unlikely(build_fail))
403                         goto failure;
404
405                 /* If we are in an open segment, try merging */
406                 if (segstart != s) {
407                         DBG("  - trying merge...\n");
408                         /* We cannot merge if:
409                          * - allocated dma_addr isn't contiguous to previous allocation
410                          */
411                         if (novmerge || (dma_addr != dma_next) ||
412                             (outs->dma_length + s->length > max_seg_size)) {
413                                 /* Can't merge: create a new segment */
414                                 segstart = s;
415                                 outcount++;
416                                 outs = sg_next(outs);
417                                 DBG("    can't merge, new segment.\n");
418                         } else {
419                                 outs->dma_length += s->length;
420                                 DBG("    merged, new len: %ux\n", outs->dma_length);
421                         }
422                 }
423
424                 if (segstart == s) {
425                         /* This is a new segment, fill entries */
426                         DBG("  - filling new segment.\n");
427                         outs->dma_address = dma_addr;
428                         outs->dma_length = slen;
429                 }
430
431                 /* Calculate next page pointer for contiguous check */
432                 dma_next = dma_addr + slen;
433
434                 DBG("  - dma next is: %lx\n", dma_next);
435         }
436
437         /* Flush/invalidate TLB caches if necessary */
438         if (ppc_md.tce_flush)
439                 ppc_md.tce_flush(tbl);
440
441         DBG("mapped %d elements:\n", outcount);
442
443         /* For the sake of iommu_unmap_sg, we clear out the length in the
444          * next entry of the sglist if we didn't fill the list completely
445          */
446         if (outcount < incount) {
447                 outs = sg_next(outs);
448                 outs->dma_address = DMA_ERROR_CODE;
449                 outs->dma_length = 0;
450         }
451
452         /* Make sure updates are seen by hardware */
453         mb();
454
455         return outcount;
456
457  failure:
458         for_each_sg(sglist, s, nelems, i) {
459                 if (s->dma_length != 0) {
460                         unsigned long vaddr, npages;
461
462                         vaddr = s->dma_address & IOMMU_PAGE_MASK;
463                         npages = iommu_num_pages(s->dma_address, s->dma_length,
464                                                  IOMMU_PAGE_SIZE);
465                         __iommu_free(tbl, vaddr, npages);
466                         s->dma_address = DMA_ERROR_CODE;
467                         s->dma_length = 0;
468                 }
469                 if (s == outs)
470                         break;
471         }
472         return 0;
473 }
474
475
476 void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
477                 int nelems, enum dma_data_direction direction,
478                 struct dma_attrs *attrs)
479 {
480         struct scatterlist *sg;
481
482         BUG_ON(direction == DMA_NONE);
483
484         if (!tbl)
485                 return;
486
487         sg = sglist;
488         while (nelems--) {
489                 unsigned int npages;
490                 dma_addr_t dma_handle = sg->dma_address;
491
492                 if (sg->dma_length == 0)
493                         break;
494                 npages = iommu_num_pages(dma_handle, sg->dma_length,
495                                          IOMMU_PAGE_SIZE);
496                 __iommu_free(tbl, dma_handle, npages);
497                 sg = sg_next(sg);
498         }
499
500         /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
501          * do not do an mb() here, the affected platforms do not need it
502          * when freeing.
503          */
504         if (ppc_md.tce_flush)
505                 ppc_md.tce_flush(tbl);
506 }
507
508 static void iommu_table_clear(struct iommu_table *tbl)
509 {
510         /*
511          * In case of firmware assisted dump system goes through clean
512          * reboot process at the time of system crash. Hence it's safe to
513          * clear the TCE entries if firmware assisted dump is active.
514          */
515         if (!is_kdump_kernel() || is_fadump_active()) {
516                 /* Clear the table in case firmware left allocations in it */
517                 ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
518                 return;
519         }
520
521 #ifdef CONFIG_CRASH_DUMP
522         if (ppc_md.tce_get) {
523                 unsigned long index, tceval, tcecount = 0;
524
525                 /* Reserve the existing mappings left by the first kernel. */
526                 for (index = 0; index < tbl->it_size; index++) {
527                         tceval = ppc_md.tce_get(tbl, index + tbl->it_offset);
528                         /*
529                          * Freed TCE entry contains 0x7fffffffffffffff on JS20
530                          */
531                         if (tceval && (tceval != 0x7fffffffffffffffUL)) {
532                                 __set_bit(index, tbl->it_map);
533                                 tcecount++;
534                         }
535                 }
536
537                 if ((tbl->it_size - tcecount) < KDUMP_MIN_TCE_ENTRIES) {
538                         printk(KERN_WARNING "TCE table is full; freeing ");
539                         printk(KERN_WARNING "%d entries for the kdump boot\n",
540                                 KDUMP_MIN_TCE_ENTRIES);
541                         for (index = tbl->it_size - KDUMP_MIN_TCE_ENTRIES;
542                                 index < tbl->it_size; index++)
543                                 __clear_bit(index, tbl->it_map);
544                 }
545         }
546 #endif
547 }
548
549 /*
550  * Build a iommu_table structure.  This contains a bit map which
551  * is used to manage allocation of the tce space.
552  */
553 struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
554 {
555         unsigned long sz;
556         static int welcomed = 0;
557         struct page *page;
558         unsigned int i;
559         struct iommu_pool *p;
560
561         /* number of bytes needed for the bitmap */
562         sz = (tbl->it_size + 7) >> 3;
563
564         page = alloc_pages_node(nid, GFP_ATOMIC, get_order(sz));
565         if (!page)
566                 panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
567         tbl->it_map = page_address(page);
568         memset(tbl->it_map, 0, sz);
569
570         /*
571          * Reserve page 0 so it will not be used for any mappings.
572          * This avoids buggy drivers that consider page 0 to be invalid
573          * to crash the machine or even lose data.
574          */
575         if (tbl->it_offset == 0)
576                 set_bit(0, tbl->it_map);
577
578         /* We only split the IOMMU table if we have 1GB or more of space */
579         if ((tbl->it_size << IOMMU_PAGE_SHIFT) >= (1UL * 1024 * 1024 * 1024))
580                 tbl->nr_pools = IOMMU_NR_POOLS;
581         else
582                 tbl->nr_pools = 1;
583
584         /* We reserve the top 1/4 of the table for large allocations */
585         tbl->poolsize = (tbl->it_size * 3 / 4) / IOMMU_NR_POOLS;
586
587         for (i = 0; i < IOMMU_NR_POOLS; i++) {
588                 p = &tbl->pools[i];
589                 spin_lock_init(&(p->lock));
590                 p->start = tbl->poolsize * i;
591                 p->hint = p->start;
592                 p->end = p->start + tbl->poolsize;
593         }
594
595         p = &tbl->large_pool;
596         spin_lock_init(&(p->lock));
597         p->start = tbl->poolsize * i;
598         p->hint = p->start;
599         p->end = tbl->it_size;
600
601         iommu_table_clear(tbl);
602
603         if (!welcomed) {
604                 printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",
605                        novmerge ? "disabled" : "enabled");
606                 welcomed = 1;
607         }
608
609         return tbl;
610 }
611
612 void iommu_free_table(struct iommu_table *tbl, const char *node_name)
613 {
614         unsigned long bitmap_sz, i;
615         unsigned int order;
616
617         if (!tbl || !tbl->it_map) {
618                 printk(KERN_ERR "%s: expected TCE map for %s\n", __func__,
619                                 node_name);
620                 return;
621         }
622
623         /* verify that table contains no entries */
624         /* it_size is in entries, and we're examining 64 at a time */
625         for (i = 0; i < (tbl->it_size/64); i++) {
626                 if (tbl->it_map[i] != 0) {
627                         printk(KERN_WARNING "%s: Unexpected TCEs for %s\n",
628                                 __func__, node_name);
629                         break;
630                 }
631         }
632
633         /* calculate bitmap size in bytes */
634         bitmap_sz = (tbl->it_size + 7) / 8;
635
636         /* free bitmap */
637         order = get_order(bitmap_sz);
638         free_pages((unsigned long) tbl->it_map, order);
639
640         /* free table */
641         kfree(tbl);
642 }
643
644 /* Creates TCEs for a user provided buffer.  The user buffer must be
645  * contiguous real kernel storage (not vmalloc).  The address passed here
646  * comprises a page address and offset into that page. The dma_addr_t
647  * returned will point to the same byte within the page as was passed in.
648  */
649 dma_addr_t iommu_map_page(struct device *dev, struct iommu_table *tbl,
650                           struct page *page, unsigned long offset, size_t size,
651                           unsigned long mask, enum dma_data_direction direction,
652                           struct dma_attrs *attrs)
653 {
654         dma_addr_t dma_handle = DMA_ERROR_CODE;
655         void *vaddr;
656         unsigned long uaddr;
657         unsigned int npages, align;
658
659         BUG_ON(direction == DMA_NONE);
660
661         vaddr = page_address(page) + offset;
662         uaddr = (unsigned long)vaddr;
663         npages = iommu_num_pages(uaddr, size, IOMMU_PAGE_SIZE);
664
665         if (tbl) {
666                 align = 0;
667                 if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && size >= PAGE_SIZE &&
668                     ((unsigned long)vaddr & ~PAGE_MASK) == 0)
669                         align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
670
671                 dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction,
672                                          mask >> IOMMU_PAGE_SHIFT, align,
673                                          attrs);
674                 if (dma_handle == DMA_ERROR_CODE) {
675                         if (printk_ratelimit())  {
676                                 dev_info(dev, "iommu_alloc failed, tbl %p "
677                                          "vaddr %p npages %d\n", tbl, vaddr,
678                                          npages);
679                         }
680                 } else
681                         dma_handle |= (uaddr & ~IOMMU_PAGE_MASK);
682         }
683
684         return dma_handle;
685 }
686
687 void iommu_unmap_page(struct iommu_table *tbl, dma_addr_t dma_handle,
688                       size_t size, enum dma_data_direction direction,
689                       struct dma_attrs *attrs)
690 {
691         unsigned int npages;
692
693         BUG_ON(direction == DMA_NONE);
694
695         if (tbl) {
696                 npages = iommu_num_pages(dma_handle, size, IOMMU_PAGE_SIZE);
697                 iommu_free(tbl, dma_handle, npages);
698         }
699 }
700
701 /* Allocates a contiguous real buffer and creates mappings over it.
702  * Returns the virtual address of the buffer and sets dma_handle
703  * to the dma address (mapping) of the first page.
704  */
705 void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,
706                            size_t size, dma_addr_t *dma_handle,
707                            unsigned long mask, gfp_t flag, int node)
708 {
709         void *ret = NULL;
710         dma_addr_t mapping;
711         unsigned int order;
712         unsigned int nio_pages, io_order;
713         struct page *page;
714
715         size = PAGE_ALIGN(size);
716         order = get_order(size);
717
718         /*
719          * Client asked for way too much space.  This is checked later
720          * anyway.  It is easier to debug here for the drivers than in
721          * the tce tables.
722          */
723         if (order >= IOMAP_MAX_ORDER) {
724                 dev_info(dev, "iommu_alloc_consistent size too large: 0x%lx\n",
725                          size);
726                 return NULL;
727         }
728
729         if (!tbl)
730                 return NULL;
731
732         /* Alloc enough pages (and possibly more) */
733         page = alloc_pages_node(node, flag, order);
734         if (!page)
735                 return NULL;
736         ret = page_address(page);
737         memset(ret, 0, size);
738
739         /* Set up tces to cover the allocated range */
740         nio_pages = size >> IOMMU_PAGE_SHIFT;
741         io_order = get_iommu_order(size);
742         mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL,
743                               mask >> IOMMU_PAGE_SHIFT, io_order, NULL);
744         if (mapping == DMA_ERROR_CODE) {
745                 free_pages((unsigned long)ret, order);
746                 return NULL;
747         }
748         *dma_handle = mapping;
749         return ret;
750 }
751
752 void iommu_free_coherent(struct iommu_table *tbl, size_t size,
753                          void *vaddr, dma_addr_t dma_handle)
754 {
755         if (tbl) {
756                 unsigned int nio_pages;
757
758                 size = PAGE_ALIGN(size);
759                 nio_pages = size >> IOMMU_PAGE_SHIFT;
760                 iommu_free(tbl, dma_handle, nio_pages);
761                 size = PAGE_ALIGN(size);
762                 free_pages((unsigned long)vaddr, get_order(size));
763         }
764 }