iommu.c revision 1.46 1 1.46 eeh /* $NetBSD: iommu.c,v 1.46 2002/02/08 00:47:04 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.45 eeh printf("IOTSB: %lx to %lx\n", is->is_ptsb, is->is_ptsb+size);
271 1.1 mrg is->is_dvmamap = extent_create(name,
272 1.45 eeh is->is_dvmabase, is->is_dvmaend - NBPG,
273 1.1 mrg M_DEVBUF, 0, 0, EX_NOWAIT);
274 1.1 mrg }
275 1.1 mrg
276 1.8 mrg /*
277 1.8 mrg * Streaming buffers don't exist on the UltraSPARC IIi; we should have
278 1.8 mrg * detected that already and disabled them. If not, we will notice that
279 1.8 mrg * they aren't there when the STRBUF_EN bit does not remain.
280 1.8 mrg */
281 1.1 mrg void
282 1.1 mrg iommu_reset(is)
283 1.1 mrg struct iommu_state *is;
284 1.1 mrg {
285 1.45 eeh struct iommu_strbuf *sb;
286 1.45 eeh int i;
287 1.1 mrg
288 1.1 mrg /* Need to do 64-bit stores */
289 1.21 eeh bus_space_write_8(is->is_bustag,
290 1.42 eeh (bus_space_handle_t)(u_long)&is->is_iommu->iommu_tsb,
291 1.42 eeh 0, is->is_ptsb);
292 1.11 eeh /* Enable IOMMU in diagnostic mode */
293 1.21 eeh bus_space_write_8(is->is_bustag,
294 1.42 eeh (bus_space_handle_t)(u_long)&is->is_iommu->iommu_cr,
295 1.42 eeh 0, is->is_cr|IOMMUCR_DE);
296 1.11 eeh
297 1.45 eeh for (i=0; i<2; i++) {
298 1.45 eeh if ((sb = is->is_sb[i]) != NULL) {
299 1.5 mrg
300 1.45 eeh /* Enable diagnostics mode? */
301 1.45 eeh bus_space_write_8(is->is_bustag,
302 1.45 eeh (bus_space_handle_t)(u_long)&sb->strbuf_ctl,
303 1.45 eeh 0, STRBUF_EN);
304 1.45 eeh
305 1.45 eeh /* No streaming buffers? Disable them */
306 1.45 eeh if (bus_space_read_8(is->is_bustag,
307 1.45 eeh (bus_space_handle_t)(u_long)&sb->strbuf_ctl,
308 1.45 eeh 0) == 0)
309 1.45 eeh is->is_sb[i] = 0;
310 1.45 eeh }
311 1.42 eeh }
312 1.2 eeh }
313 1.2 eeh
314 1.2 eeh /*
315 1.2 eeh * Here are the iommu control routines.
316 1.2 eeh */
317 1.2 eeh void
318 1.2 eeh iommu_enter(is, va, pa, flags)
319 1.2 eeh struct iommu_state *is;
320 1.2 eeh vaddr_t va;
321 1.2 eeh int64_t pa;
322 1.2 eeh int flags;
323 1.2 eeh {
324 1.2 eeh int64_t tte;
325 1.2 eeh
326 1.2 eeh #ifdef DIAGNOSTIC
327 1.45 eeh if (va < is->is_dvmabase || va > is->is_dvmaend)
328 1.13 mrg panic("iommu_enter: va %#lx not in DVMA space", va);
329 1.2 eeh #endif
330 1.2 eeh
331 1.2 eeh tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
332 1.32 thorpej (flags&BUS_DMA_STREAMING));
333 1.45 eeh tte |= (flags & 0xff000LL)<<(4*8);/* DEBUG */
334 1.2 eeh
335 1.2 eeh /* Is the streamcache flush really needed? */
336 1.42 eeh if (is->is_sb[0] || is->is_sb[1]) {
337 1.31 eeh iommu_strbuf_flush(is, va);
338 1.31 eeh iommu_strbuf_flush_done(is);
339 1.5 mrg }
340 1.22 mrg DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
341 1.25 mrg (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va));
342 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
343 1.21 eeh bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
344 1.21 eeh &is->is_iommu->iommu_flush, 0, va);
345 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
346 1.25 mrg va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize),
347 1.25 mrg (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
348 1.25 mrg (u_long)tte));
349 1.39 eeh }
350 1.39 eeh
351 1.39 eeh
352 1.39 eeh /*
353 1.39 eeh * Find the value of a DVMA address (debug routine).
354 1.39 eeh */
355 1.39 eeh paddr_t
356 1.39 eeh iommu_extract(is, dva)
357 1.39 eeh struct iommu_state *is;
358 1.39 eeh vaddr_t dva;
359 1.39 eeh {
360 1.39 eeh int64_t tte = 0;
361 1.39 eeh
362 1.45 eeh if (dva >= is->is_dvmabase && dva < is->is_dvmaend)
363 1.39 eeh tte = is->is_tsb[IOTSBSLOT(dva,is->is_tsbsize)];
364 1.39 eeh
365 1.39 eeh if ((tte&IOTTE_V) == 0)
366 1.39 eeh return ((paddr_t)-1L);
367 1.39 eeh return (tte&IOTTE_PAMASK);
368 1.2 eeh }
369 1.2 eeh
370 1.2 eeh /*
371 1.2 eeh * iommu_remove: removes mappings created by iommu_enter
372 1.2 eeh *
373 1.2 eeh * Only demap from IOMMU if flag is set.
374 1.8 mrg *
375 1.8 mrg * XXX: this function needs better internal error checking.
376 1.2 eeh */
377 1.2 eeh void
378 1.2 eeh iommu_remove(is, va, len)
379 1.2 eeh struct iommu_state *is;
380 1.2 eeh vaddr_t va;
381 1.2 eeh size_t len;
382 1.2 eeh {
383 1.2 eeh
384 1.2 eeh #ifdef DIAGNOSTIC
385 1.45 eeh if (va < is->is_dvmabase || va > is->is_dvmaend)
386 1.25 mrg panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va);
387 1.2 eeh if ((long)(va + len) < (long)va)
388 1.4 mrg panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
389 1.2 eeh (long) va, (long) len);
390 1.2 eeh if (len & ~0xfffffff)
391 1.25 mrg panic("iommu_remove: rediculous len 0x%lx", (u_long)len);
392 1.2 eeh #endif
393 1.2 eeh
394 1.2 eeh va = trunc_page(va);
395 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
396 1.25 mrg va, (u_long)IOTSBSLOT(va,is->is_tsbsize),
397 1.8 mrg &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]));
398 1.2 eeh while (len > 0) {
399 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d for va %p size %lx\n",
400 1.25 mrg (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va, (u_long)len));
401 1.42 eeh if (is->is_sb[0] || is->is_sb[0]) {
402 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
403 1.25 mrg (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
404 1.25 mrg (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
405 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
406 1.4 mrg (u_long)len));
407 1.31 eeh iommu_strbuf_flush(is, va);
408 1.10 mrg if (len <= NBPG)
409 1.31 eeh iommu_strbuf_flush_done(is);
410 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
411 1.25 mrg (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
412 1.25 mrg (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
413 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
414 1.4 mrg (u_long)len));
415 1.36 eeh }
416 1.10 mrg
417 1.10 mrg if (len <= NBPG)
418 1.10 mrg len = 0;
419 1.10 mrg else
420 1.8 mrg len -= NBPG;
421 1.8 mrg
422 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = 0;
423 1.21 eeh bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
424 1.21 eeh &is->is_iommu->iommu_flush, 0, va);
425 1.2 eeh va += NBPG;
426 1.2 eeh }
427 1.2 eeh }
428 1.2 eeh
429 1.14 mrg static int
430 1.31 eeh iommu_strbuf_flush_done(is)
431 1.2 eeh struct iommu_state *is;
432 1.2 eeh {
433 1.2 eeh struct timeval cur, flushtimeout;
434 1.2 eeh
435 1.2 eeh #define BUMPTIME(t, usec) { \
436 1.2 eeh register volatile struct timeval *tp = (t); \
437 1.2 eeh register long us; \
438 1.2 eeh \
439 1.2 eeh tp->tv_usec = us = tp->tv_usec + (usec); \
440 1.2 eeh if (us >= 1000000) { \
441 1.2 eeh tp->tv_usec = us - 1000000; \
442 1.2 eeh tp->tv_sec++; \
443 1.2 eeh } \
444 1.2 eeh }
445 1.5 mrg
446 1.42 eeh if (!is->is_sb[0] && !is->is_sb[1])
447 1.5 mrg return (0);
448 1.7 mrg
449 1.7 mrg /*
450 1.7 mrg * Streaming buffer flushes:
451 1.7 mrg *
452 1.7 mrg * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
453 1.7 mrg * we're not on a cache line boundary (64-bits):
454 1.7 mrg * 2 Store 0 in flag
455 1.7 mrg * 3 Store pointer to flag in flushsync
456 1.7 mrg * 4 wait till flushsync becomes 0x1
457 1.7 mrg *
458 1.7 mrg * If it takes more than .5 sec, something
459 1.7 mrg * went wrong.
460 1.7 mrg */
461 1.2 eeh
462 1.42 eeh is->is_flush[0] = 1;
463 1.42 eeh is->is_flush[1] = 1;
464 1.42 eeh if (is->is_sb[0]) {
465 1.42 eeh is->is_flush[0] = 0;
466 1.42 eeh bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
467 1.42 eeh &is->is_sb[0]->strbuf_flushsync, 0, is->is_flushpa);
468 1.42 eeh }
469 1.42 eeh if (is->is_sb[1]) {
470 1.42 eeh is->is_flush[0] = 1;
471 1.42 eeh bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
472 1.42 eeh &is->is_sb[1]->strbuf_flushsync, 0, is->is_flushpa + 8);
473 1.42 eeh }
474 1.2 eeh
475 1.2 eeh microtime(&flushtimeout);
476 1.2 eeh cur = flushtimeout;
477 1.2 eeh BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
478 1.2 eeh
479 1.42 eeh DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flush = %lx,%lx "
480 1.42 eeh "at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
481 1.42 eeh (long)is->is_flush[0], (long)is->is_flush[1],
482 1.42 eeh (long)&is->is_flush[0], (long)is->is_flushpa,
483 1.42 eeh cur.tv_sec, cur.tv_usec,
484 1.42 eeh flushtimeout.tv_sec, flushtimeout.tv_usec));
485 1.42 eeh
486 1.2 eeh /* Bypass non-coherent D$ */
487 1.42 eeh while ((!ldxa(is->is_flushpa, ASI_PHYS_CACHED) ||
488 1.42 eeh !ldxa(is->is_flushpa + 8, ASI_PHYS_CACHED)) &&
489 1.42 eeh ((cur.tv_sec <= flushtimeout.tv_sec) &&
490 1.42 eeh (cur.tv_usec <= flushtimeout.tv_usec)))
491 1.2 eeh microtime(&cur);
492 1.2 eeh
493 1.2 eeh #ifdef DIAGNOSTIC
494 1.44 thorpej if (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) ||
495 1.44 thorpej !ldxa(is->is_flushpa + 8, ASI_PHYS_CACHED)) {
496 1.42 eeh printf("iommu_strbuf_flush_done: flush timeout %p,%p at %p\n",
497 1.42 eeh (void *)(u_long)is->is_flush[0],
498 1.42 eeh (void *)(u_long)is->is_flush[1],
499 1.42 eeh (void *)(u_long)is->is_flushpa); /* panic? */
500 1.2 eeh #ifdef DDB
501 1.2 eeh Debugger();
502 1.2 eeh #endif
503 1.2 eeh }
504 1.2 eeh #endif
505 1.31 eeh DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flushed\n"));
506 1.42 eeh return (is->is_flush[0] && is->is_flush[1]);
507 1.7 mrg }
508 1.7 mrg
509 1.7 mrg /*
510 1.7 mrg * IOMMU DVMA operations, common to SBUS and PCI.
511 1.7 mrg */
512 1.7 mrg int
513 1.7 mrg iommu_dvmamap_load(t, is, map, buf, buflen, p, flags)
514 1.7 mrg bus_dma_tag_t t;
515 1.7 mrg struct iommu_state *is;
516 1.7 mrg bus_dmamap_t map;
517 1.7 mrg void *buf;
518 1.7 mrg bus_size_t buflen;
519 1.7 mrg struct proc *p;
520 1.7 mrg int flags;
521 1.7 mrg {
522 1.7 mrg int s;
523 1.7 mrg int err;
524 1.7 mrg bus_size_t sgsize;
525 1.7 mrg paddr_t curaddr;
526 1.40 eeh u_long dvmaddr, sgstart, sgend;
527 1.21 eeh bus_size_t align, boundary;
528 1.7 mrg vaddr_t vaddr = (vaddr_t)buf;
529 1.40 eeh int seg;
530 1.7 mrg pmap_t pmap;
531 1.7 mrg
532 1.7 mrg if (map->dm_nsegs) {
533 1.7 mrg /* Already in use?? */
534 1.7 mrg #ifdef DIAGNOSTIC
535 1.7 mrg printf("iommu_dvmamap_load: map still in use\n");
536 1.7 mrg #endif
537 1.7 mrg bus_dmamap_unload(t, map);
538 1.7 mrg }
539 1.7 mrg /*
540 1.7 mrg * Make sure that on error condition we return "no valid mappings".
541 1.7 mrg */
542 1.7 mrg map->dm_nsegs = 0;
543 1.7 mrg
544 1.7 mrg if (buflen > map->_dm_size) {
545 1.22 mrg DPRINTF(IDB_BUSDMA,
546 1.7 mrg ("iommu_dvmamap_load(): error %d > %d -- "
547 1.25 mrg "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
548 1.7 mrg return (EINVAL);
549 1.7 mrg }
550 1.7 mrg
551 1.7 mrg sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
552 1.20 mrg
553 1.7 mrg /*
554 1.21 eeh * A boundary presented to bus_dmamem_alloc() takes precedence
555 1.21 eeh * over boundary in the map.
556 1.7 mrg */
557 1.21 eeh if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
558 1.21 eeh boundary = map->_dm_boundary;
559 1.21 eeh align = max(map->dm_segs[0]._ds_align, NBPG);
560 1.7 mrg s = splhigh();
561 1.40 eeh /*
562 1.40 eeh * If our segment size is larger than the boundary we need to
563 1.40 eeh * split the transfer up int little pieces ourselves.
564 1.40 eeh */
565 1.40 eeh err = extent_alloc(is->is_dvmamap, sgsize, align,
566 1.40 eeh (sgsize > boundary) ? 0 : boundary,
567 1.40 eeh EX_NOWAIT|EX_BOUNDZERO, (u_long *)&dvmaddr);
568 1.7 mrg splx(s);
569 1.7 mrg
570 1.7 mrg #ifdef DEBUG
571 1.11 eeh if (err || (dvmaddr == (bus_addr_t)-1))
572 1.7 mrg {
573 1.7 mrg printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
574 1.25 mrg (int)sgsize, flags);
575 1.40 eeh #ifdef DDB
576 1.7 mrg Debugger();
577 1.40 eeh #endif
578 1.7 mrg }
579 1.7 mrg #endif
580 1.11 eeh if (err != 0)
581 1.11 eeh return (err);
582 1.11 eeh
583 1.7 mrg if (dvmaddr == (bus_addr_t)-1)
584 1.7 mrg return (ENOMEM);
585 1.7 mrg
586 1.40 eeh /* Set the active DVMA map */
587 1.40 eeh map->_dm_dvmastart = dvmaddr;
588 1.40 eeh map->_dm_dvmasize = sgsize;
589 1.40 eeh
590 1.40 eeh /*
591 1.40 eeh * Now split the DVMA range into segments, not crossing
592 1.40 eeh * the boundary.
593 1.40 eeh */
594 1.40 eeh seg = 0;
595 1.40 eeh sgstart = dvmaddr + (vaddr & PGOFSET);
596 1.40 eeh sgend = sgstart + buflen - 1;
597 1.40 eeh map->dm_segs[seg].ds_addr = sgstart;
598 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: boundary %lx boundary-1 %lx "
599 1.40 eeh "~(boundary-1) %lx\n", boundary, (boundary-1), ~(boundary-1)));
600 1.40 eeh while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
601 1.40 eeh /* Oops. We crossed a boundary. Split the xfer. */
602 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
603 1.40 eeh "seg %d start %lx size %lx\n", seg,
604 1.40 eeh map->dm_segs[seg].ds_addr, map->dm_segs[seg].ds_len));
605 1.40 eeh map->dm_segs[seg].ds_len = sgstart & (boundary - 1);
606 1.40 eeh if (++seg > map->_dm_segcnt) {
607 1.40 eeh /* Too many segments. Fail the operation. */
608 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
609 1.40 eeh "too many segments %d\n", seg));
610 1.40 eeh s = splhigh();
611 1.40 eeh /* How can this fail? And if it does what can we do? */
612 1.40 eeh err = extent_free(is->is_dvmamap,
613 1.40 eeh dvmaddr, sgsize, EX_NOWAIT);
614 1.40 eeh map->_dm_dvmastart = 0;
615 1.40 eeh map->_dm_dvmasize = 0;
616 1.43 eeh splx(s);
617 1.40 eeh return (E2BIG);
618 1.40 eeh }
619 1.40 eeh sgstart = roundup(sgstart, boundary);
620 1.40 eeh map->dm_segs[seg].ds_addr = sgstart;
621 1.40 eeh }
622 1.40 eeh map->dm_segs[seg].ds_len = sgend - sgstart + 1;
623 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
624 1.40 eeh "seg %d start %lx size %lx\n", seg,
625 1.40 eeh map->dm_segs[seg].ds_addr, map->dm_segs[seg].ds_len));
626 1.40 eeh map->dm_nsegs = seg+1;
627 1.7 mrg map->dm_mapsize = buflen;
628 1.7 mrg
629 1.7 mrg if (p != NULL)
630 1.7 mrg pmap = p->p_vmspace->vm_map.pmap;
631 1.7 mrg else
632 1.7 mrg pmap = pmap_kernel();
633 1.7 mrg
634 1.7 mrg for (; buflen > 0; ) {
635 1.7 mrg /*
636 1.7 mrg * Get the physical address for this page.
637 1.7 mrg */
638 1.7 mrg if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
639 1.7 mrg bus_dmamap_unload(t, map);
640 1.7 mrg return (-1);
641 1.7 mrg }
642 1.7 mrg
643 1.7 mrg /*
644 1.7 mrg * Compute the segment size, and adjust counts.
645 1.7 mrg */
646 1.7 mrg sgsize = NBPG - ((u_long)vaddr & PGOFSET);
647 1.7 mrg if (buflen < sgsize)
648 1.7 mrg sgsize = buflen;
649 1.7 mrg
650 1.22 mrg DPRINTF(IDB_BUSDMA,
651 1.36 eeh ("iommu_dvmamap_load: map %p loading va %p "
652 1.36 eeh "dva %lx at pa %lx\n",
653 1.36 eeh map, (void *)vaddr, (long)dvmaddr,
654 1.36 eeh (long)(curaddr&~(NBPG-1))));
655 1.7 mrg iommu_enter(is, trunc_page(dvmaddr), trunc_page(curaddr),
656 1.45 eeh flags|0x4000);
657 1.7 mrg
658 1.7 mrg dvmaddr += PAGE_SIZE;
659 1.7 mrg vaddr += sgsize;
660 1.7 mrg buflen -= sgsize;
661 1.7 mrg }
662 1.45 eeh #ifdef DIAGNOSTIC
663 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
664 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
665 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
666 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
667 1.45 eeh seg, map->dm_segs[seg].ds_addr,
668 1.45 eeh is->is_dvmabase, is->is_dvmaend);
669 1.45 eeh Debugger();
670 1.45 eeh }
671 1.45 eeh }
672 1.45 eeh #endif
673 1.7 mrg return (0);
674 1.7 mrg }
675 1.7 mrg
676 1.7 mrg
677 1.7 mrg void
678 1.7 mrg iommu_dvmamap_unload(t, is, map)
679 1.7 mrg bus_dma_tag_t t;
680 1.7 mrg struct iommu_state *is;
681 1.7 mrg bus_dmamap_t map;
682 1.7 mrg {
683 1.40 eeh int error, s;
684 1.7 mrg bus_size_t sgsize;
685 1.7 mrg
686 1.40 eeh /* Flush the iommu */
687 1.40 eeh #ifdef DEBUG
688 1.40 eeh if (!map->_dm_dvmastart) {
689 1.40 eeh printf("iommu_dvmamap_unload: No dvmastart is zero\n");
690 1.40 eeh #ifdef DDB
691 1.40 eeh Debugger();
692 1.40 eeh #endif
693 1.40 eeh }
694 1.40 eeh #endif
695 1.40 eeh iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize);
696 1.7 mrg
697 1.23 eeh /* Flush the caches */
698 1.23 eeh bus_dmamap_unload(t->_parent, map);
699 1.23 eeh
700 1.7 mrg /* Mark the mappings as invalid. */
701 1.7 mrg map->dm_mapsize = 0;
702 1.7 mrg map->dm_nsegs = 0;
703 1.7 mrg
704 1.7 mrg s = splhigh();
705 1.40 eeh error = extent_free(is->is_dvmamap, map->_dm_dvmastart,
706 1.40 eeh map->_dm_dvmasize, EX_NOWAIT);
707 1.43 eeh map->_dm_dvmastart = 0;
708 1.43 eeh map->_dm_dvmasize = 0;
709 1.7 mrg splx(s);
710 1.7 mrg if (error != 0)
711 1.7 mrg printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
712 1.40 eeh
713 1.40 eeh /* Clear the map */
714 1.9 eeh }
715 1.9 eeh
716 1.9 eeh
717 1.9 eeh int
718 1.22 mrg iommu_dvmamap_load_raw(t, is, map, segs, nsegs, flags, size)
719 1.9 eeh bus_dma_tag_t t;
720 1.9 eeh struct iommu_state *is;
721 1.9 eeh bus_dmamap_t map;
722 1.9 eeh bus_dma_segment_t *segs;
723 1.9 eeh int nsegs;
724 1.22 mrg int flags;
725 1.9 eeh bus_size_t size;
726 1.9 eeh {
727 1.35 chs struct vm_page *m;
728 1.40 eeh int i, j, s;
729 1.26 martin int left;
730 1.9 eeh int err;
731 1.9 eeh bus_size_t sgsize;
732 1.9 eeh paddr_t pa;
733 1.21 eeh bus_size_t boundary, align;
734 1.40 eeh u_long dvmaddr, sgstart, sgend;
735 1.9 eeh struct pglist *mlist;
736 1.9 eeh int pagesz = PAGE_SIZE;
737 1.45 eeh int npg = 0; /* DEBUG */
738 1.9 eeh
739 1.9 eeh if (map->dm_nsegs) {
740 1.9 eeh /* Already in use?? */
741 1.9 eeh #ifdef DIAGNOSTIC
742 1.9 eeh printf("iommu_dvmamap_load_raw: map still in use\n");
743 1.9 eeh #endif
744 1.9 eeh bus_dmamap_unload(t, map);
745 1.9 eeh }
746 1.40 eeh
747 1.40 eeh /*
748 1.40 eeh * A boundary presented to bus_dmamem_alloc() takes precedence
749 1.40 eeh * over boundary in the map.
750 1.40 eeh */
751 1.40 eeh if ((boundary = segs[0]._ds_boundary) == 0)
752 1.40 eeh boundary = map->_dm_boundary;
753 1.40 eeh
754 1.45 eeh align = max(segs[0]._ds_align, pagesz);
755 1.40 eeh
756 1.9 eeh /*
757 1.9 eeh * Make sure that on error condition we return "no valid mappings".
758 1.9 eeh */
759 1.9 eeh map->dm_nsegs = 0;
760 1.26 martin /* Count up the total number of pages we need */
761 1.26 martin pa = segs[0].ds_addr;
762 1.26 martin sgsize = 0;
763 1.40 eeh left = size;
764 1.40 eeh for (i=0; left && i<nsegs; i++) {
765 1.26 martin if (round_page(pa) != round_page(segs[i].ds_addr))
766 1.26 martin sgsize = round_page(sgsize);
767 1.40 eeh sgsize += min(left, segs[i].ds_len);
768 1.40 eeh left -= segs[i].ds_len;
769 1.26 martin pa = segs[i].ds_addr + segs[i].ds_len;
770 1.26 martin }
771 1.26 martin sgsize = round_page(sgsize);
772 1.9 eeh
773 1.40 eeh s = splhigh();
774 1.40 eeh /*
775 1.40 eeh * If our segment size is larger than the boundary we need to
776 1.45 eeh * split the transfer up into little pieces ourselves.
777 1.9 eeh */
778 1.40 eeh err = extent_alloc(is->is_dvmamap, sgsize, align,
779 1.40 eeh (sgsize > boundary) ? 0 : boundary,
780 1.40 eeh ((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT) |
781 1.40 eeh EX_BOUNDZERO, (u_long *)&dvmaddr);
782 1.9 eeh splx(s);
783 1.9 eeh
784 1.9 eeh if (err != 0)
785 1.9 eeh return (err);
786 1.9 eeh
787 1.9 eeh #ifdef DEBUG
788 1.9 eeh if (dvmaddr == (bus_addr_t)-1)
789 1.9 eeh {
790 1.9 eeh printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
791 1.25 mrg (int)sgsize, flags);
792 1.9 eeh Debugger();
793 1.9 eeh }
794 1.9 eeh #endif
795 1.9 eeh if (dvmaddr == (bus_addr_t)-1)
796 1.9 eeh return (ENOMEM);
797 1.9 eeh
798 1.40 eeh /* Set the active DVMA map */
799 1.40 eeh map->_dm_dvmastart = dvmaddr;
800 1.40 eeh map->_dm_dvmasize = sgsize;
801 1.40 eeh
802 1.26 martin if ((mlist = segs[0]._ds_mlist) == NULL) {
803 1.26 martin u_long prev_va = NULL;
804 1.45 eeh paddr_t prev_pa = 0;
805 1.45 eeh int end = 0, offset;
806 1.45 eeh
807 1.26 martin /*
808 1.45 eeh * This segs is made up of individual physical
809 1.45 eeh * segments, probably by _bus_dmamap_load_uio() or
810 1.26 martin * _bus_dmamap_load_mbuf(). Ignore the mlist and
811 1.45 eeh * load each one individually.
812 1.26 martin */
813 1.40 eeh map->dm_mapsize = size;
814 1.40 eeh
815 1.45 eeh j = 0;
816 1.45 eeh for (i = 0; i < nsegs ; i++) {
817 1.40 eeh
818 1.45 eeh pa = segs[i].ds_addr;
819 1.45 eeh offset = (pa & PGOFSET);
820 1.45 eeh pa = trunc_page(pa);
821 1.45 eeh dvmaddr = trunc_page(dvmaddr);
822 1.45 eeh left = min(size, segs[i].ds_len);
823 1.45 eeh
824 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: converting "
825 1.45 eeh "physseg %d start %lx size %lx\n", i,
826 1.45 eeh segs[i].ds_addr, segs[i].ds_len));
827 1.26 martin
828 1.45 eeh if (pa == prev_pa) {
829 1.45 eeh /* We can re-use this mapping */
830 1.45 eeh #ifdef DEBUG
831 1.45 eeh if (iommudebug & 0x10) printf("reusing dva %lx prev %lx pa %lx prev %lx\n",
832 1.45 eeh dvmaddr, prev_va, pa, prev_pa);
833 1.45 eeh #endif
834 1.45 eeh dvmaddr = prev_va;
835 1.45 eeh }
836 1.29 martin
837 1.45 eeh sgstart = dvmaddr + offset;
838 1.45 eeh sgend = sgstart + left - 1;
839 1.26 martin
840 1.45 eeh /* Are the segments virtually adjacent? */
841 1.45 eeh if ((end == offset) &&
842 1.45 eeh ((offset == 0) || (pa == prev_pa))) {
843 1.45 eeh /* Just append to the previous segment. */
844 1.45 eeh #ifdef DEBUG
845 1.45 eeh if (iommudebug & 0x10) {
846 1.45 eeh printf("iommu_dvmamap_load_raw: converting "
847 1.45 eeh "physseg %d start %lx size %lx\n", i,
848 1.45 eeh segs[i].ds_addr, segs[i].ds_len);
849 1.45 eeh printf("appending: offset %x pa %lx prev %lx dva %lx prev %lx\n",
850 1.45 eeh offset, pa, prev_pa, dvmaddr, prev_va);
851 1.45 eeh }
852 1.45 eeh #endif
853 1.40 eeh
854 1.45 eeh map->dm_segs[--j].ds_len += left;
855 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
856 1.45 eeh "appending seg %d start %lx size %lx\n", j,
857 1.45 eeh map->dm_segs[j].ds_addr,
858 1.45 eeh map->dm_segs[j].ds_len));
859 1.45 eeh } else {
860 1.45 eeh map->dm_segs[j].ds_addr = sgstart;
861 1.45 eeh map->dm_segs[j].ds_len = left;
862 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
863 1.45 eeh "seg %d start %lx size %lx\n", j,
864 1.45 eeh map->dm_segs[j].ds_addr,
865 1.45 eeh map->dm_segs[j].ds_len));
866 1.40 eeh }
867 1.45 eeh end = (offset + left) & PGOFSET;
868 1.40 eeh
869 1.40 eeh /* Check for boundary issues */
870 1.40 eeh while ((sgstart & ~(boundary - 1)) !=
871 1.40 eeh (sgend & ~(boundary - 1))) {
872 1.40 eeh /* Need a new segment. */
873 1.40 eeh map->dm_segs[j].ds_len =
874 1.40 eeh sgstart & (boundary - 1);
875 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
876 1.40 eeh "seg %d start %lx size %lx\n", j,
877 1.40 eeh map->dm_segs[j].ds_addr,
878 1.40 eeh map->dm_segs[j].ds_len));
879 1.40 eeh if (++j > map->_dm_segcnt) {
880 1.40 eeh iommu_dvmamap_unload(t, is, map);
881 1.40 eeh return (E2BIG);
882 1.40 eeh }
883 1.40 eeh sgstart = roundup(sgstart, boundary);
884 1.40 eeh map->dm_segs[j].ds_addr = sgstart;
885 1.40 eeh map->dm_segs[j].ds_len = sgend - sgstart + 1;
886 1.40 eeh }
887 1.40 eeh
888 1.26 martin if (sgsize == 0)
889 1.26 martin panic("iommu_dmamap_load_raw: size botch");
890 1.40 eeh
891 1.45 eeh /* Now map a series of pages. */
892 1.45 eeh while (dvmaddr < sgend) {
893 1.45 eeh DPRINTF(IDB_BUSDMA,
894 1.45 eeh ("iommu_dvmamap_load_raw: map %p "
895 1.45 eeh "loading va %lx at pa %lx\n",
896 1.45 eeh map, (long)dvmaddr,
897 1.45 eeh (long)(pa)));
898 1.45 eeh /* Enter it if we haven't before. */
899 1.46 eeh if (prev_va != dvmaddr)
900 1.45 eeh #ifdef DEBUG
901 1.46 eeh { if (iommudebug & 0x10) printf("seg %d:%d entering dvma %lx, prev %lx pa %lx\n", i,j, dvmaddr, prev_va, pa);
902 1.45 eeh #endif
903 1.45 eeh iommu_enter(is, prev_va = dvmaddr,
904 1.45 eeh prev_pa = pa,
905 1.45 eeh flags|(++npg<<12));
906 1.45 eeh #ifdef DEBUG
907 1.45 eeh } else if (iommudebug & 0x10) printf("seg %d:%d skipping dvma %lx, prev %lx\n", i,j, dvmaddr, prev_va);
908 1.45 eeh #endif
909 1.45 eeh
910 1.45 eeh dvmaddr += pagesz;
911 1.45 eeh pa += pagesz;
912 1.45 eeh }
913 1.45 eeh
914 1.45 eeh size -= left;
915 1.45 eeh ++j;
916 1.26 martin }
917 1.45 eeh
918 1.45 eeh map->dm_nsegs = j;
919 1.45 eeh #ifdef DIAGNOSTIC
920 1.45 eeh { int seg;
921 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
922 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
923 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
924 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
925 1.45 eeh seg, map->dm_segs[seg].ds_addr,
926 1.45 eeh is->is_dvmabase, is->is_dvmaend);
927 1.45 eeh Debugger();
928 1.45 eeh }
929 1.45 eeh }
930 1.45 eeh }
931 1.45 eeh #endif
932 1.26 martin return (0);
933 1.26 martin }
934 1.9 eeh /*
935 1.40 eeh * This was allocated with bus_dmamem_alloc.
936 1.40 eeh * The pages are on an `mlist'.
937 1.9 eeh */
938 1.9 eeh map->dm_mapsize = size;
939 1.26 martin i = 0;
940 1.40 eeh sgstart = dvmaddr;
941 1.40 eeh sgend = sgstart + size - 1;
942 1.40 eeh map->dm_segs[i].ds_addr = sgstart;
943 1.40 eeh while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
944 1.40 eeh /* Oops. We crossed a boundary. Split the xfer. */
945 1.40 eeh map->dm_segs[i].ds_len = sgstart & (boundary - 1);
946 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
947 1.40 eeh "seg %d start %lx size %lx\n", i,
948 1.40 eeh map->dm_segs[i].ds_addr,
949 1.40 eeh map->dm_segs[i].ds_len));
950 1.40 eeh if (++i > map->_dm_segcnt) {
951 1.40 eeh /* Too many segments. Fail the operation. */
952 1.40 eeh s = splhigh();
953 1.40 eeh /* How can this fail? And if it does what can we do? */
954 1.40 eeh err = extent_free(is->is_dvmamap,
955 1.40 eeh dvmaddr, sgsize, EX_NOWAIT);
956 1.40 eeh map->_dm_dvmastart = 0;
957 1.40 eeh map->_dm_dvmasize = 0;
958 1.43 eeh splx(s);
959 1.40 eeh return (E2BIG);
960 1.40 eeh }
961 1.40 eeh sgstart = roundup(sgstart, boundary);
962 1.40 eeh map->dm_segs[i].ds_addr = sgstart;
963 1.40 eeh }
964 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
965 1.40 eeh "seg %d start %lx size %lx\n", i,
966 1.40 eeh map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len));
967 1.40 eeh map->dm_segs[i].ds_len = sgend - sgstart + 1;
968 1.9 eeh
969 1.9 eeh for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
970 1.9 eeh if (sgsize == 0)
971 1.9 eeh panic("iommu_dmamap_load_raw: size botch");
972 1.9 eeh pa = VM_PAGE_TO_PHYS(m);
973 1.9 eeh
974 1.22 mrg DPRINTF(IDB_BUSDMA,
975 1.9 eeh ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
976 1.9 eeh map, (long)dvmaddr, (long)(pa)));
977 1.45 eeh iommu_enter(is, dvmaddr, pa, flags|0x8000);
978 1.9 eeh
979 1.9 eeh dvmaddr += pagesz;
980 1.9 eeh sgsize -= pagesz;
981 1.9 eeh }
982 1.40 eeh map->dm_mapsize = size;
983 1.40 eeh map->dm_nsegs = i+1;
984 1.45 eeh #ifdef DIAGNOSTIC
985 1.45 eeh { int seg;
986 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
987 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
988 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
989 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
990 1.45 eeh seg, map->dm_segs[seg].ds_addr,
991 1.45 eeh is->is_dvmabase, is->is_dvmaend);
992 1.45 eeh Debugger();
993 1.45 eeh }
994 1.45 eeh }
995 1.45 eeh }
996 1.45 eeh #endif
997 1.9 eeh return (0);
998 1.7 mrg }
999 1.7 mrg
1000 1.7 mrg void
1001 1.7 mrg iommu_dvmamap_sync(t, is, map, offset, len, ops)
1002 1.7 mrg bus_dma_tag_t t;
1003 1.7 mrg struct iommu_state *is;
1004 1.7 mrg bus_dmamap_t map;
1005 1.7 mrg bus_addr_t offset;
1006 1.7 mrg bus_size_t len;
1007 1.7 mrg int ops;
1008 1.7 mrg {
1009 1.7 mrg vaddr_t va = map->dm_segs[0].ds_addr + offset;
1010 1.7 mrg
1011 1.7 mrg /*
1012 1.7 mrg * We only support one DMA segment; supporting more makes this code
1013 1.7 mrg * too unweildy.
1014 1.7 mrg */
1015 1.7 mrg
1016 1.7 mrg if (ops & BUS_DMASYNC_PREREAD) {
1017 1.36 eeh DPRINTF(IDB_SYNC,
1018 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
1019 1.25 mrg "BUS_DMASYNC_PREREAD\n", (void *)(u_long)va, (u_long)len));
1020 1.7 mrg
1021 1.7 mrg /* Nothing to do */;
1022 1.7 mrg }
1023 1.7 mrg if (ops & BUS_DMASYNC_POSTREAD) {
1024 1.36 eeh DPRINTF(IDB_SYNC,
1025 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
1026 1.25 mrg "BUS_DMASYNC_POSTREAD\n", (void *)(u_long)va, (u_long)len));
1027 1.7 mrg /* if we have a streaming buffer, flush it here first */
1028 1.42 eeh if (is->is_sb[0] || is->is_sb[1])
1029 1.7 mrg while (len > 0) {
1030 1.22 mrg DPRINTF(IDB_BUSDMA,
1031 1.7 mrg ("iommu_dvmamap_sync: flushing va %p, %lu "
1032 1.25 mrg "bytes left\n", (void *)(u_long)va, (u_long)len));
1033 1.31 eeh iommu_strbuf_flush(is, va);
1034 1.7 mrg if (len <= NBPG) {
1035 1.31 eeh iommu_strbuf_flush_done(is);
1036 1.7 mrg len = 0;
1037 1.7 mrg } else
1038 1.7 mrg len -= NBPG;
1039 1.7 mrg va += NBPG;
1040 1.7 mrg }
1041 1.7 mrg }
1042 1.7 mrg if (ops & BUS_DMASYNC_PREWRITE) {
1043 1.36 eeh DPRINTF(IDB_SYNC,
1044 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
1045 1.25 mrg "BUS_DMASYNC_PREWRITE\n", (void *)(u_long)va, (u_long)len));
1046 1.31 eeh /* if we have a streaming buffer, flush it here first */
1047 1.42 eeh if (is->is_sb[0] || is->is_sb[1])
1048 1.31 eeh while (len > 0) {
1049 1.31 eeh DPRINTF(IDB_BUSDMA,
1050 1.31 eeh ("iommu_dvmamap_sync: flushing va %p, %lu "
1051 1.31 eeh "bytes left\n", (void *)(u_long)va, (u_long)len));
1052 1.31 eeh iommu_strbuf_flush(is, va);
1053 1.31 eeh if (len <= NBPG) {
1054 1.31 eeh iommu_strbuf_flush_done(is);
1055 1.31 eeh len = 0;
1056 1.31 eeh } else
1057 1.31 eeh len -= NBPG;
1058 1.31 eeh va += NBPG;
1059 1.31 eeh }
1060 1.7 mrg }
1061 1.7 mrg if (ops & BUS_DMASYNC_POSTWRITE) {
1062 1.36 eeh DPRINTF(IDB_SYNC,
1063 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
1064 1.25 mrg "BUS_DMASYNC_POSTWRITE\n", (void *)(u_long)va, (u_long)len));
1065 1.7 mrg /* Nothing to do */;
1066 1.7 mrg }
1067 1.7 mrg }
1068 1.7 mrg
1069 1.7 mrg int
1070 1.7 mrg iommu_dvmamem_alloc(t, is, size, alignment, boundary, segs, nsegs, rsegs, flags)
1071 1.7 mrg bus_dma_tag_t t;
1072 1.7 mrg struct iommu_state *is;
1073 1.7 mrg bus_size_t size, alignment, boundary;
1074 1.7 mrg bus_dma_segment_t *segs;
1075 1.7 mrg int nsegs;
1076 1.7 mrg int *rsegs;
1077 1.7 mrg int flags;
1078 1.7 mrg {
1079 1.7 mrg
1080 1.25 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
1081 1.25 mrg "segp %p flags %d\n", (unsigned long long)size,
1082 1.25 mrg (unsigned long long)alignment, (unsigned long long)boundary,
1083 1.25 mrg segs, flags));
1084 1.7 mrg return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
1085 1.21 eeh segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
1086 1.7 mrg }
1087 1.7 mrg
1088 1.7 mrg void
1089 1.7 mrg iommu_dvmamem_free(t, is, segs, nsegs)
1090 1.7 mrg bus_dma_tag_t t;
1091 1.7 mrg struct iommu_state *is;
1092 1.7 mrg bus_dma_segment_t *segs;
1093 1.7 mrg int nsegs;
1094 1.7 mrg {
1095 1.7 mrg
1096 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
1097 1.7 mrg segs, nsegs));
1098 1.7 mrg bus_dmamem_free(t->_parent, segs, nsegs);
1099 1.7 mrg }
1100 1.7 mrg
1101 1.7 mrg /*
1102 1.7 mrg * Map the DVMA mappings into the kernel pmap.
1103 1.7 mrg * Check the flags to see whether we're streaming or coherent.
1104 1.7 mrg */
1105 1.7 mrg int
1106 1.7 mrg iommu_dvmamem_map(t, is, segs, nsegs, size, kvap, flags)
1107 1.7 mrg bus_dma_tag_t t;
1108 1.7 mrg struct iommu_state *is;
1109 1.7 mrg bus_dma_segment_t *segs;
1110 1.7 mrg int nsegs;
1111 1.7 mrg size_t size;
1112 1.7 mrg caddr_t *kvap;
1113 1.7 mrg int flags;
1114 1.7 mrg {
1115 1.35 chs struct vm_page *m;
1116 1.7 mrg vaddr_t va;
1117 1.7 mrg bus_addr_t addr;
1118 1.7 mrg struct pglist *mlist;
1119 1.8 mrg int cbit;
1120 1.7 mrg
1121 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
1122 1.7 mrg segs, nsegs, size));
1123 1.7 mrg
1124 1.7 mrg /*
1125 1.8 mrg * Allocate some space in the kernel map, and then map these pages
1126 1.8 mrg * into this space.
1127 1.7 mrg */
1128 1.8 mrg size = round_page(size);
1129 1.8 mrg va = uvm_km_valloc(kernel_map, size);
1130 1.8 mrg if (va == 0)
1131 1.8 mrg return (ENOMEM);
1132 1.7 mrg
1133 1.8 mrg *kvap = (caddr_t)va;
1134 1.7 mrg
1135 1.7 mrg /*
1136 1.7 mrg * digest flags:
1137 1.7 mrg */
1138 1.7 mrg cbit = 0;
1139 1.7 mrg if (flags & BUS_DMA_COHERENT) /* Disable vcache */
1140 1.7 mrg cbit |= PMAP_NVC;
1141 1.7 mrg if (flags & BUS_DMA_NOCACHE) /* sideffects */
1142 1.7 mrg cbit |= PMAP_NC;
1143 1.7 mrg
1144 1.7 mrg /*
1145 1.8 mrg * Now take this and map it into the CPU.
1146 1.7 mrg */
1147 1.7 mrg mlist = segs[0]._ds_mlist;
1148 1.7 mrg for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1149 1.8 mrg #ifdef DIAGNOSTIC
1150 1.7 mrg if (size == 0)
1151 1.7 mrg panic("iommu_dvmamem_map: size botch");
1152 1.8 mrg #endif
1153 1.7 mrg addr = VM_PAGE_TO_PHYS(m);
1154 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
1155 1.25 mrg "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
1156 1.7 mrg pmap_enter(pmap_kernel(), va, addr | cbit,
1157 1.24 eeh VM_PROT_READ | VM_PROT_WRITE,
1158 1.24 eeh VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
1159 1.7 mrg va += PAGE_SIZE;
1160 1.7 mrg size -= PAGE_SIZE;
1161 1.7 mrg }
1162 1.38 chris pmap_update(pmap_kernel());
1163 1.7 mrg
1164 1.7 mrg return (0);
1165 1.7 mrg }
1166 1.7 mrg
1167 1.7 mrg /*
1168 1.7 mrg * Unmap DVMA mappings from kernel
1169 1.7 mrg */
1170 1.7 mrg void
1171 1.7 mrg iommu_dvmamem_unmap(t, is, kva, size)
1172 1.7 mrg bus_dma_tag_t t;
1173 1.7 mrg struct iommu_state *is;
1174 1.7 mrg caddr_t kva;
1175 1.7 mrg size_t size;
1176 1.7 mrg {
1177 1.7 mrg
1178 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
1179 1.7 mrg kva, size));
1180 1.7 mrg
1181 1.7 mrg #ifdef DIAGNOSTIC
1182 1.7 mrg if ((u_long)kva & PGOFSET)
1183 1.7 mrg panic("iommu_dvmamem_unmap");
1184 1.7 mrg #endif
1185 1.7 mrg
1186 1.7 mrg size = round_page(size);
1187 1.7 mrg pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
1188 1.38 chris pmap_update(pmap_kernel());
1189 1.8 mrg #if 0
1190 1.8 mrg /*
1191 1.8 mrg * XXX ? is this necessary? i think so and i think other
1192 1.8 mrg * implementations are missing it.
1193 1.8 mrg */
1194 1.8 mrg uvm_km_free(kernel_map, (vaddr_t)kva, size);
1195 1.8 mrg #endif
1196 1.1 mrg }
1197