20906c22998c76d55456a205cf4d98adb498192a
[sfrench/cifs-2.6.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         if (submit->flags & ETNA_SUBMIT_FENCE_FD_IN) {
181                 /*
182                  * Wait if the fence is from a foreign context, or if the fence
183                  * array contains any fence from a foreign context.
184                  */
185                 if (!dma_fence_match_context(submit->in_fence, context))
186                         ret = dma_fence_wait(submit->in_fence, true);
187         }
188
189         return ret;
190 }
191
192 static void submit_attach_object_fences(struct etnaviv_gem_submit *submit)
193 {
194         int i;
195
196         for (i = 0; i < submit->nr_bos; i++) {
197                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
198
199                 if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE)
200                         reservation_object_add_excl_fence(etnaviv_obj->resv,
201                                                           submit->out_fence);
202                 else
203                         reservation_object_add_shared_fence(etnaviv_obj->resv,
204                                                             submit->out_fence);
205
206                 submit_unlock_object(submit, i);
207         }
208 }
209
210 static void submit_unpin_objects(struct etnaviv_gem_submit *submit)
211 {
212         int i;
213
214         for (i = 0; i < submit->nr_bos; i++) {
215                 if (submit->bos[i].flags & BO_PINNED)
216                         etnaviv_gem_mapping_unreference(submit->bos[i].mapping);
217
218                 submit->bos[i].mapping = NULL;
219                 submit->bos[i].flags &= ~BO_PINNED;
220         }
221 }
222
223 static int submit_pin_objects(struct etnaviv_gem_submit *submit)
224 {
225         int i, ret = 0;
226
227         for (i = 0; i < submit->nr_bos; i++) {
228                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
229                 struct etnaviv_vram_mapping *mapping;
230
231                 mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
232                                                   submit->gpu);
233                 if (IS_ERR(mapping)) {
234                         ret = PTR_ERR(mapping);
235                         break;
236                 }
237
238                 submit->bos[i].flags |= BO_PINNED;
239                 submit->bos[i].mapping = mapping;
240         }
241
242         return ret;
243 }
244
245 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx,
246         struct etnaviv_gem_submit_bo **bo)
247 {
248         if (idx >= submit->nr_bos) {
249                 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
250                                 idx, submit->nr_bos);
251                 return -EINVAL;
252         }
253
254         *bo = &submit->bos[idx];
255
256         return 0;
257 }
258
259 /* process the reloc's and patch up the cmdstream as needed: */
260 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream,
261                 u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs,
262                 u32 nr_relocs)
263 {
264         u32 i, last_offset = 0;
265         u32 *ptr = stream;
266         int ret;
267
268         for (i = 0; i < nr_relocs; i++) {
269                 const struct drm_etnaviv_gem_submit_reloc *r = relocs + i;
270                 struct etnaviv_gem_submit_bo *bo;
271                 u32 off;
272
273                 if (unlikely(r->flags)) {
274                         DRM_ERROR("invalid reloc flags\n");
275                         return -EINVAL;
276                 }
277
278                 if (r->submit_offset % 4) {
279                         DRM_ERROR("non-aligned reloc offset: %u\n",
280                                   r->submit_offset);
281                         return -EINVAL;
282                 }
283
284                 /* offset in dwords: */
285                 off = r->submit_offset / 4;
286
287                 if ((off >= size ) ||
288                                 (off < last_offset)) {
289                         DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
290                         return -EINVAL;
291                 }
292
293                 ret = submit_bo(submit, r->reloc_idx, &bo);
294                 if (ret)
295                         return ret;
296
297                 if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) {
298                         DRM_ERROR("relocation %u outside object\n", i);
299                         return -EINVAL;
300                 }
301
302                 ptr[off] = bo->mapping->iova + r->reloc_offset;
303
304                 last_offset = off;
305         }
306
307         return 0;
308 }
309
310 static int submit_perfmon_validate(struct etnaviv_gem_submit *submit,
311                 struct etnaviv_cmdbuf *cmdbuf,
312                 const struct drm_etnaviv_gem_submit_pmr *pmrs,
313                 u32 nr_pms)
314 {
315         u32 i;
316
317         for (i = 0; i < nr_pms; i++) {
318                 const struct drm_etnaviv_gem_submit_pmr *r = pmrs + i;
319                 struct etnaviv_gem_submit_bo *bo;
320                 int ret;
321
322                 ret = submit_bo(submit, r->read_idx, &bo);
323                 if (ret)
324                         return ret;
325
326                 /* at offset 0 a sequence number gets stored used for userspace sync */
327                 if (r->read_offset == 0) {
328                         DRM_ERROR("perfmon request: offset is 0");
329                         return -EINVAL;
330                 }
331
332                 if (r->read_offset >= bo->obj->base.size - sizeof(u32)) {
333                         DRM_ERROR("perfmon request: offset %u outside object", i);
334                         return -EINVAL;
335                 }
336
337                 if (r->flags & ~(ETNA_PM_PROCESS_PRE | ETNA_PM_PROCESS_POST)) {
338                         DRM_ERROR("perfmon request: flags are not valid");
339                         return -EINVAL;
340                 }
341
342                 if (etnaviv_pm_req_validate(r, cmdbuf->exec_state)) {
343                         DRM_ERROR("perfmon request: domain or signal not valid");
344                         return -EINVAL;
345                 }
346
347                 cmdbuf->pmrs[i].flags = r->flags;
348                 cmdbuf->pmrs[i].domain = r->domain;
349                 cmdbuf->pmrs[i].signal = r->signal;
350                 cmdbuf->pmrs[i].sequence = r->sequence;
351                 cmdbuf->pmrs[i].offset = r->read_offset;
352                 cmdbuf->pmrs[i].bo_vma = etnaviv_gem_vmap(&bo->obj->base);
353         }
354
355         return 0;
356 }
357
358 static void submit_cleanup(struct etnaviv_gem_submit *submit)
359 {
360         unsigned i;
361
362         for (i = 0; i < submit->nr_bos; i++) {
363                 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
364
365                 /* if the GPU submit failed, objects might still be locked */
366                 submit_unlock_object(submit, i);
367                 drm_gem_object_put_unlocked(&etnaviv_obj->base);
368         }
369
370         ww_acquire_fini(&submit->ticket);
371         if (submit->in_fence)
372                 dma_fence_put(submit->in_fence);
373         if (submit->out_fence)
374                 dma_fence_put(submit->out_fence);
375         kfree(submit);
376 }
377
378 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
379                 struct drm_file *file)
380 {
381         struct etnaviv_drm_private *priv = dev->dev_private;
382         struct drm_etnaviv_gem_submit *args = data;
383         struct drm_etnaviv_gem_submit_reloc *relocs;
384         struct drm_etnaviv_gem_submit_pmr *pmrs;
385         struct drm_etnaviv_gem_submit_bo *bos;
386         struct etnaviv_gem_submit *submit;
387         struct etnaviv_cmdbuf *cmdbuf;
388         struct etnaviv_gpu *gpu;
389         struct sync_file *sync_file = NULL;
390         int out_fence_fd = -1;
391         void *stream;
392         int ret;
393
394         if (args->pipe >= ETNA_MAX_PIPES)
395                 return -EINVAL;
396
397         gpu = priv->gpu[args->pipe];
398         if (!gpu)
399                 return -ENXIO;
400
401         if (args->stream_size % 4) {
402                 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
403                           args->stream_size);
404                 return -EINVAL;
405         }
406
407         if (args->exec_state != ETNA_PIPE_3D &&
408             args->exec_state != ETNA_PIPE_2D &&
409             args->exec_state != ETNA_PIPE_VG) {
410                 DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state);
411                 return -EINVAL;
412         }
413
414         if (args->flags & ~ETNA_SUBMIT_FLAGS) {
415                 DRM_ERROR("invalid flags: 0x%x\n", args->flags);
416                 return -EINVAL;
417         }
418
419         /*
420          * Copy the command submission and bo array to kernel space in
421          * one go, and do this outside of any locks.
422          */
423         bos = kvmalloc_array(args->nr_bos, sizeof(*bos), GFP_KERNEL);
424         relocs = kvmalloc_array(args->nr_relocs, sizeof(*relocs), GFP_KERNEL);
425         pmrs = kvmalloc_array(args->nr_pmrs, sizeof(*pmrs), GFP_KERNEL);
426         stream = kvmalloc_array(1, args->stream_size, GFP_KERNEL);
427         cmdbuf = etnaviv_cmdbuf_new(gpu->cmdbuf_suballoc,
428                                     ALIGN(args->stream_size, 8) + 8,
429                                     args->nr_bos, args->nr_pmrs);
430         if (!bos || !relocs || !pmrs || !stream || !cmdbuf) {
431                 ret = -ENOMEM;
432                 goto err_submit_cmds;
433         }
434
435         cmdbuf->exec_state = args->exec_state;
436         cmdbuf->ctx = file->driver_priv;
437
438         ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
439                              args->nr_bos * sizeof(*bos));
440         if (ret) {
441                 ret = -EFAULT;
442                 goto err_submit_cmds;
443         }
444
445         ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
446                              args->nr_relocs * sizeof(*relocs));
447         if (ret) {
448                 ret = -EFAULT;
449                 goto err_submit_cmds;
450         }
451
452         ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs),
453                              args->nr_pmrs * sizeof(*pmrs));
454         if (ret) {
455                 ret = -EFAULT;
456                 goto err_submit_cmds;
457         }
458         cmdbuf->nr_pmrs = args->nr_pmrs;
459
460         ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
461                              args->stream_size);
462         if (ret) {
463                 ret = -EFAULT;
464                 goto err_submit_cmds;
465         }
466
467         if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
468                 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
469                 if (out_fence_fd < 0) {
470                         ret = out_fence_fd;
471                         goto err_submit_cmds;
472                 }
473         }
474
475         submit = submit_create(dev, gpu, args->nr_bos);
476         if (!submit) {
477                 ret = -ENOMEM;
478                 goto err_submit_cmds;
479         }
480
481         submit->flags = args->flags;
482
483         ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
484         if (ret)
485                 goto err_submit_objects;
486
487         ret = submit_lock_objects(submit);
488         if (ret)
489                 goto err_submit_objects;
490
491         if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
492                                       relocs, args->nr_relocs)) {
493                 ret = -EINVAL;
494                 goto err_submit_objects;
495         }
496
497         if (args->flags & ETNA_SUBMIT_FENCE_FD_IN) {
498                 submit->in_fence = sync_file_get_fence(args->fence_fd);
499                 if (!submit->in_fence) {
500                         ret = -EINVAL;
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         submit_cleanup(submit);
556
557 err_submit_cmds:
558         if (ret && (out_fence_fd >= 0))
559                 put_unused_fd(out_fence_fd);
560         /* if we still own the cmdbuf */
561         if (cmdbuf)
562                 etnaviv_cmdbuf_free(cmdbuf);
563         if (stream)
564                 kvfree(stream);
565         if (bos)
566                 kvfree(bos);
567         if (relocs)
568                 kvfree(relocs);
569         if (pmrs)
570                 kvfree(pmrs);
571
572         return ret;
573 }