drm_lock.c 10.8 KB
Newer Older
L
Linus Torvalds 已提交
1
/**
D
Dave Airlie 已提交
2
 * \file drm_lock.c
L
Linus Torvalds 已提交
3
 * IOCTLs for locking
D
Dave Airlie 已提交
4
 *
L
Linus Torvalds 已提交
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
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Created: Tue Feb  2 08:37:54 1999 by faith@valinux.com
 *
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * 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, sublicense,
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS 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.
 */

#include "drmP.h"

D
Dave Airlie 已提交
38 39
static int drm_notifier(void *priv);

D
Dave Airlie 已提交
40
/**
L
Linus Torvalds 已提交
41 42 43
 * Lock ioctl.
 *
 * \param inode device inode.
44
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
45 46 47 48 49 50
 * \param cmd command.
 * \param arg user argument, pointing to a drm_lock structure.
 * \return zero on success or negative number on failure.
 *
 * Add the current task to the lock wait queue, and attempt to take to lock.
 */
51
int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
52
{
D
Dave Airlie 已提交
53
	DECLARE_WAITQUEUE(entry, current);
54
	struct drm_lock *lock = data;
55
	struct drm_master *master = file_priv->master;
D
Dave Airlie 已提交
56
	int ret = 0;
L
Linus Torvalds 已提交
57

58
	++file_priv->lock_count;
L
Linus Torvalds 已提交
59

60
	if (lock->context == DRM_KERNEL_CONTEXT) {
D
Dave Airlie 已提交
61
		DRM_ERROR("Process %d using kernel context %d\n",
62
			  task_pid_nr(current), lock->context);
D
Dave Airlie 已提交
63 64
		return -EINVAL;
	}
L
Linus Torvalds 已提交
65

D
Dave Airlie 已提交
66
	DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
67
		  lock->context, task_pid_nr(current),
68
		  master->lock.hw_lock->lock, lock->flags);
L
Linus Torvalds 已提交
69 70

	if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE))
71
		if (lock->context < 0)
L
Linus Torvalds 已提交
72 73
			return -EINVAL;

74 75 76 77 78
	add_wait_queue(&master->lock.lock_queue, &entry);
	spin_lock_bh(&master->lock.spinlock);
	master->lock.user_waiters++;
	spin_unlock_bh(&master->lock.spinlock);

L
Linus Torvalds 已提交
79 80
	for (;;) {
		__set_current_state(TASK_INTERRUPTIBLE);
81
		if (!master->lock.hw_lock) {
L
Linus Torvalds 已提交
82 83 84 85
			/* Device has been unregistered */
			ret = -EINTR;
			break;
		}
86 87 88
		if (drm_lock_take(&master->lock, lock->context)) {
			master->lock.file_priv = file_priv;
			master->lock.lock_time = jiffies;
D
Dave Airlie 已提交
89 90
			atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
			break;	/* Got lock */
L
Linus Torvalds 已提交
91
		}
D
Dave Airlie 已提交
92

L
Linus Torvalds 已提交
93 94
		/* Contention */
		schedule();
D
Dave Airlie 已提交
95
		if (signal_pending(current)) {
L
Linus Torvalds 已提交
96 97 98 99
			ret = -ERESTARTSYS;
			break;
		}
	}
100 101 102
	spin_lock_bh(&master->lock.spinlock);
	master->lock.user_waiters--;
	spin_unlock_bh(&master->lock.spinlock);
L
Linus Torvalds 已提交
103
	__set_current_state(TASK_RUNNING);
104
	remove_wait_queue(&master->lock.lock_queue, &entry);
L
Linus Torvalds 已提交
105

106 107
	DRM_DEBUG("%d %s\n", lock->context,
		  ret ? "interrupted" : "has lock");
108
	if (ret) return ret;
109

110 111 112
	/* don't set the block all signals on the master process for now 
	 * really probably not the correct answer but lets us debug xkb
 	 * xserver for now */
113
	if (!file_priv->is_master) {
114 115 116 117 118 119
		sigemptyset(&dev->sigmask);
		sigaddset(&dev->sigmask, SIGSTOP);
		sigaddset(&dev->sigmask, SIGTSTP);
		sigaddset(&dev->sigmask, SIGTTIN);
		sigaddset(&dev->sigmask, SIGTTOU);
		dev->sigdata.context = lock->context;
120
		dev->sigdata.lock = master->lock.hw_lock;
121 122
		block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask);
	}
D
Dave Airlie 已提交
123

124
	if (dev->driver->dma_ready && (lock->flags & _DRM_LOCK_READY))
L
Linus Torvalds 已提交
125
		dev->driver->dma_ready(dev);
D
Dave Airlie 已提交
126

127 128
	if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
	{
129
		if (dev->driver->dma_quiescent(dev)) {
130 131
			DRM_DEBUG("%d waiting for DMA quiescent\n",
				  lock->context);
E
Eric Anholt 已提交
132
			return -EBUSY;
133 134
		}
	}
