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 /** @flags: Combination of drm_panthor_bo_flags flags. */ 83 u32 flags; 84 85 /** 86 * @label: BO tagging fields. The label can be assigned within the 87 * driver itself or through a specific IOCTL. 88 */ 89 struct { 90 /** 91 * @label.str: Pointer to NULL-terminated string, 92 */ 93 const char *str; 94 95 /** @lock.str: Protects access to the @label.str field. */ 96 struct mutex lock; 97 } label; 98 99 #ifdef CONFIG_DEBUG_FS 100 struct panthor_gem_debugfs debugfs; 101 #endif 102 }; 103 104 /** 105 * struct panthor_kernel_bo - Kernel buffer object. 106 * 107 * These objects are only manipulated by the kernel driver and not 108 * directly exposed to the userspace. The GPU address of a kernel 109 * BO might be passed to userspace though. 110 */ 111 struct panthor_kernel_bo { 112 /** 113 * @obj: The GEM object backing this kernel buffer object. 114 */ 115 struct drm_gem_object *obj; 116 117 /** 118 * @vm: VM this private buffer is attached to. 119 */ 120 struct panthor_vm *vm; 121 122 /** 123 * @va_node: VA space allocated to this GEM. 124 */ 125 struct drm_mm_node va_node; 126 127 /** 128 * @kmap: Kernel CPU mapping of @gem. 129 */ 130 void *kmap; 131 }; 132 133 static inline 134 struct panthor_gem_object *to_panthor_bo(struct drm_gem_object *obj) 135 { 136 return container_of(to_drm_gem_shmem_obj(obj), struct panthor_gem_object, base); 137 } 138 139 struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t size); 140 141 int 142 panthor_gem_create_with_handle(struct drm_file *file, 143 struct drm_device *ddev, 144 struct panthor_vm *exclusive_vm, 145 u64 *size, u32 flags, uint32_t *handle); 146 147 void panthor_gem_bo_set_label(struct drm_gem_object *obj, const char *label); 148 void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *label); 149 150 static inline u64 151 panthor_kernel_bo_gpuva(struct panthor_kernel_bo *bo) 152 { 153 return bo->va_node.start; 154 } 155 156 static inline size_t 157 panthor_kernel_bo_size(struct panthor_kernel_bo *bo) 158 { 159 return bo->obj->size; 160 } 161 162 static inline int 163 panthor_kernel_bo_vmap(struct panthor_kernel_bo *bo) 164 { 165 struct iosys_map map; 166 int ret; 167 168 if (bo->kmap) 169 return 0; 170 171 ret = drm_gem_vmap(bo->obj, &map); 172 if (ret) 173 return ret; 174 175 bo->kmap = map.vaddr; 176 return 0; 177 } 178 179 static inline void 180 panthor_kernel_bo_vunmap(struct panthor_kernel_bo *bo) 181 { 182 if (bo->kmap) { 183 struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->kmap); 184 185 drm_gem_vunmap(bo->obj, &map); 186 bo->kmap = NULL; 187 } 188 } 189 190 struct panthor_kernel_bo * 191 panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, 192 size_t size, u32 bo_flags, u32 vm_map_flags, 193 u64 gpu_va, const char *name); 194 195 void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo); 196 197 #ifdef CONFIG_DEBUG_FS 198 void panthor_gem_debugfs_print_bos(struct panthor_device *pfdev, 199 struct seq_file *m); 200 #endif 201 202 #endif /* __PANTHOR_GEM_H__ */ 203