apecs_dma.c revision 1.15 1 /* $NetBSD: apecs_dma.c,v 1.15 2001/07/19 18:39:29 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: apecs_dma.c,v 1.15 2001/07/19 18:39:29 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/apecsreg.h>
58 #include <alpha/pci/apecsvar.h>
59
60 bus_dma_tag_t apecs_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
61
62 int apecs_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
63 bus_size_t, struct proc *, int));
64
65 int apecs_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
66 struct mbuf *, int));
67
68 int apecs_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
69 struct uio *, int));
70
71 int apecs_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 apecs_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
75
76 /*
77 * Direct-mapped window: 1G at 1G
78 */
79 #define APECS_DIRECT_MAPPED_BASE (1*1024*1024*1024)
80 #define APECS_DIRECT_MAPPED_SIZE (1*1024*1024*1024)
81
82 /*
83 * SGMAP window: 8M at 8M
84 */
85 #define APECS_SGMAP_MAPPED_BASE (8*1024*1024)
86 #define APECS_SGMAP_MAPPED_SIZE (8*1024*1024)
87
88 /* APECS has a 256-byte out-bound DMA prefetch threshold. */
89 #define APECS_SGMAP_PFTHRESH 256
90
91 /*
92 * Macro to flush APECS scatter/gather TLB.
93 */
94 #define APECS_TLB_INVALIDATE() \
95 do { \
96 alpha_mb(); \
97 REGVAL(EPIC_TBIA) = 0; \
98 alpha_mb(); \
99 } while (0)
100
101 void
102 apecs_dma_init(acp)
103 struct apecs_config *acp;
104 {
105 bus_addr_t tbase;
106 bus_dma_tag_t t;
107
108 /*
109 * Initialize the DMA tag used for direct-mapped DMA.
110 */
111 t = &acp->ac_dmat_direct;
112 t->_cookie = acp;
113 t->_wbase = APECS_DIRECT_MAPPED_BASE;
114 t->_wsize = APECS_DIRECT_MAPPED_SIZE;
115 t->_next_window = NULL;
116 t->_boundary = 0;
117 t->_sgmap = NULL;
118 t->_get_tag = apecs_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 = &acp->ac_dmat_sgmap;
138 t->_cookie = acp;
139 t->_wbase = APECS_SGMAP_MAPPED_BASE;
140 t->_wsize = APECS_SGMAP_MAPPED_SIZE;
141 t->_next_window = NULL;
142 t->_boundary = 0;
143 t->_sgmap = &acp->ac_sgmap;
144 t->_pfthresh = APECS_SGMAP_PFTHRESH;
145 t->_get_tag = apecs_dma_get_tag;
146 t->_dmamap_create = alpha_sgmap_dmamap_create;
147 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
148 t->_dmamap_load = apecs_bus_dmamap_load_sgmap;
149 t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_sgmap;
150 t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_sgmap;
151 t->_dmamap_load_raw = apecs_bus_dmamap_load_raw_sgmap;
152 t->_dmamap_unload = apecs_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 2 as a 1G direct-mapped DMA
163 * window beginning at 1G. We leave it alone. Disable
164 * window 1.
165 */
166 REGVAL(EPIC_PCI_BASE_1) = 0;
167 alpha_mb();
168
169 /*
170 * Initialize the SGMAP.
171 */
172 alpha_sgmap_init(t, &acp->ac_sgmap, "apecs_sgmap",
173 APECS_SGMAP_MAPPED_BASE, 0, APECS_SGMAP_MAPPED_SIZE,
174 sizeof(u_int64_t), NULL, 0);
175
176 /*
177 * Set up window 1 as an 8MB SGMAP-mapped window
178 * starting at 8MB.
179 */
180 REGVAL(EPIC_PCI_BASE_1) = APECS_SGMAP_MAPPED_BASE |
181 EPIC_PCI_BASE_SGEN | EPIC_PCI_BASE_WENB;
182 alpha_mb();
183
184 REGVAL(EPIC_PCI_MASK_1) = EPIC_PCI_MASK_8M;
185 alpha_mb();
186
187 tbase = acp->ac_sgmap.aps_ptpa >> EPIC_TBASE_SHIFT;
188 if ((tbase & EPIC_TBASE_T_BASE) != tbase)
189 panic("apecs_dma_init: bad page table address");
190 REGVAL(EPIC_TBASE_1) = tbase;
191 alpha_mb();
192
193 APECS_TLB_INVALIDATE();
194
195 /* XXX XXX BEGIN XXX XXX */
196 { /* XXX */
197 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
198 alpha_XXX_dmamap_or = APECS_DIRECT_MAPPED_BASE; /* XXX */
199 } /* XXX */
200 /* XXX XXX END XXX XXX */
201 }
202
203 /*
204 * Return the bus dma tag to be used for the specified bus type.
205 * INTERNAL USE ONLY!
206 */
207 bus_dma_tag_t
208 apecs_dma_get_tag(t, bustype)
209 bus_dma_tag_t t;
210 alpha_bus_t bustype;
211 {
212 struct apecs_config *acp = t->_cookie;
213
214 switch (bustype) {
215 case ALPHA_BUS_PCI:
216 case ALPHA_BUS_EISA:
217 /*
218 * Systems with an APECS can only support 1G
219 * of memory, so we use the direct-mapped window
220 * on busses that have 32-bit DMA.
221 */
222 return (&acp->ac_dmat_direct);
223
224 case ALPHA_BUS_ISA:
225 /*
226 * ISA doesn't have enough address bits to use
227 * the direct-mapped DMA window, so we must use
228 * SGMAPs.
229 */
230 return (&acp->ac_dmat_sgmap);
231
232 default:
233 panic("apecs_dma_get_tag: shouldn't be here, really...");
234 }
235 }
236
237 /*
238 * Load an APECS SGMAP-mapped DMA map with a linear buffer.
239 */
240 int
241 apecs_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
242 bus_dma_tag_t t;
243 bus_dmamap_t map;
244 void *buf;
245 bus_size_t buflen;
246 struct proc *p;
247 int flags;
248 {
249 int error;
250
251 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
252 t->_sgmap);
253 if (error == 0)
254 APECS_TLB_INVALIDATE();
255
256 return (error);
257 }
258
259 /*
260 * Load an APECS SGMAP-mapped DMA map with an mbuf chain.
261 */
262 int
263 apecs_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
264 bus_dma_tag_t t;
265 bus_dmamap_t map;
266 struct mbuf *m;
267 int flags;
268 {
269 int error;
270
271 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
272 if (error == 0)
273 APECS_TLB_INVALIDATE();
274
275 return (error);
276 }
277
278 /*
279 * Load an APECS SGMAP-mapped DMA map with a uio.
280 */
281 int
282 apecs_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
283 bus_dma_tag_t t;
284 bus_dmamap_t map;
285 struct uio *uio;
286 int flags;
287 {
288 int error;
289
290 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
291 if (error == 0)
292 APECS_TLB_INVALIDATE();
293
294 return (error);
295 }
296
297 /*
298 * Load an APECS SGMAP-mapped DMA map with raw memory.
299 */
300 int
301 apecs_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
302 bus_dma_tag_t t;
303 bus_dmamap_t map;
304 bus_dma_segment_t *segs;
305 int nsegs;
306 bus_size_t size;
307 int flags;
308 {
309 int error;
310
311 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
312 t->_sgmap);
313 if (error == 0)
314 APECS_TLB_INVALIDATE();
315
316 return (error);
317 }
318
319 /*
320 * Unload an APECS DMA map.
321 */
322 void
323 apecs_bus_dmamap_unload_sgmap(t, map)
324 bus_dma_tag_t t;
325 bus_dmamap_t map;
326 {
327
328 /*
329 * Invalidate any SGMAP page table entries used by this
330 * mapping.
331 */
332 pci_sgmap_pte64_unload(t, map, t->_sgmap);
333 APECS_TLB_INVALIDATE();
334
335 /*
336 * Do the generic bits of the unload.
337 */
338 _bus_dmamap_unload(t, map);
339 }
340