Subversion Repositories Kolibri OS

Rev

Rev 6088 | Rev 6660 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * drm_irq.c IRQ and vblank support
  3.  *
  4.  * \author Rickard E. (Rik) Faith <faith@valinux.com>
  5.  * \author Gareth Hughes <gareth@valinux.com>
  6.  */
  7.  
  8. /*
  9.  * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
  10.  *
  11.  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  12.  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13.  * All Rights Reserved.
  14.  *
  15.  * Permission is hereby granted, free of charge, to any person obtaining a
  16.  * copy of this software and associated documentation files (the "Software"),
  17.  * to deal in the Software without restriction, including without limitation
  18.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19.  * and/or sell copies of the Software, and to permit persons to whom the
  20.  * Software is furnished to do so, subject to the following conditions:
  21.  *
  22.  * The above copyright notice and this permission notice (including the next
  23.  * paragraph) shall be included in all copies or substantial portions of the
  24.  * Software.
  25.  *
  26.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  29.  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32.  * OTHER DEALINGS IN THE SOFTWARE.
  33.  */
  34.  
  35. #include <drm/drmP.h>
  36. //#include "drm_trace.h"
  37. #include "drm_internal.h"
  38.  
  39. //#include <linux/interrupt.h>   /* For task queue support */
  40. #include <linux/slab.h>
  41.  
  42. #include <linux/vgaarb.h>
  43. #include <linux/export.h>
  44.  
  45. ktime_t ktime_get(void);
  46.  
  47. static inline ktime_t ktime_get_real(void)
  48. {
  49.         return ktime_get();
  50. }
  51.  
  52. static inline ktime_t ktime_mono_to_real(ktime_t mono)
  53. {
  54.         return mono;
  55. }
  56.  
  57. irqreturn_t device_irq_handler(struct drm_device *dev)
  58. {
  59.         return dev->driver->irq_handler(0, dev);
  60. }
  61.  
  62. /* Access macro for slots in vblank timestamp ringbuffer. */
  63. #define vblanktimestamp(dev, pipe, count) \
  64.         ((dev)->vblank[pipe].time[(count) % DRM_VBLANKTIME_RBSIZE])
  65.  
  66. /* Retry timestamp calculation up to 3 times to satisfy
  67.  * drm_timestamp_precision before giving up.
  68.  */
  69. #define DRM_TIMESTAMP_MAXRETRIES 3
  70.  
  71. /* Threshold in nanoseconds for detection of redundant
  72.  * vblank irq in drm_handle_vblank(). 1 msec should be ok.
  73.  */
  74. #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
  75.  
  76. static bool
  77. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  78.                           struct timeval *tvblank, unsigned flags);
  79.  
  80. static unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
  81.  
  82. /*
  83.  * Default to use monotonic timestamps for wait-for-vblank and page-flip
  84.  * complete events.
  85.  */
  86. unsigned int drm_timestamp_monotonic = 1;
  87.  
  88. static int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
  89.  
  90. module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
  91. module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
  92. module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
  93.  
  94. static void store_vblank(struct drm_device *dev, unsigned int pipe,
  95.                          u32 vblank_count_inc,
  96.                          struct timeval *t_vblank, u32 last)
  97. {
  98.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  99.         u32 tslot;
  100.  
  101.         assert_spin_locked(&dev->vblank_time_lock);
  102.  
  103.         vblank->last = last;
  104.  
  105.         /* All writers hold the spinlock, but readers are serialized by
  106.          * the latching of vblank->count below.
  107.          */
  108.         tslot = vblank->count + vblank_count_inc;
  109.         vblanktimestamp(dev, pipe, tslot) = *t_vblank;
  110.  
  111.         /*
  112.          * vblank timestamp updates are protected on the write side with
  113.          * vblank_time_lock, but on the read side done locklessly using a
  114.          * sequence-lock on the vblank counter. Ensure correct ordering using
  115.          * memory barrriers. We need the barrier both before and also after the
  116.          * counter update to synchronize with the next timestamp write.
  117.          * The read-side barriers for this are in drm_vblank_count_and_time.
  118.          */
  119.         smp_wmb();
  120.         vblank->count += vblank_count_inc;
  121.         smp_wmb();
  122. }
  123.  
  124. /**
  125.  * drm_reset_vblank_timestamp - reset the last timestamp to the last vblank
  126.  * @dev: DRM device
  127.  * @pipe: index of CRTC for which to reset the timestamp
  128.  *
  129.  * Reset the stored timestamp for the current vblank count to correspond
  130.  * to the last vblank occurred.
  131.  *
  132.  * Only to be called from drm_vblank_on().
  133.  *
  134.  * Note: caller must hold dev->vbl_lock since this reads & writes
  135.  * device vblank fields.
  136.  */
  137. static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
  138. {
  139.         u32 cur_vblank;
  140.         bool rc;
  141.         struct timeval t_vblank;
  142.         int count = DRM_TIMESTAMP_MAXRETRIES;
  143.  
  144.         spin_lock(&dev->vblank_time_lock);
  145.  
  146.         /*
  147.          * sample the current counter to avoid random jumps
  148.          * when drm_vblank_enable() applies the diff
  149.          */
  150.         do {
  151.                 cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
  152.                 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, 0);
  153.         } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
  154.  
  155.         /*
  156.          * Only reinitialize corresponding vblank timestamp if high-precision query
  157.          * available and didn't fail. Otherwise reinitialize delayed at next vblank
  158.          * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
  159.          */
  160.         if (!rc)
  161.                 t_vblank = (struct timeval) {0, 0};
  162.  
  163.         /*
  164.          * +1 to make sure user will never see the same
  165.          * vblank counter value before and after a modeset
  166.          */
  167.         store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
  168.  
  169.         spin_unlock(&dev->vblank_time_lock);
  170. }
  171.  
  172. /**
  173.  * drm_update_vblank_count - update the master vblank counter
  174.  * @dev: DRM device
  175.  * @pipe: counter to update
  176.  *
  177.  * Call back into the driver to update the appropriate vblank counter
  178.  * (specified by @pipe).  Deal with wraparound, if it occurred, and
  179.  * update the last read value so we can deal with wraparound on the next
  180.  * call if necessary.
  181.  *
  182.  * Only necessary when going from off->on, to account for frames we
  183.  * didn't get an interrupt for.
  184.  *
  185.  * Note: caller must hold dev->vbl_lock since this reads & writes
  186.  * device vblank fields.
  187.  */
  188. static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
  189.                                     unsigned long flags)
  190. {
  191.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  192.         u32 cur_vblank, diff;
  193.         bool rc;
  194.         struct timeval t_vblank;
  195.         int count = DRM_TIMESTAMP_MAXRETRIES;
  196.         int framedur_ns = vblank->framedur_ns;
  197.  
  198.         /*
  199.          * Interrupts were disabled prior to this call, so deal with counter
  200.          * wrap if needed.
  201.          * NOTE!  It's possible we lost a full dev->max_vblank_count + 1 events
  202.          * here if the register is small or we had vblank interrupts off for
  203.          * a long time.
  204.          *
  205.          * We repeat the hardware vblank counter & timestamp query until
  206.          * we get consistent results. This to prevent races between gpu
  207.          * updating its hardware counter while we are retrieving the
  208.          * corresponding vblank timestamp.
  209.          */
  210.         do {
  211.                 cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
  212.                 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, flags);
  213.         } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
  214.  
  215.         if (dev->max_vblank_count != 0) {
  216.                 /* trust the hw counter when it's around */
  217.                 diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
  218.         } else if (rc && framedur_ns) {
  219.                 const struct timeval *t_old;
  220.                 u64 diff_ns;
  221.  
  222.                 t_old = &vblanktimestamp(dev, pipe, vblank->count);
  223.                 diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
  224.  
  225.                 /*
  226.                  * Figure out how many vblanks we've missed based
  227.                  * on the difference in the timestamps and the
  228.                  * frame/field duration.
  229.                  */
  230.                 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
  231.  
  232.                 if (diff == 0 && flags & DRM_CALLED_FROM_VBLIRQ)
  233.                         DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
  234.                                       " diff_ns = %lld, framedur_ns = %d)\n",
  235.                                       pipe, (long long) diff_ns, framedur_ns);
  236.         } else {
  237.                 /* some kind of default for drivers w/o accurate vbl timestamping */
  238.                 diff = (flags & DRM_CALLED_FROM_VBLIRQ) != 0;
  239.         }
  240.  
  241.         DRM_DEBUG_VBL("updating vblank count on crtc %u:"
  242.                       " current=%u, diff=%u, hw=%u hw_last=%u\n",
  243.                       pipe, vblank->count, diff, cur_vblank, vblank->last);
  244.  
  245.         if (diff == 0) {
  246.                 WARN_ON_ONCE(cur_vblank != vblank->last);
  247.                 return;
  248.         }
  249.  
  250.         /*
  251.          * Only reinitialize corresponding vblank timestamp if high-precision query
  252.          * available and didn't fail, or we were called from the vblank interrupt.
  253.          * Otherwise reinitialize delayed at next vblank interrupt and assign 0
  254.          * for now, to mark the vblanktimestamp as invalid.
  255.          */
  256.         if (!rc && (flags & DRM_CALLED_FROM_VBLIRQ) == 0)
  257.                 t_vblank = (struct timeval) {0, 0};
  258.  
  259.         store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
  260. }
  261.  
  262. /*
  263.  * Disable vblank irq's on crtc, make sure that last vblank count
  264.  * of hardware and corresponding consistent software vblank counter
  265.  * are preserved, even if there are any spurious vblank irq's after
  266.  * disable.
  267.  */
  268. static void vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
  269. {
  270.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  271.         unsigned long irqflags;
  272.  
  273.         /* Prevent vblank irq processing while disabling vblank irqs,
  274.          * so no updates of timestamps or count can happen after we've
  275.          * disabled. Needed to prevent races in case of delayed irq's.
  276.          */
  277.         spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  278.  
  279.         /*
  280.          * Only disable vblank interrupts if they're enabled. This avoids
  281.          * calling the ->disable_vblank() operation in atomic context with the
  282.          * hardware potentially runtime suspended.
  283.          */
  284.         if (vblank->enabled) {
  285.                 dev->driver->disable_vblank(dev, pipe);
  286.                 vblank->enabled = false;
  287.         }
  288.  
  289.         /*
  290.          * Always update the count and timestamp to maintain the
  291.          * appearance that the counter has been ticking all along until
  292.          * this time. This makes the count account for the entire time
  293.          * between drm_vblank_on() and drm_vblank_off().
  294.          */
  295.         drm_update_vblank_count(dev, pipe, 0);
  296.  
  297.         spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  298. }
  299.  
  300. static void vblank_disable_fn(unsigned long arg)
  301. {
  302.         struct drm_vblank_crtc *vblank = (void *)arg;
  303.         struct drm_device *dev = vblank->dev;
  304.         unsigned int pipe = vblank->pipe;
  305.         unsigned long irqflags;
  306.  
  307.         if (!dev->vblank_disable_allowed)
  308.                 return;
  309.  
  310.         spin_lock_irqsave(&dev->vbl_lock, irqflags);
  311.         if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
  312.                 DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
  313.                 vblank_disable_and_save(dev, pipe);
  314.         }
  315.         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  316. }
  317.  
  318. /**
  319.  * drm_vblank_cleanup - cleanup vblank support
  320.  * @dev: DRM device
  321.  *
  322.  * This function cleans up any resources allocated in drm_vblank_init.
  323.  */
  324. void drm_vblank_cleanup(struct drm_device *dev)
  325. {
  326.         unsigned int pipe;
  327.  
  328.         /* Bail if the driver didn't call drm_vblank_init() */
  329.         if (dev->num_crtcs == 0)
  330.                 return;
  331.  
  332.         for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
  333.                 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  334.  
  335.                 WARN_ON(vblank->enabled &&
  336.                         drm_core_check_feature(dev, DRIVER_MODESET));
  337.  
  338.                 del_timer_sync(&vblank->disable_timer);
  339.         }
  340.  
  341.         kfree(dev->vblank);
  342.  
  343.         dev->num_crtcs = 0;
  344. }
  345. EXPORT_SYMBOL(drm_vblank_cleanup);
  346.  
  347. /**
  348.  * drm_vblank_init - initialize vblank support
  349.  * @dev: DRM device
  350.  * @num_crtcs: number of CRTCs supported by @dev
  351.  *
  352.  * This function initializes vblank support for @num_crtcs display pipelines.
  353.  *
  354.  * Returns:
  355.  * Zero on success or a negative error code on failure.
  356.  */
  357. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
  358. {
  359.         int ret = -ENOMEM;
  360.         unsigned int i;
  361.  
  362.         spin_lock_init(&dev->vbl_lock);
  363.         spin_lock_init(&dev->vblank_time_lock);
  364.  
  365.         dev->num_crtcs = num_crtcs;
  366.  
  367.         dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
  368.         if (!dev->vblank)
  369.                 goto err;
  370.  
  371.         for (i = 0; i < num_crtcs; i++) {
  372.                 struct drm_vblank_crtc *vblank = &dev->vblank[i];
  373.  
  374.                 vblank->dev = dev;
  375.                 vblank->pipe = i;
  376.                 init_waitqueue_head(&vblank->queue);
  377.                 setup_timer(&vblank->disable_timer, vblank_disable_fn,
  378.                             (unsigned long)vblank);
  379.         }
  380.  
  381.         DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
  382.  
  383.         /* Driver specific high-precision vblank timestamping supported? */
  384.         if (dev->driver->get_vblank_timestamp)
  385.                 DRM_INFO("Driver supports precise vblank timestamp query.\n");
  386.         else
  387.                 DRM_INFO("No driver support for vblank timestamp query.\n");
  388.  
  389.         /* Must have precise timestamping for reliable vblank instant disable */
  390.         if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
  391.                 dev->vblank_disable_immediate = false;
  392.                 DRM_INFO("Setting vblank_disable_immediate to false because "
  393.                          "get_vblank_timestamp == NULL\n");
  394.         }
  395.  
  396.         dev->vblank_disable_allowed = false;
  397.  
  398.         return 0;
  399.  
  400. err:
  401.         dev->num_crtcs = 0;
  402.         return ret;
  403. }
  404. EXPORT_SYMBOL(drm_vblank_init);
  405.  
  406.  
  407.  
  408.  
  409. /**
  410.  * drm_irq_install - install IRQ handler
  411.  * @dev: DRM device
  412.  * @irq: IRQ number to install the handler for
  413.  *
  414.  * Initializes the IRQ related data. Installs the handler, calling the driver
  415.  * irq_preinstall() and irq_postinstall() functions before and after the
  416.  * installation.
  417.  *
  418.  * This is the simplified helper interface provided for drivers with no special
  419.  * needs. Drivers which need to install interrupt handlers for multiple
  420.  * interrupts must instead set drm_device->irq_enabled to signal the DRM core
  421.  * that vblank interrupts are available.
  422.  *
  423.  * Returns:
  424.  * Zero on success or a negative error code on failure.
  425.  */
  426. int drm_irq_install(struct drm_device *dev, int irq)
  427. {
  428.         int ret;
  429.         unsigned long sh_flags = 0;
  430.  
  431.         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  432.                 return -EINVAL;
  433.  
  434.         if (irq == 0)
  435.                 return -EINVAL;
  436.  
  437.         /* Driver must have been initialized */
  438.         if (!dev->dev_private)
  439.                 return -EINVAL;
  440.  
  441.         if (dev->irq_enabled)
  442.                 return -EBUSY;
  443.         dev->irq_enabled = true;
  444.  
  445.         DRM_DEBUG("irq=%d\n", irq);
  446.  
  447.         /* Before installing handler */
  448.         if (dev->driver->irq_preinstall)
  449.                 dev->driver->irq_preinstall(dev);
  450.  
  451.     ret = !AttachIntHandler(irq, device_irq_handler, (u32)dev);
  452.  
  453.         /* After installing handler */
  454.         if (dev->driver->irq_postinstall)
  455.                 ret = dev->driver->irq_postinstall(dev);
  456.  
  457.         if (ret < 0) {
  458.                 dev->irq_enabled = false;
  459.         } else {
  460.                 dev->irq = irq;
  461.         }
  462.  
  463.     u16 cmd = PciRead16(dev->pdev->busnr, dev->pdev->devfn, 4);
  464.     cmd&= ~(1<<10);
  465.     PciWrite16(dev->pdev->busnr, dev->pdev->devfn, 4, cmd);
  466.  
  467.     return ret;
  468. }
  469. EXPORT_SYMBOL(drm_irq_install);
  470.  
  471.  
  472.  
  473.  
  474.  
  475. /**
  476.  * drm_calc_timestamping_constants - calculate vblank timestamp constants
  477.  * @crtc: drm_crtc whose timestamp constants should be updated.
  478.  * @mode: display mode containing the scanout timings
  479.  *
  480.  * Calculate and store various constants which are later
  481.  * needed by vblank and swap-completion timestamping, e.g,
  482.  * by drm_calc_vbltimestamp_from_scanoutpos(). They are
  483.  * derived from CRTC's true scanout timing, so they take
  484.  * things like panel scaling or other adjustments into account.
  485.  */
  486. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  487.                                      const struct drm_display_mode *mode)
  488. {
  489.         struct drm_device *dev = crtc->dev;
  490.         unsigned int pipe = drm_crtc_index(crtc);
  491.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  492.         int linedur_ns = 0, framedur_ns = 0;
  493.         int dotclock = mode->crtc_clock;
  494.  
  495.         if (!dev->num_crtcs)
  496.                 return;
  497.  
  498.         if (WARN_ON(pipe >= dev->num_crtcs))
  499.                 return;
  500.  
  501.         /* Valid dotclock? */
  502.         if (dotclock > 0) {
  503.                 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
  504.  
  505.                 /*
  506.                  * Convert scanline length in pixels and video
  507.                  * dot clock to line duration and frame duration
  508.                  * in nanoseconds:
  509.                  */
  510.                 linedur_ns  = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
  511.                 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
  512.  
  513.                 /*
  514.                  * Fields of interlaced scanout modes are only half a frame duration.
  515.                  */
  516.                 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  517.                         framedur_ns /= 2;
  518.         } else
  519.                 DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
  520.                           crtc->base.id);
  521.  
  522.         vblank->linedur_ns  = linedur_ns;
  523.         vblank->framedur_ns = framedur_ns;
  524.  
  525.         DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  526.                   crtc->base.id, mode->crtc_htotal,
  527.                   mode->crtc_vtotal, mode->crtc_vdisplay);
  528.         DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
  529.                   crtc->base.id, dotclock, framedur_ns, linedur_ns);
  530. }
  531. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  532.  
  533. /**
  534.  * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
  535.  * @dev: DRM device
  536.  * @pipe: index of CRTC whose vblank timestamp to retrieve
  537.  * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  538.  *             On return contains true maximum error of timestamp
  539.  * @vblank_time: Pointer to struct timeval which should receive the timestamp
  540.  * @flags: Flags to pass to driver:
  541.  *         0 = Default,
  542.  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  543.  * @mode: mode which defines the scanout timings
  544.  *
  545.  * Implements calculation of exact vblank timestamps from given drm_display_mode
  546.  * timings and current video scanout position of a CRTC. This can be called from
  547.  * within get_vblank_timestamp() implementation of a kms driver to implement the
  548.  * actual timestamping.
  549.  *
  550.  * Should return timestamps conforming to the OML_sync_control OpenML
  551.  * extension specification. The timestamp corresponds to the end of
  552.  * the vblank interval, aka start of scanout of topmost-leftmost display
  553.  * pixel in the following video frame.
  554.  *
  555.  * Requires support for optional dev->driver->get_scanout_position()
  556.  * in kms driver, plus a bit of setup code to provide a drm_display_mode
  557.  * that corresponds to the true scanout timing.
  558.  *
  559.  * The current implementation only handles standard video modes. It
  560.  * returns as no operation if a doublescan or interlaced video mode is
  561.  * active. Higher level code is expected to handle this.
  562.  *
  563.  * Returns:
  564.  * Negative value on error, failure or if not supported in current
  565.  * video mode:
  566.  *
  567.  * -EINVAL   - Invalid CRTC.
  568.  * -EAGAIN   - Temporary unavailable, e.g., called before initial modeset.
  569.  * -ENOTSUPP - Function not supported in current display mode.
  570.  * -EIO      - Failed, e.g., due to failed scanout position query.
  571.  *
  572.  * Returns or'ed positive status flags on success:
  573.  *
  574.  * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
  575.  * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
  576.  *
  577.  */
  578. int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
  579.                                           unsigned int pipe,
  580.                                           int *max_error,
  581.                                           struct timeval *vblank_time,
  582.                                           unsigned flags,
  583.                                           const struct drm_display_mode *mode)
  584. {
  585.         struct timeval tv_etime;
  586.         ktime_t stime, etime;
  587.         unsigned int vbl_status;
  588.         int ret = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
  589.         int vpos, hpos, i;
  590.         int delta_ns, duration_ns;
  591.  
  592.         if (pipe >= dev->num_crtcs) {
  593.                 DRM_ERROR("Invalid crtc %u\n", pipe);
  594.                 return -EINVAL;
  595.         }
  596.  
  597.         /* Scanout position query not supported? Should not happen. */
  598.         if (!dev->driver->get_scanout_position) {
  599.                 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  600.                 return -EIO;
  601.         }
  602.  
  603.         /* If mode timing undefined, just return as no-op:
  604.          * Happens during initial modesetting of a crtc.
  605.          */
  606.         if (mode->crtc_clock == 0) {
  607.                 DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
  608.                 return -EAGAIN;
  609.         }
  610.  
  611.         /* Get current scanout position with system timestamp.
  612.          * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  613.          * if single query takes longer than max_error nanoseconds.
  614.          *
  615.          * This guarantees a tight bound on maximum error if
  616.          * code gets preempted or delayed for some reason.
  617.          */
  618.         for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  619.                 /*
  620.                  * Get vertical and horizontal scanout position vpos, hpos,
  621.                  * and bounding timestamps stime, etime, pre/post query.
  622.                  */
  623.                 vbl_status = dev->driver->get_scanout_position(dev, pipe, flags,
  624.                                                                &vpos, &hpos,
  625.                                                                &stime, &etime,
  626.                                                                mode);
  627.  
  628.                 /* Return as no-op if scanout query unsupported or failed. */
  629.                 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
  630.                         DRM_DEBUG("crtc %u : scanoutpos query failed [0x%x].\n",
  631.                                   pipe, vbl_status);
  632.                         return -EIO;
  633.                 }
  634.  
  635.                 /* Compute uncertainty in timestamp of scanout position query. */
  636.                 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  637.  
  638.                 /* Accept result with <  max_error nsecs timing uncertainty. */
  639.                 if (duration_ns <= *max_error)
  640.                         break;
  641.         }
  642.  
  643.         /* Noisy system timing? */
  644.         if (i == DRM_TIMESTAMP_MAXRETRIES) {
  645.                 DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
  646.                           pipe, duration_ns/1000, *max_error/1000, i);
  647.         }
  648.  
  649.         /* Return upper bound of timestamp precision error. */
  650.         *max_error = duration_ns;
  651.  
  652.         /* Check if in vblank area:
  653.          * vpos is >=0 in video scanout area, but negative
  654.          * within vblank area, counting down the number of lines until
  655.          * start of scanout.
  656.          */
  657.         if (vbl_status & DRM_SCANOUTPOS_IN_VBLANK)
  658.                 ret |= DRM_VBLANKTIME_IN_VBLANK;
  659.  
  660.         /* Convert scanout position into elapsed time at raw_time query
  661.          * since start of scanout at first display scanline. delta_ns
  662.          * can be negative if start of scanout hasn't happened yet.
  663.          */
  664.         delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
  665.                            mode->crtc_clock);
  666.  
  667.         if (!drm_timestamp_monotonic)
  668.                 etime = ktime_mono_to_real(etime);
  669.  
  670.         /* save this only for debugging purposes */
  671.         tv_etime = ktime_to_timeval(etime);
  672.         /* Subtract time delta from raw timestamp to get final
  673.          * vblank_time timestamp for end of vblank.
  674.          */
  675.         if (delta_ns < 0)
  676.                 etime = ktime_add_ns(etime, -delta_ns);
  677.         else
  678.                 etime = ktime_sub_ns(etime, delta_ns);
  679.         *vblank_time = ktime_to_timeval(etime);
  680.  
  681.         DRM_DEBUG_VBL("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  682.                       pipe, vbl_status, hpos, vpos,
  683.                       (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  684.                       (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  685.                       duration_ns/1000, i);
  686.  
  687.         return ret;
  688. }
  689. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  690.  
  691. static struct timeval get_drm_timestamp(void)
  692. {
  693.         ktime_t now;
  694.  
  695.         now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
  696.         return ktime_to_timeval(now);
  697. }
  698.  
  699. /**
  700.  * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  701.  *                             vblank interval
  702.  * @dev: DRM device
  703.  * @pipe: index of CRTC whose vblank timestamp to retrieve
  704.  * @tvblank: Pointer to target struct timeval which should receive the timestamp
  705.  * @flags: Flags to pass to driver:
  706.  *         0 = Default,
  707.  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  708.  *
  709.  * Fetches the system timestamp corresponding to the time of the most recent
  710.  * vblank interval on specified CRTC. May call into kms-driver to
  711.  * compute the timestamp with a high-precision GPU specific method.
  712.  *
  713.  * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  714.  * call, i.e., it isn't very precisely locked to the true vblank.
  715.  *
  716.  * Returns:
  717.  * True if timestamp is considered to be very precise, false otherwise.
  718.  */
  719. static bool
  720. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  721.                           struct timeval *tvblank, unsigned flags)
  722. {
  723.         int ret;
  724.  
  725.         /* Define requested maximum error on timestamps (nanoseconds). */
  726.         int max_error = (int) drm_timestamp_precision * 1000;
  727.  
  728.         /* Query driver if possible and precision timestamping enabled. */
  729.         if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
  730.                 ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
  731.                                                         tvblank, flags);
  732.                 if (ret > 0)
  733.                         return true;
  734.         }
  735.  
  736.         /* GPU high precision timestamp query unsupported or failed.
  737.          * Return current monotonic/gettimeofday timestamp as best estimate.
  738.          */
  739.         *tvblank = get_drm_timestamp();
  740.  
  741.         return false;
  742. }
  743.  
  744. /**
  745.  * drm_vblank_count - retrieve "cooked" vblank counter value
  746.  * @dev: DRM device
  747.  * @pipe: index of CRTC for which to retrieve the counter
  748.  *
  749.  * Fetches the "cooked" vblank count value that represents the number of
  750.  * vblank events since the system was booted, including lost events due to
  751.  * modesetting activity.
  752.  *
  753.  * This is the legacy version of drm_crtc_vblank_count().
  754.  *
  755.  * Returns:
  756.  * The software vblank counter.
  757.  */
  758. u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
  759. {
  760.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  761.  
  762.         if (WARN_ON(pipe >= dev->num_crtcs))
  763.                 return 0;
  764.  
  765.         return vblank->count;
  766. }
  767. EXPORT_SYMBOL(drm_vblank_count);
  768.  
  769. /**
  770.  * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
  771.  * @crtc: which counter to retrieve
  772.  *
  773.  * Fetches the "cooked" vblank count value that represents the number of
  774.  * vblank events since the system was booted, including lost events due to
  775.  * modesetting activity.
  776.  *
  777.  * This is the native KMS version of drm_vblank_count().
  778.  *
  779.  * Returns:
  780.  * The software vblank counter.
  781.  */
  782. u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
  783. {
  784.         return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
  785. }
  786. EXPORT_SYMBOL(drm_crtc_vblank_count);
  787.  
  788. /**
  789.  * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
  790.  *     system timestamp corresponding to that vblank counter value.
  791.  * @dev: DRM device
  792.  * @pipe: index of CRTC whose counter to retrieve
  793.  * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  794.  *
  795.  * Fetches the "cooked" vblank count value that represents the number of
  796.  * vblank events since the system was booted, including lost events due to
  797.  * modesetting activity. Returns corresponding system timestamp of the time
  798.  * of the vblank interval that corresponds to the current vblank counter value.
  799.  *
  800.  * This is the legacy version of drm_crtc_vblank_count_and_time().
  801.  */
  802. u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
  803.                               struct timeval *vblanktime)
  804. {
  805.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  806.         int count = DRM_TIMESTAMP_MAXRETRIES;
  807.         u32 cur_vblank;
  808.  
  809.         if (WARN_ON(pipe >= dev->num_crtcs))
  810.                 return 0;
  811.  
  812.         /*
  813.          * Vblank timestamps are read lockless. To ensure consistency the vblank
  814.          * counter is rechecked and ordering is ensured using memory barriers.
  815.          * This works like a seqlock. The write-side barriers are in store_vblank.
  816.          */
  817.         do {
  818.                 cur_vblank = vblank->count;
  819.                 smp_rmb();
  820.                 *vblanktime = vblanktimestamp(dev, pipe, cur_vblank);
  821.                 smp_rmb();
  822.         } while (cur_vblank != vblank->count && --count > 0);
  823.  
  824.         return cur_vblank;
  825. }
  826. EXPORT_SYMBOL(drm_vblank_count_and_time);
  827.  
  828. /**
  829.  * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
  830.  *     and the system timestamp corresponding to that vblank counter value
  831.  * @crtc: which counter to retrieve
  832.  * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  833.  *
  834.  * Fetches the "cooked" vblank count value that represents the number of
  835.  * vblank events since the system was booted, including lost events due to
  836.  * modesetting activity. Returns corresponding system timestamp of the time
  837.  * of the vblank interval that corresponds to the current vblank counter value.
  838.  *
  839.  * This is the native KMS version of drm_vblank_count_and_time().
  840.  */
  841. u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  842.                                    struct timeval *vblanktime)
  843. {
  844.         return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
  845.                                          vblanktime);
  846. }
  847. EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
  848.  
  849. static void send_vblank_event(struct drm_device *dev,
  850.                 struct drm_pending_vblank_event *e,
  851.                 unsigned long seq, struct timeval *now)
  852. {
  853.         assert_spin_locked(&dev->event_lock);
  854.  
  855.         e->event.sequence = seq;
  856.         e->event.tv_sec = now->tv_sec;
  857.         e->event.tv_usec = now->tv_usec;
  858.  
  859.         list_add_tail(&e->base.link,
  860.                       &e->base.file_priv->event_list);
  861.         wake_up_interruptible(&e->base.file_priv->event_wait);
  862. }
  863.  
  864. /**
  865.  * drm_arm_vblank_event - arm vblank event after pageflip
  866.  * @dev: DRM device
  867.  * @pipe: CRTC index
  868.  * @e: the event to prepare to send
  869.  *
  870.  * A lot of drivers need to generate vblank events for the very next vblank
  871.  * interrupt. For example when the page flip interrupt happens when the page
  872.  * flip gets armed, but not when it actually executes within the next vblank
  873.  * period. This helper function implements exactly the required vblank arming
  874.  * behaviour.
  875.  *
  876.  * Caller must hold event lock. Caller must also hold a vblank reference for
  877.  * the event @e, which will be dropped when the next vblank arrives.
  878.  *
  879.  * This is the legacy version of drm_crtc_arm_vblank_event().
  880.  */
  881. void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
  882.                           struct drm_pending_vblank_event *e)
  883. {
  884.         assert_spin_locked(&dev->event_lock);
  885.  
  886.         e->pipe = pipe;
  887.         e->event.sequence = drm_vblank_count(dev, pipe);
  888.         list_add_tail(&e->base.link, &dev->vblank_event_list);
  889. }
  890. EXPORT_SYMBOL(drm_arm_vblank_event);
  891.  
  892. /**
  893.  * drm_crtc_arm_vblank_event - arm vblank event after pageflip
  894.  * @crtc: the source CRTC of the vblank event
  895.  * @e: the event to send
  896.  *
  897.  * A lot of drivers need to generate vblank events for the very next vblank
  898.  * interrupt. For example when the page flip interrupt happens when the page
  899.  * flip gets armed, but not when it actually executes within the next vblank
  900.  * period. This helper function implements exactly the required vblank arming
  901.  * behaviour.
  902.  *
  903.  * Caller must hold event lock. Caller must also hold a vblank reference for
  904.  * the event @e, which will be dropped when the next vblank arrives.
  905.  *
  906.  * This is the native KMS version of drm_arm_vblank_event().
  907.  */
  908. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  909.                                struct drm_pending_vblank_event *e)
  910. {
  911.         drm_arm_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
  912. }
  913. EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
  914.  
  915. /**
  916.  * drm_send_vblank_event - helper to send vblank event after pageflip
  917.  * @dev: DRM device
  918.  * @pipe: CRTC index
  919.  * @e: the event to send
  920.  *
  921.  * Updates sequence # and timestamp on event, and sends it to userspace.
  922.  * Caller must hold event lock.
  923.  *
  924.  * This is the legacy version of drm_crtc_send_vblank_event().
  925.  */
  926. void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
  927.                            struct drm_pending_vblank_event *e)
  928. {
  929.         struct timeval now;
  930.         unsigned int seq;
  931.  
  932.         if (dev->num_crtcs > 0) {
  933.                 seq = drm_vblank_count_and_time(dev, pipe, &now);
  934.         } else {
  935.                 seq = 0;
  936.  
  937.                 now = get_drm_timestamp();
  938.         }
  939.         e->pipe = pipe;
  940.         send_vblank_event(dev, e, seq, &now);
  941. }
  942. EXPORT_SYMBOL(drm_send_vblank_event);
  943.  
  944. /**
  945.  * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
  946.  * @crtc: the source CRTC of the vblank event
  947.  * @e: the event to send
  948.  *
  949.  * Updates sequence # and timestamp on event, and sends it to userspace.
  950.  * Caller must hold event lock.
  951.  *
  952.  * This is the native KMS version of drm_send_vblank_event().
  953.  */
  954. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  955.                                 struct drm_pending_vblank_event *e)
  956. {
  957.         drm_send_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
  958. }
  959. EXPORT_SYMBOL(drm_crtc_send_vblank_event);
  960.  
  961. /**
  962.  * drm_vblank_enable - enable the vblank interrupt on a CRTC
  963.  * @dev: DRM device
  964.  * @pipe: CRTC index
  965.  *
  966.  * Returns:
  967.  * Zero on success or a negative error code on failure.
  968.  */
  969. static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
  970. {
  971.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  972.         int ret = 0;
  973.  
  974.         assert_spin_locked(&dev->vbl_lock);
  975.  
  976.         spin_lock(&dev->vblank_time_lock);
  977.  
  978.         if (!vblank->enabled) {
  979.                 /*
  980.                  * Enable vblank irqs under vblank_time_lock protection.
  981.                  * All vblank count & timestamp updates are held off
  982.                  * until we are done reinitializing master counter and
  983.                  * timestamps. Filtercode in drm_handle_vblank() will
  984.                  * prevent double-accounting of same vblank interval.
  985.                  */
  986.                 ret = dev->driver->enable_vblank(dev, pipe);
  987.                 DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
  988.                 if (ret)
  989.                         atomic_dec(&vblank->refcount);
  990.                 else {
  991.                         vblank->enabled = true;
  992.                         drm_update_vblank_count(dev, pipe, 0);
  993.                 }
  994.         }
  995.  
  996.         spin_unlock(&dev->vblank_time_lock);
  997.  
  998.         return ret;
  999. }
  1000.  
  1001. /**
  1002.  * drm_vblank_get - get a reference count on vblank events
  1003.  * @dev: DRM device
  1004.  * @pipe: index of CRTC to own
  1005.  *
  1006.  * Acquire a reference count on vblank events to avoid having them disabled
  1007.  * while in use.
  1008.  *
  1009.  * This is the legacy version of drm_crtc_vblank_get().
  1010.  *
  1011.  * Returns:
  1012.  * Zero on success or a negative error code on failure.
  1013.  */
  1014. int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
  1015. {
  1016.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1017.         unsigned long irqflags;
  1018.         int ret = 0;
  1019.  
  1020.         if (!dev->num_crtcs)
  1021.                 return -EINVAL;
  1022.  
  1023.         if (WARN_ON(pipe >= dev->num_crtcs))
  1024.                 return -EINVAL;
  1025.  
  1026.         spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1027.         /* Going from 0->1 means we have to enable interrupts again */
  1028.         if (atomic_add_return(1, &vblank->refcount) == 1) {
  1029.                 ret = drm_vblank_enable(dev, pipe);
  1030.         } else {
  1031.                 if (!vblank->enabled) {
  1032.                         atomic_dec(&vblank->refcount);
  1033.                         ret = -EINVAL;
  1034.                 }
  1035.         }
  1036.         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1037.  
  1038.         return ret;
  1039. }
  1040. EXPORT_SYMBOL(drm_vblank_get);
  1041.  
  1042. /**
  1043.  * drm_crtc_vblank_get - get a reference count on vblank events
  1044.  * @crtc: which CRTC to own
  1045.  *
  1046.  * Acquire a reference count on vblank events to avoid having them disabled
  1047.  * while in use.
  1048.  *
  1049.  * This is the native kms version of drm_vblank_get().
  1050.  *
  1051.  * Returns:
  1052.  * Zero on success or a negative error code on failure.
  1053.  */
  1054. int drm_crtc_vblank_get(struct drm_crtc *crtc)
  1055. {
  1056.         return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
  1057. }
  1058. EXPORT_SYMBOL(drm_crtc_vblank_get);
  1059.  
  1060. /**
  1061.  * drm_vblank_put - release ownership of vblank events
  1062.  * @dev: DRM device
  1063.  * @pipe: index of CRTC to release
  1064.  *
  1065.  * Release ownership of a given vblank counter, turning off interrupts
  1066.  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1067.  *
  1068.  * This is the legacy version of drm_crtc_vblank_put().
  1069.  */
  1070. void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
  1071. {
  1072.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1073.  
  1074.         if (WARN_ON(pipe >= dev->num_crtcs))
  1075.                 return;
  1076.  
  1077.         if (WARN_ON(atomic_read(&vblank->refcount) == 0))
  1078.                 return;
  1079.  
  1080.         /* Last user schedules interrupt disable */
  1081.         if (atomic_dec_and_test(&vblank->refcount)) {
  1082.                 if (drm_vblank_offdelay == 0)
  1083.                         return;
  1084.                 else
  1085.                         vblank_disable_fn((unsigned long)vblank);
  1086.         }
  1087. }
  1088. EXPORT_SYMBOL(drm_vblank_put);
  1089.  
  1090. /**
  1091.  * drm_crtc_vblank_put - give up ownership of vblank events
  1092.  * @crtc: which counter to give up
  1093.  *
  1094.  * Release ownership of a given vblank counter, turning off interrupts
  1095.  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1096.  *
  1097.  * This is the native kms version of drm_vblank_put().
  1098.  */
  1099. void drm_crtc_vblank_put(struct drm_crtc *crtc)
  1100. {
  1101.         drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
  1102. }
  1103. EXPORT_SYMBOL(drm_crtc_vblank_put);
  1104.  
  1105. /**
  1106.  * drm_wait_one_vblank - wait for one vblank
  1107.  * @dev: DRM device
  1108.  * @pipe: CRTC index
  1109.  *
  1110.  * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
  1111.  * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
  1112.  * due to lack of driver support or because the crtc is off.
  1113.  */
  1114. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
  1115. {
  1116.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1117.         int ret;
  1118.         u32 last;
  1119.  
  1120.         if (WARN_ON(pipe >= dev->num_crtcs))
  1121.                 return;
  1122.  
  1123.         ret = drm_vblank_get(dev, pipe);
  1124.         if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
  1125.                 return;
  1126.  
  1127.         last = drm_vblank_count(dev, pipe);
  1128.  
  1129.         ret = wait_event_timeout(vblank->queue,
  1130.                                  last != drm_vblank_count(dev, pipe),
  1131.                                  msecs_to_jiffies(100));
  1132.  
  1133.         WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
  1134.  
  1135.         drm_vblank_put(dev, pipe);
  1136. }
  1137. EXPORT_SYMBOL(drm_wait_one_vblank);
  1138.  
  1139. /**
  1140.  * drm_crtc_wait_one_vblank - wait for one vblank
  1141.  * @crtc: DRM crtc
  1142.  *
  1143.  * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
  1144.  * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
  1145.  * due to lack of driver support or because the crtc is off.
  1146.  */
  1147. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
  1148. {
  1149.         drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
  1150. }
  1151. EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
  1152.  
  1153. /**
  1154.  * drm_vblank_off - disable vblank events on a CRTC
  1155.  * @dev: DRM device
  1156.  * @pipe: CRTC index
  1157.  *
  1158.  * Drivers can use this function to shut down the vblank interrupt handling when
  1159.  * disabling a crtc. This function ensures that the latest vblank frame count is
  1160.  * stored so that drm_vblank_on() can restore it again.
  1161.  *
  1162.  * Drivers must use this function when the hardware vblank counter can get
  1163.  * reset, e.g. when suspending.
  1164.  *
  1165.  * This is the legacy version of drm_crtc_vblank_off().
  1166.  */
  1167. void drm_vblank_off(struct drm_device *dev, unsigned int pipe)
  1168. {
  1169.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1170.         struct drm_pending_vblank_event *e, *t;
  1171.         struct timeval now;
  1172.         unsigned long irqflags;
  1173.         unsigned int seq;
  1174.  
  1175.         if (WARN_ON(pipe >= dev->num_crtcs))
  1176.                 return;
  1177.  
  1178.         spin_lock_irqsave(&dev->event_lock, irqflags);
  1179.  
  1180.         spin_lock(&dev->vbl_lock);
  1181.         vblank_disable_and_save(dev, pipe);
  1182.         wake_up(&vblank->queue);
  1183.  
  1184.         /*
  1185.          * Prevent subsequent drm_vblank_get() from re-enabling
  1186.          * the vblank interrupt by bumping the refcount.
  1187.          */
  1188.         if (!vblank->inmodeset) {
  1189.                 atomic_inc(&vblank->refcount);
  1190.                 vblank->inmodeset = 1;
  1191.         }
  1192.         spin_unlock(&dev->vbl_lock);
  1193.  
  1194.         /* Send any queued vblank events, lest the natives grow disquiet */
  1195.         seq = drm_vblank_count_and_time(dev, pipe, &now);
  1196.  
  1197.         list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1198.                 if (e->pipe != pipe)
  1199.                         continue;
  1200.                 DRM_DEBUG("Sending premature vblank event on disable: "
  1201.                           "wanted %d, current %d\n",
  1202.                           e->event.sequence, seq);
  1203.                 list_del(&e->base.link);
  1204.                 drm_vblank_put(dev, pipe);
  1205.                 send_vblank_event(dev, e, seq, &now);
  1206.         }
  1207.         spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1208. }
  1209. EXPORT_SYMBOL(drm_vblank_off);
  1210.  
  1211. /**
  1212.  * drm_crtc_vblank_off - disable vblank events on a CRTC
  1213.  * @crtc: CRTC in question
  1214.  *
  1215.  * Drivers can use this function to shut down the vblank interrupt handling when
  1216.  * disabling a crtc. This function ensures that the latest vblank frame count is
  1217.  * stored so that drm_vblank_on can restore it again.
  1218.  *
  1219.  * Drivers must use this function when the hardware vblank counter can get
  1220.  * reset, e.g. when suspending.
  1221.  *
  1222.  * This is the native kms version of drm_vblank_off().
  1223.  */
  1224. void drm_crtc_vblank_off(struct drm_crtc *crtc)
  1225. {
  1226.         drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
  1227. }
  1228. EXPORT_SYMBOL(drm_crtc_vblank_off);
  1229.  
  1230. /**
  1231.  * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
  1232.  * @crtc: CRTC in question
  1233.  *
  1234.  * Drivers can use this function to reset the vblank state to off at load time.
  1235.  * Drivers should use this together with the drm_crtc_vblank_off() and
  1236.  * drm_crtc_vblank_on() functions. The difference compared to
  1237.  * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
  1238.  * and hence doesn't need to call any driver hooks.
  1239.  */
  1240. void drm_crtc_vblank_reset(struct drm_crtc *crtc)
  1241. {
  1242.         struct drm_device *dev = crtc->dev;
  1243.         unsigned long irqflags;
  1244.         unsigned int pipe = drm_crtc_index(crtc);
  1245.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1246.  
  1247.         spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1248.         /*
  1249.          * Prevent subsequent drm_vblank_get() from enabling the vblank
  1250.          * interrupt by bumping the refcount.
  1251.          */
  1252.         if (!vblank->inmodeset) {
  1253.                 atomic_inc(&vblank->refcount);
  1254.                 vblank->inmodeset = 1;
  1255.         }
  1256.         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1257.  
  1258.         WARN_ON(!list_empty(&dev->vblank_event_list));
  1259. }
  1260. EXPORT_SYMBOL(drm_crtc_vblank_reset);
  1261.  
  1262. /**
  1263.  * drm_vblank_on - enable vblank events on a CRTC
  1264.  * @dev: DRM device
  1265.  * @pipe: CRTC index
  1266.  *
  1267.  * This functions restores the vblank interrupt state captured with
  1268.  * drm_vblank_off() again. Note that calls to drm_vblank_on() and
  1269.  * drm_vblank_off() can be unbalanced and so can also be unconditionally called
  1270.  * in driver load code to reflect the current hardware state of the crtc.
  1271.  *
  1272.  * This is the legacy version of drm_crtc_vblank_on().
  1273.  */
  1274. void drm_vblank_on(struct drm_device *dev, unsigned int pipe)
  1275. {
  1276.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1277.         unsigned long irqflags;
  1278.  
  1279.         if (WARN_ON(pipe >= dev->num_crtcs))
  1280.                 return;
  1281.  
  1282.         spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1283.         /* Drop our private "prevent drm_vblank_get" refcount */
  1284.         if (vblank->inmodeset) {
  1285.                 atomic_dec(&vblank->refcount);
  1286.                 vblank->inmodeset = 0;
  1287.         }
  1288.  
  1289.         drm_reset_vblank_timestamp(dev, pipe);
  1290.  
  1291.         /*
  1292.          * re-enable interrupts if there are users left, or the
  1293.          * user wishes vblank interrupts to be enabled all the time.
  1294.          */
  1295.         if (atomic_read(&vblank->refcount) != 0 ||
  1296.             (!dev->vblank_disable_immediate && drm_vblank_offdelay == 0))
  1297.                 WARN_ON(drm_vblank_enable(dev, pipe));
  1298.         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1299. }
  1300. EXPORT_SYMBOL(drm_vblank_on);
  1301.  
  1302. /**
  1303.  * drm_crtc_vblank_on - enable vblank events on a CRTC
  1304.  * @crtc: CRTC in question
  1305.  *
  1306.  * This functions restores the vblank interrupt state captured with
  1307.  * drm_vblank_off() again. Note that calls to drm_vblank_on() and
  1308.  * drm_vblank_off() can be unbalanced and so can also be unconditionally called
  1309.  * in driver load code to reflect the current hardware state of the crtc.
  1310.  *
  1311.  * This is the native kms version of drm_vblank_on().
  1312.  */
  1313. void drm_crtc_vblank_on(struct drm_crtc *crtc)
  1314. {
  1315.         drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
  1316. }
  1317. EXPORT_SYMBOL(drm_crtc_vblank_on);
  1318.  
  1319. /**
  1320.  * drm_vblank_pre_modeset - account for vblanks across mode sets
  1321.  * @dev: DRM device
  1322.  * @pipe: CRTC index
  1323.  *
  1324.  * Account for vblank events across mode setting events, which will likely
  1325.  * reset the hardware frame counter.
  1326.  *
  1327.  * This is done by grabbing a temporary vblank reference to ensure that the
  1328.  * vblank interrupt keeps running across the modeset sequence. With this the
  1329.  * software-side vblank frame counting will ensure that there are no jumps or
  1330.  * discontinuities.
  1331.  *
  1332.  * Unfortunately this approach is racy and also doesn't work when the vblank
  1333.  * interrupt stops running, e.g. across system suspend resume. It is therefore
  1334.  * highly recommended that drivers use the newer drm_vblank_off() and
  1335.  * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
  1336.  * using "cooked" software vblank frame counters and not relying on any hardware
  1337.  * counters.
  1338.  *
  1339.  * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
  1340.  * again.
  1341.  */
  1342. void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe)
  1343. {
  1344.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1345.  
  1346.         /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1347.         if (!dev->num_crtcs)
  1348.                 return;
  1349.  
  1350.         if (WARN_ON(pipe >= dev->num_crtcs))
  1351.                 return;
  1352.  
  1353.         /*
  1354.          * To avoid all the problems that might happen if interrupts
  1355.          * were enabled/disabled around or between these calls, we just
  1356.          * have the kernel take a reference on the CRTC (just once though
  1357.          * to avoid corrupting the count if multiple, mismatch calls occur),
  1358.          * so that interrupts remain enabled in the interim.
  1359.          */
  1360.         if (!vblank->inmodeset) {
  1361.                 vblank->inmodeset = 0x1;
  1362.                 if (drm_vblank_get(dev, pipe) == 0)
  1363.                         vblank->inmodeset |= 0x2;
  1364.         }
  1365. }
  1366. EXPORT_SYMBOL(drm_vblank_pre_modeset);
  1367.  
  1368. /**
  1369.  * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
  1370.  * @dev: DRM device
  1371.  * @pipe: CRTC index
  1372.  *
  1373.  * This function again drops the temporary vblank reference acquired in
  1374.  * drm_vblank_pre_modeset.
  1375.  */
  1376. void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe)
  1377. {
  1378.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1379.         unsigned long irqflags;
  1380.  
  1381.         /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1382.         if (!dev->num_crtcs)
  1383.                 return;
  1384.  
  1385.         if (WARN_ON(pipe >= dev->num_crtcs))
  1386.                 return;
  1387.  
  1388.         if (vblank->inmodeset) {
  1389.                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1390.                 dev->vblank_disable_allowed = true;
  1391.                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1392.  
  1393.                 if (vblank->inmodeset & 0x2)
  1394.                         drm_vblank_put(dev, pipe);
  1395.  
  1396.                 vblank->inmodeset = 0;
  1397.         }
  1398. }
  1399. EXPORT_SYMBOL(drm_vblank_post_modeset);
  1400.  
  1401. static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
  1402. {
  1403.         struct drm_pending_vblank_event *e, *t;
  1404.         struct timeval now;
  1405.         unsigned int seq;
  1406.  
  1407.         assert_spin_locked(&dev->event_lock);
  1408.  
  1409.         seq = drm_vblank_count_and_time(dev, pipe, &now);
  1410.  
  1411.         list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1412.                 if (e->pipe != pipe)
  1413.                         continue;
  1414.                 if ((seq - e->event.sequence) > (1<<23))
  1415.                         continue;
  1416.  
  1417.                 DRM_DEBUG("vblank event on %d, current %d\n",
  1418.                           e->event.sequence, seq);
  1419.  
  1420.                 list_del(&e->base.link);
  1421.                 drm_vblank_put(dev, pipe);
  1422. //       send_vblank_event(dev, e, seq, &now);
  1423.         }
  1424.  
  1425. }
  1426.  
  1427. /**
  1428.  * drm_handle_vblank - handle a vblank event
  1429.  * @dev: DRM device
  1430.  * @pipe: index of CRTC where this event occurred
  1431.  *
  1432.  * Drivers should call this routine in their vblank interrupt handlers to
  1433.  * update the vblank counter and send any signals that may be pending.
  1434.  *
  1435.  * This is the legacy version of drm_crtc_handle_vblank().
  1436.  */
  1437. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
  1438. {
  1439.         struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1440.         unsigned long irqflags;
  1441.  
  1442.         if (WARN_ON_ONCE(!dev->num_crtcs))
  1443.                 return false;
  1444.  
  1445.         if (WARN_ON(pipe >= dev->num_crtcs))
  1446.                 return false;
  1447.  
  1448.         spin_lock_irqsave(&dev->event_lock, irqflags);
  1449.  
  1450.         /* Need timestamp lock to prevent concurrent execution with
  1451.          * vblank enable/disable, as this would cause inconsistent
  1452.          * or corrupted timestamps and vblank counts.
  1453.          */
  1454.         spin_lock(&dev->vblank_time_lock);
  1455.  
  1456.         /* Vblank irq handling disabled. Nothing to do. */
  1457.         if (!vblank->enabled) {
  1458.                 spin_unlock(&dev->vblank_time_lock);
  1459.                 spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1460.                 return false;
  1461.         }
  1462.  
  1463.         drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
  1464.  
  1465.         spin_unlock(&dev->vblank_time_lock);
  1466.  
  1467.         wake_up(&vblank->queue);
  1468.         drm_handle_vblank_events(dev, pipe);
  1469.  
  1470.         spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1471.  
  1472.         return true;
  1473. }
  1474. EXPORT_SYMBOL(drm_handle_vblank);
  1475.  
  1476. /**
  1477.  * drm_crtc_handle_vblank - handle a vblank event
  1478.  * @crtc: where this event occurred
  1479.  *
  1480.  * Drivers should call this routine in their vblank interrupt handlers to
  1481.  * update the vblank counter and send any signals that may be pending.
  1482.  *
  1483.  * This is the native KMS version of drm_handle_vblank().
  1484.  *
  1485.  * Returns:
  1486.  * True if the event was successfully handled, false on failure.
  1487.  */
  1488. bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
  1489. {
  1490.         return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
  1491. }
  1492. EXPORT_SYMBOL(drm_crtc_handle_vblank);
  1493.  
  1494. /**
  1495.  * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
  1496.  * @dev: DRM device
  1497.  * @pipe: CRTC for which to read the counter
  1498.  *
  1499.  * Drivers can plug this into the .get_vblank_counter() function if
  1500.  * there is no useable hardware frame counter available.
  1501.  *
  1502.  * Returns:
  1503.  * 0
  1504.  */
  1505. u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
  1506. {
  1507.         return 0;
  1508. }
  1509. EXPORT_SYMBOL(drm_vblank_no_hw_counter);
  1510.  
  1511. u64 div64_u64(u64 dividend, u64 divisor)
  1512. {
  1513.         u32 high, d;
  1514.  
  1515.         high = divisor >> 32;
  1516.         if (high) {
  1517.                 unsigned int shift = fls(high);
  1518.  
  1519.                 d = divisor >> shift;
  1520.                 dividend >>= shift;
  1521.         } else
  1522.                 d = divisor;
  1523.  
  1524.         return div_u64(dividend, d);
  1525. }
  1526.