xref: /openbsd-src/sys/arch/alpha/pci/tsp_dma.c (revision ac7be579d92df6d99ac28b2d6c57a29ca0449be9)
1 /* $OpenBSD: tsp_dma.c,v 1.12 2015/01/03 20:44:23 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
tsp_dma_init(struct device * tsp,struct tsp_config * pcp)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 const struct map_expected {
109 		u_int32_t base, mask, physbase, enables;
110 	} premap[4] = {
111 		/* 0-8MB at 8MB with S/G DMA (isa space) */
112 		{ 0x00800000, 0x00700000, 0x00000000, WSBA_ENA | WSBA_SG },
113 		/* 0-1GB at 2GB direct */
114 		{ 0x80000000, 0x3ff00000, 0x00000000, WSBA_ENA           },
115 		{ 0, 0, 0 },
116 		{ 0, 0, 0 }
117 	};
118 
119 	alpha_mb();
120 	for(i = 0; i < 4; ++i) {
121 #ifdef DEBUG
122 		if (EDIFF(pccsr->tsp_wsba[i].tsg_r, premap[i].base) ||
123 		    EDIFF(pccsr->tsp_wsm[i].tsg_r, premap[i].mask) ||
124 		    ((premap[i].enables & WSBA_SG) == 0 &&
125 		      pccsr->tsp_tba[i].tsg_r != premap[i].physbase))
126 			printf("%s: window %d: %lx/base %lx/mask %lx"
127 			    " reinitialized\n",
128 			    tsp->dv_xname, i,
129 			    pccsr->tsp_wsba[i].tsg_r,
130 			    pccsr->tsp_wsm[i].tsg_r,
131 			    pccsr->tsp_tba[i].tsg_r);
132 #endif
133 		pccsr->tsp_wsba[i].tsg_r = premap[i].base | premap[i].enables;
134 		pccsr->tsp_wsm[i].tsg_r = premap[i].mask;
135 		if ((premap[i].enables & WSBA_SG) == 0)
136 			pccsr->tsp_tba[i].tsg_r = premap[i].physbase;
137 	}
138 	alpha_mb();
139 
140 	/*
141 	 * Initialize the DMA tags used for direct-mapped DMA.
142 	 */
143 	t = &pcp->pc_dmat_direct;
144 	t->_cookie = pcp;
145 	t->_wbase = dwbase = WSBA_ADDR(pccsr->tsp_wsba[1].tsg_r);
146 	t->_wsize = dwlen = WSM_LEN(pccsr->tsp_wsm[1].tsg_r);
147 	t->_next_window = &pcp->pc_dmat_sgmap;
148 	t->_boundary = 0;
149 	t->_sgmap = NULL;
150 	t->_get_tag = tsp_dma_get_tag;
151 	/*
152 	 * Since we fall back to sgmap if the direct mapping fails,
153 	 * we need to set up for sgmap in any case.
154 	 */
155 	t->_dmamap_create = alpha_sgmap_dmamap_create;
156 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
157 	t->_dmamap_load = _bus_dmamap_load_direct;
158 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
159 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
160 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
161 	t->_dmamap_unload = _bus_dmamap_unload;
162 	t->_dmamap_sync = _bus_dmamap_sync;
163 
164 	t->_dmamem_alloc = _bus_dmamem_alloc;
165 	t->_dmamem_free = _bus_dmamem_free;
166 	t->_dmamem_map = _bus_dmamem_map;
167 	t->_dmamem_unmap = _bus_dmamem_unmap;
168 	t->_dmamem_mmap = _bus_dmamem_mmap;
169 
170 	/*
171 	 * Initialize the DMA tag used for sgmap-mapped DMA.
172 	 */
173 	t = &pcp->pc_dmat_sgmap;
174 	t->_cookie = pcp;
175 	t->_wbase = sgwbase = WSBA_ADDR(pccsr->tsp_wsba[0].tsg_r);
176 	t->_wsize = sgwlen = WSM_LEN(pccsr->tsp_wsm[0].tsg_r);
177 	t->_next_window = NULL;
178 	t->_boundary = 0;
179 	t->_sgmap = &pcp->pc_sgmap;
180 	t->_get_tag = tsp_dma_get_tag;
181 	t->_dmamap_create = alpha_sgmap_dmamap_create;
182 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
183 	t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
184 	t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
185 	t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
186 	t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
187 	t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
188 	t->_dmamap_sync = _bus_dmamap_sync;
189 
190 	t->_dmamem_alloc = _bus_dmamem_alloc;
191 	t->_dmamem_free = _bus_dmamem_free;
192 	t->_dmamem_map = _bus_dmamem_map;
193 	t->_dmamem_unmap = _bus_dmamem_unmap;
194 	t->_dmamem_mmap = _bus_dmamem_mmap;
195 
196 	/*
197 	 * Initialize the SGMAP.  Align page table to 32k in case
198 	 * window is somewhat larger than expected.
199 	 */
200 	alpha_sgmap_init(t, &pcp->pc_sgmap, "tsp_sgmap",
201 	    sgwbase, 0, sgwlen, sizeof(u_int64_t), NULL, (32*1024));
202 
203 	/*
204 	 * Check windows for sanity, especially if we later decide to
205 	 * use the firmware's initialization in some cases.
206 	 */
207 	if ((sgwbase <= dwbase && dwbase < sgwbase + sgwlen) ||
208 	    (dwbase <= sgwbase && sgwbase < dwbase + dwlen))
209 		panic("%s: overlap sg 0x%lx len 0x%lx d 0x%lx len 0x%lx",
210 		    __func__, sgwbase, sgwlen, dwbase, dwlen);
211 
212 	tbase = pcp->pc_sgmap.aps_ptpa;
213 	if (tbase & ~0x7fffffc00UL)
214 		panic("tsp_dma_init: bad page table address");
215 	alpha_mb();
216 	pccsr->tsp_tba[0].tsg_r = tbase;
217 	alpha_mb();
218 
219 	tsp_tlb_invalidate(pcp);
220 	alpha_mb();
221 
222 	/* XXX XXX BEGIN XXX XXX */
223 	{							/* XXX */
224 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
225 		alpha_XXX_dmamap_or = dwbase;			/* XXX */
226 	}							/* XXX */
227 	/* XXX XXX END XXX XXX */
228 }
229 
230 /*
231  * Return the bus dma tag to be used for the specified bus type.
232  * INTERNAL USE ONLY!
233  */
234 bus_dma_tag_t
tsp_dma_get_tag(t,bustype)235 tsp_dma_get_tag(t, bustype)
236 	bus_dma_tag_t t;
237 	alpha_bus_t bustype;
238 {
239 	struct tsp_config *pcp = t->_cookie;
240 
241 	switch (bustype) {
242 	case ALPHA_BUS_PCI:
243 	case ALPHA_BUS_EISA:
244 		/*
245 		 * The direct mapped window will work for most systems,
246 		 * most of the time. When it doesn't, we chain to the sgmap
247 		 * window automatically.
248 		 */
249 		return (&pcp->pc_dmat_direct);
250 
251 	case ALPHA_BUS_ISA:
252 		/*
253 		 * ISA doesn't have enough address bits to use
254 		 * the direct-mapped DMA window, so we must use
255 		 * SGMAPs.
256 		 */
257 		return (&pcp->pc_dmat_sgmap);
258 
259 	default:
260 		panic("tsp_dma_get_tag: shouldn't be here, really...");
261 	}
262 }
263 
264 /*
265  * Load a TSP SGMAP-mapped DMA map with a linear buffer.
266  */
267 int
tsp_bus_dmamap_load_sgmap(t,map,buf,buflen,p,flags)268 tsp_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
269 	bus_dma_tag_t t;
270 	bus_dmamap_t map;
271 	void *buf;
272 	bus_size_t buflen;
273 	struct proc *p;
274 	int flags;
275 {
276 	int error;
277 
278 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
279 	    t->_sgmap);
280 	if (error == 0)
281 		tsp_tlb_invalidate(t->_cookie);
282 
283 	return (error);
284 }
285 
286 /*
287  * Load a TSP SGMAP-mapped DMA map with an mbuf chain.
288  */
289 int
tsp_bus_dmamap_load_mbuf_sgmap(t,map,m,flags)290 tsp_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
291 	bus_dma_tag_t t;
292 	bus_dmamap_t map;
293 	struct mbuf *m;
294 	int flags;
295 {
296 	int error;
297 
298 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
299 	if (error == 0)
300 		tsp_tlb_invalidate(t->_cookie);
301 
302 	return (error);
303 }
304 
305 /*
306  * Load a TSP SGMAP-mapped DMA map with a uio.
307  */
308 int
tsp_bus_dmamap_load_uio_sgmap(t,map,uio,flags)309 tsp_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
310 	bus_dma_tag_t t;
311 	bus_dmamap_t map;
312 	struct uio *uio;
313 	int flags;
314 {
315 	int error;
316 
317 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
318 	if (error == 0)
319 		tsp_tlb_invalidate(t->_cookie);
320 
321 	return (error);
322 }
323 
324 /*
325  * Load a TSP SGMAP-mapped DMA map with raw memory.
326  */
327 int
tsp_bus_dmamap_load_raw_sgmap(t,map,segs,nsegs,size,flags)328 tsp_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
329 	bus_dma_tag_t t;
330 	bus_dmamap_t map;
331 	bus_dma_segment_t *segs;
332 	int nsegs;
333 	bus_size_t size;
334 	int flags;
335 {
336 	int error;
337 
338 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
339 	    t->_sgmap);
340 	if (error == 0)
341 		tsp_tlb_invalidate(t->_cookie);
342 
343 	return (error);
344 }
345 
346 /*
347  * Unload a TSP DMA map.
348  */
349 void
tsp_bus_dmamap_unload_sgmap(t,map)350 tsp_bus_dmamap_unload_sgmap(t, map)
351 	bus_dma_tag_t t;
352 	bus_dmamap_t map;
353 {
354 
355 	/*
356 	 * Invalidate any SGMAP page table entries used by this
357 	 * mapping.
358 	 */
359 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
360 	tsp_tlb_invalidate(t->_cookie);
361 
362 	/*
363 	 * Do the generic bits of the unload.
364 	 */
365 	_bus_dmamap_unload(t, map);
366 }
367 
368 /*
369  * Flush the TSP scatter/gather TLB.
370  */
371 void
tsp_tlb_invalidate(pcp)372 tsp_tlb_invalidate(pcp)
373 	struct tsp_config *pcp;
374 {
375 
376 	alpha_mb();
377 	*pcp->pc_tlbia = 0;
378 	alpha_mb();
379 }
380