lca_dma.c revision 1.12 1 /* $NetBSD: lca_dma.c,v 1.12 1998/08/14 16:50:04 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.12 1998/08/14 16:50:04 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/lcareg.h>
57 #include <alpha/pci/lcavar.h>
58
59 bus_dma_tag_t lca_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
60
61 int lca_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
62 bus_size_t, bus_size_t, int, bus_dmamap_t *));
63
64 void lca_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
65
66 int lca_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
67 bus_size_t, struct proc *, int));
68
69 int lca_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
70 struct mbuf *, int));
71
72 int lca_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
73 struct uio *, int));
74
75 int lca_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
76 bus_dma_segment_t *, int, bus_size_t, int));
77
78 void lca_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
79
80 /*
81 * Direct-mapped window: 1G at 1G
82 */
83 #define LCA_DIRECT_MAPPED_BASE (1*1024*1024*1024)
84 #define LCA_DIRECT_MAPPED_SIZE (1*1024*1024*1024)
85
86 /*
87 * SGMAP window: 8M at 8M
88 */
89 #define LCA_SGMAP_MAPPED_BASE (8*1024*1024)
90 #define LCA_SGMAP_MAPPED_SIZE (8*1024*1024)
91
92 /*
93 * Macro to flush LCA scatter/gather TLB.
94 */
95 #define LCA_TLB_INVALIDATE() \
96 do { \
97 alpha_mb(); \
98 REGVAL64(LCA_IOC_TBIA) = 0; \
99 alpha_mb(); \
100 } while (0)
101
102 void
103 lca_dma_init(lcp)
104 struct lca_config *lcp;
105 {
106 bus_dma_tag_t t;
107
108 /*
109 * Initialize the DMA tag used for direct-mapped DMA.
110 */
111 t = &lcp->lc_dmat_direct;
112 t->_cookie = lcp;
113 t->_wbase = LCA_DIRECT_MAPPED_BASE;
114 t->_wsize = LCA_DIRECT_MAPPED_SIZE;
115 t->_next_window = NULL;
116 t->_boundary = 0;
117 t->_sgmap = NULL;
118 t->_get_tag = lca_dma_get_tag;
119 t->_dmamap_create = _bus_dmamap_create;
120 t->_dmamap_destroy = _bus_dmamap_destroy;
121 t->_dmamap_load = _bus_dmamap_load_direct;
122 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
123 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
124 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
125 t->_dmamap_unload = _bus_dmamap_unload;
126 t->_dmamap_sync = _bus_dmamap_sync;
127
128 t->_dmamem_alloc = _bus_dmamem_alloc;
129 t->_dmamem_free = _bus_dmamem_free;
130 t->_dmamem_map = _bus_dmamem_map;
131 t->_dmamem_unmap = _bus_dmamem_unmap;
132 t->_dmamem_mmap = _bus_dmamem_mmap;
133
134 /*
135 * Initialize the DMA tag used for sgmap-mapped DMA.
136 */
137 t = &lcp->lc_dmat_sgmap;
138 t->_cookie = lcp;
139 t->_wbase = LCA_SGMAP_MAPPED_BASE;
140 t->_wsize = LCA_SGMAP_MAPPED_SIZE;
141 t->_next_window = NULL;
142 t->_boundary = 0;
143 t->_sgmap = &lcp->lc_sgmap;
144 t->_get_tag = lca_dma_get_tag;
145 t->_dmamap_create = lca_bus_dmamap_create_sgmap;
146 t->_dmamap_destroy = lca_bus_dmamap_destroy_sgmap;
147 t->_dmamap_load = lca_bus_dmamap_load_sgmap;
148 t->_dmamap_load_mbuf = lca_bus_dmamap_load_mbuf_sgmap;
149 t->_dmamap_load_uio = lca_bus_dmamap_load_uio_sgmap;
150 t->_dmamap_load_raw = lca_bus_dmamap_load_raw_sgmap;
151 t->_dmamap_unload = lca_bus_dmamap_unload_sgmap;
152 t->_dmamap_sync = _bus_dmamap_sync;
153
154 t->_dmamem_alloc = _bus_dmamem_alloc;
155 t->_dmamem_free = _bus_dmamem_free;
156 t->_dmamem_map = _bus_dmamem_map;
157 t->_dmamem_unmap = _bus_dmamem_unmap;
158 t->_dmamem_mmap = _bus_dmamem_mmap;
159
160 /*
161 * The firmware has set up window 1 as a 1G direct-mapped DMA
162 * window beginning at 1G. We leave it alone. Disable
163 * window 0.
164 */
165 REGVAL64(LCA_IOC_W_BASE0) = 0;
166 alpha_mb();
167
168 /*
169 * Initialize the SGMAP.
170 */
171 alpha_sgmap_init(t, &lcp->lc_sgmap, "lca_sgmap",
172 LCA_SGMAP_MAPPED_BASE, 0, LCA_SGMAP_MAPPED_SIZE,
173 sizeof(u_int64_t), NULL, 0);
174
175 /*
176 * Set up window 0 as an 8MB SGMAP-mapped window
177 * starting at 8MB.
178 */
179 REGVAL64(LCA_IOC_W_BASE0) = LCA_SGMAP_MAPPED_BASE |
180 IOC_W_BASE_SG | IOC_W_BASE_WEN;
181 alpha_mb();
182
183 REGVAL64(LCA_IOC_W_MASK0) = IOC_W_MASK_8M;
184 alpha_mb();
185
186 if ((lcp->lc_sgmap.aps_ptpa & IOC_W_T_BASE) !=
187 lcp->lc_sgmap.aps_ptpa)
188 panic("lca_dma_init: bad page table address");
189 REGVAL64(LCA_IOC_W_T_BASE0) = lcp->lc_sgmap.aps_ptpa;
190 alpha_mb();
191
192 /* Enble the scatter/gather TLB. */
193 REGVAL64(LCA_IOC_TB_ENA) = IOC_TB_ENA_TEN;
194 alpha_mb();
195
196 LCA_TLB_INVALIDATE();
197
198 /* XXX XXX BEGIN XXX XXX */
199 { /* XXX */
200 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
201 alpha_XXX_dmamap_or = LCA_DIRECT_MAPPED_BASE; /* XXX */
202 } /* XXX */
203 /* XXX XXX END XXX XXX */
204 }
205
206 /*
207 * Return the bus dma tag to be used for the specified bus type.
208 * INTERNAL USE ONLY!
209 */
210 bus_dma_tag_t
211 lca_dma_get_tag(t, bustype)
212 bus_dma_tag_t t;
213 alpha_bus_t bustype;
214 {
215 struct lca_config *lcp = t->_cookie;
216
217 switch (bustype) {
218 case ALPHA_BUS_PCI:
219 case ALPHA_BUS_EISA:
220 /*
221 * Systems with an LCA can only support 1G
222 * of memory, so we use the direct-mapped window
223 * on busses that have 32-bit DMA.
224 */
225 return (&lcp->lc_dmat_direct);
226
227 case ALPHA_BUS_ISA:
228 /*
229 * ISA doesn't have enough address bits to use
230 * the direct-mapped DMA window, so we must use
231 * SGMAPs.
232 */
233 return (&lcp->lc_dmat_sgmap);
234
235 default:
236 panic("lca_dma_get_tag: shouldn't be here, really...");
237 }
238 }
239
240 /*
241 * Create an LCA SGMAP-mapped DMA map.
242 */
243 int
244 lca_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
245 flags, dmamp)
246 bus_dma_tag_t t;
247 bus_size_t size;
248 int nsegments;
249 bus_size_t maxsegsz;
250 bus_size_t boundary;
251 int flags;
252 bus_dmamap_t *dmamp;
253 {
254 bus_dmamap_t map;
255 int error;
256
257 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
258 boundary, flags, dmamp);
259 if (error)
260 return (error);
261
262 map = *dmamp;
263
264 if (flags & BUS_DMA_ALLOCNOW) {
265 error = alpha_sgmap_alloc(map, round_page(size),
266 t->_sgmap, flags);
267 if (error)
268 lca_bus_dmamap_destroy_sgmap(t, map);
269 }
270
271 return (error);
272 }
273
274 /*
275 * Destroy an LCA SGMAP-mapped DMA map.
276 */
277 void
278 lca_bus_dmamap_destroy_sgmap(t, map)
279 bus_dma_tag_t t;
280 bus_dmamap_t map;
281 {
282
283 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
284 alpha_sgmap_free(map, t->_sgmap);
285
286 _bus_dmamap_destroy(t, map);
287 }
288
289 /*
290 * Load an LCA SGMAP-mapped DMA map with a linear buffer.
291 */
292 int
293 lca_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
294 bus_dma_tag_t t;
295 bus_dmamap_t map;
296 void *buf;
297 bus_size_t buflen;
298 struct proc *p;
299 int flags;
300 {
301 int error;
302
303 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
304 t->_sgmap);
305 if (error == 0)
306 LCA_TLB_INVALIDATE();
307
308 return (error);
309 }
310
311 /*
312 * Load an LCA SGMAP-mapped DMA map with an mbuf chain.
313 */
314 int
315 lca_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
316 bus_dma_tag_t t;
317 bus_dmamap_t map;
318 struct mbuf *m;
319 int flags;
320 {
321 int error;
322
323 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
324 if (error == 0)
325 LCA_TLB_INVALIDATE();
326
327 return (error);
328 }
329
330 /*
331 * Load an LCA SGMAP-mapped DMA map with a uio.
332 */
333 int
334 lca_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
335 bus_dma_tag_t t;
336 bus_dmamap_t map;
337 struct uio *uio;
338 int flags;
339 {
340 int error;
341
342 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
343 if (error == 0)
344 LCA_TLB_INVALIDATE();
345
346 return (error);
347 }
348
349 /*
350 * Load an LCA SGMAP-mapped DMA map with raw memory.
351 */
352 int
353 lca_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
354 bus_dma_tag_t t;
355 bus_dmamap_t map;
356 bus_dma_segment_t *segs;
357 int nsegs;
358 bus_size_t size;
359 int flags;
360 {
361 int error;
362
363 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
364 t->_sgmap);
365 if (error == 0)
366 LCA_TLB_INVALIDATE();
367
368 return (error);
369 }
370
371 /*
372 * Unload an LCA DMA map.
373 */
374 void
375 lca_bus_dmamap_unload_sgmap(t, map)
376 bus_dma_tag_t t;
377 bus_dmamap_t map;
378 {
379
380 /*
381 * Invalidate any SGMAP page table entries used by this
382 * mapping.
383 */
384 pci_sgmap_pte64_unload(t, map, t->_sgmap);
385 LCA_TLB_INVALIDATE();
386
387 /*
388 * Do the generic bits of the unload.
389 */
390 _bus_dmamap_unload(t, map);
391 }
392