186d7f5d3SJohn Marino /*
286d7f5d3SJohn Marino * Copyright (C) 2003
386d7f5d3SJohn Marino * Hidetoshi Shimokawa. All rights reserved.
486d7f5d3SJohn Marino *
586d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
686d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
786d7f5d3SJohn Marino * are met:
886d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
986d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
1086d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1186d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
1286d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
1386d7f5d3SJohn Marino * 3. All advertising materials mentioning features or use of this software
1486d7f5d3SJohn Marino * must display the following acknowledgement:
1586d7f5d3SJohn Marino *
1686d7f5d3SJohn Marino * This product includes software developed by Hidetoshi Shimokawa.
1786d7f5d3SJohn Marino *
1886d7f5d3SJohn Marino * 4. Neither the name of the author nor the names of its contributors
1986d7f5d3SJohn Marino * may be used to endorse or promote products derived from this software
2086d7f5d3SJohn Marino * without specific prior written permission.
2186d7f5d3SJohn Marino *
2286d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2386d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2486d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2586d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2686d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2786d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2886d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2986d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3086d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3186d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3286d7f5d3SJohn Marino * SUCH DAMAGE.
3386d7f5d3SJohn Marino *
3486d7f5d3SJohn Marino * $FreeBSD: src/sys/dev/firewire/fwdma.h,v 1.2 2003/05/27 16:34:52 scottl Exp $
3586d7f5d3SJohn Marino * $DragonFly: src/sys/bus/firewire/fwdma.h,v 1.3 2004/02/05 13:32:08 joerg Exp $
3686d7f5d3SJohn Marino */
3786d7f5d3SJohn Marino
3886d7f5d3SJohn Marino struct fwdma_alloc {
3986d7f5d3SJohn Marino bus_dma_tag_t dma_tag;
4086d7f5d3SJohn Marino bus_dmamap_t dma_map;
4186d7f5d3SJohn Marino void * v_addr;
4286d7f5d3SJohn Marino bus_addr_t bus_addr;
4386d7f5d3SJohn Marino };
4486d7f5d3SJohn Marino
4586d7f5d3SJohn Marino struct fwdma_seg {
4686d7f5d3SJohn Marino bus_dmamap_t dma_map;
4786d7f5d3SJohn Marino void * v_addr;
4886d7f5d3SJohn Marino bus_addr_t bus_addr;
4986d7f5d3SJohn Marino };
5086d7f5d3SJohn Marino
5186d7f5d3SJohn Marino struct fwdma_alloc_multi {
5286d7f5d3SJohn Marino bus_size_t ssize;
5386d7f5d3SJohn Marino bus_size_t esize;
5486d7f5d3SJohn Marino int nseg;
5586d7f5d3SJohn Marino bus_dma_tag_t dma_tag;
5686d7f5d3SJohn Marino struct fwdma_seg seg[0];
5786d7f5d3SJohn Marino };
5886d7f5d3SJohn Marino
5986d7f5d3SJohn Marino static __inline void *
fwdma_v_addr(struct fwdma_alloc_multi * am,int index)6086d7f5d3SJohn Marino fwdma_v_addr(struct fwdma_alloc_multi *am, int index)
6186d7f5d3SJohn Marino {
6286d7f5d3SJohn Marino bus_size_t ssize = am->ssize;
6386d7f5d3SJohn Marino int offset = am->esize * index;
6486d7f5d3SJohn Marino
6586d7f5d3SJohn Marino return ((caddr_t)am->seg[offset / ssize].v_addr + (offset % ssize));
6686d7f5d3SJohn Marino }
6786d7f5d3SJohn Marino
6886d7f5d3SJohn Marino static __inline bus_addr_t
fwdma_bus_addr(struct fwdma_alloc_multi * am,int index)6986d7f5d3SJohn Marino fwdma_bus_addr(struct fwdma_alloc_multi *am, int index)
7086d7f5d3SJohn Marino {
7186d7f5d3SJohn Marino bus_size_t ssize = am->ssize;
7286d7f5d3SJohn Marino int offset = am->esize * index;
7386d7f5d3SJohn Marino
7486d7f5d3SJohn Marino return (am->seg[offset / ssize].bus_addr + (offset % ssize));
7586d7f5d3SJohn Marino }
7686d7f5d3SJohn Marino
7786d7f5d3SJohn Marino static __inline void
fwdma_sync(struct fwdma_alloc * dma,bus_dmasync_op_t op)7886d7f5d3SJohn Marino fwdma_sync(struct fwdma_alloc *dma, bus_dmasync_op_t op)
7986d7f5d3SJohn Marino {
8086d7f5d3SJohn Marino bus_dmamap_sync(dma->dma_tag, dma->dma_map, op);
8186d7f5d3SJohn Marino }
8286d7f5d3SJohn Marino
8386d7f5d3SJohn Marino static __inline void
fwdma_sync_multiseg(struct fwdma_alloc_multi * am,int start,int end,bus_dmasync_op_t op)8486d7f5d3SJohn Marino fwdma_sync_multiseg(struct fwdma_alloc_multi *am,
8586d7f5d3SJohn Marino int start, int end, bus_dmasync_op_t op)
8686d7f5d3SJohn Marino {
8786d7f5d3SJohn Marino struct fwdma_seg *seg, *eseg;
8886d7f5d3SJohn Marino
8986d7f5d3SJohn Marino seg = &am->seg[am->esize * start / am->ssize];
9086d7f5d3SJohn Marino eseg = &am->seg[am->esize * end / am->ssize];
9186d7f5d3SJohn Marino for (; seg <= eseg; seg ++)
9286d7f5d3SJohn Marino bus_dmamap_sync(am->dma_tag, seg->dma_map, op);
9386d7f5d3SJohn Marino }
9486d7f5d3SJohn Marino
9586d7f5d3SJohn Marino static __inline void
fwdma_sync_multiseg_all(struct fwdma_alloc_multi * am,bus_dmasync_op_t op)9686d7f5d3SJohn Marino fwdma_sync_multiseg_all(struct fwdma_alloc_multi *am, bus_dmasync_op_t op)
9786d7f5d3SJohn Marino {
9886d7f5d3SJohn Marino struct fwdma_seg *seg;
9986d7f5d3SJohn Marino int i;
10086d7f5d3SJohn Marino
10186d7f5d3SJohn Marino seg = &am->seg[0];
10286d7f5d3SJohn Marino for (i = 0; i < am->nseg; i++, seg++)
10386d7f5d3SJohn Marino bus_dmamap_sync(am->dma_tag, seg->dma_map, op);
10486d7f5d3SJohn Marino }
10586d7f5d3SJohn Marino
10686d7f5d3SJohn Marino void *fwdma_malloc(struct firewire_comm *, int, bus_size_t, struct fwdma_alloc *, int);
10786d7f5d3SJohn Marino void fwdma_free(struct firewire_comm *, struct fwdma_alloc *);
10886d7f5d3SJohn Marino void *fwdma_malloc_size(bus_dma_tag_t, bus_dmamap_t *, bus_size_t, bus_addr_t *, int);
10986d7f5d3SJohn Marino void fwdma_free_size(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t);
11086d7f5d3SJohn Marino struct fwdma_alloc_multi *fwdma_malloc_multiseg(struct firewire_comm *,
11186d7f5d3SJohn Marino int, int, int, int);
11286d7f5d3SJohn Marino void fwdma_free_multiseg(struct fwdma_alloc_multi *);
11386d7f5d3SJohn Marino
114