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)
38 struct etnaviv_gem_submit *submit;
39 size_t sz = size_vstruct(nr, sizeof(submit->bos[0]), sizeof(*submit));
41 submit = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
46 /* initially, until copy_from_user() and bo lookup succeeds: */
50 ww_acquire_init(&submit->ticket, &reservation_ww_class);
56 static int submit_lookup_objects(struct etnaviv_gem_submit *submit,
57 struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos,
60 struct drm_etnaviv_gem_submit_bo *bo;
64 spin_lock(&file->table_lock);
66 for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) {
67 struct drm_gem_object *obj;
69 if (bo->flags & BO_INVALID_FLAGS) {
70 DRM_ERROR("invalid flags: %x\n", bo->flags);
75 submit->bos[i].flags = bo->flags;
77 /* normally use drm_gem_object_lookup(), but for bulk lookup
78 * all under single table_lock just hit object_idr directly:
80 obj = idr_find(&file->object_idr, bo->handle);
82 DRM_ERROR("invalid handle %u at index %u\n",
89 * Take a refcount on the object. The file table lock
90 * prevents the object_idr's refcount on this being dropped.
92 drm_gem_object_get(obj);
94 submit->bos[i].obj = to_etnaviv_bo(obj);
99 spin_unlock(&file->table_lock);
104 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i)
106 if (submit->bos[i].flags & BO_LOCKED) {
107 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
109 ww_mutex_unlock(&etnaviv_obj->resv->lock);
110 submit->bos[i].flags &= ~BO_LOCKED;
114 static int submit_lock_objects(struct etnaviv_gem_submit *submit)
116 int contended, slow_locked = -1, i, ret = 0;
119 for (i = 0; i < submit->nr_bos; i++) {
120 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
122 if (slow_locked == i)
127 if (!(submit->bos[i].flags & BO_LOCKED)) {
128 ret = ww_mutex_lock_interruptible(&etnaviv_obj->resv->lock,
130 if (ret == -EALREADY)
131 DRM_ERROR("BO at index %u already on submit list\n",
135 submit->bos[i].flags |= BO_LOCKED;
139 ww_acquire_done(&submit->ticket);
145 submit_unlock_object(submit, i);
148 submit_unlock_object(submit, slow_locked);
150 if (ret == -EDEADLK) {
151 struct etnaviv_gem_object *etnaviv_obj;
153 etnaviv_obj = submit->bos[contended].obj;
155 /* we lost out in a seqno race, lock and retry.. */
156 ret = ww_mutex_lock_slow_interruptible(&etnaviv_obj->resv->lock,
159 submit->bos[contended].flags |= BO_LOCKED;
160 slow_locked = contended;
168 static int submit_fence_sync(const struct etnaviv_gem_submit *submit)
170 unsigned int context = submit->gpu->fence_context;
173 for (i = 0; i < submit->nr_bos; i++) {
174 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
175 bool write = submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE;
176 bool explicit = !!(submit->flags & ETNA_SUBMIT_NO_IMPLICIT);
178 ret = etnaviv_gpu_fence_sync_obj(etnaviv_obj, context, write,
187 static void submit_unpin_objects(struct etnaviv_gem_submit *submit)
191 for (i = 0; i < submit->nr_bos; i++) {
192 if (submit->bos[i].flags & BO_PINNED)
193 etnaviv_gem_mapping_unreference(submit->bos[i].mapping);
195 submit->bos[i].mapping = NULL;
196 submit->bos[i].flags &= ~BO_PINNED;
200 static int submit_pin_objects(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;
206 struct etnaviv_vram_mapping *mapping;
208 mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
210 if (IS_ERR(mapping)) {
211 ret = PTR_ERR(mapping);
215 submit->bos[i].flags |= BO_PINNED;
216 submit->bos[i].mapping = mapping;
222 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx,
223 struct etnaviv_gem_submit_bo **bo)
225 if (idx >= submit->nr_bos) {
226 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
227 idx, submit->nr_bos);
231 *bo = &submit->bos[idx];
236 /* process the reloc's and patch up the cmdstream as needed: */
237 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream,
238 u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs,
241 u32 i, last_offset = 0;
245 for (i = 0; i < nr_relocs; i++) {
246 const struct drm_etnaviv_gem_submit_reloc *r = relocs + i;
247 struct etnaviv_gem_submit_bo *bo;
250 if (unlikely(r->flags)) {
251 DRM_ERROR("invalid reloc flags\n");
255 if (r->submit_offset % 4) {
256 DRM_ERROR("non-aligned reloc offset: %u\n",
261 /* offset in dwords: */
262 off = r->submit_offset / 4;
264 if ((off >= size ) ||
265 (off < last_offset)) {
266 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
270 ret = submit_bo(submit, r->reloc_idx, &bo);
274 if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) {
275 DRM_ERROR("relocation %u outside object\n", i);
279 ptr[off] = bo->mapping->iova + r->reloc_offset;
287 static int submit_perfmon_validate(struct etnaviv_gem_submit *submit,
288 struct etnaviv_cmdbuf *cmdbuf,
289 const struct drm_etnaviv_gem_submit_pmr *pmrs,
294 for (i = 0; i < nr_pms; i++) {
295 const struct drm_etnaviv_gem_submit_pmr *r = pmrs + i;
296 struct etnaviv_gem_submit_bo *bo;
299 ret = submit_bo(submit, r->read_idx, &bo);
303 /* at offset 0 a sequence number gets stored used for userspace sync */
304 if (r->read_offset == 0) {
305 DRM_ERROR("perfmon request: offset is 0");
309 if (r->read_offset >= bo->obj->base.size - sizeof(u32)) {
310 DRM_ERROR("perfmon request: offset %u outside object", i);
314 if (r->flags & ~(ETNA_PM_PROCESS_PRE | ETNA_PM_PROCESS_POST)) {
315 DRM_ERROR("perfmon request: flags are not valid");
319 if (etnaviv_pm_req_validate(r, cmdbuf->exec_state)) {
320 DRM_ERROR("perfmon request: domain or signal not valid");
324 cmdbuf->pmrs[i].flags = r->flags;
325 cmdbuf->pmrs[i].domain = r->domain;
326 cmdbuf->pmrs[i].signal = r->signal;
327 cmdbuf->pmrs[i].sequence = r->sequence;
328 cmdbuf->pmrs[i].offset = r->read_offset;
329 cmdbuf->pmrs[i].bo_vma = etnaviv_gem_vmap(&bo->obj->base);
335 static void submit_cleanup(struct etnaviv_gem_submit *submit)
339 for (i = 0; i < submit->nr_bos; i++) {
340 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
342 submit_unlock_object(submit, i);
343 drm_gem_object_put_unlocked(&etnaviv_obj->base);
346 ww_acquire_fini(&submit->ticket);
348 dma_fence_put(submit->fence);
352 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
353 struct drm_file *file)
355 struct etnaviv_drm_private *priv = dev->dev_private;
356 struct drm_etnaviv_gem_submit *args = data;
357 struct drm_etnaviv_gem_submit_reloc *relocs;
358 struct drm_etnaviv_gem_submit_pmr *pmrs;
359 struct drm_etnaviv_gem_submit_bo *bos;
360 struct etnaviv_gem_submit *submit;
361 struct etnaviv_cmdbuf *cmdbuf;
362 struct etnaviv_gpu *gpu;
363 struct dma_fence *in_fence = NULL;
364 struct sync_file *sync_file = NULL;
365 int out_fence_fd = -1;
369 if (args->pipe >= ETNA_MAX_PIPES)
372 gpu = priv->gpu[args->pipe];
376 if (args->stream_size % 4) {
377 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
382 if (args->exec_state != ETNA_PIPE_3D &&
383 args->exec_state != ETNA_PIPE_2D &&
384 args->exec_state != ETNA_PIPE_VG) {
385 DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state);
389 if (args->flags & ~ETNA_SUBMIT_FLAGS) {
390 DRM_ERROR("invalid flags: 0x%x\n", args->flags);
395 * Copy the command submission and bo array to kernel space in
396 * one go, and do this outside of any locks.
398 bos = kvmalloc_array(args->nr_bos, sizeof(*bos), GFP_KERNEL);
399 relocs = kvmalloc_array(args->nr_relocs, sizeof(*relocs), GFP_KERNEL);
400 pmrs = kvmalloc_array(args->nr_pmrs, sizeof(*pmrs), GFP_KERNEL);
401 stream = kvmalloc_array(1, args->stream_size, GFP_KERNEL);
402 cmdbuf = etnaviv_cmdbuf_new(gpu->cmdbuf_suballoc,
403 ALIGN(args->stream_size, 8) + 8,
404 args->nr_bos, args->nr_pmrs);
405 if (!bos || !relocs || !pmrs || !stream || !cmdbuf) {
407 goto err_submit_cmds;
410 cmdbuf->exec_state = args->exec_state;
411 cmdbuf->ctx = file->driver_priv;
413 ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
414 args->nr_bos * sizeof(*bos));
417 goto err_submit_cmds;
420 ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
421 args->nr_relocs * sizeof(*relocs));
424 goto err_submit_cmds;
427 ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs),
428 args->nr_pmrs * sizeof(*pmrs));
431 goto err_submit_cmds;
433 cmdbuf->nr_pmrs = args->nr_pmrs;
435 ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
439 goto err_submit_cmds;
442 if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
443 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
444 if (out_fence_fd < 0) {
446 goto err_submit_cmds;
450 submit = submit_create(dev, gpu, args->nr_bos);
453 goto err_submit_cmds;
456 submit->flags = args->flags;
458 ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
460 goto err_submit_objects;
462 ret = submit_lock_objects(submit);
464 goto err_submit_objects;
466 if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
467 relocs, args->nr_relocs)) {
469 goto err_submit_objects;
472 if (args->flags & ETNA_SUBMIT_FENCE_FD_IN) {
473 in_fence = sync_file_get_fence(args->fence_fd);
476 goto err_submit_objects;
480 * Wait if the fence is from a foreign context, or if the fence
481 * array contains any fence from a foreign context.
483 if (!dma_fence_match_context(in_fence, gpu->fence_context)) {
484 ret = dma_fence_wait(in_fence, true);
486 goto err_submit_objects;
490 ret = submit_fence_sync(submit);
492 goto err_submit_objects;
494 ret = submit_pin_objects(submit);
498 ret = submit_reloc(submit, stream, args->stream_size / 4,
499 relocs, args->nr_relocs);
503 ret = submit_perfmon_validate(submit, cmdbuf, pmrs, args->nr_pmrs);
507 memcpy(cmdbuf->vaddr, stream, args->stream_size);
508 cmdbuf->user_size = ALIGN(args->stream_size, 8);
510 ret = etnaviv_gpu_submit(gpu, submit, cmdbuf);
516 if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
518 * This can be improved: ideally we want to allocate the sync
519 * file before kicking off the GPU job and just attach the
520 * fence to the sync file here, eliminating the ENOMEM
521 * possibility at this stage.
523 sync_file = sync_file_create(submit->fence);
528 fd_install(out_fence_fd, sync_file->file);
531 args->fence_fd = out_fence_fd;
532 args->fence = submit->fence->seqno;
535 submit_unpin_objects(submit);
539 dma_fence_put(in_fence);
540 submit_cleanup(submit);
543 if (ret && (out_fence_fd >= 0))
544 put_unused_fd(out_fence_fd);
545 /* if we still own the cmdbuf */
547 etnaviv_cmdbuf_free(cmdbuf);