1 /* $NetBSD: iommu.c,v 1.116 2021/04/26 07:18:01 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1999, 2000 Matthew R. Green 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 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.116 2021/04/26 07:18:01 mrg 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.h> 74 75 #include <sys/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 #include <machine/hypervisor.h> 82 83 #ifdef DEBUG 84 #define IDB_BUSDMA 0x1 85 #define IDB_IOMMU 0x2 86 #define IDB_INFO 0x4 87 #define IDB_SYNC 0x8 88 int iommudebug = 0x0; 89 #define DPRINTF(l, s) do { if (iommudebug & l) printf s; } while (0) 90 #define IOTTE_DEBUG(n) (n) 91 #else 92 #define DPRINTF(l, s) 93 #define IOTTE_DEBUG(n) 0 94 #endif 95 96 #define iommu_strbuf_flush(i, v) do { \ 97 if ((i)->sb_flush) \ 98 bus_space_write_8((i)->sb_is->is_bustag, (i)->sb_sb, \ 99 STRBUFREG(strbuf_pgflush), (v)); \ 100 } while (0) 101 102 static int iommu_strbuf_flush_done(struct strbuf_ctl *); 103 static void _iommu_dvmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t, 104 bus_size_t, int); 105 static void iommu_enter_sun4u(struct strbuf_ctl *sb, vaddr_t va, int64_t pa, int flags); 106 static void iommu_enter_sun4v(struct strbuf_ctl *sb, vaddr_t va, int64_t pa, int flags); 107 static void iommu_remove_sun4u(struct iommu_state *is, vaddr_t va, size_t len); 108 static void iommu_remove_sun4v(struct iommu_state *is, vaddr_t va, size_t len); 109 110 /* 111 * initialise the UltraSPARC IOMMU (SBUS or PCI): 112 * - allocate and setup the iotsb. 113 * - enable the IOMMU 114 * - initialise the streaming buffers (if they exist) 115 * - create a private DVMA map. 116 */ 117 void 118 iommu_init(char *name, struct iommu_state *is, int tsbsize, uint32_t iovabase) 119 { 120 psize_t size; 121 vaddr_t va; 122 paddr_t pa; 123 struct vm_page *pg; 124 struct pglist pglist; 125 126 DPRINTF(IDB_INFO, ("iommu_init: tsbsize %x iovabase %x\n", tsbsize, iovabase)); 127 128 /* 129 * Setup the iommu. 130 * 131 * The sun4u iommu is part of the SBUS or PCI controller so we will 132 * deal with it here.. 133 * 134 * For sysio and psycho/psycho+ the IOMMU address space always ends at 135 * 0xffffe000, but the starting address depends on the size of the 136 * map. The map size is 1024 * 2 ^ is->is_tsbsize entries, where each 137 * entry is 8 bytes. The start of the map can be calculated by 138 * (0xffffe000 << (8 + is->is_tsbsize)). 139 * 140 * But sabre and hummingbird use a different scheme that seems to 141 * be hard-wired, so we read the start and size from the PROM and 142 * just use those values. 143 */ 144 if (strncmp(name, "pyro", 4) == 0) { 145 is->is_cr = IOMMUREG_READ(is, iommu_cr); 146 is->is_cr &= ~IOMMUCR_FIRE_BE; 147 is->is_cr |= (IOMMUCR_FIRE_SE | IOMMUCR_FIRE_CM_EN | 148 IOMMUCR_FIRE_TE); 149 } else 150 is->is_cr = IOMMUCR_EN; 151 is->is_tsbsize = tsbsize; 152 if (iovabase == -1) { 153 is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize); 154 is->is_dvmaend = IOTSB_VEND - 1; 155 } else { 156 is->is_dvmabase = iovabase; 157 is->is_dvmaend = iovabase + IOTSB_VSIZE(tsbsize) - 1; 158 } 159 160 /* 161 * Allocate memory for I/O pagetables. They need to be physically 162 * contiguous. 163 */ 164 165 size = PAGE_SIZE << is->is_tsbsize; 166 if (uvm_pglistalloc((psize_t)size, (paddr_t)0, (paddr_t)-1, 167 (paddr_t)PAGE_SIZE, (paddr_t)0, &pglist, 1, 0) != 0) 168 panic("iommu_init: no memory"); 169 170 va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY); 171 if (va == 0) 172 panic("iommu_init: no memory"); 173 is->is_tsb = (int64_t *)va; 174 175 is->is_ptsb = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist)); 176 177 /* Map the pages */ 178 TAILQ_FOREACH(pg, &pglist, pageq.queue) { 179 pa = VM_PAGE_TO_PHYS(pg); 180 pmap_kenter_pa(va, pa | PMAP_NVC, 181 VM_PROT_READ | VM_PROT_WRITE, 0); 182 va += PAGE_SIZE; 183 } 184 pmap_update(pmap_kernel()); 185 memset(is->is_tsb, 0, size); 186 187 #ifdef DEBUG 188 if (iommudebug & IDB_INFO) 189 { 190 /* Probe the iommu */ 191 if (!CPU_ISSUN4V) { 192 printf("iommu cr=%llx tsb=%llx\n", 193 (unsigned long long)bus_space_read_8(is->is_bustag, 194 is->is_iommu, 195 offsetof(struct iommureg, iommu_cr)), 196 (unsigned long long)bus_space_read_8(is->is_bustag, 197 is->is_iommu, 198 offsetof(struct iommureg, iommu_tsb))); 199 printf("TSB base %p phys %llx\n", (void *)is->is_tsb, 200 (unsigned long long)is->is_ptsb); 201 delay(1000000); /* 1 s */ 202 } 203 } 204 #endif 205 206 /* 207 * Now all the hardware's working we need to allocate a dvma map. 208 */ 209 aprint_debug("DVMA map: %x to %x\n", 210 (unsigned int)is->is_dvmabase, 211 (unsigned int)is->is_dvmaend); 212 aprint_debug("IOTSB: %llx to %llx\n", 213 (unsigned long long)is->is_ptsb, 214 (unsigned long long)(is->is_ptsb + size - 1)); 215 is->is_dvmamap = extent_create(name, 216 is->is_dvmabase, is->is_dvmaend, 217 0, 0, EX_NOWAIT); 218 if (!is->is_dvmamap) 219 panic("iommu_init: extent_create() failed"); 220 221 mutex_init(&is->is_lock, MUTEX_DEFAULT, IPL_HIGH); 222 223 /* 224 * Set the TSB size. The relevant bits were moved to the TSB 225 * base register in the PCIe host bridges. 226 */ 227 if (is->is_flags & IOMMU_TSBSIZE_IN_PTSB) 228 is->is_ptsb |= is->is_tsbsize; 229 else 230 is->is_cr |= (is->is_tsbsize << 16); 231 232 /* 233 * now actually start up the IOMMU 234 */ 235 iommu_reset(is); 236 } 237 238 /* 239 * Streaming buffers don't exist on the UltraSPARC IIi; we should have 240 * detected that already and disabled them. If not, we will notice that 241 * they aren't there when the STRBUF_EN bit does not remain. 242 */ 243 void 244 iommu_reset(struct iommu_state *is) 245 { 246 int i; 247 struct strbuf_ctl *sb; 248 249 if (CPU_ISSUN4V) 250 return; 251 252 IOMMUREG_WRITE(is, iommu_tsb, is->is_ptsb); 253 254 /* Enable IOMMU in diagnostic mode */ 255 IOMMUREG_WRITE(is, iommu_cr, is->is_cr|IOMMUCR_DE); 256 257 for (i = 0; i < 2; i++) { 258 if ((sb = is->is_sb[i])) { 259 260 /* Enable diagnostics mode? */ 261 bus_space_write_8(is->is_bustag, is->is_sb[i]->sb_sb, 262 STRBUFREG(strbuf_ctl), STRBUF_EN); 263 264 membar_Lookaside(); 265 266 /* No streaming buffers? Disable them */ 267 if (bus_space_read_8(is->is_bustag, 268 is->is_sb[i]->sb_sb, 269 STRBUFREG(strbuf_ctl)) == 0) { 270 is->is_sb[i]->sb_flush = NULL; 271 } else { 272 273 /* 274 * locate the pa of the flush buffer. 275 */ 276 if (pmap_extract(pmap_kernel(), 277 (vaddr_t)is->is_sb[i]->sb_flush, 278 &is->is_sb[i]->sb_flushpa) == FALSE) 279 is->is_sb[i]->sb_flush = NULL; 280 } 281 } 282 } 283 284 if (is->is_flags & IOMMU_FLUSH_CACHE) 285 IOMMUREG_WRITE(is, iommu_cache_invalidate, -1ULL); 286 } 287 288 /* 289 * Here are the iommu control routines. 290 */ 291 292 static void 293 iommu_enter(struct strbuf_ctl *sb, vaddr_t va, int64_t pa, int flags) 294 { 295 DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx flags %x\n", 296 va, (long)pa, flags)); 297 if (!CPU_ISSUN4V) 298 iommu_enter_sun4u(sb, va, pa, flags); 299 else 300 iommu_enter_sun4v(sb, va, pa, flags); 301 } 302 303 304 void 305 iommu_enter_sun4u(struct strbuf_ctl *sb, vaddr_t va, int64_t pa, int flags) 306 { 307 struct iommu_state *is = sb->sb_is; 308 int strbuf = (flags & BUS_DMA_STREAMING); 309 int64_t tte; 310 311 #ifdef DIAGNOSTIC 312 if (va < is->is_dvmabase || va > is->is_dvmaend) 313 panic("iommu_enter: va %#lx not in DVMA space", va); 314 #endif 315 316 /* Is the streamcache flush really needed? */ 317 if (sb->sb_flush) 318 iommu_strbuf_flush(sb, va); 319 else 320 /* If we can't flush the strbuf don't enable it. */ 321 strbuf = 0; 322 323 tte = MAKEIOTTE(pa, !(flags & BUS_DMA_NOWRITE), 324 !(flags & BUS_DMA_NOCACHE), (strbuf)); 325 #ifdef DEBUG 326 tte |= (flags & 0xff000LL)<<(4*8); 327 #endif 328 329 is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte; 330 bus_space_write_8(is->is_bustag, is->is_iommu, 331 IOMMUREG(iommu_flush), va); 332 DPRINTF(IDB_IOMMU, ("iommu_enter: slot %d va %lx pa %lx " 333 "TSB[%lx]@%p=%lx\n", (int)IOTSBSLOT(va,is->is_tsbsize), 334 va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize), 335 (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)], 336 (u_long)tte)); 337 } 338 339 void 340 iommu_enter_sun4v(struct strbuf_ctl *sb, vaddr_t va, int64_t pa, int flags) 341 { 342 struct iommu_state *is = sb->sb_is; 343 u_int64_t tsbid = IOTSBSLOT(va, is->is_tsbsize); 344 paddr_t page_list[1], addr; 345 u_int64_t attr, nmapped; 346 int err; 347 348 #ifdef DIAGNOSTIC 349 if (va < is->is_dvmabase || (va + PAGE_MASK) > is->is_dvmaend) 350 panic("viommu_enter: va %#lx not in DVMA space", va); 351 #endif 352 353 attr = PCI_MAP_ATTR_READ | PCI_MAP_ATTR_WRITE; 354 if (flags & BUS_DMA_READ) 355 attr &= ~PCI_MAP_ATTR_READ; 356 if (flags & BUS_DMA_WRITE) 357 attr &= ~PCI_MAP_ATTR_WRITE; 358 359 page_list[0] = trunc_page(pa); 360 if (!pmap_extract(pmap_kernel(), (vaddr_t)page_list, &addr)) 361 panic("viommu_enter: pmap_extract failed"); 362 err = hv_pci_iommu_map(is->is_devhandle, tsbid, 1, attr, 363 addr, &nmapped); 364 if (err != H_EOK || nmapped != 1) 365 panic("hv_pci_iommu_map: err=%d, nmapped=%lu", err, (long unsigned int)nmapped); 366 } 367 368 /* 369 * Find the value of a DVMA address (debug routine). 370 */ 371 paddr_t 372 iommu_extract(struct iommu_state *is, vaddr_t dva) 373 { 374 int64_t tte = 0; 375 376 if (dva >= is->is_dvmabase && dva <= is->is_dvmaend) 377 tte = is->is_tsb[IOTSBSLOT(dva, is->is_tsbsize)]; 378 379 if ((tte & IOTTE_V) == 0) 380 return ((paddr_t)-1L); 381 return (tte & IOTTE_PAMASK); 382 } 383 384 /* 385 * iommu_remove: removes mappings created by iommu_enter 386 * 387 * Only demap from IOMMU if flag is set. 388 * 389 * XXX: this function needs better internal error checking. 390 */ 391 392 static void 393 iommu_remove(struct iommu_state *is, vaddr_t va, size_t len) 394 { 395 DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx len %zu\n", va, len)); 396 if (!CPU_ISSUN4V) 397 iommu_remove_sun4u(is, va, len); 398 else 399 iommu_remove_sun4v(is, va, len); 400 } 401 402 void 403 iommu_remove_sun4u(struct iommu_state *is, vaddr_t va, size_t len) 404 { 405 406 int slot; 407 408 #ifdef DIAGNOSTIC 409 if (va < is->is_dvmabase || va > is->is_dvmaend) 410 panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va); 411 if ((long)(va + len) < (long)va) 412 panic("iommu_remove: va 0x%lx + len 0x%lx wraps", 413 (long) va, (long) len); 414 if (len & ~0xfffffff) 415 panic("iommu_remove: ridiculous len 0x%lx", (u_long)len); 416 #endif 417 418 va = trunc_page(va); 419 DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n", 420 va, (u_long)IOTSBSLOT(va, is->is_tsbsize), 421 &is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)])); 422 while (len > 0) { 423 DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d " 424 "for va %p size %lx\n", 425 (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va, 426 (u_long)len)); 427 if (len <= PAGE_SIZE) 428 len = 0; 429 else 430 len -= PAGE_SIZE; 431 432 #if 0 433 /* 434 * XXX Zero-ing the entry would not require RMW 435 * 436 * Disabling valid bit while a page is used by a device 437 * causes an uncorrectable DMA error. 438 * Workaround to avoid an uncorrectable DMA error is 439 * eliminating the next line, but the page is mapped 440 * until the next iommu_enter call. 441 */ 442 is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] &= ~IOTTE_V; 443 membar_StoreStore(); 444 #endif 445 IOMMUREG_WRITE(is, iommu_flush, va); 446 447 /* Flush cache if necessary. */ 448 slot = IOTSBSLOT(trunc_page(va), is->is_tsbsize); 449 if ((is->is_flags & IOMMU_FLUSH_CACHE) && 450 (len == 0 || (slot % 8) == 7)) 451 IOMMUREG_WRITE(is, iommu_cache_flush, 452 is->is_ptsb + slot * 8); 453 454 va += PAGE_SIZE; 455 } 456 } 457 458 void 459 iommu_remove_sun4v(struct iommu_state *is, vaddr_t va, size_t len) 460 { 461 u_int64_t tsbid = IOTSBSLOT(va, is->is_tsbsize); 462 u_int64_t ndemapped; 463 int err; 464 465 #ifdef DIAGNOSTIC 466 if (va < is->is_dvmabase || (va + PAGE_MASK) > is->is_dvmaend) 467 panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va); 468 if (va != trunc_page(va)) { 469 printf("iommu_remove: unaligned va: %lx\n", va); 470 va = trunc_page(va); 471 } 472 #endif 473 474 err = hv_pci_iommu_demap(is->is_devhandle, tsbid, 1, &ndemapped); 475 if (err != H_EOK || ndemapped != 1) 476 panic("hv_pci_iommu_unmap: err=%d", err); 477 } 478 479 static int 480 iommu_strbuf_flush_done(struct strbuf_ctl *sb) 481 { 482 struct iommu_state *is = sb->sb_is; 483 struct timeval cur, flushtimeout; 484 485 #define BUMPTIME(t, usec) { \ 486 register volatile struct timeval *tp = (t); \ 487 register long us; \ 488 \ 489 tp->tv_usec = us = tp->tv_usec + (usec); \ 490 if (us >= 1000000) { \ 491 tp->tv_usec = us - 1000000; \ 492 tp->tv_sec++; \ 493 } \ 494 } 495 496 if (!sb->sb_flush) 497 return (0); 498 499 /* 500 * Streaming buffer flushes: 501 * 502 * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If 503 * we're not on a cache line boundary (64-bits): 504 * 2 Store 0 in flag 505 * 3 Store pointer to flag in flushsync 506 * 4 wait till flushsync becomes 0x1 507 * 508 * If it takes more than .5 sec, something 509 * went wrong. 510 */ 511 512 *sb->sb_flush = 0; 513 bus_space_write_8(is->is_bustag, sb->sb_sb, 514 STRBUFREG(strbuf_flushsync), sb->sb_flushpa); 515 516 microtime(&flushtimeout); 517 cur = flushtimeout; 518 BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */ 519 520 DPRINTF(IDB_IOMMU, ("%s: flush = %lx at va = %lx pa = %lx now=" 521 "%"PRIx64":%"PRIx32" until = %"PRIx64":%"PRIx32"\n", __func__, 522 (long)*sb->sb_flush, (long)sb->sb_flush, (long)sb->sb_flushpa, 523 cur.tv_sec, cur.tv_usec, 524 flushtimeout.tv_sec, flushtimeout.tv_usec)); 525 526 /* Bypass non-coherent D$ */ 527 while ((!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) && 528 timercmp(&cur, &flushtimeout, <=)) 529 microtime(&cur); 530 531 #ifdef DIAGNOSTIC 532 if (!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) { 533 printf("%s: flush timeout %p, at %p\n", __func__, 534 (void *)(u_long)*sb->sb_flush, 535 (void *)(u_long)sb->sb_flushpa); /* panic? */ 536 #ifdef DDB 537 Debugger(); 538 #endif 539 } 540 #endif 541 DPRINTF(IDB_IOMMU, ("%s: flushed\n", __func__)); 542 return (*sb->sb_flush); 543 } 544 545 /* 546 * IOMMU DVMA operations, common to SBUS and PCI. 547 */ 548 int 549 iommu_dvmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 550 bus_size_t buflen, struct proc *p, int flags) 551 { 552 struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie; 553 struct iommu_state *is = sb->sb_is; 554 int err, needsflush; 555 bus_size_t sgsize; 556 paddr_t curaddr; 557 u_long dvmaddr, sgstart, sgend, bmask; 558 bus_size_t align, boundary, len; 559 vaddr_t vaddr = (vaddr_t)buf; 560 int seg; 561 struct pmap *pmap; 562 int slot; 563 564 if (map->dm_nsegs) { 565 /* Already in use?? */ 566 #ifdef DIAGNOSTIC 567 printf("iommu_dvmamap_load: map still in use\n"); 568 #endif 569 bus_dmamap_unload(t, map); 570 } 571 572 /* 573 * Make sure that on error condition we return "no valid mappings". 574 */ 575 map->dm_nsegs = 0; 576 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz); 577 578 if (buflen > map->_dm_size) { 579 DPRINTF(IDB_BUSDMA, 580 ("iommu_dvmamap_load(): error %d > %d -- " 581 "map size exceeded!\n", (int)buflen, (int)map->_dm_size)); 582 return (EINVAL); 583 } 584 585 sgsize = round_page(buflen + ((int)vaddr & PGOFSET)); 586 587 /* 588 * A boundary presented to bus_dmamem_alloc() takes precedence 589 * over boundary in the map. 590 */ 591 if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0) 592 boundary = map->_dm_boundary; 593 align = uimax(map->dm_segs[0]._ds_align, PAGE_SIZE); 594 595 /* 596 * If our segment size is larger than the boundary we need to 597 * split the transfer up int little pieces ourselves. 598 */ 599 KASSERT(is->is_dvmamap); 600 mutex_enter(&is->is_lock); 601 err = extent_alloc(is->is_dvmamap, sgsize, align, 602 (sgsize > boundary) ? 0 : boundary, 603 EX_NOWAIT|EX_BOUNDZERO, &dvmaddr); 604 mutex_exit(&is->is_lock); 605 606 #ifdef DEBUG 607 if (err || (dvmaddr == (u_long)-1)) { 608 printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n", 609 (int)sgsize, flags); 610 #ifdef DDB 611 Debugger(); 612 #endif 613 } 614 #endif 615 if (err != 0) 616 return (err); 617 618 if (dvmaddr == (u_long)-1) 619 return (ENOMEM); 620 621 /* Set the active DVMA map */ 622 map->_dm_dvmastart = dvmaddr; 623 map->_dm_dvmasize = sgsize; 624 625 /* 626 * Now split the DVMA range into segments, not crossing 627 * the boundary. 628 */ 629 seg = 0; 630 sgstart = dvmaddr + (vaddr & PGOFSET); 631 sgend = sgstart + buflen - 1; 632 map->dm_segs[seg].ds_addr = sgstart; 633 DPRINTF(IDB_INFO, ("iommu_dvmamap_load: boundary %lx boundary - 1 %lx " 634 "~(boundary - 1) %lx\n", (long)boundary, (long)(boundary - 1), 635 (long)~(boundary - 1))); 636 bmask = ~(boundary - 1); 637 while ((sgstart & bmask) != (sgend & bmask) || 638 sgend - sgstart + 1 > map->dm_maxsegsz) { 639 /* Oops. We crossed a boundary or large seg. Split the xfer. */ 640 len = map->dm_maxsegsz; 641 if ((sgstart & bmask) != (sgend & bmask)) 642 len = uimin(len, boundary - (sgstart & (boundary - 1))); 643 map->dm_segs[seg].ds_len = len; 644 DPRINTF(IDB_INFO, ("iommu_dvmamap_load: " 645 "seg %d start %lx size %lx\n", seg, 646 (long)map->dm_segs[seg].ds_addr, 647 (long)map->dm_segs[seg].ds_len)); 648 if (++seg >= map->_dm_segcnt) { 649 /* Too many segments. Fail the operation. */ 650 DPRINTF(IDB_INFO, ("iommu_dvmamap_load: " 651 "too many segments %d\n", seg)); 652 mutex_enter(&is->is_lock); 653 err = extent_free(is->is_dvmamap, 654 dvmaddr, sgsize, EX_NOWAIT); 655 map->_dm_dvmastart = 0; 656 map->_dm_dvmasize = 0; 657 mutex_exit(&is->is_lock); 658 if (err != 0) 659 printf("warning: %s: %" PRId64 660 " of DVMA space lost\n", __func__, sgsize); 661 return (EFBIG); 662 } 663 sgstart += len; 664 map->dm_segs[seg].ds_addr = sgstart; 665 } 666 map->dm_segs[seg].ds_len = sgend - sgstart + 1; 667 DPRINTF(IDB_INFO, ("iommu_dvmamap_load: " 668 "seg %d start %lx size %lx\n", seg, 669 (long)map->dm_segs[seg].ds_addr, (long)map->dm_segs[seg].ds_len)); 670 map->dm_nsegs = seg + 1; 671 map->dm_mapsize = buflen; 672 673 if (p != NULL) 674 pmap = p->p_vmspace->vm_map.pmap; 675 else 676 pmap = pmap_kernel(); 677 678 needsflush = 0; 679 for (; buflen > 0; ) { 680 681 /* 682 * Get the physical address for this page. 683 */ 684 if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) { 685 #ifdef DIAGNOSTIC 686 printf("iommu_dvmamap_load: pmap_extract failed %lx\n", vaddr); 687 #endif 688 bus_dmamap_unload(t, map); 689 return (-1); 690 } 691 692 /* 693 * Compute the segment size, and adjust counts. 694 */ 695 sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET); 696 if (buflen < sgsize) 697 sgsize = buflen; 698 699 DPRINTF(IDB_BUSDMA, 700 ("iommu_dvmamap_load: map %p loading va %p " 701 "dva %lx at pa %lx\n", 702 map, (void *)vaddr, (long)dvmaddr, 703 (long)trunc_page(curaddr))); 704 iommu_enter(sb, trunc_page(dvmaddr), trunc_page(curaddr), 705 flags | IOTTE_DEBUG(0x4000)); 706 needsflush = 1; 707 708 vaddr += sgsize; 709 buflen -= sgsize; 710 711 /* Flush cache if necessary. */ 712 slot = IOTSBSLOT(trunc_page(dvmaddr), is->is_tsbsize); 713 if ((is->is_flags & IOMMU_FLUSH_CACHE) && 714 (buflen <= 0 || (slot % 8) == 7)) 715 IOMMUREG_WRITE(is, iommu_cache_flush, 716 is->is_ptsb + slot * 8); 717 718 dvmaddr += PAGE_SIZE; 719 } 720 if (needsflush) 721 iommu_strbuf_flush_done(sb); 722 #ifdef DIAGNOSTIC 723 for (seg = 0; seg < map->dm_nsegs; seg++) { 724 if (map->dm_segs[seg].ds_addr < is->is_dvmabase || 725 map->dm_segs[seg].ds_addr > is->is_dvmaend) { 726 printf("seg %d dvmaddr %lx out of range %x - %x\n", 727 seg, (long)map->dm_segs[seg].ds_addr, 728 is->is_dvmabase, is->is_dvmaend); 729 #ifdef DDB 730 Debugger(); 731 #endif 732 } 733 } 734 #endif 735 return (0); 736 } 737 738 739 void 740 iommu_dvmamap_unload(bus_dma_tag_t t, bus_dmamap_t map) 741 { 742 struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie; 743 struct iommu_state *is = sb->sb_is; 744 int error; 745 bus_size_t sgsize = map->_dm_dvmasize; 746 747 /* Flush the iommu */ 748 if (!map->_dm_dvmastart) 749 panic("%s: error dvmastart is zero!\n", __func__); 750 751 if (is->is_flags & IOMMU_SYNC_BEFORE_UNMAP) { 752 753 /* Flush the caches */ 754 bus_dmamap_unload(t->_parent, map); 755 756 iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize); 757 758 } else { 759 760 iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize); 761 762 /* Flush the caches */ 763 bus_dmamap_unload(t->_parent, map); 764 } 765 766 mutex_enter(&is->is_lock); 767 error = extent_free(is->is_dvmamap, map->_dm_dvmastart, 768 map->_dm_dvmasize, EX_NOWAIT); 769 map->_dm_dvmastart = 0; 770 map->_dm_dvmasize = 0; 771 mutex_exit(&is->is_lock); 772 if (error != 0) 773 printf("warning: %s: %" PRId64 " of DVMA space lost\n", 774 __func__, sgsize); 775 776 /* Clear the map */ 777 } 778 779 780 int 781 iommu_dvmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, 782 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags) 783 { 784 struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie; 785 struct iommu_state *is = sb->sb_is; 786 struct vm_page *pg; 787 int i, j; 788 int left; 789 int err, needsflush; 790 bus_size_t sgsize; 791 paddr_t pa; 792 bus_size_t boundary, align; 793 u_long dvmaddr, sgstart, sgend, bmask; 794 struct pglist *pglist; 795 const int pagesz = PAGE_SIZE; 796 int slot; 797 #ifdef DEBUG 798 int npg = 0; 799 #endif 800 801 if (map->dm_nsegs) { 802 /* Already in use?? */ 803 #ifdef DIAGNOSTIC 804 printf("iommu_dvmamap_load_raw: map still in use\n"); 805 #endif 806 bus_dmamap_unload(t, map); 807 } 808 809 /* 810 * A boundary presented to bus_dmamem_alloc() takes precedence 811 * over boundary in the map. 812 */ 813 if ((boundary = segs[0]._ds_boundary) == 0) 814 boundary = map->_dm_boundary; 815 816 align = uimax(segs[0]._ds_align, pagesz); 817 818 /* 819 * Make sure that on error condition we return "no valid mappings". 820 */ 821 map->dm_nsegs = 0; 822 /* Count up the total number of pages we need */ 823 pa = trunc_page(segs[0].ds_addr); 824 sgsize = 0; 825 left = size; 826 for (i = 0; left > 0 && i < nsegs; i++) { 827 if (round_page(pa) != round_page(segs[i].ds_addr)) 828 sgsize = round_page(sgsize) + 829 (segs[i].ds_addr & PGOFSET); 830 sgsize += uimin(left, segs[i].ds_len); 831 left -= segs[i].ds_len; 832 pa = segs[i].ds_addr + segs[i].ds_len; 833 } 834 sgsize = round_page(sgsize); 835 836 mutex_enter(&is->is_lock); 837 /* 838 * If our segment size is larger than the boundary we need to 839 * split the transfer up into little pieces ourselves. 840 */ 841 err = extent_alloc(is->is_dvmamap, sgsize, align, 842 (sgsize > boundary) ? 0 : boundary, 843 ((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT) | 844 EX_BOUNDZERO, &dvmaddr); 845 mutex_exit(&is->is_lock); 846 847 if (err != 0) 848 return (err); 849 850 #ifdef DEBUG 851 if (dvmaddr == (u_long)-1) 852 { 853 printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n", 854 (int)sgsize, flags); 855 #ifdef DDB 856 Debugger(); 857 #endif 858 } 859 #endif 860 if (dvmaddr == (u_long)-1) 861 return (ENOMEM); 862 863 /* Set the active DVMA map */ 864 map->_dm_dvmastart = dvmaddr; 865 map->_dm_dvmasize = sgsize; 866 867 bmask = ~(boundary - 1); 868 if ((pglist = segs[0]._ds_mlist) == NULL) { 869 u_long prev_va = 0UL, last_va = dvmaddr; 870 paddr_t prev_pa = 0; 871 int end = 0, offset; 872 bus_size_t len = size; 873 874 /* 875 * This segs is made up of individual physical 876 * segments, probably by _bus_dmamap_load_uio() or 877 * _bus_dmamap_load_mbuf(). Ignore the mlist and 878 * load each one individually. 879 */ 880 j = 0; 881 needsflush = 0; 882 for (i = 0; i < nsegs ; i++) { 883 884 pa = segs[i].ds_addr; 885 offset = (pa & PGOFSET); 886 pa = trunc_page(pa); 887 dvmaddr = trunc_page(dvmaddr); 888 left = uimin(len, segs[i].ds_len); 889 890 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: converting " 891 "physseg %d start %lx size %lx\n", i, 892 (long)segs[i].ds_addr, (long)segs[i].ds_len)); 893 894 if ((pa == prev_pa) && 895 ((offset != 0) || (end != offset))) { 896 /* We can re-use this mapping */ 897 dvmaddr = prev_va; 898 } 899 900 sgstart = dvmaddr + offset; 901 sgend = sgstart + left - 1; 902 903 /* Are the segments virtually adjacent? */ 904 if ((j > 0) && (end == offset) && 905 ((offset == 0) || (pa == prev_pa)) && 906 (map->dm_segs[j-1].ds_len + left <= 907 map->dm_maxsegsz)) { 908 /* Just append to the previous segment. */ 909 map->dm_segs[--j].ds_len += left; 910 /* Restore sgstart for boundary check */ 911 sgstart = map->dm_segs[j].ds_addr; 912 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: " 913 "appending seg %d start %lx size %lx\n", j, 914 (long)map->dm_segs[j].ds_addr, 915 (long)map->dm_segs[j].ds_len)); 916 } else { 917 if (j >= map->_dm_segcnt) { 918 iommu_remove(is, map->_dm_dvmastart, 919 last_va - map->_dm_dvmastart); 920 goto fail; 921 } 922 map->dm_segs[j].ds_addr = sgstart; 923 map->dm_segs[j].ds_len = left; 924 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: " 925 "seg %d start %lx size %lx\n", j, 926 (long)map->dm_segs[j].ds_addr, 927 (long)map->dm_segs[j].ds_len)); 928 } 929 end = (offset + left) & PGOFSET; 930 931 /* Check for boundary issues */ 932 while ((sgstart & bmask) != (sgend & bmask)) { 933 /* Need a new segment. */ 934 map->dm_segs[j].ds_len = 935 boundary - (sgstart & (boundary - 1)); 936 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: " 937 "seg %d start %lx size %lx\n", j, 938 (long)map->dm_segs[j].ds_addr, 939 (long)map->dm_segs[j].ds_len)); 940 if (++j >= map->_dm_segcnt) { 941 iommu_remove(is, map->_dm_dvmastart, 942 last_va - map->_dm_dvmastart); 943 goto fail; 944 } 945 sgstart += map->dm_segs[j-1].ds_len; 946 map->dm_segs[j].ds_addr = sgstart; 947 map->dm_segs[j].ds_len = sgend - sgstart + 1; 948 } 949 950 if (sgsize == 0) 951 panic("iommu_dmamap_load_raw: size botch"); 952 953 /* Now map a series of pages. */ 954 while (dvmaddr <= sgend) { 955 DPRINTF(IDB_BUSDMA, 956 ("iommu_dvmamap_load_raw: map %p " 957 "loading va %lx at pa %lx\n", 958 map, (long)dvmaddr, 959 (long)(pa))); 960 /* Enter it if we haven't before. */ 961 if (prev_va != dvmaddr) { 962 iommu_enter(sb, prev_va = dvmaddr, 963 prev_pa = pa, 964 flags | IOTTE_DEBUG(++npg << 12)); 965 needsflush = 1; 966 967 /* Flush cache if necessary. */ 968 slot = IOTSBSLOT(trunc_page(dvmaddr), is->is_tsbsize); 969 if ((is->is_flags & IOMMU_FLUSH_CACHE) && 970 ((dvmaddr + pagesz) > sgend || (slot % 8) == 7)) 971 IOMMUREG_WRITE(is, iommu_cache_flush, 972 is->is_ptsb + slot * 8); 973 } 974 975 dvmaddr += pagesz; 976 pa += pagesz; 977 last_va = dvmaddr; 978 } 979 980 len -= left; 981 ++j; 982 } 983 if (needsflush) 984 iommu_strbuf_flush_done(sb); 985 986 map->dm_mapsize = size; 987 map->dm_nsegs = j; 988 #ifdef DIAGNOSTIC 989 { int seg; 990 for (seg = 0; seg < map->dm_nsegs; seg++) { 991 if (map->dm_segs[seg].ds_addr < is->is_dvmabase || 992 map->dm_segs[seg].ds_addr > is->is_dvmaend) { 993 printf("seg %d dvmaddr %lx out of range %x - %x\n", 994 seg, (long)map->dm_segs[seg].ds_addr, 995 is->is_dvmabase, is->is_dvmaend); 996 #ifdef DDB 997 Debugger(); 998 #endif 999 } 1000 } 1001 } 1002 #endif 1003 return (0); 1004 } 1005 1006 /* 1007 * This was allocated with bus_dmamem_alloc. 1008 * The pages are on a `pglist'. 1009 */ 1010 i = 0; 1011 sgstart = dvmaddr; 1012 sgend = sgstart + size - 1; 1013 map->dm_segs[i].ds_addr = sgstart; 1014 while ((sgstart & bmask) != (sgend & bmask)) { 1015 /* Oops. We crossed a boundary. Split the xfer. */ 1016 map->dm_segs[i].ds_len = boundary - (sgstart & (boundary - 1)); 1017 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: " 1018 "seg %d start %lx size %lx\n", i, 1019 (long)map->dm_segs[i].ds_addr, 1020 (long)map->dm_segs[i].ds_len)); 1021 if (++i >= map->_dm_segcnt) { 1022 /* Too many segments. Fail the operation. */ 1023 goto fail; 1024 } 1025 sgstart += map->dm_segs[i-1].ds_len; 1026 map->dm_segs[i].ds_addr = sgstart; 1027 } 1028 DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: " 1029 "seg %d start %lx size %lx\n", i, 1030 (long)map->dm_segs[i].ds_addr, (long)map->dm_segs[i].ds_len)); 1031 map->dm_segs[i].ds_len = sgend - sgstart + 1; 1032 1033 needsflush = 0; 1034 TAILQ_FOREACH(pg, pglist, pageq.queue) { 1035 if (sgsize == 0) 1036 panic("iommu_dmamap_load_raw: size botch"); 1037 pa = VM_PAGE_TO_PHYS(pg); 1038 1039 DPRINTF(IDB_BUSDMA, 1040 ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n", 1041 map, (long)dvmaddr, (long)(pa))); 1042 iommu_enter(sb, dvmaddr, pa, flags | IOTTE_DEBUG(0x8000)); 1043 needsflush = 1; 1044 1045 sgsize -= pagesz; 1046 1047 /* Flush cache if necessary. */ 1048 slot = IOTSBSLOT(trunc_page(dvmaddr), is->is_tsbsize); 1049 if ((is->is_flags & IOMMU_FLUSH_CACHE) && 1050 (sgsize == 0 || (slot % 8) == 7)) 1051 IOMMUREG_WRITE(is, iommu_cache_flush, 1052 is->is_ptsb + slot * 8); 1053 1054 dvmaddr += pagesz; 1055 } 1056 if (needsflush) 1057 iommu_strbuf_flush_done(sb); 1058 map->dm_mapsize = size; 1059 map->dm_nsegs = i+1; 1060 #ifdef DIAGNOSTIC 1061 { int seg; 1062 for (seg = 0; seg < map->dm_nsegs; seg++) { 1063 if (map->dm_segs[seg].ds_addr < is->is_dvmabase || 1064 map->dm_segs[seg].ds_addr > is->is_dvmaend) { 1065 printf("seg %d dvmaddr %lx out of range %x - %x\n", 1066 seg, (long)map->dm_segs[seg].ds_addr, 1067 is->is_dvmabase, is->is_dvmaend); 1068 #ifdef DDB 1069 Debugger(); 1070 #endif 1071 } 1072 } 1073 } 1074 #endif 1075 return (0); 1076 1077 fail: 1078 mutex_enter(&is->is_lock); 1079 err = extent_free(is->is_dvmamap, map->_dm_dvmastart, sgsize, 1080 EX_NOWAIT); 1081 map->_dm_dvmastart = 0; 1082 map->_dm_dvmasize = 0; 1083 mutex_exit(&is->is_lock); 1084 if (err != 0) 1085 printf("warning: %s: %" PRId64 " of DVMA space lost\n", 1086 __func__, sgsize); 1087 return (EFBIG); 1088 } 1089 1090 1091 /* 1092 * Flush an individual dma segment, returns non-zero if the streaming buffers 1093 * need flushing afterwards. 1094 */ 1095 static int 1096 iommu_dvmamap_sync_range(struct strbuf_ctl *sb, vaddr_t va, bus_size_t len) 1097 { 1098 vaddr_t vaend; 1099 struct iommu_state *is = sb->sb_is; 1100 1101 #ifdef DIAGNOSTIC 1102 if (va < is->is_dvmabase || va > is->is_dvmaend) 1103 panic("invalid va: %llx", (long long)va); 1104 #endif 1105 1106 if ((is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)] & IOTTE_STREAM) == 0) { 1107 DPRINTF(IDB_SYNC, 1108 ("iommu_dvmamap_sync_range: attempting to flush " 1109 "non-streaming entry\n")); 1110 return (0); 1111 } 1112 1113 vaend = round_page(va + len) - 1; 1114 va = trunc_page(va); 1115 1116 #ifdef DIAGNOSTIC 1117 if (va < is->is_dvmabase || vaend > is->is_dvmaend) 1118 panic("invalid va range: %llx to %llx (%x to %x)", 1119 (long long)va, (long long)vaend, 1120 is->is_dvmabase, 1121 is->is_dvmaend); 1122 #endif 1123 1124 for ( ; va <= vaend; va += PAGE_SIZE) { 1125 DPRINTF(IDB_SYNC, 1126 ("iommu_dvmamap_sync_range: flushing va %p\n", 1127 (void *)(u_long)va)); 1128 iommu_strbuf_flush(sb, va); 1129 } 1130 1131 return (1); 1132 } 1133 1134 static void 1135 _iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, 1136 bus_size_t len, int ops) 1137 { 1138 struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie; 1139 bus_size_t count; 1140 int i, needsflush = 0; 1141 1142 if (!sb->sb_flush) 1143 return; 1144 1145 for (i = 0; i < map->dm_nsegs; i++) { 1146 if (offset < map->dm_segs[i].ds_len) 1147 break; 1148 offset -= map->dm_segs[i].ds_len; 1149 } 1150 1151 if (i == map->dm_nsegs) 1152 panic("%s: segment too short %llu", __func__, 1153 (unsigned long long)offset); 1154 1155 if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_POSTWRITE)) { 1156 /* Nothing to do */; 1157 } 1158 1159 if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_PREWRITE)) { 1160 1161 for (; len > 0 && i < map->dm_nsegs; i++) { 1162 count = MIN(map->dm_segs[i].ds_len - offset, len); 1163 if (count > 0 && 1164 iommu_dvmamap_sync_range(sb, 1165 map->dm_segs[i].ds_addr + offset, count)) 1166 needsflush = 1; 1167 offset = 0; 1168 len -= count; 1169 } 1170 #ifdef DIAGNOSTIC 1171 if (i == map->dm_nsegs && len > 0) 1172 panic("%s: leftover %llu", __func__, 1173 (unsigned long long)len); 1174 #endif 1175 1176 if (needsflush) 1177 iommu_strbuf_flush_done(sb); 1178 } 1179 } 1180 1181 void 1182 iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, 1183 bus_size_t len, int ops) 1184 { 1185 1186 /* If len is 0, then there is nothing to do */ 1187 if (len == 0) 1188 return; 1189 1190 if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE)) { 1191 /* Flush the CPU then the IOMMU */ 1192 bus_dmamap_sync(t->_parent, map, offset, len, ops); 1193 _iommu_dvmamap_sync(t, map, offset, len, ops); 1194 } 1195 if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE)) { 1196 /* Flush the IOMMU then the CPU */ 1197 _iommu_dvmamap_sync(t, map, offset, len, ops); 1198 bus_dmamap_sync(t->_parent, map, offset, len, ops); 1199 } 1200 } 1201 1202 int 1203 iommu_dvmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment, 1204 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs, 1205 int flags) 1206 { 1207 1208 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx " 1209 "segp %p flags %d\n", (unsigned long long)size, 1210 (unsigned long long)alignment, (unsigned long long)boundary, 1211 segs, flags)); 1212 return (bus_dmamem_alloc(t->_parent, size, alignment, boundary, 1213 segs, nsegs, rsegs, flags|BUS_DMA_DVMA)); 1214 } 1215 1216 void 1217 iommu_dvmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs) 1218 { 1219 1220 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n", 1221 segs, nsegs)); 1222 bus_dmamem_free(t->_parent, segs, nsegs); 1223 } 1224 1225 /* 1226 * Map the DVMA mappings into the kernel pmap. 1227 * Check the flags to see whether we're streaming or coherent. 1228 */ 1229 int 1230 iommu_dvmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, 1231 size_t size, void **kvap, int flags) 1232 { 1233 struct vm_page *pg; 1234 vaddr_t va; 1235 bus_addr_t addr; 1236 struct pglist *pglist; 1237 int cbit; 1238 const uvm_flag_t kmflags = 1239 (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0; 1240 1241 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n", 1242 segs, nsegs, size)); 1243 1244 /* 1245 * Allocate some space in the kernel map, and then map these pages 1246 * into this space. 1247 */ 1248 size = round_page(size); 1249 va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags); 1250 if (va == 0) 1251 return (ENOMEM); 1252 1253 *kvap = (void *)va; 1254 1255 /* 1256 * digest flags: 1257 */ 1258 cbit = 0; 1259 if (flags & BUS_DMA_COHERENT) /* Disable vcache */ 1260 cbit |= PMAP_NVC; 1261 if (flags & BUS_DMA_NOCACHE) /* side effects */ 1262 cbit |= PMAP_NC; 1263 1264 /* 1265 * Now take this and map it into the CPU. 1266 */ 1267 pglist = segs[0]._ds_mlist; 1268 TAILQ_FOREACH(pg, pglist, pageq.queue) { 1269 #ifdef DIAGNOSTIC 1270 if (size == 0) 1271 panic("iommu_dvmamem_map: size botch"); 1272 #endif 1273 addr = VM_PAGE_TO_PHYS(pg); 1274 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: " 1275 "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit)); 1276 pmap_kenter_pa(va, addr | cbit, 1277 VM_PROT_READ | VM_PROT_WRITE, 0); 1278 va += PAGE_SIZE; 1279 size -= PAGE_SIZE; 1280 } 1281 pmap_update(pmap_kernel()); 1282 return (0); 1283 } 1284 1285 /* 1286 * Unmap DVMA mappings from kernel 1287 */ 1288 void 1289 iommu_dvmamem_unmap(bus_dma_tag_t t, void *kva, size_t size) 1290 { 1291 1292 DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n", 1293 kva, size)); 1294 1295 #ifdef DIAGNOSTIC 1296 if ((u_long)kva & PGOFSET) 1297 panic("iommu_dvmamem_unmap"); 1298 #endif 1299 1300 size = round_page(size); 1301 pmap_kremove((vaddr_t)kva, size); 1302 pmap_update(pmap_kernel()); 1303 uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY); 1304 } 1305