lca_dma.c revision 1.14 1 /* $NetBSD: lca_dma.c,v 1.14 2001/01/03 19:16:00 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 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: lca_dma.c,v 1.14 2001/01/03 19:16:00 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
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/lcareg.h>
58 #include <alpha/pci/lcavar.h>
59
60 bus_dma_tag_t lca_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
61
62 int lca_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
63 bus_size_t, struct proc *, int));
64
65 int lca_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
66 struct mbuf *, int));
67
68 int lca_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
69 struct uio *, int));
70
71 int lca_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
72 bus_dma_segment_t *, int, bus_size_t, int));
73
74 void lca_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
75
76 /*
77 * Direct-mapped window: 1G at 1G
78 */
79 #define LCA_DIRECT_MAPPED_BASE (1*1024*1024*1024)
80 #define LCA_DIRECT_MAPPED_SIZE (1*1024*1024*1024)
81
82 /*
83 * SGMAP window: 8M at 8M
84 */
85 #define LCA_SGMAP_MAPPED_BASE (8*1024*1024)
86 #define LCA_SGMAP_MAPPED_SIZE (8*1024*1024)
87
88 /*
89 * Macro to flush LCA scatter/gather TLB.
90 */
91 #define LCA_TLB_INVALIDATE() \
92 do { \
93 alpha_mb(); \
94 REGVAL64(LCA_IOC_TBIA) = 0; \
95 alpha_mb(); \
96 } while (0)
97
98 void
99 lca_dma_init(lcp)
100 struct lca_config *lcp;
101 {
102 bus_dma_tag_t t;
103
104 /*
105 * Initialize the DMA tag used for direct-mapped DMA.
106 */
107 t = &lcp->lc_dmat_direct;
108 t->_cookie = lcp;
109 t->_wbase = LCA_DIRECT_MAPPED_BASE;
110 t->_wsize = LCA_DIRECT_MAPPED_SIZE;
111 t->_next_window = NULL;
112 t->_boundary = 0;
113 t->_sgmap = NULL;
114 t->_get_tag = lca_dma_get_tag;
115 t->_dmamap_create = _bus_dmamap_create;
116 t->_dmamap_destroy = _bus_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 DMA.
132 */
133 t = &lcp->lc_dmat_sgmap;
134 t->_cookie = lcp;
135 t->_wbase = LCA_SGMAP_MAPPED_BASE;
136 t->_wsize = LCA_SGMAP_MAPPED_SIZE;
137 t->_next_window = NULL;
138 t->_boundary = 0;
139 t->_sgmap = &lcp->lc_sgmap;
140 t->_get_tag = lca_dma_get_tag;
141 t->_dmamap_create = alpha_sgmap_dmamap_create;
142 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
143 t->_dmamap_load = lca_bus_dmamap_load_sgmap;
144 t->_dmamap_load_mbuf = lca_bus_dmamap_load_mbuf_sgmap;
145 t->_dmamap_load_uio = lca_bus_dmamap_load_uio_sgmap;
146 t->_dmamap_load_raw = lca_bus_dmamap_load_raw_sgmap;
147 t->_dmamap_unload = lca_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 * The firmware has set up window 1 as a 1G direct-mapped DMA
158 * window beginning at 1G. We leave it alone. Disable
159 * window 0.
160 */
161 REGVAL64(LCA_IOC_W_BASE0) = 0;
162 alpha_mb();
163
164 /*
165 * Initialize the SGMAP.
166 */
167 alpha_sgmap_init(t, &lcp->lc_sgmap, "lca_sgmap",
168 LCA_SGMAP_MAPPED_BASE, 0, LCA_SGMAP_MAPPED_SIZE,
169 sizeof(u_int64_t), NULL, 0);
170
171 /*
172 * Set up window 0 as an 8MB SGMAP-mapped window
173 * starting at 8MB.
174 */
175 REGVAL64(LCA_IOC_W_BASE0) = LCA_SGMAP_MAPPED_BASE |
176 IOC_W_BASE_SG | IOC_W_BASE_WEN;
177 alpha_mb();
178
179 REGVAL64(LCA_IOC_W_MASK0) = IOC_W_MASK_8M;
180 alpha_mb();
181
182 if ((lcp->lc_sgmap.aps_ptpa & IOC_W_T_BASE) !=
183 lcp->lc_sgmap.aps_ptpa)
184 panic("lca_dma_init: bad page table address");
185 REGVAL64(LCA_IOC_W_T_BASE0) = lcp->lc_sgmap.aps_ptpa;
186 alpha_mb();
187
188 /* Enble the scatter/gather TLB. */
189 REGVAL64(LCA_IOC_TB_ENA) = IOC_TB_ENA_TEN;
190 alpha_mb();
191
192 LCA_TLB_INVALIDATE();
193
194 /* XXX XXX BEGIN XXX XXX */
195 { /* XXX */
196 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
197 alpha_XXX_dmamap_or = LCA_DIRECT_MAPPED_BASE; /* XXX */
198 } /* XXX */
199 /* XXX XXX END XXX XXX */
200 }
201
202 /*
203 * Return the bus dma tag to be used for the specified bus type.
204 * INTERNAL USE ONLY!
205 */
206 bus_dma_tag_t
207 lca_dma_get_tag(t, bustype)
208 bus_dma_tag_t t;
209 alpha_bus_t bustype;
210 {
211 struct lca_config *lcp = t->_cookie;
212
213 switch (bustype) {
214 case ALPHA_BUS_PCI:
215 case ALPHA_BUS_EISA:
216 /*
217 * Systems with an LCA can only support 1G
218 * of memory, so we use the direct-mapped window
219 * on busses that have 32-bit DMA.
220 */
221 return (&lcp->lc_dmat_direct);
222
223 case ALPHA_BUS_ISA:
224 /*
225 * ISA doesn't have enough address bits to use
226 * the direct-mapped DMA window, so we must use
227 * SGMAPs.
228 */
229 return (&lcp->lc_dmat_sgmap);
230
231 default:
232 panic("lca_dma_get_tag: shouldn't be here, really...");
233 }
234 }
235
236 /*
237 * Load an LCA SGMAP-mapped DMA map with a linear buffer.
238 */
239 int
240 lca_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
241 bus_dma_tag_t t;
242 bus_dmamap_t map;
243 void *buf;
244 bus_size_t buflen;
245 struct proc *p;
246 int flags;
247 {
248 int error;
249
250 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
251 t->_sgmap);
252 if (error == 0)
253 LCA_TLB_INVALIDATE();
254
255 return (error);
256 }
257
258 /*
259 * Load an LCA SGMAP-mapped DMA map with an mbuf chain.
260 */
261 int
262 lca_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
263 bus_dma_tag_t t;
264 bus_dmamap_t map;
265 struct mbuf *m;
266 int flags;
267 {
268 int error;
269
270 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
271 if (error == 0)
272 LCA_TLB_INVALIDATE();
273
274 return (error);
275 }
276
277 /*
278 * Load an LCA SGMAP-mapped DMA map with a uio.
279 */
280 int
281 lca_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
282 bus_dma_tag_t t;
283 bus_dmamap_t map;
284 struct uio *uio;
285 int flags;
286 {
287 int error;
288
289 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
290 if (error == 0)
291 LCA_TLB_INVALIDATE();
292
293 return (error);
294 }
295
296 /*
297 * Load an LCA SGMAP-mapped DMA map with raw memory.
298 */
299 int
300 lca_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
301 bus_dma_tag_t t;
302 bus_dmamap_t map;
303 bus_dma_segment_t *segs;
304 int nsegs;
305 bus_size_t size;
306 int flags;
307 {
308 int error;
309
310 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
311 t->_sgmap);
312 if (error == 0)
313 LCA_TLB_INVALIDATE();
314
315 return (error);
316 }
317
318 /*
319 * Unload an LCA DMA map.
320 */
321 void
322 lca_bus_dmamap_unload_sgmap(t, map)
323 bus_dma_tag_t t;
324 bus_dmamap_t map;
325 {
326
327 /*
328 * Invalidate any SGMAP page table entries used by this
329 * mapping.
330 */
331 pci_sgmap_pte64_unload(t, map, t->_sgmap);
332 LCA_TLB_INVALIDATE();
333
334 /*
335 * Do the generic bits of the unload.
336 */
337 _bus_dmamap_unload(t, map);
338 }
339