blob: 47977a77f6c64ae1bb7dd5a16de27f14b3e59807 [file] [log] [blame]
David S. Millerad7ad572007-07-27 22:39:14 -07001/* iommu.c: Generic sparc64 IOMMU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Millerd2841422008-02-08 18:05:46 -08003 * Copyright (C) 1999, 2007, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 1999, 2000 Jakub Jelinek (jakub@redhat.com)
5 */
6
7#include <linux/kernel.h>
David S. Millerad7ad572007-07-27 22:39:14 -07008#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
David S. Miller4dbc30f2005-05-11 11:37:00 -070010#include <linux/delay.h>
David S. Millerad7ad572007-07-27 22:39:14 -070011#include <linux/device.h>
12#include <linux/dma-mapping.h>
13#include <linux/errno.h>
David S. Millerd2841422008-02-08 18:05:46 -080014#include <linux/iommu-helper.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080015#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
David S. Millerad7ad572007-07-27 22:39:14 -070017#ifdef CONFIG_PCI
18#include <linux/pci.h>
19#endif
20
21#include <asm/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "iommu_common.h"
24
David S. Millerad7ad572007-07-27 22:39:14 -070025#define STC_CTXMATCH_ADDR(STC, CTX) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
David S. Millerad7ad572007-07-27 22:39:14 -070027#define STC_FLUSHFLAG_INIT(STC) \
28 (*((STC)->strbuf_flushflag) = 0UL)
29#define STC_FLUSHFLAG_SET(STC) \
30 (*((STC)->strbuf_flushflag) != 0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
David S. Millerad7ad572007-07-27 22:39:14 -070032#define iommu_read(__reg) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070033({ u64 __ret; \
34 __asm__ __volatile__("ldxa [%1] %2, %0" \
35 : "=r" (__ret) \
36 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
37 : "memory"); \
38 __ret; \
39})
David S. Millerad7ad572007-07-27 22:39:14 -070040#define iommu_write(__reg, __val) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 __asm__ __volatile__("stxa %0, [%1] %2" \
42 : /* no outputs */ \
43 : "r" (__val), "r" (__reg), \
44 "i" (ASI_PHYS_BYPASS_EC_E))
45
46/* Must be invoked under the IOMMU lock. */
David S. Millerd2841422008-02-08 18:05:46 -080047static void iommu_flushall(struct iommu *iommu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
David S. Miller861fe902007-05-02 17:31:36 -070049 if (iommu->iommu_flushinv) {
David S. Millerad7ad572007-07-27 22:39:14 -070050 iommu_write(iommu->iommu_flushinv, ~(u64)0);
David S. Miller861fe902007-05-02 17:31:36 -070051 } else {
52 unsigned long tag;
53 int entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Millerad7ad572007-07-27 22:39:14 -070055 tag = iommu->iommu_tags;
David S. Miller861fe902007-05-02 17:31:36 -070056 for (entry = 0; entry < 16; entry++) {
David S. Millerad7ad572007-07-27 22:39:14 -070057 iommu_write(tag, 0);
David S. Miller861fe902007-05-02 17:31:36 -070058 tag += 8;
59 }
60
61 /* Ensure completion of previous PIO writes. */
David S. Millerad7ad572007-07-27 22:39:14 -070062 (void) iommu_read(iommu->write_complete_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66#define IOPTE_CONSISTENT(CTX) \
67 (IOPTE_VALID | IOPTE_CACHE | \
68 (((CTX) << 47) & IOPTE_CONTEXT))
69
70#define IOPTE_STREAMING(CTX) \
71 (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
72
73/* Existing mappings are never marked invalid, instead they
74 * are pointed to a dummy page.
75 */
76#define IOPTE_IS_DUMMY(iommu, iopte) \
77 ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
78
David S. Miller16ce82d2007-04-26 21:08:21 -070079static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 unsigned long val = iopte_val(*iopte);
82
83 val &= ~IOPTE_PAGE;
84 val |= iommu->dummy_page_pa;
85
86 iopte_val(*iopte) = val;
87}
88
David S. Millerd2841422008-02-08 18:05:46 -080089/* Based almost entirely upon the ppc64 iommu allocator. If you use the 'handle'
90 * facility it must all be done in one pass while under the iommu lock.
91 *
92 * On sun4u platforms, we only flush the IOMMU once every time we've passed
93 * over the entire page table doing allocations. Therefore we only ever advance
94 * the hint and cannot backtrack it.
95 */
96unsigned long iommu_range_alloc(struct device *dev,
97 struct iommu *iommu,
98 unsigned long npages,
99 unsigned long *handle)
David S. Miller688cb302005-10-13 22:15:24 -0700100{
David S. Millerd2841422008-02-08 18:05:46 -0800101 unsigned long n, end, start, limit, boundary_size;
David S. Miller9b3627f2007-04-24 23:51:18 -0700102 struct iommu_arena *arena = &iommu->arena;
David S. Millerd2841422008-02-08 18:05:46 -0800103 int pass = 0;
104
105 /* This allocator was derived from x86_64's bit string search */
106
107 /* Sanity check */
108 if (unlikely(npages == 0)) {
109 if (printk_ratelimit())
110 WARN_ON(1);
111 return DMA_ERROR_CODE;
112 }
113
114 if (handle && *handle)
115 start = *handle;
116 else
117 start = arena->hint;
David S. Miller688cb302005-10-13 22:15:24 -0700118
119 limit = arena->limit;
David S. Miller688cb302005-10-13 22:15:24 -0700120
David S. Millerd2841422008-02-08 18:05:46 -0800121 /* The case below can happen if we have a small segment appended
122 * to a large, or when the previous alloc was at the very end of
123 * the available space. If so, go back to the beginning and flush.
124 */
125 if (start >= limit) {
126 start = 0;
127 if (iommu->flush_all)
128 iommu->flush_all(iommu);
129 }
130
131 again:
132
133 if (dev)
134 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
135 1 << IO_PAGE_SHIFT);
136 else
137 boundary_size = ALIGN(1UL << 32, 1 << IO_PAGE_SHIFT);
138
FUJITA Tomonori89c94f22008-02-20 22:56:42 -0800139 n = iommu_area_alloc(arena->map, limit, start, npages,
140 iommu->page_table_map_base >> IO_PAGE_SHIFT,
David S. Millerd2841422008-02-08 18:05:46 -0800141 boundary_size >> IO_PAGE_SHIFT, 0);
142 if (n == -1) {
David S. Miller688cb302005-10-13 22:15:24 -0700143 if (likely(pass < 1)) {
David S. Millerd2841422008-02-08 18:05:46 -0800144 /* First failure, rescan from the beginning. */
David S. Miller688cb302005-10-13 22:15:24 -0700145 start = 0;
David S. Millerd2841422008-02-08 18:05:46 -0800146 if (iommu->flush_all)
147 iommu->flush_all(iommu);
David S. Miller688cb302005-10-13 22:15:24 -0700148 pass++;
149 goto again;
150 } else {
David S. Millerd2841422008-02-08 18:05:46 -0800151 /* Second failure, give up */
152 return DMA_ERROR_CODE;
David S. Miller688cb302005-10-13 22:15:24 -0700153 }
154 }
155
David S. Millerd2841422008-02-08 18:05:46 -0800156 end = n + npages;
David S. Miller688cb302005-10-13 22:15:24 -0700157
158 arena->hint = end;
159
David S. Millerd2841422008-02-08 18:05:46 -0800160 /* Update handle for SG allocations */
161 if (handle)
162 *handle = end;
163
David S. Miller688cb302005-10-13 22:15:24 -0700164 return n;
165}
166
David S. Millerd2841422008-02-08 18:05:46 -0800167void iommu_range_free(struct iommu *iommu, dma_addr_t dma_addr, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -0700168{
David S. Millerd2841422008-02-08 18:05:46 -0800169 struct iommu_arena *arena = &iommu->arena;
170 unsigned long entry;
David S. Miller688cb302005-10-13 22:15:24 -0700171
David S. Millerd2841422008-02-08 18:05:46 -0800172 entry = (dma_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
173
Akinobu Mitaa66022c2009-12-15 16:48:28 -0800174 bitmap_clear(arena->map, entry, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700175}
176
David S. Millerad7ad572007-07-27 22:39:14 -0700177int iommu_table_init(struct iommu *iommu, int tsbsize,
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700178 u32 dma_offset, u32 dma_addr_mask,
179 int numa_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700181 unsigned long i, order, sz, num_tsb_entries;
182 struct page *page;
David S. Miller688cb302005-10-13 22:15:24 -0700183
184 num_tsb_entries = tsbsize / sizeof(iopte_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David S. Miller51e85132005-10-13 21:10:08 -0700186 /* Setup initial software IOMMU state. */
187 spin_lock_init(&iommu->lock);
188 iommu->ctx_lowest_free = 1;
189 iommu->page_table_map_base = dma_offset;
190 iommu->dma_addr_mask = dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
David S. Miller688cb302005-10-13 22:15:24 -0700192 /* Allocate and initialize the free area map. */
193 sz = num_tsb_entries / 8;
194 sz = (sz + 7UL) & ~7UL;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700195 iommu->arena.map = kmalloc_node(sz, GFP_KERNEL, numa_node);
David S. Miller688cb302005-10-13 22:15:24 -0700196 if (!iommu->arena.map) {
David S. Millerad7ad572007-07-27 22:39:14 -0700197 printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
198 return -ENOMEM;
David S. Miller51e85132005-10-13 21:10:08 -0700199 }
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700200 memset(iommu->arena.map, 0, sz);
David S. Miller688cb302005-10-13 22:15:24 -0700201 iommu->arena.limit = num_tsb_entries;
David S. Miller51e85132005-10-13 21:10:08 -0700202
David S. Millerd2841422008-02-08 18:05:46 -0800203 if (tlb_type != hypervisor)
204 iommu->flush_all = iommu_flushall;
205
David S. Miller51e85132005-10-13 21:10:08 -0700206 /* Allocate and initialize the dummy page which we
207 * set inactive IO PTEs to point to.
208 */
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700209 page = alloc_pages_node(numa_node, GFP_KERNEL, 0);
210 if (!page) {
David S. Millerad7ad572007-07-27 22:39:14 -0700211 printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
212 goto out_free_map;
David S. Miller51e85132005-10-13 21:10:08 -0700213 }
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700214 iommu->dummy_page = (unsigned long) page_address(page);
215 memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
David S. Miller51e85132005-10-13 21:10:08 -0700216 iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
217
218 /* Now allocate and setup the IOMMU page table itself. */
219 order = get_order(tsbsize);
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700220 page = alloc_pages_node(numa_node, GFP_KERNEL, order);
221 if (!page) {
David S. Millerad7ad572007-07-27 22:39:14 -0700222 printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
223 goto out_free_dummy_page;
David S. Miller51e85132005-10-13 21:10:08 -0700224 }
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700225 iommu->page_table = (iopte_t *)page_address(page);
David S. Miller51e85132005-10-13 21:10:08 -0700226
David S. Miller688cb302005-10-13 22:15:24 -0700227 for (i = 0; i < num_tsb_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 iopte_make_dummy(iommu, &iommu->page_table[i]);
David S. Millerad7ad572007-07-27 22:39:14 -0700229
230 return 0;
231
232out_free_dummy_page:
233 free_page(iommu->dummy_page);
234 iommu->dummy_page = 0UL;
235
236out_free_map:
237 kfree(iommu->arena.map);
238 iommu->arena.map = NULL;
239
240 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
David S. Millerd2841422008-02-08 18:05:46 -0800243static inline iopte_t *alloc_npages(struct device *dev, struct iommu *iommu,
244 unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
David S. Millerd2841422008-02-08 18:05:46 -0800246 unsigned long entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David S. Millerd2841422008-02-08 18:05:46 -0800248 entry = iommu_range_alloc(dev, iommu, npages, NULL);
249 if (unlikely(entry == DMA_ERROR_CODE))
David S. Miller688cb302005-10-13 22:15:24 -0700250 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David S. Miller688cb302005-10-13 22:15:24 -0700252 return iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
David S. Miller16ce82d2007-04-26 21:08:21 -0700255static int iommu_alloc_ctx(struct iommu *iommu)
David S. Miller7c963ad2005-05-31 16:57:59 -0700256{
257 int lowest = iommu->ctx_lowest_free;
258 int sz = IOMMU_NUM_CTXS - lowest;
259 int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
260
261 if (unlikely(n == sz)) {
262 n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
263 if (unlikely(n == lowest)) {
264 printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
265 n = 0;
266 }
267 }
268 if (n)
269 __set_bit(n, iommu->ctx_bitmap);
270
271 return n;
272}
273
David S. Miller16ce82d2007-04-26 21:08:21 -0700274static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
David S. Miller7c963ad2005-05-31 16:57:59 -0700275{
276 if (likely(ctx)) {
277 __clear_bit(ctx, iommu->ctx_bitmap);
278 if (ctx < iommu->ctx_lowest_free)
279 iommu->ctx_lowest_free = ctx;
280 }
281}
282
David S. Millerad7ad572007-07-27 22:39:14 -0700283static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
284 dma_addr_t *dma_addrp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
David S. Miller688cb302005-10-13 22:15:24 -0700286 unsigned long flags, order, first_page;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700287 struct iommu *iommu;
288 struct page *page;
289 int npages, nid;
290 iopte_t *iopte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 size = IO_PAGE_ALIGN(size);
294 order = get_order(size);
295 if (order >= 10)
296 return NULL;
297
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700298 nid = dev->archdata.numa_node;
299 page = alloc_pages_node(nid, gfp, order);
300 if (unlikely(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return NULL;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700302
303 first_page = (unsigned long) page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 memset((char *)first_page, 0, PAGE_SIZE << order);
305
David S. Millerad7ad572007-07-27 22:39:14 -0700306 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800309 iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT);
David S. Miller688cb302005-10-13 22:15:24 -0700310 spin_unlock_irqrestore(&iommu->lock, flags);
311
312 if (unlikely(iopte == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 free_pages(first_page, order);
314 return NULL;
315 }
316
317 *dma_addrp = (iommu->page_table_map_base +
318 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
319 ret = (void *) first_page;
320 npages = size >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 first_page = __pa(first_page);
322 while (npages--) {
David S. Miller688cb302005-10-13 22:15:24 -0700323 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 IOPTE_WRITE |
325 (first_page & IOPTE_PAGE));
326 iopte++;
327 first_page += IO_PAGE_SIZE;
328 }
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return ret;
331}
332
David S. Millerad7ad572007-07-27 22:39:14 -0700333static void dma_4u_free_coherent(struct device *dev, size_t size,
334 void *cpu, dma_addr_t dvma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
David S. Miller16ce82d2007-04-26 21:08:21 -0700336 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700338 unsigned long flags, order, npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700341 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 iopte = iommu->page_table +
343 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
344
345 spin_lock_irqsave(&iommu->lock, flags);
346
David S. Millerd2841422008-02-08 18:05:46 -0800347 iommu_range_free(iommu, dvma, npages);
David S. Miller7c963ad2005-05-31 16:57:59 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 spin_unlock_irqrestore(&iommu->lock, flags);
350
351 order = get_order(size);
352 if (order < 10)
353 free_pages((unsigned long)cpu, order);
354}
355
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000356static dma_addr_t dma_4u_map_page(struct device *dev, struct page *page,
357 unsigned long offset, size_t sz,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900358 enum dma_data_direction direction,
359 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
David S. Miller16ce82d2007-04-26 21:08:21 -0700361 struct iommu *iommu;
362 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 iopte_t *base;
364 unsigned long flags, npages, oaddr;
365 unsigned long i, base_paddr, ctx;
366 u32 bus_addr, ret;
367 unsigned long iopte_protection;
368
David S. Millerad7ad572007-07-27 22:39:14 -0700369 iommu = dev->archdata.iommu;
370 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
David S. Millerad7ad572007-07-27 22:39:14 -0700372 if (unlikely(direction == DMA_NONE))
David S. Miller688cb302005-10-13 22:15:24 -0700373 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000375 oaddr = (unsigned long)(page_address(page) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
377 npages >>= IO_PAGE_SHIFT;
378
379 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800380 base = alloc_npages(dev, iommu, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700381 ctx = 0;
382 if (iommu->iommu_ctxflush)
383 ctx = iommu_alloc_ctx(iommu);
384 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
David S. Miller688cb302005-10-13 22:15:24 -0700386 if (unlikely(!base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 bus_addr = (iommu->page_table_map_base +
390 ((base - iommu->page_table) << IO_PAGE_SHIFT));
391 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
392 base_paddr = __pa(oaddr & IO_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (strbuf->strbuf_enabled)
394 iopte_protection = IOPTE_STREAMING(ctx);
395 else
396 iopte_protection = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700397 if (direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 iopte_protection |= IOPTE_WRITE;
399
400 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
401 iopte_val(*base) = iopte_protection | base_paddr;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return ret;
404
405bad:
David S. Miller688cb302005-10-13 22:15:24 -0700406 iommu_free_ctx(iommu, ctx);
407bad_no_ctx:
408 if (printk_ratelimit())
409 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700410 return DMA_ERROR_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
David S. Millerad7ad572007-07-27 22:39:14 -0700413static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
414 u32 vaddr, unsigned long ctx, unsigned long npages,
415 enum dma_data_direction direction)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700416{
417 int limit;
418
David S. Miller4dbc30f2005-05-11 11:37:00 -0700419 if (strbuf->strbuf_ctxflush &&
420 iommu->iommu_ctxflush) {
421 unsigned long matchreg, flushreg;
David S. Miller7c963ad2005-05-31 16:57:59 -0700422 u64 val;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700423
424 flushreg = strbuf->strbuf_ctxflush;
David S. Millerad7ad572007-07-27 22:39:14 -0700425 matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700426
David S. Millerad7ad572007-07-27 22:39:14 -0700427 iommu_write(flushreg, ctx);
428 val = iommu_read(matchreg);
David S. Miller88314ee2005-05-31 19:13:52 -0700429 val &= 0xffff;
430 if (!val)
David S. Miller7c963ad2005-05-31 16:57:59 -0700431 goto do_flush_sync;
432
David S. Miller7c963ad2005-05-31 16:57:59 -0700433 while (val) {
434 if (val & 0x1)
David S. Millerad7ad572007-07-27 22:39:14 -0700435 iommu_write(flushreg, ctx);
David S. Miller7c963ad2005-05-31 16:57:59 -0700436 val >>= 1;
David S. Millera228dfd2005-05-20 11:40:32 -0700437 }
David S. Millerad7ad572007-07-27 22:39:14 -0700438 val = iommu_read(matchreg);
David S. Miller7c963ad2005-05-31 16:57:59 -0700439 if (unlikely(val)) {
David S. Millerad7ad572007-07-27 22:39:14 -0700440 printk(KERN_WARNING "strbuf_flush: ctx flush "
Sam Ravnborg90181132009-01-06 13:19:28 -0800441 "timeout matchreg[%llx] ctx[%lx]\n",
David S. Miller7c963ad2005-05-31 16:57:59 -0700442 val, ctx);
443 goto do_page_flush;
444 }
David S. Miller4dbc30f2005-05-11 11:37:00 -0700445 } else {
446 unsigned long i;
447
David S. Miller7c963ad2005-05-31 16:57:59 -0700448 do_page_flush:
David S. Miller4dbc30f2005-05-11 11:37:00 -0700449 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
David S. Millerad7ad572007-07-27 22:39:14 -0700450 iommu_write(strbuf->strbuf_pflush, vaddr);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700451 }
452
David S. Miller7c963ad2005-05-31 16:57:59 -0700453do_flush_sync:
454 /* If the device could not have possibly put dirty data into
455 * the streaming cache, no flush-flag synchronization needs
456 * to be performed.
457 */
David S. Millerad7ad572007-07-27 22:39:14 -0700458 if (direction == DMA_TO_DEVICE)
David S. Miller7c963ad2005-05-31 16:57:59 -0700459 return;
460
David S. Millerad7ad572007-07-27 22:39:14 -0700461 STC_FLUSHFLAG_INIT(strbuf);
462 iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
463 (void) iommu_read(iommu->write_complete_reg);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700464
David S. Millera228dfd2005-05-20 11:40:32 -0700465 limit = 100000;
David S. Millerad7ad572007-07-27 22:39:14 -0700466 while (!STC_FLUSHFLAG_SET(strbuf)) {
David S. Miller4dbc30f2005-05-11 11:37:00 -0700467 limit--;
468 if (!limit)
469 break;
David S. Millera228dfd2005-05-20 11:40:32 -0700470 udelay(1);
David S. Miller4f071182005-08-29 12:46:22 -0700471 rmb();
David S. Miller4dbc30f2005-05-11 11:37:00 -0700472 }
473 if (!limit)
David S. Millerad7ad572007-07-27 22:39:14 -0700474 printk(KERN_WARNING "strbuf_flush: flushflag timeout "
David S. Miller4dbc30f2005-05-11 11:37:00 -0700475 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
476 vaddr, ctx, npages);
477}
478
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000479static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900480 size_t sz, enum dma_data_direction direction,
481 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
David S. Miller16ce82d2007-04-26 21:08:21 -0700483 struct iommu *iommu;
484 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 iopte_t *base;
David S. Miller688cb302005-10-13 22:15:24 -0700486 unsigned long flags, npages, ctx, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
David S. Millerad7ad572007-07-27 22:39:14 -0700488 if (unlikely(direction == DMA_NONE)) {
David S. Miller688cb302005-10-13 22:15:24 -0700489 if (printk_ratelimit())
490 WARN_ON(1);
491 return;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
David S. Millerad7ad572007-07-27 22:39:14 -0700494 iommu = dev->archdata.iommu;
495 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
498 npages >>= IO_PAGE_SHIFT;
499 base = iommu->page_table +
500 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 bus_addr &= IO_PAGE_MASK;
502
503 spin_lock_irqsave(&iommu->lock, flags);
504
505 /* Record the context, if any. */
506 ctx = 0;
507 if (iommu->iommu_ctxflush)
508 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
509
510 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700511 if (strbuf->strbuf_enabled)
David S. Millerad7ad572007-07-27 22:39:14 -0700512 strbuf_flush(strbuf, iommu, bus_addr, ctx,
513 npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
David S. Miller688cb302005-10-13 22:15:24 -0700515 /* Step 2: Clear out TSB entries. */
516 for (i = 0; i < npages; i++)
517 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
David S. Millerd2841422008-02-08 18:05:46 -0800519 iommu_range_free(iommu, bus_addr, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
David S. Miller7c963ad2005-05-31 16:57:59 -0700521 iommu_free_ctx(iommu, ctx);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 spin_unlock_irqrestore(&iommu->lock, flags);
524}
525
David S. Millerad7ad572007-07-27 22:39:14 -0700526static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900527 int nelems, enum dma_data_direction direction,
528 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
David S. Miller13fa14e2008-02-09 03:11:01 -0800530 struct scatterlist *s, *outs, *segstart;
531 unsigned long flags, handle, prot, ctx;
532 dma_addr_t dma_next = 0, dma_addr;
533 unsigned int max_seg_size;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700534 unsigned long seg_boundary_size;
David S. Miller13fa14e2008-02-09 03:11:01 -0800535 int outcount, incount, i;
David S. Miller16ce82d2007-04-26 21:08:21 -0700536 struct strbuf *strbuf;
David S. Miller38192d52008-02-06 03:50:26 -0800537 struct iommu *iommu;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700538 unsigned long base_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
David S. Miller13fa14e2008-02-09 03:11:01 -0800540 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
David S. Millerad7ad572007-07-27 22:39:14 -0700542 iommu = dev->archdata.iommu;
543 strbuf = dev->archdata.stc;
David S. Miller13fa14e2008-02-09 03:11:01 -0800544 if (nelems == 0 || !iommu)
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 spin_lock_irqsave(&iommu->lock, flags);
548
David S. Miller688cb302005-10-13 22:15:24 -0700549 ctx = 0;
550 if (iommu->iommu_ctxflush)
551 ctx = iommu_alloc_ctx(iommu);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (strbuf->strbuf_enabled)
David S. Miller13fa14e2008-02-09 03:11:01 -0800554 prot = IOPTE_STREAMING(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 else
David S. Miller13fa14e2008-02-09 03:11:01 -0800556 prot = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700557 if (direction != DMA_TO_DEVICE)
David S. Miller13fa14e2008-02-09 03:11:01 -0800558 prot |= IOPTE_WRITE;
David S. Miller688cb302005-10-13 22:15:24 -0700559
David S. Miller13fa14e2008-02-09 03:11:01 -0800560 outs = s = segstart = &sglist[0];
561 outcount = 1;
562 incount = nelems;
563 handle = 0;
David S. Miller688cb302005-10-13 22:15:24 -0700564
David S. Miller13fa14e2008-02-09 03:11:01 -0800565 /* Init first segment length for backout at failure */
566 outs->dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
David S. Miller13fa14e2008-02-09 03:11:01 -0800568 max_seg_size = dma_get_max_seg_size(dev);
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700569 seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
570 IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
571 base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
David S. Miller13fa14e2008-02-09 03:11:01 -0800572 for_each_sg(sglist, s, nelems, i) {
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700573 unsigned long paddr, npages, entry, out_entry = 0, slen;
David S. Miller13fa14e2008-02-09 03:11:01 -0800574 iopte_t *base;
David S. Miller38192d52008-02-06 03:50:26 -0800575
David S. Miller13fa14e2008-02-09 03:11:01 -0800576 slen = s->length;
577 /* Sanity check */
578 if (slen == 0) {
579 dma_next = 0;
580 continue;
581 }
582 /* Allocate iommu entries for that segment */
583 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
Joerg Roedel0fcff282008-10-15 22:02:14 -0700584 npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800585 entry = iommu_range_alloc(dev, iommu, npages, &handle);
586
587 /* Handle failure */
588 if (unlikely(entry == DMA_ERROR_CODE)) {
589 if (printk_ratelimit())
590 printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
591 " npages %lx\n", iommu, paddr, npages);
592 goto iommu_map_failed;
593 }
594
595 base = iommu->page_table + entry;
596
597 /* Convert entry to a dma_addr_t */
598 dma_addr = iommu->page_table_map_base +
599 (entry << IO_PAGE_SHIFT);
600 dma_addr |= (s->offset & ~IO_PAGE_MASK);
601
602 /* Insert into HW table */
David S. Miller38192d52008-02-06 03:50:26 -0800603 paddr &= IO_PAGE_MASK;
David S. Miller13fa14e2008-02-09 03:11:01 -0800604 while (npages--) {
605 iopte_val(*base) = prot | paddr;
David S. Miller38192d52008-02-06 03:50:26 -0800606 base++;
607 paddr += IO_PAGE_SIZE;
David S. Miller38192d52008-02-06 03:50:26 -0800608 }
David S. Miller13fa14e2008-02-09 03:11:01 -0800609
610 /* If we are in an open segment, try merging */
611 if (segstart != s) {
612 /* We cannot merge if:
613 * - allocated dma_addr isn't contiguous to previous allocation
614 */
615 if ((dma_addr != dma_next) ||
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700616 (outs->dma_length + s->length > max_seg_size) ||
617 (is_span_boundary(out_entry, base_shift,
618 seg_boundary_size, outs, s))) {
David S. Miller13fa14e2008-02-09 03:11:01 -0800619 /* Can't merge: create a new segment */
620 segstart = s;
621 outcount++;
622 outs = sg_next(outs);
623 } else {
624 outs->dma_length += s->length;
625 }
626 }
627
628 if (segstart == s) {
629 /* This is a new segment, fill entries */
630 outs->dma_address = dma_addr;
631 outs->dma_length = slen;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700632 out_entry = entry;
David S. Miller13fa14e2008-02-09 03:11:01 -0800633 }
634
635 /* Calculate next page pointer for contiguous check */
636 dma_next = dma_addr + slen;
David S. Miller38192d52008-02-06 03:50:26 -0800637 }
638
David S. Miller13fa14e2008-02-09 03:11:01 -0800639 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
David S. Miller13fa14e2008-02-09 03:11:01 -0800641 if (outcount < incount) {
642 outs = sg_next(outs);
643 outs->dma_address = DMA_ERROR_CODE;
644 outs->dma_length = 0;
645 }
646
647 return outcount;
648
649iommu_map_failed:
650 for_each_sg(sglist, s, nelems, i) {
651 if (s->dma_length != 0) {
David S. Miller6c830fe2008-03-25 22:44:10 -0700652 unsigned long vaddr, npages, entry, j;
David S. Miller13fa14e2008-02-09 03:11:01 -0800653 iopte_t *base;
654
655 vaddr = s->dma_address & IO_PAGE_MASK;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700656 npages = iommu_num_pages(s->dma_address, s->dma_length,
657 IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800658 iommu_range_free(iommu, vaddr, npages);
659
660 entry = (vaddr - iommu->page_table_map_base)
661 >> IO_PAGE_SHIFT;
662 base = iommu->page_table + entry;
663
David S. Miller6c830fe2008-03-25 22:44:10 -0700664 for (j = 0; j < npages; j++)
665 iopte_make_dummy(iommu, base + j);
David S. Miller13fa14e2008-02-09 03:11:01 -0800666
667 s->dma_address = DMA_ERROR_CODE;
668 s->dma_length = 0;
669 }
670 if (s == outs)
671 break;
672 }
673 spin_unlock_irqrestore(&iommu->lock, flags);
674
David S. Miller688cb302005-10-13 22:15:24 -0700675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
David S. Miller13fa14e2008-02-09 03:11:01 -0800678/* If contexts are being used, they are the same in all of the mappings
679 * we make for a particular SG.
680 */
681static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
682{
683 unsigned long ctx = 0;
684
685 if (iommu->iommu_ctxflush) {
686 iopte_t *base;
687 u32 bus_addr;
688
689 bus_addr = sg->dma_address & IO_PAGE_MASK;
690 base = iommu->page_table +
691 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
692
693 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
694 }
695 return ctx;
696}
697
David S. Millerad7ad572007-07-27 22:39:14 -0700698static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900699 int nelems, enum dma_data_direction direction,
700 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
David S. Miller13fa14e2008-02-09 03:11:01 -0800702 unsigned long flags, ctx;
703 struct scatterlist *sg;
David S. Miller38192d52008-02-06 03:50:26 -0800704 struct strbuf *strbuf;
705 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
David S. Miller13fa14e2008-02-09 03:11:01 -0800707 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
David S. Millerad7ad572007-07-27 22:39:14 -0700709 iommu = dev->archdata.iommu;
710 strbuf = dev->archdata.stc;
711
David S. Miller13fa14e2008-02-09 03:11:01 -0800712 ctx = fetch_sg_ctx(iommu, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 spin_lock_irqsave(&iommu->lock, flags);
715
David S. Miller13fa14e2008-02-09 03:11:01 -0800716 sg = sglist;
717 while (nelems--) {
718 dma_addr_t dma_handle = sg->dma_address;
719 unsigned int len = sg->dma_length;
720 unsigned long npages, entry;
721 iopte_t *base;
722 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
David S. Miller13fa14e2008-02-09 03:11:01 -0800724 if (!len)
725 break;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700726 npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800727 iommu_range_free(iommu, dma_handle, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
David S. Miller13fa14e2008-02-09 03:11:01 -0800729 entry = ((dma_handle - iommu->page_table_map_base)
730 >> IO_PAGE_SHIFT);
731 base = iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
David S. Miller13fa14e2008-02-09 03:11:01 -0800733 dma_handle &= IO_PAGE_MASK;
734 if (strbuf->strbuf_enabled)
735 strbuf_flush(strbuf, iommu, dma_handle, ctx,
736 npages, direction);
737
738 for (i = 0; i < npages; i++)
739 iopte_make_dummy(iommu, base + i);
740
741 sg = sg_next(sg);
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
David S. Miller7c963ad2005-05-31 16:57:59 -0700744 iommu_free_ctx(iommu, ctx);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 spin_unlock_irqrestore(&iommu->lock, flags);
747}
748
David S. Millerad7ad572007-07-27 22:39:14 -0700749static void dma_4u_sync_single_for_cpu(struct device *dev,
750 dma_addr_t bus_addr, size_t sz,
751 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
David S. Miller16ce82d2007-04-26 21:08:21 -0700753 struct iommu *iommu;
754 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 unsigned long flags, ctx, npages;
756
David S. Millerad7ad572007-07-27 22:39:14 -0700757 iommu = dev->archdata.iommu;
758 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (!strbuf->strbuf_enabled)
761 return;
762
763 spin_lock_irqsave(&iommu->lock, flags);
764
765 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
766 npages >>= IO_PAGE_SHIFT;
767 bus_addr &= IO_PAGE_MASK;
768
769 /* Step 1: Record the context, if any. */
770 ctx = 0;
771 if (iommu->iommu_ctxflush &&
772 strbuf->strbuf_ctxflush) {
773 iopte_t *iopte;
774
775 iopte = iommu->page_table +
776 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
777 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
778 }
779
780 /* Step 2: Kick data out of streaming buffers. */
David S. Millerad7ad572007-07-27 22:39:14 -0700781 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 spin_unlock_irqrestore(&iommu->lock, flags);
784}
785
David S. Millerad7ad572007-07-27 22:39:14 -0700786static void dma_4u_sync_sg_for_cpu(struct device *dev,
787 struct scatterlist *sglist, int nelems,
788 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
David S. Miller16ce82d2007-04-26 21:08:21 -0700790 struct iommu *iommu;
791 struct strbuf *strbuf;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700792 unsigned long flags, ctx, npages, i;
Jens Axboe2c941a22007-08-07 09:37:10 +0200793 struct scatterlist *sg, *sgprv;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700794 u32 bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
David S. Millerad7ad572007-07-27 22:39:14 -0700796 iommu = dev->archdata.iommu;
797 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 if (!strbuf->strbuf_enabled)
800 return;
801
802 spin_lock_irqsave(&iommu->lock, flags);
803
804 /* Step 1: Record the context, if any. */
805 ctx = 0;
806 if (iommu->iommu_ctxflush &&
807 strbuf->strbuf_ctxflush) {
808 iopte_t *iopte;
809
810 iopte = iommu->page_table +
811 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
812 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
813 }
814
815 /* Step 2: Kick data out of streaming buffers. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700816 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
Jens Axboe2c941a22007-08-07 09:37:10 +0200817 sgprv = NULL;
818 for_each_sg(sglist, sg, nelems, i) {
819 if (sg->dma_length == 0)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700820 break;
Jens Axboe2c941a22007-08-07 09:37:10 +0200821 sgprv = sg;
822 }
823
824 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700825 - bus_addr) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700826 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 spin_unlock_irqrestore(&iommu->lock, flags);
829}
830
FUJITA Tomonori02f7a182009-08-10 11:53:13 +0900831static struct dma_map_ops sun4u_dma_ops = {
David S. Millerad7ad572007-07-27 22:39:14 -0700832 .alloc_coherent = dma_4u_alloc_coherent,
833 .free_coherent = dma_4u_free_coherent,
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000834 .map_page = dma_4u_map_page,
835 .unmap_page = dma_4u_unmap_page,
David S. Millerad7ad572007-07-27 22:39:14 -0700836 .map_sg = dma_4u_map_sg,
837 .unmap_sg = dma_4u_unmap_sg,
838 .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
839 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800840};
841
FUJITA Tomonori02f7a182009-08-10 11:53:13 +0900842struct dma_map_ops *dma_ops = &sun4u_dma_ops;
David S. Millerad7ad572007-07-27 22:39:14 -0700843EXPORT_SYMBOL(dma_ops);
844
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900845extern int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask);
846
David S. Millerad7ad572007-07-27 22:39:14 -0700847int dma_supported(struct device *dev, u64 device_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
David S. Millerad7ad572007-07-27 22:39:14 -0700849 struct iommu *iommu = dev->archdata.iommu;
850 u64 dma_addr_mask = iommu->dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (device_mask >= (1UL << 32UL))
853 return 0;
854
David S. Millerad7ad572007-07-27 22:39:14 -0700855 if ((device_mask & dma_addr_mask) == dma_addr_mask)
856 return 1;
857
858#ifdef CONFIG_PCI
859 if (dev->bus == &pci_bus_type)
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900860 return pci64_dma_supported(to_pci_dev(dev), device_mask);
David S. Millerad7ad572007-07-27 22:39:14 -0700861#endif
862
863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
David S. Millerad7ad572007-07-27 22:39:14 -0700865EXPORT_SYMBOL(dma_supported);