xref: /linux/drivers/gpu/drm/panthor/panthor_gem.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
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/dma-buf.h>
6 #include <linux/dma-mapping.h>
7 #include <linux/err.h>
8 #include <linux/slab.h>
9 
10 #include <drm/panthor_drm.h>
11 
12 #include "panthor_device.h"
13 #include "panthor_gem.h"
14 #include "panthor_mmu.h"
15 
16 static void panthor_gem_free_object(struct drm_gem_object *obj)
17 {
18 	struct panthor_gem_object *bo = to_panthor_bo(obj);
19 	struct drm_gem_object *vm_root_gem = bo->exclusive_vm_root_gem;
20 
21 	drm_gem_free_mmap_offset(&bo->base.base);
22 	mutex_destroy(&bo->gpuva_list_lock);
23 	drm_gem_shmem_free(&bo->base);
24 	drm_gem_object_put(vm_root_gem);
25 }
26 
27 /**
28  * panthor_kernel_bo_destroy() - Destroy a kernel buffer object
29  * @bo: Kernel buffer object to destroy. If NULL or an ERR_PTR(), the destruction
30  * is skipped.
31  */
32 void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo)
33 {
34 	struct panthor_vm *vm;
35 	int ret;
36 
37 	if (IS_ERR_OR_NULL(bo))
38 		return;
39 
40 	vm = bo->vm;
41 	panthor_kernel_bo_vunmap(bo);
42 
43 	if (drm_WARN_ON(bo->obj->dev,
44 			to_panthor_bo(bo->obj)->exclusive_vm_root_gem != panthor_vm_root_gem(vm)))
45 		goto out_free_bo;
46 
47 	ret = panthor_vm_unmap_range(vm, bo->va_node.start,
48 				     panthor_kernel_bo_size(bo));
49 	if (ret)
50 		goto out_free_bo;
51 
52 	panthor_vm_free_va(vm, &bo->va_node);
53 	drm_gem_object_put(bo->obj);
54 
55 out_free_bo:
56 	panthor_vm_put(vm);
57 	kfree(bo);
58 }
59 
60 /**
61  * panthor_kernel_bo_create() - Create and map a GEM object to a VM
62  * @ptdev: Device.
63  * @vm: VM to map the GEM to. If NULL, the kernel object is not GPU mapped.
64  * @size: Size of the buffer object.
65  * @bo_flags: Combination of drm_panthor_bo_flags flags.
66  * @vm_map_flags: Combination of drm_panthor_vm_bind_op_flags (only those
67  * that are related to map operations).
68  * @gpu_va: GPU address assigned when mapping to the VM.
69  * If gpu_va == PANTHOR_VM_KERNEL_AUTO_VA, the virtual address will be
70  * automatically allocated.
71  *
72  * Return: A valid pointer in case of success, an ERR_PTR() otherwise.
73  */
74 struct panthor_kernel_bo *
75 panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
76 			 size_t size, u32 bo_flags, u32 vm_map_flags,
77 			 u64 gpu_va)
78 {
79 	struct drm_gem_shmem_object *obj;
80 	struct panthor_kernel_bo *kbo;
81 	struct panthor_gem_object *bo;
82 	int ret;
83 
84 	if (drm_WARN_ON(&ptdev->base, !vm))
85 		return ERR_PTR(-EINVAL);
86 
87 	kbo = kzalloc(sizeof(*kbo), GFP_KERNEL);
88 	if (!kbo)
89 		return ERR_PTR(-ENOMEM);
90 
91 	obj = drm_gem_shmem_create(&ptdev->base, size);
92 	if (IS_ERR(obj)) {
93 		ret = PTR_ERR(obj);
94 		goto err_free_bo;
95 	}
96 
97 	bo = to_panthor_bo(&obj->base);
98 	size = obj->base.size;
99 	kbo->obj = &obj->base;
100 	bo->flags = bo_flags;
101 
102 	ret = panthor_vm_alloc_va(vm, gpu_va, size, &kbo->va_node);
103 	if (ret)
104 		goto err_put_obj;
105 
106 	ret = panthor_vm_map_bo_range(vm, bo, 0, size, kbo->va_node.start, vm_map_flags);
107 	if (ret)
108 		goto err_free_va;
109 
110 	kbo->vm = panthor_vm_get(vm);
111 	bo->exclusive_vm_root_gem = panthor_vm_root_gem(vm);
112 	drm_gem_object_get(bo->exclusive_vm_root_gem);
113 	bo->base.base.resv = bo->exclusive_vm_root_gem->resv;
114 	return kbo;
115 
116 err_free_va:
117 	panthor_vm_free_va(vm, &kbo->va_node);
118 
119 err_put_obj:
120 	drm_gem_object_put(&obj->base);
121 
122 err_free_bo:
123 	kfree(kbo);
124 	return ERR_PTR(ret);
125 }
126 
127 static int panthor_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
128 {
129 	struct panthor_gem_object *bo = to_panthor_bo(obj);
130 
131 	/* Don't allow mmap on objects that have the NO_MMAP flag set. */
132 	if (bo->flags & DRM_PANTHOR_BO_NO_MMAP)
133 		return -EINVAL;
134 
135 	return drm_gem_shmem_object_mmap(obj, vma);
136 }
137 
138 static struct dma_buf *
139 panthor_gem_prime_export(struct drm_gem_object *obj, int flags)
140 {
141 	/* We can't export GEMs that have an exclusive VM. */
142 	if (to_panthor_bo(obj)->exclusive_vm_root_gem)
143 		return ERR_PTR(-EINVAL);
144 
145 	return drm_gem_prime_export(obj, flags);
146 }
147 
148 static const struct drm_gem_object_funcs panthor_gem_funcs = {
149 	.free = panthor_gem_free_object,
150 	.print_info = drm_gem_shmem_object_print_info,
151 	.pin = drm_gem_shmem_object_pin,
152 	.unpin = drm_gem_shmem_object_unpin,
153 	.get_sg_table = drm_gem_shmem_object_get_sg_table,
154 	.vmap = drm_gem_shmem_object_vmap,
155 	.vunmap = drm_gem_shmem_object_vunmap,
156 	.mmap = panthor_gem_mmap,
157 	.export = panthor_gem_prime_export,
158 	.vm_ops = &drm_gem_shmem_vm_ops,
159 };
160 
161 /**
162  * panthor_gem_create_object - Implementation of driver->gem_create_object.
163  * @ddev: DRM device
164  * @size: Size in bytes of the memory the object will reference
165  *
166  * This lets the GEM helpers allocate object structs for us, and keep
167  * our BO stats correct.
168  */
169 struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t size)
170 {
171 	struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
172 	struct panthor_gem_object *obj;
173 
174 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
175 	if (!obj)
176 		return ERR_PTR(-ENOMEM);
177 
178 	obj->base.base.funcs = &panthor_gem_funcs;
179 	obj->base.map_wc = !ptdev->coherent;
180 	mutex_init(&obj->gpuva_list_lock);
181 	drm_gem_gpuva_set_lock(&obj->base.base, &obj->gpuva_list_lock);
182 
183 	return &obj->base.base;
184 }
185 
186 /**
187  * panthor_gem_create_with_handle() - Create a GEM object and attach it to a handle.
188  * @file: DRM file.
189  * @ddev: DRM device.
190  * @exclusive_vm: Exclusive VM. Not NULL if the GEM object can't be shared.
191  * @size: Size of the GEM object to allocate.
192  * @flags: Combination of drm_panthor_bo_flags flags.
193  * @handle: Pointer holding the handle pointing to the new GEM object.
194  *
195  * Return: Zero on success
196  */
197 int
198 panthor_gem_create_with_handle(struct drm_file *file,
199 			       struct drm_device *ddev,
200 			       struct panthor_vm *exclusive_vm,
201 			       u64 *size, u32 flags, u32 *handle)
202 {
203 	int ret;
204 	struct drm_gem_shmem_object *shmem;
205 	struct panthor_gem_object *bo;
206 
207 	shmem = drm_gem_shmem_create(ddev, *size);
208 	if (IS_ERR(shmem))
209 		return PTR_ERR(shmem);
210 
211 	bo = to_panthor_bo(&shmem->base);
212 	bo->flags = flags;
213 
214 	if (exclusive_vm) {
215 		bo->exclusive_vm_root_gem = panthor_vm_root_gem(exclusive_vm);
216 		drm_gem_object_get(bo->exclusive_vm_root_gem);
217 		bo->base.base.resv = bo->exclusive_vm_root_gem->resv;
218 	}
219 
220 	/*
221 	 * Allocate an id of idr table where the obj is registered
222 	 * and handle has the id what user can see.
223 	 */
224 	ret = drm_gem_handle_create(file, &shmem->base, handle);
225 	if (!ret)
226 		*size = bo->base.base.size;
227 
228 	/* drop reference from allocate - handle holds it now. */
229 	drm_gem_object_put(&shmem->base);
230 
231 	return ret;
232 }
233