]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/busdma_machdep.c
Import tzdata 2020b
[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 "opt_acpi.h"
37 #include "opt_iommu.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/bus.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/lock.h>
46 #include <sys/memdesc.h>
47 #include <sys/mutex.h>
48 #include <sys/uio.h>
49 #include <sys/vmmeter.h>
50 #include <vm/vm.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_phys.h>
55 #include <vm/pmap.h>
56 #include <machine/bus.h>
57 #include <x86/include/busdma_impl.h>
58
59 /*
60  * Convenience function for manipulating driver locks from busdma (during
61  * busdma_swi, for example).  Drivers that don't provide their own locks
62  * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
63  * non-mutex locking scheme don't have to use this at all.
64  */
65 void
66 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
67 {
68         struct mtx *dmtx;
69
70         dmtx = (struct mtx *)arg;
71         switch (op) {
72         case BUS_DMA_LOCK:
73                 mtx_lock(dmtx);
74                 break;
75         case BUS_DMA_UNLOCK:
76                 mtx_unlock(dmtx);
77                 break;
78         default:
79                 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
80         }
81 }
82
83 /*
84  * dflt_lock should never get called.  It gets put into the dma tag when
85  * lockfunc == NULL, which is only valid if the maps that are associated
86  * with the tag are meant to never be defered.
87  * XXX Should have a way to identify which driver is responsible here.
88  */
89 void
90 bus_dma_dflt_lock(void *arg, bus_dma_lock_op_t op)
91 {
92
93         panic("driver error: busdma dflt_lock called");
94 }
95
96 /*
97  * Return true if a match is made.
98  *
99  * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
100  *
101  * If paddr is within the bounds of the dma tag then call the filter callback
102  * to check for a match, if there is no filter callback then assume a match.
103  */
104 int
105 bus_dma_run_filter(struct bus_dma_tag_common *tc, vm_paddr_t paddr)
106 {
107         int retval;
108
109         retval = 0;
110         do {
111                 if ((paddr >= BUS_SPACE_MAXADDR ||
112                     (paddr > tc->lowaddr && paddr <= tc->highaddr) ||
113                     (paddr & (tc->alignment - 1)) != 0) &&
114                     (tc->filter == NULL ||
115                     (*tc->filter)(tc->filterarg, paddr) != 0))
116                         retval = 1;
117
118                 tc = tc->parent;                
119         } while (retval == 0 && tc != NULL);
120         return (retval);
121 }
122
123 int
124 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
125     bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
126     bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
127     bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
128     bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
129 {
130         void *newtag;
131         struct bus_dma_tag_common *common;
132
133         KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
134         /* Basic sanity checking */
135         if (boundary != 0 && boundary < maxsegsz)
136                 maxsegsz = boundary;
137         if (maxsegsz == 0)
138                 return (EINVAL);
139         /* Return a NULL tag on failure */
140         *dmat = NULL;
141
142         newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
143         if (newtag == NULL) {
144                 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
145                     __func__, newtag, 0, ENOMEM);
146                 return (ENOMEM);
147         }
148
149         common = newtag;
150         common->impl = &bus_dma_bounce_impl;
151         common->parent = parent;
152         common->alignment = alignment;
153         common->boundary = boundary;
154         common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
155         common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
156         common->filter = filter;
157         common->filterarg = filterarg;
158         common->maxsize = maxsize;
159         common->nsegments = nsegments;
160         common->maxsegsz = maxsegsz;
161         common->flags = flags;
162         common->ref_count = 1; /* Count ourself */
163         if (lockfunc != NULL) {
164                 common->lockfunc = lockfunc;
165                 common->lockfuncarg = lockfuncarg;
166         } else {
167                 common->lockfunc = bus_dma_dflt_lock;
168                 common->lockfuncarg = NULL;
169         }
170
171         /* Take into account any restrictions imposed by our parent tag */
172         if (parent != NULL) {
173                 common->impl = parent->impl;
174                 common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
175                 common->highaddr = MAX(parent->highaddr, common->highaddr);
176                 if (common->boundary == 0)
177                         common->boundary = parent->boundary;
178                 else if (parent->boundary != 0) {
179                         common->boundary = MIN(parent->boundary,
180                             common->boundary);
181                 }
182                 if (common->filter == NULL) {
183                         /*
184                          * Short circuit looking at our parent directly
185                          * since we have encapsulated all of its information
186                          */
187                         common->filter = parent->filter;
188                         common->filterarg = parent->filterarg;
189                         common->parent = parent->parent;
190                 }
191                 common->domain = parent->domain;
192                 atomic_add_int(&parent->ref_count, 1);
193         }
194         common->domain = vm_phys_domain_match(common->domain, 0ul,
195             common->lowaddr);
196         *dmat = common;
197         return (0);
198 }
199
200 int
201 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
202 {
203         struct bus_dma_tag_common *tc;
204
205         tc = (struct bus_dma_tag_common *)dmat;
206         domain = vm_phys_domain_match(domain, 0ul, tc->lowaddr);
207         /* Only call the callback if it changes. */
208         if (domain == tc->domain)
209                 return (0);
210         tc->domain = domain;
211         return (tc->impl->tag_set_domain(dmat));
212 }
213
214 /*
215  * Allocate a device specific dma_tag.
216  */
217 int
218 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
219     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
220     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
221     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
222     void *lockfuncarg, bus_dma_tag_t *dmat)
223 {
224         struct bus_dma_tag_common *tc;
225         int error;
226
227         if (parent == NULL) {
228                 error = bus_dma_bounce_impl.tag_create(parent, alignment,
229                     boundary, lowaddr, highaddr, filter, filterarg, maxsize,
230                     nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
231         } else {
232                 tc = (struct bus_dma_tag_common *)parent;
233                 error = tc->impl->tag_create(parent, alignment,
234                     boundary, lowaddr, highaddr, filter, filterarg, maxsize,
235                     nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
236         }
237         return (error);
238 }
239
240 void
241 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
242 {
243         struct bus_dma_tag_common *common;
244
245         if (t == NULL || dmat == NULL)
246                 return;
247
248         common = (struct bus_dma_tag_common *)dmat;
249
250         t->parent = (bus_dma_tag_t)common->parent;
251         t->alignment = common->alignment;
252         t->boundary = common->boundary;
253         t->lowaddr = common->lowaddr;
254         t->highaddr = common->highaddr;
255         t->maxsize = common->maxsize;
256         t->nsegments = common->nsegments;
257         t->maxsegsize = common->maxsegsz;
258         t->flags = common->flags;
259         t->lockfunc = common->lockfunc;
260         t->lockfuncarg = common->lockfuncarg;
261 }
262
263 int
264 bus_dma_tag_destroy(bus_dma_tag_t dmat)
265 {
266         struct bus_dma_tag_common *tc;
267
268         tc = (struct bus_dma_tag_common *)dmat;
269         return (tc->impl->tag_destroy(dmat));
270 }
271
272 #ifndef IOMMU
273 bool bus_dma_iommu_set_buswide(device_t dev);
274 int bus_dma_iommu_load_ident(bus_dma_tag_t dmat, bus_dmamap_t map,   
275     vm_paddr_t start, vm_size_t length, int flags);
276
277 bool
278 bus_dma_iommu_set_buswide(device_t dev)
279 {
280         return (false);
281 }
282
283 int
284 bus_dma_iommu_load_ident(bus_dma_tag_t dmat, bus_dmamap_t map,
285     vm_paddr_t start, vm_size_t length, int flags)
286 {
287         return (0);
288 }
289 #endif