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