ttwoga_dma.c revision 1.3 1 1.3 thorpej /* $NetBSD: ttwoga_dma.c,v 1.3 2001/07/19 18:50:25 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
40 1.1 thorpej
41 1.3 thorpej __KERNEL_RCSID(0, "$NetBSD: ttwoga_dma.c,v 1.3 2001/07/19 18:50:25 thorpej Exp $");
42 1.1 thorpej
43 1.1 thorpej #include <sys/param.h>
44 1.1 thorpej #include <sys/systm.h>
45 1.1 thorpej #include <sys/kernel.h>
46 1.1 thorpej #include <sys/device.h>
47 1.1 thorpej
48 1.1 thorpej #include <uvm/uvm_extern.h>
49 1.1 thorpej
50 1.1 thorpej #define _ALPHA_BUS_DMA_PRIVATE
51 1.1 thorpej #include <machine/bus.h>
52 1.1 thorpej
53 1.1 thorpej #include <dev/pci/pcireg.h>
54 1.1 thorpej #include <dev/pci/pcivar.h>
55 1.1 thorpej
56 1.1 thorpej #include <alpha/pci/ttwogareg.h>
57 1.1 thorpej #include <alpha/pci/ttwogavar.h>
58 1.1 thorpej
59 1.1 thorpej bus_dma_tag_t ttwoga_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
60 1.1 thorpej
61 1.1 thorpej int ttwoga_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
62 1.1 thorpej bus_size_t, struct proc *, int);
63 1.1 thorpej
64 1.1 thorpej int ttwoga_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
65 1.1 thorpej struct mbuf *, int);
66 1.1 thorpej
67 1.1 thorpej int ttwoga_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
68 1.1 thorpej struct uio *, int);
69 1.1 thorpej
70 1.1 thorpej int ttwoga_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
71 1.1 thorpej bus_dma_segment_t *, int, bus_size_t, int);
72 1.1 thorpej
73 1.1 thorpej void ttwoga_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
74 1.1 thorpej
75 1.1 thorpej /*
76 1.1 thorpej * Direct-mapped window: 1G at 1G
77 1.1 thorpej */
78 1.1 thorpej #define TTWOGA_DIRECT_MAPPED_BASE (1UL*1024UL*1024UL*1024UL)
79 1.1 thorpej #define TTWOGA_DIRECT_MAPPED_SIZE (1UL*1024UL*1024UL*1024UL)
80 1.1 thorpej
81 1.1 thorpej /*
82 1.1 thorpej * SGMAP window: 8M at 8M
83 1.1 thorpej */
84 1.1 thorpej #define TTWOGA_SGMAP_MAPPED_BASE (8UL*1024UL*1024UL)
85 1.1 thorpej #define TTWOGA_SGMAP_MAPPED_SIZE (8UL*1024UL*1024UL)
86 1.1 thorpej
87 1.3 thorpej /* T2 has a 256-byte out-bound DMA prefetch threshold. */
88 1.3 thorpej #define TTWOGA_SGMAP_PFTHRESH 256
89 1.3 thorpej
90 1.1 thorpej /*
91 1.1 thorpej * Macro to flush the T2 Gate Array scatter/gather TLB.
92 1.1 thorpej */
93 1.1 thorpej #define TTWOGA_TLB_INVALIDATE(tcp) \
94 1.1 thorpej do { \
95 1.1 thorpej u_int64_t temp; \
96 1.1 thorpej \
97 1.1 thorpej alpha_mb(); \
98 1.1 thorpej temp = T2GA((tcp), T2_IOCSR); \
99 1.1 thorpej T2GA((tcp), T2_IOCSR) = temp | IOCSR_FTLB; \
100 1.1 thorpej alpha_mb(); \
101 1.1 thorpej alpha_mb(); /* MAGIC */ \
102 1.1 thorpej T2GA((tcp), T2_IOCSR) = temp; \
103 1.1 thorpej alpha_mb(); \
104 1.1 thorpej alpha_mb(); /* MAGIC */ \
105 1.1 thorpej } while (0)
106 1.1 thorpej
107 1.1 thorpej void
108 1.1 thorpej ttwoga_dma_init(struct ttwoga_config *tcp)
109 1.1 thorpej {
110 1.1 thorpej bus_dma_tag_t t;
111 1.1 thorpej
112 1.1 thorpej /*
113 1.1 thorpej * Initialize the DMA tag used for direct-mapped DMA.
114 1.1 thorpej */
115 1.1 thorpej t = &tcp->tc_dmat_direct;
116 1.1 thorpej t->_cookie = tcp;
117 1.1 thorpej t->_wbase = TTWOGA_DIRECT_MAPPED_BASE;
118 1.1 thorpej t->_wsize = TTWOGA_DIRECT_MAPPED_SIZE;
119 1.1 thorpej t->_next_window = NULL;
120 1.1 thorpej t->_boundary = 0;
121 1.1 thorpej t->_sgmap = NULL;
122 1.1 thorpej t->_get_tag = ttwoga_dma_get_tag;
123 1.1 thorpej t->_dmamap_create = _bus_dmamap_create;
124 1.1 thorpej t->_dmamap_destroy = _bus_dmamap_destroy;
125 1.1 thorpej t->_dmamap_load = _bus_dmamap_load_direct;
126 1.1 thorpej t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
127 1.1 thorpej t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
128 1.1 thorpej t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
129 1.1 thorpej t->_dmamap_unload = _bus_dmamap_unload;
130 1.1 thorpej t->_dmamap_sync = _bus_dmamap_sync;
131 1.1 thorpej
132 1.1 thorpej t->_dmamem_alloc = _bus_dmamem_alloc;
133 1.1 thorpej t->_dmamem_free = _bus_dmamem_free;
134 1.1 thorpej t->_dmamem_map = _bus_dmamem_map;
135 1.1 thorpej t->_dmamem_unmap = _bus_dmamem_unmap;
136 1.1 thorpej t->_dmamem_mmap = _bus_dmamem_mmap;
137 1.1 thorpej
138 1.1 thorpej /*
139 1.1 thorpej * Initialize the DMA tag used for sgmap-mapped DMA.
140 1.1 thorpej */
141 1.1 thorpej t = &tcp->tc_dmat_sgmap;
142 1.1 thorpej t->_cookie = tcp;
143 1.1 thorpej t->_wbase = TTWOGA_SGMAP_MAPPED_BASE;
144 1.1 thorpej t->_wsize = TTWOGA_SGMAP_MAPPED_SIZE;
145 1.1 thorpej t->_next_window = NULL;
146 1.1 thorpej t->_boundary = 0;
147 1.1 thorpej t->_sgmap = &tcp->tc_sgmap;
148 1.3 thorpej t->_pfthresh = TTWOGA_SGMAP_PFTHRESH;
149 1.1 thorpej t->_get_tag = ttwoga_dma_get_tag;
150 1.2 thorpej t->_dmamap_create = alpha_sgmap_dmamap_create;
151 1.2 thorpej t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
152 1.1 thorpej t->_dmamap_load = ttwoga_bus_dmamap_load_sgmap;
153 1.1 thorpej t->_dmamap_load_mbuf = ttwoga_bus_dmamap_load_mbuf_sgmap;
154 1.1 thorpej t->_dmamap_load_uio = ttwoga_bus_dmamap_load_uio_sgmap;
155 1.1 thorpej t->_dmamap_load_raw = ttwoga_bus_dmamap_load_raw_sgmap;
156 1.1 thorpej t->_dmamap_unload = ttwoga_bus_dmamap_unload_sgmap;
157 1.1 thorpej t->_dmamap_sync = _bus_dmamap_sync;
158 1.1 thorpej
159 1.1 thorpej t->_dmamem_alloc = _bus_dmamem_alloc;
160 1.1 thorpej t->_dmamem_free = _bus_dmamem_free;
161 1.1 thorpej t->_dmamem_map = _bus_dmamem_map;
162 1.1 thorpej t->_dmamem_unmap = _bus_dmamem_unmap;
163 1.1 thorpej t->_dmamem_mmap = _bus_dmamem_mmap;
164 1.1 thorpej
165 1.1 thorpej /*
166 1.1 thorpej * Disable the SGMAP TLB, and flush it. We reenable it if
167 1.1 thorpej * we have a Sable or a Gamma with T3 or T4; Gamma with T2
168 1.1 thorpej * has a TLB bug apparently severe enough to require disabling
169 1.1 thorpej * it.
170 1.1 thorpej */
171 1.1 thorpej alpha_mb();
172 1.1 thorpej T2GA(tcp, T2_IOCSR) &= ~IOCSR_ETLB;
173 1.1 thorpej alpha_mb();
174 1.1 thorpej alpha_mb(); /* MAGIC */
175 1.1 thorpej
176 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp);
177 1.1 thorpej
178 1.1 thorpej /*
179 1.1 thorpej * XXX We might want to make sure our DMA windows don't
180 1.1 thorpej * XXX overlap with PCI memory space!
181 1.1 thorpej */
182 1.1 thorpej
183 1.1 thorpej /*
184 1.1 thorpej * Set up window 1 as a 1G direct-mapped window
185 1.1 thorpej * starting at 1G.
186 1.1 thorpej */
187 1.1 thorpej T2GA(tcp, T2_WBASE1) = 0;
188 1.1 thorpej alpha_mb();
189 1.1 thorpej
190 1.1 thorpej T2GA(tcp, T2_WMASK1) = (TTWOGA_DIRECT_MAPPED_SIZE - 1) & WMASKx_PWM;
191 1.1 thorpej alpha_mb();
192 1.1 thorpej
193 1.1 thorpej T2GA(tcp, T2_TBASE1) = 0;
194 1.1 thorpej alpha_mb();
195 1.1 thorpej
196 1.1 thorpej T2GA(tcp, T2_WBASE1) = TTWOGA_DIRECT_MAPPED_BASE |
197 1.1 thorpej ((TTWOGA_DIRECT_MAPPED_BASE + (TTWOGA_DIRECT_MAPPED_SIZE - 1)) >>
198 1.1 thorpej WBASEx_PWxA_SHIFT) | WBASEx_PWE;
199 1.1 thorpej alpha_mb();
200 1.1 thorpej
201 1.1 thorpej /*
202 1.1 thorpej * Initialize the SGMAP.
203 1.1 thorpej */
204 1.1 thorpej alpha_sgmap_init(t, &tcp->tc_sgmap, "ttwoga_sgmap",
205 1.1 thorpej TTWOGA_SGMAP_MAPPED_BASE, 0, TTWOGA_SGMAP_MAPPED_SIZE,
206 1.1 thorpej sizeof(u_int64_t), NULL, 0);
207 1.1 thorpej
208 1.1 thorpej /*
209 1.1 thorpej * Set up window 2 as an 8MB SGMAP-mapped window
210 1.1 thorpej * starting at 8MB.
211 1.1 thorpej */
212 1.1 thorpej #ifdef DIAGNOSTIC
213 1.1 thorpej if ((TTWOGA_SGMAP_MAPPED_BASE & WBASEx_PWSA) !=
214 1.1 thorpej TTWOGA_SGMAP_MAPPED_BASE)
215 1.1 thorpej panic("ttwoga_dma_init: SGMAP base inconsistency");
216 1.1 thorpej #endif
217 1.1 thorpej T2GA(tcp, T2_WBASE2) = 0;
218 1.1 thorpej alpha_mb();
219 1.1 thorpej
220 1.1 thorpej T2GA(tcp, T2_WMASK2) = (TTWOGA_SGMAP_MAPPED_SIZE - 1) & WMASKx_PWM;
221 1.1 thorpej alpha_mb();
222 1.1 thorpej
223 1.1 thorpej T2GA(tcp, T2_TBASE2) = tcp->tc_sgmap.aps_ptpa >> 1;
224 1.1 thorpej alpha_mb();
225 1.1 thorpej
226 1.1 thorpej T2GA(tcp, T2_WBASE2) = TTWOGA_SGMAP_MAPPED_BASE |
227 1.1 thorpej ((TTWOGA_SGMAP_MAPPED_BASE + (TTWOGA_SGMAP_MAPPED_SIZE - 1)) >>
228 1.1 thorpej WBASEx_PWxA_SHIFT) | WBASEx_SGE | WBASEx_PWE;
229 1.1 thorpej alpha_mb();
230 1.1 thorpej
231 1.1 thorpej /*
232 1.1 thorpej * Enable SGMAP TLB on Sable or Gamma with T3 or T4; see above.
233 1.1 thorpej */
234 1.1 thorpej if (alpha_implver() == ALPHA_IMPLVER_EV4 ||
235 1.1 thorpej tcp->tc_rev >= TRN_T3) {
236 1.1 thorpej alpha_mb();
237 1.1 thorpej T2GA(tcp, T2_IOCSR) |= IOCSR_ETLB;
238 1.1 thorpej alpha_mb();
239 1.1 thorpej alpha_mb(); /* MAGIC */
240 1.1 thorpej tcp->tc_use_tlb = 1;
241 1.1 thorpej }
242 1.1 thorpej
243 1.1 thorpej /* XXX XXX BEGIN XXX XXX */
244 1.1 thorpej { /* XXX */
245 1.1 thorpej extern paddr_t alpha_XXX_dmamap_or; /* XXX */
246 1.1 thorpej alpha_XXX_dmamap_or = TTWOGA_DIRECT_MAPPED_BASE;/* XXX */
247 1.1 thorpej } /* XXX */
248 1.1 thorpej /* XXX XXX END XXX XXX */
249 1.1 thorpej }
250 1.1 thorpej
251 1.1 thorpej /*
252 1.1 thorpej * Return the bus dma tag to be used for the specified bus type.
253 1.1 thorpej * INTERNAL USE ONLY!
254 1.1 thorpej */
255 1.1 thorpej bus_dma_tag_t
256 1.1 thorpej ttwoga_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
257 1.1 thorpej {
258 1.1 thorpej struct ttwoga_config *tcp = t->_cookie;
259 1.1 thorpej
260 1.1 thorpej switch (bustype) {
261 1.1 thorpej case ALPHA_BUS_PCI:
262 1.1 thorpej case ALPHA_BUS_EISA:
263 1.1 thorpej /*
264 1.1 thorpej * Systems with a T2 Gate Array can have 2G
265 1.1 thorpej * of memory, but we only get a direct-mapped
266 1.1 thorpej * window of 1G!
267 1.1 thorpej *
268 1.1 thorpej * XXX FIX THIS SOMEDAY!
269 1.1 thorpej */
270 1.1 thorpej return (&tcp->tc_dmat_direct);
271 1.1 thorpej
272 1.1 thorpej case ALPHA_BUS_ISA:
273 1.1 thorpej /*
274 1.1 thorpej * ISA doesn't have enough address bits to use
275 1.1 thorpej * the direct-mapped DMA window, so we must use
276 1.1 thorpej * SGMAPs.
277 1.1 thorpej */
278 1.1 thorpej return (&tcp->tc_dmat_sgmap);
279 1.1 thorpej
280 1.1 thorpej default:
281 1.1 thorpej panic("ttwoga_dma_get_tag: shouldn't be here, really...");
282 1.1 thorpej }
283 1.1 thorpej }
284 1.1 thorpej
285 1.1 thorpej /*
286 1.1 thorpej * Load a T2 SGMAP-mapped DMA map with a liner buffer.
287 1.1 thorpej */
288 1.1 thorpej int
289 1.1 thorpej ttwoga_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
290 1.1 thorpej bus_size_t buflen, struct proc *p, int flags)
291 1.1 thorpej {
292 1.1 thorpej struct ttwoga_config *tcp = t->_cookie;
293 1.1 thorpej int error;
294 1.1 thorpej
295 1.1 thorpej error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
296 1.1 thorpej t->_sgmap);
297 1.1 thorpej if (error == 0 && tcp->tc_use_tlb)
298 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp);
299 1.1 thorpej
300 1.1 thorpej return (error);
301 1.1 thorpej }
302 1.1 thorpej
303 1.1 thorpej /*
304 1.1 thorpej * Load a T2 SGMAP-mapped DMA map with an mbuf chain.
305 1.1 thorpej */
306 1.1 thorpej int
307 1.1 thorpej ttwoga_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
308 1.1 thorpej struct mbuf *m, int flags)
309 1.1 thorpej {
310 1.1 thorpej struct ttwoga_config *tcp = t->_cookie;
311 1.1 thorpej int error;
312 1.1 thorpej
313 1.1 thorpej error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
314 1.1 thorpej if (error == 0 && tcp->tc_use_tlb)
315 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp);
316 1.1 thorpej
317 1.1 thorpej return (error);
318 1.1 thorpej }
319 1.1 thorpej
320 1.1 thorpej /*
321 1.1 thorpej * Load a T2 SGMAP-mapped DMA map with a uio.
322 1.1 thorpej */
323 1.1 thorpej int
324 1.1 thorpej ttwoga_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
325 1.1 thorpej struct uio *uio, int flags)
326 1.1 thorpej {
327 1.1 thorpej struct ttwoga_config *tcp = t->_cookie;
328 1.1 thorpej int error;
329 1.1 thorpej
330 1.1 thorpej error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
331 1.1 thorpej if (error == 0 && tcp->tc_use_tlb)
332 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp);
333 1.1 thorpej
334 1.1 thorpej return (error);
335 1.1 thorpej }
336 1.1 thorpej
337 1.1 thorpej /*
338 1.1 thorpej * Load a T2 SGMAP-mapped DMA map with raw memory.
339 1.1 thorpej */
340 1.1 thorpej int
341 1.1 thorpej ttwoga_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
342 1.1 thorpej bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
343 1.1 thorpej {
344 1.1 thorpej struct ttwoga_config *tcp = t->_cookie;
345 1.1 thorpej int error;
346 1.1 thorpej
347 1.1 thorpej error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
348 1.1 thorpej t->_sgmap);
349 1.1 thorpej if (error == 0 && tcp->tc_use_tlb)
350 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp);
351 1.1 thorpej
352 1.1 thorpej return (error);
353 1.1 thorpej }
354 1.1 thorpej
355 1.1 thorpej /*
356 1.1 thorpej * Unload an T2 DMA map.
357 1.1 thorpej */
358 1.1 thorpej void
359 1.1 thorpej ttwoga_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
360 1.1 thorpej {
361 1.1 thorpej struct ttwoga_config *tcp = t->_cookie;
362 1.1 thorpej
363 1.1 thorpej /*
364 1.1 thorpej * Invalidate any SGMAP page table entries used by this
365 1.1 thorpej * mapping.
366 1.1 thorpej */
367 1.1 thorpej pci_sgmap_pte64_unload(t, map, t->_sgmap);
368 1.1 thorpej if (tcp->tc_use_tlb)
369 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp);
370 1.1 thorpej
371 1.1 thorpej /*
372 1.1 thorpej * Do the generic bits of the unload.
373 1.1 thorpej */
374 1.1 thorpej _bus_dmamap_unload(t, map);
375 1.1 thorpej }
376