drm_irq.c 12.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/**
D
Dave Airlie 已提交
2
 * \file drm_irq.c
L
Linus Torvalds 已提交
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
 * IRQ support
 *
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
 *
 * Copyright 1999, 2000 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"

#include <linux/interrupt.h>	/* For task queue support */

/**
 * Get interrupt from bus id.
D
Dave Airlie 已提交
42
 *
L
Linus Torvalds 已提交
43
 * \param inode device inode.
44
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
45 46 47
 * \param cmd command.
 * \param arg user argument, pointing to a drm_irq_busid structure.
 * \return zero on success or a negative number on failure.
D
Dave Airlie 已提交
48
 *
L
Linus Torvalds 已提交
49 50 51 52
 * Finds the PCI device with the specified bus id and gets its IRQ number.
 * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
 * to that of the device that this DRM instance attached to.
 */
53 54
int drm_irq_by_busid(struct drm_device *dev, void *data,
		     struct drm_file *file_priv)
L
Linus Torvalds 已提交
55
{
56
	struct drm_irq_busid *p = data;
L
Linus Torvalds 已提交
57 58 59 60

	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
		return -EINVAL;

61 62 63
	if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
	    (p->busnum & 0xff) != dev->pdev->bus->number ||
	    p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
L
Linus Torvalds 已提交
64 65
		return -EINVAL;

66 67 68 69
	p->irq = dev->irq;

	DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
		  p->irq);
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83

	return 0;
}

/**
 * Install IRQ handler.
 *
 * \param dev DRM device.
 * \param irq IRQ number.
 *
 * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
 * before and after the installation.
 */
84
static int drm_irq_install(struct drm_device * dev)
L
Linus Torvalds 已提交
85 86
{
	int ret;
D
Dave Airlie 已提交
87
	unsigned long sh_flags = 0;
L
Linus Torvalds 已提交
88 89 90 91

	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
		return -EINVAL;

D
Dave Airlie 已提交
92
	if (dev->irq == 0)
L
Linus Torvalds 已提交
93 94
		return -EINVAL;

D
Dave Airlie 已提交
95
	mutex_lock(&dev->struct_mutex);
L
Linus Torvalds 已提交
96 97

	/* Driver must have been initialized */
D
Dave Airlie 已提交
98
	if (!dev->dev_private) {
D
Dave Airlie 已提交
99
		mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
100 101 102
		return -EINVAL;
	}

D
Dave Airlie 已提交
103
	if (dev->irq_enabled) {
D
Dave Airlie 已提交
104
		mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
105 106 107
		return -EBUSY;
	}
	dev->irq_enabled = 1;
D
Dave Airlie 已提交
108
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
109

110
	DRM_DEBUG("irq=%d\n", dev->irq);
L
Linus Torvalds 已提交
111

112 113 114 115 116 117 118 119 120 121 122
	if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
		init_waitqueue_head(&dev->vbl_queue);

		spin_lock_init(&dev->vbl_lock);

		INIT_LIST_HEAD(&dev->vbl_sigs);
		INIT_LIST_HEAD(&dev->vbl_sigs2);

		dev->vbl_pending = 0;
	}

D
Dave Airlie 已提交
123
	/* Before installing handler */
L
Linus Torvalds 已提交
124 125
	dev->driver->irq_preinstall(dev);

D
Dave Airlie 已提交
126
	/* Install handler */
L
Linus Torvalds 已提交
127
	if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
128
		sh_flags = IRQF_SHARED;
D
Dave Airlie 已提交
129 130 131 132

	ret = request_irq(dev->irq, dev->driver->irq_handler,
			  sh_flags, dev->devname, dev);
	if (ret < 0) {
D
Dave Airlie 已提交
133
		mutex_lock(&dev->struct_mutex);
L
Linus Torvalds 已提交
134
		dev->irq_enabled = 0;
D
Dave Airlie 已提交
135
		mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
136 137 138
		return ret;
	}

D
Dave Airlie 已提交
139
	/* After installing handler */
