blob: aa73b3b71e8513773703a06780f4e9def03bf180 [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. Millere3a411a32006-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{
David S. Miller7a715f42008-08-27 18:37:58 -0700397 struct sbus_dev *sdev = to_sbus_device(dev);
398
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200399 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (len <= 0) {
401 return 0;
402 }
403 /* XXX So what is maxphys for us and how do drivers know it? */
404 if (len > 256*1024) { /* __get_free_pages() limit */
405 return 0;
406 }
407 return mmu_get_scsi_one(va, len, sdev->bus);
408}
409
David S. Miller7a715f42008-08-27 18:37:58 -0700410void sbus_unmap_single(struct device *dev, dma_addr_t ba, size_t n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
David S. Miller7a715f42008-08-27 18:37:58 -0700412 struct sbus_dev *sdev = to_sbus_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 mmu_release_scsi_one(ba, n, sdev->bus);
414}
415
David S. Miller7a715f42008-08-27 18:37:58 -0700416int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
David S. Miller7a715f42008-08-27 18:37:58 -0700418 struct sbus_dev *sdev = to_sbus_device(dev);
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mmu_get_scsi_sgl(sg, n, sdev->bus);
421
422 /*
423 * XXX sparc64 can return a partial length here. sun4c should do this
424 * but it currently panics if it can't fulfill the request - Anton
425 */
426 return n;
427}
428
David S. Miller7a715f42008-08-27 18:37:58 -0700429void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
David S. Miller7a715f42008-08-27 18:37:58 -0700431 struct sbus_dev *sdev = to_sbus_device(dev);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 mmu_release_scsi_sgl(sg, n, sdev->bus);
434}
435
436/*
437 */
David S. Miller7a715f42008-08-27 18:37:58 -0700438void 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 -0700439{
440#if 0
David S. Miller7a715f42008-08-27 18:37:58 -0700441 struct sbus_dev *sdev = to_sbus_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 unsigned long va;
443 struct resource *res;
444
445 /* We do not need the resource, just print a message if invalid. */
446 res = _sparc_find_resource(&_sparc_dvma, ba);
447 if (res == NULL)
448 panic("sbus_dma_sync_single: 0x%x\n", ba);
449
450 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
451 /*
452 * XXX This bogosity will be fixed with the iommu rewrite coming soon
453 * to a kernel near you. - Anton
454 */
455 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
456#endif
457}
458
David S. Miller7a715f42008-08-27 18:37:58 -0700459void 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 -0700460{
461#if 0
David S. Miller7a715f42008-08-27 18:37:58 -0700462 struct sbus_dev *sdev = to_sbus_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 unsigned long va;
464 struct resource *res;
465
466 /* We do not need the resource, just print a message if invalid. */
467 res = _sparc_find_resource(&_sparc_dvma, ba);
468 if (res == NULL)
469 panic("sbus_dma_sync_single: 0x%x\n", ba);
470
471 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
472 /*
473 * XXX This bogosity will be fixed with the iommu rewrite coming soon
474 * to a kernel near you. - Anton
475 */
476 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
477#endif
478}
479
David S. Miller576c3522006-06-23 15:55:45 -0700480/* 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 &&
David S. Millere0039342008-08-25 22:47:20 -0700515 !strcmp(parent->name, "iommu"))
516 iommu_init(parent, sbus);
David S. Miller576c3522006-06-23 15:55:45 -0700517
David S. Millere0039342008-08-25 22:47:20 -0700518 if (sparc_cpu_model == sun4d)
519 iounit_init(sbus);
Al Viro5932ef02006-09-23 01:26:02 +0100520#endif
David S. Miller576c3522006-06-23 15:55:45 -0700521}
522
523void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
524{
525 if (sparc_cpu_model == sun4d) {
526 struct device_node *parent = dp->parent;
527
528 sbus->devid = of_getintprop_default(parent, "device-id", 0);
529 sbus->board = of_getintprop_default(parent, "board#", 0);
530 }
531}
532
533int __init sbus_arch_preinit(void)
534{
David S. Miller576c3522006-06-23 15:55:45 -0700535 register_proc_sparc_ioport();
536
537#ifdef CONFIG_SUN4
538 {
539 extern void sun4_dvma_init(void);
540 sun4_dvma_init();
541 }
542 return 1;
543#else
544 return 0;
545#endif
546}
547
548void __init sbus_arch_postinit(void)
549{
550 if (sparc_cpu_model == sun4d) {
551 extern void sun4d_init_sbi_irq(void);
552 sun4d_init_sbi_irq();
553 }
554}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#endif /* CONFIG_SBUS */
556
557#ifdef CONFIG_PCI
558
559/* Allocate and map kernel buffer using consistent mode DMA for a device.
560 * hwdev should be valid struct pci_dev pointer for PCI devices.
561 */
562void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
563{
564 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
565 unsigned long va;
566 struct resource *res;
567 int order;
568
569 if (len == 0) {
570 return NULL;
571 }
572 if (len > 256*1024) { /* __get_free_pages() limit */
573 return NULL;
574 }
575
576 order = get_order(len_total);
577 va = __get_free_pages(GFP_KERNEL, order);
578 if (va == 0) {
579 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
580 return NULL;
581 }
582
Yan Burmanc80892d2006-11-30 17:07:04 -0800583 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 free_pages(va, order);
585 printk("pci_alloc_consistent: no core\n");
586 return NULL;
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 if (allocate_resource(&_sparc_dvma, res, len_total,
590 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
591 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
592 free_pages(va, order);
593 kfree(res);
594 return NULL;
595 }
596 mmu_inval_dma_area(va, len_total);
597#if 0
598/* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
599 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
600#endif
601 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
602
603 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
604 return (void *) res->start;
605}
606
607/* Free and unmap a consistent DMA buffer.
608 * cpu_addr is what was returned from pci_alloc_consistent,
609 * size must be the same as what as passed into pci_alloc_consistent,
610 * and likewise dma_addr must be the same as what *dma_addrp was set to.
611 *
Simon Arlottd1a78c32007-05-11 13:51:23 -0700612 * References to the memory and mappings associated with cpu_addr/dma_addr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * past this call are illegal.
614 */
615void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
616{
617 struct resource *res;
618 unsigned long pgp;
619
620 if ((res = _sparc_find_resource(&_sparc_dvma,
621 (unsigned long)p)) == NULL) {
622 printk("pci_free_consistent: cannot free %p\n", p);
623 return;
624 }
625
626 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
627 printk("pci_free_consistent: unaligned va %p\n", p);
628 return;
629 }
630
631 n = (n + PAGE_SIZE-1) & PAGE_MASK;
632 if ((res->end-res->start)+1 != n) {
633 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
634 (long)((res->end-res->start)+1), (long)n);
635 return;
636 }
637
638 pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
639 mmu_inval_dma_area(pgp, n);
640 sparc_unmapiorange((unsigned long)p, n);
641
642 release_resource(res);
643 kfree(res);
644
645 free_pages(pgp, get_order(n));
646}
647
648/* Map a single buffer of the indicated size for DMA in streaming mode.
649 * The 32-bit bus address to use is returned.
650 *
651 * Once the device is given the dma address, the device owns this memory
652 * until either pci_unmap_single or pci_dma_sync_single_* is performed.
653 */
654dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
655 int direction)
656{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800657 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* IIep is write-through, not flushing. */
659 return virt_to_phys(ptr);
660}
661
662/* Unmap a single streaming mode DMA translation. The dma_addr and size
663 * must match what was provided for in a previous pci_map_single call. All
664 * other usages are undefined.
665 *
666 * After this call, reads by the cpu to the buffer are guaranteed to see
667 * whatever the device wrote there.
668 */
669void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
670 int direction)
671{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800672 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (direction != PCI_DMA_TODEVICE) {
674 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
675 (size + PAGE_SIZE-1) & PAGE_MASK);
676 }
677}
678
679/*
680 * Same as pci_map_single, but with pages.
681 */
682dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
683 unsigned long offset, size_t size, int direction)
684{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800685 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* IIep is write-through, not flushing. */
687 return page_to_phys(page) + offset;
688}
689
690void pci_unmap_page(struct pci_dev *hwdev,
691 dma_addr_t dma_address, size_t size, int direction)
692{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800693 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* mmu_inval_dma_area XXX */
695}
696
697/* Map a set of buffers described by scatterlist in streaming
698 * mode for DMA. This is the scather-gather version of the
699 * above pci_map_single interface. Here the scatter gather list
700 * elements are each tagged with the appropriate dma address
701 * and length. They are obtained via sg_dma_{address,length}(SG).
702 *
703 * NOTE: An implementation may be able to use a smaller number of
704 * DMA address/length pairs than there are SG table elements.
705 * (for example via virtual mapping capabilities)
706 * The routine returns the number of addr/length pairs actually
707 * used, at most nents.
708 *
709 * Device ownership issues as mentioned above for pci_map_single are
710 * the same here.
711 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200712int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int direction)
714{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200715 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int n;
717
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800718 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* IIep is write-through, not flushing. */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200720 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200721 BUG_ON(page_address(sg_page(sg)) == NULL);
722 sg->dvma_address = virt_to_phys(sg_virt(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 sg->dvma_length = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 return nents;
726}
727
728/* Unmap a set of streaming mode DMA translations.
729 * Again, cpu read rules concerning calls here are the same as for
730 * pci_unmap_single() above.
731 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200732void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int direction)
734{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200735 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int n;
737
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800738 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200740 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200741 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200743 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 }
747}
748
749/* Make physical memory consistent for a single
750 * streaming mode DMA translation before or after a transfer.
751 *
752 * If you perform a pci_map_single() but wish to interrogate the
753 * buffer using the cpu, yet do not wish to teardown the PCI dma
754 * mapping, you must call this function before doing so. At the
755 * next point you give the PCI dma address back to the card, you
756 * must first perform a pci_dma_sync_for_device, and then the
757 * device again owns the buffer.
758 */
759void pci_dma_sync_single_for_cpu(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
768void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
769{
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800770 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (direction != PCI_DMA_TODEVICE) {
772 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
773 (size + PAGE_SIZE-1) & PAGE_MASK);
774 }
775}
776
777/* Make physical memory consistent for a set of streaming
778 * mode DMA translations after a transfer.
779 *
780 * The same as pci_dma_sync_single_* but for a scatter-gather list,
781 * same rules and usage.
782 */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200783void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200785 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int n;
787
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800788 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200790 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200791 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200793 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 }
797}
798
Jens Axboe0912a5d2007-05-14 15:44:38 +0200799void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200801 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int n;
803
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800804 BUG_ON(direction == PCI_DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (direction != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200806 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200807 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200809 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 }
813}
814#endif /* CONFIG_PCI */
815
816#ifdef CONFIG_PROC_FS
817
818static int
819_sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
820 void *data)
821{
822 char *p = buf, *e = buf + length;
823 struct resource *r;
824 const char *nm;
825
826 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
827 if (p + 32 >= e) /* Better than nothing */
828 break;
829 if ((nm = r->name) == 0) nm = "???";
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -0700830 p += sprintf(p, "%016llx-%016llx: %s\n",
831 (unsigned long long)r->start,
832 (unsigned long long)r->end, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
835 return p-buf;
836}
837
838#endif /* CONFIG_PROC_FS */
839
840/*
841 * This is a version of find_resource and it belongs to kernel/resource.c.
842 * Until we have agreement with Linus and Martin, it lingers here.
843 *
844 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
845 * This probably warrants some sort of hashing.
846 */
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700847static struct resource *_sparc_find_resource(struct resource *root,
848 unsigned long hit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct resource *tmp;
851
852 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
853 if (tmp->start <= hit && tmp->end >= hit)
854 return tmp;
855 }
856 return NULL;
857}
858
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700859static void register_proc_sparc_ioport(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861#ifdef CONFIG_PROC_FS
862 create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
863 create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
864#endif
865}