xref: /netbsd-src/sys/arch/alpha/pci/apecs_dma.c (revision bcc8ec9959e7b01e313d813067bfb43a3ad70551)
1 /* $NetBSD: apecs_dma.c,v 1.14 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: apecs_dma.c,v 1.14 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/pci/apecsreg.h>
58 #include <alpha/pci/apecsvar.h>
59 
60 bus_dma_tag_t apecs_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
61 
62 int	apecs_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
63 	    bus_size_t, struct proc *, int));
64 
65 int	apecs_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
66 	    struct mbuf *, int));
67 
68 int	apecs_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
69 	    struct uio *, int));
70 
71 int	apecs_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
72 	    bus_dma_segment_t *, int, bus_size_t, int));
73 
74 void	apecs_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
75 
76 /*
77  * Direct-mapped window: 1G at 1G
78  */
79 #define	APECS_DIRECT_MAPPED_BASE (1*1024*1024*1024)
80 #define	APECS_DIRECT_MAPPED_SIZE (1*1024*1024*1024)
81 
82 /*
83  * SGMAP window: 8M at 8M
84  */
85 #define	APECS_SGMAP_MAPPED_BASE	(8*1024*1024)
86 #define	APECS_SGMAP_MAPPED_SIZE	(8*1024*1024)
87 
88 /*
89  * Macro to flush APECS scatter/gather TLB.
90  */
91 #define	APECS_TLB_INVALIDATE() \
92 do { \
93 	alpha_mb(); \
94 	REGVAL(EPIC_TBIA) = 0; \
95 	alpha_mb(); \
96 } while (0)
97 
98 void
99 apecs_dma_init(acp)
100 	struct apecs_config *acp;
101 {
102 	bus_addr_t tbase;
103 	bus_dma_tag_t t;
104 
105 	/*
106 	 * Initialize the DMA tag used for direct-mapped DMA.
107 	 */
108 	t = &acp->ac_dmat_direct;
109 	t->_cookie = acp;
110 	t->_wbase = APECS_DIRECT_MAPPED_BASE;
111 	t->_wsize = APECS_DIRECT_MAPPED_SIZE;
112 	t->_next_window = NULL;
113 	t->_boundary = 0;
114 	t->_sgmap = NULL;
115 	t->_get_tag = apecs_dma_get_tag;
116 	t->_dmamap_create = _bus_dmamap_create;
117 	t->_dmamap_destroy = _bus_dmamap_destroy;
118 	t->_dmamap_load = _bus_dmamap_load_direct;
119 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
120 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
121 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
122 	t->_dmamap_unload = _bus_dmamap_unload;
123 	t->_dmamap_sync = _bus_dmamap_sync;
124 
125 	t->_dmamem_alloc = _bus_dmamem_alloc;
126 	t->_dmamem_free = _bus_dmamem_free;
127 	t->_dmamem_map = _bus_dmamem_map;
128 	t->_dmamem_unmap = _bus_dmamem_unmap;
129 	t->_dmamem_mmap = _bus_dmamem_mmap;
130 
131 	/*
132 	 * Initialize the DMA tag used for sgmap-mapped DMA.
133 	 */
134 	t = &acp->ac_dmat_sgmap;
135 	t->_cookie = acp;
136 	t->_wbase = APECS_SGMAP_MAPPED_BASE;
137 	t->_wsize = APECS_SGMAP_MAPPED_SIZE;
138 	t->_next_window = NULL;
139 	t->_boundary = 0;
140 	t->_sgmap = &acp->ac_sgmap;
141 	t->_get_tag = apecs_dma_get_tag;
142 	t->_dmamap_create = alpha_sgmap_dmamap_create;
143 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
144 	t->_dmamap_load = apecs_bus_dmamap_load_sgmap;
145 	t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_sgmap;
146 	t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_sgmap;
147 	t->_dmamap_load_raw = apecs_bus_dmamap_load_raw_sgmap;
148 	t->_dmamap_unload = apecs_bus_dmamap_unload_sgmap;
149 	t->_dmamap_sync = _bus_dmamap_sync;
150 
151 	t->_dmamem_alloc = _bus_dmamem_alloc;
152 	t->_dmamem_free = _bus_dmamem_free;
153 	t->_dmamem_map = _bus_dmamem_map;
154 	t->_dmamem_unmap = _bus_dmamem_unmap;
155 	t->_dmamem_mmap = _bus_dmamem_mmap;
156 
157 	/*
158 	 * The firmware has set up window 2 as a 1G direct-mapped DMA
159 	 * window beginning at 1G.  We leave it alone.  Disable
160 	 * window 1.
161 	 */
162 	REGVAL(EPIC_PCI_BASE_1) = 0;
163 	alpha_mb();
164 
165 	/*
166 	 * Initialize the SGMAP.
167 	 */
168 	alpha_sgmap_init(t, &acp->ac_sgmap, "apecs_sgmap",
169 	    APECS_SGMAP_MAPPED_BASE, 0, APECS_SGMAP_MAPPED_SIZE,
170 	    sizeof(u_int64_t), NULL, 0);
171 
172 	/*
173 	 * Set up window 1 as an 8MB SGMAP-mapped window
174 	 * starting at 8MB.
175 	 */
176 	REGVAL(EPIC_PCI_BASE_1) = APECS_SGMAP_MAPPED_BASE |
177 	    EPIC_PCI_BASE_SGEN | EPIC_PCI_BASE_WENB;
178 	alpha_mb();
179 
180 	REGVAL(EPIC_PCI_MASK_1) = EPIC_PCI_MASK_8M;
181 	alpha_mb();
182 
183 	tbase = acp->ac_sgmap.aps_ptpa >> EPIC_TBASE_SHIFT;
184 	if ((tbase & EPIC_TBASE_T_BASE) != tbase)
185 		panic("apecs_dma_init: bad page table address");
186 	REGVAL(EPIC_TBASE_1) = tbase;
187 	alpha_mb();
188 
189 	APECS_TLB_INVALIDATE();
190 
191 	/* XXX XXX BEGIN XXX XXX */
192 	{							/* XXX */
193 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
194 		alpha_XXX_dmamap_or = APECS_DIRECT_MAPPED_BASE;	/* XXX */
195 	}							/* XXX */
196 	/* XXX XXX END XXX XXX */
197 }
198 
199 /*
200  * Return the bus dma tag to be used for the specified bus type.
201  * INTERNAL USE ONLY!
202  */
203 bus_dma_tag_t
204 apecs_dma_get_tag(t, bustype)
205 	bus_dma_tag_t t;
206 	alpha_bus_t bustype;
207 {
208 	struct apecs_config *acp = t->_cookie;
209 
210 	switch (bustype) {
211 	case ALPHA_BUS_PCI:
212 	case ALPHA_BUS_EISA:
213 		/*
214 		 * Systems with an APECS can only support 1G
215 		 * of memory, so we use the direct-mapped window
216 		 * on busses that have 32-bit DMA.
217 		 */
218 		return (&acp->ac_dmat_direct);
219 
220 	case ALPHA_BUS_ISA:
221 		/*
222 		 * ISA doesn't have enough address bits to use
223 		 * the direct-mapped DMA window, so we must use
224 		 * SGMAPs.
225 		 */
226 		return (&acp->ac_dmat_sgmap);
227 
228 	default:
229 		panic("apecs_dma_get_tag: shouldn't be here, really...");
230 	}
231 }
232 
233 /*
234  * Load an APECS SGMAP-mapped DMA map with a linear buffer.
235  */
236 int
237 apecs_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
238 	bus_dma_tag_t t;
239 	bus_dmamap_t map;
240 	void *buf;
241 	bus_size_t buflen;
242 	struct proc *p;
243 	int flags;
244 {
245 	int error;
246 
247 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
248 	    t->_sgmap);
249 	if (error == 0)
250 		APECS_TLB_INVALIDATE();
251 
252 	return (error);
253 }
254 
255 /*
256  * Load an APECS SGMAP-mapped DMA map with an mbuf chain.
257  */
258 int
259 apecs_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
260 	bus_dma_tag_t t;
261 	bus_dmamap_t map;
262 	struct mbuf *m;
263 	int flags;
264 {
265 	int error;
266 
267 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
268 	if (error == 0)
269 		APECS_TLB_INVALIDATE();
270 
271 	return (error);
272 }
273 
274 /*
275  * Load an APECS SGMAP-mapped DMA map with a uio.
276  */
277 int
278 apecs_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
279 	bus_dma_tag_t t;
280 	bus_dmamap_t map;
281 	struct uio *uio;
282 	int flags;
283 {
284 	int error;
285 
286 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
287 	if (error == 0)
288 		APECS_TLB_INVALIDATE();
289 
290 	return (error);
291 }
292 
293 /*
294  * Load an APECS SGMAP-mapped DMA map with raw memory.
295  */
296 int
297 apecs_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
298 	bus_dma_tag_t t;
299 	bus_dmamap_t map;
300 	bus_dma_segment_t *segs;
301 	int nsegs;
302 	bus_size_t size;
303 	int flags;
304 {
305 	int error;
306 
307 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
308 	    t->_sgmap);
309 	if (error == 0)
310 		APECS_TLB_INVALIDATE();
311 
312 	return (error);
313 }
314 
315 /*
316  * Unload an APECS DMA map.
317  */
318 void
319 apecs_bus_dmamap_unload_sgmap(t, map)
320 	bus_dma_tag_t t;
321 	bus_dmamap_t map;
322 {
323 
324 	/*
325 	 * Invalidate any SGMAP page table entries used by this
326 	 * mapping.
327 	 */
328 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
329 	APECS_TLB_INVALIDATE();
330 
331 	/*
332 	 * Do the generic bits of the unload.
333 	 */
334 	_bus_dmamap_unload(t, map);
335 }
336