Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/imagination: Add GEM and VM related code

Add a GEM implementation based on drm_gem_shmem, and support code for the
PowerVR GPU MMU. The GPU VA manager is used for address space management.

Changes since v8:
- Updated for changes to drm_gpuvm
- Switched to dma_resv locking for vm ops
- Removed linked lists for collecting BOs in vm_context and for freeing
after ops. This is now done internally in drm_gpuvm
- Corrected license identifiers

Changes since v7:
- kernel-doc fixes
- Remove prefixes from DRM_PVR_BO_* flags
- CREATE_BO ioctl now returns an error if provided size isn't page aligned
- Optimised MMU flushes

Changes since v6:
- Don't initialise kernel_vm_ctx when using MIPS firmware processor
- Rename drm_gpuva_manager uses to drm_gpuvm
- Sync GEM object to device on creation

Changes since v5:
- Use WRITE_ONCE() when writing to page tables
- Add memory barriers to page table insertion
- Fixed double backing page alloc on page table objects
- Fix BO mask checks in DRM_IOCTL_PVR_CREATE_BO handler
- Document use of pvr_page_table_*_idx when preallocing page table objs
- Remove pvr_vm_gpuva_mapping_init()
- Remove NULL check for unmap op in remap function
- Protect gem object with mutex during drm_gpuva_link/unlink
- Defer free or release of page table pages until after TLB flush
- Use drm_gpuva_op_remap_get_unmap_range() helper

Changes since v4:
- Correct sync function in vmap/vunmap function documentation
- Update for upstream GPU VA manager
- Fix missing frees when unmapping drm_gpuva objects
- Always zero GEM BOs on creation

Changes since v3:
- Split MMU and VM code
- Register page table allocations with kmemleak
- Use drm_dev_{enter,exit}

Changes since v2:
- Use GPU VA manager
- Use drm_gem_shmem

Co-developed-by: Matt Coster <matt.coster@imgtec.com>
Signed-off-by: Matt Coster <matt.coster@imgtec.com>
Co-developed-by: Donald Robson <donald.robson@imgtec.com>
Signed-off-by: Donald Robson <donald.robson@imgtec.com>
Signed-off-by: Sarah Walker <sarah.walker@imgtec.com>
Link: https://lore.kernel.org/r/3c96dd170efe759b73897e3675d7310a7c4b06d0.1700668843.git.donald.robson@imgtec.com
Signed-off-by: Maxime Ripard <mripard@kernel.org>

authored by

Donald Robson and committed by
Maxime Ripard
ff5f643d f99f5f3e

