blob: a11910be1013d82a7737a148d5698d6e476318f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: pci_iommu.c,v 1.17 2001/12/17 07:05:09 davem Exp $
2 * pci_iommu.c: UltraSparc PCI controller IOM/STC support.
3 *
4 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
5 * Copyright (C) 1999, 2000 Jakub Jelinek (jakub@redhat.com)
6 */
7
8#include <linux/kernel.h>
9#include <linux/sched.h>
10#include <linux/mm.h>
David S. Miller4dbc30f2005-05-11 11:37:00 -070011#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include <asm/pbm.h>
14
15#include "iommu_common.h"
16
17#define PCI_STC_CTXMATCH_ADDR(STC, CTX) \
18 ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
19
20/* Accessing IOMMU and Streaming Buffer registers.
21 * REG parameter is a physical address. All registers
22 * are 64-bits in size.
23 */
24#define pci_iommu_read(__reg) \
25({ u64 __ret; \
26 __asm__ __volatile__("ldxa [%1] %2, %0" \
27 : "=r" (__ret) \
28 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
29 : "memory"); \
30 __ret; \
31})
32#define pci_iommu_write(__reg, __val) \
33 __asm__ __volatile__("stxa %0, [%1] %2" \
34 : /* no outputs */ \
35 : "r" (__val), "r" (__reg), \
36 "i" (ASI_PHYS_BYPASS_EC_E))
37
38/* Must be invoked under the IOMMU lock. */
39static void __iommu_flushall(struct pci_iommu *iommu)
40{
41 unsigned long tag;
42 int entry;
43
44 tag = iommu->iommu_flush + (0xa580UL - 0x0210UL);
45 for (entry = 0; entry < 16; entry++) {
46 pci_iommu_write(tag, 0);
47 tag += 8;
48 }
49
50 /* Ensure completion of previous PIO writes. */
51 (void) pci_iommu_read(iommu->write_complete_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54#define IOPTE_CONSISTENT(CTX) \
55 (IOPTE_VALID | IOPTE_CACHE | \
56 (((CTX) << 47) & IOPTE_CONTEXT))
57
58#define IOPTE_STREAMING(CTX) \
59 (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
60
61/* Existing mappings are never marked invalid, instead they
62 * are pointed to a dummy page.
63 */
64#define IOPTE_IS_DUMMY(iommu, iopte) \
65 ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
66
67static void inline iopte_make_dummy(struct pci_iommu *iommu, iopte_t *iopte)
68{
69 unsigned long val = iopte_val(*iopte);
70
71 val &= ~IOPTE_PAGE;
72 val |= iommu->dummy_page_pa;
73
74 iopte_val(*iopte) = val;
75}
76
David S. Miller688cb302005-10-13 22:15:24 -070077/* Based largely upon the ppc64 iommu allocator. */
78static long pci_arena_alloc(struct pci_iommu *iommu, unsigned long npages)
79{
80 struct pci_iommu_arena *arena = &iommu->arena;
81 unsigned long n, i, start, end, limit;
82 int pass;
83
84 limit = arena->limit;
85 start = arena->hint;
86 pass = 0;
87
88again:
89 n = find_next_zero_bit(arena->map, limit, start);
90 end = n + npages;
91 if (unlikely(end >= limit)) {
92 if (likely(pass < 1)) {
93 limit = start;
94 start = 0;
95 __iommu_flushall(iommu);
96 pass++;
97 goto again;
98 } else {
99 /* Scanned the whole thing, give up. */
100 return -1;
101 }
102 }
103
104 for (i = n; i < end; i++) {
105 if (test_bit(i, arena->map)) {
106 start = i + 1;
107 goto again;
108 }
109 }
110
111 for (i = n; i < end; i++)
112 __set_bit(i, arena->map);
113
114 arena->hint = end;
115
116 return n;
117}
118
119static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages)
120{
121 unsigned long i;
122
123 for (i = base; i < (base + npages); i++)
124 __clear_bit(i, arena->map);
125}
126
David S. Miller51e85132005-10-13 21:10:08 -0700127void pci_iommu_table_init(struct pci_iommu *iommu, int tsbsize, u32 dma_offset, u32 dma_addr_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
David S. Miller688cb302005-10-13 22:15:24 -0700129 unsigned long i, tsbbase, order, sz, num_tsb_entries;
130
131 num_tsb_entries = tsbsize / sizeof(iopte_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
David S. Miller51e85132005-10-13 21:10:08 -0700133 /* Setup initial software IOMMU state. */
134 spin_lock_init(&iommu->lock);
135 iommu->ctx_lowest_free = 1;
136 iommu->page_table_map_base = dma_offset;
137 iommu->dma_addr_mask = dma_addr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
David S. Miller688cb302005-10-13 22:15:24 -0700139 /* Allocate and initialize the free area map. */
140 sz = num_tsb_entries / 8;
141 sz = (sz + 7UL) & ~7UL;
142 iommu->arena.map = kmalloc(sz, GFP_KERNEL);
143 if (!iommu->arena.map) {
144 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
David S. Miller51e85132005-10-13 21:10:08 -0700145 prom_halt();
David S. Miller51e85132005-10-13 21:10:08 -0700146 }
David S. Miller688cb302005-10-13 22:15:24 -0700147 memset(iommu->arena.map, 0, sz);
148 iommu->arena.limit = num_tsb_entries;
David S. Miller51e85132005-10-13 21:10:08 -0700149
150 /* Allocate and initialize the dummy page which we
151 * set inactive IO PTEs to point to.
152 */
153 iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0);
154 if (!iommu->dummy_page) {
155 prom_printf("PCI_IOMMU: Error, gfp(dummy_page) failed.\n");
156 prom_halt();
157 }
158 memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
159 iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
160
161 /* Now allocate and setup the IOMMU page table itself. */
162 order = get_order(tsbsize);
163 tsbbase = __get_free_pages(GFP_KERNEL, order);
164 if (!tsbbase) {
165 prom_printf("PCI_IOMMU: Error, gfp(tsb) failed.\n");
166 prom_halt();
167 }
168 iommu->page_table = (iopte_t *)tsbbase;
169
David S. Miller688cb302005-10-13 22:15:24 -0700170 for (i = 0; i < num_tsb_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 iopte_make_dummy(iommu, &iommu->page_table[i]);
172}
173
David S. Miller688cb302005-10-13 22:15:24 -0700174static inline iopte_t *alloc_npages(struct pci_iommu *iommu, unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
David S. Miller688cb302005-10-13 22:15:24 -0700176 long entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
David S. Miller688cb302005-10-13 22:15:24 -0700178 entry = pci_arena_alloc(iommu, npages);
179 if (unlikely(entry < 0))
180 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
David S. Miller688cb302005-10-13 22:15:24 -0700182 return iommu->page_table + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
David S. Miller688cb302005-10-13 22:15:24 -0700185static inline void free_npages(struct pci_iommu *iommu, dma_addr_t base, unsigned long npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
David S. Miller688cb302005-10-13 22:15:24 -0700187 pci_arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
David S. Miller7c963ad2005-05-31 16:57:59 -0700190static int iommu_alloc_ctx(struct pci_iommu *iommu)
191{
192 int lowest = iommu->ctx_lowest_free;
193 int sz = IOMMU_NUM_CTXS - lowest;
194 int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
195
196 if (unlikely(n == sz)) {
197 n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
198 if (unlikely(n == lowest)) {
199 printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
200 n = 0;
201 }
202 }
203 if (n)
204 __set_bit(n, iommu->ctx_bitmap);
205
206 return n;
207}
208
209static inline void iommu_free_ctx(struct pci_iommu *iommu, int ctx)
210{
211 if (likely(ctx)) {
212 __clear_bit(ctx, iommu->ctx_bitmap);
213 if (ctx < iommu->ctx_lowest_free)
214 iommu->ctx_lowest_free = ctx;
215 }
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/* Allocate and map kernel buffer of size SIZE using consistent mode
219 * DMA for PCI device PDEV. Return non-NULL cpu-side address if
220 * successful and set *DMA_ADDRP to the PCI side dma address.
221 */
222void *pci_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp)
223{
224 struct pcidev_cookie *pcp;
225 struct pci_iommu *iommu;
226 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700227 unsigned long flags, order, first_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 void *ret;
229 int npages;
230
231 size = IO_PAGE_ALIGN(size);
232 order = get_order(size);
233 if (order >= 10)
234 return NULL;
235
236 first_page = __get_free_pages(GFP_ATOMIC, order);
237 if (first_page == 0UL)
238 return NULL;
239 memset((char *)first_page, 0, PAGE_SIZE << order);
240
241 pcp = pdev->sysdata;
242 iommu = pcp->pbm->iommu;
243
244 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller688cb302005-10-13 22:15:24 -0700245 iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
246 spin_unlock_irqrestore(&iommu->lock, flags);
247
248 if (unlikely(iopte == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 free_pages(first_page, order);
250 return NULL;
251 }
252
253 *dma_addrp = (iommu->page_table_map_base +
254 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
255 ret = (void *) first_page;
256 npages = size >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 first_page = __pa(first_page);
258 while (npages--) {
David S. Miller688cb302005-10-13 22:15:24 -0700259 iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 IOPTE_WRITE |
261 (first_page & IOPTE_PAGE));
262 iopte++;
263 first_page += IO_PAGE_SIZE;
264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return ret;
267}
268
269/* Free and unmap a consistent DMA translation. */
270void pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
271{
272 struct pcidev_cookie *pcp;
273 struct pci_iommu *iommu;
274 iopte_t *iopte;
David S. Miller688cb302005-10-13 22:15:24 -0700275 unsigned long flags, order, npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
278 pcp = pdev->sysdata;
279 iommu = pcp->pbm->iommu;
280 iopte = iommu->page_table +
281 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
282
283 spin_lock_irqsave(&iommu->lock, flags);
284
David S. Miller688cb302005-10-13 22:15:24 -0700285 free_npages(iommu, dvma, npages);
David S. Miller7c963ad2005-05-31 16:57:59 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 spin_unlock_irqrestore(&iommu->lock, flags);
288
289 order = get_order(size);
290 if (order < 10)
291 free_pages((unsigned long)cpu, order);
292}
293
294/* Map a single buffer at PTR of SZ bytes for PCI DMA
295 * in streaming mode.
296 */
297dma_addr_t pci_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
298{
299 struct pcidev_cookie *pcp;
300 struct pci_iommu *iommu;
301 struct pci_strbuf *strbuf;
302 iopte_t *base;
303 unsigned long flags, npages, oaddr;
304 unsigned long i, base_paddr, ctx;
305 u32 bus_addr, ret;
306 unsigned long iopte_protection;
307
308 pcp = pdev->sysdata;
309 iommu = pcp->pbm->iommu;
310 strbuf = &pcp->pbm->stc;
311
David S. Miller688cb302005-10-13 22:15:24 -0700312 if (unlikely(direction == PCI_DMA_NONE))
313 goto bad_no_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 oaddr = (unsigned long)ptr;
316 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
317 npages >>= IO_PAGE_SHIFT;
318
319 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller688cb302005-10-13 22:15:24 -0700320 base = alloc_npages(iommu, npages);
321 ctx = 0;
322 if (iommu->iommu_ctxflush)
323 ctx = iommu_alloc_ctx(iommu);
324 spin_unlock_irqrestore(&iommu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
David S. Miller688cb302005-10-13 22:15:24 -0700326 if (unlikely(!base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 goto bad;
David S. Miller688cb302005-10-13 22:15:24 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 bus_addr = (iommu->page_table_map_base +
330 ((base - iommu->page_table) << IO_PAGE_SHIFT));
331 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
332 base_paddr = __pa(oaddr & IO_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (strbuf->strbuf_enabled)
334 iopte_protection = IOPTE_STREAMING(ctx);
335 else
336 iopte_protection = IOPTE_CONSISTENT(ctx);
337 if (direction != PCI_DMA_TODEVICE)
338 iopte_protection |= IOPTE_WRITE;
339
340 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
341 iopte_val(*base) = iopte_protection | base_paddr;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return ret;
344
345bad:
David S. Miller688cb302005-10-13 22:15:24 -0700346 iommu_free_ctx(iommu, ctx);
347bad_no_ctx:
348 if (printk_ratelimit())
349 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return PCI_DMA_ERROR_CODE;
351}
352
David S. Miller7c963ad2005-05-31 16:57:59 -0700353static void pci_strbuf_flush(struct pci_strbuf *strbuf, struct pci_iommu *iommu, u32 vaddr, unsigned long ctx, unsigned long npages, int direction)
David S. Miller4dbc30f2005-05-11 11:37:00 -0700354{
355 int limit;
356
David S. Miller4dbc30f2005-05-11 11:37:00 -0700357 if (strbuf->strbuf_ctxflush &&
358 iommu->iommu_ctxflush) {
359 unsigned long matchreg, flushreg;
David S. Miller7c963ad2005-05-31 16:57:59 -0700360 u64 val;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700361
362 flushreg = strbuf->strbuf_ctxflush;
363 matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
364
David S. Millera228dfd2005-05-20 11:40:32 -0700365 pci_iommu_write(flushreg, ctx);
David S. Miller88314ee2005-05-31 19:13:52 -0700366 val = pci_iommu_read(matchreg);
367 val &= 0xffff;
368 if (!val)
David S. Miller7c963ad2005-05-31 16:57:59 -0700369 goto do_flush_sync;
370
David S. Miller7c963ad2005-05-31 16:57:59 -0700371 while (val) {
372 if (val & 0x1)
373 pci_iommu_write(flushreg, ctx);
374 val >>= 1;
David S. Millera228dfd2005-05-20 11:40:32 -0700375 }
David S. Miller7c963ad2005-05-31 16:57:59 -0700376 val = pci_iommu_read(matchreg);
377 if (unlikely(val)) {
David S. Miller4dbc30f2005-05-11 11:37:00 -0700378 printk(KERN_WARNING "pci_strbuf_flush: ctx flush "
David S. Miller7c963ad2005-05-31 16:57:59 -0700379 "timeout matchreg[%lx] ctx[%lx]\n",
380 val, ctx);
381 goto do_page_flush;
382 }
David S. Miller4dbc30f2005-05-11 11:37:00 -0700383 } else {
384 unsigned long i;
385
David S. Miller7c963ad2005-05-31 16:57:59 -0700386 do_page_flush:
David S. Miller4dbc30f2005-05-11 11:37:00 -0700387 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
388 pci_iommu_write(strbuf->strbuf_pflush, vaddr);
389 }
390
David S. Miller7c963ad2005-05-31 16:57:59 -0700391do_flush_sync:
392 /* If the device could not have possibly put dirty data into
393 * the streaming cache, no flush-flag synchronization needs
394 * to be performed.
395 */
396 if (direction == PCI_DMA_TODEVICE)
397 return;
398
399 PCI_STC_FLUSHFLAG_INIT(strbuf);
David S. Miller4dbc30f2005-05-11 11:37:00 -0700400 pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
401 (void) pci_iommu_read(iommu->write_complete_reg);
402
David S. Millera228dfd2005-05-20 11:40:32 -0700403 limit = 100000;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700404 while (!PCI_STC_FLUSHFLAG_SET(strbuf)) {
405 limit--;
406 if (!limit)
407 break;
David S. Millera228dfd2005-05-20 11:40:32 -0700408 udelay(1);
David S. Miller4f071182005-08-29 12:46:22 -0700409 rmb();
David S. Miller4dbc30f2005-05-11 11:37:00 -0700410 }
411 if (!limit)
412 printk(KERN_WARNING "pci_strbuf_flush: flushflag timeout "
413 "vaddr[%08x] ctx[%lx] npages[%ld]\n",
414 vaddr, ctx, npages);
415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/* Unmap a single streaming mode DMA translation. */
418void pci_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
419{
420 struct pcidev_cookie *pcp;
421 struct pci_iommu *iommu;
422 struct pci_strbuf *strbuf;
423 iopte_t *base;
David S. Miller688cb302005-10-13 22:15:24 -0700424 unsigned long flags, npages, ctx, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David S. Miller688cb302005-10-13 22:15:24 -0700426 if (unlikely(direction == PCI_DMA_NONE)) {
427 if (printk_ratelimit())
428 WARN_ON(1);
429 return;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 pcp = pdev->sysdata;
433 iommu = pcp->pbm->iommu;
434 strbuf = &pcp->pbm->stc;
435
436 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
437 npages >>= IO_PAGE_SHIFT;
438 base = iommu->page_table +
439 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
440#ifdef DEBUG_PCI_IOMMU
441 if (IOPTE_IS_DUMMY(iommu, base))
442 printk("pci_unmap_single called on non-mapped region %08x,%08x from %016lx\n",
443 bus_addr, sz, __builtin_return_address(0));
444#endif
445 bus_addr &= IO_PAGE_MASK;
446
447 spin_lock_irqsave(&iommu->lock, flags);
448
449 /* Record the context, if any. */
450 ctx = 0;
451 if (iommu->iommu_ctxflush)
452 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
453
454 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700455 if (strbuf->strbuf_enabled)
David S. Miller688cb302005-10-13 22:15:24 -0700456 pci_strbuf_flush(strbuf, iommu, bus_addr, ctx,
457 npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
David S. Miller688cb302005-10-13 22:15:24 -0700459 /* Step 2: Clear out TSB entries. */
460 for (i = 0; i < npages; i++)
461 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
David S. Miller688cb302005-10-13 22:15:24 -0700463 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
David S. Miller7c963ad2005-05-31 16:57:59 -0700465 iommu_free_ctx(iommu, ctx);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 spin_unlock_irqrestore(&iommu->lock, flags);
468}
469
470#define SG_ENT_PHYS_ADDRESS(SG) \
471 (__pa(page_address((SG)->page)) + (SG)->offset)
472
473static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
474 int nused, int nelems, unsigned long iopte_protection)
475{
476 struct scatterlist *dma_sg = sg;
477 struct scatterlist *sg_end = sg + nelems;
478 int i;
479
480 for (i = 0; i < nused; i++) {
481 unsigned long pteval = ~0UL;
482 u32 dma_npages;
483
484 dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
485 dma_sg->dma_length +
486 ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
487 do {
488 unsigned long offset;
489 signed int len;
490
491 /* If we are here, we know we have at least one
492 * more page to map. So walk forward until we
493 * hit a page crossing, and begin creating new
494 * mappings from that spot.
495 */
496 for (;;) {
497 unsigned long tmp;
498
499 tmp = SG_ENT_PHYS_ADDRESS(sg);
500 len = sg->length;
501 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
502 pteval = tmp & IO_PAGE_MASK;
503 offset = tmp & (IO_PAGE_SIZE - 1UL);
504 break;
505 }
506 if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
507 pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
508 offset = 0UL;
509 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
510 break;
511 }
512 sg++;
513 }
514
515 pteval = iopte_protection | (pteval & IOPTE_PAGE);
516 while (len > 0) {
517 *iopte++ = __iopte(pteval);
518 pteval += IO_PAGE_SIZE;
519 len -= (IO_PAGE_SIZE - offset);
520 offset = 0;
521 dma_npages--;
522 }
523
524 pteval = (pteval & IOPTE_PAGE) + len;
525 sg++;
526
527 /* Skip over any tail mappings we've fully mapped,
528 * adjusting pteval along the way. Stop when we
529 * detect a page crossing event.
530 */
531 while (sg < sg_end &&
532 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
533 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
534 ((pteval ^
535 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
536 pteval += sg->length;
537 sg++;
538 }
539 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
540 pteval = ~0UL;
541 } while (dma_npages != 0);
542 dma_sg++;
543 }
544}
545
546/* Map a set of buffers described by SGLIST with NELEMS array
547 * elements in streaming mode for PCI DMA.
548 * When making changes here, inspect the assembly output. I was having
549 * hard time to kepp this routine out of using stack slots for holding variables.
550 */
551int pci_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
552{
553 struct pcidev_cookie *pcp;
554 struct pci_iommu *iommu;
555 struct pci_strbuf *strbuf;
556 unsigned long flags, ctx, npages, iopte_protection;
557 iopte_t *base;
558 u32 dma_base;
559 struct scatterlist *sgtmp;
560 int used;
561
562 /* Fast path single entry scatterlists. */
563 if (nelems == 1) {
564 sglist->dma_address =
565 pci_map_single(pdev,
566 (page_address(sglist->page) + sglist->offset),
567 sglist->length, direction);
David S. Miller688cb302005-10-13 22:15:24 -0700568 if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 sglist->dma_length = sglist->length;
571 return 1;
572 }
573
574 pcp = pdev->sysdata;
575 iommu = pcp->pbm->iommu;
576 strbuf = &pcp->pbm->stc;
577
David S. Miller688cb302005-10-13 22:15:24 -0700578 if (unlikely(direction == PCI_DMA_NONE))
579 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;
608 sgtmp++;
609 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);
618 if (direction != PCI_DMA_TODEVICE)
619 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
637/* Unmap a set of streaming mode DMA translations. */
638void pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
639{
640 struct pcidev_cookie *pcp;
641 struct pci_iommu *iommu;
642 struct pci_strbuf *strbuf;
643 iopte_t *base;
644 unsigned long flags, ctx, i, npages;
645 u32 bus_addr;
646
David S. Miller688cb302005-10-13 22:15:24 -0700647 if (unlikely(direction == PCI_DMA_NONE)) {
648 if (printk_ratelimit())
649 WARN_ON(1);
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 pcp = pdev->sysdata;
653 iommu = pcp->pbm->iommu;
654 strbuf = &pcp->pbm->stc;
655
656 bus_addr = sglist->dma_address & IO_PAGE_MASK;
657
658 for (i = 1; i < nelems; i++)
659 if (sglist[i].dma_length == 0)
660 break;
661 i--;
David S. Miller688cb302005-10-13 22:15:24 -0700662 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
663 bus_addr) >> IO_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 base = iommu->page_table +
666 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
667
668#ifdef DEBUG_PCI_IOMMU
669 if (IOPTE_IS_DUMMY(iommu, base))
670 printk("pci_unmap_sg called on non-mapped region %016lx,%d from %016lx\n", sglist->dma_address, nelems, __builtin_return_address(0));
671#endif
672
673 spin_lock_irqsave(&iommu->lock, flags);
674
675 /* Record the context, if any. */
676 ctx = 0;
677 if (iommu->iommu_ctxflush)
678 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
679
680 /* Step 1: Kick data out of streaming buffers if necessary. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700681 if (strbuf->strbuf_enabled)
David S. Miller7c963ad2005-05-31 16:57:59 -0700682 pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David S. Miller688cb302005-10-13 22:15:24 -0700684 /* Step 2: Clear out the TSB entries. */
685 for (i = 0; i < npages; i++)
686 iopte_make_dummy(iommu, base + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
David S. Miller688cb302005-10-13 22:15:24 -0700688 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
David S. Miller7c963ad2005-05-31 16:57:59 -0700690 iommu_free_ctx(iommu, ctx);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 spin_unlock_irqrestore(&iommu->lock, flags);
693}
694
695/* Make physical memory consistent for a single
696 * streaming mode DMA translation after a transfer.
697 */
698void pci_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
699{
700 struct pcidev_cookie *pcp;
701 struct pci_iommu *iommu;
702 struct pci_strbuf *strbuf;
703 unsigned long flags, ctx, npages;
704
705 pcp = pdev->sysdata;
706 iommu = pcp->pbm->iommu;
707 strbuf = &pcp->pbm->stc;
708
709 if (!strbuf->strbuf_enabled)
710 return;
711
712 spin_lock_irqsave(&iommu->lock, flags);
713
714 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
715 npages >>= IO_PAGE_SHIFT;
716 bus_addr &= IO_PAGE_MASK;
717
718 /* Step 1: Record the context, if any. */
719 ctx = 0;
720 if (iommu->iommu_ctxflush &&
721 strbuf->strbuf_ctxflush) {
722 iopte_t *iopte;
723
724 iopte = iommu->page_table +
725 ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
726 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
727 }
728
729 /* Step 2: Kick data out of streaming buffers. */
David S. Miller7c963ad2005-05-31 16:57:59 -0700730 pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 spin_unlock_irqrestore(&iommu->lock, flags);
733}
734
735/* Make physical memory consistent for a set of streaming
736 * mode DMA translations after a transfer.
737 */
738void pci_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
739{
740 struct pcidev_cookie *pcp;
741 struct pci_iommu *iommu;
742 struct pci_strbuf *strbuf;
David S. Miller4dbc30f2005-05-11 11:37:00 -0700743 unsigned long flags, ctx, npages, i;
744 u32 bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 pcp = pdev->sysdata;
747 iommu = pcp->pbm->iommu;
748 strbuf = &pcp->pbm->stc;
749
750 if (!strbuf->strbuf_enabled)
751 return;
752
753 spin_lock_irqsave(&iommu->lock, flags);
754
755 /* Step 1: Record the context, if any. */
756 ctx = 0;
757 if (iommu->iommu_ctxflush &&
758 strbuf->strbuf_ctxflush) {
759 iopte_t *iopte;
760
761 iopte = iommu->page_table +
762 ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
763 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
764 }
765
766 /* Step 2: Kick data out of streaming buffers. */
David S. Miller4dbc30f2005-05-11 11:37:00 -0700767 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
768 for(i = 1; i < nelems; i++)
769 if (!sglist[i].dma_length)
770 break;
771 i--;
772 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length)
773 - bus_addr) >> IO_PAGE_SHIFT;
David S. Miller7c963ad2005-05-31 16:57:59 -0700774 pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 spin_unlock_irqrestore(&iommu->lock, flags);
777}
778
779static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
780{
781 struct pci_dev *ali_isa_bridge;
782 u8 val;
783
784 /* ALI sound chips generate 31-bits of DMA, a special register
785 * determines what bit 31 is emitted as.
786 */
787 ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
788 PCI_DEVICE_ID_AL_M1533,
789 NULL);
790
791 pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
792 if (set_bit)
793 val |= 0x01;
794 else
795 val &= ~0x01;
796 pci_write_config_byte(ali_isa_bridge, 0x7e, val);
797 pci_dev_put(ali_isa_bridge);
798}
799
800int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
801{
802 struct pcidev_cookie *pcp = pdev->sysdata;
803 u64 dma_addr_mask;
804
805 if (pdev == NULL) {
806 dma_addr_mask = 0xffffffff;
807 } else {
808 struct pci_iommu *iommu = pcp->pbm->iommu;
809
810 dma_addr_mask = iommu->dma_addr_mask;
811
812 if (pdev->vendor == PCI_VENDOR_ID_AL &&
813 pdev->device == PCI_DEVICE_ID_AL_M5451 &&
814 device_mask == 0x7fffffff) {
815 ali_sound_dma_hack(pdev,
816 (dma_addr_mask & 0x80000000) != 0);
817 return 1;
818 }
819 }
820
821 if (device_mask >= (1UL << 32UL))
822 return 0;
823
824 return (device_mask & dma_addr_mask) == dma_addr_mask;
825}