Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013-2016 Red Hat |
| 3 | * Author: Rob Clark <robdclark@gmail.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 18 | #include <linux/fence.h> |
| 19 | |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 20 | #include "msm_drv.h" |
| 21 | #include "msm_fence.h" |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 22 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 23 | |
| 24 | struct msm_fence_context * |
| 25 | msm_fence_context_alloc(struct drm_device *dev, const char *name) |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 26 | { |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 27 | struct msm_fence_context *fctx; |
| 28 | |
| 29 | fctx = kzalloc(sizeof(*fctx), GFP_KERNEL); |
| 30 | if (!fctx) |
| 31 | return ERR_PTR(-ENOMEM); |
| 32 | |
| 33 | fctx->dev = dev; |
| 34 | fctx->name = name; |
| 35 | init_waitqueue_head(&fctx->event); |
| 36 | INIT_LIST_HEAD(&fctx->fence_cbs); |
| 37 | |
| 38 | return fctx; |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 41 | void msm_fence_context_free(struct msm_fence_context *fctx) |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 42 | { |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 43 | kfree(fctx); |
| 44 | } |
| 45 | |
| 46 | static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fence) |
| 47 | { |
| 48 | return (int32_t)(fctx->completed_fence - fence) >= 0; |
| 49 | } |
| 50 | |
| 51 | int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence, |
| 52 | ktime_t *timeout, bool interruptible) |
| 53 | { |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 54 | int ret; |
| 55 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 56 | if (fence > fctx->last_fence) { |
| 57 | DRM_ERROR("%s: waiting on invalid fence: %u (of %u)\n", |
| 58 | fctx->name, fence, fctx->last_fence); |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | if (!timeout) { |
| 63 | /* no-wait: */ |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 64 | ret = fence_completed(fctx, fence) ? 0 : -EBUSY; |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 65 | } else { |
Rob Clark | 340ff41 | 2016-03-16 14:57:22 -0400 | [diff] [blame] | 66 | unsigned long remaining_jiffies = timeout_to_jiffies(timeout); |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 67 | |
| 68 | if (interruptible) |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 69 | ret = wait_event_interruptible_timeout(fctx->event, |
| 70 | fence_completed(fctx, fence), |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 71 | remaining_jiffies); |
| 72 | else |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 73 | ret = wait_event_timeout(fctx->event, |
| 74 | fence_completed(fctx, fence), |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 75 | remaining_jiffies); |
| 76 | |
| 77 | if (ret == 0) { |
| 78 | DBG("timeout waiting for fence: %u (completed: %u)", |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 79 | fence, fctx->completed_fence); |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 80 | ret = -ETIMEDOUT; |
| 81 | } else if (ret != -ERESTARTSYS) { |
| 82 | ret = 0; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 89 | int msm_queue_fence_cb(struct msm_fence_context *fctx, |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 90 | struct msm_fence_cb *cb, uint32_t fence) |
| 91 | { |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 92 | struct msm_drm_private *priv = fctx->dev->dev_private; |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 93 | int ret = 0; |
| 94 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 95 | mutex_lock(&fctx->dev->struct_mutex); |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 96 | if (!list_empty(&cb->work.entry)) { |
| 97 | ret = -EINVAL; |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 98 | } else if (fence > fctx->completed_fence) { |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 99 | cb->fence = fence; |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 100 | list_add_tail(&cb->work.entry, &fctx->fence_cbs); |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 101 | } else { |
| 102 | queue_work(priv->wq, &cb->work); |
| 103 | } |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 104 | mutex_unlock(&fctx->dev->struct_mutex); |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 105 | |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | /* called from workqueue */ |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 110 | void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence) |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 111 | { |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 112 | struct msm_drm_private *priv = fctx->dev->dev_private; |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 113 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 114 | mutex_lock(&fctx->dev->struct_mutex); |
| 115 | fctx->completed_fence = max(fence, fctx->completed_fence); |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 116 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 117 | while (!list_empty(&fctx->fence_cbs)) { |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 118 | struct msm_fence_cb *cb; |
| 119 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 120 | cb = list_first_entry(&fctx->fence_cbs, |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 121 | struct msm_fence_cb, work.entry); |
| 122 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 123 | if (cb->fence > fctx->completed_fence) |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 124 | break; |
| 125 | |
| 126 | list_del_init(&cb->work.entry); |
| 127 | queue_work(priv->wq, &cb->work); |
| 128 | } |
| 129 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 130 | mutex_unlock(&fctx->dev->struct_mutex); |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 131 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame^] | 132 | wake_up_all(&fctx->event); |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void __msm_fence_worker(struct work_struct *work) |
| 136 | { |
| 137 | struct msm_fence_cb *cb = container_of(work, struct msm_fence_cb, work); |
| 138 | cb->func(cb); |
| 139 | } |