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.
10 * <rth> zait: as long as pci_alloc_consistent produces something addressable,
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()))
17 * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
19 * <zaitcev> Now, driver calls pci_free_consistent(with result of
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.
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>
35 #include <linux/slab.h>
36 #include <linux/pci.h> /* struct pci_dev */
37 #include <linux/proc_fs.h>
40 #include <asm/vaddrs.h>
41 #include <asm/oplib.h>
43 #include <asm/pgalloc.h>
44 #include <asm/pgtable.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 *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
51 static void *_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 "sparc_dvma", DVMA_VADDR, DVMA_END - 1
59 /* This points to the start of I/O mappings, cluable from outside. */
60 /*ext*/ struct resource sparc_iomap = {
61 "sparc_iomap", IOBASE_VADDR, IOBASE_END - 1
65 * BTFIXUP would do as well but it seems overkill for the case.
67 static void (*_sparc_mapioaddr)(unsigned long pa, unsigned long va,
69 static void (*_sparc_unmapioaddr)(unsigned long va);
72 * Our mini-allocator...
73 * Boy this is gross! We need it because we must map I/O for
74 * timers and interrupt controller before the kmalloc is available.
78 #define XNRES 10 /* SS-10 uses 8 */
81 struct resource xres; /* Must be first */
82 int xflag; /* 1 == used */
86 static struct xresource xresv[XNRES];
88 static struct xresource *xres_alloc(void) {
89 struct xresource *xrp;
93 for (n = 0; n < XNRES; n++) {
94 if (xrp->xflag == 0) {
103 static void xres_free(struct xresource *xrp) {
108 * These are typically used in PCI drivers
109 * which are trying to be cross-platform.
111 * Bus type is always zero on IIep.
113 void *ioremap(unsigned long offset, unsigned long size)
117 sprintf(name, "phys_%08x", (u32)offset);
118 return _sparc_alloc_io(0, offset, size, name);
122 * Comlimentary to ioremap().
124 void iounmap(void *virtual)
126 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
127 struct resource *res;
129 if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
130 printk("free_io/iounmap: cannot free %lx\n", vaddr);
135 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
136 xres_free((struct xresource *)res);
144 unsigned long sbus_ioremap(struct resource *phyres, unsigned long offset,
145 unsigned long size, char *name)
147 return (unsigned long) _sparc_alloc_io(phyres->flags & 0xF,
148 phyres->start + offset, size, name);
153 void sbus_iounmap(unsigned long addr, unsigned long size)
155 iounmap((void *)addr);
161 static void *_sparc_alloc_io(unsigned int busno, unsigned long phys,
162 unsigned long size, char *name)
164 static int printed_full;
165 struct xresource *xres;
166 struct resource *res;
169 void *va; /* P3 diag */
171 if (name == NULL) name = "???";
173 if ((xres = xres_alloc()) != 0) {
178 printk("ioremap: done with statics, switching to malloc\n");
182 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
183 if (tack == NULL) return NULL;
184 memset(tack, 0, sizeof(struct resource));
185 res = (struct resource *) tack;
186 tack += sizeof (struct resource);
189 strncpy(tack, name, XNMLN);
193 va = _sparc_ioremap(res, busno, phys, size);
194 /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
201 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
203 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
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: "???");
218 for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
219 (*_sparc_mapioaddr)(pa, va, bus, 0);
224 return (void *) (res->start + offset);
228 * Comlimentary to _sparc_ioremap().
230 static void _sparc_free_io(struct resource *res)
234 plen = res->end - res->start + 1;
235 if ((plen & (PAGE_SIZE-1)) != 0) BUG();
238 (*_sparc_unmapioaddr)(res->start + plen);
241 release_resource(res);
246 void sbus_set_sbus64(struct sbus_dev *sdev, int x) {
247 printk("sbus_set_sbus64: unsupported\n");
251 * Allocate a chunk of memory suitable for DMA.
252 * Typically devices use them for control blocks.
253 * CPU may access them without any explicit flushing.
255 * XXX Some clever people know that sdev is not used and supply NULL. Watch.
257 void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
259 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
261 struct resource *res;
264 /* XXX why are some lenghts signed, others unsigned? */
268 /* XXX So what is maxphys for us and how do drivers know it? */
269 if (len > 256*1024) { /* __get_free_pages() limit */
273 order = get_order(len_total);
274 va = __get_free_pages(GFP_KERNEL, order);
277 * printk here may be flooding... Consider removal XXX.
279 printk("sbus_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
283 if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
284 free_pages(va, order);
285 printk("sbus_alloc_consistent: no core\n");
288 memset((char*)res, 0, sizeof(struct resource));
290 if (allocate_resource(&_sparc_dvma, res, len_total,
291 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
292 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
293 free_pages(va, order);
298 mmu_map_dma_area(va, res->start, len_total);
300 *dma_addrp = res->start;
301 return (void *)res->start;
304 void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
306 struct resource *res;
309 if ((res = _sparc_find_resource(&_sparc_dvma,
310 (unsigned long)p)) == NULL) {
311 printk("sbus_free_consistent: cannot free %p\n", p);
315 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
316 printk("sbus_free_consistent: unaligned va %p\n", p);
320 n = (n + PAGE_SIZE-1) & PAGE_MASK;
321 if ((res->end-res->start)+1 != n) {
322 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
323 (long)((res->end-res->start)+1), n);
327 release_resource(res);
330 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
331 pgp = (unsigned long) phys_to_virt(mmu_translate_dvma(ba));
332 mmu_unmap_dma_area(ba, n);
334 free_pages(pgp, get_order(n));
338 * Map a chunk of memory so that devices can see it.
339 * CPU view of this memory may be inconsistent with
340 * a device view and explicit flushing is necessary.
342 dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
344 #if 0 /* This is the version that abuses consistent space */
345 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
346 struct resource *res;
348 /* XXX why are some lenghts signed, others unsigned? */
352 /* XXX So what is maxphys for us and how do drivers know it? */
353 if (len > 256*1024) { /* __get_free_pages() limit */
357 if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
358 printk("sbus_map_single: no core\n");
361 memset((char*)res, 0, sizeof(struct resource));
362 res->name = va; /* XXX */
364 if (allocate_resource(&_sparc_dvma, res, len_total,
365 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE) != 0) {
366 printk("sbus_map_single: cannot occupy 0x%lx", len);
371 mmu_map_dma_area(va, res->start, len_total);
372 mmu_flush_dma_area((unsigned long)va, len_total); /* in all contexts? */
376 #if 1 /* "trampoline" version */
377 /* XXX why are some lenghts signed, others unsigned? */
381 /* XXX So what is maxphys for us and how do drivers know it? */
382 if (len > 256*1024) { /* __get_free_pages() limit */
385 return mmu_get_scsi_one(va, len, sdev->bus);
389 void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
391 #if 0 /* This is the version that abuses consistent space */
392 struct resource *res;
395 if ((res = _sparc_find_resource(&_sparc_dvma, ba)) == NULL) {
396 printk("sbus_unmap_single: cannot find %08x\n", (unsigned)ba);
400 n = (n + PAGE_SIZE-1) & PAGE_MASK;
401 if ((res->end-res->start)+1 != n) {
402 printk("sbus_unmap_single: region 0x%lx asked 0x%lx\n",
403 (long)((res->end-res->start)+1), n);
407 va = (unsigned long) res->name; /* XXX Ouch */
408 mmu_inval_dma_area(va, n); /* in all contexts, mm's?... */
409 mmu_unmap_dma_area(ba, n); /* iounit cache flush is here */
410 release_resource(res);
413 #if 1 /* "trampoline" version */
414 mmu_release_scsi_one(ba, n, sdev->bus);
418 int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
420 mmu_get_scsi_sgl(sg, n, sdev->bus);
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
429 void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
431 mmu_release_scsi_sgl(sg, n, sdev->bus);
436 void sbus_dma_sync_single(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
440 struct resource *res;
442 /* We do not need the resource, just print a message if invalid. */
443 res = _sparc_find_resource(&_sparc_dvma, ba);
445 panic("sbus_dma_sync_single: 0x%x\n", ba);
447 va = (unsigned long) phys_to_virt(mmu_translate_dvma(ba));
449 * XXX This bogosity will be fixed with the iommu rewrite coming soon
450 * to a kernel near you. - Anton
452 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
456 void sbus_dma_sync_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
458 printk("sbus_dma_sync_sg: not implemented yet\n");
460 #endif /* CONFIG_SBUS */
464 /* Allocate and map kernel buffer using consistent mode DMA for a device.
465 * hwdev should be valid struct pci_dev pointer for PCI devices.
467 void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
469 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
471 struct resource *res;
477 if (len > 256*1024) { /* __get_free_pages() limit */
481 order = get_order(len_total);
482 va = __get_free_pages(GFP_KERNEL, order);
484 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
488 if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
489 free_pages(va, order);
490 printk("pci_alloc_consistent: no core\n");
493 memset((char*)res, 0, sizeof(struct resource));
495 if (allocate_resource(&_sparc_dvma, res, len_total,
496 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
497 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
498 free_pages(va, order);
503 mmu_inval_dma_area(va, len_total);
506 /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
507 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
510 unsigned long xva, xpa;
512 xpa = virt_to_phys(va);
513 while (len_total != 0) {
514 len_total -= PAGE_SIZE;
515 (*_sparc_mapioaddr)(xpa, xva, 0, 0);
521 *pba = virt_to_bus(va);
522 return (void *) res->start;
525 /* Free and unmap a consistent DMA buffer.
526 * cpu_addr is what was returned from pci_alloc_consistent,
527 * size must be the same as what as passed into pci_alloc_consistent,
528 * and likewise dma_addr must be the same as what *dma_addrp was set to.
530 * References to the memory and mappings assosciated with cpu_addr/dma_addr
531 * past this call are illegal.
533 void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
535 struct resource *res;
538 if ((res = _sparc_find_resource(&_sparc_dvma,
539 (unsigned long)p)) == NULL) {
540 printk("pci_free_consistent: cannot free %p\n", p);
544 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
545 printk("pci_free_consistent: unaligned va %p\n", p);
549 n = (n + PAGE_SIZE-1) & PAGE_MASK;
550 if ((res->end-res->start)+1 != n) {
551 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
552 (long)((res->end-res->start)+1), (long)n);
556 pgp = (unsigned long) bus_to_virt(ba);
557 mmu_inval_dma_area(pgp, n);
560 for (x = 0; x < n; x += PAGE_SIZE) {
561 (*_sparc_unmapioaddr)((unsigned long)p + n);
565 release_resource(res);
568 free_pages(pgp, get_order(n));
571 /* Map a single buffer of the indicated size for DMA in streaming mode.
572 * The 32-bit bus address to use is returned.
574 * Once the device is given the dma address, the device owns this memory
575 * until either pci_unmap_single or pci_dma_sync_single is performed.
577 dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
580 if (direction == PCI_DMA_NONE)
582 /* IIep is write-through, not flushing. */
583 return virt_to_bus(ptr);
586 /* Unmap a single streaming mode DMA translation. The dma_addr and size
587 * must match what was provided for in a previous pci_map_single call. All
588 * other usages are undefined.
590 * After this call, reads by the cpu to the buffer are guarenteed to see
591 * whatever the device wrote there.
593 void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
596 if (direction == PCI_DMA_NONE)
598 if (direction != PCI_DMA_TODEVICE) {
599 mmu_inval_dma_area((unsigned long)bus_to_virt(ba),
600 (size + PAGE_SIZE-1) & PAGE_MASK);
604 /* Map a set of buffers described by scatterlist in streaming
605 * mode for DMA. This is the scather-gather version of the
606 * above pci_map_single interface. Here the scatter gather list
607 * elements are each tagged with the appropriate dma address
608 * and length. They are obtained via sg_dma_{address,length}(SG).
610 * NOTE: An implementation may be able to use a smaller number of
611 * DMA address/length pairs than there are SG table elements.
612 * (for example via virtual mapping capabilities)
613 * The routine returns the number of addr/length pairs actually
614 * used, at most nents.
616 * Device ownership issues as mentioned above for pci_map_single are
619 int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
624 if (direction == PCI_DMA_NONE)
626 /* IIep is write-through, not flushing. */
627 for (n = 0; n < nents; n++) {
628 sg->dvma_address = virt_to_bus(sg->address);
629 sg->dvma_length = sg->length;
635 /* Unmap a set of streaming mode DMA translations.
636 * Again, cpu read rules concerning calls here are the same as for
637 * pci_unmap_single() above.
639 void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
644 if (direction == PCI_DMA_NONE)
646 if (direction != PCI_DMA_TODEVICE) {
647 for (n = 0; n < nents; n++) {
648 mmu_inval_dma_area((unsigned long)sg->address,
649 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
655 /* Make physical memory consistent for a single
656 * streaming mode DMA translation before or after a transfer.
658 * If you perform a pci_map_single() but wish to interrogate the
659 * buffer using the cpu, yet do not wish to teardown the PCI dma
660 * mapping, you must call this function before doing so. At the
661 * next point you give the PCI dma address back to the card, the
662 * device again owns the buffer.
664 void pci_dma_sync_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
666 if (direction == PCI_DMA_NONE)
668 if (direction != PCI_DMA_TODEVICE) {
669 mmu_inval_dma_area((unsigned long)bus_to_virt(ba),
670 (size + PAGE_SIZE-1) & PAGE_MASK);
674 /* Make physical memory consistent for a set of streaming
675 * mode DMA translations after a transfer.
677 * The same as pci_dma_sync_single but for a scatter-gather list,
678 * same rules and usage.
680 void pci_dma_sync_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
684 if (direction == PCI_DMA_NONE)
686 if (direction != PCI_DMA_TODEVICE) {
687 for (n = 0; n < nents; n++) {
688 mmu_inval_dma_area((unsigned long)sg->address,
689 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
694 #endif /* CONFIG_PCI */
696 #ifdef CONFIG_PROC_FS
699 _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
702 char *p = buf, *e = buf + length;
706 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
707 if (p + 32 >= e) /* Better than nothing */
709 if ((nm = r->name) == 0) nm = "???";
710 p += sprintf(p, "%08lx-%08lx: %s\n", r->start, r->end, nm);
716 #endif /* CONFIG_PROC_FS */
719 * This is a version of find_resource and it belongs to kernel/resource.c.
720 * Until we have agreement with Linus and Martin, it lingers here.
722 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
723 * This probably warrants some sort of hashing.
726 _sparc_find_resource(struct resource *root, unsigned long hit)
728 struct resource *tmp;
730 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
731 if (tmp->start <= hit && tmp->end >= hit)
738 * Necessary boot time initializations.
741 void ioport_init(void)
743 extern void sun4c_mapioaddr(unsigned long, unsigned long, int, int);
744 extern void srmmu_mapioaddr(unsigned long, unsigned long, int, int);
745 extern void sun4c_unmapioaddr(unsigned long);
746 extern void srmmu_unmapioaddr(unsigned long);
748 switch(sparc_cpu_model) {
752 _sparc_mapioaddr = sun4c_mapioaddr;
753 _sparc_unmapioaddr = sun4c_unmapioaddr;
757 _sparc_mapioaddr = srmmu_mapioaddr;
758 _sparc_unmapioaddr = srmmu_unmapioaddr;
761 printk("ioport_init: cpu type %d is unknown.\n",
768 void register_proc_sparc_ioport(void)
770 #ifdef CONFIG_PROC_FS
771 create_proc_read_entry("io_map",0,0,_sparc_io_get_info,&sparc_iomap);
772 create_proc_read_entry("dvma_map",0,0,_sparc_io_get_info,&_sparc_dvma);