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);
232 atomic_inc(&etnaviv_obj->gpu_active);
234 submit->bos[i].flags |= BO_PINNED;
235 submit->bos[i].mapping = mapping;
241 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx,
242 struct etnaviv_gem_submit_bo **bo)
244 if (idx >= submit->nr_bos) {
245 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
246 idx, submit->nr_bos);
250 *bo = &submit->bos[idx];
255 /* process the reloc's and patch up the cmdstream as needed: */
256 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream,
257 u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs,
260 u32 i, last_offset = 0;
264 for (i = 0; i < nr_relocs; i++) {
265 const struct drm_etnaviv_gem_submit_reloc *r = relocs + i;
266 struct etnaviv_gem_submit_bo *bo;
269 if (unlikely(r->flags)) {
270 DRM_ERROR("invalid reloc flags\n");
274 if (r->submit_offset % 4) {
275 DRM_ERROR("non-aligned reloc offset: %u\n",
280 /* offset in dwords: */
281 off = r->submit_offset / 4;
283 if ((off >= size ) ||
284 (off < last_offset)) {
285 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
289 ret = submit_bo(submit, r->reloc_idx, &bo);
293 if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) {
294 DRM_ERROR("relocation %u outside object\n", i);
298 ptr[off] = bo->mapping->iova + r->reloc_offset;
306 static int submit_perfmon_validate(struct etnaviv_gem_submit *submit,
307 u32 exec_state, const struct drm_etnaviv_gem_submit_pmr *pmrs)
311 for (i = 0; i < submit->nr_pmrs; i++) {
312 const struct drm_etnaviv_gem_submit_pmr *r = pmrs + i;
313 struct etnaviv_gem_submit_bo *bo;
316 ret = submit_bo(submit, r->read_idx, &bo);
320 /* at offset 0 a sequence number gets stored used for userspace sync */
321 if (r->read_offset == 0) {
322 DRM_ERROR("perfmon request: offset is 0");
326 if (r->read_offset >= bo->obj->base.size - sizeof(u32)) {
327 DRM_ERROR("perfmon request: offset %u outside object", i);
331 if (r->flags & ~(ETNA_PM_PROCESS_PRE | ETNA_PM_PROCESS_POST)) {
332 DRM_ERROR("perfmon request: flags are not valid");
336 if (etnaviv_pm_req_validate(r, exec_state)) {
337 DRM_ERROR("perfmon request: domain or signal not valid");
341 submit->pmrs[i].flags = r->flags;
342 submit->pmrs[i].domain = r->domain;
343 submit->pmrs[i].signal = r->signal;
344 submit->pmrs[i].sequence = r->sequence;
345 submit->pmrs[i].offset = r->read_offset;
346 submit->pmrs[i].bo_vma = etnaviv_gem_vmap(&bo->obj->base);
352 static void submit_cleanup(struct kref *kref)
354 struct etnaviv_gem_submit *submit =
355 container_of(kref, struct etnaviv_gem_submit, refcount);
358 if (submit->runtime_resumed)
359 pm_runtime_put_autosuspend(submit->gpu->dev);
361 if (submit->cmdbuf.suballoc)
362 etnaviv_cmdbuf_free(&submit->cmdbuf);
364 for (i = 0; i < submit->nr_bos; i++) {
365 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
367 /* unpin all objects */
368 if (submit->bos[i].flags & BO_PINNED) {
369 etnaviv_gem_mapping_unreference(submit->bos[i].mapping);
370 atomic_dec(&etnaviv_obj->gpu_active);
371 submit->bos[i].mapping = NULL;
372 submit->bos[i].flags &= ~BO_PINNED;
375 /* if the GPU submit failed, objects might still be locked */
376 submit_unlock_object(submit, i);
377 drm_gem_object_put_unlocked(&etnaviv_obj->base);
380 wake_up_all(&submit->gpu->fence_event);
382 if (submit->in_fence)
383 dma_fence_put(submit->in_fence);
384 if (submit->out_fence)
385 dma_fence_put(submit->out_fence);
390 void etnaviv_submit_put(struct etnaviv_gem_submit *submit)
392 kref_put(&submit->refcount, submit_cleanup);
395 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
396 struct drm_file *file)
398 struct etnaviv_drm_private *priv = dev->dev_private;
399 struct drm_etnaviv_gem_submit *args = data;
400 struct drm_etnaviv_gem_submit_reloc *relocs;
401 struct drm_etnaviv_gem_submit_pmr *pmrs;
402 struct drm_etnaviv_gem_submit_bo *bos;
403 struct etnaviv_gem_submit *submit;
404 struct etnaviv_gpu *gpu;
405 struct sync_file *sync_file = NULL;
406 struct ww_acquire_ctx ticket;
407 int out_fence_fd = -1;
411 if (args->pipe >= ETNA_MAX_PIPES)
414 gpu = priv->gpu[args->pipe];
418 if (args->stream_size % 4) {
419 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
424 if (args->exec_state != ETNA_PIPE_3D &&
425 args->exec_state != ETNA_PIPE_2D &&
426 args->exec_state != ETNA_PIPE_VG) {
427 DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state);
431 if (args->flags & ~ETNA_SUBMIT_FLAGS) {
432 DRM_ERROR("invalid flags: 0x%x\n", args->flags);
437 * Copy the command submission and bo array to kernel space in
438 * one go, and do this outside of any locks.
440 bos = kvmalloc_array(args->nr_bos, sizeof(*bos), GFP_KERNEL);
441 relocs = kvmalloc_array(args->nr_relocs, sizeof(*relocs), GFP_KERNEL);
442 pmrs = kvmalloc_array(args->nr_pmrs, sizeof(*pmrs), GFP_KERNEL);
443 stream = kvmalloc_array(1, args->stream_size, GFP_KERNEL);
444 if (!bos || !relocs || !pmrs || !stream) {
446 goto err_submit_cmds;
449 ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
450 args->nr_bos * sizeof(*bos));
453 goto err_submit_cmds;
456 ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
457 args->nr_relocs * sizeof(*relocs));
460 goto err_submit_cmds;
463 ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs),
464 args->nr_pmrs * sizeof(*pmrs));
467 goto err_submit_cmds;
470 ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
474 goto err_submit_cmds;
477 if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
478 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
479 if (out_fence_fd < 0) {
481 goto err_submit_cmds;
485 ww_acquire_init(&ticket, &reservation_ww_class);
487 submit = submit_create(dev, gpu, args->nr_bos, args->nr_pmrs);
490 goto err_submit_ww_acquire;
493 ret = etnaviv_cmdbuf_init(gpu->cmdbuf_suballoc, &submit->cmdbuf,
494 ALIGN(args->stream_size, 8) + 8);
496 goto err_submit_objects;
498 submit->cmdbuf.ctx = file->driver_priv;
499 submit->exec_state = args->exec_state;
500 submit->flags = args->flags;
502 ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
504 goto err_submit_objects;
506 ret = submit_lock_objects(submit, &ticket);
508 goto err_submit_objects;
510 if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
511 relocs, args->nr_relocs)) {
513 goto err_submit_objects;
516 if (args->flags & ETNA_SUBMIT_FENCE_FD_IN) {
517 submit->in_fence = sync_file_get_fence(args->fence_fd);
518 if (!submit->in_fence) {
520 goto err_submit_objects;
524 ret = submit_fence_sync(submit);
526 goto err_submit_objects;
528 ret = submit_pin_objects(submit);
530 goto err_submit_objects;
532 ret = submit_reloc(submit, stream, args->stream_size / 4,
533 relocs, args->nr_relocs);
535 goto err_submit_objects;
537 ret = submit_perfmon_validate(submit, args->exec_state, pmrs);
539 goto err_submit_objects;
541 memcpy(submit->cmdbuf.vaddr, stream, args->stream_size);
542 submit->cmdbuf.user_size = ALIGN(args->stream_size, 8);
544 ret = etnaviv_gpu_submit(gpu, submit);
546 goto err_submit_objects;
548 submit_attach_object_fences(submit);
550 if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
552 * This can be improved: ideally we want to allocate the sync
553 * file before kicking off the GPU job and just attach the
554 * fence to the sync file here, eliminating the ENOMEM
555 * possibility at this stage.
557 sync_file = sync_file_create(submit->out_fence);
560 goto err_submit_objects;
562 fd_install(out_fence_fd, sync_file->file);
565 args->fence_fd = out_fence_fd;
566 args->fence = submit->out_fence->seqno;
569 etnaviv_submit_put(submit);
571 err_submit_ww_acquire:
572 ww_acquire_fini(&ticket);
575 if (ret && (out_fence_fd >= 0))
576 put_unused_fd(out_fence_fd);