+4756 -11
+1
drivers/gpu/drm/imagination/Kconfig
··· 7 7 depends on DRM 8 8 select DRM_GEM_SHMEM_HELPER 9 9 select DRM_SCHED 10 + select DRM_GPUVM 10 11 select FW_LOADER 11 12 help 12 13 Choose this option if you have a system that has an Imagination
+4 -1
drivers/gpu/drm/imagination/Makefile
··· 7 7 pvr_device.o \ 8 8 pvr_device_info.o \ 9 9 pvr_drv.o \ 10 - pvr_fw.o 10 + pvr_fw.o \ 11 + pvr_gem.o \ 12 + pvr_mmu.o \ 13 + pvr_vm.o 11 14 12 15 obj-$(CONFIG_DRM_POWERVR) += powervr.o
+26 -1
drivers/gpu/drm/imagination/pvr_device.c
··· 6 6 7 7 #include "pvr_fw.h" 8 8 #include "pvr_rogue_cr_defs.h" 9 + #include "pvr_vm.h" 9 10 10 11 #include <drm/drm_print.h> 11 12 ··· 313 312 else 314 313 return -EINVAL; 315 314 316 - return pvr_set_dma_info(pvr_dev); 315 + err = pvr_set_dma_info(pvr_dev); 316 + if (err) 317 + return err; 318 + 319 + if (pvr_dev->fw_dev.processor_type != PVR_FW_PROCESSOR_TYPE_MIPS) { 320 + pvr_dev->kernel_vm_ctx = pvr_vm_create_context(pvr_dev, false); 321 + if (IS_ERR(pvr_dev->kernel_vm_ctx)) 322 + return PTR_ERR(pvr_dev->kernel_vm_ctx); 323 + } 324 + 325 + return 0; 326 + } 327 + 328 + /** 329 + * pvr_device_gpu_fini() - GPU-specific deinitialization for a PowerVR device 330 + * @pvr_dev: Target PowerVR device. 331 + */ 332 + static void 333 + pvr_device_gpu_fini(struct pvr_device *pvr_dev) 334 + { 335 + if (pvr_dev->fw_dev.processor_type != PVR_FW_PROCESSOR_TYPE_MIPS) { 336 + WARN_ON(!pvr_vm_context_put(pvr_dev->kernel_vm_ctx)); 337 + pvr_dev->kernel_vm_ctx = NULL; 338 + } 317 339 } 318 340 319 341 /** ··· 388 364 * Deinitialization stages are performed in reverse order compared to 389 365 * the initialization stages in pvr_device_init(). 390 366 */ 367 + pvr_device_gpu_fini(pvr_dev); 391 368 } 392 369 393 370 bool
+24
drivers/gpu/drm/imagination/pvr_device.h
··· 123 123 */ 124 124 struct clk *mem_clk; 125 125 126 + /** 127 + * @kernel_vm_ctx: Virtual memory context used for kernel mappings. 128 + * 129 + * This is used for mappings in the firmware address region when a META firmware processor 130 + * is in use. 131 + * 132 + * When a MIPS firmware processor is in use, this will be %NULL. 133 + */ 134 + struct pvr_vm_context *kernel_vm_ctx; 135 + 126 136 /** @fw_dev: Firmware related data. */ 127 137 struct pvr_fw_device fw_dev; 138 + 139 + /** 140 + * @mmu_flush_cache_flags: Records which MMU caches require flushing 141 + * before submitting the next job. 142 + */ 143 + atomic_t mmu_flush_cache_flags; 128 144 }; 129 145 130 146 /** ··· 161 145 * to_pvr_device(). 162 146 */ 163 147 struct pvr_device *pvr_dev; 148 + 149 + /** 150 + * @vm_ctx_handles: Array of VM contexts belonging to this file. Array 151 + * members are of type "struct pvr_vm_context *". 152 + * 153 + * This array is used to allocate handles returned to userspace. 154 + */ 155 + struct xarray vm_ctx_handles; 164 156 }; 165 157 166 158 /**
+280 -9
drivers/gpu/drm/imagination/pvr_drv.c
··· 3 3 4 4 #include "pvr_device.h" 5 5 #include "pvr_drv.h" 6 + #include "pvr_gem.h" 7 + #include "pvr_mmu.h" 6 8 #include "pvr_rogue_defs.h" 7 9 #include "pvr_rogue_fwif_client.h" 8 10 #include "pvr_rogue_fwif_shared.h" 11 + #include "pvr_vm.h" 9 12 10 13 #include <uapi/drm/pvr_drm.h> 11 14 ··· 63 60 pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args, 64 61 struct drm_file *file) 65 62 { 66 - return -ENOTTY; 63 + struct drm_pvr_ioctl_create_bo_args *args = raw_args; 64 + struct pvr_device *pvr_dev = to_pvr_device(drm_dev); 65 + struct pvr_file *pvr_file = to_pvr_file(file); 66 + 67 + struct pvr_gem_object *pvr_obj; 68 + size_t sanitized_size; 69 + 70 + int idx; 71 + int err; 72 + 73 + if (!drm_dev_enter(drm_dev, &idx)) 74 + return -EIO; 75 + 76 + /* All padding fields must be zeroed. */ 77 + if (args->_padding_c != 0) { 78 + err = -EINVAL; 79 + goto err_drm_dev_exit; 80 + } 81 + 82 + /* 83 + * On 64-bit platforms (our primary target), size_t is a u64. However, 84 + * on other architectures we have to check for overflow when casting 85 + * down to size_t from u64. 86 + * 87 + * We also disallow zero-sized allocations, and reserved (kernel-only) 88 + * flags. 89 + */ 90 + if (args->size > SIZE_MAX || args->size == 0 || args->flags & 91 + ~DRM_PVR_BO_FLAGS_MASK || args->size & (PVR_DEVICE_PAGE_SIZE - 1)) { 92 + err = -EINVAL; 93 + goto err_drm_dev_exit; 94 + } 95 + 96 + sanitized_size = (size_t)args->size; 97 + 98 + /* 99 + * Create a buffer object and transfer ownership to a userspace- 100 + * accessible handle. 101 + */ 102 + pvr_obj = pvr_gem_object_create(pvr_dev, sanitized_size, args->flags); 103 + if (IS_ERR(pvr_obj)) { 104 + err = PTR_ERR(pvr_obj); 105 + goto err_drm_dev_exit; 106 + } 107 + 108 + /* This function will not modify &args->handle unless it succeeds. */ 109 + err = pvr_gem_object_into_handle(pvr_obj, pvr_file, &args->handle); 110 + if (err) 111 + goto err_destroy_obj; 112 + 113 + drm_dev_exit(idx); 114 + 115 + return 0; 116 + 117 + err_destroy_obj: 118 + /* 119 + * GEM objects are refcounted, so there is no explicit destructor 120 + * function. Instead, we release the singular reference we currently 121 + * hold on the object and let GEM take care of the rest. 122 + */ 123 + pvr_gem_object_put(pvr_obj); 124 + 125 + err_drm_dev_exit: 126 + drm_dev_exit(idx); 127 + 128 + return err; 67 129 } 68 130 69 131 /** ··· 155 87 pvr_ioctl_get_bo_mmap_offset(struct drm_device *drm_dev, void *raw_args, 156 88 struct drm_file *file) 157 89 { 158 - return -ENOTTY; 90 + struct drm_pvr_ioctl_get_bo_mmap_offset_args *args = raw_args; 91 + struct pvr_file *pvr_file = to_pvr_file(file); 92 + struct pvr_gem_object *pvr_obj; 93 + struct drm_gem_object *gem_obj; 94 + int idx; 95 + int ret; 96 + 97 + if (!drm_dev_enter(drm_dev, &idx)) 98 + return -EIO; 99 + 100 + /* All padding fields must be zeroed. */ 101 + if (args->_padding_4 != 0) { 102 + ret = -EINVAL; 103 + goto err_drm_dev_exit; 104 + } 105 + 106 + /* 107 + * Obtain a kernel reference to the buffer object. This reference is 108 + * counted and must be manually dropped before returning. If a buffer 109 + * object cannot be found for the specified handle, return -%ENOENT (No 110 + * such file or directory). 111 + */ 112 + pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle); 113 + if (!pvr_obj) { 114 + ret = -ENOENT; 115 + goto err_drm_dev_exit; 116 + } 117 + 118 + gem_obj = gem_from_pvr_gem(pvr_obj); 119 + 120 + /* 121 + * Allocate a fake offset which can be used in userspace calls to mmap 122 + * on the DRM device file. If this fails, return the error code. This 123 + * operation is idempotent. 124 + */ 125 + ret = drm_gem_create_mmap_offset(gem_obj); 126 + if (ret != 0) { 127 + /* Drop our reference to the buffer object. */ 128 + drm_gem_object_put(gem_obj); 129 + goto err_drm_dev_exit; 130 + } 131 + 132 + /* 133 + * Read out the fake offset allocated by the earlier call to 134 + * drm_gem_create_mmap_offset. 135 + */ 136 + args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node); 137 + 138 + /* Drop our reference to the buffer object. */ 139 + pvr_gem_object_put(pvr_obj); 140 + 141 + err_drm_dev_exit: 142 + drm_dev_exit(idx); 143 + 144 + return ret; 159 145 } 160 146 161 147 static __always_inline u64 ··· 638 516 break; 639 517 640 518 case DRM_PVR_DEV_QUERY_HEAP_INFO_GET: 641 - return -EINVAL; 519 + ret = pvr_heap_info_get(pvr_dev, args); 520 + break; 642 521 643 522 case DRM_PVR_DEV_QUERY_STATIC_DATA_AREAS_GET: 644 - return -EINVAL; 523 + ret = pvr_static_data_areas_get(pvr_dev, args); 524 + break; 645 525 } 646 526 647 527 drm_dev_exit(idx); ··· 790 666 pvr_ioctl_create_vm_context(struct drm_device *drm_dev, void *raw_args, 791 667 struct drm_file *file) 792 668 { 793 - return -ENOTTY; 669 + struct drm_pvr_ioctl_create_vm_context_args *args = raw_args; 670 + struct pvr_file *pvr_file = to_pvr_file(file); 671 + struct pvr_vm_context *vm_ctx; 672 + int idx; 673 + int err; 674 + 675 + if (!drm_dev_enter(drm_dev, &idx)) 676 + return -EIO; 677 + 678 + if (args->_padding_4) { 679 + err = -EINVAL; 680 + goto err_drm_dev_exit; 681 + } 682 + 683 + vm_ctx = pvr_vm_create_context(pvr_file->pvr_dev, true); 684 + if (IS_ERR(vm_ctx)) { 685 + err = PTR_ERR(vm_ctx); 686 + goto err_drm_dev_exit; 687 + } 688 + 689 + /* Allocate object handle for userspace. */ 690 + err = xa_alloc(&pvr_file->vm_ctx_handles, 691 + &args->handle, 692 + vm_ctx, 693 + xa_limit_32b, 694 + GFP_KERNEL); 695 + if (err < 0) 696 + goto err_cleanup; 697 + 698 + drm_dev_exit(idx); 699 + 700 + return 0; 701 + 702 + err_cleanup: 703 + pvr_vm_context_put(vm_ctx); 704 + 705 + err_drm_dev_exit: 706 + drm_dev_exit(idx); 707 + 708 + return err; 794 709 } 795 710 796 711 /** ··· 849 686 pvr_ioctl_destroy_vm_context(struct drm_device *drm_dev, void *raw_args, 850 687 struct drm_file *file) 851 688 { 852 - return -ENOTTY; 689 + struct drm_pvr_ioctl_destroy_vm_context_args *args = raw_args; 690 + struct pvr_file *pvr_file = to_pvr_file(file); 691 + struct pvr_vm_context *vm_ctx; 692 + 693 + if (args->_padding_4) 694 + return -EINVAL; 695 + 696 + vm_ctx = xa_erase(&pvr_file->vm_ctx_handles, args->handle); 697 + if (!vm_ctx) 698 + return -EINVAL; 699 + 700 + pvr_vm_context_put(vm_ctx); 701 + return 0; 853 702 } 854 703 855 704 /** ··· 891 716 pvr_ioctl_vm_map(struct drm_device *drm_dev, void *raw_args, 892 717 struct drm_file *file) 893 718 { 894 - return -ENOTTY; 719 + struct pvr_device *pvr_dev = to_pvr_device(drm_dev); 720 + struct drm_pvr_ioctl_vm_map_args *args = raw_args; 721 + struct pvr_file *pvr_file = to_pvr_file(file); 722 + struct pvr_vm_context *vm_ctx; 723 + 724 + struct pvr_gem_object *pvr_obj; 725 + size_t pvr_obj_size; 726 + 727 + u64 offset_plus_size; 728 + int idx; 729 + int err; 730 + 731 + if (!drm_dev_enter(drm_dev, &idx)) 732 + return -EIO; 733 + 734 + /* Initial validation of args. */ 735 + if (args->_padding_14) { 736 + err = -EINVAL; 737 + goto err_drm_dev_exit; 738 + } 739 + 740 + if (args->flags != 0 || 741 + check_add_overflow(args->offset, args->size, &offset_plus_size) || 742 + !pvr_find_heap_containing(pvr_dev, args->device_addr, args->size)) { 743 + err = -EINVAL; 744 + goto err_drm_dev_exit; 745 + } 746 + 747 + vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle); 748 + if (!vm_ctx) { 749 + err = -EINVAL; 750 + goto err_drm_dev_exit; 751 + } 752 + 753 + pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle); 754 + if (!pvr_obj) { 755 + err = -ENOENT; 756 + goto err_put_vm_context; 757 + } 758 + 759 + pvr_obj_size = pvr_gem_object_size(pvr_obj); 760 + 761 + /* 762 + * Validate offset and size args. The alignment of these will be 763 + * checked when mapping; for now just check that they're within valid 764 + * bounds 765 + */ 766 + if (args->offset >= pvr_obj_size || offset_plus_size > pvr_obj_size) { 767 + err = -EINVAL; 768 + goto err_put_pvr_object; 769 + } 770 + 771 + err = pvr_vm_map(vm_ctx, pvr_obj, args->offset, 772 + args->device_addr, args->size); 773 + if (err) 774 + goto err_put_pvr_object; 775 + 776 + /* 777 + * In order to set up the mapping, we needed a reference to &pvr_obj. 778 + * However, pvr_vm_map() obtains and stores its own reference, so we 779 + * must release ours before returning. 780 + */ 781 + 782 + err_put_pvr_object: 783 + pvr_gem_object_put(pvr_obj); 784 + 785 + err_put_vm_context: 786 + pvr_vm_context_put(vm_ctx); 787 + 788 + err_drm_dev_exit: 789 + drm_dev_exit(idx); 790 + 791 + return err; 895 792 } 896 793 897 794 /** ··· 986 739 pvr_ioctl_vm_unmap(struct drm_device *drm_dev, void *raw_args, 987 740 struct drm_file *file) 988 741 { 989 - return -ENOTTY; 742 + struct drm_pvr_ioctl_vm_unmap_args *args = raw_args; 743 + struct pvr_file *pvr_file = to_pvr_file(file); 744 + struct pvr_vm_context *vm_ctx; 745 + int err; 746 + 747 + /* Initial validation of args. */ 748 + if (args->_padding_4) 749 + return -EINVAL; 750 + 751 + vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle); 752 + if (!vm_ctx) 753 + return -EINVAL; 754 + 755 + err = pvr_vm_unmap(vm_ctx, args->device_addr, args->size); 756 + 757 + pvr_vm_context_put(vm_ctx); 758 + 759 + return err; 990 760 } 991 761 992 762 /* ··· 1194 930 */ 1195 931 pvr_file->pvr_dev = pvr_dev; 1196 932 933 + xa_init_flags(&pvr_file->vm_ctx_handles, XA_FLAGS_ALLOC1); 934 + 1197 935 /* 1198 936 * Store reference to powervr-specific file private data in DRM file 1199 937 * private data. ··· 1221 955 { 1222 956 struct pvr_file *pvr_file = to_pvr_file(file); 1223 957 958 + /* Drop references on any remaining objects. */ 959 + pvr_destroy_vm_contexts_for_file(pvr_file); 960 + 1224 961 kfree(pvr_file); 1225 962 file->driver_priv = NULL; 1226 963 } ··· 1231 962 DEFINE_DRM_GEM_FOPS(pvr_drm_driver_fops); 1232 963 1233 964 static struct drm_driver pvr_drm_driver = { 1234 - .driver_features = DRIVER_RENDER, 965 + .driver_features = DRIVER_GEM | DRIVER_GEM_GPUVA | DRIVER_RENDER, 1235 966 .open = pvr_drm_driver_open, 1236 967 .postclose = pvr_drm_driver_postclose, 1237 968 .ioctls = pvr_drm_driver_ioctls, ··· 1245 976 .minor = PVR_DRIVER_MINOR, 1246 977 .patchlevel = PVR_DRIVER_PATCHLEVEL, 1247 978 979 + .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, 980 + .gem_create_object = pvr_gem_create_object, 1248 981 }; 1249 982 1250 983 static int
+414
drivers/gpu/drm/imagination/pvr_gem.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only OR MIT 2 + /* Copyright (c) 2023 Imagination Technologies Ltd. */ 3 + 4 + #include "pvr_device.h" 5 + #include "pvr_gem.h" 6 + #include "pvr_vm.h" 7 + 8 + #include <drm/drm_gem.h> 9 + #include <drm/drm_prime.h> 10 + 11 + #include <linux/compiler.h> 12 + #include <linux/compiler_attributes.h> 13 + #include <linux/dma-buf.h> 14 + #include <linux/dma-direction.h> 15 + #include <linux/dma-mapping.h> 16 + #include <linux/err.h> 17 + #include <linux/gfp.h> 18 + #include <linux/iosys-map.h> 19 + #include <linux/log2.h> 20 + #include <linux/mutex.h> 21 + #include <linux/pagemap.h> 22 + #include <linux/refcount.h> 23 + #include <linux/scatterlist.h> 24 + 25 + static void pvr_gem_object_free(struct drm_gem_object *obj) 26 + { 27 + drm_gem_shmem_object_free(obj); 28 + } 29 + 30 + static int pvr_gem_mmap(struct drm_gem_object *gem_obj, struct vm_area_struct *vma) 31 + { 32 + struct pvr_gem_object *pvr_obj = gem_to_pvr_gem(gem_obj); 33 + struct drm_gem_shmem_object *shmem_obj = shmem_gem_from_pvr_gem(pvr_obj); 34 + 35 + if (!(pvr_obj->flags & DRM_PVR_BO_ALLOW_CPU_USERSPACE_ACCESS)) 36 + return -EINVAL; 37 + 38 + return drm_gem_shmem_mmap(shmem_obj, vma); 39 + } 40 + 41 + static const struct drm_gem_object_funcs pvr_gem_object_funcs = { 42 + .free = pvr_gem_object_free, 43 + .print_info = drm_gem_shmem_object_print_info, 44 + .pin = drm_gem_shmem_object_pin, 45 + .unpin = drm_gem_shmem_object_unpin, 46 + .get_sg_table = drm_gem_shmem_object_get_sg_table, 47 + .vmap = drm_gem_shmem_object_vmap, 48 + .vunmap = drm_gem_shmem_object_vunmap, 49 + .mmap = pvr_gem_mmap, 50 + .vm_ops = &drm_gem_shmem_vm_ops, 51 + }; 52 + 53 + /** 54 + * pvr_gem_object_flags_validate() - Verify that a collection of PowerVR GEM 55 + * mapping and/or creation flags form a valid combination. 56 + * @flags: PowerVR GEM mapping/creation flags to validate. 57 + * 58 + * This function explicitly allows kernel-only flags. All ioctl entrypoints 59 + * should do their own validation as well as relying on this function. 60 + * 61 + * Return: 62 + * * %true if @flags contains valid mapping and/or creation flags, or 63 + * * %false otherwise. 64 + */ 65 + static bool 66 + pvr_gem_object_flags_validate(u64 flags) 67 + { 68 + static const u64 invalid_combinations[] = { 69 + /* 70 + * Memory flagged as PM/FW-protected cannot be mapped to 71 + * userspace. To make this explicit, we require that the two 72 + * flags allowing each of these respective features are never 73 + * specified together. 74 + */ 75 + (DRM_PVR_BO_PM_FW_PROTECT | 76 + DRM_PVR_BO_ALLOW_CPU_USERSPACE_ACCESS), 77 + }; 78 + 79 + int i; 80 + 81 + /* 82 + * Check for bits set in undefined regions. Reserved regions refer to 83 + * options that can only be set by the kernel. These are explicitly 84 + * allowed in most cases, and must be checked specifically in IOCTL 85 + * callback code. 86 + */ 87 + if ((flags & PVR_BO_UNDEFINED_MASK) != 0) 88 + return false; 89 + 90 + /* 91 + * Check for all combinations of flags marked as invalid in the array 92 + * above. 93 + */ 94 + for (i = 0; i < ARRAY_SIZE(invalid_combinations); ++i) { 95 + u64 combo = invalid_combinations[i]; 96 + 97 + if ((flags & combo) == combo) 98 + return false; 99 + } 100 + 101 + return true; 102 + } 103 + 104 + /** 105 + * pvr_gem_object_into_handle() - Convert a reference to an object into a 106 + * userspace-accessible handle. 107 + * @pvr_obj: [IN] Target PowerVR-specific object. 108 + * @pvr_file: [IN] File to associate the handle with. 109 + * @handle: [OUT] Pointer to store the created handle in. Remains unmodified if 110 + * an error is encountered. 111 + * 112 + * If an error is encountered, ownership of @pvr_obj will not have been 113 + * transferred. If this function succeeds, however, further use of @pvr_obj is 114 + * considered undefined behaviour unless another reference to it is explicitly 115 + * held. 116 + * 117 + * Return: 118 + * * 0 on success, or 119 + * * Any error encountered while attempting to allocate a handle on @pvr_file. 120 + */ 121 + int 122 + pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj, 123 + struct pvr_file *pvr_file, u32 *handle) 124 + { 125 + struct drm_gem_object *gem_obj = gem_from_pvr_gem(pvr_obj); 126 + struct drm_file *file = from_pvr_file(pvr_file); 127 + 128 + u32 new_handle; 129 + int err; 130 + 131 + err = drm_gem_handle_create(file, gem_obj, &new_handle); 132 + if (err) 133 + return err; 134 + 135 + /* 136 + * Release our reference to @pvr_obj, effectively transferring 137 + * ownership to the handle. 138 + */ 139 + pvr_gem_object_put(pvr_obj); 140 + 141 + /* 142 + * Do not store the new handle in @handle until no more errors can 143 + * occur. 144 + */ 145 + *handle = new_handle; 146 + 147 + return 0; 148 + } 149 + 150 + /** 151 + * pvr_gem_object_from_handle() - Obtain a reference to an object from a 152 + * userspace handle. 153 + * @pvr_file: PowerVR-specific file to which @handle is associated. 154 + * @handle: Userspace handle referencing the target object. 155 + * 156 + * On return, @handle always maintains its reference to the requested object 157 + * (if it had one in the first place). If this function succeeds, the returned 158 + * object will hold an additional reference. When the caller is finished with 159 + * the returned object, they should call pvr_gem_object_put() on it to release 160 + * this reference. 161 + * 162 + * Return: 163 + * * A pointer to the requested PowerVR-specific object on success, or 164 + * * %NULL otherwise. 165 + */ 166 + struct pvr_gem_object * 167 + pvr_gem_object_from_handle(struct pvr_file *pvr_file, u32 handle) 168 + { 169 + struct drm_file *file = from_pvr_file(pvr_file); 170 + struct drm_gem_object *gem_obj; 171 + 172 + gem_obj = drm_gem_object_lookup(file, handle); 173 + if (!gem_obj) 174 + return NULL; 175 + 176 + return gem_to_pvr_gem(gem_obj); 177 + } 178 + 179 + /** 180 + * pvr_gem_object_vmap() - Map a PowerVR GEM object into CPU virtual address 181 + * space. 182 + * @pvr_obj: Target PowerVR GEM object. 183 + * 184 + * Once the caller is finished with the CPU mapping, they must call 185 + * pvr_gem_object_vunmap() on @pvr_obj. 186 + * 187 + * If @pvr_obj is CPU-cached, dma_sync_sgtable_for_cpu() is called to make 188 + * sure the CPU mapping is consistent. 189 + * 190 + * Return: 191 + * * A pointer to the CPU mapping on success, 192 + * * -%ENOMEM if the mapping fails, or 193 + * * Any error encountered while attempting to acquire a reference to the 194 + * backing pages for @pvr_obj. 195 + */ 196 + void * 197 + pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) 198 + { 199 + struct drm_gem_shmem_object *shmem_obj = shmem_gem_from_pvr_gem(pvr_obj); 200 + struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj); 201 + struct iosys_map map; 202 + int err; 203 + 204 + dma_resv_lock(obj->resv, NULL); 205 + 206 + err = drm_gem_shmem_vmap(shmem_obj, &map); 207 + if (err) 208 + goto err_unlock; 209 + 210 + if (pvr_obj->flags & PVR_BO_CPU_CACHED) { 211 + struct device *dev = shmem_obj->base.dev->dev; 212 + 213 + /* If shmem_obj->sgt is NULL, that means the buffer hasn't been mapped 214 + * in GPU space yet. 215 + */ 216 + if (shmem_obj->sgt) 217 + dma_sync_sgtable_for_cpu(dev, shmem_obj->sgt, DMA_BIDIRECTIONAL); 218 + } 219 + 220 + dma_resv_unlock(obj->resv); 221 + 222 + return map.vaddr; 223 + 224 + err_unlock: 225 + dma_resv_unlock(obj->resv); 226 + 227 + return ERR_PTR(err); 228 + } 229 + 230 + /** 231 + * pvr_gem_object_vunmap() - Unmap a PowerVR memory object from CPU virtual 232 + * address space. 233 + * @pvr_obj: Target PowerVR GEM object. 234 + * 235 + * If @pvr_obj is CPU-cached, dma_sync_sgtable_for_device() is called to make 236 + * sure the GPU mapping is consistent. 237 + */ 238 + void 239 + pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj) 240 + { 241 + struct drm_gem_shmem_object *shmem_obj = shmem_gem_from_pvr_gem(pvr_obj); 242 + struct iosys_map map = IOSYS_MAP_INIT_VADDR(shmem_obj->vaddr); 243 + struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj); 244 + 245 + if (WARN_ON(!map.vaddr)) 246 + return; 247 + 248 + dma_resv_lock(obj->resv, NULL); 249 + 250 + if (pvr_obj->flags & PVR_BO_CPU_CACHED) { 251 + struct device *dev = shmem_obj->base.dev->dev; 252 + 253 + /* If shmem_obj->sgt is NULL, that means the buffer hasn't been mapped 254 + * in GPU space yet. 255 + */ 256 + if (shmem_obj->sgt) 257 + dma_sync_sgtable_for_device(dev, shmem_obj->sgt, DMA_BIDIRECTIONAL); 258 + } 259 + 260 + drm_gem_shmem_vunmap(shmem_obj, &map); 261 + 262 + dma_resv_unlock(obj->resv); 263 + } 264 + 265 + /** 266 + * pvr_gem_object_zero() - Zeroes the physical memory behind an object. 267 + * @pvr_obj: Target PowerVR GEM object. 268 + * 269 + * Return: 270 + * * 0 on success, or 271 + * * Any error encountered while attempting to map @pvr_obj to the CPU (see 272 + * pvr_gem_object_vmap()). 273 + */ 274 + static int 275 + pvr_gem_object_zero(struct pvr_gem_object *pvr_obj) 276 + { 277 + void *cpu_ptr; 278 + 279 + cpu_ptr = pvr_gem_object_vmap(pvr_obj); 280 + if (IS_ERR(cpu_ptr)) 281 + return PTR_ERR(cpu_ptr); 282 + 283 + memset(cpu_ptr, 0, pvr_gem_object_size(pvr_obj)); 284 + 285 + /* Make sure the zero-ing is done before vumap-ing the object. */ 286 + wmb(); 287 + 288 + pvr_gem_object_vunmap(pvr_obj); 289 + 290 + return 0; 291 + } 292 + 293 + /** 294 + * pvr_gem_create_object() - Allocate and pre-initializes a pvr_gem_object 295 + * @drm_dev: DRM device creating this object. 296 + * @size: Size of the object to allocate in bytes. 297 + * 298 + * Return: 299 + * * The new pre-initialized GEM object on success, 300 + * * -ENOMEM if the allocation failed. 301 + */ 302 + struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t size) 303 + { 304 + struct drm_gem_object *gem_obj; 305 + struct pvr_gem_object *pvr_obj; 306 + 307 + pvr_obj = kzalloc(sizeof(*pvr_obj), GFP_KERNEL); 308 + if (!pvr_obj) 309 + return ERR_PTR(-ENOMEM); 310 + 311 + gem_obj = gem_from_pvr_gem(pvr_obj); 312 + gem_obj->funcs = &pvr_gem_object_funcs; 313 + 314 + return gem_obj; 315 + } 316 + 317 + /** 318 + * pvr_gem_object_create() - Creates a PowerVR-specific buffer object. 319 + * @pvr_dev: Target PowerVR device. 320 + * @size: Size of the object to allocate in bytes. Must be greater than zero. 321 + * Any value which is not an exact multiple of the system page size will be 322 + * rounded up to satisfy this condition. 323 + * @flags: Options which affect both this operation and future mapping 324 + * operations performed on the returned object. Must be a combination of 325 + * DRM_PVR_BO_* and/or PVR_BO_* flags. 326 + * 327 + * The created object may be larger than @size, but can never be smaller. To 328 + * get the exact size, call pvr_gem_object_size() on the returned pointer. 329 + * 330 + * Return: 331 + * * The newly-minted PowerVR-specific buffer object on success, 332 + * * -%EINVAL if @size is zero or @flags is not valid, 333 + * * -%ENOMEM if sufficient physical memory cannot be allocated, or 334 + * * Any other error returned by drm_gem_create_mmap_offset(). 335 + */ 336 + struct pvr_gem_object * 337 + pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) 338 + { 339 + struct drm_gem_shmem_object *shmem_obj; 340 + struct pvr_gem_object *pvr_obj; 341 + struct sg_table *sgt; 342 + int err; 343 + 344 + /* Verify @size and @flags before continuing. */ 345 + if (size == 0 || !pvr_gem_object_flags_validate(flags)) 346 + return ERR_PTR(-EINVAL); 347 + 348 + shmem_obj = drm_gem_shmem_create(from_pvr_device(pvr_dev), size); 349 + if (IS_ERR(shmem_obj)) 350 + return ERR_CAST(shmem_obj); 351 + 352 + shmem_obj->pages_mark_dirty_on_put = true; 353 + shmem_obj->map_wc = !(flags & PVR_BO_CPU_CACHED); 354 + pvr_obj = shmem_gem_to_pvr_gem(shmem_obj); 355 + pvr_obj->flags = flags; 356 + 357 + sgt = drm_gem_shmem_get_pages_sgt(shmem_obj); 358 + if (IS_ERR(sgt)) { 359 + err = PTR_ERR(sgt); 360 + goto err_shmem_object_free; 361 + } 362 + 363 + dma_sync_sgtable_for_device(shmem_obj->base.dev->dev, sgt, 364 + DMA_BIDIRECTIONAL); 365 + 366 + /* 367 + * Do this last because pvr_gem_object_zero() requires a fully 368 + * configured instance of struct pvr_gem_object. 369 + */ 370 + pvr_gem_object_zero(pvr_obj); 371 + 372 + return pvr_obj; 373 + 374 + err_shmem_object_free: 375 + drm_gem_shmem_free(shmem_obj); 376 + 377 + return ERR_PTR(err); 378 + } 379 + 380 + /** 381 + * pvr_gem_get_dma_addr() - Get DMA address for given offset in object 382 + * @pvr_obj: Pointer to object to lookup address in. 383 + * @offset: Offset within object to lookup address at. 384 + * @dma_addr_out: Pointer to location to store DMA address. 385 + * 386 + * Returns: 387 + * * 0 on success, or 388 + * * -%EINVAL if object is not currently backed, or if @offset is out of valid 389 + * range for this object. 390 + */ 391 + int 392 + pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, 393 + dma_addr_t *dma_addr_out) 394 + { 395 + struct drm_gem_shmem_object *shmem_obj = shmem_gem_from_pvr_gem(pvr_obj); 396 + u32 accumulated_offset = 0; 397 + struct scatterlist *sgl; 398 + unsigned int sgt_idx; 399 + 400 + WARN_ON(!shmem_obj->sgt); 401 + for_each_sgtable_dma_sg(shmem_obj->sgt, sgl, sgt_idx) { 402 + u32 new_offset = accumulated_offset + sg_dma_len(sgl); 403 + 404 + if (offset >= accumulated_offset && offset < new_offset) { 405 + *dma_addr_out = sg_dma_address(sgl) + 406 + (offset - accumulated_offset); 407 + return 0; 408 + } 409 + 410 + accumulated_offset = new_offset; 411 + } 412 + 413 + return -EINVAL; 414 + }
+170
drivers/gpu/drm/imagination/pvr_gem.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only OR MIT */ 2 + /* Copyright (c) 2023 Imagination Technologies Ltd. */ 3 + 4 + #ifndef PVR_GEM_H 5 + #define PVR_GEM_H 6 + 7 + #include "pvr_rogue_heap_config.h" 8 + #include "pvr_rogue_meta.h" 9 + 10 + #include <uapi/drm/pvr_drm.h> 11 + 12 + #include <drm/drm_gem.h> 13 + #include <drm/drm_gem_shmem_helper.h> 14 + #include <drm/drm_mm.h> 15 + 16 + #include <linux/bitfield.h> 17 + #include <linux/bits.h> 18 + #include <linux/const.h> 19 + #include <linux/compiler_attributes.h> 20 + #include <linux/kernel.h> 21 + #include <linux/mutex.h> 22 + #include <linux/refcount.h> 23 + #include <linux/scatterlist.h> 24 + #include <linux/sizes.h> 25 + #include <linux/types.h> 26 + 27 + /* Forward declaration from "pvr_device.h". */ 28 + struct pvr_device; 29 + struct pvr_file; 30 + 31 + /** 32 + * DOC: Flags for DRM_IOCTL_PVR_CREATE_BO (kernel-only) 33 + * 34 + * Kernel-only values allowed in &pvr_gem_object->flags. The majority of options 35 + * for this field are specified in the UAPI header "pvr_drm.h" with a 36 + * DRM_PVR_BO_ prefix. To distinguish these internal options (which must exist 37 + * in ranges marked as "reserved" in the UAPI header), we drop the DRM prefix. 38 + * The public options should be used directly, DRM prefix and all. 39 + * 40 + * To avoid potentially confusing gaps in the UAPI options, these kernel-only 41 + * options are specified "in reverse", starting at bit 63. 42 + * 43 + * We use "reserved" to refer to bits defined here and not exposed in the UAPI. 44 + * Bits not defined anywhere are "undefined". 45 + * 46 + * CPU mapping options 47 + * :PVR_BO_CPU_CACHED: By default, all GEM objects are mapped write-combined on the CPU. Set this 48 + * flag to override this behaviour and map the object cached. 49 + * 50 + * Firmware options 51 + * :PVR_BO_FW_NO_CLEAR_ON_RESET: By default, all FW objects are cleared and reinitialised on hard 52 + * reset. Set this flag to override this behaviour and preserve buffer contents on reset. 53 + */ 54 + #define PVR_BO_CPU_CACHED BIT_ULL(63) 55 + 56 + #define PVR_BO_FW_NO_CLEAR_ON_RESET BIT_ULL(62) 57 + 58 + #define PVR_BO_KERNEL_FLAGS_MASK (PVR_BO_CPU_CACHED | PVR_BO_FW_NO_CLEAR_ON_RESET) 59 + 60 + /* Bits 61..3 are undefined. */ 61 + /* Bits 2..0 are defined in the UAPI. */ 62 + 63 + /* Other utilities. */ 64 + #define PVR_BO_UNDEFINED_MASK ~(PVR_BO_KERNEL_FLAGS_MASK | DRM_PVR_BO_FLAGS_MASK) 65 + 66 + /* 67 + * All firmware-mapped memory uses (mostly) the same flags. Specifically, 68 + * firmware-mapped memory should be: 69 + * * Read/write on the device, 70 + * * Read/write on the CPU, and 71 + * * Write-combined on the CPU. 72 + * 73 + * The only variation is in caching on the device. 74 + */ 75 + #define PVR_BO_FW_FLAGS_DEVICE_CACHED (ULL(0)) 76 + #define PVR_BO_FW_FLAGS_DEVICE_UNCACHED DRM_PVR_BO_BYPASS_DEVICE_CACHE 77 + 78 + /** 79 + * struct pvr_gem_object - powervr-specific wrapper for &struct drm_gem_object 80 + */ 81 + struct pvr_gem_object { 82 + /** 83 + * @base: The underlying &struct drm_gem_shmem_object. 84 + * 85 + * Do not access this member directly, instead call 86 + * shem_gem_from_pvr_gem(). 87 + */ 88 + struct drm_gem_shmem_object base; 89 + 90 + /** 91 + * @flags: Options set at creation-time. Some of these options apply to 92 + * the creation operation itself (which are stored here for reference) 93 + * with the remainder used for mapping options to both the device and 94 + * CPU. These are used every time this object is mapped, but may be 95 + * changed after creation. 96 + * 97 + * Must be a combination of DRM_PVR_BO_* and/or PVR_BO_* flags. 98 + * 99 + * .. note:: 100 + * 101 + * This member is declared const to indicate that none of these 102 + * options may change or be changed throughout the object's 103 + * lifetime. 104 + */ 105 + u64 flags; 106 + 107 + }; 108 + 109 + static_assert(offsetof(struct pvr_gem_object, base) == 0, 110 + "offsetof(struct pvr_gem_object, base) not zero"); 111 + 112 + #define shmem_gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base) 113 + 114 + #define shmem_gem_to_pvr_gem(shmem_obj) container_of_const(shmem_obj, struct pvr_gem_object, base) 115 + 116 + #define gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base.base) 117 + 118 + #define gem_to_pvr_gem(gem_obj) container_of_const(gem_obj, struct pvr_gem_object, base.base) 119 + 120 + /* Functions defined in pvr_gem.c */ 121 + 122 + struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t size); 123 + 124 + struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, 125 + size_t size, u64 flags); 126 + 127 + int pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj, 128 + struct pvr_file *pvr_file, u32 *handle); 129 + struct pvr_gem_object *pvr_gem_object_from_handle(struct pvr_file *pvr_file, 130 + u32 handle); 131 + 132 + static __always_inline struct sg_table * 133 + pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) 134 + { 135 + return drm_gem_shmem_get_pages_sgt(shmem_gem_from_pvr_gem(pvr_obj)); 136 + } 137 + 138 + void *pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj); 139 + void pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj); 140 + 141 + int pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, 142 + dma_addr_t *dma_addr_out); 143 + 144 + /** 145 + * pvr_gem_object_get() - Acquire reference on pvr_gem_object 146 + * @pvr_obj: Pointer to object to acquire reference on. 147 + */ 148 + static __always_inline void 149 + pvr_gem_object_get(struct pvr_gem_object *pvr_obj) 150 + { 151 + drm_gem_object_get(gem_from_pvr_gem(pvr_obj)); 152 + } 153 + 154 + /** 155 + * pvr_gem_object_put() - Release reference on pvr_gem_object 156 + * @pvr_obj: Pointer to object to release reference on. 157 + */ 158 + static __always_inline void 159 + pvr_gem_object_put(struct pvr_gem_object *pvr_obj) 160 + { 161 + drm_gem_object_put(gem_from_pvr_gem(pvr_obj)); 162 + } 163 + 164 + static __always_inline size_t 165 + pvr_gem_object_size(struct pvr_gem_object *pvr_obj) 166 + { 167 + return gem_from_pvr_gem(pvr_obj)->size; 168 + } 169 + 170 + #endif /* PVR_GEM_H */
+2573
drivers/gpu/drm/imagination/pvr_mmu.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only OR MIT 2 + /* Copyright (c) 2023 Imagination Technologies Ltd. */ 3 + 4 + #include "pvr_mmu.h" 5 + 6 + #include "pvr_device.h" 7 + #include "pvr_fw.h" 8 + #include "pvr_gem.h" 9 + #include "pvr_rogue_fwif.h" 10 + #include "pvr_rogue_mmu_defs.h" 11 + 12 + #include <drm/drm_drv.h> 13 + #include <linux/atomic.h> 14 + #include <linux/bitops.h> 15 + #include <linux/dma-mapping.h> 16 + #include <linux/kmemleak.h> 17 + #include <linux/minmax.h> 18 + #include <linux/sizes.h> 19 + 20 + #define PVR_SHIFT_FROM_SIZE(size_) (__builtin_ctzll(size_)) 21 + #define PVR_MASK_FROM_SIZE(size_) (~((size_) - U64_C(1))) 22 + 23 + /* 24 + * The value of the device page size (%PVR_DEVICE_PAGE_SIZE) is currently 25 + * pegged to the host page size (%PAGE_SIZE). This chunk of macro goodness both 26 + * ensures that the selected host page size corresponds to a valid device page 27 + * size and sets up values needed by the MMU code below. 28 + */ 29 + #if (PVR_DEVICE_PAGE_SIZE == SZ_4K) 30 + # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_4KB 31 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_4KB_RANGE_SHIFT 32 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_4KB_RANGE_CLRMSK 33 + #elif (PVR_DEVICE_PAGE_SIZE == SZ_16K) 34 + # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_16KB 35 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_16KB_RANGE_SHIFT 36 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_16KB_RANGE_CLRMSK 37 + #elif (PVR_DEVICE_PAGE_SIZE == SZ_64K) 38 + # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_64KB 39 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_64KB_RANGE_SHIFT 40 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_64KB_RANGE_CLRMSK 41 + #elif (PVR_DEVICE_PAGE_SIZE == SZ_256K) 42 + # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_256KB 43 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_256KB_RANGE_SHIFT 44 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_256KB_RANGE_CLRMSK 45 + #elif (PVR_DEVICE_PAGE_SIZE == SZ_1M) 46 + # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_1MB 47 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_1MB_RANGE_SHIFT 48 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_1MB_RANGE_CLRMSK 49 + #elif (PVR_DEVICE_PAGE_SIZE == SZ_2M) 50 + # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_2MB 51 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_2MB_RANGE_SHIFT 52 + # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_2MB_RANGE_CLRMSK 53 + #else 54 + # error Unsupported device page size PVR_DEVICE_PAGE_SIZE 55 + #endif 56 + 57 + #define ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X \ 58 + (ROGUE_MMUCTRL_ENTRIES_PT_VALUE >> \ 59 + (PVR_DEVICE_PAGE_SHIFT - PVR_SHIFT_FROM_SIZE(SZ_4K))) 60 + 61 + enum pvr_mmu_sync_level { 62 + PVR_MMU_SYNC_LEVEL_NONE = -1, 63 + PVR_MMU_SYNC_LEVEL_0 = 0, 64 + PVR_MMU_SYNC_LEVEL_1 = 1, 65 + PVR_MMU_SYNC_LEVEL_2 = 2, 66 + }; 67 + 68 + #define PVR_MMU_SYNC_LEVEL_0_FLAGS (ROGUE_FWIF_MMUCACHEDATA_FLAGS_PT | \ 69 + ROGUE_FWIF_MMUCACHEDATA_FLAGS_INTERRUPT | \ 70 + ROGUE_FWIF_MMUCACHEDATA_FLAGS_TLB) 71 + #define PVR_MMU_SYNC_LEVEL_1_FLAGS (PVR_MMU_SYNC_LEVEL_0_FLAGS | ROGUE_FWIF_MMUCACHEDATA_FLAGS_PD) 72 + #define PVR_MMU_SYNC_LEVEL_2_FLAGS (PVR_MMU_SYNC_LEVEL_1_FLAGS | ROGUE_FWIF_MMUCACHEDATA_FLAGS_PC) 73 + 74 + /** 75 + * pvr_mmu_set_flush_flags() - Set MMU cache flush flags for next call to 76 + * pvr_mmu_flush_exec(). 77 + * @pvr_dev: Target PowerVR device. 78 + * @flags: MMU flush flags. Must be one of %PVR_MMU_SYNC_LEVEL_*_FLAGS. 79 + * 80 + * This function must be called following any possible change to the MMU page 81 + * tables. 82 + */ 83 + static void pvr_mmu_set_flush_flags(struct pvr_device *pvr_dev, u32 flags) 84 + { 85 + atomic_fetch_or(flags, &pvr_dev->mmu_flush_cache_flags); 86 + } 87 + 88 + /** 89 + * pvr_mmu_flush_request_all() - Request flush of all MMU caches when 90 + * subsequently calling pvr_mmu_flush_exec(). 91 + * @pvr_dev: Target PowerVR device. 92 + * 93 + * This function must be called following any possible change to the MMU page 94 + * tables. 95 + */ 96 + void pvr_mmu_flush_request_all(struct pvr_device *pvr_dev) 97 + { 98 + /* TODO: implement */ 99 + } 100 + 101 + /** 102 + * pvr_mmu_flush_exec() - Execute a flush of all MMU caches previously 103 + * requested. 104 + * @pvr_dev: Target PowerVR device. 105 + * @wait: Do not return until the flush is completed. 106 + * 107 + * This function must be called prior to submitting any new GPU job. The flush 108 + * will complete before the jobs are scheduled, so this can be called once after 109 + * a series of maps. However, a single unmap should always be immediately 110 + * followed by a flush and it should be explicitly waited by setting @wait. 111 + * 112 + * As a failure to flush the MMU caches could risk memory corruption, if the 113 + * flush fails (implying the firmware is not responding) then the GPU device is 114 + * marked as lost. 115 + * 116 + * Returns: 117 + * * 0 on success when @wait is true, or 118 + * * -%EIO if the device is unavailable, or 119 + * * Any error encountered while submitting the flush command via the KCCB. 120 + */ 121 + int pvr_mmu_flush_exec(struct pvr_device *pvr_dev, bool wait) 122 + { 123 + /* TODO: implement */ 124 + return -ENODEV; 125 + } 126 + 127 + /** 128 + * DOC: PowerVR Virtual Memory Handling 129 + */ 130 + /** 131 + * DOC: PowerVR Virtual Memory Handling (constants) 132 + * 133 + * .. c:macro:: PVR_IDX_INVALID 134 + * 135 + * Default value for a u16-based index. 136 + * 137 + * This value cannot be zero, since zero is a valid index value. 138 + */ 139 + #define PVR_IDX_INVALID ((u16)(-1)) 140 + 141 + /** 142 + * DOC: MMU backing pages 143 + */ 144 + /** 145 + * DOC: MMU backing pages (constants) 146 + * 147 + * .. c:macro:: PVR_MMU_BACKING_PAGE_SIZE 148 + * 149 + * Page size of a PowerVR device's integrated MMU. The CPU page size must be 150 + * at least as large as this value for the current implementation; this is 151 + * checked at compile-time. 152 + */ 153 + #define PVR_MMU_BACKING_PAGE_SIZE SZ_4K 154 + static_assert(PAGE_SIZE >= PVR_MMU_BACKING_PAGE_SIZE); 155 + 156 + /** 157 + * struct pvr_mmu_backing_page - Represents a single page used to back a page 158 + * table of any level. 159 + * @dma_addr: DMA address of this page. 160 + * @host_ptr: CPU address of this page. 161 + * @pvr_dev: The PowerVR device to which this page is associated. **For 162 + * internal use only.** 163 + */ 164 + struct pvr_mmu_backing_page { 165 + dma_addr_t dma_addr; 166 + void *host_ptr; 167 + /* private: internal use only */ 168 + struct page *raw_page; 169 + struct pvr_device *pvr_dev; 170 + }; 171 + 172 + /** 173 + * pvr_mmu_backing_page_init() - Initialize a MMU backing page. 174 + * @page: Target backing page. 175 + * @pvr_dev: Target PowerVR device. 176 + * 177 + * This function performs three distinct operations: 178 + * 179 + * 1. Allocate a single page, 180 + * 2. Map the page to the CPU, and 181 + * 3. Map the page to DMA-space. 182 + * 183 + * It is expected that @page be zeroed (e.g. from kzalloc()) before calling 184 + * this function. 185 + * 186 + * Return: 187 + * * 0 on success, or 188 + * * -%ENOMEM if allocation of the backing page or mapping of the backing 189 + * page to DMA fails. 190 + */ 191 + static int 192 + pvr_mmu_backing_page_init(struct pvr_mmu_backing_page *page, 193 + struct pvr_device *pvr_dev) 194 + { 195 + struct device *dev = from_pvr_device(pvr_dev)->dev; 196 + 197 + struct page *raw_page; 198 + int err; 199 + 200 + dma_addr_t dma_addr; 201 + void *host_ptr; 202 + 203 + raw_page = alloc_page(__GFP_ZERO | GFP_KERNEL); 204 + if (!raw_page) 205 + return -ENOMEM; 206 + 207 + host_ptr = vmap(&raw_page, 1, VM_MAP, pgprot_writecombine(PAGE_KERNEL)); 208 + if (!host_ptr) { 209 + err = -ENOMEM; 210 + goto err_free_page; 211 + } 212 + 213 + dma_addr = dma_map_page(dev, raw_page, 0, PVR_MMU_BACKING_PAGE_SIZE, 214 + DMA_TO_DEVICE); 215 + if (dma_mapping_error(dev, dma_addr)) { 216 + err = -ENOMEM; 217 + goto err_unmap_page; 218 + } 219 + 220 + page->dma_addr = dma_addr; 221 + page->host_ptr = host_ptr; 222 + page->pvr_dev = pvr_dev; 223 + page->raw_page = raw_page; 224 + kmemleak_alloc(page->host_ptr, PAGE_SIZE, 1, GFP_KERNEL); 225 + 226 + return 0; 227 + 228 + err_unmap_page: 229 + vunmap(host_ptr); 230 + 231 + err_free_page: 232 + __free_page(raw_page); 233 + 234 + return err; 235 + } 236 + 237 + /** 238 + * pvr_mmu_backing_page_fini() - Teardown a MMU backing page. 239 + * @page: Target backing page. 240 + * 241 + * This function performs the mirror operations to pvr_mmu_backing_page_init(), 242 + * in reverse order: 243 + * 244 + * 1. Unmap the page from DMA-space, 245 + * 2. Unmap the page from the CPU, and 246 + * 3. Free the page. 247 + * 248 + * It also zeros @page. 249 + * 250 + * It is a no-op to call this function a second (or further) time on any @page. 251 + */ 252 + static void 253 + pvr_mmu_backing_page_fini(struct pvr_mmu_backing_page *page) 254 + { 255 + struct device *dev = from_pvr_device(page->pvr_dev)->dev; 256 + 257 + /* Do nothing if no allocation is present. */ 258 + if (!page->pvr_dev) 259 + return; 260 + 261 + dma_unmap_page(dev, page->dma_addr, PVR_MMU_BACKING_PAGE_SIZE, 262 + DMA_TO_DEVICE); 263 + 264 + kmemleak_free(page->host_ptr); 265 + vunmap(page->host_ptr); 266 + 267 + __free_page(page->raw_page); 268 + 269 + memset(page, 0, sizeof(*page)); 270 + } 271 + 272 + /** 273 + * pvr_mmu_backing_page_sync() - Flush a MMU backing page from the CPU to the 274 + * device. 275 + * @page: Target backing page. 276 + * 277 + * .. caution:: 278 + * 279 + * **This is potentially an expensive function call.** Only call 280 + * pvr_mmu_backing_page_sync() once you're sure you have no more changes to 281 + * make to the backing page in the immediate future. 282 + */ 283 + static void 284 + pvr_mmu_backing_page_sync(struct pvr_mmu_backing_page *page, u32 flags) 285 + { 286 + struct pvr_device *pvr_dev = page->pvr_dev; 287 + struct device *dev; 288 + 289 + /* 290 + * Do nothing if no allocation is present. This may be the case if 291 + * we are unmapping pages. 292 + */ 293 + if (!pvr_dev) 294 + return; 295 + 296 + dev = from_pvr_device(pvr_dev)->dev; 297 + 298 + dma_sync_single_for_device(dev, page->dma_addr, 299 + PVR_MMU_BACKING_PAGE_SIZE, DMA_TO_DEVICE); 300 + 301 + pvr_mmu_set_flush_flags(pvr_dev, flags); 302 + } 303 + 304 + /** 305 + * DOC: Raw page tables 306 + */ 307 + 308 + #define PVR_PAGE_TABLE_TYPEOF_ENTRY(level_) \ 309 + typeof_member(struct pvr_page_table_l##level_##_entry_raw, val) 310 + 311 + #define PVR_PAGE_TABLE_FIELD_GET(level_, name_, field_, entry_) \ 312 + (((entry_).val & \ 313 + ~ROGUE_MMUCTRL_##name_##_DATA_##field_##_CLRMSK) >> \ 314 + ROGUE_MMUCTRL_##name_##_DATA_##field_##_SHIFT) 315 + 316 + #define PVR_PAGE_TABLE_FIELD_PREP(level_, name_, field_, val_) \ 317 + ((((PVR_PAGE_TABLE_TYPEOF_ENTRY(level_))(val_)) \ 318 + << ROGUE_MMUCTRL_##name_##_DATA_##field_##_SHIFT) & \ 319 + ~ROGUE_MMUCTRL_##name_##_DATA_##field_##_CLRMSK) 320 + 321 + /** 322 + * struct pvr_page_table_l2_entry_raw - A single entry in a level 2 page table. 323 + * @val: The raw value of this entry. 324 + * 325 + * This type is a structure for type-checking purposes. At compile-time, its 326 + * size is checked against %ROGUE_MMUCTRL_ENTRY_SIZE_PC_VALUE. 327 + * 328 + * The value stored in this structure can be decoded using the following bitmap: 329 + * 330 + * .. flat-table:: 331 + * :widths: 1 5 332 + * :stub-columns: 1 333 + * 334 + * * - 31..4 335 + * - **Level 1 Page Table Base Address:** Bits 39..12 of the L1 336 + * page table base address, which is 4KiB aligned. 337 + * 338 + * * - 3..2 339 + * - *(reserved)* 340 + * 341 + * * - 1 342 + * - **Pending:** When valid bit is not set, indicates that a valid 343 + * entry is pending and the MMU should wait for the driver to map 344 + * the entry. This is used to support page demand mapping of 345 + * memory. 346 + * 347 + * * - 0 348 + * - **Valid:** Indicates that the entry contains a valid L1 page 349 + * table. If the valid bit is not set, then an attempted use of 350 + * the page would result in a page fault. 351 + */ 352 + struct pvr_page_table_l2_entry_raw { 353 + u32 val; 354 + } __packed; 355 + static_assert(sizeof(struct pvr_page_table_l2_entry_raw) * 8 == 356 + ROGUE_MMUCTRL_ENTRY_SIZE_PC_VALUE); 357 + 358 + static bool 359 + pvr_page_table_l2_entry_raw_is_valid(struct pvr_page_table_l2_entry_raw entry) 360 + { 361 + return PVR_PAGE_TABLE_FIELD_GET(2, PC, VALID, entry); 362 + } 363 + 364 + /** 365 + * pvr_page_table_l2_entry_raw_set() - Write a valid entry into a raw level 2 366 + * page table. 367 + * @entry: Target raw level 2 page table entry. 368 + * @child_table_dma_addr: DMA address of the level 1 page table to be 369 + * associated with @entry. 370 + * 371 + * When calling this function, @child_table_dma_addr must be a valid DMA 372 + * address and a multiple of %ROGUE_MMUCTRL_PC_DATA_PD_BASE_ALIGNSIZE. 373 + */ 374 + static void 375 + pvr_page_table_l2_entry_raw_set(struct pvr_page_table_l2_entry_raw *entry, 376 + dma_addr_t child_table_dma_addr) 377 + { 378 + child_table_dma_addr >>= ROGUE_MMUCTRL_PC_DATA_PD_BASE_ALIGNSHIFT; 379 + 380 + WRITE_ONCE(entry->val, 381 + PVR_PAGE_TABLE_FIELD_PREP(2, PC, VALID, true) | 382 + PVR_PAGE_TABLE_FIELD_PREP(2, PC, ENTRY_PENDING, false) | 383 + PVR_PAGE_TABLE_FIELD_PREP(2, PC, PD_BASE, child_table_dma_addr)); 384 + } 385 + 386 + static void 387 + pvr_page_table_l2_entry_raw_clear(struct pvr_page_table_l2_entry_raw *entry) 388 + { 389 + WRITE_ONCE(entry->val, 0); 390 + } 391 + 392 + /** 393 + * struct pvr_page_table_l1_entry_raw - A single entry in a level 1 page table. 394 + * @val: The raw value of this entry. 395 + * 396 + * This type is a structure for type-checking purposes. At compile-time, its 397 + * size is checked against %ROGUE_MMUCTRL_ENTRY_SIZE_PD_VALUE. 398 + * 399 + * The value stored in this structure can be decoded using the following bitmap: 400 + * 401 + * .. flat-table:: 402 + * :widths: 1 5 403 + * :stub-columns: 1 404 + * 405 + * * - 63..41 406 + * - *(reserved)* 407 + * 408 + * * - 40 409 + * - **Pending:** When valid bit is not set, indicates that a valid entry 410 + * is pending and the MMU should wait for the driver to map the entry. 411 + * This is used to support page demand mapping of memory. 412 + * 413 + * * - 39..5 414 + * - **Level 0 Page Table Base Address:** The way this value is 415 + * interpreted depends on the page size. Bits not specified in the 416 + * table below (e.g. bits 11..5 for page size 4KiB) should be 417 + * considered reserved. 418 + * 419 + * This table shows the bits used in an L1 page table entry to 420 + * represent the Physical Table Base Address for a given Page Size. 421 + * Since each L1 page table entry covers 2MiB of address space, the 422 + * maximum page size is 2MiB. 423 + * 424 + * .. flat-table:: 425 + * :widths: 1 1 1 1 426 + * :header-rows: 1 427 + * :stub-columns: 1 428 + * 429 + * * - Page size 430 + * - L0 page table base address bits 431 + * - Number of L0 page table entries 432 + * - Size of L0 page table 433 + * 434 + * * - 4KiB 435 + * - 39..12 436 + * - 512 437 + * - 4KiB 438 + * 439 + * * - 16KiB 440 + * - 39..10 441 + * - 128 442 + * - 1KiB 443 + * 444 + * * - 64KiB 445 + * - 39..8 446 + * - 32 447 + * - 256B 448 + * 449 + * * - 256KiB 450 + * - 39..6 451 + * - 8 452 + * - 64B 453 + * 454 + * * - 1MiB 455 + * - 39..5 (4 = '0') 456 + * - 2 457 + * - 16B 458 + * 459 + * * - 2MiB 460 + * - 39..5 (4..3 = '00') 461 + * - 1 462 + * - 8B 463 + * 464 + * * - 4 465 + * - *(reserved)* 466 + * 467 + * * - 3..1 468 + * - **Page Size:** Sets the page size, from 4KiB to 2MiB. 469 + * 470 + * * - 0 471 + * - **Valid:** Indicates that the entry contains a valid L0 page table. 472 + * If the valid bit is not set, then an attempted use of the page would 473 + * result in a page fault. 474 + */ 475 + struct pvr_page_table_l1_entry_raw { 476 + u64 val; 477 + } __packed; 478 + static_assert(sizeof(struct pvr_page_table_l1_entry_raw) * 8 == 479 + ROGUE_MMUCTRL_ENTRY_SIZE_PD_VALUE); 480 + 481 + static bool 482 + pvr_page_table_l1_entry_raw_is_valid(struct pvr_page_table_l1_entry_raw entry) 483 + { 484 + return PVR_PAGE_TABLE_FIELD_GET(1, PD, VALID, entry); 485 + } 486 + 487 + /** 488 + * pvr_page_table_l1_entry_raw_set() - Write a valid entry into a raw level 1 489 + * page table. 490 + * @entry: Target raw level 1 page table entry. 491 + * @child_table_dma_addr: DMA address of the level 0 page table to be 492 + * associated with @entry. 493 + * 494 + * When calling this function, @child_table_dma_addr must be a valid DMA 495 + * address and a multiple of 4 KiB. 496 + */ 497 + static void 498 + pvr_page_table_l1_entry_raw_set(struct pvr_page_table_l1_entry_raw *entry, 499 + dma_addr_t child_table_dma_addr) 500 + { 501 + WRITE_ONCE(entry->val, 502 + PVR_PAGE_TABLE_FIELD_PREP(1, PD, VALID, true) | 503 + PVR_PAGE_TABLE_FIELD_PREP(1, PD, ENTRY_PENDING, false) | 504 + PVR_PAGE_TABLE_FIELD_PREP(1, PD, PAGE_SIZE, ROGUE_MMUCTRL_PAGE_SIZE_X) | 505 + /* 506 + * The use of a 4K-specific macro here is correct. It is 507 + * a future optimization to allocate sub-host-page-sized 508 + * blocks for individual tables, so the condition that any 509 + * page table address is aligned to the size of the 510 + * largest (a 4KB) table currently holds. 511 + */ 512 + (child_table_dma_addr & ~ROGUE_MMUCTRL_PT_BASE_4KB_RANGE_CLRMSK)); 513 + } 514 + 515 + static void 516 + pvr_page_table_l1_entry_raw_clear(struct pvr_page_table_l1_entry_raw *entry) 517 + { 518 + WRITE_ONCE(entry->val, 0); 519 + } 520 + 521 + /** 522 + * struct pvr_page_table_l0_entry_raw - A single entry in a level 0 page table. 523 + * @val: The raw value of this entry. 524 + * 525 + * This type is a structure for type-checking purposes. At compile-time, its 526 + * size is checked against %ROGUE_MMUCTRL_ENTRY_SIZE_PT_VALUE. 527 + * 528 + * The value stored in this structure can be decoded using the following bitmap: 529 + * 530 + * .. flat-table:: 531 + * :widths: 1 5 532 + * :stub-columns: 1 533 + * 534 + * * - 63 535 + * - *(reserved)* 536 + * 537 + * * - 62 538 + * - **PM/FW Protect:** Indicates a protected region which only the 539 + * Parameter Manager (PM) or firmware processor can write to. 540 + * 541 + * * - 61..40 542 + * - **VP Page (High):** Virtual-physical page used for Parameter Manager 543 + * (PM) memory. This field is only used if the additional level of PB 544 + * virtualization is enabled. The VP Page field is needed by the PM in 545 + * order to correctly reconstitute the free lists after render 546 + * completion. This (High) field holds bits 39..18 of the value; the 547 + * Low field holds bits 17..12. Bits 11..0 are always zero because the 548 + * value is always aligned to the 4KiB page size. 549 + * 550 + * * - 39..12 551 + * - **Physical Page Address:** The way this value is interpreted depends 552 + * on the page size. Bits not specified in the table below (e.g. bits 553 + * 20..12 for page size 2MiB) should be considered reserved. 554 + * 555 + * This table shows the bits used in an L0 page table entry to represent 556 + * the Physical Page Address for a given page size (as defined in the 557 + * associated L1 page table entry). 558 + * 559 + * .. flat-table:: 560 + * :widths: 1 1 561 + * :header-rows: 1 562 + * :stub-columns: 1 563 + * 564 + * * - Page size 565 + * - Physical address bits 566 + * 567 + * * - 4KiB 568 + * - 39..12 569 + * 570 + * * - 16KiB 571 + * - 39..14 572 + * 573 + * * - 64KiB 574 + * - 39..16 575 + * 576 + * * - 256KiB 577 + * - 39..18 578 + * 579 + * * - 1MiB 580 + * - 39..20 581 + * 582 + * * - 2MiB 583 + * - 39..21 584 + * 585 + * * - 11..6 586 + * - **VP Page (Low):** Continuation of VP Page (High). 587 + * 588 + * * - 5 589 + * - **Pending:** When valid bit is not set, indicates that a valid entry 590 + * is pending and the MMU should wait for the driver to map the entry. 591 + * This is used to support page demand mapping of memory. 592 + * 593 + * * - 4 594 + * - **PM Src:** Set on Parameter Manager (PM) allocated page table 595 + * entries when indicated by the PM. Note that this bit will only be set 596 + * by the PM, not by the device driver. 597 + * 598 + * * - 3 599 + * - **SLC Bypass Control:** Specifies requests to this page should bypass 600 + * the System Level Cache (SLC), if enabled in SLC configuration. 601 + * 602 + * * - 2 603 + * - **Cache Coherency:** Indicates that the page is coherent (i.e. it 604 + * does not require a cache flush between operations on the CPU and the 605 + * device). 606 + * 607 + * * - 1 608 + * - **Read Only:** If set, this bit indicates that the page is read only. 609 + * An attempted write to this page would result in a write-protection 610 + * fault. 611 + * 612 + * * - 0 613 + * - **Valid:** Indicates that the entry contains a valid page. If the 614 + * valid bit is not set, then an attempted use of the page would result 615 + * in a page fault. 616 + */ 617 + struct pvr_page_table_l0_entry_raw { 618 + u64 val; 619 + } __packed; 620 + static_assert(sizeof(struct pvr_page_table_l0_entry_raw) * 8 == 621 + ROGUE_MMUCTRL_ENTRY_SIZE_PT_VALUE); 622 + 623 + /** 624 + * struct pvr_page_flags_raw - The configurable flags from a single entry in a 625 + * level 0 page table. 626 + * @val: The raw value of these flags. Since these are a strict subset of 627 + * &struct pvr_page_table_l0_entry_raw; use that type for our member here. 628 + * 629 + * The flags stored in this type are: PM/FW Protect; SLC Bypass Control; Cache 630 + * Coherency, and Read Only (bits 62, 3, 2 and 1 respectively). 631 + * 632 + * This type should never be instantiated directly; instead use 633 + * pvr_page_flags_raw_create() to ensure only valid bits of @val are set. 634 + */ 635 + struct pvr_page_flags_raw { 636 + struct pvr_page_table_l0_entry_raw val; 637 + } __packed; 638 + static_assert(sizeof(struct pvr_page_flags_raw) == 639 + sizeof(struct pvr_page_table_l0_entry_raw)); 640 + 641 + static bool 642 + pvr_page_table_l0_entry_raw_is_valid(struct pvr_page_table_l0_entry_raw entry) 643 + { 644 + return PVR_PAGE_TABLE_FIELD_GET(0, PT, VALID, entry); 645 + } 646 + 647 + /** 648 + * pvr_page_table_l0_entry_raw_set() - Write a valid entry into a raw level 0 649 + * page table. 650 + * @entry: Target raw level 0 page table entry. 651 + * @dma_addr: DMA address of the physical page to be associated with @entry. 652 + * @flags: Options to be set on @entry. 653 + * 654 + * When calling this function, @child_table_dma_addr must be a valid DMA 655 + * address and a multiple of %PVR_DEVICE_PAGE_SIZE. 656 + * 657 + * The @flags parameter is directly assigned into @entry. It is the callers 658 + * responsibility to ensure that only bits specified in 659 + * &struct pvr_page_flags_raw are set in @flags. 660 + */ 661 + static void 662 + pvr_page_table_l0_entry_raw_set(struct pvr_page_table_l0_entry_raw *entry, 663 + dma_addr_t dma_addr, 664 + struct pvr_page_flags_raw flags) 665 + { 666 + WRITE_ONCE(entry->val, PVR_PAGE_TABLE_FIELD_PREP(0, PT, VALID, true) | 667 + PVR_PAGE_TABLE_FIELD_PREP(0, PT, ENTRY_PENDING, false) | 668 + (dma_addr & ~ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK) | 669 + flags.val.val); 670 + } 671 + 672 + static void 673 + pvr_page_table_l0_entry_raw_clear(struct pvr_page_table_l0_entry_raw *entry) 674 + { 675 + WRITE_ONCE(entry->val, 0); 676 + } 677 + 678 + /** 679 + * pvr_page_flags_raw_create() - Initialize the flag bits of a raw level 0 page 680 + * table entry. 681 + * @read_only: This page is read-only (see: Read Only). 682 + * @cache_coherent: This page does not require cache flushes (see: Cache 683 + * Coherency). 684 + * @slc_bypass: This page bypasses the device cache (see: SLC Bypass Control). 685 + * @pm_fw_protect: This page is only for use by the firmware or Parameter 686 + * Manager (see PM/FW Protect). 687 + * 688 + * For more details on the use of these four options, see their respective 689 + * entries in the table under &struct pvr_page_table_l0_entry_raw. 690 + * 691 + * Return: 692 + * A new &struct pvr_page_flags_raw instance which can be passed directly to 693 + * pvr_page_table_l0_entry_raw_set() or pvr_page_table_l0_insert(). 694 + */ 695 + static struct pvr_page_flags_raw 696 + pvr_page_flags_raw_create(bool read_only, bool cache_coherent, bool slc_bypass, 697 + bool pm_fw_protect) 698 + { 699 + struct pvr_page_flags_raw flags; 700 + 701 + flags.val.val = 702 + PVR_PAGE_TABLE_FIELD_PREP(0, PT, READ_ONLY, read_only) | 703 + PVR_PAGE_TABLE_FIELD_PREP(0, PT, CC, cache_coherent) | 704 + PVR_PAGE_TABLE_FIELD_PREP(0, PT, SLC_BYPASS_CTRL, slc_bypass) | 705 + PVR_PAGE_TABLE_FIELD_PREP(0, PT, PM_META_PROTECT, pm_fw_protect); 706 + 707 + return flags; 708 + } 709 + 710 + /** 711 + * struct pvr_page_table_l2_raw - The raw data of a level 2 page table. 712 + * 713 + * This type is a structure for type-checking purposes. At compile-time, its 714 + * size is checked against %PVR_MMU_BACKING_PAGE_SIZE. 715 + */ 716 + struct pvr_page_table_l2_raw { 717 + /** @entries: The raw values of this table. */ 718 + struct pvr_page_table_l2_entry_raw 719 + entries[ROGUE_MMUCTRL_ENTRIES_PC_VALUE]; 720 + } __packed; 721 + static_assert(sizeof(struct pvr_page_table_l2_raw) == PVR_MMU_BACKING_PAGE_SIZE); 722 + 723 + /** 724 + * struct pvr_page_table_l1_raw - The raw data of a level 1 page table. 725 + * 726 + * This type is a structure for type-checking purposes. At compile-time, its 727 + * size is checked against %PVR_MMU_BACKING_PAGE_SIZE. 728 + */ 729 + struct pvr_page_table_l1_raw { 730 + /** @entries: The raw values of this table. */ 731 + struct pvr_page_table_l1_entry_raw 732 + entries[ROGUE_MMUCTRL_ENTRIES_PD_VALUE]; 733 + } __packed; 734 + static_assert(sizeof(struct pvr_page_table_l1_raw) == PVR_MMU_BACKING_PAGE_SIZE); 735 + 736 + /** 737 + * struct pvr_page_table_l0_raw - The raw data of a level 0 page table. 738 + * 739 + * This type is a structure for type-checking purposes. At compile-time, its 740 + * size is checked against %PVR_MMU_BACKING_PAGE_SIZE. 741 + * 742 + * .. caution:: 743 + * 744 + * The size of level 0 page tables is variable depending on the page size 745 + * specified in the associated level 1 page table entry. Since the device 746 + * page size in use is pegged to the host page size, it cannot vary at 747 + * runtime. This structure is therefore only defined to contain the required 748 + * number of entries for the current device page size. **You should never 749 + * read or write beyond the last supported entry.** 750 + */ 751 + struct pvr_page_table_l0_raw { 752 + /** @entries: The raw values of this table. */ 753 + struct pvr_page_table_l0_entry_raw 754 + entries[ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X]; 755 + } __packed; 756 + static_assert(sizeof(struct pvr_page_table_l0_raw) <= PVR_MMU_BACKING_PAGE_SIZE); 757 + 758 + /** 759 + * DOC: Mirror page tables 760 + */ 761 + 762 + /* 763 + * We pre-declare these types because they cross-depend on pointers to each 764 + * other. 765 + */ 766 + struct pvr_page_table_l1; 767 + struct pvr_page_table_l0; 768 + 769 + /** 770 + * struct pvr_page_table_l2 - A wrapped level 2 page table. 771 + * 772 + * To access the raw part of this table, use pvr_page_table_l2_get_raw(). 773 + * Alternatively to access a raw entry directly, use 774 + * pvr_page_table_l2_get_entry_raw(). 775 + * 776 + * A level 2 page table forms the root of the page table tree structure, so 777 + * this type has no &parent or &parent_idx members. 778 + */ 779 + struct pvr_page_table_l2 { 780 + /** 781 + * @entries: The children of this node in the page table tree 782 + * structure. These are also mirror tables. The indexing of this array 783 + * is identical to that of the raw equivalent 784 + * (&pvr_page_table_l1_raw.entries). 785 + */ 786 + struct pvr_page_table_l1 *entries[ROGUE_MMUCTRL_ENTRIES_PC_VALUE]; 787 + 788 + /** 789 + * @backing_page: A handle to the memory which holds the raw 790 + * equivalent of this table. **For internal use only.** 791 + */ 792 + struct pvr_mmu_backing_page backing_page; 793 + 794 + /** 795 + * @entry_count: The current number of valid entries (that we know of) 796 + * in this table. This value is essentially a refcount - the table is 797 + * destroyed when this value is decremented to zero by 798 + * pvr_page_table_l2_remove(). 799 + */ 800 + u16 entry_count; 801 + }; 802 + 803 + /** 804 + * pvr_page_table_l2_init() - Initialize a level 2 page table. 805 + * @table: Target level 2 page table. 806 + * @pvr_dev: Target PowerVR device 807 + * 808 + * It is expected that @table be zeroed (e.g. from kzalloc()) before calling 809 + * this function. 810 + * 811 + * Return: 812 + * * 0 on success, or 813 + * * Any error encountered while intializing &table->backing_page using 814 + * pvr_mmu_backing_page_init(). 815 + */ 816 + static int 817 + pvr_page_table_l2_init(struct pvr_page_table_l2 *table, 818 + struct pvr_device *pvr_dev) 819 + { 820 + return pvr_mmu_backing_page_init(&table->backing_page, pvr_dev); 821 + } 822 + 823 + /** 824 + * pvr_page_table_l2_fini() - Teardown a level 2 page table. 825 + * @table: Target level 2 page table. 826 + * 827 + * It is an error to attempt to use @table after calling this function. 828 + */ 829 + static void 830 + pvr_page_table_l2_fini(struct pvr_page_table_l2 *table) 831 + { 832 + pvr_mmu_backing_page_fini(&table->backing_page); 833 + } 834 + 835 + /** 836 + * pvr_page_table_l2_sync() - Flush a level 2 page table from the CPU to the 837 + * device. 838 + * @table: Target level 2 page table. 839 + * 840 + * This is just a thin wrapper around pvr_mmu_backing_page_sync(), so the 841 + * warning there applies here too: **Only call pvr_page_table_l2_sync() once 842 + * you're sure you have no more changes to make to** @table **in the immediate 843 + * future.** 844 + * 845 + * If child level 1 page tables of @table also need to be flushed, this should 846 + * be done first using pvr_page_table_l1_sync() *before* calling this function. 847 + */ 848 + static void 849 + pvr_page_table_l2_sync(struct pvr_page_table_l2 *table) 850 + { 851 + pvr_mmu_backing_page_sync(&table->backing_page, PVR_MMU_SYNC_LEVEL_2_FLAGS); 852 + } 853 + 854 + /** 855 + * pvr_page_table_l2_get_raw() - Access the raw equivalent of a mirror level 2 856 + * page table. 857 + * @table: Target level 2 page table. 858 + * 859 + * Essentially returns the CPU address of the raw equivalent of @table, cast to 860 + * a &struct pvr_page_table_l2_raw pointer. 861 + * 862 + * You probably want to call pvr_page_table_l2_get_entry_raw() instead. 863 + * 864 + * Return: 865 + * The raw equivalent of @table. 866 + */ 867 + static struct pvr_page_table_l2_raw * 868 + pvr_page_table_l2_get_raw(struct pvr_page_table_l2 *table) 869 + { 870 + return table->backing_page.host_ptr; 871 + } 872 + 873 + /** 874 + * pvr_page_table_l2_get_entry_raw() - Access an entry from the raw equivalent 875 + * of a mirror level 2 page table. 876 + * @table: Target level 2 page table. 877 + * @idx: Index of the entry to access. 878 + * 879 + * Technically this function returns a pointer to a slot in a raw level 2 page 880 + * table, since the returned "entry" is not guaranteed to be valid. The caller 881 + * must verify the validity of the entry at the returned address (perhaps using 882 + * pvr_page_table_l2_entry_raw_is_valid()) before reading or overwriting it. 883 + * 884 + * The value of @idx is not checked here; it is the callers responsibility to 885 + * ensure @idx refers to a valid index within @table before dereferencing the 886 + * returned pointer. 887 + * 888 + * Return: 889 + * A pointer to the requested raw level 2 page table entry. 890 + */ 891 + static struct pvr_page_table_l2_entry_raw * 892 + pvr_page_table_l2_get_entry_raw(struct pvr_page_table_l2 *table, u16 idx) 893 + { 894 + return &pvr_page_table_l2_get_raw(table)->entries[idx]; 895 + } 896 + 897 + /** 898 + * pvr_page_table_l2_entry_is_valid() - Check if a level 2 page table entry is 899 + * marked as valid. 900 + * @table: Target level 2 page table. 901 + * @idx: Index of the entry to check. 902 + * 903 + * The value of @idx is not checked here; it is the callers responsibility to 904 + * ensure @idx refers to a valid index within @table before calling this 905 + * function. 906 + */ 907 + static bool 908 + pvr_page_table_l2_entry_is_valid(struct pvr_page_table_l2 *table, u16 idx) 909 + { 910 + struct pvr_page_table_l2_entry_raw entry_raw = 911 + *pvr_page_table_l2_get_entry_raw(table, idx); 912 + 913 + return pvr_page_table_l2_entry_raw_is_valid(entry_raw); 914 + } 915 + 916 + /** 917 + * struct pvr_page_table_l1 - A wrapped level 1 page table. 918 + * 919 + * To access the raw part of this table, use pvr_page_table_l1_get_raw(). 920 + * Alternatively to access a raw entry directly, use 921 + * pvr_page_table_l1_get_entry_raw(). 922 + */ 923 + struct pvr_page_table_l1 { 924 + /** 925 + * @entries: The children of this node in the page table tree 926 + * structure. These are also mirror tables. The indexing of this array 927 + * is identical to that of the raw equivalent 928 + * (&pvr_page_table_l0_raw.entries). 929 + */ 930 + struct pvr_page_table_l0 *entries[ROGUE_MMUCTRL_ENTRIES_PD_VALUE]; 931 + 932 + /** 933 + * @backing_page: A handle to the memory which holds the raw 934 + * equivalent of this table. **For internal use only.** 935 + */ 936 + struct pvr_mmu_backing_page backing_page; 937 + 938 + union { 939 + /** 940 + * @parent: The parent of this node in the page table tree structure. 941 + * 942 + * This is also a mirror table. 943 + * 944 + * Only valid when the L1 page table is active. When the L1 page table 945 + * has been removed and queued for destruction, the next_free field 946 + * should be used instead. 947 + */ 948 + struct pvr_page_table_l2 *parent; 949 + 950 + /** 951 + * @next_free: Pointer to the next L1 page table to take/free. 952 + * 953 + * Used to form a linked list of L1 page tables. This is used 954 + * when preallocating tables and when the page table has been 955 + * removed and queued for destruction. 956 + */ 957 + struct pvr_page_table_l1 *next_free; 958 + }; 959 + 960 + /** 961 + * @parent_idx: The index of the entry in the parent table (see 962 + * @parent) which corresponds to this table. 963 + */ 964 + u16 parent_idx; 965 + 966 + /** 967 + * @entry_count: The current number of valid entries (that we know of) 968 + * in this table. This value is essentially a refcount - the table is 969 + * destroyed when this value is decremented to zero by 970 + * pvr_page_table_l1_remove(). 971 + */ 972 + u16 entry_count; 973 + }; 974 + 975 + /** 976 + * pvr_page_table_l1_init() - Initialize a level 1 page table. 977 + * @table: Target level 1 page table. 978 + * @pvr_dev: Target PowerVR device 979 + * 980 + * When this function returns successfully, @table is still not considered 981 + * valid. It must be inserted into the page table tree structure with 982 + * pvr_page_table_l2_insert() before it is ready for use. 983 + * 984 + * It is expected that @table be zeroed (e.g. from kzalloc()) before calling 985 + * this function. 986 + * 987 + * Return: 988 + * * 0 on success, or 989 + * * Any error encountered while intializing &table->backing_page using 990 + * pvr_mmu_backing_page_init(). 991 + */ 992 + static int 993 + pvr_page_table_l1_init(struct pvr_page_table_l1 *table, 994 + struct pvr_device *pvr_dev) 995 + { 996 + table->parent_idx = PVR_IDX_INVALID; 997 + 998 + return pvr_mmu_backing_page_init(&table->backing_page, pvr_dev); 999 + } 1000 + 1001 + /** 1002 + * pvr_page_table_l1_free() - Teardown a level 1 page table. 1003 + * @table: Target level 1 page table. 1004 + * 1005 + * It is an error to attempt to use @table after calling this function, even 1006 + * indirectly. This includes calling pvr_page_table_l2_remove(), which must 1007 + * be called *before* pvr_page_table_l1_free(). 1008 + */ 1009 + static void 1010 + pvr_page_table_l1_free(struct pvr_page_table_l1 *table) 1011 + { 1012 + pvr_mmu_backing_page_fini(&table->backing_page); 1013 + kfree(table); 1014 + } 1015 + 1016 + /** 1017 + * pvr_page_table_l1_sync() - Flush a level 1 page table from the CPU to the 1018 + * device. 1019 + * @table: Target level 1 page table. 1020 + * 1021 + * This is just a thin wrapper around pvr_mmu_backing_page_sync(), so the 1022 + * warning there applies here too: **Only call pvr_page_table_l1_sync() once 1023 + * you're sure you have no more changes to make to** @table **in the immediate 1024 + * future.** 1025 + * 1026 + * If child level 0 page tables of @table also need to be flushed, this should 1027 + * be done first using pvr_page_table_l0_sync() *before* calling this function. 1028 + */ 1029 + static void 1030 + pvr_page_table_l1_sync(struct pvr_page_table_l1 *table) 1031 + { 1032 + pvr_mmu_backing_page_sync(&table->backing_page, PVR_MMU_SYNC_LEVEL_1_FLAGS); 1033 + } 1034 + 1035 + /** 1036 + * pvr_page_table_l1_get_raw() - Access the raw equivalent of a mirror level 1 1037 + * page table. 1038 + * @table: Target level 1 page table. 1039 + * 1040 + * Essentially returns the CPU address of the raw equivalent of @table, cast to 1041 + * a &struct pvr_page_table_l1_raw pointer. 1042 + * 1043 + * You probably want to call pvr_page_table_l1_get_entry_raw() instead. 1044 + * 1045 + * Return: 1046 + * The raw equivalent of @table. 1047 + */ 1048 + static struct pvr_page_table_l1_raw * 1049 + pvr_page_table_l1_get_raw(struct pvr_page_table_l1 *table) 1050 + { 1051 + return table->backing_page.host_ptr; 1052 + } 1053 + 1054 + /** 1055 + * pvr_page_table_l1_get_entry_raw() - Access an entry from the raw equivalent 1056 + * of a mirror level 1 page table. 1057 + * @table: Target level 1 page table. 1058 + * @idx: Index of the entry to access. 1059 + * 1060 + * Technically this function returns a pointer to a slot in a raw level 1 page 1061 + * table, since the returned "entry" is not guaranteed to be valid. The caller 1062 + * must verify the validity of the entry at the returned address (perhaps using 1063 + * pvr_page_table_l1_entry_raw_is_valid()) before reading or overwriting it. 1064 + * 1065 + * The value of @idx is not checked here; it is the callers responsibility to 1066 + * ensure @idx refers to a valid index within @table before dereferencing the 1067 + * returned pointer. 1068 + * 1069 + * Return: 1070 + * A pointer to the requested raw level 1 page table entry. 1071 + */ 1072 + static struct pvr_page_table_l1_entry_raw * 1073 + pvr_page_table_l1_get_entry_raw(struct pvr_page_table_l1 *table, u16 idx) 1074 + { 1075 + return &pvr_page_table_l1_get_raw(table)->entries[idx]; 1076 + } 1077 + 1078 + /** 1079 + * pvr_page_table_l1_entry_is_valid() - Check if a level 1 page table entry is 1080 + * marked as valid. 1081 + * @table: Target level 1 page table. 1082 + * @idx: Index of the entry to check. 1083 + * 1084 + * The value of @idx is not checked here; it is the callers responsibility to 1085 + * ensure @idx refers to a valid index within @table before calling this 1086 + * function. 1087 + */ 1088 + static bool 1089 + pvr_page_table_l1_entry_is_valid(struct pvr_page_table_l1 *table, u16 idx) 1090 + { 1091 + struct pvr_page_table_l1_entry_raw entry_raw = 1092 + *pvr_page_table_l1_get_entry_raw(table, idx); 1093 + 1094 + return pvr_page_table_l1_entry_raw_is_valid(entry_raw); 1095 + } 1096 + 1097 + /** 1098 + * struct pvr_page_table_l0 - A wrapped level 0 page table. 1099 + * 1100 + * To access the raw part of this table, use pvr_page_table_l0_get_raw(). 1101 + * Alternatively to access a raw entry directly, use 1102 + * pvr_page_table_l0_get_entry_raw(). 1103 + * 1104 + * There is no mirror representation of an individual page, so this type has no 1105 + * &entries member. 1106 + */ 1107 + struct pvr_page_table_l0 { 1108 + /** 1109 + * @backing_page: A handle to the memory which holds the raw 1110 + * equivalent of this table. **For internal use only.** 1111 + */ 1112 + struct pvr_mmu_backing_page backing_page; 1113 + 1114 + union { 1115 + /** 1116 + * @parent: The parent of this node in the page table tree structure. 1117 + * 1118 + * This is also a mirror table. 1119 + * 1120 + * Only valid when the L0 page table is active. When the L0 page table 1121 + * has been removed and queued for destruction, the next_free field 1122 + * should be used instead. 1123 + */ 1124 + struct pvr_page_table_l1 *parent; 1125 + 1126 + /** 1127 + * @next_free: Pointer to the next L0 page table to take/free. 1128 + * 1129 + * Used to form a linked list of L0 page tables. This is used 1130 + * when preallocating tables and when the page table has been 1131 + * removed and queued for destruction. 1132 + */ 1133 + struct pvr_page_table_l0 *next_free; 1134 + }; 1135 + 1136 + /** 1137 + * @parent_idx: The index of the entry in the parent table (see 1138 + * @parent) which corresponds to this table. 1139 + */ 1140 + u16 parent_idx; 1141 + 1142 + /** 1143 + * @entry_count: The current number of valid entries (that we know of) 1144 + * in this table. This value is essentially a refcount - the table is 1145 + * destroyed when this value is decremented to zero by 1146 + * pvr_page_table_l0_remove(). 1147 + */ 1148 + u16 entry_count; 1149 + }; 1150 + 1151 + /** 1152 + * pvr_page_table_l0_init() - Initialize a level 0 page table. 1153 + * @table: Target level 0 page table. 1154 + * @pvr_dev: Target PowerVR device 1155 + * 1156 + * When this function returns successfully, @table is still not considered 1157 + * valid. It must be inserted into the page table tree structure with 1158 + * pvr_page_table_l1_insert() before it is ready for use. 1159 + * 1160 + * It is expected that @table be zeroed (e.g. from kzalloc()) before calling 1161 + * this function. 1162 + * 1163 + * Return: 1164 + * * 0 on success, or 1165 + * * Any error encountered while intializing &table->backing_page using 1166 + * pvr_mmu_backing_page_init(). 1167 + */ 1168 + static int 1169 + pvr_page_table_l0_init(struct pvr_page_table_l0 *table, 1170 + struct pvr_device *pvr_dev) 1171 + { 1172 + table->parent_idx = PVR_IDX_INVALID; 1173 + 1174 + return pvr_mmu_backing_page_init(&table->backing_page, pvr_dev); 1175 + } 1176 + 1177 + /** 1178 + * pvr_page_table_l0_free() - Teardown a level 0 page table. 1179 + * @table: Target level 0 page table. 1180 + * 1181 + * It is an error to attempt to use @table after calling this function, even 1182 + * indirectly. This includes calling pvr_page_table_l1_remove(), which must 1183 + * be called *before* pvr_page_table_l0_free(). 1184 + */ 1185 + static void 1186 + pvr_page_table_l0_free(struct pvr_page_table_l0 *table) 1187 + { 1188 + pvr_mmu_backing_page_fini(&table->backing_page); 1189 + kfree(table); 1190 + } 1191 + 1192 + /** 1193 + * pvr_page_table_l0_sync() - Flush a level 0 page table from the CPU to the 1194 + * device. 1195 + * @table: Target level 0 page table. 1196 + * 1197 + * This is just a thin wrapper around pvr_mmu_backing_page_sync(), so the 1198 + * warning there applies here too: **Only call pvr_page_table_l0_sync() once 1199 + * you're sure you have no more changes to make to** @table **in the immediate 1200 + * future.** 1201 + * 1202 + * If child pages of @table also need to be flushed, this should be done first 1203 + * using a DMA sync function (e.g. dma_sync_sg_for_device()) *before* calling 1204 + * this function. 1205 + */ 1206 + static void 1207 + pvr_page_table_l0_sync(struct pvr_page_table_l0 *table) 1208 + { 1209 + pvr_mmu_backing_page_sync(&table->backing_page, PVR_MMU_SYNC_LEVEL_0_FLAGS); 1210 + } 1211 + 1212 + /** 1213 + * pvr_page_table_l0_get_raw() - Access the raw equivalent of a mirror level 0 1214 + * page table. 1215 + * @table: Target level 0 page table. 1216 + * 1217 + * Essentially returns the CPU address of the raw equivalent of @table, cast to 1218 + * a &struct pvr_page_table_l0_raw pointer. 1219 + * 1220 + * You probably want to call pvr_page_table_l0_get_entry_raw() instead. 1221 + * 1222 + * Return: 1223 + * The raw equivalent of @table. 1224 + */ 1225 + static struct pvr_page_table_l0_raw * 1226 + pvr_page_table_l0_get_raw(struct pvr_page_table_l0 *table) 1227 + { 1228 + return table->backing_page.host_ptr; 1229 + } 1230 + 1231 + /** 1232 + * pvr_page_table_l0_get_entry_raw() - Access an entry from the raw equivalent 1233 + * of a mirror level 0 page table. 1234 + * @table: Target level 0 page table. 1235 + * @idx: Index of the entry to access. 1236 + * 1237 + * Technically this function returns a pointer to a slot in a raw level 0 page 1238 + * table, since the returned "entry" is not guaranteed to be valid. The caller 1239 + * must verify the validity of the entry at the returned address (perhaps using 1240 + * pvr_page_table_l0_entry_raw_is_valid()) before reading or overwriting it. 1241 + * 1242 + * The value of @idx is not checked here; it is the callers responsibility to 1243 + * ensure @idx refers to a valid index within @table before dereferencing the 1244 + * returned pointer. This is espcially important for level 0 page tables, which 1245 + * can have a variable number of entries. 1246 + * 1247 + * Return: 1248 + * A pointer to the requested raw level 0 page table entry. 1249 + */ 1250 + static struct pvr_page_table_l0_entry_raw * 1251 + pvr_page_table_l0_get_entry_raw(struct pvr_page_table_l0 *table, u16 idx) 1252 + { 1253 + return &pvr_page_table_l0_get_raw(table)->entries[idx]; 1254 + } 1255 + 1256 + /** 1257 + * pvr_page_table_l0_entry_is_valid() - Check if a level 0 page table entry is 1258 + * marked as valid. 1259 + * @table: Target level 0 page table. 1260 + * @idx: Index of the entry to check. 1261 + * 1262 + * The value of @idx is not checked here; it is the callers responsibility to 1263 + * ensure @idx refers to a valid index within @table before calling this 1264 + * function. 1265 + */ 1266 + static bool 1267 + pvr_page_table_l0_entry_is_valid(struct pvr_page_table_l0 *table, u16 idx) 1268 + { 1269 + struct pvr_page_table_l0_entry_raw entry_raw = 1270 + *pvr_page_table_l0_get_entry_raw(table, idx); 1271 + 1272 + return pvr_page_table_l0_entry_raw_is_valid(entry_raw); 1273 + } 1274 + 1275 + /** 1276 + * struct pvr_mmu_context - context holding data for operations at page 1277 + * catalogue level, intended for use with a VM context. 1278 + */ 1279 + struct pvr_mmu_context { 1280 + /** @pvr_dev: The PVR device associated with the owning VM context. */ 1281 + struct pvr_device *pvr_dev; 1282 + 1283 + /** @page_table_l2: The MMU table root. */ 1284 + struct pvr_page_table_l2 page_table_l2; 1285 + }; 1286 + 1287 + /** 1288 + * struct pvr_page_table_ptr - A reference to a single physical page as indexed 1289 + * by the page table structure. 1290 + * 1291 + * Intended for embedding in a &struct pvr_mmu_op_context. 1292 + */ 1293 + struct pvr_page_table_ptr { 1294 + /** 1295 + * @l1_table: A cached handle to the level 1 page table the 1296 + * context is currently traversing. 1297 + */ 1298 + struct pvr_page_table_l1 *l1_table; 1299 + 1300 + /** 1301 + * @l0_table: A cached handle to the level 0 page table the 1302 + * context is currently traversing. 1303 + */ 1304 + struct pvr_page_table_l0 *l0_table; 1305 + 1306 + /** 1307 + * @l2_idx: Index into the level 2 page table the context is 1308 + * currently referencing. 1309 + */ 1310 + u16 l2_idx; 1311 + 1312 + /** 1313 + * @l1_idx: Index into the level 1 page table the context is 1314 + * currently referencing. 1315 + */ 1316 + u16 l1_idx; 1317 + 1318 + /** 1319 + * @l0_idx: Index into the level 0 page table the context is 1320 + * currently referencing. 1321 + */ 1322 + u16 l0_idx; 1323 + }; 1324 + 1325 + /** 1326 + * struct pvr_mmu_op_context - context holding data for individual 1327 + * device-virtual mapping operations. Intended for use with a VM bind operation. 1328 + */ 1329 + struct pvr_mmu_op_context { 1330 + /** @mmu_ctx: The MMU context associated with the owning VM context. */ 1331 + struct pvr_mmu_context *mmu_ctx; 1332 + 1333 + /** @map: Data specifically for map operations. */ 1334 + struct { 1335 + /** 1336 + * @sgt: Scatter gather table containing pages pinned for use by 1337 + * this context - these are currently pinned when initialising 1338 + * the VM bind operation. 1339 + */ 1340 + struct sg_table *sgt; 1341 + 1342 + /** @sgt_offset: Start address of the device-virtual mapping. */ 1343 + u64 sgt_offset; 1344 + 1345 + /** 1346 + * @l1_prealloc_tables: Preallocated l1 page table objects 1347 + * use by this context when creating a page mapping. Linked list 1348 + * fully created during initialisation. 1349 + */ 1350 + struct pvr_page_table_l1 *l1_prealloc_tables; 1351 + 1352 + /** 1353 + * @l0_prealloc_tables: Preallocated l0 page table objects 1354 + * use by this context when creating a page mapping. Linked list 1355 + * fully created during initialisation. 1356 + */ 1357 + struct pvr_page_table_l0 *l0_prealloc_tables; 1358 + } map; 1359 + 1360 + /** @unmap: Data specifically for unmap operations. */ 1361 + struct { 1362 + /** 1363 + * @l1_free_tables: Collects page table objects freed by unmap 1364 + * ops. Linked list empty at creation. 1365 + */ 1366 + struct pvr_page_table_l1 *l1_free_tables; 1367 + 1368 + /** 1369 + * @l0_free_tables: Collects page table objects freed by unmap 1370 + * ops. Linked list empty at creation. 1371 + */ 1372 + struct pvr_page_table_l0 *l0_free_tables; 1373 + } unmap; 1374 + 1375 + /** 1376 + * @curr_page: A reference to a single physical page as indexed by the 1377 + * page table structure. 1378 + */ 1379 + struct pvr_page_table_ptr curr_page; 1380 + 1381 + /** 1382 + * @sync_level_required: The maximum level of the page table tree 1383 + * structure which has (possibly) been modified since it was last 1384 + * flushed to the device. 1385 + * 1386 + * This field should only be set with pvr_mmu_op_context_require_sync() 1387 + * or indirectly by pvr_mmu_op_context_sync_partial(). 1388 + */ 1389 + enum pvr_mmu_sync_level sync_level_required; 1390 + }; 1391 + 1392 + /** 1393 + * pvr_page_table_l2_insert() - Insert an entry referring to a level 1 page 1394 + * table into a level 2 page table. 1395 + * @op_ctx: Target MMU op context pointing at the entry to insert the L1 page 1396 + * table into. 1397 + * @child_table: Target level 1 page table to be referenced by the new entry. 1398 + * 1399 + * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1400 + * valid L2 entry. 1401 + * 1402 + * It is the caller's responsibility to execute any memory barries to ensure 1403 + * that the creation of @child_table is ordered before the L2 entry is inserted. 1404 + */ 1405 + static void 1406 + pvr_page_table_l2_insert(struct pvr_mmu_op_context *op_ctx, 1407 + struct pvr_page_table_l1 *child_table) 1408 + { 1409 + struct pvr_page_table_l2 *l2_table = 1410 + &op_ctx->mmu_ctx->page_table_l2; 1411 + struct pvr_page_table_l2_entry_raw *entry_raw = 1412 + pvr_page_table_l2_get_entry_raw(l2_table, 1413 + op_ctx->curr_page.l2_idx); 1414 + 1415 + pvr_page_table_l2_entry_raw_set(entry_raw, 1416 + child_table->backing_page.dma_addr); 1417 + 1418 + child_table->parent = l2_table; 1419 + child_table->parent_idx = op_ctx->curr_page.l2_idx; 1420 + l2_table->entries[op_ctx->curr_page.l2_idx] = child_table; 1421 + ++l2_table->entry_count; 1422 + op_ctx->curr_page.l1_table = child_table; 1423 + } 1424 + 1425 + /** 1426 + * pvr_page_table_l2_remove() - Remove a level 1 page table from a level 2 page 1427 + * table. 1428 + * @op_ctx: Target MMU op context pointing at the L2 entry to remove. 1429 + * 1430 + * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1431 + * valid L2 entry. 1432 + */ 1433 + static void 1434 + pvr_page_table_l2_remove(struct pvr_mmu_op_context *op_ctx) 1435 + { 1436 + struct pvr_page_table_l2 *l2_table = 1437 + &op_ctx->mmu_ctx->page_table_l2; 1438 + struct pvr_page_table_l2_entry_raw *entry_raw = 1439 + pvr_page_table_l2_get_entry_raw(l2_table, 1440 + op_ctx->curr_page.l1_table->parent_idx); 1441 + 1442 + WARN_ON(op_ctx->curr_page.l1_table->parent != l2_table); 1443 + 1444 + pvr_page_table_l2_entry_raw_clear(entry_raw); 1445 + 1446 + l2_table->entries[op_ctx->curr_page.l1_table->parent_idx] = NULL; 1447 + op_ctx->curr_page.l1_table->parent_idx = PVR_IDX_INVALID; 1448 + op_ctx->curr_page.l1_table->next_free = op_ctx->unmap.l1_free_tables; 1449 + op_ctx->unmap.l1_free_tables = op_ctx->curr_page.l1_table; 1450 + op_ctx->curr_page.l1_table = NULL; 1451 + 1452 + --l2_table->entry_count; 1453 + } 1454 + 1455 + /** 1456 + * pvr_page_table_l1_insert() - Insert an entry referring to a level 0 page 1457 + * table into a level 1 page table. 1458 + * @op_ctx: Target MMU op context pointing at the entry to insert the L0 page 1459 + * table into. 1460 + * @child_table: L0 page table to insert. 1461 + * 1462 + * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1463 + * valid L1 entry. 1464 + * 1465 + * It is the caller's responsibility to execute any memory barries to ensure 1466 + * that the creation of @child_table is ordered before the L1 entry is inserted. 1467 + */ 1468 + static void 1469 + pvr_page_table_l1_insert(struct pvr_mmu_op_context *op_ctx, 1470 + struct pvr_page_table_l0 *child_table) 1471 + { 1472 + struct pvr_page_table_l1_entry_raw *entry_raw = 1473 + pvr_page_table_l1_get_entry_raw(op_ctx->curr_page.l1_table, 1474 + op_ctx->curr_page.l1_idx); 1475 + 1476 + pvr_page_table_l1_entry_raw_set(entry_raw, 1477 + child_table->backing_page.dma_addr); 1478 + 1479 + child_table->parent = op_ctx->curr_page.l1_table; 1480 + child_table->parent_idx = op_ctx->curr_page.l1_idx; 1481 + op_ctx->curr_page.l1_table->entries[op_ctx->curr_page.l1_idx] = child_table; 1482 + ++op_ctx->curr_page.l1_table->entry_count; 1483 + op_ctx->curr_page.l0_table = child_table; 1484 + } 1485 + 1486 + /** 1487 + * pvr_page_table_l1_remove() - Remove a level 0 page table from a level 1 page 1488 + * table. 1489 + * @op_ctx: Target MMU op context pointing at the L1 entry to remove. 1490 + * 1491 + * If this function results in the L1 table becoming empty, it will be removed 1492 + * from its parent level 2 page table and destroyed. 1493 + * 1494 + * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1495 + * valid L1 entry. 1496 + */ 1497 + static void 1498 + pvr_page_table_l1_remove(struct pvr_mmu_op_context *op_ctx) 1499 + { 1500 + struct pvr_page_table_l1_entry_raw *entry_raw = 1501 + pvr_page_table_l1_get_entry_raw(op_ctx->curr_page.l0_table->parent, 1502 + op_ctx->curr_page.l0_table->parent_idx); 1503 + 1504 + WARN_ON(op_ctx->curr_page.l0_table->parent != 1505 + op_ctx->curr_page.l1_table); 1506 + 1507 + pvr_page_table_l1_entry_raw_clear(entry_raw); 1508 + 1509 + op_ctx->curr_page.l1_table->entries[op_ctx->curr_page.l0_table->parent_idx] = NULL; 1510 + op_ctx->curr_page.l0_table->parent_idx = PVR_IDX_INVALID; 1511 + op_ctx->curr_page.l0_table->next_free = op_ctx->unmap.l0_free_tables; 1512 + op_ctx->unmap.l0_free_tables = op_ctx->curr_page.l0_table; 1513 + op_ctx->curr_page.l0_table = NULL; 1514 + 1515 + if (--op_ctx->curr_page.l1_table->entry_count == 0) { 1516 + /* Clear the parent L2 page table entry. */ 1517 + if (op_ctx->curr_page.l1_table->parent_idx != PVR_IDX_INVALID) 1518 + pvr_page_table_l2_remove(op_ctx); 1519 + } 1520 + } 1521 + 1522 + /** 1523 + * pvr_page_table_l0_insert() - Insert an entry referring to a physical page 1524 + * into a level 0 page table. 1525 + * @op_ctx: Target MMU op context pointing at the L0 entry to insert. 1526 + * @dma_addr: Target DMA address to be referenced by the new entry. 1527 + * @flags: Page options to be stored in the new entry. 1528 + * 1529 + * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1530 + * valid L0 entry. 1531 + */ 1532 + static void 1533 + pvr_page_table_l0_insert(struct pvr_mmu_op_context *op_ctx, 1534 + dma_addr_t dma_addr, struct pvr_page_flags_raw flags) 1535 + { 1536 + struct pvr_page_table_l0_entry_raw *entry_raw = 1537 + pvr_page_table_l0_get_entry_raw(op_ctx->curr_page.l0_table, 1538 + op_ctx->curr_page.l0_idx); 1539 + 1540 + pvr_page_table_l0_entry_raw_set(entry_raw, dma_addr, flags); 1541 + 1542 + /* 1543 + * There is no entry to set here - we don't keep a mirror of 1544 + * individual pages. 1545 + */ 1546 + 1547 + ++op_ctx->curr_page.l0_table->entry_count; 1548 + } 1549 + 1550 + /** 1551 + * pvr_page_table_l0_remove() - Remove a physical page from a level 0 page 1552 + * table. 1553 + * @op_ctx: Target MMU op context pointing at the L0 entry to remove. 1554 + * 1555 + * If this function results in the L0 table becoming empty, it will be removed 1556 + * from its parent L1 page table and destroyed. 1557 + * 1558 + * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1559 + * valid L0 entry. 1560 + */ 1561 + static void 1562 + pvr_page_table_l0_remove(struct pvr_mmu_op_context *op_ctx) 1563 + { 1564 + struct pvr_page_table_l0_entry_raw *entry_raw = 1565 + pvr_page_table_l0_get_entry_raw(op_ctx->curr_page.l0_table, 1566 + op_ctx->curr_page.l0_idx); 1567 + 1568 + pvr_page_table_l0_entry_raw_clear(entry_raw); 1569 + 1570 + /* 1571 + * There is no entry to clear here - we don't keep a mirror of 1572 + * individual pages. 1573 + */ 1574 + 1575 + if (--op_ctx->curr_page.l0_table->entry_count == 0) { 1576 + /* Clear the parent L1 page table entry. */ 1577 + if (op_ctx->curr_page.l0_table->parent_idx != PVR_IDX_INVALID) 1578 + pvr_page_table_l1_remove(op_ctx); 1579 + } 1580 + } 1581 + 1582 + /** 1583 + * DOC: Page table index utilities 1584 + */ 1585 + 1586 + /** 1587 + * pvr_page_table_l2_idx() - Calculate the level 2 page table index for a 1588 + * device-virtual address. 1589 + * @device_addr: Target device-virtual address. 1590 + * 1591 + * This function does not perform any bounds checking - it is the caller's 1592 + * responsibility to ensure that @device_addr is valid before interpreting 1593 + * the result. 1594 + * 1595 + * Return: 1596 + * The index into a level 2 page table corresponding to @device_addr. 1597 + */ 1598 + static u16 1599 + pvr_page_table_l2_idx(u64 device_addr) 1600 + { 1601 + return (device_addr & ~ROGUE_MMUCTRL_VADDR_PC_INDEX_CLRMSK) >> 1602 + ROGUE_MMUCTRL_VADDR_PC_INDEX_SHIFT; 1603 + } 1604 + 1605 + /** 1606 + * pvr_page_table_l1_idx() - Calculate the level 1 page table index for a 1607 + * device-virtual address. 1608 + * @device_addr: Target device-virtual address. 1609 + * 1610 + * This function does not perform any bounds checking - it is the caller's 1611 + * responsibility to ensure that @device_addr is valid before interpreting 1612 + * the result. 1613 + * 1614 + * Return: 1615 + * The index into a level 1 page table corresponding to @device_addr. 1616 + */ 1617 + static u16 1618 + pvr_page_table_l1_idx(u64 device_addr) 1619 + { 1620 + return (device_addr & ~ROGUE_MMUCTRL_VADDR_PD_INDEX_CLRMSK) >> 1621 + ROGUE_MMUCTRL_VADDR_PD_INDEX_SHIFT; 1622 + } 1623 + 1624 + /** 1625 + * pvr_page_table_l0_idx() - Calculate the level 0 page table index for a 1626 + * device-virtual address. 1627 + * @device_addr: Target device-virtual address. 1628 + * 1629 + * This function does not perform any bounds checking - it is the caller's 1630 + * responsibility to ensure that @device_addr is valid before interpreting 1631 + * the result. 1632 + * 1633 + * Return: 1634 + * The index into a level 0 page table corresponding to @device_addr. 1635 + */ 1636 + static u16 1637 + pvr_page_table_l0_idx(u64 device_addr) 1638 + { 1639 + return (device_addr & ~ROGUE_MMUCTRL_VADDR_PT_INDEX_CLRMSK) >> 1640 + ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT; 1641 + } 1642 + 1643 + /** 1644 + * DOC: High-level page table operations 1645 + */ 1646 + 1647 + /** 1648 + * pvr_page_table_l1_get_or_insert() - Retrieves (optionally inserting if 1649 + * necessary) a level 1 page table from the specified level 2 page table entry. 1650 + * @op_ctx: Target MMU op context. 1651 + * @should_insert: [IN] Specifies whether new page tables should be inserted 1652 + * when empty page table entries are encountered during traversal. 1653 + * 1654 + * Return: 1655 + * * 0 on success, or 1656 + * 1657 + * If @should_insert is %false: 1658 + * * -%ENXIO if a level 1 page table would have been inserted. 1659 + * 1660 + * If @should_insert is %true: 1661 + * * Any error encountered while inserting the level 1 page table. 1662 + */ 1663 + static int 1664 + pvr_page_table_l1_get_or_insert(struct pvr_mmu_op_context *op_ctx, 1665 + bool should_insert) 1666 + { 1667 + struct pvr_page_table_l2 *l2_table = 1668 + &op_ctx->mmu_ctx->page_table_l2; 1669 + struct pvr_page_table_l1 *table; 1670 + 1671 + if (pvr_page_table_l2_entry_is_valid(l2_table, 1672 + op_ctx->curr_page.l2_idx)) { 1673 + op_ctx->curr_page.l1_table = 1674 + l2_table->entries[op_ctx->curr_page.l2_idx]; 1675 + return 0; 1676 + } 1677 + 1678 + if (!should_insert) 1679 + return -ENXIO; 1680 + 1681 + /* Take a prealloced table. */ 1682 + table = op_ctx->map.l1_prealloc_tables; 1683 + if (!table) 1684 + return -ENOMEM; 1685 + 1686 + /* Pop */ 1687 + op_ctx->map.l1_prealloc_tables = table->next_free; 1688 + table->next_free = NULL; 1689 + 1690 + /* Ensure new table is fully written out before adding to L2 page table. */ 1691 + wmb(); 1692 + 1693 + pvr_page_table_l2_insert(op_ctx, table); 1694 + 1695 + return 0; 1696 + } 1697 + 1698 + /** 1699 + * pvr_page_table_l0_get_or_insert() - Retrieves (optionally inserting if 1700 + * necessary) a level 0 page table from the specified level 1 page table entry. 1701 + * @op_ctx: Target MMU op context. 1702 + * @should_insert: [IN] Specifies whether new page tables should be inserted 1703 + * when empty page table entries are encountered during traversal. 1704 + * 1705 + * Return: 1706 + * * 0 on success, 1707 + * 1708 + * If @should_insert is %false: 1709 + * * -%ENXIO if a level 0 page table would have been inserted. 1710 + * 1711 + * If @should_insert is %true: 1712 + * * Any error encountered while inserting the level 0 page table. 1713 + */ 1714 + static int 1715 + pvr_page_table_l0_get_or_insert(struct pvr_mmu_op_context *op_ctx, 1716 + bool should_insert) 1717 + { 1718 + struct pvr_page_table_l0 *table; 1719 + 1720 + if (pvr_page_table_l1_entry_is_valid(op_ctx->curr_page.l1_table, 1721 + op_ctx->curr_page.l1_idx)) { 1722 + op_ctx->curr_page.l0_table = 1723 + op_ctx->curr_page.l1_table->entries[op_ctx->curr_page.l1_idx]; 1724 + return 0; 1725 + } 1726 + 1727 + if (!should_insert) 1728 + return -ENXIO; 1729 + 1730 + /* Take a prealloced table. */ 1731 + table = op_ctx->map.l0_prealloc_tables; 1732 + if (!table) 1733 + return -ENOMEM; 1734 + 1735 + /* Pop */ 1736 + op_ctx->map.l0_prealloc_tables = table->next_free; 1737 + table->next_free = NULL; 1738 + 1739 + /* Ensure new table is fully written out before adding to L1 page table. */ 1740 + wmb(); 1741 + 1742 + pvr_page_table_l1_insert(op_ctx, table); 1743 + 1744 + return 0; 1745 + } 1746 + 1747 + /** 1748 + * pvr_mmu_context_create() - Create an MMU context. 1749 + * @pvr_dev: PVR device associated with owning VM context. 1750 + * 1751 + * Returns: 1752 + * * Newly created MMU context object on success, or 1753 + * * -%ENOMEM if no memory is available, 1754 + * * Any error code returned by pvr_page_table_l2_init(). 1755 + */ 1756 + struct pvr_mmu_context *pvr_mmu_context_create(struct pvr_device *pvr_dev) 1757 + { 1758 + struct pvr_mmu_context *ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 1759 + int err; 1760 + 1761 + if (!ctx) 1762 + return ERR_PTR(-ENOMEM); 1763 + 1764 + err = pvr_page_table_l2_init(&ctx->page_table_l2, pvr_dev); 1765 + if (err) 1766 + return ERR_PTR(err); 1767 + 1768 + ctx->pvr_dev = pvr_dev; 1769 + 1770 + return ctx; 1771 + } 1772 + 1773 + /** 1774 + * pvr_mmu_context_destroy() - Destroy an MMU context. 1775 + * @ctx: Target MMU context. 1776 + */ 1777 + void pvr_mmu_context_destroy(struct pvr_mmu_context *ctx) 1778 + { 1779 + pvr_page_table_l2_fini(&ctx->page_table_l2); 1780 + kfree(ctx); 1781 + } 1782 + 1783 + /** 1784 + * pvr_mmu_get_root_table_dma_addr() - Get the DMA address of the root of the 1785 + * page table structure behind a VM context. 1786 + * @ctx: Target MMU context. 1787 + */ 1788 + dma_addr_t pvr_mmu_get_root_table_dma_addr(struct pvr_mmu_context *ctx) 1789 + { 1790 + return ctx->page_table_l2.backing_page.dma_addr; 1791 + } 1792 + 1793 + /** 1794 + * pvr_page_table_l1_alloc() - Allocate a l1 page_table object. 1795 + * @ctx: MMU context of owning VM context. 1796 + * 1797 + * Returns: 1798 + * * Newly created page table object on success, or 1799 + * * -%ENOMEM if no memory is available, 1800 + * * Any error code returned by pvr_page_table_l1_init(). 1801 + */ 1802 + static struct pvr_page_table_l1 * 1803 + pvr_page_table_l1_alloc(struct pvr_mmu_context *ctx) 1804 + { 1805 + int err; 1806 + 1807 + struct pvr_page_table_l1 *table = 1808 + kzalloc(sizeof(*table), GFP_KERNEL); 1809 + 1810 + if (!table) 1811 + return ERR_PTR(-ENOMEM); 1812 + 1813 + err = pvr_page_table_l1_init(table, ctx->pvr_dev); 1814 + if (err) { 1815 + kfree(table); 1816 + return ERR_PTR(err); 1817 + } 1818 + 1819 + return table; 1820 + } 1821 + 1822 + /** 1823 + * pvr_page_table_l0_alloc() - Allocate a l0 page_table object. 1824 + * @ctx: MMU context of owning VM context. 1825 + * 1826 + * Returns: 1827 + * * Newly created page table object on success, or 1828 + * * -%ENOMEM if no memory is available, 1829 + * * Any error code returned by pvr_page_table_l0_init(). 1830 + */ 1831 + static struct pvr_page_table_l0 * 1832 + pvr_page_table_l0_alloc(struct pvr_mmu_context *ctx) 1833 + { 1834 + int err; 1835 + 1836 + struct pvr_page_table_l0 *table = 1837 + kzalloc(sizeof(*table), GFP_KERNEL); 1838 + 1839 + if (!table) 1840 + return ERR_PTR(-ENOMEM); 1841 + 1842 + err = pvr_page_table_l0_init(table, ctx->pvr_dev); 1843 + if (err) { 1844 + kfree(table); 1845 + return ERR_PTR(err); 1846 + } 1847 + 1848 + return table; 1849 + } 1850 + 1851 + /** 1852 + * pvr_mmu_op_context_require_sync() - Mark an MMU op context as requiring a 1853 + * sync operation for the referenced page tables up to a specified level. 1854 + * @op_ctx: Target MMU op context. 1855 + * @level: Maximum page table level for which a sync is required. 1856 + */ 1857 + static void 1858 + pvr_mmu_op_context_require_sync(struct pvr_mmu_op_context *op_ctx, 1859 + enum pvr_mmu_sync_level level) 1860 + { 1861 + if (op_ctx->sync_level_required < level) 1862 + op_ctx->sync_level_required = level; 1863 + } 1864 + 1865 + /** 1866 + * pvr_mmu_op_context_sync_manual() - Trigger a sync of some or all of the 1867 + * page tables referenced by a MMU op context. 1868 + * @op_ctx: Target MMU op context. 1869 + * @level: Maximum page table level to sync. 1870 + * 1871 + * Do not call this function directly. Instead use 1872 + * pvr_mmu_op_context_sync_partial() which is checked against the current 1873 + * value of &op_ctx->sync_level_required as set by 1874 + * pvr_mmu_op_context_require_sync(). 1875 + */ 1876 + static void 1877 + pvr_mmu_op_context_sync_manual(struct pvr_mmu_op_context *op_ctx, 1878 + enum pvr_mmu_sync_level level) 1879 + { 1880 + /* 1881 + * We sync the page table levels in ascending order (starting from the 1882 + * leaf node) to ensure consistency. 1883 + */ 1884 + 1885 + WARN_ON(level < PVR_MMU_SYNC_LEVEL_NONE); 1886 + 1887 + if (level <= PVR_MMU_SYNC_LEVEL_NONE) 1888 + return; 1889 + 1890 + if (op_ctx->curr_page.l0_table) 1891 + pvr_page_table_l0_sync(op_ctx->curr_page.l0_table); 1892 + 1893 + if (level < PVR_MMU_SYNC_LEVEL_1) 1894 + return; 1895 + 1896 + if (op_ctx->curr_page.l1_table) 1897 + pvr_page_table_l1_sync(op_ctx->curr_page.l1_table); 1898 + 1899 + if (level < PVR_MMU_SYNC_LEVEL_2) 1900 + return; 1901 + 1902 + pvr_page_table_l2_sync(&op_ctx->mmu_ctx->page_table_l2); 1903 + } 1904 + 1905 + /** 1906 + * pvr_mmu_op_context_sync_partial() - Trigger a sync of some or all of the 1907 + * page tables referenced by a MMU op context. 1908 + * @op_ctx: Target MMU op context. 1909 + * @level: Requested page table level to sync up to (inclusive). 1910 + * 1911 + * If @level is greater than the maximum level recorded by @op_ctx as requiring 1912 + * a sync operation, only the previously recorded maximum will be used. 1913 + * 1914 + * Additionally, if @level is greater than or equal to the maximum level 1915 + * recorded by @op_ctx as requiring a sync operation, that maximum level will be 1916 + * reset as a full sync will be performed. This is equivalent to calling 1917 + * pvr_mmu_op_context_sync(). 1918 + */ 1919 + static void 1920 + pvr_mmu_op_context_sync_partial(struct pvr_mmu_op_context *op_ctx, 1921 + enum pvr_mmu_sync_level level) 1922 + { 1923 + /* 1924 + * If the requested sync level is greater than or equal to the 1925 + * currently required sync level, we do two things: 1926 + * * Don't waste time syncing levels we haven't previously marked as 1927 + * requiring a sync, and 1928 + * * Reset the required sync level since we are about to sync 1929 + * everything that was previously marked as requiring a sync. 1930 + */ 1931 + if (level >= op_ctx->sync_level_required) { 1932 + level = op_ctx->sync_level_required; 1933 + op_ctx->sync_level_required = PVR_MMU_SYNC_LEVEL_NONE; 1934 + } 1935 + 1936 + pvr_mmu_op_context_sync_manual(op_ctx, level); 1937 + } 1938 + 1939 + /** 1940 + * pvr_mmu_op_context_sync() - Trigger a sync of every page table referenced by 1941 + * a MMU op context. 1942 + * @op_ctx: Target MMU op context. 1943 + * 1944 + * The maximum level marked internally as requiring a sync will be reset so 1945 + * that subsequent calls to this function will be no-ops unless @op_ctx is 1946 + * otherwise updated. 1947 + */ 1948 + static void 1949 + pvr_mmu_op_context_sync(struct pvr_mmu_op_context *op_ctx) 1950 + { 1951 + pvr_mmu_op_context_sync_manual(op_ctx, op_ctx->sync_level_required); 1952 + 1953 + op_ctx->sync_level_required = PVR_MMU_SYNC_LEVEL_NONE; 1954 + } 1955 + 1956 + /** 1957 + * pvr_mmu_op_context_load_tables() - Load pointers to tables in each level of 1958 + * the page table tree structure needed to reference the physical page 1959 + * referenced by a MMU op context. 1960 + * @op_ctx: Target MMU op context. 1961 + * @should_create: Specifies whether new page tables should be created when 1962 + * empty page table entries are encountered during traversal. 1963 + * @load_level_required: Maximum page table level to load. 1964 + * 1965 + * If @should_create is %true, this function may modify the stored required 1966 + * sync level of @op_ctx as new page tables are created and inserted into their 1967 + * respective parents. 1968 + * 1969 + * Since there is only one root page table, it is technically incorrect to call 1970 + * this function with a value of @load_level_required greater than or equal to 1971 + * the root level number. However, this is not explicitly disallowed here. 1972 + * 1973 + * Return: 1974 + * * 0 on success, 1975 + * * Any error returned by pvr_page_table_l1_get_or_create() if 1976 + * @load_level_required >= 1 except -%ENXIO, or 1977 + * * Any error returned by pvr_page_table_l0_get_or_create() if 1978 + * @load_level_required >= 0 except -%ENXIO. 1979 + */ 1980 + static int 1981 + pvr_mmu_op_context_load_tables(struct pvr_mmu_op_context *op_ctx, 1982 + bool should_create, 1983 + enum pvr_mmu_sync_level load_level_required) 1984 + { 1985 + const struct pvr_page_table_l1 *l1_head_before = 1986 + op_ctx->map.l1_prealloc_tables; 1987 + const struct pvr_page_table_l0 *l0_head_before = 1988 + op_ctx->map.l0_prealloc_tables; 1989 + int err; 1990 + 1991 + /* Clear tables we're about to fetch in case of error states. */ 1992 + if (load_level_required >= PVR_MMU_SYNC_LEVEL_1) 1993 + op_ctx->curr_page.l1_table = NULL; 1994 + 1995 + if (load_level_required >= PVR_MMU_SYNC_LEVEL_0) 1996 + op_ctx->curr_page.l0_table = NULL; 1997 + 1998 + /* Get or create L1 page table. */ 1999 + if (load_level_required >= PVR_MMU_SYNC_LEVEL_1) { 2000 + err = pvr_page_table_l1_get_or_insert(op_ctx, should_create); 2001 + if (err) { 2002 + /* 2003 + * If @should_create is %false and no L1 page table was 2004 + * found, return early but without an error. Since 2005 + * pvr_page_table_l1_get_or_create() can only return 2006 + * -%ENXIO if @should_create is %false, there is no 2007 + * need to check it here. 2008 + */ 2009 + if (err == -ENXIO) 2010 + err = 0; 2011 + 2012 + return err; 2013 + } 2014 + } 2015 + 2016 + /* Get or create L0 page table. */ 2017 + if (load_level_required >= PVR_MMU_SYNC_LEVEL_0) { 2018 + err = pvr_page_table_l0_get_or_insert(op_ctx, should_create); 2019 + if (err) { 2020 + /* 2021 + * If @should_create is %false and no L0 page table was 2022 + * found, return early but without an error. Since 2023 + * pvr_page_table_l0_get_or_insert() can only return 2024 + * -%ENXIO if @should_create is %false, there is no 2025 + * need to check it here. 2026 + */ 2027 + if (err == -ENXIO) 2028 + err = 0; 2029 + 2030 + /* 2031 + * At this point, an L1 page table could have been 2032 + * inserted but is now empty due to the failed attempt 2033 + * at inserting an L0 page table. In this instance, we 2034 + * must remove the empty L1 page table ourselves as 2035 + * pvr_page_table_l1_remove() is never called as part 2036 + * of the error path in 2037 + * pvr_page_table_l0_get_or_insert(). 2038 + */ 2039 + if (l1_head_before != op_ctx->map.l1_prealloc_tables) { 2040 + pvr_page_table_l2_remove(op_ctx); 2041 + pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_2); 2042 + } 2043 + 2044 + return err; 2045 + } 2046 + } 2047 + 2048 + /* 2049 + * A sync is only needed if table objects were inserted. This can be 2050 + * inferred by checking if the pointer at the head of the linked list 2051 + * has changed. 2052 + */ 2053 + if (l1_head_before != op_ctx->map.l1_prealloc_tables) 2054 + pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_2); 2055 + else if (l0_head_before != op_ctx->map.l0_prealloc_tables) 2056 + pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_1); 2057 + 2058 + return 0; 2059 + } 2060 + 2061 + /** 2062 + * pvr_mmu_op_context_set_curr_page() - Reassign the current page of an MMU op 2063 + * context, syncing any page tables previously assigned to it which are no 2064 + * longer relevant. 2065 + * @op_ctx: Target MMU op context. 2066 + * @device_addr: New pointer target. 2067 + * @should_create: Specify whether new page tables should be created when 2068 + * empty page table entries are encountered during traversal. 2069 + * 2070 + * This function performs a full sync on the pointer, regardless of which 2071 + * levels are modified. 2072 + * 2073 + * Return: 2074 + * * 0 on success, or 2075 + * * Any error returned by pvr_mmu_op_context_load_tables(). 2076 + */ 2077 + static int 2078 + pvr_mmu_op_context_set_curr_page(struct pvr_mmu_op_context *op_ctx, 2079 + u64 device_addr, bool should_create) 2080 + { 2081 + pvr_mmu_op_context_sync(op_ctx); 2082 + 2083 + op_ctx->curr_page.l2_idx = pvr_page_table_l2_idx(device_addr); 2084 + op_ctx->curr_page.l1_idx = pvr_page_table_l1_idx(device_addr); 2085 + op_ctx->curr_page.l0_idx = pvr_page_table_l0_idx(device_addr); 2086 + op_ctx->curr_page.l1_table = NULL; 2087 + op_ctx->curr_page.l0_table = NULL; 2088 + 2089 + return pvr_mmu_op_context_load_tables(op_ctx, should_create, 2090 + PVR_MMU_SYNC_LEVEL_1); 2091 + } 2092 + 2093 + /** 2094 + * pvr_mmu_op_context_next_page() - Advance the current page of an MMU op 2095 + * context. 2096 + * @op_ctx: Target MMU op context. 2097 + * @should_create: Specify whether new page tables should be created when 2098 + * empty page table entries are encountered during traversal. 2099 + * 2100 + * If @should_create is %false, it is the caller's responsibility to verify that 2101 + * the state of the table references in @op_ctx is valid on return. If -%ENXIO 2102 + * is returned, at least one of the table references is invalid. It should be 2103 + * noted that @op_ctx as a whole will be left in a valid state if -%ENXIO is 2104 + * returned, unlike other error codes. The caller should check which references 2105 + * are invalid by comparing them to %NULL. Only &@ptr->l2_table is guaranteed 2106 + * to be valid, since it represents the root of the page table tree structure. 2107 + * 2108 + * Return: 2109 + * * 0 on success, 2110 + * * -%EPERM if the operation would wrap at the top of the page table 2111 + * hierarchy, 2112 + * * -%ENXIO if @should_create is %false and a page table of any level would 2113 + * have otherwise been created, or 2114 + * * Any error returned while attempting to create missing page tables if 2115 + * @should_create is %true. 2116 + */ 2117 + static int 2118 + pvr_mmu_op_context_next_page(struct pvr_mmu_op_context *op_ctx, 2119 + bool should_create) 2120 + { 2121 + s8 load_level_required = PVR_MMU_SYNC_LEVEL_NONE; 2122 + 2123 + if (++op_ctx->curr_page.l0_idx != ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X) 2124 + goto load_tables; 2125 + 2126 + op_ctx->curr_page.l0_idx = 0; 2127 + load_level_required = PVR_MMU_SYNC_LEVEL_0; 2128 + 2129 + if (++op_ctx->curr_page.l1_idx != ROGUE_MMUCTRL_ENTRIES_PD_VALUE) 2130 + goto load_tables; 2131 + 2132 + op_ctx->curr_page.l1_idx = 0; 2133 + load_level_required = PVR_MMU_SYNC_LEVEL_1; 2134 + 2135 + if (++op_ctx->curr_page.l2_idx != ROGUE_MMUCTRL_ENTRIES_PC_VALUE) 2136 + goto load_tables; 2137 + 2138 + /* 2139 + * If the pattern continued, we would set &op_ctx->curr_page.l2_idx to 2140 + * zero here. However, that would wrap the top layer of the page table 2141 + * hierarchy which is not a valid operation. Instead, we warn and return 2142 + * an error. 2143 + */ 2144 + WARN(true, 2145 + "%s(%p) attempted to loop the top of the page table hierarchy", 2146 + __func__, op_ctx); 2147 + return -EPERM; 2148 + 2149 + /* If indices have wrapped, we need to load new tables. */ 2150 + load_tables: 2151 + /* First, flush tables which will be unloaded. */ 2152 + pvr_mmu_op_context_sync_partial(op_ctx, load_level_required); 2153 + 2154 + /* Then load tables from the required level down. */ 2155 + return pvr_mmu_op_context_load_tables(op_ctx, should_create, 2156 + load_level_required); 2157 + } 2158 + 2159 + /** 2160 + * DOC: Single page operations 2161 + */ 2162 + 2163 + /** 2164 + * pvr_page_create() - Create a device-virtual memory page and insert it into 2165 + * a level 0 page table. 2166 + * @op_ctx: Target MMU op context pointing at the device-virtual address of the 2167 + * target page. 2168 + * @dma_addr: DMA address of the physical page backing the created page. 2169 + * @flags: Page options saved on the level 0 page table entry for reading by 2170 + * the device. 2171 + * 2172 + * Return: 2173 + * * 0 on success, or 2174 + * * -%EEXIST if the requested page already exists. 2175 + */ 2176 + static int 2177 + pvr_page_create(struct pvr_mmu_op_context *op_ctx, dma_addr_t dma_addr, 2178 + struct pvr_page_flags_raw flags) 2179 + { 2180 + /* Do not create a new page if one already exists. */ 2181 + if (pvr_page_table_l0_entry_is_valid(op_ctx->curr_page.l0_table, 2182 + op_ctx->curr_page.l0_idx)) { 2183 + return -EEXIST; 2184 + } 2185 + 2186 + pvr_page_table_l0_insert(op_ctx, dma_addr, flags); 2187 + 2188 + pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_0); 2189 + 2190 + return 0; 2191 + } 2192 + 2193 + /** 2194 + * pvr_page_destroy() - Destroy a device page after removing it from its 2195 + * parent level 0 page table. 2196 + * @op_ctx: Target MMU op context. 2197 + */ 2198 + static void 2199 + pvr_page_destroy(struct pvr_mmu_op_context *op_ctx) 2200 + { 2201 + /* Do nothing if the page does not exist. */ 2202 + if (!pvr_page_table_l0_entry_is_valid(op_ctx->curr_page.l0_table, 2203 + op_ctx->curr_page.l0_idx)) { 2204 + return; 2205 + } 2206 + 2207 + /* Clear the parent L0 page table entry. */ 2208 + pvr_page_table_l0_remove(op_ctx); 2209 + 2210 + pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_0); 2211 + } 2212 + 2213 + /** 2214 + * pvr_mmu_op_context_destroy() - Destroy an MMU op context. 2215 + * @op_ctx: Target MMU op context. 2216 + */ 2217 + void pvr_mmu_op_context_destroy(struct pvr_mmu_op_context *op_ctx) 2218 + { 2219 + const bool flush_caches = 2220 + op_ctx->sync_level_required != PVR_MMU_SYNC_LEVEL_NONE; 2221 + 2222 + pvr_mmu_op_context_sync(op_ctx); 2223 + 2224 + /* Unmaps should be flushed immediately. Map flushes can be deferred. */ 2225 + if (flush_caches && !op_ctx->map.sgt) 2226 + pvr_mmu_flush_exec(op_ctx->mmu_ctx->pvr_dev, true); 2227 + 2228 + while (op_ctx->map.l0_prealloc_tables) { 2229 + struct pvr_page_table_l0 *tmp = op_ctx->map.l0_prealloc_tables; 2230 + 2231 + op_ctx->map.l0_prealloc_tables = 2232 + op_ctx->map.l0_prealloc_tables->next_free; 2233 + pvr_page_table_l0_free(tmp); 2234 + } 2235 + 2236 + while (op_ctx->map.l1_prealloc_tables) { 2237 + struct pvr_page_table_l1 *tmp = op_ctx->map.l1_prealloc_tables; 2238 + 2239 + op_ctx->map.l1_prealloc_tables = 2240 + op_ctx->map.l1_prealloc_tables->next_free; 2241 + pvr_page_table_l1_free(tmp); 2242 + } 2243 + 2244 + while (op_ctx->unmap.l0_free_tables) { 2245 + struct pvr_page_table_l0 *tmp = op_ctx->unmap.l0_free_tables; 2246 + 2247 + op_ctx->unmap.l0_free_tables = 2248 + op_ctx->unmap.l0_free_tables->next_free; 2249 + pvr_page_table_l0_free(tmp); 2250 + } 2251 + 2252 + while (op_ctx->unmap.l1_free_tables) { 2253 + struct pvr_page_table_l1 *tmp = op_ctx->unmap.l1_free_tables; 2254 + 2255 + op_ctx->unmap.l1_free_tables = 2256 + op_ctx->unmap.l1_free_tables->next_free; 2257 + pvr_page_table_l1_free(tmp); 2258 + } 2259 + 2260 + kfree(op_ctx); 2261 + } 2262 + 2263 + /** 2264 + * pvr_mmu_op_context_create() - Create an MMU op context. 2265 + * @ctx: MMU context associated with owning VM context. 2266 + * @sgt: Scatter gather table containing pages pinned for use by this context. 2267 + * @sgt_offset: Start offset of the requested device-virtual memory mapping. 2268 + * @size: Size in bytes of the requested device-virtual memory mapping. For an 2269 + * unmapping, this should be zero so that no page tables are allocated. 2270 + * 2271 + * Returns: 2272 + * * Newly created MMU op context object on success, or 2273 + * * -%ENOMEM if no memory is available, 2274 + * * Any error code returned by pvr_page_table_l2_init(). 2275 + */ 2276 + struct pvr_mmu_op_context * 2277 + pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, struct sg_table *sgt, 2278 + u64 sgt_offset, u64 size) 2279 + { 2280 + int err; 2281 + 2282 + struct pvr_mmu_op_context *op_ctx = 2283 + kzalloc(sizeof(*op_ctx), GFP_KERNEL); 2284 + 2285 + if (!op_ctx) 2286 + return ERR_PTR(-ENOMEM); 2287 + 2288 + op_ctx->mmu_ctx = ctx; 2289 + op_ctx->map.sgt = sgt; 2290 + op_ctx->map.sgt_offset = sgt_offset; 2291 + op_ctx->sync_level_required = PVR_MMU_SYNC_LEVEL_NONE; 2292 + 2293 + if (size) { 2294 + /* 2295 + * The number of page table objects we need to prealloc is 2296 + * indicated by the mapping size, start offset and the sizes 2297 + * of the areas mapped per PT or PD. The range calculation is 2298 + * identical to that for the index into a table for a device 2299 + * address, so we reuse those functions here. 2300 + */ 2301 + const u32 l1_start_idx = pvr_page_table_l2_idx(sgt_offset); 2302 + const u32 l1_end_idx = pvr_page_table_l2_idx(sgt_offset + size); 2303 + const u32 l1_count = l1_end_idx - l1_start_idx + 1; 2304 + const u32 l0_start_idx = pvr_page_table_l1_idx(sgt_offset); 2305 + const u32 l0_end_idx = pvr_page_table_l1_idx(sgt_offset + size); 2306 + const u32 l0_count = l0_end_idx - l0_start_idx + 1; 2307 + 2308 + /* 2309 + * Alloc and push page table entries until we have enough of 2310 + * each type, ending with linked lists of l0 and l1 entries in 2311 + * reverse order. 2312 + */ 2313 + for (int i = 0; i < l1_count; i++) { 2314 + struct pvr_page_table_l1 *l1_tmp = 2315 + pvr_page_table_l1_alloc(ctx); 2316 + 2317 + err = PTR_ERR_OR_ZERO(l1_tmp); 2318 + if (err) 2319 + goto err_cleanup; 2320 + 2321 + l1_tmp->next_free = op_ctx->map.l1_prealloc_tables; 2322 + op_ctx->map.l1_prealloc_tables = l1_tmp; 2323 + } 2324 + 2325 + for (int i = 0; i < l0_count; i++) { 2326 + struct pvr_page_table_l0 *l0_tmp = 2327 + pvr_page_table_l0_alloc(ctx); 2328 + 2329 + err = PTR_ERR_OR_ZERO(l0_tmp); 2330 + if (err) 2331 + goto err_cleanup; 2332 + 2333 + l0_tmp->next_free = op_ctx->map.l0_prealloc_tables; 2334 + op_ctx->map.l0_prealloc_tables = l0_tmp; 2335 + } 2336 + } 2337 + 2338 + return op_ctx; 2339 + 2340 + err_cleanup: 2341 + pvr_mmu_op_context_destroy(op_ctx); 2342 + 2343 + return ERR_PTR(err); 2344 + } 2345 + 2346 + /** 2347 + * pvr_mmu_op_context_unmap_curr_page() - Unmap pages from a memory context 2348 + * starting from the current page of an MMU op context. 2349 + * @op_ctx: Target MMU op context pointing at the first page to unmap. 2350 + * @nr_pages: Number of pages to unmap. 2351 + * 2352 + * Return: 2353 + * * 0 on success, or 2354 + * * Any error encountered while advancing @op_ctx.curr_page with 2355 + * pvr_mmu_op_context_next_page() (except -%ENXIO). 2356 + */ 2357 + static int 2358 + pvr_mmu_op_context_unmap_curr_page(struct pvr_mmu_op_context *op_ctx, 2359 + u64 nr_pages) 2360 + { 2361 + int err; 2362 + 2363 + if (nr_pages == 0) 2364 + return 0; 2365 + 2366 + /* 2367 + * Destroy first page outside loop, as it doesn't require a page 2368 + * advance beforehand. If the L0 page table reference in 2369 + * @op_ctx.curr_page is %NULL, there cannot be a mapped page at 2370 + * @op_ctx.curr_page (so skip ahead). 2371 + */ 2372 + if (op_ctx->curr_page.l0_table) 2373 + pvr_page_destroy(op_ctx); 2374 + 2375 + for (u64 page = 1; page < nr_pages; ++page) { 2376 + err = pvr_mmu_op_context_next_page(op_ctx, false); 2377 + /* 2378 + * If the page table tree structure at @op_ctx.curr_page is 2379 + * incomplete, skip ahead. We don't care about unmapping pages 2380 + * that cannot exist. 2381 + * 2382 + * FIXME: This could be made more efficient by jumping ahead 2383 + * using pvr_mmu_op_context_set_curr_page(). 2384 + */ 2385 + if (err == -ENXIO) 2386 + continue; 2387 + else if (err) 2388 + return err; 2389 + 2390 + pvr_page_destroy(op_ctx); 2391 + } 2392 + 2393 + return 0; 2394 + } 2395 + 2396 + /** 2397 + * pvr_mmu_unmap() - Unmap pages from a memory context. 2398 + * @op_ctx: Target MMU op context. 2399 + * @device_addr: First device-virtual address to unmap. 2400 + * @size: Size in bytes to unmap. 2401 + * 2402 + * The total amount of device-virtual memory unmapped is 2403 + * @nr_pages * %PVR_DEVICE_PAGE_SIZE. 2404 + * 2405 + * Returns: 2406 + * * 0 on success, or 2407 + * * Any error code returned by pvr_page_table_ptr_init(), or 2408 + * * Any error code returned by pvr_page_table_ptr_unmap(). 2409 + */ 2410 + int pvr_mmu_unmap(struct pvr_mmu_op_context *op_ctx, u64 device_addr, u64 size) 2411 + { 2412 + int err = pvr_mmu_op_context_set_curr_page(op_ctx, device_addr, false); 2413 + 2414 + if (err) 2415 + return err; 2416 + 2417 + return pvr_mmu_op_context_unmap_curr_page(op_ctx, 2418 + size >> PVR_DEVICE_PAGE_SHIFT); 2419 + } 2420 + 2421 + /** 2422 + * pvr_mmu_map_sgl() - Map part of a scatter-gather table entry to 2423 + * device-virtual memory. 2424 + * @op_ctx: Target MMU op context pointing to the first page that should be 2425 + * mapped. 2426 + * @sgl: Target scatter-gather table entry. 2427 + * @offset: Offset into @sgl to map from. Must result in a starting address 2428 + * from @sgl which is CPU page-aligned. 2429 + * @size: Size of the memory to be mapped in bytes. Must be a non-zero multiple 2430 + * of the device page size. 2431 + * @page_flags: Page options to be applied to every device-virtual memory page 2432 + * in the created mapping. 2433 + * 2434 + * Return: 2435 + * * 0 on success, 2436 + * * -%EINVAL if the range specified by @offset and @size is not completely 2437 + * within @sgl, or 2438 + * * Any error encountered while creating a page with pvr_page_create(), or 2439 + * * Any error encountered while advancing @op_ctx.curr_page with 2440 + * pvr_mmu_op_context_next_page(). 2441 + */ 2442 + static int 2443 + pvr_mmu_map_sgl(struct pvr_mmu_op_context *op_ctx, struct scatterlist *sgl, 2444 + u64 offset, u64 size, struct pvr_page_flags_raw page_flags) 2445 + { 2446 + const unsigned int pages = size >> PVR_DEVICE_PAGE_SHIFT; 2447 + dma_addr_t dma_addr = sg_dma_address(sgl) + offset; 2448 + const unsigned int dma_len = sg_dma_len(sgl); 2449 + struct pvr_page_table_ptr ptr_copy; 2450 + unsigned int page; 2451 + int err; 2452 + 2453 + if (size > dma_len || offset > dma_len - size) 2454 + return -EINVAL; 2455 + 2456 + /* 2457 + * Before progressing, save a copy of the start pointer so we can use 2458 + * it again if we enter an error state and have to destroy pages. 2459 + */ 2460 + memcpy(&ptr_copy, &op_ctx->curr_page, sizeof(ptr_copy)); 2461 + 2462 + /* 2463 + * Create first page outside loop, as it doesn't require a page advance 2464 + * beforehand. 2465 + */ 2466 + err = pvr_page_create(op_ctx, dma_addr, page_flags); 2467 + if (err) 2468 + return err; 2469 + 2470 + for (page = 1; page < pages; ++page) { 2471 + err = pvr_mmu_op_context_next_page(op_ctx, true); 2472 + if (err) 2473 + goto err_destroy_pages; 2474 + 2475 + dma_addr += PVR_DEVICE_PAGE_SIZE; 2476 + 2477 + err = pvr_page_create(op_ctx, dma_addr, page_flags); 2478 + if (err) 2479 + goto err_destroy_pages; 2480 + } 2481 + 2482 + return 0; 2483 + 2484 + err_destroy_pages: 2485 + memcpy(&op_ctx->curr_page, &ptr_copy, sizeof(op_ctx->curr_page)); 2486 + err = pvr_mmu_op_context_unmap_curr_page(op_ctx, page); 2487 + 2488 + return err; 2489 + } 2490 + 2491 + /** 2492 + * pvr_mmu_map() - Map an object's virtual memory to physical memory. 2493 + * @op_ctx: Target MMU op context. 2494 + * @size: Size of memory to be mapped in bytes. Must be a non-zero multiple 2495 + * of the device page size. 2496 + * @flags: Flags from pvr_gem_object associated with the mapping. 2497 + * @device_addr: Virtual device address to map to. Must be device page-aligned. 2498 + * 2499 + * Returns: 2500 + * * 0 on success, or 2501 + * * Any error code returned by pvr_page_table_ptr_init(), or 2502 + * * Any error code returned by pvr_mmu_map_sgl(), or 2503 + * * Any error code returned by pvr_page_table_ptr_next_page(). 2504 + */ 2505 + int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags, 2506 + u64 device_addr) 2507 + { 2508 + struct pvr_page_table_ptr ptr_copy; 2509 + struct pvr_page_flags_raw flags_raw; 2510 + struct scatterlist *sgl; 2511 + u64 mapped_size = 0; 2512 + unsigned int count; 2513 + int err; 2514 + 2515 + if (!size) 2516 + return 0; 2517 + 2518 + if ((op_ctx->map.sgt_offset | size) & ~PVR_DEVICE_PAGE_MASK) 2519 + return -EINVAL; 2520 + 2521 + err = pvr_mmu_op_context_set_curr_page(op_ctx, device_addr, true); 2522 + if (err) 2523 + return -EINVAL; 2524 + 2525 + memcpy(&ptr_copy, &op_ctx->curr_page, sizeof(ptr_copy)); 2526 + 2527 + flags_raw = pvr_page_flags_raw_create(false, false, 2528 + flags & DRM_PVR_BO_BYPASS_DEVICE_CACHE, 2529 + flags & DRM_PVR_BO_PM_FW_PROTECT); 2530 + 2531 + /* Map scatter gather table */ 2532 + for_each_sgtable_dma_sg(op_ctx->map.sgt, sgl, count) { 2533 + const size_t sgl_len = sg_dma_len(sgl); 2534 + u64 sgl_offset, map_sgl_len; 2535 + 2536 + if (sgl_len <= op_ctx->map.sgt_offset) { 2537 + op_ctx->map.sgt_offset -= sgl_len; 2538 + continue; 2539 + } 2540 + 2541 + sgl_offset = op_ctx->map.sgt_offset; 2542 + map_sgl_len = min_t(u64, sgl_len - sgl_offset, size - mapped_size); 2543 + 2544 + err = pvr_mmu_map_sgl(op_ctx, sgl, sgl_offset, map_sgl_len, 2545 + flags_raw); 2546 + if (err) 2547 + break; 2548 + 2549 + /* 2550 + * Flag the L0 page table as requiring a flush when the MMU op 2551 + * context is destroyed. 2552 + */ 2553 + pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_0); 2554 + 2555 + op_ctx->map.sgt_offset = 0; 2556 + mapped_size += map_sgl_len; 2557 + 2558 + if (mapped_size >= size) 2559 + break; 2560 + 2561 + err = pvr_mmu_op_context_next_page(op_ctx, true); 2562 + if (err) 2563 + break; 2564 + } 2565 + 2566 + if (err && mapped_size) { 2567 + memcpy(&op_ctx->curr_page, &ptr_copy, sizeof(op_ctx->curr_page)); 2568 + pvr_mmu_op_context_unmap_curr_page(op_ctx, 2569 + mapped_size >> PVR_DEVICE_PAGE_SHIFT); 2570 + } 2571 + 2572 + return err; 2573 + }
+108
drivers/gpu/drm/imagination/pvr_mmu.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only OR MIT */ 2 + /* Copyright (c) 2023 Imagination Technologies Ltd. */ 3 + 4 + #ifndef PVR_MMU_H 5 + #define PVR_MMU_H 6 + 7 + #include <linux/memory.h> 8 + #include <linux/types.h> 9 + 10 + /* Forward declaration from "pvr_device.h" */ 11 + struct pvr_device; 12 + 13 + /* Forward declaration from "pvr_mmu.c" */ 14 + struct pvr_mmu_context; 15 + struct pvr_mmu_op_context; 16 + 17 + /* Forward declaration from "pvr_vm.c" */ 18 + struct pvr_vm_context; 19 + 20 + /* Forward declaration from <linux/scatterlist.h> */ 21 + struct sg_table; 22 + 23 + /** 24 + * DOC: Public API (constants) 25 + * 26 + * .. c:macro:: PVR_DEVICE_PAGE_SIZE 27 + * 28 + * Fixed page size referenced by leaf nodes in the page table tree 29 + * structure. In the current implementation, this value is pegged to the 30 + * CPU page size (%PAGE_SIZE). It is therefore an error to specify a CPU 31 + * page size which is not also a supported device page size. The supported 32 + * device page sizes are: 4KiB, 16KiB, 64KiB, 256KiB, 1MiB and 2MiB. 33 + * 34 + * .. c:macro:: PVR_DEVICE_PAGE_SHIFT 35 + * 36 + * Shift value used to efficiently multiply or divide by 37 + * %PVR_DEVICE_PAGE_SIZE. 38 + * 39 + * This value is derived from %PVR_DEVICE_PAGE_SIZE. 40 + * 41 + * .. c:macro:: PVR_DEVICE_PAGE_MASK 42 + * 43 + * Mask used to round a value down to the nearest multiple of 44 + * %PVR_DEVICE_PAGE_SIZE. When bitwise negated, it will indicate whether a 45 + * value is already a multiple of %PVR_DEVICE_PAGE_SIZE. 46 + * 47 + * This value is derived from %PVR_DEVICE_PAGE_SIZE. 48 + */ 49 + 50 + /* PVR_DEVICE_PAGE_SIZE determines the page size */ 51 + #define PVR_DEVICE_PAGE_SIZE (PAGE_SIZE) 52 + #define PVR_DEVICE_PAGE_SHIFT (PAGE_SHIFT) 53 + #define PVR_DEVICE_PAGE_MASK (PAGE_MASK) 54 + 55 + /** 56 + * DOC: Page table index utilities (constants) 57 + * 58 + * .. c:macro:: PVR_PAGE_TABLE_ADDR_SPACE_SIZE 59 + * 60 + * Size of device-virtual address space which can be represented in the page 61 + * table structure. 62 + * 63 + * This value is checked at runtime against 64 + * &pvr_device_features.virtual_address_space_bits by 65 + * pvr_vm_create_context(), which will return an error if the feature value 66 + * does not match this constant. 67 + * 68 + * .. admonition:: Future work 69 + * 70 + * It should be possible to support other values of 71 + * &pvr_device_features.virtual_address_space_bits, but so far no 72 + * hardware has been created which advertises an unsupported value. 73 + * 74 + * .. c:macro:: PVR_PAGE_TABLE_ADDR_BITS 75 + * 76 + * Number of bits needed to represent any value less than 77 + * %PVR_PAGE_TABLE_ADDR_SPACE_SIZE exactly. 78 + * 79 + * .. c:macro:: PVR_PAGE_TABLE_ADDR_MASK 80 + * 81 + * Bitmask of device-virtual addresses which are valid in the page table 82 + * structure. 83 + * 84 + * This value is derived from %PVR_PAGE_TABLE_ADDR_SPACE_SIZE, so the same 85 + * notes on that constant apply here. 86 + */ 87 + #define PVR_PAGE_TABLE_ADDR_SPACE_SIZE SZ_1T 88 + #define PVR_PAGE_TABLE_ADDR_BITS __ffs(PVR_PAGE_TABLE_ADDR_SPACE_SIZE) 89 + #define PVR_PAGE_TABLE_ADDR_MASK (PVR_PAGE_TABLE_ADDR_SPACE_SIZE - 1) 90 + 91 + void pvr_mmu_flush_request_all(struct pvr_device *pvr_dev); 92 + int pvr_mmu_flush_exec(struct pvr_device *pvr_dev, bool wait); 93 + 94 + struct pvr_mmu_context *pvr_mmu_context_create(struct pvr_device *pvr_dev); 95 + void pvr_mmu_context_destroy(struct pvr_mmu_context *ctx); 96 + 97 + dma_addr_t pvr_mmu_get_root_table_dma_addr(struct pvr_mmu_context *ctx); 98 + 99 + void pvr_mmu_op_context_destroy(struct pvr_mmu_op_context *op_ctx); 100 + struct pvr_mmu_op_context * 101 + pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, 102 + struct sg_table *sgt, u64 sgt_offset, u64 size); 103 + 104 + int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags, 105 + u64 device_addr); 106 + int pvr_mmu_unmap(struct pvr_mmu_op_context *op_ctx, u64 device_addr, u64 size); 107 + 108 + #endif /* PVR_MMU_H */
+1091
drivers/gpu/drm/imagination/pvr_vm.c
··· 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 + 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> 24 + 25 + /** 26 + * DOC: Memory context 27 + * 28 + * This is the "top level" datatype in the VM code. It's exposed in the public 29 + * API as an opaque handle. 30 + */ 31 + 32 + /** 33 + * struct pvr_vm_context - Context type used to represent a single VM. 34 + */ 35 + struct pvr_vm_context { 36 + /** 37 + * @pvr_dev: The PowerVR device to which this context is bound. 38 + * This binding is immutable for the life of the context. 39 + */ 40 + struct pvr_device *pvr_dev; 41 + 42 + /** @mmu_ctx: The context for binding to physical memory. */ 43 + struct pvr_mmu_context *mmu_ctx; 44 + 45 + /** @gpuva_mgr: GPUVA manager object associated with this context. */ 46 + struct drm_gpuvm gpuvm_mgr; 47 + 48 + /** @lock: Global lock on this VM. */ 49 + struct mutex lock; 50 + 51 + /** 52 + * @fw_mem_ctx_obj: Firmware object representing firmware memory 53 + * context. 54 + */ 55 + struct pvr_fw_object *fw_mem_ctx_obj; 56 + 57 + /** @ref_count: Reference count of object. */ 58 + struct kref ref_count; 59 + 60 + /** 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. 63 + */ 64 + struct drm_gem_object dummy_gem; 65 + }; 66 + 67 + struct pvr_vm_context *pvr_vm_context_get(struct pvr_vm_context *vm_ctx) 68 + { 69 + if (vm_ctx) 70 + kref_get(&vm_ctx->ref_count); 71 + 72 + return vm_ctx; 73 + } 74 + 75 + /** 76 + * pvr_vm_get_page_table_root_addr() - Get the DMA address of the root of the 77 + * page table structure behind a VM context. 78 + * @vm_ctx: Target VM context. 79 + */ 80 + dma_addr_t pvr_vm_get_page_table_root_addr(struct pvr_vm_context *vm_ctx) 81 + { 82 + return pvr_mmu_get_root_table_dma_addr(vm_ctx->mmu_ctx); 83 + } 84 + 85 + /** 86 + * pvr_vm_get_dma_resv() - Expose the dma_resv owned by the VM context. 87 + * @vm_ctx: Target VM context. 88 + * 89 + * This is used to allow private BOs to share a dma_resv for faster fence 90 + * updates. 91 + * 92 + * Returns: The dma_resv pointer. 93 + */ 94 + struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx) 95 + { 96 + return vm_ctx->dummy_gem.resv; 97 + } 98 + 99 + /** 100 + * DOC: Memory mappings 101 + */ 102 + 103 + /** 104 + * struct pvr_vm_gpuva - Wrapper type representing a single VM mapping. 105 + */ 106 + struct pvr_vm_gpuva { 107 + /** @base: The wrapped drm_gpuva object. */ 108 + struct drm_gpuva base; 109 + }; 110 + 111 + static __always_inline 112 + struct pvr_vm_gpuva *to_pvr_vm_gpuva(struct drm_gpuva *gpuva) 113 + { 114 + return container_of(gpuva, struct pvr_vm_gpuva, base); 115 + } 116 + 117 + enum pvr_vm_bind_type { 118 + PVR_VM_BIND_TYPE_MAP, 119 + PVR_VM_BIND_TYPE_UNMAP, 120 + }; 121 + 122 + /** 123 + * struct pvr_vm_bind_op - Context of a map/unmap operation. 124 + */ 125 + struct pvr_vm_bind_op { 126 + /** @type: Map or unmap. */ 127 + enum pvr_vm_bind_type type; 128 + 129 + /** @pvr_obj: Object associated with mapping (map only). */ 130 + struct pvr_gem_object *pvr_obj; 131 + 132 + /** 133 + * @vm_ctx: VM context where the mapping will be created or destroyed. 134 + */ 135 + struct pvr_vm_context *vm_ctx; 136 + 137 + /** @mmu_op_ctx: MMU op context. */ 138 + struct pvr_mmu_op_context *mmu_op_ctx; 139 + 140 + /** @gpuvm_bo: Prealloced wrapped BO for attaching to the gpuvm. */ 141 + struct drm_gpuvm_bo *gpuvm_bo; 142 + 143 + /** 144 + * @new_va: Prealloced VA mapping object (init in callback). 145 + * Used when creating a mapping. 146 + */ 147 + struct pvr_vm_gpuva *new_va; 148 + 149 + /** 150 + * @prev_va: Prealloced VA mapping object (init in callback). 151 + * Used when a mapping or unmapping operation overlaps an existing 152 + * mapping and splits away the beginning into a new mapping. 153 + */ 154 + struct pvr_vm_gpuva *prev_va; 155 + 156 + /** 157 + * @next_va: Prealloced VA mapping object (init in callback). 158 + * Used when a mapping or unmapping operation overlaps an existing 159 + * mapping and splits away the end into a new mapping. 160 + */ 161 + struct pvr_vm_gpuva *next_va; 162 + 163 + /** @offset: Offset into @pvr_obj to begin mapping from. */ 164 + u64 offset; 165 + 166 + /** @device_addr: Device-virtual address at the start of the mapping. */ 167 + u64 device_addr; 168 + 169 + /** @size: Size of the desired mapping. */ 170 + u64 size; 171 + }; 172 + 173 + /** 174 + * pvr_vm_bind_op_exec() - Execute a single bind op. 175 + * @bind_op: Bind op context. 176 + * 177 + * Returns: 178 + * * 0 on success, 179 + * * Any error code returned by drm_gpuva_sm_map(), drm_gpuva_sm_unmap(), or 180 + * a callback function. 181 + */ 182 + static int pvr_vm_bind_op_exec(struct pvr_vm_bind_op *bind_op) 183 + { 184 + switch (bind_op->type) { 185 + case PVR_VM_BIND_TYPE_MAP: 186 + return drm_gpuvm_sm_map(&bind_op->vm_ctx->gpuvm_mgr, 187 + bind_op, bind_op->device_addr, 188 + bind_op->size, 189 + gem_from_pvr_gem(bind_op->pvr_obj), 190 + bind_op->offset); 191 + 192 + case PVR_VM_BIND_TYPE_UNMAP: 193 + return drm_gpuvm_sm_unmap(&bind_op->vm_ctx->gpuvm_mgr, 194 + bind_op, bind_op->device_addr, 195 + bind_op->size); 196 + } 197 + 198 + /* 199 + * This shouldn't happen unless something went wrong 200 + * in drm_sched. 201 + */ 202 + WARN_ON(1); 203 + return -EINVAL; 204 + } 205 + 206 + static void pvr_vm_bind_op_fini(struct pvr_vm_bind_op *bind_op) 207 + { 208 + drm_gpuvm_bo_put(bind_op->gpuvm_bo); 209 + 210 + kfree(bind_op->new_va); 211 + kfree(bind_op->prev_va); 212 + kfree(bind_op->next_va); 213 + 214 + if (bind_op->pvr_obj) 215 + pvr_gem_object_put(bind_op->pvr_obj); 216 + 217 + if (bind_op->mmu_op_ctx) 218 + pvr_mmu_op_context_destroy(bind_op->mmu_op_ctx); 219 + } 220 + 221 + static int 222 + pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op, 223 + struct pvr_vm_context *vm_ctx, 224 + struct pvr_gem_object *pvr_obj, u64 offset, 225 + u64 device_addr, u64 size) 226 + { 227 + const bool is_user = vm_ctx == vm_ctx->pvr_dev->kernel_vm_ctx; 228 + const u64 pvr_obj_size = pvr_gem_object_size(pvr_obj); 229 + struct sg_table *sgt; 230 + u64 offset_plus_size; 231 + int err; 232 + 233 + if (check_add_overflow(offset, size, &offset_plus_size)) 234 + return -EINVAL; 235 + 236 + if (is_user && 237 + !pvr_find_heap_containing(vm_ctx->pvr_dev, device_addr, size)) { 238 + return -EINVAL; 239 + } 240 + 241 + if (!pvr_device_addr_and_size_are_valid(device_addr, size) || 242 + offset & ~PAGE_MASK || size & ~PAGE_MASK || 243 + offset >= pvr_obj_size || offset_plus_size > pvr_obj_size) 244 + return -EINVAL; 245 + 246 + bind_op->type = PVR_VM_BIND_TYPE_MAP; 247 + 248 + bind_op->gpuvm_bo = drm_gpuvm_bo_create(&vm_ctx->gpuvm_mgr, 249 + gem_from_pvr_gem(pvr_obj)); 250 + if (!bind_op->gpuvm_bo) 251 + return -ENOMEM; 252 + 253 + bind_op->new_va = kzalloc(sizeof(*bind_op->new_va), GFP_KERNEL); 254 + bind_op->prev_va = kzalloc(sizeof(*bind_op->prev_va), GFP_KERNEL); 255 + bind_op->next_va = kzalloc(sizeof(*bind_op->next_va), GFP_KERNEL); 256 + if (!bind_op->new_va || !bind_op->prev_va || !bind_op->next_va) { 257 + err = -ENOMEM; 258 + goto err_bind_op_fini; 259 + } 260 + 261 + /* Pin pages so they're ready for use. */ 262 + sgt = pvr_gem_object_get_pages_sgt(pvr_obj); 263 + err = PTR_ERR_OR_ZERO(sgt); 264 + if (err) 265 + goto err_bind_op_fini; 266 + 267 + bind_op->mmu_op_ctx = 268 + pvr_mmu_op_context_create(vm_ctx->mmu_ctx, sgt, offset, size); 269 + err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx); 270 + if (err) { 271 + bind_op->mmu_op_ctx = NULL; 272 + goto err_bind_op_fini; 273 + } 274 + 275 + bind_op->pvr_obj = pvr_obj; 276 + bind_op->vm_ctx = vm_ctx; 277 + bind_op->device_addr = device_addr; 278 + bind_op->size = size; 279 + bind_op->offset = offset; 280 + 281 + return 0; 282 + 283 + err_bind_op_fini: 284 + pvr_vm_bind_op_fini(bind_op); 285 + 286 + return err; 287 + } 288 + 289 + static int 290 + pvr_vm_bind_op_unmap_init(struct pvr_vm_bind_op *bind_op, 291 + struct pvr_vm_context *vm_ctx, u64 device_addr, 292 + u64 size) 293 + { 294 + int err; 295 + 296 + if (!pvr_device_addr_and_size_are_valid(device_addr, size)) 297 + return -EINVAL; 298 + 299 + bind_op->type = PVR_VM_BIND_TYPE_UNMAP; 300 + 301 + bind_op->prev_va = kzalloc(sizeof(*bind_op->prev_va), GFP_KERNEL); 302 + bind_op->next_va = kzalloc(sizeof(*bind_op->next_va), GFP_KERNEL); 303 + if (!bind_op->prev_va || !bind_op->next_va) { 304 + err = -ENOMEM; 305 + goto err_bind_op_fini; 306 + } 307 + 308 + bind_op->mmu_op_ctx = 309 + pvr_mmu_op_context_create(vm_ctx->mmu_ctx, NULL, 0, 0); 310 + err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx); 311 + if (err) { 312 + bind_op->mmu_op_ctx = NULL; 313 + goto err_bind_op_fini; 314 + } 315 + 316 + bind_op->vm_ctx = vm_ctx; 317 + bind_op->device_addr = device_addr; 318 + bind_op->size = size; 319 + 320 + return 0; 321 + 322 + err_bind_op_fini: 323 + pvr_vm_bind_op_fini(bind_op); 324 + 325 + return err; 326 + } 327 + 328 + static int 329 + pvr_vm_bind_op_lock_resvs(struct drm_exec *exec, struct pvr_vm_bind_op *bind_op) 330 + { 331 + drm_exec_until_all_locked(exec) { 332 + struct drm_gem_object *r_obj = &bind_op->vm_ctx->dummy_gem; 333 + struct drm_gpuvm *gpuvm = &bind_op->vm_ctx->gpuvm_mgr; 334 + struct pvr_gem_object *pvr_obj = bind_op->pvr_obj; 335 + struct drm_gpuvm_bo *gpuvm_bo; 336 + 337 + /* Acquire lock on the vm_context's reserve object. */ 338 + int err = drm_exec_lock_obj(exec, r_obj); 339 + 340 + drm_exec_retry_on_contention(exec); 341 + if (err) 342 + return err; 343 + 344 + /* Acquire lock on all BOs in the context. */ 345 + list_for_each_entry(gpuvm_bo, &gpuvm->extobj.list, 346 + list.entry.extobj) { 347 + err = drm_exec_lock_obj(exec, gpuvm_bo->obj); 348 + 349 + drm_exec_retry_on_contention(exec); 350 + if (err) 351 + return err; 352 + } 353 + 354 + /* Unmap operations don't have an object to lock. */ 355 + if (!pvr_obj) 356 + break; 357 + 358 + /* Acquire lock on the GEM being mapped. */ 359 + err = drm_exec_lock_obj(exec, 360 + gem_from_pvr_gem(bind_op->pvr_obj)); 361 + 362 + drm_exec_retry_on_contention(exec); 363 + if (err) 364 + return err; 365 + } 366 + 367 + return 0; 368 + } 369 + 370 + /** 371 + * pvr_vm_gpuva_map() - Insert a mapping into a memory context. 372 + * @op: gpuva op containing the remap details. 373 + * @op_ctx: Operation context. 374 + * 375 + * Context: Called by drm_gpuvm_sm_map following a successful mapping while 376 + * @op_ctx.vm_ctx mutex is held. 377 + * 378 + * Return: 379 + * * 0 on success, or 380 + * * Any error returned by pvr_mmu_map(). 381 + */ 382 + static int 383 + pvr_vm_gpuva_map(struct drm_gpuva_op *op, void *op_ctx) 384 + { 385 + struct pvr_gem_object *pvr_gem = gem_to_pvr_gem(op->map.gem.obj); 386 + struct pvr_vm_bind_op *ctx = op_ctx; 387 + int err; 388 + 389 + if ((op->map.gem.offset | op->map.va.range) & ~PVR_DEVICE_PAGE_MASK) 390 + return -EINVAL; 391 + 392 + err = pvr_mmu_map(ctx->mmu_op_ctx, op->map.va.range, pvr_gem->flags, 393 + op->map.va.addr); 394 + if (err) 395 + return err; 396 + 397 + drm_gpuva_map(&ctx->vm_ctx->gpuvm_mgr, &ctx->new_va->base, &op->map); 398 + drm_gpuva_link(&ctx->new_va->base, ctx->gpuvm_bo); 399 + ctx->new_va = NULL; 400 + 401 + return 0; 402 + } 403 + 404 + /** 405 + * pvr_vm_gpuva_unmap() - Remove a mapping from a memory context. 406 + * @op: gpuva op containing the unmap details. 407 + * @op_ctx: Operation context. 408 + * 409 + * Context: Called by drm_gpuvm_sm_unmap following a successful unmapping while 410 + * @op_ctx.vm_ctx mutex is held. 411 + * 412 + * Return: 413 + * * 0 on success, or 414 + * * Any error returned by pvr_mmu_unmap(). 415 + */ 416 + static int 417 + pvr_vm_gpuva_unmap(struct drm_gpuva_op *op, void *op_ctx) 418 + { 419 + struct pvr_vm_bind_op *ctx = op_ctx; 420 + 421 + int err = pvr_mmu_unmap(ctx->mmu_op_ctx, op->unmap.va->va.addr, 422 + op->unmap.va->va.range); 423 + 424 + if (err) 425 + return err; 426 + 427 + drm_gpuva_unmap(&op->unmap); 428 + drm_gpuva_unlink(op->unmap.va); 429 + 430 + return 0; 431 + } 432 + 433 + /** 434 + * pvr_vm_gpuva_remap() - Remap a mapping within a memory context. 435 + * @op: gpuva op containing the remap details. 436 + * @op_ctx: Operation context. 437 + * 438 + * Context: Called by either drm_gpuvm_sm_map or drm_gpuvm_sm_unmap when a 439 + * mapping or unmapping operation causes a region to be split. The 440 + * @op_ctx.vm_ctx mutex is held. 441 + * 442 + * Return: 443 + * * 0 on success, or 444 + * * Any error returned by pvr_vm_gpuva_unmap() or pvr_vm_gpuva_unmap(). 445 + */ 446 + static int 447 + pvr_vm_gpuva_remap(struct drm_gpuva_op *op, void *op_ctx) 448 + { 449 + struct pvr_vm_bind_op *ctx = op_ctx; 450 + u64 va_start = 0, va_range = 0; 451 + int err; 452 + 453 + drm_gpuva_op_remap_to_unmap_range(&op->remap, &va_start, &va_range); 454 + err = pvr_mmu_unmap(ctx->mmu_op_ctx, va_start, va_range); 455 + if (err) 456 + return err; 457 + 458 + /* No actual remap required: the page table tree depth is fixed to 3, 459 + * and we use 4k page table entries only for now. 460 + */ 461 + drm_gpuva_remap(&ctx->prev_va->base, &ctx->next_va->base, &op->remap); 462 + 463 + if (op->remap.prev) { 464 + pvr_gem_object_get(gem_to_pvr_gem(ctx->prev_va->base.gem.obj)); 465 + drm_gpuva_link(&ctx->prev_va->base, ctx->gpuvm_bo); 466 + ctx->prev_va = NULL; 467 + } 468 + 469 + if (op->remap.next) { 470 + pvr_gem_object_get(gem_to_pvr_gem(ctx->next_va->base.gem.obj)); 471 + drm_gpuva_link(&ctx->next_va->base, ctx->gpuvm_bo); 472 + ctx->next_va = NULL; 473 + } 474 + 475 + drm_gpuva_unlink(op->remap.unmap->va); 476 + 477 + return 0; 478 + } 479 + 480 + /* 481 + * Public API 482 + * 483 + * For an overview of these functions, see *DOC: Public API* in "pvr_vm.h". 484 + */ 485 + 486 + /** 487 + * pvr_device_addr_is_valid() - Tests whether a device-virtual address 488 + * is valid. 489 + * @device_addr: Virtual device address to test. 490 + * 491 + * Return: 492 + * * %true if @device_addr is within the valid range for a device page 493 + * table and is aligned to the device page size, or 494 + * * %false otherwise. 495 + */ 496 + bool 497 + pvr_device_addr_is_valid(u64 device_addr) 498 + { 499 + return (device_addr & ~PVR_PAGE_TABLE_ADDR_MASK) == 0 && 500 + (device_addr & ~PVR_DEVICE_PAGE_MASK) == 0; 501 + } 502 + 503 + /** 504 + * pvr_device_addr_and_size_are_valid() - Tests whether a device-virtual 505 + * address and associated size are both valid. 506 + * @device_addr: Virtual device address to test. 507 + * @size: Size of the range based at @device_addr to test. 508 + * 509 + * Calling pvr_device_addr_is_valid() twice (once on @size, and again on 510 + * @device_addr + @size) to verify a device-virtual address range initially 511 + * seems intuitive, but it produces a false-negative when the address range 512 + * is right at the end of device-virtual address space. 513 + * 514 + * This function catches that corner case, as well as checking that 515 + * @size is non-zero. 516 + * 517 + * Return: 518 + * * %true if @device_addr is device page aligned; @size is device page 519 + * aligned; the range specified by @device_addr and @size is within the 520 + * bounds of the device-virtual address space, and @size is non-zero, or 521 + * * %false otherwise. 522 + */ 523 + bool 524 + pvr_device_addr_and_size_are_valid(u64 device_addr, u64 size) 525 + { 526 + return pvr_device_addr_is_valid(device_addr) && 527 + size != 0 && (size & ~PVR_DEVICE_PAGE_MASK) == 0 && 528 + (device_addr + size <= PVR_PAGE_TABLE_ADDR_SPACE_SIZE); 529 + } 530 + 531 + void pvr_gpuvm_free(struct drm_gpuvm *gpuvm) 532 + { 533 + 534 + } 535 + 536 + static const struct drm_gpuvm_ops pvr_vm_gpuva_ops = { 537 + .vm_free = pvr_gpuvm_free, 538 + .sm_step_map = pvr_vm_gpuva_map, 539 + .sm_step_remap = pvr_vm_gpuva_remap, 540 + .sm_step_unmap = pvr_vm_gpuva_unmap, 541 + }; 542 + 543 + /** 544 + * pvr_vm_create_context() - Create a new VM context. 545 + * @pvr_dev: Target PowerVR device. 546 + * @is_userspace_context: %true if this context is for userspace. This will 547 + * create a firmware memory context for the VM context 548 + * and disable warnings when tearing down mappings. 549 + * 550 + * Return: 551 + * * A handle to the newly-minted VM context on success, 552 + * * -%EINVAL if the feature "virtual address space bits" on @pvr_dev is 553 + * missing or has an unsupported value, 554 + * * -%ENOMEM if allocation of the structure behind the opaque handle fails, 555 + * or 556 + * * Any error encountered while setting up internal structures. 557 + */ 558 + struct pvr_vm_context * 559 + pvr_vm_create_context(struct pvr_device *pvr_dev, bool is_userspace_context) 560 + { 561 + struct drm_device *drm_dev = from_pvr_device(pvr_dev); 562 + 563 + struct pvr_vm_context *vm_ctx; 564 + u16 device_addr_bits; 565 + 566 + int err; 567 + 568 + err = PVR_FEATURE_VALUE(pvr_dev, virtual_address_space_bits, 569 + &device_addr_bits); 570 + if (err) { 571 + drm_err(drm_dev, 572 + "Failed to get device virtual address space bits\n"); 573 + return ERR_PTR(err); 574 + } 575 + 576 + if (device_addr_bits != PVR_PAGE_TABLE_ADDR_BITS) { 577 + drm_err(drm_dev, 578 + "Device has unsupported virtual address space size\n"); 579 + return ERR_PTR(-EINVAL); 580 + } 581 + 582 + vm_ctx = kzalloc(sizeof(*vm_ctx), GFP_KERNEL); 583 + if (!vm_ctx) 584 + return ERR_PTR(-ENOMEM); 585 + 586 + drm_gem_private_object_init(&pvr_dev->base, &vm_ctx->dummy_gem, 0); 587 + 588 + vm_ctx->pvr_dev = pvr_dev; 589 + kref_init(&vm_ctx->ref_count); 590 + mutex_init(&vm_ctx->lock); 591 + 592 + drm_gpuvm_init(&vm_ctx->gpuvm_mgr, 593 + is_userspace_context ? "PowerVR-user-VM" : "PowerVR-FW-VM", 594 + 0, &pvr_dev->base, &vm_ctx->dummy_gem, 595 + 0, 1ULL << device_addr_bits, 0, 0, &pvr_vm_gpuva_ops); 596 + 597 + vm_ctx->mmu_ctx = pvr_mmu_context_create(pvr_dev); 598 + err = PTR_ERR_OR_ZERO(&vm_ctx->mmu_ctx); 599 + if (err) { 600 + vm_ctx->mmu_ctx = NULL; 601 + goto err_put_ctx; 602 + } 603 + 604 + if (is_userspace_context) { 605 + /* TODO: Create FW mem context */ 606 + err = -ENODEV; 607 + goto err_put_ctx; 608 + } 609 + 610 + return vm_ctx; 611 + 612 + err_put_ctx: 613 + pvr_vm_context_put(vm_ctx); 614 + 615 + return ERR_PTR(err); 616 + } 617 + 618 + /** 619 + * pvr_vm_context_release() - Teardown a VM context. 620 + * @ref_count: Pointer to reference counter of the VM context. 621 + * 622 + * This function ensures that no mappings are left dangling by unmapping them 623 + * all in order of ascending device-virtual address. 624 + */ 625 + static void 626 + pvr_vm_context_release(struct kref *ref_count) 627 + { 628 + struct pvr_vm_context *vm_ctx = 629 + container_of(ref_count, struct pvr_vm_context, ref_count); 630 + 631 + /* TODO: Destroy FW mem context */ 632 + WARN_ON(vm_ctx->fw_mem_ctx_obj); 633 + 634 + WARN_ON(pvr_vm_unmap(vm_ctx, vm_ctx->gpuvm_mgr.mm_start, 635 + vm_ctx->gpuvm_mgr.mm_range)); 636 + 637 + drm_gpuvm_put(&vm_ctx->gpuvm_mgr); 638 + pvr_mmu_context_destroy(vm_ctx->mmu_ctx); 639 + drm_gem_private_object_fini(&vm_ctx->dummy_gem); 640 + mutex_destroy(&vm_ctx->lock); 641 + 642 + kfree(vm_ctx); 643 + } 644 + 645 + /** 646 + * pvr_vm_context_lookup() - Look up VM context from handle 647 + * @pvr_file: Pointer to pvr_file structure. 648 + * @handle: Object handle. 649 + * 650 + * Takes reference on VM context object. Call pvr_vm_context_put() to release. 651 + * 652 + * Returns: 653 + * * The requested object on success, or 654 + * * %NULL on failure (object does not exist in list, or is not a VM context) 655 + */ 656 + struct pvr_vm_context * 657 + pvr_vm_context_lookup(struct pvr_file *pvr_file, u32 handle) 658 + { 659 + struct pvr_vm_context *vm_ctx; 660 + 661 + xa_lock(&pvr_file->vm_ctx_handles); 662 + vm_ctx = xa_load(&pvr_file->vm_ctx_handles, handle); 663 + if (vm_ctx) 664 + kref_get(&vm_ctx->ref_count); 665 + 666 + xa_unlock(&pvr_file->vm_ctx_handles); 667 + 668 + return vm_ctx; 669 + } 670 + 671 + /** 672 + * pvr_vm_context_put() - Release a reference on a VM context 673 + * @vm_ctx: Target VM context. 674 + * 675 + * Returns: 676 + * * %true if the VM context was destroyed, or 677 + * * %false if there are any references still remaining. 678 + */ 679 + bool 680 + pvr_vm_context_put(struct pvr_vm_context *vm_ctx) 681 + { 682 + if (vm_ctx) 683 + return kref_put(&vm_ctx->ref_count, pvr_vm_context_release); 684 + 685 + return true; 686 + } 687 + 688 + /** 689 + * pvr_destroy_vm_contexts_for_file: Destroy any VM contexts associated with the 690 + * given file. 691 + * @pvr_file: Pointer to pvr_file structure. 692 + * 693 + * Removes all vm_contexts associated with @pvr_file from the device VM context 694 + * list and drops initial references. vm_contexts will then be destroyed once 695 + * all outstanding references are dropped. 696 + */ 697 + void pvr_destroy_vm_contexts_for_file(struct pvr_file *pvr_file) 698 + { 699 + struct pvr_vm_context *vm_ctx; 700 + unsigned long handle; 701 + 702 + xa_for_each(&pvr_file->vm_ctx_handles, handle, vm_ctx) { 703 + /* vm_ctx is not used here because that would create a race with xa_erase */ 704 + pvr_vm_context_put(xa_erase(&pvr_file->vm_ctx_handles, handle)); 705 + } 706 + } 707 + 708 + /** 709 + * pvr_vm_map() - Map a section of physical memory into a section of 710 + * device-virtual memory. 711 + * @vm_ctx: Target VM context. 712 + * @pvr_obj: Target PowerVR memory object. 713 + * @pvr_obj_offset: Offset into @pvr_obj to map from. 714 + * @device_addr: Virtual device address at the start of the requested mapping. 715 + * @size: Size of the requested mapping. 716 + * 717 + * No handle is returned to represent the mapping. Instead, callers should 718 + * remember @device_addr and use that as a handle. 719 + * 720 + * Return: 721 + * * 0 on success, 722 + * * -%EINVAL if @device_addr is not a valid page-aligned device-virtual 723 + * address; the region specified by @pvr_obj_offset and @size does not fall 724 + * entirely within @pvr_obj, or any part of the specified region of @pvr_obj 725 + * is not device-virtual page-aligned, 726 + * * Any error encountered while performing internal operations required to 727 + * destroy the mapping (returned from pvr_vm_gpuva_map or 728 + * pvr_vm_gpuva_remap). 729 + */ 730 + int 731 + pvr_vm_map(struct pvr_vm_context *vm_ctx, struct pvr_gem_object *pvr_obj, 732 + u64 pvr_obj_offset, u64 device_addr, u64 size) 733 + { 734 + struct pvr_vm_bind_op bind_op = {0}; 735 + struct drm_exec exec; 736 + 737 + int err = pvr_vm_bind_op_map_init(&bind_op, vm_ctx, pvr_obj, 738 + pvr_obj_offset, device_addr, 739 + size); 740 + 741 + if (err) 742 + return err; 743 + 744 + drm_exec_init(&exec, 745 + DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES); 746 + 747 + pvr_gem_object_get(pvr_obj); 748 + 749 + err = pvr_vm_bind_op_lock_resvs(&exec, &bind_op); 750 + if (err) 751 + goto err_cleanup; 752 + 753 + err = pvr_vm_bind_op_exec(&bind_op); 754 + 755 + drm_exec_fini(&exec); 756 + 757 + err_cleanup: 758 + pvr_vm_bind_op_fini(&bind_op); 759 + 760 + return err; 761 + } 762 + 763 + /** 764 + * pvr_vm_unmap() - Unmap an already mapped section of device-virtual memory. 765 + * @vm_ctx: Target VM context. 766 + * @device_addr: Virtual device address at the start of the target mapping. 767 + * @size: Size of the target mapping. 768 + * 769 + * Return: 770 + * * 0 on success, 771 + * * -%EINVAL if @device_addr is not a valid page-aligned device-virtual 772 + * address, 773 + * * Any error encountered while performing internal operations required to 774 + * destroy the mapping (returned from pvr_vm_gpuva_unmap or 775 + * pvr_vm_gpuva_remap). 776 + */ 777 + int 778 + pvr_vm_unmap(struct pvr_vm_context *vm_ctx, u64 device_addr, u64 size) 779 + { 780 + struct pvr_vm_bind_op bind_op = {0}; 781 + struct drm_exec exec; 782 + 783 + int err = pvr_vm_bind_op_unmap_init(&bind_op, vm_ctx, device_addr, 784 + size); 785 + 786 + if (err) 787 + return err; 788 + 789 + drm_exec_init(&exec, 790 + DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES); 791 + 792 + err = pvr_vm_bind_op_lock_resvs(&exec, &bind_op); 793 + if (err) 794 + goto err_cleanup; 795 + 796 + err = pvr_vm_bind_op_exec(&bind_op); 797 + 798 + drm_exec_fini(&exec); 799 + 800 + err_cleanup: 801 + pvr_vm_bind_op_fini(&bind_op); 802 + 803 + return err; 804 + } 805 + 806 + /* Static data areas are determined by firmware. */ 807 + static const struct drm_pvr_static_data_area static_data_areas[] = { 808 + { 809 + .area_usage = DRM_PVR_STATIC_DATA_AREA_FENCE, 810 + .location_heap_id = DRM_PVR_HEAP_GENERAL, 811 + .offset = 0, 812 + .size = 128, 813 + }, 814 + { 815 + .area_usage = DRM_PVR_STATIC_DATA_AREA_YUV_CSC, 816 + .location_heap_id = DRM_PVR_HEAP_GENERAL, 817 + .offset = 128, 818 + .size = 1024, 819 + }, 820 + { 821 + .area_usage = DRM_PVR_STATIC_DATA_AREA_VDM_SYNC, 822 + .location_heap_id = DRM_PVR_HEAP_PDS_CODE_DATA, 823 + .offset = 0, 824 + .size = 128, 825 + }, 826 + { 827 + .area_usage = DRM_PVR_STATIC_DATA_AREA_EOT, 828 + .location_heap_id = DRM_PVR_HEAP_PDS_CODE_DATA, 829 + .offset = 128, 830 + .size = 128, 831 + }, 832 + { 833 + .area_usage = DRM_PVR_STATIC_DATA_AREA_VDM_SYNC, 834 + .location_heap_id = DRM_PVR_HEAP_USC_CODE, 835 + .offset = 0, 836 + .size = 128, 837 + }, 838 + }; 839 + 840 + #define GET_RESERVED_SIZE(last_offset, last_size) round_up((last_offset) + (last_size), PAGE_SIZE) 841 + 842 + /* 843 + * The values given to GET_RESERVED_SIZE() are taken from the last entry in the corresponding 844 + * static data area for each heap. 845 + */ 846 + static const struct drm_pvr_heap pvr_heaps[] = { 847 + [DRM_PVR_HEAP_GENERAL] = { 848 + .base = ROGUE_GENERAL_HEAP_BASE, 849 + .size = ROGUE_GENERAL_HEAP_SIZE, 850 + .flags = 0, 851 + .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 852 + }, 853 + [DRM_PVR_HEAP_PDS_CODE_DATA] = { 854 + .base = ROGUE_PDSCODEDATA_HEAP_BASE, 855 + .size = ROGUE_PDSCODEDATA_HEAP_SIZE, 856 + .flags = 0, 857 + .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 858 + }, 859 + [DRM_PVR_HEAP_USC_CODE] = { 860 + .base = ROGUE_USCCODE_HEAP_BASE, 861 + .size = ROGUE_USCCODE_HEAP_SIZE, 862 + .flags = 0, 863 + .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 864 + }, 865 + [DRM_PVR_HEAP_RGNHDR] = { 866 + .base = ROGUE_RGNHDR_HEAP_BASE, 867 + .size = ROGUE_RGNHDR_HEAP_SIZE, 868 + .flags = 0, 869 + .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 870 + }, 871 + [DRM_PVR_HEAP_VIS_TEST] = { 872 + .base = ROGUE_VISTEST_HEAP_BASE, 873 + .size = ROGUE_VISTEST_HEAP_SIZE, 874 + .flags = 0, 875 + .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 876 + }, 877 + [DRM_PVR_HEAP_TRANSFER_FRAG] = { 878 + .base = ROGUE_TRANSFER_FRAG_HEAP_BASE, 879 + .size = ROGUE_TRANSFER_FRAG_HEAP_SIZE, 880 + .flags = 0, 881 + .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, 882 + }, 883 + }; 884 + 885 + int 886 + pvr_static_data_areas_get(const struct pvr_device *pvr_dev, 887 + struct drm_pvr_ioctl_dev_query_args *args) 888 + { 889 + struct drm_pvr_dev_query_static_data_areas query = {0}; 890 + int err; 891 + 892 + if (!args->pointer) { 893 + args->size = sizeof(struct drm_pvr_dev_query_static_data_areas); 894 + return 0; 895 + } 896 + 897 + err = PVR_UOBJ_GET(query, args->size, args->pointer); 898 + if (err < 0) 899 + return err; 900 + 901 + if (!query.static_data_areas.array) { 902 + query.static_data_areas.count = ARRAY_SIZE(static_data_areas); 903 + query.static_data_areas.stride = sizeof(struct drm_pvr_static_data_area); 904 + goto copy_out; 905 + } 906 + 907 + if (query.static_data_areas.count > ARRAY_SIZE(static_data_areas)) 908 + query.static_data_areas.count = ARRAY_SIZE(static_data_areas); 909 + 910 + err = PVR_UOBJ_SET_ARRAY(&query.static_data_areas, static_data_areas); 911 + if (err < 0) 912 + return err; 913 + 914 + copy_out: 915 + err = PVR_UOBJ_SET(args->pointer, args->size, query); 916 + if (err < 0) 917 + return err; 918 + 919 + args->size = sizeof(query); 920 + return 0; 921 + } 922 + 923 + int 924 + pvr_heap_info_get(const struct pvr_device *pvr_dev, 925 + struct drm_pvr_ioctl_dev_query_args *args) 926 + { 927 + struct drm_pvr_dev_query_heap_info query = {0}; 928 + u64 dest; 929 + int err; 930 + 931 + if (!args->pointer) { 932 + args->size = sizeof(struct drm_pvr_dev_query_heap_info); 933 + return 0; 934 + } 935 + 936 + err = PVR_UOBJ_GET(query, args->size, args->pointer); 937 + if (err < 0) 938 + return err; 939 + 940 + if (!query.heaps.array) { 941 + query.heaps.count = ARRAY_SIZE(pvr_heaps); 942 + query.heaps.stride = sizeof(struct drm_pvr_heap); 943 + goto copy_out; 944 + } 945 + 946 + if (query.heaps.count > ARRAY_SIZE(pvr_heaps)) 947 + query.heaps.count = ARRAY_SIZE(pvr_heaps); 948 + 949 + /* Region header heap is only present if BRN63142 is present. */ 950 + dest = query.heaps.array; 951 + for (size_t i = 0; i < query.heaps.count; i++) { 952 + struct drm_pvr_heap heap = pvr_heaps[i]; 953 + 954 + if (i == DRM_PVR_HEAP_RGNHDR && !PVR_HAS_QUIRK(pvr_dev, 63142)) 955 + heap.size = 0; 956 + 957 + err = PVR_UOBJ_SET(dest, query.heaps.stride, heap); 958 + if (err < 0) 959 + return err; 960 + 961 + dest += query.heaps.stride; 962 + } 963 + 964 + copy_out: 965 + err = PVR_UOBJ_SET(args->pointer, args->size, query); 966 + if (err < 0) 967 + return err; 968 + 969 + args->size = sizeof(query); 970 + return 0; 971 + } 972 + 973 + /** 974 + * pvr_heap_contains_range() - Determine if a given heap contains the specified 975 + * device-virtual address range. 976 + * @pvr_heap: Target heap. 977 + * @start: Inclusive start of the target range. 978 + * @end: Inclusive end of the target range. 979 + * 980 + * It is an error to call this function with values of @start and @end that do 981 + * not satisfy the condition @start <= @end. 982 + */ 983 + static __always_inline bool 984 + pvr_heap_contains_range(const struct drm_pvr_heap *pvr_heap, u64 start, u64 end) 985 + { 986 + return pvr_heap->base <= start && end < pvr_heap->base + pvr_heap->size; 987 + } 988 + 989 + /** 990 + * pvr_find_heap_containing() - Find a heap which contains the specified 991 + * device-virtual address range. 992 + * @pvr_dev: Target PowerVR device. 993 + * @start: Start of the target range. 994 + * @size: Size of the target range. 995 + * 996 + * Return: 997 + * * A pointer to a constant instance of struct drm_pvr_heap representing the 998 + * heap containing the entire range specified by @start and @size on 999 + * success, or 1000 + * * %NULL if no such heap exists. 1001 + */ 1002 + const struct drm_pvr_heap * 1003 + pvr_find_heap_containing(struct pvr_device *pvr_dev, u64 start, u64 size) 1004 + { 1005 + u64 end; 1006 + 1007 + if (check_add_overflow(start, size - 1, &end)) 1008 + return NULL; 1009 + 1010 + /* 1011 + * There are no guarantees about the order of address ranges in 1012 + * &pvr_heaps, so iterate over the entire array for a heap whose 1013 + * range completely encompasses the given range. 1014 + */ 1015 + for (u32 heap_id = 0; heap_id < ARRAY_SIZE(pvr_heaps); heap_id++) { 1016 + /* Filter heaps that present only with an associated quirk */ 1017 + if (heap_id == DRM_PVR_HEAP_RGNHDR && 1018 + !PVR_HAS_QUIRK(pvr_dev, 63142)) { 1019 + continue; 1020 + } 1021 + 1022 + if (pvr_heap_contains_range(&pvr_heaps[heap_id], start, end)) 1023 + return &pvr_heaps[heap_id]; 1024 + } 1025 + 1026 + return NULL; 1027 + } 1028 + 1029 + /** 1030 + * pvr_vm_find_gem_object() - Look up a buffer object from a given 1031 + * device-virtual address. 1032 + * @vm_ctx: [IN] Target VM context. 1033 + * @device_addr: [IN] Virtual device address at the start of the required 1034 + * object. 1035 + * @mapped_offset_out: [OUT] Pointer to location to write offset of the start 1036 + * of the mapped region within the buffer object. May be 1037 + * %NULL if this information is not required. 1038 + * @mapped_size_out: [OUT] Pointer to location to write size of the mapped 1039 + * region. May be %NULL if this information is not required. 1040 + * 1041 + * If successful, a reference will be taken on the buffer object. The caller 1042 + * must drop the reference with pvr_gem_object_put(). 1043 + * 1044 + * Return: 1045 + * * The PowerVR buffer object mapped at @device_addr if one exists, or 1046 + * * %NULL otherwise. 1047 + */ 1048 + struct pvr_gem_object * 1049 + pvr_vm_find_gem_object(struct pvr_vm_context *vm_ctx, u64 device_addr, 1050 + u64 *mapped_offset_out, u64 *mapped_size_out) 1051 + { 1052 + struct pvr_gem_object *pvr_obj; 1053 + struct drm_gpuva *va; 1054 + 1055 + mutex_lock(&vm_ctx->lock); 1056 + 1057 + va = drm_gpuva_find_first(&vm_ctx->gpuvm_mgr, device_addr, 1); 1058 + if (!va) 1059 + goto err_unlock; 1060 + 1061 + pvr_obj = gem_to_pvr_gem(va->gem.obj); 1062 + pvr_gem_object_get(pvr_obj); 1063 + 1064 + if (mapped_offset_out) 1065 + *mapped_offset_out = va->gem.offset; 1066 + if (mapped_size_out) 1067 + *mapped_size_out = va->va.range; 1068 + 1069 + mutex_unlock(&vm_ctx->lock); 1070 + 1071 + return pvr_obj; 1072 + 1073 + err_unlock: 1074 + mutex_unlock(&vm_ctx->lock); 1075 + 1076 + return NULL; 1077 + } 1078 + 1079 + /** 1080 + * pvr_vm_get_fw_mem_context: Get object representing firmware memory context 1081 + * @vm_ctx: Target VM context. 1082 + * 1083 + * Returns: 1084 + * * FW object representing firmware memory context, or 1085 + * * %NULL if this VM context does not have a firmware memory context. 1086 + */ 1087 + struct pvr_fw_object * 1088 + pvr_vm_get_fw_mem_context(struct pvr_vm_context *vm_ctx) 1089 + { 1090 + return vm_ctx->fw_mem_ctx_obj; 1091 + }
+65
drivers/gpu/drm/imagination/pvr_vm.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only OR MIT */ 2 + /* Copyright (c) 2023 Imagination Technologies Ltd. */ 3 + 4 + #ifndef PVR_VM_H 5 + #define PVR_VM_H 6 + 7 + #include "pvr_rogue_mmu_defs.h" 8 + 9 + #include <uapi/drm/pvr_drm.h> 10 + 11 + #include <linux/types.h> 12 + 13 + /* Forward declaration from "pvr_device.h" */ 14 + struct pvr_device; 15 + struct pvr_file; 16 + 17 + /* Forward declaration from "pvr_gem.h" */ 18 + struct pvr_gem_object; 19 + 20 + /* Forward declaration from "pvr_vm.c" */ 21 + struct pvr_vm_context; 22 + 23 + /* Forward declaration from <uapi/drm/pvr_drm.h> */ 24 + struct drm_pvr_ioctl_get_heap_info_args; 25 + 26 + /* Forward declaration from <drm/drm_exec.h> */ 27 + struct drm_exec; 28 + 29 + /* Functions defined in pvr_vm.c */ 30 + 31 + bool pvr_device_addr_is_valid(u64 device_addr); 32 + bool pvr_device_addr_and_size_are_valid(u64 device_addr, u64 size); 33 + 34 + struct pvr_vm_context *pvr_vm_create_context(struct pvr_device *pvr_dev, 35 + bool is_userspace_context); 36 + 37 + int pvr_vm_map(struct pvr_vm_context *vm_ctx, 38 + struct pvr_gem_object *pvr_obj, u64 pvr_obj_offset, 39 + u64 device_addr, u64 size); 40 + int pvr_vm_unmap(struct pvr_vm_context *vm_ctx, u64 device_addr, u64 size); 41 + 42 + dma_addr_t pvr_vm_get_page_table_root_addr(struct pvr_vm_context *vm_ctx); 43 + struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx); 44 + 45 + int pvr_static_data_areas_get(const struct pvr_device *pvr_dev, 46 + struct drm_pvr_ioctl_dev_query_args *args); 47 + int pvr_heap_info_get(const struct pvr_device *pvr_dev, 48 + struct drm_pvr_ioctl_dev_query_args *args); 49 + const struct drm_pvr_heap *pvr_find_heap_containing(struct pvr_device *pvr_dev, 50 + u64 addr, u64 size); 51 + 52 + struct pvr_gem_object *pvr_vm_find_gem_object(struct pvr_vm_context *vm_ctx, 53 + u64 device_addr, 54 + u64 *mapped_offset_out, 55 + u64 *mapped_size_out); 56 + 57 + struct pvr_fw_object * 58 + pvr_vm_get_fw_mem_context(struct pvr_vm_context *vm_ctx); 59 + 60 + struct pvr_vm_context *pvr_vm_context_lookup(struct pvr_file *pvr_file, u32 handle); 61 + struct pvr_vm_context *pvr_vm_context_get(struct pvr_vm_context *vm_ctx); 62 + bool pvr_vm_context_put(struct pvr_vm_context *vm_ctx); 63 + void pvr_destroy_vm_contexts_for_file(struct pvr_file *pvr_file); 64 + 65 + #endif /* PVR_VM_H */