iommu.c revision 1.3.2.2 1 1.3.2.2 bouyer /* $NetBSD: iommu.c,v 1.3.2.2 2000/12/08 09:30:33 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.3.2.2 bouyer printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n",
219 1.3.2.2 bouyer (u_long)®s->iommu_cr,
220 1.3.2.2 bouyer (u_long)®s->iommu_tsb,
221 1.3.2.2 bouyer (u_long)®s->iommu_flush);
222 1.3.2.2 bouyer printf("iommu cr=%llx tsb=%llx\n", (unsigned long long)regs->iommu_cr, (unsigned long long)regs->iommu_tsb);
223 1.3.2.2 bouyer printf("TSB base %p phys %llx\n", (void *)is->is_tsb, (unsigned long long)is->is_ptsb);
224 1.1 mrg delay(1000000); /* 1 s */
225 1.1 mrg }
226 1.1 mrg #endif
227 1.1 mrg
228 1.1 mrg /*
229 1.3.2.1 bouyer * Initialize streaming buffer, if it is there.
230 1.1 mrg */
231 1.3.2.1 bouyer if (is->is_sb)
232 1.3.2.1 bouyer (void)pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush,
233 1.3.2.1 bouyer (paddr_t *)&is->is_flushpa);
234 1.1 mrg
235 1.1 mrg /*
236 1.1 mrg * now actually start up the IOMMU
237 1.1 mrg */
238 1.1 mrg iommu_reset(is);
239 1.1 mrg
240 1.1 mrg /*
241 1.1 mrg * Now all the hardware's working we need to allocate a dvma map.
242 1.1 mrg */
243 1.3.2.1 bouyer printf("DVMA map: %x to %x\n",
244 1.3.2.1 bouyer (unsigned int)is->is_dvmabase,
245 1.3.2.1 bouyer (unsigned int)IOTSB_VEND);
246 1.1 mrg is->is_dvmamap = extent_create(name,
247 1.3.2.1 bouyer is->is_dvmabase, (u_long)IOTSB_VEND,
248 1.1 mrg M_DEVBUF, 0, 0, EX_NOWAIT);
249 1.1 mrg }
250 1.1 mrg
251 1.3.2.1 bouyer /*
252 1.3.2.1 bouyer * Streaming buffers don't exist on the UltraSPARC IIi; we should have
253 1.3.2.1 bouyer * detected that already and disabled them. If not, we will notice that
254 1.3.2.1 bouyer * they aren't there when the STRBUF_EN bit does not remain.
255 1.3.2.1 bouyer */
256 1.1 mrg void
257 1.1 mrg iommu_reset(is)
258 1.1 mrg struct iommu_state *is;
259 1.1 mrg {
260 1.1 mrg
261 1.1 mrg /* Need to do 64-bit stores */
262 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
263 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_iommu->iommu_tsb,
264 1.3.2.1 bouyer 0, is->is_ptsb);
265 1.3.2.1 bouyer /* Enable IOMMU in diagnostic mode */
266 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
267 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_iommu->iommu_cr, 0,
268 1.3.2.1 bouyer is->is_cr|IOMMUCR_DE);
269 1.3.2.1 bouyer
270 1.3.2.1 bouyer
271 1.3.2.1 bouyer if (!is->is_sb)
272 1.3.2.1 bouyer return;
273 1.3.2.1 bouyer
274 1.1 mrg /* Enable diagnostics mode? */
275 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
276 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_sb->strbuf_ctl,
277 1.3.2.1 bouyer 0, STRBUF_EN);
278 1.3.2.1 bouyer
279 1.3.2.1 bouyer /* No streaming buffers? Disable them */
280 1.3.2.1 bouyer if (bus_space_read_8(is->is_bustag,
281 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_sb->strbuf_ctl,
282 1.3.2.1 bouyer 0) == 0)
283 1.3.2.1 bouyer is->is_sb = 0;
284 1.2 eeh }
285 1.2 eeh
286 1.2 eeh /*
287 1.2 eeh * Here are the iommu control routines.
288 1.2 eeh */
289 1.2 eeh void
290 1.2 eeh iommu_enter(is, va, pa, flags)
291 1.2 eeh struct iommu_state *is;
292 1.2 eeh vaddr_t va;
293 1.2 eeh int64_t pa;
294 1.2 eeh int flags;
295 1.2 eeh {
296 1.2 eeh int64_t tte;
297 1.2 eeh
298 1.2 eeh #ifdef DIAGNOSTIC
299 1.2 eeh if (va < is->is_dvmabase)
300 1.3.2.1 bouyer panic("iommu_enter: va %#lx not in DVMA space", va);
301 1.2 eeh #endif
302 1.2 eeh
303 1.2 eeh tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
304 1.2 eeh !(flags&BUS_DMA_COHERENT));
305 1.2 eeh
306 1.2 eeh /* Is the streamcache flush really needed? */
307 1.3.2.1 bouyer if (is->is_sb) {
308 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
309 1.3.2.1 bouyer &is->is_sb->strbuf_pgflush, 0, va);
310 1.3.2.1 bouyer iommu_strbuf_flush(is);
311 1.3.2.1 bouyer }
312 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
313 1.3.2.2 bouyer (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va));
314 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
315 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
316 1.3.2.1 bouyer &is->is_iommu->iommu_flush, 0, va);
317 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
318 1.3.2.2 bouyer va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize),
319 1.3.2.2 bouyer (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
320 1.3.2.2 bouyer (u_long)tte));
321 1.2 eeh }
322 1.2 eeh
323 1.2 eeh /*
324 1.2 eeh * iommu_remove: removes mappings created by iommu_enter
325 1.2 eeh *
326 1.2 eeh * Only demap from IOMMU if flag is set.
327 1.3.2.1 bouyer *
328 1.3.2.1 bouyer * XXX: this function needs better internal error checking.
329 1.2 eeh */
330 1.2 eeh void
331 1.2 eeh iommu_remove(is, va, len)
332 1.2 eeh struct iommu_state *is;
333 1.2 eeh vaddr_t va;
334 1.2 eeh size_t len;
335 1.2 eeh {
336 1.2 eeh
337 1.2 eeh #ifdef DIAGNOSTIC
338 1.2 eeh if (va < is->is_dvmabase)
339 1.3.2.2 bouyer panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va);
340 1.2 eeh if ((long)(va + len) < (long)va)
341 1.3.2.1 bouyer panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
342 1.2 eeh (long) va, (long) len);
343 1.2 eeh if (len & ~0xfffffff)
344 1.3.2.2 bouyer panic("iommu_remove: rediculous len 0x%lx", (u_long)len);
345 1.2 eeh #endif
346 1.2 eeh
347 1.2 eeh va = trunc_page(va);
348 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
349 1.3.2.2 bouyer va, (u_long)IOTSBSLOT(va,is->is_tsbsize),
350 1.3.2.1 bouyer &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]));
351 1.2 eeh while (len > 0) {
352 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d for va %p size %lx\n",
353 1.3.2.2 bouyer (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va, (u_long)len));
354 1.3.2.1 bouyer if (is->is_sb) {
355 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
356 1.3.2.2 bouyer (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
357 1.3.2.2 bouyer (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
358 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
359 1.3.2.1 bouyer (u_long)len));
360 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
361 1.3.2.1 bouyer &is->is_sb->strbuf_pgflush, 0, va);
362 1.3.2.1 bouyer if (len <= NBPG)
363 1.3.2.1 bouyer iommu_strbuf_flush(is);
364 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
365 1.3.2.2 bouyer (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
366 1.3.2.2 bouyer (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
367 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
368 1.3.2.1 bouyer (u_long)len));
369 1.3.2.1 bouyer } else
370 1.3.2.1 bouyer membar_sync(); /* XXX */
371 1.3.2.1 bouyer
372 1.3.2.1 bouyer if (len <= NBPG)
373 1.3.2.1 bouyer len = 0;
374 1.3.2.1 bouyer else
375 1.3.2.1 bouyer len -= NBPG;
376 1.3.2.1 bouyer
377 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = 0;
378 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
379 1.3.2.1 bouyer &is->is_iommu->iommu_flush, 0, va);
380 1.2 eeh va += NBPG;
381 1.2 eeh }
382 1.2 eeh }
383 1.2 eeh
384 1.3.2.1 bouyer static int
385 1.3.2.1 bouyer iommu_strbuf_flush(is)
386 1.2 eeh struct iommu_state *is;
387 1.2 eeh {
388 1.2 eeh struct timeval cur, flushtimeout;
389 1.2 eeh
390 1.2 eeh #define BUMPTIME(t, usec) { \
391 1.2 eeh register volatile struct timeval *tp = (t); \
392 1.2 eeh register long us; \
393 1.2 eeh \
394 1.2 eeh tp->tv_usec = us = tp->tv_usec + (usec); \
395 1.2 eeh if (us >= 1000000) { \
396 1.2 eeh tp->tv_usec = us - 1000000; \
397 1.2 eeh tp->tv_sec++; \
398 1.2 eeh } \
399 1.2 eeh }
400 1.2 eeh
401 1.3.2.1 bouyer if (!is->is_sb)
402 1.3.2.1 bouyer return (0);
403 1.3.2.1 bouyer
404 1.3.2.1 bouyer /*
405 1.3.2.1 bouyer * Streaming buffer flushes:
406 1.3.2.1 bouyer *
407 1.3.2.1 bouyer * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
408 1.3.2.1 bouyer * we're not on a cache line boundary (64-bits):
409 1.3.2.1 bouyer * 2 Store 0 in flag
410 1.3.2.1 bouyer * 3 Store pointer to flag in flushsync
411 1.3.2.1 bouyer * 4 wait till flushsync becomes 0x1
412 1.3.2.1 bouyer *
413 1.3.2.1 bouyer * If it takes more than .5 sec, something
414 1.3.2.1 bouyer * went wrong.
415 1.3.2.1 bouyer */
416 1.3.2.1 bouyer
417 1.2 eeh is->is_flush = 0;
418 1.2 eeh membar_sync();
419 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
420 1.3.2.1 bouyer &is->is_sb->strbuf_flushsync, 0, is->is_flushpa);
421 1.2 eeh membar_sync();
422 1.2 eeh
423 1.2 eeh microtime(&flushtimeout);
424 1.2 eeh cur = flushtimeout;
425 1.2 eeh BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
426 1.2 eeh
427 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",
428 1.2 eeh (long)is->is_flush, (long)&is->is_flush,
429 1.2 eeh (long)is->is_flushpa, cur.tv_sec, cur.tv_usec,
430 1.3.2.1 bouyer flushtimeout.tv_sec, flushtimeout.tv_usec));
431 1.2 eeh /* Bypass non-coherent D$ */
432 1.2 eeh while (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) &&
433 1.2 eeh ((cur.tv_sec <= flushtimeout.tv_sec) &&
434 1.2 eeh (cur.tv_usec <= flushtimeout.tv_usec)))
435 1.2 eeh microtime(&cur);
436 1.2 eeh
437 1.2 eeh #ifdef DIAGNOSTIC
438 1.2 eeh if (!is->is_flush) {
439 1.3.2.2 bouyer printf("iommu_strbuf_flush: flush timeout %p at %p\n",
440 1.3.2.2 bouyer (void *)(u_long)is->is_flush,
441 1.3.2.2 bouyer (void *)(u_long)is->is_flushpa); /* panic? */
442 1.2 eeh #ifdef DDB
443 1.2 eeh Debugger();
444 1.2 eeh #endif
445 1.2 eeh }
446 1.2 eeh #endif
447 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush: flushed\n"));
448 1.3.2.1 bouyer return (is->is_flush);
449 1.3.2.1 bouyer }
450 1.3.2.1 bouyer
451 1.3.2.1 bouyer /*
452 1.3.2.1 bouyer * IOMMU DVMA operations, common to SBUS and PCI.
453 1.3.2.1 bouyer */
454 1.3.2.1 bouyer int
455 1.3.2.1 bouyer iommu_dvmamap_load(t, is, map, buf, buflen, p, flags)
456 1.3.2.1 bouyer bus_dma_tag_t t;
457 1.3.2.1 bouyer struct iommu_state *is;
458 1.3.2.1 bouyer bus_dmamap_t map;
459 1.3.2.1 bouyer void *buf;
460 1.3.2.1 bouyer bus_size_t buflen;
461 1.3.2.1 bouyer struct proc *p;
462 1.3.2.1 bouyer int flags;
463 1.3.2.1 bouyer {
464 1.3.2.1 bouyer int s;
465 1.3.2.1 bouyer int err;
466 1.3.2.1 bouyer bus_size_t sgsize;
467 1.3.2.1 bouyer paddr_t curaddr;
468 1.3.2.1 bouyer u_long dvmaddr;
469 1.3.2.1 bouyer bus_size_t align, boundary;
470 1.3.2.1 bouyer vaddr_t vaddr = (vaddr_t)buf;
471 1.3.2.1 bouyer pmap_t pmap;
472 1.3.2.1 bouyer
473 1.3.2.1 bouyer if (map->dm_nsegs) {
474 1.3.2.1 bouyer /* Already in use?? */
475 1.3.2.1 bouyer #ifdef DIAGNOSTIC
476 1.3.2.1 bouyer printf("iommu_dvmamap_load: map still in use\n");
477 1.3.2.1 bouyer #endif
478 1.3.2.1 bouyer bus_dmamap_unload(t, map);
479 1.3.2.1 bouyer }
480 1.3.2.1 bouyer /*
481 1.3.2.1 bouyer * Make sure that on error condition we return "no valid mappings".
482 1.3.2.1 bouyer */
483 1.3.2.1 bouyer map->dm_nsegs = 0;
484 1.3.2.1 bouyer
485 1.3.2.1 bouyer if (buflen > map->_dm_size) {
486 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
487 1.3.2.1 bouyer ("iommu_dvmamap_load(): error %d > %d -- "
488 1.3.2.2 bouyer "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
489 1.3.2.1 bouyer return (EINVAL);
490 1.3.2.1 bouyer }
491 1.3.2.1 bouyer
492 1.3.2.1 bouyer sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
493 1.3.2.1 bouyer
494 1.3.2.1 bouyer /*
495 1.3.2.1 bouyer * A boundary presented to bus_dmamem_alloc() takes precedence
496 1.3.2.1 bouyer * over boundary in the map.
497 1.3.2.1 bouyer */
498 1.3.2.1 bouyer if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
499 1.3.2.1 bouyer boundary = map->_dm_boundary;
500 1.3.2.1 bouyer align = max(map->dm_segs[0]._ds_align, NBPG);
501 1.3.2.1 bouyer s = splhigh();
502 1.3.2.1 bouyer err = extent_alloc(is->is_dvmamap, sgsize, align,
503 1.3.2.1 bouyer boundary, EX_NOWAIT|EX_BOUNDZERO, (u_long *)&dvmaddr);
504 1.3.2.1 bouyer splx(s);
505 1.3.2.1 bouyer
506 1.3.2.1 bouyer #ifdef DEBUG
507 1.3.2.1 bouyer if (err || (dvmaddr == (bus_addr_t)-1))
508 1.3.2.1 bouyer {
509 1.3.2.1 bouyer printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
510 1.3.2.2 bouyer (int)sgsize, flags);
511 1.3.2.1 bouyer Debugger();
512 1.3.2.1 bouyer }
513 1.3.2.1 bouyer #endif
514 1.3.2.1 bouyer if (err != 0)
515 1.3.2.1 bouyer return (err);
516 1.3.2.1 bouyer
517 1.3.2.1 bouyer if (dvmaddr == (bus_addr_t)-1)
518 1.3.2.1 bouyer return (ENOMEM);
519 1.3.2.1 bouyer
520 1.3.2.1 bouyer /*
521 1.3.2.1 bouyer * We always use just one segment.
522 1.3.2.1 bouyer */
523 1.3.2.1 bouyer map->dm_mapsize = buflen;
524 1.3.2.1 bouyer map->dm_nsegs = 1;
525 1.3.2.1 bouyer map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
526 1.3.2.1 bouyer map->dm_segs[0].ds_len = buflen;
527 1.3.2.1 bouyer
528 1.3.2.1 bouyer if (p != NULL)
529 1.3.2.1 bouyer pmap = p->p_vmspace->vm_map.pmap;
530 1.3.2.1 bouyer else
531 1.3.2.1 bouyer pmap = pmap_kernel();
532 1.3.2.1 bouyer
533 1.3.2.1 bouyer dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
534 1.3.2.1 bouyer for (; buflen > 0; ) {
535 1.3.2.1 bouyer /*
536 1.3.2.1 bouyer * Get the physical address for this page.
537 1.3.2.1 bouyer */
538 1.3.2.1 bouyer if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
539 1.3.2.1 bouyer bus_dmamap_unload(t, map);
540 1.3.2.1 bouyer return (-1);
541 1.3.2.1 bouyer }
542 1.3.2.1 bouyer
543 1.3.2.1 bouyer /*
544 1.3.2.1 bouyer * Compute the segment size, and adjust counts.
545 1.3.2.1 bouyer */
546 1.3.2.1 bouyer sgsize = NBPG - ((u_long)vaddr & PGOFSET);
547 1.3.2.1 bouyer if (buflen < sgsize)
548 1.3.2.1 bouyer sgsize = buflen;
549 1.3.2.1 bouyer
550 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
551 1.3.2.1 bouyer ("iommu_dvmamap_load: map %p loading va %p dva %lx at pa %lx\n",
552 1.3.2.1 bouyer map, (void *)vaddr, (long)dvmaddr, (long)(curaddr&~(NBPG-1))));
553 1.3.2.1 bouyer iommu_enter(is, trunc_page(dvmaddr), trunc_page(curaddr),
554 1.3.2.1 bouyer flags);
555 1.3.2.1 bouyer
556 1.3.2.1 bouyer dvmaddr += PAGE_SIZE;
557 1.3.2.1 bouyer vaddr += sgsize;
558 1.3.2.1 bouyer buflen -= sgsize;
559 1.3.2.1 bouyer }
560 1.3.2.1 bouyer return (0);
561 1.3.2.1 bouyer }
562 1.3.2.1 bouyer
563 1.3.2.1 bouyer
564 1.3.2.1 bouyer void
565 1.3.2.1 bouyer iommu_dvmamap_unload(t, is, map)
566 1.3.2.1 bouyer bus_dma_tag_t t;
567 1.3.2.1 bouyer struct iommu_state *is;
568 1.3.2.1 bouyer bus_dmamap_t map;
569 1.3.2.1 bouyer {
570 1.3.2.1 bouyer vaddr_t addr;
571 1.3.2.1 bouyer size_t len;
572 1.3.2.1 bouyer int error, s;
573 1.3.2.1 bouyer bus_addr_t dvmaddr;
574 1.3.2.1 bouyer bus_size_t sgsize;
575 1.3.2.1 bouyer
576 1.3.2.1 bouyer if (map->dm_nsegs != 1)
577 1.3.2.1 bouyer panic("iommu_dvmamap_unload: nsegs = %d", map->dm_nsegs);
578 1.3.2.1 bouyer
579 1.3.2.1 bouyer addr = trunc_page(map->dm_segs[0].ds_addr);
580 1.3.2.1 bouyer len = map->dm_segs[0].ds_len;
581 1.3.2.1 bouyer
582 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
583 1.3.2.1 bouyer ("iommu_dvmamap_unload: map %p removing va %lx size %lx\n",
584 1.3.2.1 bouyer map, (long)addr, (long)len));
585 1.3.2.1 bouyer iommu_remove(is, addr, len);
586 1.3.2.1 bouyer dvmaddr = (map->dm_segs[0].ds_addr & ~PGOFSET);
587 1.3.2.1 bouyer sgsize = round_page(map->dm_segs[0].ds_len +
588 1.3.2.1 bouyer ((int)map->dm_segs[0].ds_addr & PGOFSET));
589 1.3.2.1 bouyer
590 1.3.2.1 bouyer /* Flush the caches */
591 1.3.2.1 bouyer bus_dmamap_unload(t->_parent, map);
592 1.3.2.1 bouyer
593 1.3.2.1 bouyer /* Mark the mappings as invalid. */
594 1.3.2.1 bouyer map->dm_mapsize = 0;
595 1.3.2.1 bouyer map->dm_nsegs = 0;
596 1.3.2.1 bouyer
597 1.3.2.1 bouyer s = splhigh();
598 1.3.2.1 bouyer error = extent_free(is->is_dvmamap, dvmaddr, sgsize, EX_NOWAIT);
599 1.3.2.1 bouyer splx(s);
600 1.3.2.1 bouyer if (error != 0)
601 1.3.2.1 bouyer printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
602 1.3.2.1 bouyer }
603 1.3.2.1 bouyer
604 1.3.2.1 bouyer
605 1.3.2.1 bouyer int
606 1.3.2.1 bouyer iommu_dvmamap_load_raw(t, is, map, segs, nsegs, flags, size)
607 1.3.2.1 bouyer bus_dma_tag_t t;
608 1.3.2.1 bouyer struct iommu_state *is;
609 1.3.2.1 bouyer bus_dmamap_t map;
610 1.3.2.1 bouyer bus_dma_segment_t *segs;
611 1.3.2.1 bouyer int nsegs;
612 1.3.2.1 bouyer int flags;
613 1.3.2.1 bouyer bus_size_t size;
614 1.3.2.1 bouyer {
615 1.3.2.1 bouyer vm_page_t m;
616 1.3.2.1 bouyer int s;
617 1.3.2.1 bouyer int err;
618 1.3.2.1 bouyer bus_size_t sgsize;
619 1.3.2.1 bouyer paddr_t pa;
620 1.3.2.1 bouyer bus_size_t boundary, align;
621 1.3.2.1 bouyer u_long dvmaddr;
622 1.3.2.1 bouyer struct pglist *mlist;
623 1.3.2.1 bouyer int pagesz = PAGE_SIZE;
624 1.3.2.1 bouyer
625 1.3.2.1 bouyer if (map->dm_nsegs) {
626 1.3.2.1 bouyer /* Already in use?? */
627 1.3.2.1 bouyer #ifdef DIAGNOSTIC
628 1.3.2.1 bouyer printf("iommu_dvmamap_load_raw: map still in use\n");
629 1.3.2.1 bouyer #endif
630 1.3.2.1 bouyer bus_dmamap_unload(t, map);
631 1.3.2.1 bouyer }
632 1.3.2.1 bouyer /*
633 1.3.2.1 bouyer * Make sure that on error condition we return "no valid mappings".
634 1.3.2.1 bouyer */
635 1.3.2.1 bouyer map->dm_nsegs = 0;
636 1.3.2.1 bouyer #ifdef DIAGNOSTIC
637 1.3.2.1 bouyer /* XXX - unhelpful since we can't reset these in map_unload() */
638 1.3.2.1 bouyer if (segs[0].ds_addr != 0)
639 1.3.2.1 bouyer panic("iommu_dvmamap_load_raw: segment already loaded: "
640 1.3.2.1 bouyer "addr %#llx, size %#llx",
641 1.3.2.2 bouyer (unsigned long long)segs[0].ds_addr,
642 1.3.2.2 bouyer (unsigned long long)segs[0].ds_len);
643 1.3.2.1 bouyer if (segs[0].ds_len != size)
644 1.3.2.1 bouyer panic("iommu_dvmamap_load_raw: segment size changed: "
645 1.3.2.2 bouyer "ds_len %#llx size %#llx",
646 1.3.2.2 bouyer (unsigned long long)segs[0].ds_len,
647 1.3.2.2 bouyer (unsigned long long)size);
648 1.3.2.1 bouyer #endif
649 1.3.2.1 bouyer sgsize = round_page(size);
650 1.3.2.1 bouyer
651 1.3.2.1 bouyer /*
652 1.3.2.1 bouyer * A boundary presented to bus_dmamem_alloc() takes precedence
653 1.3.2.1 bouyer * over boundary in the map.
654 1.3.2.1 bouyer */
655 1.3.2.1 bouyer if ((boundary = segs[0]._ds_boundary) == 0)
656 1.3.2.1 bouyer boundary = map->_dm_boundary;
657 1.3.2.1 bouyer
658 1.3.2.1 bouyer align = max(segs[0]._ds_align, NBPG);
659 1.3.2.1 bouyer s = splhigh();
660 1.3.2.1 bouyer err = extent_alloc(is->is_dvmamap, sgsize, align, boundary,
661 1.3.2.1 bouyer ((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT)|EX_BOUNDZERO,
662 1.3.2.1 bouyer (u_long *)&dvmaddr);
663 1.3.2.1 bouyer splx(s);
664 1.3.2.1 bouyer
665 1.3.2.1 bouyer if (err != 0)
666 1.3.2.1 bouyer return (err);
667 1.3.2.1 bouyer
668 1.2 eeh #ifdef DEBUG
669 1.3.2.1 bouyer if (dvmaddr == (bus_addr_t)-1)
670 1.3.2.1 bouyer {
671 1.3.2.1 bouyer printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
672 1.3.2.2 bouyer (int)sgsize, flags);
673 1.3.2.1 bouyer Debugger();
674 1.3.2.1 bouyer }
675 1.3.2.1 bouyer #endif
676 1.3.2.1 bouyer if (dvmaddr == (bus_addr_t)-1)
677 1.3.2.1 bouyer return (ENOMEM);
678 1.3.2.1 bouyer
679 1.3.2.1 bouyer /*
680 1.3.2.1 bouyer * We always use just one segment.
681 1.3.2.1 bouyer */
682 1.3.2.1 bouyer map->dm_mapsize = size;
683 1.3.2.1 bouyer map->dm_nsegs = 1;
684 1.3.2.1 bouyer map->dm_segs[0].ds_addr = dvmaddr;
685 1.3.2.1 bouyer map->dm_segs[0].ds_len = size;
686 1.3.2.1 bouyer
687 1.3.2.1 bouyer mlist = segs[0]._ds_mlist;
688 1.3.2.1 bouyer for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
689 1.3.2.1 bouyer if (sgsize == 0)
690 1.3.2.1 bouyer panic("iommu_dmamap_load_raw: size botch");
691 1.3.2.1 bouyer pa = VM_PAGE_TO_PHYS(m);
692 1.3.2.1 bouyer
693 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
694 1.3.2.1 bouyer ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
695 1.3.2.1 bouyer map, (long)dvmaddr, (long)(pa)));
696 1.3.2.1 bouyer iommu_enter(is, dvmaddr, pa, flags);
697 1.3.2.1 bouyer
698 1.3.2.1 bouyer dvmaddr += pagesz;
699 1.3.2.1 bouyer sgsize -= pagesz;
700 1.3.2.1 bouyer }
701 1.3.2.1 bouyer return (0);
702 1.3.2.1 bouyer }
703 1.3.2.1 bouyer
704 1.3.2.1 bouyer void
705 1.3.2.1 bouyer iommu_dvmamap_sync(t, is, map, offset, len, ops)
706 1.3.2.1 bouyer bus_dma_tag_t t;
707 1.3.2.1 bouyer struct iommu_state *is;
708 1.3.2.1 bouyer bus_dmamap_t map;
709 1.3.2.1 bouyer bus_addr_t offset;
710 1.3.2.1 bouyer bus_size_t len;
711 1.3.2.1 bouyer int ops;
712 1.3.2.1 bouyer {
713 1.3.2.1 bouyer vaddr_t va = map->dm_segs[0].ds_addr + offset;
714 1.3.2.1 bouyer
715 1.3.2.1 bouyer /*
716 1.3.2.1 bouyer * We only support one DMA segment; supporting more makes this code
717 1.3.2.1 bouyer * too unweildy.
718 1.3.2.1 bouyer */
719 1.3.2.1 bouyer
720 1.3.2.1 bouyer if (ops & BUS_DMASYNC_PREREAD) {
721 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
722 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
723 1.3.2.2 bouyer "BUS_DMASYNC_PREREAD\n", (void *)(u_long)va, (u_long)len));
724 1.3.2.1 bouyer
725 1.3.2.1 bouyer /* Nothing to do */;
726 1.3.2.1 bouyer }
727 1.3.2.1 bouyer if (ops & BUS_DMASYNC_POSTREAD) {
728 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
729 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
730 1.3.2.2 bouyer "BUS_DMASYNC_POSTREAD\n", (void *)(u_long)va, (u_long)len));
731 1.3.2.1 bouyer /* if we have a streaming buffer, flush it here first */
732 1.3.2.1 bouyer if (is->is_sb)
733 1.3.2.1 bouyer while (len > 0) {
734 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
735 1.3.2.1 bouyer ("iommu_dvmamap_sync: flushing va %p, %lu "
736 1.3.2.2 bouyer "bytes left\n", (void *)(u_long)va, (u_long)len));
737 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
738 1.3.2.1 bouyer (bus_space_handle_t)(u_long)
739 1.3.2.1 bouyer &is->is_sb->strbuf_pgflush, 0, va);
740 1.3.2.1 bouyer if (len <= NBPG) {
741 1.3.2.1 bouyer iommu_strbuf_flush(is);
742 1.3.2.1 bouyer len = 0;
743 1.3.2.1 bouyer } else
744 1.3.2.1 bouyer len -= NBPG;
745 1.3.2.1 bouyer va += NBPG;
746 1.3.2.1 bouyer }
747 1.3.2.1 bouyer }
748 1.3.2.1 bouyer if (ops & BUS_DMASYNC_PREWRITE) {
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.2 bouyer "BUS_DMASYNC_PREWRITE\n", (void *)(u_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 if (ops & BUS_DMASYNC_POSTWRITE) {
755 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
756 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
757 1.3.2.2 bouyer "BUS_DMASYNC_POSTWRITE\n", (void *)(u_long)va, (u_long)len));
758 1.3.2.1 bouyer /* Nothing to do */;
759 1.3.2.1 bouyer }
760 1.3.2.1 bouyer }
761 1.3.2.1 bouyer
762 1.3.2.1 bouyer int
763 1.3.2.1 bouyer iommu_dvmamem_alloc(t, is, size, alignment, boundary, segs, nsegs, rsegs, flags)
764 1.3.2.1 bouyer bus_dma_tag_t t;
765 1.3.2.1 bouyer struct iommu_state *is;
766 1.3.2.1 bouyer bus_size_t size, alignment, boundary;
767 1.3.2.1 bouyer bus_dma_segment_t *segs;
768 1.3.2.1 bouyer int nsegs;
769 1.3.2.1 bouyer int *rsegs;
770 1.3.2.1 bouyer int flags;
771 1.3.2.1 bouyer {
772 1.3.2.1 bouyer
773 1.3.2.2 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
774 1.3.2.2 bouyer "segp %p flags %d\n", (unsigned long long)size,
775 1.3.2.2 bouyer (unsigned long long)alignment, (unsigned long long)boundary,
776 1.3.2.2 bouyer segs, flags));
777 1.3.2.1 bouyer return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
778 1.3.2.1 bouyer segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
779 1.3.2.1 bouyer }
780 1.3.2.1 bouyer
781 1.3.2.1 bouyer void
782 1.3.2.1 bouyer iommu_dvmamem_free(t, is, segs, nsegs)
783 1.3.2.1 bouyer bus_dma_tag_t t;
784 1.3.2.1 bouyer struct iommu_state *is;
785 1.3.2.1 bouyer bus_dma_segment_t *segs;
786 1.3.2.1 bouyer int nsegs;
787 1.3.2.1 bouyer {
788 1.3.2.1 bouyer
789 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
790 1.3.2.1 bouyer segs, nsegs));
791 1.3.2.1 bouyer bus_dmamem_free(t->_parent, segs, nsegs);
792 1.3.2.1 bouyer }
793 1.3.2.1 bouyer
794 1.3.2.1 bouyer /*
795 1.3.2.1 bouyer * Map the DVMA mappings into the kernel pmap.
796 1.3.2.1 bouyer * Check the flags to see whether we're streaming or coherent.
797 1.3.2.1 bouyer */
798 1.3.2.1 bouyer int
799 1.3.2.1 bouyer iommu_dvmamem_map(t, is, segs, nsegs, size, kvap, flags)
800 1.3.2.1 bouyer bus_dma_tag_t t;
801 1.3.2.1 bouyer struct iommu_state *is;
802 1.3.2.1 bouyer bus_dma_segment_t *segs;
803 1.3.2.1 bouyer int nsegs;
804 1.3.2.1 bouyer size_t size;
805 1.3.2.1 bouyer caddr_t *kvap;
806 1.3.2.1 bouyer int flags;
807 1.3.2.1 bouyer {
808 1.3.2.1 bouyer vm_page_t m;
809 1.3.2.1 bouyer vaddr_t va;
810 1.3.2.1 bouyer bus_addr_t addr;
811 1.3.2.1 bouyer struct pglist *mlist;
812 1.3.2.1 bouyer int cbit;
813 1.3.2.1 bouyer
814 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
815 1.3.2.1 bouyer segs, nsegs, size));
816 1.3.2.1 bouyer
817 1.3.2.1 bouyer /*
818 1.3.2.1 bouyer * Allocate some space in the kernel map, and then map these pages
819 1.3.2.1 bouyer * into this space.
820 1.3.2.1 bouyer */
821 1.3.2.1 bouyer size = round_page(size);
822 1.3.2.1 bouyer va = uvm_km_valloc(kernel_map, size);
823 1.3.2.1 bouyer if (va == 0)
824 1.3.2.1 bouyer return (ENOMEM);
825 1.3.2.1 bouyer
826 1.3.2.1 bouyer *kvap = (caddr_t)va;
827 1.3.2.1 bouyer
828 1.3.2.1 bouyer /*
829 1.3.2.1 bouyer * digest flags:
830 1.3.2.1 bouyer */
831 1.3.2.1 bouyer cbit = 0;
832 1.3.2.1 bouyer if (flags & BUS_DMA_COHERENT) /* Disable vcache */
833 1.3.2.1 bouyer cbit |= PMAP_NVC;
834 1.3.2.1 bouyer if (flags & BUS_DMA_NOCACHE) /* sideffects */
835 1.3.2.1 bouyer cbit |= PMAP_NC;
836 1.3.2.1 bouyer
837 1.3.2.1 bouyer /*
838 1.3.2.1 bouyer * Now take this and map it into the CPU.
839 1.3.2.1 bouyer */
840 1.3.2.1 bouyer mlist = segs[0]._ds_mlist;
841 1.3.2.1 bouyer for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
842 1.3.2.1 bouyer #ifdef DIAGNOSTIC
843 1.3.2.1 bouyer if (size == 0)
844 1.3.2.1 bouyer panic("iommu_dvmamem_map: size botch");
845 1.3.2.1 bouyer #endif
846 1.3.2.1 bouyer addr = VM_PAGE_TO_PHYS(m);
847 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
848 1.3.2.2 bouyer "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
849 1.3.2.1 bouyer pmap_enter(pmap_kernel(), va, addr | cbit,
850 1.3.2.1 bouyer VM_PROT_READ | VM_PROT_WRITE,
851 1.3.2.1 bouyer VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
852 1.3.2.1 bouyer va += PAGE_SIZE;
853 1.3.2.1 bouyer size -= PAGE_SIZE;
854 1.3.2.1 bouyer }
855 1.3.2.1 bouyer
856 1.3.2.1 bouyer return (0);
857 1.3.2.1 bouyer }
858 1.3.2.1 bouyer
859 1.3.2.1 bouyer /*
860 1.3.2.1 bouyer * Unmap DVMA mappings from kernel
861 1.3.2.1 bouyer */
862 1.3.2.1 bouyer void
863 1.3.2.1 bouyer iommu_dvmamem_unmap(t, is, kva, size)
864 1.3.2.1 bouyer bus_dma_tag_t t;
865 1.3.2.1 bouyer struct iommu_state *is;
866 1.3.2.1 bouyer caddr_t kva;
867 1.3.2.1 bouyer size_t size;
868 1.3.2.1 bouyer {
869 1.3.2.1 bouyer
870 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
871 1.3.2.1 bouyer kva, size));
872 1.3.2.1 bouyer
873 1.3.2.1 bouyer #ifdef DIAGNOSTIC
874 1.3.2.1 bouyer if ((u_long)kva & PGOFSET)
875 1.3.2.1 bouyer panic("iommu_dvmamem_unmap");
876 1.3.2.1 bouyer #endif
877 1.3.2.1 bouyer
878 1.3.2.1 bouyer size = round_page(size);
879 1.3.2.1 bouyer pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
880 1.3.2.1 bouyer #if 0
881 1.3.2.1 bouyer /*
882 1.3.2.1 bouyer * XXX ? is this necessary? i think so and i think other
883 1.3.2.1 bouyer * implementations are missing it.
884 1.3.2.1 bouyer */
885 1.3.2.1 bouyer uvm_km_free(kernel_map, (vaddr_t)kva, size);
886 1.2 eeh #endif
887 1.1 mrg }
888