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