blob: 8414549c1834e728e114ef4bdd9546de686b1a3e [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>
David S. Miller4dbc30f2005-05-11 11:37:00 -07009#include <linux/delay.h>
David S. Millerad7ad572007-07-27 22:39:14 -070010#include <linux/device.h>
11#include <linux/dma-mapping.h>
12#include <linux/errno.h>
David S. Millerd2841422008-02-08 18:05:46 -080013#include <linux/iommu-helper.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080014#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
David S. Millerad7ad572007-07-27 22:39:14 -070016#ifdef CONFIG_PCI
17#include <linux/pci.h>
18#endif
19
20#include <asm/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "iommu_common.h"
23
David S. Millerad7ad572007-07-27 22:39:14 -070024#define STC_CTXMATCH_ADDR(STC, CTX) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
David S. Millerad7ad572007-07-27 22:39:14 -070026#define STC_FLUSHFLAG_INIT(STC) \
27 (*((STC)->strbuf_flushflag) = 0UL)
28#define STC_FLUSHFLAG_SET(STC) \
29 (*((STC)->strbuf_flushflag) != 0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
David S. Millerad7ad572007-07-27 22:39:14 -070031#define iommu_read(__reg) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070032({ u64 __ret; \
33 __asm__ __volatile__("ldxa [%1] %2, %0" \
34 : "=r" (__ret) \
35 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
36 : "memory"); \
37 __ret; \
38})
David S. Millerad7ad572007-07-27 22:39:14 -070039#define iommu_write(__reg, __val) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 __asm__ __volatile__("stxa %0, [%1] %2" \
41 : /* no outputs */ \
42 : "r" (__val), "r" (__reg), \
43 "i" (ASI_PHYS_BYPASS_EC_E))
44
45/* Must be invoked under the IOMMU lock. */
David S. Millerd2841422008-02-08 18:05:46 -080046static void iommu_flushall(struct iommu *iommu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
David S. Miller861fe902007-05-02 17:31:36 -070048 if (iommu->iommu_flushinv) {
David S. Millerad7ad572007-07-27 22:39:14 -070049 iommu_write(iommu->iommu_flushinv, ~(u64)0);
David S. Miller861fe902007-05-02 17:31:36 -070050 } else {
51 unsigned long tag;
52 int entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David S. Millerad7ad572007-07-27 22:39:14 -070054 tag = iommu->iommu_tags;
David S. Miller861fe902007-05-02 17:31:36 -070055 for (entry = 0; entry < 16; entry++) {
David S. Millerad7ad572007-07-27 22:39:14 -070056 iommu_write(tag, 0);
David S. Miller861fe902007-05-02 17:31:36 -070057 tag += 8;
58 }
59
60 /* Ensure completion of previous PIO writes. */
David S. Millerad7ad572007-07-27 22:39:14 -070061 (void) iommu_read(iommu->write_complete_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65#define IOPTE_CONSISTENT(CTX) \
66 (IOPTE_VALID | IOPTE_CACHE | \
67 (((CTX) << 47) & IOPTE_CONTEXT))
68
69#define IOPTE_STREAMING(CTX) \
70 (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
71
72/* Existing mappings are never marked invalid, instead they
73 * are pointed to a dummy page.
74 */
75#define IOPTE_IS_DUMMY(iommu, iopte) \
76 ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
77
David S. Miller16ce82d2007-04-26 21:08:21 -070078static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 unsigned long val = iopte_val(*iopte);
81
82 val &= ~IOPTE_PAGE;
83 val |= iommu->dummy_page_pa;
84
85 iopte_val(*iopte) = val;
86}
87
David S. Millerd2841422008-02-08 18:05:46 -080088/* Based almost entirely upon the ppc64 iommu allocator. If you use the 'handle'
89 * facility it must all be done in one pass while under the iommu lock.
90 *
91 * On sun4u platforms, we only flush the IOMMU once every time we've passed
92 * over the entire page table doing allocations. Therefore we only ever advance
93 * the hint and cannot backtrack it.
94 */
95unsigned long iommu_range_alloc(struct device *dev,
96 struct iommu *iommu,
97 unsigned long npages,
98 unsigned long *handle)
David S. Miller688cb302005-10-13 22:15:24 -070099{
David S. Millerd2841422008-02-08 18:05:46 -0800100 unsigned long n, end, start, limit, boundary_size;
David S. Miller9b3627f2007-04-24 23:51:18 -0700101 struct iommu_arena *arena = &iommu->arena;
David S. Millerd2841422008-02-08 18:05:46 -0800102 int pass = 0;
103
104 /* This allocator was derived from x86_64's bit string search */
105
106 /* Sanity check */
107 if (unlikely(npages == 0)) {
108 if (printk_ratelimit())
109 WARN_ON(1);
110 return DMA_ERROR_CODE;
111 }
112
113 if (handle && *handle)
114 start = *handle;
115 else
116 start = arena->hint;
David S. Miller688cb302005-10-13 22:15:24 -0700117
118 limit = arena->limit;
David S. Miller688cb302005-10-13 22:15:24 -0700119
David S. Millerd2841422008-02-08 18:05:46 -0800120 /* The case below can happen if we have a small segment appended
121 * to a large, or when the previous alloc was at the very end of
122 * the available space. If so, go back to the beginning and flush.
123 */
124 if (start >= limit) {
125 start = 0;
126 if (iommu->flush_all)
127 iommu->flush_all(iommu);
128 }
129
130 again:
131
132 if (dev)
133 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
134 1 << IO_PAGE_SHIFT);
135 else
136 boundary_size = ALIGN(1UL << 32, 1 << IO_PAGE_SHIFT);
137
FUJITA Tomonori89c94f22008-02-20 22:56:42 -0800138 n = iommu_area_alloc(arena->map, limit, start, npages,
139 iommu->page_table_map_base >> IO_PAGE_SHIFT,
David S. Millerd2841422008-02-08 18:05:46 -0800140 boundary_size >> IO_PAGE_SHIFT, 0);
141 if (n == -1) {
David S. Miller688cb302005-10-13 22:15:24 -0700142 if (likely(pass < 1)) {
David S. Millerd2841422008-02-08 18:05:46 -0800143 /* First failure, rescan from the beginning. */
David S. Miller688cb302005-10-13 22:15:24 -0700144 start = 0;
David S. Millerd2841422008-02-08 18:05:46 -0800145 if (iommu->flush_all)
146 iommu->flush_all(iommu);
David S. Miller688cb302005-10-13 22:15:24 -0700147 pass++;
148 goto again;
149 } else {
David S. Millerd2841422008-02-08 18:05:46 -0800150 /* Second failure, give up */
151 return DMA_ERROR_CODE;
David S. Miller688cb302005-10-13 22:15:24 -0700152 }
153 }
154
David S. Millerd2841422008-02-08 18:05:46 -0800155 end = n + npages;
David S. Miller688cb302005-10-13 22:15:24 -0700156
157 arena->hint = end;
158
David S. Millerd2841422008-02-08 18:05:46 -0800159 /* Update handle for SG allocations */
160 if (handle)
161 *handle = end;
162
David S. Miller688cb302005-10-13 22:15:24 -0700163 return n;
164}
165
David S. Millerd2841422008-02-08 18:05:46 -0800166void iommu_range_free(struct iommu *iommu, dma_addr_t dma_addr, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -0700167{
David S. Millerd2841422008-02-08 18:05:46 -0800168 struct iommu_arena *arena = &iommu->arena;
169 unsigned long entry;
David S. Miller688cb302005-10-13 22:15:24 -0700170
David S. Millerd2841422008-02-08 18:05:46 -0800171 entry = (dma_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
172
Akinobu Mitaa66022c2009-12-15 16:48:28 -0800173 bitmap_clear(arena->map, entry, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700174}
175
David S. Millerad7ad572007-07-27 22:39:14 -0700176int iommu_table_init(struct iommu *iommu, int tsbsize,
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700177 u32 dma_offset, u32 dma_addr_mask,
178 int numa_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700180 unsigned long i, order, sz, num_tsb_entries;
181 struct page *page;
David S. Miller688cb302005-10-13 22:15:24 -0700182
183 num_tsb_entries = tsbsize / sizeof(iopte_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David S. Miller51e85132005-10-13 21:10:08 -0700185 /* Setup initial software IOMMU state. */
186 spin_lock_init(&iommu->lock);
187 iommu->ctx_lowest_free = 1;
188 iommu->page_table_map_base = dma_offset;
189 iommu->dma_addr_mask = dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David S. Miller688cb302005-10-13 22:15:24 -0700191 /* Allocate and initialize the free area map. */
192 sz = num_tsb_entries / 8;
193 sz = (sz + 7UL) & ~7UL;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700194 iommu->arena.map = kmalloc_node(sz, GFP_KERNEL, numa_node);
David S. Miller688cb302005-10-13 22:15:24 -0700195 if (!iommu->arena.map) {
David S. Millerad7ad572007-07-27 22:39:14 -0700196 printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
197 return -ENOMEM;
David S. Miller51e85132005-10-13 21:10:08 -0700198 }
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700199 memset(iommu->arena.map, 0, sz);
David S. Miller688cb302005-10-13 22:15:24 -0700200 iommu->arena.limit = num_tsb_entries;
David S. Miller51e85132005-10-13 21:10:08 -0700201
David S. Millerd2841422008-02-08 18:05:46 -0800202 if (tlb_type != hypervisor)
203 iommu->flush_all = iommu_flushall;
204
David S. Miller51e85132005-10-13 21:10:08 -0700205 /* Allocate and initialize the dummy page which we
206 * set inactive IO PTEs to point to.
207 */
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700208 page = alloc_pages_node(numa_node, GFP_KERNEL, 0);
209 if (!page) {
David S. Millerad7ad572007-07-27 22:39:14 -0700210 printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
211 goto out_free_map;
David S. Miller51e85132005-10-13 21:10:08 -0700212 }
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700213 iommu->dummy_page = (unsigned long) page_address(page);
214 memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
David S. Miller51e85132005-10-13 21:10:08 -0700215 iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
216
217 /* Now allocate and setup the IOMMU page table itself. */
218 order = get_order(tsbsize);
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700219 page = alloc_pages_node(numa_node, GFP_KERNEL, order);
220 if (!page) {
David S. Millerad7ad572007-07-27 22:39:14 -0700221 printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
222 goto out_free_dummy_page;
David S. Miller51e85132005-10-13 21:10:08 -0700223 }
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700224 iommu->page_table = (iopte_t *)page_address(page);
David S. Miller51e85132005-10-13 21:10:08 -0700225
David S. Miller688cb302005-10-13 22:15:24 -0700226 for (i = 0; i < num_tsb_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 iopte_make_dummy(iommu, &iommu->page_table[i]);
David S. Millerad7ad572007-07-27 22:39:14 -0700228
229 return 0;
230
231out_free_dummy_page:
232 free_page(iommu->dummy_page);
233 iommu->dummy_page = 0UL;
234
235out_free_map:
236 kfree(iommu->arena.map);
237 iommu->arena.map = NULL;
238
239 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
David S. Millerd2841422008-02-08 18:05:46 -0800242static inline iopte_t *alloc_npages(struct device *dev, struct iommu *iommu,
243 unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
David S. Millerd2841422008-02-08 18:05:46 -0800245 unsigned long entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David S. Millerd2841422008-02-08 18:05:46 -0800247 entry = iommu_range_alloc(dev, iommu, npages, NULL);
248 if (unlikely(entry == DMA_ERROR_CODE))
David S. Miller688cb302005-10-13 22:15:24 -0700249 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
David S. Miller688cb302005-10-13 22:15:24 -0700251 return iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
David S. Miller16ce82d2007-04-26 21:08:21 -0700254static int iommu_alloc_ctx(struct iommu *iommu)
David S. Miller7c963ad2005-05-31 16:57:59 -0700255{
256 int lowest = iommu->ctx_lowest_free;
257 int sz = IOMMU_NUM_CTXS - lowest;
258 int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
259
260 if (unlikely(n == sz)) {
261 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,
283 dma_addr_t *dma_addrp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
David S. Miller688cb302005-10-13 22:15:24 -0700285 unsigned long flags, order, first_page;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700286 struct iommu *iommu;
287 struct page *page;
288 int npages, nid;
289 iopte_t *iopte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 size = IO_PAGE_ALIGN(size);
293 order = get_order(size);
294 if (order >= 10)
295 return NULL;
296
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700297 nid = dev->archdata.numa_node;
298 page = alloc_pages_node(nid, gfp, order);
299 if (unlikely(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return NULL;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700301
302 first_page = (unsigned long) page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 memset((char *)first_page, 0, PAGE_SIZE << order);
304
David S. Millerad7ad572007-07-27 22:39:14 -0700305 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800308 iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT);
David S. Miller688cb302005-10-13 22:15:24 -0700309 spin_unlock_irqrestore(&iommu->lock, flags);
310
311 if (unlikely(iopte == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 free_pages(first_page, order);
313 return NULL;
314 }
315
316 *dma_addrp = (iommu->page_table_map_base +
317 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
318 ret = (void *) first_page;
319 npages = size >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 first_page = __pa(first_page);
321 while (npages--) {
David S. Miller688cb302005-10-13 22:15:24 -0700322 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 IOPTE_WRITE |
324 (first_page & IOPTE_PAGE));
325 iopte++;
326 first_page += IO_PAGE_SIZE;
327 }
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return ret;
330}
331
David S. Millerad7ad572007-07-27 22:39:14 -0700332static void dma_4u_free_coherent(struct device *dev, size_t size,
333 void *cpu, dma_addr_t dvma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
David S. Miller16ce82d2007-04-26 21:08:21 -0700335 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700337 unsigned long flags, order, npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700340 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 iopte = iommu->page_table +
342 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
343
344 spin_lock_irqsave(&iommu->lock, flags);
345
David S. Millerd2841422008-02-08 18:05:46 -0800346 iommu_range_free(iommu, dvma, npages);
David S. Miller7c963ad2005-05-31 16:57:59 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 spin_unlock_irqrestore(&iommu->lock, flags);
349
350 order = get_order(size);
351 if (order < 10)
352 free_pages((unsigned long)cpu, order);
353}
354
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000355static dma_addr_t dma_4u_map_page(struct device *dev, struct page *page,
356 unsigned long offset, size_t sz,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900357 enum dma_data_direction direction,
358 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
David S. Miller16ce82d2007-04-26 21:08:21 -0700360 struct iommu *iommu;
361 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 iopte_t *base;
363 unsigned long flags, npages, oaddr;
364 unsigned long i, base_paddr, ctx;
365 u32 bus_addr, ret;
366 unsigned long iopte_protection;
367
David S. Millerad7ad572007-07-27 22:39:14 -0700368 iommu = dev->archdata.iommu;
369 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David S. Millerad7ad572007-07-27 22:39:14 -0700371 if (unlikely(direction == DMA_NONE))
David S. Miller688cb302005-10-13 22:15:24 -0700372 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000374 oaddr = (unsigned long)(page_address(page) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
376 npages >>= IO_PAGE_SHIFT;
377
378 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800379 base = alloc_npages(dev, iommu, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700380 ctx = 0;
381 if (iommu->iommu_ctxflush)
382 ctx = iommu_alloc_ctx(iommu);
383 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David S. Miller688cb302005-10-13 22:15:24 -0700385 if (unlikely(!base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 bus_addr = (iommu->page_table_map_base +
389 ((base - iommu->page_table) << IO_PAGE_SHIFT));
390 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
391 base_paddr = __pa(oaddr & IO_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (strbuf->strbuf_enabled)
393 iopte_protection = IOPTE_STREAMING(ctx);
394 else
395 iopte_protection = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700396 if (direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 iopte_protection |= IOPTE_WRITE;
398
399 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
400 iopte_val(*base) = iopte_protection | base_paddr;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return ret;
403
404bad:
David S. Miller688cb302005-10-13 22:15:24 -0700405 iommu_free_ctx(iommu, ctx);
406bad_no_ctx:
407 if (printk_ratelimit())
408 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700409 return DMA_ERROR_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
David S. Millerad7ad572007-07-27 22:39:14 -0700412static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
413 u32 vaddr, unsigned long ctx, unsigned long npages,
414 enum dma_data_direction direction)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700415{
416 int limit;
417
David S. Miller4dbc30f2005-05-11 11:37:00 -0700418 if (strbuf->strbuf_ctxflush &&
419 iommu->iommu_ctxflush) {
420 unsigned long matchreg, flushreg;
David S. Miller7c963ad2005-05-31 16:57:59 -0700421 u64 val;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700422
423 flushreg = strbuf->strbuf_ctxflush;
David S. Millerad7ad572007-07-27 22:39:14 -0700424 matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700425
David S. Millerad7ad572007-07-27 22:39:14 -0700426 iommu_write(flushreg, ctx);
427 val = iommu_read(matchreg);
David S. Miller88314ee2005-05-31 19:13:52 -0700428 val &= 0xffff;
429 if (!val)
David S. Miller7c963ad2005-05-31 16:57:59 -0700430 goto do_flush_sync;
431
David S. Miller7c963ad2005-05-31 16:57:59 -0700432 while (val) {
433 if (val & 0x1)
David S. Millerad7ad572007-07-27 22:39:14 -0700434 iommu_write(flushreg, ctx);
David S. Miller7c963ad2005-05-31 16:57:59 -0700435 val >>= 1;
David S. Millera228dfd2005-05-20 11:40:32 -0700436 }
David S. Millerad7ad572007-07-27 22:39:14 -0700437 val = iommu_read(matchreg);
David S. Miller7c963ad2005-05-31 16:57:59 -0700438 if (unlikely(val)) {
David S. Millerad7ad572007-07-27 22:39:14 -0700439 printk(KERN_WARNING "strbuf_flush: ctx flush "
Sam Ravnborg90181132009-01-06 13:19:28 -0800440 "timeout matchreg[%llx] ctx[%lx]\n",
David S. Miller7c963ad2005-05-31 16:57:59 -0700441 val, ctx);
442 goto do_page_flush;
443 }
David S. Miller4dbc30f2005-05-11 11:37:00 -0700444 } else {
445 unsigned long i;
446
David S. Miller7c963ad2005-05-31 16:57:59 -0700447 do_page_flush:
David S. Miller4dbc30f2005-05-11 11:37:00 -0700448 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
David S. Millerad7ad572007-07-27 22:39:14 -0700449 iommu_write(strbuf->strbuf_pflush, vaddr);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700450 }
451
David S. Miller7c963ad2005-05-31 16:57:59 -0700452do_flush_sync:
453 /* If the device could not have possibly put dirty data into
454 * the streaming cache, no flush-flag synchronization needs
455 * to be performed.
456 */
David S. Millerad7ad572007-07-27 22:39:14 -0700457 if (direction == DMA_TO_DEVICE)
David S. Miller7c963ad2005-05-31 16:57:59 -0700458 return;
459
David S. Millerad7ad572007-07-27 22:39:14 -0700460 STC_FLUSHFLAG_INIT(strbuf);
461 iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
462 (void) iommu_read(iommu->write_complete_reg);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700463
David S. Millera228dfd2005-05-20 11:40:32 -0700464 limit = 100000;
David S. Millerad7ad572007-07-27 22:39:14 -0700465 while (!STC_FLUSHFLAG_SET(strbuf)) {
David S. Miller4dbc30f2005-05-11 11:37:00 -0700466 limit--;
467 if (!limit)
468 break;
David S. Millera228dfd2005-05-20 11:40:32 -0700469 udelay(1);
David S. Miller4f071182005-08-29 12:46:22 -0700470 rmb();
David S. Miller4dbc30f2005-05-11 11:37:00 -0700471 }
472 if (!limit)
David S. Millerad7ad572007-07-27 22:39:14 -0700473 printk(KERN_WARNING "strbuf_flush: flushflag timeout "
David S. Miller4dbc30f2005-05-11 11:37:00 -0700474 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
475 vaddr, ctx, npages);
476}
477
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000478static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900479 size_t sz, enum dma_data_direction direction,
480 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
David S. Miller16ce82d2007-04-26 21:08:21 -0700482 struct iommu *iommu;
483 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 iopte_t *base;
David S. Miller688cb302005-10-13 22:15:24 -0700485 unsigned long flags, npages, ctx, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
David S. Millerad7ad572007-07-27 22:39:14 -0700487 if (unlikely(direction == DMA_NONE)) {
David S. Miller688cb302005-10-13 22:15:24 -0700488 if (printk_ratelimit())
489 WARN_ON(1);
490 return;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David S. Millerad7ad572007-07-27 22:39:14 -0700493 iommu = dev->archdata.iommu;
494 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
497 npages >>= IO_PAGE_SHIFT;
498 base = iommu->page_table +
499 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 bus_addr &= IO_PAGE_MASK;
501
502 spin_lock_irqsave(&iommu->lock, flags);
503
504 /* Record the context, if any. */
505 ctx = 0;
506 if (iommu->iommu_ctxflush)
507 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
508
509 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700510 if (strbuf->strbuf_enabled)
David S. Millerad7ad572007-07-27 22:39:14 -0700511 strbuf_flush(strbuf, iommu, bus_addr, ctx,
512 npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
David S. Miller688cb302005-10-13 22:15:24 -0700514 /* Step 2: Clear out TSB entries. */
515 for (i = 0; i < npages; i++)
516 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
David S. Millerd2841422008-02-08 18:05:46 -0800518 iommu_range_free(iommu, bus_addr, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
David S. Miller7c963ad2005-05-31 16:57:59 -0700520 iommu_free_ctx(iommu, ctx);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 spin_unlock_irqrestore(&iommu->lock, flags);
523}
524
David S. Millerad7ad572007-07-27 22:39:14 -0700525static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900526 int nelems, enum dma_data_direction direction,
527 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
David S. Miller13fa14e2008-02-09 03:11:01 -0800529 struct scatterlist *s, *outs, *segstart;
530 unsigned long flags, handle, prot, ctx;
531 dma_addr_t dma_next = 0, dma_addr;
532 unsigned int max_seg_size;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700533 unsigned long seg_boundary_size;
David S. Miller13fa14e2008-02-09 03:11:01 -0800534 int outcount, incount, i;
David S. Miller16ce82d2007-04-26 21:08:21 -0700535 struct strbuf *strbuf;
David S. Miller38192d52008-02-06 03:50:26 -0800536 struct iommu *iommu;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700537 unsigned long base_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David S. Miller13fa14e2008-02-09 03:11:01 -0800539 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
David S. Millerad7ad572007-07-27 22:39:14 -0700541 iommu = dev->archdata.iommu;
542 strbuf = dev->archdata.stc;
David S. Miller13fa14e2008-02-09 03:11:01 -0800543 if (nelems == 0 || !iommu)
544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 spin_lock_irqsave(&iommu->lock, flags);
547
David S. Miller688cb302005-10-13 22:15:24 -0700548 ctx = 0;
549 if (iommu->iommu_ctxflush)
550 ctx = iommu_alloc_ctx(iommu);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (strbuf->strbuf_enabled)
David S. Miller13fa14e2008-02-09 03:11:01 -0800553 prot = IOPTE_STREAMING(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 else
David S. Miller13fa14e2008-02-09 03:11:01 -0800555 prot = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700556 if (direction != DMA_TO_DEVICE)
David S. Miller13fa14e2008-02-09 03:11:01 -0800557 prot |= IOPTE_WRITE;
David S. Miller688cb302005-10-13 22:15:24 -0700558
David S. Miller13fa14e2008-02-09 03:11:01 -0800559 outs = s = segstart = &sglist[0];
560 outcount = 1;
561 incount = nelems;
562 handle = 0;
David S. Miller688cb302005-10-13 22:15:24 -0700563
David S. Miller13fa14e2008-02-09 03:11:01 -0800564 /* Init first segment length for backout at failure */
565 outs->dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
David S. Miller13fa14e2008-02-09 03:11:01 -0800567 max_seg_size = dma_get_max_seg_size(dev);
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700568 seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
569 IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
570 base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
David S. Miller13fa14e2008-02-09 03:11:01 -0800571 for_each_sg(sglist, s, nelems, i) {
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700572 unsigned long paddr, npages, entry, out_entry = 0, slen;
David S. Miller13fa14e2008-02-09 03:11:01 -0800573 iopte_t *base;
David S. Miller38192d52008-02-06 03:50:26 -0800574
David S. Miller13fa14e2008-02-09 03:11:01 -0800575 slen = s->length;
576 /* Sanity check */
577 if (slen == 0) {
578 dma_next = 0;
579 continue;
580 }
581 /* Allocate iommu entries for that segment */
582 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
Joerg Roedel0fcff282008-10-15 22:02:14 -0700583 npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800584 entry = iommu_range_alloc(dev, iommu, npages, &handle);
585
586 /* Handle failure */
587 if (unlikely(entry == DMA_ERROR_CODE)) {
588 if (printk_ratelimit())
589 printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
590 " npages %lx\n", iommu, paddr, npages);
591 goto iommu_map_failed;
592 }
593
594 base = iommu->page_table + entry;
595
596 /* Convert entry to a dma_addr_t */
597 dma_addr = iommu->page_table_map_base +
598 (entry << IO_PAGE_SHIFT);
599 dma_addr |= (s->offset & ~IO_PAGE_MASK);
600
601 /* Insert into HW table */
David S. Miller38192d52008-02-06 03:50:26 -0800602 paddr &= IO_PAGE_MASK;
David S. Miller13fa14e2008-02-09 03:11:01 -0800603 while (npages--) {
604 iopte_val(*base) = prot | paddr;
David S. Miller38192d52008-02-06 03:50:26 -0800605 base++;
606 paddr += IO_PAGE_SIZE;
David S. Miller38192d52008-02-06 03:50:26 -0800607 }
David S. Miller13fa14e2008-02-09 03:11:01 -0800608
609 /* If we are in an open segment, try merging */
610 if (segstart != s) {
611 /* We cannot merge if:
612 * - allocated dma_addr isn't contiguous to previous allocation
613 */
614 if ((dma_addr != dma_next) ||
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700615 (outs->dma_length + s->length > max_seg_size) ||
616 (is_span_boundary(out_entry, base_shift,
617 seg_boundary_size, outs, s))) {
David S. Miller13fa14e2008-02-09 03:11:01 -0800618 /* Can't merge: create a new segment */
619 segstart = s;
620 outcount++;
621 outs = sg_next(outs);
622 } else {
623 outs->dma_length += s->length;
624 }
625 }
626
627 if (segstart == s) {
628 /* This is a new segment, fill entries */
629 outs->dma_address = dma_addr;
630 outs->dma_length = slen;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700631 out_entry = entry;
David S. Miller13fa14e2008-02-09 03:11:01 -0800632 }
633
634 /* Calculate next page pointer for contiguous check */
635 dma_next = dma_addr + slen;
David S. Miller38192d52008-02-06 03:50:26 -0800636 }
637
David S. Miller13fa14e2008-02-09 03:11:01 -0800638 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
David S. Miller13fa14e2008-02-09 03:11:01 -0800640 if (outcount < incount) {
641 outs = sg_next(outs);
642 outs->dma_address = DMA_ERROR_CODE;
643 outs->dma_length = 0;
644 }
645
646 return outcount;
647
648iommu_map_failed:
649 for_each_sg(sglist, s, nelems, i) {
650 if (s->dma_length != 0) {
David S. Miller6c830fe2008-03-25 22:44:10 -0700651 unsigned long vaddr, npages, entry, j;
David S. Miller13fa14e2008-02-09 03:11:01 -0800652 iopte_t *base;
653
654 vaddr = s->dma_address & IO_PAGE_MASK;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700655 npages = iommu_num_pages(s->dma_address, s->dma_length,
656 IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800657 iommu_range_free(iommu, vaddr, npages);
658
659 entry = (vaddr - iommu->page_table_map_base)
660 >> IO_PAGE_SHIFT;
661 base = iommu->page_table + entry;
662
David S. Miller6c830fe2008-03-25 22:44:10 -0700663 for (j = 0; j < npages; j++)
664 iopte_make_dummy(iommu, base + j);
David S. Miller13fa14e2008-02-09 03:11:01 -0800665
666 s->dma_address = DMA_ERROR_CODE;
667 s->dma_length = 0;
668 }
669 if (s == outs)
670 break;
671 }
672 spin_unlock_irqrestore(&iommu->lock, flags);
673
David S. Miller688cb302005-10-13 22:15:24 -0700674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
David S. Miller13fa14e2008-02-09 03:11:01 -0800677/* If contexts are being used, they are the same in all of the mappings
678 * we make for a particular SG.
679 */
680static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
681{
682 unsigned long ctx = 0;
683
684 if (iommu->iommu_ctxflush) {
685 iopte_t *base;
686 u32 bus_addr;
687
688 bus_addr = sg->dma_address & IO_PAGE_MASK;
689 base = iommu->page_table +
690 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
691
692 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
693 }
694 return ctx;
695}
696
David S. Millerad7ad572007-07-27 22:39:14 -0700697static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonoribc0a14f2009-08-10 11:53:12 +0900698 int nelems, enum dma_data_direction direction,
699 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
David S. Miller13fa14e2008-02-09 03:11:01 -0800701 unsigned long flags, ctx;
702 struct scatterlist *sg;
David S. Miller38192d52008-02-06 03:50:26 -0800703 struct strbuf *strbuf;
704 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
David S. Miller13fa14e2008-02-09 03:11:01 -0800706 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
David S. Millerad7ad572007-07-27 22:39:14 -0700708 iommu = dev->archdata.iommu;
709 strbuf = dev->archdata.stc;
710
David S. Miller13fa14e2008-02-09 03:11:01 -0800711 ctx = fetch_sg_ctx(iommu, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 spin_lock_irqsave(&iommu->lock, flags);
714
David S. Miller13fa14e2008-02-09 03:11:01 -0800715 sg = sglist;
716 while (nelems--) {
717 dma_addr_t dma_handle = sg->dma_address;
718 unsigned int len = sg->dma_length;
719 unsigned long npages, entry;
720 iopte_t *base;
721 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
David S. Miller13fa14e2008-02-09 03:11:01 -0800723 if (!len)
724 break;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700725 npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800726 iommu_range_free(iommu, dma_handle, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
David S. Miller13fa14e2008-02-09 03:11:01 -0800728 entry = ((dma_handle - iommu->page_table_map_base)
729 >> IO_PAGE_SHIFT);
730 base = iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
David S. Miller13fa14e2008-02-09 03:11:01 -0800732 dma_handle &= IO_PAGE_MASK;
733 if (strbuf->strbuf_enabled)
734 strbuf_flush(strbuf, iommu, dma_handle, ctx,
735 npages, direction);
736
737 for (i = 0; i < npages; i++)
738 iopte_make_dummy(iommu, base + i);
739
740 sg = sg_next(sg);
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
David S. Miller7c963ad2005-05-31 16:57:59 -0700743 iommu_free_ctx(iommu, ctx);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 spin_unlock_irqrestore(&iommu->lock, flags);
746}
747
David S. Millerad7ad572007-07-27 22:39:14 -0700748static void dma_4u_sync_single_for_cpu(struct device *dev,
749 dma_addr_t bus_addr, size_t sz,
750 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
David S. Miller16ce82d2007-04-26 21:08:21 -0700752 struct iommu *iommu;
753 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 unsigned long flags, ctx, npages;
755
David S. Millerad7ad572007-07-27 22:39:14 -0700756 iommu = dev->archdata.iommu;
757 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (!strbuf->strbuf_enabled)
760 return;
761
762 spin_lock_irqsave(&iommu->lock, flags);
763
764 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
765 npages >>= IO_PAGE_SHIFT;
766 bus_addr &= IO_PAGE_MASK;
767
768 /* Step 1: Record the context, if any. */
769 ctx = 0;
770 if (iommu->iommu_ctxflush &&
771 strbuf->strbuf_ctxflush) {
772 iopte_t *iopte;
773
774 iopte = iommu->page_table +
775 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
776 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
777 }
778
779 /* Step 2: Kick data out of streaming buffers. */
David S. Millerad7ad572007-07-27 22:39:14 -0700780 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 spin_unlock_irqrestore(&iommu->lock, flags);
783}
784
David S. Millerad7ad572007-07-27 22:39:14 -0700785static void dma_4u_sync_sg_for_cpu(struct device *dev,
786 struct scatterlist *sglist, int nelems,
787 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
David S. Miller16ce82d2007-04-26 21:08:21 -0700789 struct iommu *iommu;
790 struct strbuf *strbuf;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700791 unsigned long flags, ctx, npages, i;
Jens Axboe2c941a22007-08-07 09:37:10 +0200792 struct scatterlist *sg, *sgprv;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700793 u32 bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
David S. Millerad7ad572007-07-27 22:39:14 -0700795 iommu = dev->archdata.iommu;
796 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (!strbuf->strbuf_enabled)
799 return;
800
801 spin_lock_irqsave(&iommu->lock, flags);
802
803 /* Step 1: Record the context, if any. */
804 ctx = 0;
805 if (iommu->iommu_ctxflush &&
806 strbuf->strbuf_ctxflush) {
807 iopte_t *iopte;
808
809 iopte = iommu->page_table +
810 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
811 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
812 }
813
814 /* Step 2: Kick data out of streaming buffers. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700815 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
Jens Axboe2c941a22007-08-07 09:37:10 +0200816 sgprv = NULL;
817 for_each_sg(sglist, sg, nelems, i) {
818 if (sg->dma_length == 0)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700819 break;
Jens Axboe2c941a22007-08-07 09:37:10 +0200820 sgprv = sg;
821 }
822
823 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700824 - bus_addr) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700825 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 spin_unlock_irqrestore(&iommu->lock, flags);
828}
829
FUJITA Tomonori02f7a182009-08-10 11:53:13 +0900830static struct dma_map_ops sun4u_dma_ops = {
David S. Millerad7ad572007-07-27 22:39:14 -0700831 .alloc_coherent = dma_4u_alloc_coherent,
832 .free_coherent = dma_4u_free_coherent,
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000833 .map_page = dma_4u_map_page,
834 .unmap_page = dma_4u_unmap_page,
David S. Millerad7ad572007-07-27 22:39:14 -0700835 .map_sg = dma_4u_map_sg,
836 .unmap_sg = dma_4u_unmap_sg,
837 .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
838 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800839};
840
FUJITA Tomonori02f7a182009-08-10 11:53:13 +0900841struct dma_map_ops *dma_ops = &sun4u_dma_ops;
David S. Millerad7ad572007-07-27 22:39:14 -0700842EXPORT_SYMBOL(dma_ops);
843
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900844extern int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask);
845
David S. Millerad7ad572007-07-27 22:39:14 -0700846int dma_supported(struct device *dev, u64 device_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
David S. Millerad7ad572007-07-27 22:39:14 -0700848 struct iommu *iommu = dev->archdata.iommu;
849 u64 dma_addr_mask = iommu->dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 if (device_mask >= (1UL << 32UL))
852 return 0;
853
David S. Millerad7ad572007-07-27 22:39:14 -0700854 if ((device_mask & dma_addr_mask) == dma_addr_mask)
855 return 1;
856
857#ifdef CONFIG_PCI
858 if (dev->bus == &pci_bus_type)
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900859 return pci64_dma_supported(to_pci_dev(dev), device_mask);
David S. Millerad7ad572007-07-27 22:39:14 -0700860#endif
861
862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
David S. Millerad7ad572007-07-27 22:39:14 -0700864EXPORT_SYMBOL(dma_supported);