drm_irq.c revision 1450
1450N/A/*
1450N/A * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
1450N/A */
1450N/A
1450N/A/**
1450N/A * \file drm_irq.c
1450N/A * IRQ support
1450N/A *
1450N/A * \author Rickard E. (Rik) Faith <faith@valinux.com>
1450N/A * \author Gareth Hughes <gareth@valinux.com>
1450N/A */
1450N/A
1450N/A/*
1450N/A * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
1450N/A *
1450N/A * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
1450N/A * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
1450N/A * Copyright (c) 2009, 2013, Intel Corporation.
1450N/A * All Rights Reserved.
1450N/A *
1450N/A * Permission is hereby granted, free of charge, to any person obtaining a
1450N/A * copy of this software and associated documentation files (the "Software"),
1450N/A * to deal in the Software without restriction, including without limitation
1450N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1450N/A * and/or sell copies of the Software, and to permit persons to whom the
1450N/A * Software is furnished to do so, subject to the following conditions:
1450N/A *
1450N/A * The above copyright notice and this permission notice (including the next
1450N/A * paragraph) shall be included in all copies or substantial portions of the
1450N/A * Software.
1450N/A *
1450N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1450N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1450N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1450N/A * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1450N/A * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1450N/A * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1450N/A * OTHER DEALINGS IN THE SOFTWARE.
1450N/A */
1450N/A
1450N/A#include "drm.h"
1450N/A#include "drmP.h"
1450N/A#include "drm_io32.h"
1450N/A
1450N/Astatic irqreturn_t __irq_handler_wrap(DRM_IRQ_ARGS)
1450N/A{
1450N/A drm_device_t *dev = (void *)arg;
1450N/A int ret;
1450N/A
1450N/A mutex_enter(&dev->irq_lock);
1450N/A ret = dev->driver->irq_handler(arg);
1450N/A mutex_exit(&dev->irq_lock);
1450N/A
1450N/A return (ret);
1450N/A}
1450N/A
1450N/A/* LINTED */
1450N/Astatic irqreturn_t __irq_handler_wrap_msi(caddr_t arg1, caddr_t arg2)
1450N/A{
1450N/A drm_device_t *dev = (void *)arg1;
1450N/A int ret;
1450N/A
1450N/A mutex_enter(&dev->irq_lock);
1450N/A ret = dev->driver->irq_handler(arg1);
1450N/A mutex_exit(&dev->irq_lock);
1450N/A
1450N/A return (ret);
1450N/A}
1450N/A
1450N/Astatic int __install_irq_handler(struct drm_device *dev)
1450N/A{
1450N/A struct pci_dev *pdev = dev->pdev;
1450N/A int i, ret;
1450N/A
1450N/A if (pdev->msi_handle) {
1450N/A /* Call ddi_intr_add_handler() */
1450N/A for (i = 0; i < pdev->msi_actual; i++) {
1450N/A ret = ddi_intr_add_handler(pdev->msi_handle[i],
1450N/A __irq_handler_wrap_msi, (caddr_t)dev, NULL);
1450N/A if (ret != DDI_SUCCESS) {
1450N/A DRM_DEBUG("ddi_intr_add_handler() failed");
1450N/A return (ret);
1450N/A }
1450N/A }
1450N/A
1450N/A if (pdev->msi_flag & DDI_INTR_FLAG_BLOCK) {
1450N/A /* Call ddi_intr_block_enable() for MSI */
1450N/A (void) ddi_intr_block_enable(pdev->msi_handle, pdev->msi_actual);
1450N/A } else {
1450N/A /* Call ddi_intr_enable() for MSI non block enable */
1450N/A for (i = 0; i < pdev->msi_actual; i++)
1450N/A (void) ddi_intr_enable(pdev->msi_handle[i]);
1450N/A }
1450N/A } else {
1450N/A /* setup the interrupt handler */
1450N/A if (ddi_add_intr(dev->devinfo, 0, &pdev->intr_block,
1450N/A (ddi_idevice_cookie_t *)NULL, __irq_handler_wrap,
1450N/A (caddr_t)dev) != DDI_SUCCESS) {
1450N/A DRM_ERROR("ddi_add_intr failed");
1450N/A return (DDI_FAILURE);
1450N/A }
1450N/A }
1450N/A
1450N/A return (DDI_SUCCESS);
1450N/A}
1450N/A
1450N/Astatic void __uninstall_irq_handler(struct drm_device *dev)
1450N/A{
1450N/A struct pci_dev *pdev = dev->pdev;
1450N/A int i;
1450N/A
1450N/A ASSERT(dev->devinfo);
1450N/A
1450N/A if (pdev->msi_handle) {
1450N/A /* Disable all interrupts */
1450N/A if (pdev->msi_flag & DDI_INTR_FLAG_BLOCK) {
1450N/A /* Call ddi_intr_block_disable() */
1450N/A (void) ddi_intr_block_disable(pdev->msi_handle, pdev->msi_actual);
1450N/A } else {
1450N/A for (i = 0; i < pdev->msi_actual; i++)
1450N/A (void) ddi_intr_disable(pdev->msi_handle[i]);
1450N/A }
1450N/A
1450N/A /* Call ddi_intr_remove_handler() */
1450N/A for (i = 0; i < pdev->msi_actual; i++){
1450N/A (void) ddi_intr_remove_handler(pdev->msi_handle[i]);
1450N/A }
1450N/A } else {
1450N/A ddi_remove_intr(dev->devinfo, 0, pdev->intr_block);
1450N/A }
1450N/A}
1450N/A
1450N/Aint
1450N/Apci_enable_msi(struct pci_dev *pdev)
1450N/A{
1450N/A struct drm_device *dev = pdev->dev;
1450N/A dev_info_t *devinfo = dev->devinfo;
1450N/A int count, avail, actual;
1450N/A int types;
1450N/A int i, ret;
1450N/A
1450N/A /* Get supported interrupt types */
1450N/A if (ddi_intr_get_supported_types(dev->devinfo, &types) != DDI_SUCCESS) {
1450N/A DRM_DEBUG("ddi_intr_get_supported_types() failed");
1450N/A return (DDI_FAILURE);
1450N/A }
1450N/A if (!(types & DDI_INTR_TYPE_MSI))
1450N/A return (DDI_FAILURE);
1450N/A
1450N/A /* Get number of interrupts */
1450N/A ret = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_MSI, &count);
1450N/A if ((ret != DDI_SUCCESS) || (count == 0)) {
1450N/A DRM_DEBUG("ddi_intr_get_nintrs() failed, "
1450N/A "ret: %d, count: %d", ret, count);
1450N/A return (ret);
1450N/A }
1450N/A
1450N/A /* Get number of available interrupts */
1450N/A ret = ddi_intr_get_navail(devinfo, DDI_INTR_TYPE_MSI, &avail);
1450N/A if ((ret != DDI_SUCCESS) || (avail == 0)) {
1450N/A DRM_DEBUG("ddi_intr_get_navail() failed, "
1450N/A "ret: %d, avail: %d", ret, avail);
1450N/A return (ret);
1450N/A }
1450N/A
1450N/A if (avail < count) {
1450N/A DRM_DEBUG("nitrs() returned %d, navail returned %d",
1450N/A count, avail);
1450N/A }
1450N/A
1450N/A /* Allocate memory for MSI interrupts */
1450N/A pdev->msi_size = count * sizeof (ddi_intr_handle_t);
1450N/A pdev->msi_handle = kmem_alloc(pdev->msi_size, KM_SLEEP);
1450N/A
1450N/A ret = ddi_intr_alloc(devinfo, pdev->msi_handle, DDI_INTR_TYPE_MSI, 0,
1450N/A count, &actual, DDI_INTR_ALLOC_NORMAL);
1450N/A
1450N/A if ((ret != DDI_SUCCESS) || (actual == 0)) {
1450N/A DRM_DEBUG("ddi_intr_alloc() failed: %d", ret);
1450N/A kmem_free(pdev->msi_handle, pdev->msi_size);
1450N/A return (ret);
1450N/A }
1450N/A pdev->msi_actual = actual;
1450N/A
1450N/A /*
1450N/A * Get priority for first msi, assume remaining are all the same
1450N/A */
1450N/A ret = ddi_intr_get_pri(pdev->msi_handle[0], &pdev->msi_pri);
1450N/A if (ret != DDI_SUCCESS) {
1450N/A DRM_DEBUG("ddi_intr_get_pri() failed: %d", ret);
1450N/A for(i = 0; i < actual; i++)
1450N/A (void) ddi_intr_free(pdev->msi_handle[i]);
1450N/A kmem_free(pdev->msi_handle, pdev->msi_size);
1450N/A return (ret);
1450N/A }
1450N/A
1450N/A ret = ddi_intr_get_cap(pdev->msi_handle[0], &pdev->msi_flag);
1450N/A if (ret != DDI_SUCCESS) {
1450N/A DRM_DEBUG("ddi_intr_get_cap() failed: %d", ret);
1450N/A for(i = 0; i < actual; i++)
1450N/A (void) ddi_intr_free(pdev->msi_handle[i]);
1450N/A kmem_free(pdev->msi_handle, pdev->msi_size);
1450N/A return (ret);
1450N/A }
1450N/A
1450N/A return (ret);
1450N/A}
1450N/A
1450N/Avoid
1450N/Apci_disable_msi(struct pci_dev *pdev)
1450N/A{
1450N/A int i;
1450N/A
1450N/A for (i = 0; i < pdev->msi_actual; i++)
1450N/A (void) ddi_intr_free(pdev->msi_handle[i]);
1450N/A kmem_free(pdev->msi_handle, pdev->msi_size);
1450N/A pdev->msi_handle = NULL;
1450N/A}
1450N/A
1450N/A/* Access macro for slots in vblank timestamp ringbuffer. */
1450N/A#define vblanktimestamp(dev, crtc, count) ( \
1450N/A (dev)->_vblank_time[(crtc) * DRM_VBLANKTIME_RBSIZE + \
1450N/A ((count) % DRM_VBLANKTIME_RBSIZE)])
1450N/A
1450N/A/* Retry timestamp calculation up to 3 times to satisfy
1450N/A * drm_timestamp_precision before giving up.
1450N/A */
1450N/A#define DRM_TIMESTAMP_MAXRETRIES 3
1450N/A
1450N/A/* Threshold in nanoseconds for detection of redundant
1450N/A * vblank irq in drm_handle_vblank(). 1 msec should be ok.
1450N/A */
1450N/A#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
1450N/A
1450N/A/**
1450N/A * Get interrupt from bus id.
1450N/A *
1450N/A * \param inode device inode.
1450N/A * \param file_priv DRM file private.
1450N/A * \param cmd command.
1450N/A * \param arg user argument, pointing to a drm_irq_busid structure.
1450N/A * \return zero on success or a negative number on failure.
1450N/A *
1450N/A * Finds the PCI device with the specified bus id and gets its IRQ number.
1450N/A * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
1450N/A * to that of the device that this DRM instance attached to.
1450N/A */
1450N/A/* LINTED */
1450N/Aint drm_irq_by_busid(DRM_IOCTL_ARGS)
1450N/A{
1450N/A struct drm_irq_busid *p = data;
1450N/A
1450N/A if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
1450N/A return -EINVAL;
1450N/A
1450N/A if ((p->busnum >> 8) != dev->pdev->domain ||
1450N/A (p->busnum & 0xff) != dev->pdev->bus ||
1450N/A p->devnum != dev->pdev->slot || p->funcnum != dev->pdev->func)
1450N/A return -EINVAL;
1450N/A
1450N/A p->irq = dev->pdev->irq;
1450N/A
1450N/A DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
1450N/A p->irq);
1450N/A
1450N/A return 0;
1450N/A}
1450N/A
1450N/A/*
1450N/A * Clear vblank timestamp buffer for a crtc.
1450N/A */
1450N/Astatic void clear_vblank_timestamps(struct drm_device *dev, int crtc)
1450N/A{
1450N/A (void) memset(&dev->_vblank_time[crtc * DRM_VBLANKTIME_RBSIZE], -1,
1450N/A DRM_VBLANKTIME_RBSIZE * sizeof(struct timeval));
1450N/A}
1450N/A
1450N/A/*
1450N/A * Disable vblank irq's on crtc, make sure that last vblank count
1450N/A * of hardware and corresponding consistent software vblank counter
1450N/A * are preserved, even if there are any spurious vblank irq's after
1450N/A * disable.
1450N/A */
1450N/Astatic void vblank_disable_and_save(struct drm_device *dev, int crtc)
1450N/A{
1450N/A unsigned long irqflags;
1450N/A u32 vblcount;
1450N/A s64 diff_ns;
1450N/A int vblrc;
1450N/A struct timeval tvblank;
1450N/A int count = DRM_TIMESTAMP_MAXRETRIES;
1450N/A
1450N/A /* Prevent vblank irq processing while disabling vblank irqs,
1450N/A * so no updates of timestamps or count can happen after we've
1450N/A * disabled. Needed to prevent races in case of delayed irq's.
1450N/A */
1450N/A spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
1450N/A
1450N/A dev->driver->disable_vblank(dev, crtc);
1450N/A dev->vblank_enabled[crtc] = 0;
1450N/A
1450N/A /* No further vblank irq's will be processed after
1450N/A * this point. Get current hardware vblank count and
1450N/A * vblank timestamp, repeat until they are consistent.
1450N/A *
1450N/A * FIXME: There is still a race condition here and in
1450N/A * drm_update_vblank_count() which can cause off-by-one
1450N/A * reinitialization of software vblank counter. If gpu
1450N/A * vblank counter doesn't increment exactly at the leading
1450N/A * edge of a vblank interval, then we can lose 1 count if
1450N/A * we happen to execute between start of vblank and the
1450N/A * delayed gpu counter increment.
1450N/A */
1450N/A do {
1450N/A dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
1450N/A vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
1450N/A } while (dev->last_vblank[crtc] != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
1450N/A
1450N/A if (!count)
1450N/A vblrc = 0;
1450N/A
1450N/A /* Compute time difference to stored timestamp of last vblank
1450N/A * as updated by last invocation of drm_handle_vblank() in vblank irq.
1450N/A */
1450N/A vblcount = atomic_read(&dev->_vblank_count[crtc]);
1450N/A diff_ns = timeval_to_ns(&tvblank) -
1450N/A timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1450N/A
1450N/A /* If there is at least 1 msec difference between the last stored
1450N/A * timestamp and tvblank, then we are currently executing our
1450N/A * disable inside a new vblank interval, the tvblank timestamp
1450N/A * corresponds to this new vblank interval and the irq handler
1450N/A * for this vblank didn't run yet and won't run due to our disable.
1450N/A * Therefore we need to do the job of drm_handle_vblank() and
1450N/A * increment the vblank counter by one to account for this vblank.
1450N/A *
1450N/A * Skip this step if there isn't any high precision timestamp
1450N/A * available. In that case we can't account for this and just
1450N/A * hope for the best.
1450N/A */
1450N/A if ((vblrc > 0) && (abs(diff_ns) > 1000000)) {
1450N/A atomic_inc(&dev->_vblank_count[crtc]);
1450N/A }
1450N/A
1450N/A /* Invalidate all timestamps while vblank irq's are off. */
1450N/A clear_vblank_timestamps(dev, crtc);
1450N/A
1450N/A spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
1450N/A}
1450N/A
1450N/Astatic void vblank_disable_fn(void *arg)
1450N/A{
1450N/A struct drm_device *dev = (struct drm_device *)arg;
1450N/A unsigned long irqflags;
1450N/A int i;
1450N/A
1450N/A if (!dev->vblank_disable_allowed)
1450N/A return;
1450N/A
1450N/A for (i = 0; i < dev->num_crtcs; i++) {
1450N/A spin_lock_irqsave(&dev->vbl_lock, irqflags);
1450N/A if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
1450N/A dev->vblank_enabled[i]) {
1450N/A DRM_DEBUG("disabling vblank on crtc %d\n", i);
1450N/A vblank_disable_and_save(dev, i);
1450N/A }
1450N/A spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1450N/A }
1450N/A}
1450N/A
1450N/Avoid drm_vblank_cleanup(struct drm_device *dev)
1450N/A{
1450N/A /* Bail if the driver didn't call drm_vblank_init() */
1450N/A if (dev->num_crtcs == 0)
1450N/A return;
1450N/A
1450N/A del_timer(&dev->vblank_disable_timer);
1450N/A destroy_timer(&dev->vblank_disable_timer);
1450N/A
1450N/A vblank_disable_fn((void *)dev);
1450N/A
1450N/A kfree(dev->vbl_queue, sizeof (wait_queue_head_t) * dev->num_crtcs);
1450N/A kfree(dev->_vblank_count, sizeof (atomic_t) * dev->num_crtcs);
1450N/A kfree(dev->vblank_refcount, sizeof (atomic_t) * dev->num_crtcs);
1450N/A kfree(dev->vblank_enabled, sizeof (int) * dev->num_crtcs);
1450N/A kfree(dev->last_vblank, sizeof (u32) * dev->num_crtcs);
1450N/A kfree(dev->last_vblank_wait, sizeof (u32) * dev->num_crtcs);
1450N/A kfree(dev->vblank_inmodeset, sizeof (*dev->vblank_inmodeset) * dev->num_crtcs);
1450N/A kfree(dev->_vblank_time, sizeof (*dev->_vblank_time) * dev->num_crtcs * DRM_VBLANKTIME_RBSIZE);
1450N/A
1450N/A dev->num_crtcs = 0;
1450N/A
1450N/A mutex_destroy(&dev->vbl_lock);
1450N/A}
1450N/A
1450N/Aint drm_vblank_init(struct drm_device *dev, int num_crtcs)
1450N/A{
1450N/A int i, ret = -ENOMEM;
1450N/A
1450N/A init_timer(&dev->vblank_disable_timer);
1450N/A setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
1450N/A dev);
1450N/A mutex_init(&dev->vbl_lock, NULL, MUTEX_DRIVER, (void *)dev->pdev->intr_block);
1450N/A spin_lock_init(&dev->vblank_time_lock);
1450N/A
1450N/A dev->num_crtcs = num_crtcs;
1450N/A
1450N/A dev->vbl_queue = kmalloc(sizeof(wait_queue_head_t) * num_crtcs,
1450N/A GFP_KERNEL);
1450N/A if (!dev->vbl_queue)
1450N/A goto err;
1450N/A
1450N/A dev->_vblank_count = kmalloc(sizeof(atomic_t) * num_crtcs, GFP_KERNEL);
1450N/A if (!dev->_vblank_count)
1450N/A goto err;
1450N/A
1450N/A dev->vblank_refcount = kmalloc(sizeof(atomic_t) * num_crtcs,
1450N/A GFP_KERNEL);
1450N/A if (!dev->vblank_refcount)
1450N/A goto err;
1450N/A
1450N/A dev->vblank_enabled = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
1450N/A if (!dev->vblank_enabled)
1450N/A goto err;
1450N/A
1450N/A dev->last_vblank = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
1450N/A if (!dev->last_vblank)
1450N/A goto err;
1450N/A
1450N/A dev->last_vblank_wait = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
1450N/A if (!dev->last_vblank_wait)
1450N/A goto err;
1450N/A
1450N/A dev->vblank_inmodeset = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
1450N/A if (!dev->vblank_inmodeset)
1450N/A goto err;
1450N/A
1450N/A dev->_vblank_time = kcalloc(num_crtcs * DRM_VBLANKTIME_RBSIZE,
1450N/A sizeof(struct timeval), GFP_KERNEL);
1450N/A if (!dev->_vblank_time)
1450N/A goto err;
1450N/A
1450N/A DRM_INFO("Supports vblank timestamp caching Rev 1 (10.10.2010).\n");
1450N/A
1450N/A /* Driver specific high-precision vblank timestamping supported? */
1450N/A if (dev->driver->get_vblank_timestamp)
1450N/A DRM_INFO("Driver supports precise vblank timestamp query.\n");
1450N/A else
1450N/A DRM_INFO("No driver support for vblank timestamp query.\n");
1450N/A
1450N/A /* Zero per-crtc vblank stuff */
1450N/A for (i = 0; i < num_crtcs; i++) {
1450N/A DRM_INIT_WAITQUEUE(&dev->vbl_queue[i], DRM_INTR_PRI(dev));
1450N/A atomic_set(&dev->_vblank_count[i], 0);
1450N/A atomic_set(&dev->vblank_refcount[i], 0);
1450N/A }
1450N/A
1450N/A dev->vblank_disable_allowed = 0;
1450N/A
1450N/A return 0;
1450N/A
1450N/Aerr:
1450N/A drm_vblank_cleanup(dev);
1450N/A return ret;
1450N/A}
1450N/A
1450N/A/* LINTED */
1450N/Astatic void drm_irq_vgaarb_nokms(void *cookie, bool state)
1450N/A{
1450N/A struct drm_device *dev = cookie;
1450N/A
1450N/A if (dev->driver->vgaarb_irq) {
1450N/A dev->driver->vgaarb_irq(dev, state);
1450N/A return;
1450N/A }
1450N/A
1450N/A if (!dev->irq_enabled)
1450N/A return;
1450N/A
1450N/A if (state) {
1450N/A if (dev->driver->irq_uninstall)
1450N/A dev->driver->irq_uninstall(dev);
1450N/A } else {
1450N/A if (dev->driver->irq_preinstall)
1450N/A dev->driver->irq_preinstall(dev);
1450N/A if (dev->driver->irq_postinstall)
1450N/A dev->driver->irq_postinstall(dev);
1450N/A }
1450N/A}
1450N/A
1450N/A/**
1450N/A * Install IRQ handler.
1450N/A *
1450N/A * \param dev DRM device.
1450N/A *
1450N/A * Initializes the IRQ related data. Installs the handler, calling the driver
1450N/A * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
1450N/A * before and after the installation.
1450N/A */
1450N/Aint drm_irq_install(struct drm_device *dev)
1450N/A{
1450N/A int ret;
1450N/A
1450N/A if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
1450N/A return -EINVAL;
1450N/A
1450N/A if (dev->pdev->irq == 0)
1450N/A return -EINVAL;
1450N/A
1450N/A mutex_lock(&dev->struct_mutex);
1450N/A
1450N/A /* Driver must have been initialized */
1450N/A if (!dev->dev_private) {
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A return -EINVAL;
1450N/A }
1450N/A
1450N/A if (dev->irq_enabled) {
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A return -EBUSY;
1450N/A }
1450N/A dev->irq_enabled = 1;
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A
1450N/A DRM_DEBUG("irq=%d\n", dev->pdev->irq);
1450N/A
1450N/A /* Before installing handler */
1450N/A if (dev->driver->irq_preinstall)
1450N/A dev->driver->irq_preinstall(dev);
1450N/A
1450N/A /* Install handler */
1450N/A ret = __install_irq_handler(dev);
1450N/A if (ret != DDI_SUCCESS) {
1450N/A DRM_ERROR("IRQ handler installation failed");
1450N/A mutex_lock(&dev->struct_mutex);
1450N/A dev->irq_enabled = 0;
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A return -EFAULT;
1450N/A }
1450N/A
1450N/A /* After installing handler */
1450N/A if (dev->driver->irq_postinstall)
1450N/A ret = dev->driver->irq_postinstall(dev);
1450N/A if (ret < 0) {
1450N/A mutex_lock(&dev->struct_mutex);
1450N/A dev->irq_enabled = 0;
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A return ret;
1450N/A }
1450N/A
1450N/A dev->context_flag = 0;
1450N/A return 0;
1450N/A}
1450N/A
1450N/A/**
1450N/A * Uninstall the IRQ handler.
1450N/A *
1450N/A * \param dev DRM device.
1450N/A *
1450N/A * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
1450N/A */
1450N/Aint drm_irq_uninstall(struct drm_device * dev)
1450N/A{
1450N/A unsigned long irqflags;
1450N/A int irq_enabled, i;
1450N/A
1450N/A if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
1450N/A return -EINVAL;
1450N/A
1450N/A mutex_lock(&dev->struct_mutex);
1450N/A irq_enabled = dev->irq_enabled;
1450N/A dev->irq_enabled = 0;
1450N/A mutex_unlock(&dev->struct_mutex);
1450N/A
1450N/A /*
1450N/A * Wake up any waiters so they don't hang.
1450N/A */
1450N/A if (dev->num_crtcs) {
1450N/A spin_lock_irqsave(&dev->vbl_lock, irqflags);
1450N/A for (i = 0; i < dev->num_crtcs; i++) {
1450N/A DRM_WAKEUP(&dev->vbl_queue[i]);
1450N/A dev->vblank_enabled[i] = 0;
1450N/A dev->last_vblank[i] =
1450N/A dev->driver->get_vblank_counter(dev, i);
1450N/A }
1450N/A spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1450N/A }
1450N/A
1450N/A if (!irq_enabled)
1450N/A return -EINVAL;
1450N/A
1450N/A DRM_DEBUG("irq=%d\n", dev->pdev->irq);
1450N/A
1450N/A if (dev->driver->irq_uninstall)
1450N/A dev->driver->irq_uninstall(dev);
1450N/A
1450N/A __uninstall_irq_handler(dev);
1450N/A
1450N/A return 0;
1450N/A}
1450N/A
1450N/A/**
1450N/A * IRQ control ioctl.
1450N/A *
1450N/A * \param inode device inode.
1450N/A * \param file_priv DRM file private.
1450N/A * \param cmd command.
1450N/A * \param arg user argument, pointing to a drm_control structure.
1450N/A * \return zero on success or a negative number on failure.
1450N/A *
1450N/A * Calls irq_install() or irq_uninstall() according to \p arg.
1450N/A */
1450N/A/* LINTED */
1450N/Aint drm_control(DRM_IOCTL_ARGS)
1450N/A{
1450N/A struct drm_control *ctl = data;
1450N/A
1450N/A /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
1450N/A
1450N/A
1450N/A switch (ctl->func) {
1450N/A case DRM_INST_HANDLER:
1450N/A if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
1450N/A return 0;
1450N/A if (drm_core_check_feature(dev, DRIVER_MODESET))
1450N/A return 0;
1450N/A if (dev->if_version < DRM_IF_VERSION(1, 2) &&
1450N/A ctl->irq != dev->pdev->irq)
1450N/A return -EINVAL;
1450N/A return drm_irq_install(dev);
1450N/A case DRM_UNINST_HANDLER:
1450N/A if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
1450N/A return 0;
1450N/A if (drm_core_check_feature(dev, DRIVER_MODESET))
1450N/A return 0;
1450N/A return drm_irq_uninstall(dev);
1450N/A default:
1450N/A return -EINVAL;
1450N/A }
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_calc_timestamping_constants - Calculate and
1450N/A * store various constants which are later needed by
1450N/A * vblank and swap-completion timestamping, e.g, by
1450N/A * drm_calc_vbltimestamp_from_scanoutpos().
1450N/A * They are derived from crtc's true scanout timing,
1450N/A * so they take things like panel scaling or other
1450N/A * adjustments into account.
1450N/A *
1450N/A * @crtc drm_crtc whose timestamp constants should be updated.
1450N/A *
1450N/A */
1450N/Avoid drm_calc_timestamping_constants(struct drm_crtc *crtc)
1450N/A{
1450N/A s64 linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
1450N/A u64 dotclock;
1450N/A
1450N/A /* Dot clock in Hz: */
1450N/A dotclock = (u64) crtc->hwmode.clock * 1000;
1450N/A
1450N/A /* Fields of interlaced scanout modes are only halve a frame duration.
1450N/A * Double the dotclock to get halve the frame-/line-/pixelduration.
1450N/A */
1450N/A if (crtc->hwmode.flags & DRM_MODE_FLAG_INTERLACE)
1450N/A dotclock *= 2;
1450N/A
1450N/A /* Valid dotclock? */
1450N/A if (dotclock > 0) {
1450N/A int frame_size;
1450N/A /* Convert scanline length in pixels and video dot clock to
1450N/A * line duration, frame duration and pixel duration in
1450N/A * nanoseconds:
1450N/A */
1450N/A pixeldur_ns = (s64) div_u64(1000000000, dotclock);
1450N/A linedur_ns = (s64) div_u64(((u64) crtc->hwmode.crtc_htotal *
1450N/A 1000000000), dotclock);
1450N/A frame_size = crtc->hwmode.crtc_htotal *
1450N/A crtc->hwmode.crtc_vtotal;
1450N/A framedur_ns = (s64) div_u64((u64) frame_size * 1000000000,
1450N/A dotclock);
1450N/A } else
1450N/A DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
1450N/A crtc->base.id);
1450N/A
1450N/A crtc->pixeldur_ns = pixeldur_ns;
1450N/A crtc->linedur_ns = linedur_ns;
1450N/A crtc->framedur_ns = framedur_ns;
1450N/A
1450N/A DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
1450N/A crtc->base.id, crtc->hwmode.crtc_htotal,
1450N/A crtc->hwmode.crtc_vtotal, crtc->hwmode.crtc_vdisplay);
1450N/A DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
1450N/A crtc->base.id, (int) dotclock/1000, (int) framedur_ns,
1450N/A (int) linedur_ns, (int) pixeldur_ns);
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
1450N/A * drivers. Implements calculation of exact vblank timestamps from
1450N/A * given drm_display_mode timings and current video scanout position
1450N/A * of a crtc. This can be called from within get_vblank_timestamp()
1450N/A * implementation of a kms driver to implement the actual timestamping.
1450N/A *
1450N/A * Should return timestamps conforming to the OML_sync_control OpenML
1450N/A * extension specification. The timestamp corresponds to the end of
1450N/A * the vblank interval, aka start of scanout of topmost-leftmost display
1450N/A * pixel in the following video frame.
1450N/A *
1450N/A * Requires support for optional dev->driver->get_scanout_position()
1450N/A * in kms driver, plus a bit of setup code to provide a drm_display_mode
1450N/A * that corresponds to the true scanout timing.
1450N/A *
1450N/A * The current implementation only handles standard video modes. It
1450N/A * returns as no operation if a doublescan or interlaced video mode is
1450N/A * active. Higher level code is expected to handle this.
1450N/A *
1450N/A * @dev: DRM device.
1450N/A * @crtc: Which crtc's vblank timestamp to retrieve.
1450N/A * @max_error: Desired maximum allowable error in timestamps (nanosecs).
1450N/A * On return contains true maximum error of timestamp.
1450N/A * @vblank_time: Pointer to struct timeval which should receive the timestamp.
1450N/A * @flags: Flags to pass to driver:
1450N/A * 0 = Default.
1450N/A * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
1450N/A * @refcrtc: drm_crtc* of crtc which defines scanout timing.
1450N/A *
1450N/A * Returns negative value on error, failure or if not supported in current
1450N/A * video mode:
1450N/A *
1450N/A * -EINVAL - Invalid crtc.
1450N/A * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
1450N/A * -ENOTSUPP - Function not supported in current display mode.
1450N/A * -EIO - Failed, e.g., due to failed scanout position query.
1450N/A *
1450N/A * Returns or'ed positive status flags on success:
1450N/A *
1450N/A * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
1450N/A * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
1450N/A *
1450N/A */
1450N/Aint drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
1450N/A int *max_error,
1450N/A struct timeval *vblank_time,
1450N/A unsigned flags,
1450N/A struct drm_crtc *refcrtc)
1450N/A{
1450N/A struct timeval stime, raw_time;
1450N/A struct drm_display_mode *mode;
1450N/A int vbl_status, vtotal, vdisplay;
1450N/A int vpos, hpos, i;
1450N/A s64 framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
1450N/A bool invbl;
1450N/A
1450N/A if (crtc < 0 || crtc >= dev->num_crtcs) {
1450N/A DRM_ERROR("Invalid crtc %d\n", crtc);
1450N/A return -EINVAL;
1450N/A }
1450N/A
1450N/A /* Scanout position query not supported? Should not happen. */
1450N/A if (!dev->driver->get_scanout_position) {
1450N/A DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
1450N/A return -EIO;
1450N/A }
1450N/A
1450N/A mode = &refcrtc->hwmode;
1450N/A vtotal = mode->crtc_vtotal;
1450N/A vdisplay = mode->crtc_vdisplay;
1450N/A
1450N/A /* Durations of frames, lines, pixels in nanoseconds. */
1450N/A framedur_ns = refcrtc->framedur_ns;
1450N/A linedur_ns = refcrtc->linedur_ns;
1450N/A pixeldur_ns = refcrtc->pixeldur_ns;
1450N/A
1450N/A /* If mode timing undefined, just return as no-op:
1450N/A * Happens during initial modesetting of a crtc.
1450N/A */
1450N/A if (vtotal <= 0 || vdisplay <= 0 || framedur_ns == 0) {
1450N/A DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
1450N/A return -EAGAIN;
1450N/A }
1450N/A
1450N/A /* Get current scanout position with system timestamp.
1450N/A * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
1450N/A * if single query takes longer than max_error nanoseconds.
1450N/A *
1450N/A * This guarantees a tight bound on maximum error if
1450N/A * code gets preempted or delayed for some reason.
1450N/A */
1450N/A for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
1450N/A /* Disable preemption to make it very likely to
1450N/A * succeed in the first iteration even on PREEMPT_RT kernel.
1450N/A */
1450N/A
1450N/A /* Get system timestamp before query. */
1450N/A do_gettimeofday(&stime);
1450N/A
1450N/A /* Get vertical and horizontal scanout pos. vpos, hpos. */
1450N/A vbl_status = dev->driver->get_scanout_position(dev, crtc, &vpos, &hpos);
1450N/A
1450N/A /* Get system timestamp after query. */
1450N/A do_gettimeofday(&raw_time);
1450N/A
1450N/A /* Return as no-op if scanout query unsupported or failed. */
1450N/A if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
1450N/A DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
1450N/A crtc, vbl_status);
1450N/A return -EIO;
1450N/A }
1450N/A
1450N/A duration_ns = timeval_to_ns(&raw_time) - timeval_to_ns(&stime);
1450N/A
1450N/A /* Accept result with < max_error nsecs timing uncertainty. */
1450N/A if (duration_ns <= (s64) *max_error)
1450N/A break;
1450N/A }
1450N/A
1450N/A /* Noisy system timing? */
1450N/A if (i == DRM_TIMESTAMP_MAXRETRIES) {
1450N/A DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
1450N/A crtc, (int) duration_ns/1000, *max_error/1000, i);
1450N/A }
1450N/A
1450N/A /* Return upper bound of timestamp precision error. */
1450N/A *max_error = (int) duration_ns;
1450N/A
1450N/A /* Check if in vblank area:
1450N/A * vpos is >=0 in video scanout area, but negative
1450N/A * within vblank area, counting down the number of lines until
1450N/A * start of scanout.
1450N/A */
1450N/A invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
1450N/A
1450N/A /* Convert scanout position into elapsed time at raw_time query
1450N/A * since start of scanout at first display scanline. delta_ns
1450N/A * can be negative if start of scanout hasn't happened yet.
1450N/A */
1450N/A delta_ns = (s64) vpos * linedur_ns + (s64) hpos * pixeldur_ns;
1450N/A
1450N/A /* Is vpos outside nominal vblank area, but less than
1450N/A * 1/100 of a frame height away from start of vblank?
1450N/A * If so, assume this isn't a massively delayed vblank
1450N/A * interrupt, but a vblank interrupt that fired a few
1450N/A * microseconds before true start of vblank. Compensate
1450N/A * by adding a full frame duration to the final timestamp.
1450N/A * Happens, e.g., on ATI R500, R600.
1450N/A *
1450N/A * We only do this if DRM_CALLED_FROM_VBLIRQ.
1450N/A */
1450N/A if ((flags & DRM_CALLED_FROM_VBLIRQ) && !invbl &&
1450N/A ((vdisplay - vpos) < vtotal / 100)) {
1450N/A delta_ns = delta_ns - framedur_ns;
1450N/A
1450N/A /* Signal this correction as "applied". */
1450N/A vbl_status |= 0x8;
1450N/A }
1450N/A
1450N/A /* Subtract time delta from raw timestamp to get final
1450N/A * vblank_time timestamp for end of vblank.
1450N/A */
1450N/A ns_to_timeval(timeval_to_ns(&raw_time) - delta_ns, vblank_time);
1450N/A
1450N/A DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
1450N/A crtc, (int) vbl_status, hpos, vpos, raw_time.tv_sec,
1450N/A raw_time.tv_usec, vblank_time->tv_sec, vblank_time->tv_usec,
1450N/A (int) duration_ns/1000, i);
1450N/A
1450N/A vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
1450N/A if (invbl)
1450N/A vbl_status |= DRM_VBLANKTIME_INVBL;
1450N/A
1450N/A return vbl_status;
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
1450N/A * vblank interval.
1450N/A *
1450N/A * @dev: DRM device
1450N/A * @crtc: which crtc's vblank timestamp to retrieve
1450N/A * @tvblank: Pointer to target struct timeval which should receive the timestamp
1450N/A * @flags: Flags to pass to driver:
1450N/A * 0 = Default.
1450N/A * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
1450N/A *
1450N/A * Fetches the system timestamp corresponding to the time of the most recent
1450N/A * vblank interval on specified crtc. May call into kms-driver to
1450N/A * compute the timestamp with a high-precision GPU specific method.
1450N/A *
1450N/A * Returns zero if timestamp originates from uncorrected do_gettimeofday()
1450N/A * call, i.e., it isn't very precisely locked to the true vblank.
1450N/A *
1450N/A * Returns non-zero if timestamp is considered to be very precise.
1450N/A */
1450N/Au32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
1450N/A struct timeval *tvblank, unsigned flags)
1450N/A{
1450N/A int ret = 0;
1450N/A
1450N/A /* Define requested maximum error on timestamps (nanoseconds). */
1450N/A int max_error = (int) drm_timestamp_precision * 1000;
1450N/A
1450N/A /* Query driver if possible and precision timestamping enabled. */
1450N/A if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
1450N/A ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
1450N/A tvblank, flags);
1450N/A if (ret > 0)
1450N/A return (u32) ret;
1450N/A }
1450N/A
1450N/A /* GPU high precision timestamp query unsupported or failed.
1450N/A * Return gettimeofday timestamp as best estimate.
1450N/A */
1450N/A do_gettimeofday(tvblank);
1450N/A
1450N/A return 0;
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_vblank_count - retrieve "cooked" vblank counter value
1450N/A * @dev: DRM device
1450N/A * @crtc: which counter to retrieve
1450N/A *
1450N/A * Fetches the "cooked" vblank count value that represents the number of
1450N/A * vblank events since the system was booted, including lost events due to
1450N/A * modesetting activity.
1450N/A */
1450N/Au32 drm_vblank_count(struct drm_device *dev, int crtc)
1450N/A{
1450N/A return atomic_read(&dev->_vblank_count[crtc]);
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
1450N/A * and the system timestamp corresponding to that vblank counter value.
1450N/A *
1450N/A * @dev: DRM device
1450N/A * @crtc: which counter to retrieve
1450N/A * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
1450N/A *
1450N/A * Fetches the "cooked" vblank count value that represents the number of
1450N/A * vblank events since the system was booted, including lost events due to
1450N/A * modesetting activity. Returns corresponding system timestamp of the time
1450N/A * of the vblank interval that corresponds to the current value vblank counter
1450N/A * value.
1450N/A */
1450N/Au32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1450N/A struct timeval *vblanktime)
1450N/A{
1450N/A u32 cur_vblank;
1450N/A
1450N/A /* Read timestamp from slot of _vblank_time ringbuffer
1450N/A * that corresponds to current vblank count. Retry if
1450N/A * count has incremented during readout. This works like
1450N/A * a seqlock.
1450N/A */
1450N/A do {
1450N/A cur_vblank = atomic_read(&dev->_vblank_count[crtc]);
1450N/A *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
1450N/A } while (cur_vblank != atomic_read(&dev->_vblank_count[crtc]));
1450N/A
1450N/A return cur_vblank;
1450N/A}
1450N/A
1450N/A/* LINTED */
1450N/Astatic void send_vblank_event(struct drm_device *dev,
1450N/A struct drm_pending_vblank_event *e,
1450N/A unsigned long seq, struct timeval *now)
1450N/A{
1450N/A e->event.sequence = (u32) seq;
1450N/A e->event.tv_sec = now->tv_sec;
1450N/A e->event.tv_usec = now->tv_usec;
1450N/A
1450N/A list_add_tail(&e->base.link, &e->base.file_priv->event_list, (caddr_t)&e->base);
1450N/A DRM_WAKEUP(&e->base.file_priv->event_wait);
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_send_vblank_event - helper to send vblank event after pageflip
1450N/A * @dev: DRM device
1450N/A * @crtc: CRTC in question
1450N/A * @e: the event to send
1450N/A *
1450N/A * Updates sequence # and timestamp on event, and sends it to userspace.
1450N/A * Caller must hold event lock.
1450N/A */
1450N/Avoid drm_send_vblank_event(struct drm_device *dev, int crtc,
1450N/A struct drm_pending_vblank_event *e)
1450N/A{
1450N/A struct timeval now;
1450N/A unsigned int seq;
1450N/A if (crtc >= 0) {
1450N/A seq = drm_vblank_count_and_time(dev, crtc, &now);
1450N/A } else {
1450N/A seq = 0;
1450N/A
1450N/A do_gettimeofday(&now);
1450N/A }
1450N/A e->pipe = crtc;
1450N/A send_vblank_event(dev, e, seq, &now);
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_update_vblank_count - update the master vblank counter
1450N/A * @dev: DRM device
1450N/A * @crtc: counter to update
1450N/A *
1450N/A * Call back into the driver to update the appropriate vblank counter
1450N/A * (specified by @crtc). Deal with wraparound, if it occurred, and
1450N/A * update the last read value so we can deal with wraparound on the next
1450N/A * call if necessary.
1450N/A *
1450N/A * Only necessary when going from off->on, to account for frames we
1450N/A * didn't get an interrupt for.
1450N/A *
1450N/A * Note: caller must hold dev->vbl_lock since this reads & writes
1450N/A * device vblank fields.
1450N/A */
1450N/Astatic void drm_update_vblank_count(struct drm_device *dev, int crtc)
1450N/A{
1450N/A u32 cur_vblank, diff, tslot, rc;
1450N/A struct timeval t_vblank;
1450N/A
1450N/A /*
1450N/A * Interrupts were disabled prior to this call, so deal with counter
1450N/A * wrap if needed.
1450N/A * NOTE! It's possible we lost a full dev->max_vblank_count events
1450N/A * here if the register is small or we had vblank interrupts off for
1450N/A * a long time.
1450N/A *
1450N/A * We repeat the hardware vblank counter & timestamp query until
1450N/A * we get consistent results. This to prevent races between gpu
1450N/A * updating its hardware counter while we are retrieving the
1450N/A * corresponding vblank timestamp.
1450N/A */
1450N/A do {
1450N/A cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
1450N/A rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
1450N/A } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
1450N/A
1450N/A /* Deal with counter wrap */
1450N/A diff = cur_vblank - dev->last_vblank[crtc];
1450N/A if (cur_vblank < dev->last_vblank[crtc]) {
1450N/A diff += dev->max_vblank_count;
1450N/A
1450N/A DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
1450N/A crtc, dev->last_vblank[crtc], cur_vblank, diff);
1450N/A }
1450N/A
1450N/A DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
1450N/A crtc, diff);
1450N/A
1450N/A /* Reinitialize corresponding vblank timestamp if high-precision query
1450N/A * available. Skip this step if query unsupported or failed. Will
1450N/A * reinitialize delayed at next vblank interrupt in that case.
1450N/A */
1450N/A if (rc) {
1450N/A tslot = atomic_read(&dev->_vblank_count[crtc]) + diff;
1450N/A vblanktimestamp(dev, crtc, tslot) = t_vblank;
1450N/A }
1450N/A
1450N/A atomic_add(diff, &dev->_vblank_count[crtc]);
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_vblank_get - get a reference count on vblank events
1450N/A * @dev: DRM device
1450N/A * @crtc: which CRTC to own
1450N/A *
1450N/A * Acquire a reference count on vblank events to avoid having them disabled
1450N/A * while in use.
1450N/A *
1450N/A * RETURNS
1450N/A * Zero on success, nonzero on failure.
1450N/A */
1450N/Aint drm_vblank_get(struct drm_device *dev, int crtc)
1450N/A{
1450N/A unsigned long irqflags, irqflags2;
1450N/A int ret = 0;
1450N/A
1450N/A spin_lock_irqsave(&dev->vbl_lock, irqflags);
1450N/A /* Going from 0->1 means we have to enable interrupts again */
1450N/A if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1) {
1450N/A spin_lock_irqsave(&dev->vblank_time_lock, irqflags2);
1450N/A if (!dev->vblank_enabled[crtc]) {
1450N/A ret = dev->driver->enable_vblank(dev, crtc);
1450N/A DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
1450N/A crtc, ret);
1450N/A if (ret)
1450N/A atomic_dec(&dev->vblank_refcount[crtc]);
1450N/A else {
1450N/A dev->vblank_enabled[crtc] = 1;
1450N/A drm_update_vblank_count(dev, crtc);
1450N/A }
1450N/A }
1450N/A spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags2);
1450N/A } else {
1450N/A if (!dev->vblank_enabled[crtc]) {
1450N/A atomic_dec(&dev->vblank_refcount[crtc]);
1450N/A ret = -EINVAL;
1450N/A }
1450N/A }
1450N/A spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1450N/A
1450N/A return ret;
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_vblank_put - give up ownership of vblank events
1450N/A * @dev: DRM device
1450N/A * @crtc: which counter to give up
1450N/A *
1450N/A * Release ownership of a given vblank counter, turning off interrupts
1450N/A * if possible.
1450N/A */
1450N/Avoid drm_vblank_put(struct drm_device *dev, int crtc)
1450N/A{
1450N/A BUG_ON (atomic_read (&dev->vblank_refcount[crtc]) == 0);
1450N/A
1450N/A /* Last user schedules interrupt disable */
1450N/A if (atomic_dec_and_test(&dev->vblank_refcount[crtc]) &&
1450N/A (drm_vblank_offdelay > 0))
1450N/A mod_timer(&dev->vblank_disable_timer,
1450N/A ((drm_vblank_offdelay * DRM_HZ)/1000));
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_vblank_off - disable vblank events on a CRTC
1450N/A * @dev: DRM device
1450N/A * @crtc: CRTC in question
1450N/A *
1450N/A * Caller must hold event lock.
1450N/A */
1450N/Avoid drm_vblank_off(struct drm_device *dev, int crtc)
1450N/A{
1450N/A unsigned long irqflags;
1450N/A struct drm_pending_vblank_event *e, *t;
1450N/A struct timeval now;
1450N/A unsigned int seq;
1450N/A
1450N/A spin_lock_irqsave(&dev->vbl_lock, irqflags);
1450N/A vblank_disable_and_save(dev, crtc);
1450N/A DRM_WAKEUP(&dev->vbl_queue[crtc]);
1450N/A
1450N/A /* Send any queued vblank events, lest the natives grow disquiet */
1450N/A seq = drm_vblank_count_and_time(dev, crtc, &now);
1450N/A
1450N/A spin_lock(&dev->event_lock);
1450N/A list_for_each_entry_safe(e, t, struct drm_pending_vblank_event,
1450N/A &dev->vblank_event_list, base.link) {
1450N/A if (e->pipe != crtc)
1450N/A continue;
1450N/A DRM_DEBUG("Sending premature vblank event on disable: \
1450N/A wanted %d, current %d\n",
1450N/A e->event.sequence, seq);
1450N/A list_del(&e->base.link);
1450N/A drm_vblank_put(dev, e->pipe);
1450N/A send_vblank_event(dev, e, seq, &now);
1450N/A }
1450N/A spin_unlock(&dev->event_lock);
1450N/A
1450N/A spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_vblank_pre_modeset - account for vblanks across mode sets
1450N/A * @dev: DRM device
1450N/A * @crtc: CRTC in question
1450N/A * @post: post or pre mode set?
1450N/A *
1450N/A * Account for vblank events across mode setting events, which will likely
1450N/A * reset the hardware frame counter.
1450N/A */
1450N/Avoid drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
1450N/A{
1450N/A /* vblank is not initialized (IRQ not installed ?) */
1450N/A if (!dev->num_crtcs)
1450N/A return;
1450N/A /*
1450N/A * To avoid all the problems that might happen if interrupts
1450N/A * were enabled/disabled around or between these calls, we just
1450N/A * have the kernel take a reference on the CRTC (just once though
1450N/A * to avoid corrupting the count if multiple, mismatch calls occur),
1450N/A * so that interrupts remain enabled in the interim.
1450N/A */
1450N/A if (!dev->vblank_inmodeset[crtc]) {
1450N/A dev->vblank_inmodeset[crtc] = 0x1;
1450N/A if (drm_vblank_get(dev, crtc) == 0)
1450N/A dev->vblank_inmodeset[crtc] |= 0x2;
1450N/A }
1450N/A}
1450N/A
1450N/Avoid drm_vblank_post_modeset(struct drm_device *dev, int crtc)
1450N/A{
1450N/A unsigned long irqflags;
1450N/A
1450N/A /* vblank is not initialized (IRQ not installed ?), or has been freed */
1450N/A if (!dev->num_crtcs)
1450N/A return;
1450N/A
1450N/A if (dev->vblank_inmodeset[crtc]) {
1450N/A spin_lock_irqsave(&dev->vbl_lock, irqflags);
1450N/A dev->vblank_disable_allowed = 1;
1450N/A spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1450N/A
1450N/A if (dev->vblank_inmodeset[crtc] & 0x2)
1450N/A drm_vblank_put(dev, crtc);
1450N/A
1450N/A dev->vblank_inmodeset[crtc] = 0;
1450N/A }
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_modeset_ctl - handle vblank event counter changes across mode switch
1450N/A * @DRM_IOCTL_ARGS: standard ioctl arguments
1450N/A *
1450N/A * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1450N/A * ioctls around modesetting so that any lost vblank events are accounted for.
1450N/A *
1450N/A * Generally the counter will reset across mode sets. If interrupts are
1450N/A * enabled around this call, we don't have to do anything since the counter
1450N/A * will have already been incremented.
1450N/A */
1450N/A/* LINTED */
1450N/Aint drm_modeset_ctl(DRM_IOCTL_ARGS)
1450N/A{
1450N/A struct drm_modeset_ctl *modeset = data;
1450N/A unsigned int crtc;
1450N/A
1450N/A /* If drm_vblank_init() hasn't been called yet, just no-op */
1450N/A if (!dev->num_crtcs)
1450N/A return 0;
1450N/A
1450N/A /* KMS drivers handle this internally */
1450N/A if (drm_core_check_feature(dev, DRIVER_MODESET))
1450N/A return 0;
1450N/A
1450N/A crtc = modeset->crtc;
1450N/A if (crtc >= dev->num_crtcs)
1450N/A return -EINVAL;
1450N/A
1450N/A switch (modeset->cmd) {
1450N/A case _DRM_PRE_MODESET:
1450N/A drm_vblank_pre_modeset(dev, crtc);
1450N/A break;
1450N/A case _DRM_POST_MODESET:
1450N/A drm_vblank_post_modeset(dev, crtc);
1450N/A break;
1450N/A default:
1450N/A return -EINVAL;
1450N/A }
1450N/A
1450N/A return 0;
1450N/A}
1450N/A
1450N/Astatic int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1450N/A union drm_wait_vblank *vblwait,
1450N/A struct drm_file *file_priv)
1450N/A{
1450N/A struct drm_pending_vblank_event *e;
1450N/A struct timeval now;
1450N/A unsigned long flags;
1450N/A unsigned int seq;
1450N/A int ret;
1450N/A
1450N/A e = kzalloc(sizeof *e, GFP_KERNEL);
1450N/A if (e == NULL) {
1450N/A ret = -ENOMEM;
1450N/A goto err_put;
1450N/A }
1450N/A
1450N/A e->pipe = pipe;
1450N/A e->base.pid = ddi_get_pid();
1450N/A e->event.base.type = DRM_EVENT_VBLANK;
1450N/A e->event.base.length = sizeof e->event;
1450N/A e->event.user_data = vblwait->request.signal;
1450N/A e->base.event = &e->event.base;
1450N/A e->base.file_priv = file_priv;
1450N/A e->base.destroy = (void (*) (void *, size_t)) kfree;
1450N/A
1450N/A spin_lock_irqsave(&dev->event_lock, flags);
1450N/A
1450N/A if (file_priv->event_space < sizeof e->event) {
1450N/A ret = -EBUSY;
1450N/A goto err_unlock;
1450N/A }
1450N/A
1450N/A file_priv->event_space -= sizeof e->event;
1450N/A seq = drm_vblank_count_and_time(dev, pipe, &now);
1450N/A
1450N/A if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1450N/A (seq - vblwait->request.sequence) <= (1 << 23)) {
1450N/A vblwait->request.sequence = seq + 1;
1450N/A vblwait->reply.sequence = vblwait->request.sequence;
1450N/A }
1450N/A
1450N/A DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1450N/A vblwait->request.sequence, seq, pipe);
1450N/A
1450N/A e->event.sequence = vblwait->request.sequence;
1450N/A if ((seq - vblwait->request.sequence) <= (1 << 23)) {
1450N/A drm_vblank_put(dev, pipe);
1450N/A send_vblank_event(dev, e, seq, &now);
1450N/A pollwakeup(&e->base.file_priv->drm_pollhead, POLLIN | POLLRDNORM);
1450N/A vblwait->reply.sequence = seq;
1450N/A } else {
1450N/A /* drm_handle_vblank_events will call drm_vblank_put */
1450N/A list_add_tail(&e->base.link, &dev->vblank_event_list, (caddr_t)&e->base);
1450N/A vblwait->reply.sequence = vblwait->request.sequence;
1450N/A }
1450N/A
1450N/A spin_unlock_irqrestore(&dev->event_lock, flags);
1450N/A
1450N/A return 0;
1450N/Aerr_unlock:
1450N/A spin_unlock_irqrestore(&dev->event_lock, flags);
1450N/A kfree(e, sizeof(*e));
1450N/Aerr_put:
1450N/A drm_vblank_put(dev, pipe);
1450N/A return ret;
1450N/A}
1450N/A
1450N/A/**
1450N/A * Wait for VBLANK.
1450N/A *
1450N/A * \param inode device inode.
1450N/A * \param file_priv DRM file private.
1450N/A * \param cmd command.
1450N/A * \param data user argument, pointing to a drm_wait_vblank structure.
1450N/A * \return zero on success or a negative number on failure.
1450N/A *
1450N/A * This function enables the vblank interrupt on the pipe requested, then
1450N/A * sleeps waiting for the requested sequence number to occur, and drops
1450N/A * the vblank interrupt refcount afterwards. (vblank irq disable follows that
1450N/A * after a timeout with no further vblank waits scheduled).
1450N/A */
1450N/A/* LINTED */
1450N/Aint drm_wait_vblank(DRM_IOCTL_ARGS)
1450N/A{
1450N/A union drm_wait_vblank *vblwait = data;
1450N/A int ret = 0;
1450N/A unsigned int flags, seq, crtc, high_crtc;
1450N/A
1450N/A if ((!dev->pdev->irq) || (!dev->irq_enabled))
1450N/A return -EINVAL;
1450N/A
1450N/A if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1450N/A return -EINVAL;
1450N/A
1450N/A if (vblwait->request.type &
1450N/A ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1450N/A _DRM_VBLANK_HIGH_CRTC_MASK)) {
1450N/A DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1450N/A vblwait->request.type,
1450N/A (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1450N/A _DRM_VBLANK_HIGH_CRTC_MASK));
1450N/A return -EINVAL;
1450N/A }
1450N/A
1450N/A flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1450N/A high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1450N/A if (high_crtc)
1450N/A crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1450N/A else
1450N/A crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1450N/A if (crtc >= dev->num_crtcs)
1450N/A return -EINVAL;
1450N/A
1450N/A ret = drm_vblank_get(dev, crtc);
1450N/A if (ret) {
1450N/A DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
1450N/A return ret;
1450N/A }
1450N/A seq = drm_vblank_count(dev, crtc);
1450N/A
1450N/A switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1450N/A case _DRM_VBLANK_RELATIVE:
1450N/A vblwait->request.sequence += seq;
1450N/A vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1450N/A /* LINTED */
1450N/A case _DRM_VBLANK_ABSOLUTE:
1450N/A break;
1450N/A default:
1450N/A ret = -EINVAL;
1450N/A goto done;
1450N/A }
1450N/A
1450N/A if (flags & _DRM_VBLANK_EVENT) {
1450N/A /* must hold on to the vblank ref until the event fires
1450N/A * drm_vblank_put will be called asynchronously
1450N/A */
1450N/A return drm_queue_vblank_event(dev, crtc, vblwait, file);
1450N/A }
1450N/A
1450N/A if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1450N/A (seq - vblwait->request.sequence) <= (1<<23)) {
1450N/A vblwait->request.sequence = seq + 1;
1450N/A }
1450N/A
1450N/A DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1450N/A vblwait->request.sequence, crtc);
1450N/A dev->last_vblank_wait[crtc] = vblwait->request.sequence;
1450N/A DRM_WAIT_ON(ret, &dev->vbl_queue[crtc], 3 * DRM_HZ,
1450N/A (((drm_vblank_count(dev, crtc) -
1450N/A vblwait->request.sequence) <= (1 << 23)) ||
1450N/A !dev->irq_enabled));
1450N/A
1450N/A if (ret != -EINTR) {
1450N/A struct timeval now;
1450N/A
1450N/A vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
1450N/A
1450N/A vblwait->reply.tval_sec = now.tv_sec;
1450N/A vblwait->reply.tval_usec = now.tv_usec;
1450N/A DRM_DEBUG("returning %d to client\n",
1450N/A vblwait->reply.sequence);
1450N/A } else {
1450N/A DRM_DEBUG("vblank wait interrupted by signal\n");
1450N/A }
1450N/A
1450N/Adone:
1450N/A drm_vblank_put(dev, crtc);
1450N/A return ret;
1450N/A}
1450N/A
1450N/Astatic void drm_handle_vblank_events(struct drm_device *dev, int crtc)
1450N/A{
1450N/A struct drm_pending_vblank_event *e, *t;
1450N/A struct timeval now;
1450N/A unsigned long flags;
1450N/A unsigned int seq;
1450N/A
1450N/A seq = drm_vblank_count_and_time(dev, crtc, &now);
1450N/A
1450N/A spin_lock_irqsave(&dev->event_lock, flags);
1450N/A
1450N/A list_for_each_entry_safe(e, t, struct drm_pending_vblank_event,
1450N/A &dev->vblank_event_list, base.link) {
1450N/A if (e->pipe != crtc)
1450N/A continue;
1450N/A if ((seq - e->event.sequence) > (1<<23))
1450N/A continue;
1450N/A
1450N/A DRM_DEBUG("vblank event on %d, current %d\n",
1450N/A e->event.sequence, seq);
1450N/A
1450N/A list_del(&e->base.link);
1450N/A drm_vblank_put(dev, e->pipe);
1450N/A send_vblank_event(dev, e, seq, &now);
1450N/A pollwakeup(&e->base.file_priv->drm_pollhead, POLLIN | POLLRDNORM);
1450N/A }
1450N/A
1450N/A spin_unlock_irqrestore(&dev->event_lock, flags);
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_handle_vblank - handle a vblank event
1450N/A * @dev: DRM device
1450N/A * @crtc: where this event occurred
1450N/A *
1450N/A * Drivers should call this routine in their vblank interrupt handlers to
1450N/A * update the vblank counter and send any signals that may be pending.
1450N/A */
1450N/Abool drm_handle_vblank(struct drm_device *dev, int crtc)
1450N/A{
1450N/A u32 vblcount;
1450N/A s64 diff_ns;
1450N/A struct timeval tvblank;
1450N/A unsigned long irqflags;
1450N/A
1450N/A if (!dev->num_crtcs)
1450N/A return false;
1450N/A
1450N/A /* Need timestamp lock to prevent concurrent execution with
1450N/A * vblank enable/disable, as this would cause inconsistent
1450N/A * or corrupted timestamps and vblank counts.
1450N/A */
1450N/A spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
1450N/A
1450N/A /* Vblank irq handling disabled. Nothing to do. */
1450N/A if (!dev->vblank_enabled[crtc]) {
1450N/A spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
1450N/A return false;
1450N/A }
1450N/A
1450N/A /* Fetch corresponding timestamp for this vblank interval from
1450N/A * driver and store it in proper slot of timestamp ringbuffer.
1450N/A */
1450N/A
1450N/A /* Get current timestamp and count. */
1450N/A vblcount = atomic_read(&dev->_vblank_count[crtc]);
1450N/A (void) drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1450N/A
1450N/A /* Compute time difference to timestamp of last vblank */
1450N/A diff_ns = timeval_to_ns(&tvblank) -
1450N/A timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1450N/A
1450N/A /* Update vblank timestamp and count if at least
1450N/A * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1450N/A * difference between last stored timestamp and current
1450N/A * timestamp. A smaller difference means basically
1450N/A * identical timestamps. Happens if this vblank has
1450N/A * been already processed and this is a redundant call,
1450N/A * e.g., due to spurious vblank interrupts. We need to
1450N/A * ignore those for accounting.
1450N/A */
1450N/A if (abs(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
1450N/A /* Store new timestamp in ringbuffer. */
1450N/A vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
1450N/A
1450N/A /* Increment cooked vblank count. This also atomically commits
1450N/A * the timestamp computed above.
1450N/A */
1450N/A atomic_inc(&dev->_vblank_count[crtc]);
1450N/A } else {
1450N/A DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1450N/A crtc, (int) diff_ns);
1450N/A }
1450N/A
1450N/A DRM_WAKEUP(&dev->vbl_queue[crtc]);
1450N/A drm_handle_vblank_events(dev, crtc);
1450N/A
1450N/A spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
1450N/A return true;
1450N/A}