xref: /dragonfly/sys/bus/firewire/fwdma.h (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino  * Copyright (C) 2003
3*86d7f5d3SJohn Marino  * 	Hidetoshi Shimokawa. All rights reserved.
4*86d7f5d3SJohn Marino  *
5*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino  * are met:
8*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino  * 3. All advertising materials mentioning features or use of this software
14*86d7f5d3SJohn Marino  *    must display the following acknowledgement:
15*86d7f5d3SJohn Marino  *
16*86d7f5d3SJohn Marino  *	This product includes software developed by Hidetoshi Shimokawa.
17*86d7f5d3SJohn Marino  *
18*86d7f5d3SJohn Marino  * 4. Neither the name of the author nor the names of its contributors
19*86d7f5d3SJohn Marino  *    may be used to endorse or promote products derived from this software
20*86d7f5d3SJohn Marino  *    without specific prior written permission.
21*86d7f5d3SJohn Marino  *
22*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*86d7f5d3SJohn Marino  * SUCH DAMAGE.
33*86d7f5d3SJohn Marino  *
34*86d7f5d3SJohn Marino  * $FreeBSD: src/sys/dev/firewire/fwdma.h,v 1.2 2003/05/27 16:34:52 scottl Exp $
35*86d7f5d3SJohn Marino  * $DragonFly: src/sys/bus/firewire/fwdma.h,v 1.3 2004/02/05 13:32:08 joerg Exp $
36*86d7f5d3SJohn Marino  */
37*86d7f5d3SJohn Marino 
38*86d7f5d3SJohn Marino struct fwdma_alloc {
39*86d7f5d3SJohn Marino 	bus_dma_tag_t	dma_tag;
40*86d7f5d3SJohn Marino 	bus_dmamap_t	dma_map;
41*86d7f5d3SJohn Marino 	void *		v_addr;
42*86d7f5d3SJohn Marino 	bus_addr_t	bus_addr;
43*86d7f5d3SJohn Marino };
44*86d7f5d3SJohn Marino 
45*86d7f5d3SJohn Marino struct fwdma_seg {
46*86d7f5d3SJohn Marino 	bus_dmamap_t	dma_map;
47*86d7f5d3SJohn Marino 	void *		v_addr;
48*86d7f5d3SJohn Marino 	bus_addr_t	bus_addr;
49*86d7f5d3SJohn Marino };
50*86d7f5d3SJohn Marino 
51*86d7f5d3SJohn Marino struct fwdma_alloc_multi {
52*86d7f5d3SJohn Marino 	bus_size_t	ssize;
53*86d7f5d3SJohn Marino 	bus_size_t	esize;
54*86d7f5d3SJohn Marino 	int		nseg;
55*86d7f5d3SJohn Marino 	bus_dma_tag_t	dma_tag;
56*86d7f5d3SJohn Marino 	struct fwdma_seg seg[0];
57*86d7f5d3SJohn Marino };
58*86d7f5d3SJohn Marino 
59*86d7f5d3SJohn Marino static __inline void *
fwdma_v_addr(struct fwdma_alloc_multi * am,int index)60*86d7f5d3SJohn Marino fwdma_v_addr(struct fwdma_alloc_multi *am, int index)
61*86d7f5d3SJohn Marino {
62*86d7f5d3SJohn Marino 	bus_size_t ssize = am->ssize;
63*86d7f5d3SJohn Marino 	int offset = am->esize * index;
64*86d7f5d3SJohn Marino 
65*86d7f5d3SJohn Marino 	return ((caddr_t)am->seg[offset / ssize].v_addr + (offset % ssize));
66*86d7f5d3SJohn Marino }
67*86d7f5d3SJohn Marino 
68*86d7f5d3SJohn Marino static __inline bus_addr_t
fwdma_bus_addr(struct fwdma_alloc_multi * am,int index)69*86d7f5d3SJohn Marino fwdma_bus_addr(struct fwdma_alloc_multi *am, int index)
70*86d7f5d3SJohn Marino {
71*86d7f5d3SJohn Marino 	bus_size_t ssize = am->ssize;
72*86d7f5d3SJohn Marino 	int offset = am->esize * index;
73*86d7f5d3SJohn Marino 
74*86d7f5d3SJohn Marino 	return (am->seg[offset / ssize].bus_addr + (offset % ssize));
75*86d7f5d3SJohn Marino }
76*86d7f5d3SJohn Marino 
77*86d7f5d3SJohn Marino static __inline void
fwdma_sync(struct fwdma_alloc * dma,bus_dmasync_op_t op)78*86d7f5d3SJohn Marino fwdma_sync(struct fwdma_alloc *dma, bus_dmasync_op_t op)
79*86d7f5d3SJohn Marino {
80*86d7f5d3SJohn Marino 	bus_dmamap_sync(dma->dma_tag, dma->dma_map, op);
81*86d7f5d3SJohn Marino }
82*86d7f5d3SJohn Marino 
83*86d7f5d3SJohn Marino static __inline void
fwdma_sync_multiseg(struct fwdma_alloc_multi * am,int start,int end,bus_dmasync_op_t op)84*86d7f5d3SJohn Marino fwdma_sync_multiseg(struct fwdma_alloc_multi *am,
85*86d7f5d3SJohn Marino 			int start, int end, bus_dmasync_op_t op)
86*86d7f5d3SJohn Marino {
87*86d7f5d3SJohn Marino 	struct fwdma_seg *seg, *eseg;
88*86d7f5d3SJohn Marino 
89*86d7f5d3SJohn Marino 	seg = &am->seg[am->esize * start / am->ssize];
90*86d7f5d3SJohn Marino 	eseg = &am->seg[am->esize * end / am->ssize];
91*86d7f5d3SJohn Marino 	for (; seg <= eseg; seg ++)
92*86d7f5d3SJohn Marino 		bus_dmamap_sync(am->dma_tag, seg->dma_map, op);
93*86d7f5d3SJohn Marino }
94*86d7f5d3SJohn Marino 
95*86d7f5d3SJohn Marino static __inline void
fwdma_sync_multiseg_all(struct fwdma_alloc_multi * am,bus_dmasync_op_t op)96*86d7f5d3SJohn Marino fwdma_sync_multiseg_all(struct fwdma_alloc_multi *am, bus_dmasync_op_t op)
97*86d7f5d3SJohn Marino {
98*86d7f5d3SJohn Marino 	struct fwdma_seg *seg;
99*86d7f5d3SJohn Marino 	int i;
100*86d7f5d3SJohn Marino 
101*86d7f5d3SJohn Marino 	seg = &am->seg[0];
102*86d7f5d3SJohn Marino 	for (i = 0; i < am->nseg; i++, seg++)
103*86d7f5d3SJohn Marino 		bus_dmamap_sync(am->dma_tag, seg->dma_map, op);
104*86d7f5d3SJohn Marino }
105*86d7f5d3SJohn Marino 
106*86d7f5d3SJohn Marino void *fwdma_malloc(struct firewire_comm *, int, bus_size_t, struct fwdma_alloc *, int);
107*86d7f5d3SJohn Marino void fwdma_free(struct firewire_comm *, struct fwdma_alloc *);
108*86d7f5d3SJohn Marino void *fwdma_malloc_size(bus_dma_tag_t, bus_dmamap_t *, bus_size_t, bus_addr_t *, int);
109*86d7f5d3SJohn Marino void fwdma_free_size(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t);
110*86d7f5d3SJohn Marino struct fwdma_alloc_multi *fwdma_malloc_multiseg(struct firewire_comm *,
111*86d7f5d3SJohn Marino 	int, int, int, int);
112*86d7f5d3SJohn Marino void fwdma_free_multiseg(struct fwdma_alloc_multi *);
113*86d7f5d3SJohn Marino 
114