1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright (c) 2023 Imagination Technologies Ltd. */
6 #include "pvr_device.h"
10 #include "pvr_rogue_fwif.h"
11 #include "pvr_rogue_heap_config.h"
13 #include <drm/drm_exec.h>
14 #include <drm/drm_gem.h>
15 #include <drm/drm_gpuvm.h>
17 #include <linux/container_of.h>
18 #include <linux/err.h>
19 #include <linux/errno.h>
20 #include <linux/gfp_types.h>
21 #include <linux/kref.h>
22 #include <linux/mutex.h>
23 #include <linux/stddef.h>
28 * This is the "top level" datatype in the VM code. It's exposed in the public
29 * API as an opaque handle.
33 * struct pvr_vm_context - Context type used to represent a single VM.
35 struct pvr_vm_context {
37 * @pvr_dev: The PowerVR device to which this context is bound.
38 * This binding is immutable for the life of the context.
40 struct pvr_device *pvr_dev;
42 /** @mmu_ctx: The context for binding to physical memory. */
43 struct pvr_mmu_context *mmu_ctx;
45 /** @gpuvm_mgr: GPUVM object associated with this context. */
46 struct drm_gpuvm gpuvm_mgr;
48 /** @lock: Global lock on this VM. */
52 * @fw_mem_ctx_obj: Firmware object representing firmware memory
55 struct pvr_fw_object *fw_mem_ctx_obj;
57 /** @ref_count: Reference count of object. */
58 struct kref ref_count;
61 * @dummy_gem: GEM object to enable VM reservation. All private BOs
62 * should use the @dummy_gem.resv and not their own _resv field.
64 struct drm_gem_object dummy_gem;
68 struct pvr_vm_context *to_pvr_vm_context(struct drm_gpuvm *gpuvm)
70 return container_of(gpuvm, struct pvr_vm_context, gpuvm_mgr);
73 struct pvr_vm_context *pvr_vm_context_get(struct pvr_vm_context *vm_ctx)
76 kref_get(&vm_ctx->ref_count);
82 * pvr_vm_get_page_table_root_addr() - Get the DMA address of the root of the
83 * page table structure behind a VM context.
84 * @vm_ctx: Target VM context.
86 dma_addr_t pvr_vm_get_page_table_root_addr(struct pvr_vm_context *vm_ctx)
88 return pvr_mmu_get_root_table_dma_addr(vm_ctx->mmu_ctx);
92 * pvr_vm_get_dma_resv() - Expose the dma_resv owned by the VM context.
93 * @vm_ctx: Target VM context.
95 * This is used to allow private BOs to share a dma_resv for faster fence
98 * Returns: The dma_resv pointer.
100 struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx)
102 return vm_ctx->dummy_gem.resv;
106 * DOC: Memory mappings
110 * struct pvr_vm_gpuva - Wrapper type representing a single VM mapping.
112 struct pvr_vm_gpuva {
113 /** @base: The wrapped drm_gpuva object. */
114 struct drm_gpuva base;
117 #define to_pvr_vm_gpuva(va) container_of_const(va, struct pvr_vm_gpuva, base)
119 enum pvr_vm_bind_type {
120 PVR_VM_BIND_TYPE_MAP,
121 PVR_VM_BIND_TYPE_UNMAP,
125 * struct pvr_vm_bind_op - Context of a map/unmap operation.
127 struct pvr_vm_bind_op {
128 /** @type: Map or unmap. */
129 enum pvr_vm_bind_type type;
131 /** @pvr_obj: Object associated with mapping (map only). */
132 struct pvr_gem_object *pvr_obj;
135 * @vm_ctx: VM context where the mapping will be created or destroyed.
137 struct pvr_vm_context *vm_ctx;
139 /** @mmu_op_ctx: MMU op context. */
140 struct pvr_mmu_op_context *mmu_op_ctx;
142 /** @gpuvm_bo: Prealloced wrapped BO for attaching to the gpuvm. */
143 struct drm_gpuvm_bo *gpuvm_bo;
146 * @new_va: Prealloced VA mapping object (init in callback).
147 * Used when creating a mapping.
149 struct pvr_vm_gpuva *new_va;
152 * @prev_va: Prealloced VA mapping object (init in callback).
153 * Used when a mapping or unmapping operation overlaps an existing
154 * mapping and splits away the beginning into a new mapping.
156 struct pvr_vm_gpuva *prev_va;
159 * @next_va: Prealloced VA mapping object (init in callback).
160 * Used when a mapping or unmapping operation overlaps an existing
161 * mapping and splits away the end into a new mapping.
163 struct pvr_vm_gpuva *next_va;
165 /** @offset: Offset into @pvr_obj to begin mapping from. */
168 /** @device_addr: Device-virtual address at the start of the mapping. */
171 /** @size: Size of the desired mapping. */
176 * pvr_vm_bind_op_exec() - Execute a single bind op.
177 * @bind_op: Bind op context.
181 * * Any error code returned by drm_gpuva_sm_map(), drm_gpuva_sm_unmap(), or
182 * a callback function.
184 static int pvr_vm_bind_op_exec(struct pvr_vm_bind_op *bind_op)
186 switch (bind_op->type) {
187 case PVR_VM_BIND_TYPE_MAP:
188 return drm_gpuvm_sm_map(&bind_op->vm_ctx->gpuvm_mgr,
189 bind_op, bind_op->device_addr,
191 gem_from_pvr_gem(bind_op->pvr_obj),
194 case PVR_VM_BIND_TYPE_UNMAP:
195 return drm_gpuvm_sm_unmap(&bind_op->vm_ctx->gpuvm_mgr,
196 bind_op, bind_op->device_addr,
201 * This shouldn't happen unless something went wrong
208 static void pvr_vm_bind_op_fini(struct pvr_vm_bind_op *bind_op)
210 drm_gpuvm_bo_put(bind_op->gpuvm_bo);
212 kfree(bind_op->new_va);
213 kfree(bind_op->prev_va);
214 kfree(bind_op->next_va);
216 if (bind_op->pvr_obj)
217 pvr_gem_object_put(bind_op->pvr_obj);
219 if (bind_op->mmu_op_ctx)
220 pvr_mmu_op_context_destroy(bind_op->mmu_op_ctx);
224 pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op,
225 struct pvr_vm_context *vm_ctx,
226 struct pvr_gem_object *pvr_obj, u64 offset,
227 u64 device_addr, u64 size)
229 struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj);
230 const bool is_user = vm_ctx != vm_ctx->pvr_dev->kernel_vm_ctx;
231 const u64 pvr_obj_size = pvr_gem_object_size(pvr_obj);
232 struct sg_table *sgt;
233 u64 offset_plus_size;
236 if (check_add_overflow(offset, size, &offset_plus_size))
240 !pvr_find_heap_containing(vm_ctx->pvr_dev, device_addr, size)) {
244 if (!pvr_device_addr_and_size_are_valid(vm_ctx, device_addr, size) ||
245 offset & ~PAGE_MASK || size & ~PAGE_MASK ||
246 offset >= pvr_obj_size || offset_plus_size > pvr_obj_size)
249 bind_op->type = PVR_VM_BIND_TYPE_MAP;
251 dma_resv_lock(obj->resv, NULL);
252 bind_op->gpuvm_bo = drm_gpuvm_bo_obtain(&vm_ctx->gpuvm_mgr, obj);
253 dma_resv_unlock(obj->resv);
254 if (IS_ERR(bind_op->gpuvm_bo))
255 return PTR_ERR(bind_op->gpuvm_bo);
257 bind_op->new_va = kzalloc(sizeof(*bind_op->new_va), GFP_KERNEL);
258 bind_op->prev_va = kzalloc(sizeof(*bind_op->prev_va), GFP_KERNEL);
259 bind_op->next_va = kzalloc(sizeof(*bind_op->next_va), GFP_KERNEL);
260 if (!bind_op->new_va || !bind_op->prev_va || !bind_op->next_va) {
262 goto err_bind_op_fini;
265 /* Pin pages so they're ready for use. */
266 sgt = pvr_gem_object_get_pages_sgt(pvr_obj);
267 err = PTR_ERR_OR_ZERO(sgt);
269 goto err_bind_op_fini;
271 bind_op->mmu_op_ctx =
272 pvr_mmu_op_context_create(vm_ctx->mmu_ctx, sgt, offset, size);
273 err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx);
275 bind_op->mmu_op_ctx = NULL;
276 goto err_bind_op_fini;
279 bind_op->pvr_obj = pvr_obj;
280 bind_op->vm_ctx = vm_ctx;
281 bind_op->device_addr = device_addr;
282 bind_op->size = size;
283 bind_op->offset = offset;
288 pvr_vm_bind_op_fini(bind_op);
294 pvr_vm_bind_op_unmap_init(struct pvr_vm_bind_op *bind_op,
295 struct pvr_vm_context *vm_ctx, u64 device_addr,
300 if (!pvr_device_addr_and_size_are_valid(vm_ctx, device_addr, size))
303 bind_op->type = PVR_VM_BIND_TYPE_UNMAP;
305 bind_op->prev_va = kzalloc(sizeof(*bind_op->prev_va), GFP_KERNEL);
306 bind_op->next_va = kzalloc(sizeof(*bind_op->next_va), GFP_KERNEL);
307 if (!bind_op->prev_va || !bind_op->next_va) {
309 goto err_bind_op_fini;
312 bind_op->mmu_op_ctx =
313 pvr_mmu_op_context_create(vm_ctx->mmu_ctx, NULL, 0, 0);
314 err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx);
316 bind_op->mmu_op_ctx = NULL;
317 goto err_bind_op_fini;
320 bind_op->vm_ctx = vm_ctx;
321 bind_op->device_addr = device_addr;
322 bind_op->size = size;
327 pvr_vm_bind_op_fini(bind_op);
333 * pvr_vm_gpuva_map() - Insert a mapping into a memory context.
334 * @op: gpuva op containing the remap details.
335 * @op_ctx: Operation context.
337 * Context: Called by drm_gpuvm_sm_map following a successful mapping while
338 * @op_ctx.vm_ctx mutex is held.
342 * * Any error returned by pvr_mmu_map().
345 pvr_vm_gpuva_map(struct drm_gpuva_op *op, void *op_ctx)
347 struct pvr_gem_object *pvr_gem = gem_to_pvr_gem(op->map.gem.obj);
348 struct pvr_vm_bind_op *ctx = op_ctx;
351 if ((op->map.gem.offset | op->map.va.range) & ~PVR_DEVICE_PAGE_MASK)
354 err = pvr_mmu_map(ctx->mmu_op_ctx, op->map.va.range, pvr_gem->flags,
359 drm_gpuva_map(&ctx->vm_ctx->gpuvm_mgr, &ctx->new_va->base, &op->map);
360 drm_gpuva_link(&ctx->new_va->base, ctx->gpuvm_bo);
367 * pvr_vm_gpuva_unmap() - Remove a mapping from a memory context.
368 * @op: gpuva op containing the unmap details.
369 * @op_ctx: Operation context.
371 * Context: Called by drm_gpuvm_sm_unmap following a successful unmapping while
372 * @op_ctx.vm_ctx mutex is held.
376 * * Any error returned by pvr_mmu_unmap().
379 pvr_vm_gpuva_unmap(struct drm_gpuva_op *op, void *op_ctx)
381 struct pvr_vm_bind_op *ctx = op_ctx;
383 int err = pvr_mmu_unmap(ctx->mmu_op_ctx, op->unmap.va->va.addr,
384 op->unmap.va->va.range);
389 drm_gpuva_unmap(&op->unmap);
390 drm_gpuva_unlink(op->unmap.va);
391 kfree(to_pvr_vm_gpuva(op->unmap.va));
397 * pvr_vm_gpuva_remap() - Remap a mapping within a memory context.
398 * @op: gpuva op containing the remap details.
399 * @op_ctx: Operation context.
401 * Context: Called by either drm_gpuvm_sm_map or drm_gpuvm_sm_unmap when a
402 * mapping or unmapping operation causes a region to be split. The
403 * @op_ctx.vm_ctx mutex is held.
407 * * Any error returned by pvr_vm_gpuva_unmap() or pvr_vm_gpuva_unmap().
410 pvr_vm_gpuva_remap(struct drm_gpuva_op *op, void *op_ctx)
412 struct pvr_vm_bind_op *ctx = op_ctx;
413 u64 va_start = 0, va_range = 0;
416 drm_gpuva_op_remap_to_unmap_range(&op->remap, &va_start, &va_range);
417 err = pvr_mmu_unmap(ctx->mmu_op_ctx, va_start, va_range);
421 /* No actual remap required: the page table tree depth is fixed to 3,
422 * and we use 4k page table entries only for now.
424 drm_gpuva_remap(&ctx->prev_va->base, &ctx->next_va->base, &op->remap);
426 if (op->remap.prev) {
427 pvr_gem_object_get(gem_to_pvr_gem(ctx->prev_va->base.gem.obj));
428 drm_gpuva_link(&ctx->prev_va->base, ctx->gpuvm_bo);
432 if (op->remap.next) {
433 pvr_gem_object_get(gem_to_pvr_gem(ctx->next_va->base.gem.obj));
434 drm_gpuva_link(&ctx->next_va->base, ctx->gpuvm_bo);
438 drm_gpuva_unlink(op->remap.unmap->va);
439 kfree(to_pvr_vm_gpuva(op->remap.unmap->va));
447 * For an overview of these functions, see *DOC: Public API* in "pvr_vm.h".
451 * pvr_device_addr_is_valid() - Tests whether a device-virtual address
453 * @device_addr: Virtual device address to test.
456 * * %true if @device_addr is within the valid range for a device page
457 * table and is aligned to the device page size, or
458 * * %false otherwise.
461 pvr_device_addr_is_valid(u64 device_addr)
463 return (device_addr & ~PVR_PAGE_TABLE_ADDR_MASK) == 0 &&
464 (device_addr & ~PVR_DEVICE_PAGE_MASK) == 0;
468 * pvr_device_addr_and_size_are_valid() - Tests whether a device-virtual
469 * address and associated size are both valid.
470 * @vm_ctx: Target VM context.
471 * @device_addr: Virtual device address to test.
472 * @size: Size of the range based at @device_addr to test.
474 * Calling pvr_device_addr_is_valid() twice (once on @size, and again on
475 * @device_addr + @size) to verify a device-virtual address range initially
476 * seems intuitive, but it produces a false-negative when the address range
477 * is right at the end of device-virtual address space.
479 * This function catches that corner case, as well as checking that
483 * * %true if @device_addr is device page aligned; @size is device page
484 * aligned; the range specified by @device_addr and @size is within the
485 * bounds of the device-virtual address space, and @size is non-zero, or
486 * * %false otherwise.
489 pvr_device_addr_and_size_are_valid(struct pvr_vm_context *vm_ctx,
490 u64 device_addr, u64 size)
492 return pvr_device_addr_is_valid(device_addr) &&
493 drm_gpuvm_range_valid(&vm_ctx->gpuvm_mgr, device_addr, size) &&
494 size != 0 && (size & ~PVR_DEVICE_PAGE_MASK) == 0 &&
495 (device_addr + size <= PVR_PAGE_TABLE_ADDR_SPACE_SIZE);
498 static void pvr_gpuvm_free(struct drm_gpuvm *gpuvm)
500 kfree(to_pvr_vm_context(gpuvm));
503 static const struct drm_gpuvm_ops pvr_vm_gpuva_ops = {
504 .vm_free = pvr_gpuvm_free,
505 .sm_step_map = pvr_vm_gpuva_map,
506 .sm_step_remap = pvr_vm_gpuva_remap,
507 .sm_step_unmap = pvr_vm_gpuva_unmap,
511 fw_mem_context_init(void *cpu_ptr, void *priv)
513 struct rogue_fwif_fwmemcontext *fw_mem_ctx = cpu_ptr;
514 struct pvr_vm_context *vm_ctx = priv;
516 fw_mem_ctx->pc_dev_paddr = pvr_vm_get_page_table_root_addr(vm_ctx);
517 fw_mem_ctx->page_cat_base_reg_set = ROGUE_FW_BIF_INVALID_PCSET;
521 * pvr_vm_create_context() - Create a new VM context.
522 * @pvr_dev: Target PowerVR device.
523 * @is_userspace_context: %true if this context is for userspace. This will
524 * create a firmware memory context for the VM context
525 * and disable warnings when tearing down mappings.
528 * * A handle to the newly-minted VM context on success,
529 * * -%EINVAL if the feature "virtual address space bits" on @pvr_dev is
530 * missing or has an unsupported value,
531 * * -%ENOMEM if allocation of the structure behind the opaque handle fails,
533 * * Any error encountered while setting up internal structures.
535 struct pvr_vm_context *
536 pvr_vm_create_context(struct pvr_device *pvr_dev, bool is_userspace_context)
538 struct drm_device *drm_dev = from_pvr_device(pvr_dev);
540 struct pvr_vm_context *vm_ctx;
541 u16 device_addr_bits;
545 err = PVR_FEATURE_VALUE(pvr_dev, virtual_address_space_bits,
549 "Failed to get device virtual address space bits\n");
553 if (device_addr_bits != PVR_PAGE_TABLE_ADDR_BITS) {
555 "Device has unsupported virtual address space size\n");
556 return ERR_PTR(-EINVAL);
559 vm_ctx = kzalloc(sizeof(*vm_ctx), GFP_KERNEL);
561 return ERR_PTR(-ENOMEM);
563 vm_ctx->pvr_dev = pvr_dev;
565 vm_ctx->mmu_ctx = pvr_mmu_context_create(pvr_dev);
566 err = PTR_ERR_OR_ZERO(vm_ctx->mmu_ctx);
570 if (is_userspace_context) {
571 err = pvr_fw_object_create(pvr_dev, sizeof(struct rogue_fwif_fwmemcontext),
572 PVR_BO_FW_FLAGS_DEVICE_UNCACHED,
573 fw_mem_context_init, vm_ctx, &vm_ctx->fw_mem_ctx_obj);
576 goto err_page_table_destroy;
579 drm_gem_private_object_init(&pvr_dev->base, &vm_ctx->dummy_gem, 0);
580 drm_gpuvm_init(&vm_ctx->gpuvm_mgr,
581 is_userspace_context ? "PowerVR-user-VM" : "PowerVR-FW-VM",
582 0, &pvr_dev->base, &vm_ctx->dummy_gem,
583 0, 1ULL << device_addr_bits, 0, 0, &pvr_vm_gpuva_ops);
585 mutex_init(&vm_ctx->lock);
586 kref_init(&vm_ctx->ref_count);
590 err_page_table_destroy:
591 pvr_mmu_context_destroy(vm_ctx->mmu_ctx);
600 * pvr_vm_context_release() - Teardown a VM context.
601 * @ref_count: Pointer to reference counter of the VM context.
603 * This function ensures that no mappings are left dangling by unmapping them
604 * all in order of ascending device-virtual address.
607 pvr_vm_context_release(struct kref *ref_count)
609 struct pvr_vm_context *vm_ctx =
610 container_of(ref_count, struct pvr_vm_context, ref_count);
612 if (vm_ctx->fw_mem_ctx_obj)
613 pvr_fw_object_destroy(vm_ctx->fw_mem_ctx_obj);
615 WARN_ON(pvr_vm_unmap(vm_ctx, vm_ctx->gpuvm_mgr.mm_start,
616 vm_ctx->gpuvm_mgr.mm_range));
618 pvr_mmu_context_destroy(vm_ctx->mmu_ctx);
619 drm_gem_private_object_fini(&vm_ctx->dummy_gem);
620 mutex_destroy(&vm_ctx->lock);
622 drm_gpuvm_put(&vm_ctx->gpuvm_mgr);
626 * pvr_vm_context_lookup() - Look up VM context from handle
627 * @pvr_file: Pointer to pvr_file structure.
628 * @handle: Object handle.
630 * Takes reference on VM context object. Call pvr_vm_context_put() to release.
633 * * The requested object on success, or
634 * * %NULL on failure (object does not exist in list, or is not a VM context)
636 struct pvr_vm_context *
637 pvr_vm_context_lookup(struct pvr_file *pvr_file, u32 handle)
639 struct pvr_vm_context *vm_ctx;
641 xa_lock(&pvr_file->vm_ctx_handles);
642 vm_ctx = xa_load(&pvr_file->vm_ctx_handles, handle);
644 kref_get(&vm_ctx->ref_count);
646 xa_unlock(&pvr_file->vm_ctx_handles);
652 * pvr_vm_context_put() - Release a reference on a VM context
653 * @vm_ctx: Target VM context.
656 * * %true if the VM context was destroyed, or
657 * * %false if there are any references still remaining.
660 pvr_vm_context_put(struct pvr_vm_context *vm_ctx)
663 return kref_put(&vm_ctx->ref_count, pvr_vm_context_release);
669 * pvr_destroy_vm_contexts_for_file: Destroy any VM contexts associated with the
671 * @pvr_file: Pointer to pvr_file structure.
673 * Removes all vm_contexts associated with @pvr_file from the device VM context
674 * list and drops initial references. vm_contexts will then be destroyed once
675 * all outstanding references are dropped.
677 void pvr_destroy_vm_contexts_for_file(struct pvr_file *pvr_file)
679 struct pvr_vm_context *vm_ctx;
680 unsigned long handle;
682 xa_for_each(&pvr_file->vm_ctx_handles, handle, vm_ctx) {
683 /* vm_ctx is not used here because that would create a race with xa_erase */
684 pvr_vm_context_put(xa_erase(&pvr_file->vm_ctx_handles, handle));
689 pvr_vm_lock_extra(struct drm_gpuvm_exec *vm_exec)
691 struct pvr_vm_bind_op *bind_op = vm_exec->extra.priv;
692 struct pvr_gem_object *pvr_obj = bind_op->pvr_obj;
694 /* Unmap operations don't have an object to lock. */
698 /* Acquire lock on the GEM being mapped. */
699 return drm_exec_lock_obj(&vm_exec->exec, gem_from_pvr_gem(pvr_obj));
703 * pvr_vm_map() - Map a section of physical memory into a section of
704 * device-virtual memory.
705 * @vm_ctx: Target VM context.
706 * @pvr_obj: Target PowerVR memory object.
707 * @pvr_obj_offset: Offset into @pvr_obj to map from.
708 * @device_addr: Virtual device address at the start of the requested mapping.
709 * @size: Size of the requested mapping.
711 * No handle is returned to represent the mapping. Instead, callers should
712 * remember @device_addr and use that as a handle.
716 * * -%EINVAL if @device_addr is not a valid page-aligned device-virtual
717 * address; the region specified by @pvr_obj_offset and @size does not fall
718 * entirely within @pvr_obj, or any part of the specified region of @pvr_obj
719 * is not device-virtual page-aligned,
720 * * Any error encountered while performing internal operations required to
721 * destroy the mapping (returned from pvr_vm_gpuva_map or
722 * pvr_vm_gpuva_remap).
725 pvr_vm_map(struct pvr_vm_context *vm_ctx, struct pvr_gem_object *pvr_obj,
726 u64 pvr_obj_offset, u64 device_addr, u64 size)
728 struct pvr_vm_bind_op bind_op = {0};
729 struct drm_gpuvm_exec vm_exec = {
730 .vm = &vm_ctx->gpuvm_mgr,
731 .flags = DRM_EXEC_INTERRUPTIBLE_WAIT |
732 DRM_EXEC_IGNORE_DUPLICATES,
734 .fn = pvr_vm_lock_extra,
739 int err = pvr_vm_bind_op_map_init(&bind_op, vm_ctx, pvr_obj,
740 pvr_obj_offset, device_addr,
746 pvr_gem_object_get(pvr_obj);
748 err = drm_gpuvm_exec_lock(&vm_exec);
752 err = pvr_vm_bind_op_exec(&bind_op);
754 drm_gpuvm_exec_unlock(&vm_exec);
757 pvr_vm_bind_op_fini(&bind_op);
763 * pvr_vm_unmap() - Unmap an already mapped section of device-virtual memory.
764 * @vm_ctx: Target VM context.
765 * @device_addr: Virtual device address at the start of the target mapping.
766 * @size: Size of the target mapping.
770 * * -%EINVAL if @device_addr is not a valid page-aligned device-virtual
772 * * Any error encountered while performing internal operations required to
773 * destroy the mapping (returned from pvr_vm_gpuva_unmap or
774 * pvr_vm_gpuva_remap).
777 pvr_vm_unmap(struct pvr_vm_context *vm_ctx, u64 device_addr, u64 size)
779 struct pvr_vm_bind_op bind_op = {0};
780 struct drm_gpuvm_exec vm_exec = {
781 .vm = &vm_ctx->gpuvm_mgr,
782 .flags = DRM_EXEC_INTERRUPTIBLE_WAIT |
783 DRM_EXEC_IGNORE_DUPLICATES,
785 .fn = pvr_vm_lock_extra,
790 int err = pvr_vm_bind_op_unmap_init(&bind_op, vm_ctx, device_addr,
795 err = drm_gpuvm_exec_lock(&vm_exec);
799 err = pvr_vm_bind_op_exec(&bind_op);
801 drm_gpuvm_exec_unlock(&vm_exec);
804 pvr_vm_bind_op_fini(&bind_op);
809 /* Static data areas are determined by firmware. */
810 static const struct drm_pvr_static_data_area static_data_areas[] = {
812 .area_usage = DRM_PVR_STATIC_DATA_AREA_FENCE,
813 .location_heap_id = DRM_PVR_HEAP_GENERAL,
818 .area_usage = DRM_PVR_STATIC_DATA_AREA_YUV_CSC,
819 .location_heap_id = DRM_PVR_HEAP_GENERAL,
824 .area_usage = DRM_PVR_STATIC_DATA_AREA_VDM_SYNC,
825 .location_heap_id = DRM_PVR_HEAP_PDS_CODE_DATA,
830 .area_usage = DRM_PVR_STATIC_DATA_AREA_EOT,
831 .location_heap_id = DRM_PVR_HEAP_PDS_CODE_DATA,
836 .area_usage = DRM_PVR_STATIC_DATA_AREA_VDM_SYNC,
837 .location_heap_id = DRM_PVR_HEAP_USC_CODE,
843 #define GET_RESERVED_SIZE(last_offset, last_size) round_up((last_offset) + (last_size), PAGE_SIZE)
846 * The values given to GET_RESERVED_SIZE() are taken from the last entry in the corresponding
847 * static data area for each heap.
849 static const struct drm_pvr_heap pvr_heaps[] = {
850 [DRM_PVR_HEAP_GENERAL] = {
851 .base = ROGUE_GENERAL_HEAP_BASE,
852 .size = ROGUE_GENERAL_HEAP_SIZE,
854 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
856 [DRM_PVR_HEAP_PDS_CODE_DATA] = {
857 .base = ROGUE_PDSCODEDATA_HEAP_BASE,
858 .size = ROGUE_PDSCODEDATA_HEAP_SIZE,
860 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
862 [DRM_PVR_HEAP_USC_CODE] = {
863 .base = ROGUE_USCCODE_HEAP_BASE,
864 .size = ROGUE_USCCODE_HEAP_SIZE,
866 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
868 [DRM_PVR_HEAP_RGNHDR] = {
869 .base = ROGUE_RGNHDR_HEAP_BASE,
870 .size = ROGUE_RGNHDR_HEAP_SIZE,
872 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
874 [DRM_PVR_HEAP_VIS_TEST] = {
875 .base = ROGUE_VISTEST_HEAP_BASE,
876 .size = ROGUE_VISTEST_HEAP_SIZE,
878 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
880 [DRM_PVR_HEAP_TRANSFER_FRAG] = {
881 .base = ROGUE_TRANSFER_FRAG_HEAP_BASE,
882 .size = ROGUE_TRANSFER_FRAG_HEAP_SIZE,
884 .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
889 pvr_static_data_areas_get(const struct pvr_device *pvr_dev,
890 struct drm_pvr_ioctl_dev_query_args *args)
892 struct drm_pvr_dev_query_static_data_areas query = {0};
895 if (!args->pointer) {
896 args->size = sizeof(struct drm_pvr_dev_query_static_data_areas);
900 err = PVR_UOBJ_GET(query, args->size, args->pointer);
904 if (!query.static_data_areas.array) {
905 query.static_data_areas.count = ARRAY_SIZE(static_data_areas);
906 query.static_data_areas.stride = sizeof(struct drm_pvr_static_data_area);
910 if (query.static_data_areas.count > ARRAY_SIZE(static_data_areas))
911 query.static_data_areas.count = ARRAY_SIZE(static_data_areas);
913 err = PVR_UOBJ_SET_ARRAY(&query.static_data_areas, static_data_areas);
918 err = PVR_UOBJ_SET(args->pointer, args->size, query);
922 args->size = sizeof(query);
927 pvr_heap_info_get(const struct pvr_device *pvr_dev,
928 struct drm_pvr_ioctl_dev_query_args *args)
930 struct drm_pvr_dev_query_heap_info query = {0};
934 if (!args->pointer) {
935 args->size = sizeof(struct drm_pvr_dev_query_heap_info);
939 err = PVR_UOBJ_GET(query, args->size, args->pointer);
943 if (!query.heaps.array) {
944 query.heaps.count = ARRAY_SIZE(pvr_heaps);
945 query.heaps.stride = sizeof(struct drm_pvr_heap);
949 if (query.heaps.count > ARRAY_SIZE(pvr_heaps))
950 query.heaps.count = ARRAY_SIZE(pvr_heaps);
952 /* Region header heap is only present if BRN63142 is present. */
953 dest = query.heaps.array;
954 for (size_t i = 0; i < query.heaps.count; i++) {
955 struct drm_pvr_heap heap = pvr_heaps[i];
957 if (i == DRM_PVR_HEAP_RGNHDR && !PVR_HAS_QUIRK(pvr_dev, 63142))
960 err = PVR_UOBJ_SET(dest, query.heaps.stride, heap);
964 dest += query.heaps.stride;
968 err = PVR_UOBJ_SET(args->pointer, args->size, query);
972 args->size = sizeof(query);
977 * pvr_heap_contains_range() - Determine if a given heap contains the specified
978 * device-virtual address range.
979 * @pvr_heap: Target heap.
980 * @start: Inclusive start of the target range.
981 * @end: Inclusive end of the target range.
983 * It is an error to call this function with values of @start and @end that do
984 * not satisfy the condition @start <= @end.
986 static __always_inline bool
987 pvr_heap_contains_range(const struct drm_pvr_heap *pvr_heap, u64 start, u64 end)
989 return pvr_heap->base <= start && end < pvr_heap->base + pvr_heap->size;
993 * pvr_find_heap_containing() - Find a heap which contains the specified
994 * device-virtual address range.
995 * @pvr_dev: Target PowerVR device.
996 * @start: Start of the target range.
997 * @size: Size of the target range.
1000 * * A pointer to a constant instance of struct drm_pvr_heap representing the
1001 * heap containing the entire range specified by @start and @size on
1003 * * %NULL if no such heap exists.
1005 const struct drm_pvr_heap *
1006 pvr_find_heap_containing(struct pvr_device *pvr_dev, u64 start, u64 size)
1010 if (check_add_overflow(start, size - 1, &end))
1014 * There are no guarantees about the order of address ranges in
1015 * &pvr_heaps, so iterate over the entire array for a heap whose
1016 * range completely encompasses the given range.
1018 for (u32 heap_id = 0; heap_id < ARRAY_SIZE(pvr_heaps); heap_id++) {
1019 /* Filter heaps that present only with an associated quirk */
1020 if (heap_id == DRM_PVR_HEAP_RGNHDR &&
1021 !PVR_HAS_QUIRK(pvr_dev, 63142)) {
1025 if (pvr_heap_contains_range(&pvr_heaps[heap_id], start, end))
1026 return &pvr_heaps[heap_id];
1033 * pvr_vm_find_gem_object() - Look up a buffer object from a given
1034 * device-virtual address.
1035 * @vm_ctx: [IN] Target VM context.
1036 * @device_addr: [IN] Virtual device address at the start of the required
1038 * @mapped_offset_out: [OUT] Pointer to location to write offset of the start
1039 * of the mapped region within the buffer object. May be
1040 * %NULL if this information is not required.
1041 * @mapped_size_out: [OUT] Pointer to location to write size of the mapped
1042 * region. May be %NULL if this information is not required.
1044 * If successful, a reference will be taken on the buffer object. The caller
1045 * must drop the reference with pvr_gem_object_put().
1048 * * The PowerVR buffer object mapped at @device_addr if one exists, or
1049 * * %NULL otherwise.
1051 struct pvr_gem_object *
1052 pvr_vm_find_gem_object(struct pvr_vm_context *vm_ctx, u64 device_addr,
1053 u64 *mapped_offset_out, u64 *mapped_size_out)
1055 struct pvr_gem_object *pvr_obj;
1056 struct drm_gpuva *va;
1058 mutex_lock(&vm_ctx->lock);
1060 va = drm_gpuva_find_first(&vm_ctx->gpuvm_mgr, device_addr, 1);
1064 pvr_obj = gem_to_pvr_gem(va->gem.obj);
1065 pvr_gem_object_get(pvr_obj);
1067 if (mapped_offset_out)
1068 *mapped_offset_out = va->gem.offset;
1069 if (mapped_size_out)
1070 *mapped_size_out = va->va.range;
1072 mutex_unlock(&vm_ctx->lock);
1077 mutex_unlock(&vm_ctx->lock);
1083 * pvr_vm_get_fw_mem_context: Get object representing firmware memory context
1084 * @vm_ctx: Target VM context.
1087 * * FW object representing firmware memory context, or
1088 * * %NULL if this VM context does not have a firmware memory context.
1090 struct pvr_fw_object *
1091 pvr_vm_get_fw_mem_context(struct pvr_vm_context *vm_ctx)
1093 return vm_ctx->fw_mem_ctx_obj;