tsp_dma.c revision 1.13 1 1.13 tsutsui /* $NetBSD: tsp_dma.c,v 1.13 2013/09/23 16:41:57 tsutsui 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.13 tsutsui __KERNEL_RCSID(0, "$NetBSD: tsp_dma.c,v 1.13 2013/09/23 16:41:57 tsutsui 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.1 ross #include <machine/autoconf.h>
74 1.1 ross #define _ALPHA_BUS_DMA_PRIVATE
75 1.11 dyoung #include <sys/bus.h>
76 1.1 ross #include <machine/rpb.h>
77 1.1 ross
78 1.1 ross #include <dev/pci/pcireg.h>
79 1.1 ross #include <dev/pci/pcivar.h>
80 1.1 ross #include <alpha/pci/tsreg.h>
81 1.1 ross #include <alpha/pci/tsvar.h>
82 1.1 ross
83 1.1 ross #define tsp_dma() { Generate ctags(1) key. }
84 1.1 ross
85 1.1 ross #define EDIFF(a, b) (((a) | WSBA_ENA | WSBA_SG) != ((b) | WSBA_ENA | WSBA_SG))
86 1.1 ross
87 1.8 dsl bus_dma_tag_t tsp_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
88 1.1 ross
89 1.8 dsl int tsp_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
90 1.8 dsl bus_size_t, struct proc *, int);
91 1.1 ross
92 1.8 dsl int tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
93 1.8 dsl struct mbuf *, int);
94 1.1 ross
95 1.8 dsl int tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
96 1.8 dsl struct uio *, int);
97 1.1 ross
98 1.8 dsl int tsp_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
99 1.8 dsl bus_dma_segment_t *, int, bus_size_t, int);
100 1.1 ross
101 1.8 dsl void tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
102 1.1 ross
103 1.8 dsl void tsp_tlb_invalidate(struct tsp_config *);
104 1.1 ross
105 1.4 thorpej /*
106 1.4 thorpej * XXX Need to figure out what this is, if any. Initialize it to
107 1.4 thorpej * XXX something that should be safe.
108 1.4 thorpej */
109 1.4 thorpej #define TSP_SGMAP_PFTHRESH 256
110 1.4 thorpej
111 1.1 ross void
112 1.9 dsl tsp_dma_init(struct tsp_config *pcp)
113 1.1 ross {
114 1.1 ross int i;
115 1.1 ross bus_dma_tag_t t;
116 1.1 ross struct ts_pchip *pccsr = pcp->pc_csr;
117 1.1 ross bus_addr_t dwbase, dwlen, sgwbase, sgwlen, tbase;
118 1.1 ross static struct map_expected {
119 1.12 matt uint32_t base, mask, enables;
120 1.1 ross } premap[4] = {
121 1.13 tsutsui { 0x00800000, 0x00700000, WSBA_ENA | WSBA_SG },
122 1.13 tsutsui { 0x80000000, 0x3ff00000, WSBA_ENA },
123 1.13 tsutsui { 0, 0, 0 },
124 1.13 tsutsui { 0, 0, 0 }
125 1.1 ross };
126 1.1 ross
127 1.1 ross alpha_mb();
128 1.1 ross for(i = 0; i < 4; ++i) {
129 1.1 ross if (EDIFF(pccsr->tsp_wsba[i].tsg_r, premap[i].base) ||
130 1.1 ross EDIFF(pccsr->tsp_wsm[i].tsg_r, premap[i].mask))
131 1.1 ross printf("tsp%d: window %d: %lx/base %lx/mask %lx"
132 1.1 ross " reinitialized\n",
133 1.1 ross pcp->pc_pslot, i,
134 1.1 ross pccsr->tsp_wsba[i].tsg_r,
135 1.1 ross pccsr->tsp_wsm[i].tsg_r,
136 1.1 ross pccsr->tsp_tba[i].tsg_r);
137 1.1 ross pccsr->tsp_wsba[i].tsg_r = premap[i].base | premap[i].enables;
138 1.1 ross pccsr->tsp_wsm[i].tsg_r = premap[i].mask;
139 1.1 ross }
140 1.1 ross alpha_mb();
141 1.1 ross
142 1.1 ross /*
143 1.1 ross * Initialize the DMA tag used for direct-mapped DMA.
144 1.1 ross */
145 1.1 ross t = &pcp->pc_dmat_direct;
146 1.1 ross t->_cookie = pcp;
147 1.1 ross t->_wbase = dwbase = WSBA_ADDR(pccsr->tsp_wsba[1].tsg_r);
148 1.1 ross t->_wsize = dwlen = WSM_LEN(pccsr->tsp_wsm[1].tsg_r);
149 1.1 ross t->_next_window = &pcp->pc_dmat_sgmap;
150 1.1 ross t->_boundary = 0;
151 1.1 ross t->_sgmap = NULL;
152 1.1 ross t->_get_tag = tsp_dma_get_tag;
153 1.1 ross t->_dmamap_create = _bus_dmamap_create;
154 1.1 ross t->_dmamap_destroy = _bus_dmamap_destroy;
155 1.1 ross t->_dmamap_load = _bus_dmamap_load_direct;
156 1.1 ross t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
157 1.1 ross t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
158 1.1 ross t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
159 1.1 ross t->_dmamap_unload = _bus_dmamap_unload;
160 1.1 ross t->_dmamap_sync = _bus_dmamap_sync;
161 1.1 ross
162 1.1 ross t->_dmamem_alloc = _bus_dmamem_alloc;
163 1.1 ross t->_dmamem_free = _bus_dmamem_free;
164 1.1 ross t->_dmamem_map = _bus_dmamem_map;
165 1.1 ross t->_dmamem_unmap = _bus_dmamem_unmap;
166 1.1 ross t->_dmamem_mmap = _bus_dmamem_mmap;
167 1.1 ross
168 1.1 ross /*
169 1.1 ross * Initialize the DMA tag used for sgmap-mapped DMA.
170 1.1 ross */
171 1.1 ross t = &pcp->pc_dmat_sgmap;
172 1.1 ross t->_cookie = pcp;
173 1.1 ross t->_wbase = sgwbase = WSBA_ADDR(pccsr->tsp_wsba[0].tsg_r);
174 1.1 ross t->_wsize = sgwlen = WSM_LEN(pccsr->tsp_wsm[0].tsg_r);
175 1.1 ross t->_next_window = NULL;
176 1.1 ross t->_boundary = 0;
177 1.1 ross t->_sgmap = &pcp->pc_sgmap;
178 1.4 thorpej t->_pfthresh = TSP_SGMAP_PFTHRESH;
179 1.1 ross t->_get_tag = tsp_dma_get_tag;
180 1.3 thorpej t->_dmamap_create = alpha_sgmap_dmamap_create;
181 1.3 thorpej t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
182 1.1 ross t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
183 1.1 ross t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
184 1.1 ross t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
185 1.1 ross t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
186 1.1 ross t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
187 1.1 ross t->_dmamap_sync = _bus_dmamap_sync;
188 1.1 ross
189 1.1 ross t->_dmamem_alloc = _bus_dmamem_alloc;
190 1.1 ross t->_dmamem_free = _bus_dmamem_free;
191 1.1 ross t->_dmamem_map = _bus_dmamem_map;
192 1.1 ross t->_dmamem_unmap = _bus_dmamem_unmap;
193 1.1 ross t->_dmamem_mmap = _bus_dmamem_mmap;
194 1.1 ross
195 1.1 ross /*
196 1.1 ross * Initialize the SGMAP. Align page table to 32k in case
197 1.1 ross * window is somewhat larger than expected.
198 1.1 ross */
199 1.1 ross alpha_sgmap_init(t, &pcp->pc_sgmap, "tsp_sgmap",
200 1.12 matt sgwbase, 0, sgwlen, sizeof(uint64_t), NULL, (32*1024));
201 1.1 ross
202 1.1 ross /*
203 1.1 ross * Enable window 0 and enable SG PTE mapping.
204 1.1 ross */
205 1.1 ross alpha_mb();
206 1.1 ross pccsr->tsp_wsba[0].tsg_r |= WSBA_SG | WSBA_ENA;
207 1.1 ross alpha_mb();
208 1.1 ross
209 1.1 ross /*
210 1.13 tsutsui * Enable window 1 in direct mode.
211 1.13 tsutsui */
212 1.13 tsutsui alpha_mb();
213 1.13 tsutsui pccsr->tsp_wsba[1].tsg_r =
214 1.13 tsutsui (pccsr->tsp_wsba[1].tsg_r & ~WSBA_SG) | WSBA_ENA;
215 1.13 tsutsui alpha_mb();
216 1.13 tsutsui
217 1.13 tsutsui /*
218 1.1 ross * Check windows for sanity, especially if we later decide to
219 1.1 ross * use the firmware's initialization in some cases.
220 1.1 ross */
221 1.1 ross if ((sgwbase <= dwbase && dwbase < sgwbase + sgwlen) ||
222 1.1 ross (dwbase <= sgwbase && sgwbase < dwbase + dwlen))
223 1.1 ross panic("tsp_dma_init: overlap");
224 1.1 ross
225 1.1 ross tbase = pcp->pc_sgmap.aps_ptpa;
226 1.1 ross if (tbase & ~0x7fffffc00UL)
227 1.1 ross panic("tsp_dma_init: bad page table address");
228 1.1 ross alpha_mb();
229 1.1 ross pccsr->tsp_tba[0].tsg_r = tbase;
230 1.1 ross alpha_mb();
231 1.1 ross
232 1.1 ross tsp_tlb_invalidate(pcp);
233 1.1 ross alpha_mb();
234 1.1 ross
235 1.1 ross /* XXX XXX BEGIN XXX XXX */
236 1.1 ross { /* XXX */
237 1.1 ross extern paddr_t alpha_XXX_dmamap_or; /* XXX */
238 1.1 ross alpha_XXX_dmamap_or = dwbase; /* XXX */
239 1.1 ross } /* XXX */
240 1.1 ross /* XXX XXX END XXX XXX */
241 1.1 ross }
242 1.1 ross
243 1.1 ross /*
244 1.1 ross * Return the bus dma tag to be used for the specified bus type.
245 1.1 ross * INTERNAL USE ONLY!
246 1.1 ross */
247 1.1 ross bus_dma_tag_t
248 1.9 dsl tsp_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
249 1.1 ross {
250 1.1 ross struct tsp_config *pcp = t->_cookie;
251 1.1 ross
252 1.1 ross switch (bustype) {
253 1.1 ross case ALPHA_BUS_PCI:
254 1.1 ross case ALPHA_BUS_EISA:
255 1.1 ross /*
256 1.1 ross * The direct mapped window will work for most systems,
257 1.1 ross * most of the time. When it doesn't, we chain to the sgmap
258 1.1 ross * window automatically.
259 1.1 ross */
260 1.1 ross return (&pcp->pc_dmat_direct);
261 1.1 ross
262 1.1 ross case ALPHA_BUS_ISA:
263 1.1 ross /*
264 1.1 ross * ISA doesn't have enough address bits to use
265 1.1 ross * the direct-mapped DMA window, so we must use
266 1.1 ross * SGMAPs.
267 1.1 ross */
268 1.1 ross return (&pcp->pc_dmat_sgmap);
269 1.1 ross
270 1.1 ross default:
271 1.1 ross panic("tsp_dma_get_tag: shouldn't be here, really...");
272 1.1 ross }
273 1.1 ross }
274 1.1 ross
275 1.1 ross /*
276 1.1 ross * Load a TSP SGMAP-mapped DMA map with a linear buffer.
277 1.1 ross */
278 1.1 ross int
279 1.9 dsl tsp_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags)
280 1.1 ross {
281 1.1 ross int error;
282 1.1 ross
283 1.1 ross error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
284 1.1 ross t->_sgmap);
285 1.1 ross if (error == 0)
286 1.1 ross tsp_tlb_invalidate(t->_cookie);
287 1.1 ross
288 1.1 ross return (error);
289 1.1 ross }
290 1.1 ross
291 1.1 ross /*
292 1.1 ross * Load a TSP SGMAP-mapped DMA map with an mbuf chain.
293 1.1 ross */
294 1.1 ross int
295 1.9 dsl tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags)
296 1.1 ross {
297 1.1 ross int error;
298 1.1 ross
299 1.1 ross error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
300 1.1 ross if (error == 0)
301 1.1 ross tsp_tlb_invalidate(t->_cookie);
302 1.1 ross
303 1.1 ross return (error);
304 1.1 ross }
305 1.1 ross
306 1.1 ross /*
307 1.1 ross * Load a TSP SGMAP-mapped DMA map with a uio.
308 1.1 ross */
309 1.1 ross int
310 1.9 dsl tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
311 1.1 ross {
312 1.1 ross int error;
313 1.1 ross
314 1.1 ross error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
315 1.1 ross if (error == 0)
316 1.1 ross tsp_tlb_invalidate(t->_cookie);
317 1.1 ross
318 1.1 ross return (error);
319 1.1 ross }
320 1.1 ross
321 1.1 ross /*
322 1.1 ross * Load a TSP SGMAP-mapped DMA map with raw memory.
323 1.1 ross */
324 1.1 ross int
325 1.9 dsl tsp_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
326 1.1 ross {
327 1.1 ross int error;
328 1.1 ross
329 1.1 ross error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
330 1.1 ross t->_sgmap);
331 1.1 ross if (error == 0)
332 1.1 ross tsp_tlb_invalidate(t->_cookie);
333 1.1 ross
334 1.1 ross return (error);
335 1.1 ross }
336 1.1 ross
337 1.1 ross /*
338 1.1 ross * Unload a TSP DMA map.
339 1.1 ross */
340 1.1 ross void
341 1.9 dsl tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
342 1.1 ross {
343 1.1 ross
344 1.1 ross /*
345 1.1 ross * Invalidate any SGMAP page table entries used by this
346 1.1 ross * mapping.
347 1.1 ross */
348 1.1 ross pci_sgmap_pte64_unload(t, map, t->_sgmap);
349 1.1 ross tsp_tlb_invalidate(t->_cookie);
350 1.1 ross
351 1.1 ross /*
352 1.1 ross * Do the generic bits of the unload.
353 1.1 ross */
354 1.1 ross _bus_dmamap_unload(t, map);
355 1.1 ross }
356 1.1 ross
357 1.1 ross /*
358 1.1 ross * Flush the TSP scatter/gather TLB.
359 1.1 ross */
360 1.1 ross void
361 1.9 dsl tsp_tlb_invalidate(struct tsp_config *pcp)
362 1.1 ross {
363 1.1 ross
364 1.1 ross alpha_mb();
365 1.13 tsutsui *pcp->pc_tlbia = 0;
366 1.1 ross alpha_mb();
367 1.1 ross }
368