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