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