2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
3 * Copyright (c) 2013, 2015 The FreeBSD Foundation
6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7 * under sponsorship from the FreeBSD Foundation.
9 * Portions of this software were developed by Semihalf
10 * under sponsorship of the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/memdesc.h>
42 #include <sys/mutex.h>
45 #include <vm/vm_extern.h>
46 #include <vm/vm_phys.h>
49 #include <machine/bus.h>
50 #include <arm64/include/bus_dma_impl.h>
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)
60 struct bus_dma_tag_common *common;
62 KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
63 /* Return a NULL tag on failure */
65 /* Basic sanity checking */
66 if (boundary != 0 && boundary < maxsegsz)
71 newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
73 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
74 __func__, newtag, 0, ENOMEM);
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;
92 common->lockfunc = _busdma_dflt_lock;
93 common->lockfuncarg = NULL;
96 /* Take into account any restrictions imposed by our parent tag */
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,
109 common->domain = parent->domain;
111 common->domain = vm_phys_domain_match(common->domain, 0ul,
118 * Allocate a device specific dma_tag.
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)
127 struct bus_dma_tag_common *tc;
130 /* Filters are no longer supported. */
131 if (filter != NULL || filterarg != NULL)
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);
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);
148 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
150 struct bus_dma_tag_common *common;
152 if (t == NULL || dmat == NULL)
155 common = (struct bus_dma_tag_common *)dmat;
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;
170 bus_dma_tag_destroy(bus_dma_tag_t dmat)
172 struct bus_dma_tag_common *tc;
174 tc = (struct bus_dma_tag_common *)dmat;
175 return (tc->impl->tag_destroy(dmat));
179 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
181 struct bus_dma_tag_common *tc;
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)
189 return (tc->impl->tag_set_domain(dmat));