1 /* $NetBSD: iommu.c,v 1.95 2009/12/07 19:57:34 nakayama Exp $ */
4 * Copyright (c) 1999, 2000 Matthew R. Green
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c) 2001, 2002 Eduardo Horvath
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. The name of the author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
49 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
51 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * UltraSPARC IOMMU support; used by both the sbus and pci code.
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: iommu.c,v 1.95 2009/12/07 19:57:34 nakayama Exp $");
66 #include <sys/param.h>
67 #include <sys/extent.h>
68 #include <sys/malloc.h>
69 #include <sys/systm.h>
70 #include <sys/device.h>
73 #include <uvm/uvm_extern.h>
75 #include <machine/bus.h>
76 #include <sparc64/dev/iommureg.h>
77 #include <sparc64/dev/iommuvar.h>
79 #include <machine/autoconf.h>
80 #include <machine/cpu.h>
83 #define IDB_BUSDMA 0x1
88 #define DPRINTF(l, s) do { if (iommudebug & l) printf s; } while (0)
89 #define IOTTE_DEBUG(n) (n)
92 #define IOTTE_DEBUG(n) 0
95 #define iommu_strbuf_flush(i, v) do { \
97 bus_space_write_8((i)->sb_is->is_bustag, (i)->sb_sb, \
98 STRBUFREG(strbuf_pgflush), (v)); \
101 static int iommu_strbuf_flush_done(struct strbuf_ctl
*);
102 static void _iommu_dvmamap_sync(bus_dma_tag_t
, bus_dmamap_t
, bus_addr_t
,
106 * initialise the UltraSPARC IOMMU (SBUS or PCI):
107 * - allocate and setup the iotsb.
109 * - initialise the streaming buffers (if they exist)
110 * - create a private DVMA map.
113 iommu_init(char *name
, struct iommu_state
*is
, int tsbsize
, uint32_t iovabase
)
119 struct pglist pglist
;
124 * The sun4u iommu is part of the SBUS or PCI controller so we will
125 * deal with it here..
127 * For sysio and psycho/psycho+ the IOMMU address space always ends at
128 * 0xffffe000, but the starting address depends on the size of the
129 * map. The map size is 1024 * 2 ^ is->is_tsbsize entries, where each
130 * entry is 8 bytes. The start of the map can be calculated by
131 * (0xffffe000 << (8 + is->is_tsbsize)).
133 * But sabre and hummingbird use a different scheme that seems to
134 * be hard-wired, so we read the start and size from the PROM and
135 * just use those values.
137 is
->is_cr
= (tsbsize
<< 16) | IOMMUCR_EN
;
138 is
->is_tsbsize
= tsbsize
;
139 if (iovabase
== -1) {
140 is
->is_dvmabase
= IOTSB_VSTART(is
->is_tsbsize
);
141 is
->is_dvmaend
= IOTSB_VEND
- 1;
143 is
->is_dvmabase
= iovabase
;
144 is
->is_dvmaend
= iovabase
+ IOTSB_VSIZE(tsbsize
) - 1;
148 * Allocate memory for I/O pagetables. They need to be physically
152 size
= PAGE_SIZE
<< is
->is_tsbsize
;
153 if (uvm_pglistalloc((psize_t
)size
, (paddr_t
)0, (paddr_t
)-1,
154 (paddr_t
)PAGE_SIZE
, (paddr_t
)0, &pglist
, 1, 0) != 0)
155 panic("iommu_init: no memory");
157 va
= uvm_km_alloc(kernel_map
, size
, 0, UVM_KMF_VAONLY
);
159 panic("iommu_init: no memory");
160 is
->is_tsb
= (int64_t *)va
;
162 is
->is_ptsb
= VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist
));
165 TAILQ_FOREACH(pg
, &pglist
, pageq
.queue
) {
166 pa
= VM_PAGE_TO_PHYS(pg
);
167 pmap_kenter_pa(va
, pa
| PMAP_NVC
,
168 VM_PROT_READ
| VM_PROT_WRITE
, 0);
171 pmap_update(pmap_kernel());
172 memset(is
->is_tsb
, 0, size
);
175 if (iommudebug
& IDB_INFO
)
177 /* Probe the iommu */
179 printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n",
180 (u_long
)bus_space_read_8(is
->is_bustag
, is
->is_iommu
,
181 offsetof (struct iommureg
, iommu_cr
)),
182 (u_long
)bus_space_read_8(is
->is_bustag
, is
->is_iommu
,
183 offsetof (struct iommureg
, iommu_tsb
)),
184 (u_long
)bus_space_read_8(is
->is_bustag
, is
->is_iommu
,
185 offsetof (struct iommureg
, iommu_flush
)));
186 printf("iommu cr=%llx tsb=%llx\n",
187 (unsigned long long)bus_space_read_8(is
->is_bustag
,
189 offsetof (struct iommureg
, iommu_cr
)),
190 (unsigned long long)bus_space_read_8(is
->is_bustag
,
192 offsetof (struct iommureg
, iommu_tsb
)));
193 printf("TSB base %p phys %llx\n", (void *)is
->is_tsb
,
194 (unsigned long long)is
->is_ptsb
);
195 delay(1000000); /* 1 s */
200 * now actually start up the IOMMU
205 * Now all the hardware's working we need to allocate a dvma map.
207 printf("DVMA map: %x to %x\n",
208 (unsigned int)is
->is_dvmabase
,
209 (unsigned int)is
->is_dvmaend
);
210 printf("IOTSB: %llx to %llx\n",
211 (unsigned long long)is
->is_ptsb
,
212 (unsigned long long)(is
->is_ptsb
+ size
- 1));
213 is
->is_dvmamap
= extent_create(name
,
214 is
->is_dvmabase
, is
->is_dvmaend
,
215 M_DEVBUF
, 0, 0, EX_NOWAIT
);
219 * Streaming buffers don't exist on the UltraSPARC IIi; we should have
220 * detected that already and disabled them. If not, we will notice that
221 * they aren't there when the STRBUF_EN bit does not remain.
224 iommu_reset(struct iommu_state
*is
)
227 struct strbuf_ctl
*sb
;
229 /* Need to do 64-bit stores */
230 bus_space_write_8(is
->is_bustag
, is
->is_iommu
, IOMMUREG(iommu_tsb
),
233 /* Enable IOMMU in diagnostic mode */
234 bus_space_write_8(is
->is_bustag
, is
->is_iommu
, IOMMUREG(iommu_cr
),
235 is
->is_cr
|IOMMUCR_DE
);
237 for (i
= 0; i
< 2; i
++) {
238 if ((sb
= is
->is_sb
[i
])) {
240 /* Enable diagnostics mode? */
241 bus_space_write_8(is
->is_bustag
, is
->is_sb
[i
]->sb_sb
,
242 STRBUFREG(strbuf_ctl
), STRBUF_EN
);
244 /* No streaming buffers? Disable them */
245 if (bus_space_read_8(is
->is_bustag
,
247 STRBUFREG(strbuf_ctl
)) == 0) {
248 is
->is_sb
[i
]->sb_flush
= NULL
;
252 * locate the pa of the flush buffer.
254 (void)pmap_extract(pmap_kernel(),
255 (vaddr_t
)is
->is_sb
[i
]->sb_flush
,
256 &is
->is_sb
[i
]->sb_flushpa
);
263 * Here are the iommu control routines.
266 iommu_enter(struct strbuf_ctl
*sb
, vaddr_t va
, int64_t pa
, int flags
)
268 struct iommu_state
*is
= sb
->sb_is
;
269 int strbuf
= (flags
& BUS_DMA_STREAMING
);
273 if (va
< is
->is_dvmabase
|| va
> is
->is_dvmaend
)
274 panic("iommu_enter: va %#lx not in DVMA space", va
);
277 /* Is the streamcache flush really needed? */
279 iommu_strbuf_flush(sb
, va
);
281 /* If we can't flush the strbuf don't enable it. */
284 tte
= MAKEIOTTE(pa
, !(flags
& BUS_DMA_NOWRITE
),
285 !(flags
& BUS_DMA_NOCACHE
), (strbuf
));
287 tte
|= (flags
& 0xff000LL
)<<(4*8);
290 DPRINTF(IDB_IOMMU
, ("Clearing TSB slot %d for va %p\n",
291 (int)IOTSBSLOT(va
,is
->is_tsbsize
), (void *)(u_long
)va
));
292 is
->is_tsb
[IOTSBSLOT(va
,is
->is_tsbsize
)] = tte
;
293 bus_space_write_8(is
->is_bustag
, is
->is_iommu
,
294 IOMMUREG(iommu_flush
), va
);
295 DPRINTF(IDB_IOMMU
, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
296 va
, (long)pa
, (u_long
)IOTSBSLOT(va
,is
->is_tsbsize
),
297 (void *)(u_long
)&is
->is_tsb
[IOTSBSLOT(va
,is
->is_tsbsize
)],
302 * Find the value of a DVMA address (debug routine).
305 iommu_extract(struct iommu_state
*is
, vaddr_t dva
)
309 if (dva
>= is
->is_dvmabase
&& dva
<= is
->is_dvmaend
)
310 tte
= is
->is_tsb
[IOTSBSLOT(dva
, is
->is_tsbsize
)];
312 if ((tte
& IOTTE_V
) == 0)
313 return ((paddr_t
)-1L);
314 return (tte
& IOTTE_PAMASK
);
318 * iommu_remove: removes mappings created by iommu_enter
320 * Only demap from IOMMU if flag is set.
322 * XXX: this function needs better internal error checking.
325 iommu_remove(struct iommu_state
*is
, vaddr_t va
, size_t len
)
329 if (va
< is
->is_dvmabase
|| va
> is
->is_dvmaend
)
330 panic("iommu_remove: va 0x%lx not in DVMA space", (u_long
)va
);
331 if ((long)(va
+ len
) < (long)va
)
332 panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
333 (long) va
, (long) len
);
334 if (len
& ~0xfffffff)
335 panic("iommu_remove: ridiculous len 0x%lx", (u_long
)len
);
339 DPRINTF(IDB_IOMMU
, ("iommu_remove: va %lx TSB[%lx]@%p\n",
340 va
, (u_long
)IOTSBSLOT(va
, is
->is_tsbsize
),
341 &is
->is_tsb
[IOTSBSLOT(va
, is
->is_tsbsize
)]));
343 DPRINTF(IDB_IOMMU
, ("iommu_remove: clearing TSB slot %d "
344 "for va %p size %lx\n",
345 (int)IOTSBSLOT(va
,is
->is_tsbsize
), (void *)(u_long
)va
,
347 if (len
<= PAGE_SIZE
)
353 * XXX Zero-ing the entry would not require RMW
355 * Disabling valid bit while a page is used by a device
356 * causes an uncorrectable DMA error.
357 * Workaround to avoid an uncorrectable DMA error is
358 * eliminating the next line, but the page is mapped
359 * until the next iommu_enter call.
361 is
->is_tsb
[IOTSBSLOT(va
,is
->is_tsbsize
)] &= ~IOTTE_V
;
363 bus_space_write_8(is
->is_bustag
, is
->is_iommu
,
364 IOMMUREG(iommu_flush
), va
);
370 iommu_strbuf_flush_done(struct strbuf_ctl
*sb
)
372 struct iommu_state
*is
= sb
->sb_is
;
373 struct timeval cur
, flushtimeout
;
375 #define BUMPTIME(t, usec) { \
376 register volatile struct timeval *tp = (t); \
379 tp->tv_usec = us = tp->tv_usec + (usec); \
380 if (us >= 1000000) { \
381 tp->tv_usec = us - 1000000; \
390 * Streaming buffer flushes:
392 * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
393 * we're not on a cache line boundary (64-bits):
395 * 3 Store pointer to flag in flushsync
396 * 4 wait till flushsync becomes 0x1
398 * If it takes more than .5 sec, something
403 bus_space_write_8(is
->is_bustag
, sb
->sb_sb
,
404 STRBUFREG(strbuf_flushsync
), sb
->sb_flushpa
);
406 microtime(&flushtimeout
);
408 BUMPTIME(&flushtimeout
, 500000); /* 1/2 sec */
410 DPRINTF(IDB_IOMMU
, ("iommu_strbuf_flush_done: flush = %lx "
411 "at va = %lx pa = %lx now=%"PRIx64
":%"PRIx32
" until = %"PRIx64
":%"PRIx32
"\n",
412 (long)*sb
->sb_flush
, (long)sb
->sb_flush
, (long)sb
->sb_flushpa
,
413 cur
.tv_sec
, cur
.tv_usec
,
414 flushtimeout
.tv_sec
, flushtimeout
.tv_usec
));
416 /* Bypass non-coherent D$ */
417 while ((!ldxa(sb
->sb_flushpa
, ASI_PHYS_CACHED
)) &&
418 timercmp(&cur
, &flushtimeout
, <=))
422 if (!ldxa(sb
->sb_flushpa
, ASI_PHYS_CACHED
)) {
423 printf("iommu_strbuf_flush_done: flush timeout %p, at %p\n",
424 (void *)(u_long
)*sb
->sb_flush
,
425 (void *)(u_long
)sb
->sb_flushpa
); /* panic? */
431 DPRINTF(IDB_IOMMU
, ("iommu_strbuf_flush_done: flushed\n"));
432 return (*sb
->sb_flush
);
436 * IOMMU DVMA operations, common to SBUS and PCI.
439 iommu_dvmamap_load(bus_dma_tag_t t
, bus_dmamap_t map
, void *buf
,
440 bus_size_t buflen
, struct proc
*p
, int flags
)
442 struct strbuf_ctl
*sb
= (struct strbuf_ctl
*)map
->_dm_cookie
;
443 struct iommu_state
*is
= sb
->sb_is
;
448 u_long dvmaddr
, sgstart
, sgend
, bmask
;
449 bus_size_t align
, boundary
, len
;
450 vaddr_t vaddr
= (vaddr_t
)buf
;
455 /* Already in use?? */
457 printf("iommu_dvmamap_load: map still in use\n");
459 bus_dmamap_unload(t
, map
);
463 * Make sure that on error condition we return "no valid mappings".
466 KASSERT(map
->dm_maxsegsz
<= map
->_dm_maxmaxsegsz
);
468 if (buflen
> map
->_dm_size
) {
470 ("iommu_dvmamap_load(): error %d > %d -- "
471 "map size exceeded!\n", (int)buflen
, (int)map
->_dm_size
));
475 sgsize
= round_page(buflen
+ ((int)vaddr
& PGOFSET
));
478 * A boundary presented to bus_dmamem_alloc() takes precedence
479 * over boundary in the map.
481 if ((boundary
= (map
->dm_segs
[0]._ds_boundary
)) == 0)
482 boundary
= map
->_dm_boundary
;
483 align
= max(map
->dm_segs
[0]._ds_align
, PAGE_SIZE
);
486 * If our segment size is larger than the boundary we need to
487 * split the transfer up int little pieces ourselves.
490 err
= extent_alloc(is
->is_dvmamap
, sgsize
, align
,
491 (sgsize
> boundary
) ? 0 : boundary
,
492 EX_NOWAIT
|EX_BOUNDZERO
, &dvmaddr
);
496 if (err
|| (dvmaddr
== (u_long
)-1)) {
497 printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
507 if (dvmaddr
== (u_long
)-1)
510 /* Set the active DVMA map */
511 map
->_dm_dvmastart
= dvmaddr
;
512 map
->_dm_dvmasize
= sgsize
;
515 * Now split the DVMA range into segments, not crossing
519 sgstart
= dvmaddr
+ (vaddr
& PGOFSET
);
520 sgend
= sgstart
+ buflen
- 1;
521 map
->dm_segs
[seg
].ds_addr
= sgstart
;
522 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load: boundary %lx boundary - 1 %lx "
523 "~(boundary - 1) %lx\n", (long)boundary
, (long)(boundary
- 1),
524 (long)~(boundary
- 1)));
525 bmask
= ~(boundary
- 1);
526 while ((sgstart
& bmask
) != (sgend
& bmask
) ||
527 sgend
- sgstart
+ 1 > map
->dm_maxsegsz
) {
528 /* Oops. We crossed a boundary or large seg. Split the xfer. */
529 len
= map
->dm_maxsegsz
;
530 if ((sgstart
& bmask
) != (sgend
& bmask
))
531 len
= min(len
, boundary
- (sgstart
& (boundary
- 1)));
532 map
->dm_segs
[seg
].ds_len
= len
;
533 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load: "
534 "seg %d start %lx size %lx\n", seg
,
535 (long)map
->dm_segs
[seg
].ds_addr
,
536 (long)map
->dm_segs
[seg
].ds_len
));
537 if (++seg
>= map
->_dm_segcnt
) {
538 /* Too many segments. Fail the operation. */
539 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load: "
540 "too many segments %d\n", seg
));
542 err
= extent_free(is
->is_dvmamap
,
543 dvmaddr
, sgsize
, EX_NOWAIT
);
544 map
->_dm_dvmastart
= 0;
545 map
->_dm_dvmasize
= 0;
548 printf("warning: %s: %" PRId64
549 " of DVMA space lost\n", __func__
, sgsize
);
553 map
->dm_segs
[seg
].ds_addr
= sgstart
;
555 map
->dm_segs
[seg
].ds_len
= sgend
- sgstart
+ 1;
556 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load: "
557 "seg %d start %lx size %lx\n", seg
,
558 (long)map
->dm_segs
[seg
].ds_addr
, (long)map
->dm_segs
[seg
].ds_len
));
559 map
->dm_nsegs
= seg
+ 1;
560 map
->dm_mapsize
= buflen
;
563 pmap
= p
->p_vmspace
->vm_map
.pmap
;
565 pmap
= pmap_kernel();
568 for (; buflen
> 0; ) {
571 * Get the physical address for this page.
573 if (pmap_extract(pmap
, (vaddr_t
)vaddr
, &curaddr
) == FALSE
) {
575 printf("iommu_dvmamap_load: pmap_extract failed %lx\n", vaddr
);
577 bus_dmamap_unload(t
, map
);
582 * Compute the segment size, and adjust counts.
584 sgsize
= PAGE_SIZE
- ((u_long
)vaddr
& PGOFSET
);
589 ("iommu_dvmamap_load: map %p loading va %p "
590 "dva %lx at pa %lx\n",
591 map
, (void *)vaddr
, (long)dvmaddr
,
592 (long)trunc_page(curaddr
)));
593 iommu_enter(sb
, trunc_page(dvmaddr
), trunc_page(curaddr
),
594 flags
| IOTTE_DEBUG(0x4000));
597 dvmaddr
+= PAGE_SIZE
;
602 iommu_strbuf_flush_done(sb
);
604 for (seg
= 0; seg
< map
->dm_nsegs
; seg
++) {
605 if (map
->dm_segs
[seg
].ds_addr
< is
->is_dvmabase
||
606 map
->dm_segs
[seg
].ds_addr
> is
->is_dvmaend
) {
607 printf("seg %d dvmaddr %lx out of range %x - %x\n",
608 seg
, (long)map
->dm_segs
[seg
].ds_addr
,
609 is
->is_dvmabase
, is
->is_dvmaend
);
621 iommu_dvmamap_unload(bus_dma_tag_t t
, bus_dmamap_t map
)
623 struct strbuf_ctl
*sb
= (struct strbuf_ctl
*)map
->_dm_cookie
;
624 struct iommu_state
*is
= sb
->sb_is
;
626 bus_size_t sgsize
= map
->_dm_dvmasize
;
628 /* Flush the iommu */
630 if (!map
->_dm_dvmastart
) {
631 printf("iommu_dvmamap_unload: No dvmastart is zero\n");
637 iommu_remove(is
, map
->_dm_dvmastart
, map
->_dm_dvmasize
);
639 /* Flush the caches */
640 bus_dmamap_unload(t
->_parent
, map
);
642 /* Mark the mappings as invalid. */
647 error
= extent_free(is
->is_dvmamap
, map
->_dm_dvmastart
,
648 map
->_dm_dvmasize
, EX_NOWAIT
);
649 map
->_dm_dvmastart
= 0;
650 map
->_dm_dvmasize
= 0;
653 printf("warning: %s: %" PRId64
" of DVMA space lost\n",
661 iommu_dvmamap_load_raw(bus_dma_tag_t t
, bus_dmamap_t map
,
662 bus_dma_segment_t
*segs
, int nsegs
, bus_size_t size
, int flags
)
664 struct strbuf_ctl
*sb
= (struct strbuf_ctl
*)map
->_dm_cookie
;
665 struct iommu_state
*is
= sb
->sb_is
;
672 bus_size_t boundary
, align
;
673 u_long dvmaddr
, sgstart
, sgend
, bmask
;
674 struct pglist
*pglist
;
675 const int pagesz
= PAGE_SIZE
;
681 /* Already in use?? */
683 printf("iommu_dvmamap_load_raw: map still in use\n");
685 bus_dmamap_unload(t
, map
);
689 * A boundary presented to bus_dmamem_alloc() takes precedence
690 * over boundary in the map.
692 if ((boundary
= segs
[0]._ds_boundary
) == 0)
693 boundary
= map
->_dm_boundary
;
695 align
= max(segs
[0]._ds_align
, pagesz
);
698 * Make sure that on error condition we return "no valid mappings".
701 /* Count up the total number of pages we need */
702 pa
= trunc_page(segs
[0].ds_addr
);
705 for (i
= 0; left
> 0 && i
< nsegs
; i
++) {
706 if (round_page(pa
) != round_page(segs
[i
].ds_addr
))
707 sgsize
= round_page(sgsize
) +
708 (segs
[i
].ds_addr
& PGOFSET
);
709 sgsize
+= min(left
, segs
[i
].ds_len
);
710 left
-= segs
[i
].ds_len
;
711 pa
= segs
[i
].ds_addr
+ segs
[i
].ds_len
;
713 sgsize
= round_page(sgsize
);
717 * If our segment size is larger than the boundary we need to
718 * split the transfer up into little pieces ourselves.
720 err
= extent_alloc(is
->is_dvmamap
, sgsize
, align
,
721 (sgsize
> boundary
) ? 0 : boundary
,
722 ((flags
& BUS_DMA_NOWAIT
) == 0 ? EX_WAITOK
: EX_NOWAIT
) |
723 EX_BOUNDZERO
, &dvmaddr
);
730 if (dvmaddr
== (u_long
)-1)
732 printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
739 if (dvmaddr
== (u_long
)-1)
742 /* Set the active DVMA map */
743 map
->_dm_dvmastart
= dvmaddr
;
744 map
->_dm_dvmasize
= sgsize
;
746 bmask
= ~(boundary
- 1);
747 if ((pglist
= segs
[0]._ds_mlist
) == NULL
) {
748 u_long prev_va
= 0UL, last_va
= dvmaddr
;
751 bus_size_t len
= size
;
754 * This segs is made up of individual physical
755 * segments, probably by _bus_dmamap_load_uio() or
756 * _bus_dmamap_load_mbuf(). Ignore the mlist and
757 * load each one individually.
761 for (i
= 0; i
< nsegs
; i
++) {
763 pa
= segs
[i
].ds_addr
;
764 offset
= (pa
& PGOFSET
);
766 dvmaddr
= trunc_page(dvmaddr
);
767 left
= min(len
, segs
[i
].ds_len
);
769 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load_raw: converting "
770 "physseg %d start %lx size %lx\n", i
,
771 (long)segs
[i
].ds_addr
, (long)segs
[i
].ds_len
));
773 if ((pa
== prev_pa
) &&
774 ((offset
!= 0) || (end
!= offset
))) {
775 /* We can re-use this mapping */
779 sgstart
= dvmaddr
+ offset
;
780 sgend
= sgstart
+ left
- 1;
782 /* Are the segments virtually adjacent? */
783 if ((j
> 0) && (end
== offset
) &&
784 ((offset
== 0) || (pa
== prev_pa
)) &&
785 (map
->dm_segs
[j
-1].ds_len
+ left
<=
787 /* Just append to the previous segment. */
788 map
->dm_segs
[--j
].ds_len
+= left
;
789 /* Restore sgstart for boundary check */
790 sgstart
= map
->dm_segs
[j
].ds_addr
;
791 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load_raw: "
792 "appending seg %d start %lx size %lx\n", j
,
793 (long)map
->dm_segs
[j
].ds_addr
,
794 (long)map
->dm_segs
[j
].ds_len
));
796 if (j
>= map
->_dm_segcnt
) {
797 iommu_remove(is
, map
->_dm_dvmastart
,
798 last_va
- map
->_dm_dvmastart
);
801 map
->dm_segs
[j
].ds_addr
= sgstart
;
802 map
->dm_segs
[j
].ds_len
= left
;
803 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load_raw: "
804 "seg %d start %lx size %lx\n", j
,
805 (long)map
->dm_segs
[j
].ds_addr
,
806 (long)map
->dm_segs
[j
].ds_len
));
808 end
= (offset
+ left
) & PGOFSET
;
810 /* Check for boundary issues */
811 while ((sgstart
& bmask
) != (sgend
& bmask
)) {
812 /* Need a new segment. */
813 map
->dm_segs
[j
].ds_len
=
814 boundary
- (sgstart
& (boundary
- 1));
815 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load_raw: "
816 "seg %d start %lx size %lx\n", j
,
817 (long)map
->dm_segs
[j
].ds_addr
,
818 (long)map
->dm_segs
[j
].ds_len
));
819 if (++j
>= map
->_dm_segcnt
) {
820 iommu_remove(is
, map
->_dm_dvmastart
,
821 last_va
- map
->_dm_dvmastart
);
824 sgstart
+= map
->dm_segs
[j
-1].ds_len
;
825 map
->dm_segs
[j
].ds_addr
= sgstart
;
826 map
->dm_segs
[j
].ds_len
= sgend
- sgstart
+ 1;
830 panic("iommu_dmamap_load_raw: size botch");
832 /* Now map a series of pages. */
833 while (dvmaddr
<= sgend
) {
835 ("iommu_dvmamap_load_raw: map %p "
836 "loading va %lx at pa %lx\n",
839 /* Enter it if we haven't before. */
840 if (prev_va
!= dvmaddr
) {
841 iommu_enter(sb
, prev_va
= dvmaddr
,
843 flags
| IOTTE_DEBUG(++npg
<< 12));
855 iommu_strbuf_flush_done(sb
);
857 map
->dm_mapsize
= size
;
861 for (seg
= 0; seg
< map
->dm_nsegs
; seg
++) {
862 if (map
->dm_segs
[seg
].ds_addr
< is
->is_dvmabase
||
863 map
->dm_segs
[seg
].ds_addr
> is
->is_dvmaend
) {
864 printf("seg %d dvmaddr %lx out of range %x - %x\n",
865 seg
, (long)map
->dm_segs
[seg
].ds_addr
,
866 is
->is_dvmabase
, is
->is_dvmaend
);
878 * This was allocated with bus_dmamem_alloc.
879 * The pages are on a `pglist'.
883 sgend
= sgstart
+ size
- 1;
884 map
->dm_segs
[i
].ds_addr
= sgstart
;
885 while ((sgstart
& bmask
) != (sgend
& bmask
)) {
886 /* Oops. We crossed a boundary. Split the xfer. */
887 map
->dm_segs
[i
].ds_len
= boundary
- (sgstart
& (boundary
- 1));
888 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load_raw: "
889 "seg %d start %lx size %lx\n", i
,
890 (long)map
->dm_segs
[i
].ds_addr
,
891 (long)map
->dm_segs
[i
].ds_len
));
892 if (++i
>= map
->_dm_segcnt
) {
893 /* Too many segments. Fail the operation. */
896 sgstart
+= map
->dm_segs
[i
-1].ds_len
;
897 map
->dm_segs
[i
].ds_addr
= sgstart
;
899 DPRINTF(IDB_INFO
, ("iommu_dvmamap_load_raw: "
900 "seg %d start %lx size %lx\n", i
,
901 (long)map
->dm_segs
[i
].ds_addr
, (long)map
->dm_segs
[i
].ds_len
));
902 map
->dm_segs
[i
].ds_len
= sgend
- sgstart
+ 1;
905 TAILQ_FOREACH(pg
, pglist
, pageq
.queue
) {
907 panic("iommu_dmamap_load_raw: size botch");
908 pa
= VM_PAGE_TO_PHYS(pg
);
911 ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
912 map
, (long)dvmaddr
, (long)(pa
)));
913 iommu_enter(sb
, dvmaddr
, pa
, flags
| IOTTE_DEBUG(0x8000));
920 iommu_strbuf_flush_done(sb
);
921 map
->dm_mapsize
= size
;
925 for (seg
= 0; seg
< map
->dm_nsegs
; seg
++) {
926 if (map
->dm_segs
[seg
].ds_addr
< is
->is_dvmabase
||
927 map
->dm_segs
[seg
].ds_addr
> is
->is_dvmaend
) {
928 printf("seg %d dvmaddr %lx out of range %x - %x\n",
929 seg
, (long)map
->dm_segs
[seg
].ds_addr
,
930 is
->is_dvmabase
, is
->is_dvmaend
);
942 err
= extent_free(is
->is_dvmamap
, map
->_dm_dvmastart
, sgsize
,
944 map
->_dm_dvmastart
= 0;
945 map
->_dm_dvmasize
= 0;
948 printf("warning: %s: %" PRId64
" of DVMA space lost\n",
955 * Flush an individual dma segment, returns non-zero if the streaming buffers
956 * need flushing afterwards.
959 iommu_dvmamap_sync_range(struct strbuf_ctl
*sb
, vaddr_t va
, bus_size_t len
)
962 struct iommu_state
*is
= sb
->sb_is
;
965 if (va
< is
->is_dvmabase
|| va
> is
->is_dvmaend
)
966 panic("invalid va: %llx", (long long)va
);
969 if ((is
->is_tsb
[IOTSBSLOT(va
, is
->is_tsbsize
)] & IOTTE_STREAM
) == 0) {
971 ("iommu_dvmamap_sync_range: attempting to flush "
972 "non-streaming entry\n"));
976 vaend
= round_page(va
+ len
) - 1;
980 if (va
< is
->is_dvmabase
|| vaend
> is
->is_dvmaend
)
981 panic("invalid va range: %llx to %llx (%x to %x)",
982 (long long)va
, (long long)vaend
,
987 for ( ; va
<= vaend
; va
+= PAGE_SIZE
) {
989 ("iommu_dvmamap_sync_range: flushing va %p\n",
990 (void *)(u_long
)va
));
991 iommu_strbuf_flush(sb
, va
);
998 _iommu_dvmamap_sync(bus_dma_tag_t t
, bus_dmamap_t map
, bus_addr_t offset
,
999 bus_size_t len
, int ops
)
1001 struct strbuf_ctl
*sb
= (struct strbuf_ctl
*)map
->_dm_cookie
;
1003 int i
, needsflush
= 0;
1008 for (i
= 0; i
< map
->dm_nsegs
; i
++) {
1009 if (offset
< map
->dm_segs
[i
].ds_len
)
1011 offset
-= map
->dm_segs
[i
].ds_len
;
1014 if (i
== map
->dm_nsegs
)
1015 panic("iommu_dvmamap_sync: segment too short %llu",
1016 (unsigned long long)offset
);
1018 if (ops
& (BUS_DMASYNC_PREREAD
| BUS_DMASYNC_POSTWRITE
)) {
1019 /* Nothing to do */;
1022 if (ops
& (BUS_DMASYNC_POSTREAD
| BUS_DMASYNC_PREWRITE
)) {
1024 for (; len
> 0 && i
< map
->dm_nsegs
; i
++) {
1025 count
= MIN(map
->dm_segs
[i
].ds_len
- offset
, len
);
1027 iommu_dvmamap_sync_range(sb
,
1028 map
->dm_segs
[i
].ds_addr
+ offset
, count
))
1034 if (i
== map
->dm_nsegs
&& len
> 0)
1035 panic("iommu_dvmamap_sync: leftover %llu",
1036 (unsigned long long)len
);
1040 iommu_strbuf_flush_done(sb
);
1045 iommu_dvmamap_sync(bus_dma_tag_t t
, bus_dmamap_t map
, bus_addr_t offset
,
1046 bus_size_t len
, int ops
)
1049 /* If len is 0, then there is nothing to do */
1053 if (ops
& (BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
)) {
1054 /* Flush the CPU then the IOMMU */
1055 bus_dmamap_sync(t
->_parent
, map
, offset
, len
, ops
);
1056 _iommu_dvmamap_sync(t
, map
, offset
, len
, ops
);
1058 if (ops
& (BUS_DMASYNC_POSTREAD
| BUS_DMASYNC_POSTWRITE
)) {
1059 /* Flush the IOMMU then the CPU */
1060 _iommu_dvmamap_sync(t
, map
, offset
, len
, ops
);
1061 bus_dmamap_sync(t
->_parent
, map
, offset
, len
, ops
);
1066 iommu_dvmamem_alloc(bus_dma_tag_t t
, bus_size_t size
, bus_size_t alignment
,
1067 bus_size_t boundary
, bus_dma_segment_t
*segs
, int nsegs
, int *rsegs
,
1071 DPRINTF(IDB_BUSDMA
, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
1072 "segp %p flags %d\n", (unsigned long long)size
,
1073 (unsigned long long)alignment
, (unsigned long long)boundary
,
1075 return (bus_dmamem_alloc(t
->_parent
, size
, alignment
, boundary
,
1076 segs
, nsegs
, rsegs
, flags
|BUS_DMA_DVMA
));
1080 iommu_dvmamem_free(bus_dma_tag_t t
, bus_dma_segment_t
*segs
, int nsegs
)
1083 DPRINTF(IDB_BUSDMA
, ("iommu_dvmamem_free: segp %p nsegs %d\n",
1085 bus_dmamem_free(t
->_parent
, segs
, nsegs
);
1089 * Map the DVMA mappings into the kernel pmap.
1090 * Check the flags to see whether we're streaming or coherent.
1093 iommu_dvmamem_map(bus_dma_tag_t t
, bus_dma_segment_t
*segs
, int nsegs
,
1094 size_t size
, void **kvap
, int flags
)
1099 struct pglist
*pglist
;
1101 const uvm_flag_t kmflags
=
1102 (flags
& BUS_DMA_NOWAIT
) != 0 ? UVM_KMF_NOWAIT
: 0;
1104 DPRINTF(IDB_BUSDMA
, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
1105 segs
, nsegs
, size
));
1108 * Allocate some space in the kernel map, and then map these pages
1111 size
= round_page(size
);
1112 va
= uvm_km_alloc(kernel_map
, size
, 0, UVM_KMF_VAONLY
| kmflags
);
1122 if (flags
& BUS_DMA_COHERENT
) /* Disable vcache */
1124 if (flags
& BUS_DMA_NOCACHE
) /* sideffects */
1128 * Now take this and map it into the CPU.
1130 pglist
= segs
[0]._ds_mlist
;
1131 TAILQ_FOREACH(pg
, pglist
, pageq
.queue
) {
1134 panic("iommu_dvmamem_map: size botch");
1136 addr
= VM_PAGE_TO_PHYS(pg
);
1137 DPRINTF(IDB_BUSDMA
, ("iommu_dvmamem_map: "
1138 "mapping va %lx at %llx\n", va
, (unsigned long long)addr
| cbit
));
1139 pmap_kenter_pa(va
, addr
| cbit
,
1140 VM_PROT_READ
| VM_PROT_WRITE
, 0);
1144 pmap_update(pmap_kernel());
1149 * Unmap DVMA mappings from kernel
1152 iommu_dvmamem_unmap(bus_dma_tag_t t
, void *kva
, size_t size
)
1155 DPRINTF(IDB_BUSDMA
, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
1159 if ((u_long
)kva
& PGOFSET
)
1160 panic("iommu_dvmamem_unmap");
1163 size
= round_page(size
);
1164 pmap_kremove((vaddr_t
)kva
, size
);
1165 pmap_update(pmap_kernel());
1166 uvm_km_free(kernel_map
, (vaddr_t
)kva
, size
, UVM_KMF_VAONLY
);