tsp_dma.c revision 1.18 1 /* $NetBSD: tsp_dma.c,v 1.18 2021/07/04 22:42:36 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 2021 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 /*-
34 * Copyright (c) 1999 by Ross Harvey. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by Ross Harvey.
47 * 4. The name of Ross Harvey may not be used to endorse or promote products
48 * derived from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
51 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
53 * ARE DISCLAIMED. IN NO EVENT SHALL ROSS HARVEY BE LIABLE FOR ANY
54 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: tsp_dma.c,v 1.18 2021/07/04 22:42:36 thorpej Exp $");
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/device.h>
70
71 #include <machine/autoconf.h>
72 #define _ALPHA_BUS_DMA_PRIVATE
73 #include <sys/bus.h>
74 #include <machine/rpb.h>
75
76 #include <dev/pci/pcireg.h>
77 #include <dev/pci/pcivar.h>
78 #include <alpha/pci/tsreg.h>
79 #include <alpha/pci/tsvar.h>
80
81 #define tsp_dma() { Generate ctags(1) key. }
82
83 #define EDIFF(a, b) (((a) | WSBA_ENA | WSBA_SG) != ((b) | WSBA_ENA | WSBA_SG))
84
85 static bus_dma_tag_t tsp_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
86
87 static int tsp_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
88 bus_size_t, struct proc *, int);
89
90 static int tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
91 struct mbuf *, int);
92
93 static int tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
94 struct uio *, int);
95
96 static 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
99 static void tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
100
101 static void tsp_tlb_invalidate(struct tsp_config *);
102
103 /*
104 * XXX Need to figure out what this is, if any. Initialize it to
105 * XXX something that should be safe.
106 */
107 #define TSP_SGMAP_PFTHRESH 256
108
109 /*
110 * Quoting the 21272 programmer's reference manual:
111 *
112 * <quote>
113 * 10.1.4.4 Monster Window DMA Address Translation
114 *
115 * In case of a PCI dual-address cycle command, the high-order PCI address
116 * bits <63:40> are compared to the constant value 0x0000_01 (that is, bit
117 * <40> = 1; all other bits = 0). If these bits match, a monster window hit
118 * has occurred and the low-order PCI address bits <34:0> are used unchanged
119 * as the system address bits <34:0>. PCI address bits <39:35> are ignored.
120 * The high-order 32 PCI address bits are available on b_ad<31:0> in the
121 * second cycle of a DAC, and also on b_ad<63:32> in the first cycle of a
122 * DAC if b_req64_l is asserted.
123 * </quote>
124 *
125 * This means that we can address up to 32GB of RAM using a direct-mapped
126 * 64-bit DMA tag. This leaves us possibly having to fall back on SGMAP
127 * DMA on a Titan system (those support up to 64GB of RAM), and we may have
128 * to address that with an additional large SGMAP DAC window at another
129 * time.
130 */
131 #define TSP_MONSTER_DMA_WINDOW_BASE 0x100##00000000UL
132 #define TSP_MONSTER_DMA_WINDOW_SIZE 0x008##00000000UL
133
134 void
135 tsp_dma_init(struct tsp_config *pcp)
136 {
137 int i;
138 bus_dma_tag_t t;
139 struct ts_pchip *pccsr = pcp->pc_csr;
140 bus_addr_t dwbase, dwlen, sgwbase, sgwlen, tbase;
141 static struct map_expected {
142 uint32_t base, mask, enables;
143 } premap[4] = {
144 { 0x00800000, 0x00700000, WSBA_ENA | WSBA_SG },
145 { 0x80000000, 0x3ff00000, WSBA_ENA },
146 { 0, 0, 0 },
147 { 0, 0, 0 }
148 };
149
150 alpha_mb();
151 for(i = 0; i < 4; ++i) {
152 if (EDIFF(pccsr->tsp_wsba[i].tsg_r, premap[i].base) ||
153 EDIFF(pccsr->tsp_wsm[i].tsg_r, premap[i].mask))
154 printf("tsp%d: window %d: %lx/base %lx/mask %lx"
155 " reinitialized\n",
156 pcp->pc_pslot, i,
157 pccsr->tsp_wsba[i].tsg_r,
158 pccsr->tsp_wsm[i].tsg_r,
159 pccsr->tsp_tba[i].tsg_r);
160 pccsr->tsp_wsba[i].tsg_r = premap[i].base | premap[i].enables;
161 pccsr->tsp_wsm[i].tsg_r = premap[i].mask;
162 }
163
164 /* Ensure the Monster Window is enabled. */
165 pccsr->tsp_pctl.tsg_r |= PCTL_MWIN;
166 alpha_mb();
167
168 /*
169 * Initialize the DMA tag used for direct-mapped 64-bit DMA.
170 */
171 t = &pcp->pc_dmat64_direct;
172 t->_cookie = pcp;
173 t->_wbase = TSP_MONSTER_DMA_WINDOW_BASE;
174 t->_wsize = TSP_MONSTER_DMA_WINDOW_SIZE;
175 t->_next_window = &pcp->pc_dmat_sgmap;
176 t->_boundary = 0;
177 t->_sgmap = NULL;
178 t->_get_tag = tsp_dma_get_tag;
179 t->_dmamap_create = _bus_dmamap_create;
180 t->_dmamap_destroy = _bus_dmamap_destroy;
181 t->_dmamap_load = _bus_dmamap_load_direct;
182 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
183 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
184 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
185 t->_dmamap_unload = _bus_dmamap_unload;
186 t->_dmamap_sync = _bus_dmamap_sync;
187
188 t->_dmamem_alloc = _bus_dmamem_alloc;
189 t->_dmamem_free = _bus_dmamem_free;
190 t->_dmamem_map = _bus_dmamem_map;
191 t->_dmamem_unmap = _bus_dmamem_unmap;
192 t->_dmamem_mmap = _bus_dmamem_mmap;
193
194 /*
195 * Initialize the DMA tag used for direct-mapped DMA.
196 */
197 t = &pcp->pc_dmat_direct;
198 t->_cookie = pcp;
199 t->_wbase = dwbase = WSBA_ADDR(pccsr->tsp_wsba[1].tsg_r);
200 t->_wsize = dwlen = WSM_LEN(pccsr->tsp_wsm[1].tsg_r);
201 t->_next_window = &pcp->pc_dmat_sgmap;
202 t->_boundary = 0;
203 t->_sgmap = NULL;
204 t->_get_tag = tsp_dma_get_tag;
205 t->_dmamap_create = _bus_dmamap_create;
206 t->_dmamap_destroy = _bus_dmamap_destroy;
207 t->_dmamap_load = _bus_dmamap_load_direct;
208 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
209 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
210 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
211 t->_dmamap_unload = _bus_dmamap_unload;
212 t->_dmamap_sync = _bus_dmamap_sync;
213
214 t->_dmamem_alloc = _bus_dmamem_alloc;
215 t->_dmamem_free = _bus_dmamem_free;
216 t->_dmamem_map = _bus_dmamem_map;
217 t->_dmamem_unmap = _bus_dmamem_unmap;
218 t->_dmamem_mmap = _bus_dmamem_mmap;
219
220 /*
221 * Initialize the DMA tag used for sgmap-mapped DMA.
222 */
223 t = &pcp->pc_dmat_sgmap;
224 t->_cookie = pcp;
225 t->_wbase = sgwbase = WSBA_ADDR(pccsr->tsp_wsba[0].tsg_r);
226 t->_wsize = sgwlen = WSM_LEN(pccsr->tsp_wsm[0].tsg_r);
227 t->_next_window = NULL;
228 t->_boundary = 0;
229 t->_sgmap = &pcp->pc_sgmap;
230 t->_pfthresh = TSP_SGMAP_PFTHRESH;
231 t->_get_tag = tsp_dma_get_tag;
232 t->_dmamap_create = alpha_sgmap_dmamap_create;
233 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
234 t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
235 t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
236 t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
237 t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
238 t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
239 t->_dmamap_sync = _bus_dmamap_sync;
240
241 t->_dmamem_alloc = _bus_dmamem_alloc;
242 t->_dmamem_free = _bus_dmamem_free;
243 t->_dmamem_map = _bus_dmamem_map;
244 t->_dmamem_unmap = _bus_dmamem_unmap;
245 t->_dmamem_mmap = _bus_dmamem_mmap;
246
247 /*
248 * Initialize the SGMAP. Align page table to 32k in case
249 * window is somewhat larger than expected.
250 */
251 alpha_sgmap_init(t, &pcp->pc_sgmap, "tsp_sgmap",
252 sgwbase, 0, sgwlen, sizeof(uint64_t), NULL, (32*1024));
253
254 /*
255 * Enable window 0 and enable SG PTE mapping.
256 */
257 alpha_mb();
258 pccsr->tsp_wsba[0].tsg_r |= WSBA_SG | WSBA_ENA;
259 alpha_mb();
260
261 /*
262 * Enable window 1 in direct mode.
263 */
264 alpha_mb();
265 pccsr->tsp_wsba[1].tsg_r =
266 (pccsr->tsp_wsba[1].tsg_r & ~WSBA_SG) | WSBA_ENA;
267 alpha_mb();
268
269 /*
270 * Check windows for sanity, especially if we later decide to
271 * use the firmware's initialization in some cases.
272 */
273 if ((sgwbase <= dwbase && dwbase < sgwbase + sgwlen) ||
274 (dwbase <= sgwbase && sgwbase < dwbase + dwlen))
275 panic("tsp_dma_init: overlap");
276
277 tbase = pcp->pc_sgmap.aps_ptpa;
278 if (tbase & ~0x7fffffc00UL)
279 panic("tsp_dma_init: bad page table address");
280 alpha_mb();
281 pccsr->tsp_tba[0].tsg_r = tbase;
282 alpha_mb();
283
284 tsp_tlb_invalidate(pcp);
285 alpha_mb();
286 }
287
288 /*
289 * Return the bus dma tag to be used for the specified bus type.
290 * INTERNAL USE ONLY!
291 */
292 static bus_dma_tag_t
293 tsp_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
294 {
295 struct tsp_config *pcp = t->_cookie;
296
297 switch (bustype) {
298 case ALPHA_BUS_PCI:
299 case ALPHA_BUS_EISA:
300 /*
301 * The direct mapped window will work for most systems,
302 * most of the time. When it doesn't, we chain to the sgmap
303 * window automatically.
304 */
305 return (&pcp->pc_dmat_direct);
306
307 case ALPHA_BUS_ISA:
308 /*
309 * ISA doesn't have enough address bits to use
310 * the direct-mapped DMA window, so we must use
311 * SGMAPs.
312 */
313 return (&pcp->pc_dmat_sgmap);
314
315 default:
316 panic("tsp_dma_get_tag: shouldn't be here, really...");
317 }
318 }
319
320 /*
321 * Load a TSP SGMAP-mapped DMA map with a linear buffer.
322 */
323 static int
324 tsp_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags)
325 {
326 int error;
327
328 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
329 t->_sgmap);
330 if (error == 0)
331 tsp_tlb_invalidate(t->_cookie);
332
333 return (error);
334 }
335
336 /*
337 * Load a TSP SGMAP-mapped DMA map with an mbuf chain.
338 */
339 static int
340 tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags)
341 {
342 int error;
343
344 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
345 if (error == 0)
346 tsp_tlb_invalidate(t->_cookie);
347
348 return (error);
349 }
350
351 /*
352 * Load a TSP SGMAP-mapped DMA map with a uio.
353 */
354 static int
355 tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
356 {
357 int error;
358
359 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
360 if (error == 0)
361 tsp_tlb_invalidate(t->_cookie);
362
363 return (error);
364 }
365
366 /*
367 * Load a TSP SGMAP-mapped DMA map with raw memory.
368 */
369 static int
370 tsp_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)
371 {
372 int error;
373
374 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
375 t->_sgmap);
376 if (error == 0)
377 tsp_tlb_invalidate(t->_cookie);
378
379 return (error);
380 }
381
382 /*
383 * Unload a TSP DMA map.
384 */
385 static void
386 tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
387 {
388
389 /*
390 * Invalidate any SGMAP page table entries used by this
391 * mapping.
392 */
393 pci_sgmap_pte64_unload(t, map, t->_sgmap);
394 tsp_tlb_invalidate(t->_cookie);
395
396 /*
397 * Do the generic bits of the unload.
398 */
399 _bus_dmamap_unload_common(t, map);
400 }
401
402 /*
403 * Flush the TSP scatter/gather TLB.
404 */
405 static void
406 tsp_tlb_invalidate(struct tsp_config *pcp)
407 {
408
409 alpha_mb();
410 *pcp->pc_tlbia = 0;
411 alpha_mb();
412 }
413