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