xref: /freebsd/sys/riscv/riscv/busdma_machdep.c (revision 069ac184)
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/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/ktr.h>
40 #include <sys/lock.h>
41 #include <sys/memdesc.h>
42 #include <sys/mutex.h>
43 #include <sys/uio.h>
44 #include <vm/vm.h>
45 #include <vm/vm_extern.h>
46 #include <vm/pmap.h>
47 
48 #include <machine/bus.h>
49 #include <machine/bus_dma_impl.h>
50 
51 int
52 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
53     bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
54     bus_addr_t highaddr, bus_size_t maxsize, int nsegments,
55     bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
56     void *lockfuncarg, size_t sz, void **dmat)
57 {
58 	void *newtag;
59 	struct bus_dma_tag_common *common;
60 
61 	KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
62 	/* Return a NULL tag on failure */
63 	*dmat = NULL;
64 	/* Basic sanity checking */
65 	if (boundary != 0 && boundary < maxsegsz)
66 		maxsegsz = boundary;
67 	if (maxsegsz == 0)
68 		return (EINVAL);
69 
70 	newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
71 	if (newtag == NULL) {
72 		CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
73 		    __func__, newtag, 0, ENOMEM);
74 		return (ENOMEM);
75 	}
76 
77 	common = newtag;
78 	common->impl = &bus_dma_bounce_impl;
79 	common->alignment = alignment;
80 	common->boundary = boundary;
81 	common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
82 	common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
83 	common->maxsize = maxsize;
84 	common->nsegments = nsegments;
85 	common->maxsegsz = maxsegsz;
86 	common->flags = flags;
87 	if (lockfunc != NULL) {
88 		common->lockfunc = lockfunc;
89 		common->lockfuncarg = lockfuncarg;
90 	} else {
91 		common->lockfunc = _busdma_dflt_lock;
92 		common->lockfuncarg = NULL;
93 	}
94 
95 	/* Take into account any restrictions imposed by our parent tag */
96 	if (parent != NULL) {
97 		common->impl = parent->impl;
98 		common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
99 		common->highaddr = MAX(parent->highaddr, common->highaddr);
100 		if (common->boundary == 0)
101 			common->boundary = parent->boundary;
102 		else if (parent->boundary != 0) {
103 			common->boundary = MIN(parent->boundary,
104 			    common->boundary);
105 		}
106 	}
107 	*dmat = common;
108 	return (0);
109 }
110 
111 /*
112  * Allocate a device specific dma_tag.
113  */
114 int
115 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
116     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
117     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
118     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
119     void *lockfuncarg, bus_dma_tag_t *dmat)
120 {
121 	struct bus_dma_tag_common *tc;
122 	int error;
123 
124 	/* Filters are no longer supported. */
125 	if (filter != NULL || filterarg != NULL)
126 		return (EINVAL);
127 
128 	if (parent == NULL) {
129 		error = bus_dma_bounce_impl.tag_create(parent, alignment,
130 		    boundary, lowaddr, highaddr, maxsize, nsegments, maxsegsz,
131 		    flags, lockfunc, lockfuncarg, dmat);
132 	} else {
133 		tc = (struct bus_dma_tag_common *)parent;
134 		error = tc->impl->tag_create(parent, alignment,
135 		    boundary, lowaddr, highaddr, maxsize, nsegments, maxsegsz,
136 		    flags, lockfunc, lockfuncarg, dmat);
137 	}
138 	return (error);
139 }
140 
141 void
142 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
143 {
144 	struct bus_dma_tag_common *common;
145 
146 	if (t == NULL || dmat == NULL)
147 		return;
148 
149 	common = (struct bus_dma_tag_common *)dmat;
150 
151 	t->alignment = common->alignment;
152 	t->boundary = common->boundary;
153 	t->lowaddr = common->lowaddr;
154 	t->highaddr = common->highaddr;
155 	t->maxsize = common->maxsize;
156 	t->nsegments = common->nsegments;
157 	t->maxsegsize = common->maxsegsz;
158 	t->flags = common->flags;
159 	t->lockfunc = common->lockfunc;
160 	t->lockfuncarg = common->lockfuncarg;
161 }
162 
163 int
164 bus_dma_tag_destroy(bus_dma_tag_t dmat)
165 {
166 	struct bus_dma_tag_common *tc;
167 
168 	tc = (struct bus_dma_tag_common *)dmat;
169 	return (tc->impl->tag_destroy(dmat));
170 }
171 
172 int
173 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
174 {
175 
176 	return (0);
177 }
178