xref: /openbmc/linux/arch/alpha/kernel/pci_iommu.c (revision f42b3800)
1 /*
2  *	linux/arch/alpha/kernel/pci_iommu.c
3  */
4 
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>
13 #include <linux/iommu-helper.h>
14 
15 #include <asm/io.h>
16 #include <asm/hwrpb.h>
17 
18 #include "proto.h"
19 #include "pci_impl.h"
20 
21 
22 #define DEBUG_ALLOC 0
23 #if DEBUG_ALLOC > 0
24 # define DBGA(args...)		printk(KERN_DEBUG args)
25 #else
26 # define DBGA(args...)
27 #endif
28 #if DEBUG_ALLOC > 1
29 # define DBGA2(args...)		printk(KERN_DEBUG args)
30 #else
31 # define DBGA2(args...)
32 #endif
33 
34 #define DEBUG_NODIRECT 0
35 
36 #define ISA_DMA_MASK		0x00ffffff
37 
38 static inline unsigned long
39 mk_iommu_pte(unsigned long paddr)
40 {
41 	return (paddr >> (PAGE_SHIFT-1)) | 1;
42 }
43 
44 static inline long
45 calc_npages(long bytes)
46 {
47 	return (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
48 }
49 
50 
51 /* Return the minimum of MAX or the first power of two larger
52    than main memory.  */
53 
54 unsigned long
55 size_for_memory(unsigned long max)
56 {
57 	unsigned long mem = max_low_pfn << PAGE_SHIFT;
58 	if (mem < max)
59 		max = roundup_pow_of_two(mem);
60 	return max;
61 }
62 
63 struct pci_iommu_arena * __init
64 iommu_arena_new_node(int nid, struct pci_controller *hose, dma_addr_t base,
65 		     unsigned long window_size, unsigned long align)
66 {
67 	unsigned long mem_size;
68 	struct pci_iommu_arena *arena;
69 
70 	mem_size = window_size / (PAGE_SIZE / sizeof(unsigned long));
71 
72 	/* Note that the TLB lookup logic uses bitwise concatenation,
73 	   not addition, so the required arena alignment is based on
74 	   the size of the window.  Retain the align parameter so that
75 	   particular systems can over-align the arena.  */
76 	if (align < mem_size)
77 		align = mem_size;
78 
79 
80 #ifdef CONFIG_DISCONTIGMEM
81 
82         if (!NODE_DATA(nid) ||
83             (NULL == (arena = alloc_bootmem_node(NODE_DATA(nid),
84                                                  sizeof(*arena))))) {
85                 printk("%s: couldn't allocate arena from node %d\n"
86                        "    falling back to system-wide allocation\n",
87                        __FUNCTION__, nid);
88                 arena = alloc_bootmem(sizeof(*arena));
89         }
90 
91         if (!NODE_DATA(nid) ||
92             (NULL == (arena->ptes = __alloc_bootmem_node(NODE_DATA(nid),
93                                                          mem_size,
94                                                          align,
95                                                          0)))) {
96                 printk("%s: couldn't allocate arena ptes from node %d\n"
97                        "    falling back to system-wide allocation\n",
98                        __FUNCTION__, nid);
99                 arena->ptes = __alloc_bootmem(mem_size, align, 0);
100         }
101 
102 #else /* CONFIG_DISCONTIGMEM */
103 
104 	arena = alloc_bootmem(sizeof(*arena));
105 	arena->ptes = __alloc_bootmem(mem_size, align, 0);
106 
107 #endif /* CONFIG_DISCONTIGMEM */
108 
109 	spin_lock_init(&arena->lock);
110 	arena->hose = hose;
111 	arena->dma_base = base;
112 	arena->size = window_size;
113 	arena->next_entry = 0;
114 
115 	/* Align allocations to a multiple of a page size.  Not needed
116 	   unless there are chip bugs.  */
117 	arena->align_entry = 1;
118 
119 	return arena;
120 }
121 
122 struct pci_iommu_arena * __init
123 iommu_arena_new(struct pci_controller *hose, dma_addr_t base,
124 		unsigned long window_size, unsigned long align)
125 {
126 	return iommu_arena_new_node(0, hose, base, window_size, align);
127 }
128 
129 /* Must be called with the arena lock held */
130 static long
131 iommu_arena_find_pages(struct device *dev, struct pci_iommu_arena *arena,
132 		       long n, long mask)
133 {
134 	unsigned long *ptes;
135 	long i, p, nent;
136 	int pass = 0;
137 	unsigned long base;
138 	unsigned long boundary_size;
139 
140 	base = arena->dma_base >> PAGE_SHIFT;
141 	if (dev) {
142 		boundary_size = dma_get_seg_boundary(dev) + 1;
143 		boundary_size >>= PAGE_SHIFT;
144 	} else {
145 		boundary_size = 1UL << (32 - PAGE_SHIFT);
146 	}
147 
148 	/* Search forward for the first mask-aligned sequence of N free ptes */
149 	ptes = arena->ptes;
150 	nent = arena->size >> PAGE_SHIFT;
151 	p = ALIGN(arena->next_entry, mask + 1);
152 	i = 0;
153 
154 again:
155 	while (i < n && p+i < nent) {
156 		if (!i && iommu_is_span_boundary(p, n, base, boundary_size)) {
157 			p = ALIGN(p + 1, mask + 1);
158 			goto again;
159 		}
160 
161 		if (ptes[p+i])
162 			p = ALIGN(p + i + 1, mask + 1), i = 0;
163 		else
164 			i = i + 1;
165 	}
166 
167 	if (i < n) {
168 		if (pass < 1) {
169 			/*
170 			 * Reached the end.  Flush the TLB and restart
171 			 * the search from the beginning.
172 			*/
173 			alpha_mv.mv_pci_tbi(arena->hose, 0, -1);
174 
175 			pass++;
176 			p = 0;
177 			i = 0;
178 			goto again;
179 		} else
180 			return -1;
181 	}
182 
183 	/* Success. It's the responsibility of the caller to mark them
184 	   in use before releasing the lock */
185 	return p;
186 }
187 
188 static long
189 iommu_arena_alloc(struct device *dev, struct pci_iommu_arena *arena, long n,
190 		  unsigned int align)
191 {
192 	unsigned long flags;
193 	unsigned long *ptes;
194 	long i, p, mask;
195 
196 	spin_lock_irqsave(&arena->lock, flags);
197 
198 	/* Search for N empty ptes */
199 	ptes = arena->ptes;
200 	mask = max(align, arena->align_entry) - 1;
201 	p = iommu_arena_find_pages(dev, arena, n, mask);
202 	if (p < 0) {
203 		spin_unlock_irqrestore(&arena->lock, flags);
204 		return -1;
205 	}
206 
207 	/* Success.  Mark them all in use, ie not zero and invalid
208 	   for the iommu tlb that could load them from under us.
209 	   The chip specific bits will fill this in with something
210 	   kosher when we return.  */
211 	for (i = 0; i < n; ++i)
212 		ptes[p+i] = IOMMU_INVALID_PTE;
213 
214 	arena->next_entry = p + n;
215 	spin_unlock_irqrestore(&arena->lock, flags);
216 
217 	return p;
218 }
219 
220 static void
221 iommu_arena_free(struct pci_iommu_arena *arena, long ofs, long n)
222 {
223 	unsigned long *p;
224 	long i;
225 
226 	p = arena->ptes + ofs;
227 	for (i = 0; i < n; ++i)
228 		p[i] = 0;
229 }
230 
231 /* True if the machine supports DAC addressing, and DEV can
232    make use of it given MASK.  */
233 static int pci_dac_dma_supported(struct pci_dev *hwdev, u64 mask);
234 
235 /* Map a single buffer of the indicated size for PCI DMA in streaming
236    mode.  The 32-bit PCI bus mastering address to use is returned.
237    Once the device is given the dma address, the device owns this memory
238    until either pci_unmap_single or pci_dma_sync_single is performed.  */
239 
240 static dma_addr_t
241 pci_map_single_1(struct pci_dev *pdev, void *cpu_addr, size_t size,
242 		 int dac_allowed)
243 {
244 	struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose;
245 	dma_addr_t max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
246 	struct pci_iommu_arena *arena;
247 	long npages, dma_ofs, i;
248 	unsigned long paddr;
249 	dma_addr_t ret;
250 	unsigned int align = 0;
251 	struct device *dev = pdev ? &pdev->dev : NULL;
252 
253 	paddr = __pa(cpu_addr);
254 
255 #if !DEBUG_NODIRECT
256 	/* First check to see if we can use the direct map window.  */
257 	if (paddr + size + __direct_map_base - 1 <= max_dma
258 	    && paddr + size <= __direct_map_size) {
259 		ret = paddr + __direct_map_base;
260 
261 		DBGA2("pci_map_single: [%p,%lx] -> direct %lx from %p\n",
262 		      cpu_addr, size, ret, __builtin_return_address(0));
263 
264 		return ret;
265 	}
266 #endif
267 
268 	/* Next, use DAC if selected earlier.  */
269 	if (dac_allowed) {
270 		ret = paddr + alpha_mv.pci_dac_offset;
271 
272 		DBGA2("pci_map_single: [%p,%lx] -> DAC %lx from %p\n",
273 		      cpu_addr, size, ret, __builtin_return_address(0));
274 
275 		return ret;
276 	}
277 
278 	/* If the machine doesn't define a pci_tbi routine, we have to
279 	   assume it doesn't support sg mapping, and, since we tried to
280 	   use direct_map above, it now must be considered an error. */
281 	if (! alpha_mv.mv_pci_tbi) {
282 		static int been_here = 0; /* Only print the message once. */
283 		if (!been_here) {
284 		    printk(KERN_WARNING "pci_map_single: no HW sg\n");
285 		    been_here = 1;
286 		}
287 		return 0;
288 	}
289 
290 	arena = hose->sg_pci;
291 	if (!arena || arena->dma_base + arena->size - 1 > max_dma)
292 		arena = hose->sg_isa;
293 
294 	npages = calc_npages((paddr & ~PAGE_MASK) + size);
295 
296 	/* Force allocation to 64KB boundary for ISA bridges. */
297 	if (pdev && pdev == isa_bridge)
298 		align = 8;
299 	dma_ofs = iommu_arena_alloc(dev, arena, npages, align);
300 	if (dma_ofs < 0) {
301 		printk(KERN_WARNING "pci_map_single failed: "
302 		       "could not allocate dma page tables\n");
303 		return 0;
304 	}
305 
306 	paddr &= PAGE_MASK;
307 	for (i = 0; i < npages; ++i, paddr += PAGE_SIZE)
308 		arena->ptes[i + dma_ofs] = mk_iommu_pte(paddr);
309 
310 	ret = arena->dma_base + dma_ofs * PAGE_SIZE;
311 	ret += (unsigned long)cpu_addr & ~PAGE_MASK;
312 
313 	DBGA2("pci_map_single: [%p,%lx] np %ld -> sg %lx from %p\n",
314 	      cpu_addr, size, npages, ret, __builtin_return_address(0));
315 
316 	return ret;
317 }
318 
319 dma_addr_t
320 pci_map_single(struct pci_dev *pdev, void *cpu_addr, size_t size, int dir)
321 {
322 	int dac_allowed;
323 
324 	if (dir == PCI_DMA_NONE)
325 		BUG();
326 
327 	dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
328 	return pci_map_single_1(pdev, cpu_addr, size, dac_allowed);
329 }
330 EXPORT_SYMBOL(pci_map_single);
331 
332 dma_addr_t
333 pci_map_page(struct pci_dev *pdev, struct page *page, unsigned long offset,
334 	     size_t size, int dir)
335 {
336 	int dac_allowed;
337 
338 	if (dir == PCI_DMA_NONE)
339 		BUG();
340 
341 	dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
342 	return pci_map_single_1(pdev, (char *)page_address(page) + offset,
343 				size, dac_allowed);
344 }
345 EXPORT_SYMBOL(pci_map_page);
346 
347 /* Unmap a single streaming mode DMA translation.  The DMA_ADDR and
348    SIZE must match what was provided for in a previous pci_map_single
349    call.  All other usages are undefined.  After this call, reads by
350    the cpu to the buffer are guaranteed to see whatever the device
351    wrote there.  */
352 
353 void
354 pci_unmap_single(struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
355 		 int direction)
356 {
357 	unsigned long flags;
358 	struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose;
359 	struct pci_iommu_arena *arena;
360 	long dma_ofs, npages;
361 
362 	if (direction == PCI_DMA_NONE)
363 		BUG();
364 
365 	if (dma_addr >= __direct_map_base
366 	    && dma_addr < __direct_map_base + __direct_map_size) {
367 		/* Nothing to do.  */
368 
369 		DBGA2("pci_unmap_single: direct [%lx,%lx] from %p\n",
370 		      dma_addr, size, __builtin_return_address(0));
371 
372 		return;
373 	}
374 
375 	if (dma_addr > 0xffffffff) {
376 		DBGA2("pci64_unmap_single: DAC [%lx,%lx] from %p\n",
377 		      dma_addr, size, __builtin_return_address(0));
378 		return;
379 	}
380 
381 	arena = hose->sg_pci;
382 	if (!arena || dma_addr < arena->dma_base)
383 		arena = hose->sg_isa;
384 
385 	dma_ofs = (dma_addr - arena->dma_base) >> PAGE_SHIFT;
386 	if (dma_ofs * PAGE_SIZE >= arena->size) {
387 		printk(KERN_ERR "Bogus pci_unmap_single: dma_addr %lx "
388 		       " base %lx size %x\n", dma_addr, arena->dma_base,
389 		       arena->size);
390 		return;
391 		BUG();
392 	}
393 
394 	npages = calc_npages((dma_addr & ~PAGE_MASK) + size);
395 
396 	spin_lock_irqsave(&arena->lock, flags);
397 
398 	iommu_arena_free(arena, dma_ofs, npages);
399 
400         /* If we're freeing ptes above the `next_entry' pointer (they
401            may have snuck back into the TLB since the last wrap flush),
402            we need to flush the TLB before reallocating the latter.  */
403 	if (dma_ofs >= arena->next_entry)
404 		alpha_mv.mv_pci_tbi(hose, dma_addr, dma_addr + size - 1);
405 
406 	spin_unlock_irqrestore(&arena->lock, flags);
407 
408 	DBGA2("pci_unmap_single: sg [%lx,%lx] np %ld from %p\n",
409 	      dma_addr, size, npages, __builtin_return_address(0));
410 }
411 EXPORT_SYMBOL(pci_unmap_single);
412 
413 void
414 pci_unmap_page(struct pci_dev *pdev, dma_addr_t dma_addr,
415 	       size_t size, int direction)
416 {
417 	pci_unmap_single(pdev, dma_addr, size, direction);
418 }
419 EXPORT_SYMBOL(pci_unmap_page);
420 
421 /* Allocate and map kernel buffer using consistent mode DMA for PCI
422    device.  Returns non-NULL cpu-view pointer to the buffer if
423    successful and sets *DMA_ADDRP to the pci side dma address as well,
424    else DMA_ADDRP is undefined.  */
425 
426 void *
427 __pci_alloc_consistent(struct pci_dev *pdev, size_t size,
428 		       dma_addr_t *dma_addrp, gfp_t gfp)
429 {
430 	void *cpu_addr;
431 	long order = get_order(size);
432 
433 	gfp &= ~GFP_DMA;
434 
435 try_again:
436 	cpu_addr = (void *)__get_free_pages(gfp, order);
437 	if (! cpu_addr) {
438 		printk(KERN_INFO "pci_alloc_consistent: "
439 		       "get_free_pages failed from %p\n",
440 			__builtin_return_address(0));
441 		/* ??? Really atomic allocation?  Otherwise we could play
442 		   with vmalloc and sg if we can't find contiguous memory.  */
443 		return NULL;
444 	}
445 	memset(cpu_addr, 0, size);
446 
447 	*dma_addrp = pci_map_single_1(pdev, cpu_addr, size, 0);
448 	if (*dma_addrp == 0) {
449 		free_pages((unsigned long)cpu_addr, order);
450 		if (alpha_mv.mv_pci_tbi || (gfp & GFP_DMA))
451 			return NULL;
452 		/* The address doesn't fit required mask and we
453 		   do not have iommu. Try again with GFP_DMA. */
454 		gfp |= GFP_DMA;
455 		goto try_again;
456 	}
457 
458 	DBGA2("pci_alloc_consistent: %lx -> [%p,%x] from %p\n",
459 	      size, cpu_addr, *dma_addrp, __builtin_return_address(0));
460 
461 	return cpu_addr;
462 }
463 EXPORT_SYMBOL(__pci_alloc_consistent);
464 
465 /* Free and unmap a consistent DMA buffer.  CPU_ADDR and DMA_ADDR must
466    be values that were returned from pci_alloc_consistent.  SIZE must
467    be the same as what as passed into pci_alloc_consistent.
468    References to the memory and mappings associated with CPU_ADDR or
469    DMA_ADDR past this call are illegal.  */
470 
471 void
472 pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu_addr,
473 		    dma_addr_t dma_addr)
474 {
475 	pci_unmap_single(pdev, dma_addr, size, PCI_DMA_BIDIRECTIONAL);
476 	free_pages((unsigned long)cpu_addr, get_order(size));
477 
478 	DBGA2("pci_free_consistent: [%x,%lx] from %p\n",
479 	      dma_addr, size, __builtin_return_address(0));
480 }
481 EXPORT_SYMBOL(pci_free_consistent);
482 
483 /* Classify the elements of the scatterlist.  Write dma_address
484    of each element with:
485 	0   : Followers all physically adjacent.
486 	1   : Followers all virtually adjacent.
487 	-1  : Not leader, physically adjacent to previous.
488 	-2  : Not leader, virtually adjacent to previous.
489    Write dma_length of each leader with the combined lengths of
490    the mergable followers.  */
491 
492 #define SG_ENT_VIRT_ADDRESS(SG) (sg_virt((SG)))
493 #define SG_ENT_PHYS_ADDRESS(SG) __pa(SG_ENT_VIRT_ADDRESS(SG))
494 
495 static void
496 sg_classify(struct device *dev, struct scatterlist *sg, struct scatterlist *end,
497 	    int virt_ok)
498 {
499 	unsigned long next_paddr;
500 	struct scatterlist *leader;
501 	long leader_flag, leader_length;
502 	unsigned int max_seg_size;
503 
504 	leader = sg;
505 	leader_flag = 0;
506 	leader_length = leader->length;
507 	next_paddr = SG_ENT_PHYS_ADDRESS(leader) + leader_length;
508 
509 	/* we will not marge sg without device. */
510 	max_seg_size = dev ? dma_get_max_seg_size(dev) : 0;
511 	for (++sg; sg < end; ++sg) {
512 		unsigned long addr, len;
513 		addr = SG_ENT_PHYS_ADDRESS(sg);
514 		len = sg->length;
515 
516 		if (leader_length + len > max_seg_size)
517 			goto new_segment;
518 
519 		if (next_paddr == addr) {
520 			sg->dma_address = -1;
521 			leader_length += len;
522 		} else if (((next_paddr | addr) & ~PAGE_MASK) == 0 && virt_ok) {
523 			sg->dma_address = -2;
524 			leader_flag = 1;
525 			leader_length += len;
526 		} else {
527 new_segment:
528 			leader->dma_address = leader_flag;
529 			leader->dma_length = leader_length;
530 			leader = sg;
531 			leader_flag = 0;
532 			leader_length = len;
533 		}
534 
535 		next_paddr = addr + len;
536 	}
537 
538 	leader->dma_address = leader_flag;
539 	leader->dma_length = leader_length;
540 }
541 
542 /* Given a scatterlist leader, choose an allocation method and fill
543    in the blanks.  */
544 
545 static int
546 sg_fill(struct device *dev, struct scatterlist *leader, struct scatterlist *end,
547 	struct scatterlist *out, struct pci_iommu_arena *arena,
548 	dma_addr_t max_dma, int dac_allowed)
549 {
550 	unsigned long paddr = SG_ENT_PHYS_ADDRESS(leader);
551 	long size = leader->dma_length;
552 	struct scatterlist *sg;
553 	unsigned long *ptes;
554 	long npages, dma_ofs, i;
555 
556 #if !DEBUG_NODIRECT
557 	/* If everything is physically contiguous, and the addresses
558 	   fall into the direct-map window, use it.  */
559 	if (leader->dma_address == 0
560 	    && paddr + size + __direct_map_base - 1 <= max_dma
561 	    && paddr + size <= __direct_map_size) {
562 		out->dma_address = paddr + __direct_map_base;
563 		out->dma_length = size;
564 
565 		DBGA("    sg_fill: [%p,%lx] -> direct %lx\n",
566 		     __va(paddr), size, out->dma_address);
567 
568 		return 0;
569 	}
570 #endif
571 
572 	/* If physically contiguous and DAC is available, use it.  */
573 	if (leader->dma_address == 0 && dac_allowed) {
574 		out->dma_address = paddr + alpha_mv.pci_dac_offset;
575 		out->dma_length = size;
576 
577 		DBGA("    sg_fill: [%p,%lx] -> DAC %lx\n",
578 		     __va(paddr), size, out->dma_address);
579 
580 		return 0;
581 	}
582 
583 	/* Otherwise, we'll use the iommu to make the pages virtually
584 	   contiguous.  */
585 
586 	paddr &= ~PAGE_MASK;
587 	npages = calc_npages(paddr + size);
588 	dma_ofs = iommu_arena_alloc(dev, arena, npages, 0);
589 	if (dma_ofs < 0) {
590 		/* If we attempted a direct map above but failed, die.  */
591 		if (leader->dma_address == 0)
592 			return -1;
593 
594 		/* Otherwise, break up the remaining virtually contiguous
595 		   hunks into individual direct maps and retry.  */
596 		sg_classify(dev, leader, end, 0);
597 		return sg_fill(dev, leader, end, out, arena, max_dma, dac_allowed);
598 	}
599 
600 	out->dma_address = arena->dma_base + dma_ofs*PAGE_SIZE + paddr;
601 	out->dma_length = size;
602 
603 	DBGA("    sg_fill: [%p,%lx] -> sg %lx np %ld\n",
604 	     __va(paddr), size, out->dma_address, npages);
605 
606 	/* All virtually contiguous.  We need to find the length of each
607 	   physically contiguous subsegment to fill in the ptes.  */
608 	ptes = &arena->ptes[dma_ofs];
609 	sg = leader;
610 	do {
611 #if DEBUG_ALLOC > 0
612 		struct scatterlist *last_sg = sg;
613 #endif
614 
615 		size = sg->length;
616 		paddr = SG_ENT_PHYS_ADDRESS(sg);
617 
618 		while (sg+1 < end && (int) sg[1].dma_address == -1) {
619 			size += sg[1].length;
620 			sg++;
621 		}
622 
623 		npages = calc_npages((paddr & ~PAGE_MASK) + size);
624 
625 		paddr &= PAGE_MASK;
626 		for (i = 0; i < npages; ++i, paddr += PAGE_SIZE)
627 			*ptes++ = mk_iommu_pte(paddr);
628 
629 #if DEBUG_ALLOC > 0
630 		DBGA("    (%ld) [%p,%x] np %ld\n",
631 		     last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg),
632 		     last_sg->length, npages);
633 		while (++last_sg <= sg) {
634 			DBGA("        (%ld) [%p,%x] cont\n",
635 			     last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg),
636 			     last_sg->length);
637 		}
638 #endif
639 	} while (++sg < end && (int) sg->dma_address < 0);
640 
641 	return 1;
642 }
643 
644 int
645 pci_map_sg(struct pci_dev *pdev, struct scatterlist *sg, int nents,
646 	   int direction)
647 {
648 	struct scatterlist *start, *end, *out;
649 	struct pci_controller *hose;
650 	struct pci_iommu_arena *arena;
651 	dma_addr_t max_dma;
652 	int dac_allowed;
653 	struct device *dev;
654 
655 	if (direction == PCI_DMA_NONE)
656 		BUG();
657 
658 	dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
659 
660 	dev = pdev ? &pdev->dev : NULL;
661 
662 	/* Fast path single entry scatterlists.  */
663 	if (nents == 1) {
664 		sg->dma_length = sg->length;
665 		sg->dma_address
666 		  = pci_map_single_1(pdev, SG_ENT_VIRT_ADDRESS(sg),
667 				     sg->length, dac_allowed);
668 		return sg->dma_address != 0;
669 	}
670 
671 	start = sg;
672 	end = sg + nents;
673 
674 	/* First, prepare information about the entries.  */
675 	sg_classify(dev, sg, end, alpha_mv.mv_pci_tbi != 0);
676 
677 	/* Second, figure out where we're going to map things.  */
678 	if (alpha_mv.mv_pci_tbi) {
679 		hose = pdev ? pdev->sysdata : pci_isa_hose;
680 		max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
681 		arena = hose->sg_pci;
682 		if (!arena || arena->dma_base + arena->size - 1 > max_dma)
683 			arena = hose->sg_isa;
684 	} else {
685 		max_dma = -1;
686 		arena = NULL;
687 		hose = NULL;
688 	}
689 
690 	/* Third, iterate over the scatterlist leaders and allocate
691 	   dma space as needed.  */
692 	for (out = sg; sg < end; ++sg) {
693 		if ((int) sg->dma_address < 0)
694 			continue;
695 		if (sg_fill(dev, sg, end, out, arena, max_dma, dac_allowed) < 0)
696 			goto error;
697 		out++;
698 	}
699 
700 	/* Mark the end of the list for pci_unmap_sg.  */
701 	if (out < end)
702 		out->dma_length = 0;
703 
704 	if (out - start == 0)
705 		printk(KERN_WARNING "pci_map_sg failed: no entries?\n");
706 	DBGA("pci_map_sg: %ld entries\n", out - start);
707 
708 	return out - start;
709 
710  error:
711 	printk(KERN_WARNING "pci_map_sg failed: "
712 	       "could not allocate dma page tables\n");
713 
714 	/* Some allocation failed while mapping the scatterlist
715 	   entries.  Unmap them now.  */
716 	if (out > start)
717 		pci_unmap_sg(pdev, start, out - start, direction);
718 	return 0;
719 }
720 EXPORT_SYMBOL(pci_map_sg);
721 
722 /* Unmap a set of streaming mode DMA translations.  Again, cpu read
723    rules concerning calls here are the same as for pci_unmap_single()
724    above.  */
725 
726 void
727 pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sg, int nents,
728 	     int direction)
729 {
730 	unsigned long flags;
731 	struct pci_controller *hose;
732 	struct pci_iommu_arena *arena;
733 	struct scatterlist *end;
734 	dma_addr_t max_dma;
735 	dma_addr_t fbeg, fend;
736 
737 	if (direction == PCI_DMA_NONE)
738 		BUG();
739 
740 	if (! alpha_mv.mv_pci_tbi)
741 		return;
742 
743 	hose = pdev ? pdev->sysdata : pci_isa_hose;
744 	max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
745 	arena = hose->sg_pci;
746 	if (!arena || arena->dma_base + arena->size - 1 > max_dma)
747 		arena = hose->sg_isa;
748 
749 	fbeg = -1, fend = 0;
750 
751 	spin_lock_irqsave(&arena->lock, flags);
752 
753 	for (end = sg + nents; sg < end; ++sg) {
754 		dma64_addr_t addr;
755 		size_t size;
756 		long npages, ofs;
757 		dma_addr_t tend;
758 
759 		addr = sg->dma_address;
760 		size = sg->dma_length;
761 		if (!size)
762 			break;
763 
764 		if (addr > 0xffffffff) {
765 			/* It's a DAC address -- nothing to do.  */
766 			DBGA("    (%ld) DAC [%lx,%lx]\n",
767 			      sg - end + nents, addr, size);
768 			continue;
769 		}
770 
771 		if (addr >= __direct_map_base
772 		    && addr < __direct_map_base + __direct_map_size) {
773 			/* Nothing to do.  */
774 			DBGA("    (%ld) direct [%lx,%lx]\n",
775 			      sg - end + nents, addr, size);
776 			continue;
777 		}
778 
779 		DBGA("    (%ld) sg [%lx,%lx]\n",
780 		     sg - end + nents, addr, size);
781 
782 		npages = calc_npages((addr & ~PAGE_MASK) + size);
783 		ofs = (addr - arena->dma_base) >> PAGE_SHIFT;
784 		iommu_arena_free(arena, ofs, npages);
785 
786 		tend = addr + size - 1;
787 		if (fbeg > addr) fbeg = addr;
788 		if (fend < tend) fend = tend;
789 	}
790 
791         /* If we're freeing ptes above the `next_entry' pointer (they
792            may have snuck back into the TLB since the last wrap flush),
793            we need to flush the TLB before reallocating the latter.  */
794 	if ((fend - arena->dma_base) >> PAGE_SHIFT >= arena->next_entry)
795 		alpha_mv.mv_pci_tbi(hose, fbeg, fend);
796 
797 	spin_unlock_irqrestore(&arena->lock, flags);
798 
799 	DBGA("pci_unmap_sg: %ld entries\n", nents - (end - sg));
800 }
801 EXPORT_SYMBOL(pci_unmap_sg);
802 
803 
804 /* Return whether the given PCI device DMA address mask can be
805    supported properly.  */
806 
807 int
808 pci_dma_supported(struct pci_dev *pdev, u64 mask)
809 {
810 	struct pci_controller *hose;
811 	struct pci_iommu_arena *arena;
812 
813 	/* If there exists a direct map, and the mask fits either
814 	   the entire direct mapped space or the total system memory as
815 	   shifted by the map base */
816 	if (__direct_map_size != 0
817 	    && (__direct_map_base + __direct_map_size - 1 <= mask ||
818 		__direct_map_base + (max_low_pfn << PAGE_SHIFT) - 1 <= mask))
819 		return 1;
820 
821 	/* Check that we have a scatter-gather arena that fits.  */
822 	hose = pdev ? pdev->sysdata : pci_isa_hose;
823 	arena = hose->sg_isa;
824 	if (arena && arena->dma_base + arena->size - 1 <= mask)
825 		return 1;
826 	arena = hose->sg_pci;
827 	if (arena && arena->dma_base + arena->size - 1 <= mask)
828 		return 1;
829 
830 	/* As last resort try ZONE_DMA.  */
831 	if (!__direct_map_base && MAX_DMA_ADDRESS - IDENT_ADDR - 1 <= mask)
832 		return 1;
833 
834 	return 0;
835 }
836 EXPORT_SYMBOL(pci_dma_supported);
837 
838 
839 /*
840  * AGP GART extensions to the IOMMU
841  */
842 int
843 iommu_reserve(struct pci_iommu_arena *arena, long pg_count, long align_mask)
844 {
845 	unsigned long flags;
846 	unsigned long *ptes;
847 	long i, p;
848 
849 	if (!arena) return -EINVAL;
850 
851 	spin_lock_irqsave(&arena->lock, flags);
852 
853 	/* Search for N empty ptes.  */
854 	ptes = arena->ptes;
855 	p = iommu_arena_find_pages(NULL, arena, pg_count, align_mask);
856 	if (p < 0) {
857 		spin_unlock_irqrestore(&arena->lock, flags);
858 		return -1;
859 	}
860 
861 	/* Success.  Mark them all reserved (ie not zero and invalid)
862 	   for the iommu tlb that could load them from under us.
863 	   They will be filled in with valid bits by _bind() */
864 	for (i = 0; i < pg_count; ++i)
865 		ptes[p+i] = IOMMU_RESERVED_PTE;
866 
867 	arena->next_entry = p + pg_count;
868 	spin_unlock_irqrestore(&arena->lock, flags);
869 
870 	return p;
871 }
872 
873 int
874 iommu_release(struct pci_iommu_arena *arena, long pg_start, long pg_count)
875 {
876 	unsigned long *ptes;
877 	long i;
878 
879 	if (!arena) return -EINVAL;
880 
881 	ptes = arena->ptes;
882 
883 	/* Make sure they're all reserved first... */
884 	for(i = pg_start; i < pg_start + pg_count; i++)
885 		if (ptes[i] != IOMMU_RESERVED_PTE)
886 			return -EBUSY;
887 
888 	iommu_arena_free(arena, pg_start, pg_count);
889 	return 0;
890 }
891 
892 int
893 iommu_bind(struct pci_iommu_arena *arena, long pg_start, long pg_count,
894 	   unsigned long *physaddrs)
895 {
896 	unsigned long flags;
897 	unsigned long *ptes;
898 	long i, j;
899 
900 	if (!arena) return -EINVAL;
901 
902 	spin_lock_irqsave(&arena->lock, flags);
903 
904 	ptes = arena->ptes;
905 
906 	for(j = pg_start; j < pg_start + pg_count; j++) {
907 		if (ptes[j] != IOMMU_RESERVED_PTE) {
908 			spin_unlock_irqrestore(&arena->lock, flags);
909 			return -EBUSY;
910 		}
911 	}
912 
913 	for(i = 0, j = pg_start; i < pg_count; i++, j++)
914 		ptes[j] = mk_iommu_pte(physaddrs[i]);
915 
916 	spin_unlock_irqrestore(&arena->lock, flags);
917 
918 	return 0;
919 }
920 
921 int
922 iommu_unbind(struct pci_iommu_arena *arena, long pg_start, long pg_count)
923 {
924 	unsigned long *p;
925 	long i;
926 
927 	if (!arena) return -EINVAL;
928 
929 	p = arena->ptes + pg_start;
930 	for(i = 0; i < pg_count; i++)
931 		p[i] = IOMMU_RESERVED_PTE;
932 
933 	return 0;
934 }
935 
936 /* True if the machine supports DAC addressing, and DEV can
937    make use of it given MASK.  */
938 
939 static int
940 pci_dac_dma_supported(struct pci_dev *dev, u64 mask)
941 {
942 	dma64_addr_t dac_offset = alpha_mv.pci_dac_offset;
943 	int ok = 1;
944 
945 	/* If this is not set, the machine doesn't support DAC at all.  */
946 	if (dac_offset == 0)
947 		ok = 0;
948 
949 	/* The device has to be able to address our DAC bit.  */
950 	if ((dac_offset & dev->dma_mask) != dac_offset)
951 		ok = 0;
952 
953 	/* If both conditions above are met, we are fine. */
954 	DBGA("pci_dac_dma_supported %s from %p\n",
955 	     ok ? "yes" : "no", __builtin_return_address(0));
956 
957 	return ok;
958 }
959 
960 /* Helper for generic DMA-mapping functions. */
961 
962 struct pci_dev *
963 alpha_gendev_to_pci(struct device *dev)
964 {
965 	if (dev && dev->bus == &pci_bus_type)
966 		return to_pci_dev(dev);
967 
968 	/* Assume that non-PCI devices asking for DMA are either ISA or EISA,
969 	   BUG() otherwise. */
970 	BUG_ON(!isa_bridge);
971 
972 	/* Assume non-busmaster ISA DMA when dma_mask is not set (the ISA
973 	   bridge is bus master then). */
974 	if (!dev || !dev->dma_mask || !*dev->dma_mask)
975 		return isa_bridge;
976 
977 	/* For EISA bus masters, return isa_bridge (it might have smaller
978 	   dma_mask due to wiring limitations). */
979 	if (*dev->dma_mask >= isa_bridge->dma_mask)
980 		return isa_bridge;
981 
982 	/* This assumes ISA bus master with dma_mask 0xffffff. */
983 	return NULL;
984 }
985 EXPORT_SYMBOL(alpha_gendev_to_pci);
986 
987 int
988 dma_set_mask(struct device *dev, u64 mask)
989 {
990 	if (!dev->dma_mask ||
991 	    !pci_dma_supported(alpha_gendev_to_pci(dev), mask))
992 		return -EIO;
993 
994 	*dev->dma_mask = mask;
995 
996 	return 0;
997 }
998 EXPORT_SYMBOL(dma_set_mask);
999