Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file drm_irq.c
  3.  * IRQ support
  4.  *
  5.  * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6.  * \author Gareth Hughes <gareth@valinux.com>
  7.  */
  8.  
  9. /*
  10.  * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
  11.  *
  12.  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  13.  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  14.  * All Rights Reserved.
  15.  *
  16.  * Permission is hereby granted, free of charge, to any person obtaining a
  17.  * copy of this software and associated documentation files (the "Software"),
  18.  * to deal in the Software without restriction, including without limitation
  19.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  20.  * and/or sell copies of the Software, and to permit persons to whom the
  21.  * Software is furnished to do so, subject to the following conditions:
  22.  *
  23.  * The above copyright notice and this permission notice (including the next
  24.  * paragraph) shall be included in all copies or substantial portions of the
  25.  * Software.
  26.  *
  27.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  30.  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  31.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  32.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  33.  * OTHER DEALINGS IN THE SOFTWARE.
  34.  */
  35.  
  36. #include <drm/drmP.h>
  37. #include <asm/div64.h>
  38. //#include "drm_trace.h"
  39.  
  40. //#include <linux/interrupt.h>   /* For task queue support */
  41. #include <linux/slab.h>
  42.  
  43. //#include <linux/vgaarb.h>
  44. #include <linux/export.h>
  45.  
  46. /* Access macro for slots in vblank timestamp ringbuffer. */
  47. #define vblanktimestamp(dev, crtc, count) ( \
  48.         (dev)->_vblank_time[(crtc) * DRM_VBLANKTIME_RBSIZE + \
  49.         ((count) % DRM_VBLANKTIME_RBSIZE)])
  50.  
  51. /* Retry timestamp calculation up to 3 times to satisfy
  52.  * drm_timestamp_precision before giving up.
  53.  */
  54. #define DRM_TIMESTAMP_MAXRETRIES 3
  55.  
  56. /* Threshold in nanoseconds for detection of redundant
  57.  * vblank irq in drm_handle_vblank(). 1 msec should be ok.
  58.  */
  59. #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
  60.  
  61.  
  62. irqreturn_t device_irq_handler(struct drm_device *dev)
  63. {
  64.  
  65. //    printf("video irq\n");
  66.  
  67. //    printf("device %p driver %p handler %p\n", dev, dev->driver, dev->driver->irq_handler) ;
  68.  
  69.     return dev->driver->irq_handler(0, dev);
  70. }
  71.  
  72. /**
  73.  * Install IRQ handler.
  74.  *
  75.  * \param dev DRM device.
  76.  *
  77.  * Initializes the IRQ related data. Installs the handler, calling the driver
  78.  * \c irq_preinstall() and \c irq_postinstall() functions
  79.  * before and after the installation.
  80.  */
  81. int drm_irq_install(struct drm_device *dev)
  82. {
  83.         int ret;
  84.     unsigned long sh_flags = 0;
  85.         char *irqname;
  86.  
  87.  
  88.         if (drm_dev_to_irq(dev) == 0)
  89.                 return -EINVAL;
  90.  
  91.     mutex_lock(&dev->struct_mutex);
  92.  
  93.     /* Driver must have been initialized */
  94.     if (!dev->dev_private) {
  95.             mutex_unlock(&dev->struct_mutex);
  96.             return -EINVAL;
  97.     }
  98.  
  99.     if (dev->irq_enabled) {
  100.             mutex_unlock(&dev->struct_mutex);
  101.             return -EBUSY;
  102.     }
  103.     dev->irq_enabled = 1;
  104.     mutex_unlock(&dev->struct_mutex);
  105.  
  106.     DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
  107.  
  108.     /* Before installing handler */
  109.     if (dev->driver->irq_preinstall)
  110.             dev->driver->irq_preinstall(dev);
  111.  
  112.     ret = !AttachIntHandler(drm_dev_to_irq(dev), device_irq_handler, (u32)dev);
  113.  
  114.     /* After installing handler */
  115.     if (dev->driver->irq_postinstall)
  116.             ret = dev->driver->irq_postinstall(dev);
  117.  
  118.     if (ret < 0) {
  119.                 dev->irq_enabled = 0;
  120.             DRM_ERROR(__FUNCTION__);
  121.     }
  122.  
  123.     u16_t cmd = PciRead16(dev->pdev->busnr, dev->pdev->devfn, 4);
  124.     cmd&= ~(1<<10);
  125.     PciWrite16(dev->pdev->busnr, dev->pdev->devfn, 4, cmd);
  126.  
  127.     return ret;
  128. }
  129. EXPORT_SYMBOL(drm_irq_install);
  130.  
  131.  
  132.  
  133.  
  134. u64 div64_u64(u64 dividend, u64 divisor)
  135. {
  136.         u32 high, d;
  137.  
  138.         high = divisor >> 32;
  139.         if (high) {
  140.                 unsigned int shift = fls(high);
  141.  
  142.                 d = divisor >> shift;
  143.                 dividend >>= shift;
  144.         } else
  145.                 d = divisor;
  146.  
  147.         return div_u64(dividend, d);
  148. }
  149.  
  150. /**
  151.  * drm_calc_timestamping_constants - Calculate and
  152.  * store various constants which are later needed by
  153.  * vblank and swap-completion timestamping, e.g, by
  154.  * drm_calc_vbltimestamp_from_scanoutpos().
  155.  * They are derived from crtc's true scanout timing,
  156.  * so they take things like panel scaling or other
  157.  * adjustments into account.
  158.  *
  159.  * @crtc drm_crtc whose timestamp constants should be updated.
  160.  *
  161.  */
  162. void drm_calc_timestamping_constants(struct drm_crtc *crtc)
  163. {
  164.         s64 linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
  165.         u64 dotclock;
  166.  
  167.         /* Dot clock in Hz: */
  168.         dotclock = (u64) crtc->hwmode.clock * 1000;
  169.  
  170.         /* Fields of interlaced scanout modes are only halve a frame duration.
  171.          * Double the dotclock to get halve the frame-/line-/pixelduration.
  172.          */
  173.         if (crtc->hwmode.flags & DRM_MODE_FLAG_INTERLACE)
  174.                 dotclock *= 2;
  175.  
  176.         /* Valid dotclock? */
  177.         if (dotclock > 0) {
  178.                 int frame_size;
  179.                 /* Convert scanline length in pixels and video dot clock to
  180.                  * line duration, frame duration and pixel duration in
  181.                  * nanoseconds:
  182.                  */
  183.                 pixeldur_ns = (s64) div64_u64(1000000000, dotclock);
  184.                 linedur_ns  = (s64) div64_u64(((u64) crtc->hwmode.crtc_htotal *
  185.                                               1000000000), dotclock);
  186.                 frame_size = crtc->hwmode.crtc_htotal *
  187.                                 crtc->hwmode.crtc_vtotal;
  188.                 framedur_ns = (s64) div64_u64((u64) frame_size * 1000000000,
  189.                                               dotclock);
  190.         } else
  191.                 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
  192.                           crtc->base.id);
  193.  
  194.         crtc->pixeldur_ns = pixeldur_ns;
  195.         crtc->linedur_ns  = linedur_ns;
  196.         crtc->framedur_ns = framedur_ns;
  197.  
  198.         DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  199.                   crtc->base.id, crtc->hwmode.crtc_htotal,
  200.                   crtc->hwmode.crtc_vtotal, crtc->hwmode.crtc_vdisplay);
  201.         DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
  202.                   crtc->base.id, (int) dotclock/1000, (int) framedur_ns,
  203.                   (int) linedur_ns, (int) pixeldur_ns);
  204. }
  205.  
  206.  
  207. /**
  208.  * drm_vblank_pre_modeset - account for vblanks across mode sets
  209.  * @dev: DRM device
  210.  * @crtc: CRTC in question
  211.  *
  212.  * Account for vblank events across mode setting events, which will likely
  213.  * reset the hardware frame counter.
  214.  */
  215. void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
  216. {
  217. #if 0
  218.     /* vblank is not initialized (IRQ not installed ?) */
  219.     if (!dev->num_crtcs)
  220.         return;
  221.     /*
  222.      * To avoid all the problems that might happen if interrupts
  223.      * were enabled/disabled around or between these calls, we just
  224.      * have the kernel take a reference on the CRTC (just once though
  225.      * to avoid corrupting the count if multiple, mismatch calls occur),
  226.      * so that interrupts remain enabled in the interim.
  227.      */
  228.     if (!dev->vblank_inmodeset[crtc]) {
  229.         dev->vblank_inmodeset[crtc] = 0x1;
  230.         if (drm_vblank_get(dev, crtc) == 0)
  231.             dev->vblank_inmodeset[crtc] |= 0x2;
  232.     }
  233. #endif
  234. }
  235. EXPORT_SYMBOL(drm_vblank_pre_modeset);
  236.  
  237. void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
  238. {
  239. #if 0
  240.     unsigned long irqflags;
  241.  
  242.         /* vblank is not initialized (IRQ not installed ?), or has been freed */
  243.         if (!dev->num_crtcs)
  244.                 return;
  245.  
  246.     if (dev->vblank_inmodeset[crtc]) {
  247.         spin_lock_irqsave(&dev->vbl_lock, irqflags);
  248.         dev->vblank_disable_allowed = 1;
  249.         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  250.  
  251.         if (dev->vblank_inmodeset[crtc] & 0x2)
  252.             drm_vblank_put(dev, crtc);
  253.  
  254.         dev->vblank_inmodeset[crtc] = 0;
  255.     }
  256. #endif
  257. }
  258. EXPORT_SYMBOL(drm_vblank_post_modeset);
  259.