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