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