blob: 0e347ff812a336da425cba8b10a411fd6425ad1a [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
137 n = iommu_area_alloc(arena->map, limit, start, npages, 0,
138 boundary_size >> IO_PAGE_SHIFT, 0);
139 if (n == -1) {
David S. Miller688cb302005-10-13 22:15:24 -0700140 if (likely(pass < 1)) {
David S. Millerd2841422008-02-08 18:05:46 -0800141 /* First failure, rescan from the beginning. */
David S. Miller688cb302005-10-13 22:15:24 -0700142 start = 0;
David S. Millerd2841422008-02-08 18:05:46 -0800143 if (iommu->flush_all)
144 iommu->flush_all(iommu);
David S. Miller688cb302005-10-13 22:15:24 -0700145 pass++;
146 goto again;
147 } else {
David S. Millerd2841422008-02-08 18:05:46 -0800148 /* Second failure, give up */
149 return DMA_ERROR_CODE;
David S. Miller688cb302005-10-13 22:15:24 -0700150 }
151 }
152
David S. Millerd2841422008-02-08 18:05:46 -0800153 end = n + npages;
David S. Miller688cb302005-10-13 22:15:24 -0700154
155 arena->hint = end;
156
David S. Millerd2841422008-02-08 18:05:46 -0800157 /* Update handle for SG allocations */
158 if (handle)
159 *handle = end;
160
David S. Miller688cb302005-10-13 22:15:24 -0700161 return n;
162}
163
David S. Millerd2841422008-02-08 18:05:46 -0800164void iommu_range_free(struct iommu *iommu, dma_addr_t dma_addr, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -0700165{
David S. Millerd2841422008-02-08 18:05:46 -0800166 struct iommu_arena *arena = &iommu->arena;
167 unsigned long entry;
David S. Miller688cb302005-10-13 22:15:24 -0700168
David S. Millerd2841422008-02-08 18:05:46 -0800169 entry = (dma_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
170
171 iommu_area_free(arena->map, entry, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700172}
173
David S. Millerad7ad572007-07-27 22:39:14 -0700174int iommu_table_init(struct iommu *iommu, int tsbsize,
175 u32 dma_offset, u32 dma_addr_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
David S. Miller688cb302005-10-13 22:15:24 -0700177 unsigned long i, tsbbase, order, sz, num_tsb_entries;
178
179 num_tsb_entries = tsbsize / sizeof(iopte_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
David S. Miller51e85132005-10-13 21:10:08 -0700181 /* Setup initial software IOMMU state. */
182 spin_lock_init(&iommu->lock);
183 iommu->ctx_lowest_free = 1;
184 iommu->page_table_map_base = dma_offset;
185 iommu->dma_addr_mask = dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
David S. Miller688cb302005-10-13 22:15:24 -0700187 /* Allocate and initialize the free area map. */
188 sz = num_tsb_entries / 8;
189 sz = (sz + 7UL) & ~7UL;
Eric Sesterhenn91329832006-03-06 13:48:40 -0800190 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
David S. Miller688cb302005-10-13 22:15:24 -0700191 if (!iommu->arena.map) {
David S. Millerad7ad572007-07-27 22:39:14 -0700192 printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
193 return -ENOMEM;
David S. Miller51e85132005-10-13 21:10:08 -0700194 }
David S. Miller688cb302005-10-13 22:15:24 -0700195 iommu->arena.limit = num_tsb_entries;
David S. Miller51e85132005-10-13 21:10:08 -0700196
David S. Millerd2841422008-02-08 18:05:46 -0800197 if (tlb_type != hypervisor)
198 iommu->flush_all = iommu_flushall;
199
David S. Miller51e85132005-10-13 21:10:08 -0700200 /* Allocate and initialize the dummy page which we
201 * set inactive IO PTEs to point to.
202 */
Robert P. J. Dayb83ebf52008-02-12 22:09:29 -0800203 iommu->dummy_page = get_zeroed_page(GFP_KERNEL);
David S. Miller51e85132005-10-13 21:10:08 -0700204 if (!iommu->dummy_page) {
David S. Millerad7ad572007-07-27 22:39:14 -0700205 printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
206 goto out_free_map;
David S. Miller51e85132005-10-13 21:10:08 -0700207 }
David S. Miller51e85132005-10-13 21:10:08 -0700208 iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
209
210 /* Now allocate and setup the IOMMU page table itself. */
211 order = get_order(tsbsize);
212 tsbbase = __get_free_pages(GFP_KERNEL, order);
213 if (!tsbbase) {
David S. Millerad7ad572007-07-27 22:39:14 -0700214 printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
215 goto out_free_dummy_page;
David S. Miller51e85132005-10-13 21:10:08 -0700216 }
217 iommu->page_table = (iopte_t *)tsbbase;
218
David S. Miller688cb302005-10-13 22:15:24 -0700219 for (i = 0; i < num_tsb_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 iopte_make_dummy(iommu, &iommu->page_table[i]);
David S. Millerad7ad572007-07-27 22:39:14 -0700221
222 return 0;
223
224out_free_dummy_page:
225 free_page(iommu->dummy_page);
226 iommu->dummy_page = 0UL;
227
228out_free_map:
229 kfree(iommu->arena.map);
230 iommu->arena.map = NULL;
231
232 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
David S. Millerd2841422008-02-08 18:05:46 -0800235static inline iopte_t *alloc_npages(struct device *dev, struct iommu *iommu,
236 unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
David S. Millerd2841422008-02-08 18:05:46 -0800238 unsigned long entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
David S. Millerd2841422008-02-08 18:05:46 -0800240 entry = iommu_range_alloc(dev, iommu, npages, NULL);
241 if (unlikely(entry == DMA_ERROR_CODE))
David S. Miller688cb302005-10-13 22:15:24 -0700242 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
David S. Miller688cb302005-10-13 22:15:24 -0700244 return iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
David S. Miller16ce82d2007-04-26 21:08:21 -0700247static int iommu_alloc_ctx(struct iommu *iommu)
David S. Miller7c963ad2005-05-31 16:57:59 -0700248{
249 int lowest = iommu->ctx_lowest_free;
250 int sz = IOMMU_NUM_CTXS - lowest;
251 int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
252
253 if (unlikely(n == sz)) {
254 n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
255 if (unlikely(n == lowest)) {
256 printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
257 n = 0;
258 }
259 }
260 if (n)
261 __set_bit(n, iommu->ctx_bitmap);
262
263 return n;
264}
265
David S. Miller16ce82d2007-04-26 21:08:21 -0700266static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
David S. Miller7c963ad2005-05-31 16:57:59 -0700267{
268 if (likely(ctx)) {
269 __clear_bit(ctx, iommu->ctx_bitmap);
270 if (ctx < iommu->ctx_lowest_free)
271 iommu->ctx_lowest_free = ctx;
272 }
273}
274
David S. Millerad7ad572007-07-27 22:39:14 -0700275static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
276 dma_addr_t *dma_addrp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
David S. Miller16ce82d2007-04-26 21:08:21 -0700278 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700280 unsigned long flags, order, first_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 void *ret;
282 int npages;
283
284 size = IO_PAGE_ALIGN(size);
285 order = get_order(size);
286 if (order >= 10)
287 return NULL;
288
David S. Miller42f14232006-05-23 02:07:22 -0700289 first_page = __get_free_pages(gfp, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (first_page == 0UL)
291 return NULL;
292 memset((char *)first_page, 0, PAGE_SIZE << order);
293
David S. Millerad7ad572007-07-27 22:39:14 -0700294 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800297 iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT);
David S. Miller688cb302005-10-13 22:15:24 -0700298 spin_unlock_irqrestore(&iommu->lock, flags);
299
300 if (unlikely(iopte == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 free_pages(first_page, order);
302 return NULL;
303 }
304
305 *dma_addrp = (iommu->page_table_map_base +
306 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
307 ret = (void *) first_page;
308 npages = size >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 first_page = __pa(first_page);
310 while (npages--) {
David S. Miller688cb302005-10-13 22:15:24 -0700311 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 IOPTE_WRITE |
313 (first_page & IOPTE_PAGE));
314 iopte++;
315 first_page += IO_PAGE_SIZE;
316 }
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return ret;
319}
320
David S. Millerad7ad572007-07-27 22:39:14 -0700321static void dma_4u_free_coherent(struct device *dev, size_t size,
322 void *cpu, dma_addr_t dvma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
David S. Miller16ce82d2007-04-26 21:08:21 -0700324 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700326 unsigned long flags, order, npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700329 iommu = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 iopte = iommu->page_table +
331 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
332
333 spin_lock_irqsave(&iommu->lock, flags);
334
David S. Millerd2841422008-02-08 18:05:46 -0800335 iommu_range_free(iommu, dvma, npages);
David S. Miller7c963ad2005-05-31 16:57:59 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 spin_unlock_irqrestore(&iommu->lock, flags);
338
339 order = get_order(size);
340 if (order < 10)
341 free_pages((unsigned long)cpu, order);
342}
343
David S. Millerad7ad572007-07-27 22:39:14 -0700344static dma_addr_t dma_4u_map_single(struct device *dev, void *ptr, size_t sz,
345 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
David S. Miller16ce82d2007-04-26 21:08:21 -0700347 struct iommu *iommu;
348 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 iopte_t *base;
350 unsigned long flags, npages, oaddr;
351 unsigned long i, base_paddr, ctx;
352 u32 bus_addr, ret;
353 unsigned long iopte_protection;
354
David S. Millerad7ad572007-07-27 22:39:14 -0700355 iommu = dev->archdata.iommu;
356 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David S. Millerad7ad572007-07-27 22:39:14 -0700358 if (unlikely(direction == DMA_NONE))
David S. Miller688cb302005-10-13 22:15:24 -0700359 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 oaddr = (unsigned long)ptr;
362 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
363 npages >>= IO_PAGE_SHIFT;
364
365 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800366 base = alloc_npages(dev, iommu, npages);
David S. Miller688cb302005-10-13 22:15:24 -0700367 ctx = 0;
368 if (iommu->iommu_ctxflush)
369 ctx = iommu_alloc_ctx(iommu);
370 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
David S. Miller688cb302005-10-13 22:15:24 -0700372 if (unlikely(!base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 bus_addr = (iommu->page_table_map_base +
376 ((base - iommu->page_table) << IO_PAGE_SHIFT));
377 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
378 base_paddr = __pa(oaddr & IO_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (strbuf->strbuf_enabled)
380 iopte_protection = IOPTE_STREAMING(ctx);
381 else
382 iopte_protection = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700383 if (direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 iopte_protection |= IOPTE_WRITE;
385
386 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
387 iopte_val(*base) = iopte_protection | base_paddr;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return ret;
390
391bad:
David S. Miller688cb302005-10-13 22:15:24 -0700392 iommu_free_ctx(iommu, ctx);
393bad_no_ctx:
394 if (printk_ratelimit())
395 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700396 return DMA_ERROR_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
David S. Millerad7ad572007-07-27 22:39:14 -0700399static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
400 u32 vaddr, unsigned long ctx, unsigned long npages,
401 enum dma_data_direction direction)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700402{
403 int limit;
404
David S. Miller4dbc30f2005-05-11 11:37:00 -0700405 if (strbuf->strbuf_ctxflush &&
406 iommu->iommu_ctxflush) {
407 unsigned long matchreg, flushreg;
David S. Miller7c963ad2005-05-31 16:57:59 -0700408 u64 val;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700409
410 flushreg = strbuf->strbuf_ctxflush;
David S. Millerad7ad572007-07-27 22:39:14 -0700411 matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700412
David S. Millerad7ad572007-07-27 22:39:14 -0700413 iommu_write(flushreg, ctx);
414 val = iommu_read(matchreg);
David S. Miller88314ee2005-05-31 19:13:52 -0700415 val &= 0xffff;
416 if (!val)
David S. Miller7c963ad2005-05-31 16:57:59 -0700417 goto do_flush_sync;
418
David S. Miller7c963ad2005-05-31 16:57:59 -0700419 while (val) {
420 if (val & 0x1)
David S. Millerad7ad572007-07-27 22:39:14 -0700421 iommu_write(flushreg, ctx);
David S. Miller7c963ad2005-05-31 16:57:59 -0700422 val >>= 1;
David S. Millera228dfd2005-05-20 11:40:32 -0700423 }
David S. Millerad7ad572007-07-27 22:39:14 -0700424 val = iommu_read(matchreg);
David S. Miller7c963ad2005-05-31 16:57:59 -0700425 if (unlikely(val)) {
David S. Millerad7ad572007-07-27 22:39:14 -0700426 printk(KERN_WARNING "strbuf_flush: ctx flush "
David S. Miller7c963ad2005-05-31 16:57:59 -0700427 "timeout matchreg[%lx] ctx[%lx]\n",
428 val, ctx);
429 goto do_page_flush;
430 }
David S. Miller4dbc30f2005-05-11 11:37:00 -0700431 } else {
432 unsigned long i;
433
David S. Miller7c963ad2005-05-31 16:57:59 -0700434 do_page_flush:
David S. Miller4dbc30f2005-05-11 11:37:00 -0700435 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
David S. Millerad7ad572007-07-27 22:39:14 -0700436 iommu_write(strbuf->strbuf_pflush, vaddr);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700437 }
438
David S. Miller7c963ad2005-05-31 16:57:59 -0700439do_flush_sync:
440 /* If the device could not have possibly put dirty data into
441 * the streaming cache, no flush-flag synchronization needs
442 * to be performed.
443 */
David S. Millerad7ad572007-07-27 22:39:14 -0700444 if (direction == DMA_TO_DEVICE)
David S. Miller7c963ad2005-05-31 16:57:59 -0700445 return;
446
David S. Millerad7ad572007-07-27 22:39:14 -0700447 STC_FLUSHFLAG_INIT(strbuf);
448 iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
449 (void) iommu_read(iommu->write_complete_reg);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700450
David S. Millera228dfd2005-05-20 11:40:32 -0700451 limit = 100000;
David S. Millerad7ad572007-07-27 22:39:14 -0700452 while (!STC_FLUSHFLAG_SET(strbuf)) {
David S. Miller4dbc30f2005-05-11 11:37:00 -0700453 limit--;
454 if (!limit)
455 break;
David S. Millera228dfd2005-05-20 11:40:32 -0700456 udelay(1);
David S. Miller4f071182005-08-29 12:46:22 -0700457 rmb();
David S. Miller4dbc30f2005-05-11 11:37:00 -0700458 }
459 if (!limit)
David S. Millerad7ad572007-07-27 22:39:14 -0700460 printk(KERN_WARNING "strbuf_flush: flushflag timeout "
David S. Miller4dbc30f2005-05-11 11:37:00 -0700461 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
462 vaddr, ctx, npages);
463}
464
David S. Millerad7ad572007-07-27 22:39:14 -0700465static void dma_4u_unmap_single(struct device *dev, dma_addr_t bus_addr,
466 size_t sz, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
David S. Miller16ce82d2007-04-26 21:08:21 -0700468 struct iommu *iommu;
469 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 iopte_t *base;
David S. Miller688cb302005-10-13 22:15:24 -0700471 unsigned long flags, npages, ctx, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
David S. Millerad7ad572007-07-27 22:39:14 -0700473 if (unlikely(direction == DMA_NONE)) {
David S. Miller688cb302005-10-13 22:15:24 -0700474 if (printk_ratelimit())
475 WARN_ON(1);
476 return;
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
David S. Millerad7ad572007-07-27 22:39:14 -0700479 iommu = dev->archdata.iommu;
480 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
483 npages >>= IO_PAGE_SHIFT;
484 base = iommu->page_table +
485 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 bus_addr &= IO_PAGE_MASK;
487
488 spin_lock_irqsave(&iommu->lock, flags);
489
490 /* Record the context, if any. */
491 ctx = 0;
492 if (iommu->iommu_ctxflush)
493 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
494
495 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700496 if (strbuf->strbuf_enabled)
David S. Millerad7ad572007-07-27 22:39:14 -0700497 strbuf_flush(strbuf, iommu, bus_addr, ctx,
498 npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
David S. Miller688cb302005-10-13 22:15:24 -0700500 /* Step 2: Clear out TSB entries. */
501 for (i = 0; i < npages; i++)
502 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
David S. Millerd2841422008-02-08 18:05:46 -0800504 iommu_range_free(iommu, bus_addr, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David S. Miller7c963ad2005-05-31 16:57:59 -0700506 iommu_free_ctx(iommu, ctx);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 spin_unlock_irqrestore(&iommu->lock, flags);
509}
510
David S. Millerad7ad572007-07-27 22:39:14 -0700511static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
512 int nelems, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
David S. Miller13fa14e2008-02-09 03:11:01 -0800514 struct scatterlist *s, *outs, *segstart;
515 unsigned long flags, handle, prot, ctx;
516 dma_addr_t dma_next = 0, dma_addr;
517 unsigned int max_seg_size;
518 int outcount, incount, i;
David S. Miller16ce82d2007-04-26 21:08:21 -0700519 struct strbuf *strbuf;
David S. Miller38192d52008-02-06 03:50:26 -0800520 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
David S. Miller13fa14e2008-02-09 03:11:01 -0800522 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David S. Millerad7ad572007-07-27 22:39:14 -0700524 iommu = dev->archdata.iommu;
525 strbuf = dev->archdata.stc;
David S. Miller13fa14e2008-02-09 03:11:01 -0800526 if (nelems == 0 || !iommu)
527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 spin_lock_irqsave(&iommu->lock, flags);
530
David S. Miller688cb302005-10-13 22:15:24 -0700531 ctx = 0;
532 if (iommu->iommu_ctxflush)
533 ctx = iommu_alloc_ctx(iommu);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (strbuf->strbuf_enabled)
David S. Miller13fa14e2008-02-09 03:11:01 -0800536 prot = IOPTE_STREAMING(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 else
David S. Miller13fa14e2008-02-09 03:11:01 -0800538 prot = IOPTE_CONSISTENT(ctx);
David S. Millerad7ad572007-07-27 22:39:14 -0700539 if (direction != DMA_TO_DEVICE)
David S. Miller13fa14e2008-02-09 03:11:01 -0800540 prot |= IOPTE_WRITE;
David S. Miller688cb302005-10-13 22:15:24 -0700541
David S. Miller13fa14e2008-02-09 03:11:01 -0800542 outs = s = segstart = &sglist[0];
543 outcount = 1;
544 incount = nelems;
545 handle = 0;
David S. Miller688cb302005-10-13 22:15:24 -0700546
David S. Miller13fa14e2008-02-09 03:11:01 -0800547 /* Init first segment length for backout at failure */
548 outs->dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
David S. Miller13fa14e2008-02-09 03:11:01 -0800550 max_seg_size = dma_get_max_seg_size(dev);
551 for_each_sg(sglist, s, nelems, i) {
552 unsigned long paddr, npages, entry, slen;
553 iopte_t *base;
David S. Miller38192d52008-02-06 03:50:26 -0800554
David S. Miller13fa14e2008-02-09 03:11:01 -0800555 slen = s->length;
556 /* Sanity check */
557 if (slen == 0) {
558 dma_next = 0;
559 continue;
560 }
561 /* Allocate iommu entries for that segment */
562 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
563 npages = iommu_num_pages(paddr, slen);
564 entry = iommu_range_alloc(dev, iommu, npages, &handle);
565
566 /* Handle failure */
567 if (unlikely(entry == DMA_ERROR_CODE)) {
568 if (printk_ratelimit())
569 printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
570 " npages %lx\n", iommu, paddr, npages);
571 goto iommu_map_failed;
572 }
573
574 base = iommu->page_table + entry;
575
576 /* Convert entry to a dma_addr_t */
577 dma_addr = iommu->page_table_map_base +
578 (entry << IO_PAGE_SHIFT);
579 dma_addr |= (s->offset & ~IO_PAGE_MASK);
580
581 /* Insert into HW table */
David S. Miller38192d52008-02-06 03:50:26 -0800582 paddr &= IO_PAGE_MASK;
David S. Miller13fa14e2008-02-09 03:11:01 -0800583 while (npages--) {
584 iopte_val(*base) = prot | paddr;
David S. Miller38192d52008-02-06 03:50:26 -0800585 base++;
586 paddr += IO_PAGE_SIZE;
David S. Miller38192d52008-02-06 03:50:26 -0800587 }
David S. Miller13fa14e2008-02-09 03:11:01 -0800588
589 /* If we are in an open segment, try merging */
590 if (segstart != s) {
591 /* We cannot merge if:
592 * - allocated dma_addr isn't contiguous to previous allocation
593 */
594 if ((dma_addr != dma_next) ||
595 (outs->dma_length + s->length > max_seg_size)) {
596 /* Can't merge: create a new segment */
597 segstart = s;
598 outcount++;
599 outs = sg_next(outs);
600 } else {
601 outs->dma_length += s->length;
602 }
603 }
604
605 if (segstart == s) {
606 /* This is a new segment, fill entries */
607 outs->dma_address = dma_addr;
608 outs->dma_length = slen;
609 }
610
611 /* Calculate next page pointer for contiguous check */
612 dma_next = dma_addr + slen;
David S. Miller38192d52008-02-06 03:50:26 -0800613 }
614
David S. Miller13fa14e2008-02-09 03:11:01 -0800615 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
David S. Miller13fa14e2008-02-09 03:11:01 -0800617 if (outcount < incount) {
618 outs = sg_next(outs);
619 outs->dma_address = DMA_ERROR_CODE;
620 outs->dma_length = 0;
621 }
622
623 return outcount;
624
625iommu_map_failed:
626 for_each_sg(sglist, s, nelems, i) {
627 if (s->dma_length != 0) {
628 unsigned long vaddr, npages, entry, i;
629 iopte_t *base;
630
631 vaddr = s->dma_address & IO_PAGE_MASK;
632 npages = iommu_num_pages(s->dma_address, s->dma_length);
633 iommu_range_free(iommu, vaddr, npages);
634
635 entry = (vaddr - iommu->page_table_map_base)
636 >> IO_PAGE_SHIFT;
637 base = iommu->page_table + entry;
638
639 for (i = 0; i < npages; i++)
640 iopte_make_dummy(iommu, base + i);
641
642 s->dma_address = DMA_ERROR_CODE;
643 s->dma_length = 0;
644 }
645 if (s == outs)
646 break;
647 }
648 spin_unlock_irqrestore(&iommu->lock, flags);
649
David S. Miller688cb302005-10-13 22:15:24 -0700650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
David S. Miller13fa14e2008-02-09 03:11:01 -0800653/* If contexts are being used, they are the same in all of the mappings
654 * we make for a particular SG.
655 */
656static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
657{
658 unsigned long ctx = 0;
659
660 if (iommu->iommu_ctxflush) {
661 iopte_t *base;
662 u32 bus_addr;
663
664 bus_addr = sg->dma_address & IO_PAGE_MASK;
665 base = iommu->page_table +
666 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
667
668 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
669 }
670 return ctx;
671}
672
David S. Millerad7ad572007-07-27 22:39:14 -0700673static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
674 int nelems, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
David S. Miller13fa14e2008-02-09 03:11:01 -0800676 unsigned long flags, ctx;
677 struct scatterlist *sg;
David S. Miller38192d52008-02-06 03:50:26 -0800678 struct strbuf *strbuf;
679 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
David S. Miller13fa14e2008-02-09 03:11:01 -0800681 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
David S. Millerad7ad572007-07-27 22:39:14 -0700683 iommu = dev->archdata.iommu;
684 strbuf = dev->archdata.stc;
685
David S. Miller13fa14e2008-02-09 03:11:01 -0800686 ctx = fetch_sg_ctx(iommu, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 spin_lock_irqsave(&iommu->lock, flags);
689
David S. Miller13fa14e2008-02-09 03:11:01 -0800690 sg = sglist;
691 while (nelems--) {
692 dma_addr_t dma_handle = sg->dma_address;
693 unsigned int len = sg->dma_length;
694 unsigned long npages, entry;
695 iopte_t *base;
696 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
David S. Miller13fa14e2008-02-09 03:11:01 -0800698 if (!len)
699 break;
700 npages = iommu_num_pages(dma_handle, len);
701 iommu_range_free(iommu, dma_handle, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
David S. Miller13fa14e2008-02-09 03:11:01 -0800703 entry = ((dma_handle - iommu->page_table_map_base)
704 >> IO_PAGE_SHIFT);
705 base = iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
David S. Miller13fa14e2008-02-09 03:11:01 -0800707 dma_handle &= IO_PAGE_MASK;
708 if (strbuf->strbuf_enabled)
709 strbuf_flush(strbuf, iommu, dma_handle, ctx,
710 npages, direction);
711
712 for (i = 0; i < npages; i++)
713 iopte_make_dummy(iommu, base + i);
714
715 sg = sg_next(sg);
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
David S. Miller7c963ad2005-05-31 16:57:59 -0700718 iommu_free_ctx(iommu, ctx);
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 spin_unlock_irqrestore(&iommu->lock, flags);
721}
722
David S. Millerad7ad572007-07-27 22:39:14 -0700723static void dma_4u_sync_single_for_cpu(struct device *dev,
724 dma_addr_t bus_addr, size_t sz,
725 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
David S. Miller16ce82d2007-04-26 21:08:21 -0700727 struct iommu *iommu;
728 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned long flags, ctx, npages;
730
David S. Millerad7ad572007-07-27 22:39:14 -0700731 iommu = dev->archdata.iommu;
732 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (!strbuf->strbuf_enabled)
735 return;
736
737 spin_lock_irqsave(&iommu->lock, flags);
738
739 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
740 npages >>= IO_PAGE_SHIFT;
741 bus_addr &= IO_PAGE_MASK;
742
743 /* Step 1: Record the context, if any. */
744 ctx = 0;
745 if (iommu->iommu_ctxflush &&
746 strbuf->strbuf_ctxflush) {
747 iopte_t *iopte;
748
749 iopte = iommu->page_table +
750 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
751 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
752 }
753
754 /* Step 2: Kick data out of streaming buffers. */
David S. Millerad7ad572007-07-27 22:39:14 -0700755 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 spin_unlock_irqrestore(&iommu->lock, flags);
758}
759
David S. Millerad7ad572007-07-27 22:39:14 -0700760static void dma_4u_sync_sg_for_cpu(struct device *dev,
761 struct scatterlist *sglist, int nelems,
762 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
David S. Miller16ce82d2007-04-26 21:08:21 -0700764 struct iommu *iommu;
765 struct strbuf *strbuf;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700766 unsigned long flags, ctx, npages, i;
Jens Axboe2c941a22007-08-07 09:37:10 +0200767 struct scatterlist *sg, *sgprv;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700768 u32 bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
David S. Millerad7ad572007-07-27 22:39:14 -0700770 iommu = dev->archdata.iommu;
771 strbuf = dev->archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (!strbuf->strbuf_enabled)
774 return;
775
776 spin_lock_irqsave(&iommu->lock, flags);
777
778 /* Step 1: Record the context, if any. */
779 ctx = 0;
780 if (iommu->iommu_ctxflush &&
781 strbuf->strbuf_ctxflush) {
782 iopte_t *iopte;
783
784 iopte = iommu->page_table +
785 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
786 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
787 }
788
789 /* Step 2: Kick data out of streaming buffers. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700790 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
Jens Axboe2c941a22007-08-07 09:37:10 +0200791 sgprv = NULL;
792 for_each_sg(sglist, sg, nelems, i) {
793 if (sg->dma_length == 0)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700794 break;
Jens Axboe2c941a22007-08-07 09:37:10 +0200795 sgprv = sg;
796 }
797
798 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700799 - bus_addr) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700800 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 spin_unlock_irqrestore(&iommu->lock, flags);
803}
804
David S. Millerad7ad572007-07-27 22:39:14 -0700805const struct dma_ops sun4u_dma_ops = {
806 .alloc_coherent = dma_4u_alloc_coherent,
807 .free_coherent = dma_4u_free_coherent,
808 .map_single = dma_4u_map_single,
809 .unmap_single = dma_4u_unmap_single,
810 .map_sg = dma_4u_map_sg,
811 .unmap_sg = dma_4u_unmap_sg,
812 .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
813 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800814};
815
David S. Millerad7ad572007-07-27 22:39:14 -0700816const struct dma_ops *dma_ops = &sun4u_dma_ops;
817EXPORT_SYMBOL(dma_ops);
818
819int dma_supported(struct device *dev, u64 device_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
David S. Millerad7ad572007-07-27 22:39:14 -0700821 struct iommu *iommu = dev->archdata.iommu;
822 u64 dma_addr_mask = iommu->dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (device_mask >= (1UL << 32UL))
825 return 0;
826
David S. Millerad7ad572007-07-27 22:39:14 -0700827 if ((device_mask & dma_addr_mask) == dma_addr_mask)
828 return 1;
829
830#ifdef CONFIG_PCI
831 if (dev->bus == &pci_bus_type)
832 return pci_dma_supported(to_pci_dev(dev), device_mask);
833#endif
834
835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
David S. Millerad7ad572007-07-27 22:39:14 -0700837EXPORT_SYMBOL(dma_supported);
838
839int dma_set_mask(struct device *dev, u64 dma_mask)
840{
841#ifdef CONFIG_PCI
842 if (dev->bus == &pci_bus_type)
843 return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
844#endif
845 return -EINVAL;
846}
847EXPORT_SYMBOL(dma_set_mask);