radeon_fence.c 13.0 KB
Newer Older
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
/*
 * Copyright 2009 Jerome Glisse.
 * 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 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.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 */
/*
 * Authors:
 *    Jerome Glisse <glisse@freedesktop.org>
 *    Dave Airlie
 */
#include <linux/seq_file.h>
A
Arun Sharma 已提交
32
#include <linux/atomic.h>
33 34 35
#include <linux/wait.h>
#include <linux/list.h>
#include <linux/kref.h>
36
#include <linux/slab.h>
37 38 39 40
#include "drmP.h"
#include "drm.h"
#include "radeon_reg.h"
#include "radeon.h"
41
#include "radeon_trace.h"
42

43
static void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
44
{
45 46
	u32 scratch_index;

47 48
	if (rdev->wb.enabled) {
		if (rdev->wb.use_event)
49 50
			scratch_index = R600_WB_EVENT_OFFSET +
				rdev->fence_drv[ring].scratch_reg - rdev->scratch.reg_base;
51
		else
52 53
			scratch_index = RADEON_WB_SCRATCH_OFFSET +
				rdev->fence_drv[ring].scratch_reg - rdev->scratch.reg_base;
54
		rdev->wb.wb[scratch_index/4] = cpu_to_le32(seq);
55
	} else
56
		WREG32(rdev->fence_drv[ring].scratch_reg, seq);
57 58
}

59
static u32 radeon_fence_read(struct radeon_device *rdev, int ring)
60
{
61 62
	u32 seq = 0;
	u32 scratch_index;
63 64 65

	if (rdev->wb.enabled) {
		if (rdev->wb.use_event)
66 67
			scratch_index = R600_WB_EVENT_OFFSET +
				rdev->fence_drv[ring].scratch_reg - rdev->scratch.reg_base;
68
		else
69 70
			scratch_index = RADEON_WB_SCRATCH_OFFSET +
				rdev->fence_drv[ring].scratch_reg - rdev->scratch.reg_base;
71 72
		seq = le32_to_cpu(rdev->wb.wb[scratch_index/4]);
	} else
73
		seq = RREG32(rdev->fence_drv[ring].scratch_reg);
74 75 76
	return seq;
}

77 78 79 80
int radeon_fence_emit(struct radeon_device *rdev, struct radeon_fence *fence)
{
	unsigned long irq_flags;

81
	write_lock_irqsave(&rdev->fence_lock, irq_flags);
82
	if (fence->emitted) {
83
		write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
84 85
		return 0;
	}
86
	fence->seq = atomic_add_return(1, &rdev->fence_drv[fence->ring].seq);
87
	if (!rdev->cp.ready)
88 89 90
		/* FIXME: cp is not running assume everythings is done right
		 * away
		 */
91
		radeon_fence_write(rdev, fence->seq, fence->ring);
92
	else
93
		radeon_fence_ring_emit(rdev, fence);
94

95
	trace_radeon_fence_emit(rdev->ddev, fence->seq);
96
	fence->emitted = true;
97 98
	list_move_tail(&fence->list, &rdev->fence_drv[fence->ring].emitted);
	write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
99 100 101
	return 0;
}

