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