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