1 // SPDX-License-Identifier: MIT 2 3 /* 4 * Locking: 5 * 6 * The uvmm mutex protects any operations on the GPU VA space provided by the 7 * DRM GPU VA manager. 8 * 9 * The GEMs dma_resv lock protects the GEMs GPUVA list, hence link/unlink of a 10 * mapping to it's backing GEM must be performed under this lock. 11 * 12 * Actual map/unmap operations within the fence signalling critical path are 13 * protected by installing DMA fences to the corresponding GEMs DMA 14 * reservations, such that concurrent BO moves, which itself walk the GEMs GPUVA 15 * list in order to map/unmap it's entries, can't occur concurrently. 16 * 17 * Accessing the DRM_GPUVA_INVALIDATED flag doesn't need any separate 18 * protection, since there are no accesses other than from BO move callbacks 19 * and from the fence signalling critical path, which are already protected by 20 * the corresponding GEMs DMA reservation fence. 21 */ 22 23 #include "nouveau_drv.h" 24 #include "nouveau_gem.h" 25 #include "nouveau_mem.h" 26 #include "nouveau_uvmm.h" 27 28 #include <nvif/vmm.h> 29 #include <nvif/mem.h> 30 31 #include <nvif/class.h> 32 #include <nvif/if000c.h> 33 #include <nvif/if900d.h> 34 35 #define NOUVEAU_VA_SPACE_BITS 47 /* FIXME */ 36 #define NOUVEAU_VA_SPACE_START 0x0 37 #define NOUVEAU_VA_SPACE_END (1ULL << NOUVEAU_VA_SPACE_BITS) 38 39 #define list_last_op(_ops) list_last_entry(_ops, struct bind_job_op, entry) 40 #define list_prev_op(_op) list_prev_entry(_op, entry) 41 #define list_for_each_op(_op, _ops) list_for_each_entry(_op, _ops, entry) 42 #define list_for_each_op_from_reverse(_op, _ops) \ 43 list_for_each_entry_from_reverse(_op, _ops, entry) 44 #define list_for_each_op_safe(_op, _n, _ops) list_for_each_entry_safe(_op, _n, _ops, entry) 45 46 enum vm_bind_op { 47 OP_MAP = DRM_NOUVEAU_VM_BIND_OP_MAP, 48 OP_UNMAP = DRM_NOUVEAU_VM_BIND_OP_UNMAP, 49 OP_MAP_SPARSE, 50 OP_UNMAP_SPARSE, 51 }; 52 53 struct nouveau_uvma_prealloc { 54 struct nouveau_uvma *map; 55 struct nouveau_uvma *prev; 56 struct nouveau_uvma *next; 57 }; 58 59 struct bind_job_op { 60 struct list_head entry; 61 62 enum vm_bind_op op; 63 u32 flags; 64 65 struct drm_gpuvm_bo *vm_bo; 66 67 struct { 68 u64 addr; 69 u64 range; 70 } va; 71 72 struct { 73 u32 handle; 74 u64 offset; 75 struct drm_gem_object *obj; 76 } gem; 77 78 struct nouveau_uvma_region *reg; 79 struct nouveau_uvma_prealloc new; 80 struct drm_gpuva_ops *ops; 81 }; 82 83 struct uvmm_map_args { 84 struct nouveau_uvma_region *region; 85 u64 addr; 86 u64 range; 87 u8 kind; 88 }; 89 90 static int 91 nouveau_uvmm_vmm_sparse_ref(struct nouveau_uvmm *uvmm, 92 u64 addr, u64 range) 93 { 94 struct nvif_vmm *vmm = &uvmm->vmm.vmm; 95 96 return nvif_vmm_raw_sparse(vmm, addr, range, true); 97 } 98 99 static int 100 nouveau_uvmm_vmm_sparse_unref(struct nouveau_uvmm *uvmm, 101 u64 addr, u64 range) 102 { 103 struct nvif_vmm *vmm = &uvmm->vmm.vmm; 104 105 return nvif_vmm_raw_sparse(vmm, addr, range, false); 106 } 107 108 static int 109 nouveau_uvmm_vmm_get(struct nouveau_uvmm *uvmm, 110 u64 addr, u64 range) 111 { 112 struct nvif_vmm *vmm = &uvmm->vmm.vmm; 113 114 return nvif_vmm_raw_get(vmm, addr, range, PAGE_SHIFT); 115 } 116 117 static int 118 nouveau_uvmm_vmm_put(struct nouveau_uvmm *uvmm, 119 u64 addr, u64 range) 120 { 121 struct nvif_vmm *vmm = &uvmm->vmm.vmm; 122 123 return nvif_vmm_raw_put(vmm, addr, range, PAGE_SHIFT); 124 } 125 126 static int 127 nouveau_uvmm_vmm_unmap(struct nouveau_uvmm *uvmm, 128 u64 addr, u64 range, bool sparse) 129 { 130 struct nvif_vmm *vmm = &uvmm->vmm.vmm; 131 132 return nvif_vmm_raw_unmap(vmm, addr, range, PAGE_SHIFT, sparse); 133 } 134 135 static int 136 nouveau_uvmm_vmm_map(struct nouveau_uvmm *uvmm, 137 u64 addr, u64 range, 138 u64 bo_offset, u8 kind, 139 struct nouveau_mem *mem) 140 { 141 struct nvif_vmm *vmm = &uvmm->vmm.vmm; 142 union { 143 struct gf100_vmm_map_v0 gf100; 144 } args; 145 u32 argc = 0; 146 147 switch (vmm->object.oclass) { 148 case NVIF_CLASS_VMM_GF100: 149 case NVIF_CLASS_VMM_GM200: 150 case NVIF_CLASS_VMM_GP100: 151 args.gf100.version = 0; 152 if (mem->mem.type & NVIF_MEM_VRAM) 153 args.gf100.vol = 0; 154 else 155 args.gf100.vol = 1; 156 args.gf100.ro = 0; 157 args.gf100.priv = 0; 158 args.gf100.kind = kind; 159 argc = sizeof(args.gf100); 160 break; 161 default: 162 WARN_ON(1); 163 return -ENOSYS; 164 } 165 166 return nvif_vmm_raw_map(vmm, addr, range, PAGE_SHIFT, 167 &args, argc, 168 &mem->mem, bo_offset); 169 } 170 171 static int 172 nouveau_uvma_region_sparse_unref(struct nouveau_uvma_region *reg) 173 { 174 u64 addr = reg->va.addr; 175 u64 range = reg->va.range; 176 177 return nouveau_uvmm_vmm_sparse_unref(reg->uvmm, addr, range); 178 } 179 180 static int 181 nouveau_uvma_vmm_put(struct nouveau_uvma *uvma) 182 { 183 u64 addr = uvma->va.va.addr; 184 u64 range = uvma->va.va.range; 185 186 return nouveau_uvmm_vmm_put(to_uvmm(uvma), addr, range); 187 } 188 189 static int 190 nouveau_uvma_map(struct nouveau_uvma *uvma, 191 struct nouveau_mem *mem) 192 { 193 u64 addr = uvma->va.va.addr; 194 u64 offset = uvma->va.gem.offset; 195 u64 range = uvma->va.va.range; 196 197 return nouveau_uvmm_vmm_map(to_uvmm(uvma), addr, range, 198 offset, uvma->kind, mem); 199 } 200 201 static int 202 nouveau_uvma_unmap(struct nouveau_uvma *uvma) 203 { 204 u64 addr = uvma->va.va.addr; 205 u64 range = uvma->va.va.range; 206 bool sparse = !!uvma->region; 207 208 if (drm_gpuva_invalidated(&uvma->va)) 209 return 0; 210 211 return nouveau_uvmm_vmm_unmap(to_uvmm(uvma), addr, range, sparse); 212 } 213 214 static int 215 nouveau_uvma_alloc(struct nouveau_uvma **puvma) 216 { 217 *puvma = kzalloc(sizeof(**puvma), GFP_KERNEL); 218 if (!*puvma) 219 return -ENOMEM; 220 221 return 0; 222 } 223 224 static void 225 nouveau_uvma_free(struct nouveau_uvma *uvma) 226 { 227 kfree(uvma); 228 } 229 230 static void 231 nouveau_uvma_gem_get(struct nouveau_uvma *uvma) 232 { 233 drm_gem_object_get(uvma->va.gem.obj); 234 } 235 236 static void 237 nouveau_uvma_gem_put(struct nouveau_uvma *uvma) 238 { 239 drm_gem_object_put(uvma->va.gem.obj); 240 } 241 242 static int 243 nouveau_uvma_region_alloc(struct nouveau_uvma_region **preg) 244 { 245 *preg = kzalloc(sizeof(**preg), GFP_KERNEL); 246 if (!*preg) 247 return -ENOMEM; 248 249 kref_init(&(*preg)->kref); 250 251 return 0; 252 } 253 254 static void 255 nouveau_uvma_region_free(struct kref *kref) 256 { 257 struct nouveau_uvma_region *reg = 258 container_of(kref, struct nouveau_uvma_region, kref); 259 260 kfree(reg); 261 } 262 263 static void 264 nouveau_uvma_region_get(struct nouveau_uvma_region *reg) 265 { 266 kref_get(®->kref); 267 } 268 269 static void 270 nouveau_uvma_region_put(struct nouveau_uvma_region *reg) 271 { 272 kref_put(®->kref, nouveau_uvma_region_free); 273 } 274 275 static int 276 __nouveau_uvma_region_insert(struct nouveau_uvmm *uvmm, 277 struct nouveau_uvma_region *reg) 278 { 279 u64 addr = reg->va.addr; 280 u64 range = reg->va.range; 281 u64 last = addr + range - 1; 282 MA_STATE(mas, &uvmm->region_mt, addr, addr); 283 284 if (unlikely(mas_walk(&mas))) 285 return -EEXIST; 286 287 if (unlikely(mas.last < last)) 288 return -EEXIST; 289 290 mas.index = addr; 291 mas.last = last; 292 293 mas_store_gfp(&mas, reg, GFP_KERNEL); 294 295 reg->uvmm = uvmm; 296 297 return 0; 298 } 299 300 static int 301 nouveau_uvma_region_insert(struct nouveau_uvmm *uvmm, 302 struct nouveau_uvma_region *reg, 303 u64 addr, u64 range) 304 { 305 int ret; 306 307 reg->uvmm = uvmm; 308 reg->va.addr = addr; 309 reg->va.range = range; 310 311 ret = __nouveau_uvma_region_insert(uvmm, reg); 312 if (ret) 313 return ret; 314 315 return 0; 316 } 317 318 static void 319 nouveau_uvma_region_remove(struct nouveau_uvma_region *reg) 320 { 321 struct nouveau_uvmm *uvmm = reg->uvmm; 322 MA_STATE(mas, &uvmm->region_mt, reg->va.addr, 0); 323 324 mas_erase(&mas); 325 } 326 327 static int 328 nouveau_uvma_region_create(struct nouveau_uvmm *uvmm, 329 u64 addr, u64 range) 330 { 331 struct nouveau_uvma_region *reg; 332 int ret; 333 334 if (!drm_gpuvm_interval_empty(&uvmm->base, addr, range)) 335 return -ENOSPC; 336 337 ret = nouveau_uvma_region_alloc(®); 338 if (ret) 339 return ret; 340 341 ret = nouveau_uvma_region_insert(uvmm, reg, addr, range); 342 if (ret) 343 goto err_free_region; 344 345 ret = nouveau_uvmm_vmm_sparse_ref(uvmm, addr, range); 346 if (ret) 347 goto err_region_remove; 348 349 return 0; 350 351 err_region_remove: 352 nouveau_uvma_region_remove(reg); 353 err_free_region: 354 nouveau_uvma_region_put(reg); 355 return ret; 356 } 357 358 static struct nouveau_uvma_region * 359 nouveau_uvma_region_find_first(struct nouveau_uvmm *uvmm, 360 u64 addr, u64 range) 361 { 362 MA_STATE(mas, &uvmm->region_mt, addr, 0); 363 364 return mas_find(&mas, addr + range - 1); 365 } 366 367 static struct nouveau_uvma_region * 368 nouveau_uvma_region_find(struct nouveau_uvmm *uvmm, 369 u64 addr, u64 range) 370 { 371 struct nouveau_uvma_region *reg; 372 373 reg = nouveau_uvma_region_find_first(uvmm, addr, range); 374 if (!reg) 375 return NULL; 376 377 if (reg->va.addr != addr || 378 reg->va.range != range) 379 return NULL; 380 381 return reg; 382 } 383 384 static bool 385 nouveau_uvma_region_empty(struct nouveau_uvma_region *reg) 386 { 387 struct nouveau_uvmm *uvmm = reg->uvmm; 388 389 return drm_gpuvm_interval_empty(&uvmm->base, 390 reg->va.addr, 391 reg->va.range); 392 } 393 394 static int 395 __nouveau_uvma_region_destroy(struct nouveau_uvma_region *reg) 396 { 397 struct nouveau_uvmm *uvmm = reg->uvmm; 398 u64 addr = reg->va.addr; 399 u64 range = reg->va.range; 400 401 if (!nouveau_uvma_region_empty(reg)) 402 return -EBUSY; 403 404 nouveau_uvma_region_remove(reg); 405 nouveau_uvmm_vmm_sparse_unref(uvmm, addr, range); 406 nouveau_uvma_region_put(reg); 407 408 return 0; 409 } 410 411 static int 412 nouveau_uvma_region_destroy(struct nouveau_uvmm *uvmm, 413 u64 addr, u64 range) 414 { 415 struct nouveau_uvma_region *reg; 416 417 reg = nouveau_uvma_region_find(uvmm, addr, range); 418 if (!reg) 419 return -ENOENT; 420 421 return __nouveau_uvma_region_destroy(reg); 422 } 423 424 static void 425 nouveau_uvma_region_dirty(struct nouveau_uvma_region *reg) 426 { 427 428 init_completion(®->complete); 429 reg->dirty = true; 430 } 431 432 static void 433 nouveau_uvma_region_complete(struct nouveau_uvma_region *reg) 434 { 435 complete_all(®->complete); 436 } 437 438 static void 439 op_map_prepare_unwind(struct nouveau_uvma *uvma) 440 { 441 struct drm_gpuva *va = &uvma->va; 442 nouveau_uvma_gem_put(uvma); 443 drm_gpuva_remove(va); 444 nouveau_uvma_free(uvma); 445 } 446 447 static void 448 op_unmap_prepare_unwind(struct drm_gpuva *va) 449 { 450 drm_gpuva_insert(va->vm, va); 451 } 452 453 static void 454 nouveau_uvmm_sm_prepare_unwind(struct nouveau_uvmm *uvmm, 455 struct nouveau_uvma_prealloc *new, 456 struct drm_gpuva_ops *ops, 457 struct drm_gpuva_op *last, 458 struct uvmm_map_args *args) 459 { 460 struct drm_gpuva_op *op = last; 461 u64 vmm_get_start = args ? args->addr : 0; 462 u64 vmm_get_end = args ? args->addr + args->range : 0; 463 464 /* Unwind GPUVA space. */ 465 drm_gpuva_for_each_op_from_reverse(op, ops) { 466 switch (op->op) { 467 case DRM_GPUVA_OP_MAP: 468 op_map_prepare_unwind(new->map); 469 break; 470 case DRM_GPUVA_OP_REMAP: { 471 struct drm_gpuva_op_remap *r = &op->remap; 472 struct drm_gpuva *va = r->unmap->va; 473 474 if (r->next) 475 op_map_prepare_unwind(new->next); 476 477 if (r->prev) 478 op_map_prepare_unwind(new->prev); 479 480 op_unmap_prepare_unwind(va); 481 break; 482 } 483 case DRM_GPUVA_OP_UNMAP: 484 op_unmap_prepare_unwind(op->unmap.va); 485 break; 486 default: 487 break; 488 } 489 } 490 491 /* Unmap operation don't allocate page tables, hence skip the following 492 * page table unwind. 493 */ 494 if (!args) 495 return; 496 497 drm_gpuva_for_each_op(op, ops) { 498 switch (op->op) { 499 case DRM_GPUVA_OP_MAP: { 500 u64 vmm_get_range = vmm_get_end - vmm_get_start; 501 502 if (vmm_get_range) 503 nouveau_uvmm_vmm_put(uvmm, vmm_get_start, 504 vmm_get_range); 505 break; 506 } 507 case DRM_GPUVA_OP_REMAP: { 508 struct drm_gpuva_op_remap *r = &op->remap; 509 struct drm_gpuva *va = r->unmap->va; 510 u64 ustart = va->va.addr; 511 u64 urange = va->va.range; 512 u64 uend = ustart + urange; 513 514 if (r->prev) 515 vmm_get_start = uend; 516 517 if (r->next) 518 vmm_get_end = ustart; 519 520 if (r->prev && r->next) 521 vmm_get_start = vmm_get_end = 0; 522 523 break; 524 } 525 case DRM_GPUVA_OP_UNMAP: { 526 struct drm_gpuva_op_unmap *u = &op->unmap; 527 struct drm_gpuva *va = u->va; 528 u64 ustart = va->va.addr; 529 u64 urange = va->va.range; 530 u64 uend = ustart + urange; 531 532 /* Nothing to do for mappings we merge with. */ 533 if (uend == vmm_get_start || 534 ustart == vmm_get_end) 535 break; 536 537 if (ustart > vmm_get_start) { 538 u64 vmm_get_range = ustart - vmm_get_start; 539 540 nouveau_uvmm_vmm_put(uvmm, vmm_get_start, 541 vmm_get_range); 542 } 543 vmm_get_start = uend; 544 break; 545 } 546 default: 547 break; 548 } 549 550 if (op == last) 551 break; 552 } 553 } 554 555 static void 556 nouveau_uvmm_sm_map_prepare_unwind(struct nouveau_uvmm *uvmm, 557 struct nouveau_uvma_prealloc *new, 558 struct drm_gpuva_ops *ops, 559 u64 addr, u64 range) 560 { 561 struct drm_gpuva_op *last = drm_gpuva_last_op(ops); 562 struct uvmm_map_args args = { 563 .addr = addr, 564 .range = range, 565 }; 566 567 nouveau_uvmm_sm_prepare_unwind(uvmm, new, ops, last, &args); 568 } 569 570 static void 571 nouveau_uvmm_sm_unmap_prepare_unwind(struct nouveau_uvmm *uvmm, 572 struct nouveau_uvma_prealloc *new, 573 struct drm_gpuva_ops *ops) 574 { 575 struct drm_gpuva_op *last = drm_gpuva_last_op(ops); 576 577 nouveau_uvmm_sm_prepare_unwind(uvmm, new, ops, last, NULL); 578 } 579 580 static int 581 op_map_prepare(struct nouveau_uvmm *uvmm, 582 struct nouveau_uvma **puvma, 583 struct drm_gpuva_op_map *op, 584 struct uvmm_map_args *args) 585 { 586 struct nouveau_uvma *uvma; 587 int ret; 588 589 ret = nouveau_uvma_alloc(&uvma); 590 if (ret) 591 return ret; 592 593 uvma->region = args->region; 594 uvma->kind = args->kind; 595 596 drm_gpuva_map(&uvmm->base, &uvma->va, op); 597 598 /* Keep a reference until this uvma is destroyed. */ 599 nouveau_uvma_gem_get(uvma); 600 601 *puvma = uvma; 602 return 0; 603 } 604 605 static void 606 op_unmap_prepare(struct drm_gpuva_op_unmap *u) 607 { 608 drm_gpuva_unmap(u); 609 } 610 611 /* 612 * Note: @args should not be NULL when calling for a map operation. 613 */ 614 static int 615 nouveau_uvmm_sm_prepare(struct nouveau_uvmm *uvmm, 616 struct nouveau_uvma_prealloc *new, 617 struct drm_gpuva_ops *ops, 618 struct uvmm_map_args *args) 619 { 620 struct drm_gpuva_op *op; 621 u64 vmm_get_start = args ? args->addr : 0; 622 u64 vmm_get_end = args ? args->addr + args->range : 0; 623 int ret; 624 625 drm_gpuva_for_each_op(op, ops) { 626 switch (op->op) { 627 case DRM_GPUVA_OP_MAP: { 628 u64 vmm_get_range = vmm_get_end - vmm_get_start; 629 630 ret = op_map_prepare(uvmm, &new->map, &op->map, args); 631 if (ret) 632 goto unwind; 633 634 if (vmm_get_range) { 635 ret = nouveau_uvmm_vmm_get(uvmm, vmm_get_start, 636 vmm_get_range); 637 if (ret) { 638 op_map_prepare_unwind(new->map); 639 goto unwind; 640 } 641 } 642 643 break; 644 } 645 case DRM_GPUVA_OP_REMAP: { 646 struct drm_gpuva_op_remap *r = &op->remap; 647 struct drm_gpuva *va = r->unmap->va; 648 struct uvmm_map_args remap_args = { 649 .kind = uvma_from_va(va)->kind, 650 .region = uvma_from_va(va)->region, 651 }; 652 u64 ustart = va->va.addr; 653 u64 urange = va->va.range; 654 u64 uend = ustart + urange; 655 656 op_unmap_prepare(r->unmap); 657 658 if (r->prev) { 659 ret = op_map_prepare(uvmm, &new->prev, r->prev, 660 &remap_args); 661 if (ret) 662 goto unwind; 663 664 if (args) 665 vmm_get_start = uend; 666 } 667 668 if (r->next) { 669 ret = op_map_prepare(uvmm, &new->next, r->next, 670 &remap_args); 671 if (ret) { 672 if (r->prev) 673 op_map_prepare_unwind(new->prev); 674 goto unwind; 675 } 676 677 if (args) 678 vmm_get_end = ustart; 679 } 680 681 if (args && (r->prev && r->next)) 682 vmm_get_start = vmm_get_end = 0; 683 684 break; 685 } 686 case DRM_GPUVA_OP_UNMAP: { 687 struct drm_gpuva_op_unmap *u = &op->unmap; 688 struct drm_gpuva *va = u->va; 689 u64 ustart = va->va.addr; 690 u64 urange = va->va.range; 691 u64 uend = ustart + urange; 692 693 op_unmap_prepare(u); 694 695 if (!args) 696 break; 697 698 /* Nothing to do for mappings we merge with. */ 699 if (uend == vmm_get_start || 700 ustart == vmm_get_end) 701 break; 702 703 if (ustart > vmm_get_start) { 704 u64 vmm_get_range = ustart - vmm_get_start; 705 706 ret = nouveau_uvmm_vmm_get(uvmm, vmm_get_start, 707 vmm_get_range); 708 if (ret) { 709 op_unmap_prepare_unwind(va); 710 goto unwind; 711 } 712 } 713 vmm_get_start = uend; 714 715 break; 716 } 717 default: 718 ret = -EINVAL; 719 goto unwind; 720 } 721 } 722 723 return 0; 724 725 unwind: 726 if (op != drm_gpuva_first_op(ops)) 727 nouveau_uvmm_sm_prepare_unwind(uvmm, new, ops, 728 drm_gpuva_prev_op(op), 729 args); 730 return ret; 731 } 732 733 static int 734 nouveau_uvmm_sm_map_prepare(struct nouveau_uvmm *uvmm, 735 struct nouveau_uvma_prealloc *new, 736 struct nouveau_uvma_region *region, 737 struct drm_gpuva_ops *ops, 738 u64 addr, u64 range, u8 kind) 739 { 740 struct uvmm_map_args args = { 741 .region = region, 742 .addr = addr, 743 .range = range, 744 .kind = kind, 745 }; 746 747 return nouveau_uvmm_sm_prepare(uvmm, new, ops, &args); 748 } 749 750 static int 751 nouveau_uvmm_sm_unmap_prepare(struct nouveau_uvmm *uvmm, 752 struct nouveau_uvma_prealloc *new, 753 struct drm_gpuva_ops *ops) 754 { 755 return nouveau_uvmm_sm_prepare(uvmm, new, ops, NULL); 756 } 757 758 static struct drm_gem_object * 759 op_gem_obj(struct drm_gpuva_op *op) 760 { 761 switch (op->op) { 762 case DRM_GPUVA_OP_MAP: 763 return op->map.gem.obj; 764 case DRM_GPUVA_OP_REMAP: 765 /* Actually, we're looking for the GEMs backing remap.prev and 766 * remap.next, but since this is a remap they're identical to 767 * the GEM backing the unmapped GPUVA. 768 */ 769 return op->remap.unmap->va->gem.obj; 770 case DRM_GPUVA_OP_UNMAP: 771 return op->unmap.va->gem.obj; 772 default: 773 WARN(1, "Unknown operation.\n"); 774 return NULL; 775 } 776 } 777 778 static void 779 op_map(struct nouveau_uvma *uvma) 780 { 781 struct nouveau_bo *nvbo = nouveau_gem_object(uvma->va.gem.obj); 782 783 nouveau_uvma_map(uvma, nouveau_mem(nvbo->bo.resource)); 784 } 785 786 static void 787 op_unmap(struct drm_gpuva_op_unmap *u) 788 { 789 struct drm_gpuva *va = u->va; 790 struct nouveau_uvma *uvma = uvma_from_va(va); 791 792 /* nouveau_uvma_unmap() does not unmap if backing BO is evicted. */ 793 if (!u->keep) 794 nouveau_uvma_unmap(uvma); 795 } 796 797 static void 798 op_unmap_range(struct drm_gpuva_op_unmap *u, 799 u64 addr, u64 range) 800 { 801 struct nouveau_uvma *uvma = uvma_from_va(u->va); 802 bool sparse = !!uvma->region; 803 804 if (!drm_gpuva_invalidated(u->va)) 805 nouveau_uvmm_vmm_unmap(to_uvmm(uvma), addr, range, sparse); 806 } 807 808 static void 809 op_remap(struct drm_gpuva_op_remap *r, 810 struct nouveau_uvma_prealloc *new) 811 { 812 struct drm_gpuva_op_unmap *u = r->unmap; 813 struct nouveau_uvma *uvma = uvma_from_va(u->va); 814 u64 addr = uvma->va.va.addr; 815 u64 end = uvma->va.va.addr + uvma->va.va.range; 816 817 if (r->prev) 818 addr = r->prev->va.addr + r->prev->va.range; 819 820 if (r->next) 821 end = r->next->va.addr; 822 823 op_unmap_range(u, addr, end - addr); 824 } 825 826 static int 827 nouveau_uvmm_sm(struct nouveau_uvmm *uvmm, 828 struct nouveau_uvma_prealloc *new, 829 struct drm_gpuva_ops *ops) 830 { 831 struct drm_gpuva_op *op; 832 833 drm_gpuva_for_each_op(op, ops) { 834 switch (op->op) { 835 case DRM_GPUVA_OP_MAP: 836 op_map(new->map); 837 break; 838 case DRM_GPUVA_OP_REMAP: 839 op_remap(&op->remap, new); 840 break; 841 case DRM_GPUVA_OP_UNMAP: 842 op_unmap(&op->unmap); 843 break; 844 default: 845 break; 846 } 847 } 848 849 return 0; 850 } 851 852 static int 853 nouveau_uvmm_sm_map(struct nouveau_uvmm *uvmm, 854 struct nouveau_uvma_prealloc *new, 855 struct drm_gpuva_ops *ops) 856 { 857 return nouveau_uvmm_sm(uvmm, new, ops); 858 } 859 860 static int 861 nouveau_uvmm_sm_unmap(struct nouveau_uvmm *uvmm, 862 struct nouveau_uvma_prealloc *new, 863 struct drm_gpuva_ops *ops) 864 { 865 return nouveau_uvmm_sm(uvmm, new, ops); 866 } 867 868 static void 869 nouveau_uvmm_sm_cleanup(struct nouveau_uvmm *uvmm, 870 struct nouveau_uvma_prealloc *new, 871 struct drm_gpuva_ops *ops, bool unmap) 872 { 873 struct drm_gpuva_op *op; 874 875 drm_gpuva_for_each_op(op, ops) { 876 switch (op->op) { 877 case DRM_GPUVA_OP_MAP: 878 break; 879 case DRM_GPUVA_OP_REMAP: { 880 struct drm_gpuva_op_remap *r = &op->remap; 881 struct drm_gpuva_op_map *p = r->prev; 882 struct drm_gpuva_op_map *n = r->next; 883 struct drm_gpuva *va = r->unmap->va; 884 struct nouveau_uvma *uvma = uvma_from_va(va); 885 886 if (unmap) { 887 u64 addr = va->va.addr; 888 u64 end = addr + va->va.range; 889 890 if (p) 891 addr = p->va.addr + p->va.range; 892 893 if (n) 894 end = n->va.addr; 895 896 nouveau_uvmm_vmm_put(uvmm, addr, end - addr); 897 } 898 899 nouveau_uvma_gem_put(uvma); 900 nouveau_uvma_free(uvma); 901 break; 902 } 903 case DRM_GPUVA_OP_UNMAP: { 904 struct drm_gpuva_op_unmap *u = &op->unmap; 905 struct drm_gpuva *va = u->va; 906 struct nouveau_uvma *uvma = uvma_from_va(va); 907 908 if (unmap) 909 nouveau_uvma_vmm_put(uvma); 910 911 nouveau_uvma_gem_put(uvma); 912 nouveau_uvma_free(uvma); 913 break; 914 } 915 default: 916 break; 917 } 918 } 919 } 920 921 static void 922 nouveau_uvmm_sm_map_cleanup(struct nouveau_uvmm *uvmm, 923 struct nouveau_uvma_prealloc *new, 924 struct drm_gpuva_ops *ops) 925 { 926 nouveau_uvmm_sm_cleanup(uvmm, new, ops, false); 927 } 928 929 static void 930 nouveau_uvmm_sm_unmap_cleanup(struct nouveau_uvmm *uvmm, 931 struct nouveau_uvma_prealloc *new, 932 struct drm_gpuva_ops *ops) 933 { 934 nouveau_uvmm_sm_cleanup(uvmm, new, ops, true); 935 } 936 937 static int 938 nouveau_uvmm_validate_range(struct nouveau_uvmm *uvmm, u64 addr, u64 range) 939 { 940 if (addr & ~PAGE_MASK) 941 return -EINVAL; 942 943 if (range & ~PAGE_MASK) 944 return -EINVAL; 945 946 if (!drm_gpuvm_range_valid(&uvmm->base, addr, range)) 947 return -EINVAL; 948 949 return 0; 950 } 951 952 static int 953 nouveau_uvmm_bind_job_alloc(struct nouveau_uvmm_bind_job **pjob) 954 { 955 *pjob = kzalloc(sizeof(**pjob), GFP_KERNEL); 956 if (!*pjob) 957 return -ENOMEM; 958 959 kref_init(&(*pjob)->kref); 960 961 return 0; 962 } 963 964 static void 965 nouveau_uvmm_bind_job_free(struct kref *kref) 966 { 967 struct nouveau_uvmm_bind_job *job = 968 container_of(kref, struct nouveau_uvmm_bind_job, kref); 969 struct bind_job_op *op, *next; 970 971 list_for_each_op_safe(op, next, &job->ops) { 972 list_del(&op->entry); 973 kfree(op); 974 } 975 976 nouveau_job_free(&job->base); 977 kfree(job); 978 } 979 980 static void 981 nouveau_uvmm_bind_job_get(struct nouveau_uvmm_bind_job *job) 982 { 983 kref_get(&job->kref); 984 } 985 986 static void 987 nouveau_uvmm_bind_job_put(struct nouveau_uvmm_bind_job *job) 988 { 989 kref_put(&job->kref, nouveau_uvmm_bind_job_free); 990 } 991 992 static int 993 bind_validate_op(struct nouveau_job *job, 994 struct bind_job_op *op) 995 { 996 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli); 997 struct drm_gem_object *obj = op->gem.obj; 998 999 if (op->op == OP_MAP) { 1000 if (op->gem.offset & ~PAGE_MASK) 1001 return -EINVAL; 1002 1003 if (obj->size <= op->gem.offset) 1004 return -EINVAL; 1005 1006 if (op->va.range > (obj->size - op->gem.offset)) 1007 return -EINVAL; 1008 } 1009 1010 return nouveau_uvmm_validate_range(uvmm, op->va.addr, op->va.range); 1011 } 1012 1013 static void 1014 bind_validate_map_sparse(struct nouveau_job *job, u64 addr, u64 range) 1015 { 1016 struct nouveau_sched *sched = job->sched; 1017 struct nouveau_job *__job; 1018 struct bind_job_op *op; 1019 u64 end = addr + range; 1020 1021 again: 1022 spin_lock(&sched->job_list.lock); 1023 list_for_each_entry(__job, &sched->job_list.head, entry) { 1024 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(__job); 1025 1026 list_for_each_op(op, &bind_job->ops) { 1027 if (op->op == OP_UNMAP) { 1028 u64 op_addr = op->va.addr; 1029 u64 op_end = op_addr + op->va.range; 1030 1031 if (!(end <= op_addr || addr >= op_end)) { 1032 nouveau_uvmm_bind_job_get(bind_job); 1033 spin_unlock(&sched->job_list.lock); 1034 wait_for_completion(&bind_job->complete); 1035 nouveau_uvmm_bind_job_put(bind_job); 1036 goto again; 1037 } 1038 } 1039 } 1040 } 1041 spin_unlock(&sched->job_list.lock); 1042 } 1043 1044 static int 1045 bind_validate_map_common(struct nouveau_job *job, u64 addr, u64 range, 1046 bool sparse) 1047 { 1048 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli); 1049 struct nouveau_uvma_region *reg; 1050 u64 reg_addr, reg_end; 1051 u64 end = addr + range; 1052 1053 again: 1054 nouveau_uvmm_lock(uvmm); 1055 reg = nouveau_uvma_region_find_first(uvmm, addr, range); 1056 if (!reg) { 1057 nouveau_uvmm_unlock(uvmm); 1058 return 0; 1059 } 1060 1061 /* Generally, job submits are serialized, hence only 1062 * dirty regions can be modified concurrently. 1063 */ 1064 if (reg->dirty) { 1065 nouveau_uvma_region_get(reg); 1066 nouveau_uvmm_unlock(uvmm); 1067 wait_for_completion(®->complete); 1068 nouveau_uvma_region_put(reg); 1069 goto again; 1070 } 1071 nouveau_uvmm_unlock(uvmm); 1072 1073 if (sparse) 1074 return -ENOSPC; 1075 1076 reg_addr = reg->va.addr; 1077 reg_end = reg_addr + reg->va.range; 1078 1079 /* Make sure the mapping is either outside of a 1080 * region or fully enclosed by a region. 1081 */ 1082 if (reg_addr > addr || reg_end < end) 1083 return -ENOSPC; 1084 1085 return 0; 1086 } 1087 1088 static int 1089 bind_validate_region(struct nouveau_job *job) 1090 { 1091 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job); 1092 struct bind_job_op *op; 1093 int ret; 1094 1095 list_for_each_op(op, &bind_job->ops) { 1096 u64 op_addr = op->va.addr; 1097 u64 op_range = op->va.range; 1098 bool sparse = false; 1099 1100 switch (op->op) { 1101 case OP_MAP_SPARSE: 1102 sparse = true; 1103 bind_validate_map_sparse(job, op_addr, op_range); 1104 fallthrough; 1105 case OP_MAP: 1106 ret = bind_validate_map_common(job, op_addr, op_range, 1107 sparse); 1108 if (ret) 1109 return ret; 1110 break; 1111 default: 1112 break; 1113 } 1114 } 1115 1116 return 0; 1117 } 1118 1119 static void 1120 bind_link_gpuvas(struct bind_job_op *bop) 1121 { 1122 struct nouveau_uvma_prealloc *new = &bop->new; 1123 struct drm_gpuvm_bo *vm_bo = bop->vm_bo; 1124 struct drm_gpuva_ops *ops = bop->ops; 1125 struct drm_gpuva_op *op; 1126 1127 drm_gpuva_for_each_op(op, ops) { 1128 switch (op->op) { 1129 case DRM_GPUVA_OP_MAP: 1130 drm_gpuva_link(&new->map->va, vm_bo); 1131 break; 1132 case DRM_GPUVA_OP_REMAP: { 1133 struct drm_gpuva *va = op->remap.unmap->va; 1134 1135 if (op->remap.prev) 1136 drm_gpuva_link(&new->prev->va, va->vm_bo); 1137 if (op->remap.next) 1138 drm_gpuva_link(&new->next->va, va->vm_bo); 1139 drm_gpuva_unlink(va); 1140 break; 1141 } 1142 case DRM_GPUVA_OP_UNMAP: 1143 drm_gpuva_unlink(op->unmap.va); 1144 break; 1145 default: 1146 break; 1147 } 1148 } 1149 } 1150 1151 static int 1152 bind_lock_validate(struct nouveau_job *job, struct drm_exec *exec, 1153 unsigned int num_fences) 1154 { 1155 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job); 1156 struct bind_job_op *op; 1157 int ret; 1158 1159 list_for_each_op(op, &bind_job->ops) { 1160 struct drm_gpuva_op *va_op; 1161 1162 if (!op->ops) 1163 continue; 1164 1165 drm_gpuva_for_each_op(va_op, op->ops) { 1166 struct drm_gem_object *obj = op_gem_obj(va_op); 1167 1168 if (unlikely(!obj)) 1169 continue; 1170 1171 ret = drm_exec_prepare_obj(exec, obj, num_fences); 1172 if (ret) 1173 return ret; 1174 1175 /* Don't validate GEMs backing mappings we're about to 1176 * unmap, it's not worth the effort. 1177 */ 1178 if (va_op->op == DRM_GPUVA_OP_UNMAP) 1179 continue; 1180 1181 ret = nouveau_bo_validate(nouveau_gem_object(obj), 1182 true, false); 1183 if (ret) 1184 return ret; 1185 } 1186 } 1187 1188 return 0; 1189 } 1190 1191 static int 1192 nouveau_uvmm_bind_job_submit(struct nouveau_job *job, 1193 struct drm_gpuvm_exec *vme) 1194 { 1195 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli); 1196 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job); 1197 struct drm_exec *exec = &vme->exec; 1198 struct bind_job_op *op; 1199 int ret; 1200 1201 list_for_each_op(op, &bind_job->ops) { 1202 if (op->op == OP_MAP) { 1203 struct drm_gem_object *obj = op->gem.obj = 1204 drm_gem_object_lookup(job->file_priv, 1205 op->gem.handle); 1206 if (!obj) 1207 return -ENOENT; 1208 1209 dma_resv_lock(obj->resv, NULL); 1210 op->vm_bo = drm_gpuvm_bo_obtain(&uvmm->base, obj); 1211 dma_resv_unlock(obj->resv); 1212 if (IS_ERR(op->vm_bo)) 1213 return PTR_ERR(op->vm_bo); 1214 1215 drm_gpuvm_bo_extobj_add(op->vm_bo); 1216 } 1217 1218 ret = bind_validate_op(job, op); 1219 if (ret) 1220 return ret; 1221 } 1222 1223 /* If a sparse region or mapping overlaps a dirty region, we need to 1224 * wait for the region to complete the unbind process. This is due to 1225 * how page table management is currently implemented. A future 1226 * implementation might change this. 1227 */ 1228 ret = bind_validate_region(job); 1229 if (ret) 1230 return ret; 1231 1232 /* Once we start modifying the GPU VA space we need to keep holding the 1233 * uvmm lock until we can't fail anymore. This is due to the set of GPU 1234 * VA space changes must appear atomically and we need to be able to 1235 * unwind all GPU VA space changes on failure. 1236 */ 1237 nouveau_uvmm_lock(uvmm); 1238 1239 list_for_each_op(op, &bind_job->ops) { 1240 switch (op->op) { 1241 case OP_MAP_SPARSE: 1242 ret = nouveau_uvma_region_create(uvmm, 1243 op->va.addr, 1244 op->va.range); 1245 if (ret) 1246 goto unwind_continue; 1247 1248 break; 1249 case OP_UNMAP_SPARSE: 1250 op->reg = nouveau_uvma_region_find(uvmm, op->va.addr, 1251 op->va.range); 1252 if (!op->reg || op->reg->dirty) { 1253 ret = -ENOENT; 1254 goto unwind_continue; 1255 } 1256 1257 op->ops = drm_gpuvm_sm_unmap_ops_create(&uvmm->base, 1258 op->va.addr, 1259 op->va.range); 1260 if (IS_ERR(op->ops)) { 1261 ret = PTR_ERR(op->ops); 1262 goto unwind_continue; 1263 } 1264 1265 ret = nouveau_uvmm_sm_unmap_prepare(uvmm, &op->new, 1266 op->ops); 1267 if (ret) { 1268 drm_gpuva_ops_free(&uvmm->base, op->ops); 1269 op->ops = NULL; 1270 op->reg = NULL; 1271 goto unwind_continue; 1272 } 1273 1274 nouveau_uvma_region_dirty(op->reg); 1275 1276 break; 1277 case OP_MAP: { 1278 struct nouveau_uvma_region *reg; 1279 struct drm_gpuvm_map_req map_req = { 1280 .map.va.addr = op->va.addr, 1281 .map.va.range = op->va.range, 1282 .map.gem.obj = op->gem.obj, 1283 .map.gem.offset = op->gem.offset, 1284 }; 1285 1286 reg = nouveau_uvma_region_find_first(uvmm, 1287 op->va.addr, 1288 op->va.range); 1289 if (reg) { 1290 u64 reg_addr = reg->va.addr; 1291 u64 reg_end = reg_addr + reg->va.range; 1292 u64 op_addr = op->va.addr; 1293 u64 op_end = op_addr + op->va.range; 1294 1295 if (unlikely(reg->dirty)) { 1296 ret = -EINVAL; 1297 goto unwind_continue; 1298 } 1299 1300 /* Make sure the mapping is either outside of a 1301 * region or fully enclosed by a region. 1302 */ 1303 if (reg_addr > op_addr || reg_end < op_end) { 1304 ret = -ENOSPC; 1305 goto unwind_continue; 1306 } 1307 } 1308 1309 op->ops = drm_gpuvm_sm_map_ops_create(&uvmm->base, 1310 &map_req); 1311 if (IS_ERR(op->ops)) { 1312 ret = PTR_ERR(op->ops); 1313 goto unwind_continue; 1314 } 1315 1316 ret = nouveau_uvmm_sm_map_prepare(uvmm, &op->new, 1317 reg, op->ops, 1318 op->va.addr, 1319 op->va.range, 1320 op->flags & 0xff); 1321 if (ret) { 1322 drm_gpuva_ops_free(&uvmm->base, op->ops); 1323 op->ops = NULL; 1324 goto unwind_continue; 1325 } 1326 1327 break; 1328 } 1329 case OP_UNMAP: 1330 op->ops = drm_gpuvm_sm_unmap_ops_create(&uvmm->base, 1331 op->va.addr, 1332 op->va.range); 1333 if (IS_ERR(op->ops)) { 1334 ret = PTR_ERR(op->ops); 1335 goto unwind_continue; 1336 } 1337 1338 ret = nouveau_uvmm_sm_unmap_prepare(uvmm, &op->new, 1339 op->ops); 1340 if (ret) { 1341 drm_gpuva_ops_free(&uvmm->base, op->ops); 1342 op->ops = NULL; 1343 goto unwind_continue; 1344 } 1345 1346 break; 1347 default: 1348 ret = -EINVAL; 1349 goto unwind_continue; 1350 } 1351 } 1352 1353 drm_exec_init(exec, vme->flags, 0); 1354 drm_exec_until_all_locked(exec) { 1355 ret = bind_lock_validate(job, exec, vme->num_fences); 1356 drm_exec_retry_on_contention(exec); 1357 if (ret) { 1358 op = list_last_op(&bind_job->ops); 1359 goto unwind; 1360 } 1361 } 1362 1363 /* Link and unlink GPUVAs while holding the dma_resv lock. 1364 * 1365 * As long as we validate() all GEMs and add fences to all GEMs DMA 1366 * reservations backing map and remap operations we can be sure there 1367 * won't be any concurrent (in)validations during job execution, hence 1368 * we're safe to check drm_gpuva_invalidated() within the fence 1369 * signalling critical path without holding a separate lock. 1370 * 1371 * GPUVAs about to be unmapped are safe as well, since they're unlinked 1372 * already. 1373 * 1374 * GEMs from map and remap operations must be validated before linking 1375 * their corresponding mappings to prevent the actual PT update to 1376 * happen right away in validate() rather than asynchronously as 1377 * intended. 1378 * 1379 * Note that after linking and unlinking the GPUVAs in this loop this 1380 * function cannot fail anymore, hence there is no need for an unwind 1381 * path. 1382 */ 1383 list_for_each_op(op, &bind_job->ops) { 1384 switch (op->op) { 1385 case OP_UNMAP_SPARSE: 1386 case OP_MAP: 1387 case OP_UNMAP: 1388 bind_link_gpuvas(op); 1389 break; 1390 default: 1391 break; 1392 } 1393 } 1394 nouveau_uvmm_unlock(uvmm); 1395 1396 return 0; 1397 1398 unwind_continue: 1399 op = list_prev_op(op); 1400 unwind: 1401 list_for_each_op_from_reverse(op, &bind_job->ops) { 1402 switch (op->op) { 1403 case OP_MAP_SPARSE: 1404 nouveau_uvma_region_destroy(uvmm, op->va.addr, 1405 op->va.range); 1406 break; 1407 case OP_UNMAP_SPARSE: 1408 __nouveau_uvma_region_insert(uvmm, op->reg); 1409 nouveau_uvmm_sm_unmap_prepare_unwind(uvmm, &op->new, 1410 op->ops); 1411 break; 1412 case OP_MAP: 1413 nouveau_uvmm_sm_map_prepare_unwind(uvmm, &op->new, 1414 op->ops, 1415 op->va.addr, 1416 op->va.range); 1417 break; 1418 case OP_UNMAP: 1419 nouveau_uvmm_sm_unmap_prepare_unwind(uvmm, &op->new, 1420 op->ops); 1421 break; 1422 } 1423 1424 drm_gpuva_ops_free(&uvmm->base, op->ops); 1425 op->ops = NULL; 1426 op->reg = NULL; 1427 } 1428 1429 nouveau_uvmm_unlock(uvmm); 1430 drm_gpuvm_exec_unlock(vme); 1431 return ret; 1432 } 1433 1434 static void 1435 nouveau_uvmm_bind_job_armed_submit(struct nouveau_job *job, 1436 struct drm_gpuvm_exec *vme) 1437 { 1438 drm_gpuvm_exec_resv_add_fence(vme, job->done_fence, 1439 job->resv_usage, job->resv_usage); 1440 drm_gpuvm_exec_unlock(vme); 1441 } 1442 1443 static struct dma_fence * 1444 nouveau_uvmm_bind_job_run(struct nouveau_job *job) 1445 { 1446 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job); 1447 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli); 1448 struct bind_job_op *op; 1449 int ret = 0; 1450 1451 list_for_each_op(op, &bind_job->ops) { 1452 switch (op->op) { 1453 case OP_MAP_SPARSE: 1454 /* noop */ 1455 break; 1456 case OP_MAP: 1457 ret = nouveau_uvmm_sm_map(uvmm, &op->new, op->ops); 1458 if (ret) 1459 goto out; 1460 break; 1461 case OP_UNMAP_SPARSE: 1462 fallthrough; 1463 case OP_UNMAP: 1464 ret = nouveau_uvmm_sm_unmap(uvmm, &op->new, op->ops); 1465 if (ret) 1466 goto out; 1467 break; 1468 } 1469 } 1470 1471 out: 1472 if (ret) 1473 NV_PRINTK(err, job->cli, "bind job failed: %d\n", ret); 1474 return ERR_PTR(ret); 1475 } 1476 1477 static void 1478 nouveau_uvmm_bind_job_cleanup(struct nouveau_job *job) 1479 { 1480 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job); 1481 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli); 1482 struct bind_job_op *op; 1483 1484 list_for_each_op(op, &bind_job->ops) { 1485 struct drm_gem_object *obj = op->gem.obj; 1486 1487 /* When nouveau_uvmm_bind_job_submit() fails op->ops and op->reg 1488 * will be NULL, hence skip the cleanup. 1489 */ 1490 switch (op->op) { 1491 case OP_MAP_SPARSE: 1492 /* noop */ 1493 break; 1494 case OP_UNMAP_SPARSE: 1495 if (!IS_ERR_OR_NULL(op->ops)) 1496 nouveau_uvmm_sm_unmap_cleanup(uvmm, &op->new, 1497 op->ops); 1498 1499 if (op->reg) { 1500 nouveau_uvma_region_sparse_unref(op->reg); 1501 nouveau_uvmm_lock(uvmm); 1502 nouveau_uvma_region_remove(op->reg); 1503 nouveau_uvmm_unlock(uvmm); 1504 nouveau_uvma_region_complete(op->reg); 1505 nouveau_uvma_region_put(op->reg); 1506 } 1507 1508 break; 1509 case OP_MAP: 1510 if (!IS_ERR_OR_NULL(op->ops)) 1511 nouveau_uvmm_sm_map_cleanup(uvmm, &op->new, 1512 op->ops); 1513 break; 1514 case OP_UNMAP: 1515 if (!IS_ERR_OR_NULL(op->ops)) 1516 nouveau_uvmm_sm_unmap_cleanup(uvmm, &op->new, 1517 op->ops); 1518 break; 1519 } 1520 1521 if (!IS_ERR_OR_NULL(op->ops)) 1522 drm_gpuva_ops_free(&uvmm->base, op->ops); 1523 1524 if (!IS_ERR_OR_NULL(op->vm_bo)) { 1525 dma_resv_lock(obj->resv, NULL); 1526 drm_gpuvm_bo_put(op->vm_bo); 1527 dma_resv_unlock(obj->resv); 1528 } 1529 1530 if (obj) 1531 drm_gem_object_put(obj); 1532 } 1533 1534 nouveau_job_done(job); 1535 complete_all(&bind_job->complete); 1536 1537 nouveau_uvmm_bind_job_put(bind_job); 1538 } 1539 1540 static const struct nouveau_job_ops nouveau_bind_job_ops = { 1541 .submit = nouveau_uvmm_bind_job_submit, 1542 .armed_submit = nouveau_uvmm_bind_job_armed_submit, 1543 .run = nouveau_uvmm_bind_job_run, 1544 .free = nouveau_uvmm_bind_job_cleanup, 1545 }; 1546 1547 static int 1548 bind_job_op_from_uop(struct bind_job_op **pop, 1549 struct drm_nouveau_vm_bind_op *uop) 1550 { 1551 struct bind_job_op *op; 1552 1553 op = *pop = kzalloc(sizeof(*op), GFP_KERNEL); 1554 if (!op) 1555 return -ENOMEM; 1556 1557 switch (uop->op) { 1558 case OP_MAP: 1559 op->op = uop->flags & DRM_NOUVEAU_VM_BIND_SPARSE ? 1560 OP_MAP_SPARSE : OP_MAP; 1561 break; 1562 case OP_UNMAP: 1563 op->op = uop->flags & DRM_NOUVEAU_VM_BIND_SPARSE ? 1564 OP_UNMAP_SPARSE : OP_UNMAP; 1565 break; 1566 default: 1567 op->op = uop->op; 1568 break; 1569 } 1570 1571 op->flags = uop->flags; 1572 op->va.addr = uop->addr; 1573 op->va.range = uop->range; 1574 op->gem.handle = uop->handle; 1575 op->gem.offset = uop->bo_offset; 1576 1577 return 0; 1578 } 1579 1580 static void 1581 bind_job_ops_free(struct list_head *ops) 1582 { 1583 struct bind_job_op *op, *next; 1584 1585 list_for_each_op_safe(op, next, ops) { 1586 list_del(&op->entry); 1587 kfree(op); 1588 } 1589 } 1590 1591 static int 1592 nouveau_uvmm_bind_job_init(struct nouveau_uvmm_bind_job **pjob, 1593 struct nouveau_uvmm_bind_job_args *__args) 1594 { 1595 struct nouveau_uvmm_bind_job *job; 1596 struct nouveau_job_args args = {}; 1597 struct bind_job_op *op; 1598 int i, ret; 1599 1600 ret = nouveau_uvmm_bind_job_alloc(&job); 1601 if (ret) 1602 return ret; 1603 1604 INIT_LIST_HEAD(&job->ops); 1605 1606 for (i = 0; i < __args->op.count; i++) { 1607 ret = bind_job_op_from_uop(&op, &__args->op.s[i]); 1608 if (ret) 1609 goto err_free; 1610 1611 list_add_tail(&op->entry, &job->ops); 1612 } 1613 1614 init_completion(&job->complete); 1615 1616 args.file_priv = __args->file_priv; 1617 1618 args.sched = __args->sched; 1619 args.credits = 1; 1620 1621 args.in_sync.count = __args->in_sync.count; 1622 args.in_sync.s = __args->in_sync.s; 1623 1624 args.out_sync.count = __args->out_sync.count; 1625 args.out_sync.s = __args->out_sync.s; 1626 1627 args.sync = !(__args->flags & DRM_NOUVEAU_VM_BIND_RUN_ASYNC); 1628 args.ops = &nouveau_bind_job_ops; 1629 args.resv_usage = DMA_RESV_USAGE_BOOKKEEP; 1630 1631 ret = nouveau_job_init(&job->base, &args); 1632 if (ret) 1633 goto err_free; 1634 1635 *pjob = job; 1636 return 0; 1637 1638 err_free: 1639 bind_job_ops_free(&job->ops); 1640 kfree(job); 1641 *pjob = NULL; 1642 1643 return ret; 1644 } 1645 1646 static int 1647 nouveau_uvmm_vm_bind(struct nouveau_uvmm_bind_job_args *args) 1648 { 1649 struct nouveau_uvmm_bind_job *job; 1650 int ret; 1651 1652 ret = nouveau_uvmm_bind_job_init(&job, args); 1653 if (ret) 1654 return ret; 1655 1656 ret = nouveau_job_submit(&job->base); 1657 if (ret) 1658 goto err_job_fini; 1659 1660 return 0; 1661 1662 err_job_fini: 1663 nouveau_job_fini(&job->base); 1664 return ret; 1665 } 1666 1667 static int 1668 nouveau_uvmm_vm_bind_ucopy(struct nouveau_uvmm_bind_job_args *args, 1669 struct drm_nouveau_vm_bind *req) 1670 { 1671 struct drm_nouveau_sync **s; 1672 u32 inc = req->wait_count; 1673 u64 ins = req->wait_ptr; 1674 u32 outc = req->sig_count; 1675 u64 outs = req->sig_ptr; 1676 u32 opc = req->op_count; 1677 u64 ops = req->op_ptr; 1678 int ret; 1679 1680 args->flags = req->flags; 1681 1682 if (opc) { 1683 args->op.count = opc; 1684 args->op.s = u_memcpya(ops, opc, 1685 sizeof(*args->op.s)); 1686 if (IS_ERR(args->op.s)) 1687 return PTR_ERR(args->op.s); 1688 } 1689 1690 if (inc) { 1691 s = &args->in_sync.s; 1692 1693 args->in_sync.count = inc; 1694 *s = u_memcpya(ins, inc, sizeof(**s)); 1695 if (IS_ERR(*s)) { 1696 ret = PTR_ERR(*s); 1697 goto err_free_ops; 1698 } 1699 } 1700 1701 if (outc) { 1702 s = &args->out_sync.s; 1703 1704 args->out_sync.count = outc; 1705 *s = u_memcpya(outs, outc, sizeof(**s)); 1706 if (IS_ERR(*s)) { 1707 ret = PTR_ERR(*s); 1708 goto err_free_ins; 1709 } 1710 } 1711 1712 return 0; 1713 1714 err_free_ops: 1715 u_free(args->op.s); 1716 err_free_ins: 1717 u_free(args->in_sync.s); 1718 return ret; 1719 } 1720 1721 static void 1722 nouveau_uvmm_vm_bind_ufree(struct nouveau_uvmm_bind_job_args *args) 1723 { 1724 u_free(args->op.s); 1725 u_free(args->in_sync.s); 1726 u_free(args->out_sync.s); 1727 } 1728 1729 int 1730 nouveau_uvmm_ioctl_vm_bind(struct drm_device *dev, 1731 void *data, 1732 struct drm_file *file_priv) 1733 { 1734 struct nouveau_cli *cli = nouveau_cli(file_priv); 1735 struct nouveau_uvmm_bind_job_args args = {}; 1736 struct drm_nouveau_vm_bind *req = data; 1737 int ret = 0; 1738 1739 if (unlikely(!nouveau_cli_uvmm_locked(cli))) 1740 return -ENOSYS; 1741 1742 ret = nouveau_uvmm_vm_bind_ucopy(&args, req); 1743 if (ret) 1744 return ret; 1745 1746 args.sched = cli->sched; 1747 args.file_priv = file_priv; 1748 1749 ret = nouveau_uvmm_vm_bind(&args); 1750 if (ret) 1751 goto out_free_args; 1752 1753 out_free_args: 1754 nouveau_uvmm_vm_bind_ufree(&args); 1755 return ret; 1756 } 1757 1758 void 1759 nouveau_uvmm_bo_map_all(struct nouveau_bo *nvbo, struct nouveau_mem *mem) 1760 { 1761 struct drm_gem_object *obj = &nvbo->bo.base; 1762 struct drm_gpuvm_bo *vm_bo; 1763 struct drm_gpuva *va; 1764 1765 dma_resv_assert_held(obj->resv); 1766 1767 drm_gem_for_each_gpuvm_bo(vm_bo, obj) { 1768 drm_gpuvm_bo_for_each_va(va, vm_bo) { 1769 struct nouveau_uvma *uvma = uvma_from_va(va); 1770 1771 nouveau_uvma_map(uvma, mem); 1772 drm_gpuva_invalidate(va, false); 1773 } 1774 } 1775 } 1776 1777 void 1778 nouveau_uvmm_bo_unmap_all(struct nouveau_bo *nvbo) 1779 { 1780 struct drm_gem_object *obj = &nvbo->bo.base; 1781 struct drm_gpuvm_bo *vm_bo; 1782 struct drm_gpuva *va; 1783 1784 dma_resv_assert_held(obj->resv); 1785 1786 drm_gem_for_each_gpuvm_bo(vm_bo, obj) { 1787 drm_gpuvm_bo_for_each_va(va, vm_bo) { 1788 struct nouveau_uvma *uvma = uvma_from_va(va); 1789 1790 nouveau_uvma_unmap(uvma); 1791 drm_gpuva_invalidate(va, true); 1792 } 1793 } 1794 } 1795 1796 static void 1797 nouveau_uvmm_free(struct drm_gpuvm *gpuvm) 1798 { 1799 struct nouveau_uvmm *uvmm = uvmm_from_gpuvm(gpuvm); 1800 1801 kfree(uvmm); 1802 } 1803 1804 static int 1805 nouveau_uvmm_bo_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec) 1806 { 1807 struct nouveau_bo *nvbo = nouveau_gem_object(vm_bo->obj); 1808 1809 nouveau_bo_placement_set(nvbo, nvbo->valid_domains, 0); 1810 return nouveau_bo_validate(nvbo, true, false); 1811 } 1812 1813 static const struct drm_gpuvm_ops gpuvm_ops = { 1814 .vm_free = nouveau_uvmm_free, 1815 .vm_bo_validate = nouveau_uvmm_bo_validate, 1816 }; 1817 1818 int 1819 nouveau_uvmm_ioctl_vm_init(struct drm_device *dev, 1820 void *data, 1821 struct drm_file *file_priv) 1822 { 1823 struct nouveau_uvmm *uvmm; 1824 struct nouveau_cli *cli = nouveau_cli(file_priv); 1825 struct drm_device *drm = cli->drm->dev; 1826 struct drm_gem_object *r_obj; 1827 struct drm_nouveau_vm_init *init = data; 1828 u64 kernel_managed_end; 1829 int ret; 1830 1831 if (check_add_overflow(init->kernel_managed_addr, 1832 init->kernel_managed_size, 1833 &kernel_managed_end)) 1834 return -EINVAL; 1835 1836 if (kernel_managed_end > NOUVEAU_VA_SPACE_END) 1837 return -EINVAL; 1838 1839 mutex_lock(&cli->mutex); 1840 1841 if (unlikely(cli->uvmm.disabled)) { 1842 ret = -ENOSYS; 1843 goto out_unlock; 1844 } 1845 1846 uvmm = kzalloc(sizeof(*uvmm), GFP_KERNEL); 1847 if (!uvmm) { 1848 ret = -ENOMEM; 1849 goto out_unlock; 1850 } 1851 1852 r_obj = drm_gpuvm_resv_object_alloc(drm); 1853 if (!r_obj) { 1854 kfree(uvmm); 1855 ret = -ENOMEM; 1856 goto out_unlock; 1857 } 1858 1859 mutex_init(&uvmm->mutex); 1860 mt_init_flags(&uvmm->region_mt, MT_FLAGS_LOCK_EXTERN); 1861 mt_set_external_lock(&uvmm->region_mt, &uvmm->mutex); 1862 1863 drm_gpuvm_init(&uvmm->base, cli->name, 0, drm, r_obj, 1864 NOUVEAU_VA_SPACE_START, 1865 NOUVEAU_VA_SPACE_END, 1866 init->kernel_managed_addr, 1867 init->kernel_managed_size, 1868 &gpuvm_ops); 1869 /* GPUVM takes care from here on. */ 1870 drm_gem_object_put(r_obj); 1871 1872 ret = nvif_vmm_ctor(&cli->mmu, "uvmm", 1873 cli->vmm.vmm.object.oclass, RAW, 1874 init->kernel_managed_addr, 1875 init->kernel_managed_size, 1876 NULL, 0, &uvmm->vmm.vmm); 1877 if (ret) 1878 goto out_gpuvm_fini; 1879 1880 uvmm->vmm.cli = cli; 1881 cli->uvmm.ptr = uvmm; 1882 mutex_unlock(&cli->mutex); 1883 1884 return 0; 1885 1886 out_gpuvm_fini: 1887 drm_gpuvm_put(&uvmm->base); 1888 out_unlock: 1889 mutex_unlock(&cli->mutex); 1890 return ret; 1891 } 1892 1893 void 1894 nouveau_uvmm_fini(struct nouveau_uvmm *uvmm) 1895 { 1896 MA_STATE(mas, &uvmm->region_mt, 0, 0); 1897 struct nouveau_uvma_region *reg; 1898 struct nouveau_cli *cli = uvmm->vmm.cli; 1899 struct drm_gpuva *va, *next; 1900 1901 nouveau_uvmm_lock(uvmm); 1902 drm_gpuvm_for_each_va_safe(va, next, &uvmm->base) { 1903 struct nouveau_uvma *uvma = uvma_from_va(va); 1904 struct drm_gem_object *obj = va->gem.obj; 1905 1906 if (unlikely(va == &uvmm->base.kernel_alloc_node)) 1907 continue; 1908 1909 drm_gpuva_remove(va); 1910 1911 dma_resv_lock(obj->resv, NULL); 1912 drm_gpuva_unlink(va); 1913 dma_resv_unlock(obj->resv); 1914 1915 nouveau_uvma_unmap(uvma); 1916 nouveau_uvma_vmm_put(uvma); 1917 1918 nouveau_uvma_gem_put(uvma); 1919 nouveau_uvma_free(uvma); 1920 } 1921 1922 mas_for_each(&mas, reg, ULONG_MAX) { 1923 mas_erase(&mas); 1924 nouveau_uvma_region_sparse_unref(reg); 1925 nouveau_uvma_region_put(reg); 1926 } 1927 1928 WARN(!mtree_empty(&uvmm->region_mt), 1929 "nouveau_uvma_region tree not empty, potentially leaking memory."); 1930 __mt_destroy(&uvmm->region_mt); 1931 nouveau_uvmm_unlock(uvmm); 1932 1933 mutex_lock(&cli->mutex); 1934 nouveau_vmm_fini(&uvmm->vmm); 1935 drm_gpuvm_put(&uvmm->base); 1936 mutex_unlock(&cli->mutex); 1937 } 1938