cia_dma.c revision 1.1.2.1 1 /* $NetBSD: cia_dma.c,v 1.1.2.1 1997/05/23 21:44:42 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: cia_dma.c,v 1.1.2.1 1997/05/23 21:44:42 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/ciareg.h>
58 #include <alpha/pci/ciavar.h>
59
60 bus_dma_tag_t cia_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
61
62 int cia_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int,
63 bus_size_t, bus_size_t, int, bus_dmamap_t *));
64 int cia_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
65 bus_size_t, bus_size_t, int, bus_dmamap_t *));
66
67 void cia_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
68
69 int cia_bus_dmamap_load_direct __P((bus_dma_tag_t, bus_dmamap_t, void *,
70 bus_size_t, struct proc *, int));
71 int cia_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
72 bus_size_t, struct proc *, int));
73
74 int cia_bus_dmamap_load_mbuf_direct __P((bus_dma_tag_t, bus_dmamap_t,
75 struct mbuf *, int));
76 int cia_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
77 struct mbuf *, int));
78
79 int cia_bus_dmamap_load_uio_direct __P((bus_dma_tag_t, bus_dmamap_t,
80 struct uio *, int));
81 int cia_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
82 struct uio *, int));
83
84 int cia_bus_dmamap_load_raw_direct __P((bus_dma_tag_t, bus_dmamap_t,
85 bus_dma_segment_t *, int, bus_size_t, int));
86 int cia_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
87 bus_dma_segment_t *, int, bus_size_t, int));
88
89 void cia_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
90
91 /*
92 * The direct-mapped DMA window begins at this PCI address.
93 */
94 #define CIA_DIRECT_MAPPED_BASE 0x40000000
95
96 /*
97 * The 8M SGMAP-mapped DMA window begins at this PCI address.
98 */
99 #define CIA_SGMAP_MAPPED_BASE (8*1024*1024)
100
101 /*
102 * Macro to flush CIA scatter/gather TLB.
103 */
104 #define CIA_TLB_INVALIDATE() \
105 do { \
106 REGVAL(CIA_PCI_TBIA) = CIA_PCI_TBIA_ALL; \
107 alpha_mb(); \
108 } while (0)
109
110 void
111 cia_dma_init(ccp)
112 struct cia_config *ccp;
113 {
114 bus_addr_t tbase;
115 u_int32_t memcs_en;
116 bus_dma_tag_t t;
117
118 /*
119 * Initialize the DMA tag used for direct-mapped DMA.
120 */
121 t = &ccp->cc_dmat_direct;
122 t->_cookie = ccp;
123 t->_get_tag = cia_dma_get_tag;
124 t->_dmamap_create = cia_bus_dmamap_create;
125 t->_dmamap_destroy = cia_bus_dmamap_destroy;
126 t->_dmamap_load = cia_bus_dmamap_load_direct;
127 t->_dmamap_load_mbuf = cia_bus_dmamap_load_mbuf_direct;
128 t->_dmamap_load_uio = cia_bus_dmamap_load_uio_direct;
129 t->_dmamap_load_raw = cia_bus_dmamap_load_raw_direct;
130 t->_dmamap_unload = cia_bus_dmamap_unload;
131 t->_dmamap_sync = NULL; /* Nothing to do. */
132
133 t->_dmamem_alloc = _bus_dmamem_alloc;
134 t->_dmamem_free = _bus_dmamem_free;
135 t->_dmamem_map = _bus_dmamem_map;
136 t->_dmamem_unmap = _bus_dmamem_unmap;
137 t->_dmamem_mmap = _bus_dmamem_mmap;
138
139 /*
140 * Initialize the DMA tag used for sgmap-mapped DMA.
141 */
142 t = &ccp->cc_dmat_sgmap;
143 t->_cookie = ccp;
144 t->_get_tag = cia_dma_get_tag;
145 t->_dmamap_create = cia_bus_dmamap_create_sgmap;
146 t->_dmamap_destroy = cia_bus_dmamap_destroy;
147 t->_dmamap_load = cia_bus_dmamap_load_sgmap;
148 t->_dmamap_load_mbuf = cia_bus_dmamap_load_mbuf_sgmap;
149 t->_dmamap_load_uio = cia_bus_dmamap_load_uio_sgmap;
150 t->_dmamap_load_raw = cia_bus_dmamap_load_raw_sgmap;
151 t->_dmamap_unload = cia_bus_dmamap_unload;
152 t->_dmamap_sync = NULL; /* Nothing to do. */
153
154 t->_dmamem_alloc = _bus_dmamem_alloc;
155 t->_dmamem_free = _bus_dmamem_free;
156 t->_dmamem_map = _bus_dmamem_map;
157 t->_dmamem_unmap = _bus_dmamem_unmap;
158 t->_dmamem_mmap = _bus_dmamem_mmap;
159
160 /*
161 * The firmware has set up window 1 as a 1G direct-mapped DMA
162 * window beginning at 1G. We leave it alone. Leave window
163 * 0 alone until we reconfigure it for SGMAP-mapped DMA.
164 * Windows 2 and 3 are already disabled.
165 */
166
167 /*
168 * Initialize the SGMAP if safe to do so.
169 */
170 if (ccp->cc_mallocsafe) {
171 pci_dma_sgmap_init(t, &ccp->cc_sgmap, "cia_sgmap",
172 CIA_SGMAP_MAPPED_BASE, (8*1024*1024));
173
174 /* Remember the MEMCS value. */
175 alpha_mb();
176 memcs_en = REGVAL(CIA_PCI_W0BASE) & CIA_PCI_WnBASE_MEMCS_EN;
177
178 /* Now disable window 0. */
179 REGVAL(CIA_PCI_W0BASE) = 0;
180 alpha_mb();
181
182 /*
183 * Set up window 0 as an 8MB SGMAP-mapped window
184 * starting at 8MB.
185 */
186 tbase = ccp->cc_sgmap.aps_ptpa >> CIA_PCI_TnBASE_SHIFT;
187 if ((tbase & CIA_PCI_TnBASE_MASK) != tbase)
188 panic("cia_dma_init: bad page table address");
189 REGVAL(CIA_PCI_T0BASE) = tbase;
190 REGVAL(CIA_PCI_W0MASK) = CIA_PCI_WnMASK_8M;
191 alpha_mb();
192
193 REGVAL(CIA_PCI_W0BASE) = CIA_SGMAP_MAPPED_BASE |
194 CIA_PCI_WnBASE_SG_EN | CIA_PCI_WnBASE_W_EN | memcs_en;
195 alpha_mb();
196
197 CIA_TLB_INVALIDATE();
198 }
199
200 /* XXX XXX BEGIN XXX XXX */
201 { /* XXX */
202 extern vm_offset_t alpha_XXX_dmamap_or; /* XXX */
203 alpha_XXX_dmamap_or = CIA_DIRECT_MAPPED_BASE; /* XXX */
204 } /* XXX */
205 /* XXX XXX END XXX XXX */
206 }
207
208 /*
209 * Return the bus dma tag to be used for the specified bus type.
210 * INTERNAL USE ONLY!
211 */
212 bus_dma_tag_t
213 cia_dma_get_tag(t, bustype)
214 bus_dma_tag_t t;
215 alpha_bus_t bustype;
216 {
217 struct cia_config *ccp = t->_cookie;
218
219 switch (bustype) {
220 case ALPHA_BUS_PCI:
221 case ALPHA_BUS_EISA:
222 /*
223 * Systems with a CIA can only support 1G
224 * of memory, so we use the direct-mapped window
225 * on busses that have 32-bit DMA.
226 */
227 return (&ccp->cc_dmat_direct);
228
229 case ALPHA_BUS_ISA:
230 /*
231 * ISA doesn't have enough address bits to use
232 * the direct-mapped DMA window, so we must use
233 * SGMAPs.
234 */
235 return (&ccp->cc_dmat_sgmap);
236
237 default:
238 panic("cia_dma_get_tag: shouldn't be here, really...");
239 }
240 }
241
242 /*
243 * Create a CIA DMA map.
244 */
245 int
246 cia_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
247 bus_dma_tag_t t;
248 bus_size_t size;
249 int nsegments;
250 bus_size_t maxsegsz;
251 bus_size_t boundary;
252 int flags;
253 bus_dmamap_t *dmamp;
254 {
255 struct alpha_pci_dma_cookie *a;
256 bus_dmamap_t map;
257 int error;
258
259 /* Call common function to create the basic map. */
260 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
261 flags, dmamp);
262 if (error)
263 return (error);
264
265 map = *dmamp;
266
267 /* Allocate PCI-specific housekeeping stuff. */
268 a = malloc(sizeof(struct alpha_pci_dma_cookie), M_DEVBUF,
269 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
270 if (a == NULL) {
271 _bus_dmamap_destroy(t, map);
272 return (ENOMEM);
273 }
274 bzero(a, sizeof(struct alpha_pci_dma_cookie));
275 map->_dm_cookie = a;
276
277 return (0);
278 }
279
280 int
281 cia_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
282 flags, dmamp)
283 bus_dma_tag_t t;
284 bus_size_t size;
285 int nsegments;
286 bus_size_t maxsegsz;
287 bus_size_t boundary;
288 int flags;
289 bus_dmamap_t *dmamp;
290 {
291 struct cia_config *ccp = t->_cookie;
292 struct alpha_pci_dma_cookie *a;
293 bus_dmamap_t map;
294 int error;
295
296 error = cia_bus_dmamap_create(t, size, nsegments, maxsegsz,
297 boundary, flags, dmamp);
298 if (error)
299 return (error);
300
301 map = *dmamp;
302 a = map->_dm_cookie;
303
304 if (flags & BUS_DMA_ALLOCNOW)
305 error = pci_dma_sgmap_alloc(map, round_page(size),
306 &ccp->cc_sgmap, a, flags);
307
308 return (error);
309 }
310
311 /*
312 * Destroy a CIA DMA map.
313 */
314 void
315 cia_bus_dmamap_destroy(t, map)
316 bus_dma_tag_t t;
317 bus_dmamap_t map;
318 {
319 struct cia_config *ccp = t->_cookie;
320 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
321
322 if (a->apdc_flags & APDC_HAS_SGMAP)
323 pci_dma_sgmap_free(&ccp->cc_sgmap, a);
324
325 free(map->_dm_cookie, M_DEVBUF);
326 _bus_dmamap_destroy(t, map);
327 }
328
329 /*
330 * Load a CIA direct-mapped DMA map with a linear buffer.
331 */
332 int
333 cia_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, CIA_DIRECT_MAPPED_BASE));
344 }
345
346 /*
347 * Load a CIA SGMAP-mapped DMA map with a linear buffer.
348 */
349 int
350 cia_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 cia_config *ccp = t->_cookie;
359 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
360 int error;
361
362 error = pci_dma_sgmap_load(t, map, buf, buflen, p, flags,
363 &ccp->cc_sgmap, a);
364 if (error == 0)
365 CIA_TLB_INVALIDATE();
366
367 return (error);
368 }
369
370 /*
371 * Load a CIA direct-mapped DMA map with an mbuf chain.
372 */
373 int
374 cia_bus_dmamap_load_mbuf_direct(t, map, m, flags)
375 bus_dma_tag_t t;
376 bus_dmamap_t map;
377 struct mbuf *m;
378 int flags;
379 {
380
381 return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
382 flags, CIA_DIRECT_MAPPED_BASE));
383 }
384
385 /*
386 * Load a CIA SGMAP-mapped DMA map with an mbuf chain.
387 */
388 int
389 cia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
390 bus_dma_tag_t t;
391 bus_dmamap_t map;
392 struct mbuf *m;
393 int flags;
394 {
395 struct cia_config *ccp = t->_cookie;
396 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
397 int error;
398
399 error = pci_dma_sgmap_load_mbuf(t, map, m, flags, &ccp->cc_sgmap, a);
400 if (error == 0)
401 CIA_TLB_INVALIDATE();
402
403 return (error);
404 }
405
406 /*
407 * Load a CIA direct-mapped DMA map with a uio.
408 */
409 int
410 cia_bus_dmamap_load_uio_direct(t, map, uio, flags)
411 bus_dma_tag_t t;
412 bus_dmamap_t map;
413 struct uio *uio;
414 int flags;
415 {
416
417 return (_bus_dmamap_load_uio_direct_common(t, map, uio,
418 flags, CIA_DIRECT_MAPPED_BASE));
419 }
420
421 /*
422 * Load a CIA SGMAP-mapped DMA map with a uio.
423 */
424 int
425 cia_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
426 bus_dma_tag_t t;
427 bus_dmamap_t map;
428 struct uio *uio;
429 int flags;
430 {
431 struct cia_config *ccp = t->_cookie;
432 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
433 int error;
434
435 error = pci_dma_sgmap_load_uio(t, map, uio, flags, &ccp->cc_sgmap, a);
436 if (error == 0)
437 CIA_TLB_INVALIDATE();
438
439 return (error);
440 }
441
442 /*
443 * Load a CIA direct-mapped DMA map with raw memory.
444 */
445 int
446 cia_bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
447 bus_dma_tag_t t;
448 bus_dmamap_t map;
449 bus_dma_segment_t *segs;
450 int nsegs;
451 bus_size_t size;
452 int flags;
453 {
454
455 return (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
456 size, flags, CIA_DIRECT_MAPPED_BASE));
457 }
458
459 /*
460 * Load a CIA SGMAP-mapped DMA map with raw memory.
461 */
462 int
463 cia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
464 bus_dma_tag_t t;
465 bus_dmamap_t map;
466 bus_dma_segment_t *segs;
467 int nsegs;
468 bus_size_t size;
469 int flags;
470 {
471 struct cia_config *ccp = t->_cookie;
472 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
473 int error;
474
475 error = pci_dma_sgmap_load_raw(t, map, segs, nsegs, size, flags,
476 &ccp->cc_sgmap, a);
477 if (error == 0)
478 CIA_TLB_INVALIDATE();
479
480 return (error);
481 }
482
483 /*
484 * Unload a CIA DMA map.
485 */
486 void
487 cia_bus_dmamap_unload(t, map)
488 bus_dma_tag_t t;
489 bus_dmamap_t map;
490 {
491 struct cia_config *ccp = t->_cookie;
492 struct alpha_pci_dma_cookie *a = map->_dm_cookie;
493
494 /*
495 * Invalidate any SGMAP page table entries used by this
496 * mapping.
497 */
498 if (a->apdc_flags & APDC_USING_SGMAP) {
499 pci_dma_sgmap_unload(t, map, &ccp->cc_sgmap, a);
500 CIA_TLB_INVALIDATE();
501 }
502
503 /*
504 * Do the generic bits of the unload.
505 */
506 _bus_dmamap_unload(t, map);
507 }
508