xref: /openbmc/linux/arch/sparc/kernel/ioport.c (revision e8e0929d)
1 /*
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 #include <linux/seq_file.h>
39 #include <linux/scatterlist.h>
40 #include <linux/of_device.h>
41 
42 #include <asm/io.h>
43 #include <asm/vaddrs.h>
44 #include <asm/oplib.h>
45 #include <asm/prom.h>
46 #include <asm/page.h>
47 #include <asm/pgalloc.h>
48 #include <asm/dma.h>
49 #include <asm/iommu.h>
50 #include <asm/io-unit.h>
51 
52 #define mmu_inval_dma_area(p, l)	/* Anton pulled it out for 2.4.0-xx */
53 
54 static struct resource *_sparc_find_resource(struct resource *r,
55 					     unsigned long);
56 
57 static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
58 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
59     unsigned long size, char *name);
60 static void _sparc_free_io(struct resource *res);
61 
62 static void register_proc_sparc_ioport(void);
63 
64 /* This points to the next to use virtual memory for DVMA mappings */
65 static struct resource _sparc_dvma = {
66 	.name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
67 };
68 /* This points to the start of I/O mappings, cluable from outside. */
69 /*ext*/ struct resource sparc_iomap = {
70 	.name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
71 };
72 
73 /*
74  * Our mini-allocator...
75  * Boy this is gross! We need it because we must map I/O for
76  * timers and interrupt controller before the kmalloc is available.
77  */
78 
79 #define XNMLN  15
80 #define XNRES  10	/* SS-10 uses 8 */
81 
82 struct xresource {
83 	struct resource xres;	/* Must be first */
84 	int xflag;		/* 1 == used */
85 	char xname[XNMLN+1];
86 };
87 
88 static struct xresource xresv[XNRES];
89 
90 static struct xresource *xres_alloc(void) {
91 	struct xresource *xrp;
92 	int n;
93 
94 	xrp = xresv;
95 	for (n = 0; n < XNRES; n++) {
96 		if (xrp->xflag == 0) {
97 			xrp->xflag = 1;
98 			return xrp;
99 		}
100 		xrp++;
101 	}
102 	return NULL;
103 }
104 
105 static void xres_free(struct xresource *xrp) {
106 	xrp->xflag = 0;
107 }
108 
109 /*
110  * These are typically used in PCI drivers
111  * which are trying to be cross-platform.
112  *
113  * Bus type is always zero on IIep.
114  */
115 void __iomem *ioremap(unsigned long offset, unsigned long size)
116 {
117 	char name[14];
118 
119 	sprintf(name, "phys_%08x", (u32)offset);
120 	return _sparc_alloc_io(0, offset, size, name);
121 }
122 EXPORT_SYMBOL(ioremap);
123 
124 /*
125  * Comlimentary to ioremap().
126  */
127 void iounmap(volatile void __iomem *virtual)
128 {
129 	unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
130 	struct resource *res;
131 
132 	if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
133 		printk("free_io/iounmap: cannot free %lx\n", vaddr);
134 		return;
135 	}
136 	_sparc_free_io(res);
137 
138 	if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
139 		xres_free((struct xresource *)res);
140 	} else {
141 		kfree(res);
142 	}
143 }
144 EXPORT_SYMBOL(iounmap);
145 
146 void __iomem *of_ioremap(struct resource *res, unsigned long offset,
147 			 unsigned long size, char *name)
148 {
149 	return _sparc_alloc_io(res->flags & 0xF,
150 			       res->start + offset,
151 			       size, name);
152 }
153 EXPORT_SYMBOL(of_ioremap);
154 
155 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
156 {
157 	iounmap(base);
158 }
159 EXPORT_SYMBOL(of_iounmap);
160 
161 /*
162  * Meat of mapping
163  */
164 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
165     unsigned long size, char *name)
166 {
167 	static int printed_full;
168 	struct xresource *xres;
169 	struct resource *res;
170 	char *tack;
171 	int tlen;
172 	void __iomem *va;	/* P3 diag */
173 
174 	if (name == NULL) name = "???";
175 
176 	if ((xres = xres_alloc()) != 0) {
177 		tack = xres->xname;
178 		res = &xres->xres;
179 	} else {
180 		if (!printed_full) {
181 			printk("ioremap: done with statics, switching to malloc\n");
182 			printed_full = 1;
183 		}
184 		tlen = strlen(name);
185 		tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
186 		if (tack == NULL) return NULL;
187 		memset(tack, 0, sizeof(struct resource));
188 		res = (struct resource *) tack;
189 		tack += sizeof (struct resource);
190 	}
191 
192 	strlcpy(tack, name, XNMLN+1);
193 	res->name = tack;
194 
195 	va = _sparc_ioremap(res, busno, phys, size);
196 	/* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
197 	return va;
198 }
199 
200 /*
201  */
202 static void __iomem *
203 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
204 {
205 	unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
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 	pa &= PAGE_MASK;
217 	sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
218 
219 	return (void __iomem *)(unsigned long)(res->start + offset);
220 }
221 
222 /*
223  * Comlimentary to _sparc_ioremap().
224  */
225 static void _sparc_free_io(struct resource *res)
226 {
227 	unsigned long plen;
228 
229 	plen = res->end - res->start + 1;
230 	BUG_ON((plen & (PAGE_SIZE-1)) != 0);
231 	sparc_unmapiorange(res->start, plen);
232 	release_resource(res);
233 }
234 
235 #ifdef CONFIG_SBUS
236 
237 void sbus_set_sbus64(struct device *dev, int x)
238 {
239 	printk("sbus_set_sbus64: unsupported\n");
240 }
241 EXPORT_SYMBOL(sbus_set_sbus64);
242 
243 /*
244  * Allocate a chunk of memory suitable for DMA.
245  * Typically devices use them for control blocks.
246  * CPU may access them without any explicit flushing.
247  */
248 static void *sbus_alloc_coherent(struct device *dev, size_t len,
249 				 dma_addr_t *dma_addrp, gfp_t gfp)
250 {
251 	struct of_device *op = to_of_device(dev);
252 	unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
253 	unsigned long va;
254 	struct resource *res;
255 	int order;
256 
257 	/* XXX why are some lengths signed, others unsigned? */
258 	if (len <= 0) {
259 		return NULL;
260 	}
261 	/* XXX So what is maxphys for us and how do drivers know it? */
262 	if (len > 256*1024) {			/* __get_free_pages() limit */
263 		return NULL;
264 	}
265 
266 	order = get_order(len_total);
267 	if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
268 		goto err_nopages;
269 
270 	if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
271 		goto err_nomem;
272 
273 	if (allocate_resource(&_sparc_dvma, res, len_total,
274 	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
275 		printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
276 		goto err_nova;
277 	}
278 	mmu_inval_dma_area(va, len_total);
279 	// XXX The mmu_map_dma_area does this for us below, see comments.
280 	// sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
281 	/*
282 	 * XXX That's where sdev would be used. Currently we load
283 	 * all iommu tables with the same translations.
284 	 */
285 	if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
286 		goto err_noiommu;
287 
288 	res->name = op->node->name;
289 
290 	return (void *)(unsigned long)res->start;
291 
292 err_noiommu:
293 	release_resource(res);
294 err_nova:
295 	free_pages(va, order);
296 err_nomem:
297 	kfree(res);
298 err_nopages:
299 	return NULL;
300 }
301 
302 static void sbus_free_coherent(struct device *dev, size_t n, void *p,
303 			       dma_addr_t ba)
304 {
305 	struct resource *res;
306 	struct page *pgv;
307 
308 	if ((res = _sparc_find_resource(&_sparc_dvma,
309 	    (unsigned long)p)) == NULL) {
310 		printk("sbus_free_consistent: cannot free %p\n", p);
311 		return;
312 	}
313 
314 	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
315 		printk("sbus_free_consistent: unaligned va %p\n", p);
316 		return;
317 	}
318 
319 	n = (n + PAGE_SIZE-1) & PAGE_MASK;
320 	if ((res->end-res->start)+1 != n) {
321 		printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n",
322 		    (long)((res->end-res->start)+1), n);
323 		return;
324 	}
325 
326 	release_resource(res);
327 	kfree(res);
328 
329 	/* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
330 	pgv = virt_to_page(p);
331 	mmu_unmap_dma_area(dev, ba, n);
332 
333 	__free_pages(pgv, get_order(n));
334 }
335 
336 /*
337  * Map a chunk of memory so that devices can see it.
338  * CPU view of this memory may be inconsistent with
339  * a device view and explicit flushing is necessary.
340  */
341 static dma_addr_t sbus_map_page(struct device *dev, struct page *page,
342 				unsigned long offset, size_t len,
343 				enum dma_data_direction dir,
344 				struct dma_attrs *attrs)
345 {
346 	void *va = page_address(page) + offset;
347 
348 	/* XXX why are some lengths signed, others unsigned? */
349 	if (len <= 0) {
350 		return 0;
351 	}
352 	/* XXX So what is maxphys for us and how do drivers know it? */
353 	if (len > 256*1024) {			/* __get_free_pages() limit */
354 		return 0;
355 	}
356 	return mmu_get_scsi_one(dev, va, len);
357 }
358 
359 static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n,
360 			    enum dma_data_direction dir, struct dma_attrs *attrs)
361 {
362 	mmu_release_scsi_one(dev, ba, n);
363 }
364 
365 static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n,
366 		       enum dma_data_direction dir, struct dma_attrs *attrs)
367 {
368 	mmu_get_scsi_sgl(dev, sg, n);
369 
370 	/*
371 	 * XXX sparc64 can return a partial length here. sun4c should do this
372 	 * but it currently panics if it can't fulfill the request - Anton
373 	 */
374 	return n;
375 }
376 
377 static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n,
378 			  enum dma_data_direction dir, struct dma_attrs *attrs)
379 {
380 	mmu_release_scsi_sgl(dev, sg, n);
381 }
382 
383 static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
384 				 int n,	enum dma_data_direction dir)
385 {
386 	BUG();
387 }
388 
389 static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
390 				    int n, enum dma_data_direction dir)
391 {
392 	BUG();
393 }
394 
395 struct dma_map_ops sbus_dma_ops = {
396 	.alloc_coherent		= sbus_alloc_coherent,
397 	.free_coherent		= sbus_free_coherent,
398 	.map_page		= sbus_map_page,
399 	.unmap_page		= sbus_unmap_page,
400 	.map_sg			= sbus_map_sg,
401 	.unmap_sg		= sbus_unmap_sg,
402 	.sync_sg_for_cpu	= sbus_sync_sg_for_cpu,
403 	.sync_sg_for_device	= sbus_sync_sg_for_device,
404 };
405 
406 struct dma_map_ops *dma_ops = &sbus_dma_ops;
407 EXPORT_SYMBOL(dma_ops);
408 
409 static int __init sparc_register_ioport(void)
410 {
411 	register_proc_sparc_ioport();
412 
413 	return 0;
414 }
415 
416 arch_initcall(sparc_register_ioport);
417 
418 #endif /* CONFIG_SBUS */
419 
420 #ifdef CONFIG_PCI
421 
422 /* Allocate and map kernel buffer using consistent mode DMA for a device.
423  * hwdev should be valid struct pci_dev pointer for PCI devices.
424  */
425 static void *pci32_alloc_coherent(struct device *dev, size_t len,
426 				  dma_addr_t *pba, gfp_t gfp)
427 {
428 	unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
429 	unsigned long va;
430 	struct resource *res;
431 	int order;
432 
433 	if (len == 0) {
434 		return NULL;
435 	}
436 	if (len > 256*1024) {			/* __get_free_pages() limit */
437 		return NULL;
438 	}
439 
440 	order = get_order(len_total);
441 	va = __get_free_pages(GFP_KERNEL, order);
442 	if (va == 0) {
443 		printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
444 		return NULL;
445 	}
446 
447 	if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
448 		free_pages(va, order);
449 		printk("pci_alloc_consistent: no core\n");
450 		return NULL;
451 	}
452 
453 	if (allocate_resource(&_sparc_dvma, res, len_total,
454 	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
455 		printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
456 		free_pages(va, order);
457 		kfree(res);
458 		return NULL;
459 	}
460 	mmu_inval_dma_area(va, len_total);
461 #if 0
462 /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
463   (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
464 #endif
465 	sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
466 
467 	*pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
468 	return (void *) res->start;
469 }
470 
471 /* Free and unmap a consistent DMA buffer.
472  * cpu_addr is what was returned from pci_alloc_consistent,
473  * size must be the same as what as passed into pci_alloc_consistent,
474  * and likewise dma_addr must be the same as what *dma_addrp was set to.
475  *
476  * References to the memory and mappings associated with cpu_addr/dma_addr
477  * past this call are illegal.
478  */
479 static void pci32_free_coherent(struct device *dev, size_t n, void *p,
480 				dma_addr_t ba)
481 {
482 	struct resource *res;
483 	unsigned long pgp;
484 
485 	if ((res = _sparc_find_resource(&_sparc_dvma,
486 	    (unsigned long)p)) == NULL) {
487 		printk("pci_free_consistent: cannot free %p\n", p);
488 		return;
489 	}
490 
491 	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
492 		printk("pci_free_consistent: unaligned va %p\n", p);
493 		return;
494 	}
495 
496 	n = (n + PAGE_SIZE-1) & PAGE_MASK;
497 	if ((res->end-res->start)+1 != n) {
498 		printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
499 		    (long)((res->end-res->start)+1), (long)n);
500 		return;
501 	}
502 
503 	pgp = (unsigned long) phys_to_virt(ba);	/* bus_to_virt actually */
504 	mmu_inval_dma_area(pgp, n);
505 	sparc_unmapiorange((unsigned long)p, n);
506 
507 	release_resource(res);
508 	kfree(res);
509 
510 	free_pages(pgp, get_order(n));
511 }
512 
513 /*
514  * Same as pci_map_single, but with pages.
515  */
516 static dma_addr_t pci32_map_page(struct device *dev, struct page *page,
517 				 unsigned long offset, size_t size,
518 				 enum dma_data_direction dir,
519 				 struct dma_attrs *attrs)
520 {
521 	/* IIep is write-through, not flushing. */
522 	return page_to_phys(page) + offset;
523 }
524 
525 /* Map a set of buffers described by scatterlist in streaming
526  * mode for DMA.  This is the scather-gather version of the
527  * above pci_map_single interface.  Here the scatter gather list
528  * elements are each tagged with the appropriate dma address
529  * and length.  They are obtained via sg_dma_{address,length}(SG).
530  *
531  * NOTE: An implementation may be able to use a smaller number of
532  *       DMA address/length pairs than there are SG table elements.
533  *       (for example via virtual mapping capabilities)
534  *       The routine returns the number of addr/length pairs actually
535  *       used, at most nents.
536  *
537  * Device ownership issues as mentioned above for pci_map_single are
538  * the same here.
539  */
540 static int pci32_map_sg(struct device *device, struct scatterlist *sgl,
541 			int nents, enum dma_data_direction dir,
542 			struct dma_attrs *attrs)
543 {
544 	struct scatterlist *sg;
545 	int n;
546 
547 	/* IIep is write-through, not flushing. */
548 	for_each_sg(sgl, sg, nents, n) {
549 		BUG_ON(page_address(sg_page(sg)) == NULL);
550 		sg->dma_address = virt_to_phys(sg_virt(sg));
551 		sg->dma_length = sg->length;
552 	}
553 	return nents;
554 }
555 
556 /* Unmap a set of streaming mode DMA translations.
557  * Again, cpu read rules concerning calls here are the same as for
558  * pci_unmap_single() above.
559  */
560 static void pci32_unmap_sg(struct device *dev, struct scatterlist *sgl,
561 			   int nents, enum dma_data_direction dir,
562 			   struct dma_attrs *attrs)
563 {
564 	struct scatterlist *sg;
565 	int n;
566 
567 	if (dir != PCI_DMA_TODEVICE) {
568 		for_each_sg(sgl, sg, nents, n) {
569 			BUG_ON(page_address(sg_page(sg)) == NULL);
570 			mmu_inval_dma_area(
571 			    (unsigned long) page_address(sg_page(sg)),
572 			    (sg->length + PAGE_SIZE-1) & PAGE_MASK);
573 		}
574 	}
575 }
576 
577 /* Make physical memory consistent for a single
578  * streaming mode DMA translation before or after a transfer.
579  *
580  * If you perform a pci_map_single() but wish to interrogate the
581  * buffer using the cpu, yet do not wish to teardown the PCI dma
582  * mapping, you must call this function before doing so.  At the
583  * next point you give the PCI dma address back to the card, you
584  * must first perform a pci_dma_sync_for_device, and then the
585  * device again owns the buffer.
586  */
587 static void pci32_sync_single_for_cpu(struct device *dev, dma_addr_t ba,
588 				      size_t size, enum dma_data_direction dir)
589 {
590 	if (dir != PCI_DMA_TODEVICE) {
591 		mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
592 		    (size + PAGE_SIZE-1) & PAGE_MASK);
593 	}
594 }
595 
596 static void pci32_sync_single_for_device(struct device *dev, dma_addr_t ba,
597 					 size_t size, enum dma_data_direction dir)
598 {
599 	if (dir != PCI_DMA_TODEVICE) {
600 		mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
601 		    (size + PAGE_SIZE-1) & PAGE_MASK);
602 	}
603 }
604 
605 /* Make physical memory consistent for a set of streaming
606  * mode DMA translations after a transfer.
607  *
608  * The same as pci_dma_sync_single_* but for a scatter-gather list,
609  * same rules and usage.
610  */
611 static void pci32_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
612 				  int nents, enum dma_data_direction dir)
613 {
614 	struct scatterlist *sg;
615 	int n;
616 
617 	if (dir != PCI_DMA_TODEVICE) {
618 		for_each_sg(sgl, sg, nents, n) {
619 			BUG_ON(page_address(sg_page(sg)) == NULL);
620 			mmu_inval_dma_area(
621 			    (unsigned long) page_address(sg_page(sg)),
622 			    (sg->length + PAGE_SIZE-1) & PAGE_MASK);
623 		}
624 	}
625 }
626 
627 static void pci32_sync_sg_for_device(struct device *device, struct scatterlist *sgl,
628 				     int nents, enum dma_data_direction dir)
629 {
630 	struct scatterlist *sg;
631 	int n;
632 
633 	if (dir != PCI_DMA_TODEVICE) {
634 		for_each_sg(sgl, sg, nents, n) {
635 			BUG_ON(page_address(sg_page(sg)) == NULL);
636 			mmu_inval_dma_area(
637 			    (unsigned long) page_address(sg_page(sg)),
638 			    (sg->length + PAGE_SIZE-1) & PAGE_MASK);
639 		}
640 	}
641 }
642 
643 struct dma_map_ops pci32_dma_ops = {
644 	.alloc_coherent		= pci32_alloc_coherent,
645 	.free_coherent		= pci32_free_coherent,
646 	.map_page		= pci32_map_page,
647 	.map_sg			= pci32_map_sg,
648 	.unmap_sg		= pci32_unmap_sg,
649 	.sync_single_for_cpu	= pci32_sync_single_for_cpu,
650 	.sync_single_for_device	= pci32_sync_single_for_device,
651 	.sync_sg_for_cpu	= pci32_sync_sg_for_cpu,
652 	.sync_sg_for_device	= pci32_sync_sg_for_device,
653 };
654 EXPORT_SYMBOL(pci32_dma_ops);
655 
656 #endif /* CONFIG_PCI */
657 
658 /*
659  * Return whether the given PCI device DMA address mask can be
660  * supported properly.  For example, if your device can only drive the
661  * low 24-bits during PCI bus mastering, then you would pass
662  * 0x00ffffff as the mask to this function.
663  */
664 int dma_supported(struct device *dev, u64 mask)
665 {
666 #ifdef CONFIG_PCI
667 	if (dev->bus == &pci_bus_type)
668 		return 1;
669 #endif
670 	return 0;
671 }
672 EXPORT_SYMBOL(dma_supported);
673 
674 int dma_set_mask(struct device *dev, u64 dma_mask)
675 {
676 #ifdef CONFIG_PCI
677 	if (dev->bus == &pci_bus_type)
678 		return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
679 #endif
680 	return -EOPNOTSUPP;
681 }
682 EXPORT_SYMBOL(dma_set_mask);
683 
684 
685 #ifdef CONFIG_PROC_FS
686 
687 static int sparc_io_proc_show(struct seq_file *m, void *v)
688 {
689 	struct resource *root = m->private, *r;
690 	const char *nm;
691 
692 	for (r = root->child; r != NULL; r = r->sibling) {
693 		if ((nm = r->name) == 0) nm = "???";
694 		seq_printf(m, "%016llx-%016llx: %s\n",
695 				(unsigned long long)r->start,
696 				(unsigned long long)r->end, nm);
697 	}
698 
699 	return 0;
700 }
701 
702 static int sparc_io_proc_open(struct inode *inode, struct file *file)
703 {
704 	return single_open(file, sparc_io_proc_show, PDE(inode)->data);
705 }
706 
707 static const struct file_operations sparc_io_proc_fops = {
708 	.owner		= THIS_MODULE,
709 	.open		= sparc_io_proc_open,
710 	.read		= seq_read,
711 	.llseek		= seq_lseek,
712 	.release	= single_release,
713 };
714 #endif /* CONFIG_PROC_FS */
715 
716 /*
717  * This is a version of find_resource and it belongs to kernel/resource.c.
718  * Until we have agreement with Linus and Martin, it lingers here.
719  *
720  * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
721  * This probably warrants some sort of hashing.
722  */
723 static struct resource *_sparc_find_resource(struct resource *root,
724 					     unsigned long hit)
725 {
726         struct resource *tmp;
727 
728 	for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
729 		if (tmp->start <= hit && tmp->end >= hit)
730 			return tmp;
731 	}
732 	return NULL;
733 }
734 
735 static void register_proc_sparc_ioport(void)
736 {
737 #ifdef CONFIG_PROC_FS
738 	proc_create_data("io_map", 0, NULL, &sparc_io_proc_fops, &sparc_iomap);
739 	proc_create_data("dvma_map", 0, NULL, &sparc_io_proc_fops, &_sparc_dvma);
740 #endif
741 }
742