iommu.c revision 1.94 1 /* $NetBSD: iommu.c,v 1.94 2009/12/07 11:24:30 nakayama 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 *
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
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 2001, 2002 Eduardo Horvath
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
43 *
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
54 * SUCH DAMAGE.
55 */
56
57 /*
58 * UltraSPARC IOMMU support; used by both the sbus and pci code.
59 */
60
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: iommu.c,v 1.94 2009/12/07 11:24:30 nakayama Exp $");
63
64 #include "opt_ddb.h"
65
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>
71 #include <sys/proc.h>
72
73 #include <uvm/uvm_extern.h>
74
75 #include <machine/bus.h>
76 #include <sparc64/dev/iommureg.h>
77 #include <sparc64/dev/iommuvar.h>
78
79 #include <machine/autoconf.h>
80 #include <machine/cpu.h>
81
82 #ifdef DEBUG
83 #define IDB_BUSDMA 0x1
84 #define IDB_IOMMU 0x2
85 #define IDB_INFO 0x4
86 #define IDB_SYNC 0x8
87 int iommudebug = 0x0;
88 #define DPRINTF(l, s) do { if (iommudebug & l) printf s; } while (0)
89 #define IOTTE_DEBUG(n) (n)
90 #else
91 #define DPRINTF(l, s)
92 #define IOTTE_DEBUG(n) 0
93 #endif
94
95 #define iommu_strbuf_flush(i, v) do { \
96 if ((i)->sb_flush) \
97 bus_space_write_8((i)->sb_is->is_bustag, (i)->sb_sb, \
98 STRBUFREG(strbuf_pgflush), (v)); \
99 } while (0)
100
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,
103 bus_size_t, int);
104
105 /*
106 * initialise the UltraSPARC IOMMU (SBUS or PCI):
107 * - allocate and setup the iotsb.
108 * - enable the IOMMU
109 * - initialise the streaming buffers (if they exist)
110 * - create a private DVMA map.
111 */
112 void
113 iommu_init(char *name, struct iommu_state *is, int tsbsize, uint32_t iovabase)
114 {
115 psize_t size;
116 vaddr_t va;
117 paddr_t pa;
118 struct vm_page *pg;
119 struct pglist pglist;
120
121 /*
122 * Setup the iommu.
123 *
124 * The sun4u iommu is part of the SBUS or PCI controller so we will
125 * deal with it here..
126 *
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)).
132 *
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.
136 */
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;
142 } else {
143 is->is_dvmabase = iovabase;
144 is->is_dvmaend = iovabase + IOTSB_VSIZE(tsbsize) - 1;
145 }
146
147 /*
148 * Allocate memory for I/O pagetables. They need to be physically
149 * contiguous.
150 */
151
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");
156
157 va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY);
158 if (va == 0)
159 panic("iommu_init: no memory");
160 is->is_tsb = (int64_t *)va;
161
162 is->is_ptsb = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist));
163
164 /* Map the pages */
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);
169 va += PAGE_SIZE;
170 }
171 pmap_update(pmap_kernel());
172 memset(is->is_tsb, 0, size);
173
174 #ifdef DEBUG
175 if (iommudebug & IDB_INFO)
176 {
177 /* Probe the iommu */
178
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,
188 is->is_iommu,
189 offsetof (struct iommureg, iommu_cr)),
190 (unsigned long long)bus_space_read_8(is->is_bustag,
191 is->is_iommu,
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 */
196 }
197 #endif
198
199 /*
200 * now actually start up the IOMMU
201 */
202 iommu_reset(is);
203
204 /*
205 * Now all the hardware's working we need to allocate a dvma map.
206 */
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);
216 }
217
218 /*
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.
222 */
223 void
224 iommu_reset(struct iommu_state *is)
225 {
226 int i;
227 struct strbuf_ctl *sb;
228
229 /* Need to do 64-bit stores */
230 bus_space_write_8(is->is_bustag, is->is_iommu, IOMMUREG(iommu_tsb),
231 is->is_ptsb);
232
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);
236
237 for (i = 0; i < 2; i++) {
238 if ((sb = is->is_sb[i])) {
239
240 /* Enable diagnostics mode? */
241 bus_space_write_8(is->is_bustag, is->is_sb[i]->sb_sb,
242 STRBUFREG(strbuf_ctl), STRBUF_EN);
243
244 /* No streaming buffers? Disable them */
245 if (bus_space_read_8(is->is_bustag,
246 is->is_sb[i]->sb_sb,
247 STRBUFREG(strbuf_ctl)) == 0) {
248 is->is_sb[i]->sb_flush = NULL;
249 } else {
250
251 /*
252 * locate the pa of the flush buffer.
253 */
254 (void)pmap_extract(pmap_kernel(),
255 (vaddr_t)is->is_sb[i]->sb_flush,
256 &is->is_sb[i]->sb_flushpa);
257 }
258 }
259 }
260 }
261
262 /*
263 * Here are the iommu control routines.
264 */
265 void
266 iommu_enter(struct strbuf_ctl *sb, vaddr_t va, int64_t pa, int flags)
267 {
268 struct iommu_state *is = sb->sb_is;
269 int strbuf = (flags & BUS_DMA_STREAMING);
270 int64_t tte;
271
272 #ifdef DIAGNOSTIC
273 if (va < is->is_dvmabase || va > is->is_dvmaend)
274 panic("iommu_enter: va %#lx not in DVMA space", va);
275 #endif
276
277 /* Is the streamcache flush really needed? */
278 if (sb->sb_flush)
279 iommu_strbuf_flush(sb, va);
280 else
281 /* If we can't flush the strbuf don't enable it. */
282 strbuf = 0;
283
284 tte = MAKEIOTTE(pa, !(flags & BUS_DMA_NOWRITE),
285 !(flags & BUS_DMA_NOCACHE), (strbuf));
286 #ifdef DEBUG
287 tte |= (flags & 0xff000LL)<<(4*8);
288 #endif
289
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)],
298 (u_long)tte));
299 }
300
301 /*
302 * Find the value of a DVMA address (debug routine).
303 */
304 paddr_t
305 iommu_extract(struct iommu_state *is, vaddr_t dva)
306 {
307 int64_t tte = 0;
308
309 if (dva >= is->is_dvmabase && dva <= is->is_dvmaend)
310 tte = is->is_tsb[IOTSBSLOT(dva, is->is_tsbsize)];
311
312 if ((tte & IOTTE_V) == 0)
313 return ((paddr_t)-1L);
314 return (tte & IOTTE_PAMASK);
315 }
316
317 /*
318 * iommu_remove: removes mappings created by iommu_enter
319 *
320 * Only demap from IOMMU if flag is set.
321 *
322 * XXX: this function needs better internal error checking.
323 */
324 void
325 iommu_remove(struct iommu_state *is, vaddr_t va, size_t len)
326 {
327
328 #ifdef DIAGNOSTIC
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);
336 #endif
337
338 va = trunc_page(va);
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)]));
342 while (len > 0) {
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,
346 (u_long)len));
347 if (len <= PAGE_SIZE)
348 len = 0;
349 else
350 len -= PAGE_SIZE;
351
352 /*
353 * XXX Zero-ing the entry would not require RMW
354 *
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.
360 */
361 is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] &= ~IOTTE_V;
362 membar_storestore();
363 bus_space_write_8(is->is_bustag, is->is_iommu,
364 IOMMUREG(iommu_flush), va);
365 va += PAGE_SIZE;
366 }
367 }
368
369 static int
370 iommu_strbuf_flush_done(struct strbuf_ctl *sb)
371 {
372 struct iommu_state *is = sb->sb_is;
373 struct timeval cur, flushtimeout;
374
375 #define BUMPTIME(t, usec) { \
376 register volatile struct timeval *tp = (t); \
377 register long us; \
378 \
379 tp->tv_usec = us = tp->tv_usec + (usec); \
380 if (us >= 1000000) { \
381 tp->tv_usec = us - 1000000; \
382 tp->tv_sec++; \
383 } \
384 }
385
386 if (!sb->sb_flush)
387 return (0);
388
389 /*
390 * Streaming buffer flushes:
391 *
392 * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
393 * we're not on a cache line boundary (64-bits):
394 * 2 Store 0 in flag
395 * 3 Store pointer to flag in flushsync
396 * 4 wait till flushsync becomes 0x1
397 *
398 * If it takes more than .5 sec, something
399 * went wrong.
400 */
401
402 *sb->sb_flush = 0;
403 bus_space_write_8(is->is_bustag, sb->sb_sb,
404 STRBUFREG(strbuf_flushsync), sb->sb_flushpa);
405
406 microtime(&flushtimeout);
407 cur = flushtimeout;
408 BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
409
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));
415
416 /* Bypass non-coherent D$ */
417 while ((!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) &&
418 timercmp(&cur, &flushtimeout, <=))
419 microtime(&cur);
420
421 #ifdef DIAGNOSTIC
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? */
426 #ifdef DDB
427 Debugger();
428 #endif
429 }
430 #endif
431 DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flushed\n"));
432 return (*sb->sb_flush);
433 }
434
435 /*
436 * IOMMU DVMA operations, common to SBUS and PCI.
437 */
438 int
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)
441 {
442 struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
443 struct iommu_state *is = sb->sb_is;
444 int s;
445 int err, needsflush;
446 bus_size_t sgsize;
447 paddr_t curaddr;
448 u_long dvmaddr, sgstart, sgend, bmask;
449 bus_size_t align, boundary, len;
450 vaddr_t vaddr = (vaddr_t)buf;
451 int seg;
452 struct pmap *pmap;
453
454 if (map->dm_nsegs) {
455 /* Already in use?? */
456 #ifdef DIAGNOSTIC
457 printf("iommu_dvmamap_load: map still in use\n");
458 #endif
459 bus_dmamap_unload(t, map);
460 }
461
462 /*
463 * Make sure that on error condition we return "no valid mappings".
464 */
465 map->dm_nsegs = 0;
466 if (buflen > map->_dm_size) {
467 DPRINTF(IDB_BUSDMA,
468 ("iommu_dvmamap_load(): error %d > %d -- "
469 "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
470 return (EINVAL);
471 }
472
473 sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
474
475 /*
476 * A boundary presented to bus_dmamem_alloc() takes precedence
477 * over boundary in the map.
478 */
479 if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
480 boundary = map->_dm_boundary;
481 align = max(map->dm_segs[0]._ds_align, PAGE_SIZE);
482
483 /*
484 * If our segment size is larger than the boundary we need to
485 * split the transfer up int little pieces ourselves.
486 */
487 s = splhigh();
488 err = extent_alloc(is->is_dvmamap, sgsize, align,
489 (sgsize > boundary) ? 0 : boundary,
490 EX_NOWAIT|EX_BOUNDZERO, &dvmaddr);
491 splx(s);
492
493 #ifdef DEBUG
494 if (err || (dvmaddr == (u_long)-1)) {
495 printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
496 (int)sgsize, flags);
497 #ifdef DDB
498 Debugger();
499 #endif
500 }
501 #endif
502 if (err != 0)
503 return (err);
504
505 if (dvmaddr == (u_long)-1)
506 return (ENOMEM);
507
508 /* Set the active DVMA map */
509 map->_dm_dvmastart = dvmaddr;
510 map->_dm_dvmasize = sgsize;
511
512 /*
513 * Now split the DVMA range into segments, not crossing
514 * the boundary.
515 */
516 seg = 0;
517 sgstart = dvmaddr + (vaddr & PGOFSET);
518 sgend = sgstart + buflen - 1;
519 map->dm_segs[seg].ds_addr = sgstart;
520 DPRINTF(IDB_INFO, ("iommu_dvmamap_load: boundary %lx boundary - 1 %lx "
521 "~(boundary - 1) %lx\n", (long)boundary, (long)(boundary - 1),
522 (long)~(boundary - 1)));
523 bmask = ~(boundary - 1);
524 while ((sgstart & bmask) != (sgend & bmask)) {
525 /* Oops. We crossed a boundary. Split the xfer. */
526 len = boundary - (sgstart & (boundary - 1));
527 map->dm_segs[seg].ds_len = len;
528 DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
529 "seg %d start %lx size %lx\n", seg,
530 (long)map->dm_segs[seg].ds_addr,
531 (long)map->dm_segs[seg].ds_len));
532 if (++seg >= map->_dm_segcnt) {
533 /* Too many segments. Fail the operation. */
534 DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
535 "too many segments %d\n", seg));
536 s = splhigh();
537 /* How can this fail? And if it does what can we do? */
538 err = extent_free(is->is_dvmamap,
539 dvmaddr, sgsize, EX_NOWAIT);
540 map->_dm_dvmastart = 0;
541 map->_dm_dvmasize = 0;
542 splx(s);
543 return (EFBIG);
544 }
545 sgstart += len;
546 map->dm_segs[seg].ds_addr = sgstart;
547 }
548 map->dm_segs[seg].ds_len = sgend - sgstart + 1;
549 DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
550 "seg %d start %lx size %lx\n", seg,
551 (long)map->dm_segs[seg].ds_addr, (long)map->dm_segs[seg].ds_len));
552 map->dm_nsegs = seg + 1;
553 map->dm_mapsize = buflen;
554
555 if (p != NULL)
556 pmap = p->p_vmspace->vm_map.pmap;
557 else
558 pmap = pmap_kernel();
559
560 needsflush = 0;
561 for (; buflen > 0; ) {
562
563 /*
564 * Get the physical address for this page.
565 */
566 if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
567 #ifdef DIAGNOSTIC
568 printf("iommu_dvmamap_load: pmap_extract failed %lx\n", vaddr);
569 #endif
570 bus_dmamap_unload(t, map);
571 return (-1);
572 }
573
574 /*
575 * Compute the segment size, and adjust counts.
576 */
577 sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
578 if (buflen < sgsize)
579 sgsize = buflen;
580
581 DPRINTF(IDB_BUSDMA,
582 ("iommu_dvmamap_load: map %p loading va %p "
583 "dva %lx at pa %lx\n",
584 map, (void *)vaddr, (long)dvmaddr,
585 (long)trunc_page(curaddr)));
586 iommu_enter(sb, trunc_page(dvmaddr), trunc_page(curaddr),
587 flags | IOTTE_DEBUG(0x4000));
588 needsflush = 1;
589
590 dvmaddr += PAGE_SIZE;
591 vaddr += sgsize;
592 buflen -= sgsize;
593 }
594 if (needsflush)
595 iommu_strbuf_flush_done(sb);
596 #ifdef DIAGNOSTIC
597 for (seg = 0; seg < map->dm_nsegs; seg++) {
598 if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
599 map->dm_segs[seg].ds_addr > is->is_dvmaend) {
600 printf("seg %d dvmaddr %lx out of range %x - %x\n",
601 seg, (long)map->dm_segs[seg].ds_addr,
602 is->is_dvmabase, is->is_dvmaend);
603 #ifdef DDB
604 Debugger();
605 #endif
606 }
607 }
608 #endif
609 return (0);
610 }
611
612
613 void
614 iommu_dvmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
615 {
616 struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
617 struct iommu_state *is = sb->sb_is;
618 int error, s;
619 bus_size_t sgsize = map->_dm_dvmasize;
620
621 /* Flush the iommu */
622 #ifdef DEBUG
623 if (!map->_dm_dvmastart) {
624 printf("iommu_dvmamap_unload: No dvmastart is zero\n");
625 #ifdef DDB
626 Debugger();
627 #endif
628 }
629 #endif
630 iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize);
631
632 /* Flush the caches */
633 bus_dmamap_unload(t->_parent, map);
634
635 /* Mark the mappings as invalid. */
636 map->dm_mapsize = 0;
637 map->dm_nsegs = 0;
638
639 s = splhigh();
640 error = extent_free(is->is_dvmamap, map->_dm_dvmastart,
641 map->_dm_dvmasize, EX_NOWAIT);
642 map->_dm_dvmastart = 0;
643 map->_dm_dvmasize = 0;
644 splx(s);
645 if (error != 0)
646 printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
647
648 /* Clear the map */
649 }
650
651
652 int
653 iommu_dvmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
654 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
655 {
656 struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
657 struct iommu_state *is = sb->sb_is;
658 struct vm_page *pg;
659 int i, j, s;
660 int left;
661 int err, needsflush;
662 bus_size_t sgsize;
663 paddr_t pa;
664 bus_size_t boundary, align;
665 u_long dvmaddr, sgstart, sgend, bmask;
666 struct pglist *pglist;
667 const int pagesz = PAGE_SIZE;
668 #ifdef DEBUG
669 int npg = 0;
670 #endif
671
672 if (map->dm_nsegs) {
673 /* Already in use?? */
674 #ifdef DIAGNOSTIC
675 printf("iommu_dvmamap_load_raw: map still in use\n");
676 #endif
677 bus_dmamap_unload(t, map);
678 }
679
680 /*
681 * A boundary presented to bus_dmamem_alloc() takes precedence
682 * over boundary in the map.
683 */
684 if ((boundary = segs[0]._ds_boundary) == 0)
685 boundary = map->_dm_boundary;
686
687 align = max(segs[0]._ds_align, pagesz);
688
689 /*
690 * Make sure that on error condition we return "no valid mappings".
691 */
692 map->dm_nsegs = 0;
693 /* Count up the total number of pages we need */
694 pa = trunc_page(segs[0].ds_addr);
695 sgsize = 0;
696 left = size;
697 for (i = 0; left > 0 && i < nsegs; i++) {
698 if (round_page(pa) != round_page(segs[i].ds_addr))
699 sgsize = round_page(sgsize) +
700 (segs[i].ds_addr & PGOFSET);
701 sgsize += min(left, segs[i].ds_len);
702 left -= segs[i].ds_len;
703 pa = segs[i].ds_addr + segs[i].ds_len;
704 }
705 sgsize = round_page(sgsize);
706
707 s = splhigh();
708 /*
709 * If our segment size is larger than the boundary we need to
710 * split the transfer up into little pieces ourselves.
711 */
712 err = extent_alloc(is->is_dvmamap, sgsize, align,
713 (sgsize > boundary) ? 0 : boundary,
714 ((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT) |
715 EX_BOUNDZERO, &dvmaddr);
716 splx(s);
717
718 if (err != 0)
719 return (err);
720
721 #ifdef DEBUG
722 if (dvmaddr == (u_long)-1)
723 {
724 printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
725 (int)sgsize, flags);
726 #ifdef DDB
727 Debugger();
728 #endif
729 }
730 #endif
731 if (dvmaddr == (u_long)-1)
732 return (ENOMEM);
733
734 /* Set the active DVMA map */
735 map->_dm_dvmastart = dvmaddr;
736 map->_dm_dvmasize = sgsize;
737
738 bmask = ~(boundary - 1);
739 if ((pglist = segs[0]._ds_mlist) == NULL) {
740 u_long prev_va = 0UL, last_va = dvmaddr;
741 paddr_t prev_pa = 0;
742 int end = 0, offset;
743 bus_size_t len = size;
744
745 /*
746 * This segs is made up of individual physical
747 * segments, probably by _bus_dmamap_load_uio() or
748 * _bus_dmamap_load_mbuf(). Ignore the mlist and
749 * load each one individually.
750 */
751 j = 0;
752 needsflush = 0;
753 for (i = 0; i < nsegs ; i++) {
754
755 pa = segs[i].ds_addr;
756 offset = (pa & PGOFSET);
757 pa = trunc_page(pa);
758 dvmaddr = trunc_page(dvmaddr);
759 left = min(len, segs[i].ds_len);
760
761 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: converting "
762 "physseg %d start %lx size %lx\n", i,
763 (long)segs[i].ds_addr, (long)segs[i].ds_len));
764
765 if ((pa == prev_pa) &&
766 ((offset != 0) || (end != offset))) {
767 /* We can re-use this mapping */
768 dvmaddr = prev_va;
769 }
770
771 sgstart = dvmaddr + offset;
772 sgend = sgstart + left - 1;
773
774 /* Are the segments virtually adjacent? */
775 if ((j > 0) && (end == offset) &&
776 ((offset == 0) || (pa == prev_pa))) {
777 /* Just append to the previous segment. */
778 map->dm_segs[--j].ds_len += left;
779 /* Restore sgstart for boundary check */
780 sgstart = map->dm_segs[j].ds_addr;
781 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
782 "appending seg %d start %lx size %lx\n", j,
783 (long)map->dm_segs[j].ds_addr,
784 (long)map->dm_segs[j].ds_len));
785 } else {
786 if (j >= map->_dm_segcnt) {
787 iommu_remove(is, map->_dm_dvmastart,
788 last_va - map->_dm_dvmastart);
789 goto fail;
790 }
791 map->dm_segs[j].ds_addr = sgstart;
792 map->dm_segs[j].ds_len = left;
793 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
794 "seg %d start %lx size %lx\n", j,
795 (long)map->dm_segs[j].ds_addr,
796 (long)map->dm_segs[j].ds_len));
797 }
798 end = (offset + left) & PGOFSET;
799
800 /* Check for boundary issues */
801 while ((sgstart & bmask) != (sgend & bmask)) {
802 /* Need a new segment. */
803 map->dm_segs[j].ds_len =
804 boundary - (sgstart & (boundary - 1));
805 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
806 "seg %d start %lx size %lx\n", j,
807 (long)map->dm_segs[j].ds_addr,
808 (long)map->dm_segs[j].ds_len));
809 if (++j >= map->_dm_segcnt) {
810 iommu_remove(is, map->_dm_dvmastart,
811 last_va - map->_dm_dvmastart);
812 goto fail;
813 }
814 sgstart += map->dm_segs[j-1].ds_len;
815 map->dm_segs[j].ds_addr = sgstart;
816 map->dm_segs[j].ds_len = sgend - sgstart + 1;
817 }
818
819 if (sgsize == 0)
820 panic("iommu_dmamap_load_raw: size botch");
821
822 /* Now map a series of pages. */
823 while (dvmaddr <= sgend) {
824 DPRINTF(IDB_BUSDMA,
825 ("iommu_dvmamap_load_raw: map %p "
826 "loading va %lx at pa %lx\n",
827 map, (long)dvmaddr,
828 (long)(pa)));
829 /* Enter it if we haven't before. */
830 if (prev_va != dvmaddr) {
831 iommu_enter(sb, prev_va = dvmaddr,
832 prev_pa = pa,
833 flags | IOTTE_DEBUG(++npg << 12));
834 needsflush = 1;
835 }
836 dvmaddr += pagesz;
837 pa += pagesz;
838 last_va = dvmaddr;
839 }
840
841 len -= left;
842 ++j;
843 }
844 if (needsflush)
845 iommu_strbuf_flush_done(sb);
846
847 map->dm_mapsize = size;
848 map->dm_nsegs = j;
849 #ifdef DIAGNOSTIC
850 { int seg;
851 for (seg = 0; seg < map->dm_nsegs; seg++) {
852 if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
853 map->dm_segs[seg].ds_addr > is->is_dvmaend) {
854 printf("seg %d dvmaddr %lx out of range %x - %x\n",
855 seg, (long)map->dm_segs[seg].ds_addr,
856 is->is_dvmabase, is->is_dvmaend);
857 #ifdef DDB
858 Debugger();
859 #endif
860 }
861 }
862 }
863 #endif
864 return (0);
865 }
866
867 /*
868 * This was allocated with bus_dmamem_alloc.
869 * The pages are on a `pglist'.
870 */
871 i = 0;
872 sgstart = dvmaddr;
873 sgend = sgstart + size - 1;
874 map->dm_segs[i].ds_addr = sgstart;
875 while ((sgstart & bmask) != (sgend & bmask)) {
876 /* Oops. We crossed a boundary. Split the xfer. */
877 map->dm_segs[i].ds_len = boundary - (sgstart & (boundary - 1));
878 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
879 "seg %d start %lx size %lx\n", i,
880 (long)map->dm_segs[i].ds_addr,
881 (long)map->dm_segs[i].ds_len));
882 if (++i >= map->_dm_segcnt) {
883 /* Too many segments. Fail the operation. */
884 goto fail;
885 }
886 sgstart += map->dm_segs[i-1].ds_len;
887 map->dm_segs[i].ds_addr = sgstart;
888 }
889 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
890 "seg %d start %lx size %lx\n", i,
891 (long)map->dm_segs[i].ds_addr, (long)map->dm_segs[i].ds_len));
892 map->dm_segs[i].ds_len = sgend - sgstart + 1;
893
894 needsflush = 0;
895 TAILQ_FOREACH(pg, pglist, pageq.queue) {
896 if (sgsize == 0)
897 panic("iommu_dmamap_load_raw: size botch");
898 pa = VM_PAGE_TO_PHYS(pg);
899
900 DPRINTF(IDB_BUSDMA,
901 ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
902 map, (long)dvmaddr, (long)(pa)));
903 iommu_enter(sb, dvmaddr, pa, flags | IOTTE_DEBUG(0x8000));
904 needsflush = 1;
905
906 dvmaddr += pagesz;
907 sgsize -= pagesz;
908 }
909 if (needsflush)
910 iommu_strbuf_flush_done(sb);
911 map->dm_mapsize = size;
912 map->dm_nsegs = i+1;
913 #ifdef DIAGNOSTIC
914 { int seg;
915 for (seg = 0; seg < map->dm_nsegs; seg++) {
916 if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
917 map->dm_segs[seg].ds_addr > is->is_dvmaend) {
918 printf("seg %d dvmaddr %lx out of range %x - %x\n",
919 seg, (long)map->dm_segs[seg].ds_addr,
920 is->is_dvmabase, is->is_dvmaend);
921 #ifdef DDB
922 Debugger();
923 #endif
924 }
925 }
926 }
927 #endif
928 return (0);
929
930 fail:
931 s = splhigh();
932 /* How can this fail? And if it does what can we do? */
933 err = extent_free(is->is_dvmamap, map->_dm_dvmastart, sgsize,
934 EX_NOWAIT);
935 map->_dm_dvmastart = 0;
936 map->_dm_dvmasize = 0;
937 splx(s);
938 return (EFBIG);
939 }
940
941
942 /*
943 * Flush an individual dma segment, returns non-zero if the streaming buffers
944 * need flushing afterwards.
945 */
946 static int
947 iommu_dvmamap_sync_range(struct strbuf_ctl *sb, vaddr_t va, bus_size_t len)
948 {
949 vaddr_t vaend;
950 struct iommu_state *is = sb->sb_is;
951
952 #ifdef DIAGNOSTIC
953 if (va < is->is_dvmabase || va > is->is_dvmaend)
954 panic("invalid va: %llx", (long long)va);
955 #endif
956
957 if ((is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)] & IOTTE_STREAM) == 0) {
958 DPRINTF(IDB_BUSDMA,
959 ("iommu_dvmamap_sync_range: attempting to flush "
960 "non-streaming entry\n"));
961 return (0);
962 }
963
964 vaend = round_page(va + len) - 1;
965 va = trunc_page(va);
966
967 #ifdef DIAGNOSTIC
968 if (va < is->is_dvmabase || vaend > is->is_dvmaend)
969 panic("invalid va range: %llx to %llx (%x to %x)",
970 (long long)va, (long long)vaend,
971 is->is_dvmabase,
972 is->is_dvmaend);
973 #endif
974
975 for ( ; va <= vaend; va += PAGE_SIZE) {
976 DPRINTF(IDB_BUSDMA,
977 ("iommu_dvmamap_sync_range: flushing va %p\n",
978 (void *)(u_long)va));
979 iommu_strbuf_flush(sb, va);
980 }
981
982 return (1);
983 }
984
985 static void
986 _iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
987 bus_size_t len, int ops)
988 {
989 struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
990 bus_size_t count;
991 int i, needsflush = 0;
992
993 if (!sb->sb_flush)
994 return;
995
996 for (i = 0; i < map->dm_nsegs; i++) {
997 if (offset < map->dm_segs[i].ds_len)
998 break;
999 offset -= map->dm_segs[i].ds_len;
1000 }
1001
1002 if (i == map->dm_nsegs)
1003 panic("iommu_dvmamap_sync: segment too short %llu",
1004 (unsigned long long)offset);
1005
1006 if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_POSTWRITE)) {
1007 /* Nothing to do */;
1008 }
1009
1010 if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_PREWRITE)) {
1011
1012 for (; len > 0 && i < map->dm_nsegs; i++) {
1013 count = MIN(map->dm_segs[i].ds_len - offset, len);
1014 if (count > 0 &&
1015 iommu_dvmamap_sync_range(sb,
1016 map->dm_segs[i].ds_addr + offset, count))
1017 needsflush = 1;
1018 offset = 0;
1019 len -= count;
1020 }
1021 #ifdef DIAGNOSTIC
1022 if (i == map->dm_nsegs && len > 0)
1023 panic("iommu_dvmamap_sync: leftover %llu",
1024 (unsigned long long)len);
1025 #endif
1026
1027 if (needsflush)
1028 iommu_strbuf_flush_done(sb);
1029 }
1030 }
1031
1032 void
1033 iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
1034 bus_size_t len, int ops)
1035 {
1036
1037 /* If len is 0, then there is nothing to do */
1038 if (len == 0)
1039 return;
1040
1041 if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE)) {
1042 /* Flush the CPU then the IOMMU */
1043 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1044 _iommu_dvmamap_sync(t, map, offset, len, ops);
1045 }
1046 if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE)) {
1047 /* Flush the IOMMU then the CPU */
1048 _iommu_dvmamap_sync(t, map, offset, len, ops);
1049 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1050 }
1051 }
1052
1053 int
1054 iommu_dvmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
1055 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
1056 int flags)
1057 {
1058
1059 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
1060 "segp %p flags %d\n", (unsigned long long)size,
1061 (unsigned long long)alignment, (unsigned long long)boundary,
1062 segs, flags));
1063 return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
1064 segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
1065 }
1066
1067 void
1068 iommu_dvmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
1069 {
1070
1071 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
1072 segs, nsegs));
1073 bus_dmamem_free(t->_parent, segs, nsegs);
1074 }
1075
1076 /*
1077 * Map the DVMA mappings into the kernel pmap.
1078 * Check the flags to see whether we're streaming or coherent.
1079 */
1080 int
1081 iommu_dvmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
1082 size_t size, void **kvap, int flags)
1083 {
1084 struct vm_page *pg;
1085 vaddr_t va;
1086 bus_addr_t addr;
1087 struct pglist *pglist;
1088 int cbit;
1089 const uvm_flag_t kmflags =
1090 (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0;
1091
1092 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
1093 segs, nsegs, size));
1094
1095 /*
1096 * Allocate some space in the kernel map, and then map these pages
1097 * into this space.
1098 */
1099 size = round_page(size);
1100 va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags);
1101 if (va == 0)
1102 return (ENOMEM);
1103
1104 *kvap = (void *)va;
1105
1106 /*
1107 * digest flags:
1108 */
1109 cbit = 0;
1110 if (flags & BUS_DMA_COHERENT) /* Disable vcache */
1111 cbit |= PMAP_NVC;
1112 if (flags & BUS_DMA_NOCACHE) /* sideffects */
1113 cbit |= PMAP_NC;
1114
1115 /*
1116 * Now take this and map it into the CPU.
1117 */
1118 pglist = segs[0]._ds_mlist;
1119 TAILQ_FOREACH(pg, pglist, pageq.queue) {
1120 #ifdef DIAGNOSTIC
1121 if (size == 0)
1122 panic("iommu_dvmamem_map: size botch");
1123 #endif
1124 addr = VM_PAGE_TO_PHYS(pg);
1125 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
1126 "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
1127 pmap_kenter_pa(va, addr | cbit,
1128 VM_PROT_READ | VM_PROT_WRITE, 0);
1129 va += PAGE_SIZE;
1130 size -= PAGE_SIZE;
1131 }
1132 pmap_update(pmap_kernel());
1133 return (0);
1134 }
1135
1136 /*
1137 * Unmap DVMA mappings from kernel
1138 */
1139 void
1140 iommu_dvmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
1141 {
1142
1143 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
1144 kva, size));
1145
1146 #ifdef DIAGNOSTIC
1147 if ((u_long)kva & PGOFSET)
1148 panic("iommu_dvmamem_unmap");
1149 #endif
1150
1151 size = round_page(size);
1152 pmap_kremove((vaddr_t)kva, size);
1153 pmap_update(pmap_kernel());
1154 uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
1155 }
1156