]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/bus_dma.h
zfs: merge openzfs/zfs@a03ebd9be
[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  * SPDX-License-Identifier: (BSD-2-Clause AND BSD-4-Clause)
5  *
6  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
11  * NASA Ames Research Center.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*-
36  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
37  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *      This product includes software developed by Christopher G. Demetriou
50  *      for the NetBSD Project.
51  * 4. The name of the author may not be used to endorse or promote products
52  *    derived from this software without specific prior written permission
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65
66 #ifndef _BUS_DMA_H_
67 #define _BUS_DMA_H_
68
69 #ifdef _KERNEL
70 #include <sys/_bus_dma.h>
71 #endif
72
73 /*
74  * Machine independent interface for mapping physical addresses to peripheral
75  * bus 'physical' addresses, and assisting with DMA operations.
76  *
77  * XXX This file is always included from <machine/bus_dma.h> and should not
78  *     (yet) be included directly.
79  */
80
81 /*
82  * Flags used in various bus DMA methods.
83  */
84 #define BUS_DMA_WAITOK          0x00    /* safe to sleep (pseudo-flag) */
85 #define BUS_DMA_NOWAIT          0x01    /* not safe to sleep */
86 #define BUS_DMA_ALLOCNOW        0x02    /* perform resource allocation now */
87 #define BUS_DMA_COHERENT        0x04    /* hint: map memory in a coherent way */
88 #define BUS_DMA_ZERO            0x08    /* allocate zero'ed memory */
89 #define BUS_DMA_BUS1            0x10    /* placeholders for bus functions... */
90 #define BUS_DMA_BUS2            0x20
91 #define BUS_DMA_BUS3            0x40
92 #define BUS_DMA_BUS4            0x80
93
94 /*
95  * The following two flags are non-standard or specific to only certain
96  * architectures
97  */
98 #define BUS_DMA_NOWRITE         0x100
99 #define BUS_DMA_NOCACHE         0x200
100
101 /*
102  * The following flag is a DMA tag hint that the page offset of the
103  * loaded kernel virtual address must be preserved in the first
104  * physical segment address, when the KVA is loaded into DMA.
105  */
106 #define BUS_DMA_KEEP_PG_OFFSET  0x400
107
108 #define BUS_DMA_LOAD_MBUF       0x800
109
110 /* Forwards needed by prototypes below. */
111 union ccb;
112 struct bio;
113 struct crypto_buffer;
114 struct cryptop;
115 struct mbuf;
116 struct memdesc;
117 struct pmap;
118 struct uio;
119
120 /*
121  * Operations performed by bus_dmamap_sync().
122  */
123 #define BUS_DMASYNC_PREREAD     1
124 #define BUS_DMASYNC_POSTREAD    2
125 #define BUS_DMASYNC_PREWRITE    4
126 #define BUS_DMASYNC_POSTWRITE   8
127
128 /*
129  *      bus_dma_segment_t
130  *
131  *      Describes a single contiguous DMA transaction.  Values
132  *      are suitable for programming into DMA registers.
133  */
134 typedef struct bus_dma_segment {
135         bus_addr_t      ds_addr;        /* DMA address */
136         bus_size_t      ds_len;         /* length of transfer */
137 } bus_dma_segment_t;
138
139 #ifdef _KERNEL
140 /*
141  * A function that returns 1 if the address cannot be accessed by
142  * a device and 0 if it can be.
143  */
144 typedef int bus_dma_filter_t(void *, bus_addr_t);
145
146 /*
147  * Generic helper function for manipulating mutexes.
148  */
149 void busdma_lock_mutex(void *arg, bus_dma_lock_op_t op);
150
151 /*
152  * Internal helper function used by tags that do not defer loads.
153  */
154 void _busdma_dflt_lock(void *arg, bus_dma_lock_op_t op);
155
156 /*
157  * Allocate a device specific dma_tag encapsulating the constraints of
158  * the parent tag in addition to other restrictions specified:
159  *
160  *      alignment:      Alignment for segments.
161  *      boundary:       Boundary that segments cannot cross.
162  *      lowaddr:        Low restricted address that cannot appear in a mapping.
163  *      highaddr:       High restricted address that cannot appear in a mapping.
164  *      filtfunc:       An optional function to further test if an address
165  *                      within the range of lowaddr and highaddr cannot appear
166  *                      in a mapping.
167  *      filtfuncarg:    An argument that will be passed to filtfunc in addition
168  *                      to the address to test.
169  *      maxsize:        Maximum mapping size supported by this tag.
170  *      nsegments:      Number of discontinuities allowed in maps.
171  *      maxsegsz:       Maximum size of a segment in the map.
172  *      flags:          Bus DMA flags.
173  *      lockfunc:       An optional function to handle driver-defined lock
174  *                      operations.
175  *      lockfuncarg:    An argument that will be passed to lockfunc in addition
176  *                      to the lock operation.
177  *      dmat:           A pointer to set to a valid dma tag should the return
178  *                      value of this function indicate success.
179  */
180 /* XXX Should probably allow specification of alignment */
181 int bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
182                        bus_addr_t boundary, bus_addr_t lowaddr,
183                        bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
184                        void *filtfuncarg, bus_size_t maxsize, int nsegments,
185                        bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
186                        void *lockfuncarg, bus_dma_tag_t *dmat);
187
188 /*
189  * Functions for creating and cloning tags via a template,
190  *
191  * bus_dma_template_t is made avaialble publicly so it can be allocated
192  * from the caller stack.  Its contents should be considered private, and
193  * should only be accessed via the documented APIs and macros
194  */
195 typedef struct {
196         bus_dma_tag_t           parent;
197         bus_size_t              alignment;
198         bus_addr_t              boundary;
199         bus_addr_t              lowaddr;
200         bus_addr_t              highaddr;
201         bus_size_t              maxsize;
202         int                     nsegments;
203         bus_size_t              maxsegsize;
204         int                     flags;
205         bus_dma_lock_t          *lockfunc;
206         void                    *lockfuncarg;
207         const char              *name;
208 } bus_dma_template_t;
209
210 /*
211  * These enum values should not be re-ordered.  BD_PARAM_INVALID is an
212  * invalid key and will trigger a panic.
213  */
214 typedef enum {
215         BD_PARAM_INVALID        = 0,
216         BD_PARAM_PARENT         = 1,
217         BD_PARAM_ALIGNMENT      = 2,
218         BD_PARAM_BOUNDARY       = 3,
219         BD_PARAM_LOWADDR        = 4,
220         BD_PARAM_HIGHADDR       = 5,
221         BD_PARAM_MAXSIZE        = 6,
222         BD_PARAM_NSEGMENTS      = 7,
223         BD_PARAM_MAXSEGSIZE     = 8,
224         BD_PARAM_FLAGS          = 9,
225         BD_PARAM_LOCKFUNC       = 10,
226         BD_PARAM_LOCKFUNCARG    = 11,
227         BD_PARAM_NAME           = 12
228 } bus_dma_param_key_t;
229
230 /* These contents should also be considered private */
231 typedef struct {
232         bus_dma_param_key_t     key;
233         union {
234                 void *ptr;
235                 vm_paddr_t pa;
236                 uintmax_t num;
237         };
238 } bus_dma_param_t;
239
240 #define BD_PARENT(val)          { BD_PARAM_PARENT, .ptr = val }
241 #define BD_ALIGNMENT(val)       { BD_PARAM_ALIGNMENT, .num = val }
242 #define BD_BOUNDARY(val)        { BD_PARAM_BOUNDARY, .num = val }
243 #define BD_LOWADDR(val)         { BD_PARAM_LOWADDR, .pa = val }
244 #define BD_HIGHADDR(val)        { BD_PARAM_HIGHADDR, .pa = val }
245 #define BD_MAXSIZE(val)         { BD_PARAM_MAXSIZE, .num = val }
246 #define BD_NSEGMENTS(val)       { BD_PARAM_NSEGMENTS, .num = val }
247 #define BD_MAXSEGSIZE(val)      { BD_PARAM_MAXSEGSIZE, .num = val }
248 #define BD_FLAGS(val)           { BD_PARAM_FLAGS, .num = val }
249 #define BD_LOCKFUNC(val)        { BD_PARAM_LOCKFUNC, .ptr = val }
250 #define BD_LOCKFUNCARG(val)     { BD_PARAM_LOCKFUNCARG, .ptr = val }
251 #define BD_NAME(val)            { BD_PARAM_NAME, .ptr = val }
252
253 #define BUS_DMA_TEMPLATE_FILL(t, kv...) \
254 do {                                    \
255         bus_dma_param_t pm[] = { kv };  \
256         bus_dma_template_fill(t, pm, howmany(sizeof(pm), sizeof(pm[0]))); \
257 } while (0)
258
259 void bus_dma_template_init(bus_dma_template_t *t, bus_dma_tag_t parent);
260 int bus_dma_template_tag(bus_dma_template_t *t, bus_dma_tag_t *dmat);
261 void bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat);
262 void bus_dma_template_fill(bus_dma_template_t *t, bus_dma_param_t *kv,
263     u_int count);
264
265 /*
266  * Set the memory domain to be used for allocations.
267  *
268  * Automatic for PCI devices.  Must be set prior to creating maps or
269  * allocating memory.
270  */
271 int bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain);
272
273 int bus_dma_tag_destroy(bus_dma_tag_t dmat);
274
275 /*
276  * A function that processes a successfully loaded dma map or an error
277  * from a delayed load map.
278  */
279 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
280
281 /*
282  * Like bus_dmamap_callback but includes map size in bytes.  This is
283  * defined as a separate interface to maintain compatibility for users
284  * of bus_dmamap_callback_t--at some point these interfaces should be merged.
285  */
286 typedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
287
288 /*
289  * Map the buffer buf into bus space using the dmamap map.
290  */
291 int bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
292                     bus_size_t buflen, bus_dmamap_callback_t *callback,
293                     void *callback_arg, int flags);
294
295 /*
296  * Like bus_dmamap_load but for mbufs.  Note the use of the
297  * bus_dmamap_callback2_t interface.
298  */
299 int bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
300                          struct mbuf *mbuf,
301                          bus_dmamap_callback2_t *callback, void *callback_arg,
302                          int flags);
303
304 int bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
305                             struct mbuf *mbuf, bus_dma_segment_t *segs,
306                             int *nsegs, int flags);
307
308 /*
309  * Like bus_dmamap_load but for uios.  Note the use of the
310  * bus_dmamap_callback2_t interface.
311  */
312 int bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
313                         struct uio *ui,
314                         bus_dmamap_callback2_t *callback, void *callback_arg,
315                         int flags);
316
317 /*
318  * Like bus_dmamap_load but for cam control blocks.
319  */
320 int bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
321                         bus_dmamap_callback_t *callback, void *callback_arg,
322                         int flags);
323
324 /*
325  * Like bus_dmamap_load but for bios.
326  */
327 int bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio,
328                         bus_dmamap_callback_t *callback, void *callback_arg,
329                         int flags);
330
331 /*
332  * Like bus_dmamap_load but for crypto ops.
333  */
334 int bus_dmamap_load_crp(bus_dma_tag_t dmat, bus_dmamap_t map,
335                         struct cryptop *crp, bus_dmamap_callback_t *callback,
336                         void *callback_arg, int flags);
337 int bus_dmamap_load_crp_buffer(bus_dma_tag_t dmat, bus_dmamap_t map,
338                                struct crypto_buffer *cb,
339                                bus_dmamap_callback_t *callback,
340                                void *callback_arg, int flags);
341
342 /*
343  * Loads any memory descriptor.
344  */
345 int bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map,
346                         struct memdesc *mem, bus_dmamap_callback_t *callback,
347                         void *callback_arg, int flags);
348
349 /*
350  * Placeholder for use by busdma implementations which do not benefit
351  * from optimized procedure to load an array of vm_page_t.  Falls back
352  * to do _bus_dmamap_load_phys() in loop.
353  */
354 int bus_dmamap_load_ma_triv(bus_dma_tag_t dmat, bus_dmamap_t map,
355     struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
356     bus_dma_segment_t *segs, int *segp);
357
358 #ifdef WANT_INLINE_DMAMAP
359 #define BUS_DMAMAP_OP static inline
360 #else
361 #define BUS_DMAMAP_OP
362 #endif
363
364 /*
365  * Allocate a handle for mapping from kva/uva/physical
366  * address space into bus device space.
367  */
368 BUS_DMAMAP_OP int bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
369
370 /*
371  * Destroy a handle for mapping from kva/uva/physical
372  * address space into bus device space.
373  */
374 BUS_DMAMAP_OP int bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
375
376 /*
377  * Allocate a piece of memory that can be efficiently mapped into
378  * bus device space based on the constraints listed in the dma tag.
379  * A dmamap to for use with dmamap_load is also allocated.
380  */
381 BUS_DMAMAP_OP int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
382                      bus_dmamap_t *mapp);
383
384 /*
385  * Free a piece of memory and its allocated dmamap, that was allocated
386  * via bus_dmamem_alloc.
387  */
388 BUS_DMAMAP_OP void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
389
390 /*
391  * Perform a synchronization operation on the given map. If the map
392  * is NULL we have a fully IO-coherent system.
393  */
394 BUS_DMAMAP_OP void bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t dmamap, bus_dmasync_op_t op);
395
396 /*
397  * Release the mapping held by map.
398  */
399 BUS_DMAMAP_OP void bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t dmamap);
400
401 #undef BUS_DMAMAP_OP
402 #endif /* _KERNEL */
403
404 #endif /* _BUS_DMA_H_ */