apecs_dma.c revision 1.9 1 /* $NetBSD: apecs_dma.c,v 1.9 1998/05/13 21:21:16 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.9 1998/05/13 21:21:16 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->_sgmap = NULL;
118 t->_get_tag = apecs_dma_get_tag;
119 t->_dmamap_create = _bus_dmamap_create;
120 t->_dmamap_destroy = _bus_dmamap_destroy;
121 t->_dmamap_load = _bus_dmamap_load_direct;
122 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
123 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
124 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
125 t->_dmamap_unload = _bus_dmamap_unload;
126 t->_dmamap_sync = _bus_dmamap_sync;
127
128 t->_dmamem_alloc = _bus_dmamem_alloc;
129 t->_dmamem_free = _bus_dmamem_free;
130 t->_dmamem_map = _bus_dmamem_map;
131 t->_dmamem_unmap = _bus_dmamem_unmap;
132 t->_dmamem_mmap = _bus_dmamem_mmap;
133
134 /*
135 * Initialize the DMA tag used for sgmap-mapped DMA.
136 */
137 t = &acp->ac_dmat_sgmap;
138 t->_cookie = acp;
139 t->_wbase = APECS_SGMAP_MAPPED_BASE;
140 t->_wsize = APECS_SGMAP_MAPPED_SIZE;
141 t->_next_window = NULL;
142 t->_sgmap = &acp->ac_sgmap;
143 t->_get_tag = apecs_dma_get_tag;
144 t->_dmamap_create = apecs_bus_dmamap_create_sgmap;
145 t->_dmamap_destroy = apecs_bus_dmamap_destroy_sgmap;
146 t->_dmamap_load = apecs_bus_dmamap_load_sgmap;
147 t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_sgmap;
148 t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_sgmap;
149 t->_dmamap_load_raw = apecs_bus_dmamap_load_raw_sgmap;
150 t->_dmamap_unload = apecs_bus_dmamap_unload_sgmap;
151 t->_dmamap_sync = _bus_dmamap_sync;
152
153 t->_dmamem_alloc = _bus_dmamem_alloc;
154 t->_dmamem_free = _bus_dmamem_free;
155 t->_dmamem_map = _bus_dmamem_map;
156 t->_dmamem_unmap = _bus_dmamem_unmap;
157 t->_dmamem_mmap = _bus_dmamem_mmap;
158
159 /*
160 * The firmware has set up window 2 as a 1G direct-mapped DMA
161 * window beginning at 1G. We leave it alone. Disable
162 * window 1.
163 */
164 REGVAL(EPIC_PCI_BASE_1) = 0;
165 alpha_mb();
166
167 /*
168 * Initialize the SGMAP if safe to do so.
169 */
170 if (acp->ac_mallocsafe) {
171 alpha_sgmap_init(t, &acp->ac_sgmap, "apecs_sgmap",
172 APECS_SGMAP_MAPPED_BASE, 0, APECS_SGMAP_MAPPED_SIZE,
173 sizeof(u_int64_t), NULL, 0);
174
175 /*
176 * Set up window 1 as an 8MB SGMAP-mapped window
177 * starting at 8MB.
178 */
179 REGVAL(EPIC_PCI_BASE_1) = APECS_SGMAP_MAPPED_BASE |
180 EPIC_PCI_BASE_SGEN | EPIC_PCI_BASE_WENB;
181 alpha_mb();
182
183 REGVAL(EPIC_PCI_MASK_1) = EPIC_PCI_MASK_8M;
184 alpha_mb();
185
186 tbase = acp->ac_sgmap.aps_ptpa >> EPIC_TBASE_SHIFT;
187 if ((tbase & EPIC_TBASE_T_BASE) != tbase)
188 panic("apecs_dma_init: bad page table address");
189 REGVAL(EPIC_TBASE_1) = tbase;
190 alpha_mb();
191
192 APECS_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 = APECS_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 apecs_dma_get_tag(t, bustype)
209 bus_dma_tag_t t;
210 alpha_bus_t bustype;
211 {
212 struct apecs_config *acp = t->_cookie;
213
214 switch (bustype) {
215 case ALPHA_BUS_PCI:
216 case ALPHA_BUS_EISA:
217 /*
218 * Systems with an APECS 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 (&acp->ac_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 (&acp->ac_dmat_sgmap);
231
232 default:
233 panic("apecs_dma_get_tag: shouldn't be here, really...");
234 }
235 }
236
237 /*
238 * Create an APECS SGMAP-mapped DMA map.
239 */
240 int
241 apecs_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 bus_dmamap_t map;
252 int error;
253
254 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
255 boundary, flags, dmamp);
256 if (error)
257 return (error);
258
259 map = *dmamp;
260
261 if (flags & BUS_DMA_ALLOCNOW) {
262 error = alpha_sgmap_alloc(map, round_page(size),
263 t->_sgmap, flags);
264 if (error)
265 apecs_bus_dmamap_destroy_sgmap(t, map);
266 }
267
268 return (error);
269 }
270
271 /*
272 * Destroy an APECS SGMAP-mapped DMA map.
273 */
274 void
275 apecs_bus_dmamap_destroy_sgmap(t, map)
276 bus_dma_tag_t t;
277 bus_dmamap_t map;
278 {
279
280 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
281 alpha_sgmap_free(map, t->_sgmap);
282
283 _bus_dmamap_destroy(t, map);
284 }
285
286 /*
287 * Load an APECS SGMAP-mapped DMA map with a linear buffer.
288 */
289 int
290 apecs_bus_dmamap_load_sgmap(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 int error;
299
300 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
301 t->_sgmap);
302 if (error == 0)
303 APECS_TLB_INVALIDATE();
304
305 return (error);
306 }
307
308 /*
309 * Load an APECS SGMAP-mapped DMA map with an mbuf chain.
310 */
311 int
312 apecs_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
313 bus_dma_tag_t t;
314 bus_dmamap_t map;
315 struct mbuf *m;
316 int flags;
317 {
318 int error;
319
320 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
321 if (error == 0)
322 APECS_TLB_INVALIDATE();
323
324 return (error);
325 }
326
327 /*
328 * Load an APECS SGMAP-mapped DMA map with a uio.
329 */
330 int
331 apecs_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
332 bus_dma_tag_t t;
333 bus_dmamap_t map;
334 struct uio *uio;
335 int flags;
336 {
337 int error;
338
339 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
340 if (error == 0)
341 APECS_TLB_INVALIDATE();
342
343 return (error);
344 }
345
346 /*
347 * Load an APECS SGMAP-mapped DMA map with raw memory.
348 */
349 int
350 apecs_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
351 bus_dma_tag_t t;
352 bus_dmamap_t map;
353 bus_dma_segment_t *segs;
354 int nsegs;
355 bus_size_t size;
356 int flags;
357 {
358 int error;
359
360 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
361 t->_sgmap);
362 if (error == 0)
363 APECS_TLB_INVALIDATE();
364
365 return (error);
366 }
367
368 /*
369 * Unload an APECS DMA map.
370 */
371 void
372 apecs_bus_dmamap_unload_sgmap(t, map)
373 bus_dma_tag_t t;
374 bus_dmamap_t map;
375 {
376
377 /*
378 * Invalidate any SGMAP page table entries used by this
379 * mapping.
380 */
381 pci_sgmap_pte64_unload(t, map, t->_sgmap);
382 APECS_TLB_INVALIDATE();
383
384 /*
385 * Do the generic bits of the unload.
386 */
387 _bus_dmamap_unload(t, map);
388 }
389