xref: /freebsd/sys/riscv/riscv/busdma_machdep.c (revision 1323ec57)
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 <machine/bus_dma_impl.h>
53 
54 /*
55  * Return true if a match is made.
56  *
57  * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
58  *
59  * If paddr is within the bounds of the dma tag then call the filter callback
60  * to check for a match, if there is no filter callback then assume a match.
61  */
62 int
63 bus_dma_run_filter(struct bus_dma_tag_common *tc, bus_addr_t paddr)
64 {
65 	int retval;
66 
67 	retval = 0;
68 	do {
69 		if (((paddr > tc->lowaddr && paddr <= tc->highaddr) ||
70 		    !vm_addr_align_ok(paddr, tc->alignment)) &&
71 		    (tc->filter == NULL ||
72 		    (*tc->filter)(tc->filterarg, paddr) != 0))
73 			retval = 1;
74 
75 		tc = tc->parent;
76 	} while (retval == 0 && tc != NULL);
77 	return (retval);
78 }
79 
80 int
81 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
82     bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
83     bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
84     bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
85     bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
86 {
87 	void *newtag;
88 	struct bus_dma_tag_common *common;
89 
90 	KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
91 	/* Return a NULL tag on failure */
92 	*dmat = NULL;
93 	/* Basic sanity checking */
94 	if (boundary != 0 && boundary < maxsegsz)
95 		maxsegsz = boundary;
96 	if (maxsegsz == 0)
97 		return (EINVAL);
98 
99 	newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
100 	if (newtag == NULL) {
101 		CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
102 		    __func__, newtag, 0, ENOMEM);
103 		return (ENOMEM);
104 	}
105 
106 	common = newtag;
107 	common->impl = &bus_dma_bounce_impl;
108 	common->parent = parent;
109 	common->alignment = alignment;
110 	common->boundary = boundary;
111 	common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
112 	common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
113 	common->filter = filter;
114 	common->filterarg = filterarg;
115 	common->maxsize = maxsize;
116 	common->nsegments = nsegments;
117 	common->maxsegsz = maxsegsz;
118 	common->flags = flags;
119 	common->ref_count = 1; /* Count ourself */
120 	if (lockfunc != NULL) {
121 		common->lockfunc = lockfunc;
122 		common->lockfuncarg = lockfuncarg;
123 	} else {
124 		common->lockfunc = _busdma_dflt_lock;
125 		common->lockfuncarg = NULL;
126 	}
127 
128 	/* Take into account any restrictions imposed by our parent tag */
129 	if (parent != NULL) {
130 		common->impl = parent->impl;
131 		common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
132 		common->highaddr = MAX(parent->highaddr, common->highaddr);
133 		if (common->boundary == 0)
134 			common->boundary = parent->boundary;
135 		else if (parent->boundary != 0) {
136 			common->boundary = MIN(parent->boundary,
137 			    common->boundary);
138 		}
139 		if (common->filter == NULL) {
140 			/*
141 			 * Short circuit looking at our parent directly
142 			 * since we have encapsulated all of its information
143 			 */
144 			common->filter = parent->filter;
145 			common->filterarg = parent->filterarg;
146 			common->parent = parent->parent;
147 		}
148 		atomic_add_int(&parent->ref_count, 1);
149 	}
150 	*dmat = common;
151 	return (0);
152 }
153 
154 /*
155  * Allocate a device specific dma_tag.
156  */
157 int
158 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
159     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
160     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
161     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
162     void *lockfuncarg, bus_dma_tag_t *dmat)
163 {
164 	struct bus_dma_tag_common *tc;
165 	int error;
166 
167 	if (parent == NULL) {
168 		error = bus_dma_bounce_impl.tag_create(parent, alignment,
169 		    boundary, lowaddr, highaddr, filter, filterarg, maxsize,
170 		    nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
171 	} else {
172 		tc = (struct bus_dma_tag_common *)parent;
173 		error = tc->impl->tag_create(parent, alignment,
174 		    boundary, lowaddr, highaddr, filter, filterarg, maxsize,
175 		    nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
176 	}
177 	return (error);
178 }
179 
180 void
181 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
182 {
183 	struct bus_dma_tag_common *common;
184 
185 	if (t == NULL || dmat == NULL)
186 		return;
187 
188 	common = (struct bus_dma_tag_common *)dmat;
189 
190 	t->parent = (bus_dma_tag_t)common->parent;
191 	t->alignment = common->alignment;
192 	t->boundary = common->boundary;
193 	t->lowaddr = common->lowaddr;
194 	t->highaddr = common->highaddr;
195 	t->maxsize = common->maxsize;
196 	t->nsegments = common->nsegments;
197 	t->maxsegsize = common->maxsegsz;
198 	t->flags = common->flags;
199 	t->lockfunc = common->lockfunc;
200 	t->lockfuncarg = common->lockfuncarg;
201 }
202 
203 int
204 bus_dma_tag_destroy(bus_dma_tag_t dmat)
205 {
206 	struct bus_dma_tag_common *tc;
207 
208 	tc = (struct bus_dma_tag_common *)dmat;
209 	return (tc->impl->tag_destroy(dmat));
210 }
211 
212 int
213 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
214 {
215 
216 	return (0);
217 }
218