mcpcia_dma.c revision 1.2 1 /* $NetBSD: mcpcia_dma.c,v 1.2 1998/05/07 20:09:38 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 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. This code happens to have been written
10 * by Matthew Jacob for NASA/Ames.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPEMCPCIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
42
43 __KERNEL_RCSID(0, "$NetBSD: mcpcia_dma.c,v 1.2 1998/05/07 20:09:38 thorpej Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 #include <sys/malloc.h>
50 #include <vm/vm.h>
51
52 #define _ALPHA_BUS_DMA_PRIVATE
53 #include <machine/bus.h>
54
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57 #include <alpha/pci/mcpciareg.h>
58 #include <alpha/pci/mcpciavar.h>
59 #include <alpha/pci/pci_kn300.h>
60
61 bus_dma_tag_t mcpcia_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
62
63 int mcpcia_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
64 bus_size_t, bus_size_t, int, bus_dmamap_t *));
65
66 void mcpcia_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
67
68 int mcpcia_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
69 bus_size_t, struct proc *, int));
70
71 int mcpcia_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
72 struct mbuf *, int));
73
74 int mcpcia_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
75 struct uio *, int));
76
77 int mcpcia_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
78 bus_dma_segment_t *, int, bus_size_t, int));
79
80 void mcpcia_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
81
82 #define MCPCIA_DIRECT_MAPPED_BASE 0x80000000
83 #define MCPCIA_SG_MAPPED_BASE (8 << 20)
84
85 #define MCPCIA_SGTLB_INVALIDATE(mcp) \
86 alpha_mb(), \
87 REGVAL(MCPCIA_SG_TBIA(mcp)) = 0xdeadbeef, \
88 alpha_mb()
89
90 void
91 mcpcia_dma_init(ccp)
92 struct mcpcia_config *ccp;
93 {
94 bus_dma_tag_t t;
95
96 /*
97 * Initialize the DMA tag used for direct-mapped DMA.
98 */
99 t = &ccp->cc_dmat_direct;
100 t->_cookie = ccp;
101 t->_wbase = MCPCIA_DIRECT_MAPPED_BASE;
102 t->_get_tag = mcpcia_dma_get_tag;
103 t->_dmamap_create = _bus_dmamap_create;
104 t->_dmamap_destroy = _bus_dmamap_destroy;
105 t->_dmamap_load = _bus_dmamap_load_direct;
106 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
107 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
108 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
109 t->_dmamap_unload = _bus_dmamap_unload;
110 t->_dmamap_sync = _bus_dmamap_sync;
111
112 t->_dmamem_alloc = _bus_dmamem_alloc;
113 t->_dmamem_free = _bus_dmamem_free;
114 t->_dmamem_map = _bus_dmamem_map;
115 t->_dmamem_unmap = _bus_dmamem_unmap;
116 t->_dmamem_mmap = _bus_dmamem_mmap;
117
118 /*
119 * Initialize the DMA tag used for sgmap-mapped DMA.
120 */
121 t = &ccp->cc_dmat_sgmap;
122 t->_cookie = ccp;
123 t->_wbase = MCPCIA_SG_MAPPED_BASE;
124 t->_get_tag = mcpcia_dma_get_tag;
125 t->_dmamap_create = mcpcia_bus_dmamap_create_sgmap;
126 t->_dmamap_destroy = mcpcia_bus_dmamap_destroy_sgmap;
127 t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
128 t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
129 t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
130 t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
131 t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
132 t->_dmamap_sync = _bus_dmamap_sync;
133
134 t->_dmamem_alloc = _bus_dmamem_alloc;
135 t->_dmamem_free = _bus_dmamem_free;
136 t->_dmamem_map = _bus_dmamem_map;
137 t->_dmamem_unmap = _bus_dmamem_unmap;
138 t->_dmamem_mmap = _bus_dmamem_mmap;
139
140 /*
141 * The SRM console is supposed to set up an 8MB direct mapped window
142 * at 8MB (window 0) and a 1MB direct mapped window at 1MB. Useless.
143 *
144 * The DU && OpenVMS convention is to have an 8MB S/G window
145 * at 8MB for window 0, a 1GB direct mapped window at 2GB
146 * for window 1, a 1 GB S/G window at window 2 and a 512 MB
147 * S/G window at window 3. This seems overkill.
148 *
149 * We'll just go with the 8MB S/G window and one 1GB direct window.
150 */
151
152 /*
153 * Initialize the SGMAP.
154 *
155 * Must align page table to its size.
156 */
157 alpha_sgmap_init(t, &ccp->cc_sgmap, "mcpcia_sgmap",
158 MCPCIA_SG_MAPPED_BASE, 0, (8 << 20),
159 sizeof(u_int64_t), NULL, 8 << 10);
160
161 if (ccp->cc_sgmap.aps_ptpa & (1024-1)) {
162 panic("mcpcia_dma_init: bad page table address %x",
163 ccp->cc_sgmap.aps_ptpa);
164 }
165
166 /*
167 * Disable windows first.
168 */
169
170 REGVAL(MCPCIA_W0_BASE(ccp->cc_sc)) = 0;
171 REGVAL(MCPCIA_W1_BASE(ccp->cc_sc)) = 0;
172 REGVAL(MCPCIA_W2_BASE(ccp->cc_sc)) = 0;
173 REGVAL(MCPCIA_W3_BASE(ccp->cc_sc)) = 0;
174 REGVAL(MCPCIA_T0_BASE(ccp->cc_sc)) = 0;
175 REGVAL(MCPCIA_T1_BASE(ccp->cc_sc)) = 0;
176 REGVAL(MCPCIA_T2_BASE(ccp->cc_sc)) = 0;
177 REGVAL(MCPCIA_T3_BASE(ccp->cc_sc)) = 0;
178 alpha_mb();
179
180 /*
181 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB.
182 */
183 REGVAL(MCPCIA_W0_MASK(ccp->cc_sc)) = MCPCIA_WMASK_8M;
184 REGVAL(MCPCIA_T0_BASE(ccp->cc_sc)) =
185 ccp->cc_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
186 REGVAL(MCPCIA_W0_BASE(ccp->cc_sc)) =
187 MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_SG_MAPPED_BASE;
188 alpha_mb();
189
190 MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
191
192 /*
193 * Set up window 1 as a 1 GB Direct-mapped window starting at 2GB.
194 */
195
196 REGVAL(MCPCIA_W0_MASK(ccp->cc_sc)) = MCPCIA_WMASK_1G;
197 REGVAL(MCPCIA_T0_BASE(ccp->cc_sc)) = 0;
198 REGVAL(MCPCIA_W0_BASE(ccp->cc_sc)) =
199 MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
200 alpha_mb();
201
202 /* XXX XXX BEGIN XXX XXX */
203 { /* XXX */
204 extern vm_offset_t alpha_XXX_dmamap_or; /* XXX */
205 alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */
206 } /* XXX */
207 /* XXX XXX END XXX XXX */
208 }
209
210 /*
211 * Return the bus dma tag to be used for the specified bus type.
212 * INTERNAL USE ONLY!
213 */
214 bus_dma_tag_t
215 mcpcia_dma_get_tag(t, bustype)
216 bus_dma_tag_t t;
217 alpha_bus_t bustype;
218 {
219 struct mcpcia_config *ccp = t->_cookie;
220 extern int physmem;
221
222 switch (bustype) {
223 case ALPHA_BUS_PCI:
224 case ALPHA_BUS_EISA:
225 /*
226 * As best as I can tell from the DU source, you can't
227 * do DIRECT MAPPED and S/G at the same time.
228 */
229 return (&ccp->cc_dmat_direct);
230
231 case ALPHA_BUS_ISA:
232 /*
233 * ISA doesn't have enough address bits to use
234 * the direct-mapped DMA window, so we must use
235 * SGMAPs.
236 */
237 return (&ccp->cc_dmat_sgmap);
238
239 default:
240 panic("mcpcia_dma_get_tag: shouldn't be here, really...");
241 }
242 }
243
244 /*
245 * Create a MCPCIA SGMAP-mapped DMA map.
246 */
247 int
248 mcpcia_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
249 flags, dmamp)
250 bus_dma_tag_t t;
251 bus_size_t size;
252 int nsegments;
253 bus_size_t maxsegsz;
254 bus_size_t boundary;
255 int flags;
256 bus_dmamap_t *dmamp;
257 {
258 struct mcpcia_config *ccp = t->_cookie;
259 bus_dmamap_t map;
260 int error;
261
262 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
263 boundary, flags, dmamp);
264 if (error)
265 return (error);
266
267 map = *dmamp;
268
269 if (flags & BUS_DMA_ALLOCNOW) {
270 error = alpha_sgmap_alloc(map, round_page(size),
271 &ccp->cc_sgmap, flags);
272 if (error)
273 mcpcia_bus_dmamap_destroy_sgmap(t, map);
274 }
275
276 return (error);
277 }
278
279 /*
280 * Destroy a MCPCIA SGMAP-mapped DMA map.
281 */
282 void
283 mcpcia_bus_dmamap_destroy_sgmap(t, map)
284 bus_dma_tag_t t;
285 bus_dmamap_t map;
286 {
287 struct mcpcia_config *ccp = t->_cookie;
288
289 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
290 alpha_sgmap_free(map, &ccp->cc_sgmap);
291
292 _bus_dmamap_destroy(t, map);
293 }
294
295 /*
296 * Load a MCPCIA SGMAP-mapped DMA map with a linear buffer.
297 */
298 int
299 mcpcia_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
300 bus_dma_tag_t t;
301 bus_dmamap_t map;
302 void *buf;
303 bus_size_t buflen;
304 struct proc *p;
305 int flags;
306 {
307 int error;
308 struct mcpcia_config *ccp = t->_cookie;
309
310 error = pci_sgmap_pte64_load(t, map, buf, buflen, p,
311 flags, &ccp->cc_sgmap);
312 if (error == 0)
313 MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
314 return (error);
315 }
316
317 /*
318 * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain.
319 */
320 int
321 mcpcia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
322 bus_dma_tag_t t;
323 bus_dmamap_t map;
324 struct mbuf *m;
325 int flags;
326 {
327 int error;
328 struct mcpcia_config *ccp = t->_cookie;
329
330 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, &ccp->cc_sgmap);
331 if (error == 0)
332 MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
333 return (error);
334 }
335
336 /*
337 * Load a MCPCIA SGMAP-mapped DMA map with a uio.
338 */
339 int
340 mcpcia_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
341 bus_dma_tag_t t;
342 bus_dmamap_t map;
343 struct uio *uio;
344 int flags;
345 {
346 int error;
347 struct mcpcia_config *ccp = t->_cookie;
348
349 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, &ccp->cc_sgmap);
350 if (error == 0)
351 MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
352 return (error);
353 }
354
355 /*
356 * Load a MCPCIA SGMAP-mapped DMA map with raw memory.
357 */
358 int
359 mcpcia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
360 bus_dma_tag_t t;
361 bus_dmamap_t map;
362 bus_dma_segment_t *segs;
363 int nsegs;
364 bus_size_t size;
365 int flags;
366 {
367 int error;
368 struct mcpcia_config *ccp = t->_cookie;
369
370 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs,
371 size, flags, &ccp->cc_sgmap);
372 if (error == 0)
373 MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
374 return (error);
375 }
376
377 /*
378 * Unload a MCPCIA DMA map.
379 */
380 void
381 mcpcia_bus_dmamap_unload_sgmap(t, map)
382 bus_dma_tag_t t;
383 bus_dmamap_t map;
384 {
385 struct mcpcia_config *ccp = t->_cookie;
386
387 /*
388 * Invalidate any SGMAP page table entries used by this mapping.
389 */
390 pci_sgmap_pte64_unload(t, map, &ccp->cc_sgmap);
391 MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
392
393 /*
394 * Do the generic bits of the unload.
395 */
396 _bus_dmamap_unload(t, map);
397 }
398