lca_dma.c revision 1.19 1 /* $NetBSD: lca_dma.c,v 1.19 2009/03/14 14:45:53 dsl 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34
35 __KERNEL_RCSID(0, "$NetBSD: lca_dma.c,v 1.19 2009/03/14 14:45:53 dsl Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42
43 #include <uvm/uvm_extern.h>
44
45 #define _ALPHA_BUS_DMA_PRIVATE
46 #include <machine/bus.h>
47
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <alpha/pci/lcareg.h>
51 #include <alpha/pci/lcavar.h>
52
53 bus_dma_tag_t lca_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
54
55 int lca_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
56 bus_size_t, struct proc *, int);
57
58 int lca_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
59 struct mbuf *, int);
60
61 int lca_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
62 struct uio *, int);
63
64 int lca_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 lca_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
68
69 /*
70 * Direct-mapped window: 1G at 1G
71 */
72 #define LCA_DIRECT_MAPPED_BASE (1*1024*1024*1024)
73 #define LCA_DIRECT_MAPPED_SIZE (1*1024*1024*1024)
74
75 /*
76 * SGMAP window: 8M at 8M
77 */
78 #define LCA_SGMAP_MAPPED_BASE (8*1024*1024)
79 #define LCA_SGMAP_MAPPED_SIZE (8*1024*1024)
80
81 /*
82 * The LCA doesn't have a DMA prefetch threshold. However, it is known
83 * to lose if we don't allocate a spill page. So initialize it to 256.
84 */
85 #define LCA_SGMAP_PFTHRESH 256
86
87 /*
88 * Macro to flush LCA scatter/gather TLB.
89 */
90 #define LCA_TLB_INVALIDATE() \
91 do { \
92 alpha_mb(); \
93 REGVAL64(LCA_IOC_TBIA) = 0; \
94 alpha_mb(); \
95 } while (0)
96
97 void
98 lca_dma_init(lcp)
99 struct lca_config *lcp;
100 {
101 bus_dma_tag_t t;
102
103 /*
104 * Initialize the DMA tag used for direct-mapped DMA.
105 */
106 t = &lcp->lc_dmat_direct;
107 t->_cookie = lcp;
108 t->_wbase = LCA_DIRECT_MAPPED_BASE;
109 t->_wsize = LCA_DIRECT_MAPPED_SIZE;
110 t->_next_window = NULL;
111 t->_boundary = 0;
112 t->_sgmap = NULL;
113 t->_get_tag = lca_dma_get_tag;
114 t->_dmamap_create = _bus_dmamap_create;
115 t->_dmamap_destroy = _bus_dmamap_destroy;
116 t->_dmamap_load = _bus_dmamap_load_direct;
117 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
118 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
119 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
120 t->_dmamap_unload = _bus_dmamap_unload;
121 t->_dmamap_sync = _bus_dmamap_sync;
122
123 t->_dmamem_alloc = _bus_dmamem_alloc;
124 t->_dmamem_free = _bus_dmamem_free;
125 t->_dmamem_map = _bus_dmamem_map;
126 t->_dmamem_unmap = _bus_dmamem_unmap;
127 t->_dmamem_mmap = _bus_dmamem_mmap;
128
129 /*
130 * Initialize the DMA tag used for sgmap-mapped DMA.
131 */
132 t = &lcp->lc_dmat_sgmap;
133 t->_cookie = lcp;
134 t->_wbase = LCA_SGMAP_MAPPED_BASE;
135 t->_wsize = LCA_SGMAP_MAPPED_SIZE;
136 t->_next_window = NULL;
137 t->_boundary = 0;
138 t->_sgmap = &lcp->lc_sgmap;
139 t->_pfthresh = LCA_SGMAP_PFTHRESH;
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 /* Initialize window 1 as a direct-mapped window. */
157 REGVAL64(LCA_IOC_W_BASE1) = LCA_DIRECT_MAPPED_BASE | IOC_W_BASE_WEN;
158 REGVAL64(LCA_IOC_W_MASK1) = IOC_W_MASK_1G;
159 alpha_mb();
160
161 /* Disable window 0 while we set it up. */
162 REGVAL64(LCA_IOC_W_BASE0) = 0;
163 alpha_mb();
164
165 /*
166 * Initialize the SGMAP.
167 */
168 alpha_sgmap_init(t, &lcp->lc_sgmap, "lca_sgmap",
169 LCA_SGMAP_MAPPED_BASE, 0, LCA_SGMAP_MAPPED_SIZE,
170 sizeof(u_int64_t), NULL, 0);
171
172 /*
173 * Set up window 0 as an 8MB SGMAP-mapped window
174 * starting at 8MB.
175 */
176 REGVAL64(LCA_IOC_W_BASE0) = LCA_SGMAP_MAPPED_BASE |
177 IOC_W_BASE_SG | IOC_W_BASE_WEN;
178 REGVAL64(LCA_IOC_W_MASK0) = IOC_W_MASK_8M;
179 alpha_mb();
180
181 if ((lcp->lc_sgmap.aps_ptpa & IOC_W_T_BASE) !=
182 lcp->lc_sgmap.aps_ptpa)
183 panic("lca_dma_init: bad page table address");
184 REGVAL64(LCA_IOC_W_T_BASE0) = lcp->lc_sgmap.aps_ptpa;
185 alpha_mb();
186
187 /* Enble the scatter/gather TLB. */
188 REGVAL64(LCA_IOC_TB_ENA) = IOC_TB_ENA_TEN;
189 alpha_mb();
190
191 LCA_TLB_INVALIDATE();
192
193 /* XXX XXX BEGIN XXX XXX */
194 { /* XXX */
195 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
196 alpha_XXX_dmamap_or = LCA_DIRECT_MAPPED_BASE; /* XXX */
197 } /* XXX */
198 /* XXX XXX END XXX XXX */
199 }
200
201 /*
202 * Return the bus dma tag to be used for the specified bus type.
203 * INTERNAL USE ONLY!
204 */
205 bus_dma_tag_t
206 lca_dma_get_tag(t, bustype)
207 bus_dma_tag_t t;
208 alpha_bus_t bustype;
209 {
210 struct lca_config *lcp = t->_cookie;
211
212 switch (bustype) {
213 case ALPHA_BUS_PCI:
214 case ALPHA_BUS_EISA:
215 /*
216 * Systems with an LCA can only support 1G
217 * of memory, so we use the direct-mapped window
218 * on busses that have 32-bit DMA.
219 */
220 return (&lcp->lc_dmat_direct);
221
222 case ALPHA_BUS_ISA:
223 /*
224 * ISA doesn't have enough address bits to use
225 * the direct-mapped DMA window, so we must use
226 * SGMAPs.
227 */
228 return (&lcp->lc_dmat_sgmap);
229
230 default:
231 panic("lca_dma_get_tag: shouldn't be here, really...");
232 }
233 }
234
235 /*
236 * Load an LCA SGMAP-mapped DMA map with a linear buffer.
237 */
238 int
239 lca_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
240 bus_dma_tag_t t;
241 bus_dmamap_t map;
242 void *buf;
243 bus_size_t buflen;
244 struct proc *p;
245 int flags;
246 {
247 int error;
248
249 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
250 t->_sgmap);
251 if (error == 0)
252 LCA_TLB_INVALIDATE();
253
254 return (error);
255 }
256
257 /*
258 * Load an LCA SGMAP-mapped DMA map with an mbuf chain.
259 */
260 int
261 lca_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
262 bus_dma_tag_t t;
263 bus_dmamap_t map;
264 struct mbuf *m;
265 int flags;
266 {
267 int error;
268
269 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
270 if (error == 0)
271 LCA_TLB_INVALIDATE();
272
273 return (error);
274 }
275
276 /*
277 * Load an LCA SGMAP-mapped DMA map with a uio.
278 */
279 int
280 lca_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
281 bus_dma_tag_t t;
282 bus_dmamap_t map;
283 struct uio *uio;
284 int flags;
285 {
286 int error;
287
288 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
289 if (error == 0)
290 LCA_TLB_INVALIDATE();
291
292 return (error);
293 }
294
295 /*
296 * Load an LCA SGMAP-mapped DMA map with raw memory.
297 */
298 int
299 lca_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
300 bus_dma_tag_t t;
301 bus_dmamap_t map;
302 bus_dma_segment_t *segs;
303 int nsegs;
304 bus_size_t size;
305 int flags;
306 {
307 int error;
308
309 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
310 t->_sgmap);
311 if (error == 0)
312 LCA_TLB_INVALIDATE();
313
314 return (error);
315 }
316
317 /*
318 * Unload an LCA DMA map.
319 */
320 void
321 lca_bus_dmamap_unload_sgmap(t, map)
322 bus_dma_tag_t t;
323 bus_dmamap_t map;
324 {
325
326 /*
327 * Invalidate any SGMAP page table entries used by this
328 * mapping.
329 */
330 pci_sgmap_pte64_unload(t, map, t->_sgmap);
331 LCA_TLB_INVALIDATE();
332
333 /*
334 * Do the generic bits of the unload.
335 */
336 _bus_dmamap_unload(t, map);
337 }
338