Fix typos in some messages: writting -> writing
[dragonfly.git] / sys / cpu / i386 / include / 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: src/sys/i386/include/bus_dma.h,v 1.15.2.2 2002/11/21 23:36:01 sam Exp $ */
71 /* $DragonFly: src/sys/cpu/i386/include/bus_dma.h,v 1.6 2006/10/23 21:50:31 dillon Exp $ */
72
73 #ifndef _MACHINE_BUS_DMA_H_
74 #define _MACHINE_BUS_DMA_H_
75
76 #ifndef _MACHINE_BUS_AT386_H_
77 #include <machine/bus_at386.h>
78 #endif
79
80 /*
81  * Flags used in various bus DMA methods.
82  */
83 #define BUS_DMA_WAITOK          0x00    /* safe to sleep (pseudo-flag) */
84 #define BUS_DMA_NOWAIT          0x01    /* not safe to sleep */
85 #define BUS_DMA_ALLOCNOW        0x02    /* perform resource allocation now */
86 #define BUS_DMA_COHERENT        0x04    /* map memory to not require sync */
87 #define BUS_DMA_ZERO            0x08    /* allocate zero'ed memory */
88 #define BUS_DMA_BUS1            0x10    /* placeholders for bus functions... */
89 #define BUS_DMA_BUS2            0x20
90 #define BUS_DMA_BUS3            0x40
91 #define BUS_DMA_BUS4            0x80
92
93 /* Forwards needed by prototypes below. */
94 struct mbuf;
95 struct uio;
96
97 /*
98  *      bus_dmasync_op_t
99  *
100  *      Operations performed by bus_dmamap_sync().
101  */
102 typedef enum {
103         BUS_DMASYNC_PREREAD,
104         BUS_DMASYNC_POSTREAD,
105         BUS_DMASYNC_PREWRITE,
106         BUS_DMASYNC_POSTWRITE
107 } bus_dmasync_op_t;
108
109 /*
110  *      bus_dma_tag_t
111  *
112  *      A machine-dependent opaque type describing the characteristics
113  *      of how to perform DMA mappings.  This structure encapsultes
114  *      information concerning address and alignment restrictions, number
115  *      of S/G  segments, amount of data per S/G segment, etc.
116  */
117 typedef struct bus_dma_tag      *bus_dma_tag_t;
118
119 /*
120  *      bus_dmamap_t
121  *
122  *      DMA mapping instance information.
123  */
124 typedef struct bus_dmamap       *bus_dmamap_t;
125
126 /*
127  *      bus_dma_segment_t
128  *
129  *      Describes a single contiguous DMA transaction.  Values
130  *      are suitable for programming into DMA registers.
131  */
132 typedef struct bus_dma_segment {
133         bus_addr_t      ds_addr;        /* DMA address */
134         bus_size_t      ds_len;         /* length of transfer */
135 } bus_dma_segment_t;
136
137 /*
138  * A function that returns 1 if the address cannot be accessed by
139  * a device and 0 if it can be.
140  */
141 typedef int bus_dma_filter_t(void *, bus_addr_t);
142
143 /*
144  * Allocate a device specific dma_tag encapsulating the constraints of
145  * the parent tag in addition to other restrictions specified:
146  *
147  *      alignment:      alignment for segments.
148  *      boundary:       Boundary that segments cannot cross.
149  *      lowaddr:        Low restricted address that cannot appear in a mapping.
150  *      highaddr:       High restricted address that cannot appear in a mapping.
151  *      filtfunc:       An optional function to further test if an address
152  *                      within the range of lowaddr and highaddr cannot appear
153  *                      in a mapping.
154  *      filtfuncarg:    An argument that will be passed to filtfunc in addition
155  *                      to the address to test.
156  *      maxsize:        Maximum mapping size supported by this tag.
157  *      nsegments:      Number of discontinuities allowed in maps.
158  *      maxsegsz:       Maximum size of a segment in the map.
159  *      flags:          Bus DMA flags.
160  *      dmat:           A pointer to set to a valid dma tag should the return
161  *                      value of this function indicate success.
162  */
163 /* XXX Should probably allow specification of alignment */
164 int bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
165                        bus_size_t boundary, bus_addr_t lowaddr,
166                        bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
167                        void *filtfuncarg, bus_size_t maxsize, int nsegments,
168                        bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat);
169
170 int bus_dma_tag_destroy(bus_dma_tag_t dmat);
171
172 /*
173  * Allocate a handle for mapping from kva/uva/physical
174  * address space into bus device space.
175  */
176 int bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
177
178 /*
179  * Destroy  a handle for mapping from kva/uva/physical
180  * address space into bus device space.
181  */
182 int bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
183
184 /*
185  * Allocate a piece of memory that can be efficiently mapped into
186  * bus device space based on the constraints lited in the dma tag.
187  * A dmamap to for use with dmamap_load is also allocated.
188  */
189 int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
190                      bus_dmamap_t *mapp);
191
192 /*
193  * Free a piece of memory and it's allociated dmamap, that was allocated
194  * via bus_dmamem_alloc.
195  */
196 void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
197
198 /*
199  * A function that processes a successfully loaded dma map or an error
200  * from a delayed load map.
201  */
202 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
203
204 /*
205  * Map the buffer buf into bus space using the dmamap map.
206  */
207 int bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
208                     bus_size_t buflen, bus_dmamap_callback_t *callback,
209                     void *callback_arg, int flags);
210
211 /*
212  * Like bus_dmamap_callback but includes map size in bytes.  This is
213  * defined as a separate interface to maintain compatiiblity for users
214  * of bus_dmamap_callback_t--at some point these interfaces should be merged.
215  */
216 typedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
217 /*
218  * Like bus_dmamap_load but for mbufs.  Note the use of the
219  * bus_dmamap_callback2_t interface.
220  */
221 int bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
222                          struct mbuf *mbuf,
223                          bus_dmamap_callback2_t *callback, void *callback_arg,
224                          int flags);
225 /*
226  * Like bus_dmamap_load but for uios.  Note the use of the
227  * bus_dmamap_callback2_t interface.
228  */
229 int bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
230                         struct uio *ui,
231                         bus_dmamap_callback2_t *callback, void *callback_arg,
232                         int flags);
233
234 /*
235  * Perform a syncronization operation on the given map.
236  */
237 void _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_dmasync_op_t);
238 #define bus_dmamap_sync(dmat, dmamap, op)               \
239         if ((dmamap) != NULL)                           \
240                 _bus_dmamap_sync(dmat, dmamap, op)
241
242 /*
243  * Release the mapping held by map.
244  */
245 void _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map);
246 #define bus_dmamap_unload(dmat, dmamap)                 \
247         if ((dmamap) != NULL)                           \
248                 _bus_dmamap_unload(dmat, dmamap)
249
250 #endif /* _MACHINE_BUS_DMA_H_ */