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