140
	dev->driver->irq_postinstall(dev);
L
Linus Torvalds 已提交
141

142
	return 0;
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151
}

/**
 * Uninstall the IRQ handler.
 *
 * \param dev DRM device.
 *
 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
 */
152
int drm_irq_uninstall(struct drm_device * dev)
L
Linus Torvalds 已提交
153 154 155 156 157 158
{
	int irq_enabled;

	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
		return -EINVAL;

D
Dave Airlie 已提交
159
	mutex_lock(&dev->struct_mutex);
L
Linus Torvalds 已提交
160 161
	irq_enabled = dev->irq_enabled;
	dev->irq_enabled = 0;
D
Dave Airlie 已提交
162
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
163

D
Dave Airlie 已提交
164
	if (!irq_enabled)
L
Linus Torvalds 已提交
165 166
		return -EINVAL;

167
	DRM_DEBUG("irq=%d\n", dev->irq);
L
Linus Torvalds 已提交
168 169 170

	dev->driver->irq_uninstall(dev);

D
Dave Airlie 已提交
171
	free_irq(dev->irq, dev);
L
Linus Torvalds 已提交
172

173 174
	dev->locked_tasklet_func = NULL;

L
Linus Torvalds 已提交
175 176
	return 0;
}
D
Dave Airlie 已提交
177

L
Linus Torvalds 已提交
178 179 180 181 182 183
EXPORT_SYMBOL(drm_irq_uninstall);

/**
 * IRQ control ioctl.
 *
 * \param inode device inode.
184
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
185 186 187 188 189 190
 * \param cmd command.
 * \param arg user argument, pointing to a drm_control structure.
 * \return zero on success or a negative number on failure.
 *
 * Calls irq_install() or irq_uninstall() according to \p arg.
 */
191 192
int drm_control(struct drm_device *dev, void *data,
		struct drm_file *file_priv)
L
Linus Torvalds 已提交
193
{
194
	struct drm_control *ctl = data;
D
Dave Airlie 已提交
195

L
Linus Torvalds 已提交
196 197 198
	/* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */


199
	switch (ctl->func) {
L
Linus Torvalds 已提交
200 201 202 203
	case DRM_INST_HANDLER:
		if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
			return 0;
		if (dev->if_version < DRM_IF_VERSION(1, 2) &&
204
		    ctl->irq != dev->irq)
L
Linus Torvalds 已提交
205
			return -EINVAL;
D
Dave Airlie 已提交
206
		return drm_irq_install(dev);
L
Linus Torvalds 已提交
207 208 209
	case DRM_UNINST_HANDLER:
		if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
			return 0;
D
Dave Airlie 已提交
210
		return drm_irq_uninstall(dev);
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219
	default:
		return -EINVAL;
	}
}

/**
 * Wait for VBLANK.
 *
 * \param inode device inode.
220
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
221 222 223 224
 * \param cmd command.
 * \param data user argument, pointing to a drm_wait_vblank structure.
 * \return zero on success or a negative number on failure.
 *
D
Dave Airlie 已提交
225
 * Verifies the IRQ is installed.
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234
 *
 * If a signal is requested checks if this task has already scheduled the same signal
 * for the same vblank sequence number - nothing to be done in
 * that case. If the number of tasks waiting for the interrupt exceeds 100 the
 * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
 * task.
 *
 * If a signal is not requested, then calls vblank_wait().
 */
235
int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
236
{
237
	union drm_wait_vblank *vblwait = data;
L
Linus Torvalds 已提交
238 239
	struct timeval now;
	int ret = 0;
240
	unsigned int flags, seq;
L
Linus Torvalds 已提交
241

242
	if ((!dev->irq) || (!dev->irq_enabled))
L
Linus Torvalds 已提交
243 244
		return -EINVAL;

245
	if (vblwait->request.type &
246 247
	    ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
		DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
248
			  vblwait->request.type,
249 250 251 252
			  (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
		return -EINVAL;
	}

253
	flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
254

255 256
	if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ?
				    DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL))
