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