David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 1 | /* iommu.c: Generic sparc64 IOMMU support. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 3 | * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright (C) 1999, 2000 Jakub Jelinek (jakub@redhat.com) |
| 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 9 | #include <linux/delay.h> |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 10 | #include <linux/device.h> |
| 11 | #include <linux/dma-mapping.h> |
| 12 | #include <linux/errno.h> |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 13 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 15 | #ifdef CONFIG_PCI |
| 16 | #include <linux/pci.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <asm/iommu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include "iommu_common.h" |
| 22 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 23 | #define STC_CTXMATCH_ADDR(STC, CTX) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | ((STC)->strbuf_ctxmatch_base + ((CTX) << 3)) |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 25 | #define STC_FLUSHFLAG_INIT(STC) \ |
| 26 | (*((STC)->strbuf_flushflag) = 0UL) |
| 27 | #define STC_FLUSHFLAG_SET(STC) \ |
| 28 | (*((STC)->strbuf_flushflag) != 0UL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 30 | #define iommu_read(__reg) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | ({ u64 __ret; \ |
| 32 | __asm__ __volatile__("ldxa [%1] %2, %0" \ |
| 33 | : "=r" (__ret) \ |
| 34 | : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \ |
| 35 | : "memory"); \ |
| 36 | __ret; \ |
| 37 | }) |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 38 | #define iommu_write(__reg, __val) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | __asm__ __volatile__("stxa %0, [%1] %2" \ |
| 40 | : /* no outputs */ \ |
| 41 | : "r" (__val), "r" (__reg), \ |
| 42 | "i" (ASI_PHYS_BYPASS_EC_E)) |
| 43 | |
| 44 | /* Must be invoked under the IOMMU lock. */ |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 45 | static void __iommu_flushall(struct iommu *iommu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
David S. Miller | 861fe90 | 2007-05-02 17:31:36 -0700 | [diff] [blame] | 47 | if (iommu->iommu_flushinv) { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 48 | iommu_write(iommu->iommu_flushinv, ~(u64)0); |
David S. Miller | 861fe90 | 2007-05-02 17:31:36 -0700 | [diff] [blame] | 49 | } else { |
| 50 | unsigned long tag; |
| 51 | int entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 53 | tag = iommu->iommu_tags; |
David S. Miller | 861fe90 | 2007-05-02 17:31:36 -0700 | [diff] [blame] | 54 | for (entry = 0; entry < 16; entry++) { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 55 | iommu_write(tag, 0); |
David S. Miller | 861fe90 | 2007-05-02 17:31:36 -0700 | [diff] [blame] | 56 | tag += 8; |
| 57 | } |
| 58 | |
| 59 | /* Ensure completion of previous PIO writes. */ |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 60 | (void) iommu_read(iommu->write_complete_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | #define IOPTE_CONSISTENT(CTX) \ |
| 65 | (IOPTE_VALID | IOPTE_CACHE | \ |
| 66 | (((CTX) << 47) & IOPTE_CONTEXT)) |
| 67 | |
| 68 | #define IOPTE_STREAMING(CTX) \ |
| 69 | (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF) |
| 70 | |
| 71 | /* Existing mappings are never marked invalid, instead they |
| 72 | * are pointed to a dummy page. |
| 73 | */ |
| 74 | #define IOPTE_IS_DUMMY(iommu, iopte) \ |
| 75 | ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa) |
| 76 | |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 77 | static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | unsigned long val = iopte_val(*iopte); |
| 80 | |
| 81 | val &= ~IOPTE_PAGE; |
| 82 | val |= iommu->dummy_page_pa; |
| 83 | |
| 84 | iopte_val(*iopte) = val; |
| 85 | } |
| 86 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 87 | /* Based largely upon the ppc64 iommu allocator. */ |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 88 | static long arena_alloc(struct iommu *iommu, unsigned long npages) |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 89 | { |
David S. Miller | 9b3627f | 2007-04-24 23:51:18 -0700 | [diff] [blame] | 90 | struct iommu_arena *arena = &iommu->arena; |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 91 | unsigned long n, i, start, end, limit; |
| 92 | int pass; |
| 93 | |
| 94 | limit = arena->limit; |
| 95 | start = arena->hint; |
| 96 | pass = 0; |
| 97 | |
| 98 | again: |
| 99 | n = find_next_zero_bit(arena->map, limit, start); |
| 100 | end = n + npages; |
| 101 | if (unlikely(end >= limit)) { |
| 102 | if (likely(pass < 1)) { |
| 103 | limit = start; |
| 104 | start = 0; |
| 105 | __iommu_flushall(iommu); |
| 106 | pass++; |
| 107 | goto again; |
| 108 | } else { |
| 109 | /* Scanned the whole thing, give up. */ |
| 110 | return -1; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | for (i = n; i < end; i++) { |
| 115 | if (test_bit(i, arena->map)) { |
| 116 | start = i + 1; |
| 117 | goto again; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | for (i = n; i < end; i++) |
| 122 | __set_bit(i, arena->map); |
| 123 | |
| 124 | arena->hint = end; |
| 125 | |
| 126 | return n; |
| 127 | } |
| 128 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 129 | static void arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages) |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 130 | { |
| 131 | unsigned long i; |
| 132 | |
| 133 | for (i = base; i < (base + npages); i++) |
| 134 | __clear_bit(i, arena->map); |
| 135 | } |
| 136 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 137 | int iommu_table_init(struct iommu *iommu, int tsbsize, |
| 138 | u32 dma_offset, u32 dma_addr_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 140 | unsigned long i, tsbbase, order, sz, num_tsb_entries; |
| 141 | |
| 142 | num_tsb_entries = tsbsize / sizeof(iopte_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
David S. Miller | 51e8513 | 2005-10-13 21:10:08 -0700 | [diff] [blame] | 144 | /* Setup initial software IOMMU state. */ |
| 145 | spin_lock_init(&iommu->lock); |
| 146 | iommu->ctx_lowest_free = 1; |
| 147 | iommu->page_table_map_base = dma_offset; |
| 148 | iommu->dma_addr_mask = dma_addr_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 150 | /* Allocate and initialize the free area map. */ |
| 151 | sz = num_tsb_entries / 8; |
| 152 | sz = (sz + 7UL) & ~7UL; |
Eric Sesterhenn | 9132983 | 2006-03-06 13:48:40 -0800 | [diff] [blame] | 153 | iommu->arena.map = kzalloc(sz, GFP_KERNEL); |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 154 | if (!iommu->arena.map) { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 155 | printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n"); |
| 156 | return -ENOMEM; |
David S. Miller | 51e8513 | 2005-10-13 21:10:08 -0700 | [diff] [blame] | 157 | } |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 158 | iommu->arena.limit = num_tsb_entries; |
David S. Miller | 51e8513 | 2005-10-13 21:10:08 -0700 | [diff] [blame] | 159 | |
| 160 | /* Allocate and initialize the dummy page which we |
| 161 | * set inactive IO PTEs to point to. |
| 162 | */ |
| 163 | iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0); |
| 164 | if (!iommu->dummy_page) { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 165 | printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n"); |
| 166 | goto out_free_map; |
David S. Miller | 51e8513 | 2005-10-13 21:10:08 -0700 | [diff] [blame] | 167 | } |
| 168 | memset((void *)iommu->dummy_page, 0, PAGE_SIZE); |
| 169 | iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page); |
| 170 | |
| 171 | /* Now allocate and setup the IOMMU page table itself. */ |
| 172 | order = get_order(tsbsize); |
| 173 | tsbbase = __get_free_pages(GFP_KERNEL, order); |
| 174 | if (!tsbbase) { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 175 | printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n"); |
| 176 | goto out_free_dummy_page; |
David S. Miller | 51e8513 | 2005-10-13 21:10:08 -0700 | [diff] [blame] | 177 | } |
| 178 | iommu->page_table = (iopte_t *)tsbbase; |
| 179 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 180 | for (i = 0; i < num_tsb_entries; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | iopte_make_dummy(iommu, &iommu->page_table[i]); |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | |
| 185 | out_free_dummy_page: |
| 186 | free_page(iommu->dummy_page); |
| 187 | iommu->dummy_page = 0UL; |
| 188 | |
| 189 | out_free_map: |
| 190 | kfree(iommu->arena.map); |
| 191 | iommu->arena.map = NULL; |
| 192 | |
| 193 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 196 | static inline iopte_t *alloc_npages(struct iommu *iommu, unsigned long npages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 198 | long entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 200 | entry = arena_alloc(iommu, npages); |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 201 | if (unlikely(entry < 0)) |
| 202 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 204 | return iommu->page_table + entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 207 | static inline void free_npages(struct iommu *iommu, dma_addr_t base, unsigned long npages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 209 | arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 212 | static int iommu_alloc_ctx(struct iommu *iommu) |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 213 | { |
| 214 | int lowest = iommu->ctx_lowest_free; |
| 215 | int sz = IOMMU_NUM_CTXS - lowest; |
| 216 | int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest); |
| 217 | |
| 218 | if (unlikely(n == sz)) { |
| 219 | n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1); |
| 220 | if (unlikely(n == lowest)) { |
| 221 | printk(KERN_WARNING "IOMMU: Ran out of contexts.\n"); |
| 222 | n = 0; |
| 223 | } |
| 224 | } |
| 225 | if (n) |
| 226 | __set_bit(n, iommu->ctx_bitmap); |
| 227 | |
| 228 | return n; |
| 229 | } |
| 230 | |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 231 | static inline void iommu_free_ctx(struct iommu *iommu, int ctx) |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 232 | { |
| 233 | if (likely(ctx)) { |
| 234 | __clear_bit(ctx, iommu->ctx_bitmap); |
| 235 | if (ctx < iommu->ctx_lowest_free) |
| 236 | iommu->ctx_lowest_free = ctx; |
| 237 | } |
| 238 | } |
| 239 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 240 | static void *dma_4u_alloc_coherent(struct device *dev, size_t size, |
| 241 | dma_addr_t *dma_addrp, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 243 | struct iommu *iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | iopte_t *iopte; |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 245 | unsigned long flags, order, first_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | void *ret; |
| 247 | int npages; |
| 248 | |
| 249 | size = IO_PAGE_ALIGN(size); |
| 250 | order = get_order(size); |
| 251 | if (order >= 10) |
| 252 | return NULL; |
| 253 | |
David S. Miller | 42f1423 | 2006-05-23 02:07:22 -0700 | [diff] [blame] | 254 | first_page = __get_free_pages(gfp, order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (first_page == 0UL) |
| 256 | return NULL; |
| 257 | memset((char *)first_page, 0, PAGE_SIZE << order); |
| 258 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 259 | iommu = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | spin_lock_irqsave(&iommu->lock, flags); |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 262 | iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT); |
| 263 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 264 | |
| 265 | if (unlikely(iopte == NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | free_pages(first_page, order); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | *dma_addrp = (iommu->page_table_map_base + |
| 271 | ((iopte - iommu->page_table) << IO_PAGE_SHIFT)); |
| 272 | ret = (void *) first_page; |
| 273 | npages = size >> IO_PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | first_page = __pa(first_page); |
| 275 | while (npages--) { |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 276 | iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | IOPTE_WRITE | |
| 278 | (first_page & IOPTE_PAGE)); |
| 279 | iopte++; |
| 280 | first_page += IO_PAGE_SIZE; |
| 281 | } |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return ret; |
| 284 | } |
| 285 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 286 | static void dma_4u_free_coherent(struct device *dev, size_t size, |
| 287 | void *cpu, dma_addr_t dvma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 289 | struct iommu *iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | iopte_t *iopte; |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 291 | unsigned long flags, order, npages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT; |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 294 | iommu = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | iopte = iommu->page_table + |
| 296 | ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT); |
| 297 | |
| 298 | spin_lock_irqsave(&iommu->lock, flags); |
| 299 | |
David S. Miller | 012d64f | 2006-10-25 22:33:07 -0700 | [diff] [blame] | 300 | free_npages(iommu, dvma - iommu->page_table_map_base, npages); |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 303 | |
| 304 | order = get_order(size); |
| 305 | if (order < 10) |
| 306 | free_pages((unsigned long)cpu, order); |
| 307 | } |
| 308 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 309 | static dma_addr_t dma_4u_map_single(struct device *dev, void *ptr, size_t sz, |
| 310 | enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 312 | struct iommu *iommu; |
| 313 | struct strbuf *strbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | iopte_t *base; |
| 315 | unsigned long flags, npages, oaddr; |
| 316 | unsigned long i, base_paddr, ctx; |
| 317 | u32 bus_addr, ret; |
| 318 | unsigned long iopte_protection; |
| 319 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 320 | iommu = dev->archdata.iommu; |
| 321 | strbuf = dev->archdata.stc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 323 | if (unlikely(direction == DMA_NONE)) |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 324 | goto bad_no_ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | oaddr = (unsigned long)ptr; |
| 327 | npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK); |
| 328 | npages >>= IO_PAGE_SHIFT; |
| 329 | |
| 330 | spin_lock_irqsave(&iommu->lock, flags); |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 331 | base = alloc_npages(iommu, npages); |
| 332 | ctx = 0; |
| 333 | if (iommu->iommu_ctxflush) |
| 334 | ctx = iommu_alloc_ctx(iommu); |
| 335 | spin_unlock_irqrestore(&iommu->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 337 | if (unlikely(!base)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | goto bad; |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | bus_addr = (iommu->page_table_map_base + |
| 341 | ((base - iommu->page_table) << IO_PAGE_SHIFT)); |
| 342 | ret = bus_addr | (oaddr & ~IO_PAGE_MASK); |
| 343 | base_paddr = __pa(oaddr & IO_PAGE_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (strbuf->strbuf_enabled) |
| 345 | iopte_protection = IOPTE_STREAMING(ctx); |
| 346 | else |
| 347 | iopte_protection = IOPTE_CONSISTENT(ctx); |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 348 | if (direction != DMA_TO_DEVICE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | iopte_protection |= IOPTE_WRITE; |
| 350 | |
| 351 | for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE) |
| 352 | iopte_val(*base) = iopte_protection | base_paddr; |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return ret; |
| 355 | |
| 356 | bad: |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 357 | iommu_free_ctx(iommu, ctx); |
| 358 | bad_no_ctx: |
| 359 | if (printk_ratelimit()) |
| 360 | WARN_ON(1); |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 361 | return DMA_ERROR_CODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 364 | static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu, |
| 365 | u32 vaddr, unsigned long ctx, unsigned long npages, |
| 366 | enum dma_data_direction direction) |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 367 | { |
| 368 | int limit; |
| 369 | |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 370 | if (strbuf->strbuf_ctxflush && |
| 371 | iommu->iommu_ctxflush) { |
| 372 | unsigned long matchreg, flushreg; |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 373 | u64 val; |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 374 | |
| 375 | flushreg = strbuf->strbuf_ctxflush; |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 376 | matchreg = STC_CTXMATCH_ADDR(strbuf, ctx); |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 377 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 378 | iommu_write(flushreg, ctx); |
| 379 | val = iommu_read(matchreg); |
David S. Miller | 88314ee | 2005-05-31 19:13:52 -0700 | [diff] [blame] | 380 | val &= 0xffff; |
| 381 | if (!val) |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 382 | goto do_flush_sync; |
| 383 | |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 384 | while (val) { |
| 385 | if (val & 0x1) |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 386 | iommu_write(flushreg, ctx); |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 387 | val >>= 1; |
David S. Miller | a228dfd | 2005-05-20 11:40:32 -0700 | [diff] [blame] | 388 | } |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 389 | val = iommu_read(matchreg); |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 390 | if (unlikely(val)) { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 391 | printk(KERN_WARNING "strbuf_flush: ctx flush " |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 392 | "timeout matchreg[%lx] ctx[%lx]\n", |
| 393 | val, ctx); |
| 394 | goto do_page_flush; |
| 395 | } |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 396 | } else { |
| 397 | unsigned long i; |
| 398 | |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 399 | do_page_flush: |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 400 | for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE) |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 401 | iommu_write(strbuf->strbuf_pflush, vaddr); |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 402 | } |
| 403 | |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 404 | do_flush_sync: |
| 405 | /* If the device could not have possibly put dirty data into |
| 406 | * the streaming cache, no flush-flag synchronization needs |
| 407 | * to be performed. |
| 408 | */ |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 409 | if (direction == DMA_TO_DEVICE) |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 410 | return; |
| 411 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 412 | STC_FLUSHFLAG_INIT(strbuf); |
| 413 | iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa); |
| 414 | (void) iommu_read(iommu->write_complete_reg); |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 415 | |
David S. Miller | a228dfd | 2005-05-20 11:40:32 -0700 | [diff] [blame] | 416 | limit = 100000; |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 417 | while (!STC_FLUSHFLAG_SET(strbuf)) { |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 418 | limit--; |
| 419 | if (!limit) |
| 420 | break; |
David S. Miller | a228dfd | 2005-05-20 11:40:32 -0700 | [diff] [blame] | 421 | udelay(1); |
David S. Miller | 4f07118 | 2005-08-29 12:46:22 -0700 | [diff] [blame] | 422 | rmb(); |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 423 | } |
| 424 | if (!limit) |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 425 | printk(KERN_WARNING "strbuf_flush: flushflag timeout " |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 426 | "vaddr[%08x] ctx[%lx] npages[%ld]\n", |
| 427 | vaddr, ctx, npages); |
| 428 | } |
| 429 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 430 | static void dma_4u_unmap_single(struct device *dev, dma_addr_t bus_addr, |
| 431 | size_t sz, enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 433 | struct iommu *iommu; |
| 434 | struct strbuf *strbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | iopte_t *base; |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 436 | unsigned long flags, npages, ctx, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 438 | if (unlikely(direction == DMA_NONE)) { |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 439 | if (printk_ratelimit()) |
| 440 | WARN_ON(1); |
| 441 | return; |
| 442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 444 | iommu = dev->archdata.iommu; |
| 445 | strbuf = dev->archdata.stc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK); |
| 448 | npages >>= IO_PAGE_SHIFT; |
| 449 | base = iommu->page_table + |
| 450 | ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | bus_addr &= IO_PAGE_MASK; |
| 452 | |
| 453 | spin_lock_irqsave(&iommu->lock, flags); |
| 454 | |
| 455 | /* Record the context, if any. */ |
| 456 | ctx = 0; |
| 457 | if (iommu->iommu_ctxflush) |
| 458 | ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL; |
| 459 | |
| 460 | /* Step 1: Kick data out of streaming buffers if necessary. */ |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 461 | if (strbuf->strbuf_enabled) |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 462 | strbuf_flush(strbuf, iommu, bus_addr, ctx, |
| 463 | npages, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 465 | /* Step 2: Clear out TSB entries. */ |
| 466 | for (i = 0; i < npages; i++) |
| 467 | iopte_make_dummy(iommu, base + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 469 | free_npages(iommu, bus_addr - iommu->page_table_map_base, npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 471 | iommu_free_ctx(iommu, ctx); |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 474 | } |
| 475 | |
| 476 | #define SG_ENT_PHYS_ADDRESS(SG) \ |
| 477 | (__pa(page_address((SG)->page)) + (SG)->offset) |
| 478 | |
| 479 | static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg, |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 480 | int nused, int nelems, |
| 481 | unsigned long iopte_protection) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
| 483 | struct scatterlist *dma_sg = sg; |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 484 | struct scatterlist *sg_end = sg_last(sg, nelems); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | int i; |
| 486 | |
| 487 | for (i = 0; i < nused; i++) { |
| 488 | unsigned long pteval = ~0UL; |
| 489 | u32 dma_npages; |
| 490 | |
| 491 | dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) + |
| 492 | dma_sg->dma_length + |
| 493 | ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT; |
| 494 | do { |
| 495 | unsigned long offset; |
| 496 | signed int len; |
| 497 | |
| 498 | /* If we are here, we know we have at least one |
| 499 | * more page to map. So walk forward until we |
| 500 | * hit a page crossing, and begin creating new |
| 501 | * mappings from that spot. |
| 502 | */ |
| 503 | for (;;) { |
| 504 | unsigned long tmp; |
| 505 | |
| 506 | tmp = SG_ENT_PHYS_ADDRESS(sg); |
| 507 | len = sg->length; |
| 508 | if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) { |
| 509 | pteval = tmp & IO_PAGE_MASK; |
| 510 | offset = tmp & (IO_PAGE_SIZE - 1UL); |
| 511 | break; |
| 512 | } |
| 513 | if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) { |
| 514 | pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK; |
| 515 | offset = 0UL; |
| 516 | len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL))); |
| 517 | break; |
| 518 | } |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 519 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | pteval = iopte_protection | (pteval & IOPTE_PAGE); |
| 523 | while (len > 0) { |
| 524 | *iopte++ = __iopte(pteval); |
| 525 | pteval += IO_PAGE_SIZE; |
| 526 | len -= (IO_PAGE_SIZE - offset); |
| 527 | offset = 0; |
| 528 | dma_npages--; |
| 529 | } |
| 530 | |
| 531 | pteval = (pteval & IOPTE_PAGE) + len; |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 532 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | /* Skip over any tail mappings we've fully mapped, |
| 535 | * adjusting pteval along the way. Stop when we |
| 536 | * detect a page crossing event. |
| 537 | */ |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 538 | while (sg != sg_end && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | (pteval << (64 - IO_PAGE_SHIFT)) != 0UL && |
| 540 | (pteval == SG_ENT_PHYS_ADDRESS(sg)) && |
| 541 | ((pteval ^ |
| 542 | (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) { |
| 543 | pteval += sg->length; |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 544 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |
| 546 | if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL) |
| 547 | pteval = ~0UL; |
| 548 | } while (dma_npages != 0); |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 549 | dma_sg = sg_next(dma_sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 553 | static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist, |
| 554 | int nelems, enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 556 | struct iommu *iommu; |
| 557 | struct strbuf *strbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | unsigned long flags, ctx, npages, iopte_protection; |
| 559 | iopte_t *base; |
| 560 | u32 dma_base; |
| 561 | struct scatterlist *sgtmp; |
| 562 | int used; |
| 563 | |
| 564 | /* Fast path single entry scatterlists. */ |
| 565 | if (nelems == 1) { |
| 566 | sglist->dma_address = |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 567 | dma_4u_map_single(dev, |
| 568 | (page_address(sglist->page) + |
| 569 | sglist->offset), |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 570 | sglist->length, direction); |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 571 | if (unlikely(sglist->dma_address == DMA_ERROR_CODE)) |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 572 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | sglist->dma_length = sglist->length; |
| 574 | return 1; |
| 575 | } |
| 576 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 577 | iommu = dev->archdata.iommu; |
| 578 | strbuf = dev->archdata.stc; |
| 579 | |
| 580 | if (unlikely(direction == DMA_NONE)) |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 581 | goto bad_no_ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | /* Step 1: Prepare scatter list. */ |
| 584 | |
| 585 | npages = prepare_sg(sglist, nelems); |
| 586 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 587 | /* Step 2: Allocate a cluster and context, if necessary. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | spin_lock_irqsave(&iommu->lock, flags); |
| 590 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 591 | base = alloc_npages(iommu, npages); |
| 592 | ctx = 0; |
| 593 | if (iommu->iommu_ctxflush) |
| 594 | ctx = iommu_alloc_ctx(iommu); |
| 595 | |
| 596 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | if (base == NULL) |
| 599 | goto bad; |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 600 | |
| 601 | dma_base = iommu->page_table_map_base + |
| 602 | ((base - iommu->page_table) << IO_PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
| 604 | /* Step 3: Normalize DMA addresses. */ |
| 605 | used = nelems; |
| 606 | |
| 607 | sgtmp = sglist; |
| 608 | while (used && sgtmp->dma_length) { |
| 609 | sgtmp->dma_address += dma_base; |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 610 | sgtmp = sg_next(sgtmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | used--; |
| 612 | } |
| 613 | used = nelems - used; |
| 614 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 615 | /* Step 4: Create the mappings. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | if (strbuf->strbuf_enabled) |
| 617 | iopte_protection = IOPTE_STREAMING(ctx); |
| 618 | else |
| 619 | iopte_protection = IOPTE_CONSISTENT(ctx); |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 620 | if (direction != DMA_TO_DEVICE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | iopte_protection |= IOPTE_WRITE; |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 622 | |
| 623 | fill_sg(base, sglist, used, nelems, iopte_protection); |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | #ifdef VERIFY_SG |
| 626 | verify_sglist(sglist, nelems, base, npages); |
| 627 | #endif |
| 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | return used; |
| 630 | |
| 631 | bad: |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 632 | iommu_free_ctx(iommu, ctx); |
| 633 | bad_no_ctx: |
| 634 | if (printk_ratelimit()) |
| 635 | WARN_ON(1); |
| 636 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 639 | static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 640 | int nelems, enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 642 | struct iommu *iommu; |
| 643 | struct strbuf *strbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | iopte_t *base; |
| 645 | unsigned long flags, ctx, i, npages; |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 646 | struct scatterlist *sg, *sgprv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | u32 bus_addr; |
| 648 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 649 | if (unlikely(direction == DMA_NONE)) { |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 650 | if (printk_ratelimit()) |
| 651 | WARN_ON(1); |
| 652 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 654 | iommu = dev->archdata.iommu; |
| 655 | strbuf = dev->archdata.stc; |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | bus_addr = sglist->dma_address & IO_PAGE_MASK; |
| 658 | |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 659 | sgprv = NULL; |
| 660 | for_each_sg(sglist, sg, nelems, i) { |
| 661 | if (sg->dma_length == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | break; |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 663 | sgprv = sg; |
| 664 | } |
| 665 | |
| 666 | npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length) - |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 667 | bus_addr) >> IO_PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
| 669 | base = iommu->page_table + |
| 670 | ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT); |
| 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | spin_lock_irqsave(&iommu->lock, flags); |
| 673 | |
| 674 | /* Record the context, if any. */ |
| 675 | ctx = 0; |
| 676 | if (iommu->iommu_ctxflush) |
| 677 | ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL; |
| 678 | |
| 679 | /* Step 1: Kick data out of streaming buffers if necessary. */ |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 680 | if (strbuf->strbuf_enabled) |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 681 | strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 683 | /* Step 2: Clear out the TSB entries. */ |
| 684 | for (i = 0; i < npages; i++) |
| 685 | iopte_make_dummy(iommu, base + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
David S. Miller | 688cb30 | 2005-10-13 22:15:24 -0700 | [diff] [blame] | 687 | free_npages(iommu, bus_addr - iommu->page_table_map_base, npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
David S. Miller | 7c963ad | 2005-05-31 16:57:59 -0700 | [diff] [blame] | 689 | iommu_free_ctx(iommu, ctx); |
| 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 692 | } |
| 693 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 694 | static void dma_4u_sync_single_for_cpu(struct device *dev, |
| 695 | dma_addr_t bus_addr, size_t sz, |
| 696 | enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 698 | struct iommu *iommu; |
| 699 | struct strbuf *strbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | unsigned long flags, ctx, npages; |
| 701 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 702 | iommu = dev->archdata.iommu; |
| 703 | strbuf = dev->archdata.stc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
| 705 | if (!strbuf->strbuf_enabled) |
| 706 | return; |
| 707 | |
| 708 | spin_lock_irqsave(&iommu->lock, flags); |
| 709 | |
| 710 | npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK); |
| 711 | npages >>= IO_PAGE_SHIFT; |
| 712 | bus_addr &= IO_PAGE_MASK; |
| 713 | |
| 714 | /* Step 1: Record the context, if any. */ |
| 715 | ctx = 0; |
| 716 | if (iommu->iommu_ctxflush && |
| 717 | strbuf->strbuf_ctxflush) { |
| 718 | iopte_t *iopte; |
| 719 | |
| 720 | iopte = iommu->page_table + |
| 721 | ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT); |
| 722 | ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL; |
| 723 | } |
| 724 | |
| 725 | /* Step 2: Kick data out of streaming buffers. */ |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 726 | strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
| 728 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 729 | } |
| 730 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 731 | static void dma_4u_sync_sg_for_cpu(struct device *dev, |
| 732 | struct scatterlist *sglist, int nelems, |
| 733 | enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 735 | struct iommu *iommu; |
| 736 | struct strbuf *strbuf; |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 737 | unsigned long flags, ctx, npages, i; |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 738 | struct scatterlist *sg, *sgprv; |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 739 | u32 bus_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 741 | iommu = dev->archdata.iommu; |
| 742 | strbuf = dev->archdata.stc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
| 744 | if (!strbuf->strbuf_enabled) |
| 745 | return; |
| 746 | |
| 747 | spin_lock_irqsave(&iommu->lock, flags); |
| 748 | |
| 749 | /* Step 1: Record the context, if any. */ |
| 750 | ctx = 0; |
| 751 | if (iommu->iommu_ctxflush && |
| 752 | strbuf->strbuf_ctxflush) { |
| 753 | iopte_t *iopte; |
| 754 | |
| 755 | iopte = iommu->page_table + |
| 756 | ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT); |
| 757 | ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL; |
| 758 | } |
| 759 | |
| 760 | /* Step 2: Kick data out of streaming buffers. */ |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 761 | bus_addr = sglist[0].dma_address & IO_PAGE_MASK; |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 762 | sgprv = NULL; |
| 763 | for_each_sg(sglist, sg, nelems, i) { |
| 764 | if (sg->dma_length == 0) |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 765 | break; |
Jens Axboe | 2c941a2 | 2007-08-07 09:37:10 +0200 | [diff] [blame^] | 766 | sgprv = sg; |
| 767 | } |
| 768 | |
| 769 | npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length) |
David S. Miller | 4dbc30f | 2005-05-11 11:37:00 -0700 | [diff] [blame] | 770 | - bus_addr) >> IO_PAGE_SHIFT; |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 771 | strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
| 773 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 774 | } |
| 775 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 776 | const struct dma_ops sun4u_dma_ops = { |
| 777 | .alloc_coherent = dma_4u_alloc_coherent, |
| 778 | .free_coherent = dma_4u_free_coherent, |
| 779 | .map_single = dma_4u_map_single, |
| 780 | .unmap_single = dma_4u_unmap_single, |
| 781 | .map_sg = dma_4u_map_sg, |
| 782 | .unmap_sg = dma_4u_unmap_sg, |
| 783 | .sync_single_for_cpu = dma_4u_sync_single_for_cpu, |
| 784 | .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu, |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 785 | }; |
| 786 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 787 | const struct dma_ops *dma_ops = &sun4u_dma_ops; |
| 788 | EXPORT_SYMBOL(dma_ops); |
| 789 | |
| 790 | int dma_supported(struct device *dev, u64 device_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 792 | struct iommu *iommu = dev->archdata.iommu; |
| 793 | u64 dma_addr_mask = iommu->dma_addr_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
| 795 | if (device_mask >= (1UL << 32UL)) |
| 796 | return 0; |
| 797 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 798 | if ((device_mask & dma_addr_mask) == dma_addr_mask) |
| 799 | return 1; |
| 800 | |
| 801 | #ifdef CONFIG_PCI |
| 802 | if (dev->bus == &pci_bus_type) |
| 803 | return pci_dma_supported(to_pci_dev(dev), device_mask); |
| 804 | #endif |
| 805 | |
| 806 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 808 | EXPORT_SYMBOL(dma_supported); |
| 809 | |
| 810 | int dma_set_mask(struct device *dev, u64 dma_mask) |
| 811 | { |
| 812 | #ifdef CONFIG_PCI |
| 813 | if (dev->bus == &pci_bus_type) |
| 814 | return pci_set_dma_mask(to_pci_dev(dev), dma_mask); |
| 815 | #endif |
| 816 | return -EINVAL; |
| 817 | } |
| 818 | EXPORT_SYMBOL(dma_set_mask); |