1 // SPDX-License-Identifier: GPL-2.0 or MIT
3 /* Copyright 2023 Collabora ltd. */
5 #include <linux/dma-buf.h>
6 #include <linux/dma-mapping.h>
8 #include <linux/slab.h>
10 #include <drm/panthor_drm.h>
12 #include "panthor_device.h"
13 #include "panthor_gem.h"
14 #include "panthor_mmu.h"
16 static void panthor_gem_free_object(struct drm_gem_object *obj)
18 struct panthor_gem_object *bo = to_panthor_bo(obj);
19 struct drm_gem_object *vm_root_gem = bo->exclusive_vm_root_gem;
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);
28 * panthor_kernel_bo_destroy() - Destroy a kernel buffer object
29 * @vm: The VM this BO was mapped to.
30 * @bo: Kernel buffer object to destroy. If NULL or an ERR_PTR(), the destruction
33 void panthor_kernel_bo_destroy(struct panthor_vm *vm,
34 struct panthor_kernel_bo *bo)
38 if (IS_ERR_OR_NULL(bo))
41 panthor_kernel_bo_vunmap(bo);
43 if (drm_WARN_ON(bo->obj->dev,
44 to_panthor_bo(bo->obj)->exclusive_vm_root_gem != panthor_vm_root_gem(vm)))
47 ret = panthor_vm_unmap_range(vm, bo->va_node.start,
48 panthor_kernel_bo_size(bo));
52 panthor_vm_free_va(vm, &bo->va_node);
53 drm_gem_object_put(bo->obj);
60 * panthor_kernel_bo_create() - Create and map a GEM object to a VM
62 * @vm: VM to map the GEM to. If NULL, the kernel object is not GPU mapped.
63 * @size: Size of the buffer object.
64 * @bo_flags: Combination of drm_panthor_bo_flags flags.
65 * @vm_map_flags: Combination of drm_panthor_vm_bind_op_flags (only those
66 * that are related to map operations).
67 * @gpu_va: GPU address assigned when mapping to the VM.
68 * If gpu_va == PANTHOR_VM_KERNEL_AUTO_VA, the virtual address will be
69 * automatically allocated.
71 * Return: A valid pointer in case of success, an ERR_PTR() otherwise.
73 struct panthor_kernel_bo *
74 panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
75 size_t size, u32 bo_flags, u32 vm_map_flags,
78 struct drm_gem_shmem_object *obj;
79 struct panthor_kernel_bo *kbo;
80 struct panthor_gem_object *bo;
83 if (drm_WARN_ON(&ptdev->base, !vm))
84 return ERR_PTR(-EINVAL);
86 kbo = kzalloc(sizeof(*kbo), GFP_KERNEL);
88 return ERR_PTR(-ENOMEM);
90 obj = drm_gem_shmem_create(&ptdev->base, size);
96 bo = to_panthor_bo(&obj->base);
97 size = obj->base.size;
98 kbo->obj = &obj->base;
101 ret = panthor_vm_alloc_va(vm, gpu_va, size, &kbo->va_node);
105 ret = panthor_vm_map_bo_range(vm, bo, 0, size, kbo->va_node.start, vm_map_flags);
109 bo->exclusive_vm_root_gem = panthor_vm_root_gem(vm);
110 drm_gem_object_get(bo->exclusive_vm_root_gem);
111 bo->base.base.resv = bo->exclusive_vm_root_gem->resv;
115 panthor_vm_free_va(vm, &kbo->va_node);
118 drm_gem_object_put(&obj->base);
125 static int panthor_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
127 struct panthor_gem_object *bo = to_panthor_bo(obj);
129 /* Don't allow mmap on objects that have the NO_MMAP flag set. */
130 if (bo->flags & DRM_PANTHOR_BO_NO_MMAP)
133 return drm_gem_shmem_object_mmap(obj, vma);
136 static struct dma_buf *
137 panthor_gem_prime_export(struct drm_gem_object *obj, int flags)
139 /* We can't export GEMs that have an exclusive VM. */
140 if (to_panthor_bo(obj)->exclusive_vm_root_gem)
141 return ERR_PTR(-EINVAL);
143 return drm_gem_prime_export(obj, flags);
146 static const struct drm_gem_object_funcs panthor_gem_funcs = {
147 .free = panthor_gem_free_object,
148 .print_info = drm_gem_shmem_object_print_info,
149 .pin = drm_gem_shmem_object_pin,
150 .unpin = drm_gem_shmem_object_unpin,
151 .get_sg_table = drm_gem_shmem_object_get_sg_table,
152 .vmap = drm_gem_shmem_object_vmap,
153 .vunmap = drm_gem_shmem_object_vunmap,
154 .mmap = panthor_gem_mmap,
155 .export = panthor_gem_prime_export,
156 .vm_ops = &drm_gem_shmem_vm_ops,
160 * panthor_gem_create_object - Implementation of driver->gem_create_object.
162 * @size: Size in bytes of the memory the object will reference
164 * This lets the GEM helpers allocate object structs for us, and keep
165 * our BO stats correct.
167 struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t size)
169 struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
170 struct panthor_gem_object *obj;
172 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
174 return ERR_PTR(-ENOMEM);
176 obj->base.base.funcs = &panthor_gem_funcs;
177 obj->base.map_wc = !ptdev->coherent;
178 mutex_init(&obj->gpuva_list_lock);
179 drm_gem_gpuva_set_lock(&obj->base.base, &obj->gpuva_list_lock);
181 return &obj->base.base;
185 * panthor_gem_create_with_handle() - Create a GEM object and attach it to a handle.
188 * @exclusive_vm: Exclusive VM. Not NULL if the GEM object can't be shared.
189 * @size: Size of the GEM object to allocate.
190 * @flags: Combination of drm_panthor_bo_flags flags.
191 * @handle: Pointer holding the handle pointing to the new GEM object.
193 * Return: Zero on success
196 panthor_gem_create_with_handle(struct drm_file *file,
197 struct drm_device *ddev,
198 struct panthor_vm *exclusive_vm,
199 u64 *size, u32 flags, u32 *handle)
202 struct drm_gem_shmem_object *shmem;
203 struct panthor_gem_object *bo;
205 shmem = drm_gem_shmem_create(ddev, *size);
207 return PTR_ERR(shmem);
209 bo = to_panthor_bo(&shmem->base);
213 bo->exclusive_vm_root_gem = panthor_vm_root_gem(exclusive_vm);
214 drm_gem_object_get(bo->exclusive_vm_root_gem);
215 bo->base.base.resv = bo->exclusive_vm_root_gem->resv;
219 * Allocate an id of idr table where the obj is registered
220 * and handle has the id what user can see.
222 ret = drm_gem_handle_create(file, &shmem->base, handle);
224 *size = bo->base.base.size;
226 /* drop reference from allocate - handle holds it now. */
227 drm_gem_object_put(&shmem->base);