2 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
4 * Rewrite, cleanup, new allocation schemes, virtual merging:
5 * Copyright (C) 2004 Olof Johansson, IBM Corporation
6 * and Ben. Herrenschmidt, IBM Corporation
8 * Dynamic DMA mapping support, bus-independent parts.
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.
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.
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
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/slab.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>
38 #include <asm/iommu.h>
39 #include <asm/pci-bridge.h>
40 #include <asm/machdep.h>
41 #include <asm/kdump.h>
46 static int protect4gb
= 1;
48 static void __iommu_free(struct iommu_table
*, dma_addr_t
, unsigned int);
50 static int __init
setup_protect4gb(char *str
)
52 if (strcmp(str
, "on") == 0)
54 else if (strcmp(str
, "off") == 0)
60 static int __init
setup_iommu(char *str
)
62 if (!strcmp(str
, "novmerge"))
64 else if (!strcmp(str
, "vmerge"))
69 __setup("protect4gb=", setup_protect4gb
);
70 __setup("iommu=", setup_iommu
);
72 static unsigned long iommu_range_alloc(struct device
*dev
,
73 struct iommu_table
*tbl
,
75 unsigned long *handle
,
77 unsigned int align_order
)
79 unsigned long n
, end
, start
;
81 int largealloc
= npages
> 15;
83 unsigned long align_mask
;
84 unsigned long boundary_size
;
86 align_mask
= 0xffffffffffffffffl
>> (64 - align_order
);
88 /* This allocator was derived from x86_64's bit string search */
91 if (unlikely(npages
== 0)) {
92 if (printk_ratelimit())
94 return DMA_ERROR_CODE
;
97 if (handle
&& *handle
)
100 start
= largealloc
? tbl
->it_largehint
: tbl
->it_hint
;
102 /* Use only half of the table for small allocs (15 pages or less) */
103 limit
= largealloc
? tbl
->it_size
: tbl
->it_halfpoint
;
105 if (largealloc
&& start
< tbl
->it_halfpoint
)
106 start
= tbl
->it_halfpoint
;
108 /* The case below can happen if we have a small segment appended
109 * to a large, or when the previous alloc was at the very end of
110 * the available space. If so, go back to the initial start.
113 start
= largealloc
? tbl
->it_largehint
: tbl
->it_hint
;
117 if (limit
+ tbl
->it_offset
> mask
) {
118 limit
= mask
- tbl
->it_offset
+ 1;
119 /* If we're constrained on address range, first try
120 * at the masked hint to avoid O(n) search complexity,
121 * but on second pass, start at 0.
123 if ((start
& mask
) >= limit
|| pass
> 0)
130 boundary_size
= ALIGN(dma_get_seg_boundary(dev
) + 1,
131 1 << IOMMU_PAGE_SHIFT
);
133 boundary_size
= ALIGN(1UL << 32, 1 << IOMMU_PAGE_SHIFT
);
134 /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
136 n
= iommu_area_alloc(tbl
->it_map
, limit
, start
, npages
,
137 tbl
->it_offset
, boundary_size
>> IOMMU_PAGE_SHIFT
,
140 if (likely(pass
< 2)) {
141 /* First failure, just rescan the half of the table.
142 * Second failure, rescan the other half of the table.
144 start
= (largealloc
^ pass
) ? tbl
->it_halfpoint
: 0;
145 limit
= pass
? tbl
->it_size
: limit
;
149 /* Third failure, give up */
150 return DMA_ERROR_CODE
;
156 /* Bump the hint to a new block for small allocs. */
158 /* Don't bump to new block to avoid fragmentation */
159 tbl
->it_largehint
= end
;
161 /* Overflow will be taken care of at the next allocation */
162 tbl
->it_hint
= (end
+ tbl
->it_blocksize
- 1) &
163 ~(tbl
->it_blocksize
- 1);
166 /* Update handle for SG allocations */
173 static dma_addr_t
iommu_alloc(struct device
*dev
, struct iommu_table
*tbl
,
174 void *page
, unsigned int npages
,
175 enum dma_data_direction direction
,
176 unsigned long mask
, unsigned int align_order
,
177 struct dma_attrs
*attrs
)
179 unsigned long entry
, flags
;
180 dma_addr_t ret
= DMA_ERROR_CODE
;
183 spin_lock_irqsave(&(tbl
->it_lock
), flags
);
185 entry
= iommu_range_alloc(dev
, tbl
, npages
, NULL
, mask
, align_order
);
187 if (unlikely(entry
== DMA_ERROR_CODE
)) {
188 spin_unlock_irqrestore(&(tbl
->it_lock
), flags
);
189 return DMA_ERROR_CODE
;
192 entry
+= tbl
->it_offset
; /* Offset into real TCE table */
193 ret
= entry
<< IOMMU_PAGE_SHIFT
; /* Set the return dma address */
195 /* Put the TCEs in the HW table */
196 build_fail
= ppc_md
.tce_build(tbl
, entry
, npages
,
197 (unsigned long)page
& IOMMU_PAGE_MASK
,
200 /* ppc_md.tce_build() only returns non-zero for transient errors.
201 * Clean up the table bitmap in this case and return
202 * DMA_ERROR_CODE. For all other errors the functionality is
205 if (unlikely(build_fail
)) {
206 __iommu_free(tbl
, ret
, npages
);
208 spin_unlock_irqrestore(&(tbl
->it_lock
), flags
);
209 return DMA_ERROR_CODE
;
212 /* Flush/invalidate TLB caches if necessary */
213 if (ppc_md
.tce_flush
)
214 ppc_md
.tce_flush(tbl
);
216 spin_unlock_irqrestore(&(tbl
->it_lock
), flags
);
218 /* Make sure updates are seen by hardware */
224 static void __iommu_free(struct iommu_table
*tbl
, dma_addr_t dma_addr
,
227 unsigned long entry
, free_entry
;
229 entry
= dma_addr
>> IOMMU_PAGE_SHIFT
;
230 free_entry
= entry
- tbl
->it_offset
;
232 if (((free_entry
+ npages
) > tbl
->it_size
) ||
233 (entry
< tbl
->it_offset
)) {
234 if (printk_ratelimit()) {
235 printk(KERN_INFO
"iommu_free: invalid entry\n");
236 printk(KERN_INFO
"\tentry = 0x%lx\n", entry
);
237 printk(KERN_INFO
"\tdma_addr = 0x%llx\n", (u64
)dma_addr
);
238 printk(KERN_INFO
"\tTable = 0x%llx\n", (u64
)tbl
);
239 printk(KERN_INFO
"\tbus# = 0x%llx\n", (u64
)tbl
->it_busno
);
240 printk(KERN_INFO
"\tsize = 0x%llx\n", (u64
)tbl
->it_size
);
241 printk(KERN_INFO
"\tstartOff = 0x%llx\n", (u64
)tbl
->it_offset
);
242 printk(KERN_INFO
"\tindex = 0x%llx\n", (u64
)tbl
->it_index
);
248 ppc_md
.tce_free(tbl
, entry
, npages
);
249 bitmap_clear(tbl
->it_map
, free_entry
, npages
);
252 static void iommu_free(struct iommu_table
*tbl
, dma_addr_t dma_addr
,
257 spin_lock_irqsave(&(tbl
->it_lock
), flags
);
259 __iommu_free(tbl
, dma_addr
, npages
);
261 /* Make sure TLB cache is flushed if the HW needs it. We do
262 * not do an mb() here on purpose, it is not needed on any of
263 * the current platforms.
265 if (ppc_md
.tce_flush
)
266 ppc_md
.tce_flush(tbl
);
268 spin_unlock_irqrestore(&(tbl
->it_lock
), flags
);
271 int iommu_map_sg(struct device
*dev
, struct iommu_table
*tbl
,
272 struct scatterlist
*sglist
, int nelems
,
273 unsigned long mask
, enum dma_data_direction direction
,
274 struct dma_attrs
*attrs
)
276 dma_addr_t dma_next
= 0, dma_addr
;
278 struct scatterlist
*s
, *outs
, *segstart
;
279 int outcount
, incount
, i
, build_fail
= 0;
281 unsigned long handle
;
282 unsigned int max_seg_size
;
284 BUG_ON(direction
== DMA_NONE
);
286 if ((nelems
== 0) || !tbl
)
289 outs
= s
= segstart
= &sglist
[0];
294 /* Init first segment length for backout at failure */
295 outs
->dma_length
= 0;
297 DBG("sg mapping %d elements:\n", nelems
);
299 spin_lock_irqsave(&(tbl
->it_lock
), flags
);
301 max_seg_size
= dma_get_max_seg_size(dev
);
302 for_each_sg(sglist
, s
, nelems
, i
) {
303 unsigned long vaddr
, npages
, entry
, slen
;
311 /* Allocate iommu entries for that segment */
312 vaddr
= (unsigned long) sg_virt(s
);
313 npages
= iommu_num_pages(vaddr
, slen
, IOMMU_PAGE_SIZE
);
315 if (IOMMU_PAGE_SHIFT
< PAGE_SHIFT
&& slen
>= PAGE_SIZE
&&
316 (vaddr
& ~PAGE_MASK
) == 0)
317 align
= PAGE_SHIFT
- IOMMU_PAGE_SHIFT
;
318 entry
= iommu_range_alloc(dev
, tbl
, npages
, &handle
,
319 mask
>> IOMMU_PAGE_SHIFT
, align
);
321 DBG(" - vaddr: %lx, size: %lx\n", vaddr
, slen
);
324 if (unlikely(entry
== DMA_ERROR_CODE
)) {
325 if (printk_ratelimit())
326 printk(KERN_INFO
"iommu_alloc failed, tbl %p vaddr %lx"
327 " npages %lx\n", tbl
, vaddr
, npages
);
331 /* Convert entry to a dma_addr_t */
332 entry
+= tbl
->it_offset
;
333 dma_addr
= entry
<< IOMMU_PAGE_SHIFT
;
334 dma_addr
|= (s
->offset
& ~IOMMU_PAGE_MASK
);
336 DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n",
337 npages
, entry
, dma_addr
);
339 /* Insert into HW table */
340 build_fail
= ppc_md
.tce_build(tbl
, entry
, npages
,
341 vaddr
& IOMMU_PAGE_MASK
,
343 if(unlikely(build_fail
))
346 /* If we are in an open segment, try merging */
348 DBG(" - trying merge...\n");
349 /* We cannot merge if:
350 * - allocated dma_addr isn't contiguous to previous allocation
352 if (novmerge
|| (dma_addr
!= dma_next
) ||
353 (outs
->dma_length
+ s
->length
> max_seg_size
)) {
354 /* Can't merge: create a new segment */
357 outs
= sg_next(outs
);
358 DBG(" can't merge, new segment.\n");
360 outs
->dma_length
+= s
->length
;
361 DBG(" merged, new len: %ux\n", outs
->dma_length
);
366 /* This is a new segment, fill entries */
367 DBG(" - filling new segment.\n");
368 outs
->dma_address
= dma_addr
;
369 outs
->dma_length
= slen
;
372 /* Calculate next page pointer for contiguous check */
373 dma_next
= dma_addr
+ slen
;
375 DBG(" - dma next is: %lx\n", dma_next
);
378 /* Flush/invalidate TLB caches if necessary */
379 if (ppc_md
.tce_flush
)
380 ppc_md
.tce_flush(tbl
);
382 spin_unlock_irqrestore(&(tbl
->it_lock
), flags
);
384 DBG("mapped %d elements:\n", outcount
);
386 /* For the sake of iommu_unmap_sg, we clear out the length in the
387 * next entry of the sglist if we didn't fill the list completely
389 if (outcount
< incount
) {
390 outs
= sg_next(outs
);
391 outs
->dma_address
= DMA_ERROR_CODE
;
392 outs
->dma_length
= 0;
395 /* Make sure updates are seen by hardware */
401 for_each_sg(sglist
, s
, nelems
, i
) {
402 if (s
->dma_length
!= 0) {
403 unsigned long vaddr
, npages
;
405 vaddr
= s
->dma_address
& IOMMU_PAGE_MASK
;
406 npages
= iommu_num_pages(s
->dma_address
, s
->dma_length
,
408 __iommu_free(tbl
, vaddr
, npages
);
409 s
->dma_address
= DMA_ERROR_CODE
;
415 spin_unlock_irqrestore(&(tbl
->it_lock
), flags
);
420 void iommu_unmap_sg(struct iommu_table
*tbl
, struct scatterlist
*sglist
,
421 int nelems
, enum dma_data_direction direction
,
422 struct dma_attrs
*attrs
)
424 struct scatterlist
*sg
;
427 BUG_ON(direction
== DMA_NONE
);
432 spin_lock_irqsave(&(tbl
->it_lock
), flags
);
437 dma_addr_t dma_handle
= sg
->dma_address
;
439 if (sg
->dma_length
== 0)
441 npages
= iommu_num_pages(dma_handle
, sg
->dma_length
,
443 __iommu_free(tbl
, dma_handle
, npages
);
447 /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
448 * do not do an mb() here, the affected platforms do not need it
451 if (ppc_md
.tce_flush
)
452 ppc_md
.tce_flush(tbl
);
454 spin_unlock_irqrestore(&(tbl
->it_lock
), flags
);
457 static void iommu_table_clear(struct iommu_table
*tbl
)
459 if (!is_kdump_kernel()) {
460 /* Clear the table in case firmware left allocations in it */
461 ppc_md
.tce_free(tbl
, tbl
->it_offset
, tbl
->it_size
);
465 #ifdef CONFIG_CRASH_DUMP
466 if (ppc_md
.tce_get
) {
467 unsigned long index
, tceval
, tcecount
= 0;
469 /* Reserve the existing mappings left by the first kernel. */
470 for (index
= 0; index
< tbl
->it_size
; index
++) {
471 tceval
= ppc_md
.tce_get(tbl
, index
+ tbl
->it_offset
);
473 * Freed TCE entry contains 0x7fffffffffffffff on JS20
475 if (tceval
&& (tceval
!= 0x7fffffffffffffffUL
)) {
476 __set_bit(index
, tbl
->it_map
);
481 if ((tbl
->it_size
- tcecount
) < KDUMP_MIN_TCE_ENTRIES
) {
482 printk(KERN_WARNING
"TCE table is full; freeing ");
483 printk(KERN_WARNING
"%d entries for the kdump boot\n",
484 KDUMP_MIN_TCE_ENTRIES
);
485 for (index
= tbl
->it_size
- KDUMP_MIN_TCE_ENTRIES
;
486 index
< tbl
->it_size
; index
++)
487 __clear_bit(index
, tbl
->it_map
);
494 * Build a iommu_table structure. This contains a bit map which
495 * is used to manage allocation of the tce space.
497 struct iommu_table
*iommu_init_table(struct iommu_table
*tbl
, int nid
)
500 static int welcomed
= 0;
503 /* Set aside 1/4 of the table for large allocations. */
504 tbl
->it_halfpoint
= tbl
->it_size
* 3 / 4;
506 /* number of bytes needed for the bitmap */
507 sz
= (tbl
->it_size
+ 7) >> 3;
509 page
= alloc_pages_node(nid
, GFP_ATOMIC
, get_order(sz
));
511 panic("iommu_init_table: Can't allocate %ld bytes\n", sz
);
512 tbl
->it_map
= page_address(page
);
513 memset(tbl
->it_map
, 0, sz
);
516 tbl
->it_largehint
= tbl
->it_halfpoint
;
517 spin_lock_init(&tbl
->it_lock
);
519 iommu_table_clear(tbl
);
522 printk(KERN_INFO
"IOMMU table initialized, virtual merging %s\n",
523 novmerge
? "disabled" : "enabled");
530 void iommu_free_table(struct iommu_table
*tbl
, const char *node_name
)
532 unsigned long bitmap_sz
, i
;
535 if (!tbl
|| !tbl
->it_map
) {
536 printk(KERN_ERR
"%s: expected TCE map for %s\n", __func__
,
541 /* verify that table contains no entries */
542 /* it_size is in entries, and we're examining 64 at a time */
543 for (i
= 0; i
< (tbl
->it_size
/64); i
++) {
544 if (tbl
->it_map
[i
] != 0) {
545 printk(KERN_WARNING
"%s: Unexpected TCEs for %s\n",
546 __func__
, node_name
);
551 /* calculate bitmap size in bytes */
552 bitmap_sz
= (tbl
->it_size
+ 7) / 8;
555 order
= get_order(bitmap_sz
);
556 free_pages((unsigned long) tbl
->it_map
, order
);
562 /* Creates TCEs for a user provided buffer. The user buffer must be
563 * contiguous real kernel storage (not vmalloc). The address passed here
564 * comprises a page address and offset into that page. The dma_addr_t
565 * returned will point to the same byte within the page as was passed in.
567 dma_addr_t
iommu_map_page(struct device
*dev
, struct iommu_table
*tbl
,
568 struct page
*page
, unsigned long offset
, size_t size
,
569 unsigned long mask
, enum dma_data_direction direction
,
570 struct dma_attrs
*attrs
)
572 dma_addr_t dma_handle
= DMA_ERROR_CODE
;
575 unsigned int npages
, align
;
577 BUG_ON(direction
== DMA_NONE
);
579 vaddr
= page_address(page
) + offset
;
580 uaddr
= (unsigned long)vaddr
;
581 npages
= iommu_num_pages(uaddr
, size
, IOMMU_PAGE_SIZE
);
585 if (IOMMU_PAGE_SHIFT
< PAGE_SHIFT
&& size
>= PAGE_SIZE
&&
586 ((unsigned long)vaddr
& ~PAGE_MASK
) == 0)
587 align
= PAGE_SHIFT
- IOMMU_PAGE_SHIFT
;
589 dma_handle
= iommu_alloc(dev
, tbl
, vaddr
, npages
, direction
,
590 mask
>> IOMMU_PAGE_SHIFT
, align
,
592 if (dma_handle
== DMA_ERROR_CODE
) {
593 if (printk_ratelimit()) {
594 printk(KERN_INFO
"iommu_alloc failed, "
595 "tbl %p vaddr %p npages %d\n",
599 dma_handle
|= (uaddr
& ~IOMMU_PAGE_MASK
);
605 void iommu_unmap_page(struct iommu_table
*tbl
, dma_addr_t dma_handle
,
606 size_t size
, enum dma_data_direction direction
,
607 struct dma_attrs
*attrs
)
611 BUG_ON(direction
== DMA_NONE
);
614 npages
= iommu_num_pages(dma_handle
, size
, IOMMU_PAGE_SIZE
);
615 iommu_free(tbl
, dma_handle
, npages
);
619 /* Allocates a contiguous real buffer and creates mappings over it.
620 * Returns the virtual address of the buffer and sets dma_handle
621 * to the dma address (mapping) of the first page.
623 void *iommu_alloc_coherent(struct device
*dev
, struct iommu_table
*tbl
,
624 size_t size
, dma_addr_t
*dma_handle
,
625 unsigned long mask
, gfp_t flag
, int node
)
630 unsigned int nio_pages
, io_order
;
633 size
= PAGE_ALIGN(size
);
634 order
= get_order(size
);
637 * Client asked for way too much space. This is checked later
638 * anyway. It is easier to debug here for the drivers than in
641 if (order
>= IOMAP_MAX_ORDER
) {
642 printk("iommu_alloc_consistent size too large: 0x%lx\n", size
);
649 /* Alloc enough pages (and possibly more) */
650 page
= alloc_pages_node(node
, flag
, order
);
653 ret
= page_address(page
);
654 memset(ret
, 0, size
);
656 /* Set up tces to cover the allocated range */
657 nio_pages
= size
>> IOMMU_PAGE_SHIFT
;
658 io_order
= get_iommu_order(size
);
659 mapping
= iommu_alloc(dev
, tbl
, ret
, nio_pages
, DMA_BIDIRECTIONAL
,
660 mask
>> IOMMU_PAGE_SHIFT
, io_order
, NULL
);
661 if (mapping
== DMA_ERROR_CODE
) {
662 free_pages((unsigned long)ret
, order
);
665 *dma_handle
= mapping
;
669 void iommu_free_coherent(struct iommu_table
*tbl
, size_t size
,
670 void *vaddr
, dma_addr_t dma_handle
)
673 unsigned int nio_pages
;
675 size
= PAGE_ALIGN(size
);
676 nio_pages
= size
>> IOMMU_PAGE_SHIFT
;
677 iommu_free(tbl
, dma_handle
, nio_pages
);
678 size
= PAGE_ALIGN(size
);
679 free_pages((unsigned long)vaddr
, get_order(size
));