Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / sparc / kernel / ioport.c
1 /* $Id: ioport.c,v 1.45 2001/10/30 04:54:21 davem 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/module.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/prom.h>
43 #include <asm/of_device.h>
44 #include <asm/sbus.h>
45 #include <asm/page.h>
46 #include <asm/pgalloc.h>
47 #include <asm/dma.h>
48
49 #define mmu_inval_dma_area(p, l)        /* Anton pulled it out for 2.4.0-xx */
50
51 struct resource *_sparc_find_resource(struct resource *r, unsigned long);
52
53 static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
54 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
55     unsigned long size, char *name);
56 static void _sparc_free_io(struct resource *res);
57
58 /* This points to the next to use virtual memory for DVMA mappings */
59 static struct resource _sparc_dvma = {
60         .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
61 };
62 /* This points to the start of I/O mappings, cluable from outside. */
63 /*ext*/ struct resource sparc_iomap = {
64         .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
65 };
66
67 /*
68  * Our mini-allocator...
69  * Boy this is gross! We need it because we must map I/O for
70  * timers and interrupt controller before the kmalloc is available.
71  */
72
73 #define XNMLN  15
74 #define XNRES  10       /* SS-10 uses 8 */
75
76 struct xresource {
77         struct resource xres;   /* Must be first */
78         int xflag;              /* 1 == used */
79         char xname[XNMLN+1];
80 };
81
82 static struct xresource xresv[XNRES];
83
84 static struct xresource *xres_alloc(void) {
85         struct xresource *xrp;
86         int n;
87
88         xrp = xresv;
89         for (n = 0; n < XNRES; n++) {
90                 if (xrp->xflag == 0) {
91                         xrp->xflag = 1;
92                         return xrp;
93                 }
94                 xrp++;
95         }
96         return NULL;
97 }
98
99 static void xres_free(struct xresource *xrp) {
100         xrp->xflag = 0;
101 }
102
103 /*
104  * These are typically used in PCI drivers
105  * which are trying to be cross-platform.
106  *
107  * Bus type is always zero on IIep.
108  */
109 void __iomem *ioremap(unsigned long offset, unsigned long size)
110 {
111         char name[14];
112
113         sprintf(name, "phys_%08x", (u32)offset);
114         return _sparc_alloc_io(0, offset, size, name);
115 }
116
117 /*
118  * Comlimentary to ioremap().
119  */
120 void iounmap(volatile void __iomem *virtual)
121 {
122         unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
123         struct resource *res;
124
125         if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
126                 printk("free_io/iounmap: cannot free %lx\n", vaddr);
127                 return;
128         }
129         _sparc_free_io(res);
130
131         if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
132                 xres_free((struct xresource *)res);
133         } else {
134                 kfree(res);
135         }
136 }
137
138 /*
139  */
140 void __iomem *sbus_ioremap(struct resource *phyres, unsigned long offset,
141     unsigned long size, char *name)
142 {
143         return _sparc_alloc_io(phyres->flags & 0xF,
144             phyres->start + offset, size, name);
145 }
146
147 void __iomem *of_ioremap(struct resource *res, unsigned long offset,
148                          unsigned long size, char *name)
149 {
150         return _sparc_alloc_io(res->flags & 0xF,
151                                res->start + offset,
152                                size, name);
153 }
154 EXPORT_SYMBOL(of_ioremap);
155
156 void of_iounmap(void __iomem *base, unsigned long size)
157 {
158         iounmap(base);
159 }
160 EXPORT_SYMBOL(of_iounmap);
161
162 /*
163  */
164 void sbus_iounmap(volatile void __iomem *addr, unsigned long size)
165 {
166         iounmap(addr);
167 }
168
169 /*
170  * Meat of mapping
171  */
172 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
173     unsigned long size, char *name)
174 {
175         static int printed_full;
176         struct xresource *xres;
177         struct resource *res;
178         char *tack;
179         int tlen;
180         void __iomem *va;       /* P3 diag */
181
182         if (name == NULL) name = "???";
183
184         if ((xres = xres_alloc()) != 0) {
185                 tack = xres->xname;
186                 res = &xres->xres;
187         } else {
188                 if (!printed_full) {
189                         printk("ioremap: done with statics, switching to malloc\n");
190                         printed_full = 1;
191                 }
192                 tlen = strlen(name);
193                 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
194                 if (tack == NULL) return NULL;
195                 memset(tack, 0, sizeof(struct resource));
196                 res = (struct resource *) tack;
197                 tack += sizeof (struct resource);
198         }
199
200         strlcpy(tack, name, XNMLN+1);
201         res->name = tack;
202
203         va = _sparc_ioremap(res, busno, phys, size);
204         /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
205         return va;
206 }
207
208 /*
209  */
210 static void __iomem *
211 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
212 {
213         unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
214
215         if (allocate_resource(&sparc_iomap, res,
216             (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
217             sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
218                 /* Usually we cannot see printks in this case. */
219                 prom_printf("alloc_io_res(%s): cannot occupy\n",
220                     (res->name != NULL)? res->name: "???");
221                 prom_halt();
222         }
223
224         pa &= PAGE_MASK;
225         sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
226
227         return (void __iomem *)(unsigned long)(res->start + offset);
228 }
229
230 /*
231  * Comlimentary to _sparc_ioremap().
232  */
233 static void _sparc_free_io(struct resource *res)
234 {
235         unsigned long plen;
236
237         plen = res->end - res->start + 1;
238         BUG_ON((plen & (PAGE_SIZE-1)) != 0);
239         sparc_unmapiorange(res->start, plen);
240         release_resource(res);
241 }
242
243 #ifdef CONFIG_SBUS
244
245 void sbus_set_sbus64(struct sbus_dev *sdev, int x)
246 {
247         printk("sbus_set_sbus64: unsupported\n");
248 }
249
250 extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
251 void __init sbus_fill_device_irq(struct sbus_dev *sdev)
252 {
253         struct linux_prom_irqs irqs[PROMINTR_MAX];
254         int len;
255
256         len = prom_getproperty(sdev->prom_node, "intr",
257                                (char *)irqs, sizeof(irqs));
258         if (len != -1) {
259                 sdev->num_irqs = len / 8;
260                 if (sdev->num_irqs == 0) {
261                         sdev->irqs[0] = 0;
262                 } else if (sparc_cpu_model == sun4d) {
263                         for (len = 0; len < sdev->num_irqs; len++)
264                                 sdev->irqs[len] =
265                                         sun4d_build_irq(sdev, irqs[len].pri);
266                 } else {
267                         for (len = 0; len < sdev->num_irqs; len++)
268                                 sdev->irqs[len] = irqs[len].pri;
269                 }
270         } else {
271                 int interrupts[PROMINTR_MAX];
272
273                 /* No "intr" node found-- check for "interrupts" node.
274                  * This node contains SBus interrupt levels, not IPLs
275                  * as in "intr", and no vector values.  We convert
276                  * SBus interrupt levels to PILs (platform specific).
277                  */
278                 len = prom_getproperty(sdev->prom_node, "interrupts",
279                                        (char *)interrupts, sizeof(interrupts));
280                 if (len == -1) {
281                         sdev->irqs[0] = 0;
282                         sdev->num_irqs = 0;
283                 } else {
284                         sdev->num_irqs = len / sizeof(int);
285                         for (len = 0; len < sdev->num_irqs; len++) {
286                                 sdev->irqs[len] =
287                                         sbint_to_irq(sdev, interrupts[len]);
288                         }
289                 }
290         } 
291 }
292
293 /*
294  * Allocate a chunk of memory suitable for DMA.
295  * Typically devices use them for control blocks.
296  * CPU may access them without any explicit flushing.
297  *
298  * XXX Some clever people know that sdev is not used and supply NULL. Watch.
299  */
300 void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
301 {
302         unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
303         unsigned long va;
304         struct resource *res;
305         int order;
306
307         /* XXX why are some lenghts signed, others unsigned? */
308         if (len <= 0) {
309                 return NULL;
310         }
311         /* XXX So what is maxphys for us and how do drivers know it? */
312         if (len > 256*1024) {                   /* __get_free_pages() limit */
313                 return NULL;
314         }
315
316         order = get_order(len_total);
317         if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
318                 goto err_nopages;
319
320         if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
321                 goto err_nomem;
322         memset((char*)res, 0, sizeof(struct resource));
323
324         if (allocate_resource(&_sparc_dvma, res, len_total,
325             _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
326                 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
327                 goto err_nova;
328         }
329         mmu_inval_dma_area(va, len_total);
330         // XXX The mmu_map_dma_area does this for us below, see comments.
331         // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
332         /*
333          * XXX That's where sdev would be used. Currently we load
334          * all iommu tables with the same translations.
335          */
336         if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0)
337                 goto err_noiommu;
338
339         /* Set the resource name, if known. */
340         if (sdev) {
341                 res->name = sdev->prom_name;
342         }
343
344         return (void *)(unsigned long)res->start;
345
346 err_noiommu:
347         release_resource(res);
348 err_nova:
349         free_pages(va, order);
350 err_nomem:
351         kfree(res);
352 err_nopages:
353         return NULL;
354 }
355
356 void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
357 {
358         struct resource *res;
359         struct page *pgv;
360
361         if ((res = _sparc_find_resource(&_sparc_dvma,
362             (unsigned long)p)) == NULL) {
363                 printk("sbus_free_consistent: cannot free %p\n", p);
364                 return;
365         }
366
367         if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
368                 printk("sbus_free_consistent: unaligned va %p\n", p);
369                 return;
370         }
371
372         n = (n + PAGE_SIZE-1) & PAGE_MASK;
373         if ((res->end-res->start)+1 != n) {
374                 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
375                     (long)((res->end-res->start)+1), n);
376                 return;
377         }
378
379         release_resource(res);
380         kfree(res);
381
382         /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
383         pgv = mmu_translate_dvma(ba);
384         mmu_unmap_dma_area(ba, n);
385
386         __free_pages(pgv, get_order(n));
387 }
388
389 /*
390  * Map a chunk of memory so that devices can see it.
391  * CPU view of this memory may be inconsistent with
392  * a device view and explicit flushing is necessary.
393  */
394 dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
395 {
396         /* XXX why are some lenghts signed, others unsigned? */
397         if (len <= 0) {
398                 return 0;
399         }
400         /* XXX So what is maxphys for us and how do drivers know it? */
401         if (len > 256*1024) {                   /* __get_free_pages() limit */
402                 return 0;
403         }
404         return mmu_get_scsi_one(va, len, sdev->bus);
405 }
406
407 void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
408 {
409         mmu_release_scsi_one(ba, n, sdev->bus);
410 }
411
412 int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
413 {
414         mmu_get_scsi_sgl(sg, n, sdev->bus);
415
416         /*
417          * XXX sparc64 can return a partial length here. sun4c should do this
418          * but it currently panics if it can't fulfill the request - Anton
419          */
420         return n;
421 }
422
423 void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
424 {
425         mmu_release_scsi_sgl(sg, n, sdev->bus);
426 }
427
428 /*
429  */
430 void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
431 {
432 #if 0
433         unsigned long va;
434         struct resource *res;
435
436         /* We do not need the resource, just print a message if invalid. */
437         res = _sparc_find_resource(&_sparc_dvma, ba);
438         if (res == NULL)
439                 panic("sbus_dma_sync_single: 0x%x\n", ba);
440
441         va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
442         /*
443          * XXX This bogosity will be fixed with the iommu rewrite coming soon
444          * to a kernel near you. - Anton
445          */
446         /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
447 #endif
448 }
449
450 void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
451 {
452 #if 0
453         unsigned long va;
454         struct resource *res;
455
456         /* We do not need the resource, just print a message if invalid. */
457         res = _sparc_find_resource(&_sparc_dvma, ba);
458         if (res == NULL)
459                 panic("sbus_dma_sync_single: 0x%x\n", ba);
460
461         va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
462         /*
463          * XXX This bogosity will be fixed with the iommu rewrite coming soon
464          * to a kernel near you. - Anton
465          */
466         /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
467 #endif
468 }
469
470 void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
471 {
472         printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n");
473 }
474
475 void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
476 {
477         printk("sbus_dma_sync_sg_for_device: not implemented yet\n");
478 }
479
480 /* Support code for sbus_init().  */
481 /*
482  * XXX This functions appears to be a distorted version of
483  * prom_sbus_ranges_init(), with all sun4d stuff cut away.
484  * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
485  */
486 /* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
487 void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
488 {
489         int parent_node = pn->node;
490
491         if (sparc_cpu_model == sun4d) {
492                 struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
493                 int num_iounit_ranges, len;
494
495                 len = prom_getproperty(parent_node, "ranges",
496                                        (char *) iounit_ranges,
497                                        sizeof (iounit_ranges));
498                 if (len != -1) {
499                         num_iounit_ranges =
500                                 (len / sizeof(struct linux_prom_ranges));
501                         prom_adjust_ranges(sbus->sbus_ranges,
502                                            sbus->num_sbus_ranges,
503                                            iounit_ranges, num_iounit_ranges);
504                 }
505         }
506 }
507
508 void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
509 {
510         struct device_node *parent = dp->parent;
511
512         if (sparc_cpu_model != sun4d &&
513             parent != NULL &&
514             !strcmp(parent->name, "iommu")) {
515                 extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
516
517                 iommu_init(parent->node, sbus);
518         }
519
520         if (sparc_cpu_model == sun4d) {
521                 extern void iounit_init(int sbi_node, int iounit_node,
522                                         struct sbus_bus *sbus);
523
524                 iounit_init(dp->node, parent->node, sbus);
525         }
526 }
527
528 void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
529 {
530         if (sparc_cpu_model == sun4d) {
531                 struct device_node *parent = dp->parent;
532
533                 sbus->devid = of_getintprop_default(parent, "device-id", 0);
534                 sbus->board = of_getintprop_default(parent, "board#", 0);
535         }
536 }
537
538 int __init sbus_arch_preinit(void)
539 {
540         extern void register_proc_sparc_ioport(void);
541
542         register_proc_sparc_ioport();
543
544 #ifdef CONFIG_SUN4
545         {
546                 extern void sun4_dvma_init(void);
547                 sun4_dvma_init();
548         }
549         return 1;
550 #else
551         return 0;
552 #endif
553 }
554
555 void __init sbus_arch_postinit(void)
556 {
557         if (sparc_cpu_model == sun4d) {
558                 extern void sun4d_init_sbi_irq(void);
559                 sun4d_init_sbi_irq();
560         }
561 }
562 #endif /* CONFIG_SBUS */
563
564 #ifdef CONFIG_PCI
565
566 /* Allocate and map kernel buffer using consistent mode DMA for a device.
567  * hwdev should be valid struct pci_dev pointer for PCI devices.
568  */
569 void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
570 {
571         unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
572         unsigned long va;
573         struct resource *res;
574         int order;
575
576         if (len == 0) {
577                 return NULL;
578         }
579         if (len > 256*1024) {                   /* __get_free_pages() limit */
580                 return NULL;
581         }
582
583         order = get_order(len_total);
584         va = __get_free_pages(GFP_KERNEL, order);
585         if (va == 0) {
586                 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
587                 return NULL;
588         }
589
590         if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
591                 free_pages(va, order);
592                 printk("pci_alloc_consistent: no core\n");
593                 return NULL;
594         }
595         memset((char*)res, 0, sizeof(struct resource));
596
597         if (allocate_resource(&_sparc_dvma, res, len_total,
598             _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
599                 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
600                 free_pages(va, order);
601                 kfree(res);
602                 return NULL;
603         }
604         mmu_inval_dma_area(va, len_total);
605 #if 0
606 /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
607   (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
608 #endif
609         sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
610
611         *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
612         return (void *) res->start;
613 }
614
615 /* Free and unmap a consistent DMA buffer.
616  * cpu_addr is what was returned from pci_alloc_consistent,
617  * size must be the same as what as passed into pci_alloc_consistent,
618  * and likewise dma_addr must be the same as what *dma_addrp was set to.
619  *
620  * References to the memory and mappings assosciated with cpu_addr/dma_addr
621  * past this call are illegal.
622  */
623 void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
624 {
625         struct resource *res;
626         unsigned long pgp;
627
628         if ((res = _sparc_find_resource(&_sparc_dvma,
629             (unsigned long)p)) == NULL) {
630                 printk("pci_free_consistent: cannot free %p\n", p);
631                 return;
632         }
633
634         if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
635                 printk("pci_free_consistent: unaligned va %p\n", p);
636                 return;
637         }
638
639         n = (n + PAGE_SIZE-1) & PAGE_MASK;
640         if ((res->end-res->start)+1 != n) {
641                 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
642                     (long)((res->end-res->start)+1), (long)n);
643                 return;
644         }
645
646         pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
647         mmu_inval_dma_area(pgp, n);
648         sparc_unmapiorange((unsigned long)p, n);
649
650         release_resource(res);
651         kfree(res);
652
653         free_pages(pgp, get_order(n));
654 }
655
656 /* Map a single buffer of the indicated size for DMA in streaming mode.
657  * The 32-bit bus address to use is returned.
658  *
659  * Once the device is given the dma address, the device owns this memory
660  * until either pci_unmap_single or pci_dma_sync_single_* is performed.
661  */
662 dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
663     int direction)
664 {
665         BUG_ON(direction == PCI_DMA_NONE);
666         /* IIep is write-through, not flushing. */
667         return virt_to_phys(ptr);
668 }
669
670 /* Unmap a single streaming mode DMA translation.  The dma_addr and size
671  * must match what was provided for in a previous pci_map_single call.  All
672  * other usages are undefined.
673  *
674  * After this call, reads by the cpu to the buffer are guaranteed to see
675  * whatever the device wrote there.
676  */
677 void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
678     int direction)
679 {
680         BUG_ON(direction == PCI_DMA_NONE);
681         if (direction != PCI_DMA_TODEVICE) {
682                 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
683                     (size + PAGE_SIZE-1) & PAGE_MASK);
684         }
685 }
686
687 /*
688  * Same as pci_map_single, but with pages.
689  */
690 dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
691                         unsigned long offset, size_t size, int direction)
692 {
693         BUG_ON(direction == PCI_DMA_NONE);
694         /* IIep is write-through, not flushing. */
695         return page_to_phys(page) + offset;
696 }
697
698 void pci_unmap_page(struct pci_dev *hwdev,
699                         dma_addr_t dma_address, size_t size, int direction)
700 {
701         BUG_ON(direction == PCI_DMA_NONE);
702         /* mmu_inval_dma_area XXX */
703 }
704
705 /* Map a set of buffers described by scatterlist in streaming
706  * mode for DMA.  This is the scather-gather version of the
707  * above pci_map_single interface.  Here the scatter gather list
708  * elements are each tagged with the appropriate dma address
709  * and length.  They are obtained via sg_dma_{address,length}(SG).
710  *
711  * NOTE: An implementation may be able to use a smaller number of
712  *       DMA address/length pairs than there are SG table elements.
713  *       (for example via virtual mapping capabilities)
714  *       The routine returns the number of addr/length pairs actually
715  *       used, at most nents.
716  *
717  * Device ownership issues as mentioned above for pci_map_single are
718  * the same here.
719  */
720 int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
721     int direction)
722 {
723         int n;
724
725         BUG_ON(direction == PCI_DMA_NONE);
726         /* IIep is write-through, not flushing. */
727         for (n = 0; n < nents; n++) {
728                 BUG_ON(page_address(sg->page) == NULL);
729                 sg->dvma_address = virt_to_phys(page_address(sg->page));
730                 sg->dvma_length = sg->length;
731                 sg++;
732         }
733         return nents;
734 }
735
736 /* Unmap a set of streaming mode DMA translations.
737  * Again, cpu read rules concerning calls here are the same as for
738  * pci_unmap_single() above.
739  */
740 void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
741     int direction)
742 {
743         int n;
744
745         BUG_ON(direction == PCI_DMA_NONE);
746         if (direction != PCI_DMA_TODEVICE) {
747                 for (n = 0; n < nents; n++) {
748                         BUG_ON(page_address(sg->page) == NULL);
749                         mmu_inval_dma_area(
750                             (unsigned long) page_address(sg->page),
751                             (sg->length + PAGE_SIZE-1) & PAGE_MASK);
752                         sg++;
753                 }
754         }
755 }
756
757 /* Make physical memory consistent for a single
758  * streaming mode DMA translation before or after a transfer.
759  *
760  * If you perform a pci_map_single() but wish to interrogate the
761  * buffer using the cpu, yet do not wish to teardown the PCI dma
762  * mapping, you must call this function before doing so.  At the
763  * next point you give the PCI dma address back to the card, you
764  * must first perform a pci_dma_sync_for_device, and then the
765  * device again owns the buffer.
766  */
767 void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
768 {
769         BUG_ON(direction == PCI_DMA_NONE);
770         if (direction != PCI_DMA_TODEVICE) {
771                 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
772                     (size + PAGE_SIZE-1) & PAGE_MASK);
773         }
774 }
775
776 void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
777 {
778         BUG_ON(direction == PCI_DMA_NONE);
779         if (direction != PCI_DMA_TODEVICE) {
780                 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
781                     (size + PAGE_SIZE-1) & PAGE_MASK);
782         }
783 }
784
785 /* Make physical memory consistent for a set of streaming
786  * mode DMA translations after a transfer.
787  *
788  * The same as pci_dma_sync_single_* but for a scatter-gather list,
789  * same rules and usage.
790  */
791 void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
792 {
793         int n;
794
795         BUG_ON(direction == PCI_DMA_NONE);
796         if (direction != PCI_DMA_TODEVICE) {
797                 for (n = 0; n < nents; n++) {
798                         BUG_ON(page_address(sg->page) == NULL);
799                         mmu_inval_dma_area(
800                             (unsigned long) page_address(sg->page),
801                             (sg->length + PAGE_SIZE-1) & PAGE_MASK);
802                         sg++;
803                 }
804         }
805 }
806
807 void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
808 {
809         int n;
810
811         BUG_ON(direction == PCI_DMA_NONE);
812         if (direction != PCI_DMA_TODEVICE) {
813                 for (n = 0; n < nents; n++) {
814                         BUG_ON(page_address(sg->page) == NULL);
815                         mmu_inval_dma_area(
816                             (unsigned long) page_address(sg->page),
817                             (sg->length + PAGE_SIZE-1) & PAGE_MASK);
818                         sg++;
819                 }
820         }
821 }
822 #endif /* CONFIG_PCI */
823
824 #ifdef CONFIG_PROC_FS
825
826 static int
827 _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
828     void *data)
829 {
830         char *p = buf, *e = buf + length;
831         struct resource *r;
832         const char *nm;
833
834         for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
835                 if (p + 32 >= e)        /* Better than nothing */
836                         break;
837                 if ((nm = r->name) == 0) nm = "???";
838                 p += sprintf(p, "%016llx-%016llx: %s\n",
839                                 (unsigned long long)r->start,
840                                 (unsigned long long)r->end, nm);
841         }
842
843         return p-buf;
844 }
845
846 #endif /* CONFIG_PROC_FS */
847
848 /*
849  * This is a version of find_resource and it belongs to kernel/resource.c.
850  * Until we have agreement with Linus and Martin, it lingers here.
851  *
852  * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
853  * This probably warrants some sort of hashing.
854  */
855 struct resource *
856 _sparc_find_resource(struct resource *root, unsigned long hit)
857 {
858         struct resource *tmp;
859
860         for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
861                 if (tmp->start <= hit && tmp->end >= hit)
862                         return tmp;
863         }
864         return NULL;
865 }
866
867 void register_proc_sparc_ioport(void)
868 {
869 #ifdef CONFIG_PROC_FS
870         create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
871         create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
872 #endif
873 }