]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c
drm/etnaviv: drop use of drmP.h
[linux.git] / drivers / gpu / drm / etnaviv / etnaviv_gem_submit.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 Etnaviv Project
4  */
5
6 #include <drm/drm_file.h>
7 #include <linux/dma-fence-array.h>
8 #include <linux/file.h>
9 #include <linux/reservation.h>
10 #include <linux/sync_file.h>
11 #include <linux/uaccess.h>
12 #include <linux/vmalloc.h>
13
14 #include "etnaviv_cmdbuf.h"
15 #include "etnaviv_drv.h"
16 #include "etnaviv_gpu.h"
17 #include "etnaviv_gem.h"
18 #include "etnaviv_perfmon.h"
19 #include "etnaviv_sched.h"
20
21 /*
22  * Cmdstream submission:
23  */
24
25 #define BO_INVALID_FLAGS ~(ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE)
26 /* make sure these don't conflict w/ ETNAVIV_SUBMIT_BO_x */
27 #define BO_LOCKED   0x4000
28 #define BO_PINNED   0x2000
29
30 static struct etnaviv_gem_submit *submit_create(struct drm_device *dev,
31                 struct etnaviv_gpu *gpu, size_t nr_bos, size_t nr_pmrs)
32 {
33         struct etnaviv_gem_submit *submit;
34         size_t sz = size_vstruct(nr_bos, sizeof(submit->bos[0]), sizeof(*submit));
35
36         submit = kzalloc(sz, GFP_KERNEL);
37         if (!submit)
38                 return NULL;
39
40         submit->pmrs = kcalloc(nr_pmrs, sizeof(struct etnaviv_perfmon_request),
41                                GFP_KERNEL);
42         if (!submit->pmrs) {
43                 kfree(submit);
44                 return NULL;
45         }
46         submit->nr_pmrs = nr_pmrs;
47
48         submit->gpu = gpu;
49         kref_init(&submit->refcount);
50
51         return submit;
52 }
53
54 static int submit_lookup_objects(struct etnaviv_gem_submit *submit,
55         struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos,
56         unsigned nr_bos)
57 {
58         struct drm_etnaviv_gem_submit_bo *bo;
59         unsigned i;
60         int ret = 0;
61
62         spin_lock(&file->table_lock);
63
64         for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) {
65                 struct drm_gem_object *obj;
66
67                 if (bo->flags & BO_INVALID_FLAGS) {
68                         DRM_ERROR("invalid flags: %x\n", bo->flags);
69                         ret = -EINVAL;
70                         goto out_unlock;
71                 }
72
73                 submit->bos[i].flags = bo->flags;
74
75                 /* normally use drm_gem_object_lookup(), but for bulk lookup
76                  * all under single table_lock just hit object_idr directly:
77                  */
78                 obj = idr_find(&file->object_idr, bo->handle);
79                 if (!obj) {
80                         DRM_ERROR("invalid handle %u at index %u\n",
81                                   bo->handle, i);
82                         ret = -EINVAL;
83                         goto out_unlock;
84                 }
85
86                 /*
87                  * Take a refcount on the object. The file table lock
88                  * prevents the object_idr's refcount on this being dropped.
89                  */
90                 drm_gem_object_get(obj);
91
92                 submit->bos[i].obj = to_etnaviv_bo(obj);
93         }
94
95 out_unlock:
96         submit->nr_bos = i;
97         spin_unlock(&file->table_lock);
98
99         return ret;
100 }
101
102 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i)
103 {
104         if (submit->bos[i].flags & BO_LOCKED) {
105                 struct drm_gem_object *obj = &submit->bos[i].obj->base;
106
107                 ww_mutex_unlock(&obj->resv->lock);
108                 submit->bos[i].flags &= ~BO_LOCKED;
109         }
110 }
111
112 static int submit_lock_objects(struct etnaviv_gem_submit *submit,
113                 struct ww_acquire_ctx *ticket)
114 {
115         int contended, slow_locked = -1, i, ret = 0;
116
117 retry:
118         for (i = 0; i < submit->nr_bos; i++) {
119                 struct drm_gem_object *obj = &submit->bos[i].obj->base;
120
121                 if (slow_locked == i)
122                         slow_locked = -1;
123
124                 contended = i;
125
126                 if (!(submit->bos[i].flags & BO_LOCKED)) {
127                         ret = ww_mutex_lock_interruptible(&obj->resv->lock,
128                                                           ticket);
129                         if (ret == -EALREADY)
130                                 DRM_ERROR("BO at index %u already on submit list\n",
131                                           i);
132                         if (ret)
133                                 goto fail;
134                         submit->bos[i].flags |= BO_LOCKED;
135                 }
136         }
137
138         ww_acquire_done(ticket);
139
140         return 0;
141
142 fail:
143         for (; i >= 0; i--)
144                 submit_unlock_object(submit, i);
145
146         if (slow_locked > 0)
147                 submit_unlock_object(submit, slow_locked);
148
149         if (ret == -EDEADLK) {
150                 struct drm_gem_object *obj;
151
152                 obj = &submit->bos[contended].obj->base;
153
154                 /* we lost out in a seqno race, lock and retry.. */
155                 ret = ww_mutex_lock_slow_interruptible(&obj->resv->lock,
156                                                        ticket);
157                 if (!ret) {
158                         submit->bos[contended].flags |= BO_LOCKED;
159                         slow_locked = contended;
160                         goto retry;
161                 }
162         }
163
164         return ret;
165 }
166
167 static int submit_fence_sync(struct etnaviv_gem_submit *submit)
168 {
169         int i, ret = 0;
170
171         for (i = 0; i < submit->nr_bos; i++) {
172                 struct etnaviv_gem_submit_bo *bo = &submit->bos[i];
173                 struct reservation_object *robj = bo->obj->base.resv;
174
175                 if (!(bo->flags & ETNA_SUBMIT_BO_WRITE)) {
176                         ret = reservation_object_reserve_shared(robj, 1);
177                         if (ret)
178                                 return ret;
179                 }
180
181                 if (submit->flags & ETNA_SUBMIT_NO_IMPLICIT)
182                         continue;
183
184                 if (bo->flags & ETNA_SUBMIT_BO_WRITE) {
185                         ret = reservation_object_get_fences_rcu(robj, &bo->excl,
186                                                                 &bo->nr_shared,
187                                                                 &bo->shared);
188                         if (ret)
189                                 return ret;
190                 } else {
191                         bo->excl = reservation_object_get_excl_rcu(robj);
192                 }
193
194         }
195
196         return ret;
197 }
198
199 static void submit_attach_object_fences(struct etnaviv_gem_submit *submit)
200 {
201         int i;
202
203         for (i = 0; i < submit->nr_bos; i++) {
204                 struct drm_gem_object *obj = &submit->bos[i].obj->base;
205
206                 if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE)
207                         reservation_object_add_excl_fence(obj->resv,
208                                                           submit->out_fence);
209                 else
210                         reservation_object_add_shared_fence(obj->resv,
211                                                             submit->out_fence);
212
213                 submit_unlock_object(submit, i);
214         }
215 }
216
217 static int submit_pin_objects(struct etnaviv_gem_submit *submit)
218 {
219         int i, ret = 0;
220
221         for (i = 0; i < submit->nr_bos; i++) {
222                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
223                 struct etnaviv_vram_mapping *mapping;
224
225                 mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
226                                                   submit->gpu);
227                 if (IS_ERR(mapping)) {
228                         ret = PTR_ERR(mapping);
229                         break;
230                 }
231                 atomic_inc(&etnaviv_obj->gpu_active);
232
233                 submit->bos[i].flags |= BO_PINNED;
234                 submit->bos[i].mapping = mapping;
235         }
236
237         return ret;
238 }
239
240 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx,
241         struct etnaviv_gem_submit_bo **bo)
242 {
243         if (idx >= submit->nr_bos) {
244                 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
245                                 idx, submit->nr_bos);
246                 return -EINVAL;
247         }
248
249         *bo = &submit->bos[idx];
250
251         return 0;
252 }
253
254 /* process the reloc's and patch up the cmdstream as needed: */
255 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream,
256                 u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs,
257                 u32 nr_relocs)
258 {
259         u32 i, last_offset = 0;
260         u32 *ptr = stream;
261         int ret;
262
263         for (i = 0; i < nr_relocs; i++) {
264                 const struct drm_etnaviv_gem_submit_reloc *r = relocs + i;
265                 struct etnaviv_gem_submit_bo *bo;
266                 u32 off;
267
268                 if (unlikely(r->flags)) {
269                         DRM_ERROR("invalid reloc flags\n");
270                         return -EINVAL;
271                 }
272
273                 if (r->submit_offset % 4) {
274                         DRM_ERROR("non-aligned reloc offset: %u\n",
275                                   r->submit_offset);
276                         return -EINVAL;
277                 }
278
279                 /* offset in dwords: */
280                 off = r->submit_offset / 4;
281
282                 if ((off >= size ) ||
283                                 (off < last_offset)) {
284                         DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
285                         return -EINVAL;
286                 }
287
288                 ret = submit_bo(submit, r->reloc_idx, &bo);
289                 if (ret)
290                         return ret;
291
292                 if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) {
293                         DRM_ERROR("relocation %u outside object\n", i);
294                         return -EINVAL;
295                 }
296
297                 ptr[off] = bo->mapping->iova + r->reloc_offset;
298
299                 last_offset = off;
300         }
301
302         return 0;
303 }
304
305 static int submit_perfmon_validate(struct etnaviv_gem_submit *submit,
306                 u32 exec_state, const struct drm_etnaviv_gem_submit_pmr *pmrs)
307 {
308         u32 i;
309
310         for (i = 0; i < submit->nr_pmrs; i++) {
311                 const struct drm_etnaviv_gem_submit_pmr *r = pmrs + i;
312                 struct etnaviv_gem_submit_bo *bo;
313                 int ret;
314
315                 ret = submit_bo(submit, r->read_idx, &bo);
316                 if (ret)
317                         return ret;
318
319                 /* at offset 0 a sequence number gets stored used for userspace sync */
320                 if (r->read_offset == 0) {
321                         DRM_ERROR("perfmon request: offset is 0");
322                         return -EINVAL;
323                 }
324
325                 if (r->read_offset >= bo->obj->base.size - sizeof(u32)) {
326                         DRM_ERROR("perfmon request: offset %u outside object", i);
327                         return -EINVAL;
328                 }
329
330                 if (r->flags & ~(ETNA_PM_PROCESS_PRE | ETNA_PM_PROCESS_POST)) {
331                         DRM_ERROR("perfmon request: flags are not valid");
332                         return -EINVAL;
333                 }
334
335                 if (etnaviv_pm_req_validate(r, exec_state)) {
336                         DRM_ERROR("perfmon request: domain or signal not valid");
337                         return -EINVAL;
338                 }
339
340                 submit->pmrs[i].flags = r->flags;
341                 submit->pmrs[i].domain = r->domain;
342                 submit->pmrs[i].signal = r->signal;
343                 submit->pmrs[i].sequence = r->sequence;
344                 submit->pmrs[i].offset = r->read_offset;
345                 submit->pmrs[i].bo_vma = etnaviv_gem_vmap(&bo->obj->base);
346         }
347
348         return 0;
349 }
350
351 static void submit_cleanup(struct kref *kref)
352 {
353         struct etnaviv_gem_submit *submit =
354                         container_of(kref, struct etnaviv_gem_submit, refcount);
355         unsigned i;
356
357         if (submit->runtime_resumed)
358                 pm_runtime_put_autosuspend(submit->gpu->dev);
359
360         if (submit->cmdbuf.suballoc)
361                 etnaviv_cmdbuf_free(&submit->cmdbuf);
362
363         for (i = 0; i < submit->nr_bos; i++) {
364                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
365
366                 /* unpin all objects */
367                 if (submit->bos[i].flags & BO_PINNED) {
368                         etnaviv_gem_mapping_unreference(submit->bos[i].mapping);
369                         atomic_dec(&etnaviv_obj->gpu_active);
370                         submit->bos[i].mapping = NULL;
371                         submit->bos[i].flags &= ~BO_PINNED;
372                 }
373
374                 /* if the GPU submit failed, objects might still be locked */
375                 submit_unlock_object(submit, i);
376                 drm_gem_object_put_unlocked(&etnaviv_obj->base);
377         }
378
379         wake_up_all(&submit->gpu->fence_event);
380
381         if (submit->in_fence)
382                 dma_fence_put(submit->in_fence);
383         if (submit->out_fence) {
384                 /* first remove from IDR, so fence can not be found anymore */
385                 mutex_lock(&submit->gpu->fence_lock);
386                 idr_remove(&submit->gpu->fence_idr, submit->out_fence_id);
387                 mutex_unlock(&submit->gpu->fence_lock);
388                 dma_fence_put(submit->out_fence);
389         }
390         kfree(submit->pmrs);
391         kfree(submit);
392 }
393
394 void etnaviv_submit_put(struct etnaviv_gem_submit *submit)
395 {
396         kref_put(&submit->refcount, submit_cleanup);
397 }
398
399 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
400                 struct drm_file *file)
401 {
402         struct etnaviv_file_private *ctx = file->driver_priv;
403         struct etnaviv_drm_private *priv = dev->dev_private;
404         struct drm_etnaviv_gem_submit *args = data;
405         struct drm_etnaviv_gem_submit_reloc *relocs;
406         struct drm_etnaviv_gem_submit_pmr *pmrs;
407         struct drm_etnaviv_gem_submit_bo *bos;
408         struct etnaviv_gem_submit *submit;
409         struct etnaviv_gpu *gpu;
410         struct sync_file *sync_file = NULL;
411         struct ww_acquire_ctx ticket;
412         int out_fence_fd = -1;
413         void *stream;
414         int ret;
415
416         if (args->pipe >= ETNA_MAX_PIPES)
417                 return -EINVAL;
418
419         gpu = priv->gpu[args->pipe];
420         if (!gpu)
421                 return -ENXIO;
422
423         if (args->stream_size % 4) {
424                 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
425                           args->stream_size);
426                 return -EINVAL;
427         }
428
429         if (args->exec_state != ETNA_PIPE_3D &&
430             args->exec_state != ETNA_PIPE_2D &&
431             args->exec_state != ETNA_PIPE_VG) {
432                 DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state);
433                 return -EINVAL;
434         }
435
436         if (args->flags & ~ETNA_SUBMIT_FLAGS) {
437                 DRM_ERROR("invalid flags: 0x%x\n", args->flags);
438                 return -EINVAL;
439         }
440
441         /*
442          * Copy the command submission and bo array to kernel space in
443          * one go, and do this outside of any locks.
444          */
445         bos = kvmalloc_array(args->nr_bos, sizeof(*bos), GFP_KERNEL);
446         relocs = kvmalloc_array(args->nr_relocs, sizeof(*relocs), GFP_KERNEL);
447         pmrs = kvmalloc_array(args->nr_pmrs, sizeof(*pmrs), GFP_KERNEL);
448         stream = kvmalloc_array(1, args->stream_size, GFP_KERNEL);
449         if (!bos || !relocs || !pmrs || !stream) {
450                 ret = -ENOMEM;
451                 goto err_submit_cmds;
452         }
453
454         ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
455                              args->nr_bos * sizeof(*bos));
456         if (ret) {
457                 ret = -EFAULT;
458                 goto err_submit_cmds;
459         }
460
461         ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
462                              args->nr_relocs * sizeof(*relocs));
463         if (ret) {
464                 ret = -EFAULT;
465                 goto err_submit_cmds;
466         }
467
468         ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs),
469                              args->nr_pmrs * sizeof(*pmrs));
470         if (ret) {
471                 ret = -EFAULT;
472                 goto err_submit_cmds;
473         }
474
475         ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
476                              args->stream_size);
477         if (ret) {
478                 ret = -EFAULT;
479                 goto err_submit_cmds;
480         }
481
482         if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
483                 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
484                 if (out_fence_fd < 0) {
485                         ret = out_fence_fd;
486                         goto err_submit_cmds;
487                 }
488         }
489
490         ww_acquire_init(&ticket, &reservation_ww_class);
491
492         submit = submit_create(dev, gpu, args->nr_bos, args->nr_pmrs);
493         if (!submit) {
494                 ret = -ENOMEM;
495                 goto err_submit_ww_acquire;
496         }
497
498         ret = etnaviv_cmdbuf_init(gpu->cmdbuf_suballoc, &submit->cmdbuf,
499                                   ALIGN(args->stream_size, 8) + 8);
500         if (ret)
501                 goto err_submit_objects;
502
503         submit->ctx = file->driver_priv;
504         submit->exec_state = args->exec_state;
505         submit->flags = args->flags;
506
507         ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
508         if (ret)
509                 goto err_submit_objects;
510
511         if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
512                                       relocs, args->nr_relocs)) {
513                 ret = -EINVAL;
514                 goto err_submit_objects;
515         }
516
517         if (args->flags & ETNA_SUBMIT_FENCE_FD_IN) {
518                 submit->in_fence = sync_file_get_fence(args->fence_fd);
519                 if (!submit->in_fence) {
520                         ret = -EINVAL;
521                         goto err_submit_objects;
522                 }
523         }
524
525         ret = submit_pin_objects(submit);
526         if (ret)
527                 goto err_submit_objects;
528
529         ret = submit_reloc(submit, stream, args->stream_size / 4,
530                            relocs, args->nr_relocs);
531         if (ret)
532                 goto err_submit_objects;
533
534         ret = submit_perfmon_validate(submit, args->exec_state, pmrs);
535         if (ret)
536                 goto err_submit_objects;
537
538         memcpy(submit->cmdbuf.vaddr, stream, args->stream_size);
539
540         ret = submit_lock_objects(submit, &ticket);
541         if (ret)
542                 goto err_submit_objects;
543
544         ret = submit_fence_sync(submit);
545         if (ret)
546                 goto err_submit_objects;
547
548         ret = etnaviv_sched_push_job(&ctx->sched_entity[args->pipe], submit);
549         if (ret)
550                 goto err_submit_objects;
551
552         submit_attach_object_fences(submit);
553
554         if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
555                 /*
556                  * This can be improved: ideally we want to allocate the sync
557                  * file before kicking off the GPU job and just attach the
558                  * fence to the sync file here, eliminating the ENOMEM
559                  * possibility at this stage.
560                  */
561                 sync_file = sync_file_create(submit->out_fence);
562                 if (!sync_file) {
563                         ret = -ENOMEM;
564                         goto err_submit_objects;
565                 }
566                 fd_install(out_fence_fd, sync_file->file);
567         }
568
569         args->fence_fd = out_fence_fd;
570         args->fence = submit->out_fence_id;
571
572 err_submit_objects:
573         etnaviv_submit_put(submit);
574
575 err_submit_ww_acquire:
576         ww_acquire_fini(&ticket);
577
578 err_submit_cmds:
579         if (ret && (out_fence_fd >= 0))
580                 put_unused_fd(out_fence_fd);
581         if (stream)
582                 kvfree(stream);
583         if (bos)
584                 kvfree(bos);
585         if (relocs)
586                 kvfree(relocs);
587         if (pmrs)
588                 kvfree(pmrs);
589
590         return ret;
591 }