mcpcia_dma.c revision 1.13 1 /* $NetBSD: mcpcia_dma.c,v 1.13 2000/06/29 08:58:47 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999 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 and Matthew Jacob of the Numerical Aerospace Simulation
9 * Facility, 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: mcpcia_dma.c,v 1.13 2000/06/29 08:58:47 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/mcpciareg.h>
58 #include <alpha/pci/mcpciavar.h>
59 #include <alpha/pci/pci_kn300.h>
60
61 bus_dma_tag_t mcpcia_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
62
63 int mcpcia_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
64 bus_size_t, bus_size_t, int, bus_dmamap_t *));
65
66 void mcpcia_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
67
68 int mcpcia_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
69 bus_size_t, struct proc *, int));
70
71 int mcpcia_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
72 struct mbuf *, int));
73
74 int mcpcia_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
75 struct uio *, int));
76
77 int mcpcia_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
78 bus_dma_segment_t *, int, bus_size_t, int));
79
80 void mcpcia_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
81
82 /*
83 * Direct-mapped window: 2G at 2G
84 */
85 #define MCPCIA_DIRECT_MAPPED_BASE (2UL*1024UL*1024UL*1024UL)
86 #define MCPCIA_DIRECT_MAPPED_SIZE (2UL*1024UL*1024UL*1024UL)
87
88 /*
89 * SGMAP window for PCI: 1G at 1G
90 */
91 #define MCPCIA_PCI_SG_MAPPED_BASE (1UL*1024UL*1024UL*1024UL)
92 #define MCPCIA_PCI_SG_MAPPED_SIZE (1UL*1024UL*1024UL*1024UL)
93
94 /*
95 * SGMAP window for ISA: 8M at 8M
96 */
97 #define MCPCIA_ISA_SG_MAPPED_BASE (8*1024*1024)
98 #define MCPCIA_ISA_SG_MAPPED_SIZE (8*1024*1024)
99
100 #define MCPCIA_SGTLB_INVALIDATE(ccp) \
101 do { \
102 alpha_mb(); \
103 REGVAL(MCPCIA_SG_TBIA(ccp)) = 0xdeadbeef; \
104 alpha_mb(); \
105 } while (0)
106
107 void
108 mcpcia_dma_init(ccp)
109 struct mcpcia_config *ccp;
110 {
111 bus_dma_tag_t t;
112
113 /*
114 * Initialize the DMA tag used for direct-mapped DMA.
115 */
116 t = &ccp->cc_dmat_direct;
117 t->_cookie = ccp;
118 t->_wbase = MCPCIA_DIRECT_MAPPED_BASE;
119 t->_wsize = MCPCIA_DIRECT_MAPPED_SIZE;
120 t->_next_window = &ccp->cc_dmat_pci_sgmap;
121 t->_boundary = 0;
122 t->_sgmap = NULL;
123 t->_get_tag = mcpcia_dma_get_tag;
124 t->_dmamap_create = _bus_dmamap_create;
125 t->_dmamap_destroy = _bus_dmamap_destroy;
126 t->_dmamap_load = _bus_dmamap_load_direct;
127 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
128 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
129 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
130 t->_dmamap_unload = _bus_dmamap_unload;
131 t->_dmamap_sync = _bus_dmamap_sync;
132
133 t->_dmamem_alloc = _bus_dmamem_alloc;
134 t->_dmamem_free = _bus_dmamem_free;
135 t->_dmamem_map = _bus_dmamem_map;
136 t->_dmamem_unmap = _bus_dmamem_unmap;
137 t->_dmamem_mmap = _bus_dmamem_mmap;
138
139 /*
140 * Initialize the DMA tag used for sgmap-mapped PCI DMA.
141 */
142 t = &ccp->cc_dmat_pci_sgmap;
143 t->_cookie = ccp;
144 t->_wbase = MCPCIA_PCI_SG_MAPPED_BASE;
145 t->_wsize = MCPCIA_PCI_SG_MAPPED_SIZE;
146 t->_next_window = NULL;
147 t->_boundary = 0;
148 t->_sgmap = &ccp->cc_pci_sgmap;
149 t->_get_tag = mcpcia_dma_get_tag;
150 t->_dmamap_create = mcpcia_bus_dmamap_create_sgmap;
151 t->_dmamap_destroy = mcpcia_bus_dmamap_destroy_sgmap;
152 t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
153 t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
154 t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
155 t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
156 t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
157 t->_dmamap_sync = _bus_dmamap_sync;
158
159 t->_dmamem_alloc = _bus_dmamem_alloc;
160 t->_dmamem_free = _bus_dmamem_free;
161 t->_dmamem_map = _bus_dmamem_map;
162 t->_dmamem_unmap = _bus_dmamem_unmap;
163 t->_dmamem_mmap = _bus_dmamem_mmap;
164
165 /*
166 * Initialize the DMA tag used for sgmap-mapped ISA DMA.
167 */
168 t = &ccp->cc_dmat_isa_sgmap;
169 t->_cookie = ccp;
170 t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE;
171 t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE;
172 t->_next_window = NULL;
173 t->_boundary = 0;
174 t->_sgmap = &ccp->cc_isa_sgmap;
175 t->_get_tag = mcpcia_dma_get_tag;
176 t->_dmamap_create = mcpcia_bus_dmamap_create_sgmap;
177 t->_dmamap_destroy = mcpcia_bus_dmamap_destroy_sgmap;
178 t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
179 t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
180 t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
181 t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
182 t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
183 t->_dmamap_sync = _bus_dmamap_sync;
184
185 t->_dmamem_alloc = _bus_dmamem_alloc;
186 t->_dmamem_free = _bus_dmamem_free;
187 t->_dmamem_map = _bus_dmamem_map;
188 t->_dmamem_unmap = _bus_dmamem_unmap;
189 t->_dmamem_mmap = _bus_dmamem_mmap;
190
191 /*
192 * Initialize the SGMAPs.
193 */
194 alpha_sgmap_init(&ccp->cc_dmat_pci_sgmap, &ccp->cc_pci_sgmap,
195 "mcpcia pci sgmap",
196 MCPCIA_PCI_SG_MAPPED_BASE, 0, MCPCIA_PCI_SG_MAPPED_SIZE,
197 sizeof(u_int64_t), NULL, 0);
198
199 alpha_sgmap_init(&ccp->cc_dmat_isa_sgmap, &ccp->cc_isa_sgmap,
200 "mcpcia isa sgmap",
201 MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE,
202 sizeof(u_int64_t), NULL, 0);
203
204 /*
205 * Disable windows first.
206 */
207
208 REGVAL(MCPCIA_W0_BASE(ccp)) = 0;
209 REGVAL(MCPCIA_W1_BASE(ccp)) = 0;
210 REGVAL(MCPCIA_W2_BASE(ccp)) = 0;
211 REGVAL(MCPCIA_W3_BASE(ccp)) = 0;
212 REGVAL(MCPCIA_T0_BASE(ccp)) = 0;
213 REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
214 REGVAL(MCPCIA_T2_BASE(ccp)) = 0;
215 REGVAL(MCPCIA_T3_BASE(ccp)) = 0;
216 alpha_mb();
217
218 /*
219 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB.
220 */
221 REGVAL(MCPCIA_W0_MASK(ccp)) = MCPCIA_WMASK_8M;
222 REGVAL(MCPCIA_T0_BASE(ccp)) =
223 ccp->cc_isa_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
224 alpha_mb();
225 REGVAL(MCPCIA_W0_BASE(ccp)) =
226 MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_ISA_SG_MAPPED_BASE;
227 alpha_mb();
228
229 MCPCIA_SGTLB_INVALIDATE(ccp);
230
231 /*
232 * Set up window 1 as a 2 GB Direct-mapped window starting at 2GB.
233 */
234
235 REGVAL(MCPCIA_W1_MASK(ccp)) = MCPCIA_WMASK_2G;
236 REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
237 alpha_mb();
238 REGVAL(MCPCIA_W1_BASE(ccp)) =
239 MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
240 alpha_mb();
241
242 /*
243 * Set up window 2 as a 1G SGMAP-mapped window starting at 1G.
244 */
245
246 REGVAL(MCPCIA_W2_MASK(ccp)) = MCPCIA_WMASK_1G;
247 REGVAL(MCPCIA_T2_BASE(ccp)) =
248 ccp->cc_pci_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
249 alpha_mb();
250 REGVAL(MCPCIA_W2_BASE(ccp)) =
251 MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_PCI_SG_MAPPED_BASE;
252 alpha_mb();
253
254 /* XXX XXX BEGIN XXX XXX */
255 { /* XXX */
256 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
257 alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */
258 } /* XXX */
259 /* XXX XXX END XXX XXX */
260 }
261
262 /*
263 * Return the bus dma tag to be used for the specified bus type.
264 * INTERNAL USE ONLY!
265 */
266 bus_dma_tag_t
267 mcpcia_dma_get_tag(t, bustype)
268 bus_dma_tag_t t;
269 alpha_bus_t bustype;
270 {
271 struct mcpcia_config *ccp = t->_cookie;
272
273 switch (bustype) {
274 case ALPHA_BUS_PCI:
275 case ALPHA_BUS_EISA:
276 /*
277 * Start off using the direct-mapped window. We will
278 * automatically fall backed onto the chained PCI SGMAP
279 * window if necessary.
280 */
281 return (&ccp->cc_dmat_direct);
282
283 case ALPHA_BUS_ISA:
284 /*
285 * ISA doesn't have enough address bits to use
286 * the direct-mapped DMA window, so we must use
287 * SGMAPs.
288 */
289 return (&ccp->cc_dmat_isa_sgmap);
290
291 default:
292 panic("mcpcia_dma_get_tag: shouldn't be here, really...");
293 }
294 }
295
296 /*
297 * Create a MCPCIA SGMAP-mapped DMA map.
298 */
299 int
300 mcpcia_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
301 flags, dmamp)
302 bus_dma_tag_t t;
303 bus_size_t size;
304 int nsegments;
305 bus_size_t maxsegsz;
306 bus_size_t boundary;
307 int flags;
308 bus_dmamap_t *dmamp;
309 {
310 bus_dmamap_t map;
311 int error;
312
313 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
314 boundary, flags, dmamp);
315 if (error)
316 return (error);
317
318 map = *dmamp;
319
320 if (flags & BUS_DMA_ALLOCNOW) {
321 error = alpha_sgmap_alloc(map, round_page(size),
322 t->_sgmap, flags);
323 if (error)
324 mcpcia_bus_dmamap_destroy_sgmap(t, map);
325 }
326
327 return (error);
328 }
329
330 /*
331 * Destroy a MCPCIA SGMAP-mapped DMA map.
332 */
333 void
334 mcpcia_bus_dmamap_destroy_sgmap(t, map)
335 bus_dma_tag_t t;
336 bus_dmamap_t map;
337 {
338
339 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
340 alpha_sgmap_free(map, t->_sgmap);
341
342 _bus_dmamap_destroy(t, map);
343 }
344
345 /*
346 * Load a MCPCIA SGMAP-mapped DMA map with a linear buffer.
347 */
348 int
349 mcpcia_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
350 bus_dma_tag_t t;
351 bus_dmamap_t map;
352 void *buf;
353 bus_size_t buflen;
354 struct proc *p;
355 int flags;
356 {
357 int error;
358 struct mcpcia_config *ccp = t->_cookie;
359
360 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
361 t->_sgmap);
362 if (error == 0)
363 MCPCIA_SGTLB_INVALIDATE(ccp);
364 return (error);
365 }
366
367 /*
368 * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain.
369 */
370 int
371 mcpcia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
372 bus_dma_tag_t t;
373 bus_dmamap_t map;
374 struct mbuf *m;
375 int flags;
376 {
377 int error;
378 struct mcpcia_config *ccp = t->_cookie;
379
380 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
381 if (error == 0)
382 MCPCIA_SGTLB_INVALIDATE(ccp);
383 return (error);
384 }
385
386 /*
387 * Load a MCPCIA SGMAP-mapped DMA map with a uio.
388 */
389 int
390 mcpcia_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
391 bus_dma_tag_t t;
392 bus_dmamap_t map;
393 struct uio *uio;
394 int flags;
395 {
396 int error;
397 struct mcpcia_config *ccp = t->_cookie;
398
399 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
400 if (error == 0)
401 MCPCIA_SGTLB_INVALIDATE(ccp);
402 return (error);
403 }
404
405 /*
406 * Load a MCPCIA SGMAP-mapped DMA map with raw memory.
407 */
408 int
409 mcpcia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
410 bus_dma_tag_t t;
411 bus_dmamap_t map;
412 bus_dma_segment_t *segs;
413 int nsegs;
414 bus_size_t size;
415 int flags;
416 {
417 int error;
418 struct mcpcia_config *ccp = t->_cookie;
419
420 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
421 t->_sgmap);
422 if (error == 0)
423 MCPCIA_SGTLB_INVALIDATE(ccp);
424 return (error);
425 }
426
427 /*
428 * Unload a MCPCIA DMA map.
429 */
430 void
431 mcpcia_bus_dmamap_unload_sgmap(t, map)
432 bus_dma_tag_t t;
433 bus_dmamap_t map;
434 {
435 struct mcpcia_config *ccp = t->_cookie;
436
437 /*
438 * Invalidate any SGMAP page table entries used by this mapping.
439 */
440 pci_sgmap_pte64_unload(t, map, t->_sgmap);
441 MCPCIA_SGTLB_INVALIDATE(ccp);
442
443 /*
444 * Do the generic bits of the unload.
445 */
446 _bus_dmamap_unload(t, map);
447 }
448