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