iommu.c revision 1.47 1 1.47 eeh /* $NetBSD: iommu.c,v 1.47 2002/02/08 20:03:45 eeh Exp $ */
2 1.7 mrg
3 1.7 mrg /*
4 1.7 mrg * Copyright (c) 1999, 2000 Matthew R. Green
5 1.7 mrg * All rights reserved.
6 1.7 mrg *
7 1.7 mrg * Redistribution and use in source and binary forms, with or without
8 1.7 mrg * modification, are permitted provided that the following conditions
9 1.7 mrg * are met:
10 1.7 mrg * 1. Redistributions of source code must retain the above copyright
11 1.7 mrg * notice, this list of conditions and the following disclaimer.
12 1.7 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.7 mrg * notice, this list of conditions and the following disclaimer in the
14 1.7 mrg * documentation and/or other materials provided with the distribution.
15 1.7 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.7 mrg * derived from this software without specific prior written permission.
17 1.7 mrg *
18 1.7 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.7 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.7 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.7 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.7 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.7 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.7 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.7 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.7 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.7 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.7 mrg * SUCH DAMAGE.
29 1.7 mrg */
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.7 mrg /*
113 1.7 mrg * UltraSPARC IOMMU support; used by both the sbus and pci code.
114 1.7 mrg */
115 1.4 mrg #include "opt_ddb.h"
116 1.4 mrg
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.41 chs #include <sys/proc.h>
123 1.18 mrg
124 1.18 mrg #include <uvm/uvm_extern.h>
125 1.1 mrg
126 1.1 mrg #include <machine/bus.h>
127 1.7 mrg #include <sparc64/sparc64/cache.h>
128 1.1 mrg #include <sparc64/dev/iommureg.h>
129 1.1 mrg #include <sparc64/dev/iommuvar.h>
130 1.1 mrg
131 1.1 mrg #include <machine/autoconf.h>
132 1.1 mrg #include <machine/cpu.h>
133 1.1 mrg
134 1.1 mrg #ifdef DEBUG
135 1.22 mrg #define IDB_BUSDMA 0x1
136 1.22 mrg #define IDB_IOMMU 0x2
137 1.22 mrg #define IDB_INFO 0x4
138 1.36 eeh #define IDB_SYNC 0x8
139 1.10 mrg int iommudebug = 0x0;
140 1.4 mrg #define DPRINTF(l, s) do { if (iommudebug & l) printf s; } while (0)
141 1.4 mrg #else
142 1.4 mrg #define DPRINTF(l, s)
143 1.1 mrg #endif
144 1.1 mrg
145 1.42 eeh #define iommu_strbuf_flush(i,v) do { \
146 1.42 eeh if ((i)->is_sb[0]) \
147 1.42 eeh bus_space_write_8((i)->is_bustag, \
148 1.42 eeh (bus_space_handle_t)(u_long) \
149 1.42 eeh &(i)->is_sb[0]->strbuf_pgflush, \
150 1.42 eeh 0, (v)); \
151 1.42 eeh if ((i)->is_sb[1]) \
152 1.42 eeh bus_space_write_8((i)->is_bustag, \
153 1.42 eeh (bus_space_handle_t)(u_long) \
154 1.42 eeh &(i)->is_sb[1]->strbuf_pgflush, \
155 1.42 eeh 0, (v)); \
156 1.42 eeh } while (0)
157 1.42 eeh
158 1.31 eeh static int iommu_strbuf_flush_done __P((struct iommu_state *));
159 1.11 eeh
160 1.1 mrg /*
161 1.1 mrg * initialise the UltraSPARC IOMMU (SBUS or PCI):
162 1.1 mrg * - allocate and setup the iotsb.
163 1.1 mrg * - enable the IOMMU
164 1.7 mrg * - initialise the streaming buffers (if they exist)
165 1.1 mrg * - create a private DVMA map.
166 1.1 mrg */
167 1.1 mrg void
168 1.36 eeh iommu_init(name, is, tsbsize, iovabase)
169 1.1 mrg char *name;
170 1.1 mrg struct iommu_state *is;
171 1.1 mrg int tsbsize;
172 1.36 eeh u_int32_t iovabase;
173 1.1 mrg {
174 1.11 eeh psize_t size;
175 1.11 eeh vaddr_t va;
176 1.11 eeh paddr_t pa;
177 1.35 chs struct vm_page *m;
178 1.11 eeh struct pglist mlist;
179 1.1 mrg
180 1.1 mrg /*
181 1.1 mrg * Setup the iommu.
182 1.1 mrg *
183 1.45 eeh * The sun4u iommu is part of the SBUS or PCI controller so we will
184 1.45 eeh * deal with it here..
185 1.1 mrg *
186 1.45 eeh * For sysio and psycho/psycho+ the IOMMU address space always ends at
187 1.45 eeh * 0xffffe000, but the starting address depends on the size of the
188 1.45 eeh * map. The map size is 1024 * 2 ^ is->is_tsbsize entries, where each
189 1.45 eeh * entry is 8 bytes. The start of the map can be calculated by
190 1.45 eeh * (0xffffe000 << (8 + is->is_tsbsize)).
191 1.45 eeh *
192 1.45 eeh * But sabre and hummingbird use a different scheme that seems to
193 1.45 eeh * be hard-wired, so we read the start and size from the PROM and
194 1.45 eeh * just use those values.
195 1.2 eeh */
196 1.11 eeh is->is_cr = (tsbsize << 16) | IOMMUCR_EN;
197 1.11 eeh is->is_tsbsize = tsbsize;
198 1.45 eeh if (iovabase == -1) {
199 1.45 eeh is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize);
200 1.45 eeh is->is_dvmaend = IOTSB_VEND;
201 1.45 eeh } else {
202 1.45 eeh is->is_dvmabase = iovabase;
203 1.45 eeh is->is_dvmaend = iovabase + IOTSB_VSIZE(tsbsize);
204 1.45 eeh }
205 1.11 eeh
206 1.11 eeh /*
207 1.15 eeh * Allocate memory for I/O pagetables. They need to be physically
208 1.15 eeh * contiguous.
209 1.11 eeh */
210 1.11 eeh
211 1.11 eeh size = NBPG<<(is->is_tsbsize);
212 1.11 eeh TAILQ_INIT(&mlist);
213 1.11 eeh if (uvm_pglistalloc((psize_t)size, (paddr_t)0, (paddr_t)-1,
214 1.11 eeh (paddr_t)NBPG, (paddr_t)0, &mlist, 1, 0) != 0)
215 1.11 eeh panic("iommu_init: no memory");
216 1.11 eeh
217 1.11 eeh va = uvm_km_valloc(kernel_map, size);
218 1.11 eeh if (va == 0)
219 1.11 eeh panic("iommu_init: no memory");
220 1.11 eeh is->is_tsb = (int64_t *)va;
221 1.11 eeh
222 1.11 eeh m = TAILQ_FIRST(&mlist);
223 1.11 eeh is->is_ptsb = VM_PAGE_TO_PHYS(m);
224 1.11 eeh
225 1.11 eeh /* Map the pages */
226 1.11 eeh for (; m != NULL; m = TAILQ_NEXT(m,pageq)) {
227 1.11 eeh pa = VM_PAGE_TO_PHYS(m);
228 1.11 eeh pmap_enter(pmap_kernel(), va, pa | PMAP_NVC,
229 1.11 eeh VM_PROT_READ|VM_PROT_WRITE,
230 1.11 eeh VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
231 1.11 eeh va += NBPG;
232 1.11 eeh }
233 1.38 chris pmap_update(pmap_kernel());
234 1.11 eeh bzero(is->is_tsb, size);
235 1.1 mrg
236 1.1 mrg #ifdef DEBUG
237 1.22 mrg if (iommudebug & IDB_INFO)
238 1.1 mrg {
239 1.1 mrg /* Probe the iommu */
240 1.1 mrg struct iommureg *regs = is->is_iommu;
241 1.1 mrg
242 1.25 mrg printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n",
243 1.25 mrg (u_long)®s->iommu_cr,
244 1.25 mrg (u_long)®s->iommu_tsb,
245 1.25 mrg (u_long)®s->iommu_flush);
246 1.25 mrg printf("iommu cr=%llx tsb=%llx\n", (unsigned long long)regs->iommu_cr, (unsigned long long)regs->iommu_tsb);
247 1.25 mrg printf("TSB base %p phys %llx\n", (void *)is->is_tsb, (unsigned long long)is->is_ptsb);
248 1.1 mrg delay(1000000); /* 1 s */
249 1.1 mrg }
250 1.1 mrg #endif
251 1.1 mrg
252 1.1 mrg /*
253 1.8 mrg * Initialize streaming buffer, if it is there.
254 1.1 mrg */
255 1.42 eeh if (is->is_sb[0] || is->is_sb[1])
256 1.42 eeh (void)pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush[0],
257 1.8 mrg (paddr_t *)&is->is_flushpa);
258 1.1 mrg
259 1.1 mrg /*
260 1.1 mrg * now actually start up the IOMMU
261 1.1 mrg */
262 1.1 mrg iommu_reset(is);
263 1.1 mrg
264 1.1 mrg /*
265 1.1 mrg * Now all the hardware's working we need to allocate a dvma map.
266 1.1 mrg */
267 1.11 eeh printf("DVMA map: %x to %x\n",
268 1.11 eeh (unsigned int)is->is_dvmabase,
269 1.45 eeh (unsigned int)is->is_dvmaend);
270 1.47 eeh printf("IOTSB: %llx to %llx\n",
271 1.47 eeh (unsigned long long)is->is_ptsb,
272 1.47 eeh (unsigned long long)(is->is_ptsb + size));
273 1.1 mrg is->is_dvmamap = extent_create(name,
274 1.45 eeh is->is_dvmabase, is->is_dvmaend - NBPG,
275 1.1 mrg M_DEVBUF, 0, 0, EX_NOWAIT);
276 1.1 mrg }
277 1.1 mrg
278 1.8 mrg /*
279 1.8 mrg * Streaming buffers don't exist on the UltraSPARC IIi; we should have
280 1.8 mrg * detected that already and disabled them. If not, we will notice that
281 1.8 mrg * they aren't there when the STRBUF_EN bit does not remain.
282 1.8 mrg */
283 1.1 mrg void
284 1.1 mrg iommu_reset(is)
285 1.1 mrg struct iommu_state *is;
286 1.1 mrg {
287 1.45 eeh struct iommu_strbuf *sb;
288 1.45 eeh int i;
289 1.1 mrg
290 1.1 mrg /* Need to do 64-bit stores */
291 1.21 eeh bus_space_write_8(is->is_bustag,
292 1.42 eeh (bus_space_handle_t)(u_long)&is->is_iommu->iommu_tsb,
293 1.42 eeh 0, is->is_ptsb);
294 1.11 eeh /* Enable IOMMU in diagnostic mode */
295 1.21 eeh bus_space_write_8(is->is_bustag,
296 1.42 eeh (bus_space_handle_t)(u_long)&is->is_iommu->iommu_cr,
297 1.42 eeh 0, is->is_cr|IOMMUCR_DE);
298 1.11 eeh
299 1.45 eeh for (i=0; i<2; i++) {
300 1.45 eeh if ((sb = is->is_sb[i]) != NULL) {
301 1.5 mrg
302 1.45 eeh /* Enable diagnostics mode? */
303 1.45 eeh bus_space_write_8(is->is_bustag,
304 1.45 eeh (bus_space_handle_t)(u_long)&sb->strbuf_ctl,
305 1.45 eeh 0, STRBUF_EN);
306 1.45 eeh
307 1.45 eeh /* No streaming buffers? Disable them */
308 1.45 eeh if (bus_space_read_8(is->is_bustag,
309 1.45 eeh (bus_space_handle_t)(u_long)&sb->strbuf_ctl,
310 1.45 eeh 0) == 0)
311 1.45 eeh is->is_sb[i] = 0;
312 1.45 eeh }
313 1.42 eeh }
314 1.2 eeh }
315 1.2 eeh
316 1.2 eeh /*
317 1.2 eeh * Here are the iommu control routines.
318 1.2 eeh */
319 1.2 eeh void
320 1.2 eeh iommu_enter(is, va, pa, flags)
321 1.2 eeh struct iommu_state *is;
322 1.2 eeh vaddr_t va;
323 1.2 eeh int64_t pa;
324 1.2 eeh int flags;
325 1.2 eeh {
326 1.2 eeh int64_t tte;
327 1.2 eeh
328 1.2 eeh #ifdef DIAGNOSTIC
329 1.45 eeh if (va < is->is_dvmabase || va > is->is_dvmaend)
330 1.13 mrg panic("iommu_enter: va %#lx not in DVMA space", va);
331 1.2 eeh #endif
332 1.2 eeh
333 1.2 eeh tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
334 1.32 thorpej (flags&BUS_DMA_STREAMING));
335 1.45 eeh tte |= (flags & 0xff000LL)<<(4*8);/* DEBUG */
336 1.2 eeh
337 1.2 eeh /* Is the streamcache flush really needed? */
338 1.42 eeh if (is->is_sb[0] || is->is_sb[1]) {
339 1.31 eeh iommu_strbuf_flush(is, va);
340 1.31 eeh iommu_strbuf_flush_done(is);
341 1.5 mrg }
342 1.22 mrg DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
343 1.25 mrg (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va));
344 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
345 1.21 eeh bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
346 1.21 eeh &is->is_iommu->iommu_flush, 0, va);
347 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
348 1.25 mrg va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize),
349 1.25 mrg (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
350 1.25 mrg (u_long)tte));
351 1.39 eeh }
352 1.39 eeh
353 1.39 eeh
354 1.39 eeh /*
355 1.39 eeh * Find the value of a DVMA address (debug routine).
356 1.39 eeh */
357 1.39 eeh paddr_t
358 1.39 eeh iommu_extract(is, dva)
359 1.39 eeh struct iommu_state *is;
360 1.39 eeh vaddr_t dva;
361 1.39 eeh {
362 1.39 eeh int64_t tte = 0;
363 1.39 eeh
364 1.45 eeh if (dva >= is->is_dvmabase && dva < is->is_dvmaend)
365 1.39 eeh tte = is->is_tsb[IOTSBSLOT(dva,is->is_tsbsize)];
366 1.39 eeh
367 1.39 eeh if ((tte&IOTTE_V) == 0)
368 1.39 eeh return ((paddr_t)-1L);
369 1.39 eeh return (tte&IOTTE_PAMASK);
370 1.2 eeh }
371 1.2 eeh
372 1.2 eeh /*
373 1.2 eeh * iommu_remove: removes mappings created by iommu_enter
374 1.2 eeh *
375 1.2 eeh * Only demap from IOMMU if flag is set.
376 1.8 mrg *
377 1.8 mrg * XXX: this function needs better internal error checking.
378 1.2 eeh */
379 1.2 eeh void
380 1.2 eeh iommu_remove(is, va, len)
381 1.2 eeh struct iommu_state *is;
382 1.2 eeh vaddr_t va;
383 1.2 eeh size_t len;
384 1.2 eeh {
385 1.2 eeh
386 1.2 eeh #ifdef DIAGNOSTIC
387 1.45 eeh if (va < is->is_dvmabase || va > is->is_dvmaend)
388 1.25 mrg panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va);
389 1.2 eeh if ((long)(va + len) < (long)va)
390 1.4 mrg panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
391 1.2 eeh (long) va, (long) len);
392 1.2 eeh if (len & ~0xfffffff)
393 1.25 mrg panic("iommu_remove: rediculous len 0x%lx", (u_long)len);
394 1.2 eeh #endif
395 1.2 eeh
396 1.2 eeh va = trunc_page(va);
397 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
398 1.25 mrg va, (u_long)IOTSBSLOT(va,is->is_tsbsize),
399 1.8 mrg &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]));
400 1.2 eeh while (len > 0) {
401 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d for va %p size %lx\n",
402 1.25 mrg (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va, (u_long)len));
403 1.42 eeh if (is->is_sb[0] || is->is_sb[0]) {
404 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
405 1.25 mrg (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
406 1.25 mrg (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
407 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
408 1.4 mrg (u_long)len));
409 1.31 eeh iommu_strbuf_flush(is, va);
410 1.10 mrg if (len <= NBPG)
411 1.31 eeh iommu_strbuf_flush_done(is);
412 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
413 1.25 mrg (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
414 1.25 mrg (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
415 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
416 1.4 mrg (u_long)len));
417 1.36 eeh }
418 1.10 mrg
419 1.10 mrg if (len <= NBPG)
420 1.10 mrg len = 0;
421 1.10 mrg else
422 1.8 mrg len -= NBPG;
423 1.8 mrg
424 1.47 eeh /* XXX Zero-ing the entry would not require RMW */
425 1.47 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] &= ~IOTTE_V;
426 1.21 eeh bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
427 1.21 eeh &is->is_iommu->iommu_flush, 0, va);
428 1.2 eeh va += NBPG;
429 1.2 eeh }
430 1.2 eeh }
431 1.2 eeh
432 1.14 mrg static int
433 1.31 eeh iommu_strbuf_flush_done(is)
434 1.2 eeh struct iommu_state *is;
435 1.2 eeh {
436 1.2 eeh struct timeval cur, flushtimeout;
437 1.2 eeh
438 1.2 eeh #define BUMPTIME(t, usec) { \
439 1.2 eeh register volatile struct timeval *tp = (t); \
440 1.2 eeh register long us; \
441 1.2 eeh \
442 1.2 eeh tp->tv_usec = us = tp->tv_usec + (usec); \
443 1.2 eeh if (us >= 1000000) { \
444 1.2 eeh tp->tv_usec = us - 1000000; \
445 1.2 eeh tp->tv_sec++; \
446 1.2 eeh } \
447 1.2 eeh }
448 1.5 mrg
449 1.42 eeh if (!is->is_sb[0] && !is->is_sb[1])
450 1.5 mrg return (0);
451 1.7 mrg
452 1.7 mrg /*
453 1.7 mrg * Streaming buffer flushes:
454 1.7 mrg *
455 1.7 mrg * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
456 1.7 mrg * we're not on a cache line boundary (64-bits):
457 1.7 mrg * 2 Store 0 in flag
458 1.7 mrg * 3 Store pointer to flag in flushsync
459 1.7 mrg * 4 wait till flushsync becomes 0x1
460 1.7 mrg *
461 1.7 mrg * If it takes more than .5 sec, something
462 1.7 mrg * went wrong.
463 1.7 mrg */
464 1.2 eeh
465 1.42 eeh is->is_flush[0] = 1;
466 1.42 eeh is->is_flush[1] = 1;
467 1.42 eeh if (is->is_sb[0]) {
468 1.42 eeh is->is_flush[0] = 0;
469 1.42 eeh bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
470 1.42 eeh &is->is_sb[0]->strbuf_flushsync, 0, is->is_flushpa);
471 1.42 eeh }
472 1.42 eeh if (is->is_sb[1]) {
473 1.42 eeh is->is_flush[0] = 1;
474 1.42 eeh bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
475 1.42 eeh &is->is_sb[1]->strbuf_flushsync, 0, is->is_flushpa + 8);
476 1.42 eeh }
477 1.2 eeh
478 1.2 eeh microtime(&flushtimeout);
479 1.2 eeh cur = flushtimeout;
480 1.2 eeh BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
481 1.2 eeh
482 1.42 eeh DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flush = %lx,%lx "
483 1.42 eeh "at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
484 1.42 eeh (long)is->is_flush[0], (long)is->is_flush[1],
485 1.42 eeh (long)&is->is_flush[0], (long)is->is_flushpa,
486 1.42 eeh cur.tv_sec, cur.tv_usec,
487 1.42 eeh flushtimeout.tv_sec, flushtimeout.tv_usec));
488 1.42 eeh
489 1.2 eeh /* Bypass non-coherent D$ */
490 1.42 eeh while ((!ldxa(is->is_flushpa, ASI_PHYS_CACHED) ||
491 1.42 eeh !ldxa(is->is_flushpa + 8, ASI_PHYS_CACHED)) &&
492 1.42 eeh ((cur.tv_sec <= flushtimeout.tv_sec) &&
493 1.42 eeh (cur.tv_usec <= flushtimeout.tv_usec)))
494 1.2 eeh microtime(&cur);
495 1.2 eeh
496 1.2 eeh #ifdef DIAGNOSTIC
497 1.44 thorpej if (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) ||
498 1.44 thorpej !ldxa(is->is_flushpa + 8, ASI_PHYS_CACHED)) {
499 1.42 eeh printf("iommu_strbuf_flush_done: flush timeout %p,%p at %p\n",
500 1.42 eeh (void *)(u_long)is->is_flush[0],
501 1.42 eeh (void *)(u_long)is->is_flush[1],
502 1.42 eeh (void *)(u_long)is->is_flushpa); /* panic? */
503 1.2 eeh #ifdef DDB
504 1.2 eeh Debugger();
505 1.2 eeh #endif
506 1.2 eeh }
507 1.2 eeh #endif
508 1.31 eeh DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flushed\n"));
509 1.42 eeh return (is->is_flush[0] && is->is_flush[1]);
510 1.7 mrg }
511 1.7 mrg
512 1.7 mrg /*
513 1.7 mrg * IOMMU DVMA operations, common to SBUS and PCI.
514 1.7 mrg */
515 1.7 mrg int
516 1.7 mrg iommu_dvmamap_load(t, is, map, buf, buflen, p, flags)
517 1.7 mrg bus_dma_tag_t t;
518 1.7 mrg struct iommu_state *is;
519 1.7 mrg bus_dmamap_t map;
520 1.7 mrg void *buf;
521 1.7 mrg bus_size_t buflen;
522 1.7 mrg struct proc *p;
523 1.7 mrg int flags;
524 1.7 mrg {
525 1.7 mrg int s;
526 1.7 mrg int err;
527 1.7 mrg bus_size_t sgsize;
528 1.7 mrg paddr_t curaddr;
529 1.40 eeh u_long dvmaddr, sgstart, sgend;
530 1.21 eeh bus_size_t align, boundary;
531 1.7 mrg vaddr_t vaddr = (vaddr_t)buf;
532 1.40 eeh int seg;
533 1.7 mrg pmap_t pmap;
534 1.7 mrg
535 1.7 mrg if (map->dm_nsegs) {
536 1.7 mrg /* Already in use?? */
537 1.7 mrg #ifdef DIAGNOSTIC
538 1.7 mrg printf("iommu_dvmamap_load: map still in use\n");
539 1.7 mrg #endif
540 1.7 mrg bus_dmamap_unload(t, map);
541 1.7 mrg }
542 1.7 mrg /*
543 1.7 mrg * Make sure that on error condition we return "no valid mappings".
544 1.7 mrg */
545 1.7 mrg map->dm_nsegs = 0;
546 1.7 mrg
547 1.7 mrg if (buflen > map->_dm_size) {
548 1.22 mrg DPRINTF(IDB_BUSDMA,
549 1.7 mrg ("iommu_dvmamap_load(): error %d > %d -- "
550 1.25 mrg "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
551 1.7 mrg return (EINVAL);
552 1.7 mrg }
553 1.7 mrg
554 1.7 mrg sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
555 1.20 mrg
556 1.7 mrg /*
557 1.21 eeh * A boundary presented to bus_dmamem_alloc() takes precedence
558 1.21 eeh * over boundary in the map.
559 1.7 mrg */
560 1.21 eeh if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
561 1.21 eeh boundary = map->_dm_boundary;
562 1.21 eeh align = max(map->dm_segs[0]._ds_align, NBPG);
563 1.7 mrg s = splhigh();
564 1.40 eeh /*
565 1.40 eeh * If our segment size is larger than the boundary we need to
566 1.40 eeh * split the transfer up int little pieces ourselves.
567 1.40 eeh */
568 1.40 eeh err = extent_alloc(is->is_dvmamap, sgsize, align,
569 1.40 eeh (sgsize > boundary) ? 0 : boundary,
570 1.40 eeh EX_NOWAIT|EX_BOUNDZERO, (u_long *)&dvmaddr);
571 1.7 mrg splx(s);
572 1.7 mrg
573 1.7 mrg #ifdef DEBUG
574 1.11 eeh if (err || (dvmaddr == (bus_addr_t)-1))
575 1.7 mrg {
576 1.7 mrg printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
577 1.25 mrg (int)sgsize, flags);
578 1.40 eeh #ifdef DDB
579 1.7 mrg Debugger();
580 1.40 eeh #endif
581 1.7 mrg }
582 1.7 mrg #endif
583 1.11 eeh if (err != 0)
584 1.11 eeh return (err);
585 1.11 eeh
586 1.7 mrg if (dvmaddr == (bus_addr_t)-1)
587 1.7 mrg return (ENOMEM);
588 1.7 mrg
589 1.40 eeh /* Set the active DVMA map */
590 1.40 eeh map->_dm_dvmastart = dvmaddr;
591 1.40 eeh map->_dm_dvmasize = sgsize;
592 1.40 eeh
593 1.40 eeh /*
594 1.40 eeh * Now split the DVMA range into segments, not crossing
595 1.40 eeh * the boundary.
596 1.40 eeh */
597 1.40 eeh seg = 0;
598 1.40 eeh sgstart = dvmaddr + (vaddr & PGOFSET);
599 1.40 eeh sgend = sgstart + buflen - 1;
600 1.40 eeh map->dm_segs[seg].ds_addr = sgstart;
601 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: boundary %lx boundary-1 %lx "
602 1.40 eeh "~(boundary-1) %lx\n", boundary, (boundary-1), ~(boundary-1)));
603 1.40 eeh while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
604 1.40 eeh /* Oops. We crossed a boundary. Split the xfer. */
605 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
606 1.40 eeh "seg %d start %lx size %lx\n", seg,
607 1.40 eeh map->dm_segs[seg].ds_addr, map->dm_segs[seg].ds_len));
608 1.40 eeh map->dm_segs[seg].ds_len = sgstart & (boundary - 1);
609 1.40 eeh if (++seg > map->_dm_segcnt) {
610 1.40 eeh /* Too many segments. Fail the operation. */
611 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
612 1.40 eeh "too many segments %d\n", seg));
613 1.40 eeh s = splhigh();
614 1.40 eeh /* How can this fail? And if it does what can we do? */
615 1.40 eeh err = extent_free(is->is_dvmamap,
616 1.40 eeh dvmaddr, sgsize, EX_NOWAIT);
617 1.40 eeh map->_dm_dvmastart = 0;
618 1.40 eeh map->_dm_dvmasize = 0;
619 1.43 eeh splx(s);
620 1.40 eeh return (E2BIG);
621 1.40 eeh }
622 1.40 eeh sgstart = roundup(sgstart, boundary);
623 1.40 eeh map->dm_segs[seg].ds_addr = sgstart;
624 1.40 eeh }
625 1.40 eeh map->dm_segs[seg].ds_len = sgend - sgstart + 1;
626 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
627 1.40 eeh "seg %d start %lx size %lx\n", seg,
628 1.40 eeh map->dm_segs[seg].ds_addr, map->dm_segs[seg].ds_len));
629 1.40 eeh map->dm_nsegs = seg+1;
630 1.7 mrg map->dm_mapsize = buflen;
631 1.7 mrg
632 1.7 mrg if (p != NULL)
633 1.7 mrg pmap = p->p_vmspace->vm_map.pmap;
634 1.7 mrg else
635 1.7 mrg pmap = pmap_kernel();
636 1.7 mrg
637 1.7 mrg for (; buflen > 0; ) {
638 1.7 mrg /*
639 1.7 mrg * Get the physical address for this page.
640 1.7 mrg */
641 1.7 mrg if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
642 1.7 mrg bus_dmamap_unload(t, map);
643 1.7 mrg return (-1);
644 1.7 mrg }
645 1.7 mrg
646 1.7 mrg /*
647 1.7 mrg * Compute the segment size, and adjust counts.
648 1.7 mrg */
649 1.7 mrg sgsize = NBPG - ((u_long)vaddr & PGOFSET);
650 1.7 mrg if (buflen < sgsize)
651 1.7 mrg sgsize = buflen;
652 1.7 mrg
653 1.22 mrg DPRINTF(IDB_BUSDMA,
654 1.36 eeh ("iommu_dvmamap_load: map %p loading va %p "
655 1.36 eeh "dva %lx at pa %lx\n",
656 1.36 eeh map, (void *)vaddr, (long)dvmaddr,
657 1.36 eeh (long)(curaddr&~(NBPG-1))));
658 1.7 mrg iommu_enter(is, trunc_page(dvmaddr), trunc_page(curaddr),
659 1.45 eeh flags|0x4000);
660 1.7 mrg
661 1.7 mrg dvmaddr += PAGE_SIZE;
662 1.7 mrg vaddr += sgsize;
663 1.7 mrg buflen -= sgsize;
664 1.7 mrg }
665 1.45 eeh #ifdef DIAGNOSTIC
666 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
667 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
668 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
669 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
670 1.45 eeh seg, map->dm_segs[seg].ds_addr,
671 1.45 eeh is->is_dvmabase, is->is_dvmaend);
672 1.45 eeh Debugger();
673 1.45 eeh }
674 1.45 eeh }
675 1.45 eeh #endif
676 1.7 mrg return (0);
677 1.7 mrg }
678 1.7 mrg
679 1.7 mrg
680 1.7 mrg void
681 1.7 mrg iommu_dvmamap_unload(t, is, map)
682 1.7 mrg bus_dma_tag_t t;
683 1.7 mrg struct iommu_state *is;
684 1.7 mrg bus_dmamap_t map;
685 1.7 mrg {
686 1.40 eeh int error, s;
687 1.7 mrg bus_size_t sgsize;
688 1.7 mrg
689 1.40 eeh /* Flush the iommu */
690 1.40 eeh #ifdef DEBUG
691 1.40 eeh if (!map->_dm_dvmastart) {
692 1.40 eeh printf("iommu_dvmamap_unload: No dvmastart is zero\n");
693 1.40 eeh #ifdef DDB
694 1.40 eeh Debugger();
695 1.40 eeh #endif
696 1.40 eeh }
697 1.40 eeh #endif
698 1.40 eeh iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize);
699 1.7 mrg
700 1.23 eeh /* Flush the caches */
701 1.23 eeh bus_dmamap_unload(t->_parent, map);
702 1.23 eeh
703 1.7 mrg /* Mark the mappings as invalid. */
704 1.7 mrg map->dm_mapsize = 0;
705 1.7 mrg map->dm_nsegs = 0;
706 1.7 mrg
707 1.7 mrg s = splhigh();
708 1.40 eeh error = extent_free(is->is_dvmamap, map->_dm_dvmastart,
709 1.40 eeh map->_dm_dvmasize, EX_NOWAIT);
710 1.43 eeh map->_dm_dvmastart = 0;
711 1.43 eeh map->_dm_dvmasize = 0;
712 1.7 mrg splx(s);
713 1.7 mrg if (error != 0)
714 1.7 mrg printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
715 1.40 eeh
716 1.40 eeh /* Clear the map */
717 1.9 eeh }
718 1.9 eeh
719 1.9 eeh
720 1.9 eeh int
721 1.22 mrg iommu_dvmamap_load_raw(t, is, map, segs, nsegs, flags, size)
722 1.9 eeh bus_dma_tag_t t;
723 1.9 eeh struct iommu_state *is;
724 1.9 eeh bus_dmamap_t map;
725 1.9 eeh bus_dma_segment_t *segs;
726 1.9 eeh int nsegs;
727 1.22 mrg int flags;
728 1.9 eeh bus_size_t size;
729 1.9 eeh {
730 1.35 chs struct vm_page *m;
731 1.40 eeh int i, j, s;
732 1.26 martin int left;
733 1.9 eeh int err;
734 1.9 eeh bus_size_t sgsize;
735 1.9 eeh paddr_t pa;
736 1.21 eeh bus_size_t boundary, align;
737 1.40 eeh u_long dvmaddr, sgstart, sgend;
738 1.9 eeh struct pglist *mlist;
739 1.9 eeh int pagesz = PAGE_SIZE;
740 1.45 eeh int npg = 0; /* DEBUG */
741 1.9 eeh
742 1.9 eeh if (map->dm_nsegs) {
743 1.9 eeh /* Already in use?? */
744 1.9 eeh #ifdef DIAGNOSTIC
745 1.9 eeh printf("iommu_dvmamap_load_raw: map still in use\n");
746 1.9 eeh #endif
747 1.9 eeh bus_dmamap_unload(t, map);
748 1.9 eeh }
749 1.40 eeh
750 1.40 eeh /*
751 1.40 eeh * A boundary presented to bus_dmamem_alloc() takes precedence
752 1.40 eeh * over boundary in the map.
753 1.40 eeh */
754 1.40 eeh if ((boundary = segs[0]._ds_boundary) == 0)
755 1.40 eeh boundary = map->_dm_boundary;
756 1.40 eeh
757 1.45 eeh align = max(segs[0]._ds_align, pagesz);
758 1.40 eeh
759 1.9 eeh /*
760 1.9 eeh * Make sure that on error condition we return "no valid mappings".
761 1.9 eeh */
762 1.9 eeh map->dm_nsegs = 0;
763 1.26 martin /* Count up the total number of pages we need */
764 1.26 martin pa = segs[0].ds_addr;
765 1.26 martin sgsize = 0;
766 1.40 eeh left = size;
767 1.40 eeh for (i=0; left && i<nsegs; i++) {
768 1.26 martin if (round_page(pa) != round_page(segs[i].ds_addr))
769 1.26 martin sgsize = round_page(sgsize);
770 1.40 eeh sgsize += min(left, segs[i].ds_len);
771 1.40 eeh left -= segs[i].ds_len;
772 1.26 martin pa = segs[i].ds_addr + segs[i].ds_len;
773 1.26 martin }
774 1.26 martin sgsize = round_page(sgsize);
775 1.9 eeh
776 1.40 eeh s = splhigh();
777 1.40 eeh /*
778 1.40 eeh * If our segment size is larger than the boundary we need to
779 1.45 eeh * split the transfer up into little pieces ourselves.
780 1.9 eeh */
781 1.40 eeh err = extent_alloc(is->is_dvmamap, sgsize, align,
782 1.40 eeh (sgsize > boundary) ? 0 : boundary,
783 1.40 eeh ((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT) |
784 1.40 eeh EX_BOUNDZERO, (u_long *)&dvmaddr);
785 1.9 eeh splx(s);
786 1.9 eeh
787 1.9 eeh if (err != 0)
788 1.9 eeh return (err);
789 1.9 eeh
790 1.9 eeh #ifdef DEBUG
791 1.9 eeh if (dvmaddr == (bus_addr_t)-1)
792 1.9 eeh {
793 1.9 eeh printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
794 1.25 mrg (int)sgsize, flags);
795 1.9 eeh Debugger();
796 1.9 eeh }
797 1.9 eeh #endif
798 1.9 eeh if (dvmaddr == (bus_addr_t)-1)
799 1.9 eeh return (ENOMEM);
800 1.9 eeh
801 1.40 eeh /* Set the active DVMA map */
802 1.40 eeh map->_dm_dvmastart = dvmaddr;
803 1.40 eeh map->_dm_dvmasize = sgsize;
804 1.40 eeh
805 1.26 martin if ((mlist = segs[0]._ds_mlist) == NULL) {
806 1.26 martin u_long prev_va = NULL;
807 1.45 eeh paddr_t prev_pa = 0;
808 1.45 eeh int end = 0, offset;
809 1.45 eeh
810 1.26 martin /*
811 1.45 eeh * This segs is made up of individual physical
812 1.45 eeh * segments, probably by _bus_dmamap_load_uio() or
813 1.26 martin * _bus_dmamap_load_mbuf(). Ignore the mlist and
814 1.45 eeh * load each one individually.
815 1.26 martin */
816 1.40 eeh map->dm_mapsize = size;
817 1.40 eeh
818 1.45 eeh j = 0;
819 1.45 eeh for (i = 0; i < nsegs ; i++) {
820 1.40 eeh
821 1.45 eeh pa = segs[i].ds_addr;
822 1.45 eeh offset = (pa & PGOFSET);
823 1.45 eeh pa = trunc_page(pa);
824 1.45 eeh dvmaddr = trunc_page(dvmaddr);
825 1.45 eeh left = min(size, segs[i].ds_len);
826 1.45 eeh
827 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: converting "
828 1.45 eeh "physseg %d start %lx size %lx\n", i,
829 1.45 eeh segs[i].ds_addr, segs[i].ds_len));
830 1.26 martin
831 1.47 eeh if ((pa == prev_pa) &&
832 1.47 eeh ((offset != 0) || (end != offset))) {
833 1.45 eeh /* We can re-use this mapping */
834 1.45 eeh #ifdef DEBUG
835 1.45 eeh if (iommudebug & 0x10) printf("reusing dva %lx prev %lx pa %lx prev %lx\n",
836 1.45 eeh dvmaddr, prev_va, pa, prev_pa);
837 1.45 eeh #endif
838 1.45 eeh dvmaddr = prev_va;
839 1.45 eeh }
840 1.29 martin
841 1.45 eeh sgstart = dvmaddr + offset;
842 1.45 eeh sgend = sgstart + left - 1;
843 1.26 martin
844 1.45 eeh /* Are the segments virtually adjacent? */
845 1.45 eeh if ((end == offset) &&
846 1.45 eeh ((offset == 0) || (pa == prev_pa))) {
847 1.45 eeh /* Just append to the previous segment. */
848 1.45 eeh #ifdef DEBUG
849 1.45 eeh if (iommudebug & 0x10) {
850 1.45 eeh printf("appending: offset %x pa %lx prev %lx dva %lx prev %lx\n",
851 1.45 eeh offset, pa, prev_pa, dvmaddr, prev_va);
852 1.45 eeh }
853 1.45 eeh #endif
854 1.40 eeh
855 1.45 eeh map->dm_segs[--j].ds_len += left;
856 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
857 1.45 eeh "appending seg %d start %lx size %lx\n", j,
858 1.45 eeh map->dm_segs[j].ds_addr,
859 1.45 eeh map->dm_segs[j].ds_len));
860 1.45 eeh } else {
861 1.45 eeh map->dm_segs[j].ds_addr = sgstart;
862 1.45 eeh map->dm_segs[j].ds_len = left;
863 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
864 1.45 eeh "seg %d start %lx size %lx\n", j,
865 1.45 eeh map->dm_segs[j].ds_addr,
866 1.45 eeh map->dm_segs[j].ds_len));
867 1.40 eeh }
868 1.45 eeh end = (offset + left) & PGOFSET;
869 1.40 eeh
870 1.40 eeh /* Check for boundary issues */
871 1.40 eeh while ((sgstart & ~(boundary - 1)) !=
872 1.40 eeh (sgend & ~(boundary - 1))) {
873 1.40 eeh /* Need a new segment. */
874 1.40 eeh map->dm_segs[j].ds_len =
875 1.40 eeh sgstart & (boundary - 1);
876 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
877 1.40 eeh "seg %d start %lx size %lx\n", j,
878 1.40 eeh map->dm_segs[j].ds_addr,
879 1.40 eeh map->dm_segs[j].ds_len));
880 1.40 eeh if (++j > map->_dm_segcnt) {
881 1.40 eeh iommu_dvmamap_unload(t, is, map);
882 1.40 eeh return (E2BIG);
883 1.40 eeh }
884 1.40 eeh sgstart = roundup(sgstart, boundary);
885 1.40 eeh map->dm_segs[j].ds_addr = sgstart;
886 1.40 eeh map->dm_segs[j].ds_len = sgend - sgstart + 1;
887 1.40 eeh }
888 1.40 eeh
889 1.26 martin if (sgsize == 0)
890 1.26 martin panic("iommu_dmamap_load_raw: size botch");
891 1.40 eeh
892 1.45 eeh /* Now map a series of pages. */
893 1.45 eeh while (dvmaddr < sgend) {
894 1.45 eeh DPRINTF(IDB_BUSDMA,
895 1.45 eeh ("iommu_dvmamap_load_raw: map %p "
896 1.45 eeh "loading va %lx at pa %lx\n",
897 1.45 eeh map, (long)dvmaddr,
898 1.45 eeh (long)(pa)));
899 1.45 eeh /* Enter it if we haven't before. */
900 1.46 eeh if (prev_va != dvmaddr)
901 1.45 eeh #ifdef DEBUG
902 1.46 eeh { if (iommudebug & 0x10) printf("seg %d:%d entering dvma %lx, prev %lx pa %lx\n", i,j, dvmaddr, prev_va, pa);
903 1.45 eeh #endif
904 1.45 eeh iommu_enter(is, prev_va = dvmaddr,
905 1.45 eeh prev_pa = pa,
906 1.45 eeh flags|(++npg<<12));
907 1.45 eeh #ifdef DEBUG
908 1.45 eeh } else if (iommudebug & 0x10) printf("seg %d:%d skipping dvma %lx, prev %lx\n", i,j, dvmaddr, prev_va);
909 1.45 eeh #endif
910 1.45 eeh
911 1.45 eeh dvmaddr += pagesz;
912 1.45 eeh pa += pagesz;
913 1.45 eeh }
914 1.45 eeh
915 1.45 eeh size -= left;
916 1.45 eeh ++j;
917 1.26 martin }
918 1.45 eeh
919 1.45 eeh map->dm_nsegs = j;
920 1.45 eeh #ifdef DIAGNOSTIC
921 1.45 eeh { int seg;
922 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
923 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
924 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
925 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
926 1.45 eeh seg, map->dm_segs[seg].ds_addr,
927 1.45 eeh is->is_dvmabase, is->is_dvmaend);
928 1.45 eeh Debugger();
929 1.45 eeh }
930 1.45 eeh }
931 1.45 eeh }
932 1.45 eeh #endif
933 1.26 martin return (0);
934 1.26 martin }
935 1.9 eeh /*
936 1.40 eeh * This was allocated with bus_dmamem_alloc.
937 1.40 eeh * The pages are on an `mlist'.
938 1.9 eeh */
939 1.9 eeh map->dm_mapsize = size;
940 1.26 martin i = 0;
941 1.40 eeh sgstart = dvmaddr;
942 1.40 eeh sgend = sgstart + size - 1;
943 1.40 eeh map->dm_segs[i].ds_addr = sgstart;
944 1.40 eeh while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
945 1.40 eeh /* Oops. We crossed a boundary. Split the xfer. */
946 1.40 eeh map->dm_segs[i].ds_len = sgstart & (boundary - 1);
947 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
948 1.40 eeh "seg %d start %lx size %lx\n", i,
949 1.40 eeh map->dm_segs[i].ds_addr,
950 1.40 eeh map->dm_segs[i].ds_len));
951 1.40 eeh if (++i > map->_dm_segcnt) {
952 1.40 eeh /* Too many segments. Fail the operation. */
953 1.40 eeh s = splhigh();
954 1.40 eeh /* How can this fail? And if it does what can we do? */
955 1.40 eeh err = extent_free(is->is_dvmamap,
956 1.40 eeh dvmaddr, sgsize, EX_NOWAIT);
957 1.40 eeh map->_dm_dvmastart = 0;
958 1.40 eeh map->_dm_dvmasize = 0;
959 1.43 eeh splx(s);
960 1.40 eeh return (E2BIG);
961 1.40 eeh }
962 1.40 eeh sgstart = roundup(sgstart, boundary);
963 1.40 eeh map->dm_segs[i].ds_addr = sgstart;
964 1.40 eeh }
965 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
966 1.40 eeh "seg %d start %lx size %lx\n", i,
967 1.40 eeh map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len));
968 1.40 eeh map->dm_segs[i].ds_len = sgend - sgstart + 1;
969 1.9 eeh
970 1.9 eeh for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
971 1.9 eeh if (sgsize == 0)
972 1.9 eeh panic("iommu_dmamap_load_raw: size botch");
973 1.9 eeh pa = VM_PAGE_TO_PHYS(m);
974 1.9 eeh
975 1.22 mrg DPRINTF(IDB_BUSDMA,
976 1.9 eeh ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
977 1.9 eeh map, (long)dvmaddr, (long)(pa)));
978 1.45 eeh iommu_enter(is, dvmaddr, pa, flags|0x8000);
979 1.9 eeh
980 1.9 eeh dvmaddr += pagesz;
981 1.9 eeh sgsize -= pagesz;
982 1.9 eeh }
983 1.40 eeh map->dm_mapsize = size;
984 1.40 eeh map->dm_nsegs = i+1;
985 1.45 eeh #ifdef DIAGNOSTIC
986 1.45 eeh { int seg;
987 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
988 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
989 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
990 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
991 1.45 eeh seg, map->dm_segs[seg].ds_addr,
992 1.45 eeh is->is_dvmabase, is->is_dvmaend);
993 1.45 eeh Debugger();
994 1.45 eeh }
995 1.45 eeh }
996 1.45 eeh }
997 1.45 eeh #endif
998 1.9 eeh return (0);
999 1.7 mrg }
1000 1.7 mrg
1001 1.7 mrg void
1002 1.7 mrg iommu_dvmamap_sync(t, is, map, offset, len, ops)
1003 1.7 mrg bus_dma_tag_t t;
1004 1.7 mrg struct iommu_state *is;
1005 1.7 mrg bus_dmamap_t map;
1006 1.7 mrg bus_addr_t offset;
1007 1.7 mrg bus_size_t len;
1008 1.7 mrg int ops;
1009 1.7 mrg {
1010 1.7 mrg vaddr_t va = map->dm_segs[0].ds_addr + offset;
1011 1.7 mrg
1012 1.7 mrg /*
1013 1.7 mrg * We only support one DMA segment; supporting more makes this code
1014 1.7 mrg * too unweildy.
1015 1.7 mrg */
1016 1.7 mrg
1017 1.7 mrg if (ops & BUS_DMASYNC_PREREAD) {
1018 1.36 eeh DPRINTF(IDB_SYNC,
1019 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
1020 1.25 mrg "BUS_DMASYNC_PREREAD\n", (void *)(u_long)va, (u_long)len));
1021 1.7 mrg
1022 1.7 mrg /* Nothing to do */;
1023 1.7 mrg }
1024 1.7 mrg if (ops & BUS_DMASYNC_POSTREAD) {
1025 1.36 eeh DPRINTF(IDB_SYNC,
1026 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
1027 1.25 mrg "BUS_DMASYNC_POSTREAD\n", (void *)(u_long)va, (u_long)len));
1028 1.7 mrg /* if we have a streaming buffer, flush it here first */
1029 1.42 eeh if (is->is_sb[0] || is->is_sb[1])
1030 1.7 mrg while (len > 0) {
1031 1.22 mrg DPRINTF(IDB_BUSDMA,
1032 1.7 mrg ("iommu_dvmamap_sync: flushing va %p, %lu "
1033 1.25 mrg "bytes left\n", (void *)(u_long)va, (u_long)len));
1034 1.31 eeh iommu_strbuf_flush(is, va);
1035 1.7 mrg if (len <= NBPG) {
1036 1.31 eeh iommu_strbuf_flush_done(is);
1037 1.7 mrg len = 0;
1038 1.7 mrg } else
1039 1.7 mrg len -= NBPG;
1040 1.7 mrg va += NBPG;
1041 1.7 mrg }
1042 1.7 mrg }
1043 1.7 mrg if (ops & BUS_DMASYNC_PREWRITE) {
1044 1.36 eeh DPRINTF(IDB_SYNC,
1045 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
1046 1.25 mrg "BUS_DMASYNC_PREWRITE\n", (void *)(u_long)va, (u_long)len));
1047 1.31 eeh /* if we have a streaming buffer, flush it here first */
1048 1.42 eeh if (is->is_sb[0] || is->is_sb[1])
1049 1.31 eeh while (len > 0) {
1050 1.31 eeh DPRINTF(IDB_BUSDMA,
1051 1.31 eeh ("iommu_dvmamap_sync: flushing va %p, %lu "
1052 1.31 eeh "bytes left\n", (void *)(u_long)va, (u_long)len));
1053 1.31 eeh iommu_strbuf_flush(is, va);
1054 1.31 eeh if (len <= NBPG) {
1055 1.31 eeh iommu_strbuf_flush_done(is);
1056 1.31 eeh len = 0;
1057 1.31 eeh } else
1058 1.31 eeh len -= NBPG;
1059 1.31 eeh va += NBPG;
1060 1.31 eeh }
1061 1.7 mrg }
1062 1.7 mrg if (ops & BUS_DMASYNC_POSTWRITE) {
1063 1.36 eeh DPRINTF(IDB_SYNC,
1064 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
1065 1.25 mrg "BUS_DMASYNC_POSTWRITE\n", (void *)(u_long)va, (u_long)len));
1066 1.7 mrg /* Nothing to do */;
1067 1.7 mrg }
1068 1.7 mrg }
1069 1.7 mrg
1070 1.7 mrg int
1071 1.7 mrg iommu_dvmamem_alloc(t, is, size, alignment, boundary, segs, nsegs, rsegs, flags)
1072 1.7 mrg bus_dma_tag_t t;
1073 1.7 mrg struct iommu_state *is;
1074 1.7 mrg bus_size_t size, alignment, boundary;
1075 1.7 mrg bus_dma_segment_t *segs;
1076 1.7 mrg int nsegs;
1077 1.7 mrg int *rsegs;
1078 1.7 mrg int flags;
1079 1.7 mrg {
1080 1.7 mrg
1081 1.25 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
1082 1.25 mrg "segp %p flags %d\n", (unsigned long long)size,
1083 1.25 mrg (unsigned long long)alignment, (unsigned long long)boundary,
1084 1.25 mrg segs, flags));
1085 1.7 mrg return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
1086 1.21 eeh segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
1087 1.7 mrg }
1088 1.7 mrg
1089 1.7 mrg void
1090 1.7 mrg iommu_dvmamem_free(t, is, segs, nsegs)
1091 1.7 mrg bus_dma_tag_t t;
1092 1.7 mrg struct iommu_state *is;
1093 1.7 mrg bus_dma_segment_t *segs;
1094 1.7 mrg int nsegs;
1095 1.7 mrg {
1096 1.7 mrg
1097 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
1098 1.7 mrg segs, nsegs));
1099 1.7 mrg bus_dmamem_free(t->_parent, segs, nsegs);
1100 1.7 mrg }
1101 1.7 mrg
1102 1.7 mrg /*
1103 1.7 mrg * Map the DVMA mappings into the kernel pmap.
1104 1.7 mrg * Check the flags to see whether we're streaming or coherent.
1105 1.7 mrg */
1106 1.7 mrg int
1107 1.7 mrg iommu_dvmamem_map(t, is, segs, nsegs, size, kvap, flags)
1108 1.7 mrg bus_dma_tag_t t;
1109 1.7 mrg struct iommu_state *is;
1110 1.7 mrg bus_dma_segment_t *segs;
1111 1.7 mrg int nsegs;
1112 1.7 mrg size_t size;
1113 1.7 mrg caddr_t *kvap;
1114 1.7 mrg int flags;
1115 1.7 mrg {
1116 1.35 chs struct vm_page *m;
1117 1.7 mrg vaddr_t va;
1118 1.7 mrg bus_addr_t addr;
1119 1.7 mrg struct pglist *mlist;
1120 1.8 mrg int cbit;
1121 1.7 mrg
1122 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
1123 1.7 mrg segs, nsegs, size));
1124 1.7 mrg
1125 1.7 mrg /*
1126 1.8 mrg * Allocate some space in the kernel map, and then map these pages
1127 1.8 mrg * into this space.
1128 1.7 mrg */
1129 1.8 mrg size = round_page(size);
1130 1.8 mrg va = uvm_km_valloc(kernel_map, size);
1131 1.8 mrg if (va == 0)
1132 1.8 mrg return (ENOMEM);
1133 1.7 mrg
1134 1.8 mrg *kvap = (caddr_t)va;
1135 1.7 mrg
1136 1.7 mrg /*
1137 1.7 mrg * digest flags:
1138 1.7 mrg */
1139 1.7 mrg cbit = 0;
1140 1.7 mrg if (flags & BUS_DMA_COHERENT) /* Disable vcache */
1141 1.7 mrg cbit |= PMAP_NVC;
1142 1.7 mrg if (flags & BUS_DMA_NOCACHE) /* sideffects */
1143 1.7 mrg cbit |= PMAP_NC;
1144 1.7 mrg
1145 1.7 mrg /*
1146 1.8 mrg * Now take this and map it into the CPU.
1147 1.7 mrg */
1148 1.7 mrg mlist = segs[0]._ds_mlist;
1149 1.7 mrg for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1150 1.8 mrg #ifdef DIAGNOSTIC
1151 1.7 mrg if (size == 0)
1152 1.7 mrg panic("iommu_dvmamem_map: size botch");
1153 1.8 mrg #endif
1154 1.7 mrg addr = VM_PAGE_TO_PHYS(m);
1155 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
1156 1.25 mrg "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
1157 1.7 mrg pmap_enter(pmap_kernel(), va, addr | cbit,
1158 1.24 eeh VM_PROT_READ | VM_PROT_WRITE,
1159 1.24 eeh VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
1160 1.7 mrg va += PAGE_SIZE;
1161 1.7 mrg size -= PAGE_SIZE;
1162 1.7 mrg }
1163 1.38 chris pmap_update(pmap_kernel());
1164 1.7 mrg
1165 1.7 mrg return (0);
1166 1.7 mrg }
1167 1.7 mrg
1168 1.7 mrg /*
1169 1.7 mrg * Unmap DVMA mappings from kernel
1170 1.7 mrg */
1171 1.7 mrg void
1172 1.7 mrg iommu_dvmamem_unmap(t, is, kva, size)
1173 1.7 mrg bus_dma_tag_t t;
1174 1.7 mrg struct iommu_state *is;
1175 1.7 mrg caddr_t kva;
1176 1.7 mrg size_t size;
1177 1.7 mrg {
1178 1.7 mrg
1179 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
1180 1.7 mrg kva, size));
1181 1.7 mrg
1182 1.7 mrg #ifdef DIAGNOSTIC
1183 1.7 mrg if ((u_long)kva & PGOFSET)
1184 1.7 mrg panic("iommu_dvmamem_unmap");
1185 1.7 mrg #endif
1186 1.7 mrg
1187 1.7 mrg size = round_page(size);
1188 1.7 mrg pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
1189 1.38 chris pmap_update(pmap_kernel());
1190 1.8 mrg #if 0
1191 1.8 mrg /*
1192 1.8 mrg * XXX ? is this necessary? i think so and i think other
1193 1.8 mrg * implementations are missing it.
1194 1.8 mrg */
1195 1.8 mrg uvm_km_free(kernel_map, (vaddr_t)kva, size);
1196 1.8 mrg #endif
1197 1.1 mrg }
1198