Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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