2 * Copyright (C) 2015 Etnaviv Project
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.
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
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/>.
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 #include "etnaviv_sched.h"
28 * Cmdstream submission:
31 #define BO_INVALID_FLAGS ~(ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE)
32 /* make sure these don't conflict w/ ETNAVIV_SUBMIT_BO_x */
33 #define BO_LOCKED 0x4000
34 #define BO_PINNED 0x2000
36 static struct etnaviv_gem_submit *submit_create(struct drm_device *dev,
37 struct etnaviv_gpu *gpu, size_t nr_bos, size_t nr_pmrs)
39 struct etnaviv_gem_submit *submit;
40 size_t sz = size_vstruct(nr_bos, sizeof(submit->bos[0]), sizeof(*submit));
42 submit = kzalloc(sz, GFP_KERNEL);
46 submit->pmrs = kcalloc(nr_pmrs, sizeof(struct etnaviv_perfmon_request),
52 submit->nr_pmrs = nr_pmrs;
55 kref_init(&submit->refcount);
60 static int submit_lookup_objects(struct etnaviv_gem_submit *submit,
61 struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos,
64 struct drm_etnaviv_gem_submit_bo *bo;
68 spin_lock(&file->table_lock);
70 for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) {
71 struct drm_gem_object *obj;
73 if (bo->flags & BO_INVALID_FLAGS) {
74 DRM_ERROR("invalid flags: %x\n", bo->flags);
79 submit->bos[i].flags = bo->flags;
81 /* normally use drm_gem_object_lookup(), but for bulk lookup
82 * all under single table_lock just hit object_idr directly:
84 obj = idr_find(&file->object_idr, bo->handle);
86 DRM_ERROR("invalid handle %u at index %u\n",
93 * Take a refcount on the object. The file table lock
94 * prevents the object_idr's refcount on this being dropped.
96 drm_gem_object_get(obj);
98 submit->bos[i].obj = to_etnaviv_bo(obj);
103 spin_unlock(&file->table_lock);
108 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i)
110 if (submit->bos[i].flags & BO_LOCKED) {
111 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
113 ww_mutex_unlock(&etnaviv_obj->resv->lock);
114 submit->bos[i].flags &= ~BO_LOCKED;
118 static int submit_lock_objects(struct etnaviv_gem_submit *submit,
119 struct ww_acquire_ctx *ticket)
121 int contended, slow_locked = -1, i, ret = 0;
124 for (i = 0; i < submit->nr_bos; i++) {
125 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
127 if (slow_locked == i)
132 if (!(submit->bos[i].flags & BO_LOCKED)) {
133 ret = ww_mutex_lock_interruptible(&etnaviv_obj->resv->lock,
135 if (ret == -EALREADY)
136 DRM_ERROR("BO at index %u already on submit list\n",
140 submit->bos[i].flags |= BO_LOCKED;
144 ww_acquire_done(ticket);
150 submit_unlock_object(submit, i);
153 submit_unlock_object(submit, slow_locked);
155 if (ret == -EDEADLK) {
156 struct etnaviv_gem_object *etnaviv_obj;
158 etnaviv_obj = submit->bos[contended].obj;
160 /* we lost out in a seqno race, lock and retry.. */
161 ret = ww_mutex_lock_slow_interruptible(&etnaviv_obj->resv->lock,
164 submit->bos[contended].flags |= BO_LOCKED;
165 slow_locked = contended;
173 static int submit_fence_sync(struct etnaviv_gem_submit *submit)
177 for (i = 0; i < submit->nr_bos; i++) {
178 struct etnaviv_gem_submit_bo *bo = &submit->bos[i];
179 struct reservation_object *robj = bo->obj->resv;
181 if (!(bo->flags & ETNA_SUBMIT_BO_WRITE)) {
182 ret = reservation_object_reserve_shared(robj);
187 if (submit->flags & ETNA_SUBMIT_NO_IMPLICIT)
190 if (bo->flags & ETNA_SUBMIT_BO_WRITE) {
191 ret = reservation_object_get_fences_rcu(robj, &bo->excl,
197 bo->excl = reservation_object_get_excl_rcu(robj);
205 static void submit_attach_object_fences(struct etnaviv_gem_submit *submit)
209 for (i = 0; i < submit->nr_bos; i++) {
210 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
212 if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE)
213 reservation_object_add_excl_fence(etnaviv_obj->resv,
216 reservation_object_add_shared_fence(etnaviv_obj->resv,
219 submit_unlock_object(submit, i);
223 static int submit_pin_objects(struct etnaviv_gem_submit *submit)
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;
231 mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
233 if (IS_ERR(mapping)) {
234 ret = PTR_ERR(mapping);
237 atomic_inc(&etnaviv_obj->gpu_active);
239 submit->bos[i].flags |= BO_PINNED;
240 submit->bos[i].mapping = mapping;
246 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx,
247 struct etnaviv_gem_submit_bo **bo)
249 if (idx >= submit->nr_bos) {
250 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
251 idx, submit->nr_bos);
255 *bo = &submit->bos[idx];
260 /* process the reloc's and patch up the cmdstream as needed: */
261 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream,
262 u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs,
265 u32 i, last_offset = 0;
269 for (i = 0; i < nr_relocs; i++) {
270 const struct drm_etnaviv_gem_submit_reloc *r = relocs + i;
271 struct etnaviv_gem_submit_bo *bo;
274 if (unlikely(r->flags)) {
275 DRM_ERROR("invalid reloc flags\n");
279 if (r->submit_offset % 4) {
280 DRM_ERROR("non-aligned reloc offset: %u\n",
285 /* offset in dwords: */
286 off = r->submit_offset / 4;
288 if ((off >= size ) ||
289 (off < last_offset)) {
290 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
294 ret = submit_bo(submit, r->reloc_idx, &bo);
298 if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) {
299 DRM_ERROR("relocation %u outside object\n", i);
303 ptr[off] = bo->mapping->iova + r->reloc_offset;
311 static int submit_perfmon_validate(struct etnaviv_gem_submit *submit,
312 u32 exec_state, const struct drm_etnaviv_gem_submit_pmr *pmrs)
316 for (i = 0; i < submit->nr_pmrs; i++) {
317 const struct drm_etnaviv_gem_submit_pmr *r = pmrs + i;
318 struct etnaviv_gem_submit_bo *bo;
321 ret = submit_bo(submit, r->read_idx, &bo);
325 /* at offset 0 a sequence number gets stored used for userspace sync */
326 if (r->read_offset == 0) {
327 DRM_ERROR("perfmon request: offset is 0");
331 if (r->read_offset >= bo->obj->base.size - sizeof(u32)) {
332 DRM_ERROR("perfmon request: offset %u outside object", i);
336 if (r->flags & ~(ETNA_PM_PROCESS_PRE | ETNA_PM_PROCESS_POST)) {
337 DRM_ERROR("perfmon request: flags are not valid");
341 if (etnaviv_pm_req_validate(r, exec_state)) {
342 DRM_ERROR("perfmon request: domain or signal not valid");
346 submit->pmrs[i].flags = r->flags;
347 submit->pmrs[i].domain = r->domain;
348 submit->pmrs[i].signal = r->signal;
349 submit->pmrs[i].sequence = r->sequence;
350 submit->pmrs[i].offset = r->read_offset;
351 submit->pmrs[i].bo_vma = etnaviv_gem_vmap(&bo->obj->base);
357 static void submit_cleanup(struct kref *kref)
359 struct etnaviv_gem_submit *submit =
360 container_of(kref, struct etnaviv_gem_submit, refcount);
363 if (submit->runtime_resumed)
364 pm_runtime_put_autosuspend(submit->gpu->dev);
366 if (submit->cmdbuf.suballoc)
367 etnaviv_cmdbuf_free(&submit->cmdbuf);
369 for (i = 0; i < submit->nr_bos; i++) {
370 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
372 /* unpin all objects */
373 if (submit->bos[i].flags & BO_PINNED) {
374 etnaviv_gem_mapping_unreference(submit->bos[i].mapping);
375 atomic_dec(&etnaviv_obj->gpu_active);
376 submit->bos[i].mapping = NULL;
377 submit->bos[i].flags &= ~BO_PINNED;
380 /* if the GPU submit failed, objects might still be locked */
381 submit_unlock_object(submit, i);
382 drm_gem_object_put_unlocked(&etnaviv_obj->base);
385 wake_up_all(&submit->gpu->fence_event);
387 if (submit->in_fence)
388 dma_fence_put(submit->in_fence);
389 if (submit->out_fence) {
390 /* first remove from IDR, so fence can not be found anymore */
391 mutex_lock(&submit->gpu->fence_lock);
392 idr_remove(&submit->gpu->fence_idr, submit->out_fence_id);
393 mutex_unlock(&submit->gpu->fence_lock);
394 dma_fence_put(submit->out_fence);
400 void etnaviv_submit_put(struct etnaviv_gem_submit *submit)
402 kref_put(&submit->refcount, submit_cleanup);
405 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
406 struct drm_file *file)
408 struct etnaviv_file_private *ctx = file->driver_priv;
409 struct etnaviv_drm_private *priv = dev->dev_private;
410 struct drm_etnaviv_gem_submit *args = data;
411 struct drm_etnaviv_gem_submit_reloc *relocs;
412 struct drm_etnaviv_gem_submit_pmr *pmrs;
413 struct drm_etnaviv_gem_submit_bo *bos;
414 struct etnaviv_gem_submit *submit;
415 struct etnaviv_gpu *gpu;
416 struct sync_file *sync_file = NULL;
417 struct ww_acquire_ctx ticket;
418 int out_fence_fd = -1;
422 if (args->pipe >= ETNA_MAX_PIPES)
425 gpu = priv->gpu[args->pipe];
429 if (args->stream_size % 4) {
430 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
435 if (args->exec_state != ETNA_PIPE_3D &&
436 args->exec_state != ETNA_PIPE_2D &&
437 args->exec_state != ETNA_PIPE_VG) {
438 DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state);
442 if (args->flags & ~ETNA_SUBMIT_FLAGS) {
443 DRM_ERROR("invalid flags: 0x%x\n", args->flags);
447 if (args->stream_size > SZ_128K || args->nr_relocs > SZ_128K ||
448 args->nr_bos > SZ_128K || args->nr_pmrs > 128) {
449 DRM_ERROR("submit arguments out of size limits\n");
454 * Copy the command submission and bo array to kernel space in
455 * one go, and do this outside of any locks.
457 bos = kvmalloc_array(args->nr_bos, sizeof(*bos), GFP_KERNEL);
458 relocs = kvmalloc_array(args->nr_relocs, sizeof(*relocs), GFP_KERNEL);
459 pmrs = kvmalloc_array(args->nr_pmrs, sizeof(*pmrs), GFP_KERNEL);
460 stream = kvmalloc_array(1, args->stream_size, GFP_KERNEL);
461 if (!bos || !relocs || !pmrs || !stream) {
463 goto err_submit_cmds;
466 ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
467 args->nr_bos * sizeof(*bos));
470 goto err_submit_cmds;
473 ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
474 args->nr_relocs * sizeof(*relocs));
477 goto err_submit_cmds;
480 ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs),
481 args->nr_pmrs * sizeof(*pmrs));
484 goto err_submit_cmds;
487 ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
491 goto err_submit_cmds;
494 if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
495 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
496 if (out_fence_fd < 0) {
498 goto err_submit_cmds;
502 ww_acquire_init(&ticket, &reservation_ww_class);
504 submit = submit_create(dev, gpu, args->nr_bos, args->nr_pmrs);
507 goto err_submit_ww_acquire;
510 ret = etnaviv_cmdbuf_init(gpu->cmdbuf_suballoc, &submit->cmdbuf,
511 ALIGN(args->stream_size, 8) + 8);
513 goto err_submit_objects;
515 submit->cmdbuf.ctx = file->driver_priv;
516 submit->exec_state = args->exec_state;
517 submit->flags = args->flags;
519 ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
521 goto err_submit_objects;
523 if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
524 relocs, args->nr_relocs)) {
526 goto err_submit_objects;
529 if (args->flags & ETNA_SUBMIT_FENCE_FD_IN) {
530 submit->in_fence = sync_file_get_fence(args->fence_fd);
531 if (!submit->in_fence) {
533 goto err_submit_objects;
537 ret = submit_pin_objects(submit);
539 goto err_submit_objects;
541 ret = submit_reloc(submit, stream, args->stream_size / 4,
542 relocs, args->nr_relocs);
544 goto err_submit_objects;
546 ret = submit_perfmon_validate(submit, args->exec_state, pmrs);
548 goto err_submit_objects;
550 memcpy(submit->cmdbuf.vaddr, stream, args->stream_size);
552 ret = submit_lock_objects(submit, &ticket);
554 goto err_submit_objects;
556 ret = submit_fence_sync(submit);
558 goto err_submit_objects;
560 ret = etnaviv_sched_push_job(&ctx->sched_entity[args->pipe], submit);
562 goto err_submit_objects;
564 submit_attach_object_fences(submit);
566 if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
568 * This can be improved: ideally we want to allocate the sync
569 * file before kicking off the GPU job and just attach the
570 * fence to the sync file here, eliminating the ENOMEM
571 * possibility at this stage.
573 sync_file = sync_file_create(submit->out_fence);
576 goto err_submit_objects;
578 fd_install(out_fence_fd, sync_file->file);
581 args->fence_fd = out_fence_fd;
582 args->fence = submit->out_fence_id;
585 etnaviv_submit_put(submit);
587 err_submit_ww_acquire:
588 ww_acquire_fini(&ticket);
591 if (ret && (out_fence_fd >= 0))
592 put_unused_fd(out_fence_fd);