apecs_dma.c revision 1.8 1 /* $NetBSD: apecs_dma.c,v 1.8 1998/05/07 20:09:37 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.8 1998/05/07 20:09:37 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_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
67 bus_size_t, struct proc *, int));
68
69 int apecs_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
70 struct mbuf *, int));
71
72 int apecs_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
73 struct uio *, int));
74
75 int apecs_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
76 bus_dma_segment_t *, int, bus_size_t, int));
77
78 void apecs_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
79
80 /*
81 * The 1G direct-mapped DMA window begins at this PCI address.
82 */
83 #define APECS_DIRECT_MAPPED_BASE 0x40000000
84
85 /*
86 * The 8M SGMAP-mapped DMA window begins at this PCI address.
87 */
88 #define APECS_SGMAP_MAPPED_BASE (8*1024*1024)
89
90 /*
91 * Macro to flush APECS scatter/gather TLB.
92 */
93 #define APECS_TLB_INVALIDATE() \
94 do { \
95 alpha_mb(); \
96 REGVAL(EPIC_TBIA) = 0; \
97 alpha_mb(); \
98 } while (0)
99
100 void
101 apecs_dma_init(acp)
102 struct apecs_config *acp;
103 {
104 bus_addr_t tbase;
105 bus_dma_tag_t t;
106
107 /*
108 * Initialize the DMA tag used for direct-mapped DMA.
109 */
110 t = &acp->ac_dmat_direct;
111 t->_cookie = acp;
112 t->_wbase = APECS_DIRECT_MAPPED_BASE;
113 t->_get_tag = apecs_dma_get_tag;
114 t->_dmamap_create = _bus_dmamap_create;
115 t->_dmamap_destroy = _bus_dmamap_destroy;
116 t->_dmamap_load = _bus_dmamap_load_direct;
117 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
118 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
119 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
120 t->_dmamap_unload = _bus_dmamap_unload;
121 t->_dmamap_sync = _bus_dmamap_sync;
122
123 t->_dmamem_alloc = _bus_dmamem_alloc;
124 t->_dmamem_free = _bus_dmamem_free;
125 t->_dmamem_map = _bus_dmamem_map;
126 t->_dmamem_unmap = _bus_dmamem_unmap;
127 t->_dmamem_mmap = _bus_dmamem_mmap;
128
129 /*
130 * Initialize the DMA tag used for sgmap-mapped DMA.
131 */
132 t = &acp->ac_dmat_sgmap;
133 t->_cookie = acp;
134 t->_wbase = APECS_SGMAP_MAPPED_BASE;
135 t->_get_tag = apecs_dma_get_tag;
136 t->_dmamap_create = apecs_bus_dmamap_create_sgmap;
137 t->_dmamap_destroy = apecs_bus_dmamap_destroy_sgmap;
138 t->_dmamap_load = apecs_bus_dmamap_load_sgmap;
139 t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_sgmap;
140 t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_sgmap;
141 t->_dmamap_load_raw = apecs_bus_dmamap_load_raw_sgmap;
142 t->_dmamap_unload = apecs_bus_dmamap_unload_sgmap;
143 t->_dmamap_sync = _bus_dmamap_sync;
144
145 t->_dmamem_alloc = _bus_dmamem_alloc;
146 t->_dmamem_free = _bus_dmamem_free;
147 t->_dmamem_map = _bus_dmamem_map;
148 t->_dmamem_unmap = _bus_dmamem_unmap;
149 t->_dmamem_mmap = _bus_dmamem_mmap;
150
151 /*
152 * The firmware has set up window 2 as a 1G direct-mapped DMA
153 * window beginning at 1G. We leave it alone. Disable
154 * window 1.
155 */
156 REGVAL(EPIC_PCI_BASE_1) = 0;
157 alpha_mb();
158
159 /*
160 * Initialize the SGMAP if safe to do so.
161 */
162 if (acp->ac_mallocsafe) {
163 alpha_sgmap_init(t, &acp->ac_sgmap, "apecs_sgmap",
164 APECS_SGMAP_MAPPED_BASE, 0, (8*1024*1024),
165 sizeof(u_int64_t), NULL, 0);
166
167 /*
168 * Set up window 1 as an 8MB SGMAP-mapped window
169 * starting at 8MB.
170 */
171 REGVAL(EPIC_PCI_BASE_1) = APECS_SGMAP_MAPPED_BASE |
172 EPIC_PCI_BASE_SGEN | EPIC_PCI_BASE_WENB;
173 alpha_mb();
174
175 REGVAL(EPIC_PCI_MASK_1) = EPIC_PCI_MASK_8M;
176 alpha_mb();
177
178 tbase = acp->ac_sgmap.aps_ptpa >> EPIC_TBASE_SHIFT;
179 if ((tbase & EPIC_TBASE_T_BASE) != tbase)
180 panic("apecs_dma_init: bad page table address");
181 REGVAL(EPIC_TBASE_1) = tbase;
182 alpha_mb();
183
184 APECS_TLB_INVALIDATE();
185 }
186
187 /* XXX XXX BEGIN XXX XXX */
188 { /* XXX */
189 extern vm_offset_t alpha_XXX_dmamap_or; /* XXX */
190 alpha_XXX_dmamap_or = APECS_DIRECT_MAPPED_BASE; /* XXX */
191 } /* XXX */
192 /* XXX XXX END XXX XXX */
193 }
194
195 /*
196 * Return the bus dma tag to be used for the specified bus type.
197 * INTERNAL USE ONLY!
198 */
199 bus_dma_tag_t
200 apecs_dma_get_tag(t, bustype)
201 bus_dma_tag_t t;
202 alpha_bus_t bustype;
203 {
204 struct apecs_config *acp = t->_cookie;
205
206 switch (bustype) {
207 case ALPHA_BUS_PCI:
208 case ALPHA_BUS_EISA:
209 /*
210 * Systems with an APECS can only support 1G
211 * of memory, so we use the direct-mapped window
212 * on busses that have 32-bit DMA.
213 */
214 return (&acp->ac_dmat_direct);
215
216 case ALPHA_BUS_ISA:
217 /*
218 * ISA doesn't have enough address bits to use
219 * the direct-mapped DMA window, so we must use
220 * SGMAPs.
221 */
222 return (&acp->ac_dmat_sgmap);
223
224 default:
225 panic("apecs_dma_get_tag: shouldn't be here, really...");
226 }
227 }
228
229 /*
230 * Create an APECS SGMAP-mapped DMA map.
231 */
232 int
233 apecs_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
234 flags, dmamp)
235 bus_dma_tag_t t;
236 bus_size_t size;
237 int nsegments;
238 bus_size_t maxsegsz;
239 bus_size_t boundary;
240 int flags;
241 bus_dmamap_t *dmamp;
242 {
243 struct apecs_config *acp = t->_cookie;
244 bus_dmamap_t map;
245 int error;
246
247 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
248 boundary, flags, dmamp);
249 if (error)
250 return (error);
251
252 map = *dmamp;
253
254 if (flags & BUS_DMA_ALLOCNOW) {
255 error = alpha_sgmap_alloc(map, round_page(size),
256 &acp->ac_sgmap, flags);
257 if (error)
258 apecs_bus_dmamap_destroy_sgmap(t, map);
259 }
260
261 return (error);
262 }
263
264 /*
265 * Destroy an APECS SGMAP-mapped DMA map.
266 */
267 void
268 apecs_bus_dmamap_destroy_sgmap(t, map)
269 bus_dma_tag_t t;
270 bus_dmamap_t map;
271 {
272 struct apecs_config *acp = t->_cookie;
273
274 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
275 alpha_sgmap_free(map, &acp->ac_sgmap);
276
277 _bus_dmamap_destroy(t, map);
278 }
279
280 /*
281 * Load an APECS SGMAP-mapped DMA map with a linear buffer.
282 */
283 int
284 apecs_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
285 bus_dma_tag_t t;
286 bus_dmamap_t map;
287 void *buf;
288 bus_size_t buflen;
289 struct proc *p;
290 int flags;
291 {
292 struct apecs_config *acp = t->_cookie;
293 int error;
294
295 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
296 &acp->ac_sgmap);
297 if (error == 0)
298 APECS_TLB_INVALIDATE();
299
300 return (error);
301 }
302
303 /*
304 * Load an APECS SGMAP-mapped DMA map with an mbuf chain.
305 */
306 int
307 apecs_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
308 bus_dma_tag_t t;
309 bus_dmamap_t map;
310 struct mbuf *m;
311 int flags;
312 {
313 struct apecs_config *acp = t->_cookie;
314 int error;
315
316 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, &acp->ac_sgmap);
317 if (error == 0)
318 APECS_TLB_INVALIDATE();
319
320 return (error);
321 }
322
323 /*
324 * Load an APECS SGMAP-mapped DMA map with a uio.
325 */
326 int
327 apecs_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
328 bus_dma_tag_t t;
329 bus_dmamap_t map;
330 struct uio *uio;
331 int flags;
332 {
333 struct apecs_config *acp = t->_cookie;
334 int error;
335
336 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, &acp->ac_sgmap);
337 if (error == 0)
338 APECS_TLB_INVALIDATE();
339
340 return (error);
341 }
342
343 /*
344 * Load an APECS SGMAP-mapped DMA map with raw memory.
345 */
346 int
347 apecs_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
348 bus_dma_tag_t t;
349 bus_dmamap_t map;
350 bus_dma_segment_t *segs;
351 int nsegs;
352 bus_size_t size;
353 int flags;
354 {
355 struct apecs_config *acp = t->_cookie;
356 int error;
357
358 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
359 &acp->ac_sgmap);
360 if (error == 0)
361 APECS_TLB_INVALIDATE();
362
363 return (error);
364 }
365
366 /*
367 * Unload an APECS DMA map.
368 */
369 void
370 apecs_bus_dmamap_unload_sgmap(t, map)
371 bus_dma_tag_t t;
372 bus_dmamap_t map;
373 {
374 struct apecs_config *acp = t->_cookie;
375
376 /*
377 * Invalidate any SGMAP page table entries used by this
378 * mapping.
379 */
380 pci_sgmap_pte64_unload(t, map, &acp->ac_sgmap);
381 APECS_TLB_INVALIDATE();
382
383 /*
384 * Do the generic bits of the unload.
385 */
386 _bus_dmamap_unload(t, map);
387 }
388