]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/busdma_machdep.c
OpenSSL: update to 3.0.12
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / busdma_machdep.c
1 /*-
2  * Copyright (c) 1997, 1998 Justin T. Gibbs.
3  * Copyright (c) 2013, 2015 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Portions of this software were developed by Semihalf
10  * under sponsorship of the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification, immediately at the beginning of the file.
18  * 2. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/ktr.h>
41 #include <sys/lock.h>
42 #include <sys/memdesc.h>
43 #include <sys/mutex.h>
44 #include <sys/uio.h>
45 #include <vm/vm.h>
46 #include <vm/vm_extern.h>
47 #include <vm/vm_phys.h>
48 #include <vm/pmap.h>
49
50 #include <machine/bus.h>
51 #include <arm64/include/bus_dma_impl.h>
52
53 /*
54  * Return true if a match is made.
55  *
56  * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
57  *
58  * If paddr is within the bounds of the dma tag then call the filter callback
59  * to check for a match, if there is no filter callback then assume a match.
60  */
61 int
62 bus_dma_run_filter(struct bus_dma_tag_common *tc, bus_addr_t paddr)
63 {
64
65         while (tc != NULL) {
66                 if ((paddr > tc->lowaddr && paddr <= tc->highaddr) &&
67                     (tc->filter == NULL ||
68                     (*tc->filter)(tc->filterarg, paddr) != 0))
69                         return (1);
70
71                 tc = tc->parent;                
72         }
73
74         return (0);
75 }
76
77 int
78 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
79     bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
80     bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
81     bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
82     bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
83 {
84         void *newtag;
85         struct bus_dma_tag_common *common;
86
87         KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
88         /* Return a NULL tag on failure */
89         *dmat = NULL;
90         /* Basic sanity checking */
91         if (boundary != 0 && boundary < maxsegsz)
92                 maxsegsz = boundary;
93         if (maxsegsz == 0)
94                 return (EINVAL);
95
96         newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
97         if (newtag == NULL) {
98                 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
99                     __func__, newtag, 0, ENOMEM);
100                 return (ENOMEM);
101         }
102
103         common = newtag;
104         common->impl = &bus_dma_bounce_impl;
105         common->parent = parent;
106         common->alignment = alignment;
107         common->boundary = boundary;
108         common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
109         common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
110         common->filter = filter;
111         common->filterarg = filterarg;
112         common->maxsize = maxsize;
113         common->nsegments = nsegments;
114         common->maxsegsz = maxsegsz;
115         common->flags = flags;
116         common->ref_count = 1; /* Count ourself */
117         if (lockfunc != NULL) {
118                 common->lockfunc = lockfunc;
119                 common->lockfuncarg = lockfuncarg;
120         } else {
121                 common->lockfunc = _busdma_dflt_lock;
122                 common->lockfuncarg = NULL;
123         }
124
125         /* Take into account any restrictions imposed by our parent tag */
126         if (parent != NULL) {
127                 common->impl = parent->impl;
128                 common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
129                 common->highaddr = MAX(parent->highaddr, common->highaddr);
130                 common->alignment = MAX(parent->alignment, common->alignment);
131                 if (common->boundary == 0)
132                         common->boundary = parent->boundary;
133                 else if (parent->boundary != 0) {
134                         common->boundary = MIN(parent->boundary,
135                             common->boundary);
136                 }
137                 if (common->filter == NULL) {
138                         /*
139                          * Short circuit looking at our parent directly
140                          * since we have encapsulated all of its information
141                          */
142                         common->filter = parent->filter;
143                         common->filterarg = parent->filterarg;
144                         common->parent = parent->parent;
145                 }
146                 common->domain = parent->domain;
147                 atomic_add_int(&parent->ref_count, 1);
148         }
149         common->domain = vm_phys_domain_match(common->domain, 0ul,
150             common->lowaddr);
151         *dmat = common;
152         return (0);
153 }
154
155 /*
156  * Allocate a device specific dma_tag.
157  */
158 int
159 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
160     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
161     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
162     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
163     void *lockfuncarg, bus_dma_tag_t *dmat)
164 {
165         struct bus_dma_tag_common *tc;
166         int error;
167
168         if (parent == NULL) {
169                 error = bus_dma_bounce_impl.tag_create(parent, alignment,
170                     boundary, lowaddr, highaddr, filter, filterarg, maxsize,
171                     nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
172         } else {
173                 tc = (struct bus_dma_tag_common *)parent;
174                 error = tc->impl->tag_create(parent, alignment,
175                     boundary, lowaddr, highaddr, filter, filterarg, maxsize,
176                     nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
177         }
178         return (error);
179 }
180
181 void
182 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
183 {
184         struct bus_dma_tag_common *common;
185
186         if (t == NULL || dmat == NULL)
187                 return;
188
189         common = (struct bus_dma_tag_common *)dmat;
190
191         t->parent = (bus_dma_tag_t)common->parent;
192         t->alignment = common->alignment;
193         t->boundary = common->boundary;
194         t->lowaddr = common->lowaddr;
195         t->highaddr = common->highaddr;
196         t->maxsize = common->maxsize;
197         t->nsegments = common->nsegments;
198         t->maxsegsize = common->maxsegsz;
199         t->flags = common->flags;
200         t->lockfunc = common->lockfunc;
201         t->lockfuncarg = common->lockfuncarg;
202 }
203
204 int
205 bus_dma_tag_destroy(bus_dma_tag_t dmat)
206 {
207         struct bus_dma_tag_common *tc;
208
209         tc = (struct bus_dma_tag_common *)dmat;
210         return (tc->impl->tag_destroy(dmat));
211 }
212
213 int
214 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
215 {
216         struct bus_dma_tag_common *tc;
217
218         tc = (struct bus_dma_tag_common *)dmat;
219         domain = vm_phys_domain_match(domain, 0ul, tc->lowaddr);
220         /* Only call the callback if it changes. */
221         if (domain == tc->domain)
222                 return (0);
223         tc->domain = domain;
224         return (tc->impl->tag_set_domain(dmat));
225 }