257 258
		return -EINVAL;

259 260
	seq = atomic_read((flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2
			  : &dev->vbl_received);
261

262
	switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
L
Linus Torvalds 已提交
263
	case _DRM_VBLANK_RELATIVE:
264 265
		vblwait->request.sequence += seq;
		vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
L
Linus Torvalds 已提交
266 267 268 269 270 271
	case _DRM_VBLANK_ABSOLUTE:
		break;
	default:
		return -EINVAL;
	}

272
	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
273 274
	    (seq - vblwait->request.sequence) <= (1<<23)) {
		vblwait->request.sequence = seq + 1;
275 276
	}

D
Dave Airlie 已提交
277
	if (flags & _DRM_VBLANK_SIGNAL) {
L
Linus Torvalds 已提交
278
		unsigned long irqflags;
279 280
		struct list_head *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY)
				      ? &dev->vbl_sigs2 : &dev->vbl_sigs;
D
Dave Airlie 已提交
281
		struct drm_vbl_sig *vbl_sig;
L
Linus Torvalds 已提交
282

D
Dave Airlie 已提交
283
		spin_lock_irqsave(&dev->vbl_lock, irqflags);
L
Linus Torvalds 已提交
284 285 286 287 288

		/* Check if this task has already scheduled the same signal
		 * for the same vblank sequence number; nothing to be done in
		 * that case
		 */
289
		list_for_each_entry(vbl_sig, vbl_sigs, head) {
290 291 292
			if (vbl_sig->sequence == vblwait->request.sequence
			    && vbl_sig->info.si_signo ==
			    vblwait->request.signal
D
Dave Airlie 已提交
293 294 295
			    && vbl_sig->task == current) {
				spin_unlock_irqrestore(&dev->vbl_lock,
						       irqflags);
296
				vblwait->reply.sequence = seq;
L
Linus Torvalds 已提交
297 298 299 300
				goto done;
			}
		}

301
		if (dev->vbl_pending >= 100) {
D
Dave Airlie 已提交
302
			spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
L
Linus Torvalds 已提交
303 304 305
			return -EBUSY;
		}

306 307
		dev->vbl_pending++;

D
Dave Airlie 已提交
308
		spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
L
Linus Torvalds 已提交
309

310 311 312
		if (!
		    (vbl_sig =
		     drm_alloc(sizeof(struct drm_vbl_sig), DRM_MEM_DRIVER))) {
L
Linus Torvalds 已提交
313 314 315
			return -ENOMEM;
		}

316
		memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
L
Linus Torvalds 已提交
317

318 319
		vbl_sig->sequence = vblwait->request.sequence;
		vbl_sig->info.si_signo = vblwait->request.signal;
L
Linus Torvalds 已提交
320 321
		vbl_sig->task = current;

D
Dave Airlie 已提交
322
		spin_lock_irqsave(&dev->vbl_lock, irqflags);
L
Linus Torvalds 已提交
323

324
		list_add_tail(&vbl_sig->head, vbl_sigs);
L
Linus Torvalds 已提交
325

D
Dave Airlie 已提交
326
		spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
327

328
		vblwait->reply.sequence = seq;
L
Linus Torvalds 已提交
329
	} else {
330 331 332 333 334 335 336
		if (flags & _DRM_VBLANK_SECONDARY) {
			if (dev->driver->vblank_wait2)
				ret = dev->driver->vblank_wait2(dev, &vblwait->request.sequence);
		} else if (dev->driver->vblank_wait)
			ret =
			    dev->driver->vblank_wait(dev,
						     &vblwait->request.sequence);
337

338
		do_gettimeofday(&now);
339 340
		vblwait->reply.tval_sec = now.tv_sec;
		vblwait->reply.tval_usec = now.tv_usec;
L
Linus Torvalds 已提交
341 342
	}

D
Dave Airlie 已提交
343
      done:
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352 353 354 355
	return ret;
}

