]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/busdma_machdep.c
Merge clang trunk r321017 to contrib/llvm/tools/clang.
[FreeBSD/FreeBSD.git] / sys / x86 / x86 / busdma_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1997, 1998 Justin T. Gibbs.
5  * Copyright (c) 2013 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
9  * under sponsorship from the FreeBSD Foundation.
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  *    without modification, immediately at the beginning of the file.
17  * 2. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/lock.h>
43 #include <sys/memdesc.h>
44 #include <sys/mutex.h>
45 #include <sys/uio.h>
46 #include <vm/vm.h>
47 #include <vm/vm_extern.h>
48 #include <vm/pmap.h>
49 #include <machine/bus.h>
50 #include <x86/include/busdma_impl.h>
51
52 /*
53  * Convenience function for manipulating driver locks from busdma (during
54  * busdma_swi, for example).  Drivers that don't provide their own locks
55  * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
56  * non-mutex locking scheme don't have to use this at all.
57  */
58 void
59 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
60 {
61         struct mtx *dmtx;
62
63         dmtx = (struct mtx *)arg;
64         switch (op) {
65         case BUS_DMA_LOCK:
66                 mtx_lock(dmtx);
67                 break;
68         case BUS_DMA_UNLOCK:
69                 mtx_unlock(dmtx);
70                 break;
71         default:
72                 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
73         }
74 }
75
76 /*
77  * dflt_lock should never get called.  It gets put into the dma tag when
78  * lockfunc == NULL, which is only valid if the maps that are associated
79  * with the tag are meant to never be defered.
80  * XXX Should have a way to identify which driver is responsible here.
81  */
82 void
83 bus_dma_dflt_lock(void *arg, bus_dma_lock_op_t op)
84 {
85
86         panic("driver error: busdma dflt_lock called");
87 }
88
89 /*
90  * Return true if a match is made.
91  *
92  * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
93  *
94  * If paddr is within the bounds of the dma tag then call the filter callback
95  * to check for a match, if there is no filter callback then assume a match.
96  */
97 int
98 bus_dma_run_filter(struct bus_dma_tag_common *tc, bus_addr_t paddr)
99 {
100         int retval;
101
102         retval = 0;
103         do {
104                 if (((paddr > tc->lowaddr && paddr <= tc->highaddr) ||
105                     ((paddr & (tc->alignment - 1)) != 0)) &&
106                     (tc->filter == NULL ||
107                     (*tc->filter)(tc->filterarg, paddr) != 0))
108                         retval = 1;
109
110                 tc = tc->parent;                
111         } while (retval == 0 && tc != NULL);
112         return (retval);
113 }
114
115 int
116 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
117     bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
118     bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
119     bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
120     bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
121 {
122         void *newtag;
123         struct bus_dma_tag_common *common;
124
125         KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
126         /* Basic sanity checking */
127         if (boundary != 0 && boundary < maxsegsz)
128                 maxsegsz = boundary;
129         if (maxsegsz == 0)
130                 return (EINVAL);
131         /* Return a NULL tag on failure */
132         *dmat = NULL;
133
134         newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
135         if (newtag == NULL) {
136                 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
137                     __func__, newtag, 0, ENOMEM);
138                 return (ENOMEM);
139         }
140
141         common = newtag;
142         common->impl = &bus_dma_bounce_impl;
143         common->parent = parent;
144         common->alignment = alignment;
145         common->boundary = boundary;
146         common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
147         common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
148         common->filter = filter;
149         common->filterarg = filterarg;
150         common->maxsize = maxsize;
151         common->nsegments = nsegments;
152         common->maxsegsz = maxsegsz;
153         common->flags = flags;
154         common->ref_count = 1; /* Count ourself */
155         if (lockfunc != NULL) {
156                 common->lockfunc = lockfunc;
157                 common->lockfuncarg = lockfuncarg;
158         } else {
159                 common->lockfunc = bus_dma_dflt_lock;
160                 common->lockfuncarg = NULL;
161         }
162
163         /* Take into account any restrictions imposed by our parent tag */
164         if (parent != NULL) {
165                 common->impl = parent->impl;
166                 common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
167                 common->highaddr = MAX(parent->highaddr, common->highaddr);
168                 if (common->boundary == 0)
169                         common->boundary = parent->boundary;
170                 else if (parent->boundary != 0) {
171                         common->boundary = MIN(parent->boundary,
172                             common->boundary);
173                 }
174                 if (common->filter == NULL) {
175                         /*
176                          * Short circuit looking at our parent directly
177                          * since we have encapsulated all of its information
178                          */
179                         common->filter = parent->filter;
180                         common->filterarg = parent->filterarg;
181                         common->parent = parent->parent;
182                 }
183                 atomic_add_int(&parent->ref_count, 1);
184         }
185         *dmat = common;
186         return (0);
187 }
188
189 /*
190  * Allocate a device specific dma_tag.
191  */
192 int
193 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
194     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
195     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
196     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
197     void *lockfuncarg, bus_dma_tag_t *dmat)
198 {
199         struct bus_dma_tag_common *tc;
200         int error;
201
202         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s", __func__);
203
204         if (parent == NULL) {
205                 error = bus_dma_bounce_impl.tag_create(parent, alignment,
206                     boundary, lowaddr, highaddr, filter, filterarg, maxsize,
207                     nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
208         } else {
209                 tc = (struct bus_dma_tag_common *)parent;
210                 error = tc->impl->tag_create(parent, alignment,
211                     boundary, lowaddr, highaddr, filter, filterarg, maxsize,
212                     nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
213         }
214         return (error);
215 }
216
217 int
218 bus_dma_tag_destroy(bus_dma_tag_t dmat)
219 {
220         struct bus_dma_tag_common *tc;
221
222         tc = (struct bus_dma_tag_common *)dmat;
223         return (tc->impl->tag_destroy(dmat));
224 }
225