xref: /linux/drivers/gpu/drm/panthor/panthor_gem.h (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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 /* Copyright 2025 ARM Limited. All rights reserved. */
5 
6 #ifndef __PANTHOR_GEM_H__
7 #define __PANTHOR_GEM_H__
8 
9 #include <drm/drm_gem.h>
10 #include <drm/drm_mm.h>
11 
12 #include <linux/iosys-map.h>
13 #include <linux/rwsem.h>
14 
15 struct panthor_vm;
16 
17 #define PANTHOR_BO_LABEL_MAXLEN	4096
18 
19 enum panthor_debugfs_gem_state_flags {
20 	PANTHOR_DEBUGFS_GEM_STATE_IMPORTED_BIT = 0,
21 	PANTHOR_DEBUGFS_GEM_STATE_EXPORTED_BIT = 1,
22 	PANTHOR_DEBUGFS_GEM_STATE_EVICTED_BIT = 2,
23 
24 	/** @PANTHOR_DEBUGFS_GEM_STATE_FLAG_IMPORTED: GEM BO is PRIME imported. */
25 	PANTHOR_DEBUGFS_GEM_STATE_FLAG_IMPORTED = BIT(PANTHOR_DEBUGFS_GEM_STATE_IMPORTED_BIT),
26 
27 	/** @PANTHOR_DEBUGFS_GEM_STATE_FLAG_EXPORTED: GEM BO is PRIME exported. */
28 	PANTHOR_DEBUGFS_GEM_STATE_FLAG_EXPORTED = BIT(PANTHOR_DEBUGFS_GEM_STATE_EXPORTED_BIT),
29 
30 	/** @PANTHOR_DEBUGFS_GEM_STATE_FLAG_EVICTED: GEM BO is evicted to swap. */
31 	PANTHOR_DEBUGFS_GEM_STATE_FLAG_EVICTED = BIT(PANTHOR_DEBUGFS_GEM_STATE_EVICTED_BIT),
32 };
33 
34 enum panthor_debugfs_gem_usage_flags {
35 	PANTHOR_DEBUGFS_GEM_USAGE_KERNEL_BIT = 0,
36 	PANTHOR_DEBUGFS_GEM_USAGE_FW_MAPPED_BIT = 1,
37 
38 	/** @PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL: BO is for kernel use only. */
39 	PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL = BIT(PANTHOR_DEBUGFS_GEM_USAGE_KERNEL_BIT),
40 
41 	/** @PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED: BO is mapped on the FW VM. */
42 	PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED = BIT(PANTHOR_DEBUGFS_GEM_USAGE_FW_MAPPED_BIT),
43 };
44 
45 /**
46  * struct panthor_gem_debugfs - GEM object's DebugFS list information
47  */
48 struct panthor_gem_debugfs {
49 	/**
50 	 * @node: Node used to insert the object in the device-wide list of
51 	 * GEM objects, to display information about it through a DebugFS file.
52 	 */
53 	struct list_head node;
54 
55 	/** @creator: Information about the UM process which created the GEM. */
56 	struct {
57 		/** @creator.process_name: Group leader name in owning thread's process */
58 		char process_name[TASK_COMM_LEN];
59 
60 		/** @creator.tgid: PID of the thread's group leader within its process */
61 		pid_t tgid;
62 	} creator;
63 
64 	/** @flags: Combination of panthor_debugfs_gem_usage_flags flags */
65 	u32 flags;
66 };
67 
68 /**
69  * struct panthor_gem_backing - GEM memory backing related data
70  */
71 struct panthor_gem_backing {
72 	/** @pages: Pages requested with drm_gem_get_pages() */
73 	struct page **pages;
74 
75 	/** @pin_count: Number of active pin requests on this GEM */
76 	refcount_t pin_count;
77 };
78 
79 /**
80  * struct panthor_gem_cpu_map - GEM CPU mapping related data
81  */
82 struct panthor_gem_cpu_map {
83 	/** @vaddr: Address returned by vmap() */
84 	void *vaddr;
85 
86 	/** @vaddr_use_count: Number of active vmap() requests on this GEM */
87 	refcount_t vaddr_use_count;
88 
89 	/** @mmap_count: Number of active mmap() requests on this GEM */
90 	refcount_t mmap_count;
91 };
92 
93 /**
94  * struct panthor_gem_dev_map - GEM device mapping related data
95  */
96 struct panthor_gem_dev_map {
97 	/** @sgt: Device mapped sg_table for this GEM */
98 	struct sg_table *sgt;
99 };
100 
101 /**
102  * enum panthor_gem_reclaim_state - Reclaim state of a GEM object
103  *
104  * This is defined in descending reclaimability order and some part
105  * of the code depends on that.
106  */
107 enum panthor_gem_reclaim_state {
108 	/**
109 	 * @PANTHOR_GEM_UNUSED: GEM is currently unused
110 	 *
111 	 * This can happen when the GEM was previously vmap-ed, mmap-ed,
112 	 * and/or GPU mapped and got unmapped. Because pages are lazily
113 	 * returned to the shmem layer, we want to keep a list of such
114 	 * BOs, because they should be fairly easy to reclaim (no need
115 	 * to wait for GPU to be done, and no need to tear down user
116 	 * mappings either).
117 	 */
118 	PANTHOR_GEM_UNUSED,
119 
120 	/**
121 	 * @PANTHOR_GEM_MMAPPED: GEM is currently mmap-ed
122 	 *
123 	 * When a GEM has pages allocated and the mmap_count is > 0, the
124 	 * GEM is placed in the mmapped list. This comes right after
125 	 * unused because we can relatively easily tear down user mappings.
126 	 */
127 	PANTHOR_GEM_MMAPPED,
128 
129 	/**
130 	 * @PANTHOR_GEM_GPU_MAPPED_SINGLE_VM: GEM is GPU mapped to only one VM
131 	 *
132 	 * When a GEM is mapped to a single VM, reclaim requests have more
133 	 * chances to succeed, because we only need to synchronize against
134 	 * a single GPU context. This is more annoying than reclaiming
135 	 * mmap-ed pages still, because we have to wait for in-flight jobs
136 	 * to land, and we might not be able to acquire all necessary locks
137 	 * at reclaim time either.
138 	 */
139 	PANTHOR_GEM_GPU_MAPPED_SINGLE_VM,
140 
141 	/**
142 	 * @PANTHOR_GEM_GPU_MAPPED_MULTI_VM: GEM is GPU mapped to multiple VMs
143 	 *
144 	 * Like PANTHOR_GEM_GPU_MAPPED_SINGLE_VM, but the synchronization across
145 	 * VMs makes such BOs harder to reclaim.
146 	 */
147 	PANTHOR_GEM_GPU_MAPPED_MULTI_VM,
148 
149 	/**
150 	 * @PANTHOR_GEM_UNRECLAIMABLE: GEM can't be reclaimed
151 	 *
152 	 * Happens when the GEM memory is pinned. It's also the state all GEM
153 	 * objects start in, because no memory is allocated until explicitly
154 	 * requested by a CPU or GPU map, meaning there's nothing to reclaim
155 	 * until such an allocation happens.
156 	 */
157 	PANTHOR_GEM_UNRECLAIMABLE,
158 };
159 
160 /**
161  * struct panthor_gem_object - Driver specific GEM object.
162  */
163 struct panthor_gem_object {
164 	/** @base: Inherit from drm_gem_object. */
165 	struct drm_gem_object base;
166 
167 	/** @backing: Memory backing state */
168 	struct panthor_gem_backing backing;
169 
170 	/** @cmap: CPU mapping state */
171 	struct panthor_gem_cpu_map cmap;
172 
173 	/** @dmap: Device mapping state */
174 	struct panthor_gem_dev_map dmap;
175 
176 	/** @reclaim_state: Cached reclaim state */
177 	enum panthor_gem_reclaim_state reclaim_state;
178 
179 	/**
180 	 * @reclaimed_count: How many times object has been evicted to swap.
181 	 * The count saturates at %INT_MAX and will never wrap around to 0.
182 	 */
183 	atomic_t reclaimed_count;
184 
185 	/**
186 	 * @exclusive_vm_root_gem: Root GEM of the exclusive VM this GEM object
187 	 * is attached to.
188 	 *
189 	 * If @exclusive_vm_root_gem != NULL, any attempt to bind the GEM to a
190 	 * different VM will fail.
191 	 *
192 	 * All FW memory objects have this field set to the root GEM of the MCU
193 	 * VM.
194 	 */
195 	struct drm_gem_object *exclusive_vm_root_gem;
196 
197 	/** @flags: Combination of drm_panthor_bo_flags flags. */
198 	u32 flags;
199 
200 	/**
201 	 * @label: BO tagging fields. The label can be assigned within the
202 	 * driver itself or through a specific IOCTL.
203 	 */
204 	struct {
205 		/**
206 		 * @label.str: Pointer to NULL-terminated string,
207 		 */
208 		const char *str;
209 
210 		/** @lock.str: Protects access to the @label.str field. */
211 		struct mutex lock;
212 	} label;
213 
214 #ifdef CONFIG_DEBUG_FS
215 	struct panthor_gem_debugfs debugfs;
216 #endif
217 };
218 
219 /**
220  * struct panthor_kernel_bo - Kernel buffer object.
221  *
222  * These objects are only manipulated by the kernel driver and not
223  * directly exposed to the userspace. The GPU address of a kernel
224  * BO might be passed to userspace though.
225  */
226 struct panthor_kernel_bo {
227 	/**
228 	 * @obj: The GEM object backing this kernel buffer object.
229 	 */
230 	struct drm_gem_object *obj;
231 
232 	/**
233 	 * @vm: VM this private buffer is attached to.
234 	 */
235 	struct panthor_vm *vm;
236 
237 	/**
238 	 * @va_node: VA space allocated to this GEM.
239 	 */
240 	struct drm_mm_node va_node;
241 
242 	/**
243 	 * @kmap: Kernel CPU mapping of @gem.
244 	 */
245 	void *kmap;
246 };
247 
248 #define to_panthor_bo(obj) container_of_const(obj, struct panthor_gem_object, base)
249 
250 void panthor_gem_init(struct panthor_device *ptdev);
251 
252 struct drm_gem_object *
253 panthor_gem_prime_import_sg_table(struct drm_device *dev,
254 				  struct dma_buf_attachment *attach,
255 				  struct sg_table *sgt);
256 int
257 panthor_gem_create_with_handle(struct drm_file *file,
258 			       struct drm_device *ddev,
259 			       struct panthor_vm *exclusive_vm,
260 			       u64 *size, u32 flags, uint32_t *handle);
261 
262 struct sg_table *
263 panthor_gem_get_dev_sgt(struct panthor_gem_object *bo);
264 int panthor_gem_pin(struct panthor_gem_object *bo);
265 void panthor_gem_unpin(struct panthor_gem_object *bo);
266 int panthor_gem_swapin_locked(struct panthor_gem_object *bo);
267 void panthor_gem_update_reclaim_state_locked(struct panthor_gem_object *bo,
268 					     enum panthor_gem_reclaim_state *old_state);
269 int panthor_gem_shrinker_init(struct panthor_device *ptdev);
270 void panthor_gem_shrinker_unplug(struct panthor_device *ptdev);
271 
272 void panthor_gem_bo_set_label(struct drm_gem_object *obj, const char *label);
273 void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *label);
274 int panthor_gem_sync(struct drm_gem_object *obj,
275 		     u32 type, u64 offset, u64 size);
276 
277 struct drm_gem_object *
278 panthor_gem_prime_import(struct drm_device *dev,
279 			 struct dma_buf *dma_buf);
280 
281 static inline u64
282 panthor_kernel_bo_gpuva(struct panthor_kernel_bo *bo)
283 {
284 	return bo->va_node.start;
285 }
286 
287 static inline size_t
288 panthor_kernel_bo_size(struct panthor_kernel_bo *bo)
289 {
290 	return bo->obj->size;
291 }
292 
293 static inline int
294 panthor_kernel_bo_vmap(struct panthor_kernel_bo *bo)
295 {
296 	struct iosys_map map;
297 	int ret;
298 
299 	if (bo->kmap)
300 		return 0;
301 
302 	ret = drm_gem_vmap(bo->obj, &map);
303 	if (ret)
304 		return ret;
305 
306 	bo->kmap = map.vaddr;
307 	return 0;
308 }
309 
310 static inline void
311 panthor_kernel_bo_vunmap(struct panthor_kernel_bo *bo)
312 {
313 	if (bo->kmap) {
314 		struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->kmap);
315 
316 		drm_gem_vunmap(bo->obj, &map);
317 		bo->kmap = NULL;
318 	}
319 }
320 
321 struct panthor_kernel_bo *
322 panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
323 			 size_t size, u32 bo_flags, u32 vm_map_flags,
324 			 u64 gpu_va, const char *name);
325 
326 void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo);
327 
328 #ifdef CONFIG_DEBUG_FS
329 void panthor_gem_debugfs_init(struct drm_minor *minor);
330 #endif
331 
332 #endif /* __PANTHOR_GEM_H__ */
333