summaryrefslogtreecommitdiff
path: root/linux-core/ttm/ttm_fence.c
blob: 2f61fadd5688f2ad05efabd7bc891f4c2b655f45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/**************************************************************************
 *
 * Copyright (c) 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
 * All Rights Reserved.
 * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/
/*
 * Authors: Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
 */

#include "ttm/ttm_fence_api.h"
#include "ttm/ttm_fence_driver.h"
#include <linux/wait.h>
#include <linux/sched.h>

#include "drmP.h"

/*
 * Simple implementation for now.
 */

static void ttm_fence_lockup(struct ttm_fence_object *fence, uint32_t mask)
{
	struct ttm_fence_class_manager *fc = ttm_fence_fc(fence);

	printk(KERN_ERR "GPU lockup dectected on engine %u "
	       "fence type 0x%08x\n",
	       (unsigned int)fence->fence_class, (unsigned int)mask);
	/*
	 * Give engines some time to idle?
	 */

	write_lock(&fc->lock);
	ttm_fence_handler(fence->fdev, fence->fence_class,
			  fence->sequence, mask, -EBUSY);
	write_unlock(&fc->lock);
}

/*
 * Convenience function to be called by fence::wait methods that
 * need polling.
 */

int ttm_fence_wait_polling(struct ttm_fence_object *fence, bool lazy,
			   bool interruptible, uint32_t mask)
{
	struct ttm_fence_class_manager *fc = ttm_fence_fc(fence);
	const struct ttm_fence_driver *driver = ttm_fence_driver(fence);
	uint32_t count = 0;
	int ret;
	unsigned long end_jiffies = fence->timeout_jiffies;

	DECLARE_WAITQUEUE(entry, current);
	add_wait_queue(&fc->fence_queue, &entry);

	ret = 0;

	for (;;) {
		__set_current_state((interruptible) ?
				    TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
		if (ttm_fence_object_signaled(fence, mask))
			break;
		if (time_after_eq(jiffies, end_jiffies)) {
			if (driver->lockup)
				driver->lockup(fence, mask);
			else
				ttm_fence_lockup(fence, mask);
			continue;
		}
		if (lazy)
			schedule_timeout(1);
		else if ((++count & 0x0F) == 0) {
			__set_current_state(TASK_RUNNING);
			schedule();
			__set_current_state((interruptible) ?
					    TASK_INTERRUPTIBLE :
					    TASK_UNINTERRUPTIBLE);
		}
		if (interruptible && signal_pending(current)) {
			ret = -ERESTART;
			break;
		}
	}
	__set_current_state(TASK_RUNNING);
	remove_wait_queue(&fc->fence_queue, &entry);
	return ret;
}

/*
 * Typically called by the IRQ handler.
 */

void ttm_fence_handler(struct ttm_fence_device *fdev, uint32_t fence_class,
		       uint32_t sequence, uint32_t type, uint32_t error)
{
	int wake = 0;
	uint32_t diff;
	uint32_t relevant_type;
	uint32_t new_type;
	struct ttm_fence_class_manager *fc = &fdev->fence_class[fence_class];
	const struct ttm_fence_driver *driver = ttm_fence_driver_from_dev(fdev);
	struct list_head *head;
	struct ttm_fence_object *fence, *next;
	bool found = false;

	if (list_empty(&fc->ring))
		return;

	list_for_each_entry(fence, &fc->ring, ring) {
		diff = (sequence - fence->sequence) & fc->sequence_mask;
		if (diff > fc->wrap_diff) {
			found = true;
			break;
		}
	}

	fc->waiting_types &= ~type;
	head = (found) ? &fence->ring : &fc->ring;

	list_for_each_entry_safe_reverse(fence, next, head, ring) {
		if (&fence->ring == &fc->ring)
			break;

		DRM_DEBUG("Fence 0x%08lx, sequence 0x%08x, type 0x%08x\n",
			  (unsigned long)fence, fence->sequence,
			  fence->fence_type);

		if (error) {
			fence->info.error = error;
			fence->info.signaled_types = fence->fence_type;
			list_del_init(&fence->ring);
			wake = 1;
			break;
		}

		relevant_type = type & fence->fence_type;
		new_type = (fence->info.signaled_types | relevant_type) ^
		    fence->info.signaled_types;

		if (new_type) {
			fence->info.signaled_types |= new_type;
			DRM_DEBUG("Fence 0x%08lx signaled 0x%08x\n",
				  (unsigned long)fence,
				  fence->info.signaled_types);

			if (unlikely(driver->signaled))
				driver->signaled(fence);

			if (driver->needed_flush)
				fc->pending_flush |=
				    driver->needed_flush(fence);

			if (new_type & fence->waiting_types)
				wake = 1;
		}

		fc->waiting_types |=
		    fence->waiting_types & ~fence->info.signaled_types;

		if (!(fence->fence_type & ~fence->info.signaled_types)) {
			DRM_DEBUG("Fence completely signaled 0x%08lx\n",
				  (unsigned long)fence);
			list_del_init(&fence->ring);
		}
	}

	/*
	 * Reinstate lost waiting types.
	 */

	if ((fc->waiting_types & type) != type) {
		head = head->prev;
		list_for_each_entry(fence, head, ring) {
			if (&fence->ring == &fc->ring)
				break;
			diff =
			    (fc->highest_waiting_sequence -
			     fence->sequence) & fc->sequence_mask;
			if (diff > fc->wrap_diff)
				break;

			fc->waiting_types |=
			    fence->waiting_types & ~fence->info.signaled_types;
		}
	}

	if (wake)
		wake_up_all(&fc->fence_queue);
}

static void ttm_fence_unring(struct ttm_fence_object *fence)
{
	struct ttm_fence_class_manager *fc = ttm_fence_fc(fence);
	unsigned long irq_flags;

	write_lock_irqsave(&fc->lock, irq_flags);
	list_del_init(&fence->ring);
	write_unlock_irqrestore(&fc->lock, irq_flags);
}

bool ttm_fence_object_signaled(struct ttm_fence_object *fence, uint32_t mask)
{
	unsigned long flags;
	bool signaled;
	const struct ttm_fence_driver *driver = ttm_fence_driver(fence);
	struct ttm_fence_class_manager *fc = ttm_fence_fc(fence);

	mask &= fence->fence_type;
	read_lock_irqsave(&fc->lock, flags);
	signaled = (mask & fence->info.signaled_types) == mask;
	read_unlock_irqrestore(&fc->lock, flags);
	if (!signaled && driver->poll) {
		write_lock_irqsave(&fc->lock, flags);
		driver->poll(fence->fdev, fence->fence_class, mask);
		signaled = (mask & fence->info.signaled_types) == mask;
		write_unlock_irqrestore(&fc->lock, flags);
	}
	return signaled;
}

int ttm_fence_object_flush(struct ttm_fence_object *fence, uint32_t type)
{
	const struct ttm_fence_driver *driver = ttm_fence_driver(fence);
	struct ttm_fence_class_manager *fc = ttm_fence_fc(fence);
	unsigned long irq_flags;
	uint32_t saved_pending_flush;
	uint32_t diff;
	bool call_flush;

	if (type & ~fence->fence_type) {
		DRM_ERROR("Flush trying to extend fence type, "
			  "0x%x, 0x%x\n", type, fence->fence_type);
		return -EINVAL;
	}

	write_lock_irqsave(&fc->lock, irq_flags);
	fence->waiting_types |= type;
	fc->waiting_types |= fence->waiting_types;
	diff = (fence->sequence - fc->highest_waiting_sequence) &
	    fc->sequence_mask;

	if (diff < fc->wrap_diff)
		fc->highest_waiting_sequence = fence->sequence;

	/*
	 * fence->waiting_types has changed. Determine whether
	 * we need to initiate some kind of flush as a result of this.
	 */

	saved_pending_flush = fc->pending_flush;
	if (driver->needed_flush)
		fc->pending_flush |= driver->needed_flush(fence);

	if (driver->poll)
		driver->poll(fence->fdev, fence->fence_class,
			     fence->waiting_types);

	call_flush = (fc->pending_flush != 0);
	write_unlock_irqrestore(&fc->lock, irq_flags);

	if (call_flush && driver->flush)
		driver->flush(fence->fdev, fence->fence_class);

	return 0;
}

/*
 * Make sure old fence objects are signaled before their fence sequences are
 * wrapped around and reused.
 */

void ttm_fence_flush_old(struct ttm_fence_device *fdev,
			 uint32_t fence_class, uint32_t sequence)
{
	struct ttm_fence_class_manager *fc = &fdev->fence_class[fence_class];
	struct ttm_fence_object *fence;
	unsigned long irq_flags;
	const struct ttm_fence_driver *driver = fdev->driver;
	bool call_flush;

	uint32_t diff;

	write_lock_irqsave(&fc->lock, irq_flags);

	list_for_each_entry_reverse(fence, &fc->ring, ring) {
		diff = (sequence - fence->sequence) & fc->sequence_mask;
		if (diff <= fc->flush_diff)
			break;

		fence->waiting_types = fence->fence_type;
		fc->waiting_types |= fence->fence_type;

		if (driver->needed_flush)
			fc->pending_flush |= driver->needed_flush(fence);
	}

	if (driver->poll)
		driver->poll(fdev, fence_class, fc->waiting_types);

	call_flush = (fc->pending_flush != 0);
	write_unlock_irqrestore(&fc->lock, irq_flags);

	if (call_flush && driver->flush)
		driver->flush(fdev, fence->fence_class);

	/*
	 * FIXME: Shold we implement a wait here for really old fences?
	 */

}

int ttm_fence_object_wait(struct ttm_fence_object *fence,
			  bool lazy, bool interruptible, uint32_t mask)
{
	const struct ttm_fence_driver *driver = ttm_fence_driver(fence);
	struct ttm_fence_class_manager *fc = ttm_fence_fc(fence);
	int ret = 0;
	unsigned long timeout;
	unsigned long cur_jiffies;
	unsigned long to_jiffies;

	if (mask & ~fence->fence_type) {
		DRM_ERROR("Wait trying to extend fence type"
			  " 0x%08x 0x%08x\n", mask, fence->fence_type);
		BUG();
		return -EINVAL;
	}

	if (driver->wait)
		return driver->wait(fence, lazy, interruptible, mask);

	ttm_fence_object_flush(fence, mask);
      retry:
	if (!driver->has_irq ||
	    driver->has_irq(fence->fdev, fence->fence_class, mask)) {

		cur_jiffies = jiffies;
		to_jiffies = fence->timeout_jiffies;

		timeout = (time_after(to_jiffies, cur_jiffies)) ?
		    to_jiffies - cur_jiffies : 1;

		if (interruptible)
			ret = wait_event_interruptible_timeout
			    (fc->fence_queue,
			     ttm_fence_object_signaled(fence, mask), timeout);
		else
			ret = wait_event_timeout
			    (fc->fence_queue,
			     ttm_fence_object_signaled(fence, mask), timeout);

		if (unlikely(ret == -ERESTARTSYS))
			return -ERESTART;

		if (unlikely(ret == 0)) {
			if (driver->lockup)
				driver->lockup(fence, mask);
			else
				ttm_fence_lockup(fence, mask);
			goto retry;
		}

		return 0;
	}

	return ttm_fence_wait_polling(fence, lazy, interruptible, mask);
}

int ttm_fence_object_emit(struct ttm_fence_object *fence, uint32_t fence_flags,
			  uint32_t fence_class, uint32_t type)
{
	const struct ttm_fence_driver *driver = ttm_fence_driver(fence);
	struct ttm_fence_class_manager *fc = ttm_fence_fc(fence);
	unsigned long flags;
	uint32_t sequence;
	unsigned long timeout;
	int ret;

	ttm_fence_unring(fence);
	ret = driver->emit(fence->fdev,
			   fence_class, fence_flags, &sequence, &timeout);
	if (ret)
		return ret;

	write_lock_irqsave(&fc->lock, flags);
	fence->fence_class = fence_class;
	fence->fence_type = type;
	fence->waiting_types = 0;
	fence->info.signaled_types = 0;
	fence->info.error = 0;
	fence->sequence = sequence;
	fence->timeout_jiffies = timeout;
	if (list_empty(&fc->ring))
		fc->highest_waiting_sequence = sequence - 1;
	list_add_tail(&fence->ring, &fc->ring);
	fc->latest_queued_sequence = sequence;
	write_unlock_irqrestore(&fc->lock, flags);
	return 0;
}

int ttm_fence_object_init(struct ttm_fence_device *fdev,
			  uint32_t fence_class,
			  uint32_t type,
			  uint32_t create_flags,
			  void (*destroy) (struct ttm_fence_object *),
			  struct ttm_fence_object *fence)
{
	int ret = 0;

	kref_init(&fence->kref);
	fence->fence_class = fence_class;
	fence->fence_type = type;
	fence->info.signaled_types = 0;
	fence->waiting_types = 0;
	fence->sequence = 0;
	fence->info.error = 0;
	fence->fdev = fdev;
	fence->destroy = destroy;
	INIT_LIST_HEAD(&fence->ring);
	atomic_inc(&fdev->count);

	if (create_flags & TTM_FENCE_FLAG_EMIT) {
		ret = ttm_fence_object_emit(fence, create_flags,
					    fence->fence_class, type);
	}

	return ret;
}

int ttm_fence_object_create(struct ttm_fence_device *fdev,
			    uint32_t fence_class,
			    uint32_t type,
			    uint32_t create_flags,
			    struct ttm_fence_object **c_fence)
{
	struct ttm_fence_object *fence;
	int ret;

	ret = ttm_mem_global_alloc(fdev->mem_glob, sizeof(*fence), false, false, false);
	if (unlikely(ret != 0)) {
		printk(KERN_ERR "Out of memory creating fence object\n");
		return ret;
	}

	fence = kmalloc(sizeof(*fence), GFP_KERNEL);
	if (!fence) {
		printk(KERN_ERR "Out of memory creating fence object\n");
		ttm_mem_global_free(fdev->mem_glob, sizeof(*fence), false);
		return -ENOMEM;
	}

	ret = ttm_fence_object_init(fdev, fence_class, type,
				    create_flags, NULL, fence);
	if (ret) {
		ttm_fence_object_unref(&fence);
		return ret;
	}
	*c_fence = fence;

	return 0;
}

static void ttm_fence_object_destroy(struct kref *kref)
{
	struct ttm_fence_object *fence =
	    container_of(kref, struct ttm_fence_object, kref);
	struct ttm_fence_class_manager *fc = ttm_fence_fc(fence);
	unsigned long irq_flags;

	write_lock_irqsave(&fc->lock, irq_flags);
	list_del_init(&fence->ring);
	write_unlock_irqrestore(&fc->lock, irq_flags);

	atomic_dec(&fence->fdev->count);
	if (fence->destroy)
		fence->destroy(fence);
	else {
		ttm_mem_global_free(fence->fdev->mem_glob, sizeof(*fence), false);
		kfree(fence);
	}
}

void ttm_fence_device_release(struct ttm_fence_device *fdev)
{
	kfree(fdev->fence_class);
}

int
ttm_fence_device_init(int num_classes,
		      struct ttm_mem_global *mem_glob,
		      struct ttm_fence_device *fdev,
		      const struct ttm_fence_class_init *init,
		      bool replicate_init, const struct ttm_fence_driver *driver)
{
	struct ttm_fence_class_manager *fc;
	const struct ttm_fence_class_init *fci;
	int i;

	fdev->mem_glob = mem_glob;
	fdev->fence_class = kzalloc(num_classes *
				    sizeof(*fdev->fence_class), GFP_KERNEL);

	if (unlikely(!fdev->fence_class))
		return -ENOMEM;

	fdev->num_classes = num_classes;
	atomic_set(&fdev->count, 0);
	fdev->driver = driver;

	for (i = 0; i < fdev->num_classes; ++i) {
		fc = &fdev->fence_class[i];
		fci = &init[(replicate_init) ? 0 : i];

		fc->wrap_diff = fci->wrap_diff;
		fc->flush_diff = fci->flush_diff;
		fc->sequence_mask = fci->sequence_mask;

		rwlock_init(&fc->lock);
		INIT_LIST_HEAD(&fc->ring);
		init_waitqueue_head(&fc->fence_queue);
	}

	return 0;
}

struct ttm_fence_info ttm_fence_get_info(struct ttm_fence_object *fence)
{
	struct ttm_fence_class_manager *fc = ttm_fence_fc(fence);
	struct ttm_fence_info tmp;
	unsigned long irq_flags;

	read_lock_irqsave(&fc->lock, irq_flags);
	tmp = fence->info;
	read_unlock_irqrestore(&fc->lock, irq_flags);

	return tmp;
}

void ttm_fence_object_unref(struct ttm_fence_object **p_fence)
{
	struct ttm_fence_object *fence = *p_fence;

	*p_fence = NULL;
	(void)kref_put(&fence->kref, &ttm_fence_object_destroy);
}

/*
 * Placement / BO sync object glue.
 */

bool ttm_fence_sync_obj_signaled(void *sync_obj, void *sync_arg)
{
	struct ttm_fence_object *fence = (struct ttm_fence_object *)sync_obj;
	uint32_t fence_types = (uint32_t) (unsigned long)sync_arg;

	return ttm_fence_object_signaled(fence, fence_types);
}

int ttm_fence_sync_obj_wait(void *sync_obj, void *sync_arg,
			    bool lazy, bool interruptible)
{
	struct ttm_fence_object *fence = (struct ttm_fence_object *)sync_obj;
	uint32_t fence_types = (uint32_t) (unsigned long)sync_arg;

	return ttm_fence_object_wait(fence, lazy, interruptible, fence_types);
}

int ttm_fence_sync_obj_flush(void *sync_obj, void *sync_arg)
{
	struct ttm_fence_object *fence = (struct ttm_fence_object *)sync_obj;
	uint32_t fence_types = (uint32_t) (unsigned long)sync_arg;

	return ttm_fence_object_flush(fence, fence_types);
}

void ttm_fence_sync_obj_unref(void **sync_obj)
{
	ttm_fence_object_unref((struct ttm_fence_object **)sync_obj);
}

void *ttm_fence_sync_obj_ref(void *sync_obj)
{
	return (void *)
	    ttm_fence_object_ref((struct ttm_fence_object *)sync_obj);
}