apecs_dma.c revision 1.7 1 /* $NetBSD: apecs_dma.c,v 1.7 1998/02/04 07:37:28 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.7 1998/02/04 07:37:28 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 = _bus_dmamap_sync;
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 = _bus_dmamap_sync;
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 REGVAL(EPIC_PCI_BASE_1) = APECS_SGMAP_MAPPED_BASE |
178 EPIC_PCI_BASE_SGEN | EPIC_PCI_BASE_WENB;
179 alpha_mb();
180
181 REGVAL(EPIC_PCI_MASK_1) = EPIC_PCI_MASK_8M;
182 alpha_mb();
183
184 tbase = acp->ac_sgmap.aps_ptpa >> EPIC_TBASE_SHIFT;
185 if ((tbase & EPIC_TBASE_T_BASE) != tbase)
186 panic("apecs_dma_init: bad page table address");
187 REGVAL(EPIC_TBASE_1) = tbase;
188 alpha_mb();
189
190 APECS_TLB_INVALIDATE();
191 }
192
193 /* XXX XXX BEGIN XXX XXX */
194 { /* XXX */
195 extern vm_offset_t alpha_XXX_dmamap_or; /* XXX */
196 alpha_XXX_dmamap_or = APECS_DIRECT_MAPPED_BASE; /* XXX */
197 } /* XXX */
198 /* XXX XXX END XXX XXX */
199 }
200
201 /*
202 * Return the bus dma tag to be used for the specified bus type.
203 * INTERNAL USE ONLY!
204 */
205 bus_dma_tag_t
206 apecs_dma_get_tag(t, bustype)
207 bus_dma_tag_t t;
208 alpha_bus_t bustype;
209 {
210 struct apecs_config *acp = t->_cookie;
211
212 switch (bustype) {
213 case ALPHA_BUS_PCI:
214 case ALPHA_BUS_EISA:
215 /*
216 * Systems with an APECS can only support 1G
217 * of memory, so we use the direct-mapped window
218 * on busses that have 32-bit DMA.
219 */
220 return (&acp->ac_dmat_direct);
221
222 case ALPHA_BUS_ISA:
223 /*
224 * ISA doesn't have enough address bits to use
225 * the direct-mapped DMA window, so we must use
226 * SGMAPs.
227 */
228 return (&acp->ac_dmat_sgmap);
229
230 default:
231 panic("apecs_dma_get_tag: shouldn't be here, really...");
232 }
233 }
234
235 /*
236 * Create an APECS SGMAP-mapped DMA map.
237 */
238 int
239 apecs_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
240 flags, dmamp)
241 bus_dma_tag_t t;
242 bus_size_t size;
243 int nsegments;
244 bus_size_t maxsegsz;
245 bus_size_t boundary;
246 int flags;
247 bus_dmamap_t *dmamp;
248 {
249 struct apecs_config *acp = t->_cookie;
250 bus_dmamap_t map;
251 int error;
252
253 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
254 boundary, flags, dmamp);
255 if (error)
256 return (error);
257
258 map = *dmamp;
259
260 if (flags & BUS_DMA_ALLOCNOW) {
261 error = alpha_sgmap_alloc(map, round_page(size),
262 &acp->ac_sgmap, flags);
263 if (error)
264 apecs_bus_dmamap_destroy_sgmap(t, map);
265 }
266
267 return (error);
268 }
269
270 /*
271 * Destroy an APECS SGMAP-mapped DMA map.
272 */
273 void
274 apecs_bus_dmamap_destroy_sgmap(t, map)
275 bus_dma_tag_t t;
276 bus_dmamap_t map;
277 {
278 struct apecs_config *acp = t->_cookie;
279
280 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
281 alpha_sgmap_free(map, &acp->ac_sgmap);
282
283 _bus_dmamap_destroy(t, map);
284 }
285
286 /*
287 * Load an APECS direct-mapped DMA map with a linear buffer.
288 */
289 int
290 apecs_bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
291 bus_dma_tag_t t;
292 bus_dmamap_t map;
293 void *buf;
294 bus_size_t buflen;
295 struct proc *p;
296 int flags;
297 {
298
299 return (_bus_dmamap_load_direct_common(t, map, buf, buflen, p,
300 flags, APECS_DIRECT_MAPPED_BASE));
301 }
302
303 /*
304 * Load an APECS SGMAP-mapped DMA map with a linear buffer.
305 */
306 int
307 apecs_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
308 bus_dma_tag_t t;
309 bus_dmamap_t map;
310 void *buf;
311 bus_size_t buflen;
312 struct proc *p;
313 int flags;
314 {
315 struct apecs_config *acp = t->_cookie;
316 int error;
317
318 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
319 &acp->ac_sgmap);
320 if (error == 0)
321 APECS_TLB_INVALIDATE();
322
323 return (error);
324 }
325
326 /*
327 * Load an APECS direct-mapped DMA map with an mbuf chain.
328 */
329 int
330 apecs_bus_dmamap_load_mbuf_direct(t, map, m, flags)
331 bus_dma_tag_t t;
332 bus_dmamap_t map;
333 struct mbuf *m;
334 int flags;
335 {
336
337 return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
338 flags, APECS_DIRECT_MAPPED_BASE));
339 }
340
341 /*
342 * Load an APECS SGMAP-mapped DMA map with an mbuf chain.
343 */
344 int
345 apecs_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
346 bus_dma_tag_t t;
347 bus_dmamap_t map;
348 struct mbuf *m;
349 int flags;
350 {
351 struct apecs_config *acp = t->_cookie;
352 int error;
353
354 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, &acp->ac_sgmap);
355 if (error == 0)
356 APECS_TLB_INVALIDATE();
357
358 return (error);
359 }
360
361 /*
362 * Load an APECS direct-mapped DMA map with a uio.
363 */
364 int
365 apecs_bus_dmamap_load_uio_direct(t, map, uio, flags)
366 bus_dma_tag_t t;
367 bus_dmamap_t map;
368 struct uio *uio;
369 int flags;
370 {
371
372 return (_bus_dmamap_load_uio_direct_common(t, map, uio,
373 flags, APECS_DIRECT_MAPPED_BASE));
374 }
375
376 /*
377 * Load an APECS SGMAP-mapped DMA map with a uio.
378 */
379 int
380 apecs_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
381 bus_dma_tag_t t;
382 bus_dmamap_t map;
383 struct uio *uio;
384 int flags;
385 {
386 struct apecs_config *acp = t->_cookie;
387 int error;
388
389 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, &acp->ac_sgmap);
390 if (error == 0)
391 APECS_TLB_INVALIDATE();
392
393 return (error);
394 }
395
396 /*
397 * Load an APECS direct-mapped DMA map with raw memory.
398 */
399 int
400 apecs_bus_dmamap_load_raw_direct(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 (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
410 size, flags, APECS_DIRECT_MAPPED_BASE));
411 }
412
413 /*
414 * Load an APECS SGMAP-mapped DMA map with raw memory.
415 */
416 int
417 apecs_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
418 bus_dma_tag_t t;
419 bus_dmamap_t map;
420 bus_dma_segment_t *segs;
421 int nsegs;
422 bus_size_t size;
423 int flags;
424 {
425 struct apecs_config *acp = t->_cookie;
426 int error;
427
428 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
429 &acp->ac_sgmap);
430 if (error == 0)
431 APECS_TLB_INVALIDATE();
432
433 return (error);
434 }
435
436 /*
437 * Unload an APECS DMA map.
438 */
439 void
440 apecs_bus_dmamap_unload_sgmap(t, map)
441 bus_dma_tag_t t;
442 bus_dmamap_t map;
443 {
444 struct apecs_config *acp = t->_cookie;
445
446 /*
447 * Invalidate any SGMAP page table entries used by this
448 * mapping.
449 */
450 pci_sgmap_pte64_unload(t, map, &acp->ac_sgmap);
451 APECS_TLB_INVALIDATE();
452
453 /*
454 * Do the generic bits of the unload.
455 */
456 _bus_dmamap_unload(t, map);
457 }
458