102
static bool radeon_fence_poll_locked(struct radeon_device *rdev, int ring)
103 104 105 106 107
{
	struct radeon_fence *fence;
	struct list_head *i, *n;
	uint32_t seq;
	bool wake = false;
108
	unsigned long cjiffies;
109

110 111 112 113 114
	seq = radeon_fence_read(rdev, ring);
	if (seq != rdev->fence_drv[ring].last_seq) {
		rdev->fence_drv[ring].last_seq = seq;
		rdev->fence_drv[ring].last_jiffies = jiffies;
		rdev->fence_drv[ring].last_timeout = RADEON_FENCE_JIFFIES_TIMEOUT;
115 116
	} else {
		cjiffies = jiffies;
117 118 119
		if (time_after(cjiffies, rdev->fence_drv[ring].last_jiffies)) {
			cjiffies -= rdev->fence_drv[ring].last_jiffies;
			if (time_after(rdev->fence_drv[ring].last_timeout, cjiffies)) {
120
				/* update the timeout */
121
				rdev->fence_drv[ring].last_timeout -= cjiffies;
122 123 124 125
			} else {
				/* the 500ms timeout is elapsed we should test
				 * for GPU lockup
				 */
126
				rdev->fence_drv[ring].last_timeout = 1;
127 128 129 130 131
			}
		} else {
			/* wrap around update last jiffies, we will just wait
			 * a little longer
			 */
132
			rdev->fence_drv[ring].last_jiffies = cjiffies;
133 134 135
		}
		return false;
	}
136
	n = NULL;
137
	list_for_each(i, &rdev->fence_drv[ring].emitted) {
138 139 140 141 142 143 144 145 146 147 148
		fence = list_entry(i, struct radeon_fence, list);
		if (fence->seq == seq) {
			n = i;
			break;
		}
	}
	/* all fence previous to this one are considered as signaled */
	if (n) {
		i = n;
		do {
			n = i->prev;
149
			list_move_tail(i, &rdev->fence_drv[ring].signaled);
150 151 152
			fence = list_entry(i, struct radeon_fence, list);
			fence->signaled = true;
			i = n;
153
		} while (i != &rdev->fence_drv[ring].emitted);
154 155 156 157 158 159 160 161 162 163 164
		wake = true;
	}
	return wake;
}

static void radeon_fence_destroy(struct kref *kref)
{
	unsigned long irq_flags;
        struct radeon_fence *fence;

	fence = container_of(kref, struct radeon_fence, kref);
165
	write_lock_irqsave(&fence->rdev->fence_lock, irq_flags);
166
	list_del(&fence->list);
167
	fence->emitted = false;
168
	write_unlock_irqrestore(&fence->rdev->fence_lock, irq_flags);
169 170 171
	kfree(fence);
}

172 173 174
int radeon_fence_create(struct radeon_device *rdev,
			struct radeon_fence **fence,
			int ring)
175 176 177 178 179 180 181 182 183
{
	unsigned long irq_flags;

	*fence = kmalloc(sizeof(struct radeon_fence), GFP_KERNEL);
	if ((*fence) == NULL) {
		return -ENOMEM;
	}
	kref_init(&((*fence)->kref));
	(*fence)->rdev = rdev;
184
	(*fence)->emitted = false;
185 186
	(*fence)->signaled = false;
	(*fence)->seq = 0;
187
	(*fence)->ring = ring;
188 189
	INIT_LIST_HEAD(&(*fence)->list);

190 191 192
	write_lock_irqsave(&rdev->fence_lock, irq_flags);
	list_add_tail(&(*fence)->list, &rdev->fence_drv[ring].created);
	write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
193 194 195 196 197 198 199 200
	return 0;
}

bool radeon_fence_signaled(struct radeon_fence *fence)
{
	unsigned long irq_flags;
	bool signaled = false;

201
	if (!fence)
202
		return true;
203 204

	if (fence->rdev->gpu_lockup)
205
		return true;
206

207
	write_lock_irqsave(&fence->rdev->fence_lock, irq_flags);
208 209 210 211 212
	signaled = fence->signaled;
	/* if we are shuting down report all fence as signaled */
	if (fence->rdev->shutdown) {
		signaled = true;
	}
213 214
	if (!fence->emitted) {
		WARN(1, "Querying an unemitted fence : %p !\n", fence);
215 216 217
		signaled = true;
	}
	if (!signaled) {
218
		radeon_fence_poll_locked(fence->rdev, fence->ring);
219 220
		signaled = fence->signaled;
	}
221
	write_unlock_irqrestore(&fence->rdev->fence_lock, irq_flags);
222 223 224
	return signaled;
}