D
Dave Airlie 已提交
135 136

	if (dev->driver->kernel_context_switch &&
137
	    dev->last_context != lock->context) {
D
Dave Airlie 已提交
138
		dev->driver->kernel_context_switch(dev, dev->last_context,
139
						   lock->context);
L
Linus Torvalds 已提交
140
	}
141

142
	return 0;
L
Linus Torvalds 已提交
143 144
}

D
Dave Airlie 已提交
145
/**
L
Linus Torvalds 已提交
146 147 148
 * Unlock ioctl.
 *
 * \param inode device inode.
149
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
150 151 152 153 154 155
 * \param cmd command.
 * \param arg user argument, pointing to a drm_lock structure.
 * \return zero on success or negative number on failure.
 *
 * Transfer and free the lock.
 */
156
int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
157
{
158
	struct drm_lock *lock = data;
159
	struct drm_master *master = file_priv->master;
L
Linus Torvalds 已提交
160

161
	if (lock->context == DRM_KERNEL_CONTEXT) {
D
Dave Airlie 已提交
162
		DRM_ERROR("Process %d using kernel context %d\n",
163
			  task_pid_nr(current), lock->context);
L
Linus Torvalds 已提交
164 165 166
		return -EINVAL;
	}

D
Dave Airlie 已提交
167
	atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
L
Linus Torvalds 已提交
168 169 170 171 172

	/* kernel_context_switch isn't used by any of the x86 drm
	 * modules but is required by the Sparc driver.
	 */
	if (dev->driver->kernel_context_switch_unlock)
173
		dev->driver->kernel_context_switch_unlock(dev);
L
Linus Torvalds 已提交
174
	else {
175
		if (drm_lock_free(&master->lock, lock->context)) {
176
			/* FIXME: Should really bail out here. */
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		}
	}

	unblock_all_signals();
	return 0;
}

/**
 * Take the heavyweight lock.
 *
 * \param lock lock pointer.
 * \param context locking context.
 * \return one if the lock is held, or zero otherwise.
 *
 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
 */
D
Dave Airlie 已提交
193
int drm_lock_take(struct drm_lock_data *lock_data,
194
		  unsigned int context)
L
Linus Torvalds 已提交
195 196
{
	unsigned int old, new, prev;
197
	volatile unsigned int *lock = &lock_data->hw_lock->lock;
L
Linus Torvalds 已提交
198

199
	spin_lock_bh(&lock_data->spinlock);
L
Linus Torvalds 已提交
200 201
	do {
		old = *lock;
D
Dave Airlie 已提交
202 203
		if (old & _DRM_LOCK_HELD)
			new = old | _DRM_LOCK_CONT;
204 205 206 207 208
		else {
			new = context | _DRM_LOCK_HELD |
				((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
				 _DRM_LOCK_CONT : 0);
		}
L
Linus Torvalds 已提交
209 210
		prev = cmpxchg(lock, old, new);
	} while (prev != old);
211
	spin_unlock_bh(&lock_data->spinlock);
212

L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221
	if (_DRM_LOCKING_CONTEXT(old) == context) {
		if (old & _DRM_LOCK_HELD) {
			if (context != DRM_KERNEL_CONTEXT) {
				DRM_ERROR("%d holds heavyweight lock\n",
					  context);
			}
			return 0;
		}
	}
222 223

	if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
D
Dave Airlie 已提交
224
		/* Have lock */
L
Linus Torvalds 已提交
225 226 227 228
		return 1;
	}
	return 0;
}
229
EXPORT_SYMBOL(drm_lock_take);
L
Linus Torvalds 已提交
230 231 232

/**
 * This takes a lock forcibly and hands it to context.	Should ONLY be used
D
Dave Airlie 已提交
233 234
 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
 *
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242
 * \param dev DRM device.
 * \param lock lock pointer.
 * \param context locking context.
 * \return always one.
 *
 * Resets the lock file pointer.
 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
 */
D
Dave Airlie 已提交
243
static int drm_lock_transfer(struct drm_lock_data *lock_data,
D
Dave Airlie 已提交
244
			     unsigned int context)
L
Linus Torvalds 已提交
245 246
{
	unsigned int old, new, prev;
247
	volatile unsigned int *lock = &lock_data->hw_lock->lock;
L
Linus Torvalds 已提交
248

249
	lock_data->file_priv = NULL;
L
Linus Torvalds 已提交
250
	do {
D
Dave Airlie 已提交
251 252
		old = *lock;
		new = context | _DRM_LOCK_HELD;
L
Linus Torvalds 已提交
253 254 255 256 257 258 259
		prev = cmpxchg(lock, old, new);
	} while (prev != old);
	return 1;
}

/**
 * Free lock.
D
Dave Airlie 已提交
260
 *
L
Linus Torvalds 已提交
261 262 263
 * \param dev DRM device.
 * \param lock lock.
 * \param context context.
D
Dave Airlie 已提交
264
 *
L
Linus Torvalds 已提交
265 266 267 268
 * Resets the lock file pointer.
 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
 * waiting on the lock queue.
 */
