1 /* SPDX-License-Identifier: GPL-2.0 or MIT */ 2 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */ 3 /* Copyright 2023 Collabora ltd. */ 4 5 #ifndef __PANTHOR_GEM_H__ 6 #define __PANTHOR_GEM_H__ 7 8 #include <drm/drm_gem_shmem_helper.h> 9 #include <drm/drm_mm.h> 10 11 #include <linux/iosys-map.h> 12 #include <linux/rwsem.h> 13 14 struct panthor_vm; 15 16 #define PANTHOR_BO_LABEL_MAXLEN 4096 17 18 enum panthor_debugfs_gem_state_flags { 19 PANTHOR_DEBUGFS_GEM_STATE_IMPORTED_BIT = 0, 20 PANTHOR_DEBUGFS_GEM_STATE_EXPORTED_BIT = 1, 21 22 /** @PANTHOR_DEBUGFS_GEM_STATE_FLAG_IMPORTED: GEM BO is PRIME imported. */ 23 PANTHOR_DEBUGFS_GEM_STATE_FLAG_IMPORTED = BIT(PANTHOR_DEBUGFS_GEM_STATE_IMPORTED_BIT), 24 25 /** @PANTHOR_DEBUGFS_GEM_STATE_FLAG_EXPORTED: GEM BO is PRIME exported. */ 26 PANTHOR_DEBUGFS_GEM_STATE_FLAG_EXPORTED = BIT(PANTHOR_DEBUGFS_GEM_STATE_EXPORTED_BIT), 27 }; 28 29 enum panthor_debugfs_gem_usage_flags { 30 PANTHOR_DEBUGFS_GEM_USAGE_KERNEL_BIT = 0, 31 PANTHOR_DEBUGFS_GEM_USAGE_FW_MAPPED_BIT = 1, 32 33 /** @PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL: BO is for kernel use only. */ 34 PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL = BIT(PANTHOR_DEBUGFS_GEM_USAGE_KERNEL_BIT), 35 36 /** @PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED: BO is mapped on the FW VM. */ 37 PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED = BIT(PANTHOR_DEBUGFS_GEM_USAGE_FW_MAPPED_BIT), 38 }; 39 40 /** 41 * struct panthor_gem_debugfs - GEM object's DebugFS list information 42 */ 43 struct panthor_gem_debugfs { 44 /** 45 * @node: Node used to insert the object in the device-wide list of 46 * GEM objects, to display information about it through a DebugFS file. 47 */ 48 struct list_head node; 49 50 /** @creator: Information about the UM process which created the GEM. */ 51 struct { 52 /** @creator.process_name: Group leader name in owning thread's process */ 53 char process_name[TASK_COMM_LEN]; 54 55 /** @creator.tgid: PID of the thread's group leader within its process */ 56 pid_t tgid; 57 } creator; 58 59 /** @flags: Combination of panthor_debugfs_gem_usage_flags flags */ 60 u32 flags; 61 }; 62 63 /** 64 * struct panthor_gem_object - Driver specific GEM object. 65 */ 66 struct panthor_gem_object { 67 /** @base: Inherit from drm_gem_shmem_object. */ 68 struct drm_gem_shmem_object base; 69 70 /** 71 * @exclusive_vm_root_gem: Root GEM of the exclusive VM this GEM object 72 * is attached to. 73 * 74 * If @exclusive_vm_root_gem != NULL, any attempt to bind the GEM to a 75 * different VM will fail. 76 * 77 * All FW memory objects have this field set to the root GEM of the MCU 78 * VM. 79 */ 80 struct drm_gem_object *exclusive_vm_root_gem; 81 82 /** 83 * @gpuva_list_lock: Custom GPUVA lock. 84 * 85 * Used to protect insertion of drm_gpuva elements to the 86 * drm_gem_object.gpuva.list list. 87 * 88 * We can't use the GEM resv for that, because drm_gpuva_link() is 89 * called in a dma-signaling path, where we're not allowed to take 90 * resv locks. 91 */ 92 struct mutex gpuva_list_lock; 93 94 /** @flags: Combination of drm_panthor_bo_flags flags. */ 95 u32 flags; 96 97 /** 98 * @label: BO tagging fields. The label can be assigned within the 99 * driver itself or through a specific IOCTL. 100 */ 101 struct { 102 /** 103 * @label.str: Pointer to NULL-terminated string, 104 */ 105 const char *str; 106 107 /** @lock.str: Protects access to the @label.str field. */ 108 struct mutex lock; 109 } label; 110 111 #ifdef CONFIG_DEBUG_FS 112 struct panthor_gem_debugfs debugfs; 113 #endif 114 }; 115 116 /** 117 * struct panthor_kernel_bo - Kernel buffer object. 118 * 119 * These objects are only manipulated by the kernel driver and not 120 * directly exposed to the userspace. The GPU address of a kernel 121 * BO might be passed to userspace though. 122 */ 123 struct panthor_kernel_bo { 124 /** 125 * @obj: The GEM object backing this kernel buffer object. 126 */ 127 struct drm_gem_object *obj; 128 129 /** 130 * @vm: VM this private buffer is attached to. 131 */ 132 struct panthor_vm *vm; 133 134 /** 135 * @va_node: VA space allocated to this GEM. 136 */ 137 struct drm_mm_node va_node; 138 139 /** 140 * @kmap: Kernel CPU mapping of @gem. 141 */ 142 void *kmap; 143 }; 144 145 static inline 146 struct panthor_gem_object *to_panthor_bo(struct drm_gem_object *obj) 147 { 148 return container_of(to_drm_gem_shmem_obj(obj), struct panthor_gem_object, base); 149 } 150 151 struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t size); 152 153 int 154 panthor_gem_create_with_handle(struct drm_file *file, 155 struct drm_device *ddev, 156 struct panthor_vm *exclusive_vm, 157 u64 *size, u32 flags, uint32_t *handle); 158 159 void panthor_gem_bo_set_label(struct drm_gem_object *obj, const char *label); 160 void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *label); 161 162 static inline u64 163 panthor_kernel_bo_gpuva(struct panthor_kernel_bo *bo) 164 { 165 return bo->va_node.start; 166 } 167 168 static inline size_t 169 panthor_kernel_bo_size(struct panthor_kernel_bo *bo) 170 { 171 return bo->obj->size; 172 } 173 174 static inline int 175 panthor_kernel_bo_vmap(struct panthor_kernel_bo *bo) 176 { 177 struct iosys_map map; 178 int ret; 179 180 if (bo->kmap) 181 return 0; 182 183 ret = drm_gem_vmap(bo->obj, &map); 184 if (ret) 185 return ret; 186 187 bo->kmap = map.vaddr; 188 return 0; 189 } 190 191 static inline void 192 panthor_kernel_bo_vunmap(struct panthor_kernel_bo *bo) 193 { 194 if (bo->kmap) { 195 struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->kmap); 196 197 drm_gem_vunmap(bo->obj, &map); 198 bo->kmap = NULL; 199 } 200 } 201 202 struct panthor_kernel_bo * 203 panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, 204 size_t size, u32 bo_flags, u32 vm_map_flags, 205 u64 gpu_va, const char *name); 206 207 void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo); 208 209 #ifdef CONFIG_DEBUG_FS 210 void panthor_gem_debugfs_print_bos(struct panthor_device *pfdev, 211 struct seq_file *m); 212 #endif 213 214 #endif /* __PANTHOR_GEM_H__ */ 215