blob: d8900e1d5aad0cbd2253079e0ef927c9465e4fe5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
David S. Millerad7ad572007-07-27 22:39:14 -070015#ifdef CONFIG_PCI
16#include <linux/pci.h>
17#endif
18
19#include <asm/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "iommu_common.h"
22
David S. Millerad7ad572007-07-27 22:39:14 -070023#define STC_CTXMATCH_ADDR(STC, CTX) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
David S. Millerad7ad572007-07-27 22:39:14 -070025#define STC_FLUSHFLAG_INIT(STC) \
26 (*((STC)->strbuf_flushflag) = 0UL)
27#define STC_FLUSHFLAG_SET(STC) \
28 (*((STC)->strbuf_flushflag) != 0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Millerad7ad572007-07-27 22:39:14 -070030#define iommu_read(__reg) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070031({ u64 __ret; \
32 __asm__ __volatile__("ldxa [%1] %2, %0" \
33 : "=r" (__ret) \
34 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
35 : "memory"); \
36 __ret; \
37})
David S. Millerad7ad572007-07-27 22:39:14 -070038#define iommu_write(__reg, __val) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 __asm__ __volatile__("stxa %0, [%1] %2" \
40 : /* no outputs */ \
41 : "r" (__val), "r" (__reg), \
42 "i" (ASI_PHYS_BYPASS_EC_E))
43
44/* Must be invoked under the IOMMU lock. */
David S. Millerd2841422008-02-08 18:05:46 -080045static void iommu_flushall(struct iommu *iommu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
David S. Miller861fe902007-05-02 17:31:36 -070047 if (iommu->iommu_flushinv) {
David S. Millerad7ad572007-07-27 22:39:14 -070048 iommu_write(iommu->iommu_flushinv, ~(u64)0);
David S. Miller861fe902007-05-02 17:31:36 -070049 } else {
50 unsigned long tag;
51 int entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
David S. Millerad7ad572007-07-27 22:39:14 -070053 tag = iommu->iommu_tags;
David S. Miller861fe902007-05-02 17:31:36 -070054 for (entry = 0; entry < 16; entry++) {
David S. Millerad7ad572007-07-27 22:39:14 -070055 iommu_write(tag, 0);
David S. Miller861fe902007-05-02 17:31:36 -070056 tag += 8;
57 }
58
59 /* Ensure completion of previous PIO writes. */
David S. Millerad7ad572007-07-27 22:39:14 -070060 (void) iommu_read(iommu->write_complete_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64#define IOPTE_CONSISTENT(CTX) \
65 (IOPTE_VALID | IOPTE_CACHE | \
66 (((CTX) << 47) & IOPTE_CONTEXT))
67
68#define IOPTE_STREAMING(CTX) \
69 (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
70
71/* Existing mappings are never marked invalid, instead they
72 * are pointed to a dummy page.
73 */
74#define IOPTE_IS_DUMMY(iommu, iopte) \
75 ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
76
David S. Miller16ce82d2007-04-26 21:08:21 -070077static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 unsigned long val = iopte_val(*iopte);
80
81 val &= ~IOPTE_PAGE;
82 val |= iommu->dummy_page_pa;
83
84 iopte_val(*iopte) = val;
85}
86
David S. Millerd2841422008-02-08 18:05:46 -080087/* Based almost entirely upon the ppc64 iommu allocator. If you use the 'handle'
88 * facility it must all be done in one pass while under the iommu lock.
89 *
90 * On sun4u platforms, we only flush the IOMMU once every time we've passed
91 * over the entire page table doing allocations. Therefore we only ever advance
92 * the hint and cannot backtrack it.
93 */
94unsigned long iommu_range_alloc(struct device *dev,
95 struct iommu *iommu,
96 unsigned long npages,
97 unsigned long *handle)
David S. Miller688cb302005-10-13 22:15:24 -070098{
David S. Millerd2841422008-02-08 18:05:46 -080099 unsigned long n, end, start, limit, boundary_size;
David S. Miller9b3627f2007-04-24 23:51:18 -0700100 struct iommu_arena *arena = &iommu->arena;
David S. Millerd2841422008-02-08 18:05:46 -0800101 int pass = 0;
102
103 /* This allocator was derived from x86_64's bit string search */
104
105 /* Sanity check */
106 if (unlikely(npages == 0)) {
107 if (printk_ratelimit())
108 WARN_ON(1);
109 return DMA_ERROR_CODE;
110 }
111
112 if (handle && *handle)
113 start = *handle;
114 else
115 start = arena->hint;
David S. Miller688cb302005-10-13 22:15:24 -0700116
117 limit = arena->limit;
David S. Miller688cb302005-10-13 22:15:24 -0700118
David S. Millerd2841422008-02-08 18:05:46 -0800119 /* The case below can happen if we have a small segment appended
120 * to a large, or when the previous alloc was at the very end of
121 * the available space. If so, go back to the beginning and flush.
122 */
123 if (start >= limit) {
124 start = 0;
125 if (iommu->flush_all)
126 iommu->flush_all(iommu);
127 }
128
129 again:
130
131 if (dev)
132 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
133 1 << IO_PAGE_SHIFT);
134 else
135 boundary_size = ALIGN(1UL << 32, 1 << IO_PAGE_SHIFT);
136
FUJITA Tomonori89c94f22008-02-20 22:56:42 -0800137 n = iommu_area_alloc(arena->map, limit, start, npages,
138 iommu->page_table_map_base >> IO_PAGE_SHIFT,
David S. Millerd2841422008-02-08 18:05:46 -0800139 boundary_size >> IO_PAGE_SHIFT, 0);
140 if (n == -1) {
David S. Miller688cb302005-10-13 22:15:24 -0700141 if (likely(pass < 1)) {
David S. Millerd2841422008-02-08 18:05:46 -0800142 /* First failure, rescan from the beginning. */
David S. Miller688cb302005-10-13 22:15:24 -0700143 start = 0;
David S. Millerd2841422008-02-08 18:05:46 -0800144 if (iommu->flush_all)
145 iommu->flush_all(iommu);
David S. Miller688cb302005-10-13 22:15:24 -0700146 pass++;
147 goto again;
148 } else {
David S. Millerd2841422008-02-08 18:05:46 -0800149 /* Second failure, give up */
150 return DMA_ERROR_CODE;
David S. Miller688cb302005-10-13 22:15:24 -0700151 }
152 }
153
David S. Millerd2841422008-02-08 18:05:46 -0800154 end = n + npages;
David S. Miller688cb302005-10-13 22:15:24 -0700155
156 arena->hint = end;
157
David S. Millerd2841422008-02-08 18:05:46 -0800158 /* Update handle for SG allocations */
159 if (handle)
160 *handle = end;
161
David S. Miller688cb302005-10-13 22:15:24 -0700162 return n;
163}
164
David S. Millerd2841422008-02-08 18:05:46 -0800165void iommu_range_free(struct iommu *iommu, dma_addr_t dma_addr, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -0700166{
David S. Millerd2841422008-02-08 18:05:46 -0800167 struct iommu_arena *arena = &iommu->arena;
168 unsigned long entry;
David S. Miller688cb302005-10-13 22:15:24 -0700169
David S. Millerd2841422008-02-08 18:05:46 -0800170 entry = (dma_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
171
172 iommu_area_free(arena->map, entry, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700173}
174
David S. Millerad7ad572007-07-27 22:39:14 -0700175int iommu_table_init(struct iommu *iommu, int tsbsize,
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700176 u32 dma_offset, u32 dma_addr_mask,
177 int numa_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700179 unsigned long i, order, sz, num_tsb_entries;
180 struct page *page;
David S. Miller688cb302005-10-13 22:15:24 -0700181
182 num_tsb_entries = tsbsize / sizeof(iopte_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
David S. Miller51e85132005-10-13 21:10:08 -0700184 /* Setup initial software IOMMU state. */
185 spin_lock_init(&iommu->lock);
186 iommu->ctx_lowest_free = 1;
187 iommu->page_table_map_base = dma_offset;
188 iommu->dma_addr_mask = dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
David S. Miller688cb302005-10-13 22:15:24 -0700190 /* Allocate and initialize the free area map. */
191 sz = num_tsb_entries / 8;
192 sz = (sz + 7UL) & ~7UL;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700193 iommu->arena.map = kmalloc_node(sz, GFP_KERNEL, numa_node);
David S. Miller688cb302005-10-13 22:15:24 -0700194 if (!iommu->arena.map) {
David S. Millerad7ad572007-07-27 22:39:14 -0700195 printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
196 return -ENOMEM;
David S. Miller51e85132005-10-13 21:10:08 -0700197 }
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700198 memset(iommu->arena.map, 0, sz);
David S. Miller688cb302005-10-13 22:15:24 -0700199 iommu->arena.limit = num_tsb_entries;
David S. Miller51e85132005-10-13 21:10:08 -0700200
David S. Millerd2841422008-02-08 18:05:46 -0800201 if (tlb_type != hypervisor)
202 iommu->flush_all = iommu_flushall;
203
David S. Miller51e85132005-10-13 21:10:08 -0700204 /* Allocate and initialize the dummy page which we
205 * set inactive IO PTEs to point to.
206 */
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700207 page = alloc_pages_node(numa_node, GFP_KERNEL, 0);
208 if (!page) {
David S. Millerad7ad572007-07-27 22:39:14 -0700209 printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
210 goto out_free_map;
David S. Miller51e85132005-10-13 21:10:08 -0700211 }
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700212 iommu->dummy_page = (unsigned long) page_address(page);
213 memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
David S. Miller51e85132005-10-13 21:10:08 -0700214 iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
215
216 /* Now allocate and setup the IOMMU page table itself. */
217 order = get_order(tsbsize);
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700218 page = alloc_pages_node(numa_node, GFP_KERNEL, order);
219 if (!page) {
David S. Millerad7ad572007-07-27 22:39:14 -0700220 printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
221 goto out_free_dummy_page;
David S. Miller51e85132005-10-13 21:10:08 -0700222 }
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700223 iommu->page_table = (iopte_t *)page_address(page);
David S. Miller51e85132005-10-13 21:10:08 -0700224
David S. Miller688cb302005-10-13 22:15:24 -0700225 for (i = 0; i < num_tsb_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 iopte_make_dummy(iommu, &iommu->page_table[i]);
David S. Millerad7ad572007-07-27 22:39:14 -0700227
228 return 0;
229
230out_free_dummy_page:
231 free_page(iommu->dummy_page);
232 iommu->dummy_page = 0UL;
233
234out_free_map:
235 kfree(iommu->arena.map);
236 iommu->arena.map = NULL;
237
238 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
David S. Millerd2841422008-02-08 18:05:46 -0800241static inline iopte_t *alloc_npages(struct device *dev, struct iommu *iommu,
242 unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
David S. Millerd2841422008-02-08 18:05:46 -0800244 unsigned long entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
David S. Millerd2841422008-02-08 18:05:46 -0800246 entry = iommu_range_alloc(dev, iommu, npages, NULL);
247 if (unlikely(entry == DMA_ERROR_CODE))
David S. Miller688cb302005-10-13 22:15:24 -0700248 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
David S. Miller688cb302005-10-13 22:15:24 -0700250 return iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
David S. Miller16ce82d2007-04-26 21:08:21 -0700253static int iommu_alloc_ctx(struct iommu *iommu)
David S. Miller7c963ad2005-05-31 16:57:59 -0700254{
255 int lowest = iommu->ctx_lowest_free;
256 int sz = IOMMU_NUM_CTXS - lowest;
257 int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
258
259 if (unlikely(n == sz)) {
260 n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
261 if (unlikely(n == lowest)) {
262 printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
263 n = 0;
264 }
265 }
266 if (n)
267 __set_bit(n, iommu->ctx_bitmap);
268
269 return n;
270}
271
David S. Miller16ce82d2007-04-26 21:08:21 -0700272static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
David S. Miller7c963ad2005-05-31 16:57:59 -0700273{
274 if (likely(ctx)) {
275 __clear_bit(ctx, iommu->ctx_bitmap);
276 if (ctx < iommu->ctx_lowest_free)
277 iommu->ctx_lowest_free = ctx;
278 }
279}
280
David S. Millerad7ad572007-07-27 22:39:14 -0700281static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
282 dma_addr_t *dma_addrp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
David S. Miller688cb302005-10-13 22:15:24 -0700284 unsigned long flags, order, first_page;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700285 struct iommu *iommu;
286 struct page *page;
287 int npages, nid;
288 iopte_t *iopte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 size = IO_PAGE_ALIGN(size);
292 order = get_order(size);
293 if (order >= 10)
294 return NULL;
295
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700296 nid = dev->archdata.numa_node;
297 page = alloc_pages_node(nid, gfp, order);
298 if (unlikely(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return NULL;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700300
301 first_page = (unsigned long) page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 memset((char *)first_page, 0, PAGE_SIZE << order);
303
David S. Millerad7ad572007-07-27 22:39:14 -0700304 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800307 iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT);
David S. Miller688cb302005-10-13 22:15:24 -0700308 spin_unlock_irqrestore(&iommu->lock, flags);
309
310 if (unlikely(iopte == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 free_pages(first_page, order);
312 return NULL;
313 }
314
315 *dma_addrp = (iommu->page_table_map_base +
316 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
317 ret = (void *) first_page;
318 npages = size >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 first_page = __pa(first_page);
320 while (npages--) {
David S. Miller688cb302005-10-13 22:15:24 -0700321 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 IOPTE_WRITE |
323 (first_page & IOPTE_PAGE));
324 iopte++;
325 first_page += IO_PAGE_SIZE;
326 }
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return ret;
329}
330
David S. Millerad7ad572007-07-27 22:39:14 -0700331static void dma_4u_free_coherent(struct device *dev, size_t size,
332 void *cpu, dma_addr_t dvma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
David S. Miller16ce82d2007-04-26 21:08:21 -0700334 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700336 unsigned long flags, order, npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700339 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 iopte = iommu->page_table +
341 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
342
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
David S. Millerad7ad572007-07-27 22:39:14 -0700354static dma_addr_t dma_4u_map_single(struct device *dev, void *ptr, size_t sz,
355 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
David S. Miller16ce82d2007-04-26 21:08:21 -0700357 struct iommu *iommu;
358 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 iopte_t *base;
360 unsigned long flags, npages, oaddr;
361 unsigned long i, base_paddr, ctx;
362 u32 bus_addr, ret;
363 unsigned long iopte_protection;
364
David S. Millerad7ad572007-07-27 22:39:14 -0700365 iommu = dev->archdata.iommu;
366 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
David S. Millerad7ad572007-07-27 22:39:14 -0700368 if (unlikely(direction == DMA_NONE))
David S. Miller688cb302005-10-13 22:15:24 -0700369 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 oaddr = (unsigned long)ptr;
372 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
373 npages >>= IO_PAGE_SHIFT;
374
375 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800376 base = alloc_npages(dev, iommu, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700377 ctx = 0;
378 if (iommu->iommu_ctxflush)
379 ctx = iommu_alloc_ctx(iommu);
380 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
David S. Miller688cb302005-10-13 22:15:24 -0700382 if (unlikely(!base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 bus_addr = (iommu->page_table_map_base +
386 ((base - iommu->page_table) << IO_PAGE_SHIFT));
387 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
388 base_paddr = __pa(oaddr & IO_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (strbuf->strbuf_enabled)
390 iopte_protection = IOPTE_STREAMING(ctx);
391 else
392 iopte_protection = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700393 if (direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 iopte_protection |= IOPTE_WRITE;
395
396 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
397 iopte_val(*base) = iopte_protection | base_paddr;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return ret;
400
401bad:
David S. Miller688cb302005-10-13 22:15:24 -0700402 iommu_free_ctx(iommu, ctx);
403bad_no_ctx:
404 if (printk_ratelimit())
405 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700406 return DMA_ERROR_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
David S. Millerad7ad572007-07-27 22:39:14 -0700409static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
410 u32 vaddr, unsigned long ctx, unsigned long npages,
411 enum dma_data_direction direction)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700412{
413 int limit;
414
David S. Miller4dbc30f2005-05-11 11:37:00 -0700415 if (strbuf->strbuf_ctxflush &&
416 iommu->iommu_ctxflush) {
417 unsigned long matchreg, flushreg;
David S. Miller7c963ad2005-05-31 16:57:59 -0700418 u64 val;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700419
420 flushreg = strbuf->strbuf_ctxflush;
David S. Millerad7ad572007-07-27 22:39:14 -0700421 matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700422
David S. Millerad7ad572007-07-27 22:39:14 -0700423 iommu_write(flushreg, ctx);
424 val = iommu_read(matchreg);
David S. Miller88314ee2005-05-31 19:13:52 -0700425 val &= 0xffff;
426 if (!val)
David S. Miller7c963ad2005-05-31 16:57:59 -0700427 goto do_flush_sync;
428
David S. Miller7c963ad2005-05-31 16:57:59 -0700429 while (val) {
430 if (val & 0x1)
David S. Millerad7ad572007-07-27 22:39:14 -0700431 iommu_write(flushreg, ctx);
David S. Miller7c963ad2005-05-31 16:57:59 -0700432 val >>= 1;
David S. Millera228dfd2005-05-20 11:40:32 -0700433 }
David S. Millerad7ad572007-07-27 22:39:14 -0700434 val = iommu_read(matchreg);
David S. Miller7c963ad2005-05-31 16:57:59 -0700435 if (unlikely(val)) {
David S. Millerad7ad572007-07-27 22:39:14 -0700436 printk(KERN_WARNING "strbuf_flush: ctx flush "
Sam Ravnborg90181132009-01-06 13:19:28 -0800437 "timeout matchreg[%llx] ctx[%lx]\n",
David S. Miller7c963ad2005-05-31 16:57:59 -0700438 val, ctx);
439 goto do_page_flush;
440 }
David S. Miller4dbc30f2005-05-11 11:37:00 -0700441 } else {
442 unsigned long i;
443
David S. Miller7c963ad2005-05-31 16:57:59 -0700444 do_page_flush:
David S. Miller4dbc30f2005-05-11 11:37:00 -0700445 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
David S. Millerad7ad572007-07-27 22:39:14 -0700446 iommu_write(strbuf->strbuf_pflush, vaddr);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700447 }
448
David S. Miller7c963ad2005-05-31 16:57:59 -0700449do_flush_sync:
450 /* If the device could not have possibly put dirty data into
451 * the streaming cache, no flush-flag synchronization needs
452 * to be performed.
453 */
David S. Millerad7ad572007-07-27 22:39:14 -0700454 if (direction == DMA_TO_DEVICE)
David S. Miller7c963ad2005-05-31 16:57:59 -0700455 return;
456
David S. Millerad7ad572007-07-27 22:39:14 -0700457 STC_FLUSHFLAG_INIT(strbuf);
458 iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
459 (void) iommu_read(iommu->write_complete_reg);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700460
David S. Millera228dfd2005-05-20 11:40:32 -0700461 limit = 100000;
David S. Millerad7ad572007-07-27 22:39:14 -0700462 while (!STC_FLUSHFLAG_SET(strbuf)) {
David S. Miller4dbc30f2005-05-11 11:37:00 -0700463 limit--;
464 if (!limit)
465 break;
David S. Millera228dfd2005-05-20 11:40:32 -0700466 udelay(1);
David S. Miller4f071182005-08-29 12:46:22 -0700467 rmb();
David S. Miller4dbc30f2005-05-11 11:37:00 -0700468 }
469 if (!limit)
David S. Millerad7ad572007-07-27 22:39:14 -0700470 printk(KERN_WARNING "strbuf_flush: flushflag timeout "
David S. Miller4dbc30f2005-05-11 11:37:00 -0700471 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
472 vaddr, ctx, npages);
473}
474
David S. Millerad7ad572007-07-27 22:39:14 -0700475static void dma_4u_unmap_single(struct device *dev, dma_addr_t bus_addr,
476 size_t sz, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
David S. Miller16ce82d2007-04-26 21:08:21 -0700478 struct iommu *iommu;
479 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 iopte_t *base;
David S. Miller688cb302005-10-13 22:15:24 -0700481 unsigned long flags, npages, ctx, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
David S. Millerad7ad572007-07-27 22:39:14 -0700483 if (unlikely(direction == DMA_NONE)) {
David S. Miller688cb302005-10-13 22:15:24 -0700484 if (printk_ratelimit())
485 WARN_ON(1);
486 return;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David S. Millerad7ad572007-07-27 22:39:14 -0700489 iommu = dev->archdata.iommu;
490 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
493 npages >>= IO_PAGE_SHIFT;
494 base = iommu->page_table +
495 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 bus_addr &= IO_PAGE_MASK;
497
498 spin_lock_irqsave(&iommu->lock, flags);
499
500 /* Record the context, if any. */
501 ctx = 0;
502 if (iommu->iommu_ctxflush)
503 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
504
505 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700506 if (strbuf->strbuf_enabled)
David S. Millerad7ad572007-07-27 22:39:14 -0700507 strbuf_flush(strbuf, iommu, bus_addr, ctx,
508 npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
David S. Miller688cb302005-10-13 22:15:24 -0700510 /* Step 2: Clear out TSB entries. */
511 for (i = 0; i < npages; i++)
512 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
David S. Millerd2841422008-02-08 18:05:46 -0800514 iommu_range_free(iommu, bus_addr, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
David S. Miller7c963ad2005-05-31 16:57:59 -0700516 iommu_free_ctx(iommu, ctx);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 spin_unlock_irqrestore(&iommu->lock, flags);
519}
520
David S. Millerad7ad572007-07-27 22:39:14 -0700521static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
522 int nelems, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
David S. Miller13fa14e2008-02-09 03:11:01 -0800524 struct scatterlist *s, *outs, *segstart;
525 unsigned long flags, handle, prot, ctx;
526 dma_addr_t dma_next = 0, dma_addr;
527 unsigned int max_seg_size;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700528 unsigned long seg_boundary_size;
David S. Miller13fa14e2008-02-09 03:11:01 -0800529 int outcount, incount, i;
David S. Miller16ce82d2007-04-26 21:08:21 -0700530 struct strbuf *strbuf;
David S. Miller38192d52008-02-06 03:50:26 -0800531 struct iommu *iommu;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700532 unsigned long base_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
David S. Miller13fa14e2008-02-09 03:11:01 -0800534 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
David S. Millerad7ad572007-07-27 22:39:14 -0700536 iommu = dev->archdata.iommu;
537 strbuf = dev->archdata.stc;
David S. Miller13fa14e2008-02-09 03:11:01 -0800538 if (nelems == 0 || !iommu)
539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 spin_lock_irqsave(&iommu->lock, flags);
542
David S. Miller688cb302005-10-13 22:15:24 -0700543 ctx = 0;
544 if (iommu->iommu_ctxflush)
545 ctx = iommu_alloc_ctx(iommu);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (strbuf->strbuf_enabled)
David S. Miller13fa14e2008-02-09 03:11:01 -0800548 prot = IOPTE_STREAMING(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 else
David S. Miller13fa14e2008-02-09 03:11:01 -0800550 prot = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700551 if (direction != DMA_TO_DEVICE)
David S. Miller13fa14e2008-02-09 03:11:01 -0800552 prot |= IOPTE_WRITE;
David S. Miller688cb302005-10-13 22:15:24 -0700553
David S. Miller13fa14e2008-02-09 03:11:01 -0800554 outs = s = segstart = &sglist[0];
555 outcount = 1;
556 incount = nelems;
557 handle = 0;
David S. Miller688cb302005-10-13 22:15:24 -0700558
David S. Miller13fa14e2008-02-09 03:11:01 -0800559 /* Init first segment length for backout at failure */
560 outs->dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
David S. Miller13fa14e2008-02-09 03:11:01 -0800562 max_seg_size = dma_get_max_seg_size(dev);
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700563 seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
564 IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
565 base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
David S. Miller13fa14e2008-02-09 03:11:01 -0800566 for_each_sg(sglist, s, nelems, i) {
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700567 unsigned long paddr, npages, entry, out_entry = 0, slen;
David S. Miller13fa14e2008-02-09 03:11:01 -0800568 iopte_t *base;
David S. Miller38192d52008-02-06 03:50:26 -0800569
David S. Miller13fa14e2008-02-09 03:11:01 -0800570 slen = s->length;
571 /* Sanity check */
572 if (slen == 0) {
573 dma_next = 0;
574 continue;
575 }
576 /* Allocate iommu entries for that segment */
577 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
Joerg Roedel0fcff282008-10-15 22:02:14 -0700578 npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800579 entry = iommu_range_alloc(dev, iommu, npages, &handle);
580
581 /* Handle failure */
582 if (unlikely(entry == DMA_ERROR_CODE)) {
583 if (printk_ratelimit())
584 printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
585 " npages %lx\n", iommu, paddr, npages);
586 goto iommu_map_failed;
587 }
588
589 base = iommu->page_table + entry;
590
591 /* Convert entry to a dma_addr_t */
592 dma_addr = iommu->page_table_map_base +
593 (entry << IO_PAGE_SHIFT);
594 dma_addr |= (s->offset & ~IO_PAGE_MASK);
595
596 /* Insert into HW table */
David S. Miller38192d52008-02-06 03:50:26 -0800597 paddr &= IO_PAGE_MASK;
David S. Miller13fa14e2008-02-09 03:11:01 -0800598 while (npages--) {
599 iopte_val(*base) = prot | paddr;
David S. Miller38192d52008-02-06 03:50:26 -0800600 base++;
601 paddr += IO_PAGE_SIZE;
David S. Miller38192d52008-02-06 03:50:26 -0800602 }
David S. Miller13fa14e2008-02-09 03:11:01 -0800603
604 /* If we are in an open segment, try merging */
605 if (segstart != s) {
606 /* We cannot merge if:
607 * - allocated dma_addr isn't contiguous to previous allocation
608 */
609 if ((dma_addr != dma_next) ||
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700610 (outs->dma_length + s->length > max_seg_size) ||
611 (is_span_boundary(out_entry, base_shift,
612 seg_boundary_size, outs, s))) {
David S. Miller13fa14e2008-02-09 03:11:01 -0800613 /* Can't merge: create a new segment */
614 segstart = s;
615 outcount++;
616 outs = sg_next(outs);
617 } else {
618 outs->dma_length += s->length;
619 }
620 }
621
622 if (segstart == s) {
623 /* This is a new segment, fill entries */
624 outs->dma_address = dma_addr;
625 outs->dma_length = slen;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700626 out_entry = entry;
David S. Miller13fa14e2008-02-09 03:11:01 -0800627 }
628
629 /* Calculate next page pointer for contiguous check */
630 dma_next = dma_addr + slen;
David S. Miller38192d52008-02-06 03:50:26 -0800631 }
632
David S. Miller13fa14e2008-02-09 03:11:01 -0800633 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
David S. Miller13fa14e2008-02-09 03:11:01 -0800635 if (outcount < incount) {
636 outs = sg_next(outs);
637 outs->dma_address = DMA_ERROR_CODE;
638 outs->dma_length = 0;
639 }
640
641 return outcount;
642
643iommu_map_failed:
644 for_each_sg(sglist, s, nelems, i) {
645 if (s->dma_length != 0) {
David S. Miller6c830fe2008-03-25 22:44:10 -0700646 unsigned long vaddr, npages, entry, j;
David S. Miller13fa14e2008-02-09 03:11:01 -0800647 iopte_t *base;
648
649 vaddr = s->dma_address & IO_PAGE_MASK;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700650 npages = iommu_num_pages(s->dma_address, s->dma_length,
651 IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800652 iommu_range_free(iommu, vaddr, npages);
653
654 entry = (vaddr - iommu->page_table_map_base)
655 >> IO_PAGE_SHIFT;
656 base = iommu->page_table + entry;
657
David S. Miller6c830fe2008-03-25 22:44:10 -0700658 for (j = 0; j < npages; j++)
659 iopte_make_dummy(iommu, base + j);
David S. Miller13fa14e2008-02-09 03:11:01 -0800660
661 s->dma_address = DMA_ERROR_CODE;
662 s->dma_length = 0;
663 }
664 if (s == outs)
665 break;
666 }
667 spin_unlock_irqrestore(&iommu->lock, flags);
668
David S. Miller688cb302005-10-13 22:15:24 -0700669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
David S. Miller13fa14e2008-02-09 03:11:01 -0800672/* If contexts are being used, they are the same in all of the mappings
673 * we make for a particular SG.
674 */
675static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
676{
677 unsigned long ctx = 0;
678
679 if (iommu->iommu_ctxflush) {
680 iopte_t *base;
681 u32 bus_addr;
682
683 bus_addr = sg->dma_address & IO_PAGE_MASK;
684 base = iommu->page_table +
685 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
686
687 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
688 }
689 return ctx;
690}
691
David S. Millerad7ad572007-07-27 22:39:14 -0700692static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
693 int nelems, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
David S. Miller13fa14e2008-02-09 03:11:01 -0800695 unsigned long flags, ctx;
696 struct scatterlist *sg;
David S. Miller38192d52008-02-06 03:50:26 -0800697 struct strbuf *strbuf;
698 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David S. Miller13fa14e2008-02-09 03:11:01 -0800700 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
David S. Millerad7ad572007-07-27 22:39:14 -0700702 iommu = dev->archdata.iommu;
703 strbuf = dev->archdata.stc;
704
David S. Miller13fa14e2008-02-09 03:11:01 -0800705 ctx = fetch_sg_ctx(iommu, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 spin_lock_irqsave(&iommu->lock, flags);
708
David S. Miller13fa14e2008-02-09 03:11:01 -0800709 sg = sglist;
710 while (nelems--) {
711 dma_addr_t dma_handle = sg->dma_address;
712 unsigned int len = sg->dma_length;
713 unsigned long npages, entry;
714 iopte_t *base;
715 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
David S. Miller13fa14e2008-02-09 03:11:01 -0800717 if (!len)
718 break;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700719 npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800720 iommu_range_free(iommu, dma_handle, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
David S. Miller13fa14e2008-02-09 03:11:01 -0800722 entry = ((dma_handle - iommu->page_table_map_base)
723 >> IO_PAGE_SHIFT);
724 base = iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
David S. Miller13fa14e2008-02-09 03:11:01 -0800726 dma_handle &= IO_PAGE_MASK;
727 if (strbuf->strbuf_enabled)
728 strbuf_flush(strbuf, iommu, dma_handle, ctx,
729 npages, direction);
730
731 for (i = 0; i < npages; i++)
732 iopte_make_dummy(iommu, base + i);
733
734 sg = sg_next(sg);
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
David S. Miller7c963ad2005-05-31 16:57:59 -0700737 iommu_free_ctx(iommu, ctx);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 spin_unlock_irqrestore(&iommu->lock, flags);
740}
741
David S. Millerad7ad572007-07-27 22:39:14 -0700742static void dma_4u_sync_single_for_cpu(struct device *dev,
743 dma_addr_t bus_addr, size_t sz,
744 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
David S. Miller16ce82d2007-04-26 21:08:21 -0700746 struct iommu *iommu;
747 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 unsigned long flags, ctx, npages;
749
David S. Millerad7ad572007-07-27 22:39:14 -0700750 iommu = dev->archdata.iommu;
751 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (!strbuf->strbuf_enabled)
754 return;
755
756 spin_lock_irqsave(&iommu->lock, flags);
757
758 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
759 npages >>= IO_PAGE_SHIFT;
760 bus_addr &= IO_PAGE_MASK;
761
762 /* Step 1: Record the context, if any. */
763 ctx = 0;
764 if (iommu->iommu_ctxflush &&
765 strbuf->strbuf_ctxflush) {
766 iopte_t *iopte;
767
768 iopte = iommu->page_table +
769 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
770 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
771 }
772
773 /* Step 2: Kick data out of streaming buffers. */
David S. Millerad7ad572007-07-27 22:39:14 -0700774 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 spin_unlock_irqrestore(&iommu->lock, flags);
777}
778
David S. Millerad7ad572007-07-27 22:39:14 -0700779static void dma_4u_sync_sg_for_cpu(struct device *dev,
780 struct scatterlist *sglist, int nelems,
781 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
David S. Miller16ce82d2007-04-26 21:08:21 -0700783 struct iommu *iommu;
784 struct strbuf *strbuf;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700785 unsigned long flags, ctx, npages, i;
Jens Axboe2c941a22007-08-07 09:37:10 +0200786 struct scatterlist *sg, *sgprv;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700787 u32 bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
David S. Millerad7ad572007-07-27 22:39:14 -0700789 iommu = dev->archdata.iommu;
790 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (!strbuf->strbuf_enabled)
793 return;
794
795 spin_lock_irqsave(&iommu->lock, flags);
796
797 /* Step 1: Record the context, if any. */
798 ctx = 0;
799 if (iommu->iommu_ctxflush &&
800 strbuf->strbuf_ctxflush) {
801 iopte_t *iopte;
802
803 iopte = iommu->page_table +
804 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
805 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
806 }
807
808 /* Step 2: Kick data out of streaming buffers. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700809 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
Jens Axboe2c941a22007-08-07 09:37:10 +0200810 sgprv = NULL;
811 for_each_sg(sglist, sg, nelems, i) {
812 if (sg->dma_length == 0)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700813 break;
Jens Axboe2c941a22007-08-07 09:37:10 +0200814 sgprv = sg;
815 }
816
817 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700818 - bus_addr) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700819 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 spin_unlock_irqrestore(&iommu->lock, flags);
822}
823
David S. Miller6c830fe2008-03-25 22:44:10 -0700824static const struct dma_ops sun4u_dma_ops = {
David S. Millerad7ad572007-07-27 22:39:14 -0700825 .alloc_coherent = dma_4u_alloc_coherent,
826 .free_coherent = dma_4u_free_coherent,
827 .map_single = dma_4u_map_single,
828 .unmap_single = dma_4u_unmap_single,
829 .map_sg = dma_4u_map_sg,
830 .unmap_sg = dma_4u_unmap_sg,
831 .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
832 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800833};
834
David S. Millerad7ad572007-07-27 22:39:14 -0700835const struct dma_ops *dma_ops = &sun4u_dma_ops;
836EXPORT_SYMBOL(dma_ops);
837
838int dma_supported(struct device *dev, u64 device_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
David S. Millerad7ad572007-07-27 22:39:14 -0700840 struct iommu *iommu = dev->archdata.iommu;
841 u64 dma_addr_mask = iommu->dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (device_mask >= (1UL << 32UL))
844 return 0;
845
David S. Millerad7ad572007-07-27 22:39:14 -0700846 if ((device_mask & dma_addr_mask) == dma_addr_mask)
847 return 1;
848
849#ifdef CONFIG_PCI
850 if (dev->bus == &pci_bus_type)
851 return pci_dma_supported(to_pci_dev(dev), device_mask);
852#endif
853
854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
David S. Millerad7ad572007-07-27 22:39:14 -0700856EXPORT_SYMBOL(dma_supported);
857
858int dma_set_mask(struct device *dev, u64 dma_mask)
859{
860#ifdef CONFIG_PCI
861 if (dev->bus == &pci_bus_type)
862 return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
863#endif
864 return -EINVAL;
865}
866EXPORT_SYMBOL(dma_set_mask);