blob: 39ff1e0c518347d51d5fbfd7ba23c07e022b297c [file] [log] [blame]
Adrian Bunk88278ca2008-05-19 16:53:02 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
David S. Miller3ca9fab2006-06-29 14:35:33 -070028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Jens Axboe0912a5d2007-05-14 15:44:38 +020038#include <linux/scatterlist.h>
Stephen Rothwell764f2572008-08-07 15:33:36 -070039#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/io.h>
42#include <asm/vaddrs.h>
43#include <asm/oplib.h>
David S. Miller576c3522006-06-23 15:55:45 -070044#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/page.h>
46#include <asm/pgalloc.h>
47#include <asm/dma.h>
David S. Millere0039342008-08-25 22:47:20 -070048#include <asm/iommu.h>
49#include <asm/io-unit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
David S. Miller944c67df2008-08-27 18:01:36 -070051#include "dma.h"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
54
Adrian Bunkc61c65c2008-06-05 11:40:58 -070055static struct resource *_sparc_find_resource(struct resource *r,
56 unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
59static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
60 unsigned long size, char *name);
61static void _sparc_free_io(struct resource *res);
62
Adrian Bunkc61c65c2008-06-05 11:40:58 -070063static void register_proc_sparc_ioport(void);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* This points to the next to use virtual memory for DVMA mappings */
66static struct resource _sparc_dvma = {
67 .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
68};
69/* This points to the start of I/O mappings, cluable from outside. */
70/*ext*/ struct resource sparc_iomap = {
71 .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
72};
73
74/*
75 * Our mini-allocator...
76 * Boy this is gross! We need it because we must map I/O for
77 * timers and interrupt controller before the kmalloc is available.
78 */
79
80#define XNMLN 15
81#define XNRES 10 /* SS-10 uses 8 */
82
83struct xresource {
84 struct resource xres; /* Must be first */
85 int xflag; /* 1 == used */
86 char xname[XNMLN+1];
87};
88
89static struct xresource xresv[XNRES];
90
91static struct xresource *xres_alloc(void) {
92 struct xresource *xrp;
93 int n;
94
95 xrp = xresv;
96 for (n = 0; n < XNRES; n++) {
97 if (xrp->xflag == 0) {
98 xrp->xflag = 1;
99 return xrp;
100 }
101 xrp++;
102 }
103 return NULL;
104}
105
106static void xres_free(struct xresource *xrp) {
107 xrp->xflag = 0;
108}
109
110/*
111 * These are typically used in PCI drivers
112 * which are trying to be cross-platform.
113 *
114 * Bus type is always zero on IIep.
115 */
116void __iomem *ioremap(unsigned long offset, unsigned long size)
117{
118 char name[14];
119
120 sprintf(name, "phys_%08x", (u32)offset);
121 return _sparc_alloc_io(0, offset, size, name);
122}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800123EXPORT_SYMBOL(ioremap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*
126 * Comlimentary to ioremap().
127 */
128void iounmap(volatile void __iomem *virtual)
129{
130 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
131 struct resource *res;
132
133 if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
134 printk("free_io/iounmap: cannot free %lx\n", vaddr);
135 return;
136 }
137 _sparc_free_io(res);
138
139 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
140 xres_free((struct xresource *)res);
141 } else {
142 kfree(res);
143 }
144}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800145EXPORT_SYMBOL(iounmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
David S. Miller3ca9fab2006-06-29 14:35:33 -0700147void __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
David S. Millere3a411a2006-12-28 21:01:32 -0800156void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
David S. Miller3ca9fab2006-06-29 14:35:33 -0700157{
158 iounmap(base);
159}
160EXPORT_SYMBOL(of_iounmap);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * Meat of mapping
164 */
165static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
166 unsigned long size, char *name)
167{
168 static int printed_full;
169 struct xresource *xres;
170 struct resource *res;
171 char *tack;
172 int tlen;
173 void __iomem *va; /* P3 diag */
174
175 if (name == NULL) name = "???";
176
177 if ((xres = xres_alloc()) != 0) {
178 tack = xres->xname;
179 res = &xres->xres;
180 } else {
181 if (!printed_full) {
182 printk("ioremap: done with statics, switching to malloc\n");
183 printed_full = 1;
184 }
185 tlen = strlen(name);
186 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
187 if (tack == NULL) return NULL;
188 memset(tack, 0, sizeof(struct resource));
189 res = (struct resource *) tack;
190 tack += sizeof (struct resource);
191 }
192
193 strlcpy(tack, name, XNMLN+1);
194 res->name = tack;
195
196 va = _sparc_ioremap(res, busno, phys, size);
197 /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
198 return va;
199}
200
201/*
202 */
203static void __iomem *
204_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
205{
206 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
207
208 if (allocate_resource(&sparc_iomap, res,
209 (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
210 sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
211 /* Usually we cannot see printks in this case. */
212 prom_printf("alloc_io_res(%s): cannot occupy\n",
213 (res->name != NULL)? res->name: "???");
214 prom_halt();
215 }
216
217 pa &= PAGE_MASK;
218 sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
219
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700220 return (void __iomem *)(unsigned long)(res->start + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/*
224 * Comlimentary to _sparc_ioremap().
225 */
226static void _sparc_free_io(struct resource *res)
227{
228 unsigned long plen;
229
230 plen = res->end - res->start + 1;
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800231 BUG_ON((plen & (PAGE_SIZE-1)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sparc_unmapiorange(res->start, plen);
233 release_resource(res);
234}
235
236#ifdef CONFIG_SBUS
237
David S. Miller63237ee2008-08-26 23:33:42 -0700238void sbus_set_sbus64(struct device *dev, int x)
David S. Miller8fae0972006-06-20 15:23:28 -0700239{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 printk("sbus_set_sbus64: unsupported\n");
241}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800242EXPORT_SYMBOL(sbus_set_sbus64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244/*
245 * Allocate a chunk of memory suitable for DMA.
246 * Typically devices use them for control blocks.
247 * CPU may access them without any explicit flushing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
David S. Miller7a715f42008-08-27 18:37:58 -0700249void *sbus_alloc_consistent(struct device *dev, long len, u32 *dma_addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
David S. Miller7a715f42008-08-27 18:37:58 -0700251 struct of_device *op = to_of_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
253 unsigned long va;
254 struct resource *res;
255 int order;
256
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200257 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 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);
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800267 if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto err_nopages;
269
Yan Burmanc80892d2006-11-30 17:07:04 -0800270 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
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 */
David S. Miller4b1c5df2008-08-27 18:40:38 -0700285 if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto err_noiommu;
287
David S. Miller7a715f42008-08-27 18:37:58 -0700288 res->name = op->node->name;
Martin Habets4cfbd7e2006-05-07 23:43:19 -0700289
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700290 return (void *)(unsigned long)res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292err_noiommu:
293 release_resource(res);
294err_nova:
295 free_pages(va, order);
296err_nomem:
297 kfree(res);
298err_nopages:
299 return NULL;
300}
301
David S. Miller7a715f42008-08-27 18:37:58 -0700302void sbus_free_consistent(struct device *dev, long n, void *p, u32 ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct resource *res;
305 struct page *pgv;
306
307 if ((res = _sparc_find_resource(&_sparc_dvma,
308 (unsigned long)p)) == NULL) {
309 printk("sbus_free_consistent: cannot free %p\n", p);
310 return;
311 }
312
313 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
314 printk("sbus_free_consistent: unaligned va %p\n", p);
315 return;
316 }
317
318 n = (n + PAGE_SIZE-1) & PAGE_MASK;
319 if ((res->end-res->start)+1 != n) {
320 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
321 (long)((res->end-res->start)+1), n);
322 return;
323 }
324
325 release_resource(res);
326 kfree(res);
327
328 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
David S. Milleraba945e2008-08-27 02:20:35 -0700329 pgv = virt_to_page(p);
David S. Miller4b1c5df2008-08-27 18:40:38 -0700330 mmu_unmap_dma_area(dev, ba, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 __free_pages(pgv, get_order(n));
333}
334
335/*
336 * Map a chunk of memory so that devices can see it.
337 * CPU view of this memory may be inconsistent with
338 * a device view and explicit flushing is necessary.
339 */
FUJITA Tomonoric2c07db2009-08-10 11:53:15 +0900340dma_addr_t sbus_map_page(struct device *dev, struct page *page,
341 unsigned long offset, size_t len, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
FUJITA Tomonoric2c07db2009-08-10 11:53:15 +0900343 void *va = page_address(page) + offset;
344
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200345 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (len <= 0) {
347 return 0;
348 }
349 /* XXX So what is maxphys for us and how do drivers know it? */
350 if (len > 256*1024) { /* __get_free_pages() limit */
351 return 0;
352 }
David S. Miller260489f2008-08-26 23:00:58 -0700353 return mmu_get_scsi_one(dev, va, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
FUJITA Tomonoric2c07db2009-08-10 11:53:15 +0900356void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
David S. Miller260489f2008-08-26 23:00:58 -0700358 mmu_release_scsi_one(dev, ba, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
David S. Miller7a715f42008-08-27 18:37:58 -0700361int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
David S. Miller260489f2008-08-26 23:00:58 -0700363 mmu_get_scsi_sgl(dev, sg, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /*
366 * XXX sparc64 can return a partial length here. sun4c should do this
367 * but it currently panics if it can't fulfill the request - Anton
368 */
369 return n;
370}
371
David S. Miller7a715f42008-08-27 18:37:58 -0700372void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
David S. Miller260489f2008-08-26 23:00:58 -0700374 mmu_release_scsi_sgl(dev, sg, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
David S. Miller7a715f42008-08-27 18:37:58 -0700377void sbus_dma_sync_single_for_cpu(struct device *dev, dma_addr_t ba, size_t size, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
David S. Miller7a715f42008-08-27 18:37:58 -0700381void sbus_dma_sync_single_for_device(struct device *dev, dma_addr_t ba, size_t size, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
David S. Millerf8e4d322008-08-27 04:20:14 -0700385static int __init sparc_register_ioport(void)
David S. Miller576c3522006-06-23 15:55:45 -0700386{
David S. Miller576c3522006-06-23 15:55:45 -0700387 register_proc_sparc_ioport();
388
David S. Miller576c3522006-06-23 15:55:45 -0700389 return 0;
David S. Miller576c3522006-06-23 15:55:45 -0700390}
391
David S. Millerf8e4d322008-08-27 04:20:14 -0700392arch_initcall(sparc_register_ioport);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#endif /* CONFIG_SBUS */
395
396#ifdef CONFIG_PCI
397
398/* Allocate and map kernel buffer using consistent mode DMA for a device.
399 * hwdev should be valid struct pci_dev pointer for PCI devices.
400 */
401void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
402{
403 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
404 unsigned long va;
405 struct resource *res;
406 int order;
407
408 if (len == 0) {
409 return NULL;
410 }
411 if (len > 256*1024) { /* __get_free_pages() limit */
412 return NULL;
413 }
414
415 order = get_order(len_total);
416 va = __get_free_pages(GFP_KERNEL, order);
417 if (va == 0) {
418 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
419 return NULL;
420 }
421
Yan Burmanc80892d2006-11-30 17:07:04 -0800422 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 free_pages(va, order);
424 printk("pci_alloc_consistent: no core\n");
425 return NULL;
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (allocate_resource(&_sparc_dvma, res, len_total,
429 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
430 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
431 free_pages(va, order);
432 kfree(res);
433 return NULL;
434 }
435 mmu_inval_dma_area(va, len_total);
436#if 0
437/* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
438 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
439#endif
440 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
441
442 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
443 return (void *) res->start;
444}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800445EXPORT_SYMBOL(pci_alloc_consistent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447/* Free and unmap a consistent DMA buffer.
448 * cpu_addr is what was returned from pci_alloc_consistent,
449 * size must be the same as what as passed into pci_alloc_consistent,
450 * and likewise dma_addr must be the same as what *dma_addrp was set to.
451 *
Simon Arlottd1a78c32007-05-11 13:51:23 -0700452 * References to the memory and mappings associated with cpu_addr/dma_addr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * past this call are illegal.
454 */
455void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
456{
457 struct resource *res;
458 unsigned long pgp;
459
460 if ((res = _sparc_find_resource(&_sparc_dvma,
461 (unsigned long)p)) == NULL) {
462 printk("pci_free_consistent: cannot free %p\n", p);
463 return;
464 }
465
466 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
467 printk("pci_free_consistent: unaligned va %p\n", p);
468 return;
469 }
470
471 n = (n + PAGE_SIZE-1) & PAGE_MASK;
472 if ((res->end-res->start)+1 != n) {
473 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
474 (long)((res->end-res->start)+1), (long)n);
475 return;
476 }
477
478 pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
479 mmu_inval_dma_area(pgp, n);
480 sparc_unmapiorange((unsigned long)p, n);
481
482 release_resource(res);
483 kfree(res);
484
485 free_pages(pgp, get_order(n));
486}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800487EXPORT_SYMBOL(pci_free_consistent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489/* Map a single buffer of the indicated size for DMA in streaming mode.
490 * The 32-bit bus address to use is returned.
491 *
492 * Once the device is given the dma address, the device owns this memory
493 * until either pci_unmap_single or pci_dma_sync_single_* is performed.
494 */
495dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
496 int direction)
497{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800498 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* IIep is write-through, not flushing. */
500 return virt_to_phys(ptr);
501}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800502EXPORT_SYMBOL(pci_map_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504/* Unmap a single streaming mode DMA translation. The dma_addr and size
505 * must match what was provided for in a previous pci_map_single call. All
506 * other usages are undefined.
507 *
508 * After this call, reads by the cpu to the buffer are guaranteed to see
509 * whatever the device wrote there.
510 */
511void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
512 int direction)
513{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800514 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (direction != PCI_DMA_TODEVICE) {
516 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
517 (size + PAGE_SIZE-1) & PAGE_MASK);
518 }
519}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800520EXPORT_SYMBOL(pci_unmap_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522/*
523 * Same as pci_map_single, but with pages.
524 */
525dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
526 unsigned long offset, size_t size, int direction)
527{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800528 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* IIep is write-through, not flushing. */
530 return page_to_phys(page) + offset;
531}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800532EXPORT_SYMBOL(pci_map_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534void pci_unmap_page(struct pci_dev *hwdev,
535 dma_addr_t dma_address, size_t size, int direction)
536{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800537 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* mmu_inval_dma_area XXX */
539}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800540EXPORT_SYMBOL(pci_unmap_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542/* Map a set of buffers described by scatterlist in streaming
543 * mode for DMA. This is the scather-gather version of the
544 * above pci_map_single interface. Here the scatter gather list
545 * elements are each tagged with the appropriate dma address
546 * and length. They are obtained via sg_dma_{address,length}(SG).
547 *
548 * NOTE: An implementation may be able to use a smaller number of
549 * DMA address/length pairs than there are SG table elements.
550 * (for example via virtual mapping capabilities)
551 * The routine returns the number of addr/length pairs actually
552 * used, at most nents.
553 *
554 * Device ownership issues as mentioned above for pci_map_single are
555 * the same here.
556 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200557int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int direction)
559{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200560 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int n;
562
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800563 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* IIep is write-through, not flushing. */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200565 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200566 BUG_ON(page_address(sg_page(sg)) == NULL);
Robert Reifaa83a262008-12-11 20:24:58 -0800567 sg->dma_address = virt_to_phys(sg_virt(sg));
568 sg->dma_length = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 return nents;
571}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800572EXPORT_SYMBOL(pci_map_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574/* Unmap a set of streaming mode DMA translations.
575 * Again, cpu read rules concerning calls here are the same as for
576 * pci_unmap_single() above.
577 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200578void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int direction)
580{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200581 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int n;
583
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800584 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200586 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200587 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200589 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 }
593}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800594EXPORT_SYMBOL(pci_unmap_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596/* Make physical memory consistent for a single
597 * streaming mode DMA translation before or after a transfer.
598 *
599 * If you perform a pci_map_single() but wish to interrogate the
600 * buffer using the cpu, yet do not wish to teardown the PCI dma
601 * mapping, you must call this function before doing so. At the
602 * next point you give the PCI dma address back to the card, you
603 * must first perform a pci_dma_sync_for_device, and then the
604 * device again owns the buffer.
605 */
606void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
607{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800608 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (direction != PCI_DMA_TODEVICE) {
610 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
611 (size + PAGE_SIZE-1) & PAGE_MASK);
612 }
613}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800614EXPORT_SYMBOL(pci_dma_sync_single_for_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
617{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800618 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (direction != PCI_DMA_TODEVICE) {
620 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
621 (size + PAGE_SIZE-1) & PAGE_MASK);
622 }
623}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800624EXPORT_SYMBOL(pci_dma_sync_single_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626/* Make physical memory consistent for a set of streaming
627 * mode DMA translations after a transfer.
628 *
629 * The same as pci_dma_sync_single_* but for a scatter-gather list,
630 * same rules and usage.
631 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200632void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200634 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int n;
636
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800637 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200639 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200640 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200642 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645 }
646}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800647EXPORT_SYMBOL(pci_dma_sync_sg_for_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jens Axboe0912a5d2007-05-14 15:44:38 +0200649void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200651 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int n;
653
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800654 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200656 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200657 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200659 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 }
663}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800664EXPORT_SYMBOL(pci_dma_sync_sg_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#endif /* CONFIG_PCI */
666
667#ifdef CONFIG_PROC_FS
668
669static int
670_sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
671 void *data)
672{
673 char *p = buf, *e = buf + length;
674 struct resource *r;
675 const char *nm;
676
677 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
678 if (p + 32 >= e) /* Better than nothing */
679 break;
680 if ((nm = r->name) == 0) nm = "???";
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700681 p += sprintf(p, "%016llx-%016llx: %s\n",
682 (unsigned long long)r->start,
683 (unsigned long long)r->end, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
686 return p-buf;
687}
688
689#endif /* CONFIG_PROC_FS */
690
691/*
692 * This is a version of find_resource and it belongs to kernel/resource.c.
693 * Until we have agreement with Linus and Martin, it lingers here.
694 *
695 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
696 * This probably warrants some sort of hashing.
697 */
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700698static struct resource *_sparc_find_resource(struct resource *root,
699 unsigned long hit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 struct resource *tmp;
702
703 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
704 if (tmp->start <= hit && tmp->end >= hit)
705 return tmp;
706 }
707 return NULL;
708}
709
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700710static void register_proc_sparc_ioport(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712#ifdef CONFIG_PROC_FS
713 create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
714 create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
715#endif
716}