apecs_dma.c revision 1.5 1 /* $NetBSD: apecs_dma.c,v 1.5 1998/01/17 21:53:56 thorpej 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: apecs_dma.c,v 1.5 1998/01/17 21:53:56 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/pci/apecsreg.h>
57 #include <alpha/pci/apecsvar.h>
58
59 bus_dma_tag_t apecs_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
60
61 int apecs_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
62 bus_size_t, bus_size_t, int, bus_dmamap_t *));
63
64 void apecs_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
65
66 int apecs_bus_dmamap_load_direct __P((bus_dma_tag_t, bus_dmamap_t, void *,
67 bus_size_t, struct proc *, int));
68 int apecs_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
69 bus_size_t, struct proc *, int));
70
71 int apecs_bus_dmamap_load_mbuf_direct __P((bus_dma_tag_t, bus_dmamap_t,
72 struct mbuf *, int));
73 int apecs_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
74 struct mbuf *, int));
75
76 int apecs_bus_dmamap_load_uio_direct __P((bus_dma_tag_t, bus_dmamap_t,
77 struct uio *, int));
78 int apecs_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
79 struct uio *, int));
80
81 int apecs_bus_dmamap_load_raw_direct __P((bus_dma_tag_t, bus_dmamap_t,
82 bus_dma_segment_t *, int, bus_size_t, int));
83 int apecs_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
84 bus_dma_segment_t *, int, bus_size_t, int));
85
86 void apecs_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
87
88 /*
89 * The 1G direct-mapped DMA window begins at this PCI address.
90 */
91 #define APECS_DIRECT_MAPPED_BASE 0x40000000
92
93 /*
94 * The 8M SGMAP-mapped DMA window begins at this PCI address.
95 */
96 #define APECS_SGMAP_MAPPED_BASE (8*1024*1024)
97
98 /*
99 * Macro to flush APECS scatter/gather TLB.
100 */
101 #define APECS_TLB_INVALIDATE() \
102 do { \
103 alpha_mb(); \
104 REGVAL(EPIC_TBIA) = 0; \
105 alpha_mb(); \
106 } while (0)
107
108 void
109 apecs_dma_init(acp)
110 struct apecs_config *acp;
111 {
112 bus_addr_t tbase;
113 bus_dma_tag_t t;
114
115 /*
116 * Initialize the DMA tag used for direct-mapped DMA.
117 */
118 t = &acp->ac_dmat_direct;
119 t->_cookie = acp;
120 t->_get_tag = apecs_dma_get_tag;
121 t->_dmamap_create = _bus_dmamap_create;
122 t->_dmamap_destroy = _bus_dmamap_destroy;
123 t->_dmamap_load = apecs_bus_dmamap_load_direct;
124 t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_direct;
125 t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_direct;
126 t->_dmamap_load_raw = apecs_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 = &acp->ac_dmat_sgmap;
140 t->_cookie = acp;
141 t->_get_tag = apecs_dma_get_tag;
142 t->_dmamap_create = apecs_bus_dmamap_create_sgmap;
143 t->_dmamap_destroy = apecs_bus_dmamap_destroy_sgmap;
144 t->_dmamap_load = apecs_bus_dmamap_load_sgmap;
145 t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_sgmap;
146 t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_sgmap;
147 t->_dmamap_load_raw = apecs_bus_dmamap_load_raw_sgmap;
148 t->_dmamap_unload = apecs_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 2 as a 1G direct-mapped DMA
159 * window beginning at 1G. We leave it alone. Disable
160 * window 1.
161 */
162 REGVAL(EPIC_PCI_BASE_1) = 0;
163 alpha_mb();
164
165 /*
166 * Initialize the SGMAP if safe to do so.
167 */
168 if (acp->ac_mallocsafe) {
169 alpha_sgmap_init(t, &acp->ac_sgmap, "apecs_sgmap",
170 APECS_SGMAP_MAPPED_BASE, 0, (8*1024*1024),
171 sizeof(u_int64_t), NULL, 0);
172
173 /*
174 * Set up window 1 as an 8MB SGMAP-mapped window
175 * starting at 8MB.
176 */
177 tbase = acp->ac_sgmap.aps_ptpa >> EPIC_TBASE_SHIFT;
178 if ((tbase & EPIC_TBASE_T_BASE) != tbase)
179 panic("apecs_dma_init: bad page table address");
180 REGVAL(EPIC_TBASE_1) = tbase;
181 REGVAL(EPIC_PCI_MASK_1) = EPIC_PCI_MASK_8M;
182 alpha_mb();
183
184 REGVAL(EPIC_PCI_BASE_1) = APECS_SGMAP_MAPPED_BASE |
185 EPIC_PCI_BASE_SGEN | EPIC_PCI_BASE_WENB;
186 alpha_mb();
187
188 APECS_TLB_INVALIDATE();
189 }
190
191 /* XXX XXX BEGIN XXX XXX */
192 { /* XXX */
193 extern vm_offset_t alpha_XXX_dmamap_or; /* XXX */
194 alpha_XXX_dmamap_or = APECS_DIRECT_MAPPED_BASE; /* XXX */
195 } /* XXX */
196 /* XXX XXX END XXX XXX */
197 }
198
199 /*
200 * Return the bus dma tag to be used for the specified bus type.
201 * INTERNAL USE ONLY!
202 */
203 bus_dma_tag_t
204 apecs_dma_get_tag(t, bustype)
205 bus_dma_tag_t t;
206 alpha_bus_t bustype;
207 {
208 struct apecs_config *acp = t->_cookie;
209
210 switch (bustype) {
211 case ALPHA_BUS_PCI:
212 case ALPHA_BUS_EISA:
213 /*
214 * Systems with an APECS can only support 1G
215 * of memory, so we use the direct-mapped window
216 * on busses that have 32-bit DMA.
217 */
218 return (&acp->ac_dmat_direct);
219
220 case ALPHA_BUS_ISA:
221 /*
222 * ISA doesn't have enough address bits to use
223 * the direct-mapped DMA window, so we must use
224 * SGMAPs.
225 */
226 return (&acp->ac_dmat_sgmap);
227
228 default:
229 panic("apecs_dma_get_tag: shouldn't be here, really...");
230 }
231 }
232
233 /*
234 * Create an APECS SGMAP-mapped DMA map.
235 */
236 int
237 apecs_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
238 flags, dmamp)
239 bus_dma_tag_t t;
240 bus_size_t size;
241 int nsegments;
242 bus_size_t maxsegsz;
243 bus_size_t boundary;
244 int flags;
245 bus_dmamap_t *dmamp;
246 {
247 struct apecs_config *acp = t->_cookie;
248 bus_dmamap_t map;
249 int error;
250
251 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
252 boundary, flags, dmamp);
253 if (error)
254 return (error);
255
256 map = *dmamp;
257
258 if (flags & BUS_DMA_ALLOCNOW) {
259 error = alpha_sgmap_alloc(map, round_page(size),
260 &acp->ac_sgmap, flags);
261 if (error)
262 apecs_bus_dmamap_destroy_sgmap(t, map);
263 }
264
265 return (error);
266 }
267
268 /*
269 * Destroy an APECS SGMAP-mapped DMA map.
270 */
271 void
272 apecs_bus_dmamap_destroy_sgmap(t, map)
273 bus_dma_tag_t t;
274 bus_dmamap_t map;
275 {
276 struct apecs_config *acp = t->_cookie;
277
278 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
279 alpha_sgmap_free(map, &acp->ac_sgmap);
280
281 _bus_dmamap_destroy(t, map);
282 }
283
284 /*
285 * Load an APECS direct-mapped DMA map with a linear buffer.
286 */
287 int
288 apecs_bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
289 bus_dma_tag_t t;
290 bus_dmamap_t map;
291 void *buf;
292 bus_size_t buflen;
293 struct proc *p;
294 int flags;
295 {
296
297 return (_bus_dmamap_load_direct_common(t, map, buf, buflen, p,
298 flags, APECS_DIRECT_MAPPED_BASE));
299 }
300
301 /*
302 * Load an APECS SGMAP-mapped DMA map with a linear buffer.
303 */
304 int
305 apecs_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
306 bus_dma_tag_t t;
307 bus_dmamap_t map;
308 void *buf;
309 bus_size_t buflen;
310 struct proc *p;
311 int flags;
312 {
313 struct apecs_config *acp = t->_cookie;
314 int error;
315
316 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
317 &acp->ac_sgmap);
318 if (error == 0)
319 APECS_TLB_INVALIDATE();
320
321 return (error);
322 }
323
324 /*
325 * Load an APECS direct-mapped DMA map with an mbuf chain.
326 */
327 int
328 apecs_bus_dmamap_load_mbuf_direct(t, map, m, flags)
329 bus_dma_tag_t t;
330 bus_dmamap_t map;
331 struct mbuf *m;
332 int flags;
333 {
334
335 return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
336 flags, APECS_DIRECT_MAPPED_BASE));
337 }
338
339 /*
340 * Load an APECS SGMAP-mapped DMA map with an mbuf chain.
341 */
342 int
343 apecs_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
344 bus_dma_tag_t t;
345 bus_dmamap_t map;
346 struct mbuf *m;
347 int flags;
348 {
349 struct apecs_config *acp = t->_cookie;
350 int error;
351
352 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, &acp->ac_sgmap);
353 if (error == 0)
354 APECS_TLB_INVALIDATE();
355
356 return (error);
357 }
358
359 /*
360 * Load an APECS direct-mapped DMA map with a uio.
361 */
362 int
363 apecs_bus_dmamap_load_uio_direct(t, map, uio, flags)
364 bus_dma_tag_t t;
365 bus_dmamap_t map;
366 struct uio *uio;
367 int flags;
368 {
369
370 return (_bus_dmamap_load_uio_direct_common(t, map, uio,
371 flags, APECS_DIRECT_MAPPED_BASE));
372 }
373
374 /*
375 * Load an APECS SGMAP-mapped DMA map with a uio.
376 */
377 int
378 apecs_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
379 bus_dma_tag_t t;
380 bus_dmamap_t map;
381 struct uio *uio;
382 int flags;
383 {
384 struct apecs_config *acp = t->_cookie;
385 int error;
386
387 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, &acp->ac_sgmap);
388 if (error == 0)
389 APECS_TLB_INVALIDATE();
390
391 return (error);
392 }
393
394 /*
395 * Load an APECS direct-mapped DMA map with raw memory.
396 */
397 int
398 apecs_bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
399 bus_dma_tag_t t;
400 bus_dmamap_t map;
401 bus_dma_segment_t *segs;
402 int nsegs;
403 bus_size_t size;
404 int flags;
405 {
406
407 return (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
408 size, flags, APECS_DIRECT_MAPPED_BASE));
409 }
410
411 /*
412 * Load an APECS SGMAP-mapped DMA map with raw memory.
413 */
414 int
415 apecs_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
416 bus_dma_tag_t t;
417 bus_dmamap_t map;
418 bus_dma_segment_t *segs;
419 int nsegs;
420 bus_size_t size;
421 int flags;
422 {
423 struct apecs_config *acp = t->_cookie;
424 int error;
425
426 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
427 &acp->ac_sgmap);
428 if (error == 0)
429 APECS_TLB_INVALIDATE();
430
431 return (error);
432 }
433
434 /*
435 * Unload an APECS DMA map.
436 */
437 void
438 apecs_bus_dmamap_unload_sgmap(t, map)
439 bus_dma_tag_t t;
440 bus_dmamap_t map;
441 {
442 struct apecs_config *acp = t->_cookie;
443
444 /*
445 * Invalidate any SGMAP page table entries used by this
446 * mapping.
447 */
448 pci_sgmap_pte64_unload(t, map, &acp->ac_sgmap);
449 APECS_TLB_INVALIDATE();
450
451 /*
452 * Do the generic bits of the unload.
453 */
454 _bus_dmamap_unload(t, map);
455 }
456