blob: c2ecb3ed009fd6f714c9961d0deee66debc2fbfc [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"
Jesse Barnesac2874b2010-07-01 16:47:31 -070037#include "drm_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <linux/interrupt.h> /* For task queue support */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Dave Airlie28d52042009-09-21 14:33:58 +100042#include <linux/vgaarb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/**
44 * Get interrupt from bus id.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100045 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +100047 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * \param cmd command.
49 * \param arg user argument, pointing to a drm_irq_busid structure.
50 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100051 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * Finds the PCI device with the specified bus id and gets its IRQ number.
53 * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
54 * to that of the device that this DRM instance attached to.
55 */
Eric Anholtc153f452007-09-03 12:06:45 +100056int drm_irq_by_busid(struct drm_device *dev, void *data,
57 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Eric Anholtc153f452007-09-03 12:06:45 +100059 struct drm_irq_busid *p = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
62 return -EINVAL;
63
Eric Anholtc153f452007-09-03 12:06:45 +100064 if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
65 (p->busnum & 0xff) != dev->pdev->bus->number ||
66 p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return -EINVAL;
68
Eric Anholted4cb412008-07-29 12:10:39 -070069 p->irq = dev->pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Eric Anholtc153f452007-09-03 12:06:45 +100071 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
72 p->irq);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
75}
76
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070077static void vblank_disable_fn(unsigned long arg)
78{
79 struct drm_device *dev = (struct drm_device *)arg;
80 unsigned long irqflags;
81 int i;
82
83 if (!dev->vblank_disable_allowed)
84 return;
85
86 for (i = 0; i < dev->num_crtcs; i++) {
87 spin_lock_irqsave(&dev->vbl_lock, irqflags);
88 if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
89 dev->vblank_enabled[i]) {
90 DRM_DEBUG("disabling vblank on crtc %d\n", i);
91 dev->last_vblank[i] =
92 dev->driver->get_vblank_counter(dev, i);
93 dev->driver->disable_vblank(dev, i);
94 dev->vblank_enabled[i] = 0;
95 }
96 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
97 }
98}
99
Keith Packard52440212008-11-18 09:30:25 -0800100void drm_vblank_cleanup(struct drm_device *dev)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700101{
102 /* Bail if the driver didn't call drm_vblank_init() */
103 if (dev->num_crtcs == 0)
104 return;
105
106 del_timer(&dev->vblank_disable_timer);
107
108 vblank_disable_fn((unsigned long)dev);
109
Eric Anholt9a298b22009-03-24 12:23:04 -0700110 kfree(dev->vbl_queue);
111 kfree(dev->_vblank_count);
112 kfree(dev->vblank_refcount);
113 kfree(dev->vblank_enabled);
114 kfree(dev->last_vblank);
115 kfree(dev->last_vblank_wait);
116 kfree(dev->vblank_inmodeset);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700117
118 dev->num_crtcs = 0;
119}
Jerome Glissee77cef92010-01-07 15:39:13 +0100120EXPORT_SYMBOL(drm_vblank_cleanup);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700121
122int drm_vblank_init(struct drm_device *dev, int num_crtcs)
123{
124 int i, ret = -ENOMEM;
125
126 setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
127 (unsigned long)dev);
128 spin_lock_init(&dev->vbl_lock);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700129 dev->num_crtcs = num_crtcs;
130
Eric Anholt9a298b22009-03-24 12:23:04 -0700131 dev->vbl_queue = kmalloc(sizeof(wait_queue_head_t) * num_crtcs,
132 GFP_KERNEL);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700133 if (!dev->vbl_queue)
134 goto err;
135
Eric Anholt9a298b22009-03-24 12:23:04 -0700136 dev->_vblank_count = kmalloc(sizeof(atomic_t) * num_crtcs, GFP_KERNEL);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700137 if (!dev->_vblank_count)
138 goto err;
139
Eric Anholt9a298b22009-03-24 12:23:04 -0700140 dev->vblank_refcount = kmalloc(sizeof(atomic_t) * num_crtcs,
141 GFP_KERNEL);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700142 if (!dev->vblank_refcount)
143 goto err;
144
Eric Anholt9a298b22009-03-24 12:23:04 -0700145 dev->vblank_enabled = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700146 if (!dev->vblank_enabled)
147 goto err;
148
Eric Anholt9a298b22009-03-24 12:23:04 -0700149 dev->last_vblank = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700150 if (!dev->last_vblank)
151 goto err;
152
Eric Anholt9a298b22009-03-24 12:23:04 -0700153 dev->last_vblank_wait = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
Eric Anholtfede5c92008-12-19 17:23:38 -0800154 if (!dev->last_vblank_wait)
155 goto err;
156
Eric Anholt9a298b22009-03-24 12:23:04 -0700157 dev->vblank_inmodeset = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700158 if (!dev->vblank_inmodeset)
159 goto err;
160
161 /* Zero per-crtc vblank stuff */
162 for (i = 0; i < num_crtcs; i++) {
163 init_waitqueue_head(&dev->vbl_queue[i]);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700164 atomic_set(&dev->_vblank_count[i], 0);
165 atomic_set(&dev->vblank_refcount[i], 0);
166 }
167
168 dev->vblank_disable_allowed = 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700169 return 0;
170
171err:
172 drm_vblank_cleanup(dev);
173 return ret;
174}
175EXPORT_SYMBOL(drm_vblank_init);
176
Dave Airlie28d52042009-09-21 14:33:58 +1000177static void drm_irq_vgaarb_nokms(void *cookie, bool state)
178{
179 struct drm_device *dev = cookie;
180
181 if (dev->driver->vgaarb_irq) {
182 dev->driver->vgaarb_irq(dev, state);
183 return;
184 }
185
186 if (!dev->irq_enabled)
187 return;
188
189 if (state)
190 dev->driver->irq_uninstall(dev);
191 else {
192 dev->driver->irq_preinstall(dev);
193 dev->driver->irq_postinstall(dev);
194 }
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/**
198 * Install IRQ handler.
199 *
200 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700202 * Initializes the IRQ related data. Installs the handler, calling the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
204 * before and after the installation.
205 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700206int drm_irq_install(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700208 int ret = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 unsigned long sh_flags = 0;
Dave Airlieb8da7de2009-06-02 16:50:35 +1000210 char *irqname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
213 return -EINVAL;
214
Eric Anholted4cb412008-07-29 12:10:39 -0700215 if (dev->pdev->irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -EINVAL;
217
Dave Airlie30e2fb12006-02-02 19:37:46 +1100218 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Driver must have been initialized */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000221 if (!dev->dev_private) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100222 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -EINVAL;
224 }
225
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 if (dev->irq_enabled) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100227 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EBUSY;
229 }
230 dev->irq_enabled = 1;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100231 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Eric Anholted4cb412008-07-29 12:10:39 -0700233 DRM_DEBUG("irq=%d\n", dev->pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235 /* Before installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 dev->driver->irq_preinstall(dev);
237
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700240 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241
Dave Airlieb8da7de2009-06-02 16:50:35 +1000242 if (dev->devname)
243 irqname = dev->devname;
244 else
245 irqname = dev->driver->name;
246
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -0700247 ret = request_irq(drm_dev_to_irq(dev), dev->driver->irq_handler,
Dave Airlieb8da7de2009-06-02 16:50:35 +1000248 sh_flags, irqname, dev);
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -0700249
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250 if (ret < 0) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100251 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100253 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return ret;
255 }
256
Dave Airlie28d52042009-09-21 14:33:58 +1000257 if (!drm_core_check_feature(dev, DRIVER_MODESET))
258 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
259
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 /* After installing handler */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700261 ret = dev->driver->irq_postinstall(dev);
262 if (ret < 0) {
263 mutex_lock(&dev->struct_mutex);
264 dev->irq_enabled = 0;
265 mutex_unlock(&dev->struct_mutex);
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700268 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700270EXPORT_SYMBOL(drm_irq_install);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/**
273 * Uninstall the IRQ handler.
274 *
275 * \param dev DRM device.
276 *
277 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
278 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000279int drm_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800281 unsigned long irqflags;
282 int irq_enabled, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
285 return -EINVAL;
286
Dave Airlie30e2fb12006-02-02 19:37:46 +1100287 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 irq_enabled = dev->irq_enabled;
289 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100290 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800292 /*
293 * Wake up any waiters so they don't hang.
294 */
295 spin_lock_irqsave(&dev->vbl_lock, irqflags);
296 for (i = 0; i < dev->num_crtcs; i++) {
297 DRM_WAKEUP(&dev->vbl_queue[i]);
298 dev->vblank_enabled[i] = 0;
Jesse Barnes14d200c2009-02-06 13:04:49 -0800299 dev->last_vblank[i] = dev->driver->get_vblank_counter(dev, i);
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800300 }
301 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
302
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000303 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return -EINVAL;
305
Eric Anholted4cb412008-07-29 12:10:39 -0700306 DRM_DEBUG("irq=%d\n", dev->pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dave Airlie28d52042009-09-21 14:33:58 +1000308 if (!drm_core_check_feature(dev, DRIVER_MODESET))
309 vga_client_register(dev->pdev, NULL, NULL, NULL);
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 dev->driver->irq_uninstall(dev);
312
Eric Anholted4cb412008-07-29 12:10:39 -0700313 free_irq(dev->pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 return 0;
316}
317EXPORT_SYMBOL(drm_irq_uninstall);
318
319/**
320 * IRQ control ioctl.
321 *
322 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000323 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * \param cmd command.
325 * \param arg user argument, pointing to a drm_control structure.
326 * \return zero on success or a negative number on failure.
327 *
328 * Calls irq_install() or irq_uninstall() according to \p arg.
329 */
Eric Anholtc153f452007-09-03 12:06:45 +1000330int drm_control(struct drm_device *dev, void *data,
331 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Eric Anholtc153f452007-09-03 12:06:45 +1000333 struct drm_control *ctl = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Eric Anholtc153f452007-09-03 12:06:45 +1000338 switch (ctl->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 case DRM_INST_HANDLER:
340 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
341 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800342 if (drm_core_check_feature(dev, DRIVER_MODESET))
343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
Eric Anholted4cb412008-07-29 12:10:39 -0700345 ctl->irq != dev->pdev->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 return drm_irq_install(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 case DRM_UNINST_HANDLER:
349 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
350 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800351 if (drm_core_check_feature(dev, DRIVER_MODESET))
352 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000353 return drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 default:
355 return -EINVAL;
356 }
357}
358
359/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700360 * drm_vblank_count - retrieve "cooked" vblank counter value
361 * @dev: DRM device
362 * @crtc: which counter to retrieve
363 *
364 * Fetches the "cooked" vblank count value that represents the number of
365 * vblank events since the system was booted, including lost events due to
366 * modesetting activity.
367 */
368u32 drm_vblank_count(struct drm_device *dev, int crtc)
369{
370 return atomic_read(&dev->_vblank_count[crtc]);
371}
372EXPORT_SYMBOL(drm_vblank_count);
373
374/**
375 * drm_update_vblank_count - update the master vblank counter
376 * @dev: DRM device
377 * @crtc: counter to update
378 *
379 * Call back into the driver to update the appropriate vblank counter
380 * (specified by @crtc). Deal with wraparound, if it occurred, and
381 * update the last read value so we can deal with wraparound on the next
382 * call if necessary.
383 *
384 * Only necessary when going from off->on, to account for frames we
385 * didn't get an interrupt for.
386 *
387 * Note: caller must hold dev->vbl_lock since this reads & writes
388 * device vblank fields.
389 */
390static void drm_update_vblank_count(struct drm_device *dev, int crtc)
391{
392 u32 cur_vblank, diff;
393
394 /*
395 * Interrupts were disabled prior to this call, so deal with counter
396 * wrap if needed.
397 * NOTE! It's possible we lost a full dev->max_vblank_count events
398 * here if the register is small or we had vblank interrupts off for
399 * a long time.
400 */
401 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
402 diff = cur_vblank - dev->last_vblank[crtc];
403 if (cur_vblank < dev->last_vblank[crtc]) {
404 diff += dev->max_vblank_count;
405
406 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
407 crtc, dev->last_vblank[crtc], cur_vblank, diff);
408 }
409
410 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
411 crtc, diff);
412
413 atomic_add(diff, &dev->_vblank_count[crtc]);
414}
415
416/**
417 * drm_vblank_get - get a reference count on vblank events
418 * @dev: DRM device
419 * @crtc: which CRTC to own
420 *
421 * Acquire a reference count on vblank events to avoid having them disabled
422 * while in use.
423 *
424 * RETURNS
425 * Zero on success, nonzero on failure.
426 */
427int drm_vblank_get(struct drm_device *dev, int crtc)
428{
429 unsigned long irqflags;
430 int ret = 0;
431
432 spin_lock_irqsave(&dev->vbl_lock, irqflags);
433 /* Going from 0->1 means we have to enable interrupts again */
Li Peng778c9022009-11-09 12:51:22 +0800434 if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1) {
435 if (!dev->vblank_enabled[crtc]) {
436 ret = dev->driver->enable_vblank(dev, crtc);
437 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
438 if (ret)
439 atomic_dec(&dev->vblank_refcount[crtc]);
440 else {
441 dev->vblank_enabled[crtc] = 1;
442 drm_update_vblank_count(dev, crtc);
443 }
444 }
445 } else {
446 if (!dev->vblank_enabled[crtc]) {
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700447 atomic_dec(&dev->vblank_refcount[crtc]);
Li Peng778c9022009-11-09 12:51:22 +0800448 ret = -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700449 }
450 }
451 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
452
453 return ret;
454}
455EXPORT_SYMBOL(drm_vblank_get);
456
457/**
458 * drm_vblank_put - give up ownership of vblank events
459 * @dev: DRM device
460 * @crtc: which counter to give up
461 *
462 * Release ownership of a given vblank counter, turning off interrupts
463 * if possible.
464 */
465void drm_vblank_put(struct drm_device *dev, int crtc)
466{
Chris Wilsonb3f5e732009-02-19 14:48:22 +0000467 BUG_ON (atomic_read (&dev->vblank_refcount[crtc]) == 0);
468
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700469 /* Last user schedules interrupt disable */
470 if (atomic_dec_and_test(&dev->vblank_refcount[crtc]))
471 mod_timer(&dev->vblank_disable_timer, jiffies + 5*DRM_HZ);
472}
473EXPORT_SYMBOL(drm_vblank_put);
474
Li Peng778c9022009-11-09 12:51:22 +0800475void drm_vblank_off(struct drm_device *dev, int crtc)
476{
477 unsigned long irqflags;
478
479 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Jesse Barnese32ee7f2010-03-26 18:07:15 +0000480 dev->driver->disable_vblank(dev, crtc);
Li Peng778c9022009-11-09 12:51:22 +0800481 DRM_WAKEUP(&dev->vbl_queue[crtc]);
482 dev->vblank_enabled[crtc] = 0;
483 dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
484 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
485}
486EXPORT_SYMBOL(drm_vblank_off);
487
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700488/**
Dave Airlief453ba02008-11-07 14:05:41 -0800489 * drm_vblank_pre_modeset - account for vblanks across mode sets
490 * @dev: DRM device
491 * @crtc: CRTC in question
492 * @post: post or pre mode set?
493 *
494 * Account for vblank events across mode setting events, which will likely
495 * reset the hardware frame counter.
496 */
497void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
498{
Jerome Glissee77cef92010-01-07 15:39:13 +0100499 /* vblank is not initialized (IRQ not installed ?) */
500 if (!dev->num_crtcs)
501 return;
Dave Airlief453ba02008-11-07 14:05:41 -0800502 /*
503 * To avoid all the problems that might happen if interrupts
504 * were enabled/disabled around or between these calls, we just
505 * have the kernel take a reference on the CRTC (just once though
506 * to avoid corrupting the count if multiple, mismatch calls occur),
507 * so that interrupts remain enabled in the interim.
508 */
509 if (!dev->vblank_inmodeset[crtc]) {
Chris Wilsonb3f5e732009-02-19 14:48:22 +0000510 dev->vblank_inmodeset[crtc] = 0x1;
511 if (drm_vblank_get(dev, crtc) == 0)
512 dev->vblank_inmodeset[crtc] |= 0x2;
Dave Airlief453ba02008-11-07 14:05:41 -0800513 }
514}
515EXPORT_SYMBOL(drm_vblank_pre_modeset);
516
517void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
518{
519 unsigned long irqflags;
520
521 if (dev->vblank_inmodeset[crtc]) {
522 spin_lock_irqsave(&dev->vbl_lock, irqflags);
523 dev->vblank_disable_allowed = 1;
Dave Airlief453ba02008-11-07 14:05:41 -0800524 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Chris Wilsonb3f5e732009-02-19 14:48:22 +0000525
526 if (dev->vblank_inmodeset[crtc] & 0x2)
527 drm_vblank_put(dev, crtc);
528
529 dev->vblank_inmodeset[crtc] = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800530 }
531}
532EXPORT_SYMBOL(drm_vblank_post_modeset);
533
534/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700535 * drm_modeset_ctl - handle vblank event counter changes across mode switch
536 * @DRM_IOCTL_ARGS: standard ioctl arguments
537 *
538 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
539 * ioctls around modesetting so that any lost vblank events are accounted for.
540 *
541 * Generally the counter will reset across mode sets. If interrupts are
542 * enabled around this call, we don't have to do anything since the counter
543 * will have already been incremented.
544 */
545int drm_modeset_ctl(struct drm_device *dev, void *data,
546 struct drm_file *file_priv)
547{
548 struct drm_modeset_ctl *modeset = data;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700549 int crtc, ret = 0;
550
551 /* If drm_vblank_init() hasn't been called yet, just no-op */
552 if (!dev->num_crtcs)
553 goto out;
554
555 crtc = modeset->crtc;
556 if (crtc >= dev->num_crtcs) {
557 ret = -EINVAL;
558 goto out;
559 }
560
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700561 switch (modeset->cmd) {
562 case _DRM_PRE_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -0800563 drm_vblank_pre_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700564 break;
565 case _DRM_POST_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -0800566 drm_vblank_post_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700567 break;
568 default:
569 ret = -EINVAL;
570 break;
571 }
572
573out:
574 return ret;
575}
576
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000577static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
578 union drm_wait_vblank *vblwait,
579 struct drm_file *file_priv)
580{
581 struct drm_pending_vblank_event *e;
582 struct timeval now;
583 unsigned long flags;
584 unsigned int seq;
585
586 e = kzalloc(sizeof *e, GFP_KERNEL);
587 if (e == NULL)
588 return -ENOMEM;
589
590 e->pipe = pipe;
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -0700591 e->base.pid = current->pid;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000592 e->event.base.type = DRM_EVENT_VBLANK;
593 e->event.base.length = sizeof e->event;
594 e->event.user_data = vblwait->request.signal;
595 e->base.event = &e->event.base;
596 e->base.file_priv = file_priv;
597 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
598
599 do_gettimeofday(&now);
600 spin_lock_irqsave(&dev->event_lock, flags);
601
602 if (file_priv->event_space < sizeof e->event) {
603 spin_unlock_irqrestore(&dev->event_lock, flags);
604 kfree(e);
605 return -ENOMEM;
606 }
607
608 file_priv->event_space -= sizeof e->event;
609 seq = drm_vblank_count(dev, pipe);
610 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
611 (seq - vblwait->request.sequence) <= (1 << 23)) {
612 vblwait->request.sequence = seq + 1;
Jesse Barnes4a921642009-11-10 08:21:25 +0000613 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000614 }
615
616 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
617 vblwait->request.sequence, seq, pipe);
618
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -0700619 trace_drm_vblank_event_queued(current->pid, pipe,
620 vblwait->request.sequence);
621
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000622 e->event.sequence = vblwait->request.sequence;
623 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
624 e->event.tv_sec = now.tv_sec;
625 e->event.tv_usec = now.tv_usec;
626 drm_vblank_put(dev, e->pipe);
627 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
628 wake_up_interruptible(&e->base.file_priv->event_wait);
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -0700629 trace_drm_vblank_event_delivered(current->pid, pipe,
630 vblwait->request.sequence);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000631 } else {
632 list_add_tail(&e->base.link, &dev->vblank_event_list);
633 }
634
635 spin_unlock_irqrestore(&dev->event_lock, flags);
636
637 return 0;
638}
639
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700640/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * Wait for VBLANK.
642 *
643 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000644 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * \param cmd command.
646 * \param data user argument, pointing to a drm_wait_vblank structure.
647 * \return zero on success or a negative number on failure.
648 *
Eric Anholt30b23632009-01-27 21:19:41 -0800649 * This function enables the vblank interrupt on the pipe requested, then
650 * sleeps waiting for the requested sequence number to occur, and drops
651 * the vblank interrupt refcount afterwards. (vblank irq disable follows that
652 * after a timeout with no further vblank waits scheduled).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700654int drm_wait_vblank(struct drm_device *dev, void *data,
655 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Eric Anholtc153f452007-09-03 12:06:45 +1000657 union drm_wait_vblank *vblwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int ret = 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700659 unsigned int flags, seq, crtc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Eric Anholted4cb412008-07-29 12:10:39 -0700661 if ((!dev->pdev->irq) || (!dev->irq_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -EINVAL;
663
Eric Anholt30b23632009-01-27 21:19:41 -0800664 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
665 return -EINVAL;
666
Eric Anholtc153f452007-09-03 12:06:45 +1000667 if (vblwait->request.type &
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000668 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
669 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000670 vblwait->request.type,
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000671 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
672 return -EINVAL;
673 }
674
Eric Anholtc153f452007-09-03 12:06:45 +1000675 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700676 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000677
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700678 if (crtc >= dev->num_crtcs)
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000679 return -EINVAL;
680
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700681 ret = drm_vblank_get(dev, crtc);
682 if (ret) {
Paul Rolland8d3457e2009-08-09 12:24:01 +1000683 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700684 return ret;
685 }
686 seq = drm_vblank_count(dev, crtc);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000687
Eric Anholtc153f452007-09-03 12:06:45 +1000688 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 case _DRM_VBLANK_RELATIVE:
Eric Anholtc153f452007-09-03 12:06:45 +1000690 vblwait->request.sequence += seq;
691 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 case _DRM_VBLANK_ABSOLUTE:
693 break;
694 default:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700695 ret = -EINVAL;
696 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000699 if (flags & _DRM_VBLANK_EVENT)
700 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
701
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000702 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Eric Anholtc153f452007-09-03 12:06:45 +1000703 (seq - vblwait->request.sequence) <= (1<<23)) {
704 vblwait->request.sequence = seq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000705 }
706
Eric Anholt30b23632009-01-27 21:19:41 -0800707 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
708 vblwait->request.sequence, crtc);
709 dev->last_vblank_wait[crtc] = vblwait->request.sequence;
710 DRM_WAIT_ON(ret, dev->vbl_queue[crtc], 3 * DRM_HZ,
711 (((drm_vblank_count(dev, crtc) -
712 vblwait->request.sequence) <= (1 << 23)) ||
713 !dev->irq_enabled));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Eric Anholt30b23632009-01-27 21:19:41 -0800715 if (ret != -EINTR) {
716 struct timeval now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Eric Anholt30b23632009-01-27 21:19:41 -0800718 do_gettimeofday(&now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Eric Anholt30b23632009-01-27 21:19:41 -0800720 vblwait->reply.tval_sec = now.tv_sec;
721 vblwait->reply.tval_usec = now.tv_usec;
722 vblwait->reply.sequence = drm_vblank_count(dev, crtc);
723 DRM_DEBUG("returning %d to client\n",
724 vblwait->reply.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 } else {
Eric Anholt30b23632009-01-27 21:19:41 -0800726 DRM_DEBUG("vblank wait interrupted by signal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700729done:
730 drm_vblank_put(dev, crtc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return ret;
732}
733
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000734void drm_handle_vblank_events(struct drm_device *dev, int crtc)
735{
736 struct drm_pending_vblank_event *e, *t;
737 struct timeval now;
738 unsigned long flags;
739 unsigned int seq;
740
741 do_gettimeofday(&now);
742 seq = drm_vblank_count(dev, crtc);
743
744 spin_lock_irqsave(&dev->event_lock, flags);
745
746 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
747 if (e->pipe != crtc)
748 continue;
749 if ((seq - e->event.sequence) > (1<<23))
750 continue;
751
752 DRM_DEBUG("vblank event on %d, current %d\n",
753 e->event.sequence, seq);
754
755 e->event.sequence = seq;
756 e->event.tv_sec = now.tv_sec;
757 e->event.tv_usec = now.tv_usec;
758 drm_vblank_put(dev, e->pipe);
759 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
760 wake_up_interruptible(&e->base.file_priv->event_wait);
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -0700761 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
762 e->event.sequence);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000763 }
764
765 spin_unlock_irqrestore(&dev->event_lock, flags);
Jesse Barnesac2874b2010-07-01 16:47:31 -0700766
767 trace_drm_vblank_event(crtc, seq);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700771 * drm_handle_vblank - handle a vblank event
772 * @dev: DRM device
773 * @crtc: where this event occurred
774 *
775 * Drivers should call this routine in their vblank interrupt handlers to
776 * update the vblank counter and send any signals that may be pending.
777 */
778void drm_handle_vblank(struct drm_device *dev, int crtc)
779{
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000780 if (!dev->num_crtcs)
781 return;
782
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700783 atomic_inc(&dev->_vblank_count[crtc]);
784 DRM_WAKEUP(&dev->vbl_queue[crtc]);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000785 drm_handle_vblank_events(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700786}
787EXPORT_SYMBOL(drm_handle_vblank);