1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1997, 1998 Justin T. Gibbs.
5 * Copyright (c) 2013 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
9 * under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions, and the following disclaimer,
16 *    without modification, immediately at the beginning of the file.
17 * 2. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34#include "opt_acpi.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/malloc.h>
39#include <sys/bus.h>
40#include <sys/kernel.h>
41#include <sys/ktr.h>
42#include <sys/lock.h>
43#include <sys/memdesc.h>
44#include <sys/mutex.h>
45#include <sys/uio.h>
46#include <sys/vmmeter.h>
47#include <vm/vm.h>
48#include <vm/vm_extern.h>
49#include <vm/vm_param.h>
50#include <vm/vm_page.h>
51#include <vm/vm_phys.h>
52#include <vm/pmap.h>
53#include <machine/bus.h>
54#include <x86/include/busdma_impl.h>
55
56int
57common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
58    bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
59    bus_addr_t highaddr, bus_size_t maxsize, int nsegments, bus_size_t maxsegsz,
60    int flags, bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz,
61    void **dmat)
62{
63	void *newtag;
64	struct bus_dma_tag_common *common;
65
66	KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
67	/* Basic sanity checking */
68	if (boundary != 0 && boundary < maxsegsz)
69		maxsegsz = boundary;
70	if (maxsegsz == 0)
71		return (EINVAL);
72	/* Return a NULL tag on failure */
73	*dmat = NULL;
74
75	newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
76	if (newtag == NULL) {
77		CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
78		    __func__, newtag, 0, ENOMEM);
79		return (ENOMEM);
80	}
81
82	common = newtag;
83	common->impl = &bus_dma_bounce_impl;
84	common->alignment = alignment;
85	common->boundary = boundary;
86	common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
87	common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
88	common->maxsize = maxsize;
89	common->nsegments = nsegments;
90	common->maxsegsz = maxsegsz;
91	common->flags = flags;
92	if (lockfunc != NULL) {
93		common->lockfunc = lockfunc;
94		common->lockfuncarg = lockfuncarg;
95	} else {
96		common->lockfunc = _busdma_dflt_lock;
97		common->lockfuncarg = NULL;
98	}
99
100	/* Take into account any restrictions imposed by our parent tag */
101	if (parent != NULL) {
102		common->impl = parent->impl;
103		common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
104		common->highaddr = MAX(parent->highaddr, common->highaddr);
105		if (common->boundary == 0)
106			common->boundary = parent->boundary;
107		else if (parent->boundary != 0) {
108			common->boundary = MIN(parent->boundary,
109			    common->boundary);
110		}
111
112		common->domain = parent->domain;
113	}
114	common->domain = vm_phys_domain_match(common->domain, 0ul,
115	    common->lowaddr);
116	*dmat = common;
117	return (0);
118}
119
120int
121bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
122{
123	struct bus_dma_tag_common *tc;
124
125	tc = (struct bus_dma_tag_common *)dmat;
126	domain = vm_phys_domain_match(domain, 0ul, tc->lowaddr);
127	/* Only call the callback if it changes. */
128	if (domain == tc->domain)
129		return (0);
130	tc->domain = domain;
131	return (tc->impl->tag_set_domain(dmat));
132}
133
134/*
135 * Allocate a device specific dma_tag.
136 */
137int
138bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
139    bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
140    bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
141    int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
142    void *lockfuncarg, bus_dma_tag_t *dmat)
143{
144	struct bus_dma_tag_common *tc;
145	int error;
146
147	/* Filters are no longer supported. */
148	if (filter != NULL || filterarg != NULL)
149		return (EINVAL);
150
151	if (parent == NULL) {
152		error = bus_dma_bounce_impl.tag_create(parent, alignment,
153		    boundary, lowaddr, highaddr, maxsize, nsegments, maxsegsz,
154		    flags, lockfunc, lockfuncarg, dmat);
155	} else {
156		tc = (struct bus_dma_tag_common *)parent;
157		error = tc->impl->tag_create(parent, alignment,
158		    boundary, lowaddr, highaddr, maxsize, nsegments, maxsegsz,
159		    flags, lockfunc, lockfuncarg, dmat);
160	}
161	return (error);
162}
163
164void
165bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
166{
167	struct bus_dma_tag_common *common;
168
169	if (t == NULL || dmat == NULL)
170		return;
171
172	common = (struct bus_dma_tag_common *)dmat;
173
174	t->alignment = common->alignment;
175	t->boundary = common->boundary;
176	t->lowaddr = common->lowaddr;
177	t->highaddr = common->highaddr;
178	t->maxsize = common->maxsize;
179	t->nsegments = common->nsegments;
180	t->maxsegsize = common->maxsegsz;
181	t->flags = common->flags;
182	t->lockfunc = common->lockfunc;
183	t->lockfuncarg = common->lockfuncarg;
184}
185
186int
187bus_dma_tag_destroy(bus_dma_tag_t dmat)
188{
189	struct bus_dma_tag_common *tc;
190
191	tc = (struct bus_dma_tag_common *)dmat;
192	return (tc->impl->tag_destroy(dmat));
193}
194