blob: 070a4846c0cb7d7dcb06be192bcd8879733f5112 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
David S. Millerad7ad572007-07-27 22:39:14 -070014#ifdef CONFIG_PCI
15#include <linux/pci.h>
16#endif
17
18#include <asm/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "iommu_common.h"
21
David S. Millerad7ad572007-07-27 22:39:14 -070022#define STC_CTXMATCH_ADDR(STC, CTX) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
David S. Millerad7ad572007-07-27 22:39:14 -070024#define STC_FLUSHFLAG_INIT(STC) \
25 (*((STC)->strbuf_flushflag) = 0UL)
26#define STC_FLUSHFLAG_SET(STC) \
27 (*((STC)->strbuf_flushflag) != 0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David S. Millerad7ad572007-07-27 22:39:14 -070029#define iommu_read(__reg) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070030({ u64 __ret; \
31 __asm__ __volatile__("ldxa [%1] %2, %0" \
32 : "=r" (__ret) \
33 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
34 : "memory"); \
35 __ret; \
36})
David S. Millerad7ad572007-07-27 22:39:14 -070037#define iommu_write(__reg, __val) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 __asm__ __volatile__("stxa %0, [%1] %2" \
39 : /* no outputs */ \
40 : "r" (__val), "r" (__reg), \
41 "i" (ASI_PHYS_BYPASS_EC_E))
42
43/* Must be invoked under the IOMMU lock. */
David S. Miller16ce82d2007-04-26 21:08:21 -070044static void __iommu_flushall(struct iommu *iommu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
David S. Miller861fe902007-05-02 17:31:36 -070046 if (iommu->iommu_flushinv) {
David S. Millerad7ad572007-07-27 22:39:14 -070047 iommu_write(iommu->iommu_flushinv, ~(u64)0);
David S. Miller861fe902007-05-02 17:31:36 -070048 } else {
49 unsigned long tag;
50 int entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David S. Millerad7ad572007-07-27 22:39:14 -070052 tag = iommu->iommu_tags;
David S. Miller861fe902007-05-02 17:31:36 -070053 for (entry = 0; entry < 16; entry++) {
David S. Millerad7ad572007-07-27 22:39:14 -070054 iommu_write(tag, 0);
David S. Miller861fe902007-05-02 17:31:36 -070055 tag += 8;
56 }
57
58 /* Ensure completion of previous PIO writes. */
David S. Millerad7ad572007-07-27 22:39:14 -070059 (void) iommu_read(iommu->write_complete_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63#define IOPTE_CONSISTENT(CTX) \
64 (IOPTE_VALID | IOPTE_CACHE | \
65 (((CTX) << 47) & IOPTE_CONTEXT))
66
67#define IOPTE_STREAMING(CTX) \
68 (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
69
70/* Existing mappings are never marked invalid, instead they
71 * are pointed to a dummy page.
72 */
73#define IOPTE_IS_DUMMY(iommu, iopte) \
74 ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
75
David S. Miller16ce82d2007-04-26 21:08:21 -070076static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 unsigned long val = iopte_val(*iopte);
79
80 val &= ~IOPTE_PAGE;
81 val |= iommu->dummy_page_pa;
82
83 iopte_val(*iopte) = val;
84}
85
David S. Miller688cb302005-10-13 22:15:24 -070086/* Based largely upon the ppc64 iommu allocator. */
David S. Millerad7ad572007-07-27 22:39:14 -070087static long arena_alloc(struct iommu *iommu, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -070088{
David S. Miller9b3627f2007-04-24 23:51:18 -070089 struct iommu_arena *arena = &iommu->arena;
David S. Miller688cb302005-10-13 22:15:24 -070090 unsigned long n, i, start, end, limit;
91 int pass;
92
93 limit = arena->limit;
94 start = arena->hint;
95 pass = 0;
96
97again:
98 n = find_next_zero_bit(arena->map, limit, start);
99 end = n + npages;
100 if (unlikely(end >= limit)) {
101 if (likely(pass < 1)) {
102 limit = start;
103 start = 0;
104 __iommu_flushall(iommu);
105 pass++;
106 goto again;
107 } else {
108 /* Scanned the whole thing, give up. */
109 return -1;
110 }
111 }
112
113 for (i = n; i < end; i++) {
114 if (test_bit(i, arena->map)) {
115 start = i + 1;
116 goto again;
117 }
118 }
119
120 for (i = n; i < end; i++)
121 __set_bit(i, arena->map);
122
123 arena->hint = end;
124
125 return n;
126}
127
David S. Millerad7ad572007-07-27 22:39:14 -0700128static void arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -0700129{
130 unsigned long i;
131
132 for (i = base; i < (base + npages); i++)
133 __clear_bit(i, arena->map);
134}
135
David S. Millerad7ad572007-07-27 22:39:14 -0700136int iommu_table_init(struct iommu *iommu, int tsbsize,
137 u32 dma_offset, u32 dma_addr_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
David S. Miller688cb302005-10-13 22:15:24 -0700139 unsigned long i, tsbbase, order, sz, num_tsb_entries;
140
141 num_tsb_entries = tsbsize / sizeof(iopte_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
David S. Miller51e85132005-10-13 21:10:08 -0700143 /* Setup initial software IOMMU state. */
144 spin_lock_init(&iommu->lock);
145 iommu->ctx_lowest_free = 1;
146 iommu->page_table_map_base = dma_offset;
147 iommu->dma_addr_mask = dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
David S. Miller688cb302005-10-13 22:15:24 -0700149 /* Allocate and initialize the free area map. */
150 sz = num_tsb_entries / 8;
151 sz = (sz + 7UL) & ~7UL;
Eric Sesterhenn91329832006-03-06 13:48:40 -0800152 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
David S. Miller688cb302005-10-13 22:15:24 -0700153 if (!iommu->arena.map) {
David S. Millerad7ad572007-07-27 22:39:14 -0700154 printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
155 return -ENOMEM;
David S. Miller51e85132005-10-13 21:10:08 -0700156 }
David S. Miller688cb302005-10-13 22:15:24 -0700157 iommu->arena.limit = num_tsb_entries;
David S. Miller51e85132005-10-13 21:10:08 -0700158
159 /* Allocate and initialize the dummy page which we
160 * set inactive IO PTEs to point to.
161 */
162 iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0);
163 if (!iommu->dummy_page) {
David S. Millerad7ad572007-07-27 22:39:14 -0700164 printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
165 goto out_free_map;
David S. Miller51e85132005-10-13 21:10:08 -0700166 }
167 memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
168 iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
169
170 /* Now allocate and setup the IOMMU page table itself. */
171 order = get_order(tsbsize);
172 tsbbase = __get_free_pages(GFP_KERNEL, order);
173 if (!tsbbase) {
David S. Millerad7ad572007-07-27 22:39:14 -0700174 printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
175 goto out_free_dummy_page;
David S. Miller51e85132005-10-13 21:10:08 -0700176 }
177 iommu->page_table = (iopte_t *)tsbbase;
178
David S. Miller688cb302005-10-13 22:15:24 -0700179 for (i = 0; i < num_tsb_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 iopte_make_dummy(iommu, &iommu->page_table[i]);
David S. Millerad7ad572007-07-27 22:39:14 -0700181
182 return 0;
183
184out_free_dummy_page:
185 free_page(iommu->dummy_page);
186 iommu->dummy_page = 0UL;
187
188out_free_map:
189 kfree(iommu->arena.map);
190 iommu->arena.map = NULL;
191
192 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
David S. Miller16ce82d2007-04-26 21:08:21 -0700195static inline iopte_t *alloc_npages(struct iommu *iommu, unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
David S. Miller688cb302005-10-13 22:15:24 -0700197 long entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
David S. Millerad7ad572007-07-27 22:39:14 -0700199 entry = arena_alloc(iommu, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700200 if (unlikely(entry < 0))
201 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
David S. Miller688cb302005-10-13 22:15:24 -0700203 return iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
David S. Miller16ce82d2007-04-26 21:08:21 -0700206static inline void free_npages(struct iommu *iommu, dma_addr_t base, unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
David S. Millerad7ad572007-07-27 22:39:14 -0700208 arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
David S. Miller16ce82d2007-04-26 21:08:21 -0700211static int iommu_alloc_ctx(struct iommu *iommu)
David S. Miller7c963ad2005-05-31 16:57:59 -0700212{
213 int lowest = iommu->ctx_lowest_free;
214 int sz = IOMMU_NUM_CTXS - lowest;
215 int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
216
217 if (unlikely(n == sz)) {
218 n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
219 if (unlikely(n == lowest)) {
220 printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
221 n = 0;
222 }
223 }
224 if (n)
225 __set_bit(n, iommu->ctx_bitmap);
226
227 return n;
228}
229
David S. Miller16ce82d2007-04-26 21:08:21 -0700230static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
David S. Miller7c963ad2005-05-31 16:57:59 -0700231{
232 if (likely(ctx)) {
233 __clear_bit(ctx, iommu->ctx_bitmap);
234 if (ctx < iommu->ctx_lowest_free)
235 iommu->ctx_lowest_free = ctx;
236 }
237}
238
David S. Millerad7ad572007-07-27 22:39:14 -0700239static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
240 dma_addr_t *dma_addrp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
David S. Miller16ce82d2007-04-26 21:08:21 -0700242 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700244 unsigned long flags, order, first_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 void *ret;
246 int npages;
247
248 size = IO_PAGE_ALIGN(size);
249 order = get_order(size);
250 if (order >= 10)
251 return NULL;
252
David S. Miller42f14232006-05-23 02:07:22 -0700253 first_page = __get_free_pages(gfp, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (first_page == 0UL)
255 return NULL;
256 memset((char *)first_page, 0, PAGE_SIZE << order);
257
David S. Millerad7ad572007-07-27 22:39:14 -0700258 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller688cb302005-10-13 22:15:24 -0700261 iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
262 spin_unlock_irqrestore(&iommu->lock, flags);
263
264 if (unlikely(iopte == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 free_pages(first_page, order);
266 return NULL;
267 }
268
269 *dma_addrp = (iommu->page_table_map_base +
270 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
271 ret = (void *) first_page;
272 npages = size >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 first_page = __pa(first_page);
274 while (npages--) {
David S. Miller688cb302005-10-13 22:15:24 -0700275 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 IOPTE_WRITE |
277 (first_page & IOPTE_PAGE));
278 iopte++;
279 first_page += IO_PAGE_SIZE;
280 }
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return ret;
283}
284
David S. Millerad7ad572007-07-27 22:39:14 -0700285static void dma_4u_free_coherent(struct device *dev, size_t size,
286 void *cpu, dma_addr_t dvma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
David S. Miller16ce82d2007-04-26 21:08:21 -0700288 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700290 unsigned long flags, order, npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700293 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 iopte = iommu->page_table +
295 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
296
297 spin_lock_irqsave(&iommu->lock, flags);
298
David S. Miller012d64f2006-10-25 22:33:07 -0700299 free_npages(iommu, dvma - iommu->page_table_map_base, npages);
David S. Miller7c963ad2005-05-31 16:57:59 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 spin_unlock_irqrestore(&iommu->lock, flags);
302
303 order = get_order(size);
304 if (order < 10)
305 free_pages((unsigned long)cpu, order);
306}
307
David S. Millerad7ad572007-07-27 22:39:14 -0700308static dma_addr_t dma_4u_map_single(struct device *dev, void *ptr, size_t sz,
309 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
David S. Miller16ce82d2007-04-26 21:08:21 -0700311 struct iommu *iommu;
312 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 iopte_t *base;
314 unsigned long flags, npages, oaddr;
315 unsigned long i, base_paddr, ctx;
316 u32 bus_addr, ret;
317 unsigned long iopte_protection;
318
David S. Millerad7ad572007-07-27 22:39:14 -0700319 iommu = dev->archdata.iommu;
320 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
David S. Millerad7ad572007-07-27 22:39:14 -0700322 if (unlikely(direction == DMA_NONE))
David S. Miller688cb302005-10-13 22:15:24 -0700323 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 oaddr = (unsigned long)ptr;
326 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
327 npages >>= IO_PAGE_SHIFT;
328
329 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller688cb302005-10-13 22:15:24 -0700330 base = alloc_npages(iommu, npages);
331 ctx = 0;
332 if (iommu->iommu_ctxflush)
333 ctx = iommu_alloc_ctx(iommu);
334 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David S. Miller688cb302005-10-13 22:15:24 -0700336 if (unlikely(!base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 bus_addr = (iommu->page_table_map_base +
340 ((base - iommu->page_table) << IO_PAGE_SHIFT));
341 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
342 base_paddr = __pa(oaddr & IO_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (strbuf->strbuf_enabled)
344 iopte_protection = IOPTE_STREAMING(ctx);
345 else
346 iopte_protection = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700347 if (direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 iopte_protection |= IOPTE_WRITE;
349
350 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
351 iopte_val(*base) = iopte_protection | base_paddr;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return ret;
354
355bad:
David S. Miller688cb302005-10-13 22:15:24 -0700356 iommu_free_ctx(iommu, ctx);
357bad_no_ctx:
358 if (printk_ratelimit())
359 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700360 return DMA_ERROR_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
David S. Millerad7ad572007-07-27 22:39:14 -0700363static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
364 u32 vaddr, unsigned long ctx, unsigned long npages,
365 enum dma_data_direction direction)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700366{
367 int limit;
368
David S. Miller4dbc30f2005-05-11 11:37:00 -0700369 if (strbuf->strbuf_ctxflush &&
370 iommu->iommu_ctxflush) {
371 unsigned long matchreg, flushreg;
David S. Miller7c963ad2005-05-31 16:57:59 -0700372 u64 val;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700373
374 flushreg = strbuf->strbuf_ctxflush;
David S. Millerad7ad572007-07-27 22:39:14 -0700375 matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700376
David S. Millerad7ad572007-07-27 22:39:14 -0700377 iommu_write(flushreg, ctx);
378 val = iommu_read(matchreg);
David S. Miller88314ee2005-05-31 19:13:52 -0700379 val &= 0xffff;
380 if (!val)
David S. Miller7c963ad2005-05-31 16:57:59 -0700381 goto do_flush_sync;
382
David S. Miller7c963ad2005-05-31 16:57:59 -0700383 while (val) {
384 if (val & 0x1)
David S. Millerad7ad572007-07-27 22:39:14 -0700385 iommu_write(flushreg, ctx);
David S. Miller7c963ad2005-05-31 16:57:59 -0700386 val >>= 1;
David S. Millera228dfd2005-05-20 11:40:32 -0700387 }
David S. Millerad7ad572007-07-27 22:39:14 -0700388 val = iommu_read(matchreg);
David S. Miller7c963ad2005-05-31 16:57:59 -0700389 if (unlikely(val)) {
David S. Millerad7ad572007-07-27 22:39:14 -0700390 printk(KERN_WARNING "strbuf_flush: ctx flush "
David S. Miller7c963ad2005-05-31 16:57:59 -0700391 "timeout matchreg[%lx] ctx[%lx]\n",
392 val, ctx);
393 goto do_page_flush;
394 }
David S. Miller4dbc30f2005-05-11 11:37:00 -0700395 } else {
396 unsigned long i;
397
David S. Miller7c963ad2005-05-31 16:57:59 -0700398 do_page_flush:
David S. Miller4dbc30f2005-05-11 11:37:00 -0700399 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
David S. Millerad7ad572007-07-27 22:39:14 -0700400 iommu_write(strbuf->strbuf_pflush, vaddr);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700401 }
402
David S. Miller7c963ad2005-05-31 16:57:59 -0700403do_flush_sync:
404 /* If the device could not have possibly put dirty data into
405 * the streaming cache, no flush-flag synchronization needs
406 * to be performed.
407 */
David S. Millerad7ad572007-07-27 22:39:14 -0700408 if (direction == DMA_TO_DEVICE)
David S. Miller7c963ad2005-05-31 16:57:59 -0700409 return;
410
David S. Millerad7ad572007-07-27 22:39:14 -0700411 STC_FLUSHFLAG_INIT(strbuf);
412 iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
413 (void) iommu_read(iommu->write_complete_reg);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700414
David S. Millera228dfd2005-05-20 11:40:32 -0700415 limit = 100000;
David S. Millerad7ad572007-07-27 22:39:14 -0700416 while (!STC_FLUSHFLAG_SET(strbuf)) {
David S. Miller4dbc30f2005-05-11 11:37:00 -0700417 limit--;
418 if (!limit)
419 break;
David S. Millera228dfd2005-05-20 11:40:32 -0700420 udelay(1);
David S. Miller4f071182005-08-29 12:46:22 -0700421 rmb();
David S. Miller4dbc30f2005-05-11 11:37:00 -0700422 }
423 if (!limit)
David S. Millerad7ad572007-07-27 22:39:14 -0700424 printk(KERN_WARNING "strbuf_flush: flushflag timeout "
David S. Miller4dbc30f2005-05-11 11:37:00 -0700425 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
426 vaddr, ctx, npages);
427}
428
David S. Millerad7ad572007-07-27 22:39:14 -0700429static void dma_4u_unmap_single(struct device *dev, dma_addr_t bus_addr,
430 size_t sz, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
David S. Miller16ce82d2007-04-26 21:08:21 -0700432 struct iommu *iommu;
433 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 iopte_t *base;
David S. Miller688cb302005-10-13 22:15:24 -0700435 unsigned long flags, npages, ctx, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
David S. Millerad7ad572007-07-27 22:39:14 -0700437 if (unlikely(direction == DMA_NONE)) {
David S. Miller688cb302005-10-13 22:15:24 -0700438 if (printk_ratelimit())
439 WARN_ON(1);
440 return;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David S. Millerad7ad572007-07-27 22:39:14 -0700443 iommu = dev->archdata.iommu;
444 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
447 npages >>= IO_PAGE_SHIFT;
448 base = iommu->page_table +
449 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 bus_addr &= IO_PAGE_MASK;
451
452 spin_lock_irqsave(&iommu->lock, flags);
453
454 /* Record the context, if any. */
455 ctx = 0;
456 if (iommu->iommu_ctxflush)
457 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
458
459 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700460 if (strbuf->strbuf_enabled)
David S. Millerad7ad572007-07-27 22:39:14 -0700461 strbuf_flush(strbuf, iommu, bus_addr, ctx,
462 npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
David S. Miller688cb302005-10-13 22:15:24 -0700464 /* Step 2: Clear out TSB entries. */
465 for (i = 0; i < npages; i++)
466 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
David S. Miller688cb302005-10-13 22:15:24 -0700468 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
David S. Miller7c963ad2005-05-31 16:57:59 -0700470 iommu_free_ctx(iommu, ctx);
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 spin_unlock_irqrestore(&iommu->lock, flags);
473}
474
Jens Axboe58b053e2007-10-22 20:02:46 +0200475#define SG_ENT_PHYS_ADDRESS(SG) (__pa(sg_virt((SG))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
David S. Miller58045092007-10-17 04:08:48 -0700477static void fill_sg(iopte_t *iopte, struct scatterlist *sg,
478 int nused, int nelems,
479 unsigned long iopte_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct scatterlist *dma_sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int i;
483
484 for (i = 0; i < nused; i++) {
485 unsigned long pteval = ~0UL;
486 u32 dma_npages;
487
488 dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
489 dma_sg->dma_length +
490 ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
491 do {
492 unsigned long offset;
493 signed int len;
494
495 /* If we are here, we know we have at least one
496 * more page to map. So walk forward until we
497 * hit a page crossing, and begin creating new
498 * mappings from that spot.
499 */
500 for (;;) {
501 unsigned long tmp;
502
503 tmp = SG_ENT_PHYS_ADDRESS(sg);
504 len = sg->length;
505 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
506 pteval = tmp & IO_PAGE_MASK;
507 offset = tmp & (IO_PAGE_SIZE - 1UL);
508 break;
509 }
510 if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
511 pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
512 offset = 0UL;
513 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
514 break;
515 }
Jens Axboe2c941a22007-08-07 09:37:10 +0200516 sg = sg_next(sg);
David S. Miller58045092007-10-17 04:08:48 -0700517 nelems--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 pteval = iopte_protection | (pteval & IOPTE_PAGE);
521 while (len > 0) {
522 *iopte++ = __iopte(pteval);
523 pteval += IO_PAGE_SIZE;
524 len -= (IO_PAGE_SIZE - offset);
525 offset = 0;
526 dma_npages--;
527 }
528
529 pteval = (pteval & IOPTE_PAGE) + len;
Jens Axboe2c941a22007-08-07 09:37:10 +0200530 sg = sg_next(sg);
David S. Miller58045092007-10-17 04:08:48 -0700531 nelems--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* Skip over any tail mappings we've fully mapped,
534 * adjusting pteval along the way. Stop when we
535 * detect a page crossing event.
536 */
David S. Miller58045092007-10-17 04:08:48 -0700537 while (nelems &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
539 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
540 ((pteval ^
541 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
542 pteval += sg->length;
Jens Axboe2c941a22007-08-07 09:37:10 +0200543 sg = sg_next(sg);
David S. Miller58045092007-10-17 04:08:48 -0700544 nelems--;
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 =
Jens Axboe58b053e2007-10-22 20:02:46 +0200567 dma_4u_map_single(dev, sg_virt(sglist),
David S. Miller18397942006-02-10 00:08:26 -0800568 sglist->length, direction);
David S. Millerad7ad572007-07-27 22:39:14 -0700569 if (unlikely(sglist->dma_address == DMA_ERROR_CODE))
David S. Miller688cb302005-10-13 22:15:24 -0700570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 sglist->dma_length = sglist->length;
572 return 1;
573 }
574
David S. Millerad7ad572007-07-27 22:39:14 -0700575 iommu = dev->archdata.iommu;
576 strbuf = dev->archdata.stc;
577
578 if (unlikely(direction == DMA_NONE))
David S. Miller688cb302005-10-13 22:15:24 -0700579 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* Step 1: Prepare scatter list. */
582
583 npages = prepare_sg(sglist, nelems);
584
David S. Miller688cb302005-10-13 22:15:24 -0700585 /* Step 2: Allocate a cluster and context, if necessary. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 spin_lock_irqsave(&iommu->lock, flags);
588
David S. Miller688cb302005-10-13 22:15:24 -0700589 base = alloc_npages(iommu, npages);
590 ctx = 0;
591 if (iommu->iommu_ctxflush)
592 ctx = iommu_alloc_ctx(iommu);
593
594 spin_unlock_irqrestore(&iommu->lock, flags);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (base == NULL)
597 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700598
599 dma_base = iommu->page_table_map_base +
600 ((base - iommu->page_table) << IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Step 3: Normalize DMA addresses. */
603 used = nelems;
604
605 sgtmp = sglist;
606 while (used && sgtmp->dma_length) {
607 sgtmp->dma_address += dma_base;
Jens Axboe2c941a22007-08-07 09:37:10 +0200608 sgtmp = sg_next(sgtmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 used--;
610 }
611 used = nelems - used;
612
David S. Miller688cb302005-10-13 22:15:24 -0700613 /* Step 4: Create the mappings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (strbuf->strbuf_enabled)
615 iopte_protection = IOPTE_STREAMING(ctx);
616 else
617 iopte_protection = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700618 if (direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 iopte_protection |= IOPTE_WRITE;
David S. Miller688cb302005-10-13 22:15:24 -0700620
621 fill_sg(base, sglist, used, nelems, iopte_protection);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623#ifdef VERIFY_SG
624 verify_sglist(sglist, nelems, base, npages);
625#endif
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return used;
628
629bad:
David S. Miller688cb302005-10-13 22:15:24 -0700630 iommu_free_ctx(iommu, ctx);
631bad_no_ctx:
632 if (printk_ratelimit())
633 WARN_ON(1);
634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
David S. Millerad7ad572007-07-27 22:39:14 -0700637static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
638 int nelems, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
David S. Miller16ce82d2007-04-26 21:08:21 -0700640 struct iommu *iommu;
641 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 iopte_t *base;
643 unsigned long flags, ctx, i, npages;
Jens Axboe2c941a22007-08-07 09:37:10 +0200644 struct scatterlist *sg, *sgprv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 u32 bus_addr;
646
David S. Millerad7ad572007-07-27 22:39:14 -0700647 if (unlikely(direction == DMA_NONE)) {
David S. Miller688cb302005-10-13 22:15:24 -0700648 if (printk_ratelimit())
649 WARN_ON(1);
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
David S. Millerad7ad572007-07-27 22:39:14 -0700652 iommu = dev->archdata.iommu;
653 strbuf = dev->archdata.stc;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 bus_addr = sglist->dma_address & IO_PAGE_MASK;
656
Jens Axboe2c941a22007-08-07 09:37:10 +0200657 sgprv = NULL;
658 for_each_sg(sglist, sg, nelems, i) {
659 if (sg->dma_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
Jens Axboe2c941a22007-08-07 09:37:10 +0200661 sgprv = sg;
662 }
663
664 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length) -
David S. Miller688cb302005-10-13 22:15:24 -0700665 bus_addr) >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 base = iommu->page_table +
668 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 spin_lock_irqsave(&iommu->lock, flags);
671
672 /* Record the context, if any. */
673 ctx = 0;
674 if (iommu->iommu_ctxflush)
675 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
676
677 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700678 if (strbuf->strbuf_enabled)
David S. Millerad7ad572007-07-27 22:39:14 -0700679 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
David S. Miller688cb302005-10-13 22:15:24 -0700681 /* Step 2: Clear out the TSB entries. */
682 for (i = 0; i < npages; i++)
683 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
David S. Miller688cb302005-10-13 22:15:24 -0700685 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
David S. Miller7c963ad2005-05-31 16:57:59 -0700687 iommu_free_ctx(iommu, ctx);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 spin_unlock_irqrestore(&iommu->lock, flags);
690}
691
David S. Millerad7ad572007-07-27 22:39:14 -0700692static void dma_4u_sync_single_for_cpu(struct device *dev,
693 dma_addr_t bus_addr, size_t sz,
694 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
David S. Miller16ce82d2007-04-26 21:08:21 -0700696 struct iommu *iommu;
697 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 unsigned long flags, ctx, npages;
699
David S. Millerad7ad572007-07-27 22:39:14 -0700700 iommu = dev->archdata.iommu;
701 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (!strbuf->strbuf_enabled)
704 return;
705
706 spin_lock_irqsave(&iommu->lock, flags);
707
708 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
709 npages >>= IO_PAGE_SHIFT;
710 bus_addr &= IO_PAGE_MASK;
711
712 /* Step 1: Record the context, if any. */
713 ctx = 0;
714 if (iommu->iommu_ctxflush &&
715 strbuf->strbuf_ctxflush) {
716 iopte_t *iopte;
717
718 iopte = iommu->page_table +
719 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
720 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
721 }
722
723 /* Step 2: Kick data out of streaming buffers. */
David S. Millerad7ad572007-07-27 22:39:14 -0700724 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 spin_unlock_irqrestore(&iommu->lock, flags);
727}
728
David S. Millerad7ad572007-07-27 22:39:14 -0700729static void dma_4u_sync_sg_for_cpu(struct device *dev,
730 struct scatterlist *sglist, int nelems,
731 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
David S. Miller16ce82d2007-04-26 21:08:21 -0700733 struct iommu *iommu;
734 struct strbuf *strbuf;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700735 unsigned long flags, ctx, npages, i;
Jens Axboe2c941a22007-08-07 09:37:10 +0200736 struct scatterlist *sg, *sgprv;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700737 u32 bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
David S. Millerad7ad572007-07-27 22:39:14 -0700739 iommu = dev->archdata.iommu;
740 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (!strbuf->strbuf_enabled)
743 return;
744
745 spin_lock_irqsave(&iommu->lock, flags);
746
747 /* Step 1: Record the context, if any. */
748 ctx = 0;
749 if (iommu->iommu_ctxflush &&
750 strbuf->strbuf_ctxflush) {
751 iopte_t *iopte;
752
753 iopte = iommu->page_table +
754 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
755 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
756 }
757
758 /* Step 2: Kick data out of streaming buffers. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700759 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
Jens Axboe2c941a22007-08-07 09:37:10 +0200760 sgprv = NULL;
761 for_each_sg(sglist, sg, nelems, i) {
762 if (sg->dma_length == 0)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700763 break;
Jens Axboe2c941a22007-08-07 09:37:10 +0200764 sgprv = sg;
765 }
766
767 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700768 - bus_addr) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700769 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 spin_unlock_irqrestore(&iommu->lock, flags);
772}
773
David S. Millerad7ad572007-07-27 22:39:14 -0700774const struct dma_ops sun4u_dma_ops = {
775 .alloc_coherent = dma_4u_alloc_coherent,
776 .free_coherent = dma_4u_free_coherent,
777 .map_single = dma_4u_map_single,
778 .unmap_single = dma_4u_unmap_single,
779 .map_sg = dma_4u_map_sg,
780 .unmap_sg = dma_4u_unmap_sg,
781 .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
782 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800783};
784
David S. Millerad7ad572007-07-27 22:39:14 -0700785const struct dma_ops *dma_ops = &sun4u_dma_ops;
786EXPORT_SYMBOL(dma_ops);
787
788int dma_supported(struct device *dev, u64 device_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
David S. Millerad7ad572007-07-27 22:39:14 -0700790 struct iommu *iommu = dev->archdata.iommu;
791 u64 dma_addr_mask = iommu->dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (device_mask >= (1UL << 32UL))
794 return 0;
795
David S. Millerad7ad572007-07-27 22:39:14 -0700796 if ((device_mask & dma_addr_mask) == dma_addr_mask)
797 return 1;
798
799#ifdef CONFIG_PCI
800 if (dev->bus == &pci_bus_type)
801 return pci_dma_supported(to_pci_dev(dev), device_mask);
802#endif
803
804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
David S. Millerad7ad572007-07-27 22:39:14 -0700806EXPORT_SYMBOL(dma_supported);
807
808int dma_set_mask(struct device *dev, u64 dma_mask)
809{
810#ifdef CONFIG_PCI
811 if (dev->bus == &pci_bus_type)
812 return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
813#endif
814 return -EINVAL;
815}
816EXPORT_SYMBOL(dma_set_mask);