1/* $Id: ioport.c,v 1.1.1.1 2007/08/03 18:52:17 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
51struct resource *_sparc_find_resource(struct resource *r, unsigned long);
52
53static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
54static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
55    unsigned long size, char *name);
56static void _sparc_free_io(struct resource *res);
57
58/* This points to the next to use virtual memory for DVMA mappings */
59static 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
76struct xresource {
77	struct resource xres;	/* Must be first */
78	int xflag;		/* 1 == used */
79	char xname[XNMLN+1];
80};
81
82static struct xresource xresv[XNRES];
83
84static 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
99static 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 */
109void __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 */
120void 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 */
140void __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
147void __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}
154EXPORT_SYMBOL(of_ioremap);
155
156void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
157{
158	iounmap(base);
159}
160EXPORT_SYMBOL(of_iounmap);
161
162/*
163 */
164void sbus_iounmap(volatile void __iomem *addr, unsigned long size)
165{
166	iounmap(addr);
167}
168
169/*
170 * Meat of mapping
171 */
172static 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 */
210static 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 */
233static 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
245void sbus_set_sbus64(struct sbus_dev *sdev, int x)
246{
247	printk("sbus_set_sbus64: unsupported\n");
248}
249
250extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
251void __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
293void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
294{
295	unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
296	unsigned long va;
297	struct resource *res;
298	int order;
299
300	if (len <= 0) {
301		return NULL;
302	}
303	if (len > 256*1024) {			/* __get_free_pages() limit */
304		return NULL;
305	}
306
307	order = get_order(len_total);
308	if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
309		goto err_nopages;
310
311	if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
312		goto err_nomem;
313
314	if (allocate_resource(&_sparc_dvma, res, len_total,
315	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
316		printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
317		goto err_nova;
318	}
319	mmu_inval_dma_area(va, len_total);
320	// sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
321	if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0)
322		goto err_noiommu;
323
324	/* Set the resource name, if known. */
325	if (sdev) {
326		res->name = sdev->prom_name;
327	}
328
329	return (void *)(unsigned long)res->start;
330
331err_noiommu:
332	release_resource(res);
333err_nova:
334	free_pages(va, order);
335err_nomem:
336	kfree(res);
337err_nopages:
338	return NULL;
339}
340
341void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
342{
343	struct resource *res;
344	struct page *pgv;
345
346	if ((res = _sparc_find_resource(&_sparc_dvma,
347	    (unsigned long)p)) == NULL) {
348		printk("sbus_free_consistent: cannot free %p\n", p);
349		return;
350	}
351
352	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
353		printk("sbus_free_consistent: unaligned va %p\n", p);
354		return;
355	}
356
357	n = (n + PAGE_SIZE-1) & PAGE_MASK;
358	if ((res->end-res->start)+1 != n) {
359		printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
360		    (long)((res->end-res->start)+1), n);
361		return;
362	}
363
364	release_resource(res);
365	kfree(res);
366
367	/* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
368	pgv = mmu_translate_dvma(ba);
369	mmu_unmap_dma_area(ba, n);
370
371	__free_pages(pgv, get_order(n));
372}
373
374/*
375 * Map a chunk of memory so that devices can see it.
376 * CPU view of this memory may be inconsistent with
377 * a device view and explicit flushing is necessary.
378 */
379dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
380{
381	if (len <= 0) {
382		return 0;
383	}
384	if (len > 256*1024) {			/* __get_free_pages() limit */
385		return 0;
386	}
387	return mmu_get_scsi_one(va, len, sdev->bus);
388}
389
390void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
391{
392	mmu_release_scsi_one(ba, n, sdev->bus);
393}
394
395int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
396{
397	mmu_get_scsi_sgl(sg, n, sdev->bus);
398
399	return n;
400}
401
402void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
403{
404	mmu_release_scsi_sgl(sg, n, sdev->bus);
405}
406
407/*
408 */
409void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
410{
411}
412
413void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
414{
415}
416
417void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
418{
419	printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n");
420}
421
422void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
423{
424	printk("sbus_dma_sync_sg_for_device: not implemented yet\n");
425}
426
427/* Support code for sbus_init().  */
428/* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
429void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
430{
431	int parent_node = pn->node;
432
433	if (sparc_cpu_model == sun4d) {
434		struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
435		int num_iounit_ranges, len;
436
437		len = prom_getproperty(parent_node, "ranges",
438				       (char *) iounit_ranges,
439				       sizeof (iounit_ranges));
440		if (len != -1) {
441			num_iounit_ranges =
442				(len / sizeof(struct linux_prom_ranges));
443			prom_adjust_ranges(sbus->sbus_ranges,
444					   sbus->num_sbus_ranges,
445					   iounit_ranges, num_iounit_ranges);
446		}
447	}
448}
449
450void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
451{
452#ifndef CONFIG_SUN4
453	struct device_node *parent = dp->parent;
454
455	if (sparc_cpu_model != sun4d &&
456	    parent != NULL &&
457	    !strcmp(parent->name, "iommu")) {
458		extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
459
460		iommu_init(parent->node, sbus);
461	}
462
463	if (sparc_cpu_model == sun4d) {
464		extern void iounit_init(int sbi_node, int iounit_node,
465					struct sbus_bus *sbus);
466
467		iounit_init(dp->node, parent->node, sbus);
468	}
469#endif
470}
471
472void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
473{
474	if (sparc_cpu_model == sun4d) {
475		struct device_node *parent = dp->parent;
476
477		sbus->devid = of_getintprop_default(parent, "device-id", 0);
478		sbus->board = of_getintprop_default(parent, "board#", 0);
479	}
480}
481
482int __init sbus_arch_preinit(void)
483{
484	extern void register_proc_sparc_ioport(void);
485
486	register_proc_sparc_ioport();
487
488#ifdef CONFIG_SUN4
489	{
490		extern void sun4_dvma_init(void);
491		sun4_dvma_init();
492	}
493	return 1;
494#else
495	return 0;
496#endif
497}
498
499void __init sbus_arch_postinit(void)
500{
501	if (sparc_cpu_model == sun4d) {
502		extern void sun4d_init_sbi_irq(void);
503		sun4d_init_sbi_irq();
504	}
505}
506#endif /* CONFIG_SBUS */
507
508#ifdef CONFIG_PCI
509
510/* Allocate and map kernel buffer using consistent mode DMA for a device.
511 * hwdev should be valid struct pci_dev pointer for PCI devices.
512 */
513void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
514{
515	unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
516	unsigned long va;
517	struct resource *res;
518	int order;
519
520	if (len == 0) {
521		return NULL;
522	}
523	if (len > 256*1024) {			/* __get_free_pages() limit */
524		return NULL;
525	}
526
527	order = get_order(len_total);
528	va = __get_free_pages(GFP_KERNEL, order);
529	if (va == 0) {
530		printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
531		return NULL;
532	}
533
534	if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
535		free_pages(va, order);
536		printk("pci_alloc_consistent: no core\n");
537		return NULL;
538	}
539
540	if (allocate_resource(&_sparc_dvma, res, len_total,
541	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
542		printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
543		free_pages(va, order);
544		kfree(res);
545		return NULL;
546	}
547	mmu_inval_dma_area(va, len_total);
548	sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
549
550	*pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
551	return (void *) res->start;
552}
553
554/* Free and unmap a consistent DMA buffer.
555 * cpu_addr is what was returned from pci_alloc_consistent,
556 * size must be the same as what as passed into pci_alloc_consistent,
557 * and likewise dma_addr must be the same as what *dma_addrp was set to.
558 *
559 * References to the memory and mappings associated with cpu_addr/dma_addr
560 * past this call are illegal.
561 */
562void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
563{
564	struct resource *res;
565	unsigned long pgp;
566
567	if ((res = _sparc_find_resource(&_sparc_dvma,
568	    (unsigned long)p)) == NULL) {
569		printk("pci_free_consistent: cannot free %p\n", p);
570		return;
571	}
572
573	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
574		printk("pci_free_consistent: unaligned va %p\n", p);
575		return;
576	}
577
578	n = (n + PAGE_SIZE-1) & PAGE_MASK;
579	if ((res->end-res->start)+1 != n) {
580		printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
581		    (long)((res->end-res->start)+1), (long)n);
582		return;
583	}
584
585	pgp = (unsigned long) phys_to_virt(ba);	/* bus_to_virt actually */
586	mmu_inval_dma_area(pgp, n);
587	sparc_unmapiorange((unsigned long)p, n);
588
589	release_resource(res);
590	kfree(res);
591
592	free_pages(pgp, get_order(n));
593}
594
595/* Map a single buffer of the indicated size for DMA in streaming mode.
596 * The 32-bit bus address to use is returned.
597 *
598 * Once the device is given the dma address, the device owns this memory
599 * until either pci_unmap_single or pci_dma_sync_single_* is performed.
600 */
601dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
602    int direction)
603{
604	BUG_ON(direction == PCI_DMA_NONE);
605	/* IIep is write-through, not flushing. */
606	return virt_to_phys(ptr);
607}
608
609/* Unmap a single streaming mode DMA translation.  The dma_addr and size
610 * must match what was provided for in a previous pci_map_single call.  All
611 * other usages are undefined.
612 *
613 * After this call, reads by the cpu to the buffer are guaranteed to see
614 * whatever the device wrote there.
615 */
616void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
617    int direction)
618{
619	BUG_ON(direction == PCI_DMA_NONE);
620	if (direction != PCI_DMA_TODEVICE) {
621		mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
622		    (size + PAGE_SIZE-1) & PAGE_MASK);
623	}
624}
625
626/*
627 * Same as pci_map_single, but with pages.
628 */
629dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
630			unsigned long offset, size_t size, int direction)
631{
632	BUG_ON(direction == PCI_DMA_NONE);
633	/* IIep is write-through, not flushing. */
634	return page_to_phys(page) + offset;
635}
636
637void pci_unmap_page(struct pci_dev *hwdev,
638			dma_addr_t dma_address, size_t size, int direction)
639{
640	BUG_ON(direction == PCI_DMA_NONE);
641}
642
643/* Map a set of buffers described by scatterlist in streaming
644 * mode for DMA.  This is the scather-gather version of the
645 * above pci_map_single interface.  Here the scatter gather list
646 * elements are each tagged with the appropriate dma address
647 * and length.  They are obtained via sg_dma_{address,length}(SG).
648 *
649 * NOTE: An implementation may be able to use a smaller number of
650 *       DMA address/length pairs than there are SG table elements.
651 *       (for example via virtual mapping capabilities)
652 *       The routine returns the number of addr/length pairs actually
653 *       used, at most nents.
654 *
655 * Device ownership issues as mentioned above for pci_map_single are
656 * the same here.
657 */
658int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
659    int direction)
660{
661	int n;
662
663	BUG_ON(direction == PCI_DMA_NONE);
664	/* IIep is write-through, not flushing. */
665	for (n = 0; n < nents; n++) {
666		BUG_ON(page_address(sg->page) == NULL);
667		sg->dvma_address =
668			virt_to_phys(page_address(sg->page)) + sg->offset;
669		sg->dvma_length = sg->length;
670		sg++;
671	}
672	return nents;
673}
674
675/* Unmap a set of streaming mode DMA translations.
676 * Again, cpu read rules concerning calls here are the same as for
677 * pci_unmap_single() above.
678 */
679void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
680    int direction)
681{
682	int n;
683
684	BUG_ON(direction == PCI_DMA_NONE);
685	if (direction != PCI_DMA_TODEVICE) {
686		for (n = 0; n < nents; n++) {
687			BUG_ON(page_address(sg->page) == NULL);
688			mmu_inval_dma_area(
689			    (unsigned long) page_address(sg->page),
690			    (sg->length + PAGE_SIZE-1) & PAGE_MASK);
691			sg++;
692		}
693	}
694}
695
696/* Make physical memory consistent for a single
697 * streaming mode DMA translation before or after a transfer.
698 *
699 * If you perform a pci_map_single() but wish to interrogate the
700 * buffer using the cpu, yet do not wish to teardown the PCI dma
701 * mapping, you must call this function before doing so.  At the
702 * next point you give the PCI dma address back to the card, you
703 * must first perform a pci_dma_sync_for_device, and then the
704 * device again owns the buffer.
705 */
706void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
707{
708	BUG_ON(direction == PCI_DMA_NONE);
709	if (direction != PCI_DMA_TODEVICE) {
710		mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
711		    (size + PAGE_SIZE-1) & PAGE_MASK);
712	}
713}
714
715void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
716{
717	BUG_ON(direction == PCI_DMA_NONE);
718	if (direction != PCI_DMA_TODEVICE) {
719		mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
720		    (size + PAGE_SIZE-1) & PAGE_MASK);
721	}
722}
723
724/* Make physical memory consistent for a set of streaming
725 * mode DMA translations after a transfer.
726 *
727 * The same as pci_dma_sync_single_* but for a scatter-gather list,
728 * same rules and usage.
729 */
730void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
731{
732	int n;
733
734	BUG_ON(direction == PCI_DMA_NONE);
735	if (direction != PCI_DMA_TODEVICE) {
736		for (n = 0; n < nents; n++) {
737			BUG_ON(page_address(sg->page) == NULL);
738			mmu_inval_dma_area(
739			    (unsigned long) page_address(sg->page),
740			    (sg->length + PAGE_SIZE-1) & PAGE_MASK);
741			sg++;
742		}
743	}
744}
745
746void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
747{
748	int n;
749
750	BUG_ON(direction == PCI_DMA_NONE);
751	if (direction != PCI_DMA_TODEVICE) {
752		for (n = 0; n < nents; n++) {
753			BUG_ON(page_address(sg->page) == NULL);
754			mmu_inval_dma_area(
755			    (unsigned long) page_address(sg->page),
756			    (sg->length + PAGE_SIZE-1) & PAGE_MASK);
757			sg++;
758		}
759	}
760}
761#endif /* CONFIG_PCI */
762
763#ifdef CONFIG_PROC_FS
764
765static int
766_sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
767    void *data)
768{
769	char *p = buf, *e = buf + length;
770	struct resource *r;
771	const char *nm;
772
773	for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
774		if (p + 32 >= e)	/* Better than nothing */
775			break;
776		if ((nm = r->name) == 0) nm = "???";
777		p += sprintf(p, "%016llx-%016llx: %s\n",
778				(unsigned long long)r->start,
779				(unsigned long long)r->end, nm);
780	}
781
782	return p-buf;
783}
784
785#endif /* CONFIG_PROC_FS */
786
787struct resource *
788_sparc_find_resource(struct resource *root, unsigned long hit)
789{
790        struct resource *tmp;
791
792	for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
793		if (tmp->start <= hit && tmp->end >= hit)
794			return tmp;
795	}
796	return NULL;
797}
798
799void register_proc_sparc_ioport(void)
800{
801#ifdef CONFIG_PROC_FS
802	create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
803	create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
804#endif
805}
806