drm_lock.c 10.3 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
 * \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.
 */

36
#include <linux/export.h>
L
Linus Torvalds 已提交
37 38
#include "drmP.h"

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

41 42
static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);

D
Dave Airlie 已提交
43
/**
L
Linus Torvalds 已提交
44 45 46
 * Lock ioctl.
 *
 * \param inode device inode.
47
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
48 49 50 51 52 53
 * \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.
 */
54
int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
55
{
D
Dave Airlie 已提交
56
	DECLARE_WAITQUEUE(entry, current);
57
	struct drm_lock *lock = data;
58
	struct drm_master *master = file_priv->master;
D
Dave Airlie 已提交
59
	int ret = 0;
L
Linus Torvalds 已提交
60

61
	++file_priv->lock_count;
L
Linus Torvalds 已提交
62

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

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

73 74 75 76 77
	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 已提交
78 79
	for (;;) {
		__set_current_state(TASK_INTERRUPTIBLE);
80
		if (!master->lock.hw_lock) {
L
Linus Torvalds 已提交
81
			/* Device has been unregistered */
82
			send_sig(SIGTERM, current, 0);
L
Linus Torvalds 已提交
83 84 85
			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
		/* Contention */
94
		mutex_unlock(&drm_global_mutex);
L
Linus Torvalds 已提交
95
		schedule();
96
		mutex_lock(&drm_global_mutex);
D
Dave Airlie 已提交
97
		if (signal_pending(current)) {
98
			ret = -EINTR;
L
Linus Torvalds 已提交
99 100 101
			break;
		}
	}
102 103 104
	spin_lock_bh(&master->lock.spinlock);
	master->lock.user_waiters--;
	spin_unlock_bh(&master->lock.spinlock);
L
Linus Torvalds 已提交
105
	__set_current_state(TASK_RUNNING);
106
	remove_wait_queue(&master->lock.lock_queue, &entry);
L
Linus Torvalds 已提交
107

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

112 113 114
	/* 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 */
115
	if (!file_priv->is_master) {
116 117 118 119 120 121
		sigemptyset(&dev->sigmask);
		sigaddset(&dev->sigmask, SIGSTOP);
		sigaddset(&dev->sigmask, SIGTSTP);
		sigaddset(&dev->sigmask, SIGTTIN);
		sigaddset(&dev->sigmask, SIGTTOU);
		dev->sigdata.context = lock->context;
122
		dev->sigdata.lock = master->lock.hw_lock;
123 124
		block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask);
	}
D
Dave Airlie 已提交
125

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

135
	return 0;
L
Linus Torvalds 已提交
136 137
}

D
Dave Airlie 已提交
138
/**
L
Linus Torvalds 已提交
139 140 141
 * Unlock ioctl.
 *
 * \param inode device inode.
142
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
143 144 145 146 147 148
 * \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.
 */
149
int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
150
{
151
	struct drm_lock *lock = data;
152
	struct drm_master *master = file_priv->master;
L
Linus Torvalds 已提交
153

154
	if (lock->context == DRM_KERNEL_CONTEXT) {
D
Dave Airlie 已提交
155
		DRM_ERROR("Process %d using kernel context %d\n",
156
			  task_pid_nr(current), lock->context);
L
Linus Torvalds 已提交
157 158 159
		return -EINVAL;
	}

D
Dave Airlie 已提交
160
	atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
L
Linus Torvalds 已提交
161

162 163 164 165
	if (drm_lock_free(&master->lock, lock->context)) {
		/* FIXME: Should really bail out here. */
	}

L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178
	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.
 */
179
static
D
Dave Airlie 已提交
180
int drm_lock_take(struct drm_lock_data *lock_data,
181
		  unsigned int context)
L
Linus Torvalds 已提交
182 183
{
	unsigned int old, new, prev;
184
	volatile unsigned int *lock = &lock_data->hw_lock->lock;
L
Linus Torvalds 已提交
185

186
	spin_lock_bh(&lock_data->spinlock);
L
Linus Torvalds 已提交
187 188
	do {
		old = *lock;
D
Dave Airlie 已提交
189 190
		if (old & _DRM_LOCK_HELD)
			new = old | _DRM_LOCK_CONT;
191 192 193 194 195
		else {
			new = context | _DRM_LOCK_HELD |
				((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
				 _DRM_LOCK_CONT : 0);
		}
L
Linus Torvalds 已提交
196 197
		prev = cmpxchg(lock, old, new);
	} while (prev != old);
198
	spin_unlock_bh(&lock_data->spinlock);
199

L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208
	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;
		}
	}
209 210

	if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
D
Dave Airlie 已提交
211
		/* Have lock */
L
Linus Torvalds 已提交
212 213 214 215 216 217 218
		return 1;
	}
	return 0;
}

