blob: db3ffcf7a12036bee200f0ca207cf52bcab95e23 [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. Miller16ce82d2007-04-26 21:08:21 -07003 * Copyright (C) 1999, 2007 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>
Jens Axboe2c941a22007-08-07 09:37:10 +020013#include <linux/scatterlist.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. Miller16ce82d2007-04-26 21:08:21 -070045static 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. Miller688cb302005-10-13 22:15:24 -070087/* Based largely upon the ppc64 iommu allocator. */
David S. Millerad7ad572007-07-27 22:39:14 -070088static long arena_alloc(struct iommu *iommu, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -070089{
David S. Miller9b3627f2007-04-24 23:51:18 -070090 struct iommu_arena *arena = &iommu->arena;
David S. Miller688cb302005-10-13 22:15:24 -070091 unsigned long n, i, start, end, limit;
92 int pass;
93
94 limit = arena->limit;
95 start = arena->hint;
96 pass = 0;
97
98again:
99 n = find_next_zero_bit(arena->map, limit, start);
100 end = n + npages;
101 if (unlikely(end >= limit)) {
102 if (likely(pass < 1)) {
103 limit = start;
104 start = 0;
105 __iommu_flushall(iommu);
106 pass++;
107 goto again;
108 } else {
109 /* Scanned the whole thing, give up. */
110 return -1;
111 }
112 }
113
114 for (i = n; i < end; i++) {
115 if (test_bit(i, arena->map)) {
116 start = i + 1;
117 goto again;
118 }
119 }
120
121 for (i = n; i < end; i++)
122 __set_bit(i, arena->map);
123
124 arena->hint = end;
125
126 return n;
127}
128
David S. Millerad7ad572007-07-27 22:39:14 -0700129static void arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -0700130{
131 unsigned long i;
132
133 for (i = base; i < (base + npages); i++)
134 __clear_bit(i, arena->map);
135}
136
David S. Millerad7ad572007-07-27 22:39:14 -0700137int iommu_table_init(struct iommu *iommu, int tsbsize,
138 u32 dma_offset, u32 dma_addr_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
David S. Miller688cb302005-10-13 22:15:24 -0700140 unsigned long i, tsbbase, order, sz, num_tsb_entries;
141
142 num_tsb_entries = tsbsize / sizeof(iopte_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
David S. Miller51e85132005-10-13 21:10:08 -0700144 /* Setup initial software IOMMU state. */
145 spin_lock_init(&iommu->lock);
146 iommu->ctx_lowest_free = 1;
147 iommu->page_table_map_base = dma_offset;
148 iommu->dma_addr_mask = dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
David S. Miller688cb302005-10-13 22:15:24 -0700150 /* Allocate and initialize the free area map. */
151 sz = num_tsb_entries / 8;
152 sz = (sz + 7UL) & ~7UL;
Eric Sesterhenn91329832006-03-06 13:48:40 -0800153 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
David S. Miller688cb302005-10-13 22:15:24 -0700154 if (!iommu->arena.map) {
David S. Millerad7ad572007-07-27 22:39:14 -0700155 printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
156 return -ENOMEM;
David S. Miller51e85132005-10-13 21:10:08 -0700157 }
David S. Miller688cb302005-10-13 22:15:24 -0700158 iommu->arena.limit = num_tsb_entries;
David S. Miller51e85132005-10-13 21:10:08 -0700159
160 /* Allocate and initialize the dummy page which we
161 * set inactive IO PTEs to point to.
162 */
163 iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0);
164 if (!iommu->dummy_page) {
David S. Millerad7ad572007-07-27 22:39:14 -0700165 printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
166 goto out_free_map;
David S. Miller51e85132005-10-13 21:10:08 -0700167 }
168 memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
169 iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
170
171 /* Now allocate and setup the IOMMU page table itself. */
172 order = get_order(tsbsize);
173 tsbbase = __get_free_pages(GFP_KERNEL, order);
174 if (!tsbbase) {
David S. Millerad7ad572007-07-27 22:39:14 -0700175 printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
176 goto out_free_dummy_page;
David S. Miller51e85132005-10-13 21:10:08 -0700177 }
178 iommu->page_table = (iopte_t *)tsbbase;
179
David S. Miller688cb302005-10-13 22:15:24 -0700180 for (i = 0; i < num_tsb_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 iopte_make_dummy(iommu, &iommu->page_table[i]);
David S. Millerad7ad572007-07-27 22:39:14 -0700182
183 return 0;
184
185out_free_dummy_page:
186 free_page(iommu->dummy_page);
187 iommu->dummy_page = 0UL;
188
189out_free_map:
190 kfree(iommu->arena.map);
191 iommu->arena.map = NULL;
192
193 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
David S. Miller16ce82d2007-04-26 21:08:21 -0700196static inline iopte_t *alloc_npages(struct iommu *iommu, unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
David S. Miller688cb302005-10-13 22:15:24 -0700198 long entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
David S. Millerad7ad572007-07-27 22:39:14 -0700200 entry = arena_alloc(iommu, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700201 if (unlikely(entry < 0))
202 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
David S. Miller688cb302005-10-13 22:15:24 -0700204 return iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
David S. Miller16ce82d2007-04-26 21:08:21 -0700207static inline void free_npages(struct iommu *iommu, dma_addr_t base, unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
David S. Millerad7ad572007-07-27 22:39:14 -0700209 arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
David S. Miller16ce82d2007-04-26 21:08:21 -0700212static int iommu_alloc_ctx(struct iommu *iommu)
David S. Miller7c963ad2005-05-31 16:57:59 -0700213{
214 int lowest = iommu->ctx_lowest_free;
215 int sz = IOMMU_NUM_CTXS - lowest;
216 int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
217
218 if (unlikely(n == sz)) {
219 n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
220 if (unlikely(n == lowest)) {
221 printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
222 n = 0;
223 }
224 }
225 if (n)
226 __set_bit(n, iommu->ctx_bitmap);
227
228 return n;
229}
230
David S. Miller16ce82d2007-04-26 21:08:21 -0700231static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
David S. Miller7c963ad2005-05-31 16:57:59 -0700232{
233 if (likely(ctx)) {
234 __clear_bit(ctx, iommu->ctx_bitmap);
235 if (ctx < iommu->ctx_lowest_free)
236 iommu->ctx_lowest_free = ctx;
237 }
238}
239
David S. Millerad7ad572007-07-27 22:39:14 -0700240static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
241 dma_addr_t *dma_addrp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
David S. Miller16ce82d2007-04-26 21:08:21 -0700243 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700245 unsigned long flags, order, first_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 void *ret;
247 int npages;
248
249 size = IO_PAGE_ALIGN(size);
250 order = get_order(size);
251 if (order >= 10)
252 return NULL;
253
David S. Miller42f14232006-05-23 02:07:22 -0700254 first_page = __get_free_pages(gfp, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (first_page == 0UL)
256 return NULL;
257 memset((char *)first_page, 0, PAGE_SIZE << order);
258
David S. Millerad7ad572007-07-27 22:39:14 -0700259 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller688cb302005-10-13 22:15:24 -0700262 iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
263 spin_unlock_irqrestore(&iommu->lock, flags);
264
265 if (unlikely(iopte == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 free_pages(first_page, order);
267 return NULL;
268 }
269
270 *dma_addrp = (iommu->page_table_map_base +
271 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
272 ret = (void *) first_page;
273 npages = size >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 first_page = __pa(first_page);
275 while (npages--) {
David S. Miller688cb302005-10-13 22:15:24 -0700276 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 IOPTE_WRITE |
278 (first_page & IOPTE_PAGE));
279 iopte++;
280 first_page += IO_PAGE_SIZE;
281 }
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return ret;
284}
285
David S. Millerad7ad572007-07-27 22:39:14 -0700286static void dma_4u_free_coherent(struct device *dev, size_t size,
287 void *cpu, dma_addr_t dvma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
David S. Miller16ce82d2007-04-26 21:08:21 -0700289 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700291 unsigned long flags, order, npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700294 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 iopte = iommu->page_table +
296 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
297
298 spin_lock_irqsave(&iommu->lock, flags);
299
David S. Miller012d64f2006-10-25 22:33:07 -0700300 free_npages(iommu, dvma - iommu->page_table_map_base, npages);
David S. Miller7c963ad2005-05-31 16:57:59 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 spin_unlock_irqrestore(&iommu->lock, flags);
303
304 order = get_order(size);
305 if (order < 10)
306 free_pages((unsigned long)cpu, order);
307}
308
David S. Millerad7ad572007-07-27 22:39:14 -0700309static dma_addr_t dma_4u_map_single(struct device *dev, void *ptr, size_t sz,
310 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
David S. Miller16ce82d2007-04-26 21:08:21 -0700312 struct iommu *iommu;
313 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 iopte_t *base;
315 unsigned long flags, npages, oaddr;
316 unsigned long i, base_paddr, ctx;
317 u32 bus_addr, ret;
318 unsigned long iopte_protection;
319
David S. Millerad7ad572007-07-27 22:39:14 -0700320 iommu = dev->archdata.iommu;
321 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
David S. Millerad7ad572007-07-27 22:39:14 -0700323 if (unlikely(direction == DMA_NONE))
David S. Miller688cb302005-10-13 22:15:24 -0700324 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 oaddr = (unsigned long)ptr;
327 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
328 npages >>= IO_PAGE_SHIFT;
329
330 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller688cb302005-10-13 22:15:24 -0700331 base = alloc_npages(iommu, npages);
332 ctx = 0;
333 if (iommu->iommu_ctxflush)
334 ctx = iommu_alloc_ctx(iommu);
335 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David S. Miller688cb302005-10-13 22:15:24 -0700337 if (unlikely(!base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 bus_addr = (iommu->page_table_map_base +
341 ((base - iommu->page_table) << IO_PAGE_SHIFT));
342 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
343 base_paddr = __pa(oaddr & IO_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (strbuf->strbuf_enabled)
345 iopte_protection = IOPTE_STREAMING(ctx);
346 else
347 iopte_protection = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700348 if (direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 iopte_protection |= IOPTE_WRITE;
350
351 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
352 iopte_val(*base) = iopte_protection | base_paddr;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return ret;
355
356bad:
David S. Miller688cb302005-10-13 22:15:24 -0700357 iommu_free_ctx(iommu, ctx);
358bad_no_ctx:
359 if (printk_ratelimit())
360 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700361 return DMA_ERROR_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
David S. Millerad7ad572007-07-27 22:39:14 -0700364static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
365 u32 vaddr, unsigned long ctx, unsigned long npages,
366 enum dma_data_direction direction)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700367{
368 int limit;
369
David S. Miller4dbc30f2005-05-11 11:37:00 -0700370 if (strbuf->strbuf_ctxflush &&
371 iommu->iommu_ctxflush) {
372 unsigned long matchreg, flushreg;
David S. Miller7c963ad2005-05-31 16:57:59 -0700373 u64 val;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700374
375 flushreg = strbuf->strbuf_ctxflush;
David S. Millerad7ad572007-07-27 22:39:14 -0700376 matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700377
David S. Millerad7ad572007-07-27 22:39:14 -0700378 iommu_write(flushreg, ctx);
379 val = iommu_read(matchreg);
David S. Miller88314ee2005-05-31 19:13:52 -0700380 val &= 0xffff;
381 if (!val)
David S. Miller7c963ad2005-05-31 16:57:59 -0700382 goto do_flush_sync;
383
David S. Miller7c963ad2005-05-31 16:57:59 -0700384 while (val) {
385 if (val & 0x1)
David S. Millerad7ad572007-07-27 22:39:14 -0700386 iommu_write(flushreg, ctx);
David S. Miller7c963ad2005-05-31 16:57:59 -0700387 val >>= 1;
David S. Millera228dfd2005-05-20 11:40:32 -0700388 }
David S. Millerad7ad572007-07-27 22:39:14 -0700389 val = iommu_read(matchreg);
David S. Miller7c963ad2005-05-31 16:57:59 -0700390 if (unlikely(val)) {
David S. Millerad7ad572007-07-27 22:39:14 -0700391 printk(KERN_WARNING "strbuf_flush: ctx flush "
David S. Miller7c963ad2005-05-31 16:57:59 -0700392 "timeout matchreg[%lx] ctx[%lx]\n",
393 val, ctx);
394 goto do_page_flush;
395 }
David S. Miller4dbc30f2005-05-11 11:37:00 -0700396 } else {
397 unsigned long i;
398
David S. Miller7c963ad2005-05-31 16:57:59 -0700399 do_page_flush:
David S. Miller4dbc30f2005-05-11 11:37:00 -0700400 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
David S. Millerad7ad572007-07-27 22:39:14 -0700401 iommu_write(strbuf->strbuf_pflush, vaddr);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700402 }
403
David S. Miller7c963ad2005-05-31 16:57:59 -0700404do_flush_sync:
405 /* If the device could not have possibly put dirty data into
406 * the streaming cache, no flush-flag synchronization needs
407 * to be performed.
408 */
David S. Millerad7ad572007-07-27 22:39:14 -0700409 if (direction == DMA_TO_DEVICE)
David S. Miller7c963ad2005-05-31 16:57:59 -0700410 return;
411
David S. Millerad7ad572007-07-27 22:39:14 -0700412 STC_FLUSHFLAG_INIT(strbuf);
413 iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
414 (void) iommu_read(iommu->write_complete_reg);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700415
David S. Millera228dfd2005-05-20 11:40:32 -0700416 limit = 100000;
David S. Millerad7ad572007-07-27 22:39:14 -0700417 while (!STC_FLUSHFLAG_SET(strbuf)) {
David S. Miller4dbc30f2005-05-11 11:37:00 -0700418 limit--;
419 if (!limit)
420 break;
David S. Millera228dfd2005-05-20 11:40:32 -0700421 udelay(1);
David S. Miller4f071182005-08-29 12:46:22 -0700422 rmb();
David S. Miller4dbc30f2005-05-11 11:37:00 -0700423 }
424 if (!limit)
David S. Millerad7ad572007-07-27 22:39:14 -0700425 printk(KERN_WARNING "strbuf_flush: flushflag timeout "
David S. Miller4dbc30f2005-05-11 11:37:00 -0700426 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
427 vaddr, ctx, npages);
428}
429
David S. Millerad7ad572007-07-27 22:39:14 -0700430static void dma_4u_unmap_single(struct device *dev, dma_addr_t bus_addr,
431 size_t sz, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
David S. Miller16ce82d2007-04-26 21:08:21 -0700433 struct iommu *iommu;
434 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 iopte_t *base;
David S. Miller688cb302005-10-13 22:15:24 -0700436 unsigned long flags, npages, ctx, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
David S. Millerad7ad572007-07-27 22:39:14 -0700438 if (unlikely(direction == DMA_NONE)) {
David S. Miller688cb302005-10-13 22:15:24 -0700439 if (printk_ratelimit())
440 WARN_ON(1);
441 return;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
David S. Millerad7ad572007-07-27 22:39:14 -0700444 iommu = dev->archdata.iommu;
445 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
448 npages >>= IO_PAGE_SHIFT;
449 base = iommu->page_table +
450 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 bus_addr &= IO_PAGE_MASK;
452
453 spin_lock_irqsave(&iommu->lock, flags);
454
455 /* Record the context, if any. */
456 ctx = 0;
457 if (iommu->iommu_ctxflush)
458 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
459
460 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700461 if (strbuf->strbuf_enabled)
David S. Millerad7ad572007-07-27 22:39:14 -0700462 strbuf_flush(strbuf, iommu, bus_addr, ctx,
463 npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
David S. Miller688cb302005-10-13 22:15:24 -0700465 /* Step 2: Clear out TSB entries. */
466 for (i = 0; i < npages; i++)
467 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
David S. Miller688cb302005-10-13 22:15:24 -0700469 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
David S. Miller7c963ad2005-05-31 16:57:59 -0700471 iommu_free_ctx(iommu, ctx);
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 spin_unlock_irqrestore(&iommu->lock, flags);
474}
475
476#define SG_ENT_PHYS_ADDRESS(SG) \
477 (__pa(page_address((SG)->page)) + (SG)->offset)
478
479static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
David S. Millerad7ad572007-07-27 22:39:14 -0700480 int nused, int nelems,
481 unsigned long iopte_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct scatterlist *dma_sg = sg;
Jens Axboe2c941a22007-08-07 09:37:10 +0200484 struct scatterlist *sg_end = sg_last(sg, nelems);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int i;
486
487 for (i = 0; i < nused; i++) {
488 unsigned long pteval = ~0UL;
489 u32 dma_npages;
490
491 dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
492 dma_sg->dma_length +
493 ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
494 do {
495 unsigned long offset;
496 signed int len;
497
498 /* If we are here, we know we have at least one
499 * more page to map. So walk forward until we
500 * hit a page crossing, and begin creating new
501 * mappings from that spot.
502 */
503 for (;;) {
504 unsigned long tmp;
505
506 tmp = SG_ENT_PHYS_ADDRESS(sg);
507 len = sg->length;
508 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
509 pteval = tmp & IO_PAGE_MASK;
510 offset = tmp & (IO_PAGE_SIZE - 1UL);
511 break;
512 }
513 if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
514 pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
515 offset = 0UL;
516 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
517 break;
518 }
Jens Axboe2c941a22007-08-07 09:37:10 +0200519 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 pteval = iopte_protection | (pteval & IOPTE_PAGE);
523 while (len > 0) {
524 *iopte++ = __iopte(pteval);
525 pteval += IO_PAGE_SIZE;
526 len -= (IO_PAGE_SIZE - offset);
527 offset = 0;
528 dma_npages--;
529 }
530
531 pteval = (pteval & IOPTE_PAGE) + len;
Jens Axboe2c941a22007-08-07 09:37:10 +0200532 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Skip over any tail mappings we've fully mapped,
535 * adjusting pteval along the way. Stop when we
536 * detect a page crossing event.
537 */
Jens Axboe2c941a22007-08-07 09:37:10 +0200538 while (sg != sg_end &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
540 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
541 ((pteval ^
542 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
543 pteval += sg->length;
Jens Axboe2c941a22007-08-07 09:37:10 +0200544 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
547 pteval = ~0UL;
548 } while (dma_npages != 0);
Jens Axboe2c941a22007-08-07 09:37:10 +0200549 dma_sg = sg_next(dma_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551}
552
David S. Millerad7ad572007-07-27 22:39:14 -0700553static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
554 int nelems, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
David S. Miller16ce82d2007-04-26 21:08:21 -0700556 struct iommu *iommu;
557 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned long flags, ctx, npages, iopte_protection;
559 iopte_t *base;
560 u32 dma_base;
561 struct scatterlist *sgtmp;
562 int used;
563
564 /* Fast path single entry scatterlists. */
565 if (nelems == 1) {
566 sglist->dma_address =
David S. Millerad7ad572007-07-27 22:39:14 -0700567 dma_4u_map_single(dev,
568 (page_address(sglist->page) +
569 sglist->offset),
David S. Miller18397942006-02-10 00:08:26 -0800570 sglist->length, direction);
David S. Millerad7ad572007-07-27 22:39:14 -0700571 if (unlikely(sglist->dma_address == DMA_ERROR_CODE))
David S. Miller688cb302005-10-13 22:15:24 -0700572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 sglist->dma_length = sglist->length;
574 return 1;
575 }
576
David S. Millerad7ad572007-07-27 22:39:14 -0700577 iommu = dev->archdata.iommu;
578 strbuf = dev->archdata.stc;
579
580 if (unlikely(direction == DMA_NONE))
David S. Miller688cb302005-10-13 22:15:24 -0700581 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* Step 1: Prepare scatter list. */
584
585 npages = prepare_sg(sglist, nelems);
586
David S. Miller688cb302005-10-13 22:15:24 -0700587 /* Step 2: Allocate a cluster and context, if necessary. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 spin_lock_irqsave(&iommu->lock, flags);
590
David S. Miller688cb302005-10-13 22:15:24 -0700591 base = alloc_npages(iommu, npages);
592 ctx = 0;
593 if (iommu->iommu_ctxflush)
594 ctx = iommu_alloc_ctx(iommu);
595
596 spin_unlock_irqrestore(&iommu->lock, flags);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (base == NULL)
599 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700600
601 dma_base = iommu->page_table_map_base +
602 ((base - iommu->page_table) << IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* Step 3: Normalize DMA addresses. */
605 used = nelems;
606
607 sgtmp = sglist;
608 while (used && sgtmp->dma_length) {
609 sgtmp->dma_address += dma_base;
Jens Axboe2c941a22007-08-07 09:37:10 +0200610 sgtmp = sg_next(sgtmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 used--;
612 }
613 used = nelems - used;
614
David S. Miller688cb302005-10-13 22:15:24 -0700615 /* Step 4: Create the mappings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (strbuf->strbuf_enabled)
617 iopte_protection = IOPTE_STREAMING(ctx);
618 else
619 iopte_protection = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700620 if (direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 iopte_protection |= IOPTE_WRITE;
David S. Miller688cb302005-10-13 22:15:24 -0700622
623 fill_sg(base, sglist, used, nelems, iopte_protection);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#ifdef VERIFY_SG
626 verify_sglist(sglist, nelems, base, npages);
627#endif
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return used;
630
631bad:
David S. Miller688cb302005-10-13 22:15:24 -0700632 iommu_free_ctx(iommu, ctx);
633bad_no_ctx:
634 if (printk_ratelimit())
635 WARN_ON(1);
636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
David S. Millerad7ad572007-07-27 22:39:14 -0700639static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
640 int nelems, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
David S. Miller16ce82d2007-04-26 21:08:21 -0700642 struct iommu *iommu;
643 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 iopte_t *base;
645 unsigned long flags, ctx, i, npages;
Jens Axboe2c941a22007-08-07 09:37:10 +0200646 struct scatterlist *sg, *sgprv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 u32 bus_addr;
648
David S. Millerad7ad572007-07-27 22:39:14 -0700649 if (unlikely(direction == DMA_NONE)) {
David S. Miller688cb302005-10-13 22:15:24 -0700650 if (printk_ratelimit())
651 WARN_ON(1);
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
David S. Millerad7ad572007-07-27 22:39:14 -0700654 iommu = dev->archdata.iommu;
655 strbuf = dev->archdata.stc;
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 bus_addr = sglist->dma_address & IO_PAGE_MASK;
658
Jens Axboe2c941a22007-08-07 09:37:10 +0200659 sgprv = NULL;
660 for_each_sg(sglist, sg, nelems, i) {
661 if (sg->dma_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
Jens Axboe2c941a22007-08-07 09:37:10 +0200663 sgprv = sg;
664 }
665
666 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length) -
David S. Miller688cb302005-10-13 22:15:24 -0700667 bus_addr) >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 base = iommu->page_table +
670 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 spin_lock_irqsave(&iommu->lock, flags);
673
674 /* Record the context, if any. */
675 ctx = 0;
676 if (iommu->iommu_ctxflush)
677 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
678
679 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700680 if (strbuf->strbuf_enabled)
David S. Millerad7ad572007-07-27 22:39:14 -0700681 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
David S. Miller688cb302005-10-13 22:15:24 -0700683 /* Step 2: Clear out the TSB entries. */
684 for (i = 0; i < npages; i++)
685 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
David S. Miller688cb302005-10-13 22:15:24 -0700687 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David S. Miller7c963ad2005-05-31 16:57:59 -0700689 iommu_free_ctx(iommu, ctx);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 spin_unlock_irqrestore(&iommu->lock, flags);
692}
693
David S. Millerad7ad572007-07-27 22:39:14 -0700694static void dma_4u_sync_single_for_cpu(struct device *dev,
695 dma_addr_t bus_addr, size_t sz,
696 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
David S. Miller16ce82d2007-04-26 21:08:21 -0700698 struct iommu *iommu;
699 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 unsigned long flags, ctx, npages;
701
David S. Millerad7ad572007-07-27 22:39:14 -0700702 iommu = dev->archdata.iommu;
703 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (!strbuf->strbuf_enabled)
706 return;
707
708 spin_lock_irqsave(&iommu->lock, flags);
709
710 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
711 npages >>= IO_PAGE_SHIFT;
712 bus_addr &= IO_PAGE_MASK;
713
714 /* Step 1: Record the context, if any. */
715 ctx = 0;
716 if (iommu->iommu_ctxflush &&
717 strbuf->strbuf_ctxflush) {
718 iopte_t *iopte;
719
720 iopte = iommu->page_table +
721 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
722 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
723 }
724
725 /* Step 2: Kick data out of streaming buffers. */
David S. Millerad7ad572007-07-27 22:39:14 -0700726 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 spin_unlock_irqrestore(&iommu->lock, flags);
729}
730
David S. Millerad7ad572007-07-27 22:39:14 -0700731static void dma_4u_sync_sg_for_cpu(struct device *dev,
732 struct scatterlist *sglist, int nelems,
733 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
David S. Miller16ce82d2007-04-26 21:08:21 -0700735 struct iommu *iommu;
736 struct strbuf *strbuf;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700737 unsigned long flags, ctx, npages, i;
Jens Axboe2c941a22007-08-07 09:37:10 +0200738 struct scatterlist *sg, *sgprv;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700739 u32 bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
David S. Millerad7ad572007-07-27 22:39:14 -0700741 iommu = dev->archdata.iommu;
742 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 if (!strbuf->strbuf_enabled)
745 return;
746
747 spin_lock_irqsave(&iommu->lock, flags);
748
749 /* Step 1: Record the context, if any. */
750 ctx = 0;
751 if (iommu->iommu_ctxflush &&
752 strbuf->strbuf_ctxflush) {
753 iopte_t *iopte;
754
755 iopte = iommu->page_table +
756 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
757 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
758 }
759
760 /* Step 2: Kick data out of streaming buffers. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700761 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
Jens Axboe2c941a22007-08-07 09:37:10 +0200762 sgprv = NULL;
763 for_each_sg(sglist, sg, nelems, i) {
764 if (sg->dma_length == 0)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700765 break;
Jens Axboe2c941a22007-08-07 09:37:10 +0200766 sgprv = sg;
767 }
768
769 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700770 - bus_addr) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700771 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 spin_unlock_irqrestore(&iommu->lock, flags);
774}
775
David S. Millerad7ad572007-07-27 22:39:14 -0700776const struct dma_ops sun4u_dma_ops = {
777 .alloc_coherent = dma_4u_alloc_coherent,
778 .free_coherent = dma_4u_free_coherent,
779 .map_single = dma_4u_map_single,
780 .unmap_single = dma_4u_unmap_single,
781 .map_sg = dma_4u_map_sg,
782 .unmap_sg = dma_4u_unmap_sg,
783 .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
784 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800785};
786
David S. Millerad7ad572007-07-27 22:39:14 -0700787const struct dma_ops *dma_ops = &sun4u_dma_ops;
788EXPORT_SYMBOL(dma_ops);
789
790int dma_supported(struct device *dev, u64 device_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
David S. Millerad7ad572007-07-27 22:39:14 -0700792 struct iommu *iommu = dev->archdata.iommu;
793 u64 dma_addr_mask = iommu->dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (device_mask >= (1UL << 32UL))
796 return 0;
797
David S. Millerad7ad572007-07-27 22:39:14 -0700798 if ((device_mask & dma_addr_mask) == dma_addr_mask)
799 return 1;
800
801#ifdef CONFIG_PCI
802 if (dev->bus == &pci_bus_type)
803 return pci_dma_supported(to_pci_dev(dev), device_mask);
804#endif
805
806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
David S. Millerad7ad572007-07-27 22:39:14 -0700808EXPORT_SYMBOL(dma_supported);
809
810int dma_set_mask(struct device *dev, u64 dma_mask)
811{
812#ifdef CONFIG_PCI
813 if (dev->bus == &pci_bus_type)
814 return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
815#endif
816 return -EINVAL;
817}
818EXPORT_SYMBOL(dma_set_mask);