tsp_dma.c revision 1.1 1 /* $NetBSD: tsp_dma.c,v 1.1 1999/06/29 06:46:47 ross Exp $ */
2
3 /*-
4 * Copyright (c) 1999 by Ross Harvey. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Ross Harvey.
17 * 4. The name of Ross Harvey may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
21 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
23 * ARE DISCLAIMED. IN NO EVENT SHALL ROSS HARVEY BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 */
33
34 /*-
35 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
36 * All rights reserved.
37 *
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
40 * NASA Ames Research Center.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the NetBSD
53 * Foundation, Inc. and its contributors.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/device.h>
75 #include <sys/malloc.h>
76 #include <vm/vm.h>
77
78 #include <machine/autoconf.h>
79 #define _ALPHA_BUS_DMA_PRIVATE
80 #include <machine/bus.h>
81 #include <machine/rpb.h>
82
83 #include <dev/pci/pcireg.h>
84 #include <dev/pci/pcivar.h>
85 #include <alpha/pci/tsreg.h>
86 #include <alpha/pci/tsvar.h>
87
88 #define tsp_dma() { Generate ctags(1) key. }
89
90 #define EDIFF(a, b) (((a) | WSBA_ENA | WSBA_SG) != ((b) | WSBA_ENA | WSBA_SG))
91
92 bus_dma_tag_t tsp_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
93
94 int tsp_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
95 bus_size_t, bus_size_t, int, bus_dmamap_t *));
96
97 void tsp_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
98
99 int tsp_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
100 bus_size_t, struct proc *, int));
101
102 int tsp_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
103 struct mbuf *, int));
104
105 int tsp_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
106 struct uio *, int));
107
108 int tsp_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
109 bus_dma_segment_t *, int, bus_size_t, int));
110
111 void tsp_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
112
113 void tsp_tlb_invalidate __P((struct tsp_config *));
114
115 void
116 tsp_dma_init(pcp)
117 struct tsp_config *pcp;
118 {
119 int i;
120 bus_dma_tag_t t;
121 struct ts_pchip *pccsr = pcp->pc_csr;
122 bus_addr_t dwbase, dwlen, sgwbase, sgwlen, tbase;
123 static struct map_expected {
124 u_int32_t base, mask, enables;
125 } premap[4] = {
126 { 0x800000, 0x700000, WSBA_ENA | WSBA_SG },
127 { 0x80000000 | WSBA_ENA, 0x3ff00000, WSBA_ENA },
128 { 0, 0 },
129 { 0, 0 }
130 };
131
132 alpha_mb();
133 for(i = 0; i < 4; ++i) {
134 if (EDIFF(pccsr->tsp_wsba[i].tsg_r, premap[i].base) ||
135 EDIFF(pccsr->tsp_wsm[i].tsg_r, premap[i].mask))
136 printf("tsp%d: window %d: %lx/base %lx/mask %lx"
137 " reinitialized\n",
138 pcp->pc_pslot, i,
139 pccsr->tsp_wsba[i].tsg_r,
140 pccsr->tsp_wsm[i].tsg_r,
141 pccsr->tsp_tba[i].tsg_r);
142 pccsr->tsp_wsba[i].tsg_r = premap[i].base | premap[i].enables;
143 pccsr->tsp_wsm[i].tsg_r = premap[i].mask;
144 }
145 alpha_mb();
146
147 /*
148 * Initialize the DMA tag used for direct-mapped DMA.
149 */
150 t = &pcp->pc_dmat_direct;
151 t->_cookie = pcp;
152 t->_wbase = dwbase = WSBA_ADDR(pccsr->tsp_wsba[1].tsg_r);
153 t->_wsize = dwlen = WSM_LEN(pccsr->tsp_wsm[1].tsg_r);
154 t->_next_window = &pcp->pc_dmat_sgmap;
155 t->_boundary = 0;
156 t->_sgmap = NULL;
157 t->_get_tag = tsp_dma_get_tag;
158 t->_dmamap_create = _bus_dmamap_create;
159 t->_dmamap_destroy = _bus_dmamap_destroy;
160 t->_dmamap_load = _bus_dmamap_load_direct;
161 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
162 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
163 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
164 t->_dmamap_unload = _bus_dmamap_unload;
165 t->_dmamap_sync = _bus_dmamap_sync;
166
167 t->_dmamem_alloc = _bus_dmamem_alloc;
168 t->_dmamem_free = _bus_dmamem_free;
169 t->_dmamem_map = _bus_dmamem_map;
170 t->_dmamem_unmap = _bus_dmamem_unmap;
171 t->_dmamem_mmap = _bus_dmamem_mmap;
172
173 /*
174 * Initialize the DMA tag used for sgmap-mapped DMA.
175 */
176 t = &pcp->pc_dmat_sgmap;
177 t->_cookie = pcp;
178 t->_wbase = sgwbase = WSBA_ADDR(pccsr->tsp_wsba[0].tsg_r);
179 t->_wsize = sgwlen = WSM_LEN(pccsr->tsp_wsm[0].tsg_r);
180 t->_next_window = NULL;
181 t->_boundary = 0;
182 t->_sgmap = &pcp->pc_sgmap;
183 t->_get_tag = tsp_dma_get_tag;
184 t->_dmamap_create = tsp_bus_dmamap_create_sgmap;
185 t->_dmamap_destroy = tsp_bus_dmamap_destroy_sgmap;
186 t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
187 t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
188 t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
189 t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
190 t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
191 t->_dmamap_sync = _bus_dmamap_sync;
192
193 t->_dmamem_alloc = _bus_dmamem_alloc;
194 t->_dmamem_free = _bus_dmamem_free;
195 t->_dmamem_map = _bus_dmamem_map;
196 t->_dmamem_unmap = _bus_dmamem_unmap;
197 t->_dmamem_mmap = _bus_dmamem_mmap;
198
199 /*
200 * Initialize the SGMAP. Align page table to 32k in case
201 * window is somewhat larger than expected.
202 */
203 alpha_sgmap_init(t, &pcp->pc_sgmap, "tsp_sgmap",
204 sgwbase, 0, sgwlen, sizeof(u_int64_t), NULL, (32*1024));
205
206 /*
207 * Enable window 0 and enable SG PTE mapping.
208 */
209 alpha_mb();
210 pccsr->tsp_wsba[0].tsg_r |= WSBA_SG | WSBA_ENA;
211 alpha_mb();
212
213 /*
214 * Check windows for sanity, especially if we later decide to
215 * use the firmware's initialization in some cases.
216 */
217 if ((sgwbase <= dwbase && dwbase < sgwbase + sgwlen) ||
218 (dwbase <= sgwbase && sgwbase < dwbase + dwlen))
219 panic("tsp_dma_init: overlap");
220
221 tbase = pcp->pc_sgmap.aps_ptpa;
222 if (tbase & ~0x7fffffc00UL)
223 panic("tsp_dma_init: bad page table address");
224 alpha_mb();
225 pccsr->tsp_tba[0].tsg_r = tbase;
226 alpha_mb();
227
228 tsp_tlb_invalidate(pcp);
229 alpha_mb();
230
231 /* XXX XXX BEGIN XXX XXX */
232 { /* XXX */
233 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
234 alpha_XXX_dmamap_or = dwbase; /* XXX */
235 } /* XXX */
236 /* XXX XXX END XXX XXX */
237 }
238
239 /*
240 * Return the bus dma tag to be used for the specified bus type.
241 * INTERNAL USE ONLY!
242 */
243 bus_dma_tag_t
244 tsp_dma_get_tag(t, bustype)
245 bus_dma_tag_t t;
246 alpha_bus_t bustype;
247 {
248 struct tsp_config *pcp = t->_cookie;
249
250 switch (bustype) {
251 case ALPHA_BUS_PCI:
252 case ALPHA_BUS_EISA:
253 /*
254 * The direct mapped window will work for most systems,
255 * most of the time. When it doesn't, we chain to the sgmap
256 * window automatically.
257 */
258 return (&pcp->pc_dmat_direct);
259
260 case ALPHA_BUS_ISA:
261 /*
262 * ISA doesn't have enough address bits to use
263 * the direct-mapped DMA window, so we must use
264 * SGMAPs.
265 */
266 return (&pcp->pc_dmat_sgmap);
267
268 default:
269 panic("tsp_dma_get_tag: shouldn't be here, really...");
270 }
271 }
272
273 /*
274 * Create a TSP SGMAP-mapped DMA map.
275 */
276 int
277 tsp_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
278 flags, dmamp)
279 bus_dma_tag_t t;
280 bus_size_t size;
281 int nsegments;
282 bus_size_t maxsegsz;
283 bus_size_t boundary;
284 int flags;
285 bus_dmamap_t *dmamp;
286 {
287 bus_dmamap_t map;
288 int error;
289
290 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
291 boundary, flags, dmamp);
292 if (error)
293 return (error);
294
295 map = *dmamp;
296
297 if (flags & BUS_DMA_ALLOCNOW) {
298 error = alpha_sgmap_alloc(map, round_page(size),
299 t->_sgmap, flags);
300 if (error)
301 tsp_bus_dmamap_destroy_sgmap(t, map);
302 }
303
304 return (error);
305 }
306
307 /*
308 * Destroy a TSP SGMAP-mapped DMA map.
309 */
310 void
311 tsp_bus_dmamap_destroy_sgmap(t, map)
312 bus_dma_tag_t t;
313 bus_dmamap_t map;
314 {
315
316 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
317 alpha_sgmap_free(map, t->_sgmap);
318
319 _bus_dmamap_destroy(t, map);
320 }
321
322 /*
323 * Load a TSP SGMAP-mapped DMA map with a linear buffer.
324 */
325 int
326 tsp_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
327 bus_dma_tag_t t;
328 bus_dmamap_t map;
329 void *buf;
330 bus_size_t buflen;
331 struct proc *p;
332 int flags;
333 {
334 int error;
335
336 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
337 t->_sgmap);
338 if (error == 0)
339 tsp_tlb_invalidate(t->_cookie);
340
341 return (error);
342 }
343
344 /*
345 * Load a TSP SGMAP-mapped DMA map with an mbuf chain.
346 */
347 int
348 tsp_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
349 bus_dma_tag_t t;
350 bus_dmamap_t map;
351 struct mbuf *m;
352 int flags;
353 {
354 int error;
355
356 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
357 if (error == 0)
358 tsp_tlb_invalidate(t->_cookie);
359
360 return (error);
361 }
362
363 /*
364 * Load a TSP SGMAP-mapped DMA map with a uio.
365 */
366 int
367 tsp_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
368 bus_dma_tag_t t;
369 bus_dmamap_t map;
370 struct uio *uio;
371 int flags;
372 {
373 int error;
374
375 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
376 if (error == 0)
377 tsp_tlb_invalidate(t->_cookie);
378
379 return (error);
380 }
381
382 /*
383 * Load a TSP SGMAP-mapped DMA map with raw memory.
384 */
385 int
386 tsp_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
387 bus_dma_tag_t t;
388 bus_dmamap_t map;
389 bus_dma_segment_t *segs;
390 int nsegs;
391 bus_size_t size;
392 int flags;
393 {
394 int error;
395
396 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
397 t->_sgmap);
398 if (error == 0)
399 tsp_tlb_invalidate(t->_cookie);
400
401 return (error);
402 }
403
404 /*
405 * Unload a TSP DMA map.
406 */
407 void
408 tsp_bus_dmamap_unload_sgmap(t, map)
409 bus_dma_tag_t t;
410 bus_dmamap_t map;
411 {
412
413 /*
414 * Invalidate any SGMAP page table entries used by this
415 * mapping.
416 */
417 pci_sgmap_pte64_unload(t, map, t->_sgmap);
418 tsp_tlb_invalidate(t->_cookie);
419
420 /*
421 * Do the generic bits of the unload.
422 */
423 _bus_dmamap_unload(t, map);
424 }
425
426 /*
427 * Flush the TSP scatter/gather TLB.
428 */
429 void
430 tsp_tlb_invalidate(pcp)
431 struct tsp_config *pcp;
432 {
433
434 alpha_mb();
435 pcp->pc_csr->tsp_tlbia.tsg_r = 0;
436 alpha_mb();
437 }
438