drm/nouveau: specify if interruptible wait is desired in nouveau_fence_sync
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nouveau_fence.c
CommitLineData
6ee73861
BS
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
760285e7 27#include <drm/drmP.h>
6ee73861 28
bd35fe5a
MS
29#include <linux/ktime.h>
30#include <linux/hrtimer.h>
29ba89b2 31#include <trace/events/fence.h>
bd35fe5a 32
867920f8
BS
33#include <nvif/notify.h>
34#include <nvif/event.h>
35
ebb945a9 36#include "nouveau_drm.h"
6ee73861 37#include "nouveau_dma.h"
ebb945a9 38#include "nouveau_fence.h"
6ee73861 39
29ba89b2
ML
40static const struct fence_ops nouveau_fence_ops_uevent;
41static const struct fence_ops nouveau_fence_ops_legacy;
42
43static inline struct nouveau_fence *
44from_fence(struct fence *fence)
45{
46 return container_of(fence, struct nouveau_fence, base);
47}
48
49static inline struct nouveau_fence_chan *
50nouveau_fctx(struct nouveau_fence *fence)
51{
52 return container_of(fence->base.lock, struct nouveau_fence_chan, lock);
53}
c4c7044f
BS
54
55static void
56nouveau_fence_signal(struct nouveau_fence *fence)
57{
29ba89b2
ML
58 fence_signal_locked(&fence->base);
59 list_del(&fence->head);
60
61 if (test_bit(FENCE_FLAG_USER_BITS, &fence->base.flags)) {
62 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
c4c7044f 63
29ba89b2
ML
64 if (!--fctx->notify_ref)
65 nvif_notify_put(&fctx->notify);
c4c7044f
BS
66 }
67
29ba89b2
ML
68 fence_put(&fence->base);
69}
70
71static struct nouveau_fence *
72nouveau_local_fence(struct fence *fence, struct nouveau_drm *drm) {
73 struct nouveau_fence_priv *priv = (void*)drm->fence;
74
75 if (fence->ops != &nouveau_fence_ops_legacy &&
76 fence->ops != &nouveau_fence_ops_uevent)
77 return NULL;
78
79 if (fence->context < priv->context_base ||
80 fence->context >= priv->context_base + priv->contexts)
81 return NULL;
82
83 return from_fence(fence);
c4c7044f
BS
84}
85
5e120f6e
BS
86void
87nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
88{
29ba89b2
ML
89 struct nouveau_fence *fence;
90
91 nvif_notify_fini(&fctx->notify);
92
93 spin_lock_irq(&fctx->lock);
94 while (!list_empty(&fctx->pending)) {
95 fence = list_entry(fctx->pending.next, typeof(*fence), head);
96
c4c7044f 97 nouveau_fence_signal(fence);
29ba89b2 98 fence->channel = NULL;
5e120f6e 99 }
29ba89b2
ML
100 spin_unlock_irq(&fctx->lock);
101}
102
103static void
104nouveau_fence_update(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx)
105{
106 struct nouveau_fence *fence;
107
108 u32 seq = fctx->read(chan);
109
110 while (!list_empty(&fctx->pending)) {
111 fence = list_entry(fctx->pending.next, typeof(*fence), head);
112
113 if ((int)(seq - fence->base.seqno) < 0)
114 return;
115
116 nouveau_fence_signal(fence);
117 }
118}
119
120static int
121nouveau_fence_wait_uevent_handler(struct nvif_notify *notify)
122{
123 struct nouveau_fence_chan *fctx =
124 container_of(notify, typeof(*fctx), notify);
125 unsigned long flags;
126
127 spin_lock_irqsave(&fctx->lock, flags);
128 if (!list_empty(&fctx->pending)) {
129 struct nouveau_fence *fence;
130
131 fence = list_entry(fctx->pending.next, typeof(*fence), head);
132 nouveau_fence_update(fence->channel, fctx);
133 }
134 spin_unlock_irqrestore(&fctx->lock, flags);
135
136 /* Always return keep here. NVIF refcount is handled with nouveau_fence_update */
137 return NVIF_NOTIFY_KEEP;
5e120f6e
BS
138}
139
140void
29ba89b2 141nouveau_fence_context_new(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx)
5e120f6e 142{
29ba89b2
ML
143 struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
144 int ret;
145
f589be88 146 INIT_LIST_HEAD(&fctx->flip);
5e120f6e
BS
147 INIT_LIST_HEAD(&fctx->pending);
148 spin_lock_init(&fctx->lock);
29ba89b2
ML
149 fctx->context = priv->context_base + chan->chid;
150
151 if (!priv->uevent)
152 return;
153
154 ret = nvif_notify_init(chan->object, NULL,
155 nouveau_fence_wait_uevent_handler, false,
156 G82_CHANNEL_DMA_V0_NTFY_UEVENT,
157 &(struct nvif_notify_uevent_req) { },
158 sizeof(struct nvif_notify_uevent_req),
159 sizeof(struct nvif_notify_uevent_rep),
160 &fctx->notify);
161
162 WARN_ON(ret);
5e120f6e 163}
6ee73861 164
29ba89b2
ML
165struct nouveau_fence_work {
166 struct work_struct work;
167 struct fence_cb cb;
168 void (*func)(void *);
169 void *data;
170};
171
c4c7044f
BS
172static void
173nouveau_fence_work_handler(struct work_struct *kwork)
174{
29ba89b2 175 struct nouveau_fence_work *work = container_of(kwork, typeof(*work), work);
c4c7044f
BS
176 work->func(work->data);
177 kfree(work);
178}
179
29ba89b2
ML
180static void nouveau_fence_work_cb(struct fence *fence, struct fence_cb *cb)
181{
182 struct nouveau_fence_work *work = container_of(cb, typeof(*work), cb);
183
184 schedule_work(&work->work);
185}
186
c4c7044f 187void
f2c24b83 188nouveau_fence_work(struct fence *fence,
c4c7044f
BS
189 void (*func)(void *), void *data)
190{
29ba89b2 191 struct nouveau_fence_work *work;
c4c7044f 192
f2c24b83 193 if (fence_is_signaled(fence))
29ba89b2 194 goto err;
c4c7044f 195
c4c7044f
BS
196 work = kmalloc(sizeof(*work), GFP_KERNEL);
197 if (!work) {
f2c24b83
ML
198 WARN_ON(nouveau_fence_wait((struct nouveau_fence *)fence,
199 false, false));
29ba89b2 200 goto err;
c4c7044f
BS
201 }
202
29ba89b2 203 INIT_WORK(&work->work, nouveau_fence_work_handler);
c4c7044f
BS
204 work->func = func;
205 work->data = data;
6ee73861 206
f2c24b83 207 if (fence_add_callback(fence, &work->cb, nouveau_fence_work_cb) < 0)
29ba89b2
ML
208 goto err_free;
209 return;
b08abd4e 210
29ba89b2
ML
211err_free:
212 kfree(work);
213err:
214 func(data);
6ee73861
BS
215}
216
217int
d375e7d5 218nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
6ee73861 219{
e193b1d4 220 struct nouveau_fence_chan *fctx = chan->fence;
29ba89b2 221 struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
6ee73861
BS
222 int ret;
223
5e120f6e 224 fence->channel = chan;
bfd8303a 225 fence->timeout = jiffies + (15 * HZ);
6ee73861 226
29ba89b2
ML
227 if (priv->uevent)
228 fence_init(&fence->base, &nouveau_fence_ops_uevent,
e3be4c23 229 &fctx->lock, fctx->context, ++fctx->sequence);
29ba89b2
ML
230 else
231 fence_init(&fence->base, &nouveau_fence_ops_legacy,
e3be4c23 232 &fctx->lock, fctx->context, ++fctx->sequence);
29ba89b2
ML
233
234 trace_fence_emit(&fence->base);
827520ce 235 ret = fctx->emit(fence);
5e120f6e 236 if (!ret) {
29ba89b2
ML
237 fence_get(&fence->base);
238 spin_lock_irq(&fctx->lock);
239 nouveau_fence_update(chan, fctx);
5e120f6e 240 list_add_tail(&fence->head, &fctx->pending);
29ba89b2 241 spin_unlock_irq(&fctx->lock);
529c4959 242 }
6ee73861 243
5e120f6e 244 return ret;
6ee73861
BS
245}
246
6ee73861 247bool
d375e7d5 248nouveau_fence_done(struct nouveau_fence *fence)
6ee73861 249{
29ba89b2
ML
250 if (fence->base.ops == &nouveau_fence_ops_legacy ||
251 fence->base.ops == &nouveau_fence_ops_uevent) {
252 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
253 unsigned long flags;
6ee73861 254
29ba89b2
ML
255 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
256 return true;
79ca2770 257
29ba89b2
ML
258 spin_lock_irqsave(&fctx->lock, flags);
259 nouveau_fence_update(fence->channel, fctx);
260 spin_unlock_irqrestore(&fctx->lock, flags);
261 }
262 return fence_is_signaled(&fence->base);
e18c080f
BS
263}
264
29ba89b2
ML
265static long
266nouveau_fence_wait_legacy(struct fence *f, bool intr, long wait)
e18c080f 267{
29ba89b2
ML
268 struct nouveau_fence *fence = from_fence(f);
269 unsigned long sleep_time = NSEC_PER_MSEC / 1000;
270 unsigned long t = jiffies, timeout = t + wait;
e18c080f 271
29ba89b2
ML
272 while (!nouveau_fence_done(fence)) {
273 ktime_t kt;
51cb4b39 274
29ba89b2 275 t = jiffies;
e18c080f 276
29ba89b2
ML
277 if (wait != MAX_SCHEDULE_TIMEOUT && time_after_eq(t, timeout)) {
278 __set_current_state(TASK_RUNNING);
279 return 0;
e18c080f 280 }
29ba89b2
ML
281
282 __set_current_state(intr ? TASK_INTERRUPTIBLE :
283 TASK_UNINTERRUPTIBLE);
284
285 kt = ktime_set(0, sleep_time);
286 schedule_hrtimeout(&kt, HRTIMER_MODE_REL);
287 sleep_time *= 2;
288 if (sleep_time > NSEC_PER_MSEC)
289 sleep_time = NSEC_PER_MSEC;
290
291 if (intr && signal_pending(current))
292 return -ERESTARTSYS;
e18c080f
BS
293 }
294
29ba89b2 295 __set_current_state(TASK_RUNNING);
e18c080f 296
29ba89b2 297 return timeout - t;
e18c080f
BS
298}
299
29ba89b2
ML
300static int
301nouveau_fence_wait_busy(struct nouveau_fence *fence, bool intr)
6ee73861 302{
6ee73861
BS
303 int ret = 0;
304
d375e7d5 305 while (!nouveau_fence_done(fence)) {
29ba89b2 306 if (time_after_eq(jiffies, fence->timeout)) {
6ee73861
BS
307 ret = -EBUSY;
308 break;
309 }
310
29ba89b2
ML
311 __set_current_state(intr ?
312 TASK_INTERRUPTIBLE :
313 TASK_UNINTERRUPTIBLE);
6ee73861
BS
314
315 if (intr && signal_pending(current)) {
9ddc8c52 316 ret = -ERESTARTSYS;
6ee73861
BS
317 break;
318 }
319 }
320
321 __set_current_state(TASK_RUNNING);
d375e7d5
BS
322 return ret;
323}
324
5e120f6e 325int
29ba89b2
ML
326nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
327{
328 long ret;
329
330 if (!lazy)
331 return nouveau_fence_wait_busy(fence, intr);
332
333 ret = fence_wait_timeout(&fence->base, intr, 15 * HZ);
334 if (ret < 0)
335 return ret;
336 else if (!ret)
337 return -EBUSY;
338 else
339 return 0;
340}
341
342int
e3be4c23 343nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, bool exclusive, bool intr)
5e120f6e 344{
827520ce 345 struct nouveau_fence_chan *fctx = chan->fence;
809e9447 346 struct fence *fence;
29ba89b2
ML
347 struct reservation_object *resv = nvbo->bo.resv;
348 struct reservation_object_list *fobj;
809e9447 349 struct nouveau_fence *f;
29ba89b2
ML
350 int ret = 0, i;
351
809e9447
ML
352 if (!exclusive) {
353 ret = reservation_object_reserve_shared(resv);
354
355 if (ret)
356 return ret;
357 }
358
359 fobj = reservation_object_get_list(resv);
f2c24b83 360 fence = reservation_object_get_excl(resv);
29ba89b2 361
809e9447
ML
362 if (fence && (!exclusive || !fobj || !fobj->shared_count)) {
363 struct nouveau_channel *prev = NULL;
5e120f6e 364
809e9447
ML
365 f = nouveau_local_fence(fence, chan->drm);
366 if (f)
367 prev = f->channel;
368
369 if (!prev || (prev != chan && (ret = fctx->sync(f, prev, chan))))
e3be4c23 370 ret = fence_wait(fence, intr);
5e120f6e 371
29ba89b2 372 return ret;
809e9447 373 }
5e120f6e 374
809e9447 375 if (!exclusive || !fobj)
29ba89b2
ML
376 return ret;
377
378 for (i = 0; i < fobj->shared_count && !ret; ++i) {
809e9447
ML
379 struct nouveau_channel *prev = NULL;
380
29ba89b2
ML
381 fence = rcu_dereference_protected(fobj->shared[i],
382 reservation_object_held(resv));
383
809e9447
ML
384 f = nouveau_local_fence(fence, chan->drm);
385 if (f)
386 prev = f->channel;
387
e3be4c23
ML
388 if (!prev || (prev != chan && (ret = fctx->sync(f, prev, chan))))
389 ret = fence_wait(fence, intr);
809e9447
ML
390
391 if (ret)
392 break;
29ba89b2
ML
393 }
394
395 return ret;
d375e7d5
BS
396}
397
398void
399nouveau_fence_unref(struct nouveau_fence **pfence)
400{
401 if (*pfence)
29ba89b2 402 fence_put(&(*pfence)->base);
d375e7d5
BS
403 *pfence = NULL;
404}
405
d375e7d5 406int
264ce192
BS
407nouveau_fence_new(struct nouveau_channel *chan, bool sysmem,
408 struct nouveau_fence **pfence)
d375e7d5
BS
409{
410 struct nouveau_fence *fence;
411 int ret = 0;
6ee73861 412
e193b1d4 413 if (unlikely(!chan->fence))
5e120f6e
BS
414 return -ENODEV;
415
d375e7d5
BS
416 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
417 if (!fence)
418 return -ENOMEM;
264ce192
BS
419
420 fence->sysmem = sysmem;
d375e7d5 421
b5d8f052
CD
422 ret = nouveau_fence_emit(fence, chan);
423 if (ret)
424 nouveau_fence_unref(&fence);
d375e7d5
BS
425
426 *pfence = fence;
6ee73861
BS
427 return ret;
428}
29ba89b2
ML
429
430static const char *nouveau_fence_get_get_driver_name(struct fence *fence)
431{
432 return "nouveau";
433}
434
435static const char *nouveau_fence_get_timeline_name(struct fence *f)
436{
437 struct nouveau_fence *fence = from_fence(f);
438 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
439
440 return fence->channel ? fctx->name : "dead channel";
441}
442
443/*
444 * In an ideal world, read would not assume the channel context is still alive.
445 * This function may be called from another device, running into free memory as a
446 * result. The drm node should still be there, so we can derive the index from
447 * the fence context.
448 */
449static bool nouveau_fence_is_signaled(struct fence *f)
450{
451 struct nouveau_fence *fence = from_fence(f);
452 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
453 struct nouveau_channel *chan = fence->channel;
454
455 return (int)(fctx->read(chan) - fence->base.seqno) >= 0;
456}
457
458static bool nouveau_fence_no_signaling(struct fence *f)
459{
460 struct nouveau_fence *fence = from_fence(f);
461
462 /*
463 * caller should have a reference on the fence,
464 * else fence could get freed here
465 */
466 WARN_ON(atomic_read(&fence->base.refcount.refcount) <= 1);
467
468 /*
469 * This needs uevents to work correctly, but fence_add_callback relies on
470 * being able to enable signaling. It will still get signaled eventually,
471 * just not right away.
472 */
473 if (nouveau_fence_is_signaled(f)) {
474 list_del(&fence->head);
475
476 fence_put(&fence->base);
477 return false;
478 }
479
480 return true;
481}
482
483static const struct fence_ops nouveau_fence_ops_legacy = {
484 .get_driver_name = nouveau_fence_get_get_driver_name,
485 .get_timeline_name = nouveau_fence_get_timeline_name,
486 .enable_signaling = nouveau_fence_no_signaling,
487 .signaled = nouveau_fence_is_signaled,
488 .wait = nouveau_fence_wait_legacy,
489 .release = NULL
490};
491
492static bool nouveau_fence_enable_signaling(struct fence *f)
493{
494 struct nouveau_fence *fence = from_fence(f);
495 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
496 bool ret;
497
498 if (!fctx->notify_ref++)
499 nvif_notify_get(&fctx->notify);
500
501 ret = nouveau_fence_no_signaling(f);
502 if (ret)
503 set_bit(FENCE_FLAG_USER_BITS, &fence->base.flags);
504 else if (!--fctx->notify_ref)
505 nvif_notify_put(&fctx->notify);
506
507 return ret;
508}
509
510static const struct fence_ops nouveau_fence_ops_uevent = {
511 .get_driver_name = nouveau_fence_get_get_driver_name,
512 .get_timeline_name = nouveau_fence_get_timeline_name,
513 .enable_signaling = nouveau_fence_enable_signaling,
514 .signaled = nouveau_fence_is_signaled,
515 .wait = fence_default_wait,
516 .release = NULL
517};
This page took 0.31437 seconds and 5 git commands to generate.