blob: f6158c4a399558d722375c4d52454cdd79a8ab5a [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.
303 *
304 * XXX Some clever people know that sdev is not used and supply NULL. Watch.
305 */
306void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
307{
308 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
309 unsigned long va;
310 struct resource *res;
311 int order;
312
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200313 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (len <= 0) {
315 return NULL;
316 }
317 /* XXX So what is maxphys for us and how do drivers know it? */
318 if (len > 256*1024) { /* __get_free_pages() limit */
319 return NULL;
320 }
321
322 order = get_order(len_total);
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800323 if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 goto err_nopages;
325
Yan Burmanc80892d2006-11-30 17:07:04 -0800326 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (allocate_resource(&_sparc_dvma, res, len_total,
330 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
331 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
332 goto err_nova;
333 }
334 mmu_inval_dma_area(va, len_total);
335 // XXX The mmu_map_dma_area does this for us below, see comments.
336 // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
337 /*
338 * XXX That's where sdev would be used. Currently we load
339 * all iommu tables with the same translations.
340 */
341 if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0)
342 goto err_noiommu;
343
Martin Habets4cfbd7e2006-05-07 23:43:19 -0700344 /* Set the resource name, if known. */
345 if (sdev) {
346 res->name = sdev->prom_name;
347 }
348
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700349 return (void *)(unsigned long)res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351err_noiommu:
352 release_resource(res);
353err_nova:
354 free_pages(va, order);
355err_nomem:
356 kfree(res);
357err_nopages:
358 return NULL;
359}
360
361void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
362{
363 struct resource *res;
364 struct page *pgv;
365
366 if ((res = _sparc_find_resource(&_sparc_dvma,
367 (unsigned long)p)) == NULL) {
368 printk("sbus_free_consistent: cannot free %p\n", p);
369 return;
370 }
371
372 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
373 printk("sbus_free_consistent: unaligned va %p\n", p);
374 return;
375 }
376
377 n = (n + PAGE_SIZE-1) & PAGE_MASK;
378 if ((res->end-res->start)+1 != n) {
379 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
380 (long)((res->end-res->start)+1), n);
381 return;
382 }
383
384 release_resource(res);
385 kfree(res);
386
387 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
388 pgv = mmu_translate_dvma(ba);
389 mmu_unmap_dma_area(ba, n);
390
391 __free_pages(pgv, get_order(n));
392}
393
394/*
395 * Map a chunk of memory so that devices can see it.
396 * CPU view of this memory may be inconsistent with
397 * a device view and explicit flushing is necessary.
398 */
399dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
400{
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200401 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (len <= 0) {
403 return 0;
404 }
405 /* XXX So what is maxphys for us and how do drivers know it? */
406 if (len > 256*1024) { /* __get_free_pages() limit */
407 return 0;
408 }
409 return mmu_get_scsi_one(va, len, sdev->bus);
410}
411
412void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
413{
414 mmu_release_scsi_one(ba, n, sdev->bus);
415}
416
417int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
418{
419 mmu_get_scsi_sgl(sg, n, sdev->bus);
420
421 /*
422 * XXX sparc64 can return a partial length here. sun4c should do this
423 * but it currently panics if it can't fulfill the request - Anton
424 */
425 return n;
426}
427
428void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
429{
430 mmu_release_scsi_sgl(sg, n, sdev->bus);
431}
432
433/*
434 */
435void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
436{
437#if 0
438 unsigned long va;
439 struct resource *res;
440
441 /* We do not need the resource, just print a message if invalid. */
442 res = _sparc_find_resource(&_sparc_dvma, ba);
443 if (res == NULL)
444 panic("sbus_dma_sync_single: 0x%x\n", ba);
445
446 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
447 /*
448 * XXX This bogosity will be fixed with the iommu rewrite coming soon
449 * to a kernel near you. - Anton
450 */
451 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
452#endif
453}
454
455void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
456{
457#if 0
458 unsigned long va;
459 struct resource *res;
460
461 /* We do not need the resource, just print a message if invalid. */
462 res = _sparc_find_resource(&_sparc_dvma, ba);
463 if (res == NULL)
464 panic("sbus_dma_sync_single: 0x%x\n", ba);
465
466 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
467 /*
468 * XXX This bogosity will be fixed with the iommu rewrite coming soon
469 * to a kernel near you. - Anton
470 */
471 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
472#endif
473}
474
475void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
476{
477 printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n");
478}
479
480void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
481{
482 printk("sbus_dma_sync_sg_for_device: not implemented yet\n");
483}
David S. Miller576c3522006-06-23 15:55:45 -0700484
485/* Support code for sbus_init(). */
486/*
487 * XXX This functions appears to be a distorted version of
488 * prom_sbus_ranges_init(), with all sun4d stuff cut away.
489 * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
490 */
491/* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
492void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
493{
494 int parent_node = pn->node;
495
496 if (sparc_cpu_model == sun4d) {
497 struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
498 int num_iounit_ranges, len;
499
500 len = prom_getproperty(parent_node, "ranges",
501 (char *) iounit_ranges,
502 sizeof (iounit_ranges));
503 if (len != -1) {
504 num_iounit_ranges =
505 (len / sizeof(struct linux_prom_ranges));
506 prom_adjust_ranges(sbus->sbus_ranges,
507 sbus->num_sbus_ranges,
508 iounit_ranges, num_iounit_ranges);
509 }
510 }
511}
512
513void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
514{
Al Viro5932ef02006-09-23 01:26:02 +0100515#ifndef CONFIG_SUN4
David S. Miller576c3522006-06-23 15:55:45 -0700516 struct device_node *parent = dp->parent;
517
518 if (sparc_cpu_model != sun4d &&
519 parent != NULL &&
David S. Millere0039342008-08-25 22:47:20 -0700520 !strcmp(parent->name, "iommu"))
521 iommu_init(parent, sbus);
David S. Miller576c3522006-06-23 15:55:45 -0700522
David S. Millere0039342008-08-25 22:47:20 -0700523 if (sparc_cpu_model == sun4d)
524 iounit_init(sbus);
Al Viro5932ef02006-09-23 01:26:02 +0100525#endif
David S. Miller576c3522006-06-23 15:55:45 -0700526}
527
528void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
529{
530 if (sparc_cpu_model == sun4d) {
531 struct device_node *parent = dp->parent;
532
533 sbus->devid = of_getintprop_default(parent, "device-id", 0);
534 sbus->board = of_getintprop_default(parent, "board#", 0);
535 }
536}
537
538int __init sbus_arch_preinit(void)
539{
David S. Miller576c3522006-06-23 15:55:45 -0700540 register_proc_sparc_ioport();
541
542#ifdef CONFIG_SUN4
543 {
544 extern void sun4_dvma_init(void);
545 sun4_dvma_init();
546 }
547 return 1;
548#else
549 return 0;
550#endif
551}
552
553void __init sbus_arch_postinit(void)
554{
555 if (sparc_cpu_model == sun4d) {
556 extern void sun4d_init_sbi_irq(void);
557 sun4d_init_sbi_irq();
558 }
559}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#endif /* CONFIG_SBUS */
561
562#ifdef CONFIG_PCI
563
564/* Allocate and map kernel buffer using consistent mode DMA for a device.
565 * hwdev should be valid struct pci_dev pointer for PCI devices.
566 */
567void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
568{
569 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
570 unsigned long va;
571 struct resource *res;
572 int order;
573
574 if (len == 0) {
575 return NULL;
576 }
577 if (len > 256*1024) { /* __get_free_pages() limit */
578 return NULL;
579 }
580
581 order = get_order(len_total);
582 va = __get_free_pages(GFP_KERNEL, order);
583 if (va == 0) {
584 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
585 return NULL;
586 }
587
Yan Burmanc80892d2006-11-30 17:07:04 -0800588 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 free_pages(va, order);
590 printk("pci_alloc_consistent: no core\n");
591 return NULL;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (allocate_resource(&_sparc_dvma, res, len_total,
595 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
596 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
597 free_pages(va, order);
598 kfree(res);
599 return NULL;
600 }
601 mmu_inval_dma_area(va, len_total);
602#if 0
603/* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
604 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
605#endif
606 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
607
608 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
609 return (void *) res->start;
610}
611
612/* Free and unmap a consistent DMA buffer.
613 * cpu_addr is what was returned from pci_alloc_consistent,
614 * size must be the same as what as passed into pci_alloc_consistent,
615 * and likewise dma_addr must be the same as what *dma_addrp was set to.
616 *
Simon Arlottd1a78c32007-05-11 13:51:23 -0700617 * References to the memory and mappings associated with cpu_addr/dma_addr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * past this call are illegal.
619 */
620void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
621{
622 struct resource *res;
623 unsigned long pgp;
624
625 if ((res = _sparc_find_resource(&_sparc_dvma,
626 (unsigned long)p)) == NULL) {
627 printk("pci_free_consistent: cannot free %p\n", p);
628 return;
629 }
630
631 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
632 printk("pci_free_consistent: unaligned va %p\n", p);
633 return;
634 }
635
636 n = (n + PAGE_SIZE-1) & PAGE_MASK;
637 if ((res->end-res->start)+1 != n) {
638 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
639 (long)((res->end-res->start)+1), (long)n);
640 return;
641 }
642
643 pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
644 mmu_inval_dma_area(pgp, n);
645 sparc_unmapiorange((unsigned long)p, n);
646
647 release_resource(res);
648 kfree(res);
649
650 free_pages(pgp, get_order(n));
651}
652
653/* Map a single buffer of the indicated size for DMA in streaming mode.
654 * The 32-bit bus address to use is returned.
655 *
656 * Once the device is given the dma address, the device owns this memory
657 * until either pci_unmap_single or pci_dma_sync_single_* is performed.
658 */
659dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
660 int direction)
661{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800662 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* IIep is write-through, not flushing. */
664 return virt_to_phys(ptr);
665}
666
667/* Unmap a single streaming mode DMA translation. The dma_addr and size
668 * must match what was provided for in a previous pci_map_single call. All
669 * other usages are undefined.
670 *
671 * After this call, reads by the cpu to the buffer are guaranteed to see
672 * whatever the device wrote there.
673 */
674void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
675 int direction)
676{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800677 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (direction != PCI_DMA_TODEVICE) {
679 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
680 (size + PAGE_SIZE-1) & PAGE_MASK);
681 }
682}
683
684/*
685 * Same as pci_map_single, but with pages.
686 */
687dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
688 unsigned long offset, size_t size, int direction)
689{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800690 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* IIep is write-through, not flushing. */
692 return page_to_phys(page) + offset;
693}
694
695void pci_unmap_page(struct pci_dev *hwdev,
696 dma_addr_t dma_address, size_t size, int direction)
697{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800698 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* mmu_inval_dma_area XXX */
700}
701
702/* Map a set of buffers described by scatterlist in streaming
703 * mode for DMA. This is the scather-gather version of the
704 * above pci_map_single interface. Here the scatter gather list
705 * elements are each tagged with the appropriate dma address
706 * and length. They are obtained via sg_dma_{address,length}(SG).
707 *
708 * NOTE: An implementation may be able to use a smaller number of
709 * DMA address/length pairs than there are SG table elements.
710 * (for example via virtual mapping capabilities)
711 * The routine returns the number of addr/length pairs actually
712 * used, at most nents.
713 *
714 * Device ownership issues as mentioned above for pci_map_single are
715 * the same here.
716 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200717int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 int direction)
719{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200720 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int n;
722
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800723 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /* IIep is write-through, not flushing. */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200725 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200726 BUG_ON(page_address(sg_page(sg)) == NULL);
727 sg->dvma_address = virt_to_phys(sg_virt(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 sg->dvma_length = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 return nents;
731}
732
733/* Unmap a set of streaming mode DMA translations.
734 * Again, cpu read rules concerning calls here are the same as for
735 * pci_unmap_single() above.
736 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200737void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int direction)
739{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200740 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 int n;
742
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800743 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200745 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200746 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200748 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751 }
752}
753
754/* Make physical memory consistent for a single
755 * streaming mode DMA translation before or after a transfer.
756 *
757 * If you perform a pci_map_single() but wish to interrogate the
758 * buffer using the cpu, yet do not wish to teardown the PCI dma
759 * mapping, you must call this function before doing so. At the
760 * next point you give the PCI dma address back to the card, you
761 * must first perform a pci_dma_sync_for_device, and then the
762 * device again owns the buffer.
763 */
764void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
765{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800766 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (direction != PCI_DMA_TODEVICE) {
768 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
769 (size + PAGE_SIZE-1) & PAGE_MASK);
770 }
771}
772
773void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
774{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800775 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (direction != PCI_DMA_TODEVICE) {
777 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
778 (size + PAGE_SIZE-1) & PAGE_MASK);
779 }
780}
781
782/* Make physical memory consistent for a set of streaming
783 * mode DMA translations after a transfer.
784 *
785 * The same as pci_dma_sync_single_* but for a scatter-gather list,
786 * same rules and usage.
787 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200788void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200790 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int n;
792
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800793 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200795 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200796 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200798 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 }
802}
803
Jens Axboe0912a5d2007-05-14 15:44:38 +0200804void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200806 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 int n;
808
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800809 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200811 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200812 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200814 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 }
818}
819#endif /* CONFIG_PCI */
820
821#ifdef CONFIG_PROC_FS
822
823static int
824_sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
825 void *data)
826{
827 char *p = buf, *e = buf + length;
828 struct resource *r;
829 const char *nm;
830
831 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
832 if (p + 32 >= e) /* Better than nothing */
833 break;
834 if ((nm = r->name) == 0) nm = "???";
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700835 p += sprintf(p, "%016llx-%016llx: %s\n",
836 (unsigned long long)r->start,
837 (unsigned long long)r->end, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
840 return p-buf;
841}
842
843#endif /* CONFIG_PROC_FS */
844
845/*
846 * This is a version of find_resource and it belongs to kernel/resource.c.
847 * Until we have agreement with Linus and Martin, it lingers here.
848 *
849 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
850 * This probably warrants some sort of hashing.
851 */
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700852static struct resource *_sparc_find_resource(struct resource *root,
853 unsigned long hit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct resource *tmp;
856
857 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
858 if (tmp->start <= hit && tmp->end >= hit)
859 return tmp;
860 }
861 return NULL;
862}
863
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700864static void register_proc_sparc_ioport(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866#ifdef CONFIG_PROC_FS
867 create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
868 create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
869#endif
870}