/**
 * This takes a lock forcibly and hands it to context.	Should ONLY be used
D
Dave Airlie 已提交
219 220
 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
 *
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228
 * \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 已提交
229
static int drm_lock_transfer(struct drm_lock_data *lock_data,
D
Dave Airlie 已提交
230
			     unsigned int context)
L
Linus Torvalds 已提交
231 232
{
	unsigned int old, new, prev;
233
	volatile unsigned int *lock = &lock_data->hw_lock->lock;
L
Linus Torvalds 已提交
234

235
	lock_data->file_priv = NULL;
L
Linus Torvalds 已提交
236
	do {
D
Dave Airlie 已提交
237 238
		old = *lock;
		new = context | _DRM_LOCK_HELD;
L
Linus Torvalds 已提交
239 240 241 242 243 244 245
		prev = cmpxchg(lock, old, new);
	} while (prev != old);
	return 1;
}

/**
 * Free lock.
D
Dave Airlie 已提交
246
 *
L
Linus Torvalds 已提交
247 248 249
 * \param dev DRM device.
 * \param lock lock.
 * \param context context.
D
Dave Airlie 已提交
250
 *
L
Linus Torvalds 已提交
251 252 253 254
 * 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 已提交
255
int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
L
Linus Torvalds 已提交
256 257
{
	unsigned int old, new, prev;
258 259
	volatile unsigned int *lock = &lock_data->hw_lock->lock;

260
	spin_lock_bh(&lock_data->spinlock);
261 262 263
	if (lock_data->kernel_waiters != 0) {
		drm_lock_transfer(lock_data, 0);
		lock_data->idle_has_lock = 1;
264
		spin_unlock_bh(&lock_data->spinlock);
265 266
		return 1;
	}
267
	spin_unlock_bh(&lock_data->spinlock);
L
Linus Torvalds 已提交
268 269

	do {
D
Dave Airlie 已提交
270
		old = *lock;
271
		new = _DRM_LOCKING_CONTEXT(old);
L
Linus Torvalds 已提交
272 273
		prev = cmpxchg(lock, old, new);
	} while (prev != old);
274

L
Linus Torvalds 已提交
275 276
	if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
		DRM_ERROR("%d freed heavyweight lock held by %d\n",
D
Dave Airlie 已提交
277
			  context, _DRM_LOCKING_CONTEXT(old));
L
Linus Torvalds 已提交
278 279
		return 1;
	}
280
	wake_up_interruptible(&lock_data->lock_queue);
L
Linus Torvalds 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294
	return 0;
}

/**
 * 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 已提交
295
static int drm_notifier(void *priv)
L
Linus Torvalds 已提交
296
{
D
Dave Airlie 已提交
297
	struct drm_sigdata *s = (struct drm_sigdata *) priv;
D
Dave Airlie 已提交
298
	unsigned int old, new, prev;
L
Linus Torvalds 已提交
299

D
Dave Airlie 已提交
300
	/* Allow signal delivery if lock isn't held */
L
Linus Torvalds 已提交
301
	if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
D
Dave Airlie 已提交
302 303
	    || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
		return 1;
L
Linus Torvalds 已提交
304

D
Dave Airlie 已提交
305 306
	/* Otherwise, set flag to force call to
	   drmUnlock */
L
Linus Torvalds 已提交
307
	do {
D
Dave Airlie 已提交
308 309
		old = s->lock->lock;
		new = old | _DRM_LOCK_CONT;
L
Linus Torvalds 已提交
310 311 312 313
		prev = cmpxchg(&s->lock->lock, old, new);
	} while (prev != old);
	return 0;
}
314 315 316 317 318 319 320 321 322 323 324 325 326 327

/**
 * 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 已提交
328
void drm_idlelock_take(struct drm_lock_data *lock_data)
329
{
330
	int ret;
331

332
	spin_lock_bh(&lock_data->spinlock);
333 334 335
	lock_data->kernel_waiters++;
	if (!lock_data->idle_has_lock) {

336
		spin_unlock_bh(&lock_data->spinlock);
337
		ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
338
		spin_lock_bh(&lock_data->spinlock);
339 340 341 342

		if (ret == 1)
			lock_data->idle_has_lock = 1;
	}
343
	spin_unlock_bh(&lock_data->spinlock);
344
}
345
EXPORT_SYMBOL(drm_idlelock_take);
346

D
Dave Airlie 已提交
347
void drm_idlelock_release(struct drm_lock_data *lock_data)
348 349 350 351
{
	unsigned int old, prev;
	volatile unsigned int *lock = &lock_data->hw_lock->lock;

352
	spin_lock_bh(&lock_data->spinlock);
353 354 355 356 357 358 359 360 361 362
	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;
		}
	}
363
	spin_unlock_bh(&lock_data->spinlock);
364
}
365
EXPORT_SYMBOL(drm_idlelock_release);
366

367
int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv)
368
{
369 370 371 372
	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);
373
}