blob: 54d51b404603091e377f1e311df323b64caffc12 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: ioport.c,v 1.45 2001/10/30 04:54:21 davem Exp $
2 * ioport.c: Simple io mapping allocator.
3 *
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
6 *
7 * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
8 *
9 * 2000/01/29
10 * <rth> zait: as long as pci_alloc_consistent produces something addressable,
11 * things are ok.
12 * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a
13 * pointer into the big page mapping
14 * <rth> zait: so what?
15 * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page()))
16 * <zaitcev> Hmm
17 * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
18 * So far so good.
19 * <zaitcev> Now, driver calls pci_free_consistent(with result of
20 * remap_it_my_way()).
21 * <zaitcev> How do you find the address to pass to free_pages()?
22 * <rth> zait: walk the page tables? It's only two or three level after all.
23 * <rth> zait: you have to walk them anyway to remove the mapping.
24 * <zaitcev> Hmm
25 * <zaitcev> Sounds reasonable
26 */
27
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>
38
39#include <asm/io.h>
40#include <asm/vaddrs.h>
41#include <asm/oplib.h>
David S. Miller576c3522006-06-23 15:55:45 -070042#include <asm/prom.h>
David S. Miller3ca9fab2006-06-29 14:35:33 -070043#include <asm/of_device.h>
David S. Miller576c3522006-06-23 15:55:45 -070044#include <asm/sbus.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/page.h>
46#include <asm/pgalloc.h>
47#include <asm/dma.h>
48
49#define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
50
51struct resource *_sparc_find_resource(struct resource *r, unsigned long);
52
53static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
54static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
55 unsigned long size, char *name);
56static void _sparc_free_io(struct resource *res);
57
58/* This points to the next to use virtual memory for DVMA mappings */
59static struct resource _sparc_dvma = {
60 .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
61};
62/* This points to the start of I/O mappings, cluable from outside. */
63/*ext*/ struct resource sparc_iomap = {
64 .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
65};
66
67/*
68 * Our mini-allocator...
69 * Boy this is gross! We need it because we must map I/O for
70 * timers and interrupt controller before the kmalloc is available.
71 */
72
73#define XNMLN 15
74#define XNRES 10 /* SS-10 uses 8 */
75
76struct xresource {
77 struct resource xres; /* Must be first */
78 int xflag; /* 1 == used */
79 char xname[XNMLN+1];
80};
81
82static struct xresource xresv[XNRES];
83
84static struct xresource *xres_alloc(void) {
85 struct xresource *xrp;
86 int n;
87
88 xrp = xresv;
89 for (n = 0; n < XNRES; n++) {
90 if (xrp->xflag == 0) {
91 xrp->xflag = 1;
92 return xrp;
93 }
94 xrp++;
95 }
96 return NULL;
97}
98
99static void xres_free(struct xresource *xrp) {
100 xrp->xflag = 0;
101}
102
103/*
104 * These are typically used in PCI drivers
105 * which are trying to be cross-platform.
106 *
107 * Bus type is always zero on IIep.
108 */
109void __iomem *ioremap(unsigned long offset, unsigned long size)
110{
111 char name[14];
112
113 sprintf(name, "phys_%08x", (u32)offset);
114 return _sparc_alloc_io(0, offset, size, name);
115}
116
117/*
118 * Comlimentary to ioremap().
119 */
120void iounmap(volatile void __iomem *virtual)
121{
122 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
123 struct resource *res;
124
125 if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
126 printk("free_io/iounmap: cannot free %lx\n", vaddr);
127 return;
128 }
129 _sparc_free_io(res);
130
131 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
132 xres_free((struct xresource *)res);
133 } else {
134 kfree(res);
135 }
136}
137
138/*
139 */
140void __iomem *sbus_ioremap(struct resource *phyres, unsigned long offset,
141 unsigned long size, char *name)
142{
143 return _sparc_alloc_io(phyres->flags & 0xF,
144 phyres->start + offset, size, name);
145}
146
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
156void of_iounmap(void __iomem *base, unsigned long size)
157{
158 iounmap(base);
159}
160EXPORT_SYMBOL(of_iounmap);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
163 */
164void sbus_iounmap(volatile void __iomem *addr, unsigned long size)
165{
166 iounmap(addr);
167}
168
169/*
170 * Meat of mapping
171 */
172static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
173 unsigned long size, char *name)
174{
175 static int printed_full;
176 struct xresource *xres;
177 struct resource *res;
178 char *tack;
179 int tlen;
180 void __iomem *va; /* P3 diag */
181
182 if (name == NULL) name = "???";
183
184 if ((xres = xres_alloc()) != 0) {
185 tack = xres->xname;
186 res = &xres->xres;
187 } else {
188 if (!printed_full) {
189 printk("ioremap: done with statics, switching to malloc\n");
190 printed_full = 1;
191 }
192 tlen = strlen(name);
193 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
194 if (tack == NULL) return NULL;
195 memset(tack, 0, sizeof(struct resource));
196 res = (struct resource *) tack;
197 tack += sizeof (struct resource);
198 }
199
200 strlcpy(tack, name, XNMLN+1);
201 res->name = tack;
202
203 va = _sparc_ioremap(res, busno, phys, size);
204 /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
205 return va;
206}
207
208/*
209 */
210static void __iomem *
211_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
212{
213 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
214
215 if (allocate_resource(&sparc_iomap, res,
216 (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
217 sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
218 /* Usually we cannot see printks in this case. */
219 prom_printf("alloc_io_res(%s): cannot occupy\n",
220 (res->name != NULL)? res->name: "???");
221 prom_halt();
222 }
223
224 pa &= PAGE_MASK;
225 sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
226
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700227 return (void __iomem *)(unsigned long)(res->start + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230/*
231 * Comlimentary to _sparc_ioremap().
232 */
233static void _sparc_free_io(struct resource *res)
234{
235 unsigned long plen;
236
237 plen = res->end - res->start + 1;
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800238 BUG_ON((plen & (PAGE_SIZE-1)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 sparc_unmapiorange(res->start, plen);
240 release_resource(res);
241}
242
243#ifdef CONFIG_SBUS
244
David S. Miller8fae0972006-06-20 15:23:28 -0700245void sbus_set_sbus64(struct sbus_dev *sdev, int x)
246{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 printk("sbus_set_sbus64: unsupported\n");
248}
249
David S. Miller8fae0972006-06-20 15:23:28 -0700250extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
251void __init sbus_fill_device_irq(struct sbus_dev *sdev)
252{
253 struct linux_prom_irqs irqs[PROMINTR_MAX];
254 int len;
255
256 len = prom_getproperty(sdev->prom_node, "intr",
257 (char *)irqs, sizeof(irqs));
258 if (len != -1) {
259 sdev->num_irqs = len / 8;
260 if (sdev->num_irqs == 0) {
261 sdev->irqs[0] = 0;
262 } else if (sparc_cpu_model == sun4d) {
263 for (len = 0; len < sdev->num_irqs; len++)
264 sdev->irqs[len] =
265 sun4d_build_irq(sdev, irqs[len].pri);
266 } else {
267 for (len = 0; len < sdev->num_irqs; len++)
268 sdev->irqs[len] = irqs[len].pri;
269 }
270 } else {
271 int interrupts[PROMINTR_MAX];
272
273 /* No "intr" node found-- check for "interrupts" node.
274 * This node contains SBus interrupt levels, not IPLs
275 * as in "intr", and no vector values. We convert
276 * SBus interrupt levels to PILs (platform specific).
277 */
278 len = prom_getproperty(sdev->prom_node, "interrupts",
279 (char *)interrupts, sizeof(interrupts));
280 if (len == -1) {
281 sdev->irqs[0] = 0;
282 sdev->num_irqs = 0;
283 } else {
284 sdev->num_irqs = len / sizeof(int);
285 for (len = 0; len < sdev->num_irqs; len++) {
286 sdev->irqs[len] =
287 sbint_to_irq(sdev, interrupts[len]);
288 }
289 }
290 }
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * Allocate a chunk of memory suitable for DMA.
295 * Typically devices use them for control blocks.
296 * CPU may access them without any explicit flushing.
297 *
298 * XXX Some clever people know that sdev is not used and supply NULL. Watch.
299 */
300void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
301{
302 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
303 unsigned long va;
304 struct resource *res;
305 int order;
306
307 /* XXX why are some lenghts signed, others unsigned? */
308 if (len <= 0) {
309 return NULL;
310 }
311 /* XXX So what is maxphys for us and how do drivers know it? */
312 if (len > 256*1024) { /* __get_free_pages() limit */
313 return NULL;
314 }
315
316 order = get_order(len_total);
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800317 if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto err_nopages;
319
320 if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
321 goto err_nomem;
322 memset((char*)res, 0, sizeof(struct resource));
323
324 if (allocate_resource(&_sparc_dvma, res, len_total,
325 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
326 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
327 goto err_nova;
328 }
329 mmu_inval_dma_area(va, len_total);
330 // XXX The mmu_map_dma_area does this for us below, see comments.
331 // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
332 /*
333 * XXX That's where sdev would be used. Currently we load
334 * all iommu tables with the same translations.
335 */
336 if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0)
337 goto err_noiommu;
338
Martin Habets4cfbd7e2006-05-07 23:43:19 -0700339 /* Set the resource name, if known. */
340 if (sdev) {
341 res->name = sdev->prom_name;
342 }
343
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700344 return (void *)(unsigned long)res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346err_noiommu:
347 release_resource(res);
348err_nova:
349 free_pages(va, order);
350err_nomem:
351 kfree(res);
352err_nopages:
353 return NULL;
354}
355
356void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
357{
358 struct resource *res;
359 struct page *pgv;
360
361 if ((res = _sparc_find_resource(&_sparc_dvma,
362 (unsigned long)p)) == NULL) {
363 printk("sbus_free_consistent: cannot free %p\n", p);
364 return;
365 }
366
367 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
368 printk("sbus_free_consistent: unaligned va %p\n", p);
369 return;
370 }
371
372 n = (n + PAGE_SIZE-1) & PAGE_MASK;
373 if ((res->end-res->start)+1 != n) {
374 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
375 (long)((res->end-res->start)+1), n);
376 return;
377 }
378
379 release_resource(res);
380 kfree(res);
381
382 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
383 pgv = mmu_translate_dvma(ba);
384 mmu_unmap_dma_area(ba, n);
385
386 __free_pages(pgv, get_order(n));
387}
388
389/*
390 * Map a chunk of memory so that devices can see it.
391 * CPU view of this memory may be inconsistent with
392 * a device view and explicit flushing is necessary.
393 */
394dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
395{
396 /* XXX why are some lenghts signed, others unsigned? */
397 if (len <= 0) {
398 return 0;
399 }
400 /* XXX So what is maxphys for us and how do drivers know it? */
401 if (len > 256*1024) { /* __get_free_pages() limit */
402 return 0;
403 }
404 return mmu_get_scsi_one(va, len, sdev->bus);
405}
406
407void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
408{
409 mmu_release_scsi_one(ba, n, sdev->bus);
410}
411
412int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
413{
414 mmu_get_scsi_sgl(sg, n, sdev->bus);
415
416 /*
417 * XXX sparc64 can return a partial length here. sun4c should do this
418 * but it currently panics if it can't fulfill the request - Anton
419 */
420 return n;
421}
422
423void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
424{
425 mmu_release_scsi_sgl(sg, n, sdev->bus);
426}
427
428/*
429 */
430void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
431{
432#if 0
433 unsigned long va;
434 struct resource *res;
435
436 /* We do not need the resource, just print a message if invalid. */
437 res = _sparc_find_resource(&_sparc_dvma, ba);
438 if (res == NULL)
439 panic("sbus_dma_sync_single: 0x%x\n", ba);
440
441 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
442 /*
443 * XXX This bogosity will be fixed with the iommu rewrite coming soon
444 * to a kernel near you. - Anton
445 */
446 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
447#endif
448}
449
450void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
451{
452#if 0
453 unsigned long va;
454 struct resource *res;
455
456 /* We do not need the resource, just print a message if invalid. */
457 res = _sparc_find_resource(&_sparc_dvma, ba);
458 if (res == NULL)
459 panic("sbus_dma_sync_single: 0x%x\n", ba);
460
461 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
462 /*
463 * XXX This bogosity will be fixed with the iommu rewrite coming soon
464 * to a kernel near you. - Anton
465 */
466 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
467#endif
468}
469
470void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
471{
472 printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n");
473}
474
475void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
476{
477 printk("sbus_dma_sync_sg_for_device: not implemented yet\n");
478}
David S. Miller576c3522006-06-23 15:55:45 -0700479
480/* Support code for sbus_init(). */
481/*
482 * XXX This functions appears to be a distorted version of
483 * prom_sbus_ranges_init(), with all sun4d stuff cut away.
484 * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
485 */
486/* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
487void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
488{
489 int parent_node = pn->node;
490
491 if (sparc_cpu_model == sun4d) {
492 struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
493 int num_iounit_ranges, len;
494
495 len = prom_getproperty(parent_node, "ranges",
496 (char *) iounit_ranges,
497 sizeof (iounit_ranges));
498 if (len != -1) {
499 num_iounit_ranges =
500 (len / sizeof(struct linux_prom_ranges));
501 prom_adjust_ranges(sbus->sbus_ranges,
502 sbus->num_sbus_ranges,
503 iounit_ranges, num_iounit_ranges);
504 }
505 }
506}
507
508void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
509{
Al Viro5932ef02006-09-23 01:26:02 +0100510#ifndef CONFIG_SUN4
David S. Miller576c3522006-06-23 15:55:45 -0700511 struct device_node *parent = dp->parent;
512
513 if (sparc_cpu_model != sun4d &&
514 parent != NULL &&
515 !strcmp(parent->name, "iommu")) {
516 extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
517
518 iommu_init(parent->node, sbus);
519 }
520
521 if (sparc_cpu_model == sun4d) {
522 extern void iounit_init(int sbi_node, int iounit_node,
523 struct sbus_bus *sbus);
524
525 iounit_init(dp->node, parent->node, sbus);
526 }
Al Viro5932ef02006-09-23 01:26:02 +0100527#endif
David S. Miller576c3522006-06-23 15:55:45 -0700528}
529
530void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
531{
532 if (sparc_cpu_model == sun4d) {
533 struct device_node *parent = dp->parent;
534
535 sbus->devid = of_getintprop_default(parent, "device-id", 0);
536 sbus->board = of_getintprop_default(parent, "board#", 0);
537 }
538}
539
540int __init sbus_arch_preinit(void)
541{
542 extern void register_proc_sparc_ioport(void);
543
544 register_proc_sparc_ioport();
545
546#ifdef CONFIG_SUN4
547 {
548 extern void sun4_dvma_init(void);
549 sun4_dvma_init();
550 }
551 return 1;
552#else
553 return 0;
554#endif
555}
556
557void __init sbus_arch_postinit(void)
558{
559 if (sparc_cpu_model == sun4d) {
560 extern void sun4d_init_sbi_irq(void);
561 sun4d_init_sbi_irq();
562 }
563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#endif /* CONFIG_SBUS */
565
566#ifdef CONFIG_PCI
567
568/* Allocate and map kernel buffer using consistent mode DMA for a device.
569 * hwdev should be valid struct pci_dev pointer for PCI devices.
570 */
571void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
572{
573 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
574 unsigned long va;
575 struct resource *res;
576 int order;
577
578 if (len == 0) {
579 return NULL;
580 }
581 if (len > 256*1024) { /* __get_free_pages() limit */
582 return NULL;
583 }
584
585 order = get_order(len_total);
586 va = __get_free_pages(GFP_KERNEL, order);
587 if (va == 0) {
588 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
589 return NULL;
590 }
591
592 if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
593 free_pages(va, order);
594 printk("pci_alloc_consistent: no core\n");
595 return NULL;
596 }
597 memset((char*)res, 0, sizeof(struct resource));
598
599 if (allocate_resource(&_sparc_dvma, res, len_total,
600 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
601 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
602 free_pages(va, order);
603 kfree(res);
604 return NULL;
605 }
606 mmu_inval_dma_area(va, len_total);
607#if 0
608/* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
609 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
610#endif
611 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
612
613 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
614 return (void *) res->start;
615}
616
617/* Free and unmap a consistent DMA buffer.
618 * cpu_addr is what was returned from pci_alloc_consistent,
619 * size must be the same as what as passed into pci_alloc_consistent,
620 * and likewise dma_addr must be the same as what *dma_addrp was set to.
621 *
622 * References to the memory and mappings assosciated with cpu_addr/dma_addr
623 * past this call are illegal.
624 */
625void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
626{
627 struct resource *res;
628 unsigned long pgp;
629
630 if ((res = _sparc_find_resource(&_sparc_dvma,
631 (unsigned long)p)) == NULL) {
632 printk("pci_free_consistent: cannot free %p\n", p);
633 return;
634 }
635
636 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
637 printk("pci_free_consistent: unaligned va %p\n", p);
638 return;
639 }
640
641 n = (n + PAGE_SIZE-1) & PAGE_MASK;
642 if ((res->end-res->start)+1 != n) {
643 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
644 (long)((res->end-res->start)+1), (long)n);
645 return;
646 }
647
648 pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
649 mmu_inval_dma_area(pgp, n);
650 sparc_unmapiorange((unsigned long)p, n);
651
652 release_resource(res);
653 kfree(res);
654
655 free_pages(pgp, get_order(n));
656}
657
658/* Map a single buffer of the indicated size for DMA in streaming mode.
659 * The 32-bit bus address to use is returned.
660 *
661 * Once the device is given the dma address, the device owns this memory
662 * until either pci_unmap_single or pci_dma_sync_single_* is performed.
663 */
664dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
665 int direction)
666{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800667 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* IIep is write-through, not flushing. */
669 return virt_to_phys(ptr);
670}
671
672/* Unmap a single streaming mode DMA translation. The dma_addr and size
673 * must match what was provided for in a previous pci_map_single call. All
674 * other usages are undefined.
675 *
676 * After this call, reads by the cpu to the buffer are guaranteed to see
677 * whatever the device wrote there.
678 */
679void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
680 int direction)
681{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800682 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (direction != PCI_DMA_TODEVICE) {
684 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
685 (size + PAGE_SIZE-1) & PAGE_MASK);
686 }
687}
688
689/*
690 * Same as pci_map_single, but with pages.
691 */
692dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
693 unsigned long offset, size_t size, int direction)
694{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800695 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* IIep is write-through, not flushing. */
697 return page_to_phys(page) + offset;
698}
699
700void pci_unmap_page(struct pci_dev *hwdev,
701 dma_addr_t dma_address, size_t size, int direction)
702{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800703 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /* mmu_inval_dma_area XXX */
705}
706
707/* Map a set of buffers described by scatterlist in streaming
708 * mode for DMA. This is the scather-gather version of the
709 * above pci_map_single interface. Here the scatter gather list
710 * elements are each tagged with the appropriate dma address
711 * and length. They are obtained via sg_dma_{address,length}(SG).
712 *
713 * NOTE: An implementation may be able to use a smaller number of
714 * DMA address/length pairs than there are SG table elements.
715 * (for example via virtual mapping capabilities)
716 * The routine returns the number of addr/length pairs actually
717 * used, at most nents.
718 *
719 * Device ownership issues as mentioned above for pci_map_single are
720 * the same here.
721 */
722int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
723 int direction)
724{
725 int n;
726
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800727 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* IIep is write-through, not flushing. */
729 for (n = 0; n < nents; n++) {
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800730 BUG_ON(page_address(sg->page) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 sg->dvma_address = virt_to_phys(page_address(sg->page));
732 sg->dvma_length = sg->length;
733 sg++;
734 }
735 return nents;
736}
737
738/* Unmap a set of streaming mode DMA translations.
739 * Again, cpu read rules concerning calls here are the same as for
740 * pci_unmap_single() above.
741 */
742void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
743 int direction)
744{
745 int n;
746
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800747 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (direction != PCI_DMA_TODEVICE) {
749 for (n = 0; n < nents; n++) {
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800750 BUG_ON(page_address(sg->page) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 mmu_inval_dma_area(
752 (unsigned long) page_address(sg->page),
753 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
754 sg++;
755 }
756 }
757}
758
759/* Make physical memory consistent for a single
760 * streaming mode DMA translation before or after a transfer.
761 *
762 * If you perform a pci_map_single() but wish to interrogate the
763 * buffer using the cpu, yet do not wish to teardown the PCI dma
764 * mapping, you must call this function before doing so. At the
765 * next point you give the PCI dma address back to the card, you
766 * must first perform a pci_dma_sync_for_device, and then the
767 * device again owns the buffer.
768 */
769void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
770{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800771 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (direction != PCI_DMA_TODEVICE) {
773 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
774 (size + PAGE_SIZE-1) & PAGE_MASK);
775 }
776}
777
778void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
779{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800780 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (direction != PCI_DMA_TODEVICE) {
782 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
783 (size + PAGE_SIZE-1) & PAGE_MASK);
784 }
785}
786
787/* Make physical memory consistent for a set of streaming
788 * mode DMA translations after a transfer.
789 *
790 * The same as pci_dma_sync_single_* but for a scatter-gather list,
791 * same rules and usage.
792 */
793void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
794{
795 int n;
796
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800797 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (direction != PCI_DMA_TODEVICE) {
799 for (n = 0; n < nents; n++) {
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800800 BUG_ON(page_address(sg->page) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 mmu_inval_dma_area(
802 (unsigned long) page_address(sg->page),
803 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
804 sg++;
805 }
806 }
807}
808
809void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
810{
811 int n;
812
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800813 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (direction != PCI_DMA_TODEVICE) {
815 for (n = 0; n < nents; n++) {
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800816 BUG_ON(page_address(sg->page) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 mmu_inval_dma_area(
818 (unsigned long) page_address(sg->page),
819 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
820 sg++;
821 }
822 }
823}
824#endif /* CONFIG_PCI */
825
826#ifdef CONFIG_PROC_FS
827
828static int
829_sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
830 void *data)
831{
832 char *p = buf, *e = buf + length;
833 struct resource *r;
834 const char *nm;
835
836 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
837 if (p + 32 >= e) /* Better than nothing */
838 break;
839 if ((nm = r->name) == 0) nm = "???";
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700840 p += sprintf(p, "%016llx-%016llx: %s\n",
841 (unsigned long long)r->start,
842 (unsigned long long)r->end, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
845 return p-buf;
846}
847
848#endif /* CONFIG_PROC_FS */
849
850/*
851 * This is a version of find_resource and it belongs to kernel/resource.c.
852 * Until we have agreement with Linus and Martin, it lingers here.
853 *
854 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
855 * This probably warrants some sort of hashing.
856 */
857struct resource *
858_sparc_find_resource(struct resource *root, unsigned long hit)
859{
860 struct resource *tmp;
861
862 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
863 if (tmp->start <= hit && tmp->end >= hit)
864 return tmp;
865 }
866 return NULL;
867}
868
869void register_proc_sparc_ioport(void)
870{
871#ifdef CONFIG_PROC_FS
872 create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
873 create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
874#endif
875}