tc_dma_3000_500.c revision 1.2 1 /* $NetBSD: tc_dma_3000_500.c,v 1.2 1997/06/07 00:02:18 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 <machine/options.h> /* Config options headers */
41 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
42
43 __KERNEL_RCSID(0, "$NetBSD: tc_dma_3000_500.c,v 1.2 1997/06/07 00:02:18 thorpej Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <vm/vm.h>
50
51 #define _ALPHA_BUS_DMA_PRIVATE
52 #include <machine/bus.h>
53
54 #include <dev/tc/tcvar.h>
55 #include <alpha/tc/tc_sgmap.h>
56 #include <alpha/tc/tc_dma_3000_500.h>
57
58 struct alpha_bus_dma_tag tc_dmat_sgmap = {
59 NULL, /* _cookie */
60 NULL, /* _get_tag */
61 tc_bus_dmamap_create_sgmap,
62 tc_bus_dmamap_destroy_sgmap,
63 tc_bus_dmamap_load_sgmap,
64 tc_bus_dmamap_load_mbuf_sgmap,
65 tc_bus_dmamap_load_uio_sgmap,
66 tc_bus_dmamap_load_raw_sgmap,
67 tc_bus_dmamap_unload_sgmap,
68 NULL, /* _dmamap_sync */
69 _bus_dmamem_alloc,
70 _bus_dmamem_free,
71 _bus_dmamem_map,
72 _bus_dmamem_unmap,
73 _bus_dmamem_mmap,
74 };
75
76 struct tc_dma_slot_info {
77 struct alpha_sgmap tdsi_sgmap; /* sgmap for slot */
78 struct alpha_bus_dma_tag tdsi_dmat; /* dma tag for slot */
79 };
80 struct tc_dma_slot_info *tc_dma_slot_info;
81
82 void
83 tc_dma_init_3000_500(nslots)
84 int nslots;
85 {
86 extern struct alpha_bus_dma_tag tc_dmat_direct;
87 size_t sisize;
88 int i;
89
90 /* Allocate per-slot DMA info. */
91 sisize = nslots * sizeof(struct tc_dma_slot_info);
92 tc_dma_slot_info = malloc(sisize, M_DEVBUF, M_NOWAIT);
93 if (tc_dma_slot_info == NULL)
94 panic("tc_dma_init: can't allocate per-slot DMA info");
95 bzero(tc_dma_slot_info, sisize);
96
97 /* Default all slots to direct-mapped. */
98 for (i = 0; i < nslots; i++)
99 bcopy(&tc_dmat_direct, &tc_dma_slot_info[i].tdsi_dmat,
100 sizeof(tc_dma_slot_info[i].tdsi_dmat));
101 }
102
103 /*
104 * Return the DMA tag for the given slot.
105 */
106 bus_dma_tag_t
107 tc_dma_get_tag_3000_500(slot)
108 int slot;
109 {
110
111 return (&tc_dma_slot_info[slot].tdsi_dmat);
112 }
113
114 /*
115 * Create a TurboChannel SGMAP-mapped DMA map.
116 */
117 int
118 tc_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
119 flags, dmamp)
120 bus_dma_tag_t t;
121 bus_size_t size;
122 int nsegments;
123 bus_size_t maxsegsz;
124 bus_size_t boundary;
125 int flags;
126 bus_dmamap_t *dmamp;
127 {
128 struct tc_dma_slot_info *tdsi = t->_cookie;
129 struct alpha_sgmap_cookie *a;
130 bus_dmamap_t map;
131 int error;
132
133 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
134 boundary, flags, dmamp);
135 if (error)
136 return (error);
137
138 map = *dmamp;
139
140 a = malloc(sizeof(struct alpha_sgmap_cookie), M_DEVBUF,
141 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
142 if (a == NULL) {
143 _bus_dmamap_destroy(t, map);
144 return (ENOMEM);
145 }
146 bzero(a, sizeof(struct alpha_sgmap_cookie));
147 map->_dm_sgcookie = a;
148
149 if (flags & BUS_DMA_ALLOCNOW) {
150 error = alpha_sgmap_alloc(map, round_page(size),
151 &tdsi->tdsi_sgmap, flags);
152 if (error)
153 tc_bus_dmamap_destroy_sgmap(t, map);
154 }
155
156 return (error);
157 }
158
159 /*
160 * Destroy a TurboChannel SGMAP-mapped DMA map.
161 */
162 void
163 tc_bus_dmamap_destroy_sgmap(t, map)
164 bus_dma_tag_t t;
165 bus_dmamap_t map;
166 {
167 struct tc_dma_slot_info *tdsi = t->_cookie;
168 struct alpha_sgmap_cookie *a = map->_dm_sgcookie;
169
170 if (a->apdc_flags & APDC_HAS_SGMAP)
171 alpha_sgmap_free(&tdsi->tdsi_sgmap, a);
172
173 free(a, M_DEVBUF);
174 _bus_dmamap_destroy(t, map);
175 }
176
177 /*
178 * Load a TurboChannel SGMAP-mapped DMA map with a linear buffer.
179 */
180 int
181 tc_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
182 bus_dma_tag_t t;
183 bus_dmamap_t map;
184 void *buf;
185 bus_size_t buflen;
186 struct proc *p;
187 int flags;
188 {
189 struct tc_dma_slot_info *tdsi = t->_cookie;
190
191 return (tc_sgmap_load(t, map, buf, buflen, p, flags,
192 &tdsi->tdsi_sgmap));
193 }
194
195 /*
196 * Load a TurboChannel SGMAP-mapped DMA map with an mbuf chain.
197 */
198 int
199 tc_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
200 bus_dma_tag_t t;
201 bus_dmamap_t map;
202 struct mbuf *m;
203 int flags;
204 {
205 struct tc_dma_slot_info *tdsi = t->_cookie;
206
207 return (tc_sgmap_load_mbuf(t, map, m, flags, &tdsi->tdsi_sgmap));
208 }
209
210 /*
211 * Load a TurboChannel SGMAP-mapped DMA map with a uio.
212 */
213 int
214 tc_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
215 bus_dma_tag_t t;
216 bus_dmamap_t map;
217 struct uio *uio;
218 int flags;
219 {
220 struct tc_dma_slot_info *tdsi = t->_cookie;
221
222 return (tc_sgmap_load_uio(t, map, uio, flags, &tdsi->tdsi_sgmap));
223 }
224
225 /*
226 * Load a TurboChannel SGMAP-mapped DMA map with raw memory.
227 */
228 int
229 tc_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
230 bus_dma_tag_t t;
231 bus_dmamap_t map;
232 bus_dma_segment_t *segs;
233 int nsegs;
234 bus_size_t size;
235 int flags;
236 {
237 struct tc_dma_slot_info *tdsi = t->_cookie;
238
239 return (tc_sgmap_load_raw(t, map, segs, nsegs, size, flags,
240 &tdsi->tdsi_sgmap));
241 }
242
243 /*
244 * Unload a TurboChannel SGMAP-mapped DMA map.
245 */
246 void
247 tc_bus_dmamap_unload_sgmap(t, map)
248 bus_dma_tag_t t;
249 bus_dmamap_t map;
250 {
251 struct tc_dma_slot_info *tdsi = t->_cookie;
252
253 /*
254 * Invalidate any SGMAP page table entries used by this
255 * mapping.
256 */
257 tc_sgmap_unload(t, map, &tdsi->tdsi_sgmap);
258
259 /*
260 * Do the generic bits of the unload.
261 */
262 _bus_dmamap_unload(t, map);
263 }
264