dwlpx_dma.c revision 1.12.6.1 1 /* $NetBSD: dwlpx_dma.c,v 1.12.6.1 1999/04/07 08:12:40 pk 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.
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: dwlpx_dma.c,v 1.12.6.1 1999/04/07 08:12:40 pk 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_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
71 bus_size_t, struct proc *, int));
72
73 int dwlpx_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
74 struct mbuf *, int));
75
76 int dwlpx_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
77 struct uio *, int));
78
79 int dwlpx_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
80 bus_dma_segment_t *, int, bus_size_t, int));
81
82 void dwlpx_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
83
84 /*
85 * Direct-mapped window: 2G at 2G
86 */
87 #define DWLPx_DIRECT_MAPPED_BASE (2UL*1024UL*1024UL*1024UL)
88 #define DWLPx_DIRECT_MAPPED_SIZE (2UL*1024UL*1024UL*1024UL)
89
90 /*
91 * SGMAP window A: 256M or 1G at 1G
92 */
93 #define DWLPx_SG_MAPPED_BASE (1*1024*1024*1024)
94 #define DWLPx_SG_MAPPED_SIZE(x) ((x) * PAGE_SIZE)
95
96 /*
97 * Set this to always use SGMAPs.
98 */
99 #ifdef DWLPX_ALWAYS_USE_SGMAP
100 int dwlpx_always_use_sgmap = 1;
101 #else
102 int dwlpx_always_use_sgmap = 0;
103 #endif
104
105 void
106 dwlpx_dma_init(ccp)
107 struct dwlpx_config *ccp;
108 {
109 char *exname;
110 bus_dma_tag_t t;
111 u_int32_t *page_table;
112 int i, lim, wmask;
113
114 /*
115 * Determine size of Window C based on the amount of SGMAP
116 * page table SRAM available.
117 */
118 if (ccp->cc_sc->dwlpx_sgmapsz == DWLPX_SG128K) {
119 lim = 128 * 1024;
120 wmask = PCIA_WMASK_1G;
121 } else {
122 lim = 32 * 1024;
123 wmask = PCIA_WMASK_256M;
124 }
125
126 /*
127 * Initialize the DMA tag used for direct-mapped DMA. Chain
128 * this window to the SGMAP window, because we might have
129 * a lot of memory in the machine.
130 */
131 t = &ccp->cc_dmat_direct;
132 t->_cookie = ccp;
133 t->_wbase = DWLPx_DIRECT_MAPPED_BASE;
134 t->_wsize = DWLPx_DIRECT_MAPPED_SIZE;
135 t->_next_window = &ccp->cc_dmat_sgmap;
136 t->_boundary = 0;
137 t->_sgmap = NULL;
138 t->_get_tag = dwlpx_dma_get_tag;
139 t->_dmamap_create = _bus_dmamap_create;
140 t->_dmamap_destroy = _bus_dmamap_destroy;
141 t->_dmamap_load = _bus_dmamap_load_direct;
142 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
143 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
144 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
145 t->_dmamap_unload = _bus_dmamap_unload;
146 t->_dmamap_sync = _bus_dmamap_sync;
147
148 t->_dmamem_alloc = _bus_dmamem_alloc;
149 t->_dmamem_free = _bus_dmamem_free;
150 t->_dmamem_map = _bus_dmamem_map;
151 t->_dmamem_unmap = _bus_dmamem_unmap;
152 t->_dmamem_mmap = _bus_dmamem_mmap;
153
154 /*
155 * Initialize the DMA tag used for sgmap-mapped DMA.
156 */
157 t = &ccp->cc_dmat_sgmap;
158 t->_cookie = ccp;
159 t->_wbase = DWLPx_SG_MAPPED_BASE;
160 t->_wsize = DWLPx_SG_MAPPED_SIZE(lim);
161 t->_next_window = NULL;
162 t->_boundary = 0;
163 t->_sgmap = &ccp->cc_sgmap;
164 t->_get_tag = dwlpx_dma_get_tag;
165 t->_dmamap_create = dwlpx_bus_dmamap_create_sgmap;
166 t->_dmamap_destroy = dwlpx_bus_dmamap_destroy_sgmap;
167 t->_dmamap_load = dwlpx_bus_dmamap_load_sgmap;
168 t->_dmamap_load_mbuf = dwlpx_bus_dmamap_load_mbuf_sgmap;
169 t->_dmamap_load_uio = dwlpx_bus_dmamap_load_uio_sgmap;
170 t->_dmamap_load_raw = dwlpx_bus_dmamap_load_raw_sgmap;
171 t->_dmamap_unload = dwlpx_bus_dmamap_unload_sgmap;
172 t->_dmamap_sync = _bus_dmamap_sync;
173
174 t->_dmamem_alloc = _bus_dmamem_alloc;
175 t->_dmamem_free = _bus_dmamem_free;
176 t->_dmamem_map = _bus_dmamem_map;
177 t->_dmamem_unmap = _bus_dmamem_unmap;
178 t->_dmamem_mmap = _bus_dmamem_mmap;
179
180 /*
181 * A few notes about SGMAP-mapped DMA on the DWLPx:
182 *
183 * The DWLPx has PCIA-resident SRAM that is used for
184 * the SGMAP page table; there is no TLB. The DWLPA
185 * has room for 32K entries, yielding a total of 256M
186 * of sgva space. The DWLPB has 32K entries or 128K
187 * entries, depending on TBIT, yielding either 256M or
188 * 1G of sgva space.
189 *
190 * This sgva space must be shared across all windows
191 * that wish to use SGMAP-mapped DMA; make sure to
192 * adjust the "sgvabase" argument to alpha_sgmap_init()
193 * accordingly if you create more than one SGMAP-mapped
194 * window. Note that sgvabase != window base. The former
195 * is used to compute indexes into the page table only.
196 *
197 * In the current implementation the first window is a
198 * 2G window direct-mapped mapped at 2G and the second
199 * window is disabled and the third window is a 256M
200 * or 1GB scatter/gather map at 1GB.
201 *
202 * We just punt on trying to support ISA DMA. If you want
203 * try that on a TurboLaser, well, you just lose.
204 */
205
206 /*
207 * Initialize the page table.
208 */
209 page_table =
210 (u_int32_t *)ALPHA_PHYS_TO_K0SEG(PCIA_SGMAP_PT + ccp->cc_sysbase);
211 for (i = 0; i < lim; i++)
212 page_table[i * SGMAP_PTE_SPACING] = 0;
213 alpha_mb();
214
215 /*
216 * Initialize the SGMAP for window C:
217 *
218 * Size: 256M or 1GB
219 * Window base: 1GB
220 * SGVA base: 0
221 */
222 exname = malloc(16, M_DEVBUF, M_NOWAIT);
223 if (exname == NULL)
224 panic("dwlpx_dma_init");
225 sprintf(exname, "%s_sgmap_a", ccp->cc_sc->dwlpx_dev.dv_xname);
226 alpha_sgmap_init(t, &ccp->cc_sgmap, exname, DWLPx_SG_MAPPED_BASE,
227 0, DWLPx_SG_MAPPED_SIZE(lim), sizeof(u_int32_t),
228 (void *)page_table, 0);
229
230 /*
231 * Set up DMA windows for this DWLPx.
232 *
233 * Do this even for all HPCs- even for the nonexistent
234 * one on hose zero of a KFTIA.
235 */
236 for (i = 0; i < NHPC; i++) {
237 REGVAL(PCIA_WMASK_A(i) + ccp->cc_sysbase) = PCIA_WMASK_2G;
238 REGVAL(PCIA_TBASE_A(i) + ccp->cc_sysbase) = 0;
239 REGVAL(PCIA_WBASE_A(i) + ccp->cc_sysbase) =
240 DWLPx_DIRECT_MAPPED_BASE | PCIA_WBASE_W_EN;
241
242 REGVAL(PCIA_WMASK_B(i) + ccp->cc_sysbase) = 0;
243 REGVAL(PCIA_TBASE_B(i) + ccp->cc_sysbase) = 0;
244 REGVAL(PCIA_WBASE_B(i) + ccp->cc_sysbase) = 0;
245
246 REGVAL(PCIA_WMASK_C(i) + ccp->cc_sysbase) = wmask;
247 REGVAL(PCIA_TBASE_C(i) + ccp->cc_sysbase) = 0;
248 REGVAL(PCIA_WBASE_C(i) + ccp->cc_sysbase) =
249 DWLPx_SG_MAPPED_BASE | PCIA_WBASE_W_EN | PCIA_WBASE_SG_EN;
250 alpha_mb();
251 }
252
253 /* XXX XXX BEGIN XXX XXX */
254 { /* XXX */
255 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
256 alpha_XXX_dmamap_or = DWLPx_DIRECT_MAPPED_BASE; /* XXX */
257 } /* XXX */
258 /* XXX XXX END XXX XXX */
259 }
260
261 /*
262 * Return the bus dma tag to be used for the specified bus type.
263 * INTERNAL USE ONLY!
264 */
265 bus_dma_tag_t
266 dwlpx_dma_get_tag(t, bustype)
267 bus_dma_tag_t t;
268 alpha_bus_t bustype;
269 {
270 struct dwlpx_config *ccp = t->_cookie;
271
272 switch (bustype) {
273 case ALPHA_BUS_PCI:
274 case ALPHA_BUS_EISA:
275 if (dwlpx_always_use_sgmap) {
276 /*
277 * Always use SGMAPs (for testing purposes?)
278 */
279 return (&ccp->cc_dmat_sgmap);
280 }
281 /*
282 * Give these busses the direct-mapped window; if the
283 * address crosses the threshold, it will fall back
284 * onto the SGMAP window.
285 */
286 return (&ccp->cc_dmat_direct);
287
288 case ALPHA_BUS_ISA:
289 /*
290 * Don't even bother; it's not easy, given how the
291 * page table is arranged, and it's not really worth
292 * the trouble.
293 */
294 panic("dwlpx_dma_get_tag: ISA DMA? Forget it...");
295 /* NOTREACHED */
296
297 default:
298 panic("dwlpx_dma_get_tag: shouldn't be here, really...");
299 }
300 }
301
302 /*
303 * Create a DWLPx SGMAP-mapped DMA map.
304 */
305 int
306 dwlpx_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
307 flags, dmamp)
308 bus_dma_tag_t t;
309 bus_size_t size;
310 int nsegments;
311 bus_size_t maxsegsz;
312 bus_size_t boundary;
313 int flags;
314 bus_dmamap_t *dmamp;
315 {
316 bus_dmamap_t map;
317 int error;
318
319 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
320 boundary, flags, dmamp);
321 if (error)
322 return (error);
323
324 map = *dmamp;
325
326 if (flags & BUS_DMA_ALLOCNOW) {
327 error = alpha_sgmap_alloc(map, round_page(size),
328 t->_sgmap, flags);
329 if (error)
330 dwlpx_bus_dmamap_destroy_sgmap(t, map);
331 }
332
333 return (error);
334 }
335
336 /*
337 * Destroy a DWLPx SGMAP-mapped DMA map.
338 */
339 void
340 dwlpx_bus_dmamap_destroy_sgmap(t, map)
341 bus_dma_tag_t t;
342 bus_dmamap_t map;
343 {
344
345 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
346 alpha_sgmap_free(map, t->_sgmap);
347
348 _bus_dmamap_destroy(t, map);
349 }
350
351 /*
352 * Load a DWLPx SGMAP-mapped DMA map with a linear buffer.
353 */
354 int
355 dwlpx_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
356 bus_dma_tag_t t;
357 bus_dmamap_t map;
358 void *buf;
359 bus_size_t buflen;
360 struct proc *p;
361 int flags;
362 {
363
364 return (pci_sgmap_pte32_load(t, map, buf, buflen, p, flags,
365 t->_sgmap));
366 }
367
368 /*
369 * Load a DWLPx SGMAP-mapped DMA map with an mbuf chain.
370 */
371 int
372 dwlpx_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
373 bus_dma_tag_t t;
374 bus_dmamap_t map;
375 struct mbuf *m;
376 int flags;
377 {
378
379 return (pci_sgmap_pte32_load_mbuf(t, map, m, flags, t->_sgmap));
380 }
381
382 /*
383 * Load a DWLPx SGMAP-mapped DMA map with a uio.
384 */
385 int
386 dwlpx_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
387 bus_dma_tag_t t;
388 bus_dmamap_t map;
389 struct uio *uio;
390 int flags;
391 {
392
393 return (pci_sgmap_pte32_load_uio(t, map, uio, flags, t->_sgmap));
394 }
395
396 /*
397 * Load a DWLPx SGMAP-mapped DMA map with raw memory.
398 */
399 int
400 dwlpx_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
401 bus_dma_tag_t t;
402 bus_dmamap_t map;
403 bus_dma_segment_t *segs;
404 int nsegs;
405 bus_size_t size;
406 int flags;
407 {
408
409 return (pci_sgmap_pte32_load_raw(t, map, segs, nsegs, size, flags,
410 t->_sgmap));
411 }
412
413 /*
414 * Unload a DWLPx DMA map.
415 */
416 void
417 dwlpx_bus_dmamap_unload_sgmap(t, map)
418 bus_dma_tag_t t;
419 bus_dmamap_t map;
420 {
421
422 /*
423 * Invalidate any SGMAP page table entries used by this
424 * mapping.
425 */
426 pci_sgmap_pte32_unload(t, map, t->_sgmap);
427
428 /*
429 * Do the generic bits of the unload.
430 */
431 _bus_dmamap_unload(t, map);
432 }
433