ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sparc64 / kernel / pci_iommu.c
1 /* $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>
11
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. */
38 static void __iommu_flushall(struct pci_iommu *iommu)
39 {
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);
51
52         /* Now update everyone's flush point. */
53         for (entry = 0; entry < PBM_NCLUSTERS; entry++) {
54                 iommu->alloc_info[entry].flush =
55                         iommu->alloc_info[entry].next;
56         }
57 }
58
59 static iopte_t *alloc_streaming_cluster(struct pci_iommu *iommu, unsigned long npages)
60 {
61         iopte_t *iopte, *limit, *first;
62         unsigned long cnum, ent, flush_point;
63
64         cnum = 0;
65         while ((1UL << cnum) < npages)
66                 cnum++;
67         iopte  = (iommu->page_table +
68                   (cnum << (iommu->page_table_sz_bits - PBM_LOGCLUSTERS)));
69
70         if (cnum == 0)
71                 limit = (iommu->page_table +
72                          iommu->lowest_consistent_map);
73         else
74                 limit = (iopte +
75                          (1 << (iommu->page_table_sz_bits - PBM_LOGCLUSTERS)));
76
77         iopte += ((ent = iommu->alloc_info[cnum].next) << cnum);
78         flush_point = iommu->alloc_info[cnum].flush;
79         
80         first = iopte;
81         for (;;) {
82                 if (iopte_val(*iopte) == 0UL) {
83                         if ((iopte + (1 << cnum)) >= limit)
84                                 ent = 0;
85                         else
86                                 ent = ent + 1;
87                         iommu->alloc_info[cnum].next = ent;
88                         if (ent == flush_point)
89                                 __iommu_flushall(iommu);
90                         break;
91                 }
92                 iopte += (1 << cnum);
93                 ent++;
94                 if (iopte >= limit) {
95                         iopte = (iommu->page_table +
96                                  (cnum <<
97                                   (iommu->page_table_sz_bits - PBM_LOGCLUSTERS)));
98                         ent = 0;
99                 }
100                 if (ent == flush_point)
101                         __iommu_flushall(iommu);
102                 if (iopte == first)
103                         goto bad;
104         }
105
106         /* I've got your streaming cluster right here buddy boy... */
107         return iopte;
108
109 bad:
110         printk(KERN_EMERG "pci_iommu: alloc_streaming_cluster of npages(%ld) failed!\n",
111                npages);
112         return NULL;
113 }
114
115 static void free_streaming_cluster(struct pci_iommu *iommu, dma_addr_t base,
116                                    unsigned long npages, unsigned long ctx)
117 {
118         unsigned long cnum, ent;
119
120         cnum = 0;
121         while ((1UL << cnum) < npages)
122                 cnum++;
123
124         ent = (base << (32 - IO_PAGE_SHIFT + PBM_LOGCLUSTERS - iommu->page_table_sz_bits))
125                 >> (32 + PBM_LOGCLUSTERS + cnum - iommu->page_table_sz_bits);
126
127         /* If the global flush might not have caught this entry,
128          * adjust the flush point such that we will flush before
129          * ever trying to reuse it.
130          */
131 #define between(X,Y,Z)  (((Z) - (Y)) >= ((X) - (Y)))
132         if (between(ent, iommu->alloc_info[cnum].next, iommu->alloc_info[cnum].flush))
133                 iommu->alloc_info[cnum].flush = ent;
134 #undef between
135 }
136
137 /* We allocate consistent mappings from the end of cluster zero. */
138 static iopte_t *alloc_consistent_cluster(struct pci_iommu *iommu, unsigned long npages)
139 {
140         iopte_t *iopte;
141
142         iopte = iommu->page_table + (1 << (iommu->page_table_sz_bits - PBM_LOGCLUSTERS));
143         while (iopte > iommu->page_table) {
144                 iopte--;
145                 if (!(iopte_val(*iopte) & IOPTE_VALID)) {
146                         unsigned long tmp = npages;
147
148                         while (--tmp) {
149                                 iopte--;
150                                 if (iopte_val(*iopte) & IOPTE_VALID)
151                                         break;
152                         }
153                         if (tmp == 0) {
154                                 u32 entry = (iopte - iommu->page_table);
155
156                                 if (entry < iommu->lowest_consistent_map)
157                                         iommu->lowest_consistent_map = entry;
158                                 return iopte;
159                         }
160                 }
161         }
162         return NULL;
163 }
164
165 #define IOPTE_CONSISTENT(CTX) \
166         (IOPTE_VALID | IOPTE_CACHE | \
167          (((CTX) << 47) & IOPTE_CONTEXT))
168
169 #define IOPTE_STREAMING(CTX) \
170         (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
171
172 #define IOPTE_INVALID   0UL
173
174 /* Allocate and map kernel buffer of size SIZE using consistent mode
175  * DMA for PCI device PDEV.  Return non-NULL cpu-side address if
176  * successful and set *DMA_ADDRP to the PCI side dma address.
177  */
178 void *pci_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp)
179 {
180         struct pcidev_cookie *pcp;
181         struct pci_iommu *iommu;
182         iopte_t *iopte;
183         unsigned long flags, order, first_page, ctx;
184         void *ret;
185         int npages;
186
187         size = IO_PAGE_ALIGN(size);
188         order = get_order(size);
189         if (order >= 10)
190                 return NULL;
191
192         first_page = __get_free_pages(GFP_ATOMIC, order);
193         if (first_page == 0UL)
194                 return NULL;
195         memset((char *)first_page, 0, PAGE_SIZE << order);
196
197         pcp = pdev->sysdata;
198         iommu = pcp->pbm->iommu;
199
200         spin_lock_irqsave(&iommu->lock, flags);
201         iopte = alloc_consistent_cluster(iommu, size >> IO_PAGE_SHIFT);
202         if (iopte == NULL) {
203                 spin_unlock_irqrestore(&iommu->lock, flags);
204                 free_pages(first_page, order);
205                 return NULL;
206         }
207
208         *dma_addrp = (iommu->page_table_map_base +
209                       ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
210         ret = (void *) first_page;
211         npages = size >> IO_PAGE_SHIFT;
212         ctx = 0;
213         if (iommu->iommu_ctxflush)
214                 ctx = iommu->iommu_cur_ctx++;
215         first_page = __pa(first_page);
216         while (npages--) {
217                 iopte_val(*iopte) = (IOPTE_CONSISTENT(ctx) |
218                                      IOPTE_WRITE |
219                                      (first_page & IOPTE_PAGE));
220                 iopte++;
221                 first_page += IO_PAGE_SIZE;
222         }
223
224         {
225                 int i;
226                 u32 daddr = *dma_addrp;
227
228                 npages = size >> IO_PAGE_SHIFT;
229                 for (i = 0; i < npages; i++) {
230                         pci_iommu_write(iommu->iommu_flush, daddr);
231                         daddr += IO_PAGE_SIZE;
232                 }
233         }
234
235         spin_unlock_irqrestore(&iommu->lock, flags);
236
237         return ret;
238 }
239
240 /* Free and unmap a consistent DMA translation. */
241 void pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
242 {
243         struct pcidev_cookie *pcp;
244         struct pci_iommu *iommu;
245         iopte_t *iopte;
246         unsigned long flags, order, npages, i, ctx;
247
248         npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
249         pcp = pdev->sysdata;
250         iommu = pcp->pbm->iommu;
251         iopte = iommu->page_table +
252                 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
253
254         spin_lock_irqsave(&iommu->lock, flags);
255
256         if ((iopte - iommu->page_table) ==
257             iommu->lowest_consistent_map) {
258                 iopte_t *walk = iopte + npages;
259                 iopte_t *limit;
260
261                 limit = (iommu->page_table +
262                          (1 << (iommu->page_table_sz_bits - PBM_LOGCLUSTERS)));
263                 while (walk < limit) {
264                         if (iopte_val(*walk) != IOPTE_INVALID)
265                                 break;
266                         walk++;
267                 }
268                 iommu->lowest_consistent_map =
269                         (walk - iommu->page_table);
270         }
271
272         /* Data for consistent mappings cannot enter the streaming
273          * buffers, so we only need to update the TSB.  We flush
274          * the IOMMU here as well to prevent conflicts with the
275          * streaming mapping deferred tlb flush scheme.
276          */
277
278         ctx = 0;
279         if (iommu->iommu_ctxflush)
280                 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
281
282         for (i = 0; i < npages; i++, iopte++)
283                 iopte_val(*iopte) = IOPTE_INVALID;
284
285         if (iommu->iommu_ctxflush) {
286                 pci_iommu_write(iommu->iommu_ctxflush, ctx);
287         } else {
288                 for (i = 0; i < npages; i++) {
289                         u32 daddr = dvma + (i << IO_PAGE_SHIFT);
290
291                         pci_iommu_write(iommu->iommu_flush, daddr);
292                 }
293         }
294
295         spin_unlock_irqrestore(&iommu->lock, flags);
296
297         order = get_order(size);
298         if (order < 10)
299                 free_pages((unsigned long)cpu, order);
300 }
301
302 /* Map a single buffer at PTR of SZ bytes for PCI DMA
303  * in streaming mode.
304  */
305 dma_addr_t pci_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
306 {
307         struct pcidev_cookie *pcp;
308         struct pci_iommu *iommu;
309         struct pci_strbuf *strbuf;
310         iopte_t *base;
311         unsigned long flags, npages, oaddr;
312         unsigned long i, base_paddr, ctx;
313         u32 bus_addr, ret;
314         unsigned long iopte_protection;
315
316         pcp = pdev->sysdata;
317         iommu = pcp->pbm->iommu;
318         strbuf = &pcp->pbm->stc;
319
320         if (direction == PCI_DMA_NONE)
321                 BUG();
322
323         oaddr = (unsigned long)ptr;
324         npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
325         npages >>= IO_PAGE_SHIFT;
326
327         spin_lock_irqsave(&iommu->lock, flags);
328
329         base = alloc_streaming_cluster(iommu, npages);
330         if (base == NULL)
331                 goto bad;
332         bus_addr = (iommu->page_table_map_base +
333                     ((base - iommu->page_table) << IO_PAGE_SHIFT));
334         ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
335         base_paddr = __pa(oaddr & IO_PAGE_MASK);
336         ctx = 0;
337         if (iommu->iommu_ctxflush)
338                 ctx = iommu->iommu_cur_ctx++;
339         if (strbuf->strbuf_enabled)
340                 iopte_protection = IOPTE_STREAMING(ctx);
341         else
342                 iopte_protection = IOPTE_CONSISTENT(ctx);
343         if (direction != PCI_DMA_TODEVICE)
344                 iopte_protection |= IOPTE_WRITE;
345
346         for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
347                 iopte_val(*base) = iopte_protection | base_paddr;
348
349         spin_unlock_irqrestore(&iommu->lock, flags);
350
351         return ret;
352
353 bad:
354         spin_unlock_irqrestore(&iommu->lock, flags);
355         return PCI_DMA_ERROR_CODE;
356 }
357
358 /* Unmap a single streaming mode DMA translation. */
359 void pci_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
360 {
361         struct pcidev_cookie *pcp;
362         struct pci_iommu *iommu;
363         struct pci_strbuf *strbuf;
364         iopte_t *base;
365         unsigned long flags, npages, i, ctx;
366
367         if (direction == PCI_DMA_NONE)
368                 BUG();
369
370         pcp = pdev->sysdata;
371         iommu = pcp->pbm->iommu;
372         strbuf = &pcp->pbm->stc;
373
374         npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
375         npages >>= IO_PAGE_SHIFT;
376         base = iommu->page_table +
377                 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
378 #ifdef DEBUG_PCI_IOMMU
379         if (iopte_val(*base) == IOPTE_INVALID)
380                 printk("pci_unmap_single called on non-mapped region %08x,%08x from %016lx\n",
381                        bus_addr, sz, __builtin_return_address(0));
382 #endif
383         bus_addr &= IO_PAGE_MASK;
384
385         spin_lock_irqsave(&iommu->lock, flags);
386
387         /* Record the context, if any. */
388         ctx = 0;
389         if (iommu->iommu_ctxflush)
390                 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
391
392         /* Step 1: Kick data out of streaming buffers if necessary. */
393         if (strbuf->strbuf_enabled) {
394                 u32 vaddr = bus_addr;
395
396                 PCI_STC_FLUSHFLAG_INIT(strbuf);
397                 if (strbuf->strbuf_ctxflush &&
398                     iommu->iommu_ctxflush) {
399                         unsigned long matchreg, flushreg;
400
401                         flushreg = strbuf->strbuf_ctxflush;
402                         matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
403                         do {
404                                 pci_iommu_write(flushreg, ctx);
405                         } while(((long)pci_iommu_read(matchreg)) < 0L);
406                 } else {
407                         for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
408                                 pci_iommu_write(strbuf->strbuf_pflush, vaddr);
409                 }
410
411                 pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
412                 (void) pci_iommu_read(iommu->write_complete_reg);
413                 while (!PCI_STC_FLUSHFLAG_SET(strbuf))
414                         membar("#LoadLoad");
415         }
416
417         /* Step 2: Clear out first TSB entry. */
418         iopte_val(*base) = IOPTE_INVALID;
419
420         free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base,
421                                npages, ctx);
422
423         spin_unlock_irqrestore(&iommu->lock, flags);
424 }
425
426 #define SG_ENT_PHYS_ADDRESS(SG) \
427         (__pa(page_address((SG)->page)) + (SG)->offset)
428
429 static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
430                            int nused, int nelems, unsigned long iopte_protection)
431 {
432         struct scatterlist *dma_sg = sg;
433         struct scatterlist *sg_end = sg + nelems;
434         int i;
435
436         for (i = 0; i < nused; i++) {
437                 unsigned long pteval = ~0UL;
438                 u32 dma_npages;
439
440                 dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
441                               dma_sg->dma_length +
442                               ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
443                 do {
444                         unsigned long offset;
445                         signed int len;
446
447                         /* If we are here, we know we have at least one
448                          * more page to map.  So walk forward until we
449                          * hit a page crossing, and begin creating new
450                          * mappings from that spot.
451                          */
452                         for (;;) {
453                                 unsigned long tmp;
454
455                                 tmp = SG_ENT_PHYS_ADDRESS(sg);
456                                 len = sg->length;
457                                 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
458                                         pteval = tmp & IO_PAGE_MASK;
459                                         offset = tmp & (IO_PAGE_SIZE - 1UL);
460                                         break;
461                                 }
462                                 if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
463                                         pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
464                                         offset = 0UL;
465                                         len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
466                                         break;
467                                 }
468                                 sg++;
469                         }
470
471                         pteval = iopte_protection | (pteval & IOPTE_PAGE);
472                         while (len > 0) {
473                                 *iopte++ = __iopte(pteval);
474                                 pteval += IO_PAGE_SIZE;
475                                 len -= (IO_PAGE_SIZE - offset);
476                                 offset = 0;
477                                 dma_npages--;
478                         }
479
480                         pteval = (pteval & IOPTE_PAGE) + len;
481                         sg++;
482
483                         /* Skip over any tail mappings we've fully mapped,
484                          * adjusting pteval along the way.  Stop when we
485                          * detect a page crossing event.
486                          */
487                         while (sg < sg_end &&
488                                (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
489                                (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
490                                ((pteval ^
491                                  (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
492                                 pteval += sg->length;
493                                 sg++;
494                         }
495                         if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
496                                 pteval = ~0UL;
497                 } while (dma_npages != 0);
498                 dma_sg++;
499         }
500 }
501
502 /* Map a set of buffers described by SGLIST with NELEMS array
503  * elements in streaming mode for PCI DMA.
504  * When making changes here, inspect the assembly output. I was having
505  * hard time to kepp this routine out of using stack slots for holding variables.
506  */
507 int pci_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
508 {
509         struct pcidev_cookie *pcp;
510         struct pci_iommu *iommu;
511         struct pci_strbuf *strbuf;
512         unsigned long flags, ctx, npages, iopte_protection;
513         iopte_t *base;
514         u32 dma_base;
515         struct scatterlist *sgtmp;
516         int used;
517
518         /* Fast path single entry scatterlists. */
519         if (nelems == 1) {
520                 sglist->dma_address =
521                         pci_map_single(pdev,
522                                        (page_address(sglist->page) + sglist->offset),
523                                        sglist->length, direction);
524                 sglist->dma_length = sglist->length;
525                 return 1;
526         }
527
528         pcp = pdev->sysdata;
529         iommu = pcp->pbm->iommu;
530         strbuf = &pcp->pbm->stc;
531         
532         if (direction == PCI_DMA_NONE)
533                 BUG();
534
535         /* Step 1: Prepare scatter list. */
536
537         npages = prepare_sg(sglist, nelems);
538
539         /* Step 2: Allocate a cluster. */
540
541         spin_lock_irqsave(&iommu->lock, flags);
542
543         base = alloc_streaming_cluster(iommu, npages);
544         if (base == NULL)
545                 goto bad;
546         dma_base = iommu->page_table_map_base + ((base - iommu->page_table) << IO_PAGE_SHIFT);
547
548         /* Step 3: Normalize DMA addresses. */
549         used = nelems;
550
551         sgtmp = sglist;
552         while (used && sgtmp->dma_length) {
553                 sgtmp->dma_address += dma_base;
554                 sgtmp++;
555                 used--;
556         }
557         used = nelems - used;
558
559         /* Step 4: Choose a context if necessary. */
560         ctx = 0;
561         if (iommu->iommu_ctxflush)
562                 ctx = iommu->iommu_cur_ctx++;
563
564         /* Step 5: Create the mappings. */
565         if (strbuf->strbuf_enabled)
566                 iopte_protection = IOPTE_STREAMING(ctx);
567         else
568                 iopte_protection = IOPTE_CONSISTENT(ctx);
569         if (direction != PCI_DMA_TODEVICE)
570                 iopte_protection |= IOPTE_WRITE;
571         fill_sg (base, sglist, used, nelems, iopte_protection);
572 #ifdef VERIFY_SG
573         verify_sglist(sglist, nelems, base, npages);
574 #endif
575
576         spin_unlock_irqrestore(&iommu->lock, flags);
577
578         return used;
579
580 bad:
581         spin_unlock_irqrestore(&iommu->lock, flags);
582         return PCI_DMA_ERROR_CODE;
583 }
584
585 /* Unmap a set of streaming mode DMA translations. */
586 void pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
587 {
588         struct pcidev_cookie *pcp;
589         struct pci_iommu *iommu;
590         struct pci_strbuf *strbuf;
591         iopte_t *base;
592         unsigned long flags, ctx, i, npages;
593         u32 bus_addr;
594
595         if (direction == PCI_DMA_NONE)
596                 BUG();
597
598         pcp = pdev->sysdata;
599         iommu = pcp->pbm->iommu;
600         strbuf = &pcp->pbm->stc;
601         
602         bus_addr = sglist->dma_address & IO_PAGE_MASK;
603
604         for (i = 1; i < nelems; i++)
605                 if (sglist[i].dma_length == 0)
606                         break;
607         i--;
608         npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) - bus_addr) >> IO_PAGE_SHIFT;
609
610         base = iommu->page_table +
611                 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
612
613 #ifdef DEBUG_PCI_IOMMU
614         if (iopte_val(*base) == IOPTE_INVALID)
615                 printk("pci_unmap_sg called on non-mapped region %016lx,%d from %016lx\n", sglist->dma_address, nelems, __builtin_return_address(0));
616 #endif
617
618         spin_lock_irqsave(&iommu->lock, flags);
619
620         /* Record the context, if any. */
621         ctx = 0;
622         if (iommu->iommu_ctxflush)
623                 ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
624
625         /* Step 1: Kick data out of streaming buffers if necessary. */
626         if (strbuf->strbuf_enabled) {
627                 u32 vaddr = (u32) bus_addr;
628
629                 PCI_STC_FLUSHFLAG_INIT(strbuf);
630                 if (strbuf->strbuf_ctxflush &&
631                     iommu->iommu_ctxflush) {
632                         unsigned long matchreg, flushreg;
633
634                         flushreg = strbuf->strbuf_ctxflush;
635                         matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
636                         do {
637                                 pci_iommu_write(flushreg, ctx);
638                         } while(((long)pci_iommu_read(matchreg)) < 0L);
639                 } else {
640                         for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
641                                 pci_iommu_write(strbuf->strbuf_pflush, vaddr);
642                 }
643
644                 pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
645                 (void) pci_iommu_read(iommu->write_complete_reg);
646                 while (!PCI_STC_FLUSHFLAG_SET(strbuf))
647                         membar("#LoadLoad");
648         }
649
650         /* Step 2: Clear out first TSB entry. */
651         iopte_val(*base) = IOPTE_INVALID;
652
653         free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base,
654                                npages, ctx);
655
656         spin_unlock_irqrestore(&iommu->lock, flags);
657 }
658
659 /* Make physical memory consistent for a single
660  * streaming mode DMA translation after a transfer.
661  */
662 void pci_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
663 {
664         struct pcidev_cookie *pcp;
665         struct pci_iommu *iommu;
666         struct pci_strbuf *strbuf;
667         unsigned long flags, ctx, npages;
668
669         pcp = pdev->sysdata;
670         iommu = pcp->pbm->iommu;
671         strbuf = &pcp->pbm->stc;
672
673         if (!strbuf->strbuf_enabled)
674                 return;
675
676         spin_lock_irqsave(&iommu->lock, flags);
677
678         npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
679         npages >>= IO_PAGE_SHIFT;
680         bus_addr &= IO_PAGE_MASK;
681
682         /* Step 1: Record the context, if any. */
683         ctx = 0;
684         if (iommu->iommu_ctxflush &&
685             strbuf->strbuf_ctxflush) {
686                 iopte_t *iopte;
687
688                 iopte = iommu->page_table +
689                         ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
690                 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
691         }
692
693         /* Step 2: Kick data out of streaming buffers. */
694         PCI_STC_FLUSHFLAG_INIT(strbuf);
695         if (iommu->iommu_ctxflush &&
696             strbuf->strbuf_ctxflush) {
697                 unsigned long matchreg, flushreg;
698
699                 flushreg = strbuf->strbuf_ctxflush;
700                 matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
701                 do {
702                         pci_iommu_write(flushreg, ctx);
703                 } while(((long)pci_iommu_read(matchreg)) < 0L);
704         } else {
705                 unsigned long i;
706
707                 for (i = 0; i < npages; i++, bus_addr += IO_PAGE_SIZE)
708                         pci_iommu_write(strbuf->strbuf_pflush, bus_addr);
709         }
710
711         /* Step 3: Perform flush synchronization sequence. */
712         pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
713         (void) pci_iommu_read(iommu->write_complete_reg);
714         while (!PCI_STC_FLUSHFLAG_SET(strbuf))
715                 membar("#LoadLoad");
716
717         spin_unlock_irqrestore(&iommu->lock, flags);
718 }
719
720 /* Make physical memory consistent for a set of streaming
721  * mode DMA translations after a transfer.
722  */
723 void pci_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
724 {
725         struct pcidev_cookie *pcp;
726         struct pci_iommu *iommu;
727         struct pci_strbuf *strbuf;
728         unsigned long flags, ctx;
729
730         pcp = pdev->sysdata;
731         iommu = pcp->pbm->iommu;
732         strbuf = &pcp->pbm->stc;
733
734         if (!strbuf->strbuf_enabled)
735                 return;
736
737         spin_lock_irqsave(&iommu->lock, flags);
738
739         /* Step 1: Record the context, if any. */
740         ctx = 0;
741         if (iommu->iommu_ctxflush &&
742             strbuf->strbuf_ctxflush) {
743                 iopte_t *iopte;
744
745                 iopte = iommu->page_table +
746                         ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
747                 ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
748         }
749
750         /* Step 2: Kick data out of streaming buffers. */
751         PCI_STC_FLUSHFLAG_INIT(strbuf);
752         if (iommu->iommu_ctxflush &&
753             strbuf->strbuf_ctxflush) {
754                 unsigned long matchreg, flushreg;
755
756                 flushreg = strbuf->strbuf_ctxflush;
757                 matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
758                 do {
759                         pci_iommu_write(flushreg, ctx);
760                 } while (((long)pci_iommu_read(matchreg)) < 0L);
761         } else {
762                 unsigned long i, npages;
763                 u32 bus_addr;
764
765                 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
766
767                 for(i = 1; i < nelems; i++)
768                         if (!sglist[i].dma_length)
769                                 break;
770                 i--;
771                 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) - bus_addr) >> IO_PAGE_SHIFT;
772                 for (i = 0; i < npages; i++, bus_addr += IO_PAGE_SIZE)
773                         pci_iommu_write(strbuf->strbuf_pflush, bus_addr);
774         }
775
776         /* Step 3: Perform flush synchronization sequence. */
777         pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
778         (void) pci_iommu_read(iommu->write_complete_reg);
779         while (!PCI_STC_FLUSHFLAG_SET(strbuf))
780                 membar("#LoadLoad");
781
782         spin_unlock_irqrestore(&iommu->lock, flags);
783 }
784
785 static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
786 {
787         struct pci_dev *ali_isa_bridge;
788         u8 val;
789
790         /* ALI sound chips generate 31-bits of DMA, a special register
791          * determines what bit 31 is emitted as.
792          */
793         ali_isa_bridge = pci_find_device(PCI_VENDOR_ID_AL,
794                                          PCI_DEVICE_ID_AL_M1533,
795                                          NULL);
796
797         pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
798         if (set_bit)
799                 val |= 0x01;
800         else
801                 val &= ~0x01;
802         pci_write_config_byte(ali_isa_bridge, 0x7e, val);
803 }
804
805 int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
806 {
807         struct pcidev_cookie *pcp = pdev->sysdata;
808         u64 dma_addr_mask;
809
810         if (pdev == NULL) {
811                 dma_addr_mask = 0xffffffff;
812         } else {
813                 struct pci_iommu *iommu = pcp->pbm->iommu;
814
815                 dma_addr_mask = iommu->dma_addr_mask;
816
817                 if (pdev->vendor == PCI_VENDOR_ID_AL &&
818                     pdev->device == PCI_DEVICE_ID_AL_M5451 &&
819                     device_mask == 0x7fffffff) {
820                         ali_sound_dma_hack(pdev,
821                                            (dma_addr_mask & 0x80000000) != 0);
822                         return 1;
823                 }
824         }
825
826         if (device_mask >= (1UL << 32UL))
827                 return 0;
828
829         return (device_mask & dma_addr_mask) == dma_addr_mask;
830 }