]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/busdma_machdep.c
arm64: gicv3: setup PPIs on all APs after they're online
[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 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/ktr.h>
43 #include <sys/lock.h>
44 #include <sys/memdesc.h>
45 #include <sys/mutex.h>
46 #include <sys/uio.h>
47 #include <vm/vm.h>
48 #include <vm/vm_extern.h>
49 #include <vm/pmap.h>
50
51 #include <machine/bus.h>
52 #include <arm64/include/bus_dma_impl.h>
53
54 /*
55  * Convenience function for manipulating driver locks from busdma (during
56  * busdma_swi, for example).
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
101         while (tc != NULL) {
102                 if ((paddr > tc->lowaddr && paddr <= tc->highaddr) &&
103                     (tc->filter == NULL ||
104                     (*tc->filter)(tc->filterarg, paddr) != 0))
105                         return (1);
106
107                 tc = tc->parent;                
108         }
109
110         return (0);
111 }
112
113 int
114 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
115     bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
116     bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
117     bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
118     bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
119 {
120         void *newtag;
121         struct bus_dma_tag_common *common;
122
123         KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
124         /* Return a NULL tag on failure */
125         *dmat = NULL;
126         /* Basic sanity checking */
127         if (boundary != 0 && boundary < maxsegsz)
128                 maxsegsz = boundary;
129         if (maxsegsz == 0)
130                 return (EINVAL);
131
132         newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
133         if (newtag == NULL) {
134                 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
135                     __func__, newtag, 0, ENOMEM);
136                 return (ENOMEM);
137         }
138
139         common = newtag;
140         common->impl = &bus_dma_bounce_impl;
141         common->parent = parent;
142         common->alignment = alignment;
143         common->boundary = boundary;
144         common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
145         common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
146         common->filter = filter;
147         common->filterarg = filterarg;
148         common->maxsize = maxsize;
149         common->nsegments = nsegments;
150         common->maxsegsz = maxsegsz;
151         common->flags = flags;
152         common->ref_count = 1; /* Count ourself */
153         if (lockfunc != NULL) {
154                 common->lockfunc = lockfunc;
155                 common->lockfuncarg = lockfuncarg;
156         } else {
157                 common->lockfunc = bus_dma_dflt_lock;
158                 common->lockfuncarg = NULL;
159         }
160
161         /* Take into account any restrictions imposed by our parent tag */
162         if (parent != NULL) {
163                 common->impl = parent->impl;
164                 common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
165                 common->highaddr = MAX(parent->highaddr, common->highaddr);
166                 common->alignment = MAX(parent->alignment, common->alignment);
167                 if (common->boundary == 0)
168                         common->boundary = parent->boundary;
169                 else if (parent->boundary != 0) {
170                         common->boundary = MIN(parent->boundary,
171                             common->boundary);
172                 }
173                 if (common->filter == NULL) {
174                         /*
175                          * Short circuit looking at our parent directly
176                          * since we have encapsulated all of its information
177                          */
178                         common->filter = parent->filter;
179                         common->filterarg = parent->filterarg;
180                         common->parent = parent->parent;
181                 }
182                 atomic_add_int(&parent->ref_count, 1);
183         }
184         *dmat = common;
185         return (0);
186 }
187
188 /*
189  * Allocate a device specific dma_tag.
190  */
191 int
192 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
193     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
194     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
195     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
196     void *lockfuncarg, bus_dma_tag_t *dmat)
197 {
198         struct bus_dma_tag_common *tc;
199         int error;
200
201         if (parent == NULL) {
202                 error = bus_dma_bounce_impl.tag_create(parent, alignment,
203                     boundary, lowaddr, highaddr, filter, filterarg, maxsize,
204                     nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
205         } else {
206                 tc = (struct bus_dma_tag_common *)parent;
207                 error = tc->impl->tag_create(parent, alignment,
208                     boundary, lowaddr, highaddr, filter, filterarg, maxsize,
209                     nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
210         }
211         return (error);
212 }
213
214 void
215 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
216 {
217         struct bus_dma_tag_common *common;
218
219         if (t == NULL || dmat == NULL)
220                 return;
221
222         common = (struct bus_dma_tag_common *)dmat;
223
224         t->parent = (bus_dma_tag_t)common->parent;
225         t->alignment = common->alignment;
226         t->boundary = common->boundary;
227         t->lowaddr = common->lowaddr;
228         t->highaddr = common->highaddr;
229         t->maxsize = common->maxsize;
230         t->nsegments = common->nsegments;
231         t->maxsegsize = common->maxsegsz;
232         t->flags = common->flags;
233         t->lockfunc = common->lockfunc;
234         t->lockfuncarg = common->lockfuncarg;
235 }
236
237 int
238 bus_dma_tag_destroy(bus_dma_tag_t dmat)
239 {
240         struct bus_dma_tag_common *tc;
241
242         tc = (struct bus_dma_tag_common *)dmat;
243         return (tc->impl->tag_destroy(dmat));
244 }
245
246 int
247 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
248 {
249
250         return (0);
251 }