]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/bus_dma.h
dts: Update our copy for arm, arm64 and riscv dts to Linux 5.5
[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 mbuf;
115 struct memdesc;
116 struct pmap;
117 struct uio;
118
119 /*
120  * Operations performed by bus_dmamap_sync().
121  */
122 #define BUS_DMASYNC_PREREAD     1
123 #define BUS_DMASYNC_POSTREAD    2
124 #define BUS_DMASYNC_PREWRITE    4
125 #define BUS_DMASYNC_POSTWRITE   8
126
127 /*
128  *      bus_dma_segment_t
129  *
130  *      Describes a single contiguous DMA transaction.  Values
131  *      are suitable for programming into DMA registers.
132  */
133 typedef struct bus_dma_segment {
134         bus_addr_t      ds_addr;        /* DMA address */
135         bus_size_t      ds_len;         /* length of transfer */
136 } bus_dma_segment_t;
137
138 #ifdef _KERNEL
139 /*
140  * A function that returns 1 if the address cannot be accessed by
141  * a device and 0 if it can be.
142  */
143 typedef int bus_dma_filter_t(void *, bus_addr_t);
144
145 /*
146  * Generic helper function for manipulating mutexes.
147  */
148 void busdma_lock_mutex(void *arg, bus_dma_lock_op_t op);
149
150 /*
151  * Allocate a device specific dma_tag encapsulating the constraints of
152  * the parent tag in addition to other restrictions specified:
153  *
154  *      alignment:      Alignment for segments.
155  *      boundary:       Boundary that segments cannot cross.
156  *      lowaddr:        Low restricted address that cannot appear in a mapping.
157  *      highaddr:       High restricted address that cannot appear in a mapping.
158  *      filtfunc:       An optional function to further test if an address
159  *                      within the range of lowaddr and highaddr cannot appear
160  *                      in a mapping.
161  *      filtfuncarg:    An argument that will be passed to filtfunc in addition
162  *                      to the address to test.
163  *      maxsize:        Maximum mapping size supported by this tag.
164  *      nsegments:      Number of discontinuities allowed in maps.
165  *      maxsegsz:       Maximum size of a segment in the map.
166  *      flags:          Bus DMA flags.
167  *      lockfunc:       An optional function to handle driver-defined lock
168  *                      operations.
169  *      lockfuncarg:    An argument that will be passed to lockfunc in addition
170  *                      to the lock operation.
171  *      dmat:           A pointer to set to a valid dma tag should the return
172  *                      value of this function indicate success.
173  */
174 /* XXX Should probably allow specification of alignment */
175 int bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
176                        bus_addr_t boundary, bus_addr_t lowaddr,
177                        bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
178                        void *filtfuncarg, bus_size_t maxsize, int nsegments,
179                        bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
180                        void *lockfuncarg, bus_dma_tag_t *dmat);
181
182 /* Functions for creating and cloning tags via a template */
183 typedef struct {
184         bus_dma_tag_t           parent;
185         bus_size_t              alignment;
186         bus_addr_t              boundary;
187         bus_addr_t              lowaddr;
188         bus_addr_t              highaddr;
189         bus_size_t              maxsize;
190         int                     nsegments;
191         bus_size_t              maxsegsize;
192         int                     flags;
193         bus_dma_lock_t          *lockfunc;
194         void                    *lockfuncarg;
195 } bus_dma_tag_template_t;
196 void bus_dma_template_init(bus_dma_tag_template_t *t, bus_dma_tag_t parent);
197 int bus_dma_template_tag(bus_dma_tag_template_t *t, bus_dma_tag_t *dmat);
198 void bus_dma_template_clone(bus_dma_tag_template_t *t, bus_dma_tag_t dmat);
199
200 /*
201  * Set the memory domain to be used for allocations.
202  *
203  * Automatic for PCI devices.  Must be set prior to creating maps or
204  * allocating memory.
205  */
206 int bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain);
207
208 int bus_dma_tag_destroy(bus_dma_tag_t dmat);
209
210 /*
211  * A function that processes a successfully loaded dma map or an error
212  * from a delayed load map.
213  */
214 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
215
216 /*
217  * Like bus_dmamap_callback but includes map size in bytes.  This is
218  * defined as a separate interface to maintain compatibility for users
219  * of bus_dmamap_callback_t--at some point these interfaces should be merged.
220  */
221 typedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
222
223 /*
224  * Map the buffer buf into bus space using the dmamap map.
225  */
226 int bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
227                     bus_size_t buflen, bus_dmamap_callback_t *callback,
228                     void *callback_arg, int flags);
229
230 /*
231  * Like bus_dmamap_load but for mbufs.  Note the use of the
232  * bus_dmamap_callback2_t interface.
233  */
234 int bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
235                          struct mbuf *mbuf,
236                          bus_dmamap_callback2_t *callback, void *callback_arg,
237                          int flags);
238
239 int bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
240                             struct mbuf *mbuf, bus_dma_segment_t *segs,
241                             int *nsegs, int flags);
242
243 /*
244  * Like bus_dmamap_load but for uios.  Note the use of the
245  * bus_dmamap_callback2_t interface.
246  */
247 int bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
248                         struct uio *ui,
249                         bus_dmamap_callback2_t *callback, void *callback_arg,
250                         int flags);
251
252 /*
253  * Like bus_dmamap_load but for cam control blocks.
254  */
255 int bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
256                         bus_dmamap_callback_t *callback, void *callback_arg,
257                         int flags);
258
259 /*
260  * Like bus_dmamap_load but for bios.
261  */
262 int bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio,
263                         bus_dmamap_callback_t *callback, void *callback_arg,
264                         int flags);
265
266 /*
267  * Loads any memory descriptor.
268  */
269 int bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map,
270                         struct memdesc *mem, bus_dmamap_callback_t *callback,
271                         void *callback_arg, int flags);
272
273 /*
274  * Placeholder for use by busdma implementations which do not benefit
275  * from optimized procedure to load an array of vm_page_t.  Falls back
276  * to do _bus_dmamap_load_phys() in loop.
277  */
278 int bus_dmamap_load_ma_triv(bus_dma_tag_t dmat, bus_dmamap_t map,
279     struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
280     bus_dma_segment_t *segs, int *segp);
281
282 #ifdef WANT_INLINE_DMAMAP
283 #define BUS_DMAMAP_OP static inline
284 #else
285 #define BUS_DMAMAP_OP
286 #endif
287
288 /*
289  * Allocate a handle for mapping from kva/uva/physical
290  * address space into bus device space.
291  */
292 BUS_DMAMAP_OP int bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
293
294 /*
295  * Destroy a handle for mapping from kva/uva/physical
296  * address space into bus device space.
297  */
298 BUS_DMAMAP_OP int bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
299
300 /*
301  * Allocate a piece of memory that can be efficiently mapped into
302  * bus device space based on the constraints listed in the dma tag.
303  * A dmamap to for use with dmamap_load is also allocated.
304  */
305 BUS_DMAMAP_OP int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
306                      bus_dmamap_t *mapp);
307
308 /*
309  * Free a piece of memory and its allocated dmamap, that was allocated
310  * via bus_dmamem_alloc.
311  */
312 BUS_DMAMAP_OP void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
313
314 /*
315  * Perform a synchronization operation on the given map. If the map
316  * is NULL we have a fully IO-coherent system.
317  */
318 BUS_DMAMAP_OP void bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t dmamap, bus_dmasync_op_t op);
319
320 /*
321  * Release the mapping held by map.
322  */
323 BUS_DMAMAP_OP void bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t dmamap);
324
325 #undef BUS_DMAMAP_OP
326 #endif /* _KERNEL */
327
328 #endif /* _BUS_DMA_H_ */