xref: /linux/drivers/gpu/drm/panthor/panthor_gem.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
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 #include <linux/cleanup.h>
6 #include <linux/dma-buf.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/err.h>
9 #include <linux/slab.h>
10 
11 #include <drm/drm_print.h>
12 #include <drm/panthor_drm.h>
13 
14 #include "panthor_device.h"
15 #include "panthor_fw.h"
16 #include "panthor_gem.h"
17 #include "panthor_mmu.h"
18 
19 #ifdef CONFIG_DEBUG_FS
20 static void panthor_gem_debugfs_bo_init(struct panthor_gem_object *bo)
21 {
22 	INIT_LIST_HEAD(&bo->debugfs.node);
23 }
24 
25 static void panthor_gem_debugfs_bo_add(struct panthor_gem_object *bo)
26 {
27 	struct panthor_device *ptdev = container_of(bo->base.base.dev,
28 						    struct panthor_device, base);
29 
30 	bo->debugfs.creator.tgid = current->group_leader->pid;
31 	get_task_comm(bo->debugfs.creator.process_name, current->group_leader);
32 
33 	mutex_lock(&ptdev->gems.lock);
34 	list_add_tail(&bo->debugfs.node, &ptdev->gems.node);
35 	mutex_unlock(&ptdev->gems.lock);
36 }
37 
38 static void panthor_gem_debugfs_bo_rm(struct panthor_gem_object *bo)
39 {
40 	struct panthor_device *ptdev = container_of(bo->base.base.dev,
41 						    struct panthor_device, base);
42 
43 	if (list_empty(&bo->debugfs.node))
44 		return;
45 
46 	mutex_lock(&ptdev->gems.lock);
47 	list_del_init(&bo->debugfs.node);
48 	mutex_unlock(&ptdev->gems.lock);
49 }
50 
51 static void panthor_gem_debugfs_set_usage_flags(struct panthor_gem_object *bo, u32 usage_flags)
52 {
53 	bo->debugfs.flags = usage_flags;
54 	panthor_gem_debugfs_bo_add(bo);
55 }
56 #else
57 static void panthor_gem_debugfs_bo_rm(struct panthor_gem_object *bo) {}
58 static void panthor_gem_debugfs_set_usage_flags(struct panthor_gem_object *bo, u32 usage_flags) {}
59 static void panthor_gem_debugfs_bo_init(struct panthor_gem_object *bo) {}
60 #endif
61 
62 static void panthor_gem_free_object(struct drm_gem_object *obj)
63 {
64 	struct panthor_gem_object *bo = to_panthor_bo(obj);
65 	struct drm_gem_object *vm_root_gem = bo->exclusive_vm_root_gem;
66 
67 	panthor_gem_debugfs_bo_rm(bo);
68 
69 	/*
70 	 * Label might have been allocated with kstrdup_const(),
71 	 * we need to take that into account when freeing the memory
72 	 */
73 	kfree_const(bo->label.str);
74 
75 	mutex_destroy(&bo->label.lock);
76 
77 	drm_gem_free_mmap_offset(&bo->base.base);
78 	drm_gem_shmem_free(&bo->base);
79 	drm_gem_object_put(vm_root_gem);
80 }
81 
82 /**
83  * panthor_kernel_bo_destroy() - Destroy a kernel buffer object
84  * @bo: Kernel buffer object to destroy. If NULL or an ERR_PTR(), the destruction
85  * is skipped.
86  */
87 void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo)
88 {
89 	struct panthor_vm *vm;
90 
91 	if (IS_ERR_OR_NULL(bo))
92 		return;
93 
94 	vm = bo->vm;
95 	panthor_kernel_bo_vunmap(bo);
96 
97 	drm_WARN_ON(bo->obj->dev,
98 		    to_panthor_bo(bo->obj)->exclusive_vm_root_gem != panthor_vm_root_gem(vm));
99 	panthor_vm_unmap_range(vm, bo->va_node.start, bo->va_node.size);
100 	panthor_vm_free_va(vm, &bo->va_node);
101 	drm_gem_object_put(bo->obj);
102 	panthor_vm_put(vm);
103 	kfree(bo);
104 }
105 
106 /**
107  * panthor_kernel_bo_create() - Create and map a GEM object to a VM
108  * @ptdev: Device.
109  * @vm: VM to map the GEM to. If NULL, the kernel object is not GPU mapped.
110  * @size: Size of the buffer object.
111  * @bo_flags: Combination of drm_panthor_bo_flags flags.
112  * @vm_map_flags: Combination of drm_panthor_vm_bind_op_flags (only those
113  * that are related to map operations).
114  * @gpu_va: GPU address assigned when mapping to the VM.
115  * If gpu_va == PANTHOR_VM_KERNEL_AUTO_VA, the virtual address will be
116  * automatically allocated.
117  * @name: Descriptive label of the BO's contents
118  *
119  * Return: A valid pointer in case of success, an ERR_PTR() otherwise.
120  */
121 struct panthor_kernel_bo *
122 panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
123 			 size_t size, u32 bo_flags, u32 vm_map_flags,
124 			 u64 gpu_va, const char *name)
125 {
126 	struct drm_gem_shmem_object *obj;
127 	struct panthor_kernel_bo *kbo;
128 	struct panthor_gem_object *bo;
129 	u32 debug_flags = PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL;
130 	int ret;
131 
132 	if (drm_WARN_ON(&ptdev->base, !vm))
133 		return ERR_PTR(-EINVAL);
134 
135 	kbo = kzalloc(sizeof(*kbo), GFP_KERNEL);
136 	if (!kbo)
137 		return ERR_PTR(-ENOMEM);
138 
139 	obj = drm_gem_shmem_create(&ptdev->base, size);
140 	if (IS_ERR(obj)) {
141 		ret = PTR_ERR(obj);
142 		goto err_free_bo;
143 	}
144 
145 	bo = to_panthor_bo(&obj->base);
146 	kbo->obj = &obj->base;
147 	bo->flags = bo_flags;
148 
149 	if (vm == panthor_fw_vm(ptdev))
150 		debug_flags |= PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED;
151 
152 	panthor_gem_kernel_bo_set_label(kbo, name);
153 	panthor_gem_debugfs_set_usage_flags(to_panthor_bo(kbo->obj), debug_flags);
154 
155 	/* The system and GPU MMU page size might differ, which becomes a
156 	 * problem for FW sections that need to be mapped at explicit address
157 	 * since our PAGE_SIZE alignment might cover a VA range that's
158 	 * expected to be used for another section.
159 	 * Make sure we never map more than we need.
160 	 */
161 	size = ALIGN(size, panthor_vm_page_size(vm));
162 	ret = panthor_vm_alloc_va(vm, gpu_va, size, &kbo->va_node);
163 	if (ret)
164 		goto err_put_obj;
165 
166 	ret = panthor_vm_map_bo_range(vm, bo, 0, size, kbo->va_node.start, vm_map_flags);
167 	if (ret)
168 		goto err_free_va;
169 
170 	kbo->vm = panthor_vm_get(vm);
171 	bo->exclusive_vm_root_gem = panthor_vm_root_gem(vm);
172 	drm_gem_object_get(bo->exclusive_vm_root_gem);
173 	bo->base.base.resv = bo->exclusive_vm_root_gem->resv;
174 	return kbo;
175 
176 err_free_va:
177 	panthor_vm_free_va(vm, &kbo->va_node);
178 
179 err_put_obj:
180 	drm_gem_object_put(&obj->base);
181 
182 err_free_bo:
183 	kfree(kbo);
184 	return ERR_PTR(ret);
185 }
186 
187 static struct dma_buf *
188 panthor_gem_prime_export(struct drm_gem_object *obj, int flags)
189 {
190 	/* We can't export GEMs that have an exclusive VM. */
191 	if (to_panthor_bo(obj)->exclusive_vm_root_gem)
192 		return ERR_PTR(-EINVAL);
193 
194 	return drm_gem_prime_export(obj, flags);
195 }
196 
197 static enum drm_gem_object_status panthor_gem_status(struct drm_gem_object *obj)
198 {
199 	struct panthor_gem_object *bo = to_panthor_bo(obj);
200 	enum drm_gem_object_status res = 0;
201 
202 	if (drm_gem_is_imported(&bo->base.base) || bo->base.pages)
203 		res |= DRM_GEM_OBJECT_RESIDENT;
204 
205 	return res;
206 }
207 
208 static const struct drm_gem_object_funcs panthor_gem_funcs = {
209 	.free = panthor_gem_free_object,
210 	.print_info = drm_gem_shmem_object_print_info,
211 	.pin = drm_gem_shmem_object_pin,
212 	.unpin = drm_gem_shmem_object_unpin,
213 	.get_sg_table = drm_gem_shmem_object_get_sg_table,
214 	.vmap = drm_gem_shmem_object_vmap,
215 	.vunmap = drm_gem_shmem_object_vunmap,
216 	.mmap = drm_gem_shmem_object_mmap,
217 	.status = panthor_gem_status,
218 	.export = panthor_gem_prime_export,
219 	.vm_ops = &drm_gem_shmem_vm_ops,
220 };
221 
222 /**
223  * panthor_gem_create_object - Implementation of driver->gem_create_object.
224  * @ddev: DRM device
225  * @size: Size in bytes of the memory the object will reference
226  *
227  * This lets the GEM helpers allocate object structs for us, and keep
228  * our BO stats correct.
229  */
230 struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t size)
231 {
232 	struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
233 	struct panthor_gem_object *obj;
234 
235 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
236 	if (!obj)
237 		return ERR_PTR(-ENOMEM);
238 
239 	obj->base.base.funcs = &panthor_gem_funcs;
240 	obj->base.map_wc = !ptdev->coherent;
241 	mutex_init(&obj->label.lock);
242 
243 	panthor_gem_debugfs_bo_init(obj);
244 
245 	return &obj->base.base;
246 }
247 
248 /**
249  * panthor_gem_create_with_handle() - Create a GEM object and attach it to a handle.
250  * @file: DRM file.
251  * @ddev: DRM device.
252  * @exclusive_vm: Exclusive VM. Not NULL if the GEM object can't be shared.
253  * @size: Size of the GEM object to allocate.
254  * @flags: Combination of drm_panthor_bo_flags flags.
255  * @handle: Pointer holding the handle pointing to the new GEM object.
256  *
257  * Return: Zero on success
258  */
259 int
260 panthor_gem_create_with_handle(struct drm_file *file,
261 			       struct drm_device *ddev,
262 			       struct panthor_vm *exclusive_vm,
263 			       u64 *size, u32 flags, u32 *handle)
264 {
265 	int ret;
266 	struct drm_gem_shmem_object *shmem;
267 	struct panthor_gem_object *bo;
268 
269 	shmem = drm_gem_shmem_create(ddev, *size);
270 	if (IS_ERR(shmem))
271 		return PTR_ERR(shmem);
272 
273 	bo = to_panthor_bo(&shmem->base);
274 	bo->flags = flags;
275 
276 	if (exclusive_vm) {
277 		bo->exclusive_vm_root_gem = panthor_vm_root_gem(exclusive_vm);
278 		drm_gem_object_get(bo->exclusive_vm_root_gem);
279 		bo->base.base.resv = bo->exclusive_vm_root_gem->resv;
280 	}
281 
282 	panthor_gem_debugfs_set_usage_flags(bo, 0);
283 
284 	/*
285 	 * Allocate an id of idr table where the obj is registered
286 	 * and handle has the id what user can see.
287 	 */
288 	ret = drm_gem_handle_create(file, &shmem->base, handle);
289 	if (!ret)
290 		*size = bo->base.base.size;
291 
292 	/* drop reference from allocate - handle holds it now. */
293 	drm_gem_object_put(&shmem->base);
294 
295 	return ret;
296 }
297 
298 void
299 panthor_gem_bo_set_label(struct drm_gem_object *obj, const char *label)
300 {
301 	struct panthor_gem_object *bo = to_panthor_bo(obj);
302 	const char *old_label;
303 
304 	scoped_guard(mutex, &bo->label.lock) {
305 		old_label = bo->label.str;
306 		bo->label.str = label;
307 	}
308 
309 	kfree_const(old_label);
310 }
311 
312 void
313 panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *label)
314 {
315 	const char *str;
316 
317 	/* We should never attempt labelling a UM-exposed GEM object */
318 	if (drm_WARN_ON(bo->obj->dev, bo->obj->handle_count > 0))
319 		return;
320 
321 	if (!label)
322 		return;
323 
324 	str = kstrdup_const(label, GFP_KERNEL);
325 	if (!str) {
326 		/* Failing to allocate memory for a label isn't a fatal condition */
327 		drm_warn(bo->obj->dev, "Not enough memory to allocate BO label");
328 		return;
329 	}
330 
331 	panthor_gem_bo_set_label(bo->obj, str);
332 }
333 
334 #ifdef CONFIG_DEBUG_FS
335 struct gem_size_totals {
336 	size_t size;
337 	size_t resident;
338 	size_t reclaimable;
339 };
340 
341 static void panthor_gem_debugfs_print_flag_names(struct seq_file *m)
342 {
343 	int len;
344 	int i;
345 
346 	static const char * const gem_state_flags_names[] = {
347 		[PANTHOR_DEBUGFS_GEM_STATE_IMPORTED_BIT] = "imported",
348 		[PANTHOR_DEBUGFS_GEM_STATE_EXPORTED_BIT] = "exported",
349 	};
350 
351 	static const char * const gem_usage_flags_names[] = {
352 		[PANTHOR_DEBUGFS_GEM_USAGE_KERNEL_BIT] = "kernel",
353 		[PANTHOR_DEBUGFS_GEM_USAGE_FW_MAPPED_BIT] = "fw-mapped",
354 	};
355 
356 	seq_puts(m, "GEM state flags: ");
357 	for (i = 0, len = ARRAY_SIZE(gem_state_flags_names); i < len; i++) {
358 		if (!gem_state_flags_names[i])
359 			continue;
360 		seq_printf(m, "%s (0x%x)%s", gem_state_flags_names[i],
361 			   (u32)BIT(i), (i < len - 1) ? ", " : "\n");
362 	}
363 
364 	seq_puts(m, "GEM usage flags: ");
365 	for (i = 0, len = ARRAY_SIZE(gem_usage_flags_names); i < len; i++) {
366 		if (!gem_usage_flags_names[i])
367 			continue;
368 		seq_printf(m, "%s (0x%x)%s", gem_usage_flags_names[i],
369 			   (u32)BIT(i), (i < len - 1) ? ", " : "\n\n");
370 	}
371 }
372 
373 static void panthor_gem_debugfs_bo_print(struct panthor_gem_object *bo,
374 					 struct seq_file *m,
375 					 struct gem_size_totals *totals)
376 {
377 	unsigned int refcount = kref_read(&bo->base.base.refcount);
378 	char creator_info[32] = {};
379 	size_t resident_size;
380 	u32 gem_usage_flags = bo->debugfs.flags;
381 	u32 gem_state_flags = 0;
382 
383 	/* Skip BOs being destroyed. */
384 	if (!refcount)
385 		return;
386 
387 	resident_size = bo->base.pages ? bo->base.base.size : 0;
388 
389 	snprintf(creator_info, sizeof(creator_info),
390 		 "%s/%d", bo->debugfs.creator.process_name, bo->debugfs.creator.tgid);
391 	seq_printf(m, "%-32s%-16d%-16d%-16zd%-16zd0x%-16lx",
392 		   creator_info,
393 		   bo->base.base.name,
394 		   refcount,
395 		   bo->base.base.size,
396 		   resident_size,
397 		   drm_vma_node_start(&bo->base.base.vma_node));
398 
399 	if (bo->base.base.import_attach)
400 		gem_state_flags |= PANTHOR_DEBUGFS_GEM_STATE_FLAG_IMPORTED;
401 	if (bo->base.base.dma_buf)
402 		gem_state_flags |= PANTHOR_DEBUGFS_GEM_STATE_FLAG_EXPORTED;
403 
404 	seq_printf(m, "0x%-8x 0x%-10x", gem_state_flags, gem_usage_flags);
405 
406 	scoped_guard(mutex, &bo->label.lock) {
407 		seq_printf(m, "%s\n", bo->label.str ? : "");
408 	}
409 
410 	totals->size += bo->base.base.size;
411 	totals->resident += resident_size;
412 	if (bo->base.madv > 0)
413 		totals->reclaimable += resident_size;
414 }
415 
416 void panthor_gem_debugfs_print_bos(struct panthor_device *ptdev,
417 				   struct seq_file *m)
418 {
419 	struct gem_size_totals totals = {0};
420 	struct panthor_gem_object *bo;
421 
422 	panthor_gem_debugfs_print_flag_names(m);
423 
424 	seq_puts(m, "created-by                      global-name     refcount        size            resident-size   file-offset       state      usage       label\n");
425 	seq_puts(m, "----------------------------------------------------------------------------------------------------------------------------------------------\n");
426 
427 	scoped_guard(mutex, &ptdev->gems.lock) {
428 		list_for_each_entry(bo, &ptdev->gems.node, debugfs.node) {
429 			panthor_gem_debugfs_bo_print(bo, m, &totals);
430 		}
431 	}
432 
433 	seq_puts(m, "==============================================================================================================================================\n");
434 	seq_printf(m, "Total size: %zd, Total resident: %zd, Total reclaimable: %zd\n",
435 		   totals.size, totals.resident, totals.reclaimable);
436 }
437 #endif
438