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"
27 * Cmdstream submission:
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
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)
38 struct etnaviv_gem_submit *submit;
39 size_t sz = size_vstruct(nr_bos, sizeof(submit->bos[0]), sizeof(*submit));
41 submit = kzalloc(sz, GFP_KERNEL);
45 submit->pmrs = kcalloc(nr_pmrs, sizeof(struct etnaviv_perfmon_request),
51 submit->nr_pmrs = nr_pmrs;
54 kref_init(&submit->refcount);
59 static int submit_lookup_objects(struct etnaviv_gem_submit *submit,
60 struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos,
63 struct drm_etnaviv_gem_submit_bo *bo;
67 spin_lock(&file->table_lock);
69 for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) {
70 struct drm_gem_object *obj;
72 if (bo->flags & BO_INVALID_FLAGS) {
73 DRM_ERROR("invalid flags: %x\n", bo->flags);
78 submit->bos[i].flags = bo->flags;
80 /* normally use drm_gem_object_lookup(), but for bulk lookup
81 * all under single table_lock just hit object_idr directly:
83 obj = idr_find(&file->object_idr, bo->handle);
85 DRM_ERROR("invalid handle %u at index %u\n",
92 * Take a refcount on the object. The file table lock
93 * prevents the object_idr's refcount on this being dropped.
95 drm_gem_object_get(obj);
97 submit->bos[i].obj = to_etnaviv_bo(obj);
102 spin_unlock(&file->table_lock);
107 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i)
109 if (submit->bos[i].flags & BO_LOCKED) {
110 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
112 ww_mutex_unlock(&etnaviv_obj->resv->lock);
113 submit->bos[i].flags &= ~BO_LOCKED;
117 static int submit_lock_objects(struct etnaviv_gem_submit *submit,
118 struct ww_acquire_ctx *ticket)
120 int contended, slow_locked = -1, i, ret = 0;
123 for (i = 0; i < submit->nr_bos; i++) {
124 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
126 if (slow_locked == i)
131 if (!(submit->bos[i].flags & BO_LOCKED)) {
132 ret = ww_mutex_lock_interruptible(&etnaviv_obj->resv->lock,
134 if (ret == -EALREADY)
135 DRM_ERROR("BO at index %u already on submit list\n",
139 submit->bos[i].flags |= BO_LOCKED;
143 ww_acquire_done(ticket);
149 submit_unlock_object(submit, i);
152 submit_unlock_object(submit, slow_locked);
154 if (ret == -EDEADLK) {
155 struct etnaviv_gem_object *etnaviv_obj;
157 etnaviv_obj = submit->bos[contended].obj;
159 /* we lost out in a seqno race, lock and retry.. */
160 ret = ww_mutex_lock_slow_interruptible(&etnaviv_obj->resv->lock,
163 submit->bos[contended].flags |= BO_LOCKED;
164 slow_locked = contended;
172 static int submit_fence_sync(const struct etnaviv_gem_submit *submit)
174 unsigned int context = submit->gpu->fence_context;
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);
182 ret = etnaviv_gpu_fence_sync_obj(etnaviv_obj, context, write,
188 if (submit->flags & ETNA_SUBMIT_FENCE_FD_IN) {
190 * Wait if the fence is from a foreign context, or if the fence
191 * array contains any fence from a foreign context.
193 if (!dma_fence_match_context(submit->in_fence, context))
194 ret = dma_fence_wait(submit->in_fence, true);
200 static void submit_attach_object_fences(struct etnaviv_gem_submit *submit)
204 for (i = 0; i < submit->nr_bos; i++) {
205 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
207 if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE)
208 reservation_object_add_excl_fence(etnaviv_obj->resv,
211 reservation_object_add_shared_fence(etnaviv_obj->resv,
214 submit_unlock_object(submit, i);
218 static int submit_pin_objects(struct etnaviv_gem_submit *submit)
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;
226 mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
228 if (IS_ERR(mapping)) {
229 ret = PTR_ERR(mapping);
233 submit->bos[i].flags |= BO_PINNED;
234 submit->bos[i].mapping = mapping;
240 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx,
241 struct etnaviv_gem_submit_bo **bo)
243 if (idx >= submit->nr_bos) {
244 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
245 idx, submit->nr_bos);
249 *bo = &submit->bos[idx];
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,
259 u32 i, last_offset = 0;
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;
268 if (unlikely(r->flags)) {
269 DRM_ERROR("invalid reloc flags\n");
273 if (r->submit_offset % 4) {
274 DRM_ERROR("non-aligned reloc offset: %u\n",
279 /* offset in dwords: */
280 off = r->submit_offset / 4;
282 if ((off >= size ) ||
283 (off < last_offset)) {
284 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
288 ret = submit_bo(submit, r->reloc_idx, &bo);
292 if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) {
293 DRM_ERROR("relocation %u outside object\n", i);
297 ptr[off] = bo->mapping->iova + r->reloc_offset;
305 static int submit_perfmon_validate(struct etnaviv_gem_submit *submit,
306 u32 exec_state, const struct drm_etnaviv_gem_submit_pmr *pmrs)
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;
315 ret = submit_bo(submit, r->read_idx, &bo);
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");
325 if (r->read_offset >= bo->obj->base.size - sizeof(u32)) {
326 DRM_ERROR("perfmon request: offset %u outside object", i);
330 if (r->flags & ~(ETNA_PM_PROCESS_PRE | ETNA_PM_PROCESS_POST)) {
331 DRM_ERROR("perfmon request: flags are not valid");
335 if (etnaviv_pm_req_validate(r, exec_state)) {
336 DRM_ERROR("perfmon request: domain or signal not valid");
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);
351 static void submit_cleanup(struct kref *kref)
353 struct etnaviv_gem_submit *submit =
354 container_of(kref, struct etnaviv_gem_submit, refcount);
357 for (i = 0; i < submit->nr_bos; i++) {
358 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
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;
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);
372 if (submit->in_fence)
373 dma_fence_put(submit->in_fence);
374 if (submit->out_fence)
375 dma_fence_put(submit->out_fence);
380 void etnaviv_submit_put(struct etnaviv_gem_submit *submit)
382 kref_put(&submit->refcount, submit_cleanup);
385 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
386 struct drm_file *file)
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;
402 if (args->pipe >= ETNA_MAX_PIPES)
405 gpu = priv->gpu[args->pipe];
409 if (args->stream_size % 4) {
410 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
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);
422 if (args->flags & ~ETNA_SUBMIT_FLAGS) {
423 DRM_ERROR("invalid flags: 0x%x\n", args->flags);
428 * Copy the command submission and bo array to kernel space in
429 * one go, and do this outside of any locks.
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,
438 if (!bos || !relocs || !pmrs || !stream || !cmdbuf) {
440 goto err_submit_cmds;
443 cmdbuf->ctx = file->driver_priv;
445 ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
446 args->nr_bos * sizeof(*bos));
449 goto err_submit_cmds;
452 ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
453 args->nr_relocs * sizeof(*relocs));
456 goto err_submit_cmds;
459 ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs),
460 args->nr_pmrs * sizeof(*pmrs));
463 goto err_submit_cmds;
466 ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
470 goto err_submit_cmds;
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) {
477 goto err_submit_cmds;
481 ww_acquire_init(&ticket, &reservation_ww_class);
483 submit = submit_create(dev, gpu, args->nr_bos, args->nr_pmrs);
486 goto err_submit_ww_acquire;
489 submit->exec_state = args->exec_state;
490 submit->flags = args->flags;
492 ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
494 goto err_submit_objects;
496 ret = submit_lock_objects(submit, &ticket);
498 goto err_submit_objects;
500 if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
501 relocs, args->nr_relocs)) {
503 goto err_submit_objects;
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) {
510 goto err_submit_objects;
514 ret = submit_fence_sync(submit);
516 goto err_submit_objects;
518 ret = submit_pin_objects(submit);
520 goto err_submit_objects;
522 ret = submit_reloc(submit, stream, args->stream_size / 4,
523 relocs, args->nr_relocs);
525 goto err_submit_objects;
527 ret = submit_perfmon_validate(submit, args->exec_state, pmrs);
529 goto err_submit_objects;
531 memcpy(cmdbuf->vaddr, stream, args->stream_size);
532 cmdbuf->user_size = ALIGN(args->stream_size, 8);
534 ret = etnaviv_gpu_submit(gpu, submit, cmdbuf);
536 goto err_submit_objects;
538 submit_attach_object_fences(submit);
542 if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
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.
549 sync_file = sync_file_create(submit->out_fence);
552 goto err_submit_objects;
554 fd_install(out_fence_fd, sync_file->file);
557 args->fence_fd = out_fence_fd;
558 args->fence = submit->out_fence->seqno;
561 etnaviv_submit_put(submit);
563 err_submit_ww_acquire:
564 ww_acquire_fini(&ticket);
567 if (ret && (out_fence_fd >= 0))
568 put_unused_fd(out_fence_fd);
569 /* if we still own the cmdbuf */
571 etnaviv_cmdbuf_free(cmdbuf);