dwlpx_dma.c revision 1.1.2.1 1 /* $NetBSD: dwlpx_dma.c,v 1.1.2.1 1997/05/23 21:44:45 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, SPEDWLPXL, 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: dwlpx_dma.c,v 1.1.2.1 1997/05/23 21:44:45 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/tlsb/tlsbreg.h>
58 #include <alpha/tlsb/kftxxvar.h>
59 #include <alpha/tlsb/kftxxreg.h>
60 #include <alpha/pci/dwlpxreg.h>
61 #include <alpha/pci/dwlpxvar.h>
62 #include <alpha/pci/pci_kn8ae.h>
63
64 bus_dma_tag_t dwlpx_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
65
66 int dwlpx_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int,
67 bus_size_t, bus_size_t, int, bus_dmamap_t *));
68 int dwlpx_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
69 bus_size_t, bus_size_t, int, bus_dmamap_t *));
70
71 void dwlpx_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
72
73 int dwlpx_bus_dmamap_load_direct __P((bus_dma_tag_t, bus_dmamap_t, void *,
74 bus_size_t, struct proc *, int));
75 int dwlpx_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
76 bus_size_t, struct proc *, int));
77
78 int dwlpx_bus_dmamap_load_mbuf_direct __P((bus_dma_tag_t, bus_dmamap_t,
79 struct mbuf *, int));
80 int dwlpx_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
81 struct mbuf *, int));
82
83 int dwlpx_bus_dmamap_load_uio_direct __P((bus_dma_tag_t, bus_dmamap_t,
84 struct uio *, int));
85 int dwlpx_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
86 struct uio *, int));
87
88 int dwlpx_bus_dmamap_load_raw_direct __P((bus_dma_tag_t, bus_dmamap_t,
89 bus_dma_segment_t *, int, bus_size_t, int));
90 int dwlpx_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
91 bus_dma_segment_t *, int, bus_size_t, int));
92
93 void dwlpx_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
94
95 /*
96 * The direct-mapped DMA window begins at this PCI address.
97 */
98 #define DWLPX_DIRECT_MAPPED_BASE 0x40000000
99
100 void
101 dwlpx_dma_init(ccp)
102 struct dwlpx_config *ccp;
103 {
104 char *exname;
105 bus_dma_tag_t t;
106 int i;
107
108 /*
109 * Initialize the DMA tag used for direct-mapped DMA.
110 */
111 t = &ccp->cc_dmat_direct;
112 t->_cookie = ccp;
113 t->_get_tag = dwlpx_dma_get_tag;
114 t->_dmamap_create = dwlpx_bus_dmamap_create;
115 t->_dmamap_destroy = dwlpx_bus_dmamap_destroy;
116 t->_dmamap_load = dwlpx_bus_dmamap_load_direct;
117 t->_dmamap_load_mbuf = dwlpx_bus_dmamap_load_mbuf_direct;
118 t->_dmamap_load_uio = dwlpx_bus_dmamap_load_uio_direct;
119 t->_dmamap_load_raw = dwlpx_bus_dmamap_load_raw_direct;
120 t->_dmamap_unload = dwlpx_bus_dmamap_unload;
121 t->_dmamap_sync = NULL; /* Nothing to do. */
122
123 t->_dmamem_alloc = _bus_dmamem_alloc;
124 t->_dmamem_free = _bus_dmamem_free;
125 t->_dmamem_map = _bus_dmamem_map;
126 t->_dmamem_unmap = _bus_dmamem_unmap;
127 t->_dmamem_mmap = _bus_dmamem_mmap;
128
129 /*
130 * Initialize the DMA tag used for sgmap-mapped DMA.
131 */
132 t = &ccp->cc_dmat_sgmap;
133 t->_cookie = ccp;
134 t->_get_tag = dwlpx_dma_get_tag;
135 t->_dmamap_create = dwlpx_bus_dmamap_create_sgmap;
136 t->_dmamap_destroy = dwlpx_bus_dmamap_destroy;
137 t->_dmamap_load = dwlpx_bus_dmamap_load_sgmap;
138 t->_dmamap_load_mbuf = dwlpx_bus_dmamap_load_mbuf_sgmap;
139 t->_dmamap_load_uio = dwlpx_bus_dmamap_load_uio_sgmap;
140 t->_dmamap_load_raw = dwlpx_bus_dmamap_load_raw_sgmap;
141 t->_dmamap_unload = dwlpx_bus_dmamap_unload;
142 t->_dmamap_sync = NULL; /* Nothing to do. */
143
144 t->_dmamem_alloc = _bus_dmamem_alloc;
145 t->_dmamem_free = _bus_dmamem_free;
146 t->_dmamem_map = _bus_dmamem_map;
147 t->_dmamem_unmap = _bus_dmamem_unmap;
148 t->_dmamem_mmap = _bus_dmamem_mmap;
149
150 /*
151 * Initialize the SGMAP.
152 */
153 exname = malloc(16, M_DEVBUF, M_NOWAIT);
154 if (exname == NULL)
155 panic("dwlpx_dma_init");
156 sprintf(exname, "%s_sgmap", ccp->cc_sc->dwlpx_dev.dv_xname);
157 pci_dma_sgmap_init(t, &ccp->cc_sgmap, exname,
158 (8*1024*1024), (8*1024*1024));
159
160 /*
161 * Set up DMA windows for this DWLPX.
162 *
163 * Basically, we set up for a 1GB direct mapped window,
164 * starting from PCI address 0x40000000. And that's it.
165 *
166 * Do this even for all HPCs- even for the nonexistent
167 * one on hose zero of a KFTIA.
168 */
169 for (i = 0; i < NHPC; i++) {
170 REGVAL(PCIA_WMASK_A(i) + ccp->cc_sysbase) = 0;
171 REGVAL(PCIA_TBASE_A(i) + ccp->cc_sysbase) = 0;
172 REGVAL(PCIA_WBASE_A(i) + ccp->cc_sysbase) = 0;
173 REGVAL(PCIA_WMASK_B(i) + ccp->cc_sysbase) = 0x3fff0000;
174 REGVAL(PCIA_TBASE_B(i) + ccp->cc_sysbase) = 0;
175 REGVAL(PCIA_WBASE_B(i) + ccp->cc_sysbase) = 0x40000002;
176 REGVAL(PCIA_WMASK_C(i) + ccp->cc_sysbase) = 0;
177 REGVAL(PCIA_TBASE_C(i) + ccp->cc_sysbase) = 0;
178 REGVAL(PCIA_WBASE_C(i) + ccp->cc_sysbase) = 0;
179 }
180
181 /* XXX XXX BEGIN XXX XXX */
182 { /* XXX */
183 extern vm_offset_t alpha_XXX_dmamap_or; /* XXX */
184 alpha_XXX_dmamap_or = DWLPX_DIRECT_MAPPED_BASE; /* XXX */
185 } /* XXX */
186 /* XXX XXX END XXX XXX */
187 }
188
189 /*
190 * Return the bus dma tag to be used for the specified bus type.
191 * INTERNAL USE ONLY!
192 */
193 bus_dma_tag_t
194 dwlpx_dma_get_tag(t, bustype)
195 bus_dma_tag_t t;
196 alpha_bus_t bustype;
197 {
198 struct dwlpx_config *ccp = t->_cookie;
199
200 switch (bustype) {
201 case ALPHA_BUS_PCI:
202 case ALPHA_BUS_EISA:
203 /*
204 * XXX FIXME!
205 * XXX If the system has more than 1G of RAM,
206 * XXX we need to use SGMAPs, or some combination
207 * XXX of direct-mapped and SGMAP-mapped DMA.
208 */
209 return (&ccp->cc_dmat_direct);
210
211 case ALPHA_BUS_ISA:
212 /*
213 * ISA doesn't have enough address bits to use
214 * the direct-mapped DMA window, so we must use
215 * SGMAPs.
216 */
217 return (&ccp->cc_dmat_sgmap);
218
219 default:
220 panic("dwlpx_dma_get_tag: shouldn't be here, really...");
221 }
222 }
223
224 /*
225 * Create a DWLPX DMA map.
226 */
227 int
228 dwlpx_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
229 bus_dma_tag_t t;
230 bus_size_t size;
231 int nsegments;
232 bus_size_t maxsegsz;
233 bus_size_t boundary;
234 int flags;
235 bus_dmamap_t *dmamp;
236 {
237 struct alpha_pci_dma_cookie *a;
238 bus_dmamap_t map;
239 int error;
240
241 /* Call common function to create the basic map. */
242 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
243 flags, dmamp);
244 if (error)
245 return (error);
246
247 map = *dmamp;
248
249 /* Allocate PCI-specific housekeeping stuff. */
250 a = malloc(sizeof(struct alpha_pci_dma_cookie), M_DEVBUF,
251 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
252 if (a == NULL) {
253 _bus_dmamap_destroy(t, map);
254 return (ENOMEM);
255 }
256 bzero(a, sizeof(struct alpha_pci_dma_cookie));
257 map->_dm_cookie = a;
258
259 return (0);
260 }
261
262 int
263 dwlpx_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
264 flags, dmamp)
265 bus_dma_tag_t t;
266 bus_size_t size;
267 int nsegments;
268 bus_size_t maxsegsz;
269 bus_size_t boundary;
270 int flags;
271 bus_dmamap_t *dmamp;
272 {
273 struct dwlpx_config *ccp = t->_cookie;
274 struct alpha_pci_dma_cookie *a;
275 bus_dmamap_t map;
276 int error;
277
278 error = dwlpx_bus_dmamap_create(t, size, nsegments, maxsegsz,
279 boundary, flags, dmamp);
280 if (error)
281 return (error);
282
283 map = *dmamp;
284 a = map->_dm_cookie;
285
286 if (flags & BUS_DMA_ALLOCNOW)
287 error = pci_dma_sgmap_alloc(map, round_page(size),
288 &ccp->cc_sgmap, a, flags);
289
290 return (error);
291 }
292
293 /*
294 * Destroy a DWLPX DMA map.
295 */
296 void
297 dwlpx_bus_dmamap_destroy(t, map)
298 bus_dma_tag_t t;
299 bus_dmamap_t map;
300 {
301 struct dwlpx_config *ccp = t->_cookie;
302 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
303
304 if (a->apdc_flags & APDC_HAS_SGMAP)
305 pci_dma_sgmap_free(&ccp->cc_sgmap, a);
306
307 free(map->_dm_cookie, M_DEVBUF);
308 _bus_dmamap_destroy(t, map);
309 }
310
311 /*
312 * Load a DWLPX direct-mapped DMA map with a linear buffer.
313 */
314 int
315 dwlpx_bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
316 bus_dma_tag_t t;
317 bus_dmamap_t map;
318 void *buf;
319 bus_size_t buflen;
320 struct proc *p;
321 int flags;
322 {
323
324 return (_bus_dmamap_load_direct_common(t, map, buf, buflen, p,
325 flags, DWLPX_DIRECT_MAPPED_BASE));
326 }
327
328 /*
329 * Load a DWLPX SGMAP-mapped DMA map with a linear buffer.
330 */
331 int
332 dwlpx_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
333 bus_dma_tag_t t;
334 bus_dmamap_t map;
335 void *buf;
336 bus_size_t buflen;
337 struct proc *p;
338 int flags;
339 {
340 struct dwlpx_config *ccp = t->_cookie;
341 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
342 int error;
343
344 error = pci_dma_sgmap_load(t, map, buf, buflen, p, flags,
345 &ccp->cc_sgmap, a);
346 if (error == 0) {
347 /*
348 * Invalidate DWLPX TLB.
349 */
350 /* XXX implement XXX */
351 }
352 return (error);
353 }
354
355 /*
356 * Load a DWLPX direct-mapped DMA map with an mbuf chain.
357 */
358 int
359 dwlpx_bus_dmamap_load_mbuf_direct(t, map, m, flags)
360 bus_dma_tag_t t;
361 bus_dmamap_t map;
362 struct mbuf *m;
363 int flags;
364 {
365
366 return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
367 flags, DWLPX_DIRECT_MAPPED_BASE));
368 }
369
370 /*
371 * Load a DWLPX SGMAP-mapped DMA map with an mbuf chain.
372 */
373 int
374 dwlpx_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
375 bus_dma_tag_t t;
376 bus_dmamap_t map;
377 struct mbuf *m;
378 int flags;
379 {
380 struct dwlpx_config *ccp = t->_cookie;
381 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
382 int error;
383
384 error = pci_dma_sgmap_load_mbuf(t, map, m, flags, &ccp->cc_sgmap, a);
385 if (error == 0) {
386 /*
387 * Invalidate DWLPX TLB.
388 */
389 /* XXX implement XXX */
390 }
391 return (error);
392 }
393
394 /*
395 * Load a DWLPX direct-mapped DMA map with a uio.
396 */
397 int
398 dwlpx_bus_dmamap_load_uio_direct(t, map, uio, flags)
399 bus_dma_tag_t t;
400 bus_dmamap_t map;
401 struct uio *uio;
402 int flags;
403 {
404
405 return (_bus_dmamap_load_uio_direct_common(t, map, uio,
406 flags, DWLPX_DIRECT_MAPPED_BASE));
407 }
408
409 /*
410 * Load a DWLPX SGMAP-mapped DMA map with a uio.
411 */
412 int
413 dwlpx_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
414 bus_dma_tag_t t;
415 bus_dmamap_t map;
416 struct uio *uio;
417 int flags;
418 {
419 struct dwlpx_config *ccp = t->_cookie;
420 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
421 int error;
422
423 error = pci_dma_sgmap_load_uio(t, map, uio, flags, &ccp->cc_sgmap, a);
424 if (error == 0) {
425 /*
426 * Invalidate DWLPX TLB.
427 */
428 /* XXX implement XXX */
429 }
430 return (error);
431 }
432
433 /*
434 * Load a DWLPX direct-mapped DMA map with raw memory.
435 */
436 int
437 dwlpx_bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
438 bus_dma_tag_t t;
439 bus_dmamap_t map;
440 bus_dma_segment_t *segs;
441 int nsegs;
442 bus_size_t size;
443 int flags;
444 {
445
446 return (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
447 size, flags, DWLPX_DIRECT_MAPPED_BASE));
448 }
449
450 /*
451 * Load a DWLPX SGMAP-mapped DMA map with raw memory.
452 */
453 int
454 dwlpx_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
455 bus_dma_tag_t t;
456 bus_dmamap_t map;
457 bus_dma_segment_t *segs;
458 int nsegs;
459 bus_size_t size;
460 int flags;
461 {
462 struct dwlpx_config *ccp = t->_cookie;
463 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
464 int error;
465
466 error = pci_dma_sgmap_load_raw(t, map, segs, nsegs, size, flags,
467 &ccp->cc_sgmap, a);
468 if (error == 0) {
469 /*
470 * Invalidate DWLPX TLB.
471 */
472 /* XXX implement XXX */
473 }
474 return (error);
475 }
476
477 /*
478 * Unload a DWLPX DMA map.
479 */
480 void
481 dwlpx_bus_dmamap_unload(t, map)
482 bus_dma_tag_t t;
483 bus_dmamap_t map;
484 {
485 struct dwlpx_config *ccp = t->_cookie;
486 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
487
488 /*
489 * Invalidate any SGMAP page table entries used by this
490 * mapping.
491 */
492 if (a->apdc_flags & APDC_USING_SGMAP) {
493 pci_dma_sgmap_unload(t, map, &ccp->cc_sgmap, a);
494 /* XXX invalidate TLB */
495 }
496
497 /*
498 * Do the generic bits of the unload.
499 */
500 _bus_dmamap_unload(t, map);
501 }
502