tc_dma_3000_500.c revision 1.2.6.1 1 /* $NetBSD: tc_dma_3000_500.c,v 1.2.6.1 1997/09/04 00:54:20 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
41
42 __KERNEL_RCSID(0, "$NetBSD: tc_dma_3000_500.c,v 1.2.6.1 1997/09/04 00:54:20 thorpej Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <vm/vm.h>
49
50 #define _ALPHA_BUS_DMA_PRIVATE
51 #include <machine/bus.h>
52
53 #include <dev/tc/tcvar.h>
54 #include <alpha/tc/tc_sgmap.h>
55 #include <alpha/tc/tc_dma_3000_500.h>
56
57 struct alpha_bus_dma_tag tc_dmat_sgmap = {
58 NULL, /* _cookie */
59 NULL, /* _get_tag */
60 tc_bus_dmamap_create_sgmap,
61 tc_bus_dmamap_destroy_sgmap,
62 tc_bus_dmamap_load_sgmap,
63 tc_bus_dmamap_load_mbuf_sgmap,
64 tc_bus_dmamap_load_uio_sgmap,
65 tc_bus_dmamap_load_raw_sgmap,
66 tc_bus_dmamap_unload_sgmap,
67 NULL, /* _dmamap_sync */
68 _bus_dmamem_alloc,
69 _bus_dmamem_free,
70 _bus_dmamem_map,
71 _bus_dmamem_unmap,
72 _bus_dmamem_mmap,
73 };
74
75 struct tc_dma_slot_info {
76 struct alpha_sgmap tdsi_sgmap; /* sgmap for slot */
77 struct alpha_bus_dma_tag tdsi_dmat; /* dma tag for slot */
78 };
79 struct tc_dma_slot_info *tc_dma_slot_info;
80
81 void
82 tc_dma_init_3000_500(nslots)
83 int nslots;
84 {
85 extern struct alpha_bus_dma_tag tc_dmat_direct;
86 size_t sisize;
87 int i;
88
89 /* Allocate per-slot DMA info. */
90 sisize = nslots * sizeof(struct tc_dma_slot_info);
91 tc_dma_slot_info = malloc(sisize, M_DEVBUF, M_NOWAIT);
92 if (tc_dma_slot_info == NULL)
93 panic("tc_dma_init: can't allocate per-slot DMA info");
94 bzero(tc_dma_slot_info, sisize);
95
96 /* Default all slots to direct-mapped. */
97 for (i = 0; i < nslots; i++)
98 bcopy(&tc_dmat_direct, &tc_dma_slot_info[i].tdsi_dmat,
99 sizeof(tc_dma_slot_info[i].tdsi_dmat));
100 }
101
102 /*
103 * Return the DMA tag for the given slot.
104 */
105 bus_dma_tag_t
106 tc_dma_get_tag_3000_500(slot)
107 int slot;
108 {
109
110 return (&tc_dma_slot_info[slot].tdsi_dmat);
111 }
112
113 /*
114 * Create a TurboChannel SGMAP-mapped DMA map.
115 */
116 int
117 tc_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
118 flags, dmamp)
119 bus_dma_tag_t t;
120 bus_size_t size;
121 int nsegments;
122 bus_size_t maxsegsz;
123 bus_size_t boundary;
124 int flags;
125 bus_dmamap_t *dmamp;
126 {
127 struct tc_dma_slot_info *tdsi = t->_cookie;
128 struct alpha_sgmap_cookie *a;
129 bus_dmamap_t map;
130 int error;
131
132 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
133 boundary, flags, dmamp);
134 if (error)
135 return (error);
136
137 map = *dmamp;
138
139 a = malloc(sizeof(struct alpha_sgmap_cookie), M_DEVBUF,
140 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
141 if (a == NULL) {
142 _bus_dmamap_destroy(t, map);
143 return (ENOMEM);
144 }
145 bzero(a, sizeof(struct alpha_sgmap_cookie));
146 map->_dm_sgcookie = a;
147
148 if (flags & BUS_DMA_ALLOCNOW) {
149 error = alpha_sgmap_alloc(map, round_page(size),
150 &tdsi->tdsi_sgmap, flags);
151 if (error)
152 tc_bus_dmamap_destroy_sgmap(t, map);
153 }
154
155 return (error);
156 }
157
158 /*
159 * Destroy a TurboChannel SGMAP-mapped DMA map.
160 */
161 void
162 tc_bus_dmamap_destroy_sgmap(t, map)
163 bus_dma_tag_t t;
164 bus_dmamap_t map;
165 {
166 struct tc_dma_slot_info *tdsi = t->_cookie;
167 struct alpha_sgmap_cookie *a = map->_dm_sgcookie;
168
169 if (a->apdc_flags & APDC_HAS_SGMAP)
170 alpha_sgmap_free(&tdsi->tdsi_sgmap, a);
171
172 free(a, M_DEVBUF);
173 _bus_dmamap_destroy(t, map);
174 }
175
176 /*
177 * Load a TurboChannel SGMAP-mapped DMA map with a linear buffer.
178 */
179 int
180 tc_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
181 bus_dma_tag_t t;
182 bus_dmamap_t map;
183 void *buf;
184 bus_size_t buflen;
185 struct proc *p;
186 int flags;
187 {
188 struct tc_dma_slot_info *tdsi = t->_cookie;
189
190 return (tc_sgmap_load(t, map, buf, buflen, p, flags,
191 &tdsi->tdsi_sgmap));
192 }
193
194 /*
195 * Load a TurboChannel SGMAP-mapped DMA map with an mbuf chain.
196 */
197 int
198 tc_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
199 bus_dma_tag_t t;
200 bus_dmamap_t map;
201 struct mbuf *m;
202 int flags;
203 {
204 struct tc_dma_slot_info *tdsi = t->_cookie;
205
206 return (tc_sgmap_load_mbuf(t, map, m, flags, &tdsi->tdsi_sgmap));
207 }
208
209 /*
210 * Load a TurboChannel SGMAP-mapped DMA map with a uio.
211 */
212 int
213 tc_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
214 bus_dma_tag_t t;
215 bus_dmamap_t map;
216 struct uio *uio;
217 int flags;
218 {
219 struct tc_dma_slot_info *tdsi = t->_cookie;
220
221 return (tc_sgmap_load_uio(t, map, uio, flags, &tdsi->tdsi_sgmap));
222 }
223
224 /*
225 * Load a TurboChannel SGMAP-mapped DMA map with raw memory.
226 */
227 int
228 tc_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
229 bus_dma_tag_t t;
230 bus_dmamap_t map;
231 bus_dma_segment_t *segs;
232 int nsegs;
233 bus_size_t size;
234 int flags;
235 {
236 struct tc_dma_slot_info *tdsi = t->_cookie;
237
238 return (tc_sgmap_load_raw(t, map, segs, nsegs, size, flags,
239 &tdsi->tdsi_sgmap));
240 }
241
242 /*
243 * Unload a TurboChannel SGMAP-mapped DMA map.
244 */
245 void
246 tc_bus_dmamap_unload_sgmap(t, map)
247 bus_dma_tag_t t;
248 bus_dmamap_t map;
249 {
250 struct tc_dma_slot_info *tdsi = t->_cookie;
251
252 /*
253 * Invalidate any SGMAP page table entries used by this
254 * mapping.
255 */
256 tc_sgmap_unload(t, map, &tdsi->tdsi_sgmap);
257
258 /*
259 * Do the generic bits of the unload.
260 */
261 _bus_dmamap_unload(t, map);
262 }
263