1 // SPDX-License-Identifier: GPL-2.0-only OR MIT 2 /* Copyright (c) 2023 Imagination Technologies Ltd. */ 3 4 #include "pvr_vm.h" 5 6 #include "pvr_device.h" 7 #include "pvr_drv.h" 8 #include "pvr_gem.h" 9 #include "pvr_mmu.h" 10 #include "pvr_rogue_fwif.h" 11 #include "pvr_rogue_heap_config.h" 12 13 #include <drm/drm_exec.h> 14 #include <drm/drm_gem.h> 15 #include <drm/drm_gpuvm.h> 16 #include <drm/drm_print.h> 17 18 #include <linux/bug.h> 19 #include <linux/container_of.h> 20 #include <linux/err.h> 21 #include <linux/errno.h> 22 #include <linux/gfp_types.h> 23 #include <linux/kref.h> 24 #include <linux/mutex.h> 25 #include <linux/stddef.h> 26 27 /** 28 * DOC: Memory context 29 * 30 * This is the "top level" datatype in the VM code. It's exposed in the public 31 * API as an opaque handle. 32 */ 33 34 /** 35 * struct pvr_vm_context - Context type used to represent a single VM. 36 */ 37 struct pvr_vm_context { 38 /** 39 * @pvr_dev: The PowerVR device to which this context is bound. 40 * This binding is immutable for the life of the context. 41 */ 42 struct pvr_device *pvr_dev; 43 44 /** @mmu_ctx: The context for binding to physical memory. */ 45 struct pvr_mmu_context *mmu_ctx; 46 47 /** @gpuvm_mgr: GPUVM object associated with this context. */ 48 struct drm_gpuvm gpuvm_mgr; 49 50 /** @lock: Global lock on this VM. */ 51 struct mutex lock; 52 53 /** 54 * @fw_mem_ctx_obj: Firmware object representing firmware memory 55 * context. 56 */ 57 struct pvr_fw_object *fw_mem_ctx_obj; 58 59 /** @ref_count: Reference count of object. */ 60 struct kref ref_count; 61 62 /** 63 * @dummy_gem: GEM object to enable VM reservation. All private BOs 64 * should use the @dummy_gem.resv and not their own _resv field. 65 */ 66 struct drm_gem_object dummy_gem; 67 }; 68 69 static inline 70 struct pvr_vm_context *to_pvr_vm_context(struct drm_gpuvm *gpuvm) 71 { 72 return container_of(gpuvm, struct pvr_vm_context, gpuvm_mgr); 73 } 74 75 struct pvr_vm_context *pvr_vm_context_get(struct pvr_vm_context *vm_ctx) 76 { 77 if (vm_ctx) 78 kref_get(&vm_ctx->ref_count); 79 80 return vm_ctx; 81 } 82 83 /** 84 * pvr_vm_get_page_table_root_addr() - Get the DMA address of the root of the 85 * page table structure behind a VM context. 86 * @vm_ctx: Target VM context. 87 */ 88 dma_addr_t pvr_vm_get_page_table_root_addr(struct pvr_vm_context *vm_ctx) 89 { 90 return pvr_mmu_get_root_table_dma_addr(vm_ctx->mmu_ctx); 91 } 92 93 /** 94 * pvr_vm_get_dma_resv() - Expose the dma_resv owned by the VM context. 95 * @vm_ctx: Target VM context. 96 * 97 * This is used to allow private BOs to share a dma_resv for faster fence 98 * updates. 99 * 100 * Returns: The dma_resv pointer. 101 */ 102 struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx) 103 { 104 return vm_ctx->dummy_gem.resv; 105 } 106 107 /** 108 * DOC: Memory mappings 109 */ 110 111 /** 112 * struct pvr_vm_gpuva - Wrapper type representing a single VM mapping. 113 */ 114 struct pvr_vm_gpuva { 115 /** @base: The wrapped drm_gpuva object. */ 116 struct drm_gpuva base; 117 }; 118 119 #define to_pvr_vm_gpuva(va) container_of_const(va, struct pvr_vm_gpuva, base) 120 121 enum pvr_vm_bind_type { 122 PVR_VM_BIND_TYPE_MAP, 123 PVR_VM_BIND_TYPE_UNMAP, 124 }; 125 126 /** 127 * struct pvr_vm_bind_op - Context of a map/unmap operation. 128 */ 129 struct pvr_vm_bind_op { 130 /** @type: Map or unmap. */ 131 enum pvr_vm_bind_type type; 132 133 /** @pvr_obj: Object associated with mapping (map only). */ 134 struct pvr_gem_object *pvr_obj; 135 136 /** 137 * @vm_ctx: VM context where the mapping will be created or destroyed. 138 */ 139 struct pvr_vm_context *vm_ctx; 140 141 /** @mmu_op_ctx: MMU op context. */ 142 struct pvr_mmu_op_context *mmu_op_ctx; 143 144 /** @gpuvm_bo: Prealloced wrapped BO for attaching to the gpuvm. */ 145 struct drm_gpuvm_bo *gpuvm_bo; 146 147 /** 148 * @new_va: Prealloced VA mapping object (init in callback). 149 * Used when creating a mapping. 150 */ 151 struct pvr_vm_gpuva *new_va; 152 153 /** 154 * @prev_va: Prealloced VA mapping object (init in callback). 155 * Used when a mapping or unmapping operation overlaps an existing 156 * mapping and splits away the beginning into a new mapping. 157 */ 158 struct pvr_vm_gpuva *prev_va; 159 160 /** 161 * @next_va: Prealloced VA mapping object (init in callback). 162 * Used when a mapping or unmapping operation overlaps an existing 163 * mapping and splits away the end into a new mapping. 164 */ 165 struct pvr_vm_gpuva *next_va; 166 167 /** @offset: Offset into @pvr_obj to begin mapping from. */ 168 u64 offset; 169 170 /** @device_addr: Device-virtual address at the start of the mapping. */ 171 u64 device_addr; 172 173 /** @size: Size of the desired mapping. */ 174 u64 size; 175 }; 176 177 /** 178 * pvr_vm_bind_op_exec() - Execute a single bind op. 179 * @bind_op: Bind op context. 180 * 181 * Returns: 182 * * 0 on success, 183 * * Any error code returned by drm_gpuva_sm_map(), drm_gpuva_sm_unmap(), or 184 * a callback function. 185 */ 186 static int pvr_vm_bind_op_exec(struct pvr_vm_bind_op *bind_op) 187 { 188 switch (bind_op->type) { 189 case PVR_VM_BIND_TYPE_MAP: { 190 const struct drm_gpuvm_map_req map_req = { 191 .map.va.addr = bind_op->device_addr, 192 .map.va.range = bind_op->size, 193 .map.gem.obj = gem_from_pvr_gem(bind_op->pvr_obj), 194 .map.gem.offset = bind_op->offset, 195 }; 196 197 return drm_gpuvm_sm_map(&bind_op->vm_ctx->gpuvm_mgr, 198 bind_op, &map_req); 199 } 200 201 case PVR_VM_BIND_TYPE_UNMAP: 202 return drm_gpuvm_sm_unmap(&bind_op->vm_ctx->gpuvm_mgr, 203 bind_op, bind_op->device_addr, 204 bind_op->size); 205 } 206 207 /* 208 * This shouldn't happen unless something went wrong 209 * in drm_sched. 210 */ 211 WARN_ON(1); 212 return -EINVAL; 213 } 214 215 static void pvr_vm_bind_op_fini(struct pvr_vm_bind_op *bind_op) 216 { 217 drm_gpuvm_bo_put(bind_op->gpuvm_bo); 218 219 kfree(bind_op->new_va); 220 kfree(bind_op->prev_va); 221 kfree(bind_op->next_va); 222 223 if (bind_op->pvr_obj) 224 pvr_gem_object_put(bind_op->pvr_obj); 225 226 if (bind_op->mmu_op_ctx) 227 pvr_mmu_op_context_destroy(bind_op->mmu_op_ctx); 228 } 229 230 static int 231 pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op, 232 struct pvr_vm_context *vm_ctx, 233 struct pvr_gem_object *pvr_obj, u64 offset, 234 u64 device_addr, u64 size) 235 { 236 struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj); 237 const bool is_user = vm_ctx != vm_ctx->pvr_dev->kernel_vm_ctx; 238 const u64 pvr_obj_size = pvr_gem_object_size(pvr_obj); 239 struct sg_table *sgt; 240 u64 offset_plus_size; 241 int err; 242 243 if (check_add_overflow(offset, size, &offset_plus_size)) 244 return -EINVAL; 245 246 if (is_user && 247 !pvr_find_heap_containing(vm_ctx->pvr_dev, device_addr, size)) { 248 return -EINVAL; 249 } 250 251 if (!pvr_device_addr_and_size_are_valid(vm_ctx, device_addr, size) || 252 offset & ~PAGE_MASK || size & ~PAGE_MASK || 253 offset >= pvr_obj_size || offset_plus_size > pvr_obj_size) 254 return -EINVAL; 255 256 bind_op->type = PVR_VM_BIND_TYPE_MAP; 257 258 dma_resv_lock(obj->resv, NULL); 259 bind_op->gpuvm_bo = drm_gpuvm_bo_obtain_locked(&vm_ctx->gpuvm_mgr, obj); 260 dma_resv_unlock(obj->resv); 261 if (IS_ERR(bind_op->gpuvm_bo)) 262 return PTR_ERR(bind_op->gpuvm_bo); 263 264 bind_op->new_va = kzalloc_obj(*bind_op->new_va); 265 bind_op->prev_va = kzalloc_obj(*bind_op->prev_va); 266 bind_op->next_va = kzalloc_obj(*bind_op->next_va); 267 if (!bind_op->new_va || !bind_op->prev_va || !bind_op->next_va) { 268 err = -ENOMEM; 269 goto err_bind_op_fini; 270 } 271 272 /* Pin pages so they're ready for use. */ 273 sgt = pvr_gem_object_get_pages_sgt(pvr_obj); 274 err = PTR_ERR_OR_ZERO(sgt); 275 if (err) 276 goto err_bind_op_fini; 277 278 bind_op->mmu_op_ctx = 279 pvr_mmu_op_context_create(vm_ctx->mmu_ctx, sgt, offset, size); 280 err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx); 281 if (err) { 282 bind_op->mmu_op_ctx = NULL; 283 goto err_bind_op_fini; 284 } 285 286 bind_op->pvr_obj = pvr_obj; 287 bind_op->vm_ctx = vm_ctx; 288 bind_op->device_addr = device_addr; 289 bind_op->size = size; 290 bind_op->offset = offset; 291 292 return 0; 293 294 err_bind_op_fini: 295 pvr_vm_bind_op_fini(bind_op); 296 297 return err; 298 } 299 300 static int 301 pvr_vm_bind_op_unmap_init(struct pvr_vm_bind_op *bind_op, 302 struct pvr_vm_context *vm_ctx, 303 struct pvr_gem_object *pvr_obj, 304 u64 device_addr, u64 size) 305 { 306 int err; 307 308 if (!pvr_device_addr_and_size_are_valid(vm_ctx, device_addr, size)) 309 return -EINVAL; 310 311 bind_op->type = PVR_VM_BIND_TYPE_UNMAP; 312 313 bind_op->prev_va = kzalloc_obj(*bind_op->prev_va); 314 bind_op->next_va = kzalloc_obj(*bind_op->next_va); 315 if (!bind_op->prev_va || !bind_op->next_va) { 316 err = -ENOMEM; 317 goto err_bind_op_fini; 318 } 319 320 bind_op->mmu_op_ctx = 321 pvr_mmu_op_context_create(vm_ctx->mmu_ctx, NULL, 0, 0); 322 err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx); 323 if (err) { 324 bind_op->mmu_op_ctx = NULL; 325 goto err_bind_op_fini; 326 } 327 328 bind_op->pvr_obj = pvr_obj; 329 bind_op->vm_ctx = vm_ctx; 330 bind_op->device_addr = device_addr; 331 bind_op->size = size; 332 333 return 0; 334 335 err_bind_op_fini: 336 pvr_vm_bind_op_fini(bind_op); 337 338 return err; 339 } 340 341 /** 342 * pvr_vm_gpuva_map() - Insert a mapping into a memory context. 343 * @op: gpuva op containing the remap details. 344 * @op_ctx: Operation context. 345 * 346 * Context: Called by drm_gpuvm_sm_map following a successful mapping while 347 * @op_ctx.vm_ctx mutex is held. 348 * 349 * Return: 350 * * 0 on success, or 351 * * Any error returned by pvr_mmu_map(). 352 */ 353 static int 354 pvr_vm_gpuva_map(struct drm_gpuva_op *op, void *op_ctx) 355 { 356 struct pvr_gem_object *pvr_gem = gem_to_pvr_gem(op->map.gem.obj); 357 struct pvr_vm_bind_op *ctx = op_ctx; 358 int err; 359 360 if ((op->map.gem.offset | op->map.va.range) & ~PVR_DEVICE_PAGE_MASK) 361 return -EINVAL; 362 363 err = pvr_mmu_map(ctx->mmu_op_ctx, op->map.va.range, pvr_gem->flags, 364 op->map.va.addr); 365 if (err) 366 return err; 367 368 drm_gpuva_map(&ctx->vm_ctx->gpuvm_mgr, &ctx->new_va->base, &op->map); 369 drm_gpuva_link(&ctx->new_va->base, ctx->gpuvm_bo); 370 ctx->new_va = NULL; 371 372 return 0; 373 } 374 375 /** 376 * pvr_vm_gpuva_unmap() - Remove a mapping from a memory context. 377 * @op: gpuva op containing the unmap details. 378 * @op_ctx: Operation context. 379 * 380 * Context: Called by drm_gpuvm_sm_unmap following a successful unmapping while 381 * @op_ctx.vm_ctx mutex is held. 382 * 383 * Return: 384 * * 0 on success, or 385 * * Any error returned by pvr_mmu_unmap(). 386 */ 387 static int 388 pvr_vm_gpuva_unmap(struct drm_gpuva_op *op, void *op_ctx) 389 { 390 struct pvr_vm_bind_op *ctx = op_ctx; 391 392 int err = pvr_mmu_unmap(ctx->mmu_op_ctx, op->unmap.va->va.addr, 393 op->unmap.va->va.range); 394 395 if (err) 396 return err; 397 398 drm_gpuva_unmap(&op->unmap); 399 drm_gpuva_unlink(op->unmap.va); 400 kfree(to_pvr_vm_gpuva(op->unmap.va)); 401 402 return 0; 403 } 404 405 /** 406 * pvr_vm_gpuva_remap() - Remap a mapping within a memory context. 407 * @op: gpuva op containing the remap details. 408 * @op_ctx: Operation context. 409 * 410 * Context: Called by either drm_gpuvm_sm_map or drm_gpuvm_sm_unmap when a 411 * mapping or unmapping operation causes a region to be split. The 412 * @op_ctx.vm_ctx mutex is held. 413 * 414 * Return: 415 * * 0 on success, or 416 * * Any error returned by pvr_vm_gpuva_unmap() or pvr_vm_gpuva_unmap(). 417 */ 418 static int 419 pvr_vm_gpuva_remap(struct drm_gpuva_op *op, void *op_ctx) 420 { 421 struct pvr_vm_bind_op *ctx = op_ctx; 422 u64 va_start = 0, va_range = 0; 423 int err; 424 425 drm_gpuva_op_remap_to_unmap_range(&op->remap, &va_start, &va_range); 426 err = pvr_mmu_unmap(ctx->mmu_op_ctx, va_start, va_range); 427 if (err) 428 return err; 429 430 /* No actual remap required: the page table tree depth is fixed to 3, 431 * and we use 4k page table entries only for now. 432 */ 433 drm_gpuva_remap(&ctx->prev_va->base, &ctx->next_va->base, &op->remap); 434 435 if (op->remap.prev) { 436 pvr_gem_object_get(gem_to_pvr_gem(ctx->prev_va->base.gem.obj)); 437 drm_gpuva_link(&ctx->prev_va->base, ctx->gpuvm_bo); 438 ctx->prev_va = NULL; 439 } 440 441 if (op->remap.next) { 442 pvr_gem_object_get(gem_to_pvr_gem(ctx->next_va->base.gem.obj)); 443 drm_gpuva_link(&ctx->next_va->base, ctx->gpuvm_bo); 444 ctx->next_va = NULL; 445 } 446 447 drm_gpuva_unlink(op->remap.unmap->va); 448 kfree(to_pvr_vm_gpuva(op->remap.unmap->va)); 449 450 return 0; 451 } 452 453 /* 454 * Public API 455 * 456 * For an overview of these functions, see *DOC: Public API* in "pvr_vm.h". 457 */ 458 459 /** 460 * pvr_device_addr_is_valid() - Tests whether a device-virtual address 461 * is valid. 462 * @device_addr: Virtual device address to test. 463 * 464 * Return: 465 * * %true if @device_addr is within the valid range for a device page 466 * table and is aligned to the device page size, or 467 * * %false otherwise. 468 */ 469 bool 470 pvr_device_addr_is_valid(u64 device_addr) 471 { 472 return (device_addr & ~PVR_PAGE_TABLE_ADDR_MASK) == 0 && 473 (device_addr & ~PVR_DEVICE_PAGE_MASK) == 0; 474 } 475 476 /** 477 * pvr_device_addr_and_size_are_valid() - Tests whether a device-virtual 478 * address and associated size are both valid. 479 * @vm_ctx: Target VM context. 480 * @device_addr: Virtual device address to test. 481 * @size: Size of the range based at @device_addr to test. 482 * 483 * Calling pvr_device_addr_is_valid() twice (once on @size, and again on 484 * @device_addr + @size) to verify a device-virtual address range initially 485 * seems intuitive, but it produces a false-negative when the address range 486 * is right at the end of device-virtual address space. 487 * 488 * This function catches that corner case, as well as checking that 489 * @size is non-zero. 490 * 491 * Return: 492 * * %true if @device_addr is device page aligned; @size is device page 493 * aligned; the range specified by @device_addr and @size is within the 494 * bounds of the device-virtual address space, and @size is non-zero, or 495 * * %false otherwise. 496 */ 497 bool 498 pvr_device_addr_and_size_are_valid(struct pvr_vm_context *vm_ctx, 499 u64 device_addr, u64 size) 500 { 501 return pvr_device_addr_is_valid(device_addr) && 502 drm_gpuvm_range_valid(&vm_ctx->gpuvm_mgr, device_addr, size) && 503 size != 0 && (size & ~PVR_DEVICE_PAGE_MASK) == 0 && 504 (device_addr + size <= PVR_PAGE_TABLE_ADDR_SPACE_SIZE); 505 } 506 507 static void pvr_gpuvm_free(struct drm_gpuvm *gpuvm) 508 { 509 kfree(to_pvr_vm_context(gpuvm)); 510 } 511 512 static const struct drm_gpuvm_ops pvr_vm_gpuva_ops = { 513 .vm_free = pvr_gpuvm_free, 514 .sm_step_map = pvr_vm_gpuva_map, 515 .sm_step_remap = pvr_vm_gpuva_remap, 516 .sm_step_unmap = pvr_vm_gpuva_unmap, 517 }; 518 519 static void 520 fw_mem_context_init(void *cpu_ptr, void *priv) 521 { 522 struct rogue_fwif_fwmemcontext *fw_mem_ctx = cpu_ptr; 523 struct pvr_vm_context *vm_ctx = priv; 524 525 fw_mem_ctx->pc_dev_paddr = pvr_vm_get_page_table_root_addr(vm_ctx); 526 fw_mem_ctx->page_cat_base_reg_set = ROGUE_FW_BIF_INVALID_PCSET; 527 } 528 529 /** 530 * pvr_vm_create_context() - Create a new VM context. 531 * @pvr_dev: Target PowerVR device. 532 * @is_userspace_context: %true if this context is for userspace. This will 533 * create a firmware memory context for the VM context 534 * and disable warnings when tearing down mappings. 535 * 536 * Return: 537 * * A handle to the newly-minted VM context on success, 538 * * -%EINVAL if the feature "virtual address space bits" on @pvr_dev is 539 * missing or has an unsupported value, 540 * * -%ENOMEM if allocation of the structure behind the opaque handle fails, 541 * or 542 * * Any error encountered while setting up internal structures. 543 */ 544 struct pvr_vm_context * 545 pvr_vm_create_context(struct pvr_device *pvr_dev, bool is_userspace_context) 546 { 547 struct drm_device *drm_dev = from_pvr_device(pvr_dev); 548 549 struct pvr_vm_context *vm_ctx; 550 u16 device_addr_bits; 551 552 int err; 553 554 err = PVR_FEATURE_VALUE(pvr_dev, virtual_address_space_bits, 555 &device_addr_bits); 556 if (err) { 557 drm_err(drm_dev, 558 "Failed to get device virtual address space bits\n"); 559 return ERR_PTR(err); 560 } 561 562 if (device_addr_bits != PVR_PAGE_TABLE_ADDR_BITS) { 563 drm_err(drm_dev, 564 "Device has unsupported virtual address space size\n"); 565 return ERR_PTR(-EINVAL); 566 } 567 568 vm_ctx = kzalloc_obj(*vm_ctx); 569 if (!vm_ctx) 570 return ERR_PTR(-ENOMEM); 571 572 vm_ctx->pvr_dev = pvr_dev; 573 574 vm_ctx->mmu_ctx = pvr_mmu_context_create(pvr_dev); 575 err = PTR_ERR_OR_ZERO(vm_ctx->mmu_ctx); 576 if (err) 577 goto err_free; 578 579 if (is_userspace_context) { 580 err = pvr_fw_object_create(pvr_dev, sizeof(struct rogue_fwif_fwmemcontext), 581 PVR_BO_FW_FLAGS_DEVICE_UNCACHED, 582 fw_mem_context_init, vm_ctx, &vm_ctx->fw_mem_ctx_obj); 583 584 if (err) 585 goto err_page_table_destroy; 586 } 587 588 drm_gem_private_object_init(&pvr_dev->base, &vm_ctx->dummy_gem, 0); 589 drm_gpuvm_init(&vm_ctx->gpuvm_mgr, 590 is_userspace_context ? "PowerVR-user-VM" : "PowerVR-FW-VM", 591 0, &pvr_dev->base, &vm_ctx->dummy_gem, 592 0, 1ULL << device_addr_bits, 0, 0, &pvr_vm_gpuva_ops); 593 594 mutex_init(&vm_ctx->lock); 595 kref_init(&vm_ctx->ref_count); 596 597 return vm_ctx; 598 599 err_page_table_destroy: 600 pvr_mmu_context_destroy(vm_ctx->mmu_ctx); 601 602 err_free: 603 kfree(vm_ctx); 604 605 return ERR_PTR(err); 606 } 607 608 /** 609 * pvr_vm_context_release() - Teardown a VM context. 610 * @ref_count: Pointer to reference counter of the VM context. 611 * 612 * This function also ensures that no mappings are left dangling by calling 613 * pvr_vm_unmap_all. 614 */ 615 static void 616 pvr_vm_context_release(struct kref *ref_count) 617 { 618 struct pvr_vm_context *vm_ctx = 619 container_of(ref_count, struct pvr_vm_context, ref_count); 620 621 if (vm_ctx->fw_mem_ctx_obj) 622 pvr_fw_object_destroy(vm_ctx->fw_mem_ctx_obj); 623 624 pvr_vm_unmap_all(vm_ctx); 625 626 pvr_mmu_context_destroy(vm_ctx->mmu_ctx); 627 drm_gem_private_object_fini(&vm_ctx->dummy_gem); 628 mutex_destroy(&vm_ctx->lock); 629 630 drm_gpuvm_put(&vm_ctx->gpuvm_mgr); 631 } 632 633 /** 634 * pvr_vm_context_lookup() - Look up VM context from handle 635 * @pvr_file: Pointer to pvr_file structure. 636 * @handle: Object handle. 637 * 638 * Takes reference on VM context object. Call pvr_vm_context_put() to release. 639 * 640 * Returns: 641 * * The requested object on success, or 642 * * %NULL on failure (object does not exist in list, or is not a VM context) 643 */ 644 struct pvr_vm_context * 645 pvr_vm_context_lookup(struct pvr_file *pvr_file, u32 handle) 646 { 647 struct pvr_vm_context *vm_ctx; 648 649 xa_lock(&pvr_file->vm_ctx_handles); 650 vm_ctx = xa_load(&pvr_file->vm_ctx_handles, handle); 651 pvr_vm_context_get(vm_ctx); 652 xa_unlock(&pvr_file->vm_ctx_handles); 653 654 return vm_ctx; 655 } 656 657 /** 658 * pvr_vm_context_put() - Release a reference on a VM context 659 * @vm_ctx: Target VM context. 660 * 661 * Returns: 662 * * %true if the VM context was destroyed, or 663 * * %false if there are any references still remaining. 664 */ 665 bool 666 pvr_vm_context_put(struct pvr_vm_context *vm_ctx) 667 { 668 if (vm_ctx) 669 return kref_put(&vm_ctx->ref_count, pvr_vm_context_release); 670 671 return true; 672 } 673 674 /** 675 * pvr_destroy_vm_contexts_for_file: Destroy any VM contexts associated with the 676 * given file. 677 * @pvr_file: Pointer to pvr_file structure. 678 * 679 * Removes all vm_contexts associated with @pvr_file from the device VM context 680 * list and drops initial references. vm_contexts will then be destroyed once 681 * all outstanding references are dropped. 682 */ 683 void pvr_destroy_vm_contexts_for_file(struct pvr_file *pvr_file) 684 { 685 struct pvr_vm_context *vm_ctx; 686 unsigned long handle; 687 688 xa_for_each(&pvr_file->vm_ctx_handles, handle, vm_ctx) { 689 /* vm_ctx is not used here because that would create a race with xa_erase */ 690 pvr_vm_context_put(xa_erase(&pvr_file->vm_ctx_handles, handle)); 691 } 692 } 693 694 static int 695 pvr_vm_lock_extra(struct drm_gpuvm_exec *vm_exec) 696 { 697 struct pvr_vm_bind_op *bind_op = vm_exec->extra.priv; 698 struct pvr_gem_object *pvr_obj = bind_op->pvr_obj; 699 700 /* Acquire lock on the GEM object being mapped/unmapped. */ 701 return drm_exec_lock_obj(&vm_exec->exec, gem_from_pvr_gem(pvr_obj)); 702 } 703 704 /** 705 * pvr_vm_map() - Map a section of physical memory into a section of 706 * device-virtual memory. 707 * @vm_ctx: Target VM context. 708 * @pvr_obj: Target PowerVR memory object. 709 * @pvr_obj_offset: Offset into @pvr_obj to map from. 710 * @device_addr: Virtual device address at the start of the requested mapping. 711 * @size: Size of the requested mapping. 712 * 713 * No handle is returned to represent the mapping. Instead, callers should 714 * remember @device_addr and use that as a handle. 715 * 716 * Return: 717 * * 0 on success, 718 * * -%EINVAL if @device_addr is not a valid page-aligned device-virtual 719 * address; the region specified by @pvr_obj_offset and @size does not fall 720 * entirely within @pvr_obj, or any part of the specified region of @pvr_obj 721 * is not device-virtual page-aligned, 722 * * Any error encountered while performing internal operations required to 723 * destroy the mapping (returned from pvr_vm_gpuva_map or 724 * pvr_vm_gpuva_remap). 725 */ 726 int 727 pvr_vm_map(struct pvr_vm_context *vm_ctx, struct pvr_gem_object *pvr_obj, 728 u64 pvr_obj_offset, u64 device_addr, u64 size) 729 { 730 struct pvr_vm_bind_op bind_op = {0}; 731 struct drm_gpuvm_exec vm_exec = { 732 .vm = &vm_ctx->gpuvm_mgr, 733 .flags = DRM_EXEC_INTERRUPTIBLE_WAIT | 734 DRM_EXEC_IGNORE_DUPLICATES, 735 .extra = { 736 .fn = pvr_vm_lock_extra, 737 .priv = &bind_op, 738 }, 739 }; 740 741 int err = pvr_vm_bind_op_map_init(&bind_op, vm_ctx, pvr_obj, 742 pvr_obj_offset, device_addr, 743 size); 744 745 if (err) 746 return err; 747 748 pvr_gem_object_get(pvr_obj); 749 750 mutex_lock(&vm_ctx->lock); 751 err = drm_gpuvm_exec_lock(&vm_exec); 752 if (err) 753 goto err_cleanup; 754 755 err = pvr_vm_bind_op_exec(&bind_op); 756 757 drm_gpuvm_exec_unlock(&vm_exec); 758 759 err_cleanup: 760 mutex_unlock(&vm_ctx->lock); 761 pvr_vm_bind_op_fini(&bind_op); 762 763 return err; 764 } 765 766 /** 767 * pvr_vm_unmap_obj_locked() - Unmap an already mapped section of device-virtual 768 * memory. 769 * @vm_ctx: Target VM context. 770 * @pvr_obj: Target PowerVR memory object. 771 * @device_addr: Virtual device address at the start of the target mapping. 772 * @size: Size of the target mapping. 773 * 774 * Return: 775 * * 0 on success, 776 * * -%EINVAL if @device_addr is not a valid page-aligned device-virtual 777 * address, 778 * * Any error encountered while performing internal operations required to 779 * destroy the mapping (returned from pvr_vm_gpuva_unmap or 780 * pvr_vm_gpuva_remap). 781 * 782 * The vm_ctx->lock must be held when calling this function. 783 */ 784 static int 785 pvr_vm_unmap_obj_locked(struct pvr_vm_context *vm_ctx, 786 struct pvr_gem_object *pvr_obj, 787 u64 device_addr, u64 size) 788 { 789 struct pvr_vm_bind_op bind_op = {0}; 790 struct drm_gpuvm_exec vm_exec = { 791 .vm = &vm_ctx->gpuvm_mgr, 792 .flags = DRM_EXEC_INTERRUPTIBLE_WAIT | 793 DRM_EXEC_IGNORE_DUPLICATES, 794 .extra = { 795 .fn = pvr_vm_lock_extra, 796 .priv = &bind_op, 797 }, 798 }; 799 800 int err = pvr_vm_bind_op_unmap_init(&bind_op, vm_ctx, pvr_obj, 801 device_addr, size); 802 if (err) 803 return err; 804 805 pvr_gem_object_get(pvr_obj); 806 807 err = drm_gpuvm_exec_lock(&vm_exec); 808 if (err) 809 goto err_cleanup; 810 811 err = pvr_vm_bind_op_exec(&bind_op); 812 813 drm_gpuvm_exec_unlock(&vm_exec); 814 815 err_cleanup: 816 pvr_vm_bind_op_fini(&bind_op); 817 818 return err; 819 } 820 821 /** 822 * pvr_vm_unmap_obj() - Unmap an already mapped section of device-virtual 823 * memory. 824 * @vm_ctx: Target VM context. 825 * @pvr_obj: Target PowerVR memory object. 826 * @device_addr: Virtual device address at the start of the target mapping. 827 * @size: Size of the target mapping. 828 * 829 * Return: 830 * * 0 on success, 831 * * Any error encountered by pvr_vm_unmap_obj_locked. 832 */ 833 int 834 pvr_vm_unmap_obj(struct pvr_vm_context *vm_ctx, struct pvr_gem_object *pvr_obj, 835 u64 device_addr, u64 size) 836 { 837 int err; 838 839 mutex_lock(&vm_ctx->lock); 840 err = pvr_vm_unmap_obj_locked(vm_ctx, pvr_obj, device_addr, size); 841 mutex_unlock(&vm_ctx->lock); 842 843 return err; 844 } 845 846 /** 847 * pvr_vm_unmap() - Unmap an already mapped section of device-virtual memory. 848 * @vm_ctx: Target VM context. 849 * @device_addr: Virtual device address at the start of the target mapping. 850 * @size: Size of the target mapping. 851 * 852 * Return: 853 * * 0 on success, 854 * * Any error encountered by drm_gpuva_find, 855 * * Any error encountered by pvr_vm_unmap_obj_locked. 856 */ 857 int 858 pvr_vm_unmap(struct pvr_vm_context *vm_ctx, u64 device_addr, u64 size) 859 { 860 struct pvr_gem_object *pvr_obj; 861 struct drm_gpuva *va; 862 int err; 863 864 mutex_lock(&vm_ctx->lock); 865 866 va = drm_gpuva_find(&vm_ctx->gpuvm_mgr, device_addr, size); 867 if (va) { 868 pvr_obj = gem_to_pvr_gem(va->gem.obj); 869 err = pvr_vm_unmap_obj_locked(vm_ctx, pvr_obj, 870 va->va.addr, va->va.range); 871 } else { 872 err = -ENOENT; 873 } 874 875 mutex_unlock(&vm_ctx->lock); 876 877 return err; 878 } 879 880 /** 881 * pvr_vm_unmap_all() - Unmap all mappings associated with a VM context. 882 * @vm_ctx: Target VM context. 883 * 884 * This function ensures that no mappings are left dangling by unmapping them 885 * all in order of ascending device-virtual address. 886 */ 887 void 888 pvr_vm_unmap_all(struct pvr_vm_context *vm_ctx) 889 { 890 mutex_lock(&vm_ctx->lock); 891 892 for (;;) { 893 struct pvr_gem_object *pvr_obj; 894 struct drm_gpuva *va; 895 896 va = drm_gpuva_find_first(&vm_ctx->gpuvm_mgr, 897 vm_ctx->gpuvm_mgr.mm_start, 898 vm_ctx->gpuvm_mgr.mm_range); 899 if (!va) 900 break; 901 902 pvr_obj = gem_to_pvr_gem(va->gem.obj); 903 904 WARN_ON(pvr_vm_unmap_obj_locked(vm_ctx, pvr_obj, 905 va->va.addr, va->va.range)); 906 } 907 908 mutex_unlock(&vm_ctx->lock); 909 } 910 911 /* Static data areas are determined by firmware. */ 912 static const struct drm_pvr_static_data_area static_data_areas[] = { 913 { 914 .area_usage = DRM_PVR_STATIC_DATA_AREA_FENCE, 915 .location_heap_id = DRM_PVR_HEAP_GENERAL, 916 .offset = 0, 917 .size = 128, 918 }, 919 { 920 .area_usage = DRM_PVR_STATIC_DATA_AREA_YUV_CSC, 921 .location_heap_id = DRM_PVR_HEAP_GENERAL, 922 .offset = 128, 923 .size = 1024, 924 }, 925 { 926 .area_usage = DRM_PVR_STATIC_DATA_AREA_VDM_SYNC, 927 .location_heap_id = DRM_PVR_HEAP_PDS_CODE_DATA, 928 .offset = 0, 929 .size = 128, 930 }, 931 { 932 .area_usage = DRM_PVR_STATIC_DATA_AREA_EOT, 933 .location_heap_id = DRM_PVR_HEAP_PDS_CODE_DATA, 934 .offset = 128, 935 .size = 128, 936 }, 937 { 938 .area_usage = DRM_PVR_STATIC_DATA_AREA_VDM_SYNC, 939 .location_heap_id = DRM_PVR_HEAP_USC_CODE, 940 .offset = 0, 941 .size = 128, 942 }, 943 }; 944 945 #define GET_RESERVED_SIZE(last_offset, last_size) round_up((last_offset) + (last_size), PAGE_SIZE) 946 947 /* 948 * The values given to GET_RESERVED_SIZE() are taken from the last entry in the corresponding 949 * static data area for each heap. 950 */ 951 static const struct drm_pvr_heap pvr_heaps[] = { 952 [DRM_PVR_HEAP_GENERAL] = { 953 .base = ROGUE_GENERAL_HEAP_BASE, 954 .size = ROGUE_GENERAL_HEAP_SIZE, 955 .flags = 0, 956 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 957 }, 958 [DRM_PVR_HEAP_PDS_CODE_DATA] = { 959 .base = ROGUE_PDSCODEDATA_HEAP_BASE, 960 .size = ROGUE_PDSCODEDATA_HEAP_SIZE, 961 .flags = 0, 962 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 963 }, 964 [DRM_PVR_HEAP_USC_CODE] = { 965 .base = ROGUE_USCCODE_HEAP_BASE, 966 .size = ROGUE_USCCODE_HEAP_SIZE, 967 .flags = 0, 968 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 969 }, 970 [DRM_PVR_HEAP_RGNHDR] = { 971 .base = ROGUE_RGNHDR_HEAP_BASE, 972 .size = ROGUE_RGNHDR_HEAP_SIZE, 973 .flags = 0, 974 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 975 }, 976 [DRM_PVR_HEAP_VIS_TEST] = { 977 .base = ROGUE_VISTEST_HEAP_BASE, 978 .size = ROGUE_VISTEST_HEAP_SIZE, 979 .flags = 0, 980 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 981 }, 982 [DRM_PVR_HEAP_TRANSFER_FRAG] = { 983 .base = ROGUE_TRANSFER_FRAG_HEAP_BASE, 984 .size = ROGUE_TRANSFER_FRAG_HEAP_SIZE, 985 .flags = 0, 986 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 987 }, 988 }; 989 990 int 991 pvr_static_data_areas_get(const struct pvr_device *pvr_dev, 992 struct drm_pvr_ioctl_dev_query_args *args) 993 { 994 struct drm_pvr_dev_query_static_data_areas query = {0}; 995 int err; 996 997 if (!args->pointer) { 998 args->size = sizeof(struct drm_pvr_dev_query_static_data_areas); 999 return 0; 1000 } 1001 1002 err = PVR_UOBJ_GET(query, args->size, args->pointer); 1003 if (err < 0) 1004 return err; 1005 1006 if (!query.static_data_areas.array) { 1007 query.static_data_areas.count = ARRAY_SIZE(static_data_areas); 1008 query.static_data_areas.stride = sizeof(struct drm_pvr_static_data_area); 1009 goto copy_out; 1010 } 1011 1012 if (query.static_data_areas.count > ARRAY_SIZE(static_data_areas)) 1013 query.static_data_areas.count = ARRAY_SIZE(static_data_areas); 1014 1015 err = PVR_UOBJ_SET_ARRAY(&query.static_data_areas, static_data_areas); 1016 if (err < 0) 1017 return err; 1018 1019 copy_out: 1020 err = PVR_UOBJ_SET(args->pointer, args->size, query); 1021 if (err < 0) 1022 return err; 1023 1024 if (args->size > sizeof(query)) 1025 args->size = sizeof(query); 1026 return 0; 1027 } 1028 1029 int 1030 pvr_heap_info_get(const struct pvr_device *pvr_dev, 1031 struct drm_pvr_ioctl_dev_query_args *args) 1032 { 1033 struct drm_pvr_dev_query_heap_info query = {0}; 1034 u64 dest; 1035 int err; 1036 1037 if (!args->pointer) { 1038 args->size = sizeof(struct drm_pvr_dev_query_heap_info); 1039 return 0; 1040 } 1041 1042 err = PVR_UOBJ_GET(query, args->size, args->pointer); 1043 if (err < 0) 1044 return err; 1045 1046 if (!query.heaps.array) { 1047 query.heaps.count = ARRAY_SIZE(pvr_heaps); 1048 query.heaps.stride = sizeof(struct drm_pvr_heap); 1049 goto copy_out; 1050 } 1051 1052 if (query.heaps.count > ARRAY_SIZE(pvr_heaps)) 1053 query.heaps.count = ARRAY_SIZE(pvr_heaps); 1054 1055 /* Region header heap is only present if BRN63142 is present. */ 1056 dest = query.heaps.array; 1057 for (size_t i = 0; i < query.heaps.count; i++) { 1058 struct drm_pvr_heap heap = pvr_heaps[i]; 1059 1060 if (i == DRM_PVR_HEAP_RGNHDR && !PVR_HAS_QUIRK(pvr_dev, 63142)) 1061 heap.size = 0; 1062 1063 err = PVR_UOBJ_SET(dest, query.heaps.stride, heap); 1064 if (err < 0) 1065 return err; 1066 1067 dest += query.heaps.stride; 1068 } 1069 1070 copy_out: 1071 err = PVR_UOBJ_SET(args->pointer, args->size, query); 1072 if (err < 0) 1073 return err; 1074 1075 if (args->size > sizeof(query)) 1076 args->size = sizeof(query); 1077 return 0; 1078 } 1079 1080 /** 1081 * pvr_heap_contains_range() - Determine if a given heap contains the specified 1082 * device-virtual address range. 1083 * @pvr_heap: Target heap. 1084 * @start: Inclusive start of the target range. 1085 * @end: Inclusive end of the target range. 1086 * 1087 * It is an error to call this function with values of @start and @end that do 1088 * not satisfy the condition @start <= @end. 1089 */ 1090 static __always_inline bool 1091 pvr_heap_contains_range(const struct drm_pvr_heap *pvr_heap, u64 start, u64 end) 1092 { 1093 return pvr_heap->base <= start && end < pvr_heap->base + pvr_heap->size; 1094 } 1095 1096 /** 1097 * pvr_find_heap_containing() - Find a heap which contains the specified 1098 * device-virtual address range. 1099 * @pvr_dev: Target PowerVR device. 1100 * @start: Start of the target range. 1101 * @size: Size of the target range. 1102 * 1103 * Return: 1104 * * A pointer to a constant instance of struct drm_pvr_heap representing the 1105 * heap containing the entire range specified by @start and @size on 1106 * success, or 1107 * * %NULL if no such heap exists. 1108 */ 1109 const struct drm_pvr_heap * 1110 pvr_find_heap_containing(struct pvr_device *pvr_dev, u64 start, u64 size) 1111 { 1112 u64 end; 1113 1114 if (check_add_overflow(start, size - 1, &end)) 1115 return NULL; 1116 1117 /* 1118 * There are no guarantees about the order of address ranges in 1119 * &pvr_heaps, so iterate over the entire array for a heap whose 1120 * range completely encompasses the given range. 1121 */ 1122 for (u32 heap_id = 0; heap_id < ARRAY_SIZE(pvr_heaps); heap_id++) { 1123 /* Filter heaps that present only with an associated quirk */ 1124 if (heap_id == DRM_PVR_HEAP_RGNHDR && 1125 !PVR_HAS_QUIRK(pvr_dev, 63142)) { 1126 continue; 1127 } 1128 1129 if (pvr_heap_contains_range(&pvr_heaps[heap_id], start, end)) 1130 return &pvr_heaps[heap_id]; 1131 } 1132 1133 return NULL; 1134 } 1135 1136 /** 1137 * pvr_vm_find_gem_object() - Look up a buffer object from a given 1138 * device-virtual address. 1139 * @vm_ctx: [IN] Target VM context. 1140 * @device_addr: [IN] Virtual device address at the start of the required 1141 * object. 1142 * @mapped_offset_out: [OUT] Pointer to location to write offset of the start 1143 * of the mapped region within the buffer object. May be 1144 * %NULL if this information is not required. 1145 * @mapped_size_out: [OUT] Pointer to location to write size of the mapped 1146 * region. May be %NULL if this information is not required. 1147 * 1148 * If successful, a reference will be taken on the buffer object. The caller 1149 * must drop the reference with pvr_gem_object_put(). 1150 * 1151 * Return: 1152 * * The PowerVR buffer object mapped at @device_addr if one exists, or 1153 * * %NULL otherwise. 1154 */ 1155 struct pvr_gem_object * 1156 pvr_vm_find_gem_object(struct pvr_vm_context *vm_ctx, u64 device_addr, 1157 u64 *mapped_offset_out, u64 *mapped_size_out) 1158 { 1159 struct pvr_gem_object *pvr_obj; 1160 struct drm_gpuva *va; 1161 1162 mutex_lock(&vm_ctx->lock); 1163 1164 va = drm_gpuva_find_first(&vm_ctx->gpuvm_mgr, device_addr, 1); 1165 if (!va) 1166 goto err_unlock; 1167 1168 pvr_obj = gem_to_pvr_gem(va->gem.obj); 1169 pvr_gem_object_get(pvr_obj); 1170 1171 if (mapped_offset_out) 1172 *mapped_offset_out = va->gem.offset; 1173 if (mapped_size_out) 1174 *mapped_size_out = va->va.range; 1175 1176 mutex_unlock(&vm_ctx->lock); 1177 1178 return pvr_obj; 1179 1180 err_unlock: 1181 mutex_unlock(&vm_ctx->lock); 1182 1183 return NULL; 1184 } 1185 1186 /** 1187 * pvr_vm_get_fw_mem_context: Get object representing firmware memory context 1188 * @vm_ctx: Target VM context. 1189 * 1190 * Returns: 1191 * * FW object representing firmware memory context, or 1192 * * %NULL if this VM context does not have a firmware memory context. 1193 */ 1194 struct pvr_fw_object * 1195 pvr_vm_get_fw_mem_context(struct pvr_vm_context *vm_ctx) 1196 { 1197 return vm_ctx->fw_mem_ctx_obj; 1198 } 1199