blob: 11dccf9451536114a9c1afb350c3feae4837114e [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
52#define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
53
Adrian Bunkc61c65c2008-06-05 11:40:58 -070054static struct resource *_sparc_find_resource(struct resource *r,
55 unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
58static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
59 unsigned long size, char *name);
60static void _sparc_free_io(struct resource *res);
61
Adrian Bunkc61c65c2008-06-05 11:40:58 -070062static void register_proc_sparc_ioport(void);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* This points to the next to use virtual memory for DVMA mappings */
65static 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
82struct xresource {
83 struct resource xres; /* Must be first */
84 int xflag; /* 1 == used */
85 char xname[XNMLN+1];
86};
87
88static struct xresource xresv[XNRES];
89
90static 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
105static 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 */
115void __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
123/*
124 * Comlimentary to ioremap().
125 */
126void iounmap(volatile void __iomem *virtual)
127{
128 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
129 struct resource *res;
130
131 if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
132 printk("free_io/iounmap: cannot free %lx\n", vaddr);
133 return;
134 }
135 _sparc_free_io(res);
136
137 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
138 xres_free((struct xresource *)res);
139 } else {
140 kfree(res);
141 }
142}
143
144/*
145 */
146void __iomem *sbus_ioremap(struct resource *phyres, unsigned long offset,
147 unsigned long size, char *name)
148{
149 return _sparc_alloc_io(phyres->flags & 0xF,
150 phyres->start + offset, size, name);
151}
152
David S. Miller3ca9fab2006-06-29 14:35:33 -0700153void __iomem *of_ioremap(struct resource *res, unsigned long offset,
154 unsigned long size, char *name)
155{
156 return _sparc_alloc_io(res->flags & 0xF,
157 res->start + offset,
158 size, name);
159}
160EXPORT_SYMBOL(of_ioremap);
161
David S. Millere3a411a2006-12-28 21:01:32 -0800162void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
David S. Miller3ca9fab2006-06-29 14:35:33 -0700163{
164 iounmap(base);
165}
166EXPORT_SYMBOL(of_iounmap);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
169 */
170void sbus_iounmap(volatile void __iomem *addr, unsigned long size)
171{
172 iounmap(addr);
173}
174
175/*
176 * Meat of mapping
177 */
178static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
179 unsigned long size, char *name)
180{
181 static int printed_full;
182 struct xresource *xres;
183 struct resource *res;
184 char *tack;
185 int tlen;
186 void __iomem *va; /* P3 diag */
187
188 if (name == NULL) name = "???";
189
190 if ((xres = xres_alloc()) != 0) {
191 tack = xres->xname;
192 res = &xres->xres;
193 } else {
194 if (!printed_full) {
195 printk("ioremap: done with statics, switching to malloc\n");
196 printed_full = 1;
197 }
198 tlen = strlen(name);
199 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
200 if (tack == NULL) return NULL;
201 memset(tack, 0, sizeof(struct resource));
202 res = (struct resource *) tack;
203 tack += sizeof (struct resource);
204 }
205
206 strlcpy(tack, name, XNMLN+1);
207 res->name = tack;
208
209 va = _sparc_ioremap(res, busno, phys, size);
210 /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
211 return va;
212}
213
214/*
215 */
216static void __iomem *
217_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
218{
219 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
220
221 if (allocate_resource(&sparc_iomap, res,
222 (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
223 sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
224 /* Usually we cannot see printks in this case. */
225 prom_printf("alloc_io_res(%s): cannot occupy\n",
226 (res->name != NULL)? res->name: "???");
227 prom_halt();
228 }
229
230 pa &= PAGE_MASK;
231 sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
232
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700233 return (void __iomem *)(unsigned long)(res->start + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/*
237 * Comlimentary to _sparc_ioremap().
238 */
239static void _sparc_free_io(struct resource *res)
240{
241 unsigned long plen;
242
243 plen = res->end - res->start + 1;
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800244 BUG_ON((plen & (PAGE_SIZE-1)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 sparc_unmapiorange(res->start, plen);
246 release_resource(res);
247}
248
249#ifdef CONFIG_SBUS
250
David S. Miller8fae0972006-06-20 15:23:28 -0700251void sbus_set_sbus64(struct sbus_dev *sdev, int x)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 printk("sbus_set_sbus64: unsupported\n");
254}
255
David S. Miller8fae0972006-06-20 15:23:28 -0700256extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
257void __init sbus_fill_device_irq(struct sbus_dev *sdev)
258{
259 struct linux_prom_irqs irqs[PROMINTR_MAX];
260 int len;
261
262 len = prom_getproperty(sdev->prom_node, "intr",
263 (char *)irqs, sizeof(irqs));
264 if (len != -1) {
265 sdev->num_irqs = len / 8;
266 if (sdev->num_irqs == 0) {
267 sdev->irqs[0] = 0;
268 } else if (sparc_cpu_model == sun4d) {
269 for (len = 0; len < sdev->num_irqs; len++)
270 sdev->irqs[len] =
271 sun4d_build_irq(sdev, irqs[len].pri);
272 } else {
273 for (len = 0; len < sdev->num_irqs; len++)
274 sdev->irqs[len] = irqs[len].pri;
275 }
276 } else {
277 int interrupts[PROMINTR_MAX];
278
279 /* No "intr" node found-- check for "interrupts" node.
280 * This node contains SBus interrupt levels, not IPLs
281 * as in "intr", and no vector values. We convert
282 * SBus interrupt levels to PILs (platform specific).
283 */
284 len = prom_getproperty(sdev->prom_node, "interrupts",
285 (char *)interrupts, sizeof(interrupts));
286 if (len == -1) {
287 sdev->irqs[0] = 0;
288 sdev->num_irqs = 0;
289 } else {
290 sdev->num_irqs = len / sizeof(int);
291 for (len = 0; len < sdev->num_irqs; len++) {
292 sdev->irqs[len] =
293 sbint_to_irq(sdev, interrupts[len]);
294 }
295 }
296 }
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * Allocate a chunk of memory suitable for DMA.
301 * Typically devices use them for control blocks.
302 * CPU may access them without any explicit flushing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
David S. Miller7a715f42008-08-27 18:37:58 -0700304void *sbus_alloc_consistent(struct device *dev, long len, u32 *dma_addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
David S. Miller7a715f42008-08-27 18:37:58 -0700306 struct of_device *op = to_of_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
308 unsigned long va;
309 struct resource *res;
310 int order;
311
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200312 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (len <= 0) {
314 return NULL;
315 }
316 /* XXX So what is maxphys for us and how do drivers know it? */
317 if (len > 256*1024) { /* __get_free_pages() limit */
318 return NULL;
319 }
320
321 order = get_order(len_total);
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800322 if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto err_nopages;
324
Yan Burmanc80892d2006-11-30 17:07:04 -0800325 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (allocate_resource(&_sparc_dvma, res, len_total,
329 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
330 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
331 goto err_nova;
332 }
333 mmu_inval_dma_area(va, len_total);
334 // XXX The mmu_map_dma_area does this for us below, see comments.
335 // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
336 /*
337 * XXX That's where sdev would be used. Currently we load
338 * all iommu tables with the same translations.
339 */
340 if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0)
341 goto err_noiommu;
342
David S. Miller7a715f42008-08-27 18:37:58 -0700343 res->name = op->node->name;
Martin Habets4cfbd7e2006-05-07 23:43:19 -0700344
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700345 return (void *)(unsigned long)res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347err_noiommu:
348 release_resource(res);
349err_nova:
350 free_pages(va, order);
351err_nomem:
352 kfree(res);
353err_nopages:
354 return NULL;
355}
356
David S. Miller7a715f42008-08-27 18:37:58 -0700357void sbus_free_consistent(struct device *dev, long n, void *p, u32 ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct resource *res;
360 struct page *pgv;
361
362 if ((res = _sparc_find_resource(&_sparc_dvma,
363 (unsigned long)p)) == NULL) {
364 printk("sbus_free_consistent: cannot free %p\n", p);
365 return;
366 }
367
368 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
369 printk("sbus_free_consistent: unaligned va %p\n", p);
370 return;
371 }
372
373 n = (n + PAGE_SIZE-1) & PAGE_MASK;
374 if ((res->end-res->start)+1 != n) {
375 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
376 (long)((res->end-res->start)+1), n);
377 return;
378 }
379
380 release_resource(res);
381 kfree(res);
382
383 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
384 pgv = mmu_translate_dvma(ba);
385 mmu_unmap_dma_area(ba, n);
386
387 __free_pages(pgv, get_order(n));
388}
389
390/*
391 * Map a chunk of memory so that devices can see it.
392 * CPU view of this memory may be inconsistent with
393 * a device view and explicit flushing is necessary.
394 */
David S. Miller7a715f42008-08-27 18:37:58 -0700395dma_addr_t sbus_map_single(struct device *dev, void *va, size_t len, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200397 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (len <= 0) {
399 return 0;
400 }
401 /* XXX So what is maxphys for us and how do drivers know it? */
402 if (len > 256*1024) { /* __get_free_pages() limit */
403 return 0;
404 }
David S. Miller260489f2008-08-26 23:00:58 -0700405 return mmu_get_scsi_one(dev, va, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
David S. Miller7a715f42008-08-27 18:37:58 -0700408void sbus_unmap_single(struct device *dev, dma_addr_t ba, size_t n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
David S. Miller260489f2008-08-26 23:00:58 -0700410 mmu_release_scsi_one(dev, ba, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
David S. Miller7a715f42008-08-27 18:37:58 -0700413int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
David S. Miller260489f2008-08-26 23:00:58 -0700415 mmu_get_scsi_sgl(dev, sg, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /*
418 * XXX sparc64 can return a partial length here. sun4c should do this
419 * but it currently panics if it can't fulfill the request - Anton
420 */
421 return n;
422}
423
David S. Miller7a715f42008-08-27 18:37:58 -0700424void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
David S. Miller260489f2008-08-26 23:00:58 -0700426 mmu_release_scsi_sgl(dev, sg, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429/*
430 */
David S. Miller7a715f42008-08-27 18:37:58 -0700431void 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 -0700432{
433#if 0
434 unsigned long va;
435 struct resource *res;
436
437 /* We do not need the resource, just print a message if invalid. */
438 res = _sparc_find_resource(&_sparc_dvma, ba);
439 if (res == NULL)
440 panic("sbus_dma_sync_single: 0x%x\n", ba);
441
442 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
443 /*
444 * XXX This bogosity will be fixed with the iommu rewrite coming soon
445 * to a kernel near you. - Anton
446 */
447 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
448#endif
449}
450
David S. Miller7a715f42008-08-27 18:37:58 -0700451void 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 -0700452{
453#if 0
454 unsigned long va;
455 struct resource *res;
456
457 /* We do not need the resource, just print a message if invalid. */
458 res = _sparc_find_resource(&_sparc_dvma, ba);
459 if (res == NULL)
460 panic("sbus_dma_sync_single: 0x%x\n", ba);
461
462 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
463 /*
464 * XXX This bogosity will be fixed with the iommu rewrite coming soon
465 * to a kernel near you. - Anton
466 */
467 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
468#endif
469}
470
David S. Miller576c3522006-06-23 15:55:45 -0700471/* Support code for sbus_init(). */
472/*
473 * XXX This functions appears to be a distorted version of
474 * prom_sbus_ranges_init(), with all sun4d stuff cut away.
475 * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
476 */
477/* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
478void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
479{
480 int parent_node = pn->node;
481
482 if (sparc_cpu_model == sun4d) {
483 struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
484 int num_iounit_ranges, len;
485
486 len = prom_getproperty(parent_node, "ranges",
487 (char *) iounit_ranges,
488 sizeof (iounit_ranges));
489 if (len != -1) {
490 num_iounit_ranges =
491 (len / sizeof(struct linux_prom_ranges));
492 prom_adjust_ranges(sbus->sbus_ranges,
493 sbus->num_sbus_ranges,
494 iounit_ranges, num_iounit_ranges);
495 }
496 }
497}
498
499void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
500{
Al Viro5932ef02006-09-23 01:26:02 +0100501#ifndef CONFIG_SUN4
David S. Miller576c3522006-06-23 15:55:45 -0700502 struct device_node *parent = dp->parent;
503
504 if (sparc_cpu_model != sun4d &&
505 parent != NULL &&
David S. Millere0039342008-08-25 22:47:20 -0700506 !strcmp(parent->name, "iommu"))
507 iommu_init(parent, sbus);
David S. Miller576c3522006-06-23 15:55:45 -0700508
David S. Millere0039342008-08-25 22:47:20 -0700509 if (sparc_cpu_model == sun4d)
510 iounit_init(sbus);
Al Viro5932ef02006-09-23 01:26:02 +0100511#endif
David S. Miller576c3522006-06-23 15:55:45 -0700512}
513
514void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
515{
516 if (sparc_cpu_model == sun4d) {
517 struct device_node *parent = dp->parent;
518
519 sbus->devid = of_getintprop_default(parent, "device-id", 0);
520 sbus->board = of_getintprop_default(parent, "board#", 0);
521 }
522}
523
524int __init sbus_arch_preinit(void)
525{
David S. Miller576c3522006-06-23 15:55:45 -0700526 register_proc_sparc_ioport();
527
528#ifdef CONFIG_SUN4
529 {
530 extern void sun4_dvma_init(void);
531 sun4_dvma_init();
532 }
533 return 1;
534#else
535 return 0;
536#endif
537}
538
539void __init sbus_arch_postinit(void)
540{
541 if (sparc_cpu_model == sun4d) {
542 extern void sun4d_init_sbi_irq(void);
543 sun4d_init_sbi_irq();
544 }
545}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546#endif /* CONFIG_SBUS */
547
548#ifdef CONFIG_PCI
549
550/* Allocate and map kernel buffer using consistent mode DMA for a device.
551 * hwdev should be valid struct pci_dev pointer for PCI devices.
552 */
553void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
554{
555 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
556 unsigned long va;
557 struct resource *res;
558 int order;
559
560 if (len == 0) {
561 return NULL;
562 }
563 if (len > 256*1024) { /* __get_free_pages() limit */
564 return NULL;
565 }
566
567 order = get_order(len_total);
568 va = __get_free_pages(GFP_KERNEL, order);
569 if (va == 0) {
570 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
571 return NULL;
572 }
573
Yan Burmanc80892d2006-11-30 17:07:04 -0800574 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 free_pages(va, order);
576 printk("pci_alloc_consistent: no core\n");
577 return NULL;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (allocate_resource(&_sparc_dvma, res, len_total,
581 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
582 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
583 free_pages(va, order);
584 kfree(res);
585 return NULL;
586 }
587 mmu_inval_dma_area(va, len_total);
588#if 0
589/* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
590 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
591#endif
592 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
593
594 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
595 return (void *) res->start;
596}
597
598/* Free and unmap a consistent DMA buffer.
599 * cpu_addr is what was returned from pci_alloc_consistent,
600 * size must be the same as what as passed into pci_alloc_consistent,
601 * and likewise dma_addr must be the same as what *dma_addrp was set to.
602 *
Simon Arlottd1a78c32007-05-11 13:51:23 -0700603 * References to the memory and mappings associated with cpu_addr/dma_addr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * past this call are illegal.
605 */
606void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
607{
608 struct resource *res;
609 unsigned long pgp;
610
611 if ((res = _sparc_find_resource(&_sparc_dvma,
612 (unsigned long)p)) == NULL) {
613 printk("pci_free_consistent: cannot free %p\n", p);
614 return;
615 }
616
617 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
618 printk("pci_free_consistent: unaligned va %p\n", p);
619 return;
620 }
621
622 n = (n + PAGE_SIZE-1) & PAGE_MASK;
623 if ((res->end-res->start)+1 != n) {
624 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
625 (long)((res->end-res->start)+1), (long)n);
626 return;
627 }
628
629 pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
630 mmu_inval_dma_area(pgp, n);
631 sparc_unmapiorange((unsigned long)p, n);
632
633 release_resource(res);
634 kfree(res);
635
636 free_pages(pgp, get_order(n));
637}
638
639/* Map a single buffer of the indicated size for DMA in streaming mode.
640 * The 32-bit bus address to use is returned.
641 *
642 * Once the device is given the dma address, the device owns this memory
643 * until either pci_unmap_single or pci_dma_sync_single_* is performed.
644 */
645dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
646 int direction)
647{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800648 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* IIep is write-through, not flushing. */
650 return virt_to_phys(ptr);
651}
652
653/* Unmap a single streaming mode DMA translation. The dma_addr and size
654 * must match what was provided for in a previous pci_map_single call. All
655 * other usages are undefined.
656 *
657 * After this call, reads by the cpu to the buffer are guaranteed to see
658 * whatever the device wrote there.
659 */
660void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
661 int direction)
662{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800663 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (direction != PCI_DMA_TODEVICE) {
665 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
666 (size + PAGE_SIZE-1) & PAGE_MASK);
667 }
668}
669
670/*
671 * Same as pci_map_single, but with pages.
672 */
673dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
674 unsigned long offset, size_t size, int direction)
675{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800676 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* IIep is write-through, not flushing. */
678 return page_to_phys(page) + offset;
679}
680
681void pci_unmap_page(struct pci_dev *hwdev,
682 dma_addr_t dma_address, size_t size, int direction)
683{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800684 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* mmu_inval_dma_area XXX */
686}
687
688/* Map a set of buffers described by scatterlist in streaming
689 * mode for DMA. This is the scather-gather version of the
690 * above pci_map_single interface. Here the scatter gather list
691 * elements are each tagged with the appropriate dma address
692 * and length. They are obtained via sg_dma_{address,length}(SG).
693 *
694 * NOTE: An implementation may be able to use a smaller number of
695 * DMA address/length pairs than there are SG table elements.
696 * (for example via virtual mapping capabilities)
697 * The routine returns the number of addr/length pairs actually
698 * used, at most nents.
699 *
700 * Device ownership issues as mentioned above for pci_map_single are
701 * the same here.
702 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200703int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int direction)
705{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200706 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 int n;
708
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800709 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* IIep is write-through, not flushing. */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200711 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200712 BUG_ON(page_address(sg_page(sg)) == NULL);
713 sg->dvma_address = virt_to_phys(sg_virt(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 sg->dvma_length = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 return nents;
717}
718
719/* Unmap a set of streaming mode DMA translations.
720 * Again, cpu read rules concerning calls here are the same as for
721 * pci_unmap_single() above.
722 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200723void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int direction)
725{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200726 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int n;
728
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800729 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200731 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200732 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200734 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 }
738}
739
740/* Make physical memory consistent for a single
741 * streaming mode DMA translation before or after a transfer.
742 *
743 * If you perform a pci_map_single() but wish to interrogate the
744 * buffer using the cpu, yet do not wish to teardown the PCI dma
745 * mapping, you must call this function before doing so. At the
746 * next point you give the PCI dma address back to the card, you
747 * must first perform a pci_dma_sync_for_device, and then the
748 * device again owns the buffer.
749 */
750void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
751{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800752 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (direction != PCI_DMA_TODEVICE) {
754 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
755 (size + PAGE_SIZE-1) & PAGE_MASK);
756 }
757}
758
759void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
760{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800761 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (direction != PCI_DMA_TODEVICE) {
763 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
764 (size + PAGE_SIZE-1) & PAGE_MASK);
765 }
766}
767
768/* Make physical memory consistent for a set of streaming
769 * mode DMA translations after a transfer.
770 *
771 * The same as pci_dma_sync_single_* but for a scatter-gather list,
772 * same rules and usage.
773 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200774void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200776 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int n;
778
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800779 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200781 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200782 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200784 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787 }
788}
789
Jens Axboe0912a5d2007-05-14 15:44:38 +0200790void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200792 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 int n;
794
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800795 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200797 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200798 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200800 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 }
804}
805#endif /* CONFIG_PCI */
806
807#ifdef CONFIG_PROC_FS
808
809static int
810_sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
811 void *data)
812{
813 char *p = buf, *e = buf + length;
814 struct resource *r;
815 const char *nm;
816
817 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
818 if (p + 32 >= e) /* Better than nothing */
819 break;
820 if ((nm = r->name) == 0) nm = "???";
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700821 p += sprintf(p, "%016llx-%016llx: %s\n",
822 (unsigned long long)r->start,
823 (unsigned long long)r->end, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
826 return p-buf;
827}
828
829#endif /* CONFIG_PROC_FS */
830
831/*
832 * This is a version of find_resource and it belongs to kernel/resource.c.
833 * Until we have agreement with Linus and Martin, it lingers here.
834 *
835 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
836 * This probably warrants some sort of hashing.
837 */
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700838static struct resource *_sparc_find_resource(struct resource *root,
839 unsigned long hit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct resource *tmp;
842
843 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
844 if (tmp->start <= hit && tmp->end >= hit)
845 return tmp;
846 }
847 return NULL;
848}
849
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700850static void register_proc_sparc_ioport(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852#ifdef CONFIG_PROC_FS
853 create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
854 create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
855#endif
856}