1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 2003
5 * 	Hidetoshi Shimokawa. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *
18 *	This product includes software developed by Hidetoshi Shimokawa.
19 *
20 * 4. Neither the name of the author nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 */
37
38#ifdef __FreeBSD__
39#include <sys/cdefs.h>
40#endif
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/types.h>
45#include <sys/kernel.h>
46#include <sys/malloc.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49
50#include <sys/bus.h>
51#include <machine/bus.h>
52
53#include <dev/firewire/firewire.h>
54#include <dev/firewire/firewirereg.h>
55#include <dev/firewire/fwdma.h>
56
57static void
58fwdma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
59{
60	bus_addr_t *baddr;
61
62	if (error)
63		printf("fwdma_map_cb: error=%d\n", error);
64	baddr = (bus_addr_t *)arg;
65	*baddr = segs->ds_addr;
66}
67
68void *
69fwdma_malloc(struct firewire_comm *fc, int alignment, bus_size_t size,
70	struct fwdma_alloc *dma, int flag)
71{
72	int err;
73
74	dma->v_addr = NULL;
75	err = bus_dma_tag_create(
76		/*parent*/ fc->dmat,
77		/*alignment*/ alignment,
78		/*boundary*/ 0,
79		/*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
80		/*highaddr*/ BUS_SPACE_MAXADDR,
81		/*filter*/NULL, /*filterarg*/NULL,
82		/*maxsize*/ size,
83		/*nsegments*/ 1,
84		/*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
85		/*flags*/ BUS_DMA_ALLOCNOW,
86		/*lockfunc*/busdma_lock_mutex,
87		/*lockarg*/FW_GMTX(fc),
88		&dma->dma_tag);
89	if (err) {
90		printf("fwdma_malloc: failed(1)\n");
91		return (NULL);
92	}
93
94	err = bus_dmamem_alloc(dma->dma_tag, &dma->v_addr,
95		flag, &dma->dma_map);
96	if (err) {
97		printf("fwdma_malloc: failed(2)\n");
98		/* XXX destroy tag */
99		return (NULL);
100	}
101
102	bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->v_addr,
103		size, fwdma_map_cb, &dma->bus_addr, /*flags*/0);
104
105	return (dma->v_addr);
106}
107
108void
109fwdma_free(struct firewire_comm *fc, struct fwdma_alloc *dma)
110{
111        bus_dmamap_unload(dma->dma_tag, dma->dma_map);
112	bus_dmamem_free(dma->dma_tag, dma->v_addr, dma->dma_map);
113	bus_dma_tag_destroy(dma->dma_tag);
114}
115
116
117void *
118fwdma_malloc_size(bus_dma_tag_t dmat, bus_dmamap_t *dmamap,
119	bus_size_t size, bus_addr_t *bus_addr, int flag)
120{
121	void *v_addr;
122
123	if (bus_dmamem_alloc(dmat, &v_addr, flag, dmamap)) {
124		printf("fwdma_malloc_size: failed(1)\n");
125		return (NULL);
126	}
127	bus_dmamap_load(dmat, *dmamap, v_addr, size,
128			fwdma_map_cb, bus_addr, /*flags*/0);
129	return (v_addr);
130}
131
132void
133fwdma_free_size(bus_dma_tag_t dmat, bus_dmamap_t dmamap,
134		void *vaddr, bus_size_t size)
135{
136	bus_dmamap_unload(dmat, dmamap);
137	bus_dmamem_free(dmat, vaddr, dmamap);
138}
139
140/*
141 * Allocate multisegment dma buffers
142 * each segment size is eqaul to ssize except last segment.
143 */
144struct fwdma_alloc_multi *
145fwdma_malloc_multiseg(struct firewire_comm *fc, int alignment,
146		int esize, int n, int flag)
147{
148	struct fwdma_alloc_multi *am;
149	struct fwdma_seg *seg;
150	bus_size_t ssize;
151	int nseg;
152
153	if (esize > PAGE_SIZE) {
154		/* round up to PAGE_SIZE */
155		esize = ssize = roundup2(esize, PAGE_SIZE);
156		nseg = n;
157	} else {
158		/* allocate PAGE_SIZE segment for small elements */
159		ssize = rounddown(PAGE_SIZE, esize);
160		nseg = howmany(n, ssize / esize);
161	}
162	am = (struct fwdma_alloc_multi *)malloc(sizeof(struct fwdma_alloc_multi)
163			+ sizeof(struct fwdma_seg)*nseg, M_FW, M_WAITOK);
164	am->ssize = ssize;
165	am->esize = esize;
166	am->nseg = 0;
167	if (bus_dma_tag_create(
168			/*parent*/ fc->dmat,
169			/*alignment*/ alignment,
170			/*boundary*/ 0,
171			/*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
172			/*highaddr*/ BUS_SPACE_MAXADDR,
173			/*filter*/NULL, /*filterarg*/NULL,
174			/*maxsize*/ ssize,
175			/*nsegments*/ 1,
176			/*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
177			/*flags*/ BUS_DMA_ALLOCNOW,
178			/*lockfunc*/busdma_lock_mutex,
179			/*lockarg*/FW_GMTX(fc),
180			&am->dma_tag)) {
181		printf("fwdma_malloc_multiseg: tag_create failed\n");
182		free(am, M_FW);
183		return (NULL);
184	}
185
186	for (seg = &am->seg[0]; nseg--; seg++) {
187		seg->v_addr = fwdma_malloc_size(am->dma_tag, &seg->dma_map,
188			ssize, &seg->bus_addr, flag);
189		if (seg->v_addr == NULL) {
190			printf("fwdma_malloc_multi: malloc_size failed %d\n",
191				am->nseg);
192			fwdma_free_multiseg(am);
193			return (NULL);
194		}
195		am->nseg++;
196	}
197	return (am);
198}
199
200void
201fwdma_free_multiseg(struct fwdma_alloc_multi *am)
202{
203	struct fwdma_seg *seg;
204
205	for (seg = &am->seg[0]; am->nseg--; seg++) {
206		fwdma_free_size(am->dma_tag, seg->dma_map,
207			seg->v_addr, am->ssize);
208	}
209	bus_dma_tag_destroy(am->dma_tag);
210	free(am, M_FW);
211}
212