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