xref: /netbsd-src/sys/arch/alpha/pci/dwlpx_dma.c (revision 17306b8fd0952c7489f93f0230818481e5a1e2c9)
1 /* $NetBSD: dwlpx_dma.c,v 1.15 2001/01/03 19:16:00 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
41 
42 __KERNEL_RCSID(0, "$NetBSD: dwlpx_dma.c,v 1.15 2001/01/03 19:16:00 thorpej Exp $");
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/device.h>
48 #include <sys/malloc.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #define _ALPHA_BUS_DMA_PRIVATE
53 #include <machine/bus.h>
54 
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57 #include <alpha/tlsb/tlsbreg.h>
58 #include <alpha/tlsb/kftxxvar.h>
59 #include <alpha/tlsb/kftxxreg.h>
60 #include <alpha/pci/dwlpxreg.h>
61 #include <alpha/pci/dwlpxvar.h>
62 #include <alpha/pci/pci_kn8ae.h>
63 
64 bus_dma_tag_t dwlpx_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
65 
66 int	dwlpx_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
67 	    bus_size_t, struct proc *, int));
68 
69 int	dwlpx_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
70 	    struct mbuf *, int));
71 
72 int	dwlpx_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
73 	    struct uio *, int));
74 
75 int	dwlpx_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
76 	    bus_dma_segment_t *, int, bus_size_t, int));
77 
78 void	dwlpx_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
79 
80 /*
81  * Direct-mapped window: 2G at 2G
82  */
83 #define	DWLPx_DIRECT_MAPPED_BASE	(2UL*1024UL*1024UL*1024UL)
84 #define	DWLPx_DIRECT_MAPPED_SIZE	(2UL*1024UL*1024UL*1024UL)
85 
86 /*
87  * SGMAP window A: 256M or 1G at 1G
88  */
89 #define	DWLPx_SG_MAPPED_BASE		(1*1024*1024*1024)
90 #define	DWLPx_SG_MAPPED_SIZE(x)		((x) * PAGE_SIZE)
91 
92 /*
93  * Set this to always use SGMAPs.
94  */
95 #ifdef DWLPX_ALWAYS_USE_SGMAP
96 int	dwlpx_always_use_sgmap = 1;
97 #else
98 int	dwlpx_always_use_sgmap = 0;
99 #endif
100 
101 void
102 dwlpx_dma_init(ccp)
103 	struct dwlpx_config *ccp;
104 {
105 	char *exname;
106 	bus_dma_tag_t t;
107 	u_int32_t *page_table;
108 	int i, lim, wmask;
109 
110 	/*
111 	 * Determine size of Window C based on the amount of SGMAP
112 	 * page table SRAM available.
113 	 */
114 	if (ccp->cc_sc->dwlpx_sgmapsz == DWLPX_SG128K) {
115 		lim = 128 * 1024;
116 		wmask = PCIA_WMASK_1G;
117 	} else {
118 		lim = 32 * 1024;
119 		wmask = PCIA_WMASK_256M;
120 	}
121 
122 	/*
123 	 * Initialize the DMA tag used for direct-mapped DMA.  Chain
124 	 * this window to the SGMAP window, because we might have
125 	 * a lot of memory in the machine.
126 	 */
127 	t = &ccp->cc_dmat_direct;
128 	t->_cookie = ccp;
129 	t->_wbase = DWLPx_DIRECT_MAPPED_BASE;
130 	t->_wsize = DWLPx_DIRECT_MAPPED_SIZE;
131 	t->_next_window = &ccp->cc_dmat_sgmap;
132 	t->_boundary = 0;
133 	t->_sgmap = NULL;
134 	t->_get_tag = dwlpx_dma_get_tag;
135 	t->_dmamap_create = _bus_dmamap_create;
136 	t->_dmamap_destroy = _bus_dmamap_destroy;
137 	t->_dmamap_load = _bus_dmamap_load_direct;
138 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
139 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
140 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
141 	t->_dmamap_unload = _bus_dmamap_unload;
142 	t->_dmamap_sync = _bus_dmamap_sync;
143 
144 	t->_dmamem_alloc = _bus_dmamem_alloc;
145 	t->_dmamem_free = _bus_dmamem_free;
146 	t->_dmamem_map = _bus_dmamem_map;
147 	t->_dmamem_unmap = _bus_dmamem_unmap;
148 	t->_dmamem_mmap = _bus_dmamem_mmap;
149 
150 	/*
151 	 * Initialize the DMA tag used for sgmap-mapped DMA.
152 	 */
153 	t = &ccp->cc_dmat_sgmap;
154 	t->_cookie = ccp;
155 	t->_wbase = DWLPx_SG_MAPPED_BASE;
156 	t->_wsize = DWLPx_SG_MAPPED_SIZE(lim);
157 	t->_next_window = NULL;
158 	t->_boundary = 0;
159 	t->_sgmap = &ccp->cc_sgmap;
160 	t->_get_tag = dwlpx_dma_get_tag;
161 	t->_dmamap_create = alpha_sgmap_dmamap_create;
162 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
163 	t->_dmamap_load = dwlpx_bus_dmamap_load_sgmap;
164 	t->_dmamap_load_mbuf = dwlpx_bus_dmamap_load_mbuf_sgmap;
165 	t->_dmamap_load_uio = dwlpx_bus_dmamap_load_uio_sgmap;
166 	t->_dmamap_load_raw = dwlpx_bus_dmamap_load_raw_sgmap;
167 	t->_dmamap_unload = dwlpx_bus_dmamap_unload_sgmap;
168 	t->_dmamap_sync = _bus_dmamap_sync;
169 
170 	t->_dmamem_alloc = _bus_dmamem_alloc;
171 	t->_dmamem_free = _bus_dmamem_free;
172 	t->_dmamem_map = _bus_dmamem_map;
173 	t->_dmamem_unmap = _bus_dmamem_unmap;
174 	t->_dmamem_mmap = _bus_dmamem_mmap;
175 
176 	/*
177 	 * A few notes about SGMAP-mapped DMA on the DWLPx:
178 	 *
179 	 * The DWLPx has PCIA-resident SRAM that is used for
180 	 * the SGMAP page table; there is no TLB.  The DWLPA
181 	 * has room for 32K entries, yielding a total of 256M
182 	 * of sgva space.  The DWLPB has 32K entries or 128K
183 	 * entries, depending on TBIT, yielding either 256M or
184 	 * 1G of sgva space.
185 	 *
186 	 * This sgva space must be shared across all windows
187 	 * that wish to use SGMAP-mapped DMA; make sure to
188 	 * adjust the "sgvabase" argument to alpha_sgmap_init()
189 	 * accordingly if you create more than one SGMAP-mapped
190 	 * window.  Note that sgvabase != window base.  The former
191 	 * is used to compute indexes into the page table only.
192 	 *
193 	 * In the current implementation the first window is a
194 	 * 2G window direct-mapped mapped at 2G and the second
195 	 * window is disabled and the third window is a 256M
196 	 * or 1GB scatter/gather map at 1GB.
197 	 *
198 	 * We just punt on trying to support ISA DMA.  If you want
199 	 * try that on a TurboLaser, well, you just lose.
200 	 */
201 
202 	/*
203 	 * Initialize the page table.
204 	 */
205 	page_table =
206 	    (u_int32_t *)ALPHA_PHYS_TO_K0SEG(PCIA_SGMAP_PT + ccp->cc_sysbase);
207 	for (i = 0; i < lim; i++)
208 		page_table[i * SGMAP_PTE_SPACING] = 0;
209 	alpha_mb();
210 
211 	/*
212 	 * Initialize the SGMAP for window C:
213 	 *
214 	 *	Size: 256M or 1GB
215 	 *	Window base: 1GB
216 	 *	SGVA base: 0
217 	 */
218 	exname = malloc(16, M_DEVBUF, M_NOWAIT);
219 	if (exname == NULL)
220 		panic("dwlpx_dma_init");
221 	sprintf(exname, "%s_sgmap_a", ccp->cc_sc->dwlpx_dev.dv_xname);
222 	alpha_sgmap_init(t, &ccp->cc_sgmap, exname, DWLPx_SG_MAPPED_BASE,
223 	    0, DWLPx_SG_MAPPED_SIZE(lim), sizeof(u_int32_t),
224 	    (void *)page_table, 0);
225 
226 	/*
227 	 * Set up DMA windows for this DWLPx.
228 	 *
229 	 * Do this even for all HPCs- even for the nonexistent
230 	 * one on hose zero of a KFTIA.
231 	 */
232 	for (i = 0; i < NHPC; i++) {
233 		REGVAL(PCIA_WMASK_A(i) + ccp->cc_sysbase) = PCIA_WMASK_2G;
234 		REGVAL(PCIA_TBASE_A(i) + ccp->cc_sysbase) = 0;
235 		REGVAL(PCIA_WBASE_A(i) + ccp->cc_sysbase) =
236 		    DWLPx_DIRECT_MAPPED_BASE | PCIA_WBASE_W_EN;
237 
238 		REGVAL(PCIA_WMASK_B(i) + ccp->cc_sysbase) = 0;
239 		REGVAL(PCIA_TBASE_B(i) + ccp->cc_sysbase) = 0;
240 		REGVAL(PCIA_WBASE_B(i) + ccp->cc_sysbase) = 0;
241 
242 		REGVAL(PCIA_WMASK_C(i) + ccp->cc_sysbase) = wmask;
243 		REGVAL(PCIA_TBASE_C(i) + ccp->cc_sysbase) = 0;
244 		REGVAL(PCIA_WBASE_C(i) + ccp->cc_sysbase) =
245 		    DWLPx_SG_MAPPED_BASE | PCIA_WBASE_W_EN | PCIA_WBASE_SG_EN;
246 		alpha_mb();
247 	}
248 
249 	/* XXX XXX BEGIN XXX XXX */
250 	{							/* XXX */
251 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
252 		alpha_XXX_dmamap_or = DWLPx_DIRECT_MAPPED_BASE;	/* XXX */
253 	}							/* XXX */
254 	/* XXX XXX END XXX XXX */
255 }
256 
257 /*
258  * Return the bus dma tag to be used for the specified bus type.
259  * INTERNAL USE ONLY!
260  */
261 bus_dma_tag_t
262 dwlpx_dma_get_tag(t, bustype)
263 	bus_dma_tag_t t;
264 	alpha_bus_t bustype;
265 {
266 	struct dwlpx_config *ccp = t->_cookie;
267 
268 	switch (bustype) {
269 	case ALPHA_BUS_PCI:
270 	case ALPHA_BUS_EISA:
271 		if (dwlpx_always_use_sgmap) {
272 			/*
273 			 * Always use SGMAPs (for testing purposes?)
274 			 */
275 			return (&ccp->cc_dmat_sgmap);
276 		}
277 		/*
278 		 * Give these busses the direct-mapped window; if the
279 		 * address crosses the threshold, it will fall back
280 		 * onto the SGMAP window.
281 		 */
282 		return (&ccp->cc_dmat_direct);
283 
284 	case ALPHA_BUS_ISA:
285 		/*
286 		 * Don't even bother; it's not easy, given how the
287 		 * page table is arranged, and it's not really worth
288 		 * the trouble.
289 		 */
290 		panic("dwlpx_dma_get_tag: ISA DMA?  Forget it...");
291 		/* NOTREACHED */
292 
293 	default:
294 		panic("dwlpx_dma_get_tag: shouldn't be here, really...");
295 	}
296 }
297 
298 /*
299  * Load a DWLPx SGMAP-mapped DMA map with a linear buffer.
300  */
301 int
302 dwlpx_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
303 	bus_dma_tag_t t;
304 	bus_dmamap_t map;
305 	void *buf;
306 	bus_size_t buflen;
307 	struct proc *p;
308 	int flags;
309 {
310 
311 	return (pci_sgmap_pte32_load(t, map, buf, buflen, p, flags,
312 	    t->_sgmap));
313 }
314 
315 /*
316  * Load a DWLPx SGMAP-mapped DMA map with an mbuf chain.
317  */
318 int
319 dwlpx_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
320 	bus_dma_tag_t t;
321 	bus_dmamap_t map;
322 	struct mbuf *m;
323 	int flags;
324 {
325 
326 	return (pci_sgmap_pte32_load_mbuf(t, map, m, flags, t->_sgmap));
327 }
328 
329 /*
330  * Load a DWLPx SGMAP-mapped DMA map with a uio.
331  */
332 int
333 dwlpx_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
334 	bus_dma_tag_t t;
335 	bus_dmamap_t map;
336 	struct uio *uio;
337 	int flags;
338 {
339 
340 	return (pci_sgmap_pte32_load_uio(t, map, uio, flags, t->_sgmap));
341 }
342 
343 /*
344  * Load a DWLPx SGMAP-mapped DMA map with raw memory.
345  */
346 int
347 dwlpx_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
348 	bus_dma_tag_t t;
349 	bus_dmamap_t map;
350 	bus_dma_segment_t *segs;
351 	int nsegs;
352 	bus_size_t size;
353 	int flags;
354 {
355 
356 	return (pci_sgmap_pte32_load_raw(t, map, segs, nsegs, size, flags,
357 	    t->_sgmap));
358 }
359 
360 /*
361  * Unload a DWLPx DMA map.
362  */
363 void
364 dwlpx_bus_dmamap_unload_sgmap(t, map)
365 	bus_dma_tag_t t;
366 	bus_dmamap_t map;
367 {
368 
369 	/*
370 	 * Invalidate any SGMAP page table entries used by this
371 	 * mapping.
372 	 */
373 	pci_sgmap_pte32_unload(t, map, t->_sgmap);
374 
375 	/*
376 	 * Do the generic bits of the unload.
377 	 */
378 	_bus_dmamap_unload(t, map);
379 }
380