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>
37
#include <drm/drmP.h>
38
#include "drm_legacy.h"
L
Linus Torvalds 已提交
39

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

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

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

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

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

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

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
			/* Device has been unregistered */
83
			send_sig(SIGTERM, current, 0);
L
Linus Torvalds 已提交
84 85 86
			ret = -EINTR;
			break;
		}
87 88 89
		if (drm_lock_take(&master->lock, lock->context)) {
			master->lock.file_priv = file_priv;
			master->lock.lock_time = jiffies;
D
Dave Airlie 已提交
90
			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
		block_all_signals(drm_notifier, dev, &dev->sigmask);
124
	}
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;
	}

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

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

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

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

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

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

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

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

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

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

L
Linus Torvalds 已提交
273 274
	if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
		DRM_ERROR("%d freed heavyweight lock held by %d\n",
D
Dave Airlie 已提交
275
			  context, _DRM_LOCKING_CONTEXT(old));
L
Linus Torvalds 已提交
276 277
		return 1;
	}
278
	wake_up_interruptible(&lock_data->lock_queue);
L
Linus Torvalds 已提交
279 280 281 282 283 284 285 286 287 288
	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.
 *
289
 * \param priv pointer to a drm_device structure.
L
Linus Torvalds 已提交
290 291 292
 * \return one if the signal should be delivered normally, or zero if the
 * signal should be blocked.
 */
D
Dave Airlie 已提交
293
static int drm_notifier(void *priv)
L
Linus Torvalds 已提交
294
{
295 296
	struct drm_device *dev = priv;
	struct drm_hw_lock *lock = dev->sigdata.lock;
D
Dave Airlie 已提交
297
	unsigned int old, new, prev;
L
Linus Torvalds 已提交
298

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

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

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

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

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

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

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

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

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