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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 27 | #include <drm/drmP.h> |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 28 | |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 29 | #include <linux/ktime.h> |
| 30 | #include <linux/hrtimer.h> |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 31 | #include <trace/events/fence.h> |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 32 | |
Ben Skeggs | 867920f | 2014-08-10 04:10:25 +1000 | [diff] [blame] | 33 | #include <nvif/notify.h> |
| 34 | #include <nvif/event.h> |
| 35 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 36 | #include "nouveau_drm.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 37 | #include "nouveau_dma.h" |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 38 | #include "nouveau_fence.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 39 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 40 | static const struct fence_ops nouveau_fence_ops_uevent; |
| 41 | static const struct fence_ops nouveau_fence_ops_legacy; |
| 42 | |
| 43 | static inline struct nouveau_fence * |
| 44 | from_fence(struct fence *fence) |
| 45 | { |
| 46 | return container_of(fence, struct nouveau_fence, base); |
| 47 | } |
| 48 | |
| 49 | static inline struct nouveau_fence_chan * |
| 50 | nouveau_fctx(struct nouveau_fence *fence) |
| 51 | { |
| 52 | return container_of(fence->base.lock, struct nouveau_fence_chan, lock); |
| 53 | } |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 54 | |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 55 | static int |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 56 | nouveau_fence_signal(struct nouveau_fence *fence) |
| 57 | { |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 58 | int drop = 0; |
| 59 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 60 | fence_signal_locked(&fence->base); |
| 61 | list_del(&fence->head); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 62 | rcu_assign_pointer(fence->channel, NULL); |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 63 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 64 | if (test_bit(FENCE_FLAG_USER_BITS, &fence->base.flags)) { |
| 65 | struct nouveau_fence_chan *fctx = nouveau_fctx(fence); |
| 66 | |
| 67 | if (!--fctx->notify_ref) |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 68 | drop = 1; |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 69 | } |
| 70 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 71 | fence_put(&fence->base); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 72 | return drop; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static struct nouveau_fence * |
| 76 | nouveau_local_fence(struct fence *fence, struct nouveau_drm *drm) { |
| 77 | struct nouveau_fence_priv *priv = (void*)drm->fence; |
| 78 | |
| 79 | if (fence->ops != &nouveau_fence_ops_legacy && |
| 80 | fence->ops != &nouveau_fence_ops_uevent) |
| 81 | return NULL; |
| 82 | |
| 83 | if (fence->context < priv->context_base || |
| 84 | fence->context >= priv->context_base + priv->contexts) |
| 85 | return NULL; |
| 86 | |
| 87 | return from_fence(fence); |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 88 | } |
| 89 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 90 | void |
| 91 | nouveau_fence_context_del(struct nouveau_fence_chan *fctx) |
| 92 | { |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 93 | struct nouveau_fence *fence; |
| 94 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 95 | spin_lock_irq(&fctx->lock); |
| 96 | while (!list_empty(&fctx->pending)) { |
| 97 | fence = list_entry(fctx->pending.next, typeof(*fence), head); |
| 98 | |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 99 | if (nouveau_fence_signal(fence)) |
| 100 | nvif_notify_put(&fctx->notify); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 101 | } |
| 102 | spin_unlock_irq(&fctx->lock); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 103 | |
| 104 | nvif_notify_fini(&fctx->notify); |
| 105 | fctx->dead = 1; |
| 106 | |
| 107 | /* |
| 108 | * Ensure that all accesses to fence->channel complete before freeing |
| 109 | * the channel. |
| 110 | */ |
| 111 | synchronize_rcu(); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static void |
Maarten Lankhorst | 15a996b | 2014-09-29 10:06:18 +0200 | [diff] [blame] | 115 | nouveau_fence_context_put(struct kref *fence_ref) |
| 116 | { |
| 117 | kfree(container_of(fence_ref, struct nouveau_fence_chan, fence_ref)); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | nouveau_fence_context_free(struct nouveau_fence_chan *fctx) |
| 122 | { |
| 123 | kref_put(&fctx->fence_ref, nouveau_fence_context_put); |
| 124 | } |
| 125 | |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 126 | static int |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 127 | nouveau_fence_update(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx) |
| 128 | { |
| 129 | struct nouveau_fence *fence; |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 130 | int drop = 0; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 131 | u32 seq = fctx->read(chan); |
| 132 | |
| 133 | while (!list_empty(&fctx->pending)) { |
| 134 | fence = list_entry(fctx->pending.next, typeof(*fence), head); |
| 135 | |
| 136 | if ((int)(seq - fence->base.seqno) < 0) |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 137 | break; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 138 | |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 139 | drop |= nouveau_fence_signal(fence); |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 140 | } |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 141 | |
| 142 | return drop; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static int |
| 146 | nouveau_fence_wait_uevent_handler(struct nvif_notify *notify) |
| 147 | { |
| 148 | struct nouveau_fence_chan *fctx = |
| 149 | container_of(notify, typeof(*fctx), notify); |
| 150 | unsigned long flags; |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 151 | int ret = NVIF_NOTIFY_KEEP; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 152 | |
| 153 | spin_lock_irqsave(&fctx->lock, flags); |
| 154 | if (!list_empty(&fctx->pending)) { |
| 155 | struct nouveau_fence *fence; |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 156 | struct nouveau_channel *chan; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 157 | |
| 158 | fence = list_entry(fctx->pending.next, typeof(*fence), head); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 159 | chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock)); |
| 160 | if (nouveau_fence_update(fence->channel, fctx)) |
| 161 | ret = NVIF_NOTIFY_DROP; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 162 | } |
| 163 | spin_unlock_irqrestore(&fctx->lock, flags); |
| 164 | |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 165 | return ret; |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 169 | nouveau_fence_context_new(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx) |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 170 | { |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 171 | struct nouveau_fence_priv *priv = (void*)chan->drm->fence; |
Ben Skeggs | a01ca78 | 2015-08-20 14:54:15 +1000 | [diff] [blame] | 172 | struct nouveau_cli *cli = (void *)chan->user.client; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 173 | int ret; |
| 174 | |
Ben Skeggs | f589be8 | 2012-07-22 11:55:54 +1000 | [diff] [blame] | 175 | INIT_LIST_HEAD(&fctx->flip); |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 176 | INIT_LIST_HEAD(&fctx->pending); |
| 177 | spin_lock_init(&fctx->lock); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 178 | fctx->context = priv->context_base + chan->chid; |
| 179 | |
Maarten Lankhorst | 15a996b | 2014-09-29 10:06:18 +0200 | [diff] [blame] | 180 | if (chan == chan->drm->cechan) |
| 181 | strcpy(fctx->name, "copy engine channel"); |
| 182 | else if (chan == chan->drm->channel) |
| 183 | strcpy(fctx->name, "generic kernel channel"); |
| 184 | else |
Ben Skeggs | 989aa5b | 2015-01-12 12:33:37 +1000 | [diff] [blame] | 185 | strcpy(fctx->name, nvxx_client(&cli->base)->name); |
Maarten Lankhorst | 15a996b | 2014-09-29 10:06:18 +0200 | [diff] [blame] | 186 | |
| 187 | kref_init(&fctx->fence_ref); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 188 | if (!priv->uevent) |
| 189 | return; |
| 190 | |
Ben Skeggs | a01ca78 | 2015-08-20 14:54:15 +1000 | [diff] [blame] | 191 | ret = nvif_notify_init(&chan->user, nouveau_fence_wait_uevent_handler, |
| 192 | false, G82_CHANNEL_DMA_V0_NTFY_UEVENT, |
| 193 | &(struct nvif_notify_uevent_req) { }, |
| 194 | sizeof(struct nvif_notify_uevent_req), |
| 195 | sizeof(struct nvif_notify_uevent_rep), |
| 196 | &fctx->notify); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 197 | |
| 198 | WARN_ON(ret); |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 199 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 200 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 201 | struct nouveau_fence_work { |
| 202 | struct work_struct work; |
| 203 | struct fence_cb cb; |
| 204 | void (*func)(void *); |
| 205 | void *data; |
| 206 | }; |
| 207 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 208 | static void |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 209 | nouveau_fence_work_handler(struct work_struct *kwork) |
| 210 | { |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 211 | struct nouveau_fence_work *work = container_of(kwork, typeof(*work), work); |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 212 | work->func(work->data); |
| 213 | kfree(work); |
| 214 | } |
| 215 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 216 | static void nouveau_fence_work_cb(struct fence *fence, struct fence_cb *cb) |
| 217 | { |
| 218 | struct nouveau_fence_work *work = container_of(cb, typeof(*work), cb); |
| 219 | |
| 220 | schedule_work(&work->work); |
| 221 | } |
| 222 | |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 223 | void |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 224 | nouveau_fence_work(struct fence *fence, |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 225 | void (*func)(void *), void *data) |
| 226 | { |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 227 | struct nouveau_fence_work *work; |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 228 | |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 229 | if (fence_is_signaled(fence)) |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 230 | goto err; |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 231 | |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 232 | work = kmalloc(sizeof(*work), GFP_KERNEL); |
| 233 | if (!work) { |
Maarten Lankhorst | bb6178b | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 234 | /* |
| 235 | * this might not be a nouveau fence any more, |
| 236 | * so force a lazy wait here |
| 237 | */ |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 238 | WARN_ON(nouveau_fence_wait((struct nouveau_fence *)fence, |
Maarten Lankhorst | bb6178b | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 239 | true, false)); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 240 | goto err; |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 241 | } |
| 242 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 243 | INIT_WORK(&work->work, nouveau_fence_work_handler); |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 244 | work->func = func; |
| 245 | work->data = data; |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 246 | |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 247 | if (fence_add_callback(fence, &work->cb, nouveau_fence_work_cb) < 0) |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 248 | goto err_free; |
| 249 | return; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 250 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 251 | err_free: |
| 252 | kfree(work); |
| 253 | err: |
| 254 | func(data); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | int |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 258 | nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 259 | { |
Ben Skeggs | e193b1d | 2012-07-19 10:51:42 +1000 | [diff] [blame] | 260 | struct nouveau_fence_chan *fctx = chan->fence; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 261 | struct nouveau_fence_priv *priv = (void*)chan->drm->fence; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 262 | int ret; |
| 263 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 264 | fence->channel = chan; |
Daniel Vetter | bfd8303 | 2013-12-11 11:34:41 +0100 | [diff] [blame] | 265 | fence->timeout = jiffies + (15 * HZ); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 266 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 267 | if (priv->uevent) |
| 268 | fence_init(&fence->base, &nouveau_fence_ops_uevent, |
Maarten Lankhorst | e3be4c2 | 2014-09-16 11:15:07 +0200 | [diff] [blame] | 269 | &fctx->lock, fctx->context, ++fctx->sequence); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 270 | else |
| 271 | fence_init(&fence->base, &nouveau_fence_ops_legacy, |
Maarten Lankhorst | e3be4c2 | 2014-09-16 11:15:07 +0200 | [diff] [blame] | 272 | &fctx->lock, fctx->context, ++fctx->sequence); |
Maarten Lankhorst | 15a996b | 2014-09-29 10:06:18 +0200 | [diff] [blame] | 273 | kref_get(&fctx->fence_ref); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 274 | |
| 275 | trace_fence_emit(&fence->base); |
Ben Skeggs | 827520c | 2013-02-14 13:20:17 +1000 | [diff] [blame] | 276 | ret = fctx->emit(fence); |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 277 | if (!ret) { |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 278 | fence_get(&fence->base); |
| 279 | spin_lock_irq(&fctx->lock); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 280 | |
| 281 | if (nouveau_fence_update(chan, fctx)) |
| 282 | nvif_notify_put(&fctx->notify); |
| 283 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 284 | list_add_tail(&fence->head, &fctx->pending); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 285 | spin_unlock_irq(&fctx->lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 286 | } |
| 287 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 288 | return ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 289 | } |
| 290 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 291 | bool |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 292 | nouveau_fence_done(struct nouveau_fence *fence) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 293 | { |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 294 | if (fence->base.ops == &nouveau_fence_ops_legacy || |
| 295 | fence->base.ops == &nouveau_fence_ops_uevent) { |
| 296 | struct nouveau_fence_chan *fctx = nouveau_fctx(fence); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 297 | struct nouveau_channel *chan; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 298 | unsigned long flags; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 299 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 300 | if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags)) |
| 301 | return true; |
Ben Skeggs | 79ca277 | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 302 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 303 | spin_lock_irqsave(&fctx->lock, flags); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 304 | chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock)); |
| 305 | if (chan && nouveau_fence_update(chan, fctx)) |
| 306 | nvif_notify_put(&fctx->notify); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 307 | spin_unlock_irqrestore(&fctx->lock, flags); |
Ben Skeggs | e18c080 | 2013-01-31 14:57:33 +1000 | [diff] [blame] | 308 | } |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 309 | return fence_is_signaled(&fence->base); |
Ben Skeggs | e18c080 | 2013-01-31 14:57:33 +1000 | [diff] [blame] | 310 | } |
| 311 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 312 | static long |
| 313 | nouveau_fence_wait_legacy(struct fence *f, bool intr, long wait) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 314 | { |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 315 | struct nouveau_fence *fence = from_fence(f); |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 316 | unsigned long sleep_time = NSEC_PER_MSEC / 1000; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 317 | unsigned long t = jiffies, timeout = t + wait; |
Ben Skeggs | e18c080 | 2013-01-31 14:57:33 +1000 | [diff] [blame] | 318 | |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 319 | while (!nouveau_fence_done(fence)) { |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 320 | ktime_t kt; |
| 321 | |
| 322 | t = jiffies; |
| 323 | |
| 324 | if (wait != MAX_SCHEDULE_TIMEOUT && time_after_eq(t, timeout)) { |
| 325 | __set_current_state(TASK_RUNNING); |
| 326 | return 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 327 | } |
| 328 | |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 329 | __set_current_state(intr ? TASK_INTERRUPTIBLE : |
| 330 | TASK_UNINTERRUPTIBLE); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 331 | |
| 332 | kt = ktime_set(0, sleep_time); |
| 333 | schedule_hrtimeout(&kt, HRTIMER_MODE_REL); |
| 334 | sleep_time *= 2; |
| 335 | if (sleep_time > NSEC_PER_MSEC) |
| 336 | sleep_time = NSEC_PER_MSEC; |
| 337 | |
| 338 | if (intr && signal_pending(current)) |
| 339 | return -ERESTARTSYS; |
| 340 | } |
| 341 | |
| 342 | __set_current_state(TASK_RUNNING); |
| 343 | |
| 344 | return timeout - t; |
| 345 | } |
| 346 | |
| 347 | static int |
| 348 | nouveau_fence_wait_busy(struct nouveau_fence *fence, bool intr) |
| 349 | { |
| 350 | int ret = 0; |
| 351 | |
| 352 | while (!nouveau_fence_done(fence)) { |
| 353 | if (time_after_eq(jiffies, fence->timeout)) { |
| 354 | ret = -EBUSY; |
| 355 | break; |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 356 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 357 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 358 | __set_current_state(intr ? |
| 359 | TASK_INTERRUPTIBLE : |
| 360 | TASK_UNINTERRUPTIBLE); |
| 361 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 362 | if (intr && signal_pending(current)) { |
Ben Skeggs | 9ddc8c5 | 2009-12-15 11:04:25 +1000 | [diff] [blame] | 363 | ret = -ERESTARTSYS; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | __set_current_state(TASK_RUNNING); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 369 | return ret; |
| 370 | } |
| 371 | |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 372 | int |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 373 | nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr) |
| 374 | { |
| 375 | long ret; |
| 376 | |
| 377 | if (!lazy) |
| 378 | return nouveau_fence_wait_busy(fence, intr); |
| 379 | |
| 380 | ret = fence_wait_timeout(&fence->base, intr, 15 * HZ); |
| 381 | if (ret < 0) |
| 382 | return ret; |
| 383 | else if (!ret) |
| 384 | return -EBUSY; |
| 385 | else |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | int |
Maarten Lankhorst | e3be4c2 | 2014-09-16 11:15:07 +0200 | [diff] [blame] | 390 | nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, bool exclusive, bool intr) |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 391 | { |
Ben Skeggs | 827520c | 2013-02-14 13:20:17 +1000 | [diff] [blame] | 392 | struct nouveau_fence_chan *fctx = chan->fence; |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 393 | struct fence *fence; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 394 | struct reservation_object *resv = nvbo->bo.resv; |
| 395 | struct reservation_object_list *fobj; |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 396 | struct nouveau_fence *f; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 397 | int ret = 0, i; |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 398 | |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 399 | if (!exclusive) { |
| 400 | ret = reservation_object_reserve_shared(resv); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 401 | |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 402 | if (ret) |
| 403 | return ret; |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 404 | } |
| 405 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 406 | fobj = reservation_object_get_list(resv); |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 407 | fence = reservation_object_get_excl(resv); |
| 408 | |
| 409 | if (fence && (!exclusive || !fobj || !fobj->shared_count)) { |
| 410 | struct nouveau_channel *prev = NULL; |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 411 | bool must_wait = true; |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 412 | |
| 413 | f = nouveau_local_fence(fence, chan->drm); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 414 | if (f) { |
| 415 | rcu_read_lock(); |
| 416 | prev = rcu_dereference(f->channel); |
| 417 | if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0)) |
| 418 | must_wait = false; |
| 419 | rcu_read_unlock(); |
| 420 | } |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 421 | |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 422 | if (must_wait) |
Maarten Lankhorst | e3be4c2 | 2014-09-16 11:15:07 +0200 | [diff] [blame] | 423 | ret = fence_wait(fence, intr); |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 424 | |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | if (!exclusive || !fobj) |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 429 | return ret; |
| 430 | |
| 431 | for (i = 0; i < fobj->shared_count && !ret; ++i) { |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 432 | struct nouveau_channel *prev = NULL; |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 433 | bool must_wait = true; |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 434 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 435 | fence = rcu_dereference_protected(fobj->shared[i], |
| 436 | reservation_object_held(resv)); |
| 437 | |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 438 | f = nouveau_local_fence(fence, chan->drm); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 439 | if (f) { |
| 440 | rcu_read_lock(); |
| 441 | prev = rcu_dereference(f->channel); |
| 442 | if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0)) |
| 443 | must_wait = false; |
| 444 | rcu_read_unlock(); |
| 445 | } |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 446 | |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 447 | if (must_wait) |
Maarten Lankhorst | e3be4c2 | 2014-09-16 11:15:07 +0200 | [diff] [blame] | 448 | ret = fence_wait(fence, intr); |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | return ret; |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void |
| 455 | nouveau_fence_unref(struct nouveau_fence **pfence) |
| 456 | { |
| 457 | if (*pfence) |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 458 | fence_put(&(*pfence)->base); |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 459 | *pfence = NULL; |
| 460 | } |
| 461 | |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 462 | int |
Ben Skeggs | 264ce19 | 2013-02-14 13:43:21 +1000 | [diff] [blame] | 463 | nouveau_fence_new(struct nouveau_channel *chan, bool sysmem, |
| 464 | struct nouveau_fence **pfence) |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 465 | { |
| 466 | struct nouveau_fence *fence; |
| 467 | int ret = 0; |
| 468 | |
Ben Skeggs | e193b1d | 2012-07-19 10:51:42 +1000 | [diff] [blame] | 469 | if (unlikely(!chan->fence)) |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 470 | return -ENODEV; |
| 471 | |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 472 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
| 473 | if (!fence) |
| 474 | return -ENOMEM; |
Ben Skeggs | 264ce19 | 2013-02-14 13:43:21 +1000 | [diff] [blame] | 475 | |
| 476 | fence->sysmem = sysmem; |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 477 | |
Cong Ding | b5d8f05 | 2013-01-15 18:19:49 +0100 | [diff] [blame] | 478 | ret = nouveau_fence_emit(fence, chan); |
| 479 | if (ret) |
| 480 | nouveau_fence_unref(&fence); |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 481 | |
| 482 | *pfence = fence; |
| 483 | return ret; |
| 484 | } |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 485 | |
| 486 | static const char *nouveau_fence_get_get_driver_name(struct fence *fence) |
| 487 | { |
| 488 | return "nouveau"; |
| 489 | } |
| 490 | |
| 491 | static const char *nouveau_fence_get_timeline_name(struct fence *f) |
| 492 | { |
| 493 | struct nouveau_fence *fence = from_fence(f); |
| 494 | struct nouveau_fence_chan *fctx = nouveau_fctx(fence); |
| 495 | |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 496 | return !fctx->dead ? fctx->name : "dead channel"; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* |
| 500 | * In an ideal world, read would not assume the channel context is still alive. |
| 501 | * This function may be called from another device, running into free memory as a |
| 502 | * result. The drm node should still be there, so we can derive the index from |
| 503 | * the fence context. |
| 504 | */ |
| 505 | static bool nouveau_fence_is_signaled(struct fence *f) |
| 506 | { |
| 507 | struct nouveau_fence *fence = from_fence(f); |
| 508 | struct nouveau_fence_chan *fctx = nouveau_fctx(fence); |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 509 | struct nouveau_channel *chan; |
| 510 | bool ret = false; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 511 | |
Maarten Lankhorst | 0ec5f02 | 2014-12-01 19:11:06 +1000 | [diff] [blame] | 512 | rcu_read_lock(); |
| 513 | chan = rcu_dereference(fence->channel); |
| 514 | if (chan) |
| 515 | ret = (int)(fctx->read(chan) - fence->base.seqno) >= 0; |
| 516 | rcu_read_unlock(); |
| 517 | |
| 518 | return ret; |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static bool nouveau_fence_no_signaling(struct fence *f) |
| 522 | { |
| 523 | struct nouveau_fence *fence = from_fence(f); |
| 524 | |
| 525 | /* |
| 526 | * caller should have a reference on the fence, |
| 527 | * else fence could get freed here |
| 528 | */ |
| 529 | WARN_ON(atomic_read(&fence->base.refcount.refcount) <= 1); |
| 530 | |
| 531 | /* |
| 532 | * This needs uevents to work correctly, but fence_add_callback relies on |
| 533 | * being able to enable signaling. It will still get signaled eventually, |
| 534 | * just not right away. |
| 535 | */ |
| 536 | if (nouveau_fence_is_signaled(f)) { |
| 537 | list_del(&fence->head); |
| 538 | |
| 539 | fence_put(&fence->base); |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | return true; |
| 544 | } |
| 545 | |
Maarten Lankhorst | 15a996b | 2014-09-29 10:06:18 +0200 | [diff] [blame] | 546 | static void nouveau_fence_release(struct fence *f) |
| 547 | { |
| 548 | struct nouveau_fence *fence = from_fence(f); |
| 549 | struct nouveau_fence_chan *fctx = nouveau_fctx(fence); |
| 550 | |
| 551 | kref_put(&fctx->fence_ref, nouveau_fence_context_put); |
| 552 | fence_free(&fence->base); |
| 553 | } |
| 554 | |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 555 | static const struct fence_ops nouveau_fence_ops_legacy = { |
| 556 | .get_driver_name = nouveau_fence_get_get_driver_name, |
| 557 | .get_timeline_name = nouveau_fence_get_timeline_name, |
| 558 | .enable_signaling = nouveau_fence_no_signaling, |
| 559 | .signaled = nouveau_fence_is_signaled, |
| 560 | .wait = nouveau_fence_wait_legacy, |
Maarten Lankhorst | 15a996b | 2014-09-29 10:06:18 +0200 | [diff] [blame] | 561 | .release = nouveau_fence_release |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | static bool nouveau_fence_enable_signaling(struct fence *f) |
| 565 | { |
| 566 | struct nouveau_fence *fence = from_fence(f); |
| 567 | struct nouveau_fence_chan *fctx = nouveau_fctx(fence); |
| 568 | bool ret; |
| 569 | |
| 570 | if (!fctx->notify_ref++) |
| 571 | nvif_notify_get(&fctx->notify); |
| 572 | |
| 573 | ret = nouveau_fence_no_signaling(f); |
| 574 | if (ret) |
| 575 | set_bit(FENCE_FLAG_USER_BITS, &fence->base.flags); |
| 576 | else if (!--fctx->notify_ref) |
| 577 | nvif_notify_put(&fctx->notify); |
| 578 | |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | static const struct fence_ops nouveau_fence_ops_uevent = { |
| 583 | .get_driver_name = nouveau_fence_get_get_driver_name, |
| 584 | .get_timeline_name = nouveau_fence_get_timeline_name, |
| 585 | .enable_signaling = nouveau_fence_enable_signaling, |
| 586 | .signaled = nouveau_fence_is_signaled, |
| 587 | .wait = fence_default_wait, |
| 588 | .release = NULL |
| 589 | }; |