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