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