]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/bus_dma.h
Update libucl to 2014-03-03
[FreeBSD/FreeBSD.git] / sys / sys / bus_dma.h
1 /*      $NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $    */
2
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the NetBSD
22  *      Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 /*-
41  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
42  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *      This product includes software developed by Christopher G. Demetriou
55  *      for the NetBSD Project.
56  * 4. The name of the author may not be used to endorse or promote products
57  *    derived from this software without specific prior written permission
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69  */
70 /* $FreeBSD$ */
71
72 #ifndef _BUS_DMA_H_
73 #define _BUS_DMA_H_
74
75 #include <sys/_bus_dma.h>
76
77 /*
78  * Machine independent interface for mapping physical addresses to peripheral
79  * bus 'physical' addresses, and assisting with DMA operations.
80  *
81  * XXX This file is always included from <machine/bus_dma.h> and should not
82  *     (yet) be included directly.
83  */
84
85 /*
86  * Flags used in various bus DMA methods.
87  */
88 #define BUS_DMA_WAITOK          0x00    /* safe to sleep (pseudo-flag) */
89 #define BUS_DMA_NOWAIT          0x01    /* not safe to sleep */
90 #define BUS_DMA_ALLOCNOW        0x02    /* perform resource allocation now */
91 #define BUS_DMA_COHERENT        0x04    /* hint: map memory in a coherent way */
92 #define BUS_DMA_ZERO            0x08    /* allocate zero'ed memory */
93 #define BUS_DMA_BUS1            0x10    /* placeholders for bus functions... */
94 #define BUS_DMA_BUS2            0x20
95 #define BUS_DMA_BUS3            0x40
96 #define BUS_DMA_BUS4            0x80
97
98 /*
99  * The following two flags are non-standard or specific to only certain
100  * architectures
101  */
102 #define BUS_DMA_NOWRITE         0x100
103 #define BUS_DMA_NOCACHE         0x200
104
105 /*
106  * The following flag is a DMA tag hint that the page offset of the
107  * loaded kernel virtual address must be preserved in the first
108  * physical segment address, when the KVA is loaded into DMA.
109  */
110 #define BUS_DMA_KEEP_PG_OFFSET  0x400
111
112 #define BUS_DMA_LOAD_MBUF       0x800
113
114 /* Forwards needed by prototypes below. */
115 union ccb;
116 struct bio;
117 struct mbuf;
118 struct memdesc;
119 struct pmap;
120 struct uio;
121
122 /*
123  * Operations performed by bus_dmamap_sync().
124  */
125 #define BUS_DMASYNC_PREREAD     1
126 #define BUS_DMASYNC_POSTREAD    2
127 #define BUS_DMASYNC_PREWRITE    4
128 #define BUS_DMASYNC_POSTWRITE   8
129
130 /*
131  *      bus_dma_segment_t
132  *
133  *      Describes a single contiguous DMA transaction.  Values
134  *      are suitable for programming into DMA registers.
135  */
136 typedef struct bus_dma_segment {
137         bus_addr_t      ds_addr;        /* DMA address */
138         bus_size_t      ds_len;         /* length of transfer */
139 } bus_dma_segment_t;
140
141 /*
142  * A function that returns 1 if the address cannot be accessed by
143  * a device and 0 if it can be.
144  */
145 typedef int bus_dma_filter_t(void *, bus_addr_t);
146
147 /*
148  * Generic helper function for manipulating mutexes.
149  */
150 void busdma_lock_mutex(void *arg, bus_dma_lock_op_t op);
151
152 /*
153  * Allocate a device specific dma_tag encapsulating the constraints of
154  * the parent tag in addition to other restrictions specified:
155  *
156  *      alignment:      Alignment for segments.
157  *      boundary:       Boundary that segments cannot cross.
158  *      lowaddr:        Low restricted address that cannot appear in a mapping.
159  *      highaddr:       High restricted address that cannot appear in a mapping.
160  *      filtfunc:       An optional function to further test if an address
161  *                      within the range of lowaddr and highaddr cannot appear
162  *                      in a mapping.
163  *      filtfuncarg:    An argument that will be passed to filtfunc in addition
164  *                      to the address to test.
165  *      maxsize:        Maximum mapping size supported by this tag.
166  *      nsegments:      Number of discontinuities allowed in maps.
167  *      maxsegsz:       Maximum size of a segment in the map.
168  *      flags:          Bus DMA flags.
169  *      lockfunc:       An optional function to handle driver-defined lock
170  *                      operations.
171  *      lockfuncarg:    An argument that will be passed to lockfunc in addition
172  *                      to the lock operation.
173  *      dmat:           A pointer to set to a valid dma tag should the return
174  *                      value of this function indicate success.
175  */
176 /* XXX Should probably allow specification of alignment */
177 int bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
178                        bus_addr_t boundary, bus_addr_t lowaddr,
179                        bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
180                        void *filtfuncarg, bus_size_t maxsize, int nsegments,
181                        bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
182                        void *lockfuncarg, bus_dma_tag_t *dmat);
183
184 int bus_dma_tag_destroy(bus_dma_tag_t dmat);
185
186 /*
187  * A function that processes a successfully loaded dma map or an error
188  * from a delayed load map.
189  */
190 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
191
192 /*
193  * Like bus_dmamap_callback but includes map size in bytes.  This is
194  * defined as a separate interface to maintain compatibility for users
195  * of bus_dmamap_callback_t--at some point these interfaces should be merged.
196  */
197 typedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
198
199 /*
200  * Map the buffer buf into bus space using the dmamap map.
201  */
202 int bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
203                     bus_size_t buflen, bus_dmamap_callback_t *callback,
204                     void *callback_arg, int flags);
205
206 /*
207  * Like bus_dmamap_load but for mbufs.  Note the use of the
208  * bus_dmamap_callback2_t interface.
209  */
210 int bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
211                          struct mbuf *mbuf,
212                          bus_dmamap_callback2_t *callback, void *callback_arg,
213                          int flags);
214
215 int bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
216                             struct mbuf *mbuf, bus_dma_segment_t *segs,
217                             int *nsegs, int flags);
218
219 /*
220  * Like bus_dmamap_load but for uios.  Note the use of the
221  * bus_dmamap_callback2_t interface.
222  */
223 int bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
224                         struct uio *ui,
225                         bus_dmamap_callback2_t *callback, void *callback_arg,
226                         int flags);
227
228 /*
229  * Like bus_dmamap_load but for cam control blocks.
230  */
231 int bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
232                         bus_dmamap_callback_t *callback, void *callback_arg,
233                         int flags);
234
235 /*
236  * Like bus_dmamap_load but for bios.
237  */
238 int bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio,
239                         bus_dmamap_callback_t *callback, void *callback_arg,
240                         int flags);
241
242 /*
243  * Loads any memory descriptor.
244  */
245 int bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map,
246                         struct memdesc *mem, bus_dmamap_callback_t *callback,
247                         void *callback_arg, int flags);
248
249 /*
250  * Placeholder for use by busdma implementations which do not benefit
251  * from optimized procedure to load an array of vm_page_t.  Falls back
252  * to do _bus_dmamap_load_phys() in loop.
253  */
254 int bus_dmamap_load_ma_triv(bus_dma_tag_t dmat, bus_dmamap_t map,
255     struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
256     bus_dma_segment_t *segs, int *segp);
257
258 /*
259  * XXX sparc64 uses the same interface, but a much different implementation.
260  *     <machine/bus_dma.h> for the sparc64 arch contains the equivalent
261  *     declarations.
262  */
263 #if !defined(__sparc64__)
264
265 /*
266  * Allocate a handle for mapping from kva/uva/physical
267  * address space into bus device space.
268  */
269 int bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
270
271 /*
272  * Destroy a handle for mapping from kva/uva/physical
273  * address space into bus device space.
274  */
275 int bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
276
277 /*
278  * Allocate a piece of memory that can be efficiently mapped into
279  * bus device space based on the constraints listed in the dma tag.
280  * A dmamap to for use with dmamap_load is also allocated.
281  */
282 int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
283                      bus_dmamap_t *mapp);
284
285 /*
286  * Free a piece of memory and its allocated dmamap, that was allocated
287  * via bus_dmamem_alloc.
288  */
289 void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
290
291 /*
292  * Perform a synchronization operation on the given map.
293  */
294 void _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_dmasync_op_t);
295 #define bus_dmamap_sync(dmat, dmamap, op)                       \
296         do {                                                    \
297                 if ((dmamap) != NULL)                           \
298                         _bus_dmamap_sync(dmat, dmamap, op);     \
299         } while (0)
300
301 /*
302  * Release the mapping held by map.
303  */
304 void _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map);
305 #define bus_dmamap_unload(dmat, dmamap)                         \
306         do {                                                    \
307                 if ((dmamap) != NULL)                           \
308                         _bus_dmamap_unload(dmat, dmamap);       \
309         } while (0)
310
311 /*
312  * The following functions define the interface between the MD and MI
313  * busdma layers.  These are not intended for consumption by driver
314  * software.
315  */
316 void __bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
317                          struct memdesc *mem,
318                          bus_dmamap_callback_t *callback,
319                          void *callback_arg);
320
321 #define _bus_dmamap_waitok(dmat, map, mem, callback, callback_arg)      \
322         do {                                                            \
323                 if ((map) != NULL)                                      \
324                         __bus_dmamap_waitok(dmat, map, mem, callback,   \
325                             callback_arg);                              \
326         } while (0);
327
328 int _bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map,
329                             void *buf, bus_size_t buflen, struct pmap *pmap,
330                             int flags, bus_dma_segment_t *segs, int *segp);
331
332 int _bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
333                           vm_paddr_t paddr, bus_size_t buflen,
334                           int flags, bus_dma_segment_t *segs, int *segp);
335
336 int _bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map,
337     struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
338     bus_dma_segment_t *segs, int *segp);
339
340 bus_dma_segment_t *_bus_dmamap_complete(bus_dma_tag_t dmat,
341                                         bus_dmamap_t map,
342                                         bus_dma_segment_t *segs,
343                                         int nsegs, int error);
344
345 #endif /* __sparc64__ */
346
347 #endif /* _BUS_DMA_H_ */