blob: 4553a3a1e496c463b82b6a93e7cda756aec350a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_irq.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * IRQ support
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
11 *
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36#include "drmP.h"
37
38#include <linux/interrupt.h> /* For task queue support */
39
40/**
41 * Get interrupt from bus id.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100042 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * \param inode device inode.
44 * \param filp file pointer.
45 * \param cmd command.
46 * \param arg user argument, pointing to a drm_irq_busid structure.
47 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100048 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * Finds the PCI device with the specified bus id and gets its IRQ number.
50 * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
51 * to that of the device that this DRM instance attached to.
52 */
53int drm_irq_by_busid(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100054 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 drm_file_t *priv = filp->private_data;
57 drm_device_t *dev = priv->head->dev;
58 drm_irq_busid_t __user *argp = (void __user *)arg;
59 drm_irq_busid_t p;
60
61 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
62 return -EINVAL;
63
64 if (copy_from_user(&p, argp, sizeof(p)))
65 return -EFAULT;
66
Dave Airlie332296012006-08-07 20:23:42 +100067 if ((p.busnum >> 8) != drm_get_pci_domain(dev) ||
Dave242ef0e2006-07-18 04:01:01 +100068 (p.busnum & 0xff) != dev->pdev->bus->number ||
69 p.devnum != PCI_SLOT(dev->pdev->devfn) || p.funcnum != PCI_FUNC(dev->pdev->devfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return -EINVAL;
71
72 p.irq = dev->irq;
73
Dave Airlieb5e89ed2005-09-25 14:28:13 +100074 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p.busnum, p.devnum, p.funcnum, p.irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (copy_to_user(argp, &p, sizeof(p)))
76 return -EFAULT;
77 return 0;
78}
79
80/**
81 * Install IRQ handler.
82 *
83 * \param dev DRM device.
84 * \param irq IRQ number.
85 *
86 * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
87 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
88 * before and after the installation.
89 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090static int drm_irq_install(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
96 return -EINVAL;
97
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 if (dev->irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -EINVAL;
100
Dave Airlie30e2fb12006-02-02 19:37:46 +1100101 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* Driver must have been initialized */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104 if (!dev->dev_private) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100105 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -EINVAL;
107 }
108
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 if (dev->irq_enabled) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100110 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return -EBUSY;
112 }
113 dev->irq_enabled = 1;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100114 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
119 init_waitqueue_head(&dev->vbl_queue);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120
121 spin_lock_init(&dev->vbl_lock);
122
123 INIT_LIST_HEAD(&dev->vbl_sigs.head);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 dev->vbl_pending = 0;
126 }
127
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 /* Before installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 dev->driver->irq_preinstall(dev);
130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700133 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000134
135 ret = request_irq(dev->irq, dev->driver->irq_handler,
136 sh_flags, dev->devname, dev);
137 if (ret < 0) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100138 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100140 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return ret;
142 }
143
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000144 /* After installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 dev->driver->irq_postinstall(dev);
146
147 return 0;
148}
149
150/**
151 * Uninstall the IRQ handler.
152 *
153 * \param dev DRM device.
154 *
155 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
156 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157int drm_irq_uninstall(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 int irq_enabled;
160
161 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
162 return -EINVAL;
163
Dave Airlie30e2fb12006-02-02 19:37:46 +1100164 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 irq_enabled = dev->irq_enabled;
166 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100167 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EINVAL;
171
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172 DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 dev->driver->irq_uninstall(dev);
175
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 return 0;
179}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181EXPORT_SYMBOL(drm_irq_uninstall);
182
183/**
184 * IRQ control ioctl.
185 *
186 * \param inode device inode.
187 * \param filp file pointer.
188 * \param cmd command.
189 * \param arg user argument, pointing to a drm_control structure.
190 * \return zero on success or a negative number on failure.
191 *
192 * Calls irq_install() or irq_uninstall() according to \p arg.
193 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194int drm_control(struct inode *inode, struct file *filp,
195 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 drm_file_t *priv = filp->private_data;
198 drm_device_t *dev = priv->head->dev;
199 drm_control_t ctl;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
202
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000203 if (copy_from_user(&ctl, (drm_control_t __user *) arg, sizeof(ctl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EFAULT;
205
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 switch (ctl.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 case DRM_INST_HANDLER:
208 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
209 return 0;
210 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
211 ctl.irq != dev->irq)
212 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 return drm_irq_install(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 case DRM_UNINST_HANDLER:
215 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
216 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 return drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 default:
219 return -EINVAL;
220 }
221}
222
223/**
224 * Wait for VBLANK.
225 *
226 * \param inode device inode.
227 * \param filp file pointer.
228 * \param cmd command.
229 * \param data user argument, pointing to a drm_wait_vblank structure.
230 * \return zero on success or a negative number on failure.
231 *
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232 * Verifies the IRQ is installed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
234 * If a signal is requested checks if this task has already scheduled the same signal
235 * for the same vblank sequence number - nothing to be done in
236 * that case. If the number of tasks waiting for the interrupt exceeds 100 the
237 * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
238 * task.
239 *
240 * If a signal is not requested, then calls vblank_wait().
241 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242int drm_wait_vblank(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 drm_file_t *priv = filp->private_data;
245 drm_device_t *dev = priv->head->dev;
246 drm_wait_vblank_t __user *argp = (void __user *)data;
247 drm_wait_vblank_t vblwait;
248 struct timeval now;
249 int ret = 0;
250 unsigned int flags;
251
252 if (!drm_core_check_feature(dev, DRIVER_IRQ_VBL))
253 return -EINVAL;
254
255 if (!dev->irq)
256 return -EINVAL;
257
Dave Airlie3d774612006-08-07 20:07:43 +1000258 if (copy_from_user(&vblwait, argp, sizeof(vblwait)))
259 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 switch (vblwait.request.type & ~_DRM_VBLANK_FLAGS_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 case _DRM_VBLANK_RELATIVE:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263 vblwait.request.sequence += atomic_read(&dev->vbl_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
265 case _DRM_VBLANK_ABSOLUTE:
266 break;
267 default:
268 return -EINVAL;
269 }
270
271 flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272
273 if (flags & _DRM_VBLANK_SIGNAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 unsigned long irqflags;
275 drm_vbl_sig_t *vbl_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277 vblwait.reply.sequence = atomic_read(&dev->vbl_received);
278
279 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* Check if this task has already scheduled the same signal
282 * for the same vblank sequence number; nothing to be done in
283 * that case
284 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 list_for_each_entry(vbl_sig, &dev->vbl_sigs.head, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (vbl_sig->sequence == vblwait.request.sequence
287 && vbl_sig->info.si_signo == vblwait.request.signal
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 && vbl_sig->task == current) {
289 spin_unlock_irqrestore(&dev->vbl_lock,
290 irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 goto done;
292 }
293 }
294
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295 if (dev->vbl_pending >= 100) {
296 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return -EBUSY;
298 }
299
300 dev->vbl_pending++;
301
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000302 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000304 if (!
305 (vbl_sig =
306 drm_alloc(sizeof(drm_vbl_sig_t), DRM_MEM_DRIVER))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -ENOMEM;
308 }
309
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 vbl_sig->sequence = vblwait.request.sequence;
313 vbl_sig->info.si_signo = vblwait.request.signal;
314 vbl_sig->task = current;
315
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 list_add_tail((struct list_head *)vbl_sig, &dev->vbl_sigs.head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 } else {
322 if (dev->driver->vblank_wait)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 ret =
324 dev->driver->vblank_wait(dev,
325 &vblwait.request.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 do_gettimeofday(&now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 vblwait.reply.tval_sec = now.tv_sec;
329 vblwait.reply.tval_usec = now.tv_usec;
330 }
331
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 done:
Dave Airlie3d774612006-08-07 20:07:43 +1000333 if (copy_to_user(argp, &vblwait, sizeof(vblwait)))
334 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 return ret;
337}
338
339/**
340 * Send the VBLANK signals.
341 *
342 * \param dev DRM device.
343 *
344 * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
345 *
346 * If a signal is not requested, then calls vblank_wait().
347 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000348void drm_vbl_send_signals(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct list_head *list, *tmp;
351 drm_vbl_sig_t *vbl_sig;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000352 unsigned int vbl_seq = atomic_read(&dev->vbl_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 unsigned long flags;
354
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 spin_lock_irqsave(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000357 list_for_each_safe(list, tmp, &dev->vbl_sigs.head) {
358 vbl_sig = list_entry(list, drm_vbl_sig_t, head);
359 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 vbl_sig->info.si_code = vbl_seq;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361 send_sig_info(vbl_sig->info.si_signo, &vbl_sig->info,
362 vbl_sig->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364 list_del(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000366 drm_free(vbl_sig, sizeof(*vbl_sig), DRM_MEM_DRIVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 dev->vbl_pending--;
369 }
370 }
371
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372 spin_unlock_irqrestore(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375EXPORT_SYMBOL(drm_vbl_send_signals);