1 // SPDX-License-Identifier: GPL-2.0 OR MIT
5 #include <linux/sync_file.h>
6 #include <linux/pfn_t.h>
8 #include <drm/drm_file.h>
9 #include <drm/drm_syncobj.h>
10 #include <drm/drm_utils.h>
12 #include <drm/lima_drm.h>
16 #include "lima_gem_prime.h"
18 #include "lima_object.h"
20 int lima_gem_create_handle(struct drm_device *dev, struct drm_file *file,
21 u32 size, u32 flags, u32 *handle)
25 struct lima_device *ldev = to_lima_dev(dev);
27 bo = lima_bo_create(ldev, size, flags, NULL);
31 err = drm_gem_handle_create(file, &bo->gem, handle);
33 /* drop reference from allocate - handle holds it now */
34 drm_gem_object_put_unlocked(&bo->gem);
39 void lima_gem_free_object(struct drm_gem_object *obj)
41 struct lima_bo *bo = to_lima_bo(obj);
43 if (!list_empty(&bo->va))
44 dev_err(obj->dev->dev, "lima gem free bo still has va\n");
49 int lima_gem_object_open(struct drm_gem_object *obj, struct drm_file *file)
51 struct lima_bo *bo = to_lima_bo(obj);
52 struct lima_drm_priv *priv = to_lima_drm_priv(file);
53 struct lima_vm *vm = priv->vm;
55 return lima_vm_bo_add(vm, bo, true);
58 void lima_gem_object_close(struct drm_gem_object *obj, struct drm_file *file)
60 struct lima_bo *bo = to_lima_bo(obj);
61 struct lima_drm_priv *priv = to_lima_drm_priv(file);
62 struct lima_vm *vm = priv->vm;
64 lima_vm_bo_del(vm, bo);
67 int lima_gem_get_info(struct drm_file *file, u32 handle, u32 *va, u64 *offset)
69 struct drm_gem_object *obj;
71 struct lima_drm_priv *priv = to_lima_drm_priv(file);
72 struct lima_vm *vm = priv->vm;
75 obj = drm_gem_object_lookup(file, handle);
81 *va = lima_vm_get_va(vm, bo);
83 err = drm_gem_create_mmap_offset(obj);
85 *offset = drm_vma_node_offset_addr(&obj->vma_node);
87 drm_gem_object_put_unlocked(obj);
91 static vm_fault_t lima_gem_fault(struct vm_fault *vmf)
93 struct vm_area_struct *vma = vmf->vma;
94 struct drm_gem_object *obj = vma->vm_private_data;
95 struct lima_bo *bo = to_lima_bo(obj);
99 /* We don't use vmf->pgoff since that has the fake offset: */
100 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
101 pfn = __pfn_to_pfn_t(page_to_pfn(bo->pages[pgoff]), PFN_DEV);
103 return vmf_insert_mixed(vma, vmf->address, pfn);
106 const struct vm_operations_struct lima_gem_vm_ops = {
107 .fault = lima_gem_fault,
108 .open = drm_gem_vm_open,
109 .close = drm_gem_vm_close,
112 void lima_set_vma_flags(struct vm_area_struct *vma)
114 pgprot_t prot = vm_get_page_prot(vma->vm_flags);
116 vma->vm_flags |= VM_MIXEDMAP;
117 vma->vm_flags &= ~VM_PFNMAP;
118 vma->vm_page_prot = pgprot_writecombine(prot);
121 int lima_gem_mmap(struct file *filp, struct vm_area_struct *vma)
125 ret = drm_gem_mmap(filp, vma);
129 lima_set_vma_flags(vma);
133 static int lima_gem_sync_bo(struct lima_sched_task *task, struct lima_bo *bo,
134 bool write, bool explicit)
139 err = dma_resv_reserve_shared(bo->gem.resv, 1);
144 /* explicit sync use user passed dep fence */
148 return drm_gem_fence_array_add_implicit(&task->deps, &bo->gem, write);
151 static int lima_gem_lock_bos(struct lima_bo **bos, u32 nr_bos,
152 struct ww_acquire_ctx *ctx)
154 int i, ret = 0, contended, slow_locked = -1;
156 ww_acquire_init(ctx, &reservation_ww_class);
159 for (i = 0; i < nr_bos; i++) {
160 if (i == slow_locked) {
165 ret = ww_mutex_lock_interruptible(&bos[i]->gem.resv->lock, ctx);
172 ww_acquire_done(ctx);
176 for (i--; i >= 0; i--)
177 ww_mutex_unlock(&bos[i]->gem.resv->lock);
179 if (slow_locked >= 0)
180 ww_mutex_unlock(&bos[slow_locked]->gem.resv->lock);
182 if (ret == -EDEADLK) {
183 /* we lost out in a seqno race, lock and retry.. */
184 ret = ww_mutex_lock_slow_interruptible(
185 &bos[contended]->gem.resv->lock, ctx);
187 slow_locked = contended;
191 ww_acquire_fini(ctx);
196 static void lima_gem_unlock_bos(struct lima_bo **bos, u32 nr_bos,
197 struct ww_acquire_ctx *ctx)
201 for (i = 0; i < nr_bos; i++)
202 ww_mutex_unlock(&bos[i]->gem.resv->lock);
203 ww_acquire_fini(ctx);
206 static int lima_gem_add_deps(struct drm_file *file, struct lima_submit *submit)
210 for (i = 0; i < ARRAY_SIZE(submit->in_sync); i++) {
211 struct dma_fence *fence = NULL;
213 if (!submit->in_sync[i])
216 err = drm_syncobj_find_fence(file, submit->in_sync[i],
221 err = drm_gem_fence_array_add(&submit->task->deps, fence);
223 dma_fence_put(fence);
231 int lima_gem_submit(struct drm_file *file, struct lima_submit *submit)
234 struct ww_acquire_ctx ctx;
235 struct lima_drm_priv *priv = to_lima_drm_priv(file);
236 struct lima_vm *vm = priv->vm;
237 struct drm_syncobj *out_sync = NULL;
238 struct dma_fence *fence;
239 struct lima_bo **bos = submit->lbos;
241 if (submit->out_sync) {
242 out_sync = drm_syncobj_find(file, submit->out_sync);
247 for (i = 0; i < submit->nr_bos; i++) {
248 struct drm_gem_object *obj;
251 obj = drm_gem_object_lookup(file, submit->bos[i].handle);
257 bo = to_lima_bo(obj);
259 /* increase refcnt of gpu va map to prevent unmapped when executing,
260 * will be decreased when task done
262 err = lima_vm_bo_add(vm, bo, false);
264 drm_gem_object_put_unlocked(obj);
271 err = lima_gem_lock_bos(bos, submit->nr_bos, &ctx);
275 err = lima_sched_task_init(
276 submit->task, submit->ctx->context + submit->pipe,
277 bos, submit->nr_bos, vm);
281 err = lima_gem_add_deps(file, submit);
285 for (i = 0; i < submit->nr_bos; i++) {
286 err = lima_gem_sync_bo(
287 submit->task, bos[i],
288 submit->bos[i].flags & LIMA_SUBMIT_BO_WRITE,
289 submit->flags & LIMA_SUBMIT_FLAG_EXPLICIT_FENCE);
294 fence = lima_sched_context_queue_task(
295 submit->ctx->context + submit->pipe, submit->task);
297 for (i = 0; i < submit->nr_bos; i++) {
298 if (submit->bos[i].flags & LIMA_SUBMIT_BO_WRITE)
299 dma_resv_add_excl_fence(bos[i]->gem.resv, fence);
301 dma_resv_add_shared_fence(bos[i]->gem.resv, fence);
304 lima_gem_unlock_bos(bos, submit->nr_bos, &ctx);
306 for (i = 0; i < submit->nr_bos; i++)
307 drm_gem_object_put_unlocked(&bos[i]->gem);
310 drm_syncobj_replace_fence(out_sync, fence);
311 drm_syncobj_put(out_sync);
314 dma_fence_put(fence);
319 lima_sched_task_fini(submit->task);
321 lima_gem_unlock_bos(bos, submit->nr_bos, &ctx);
323 for (i = 0; i < submit->nr_bos; i++) {
326 lima_vm_bo_del(vm, bos[i]);
327 drm_gem_object_put_unlocked(&bos[i]->gem);
330 drm_syncobj_put(out_sync);
334 int lima_gem_wait(struct drm_file *file, u32 handle, u32 op, s64 timeout_ns)
336 bool write = op & LIMA_GEM_WAIT_WRITE;
342 timeout = drm_timeout_abs_to_jiffies(timeout_ns);
344 ret = drm_gem_dma_resv_wait(file, handle, write, timeout);
346 ret = timeout ? -ETIMEDOUT : -EBUSY;