1 /* $NetBSD: bus_private.h,v 1.12 2010/11/12 13:40:11 uebayasi Exp $ */ 2 /* NetBSD: bus.h,v 1.8 2005/03/09 19:04:46 matt Exp */ 3 4 /*- 5 * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1996 Charles M. Hannum. All rights reserved. 36 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by Christopher G. Demetriou 49 * for the NetBSD Project. 50 * 4. The name of the author may not be used to endorse or promote products 51 * derived from this software without specific prior written permission 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 54 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 55 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 56 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 57 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 58 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 62 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 63 */ 64 65 #if !defined(_X86_BUS_PRIVATE_H_) 66 #define _X86_BUS_PRIVATE_H_ 67 68 int _bus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t, 69 bus_size_t, int, bus_dmamap_t *); 70 void _bus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t); 71 int _bus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, 72 bus_size_t, struct proc *, int); 73 int _bus_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t, 74 struct mbuf *, int); 75 int _bus_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t, 76 struct uio *, int); 77 int _bus_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t, 78 bus_dma_segment_t *, int, bus_size_t, int); 79 void _bus_dmamap_unload(bus_dma_tag_t, bus_dmamap_t); 80 void _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t, 81 bus_size_t, int); 82 83 int _bus_dmamem_alloc(bus_dma_tag_t tag, bus_size_t size, 84 bus_size_t alignment, bus_size_t boundary, 85 bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags); 86 void _bus_dmamem_free(bus_dma_tag_t tag, bus_dma_segment_t *segs, 87 int nsegs); 88 int _bus_dmamem_map(bus_dma_tag_t tag, bus_dma_segment_t *segs, 89 int nsegs, size_t size, void **kvap, int flags); 90 void _bus_dmamem_unmap(bus_dma_tag_t tag, void *kva, size_t size); 91 paddr_t _bus_dmamem_mmap(bus_dma_tag_t tag, bus_dma_segment_t *segs, 92 int nsegs, off_t off, int prot, int flags); 93 94 int _bus_dmatag_subregion(bus_dma_tag_t tag, bus_addr_t min_addr, 95 bus_addr_t max_addr, bus_dma_tag_t *newtag, int flags); 96 void _bus_dmatag_destroy(bus_dma_tag_t tag); 97 98 #ifndef _BUS_DMAMEM_ALLOC_RANGE 99 int _bus_dmamem_alloc_range(bus_dma_tag_t tag, bus_size_t size, 100 bus_size_t alignment, bus_size_t boundary, 101 bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags, 102 bus_addr_t low, bus_addr_t high); 103 #endif 104 105 /* 106 * Cookie used for bounce buffers. A pointer to one of these it stashed in 107 * the DMA map. 108 */ 109 struct x86_bus_dma_cookie { 110 int id_flags; /* flags; see below */ 111 112 /* 113 * Information about the original buffer used during 114 * DMA map syncs. Note that origibuflen is only used 115 * for ID_BUFTYPE_LINEAR. 116 */ 117 void *id_origbuf; /* pointer to orig buffer if 118 bouncing */ 119 bus_size_t id_origbuflen; /* ...and size */ 120 int id_buftype; /* type of buffer */ 121 122 void *id_bouncebuf; /* pointer to the bounce buffer */ 123 bus_size_t id_bouncebuflen; /* ...and size */ 124 int id_nbouncesegs; /* number of valid bounce segs */ 125 bus_dma_segment_t id_bouncesegs[0]; /* array of bounce buffer 126 physical memory segments */ 127 }; 128 129 /* id_flags */ 130 #define X86_DMA_MIGHT_NEED_BOUNCE 0x01 /* may need bounce buffers */ 131 #define X86_DMA_HAS_BOUNCE 0x02 /* has bounce buffers */ 132 #define X86_DMA_IS_BOUNCING 0x04 /* is bouncing current xfer */ 133 134 /* id_buftype */ 135 #define X86_DMA_BUFTYPE_INVALID 0 136 #define X86_DMA_BUFTYPE_LINEAR 1 137 #define X86_DMA_BUFTYPE_MBUF 2 138 #define X86_DMA_BUFTYPE_UIO 3 139 #define X86_DMA_BUFTYPE_RAW 4 140 141 /* 142 * default address translation macros, which are appropriate where 143 * paddr_t == bus_addr_t. 144 */ 145 146 #if !defined(_BUS_PHYS_TO_BUS) 147 #define _BUS_PHYS_TO_BUS(pa) ((bus_addr_t)(pa)) 148 #endif /* !defined(_BUS_PHYS_TO_BUS) */ 149 150 #if !defined(_BUS_BUS_TO_PHYS) 151 #define _BUS_BUS_TO_PHYS(ba) ((paddr_t)(ba)) 152 #endif /* !defined(_BUS_BUS_TO_PHYS) */ 153 154 #if !defined(_BUS_VM_PAGE_TO_BUS) 155 #define _BUS_VM_PAGE_TO_BUS(pg) _BUS_PHYS_TO_BUS(VM_PAGE_TO_PHYS(pg)) 156 #endif /* !defined(_BUS_VM_PAGE_TO_BUS) */ 157 158 #if !defined(_BUS_BUS_TO_VM_PAGE) 159 #define _BUS_BUS_TO_VM_PAGE(ba) PHYS_TO_VM_PAGE(ba) 160 #endif /* !defined(_BUS_BUS_TO_VM_PAGE) */ 161 162 #if !defined(_BUS_PMAP_ENTER) 163 #define _BUS_PMAP_ENTER(pmap, va, ba, prot, flags) \ 164 pmap_enter(pmap, va, ba, prot, flags) 165 #endif /* _BUS_PMAP_ENTER */ 166 167 #if !defined(_BUS_VIRT_TO_BUS) 168 #include <uvm/uvm.h> 169 170 static __inline bus_addr_t _bus_virt_to_bus(struct pmap *, vaddr_t); 171 #define _BUS_VIRT_TO_BUS(pm, va) _bus_virt_to_bus((pm), (va)) 172 173 static __inline bus_addr_t 174 _bus_virt_to_bus(struct pmap *pm, vaddr_t va) 175 { 176 paddr_t pa; 177 178 if (!pmap_extract(pm, va, &pa)) { 179 panic("_bus_virt_to_bus"); 180 } 181 182 return _BUS_PHYS_TO_BUS(pa); 183 } 184 #endif /* !defined(_BUS_VIRT_TO_BUS) */ 185 186 /* 187 * by default, the end address of RAM visible on bus is the same as the 188 * largest physical address. 189 */ 190 #ifndef _BUS_AVAIL_END 191 #define _BUS_AVAIL_END (avail_end) 192 #endif 193 194 struct x86_bus_dma_tag { 195 /* 196 * The `bounce threshold' is checked while we are loading 197 * the DMA map. If the physical address of the segment 198 * exceeds the threshold, an error will be returned. The 199 * caller can then take whatever action is necessary to 200 * bounce the transfer. If this value is 0, it will be 201 * ignored. 202 */ 203 int _tag_needs_free; 204 bus_addr_t _bounce_thresh; 205 bus_addr_t _bounce_alloc_lo; 206 bus_addr_t _bounce_alloc_hi; 207 int (*_may_bounce)(bus_dma_tag_t, bus_dmamap_t, int, int *); 208 209 /* 210 * DMA mapping methods. 211 */ 212 int (*_dmamap_create)(bus_dma_tag_t, bus_size_t, int, 213 bus_size_t, bus_size_t, int, bus_dmamap_t *); 214 void (*_dmamap_destroy)(bus_dma_tag_t, bus_dmamap_t); 215 int (*_dmamap_load)(bus_dma_tag_t, bus_dmamap_t, void *, 216 bus_size_t, struct proc *, int); 217 int (*_dmamap_load_mbuf)(bus_dma_tag_t, bus_dmamap_t, 218 struct mbuf *, int); 219 int (*_dmamap_load_uio)(bus_dma_tag_t, bus_dmamap_t, 220 struct uio *, int); 221 int (*_dmamap_load_raw)(bus_dma_tag_t, bus_dmamap_t, 222 bus_dma_segment_t *, int, bus_size_t, int); 223 void (*_dmamap_unload)(bus_dma_tag_t, bus_dmamap_t); 224 void (*_dmamap_sync)(bus_dma_tag_t, bus_dmamap_t, 225 bus_addr_t, bus_size_t, int); 226 227 /* 228 * DMA memory utility functions. 229 */ 230 int (*_dmamem_alloc)(bus_dma_tag_t, bus_size_t, bus_size_t, 231 bus_size_t, bus_dma_segment_t *, int, int *, int); 232 void (*_dmamem_free)(bus_dma_tag_t, bus_dma_segment_t *, int); 233 int (*_dmamem_map)(bus_dma_tag_t, bus_dma_segment_t *, 234 int, size_t, void **, int); 235 void (*_dmamem_unmap)(bus_dma_tag_t, void *, size_t); 236 paddr_t (*_dmamem_mmap)(bus_dma_tag_t, bus_dma_segment_t *, 237 int, off_t, int, int); 238 int (*_dmatag_subregion)(bus_dma_tag_t, bus_addr_t, bus_addr_t, 239 bus_dma_tag_t *, int); 240 void (*_dmatag_destroy)(bus_dma_tag_t); 241 }; 242 243 #endif /* !defined(_X86_BUS_PRIVATE_H_) */ 244