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