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