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