xref: /netbsd-src/sys/arch/alpha/pci/lca_dma.c (revision 87305e5a45705adaf8ef16bfa874ae42fced1483)
1 /* $NetBSD: lca_dma.c,v 1.28 2023/08/01 21:26:27 andvar 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
34 
35 __KERNEL_RCSID(0, "$NetBSD: lca_dma.c,v 1.28 2023/08/01 21:26:27 andvar Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 
42 #define _ALPHA_BUS_DMA_PRIVATE
43 #include <sys/bus.h>
44 
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <alpha/pci/lcareg.h>
48 #include <alpha/pci/lcavar.h>
49 
50 static bus_dma_tag_t lca_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
51 
52 static int	lca_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
53 		    bus_size_t, struct proc *, int);
54 
55 static int	lca_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
56 		    struct mbuf *, int);
57 
58 static int	lca_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
59 		    struct uio *, int);
60 
61 static int	lca_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
62 		    bus_dma_segment_t *, int, bus_size_t, int);
63 
64 static void	lca_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
65 
66 /*
67  * Direct-mapped window: 1G at 1G
68  */
69 #define	LCA_DIRECT_MAPPED_BASE	(1*1024*1024*1024)
70 #define	LCA_DIRECT_MAPPED_SIZE	(1*1024*1024*1024)
71 
72 /*
73  * SGMAP window: 8M at 8M
74  */
75 #define	LCA_SGMAP_MAPPED_BASE	(8*1024*1024)
76 #define	LCA_SGMAP_MAPPED_SIZE	(8*1024*1024)
77 
78 /*
79  * The LCA doesn't have a DMA prefetch threshold.  However, it is known
80  * to lose if we don't allocate a spill page.  So initialize it to 256.
81  */
82 #define	LCA_SGMAP_PFTHRESH	256
83 
84 /*
85  * Macro to flush LCA scatter/gather TLB.
86  */
87 #define	LCA_TLB_INVALIDATE() \
88 do { \
89 	alpha_mb(); \
90 	REGVAL64(LCA_IOC_TBIA) = 0; \
91 	alpha_mb(); \
92 } while (0)
93 
94 void
lca_dma_init(struct lca_config * lcp)95 lca_dma_init(struct lca_config *lcp)
96 {
97 	bus_dma_tag_t t;
98 
99 	/*
100 	 * Initialize the DMA tag used for direct-mapped DMA.
101 	 */
102 	t = &lcp->lc_dmat_direct;
103 	t->_cookie = lcp;
104 	t->_wbase = LCA_DIRECT_MAPPED_BASE;
105 	t->_wsize = LCA_DIRECT_MAPPED_SIZE;
106 	t->_next_window = NULL;
107 	t->_boundary = 0;
108 	t->_sgmap = NULL;
109 	t->_get_tag = lca_dma_get_tag;
110 	t->_dmamap_create = _bus_dmamap_create;
111 	t->_dmamap_destroy = _bus_dmamap_destroy;
112 	t->_dmamap_load = _bus_dmamap_load_direct;
113 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
114 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
115 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
116 	t->_dmamap_unload = _bus_dmamap_unload;
117 	t->_dmamap_sync = _bus_dmamap_sync;
118 
119 	t->_dmamem_alloc = _bus_dmamem_alloc;
120 	t->_dmamem_free = _bus_dmamem_free;
121 	t->_dmamem_map = _bus_dmamem_map;
122 	t->_dmamem_unmap = _bus_dmamem_unmap;
123 	t->_dmamem_mmap = _bus_dmamem_mmap;
124 
125 	/*
126 	 * Initialize the DMA tag used for sgmap-mapped DMA.
127 	 */
128 	t = &lcp->lc_dmat_sgmap;
129 	t->_cookie = lcp;
130 	t->_wbase = LCA_SGMAP_MAPPED_BASE;
131 	t->_wsize = LCA_SGMAP_MAPPED_SIZE;
132 	t->_next_window = NULL;
133 	t->_boundary = 0;
134 	t->_sgmap = &lcp->lc_sgmap;
135 	t->_pfthresh = LCA_SGMAP_PFTHRESH;
136 	t->_get_tag = lca_dma_get_tag;
137 	t->_dmamap_create = alpha_sgmap_dmamap_create;
138 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
139 	t->_dmamap_load = lca_bus_dmamap_load_sgmap;
140 	t->_dmamap_load_mbuf = lca_bus_dmamap_load_mbuf_sgmap;
141 	t->_dmamap_load_uio = lca_bus_dmamap_load_uio_sgmap;
142 	t->_dmamap_load_raw = lca_bus_dmamap_load_raw_sgmap;
143 	t->_dmamap_unload = lca_bus_dmamap_unload_sgmap;
144 	t->_dmamap_sync = _bus_dmamap_sync;
145 
146 	t->_dmamem_alloc = _bus_dmamem_alloc;
147 	t->_dmamem_free = _bus_dmamem_free;
148 	t->_dmamem_map = _bus_dmamem_map;
149 	t->_dmamem_unmap = _bus_dmamem_unmap;
150 	t->_dmamem_mmap = _bus_dmamem_mmap;
151 
152 	/* Initialize window 1 as a direct-mapped window. */
153 	REGVAL64(LCA_IOC_W_BASE1) = LCA_DIRECT_MAPPED_BASE | IOC_W_BASE_WEN;
154 	REGVAL64(LCA_IOC_W_MASK1) = IOC_W_MASK_1G;
155 	alpha_mb();
156 
157 	/* Disable window 0 while we set it up. */
158 	REGVAL64(LCA_IOC_W_BASE0) = 0;
159 	alpha_mb();
160 
161 	/*
162 	 * Initialize the SGMAP.
163 	 */
164 	alpha_sgmap_init(t, &lcp->lc_sgmap, "lca_sgmap",
165 	    LCA_SGMAP_MAPPED_BASE, 0, LCA_SGMAP_MAPPED_SIZE,
166 	    sizeof(uint64_t), NULL, 0);
167 
168 	/*
169 	 * Set up window 0 as an 8MB SGMAP-mapped window
170 	 * starting at 8MB.
171 	 */
172 	REGVAL64(LCA_IOC_W_BASE0) = LCA_SGMAP_MAPPED_BASE |
173 	    IOC_W_BASE_SG | IOC_W_BASE_WEN;
174 	REGVAL64(LCA_IOC_W_MASK0) = IOC_W_MASK_8M;
175 	alpha_mb();
176 
177 	if ((lcp->lc_sgmap.aps_ptpa & IOC_W_T_BASE) !=
178 	    lcp->lc_sgmap.aps_ptpa)
179 		panic("lca_dma_init: bad page table address");
180 	REGVAL64(LCA_IOC_W_T_BASE0) = lcp->lc_sgmap.aps_ptpa;
181 	alpha_mb();
182 
183 	/* Enable the scatter/gather TLB. */
184 	REGVAL64(LCA_IOC_TB_ENA) = IOC_TB_ENA_TEN;
185 	alpha_mb();
186 
187 	LCA_TLB_INVALIDATE();
188 }
189 
190 /*
191  * Return the bus dma tag to be used for the specified bus type.
192  * INTERNAL USE ONLY!
193  */
194 static bus_dma_tag_t
lca_dma_get_tag(bus_dma_tag_t t,alpha_bus_t bustype)195 lca_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
196 {
197 	struct lca_config *lcp = t->_cookie;
198 
199 	switch (bustype) {
200 	case ALPHA_BUS_PCI:
201 	case ALPHA_BUS_EISA:
202 		/*
203 		 * Systems with an LCA can only support 1G
204 		 * of memory, so we use the direct-mapped window
205 		 * on busses that have 32-bit DMA.
206 		 */
207 		return (&lcp->lc_dmat_direct);
208 
209 	case ALPHA_BUS_ISA:
210 		/*
211 		 * ISA doesn't have enough address bits to use
212 		 * the direct-mapped DMA window, so we must use
213 		 * SGMAPs.
214 		 */
215 		return (&lcp->lc_dmat_sgmap);
216 
217 	default:
218 		panic("lca_dma_get_tag: shouldn't be here, really...");
219 	}
220 }
221 
222 /*
223  * Load an LCA SGMAP-mapped DMA map with a linear buffer.
224  */
225 static int
lca_bus_dmamap_load_sgmap(bus_dma_tag_t t,bus_dmamap_t map,void * buf,bus_size_t buflen,struct proc * p,int flags)226 lca_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags)
227 {
228 	int error;
229 
230 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
231 	    t->_sgmap);
232 	if (error == 0)
233 		LCA_TLB_INVALIDATE();
234 
235 	return (error);
236 }
237 
238 /*
239  * Load an LCA SGMAP-mapped DMA map with an mbuf chain.
240  */
241 static int
lca_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t,bus_dmamap_t map,struct mbuf * m,int flags)242 lca_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags)
243 {
244 	int error;
245 
246 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
247 	if (error == 0)
248 		LCA_TLB_INVALIDATE();
249 
250 	return (error);
251 }
252 
253 /*
254  * Load an LCA SGMAP-mapped DMA map with a uio.
255  */
256 static int
lca_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t,bus_dmamap_t map,struct uio * uio,int flags)257 lca_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
258 {
259 	int error;
260 
261 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
262 	if (error == 0)
263 		LCA_TLB_INVALIDATE();
264 
265 	return (error);
266 }
267 
268 /*
269  * Load an LCA SGMAP-mapped DMA map with raw memory.
270  */
271 static int
lca_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)272 lca_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)
273 {
274 	int error;
275 
276 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
277 	    t->_sgmap);
278 	if (error == 0)
279 		LCA_TLB_INVALIDATE();
280 
281 	return (error);
282 }
283 
284 /*
285  * Unload an LCA DMA map.
286  */
287 static void
lca_bus_dmamap_unload_sgmap(bus_dma_tag_t t,bus_dmamap_t map)288 lca_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
289 {
290 
291 	/*
292 	 * Invalidate any SGMAP page table entries used by this
293 	 * mapping.
294 	 */
295 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
296 	LCA_TLB_INVALIDATE();
297 
298 	/*
299 	 * Do the generic bits of the unload.
300 	 */
301 	_bus_dmamap_unload_common(t, map);
302 }
303