]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/bus_dma.h
ident(1): Normalizing date format
[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-NetBSD 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 /* $FreeBSD$ */
66
67 #ifndef _BUS_DMA_H_
68 #define _BUS_DMA_H_
69
70 #ifdef _KERNEL
71 #include <sys/_bus_dma.h>
72 #endif
73
74 /*
75  * Machine independent interface for mapping physical addresses to peripheral
76  * bus 'physical' addresses, and assisting with DMA operations.
77  *
78  * XXX This file is always included from <machine/bus_dma.h> and should not
79  *     (yet) be included directly.
80  */
81
82 /*
83  * Flags used in various bus DMA methods.
84  */
85 #define BUS_DMA_WAITOK          0x00    /* safe to sleep (pseudo-flag) */
86 #define BUS_DMA_NOWAIT          0x01    /* not safe to sleep */
87 #define BUS_DMA_ALLOCNOW        0x02    /* perform resource allocation now */
88 #define BUS_DMA_COHERENT        0x04    /* hint: map memory in a coherent way */
89 #define BUS_DMA_ZERO            0x08    /* allocate zero'ed memory */
90 #define BUS_DMA_BUS1            0x10    /* placeholders for bus functions... */
91 #define BUS_DMA_BUS2            0x20
92 #define BUS_DMA_BUS3            0x40
93 #define BUS_DMA_BUS4            0x80
94
95 /*
96  * The following two flags are non-standard or specific to only certain
97  * architectures
98  */
99 #define BUS_DMA_NOWRITE         0x100
100 #define BUS_DMA_NOCACHE         0x200
101
102 /*
103  * The following flag is a DMA tag hint that the page offset of the
104  * loaded kernel virtual address must be preserved in the first
105  * physical segment address, when the KVA is loaded into DMA.
106  */
107 #define BUS_DMA_KEEP_PG_OFFSET  0x400
108
109 #define BUS_DMA_LOAD_MBUF       0x800
110
111 /* Forwards needed by prototypes below. */
112 union ccb;
113 struct bio;
114 struct crypto_buffer;
115 struct cryptop;
116 struct mbuf;
117 struct memdesc;
118 struct pmap;
119 struct uio;
120
121 /*
122  * Operations performed by bus_dmamap_sync().
123  */
124 #define BUS_DMASYNC_PREREAD     1
125 #define BUS_DMASYNC_POSTREAD    2
126 #define BUS_DMASYNC_PREWRITE    4
127 #define BUS_DMASYNC_POSTWRITE   8
128
129 /*
130  *      bus_dma_segment_t
131  *
132  *      Describes a single contiguous DMA transaction.  Values
133  *      are suitable for programming into DMA registers.
134  */
135 typedef struct bus_dma_segment {
136         bus_addr_t      ds_addr;        /* DMA address */
137         bus_size_t      ds_len;         /* length of transfer */
138 } bus_dma_segment_t;
139
140 #ifdef _KERNEL
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 /*
185  * Functions for creating and cloning tags via a template,
186  *
187  * bus_dma_template_t is made avaialble publicly so it can be allocated
188  * from the caller stack.  Its contents should be considered private, and
189  * should only be accessed via the documented APIs and macros
190  */
191 typedef struct {
192         bus_dma_tag_t           parent;
193         bus_size_t              alignment;
194         bus_addr_t              boundary;
195         bus_addr_t              lowaddr;
196         bus_addr_t              highaddr;
197         bus_size_t              maxsize;
198         int                     nsegments;
199         bus_size_t              maxsegsize;
200         int                     flags;
201         bus_dma_lock_t          *lockfunc;
202         void                    *lockfuncarg;
203         const char              *name;
204 } bus_dma_template_t;
205
206 /*
207  * These enum values should not be re-ordered.  BD_PARAM_INVALID is an
208  * invalid key and will trigger a panic.
209  */
210 typedef enum {
211         BD_PARAM_INVALID        = 0,
212         BD_PARAM_PARENT         = 1,
213         BD_PARAM_ALIGNMENT      = 2,
214         BD_PARAM_BOUNDARY       = 3,
215         BD_PARAM_LOWADDR        = 4,
216         BD_PARAM_HIGHADDR       = 5,
217         BD_PARAM_MAXSIZE        = 6,
218         BD_PARAM_NSEGMENTS      = 7,
219         BD_PARAM_MAXSEGSIZE     = 8,
220         BD_PARAM_FLAGS          = 9,
221         BD_PARAM_LOCKFUNC       = 10,
222         BD_PARAM_LOCKFUNCARG    = 11,
223         BD_PARAM_NAME           = 12
224 } bus_dma_param_key_t;
225
226 /* These contents should also be considered private */
227 typedef struct {
228         bus_dma_param_key_t     key;
229         union {
230                 void *ptr;
231                 vm_paddr_t pa;
232                 uintmax_t num;
233         };
234 } bus_dma_param_t;
235
236 #define BD_PARENT(val)          { BD_PARAM_PARENT, .ptr = val }
237 #define BD_ALIGNMENT(val)       { BD_PARAM_ALIGNMENT, .num = val }
238 #define BD_BOUNDARY(val)        { BD_PARAM_BOUNDARY, .num = val }
239 #define BD_LOWADDR(val)         { BD_PARAM_LOWADDR, .pa = val }
240 #define BD_HIGHADDR(val)        { BD_PARAM_HIGHADDR, .pa = val }
241 #define BD_MAXSIZE(val)         { BD_PARAM_MAXSIZE, .num = val }
242 #define BD_NSEGMENTS(val)       { BD_PARAM_NSEGMENTS, .num = val }
243 #define BD_MAXSEGSIZE(val)      { BD_PARAM_MAXSEGSIZE, .num = val }
244 #define BD_FLAGS(val)           { BD_PARAM_FLAGS, .num = val }
245 #define BD_LOCKFUNC(val)        { BD_PARAM_LOCKFUNC, .ptr = val }
246 #define BD_LOCKFUNCARG(val)     { BD_PARAM_LOCKFUNCARG, .ptr = val }
247 #define BD_NAME(val)            { BD_PARAM_NAME, .ptr = val }
248
249 #define BUS_DMA_TEMPLATE_FILL(t, kv...) \
250 do {                                    \
251         bus_dma_param_t pm[] = { kv };  \
252         bus_dma_template_fill(t, pm, howmany(sizeof(pm), sizeof(pm[0]))); \
253 } while (0)
254
255 void bus_dma_template_init(bus_dma_template_t *t, bus_dma_tag_t parent);
256 int bus_dma_template_tag(bus_dma_template_t *t, bus_dma_tag_t *dmat);
257 void bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat);
258 void bus_dma_template_fill(bus_dma_template_t *t, bus_dma_param_t *kv,
259     u_int count);
260
261 /*
262  * Set the memory domain to be used for allocations.
263  *
264  * Automatic for PCI devices.  Must be set prior to creating maps or
265  * allocating memory.
266  */
267 int bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain);
268
269 int bus_dma_tag_destroy(bus_dma_tag_t dmat);
270
271 /*
272  * A function that processes a successfully loaded dma map or an error
273  * from a delayed load map.
274  */
275 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
276
277 /*
278  * Like bus_dmamap_callback but includes map size in bytes.  This is
279  * defined as a separate interface to maintain compatibility for users
280  * of bus_dmamap_callback_t--at some point these interfaces should be merged.
281  */
282 typedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
283
284 /*
285  * Map the buffer buf into bus space using the dmamap map.
286  */
287 int bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
288                     bus_size_t buflen, bus_dmamap_callback_t *callback,
289                     void *callback_arg, int flags);
290
291 /*
292  * Like bus_dmamap_load but for mbufs.  Note the use of the
293  * bus_dmamap_callback2_t interface.
294  */
295 int bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
296                          struct mbuf *mbuf,
297                          bus_dmamap_callback2_t *callback, void *callback_arg,
298                          int flags);
299
300 int bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
301                             struct mbuf *mbuf, bus_dma_segment_t *segs,
302                             int *nsegs, int flags);
303
304 /*
305  * Like bus_dmamap_load but for uios.  Note the use of the
306  * bus_dmamap_callback2_t interface.
307  */
308 int bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
309                         struct uio *ui,
310                         bus_dmamap_callback2_t *callback, void *callback_arg,
311                         int flags);
312
313 /*
314  * Like bus_dmamap_load but for cam control blocks.
315  */
316 int bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
317                         bus_dmamap_callback_t *callback, void *callback_arg,
318                         int flags);
319
320 /*
321  * Like bus_dmamap_load but for bios.
322  */
323 int bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio,
324                         bus_dmamap_callback_t *callback, void *callback_arg,
325                         int flags);
326
327 /*
328  * Like bus_dmamap_load but for crypto ops.
329  */
330 int bus_dmamap_load_crp(bus_dma_tag_t dmat, bus_dmamap_t map,
331                         struct cryptop *crp, bus_dmamap_callback_t *callback,
332                         void *callback_arg, int flags);
333 int bus_dmamap_load_crp_buffer(bus_dma_tag_t dmat, bus_dmamap_t map,
334                                struct crypto_buffer *cb,
335                                bus_dmamap_callback_t *callback,
336                                void *callback_arg, int flags);
337
338 /*
339  * Loads any memory descriptor.
340  */
341 int bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map,
342                         struct memdesc *mem, bus_dmamap_callback_t *callback,
343                         void *callback_arg, int flags);
344
345 /*
346  * Placeholder for use by busdma implementations which do not benefit
347  * from optimized procedure to load an array of vm_page_t.  Falls back
348  * to do _bus_dmamap_load_phys() in loop.
349  */
350 int bus_dmamap_load_ma_triv(bus_dma_tag_t dmat, bus_dmamap_t map,
351     struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
352     bus_dma_segment_t *segs, int *segp);
353
354 #ifdef WANT_INLINE_DMAMAP
355 #define BUS_DMAMAP_OP static inline
356 #else
357 #define BUS_DMAMAP_OP
358 #endif
359
360 /*
361  * Allocate a handle for mapping from kva/uva/physical
362  * address space into bus device space.
363  */
364 BUS_DMAMAP_OP int bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
365
366 /*
367  * Destroy a handle for mapping from kva/uva/physical
368  * address space into bus device space.
369  */
370 BUS_DMAMAP_OP int bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
371
372 /*
373  * Allocate a piece of memory that can be efficiently mapped into
374  * bus device space based on the constraints listed in the dma tag.
375  * A dmamap to for use with dmamap_load is also allocated.
376  */
377 BUS_DMAMAP_OP int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
378                      bus_dmamap_t *mapp);
379
380 /*
381  * Free a piece of memory and its allocated dmamap, that was allocated
382  * via bus_dmamem_alloc.
383  */
384 BUS_DMAMAP_OP void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
385
386 /*
387  * Perform a synchronization operation on the given map. If the map
388  * is NULL we have a fully IO-coherent system.
389  */
390 BUS_DMAMAP_OP void bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t dmamap, bus_dmasync_op_t op);
391
392 /*
393  * Release the mapping held by map.
394  */
395 BUS_DMAMAP_OP void bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t dmamap);
396
397 #undef BUS_DMAMAP_OP
398 #endif /* _KERNEL */
399
400 #endif /* _BUS_DMA_H_ */