2 * linux/arch/alpha/kernel/pci_iommu.c
5 #include <linux/kernel.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>
15 #include <asm/hwrpb.h>
23 # define DBGA(args...) printk(KERN_DEBUG args)
25 # define DBGA(args...)
28 # define DBGA2(args...) printk(KERN_DEBUG args)
30 # define DBGA2(args...)
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;
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
54 size_for_memory(unsigned long max
)
56 unsigned long mem
= max_low_pfn
<< PAGE_SHIFT
;
58 max
= roundup_pow_of_two(mem
);
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. */
79 #ifdef CONFIG_DISCONTIGMEM
81 if (!NODE_DATA(nid
) ||
82 (NULL
== (arena
= alloc_bootmem_node(NODE_DATA(nid
),
84 printk("%s: couldn't allocate arena from node %d\n"
85 " falling back to system-wide allocation\n",
87 arena
= alloc_bootmem(sizeof(*arena
));
90 if (!NODE_DATA(nid
) ||
91 (NULL
== (arena
->ptes
= __alloc_bootmem_node(NODE_DATA(nid
),
95 printk("%s: couldn't allocate arena ptes from node %d\n"
96 " falling back to system-wide allocation\n",
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
);
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;
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
,
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 */
138 iommu_arena_find_pages(struct device
*dev
, struct pci_iommu_arena
*arena
,
145 unsigned long boundary_size
;
147 BUG_ON(arena
->dma_base
& ~PAGE_MASK
);
148 base
= arena
->dma_base
>> PAGE_SHIFT
;
150 boundary_size
= ALIGN(dma_get_max_seg_size(dev
) + 1, PAGE_SIZE
)
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 */
159 nent
= arena
->size
>> PAGE_SHIFT
;
160 p
= ALIGN(arena
->next_entry
, mask
+ 1);
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);
171 p
= ALIGN(p
+ i
+ 1, mask
+ 1), i
= 0;
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);
192 /* Success. It's the responsibility of the caller to mark them
193 in use before releasing the lock */
198 iommu_arena_alloc(struct device
*dev
, struct pci_iommu_arena
*arena
, long n
,
205 spin_lock_irqsave(&arena
->lock
, flags
);
207 /* Search for N empty ptes */
209 mask
= max(align
, arena
->align_entry
) - 1;
210 p
= iommu_arena_find_pages(dev
, arena
, n
, mask
);
212 spin_unlock_irqrestore(&arena
->lock
, flags
);
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
);
230 iommu_arena_free(struct pci_iommu_arena
*arena
, long ofs
, long n
)
235 p
= arena
->ptes
+ ofs
;
236 for (i
= 0; i
< n
; ++i
)
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. */
250 pci_map_single_1(struct pci_dev
*pdev
, void *cpu_addr
, size_t size
,
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
;
259 unsigned int align
= 0;
260 struct device
*dev
= pdev
? &pdev
->dev
: NULL
;
262 paddr
= __pa(cpu_addr
);
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));
277 /* Next, use DAC if selected earlier. */
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));
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. */
293 printk(KERN_WARNING
"pci_map_single: no HW sg\n");
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
)
308 dma_ofs
= iommu_arena_alloc(dev
, arena
, npages
, align
);
310 printk(KERN_WARNING
"pci_map_single failed: "
311 "could not allocate dma page tables\n");
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));
329 pci_map_single(struct pci_dev
*pdev
, void *cpu_addr
, size_t size
, int dir
)
333 if (dir
== PCI_DMA_NONE
)
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
);
342 pci_map_page(struct pci_dev
*pdev
, struct page
*page
, unsigned long offset
,
343 size_t size
, int dir
)
347 if (dir
== PCI_DMA_NONE
)
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
,
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
363 pci_unmap_single(struct pci_dev
*pdev
, dma_addr_t dma_addr
, size_t size
,
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
)
374 if (dma_addr
>= __direct_map_base
375 && dma_addr
< __direct_map_base
+ __direct_map_size
) {
378 DBGA2("pci_unmap_single: direct [%lx,%lx] from %p\n",
379 dma_addr
, size
, __builtin_return_address(0));
384 if (dma_addr
> 0xffffffff) {
385 DBGA2("pci64_unmap_single: DAC [%lx,%lx] from %p\n",
386 dma_addr
, size
, __builtin_return_address(0));
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
,
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
);
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. */
436 pci_alloc_consistent(struct pci_dev
*pdev
, size_t size
, dma_addr_t
*dma_addrp
)
439 long order
= get_order(size
);
440 gfp_t gfp
= GFP_ATOMIC
;
443 cpu_addr
= (void *)__get_free_pages(gfp
, order
);
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. */
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
))
459 /* The address doesn't fit required mask and we
460 do not have iommu. Try again with GFP_DMA. */
465 DBGA2("pci_alloc_consistent: %lx -> [%p,%x] from %p\n",
466 size
, cpu_addr
, *dma_addrp
, __builtin_return_address(0));
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. */
479 pci_free_consistent(struct pci_dev
*pdev
, size_t size
, void *cpu_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))
503 sg_classify(struct device
*dev
, struct scatterlist
*sg
, struct scatterlist
*end
,
506 unsigned long next_paddr
;
507 struct scatterlist
*leader
;
508 long leader_flag
, leader_length
;
509 unsigned int max_seg_size
;
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
);
523 if (leader_length
+ len
> max_seg_size
)
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;
532 leader_length
+= len
;
535 leader
->dma_address
= leader_flag
;
536 leader
->dma_length
= leader_length
;
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
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
;
561 long npages
, dma_ofs
, i
;
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
);
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
);
590 /* Otherwise, we'll use the iommu to make the pages virtually
594 npages
= calc_npages(paddr
+ size
);
595 dma_ofs
= iommu_arena_alloc(dev
, arena
, npages
, 0);
597 /* If we attempted a direct map above but failed, die. */
598 if (leader
->dma_address
== 0)
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
];
619 struct scatterlist
*last_sg
= sg
;
623 paddr
= SG_ENT_PHYS_ADDRESS(sg
);
625 while (sg
+1 < end
&& (int) sg
[1].dma_address
== -1) {
626 size
+= sg
[1].length
;
630 npages
= calc_npages((paddr
& ~PAGE_MASK
) + size
);
633 for (i
= 0; i
< npages
; ++i
, paddr
+= PAGE_SIZE
)
634 *ptes
++ = mk_iommu_pte(paddr
);
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
),
646 } while (++sg
< end
&& (int) sg
->dma_address
< 0);
652 pci_map_sg(struct pci_dev
*pdev
, struct scatterlist
*sg
, int nents
,
655 struct scatterlist
*start
, *end
, *out
;
656 struct pci_controller
*hose
;
657 struct pci_iommu_arena
*arena
;
662 if (direction
== PCI_DMA_NONE
)
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. */
671 sg
->dma_length
= sg
->length
;
673 = pci_map_single_1(pdev
, SG_ENT_VIRT_ADDRESS(sg
),
674 sg
->length
, dac_allowed
);
675 return sg
->dma_address
!= 0;
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
;
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)
702 if (sg_fill(dev
, sg
, end
, out
, arena
, max_dma
, dac_allowed
) < 0)
707 /* Mark the end of the list for pci_unmap_sg. */
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
);
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. */
724 pci_unmap_sg(pdev
, start
, out
- start
, direction
);
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()
734 pci_unmap_sg(struct pci_dev
*pdev
, struct scatterlist
*sg
, int nents
,
738 struct pci_controller
*hose
;
739 struct pci_iommu_arena
*arena
;
740 struct scatterlist
*end
;
742 dma_addr_t fbeg
, fend
;
744 if (direction
== PCI_DMA_NONE
)
747 if (! alpha_mv
.mv_pci_tbi
)
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
;
758 spin_lock_irqsave(&arena
->lock
, flags
);
760 for (end
= sg
+ nents
; sg
< end
; ++sg
) {
766 addr
= sg
->dma_address
;
767 size
= sg
->dma_length
;
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
);
778 if (addr
>= __direct_map_base
779 && addr
< __direct_map_base
+ __direct_map_size
) {
781 DBGA(" (%ld) direct [%lx,%lx]\n",
782 sg
- end
+ nents
, addr
, size
);
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
))
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
)
833 arena
= hose
->sg_pci
;
834 if (arena
&& arena
->dma_base
+ arena
->size
- 1 <= mask
)
837 /* As last resort try ZONE_DMA. */
838 if (!__direct_map_base
&& MAX_DMA_ADDRESS
- IDENT_ADDR
- 1 <= mask
)
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
)
856 if (!arena
) return -EINVAL
;
858 spin_lock_irqsave(&arena
->lock
, flags
);
860 /* Search for N empty ptes. */
862 p
= iommu_arena_find_pages(NULL
, arena
, pg_count
, align_mask
);
864 spin_unlock_irqrestore(&arena
->lock
, flags
);
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
);
881 iommu_release(struct pci_iommu_arena
*arena
, long pg_start
, long pg_count
)
886 if (!arena
) return -EINVAL
;
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
)
895 iommu_arena_free(arena
, pg_start
, pg_count
);
900 iommu_bind(struct pci_iommu_arena
*arena
, long pg_start
, long pg_count
,
901 unsigned long *physaddrs
)
907 if (!arena
) return -EINVAL
;
909 spin_lock_irqsave(&arena
->lock
, flags
);
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
);
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
);
929 iommu_unbind(struct pci_iommu_arena
*arena
, long pg_start
, long pg_count
)
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
;
943 /* True if the machine supports DAC addressing, and DEV can
944 make use of it given MASK. */
947 pci_dac_dma_supported(struct pci_dev
*dev
, u64 mask
)
949 dma64_addr_t dac_offset
= alpha_mv
.pci_dac_offset
;
952 /* If this is not set, the machine doesn't support DAC at all. */
956 /* The device has to be able to address our DAC bit. */
957 if ((dac_offset
& dev
->dma_mask
) != dac_offset
)
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));
967 /* Helper for generic DMA-mapping functions. */
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,
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
)
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
)
989 /* This assumes ISA bus master with dma_mask 0xffffff. */
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
))
1001 *dev
->dma_mask
= mask
;
1005 EXPORT_SYMBOL(dma_set_mask
);