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