1 /* $OpenBSD: lca_dma.c,v 1.10 2013/03/08 18:26:54 miod Exp $ */
2 /* $NetBSD: lca_dma.c,v 1.13 2000/06/29 08:58:47 mrg Exp $ */
3
4 /*-
5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * XXX - We should define this before including bus.h, but since other stuff
36 * pulls in bus.h we must do this here.
37 */
38 #define _ALPHA_BUS_DMA_PRIVATE
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45
46 #include <uvm/uvm_extern.h>
47
48 #include <machine/bus.h>
49
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 #include <alpha/pci/lcareg.h>
53 #include <alpha/pci/lcavar.h>
54
55 bus_dma_tag_t lca_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
56
57 int lca_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
58 bus_size_t, struct proc *, int);
59
60 int lca_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
61 struct mbuf *, int);
62
63 int lca_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
64 struct uio *, int);
65
66 int lca_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
67 bus_dma_segment_t *, int, bus_size_t, int);
68
69 void lca_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
70
71 /*
72 * Direct-mapped window: 1G at 1G
73 */
74 #define LCA_DIRECT_MAPPED_BASE (1*1024*1024*1024)
75 #define LCA_DIRECT_MAPPED_SIZE (1*1024*1024*1024)
76
77 /*
78 * SGMAP window: 8M at 8M
79 */
80 #define LCA_SGMAP_MAPPED_BASE (8*1024*1024)
81 #define LCA_SGMAP_MAPPED_SIZE (8*1024*1024)
82
83 /*
84 * Macro to flush LCA scatter/gather TLB.
85 */
86 #define LCA_TLB_INVALIDATE() \
87 do { \
88 alpha_mb(); \
89 REGVAL64(LCA_IOC_TBIA) = 0; \
90 alpha_mb(); \
91 } while (0)
92
93 void
lca_dma_init(lcp)94 lca_dma_init(lcp)
95 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->_get_tag = lca_dma_get_tag;
136 t->_dmamap_create = alpha_sgmap_dmamap_create;
137
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(u_int64_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 /* XXX XXX BEGIN XXX XXX */
190 { /* XXX */
191 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
192 alpha_XXX_dmamap_or = LCA_DIRECT_MAPPED_BASE; /* XXX */
193 } /* XXX */
194 /* XXX XXX END XXX XXX */
195 }
196
197 /*
198 * Return the bus dma tag to be used for the specified bus type.
199 * INTERNAL USE ONLY!
200 */
201 bus_dma_tag_t
lca_dma_get_tag(t,bustype)202 lca_dma_get_tag(t, bustype)
203 bus_dma_tag_t t;
204 alpha_bus_t bustype;
205 {
206 struct lca_config *lcp = t->_cookie;
207
208 switch (bustype) {
209 case ALPHA_BUS_PCI:
210 case ALPHA_BUS_EISA:
211 /*
212 * Systems with an LCA can only support 1G
213 * of memory, so we use the direct-mapped window
214 * on busses that have 32-bit DMA.
215 */
216 return (&lcp->lc_dmat_direct);
217
218 case ALPHA_BUS_ISA:
219 /*
220 * ISA doesn't have enough address bits to use
221 * the direct-mapped DMA window, so we must use
222 * SGMAPs.
223 */
224 return (&lcp->lc_dmat_sgmap);
225
226 default:
227 panic("lca_dma_get_tag: shouldn't be here, really...");
228 }
229 }
230
231 /*
232 * Load an LCA SGMAP-mapped DMA map with a linear buffer.
233 */
234 int
lca_bus_dmamap_load_sgmap(t,map,buf,buflen,p,flags)235 lca_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
236 bus_dma_tag_t t;
237 bus_dmamap_t map;
238 void *buf;
239 bus_size_t buflen;
240 struct proc *p;
241 int flags;
242 {
243 int error;
244
245 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
246 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 an mbuf chain.
255 */
256 int
lca_bus_dmamap_load_mbuf_sgmap(t,map,m,flags)257 lca_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
258 bus_dma_tag_t t;
259 bus_dmamap_t map;
260 struct mbuf *m;
261 int flags;
262 {
263 int error;
264
265 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
266 if (error == 0)
267 LCA_TLB_INVALIDATE();
268
269 return (error);
270 }
271
272 /*
273 * Load an LCA SGMAP-mapped DMA map with a uio.
274 */
275 int
lca_bus_dmamap_load_uio_sgmap(t,map,uio,flags)276 lca_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
277 bus_dma_tag_t t;
278 bus_dmamap_t map;
279 struct uio *uio;
280 int flags;
281 {
282 int error;
283
284 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
285 if (error == 0)
286 LCA_TLB_INVALIDATE();
287
288 return (error);
289 }
290
291 /*
292 * Load an LCA SGMAP-mapped DMA map with raw memory.
293 */
294 int
lca_bus_dmamap_load_raw_sgmap(t,map,segs,nsegs,size,flags)295 lca_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
296 bus_dma_tag_t t;
297 bus_dmamap_t map;
298 bus_dma_segment_t *segs;
299 int nsegs;
300 bus_size_t size;
301 int flags;
302 {
303 int error;
304
305 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
306 t->_sgmap);
307 if (error == 0)
308 LCA_TLB_INVALIDATE();
309
310 return (error);
311 }
312
313 /*
314 * Unload an LCA DMA map.
315 */
316 void
lca_bus_dmamap_unload_sgmap(t,map)317 lca_bus_dmamap_unload_sgmap(t, map)
318 bus_dma_tag_t t;
319 bus_dmamap_t map;
320 {
321
322 /*
323 * Invalidate any SGMAP page table entries used by this
324 * mapping.
325 */
326 pci_sgmap_pte64_unload(t, map, t->_sgmap);
327 LCA_TLB_INVALIDATE();
328
329 /*
330 * Do the generic bits of the unload.
331 */
332 _bus_dmamap_unload(t, map);
333 }
334