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/dma_fence.h>
32 
33 #include <nvif/cl826e.h>
34 #include <nvif/notify.h>
35 #include <nvif/event.h>
36 
37 #include "nouveau_drv.h"
38 #include "nouveau_dma.h"
39 #include "nouveau_fence.h"
40 
41 static const struct dma_fence_ops nouveau_fence_ops_uevent;
42 static const struct dma_fence_ops nouveau_fence_ops_legacy;
43 
44 static inline struct nouveau_fence *
45 from_fence(struct dma_fence *fence)
46 {
47 	return container_of(fence, struct nouveau_fence, base);
48 }
49 
50 static inline struct nouveau_fence_chan *
51 nouveau_fctx(struct nouveau_fence *fence)
52 {
53 	return container_of(fence->base.lock, struct nouveau_fence_chan, lock);
54 }
55 
56 static int
57 nouveau_fence_signal(struct nouveau_fence *fence)
58 {
59 	int drop = 0;
60 
61 	dma_fence_signal_locked(&fence->base);
62 	list_del(&fence->head);
63 	rcu_assign_pointer(fence->channel, NULL);
64 
65 	if (test_bit(DMA_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 	dma_fence_put(&fence->base);
73 	return drop;
74 }
75 
76 static struct nouveau_fence *
77 nouveau_local_fence(struct dma_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 
91 void
92 nouveau_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 
115 static void
116 nouveau_fence_context_put(struct kref *fence_ref)
117 {
118 	kfree(container_of(fence_ref, struct nouveau_fence_chan, fence_ref));
119 }
120 
121 void
122 nouveau_fence_context_free(struct nouveau_fence_chan *fctx)
123 {
124 	kref_put(&fctx->fence_ref, nouveau_fence_context_put);
125 }
126 
127 static int
128 nouveau_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 
146 static int
147 nouveau_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 
169 void
170 nouveau_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, NV826E_V0_NTFY_NON_STALL_INTERRUPT,
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 
202 int
203 nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
204 {
205 	struct nouveau_fence_chan *fctx = chan->fence;
206 	struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
207 	int ret;
208 
209 	fence->channel  = chan;
210 	fence->timeout  = jiffies + (15 * HZ);
211 
212 	if (priv->uevent)
213 		dma_fence_init(&fence->base, &nouveau_fence_ops_uevent,
214 			       &fctx->lock, fctx->context, ++fctx->sequence);
215 	else
216 		dma_fence_init(&fence->base, &nouveau_fence_ops_legacy,
217 			       &fctx->lock, fctx->context, ++fctx->sequence);
218 	kref_get(&fctx->fence_ref);
219 
220 	trace_dma_fence_emit(&fence->base);
221 	ret = fctx->emit(fence);
222 	if (!ret) {
223 		dma_fence_get(&fence->base);
224 		spin_lock_irq(&fctx->lock);
225 
226 		if (nouveau_fence_update(chan, fctx))
227 			nvif_notify_put(&fctx->notify);
228 
229 		list_add_tail(&fence->head, &fctx->pending);
230 		spin_unlock_irq(&fctx->lock);
231 	}
232 
233 	return ret;
234 }
235 
236 bool
237 nouveau_fence_done(struct nouveau_fence *fence)
238 {
239 	if (fence->base.ops == &nouveau_fence_ops_legacy ||
240 	    fence->base.ops == &nouveau_fence_ops_uevent) {
241 		struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
242 		struct nouveau_channel *chan;
243 		unsigned long flags;
244 
245 		if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
246 			return true;
247 
248 		spin_lock_irqsave(&fctx->lock, flags);
249 		chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock));
250 		if (chan && nouveau_fence_update(chan, fctx))
251 			nvif_notify_put(&fctx->notify);
252 		spin_unlock_irqrestore(&fctx->lock, flags);
253 	}
254 	return dma_fence_is_signaled(&fence->base);
255 }
256 
257 static long
258 nouveau_fence_wait_legacy(struct dma_fence *f, bool intr, long wait)
259 {
260 	struct nouveau_fence *fence = from_fence(f);
261 	unsigned long sleep_time = NSEC_PER_MSEC / 1000;
262 	unsigned long t = jiffies, timeout = t + wait;
263 
264 	while (!nouveau_fence_done(fence)) {
265 		ktime_t kt;
266 
267 		t = jiffies;
268 
269 		if (wait != MAX_SCHEDULE_TIMEOUT && time_after_eq(t, timeout)) {
270 			__set_current_state(TASK_RUNNING);
271 			return 0;
272 		}
273 
274 		__set_current_state(intr ? TASK_INTERRUPTIBLE :
275 					   TASK_UNINTERRUPTIBLE);
276 
277 		kt = sleep_time;
278 		schedule_hrtimeout(&kt, HRTIMER_MODE_REL);
279 		sleep_time *= 2;
280 		if (sleep_time > NSEC_PER_MSEC)
281 			sleep_time = NSEC_PER_MSEC;
282 
283 		if (intr && signal_pending(current))
284 			return -ERESTARTSYS;
285 	}
286 
287 	__set_current_state(TASK_RUNNING);
288 
289 	return timeout - t;
290 }
291 
292 static int
293 nouveau_fence_wait_busy(struct nouveau_fence *fence, bool intr)
294 {
295 	int ret = 0;
296 
297 	while (!nouveau_fence_done(fence)) {
298 		if (time_after_eq(jiffies, fence->timeout)) {
299 			ret = -EBUSY;
300 			break;
301 		}
302 
303 		__set_current_state(intr ?
304 				    TASK_INTERRUPTIBLE :
305 				    TASK_UNINTERRUPTIBLE);
306 
307 		if (intr && signal_pending(current)) {
308 			ret = -ERESTARTSYS;
309 			break;
310 		}
311 	}
312 
313 	__set_current_state(TASK_RUNNING);
314 	return ret;
315 }
316 
317 int
318 nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
319 {
320 	long ret;
321 
322 	if (!lazy)
323 		return nouveau_fence_wait_busy(fence, intr);
324 
325 	ret = dma_fence_wait_timeout(&fence->base, intr, 15 * HZ);
326 	if (ret < 0)
327 		return ret;
328 	else if (!ret)
329 		return -EBUSY;
330 	else
331 		return 0;
332 }
333 
334 int
335 nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, bool exclusive, bool intr)
336 {
337 	struct nouveau_fence_chan *fctx = chan->fence;
338 	struct dma_fence *fence;
339 	struct reservation_object *resv = nvbo->bo.resv;
340 	struct reservation_object_list *fobj;
341 	struct nouveau_fence *f;
342 	int ret = 0, i;
343 
344 	if (!exclusive) {
345 		ret = reservation_object_reserve_shared(resv);
346 
347 		if (ret)
348 			return ret;
349 	}
350 
351 	fobj = reservation_object_get_list(resv);
352 	fence = reservation_object_get_excl(resv);
353 
354 	if (fence && (!exclusive || !fobj || !fobj->shared_count)) {
355 		struct nouveau_channel *prev = NULL;
356 		bool must_wait = true;
357 
358 		f = nouveau_local_fence(fence, chan->drm);
359 		if (f) {
360 			rcu_read_lock();
361 			prev = rcu_dereference(f->channel);
362 			if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0))
363 				must_wait = false;
364 			rcu_read_unlock();
365 		}
366 
367 		if (must_wait)
368 			ret = dma_fence_wait(fence, intr);
369 
370 		return ret;
371 	}
372 
373 	if (!exclusive || !fobj)
374 		return ret;
375 
376 	for (i = 0; i < fobj->shared_count && !ret; ++i) {
377 		struct nouveau_channel *prev = NULL;
378 		bool must_wait = true;
379 
380 		fence = rcu_dereference_protected(fobj->shared[i],
381 						reservation_object_held(resv));
382 
383 		f = nouveau_local_fence(fence, chan->drm);
384 		if (f) {
385 			rcu_read_lock();
386 			prev = rcu_dereference(f->channel);
387 			if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0))
388 				must_wait = false;
389 			rcu_read_unlock();
390 		}
391 
392 		if (must_wait)
393 			ret = dma_fence_wait(fence, intr);
394 	}
395 
396 	return ret;
397 }
398 
399 void
400 nouveau_fence_unref(struct nouveau_fence **pfence)
401 {
402 	if (*pfence)
403 		dma_fence_put(&(*pfence)->base);
404 	*pfence = NULL;
405 }
406 
407 int
408 nouveau_fence_new(struct nouveau_channel *chan, bool sysmem,
409 		  struct nouveau_fence **pfence)
410 {
411 	struct nouveau_fence *fence;
412 	int ret = 0;
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 	ret = nouveau_fence_emit(fence, chan);
422 	if (ret)
423 		nouveau_fence_unref(&fence);
424 
425 	*pfence = fence;
426 	return ret;
427 }
428 
429 static const char *nouveau_fence_get_get_driver_name(struct dma_fence *fence)
430 {
431 	return "nouveau";
432 }
433 
434 static const char *nouveau_fence_get_timeline_name(struct dma_fence *f)
435 {
436 	struct nouveau_fence *fence = from_fence(f);
437 	struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
438 
439 	return !fctx->dead ? fctx->name : "dead channel";
440 }
441 
442 /*
443  * In an ideal world, read would not assume the channel context is still alive.
444  * This function may be called from another device, running into free memory as a
445  * result. The drm node should still be there, so we can derive the index from
446  * the fence context.
447  */
448 static bool nouveau_fence_is_signaled(struct dma_fence *f)
449 {
450 	struct nouveau_fence *fence = from_fence(f);
451 	struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
452 	struct nouveau_channel *chan;
453 	bool ret = false;
454 
455 	rcu_read_lock();
456 	chan = rcu_dereference(fence->channel);
457 	if (chan)
458 		ret = (int)(fctx->read(chan) - fence->base.seqno) >= 0;
459 	rcu_read_unlock();
460 
461 	return ret;
462 }
463 
464 static bool nouveau_fence_no_signaling(struct dma_fence *f)
465 {
466 	struct nouveau_fence *fence = from_fence(f);
467 
468 	/*
469 	 * caller should have a reference on the fence,
470 	 * else fence could get freed here
471 	 */
472 	WARN_ON(kref_read(&fence->base.refcount) <= 1);
473 
474 	/*
475 	 * This needs uevents to work correctly, but dma_fence_add_callback relies on
476 	 * being able to enable signaling. It will still get signaled eventually,
477 	 * just not right away.
478 	 */
479 	if (nouveau_fence_is_signaled(f)) {
480 		list_del(&fence->head);
481 
482 		dma_fence_put(&fence->base);
483 		return false;
484 	}
485 
486 	return true;
487 }
488 
489 static void nouveau_fence_release(struct dma_fence *f)
490 {
491 	struct nouveau_fence *fence = from_fence(f);
492 	struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
493 
494 	kref_put(&fctx->fence_ref, nouveau_fence_context_put);
495 	dma_fence_free(&fence->base);
496 }
497 
498 static const struct dma_fence_ops nouveau_fence_ops_legacy = {
499 	.get_driver_name = nouveau_fence_get_get_driver_name,
500 	.get_timeline_name = nouveau_fence_get_timeline_name,
501 	.enable_signaling = nouveau_fence_no_signaling,
502 	.signaled = nouveau_fence_is_signaled,
503 	.wait = nouveau_fence_wait_legacy,
504 	.release = nouveau_fence_release
505 };
506 
507 static bool nouveau_fence_enable_signaling(struct dma_fence *f)
508 {
509 	struct nouveau_fence *fence = from_fence(f);
510 	struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
511 	bool ret;
512 
513 	if (!fctx->notify_ref++)
514 		nvif_notify_get(&fctx->notify);
515 
516 	ret = nouveau_fence_no_signaling(f);
517 	if (ret)
518 		set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags);
519 	else if (!--fctx->notify_ref)
520 		nvif_notify_put(&fctx->notify);
521 
522 	return ret;
523 }
524 
525 static const struct dma_fence_ops nouveau_fence_ops_uevent = {
526 	.get_driver_name = nouveau_fence_get_get_driver_name,
527 	.get_timeline_name = nouveau_fence_get_timeline_name,
528 	.enable_signaling = nouveau_fence_enable_signaling,
529 	.signaled = nouveau_fence_is_signaled,
530 	.wait = dma_fence_default_wait,
531 	.release = nouveau_fence_release
532 };
533