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>
48 #include <machine/bus.h>
49 #include <machine/bus_dma_impl.h>
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)
59 struct bus_dma_tag_common *common;
61 KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
62 /* Return a NULL tag on failure */
64 /* Basic sanity checking */
65 if (boundary != 0 && boundary < maxsegsz)
70 newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
72 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
73 __func__, newtag, 0, ENOMEM);
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;
91 common->lockfunc = _busdma_dflt_lock;
92 common->lockfuncarg = NULL;
95 /* Take into account any restrictions imposed by our parent tag */
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,
112 * Allocate a device specific dma_tag.
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)
121 struct bus_dma_tag_common *tc;
124 /* Filters are no longer supported. */
125 if (filter != NULL || filterarg != NULL)
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);
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);
142 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
144 struct bus_dma_tag_common *common;
146 if (t == NULL || dmat == NULL)
149 common = (struct bus_dma_tag_common *)dmat;
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;
164 bus_dma_tag_destroy(bus_dma_tag_t dmat)
166 struct bus_dma_tag_common *tc;
168 tc = (struct bus_dma_tag_common *)dmat;
169 return (tc->impl->tag_destroy(dmat));
173 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)