d87a7f00bca33573a5bd0186c5c82c4376fa0274
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / etnaviv / etnaviv_gem_submit.c
1 /*
2  * Copyright (C) 2015 Etnaviv Project
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include <linux/dma-fence-array.h>
18 #include <linux/reservation.h>
19 #include <linux/sync_file.h>
20 #include "etnaviv_cmdbuf.h"
21 #include "etnaviv_drv.h"
22 #include "etnaviv_gpu.h"
23 #include "etnaviv_gem.h"
24 #include "etnaviv_perfmon.h"
25
26 /*
27  * Cmdstream submission:
28  */
29
30 #define BO_INVALID_FLAGS ~(ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE)
31 /* make sure these don't conflict w/ ETNAVIV_SUBMIT_BO_x */
32 #define BO_LOCKED   0x4000
33 #define BO_PINNED   0x2000
34
35 static struct etnaviv_gem_submit *submit_create(struct drm_device *dev,
36                 struct etnaviv_gpu *gpu, size_t nr_bos, size_t nr_pmrs)
37 {
38         struct etnaviv_gem_submit *submit;
39         size_t sz = size_vstruct(nr_bos, sizeof(submit->bos[0]), sizeof(*submit));
40
41         submit = kzalloc(sz, GFP_KERNEL);
42         if (!submit)
43                 return NULL;
44
45         submit->pmrs = kcalloc(nr_pmrs, sizeof(struct etnaviv_perfmon_request),
46                                GFP_KERNEL);
47         if (!submit->pmrs) {
48                 kfree(submit);
49                 return NULL;
50         }
51         submit->nr_pmrs = nr_pmrs;
52
53         submit->gpu = gpu;
54         kref_init(&submit->refcount);
55
56         return submit;
57 }
58
59 static int submit_lookup_objects(struct etnaviv_gem_submit *submit,
60         struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos,
61         unsigned nr_bos)
62 {
63         struct drm_etnaviv_gem_submit_bo *bo;
64         unsigned i;
65         int ret = 0;
66
67         spin_lock(&file->table_lock);
68
69         for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) {
70                 struct drm_gem_object *obj;
71
72                 if (bo->flags & BO_INVALID_FLAGS) {
73                         DRM_ERROR("invalid flags: %x\n", bo->flags);
74                         ret = -EINVAL;
75                         goto out_unlock;
76                 }
77
78                 submit->bos[i].flags = bo->flags;
79
80                 /* normally use drm_gem_object_lookup(), but for bulk lookup
81                  * all under single table_lock just hit object_idr directly:
82                  */
83                 obj = idr_find(&file->object_idr, bo->handle);
84                 if (!obj) {
85                         DRM_ERROR("invalid handle %u at index %u\n",
86                                   bo->handle, i);
87                         ret = -EINVAL;
88                         goto out_unlock;
89                 }
90
91                 /*
92                  * Take a refcount on the object. The file table lock
93                  * prevents the object_idr's refcount on this being dropped.
94                  */
95                 drm_gem_object_get(obj);
96
97                 submit->bos[i].obj = to_etnaviv_bo(obj);
98         }
99
100 out_unlock:
101         submit->nr_bos = i;
102         spin_unlock(&file->table_lock);
103
104         return ret;
105 }
106
107 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i)
108 {
109         if (submit->bos[i].flags & BO_LOCKED) {
110                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
111
112                 ww_mutex_unlock(&etnaviv_obj->resv->lock);
113                 submit->bos[i].flags &= ~BO_LOCKED;
114         }
115 }
116
117 static int submit_lock_objects(struct etnaviv_gem_submit *submit,
118                 struct ww_acquire_ctx *ticket)
119 {
120         int contended, slow_locked = -1, i, ret = 0;
121
122 retry:
123         for (i = 0; i < submit->nr_bos; i++) {
124                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
125
126                 if (slow_locked == i)
127                         slow_locked = -1;
128
129                 contended = i;
130
131                 if (!(submit->bos[i].flags & BO_LOCKED)) {
132                         ret = ww_mutex_lock_interruptible(&etnaviv_obj->resv->lock,
133                                                           ticket);
134                         if (ret == -EALREADY)
135                                 DRM_ERROR("BO at index %u already on submit list\n",
136                                           i);
137                         if (ret)
138                                 goto fail;
139                         submit->bos[i].flags |= BO_LOCKED;
140                 }
141         }
142
143         ww_acquire_done(ticket);
144
145         return 0;
146
147 fail:
148         for (; i >= 0; i--)
149                 submit_unlock_object(submit, i);
150
151         if (slow_locked > 0)
152                 submit_unlock_object(submit, slow_locked);
153
154         if (ret == -EDEADLK) {
155                 struct etnaviv_gem_object *etnaviv_obj;
156
157                 etnaviv_obj = submit->bos[contended].obj;
158
159                 /* we lost out in a seqno race, lock and retry.. */
160                 ret = ww_mutex_lock_slow_interruptible(&etnaviv_obj->resv->lock,
161                                                        ticket);
162                 if (!ret) {
163                         submit->bos[contended].flags |= BO_LOCKED;
164                         slow_locked = contended;
165                         goto retry;
166                 }
167         }
168
169         return ret;
170 }
171
172 static int submit_fence_sync(const struct etnaviv_gem_submit *submit)
173 {
174         unsigned int context = submit->gpu->fence_context;
175         int i, ret = 0;
176
177         for (i = 0; i < submit->nr_bos; i++) {
178                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
179                 bool write = submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE;
180                 bool explicit = !!(submit->flags & ETNA_SUBMIT_NO_IMPLICIT);
181
182                 ret = etnaviv_gpu_fence_sync_obj(etnaviv_obj, context, write,
183                                                  explicit);
184                 if (ret)
185                         break;
186         }
187
188         if (submit->flags & ETNA_SUBMIT_FENCE_FD_IN) {
189                 /*
190                  * Wait if the fence is from a foreign context, or if the fence
191                  * array contains any fence from a foreign context.
192                  */
193                 if (!dma_fence_match_context(submit->in_fence, context))
194                         ret = dma_fence_wait(submit->in_fence, true);
195         }
196
197         return ret;
198 }
199
200 static void submit_attach_object_fences(struct etnaviv_gem_submit *submit)
201 {
202         int i;
203
204         for (i = 0; i < submit->nr_bos; i++) {
205                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
206
207                 if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE)
208                         reservation_object_add_excl_fence(etnaviv_obj->resv,
209                                                           submit->out_fence);
210                 else
211                         reservation_object_add_shared_fence(etnaviv_obj->resv,
212                                                             submit->out_fence);
213
214                 submit_unlock_object(submit, i);
215         }
216 }
217
218 static int submit_pin_objects(struct etnaviv_gem_submit *submit)
219 {
220         int i, ret = 0;
221
222         for (i = 0; i < submit->nr_bos; i++) {
223                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
224                 struct etnaviv_vram_mapping *mapping;
225
226                 mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
227                                                   submit->gpu);
228                 if (IS_ERR(mapping)) {
229                         ret = PTR_ERR(mapping);
230                         break;
231                 }
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         for (i = 0; i < submit->nr_bos; i++) {
358                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
359
360                 /* unpin all objects */
361                 if (submit->bos[i].flags & BO_PINNED) {
362                         etnaviv_gem_mapping_unreference(submit->bos[i].mapping);
363                         submit->bos[i].mapping = NULL;
364                         submit->bos[i].flags &= ~BO_PINNED;
365                 }
366
367                 /* if the GPU submit failed, objects might still be locked */
368                 submit_unlock_object(submit, i);
369                 drm_gem_object_put_unlocked(&etnaviv_obj->base);
370         }
371
372         if (submit->in_fence)
373                 dma_fence_put(submit->in_fence);
374         if (submit->out_fence)
375                 dma_fence_put(submit->out_fence);
376         kfree(submit->pmrs);
377         kfree(submit);
378 }
379
380 void etnaviv_submit_put(struct etnaviv_gem_submit *submit)
381 {
382         kref_put(&submit->refcount, submit_cleanup);
383 }
384
385 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
386                 struct drm_file *file)
387 {
388         struct etnaviv_drm_private *priv = dev->dev_private;
389         struct drm_etnaviv_gem_submit *args = data;
390         struct drm_etnaviv_gem_submit_reloc *relocs;
391         struct drm_etnaviv_gem_submit_pmr *pmrs;
392         struct drm_etnaviv_gem_submit_bo *bos;
393         struct etnaviv_gem_submit *submit;
394         struct etnaviv_cmdbuf *cmdbuf;
395         struct etnaviv_gpu *gpu;
396         struct sync_file *sync_file = NULL;
397         struct ww_acquire_ctx ticket;
398         int out_fence_fd = -1;
399         void *stream;
400         int ret;
401
402         if (args->pipe >= ETNA_MAX_PIPES)
403                 return -EINVAL;
404
405         gpu = priv->gpu[args->pipe];
406         if (!gpu)
407                 return -ENXIO;
408
409         if (args->stream_size % 4) {
410                 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
411                           args->stream_size);
412                 return -EINVAL;
413         }
414
415         if (args->exec_state != ETNA_PIPE_3D &&
416             args->exec_state != ETNA_PIPE_2D &&
417             args->exec_state != ETNA_PIPE_VG) {
418                 DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state);
419                 return -EINVAL;
420         }
421
422         if (args->flags & ~ETNA_SUBMIT_FLAGS) {
423                 DRM_ERROR("invalid flags: 0x%x\n", args->flags);
424                 return -EINVAL;
425         }
426
427         /*
428          * Copy the command submission and bo array to kernel space in
429          * one go, and do this outside of any locks.
430          */
431         bos = kvmalloc_array(args->nr_bos, sizeof(*bos), GFP_KERNEL);
432         relocs = kvmalloc_array(args->nr_relocs, sizeof(*relocs), GFP_KERNEL);
433         pmrs = kvmalloc_array(args->nr_pmrs, sizeof(*pmrs), GFP_KERNEL);
434         stream = kvmalloc_array(1, args->stream_size, GFP_KERNEL);
435         cmdbuf = etnaviv_cmdbuf_new(gpu->cmdbuf_suballoc,
436                                     ALIGN(args->stream_size, 8) + 8,
437                                     args->nr_bos);
438         if (!bos || !relocs || !pmrs || !stream || !cmdbuf) {
439                 ret = -ENOMEM;
440                 goto err_submit_cmds;
441         }
442
443         cmdbuf->ctx = file->driver_priv;
444
445         ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
446                              args->nr_bos * sizeof(*bos));
447         if (ret) {
448                 ret = -EFAULT;
449                 goto err_submit_cmds;
450         }
451
452         ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
453                              args->nr_relocs * sizeof(*relocs));
454         if (ret) {
455                 ret = -EFAULT;
456                 goto err_submit_cmds;
457         }
458
459         ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs),
460                              args->nr_pmrs * sizeof(*pmrs));
461         if (ret) {
462                 ret = -EFAULT;
463                 goto err_submit_cmds;
464         }
465
466         ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
467                              args->stream_size);
468         if (ret) {
469                 ret = -EFAULT;
470                 goto err_submit_cmds;
471         }
472
473         if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
474                 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
475                 if (out_fence_fd < 0) {
476                         ret = out_fence_fd;
477                         goto err_submit_cmds;
478                 }
479         }
480
481         ww_acquire_init(&ticket, &reservation_ww_class);
482
483         submit = submit_create(dev, gpu, args->nr_bos, args->nr_pmrs);
484         if (!submit) {
485                 ret = -ENOMEM;
486                 goto err_submit_ww_acquire;
487         }
488
489         submit->exec_state = args->exec_state;
490         submit->flags = args->flags;
491
492         ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
493         if (ret)
494                 goto err_submit_objects;
495
496         ret = submit_lock_objects(submit, &ticket);
497         if (ret)
498                 goto err_submit_objects;
499
500         if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
501                                       relocs, args->nr_relocs)) {
502                 ret = -EINVAL;
503                 goto err_submit_objects;
504         }
505
506         if (args->flags & ETNA_SUBMIT_FENCE_FD_IN) {
507                 submit->in_fence = sync_file_get_fence(args->fence_fd);
508                 if (!submit->in_fence) {
509                         ret = -EINVAL;
510                         goto err_submit_objects;
511                 }
512         }
513
514         ret = submit_fence_sync(submit);
515         if (ret)
516                 goto err_submit_objects;
517
518         ret = submit_pin_objects(submit);
519         if (ret)
520                 goto err_submit_objects;
521
522         ret = submit_reloc(submit, stream, args->stream_size / 4,
523                            relocs, args->nr_relocs);
524         if (ret)
525                 goto err_submit_objects;
526
527         ret = submit_perfmon_validate(submit, args->exec_state, pmrs);
528         if (ret)
529                 goto err_submit_objects;
530
531         memcpy(cmdbuf->vaddr, stream, args->stream_size);
532         cmdbuf->user_size = ALIGN(args->stream_size, 8);
533
534         ret = etnaviv_gpu_submit(gpu, submit, cmdbuf);
535         if (ret)
536                 goto err_submit_objects;
537
538         submit_attach_object_fences(submit);
539
540         cmdbuf = NULL;
541
542         if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
543                 /*
544                  * This can be improved: ideally we want to allocate the sync
545                  * file before kicking off the GPU job and just attach the
546                  * fence to the sync file here, eliminating the ENOMEM
547                  * possibility at this stage.
548                  */
549                 sync_file = sync_file_create(submit->out_fence);
550                 if (!sync_file) {
551                         ret = -ENOMEM;
552                         goto err_submit_objects;
553                 }
554                 fd_install(out_fence_fd, sync_file->file);
555         }
556
557         args->fence_fd = out_fence_fd;
558         args->fence = submit->out_fence->seqno;
559
560 err_submit_objects:
561         etnaviv_submit_put(submit);
562
563 err_submit_ww_acquire:
564         ww_acquire_fini(&ticket);
565
566 err_submit_cmds:
567         if (ret && (out_fence_fd >= 0))
568                 put_unused_fd(out_fence_fd);
569         /* if we still own the cmdbuf */
570         if (cmdbuf)
571                 etnaviv_cmdbuf_free(cmdbuf);
572         if (stream)
573                 kvfree(stream);
574         if (bos)
575                 kvfree(bos);
576         if (relocs)
577                 kvfree(relocs);
578         if (pmrs)
579                 kvfree(pmrs);
580
581         return ret;
582 }