1 /* $Id: ioport.c,v 1.42 2000/12/05 00:56:36 anton 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/malloc.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
= 0;
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);
225 * XXX Playing with implementation details here.
226 * On sparc64 Ebus has resources with precise boundaries.
227 * We share drivers with sparc64. Too clever drivers use
228 * start of a resource instead of a base adress.
230 * XXX-2 This may be not valid anymore, clean when
231 * interface to sbus_ioremap() is resolved.
233 res
->start
+= offset
;
234 res
->end
= res
->start
+ sz
- 1; /* not strictly necessary.. */
236 return (void *) res
->start
;
240 * Comlimentary to _sparc_ioremap().
242 static void _sparc_free_io(struct resource
*res
)
246 plen
= res
->end
- res
->start
+ 1;
247 plen
= (plen
+ PAGE_SIZE
-1) & PAGE_MASK
;
250 (*_sparc_unmapioaddr
)(res
->start
+ plen
);
253 release_resource(res
);
258 void sbus_set_sbus64(struct sbus_dev
*sdev
, int x
) {
259 printk("sbus_set_sbus64: unsupported\n");
263 * Allocate a chunk of memory suitable for DMA.
264 * Typically devices use them for control blocks.
265 * CPU may access them without any explicit flushing.
267 * XXX Some clever people know that sdev is not used and supply NULL. Watch.
269 void *sbus_alloc_consistent(struct sbus_dev
*sdev
, long len
, u32
*dma_addrp
)
271 unsigned long len_total
= (len
+ PAGE_SIZE
-1) & PAGE_MASK
;
273 struct resource
*res
;
276 /* XXX why are some lenghts signed, others unsigned? */
280 /* XXX So what is maxphys for us and how do drivers know it? */
281 if (len
> 256*1024) { /* __get_free_pages() limit */
285 order
= get_order(len_total
);
286 va
= __get_free_pages(GFP_KERNEL
, order
);
289 * printk here may be flooding... Consider removal XXX.
291 printk("sbus_alloc_consistent: no %ld pages\n", len_total
>>PAGE_SHIFT
);
295 if ((res
= kmalloc(sizeof(struct resource
), GFP_KERNEL
)) == NULL
) {
296 free_pages(va
, order
);
297 printk("sbus_alloc_consistent: no core\n");
300 memset((char*)res
, 0, sizeof(struct resource
));
302 if (allocate_resource(&_sparc_dvma
, res
, len_total
,
303 _sparc_dvma
.start
, _sparc_dvma
.end
, PAGE_SIZE
, NULL
, NULL
) != 0) {
304 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total
);
305 free_pages(va
, order
);
310 mmu_map_dma_area(va
, res
->start
, len_total
);
312 *dma_addrp
= res
->start
;
313 return (void *)res
->start
;
316 void sbus_free_consistent(struct sbus_dev
*sdev
, long n
, void *p
, u32 ba
)
318 struct resource
*res
;
321 if ((res
= _sparc_find_resource(&_sparc_dvma
,
322 (unsigned long)p
)) == NULL
) {
323 printk("sbus_free_consistent: cannot free %p\n", p
);
327 if (((unsigned long)p
& (PAGE_SIZE
-1)) != 0) {
328 printk("sbus_free_consistent: unaligned va %p\n", p
);
332 n
= (n
+ PAGE_SIZE
-1) & PAGE_MASK
;
333 if ((res
->end
-res
->start
)+1 != n
) {
334 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
335 (long)((res
->end
-res
->start
)+1), n
);
339 release_resource(res
);
342 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
343 pgp
= (unsigned long) phys_to_virt(mmu_translate_dvma(ba
));
344 mmu_unmap_dma_area(ba
, n
);
346 free_pages(pgp
, get_order(n
));
350 * Map a chunk of memory so that devices can see it.
351 * CPU view of this memory may be inconsistent with
352 * a device view and explicit flushing is necessary.
354 u32
sbus_map_single(struct sbus_dev
*sdev
, void *va
, long len
, int direction
)
356 #if 0 /* This is the version that abuses consistent space */
357 unsigned long len_total
= (len
+ PAGE_SIZE
-1) & PAGE_MASK
;
358 struct resource
*res
;
360 /* XXX why are some lenghts signed, others unsigned? */
364 /* XXX So what is maxphys for us and how do drivers know it? */
365 if (len
> 256*1024) { /* __get_free_pages() limit */
369 if ((res
= kmalloc(sizeof(struct resource
), GFP_KERNEL
)) == NULL
) {
370 printk("sbus_map_single: no core\n");
373 memset((char*)res
, 0, sizeof(struct resource
));
374 res
->name
= va
; /* XXX */
376 if (allocate_resource(&_sparc_dvma
, res
, len_total
,
377 _sparc_dvma
.start
, _sparc_dvma
.end
, PAGE_SIZE
) != 0) {
378 printk("sbus_map_single: cannot occupy 0x%lx", len
);
383 mmu_map_dma_area(va
, res
->start
, len_total
);
384 mmu_flush_dma_area((unsigned long)va
, len_total
); /* in all contexts? */
388 #if 1 /* "trampoline" version */
389 /* XXX why are some lenghts signed, others unsigned? */
393 /* XXX So what is maxphys for us and how do drivers know it? */
394 if (len
> 256*1024) { /* __get_free_pages() limit */
397 return mmu_get_scsi_one(va
, len
, sdev
->bus
);
401 void sbus_unmap_single(struct sbus_dev
*sdev
, u32 ba
, long n
, int direction
)
403 #if 0 /* This is the version that abuses consistent space */
404 struct resource
*res
;
407 if ((res
= _sparc_find_resource(&_sparc_dvma
, ba
)) == NULL
) {
408 printk("sbus_unmap_single: cannot find %08x\n", (unsigned)ba
);
412 n
= (n
+ PAGE_SIZE
-1) & PAGE_MASK
;
413 if ((res
->end
-res
->start
)+1 != n
) {
414 printk("sbus_unmap_single: region 0x%lx asked 0x%lx\n",
415 (long)((res
->end
-res
->start
)+1), n
);
419 va
= (unsigned long) res
->name
; /* XXX Ouch */
420 mmu_inval_dma_area(va
, n
); /* in all contexts, mm's?... */
421 mmu_unmap_dma_area(ba
, n
); /* iounit cache flush is here */
422 release_resource(res
);
425 #if 1 /* "trampoline" version */
426 mmu_release_scsi_one(ba
, n
, sdev
->bus
);
430 int sbus_map_sg(struct sbus_dev
*sdev
, struct scatterlist
*sg
, int n
, int direction
)
432 mmu_get_scsi_sgl(sg
, n
, sdev
->bus
);
435 * XXX sparc64 can return a partial length here. sun4c should do this
436 * but it currently panics if it can't fulfill the request - Anton
441 void sbus_unmap_sg(struct sbus_dev
*sdev
, struct scatterlist
*sg
, int n
, int direction
)
443 mmu_release_scsi_sgl(sg
, n
, sdev
->bus
);
448 void sbus_dma_sync_single(struct sbus_dev
*sdev
, u32 ba
, long size
, int direction
)
452 struct resource
*res
;
454 /* We do not need the resource, just print a message if invalid. */
455 res
= _sparc_find_resource(&_sparc_dvma
, ba
);
457 panic("sbus_dma_sync_single: 0x%x\n", ba
);
459 va
= (unsigned long) phys_to_virt(mmu_translate_dvma(ba
));
461 * XXX This bogosity will be fixed with the iommu rewrite coming soon
462 * to a kernel near you. - Anton
464 /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
468 void sbus_dma_sync_sg(struct sbus_dev
*sdev
, struct scatterlist
*sg
, int n
, int direction
)
470 printk("sbus_dma_sync_sg: not implemented yet\n");
472 #endif /* CONFIG_SBUS */
476 /* Allocate and map kernel buffer using consistent mode DMA for a device.
477 * hwdev should be valid struct pci_dev pointer for PCI devices.
479 void *pci_alloc_consistent(struct pci_dev
*pdev
, size_t len
, dma_addr_t
*pba
)
481 unsigned long len_total
= (len
+ PAGE_SIZE
-1) & PAGE_MASK
;
483 struct resource
*res
;
489 if (len
> 256*1024) { /* __get_free_pages() limit */
493 order
= get_order(len_total
);
494 va
= __get_free_pages(GFP_KERNEL
, order
);
496 printk("pci_alloc_consistent: no %ld pages\n", len_total
>>PAGE_SHIFT
);
500 if ((res
= kmalloc(sizeof(struct resource
), GFP_KERNEL
)) == NULL
) {
501 free_pages(va
, order
);
502 printk("pci_alloc_consistent: no core\n");
505 memset((char*)res
, 0, sizeof(struct resource
));
507 if (allocate_resource(&_sparc_dvma
, res
, len_total
,
508 _sparc_dvma
.start
, _sparc_dvma
.end
, PAGE_SIZE
, NULL
, NULL
) != 0) {
509 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total
);
510 free_pages(va
, order
);
515 mmu_inval_dma_area(va
, len_total
);
518 /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %x\n",
519 (long)va
, (long)res
->start
, (long)virt_to_phys(va
), len_total
);
522 unsigned long xva
, xpa
;
524 xpa
= virt_to_phys(va
);
525 while (len_total
!= 0) {
526 len_total
-= PAGE_SIZE
;
527 (*_sparc_mapioaddr
)(xpa
, xva
, 0, 0);
533 *pba
= virt_to_bus(va
);
534 return (void *) res
->start
;
537 /* Free and unmap a consistent DMA buffer.
538 * cpu_addr is what was returned from pci_alloc_consistent,
539 * size must be the same as what as passed into pci_alloc_consistent,
540 * and likewise dma_addr must be the same as what *dma_addrp was set to.
542 * References to the memory and mappings assosciated with cpu_addr/dma_addr
543 * past this call are illegal.
545 void pci_free_consistent(struct pci_dev
*pdev
, size_t n
, void *p
, dma_addr_t ba
)
547 struct resource
*res
;
550 if ((res
= _sparc_find_resource(&_sparc_dvma
,
551 (unsigned long)p
)) == NULL
) {
552 printk("pci_free_consistent: cannot free %p\n", p
);
556 if (((unsigned long)p
& (PAGE_SIZE
-1)) != 0) {
557 printk("pci_free_consistent: unaligned va %p\n", p
);
561 n
= (n
+ PAGE_SIZE
-1) & PAGE_MASK
;
562 if ((res
->end
-res
->start
)+1 != n
) {
563 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
564 (long)((res
->end
-res
->start
)+1), (long)n
);
568 pgp
= (unsigned long) bus_to_virt(ba
);
569 mmu_inval_dma_area(pgp
, n
);
572 for (x
= 0; x
< n
; x
+= PAGE_SIZE
) {
573 (*_sparc_unmapioaddr
)((unsigned long)p
+ n
);
577 release_resource(res
);
580 free_pages(pgp
, get_order(n
));
583 /* Map a single buffer of the indicated size for DMA in streaming mode.
584 * The 32-bit bus address to use is returned.
586 * Once the device is given the dma address, the device owns this memory
587 * until either pci_unmap_single or pci_dma_sync_single is performed.
589 dma_addr_t
pci_map_single(struct pci_dev
*hwdev
, void *ptr
, size_t size
,
592 if (direction
== PCI_DMA_NONE
)
594 /* IIep is write-through, not flushing. */
595 return virt_to_bus(ptr
);
598 /* Unmap a single streaming mode DMA translation. The dma_addr and size
599 * must match what was provided for in a previous pci_map_single call. All
600 * other usages are undefined.
602 * After this call, reads by the cpu to the buffer are guarenteed to see
603 * whatever the device wrote there.
605 void pci_unmap_single(struct pci_dev
*hwdev
, dma_addr_t ba
, size_t size
,
608 if (direction
== PCI_DMA_NONE
)
610 if (direction
!= PCI_DMA_TODEVICE
) {
611 mmu_inval_dma_area((unsigned long)bus_to_virt(ba
),
612 (size
+ PAGE_SIZE
-1) & PAGE_MASK
);
616 /* Map a set of buffers described by scatterlist in streaming
617 * mode for DMA. This is the scather-gather version of the
618 * above pci_map_single interface. Here the scatter gather list
619 * elements are each tagged with the appropriate dma address
620 * and length. They are obtained via sg_dma_{address,length}(SG).
622 * NOTE: An implementation may be able to use a smaller number of
623 * DMA address/length pairs than there are SG table elements.
624 * (for example via virtual mapping capabilities)
625 * The routine returns the number of addr/length pairs actually
626 * used, at most nents.
628 * Device ownership issues as mentioned above for pci_map_single are
631 int pci_map_sg(struct pci_dev
*hwdev
, struct scatterlist
*sg
, int nents
,
636 if (direction
== PCI_DMA_NONE
)
638 /* IIep is write-through, not flushing. */
639 for (n
= 0; n
< nents
; n
++) {
640 sg
->dvma_address
= virt_to_bus(sg
->address
);
641 sg
->dvma_length
= sg
->length
;
647 /* Unmap a set of streaming mode DMA translations.
648 * Again, cpu read rules concerning calls here are the same as for
649 * pci_unmap_single() above.
651 void pci_unmap_sg(struct pci_dev
*hwdev
, struct scatterlist
*sg
, int nents
,
656 if (direction
== PCI_DMA_NONE
)
658 if (direction
!= PCI_DMA_TODEVICE
) {
659 for (n
= 0; n
< nents
; n
++) {
660 mmu_inval_dma_area((unsigned long)sg
->address
,
661 (sg
->length
+ PAGE_SIZE
-1) & PAGE_MASK
);
667 /* Make physical memory consistent for a single
668 * streaming mode DMA translation before or after a transfer.
670 * If you perform a pci_map_single() but wish to interrogate the
671 * buffer using the cpu, yet do not wish to teardown the PCI dma
672 * mapping, you must call this function before doing so. At the
673 * next point you give the PCI dma address back to the card, the
674 * device again owns the buffer.
676 void pci_dma_sync_single(struct pci_dev
*hwdev
, dma_addr_t ba
, size_t size
, int direction
)
678 if (direction
== PCI_DMA_NONE
)
680 if (direction
!= PCI_DMA_TODEVICE
) {
681 mmu_inval_dma_area((unsigned long)bus_to_virt(ba
),
682 (size
+ PAGE_SIZE
-1) & PAGE_MASK
);
686 /* Make physical memory consistent for a set of streaming
687 * mode DMA translations after a transfer.
689 * The same as pci_dma_sync_single but for a scatter-gather list,
690 * same rules and usage.
692 void pci_dma_sync_sg(struct pci_dev
*hwdev
, struct scatterlist
*sg
, int nents
, int direction
)
696 if (direction
== PCI_DMA_NONE
)
698 if (direction
!= PCI_DMA_TODEVICE
) {
699 for (n
= 0; n
< nents
; n
++) {
700 mmu_inval_dma_area((unsigned long)sg
->address
,
701 (sg
->length
+ PAGE_SIZE
-1) & PAGE_MASK
);
708 #ifdef CONFIG_PROC_FS
711 _sparc_io_get_info(char *buf
, char **start
, off_t fpos
, int length
, int *eof
,
714 char *p
= buf
, *e
= buf
+ length
;
718 for (r
= ((struct resource
*)data
)->child
; r
!= NULL
; r
= r
->sibling
) {
719 if (p
+ 32 >= e
) /* Better than nothing */
721 if ((nm
= r
->name
) == 0) nm
= "???";
722 p
+= sprintf(p
, "%08lx-%08lx: %s\n", r
->start
, r
->end
, nm
);
728 #endif CONFIG_PROC_FS
731 * This is a version of find_resource and it belongs to kernel/resource.c.
732 * Until we have agreement with Linus and Martin, it lingers here.
734 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
735 * This probably warrants some sort of hashing.
738 _sparc_find_resource(struct resource
*root
, unsigned long hit
)
740 struct resource
*tmp
;
742 for (tmp
= root
->child
; tmp
!= 0; tmp
= tmp
->sibling
) {
743 if (tmp
->start
<= hit
&& tmp
->end
>= hit
)
750 * Necessary boot time initializations.
753 void ioport_init(void)
755 extern void sun4c_mapioaddr(unsigned long, unsigned long, int, int);
756 extern void srmmu_mapioaddr(unsigned long, unsigned long, int, int);
757 extern void sun4c_unmapioaddr(unsigned long);
758 extern void srmmu_unmapioaddr(unsigned long);
760 switch(sparc_cpu_model
) {
764 _sparc_mapioaddr
= sun4c_mapioaddr
;
765 _sparc_unmapioaddr
= sun4c_unmapioaddr
;
769 _sparc_mapioaddr
= srmmu_mapioaddr
;
770 _sparc_unmapioaddr
= srmmu_unmapioaddr
;
773 printk("ioport_init: cpu type %d is unknown.\n",
780 void register_proc_sparc_ioport(void)
782 #ifdef CONFIG_PROC_FS
783 create_proc_read_entry("io_map",0,0,_sparc_io_get_info
,&sparc_iomap
);
784 create_proc_read_entry("dvma_map",0,0,_sparc_io_get_info
,&_sparc_dvma
);