iommu.c revision 1.3.2.1 1 1.3.2.1 bouyer /* $NetBSD: iommu.c,v 1.3.2.1 2000/11/20 20:26:43 bouyer Exp $ */
2 1.3.2.1 bouyer
3 1.3.2.1 bouyer /*
4 1.3.2.1 bouyer * Copyright (c) 1999, 2000 Matthew R. Green
5 1.3.2.1 bouyer * All rights reserved.
6 1.3.2.1 bouyer *
7 1.3.2.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.3.2.1 bouyer * modification, are permitted provided that the following conditions
9 1.3.2.1 bouyer * are met:
10 1.3.2.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.3.2.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.3.2.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.2.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.3.2.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.3.2.1 bouyer * 3. The name of the author may not be used to endorse or promote products
16 1.3.2.1 bouyer * derived from this software without specific prior written permission.
17 1.3.2.1 bouyer *
18 1.3.2.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.3.2.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.3.2.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.3.2.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.3.2.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.3.2.1 bouyer * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.3.2.1 bouyer * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.3.2.1 bouyer * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.3.2.1 bouyer * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.3.2.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.3.2.1 bouyer * SUCH DAMAGE.
29 1.3.2.1 bouyer */
30 1.1 mrg
31 1.1 mrg /*-
32 1.1 mrg * Copyright (c) 1998 The NetBSD Foundation, Inc.
33 1.1 mrg * All rights reserved.
34 1.1 mrg *
35 1.1 mrg * This code is derived from software contributed to The NetBSD Foundation
36 1.1 mrg * by Paul Kranenburg.
37 1.1 mrg *
38 1.1 mrg * Redistribution and use in source and binary forms, with or without
39 1.1 mrg * modification, are permitted provided that the following conditions
40 1.1 mrg * are met:
41 1.1 mrg * 1. Redistributions of source code must retain the above copyright
42 1.1 mrg * notice, this list of conditions and the following disclaimer.
43 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
44 1.1 mrg * notice, this list of conditions and the following disclaimer in the
45 1.1 mrg * documentation and/or other materials provided with the distribution.
46 1.1 mrg * 3. All advertising materials mentioning features or use of this software
47 1.1 mrg * must display the following acknowledgement:
48 1.1 mrg * This product includes software developed by the NetBSD
49 1.1 mrg * Foundation, Inc. and its contributors.
50 1.1 mrg * 4. Neither the name of The NetBSD Foundation nor the names of its
51 1.1 mrg * contributors may be used to endorse or promote products derived
52 1.1 mrg * from this software without specific prior written permission.
53 1.1 mrg *
54 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55 1.1 mrg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 1.1 mrg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 1.1 mrg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58 1.1 mrg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 1.1 mrg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 1.1 mrg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 1.1 mrg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 1.1 mrg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 1.1 mrg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 1.1 mrg * POSSIBILITY OF SUCH DAMAGE.
65 1.1 mrg */
66 1.1 mrg
67 1.1 mrg /*
68 1.1 mrg * Copyright (c) 1992, 1993
69 1.1 mrg * The Regents of the University of California. All rights reserved.
70 1.1 mrg *
71 1.1 mrg * This software was developed by the Computer Systems Engineering group
72 1.1 mrg * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
73 1.1 mrg * contributed to Berkeley.
74 1.1 mrg *
75 1.1 mrg * All advertising materials mentioning features or use of this software
76 1.1 mrg * must display the following acknowledgement:
77 1.1 mrg * This product includes software developed by the University of
78 1.1 mrg * California, Lawrence Berkeley Laboratory.
79 1.1 mrg *
80 1.1 mrg * Redistribution and use in source and binary forms, with or without
81 1.1 mrg * modification, are permitted provided that the following conditions
82 1.1 mrg * are met:
83 1.1 mrg * 1. Redistributions of source code must retain the above copyright
84 1.1 mrg * notice, this list of conditions and the following disclaimer.
85 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
86 1.1 mrg * notice, this list of conditions and the following disclaimer in the
87 1.1 mrg * documentation and/or other materials provided with the distribution.
88 1.1 mrg * 3. All advertising materials mentioning features or use of this software
89 1.1 mrg * must display the following acknowledgement:
90 1.1 mrg * This product includes software developed by the University of
91 1.1 mrg * California, Berkeley and its contributors.
92 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
93 1.1 mrg * may be used to endorse or promote products derived from this software
94 1.1 mrg * without specific prior written permission.
95 1.1 mrg *
96 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
97 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
98 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
99 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
100 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
101 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
102 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
103 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
104 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
105 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
106 1.1 mrg * SUCH DAMAGE.
107 1.1 mrg *
108 1.1 mrg * from: NetBSD: sbus.c,v 1.13 1999/05/23 07:24:02 mrg Exp
109 1.1 mrg * from: @(#)sbus.c 8.1 (Berkeley) 6/11/93
110 1.1 mrg */
111 1.1 mrg
112 1.3.2.1 bouyer /*
113 1.3.2.1 bouyer * UltraSPARC IOMMU support; used by both the sbus and pci code.
114 1.3.2.1 bouyer */
115 1.3.2.1 bouyer #include "opt_ddb.h"
116 1.3.2.1 bouyer
117 1.1 mrg #include <sys/param.h>
118 1.1 mrg #include <sys/extent.h>
119 1.1 mrg #include <sys/malloc.h>
120 1.1 mrg #include <sys/systm.h>
121 1.1 mrg #include <sys/device.h>
122 1.3.2.1 bouyer
123 1.3.2.1 bouyer #include <uvm/uvm_extern.h>
124 1.1 mrg
125 1.1 mrg #include <machine/bus.h>
126 1.3.2.1 bouyer #include <sparc64/sparc64/cache.h>
127 1.1 mrg #include <sparc64/dev/iommureg.h>
128 1.1 mrg #include <sparc64/dev/iommuvar.h>
129 1.1 mrg
130 1.1 mrg #include <machine/autoconf.h>
131 1.1 mrg #include <machine/cpu.h>
132 1.1 mrg
133 1.1 mrg #ifdef DEBUG
134 1.3.2.1 bouyer #define IDB_BUSDMA 0x1
135 1.3.2.1 bouyer #define IDB_IOMMU 0x2
136 1.3.2.1 bouyer #define IDB_INFO 0x4
137 1.3.2.1 bouyer int iommudebug = 0x0;
138 1.3.2.1 bouyer #define DPRINTF(l, s) do { if (iommudebug & l) printf s; } while (0)
139 1.3.2.1 bouyer #else
140 1.3.2.1 bouyer #define DPRINTF(l, s)
141 1.1 mrg #endif
142 1.1 mrg
143 1.3.2.1 bouyer static int iommu_strbuf_flush __P((struct iommu_state *));
144 1.3.2.1 bouyer
145 1.1 mrg /*
146 1.1 mrg * initialise the UltraSPARC IOMMU (SBUS or PCI):
147 1.1 mrg * - allocate and setup the iotsb.
148 1.1 mrg * - enable the IOMMU
149 1.3.2.1 bouyer * - initialise the streaming buffers (if they exist)
150 1.1 mrg * - create a private DVMA map.
151 1.1 mrg */
152 1.1 mrg void
153 1.1 mrg iommu_init(name, is, tsbsize)
154 1.1 mrg char *name;
155 1.1 mrg struct iommu_state *is;
156 1.1 mrg int tsbsize;
157 1.1 mrg {
158 1.3.2.1 bouyer psize_t size;
159 1.3.2.1 bouyer vaddr_t va;
160 1.3.2.1 bouyer paddr_t pa;
161 1.3.2.1 bouyer vm_page_t m;
162 1.3.2.1 bouyer struct pglist mlist;
163 1.1 mrg
164 1.1 mrg /*
165 1.1 mrg * Setup the iommu.
166 1.1 mrg *
167 1.3.2.1 bouyer * The sun4u iommu is part of the SBUS or PCI controller so we
168 1.3.2.1 bouyer * will deal with it here..
169 1.2 eeh *
170 1.2 eeh * The IOMMU address space always ends at 0xffffe000, but the starting
171 1.2 eeh * address depends on the size of the map. The map size is 1024 * 2 ^
172 1.2 eeh * is->is_tsbsize entries, where each entry is 8 bytes. The start of
173 1.2 eeh * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
174 1.2 eeh *
175 1.2 eeh * Note: the stupid IOMMU ignores the high bits of an address, so a
176 1.2 eeh * NULL DMA pointer will be translated by the first page of the IOTSB.
177 1.2 eeh * To trap bugs we'll skip the first entry in the IOTSB.
178 1.2 eeh */
179 1.3.2.1 bouyer is->is_cr = (tsbsize << 16) | IOMMUCR_EN;
180 1.1 mrg is->is_tsbsize = tsbsize;
181 1.3.2.1 bouyer is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize) + NBPG;
182 1.3.2.1 bouyer
183 1.3.2.1 bouyer /*
184 1.3.2.1 bouyer * Allocate memory for I/O pagetables. They need to be physically
185 1.3.2.1 bouyer * contiguous.
186 1.3.2.1 bouyer */
187 1.3.2.1 bouyer
188 1.3.2.1 bouyer size = NBPG<<(is->is_tsbsize);
189 1.3.2.1 bouyer TAILQ_INIT(&mlist);
190 1.3.2.1 bouyer if (uvm_pglistalloc((psize_t)size, (paddr_t)0, (paddr_t)-1,
191 1.3.2.1 bouyer (paddr_t)NBPG, (paddr_t)0, &mlist, 1, 0) != 0)
192 1.3.2.1 bouyer panic("iommu_init: no memory");
193 1.3.2.1 bouyer
194 1.3.2.1 bouyer va = uvm_km_valloc(kernel_map, size);
195 1.3.2.1 bouyer if (va == 0)
196 1.3.2.1 bouyer panic("iommu_init: no memory");
197 1.3.2.1 bouyer is->is_tsb = (int64_t *)va;
198 1.3.2.1 bouyer
199 1.3.2.1 bouyer m = TAILQ_FIRST(&mlist);
200 1.3.2.1 bouyer is->is_ptsb = VM_PAGE_TO_PHYS(m);
201 1.3.2.1 bouyer
202 1.3.2.1 bouyer /* Map the pages */
203 1.3.2.1 bouyer for (; m != NULL; m = TAILQ_NEXT(m,pageq)) {
204 1.3.2.1 bouyer pa = VM_PAGE_TO_PHYS(m);
205 1.3.2.1 bouyer pmap_enter(pmap_kernel(), va, pa | PMAP_NVC,
206 1.3.2.1 bouyer VM_PROT_READ|VM_PROT_WRITE,
207 1.3.2.1 bouyer VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
208 1.3.2.1 bouyer va += NBPG;
209 1.3.2.1 bouyer }
210 1.3.2.1 bouyer bzero(is->is_tsb, size);
211 1.1 mrg
212 1.1 mrg #ifdef DEBUG
213 1.3.2.1 bouyer if (iommudebug & IDB_INFO)
214 1.1 mrg {
215 1.1 mrg /* Probe the iommu */
216 1.1 mrg struct iommureg *regs = is->is_iommu;
217 1.1 mrg
218 1.1 mrg printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n", ®s->iommu_cr,
219 1.1 mrg ®s->iommu_tsb, ®s->iommu_flush);
220 1.3.2.1 bouyer printf("iommu cr=%qx tsb=%qx\n", regs->iommu_cr, regs->iommu_tsb);
221 1.3.2.1 bouyer printf("TSB base %p phys %qx\n", (void *)is->is_tsb, (u_int64_t)is->is_ptsb);
222 1.1 mrg delay(1000000); /* 1 s */
223 1.1 mrg }
224 1.1 mrg #endif
225 1.1 mrg
226 1.1 mrg /*
227 1.3.2.1 bouyer * Initialize streaming buffer, if it is there.
228 1.1 mrg */
229 1.3.2.1 bouyer if (is->is_sb)
230 1.3.2.1 bouyer (void)pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush,
231 1.3.2.1 bouyer (paddr_t *)&is->is_flushpa);
232 1.1 mrg
233 1.1 mrg /*
234 1.1 mrg * now actually start up the IOMMU
235 1.1 mrg */
236 1.1 mrg iommu_reset(is);
237 1.1 mrg
238 1.1 mrg /*
239 1.1 mrg * Now all the hardware's working we need to allocate a dvma map.
240 1.1 mrg */
241 1.3.2.1 bouyer printf("DVMA map: %x to %x\n",
242 1.3.2.1 bouyer (unsigned int)is->is_dvmabase,
243 1.3.2.1 bouyer (unsigned int)IOTSB_VEND);
244 1.1 mrg is->is_dvmamap = extent_create(name,
245 1.3.2.1 bouyer is->is_dvmabase, (u_long)IOTSB_VEND,
246 1.1 mrg M_DEVBUF, 0, 0, EX_NOWAIT);
247 1.1 mrg }
248 1.1 mrg
249 1.3.2.1 bouyer /*
250 1.3.2.1 bouyer * Streaming buffers don't exist on the UltraSPARC IIi; we should have
251 1.3.2.1 bouyer * detected that already and disabled them. If not, we will notice that
252 1.3.2.1 bouyer * they aren't there when the STRBUF_EN bit does not remain.
253 1.3.2.1 bouyer */
254 1.1 mrg void
255 1.1 mrg iommu_reset(is)
256 1.1 mrg struct iommu_state *is;
257 1.1 mrg {
258 1.1 mrg
259 1.1 mrg /* Need to do 64-bit stores */
260 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
261 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_iommu->iommu_tsb,
262 1.3.2.1 bouyer 0, is->is_ptsb);
263 1.3.2.1 bouyer /* Enable IOMMU in diagnostic mode */
264 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
265 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_iommu->iommu_cr, 0,
266 1.3.2.1 bouyer is->is_cr|IOMMUCR_DE);
267 1.3.2.1 bouyer
268 1.3.2.1 bouyer
269 1.3.2.1 bouyer if (!is->is_sb)
270 1.3.2.1 bouyer return;
271 1.3.2.1 bouyer
272 1.1 mrg /* Enable diagnostics mode? */
273 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
274 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_sb->strbuf_ctl,
275 1.3.2.1 bouyer 0, STRBUF_EN);
276 1.3.2.1 bouyer
277 1.3.2.1 bouyer /* No streaming buffers? Disable them */
278 1.3.2.1 bouyer if (bus_space_read_8(is->is_bustag,
279 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_sb->strbuf_ctl,
280 1.3.2.1 bouyer 0) == 0)
281 1.3.2.1 bouyer is->is_sb = 0;
282 1.2 eeh }
283 1.2 eeh
284 1.2 eeh /*
285 1.2 eeh * Here are the iommu control routines.
286 1.2 eeh */
287 1.2 eeh void
288 1.2 eeh iommu_enter(is, va, pa, flags)
289 1.2 eeh struct iommu_state *is;
290 1.2 eeh vaddr_t va;
291 1.2 eeh int64_t pa;
292 1.2 eeh int flags;
293 1.2 eeh {
294 1.2 eeh int64_t tte;
295 1.2 eeh
296 1.2 eeh #ifdef DIAGNOSTIC
297 1.2 eeh if (va < is->is_dvmabase)
298 1.3.2.1 bouyer panic("iommu_enter: va %#lx not in DVMA space", va);
299 1.2 eeh #endif
300 1.2 eeh
301 1.2 eeh tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
302 1.2 eeh !(flags&BUS_DMA_COHERENT));
303 1.2 eeh
304 1.2 eeh /* Is the streamcache flush really needed? */
305 1.3.2.1 bouyer if (is->is_sb) {
306 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
307 1.3.2.1 bouyer &is->is_sb->strbuf_pgflush, 0, va);
308 1.3.2.1 bouyer iommu_strbuf_flush(is);
309 1.3.2.1 bouyer }
310 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
311 1.3.2.1 bouyer (int)IOTSBSLOT(va,is->is_tsbsize), va));
312 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
313 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
314 1.3.2.1 bouyer &is->is_iommu->iommu_flush, 0, va);
315 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
316 1.2 eeh va, (long)pa, IOTSBSLOT(va,is->is_tsbsize),
317 1.2 eeh &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
318 1.3.2.1 bouyer (long)tte));
319 1.2 eeh }
320 1.2 eeh
321 1.2 eeh /*
322 1.2 eeh * iommu_remove: removes mappings created by iommu_enter
323 1.2 eeh *
324 1.2 eeh * Only demap from IOMMU if flag is set.
325 1.3.2.1 bouyer *
326 1.3.2.1 bouyer * XXX: this function needs better internal error checking.
327 1.2 eeh */
328 1.2 eeh void
329 1.2 eeh iommu_remove(is, va, len)
330 1.2 eeh struct iommu_state *is;
331 1.2 eeh vaddr_t va;
332 1.2 eeh size_t len;
333 1.2 eeh {
334 1.2 eeh
335 1.2 eeh #ifdef DIAGNOSTIC
336 1.2 eeh if (va < is->is_dvmabase)
337 1.3.2.1 bouyer panic("iommu_remove: va 0x%lx not in DVMA space", (long)va);
338 1.2 eeh if ((long)(va + len) < (long)va)
339 1.3.2.1 bouyer panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
340 1.2 eeh (long) va, (long) len);
341 1.2 eeh if (len & ~0xfffffff)
342 1.3.2.1 bouyer panic("iommu_remove: rediculous len 0x%lx", (long)len);
343 1.2 eeh #endif
344 1.2 eeh
345 1.2 eeh va = trunc_page(va);
346 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
347 1.3.2.1 bouyer va, IOTSBSLOT(va,is->is_tsbsize),
348 1.3.2.1 bouyer &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]));
349 1.2 eeh while (len > 0) {
350 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d for va %p size %lx\n",
351 1.3.2.1 bouyer (int)IOTSBSLOT(va,is->is_tsbsize), va, (u_long)len));
352 1.3.2.1 bouyer if (is->is_sb) {
353 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
354 1.2 eeh (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
355 1.2 eeh (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
356 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
357 1.3.2.1 bouyer (u_long)len));
358 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
359 1.3.2.1 bouyer &is->is_sb->strbuf_pgflush, 0, va);
360 1.3.2.1 bouyer if (len <= NBPG)
361 1.3.2.1 bouyer iommu_strbuf_flush(is);
362 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
363 1.2 eeh (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
364 1.2 eeh (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
365 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
366 1.3.2.1 bouyer (u_long)len));
367 1.3.2.1 bouyer } else
368 1.3.2.1 bouyer membar_sync(); /* XXX */
369 1.3.2.1 bouyer
370 1.3.2.1 bouyer if (len <= NBPG)
371 1.3.2.1 bouyer len = 0;
372 1.3.2.1 bouyer else
373 1.3.2.1 bouyer len -= NBPG;
374 1.3.2.1 bouyer
375 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = 0;
376 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
377 1.3.2.1 bouyer &is->is_iommu->iommu_flush, 0, va);
378 1.2 eeh va += NBPG;
379 1.2 eeh }
380 1.2 eeh }
381 1.2 eeh
382 1.3.2.1 bouyer static int
383 1.3.2.1 bouyer iommu_strbuf_flush(is)
384 1.2 eeh struct iommu_state *is;
385 1.2 eeh {
386 1.2 eeh struct timeval cur, flushtimeout;
387 1.2 eeh
388 1.2 eeh #define BUMPTIME(t, usec) { \
389 1.2 eeh register volatile struct timeval *tp = (t); \
390 1.2 eeh register long us; \
391 1.2 eeh \
392 1.2 eeh tp->tv_usec = us = tp->tv_usec + (usec); \
393 1.2 eeh if (us >= 1000000) { \
394 1.2 eeh tp->tv_usec = us - 1000000; \
395 1.2 eeh tp->tv_sec++; \
396 1.2 eeh } \
397 1.2 eeh }
398 1.2 eeh
399 1.3.2.1 bouyer if (!is->is_sb)
400 1.3.2.1 bouyer return (0);
401 1.3.2.1 bouyer
402 1.3.2.1 bouyer /*
403 1.3.2.1 bouyer * Streaming buffer flushes:
404 1.3.2.1 bouyer *
405 1.3.2.1 bouyer * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
406 1.3.2.1 bouyer * we're not on a cache line boundary (64-bits):
407 1.3.2.1 bouyer * 2 Store 0 in flag
408 1.3.2.1 bouyer * 3 Store pointer to flag in flushsync
409 1.3.2.1 bouyer * 4 wait till flushsync becomes 0x1
410 1.3.2.1 bouyer *
411 1.3.2.1 bouyer * If it takes more than .5 sec, something
412 1.3.2.1 bouyer * went wrong.
413 1.3.2.1 bouyer */
414 1.3.2.1 bouyer
415 1.2 eeh is->is_flush = 0;
416 1.2 eeh membar_sync();
417 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
418 1.3.2.1 bouyer &is->is_sb->strbuf_flushsync, 0, is->is_flushpa);
419 1.2 eeh membar_sync();
420 1.2 eeh
421 1.2 eeh microtime(&flushtimeout);
422 1.2 eeh cur = flushtimeout;
423 1.2 eeh BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
424 1.2 eeh
425 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush: flush = %lx at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
426 1.2 eeh (long)is->is_flush, (long)&is->is_flush,
427 1.2 eeh (long)is->is_flushpa, cur.tv_sec, cur.tv_usec,
428 1.3.2.1 bouyer flushtimeout.tv_sec, flushtimeout.tv_usec));
429 1.2 eeh /* Bypass non-coherent D$ */
430 1.2 eeh while (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) &&
431 1.2 eeh ((cur.tv_sec <= flushtimeout.tv_sec) &&
432 1.2 eeh (cur.tv_usec <= flushtimeout.tv_usec)))
433 1.2 eeh microtime(&cur);
434 1.2 eeh
435 1.2 eeh #ifdef DIAGNOSTIC
436 1.2 eeh if (!is->is_flush) {
437 1.3.2.1 bouyer printf("iommu_strbuf_flush: flush timeout %p at %p\n", (long)is->is_flush,
438 1.2 eeh (long)is->is_flushpa); /* panic? */
439 1.2 eeh #ifdef DDB
440 1.2 eeh Debugger();
441 1.2 eeh #endif
442 1.2 eeh }
443 1.2 eeh #endif
444 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush: flushed\n"));
445 1.3.2.1 bouyer return (is->is_flush);
446 1.3.2.1 bouyer }
447 1.3.2.1 bouyer
448 1.3.2.1 bouyer /*
449 1.3.2.1 bouyer * IOMMU DVMA operations, common to SBUS and PCI.
450 1.3.2.1 bouyer */
451 1.3.2.1 bouyer int
452 1.3.2.1 bouyer iommu_dvmamap_load(t, is, map, buf, buflen, p, flags)
453 1.3.2.1 bouyer bus_dma_tag_t t;
454 1.3.2.1 bouyer struct iommu_state *is;
455 1.3.2.1 bouyer bus_dmamap_t map;
456 1.3.2.1 bouyer void *buf;
457 1.3.2.1 bouyer bus_size_t buflen;
458 1.3.2.1 bouyer struct proc *p;
459 1.3.2.1 bouyer int flags;
460 1.3.2.1 bouyer {
461 1.3.2.1 bouyer int s;
462 1.3.2.1 bouyer int err;
463 1.3.2.1 bouyer bus_size_t sgsize;
464 1.3.2.1 bouyer paddr_t curaddr;
465 1.3.2.1 bouyer u_long dvmaddr;
466 1.3.2.1 bouyer bus_size_t align, boundary;
467 1.3.2.1 bouyer vaddr_t vaddr = (vaddr_t)buf;
468 1.3.2.1 bouyer pmap_t pmap;
469 1.3.2.1 bouyer
470 1.3.2.1 bouyer if (map->dm_nsegs) {
471 1.3.2.1 bouyer /* Already in use?? */
472 1.3.2.1 bouyer #ifdef DIAGNOSTIC
473 1.3.2.1 bouyer printf("iommu_dvmamap_load: map still in use\n");
474 1.3.2.1 bouyer #endif
475 1.3.2.1 bouyer bus_dmamap_unload(t, map);
476 1.3.2.1 bouyer }
477 1.3.2.1 bouyer /*
478 1.3.2.1 bouyer * Make sure that on error condition we return "no valid mappings".
479 1.3.2.1 bouyer */
480 1.3.2.1 bouyer map->dm_nsegs = 0;
481 1.3.2.1 bouyer
482 1.3.2.1 bouyer if (buflen > map->_dm_size) {
483 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
484 1.3.2.1 bouyer ("iommu_dvmamap_load(): error %d > %d -- "
485 1.3.2.1 bouyer "map size exceeded!\n", buflen, map->_dm_size));
486 1.3.2.1 bouyer return (EINVAL);
487 1.3.2.1 bouyer }
488 1.3.2.1 bouyer
489 1.3.2.1 bouyer sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
490 1.3.2.1 bouyer
491 1.3.2.1 bouyer /*
492 1.3.2.1 bouyer * A boundary presented to bus_dmamem_alloc() takes precedence
493 1.3.2.1 bouyer * over boundary in the map.
494 1.3.2.1 bouyer */
495 1.3.2.1 bouyer if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
496 1.3.2.1 bouyer boundary = map->_dm_boundary;
497 1.3.2.1 bouyer align = max(map->dm_segs[0]._ds_align, NBPG);
498 1.3.2.1 bouyer s = splhigh();
499 1.3.2.1 bouyer err = extent_alloc(is->is_dvmamap, sgsize, align,
500 1.3.2.1 bouyer boundary, EX_NOWAIT|EX_BOUNDZERO, (u_long *)&dvmaddr);
501 1.3.2.1 bouyer splx(s);
502 1.3.2.1 bouyer
503 1.3.2.1 bouyer #ifdef DEBUG
504 1.3.2.1 bouyer if (err || (dvmaddr == (bus_addr_t)-1))
505 1.3.2.1 bouyer {
506 1.3.2.1 bouyer printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
507 1.3.2.1 bouyer sgsize, flags);
508 1.3.2.1 bouyer Debugger();
509 1.3.2.1 bouyer }
510 1.3.2.1 bouyer #endif
511 1.3.2.1 bouyer if (err != 0)
512 1.3.2.1 bouyer return (err);
513 1.3.2.1 bouyer
514 1.3.2.1 bouyer if (dvmaddr == (bus_addr_t)-1)
515 1.3.2.1 bouyer return (ENOMEM);
516 1.3.2.1 bouyer
517 1.3.2.1 bouyer /*
518 1.3.2.1 bouyer * We always use just one segment.
519 1.3.2.1 bouyer */
520 1.3.2.1 bouyer map->dm_mapsize = buflen;
521 1.3.2.1 bouyer map->dm_nsegs = 1;
522 1.3.2.1 bouyer map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
523 1.3.2.1 bouyer map->dm_segs[0].ds_len = buflen;
524 1.3.2.1 bouyer
525 1.3.2.1 bouyer if (p != NULL)
526 1.3.2.1 bouyer pmap = p->p_vmspace->vm_map.pmap;
527 1.3.2.1 bouyer else
528 1.3.2.1 bouyer pmap = pmap_kernel();
529 1.3.2.1 bouyer
530 1.3.2.1 bouyer dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
531 1.3.2.1 bouyer for (; buflen > 0; ) {
532 1.3.2.1 bouyer /*
533 1.3.2.1 bouyer * Get the physical address for this page.
534 1.3.2.1 bouyer */
535 1.3.2.1 bouyer if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
536 1.3.2.1 bouyer bus_dmamap_unload(t, map);
537 1.3.2.1 bouyer return (-1);
538 1.3.2.1 bouyer }
539 1.3.2.1 bouyer
540 1.3.2.1 bouyer /*
541 1.3.2.1 bouyer * Compute the segment size, and adjust counts.
542 1.3.2.1 bouyer */
543 1.3.2.1 bouyer sgsize = NBPG - ((u_long)vaddr & PGOFSET);
544 1.3.2.1 bouyer if (buflen < sgsize)
545 1.3.2.1 bouyer sgsize = buflen;
546 1.3.2.1 bouyer
547 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
548 1.3.2.1 bouyer ("iommu_dvmamap_load: map %p loading va %p dva %lx at pa %lx\n",
549 1.3.2.1 bouyer map, (void *)vaddr, (long)dvmaddr, (long)(curaddr&~(NBPG-1))));
550 1.3.2.1 bouyer iommu_enter(is, trunc_page(dvmaddr), trunc_page(curaddr),
551 1.3.2.1 bouyer flags);
552 1.3.2.1 bouyer
553 1.3.2.1 bouyer dvmaddr += PAGE_SIZE;
554 1.3.2.1 bouyer vaddr += sgsize;
555 1.3.2.1 bouyer buflen -= sgsize;
556 1.3.2.1 bouyer }
557 1.3.2.1 bouyer return (0);
558 1.3.2.1 bouyer }
559 1.3.2.1 bouyer
560 1.3.2.1 bouyer
561 1.3.2.1 bouyer void
562 1.3.2.1 bouyer iommu_dvmamap_unload(t, is, map)
563 1.3.2.1 bouyer bus_dma_tag_t t;
564 1.3.2.1 bouyer struct iommu_state *is;
565 1.3.2.1 bouyer bus_dmamap_t map;
566 1.3.2.1 bouyer {
567 1.3.2.1 bouyer vaddr_t addr;
568 1.3.2.1 bouyer size_t len;
569 1.3.2.1 bouyer int error, s;
570 1.3.2.1 bouyer bus_addr_t dvmaddr;
571 1.3.2.1 bouyer bus_size_t sgsize;
572 1.3.2.1 bouyer
573 1.3.2.1 bouyer if (map->dm_nsegs != 1)
574 1.3.2.1 bouyer panic("iommu_dvmamap_unload: nsegs = %d", map->dm_nsegs);
575 1.3.2.1 bouyer
576 1.3.2.1 bouyer addr = trunc_page(map->dm_segs[0].ds_addr);
577 1.3.2.1 bouyer len = map->dm_segs[0].ds_len;
578 1.3.2.1 bouyer
579 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
580 1.3.2.1 bouyer ("iommu_dvmamap_unload: map %p removing va %lx size %lx\n",
581 1.3.2.1 bouyer map, (long)addr, (long)len));
582 1.3.2.1 bouyer iommu_remove(is, addr, len);
583 1.3.2.1 bouyer dvmaddr = (map->dm_segs[0].ds_addr & ~PGOFSET);
584 1.3.2.1 bouyer sgsize = round_page(map->dm_segs[0].ds_len +
585 1.3.2.1 bouyer ((int)map->dm_segs[0].ds_addr & PGOFSET));
586 1.3.2.1 bouyer
587 1.3.2.1 bouyer /* Flush the caches */
588 1.3.2.1 bouyer bus_dmamap_unload(t->_parent, map);
589 1.3.2.1 bouyer
590 1.3.2.1 bouyer /* Mark the mappings as invalid. */
591 1.3.2.1 bouyer map->dm_mapsize = 0;
592 1.3.2.1 bouyer map->dm_nsegs = 0;
593 1.3.2.1 bouyer
594 1.3.2.1 bouyer s = splhigh();
595 1.3.2.1 bouyer error = extent_free(is->is_dvmamap, dvmaddr, sgsize, EX_NOWAIT);
596 1.3.2.1 bouyer splx(s);
597 1.3.2.1 bouyer if (error != 0)
598 1.3.2.1 bouyer printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
599 1.3.2.1 bouyer }
600 1.3.2.1 bouyer
601 1.3.2.1 bouyer
602 1.3.2.1 bouyer int
603 1.3.2.1 bouyer iommu_dvmamap_load_raw(t, is, map, segs, nsegs, flags, size)
604 1.3.2.1 bouyer bus_dma_tag_t t;
605 1.3.2.1 bouyer struct iommu_state *is;
606 1.3.2.1 bouyer bus_dmamap_t map;
607 1.3.2.1 bouyer bus_dma_segment_t *segs;
608 1.3.2.1 bouyer int nsegs;
609 1.3.2.1 bouyer int flags;
610 1.3.2.1 bouyer bus_size_t size;
611 1.3.2.1 bouyer {
612 1.3.2.1 bouyer vm_page_t m;
613 1.3.2.1 bouyer int s;
614 1.3.2.1 bouyer int err;
615 1.3.2.1 bouyer bus_size_t sgsize;
616 1.3.2.1 bouyer paddr_t pa;
617 1.3.2.1 bouyer bus_size_t boundary, align;
618 1.3.2.1 bouyer u_long dvmaddr;
619 1.3.2.1 bouyer struct pglist *mlist;
620 1.3.2.1 bouyer int pagesz = PAGE_SIZE;
621 1.3.2.1 bouyer
622 1.3.2.1 bouyer if (map->dm_nsegs) {
623 1.3.2.1 bouyer /* Already in use?? */
624 1.3.2.1 bouyer #ifdef DIAGNOSTIC
625 1.3.2.1 bouyer printf("iommu_dvmamap_load_raw: map still in use\n");
626 1.3.2.1 bouyer #endif
627 1.3.2.1 bouyer bus_dmamap_unload(t, map);
628 1.3.2.1 bouyer }
629 1.3.2.1 bouyer /*
630 1.3.2.1 bouyer * Make sure that on error condition we return "no valid mappings".
631 1.3.2.1 bouyer */
632 1.3.2.1 bouyer map->dm_nsegs = 0;
633 1.3.2.1 bouyer #ifdef DIAGNOSTIC
634 1.3.2.1 bouyer /* XXX - unhelpful since we can't reset these in map_unload() */
635 1.3.2.1 bouyer if (segs[0].ds_addr != 0)
636 1.3.2.1 bouyer panic("iommu_dvmamap_load_raw: segment already loaded: "
637 1.3.2.1 bouyer "addr %#llx, size %#llx",
638 1.3.2.1 bouyer (u_int64_t)segs[0].ds_addr, (u_int64_t)segs[0].ds_len);
639 1.3.2.1 bouyer if (segs[0].ds_len != size)
640 1.3.2.1 bouyer panic("iommu_dvmamap_load_raw: segment size changed: "
641 1.3.2.1 bouyer "ds_len %#llx size %#llx", segs[0].ds_len, size);
642 1.3.2.1 bouyer #endif
643 1.3.2.1 bouyer sgsize = round_page(size);
644 1.3.2.1 bouyer
645 1.3.2.1 bouyer /*
646 1.3.2.1 bouyer * A boundary presented to bus_dmamem_alloc() takes precedence
647 1.3.2.1 bouyer * over boundary in the map.
648 1.3.2.1 bouyer */
649 1.3.2.1 bouyer if ((boundary = segs[0]._ds_boundary) == 0)
650 1.3.2.1 bouyer boundary = map->_dm_boundary;
651 1.3.2.1 bouyer
652 1.3.2.1 bouyer align = max(segs[0]._ds_align, NBPG);
653 1.3.2.1 bouyer s = splhigh();
654 1.3.2.1 bouyer err = extent_alloc(is->is_dvmamap, sgsize, align, boundary,
655 1.3.2.1 bouyer ((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT)|EX_BOUNDZERO,
656 1.3.2.1 bouyer (u_long *)&dvmaddr);
657 1.3.2.1 bouyer splx(s);
658 1.3.2.1 bouyer
659 1.3.2.1 bouyer if (err != 0)
660 1.3.2.1 bouyer return (err);
661 1.3.2.1 bouyer
662 1.2 eeh #ifdef DEBUG
663 1.3.2.1 bouyer if (dvmaddr == (bus_addr_t)-1)
664 1.3.2.1 bouyer {
665 1.3.2.1 bouyer printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
666 1.3.2.1 bouyer sgsize, flags);
667 1.3.2.1 bouyer Debugger();
668 1.3.2.1 bouyer }
669 1.3.2.1 bouyer #endif
670 1.3.2.1 bouyer if (dvmaddr == (bus_addr_t)-1)
671 1.3.2.1 bouyer return (ENOMEM);
672 1.3.2.1 bouyer
673 1.3.2.1 bouyer /*
674 1.3.2.1 bouyer * We always use just one segment.
675 1.3.2.1 bouyer */
676 1.3.2.1 bouyer map->dm_mapsize = size;
677 1.3.2.1 bouyer map->dm_nsegs = 1;
678 1.3.2.1 bouyer map->dm_segs[0].ds_addr = dvmaddr;
679 1.3.2.1 bouyer map->dm_segs[0].ds_len = size;
680 1.3.2.1 bouyer
681 1.3.2.1 bouyer mlist = segs[0]._ds_mlist;
682 1.3.2.1 bouyer for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
683 1.3.2.1 bouyer if (sgsize == 0)
684 1.3.2.1 bouyer panic("iommu_dmamap_load_raw: size botch");
685 1.3.2.1 bouyer pa = VM_PAGE_TO_PHYS(m);
686 1.3.2.1 bouyer
687 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
688 1.3.2.1 bouyer ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
689 1.3.2.1 bouyer map, (long)dvmaddr, (long)(pa)));
690 1.3.2.1 bouyer iommu_enter(is, dvmaddr, pa, flags);
691 1.3.2.1 bouyer
692 1.3.2.1 bouyer dvmaddr += pagesz;
693 1.3.2.1 bouyer sgsize -= pagesz;
694 1.3.2.1 bouyer }
695 1.3.2.1 bouyer return (0);
696 1.3.2.1 bouyer }
697 1.3.2.1 bouyer
698 1.3.2.1 bouyer void
699 1.3.2.1 bouyer iommu_dvmamap_sync(t, is, map, offset, len, ops)
700 1.3.2.1 bouyer bus_dma_tag_t t;
701 1.3.2.1 bouyer struct iommu_state *is;
702 1.3.2.1 bouyer bus_dmamap_t map;
703 1.3.2.1 bouyer bus_addr_t offset;
704 1.3.2.1 bouyer bus_size_t len;
705 1.3.2.1 bouyer int ops;
706 1.3.2.1 bouyer {
707 1.3.2.1 bouyer vaddr_t va = map->dm_segs[0].ds_addr + offset;
708 1.3.2.1 bouyer
709 1.3.2.1 bouyer /*
710 1.3.2.1 bouyer * We only support one DMA segment; supporting more makes this code
711 1.3.2.1 bouyer * too unweildy.
712 1.3.2.1 bouyer */
713 1.3.2.1 bouyer
714 1.3.2.1 bouyer if (ops & BUS_DMASYNC_PREREAD) {
715 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
716 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
717 1.3.2.1 bouyer "BUS_DMASYNC_PREREAD\n", (long)va, (u_long)len));
718 1.3.2.1 bouyer
719 1.3.2.1 bouyer /* Nothing to do */;
720 1.3.2.1 bouyer }
721 1.3.2.1 bouyer if (ops & BUS_DMASYNC_POSTREAD) {
722 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
723 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
724 1.3.2.1 bouyer "BUS_DMASYNC_POSTREAD\n", (long)va, (u_long)len));
725 1.3.2.1 bouyer /* if we have a streaming buffer, flush it here first */
726 1.3.2.1 bouyer if (is->is_sb)
727 1.3.2.1 bouyer while (len > 0) {
728 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
729 1.3.2.1 bouyer ("iommu_dvmamap_sync: flushing va %p, %lu "
730 1.3.2.1 bouyer "bytes left\n", (long)va, (u_long)len));
731 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
732 1.3.2.1 bouyer (bus_space_handle_t)(u_long)
733 1.3.2.1 bouyer &is->is_sb->strbuf_pgflush, 0, va);
734 1.3.2.1 bouyer if (len <= NBPG) {
735 1.3.2.1 bouyer iommu_strbuf_flush(is);
736 1.3.2.1 bouyer len = 0;
737 1.3.2.1 bouyer } else
738 1.3.2.1 bouyer len -= NBPG;
739 1.3.2.1 bouyer va += NBPG;
740 1.3.2.1 bouyer }
741 1.3.2.1 bouyer }
742 1.3.2.1 bouyer if (ops & BUS_DMASYNC_PREWRITE) {
743 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
744 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
745 1.3.2.1 bouyer "BUS_DMASYNC_PREWRITE\n", (long)va, (u_long)len));
746 1.3.2.1 bouyer /* Nothing to do */;
747 1.3.2.1 bouyer }
748 1.3.2.1 bouyer if (ops & BUS_DMASYNC_POSTWRITE) {
749 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
750 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
751 1.3.2.1 bouyer "BUS_DMASYNC_POSTWRITE\n", (long)va, (u_long)len));
752 1.3.2.1 bouyer /* Nothing to do */;
753 1.3.2.1 bouyer }
754 1.3.2.1 bouyer }
755 1.3.2.1 bouyer
756 1.3.2.1 bouyer int
757 1.3.2.1 bouyer iommu_dvmamem_alloc(t, is, size, alignment, boundary, segs, nsegs, rsegs, flags)
758 1.3.2.1 bouyer bus_dma_tag_t t;
759 1.3.2.1 bouyer struct iommu_state *is;
760 1.3.2.1 bouyer bus_size_t size, alignment, boundary;
761 1.3.2.1 bouyer bus_dma_segment_t *segs;
762 1.3.2.1 bouyer int nsegs;
763 1.3.2.1 bouyer int *rsegs;
764 1.3.2.1 bouyer int flags;
765 1.3.2.1 bouyer {
766 1.3.2.1 bouyer
767 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %qx align %qx bound %qx "
768 1.3.2.1 bouyer "segp %p flags %d\n", size, alignment, boundary, segs, flags));
769 1.3.2.1 bouyer return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
770 1.3.2.1 bouyer segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
771 1.3.2.1 bouyer }
772 1.3.2.1 bouyer
773 1.3.2.1 bouyer void
774 1.3.2.1 bouyer iommu_dvmamem_free(t, is, segs, nsegs)
775 1.3.2.1 bouyer bus_dma_tag_t t;
776 1.3.2.1 bouyer struct iommu_state *is;
777 1.3.2.1 bouyer bus_dma_segment_t *segs;
778 1.3.2.1 bouyer int nsegs;
779 1.3.2.1 bouyer {
780 1.3.2.1 bouyer
781 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
782 1.3.2.1 bouyer segs, nsegs));
783 1.3.2.1 bouyer bus_dmamem_free(t->_parent, segs, nsegs);
784 1.3.2.1 bouyer }
785 1.3.2.1 bouyer
786 1.3.2.1 bouyer /*
787 1.3.2.1 bouyer * Map the DVMA mappings into the kernel pmap.
788 1.3.2.1 bouyer * Check the flags to see whether we're streaming or coherent.
789 1.3.2.1 bouyer */
790 1.3.2.1 bouyer int
791 1.3.2.1 bouyer iommu_dvmamem_map(t, is, segs, nsegs, size, kvap, flags)
792 1.3.2.1 bouyer bus_dma_tag_t t;
793 1.3.2.1 bouyer struct iommu_state *is;
794 1.3.2.1 bouyer bus_dma_segment_t *segs;
795 1.3.2.1 bouyer int nsegs;
796 1.3.2.1 bouyer size_t size;
797 1.3.2.1 bouyer caddr_t *kvap;
798 1.3.2.1 bouyer int flags;
799 1.3.2.1 bouyer {
800 1.3.2.1 bouyer vm_page_t m;
801 1.3.2.1 bouyer vaddr_t va;
802 1.3.2.1 bouyer bus_addr_t addr;
803 1.3.2.1 bouyer struct pglist *mlist;
804 1.3.2.1 bouyer int cbit;
805 1.3.2.1 bouyer
806 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
807 1.3.2.1 bouyer segs, nsegs, size));
808 1.3.2.1 bouyer
809 1.3.2.1 bouyer /*
810 1.3.2.1 bouyer * Allocate some space in the kernel map, and then map these pages
811 1.3.2.1 bouyer * into this space.
812 1.3.2.1 bouyer */
813 1.3.2.1 bouyer size = round_page(size);
814 1.3.2.1 bouyer va = uvm_km_valloc(kernel_map, size);
815 1.3.2.1 bouyer if (va == 0)
816 1.3.2.1 bouyer return (ENOMEM);
817 1.3.2.1 bouyer
818 1.3.2.1 bouyer *kvap = (caddr_t)va;
819 1.3.2.1 bouyer
820 1.3.2.1 bouyer /*
821 1.3.2.1 bouyer * digest flags:
822 1.3.2.1 bouyer */
823 1.3.2.1 bouyer cbit = 0;
824 1.3.2.1 bouyer if (flags & BUS_DMA_COHERENT) /* Disable vcache */
825 1.3.2.1 bouyer cbit |= PMAP_NVC;
826 1.3.2.1 bouyer if (flags & BUS_DMA_NOCACHE) /* sideffects */
827 1.3.2.1 bouyer cbit |= PMAP_NC;
828 1.3.2.1 bouyer
829 1.3.2.1 bouyer /*
830 1.3.2.1 bouyer * Now take this and map it into the CPU.
831 1.3.2.1 bouyer */
832 1.3.2.1 bouyer mlist = segs[0]._ds_mlist;
833 1.3.2.1 bouyer for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
834 1.3.2.1 bouyer #ifdef DIAGNOSTIC
835 1.3.2.1 bouyer if (size == 0)
836 1.3.2.1 bouyer panic("iommu_dvmamem_map: size botch");
837 1.3.2.1 bouyer #endif
838 1.3.2.1 bouyer addr = VM_PAGE_TO_PHYS(m);
839 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
840 1.3.2.1 bouyer "mapping va %lx at %qx\n", va, addr | cbit));
841 1.3.2.1 bouyer pmap_enter(pmap_kernel(), va, addr | cbit,
842 1.3.2.1 bouyer VM_PROT_READ | VM_PROT_WRITE,
843 1.3.2.1 bouyer VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
844 1.3.2.1 bouyer va += PAGE_SIZE;
845 1.3.2.1 bouyer size -= PAGE_SIZE;
846 1.3.2.1 bouyer }
847 1.3.2.1 bouyer
848 1.3.2.1 bouyer return (0);
849 1.3.2.1 bouyer }
850 1.3.2.1 bouyer
851 1.3.2.1 bouyer /*
852 1.3.2.1 bouyer * Unmap DVMA mappings from kernel
853 1.3.2.1 bouyer */
854 1.3.2.1 bouyer void
855 1.3.2.1 bouyer iommu_dvmamem_unmap(t, is, kva, size)
856 1.3.2.1 bouyer bus_dma_tag_t t;
857 1.3.2.1 bouyer struct iommu_state *is;
858 1.3.2.1 bouyer caddr_t kva;
859 1.3.2.1 bouyer size_t size;
860 1.3.2.1 bouyer {
861 1.3.2.1 bouyer
862 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
863 1.3.2.1 bouyer kva, size));
864 1.3.2.1 bouyer
865 1.3.2.1 bouyer #ifdef DIAGNOSTIC
866 1.3.2.1 bouyer if ((u_long)kva & PGOFSET)
867 1.3.2.1 bouyer panic("iommu_dvmamem_unmap");
868 1.3.2.1 bouyer #endif
869 1.3.2.1 bouyer
870 1.3.2.1 bouyer size = round_page(size);
871 1.3.2.1 bouyer pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
872 1.3.2.1 bouyer #if 0
873 1.3.2.1 bouyer /*
874 1.3.2.1 bouyer * XXX ? is this necessary? i think so and i think other
875 1.3.2.1 bouyer * implementations are missing it.
876 1.3.2.1 bouyer */
877 1.3.2.1 bouyer uvm_km_free(kernel_map, (vaddr_t)kva, size);
878 1.2 eeh #endif
879 1.1 mrg }
880