Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* $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 | ||
28 | #include <linux/config.h> | |
29 | #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> | |
42 | #include <asm/page.h> | |
43 | #include <asm/pgalloc.h> | |
44 | #include <asm/dma.h> | |
45 | ||
46 | #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */ | |
47 | ||
48 | struct resource *_sparc_find_resource(struct resource *r, unsigned long); | |
49 | ||
50 | static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz); | |
51 | static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys, | |
52 | unsigned long size, char *name); | |
53 | static void _sparc_free_io(struct resource *res); | |
54 | ||
55 | /* This points to the next to use virtual memory for DVMA mappings */ | |
56 | static struct resource _sparc_dvma = { | |
57 | .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1 | |
58 | }; | |
59 | /* This points to the start of I/O mappings, cluable from outside. */ | |
60 | /*ext*/ struct resource sparc_iomap = { | |
61 | .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1 | |
62 | }; | |
63 | ||
64 | /* | |
65 | * Our mini-allocator... | |
66 | * Boy this is gross! We need it because we must map I/O for | |
67 | * timers and interrupt controller before the kmalloc is available. | |
68 | */ | |
69 | ||
70 | #define XNMLN 15 | |
71 | #define XNRES 10 /* SS-10 uses 8 */ | |
72 | ||
73 | struct xresource { | |
74 | struct resource xres; /* Must be first */ | |
75 | int xflag; /* 1 == used */ | |
76 | char xname[XNMLN+1]; | |
77 | }; | |
78 | ||
79 | static struct xresource xresv[XNRES]; | |
80 | ||
81 | static struct xresource *xres_alloc(void) { | |
82 | struct xresource *xrp; | |
83 | int n; | |
84 | ||
85 | xrp = xresv; | |
86 | for (n = 0; n < XNRES; n++) { | |
87 | if (xrp->xflag == 0) { | |
88 | xrp->xflag = 1; | |
89 | return xrp; | |
90 | } | |
91 | xrp++; | |
92 | } | |
93 | return NULL; | |
94 | } | |
95 | ||
96 | static void xres_free(struct xresource *xrp) { | |
97 | xrp->xflag = 0; | |
98 | } | |
99 | ||
100 | /* | |
101 | * These are typically used in PCI drivers | |
102 | * which are trying to be cross-platform. | |
103 | * | |
104 | * Bus type is always zero on IIep. | |
105 | */ | |
106 | void __iomem *ioremap(unsigned long offset, unsigned long size) | |
107 | { | |
108 | char name[14]; | |
109 | ||
110 | sprintf(name, "phys_%08x", (u32)offset); | |
111 | return _sparc_alloc_io(0, offset, size, name); | |
112 | } | |
113 | ||
114 | /* | |
115 | * Comlimentary to ioremap(). | |
116 | */ | |
117 | void iounmap(volatile void __iomem *virtual) | |
118 | { | |
119 | unsigned long vaddr = (unsigned long) virtual & PAGE_MASK; | |
120 | struct resource *res; | |
121 | ||
122 | if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) { | |
123 | printk("free_io/iounmap: cannot free %lx\n", vaddr); | |
124 | return; | |
125 | } | |
126 | _sparc_free_io(res); | |
127 | ||
128 | if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) { | |
129 | xres_free((struct xresource *)res); | |
130 | } else { | |
131 | kfree(res); | |
132 | } | |
133 | } | |
134 | ||
135 | /* | |
136 | */ | |
137 | void __iomem *sbus_ioremap(struct resource *phyres, unsigned long offset, | |
138 | unsigned long size, char *name) | |
139 | { | |
140 | return _sparc_alloc_io(phyres->flags & 0xF, | |
141 | phyres->start + offset, size, name); | |
142 | } | |
143 | ||
144 | /* | |
145 | */ | |
146 | void sbus_iounmap(volatile void __iomem *addr, unsigned long size) | |
147 | { | |
148 | iounmap(addr); | |
149 | } | |
150 | ||
151 | /* | |
152 | * Meat of mapping | |
153 | */ | |
154 | static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys, | |
155 | unsigned long size, char *name) | |
156 | { | |
157 | static int printed_full; | |
158 | struct xresource *xres; | |
159 | struct resource *res; | |
160 | char *tack; | |
161 | int tlen; | |
162 | void __iomem *va; /* P3 diag */ | |
163 | ||
164 | if (name == NULL) name = "???"; | |
165 | ||
166 | if ((xres = xres_alloc()) != 0) { | |
167 | tack = xres->xname; | |
168 | res = &xres->xres; | |
169 | } else { | |
170 | if (!printed_full) { | |
171 | printk("ioremap: done with statics, switching to malloc\n"); | |
172 | printed_full = 1; | |
173 | } | |
174 | tlen = strlen(name); | |
175 | tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL); | |
176 | if (tack == NULL) return NULL; | |
177 | memset(tack, 0, sizeof(struct resource)); | |
178 | res = (struct resource *) tack; | |
179 | tack += sizeof (struct resource); | |
180 | } | |
181 | ||
182 | strlcpy(tack, name, XNMLN+1); | |
183 | res->name = tack; | |
184 | ||
185 | va = _sparc_ioremap(res, busno, phys, size); | |
186 | /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */ | |
187 | return va; | |
188 | } | |
189 | ||
190 | /* | |
191 | */ | |
192 | static void __iomem * | |
193 | _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz) | |
194 | { | |
195 | unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK); | |
196 | ||
197 | if (allocate_resource(&sparc_iomap, res, | |
198 | (offset + sz + PAGE_SIZE-1) & PAGE_MASK, | |
199 | sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) { | |
200 | /* Usually we cannot see printks in this case. */ | |
201 | prom_printf("alloc_io_res(%s): cannot occupy\n", | |
202 | (res->name != NULL)? res->name: "???"); | |
203 | prom_halt(); | |
204 | } | |
205 | ||
206 | pa &= PAGE_MASK; | |
207 | sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1); | |
208 | ||
209 | return (void __iomem *) (res->start + offset); | |
210 | } | |
211 | ||
212 | /* | |
213 | * Comlimentary to _sparc_ioremap(). | |
214 | */ | |
215 | static void _sparc_free_io(struct resource *res) | |
216 | { | |
217 | unsigned long plen; | |
218 | ||
219 | plen = res->end - res->start + 1; | |
30d4d1ff | 220 | BUG_ON((plen & (PAGE_SIZE-1)) != 0); |
1da177e4 LT |
221 | sparc_unmapiorange(res->start, plen); |
222 | release_resource(res); | |
223 | } | |
224 | ||
225 | #ifdef CONFIG_SBUS | |
226 | ||
227 | void sbus_set_sbus64(struct sbus_dev *sdev, int x) { | |
228 | printk("sbus_set_sbus64: unsupported\n"); | |
229 | } | |
230 | ||
231 | /* | |
232 | * Allocate a chunk of memory suitable for DMA. | |
233 | * Typically devices use them for control blocks. | |
234 | * CPU may access them without any explicit flushing. | |
235 | * | |
236 | * XXX Some clever people know that sdev is not used and supply NULL. Watch. | |
237 | */ | |
238 | void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp) | |
239 | { | |
240 | unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK; | |
241 | unsigned long va; | |
242 | struct resource *res; | |
243 | int order; | |
244 | ||
245 | /* XXX why are some lenghts signed, others unsigned? */ | |
246 | if (len <= 0) { | |
247 | return NULL; | |
248 | } | |
249 | /* XXX So what is maxphys for us and how do drivers know it? */ | |
250 | if (len > 256*1024) { /* __get_free_pages() limit */ | |
251 | return NULL; | |
252 | } | |
253 | ||
254 | order = get_order(len_total); | |
f3d48f03 | 255 | if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0) |
1da177e4 LT |
256 | goto err_nopages; |
257 | ||
258 | if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) | |
259 | goto err_nomem; | |
260 | memset((char*)res, 0, sizeof(struct resource)); | |
261 | ||
262 | if (allocate_resource(&_sparc_dvma, res, len_total, | |
263 | _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { | |
264 | printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total); | |
265 | goto err_nova; | |
266 | } | |
267 | mmu_inval_dma_area(va, len_total); | |
268 | // XXX The mmu_map_dma_area does this for us below, see comments. | |
269 | // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total); | |
270 | /* | |
271 | * XXX That's where sdev would be used. Currently we load | |
272 | * all iommu tables with the same translations. | |
273 | */ | |
274 | if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0) | |
275 | goto err_noiommu; | |
276 | ||
4cfbd7eb MH |
277 | /* Set the resource name, if known. */ |
278 | if (sdev) { | |
279 | res->name = sdev->prom_name; | |
280 | } | |
281 | ||
1da177e4 LT |
282 | return (void *)res->start; |
283 | ||
284 | err_noiommu: | |
285 | release_resource(res); | |
286 | err_nova: | |
287 | free_pages(va, order); | |
288 | err_nomem: | |
289 | kfree(res); | |
290 | err_nopages: | |
291 | return NULL; | |
292 | } | |
293 | ||
294 | void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba) | |
295 | { | |
296 | struct resource *res; | |
297 | struct page *pgv; | |
298 | ||
299 | if ((res = _sparc_find_resource(&_sparc_dvma, | |
300 | (unsigned long)p)) == NULL) { | |
301 | printk("sbus_free_consistent: cannot free %p\n", p); | |
302 | return; | |
303 | } | |
304 | ||
305 | if (((unsigned long)p & (PAGE_SIZE-1)) != 0) { | |
306 | printk("sbus_free_consistent: unaligned va %p\n", p); | |
307 | return; | |
308 | } | |
309 | ||
310 | n = (n + PAGE_SIZE-1) & PAGE_MASK; | |
311 | if ((res->end-res->start)+1 != n) { | |
312 | printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n", | |
313 | (long)((res->end-res->start)+1), n); | |
314 | return; | |
315 | } | |
316 | ||
317 | release_resource(res); | |
318 | kfree(res); | |
319 | ||
320 | /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */ | |
321 | pgv = mmu_translate_dvma(ba); | |
322 | mmu_unmap_dma_area(ba, n); | |
323 | ||
324 | __free_pages(pgv, get_order(n)); | |
325 | } | |
326 | ||
327 | /* | |
328 | * Map a chunk of memory so that devices can see it. | |
329 | * CPU view of this memory may be inconsistent with | |
330 | * a device view and explicit flushing is necessary. | |
331 | */ | |
332 | dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction) | |
333 | { | |
334 | /* XXX why are some lenghts signed, others unsigned? */ | |
335 | if (len <= 0) { | |
336 | return 0; | |
337 | } | |
338 | /* XXX So what is maxphys for us and how do drivers know it? */ | |
339 | if (len > 256*1024) { /* __get_free_pages() limit */ | |
340 | return 0; | |
341 | } | |
342 | return mmu_get_scsi_one(va, len, sdev->bus); | |
343 | } | |
344 | ||
345 | void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction) | |
346 | { | |
347 | mmu_release_scsi_one(ba, n, sdev->bus); | |
348 | } | |
349 | ||
350 | int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction) | |
351 | { | |
352 | mmu_get_scsi_sgl(sg, n, sdev->bus); | |
353 | ||
354 | /* | |
355 | * XXX sparc64 can return a partial length here. sun4c should do this | |
356 | * but it currently panics if it can't fulfill the request - Anton | |
357 | */ | |
358 | return n; | |
359 | } | |
360 | ||
361 | void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction) | |
362 | { | |
363 | mmu_release_scsi_sgl(sg, n, sdev->bus); | |
364 | } | |
365 | ||
366 | /* | |
367 | */ | |
368 | void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction) | |
369 | { | |
370 | #if 0 | |
371 | unsigned long va; | |
372 | struct resource *res; | |
373 | ||
374 | /* We do not need the resource, just print a message if invalid. */ | |
375 | res = _sparc_find_resource(&_sparc_dvma, ba); | |
376 | if (res == NULL) | |
377 | panic("sbus_dma_sync_single: 0x%x\n", ba); | |
378 | ||
379 | va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */ | |
380 | /* | |
381 | * XXX This bogosity will be fixed with the iommu rewrite coming soon | |
382 | * to a kernel near you. - Anton | |
383 | */ | |
384 | /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */ | |
385 | #endif | |
386 | } | |
387 | ||
388 | void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction) | |
389 | { | |
390 | #if 0 | |
391 | unsigned long va; | |
392 | struct resource *res; | |
393 | ||
394 | /* We do not need the resource, just print a message if invalid. */ | |
395 | res = _sparc_find_resource(&_sparc_dvma, ba); | |
396 | if (res == NULL) | |
397 | panic("sbus_dma_sync_single: 0x%x\n", ba); | |
398 | ||
399 | va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */ | |
400 | /* | |
401 | * XXX This bogosity will be fixed with the iommu rewrite coming soon | |
402 | * to a kernel near you. - Anton | |
403 | */ | |
404 | /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */ | |
405 | #endif | |
406 | } | |
407 | ||
408 | void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction) | |
409 | { | |
410 | printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n"); | |
411 | } | |
412 | ||
413 | void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction) | |
414 | { | |
415 | printk("sbus_dma_sync_sg_for_device: not implemented yet\n"); | |
416 | } | |
417 | #endif /* CONFIG_SBUS */ | |
418 | ||
419 | #ifdef CONFIG_PCI | |
420 | ||
421 | /* Allocate and map kernel buffer using consistent mode DMA for a device. | |
422 | * hwdev should be valid struct pci_dev pointer for PCI devices. | |
423 | */ | |
424 | void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba) | |
425 | { | |
426 | unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK; | |
427 | unsigned long va; | |
428 | struct resource *res; | |
429 | int order; | |
430 | ||
431 | if (len == 0) { | |
432 | return NULL; | |
433 | } | |
434 | if (len > 256*1024) { /* __get_free_pages() limit */ | |
435 | return NULL; | |
436 | } | |
437 | ||
438 | order = get_order(len_total); | |
439 | va = __get_free_pages(GFP_KERNEL, order); | |
440 | if (va == 0) { | |
441 | printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT); | |
442 | return NULL; | |
443 | } | |
444 | ||
445 | if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) { | |
446 | free_pages(va, order); | |
447 | printk("pci_alloc_consistent: no core\n"); | |
448 | return NULL; | |
449 | } | |
450 | memset((char*)res, 0, sizeof(struct resource)); | |
451 | ||
452 | if (allocate_resource(&_sparc_dvma, res, len_total, | |
453 | _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { | |
454 | printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total); | |
455 | free_pages(va, order); | |
456 | kfree(res); | |
457 | return NULL; | |
458 | } | |
459 | mmu_inval_dma_area(va, len_total); | |
460 | #if 0 | |
461 | /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n", | |
462 | (long)va, (long)res->start, (long)virt_to_phys(va), len_total); | |
463 | #endif | |
464 | sparc_mapiorange(0, virt_to_phys(va), res->start, len_total); | |
465 | ||
466 | *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */ | |
467 | return (void *) res->start; | |
468 | } | |
469 | ||
470 | /* Free and unmap a consistent DMA buffer. | |
471 | * cpu_addr is what was returned from pci_alloc_consistent, | |
472 | * size must be the same as what as passed into pci_alloc_consistent, | |
473 | * and likewise dma_addr must be the same as what *dma_addrp was set to. | |
474 | * | |
475 | * References to the memory and mappings assosciated with cpu_addr/dma_addr | |
476 | * past this call are illegal. | |
477 | */ | |
478 | void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba) | |
479 | { | |
480 | struct resource *res; | |
481 | unsigned long pgp; | |
482 | ||
483 | if ((res = _sparc_find_resource(&_sparc_dvma, | |
484 | (unsigned long)p)) == NULL) { | |
485 | printk("pci_free_consistent: cannot free %p\n", p); | |
486 | return; | |
487 | } | |
488 | ||
489 | if (((unsigned long)p & (PAGE_SIZE-1)) != 0) { | |
490 | printk("pci_free_consistent: unaligned va %p\n", p); | |
491 | return; | |
492 | } | |
493 | ||
494 | n = (n + PAGE_SIZE-1) & PAGE_MASK; | |
495 | if ((res->end-res->start)+1 != n) { | |
496 | printk("pci_free_consistent: region 0x%lx asked 0x%lx\n", | |
497 | (long)((res->end-res->start)+1), (long)n); | |
498 | return; | |
499 | } | |
500 | ||
501 | pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */ | |
502 | mmu_inval_dma_area(pgp, n); | |
503 | sparc_unmapiorange((unsigned long)p, n); | |
504 | ||
505 | release_resource(res); | |
506 | kfree(res); | |
507 | ||
508 | free_pages(pgp, get_order(n)); | |
509 | } | |
510 | ||
511 | /* Map a single buffer of the indicated size for DMA in streaming mode. | |
512 | * The 32-bit bus address to use is returned. | |
513 | * | |
514 | * Once the device is given the dma address, the device owns this memory | |
515 | * until either pci_unmap_single or pci_dma_sync_single_* is performed. | |
516 | */ | |
517 | dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size, | |
518 | int direction) | |
519 | { | |
30d4d1ff | 520 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
521 | /* IIep is write-through, not flushing. */ |
522 | return virt_to_phys(ptr); | |
523 | } | |
524 | ||
525 | /* Unmap a single streaming mode DMA translation. The dma_addr and size | |
526 | * must match what was provided for in a previous pci_map_single call. All | |
527 | * other usages are undefined. | |
528 | * | |
529 | * After this call, reads by the cpu to the buffer are guaranteed to see | |
530 | * whatever the device wrote there. | |
531 | */ | |
532 | void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size, | |
533 | int direction) | |
534 | { | |
30d4d1ff | 535 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
536 | if (direction != PCI_DMA_TODEVICE) { |
537 | mmu_inval_dma_area((unsigned long)phys_to_virt(ba), | |
538 | (size + PAGE_SIZE-1) & PAGE_MASK); | |
539 | } | |
540 | } | |
541 | ||
542 | /* | |
543 | * Same as pci_map_single, but with pages. | |
544 | */ | |
545 | dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page, | |
546 | unsigned long offset, size_t size, int direction) | |
547 | { | |
30d4d1ff | 548 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
549 | /* IIep is write-through, not flushing. */ |
550 | return page_to_phys(page) + offset; | |
551 | } | |
552 | ||
553 | void pci_unmap_page(struct pci_dev *hwdev, | |
554 | dma_addr_t dma_address, size_t size, int direction) | |
555 | { | |
30d4d1ff | 556 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
557 | /* mmu_inval_dma_area XXX */ |
558 | } | |
559 | ||
560 | /* Map a set of buffers described by scatterlist in streaming | |
561 | * mode for DMA. This is the scather-gather version of the | |
562 | * above pci_map_single interface. Here the scatter gather list | |
563 | * elements are each tagged with the appropriate dma address | |
564 | * and length. They are obtained via sg_dma_{address,length}(SG). | |
565 | * | |
566 | * NOTE: An implementation may be able to use a smaller number of | |
567 | * DMA address/length pairs than there are SG table elements. | |
568 | * (for example via virtual mapping capabilities) | |
569 | * The routine returns the number of addr/length pairs actually | |
570 | * used, at most nents. | |
571 | * | |
572 | * Device ownership issues as mentioned above for pci_map_single are | |
573 | * the same here. | |
574 | */ | |
575 | int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, | |
576 | int direction) | |
577 | { | |
578 | int n; | |
579 | ||
30d4d1ff | 580 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
581 | /* IIep is write-through, not flushing. */ |
582 | for (n = 0; n < nents; n++) { | |
30d4d1ff | 583 | BUG_ON(page_address(sg->page) == NULL); |
1da177e4 LT |
584 | sg->dvma_address = virt_to_phys(page_address(sg->page)); |
585 | sg->dvma_length = sg->length; | |
586 | sg++; | |
587 | } | |
588 | return nents; | |
589 | } | |
590 | ||
591 | /* Unmap a set of streaming mode DMA translations. | |
592 | * Again, cpu read rules concerning calls here are the same as for | |
593 | * pci_unmap_single() above. | |
594 | */ | |
595 | void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, | |
596 | int direction) | |
597 | { | |
598 | int n; | |
599 | ||
30d4d1ff | 600 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
601 | if (direction != PCI_DMA_TODEVICE) { |
602 | for (n = 0; n < nents; n++) { | |
30d4d1ff | 603 | BUG_ON(page_address(sg->page) == NULL); |
1da177e4 LT |
604 | mmu_inval_dma_area( |
605 | (unsigned long) page_address(sg->page), | |
606 | (sg->length + PAGE_SIZE-1) & PAGE_MASK); | |
607 | sg++; | |
608 | } | |
609 | } | |
610 | } | |
611 | ||
612 | /* Make physical memory consistent for a single | |
613 | * streaming mode DMA translation before or after a transfer. | |
614 | * | |
615 | * If you perform a pci_map_single() but wish to interrogate the | |
616 | * buffer using the cpu, yet do not wish to teardown the PCI dma | |
617 | * mapping, you must call this function before doing so. At the | |
618 | * next point you give the PCI dma address back to the card, you | |
619 | * must first perform a pci_dma_sync_for_device, and then the | |
620 | * device again owns the buffer. | |
621 | */ | |
622 | void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction) | |
623 | { | |
30d4d1ff | 624 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
625 | if (direction != PCI_DMA_TODEVICE) { |
626 | mmu_inval_dma_area((unsigned long)phys_to_virt(ba), | |
627 | (size + PAGE_SIZE-1) & PAGE_MASK); | |
628 | } | |
629 | } | |
630 | ||
631 | void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction) | |
632 | { | |
30d4d1ff | 633 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
634 | if (direction != PCI_DMA_TODEVICE) { |
635 | mmu_inval_dma_area((unsigned long)phys_to_virt(ba), | |
636 | (size + PAGE_SIZE-1) & PAGE_MASK); | |
637 | } | |
638 | } | |
639 | ||
640 | /* Make physical memory consistent for a set of streaming | |
641 | * mode DMA translations after a transfer. | |
642 | * | |
643 | * The same as pci_dma_sync_single_* but for a scatter-gather list, | |
644 | * same rules and usage. | |
645 | */ | |
646 | void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction) | |
647 | { | |
648 | int n; | |
649 | ||
30d4d1ff | 650 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
651 | if (direction != PCI_DMA_TODEVICE) { |
652 | for (n = 0; n < nents; n++) { | |
30d4d1ff | 653 | BUG_ON(page_address(sg->page) == NULL); |
1da177e4 LT |
654 | mmu_inval_dma_area( |
655 | (unsigned long) page_address(sg->page), | |
656 | (sg->length + PAGE_SIZE-1) & PAGE_MASK); | |
657 | sg++; | |
658 | } | |
659 | } | |
660 | } | |
661 | ||
662 | void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction) | |
663 | { | |
664 | int n; | |
665 | ||
30d4d1ff | 666 | BUG_ON(direction == PCI_DMA_NONE); |
1da177e4 LT |
667 | if (direction != PCI_DMA_TODEVICE) { |
668 | for (n = 0; n < nents; n++) { | |
30d4d1ff | 669 | BUG_ON(page_address(sg->page) == NULL); |
1da177e4 LT |
670 | mmu_inval_dma_area( |
671 | (unsigned long) page_address(sg->page), | |
672 | (sg->length + PAGE_SIZE-1) & PAGE_MASK); | |
673 | sg++; | |
674 | } | |
675 | } | |
676 | } | |
677 | #endif /* CONFIG_PCI */ | |
678 | ||
679 | #ifdef CONFIG_PROC_FS | |
680 | ||
681 | static int | |
682 | _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof, | |
683 | void *data) | |
684 | { | |
685 | char *p = buf, *e = buf + length; | |
686 | struct resource *r; | |
687 | const char *nm; | |
688 | ||
689 | for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) { | |
690 | if (p + 32 >= e) /* Better than nothing */ | |
691 | break; | |
692 | if ((nm = r->name) == 0) nm = "???"; | |
693 | p += sprintf(p, "%08lx-%08lx: %s\n", r->start, r->end, nm); | |
694 | } | |
695 | ||
696 | return p-buf; | |
697 | } | |
698 | ||
699 | #endif /* CONFIG_PROC_FS */ | |
700 | ||
701 | /* | |
702 | * This is a version of find_resource and it belongs to kernel/resource.c. | |
703 | * Until we have agreement with Linus and Martin, it lingers here. | |
704 | * | |
705 | * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case. | |
706 | * This probably warrants some sort of hashing. | |
707 | */ | |
708 | struct resource * | |
709 | _sparc_find_resource(struct resource *root, unsigned long hit) | |
710 | { | |
711 | struct resource *tmp; | |
712 | ||
713 | for (tmp = root->child; tmp != 0; tmp = tmp->sibling) { | |
714 | if (tmp->start <= hit && tmp->end >= hit) | |
715 | return tmp; | |
716 | } | |
717 | return NULL; | |
718 | } | |
719 | ||
720 | void register_proc_sparc_ioport(void) | |
721 | { | |
722 | #ifdef CONFIG_PROC_FS | |
723 | create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap); | |
724 | create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma); | |
725 | #endif | |
726 | } |