blob: 9f61fd8cbb7b80d8ade28e73623567306bc171ee [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>
Alexey Dobriyane7a088f2009-09-01 17:54:07 -070038#include <linux/seq_file.h>
Jens Axboe0912a5d2007-05-14 15:44:38 +020039#include <linux/scatterlist.h>
Stephen Rothwell764f2572008-08-07 15:33:36 -070040#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/io.h>
43#include <asm/vaddrs.h>
44#include <asm/oplib.h>
David S. Miller576c3522006-06-23 15:55:45 -070045#include <asm/prom.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}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800122EXPORT_SYMBOL(ioremap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/*
125 * Comlimentary to ioremap().
126 */
127void iounmap(volatile void __iomem *virtual)
128{
129 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
130 struct resource *res;
131
132 if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
133 printk("free_io/iounmap: cannot free %lx\n", vaddr);
134 return;
135 }
136 _sparc_free_io(res);
137
138 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
139 xres_free((struct xresource *)res);
140 } else {
141 kfree(res);
142 }
143}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800144EXPORT_SYMBOL(iounmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
David S. Miller3ca9fab2006-06-29 14:35:33 -0700146void __iomem *of_ioremap(struct resource *res, unsigned long offset,
147 unsigned long size, char *name)
148{
149 return _sparc_alloc_io(res->flags & 0xF,
150 res->start + offset,
151 size, name);
152}
153EXPORT_SYMBOL(of_ioremap);
154
David S. Millere3a411a2006-12-28 21:01:32 -0800155void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
David S. Miller3ca9fab2006-06-29 14:35:33 -0700156{
157 iounmap(base);
158}
159EXPORT_SYMBOL(of_iounmap);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * Meat of mapping
163 */
164static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
165 unsigned long size, char *name)
166{
167 static int printed_full;
168 struct xresource *xres;
169 struct resource *res;
170 char *tack;
171 int tlen;
172 void __iomem *va; /* P3 diag */
173
174 if (name == NULL) name = "???";
175
176 if ((xres = xres_alloc()) != 0) {
177 tack = xres->xname;
178 res = &xres->xres;
179 } else {
180 if (!printed_full) {
181 printk("ioremap: done with statics, switching to malloc\n");
182 printed_full = 1;
183 }
184 tlen = strlen(name);
185 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
186 if (tack == NULL) return NULL;
187 memset(tack, 0, sizeof(struct resource));
188 res = (struct resource *) tack;
189 tack += sizeof (struct resource);
190 }
191
192 strlcpy(tack, name, XNMLN+1);
193 res->name = tack;
194
195 va = _sparc_ioremap(res, busno, phys, size);
196 /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
197 return va;
198}
199
200/*
201 */
202static void __iomem *
203_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
204{
205 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
206
207 if (allocate_resource(&sparc_iomap, res,
208 (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
209 sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
210 /* Usually we cannot see printks in this case. */
211 prom_printf("alloc_io_res(%s): cannot occupy\n",
212 (res->name != NULL)? res->name: "???");
213 prom_halt();
214 }
215
216 pa &= PAGE_MASK;
217 sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
218
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700219 return (void __iomem *)(unsigned long)(res->start + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/*
223 * Comlimentary to _sparc_ioremap().
224 */
225static void _sparc_free_io(struct resource *res)
226{
227 unsigned long plen;
228
229 plen = res->end - res->start + 1;
Eric Sesterhenn30d4d1f2006-03-10 02:55:20 -0800230 BUG_ON((plen & (PAGE_SIZE-1)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 sparc_unmapiorange(res->start, plen);
232 release_resource(res);
233}
234
235#ifdef CONFIG_SBUS
236
David S. Miller63237ee2008-08-26 23:33:42 -0700237void sbus_set_sbus64(struct device *dev, int x)
David S. Miller8fae0972006-06-20 15:23:28 -0700238{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 printk("sbus_set_sbus64: unsupported\n");
240}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800241EXPORT_SYMBOL(sbus_set_sbus64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/*
244 * Allocate a chunk of memory suitable for DMA.
245 * Typically devices use them for control blocks.
246 * CPU may access them without any explicit flushing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900248static void *sbus_alloc_coherent(struct device *dev, size_t len,
249 dma_addr_t *dma_addrp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
David S. Miller7a715f42008-08-27 18:37:58 -0700251 struct of_device *op = to_of_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
253 unsigned long va;
254 struct resource *res;
255 int order;
256
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200257 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (len <= 0) {
259 return NULL;
260 }
261 /* XXX So what is maxphys for us and how do drivers know it? */
262 if (len > 256*1024) { /* __get_free_pages() limit */
263 return NULL;
264 }
265
266 order = get_order(len_total);
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800267 if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto err_nopages;
269
Yan Burmanc80892d2006-11-30 17:07:04 -0800270 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (allocate_resource(&_sparc_dvma, res, len_total,
274 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
275 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
276 goto err_nova;
277 }
278 mmu_inval_dma_area(va, len_total);
279 // XXX The mmu_map_dma_area does this for us below, see comments.
280 // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
281 /*
282 * XXX That's where sdev would be used. Currently we load
283 * all iommu tables with the same translations.
284 */
David S. Miller4b1c5df2008-08-27 18:40:38 -0700285 if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto err_noiommu;
287
David S. Miller7a715f42008-08-27 18:37:58 -0700288 res->name = op->node->name;
Martin Habets4cfbd7e2006-05-07 23:43:19 -0700289
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700290 return (void *)(unsigned long)res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292err_noiommu:
293 release_resource(res);
294err_nova:
295 free_pages(va, order);
296err_nomem:
297 kfree(res);
298err_nopages:
299 return NULL;
300}
301
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900302static void sbus_free_coherent(struct device *dev, size_t n, void *p,
303 dma_addr_t ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 struct resource *res;
306 struct page *pgv;
307
308 if ((res = _sparc_find_resource(&_sparc_dvma,
309 (unsigned long)p)) == NULL) {
310 printk("sbus_free_consistent: cannot free %p\n", p);
311 return;
312 }
313
314 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
315 printk("sbus_free_consistent: unaligned va %p\n", p);
316 return;
317 }
318
319 n = (n + PAGE_SIZE-1) & PAGE_MASK;
320 if ((res->end-res->start)+1 != n) {
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900321 printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 (long)((res->end-res->start)+1), n);
323 return;
324 }
325
326 release_resource(res);
327 kfree(res);
328
329 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
David S. Milleraba945e2008-08-27 02:20:35 -0700330 pgv = virt_to_page(p);
David S. Miller4b1c5df2008-08-27 18:40:38 -0700331 mmu_unmap_dma_area(dev, ba, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 __free_pages(pgv, get_order(n));
334}
335
336/*
337 * Map a chunk of memory so that devices can see it.
338 * CPU view of this memory may be inconsistent with
339 * a device view and explicit flushing is necessary.
340 */
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900341static dma_addr_t sbus_map_page(struct device *dev, struct page *page,
342 unsigned long offset, size_t len,
343 enum dma_data_direction dir,
344 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
FUJITA Tomonoric2c07db2009-08-10 11:53:15 +0900346 void *va = page_address(page) + offset;
347
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200348 /* XXX why are some lengths signed, others unsigned? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (len <= 0) {
350 return 0;
351 }
352 /* XXX So what is maxphys for us and how do drivers know it? */
353 if (len > 256*1024) { /* __get_free_pages() limit */
354 return 0;
355 }
David S. Miller260489f2008-08-26 23:00:58 -0700356 return mmu_get_scsi_one(dev, va, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900359static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n,
360 enum dma_data_direction dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
David S. Miller260489f2008-08-26 23:00:58 -0700362 mmu_release_scsi_one(dev, ba, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900365static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n,
366 enum dma_data_direction dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
David S. Miller260489f2008-08-26 23:00:58 -0700368 mmu_get_scsi_sgl(dev, sg, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /*
371 * XXX sparc64 can return a partial length here. sun4c should do this
372 * but it currently panics if it can't fulfill the request - Anton
373 */
374 return n;
375}
376
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900377static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n,
378 enum dma_data_direction dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
David S. Miller260489f2008-08-26 23:00:58 -0700380 mmu_release_scsi_sgl(dev, sg, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900383static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
384 int n, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900386 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900389static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
390 int n, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900392 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900395struct dma_map_ops sbus_dma_ops = {
396 .alloc_coherent = sbus_alloc_coherent,
397 .free_coherent = sbus_free_coherent,
398 .map_page = sbus_map_page,
399 .unmap_page = sbus_unmap_page,
400 .map_sg = sbus_map_sg,
401 .unmap_sg = sbus_unmap_sg,
402 .sync_sg_for_cpu = sbus_sync_sg_for_cpu,
403 .sync_sg_for_device = sbus_sync_sg_for_device,
404};
405
406struct dma_map_ops *dma_ops = &sbus_dma_ops;
407EXPORT_SYMBOL(dma_ops);
408
David S. Millerf8e4d322008-08-27 04:20:14 -0700409static int __init sparc_register_ioport(void)
David S. Miller576c3522006-06-23 15:55:45 -0700410{
David S. Miller576c3522006-06-23 15:55:45 -0700411 register_proc_sparc_ioport();
412
David S. Miller576c3522006-06-23 15:55:45 -0700413 return 0;
David S. Miller576c3522006-06-23 15:55:45 -0700414}
415
David S. Millerf8e4d322008-08-27 04:20:14 -0700416arch_initcall(sparc_register_ioport);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#endif /* CONFIG_SBUS */
419
420#ifdef CONFIG_PCI
421
422/* Allocate and map kernel buffer using consistent mode DMA for a device.
423 * hwdev should be valid struct pci_dev pointer for PCI devices.
424 */
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900425static void *pci32_alloc_coherent(struct device *dev, size_t len,
426 dma_addr_t *pba, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
429 unsigned long va;
430 struct resource *res;
431 int order;
432
433 if (len == 0) {
434 return NULL;
435 }
436 if (len > 256*1024) { /* __get_free_pages() limit */
437 return NULL;
438 }
439
440 order = get_order(len_total);
441 va = __get_free_pages(GFP_KERNEL, order);
442 if (va == 0) {
443 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
444 return NULL;
445 }
446
Yan Burmanc80892d2006-11-30 17:07:04 -0800447 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 free_pages(va, order);
449 printk("pci_alloc_consistent: no core\n");
450 return NULL;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (allocate_resource(&_sparc_dvma, res, len_total,
454 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
455 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
456 free_pages(va, order);
457 kfree(res);
458 return NULL;
459 }
460 mmu_inval_dma_area(va, len_total);
461#if 0
462/* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
463 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
464#endif
465 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
466
467 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
468 return (void *) res->start;
469}
470
471/* Free and unmap a consistent DMA buffer.
472 * cpu_addr is what was returned from pci_alloc_consistent,
473 * size must be the same as what as passed into pci_alloc_consistent,
474 * and likewise dma_addr must be the same as what *dma_addrp was set to.
475 *
Simon Arlottd1a78c32007-05-11 13:51:23 -0700476 * References to the memory and mappings associated with cpu_addr/dma_addr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * past this call are illegal.
478 */
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900479static void pci32_free_coherent(struct device *dev, size_t n, void *p,
480 dma_addr_t ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct resource *res;
483 unsigned long pgp;
484
485 if ((res = _sparc_find_resource(&_sparc_dvma,
486 (unsigned long)p)) == NULL) {
487 printk("pci_free_consistent: cannot free %p\n", p);
488 return;
489 }
490
491 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
492 printk("pci_free_consistent: unaligned va %p\n", p);
493 return;
494 }
495
496 n = (n + PAGE_SIZE-1) & PAGE_MASK;
497 if ((res->end-res->start)+1 != n) {
498 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
499 (long)((res->end-res->start)+1), (long)n);
500 return;
501 }
502
503 pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
504 mmu_inval_dma_area(pgp, n);
505 sparc_unmapiorange((unsigned long)p, n);
506
507 release_resource(res);
508 kfree(res);
509
510 free_pages(pgp, get_order(n));
511}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513/*
514 * Same as pci_map_single, but with pages.
515 */
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900516static dma_addr_t pci32_map_page(struct device *dev, struct page *page,
517 unsigned long offset, size_t size,
518 enum dma_data_direction dir,
519 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* IIep is write-through, not flushing. */
522 return page_to_phys(page) + offset;
523}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525/* Map a set of buffers described by scatterlist in streaming
526 * mode for DMA. This is the scather-gather version of the
527 * above pci_map_single interface. Here the scatter gather list
528 * elements are each tagged with the appropriate dma address
529 * and length. They are obtained via sg_dma_{address,length}(SG).
530 *
531 * NOTE: An implementation may be able to use a smaller number of
532 * DMA address/length pairs than there are SG table elements.
533 * (for example via virtual mapping capabilities)
534 * The routine returns the number of addr/length pairs actually
535 * used, at most nents.
536 *
537 * Device ownership issues as mentioned above for pci_map_single are
538 * the same here.
539 */
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900540static int pci32_map_sg(struct device *device, struct scatterlist *sgl,
541 int nents, enum dma_data_direction dir,
542 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200544 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 int n;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* IIep is write-through, not flushing. */
Jens Axboe0912a5d2007-05-14 15:44:38 +0200548 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200549 BUG_ON(page_address(sg_page(sg)) == NULL);
Robert Reifaa83a262008-12-11 20:24:58 -0800550 sg->dma_address = virt_to_phys(sg_virt(sg));
551 sg->dma_length = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 return nents;
554}
555
556/* Unmap a set of streaming mode DMA translations.
557 * Again, cpu read rules concerning calls here are the same as for
558 * pci_unmap_single() above.
559 */
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900560static void pci32_unmap_sg(struct device *dev, struct scatterlist *sgl,
561 int nents, enum dma_data_direction dir,
562 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200564 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int n;
566
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900567 if (dir != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200568 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200569 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200571 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 }
575}
576
577/* Make physical memory consistent for a single
578 * streaming mode DMA translation before or after a transfer.
579 *
580 * If you perform a pci_map_single() but wish to interrogate the
581 * buffer using the cpu, yet do not wish to teardown the PCI dma
582 * mapping, you must call this function before doing so. At the
583 * next point you give the PCI dma address back to the card, you
584 * must first perform a pci_dma_sync_for_device, and then the
585 * device again owns the buffer.
586 */
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900587static void pci32_sync_single_for_cpu(struct device *dev, dma_addr_t ba,
588 size_t size, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900590 if (dir != PCI_DMA_TODEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
592 (size + PAGE_SIZE-1) & PAGE_MASK);
593 }
594}
595
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900596static void pci32_sync_single_for_device(struct device *dev, dma_addr_t ba,
597 size_t size, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900599 if (dir != PCI_DMA_TODEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
601 (size + PAGE_SIZE-1) & PAGE_MASK);
602 }
603}
604
605/* Make physical memory consistent for a set of streaming
606 * mode DMA translations after a transfer.
607 *
608 * The same as pci_dma_sync_single_* but for a scatter-gather list,
609 * same rules and usage.
610 */
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900611static void pci32_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
612 int nents, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200614 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int n;
616
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900617 if (dir != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200618 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200619 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200621 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 }
625}
626
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900627static void pci32_sync_sg_for_device(struct device *device, struct scatterlist *sgl,
628 int nents, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Jens Axboe0912a5d2007-05-14 15:44:38 +0200630 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int n;
632
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900633 if (dir != PCI_DMA_TODEVICE) {
Jens Axboe0912a5d2007-05-14 15:44:38 +0200634 for_each_sg(sgl, sg, nents, n) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200635 BUG_ON(page_address(sg_page(sg)) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 mmu_inval_dma_area(
Jens Axboe58b053e2007-10-22 20:02:46 +0200637 (unsigned long) page_address(sg_page(sg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 }
641}
FUJITA Tomonoriee664a92009-08-10 11:53:16 +0900642
643struct dma_map_ops pci32_dma_ops = {
644 .alloc_coherent = pci32_alloc_coherent,
645 .free_coherent = pci32_free_coherent,
646 .map_page = pci32_map_page,
647 .map_sg = pci32_map_sg,
648 .unmap_sg = pci32_unmap_sg,
649 .sync_single_for_cpu = pci32_sync_single_for_cpu,
650 .sync_single_for_device = pci32_sync_single_for_device,
651 .sync_sg_for_cpu = pci32_sync_sg_for_cpu,
652 .sync_sg_for_device = pci32_sync_sg_for_device,
653};
654EXPORT_SYMBOL(pci32_dma_ops);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#endif /* CONFIG_PCI */
657
FUJITA Tomonori451d7402009-08-10 11:53:17 +0900658/*
659 * Return whether the given PCI device DMA address mask can be
660 * supported properly. For example, if your device can only drive the
661 * low 24-bits during PCI bus mastering, then you would pass
662 * 0x00ffffff as the mask to this function.
663 */
664int dma_supported(struct device *dev, u64 mask)
665{
666#ifdef CONFIG_PCI
667 if (dev->bus == &pci_bus_type)
668 return 1;
669#endif
670 return 0;
671}
672EXPORT_SYMBOL(dma_supported);
673
674int dma_set_mask(struct device *dev, u64 dma_mask)
675{
676#ifdef CONFIG_PCI
677 if (dev->bus == &pci_bus_type)
678 return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
679#endif
680 return -EOPNOTSUPP;
681}
682EXPORT_SYMBOL(dma_set_mask);
683
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#ifdef CONFIG_PROC_FS
686
Alexey Dobriyane7a088f2009-09-01 17:54:07 -0700687static int sparc_io_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Alexey Dobriyane7a088f2009-09-01 17:54:07 -0700689 struct resource *root = m->private, *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 const char *nm;
691
Alexey Dobriyane7a088f2009-09-01 17:54:07 -0700692 for (r = root->child; r != NULL; r = r->sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if ((nm = r->name) == 0) nm = "???";
Alexey Dobriyane7a088f2009-09-01 17:54:07 -0700694 seq_printf(m, "%016llx-%016llx: %s\n",
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700695 (unsigned long long)r->start,
696 (unsigned long long)r->end, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Alexey Dobriyane7a088f2009-09-01 17:54:07 -0700699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Alexey Dobriyane7a088f2009-09-01 17:54:07 -0700702static int sparc_io_proc_open(struct inode *inode, struct file *file)
703{
704 return single_open(file, sparc_io_proc_show, PDE(inode)->data);
705}
706
707static const struct file_operations sparc_io_proc_fops = {
708 .owner = THIS_MODULE,
709 .open = sparc_io_proc_open,
710 .read = seq_read,
711 .llseek = seq_lseek,
712 .release = single_release,
713};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714#endif /* CONFIG_PROC_FS */
715
716/*
717 * This is a version of find_resource and it belongs to kernel/resource.c.
718 * Until we have agreement with Linus and Martin, it lingers here.
719 *
720 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
721 * This probably warrants some sort of hashing.
722 */
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700723static struct resource *_sparc_find_resource(struct resource *root,
724 unsigned long hit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct resource *tmp;
727
728 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
729 if (tmp->start <= hit && tmp->end >= hit)
730 return tmp;
731 }
732 return NULL;
733}
734
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700735static void register_proc_sparc_ioport(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737#ifdef CONFIG_PROC_FS
Alexey Dobriyane7a088f2009-09-01 17:54:07 -0700738 proc_create_data("io_map", 0, NULL, &sparc_io_proc_fops, &sparc_iomap);
739 proc_create_data("dvma_map", 0, NULL, &sparc_io_proc_fops, &_sparc_dvma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740#endif
741}