D
Dave Airlie 已提交
269
int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
L
Linus Torvalds 已提交
270 271
{
	unsigned int old, new, prev;
272 273
	volatile unsigned int *lock = &lock_data->hw_lock->lock;

274
	spin_lock_bh(&lock_data->spinlock);
275 276 277
	if (lock_data->kernel_waiters != 0) {
		drm_lock_transfer(lock_data, 0);
		lock_data->idle_has_lock = 1;
278
		spin_unlock_bh(&lock_data->spinlock);
279 280
		return 1;
	}
281
	spin_unlock_bh(&lock_data->spinlock);
L
Linus Torvalds 已提交
282 283

	do {
D
Dave Airlie 已提交
284
		old = *lock;
285
		new = _DRM_LOCKING_CONTEXT(old);
L
Linus Torvalds 已提交
286 287
		prev = cmpxchg(lock, old, new);
	} while (prev != old);
288

L
Linus Torvalds 已提交
289 290
	if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
		DRM_ERROR("%d freed heavyweight lock held by %d\n",
D
Dave Airlie 已提交
291
			  context, _DRM_LOCKING_CONTEXT(old));
L
Linus Torvalds 已提交
292 293
		return 1;
	}
294
	wake_up_interruptible(&lock_data->lock_queue);
L
Linus Torvalds 已提交
295 296
	return 0;
}
297
EXPORT_SYMBOL(drm_lock_free);
L
Linus Torvalds 已提交
298 299 300 301 302 303 304 305 306 307 308 309

/**
 * If we get here, it means that the process has called DRM_IOCTL_LOCK
 * without calling DRM_IOCTL_UNLOCK.
 *
 * If the lock is not held, then let the signal proceed as usual.  If the lock
 * is held, then set the contended flag and keep the signal blocked.
 *
 * \param priv pointer to a drm_sigdata structure.
 * \return one if the signal should be delivered normally, or zero if the
 * signal should be blocked.
 */
D
Dave Airlie 已提交
310
static int drm_notifier(void *priv)
L
Linus Torvalds 已提交
311
{
D
Dave Airlie 已提交
312
	struct drm_sigdata *s = (struct drm_sigdata *) priv;
D
Dave Airlie 已提交
313
	unsigned int old, new, prev;
L
Linus Torvalds 已提交
314

D
Dave Airlie 已提交
315
	/* Allow signal delivery if lock isn't held */
L
Linus Torvalds 已提交
316
	if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
D
Dave Airlie 已提交
317 318
	    || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
		return 1;
L
Linus Torvalds 已提交
319

D
Dave Airlie 已提交
320 321
	/* Otherwise, set flag to force call to
	   drmUnlock */
L
Linus Torvalds 已提交
322
	do {
D
Dave Airlie 已提交
323 324
		old = s->lock->lock;
		new = old | _DRM_LOCK_CONT;
L
Linus Torvalds 已提交
325 326 327 328
		prev = cmpxchg(&s->lock->lock, old, new);
	} while (prev != old);
	return 0;
}
329 330 331 332 333 334 335 336 337 338 339 340 341 342

/**
 * This function returns immediately and takes the hw lock
 * with the kernel context if it is free, otherwise it gets the highest priority when and if
 * it is eventually released.
 *
 * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
 * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
 * a deadlock, which is why the "idlelock" was invented).
 *
 * This should be sufficient to wait for GPU idle without
 * having to worry about starvation.
 */

D
Dave Airlie 已提交
343
void drm_idlelock_take(struct drm_lock_data *lock_data)
344 345 346
{
	int ret = 0;

347
	spin_lock_bh(&lock_data->spinlock);
348 349 350
	lock_data->kernel_waiters++;
	if (!lock_data->idle_has_lock) {

351
		spin_unlock_bh(&lock_data->spinlock);
352
		ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
353
		spin_lock_bh(&lock_data->spinlock);
354 355 356 357

		if (ret == 1)
			lock_data->idle_has_lock = 1;
	}
358
	spin_unlock_bh(&lock_data->spinlock);
359 360 361
}
EXPORT_SYMBOL(drm_idlelock_take);

D
Dave Airlie 已提交
362
void drm_idlelock_release(struct drm_lock_data *lock_data)
363 364 365 366
{
	unsigned int old, prev;
	volatile unsigned int *lock = &lock_data->hw_lock->lock;

367
	spin_lock_bh(&lock_data->spinlock);
368 369 370 371 372 373 374 375 376 377
	if (--lock_data->kernel_waiters == 0) {
		if (lock_data->idle_has_lock) {
			do {
				old = *lock;
				prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
			} while (prev != old);
			wake_up_interruptible(&lock_data->lock_queue);
			lock_data->idle_has_lock = 0;
		}
	}
378
	spin_unlock_bh(&lock_data->spinlock);
379 380 381 382
}
EXPORT_SYMBOL(drm_idlelock_release);


383
int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv)
384
{
385 386 387 388
	struct drm_master *master = file_priv->master;
	return (file_priv->lock_count && master->lock.hw_lock &&
		_DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
		master->lock.file_priv == file_priv);
389 390 391
}

EXPORT_SYMBOL(drm_i_have_hw_lock);