blob: 66712772f494d0c2be56b7292a92132af6657d07 [file] [log] [blame]
David S. Miller16ce82d2007-04-26 21:08:21 -07001/* pci_iommu.c: UltraSparc PCI controller IOM/STC 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>
8#include <linux/sched.h>
9#include <linux/mm.h>
David S. Miller4dbc30f2005-05-11 11:37:00 -070010#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <asm/pbm.h>
13
14#include "iommu_common.h"
15
16#define PCI_STC_CTXMATCH_ADDR(STC, CTX) \
17 ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
18
19/* Accessing IOMMU and Streaming Buffer registers.
20 * REG parameter is a physical address. All registers
21 * are 64-bits in size.
22 */
23#define pci_iommu_read(__reg) \
24({ u64 __ret; \
25 __asm__ __volatile__("ldxa [%1] %2, %0" \
26 : "=r" (__ret) \
27 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
28 : "memory"); \
29 __ret; \
30})
31#define pci_iommu_write(__reg, __val) \
32 __asm__ __volatile__("stxa %0, [%1] %2" \
33 : /* no outputs */ \
34 : "r" (__val), "r" (__reg), \
35 "i" (ASI_PHYS_BYPASS_EC_E))
36
37/* Must be invoked under the IOMMU lock. */
David S. Miller16ce82d2007-04-26 21:08:21 -070038static void __iommu_flushall(struct iommu *iommu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 unsigned long tag;
41 int entry;
42
43 tag = iommu->iommu_flush + (0xa580UL - 0x0210UL);
44 for (entry = 0; entry < 16; entry++) {
45 pci_iommu_write(tag, 0);
46 tag += 8;
47 }
48
49 /* Ensure completion of previous PIO writes. */
50 (void) pci_iommu_read(iommu->write_complete_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53#define IOPTE_CONSISTENT(CTX) \
54 (IOPTE_VALID | IOPTE_CACHE | \
55 (((CTX) << 47) & IOPTE_CONTEXT))
56
57#define IOPTE_STREAMING(CTX) \
58 (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
59
60/* Existing mappings are never marked invalid, instead they
61 * are pointed to a dummy page.
62 */
63#define IOPTE_IS_DUMMY(iommu, iopte) \
64 ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
65
David S. Miller16ce82d2007-04-26 21:08:21 -070066static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 unsigned long val = iopte_val(*iopte);
69
70 val &= ~IOPTE_PAGE;
71 val |= iommu->dummy_page_pa;
72
73 iopte_val(*iopte) = val;
74}
75
David S. Miller688cb302005-10-13 22:15:24 -070076/* Based largely upon the ppc64 iommu allocator. */
David S. Miller16ce82d2007-04-26 21:08:21 -070077static long pci_arena_alloc(struct iommu *iommu, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -070078{
David S. Miller9b3627f2007-04-24 23:51:18 -070079 struct iommu_arena *arena = &iommu->arena;
David S. Miller688cb302005-10-13 22:15:24 -070080 unsigned long n, i, start, end, limit;
81 int pass;
82
83 limit = arena->limit;
84 start = arena->hint;
85 pass = 0;
86
87again:
88 n = find_next_zero_bit(arena->map, limit, start);
89 end = n + npages;
90 if (unlikely(end >= limit)) {
91 if (likely(pass < 1)) {
92 limit = start;
93 start = 0;
94 __iommu_flushall(iommu);
95 pass++;
96 goto again;
97 } else {
98 /* Scanned the whole thing, give up. */
99 return -1;
100 }
101 }
102
103 for (i = n; i < end; i++) {
104 if (test_bit(i, arena->map)) {
105 start = i + 1;
106 goto again;
107 }
108 }
109
110 for (i = n; i < end; i++)
111 __set_bit(i, arena->map);
112
113 arena->hint = end;
114
115 return n;
116}
117
David S. Miller9b3627f2007-04-24 23:51:18 -0700118static void pci_arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
David S. Miller688cb302005-10-13 22:15:24 -0700119{
120 unsigned long i;
121
122 for (i = base; i < (base + npages); i++)
123 __clear_bit(i, arena->map);
124}
125
David S. Miller16ce82d2007-04-26 21:08:21 -0700126void pci_iommu_table_init(struct iommu *iommu, int tsbsize, u32 dma_offset, u32 dma_addr_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
David S. Miller688cb302005-10-13 22:15:24 -0700128 unsigned long i, tsbbase, order, sz, num_tsb_entries;
129
130 num_tsb_entries = tsbsize / sizeof(iopte_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David S. Miller51e85132005-10-13 21:10:08 -0700132 /* Setup initial software IOMMU state. */
133 spin_lock_init(&iommu->lock);
134 iommu->ctx_lowest_free = 1;
135 iommu->page_table_map_base = dma_offset;
136 iommu->dma_addr_mask = dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
David S. Miller688cb302005-10-13 22:15:24 -0700138 /* Allocate and initialize the free area map. */
139 sz = num_tsb_entries / 8;
140 sz = (sz + 7UL) & ~7UL;
Eric Sesterhenn91329832006-03-06 13:48:40 -0800141 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
David S. Miller688cb302005-10-13 22:15:24 -0700142 if (!iommu->arena.map) {
143 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
David S. Miller51e85132005-10-13 21:10:08 -0700144 prom_halt();
David S. Miller51e85132005-10-13 21:10:08 -0700145 }
David S. Miller688cb302005-10-13 22:15:24 -0700146 iommu->arena.limit = num_tsb_entries;
David S. Miller51e85132005-10-13 21:10:08 -0700147
148 /* Allocate and initialize the dummy page which we
149 * set inactive IO PTEs to point to.
150 */
151 iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0);
152 if (!iommu->dummy_page) {
153 prom_printf("PCI_IOMMU: Error, gfp(dummy_page) failed.\n");
154 prom_halt();
155 }
156 memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
157 iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
158
159 /* Now allocate and setup the IOMMU page table itself. */
160 order = get_order(tsbsize);
161 tsbbase = __get_free_pages(GFP_KERNEL, order);
162 if (!tsbbase) {
163 prom_printf("PCI_IOMMU: Error, gfp(tsb) failed.\n");
164 prom_halt();
165 }
166 iommu->page_table = (iopte_t *)tsbbase;
167
David S. Miller688cb302005-10-13 22:15:24 -0700168 for (i = 0; i < num_tsb_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 iopte_make_dummy(iommu, &iommu->page_table[i]);
170}
171
David S. Miller16ce82d2007-04-26 21:08:21 -0700172static inline iopte_t *alloc_npages(struct iommu *iommu, unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
David S. Miller688cb302005-10-13 22:15:24 -0700174 long entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David S. Miller688cb302005-10-13 22:15:24 -0700176 entry = pci_arena_alloc(iommu, npages);
177 if (unlikely(entry < 0))
178 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
David S. Miller688cb302005-10-13 22:15:24 -0700180 return iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
David S. Miller16ce82d2007-04-26 21:08:21 -0700183static inline void free_npages(struct iommu *iommu, dma_addr_t base, unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
David S. Miller688cb302005-10-13 22:15:24 -0700185 pci_arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
David S. Miller16ce82d2007-04-26 21:08:21 -0700188static int iommu_alloc_ctx(struct iommu *iommu)
David S. Miller7c963ad2005-05-31 16:57:59 -0700189{
190 int lowest = iommu->ctx_lowest_free;
191 int sz = IOMMU_NUM_CTXS - lowest;
192 int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
193
194 if (unlikely(n == sz)) {
195 n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
196 if (unlikely(n == lowest)) {
197 printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
198 n = 0;
199 }
200 }
201 if (n)
202 __set_bit(n, iommu->ctx_bitmap);
203
204 return n;
205}
206
David S. Miller16ce82d2007-04-26 21:08:21 -0700207static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
David S. Miller7c963ad2005-05-31 16:57:59 -0700208{
209 if (likely(ctx)) {
210 __clear_bit(ctx, iommu->ctx_bitmap);
211 if (ctx < iommu->ctx_lowest_free)
212 iommu->ctx_lowest_free = ctx;
213 }
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/* Allocate and map kernel buffer of size SIZE using consistent mode
217 * DMA for PCI device PDEV. Return non-NULL cpu-side address if
218 * successful and set *DMA_ADDRP to the PCI side dma address.
219 */
David S. Miller42f14232006-05-23 02:07:22 -0700220static void *pci_4u_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
David S. Miller16ce82d2007-04-26 21:08:21 -0700222 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700224 unsigned long flags, order, first_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 void *ret;
226 int npages;
227
228 size = IO_PAGE_ALIGN(size);
229 order = get_order(size);
230 if (order >= 10)
231 return NULL;
232
David S. Miller42f14232006-05-23 02:07:22 -0700233 first_page = __get_free_pages(gfp, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (first_page == 0UL)
235 return NULL;
236 memset((char *)first_page, 0, PAGE_SIZE << order);
237
David S. Millera2fb23a2007-02-28 23:35:04 -0800238 iommu = pdev->dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller688cb302005-10-13 22:15:24 -0700241 iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
242 spin_unlock_irqrestore(&iommu->lock, flags);
243
244 if (unlikely(iopte == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 free_pages(first_page, order);
246 return NULL;
247 }
248
249 *dma_addrp = (iommu->page_table_map_base +
250 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
251 ret = (void *) first_page;
252 npages = size >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 first_page = __pa(first_page);
254 while (npages--) {
David S. Miller688cb302005-10-13 22:15:24 -0700255 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 IOPTE_WRITE |
257 (first_page & IOPTE_PAGE));
258 iopte++;
259 first_page += IO_PAGE_SIZE;
260 }
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return ret;
263}
264
265/* Free and unmap a consistent DMA translation. */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800266static void pci_4u_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
David S. Miller16ce82d2007-04-26 21:08:21 -0700268 struct iommu *iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700270 unsigned long flags, order, npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millera2fb23a2007-02-28 23:35:04 -0800273 iommu = pdev->dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 iopte = iommu->page_table +
275 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
276
277 spin_lock_irqsave(&iommu->lock, flags);
278
David S. Miller012d64f2006-10-25 22:33:07 -0700279 free_npages(iommu, dvma - iommu->page_table_map_base, npages);
David S. Miller7c963ad2005-05-31 16:57:59 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 spin_unlock_irqrestore(&iommu->lock, flags);
282
283 order = get_order(size);
284 if (order < 10)
285 free_pages((unsigned long)cpu, order);
286}
287
288/* Map a single buffer at PTR of SZ bytes for PCI DMA
289 * in streaming mode.
290 */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800291static dma_addr_t pci_4u_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
David S. Miller16ce82d2007-04-26 21:08:21 -0700293 struct iommu *iommu;
294 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 iopte_t *base;
296 unsigned long flags, npages, oaddr;
297 unsigned long i, base_paddr, ctx;
298 u32 bus_addr, ret;
299 unsigned long iopte_protection;
300
David S. Millera2fb23a2007-02-28 23:35:04 -0800301 iommu = pdev->dev.archdata.iommu;
302 strbuf = pdev->dev.archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
David S. Miller688cb302005-10-13 22:15:24 -0700304 if (unlikely(direction == PCI_DMA_NONE))
305 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 oaddr = (unsigned long)ptr;
308 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
309 npages >>= IO_PAGE_SHIFT;
310
311 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller688cb302005-10-13 22:15:24 -0700312 base = alloc_npages(iommu, npages);
313 ctx = 0;
314 if (iommu->iommu_ctxflush)
315 ctx = iommu_alloc_ctx(iommu);
316 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
David S. Miller688cb302005-10-13 22:15:24 -0700318 if (unlikely(!base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 bus_addr = (iommu->page_table_map_base +
322 ((base - iommu->page_table) << IO_PAGE_SHIFT));
323 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
324 base_paddr = __pa(oaddr & IO_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (strbuf->strbuf_enabled)
326 iopte_protection = IOPTE_STREAMING(ctx);
327 else
328 iopte_protection = IOPTE_CONSISTENT(ctx);
329 if (direction != PCI_DMA_TODEVICE)
330 iopte_protection |= IOPTE_WRITE;
331
332 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
333 iopte_val(*base) = iopte_protection | base_paddr;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return ret;
336
337bad:
David S. Miller688cb302005-10-13 22:15:24 -0700338 iommu_free_ctx(iommu, ctx);
339bad_no_ctx:
340 if (printk_ratelimit())
341 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return PCI_DMA_ERROR_CODE;
343}
344
David S. Miller16ce82d2007-04-26 21:08:21 -0700345static void pci_strbuf_flush(struct strbuf *strbuf, struct iommu *iommu, u32 vaddr, unsigned long ctx, unsigned long npages, int direction)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700346{
347 int limit;
348
David S. Miller4dbc30f2005-05-11 11:37:00 -0700349 if (strbuf->strbuf_ctxflush &&
350 iommu->iommu_ctxflush) {
351 unsigned long matchreg, flushreg;
David S. Miller7c963ad2005-05-31 16:57:59 -0700352 u64 val;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700353
354 flushreg = strbuf->strbuf_ctxflush;
355 matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
356
David S. Millera228dfd2005-05-20 11:40:32 -0700357 pci_iommu_write(flushreg, ctx);
David S. Miller88314ee2005-05-31 19:13:52 -0700358 val = pci_iommu_read(matchreg);
359 val &= 0xffff;
360 if (!val)
David S. Miller7c963ad2005-05-31 16:57:59 -0700361 goto do_flush_sync;
362
David S. Miller7c963ad2005-05-31 16:57:59 -0700363 while (val) {
364 if (val & 0x1)
365 pci_iommu_write(flushreg, ctx);
366 val >>= 1;
David S. Millera228dfd2005-05-20 11:40:32 -0700367 }
David S. Miller7c963ad2005-05-31 16:57:59 -0700368 val = pci_iommu_read(matchreg);
369 if (unlikely(val)) {
David S. Miller4dbc30f2005-05-11 11:37:00 -0700370 printk(KERN_WARNING "pci_strbuf_flush: ctx flush "
David S. Miller7c963ad2005-05-31 16:57:59 -0700371 "timeout matchreg[%lx] ctx[%lx]\n",
372 val, ctx);
373 goto do_page_flush;
374 }
David S. Miller4dbc30f2005-05-11 11:37:00 -0700375 } else {
376 unsigned long i;
377
David S. Miller7c963ad2005-05-31 16:57:59 -0700378 do_page_flush:
David S. Miller4dbc30f2005-05-11 11:37:00 -0700379 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
380 pci_iommu_write(strbuf->strbuf_pflush, vaddr);
381 }
382
David S. Miller7c963ad2005-05-31 16:57:59 -0700383do_flush_sync:
384 /* If the device could not have possibly put dirty data into
385 * the streaming cache, no flush-flag synchronization needs
386 * to be performed.
387 */
388 if (direction == PCI_DMA_TODEVICE)
389 return;
390
391 PCI_STC_FLUSHFLAG_INIT(strbuf);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700392 pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
393 (void) pci_iommu_read(iommu->write_complete_reg);
394
David S. Millera228dfd2005-05-20 11:40:32 -0700395 limit = 100000;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700396 while (!PCI_STC_FLUSHFLAG_SET(strbuf)) {
397 limit--;
398 if (!limit)
399 break;
David S. Millera228dfd2005-05-20 11:40:32 -0700400 udelay(1);
David S. Miller4f071182005-08-29 12:46:22 -0700401 rmb();
David S. Miller4dbc30f2005-05-11 11:37:00 -0700402 }
403 if (!limit)
404 printk(KERN_WARNING "pci_strbuf_flush: flushflag timeout "
405 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
406 vaddr, ctx, npages);
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/* Unmap a single streaming mode DMA translation. */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800410static void pci_4u_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
David S. Miller16ce82d2007-04-26 21:08:21 -0700412 struct iommu *iommu;
413 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 iopte_t *base;
David S. Miller688cb302005-10-13 22:15:24 -0700415 unsigned long flags, npages, ctx, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David S. Miller688cb302005-10-13 22:15:24 -0700417 if (unlikely(direction == PCI_DMA_NONE)) {
418 if (printk_ratelimit())
419 WARN_ON(1);
420 return;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
David S. Millera2fb23a2007-02-28 23:35:04 -0800423 iommu = pdev->dev.archdata.iommu;
424 strbuf = pdev->dev.archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
427 npages >>= IO_PAGE_SHIFT;
428 base = iommu->page_table +
429 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
430#ifdef DEBUG_PCI_IOMMU
431 if (IOPTE_IS_DUMMY(iommu, base))
432 printk("pci_unmap_single called on non-mapped region %08x,%08x from %016lx\n",
433 bus_addr, sz, __builtin_return_address(0));
434#endif
435 bus_addr &= IO_PAGE_MASK;
436
437 spin_lock_irqsave(&iommu->lock, flags);
438
439 /* Record the context, if any. */
440 ctx = 0;
441 if (iommu->iommu_ctxflush)
442 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
443
444 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700445 if (strbuf->strbuf_enabled)
David S. Miller688cb302005-10-13 22:15:24 -0700446 pci_strbuf_flush(strbuf, iommu, bus_addr, ctx,
447 npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
David S. Miller688cb302005-10-13 22:15:24 -0700449 /* Step 2: Clear out TSB entries. */
450 for (i = 0; i < npages; i++)
451 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
David S. Miller688cb302005-10-13 22:15:24 -0700453 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
David S. Miller7c963ad2005-05-31 16:57:59 -0700455 iommu_free_ctx(iommu, ctx);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 spin_unlock_irqrestore(&iommu->lock, flags);
458}
459
460#define SG_ENT_PHYS_ADDRESS(SG) \
461 (__pa(page_address((SG)->page)) + (SG)->offset)
462
463static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
464 int nused, int nelems, unsigned long iopte_protection)
465{
466 struct scatterlist *dma_sg = sg;
467 struct scatterlist *sg_end = sg + nelems;
468 int i;
469
470 for (i = 0; i < nused; i++) {
471 unsigned long pteval = ~0UL;
472 u32 dma_npages;
473
474 dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
475 dma_sg->dma_length +
476 ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
477 do {
478 unsigned long offset;
479 signed int len;
480
481 /* If we are here, we know we have at least one
482 * more page to map. So walk forward until we
483 * hit a page crossing, and begin creating new
484 * mappings from that spot.
485 */
486 for (;;) {
487 unsigned long tmp;
488
489 tmp = SG_ENT_PHYS_ADDRESS(sg);
490 len = sg->length;
491 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
492 pteval = tmp & IO_PAGE_MASK;
493 offset = tmp & (IO_PAGE_SIZE - 1UL);
494 break;
495 }
496 if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
497 pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
498 offset = 0UL;
499 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
500 break;
501 }
502 sg++;
503 }
504
505 pteval = iopte_protection | (pteval & IOPTE_PAGE);
506 while (len > 0) {
507 *iopte++ = __iopte(pteval);
508 pteval += IO_PAGE_SIZE;
509 len -= (IO_PAGE_SIZE - offset);
510 offset = 0;
511 dma_npages--;
512 }
513
514 pteval = (pteval & IOPTE_PAGE) + len;
515 sg++;
516
517 /* Skip over any tail mappings we've fully mapped,
518 * adjusting pteval along the way. Stop when we
519 * detect a page crossing event.
520 */
521 while (sg < sg_end &&
522 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
523 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
524 ((pteval ^
525 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
526 pteval += sg->length;
527 sg++;
528 }
529 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
530 pteval = ~0UL;
531 } while (dma_npages != 0);
532 dma_sg++;
533 }
534}
535
536/* Map a set of buffers described by SGLIST with NELEMS array
537 * elements in streaming mode for PCI DMA.
538 * When making changes here, inspect the assembly output. I was having
539 * hard time to kepp this routine out of using stack slots for holding variables.
540 */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800541static int pci_4u_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
David S. Miller16ce82d2007-04-26 21:08:21 -0700543 struct iommu *iommu;
544 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned long flags, ctx, npages, iopte_protection;
546 iopte_t *base;
547 u32 dma_base;
548 struct scatterlist *sgtmp;
549 int used;
550
551 /* Fast path single entry scatterlists. */
552 if (nelems == 1) {
553 sglist->dma_address =
David S. Miller18397942006-02-10 00:08:26 -0800554 pci_4u_map_single(pdev,
555 (page_address(sglist->page) + sglist->offset),
556 sglist->length, direction);
David S. Miller688cb302005-10-13 22:15:24 -0700557 if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 sglist->dma_length = sglist->length;
560 return 1;
561 }
562
David S. Millera2fb23a2007-02-28 23:35:04 -0800563 iommu = pdev->dev.archdata.iommu;
564 strbuf = pdev->dev.archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
David S. Miller688cb302005-10-13 22:15:24 -0700566 if (unlikely(direction == PCI_DMA_NONE))
567 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Step 1: Prepare scatter list. */
570
571 npages = prepare_sg(sglist, nelems);
572
David S. Miller688cb302005-10-13 22:15:24 -0700573 /* Step 2: Allocate a cluster and context, if necessary. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 spin_lock_irqsave(&iommu->lock, flags);
576
David S. Miller688cb302005-10-13 22:15:24 -0700577 base = alloc_npages(iommu, npages);
578 ctx = 0;
579 if (iommu->iommu_ctxflush)
580 ctx = iommu_alloc_ctx(iommu);
581
582 spin_unlock_irqrestore(&iommu->lock, flags);
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (base == NULL)
585 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700586
587 dma_base = iommu->page_table_map_base +
588 ((base - iommu->page_table) << IO_PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* Step 3: Normalize DMA addresses. */
591 used = nelems;
592
593 sgtmp = sglist;
594 while (used && sgtmp->dma_length) {
595 sgtmp->dma_address += dma_base;
596 sgtmp++;
597 used--;
598 }
599 used = nelems - used;
600
David S. Miller688cb302005-10-13 22:15:24 -0700601 /* Step 4: Create the mappings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (strbuf->strbuf_enabled)
603 iopte_protection = IOPTE_STREAMING(ctx);
604 else
605 iopte_protection = IOPTE_CONSISTENT(ctx);
606 if (direction != PCI_DMA_TODEVICE)
607 iopte_protection |= IOPTE_WRITE;
David S. Miller688cb302005-10-13 22:15:24 -0700608
609 fill_sg(base, sglist, used, nelems, iopte_protection);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#ifdef VERIFY_SG
612 verify_sglist(sglist, nelems, base, npages);
613#endif
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return used;
616
617bad:
David S. Miller688cb302005-10-13 22:15:24 -0700618 iommu_free_ctx(iommu, ctx);
619bad_no_ctx:
620 if (printk_ratelimit())
621 WARN_ON(1);
622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/* Unmap a set of streaming mode DMA translations. */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800626static void pci_4u_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
David S. Miller16ce82d2007-04-26 21:08:21 -0700628 struct iommu *iommu;
629 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 iopte_t *base;
631 unsigned long flags, ctx, i, npages;
632 u32 bus_addr;
633
David S. Miller688cb302005-10-13 22:15:24 -0700634 if (unlikely(direction == PCI_DMA_NONE)) {
635 if (printk_ratelimit())
636 WARN_ON(1);
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David S. Millera2fb23a2007-02-28 23:35:04 -0800639 iommu = pdev->dev.archdata.iommu;
640 strbuf = pdev->dev.archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 bus_addr = sglist->dma_address & IO_PAGE_MASK;
643
644 for (i = 1; i < nelems; i++)
645 if (sglist[i].dma_length == 0)
646 break;
647 i--;
David S. Miller688cb302005-10-13 22:15:24 -0700648 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
649 bus_addr) >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 base = iommu->page_table +
652 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
653
654#ifdef DEBUG_PCI_IOMMU
655 if (IOPTE_IS_DUMMY(iommu, base))
656 printk("pci_unmap_sg called on non-mapped region %016lx,%d from %016lx\n", sglist->dma_address, nelems, __builtin_return_address(0));
657#endif
658
659 spin_lock_irqsave(&iommu->lock, flags);
660
661 /* Record the context, if any. */
662 ctx = 0;
663 if (iommu->iommu_ctxflush)
664 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
665
666 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700667 if (strbuf->strbuf_enabled)
David S. Miller7c963ad2005-05-31 16:57:59 -0700668 pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
David S. Miller688cb302005-10-13 22:15:24 -0700670 /* Step 2: Clear out the TSB entries. */
671 for (i = 0; i < npages; i++)
672 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
David S. Miller688cb302005-10-13 22:15:24 -0700674 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
David S. Miller7c963ad2005-05-31 16:57:59 -0700676 iommu_free_ctx(iommu, ctx);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 spin_unlock_irqrestore(&iommu->lock, flags);
679}
680
681/* Make physical memory consistent for a single
682 * streaming mode DMA translation after a transfer.
683 */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800684static void pci_4u_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
David S. Miller16ce82d2007-04-26 21:08:21 -0700686 struct iommu *iommu;
687 struct strbuf *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 unsigned long flags, ctx, npages;
689
David S. Millera2fb23a2007-02-28 23:35:04 -0800690 iommu = pdev->dev.archdata.iommu;
691 strbuf = pdev->dev.archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (!strbuf->strbuf_enabled)
694 return;
695
696 spin_lock_irqsave(&iommu->lock, flags);
697
698 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
699 npages >>= IO_PAGE_SHIFT;
700 bus_addr &= IO_PAGE_MASK;
701
702 /* Step 1: Record the context, if any. */
703 ctx = 0;
704 if (iommu->iommu_ctxflush &&
705 strbuf->strbuf_ctxflush) {
706 iopte_t *iopte;
707
708 iopte = iommu->page_table +
709 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
710 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
711 }
712
713 /* Step 2: Kick data out of streaming buffers. */
David S. Miller7c963ad2005-05-31 16:57:59 -0700714 pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 spin_unlock_irqrestore(&iommu->lock, flags);
717}
718
719/* Make physical memory consistent for a set of streaming
720 * mode DMA translations after a transfer.
721 */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800722static void pci_4u_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
David S. Miller16ce82d2007-04-26 21:08:21 -0700724 struct iommu *iommu;
725 struct strbuf *strbuf;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700726 unsigned long flags, ctx, npages, i;
727 u32 bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
David S. Millera2fb23a2007-02-28 23:35:04 -0800729 iommu = pdev->dev.archdata.iommu;
730 strbuf = pdev->dev.archdata.stc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (!strbuf->strbuf_enabled)
733 return;
734
735 spin_lock_irqsave(&iommu->lock, flags);
736
737 /* Step 1: Record the context, if any. */
738 ctx = 0;
739 if (iommu->iommu_ctxflush &&
740 strbuf->strbuf_ctxflush) {
741 iopte_t *iopte;
742
743 iopte = iommu->page_table +
744 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
745 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
746 }
747
748 /* Step 2: Kick data out of streaming buffers. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700749 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
750 for(i = 1; i < nelems; i++)
751 if (!sglist[i].dma_length)
752 break;
753 i--;
754 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length)
755 - bus_addr) >> IO_PAGE_SHIFT;
David S. Miller7c963ad2005-05-31 16:57:59 -0700756 pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 spin_unlock_irqrestore(&iommu->lock, flags);
759}
760
David S. Millerc6e87562007-03-09 16:58:43 -0800761const struct pci_iommu_ops pci_sun4u_iommu_ops = {
David S. Miller8f6a93a2006-02-09 21:32:07 -0800762 .alloc_consistent = pci_4u_alloc_consistent,
763 .free_consistent = pci_4u_free_consistent,
764 .map_single = pci_4u_map_single,
765 .unmap_single = pci_4u_unmap_single,
766 .map_sg = pci_4u_map_sg,
767 .unmap_sg = pci_4u_unmap_sg,
768 .dma_sync_single_for_cpu = pci_4u_dma_sync_single_for_cpu,
769 .dma_sync_sg_for_cpu = pci_4u_dma_sync_sg_for_cpu,
770};
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
773{
774 struct pci_dev *ali_isa_bridge;
775 u8 val;
776
777 /* ALI sound chips generate 31-bits of DMA, a special register
778 * determines what bit 31 is emitted as.
779 */
780 ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
781 PCI_DEVICE_ID_AL_M1533,
782 NULL);
783
784 pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
785 if (set_bit)
786 val |= 0x01;
787 else
788 val &= ~0x01;
789 pci_write_config_byte(ali_isa_bridge, 0x7e, val);
790 pci_dev_put(ali_isa_bridge);
791}
792
793int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
794{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 u64 dma_addr_mask;
796
797 if (pdev == NULL) {
798 dma_addr_mask = 0xffffffff;
799 } else {
David S. Miller16ce82d2007-04-26 21:08:21 -0700800 struct iommu *iommu = pdev->dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 dma_addr_mask = iommu->dma_addr_mask;
803
804 if (pdev->vendor == PCI_VENDOR_ID_AL &&
805 pdev->device == PCI_DEVICE_ID_AL_M5451 &&
806 device_mask == 0x7fffffff) {
807 ali_sound_dma_hack(pdev,
808 (dma_addr_mask & 0x80000000) != 0);
809 return 1;
810 }
811 }
812
813 if (device_mask >= (1UL << 32UL))
814 return 0;
815
816 return (device_mask & dma_addr_mask) == dma_addr_mask;
817}