mcpcia_dma.c revision 1.20 1 /* $NetBSD: mcpcia_dma.c,v 1.20 2011/07/01 19:19:49 dyoung 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34
35 __KERNEL_RCSID(0, "$NetBSD: mcpcia_dma.c,v 1.20 2011/07/01 19:19:49 dyoung Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42
43 #define _ALPHA_BUS_DMA_PRIVATE
44 #include <sys/bus.h>
45
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 #include <alpha/pci/mcpciareg.h>
49 #include <alpha/pci/mcpciavar.h>
50 #include <alpha/pci/pci_kn300.h>
51
52 bus_dma_tag_t mcpcia_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
53
54 int mcpcia_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
55 bus_size_t, struct proc *, int);
56
57 int mcpcia_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
58 struct mbuf *, int);
59
60 int mcpcia_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
61 struct uio *, int);
62
63 int mcpcia_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
64 bus_dma_segment_t *, int, bus_size_t, int);
65
66 void mcpcia_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
67
68 /*
69 * Direct-mapped window: 2G at 2G
70 */
71 #define MCPCIA_DIRECT_MAPPED_BASE (2UL*1024UL*1024UL*1024UL)
72 #define MCPCIA_DIRECT_MAPPED_SIZE (2UL*1024UL*1024UL*1024UL)
73
74 /*
75 * SGMAP window for PCI: 1G at 1G
76 */
77 #define MCPCIA_PCI_SG_MAPPED_BASE (1UL*1024UL*1024UL*1024UL)
78 #define MCPCIA_PCI_SG_MAPPED_SIZE (1UL*1024UL*1024UL*1024UL)
79
80 /*
81 * SGMAP window for ISA: 8M at 8M
82 */
83 #define MCPCIA_ISA_SG_MAPPED_BASE (8*1024*1024)
84 #define MCPCIA_ISA_SG_MAPPED_SIZE (8*1024*1024)
85
86 /* MCPCIA has a 256-byte out-bound DMA prefetch threshold. */
87 #define MCPCIA_SG_MAPPED_PFTHRESH 256
88
89 #define MCPCIA_SGTLB_INVALIDATE(ccp) \
90 do { \
91 alpha_mb(); \
92 REGVAL(MCPCIA_SG_TBIA(ccp)) = 0xdeadbeef; \
93 alpha_mb(); \
94 } while (0)
95
96 void
97 mcpcia_dma_init(struct mcpcia_config *ccp)
98 {
99 bus_dma_tag_t t;
100
101 /*
102 * Initialize the DMA tag used for direct-mapped DMA.
103 */
104 t = &ccp->cc_dmat_direct;
105 t->_cookie = ccp;
106 t->_wbase = MCPCIA_DIRECT_MAPPED_BASE;
107 t->_wsize = MCPCIA_DIRECT_MAPPED_SIZE;
108 t->_next_window = &ccp->cc_dmat_pci_sgmap;
109 t->_boundary = 0;
110 t->_sgmap = NULL;
111 t->_get_tag = mcpcia_dma_get_tag;
112 t->_dmamap_create = _bus_dmamap_create;
113 t->_dmamap_destroy = _bus_dmamap_destroy;
114 t->_dmamap_load = _bus_dmamap_load_direct;
115 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
116 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
117 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
118 t->_dmamap_unload = _bus_dmamap_unload;
119 t->_dmamap_sync = _bus_dmamap_sync;
120
121 t->_dmamem_alloc = _bus_dmamem_alloc;
122 t->_dmamem_free = _bus_dmamem_free;
123 t->_dmamem_map = _bus_dmamem_map;
124 t->_dmamem_unmap = _bus_dmamem_unmap;
125 t->_dmamem_mmap = _bus_dmamem_mmap;
126
127 /*
128 * Initialize the DMA tag used for sgmap-mapped PCI DMA.
129 */
130 t = &ccp->cc_dmat_pci_sgmap;
131 t->_cookie = ccp;
132 t->_wbase = MCPCIA_PCI_SG_MAPPED_BASE;
133 t->_wsize = MCPCIA_PCI_SG_MAPPED_SIZE;
134 t->_next_window = NULL;
135 t->_boundary = 0;
136 t->_sgmap = &ccp->cc_pci_sgmap;
137 t->_pfthresh = MCPCIA_SG_MAPPED_PFTHRESH;
138 t->_get_tag = mcpcia_dma_get_tag;
139 t->_dmamap_create = alpha_sgmap_dmamap_create;
140 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
141 t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
142 t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
143 t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
144 t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
145 t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
146 t->_dmamap_sync = _bus_dmamap_sync;
147
148 t->_dmamem_alloc = _bus_dmamem_alloc;
149 t->_dmamem_free = _bus_dmamem_free;
150 t->_dmamem_map = _bus_dmamem_map;
151 t->_dmamem_unmap = _bus_dmamem_unmap;
152 t->_dmamem_mmap = _bus_dmamem_mmap;
153
154 /*
155 * Initialize the DMA tag used for sgmap-mapped ISA DMA.
156 */
157 t = &ccp->cc_dmat_isa_sgmap;
158 t->_cookie = ccp;
159 t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE;
160 t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE;
161 t->_next_window = NULL;
162 t->_boundary = 0;
163 t->_sgmap = &ccp->cc_isa_sgmap;
164 t->_pfthresh = MCPCIA_SG_MAPPED_PFTHRESH;
165 t->_get_tag = mcpcia_dma_get_tag;
166 t->_dmamap_create = alpha_sgmap_dmamap_create;
167 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
168 t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
169 t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
170 t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
171 t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
172 t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
173 t->_dmamap_sync = _bus_dmamap_sync;
174
175 t->_dmamem_alloc = _bus_dmamem_alloc;
176 t->_dmamem_free = _bus_dmamem_free;
177 t->_dmamem_map = _bus_dmamem_map;
178 t->_dmamem_unmap = _bus_dmamem_unmap;
179 t->_dmamem_mmap = _bus_dmamem_mmap;
180
181 /*
182 * Initialize the SGMAPs.
183 */
184 alpha_sgmap_init(&ccp->cc_dmat_pci_sgmap, &ccp->cc_pci_sgmap,
185 "mcpcia pci sgmap",
186 MCPCIA_PCI_SG_MAPPED_BASE, 0, MCPCIA_PCI_SG_MAPPED_SIZE,
187 sizeof(u_int64_t), NULL, 0);
188
189 alpha_sgmap_init(&ccp->cc_dmat_isa_sgmap, &ccp->cc_isa_sgmap,
190 "mcpcia isa sgmap",
191 MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE,
192 sizeof(u_int64_t), NULL, 0);
193
194 /*
195 * Disable windows first.
196 */
197
198 REGVAL(MCPCIA_W0_BASE(ccp)) = 0;
199 REGVAL(MCPCIA_W1_BASE(ccp)) = 0;
200 REGVAL(MCPCIA_W2_BASE(ccp)) = 0;
201 REGVAL(MCPCIA_W3_BASE(ccp)) = 0;
202 REGVAL(MCPCIA_T0_BASE(ccp)) = 0;
203 REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
204 REGVAL(MCPCIA_T2_BASE(ccp)) = 0;
205 REGVAL(MCPCIA_T3_BASE(ccp)) = 0;
206 alpha_mb();
207
208 /*
209 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB.
210 */
211 REGVAL(MCPCIA_W0_MASK(ccp)) = MCPCIA_WMASK_8M;
212 REGVAL(MCPCIA_T0_BASE(ccp)) =
213 ccp->cc_isa_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
214 alpha_mb();
215 REGVAL(MCPCIA_W0_BASE(ccp)) =
216 MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_ISA_SG_MAPPED_BASE;
217 alpha_mb();
218
219 MCPCIA_SGTLB_INVALIDATE(ccp);
220
221 /*
222 * Set up window 1 as a 2 GB Direct-mapped window starting at 2GB.
223 */
224
225 REGVAL(MCPCIA_W1_MASK(ccp)) = MCPCIA_WMASK_2G;
226 REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
227 alpha_mb();
228 REGVAL(MCPCIA_W1_BASE(ccp)) =
229 MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
230 alpha_mb();
231
232 /*
233 * Set up window 2 as a 1G SGMAP-mapped window starting at 1G.
234 */
235
236 REGVAL(MCPCIA_W2_MASK(ccp)) = MCPCIA_WMASK_1G;
237 REGVAL(MCPCIA_T2_BASE(ccp)) =
238 ccp->cc_pci_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
239 alpha_mb();
240 REGVAL(MCPCIA_W2_BASE(ccp)) =
241 MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_PCI_SG_MAPPED_BASE;
242 alpha_mb();
243
244 /* XXX XXX BEGIN XXX XXX */
245 { /* XXX */
246 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
247 alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */
248 } /* XXX */
249 /* XXX XXX END XXX XXX */
250 }
251
252 /*
253 * Return the bus dma tag to be used for the specified bus type.
254 * INTERNAL USE ONLY!
255 */
256 bus_dma_tag_t
257 mcpcia_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
258 {
259 struct mcpcia_config *ccp = t->_cookie;
260
261 switch (bustype) {
262 case ALPHA_BUS_PCI:
263 case ALPHA_BUS_EISA:
264 /*
265 * Start off using the direct-mapped window. We will
266 * automatically fall backed onto the chained PCI SGMAP
267 * window if necessary.
268 */
269 return (&ccp->cc_dmat_direct);
270
271 case ALPHA_BUS_ISA:
272 /*
273 * ISA doesn't have enough address bits to use
274 * the direct-mapped DMA window, so we must use
275 * SGMAPs.
276 */
277 return (&ccp->cc_dmat_isa_sgmap);
278
279 default:
280 panic("mcpcia_dma_get_tag: shouldn't be here, really...");
281 }
282 }
283
284 /*
285 * Load a MCPCIA SGMAP-mapped DMA map with a linear buffer.
286 */
287 int
288 mcpcia_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags)
289 {
290 int error;
291 struct mcpcia_config *ccp = t->_cookie;
292
293 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
294 t->_sgmap);
295 if (error == 0)
296 MCPCIA_SGTLB_INVALIDATE(ccp);
297 return (error);
298 }
299
300 /*
301 * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain.
302 */
303 int
304 mcpcia_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags)
305 {
306 int error;
307 struct mcpcia_config *ccp = t->_cookie;
308
309 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
310 if (error == 0)
311 MCPCIA_SGTLB_INVALIDATE(ccp);
312 return (error);
313 }
314
315 /*
316 * Load a MCPCIA SGMAP-mapped DMA map with a uio.
317 */
318 int
319 mcpcia_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
320 {
321 int error;
322 struct mcpcia_config *ccp = t->_cookie;
323
324 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
325 if (error == 0)
326 MCPCIA_SGTLB_INVALIDATE(ccp);
327 return (error);
328 }
329
330 /*
331 * Load a MCPCIA SGMAP-mapped DMA map with raw memory.
332 */
333 int
334 mcpcia_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
335 {
336 int error;
337 struct mcpcia_config *ccp = t->_cookie;
338
339 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
340 t->_sgmap);
341 if (error == 0)
342 MCPCIA_SGTLB_INVALIDATE(ccp);
343 return (error);
344 }
345
346 /*
347 * Unload a MCPCIA DMA map.
348 */
349 void
350 mcpcia_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
351 {
352 struct mcpcia_config *ccp = t->_cookie;
353
354 /*
355 * Invalidate any SGMAP page table entries used by this mapping.
356 */
357 pci_sgmap_pte64_unload(t, map, t->_sgmap);
358 MCPCIA_SGTLB_INVALIDATE(ccp);
359
360 /*
361 * Do the generic bits of the unload.
362 */
363 _bus_dmamap_unload(t, map);
364 }
365