1 /*
2 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
3 */
4
5 /**
6 * \file drm_irq.c
7 * IRQ support
8 *
9 * \author Rickard E. (Rik) Faith <faith@valinux.com>
10 * \author Gareth Hughes <gareth@valinux.com>
11 */
12
13 /*
14 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
15 *
16 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
17 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
18 * Copyright (c) 2009, 2013, Intel Corporation.
19 * All Rights Reserved.
20 *
21 * Permission is hereby granted, free of charge, to any person obtaining a
22 * copy of this software and associated documentation files (the "Software"),
23 * to deal in the Software without restriction, including without limitation
24 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
25 * and/or sell copies of the Software, and to permit persons to whom the
26 * Software is furnished to do so, subject to the following conditions:
27 *
28 * The above copyright notice and this permission notice (including the next
29 * paragraph) shall be included in all copies or substantial portions of the
30 * Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
35 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
36 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
37 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
38 * OTHER DEALINGS IN THE SOFTWARE.
39 */
40
41 #include "drm.h"
42 #include "drmP.h"
43 #include "drm_io32.h"
44
__irq_handler_wrap(DRM_IRQ_ARGS)45 static irqreturn_t __irq_handler_wrap(DRM_IRQ_ARGS)
46 {
47 drm_device_t *dev = (void *)arg;
48 int ret;
49
50 mutex_enter(&dev->irq_lock);
51 ret = dev->driver->irq_handler(arg);
52 mutex_exit(&dev->irq_lock);
53
54 return (ret);
55 }
56
57 /* LINTED */
__irq_handler_wrap_msi(caddr_t arg1,caddr_t arg2)58 static irqreturn_t __irq_handler_wrap_msi(caddr_t arg1, caddr_t arg2)
59 {
60 drm_device_t *dev = (void *)arg1;
61 int ret;
62
63 mutex_enter(&dev->irq_lock);
64 ret = dev->driver->irq_handler(arg1);
65 mutex_exit(&dev->irq_lock);
66
67 return (ret);
68 }
69
__install_irq_handler(struct drm_device * dev)70 static int __install_irq_handler(struct drm_device *dev)
71 {
72 struct pci_dev *pdev = dev->pdev;
73 int i, ret;
74
75 if (pdev->msi_handle) {
76 /* Call ddi_intr_add_handler() */
77 for (i = 0; i < pdev->msi_actual; i++) {
78 ret = ddi_intr_add_handler(pdev->msi_handle[i],
79 __irq_handler_wrap_msi, (caddr_t)dev, NULL);
80 if (ret != DDI_SUCCESS) {
81 DRM_DEBUG("ddi_intr_add_handler() failed");
82 return (ret);
83 }
84 }
85
86 if (pdev->msi_flag & DDI_INTR_FLAG_BLOCK) {
87 /* Call ddi_intr_block_enable() for MSI */
88 (void) ddi_intr_block_enable(pdev->msi_handle, pdev->msi_actual);
89 } else {
90 /* Call ddi_intr_enable() for MSI non block enable */
91 for (i = 0; i < pdev->msi_actual; i++)
92 (void) ddi_intr_enable(pdev->msi_handle[i]);
93 }
94 } else {
95 /* setup the interrupt handler */
96 if (ddi_add_intr(dev->devinfo, 0, &pdev->intr_block,
97 (ddi_idevice_cookie_t *)NULL, __irq_handler_wrap,
98 (caddr_t)dev) != DDI_SUCCESS) {
99 DRM_ERROR("ddi_add_intr failed");
100 return (DDI_FAILURE);
101 }
102 }
103
104 return (DDI_SUCCESS);
105 }
106
__uninstall_irq_handler(struct drm_device * dev)107 static void __uninstall_irq_handler(struct drm_device *dev)
108 {
109 struct pci_dev *pdev = dev->pdev;
110 int i;
111
112 ASSERT(dev->devinfo);
113
114 if (pdev->msi_handle) {
115 /* Disable all interrupts */
116 if (pdev->msi_flag & DDI_INTR_FLAG_BLOCK) {
117 /* Call ddi_intr_block_disable() */
118 (void) ddi_intr_block_disable(pdev->msi_handle, pdev->msi_actual);
119 } else {
120 for (i = 0; i < pdev->msi_actual; i++)
121 (void) ddi_intr_disable(pdev->msi_handle[i]);
122 }
123
124 /* Call ddi_intr_remove_handler() */
125 for (i = 0; i < pdev->msi_actual; i++){
126 (void) ddi_intr_remove_handler(pdev->msi_handle[i]);
127 }
128 } else {
129 ddi_remove_intr(dev->devinfo, 0, pdev->intr_block);
130 }
131 }
132
133 int
pci_enable_msi(struct pci_dev * pdev)134 pci_enable_msi(struct pci_dev *pdev)
135 {
136 struct drm_device *dev = pdev->dev;
137 dev_info_t *devinfo = dev->devinfo;
138 int count, avail, actual;
139 int types;
140 int i, ret;
141
142 /* Get supported interrupt types */
143 if (ddi_intr_get_supported_types(dev->devinfo, &types) != DDI_SUCCESS) {
144 DRM_DEBUG("ddi_intr_get_supported_types() failed");
145 return (DDI_FAILURE);
146 }
147 if (!(types & DDI_INTR_TYPE_MSI))
148 return (DDI_FAILURE);
149
150 /* Get number of interrupts */
151 ret = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_MSI, &count);
152 if ((ret != DDI_SUCCESS) || (count == 0)) {
153 DRM_DEBUG("ddi_intr_get_nintrs() failed, "
154 "ret: %d, count: %d", ret, count);
155 return (ret);
156 }
157
158 /* Get number of available interrupts */
159 ret = ddi_intr_get_navail(devinfo, DDI_INTR_TYPE_MSI, &avail);
160 if ((ret != DDI_SUCCESS) || (avail == 0)) {
161 DRM_DEBUG("ddi_intr_get_navail() failed, "
162 "ret: %d, avail: %d", ret, avail);
163 return (ret);
164 }
165
166 if (avail < count) {
167 DRM_DEBUG("nitrs() returned %d, navail returned %d",
168 count, avail);
169 }
170
171 /* Allocate memory for MSI interrupts */
172 pdev->msi_size = count * sizeof (ddi_intr_handle_t);
173 pdev->msi_handle = kmem_alloc(pdev->msi_size, KM_SLEEP);
174
175 ret = ddi_intr_alloc(devinfo, pdev->msi_handle, DDI_INTR_TYPE_MSI, 0,
176 count, &actual, DDI_INTR_ALLOC_NORMAL);
177
178 if ((ret != DDI_SUCCESS) || (actual == 0)) {
179 DRM_DEBUG("ddi_intr_alloc() failed: %d", ret);
180 kmem_free(pdev->msi_handle, pdev->msi_size);
181 return (ret);
182 }
183 pdev->msi_actual = actual;
184
185 /*
186 * Get priority for first msi, assume remaining are all the same
187 */
188 ret = ddi_intr_get_pri(pdev->msi_handle[0], &pdev->msi_pri);
189 if (ret != DDI_SUCCESS) {
190 DRM_DEBUG("ddi_intr_get_pri() failed: %d", ret);
191 for(i = 0; i < actual; i++)
192 (void) ddi_intr_free(pdev->msi_handle[i]);
193 kmem_free(pdev->msi_handle, pdev->msi_size);
194 return (ret);
195 }
196
197 ret = ddi_intr_get_cap(pdev->msi_handle[0], &pdev->msi_flag);
198 if (ret != DDI_SUCCESS) {
199 DRM_DEBUG("ddi_intr_get_cap() failed: %d", ret);
200 for(i = 0; i < actual; i++)
201 (void) ddi_intr_free(pdev->msi_handle[i]);
202 kmem_free(pdev->msi_handle, pdev->msi_size);
203 return (ret);
204 }
205
206 return (ret);
207 }
208
209 void
pci_disable_msi(struct pci_dev * pdev)210 pci_disable_msi(struct pci_dev *pdev)
211 {
212 int i;
213
214 for (i = 0; i < pdev->msi_actual; i++)
215 (void) ddi_intr_free(pdev->msi_handle[i]);
216 kmem_free(pdev->msi_handle, pdev->msi_size);
217 pdev->msi_handle = NULL;
218 }
219
220 /* Access macro for slots in vblank timestamp ringbuffer. */
221 #define vblanktimestamp(dev, crtc, count) ( \
222 (dev)->_vblank_time[(crtc) * DRM_VBLANKTIME_RBSIZE + \
223 ((count) % DRM_VBLANKTIME_RBSIZE)])
224
225 /* Retry timestamp calculation up to 3 times to satisfy
226 * drm_timestamp_precision before giving up.
227 */
228 #define DRM_TIMESTAMP_MAXRETRIES 3
229
230 /* Threshold in nanoseconds for detection of redundant
231 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
232 */
233 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
234
235 /**
236 * Get interrupt from bus id.
237 *
238 * \param inode device inode.
239 * \param file_priv DRM file private.
240 * \param cmd command.
241 * \param arg user argument, pointing to a drm_irq_busid structure.
242 * \return zero on success or a negative number on failure.
243 *
244 * Finds the PCI device with the specified bus id and gets its IRQ number.
245 * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
246 * to that of the device that this DRM instance attached to.
247 */
248 /* LINTED */
drm_irq_by_busid(DRM_IOCTL_ARGS)249 int drm_irq_by_busid(DRM_IOCTL_ARGS)
250 {
251 struct drm_irq_busid *p = data;
252
253 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
254 return -EINVAL;
255
256 if ((p->busnum >> 8) != dev->pdev->domain ||
257 (p->busnum & 0xff) != dev->pdev->bus ||
258 p->devnum != dev->pdev->slot || p->funcnum != dev->pdev->func)
259 return -EINVAL;
260
261 p->irq = dev->pdev->irq;
262
263 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
264 p->irq);
265
266 return 0;
267 }
268
269 /*
270 * Clear vblank timestamp buffer for a crtc.
271 */
clear_vblank_timestamps(struct drm_device * dev,int crtc)272 static void clear_vblank_timestamps(struct drm_device *dev, int crtc)
273 {
274 (void) memset(&dev->_vblank_time[crtc * DRM_VBLANKTIME_RBSIZE], -1,
275 DRM_VBLANKTIME_RBSIZE * sizeof(struct timeval));
276 }
277
278 /*
279 * Disable vblank irq's on crtc, make sure that last vblank count
280 * of hardware and corresponding consistent software vblank counter
281 * are preserved, even if there are any spurious vblank irq's after
282 * disable.
283 */
vblank_disable_and_save(struct drm_device * dev,int crtc)284 static void vblank_disable_and_save(struct drm_device *dev, int crtc)
285 {
286 unsigned long irqflags;
287 u32 vblcount;
288 s64 diff_ns;
289 int vblrc;
290 struct timeval tvblank;
291 int count = DRM_TIMESTAMP_MAXRETRIES;
292
293 /* Prevent vblank irq processing while disabling vblank irqs,
294 * so no updates of timestamps or count can happen after we've
295 * disabled. Needed to prevent races in case of delayed irq's.
296 */
297 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
298
299 dev->driver->disable_vblank(dev, crtc);
300 dev->vblank_enabled[crtc] = 0;
301
302 /* No further vblank irq's will be processed after
303 * this point. Get current hardware vblank count and
304 * vblank timestamp, repeat until they are consistent.
305 *
306 * FIXME: There is still a race condition here and in
307 * drm_update_vblank_count() which can cause off-by-one
308 * reinitialization of software vblank counter. If gpu
309 * vblank counter doesn't increment exactly at the leading
310 * edge of a vblank interval, then we can lose 1 count if
311 * we happen to execute between start of vblank and the
312 * delayed gpu counter increment.
313 */
314 do {
315 dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
316 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
317 } while (dev->last_vblank[crtc] != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
318
319 if (!count)
320 vblrc = 0;
321
322 /* Compute time difference to stored timestamp of last vblank
323 * as updated by last invocation of drm_handle_vblank() in vblank irq.
324 */
325 vblcount = atomic_read(&dev->_vblank_count[crtc]);
326 diff_ns = timeval_to_ns(&tvblank) -
327 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
328
329 /* If there is at least 1 msec difference between the last stored
330 * timestamp and tvblank, then we are currently executing our
331 * disable inside a new vblank interval, the tvblank timestamp
332 * corresponds to this new vblank interval and the irq handler
333 * for this vblank didn't run yet and won't run due to our disable.
334 * Therefore we need to do the job of drm_handle_vblank() and
335 * increment the vblank counter by one to account for this vblank.
336 *
337 * Skip this step if there isn't any high precision timestamp
338 * available. In that case we can't account for this and just
339 * hope for the best.
340 */
341 if ((vblrc > 0) && (abs(diff_ns) > 1000000)) {
342 atomic_inc(&dev->_vblank_count[crtc]);
343 }
344
345 /* Invalidate all timestamps while vblank irq's are off. */
346 clear_vblank_timestamps(dev, crtc);
347
348 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
349 }
350
vblank_disable_fn(void * arg)351 static void vblank_disable_fn(void *arg)
352 {
353 struct drm_device *dev = (struct drm_device *)arg;
354 unsigned long irqflags;
355 int i;
356
357 if (!dev->vblank_disable_allowed)
358 return;
359
360 for (i = 0; i < dev->num_crtcs; i++) {
361 spin_lock_irqsave(&dev->vbl_lock, irqflags);
362 if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
363 dev->vblank_enabled[i]) {
364 DRM_DEBUG("disabling vblank on crtc %d\n", i);
365 vblank_disable_and_save(dev, i);
366 }
367 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
368 }
369 }
370
drm_vblank_cleanup(struct drm_device * dev)371 void drm_vblank_cleanup(struct drm_device *dev)
372 {
373 /* Bail if the driver didn't call drm_vblank_init() */
374 if (dev->num_crtcs == 0)
375 return;
376
377 del_timer(&dev->vblank_disable_timer);
378 destroy_timer(&dev->vblank_disable_timer);
379
380 vblank_disable_fn((void *)dev);
381
382 kfree(dev->vbl_queue, sizeof (wait_queue_head_t) * dev->num_crtcs);
383 kfree(dev->_vblank_count, sizeof (atomic_t) * dev->num_crtcs);
384 kfree(dev->vblank_refcount, sizeof (atomic_t) * dev->num_crtcs);
385 kfree(dev->vblank_enabled, sizeof (int) * dev->num_crtcs);
386 kfree(dev->last_vblank, sizeof (u32) * dev->num_crtcs);
387 kfree(dev->last_vblank_wait, sizeof (u32) * dev->num_crtcs);
388 kfree(dev->vblank_inmodeset, sizeof (*dev->vblank_inmodeset) * dev->num_crtcs);
389 kfree(dev->_vblank_time, sizeof (*dev->_vblank_time) * dev->num_crtcs * DRM_VBLANKTIME_RBSIZE);
390
391 dev->num_crtcs = 0;
392
393 mutex_destroy(&dev->vbl_lock);
394 }
395
drm_vblank_init(struct drm_device * dev,int num_crtcs)396 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
397 {
398 int i, ret = -ENOMEM;
399
400 init_timer(&dev->vblank_disable_timer);
401 setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
402 dev);
403 mutex_init(&dev->vbl_lock, NULL, MUTEX_DRIVER, (void *)dev->pdev->intr_block);
404 spin_lock_init(&dev->vblank_time_lock);
405
406 dev->num_crtcs = num_crtcs;
407
408 dev->vbl_queue = kmalloc(sizeof(wait_queue_head_t) * num_crtcs,
409 GFP_KERNEL);
410 if (!dev->vbl_queue)
411 goto err;
412
413 dev->_vblank_count = kmalloc(sizeof(atomic_t) * num_crtcs, GFP_KERNEL);
414 if (!dev->_vblank_count)
415 goto err;
416
417 dev->vblank_refcount = kmalloc(sizeof(atomic_t) * num_crtcs,
418 GFP_KERNEL);
419 if (!dev->vblank_refcount)
420 goto err;
421
422 dev->vblank_enabled = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
423 if (!dev->vblank_enabled)
424 goto err;
425
426 dev->last_vblank = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
427 if (!dev->last_vblank)
428 goto err;
429
430 dev->last_vblank_wait = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
431 if (!dev->last_vblank_wait)
432 goto err;
433
434 dev->vblank_inmodeset = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
435 if (!dev->vblank_inmodeset)
436 goto err;
437
438 dev->_vblank_time = kcalloc(num_crtcs * DRM_VBLANKTIME_RBSIZE,
439 sizeof(struct timeval), GFP_KERNEL);
440 if (!dev->_vblank_time)
441 goto err;
442
443 DRM_INFO("Supports vblank timestamp caching Rev 1 (10.10.2010).\n");
444
445 /* Driver specific high-precision vblank timestamping supported? */
446 if (dev->driver->get_vblank_timestamp)
447 DRM_INFO("Driver supports precise vblank timestamp query.\n");
448 else
449 DRM_INFO("No driver support for vblank timestamp query.\n");
450
451 /* Zero per-crtc vblank stuff */
452 for (i = 0; i < num_crtcs; i++) {
453 DRM_INIT_WAITQUEUE(&dev->vbl_queue[i], DRM_INTR_PRI(dev));
454 atomic_set(&dev->_vblank_count[i], 0);
455 atomic_set(&dev->vblank_refcount[i], 0);
456 }
457
458 dev->vblank_disable_allowed = 0;
459
460 return 0;
461
462 err:
463 drm_vblank_cleanup(dev);
464 return ret;
465 }
466
467 /* LINTED */
drm_irq_vgaarb_nokms(void * cookie,bool state)468 static void drm_irq_vgaarb_nokms(void *cookie, bool state)
469 {
470 struct drm_device *dev = cookie;
471
472 if (dev->driver->vgaarb_irq) {
473 dev->driver->vgaarb_irq(dev, state);
474 return;
475 }
476
477 if (!dev->irq_enabled)
478 return;
479
480 if (state) {
481 if (dev->driver->irq_uninstall)
482 dev->driver->irq_uninstall(dev);
483 } else {
484 if (dev->driver->irq_preinstall)
485 dev->driver->irq_preinstall(dev);
486 if (dev->driver->irq_postinstall)
487 dev->driver->irq_postinstall(dev);
488 }
489 }
490
491 /**
492 * Install IRQ handler.
493 *
494 * \param dev DRM device.
495 *
496 * Initializes the IRQ related data. Installs the handler, calling the driver
497 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
498 * before and after the installation.
499 */
drm_irq_install(struct drm_device * dev)500 int drm_irq_install(struct drm_device *dev)
501 {
502 int ret;
503
504 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
505 return -EINVAL;
506
507 if (dev->pdev->irq == 0)
508 return -EINVAL;
509
510 mutex_lock(&dev->struct_mutex);
511
512 /* Driver must have been initialized */
513 if (!dev->dev_private) {
514 mutex_unlock(&dev->struct_mutex);
515 return -EINVAL;
516 }
517
518 if (dev->irq_enabled) {
519 mutex_unlock(&dev->struct_mutex);
520 return -EBUSY;
521 }
522 dev->irq_enabled = 1;
523 mutex_unlock(&dev->struct_mutex);
524
525 DRM_DEBUG("irq=%d\n", dev->pdev->irq);
526
527 /* Before installing handler */
528 if (dev->driver->irq_preinstall)
529 dev->driver->irq_preinstall(dev);
530
531 /* Install handler */
532 ret = __install_irq_handler(dev);
533 if (ret != DDI_SUCCESS) {
534 DRM_ERROR("IRQ handler installation failed");
535 mutex_lock(&dev->struct_mutex);
536 dev->irq_enabled = 0;
537 mutex_unlock(&dev->struct_mutex);
538 return -EFAULT;
539 }
540
541 /* After installing handler */
542 if (dev->driver->irq_postinstall)
543 ret = dev->driver->irq_postinstall(dev);
544 if (ret < 0) {
545 mutex_lock(&dev->struct_mutex);
546 dev->irq_enabled = 0;
547 mutex_unlock(&dev->struct_mutex);
548 return ret;
549 }
550
551 dev->context_flag = 0;
552 return 0;
553 }
554
555 /**
556 * Uninstall the IRQ handler.
557 *
558 * \param dev DRM device.
559 *
560 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
561 */
drm_irq_uninstall(struct drm_device * dev)562 int drm_irq_uninstall(struct drm_device * dev)
563 {
564 unsigned long irqflags;
565 int irq_enabled, i;
566
567 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
568 return -EINVAL;
569
570 mutex_lock(&dev->struct_mutex);
571 irq_enabled = dev->irq_enabled;
572 dev->irq_enabled = 0;
573 mutex_unlock(&dev->struct_mutex);
574
575 /*
576 * Wake up any waiters so they don't hang.
577 */
578 if (dev->num_crtcs) {
579 spin_lock_irqsave(&dev->vbl_lock, irqflags);
580 for (i = 0; i < dev->num_crtcs; i++) {
581 DRM_WAKEUP(&dev->vbl_queue[i]);
582 dev->vblank_enabled[i] = 0;
583 dev->last_vblank[i] =
584 dev->driver->get_vblank_counter(dev, i);
585 }
586 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
587 }
588
589 if (!irq_enabled)
590 return -EINVAL;
591
592 DRM_DEBUG("irq=%d\n", dev->pdev->irq);
593
594 if (dev->driver->irq_uninstall)
595 dev->driver->irq_uninstall(dev);
596
597 __uninstall_irq_handler(dev);
598
599 return 0;
600 }
601
602 /**
603 * IRQ control ioctl.
604 *
605 * \param inode device inode.
606 * \param file_priv DRM file private.
607 * \param cmd command.
608 * \param arg user argument, pointing to a drm_control structure.
609 * \return zero on success or a negative number on failure.
610 *
611 * Calls irq_install() or irq_uninstall() according to \p arg.
612 */
613 /* LINTED */
drm_control(DRM_IOCTL_ARGS)614 int drm_control(DRM_IOCTL_ARGS)
615 {
616 struct drm_control *ctl = data;
617
618 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
619
620
621 switch (ctl->func) {
622 case DRM_INST_HANDLER:
623 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
624 return 0;
625 if (drm_core_check_feature(dev, DRIVER_MODESET))
626 return 0;
627 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
628 ctl->irq != dev->pdev->irq)
629 return -EINVAL;
630 return drm_irq_install(dev);
631 case DRM_UNINST_HANDLER:
632 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
633 return 0;
634 if (drm_core_check_feature(dev, DRIVER_MODESET))
635 return 0;
636 return drm_irq_uninstall(dev);
637 default:
638 return -EINVAL;
639 }
640 }
641
642 /**
643 * drm_calc_timestamping_constants - Calculate and
644 * store various constants which are later needed by
645 * vblank and swap-completion timestamping, e.g, by
646 * drm_calc_vbltimestamp_from_scanoutpos().
647 * They are derived from crtc's true scanout timing,
648 * so they take things like panel scaling or other
649 * adjustments into account.
650 *
651 * @crtc drm_crtc whose timestamp constants should be updated.
652 *
653 */
drm_calc_timestamping_constants(struct drm_crtc * crtc)654 void drm_calc_timestamping_constants(struct drm_crtc *crtc)
655 {
656 s64 linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
657 u64 dotclock;
658
659 /* Dot clock in Hz: */
660 dotclock = (u64) crtc->hwmode.clock * 1000;
661
662 /* Fields of interlaced scanout modes are only halve a frame duration.
663 * Double the dotclock to get halve the frame-/line-/pixelduration.
664 */
665 if (crtc->hwmode.flags & DRM_MODE_FLAG_INTERLACE)
666 dotclock *= 2;
667
668 /* Valid dotclock? */
669 if (dotclock > 0) {
670 int frame_size;
671 /* Convert scanline length in pixels and video dot clock to
672 * line duration, frame duration and pixel duration in
673 * nanoseconds:
674 */
675 pixeldur_ns = (s64) div_u64(1000000000, dotclock);
676 linedur_ns = (s64) div_u64(((u64) crtc->hwmode.crtc_htotal *
677 1000000000), dotclock);
678 frame_size = crtc->hwmode.crtc_htotal *
679 crtc->hwmode.crtc_vtotal;
680 framedur_ns = (s64) div_u64((u64) frame_size * 1000000000,
681 dotclock);
682 } else
683 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
684 crtc->base.id);
685
686 crtc->pixeldur_ns = pixeldur_ns;
687 crtc->linedur_ns = linedur_ns;
688 crtc->framedur_ns = framedur_ns;
689
690 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
691 crtc->base.id, crtc->hwmode.crtc_htotal,
692 crtc->hwmode.crtc_vtotal, crtc->hwmode.crtc_vdisplay);
693 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
694 crtc->base.id, (int) dotclock/1000, (int) framedur_ns,
695 (int) linedur_ns, (int) pixeldur_ns);
696 }
697
698 /**
699 * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
700 * drivers. Implements calculation of exact vblank timestamps from
701 * given drm_display_mode timings and current video scanout position
702 * of a crtc. This can be called from within get_vblank_timestamp()
703 * implementation of a kms driver to implement the actual timestamping.
704 *
705 * Should return timestamps conforming to the OML_sync_control OpenML
706 * extension specification. The timestamp corresponds to the end of
707 * the vblank interval, aka start of scanout of topmost-leftmost display
708 * pixel in the following video frame.
709 *
710 * Requires support for optional dev->driver->get_scanout_position()
711 * in kms driver, plus a bit of setup code to provide a drm_display_mode
712 * that corresponds to the true scanout timing.
713 *
714 * The current implementation only handles standard video modes. It
715 * returns as no operation if a doublescan or interlaced video mode is
716 * active. Higher level code is expected to handle this.
717 *
718 * @dev: DRM device.
719 * @crtc: Which crtc's vblank timestamp to retrieve.
720 * @max_error: Desired maximum allowable error in timestamps (nanosecs).
721 * On return contains true maximum error of timestamp.
722 * @vblank_time: Pointer to struct timeval which should receive the timestamp.
723 * @flags: Flags to pass to driver:
724 * 0 = Default.
725 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
726 * @refcrtc: drm_crtc* of crtc which defines scanout timing.
727 *
728 * Returns negative value on error, failure or if not supported in current
729 * video mode:
730 *
731 * -EINVAL - Invalid crtc.
732 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
733 * -ENOTSUPP - Function not supported in current display mode.
734 * -EIO - Failed, e.g., due to failed scanout position query.
735 *
736 * Returns or'ed positive status flags on success:
737 *
738 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
739 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
740 *
741 */
drm_calc_vbltimestamp_from_scanoutpos(struct drm_device * dev,int crtc,int * max_error,struct timeval * vblank_time,unsigned flags,struct drm_crtc * refcrtc)742 int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
743 int *max_error,
744 struct timeval *vblank_time,
745 unsigned flags,
746 struct drm_crtc *refcrtc)
747 {
748 struct timeval stime, raw_time;
749 struct drm_display_mode *mode;
750 int vbl_status, vtotal, vdisplay;
751 int vpos, hpos, i;
752 s64 framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
753 bool invbl;
754
755 if (crtc < 0 || crtc >= dev->num_crtcs) {
756 DRM_ERROR("Invalid crtc %d\n", crtc);
757 return -EINVAL;
758 }
759
760 /* Scanout position query not supported? Should not happen. */
761 if (!dev->driver->get_scanout_position) {
762 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
763 return -EIO;
764 }
765
766 mode = &refcrtc->hwmode;
767 vtotal = mode->crtc_vtotal;
768 vdisplay = mode->crtc_vdisplay;
769
770 /* Durations of frames, lines, pixels in nanoseconds. */
771 framedur_ns = refcrtc->framedur_ns;
772 linedur_ns = refcrtc->linedur_ns;
773 pixeldur_ns = refcrtc->pixeldur_ns;
774
775 /* If mode timing undefined, just return as no-op:
776 * Happens during initial modesetting of a crtc.
777 */
778 if (vtotal <= 0 || vdisplay <= 0 || framedur_ns == 0) {
779 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
780 return -EAGAIN;
781 }
782
783 /* Get current scanout position with system timestamp.
784 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
785 * if single query takes longer than max_error nanoseconds.
786 *
787 * This guarantees a tight bound on maximum error if
788 * code gets preempted or delayed for some reason.
789 */
790 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
791 /* Disable preemption to make it very likely to
792 * succeed in the first iteration even on PREEMPT_RT kernel.
793 */
794
795 /* Get system timestamp before query. */
796 do_gettimeofday(&stime);
797
798 /* Get vertical and horizontal scanout pos. vpos, hpos. */
799 vbl_status = dev->driver->get_scanout_position(dev, crtc, &vpos, &hpos);
800
801 /* Get system timestamp after query. */
802 do_gettimeofday(&raw_time);
803
804 /* Return as no-op if scanout query unsupported or failed. */
805 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
806 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
807 crtc, vbl_status);
808 return -EIO;
809 }
810
811 duration_ns = timeval_to_ns(&raw_time) - timeval_to_ns(&stime);
812
813 /* Accept result with < max_error nsecs timing uncertainty. */
814 if (duration_ns <= (s64) *max_error)
815 break;
816 }
817
818 /* Noisy system timing? */
819 if (i == DRM_TIMESTAMP_MAXRETRIES) {
820 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
821 crtc, (int) duration_ns/1000, *max_error/1000, i);
822 }
823
824 /* Return upper bound of timestamp precision error. */
825 *max_error = (int) duration_ns;
826
827 /* Check if in vblank area:
828 * vpos is >=0 in video scanout area, but negative
829 * within vblank area, counting down the number of lines until
830 * start of scanout.
831 */
832 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
833
834 /* Convert scanout position into elapsed time at raw_time query
835 * since start of scanout at first display scanline. delta_ns
836 * can be negative if start of scanout hasn't happened yet.
837 */
838 delta_ns = (s64) vpos * linedur_ns + (s64) hpos * pixeldur_ns;
839
840 /* Is vpos outside nominal vblank area, but less than
841 * 1/100 of a frame height away from start of vblank?
842 * If so, assume this isn't a massively delayed vblank
843 * interrupt, but a vblank interrupt that fired a few
844 * microseconds before true start of vblank. Compensate
845 * by adding a full frame duration to the final timestamp.
846 * Happens, e.g., on ATI R500, R600.
847 *
848 * We only do this if DRM_CALLED_FROM_VBLIRQ.
849 */
850 if ((flags & DRM_CALLED_FROM_VBLIRQ) && !invbl &&
851 ((vdisplay - vpos) < vtotal / 100)) {
852 delta_ns = delta_ns - framedur_ns;
853
854 /* Signal this correction as "applied". */
855 vbl_status |= 0x8;
856 }
857
858 /* Subtract time delta from raw timestamp to get final
859 * vblank_time timestamp for end of vblank.
860 */
861 ns_to_timeval(timeval_to_ns(&raw_time) - delta_ns, vblank_time);
862
863 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
864 crtc, (int) vbl_status, hpos, vpos, raw_time.tv_sec,
865 raw_time.tv_usec, vblank_time->tv_sec, vblank_time->tv_usec,
866 (int) duration_ns/1000, i);
867
868 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
869 if (invbl)
870 vbl_status |= DRM_VBLANKTIME_INVBL;
871
872 return vbl_status;
873 }
874
875 /**
876 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
877 * vblank interval.
878 *
879 * @dev: DRM device
880 * @crtc: which crtc's vblank timestamp to retrieve
881 * @tvblank: Pointer to target struct timeval which should receive the timestamp
882 * @flags: Flags to pass to driver:
883 * 0 = Default.
884 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
885 *
886 * Fetches the system timestamp corresponding to the time of the most recent
887 * vblank interval on specified crtc. May call into kms-driver to
888 * compute the timestamp with a high-precision GPU specific method.
889 *
890 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
891 * call, i.e., it isn't very precisely locked to the true vblank.
892 *
893 * Returns non-zero if timestamp is considered to be very precise.
894 */
drm_get_last_vbltimestamp(struct drm_device * dev,int crtc,struct timeval * tvblank,unsigned flags)895 u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
896 struct timeval *tvblank, unsigned flags)
897 {
898 int ret = 0;
899
900 /* Define requested maximum error on timestamps (nanoseconds). */
901 int max_error = (int) drm_timestamp_precision * 1000;
902
903 /* Query driver if possible and precision timestamping enabled. */
904 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
905 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
906 tvblank, flags);
907 if (ret > 0)
908 return (u32) ret;
909 }
910
911 /* GPU high precision timestamp query unsupported or failed.
912 * Return gettimeofday timestamp as best estimate.
913 */
914 do_gettimeofday(tvblank);
915
916 return 0;
917 }
918
919 /**
920 * drm_vblank_count - retrieve "cooked" vblank counter value
921 * @dev: DRM device
922 * @crtc: which counter to retrieve
923 *
924 * Fetches the "cooked" vblank count value that represents the number of
925 * vblank events since the system was booted, including lost events due to
926 * modesetting activity.
927 */
drm_vblank_count(struct drm_device * dev,int crtc)928 u32 drm_vblank_count(struct drm_device *dev, int crtc)
929 {
930 return atomic_read(&dev->_vblank_count[crtc]);
931 }
932
933 /**
934 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
935 * and the system timestamp corresponding to that vblank counter value.
936 *
937 * @dev: DRM device
938 * @crtc: which counter to retrieve
939 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
940 *
941 * Fetches the "cooked" vblank count value that represents the number of
942 * vblank events since the system was booted, including lost events due to
943 * modesetting activity. Returns corresponding system timestamp of the time
944 * of the vblank interval that corresponds to the current value vblank counter
945 * value.
946 */
drm_vblank_count_and_time(struct drm_device * dev,int crtc,struct timeval * vblanktime)947 u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
948 struct timeval *vblanktime)
949 {
950 u32 cur_vblank;
951
952 /* Read timestamp from slot of _vblank_time ringbuffer
953 * that corresponds to current vblank count. Retry if
954 * count has incremented during readout. This works like
955 * a seqlock.
956 */
957 do {
958 cur_vblank = atomic_read(&dev->_vblank_count[crtc]);
959 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
960 } while (cur_vblank != atomic_read(&dev->_vblank_count[crtc]));
961
962 return cur_vblank;
963 }
964
965 /* LINTED */
send_vblank_event(struct drm_device * dev,struct drm_pending_vblank_event * e,unsigned long seq,struct timeval * now)966 static void send_vblank_event(struct drm_device *dev,
967 struct drm_pending_vblank_event *e,
968 unsigned long seq, struct timeval *now)
969 {
970 e->event.sequence = (u32) seq;
971 e->event.tv_sec = now->tv_sec;
972 e->event.tv_usec = now->tv_usec;
973
974 list_add_tail(&e->base.link, &e->base.file_priv->event_list, (caddr_t)&e->base);
975 DRM_WAKEUP(&e->base.file_priv->event_wait);
976 }
977
978 /**
979 * drm_send_vblank_event - helper to send vblank event after pageflip
980 * @dev: DRM device
981 * @crtc: CRTC in question
982 * @e: the event to send
983 *
984 * Updates sequence # and timestamp on event, and sends it to userspace.
985 * Caller must hold event lock.
986 */
drm_send_vblank_event(struct drm_device * dev,int crtc,struct drm_pending_vblank_event * e)987 void drm_send_vblank_event(struct drm_device *dev, int crtc,
988 struct drm_pending_vblank_event *e)
989 {
990 struct timeval now;
991 unsigned int seq;
992 if (crtc >= 0) {
993 seq = drm_vblank_count_and_time(dev, crtc, &now);
994 } else {
995 seq = 0;
996
997 do_gettimeofday(&now);
998 }
999 e->pipe = crtc;
1000 send_vblank_event(dev, e, seq, &now);
1001 }
1002
1003 /**
1004 * drm_update_vblank_count - update the master vblank counter
1005 * @dev: DRM device
1006 * @crtc: counter to update
1007 *
1008 * Call back into the driver to update the appropriate vblank counter
1009 * (specified by @crtc). Deal with wraparound, if it occurred, and
1010 * update the last read value so we can deal with wraparound on the next
1011 * call if necessary.
1012 *
1013 * Only necessary when going from off->on, to account for frames we
1014 * didn't get an interrupt for.
1015 *
1016 * Note: caller must hold dev->vbl_lock since this reads & writes
1017 * device vblank fields.
1018 */
drm_update_vblank_count(struct drm_device * dev,int crtc)1019 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
1020 {
1021 u32 cur_vblank, diff, tslot, rc;
1022 struct timeval t_vblank;
1023
1024 /*
1025 * Interrupts were disabled prior to this call, so deal with counter
1026 * wrap if needed.
1027 * NOTE! It's possible we lost a full dev->max_vblank_count events
1028 * here if the register is small or we had vblank interrupts off for
1029 * a long time.
1030 *
1031 * We repeat the hardware vblank counter & timestamp query until
1032 * we get consistent results. This to prevent races between gpu
1033 * updating its hardware counter while we are retrieving the
1034 * corresponding vblank timestamp.
1035 */
1036 do {
1037 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
1038 rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
1039 } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
1040
1041 /* Deal with counter wrap */
1042 diff = cur_vblank - dev->last_vblank[crtc];
1043 if (cur_vblank < dev->last_vblank[crtc]) {
1044 diff += dev->max_vblank_count;
1045
1046 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
1047 crtc, dev->last_vblank[crtc], cur_vblank, diff);
1048 }
1049
1050 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
1051 crtc, diff);
1052
1053 /* Reinitialize corresponding vblank timestamp if high-precision query
1054 * available. Skip this step if query unsupported or failed. Will
1055 * reinitialize delayed at next vblank interrupt in that case.
1056 */
1057 if (rc) {
1058 tslot = atomic_read(&dev->_vblank_count[crtc]) + diff;
1059 vblanktimestamp(dev, crtc, tslot) = t_vblank;
1060 }
1061
1062 atomic_add(diff, &dev->_vblank_count[crtc]);
1063 }
1064
1065 /**
1066 * drm_vblank_get - get a reference count on vblank events
1067 * @dev: DRM device
1068 * @crtc: which CRTC to own
1069 *
1070 * Acquire a reference count on vblank events to avoid having them disabled
1071 * while in use.
1072 *
1073 * RETURNS
1074 * Zero on success, nonzero on failure.
1075 */
drm_vblank_get(struct drm_device * dev,int crtc)1076 int drm_vblank_get(struct drm_device *dev, int crtc)
1077 {
1078 unsigned long irqflags, irqflags2;
1079 int ret = 0;
1080
1081 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1082 /* Going from 0->1 means we have to enable interrupts again */
1083 if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1) {
1084 spin_lock_irqsave(&dev->vblank_time_lock, irqflags2);
1085 if (!dev->vblank_enabled[crtc]) {
1086 ret = dev->driver->enable_vblank(dev, crtc);
1087 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
1088 crtc, ret);
1089 if (ret)
1090 atomic_dec(&dev->vblank_refcount[crtc]);
1091 else {
1092 dev->vblank_enabled[crtc] = 1;
1093 drm_update_vblank_count(dev, crtc);
1094 }
1095 }
1096 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags2);
1097 } else {
1098 if (!dev->vblank_enabled[crtc]) {
1099 atomic_dec(&dev->vblank_refcount[crtc]);
1100 ret = -EINVAL;
1101 }
1102 }
1103 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1104
1105 return ret;
1106 }
1107
1108 /**
1109 * drm_vblank_put - give up ownership of vblank events
1110 * @dev: DRM device
1111 * @crtc: which counter to give up
1112 *
1113 * Release ownership of a given vblank counter, turning off interrupts
1114 * if possible.
1115 */
drm_vblank_put(struct drm_device * dev,int crtc)1116 void drm_vblank_put(struct drm_device *dev, int crtc)
1117 {
1118 BUG_ON (atomic_read (&dev->vblank_refcount[crtc]) == 0);
1119
1120 /* Last user schedules interrupt disable */
1121 if (atomic_dec_and_test(&dev->vblank_refcount[crtc]) &&
1122 (drm_vblank_offdelay > 0))
1123 mod_timer(&dev->vblank_disable_timer,
1124 ((drm_vblank_offdelay * DRM_HZ)/1000));
1125 }
1126
1127 /**
1128 * drm_vblank_off - disable vblank events on a CRTC
1129 * @dev: DRM device
1130 * @crtc: CRTC in question
1131 *
1132 * Caller must hold event lock.
1133 */
drm_vblank_off(struct drm_device * dev,int crtc)1134 void drm_vblank_off(struct drm_device *dev, int crtc)
1135 {
1136 unsigned long irqflags;
1137 struct drm_pending_vblank_event *e, *t;
1138 struct timeval now;
1139 unsigned int seq;
1140
1141 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1142 vblank_disable_and_save(dev, crtc);
1143 DRM_WAKEUP(&dev->vbl_queue[crtc]);
1144
1145 /* Send any queued vblank events, lest the natives grow disquiet */
1146 seq = drm_vblank_count_and_time(dev, crtc, &now);
1147
1148 spin_lock(&dev->event_lock);
1149 list_for_each_entry_safe(e, t, struct drm_pending_vblank_event,
1150 &dev->vblank_event_list, base.link) {
1151 if (e->pipe != crtc)
1152 continue;
1153 DRM_DEBUG("Sending premature vblank event on disable: \
1154 wanted %d, current %d\n",
1155 e->event.sequence, seq);
1156 list_del(&e->base.link);
1157 drm_vblank_put(dev, e->pipe);
1158 send_vblank_event(dev, e, seq, &now);
1159 }
1160 spin_unlock(&dev->event_lock);
1161
1162 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1163 }
1164
1165 /**
1166 * drm_vblank_pre_modeset - account for vblanks across mode sets
1167 * @dev: DRM device
1168 * @crtc: CRTC in question
1169 * @post: post or pre mode set?
1170 *
1171 * Account for vblank events across mode setting events, which will likely
1172 * reset the hardware frame counter.
1173 */
drm_vblank_pre_modeset(struct drm_device * dev,int crtc)1174 void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
1175 {
1176 /* vblank is not initialized (IRQ not installed ?) */
1177 if (!dev->num_crtcs)
1178 return;
1179 /*
1180 * To avoid all the problems that might happen if interrupts
1181 * were enabled/disabled around or between these calls, we just
1182 * have the kernel take a reference on the CRTC (just once though
1183 * to avoid corrupting the count if multiple, mismatch calls occur),
1184 * so that interrupts remain enabled in the interim.
1185 */
1186 if (!dev->vblank_inmodeset[crtc]) {
1187 dev->vblank_inmodeset[crtc] = 0x1;
1188 if (drm_vblank_get(dev, crtc) == 0)
1189 dev->vblank_inmodeset[crtc] |= 0x2;
1190 }
1191 }
1192
drm_vblank_post_modeset(struct drm_device * dev,int crtc)1193 void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
1194 {
1195 unsigned long irqflags;
1196
1197 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1198 if (!dev->num_crtcs)
1199 return;
1200
1201 if (dev->vblank_inmodeset[crtc]) {
1202 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1203 dev->vblank_disable_allowed = 1;
1204 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1205
1206 if (dev->vblank_inmodeset[crtc] & 0x2)
1207 drm_vblank_put(dev, crtc);
1208
1209 dev->vblank_inmodeset[crtc] = 0;
1210 }
1211 }
1212
1213 /**
1214 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1215 * @DRM_IOCTL_ARGS: standard ioctl arguments
1216 *
1217 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1218 * ioctls around modesetting so that any lost vblank events are accounted for.
1219 *
1220 * Generally the counter will reset across mode sets. If interrupts are
1221 * enabled around this call, we don't have to do anything since the counter
1222 * will have already been incremented.
1223 */
1224 /* LINTED */
drm_modeset_ctl(DRM_IOCTL_ARGS)1225 int drm_modeset_ctl(DRM_IOCTL_ARGS)
1226 {
1227 struct drm_modeset_ctl *modeset = data;
1228 unsigned int crtc;
1229
1230 /* If drm_vblank_init() hasn't been called yet, just no-op */
1231 if (!dev->num_crtcs)
1232 return 0;
1233
1234 /* KMS drivers handle this internally */
1235 if (drm_core_check_feature(dev, DRIVER_MODESET))
1236 return 0;
1237
1238 crtc = modeset->crtc;
1239 if (crtc >= dev->num_crtcs)
1240 return -EINVAL;
1241
1242 switch (modeset->cmd) {
1243 case _DRM_PRE_MODESET:
1244 drm_vblank_pre_modeset(dev, crtc);
1245 break;
1246 case _DRM_POST_MODESET:
1247 drm_vblank_post_modeset(dev, crtc);
1248 break;
1249 default:
1250 return -EINVAL;
1251 }
1252
1253 return 0;
1254 }
1255
drm_queue_vblank_event(struct drm_device * dev,int pipe,union drm_wait_vblank * vblwait,struct drm_file * file_priv)1256 static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1257 union drm_wait_vblank *vblwait,
1258 struct drm_file *file_priv)
1259 {
1260 struct drm_pending_vblank_event *e;
1261 struct timeval now;
1262 unsigned long flags;
1263 unsigned int seq;
1264 int ret;
1265
1266 e = kzalloc(sizeof *e, GFP_KERNEL);
1267 if (e == NULL) {
1268 ret = -ENOMEM;
1269 goto err_put;
1270 }
1271
1272 e->pipe = pipe;
1273 e->base.pid = ddi_get_pid();
1274 e->event.base.type = DRM_EVENT_VBLANK;
1275 e->event.base.length = sizeof e->event;
1276 e->event.user_data = vblwait->request.signal;
1277 e->base.event = &e->event.base;
1278 e->base.file_priv = file_priv;
1279 e->base.destroy = (void (*) (void *, size_t)) kfree;
1280
1281 spin_lock_irqsave(&dev->event_lock, flags);
1282
1283 if (file_priv->event_space < sizeof e->event) {
1284 ret = -EBUSY;
1285 goto err_unlock;
1286 }
1287
1288 file_priv->event_space -= sizeof e->event;
1289 seq = drm_vblank_count_and_time(dev, pipe, &now);
1290
1291 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1292 (seq - vblwait->request.sequence) <= (1 << 23)) {
1293 vblwait->request.sequence = seq + 1;
1294 vblwait->reply.sequence = vblwait->request.sequence;
1295 }
1296
1297 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1298 vblwait->request.sequence, seq, pipe);
1299
1300 e->event.sequence = vblwait->request.sequence;
1301 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
1302 drm_vblank_put(dev, pipe);
1303 send_vblank_event(dev, e, seq, &now);
1304 pollwakeup(&e->base.file_priv->drm_pollhead, POLLIN | POLLRDNORM);
1305 vblwait->reply.sequence = seq;
1306 } else {
1307 /* drm_handle_vblank_events will call drm_vblank_put */
1308 list_add_tail(&e->base.link, &dev->vblank_event_list, (caddr_t)&e->base);
1309 vblwait->reply.sequence = vblwait->request.sequence;
1310 }
1311
1312 spin_unlock_irqrestore(&dev->event_lock, flags);
1313
1314 return 0;
1315 err_unlock:
1316 spin_unlock_irqrestore(&dev->event_lock, flags);
1317 kfree(e, sizeof(*e));
1318 err_put:
1319 drm_vblank_put(dev, pipe);
1320 return ret;
1321 }
1322
1323 /**
1324 * Wait for VBLANK.
1325 *
1326 * \param inode device inode.
1327 * \param file_priv DRM file private.
1328 * \param cmd command.
1329 * \param data user argument, pointing to a drm_wait_vblank structure.
1330 * \return zero on success or a negative number on failure.
1331 *
1332 * This function enables the vblank interrupt on the pipe requested, then
1333 * sleeps waiting for the requested sequence number to occur, and drops
1334 * the vblank interrupt refcount afterwards. (vblank irq disable follows that
1335 * after a timeout with no further vblank waits scheduled).
1336 */
1337 /* LINTED */
drm_wait_vblank(DRM_IOCTL_ARGS)1338 int drm_wait_vblank(DRM_IOCTL_ARGS)
1339 {
1340 union drm_wait_vblank *vblwait = data;
1341 int ret = 0;
1342 unsigned int flags, seq, crtc, high_crtc;
1343
1344 if ((!dev->pdev->irq) || (!dev->irq_enabled))
1345 return -EINVAL;
1346
1347 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1348 return -EINVAL;
1349
1350 if (vblwait->request.type &
1351 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1352 _DRM_VBLANK_HIGH_CRTC_MASK)) {
1353 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1354 vblwait->request.type,
1355 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1356 _DRM_VBLANK_HIGH_CRTC_MASK));
1357 return -EINVAL;
1358 }
1359
1360 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1361 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1362 if (high_crtc)
1363 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1364 else
1365 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1366 if (crtc >= dev->num_crtcs)
1367 return -EINVAL;
1368
1369 ret = drm_vblank_get(dev, crtc);
1370 if (ret) {
1371 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
1372 return ret;
1373 }
1374 seq = drm_vblank_count(dev, crtc);
1375
1376 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1377 case _DRM_VBLANK_RELATIVE:
1378 vblwait->request.sequence += seq;
1379 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1380 /* LINTED */
1381 case _DRM_VBLANK_ABSOLUTE:
1382 break;
1383 default:
1384 ret = -EINVAL;
1385 goto done;
1386 }
1387
1388 if (flags & _DRM_VBLANK_EVENT) {
1389 /* must hold on to the vblank ref until the event fires
1390 * drm_vblank_put will be called asynchronously
1391 */
1392 return drm_queue_vblank_event(dev, crtc, vblwait, file);
1393 }
1394
1395 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1396 (seq - vblwait->request.sequence) <= (1<<23)) {
1397 vblwait->request.sequence = seq + 1;
1398 }
1399
1400 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1401 vblwait->request.sequence, crtc);
1402 dev->last_vblank_wait[crtc] = vblwait->request.sequence;
1403 DRM_WAIT_ON(ret, &dev->vbl_queue[crtc], 3 * DRM_HZ,
1404 (((drm_vblank_count(dev, crtc) -
1405 vblwait->request.sequence) <= (1 << 23)) ||
1406 !dev->irq_enabled));
1407
1408 if (ret != -EINTR) {
1409 struct timeval now;
1410
1411 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
1412
1413 vblwait->reply.tval_sec = now.tv_sec;
1414 vblwait->reply.tval_usec = now.tv_usec;
1415 DRM_DEBUG("returning %d to client\n",
1416 vblwait->reply.sequence);
1417 } else {
1418 DRM_DEBUG("vblank wait interrupted by signal\n");
1419 }
1420
1421 done:
1422 drm_vblank_put(dev, crtc);
1423 return ret;
1424 }
1425
drm_handle_vblank_events(struct drm_device * dev,int crtc)1426 static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
1427 {
1428 struct drm_pending_vblank_event *e, *t;
1429 struct timeval now;
1430 unsigned long flags;
1431 unsigned int seq;
1432
1433 seq = drm_vblank_count_and_time(dev, crtc, &now);
1434
1435 spin_lock_irqsave(&dev->event_lock, flags);
1436
1437 list_for_each_entry_safe(e, t, struct drm_pending_vblank_event,
1438 &dev->vblank_event_list, base.link) {
1439 if (e->pipe != crtc)
1440 continue;
1441 if ((seq - e->event.sequence) > (1<<23))
1442 continue;
1443
1444 DRM_DEBUG("vblank event on %d, current %d\n",
1445 e->event.sequence, seq);
1446
1447 list_del(&e->base.link);
1448 drm_vblank_put(dev, e->pipe);
1449 send_vblank_event(dev, e, seq, &now);
1450 pollwakeup(&e->base.file_priv->drm_pollhead, POLLIN | POLLRDNORM);
1451 }
1452
1453 spin_unlock_irqrestore(&dev->event_lock, flags);
1454 }
1455
1456 /**
1457 * drm_handle_vblank - handle a vblank event
1458 * @dev: DRM device
1459 * @crtc: where this event occurred
1460 *
1461 * Drivers should call this routine in their vblank interrupt handlers to
1462 * update the vblank counter and send any signals that may be pending.
1463 */
drm_handle_vblank(struct drm_device * dev,int crtc)1464 bool drm_handle_vblank(struct drm_device *dev, int crtc)
1465 {
1466 u32 vblcount;
1467 s64 diff_ns;
1468 struct timeval tvblank;
1469 unsigned long irqflags;
1470
1471 if (!dev->num_crtcs)
1472 return false;
1473
1474 /* Need timestamp lock to prevent concurrent execution with
1475 * vblank enable/disable, as this would cause inconsistent
1476 * or corrupted timestamps and vblank counts.
1477 */
1478 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
1479
1480 /* Vblank irq handling disabled. Nothing to do. */
1481 if (!dev->vblank_enabled[crtc]) {
1482 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
1483 return false;
1484 }
1485
1486 /* Fetch corresponding timestamp for this vblank interval from
1487 * driver and store it in proper slot of timestamp ringbuffer.
1488 */
1489
1490 /* Get current timestamp and count. */
1491 vblcount = atomic_read(&dev->_vblank_count[crtc]);
1492 (void) drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1493
1494 /* Compute time difference to timestamp of last vblank */
1495 diff_ns = timeval_to_ns(&tvblank) -
1496 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1497
1498 /* Update vblank timestamp and count if at least
1499 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1500 * difference between last stored timestamp and current
1501 * timestamp. A smaller difference means basically
1502 * identical timestamps. Happens if this vblank has
1503 * been already processed and this is a redundant call,
1504 * e.g., due to spurious vblank interrupts. We need to
1505 * ignore those for accounting.
1506 */
1507 if (abs(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
1508 /* Store new timestamp in ringbuffer. */
1509 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
1510
1511 /* Increment cooked vblank count. This also atomically commits
1512 * the timestamp computed above.
1513 */
1514 atomic_inc(&dev->_vblank_count[crtc]);
1515 } else {
1516 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1517 crtc, (int) diff_ns);
1518 }
1519
1520 DRM_WAKEUP(&dev->vbl_queue[crtc]);
1521 drm_handle_vblank_events(dev, crtc);
1522
1523 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
1524 return true;
1525 }
1526