lca_dma.c revision 1.2 1 /* $NetBSD: lca_dma.c,v 1.2 1997/06/06 23:59:26 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.2 1997/06/06 23:59:26 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_sgmap __P((bus_dma_tag_t, bus_size_t, int,
63 bus_size_t, bus_size_t, int, bus_dmamap_t *));
64
65 void lca_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
66
67 int lca_bus_dmamap_load_direct __P((bus_dma_tag_t, bus_dmamap_t, void *,
68 bus_size_t, struct proc *, int));
69 int lca_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
70 bus_size_t, struct proc *, int));
71
72 int lca_bus_dmamap_load_mbuf_direct __P((bus_dma_tag_t, bus_dmamap_t,
73 struct mbuf *, int));
74 int lca_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
75 struct mbuf *, int));
76
77 int lca_bus_dmamap_load_uio_direct __P((bus_dma_tag_t, bus_dmamap_t,
78 struct uio *, int));
79 int lca_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
80 struct uio *, int));
81
82 int lca_bus_dmamap_load_raw_direct __P((bus_dma_tag_t, bus_dmamap_t,
83 bus_dma_segment_t *, int, bus_size_t, int));
84 int lca_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
85 bus_dma_segment_t *, int, bus_size_t, int));
86
87 void lca_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
88
89 /*
90 * The 1G direct-mapped DMA window begins at this PCI address.
91 */
92 #define LCA_DIRECT_MAPPED_BASE 0x40000000
93
94 /*
95 * The 8M SGMAP-mapped DMA window begins at this PCI address.
96 */
97 #define LCA_SGMAP_MAPPED_BASE (8*1024*1024)
98
99 /*
100 * Macro to flush LCA scatter/gather TLB.
101 */
102 #define LCA_TLB_INVALIDATE() \
103 do { \
104 alpha_mb(); \
105 REGVAL64(LCA_IOC_TBIA) = 0; \
106 alpha_mb(); \
107 } while (0)
108
109 void
110 lca_dma_init(lcp)
111 struct lca_config *lcp;
112 {
113 bus_dma_tag_t t;
114
115 /*
116 * Initialize the DMA tag used for direct-mapped DMA.
117 */
118 t = &lcp->lc_dmat_direct;
119 t->_cookie = lcp;
120 t->_get_tag = lca_dma_get_tag;
121 t->_dmamap_create = _bus_dmamap_create;
122 t->_dmamap_destroy = _bus_dmamap_destroy;
123 t->_dmamap_load = lca_bus_dmamap_load_direct;
124 t->_dmamap_load_mbuf = lca_bus_dmamap_load_mbuf_direct;
125 t->_dmamap_load_uio = lca_bus_dmamap_load_uio_direct;
126 t->_dmamap_load_raw = lca_bus_dmamap_load_raw_direct;
127 t->_dmamap_unload = _bus_dmamap_unload;
128 t->_dmamap_sync = NULL; /* Nothing to do. */
129
130 t->_dmamem_alloc = _bus_dmamem_alloc;
131 t->_dmamem_free = _bus_dmamem_free;
132 t->_dmamem_map = _bus_dmamem_map;
133 t->_dmamem_unmap = _bus_dmamem_unmap;
134 t->_dmamem_mmap = _bus_dmamem_mmap;
135
136 /*
137 * Initialize the DMA tag used for sgmap-mapped DMA.
138 */
139 t = &lcp->lc_dmat_sgmap;
140 t->_cookie = lcp;
141 t->_get_tag = lca_dma_get_tag;
142 t->_dmamap_create = lca_bus_dmamap_create_sgmap;
143 t->_dmamap_destroy = lca_bus_dmamap_destroy_sgmap;
144 t->_dmamap_load = lca_bus_dmamap_load_sgmap;
145 t->_dmamap_load_mbuf = lca_bus_dmamap_load_mbuf_sgmap;
146 t->_dmamap_load_uio = lca_bus_dmamap_load_uio_sgmap;
147 t->_dmamap_load_raw = lca_bus_dmamap_load_raw_sgmap;
148 t->_dmamap_unload = lca_bus_dmamap_unload_sgmap;
149 t->_dmamap_sync = NULL; /* Nothing to do. */
150
151 t->_dmamem_alloc = _bus_dmamem_alloc;
152 t->_dmamem_free = _bus_dmamem_free;
153 t->_dmamem_map = _bus_dmamem_map;
154 t->_dmamem_unmap = _bus_dmamem_unmap;
155 t->_dmamem_mmap = _bus_dmamem_mmap;
156
157 /*
158 * The firmware has set up window 1 as a 1G direct-mapped DMA
159 * window beginning at 1G. We leave it alone. Disable
160 * window 0.
161 */
162 REGVAL64(LCA_IOC_W_BASE0) = 0;
163 alpha_mb();
164
165 /*
166 * Initialize the SGMAP if safe to do so.
167 */
168 if (lcp->lc_mallocsafe) {
169 alpha_sgmap_init(t, &lcp->lc_sgmap, "lca_sgmap",
170 LCA_SGMAP_MAPPED_BASE, 0, (8*1024*1024),
171 sizeof(u_int64_t), NULL);
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 SGMAP-mapped DMA map.
239 */
240 int
241 lca_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
242 flags, dmamp)
243 bus_dma_tag_t t;
244 bus_size_t size;
245 int nsegments;
246 bus_size_t maxsegsz;
247 bus_size_t boundary;
248 int flags;
249 bus_dmamap_t *dmamp;
250 {
251 struct lca_config *lcp = t->_cookie;
252 struct alpha_sgmap_cookie *a;
253 bus_dmamap_t map;
254 int error;
255
256 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
257 boundary, flags, dmamp);
258 if (error)
259 return (error);
260
261 map = *dmamp;
262
263 a = malloc(sizeof(struct alpha_sgmap_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_sgmap_cookie));
270 map->_dm_sgcookie = a;
271
272 if (flags & BUS_DMA_ALLOCNOW) {
273 error = alpha_sgmap_alloc(map, round_page(size),
274 &lcp->lc_sgmap, flags);
275 if (error)
276 lca_bus_dmamap_destroy_sgmap(t, map);
277 }
278
279 return (error);
280 }
281
282 /*
283 * Destroy an LCA SGMAP-mapped DMA map.
284 */
285 void
286 lca_bus_dmamap_destroy_sgmap(t, map)
287 bus_dma_tag_t t;
288 bus_dmamap_t map;
289 {
290 struct lca_config *lcp = t->_cookie;
291 struct alpha_sgmap_cookie *a = map->_dm_sgcookie;
292
293 if (a->apdc_flags & APDC_HAS_SGMAP)
294 alpha_sgmap_free(&lcp->lc_sgmap, a);
295
296 free(a, M_DEVBUF);
297 _bus_dmamap_destroy(t, map);
298 }
299
300 /*
301 * Load an LCA direct-mapped DMA map with a linear buffer.
302 */
303 int
304 lca_bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
305 bus_dma_tag_t t;
306 bus_dmamap_t map;
307 void *buf;
308 bus_size_t buflen;
309 struct proc *p;
310 int flags;
311 {
312
313 return (_bus_dmamap_load_direct_common(t, map, buf, buflen, p,
314 flags, LCA_DIRECT_MAPPED_BASE));
315 }
316
317 /*
318 * Load an LCA SGMAP-mapped DMA map with a linear buffer.
319 */
320 int
321 lca_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
322 bus_dma_tag_t t;
323 bus_dmamap_t map;
324 void *buf;
325 bus_size_t buflen;
326 struct proc *p;
327 int flags;
328 {
329 struct lca_config *lcp = t->_cookie;
330 int error;
331
332 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
333 &lcp->lc_sgmap);
334 if (error == 0)
335 LCA_TLB_INVALIDATE();
336
337 return (error);
338 }
339
340 /*
341 * Load an LCA direct-mapped DMA map with an mbuf chain.
342 */
343 int
344 lca_bus_dmamap_load_mbuf_direct(t, map, m, flags)
345 bus_dma_tag_t t;
346 bus_dmamap_t map;
347 struct mbuf *m;
348 int flags;
349 {
350
351 return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
352 flags, LCA_DIRECT_MAPPED_BASE));
353 }
354
355 /*
356 * Load an LCA SGMAP-mapped DMA map with an mbuf chain.
357 */
358 int
359 lca_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
360 bus_dma_tag_t t;
361 bus_dmamap_t map;
362 struct mbuf *m;
363 int flags;
364 {
365 struct lca_config *lcp = t->_cookie;
366 int error;
367
368 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, &lcp->lc_sgmap);
369 if (error == 0)
370 LCA_TLB_INVALIDATE();
371
372 return (error);
373 }
374
375 /*
376 * Load an LCA direct-mapped DMA map with a uio.
377 */
378 int
379 lca_bus_dmamap_load_uio_direct(t, map, uio, flags)
380 bus_dma_tag_t t;
381 bus_dmamap_t map;
382 struct uio *uio;
383 int flags;
384 {
385
386 return (_bus_dmamap_load_uio_direct_common(t, map, uio,
387 flags, LCA_DIRECT_MAPPED_BASE));
388 }
389
390 /*
391 * Load an LCA SGMAP-mapped DMA map with a uio.
392 */
393 int
394 lca_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
395 bus_dma_tag_t t;
396 bus_dmamap_t map;
397 struct uio *uio;
398 int flags;
399 {
400 struct lca_config *lcp = t->_cookie;
401 int error;
402
403 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, &lcp->lc_sgmap);
404 if (error == 0)
405 LCA_TLB_INVALIDATE();
406
407 return (error);
408 }
409
410 /*
411 * Load an LCA direct-mapped DMA map with raw memory.
412 */
413 int
414 lca_bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
415 bus_dma_tag_t t;
416 bus_dmamap_t map;
417 bus_dma_segment_t *segs;
418 int nsegs;
419 bus_size_t size;
420 int flags;
421 {
422
423 return (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
424 size, flags, LCA_DIRECT_MAPPED_BASE));
425 }
426
427 /*
428 * Load an LCA SGMAP-mapped DMA map with raw memory.
429 */
430 int
431 lca_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
432 bus_dma_tag_t t;
433 bus_dmamap_t map;
434 bus_dma_segment_t *segs;
435 int nsegs;
436 bus_size_t size;
437 int flags;
438 {
439 struct lca_config *lcp = t->_cookie;
440 int error;
441
442 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
443 &lcp->lc_sgmap);
444 if (error == 0)
445 LCA_TLB_INVALIDATE();
446
447 return (error);
448 }
449
450 /*
451 * Unload an LCA DMA map.
452 */
453 void
454 lca_bus_dmamap_unload_sgmap(t, map)
455 bus_dma_tag_t t;
456 bus_dmamap_t map;
457 {
458 struct lca_config *lcp = t->_cookie;
459
460 /*
461 * Invalidate any SGMAP page table entries used by this
462 * mapping.
463 */
464 pci_sgmap_pte64_unload(t, map, &lcp->lc_sgmap);
465 LCA_TLB_INVALIDATE();
466
467 /*
468 * Do the generic bits of the unload.
469 */
470 _bus_dmamap_unload(t, map);
471 }
472