[MIPS] Always pass -msoft-float.
[linux-2.6/openmoko-kernel/knife-kernel.git] / arch / sparc / kernel / ioport.c
blobd39c9f2062717c84c7ec4ae5b7b31d8afca33d54
1 /* $Id: ioport.c,v 1.45 2001/10/30 04:54:21 davem Exp $
2 * ioport.c: Simple io mapping allocator.
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
7 * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
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
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>
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>
46 #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
48 struct resource *_sparc_find_resource(struct resource *r, unsigned long);
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);
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
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
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.
70 #define XNMLN 15
71 #define XNRES 10 /* SS-10 uses 8 */
73 struct xresource {
74 struct resource xres; /* Must be first */
75 int xflag; /* 1 == used */
76 char xname[XNMLN+1];
79 static struct xresource xresv[XNRES];
81 static struct xresource *xres_alloc(void) {
82 struct xresource *xrp;
83 int n;
85 xrp = xresv;
86 for (n = 0; n < XNRES; n++) {
87 if (xrp->xflag == 0) {
88 xrp->xflag = 1;
89 return xrp;
91 xrp++;
93 return NULL;
96 static void xres_free(struct xresource *xrp) {
97 xrp->xflag = 0;
101 * These are typically used in PCI drivers
102 * which are trying to be cross-platform.
104 * Bus type is always zero on IIep.
106 void __iomem *ioremap(unsigned long offset, unsigned long size)
108 char name[14];
110 sprintf(name, "phys_%08x", (u32)offset);
111 return _sparc_alloc_io(0, offset, size, name);
115 * Comlimentary to ioremap().
117 void iounmap(volatile void __iomem *virtual)
119 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
120 struct resource *res;
122 if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
123 printk("free_io/iounmap: cannot free %lx\n", vaddr);
124 return;
126 _sparc_free_io(res);
128 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
129 xres_free((struct xresource *)res);
130 } else {
131 kfree(res);
137 void __iomem *sbus_ioremap(struct resource *phyres, unsigned long offset,
138 unsigned long size, char *name)
140 return _sparc_alloc_io(phyres->flags & 0xF,
141 phyres->start + offset, size, name);
146 void sbus_iounmap(volatile void __iomem *addr, unsigned long size)
148 iounmap(addr);
152 * Meat of mapping
154 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
155 unsigned long size, char *name)
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 */
164 if (name == NULL) name = "???";
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;
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);
182 strlcpy(tack, name, XNMLN+1);
183 res->name = tack;
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;
192 static void __iomem *
193 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
195 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
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();
206 pa &= PAGE_MASK;
207 sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
209 return (void __iomem *) (res->start + offset);
213 * Comlimentary to _sparc_ioremap().
215 static void _sparc_free_io(struct resource *res)
217 unsigned long plen;
219 plen = res->end - res->start + 1;
220 if ((plen & (PAGE_SIZE-1)) != 0) BUG();
221 sparc_unmapiorange(res->start, plen);
222 release_resource(res);
225 #ifdef CONFIG_SBUS
227 void sbus_set_sbus64(struct sbus_dev *sdev, int x) {
228 printk("sbus_set_sbus64: unsupported\n");
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.
236 * XXX Some clever people know that sdev is not used and supply NULL. Watch.
238 void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
240 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
241 unsigned long va;
242 struct resource *res;
243 int order;
245 /* XXX why are some lenghts signed, others unsigned? */
246 if (len <= 0) {
247 return NULL;
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;
254 order = get_order(len_total);
255 if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
256 goto err_nopages;
258 if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
259 goto err_nomem;
260 memset((char*)res, 0, sizeof(struct resource));
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;
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);
271 * XXX That's where sdev would be used. Currently we load
272 * all iommu tables with the same translations.
274 if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0)
275 goto err_noiommu;
277 return (void *)res->start;
279 err_noiommu:
280 release_resource(res);
281 err_nova:
282 free_pages(va, order);
283 err_nomem:
284 kfree(res);
285 err_nopages:
286 return NULL;
289 void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
291 struct resource *res;
292 struct page *pgv;
294 if ((res = _sparc_find_resource(&_sparc_dvma,
295 (unsigned long)p)) == NULL) {
296 printk("sbus_free_consistent: cannot free %p\n", p);
297 return;
300 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
301 printk("sbus_free_consistent: unaligned va %p\n", p);
302 return;
305 n = (n + PAGE_SIZE-1) & PAGE_MASK;
306 if ((res->end-res->start)+1 != n) {
307 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
308 (long)((res->end-res->start)+1), n);
309 return;
312 release_resource(res);
313 kfree(res);
315 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
316 pgv = mmu_translate_dvma(ba);
317 mmu_unmap_dma_area(ba, n);
319 __free_pages(pgv, get_order(n));
323 * Map a chunk of memory so that devices can see it.
324 * CPU view of this memory may be inconsistent with
325 * a device view and explicit flushing is necessary.
327 dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
329 /* XXX why are some lenghts signed, others unsigned? */
330 if (len <= 0) {
331 return 0;
333 /* XXX So what is maxphys for us and how do drivers know it? */
334 if (len > 256*1024) { /* __get_free_pages() limit */
335 return 0;
337 return mmu_get_scsi_one(va, len, sdev->bus);
340 void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
342 mmu_release_scsi_one(ba, n, sdev->bus);
345 int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
347 mmu_get_scsi_sgl(sg, n, sdev->bus);
350 * XXX sparc64 can return a partial length here. sun4c should do this
351 * but it currently panics if it can't fulfill the request - Anton
353 return n;
356 void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
358 mmu_release_scsi_sgl(sg, n, sdev->bus);
363 void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
365 #if 0
366 unsigned long va;
367 struct resource *res;
369 /* We do not need the resource, just print a message if invalid. */
370 res = _sparc_find_resource(&_sparc_dvma, ba);
371 if (res == NULL)
372 panic("sbus_dma_sync_single: 0x%x\n", ba);
374 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
376 * XXX This bogosity will be fixed with the iommu rewrite coming soon
377 * to a kernel near you. - Anton
379 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
380 #endif
383 void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
385 #if 0
386 unsigned long va;
387 struct resource *res;
389 /* We do not need the resource, just print a message if invalid. */
390 res = _sparc_find_resource(&_sparc_dvma, ba);
391 if (res == NULL)
392 panic("sbus_dma_sync_single: 0x%x\n", ba);
394 va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
396 * XXX This bogosity will be fixed with the iommu rewrite coming soon
397 * to a kernel near you. - Anton
399 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
400 #endif
403 void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
405 printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n");
408 void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
410 printk("sbus_dma_sync_sg_for_device: not implemented yet\n");
412 #endif /* CONFIG_SBUS */
414 #ifdef CONFIG_PCI
416 /* Allocate and map kernel buffer using consistent mode DMA for a device.
417 * hwdev should be valid struct pci_dev pointer for PCI devices.
419 void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
421 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
422 unsigned long va;
423 struct resource *res;
424 int order;
426 if (len == 0) {
427 return NULL;
429 if (len > 256*1024) { /* __get_free_pages() limit */
430 return NULL;
433 order = get_order(len_total);
434 va = __get_free_pages(GFP_KERNEL, order);
435 if (va == 0) {
436 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
437 return NULL;
440 if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
441 free_pages(va, order);
442 printk("pci_alloc_consistent: no core\n");
443 return NULL;
445 memset((char*)res, 0, sizeof(struct resource));
447 if (allocate_resource(&_sparc_dvma, res, len_total,
448 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
449 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
450 free_pages(va, order);
451 kfree(res);
452 return NULL;
454 mmu_inval_dma_area(va, len_total);
455 #if 0
456 /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
457 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
458 #endif
459 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
461 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
462 return (void *) res->start;
465 /* Free and unmap a consistent DMA buffer.
466 * cpu_addr is what was returned from pci_alloc_consistent,
467 * size must be the same as what as passed into pci_alloc_consistent,
468 * and likewise dma_addr must be the same as what *dma_addrp was set to.
470 * References to the memory and mappings assosciated with cpu_addr/dma_addr
471 * past this call are illegal.
473 void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
475 struct resource *res;
476 unsigned long pgp;
478 if ((res = _sparc_find_resource(&_sparc_dvma,
479 (unsigned long)p)) == NULL) {
480 printk("pci_free_consistent: cannot free %p\n", p);
481 return;
484 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
485 printk("pci_free_consistent: unaligned va %p\n", p);
486 return;
489 n = (n + PAGE_SIZE-1) & PAGE_MASK;
490 if ((res->end-res->start)+1 != n) {
491 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
492 (long)((res->end-res->start)+1), (long)n);
493 return;
496 pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
497 mmu_inval_dma_area(pgp, n);
498 sparc_unmapiorange((unsigned long)p, n);
500 release_resource(res);
501 kfree(res);
503 free_pages(pgp, get_order(n));
506 /* Map a single buffer of the indicated size for DMA in streaming mode.
507 * The 32-bit bus address to use is returned.
509 * Once the device is given the dma address, the device owns this memory
510 * until either pci_unmap_single or pci_dma_sync_single_* is performed.
512 dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
513 int direction)
515 if (direction == PCI_DMA_NONE)
516 BUG();
517 /* IIep is write-through, not flushing. */
518 return virt_to_phys(ptr);
521 /* Unmap a single streaming mode DMA translation. The dma_addr and size
522 * must match what was provided for in a previous pci_map_single call. All
523 * other usages are undefined.
525 * After this call, reads by the cpu to the buffer are guaranteed to see
526 * whatever the device wrote there.
528 void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
529 int direction)
531 if (direction == PCI_DMA_NONE)
532 BUG();
533 if (direction != PCI_DMA_TODEVICE) {
534 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
535 (size + PAGE_SIZE-1) & PAGE_MASK);
540 * Same as pci_map_single, but with pages.
542 dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
543 unsigned long offset, size_t size, int direction)
545 if (direction == PCI_DMA_NONE)
546 BUG();
547 /* IIep is write-through, not flushing. */
548 return page_to_phys(page) + offset;
551 void pci_unmap_page(struct pci_dev *hwdev,
552 dma_addr_t dma_address, size_t size, int direction)
554 if (direction == PCI_DMA_NONE)
555 BUG();
556 /* mmu_inval_dma_area XXX */
559 /* Map a set of buffers described by scatterlist in streaming
560 * mode for DMA. This is the scather-gather version of the
561 * above pci_map_single interface. Here the scatter gather list
562 * elements are each tagged with the appropriate dma address
563 * and length. They are obtained via sg_dma_{address,length}(SG).
565 * NOTE: An implementation may be able to use a smaller number of
566 * DMA address/length pairs than there are SG table elements.
567 * (for example via virtual mapping capabilities)
568 * The routine returns the number of addr/length pairs actually
569 * used, at most nents.
571 * Device ownership issues as mentioned above for pci_map_single are
572 * the same here.
574 int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
575 int direction)
577 int n;
579 if (direction == PCI_DMA_NONE)
580 BUG();
581 /* IIep is write-through, not flushing. */
582 for (n = 0; n < nents; n++) {
583 if (page_address(sg->page) == NULL) BUG();
584 sg->dvma_address = virt_to_phys(page_address(sg->page));
585 sg->dvma_length = sg->length;
586 sg++;
588 return nents;
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.
595 void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
596 int direction)
598 int n;
600 if (direction == PCI_DMA_NONE)
601 BUG();
602 if (direction != PCI_DMA_TODEVICE) {
603 for (n = 0; n < nents; n++) {
604 if (page_address(sg->page) == NULL) BUG();
605 mmu_inval_dma_area(
606 (unsigned long) page_address(sg->page),
607 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
608 sg++;
613 /* Make physical memory consistent for a single
614 * streaming mode DMA translation before or after a transfer.
616 * If you perform a pci_map_single() but wish to interrogate the
617 * buffer using the cpu, yet do not wish to teardown the PCI dma
618 * mapping, you must call this function before doing so. At the
619 * next point you give the PCI dma address back to the card, you
620 * must first perform a pci_dma_sync_for_device, and then the
621 * device again owns the buffer.
623 void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
625 if (direction == PCI_DMA_NONE)
626 BUG();
627 if (direction != PCI_DMA_TODEVICE) {
628 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
629 (size + PAGE_SIZE-1) & PAGE_MASK);
633 void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
635 if (direction == PCI_DMA_NONE)
636 BUG();
637 if (direction != PCI_DMA_TODEVICE) {
638 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
639 (size + PAGE_SIZE-1) & PAGE_MASK);
643 /* Make physical memory consistent for a set of streaming
644 * mode DMA translations after a transfer.
646 * The same as pci_dma_sync_single_* but for a scatter-gather list,
647 * same rules and usage.
649 void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
651 int n;
653 if (direction == PCI_DMA_NONE)
654 BUG();
655 if (direction != PCI_DMA_TODEVICE) {
656 for (n = 0; n < nents; n++) {
657 if (page_address(sg->page) == NULL) BUG();
658 mmu_inval_dma_area(
659 (unsigned long) page_address(sg->page),
660 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
661 sg++;
666 void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
668 int n;
670 if (direction == PCI_DMA_NONE)
671 BUG();
672 if (direction != PCI_DMA_TODEVICE) {
673 for (n = 0; n < nents; n++) {
674 if (page_address(sg->page) == NULL) BUG();
675 mmu_inval_dma_area(
676 (unsigned long) page_address(sg->page),
677 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
678 sg++;
682 #endif /* CONFIG_PCI */
684 #ifdef CONFIG_PROC_FS
686 static int
687 _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
688 void *data)
690 char *p = buf, *e = buf + length;
691 struct resource *r;
692 const char *nm;
694 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
695 if (p + 32 >= e) /* Better than nothing */
696 break;
697 if ((nm = r->name) == 0) nm = "???";
698 p += sprintf(p, "%08lx-%08lx: %s\n", r->start, r->end, nm);
701 return p-buf;
704 #endif /* CONFIG_PROC_FS */
707 * This is a version of find_resource and it belongs to kernel/resource.c.
708 * Until we have agreement with Linus and Martin, it lingers here.
710 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
711 * This probably warrants some sort of hashing.
713 struct resource *
714 _sparc_find_resource(struct resource *root, unsigned long hit)
716 struct resource *tmp;
718 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
719 if (tmp->start <= hit && tmp->end >= hit)
720 return tmp;
722 return NULL;
725 void register_proc_sparc_ioport(void)
727 #ifdef CONFIG_PROC_FS
728 create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
729 create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
730 #endif