225
int radeon_fence_wait(struct radeon_fence *fence, bool intr)
226 227
{
	struct radeon_device *rdev;
228 229
	unsigned long irq_flags, timeout;
	u32 seq;
230 231 232 233 234 235 236 237 238 239
	int r;

	if (fence == NULL) {
		WARN(1, "Querying an invalid fence : %p !\n", fence);
		return 0;
	}
	rdev = fence->rdev;
	if (radeon_fence_signaled(fence)) {
		return 0;
	}
240
	timeout = rdev->fence_drv[fence->ring].last_timeout;
241
retry:
242
	/* save current sequence used to check for GPU lockup */
243
	seq = rdev->fence_drv[fence->ring].last_seq;
244
	trace_radeon_fence_wait_begin(rdev->ddev, seq);
245
	if (intr) {
246
		radeon_irq_kms_sw_irq_get(rdev);
247
		r = wait_event_interruptible_timeout(rdev->fence_drv[fence->ring].queue,
248
				radeon_fence_signaled(fence), timeout);
249
		radeon_irq_kms_sw_irq_put(rdev);
250
		if (unlikely(r < 0)) {
251
			return r;
252
		}
253
	} else {
254
		radeon_irq_kms_sw_irq_get(rdev);
255
		r = wait_event_timeout(rdev->fence_drv[fence->ring].queue,
256
			 radeon_fence_signaled(fence), timeout);
257
		radeon_irq_kms_sw_irq_put(rdev);
258
	}
259
	trace_radeon_fence_wait_end(rdev->ddev, seq);
260
	if (unlikely(!radeon_fence_signaled(fence))) {
261 262 263 264 265 266
		/* we were interrupted for some reason and fence isn't
		 * isn't signaled yet, resume wait
		 */
		if (r) {
			timeout = r;
			goto retry;
267
		}
268
		/* don't protect read access to rdev->fence_drv[t].last_seq
269 270
		 * if we experiencing a lockup the value doesn't change
		 */
271 272
		if (seq == rdev->fence_drv[fence->ring].last_seq &&
		    radeon_gpu_is_lockup(rdev)) {
273
			/* good news we believe it's a lockup */
274
			printk(KERN_WARNING "GPU lockup (waiting for 0x%08X last fence id 0x%08X)\n",
J
Joe Perches 已提交
275
			     fence->seq, seq);
276 277 278
			/* FIXME: what should we do ? marking everyone
			 * as signaled for now
			 */
279 280 281 282
			rdev->gpu_lockup = true;
			r = radeon_gpu_reset(rdev);
			if (r)
				return r;
283
			radeon_fence_write(rdev, fence->seq, fence->ring);
284
			rdev->gpu_lockup = false;
285
		}
286
		timeout = RADEON_FENCE_JIFFIES_TIMEOUT;
287 288 289 290
		write_lock_irqsave(&rdev->fence_lock, irq_flags);
		rdev->fence_drv[fence->ring].last_timeout = RADEON_FENCE_JIFFIES_TIMEOUT;
		rdev->fence_drv[fence->ring].last_jiffies = jiffies;
		write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
291 292 293 294 295
		goto retry;
	}
	return 0;
}

296
int radeon_fence_wait_next(struct radeon_device *rdev, int ring)
297 298 299 300 301 302 303 304
{
	unsigned long irq_flags;
	struct radeon_fence *fence;
	int r;

	if (rdev->gpu_lockup) {
		return 0;
	}
305 306 307
	write_lock_irqsave(&rdev->fence_lock, irq_flags);
	if (list_empty(&rdev->fence_drv[ring].emitted)) {
		write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
308 309
		return 0;
	}
310
	fence = list_entry(rdev->fence_drv[ring].emitted.next,
311 312
			   struct radeon_fence, list);
	radeon_fence_ref(fence);
313
	write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
314 315 316 317 318
	r = radeon_fence_wait(fence, false);
	radeon_fence_unref(&fence);
	return r;
}

319
int radeon_fence_wait_last(struct radeon_device *rdev, int ring)
320 321 322 323 324 325 326 327
{
	unsigned long irq_flags;
	struct radeon_fence *fence;
	int r;

	if (rdev->gpu_lockup) {
		return 0;
	}
328 329 330
	write_lock_irqsave(&rdev->fence_lock, irq_flags);
	if (list_empty(&rdev->fence_drv[ring].emitted)) {
		write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
331 332
		return 0;
	}
333
	fence = list_entry(rdev->fence_drv[ring].emitted.prev,
334 335
			   struct radeon_fence, list);
	radeon_fence_ref(fence);
336
	write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	r = radeon_fence_wait(fence, false);
	radeon_fence_unref(&fence);
	return r;
}

struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
{
	kref_get(&fence->kref);
	return fence;
}

void radeon_fence_unref(struct radeon_fence **fence)
{
	struct radeon_fence *tmp = *fence;

	*fence = NULL;
	if (tmp) {
P
Paul Bolle 已提交
354
		kref_put(&tmp->kref, radeon_fence_destroy);
355 356 357
	}
}

358
void radeon_fence_process(struct radeon_device *rdev, int ring)
359 360 361 362
{
	unsigned long irq_flags;
	bool wake;

363 364 365
	write_lock_irqsave(&rdev->fence_lock, irq_flags);
	wake = radeon_fence_poll_locked(rdev, ring);
	write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
366
	if (wake) {
367
		wake_up_all(&rdev->fence_drv[ring].queue);
368 369 370
	}
}

371
int radeon_fence_driver_init(struct radeon_device *rdev, int num_rings)
372 373
{
	unsigned long irq_flags;
374
	int r, ring;
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
	for (ring = 0; ring < num_rings; ring++) {
		write_lock_irqsave(&rdev->fence_lock, irq_flags);
		r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
		if (r) {
			dev_err(rdev->dev, "fence failed to get scratch register\n");
			write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
			return r;
		}
		radeon_fence_write(rdev, 0, ring);
		atomic_set(&rdev->fence_drv[ring].seq, 0);
		INIT_LIST_HEAD(&rdev->fence_drv[ring].created);
		INIT_LIST_HEAD(&rdev->fence_drv[ring].emitted);
		INIT_LIST_HEAD(&rdev->fence_drv[ring].signaled);
		init_waitqueue_head(&rdev->fence_drv[ring].queue);
		rdev->fence_drv[ring].initialized = true;
		write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
	}
	for (ring = num_rings; ring < RADEON_NUM_RINGS; ring++) {
		write_lock_irqsave(&rdev->fence_lock, irq_flags);
		INIT_LIST_HEAD(&rdev->fence_drv[ring].created);
		INIT_LIST_HEAD(&rdev->fence_drv[ring].emitted);
		INIT_LIST_HEAD(&rdev->fence_drv[ring].signaled);
		rdev->fence_drv[ring].initialized = false;
		write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
400 401
	}
	if (radeon_debugfs_fence_init(rdev)) {
402
		dev_err(rdev->dev, "fence debugfs file creation failed\n");
403 404 405 406 407 408 409
	}
	return 0;
}

void radeon_fence_driver_fini(struct radeon_device *rdev)
{
	unsigned long irq_flags;
410 411 412 413 414 415 416 417 418 419 420
	int ring;

	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
		if (!rdev->fence_drv[ring].initialized)
			continue;
		wake_up_all(&rdev->fence_drv[ring].queue);
		write_lock_irqsave(&rdev->fence_lock, irq_flags);
		radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
		write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
		rdev->fence_drv[ring].initialized = false;
	}
421 422 423 424 425 426 427 428 429 430 431 432 433
}


/*
 * Fence debugfs
 */
#if defined(CONFIG_DEBUG_FS)
static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
{
	struct drm_info_node *node = (struct drm_info_node *)m->private;
	struct drm_device *dev = node->minor->dev;
	struct radeon_device *rdev = dev->dev_private;
	struct radeon_fence *fence;
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	int i;

	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
		if (!rdev->fence_drv[i].initialized)
			continue;

		seq_printf(m, "--- ring %d ---\n", i);
		seq_printf(m, "Last signaled fence 0x%08X\n",
			   radeon_fence_read(rdev, i));
		if (!list_empty(&rdev->fence_drv[i].emitted)) {
			fence = list_entry(rdev->fence_drv[i].emitted.prev,
					   struct radeon_fence, list);
			seq_printf(m, "Last emitted fence %p with 0x%08X\n",
				   fence,  fence->seq);
		}
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	}
	return 0;
}

static struct drm_info_list radeon_debugfs_fence_list[] = {
	{"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
};
#endif

int radeon_debugfs_fence_init(struct radeon_device *rdev)
{
#if defined(CONFIG_DEBUG_FS)
	return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 1);
#else
	return 0;
#endif
}