blob: 24645f9f56f5e85fa948022e140baa571a29b2ee [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>
45#include <asm/sbus.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/page.h>
47#include <asm/pgalloc.h>
48#include <asm/dma.h>
David S. Millere0039342008-08-25 22:47:20 -070049#include <asm/iommu.h>
50#include <asm/io-unit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David S. Miller944c67df2008-08-27 18:01:36 -070052#include "dma.h"
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
55
Adrian Bunkc61c65c2008-06-05 11:40:58 -070056static struct resource *_sparc_find_resource(struct resource *r,
57 unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
60static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
61 unsigned long size, char *name);
62static void _sparc_free_io(struct resource *res);
63
Adrian Bunkc61c65c2008-06-05 11:40:58 -070064static void register_proc_sparc_ioport(void);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* This points to the next to use virtual memory for DVMA mappings */
67static struct resource _sparc_dvma = {
68 .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
69};
70/* This points to the start of I/O mappings, cluable from outside. */
71/*ext*/ struct resource sparc_iomap = {
72 .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
73};
74
75/*
76 * Our mini-allocator...
77 * Boy this is gross! We need it because we must map I/O for
78 * timers and interrupt controller before the kmalloc is available.
79 */
80
81#define XNMLN 15
82#define XNRES 10 /* SS-10 uses 8 */
83
84struct xresource {
85 struct resource xres; /* Must be first */
86 int xflag; /* 1 == used */
87 char xname[XNMLN+1];
88};
89
90static struct xresource xresv[XNRES];
91
92static struct xresource *xres_alloc(void) {
93 struct xresource *xrp;
94 int n;
95
96 xrp = xresv;
97 for (n = 0; n < XNRES; n++) {
98 if (xrp->xflag == 0) {
99 xrp->xflag = 1;
100 return xrp;
101 }
102 xrp++;
103 }
104 return NULL;
105}
106
107static void xres_free(struct xresource *xrp) {
108 xrp->xflag = 0;
109}
110
111/*
112 * These are typically used in PCI drivers
113 * which are trying to be cross-platform.
114 *
115 * Bus type is always zero on IIep.
116 */
117void __iomem *ioremap(unsigned long offset, unsigned long size)
118{
119 char name[14];
120
121 sprintf(name, "phys_%08x", (u32)offset);
122 return _sparc_alloc_io(0, offset, size, name);
123}
124
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}
145
David S. Miller3ca9fab2006-06-29 14:35:33 -0700146void __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}
153EXPORT_SYMBOL(of_ioremap);
154
David S. Millere3a411a2006-12-28 21:01:32 -0800155void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
David S. Miller3ca9fab2006-06-29 14:35:33 -0700156{
157 iounmap(base);
158}
159EXPORT_SYMBOL(of_iounmap);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * Meat of mapping
163 */
164static 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 */
202static 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
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700219 return (void __iomem *)(unsigned long)(res->start + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/*
223 * Comlimentary to _sparc_ioremap().
224 */
225static void _sparc_free_io(struct resource *res)
226{
227 unsigned long plen;
228
229 plen = res->end - res->start + 1;
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800230 BUG_ON((plen & (PAGE_SIZE-1)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 sparc_unmapiorange(res->start, plen);
232 release_resource(res);
233}
234
235#ifdef CONFIG_SBUS
236
David S. Miller63237ee2008-08-26 23:33:42 -0700237void sbus_set_sbus64(struct device *dev, int x)
David S. Miller8fae0972006-06-20 15:23:28 -0700238{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 printk("sbus_set_sbus64: unsupported\n");
240}
241
242/*
243 * Allocate a chunk of memory suitable for DMA.
244 * Typically devices use them for control blocks.
245 * CPU may access them without any explicit flushing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
David S. Miller7a715f42008-08-27 18:37:58 -0700247void *sbus_alloc_consistent(struct device *dev, long len, u32 *dma_addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
David S. Miller7a715f42008-08-27 18:37:58 -0700249 struct of_device *op = to_of_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
251 unsigned long va;
252 struct resource *res;
253 int order;
254
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200255 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (len <= 0) {
257 return NULL;
258 }
259 /* XXX So what is maxphys for us and how do drivers know it? */
260 if (len > 256*1024) { /* __get_free_pages() limit */
261 return NULL;
262 }
263
264 order = get_order(len_total);
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800265 if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 goto err_nopages;
267
Yan Burmanc80892d2006-11-30 17:07:04 -0800268 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (allocate_resource(&_sparc_dvma, res, len_total,
272 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
273 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
274 goto err_nova;
275 }
276 mmu_inval_dma_area(va, len_total);
277 // XXX The mmu_map_dma_area does this for us below, see comments.
278 // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
279 /*
280 * XXX That's where sdev would be used. Currently we load
281 * all iommu tables with the same translations.
282 */
David S. Miller4b1c5df2008-08-27 18:40:38 -0700283 if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 goto err_noiommu;
285
David S. Miller7a715f42008-08-27 18:37:58 -0700286 res->name = op->node->name;
Martin Habets4cfbd7e2006-05-07 23:43:19 -0700287
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700288 return (void *)(unsigned long)res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290err_noiommu:
291 release_resource(res);
292err_nova:
293 free_pages(va, order);
294err_nomem:
295 kfree(res);
296err_nopages:
297 return NULL;
298}
299
David S. Miller7a715f42008-08-27 18:37:58 -0700300void sbus_free_consistent(struct device *dev, long n, void *p, u32 ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct resource *res;
303 struct page *pgv;
304
305 if ((res = _sparc_find_resource(&_sparc_dvma,
306 (unsigned long)p)) == NULL) {
307 printk("sbus_free_consistent: cannot free %p\n", p);
308 return;
309 }
310
311 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
312 printk("sbus_free_consistent: unaligned va %p\n", p);
313 return;
314 }
315
316 n = (n + PAGE_SIZE-1) & PAGE_MASK;
317 if ((res->end-res->start)+1 != n) {
318 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
319 (long)((res->end-res->start)+1), n);
320 return;
321 }
322
323 release_resource(res);
324 kfree(res);
325
326 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
David S. Milleraba945e2008-08-27 02:20:35 -0700327 pgv = virt_to_page(p);
David S. Miller4b1c5df2008-08-27 18:40:38 -0700328 mmu_unmap_dma_area(dev, ba, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 __free_pages(pgv, get_order(n));
331}
332
333/*
334 * Map a chunk of memory so that devices can see it.
335 * CPU view of this memory may be inconsistent with
336 * a device view and explicit flushing is necessary.
337 */
David S. Miller7a715f42008-08-27 18:37:58 -0700338dma_addr_t sbus_map_single(struct device *dev, void *va, size_t len, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200340 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (len <= 0) {
342 return 0;
343 }
344 /* XXX So what is maxphys for us and how do drivers know it? */
345 if (len > 256*1024) { /* __get_free_pages() limit */
346 return 0;
347 }
David S. Miller260489f2008-08-26 23:00:58 -0700348 return mmu_get_scsi_one(dev, va, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
David S. Miller7a715f42008-08-27 18:37:58 -0700351void sbus_unmap_single(struct device *dev, dma_addr_t ba, size_t n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
David S. Miller260489f2008-08-26 23:00:58 -0700353 mmu_release_scsi_one(dev, ba, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
David S. Miller7a715f42008-08-27 18:37:58 -0700356int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
David S. Miller260489f2008-08-26 23:00:58 -0700358 mmu_get_scsi_sgl(dev, sg, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /*
361 * XXX sparc64 can return a partial length here. sun4c should do this
362 * but it currently panics if it can't fulfill the request - Anton
363 */
364 return n;
365}
366
David S. Miller7a715f42008-08-27 18:37:58 -0700367void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
David S. Miller260489f2008-08-26 23:00:58 -0700369 mmu_release_scsi_sgl(dev, sg, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
David S. Miller7a715f42008-08-27 18:37:58 -0700372void 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 -0700373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
David S. Miller7a715f42008-08-27 18:37:58 -0700376void 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 -0700377{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
David S. Miller576c3522006-06-23 15:55:45 -0700380/* Support code for sbus_init(). */
David S. Miller576c3522006-06-23 15:55:45 -0700381void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
382{
Al Viro5932ef02006-09-23 01:26:02 +0100383#ifndef CONFIG_SUN4
David S. Miller576c3522006-06-23 15:55:45 -0700384 struct device_node *parent = dp->parent;
385
386 if (sparc_cpu_model != sun4d &&
387 parent != NULL &&
David S. Millere0039342008-08-25 22:47:20 -0700388 !strcmp(parent->name, "iommu"))
389 iommu_init(parent, sbus);
David S. Miller576c3522006-06-23 15:55:45 -0700390
David S. Millere0039342008-08-25 22:47:20 -0700391 if (sparc_cpu_model == sun4d)
392 iounit_init(sbus);
Al Viro5932ef02006-09-23 01:26:02 +0100393#endif
David S. Miller576c3522006-06-23 15:55:45 -0700394}
395
David S. Millerf8e4d322008-08-27 04:20:14 -0700396static int __init sparc_register_ioport(void)
David S. Miller576c3522006-06-23 15:55:45 -0700397{
David S. Miller576c3522006-06-23 15:55:45 -0700398 register_proc_sparc_ioport();
399
David S. Miller576c3522006-06-23 15:55:45 -0700400 return 0;
David S. Miller576c3522006-06-23 15:55:45 -0700401}
402
David S. Millerf8e4d322008-08-27 04:20:14 -0700403arch_initcall(sparc_register_ioport);
404
David S. Miller576c3522006-06-23 15:55:45 -0700405void __init sbus_arch_postinit(void)
406{
407 if (sparc_cpu_model == sun4d) {
408 extern void sun4d_init_sbi_irq(void);
409 sun4d_init_sbi_irq();
410 }
411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#endif /* CONFIG_SBUS */
413
414#ifdef CONFIG_PCI
415
416/* Allocate and map kernel buffer using consistent mode DMA for a device.
417 * hwdev should be valid struct pci_dev pointer for PCI devices.
418 */
419void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
420{
421 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
422 unsigned long va;
423 struct resource *res;
424 int order;
425
426 if (len == 0) {
427 return NULL;
428 }
429 if (len > 256*1024) { /* __get_free_pages() limit */
430 return NULL;
431 }
432
433 order = get_order(len_total);
434 va = __get_free_pages(GFP_KERNEL, order);
435 if (va == 0) {
436 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
437 return NULL;
438 }
439
Yan Burmanc80892d2006-11-30 17:07:04 -0800440 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 free_pages(va, order);
442 printk("pci_alloc_consistent: no core\n");
443 return NULL;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (allocate_resource(&_sparc_dvma, res, len_total,
447 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
448 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
449 free_pages(va, order);
450 kfree(res);
451 return NULL;
452 }
453 mmu_inval_dma_area(va, len_total);
454#if 0
455/* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
456 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
457#endif
458 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
459
460 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
461 return (void *) res->start;
462}
463
464/* Free and unmap a consistent DMA buffer.
465 * cpu_addr is what was returned from pci_alloc_consistent,
466 * size must be the same as what as passed into pci_alloc_consistent,
467 * and likewise dma_addr must be the same as what *dma_addrp was set to.
468 *
Simon Arlottd1a78c32007-05-11 13:51:23 -0700469 * References to the memory and mappings associated with cpu_addr/dma_addr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * past this call are illegal.
471 */
472void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
473{
474 struct resource *res;
475 unsigned long pgp;
476
477 if ((res = _sparc_find_resource(&_sparc_dvma,
478 (unsigned long)p)) == NULL) {
479 printk("pci_free_consistent: cannot free %p\n", p);
480 return;
481 }
482
483 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
484 printk("pci_free_consistent: unaligned va %p\n", p);
485 return;
486 }
487
488 n = (n + PAGE_SIZE-1) & PAGE_MASK;
489 if ((res->end-res->start)+1 != n) {
490 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
491 (long)((res->end-res->start)+1), (long)n);
492 return;
493 }
494
495 pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
496 mmu_inval_dma_area(pgp, n);
497 sparc_unmapiorange((unsigned long)p, n);
498
499 release_resource(res);
500 kfree(res);
501
502 free_pages(pgp, get_order(n));
503}
504
505/* Map a single buffer of the indicated size for DMA in streaming mode.
506 * The 32-bit bus address to use is returned.
507 *
508 * Once the device is given the dma address, the device owns this memory
509 * until either pci_unmap_single or pci_dma_sync_single_* is performed.
510 */
511dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, 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 /* IIep is write-through, not flushing. */
516 return virt_to_phys(ptr);
517}
518
519/* Unmap a single streaming mode DMA translation. The dma_addr and size
520 * must match what was provided for in a previous pci_map_single call. All
521 * other usages are undefined.
522 *
523 * After this call, reads by the cpu to the buffer are guaranteed to see
524 * whatever the device wrote there.
525 */
526void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
527 int direction)
528{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800529 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (direction != PCI_DMA_TODEVICE) {
531 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
532 (size + PAGE_SIZE-1) & PAGE_MASK);
533 }
534}
535
536/*
537 * Same as pci_map_single, but with pages.
538 */
539dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
540 unsigned long offset, size_t size, int direction)
541{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800542 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* IIep is write-through, not flushing. */
544 return page_to_phys(page) + offset;
545}
546
547void pci_unmap_page(struct pci_dev *hwdev,
548 dma_addr_t dma_address, size_t size, int direction)
549{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800550 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* mmu_inval_dma_area XXX */
552}
553
554/* Map a set of buffers described by scatterlist in streaming
555 * mode for DMA. This is the scather-gather version of the
556 * above pci_map_single interface. Here the scatter gather list
557 * elements are each tagged with the appropriate dma address
558 * and length. They are obtained via sg_dma_{address,length}(SG).
559 *
560 * NOTE: An implementation may be able to use a smaller number of
561 * DMA address/length pairs than there are SG table elements.
562 * (for example via virtual mapping capabilities)
563 * The routine returns the number of addr/length pairs actually
564 * used, at most nents.
565 *
566 * Device ownership issues as mentioned above for pci_map_single are
567 * the same here.
568 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200569int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int direction)
571{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200572 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int n;
574
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800575 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* IIep is write-through, not flushing. */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200577 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200578 BUG_ON(page_address(sg_page(sg)) == NULL);
579 sg->dvma_address = virt_to_phys(sg_virt(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 sg->dvma_length = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582 return nents;
583}
584
585/* Unmap a set of streaming mode DMA translations.
586 * Again, cpu read rules concerning calls here are the same as for
587 * pci_unmap_single() above.
588 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200589void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int direction)
591{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200592 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int n;
594
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800595 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200597 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200598 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200600 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 }
604}
605
606/* Make physical memory consistent for a single
607 * streaming mode DMA translation before or after a transfer.
608 *
609 * If you perform a pci_map_single() but wish to interrogate the
610 * buffer using the cpu, yet do not wish to teardown the PCI dma
611 * mapping, you must call this function before doing so. At the
612 * next point you give the PCI dma address back to the card, you
613 * must first perform a pci_dma_sync_for_device, and then the
614 * device again owns the buffer.
615 */
616void pci_dma_sync_single_for_cpu(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}
624
625void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
626{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800627 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (direction != PCI_DMA_TODEVICE) {
629 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
630 (size + PAGE_SIZE-1) & PAGE_MASK);
631 }
632}
633
634/* Make physical memory consistent for a set of streaming
635 * mode DMA translations after a transfer.
636 *
637 * The same as pci_dma_sync_single_* but for a scatter-gather list,
638 * same rules and usage.
639 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200640void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200642 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int n;
644
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800645 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200647 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200648 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200650 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653 }
654}
655
Jens Axboe0912a5d2007-05-14 15:44:38 +0200656void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200658 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int n;
660
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800661 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200663 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200664 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200666 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 }
670}
671#endif /* CONFIG_PCI */
672
673#ifdef CONFIG_PROC_FS
674
675static int
676_sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
677 void *data)
678{
679 char *p = buf, *e = buf + length;
680 struct resource *r;
681 const char *nm;
682
683 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
684 if (p + 32 >= e) /* Better than nothing */
685 break;
686 if ((nm = r->name) == 0) nm = "???";
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700687 p += sprintf(p, "%016llx-%016llx: %s\n",
688 (unsigned long long)r->start,
689 (unsigned long long)r->end, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
692 return p-buf;
693}
694
695#endif /* CONFIG_PROC_FS */
696
697/*
698 * This is a version of find_resource and it belongs to kernel/resource.c.
699 * Until we have agreement with Linus and Martin, it lingers here.
700 *
701 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
702 * This probably warrants some sort of hashing.
703 */
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700704static struct resource *_sparc_find_resource(struct resource *root,
705 unsigned long hit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct resource *tmp;
708
709 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
710 if (tmp->start <= hit && tmp->end >= hit)
711 return tmp;
712 }
713 return NULL;
714}
715
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700716static void register_proc_sparc_ioport(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718#ifdef CONFIG_PROC_FS
719 create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
720 create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
721#endif
722}