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