setup enviroment for compilation
[linux-2.4.21-pre4.git] / arch / sparc / kernel / ioport.c
1 /* $Id: ioport.c,v 1.1.1.1 2005/04/11 02:50:12 jack Exp $
2  * ioport.c:  Simple io mapping allocator.
3  *
4  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
6  *
7  * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
8  *
9  * 2000/01/29
10  * <rth> zait: as long as pci_alloc_consistent produces something addressable, 
11  *      things are ok.
12  * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a
13  *      pointer into the big page mapping
14  * <rth> zait: so what?
15  * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page()))
16  * <zaitcev> Hmm
17  * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
18  *      So far so good.
19  * <zaitcev> Now, driver calls pci_free_consistent(with result of
20  *      remap_it_my_way()).
21  * <zaitcev> How do you find the address to pass to free_pages()?
22  * <rth> zait: walk the page tables?  It's only two or three level after all.
23  * <rth> zait: you have to walk them anyway to remove the mapping.
24  * <zaitcev> Hmm
25  * <zaitcev> Sounds reasonable
26  */
27
28 #include <linux/config.h>
29 #include <linux/sched.h>
30 #include <linux/kernel.h>
31 #include <linux/errno.h>
32 #include <linux/types.h>
33 #include <linux/ioport.h>
34 #include <linux/mm.h>
35 #include <linux/slab.h>
36 #include <linux/pci.h>          /* struct pci_dev */
37 #include <linux/proc_fs.h>
38
39 #include <asm/io.h>
40 #include <asm/vaddrs.h>
41 #include <asm/oplib.h>
42 #include <asm/page.h>
43 #include <asm/pgalloc.h>
44 #include <asm/pgtable.h>
45
46 #define mmu_inval_dma_area(p, l)        /* Anton pulled it out for 2.4.0-xx */
47
48 struct resource *_sparc_find_resource(struct resource *r, unsigned long);
49
50 static void *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
51 static void *_sparc_alloc_io(unsigned int busno, unsigned long phys,
52     unsigned long size, char *name);
53 static void _sparc_free_io(struct resource *res);
54
55 /* This points to the next to use virtual memory for DVMA mappings */
56 static struct resource _sparc_dvma = {
57         "sparc_dvma", DVMA_VADDR, DVMA_END - 1
58 };
59 /* This points to the start of I/O mappings, cluable from outside. */
60 /*ext*/ struct resource sparc_iomap = {
61         "sparc_iomap", IOBASE_VADDR, IOBASE_END - 1
62 };
63
64 /*
65  * BTFIXUP would do as well but it seems overkill for the case.
66  */
67 static void (*_sparc_mapioaddr)(unsigned long pa, unsigned long va,
68     int bus, int ro);
69 static void (*_sparc_unmapioaddr)(unsigned long va);
70
71 /*
72  * Our mini-allocator...
73  * Boy this is gross! We need it because we must map I/O for
74  * timers and interrupt controller before the kmalloc is available.
75  */
76
77 #define XNMLN  15
78 #define XNRES  10       /* SS-10 uses 8 */
79
80 struct xresource {
81         struct resource xres;   /* Must be first */
82         int xflag;              /* 1 == used */
83         char xname[XNMLN+1];
84 };
85
86 static struct xresource xresv[XNRES];
87
88 static struct xresource *xres_alloc(void) {
89         struct xresource *xrp;
90         int n;
91
92         xrp = xresv;
93         for (n = 0; n < XNRES; n++) {
94                 if (xrp->xflag == 0) {
95                         xrp->xflag = 1;
96                         return xrp;
97                 }
98                 xrp++;
99         }
100         return NULL;
101 }
102
103 static void xres_free(struct xresource *xrp) {
104         xrp->xflag = 0;
105 }
106
107 /*
108  * These are typically used in PCI drivers
109  * which are trying to be cross-platform.
110  *
111  * Bus type is always zero on IIep.
112  */
113 void *ioremap(unsigned long offset, unsigned long size)
114 {
115         char name[14];
116
117         sprintf(name, "phys_%08x", (u32)offset);
118         return _sparc_alloc_io(0, offset, size, name);
119 }
120
121 /*
122  * Comlimentary to ioremap().
123  */
124 void iounmap(void *virtual)
125 {
126         unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
127         struct resource *res;
128
129         if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
130                 printk("free_io/iounmap: cannot free %lx\n", vaddr);
131                 return;
132         }
133         _sparc_free_io(res);
134
135         if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
136                 xres_free((struct xresource *)res);
137         } else {
138                 kfree(res);
139         }
140 }
141
142 /*
143  */
144 unsigned long sbus_ioremap(struct resource *phyres, unsigned long offset,
145     unsigned long size, char *name)
146 {
147         return (unsigned long) _sparc_alloc_io(phyres->flags & 0xF,
148             phyres->start + offset, size, name);
149 }
150
151 /*
152  */
153 void sbus_iounmap(unsigned long addr, unsigned long size)
154 {
155         iounmap((void *)addr);
156 }
157
158 /*
159  * Meat of mapping
160  */
161 static void *_sparc_alloc_io(unsigned int busno, unsigned long phys,
162     unsigned long size, char *name)
163 {
164         static int printed_full;
165         struct xresource *xres;
166         struct resource *res;
167         char *tack;
168         int tlen;
169         void *va;       /* P3 diag */
170
171         if (name == NULL) name = "???";
172
173         if ((xres = xres_alloc()) != 0) {
174                 tack = xres->xname;
175                 res = &xres->xres;
176         } else {
177                 if (!printed_full) {
178                         printk("ioremap: done with statics, switching to malloc\n");
179                         printed_full = 1;
180                 }
181                 tlen = strlen(name);
182                 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
183                 if (tack == NULL) return NULL;
184                 memset(tack, 0, sizeof(struct resource));
185                 res = (struct resource *) tack;
186                 tack += sizeof (struct resource);
187         }
188
189         strncpy(tack, name, XNMLN);
190         tack[XNMLN] = 0;
191         res->name = tack;
192
193         va = _sparc_ioremap(res, busno, phys, size);
194         /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
195         return va;
196 }
197
198 /*
199  */
200 static void *
201 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
202 {
203         unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
204         unsigned long va;
205         unsigned int psz;
206
207         if (allocate_resource(&sparc_iomap, res,
208             (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
209             sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
210                 /* Usually we cannot see printks in this case. */
211                 prom_printf("alloc_io_res(%s): cannot occupy\n",
212                     (res->name != NULL)? res->name: "???");
213                 prom_halt();
214         }
215
216         va = res->start;
217         pa &= PAGE_MASK;
218         for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
219                 (*_sparc_mapioaddr)(pa, va, bus, 0);
220                 va += PAGE_SIZE;
221                 pa += PAGE_SIZE;
222         }
223
224         /*
225          * XXX Playing with implementation details here.
226          * On sparc64 Ebus has resources with precise boundaries.
227          * We share drivers with sparc64. Too clever drivers use
228          * start of a resource instead of a base address.
229          *
230          * XXX-2 This may be not valid anymore, clean when
231          * interface to sbus_ioremap() is resolved.
232          */
233         res->start += offset;
234         res->end = res->start + sz - 1;         /* not strictly necessary.. */
235
236         return (void *) res->start;
237 }
238
239 /*
240  * Comlimentary to _sparc_ioremap().
241  */
242 static void _sparc_free_io(struct resource *res)
243 {
244         unsigned long plen;
245
246         plen = res->end - res->start + 1;
247         plen = (plen + PAGE_SIZE-1) & PAGE_MASK;
248         while (plen != 0) {
249                 plen -= PAGE_SIZE;
250                 (*_sparc_unmapioaddr)(res->start + plen);
251         }
252
253         release_resource(res);
254 }
255
256 #ifdef CONFIG_SBUS
257
258 void sbus_set_sbus64(struct sbus_dev *sdev, int x) {
259         printk("sbus_set_sbus64: unsupported\n");
260 }
261
262 /*
263  * Allocate a chunk of memory suitable for DMA.
264  * Typically devices use them for control blocks.
265  * CPU may access them without any explicit flushing.
266  *
267  * XXX Some clever people know that sdev is not used and supply NULL. Watch.
268  */
269 void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
270 {
271         unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
272         unsigned long va;
273         struct resource *res;
274         int order;
275
276         /* XXX why are some lenghts signed, others unsigned? */
277         if (len <= 0) {
278                 return NULL;
279         }
280         /* XXX So what is maxphys for us and how do drivers know it? */
281         if (len > 256*1024) {                   /* __get_free_pages() limit */
282                 return NULL;
283         }
284
285         order = get_order(len_total);
286         va = __get_free_pages(GFP_KERNEL, order);
287         if (va == 0) {
288                 /*
289                  * printk here may be flooding... Consider removal XXX.
290                  */
291                 printk("sbus_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
292                 return NULL;
293         }
294
295         if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
296                 free_pages(va, order);
297                 printk("sbus_alloc_consistent: no core\n");
298                 return NULL;
299         }
300         memset((char*)res, 0, sizeof(struct resource));
301
302         if (allocate_resource(&_sparc_dvma, res, len_total,
303             _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
304                 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
305                 free_pages(va, order);
306                 kfree(res);
307                 return NULL;
308         }
309
310         mmu_map_dma_area(va, res->start, len_total);
311
312         *dma_addrp = res->start;
313         return (void *)res->start;
314 }
315
316 void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
317 {
318         struct resource *res;
319         unsigned long pgp;
320
321         if ((res = _sparc_find_resource(&_sparc_dvma,
322             (unsigned long)p)) == NULL) {
323                 printk("sbus_free_consistent: cannot free %p\n", p);
324                 return;
325         }
326
327         if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
328                 printk("sbus_free_consistent: unaligned va %p\n", p);
329                 return;
330         }
331
332         n = (n + PAGE_SIZE-1) & PAGE_MASK;
333         if ((res->end-res->start)+1 != n) {
334                 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
335                     (long)((res->end-res->start)+1), n);
336                 return;
337         }
338
339         release_resource(res);
340         kfree(res);
341
342         /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
343         pgp = (unsigned long) phys_to_virt(mmu_translate_dvma(ba));
344         mmu_unmap_dma_area(ba, n);
345
346         free_pages(pgp, get_order(n));
347 }
348
349 /*
350  * Map a chunk of memory so that devices can see it.
351  * CPU view of this memory may be inconsistent with
352  * a device view and explicit flushing is necessary.
353  */
354 dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
355 {
356 #if 0 /* This is the version that abuses consistent space */
357         unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
358         struct resource *res;
359
360         /* XXX why are some lenghts signed, others unsigned? */
361         if (len <= 0) {
362                 return 0;
363         }
364         /* XXX So what is maxphys for us and how do drivers know it? */
365         if (len > 256*1024) {                   /* __get_free_pages() limit */
366                 return 0;
367         }
368
369         if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
370                 printk("sbus_map_single: no core\n");
371                 return 0;
372         }
373         memset((char*)res, 0, sizeof(struct resource));
374         res->name = va; /* XXX */
375
376         if (allocate_resource(&_sparc_dvma, res, len_total,
377             _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE) != 0) {
378                 printk("sbus_map_single: cannot occupy 0x%lx", len);
379                 kfree(res);
380                 return 0;
381         }
382
383         mmu_map_dma_area(va, res->start, len_total);
384         mmu_flush_dma_area((unsigned long)va, len_total); /* in all contexts? */
385
386         return res->start;
387 #endif
388 #if 1 /* "trampoline" version */
389         /* XXX why are some lenghts signed, others unsigned? */
390         if (len <= 0) {
391                 return 0;
392         }
393         /* XXX So what is maxphys for us and how do drivers know it? */
394         if (len > 256*1024) {                   /* __get_free_pages() limit */
395                 return 0;
396         }
397         return mmu_get_scsi_one(va, len, sdev->bus);
398 #endif
399 }
400
401 void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
402 {
403 #if 0 /* This is the version that abuses consistent space */
404         struct resource *res;
405         unsigned long va;
406
407         if ((res = _sparc_find_resource(&_sparc_dvma, ba)) == NULL) {
408                 printk("sbus_unmap_single: cannot find %08x\n", (unsigned)ba);
409                 return;
410         }
411
412         n = (n + PAGE_SIZE-1) & PAGE_MASK;
413         if ((res->end-res->start)+1 != n) {
414                 printk("sbus_unmap_single: region 0x%lx asked 0x%lx\n",
415                     (long)((res->end-res->start)+1), n);
416                 return;
417         }
418
419         va = (unsigned long) res->name; /* XXX Ouch */
420         mmu_inval_dma_area(va, n);      /* in all contexts, mm's?... */
421         mmu_unmap_dma_area(ba, n);      /* iounit cache flush is here */
422         release_resource(res);
423         kfree(res);
424 #endif
425 #if 1 /* "trampoline" version */
426         mmu_release_scsi_one(ba, n, sdev->bus);
427 #endif
428 }
429
430 int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
431 {
432         mmu_get_scsi_sgl(sg, n, sdev->bus);
433
434         /*
435          * XXX sparc64 can return a partial length here. sun4c should do this
436          * but it currently panics if it can't fulfill the request - Anton
437          */
438         return n;
439 }
440
441 void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
442 {
443         mmu_release_scsi_sgl(sg, n, sdev->bus);
444 }
445
446 /*
447  */
448 void sbus_dma_sync_single(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
449 {
450 #if 0
451         unsigned long va;
452         struct resource *res;
453
454         /* We do not need the resource, just print a message if invalid. */
455         res = _sparc_find_resource(&_sparc_dvma, ba);
456         if (res == NULL)
457                 panic("sbus_dma_sync_single: 0x%x\n", ba);
458
459         va = (unsigned long) phys_to_virt(mmu_translate_dvma(ba));
460         /*
461          * XXX This bogosity will be fixed with the iommu rewrite coming soon
462          * to a kernel near you. - Anton
463          */
464         /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
465 #endif
466 }
467
468 void sbus_dma_sync_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
469 {
470         printk("sbus_dma_sync_sg: not implemented yet\n");
471 }
472 #endif /* CONFIG_SBUS */
473
474 #ifdef CONFIG_PCI
475
476 /* Allocate and map kernel buffer using consistent mode DMA for a device.
477  * hwdev should be valid struct pci_dev pointer for PCI devices.
478  */
479 void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
480 {
481         unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
482         unsigned long va;
483         struct resource *res;
484         int order;
485
486         if (len == 0) {
487                 return NULL;
488         }
489         if (len > 256*1024) {                   /* __get_free_pages() limit */
490                 return NULL;
491         }
492
493         order = get_order(len_total);
494         va = __get_free_pages(GFP_KERNEL, order);
495         if (va == 0) {
496                 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
497                 return NULL;
498         }
499
500         if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
501                 free_pages(va, order);
502                 printk("pci_alloc_consistent: no core\n");
503                 return NULL;
504         }
505         memset((char*)res, 0, sizeof(struct resource));
506
507         if (allocate_resource(&_sparc_dvma, res, len_total,
508             _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
509                 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
510                 free_pages(va, order);
511                 kfree(res);
512                 return NULL;
513         }
514
515         mmu_inval_dma_area(va, len_total);
516
517 #if 1
518 /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %x\n",
519   (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
520 #endif
521         {
522                 unsigned long xva, xpa;
523                 xva = res->start;
524                 xpa = virt_to_phys(va);
525                 while (len_total != 0) {
526                         len_total -= PAGE_SIZE;
527                         (*_sparc_mapioaddr)(xpa, xva, 0, 0);
528                         xva += PAGE_SIZE;
529                         xpa += PAGE_SIZE;
530                 }
531         }
532
533         *pba = virt_to_bus(va);
534         return (void *) res->start;
535 }
536
537 /* Free and unmap a consistent DMA buffer.
538  * cpu_addr is what was returned from pci_alloc_consistent,
539  * size must be the same as what as passed into pci_alloc_consistent,
540  * and likewise dma_addr must be the same as what *dma_addrp was set to.
541  *
542  * References to the memory and mappings assosciated with cpu_addr/dma_addr
543  * past this call are illegal.
544  */
545 void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
546 {
547         struct resource *res;
548         unsigned long pgp;
549
550         if ((res = _sparc_find_resource(&_sparc_dvma,
551             (unsigned long)p)) == NULL) {
552                 printk("pci_free_consistent: cannot free %p\n", p);
553                 return;
554         }
555
556         if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
557                 printk("pci_free_consistent: unaligned va %p\n", p);
558                 return;
559         }
560
561         n = (n + PAGE_SIZE-1) & PAGE_MASK;
562         if ((res->end-res->start)+1 != n) {
563                 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
564                     (long)((res->end-res->start)+1), (long)n);
565                 return;
566         }
567
568         pgp = (unsigned long) bus_to_virt(ba);
569         mmu_inval_dma_area(pgp, n);
570         {
571                 int x;
572                 for (x = 0; x < n; x += PAGE_SIZE) {
573                         (*_sparc_unmapioaddr)((unsigned long)p + n);
574                 }
575         }
576
577         release_resource(res);
578         kfree(res);
579
580         free_pages(pgp, get_order(n));
581 }
582
583 /* Map a single buffer of the indicated size for DMA in streaming mode.
584  * The 32-bit bus address to use is returned.
585  *
586  * Once the device is given the dma address, the device owns this memory
587  * until either pci_unmap_single or pci_dma_sync_single is performed.
588  */
589 dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
590     int direction)
591 {
592         if (direction == PCI_DMA_NONE)
593                 BUG();
594         /* IIep is write-through, not flushing. */
595         return virt_to_bus(ptr);
596 }
597
598 /* Unmap a single streaming mode DMA translation.  The dma_addr and size
599  * must match what was provided for in a previous pci_map_single call.  All
600  * other usages are undefined.
601  *
602  * After this call, reads by the cpu to the buffer are guarenteed to see
603  * whatever the device wrote there.
604  */
605 void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
606     int direction)
607 {
608         if (direction == PCI_DMA_NONE)
609                 BUG();
610         if (direction != PCI_DMA_TODEVICE) {
611                 mmu_inval_dma_area((unsigned long)bus_to_virt(ba),
612                     (size + PAGE_SIZE-1) & PAGE_MASK);
613         }
614 }
615
616 /* Map a set of buffers described by scatterlist in streaming
617  * mode for DMA.  This is the scather-gather version of the
618  * above pci_map_single interface.  Here the scatter gather list
619  * elements are each tagged with the appropriate dma address
620  * and length.  They are obtained via sg_dma_{address,length}(SG).
621  *
622  * NOTE: An implementation may be able to use a smaller number of
623  *       DMA address/length pairs than there are SG table elements.
624  *       (for example via virtual mapping capabilities)
625  *       The routine returns the number of addr/length pairs actually
626  *       used, at most nents.
627  *
628  * Device ownership issues as mentioned above for pci_map_single are
629  * the same here.
630  */
631 int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
632     int direction)
633 {
634         int n;
635
636         if (direction == PCI_DMA_NONE)
637                 BUG();
638         /* IIep is write-through, not flushing. */
639         for (n = 0; n < nents; n++) {
640                 sg->dvma_address = virt_to_bus(sg->address);
641                 sg->dvma_length = sg->length;
642                 sg++;
643         }
644         return nents;
645 }
646
647 /* Unmap a set of streaming mode DMA translations.
648  * Again, cpu read rules concerning calls here are the same as for
649  * pci_unmap_single() above.
650  */
651 void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
652     int direction)
653 {
654         int n;
655
656         if (direction == PCI_DMA_NONE)
657                 BUG();
658         if (direction != PCI_DMA_TODEVICE) {
659                 for (n = 0; n < nents; n++) {
660                         mmu_inval_dma_area((unsigned long)sg->address,
661                             (sg->length + PAGE_SIZE-1) & PAGE_MASK);
662                         sg++;
663                 }
664         }
665 }
666
667 /* Make physical memory consistent for a single
668  * streaming mode DMA translation before or after a transfer.
669  *
670  * If you perform a pci_map_single() but wish to interrogate the
671  * buffer using the cpu, yet do not wish to teardown the PCI dma
672  * mapping, you must call this function before doing so.  At the
673  * next point you give the PCI dma address back to the card, the
674  * device again owns the buffer.
675  */
676 void pci_dma_sync_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
677 {
678         if (direction == PCI_DMA_NONE)
679                 BUG();
680         if (direction != PCI_DMA_TODEVICE) {
681                 mmu_inval_dma_area((unsigned long)bus_to_virt(ba),
682                     (size + PAGE_SIZE-1) & PAGE_MASK);
683         }
684 }
685
686 /* Make physical memory consistent for a set of streaming
687  * mode DMA translations after a transfer.
688  *
689  * The same as pci_dma_sync_single but for a scatter-gather list,
690  * same rules and usage.
691  */
692 void pci_dma_sync_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
693 {
694         int n;
695
696         if (direction == PCI_DMA_NONE)
697                 BUG();
698         if (direction != PCI_DMA_TODEVICE) {
699                 for (n = 0; n < nents; n++) {
700                         mmu_inval_dma_area((unsigned long)sg->address,
701                             (sg->length + PAGE_SIZE-1) & PAGE_MASK);
702                         sg++;
703                 }
704         }
705 }
706 #endif /* CONFIG_PCI */
707
708 #ifdef CONFIG_PROC_FS
709
710 static int
711 _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
712     void *data)
713 {
714         char *p = buf, *e = buf + length;
715         struct resource *r;
716         const char *nm;
717
718         for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
719                 if (p + 32 >= e)        /* Better than nothing */
720                         break;
721                 if ((nm = r->name) == 0) nm = "???";
722                 p += sprintf(p, "%08lx-%08lx: %s\n", r->start, r->end, nm);
723         }
724
725         return p-buf;
726 }
727
728 #endif /* CONFIG_PROC_FS */
729
730 /*
731  * This is a version of find_resource and it belongs to kernel/resource.c.
732  * Until we have agreement with Linus and Martin, it lingers here.
733  *
734  * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
735  * This probably warrants some sort of hashing.
736  */
737 struct resource *
738 _sparc_find_resource(struct resource *root, unsigned long hit)
739 {
740         struct resource *tmp;
741
742         for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
743                 if (tmp->start <= hit && tmp->end >= hit)
744                         return tmp;
745         }
746         return NULL;
747 }
748
749 /*
750  * Necessary boot time initializations.
751  */
752
753 void ioport_init(void)
754 {
755         extern void sun4c_mapioaddr(unsigned long, unsigned long, int, int);
756         extern void srmmu_mapioaddr(unsigned long, unsigned long, int, int);
757         extern void sun4c_unmapioaddr(unsigned long);
758         extern void srmmu_unmapioaddr(unsigned long);
759
760         switch(sparc_cpu_model) {
761         case sun4c:
762         case sun4:
763         case sun4e:
764                 _sparc_mapioaddr = sun4c_mapioaddr;
765                 _sparc_unmapioaddr = sun4c_unmapioaddr;
766                 break;
767         case sun4m:
768         case sun4d:
769                 _sparc_mapioaddr = srmmu_mapioaddr;
770                 _sparc_unmapioaddr = srmmu_unmapioaddr;
771                 break;
772         default:
773                 printk("ioport_init: cpu type %d is unknown.\n",
774                     sparc_cpu_model);
775                 halt();
776         };
777
778 }
779
780 void register_proc_sparc_ioport(void)
781 {
782 #ifdef CONFIG_PROC_FS
783         create_proc_read_entry("io_map",0,0,_sparc_io_get_info,&sparc_iomap);
784         create_proc_read_entry("dvma_map",0,0,_sparc_io_get_info,&_sparc_dvma);
785 #endif
786 }