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