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