alpha: remove unused DEBUG_FORCEDAC define in IOMMU
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / alpha / kernel / pci_iommu.c
blobbe6fa105cd34214d11c8b5bbf11dbc305f7ddae3
1 /*
2 * linux/arch/alpha/kernel/pci_iommu.c
3 */
5 #include <linux/kernel.h>
6 #include <linux/mm.h>
7 #include <linux/pci.h>
8 #include <linux/slab.h>
9 #include <linux/bootmem.h>
10 #include <linux/scatterlist.h>
11 #include <linux/log2.h>
12 #include <linux/dma-mapping.h>
14 #include <asm/io.h>
15 #include <asm/hwrpb.h>
17 #include "proto.h"
18 #include "pci_impl.h"
21 #define DEBUG_ALLOC 0
22 #if DEBUG_ALLOC > 0
23 # define DBGA(args...) printk(KERN_DEBUG args)
24 #else
25 # define DBGA(args...)
26 #endif
27 #if DEBUG_ALLOC > 1
28 # define DBGA2(args...) printk(KERN_DEBUG args)
29 #else
30 # define DBGA2(args...)
31 #endif
33 #define DEBUG_NODIRECT 0
35 #define ISA_DMA_MASK 0x00ffffff
37 static inline unsigned long
38 mk_iommu_pte(unsigned long paddr)
40 return (paddr >> (PAGE_SHIFT-1)) | 1;
43 static inline long
44 calc_npages(long bytes)
46 return (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
50 /* Return the minimum of MAX or the first power of two larger
51 than main memory. */
53 unsigned long
54 size_for_memory(unsigned long max)
56 unsigned long mem = max_low_pfn << PAGE_SHIFT;
57 if (mem < max)
58 max = roundup_pow_of_two(mem);
59 return max;
62 struct pci_iommu_arena * __init
63 iommu_arena_new_node(int nid, struct pci_controller *hose, dma_addr_t base,
64 unsigned long window_size, unsigned long align)
66 unsigned long mem_size;
67 struct pci_iommu_arena *arena;
69 mem_size = window_size / (PAGE_SIZE / sizeof(unsigned long));
71 /* Note that the TLB lookup logic uses bitwise concatenation,
72 not addition, so the required arena alignment is based on
73 the size of the window. Retain the align parameter so that
74 particular systems can over-align the arena. */
75 if (align < mem_size)
76 align = mem_size;
79 #ifdef CONFIG_DISCONTIGMEM
81 if (!NODE_DATA(nid) ||
82 (NULL == (arena = alloc_bootmem_node(NODE_DATA(nid),
83 sizeof(*arena))))) {
84 printk("%s: couldn't allocate arena from node %d\n"
85 " falling back to system-wide allocation\n",
86 __FUNCTION__, nid);
87 arena = alloc_bootmem(sizeof(*arena));
90 if (!NODE_DATA(nid) ||
91 (NULL == (arena->ptes = __alloc_bootmem_node(NODE_DATA(nid),
92 mem_size,
93 align,
94 0)))) {
95 printk("%s: couldn't allocate arena ptes from node %d\n"
96 " falling back to system-wide allocation\n",
97 __FUNCTION__, nid);
98 arena->ptes = __alloc_bootmem(mem_size, align, 0);
101 #else /* CONFIG_DISCONTIGMEM */
103 arena = alloc_bootmem(sizeof(*arena));
104 arena->ptes = __alloc_bootmem(mem_size, align, 0);
106 #endif /* CONFIG_DISCONTIGMEM */
108 spin_lock_init(&arena->lock);
109 arena->hose = hose;
110 arena->dma_base = base;
111 arena->size = window_size;
112 arena->next_entry = 0;
114 /* Align allocations to a multiple of a page size. Not needed
115 unless there are chip bugs. */
116 arena->align_entry = 1;
118 return arena;
121 struct pci_iommu_arena * __init
122 iommu_arena_new(struct pci_controller *hose, dma_addr_t base,
123 unsigned long window_size, unsigned long align)
125 return iommu_arena_new_node(0, hose, base, window_size, align);
128 static inline int is_span_boundary(unsigned int index, unsigned int nr,
129 unsigned long shift,
130 unsigned long boundary_size)
132 shift = (shift + index) & (boundary_size - 1);
133 return shift + nr > boundary_size;
136 /* Must be called with the arena lock held */
137 static long
138 iommu_arena_find_pages(struct device *dev, struct pci_iommu_arena *arena,
139 long n, long mask)
141 unsigned long *ptes;
142 long i, p, nent;
143 int pass = 0;
144 unsigned long base;
145 unsigned long boundary_size;
147 BUG_ON(arena->dma_base & ~PAGE_MASK);
148 base = arena->dma_base >> PAGE_SHIFT;
149 if (dev)
150 boundary_size = ALIGN(dma_get_max_seg_size(dev) + 1, PAGE_SIZE)
151 >> PAGE_SHIFT;
152 else
153 boundary_size = ALIGN(1UL << 32, PAGE_SIZE) >> PAGE_SHIFT;
155 BUG_ON(!is_power_of_2(boundary_size));
157 /* Search forward for the first mask-aligned sequence of N free ptes */
158 ptes = arena->ptes;
159 nent = arena->size >> PAGE_SHIFT;
160 p = ALIGN(arena->next_entry, mask + 1);
161 i = 0;
163 again:
164 while (i < n && p+i < nent) {
165 if (!i && is_span_boundary(p, n, base, boundary_size)) {
166 p = ALIGN(p + 1, mask + 1);
167 goto again;
170 if (ptes[p+i])
171 p = ALIGN(p + i + 1, mask + 1), i = 0;
172 else
173 i = i + 1;
176 if (i < n) {
177 if (pass < 1) {
179 * Reached the end. Flush the TLB and restart
180 * the search from the beginning.
182 alpha_mv.mv_pci_tbi(arena->hose, 0, -1);
184 pass++;
185 p = 0;
186 i = 0;
187 goto again;
188 } else
189 return -1;
192 /* Success. It's the responsibility of the caller to mark them
193 in use before releasing the lock */
194 return p;
197 static long
198 iommu_arena_alloc(struct device *dev, struct pci_iommu_arena *arena, long n,
199 unsigned int align)
201 unsigned long flags;
202 unsigned long *ptes;
203 long i, p, mask;
205 spin_lock_irqsave(&arena->lock, flags);
207 /* Search for N empty ptes */
208 ptes = arena->ptes;
209 mask = max(align, arena->align_entry) - 1;
210 p = iommu_arena_find_pages(dev, arena, n, mask);
211 if (p < 0) {
212 spin_unlock_irqrestore(&arena->lock, flags);
213 return -1;
216 /* Success. Mark them all in use, ie not zero and invalid
217 for the iommu tlb that could load them from under us.
218 The chip specific bits will fill this in with something
219 kosher when we return. */
220 for (i = 0; i < n; ++i)
221 ptes[p+i] = IOMMU_INVALID_PTE;
223 arena->next_entry = p + n;
224 spin_unlock_irqrestore(&arena->lock, flags);
226 return p;
229 static void
230 iommu_arena_free(struct pci_iommu_arena *arena, long ofs, long n)
232 unsigned long *p;
233 long i;
235 p = arena->ptes + ofs;
236 for (i = 0; i < n; ++i)
237 p[i] = 0;
240 /* True if the machine supports DAC addressing, and DEV can
241 make use of it given MASK. */
242 static int pci_dac_dma_supported(struct pci_dev *hwdev, u64 mask);
244 /* Map a single buffer of the indicated size for PCI DMA in streaming
245 mode. The 32-bit PCI bus mastering address to use is returned.
246 Once the device is given the dma address, the device owns this memory
247 until either pci_unmap_single or pci_dma_sync_single is performed. */
249 static dma_addr_t
250 pci_map_single_1(struct pci_dev *pdev, void *cpu_addr, size_t size,
251 int dac_allowed)
253 struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose;
254 dma_addr_t max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
255 struct pci_iommu_arena *arena;
256 long npages, dma_ofs, i;
257 unsigned long paddr;
258 dma_addr_t ret;
259 unsigned int align = 0;
260 struct device *dev = pdev ? &pdev->dev : NULL;
262 paddr = __pa(cpu_addr);
264 #if !DEBUG_NODIRECT
265 /* First check to see if we can use the direct map window. */
266 if (paddr + size + __direct_map_base - 1 <= max_dma
267 && paddr + size <= __direct_map_size) {
268 ret = paddr + __direct_map_base;
270 DBGA2("pci_map_single: [%p,%lx] -> direct %lx from %p\n",
271 cpu_addr, size, ret, __builtin_return_address(0));
273 return ret;
275 #endif
277 /* Next, use DAC if selected earlier. */
278 if (dac_allowed) {
279 ret = paddr + alpha_mv.pci_dac_offset;
281 DBGA2("pci_map_single: [%p,%lx] -> DAC %lx from %p\n",
282 cpu_addr, size, ret, __builtin_return_address(0));
284 return ret;
287 /* If the machine doesn't define a pci_tbi routine, we have to
288 assume it doesn't support sg mapping, and, since we tried to
289 use direct_map above, it now must be considered an error. */
290 if (! alpha_mv.mv_pci_tbi) {
291 static int been_here = 0; /* Only print the message once. */
292 if (!been_here) {
293 printk(KERN_WARNING "pci_map_single: no HW sg\n");
294 been_here = 1;
296 return 0;
299 arena = hose->sg_pci;
300 if (!arena || arena->dma_base + arena->size - 1 > max_dma)
301 arena = hose->sg_isa;
303 npages = calc_npages((paddr & ~PAGE_MASK) + size);
305 /* Force allocation to 64KB boundary for ISA bridges. */
306 if (pdev && pdev == isa_bridge)
307 align = 8;
308 dma_ofs = iommu_arena_alloc(dev, arena, npages, align);
309 if (dma_ofs < 0) {
310 printk(KERN_WARNING "pci_map_single failed: "
311 "could not allocate dma page tables\n");
312 return 0;
315 paddr &= PAGE_MASK;
316 for (i = 0; i < npages; ++i, paddr += PAGE_SIZE)
317 arena->ptes[i + dma_ofs] = mk_iommu_pte(paddr);
319 ret = arena->dma_base + dma_ofs * PAGE_SIZE;
320 ret += (unsigned long)cpu_addr & ~PAGE_MASK;
322 DBGA2("pci_map_single: [%p,%lx] np %ld -> sg %lx from %p\n",
323 cpu_addr, size, npages, ret, __builtin_return_address(0));
325 return ret;
328 dma_addr_t
329 pci_map_single(struct pci_dev *pdev, void *cpu_addr, size_t size, int dir)
331 int dac_allowed;
333 if (dir == PCI_DMA_NONE)
334 BUG();
336 dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
337 return pci_map_single_1(pdev, cpu_addr, size, dac_allowed);
339 EXPORT_SYMBOL(pci_map_single);
341 dma_addr_t
342 pci_map_page(struct pci_dev *pdev, struct page *page, unsigned long offset,
343 size_t size, int dir)
345 int dac_allowed;
347 if (dir == PCI_DMA_NONE)
348 BUG();
350 dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
351 return pci_map_single_1(pdev, (char *)page_address(page) + offset,
352 size, dac_allowed);
354 EXPORT_SYMBOL(pci_map_page);
356 /* Unmap a single streaming mode DMA translation. The DMA_ADDR and
357 SIZE must match what was provided for in a previous pci_map_single
358 call. All other usages are undefined. After this call, reads by
359 the cpu to the buffer are guaranteed to see whatever the device
360 wrote there. */
362 void
363 pci_unmap_single(struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
364 int direction)
366 unsigned long flags;
367 struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose;
368 struct pci_iommu_arena *arena;
369 long dma_ofs, npages;
371 if (direction == PCI_DMA_NONE)
372 BUG();
374 if (dma_addr >= __direct_map_base
375 && dma_addr < __direct_map_base + __direct_map_size) {
376 /* Nothing to do. */
378 DBGA2("pci_unmap_single: direct [%lx,%lx] from %p\n",
379 dma_addr, size, __builtin_return_address(0));
381 return;
384 if (dma_addr > 0xffffffff) {
385 DBGA2("pci64_unmap_single: DAC [%lx,%lx] from %p\n",
386 dma_addr, size, __builtin_return_address(0));
387 return;
390 arena = hose->sg_pci;
391 if (!arena || dma_addr < arena->dma_base)
392 arena = hose->sg_isa;
394 dma_ofs = (dma_addr - arena->dma_base) >> PAGE_SHIFT;
395 if (dma_ofs * PAGE_SIZE >= arena->size) {
396 printk(KERN_ERR "Bogus pci_unmap_single: dma_addr %lx "
397 " base %lx size %x\n", dma_addr, arena->dma_base,
398 arena->size);
399 return;
400 BUG();
403 npages = calc_npages((dma_addr & ~PAGE_MASK) + size);
405 spin_lock_irqsave(&arena->lock, flags);
407 iommu_arena_free(arena, dma_ofs, npages);
409 /* If we're freeing ptes above the `next_entry' pointer (they
410 may have snuck back into the TLB since the last wrap flush),
411 we need to flush the TLB before reallocating the latter. */
412 if (dma_ofs >= arena->next_entry)
413 alpha_mv.mv_pci_tbi(hose, dma_addr, dma_addr + size - 1);
415 spin_unlock_irqrestore(&arena->lock, flags);
417 DBGA2("pci_unmap_single: sg [%lx,%lx] np %ld from %p\n",
418 dma_addr, size, npages, __builtin_return_address(0));
420 EXPORT_SYMBOL(pci_unmap_single);
422 void
423 pci_unmap_page(struct pci_dev *pdev, dma_addr_t dma_addr,
424 size_t size, int direction)
426 pci_unmap_single(pdev, dma_addr, size, direction);
428 EXPORT_SYMBOL(pci_unmap_page);
430 /* Allocate and map kernel buffer using consistent mode DMA for PCI
431 device. Returns non-NULL cpu-view pointer to the buffer if
432 successful and sets *DMA_ADDRP to the pci side dma address as well,
433 else DMA_ADDRP is undefined. */
435 void *
436 pci_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp)
438 void *cpu_addr;
439 long order = get_order(size);
440 gfp_t gfp = GFP_ATOMIC;
442 try_again:
443 cpu_addr = (void *)__get_free_pages(gfp, order);
444 if (! cpu_addr) {
445 printk(KERN_INFO "pci_alloc_consistent: "
446 "get_free_pages failed from %p\n",
447 __builtin_return_address(0));
448 /* ??? Really atomic allocation? Otherwise we could play
449 with vmalloc and sg if we can't find contiguous memory. */
450 return NULL;
452 memset(cpu_addr, 0, size);
454 *dma_addrp = pci_map_single_1(pdev, cpu_addr, size, 0);
455 if (*dma_addrp == 0) {
456 free_pages((unsigned long)cpu_addr, order);
457 if (alpha_mv.mv_pci_tbi || (gfp & GFP_DMA))
458 return NULL;
459 /* The address doesn't fit required mask and we
460 do not have iommu. Try again with GFP_DMA. */
461 gfp |= GFP_DMA;
462 goto try_again;
465 DBGA2("pci_alloc_consistent: %lx -> [%p,%x] from %p\n",
466 size, cpu_addr, *dma_addrp, __builtin_return_address(0));
468 return cpu_addr;
470 EXPORT_SYMBOL(pci_alloc_consistent);
472 /* Free and unmap a consistent DMA buffer. CPU_ADDR and DMA_ADDR must
473 be values that were returned from pci_alloc_consistent. SIZE must
474 be the same as what as passed into pci_alloc_consistent.
475 References to the memory and mappings associated with CPU_ADDR or
476 DMA_ADDR past this call are illegal. */
478 void
479 pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu_addr,
480 dma_addr_t dma_addr)
482 pci_unmap_single(pdev, dma_addr, size, PCI_DMA_BIDIRECTIONAL);
483 free_pages((unsigned long)cpu_addr, get_order(size));
485 DBGA2("pci_free_consistent: [%x,%lx] from %p\n",
486 dma_addr, size, __builtin_return_address(0));
488 EXPORT_SYMBOL(pci_free_consistent);
490 /* Classify the elements of the scatterlist. Write dma_address
491 of each element with:
492 0 : Followers all physically adjacent.
493 1 : Followers all virtually adjacent.
494 -1 : Not leader, physically adjacent to previous.
495 -2 : Not leader, virtually adjacent to previous.
496 Write dma_length of each leader with the combined lengths of
497 the mergable followers. */
499 #define SG_ENT_VIRT_ADDRESS(SG) (sg_virt((SG)))
500 #define SG_ENT_PHYS_ADDRESS(SG) __pa(SG_ENT_VIRT_ADDRESS(SG))
502 static void
503 sg_classify(struct device *dev, struct scatterlist *sg, struct scatterlist *end,
504 int virt_ok)
506 unsigned long next_paddr;
507 struct scatterlist *leader;
508 long leader_flag, leader_length;
509 unsigned int max_seg_size;
511 leader = sg;
512 leader_flag = 0;
513 leader_length = leader->length;
514 next_paddr = SG_ENT_PHYS_ADDRESS(leader) + leader_length;
516 /* we will not marge sg without device. */
517 max_seg_size = dev ? dma_get_max_seg_size(dev) : 0;
518 for (++sg; sg < end; ++sg) {
519 unsigned long addr, len;
520 addr = SG_ENT_PHYS_ADDRESS(sg);
521 len = sg->length;
523 if (leader_length + len > max_seg_size)
524 goto new_segment;
526 if (next_paddr == addr) {
527 sg->dma_address = -1;
528 leader_length += len;
529 } else if (((next_paddr | addr) & ~PAGE_MASK) == 0 && virt_ok) {
530 sg->dma_address = -2;
531 leader_flag = 1;
532 leader_length += len;
533 } else {
534 new_segment:
535 leader->dma_address = leader_flag;
536 leader->dma_length = leader_length;
537 leader = sg;
538 leader_flag = 0;
539 leader_length = len;
542 next_paddr = addr + len;
545 leader->dma_address = leader_flag;
546 leader->dma_length = leader_length;
549 /* Given a scatterlist leader, choose an allocation method and fill
550 in the blanks. */
552 static int
553 sg_fill(struct device *dev, struct scatterlist *leader, struct scatterlist *end,
554 struct scatterlist *out, struct pci_iommu_arena *arena,
555 dma_addr_t max_dma, int dac_allowed)
557 unsigned long paddr = SG_ENT_PHYS_ADDRESS(leader);
558 long size = leader->dma_length;
559 struct scatterlist *sg;
560 unsigned long *ptes;
561 long npages, dma_ofs, i;
563 #if !DEBUG_NODIRECT
564 /* If everything is physically contiguous, and the addresses
565 fall into the direct-map window, use it. */
566 if (leader->dma_address == 0
567 && paddr + size + __direct_map_base - 1 <= max_dma
568 && paddr + size <= __direct_map_size) {
569 out->dma_address = paddr + __direct_map_base;
570 out->dma_length = size;
572 DBGA(" sg_fill: [%p,%lx] -> direct %lx\n",
573 __va(paddr), size, out->dma_address);
575 return 0;
577 #endif
579 /* If physically contiguous and DAC is available, use it. */
580 if (leader->dma_address == 0 && dac_allowed) {
581 out->dma_address = paddr + alpha_mv.pci_dac_offset;
582 out->dma_length = size;
584 DBGA(" sg_fill: [%p,%lx] -> DAC %lx\n",
585 __va(paddr), size, out->dma_address);
587 return 0;
590 /* Otherwise, we'll use the iommu to make the pages virtually
591 contiguous. */
593 paddr &= ~PAGE_MASK;
594 npages = calc_npages(paddr + size);
595 dma_ofs = iommu_arena_alloc(dev, arena, npages, 0);
596 if (dma_ofs < 0) {
597 /* If we attempted a direct map above but failed, die. */
598 if (leader->dma_address == 0)
599 return -1;
601 /* Otherwise, break up the remaining virtually contiguous
602 hunks into individual direct maps and retry. */
603 sg_classify(dev, leader, end, 0);
604 return sg_fill(dev, leader, end, out, arena, max_dma, dac_allowed);
607 out->dma_address = arena->dma_base + dma_ofs*PAGE_SIZE + paddr;
608 out->dma_length = size;
610 DBGA(" sg_fill: [%p,%lx] -> sg %lx np %ld\n",
611 __va(paddr), size, out->dma_address, npages);
613 /* All virtually contiguous. We need to find the length of each
614 physically contiguous subsegment to fill in the ptes. */
615 ptes = &arena->ptes[dma_ofs];
616 sg = leader;
617 do {
618 #if DEBUG_ALLOC > 0
619 struct scatterlist *last_sg = sg;
620 #endif
622 size = sg->length;
623 paddr = SG_ENT_PHYS_ADDRESS(sg);
625 while (sg+1 < end && (int) sg[1].dma_address == -1) {
626 size += sg[1].length;
627 sg++;
630 npages = calc_npages((paddr & ~PAGE_MASK) + size);
632 paddr &= PAGE_MASK;
633 for (i = 0; i < npages; ++i, paddr += PAGE_SIZE)
634 *ptes++ = mk_iommu_pte(paddr);
636 #if DEBUG_ALLOC > 0
637 DBGA(" (%ld) [%p,%x] np %ld\n",
638 last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg),
639 last_sg->length, npages);
640 while (++last_sg <= sg) {
641 DBGA(" (%ld) [%p,%x] cont\n",
642 last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg),
643 last_sg->length);
645 #endif
646 } while (++sg < end && (int) sg->dma_address < 0);
648 return 1;
652 pci_map_sg(struct pci_dev *pdev, struct scatterlist *sg, int nents,
653 int direction)
655 struct scatterlist *start, *end, *out;
656 struct pci_controller *hose;
657 struct pci_iommu_arena *arena;
658 dma_addr_t max_dma;
659 int dac_allowed;
660 struct device *dev;
662 if (direction == PCI_DMA_NONE)
663 BUG();
665 dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
667 dev = pdev ? &pdev->dev : NULL;
669 /* Fast path single entry scatterlists. */
670 if (nents == 1) {
671 sg->dma_length = sg->length;
672 sg->dma_address
673 = pci_map_single_1(pdev, SG_ENT_VIRT_ADDRESS(sg),
674 sg->length, dac_allowed);
675 return sg->dma_address != 0;
678 start = sg;
679 end = sg + nents;
681 /* First, prepare information about the entries. */
682 sg_classify(dev, sg, end, alpha_mv.mv_pci_tbi != 0);
684 /* Second, figure out where we're going to map things. */
685 if (alpha_mv.mv_pci_tbi) {
686 hose = pdev ? pdev->sysdata : pci_isa_hose;
687 max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
688 arena = hose->sg_pci;
689 if (!arena || arena->dma_base + arena->size - 1 > max_dma)
690 arena = hose->sg_isa;
691 } else {
692 max_dma = -1;
693 arena = NULL;
694 hose = NULL;
697 /* Third, iterate over the scatterlist leaders and allocate
698 dma space as needed. */
699 for (out = sg; sg < end; ++sg) {
700 if ((int) sg->dma_address < 0)
701 continue;
702 if (sg_fill(dev, sg, end, out, arena, max_dma, dac_allowed) < 0)
703 goto error;
704 out++;
707 /* Mark the end of the list for pci_unmap_sg. */
708 if (out < end)
709 out->dma_length = 0;
711 if (out - start == 0)
712 printk(KERN_WARNING "pci_map_sg failed: no entries?\n");
713 DBGA("pci_map_sg: %ld entries\n", out - start);
715 return out - start;
717 error:
718 printk(KERN_WARNING "pci_map_sg failed: "
719 "could not allocate dma page tables\n");
721 /* Some allocation failed while mapping the scatterlist
722 entries. Unmap them now. */
723 if (out > start)
724 pci_unmap_sg(pdev, start, out - start, direction);
725 return 0;
727 EXPORT_SYMBOL(pci_map_sg);
729 /* Unmap a set of streaming mode DMA translations. Again, cpu read
730 rules concerning calls here are the same as for pci_unmap_single()
731 above. */
733 void
734 pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sg, int nents,
735 int direction)
737 unsigned long flags;
738 struct pci_controller *hose;
739 struct pci_iommu_arena *arena;
740 struct scatterlist *end;
741 dma_addr_t max_dma;
742 dma_addr_t fbeg, fend;
744 if (direction == PCI_DMA_NONE)
745 BUG();
747 if (! alpha_mv.mv_pci_tbi)
748 return;
750 hose = pdev ? pdev->sysdata : pci_isa_hose;
751 max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
752 arena = hose->sg_pci;
753 if (!arena || arena->dma_base + arena->size - 1 > max_dma)
754 arena = hose->sg_isa;
756 fbeg = -1, fend = 0;
758 spin_lock_irqsave(&arena->lock, flags);
760 for (end = sg + nents; sg < end; ++sg) {
761 dma64_addr_t addr;
762 size_t size;
763 long npages, ofs;
764 dma_addr_t tend;
766 addr = sg->dma_address;
767 size = sg->dma_length;
768 if (!size)
769 break;
771 if (addr > 0xffffffff) {
772 /* It's a DAC address -- nothing to do. */
773 DBGA(" (%ld) DAC [%lx,%lx]\n",
774 sg - end + nents, addr, size);
775 continue;
778 if (addr >= __direct_map_base
779 && addr < __direct_map_base + __direct_map_size) {
780 /* Nothing to do. */
781 DBGA(" (%ld) direct [%lx,%lx]\n",
782 sg - end + nents, addr, size);
783 continue;
786 DBGA(" (%ld) sg [%lx,%lx]\n",
787 sg - end + nents, addr, size);
789 npages = calc_npages((addr & ~PAGE_MASK) + size);
790 ofs = (addr - arena->dma_base) >> PAGE_SHIFT;
791 iommu_arena_free(arena, ofs, npages);
793 tend = addr + size - 1;
794 if (fbeg > addr) fbeg = addr;
795 if (fend < tend) fend = tend;
798 /* If we're freeing ptes above the `next_entry' pointer (they
799 may have snuck back into the TLB since the last wrap flush),
800 we need to flush the TLB before reallocating the latter. */
801 if ((fend - arena->dma_base) >> PAGE_SHIFT >= arena->next_entry)
802 alpha_mv.mv_pci_tbi(hose, fbeg, fend);
804 spin_unlock_irqrestore(&arena->lock, flags);
806 DBGA("pci_unmap_sg: %ld entries\n", nents - (end - sg));
808 EXPORT_SYMBOL(pci_unmap_sg);
811 /* Return whether the given PCI device DMA address mask can be
812 supported properly. */
815 pci_dma_supported(struct pci_dev *pdev, u64 mask)
817 struct pci_controller *hose;
818 struct pci_iommu_arena *arena;
820 /* If there exists a direct map, and the mask fits either
821 the entire direct mapped space or the total system memory as
822 shifted by the map base */
823 if (__direct_map_size != 0
824 && (__direct_map_base + __direct_map_size - 1 <= mask ||
825 __direct_map_base + (max_low_pfn << PAGE_SHIFT) - 1 <= mask))
826 return 1;
828 /* Check that we have a scatter-gather arena that fits. */
829 hose = pdev ? pdev->sysdata : pci_isa_hose;
830 arena = hose->sg_isa;
831 if (arena && arena->dma_base + arena->size - 1 <= mask)
832 return 1;
833 arena = hose->sg_pci;
834 if (arena && arena->dma_base + arena->size - 1 <= mask)
835 return 1;
837 /* As last resort try ZONE_DMA. */
838 if (!__direct_map_base && MAX_DMA_ADDRESS - IDENT_ADDR - 1 <= mask)
839 return 1;
841 return 0;
843 EXPORT_SYMBOL(pci_dma_supported);
847 * AGP GART extensions to the IOMMU
850 iommu_reserve(struct pci_iommu_arena *arena, long pg_count, long align_mask)
852 unsigned long flags;
853 unsigned long *ptes;
854 long i, p;
856 if (!arena) return -EINVAL;
858 spin_lock_irqsave(&arena->lock, flags);
860 /* Search for N empty ptes. */
861 ptes = arena->ptes;
862 p = iommu_arena_find_pages(NULL, arena, pg_count, align_mask);
863 if (p < 0) {
864 spin_unlock_irqrestore(&arena->lock, flags);
865 return -1;
868 /* Success. Mark them all reserved (ie not zero and invalid)
869 for the iommu tlb that could load them from under us.
870 They will be filled in with valid bits by _bind() */
871 for (i = 0; i < pg_count; ++i)
872 ptes[p+i] = IOMMU_RESERVED_PTE;
874 arena->next_entry = p + pg_count;
875 spin_unlock_irqrestore(&arena->lock, flags);
877 return p;
880 int
881 iommu_release(struct pci_iommu_arena *arena, long pg_start, long pg_count)
883 unsigned long *ptes;
884 long i;
886 if (!arena) return -EINVAL;
888 ptes = arena->ptes;
890 /* Make sure they're all reserved first... */
891 for(i = pg_start; i < pg_start + pg_count; i++)
892 if (ptes[i] != IOMMU_RESERVED_PTE)
893 return -EBUSY;
895 iommu_arena_free(arena, pg_start, pg_count);
896 return 0;
900 iommu_bind(struct pci_iommu_arena *arena, long pg_start, long pg_count,
901 unsigned long *physaddrs)
903 unsigned long flags;
904 unsigned long *ptes;
905 long i, j;
907 if (!arena) return -EINVAL;
909 spin_lock_irqsave(&arena->lock, flags);
911 ptes = arena->ptes;
913 for(j = pg_start; j < pg_start + pg_count; j++) {
914 if (ptes[j] != IOMMU_RESERVED_PTE) {
915 spin_unlock_irqrestore(&arena->lock, flags);
916 return -EBUSY;
920 for(i = 0, j = pg_start; i < pg_count; i++, j++)
921 ptes[j] = mk_iommu_pte(physaddrs[i]);
923 spin_unlock_irqrestore(&arena->lock, flags);
925 return 0;
929 iommu_unbind(struct pci_iommu_arena *arena, long pg_start, long pg_count)
931 unsigned long *p;
932 long i;
934 if (!arena) return -EINVAL;
936 p = arena->ptes + pg_start;
937 for(i = 0; i < pg_count; i++)
938 p[i] = IOMMU_RESERVED_PTE;
940 return 0;
943 /* True if the machine supports DAC addressing, and DEV can
944 make use of it given MASK. */
946 static int
947 pci_dac_dma_supported(struct pci_dev *dev, u64 mask)
949 dma64_addr_t dac_offset = alpha_mv.pci_dac_offset;
950 int ok = 1;
952 /* If this is not set, the machine doesn't support DAC at all. */
953 if (dac_offset == 0)
954 ok = 0;
956 /* The device has to be able to address our DAC bit. */
957 if ((dac_offset & dev->dma_mask) != dac_offset)
958 ok = 0;
960 /* If both conditions above are met, we are fine. */
961 DBGA("pci_dac_dma_supported %s from %p\n",
962 ok ? "yes" : "no", __builtin_return_address(0));
964 return ok;
967 /* Helper for generic DMA-mapping functions. */
969 struct pci_dev *
970 alpha_gendev_to_pci(struct device *dev)
972 if (dev && dev->bus == &pci_bus_type)
973 return to_pci_dev(dev);
975 /* Assume that non-PCI devices asking for DMA are either ISA or EISA,
976 BUG() otherwise. */
977 BUG_ON(!isa_bridge);
979 /* Assume non-busmaster ISA DMA when dma_mask is not set (the ISA
980 bridge is bus master then). */
981 if (!dev || !dev->dma_mask || !*dev->dma_mask)
982 return isa_bridge;
984 /* For EISA bus masters, return isa_bridge (it might have smaller
985 dma_mask due to wiring limitations). */
986 if (*dev->dma_mask >= isa_bridge->dma_mask)
987 return isa_bridge;
989 /* This assumes ISA bus master with dma_mask 0xffffff. */
990 return NULL;
992 EXPORT_SYMBOL(alpha_gendev_to_pci);
995 dma_set_mask(struct device *dev, u64 mask)
997 if (!dev->dma_mask ||
998 !pci_dma_supported(alpha_gendev_to_pci(dev), mask))
999 return -EIO;
1001 *dev->dma_mask = mask;
1003 return 0;
1005 EXPORT_SYMBOL(dma_set_mask);