blob: 070ed141aac79728da15f406289219a9c385fe27 [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>
Paul Gortmaker066bcac2011-07-22 13:18:16 -04008#include <linux/export.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;
Akinobu Mita711c71a2011-02-08 04:59:50 +0000258 int n = find_next_zero_bit(iommu->ctx_bitmap, IOMMU_NUM_CTXS, lowest);
David S. Miller7c963ad2005-05-31 16:57:59 -0700259
Akinobu Mita711c71a2011-02-08 04:59:50 +0000260 if (unlikely(n == IOMMU_NUM_CTXS)) {
David S. Miller7c963ad2005-05-31 16:57:59 -0700261 n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
262 if (unlikely(n == lowest)) {
263 printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
264 n = 0;
265 }
266 }
267 if (n)
268 __set_bit(n, iommu->ctx_bitmap);
269
270 return n;
271}
272
David S. Miller16ce82d2007-04-26 21:08:21 -0700273static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
David S. Miller7c963ad2005-05-31 16:57:59 -0700274{
275 if (likely(ctx)) {
276 __clear_bit(ctx, iommu->ctx_bitmap);
277 if (ctx < iommu->ctx_lowest_free)
278 iommu->ctx_lowest_free = ctx;
279 }
280}
281
David S. Millerad7ad572007-07-27 22:39:14 -0700282static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczc4162582012-03-27 14:56:55 +0200283 dma_addr_t *dma_addrp, gfp_t gfp,
284 struct dma_attrs *attrs)
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,
Andrzej Pietrasiewiczc4162582012-03-27 14:56:55 +0200334 void *cpu, dma_addr_t dvma,
335 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
David S. Miller16ce82d2007-04-26 21:08:21 -0700337 struct iommu *iommu;
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
343 spin_lock_irqsave(&iommu->lock, flags);
344
David S. Millerd2841422008-02-08 18:05:46 -0800345 iommu_range_free(iommu, dvma, npages);
David S. Miller7c963ad2005-05-31 16:57:59 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_unlock_irqrestore(&iommu->lock, flags);
348
349 order = get_order(size);
350 if (order < 10)
351 free_pages((unsigned long)cpu, order);
352}
353
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000354static dma_addr_t dma_4u_map_page(struct device *dev, struct page *page,
355 unsigned long offset, size_t sz,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900356 enum dma_data_direction direction,
357 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
David S. Miller16ce82d2007-04-26 21:08:21 -0700359 struct iommu *iommu;
360 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 iopte_t *base;
362 unsigned long flags, npages, oaddr;
363 unsigned long i, base_paddr, ctx;
364 u32 bus_addr, ret;
365 unsigned long iopte_protection;
366
David S. Millerad7ad572007-07-27 22:39:14 -0700367 iommu = dev->archdata.iommu;
368 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
David S. Millerad7ad572007-07-27 22:39:14 -0700370 if (unlikely(direction == DMA_NONE))
David S. Miller688cb302005-10-13 22:15:24 -0700371 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000373 oaddr = (unsigned long)(page_address(page) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
375 npages >>= IO_PAGE_SHIFT;
376
377 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800378 base = alloc_npages(dev, iommu, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700379 ctx = 0;
380 if (iommu->iommu_ctxflush)
381 ctx = iommu_alloc_ctx(iommu);
382 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
David S. Miller688cb302005-10-13 22:15:24 -0700384 if (unlikely(!base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 bus_addr = (iommu->page_table_map_base +
388 ((base - iommu->page_table) << IO_PAGE_SHIFT));
389 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
390 base_paddr = __pa(oaddr & IO_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (strbuf->strbuf_enabled)
392 iopte_protection = IOPTE_STREAMING(ctx);
393 else
394 iopte_protection = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700395 if (direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 iopte_protection |= IOPTE_WRITE;
397
398 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
399 iopte_val(*base) = iopte_protection | base_paddr;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return ret;
402
403bad:
David S. Miller688cb302005-10-13 22:15:24 -0700404 iommu_free_ctx(iommu, ctx);
405bad_no_ctx:
406 if (printk_ratelimit())
407 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700408 return DMA_ERROR_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
David S. Millerad7ad572007-07-27 22:39:14 -0700411static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
412 u32 vaddr, unsigned long ctx, unsigned long npages,
413 enum dma_data_direction direction)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700414{
415 int limit;
416
David S. Miller4dbc30f2005-05-11 11:37:00 -0700417 if (strbuf->strbuf_ctxflush &&
418 iommu->iommu_ctxflush) {
419 unsigned long matchreg, flushreg;
David S. Miller7c963ad2005-05-31 16:57:59 -0700420 u64 val;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700421
422 flushreg = strbuf->strbuf_ctxflush;
David S. Millerad7ad572007-07-27 22:39:14 -0700423 matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700424
David S. Millerad7ad572007-07-27 22:39:14 -0700425 iommu_write(flushreg, ctx);
426 val = iommu_read(matchreg);
David S. Miller88314ee2005-05-31 19:13:52 -0700427 val &= 0xffff;
428 if (!val)
David S. Miller7c963ad2005-05-31 16:57:59 -0700429 goto do_flush_sync;
430
David S. Miller7c963ad2005-05-31 16:57:59 -0700431 while (val) {
432 if (val & 0x1)
David S. Millerad7ad572007-07-27 22:39:14 -0700433 iommu_write(flushreg, ctx);
David S. Miller7c963ad2005-05-31 16:57:59 -0700434 val >>= 1;
David S. Millera228dfd2005-05-20 11:40:32 -0700435 }
David S. Millerad7ad572007-07-27 22:39:14 -0700436 val = iommu_read(matchreg);
David S. Miller7c963ad2005-05-31 16:57:59 -0700437 if (unlikely(val)) {
David S. Millerad7ad572007-07-27 22:39:14 -0700438 printk(KERN_WARNING "strbuf_flush: ctx flush "
Sam Ravnborg90181132009-01-06 13:19:28 -0800439 "timeout matchreg[%llx] ctx[%lx]\n",
David S. Miller7c963ad2005-05-31 16:57:59 -0700440 val, ctx);
441 goto do_page_flush;
442 }
David S. Miller4dbc30f2005-05-11 11:37:00 -0700443 } else {
444 unsigned long i;
445
David S. Miller7c963ad2005-05-31 16:57:59 -0700446 do_page_flush:
David S. Miller4dbc30f2005-05-11 11:37:00 -0700447 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
David S. Millerad7ad572007-07-27 22:39:14 -0700448 iommu_write(strbuf->strbuf_pflush, vaddr);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700449 }
450
David S. Miller7c963ad2005-05-31 16:57:59 -0700451do_flush_sync:
452 /* If the device could not have possibly put dirty data into
453 * the streaming cache, no flush-flag synchronization needs
454 * to be performed.
455 */
David S. Millerad7ad572007-07-27 22:39:14 -0700456 if (direction == DMA_TO_DEVICE)
David S. Miller7c963ad2005-05-31 16:57:59 -0700457 return;
458
David S. Millerad7ad572007-07-27 22:39:14 -0700459 STC_FLUSHFLAG_INIT(strbuf);
460 iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
461 (void) iommu_read(iommu->write_complete_reg);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700462
David S. Millera228dfd2005-05-20 11:40:32 -0700463 limit = 100000;
David S. Millerad7ad572007-07-27 22:39:14 -0700464 while (!STC_FLUSHFLAG_SET(strbuf)) {
David S. Miller4dbc30f2005-05-11 11:37:00 -0700465 limit--;
466 if (!limit)
467 break;
David S. Millera228dfd2005-05-20 11:40:32 -0700468 udelay(1);
David S. Miller4f071182005-08-29 12:46:22 -0700469 rmb();
David S. Miller4dbc30f2005-05-11 11:37:00 -0700470 }
471 if (!limit)
David S. Millerad7ad572007-07-27 22:39:14 -0700472 printk(KERN_WARNING "strbuf_flush: flushflag timeout "
David S. Miller4dbc30f2005-05-11 11:37:00 -0700473 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
474 vaddr, ctx, npages);
475}
476
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000477static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900478 size_t sz, enum dma_data_direction direction,
479 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
David S. Miller16ce82d2007-04-26 21:08:21 -0700481 struct iommu *iommu;
482 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 iopte_t *base;
David S. Miller688cb302005-10-13 22:15:24 -0700484 unsigned long flags, npages, ctx, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
David S. Millerad7ad572007-07-27 22:39:14 -0700486 if (unlikely(direction == DMA_NONE)) {
David S. Miller688cb302005-10-13 22:15:24 -0700487 if (printk_ratelimit())
488 WARN_ON(1);
489 return;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David S. Millerad7ad572007-07-27 22:39:14 -0700492 iommu = dev->archdata.iommu;
493 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
496 npages >>= IO_PAGE_SHIFT;
497 base = iommu->page_table +
498 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 bus_addr &= IO_PAGE_MASK;
500
501 spin_lock_irqsave(&iommu->lock, flags);
502
503 /* Record the context, if any. */
504 ctx = 0;
505 if (iommu->iommu_ctxflush)
506 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
507
508 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700509 if (strbuf->strbuf_enabled)
David S. Millerad7ad572007-07-27 22:39:14 -0700510 strbuf_flush(strbuf, iommu, bus_addr, ctx,
511 npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
David S. Miller688cb302005-10-13 22:15:24 -0700513 /* Step 2: Clear out TSB entries. */
514 for (i = 0; i < npages; i++)
515 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
David S. Millerd2841422008-02-08 18:05:46 -0800517 iommu_range_free(iommu, bus_addr, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
David S. Miller7c963ad2005-05-31 16:57:59 -0700519 iommu_free_ctx(iommu, ctx);
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 spin_unlock_irqrestore(&iommu->lock, flags);
522}
523
David S. Millerad7ad572007-07-27 22:39:14 -0700524static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900525 int nelems, enum dma_data_direction direction,
526 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
David S. Miller13fa14e2008-02-09 03:11:01 -0800528 struct scatterlist *s, *outs, *segstart;
529 unsigned long flags, handle, prot, ctx;
530 dma_addr_t dma_next = 0, dma_addr;
531 unsigned int max_seg_size;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700532 unsigned long seg_boundary_size;
David S. Miller13fa14e2008-02-09 03:11:01 -0800533 int outcount, incount, i;
David S. Miller16ce82d2007-04-26 21:08:21 -0700534 struct strbuf *strbuf;
David S. Miller38192d52008-02-06 03:50:26 -0800535 struct iommu *iommu;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700536 unsigned long base_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
David S. Miller13fa14e2008-02-09 03:11:01 -0800538 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
David S. Millerad7ad572007-07-27 22:39:14 -0700540 iommu = dev->archdata.iommu;
541 strbuf = dev->archdata.stc;
David S. Miller13fa14e2008-02-09 03:11:01 -0800542 if (nelems == 0 || !iommu)
543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 spin_lock_irqsave(&iommu->lock, flags);
546
David S. Miller688cb302005-10-13 22:15:24 -0700547 ctx = 0;
548 if (iommu->iommu_ctxflush)
549 ctx = iommu_alloc_ctx(iommu);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (strbuf->strbuf_enabled)
David S. Miller13fa14e2008-02-09 03:11:01 -0800552 prot = IOPTE_STREAMING(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 else
David S. Miller13fa14e2008-02-09 03:11:01 -0800554 prot = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700555 if (direction != DMA_TO_DEVICE)
David S. Miller13fa14e2008-02-09 03:11:01 -0800556 prot |= IOPTE_WRITE;
David S. Miller688cb302005-10-13 22:15:24 -0700557
David S. Miller13fa14e2008-02-09 03:11:01 -0800558 outs = s = segstart = &sglist[0];
559 outcount = 1;
560 incount = nelems;
561 handle = 0;
David S. Miller688cb302005-10-13 22:15:24 -0700562
David S. Miller13fa14e2008-02-09 03:11:01 -0800563 /* Init first segment length for backout at failure */
564 outs->dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
David S. Miller13fa14e2008-02-09 03:11:01 -0800566 max_seg_size = dma_get_max_seg_size(dev);
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700567 seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
568 IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
569 base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
David S. Miller13fa14e2008-02-09 03:11:01 -0800570 for_each_sg(sglist, s, nelems, i) {
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700571 unsigned long paddr, npages, entry, out_entry = 0, slen;
David S. Miller13fa14e2008-02-09 03:11:01 -0800572 iopte_t *base;
David S. Miller38192d52008-02-06 03:50:26 -0800573
David S. Miller13fa14e2008-02-09 03:11:01 -0800574 slen = s->length;
575 /* Sanity check */
576 if (slen == 0) {
577 dma_next = 0;
578 continue;
579 }
580 /* Allocate iommu entries for that segment */
581 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
Joerg Roedel0fcff282008-10-15 22:02:14 -0700582 npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800583 entry = iommu_range_alloc(dev, iommu, npages, &handle);
584
585 /* Handle failure */
586 if (unlikely(entry == DMA_ERROR_CODE)) {
587 if (printk_ratelimit())
588 printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
589 " npages %lx\n", iommu, paddr, npages);
590 goto iommu_map_failed;
591 }
592
593 base = iommu->page_table + entry;
594
595 /* Convert entry to a dma_addr_t */
596 dma_addr = iommu->page_table_map_base +
597 (entry << IO_PAGE_SHIFT);
598 dma_addr |= (s->offset & ~IO_PAGE_MASK);
599
600 /* Insert into HW table */
David S. Miller38192d52008-02-06 03:50:26 -0800601 paddr &= IO_PAGE_MASK;
David S. Miller13fa14e2008-02-09 03:11:01 -0800602 while (npages--) {
603 iopte_val(*base) = prot | paddr;
David S. Miller38192d52008-02-06 03:50:26 -0800604 base++;
605 paddr += IO_PAGE_SIZE;
David S. Miller38192d52008-02-06 03:50:26 -0800606 }
David S. Miller13fa14e2008-02-09 03:11:01 -0800607
608 /* If we are in an open segment, try merging */
609 if (segstart != s) {
610 /* We cannot merge if:
611 * - allocated dma_addr isn't contiguous to previous allocation
612 */
613 if ((dma_addr != dma_next) ||
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700614 (outs->dma_length + s->length > max_seg_size) ||
615 (is_span_boundary(out_entry, base_shift,
616 seg_boundary_size, outs, s))) {
David S. Miller13fa14e2008-02-09 03:11:01 -0800617 /* Can't merge: create a new segment */
618 segstart = s;
619 outcount++;
620 outs = sg_next(outs);
621 } else {
622 outs->dma_length += s->length;
623 }
624 }
625
626 if (segstart == s) {
627 /* This is a new segment, fill entries */
628 outs->dma_address = dma_addr;
629 outs->dma_length = slen;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700630 out_entry = entry;
David S. Miller13fa14e2008-02-09 03:11:01 -0800631 }
632
633 /* Calculate next page pointer for contiguous check */
634 dma_next = dma_addr + slen;
David S. Miller38192d52008-02-06 03:50:26 -0800635 }
636
David S. Miller13fa14e2008-02-09 03:11:01 -0800637 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David S. Miller13fa14e2008-02-09 03:11:01 -0800639 if (outcount < incount) {
640 outs = sg_next(outs);
641 outs->dma_address = DMA_ERROR_CODE;
642 outs->dma_length = 0;
643 }
644
645 return outcount;
646
647iommu_map_failed:
648 for_each_sg(sglist, s, nelems, i) {
649 if (s->dma_length != 0) {
David S. Miller6c830fe2008-03-25 22:44:10 -0700650 unsigned long vaddr, npages, entry, j;
David S. Miller13fa14e2008-02-09 03:11:01 -0800651 iopte_t *base;
652
653 vaddr = s->dma_address & IO_PAGE_MASK;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700654 npages = iommu_num_pages(s->dma_address, s->dma_length,
655 IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800656 iommu_range_free(iommu, vaddr, npages);
657
658 entry = (vaddr - iommu->page_table_map_base)
659 >> IO_PAGE_SHIFT;
660 base = iommu->page_table + entry;
661
David S. Miller6c830fe2008-03-25 22:44:10 -0700662 for (j = 0; j < npages; j++)
663 iopte_make_dummy(iommu, base + j);
David S. Miller13fa14e2008-02-09 03:11:01 -0800664
665 s->dma_address = DMA_ERROR_CODE;
666 s->dma_length = 0;
667 }
668 if (s == outs)
669 break;
670 }
671 spin_unlock_irqrestore(&iommu->lock, flags);
672
David S. Miller688cb302005-10-13 22:15:24 -0700673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
David S. Miller13fa14e2008-02-09 03:11:01 -0800676/* If contexts are being used, they are the same in all of the mappings
677 * we make for a particular SG.
678 */
679static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
680{
681 unsigned long ctx = 0;
682
683 if (iommu->iommu_ctxflush) {
684 iopte_t *base;
685 u32 bus_addr;
686
687 bus_addr = sg->dma_address & IO_PAGE_MASK;
688 base = iommu->page_table +
689 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
690
691 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
692 }
693 return ctx;
694}
695
David S. Millerad7ad572007-07-27 22:39:14 -0700696static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900697 int nelems, enum dma_data_direction direction,
698 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
David S. Miller13fa14e2008-02-09 03:11:01 -0800700 unsigned long flags, ctx;
701 struct scatterlist *sg;
David S. Miller38192d52008-02-06 03:50:26 -0800702 struct strbuf *strbuf;
703 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
David S. Miller13fa14e2008-02-09 03:11:01 -0800705 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
David S. Millerad7ad572007-07-27 22:39:14 -0700707 iommu = dev->archdata.iommu;
708 strbuf = dev->archdata.stc;
709
David S. Miller13fa14e2008-02-09 03:11:01 -0800710 ctx = fetch_sg_ctx(iommu, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 spin_lock_irqsave(&iommu->lock, flags);
713
David S. Miller13fa14e2008-02-09 03:11:01 -0800714 sg = sglist;
715 while (nelems--) {
716 dma_addr_t dma_handle = sg->dma_address;
717 unsigned int len = sg->dma_length;
718 unsigned long npages, entry;
719 iopte_t *base;
720 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
David S. Miller13fa14e2008-02-09 03:11:01 -0800722 if (!len)
723 break;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700724 npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800725 iommu_range_free(iommu, dma_handle, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
David S. Miller13fa14e2008-02-09 03:11:01 -0800727 entry = ((dma_handle - iommu->page_table_map_base)
728 >> IO_PAGE_SHIFT);
729 base = iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
David S. Miller13fa14e2008-02-09 03:11:01 -0800731 dma_handle &= IO_PAGE_MASK;
732 if (strbuf->strbuf_enabled)
733 strbuf_flush(strbuf, iommu, dma_handle, ctx,
734 npages, direction);
735
736 for (i = 0; i < npages; i++)
737 iopte_make_dummy(iommu, base + i);
738
739 sg = sg_next(sg);
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
David S. Miller7c963ad2005-05-31 16:57:59 -0700742 iommu_free_ctx(iommu, ctx);
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 spin_unlock_irqrestore(&iommu->lock, flags);
745}
746
David S. Millerad7ad572007-07-27 22:39:14 -0700747static void dma_4u_sync_single_for_cpu(struct device *dev,
748 dma_addr_t bus_addr, size_t sz,
749 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
David S. Miller16ce82d2007-04-26 21:08:21 -0700751 struct iommu *iommu;
752 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 unsigned long flags, ctx, npages;
754
David S. Millerad7ad572007-07-27 22:39:14 -0700755 iommu = dev->archdata.iommu;
756 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (!strbuf->strbuf_enabled)
759 return;
760
761 spin_lock_irqsave(&iommu->lock, flags);
762
763 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
764 npages >>= IO_PAGE_SHIFT;
765 bus_addr &= IO_PAGE_MASK;
766
767 /* Step 1: Record the context, if any. */
768 ctx = 0;
769 if (iommu->iommu_ctxflush &&
770 strbuf->strbuf_ctxflush) {
771 iopte_t *iopte;
772
773 iopte = iommu->page_table +
774 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
775 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
776 }
777
778 /* Step 2: Kick data out of streaming buffers. */
David S. Millerad7ad572007-07-27 22:39:14 -0700779 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 spin_unlock_irqrestore(&iommu->lock, flags);
782}
783
David S. Millerad7ad572007-07-27 22:39:14 -0700784static void dma_4u_sync_sg_for_cpu(struct device *dev,
785 struct scatterlist *sglist, int nelems,
786 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
David S. Miller16ce82d2007-04-26 21:08:21 -0700788 struct iommu *iommu;
789 struct strbuf *strbuf;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700790 unsigned long flags, ctx, npages, i;
Jens Axboe2c941a22007-08-07 09:37:10 +0200791 struct scatterlist *sg, *sgprv;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700792 u32 bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
David S. Millerad7ad572007-07-27 22:39:14 -0700794 iommu = dev->archdata.iommu;
795 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 if (!strbuf->strbuf_enabled)
798 return;
799
800 spin_lock_irqsave(&iommu->lock, flags);
801
802 /* Step 1: Record the context, if any. */
803 ctx = 0;
804 if (iommu->iommu_ctxflush &&
805 strbuf->strbuf_ctxflush) {
806 iopte_t *iopte;
807
808 iopte = iommu->page_table +
809 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
810 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
811 }
812
813 /* Step 2: Kick data out of streaming buffers. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700814 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
Jens Axboe2c941a22007-08-07 09:37:10 +0200815 sgprv = NULL;
816 for_each_sg(sglist, sg, nelems, i) {
817 if (sg->dma_length == 0)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700818 break;
Jens Axboe2c941a22007-08-07 09:37:10 +0200819 sgprv = sg;
820 }
821
822 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700823 - bus_addr) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700824 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 spin_unlock_irqrestore(&iommu->lock, flags);
827}
828
FUJITA Tomonori02f7a182009-08-10 11:53:13 +0900829static struct dma_map_ops sun4u_dma_ops = {
Andrzej Pietrasiewiczc4162582012-03-27 14:56:55 +0200830 .alloc = dma_4u_alloc_coherent,
831 .free = dma_4u_free_coherent,
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000832 .map_page = dma_4u_map_page,
833 .unmap_page = dma_4u_unmap_page,
David S. Millerad7ad572007-07-27 22:39:14 -0700834 .map_sg = dma_4u_map_sg,
835 .unmap_sg = dma_4u_unmap_sg,
836 .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
837 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800838};
839
FUJITA Tomonori02f7a182009-08-10 11:53:13 +0900840struct dma_map_ops *dma_ops = &sun4u_dma_ops;
David S. Millerad7ad572007-07-27 22:39:14 -0700841EXPORT_SYMBOL(dma_ops);
842
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900843extern int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask);
844
David S. Millerad7ad572007-07-27 22:39:14 -0700845int dma_supported(struct device *dev, u64 device_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
David S. Millerad7ad572007-07-27 22:39:14 -0700847 struct iommu *iommu = dev->archdata.iommu;
848 u64 dma_addr_mask = iommu->dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 if (device_mask >= (1UL << 32UL))
851 return 0;
852
David S. Millerad7ad572007-07-27 22:39:14 -0700853 if ((device_mask & dma_addr_mask) == dma_addr_mask)
854 return 1;
855
856#ifdef CONFIG_PCI
857 if (dev->bus == &pci_bus_type)
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900858 return pci64_dma_supported(to_pci_dev(dev), device_mask);
David S. Millerad7ad572007-07-27 22:39:14 -0700859#endif
860
861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
David S. Millerad7ad572007-07-27 22:39:14 -0700863EXPORT_SYMBOL(dma_supported);