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