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