2 * arch/ppc64/kernel/iommu.c
3 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
5 * Rewrite, cleanup, new allocation schemes, virtual merging:
6 * Copyright (C) 2004 Olof Johansson, IBM Corporation
7 * and Ben. Herrenschmidt, IBM Corporation
9 * Dynamic DMA mapping support, bus-independent parts.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/config.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/string.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/init.h>
36 #include <linux/bitops.h>
39 #include <asm/iommu.h>
40 #include <asm/pci-bridge.h>
41 #include <asm/machdep.h>
45 #ifdef CONFIG_IOMMU_VMERGE
46 static int novmerge = 0;
48 static int novmerge = 1;
51 static int __init setup_iommu(char *str)
53 if (!strcmp(str, "novmerge"))
55 else if (!strcmp(str, "vmerge"))
60 __setup("iommu=", setup_iommu);
62 static unsigned long iommu_range_alloc(struct iommu_table *tbl,
64 unsigned long *handle,
65 unsigned int align_order)
67 unsigned long n, end, i, start;
69 int largealloc = npages > 15;
71 unsigned long align_mask;
73 align_mask = 0xffffffffffffffffl >> (64 - align_order);
75 /* This allocator was derived from x86_64's bit string search */
78 if (unlikely(npages) == 0) {
79 if (printk_ratelimit())
81 return DMA_ERROR_CODE;
84 if (handle && *handle)
87 start = largealloc ? tbl->it_largehint : tbl->it_hint;
89 /* Use only half of the table for small allocs (15 pages or less) */
90 limit = largealloc ? tbl->it_size : tbl->it_halfpoint;
92 if (largealloc && start < tbl->it_halfpoint)
93 start = tbl->it_halfpoint;
95 /* The case below can happen if we have a small segment appended
96 * to a large, or when the previous alloc was at the very end of
97 * the available space. If so, go back to the initial start.
100 start = largealloc ? tbl->it_largehint : tbl->it_hint;
104 n = find_next_zero_bit(tbl->it_map, limit, start);
106 /* Align allocation */
107 n = (n + align_mask) & ~align_mask;
111 if (unlikely(end >= limit)) {
112 if (likely(pass < 2)) {
113 /* First failure, just rescan the half of the table.
114 * Second failure, rescan the other half of the table.
116 start = (largealloc ^ pass) ? tbl->it_halfpoint : 0;
117 limit = pass ? tbl->it_size : limit;
121 /* Third failure, give up */
122 return DMA_ERROR_CODE;
126 for (i = n; i < end; i++)
127 if (test_bit(i, tbl->it_map)) {
132 for (i = n; i < end; i++)
133 __set_bit(i, tbl->it_map);
135 /* Bump the hint to a new block for small allocs. */
137 /* Don't bump to new block to avoid fragmentation */
138 tbl->it_largehint = end;
140 /* Overflow will be taken care of at the next allocation */
141 tbl->it_hint = (end + tbl->it_blocksize - 1) &
142 ~(tbl->it_blocksize - 1);
145 /* Update handle for SG allocations */
152 static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *page,
153 unsigned int npages, enum dma_data_direction direction,
154 unsigned int align_order)
156 unsigned long entry, flags;
157 dma_addr_t ret = DMA_ERROR_CODE;
159 spin_lock_irqsave(&(tbl->it_lock), flags);
161 entry = iommu_range_alloc(tbl, npages, NULL, align_order);
163 if (unlikely(entry == DMA_ERROR_CODE)) {
164 spin_unlock_irqrestore(&(tbl->it_lock), flags);
165 return DMA_ERROR_CODE;
168 entry += tbl->it_offset; /* Offset into real TCE table */
169 ret = entry << PAGE_SHIFT; /* Set the return dma address */
171 /* Put the TCEs in the HW table */
172 ppc_md.tce_build(tbl, entry, npages, (unsigned long)page & PAGE_MASK,
176 /* Flush/invalidate TLB caches if necessary */
177 if (ppc_md.tce_flush)
178 ppc_md.tce_flush(tbl);
180 spin_unlock_irqrestore(&(tbl->it_lock), flags);
182 /* Make sure updates are seen by hardware */
188 static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
191 unsigned long entry, free_entry;
194 entry = dma_addr >> PAGE_SHIFT;
195 free_entry = entry - tbl->it_offset;
197 if (((free_entry + npages) > tbl->it_size) ||
198 (entry < tbl->it_offset)) {
199 if (printk_ratelimit()) {
200 printk(KERN_INFO "iommu_free: invalid entry\n");
201 printk(KERN_INFO "\tentry = 0x%lx\n", entry);
202 printk(KERN_INFO "\tdma_addr = 0x%lx\n", (u64)dma_addr);
203 printk(KERN_INFO "\tTable = 0x%lx\n", (u64)tbl);
204 printk(KERN_INFO "\tbus# = 0x%lx\n", (u64)tbl->it_busno);
205 printk(KERN_INFO "\tsize = 0x%lx\n", (u64)tbl->it_size);
206 printk(KERN_INFO "\tstartOff = 0x%lx\n", (u64)tbl->it_offset);
207 printk(KERN_INFO "\tindex = 0x%lx\n", (u64)tbl->it_index);
213 ppc_md.tce_free(tbl, entry, npages);
215 for (i = 0; i < npages; i++)
216 __clear_bit(free_entry+i, tbl->it_map);
219 static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
224 spin_lock_irqsave(&(tbl->it_lock), flags);
226 __iommu_free(tbl, dma_addr, npages);
228 /* Make sure TLB cache is flushed if the HW needs it. We do
229 * not do an mb() here on purpose, it is not needed on any of
230 * the current platforms.
232 if (ppc_md.tce_flush)
233 ppc_md.tce_flush(tbl);
235 spin_unlock_irqrestore(&(tbl->it_lock), flags);
238 int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
239 struct scatterlist *sglist, int nelems,
240 enum dma_data_direction direction)
242 dma_addr_t dma_next = 0, dma_addr;
244 struct scatterlist *s, *outs, *segstart;
245 int outcount, incount;
246 unsigned long handle;
248 BUG_ON(direction == DMA_NONE);
250 if ((nelems == 0) || !tbl)
253 outs = s = segstart = &sglist[0];
258 /* Init first segment length for backout at failure */
259 outs->dma_length = 0;
261 DBG("mapping %d elements:\n", nelems);
263 spin_lock_irqsave(&(tbl->it_lock), flags);
265 for (s = outs; nelems; nelems--, s++) {
266 unsigned long vaddr, npages, entry, slen;
274 /* Allocate iommu entries for that segment */
275 vaddr = (unsigned long)page_address(s->page) + s->offset;
276 npages = PAGE_ALIGN(vaddr + slen) - (vaddr & PAGE_MASK);
277 npages >>= PAGE_SHIFT;
278 entry = iommu_range_alloc(tbl, npages, &handle, 0);
280 DBG(" - vaddr: %lx, size: %lx\n", vaddr, slen);
283 if (unlikely(entry == DMA_ERROR_CODE)) {
284 if (printk_ratelimit())
285 printk(KERN_INFO "iommu_alloc failed, tbl %p vaddr %lx"
286 " npages %lx\n", tbl, vaddr, npages);
290 /* Convert entry to a dma_addr_t */
291 entry += tbl->it_offset;
292 dma_addr = entry << PAGE_SHIFT;
293 dma_addr |= s->offset;
295 DBG(" - %lx pages, entry: %lx, dma_addr: %lx\n",
296 npages, entry, dma_addr);
298 /* Insert into HW table */
299 ppc_md.tce_build(tbl, entry, npages, vaddr & PAGE_MASK, direction);
301 /* If we are in an open segment, try merging */
303 DBG(" - trying merge...\n");
304 /* We cannot merge if:
305 * - allocated dma_addr isn't contiguous to previous allocation
307 if (novmerge || (dma_addr != dma_next)) {
308 /* Can't merge: create a new segment */
311 DBG(" can't merge, new segment.\n");
313 outs->dma_length += s->length;
314 DBG(" merged, new len: %lx\n", outs->dma_length);
319 /* This is a new segment, fill entries */
320 DBG(" - filling new segment.\n");
321 outs->dma_address = dma_addr;
322 outs->dma_length = slen;
325 /* Calculate next page pointer for contiguous check */
326 dma_next = dma_addr + slen;
328 DBG(" - dma next is: %lx\n", dma_next);
331 /* Flush/invalidate TLB caches if necessary */
332 if (ppc_md.tce_flush)
333 ppc_md.tce_flush(tbl);
335 spin_unlock_irqrestore(&(tbl->it_lock), flags);
337 DBG("mapped %d elements:\n", outcount);
339 /* For the sake of iommu_unmap_sg, we clear out the length in the
340 * next entry of the sglist if we didn't fill the list completely
342 if (outcount < incount) {
344 outs->dma_address = DMA_ERROR_CODE;
345 outs->dma_length = 0;
348 /* Make sure updates are seen by hardware */
354 for (s = &sglist[0]; s <= outs; s++) {
355 if (s->dma_length != 0) {
356 unsigned long vaddr, npages;
358 vaddr = s->dma_address & PAGE_MASK;
359 npages = (PAGE_ALIGN(s->dma_address + s->dma_length) - vaddr)
361 __iommu_free(tbl, vaddr, npages);
362 s->dma_address = DMA_ERROR_CODE;
366 spin_unlock_irqrestore(&(tbl->it_lock), flags);
371 void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
372 int nelems, enum dma_data_direction direction)
376 BUG_ON(direction == DMA_NONE);
381 spin_lock_irqsave(&(tbl->it_lock), flags);
385 dma_addr_t dma_handle = sglist->dma_address;
387 if (sglist->dma_length == 0)
389 npages = (PAGE_ALIGN(dma_handle + sglist->dma_length)
390 - (dma_handle & PAGE_MASK)) >> PAGE_SHIFT;
391 __iommu_free(tbl, dma_handle, npages);
395 /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
396 * do not do an mb() here, the affected platforms do not need it
399 if (ppc_md.tce_flush)
400 ppc_md.tce_flush(tbl);
402 spin_unlock_irqrestore(&(tbl->it_lock), flags);
406 * Build a iommu_table structure. This contains a bit map which
407 * is used to manage allocation of the tce space.
409 struct iommu_table *iommu_init_table(struct iommu_table *tbl)
412 static int welcomed = 0;
414 /* Set aside 1/4 of the table for large allocations. */
415 tbl->it_halfpoint = tbl->it_size * 3 / 4;
417 /* number of bytes needed for the bitmap */
418 sz = (tbl->it_size + 7) >> 3;
420 tbl->it_map = (unsigned long *)__get_free_pages(GFP_ATOMIC, get_order(sz));
422 panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
424 memset(tbl->it_map, 0, sz);
427 tbl->it_largehint = tbl->it_halfpoint;
428 spin_lock_init(&tbl->it_lock);
430 /* Clear the hardware table in case firmware left allocations in it */
431 ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
434 printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",
435 novmerge ? "disabled" : "enabled");
442 void iommu_free_table(struct device_node *dn)
444 struct pci_dn *pdn = dn->data;
445 struct iommu_table *tbl = pdn->iommu_table;
446 unsigned long bitmap_sz, i;
449 if (!tbl || !tbl->it_map) {
450 printk(KERN_ERR "%s: expected TCE map for %s\n", __FUNCTION__,
455 /* verify that table contains no entries */
456 /* it_size is in entries, and we're examining 64 at a time */
457 for (i = 0; i < (tbl->it_size/64); i++) {
458 if (tbl->it_map[i] != 0) {
459 printk(KERN_WARNING "%s: Unexpected TCEs for %s\n",
460 __FUNCTION__, dn->full_name);
465 /* calculate bitmap size in bytes */
466 bitmap_sz = (tbl->it_size + 7) / 8;
469 order = get_order(bitmap_sz);
470 free_pages((unsigned long) tbl->it_map, order);
476 /* Creates TCEs for a user provided buffer. The user buffer must be
477 * contiguous real kernel storage (not vmalloc). The address of the buffer
478 * passed here is the kernel (virtual) address of the buffer. The buffer
479 * need not be page aligned, the dma_addr_t returned will point to the same
480 * byte within the page as vaddr.
482 dma_addr_t iommu_map_single(struct iommu_table *tbl, void *vaddr,
483 size_t size, enum dma_data_direction direction)
485 dma_addr_t dma_handle = DMA_ERROR_CODE;
489 BUG_ON(direction == DMA_NONE);
491 uaddr = (unsigned long)vaddr;
492 npages = PAGE_ALIGN(uaddr + size) - (uaddr & PAGE_MASK);
493 npages >>= PAGE_SHIFT;
496 dma_handle = iommu_alloc(tbl, vaddr, npages, direction, 0);
497 if (dma_handle == DMA_ERROR_CODE) {
498 if (printk_ratelimit()) {
499 printk(KERN_INFO "iommu_alloc failed, "
500 "tbl %p vaddr %p npages %d\n",
504 dma_handle |= (uaddr & ~PAGE_MASK);
510 void iommu_unmap_single(struct iommu_table *tbl, dma_addr_t dma_handle,
511 size_t size, enum dma_data_direction direction)
513 BUG_ON(direction == DMA_NONE);
516 iommu_free(tbl, dma_handle, (PAGE_ALIGN(dma_handle + size) -
517 (dma_handle & PAGE_MASK)) >> PAGE_SHIFT);
520 /* Allocates a contiguous real buffer and creates mappings over it.
521 * Returns the virtual address of the buffer and sets dma_handle
522 * to the dma address (mapping) of the first page.
524 void *iommu_alloc_coherent(struct iommu_table *tbl, size_t size,
525 dma_addr_t *dma_handle, gfp_t flag)
529 unsigned int npages, order;
531 size = PAGE_ALIGN(size);
532 npages = size >> PAGE_SHIFT;
533 order = get_order(size);
536 * Client asked for way too much space. This is checked later
537 * anyway. It is easier to debug here for the drivers than in
540 if (order >= IOMAP_MAX_ORDER) {
541 printk("iommu_alloc_consistent size too large: 0x%lx\n", size);
548 /* Alloc enough pages (and possibly more) */
549 ret = (void *)__get_free_pages(flag, order);
552 memset(ret, 0, size);
554 /* Set up tces to cover the allocated range */
555 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL, order);
556 if (mapping == DMA_ERROR_CODE) {
557 free_pages((unsigned long)ret, order);
560 *dma_handle = mapping;
564 void iommu_free_coherent(struct iommu_table *tbl, size_t size,
565 void *vaddr, dma_addr_t dma_handle)
570 size = PAGE_ALIGN(size);
571 npages = size >> PAGE_SHIFT;
572 iommu_free(tbl, dma_handle, npages);
573 free_pages((unsigned long)vaddr, get_order(size));