dwlpx_dma.c revision 1.4 1 /* $NetBSD: dwlpx_dma.c,v 1.4 1998/01/17 03:40:33 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 <sys/cdefs.h> /* RCS ID & Copyright macro defns */
41
42 __KERNEL_RCSID(0, "$NetBSD: dwlpx_dma.c,v 1.4 1998/01/17 03:40:33 thorpej Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/device.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/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 #include <alpha/tlsb/tlsbreg.h>
57 #include <alpha/tlsb/kftxxvar.h>
58 #include <alpha/tlsb/kftxxreg.h>
59 #include <alpha/pci/dwlpxreg.h>
60 #include <alpha/pci/dwlpxvar.h>
61 #include <alpha/pci/pci_kn8ae.h>
62
63 bus_dma_tag_t dwlpx_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
64
65 int dwlpx_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
66 bus_size_t, bus_size_t, int, bus_dmamap_t *));
67
68 void dwlpx_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
69
70 int dwlpx_bus_dmamap_load_direct __P((bus_dma_tag_t, bus_dmamap_t, void *,
71 bus_size_t, struct proc *, int));
72 int dwlpx_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
73 bus_size_t, struct proc *, int));
74
75 int dwlpx_bus_dmamap_load_mbuf_direct __P((bus_dma_tag_t, bus_dmamap_t,
76 struct mbuf *, int));
77 int dwlpx_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
78 struct mbuf *, int));
79
80 int dwlpx_bus_dmamap_load_uio_direct __P((bus_dma_tag_t, bus_dmamap_t,
81 struct uio *, int));
82 int dwlpx_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
83 struct uio *, int));
84
85 int dwlpx_bus_dmamap_load_raw_direct __P((bus_dma_tag_t, bus_dmamap_t,
86 bus_dma_segment_t *, int, bus_size_t, int));
87 int dwlpx_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
88 bus_dma_segment_t *, int, bus_size_t, int));
89
90 void dwlpx_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
91
92 /*
93 * The direct-mapped DMA window begins at this PCI address.
94 */
95 #define DWLPx_DIRECT_MAPPED_BASE 0x40000000
96
97 void
98 dwlpx_dma_init(ccp)
99 struct dwlpx_config *ccp;
100 {
101 char *exname;
102 bus_dma_tag_t t;
103 u_int32_t *page_table;
104 int i;
105
106 /*
107 * Initialize the DMA tag used for direct-mapped DMA.
108 */
109 t = &ccp->cc_dmat_direct;
110 t->_cookie = ccp;
111 t->_get_tag = dwlpx_dma_get_tag;
112 t->_dmamap_create = _bus_dmamap_create;
113 t->_dmamap_destroy = _bus_dmamap_destroy;
114 t->_dmamap_load = dwlpx_bus_dmamap_load_direct;
115 t->_dmamap_load_mbuf = dwlpx_bus_dmamap_load_mbuf_direct;
116 t->_dmamap_load_uio = dwlpx_bus_dmamap_load_uio_direct;
117 t->_dmamap_load_raw = dwlpx_bus_dmamap_load_raw_direct;
118 t->_dmamap_unload = _bus_dmamap_unload;
119 t->_dmamap_sync = NULL; /* Nothing to do. */
120
121 t->_dmamem_alloc = _bus_dmamem_alloc;
122 t->_dmamem_free = _bus_dmamem_free;
123 t->_dmamem_map = _bus_dmamem_map;
124 t->_dmamem_unmap = _bus_dmamem_unmap;
125 t->_dmamem_mmap = _bus_dmamem_mmap;
126
127 /*
128 * Initialize the DMA tag used for sgmap-mapped DMA.
129 */
130 t = &ccp->cc_dmat_sgmap;
131 t->_cookie = ccp;
132 t->_get_tag = dwlpx_dma_get_tag;
133 t->_dmamap_create = dwlpx_bus_dmamap_create_sgmap;
134 t->_dmamap_destroy = dwlpx_bus_dmamap_destroy_sgmap;
135 t->_dmamap_load = dwlpx_bus_dmamap_load_sgmap;
136 t->_dmamap_load_mbuf = dwlpx_bus_dmamap_load_mbuf_sgmap;
137 t->_dmamap_load_uio = dwlpx_bus_dmamap_load_uio_sgmap;
138 t->_dmamap_load_raw = dwlpx_bus_dmamap_load_raw_sgmap;
139 t->_dmamap_unload = dwlpx_bus_dmamap_unload_sgmap;
140 t->_dmamap_sync = NULL; /* Nothing to do. */
141
142 t->_dmamem_alloc = _bus_dmamem_alloc;
143 t->_dmamem_free = _bus_dmamem_free;
144 t->_dmamem_map = _bus_dmamem_map;
145 t->_dmamem_unmap = _bus_dmamem_unmap;
146 t->_dmamem_mmap = _bus_dmamem_mmap;
147
148 /*
149 * A few notes about SGMAP-mapped DMA on the DWLPx:
150 *
151 * The DWLPx has PCIA-resident SRAM that is used for
152 * the SGMAP page table; there is no TLB. The DWLPA
153 * has room for 32K entries, yielding a total of 256M
154 * of sgva space. The DWLPB has 32K entries or 128K
155 * entries, depending on TBIT, yielding wither 256M or
156 * 1G of sgva space.
157 *
158 * This sgva space must be shared across all windows
159 * that wish to use SGMAP-mapped DMA; make sure to
160 * adjust the "sgvabase" argument to alpha_sgmap_init()
161 * accordingly if you create more than one SGMAP-mapped
162 * window. Note that sgvabase != window base. The former
163 * is used to compute indexes into the page table only.
164 *
165 * In the current implementation, we follow the lead of
166 * the workstation chipsets; the first window is an 8M
167 * window SGMAP-mapped mapped at 8M, and the second window
168 * is a 1G window direct-mapped mapped at 1G.
169 */
170
171 /*
172 * Initialize the page table.
173 */
174 page_table =
175 (u_int32_t *)ALPHA_PHYS_TO_K0SEG(PCIA_SGMAP_PT + ccp->cc_sysbase);
176 for (i = 0; i < (32*1024); i++)
177 page_table[i] = 0;
178 alpha_mb();
179
180 /*
181 * Initialize the SGMAP for window A:
182 *
183 * Size: 8M
184 * Window base: 8M
185 * SGVA base: 0
186 */
187 exname = malloc(16, M_DEVBUF, M_NOWAIT);
188 if (exname == NULL)
189 panic("dwlpx_dma_init");
190 sprintf(exname, "%s_sgmap_a", ccp->cc_sc->dwlpx_dev.dv_xname);
191 alpha_sgmap_init(t, &ccp->cc_sgmap, exname,
192 (8*1024*1024), 0, (8*1024*1024), sizeof(u_int32_t),
193 (void *)page_table, 0);
194
195 /*
196 * Set up DMA windows for this DWLPx.
197 *
198 * Do this even for all HPCs- even for the nonexistent
199 * one on hose zero of a KFTIA.
200 */
201 for (i = 0; i < NHPC; i++) {
202 REGVAL(PCIA_WMASK_A(i) + ccp->cc_sysbase) = PCIA_WMASK_8M;
203 REGVAL(PCIA_TBASE_A(i) + ccp->cc_sysbase) = 0;
204 alpha_mb();
205 REGVAL(PCIA_WBASE_A(i) + ccp->cc_sysbase) =
206 (8*1024*1024) | PCIA_WBASE_W_EN | PCIA_WBASE_SG_EN;
207 alpha_mb();
208
209 REGVAL(PCIA_WMASK_B(i) + ccp->cc_sysbase) = PCIA_WMASK_1G;
210 REGVAL(PCIA_TBASE_B(i) + ccp->cc_sysbase) = 0;
211 alpha_mb();
212 REGVAL(PCIA_WBASE_B(i) + ccp->cc_sysbase) =
213 DWLPx_DIRECT_MAPPED_BASE | PCIA_WBASE_W_EN;
214 alpha_mb();
215
216 REGVAL(PCIA_WMASK_C(i) + ccp->cc_sysbase) = 0;
217 REGVAL(PCIA_TBASE_C(i) + ccp->cc_sysbase) = 0;
218 alpha_mb();
219 REGVAL(PCIA_WBASE_C(i) + ccp->cc_sysbase) = 0;
220 alpha_mb();
221 }
222
223 /* XXX XXX BEGIN XXX XXX */
224 { /* XXX */
225 extern vm_offset_t alpha_XXX_dmamap_or; /* XXX */
226 alpha_XXX_dmamap_or = DWLPx_DIRECT_MAPPED_BASE; /* XXX */
227 } /* XXX */
228 /* XXX XXX END XXX XXX */
229 }
230
231 /*
232 * Return the bus dma tag to be used for the specified bus type.
233 * INTERNAL USE ONLY!
234 */
235 bus_dma_tag_t
236 dwlpx_dma_get_tag(t, bustype)
237 bus_dma_tag_t t;
238 alpha_bus_t bustype;
239 {
240 struct dwlpx_config *ccp = t->_cookie;
241
242 switch (bustype) {
243 case ALPHA_BUS_PCI:
244 case ALPHA_BUS_EISA:
245 /*
246 * XXX FIXME!
247 * XXX If the system has more than 1G of RAM,
248 * XXX we need to use SGMAPs, or some combination
249 * XXX of direct-mapped and SGMAP-mapped DMA.
250 */
251 return (&ccp->cc_dmat_direct);
252
253 case ALPHA_BUS_ISA:
254 /*
255 * ISA doesn't have enough address bits to use
256 * the direct-mapped DMA window, so we must use
257 * SGMAPs.
258 */
259 return (&ccp->cc_dmat_sgmap);
260
261 default:
262 panic("dwlpx_dma_get_tag: shouldn't be here, really...");
263 }
264 }
265
266 /*
267 * Create a DWLPx SGMAP-mapped DMA map.
268 */
269 int
270 dwlpx_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
271 flags, dmamp)
272 bus_dma_tag_t t;
273 bus_size_t size;
274 int nsegments;
275 bus_size_t maxsegsz;
276 bus_size_t boundary;
277 int flags;
278 bus_dmamap_t *dmamp;
279 {
280 struct dwlpx_config *ccp = t->_cookie;
281 struct alpha_sgmap_cookie *a;
282 bus_dmamap_t map;
283 int error;
284
285 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
286 boundary, flags, dmamp);
287 if (error)
288 return (error);
289
290 map = *dmamp;
291
292 a = malloc(sizeof(struct alpha_sgmap_cookie), M_DEVBUF,
293 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
294 if (a == NULL) {
295 _bus_dmamap_destroy(t, map);
296 return (ENOMEM);
297 }
298 bzero(a, sizeof(struct alpha_sgmap_cookie));
299 map->_dm_sgcookie = a;
300
301 if (flags & BUS_DMA_ALLOCNOW) {
302 error = alpha_sgmap_alloc(map, round_page(size),
303 &ccp->cc_sgmap, flags);
304 if (error)
305 dwlpx_bus_dmamap_destroy_sgmap(t, map);
306 }
307
308 return (error);
309 }
310
311 /*
312 * Destroy a DWLPx SGMAP-mapped DMA map.
313 */
314 void
315 dwlpx_bus_dmamap_destroy_sgmap(t, map)
316 bus_dma_tag_t t;
317 bus_dmamap_t map;
318 {
319 struct dwlpx_config *ccp = t->_cookie;
320 struct alpha_sgmap_cookie *a = map->_dm_sgcookie;
321
322 if (a->apdc_flags & APDC_HAS_SGMAP)
323 alpha_sgmap_free(&ccp->cc_sgmap, a);
324
325 free(a, M_DEVBUF);
326 _bus_dmamap_destroy(t, map);
327 }
328
329 /*
330 * Load a DWLPx direct-mapped DMA map with a linear buffer.
331 */
332 int
333 dwlpx_bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
334 bus_dma_tag_t t;
335 bus_dmamap_t map;
336 void *buf;
337 bus_size_t buflen;
338 struct proc *p;
339 int flags;
340 {
341
342 return (_bus_dmamap_load_direct_common(t, map, buf, buflen, p,
343 flags, DWLPx_DIRECT_MAPPED_BASE));
344 }
345
346 /*
347 * Load a DWLPx SGMAP-mapped DMA map with a linear buffer.
348 */
349 int
350 dwlpx_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
351 bus_dma_tag_t t;
352 bus_dmamap_t map;
353 void *buf;
354 bus_size_t buflen;
355 struct proc *p;
356 int flags;
357 {
358 struct dwlpx_config *ccp = t->_cookie;
359
360 return (pci_sgmap_pte32_load(t, map, buf, buflen, p, flags,
361 &ccp->cc_sgmap));
362 }
363
364 /*
365 * Load a DWLPx direct-mapped DMA map with an mbuf chain.
366 */
367 int
368 dwlpx_bus_dmamap_load_mbuf_direct(t, map, m, flags)
369 bus_dma_tag_t t;
370 bus_dmamap_t map;
371 struct mbuf *m;
372 int flags;
373 {
374
375 return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
376 flags, DWLPx_DIRECT_MAPPED_BASE));
377 }
378
379 /*
380 * Load a DWLPx SGMAP-mapped DMA map with an mbuf chain.
381 */
382 int
383 dwlpx_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
384 bus_dma_tag_t t;
385 bus_dmamap_t map;
386 struct mbuf *m;
387 int flags;
388 {
389 struct dwlpx_config *ccp = t->_cookie;
390
391 return (pci_sgmap_pte32_load_mbuf(t, map, m, flags, &ccp->cc_sgmap));
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
421 return (pci_sgmap_pte32_load_uio(t, map, uio, flags, &ccp->cc_sgmap));
422 }
423
424 /*
425 * Load a DWLPx direct-mapped DMA map with raw memory.
426 */
427 int
428 dwlpx_bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
429 bus_dma_tag_t t;
430 bus_dmamap_t map;
431 bus_dma_segment_t *segs;
432 int nsegs;
433 bus_size_t size;
434 int flags;
435 {
436
437 return (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
438 size, flags, DWLPx_DIRECT_MAPPED_BASE));
439 }
440
441 /*
442 * Load a DWLPx SGMAP-mapped DMA map with raw memory.
443 */
444 int
445 dwlpx_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
446 bus_dma_tag_t t;
447 bus_dmamap_t map;
448 bus_dma_segment_t *segs;
449 int nsegs;
450 bus_size_t size;
451 int flags;
452 {
453 struct dwlpx_config *ccp = t->_cookie;
454
455 return (pci_sgmap_pte32_load_raw(t, map, segs, nsegs, size, flags,
456 &ccp->cc_sgmap));
457 }
458
459 /*
460 * Unload a DWLPx DMA map.
461 */
462 void
463 dwlpx_bus_dmamap_unload_sgmap(t, map)
464 bus_dma_tag_t t;
465 bus_dmamap_t map;
466 {
467 struct dwlpx_config *ccp = t->_cookie;
468
469 /*
470 * Invalidate any SGMAP page table entries used by this
471 * mapping.
472 */
473 pci_sgmap_pte32_unload(t, map, &ccp->cc_sgmap);
474
475 /*
476 * Do the generic bits of the unload.
477 */
478 _bus_dmamap_unload(t, map);
479 }
480