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