/**
 * Send the VBLANK signals.
 *
 * \param dev DRM device.
 *
 * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
 *
 * If a signal is not requested, then calls vblank_wait().
 */
356
void drm_vbl_send_signals(struct drm_device * dev)
L
Linus Torvalds 已提交
357 358
{
	unsigned long flags;
359
	int i;
L
Linus Torvalds 已提交
360

D
Dave Airlie 已提交
361
	spin_lock_irqsave(&dev->vbl_lock, flags);
L
Linus Torvalds 已提交
362

363 364 365 366 367
	for (i = 0; i < 2; i++) {
		struct drm_vbl_sig *vbl_sig, *tmp;
		struct list_head *vbl_sigs = i ? &dev->vbl_sigs2 : &dev->vbl_sigs;
		unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 :
						   &dev->vbl_received);
368

369 370 371 372 373
		list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) {
			if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
				vbl_sig->info.si_code = vbl_seq;
				send_sig_info(vbl_sig->info.si_signo,
					      &vbl_sig->info, vbl_sig->task);
L
Linus Torvalds 已提交
374

375
				list_del(&vbl_sig->head);
L
Linus Torvalds 已提交
376

377 378 379 380 381 382
				drm_free(vbl_sig, sizeof(*vbl_sig),
					 DRM_MEM_DRIVER);

				dev->vbl_pending--;
			}
		}
L
Linus Torvalds 已提交
383 384
	}

D
Dave Airlie 已提交
385
	spin_unlock_irqrestore(&dev->vbl_lock, flags);
L
Linus Torvalds 已提交
386 387
}

388
EXPORT_SYMBOL(drm_vbl_send_signals);
389 390 391 392 393 394 395 396 397 398 399 400

/**
 * Tasklet wrapper function.
 *
 * \param data DRM device in disguise.
 *
 * Attempts to grab the HW lock and calls the driver callback on success. On
 * failure, leave the lock marked as contended so the callback can be called
 * from drm_unlock().
 */
static void drm_locked_tasklet_func(unsigned long data)
{
401
	struct drm_device *dev = (struct drm_device *)data;
402
	unsigned long irqflags;
403 404
	void (*tasklet_func)(struct drm_device *);
	
405
	spin_lock_irqsave(&dev->tasklet_lock, irqflags);
406 407
	tasklet_func = dev->locked_tasklet_func;
	spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
408

409
	if (!tasklet_func ||
410
	    !drm_lock_take(&dev->lock,
411 412 413 414 415 416 417
			   DRM_KERNEL_CONTEXT)) {
		return;
	}

	dev->lock.lock_time = jiffies;
	atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);

418 419 420 421 422 423 424
	spin_lock_irqsave(&dev->tasklet_lock, irqflags);
	tasklet_func = dev->locked_tasklet_func;
	dev->locked_tasklet_func = NULL;
	spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
	
	if (tasklet_func != NULL)
		tasklet_func(dev);
425

426
	drm_lock_free(&dev->lock,
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
		      DRM_KERNEL_CONTEXT);
}

/**
 * Schedule a tasklet to call back a driver hook with the HW lock held.
 *
 * \param dev DRM device.
 * \param func Driver callback.
 *
 * This is intended for triggering actions that require the HW lock from an
 * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
 * completes. Note that the callback may be called from interrupt or process
 * context, it must not make any assumptions about this. Also, the HW lock will
 * be held with the kernel context or any client context.
 */
442
void drm_locked_tasklet(struct drm_device *dev, void (*func)(struct drm_device *))
443 444 445 446
{
	unsigned long irqflags;
	static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);

447 448
	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) ||
	    test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
		return;

	spin_lock_irqsave(&dev->tasklet_lock, irqflags);

	if (dev->locked_tasklet_func) {
		spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
		return;
	}

	dev->locked_tasklet_func = func;

	spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);

	drm_tasklet.data = (unsigned long)dev;

	tasklet_hi_schedule(&drm_tasklet);
}
EXPORT_SYMBOL(drm_locked_tasklet);