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