1 /* $OpenBSD: mcpcia_dma.c,v 1.5 2014/06/14 23:11:20 jmatthew Exp $ */
2 /* $NetBSD: mcpcia_dma.c,v 1.15 2001/07/19 18:55:40 thorpej Exp $ */
3
4 /*-
5 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe and Matthew Jacob of the Numerical Aerospace Simulation
10 * Facility, NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #define _ALPHA_BUS_DMA_PRIVATE
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41
42 #include <uvm/uvm_extern.h>
43
44 #include <machine/bus.h>
45 #include <machine/rpb.h>
46
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <alpha/pci/mcpciareg.h>
50 #include <alpha/pci/mcpciavar.h>
51 #include <alpha/pci/pci_kn300.h>
52
53 bus_dma_tag_t mcpcia_dma_get_tag (bus_dma_tag_t, alpha_bus_t);
54
55 int mcpcia_bus_dmamap_load_sgmap (bus_dma_tag_t, bus_dmamap_t, void *,
56 bus_size_t, struct proc *, int);
57
58 int mcpcia_bus_dmamap_load_mbuf_sgmap (bus_dma_tag_t, bus_dmamap_t,
59 struct mbuf *, int);
60
61 int mcpcia_bus_dmamap_load_uio_sgmap (bus_dma_tag_t, bus_dmamap_t,
62 struct uio *, int);
63
64 int mcpcia_bus_dmamap_load_raw_sgmap (bus_dma_tag_t, bus_dmamap_t,
65 bus_dma_segment_t *, int, bus_size_t, int);
66
67 void mcpcia_bus_dmamap_unload_sgmap (bus_dma_tag_t, bus_dmamap_t);
68
69 /*
70 * Direct-mapped window: 2G at 2G
71 */
72 #define MCPCIA_DIRECT_MAPPED_BASE (2UL*1024UL*1024UL*1024UL)
73 #define MCPCIA_DIRECT_MAPPED_SIZE (2UL*1024UL*1024UL*1024UL)
74
75 /*
76 * SGMAP window for PCI: 1G at 1G
77 */
78 #define MCPCIA_PCI_SG_MAPPED_BASE (1UL*1024UL*1024UL*1024UL)
79 #define MCPCIA_PCI_SG_MAPPED_SIZE (1UL*1024UL*1024UL*1024UL)
80
81 /*
82 * SGMAP window for ISA: 8M at 8M
83 */
84 #define MCPCIA_ISA_SG_MAPPED_BASE (8*1024*1024)
85 #define MCPCIA_ISA_SG_MAPPED_SIZE (8*1024*1024)
86
87 #define MCPCIA_SGTLB_INVALIDATE(ccp) \
88 do { \
89 alpha_mb(); \
90 REGVAL(MCPCIA_SG_TBIA(ccp)) = 0xdeadbeef; \
91 alpha_mb(); \
92 } while (0)
93
94 void
mcpcia_dma_init(ccp)95 mcpcia_dma_init(ccp)
96 struct mcpcia_config *ccp;
97 {
98 bus_dma_tag_t t;
99
100 /*
101 * Initialize the DMA tag used for direct-mapped DMA.
102 */
103 t = &ccp->cc_dmat_direct;
104 t->_cookie = ccp;
105 t->_wbase = MCPCIA_DIRECT_MAPPED_BASE;
106 t->_wsize = MCPCIA_DIRECT_MAPPED_SIZE;
107 t->_next_window = &ccp->cc_dmat_pci_sgmap;
108 t->_boundary = 0;
109 t->_sgmap = NULL;
110 t->_get_tag = mcpcia_dma_get_tag;
111 /*
112 * Since we fall back to sgmap if the direct mapping fails,
113 * we need to set up for sgmap in any case.
114 */
115 t->_dmamap_create = alpha_sgmap_dmamap_create;
116 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
117 t->_dmamap_load = _bus_dmamap_load_direct;
118 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
119 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
120 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
121 t->_dmamap_unload = _bus_dmamap_unload;
122 t->_dmamap_sync = _bus_dmamap_sync;
123
124 t->_dmamem_alloc = _bus_dmamem_alloc;
125 t->_dmamem_free = _bus_dmamem_free;
126 t->_dmamem_map = _bus_dmamem_map;
127 t->_dmamem_unmap = _bus_dmamem_unmap;
128 t->_dmamem_mmap = _bus_dmamem_mmap;
129
130 /*
131 * Initialize the DMA tag used for sgmap-mapped PCI DMA.
132 */
133 t = &ccp->cc_dmat_pci_sgmap;
134 t->_cookie = ccp;
135 t->_wbase = MCPCIA_PCI_SG_MAPPED_BASE;
136 t->_wsize = MCPCIA_PCI_SG_MAPPED_SIZE;
137 t->_next_window = NULL;
138 t->_boundary = 0;
139 t->_sgmap = &ccp->cc_pci_sgmap;
140 t->_get_tag = mcpcia_dma_get_tag;
141 t->_dmamap_create = alpha_sgmap_dmamap_create;
142 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
143 t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
144 t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
145 t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
146 t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
147 t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
148 t->_dmamap_sync = _bus_dmamap_sync;
149
150 t->_dmamem_alloc = _bus_dmamem_alloc;
151 t->_dmamem_free = _bus_dmamem_free;
152 t->_dmamem_map = _bus_dmamem_map;
153 t->_dmamem_unmap = _bus_dmamem_unmap;
154 t->_dmamem_mmap = _bus_dmamem_mmap;
155
156 /*
157 * Initialize the DMA tag used for sgmap-mapped ISA DMA.
158 */
159 t = &ccp->cc_dmat_isa_sgmap;
160 t->_cookie = ccp;
161 t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE;
162 t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE;
163 t->_next_window = NULL;
164 t->_boundary = 0;
165 t->_sgmap = &ccp->cc_isa_sgmap;
166 t->_get_tag = mcpcia_dma_get_tag;
167 t->_dmamap_create = alpha_sgmap_dmamap_create;
168 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
169 t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
170 t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
171 t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
172 t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
173 t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
174 t->_dmamap_sync = _bus_dmamap_sync;
175
176 t->_dmamem_alloc = _bus_dmamem_alloc;
177 t->_dmamem_free = _bus_dmamem_free;
178 t->_dmamem_map = _bus_dmamem_map;
179 t->_dmamem_unmap = _bus_dmamem_unmap;
180 t->_dmamem_mmap = _bus_dmamem_mmap;
181
182 /*
183 * Initialize the SGMAPs.
184 */
185 alpha_sgmap_init(&ccp->cc_dmat_pci_sgmap, &ccp->cc_pci_sgmap,
186 "mcpcia pci sgmap",
187 MCPCIA_PCI_SG_MAPPED_BASE, 0, MCPCIA_PCI_SG_MAPPED_SIZE,
188 sizeof(u_int64_t), NULL, 0);
189
190 alpha_sgmap_init(&ccp->cc_dmat_isa_sgmap, &ccp->cc_isa_sgmap,
191 "mcpcia isa sgmap",
192 MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE,
193 sizeof(u_int64_t), NULL, 0);
194
195 /*
196 * Disable windows first.
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 REGVAL(MCPCIA_W1_MASK(ccp)) = MCPCIA_WMASK_2G;
225 REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
226 alpha_mb();
227 REGVAL(MCPCIA_W1_BASE(ccp)) =
228 MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
229 alpha_mb();
230
231 /*
232 * Set up window 2 as a 1G SGMAP-mapped window starting at 1G.
233 */
234 REGVAL(MCPCIA_W2_MASK(ccp)) = MCPCIA_WMASK_1G;
235 REGVAL(MCPCIA_T2_BASE(ccp)) =
236 ccp->cc_pci_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
237 alpha_mb();
238 REGVAL(MCPCIA_W2_BASE(ccp)) =
239 MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_PCI_SG_MAPPED_BASE;
240 alpha_mb();
241
242 /* XXX XXX BEGIN XXX XXX */
243 { /* XXX */
244 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
245 alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */
246 } /* XXX */
247 /* XXX XXX END XXX XXX */
248 }
249
250 /*
251 * Return the bus dma tag to be used for the specified bus type.
252 * INTERNAL USE ONLY!
253 */
254 bus_dma_tag_t
mcpcia_dma_get_tag(t,bustype)255 mcpcia_dma_get_tag(t, bustype)
256 bus_dma_tag_t t;
257 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
mcpcia_bus_dmamap_load_sgmap(t,map,buf,buflen,p,flags)288 mcpcia_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
289 bus_dma_tag_t t;
290 bus_dmamap_t map;
291 void *buf;
292 bus_size_t buflen;
293 struct proc *p;
294 int flags;
295 {
296 int error;
297 struct mcpcia_config *ccp = t->_cookie;
298
299 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
300 t->_sgmap);
301 if (error == 0)
302 MCPCIA_SGTLB_INVALIDATE(ccp);
303 return (error);
304 }
305
306 /*
307 * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain.
308 */
309 int
mcpcia_bus_dmamap_load_mbuf_sgmap(t,map,m,flags)310 mcpcia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
311 bus_dma_tag_t t;
312 bus_dmamap_t map;
313 struct mbuf *m;
314 int flags;
315 {
316 int error;
317 struct mcpcia_config *ccp = t->_cookie;
318
319 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
320 if (error == 0)
321 MCPCIA_SGTLB_INVALIDATE(ccp);
322 return (error);
323 }
324
325 /*
326 * Load a MCPCIA SGMAP-mapped DMA map with a uio.
327 */
328 int
mcpcia_bus_dmamap_load_uio_sgmap(t,map,uio,flags)329 mcpcia_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
330 bus_dma_tag_t t;
331 bus_dmamap_t map;
332 struct uio *uio;
333 int flags;
334 {
335 int error;
336 struct mcpcia_config *ccp = t->_cookie;
337
338 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
339 if (error == 0)
340 MCPCIA_SGTLB_INVALIDATE(ccp);
341 return (error);
342 }
343
344 /*
345 * Load a MCPCIA SGMAP-mapped DMA map with raw memory.
346 */
347 int
mcpcia_bus_dmamap_load_raw_sgmap(t,map,segs,nsegs,size,flags)348 mcpcia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
349 bus_dma_tag_t t;
350 bus_dmamap_t map;
351 bus_dma_segment_t *segs;
352 int nsegs;
353 bus_size_t size;
354 int flags;
355 {
356 int error;
357 struct mcpcia_config *ccp = t->_cookie;
358
359 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
360 t->_sgmap);
361 if (error == 0)
362 MCPCIA_SGTLB_INVALIDATE(ccp);
363 return (error);
364 }
365
366 /*
367 * Unload a MCPCIA DMA map.
368 */
369 void
mcpcia_bus_dmamap_unload_sgmap(t,map)370 mcpcia_bus_dmamap_unload_sgmap(t, map)
371 bus_dma_tag_t t;
372 bus_dmamap_t map;
373 {
374 struct mcpcia_config *ccp = t->_cookie;
375
376 /*
377 * Invalidate any SGMAP page table entries used by this mapping.
378 */
379 pci_sgmap_pte64_unload(t, map, t->_sgmap);
380 MCPCIA_SGTLB_INVALIDATE(ccp);
381
382 /*
383 * Do the generic bits of the unload.
384 */
385 _bus_dmamap_unload(t, map);
386 }
387