Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Ben Skeggs. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE |
| 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include "drmP.h" |
| 28 | #include "drm.h" |
| 29 | |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 30 | #include <linux/ktime.h> |
| 31 | #include <linux/hrtimer.h> |
| 32 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 33 | #include "nouveau_drv.h" |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 34 | #include "nouveau_ramht.h" |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 35 | #include "nouveau_fence.h" |
Ben Skeggs | 20abd16 | 2012-04-30 11:33:43 -0500 | [diff] [blame] | 36 | #include "nouveau_software.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 37 | #include "nouveau_dma.h" |
| 38 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 39 | void |
| 40 | nouveau_fence_context_del(struct nouveau_fence_chan *fctx) |
| 41 | { |
| 42 | struct nouveau_fence *fence, *fnext; |
| 43 | spin_lock(&fctx->lock); |
| 44 | list_for_each_entry_safe(fence, fnext, &fctx->pending, head) { |
| 45 | if (fence->work) |
| 46 | fence->work(fence->priv, false); |
| 47 | fence->channel = NULL; |
| 48 | list_del(&fence->head); |
| 49 | nouveau_fence_unref(&fence); |
| 50 | } |
| 51 | spin_unlock(&fctx->lock); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | nouveau_fence_context_new(struct nouveau_fence_chan *fctx) |
| 56 | { |
| 57 | INIT_LIST_HEAD(&fctx->pending); |
| 58 | spin_lock_init(&fctx->lock); |
| 59 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 60 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 61 | void |
| 62 | nouveau_fence_update(struct nouveau_channel *chan) |
| 63 | { |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 64 | struct drm_device *dev = chan->dev; |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 65 | struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE); |
| 66 | struct nouveau_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE]; |
| 67 | struct nouveau_fence *fence, *fnext; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 68 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 69 | spin_lock(&fctx->lock); |
| 70 | list_for_each_entry_safe(fence, fnext, &fctx->pending, head) { |
| 71 | if (priv->read(chan) < fence->sequence) |
Ben Skeggs | b08abd4 | 2012-03-21 13:51:03 +1000 | [diff] [blame] | 72 | break; |
| 73 | |
Ben Skeggs | b08abd4 | 2012-03-21 13:51:03 +1000 | [diff] [blame] | 74 | if (fence->work) |
Francisco Jerez | 8ac3891 | 2010-09-21 20:49:39 +0200 | [diff] [blame] | 75 | fence->work(fence->priv, true); |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 76 | fence->channel = NULL; |
| 77 | list_del(&fence->head); |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 78 | nouveau_fence_unref(&fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 79 | } |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 80 | spin_unlock(&fctx->lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 84 | nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 85 | { |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 86 | struct drm_device *dev = chan->dev; |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 87 | struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE); |
| 88 | struct nouveau_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE]; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 89 | int ret; |
| 90 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 91 | fence->channel = chan; |
| 92 | fence->timeout = jiffies + (3 * DRM_HZ); |
| 93 | fence->sequence = ++fctx->sequence; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 94 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 95 | ret = priv->emit(fence); |
| 96 | if (!ret) { |
| 97 | kref_get(&fence->kref); |
| 98 | spin_lock(&fctx->lock); |
| 99 | list_add_tail(&fence->head, &fctx->pending); |
| 100 | spin_unlock(&fctx->lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 101 | } |
| 102 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 103 | return ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 104 | } |
| 105 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 106 | bool |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 107 | nouveau_fence_done(struct nouveau_fence *fence) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 108 | { |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 109 | if (fence->channel) |
| 110 | nouveau_fence_update(fence->channel); |
| 111 | return !fence->channel; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 115 | nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 116 | { |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 117 | unsigned long sleep_time = NSEC_PER_MSEC / 1000; |
| 118 | ktime_t t; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 119 | int ret = 0; |
| 120 | |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 121 | while (!nouveau_fence_done(fence)) { |
| 122 | if (fence->timeout && time_after_eq(jiffies, fence->timeout)) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 123 | ret = -EBUSY; |
| 124 | break; |
| 125 | } |
| 126 | |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 127 | __set_current_state(intr ? TASK_INTERRUPTIBLE : |
| 128 | TASK_UNINTERRUPTIBLE); |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 129 | if (lazy) { |
| 130 | t = ktime_set(0, sleep_time); |
| 131 | schedule_hrtimeout(&t, HRTIMER_MODE_REL); |
| 132 | sleep_time *= 2; |
| 133 | if (sleep_time > NSEC_PER_MSEC) |
| 134 | sleep_time = NSEC_PER_MSEC; |
| 135 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 136 | |
| 137 | if (intr && signal_pending(current)) { |
Ben Skeggs | 9ddc8c5 | 2009-12-15 11:04:25 +1000 | [diff] [blame] | 138 | ret = -ERESTARTSYS; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | __set_current_state(TASK_RUNNING); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 147 | int |
| 148 | nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan) |
| 149 | { |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 150 | struct drm_device *dev = chan->dev; |
| 151 | struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE); |
Ben Skeggs | 906c033 | 2012-05-04 16:25:47 +1000 | [diff] [blame] | 152 | struct nouveau_channel *prev; |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 153 | int ret = 0; |
| 154 | |
Ben Skeggs | 906c033 | 2012-05-04 16:25:47 +1000 | [diff] [blame] | 155 | prev = fence ? nouveau_channel_get_unlocked(fence->channel) : NULL; |
| 156 | if (prev) { |
| 157 | if (unlikely(prev != chan && !nouveau_fence_done(fence))) { |
| 158 | ret = priv->sync(fence, prev, chan); |
| 159 | if (unlikely(ret)) |
| 160 | ret = nouveau_fence_wait(fence, true, false); |
| 161 | } |
| 162 | nouveau_channel_put_unlocked(&prev); |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return ret; |
| 166 | } |
| 167 | |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 168 | static void |
| 169 | nouveau_fence_del(struct kref *kref) |
| 170 | { |
| 171 | struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref); |
| 172 | kfree(fence); |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | nouveau_fence_unref(struct nouveau_fence **pfence) |
| 177 | { |
| 178 | if (*pfence) |
| 179 | kref_put(&(*pfence)->kref, nouveau_fence_del); |
| 180 | *pfence = NULL; |
| 181 | } |
| 182 | |
| 183 | struct nouveau_fence * |
| 184 | nouveau_fence_ref(struct nouveau_fence *fence) |
| 185 | { |
| 186 | kref_get(&fence->kref); |
| 187 | return fence; |
| 188 | } |
| 189 | |
| 190 | int |
| 191 | nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence) |
| 192 | { |
| 193 | struct nouveau_fence *fence; |
| 194 | int ret = 0; |
| 195 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 196 | if (unlikely(!chan->engctx[NVOBJ_ENGINE_FENCE])) |
| 197 | return -ENODEV; |
| 198 | |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 199 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
| 200 | if (!fence) |
| 201 | return -ENOMEM; |
| 202 | kref_init(&fence->kref); |
| 203 | |
| 204 | if (chan) { |
| 205 | ret = nouveau_fence_emit(fence, chan); |
| 206 | if (ret) |
| 207 | nouveau_fence_unref(&fence); |
| 208 | } |
| 209 | |
| 210 | *pfence = fence; |
| 211 | return ret; |
| 212 | } |