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