Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * drm_irq.c IRQ and vblank support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * \author Rickard E. (Rik) Faith <faith@valinux.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * \author Gareth Hughes <gareth@valinux.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Permission is hereby granted, free of charge, to any person obtaining a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * copy of this software and associated documentation files (the "Software"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * to deal in the Software without restriction, including without limitation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * and/or sell copies of the Software, and to permit persons to whom the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * Software is furnished to do so, subject to the following conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * The above copyright notice and this permission notice (including the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * paragraph) shall be included in all copies or substantial portions of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * Software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * OTHER DEALINGS IN THE SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <drm/drm_crtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <drm/drm_drv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <drm/drm_framebuffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <drm/drm_managed.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <drm/drm_modeset_helper_vtables.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <drm/drm_print.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <drm/drm_vblank.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include "drm_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include "drm_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * DOC: vblank handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * From the computer's perspective, every time the monitor displays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * a new frame the scanout engine has "scanned out" the display image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * from top to bottom, one row of pixels at a time. The current row
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * of pixels is referred to as the current scanline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * In addition to the display's visible area, there's usually a couple of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * extra scanlines which aren't actually displayed on the screen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * These extra scanlines don't contain image data and are occasionally used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * for features like audio and infoframes. The region made up of these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * scanlines is referred to as the vertical blanking region, or vblank for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * short.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * For historical reference, the vertical blanking period was designed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * give the electron gun (on CRTs) enough time to move back to the top of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * the screen to start scanning out the next frame. Similar for horizontal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * blanking periods. They were designed to give the electron gun enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * time to move back to the other side of the screen to start scanning the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * next scanline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  *    physical →   ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  *    top of      |                                        |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  *    display     |                                        |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  *                |               New frame                |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  *                |                                        |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  *                |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  *                |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| ← Scanline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  *                |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓|   updates the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  *                |                                        |   frame as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  *                |                                        |   travels down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  *                |                                        |   ("sacn out")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  *                |               Old frame                |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  *                |                                        |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  *                |                                        |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  *                |                                        |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  *                |                                        |   physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  *                |                                        |   bottom of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  *    vertical    |⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽| ← display
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  *    blanking    ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  *    region   →  ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  *                ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  *    start of →   ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  *    new frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * "Physical top of display" is the reference point for the high-precision/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * corrected timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * On a lot of display hardware, programming needs to take effect during the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * vertical blanking period so that settings like gamma, the image buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  * buffer to be scanned out, etc. can safely be changed without showing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * any visual artifacts on the screen. In some unforgiving hardware, some of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * this programming has to both start and end in the same vblank. To help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * with the timing of the hardware programming, an interrupt is usually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * available to notify the driver when it can start the updating of registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * The interrupt is in this context named the vblank interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * The vblank interrupt may be fired at different points depending on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * hardware. Some hardware implementations will fire the interrupt when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * new frame start, other implementations will fire the interrupt at different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * points in time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * Vertical blanking plays a major role in graphics rendering. To achieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  * tear-free display, users must synchronize page flips and/or rendering to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * vertical blanking. The DRM API offers ioctls to perform page flips
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  * synchronized to vertical blanking and wait for vertical blanking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  * The DRM core handles most of the vertical blanking management logic, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * involves filtering out spurious interrupts, keeping race-free blanking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * counters, coping with counter wrap-around and resets and keeping use counts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * It relies on the driver to generate vertical blanking interrupts and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * optionally provide a hardware vertical blanking counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  * Drivers must initialize the vertical blanking handling core with a call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * drm_vblank_init(). Minimally, a driver needs to implement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  * drm_crtc_handle_vblank() in its vblank interrupt handler for working vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  * support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  * Vertical blanking interrupts can be enabled by the DRM core or by drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  * themselves (for instance to handle page flipping operations).  The DRM core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  * maintains a vertical blanking use count to ensure that the interrupts are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128)  * disabled while a user still needs them. To increment the use count, drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  * call drm_crtc_vblank_get() and release the vblank reference again with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  * guaranteed to be enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133)  * On many hardware disabling the vblank interrupt cannot be done in a race-free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  * manner, see &drm_driver.vblank_disable_immediate and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  * &drm_driver.max_vblank_count. In that case the vblank core only disables the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  * vblanks after a timer has expired, which can be configured through the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  * ``vblankoffdelay`` module parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  * Drivers for hardware without support for vertical-blanking interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  * must not call drm_vblank_init(). For such drivers, atomic helpers will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  * automatically generate fake vblank events as part of the display update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  * This functionality also can be controlled by the driver by enabling and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143)  * disabling struct drm_crtc_state.no_vblank.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) /* Retry timestamp calculation up to 3 times to satisfy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * drm_timestamp_precision before giving up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) #define DRM_TIMESTAMP_MAXRETRIES 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) /* Threshold in nanoseconds for detection of redundant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * vblank irq in drm_handle_vblank(). 1 msec should be ok.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 			  ktime_t *tvblank, bool in_vblank_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) static unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) static int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) static void store_vblank(struct drm_device *dev, unsigned int pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			 u32 vblank_count_inc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 			 ktime_t t_vblank, u32 last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	assert_spin_locked(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	vblank->last = last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	write_seqlock(&vblank->seqlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	vblank->time = t_vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	atomic64_add(vblank_count_inc, &vblank->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	write_sequnlock(&vblank->seqlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) static u32 drm_max_vblank_count(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	return vblank->max_vblank_count ?: dev->max_vblank_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  * "No hw counter" fallback implementation of .get_vblank_counter() hook,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  * if there is no useable hardware frame counter available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	drm_WARN_ON_ONCE(dev, drm_max_vblank_count(dev, pipe) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		if (drm_WARN_ON(dev, !crtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		if (crtc->funcs->get_vblank_counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			return crtc->funcs->get_vblank_counter(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	} else if (dev->driver->get_vblank_counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		return dev->driver->get_vblank_counter(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	return drm_vblank_no_hw_counter(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220)  * Reset the stored timestamp for the current vblank count to correspond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221)  * to the last vblank occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223)  * Only to be called from drm_crtc_vblank_on().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225)  * Note: caller must hold &drm_device.vbl_lock since this reads & writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226)  * device vblank fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	u32 cur_vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	bool rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	ktime_t t_vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	int count = DRM_TIMESTAMP_MAXRETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	spin_lock(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	 * sample the current counter to avoid random jumps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	 * when drm_vblank_enable() applies the diff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		cur_vblank = __get_vblank_counter(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	} while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	 * Only reinitialize corresponding vblank timestamp if high-precision query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	 * available and didn't fail. Otherwise reinitialize delayed at next vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		t_vblank = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	 * +1 to make sure user will never see the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	 * vblank counter value before and after a modeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	store_vblank(dev, pipe, 1, t_vblank, cur_vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	spin_unlock(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264)  * Call back into the driver to update the appropriate vblank counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265)  * (specified by @pipe).  Deal with wraparound, if it occurred, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  * update the last read value so we can deal with wraparound on the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267)  * call if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269)  * Only necessary when going from off->on, to account for frames we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270)  * didn't get an interrupt for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272)  * Note: caller must hold &drm_device.vbl_lock since this reads & writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273)  * device vblank fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 				    bool in_vblank_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	u32 cur_vblank, diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	bool rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	ktime_t t_vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	int count = DRM_TIMESTAMP_MAXRETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	int framedur_ns = vblank->framedur_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	u32 max_vblank_count = drm_max_vblank_count(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	 * Interrupts were disabled prior to this call, so deal with counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	 * wrap if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	 * NOTE!  It's possible we lost a full dev->max_vblank_count + 1 events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	 * here if the register is small or we had vblank interrupts off for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	 * a long time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	 * We repeat the hardware vblank counter & timestamp query until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	 * we get consistent results. This to prevent races between gpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	 * updating its hardware counter while we are retrieving the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	 * corresponding vblank timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		cur_vblank = __get_vblank_counter(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	} while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	if (max_vblank_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		/* trust the hw counter when it's around */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		diff = (cur_vblank - vblank->last) & max_vblank_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	} else if (rc && framedur_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		 * Figure out how many vblanks we've missed based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		 * on the difference in the timestamps and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		 * frame/field duration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		drm_dbg_vbl(dev, "crtc %u: Calculating number of vblanks."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			    " diff_ns = %lld, framedur_ns = %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 			    pipe, (long long)diff_ns, framedur_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		if (diff == 0 && in_vblank_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			drm_dbg_vbl(dev, "crtc %u: Redundant vblirq ignored\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 				    pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		/* some kind of default for drivers w/o accurate vbl timestamping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		diff = in_vblank_irq ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	 * interval? If so then vblank irqs keep running and it will likely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	 * happen that the hardware vblank counter is not trustworthy as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	 * might reset at some point in that interval and vblank timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	 * are not trustworthy either in that interval. Iow. this can result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	 * in a bogus diff >> 1 which must be avoided as it would cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	 * random large forward jumps of the software vblank counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	if (diff > 1 && (vblank->inmodeset & 0x2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		drm_dbg_vbl(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 			    "clamping vblank bump to 1 on crtc %u: diffr=%u"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 			    " due to pre-modeset.\n", pipe, diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		diff = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	drm_dbg_vbl(dev, "updating vblank count on crtc %u:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		    " current=%llu, diff=%u, hw=%u hw_last=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		    pipe, (unsigned long long)atomic64_read(&vblank->count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		    diff, cur_vblank, vblank->last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	if (diff == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		drm_WARN_ON_ONCE(dev, cur_vblank != vblank->last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	 * Only reinitialize corresponding vblank timestamp if high-precision query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	 * available and didn't fail, or we were called from the vblank interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	 * for now, to mark the vblanktimestamp as invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	if (!rc && !in_vblank_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		t_vblank = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	u64 count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	count = atomic64_read(&vblank->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	 * This read barrier corresponds to the implicit write barrier of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	 * write seqlock in store_vblank(). Note that this is the only place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	 * where we need an explicit barrier, since all other access goes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	 * through drm_vblank_count_and_time(), which already has the required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	 * read barrier curtesy of the read seqlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390)  * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391)  * @crtc: which counter to retrieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393)  * This function is similar to drm_crtc_vblank_count() but this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394)  * interpolates to handle a race with vblank interrupts using the high precision
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  * timestamping support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  * This is mostly useful for hardware that can obtain the scanout position, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * doesn't have a hardware frame counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	struct drm_device *dev = crtc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	unsigned int pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	u64 vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	drm_WARN_ONCE(dev, drm_debug_enabled(DRM_UT_VBL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		      !crtc->funcs->get_vblank_timestamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		      "This function requires support for accurate vblank timestamps.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	spin_lock_irqsave(&dev->vblank_time_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	drm_update_vblank_count(dev, pipe, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	vblank = drm_vblank_count(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	return vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) EXPORT_SYMBOL(drm_crtc_accurate_vblank_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		if (drm_WARN_ON(dev, !crtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		if (crtc->funcs->disable_vblank)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 			crtc->funcs->disable_vblank(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		dev->driver->disable_vblank(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  * Disable vblank irq's on crtc, make sure that last vblank count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439)  * of hardware and corresponding consistent software vblank counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440)  * are preserved, even if there are any spurious vblank irq's after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441)  * disable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	unsigned long irqflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	assert_spin_locked(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	/* Prevent vblank irq processing while disabling vblank irqs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	 * so no updates of timestamps or count can happen after we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	 * disabled. Needed to prevent races in case of delayed irq's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	 * Update vblank count and disable vblank interrupts only if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	 * interrupts were enabled. This avoids calling the ->disable_vblank()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	 * operation in atomic context with the hardware potentially runtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	 * suspended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	if (!vblank->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	 * Update the count and timestamp to maintain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	 * appearance that the counter has been ticking all along until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	 * this time. This makes the count account for the entire time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	 * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	drm_update_vblank_count(dev, pipe, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	__disable_vblank(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	vblank->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) static void vblank_disable_fn(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	struct drm_vblank_crtc *vblank = from_timer(vblank, t, disable_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	struct drm_device *dev = vblank->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	unsigned int pipe = vblank->pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	unsigned long irqflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		drm_dbg_core(dev, "disabling vblank on crtc %u\n", pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		drm_vblank_disable_and_save(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) static void drm_vblank_init_release(struct drm_device *dev, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	struct drm_vblank_crtc *vblank = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	drm_WARN_ON(dev, READ_ONCE(vblank->enabled) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		    drm_core_check_feature(dev, DRIVER_MODESET));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	drm_vblank_destroy_worker(vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	del_timer_sync(&vblank->disable_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506)  * drm_vblank_init - initialize vblank support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507)  * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508)  * @num_crtcs: number of CRTCs supported by @dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510)  * This function initializes vblank support for @num_crtcs display pipelines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511)  * Cleanup is handled automatically through a cleanup function added with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512)  * drmm_add_action_or_reset().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515)  * Zero on success or a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	spin_lock_init(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	spin_lock_init(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	dev->vblank = drmm_kcalloc(dev, num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if (!dev->vblank)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	dev->num_crtcs = num_crtcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	for (i = 0; i < num_crtcs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		struct drm_vblank_crtc *vblank = &dev->vblank[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		vblank->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		vblank->pipe = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		init_waitqueue_head(&vblank->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		seqlock_init(&vblank->seqlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		ret = drmm_add_action_or_reset(dev, drm_vblank_init_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 					       vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		ret = drm_vblank_worker_init(vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) EXPORT_SYMBOL(drm_vblank_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555)  * drm_dev_has_vblank - test if vblanking has been initialized for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556)  *                      a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557)  * @dev: the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559)  * Drivers may call this function to test if vblank support is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560)  * initialized for a device. For most hardware this means that vblanking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561)  * can also be enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563)  * Atomic helpers use this function to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564)  * &drm_crtc_state.no_vblank. See also drm_atomic_helper_check_modeset().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567)  * True if vblanking has been initialized for the given device, false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568)  * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) bool drm_dev_has_vblank(const struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	return dev->num_crtcs != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) EXPORT_SYMBOL(drm_dev_has_vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577)  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  * @crtc: which CRTC's vblank waitqueue to retrieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580)  * This function returns a pointer to the vblank waitqueue for the CRTC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581)  * Drivers can use this to implement vblank waits using wait_event() and related
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582)  * functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592)  * drm_calc_timestamping_constants - calculate vblank timestamp constants
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593)  * @crtc: drm_crtc whose timestamp constants should be updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)  * @mode: display mode containing the scanout timings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  * Calculate and store various constants which are later needed by vblank and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  * swap-completion timestamping, e.g, by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  * drm_crtc_vblank_helper_get_vblank_timestamp(). They are derived from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  * CRTC's true scanout timing, so they take things like panel scaling or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  * other adjustments into account.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) void drm_calc_timestamping_constants(struct drm_crtc *crtc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 				     const struct drm_display_mode *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	struct drm_device *dev = crtc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	unsigned int pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	int linedur_ns = 0, framedur_ns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	int dotclock = mode->crtc_clock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	if (!drm_dev_has_vblank(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	/* Valid dotclock? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (dotclock > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		 * Convert scanline length in pixels and video
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		 * dot clock to line duration and frame duration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		 * in nanoseconds:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		linedur_ns  = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		 * Fields of interlaced scanout modes are only half a frame duration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			framedur_ns /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		drm_err(dev, "crtc %u: Can't calculate constants, dotclock = 0!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			crtc->base.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	vblank->linedur_ns  = linedur_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	vblank->framedur_ns = framedur_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	vblank->hwmode = *mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	drm_dbg_core(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		     "crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		     crtc->base.id, mode->crtc_htotal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		     mode->crtc_vtotal, mode->crtc_vdisplay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	drm_dbg_core(dev, "crtc %u: clock %d kHz framedur %d linedur %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		     crtc->base.id, dotclock, framedur_ns, linedur_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) EXPORT_SYMBOL(drm_calc_timestamping_constants);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653)  * drm_crtc_vblank_helper_get_vblank_timestamp_internal - precise vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654)  *                                                        timestamp helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655)  * @crtc: CRTC whose vblank timestamp to retrieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656)  * @max_error: Desired maximum allowable error in timestamps (nanosecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657)  *             On return contains true maximum error of timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658)  * @vblank_time: Pointer to time which should receive the timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659)  * @in_vblank_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660)  *     True when called from drm_crtc_handle_vblank().  Some drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661)  *     need to apply some workarounds for gpu-specific vblank irq quirks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662)  *     if flag is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663)  * @get_scanout_position:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664)  *     Callback function to retrieve the scanout position. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  *     @struct drm_crtc_helper_funcs.get_scanout_position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * Implements calculation of exact vblank timestamps from given drm_display_mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  * timings and current video scanout position of a CRTC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670)  * The current implementation only handles standard video modes. For double scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671)  * and interlaced modes the driver is supposed to adjust the hardware mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672)  * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673)  * match the scanout position reported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675)  * Note that atomic drivers must call drm_calc_timestamping_constants() before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676)  * enabling a CRTC. The atomic helpers already take care of that in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677)  * drm_atomic_helper_calc_timestamping_constants().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681)  * Returns true on success, and false on failure, i.e. when no accurate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682)  * timestamp could be acquired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) drm_crtc_vblank_helper_get_vblank_timestamp_internal(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	struct drm_crtc *crtc, int *max_error, ktime_t *vblank_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	bool in_vblank_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	drm_vblank_get_scanout_position_func get_scanout_position)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	struct drm_device *dev = crtc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	unsigned int pipe = crtc->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	struct timespec64 ts_etime, ts_vblank_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	ktime_t stime, etime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	bool vbl_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	const struct drm_display_mode *mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	int vpos, hpos, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	int delta_ns, duration_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	if (pipe >= dev->num_crtcs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		drm_err(dev, "Invalid crtc %u\n", pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	/* Scanout position query not supported? Should not happen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	if (!get_scanout_position) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		drm_err(dev, "Called from CRTC w/o get_scanout_position()!?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	if (drm_drv_uses_atomic_modeset(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		mode = &vblank->hwmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		mode = &crtc->hwmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	/* If mode timing undefined, just return as no-op:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	 * Happens during initial modesetting of a crtc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	if (mode->crtc_clock == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		drm_dbg_core(dev, "crtc %u: Noop due to uninitialized mode.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 			     pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	/* Get current scanout position with system timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	 * if single query takes longer than max_error nanoseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	 * This guarantees a tight bound on maximum error if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	 * code gets preempted or delayed for some reason.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		 * Get vertical and horizontal scanout position vpos, hpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		 * and bounding timestamps stime, etime, pre/post query.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		vbl_status = get_scanout_position(crtc, in_vblank_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 						  &vpos, &hpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 						  &stime, &etime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 						  mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		/* Return as no-op if scanout query unsupported or failed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		if (!vbl_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 			drm_dbg_core(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 				     "crtc %u : scanoutpos query failed.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 				     pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		/* Compute uncertainty in timestamp of scanout position query. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		/* Accept result with <  max_error nsecs timing uncertainty. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		if (duration_ns <= *max_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	/* Noisy system timing? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	if (i == DRM_TIMESTAMP_MAXRETRIES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		drm_dbg_core(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			     "crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			     pipe, duration_ns / 1000, *max_error / 1000, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	/* Return upper bound of timestamp precision error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	*max_error = duration_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	/* Convert scanout position into elapsed time at raw_time query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	 * since start of scanout at first display scanline. delta_ns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	 * can be negative if start of scanout hasn't happened yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			   mode->crtc_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	/* Subtract time delta from raw timestamp to get final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	 * vblank_time timestamp for end of vblank.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	*vblank_time = ktime_sub_ns(etime, delta_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if (!drm_debug_enabled(DRM_UT_VBL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	ts_etime = ktime_to_timespec64(etime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	ts_vblank_time = ktime_to_timespec64(*vblank_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	drm_dbg_vbl(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		    "crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		    pipe, hpos, vpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		    (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		    (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		    duration_ns / 1000, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp_internal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799)  * drm_crtc_vblank_helper_get_vblank_timestamp - precise vblank timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800)  *                                               helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801)  * @crtc: CRTC whose vblank timestamp to retrieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802)  * @max_error: Desired maximum allowable error in timestamps (nanosecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803)  *             On return contains true maximum error of timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  * @vblank_time: Pointer to time which should receive the timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  * @in_vblank_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806)  *     True when called from drm_crtc_handle_vblank().  Some drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807)  *     need to apply some workarounds for gpu-specific vblank irq quirks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808)  *     if flag is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810)  * Implements calculation of exact vblank timestamps from given drm_display_mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811)  * timings and current video scanout position of a CRTC. This can be directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)  * used as the &drm_crtc_funcs.get_vblank_timestamp implementation of a kms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813)  * driver if &drm_crtc_helper_funcs.get_scanout_position is implemented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815)  * The current implementation only handles standard video modes. For double scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816)  * and interlaced modes the driver is supposed to adjust the hardware mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818)  * match the scanout position reported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  * Note that atomic drivers must call drm_calc_timestamping_constants() before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  * enabling a CRTC. The atomic helpers already take care of that in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  * drm_atomic_helper_calc_timestamping_constants().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826)  * Returns true on success, and false on failure, i.e. when no accurate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827)  * timestamp could be acquired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 						 int *max_error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 						 ktime_t *vblank_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 						 bool in_vblank_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		crtc, max_error, vblank_time, in_vblank_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		crtc->helper_private->get_scanout_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841)  * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842)  *                             vblank interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843)  * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844)  * @pipe: index of CRTC whose vblank timestamp to retrieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  * @tvblank: Pointer to target time which should receive the timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  * @in_vblank_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847)  *     True when called from drm_crtc_handle_vblank().  Some drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  *     need to apply some workarounds for gpu-specific vblank irq quirks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  *     if flag is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851)  * Fetches the system timestamp corresponding to the time of the most recent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852)  * vblank interval on specified CRTC. May call into kms-driver to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853)  * compute the timestamp with a high-precision GPU specific method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855)  * Returns zero if timestamp originates from uncorrected do_gettimeofday()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856)  * call, i.e., it isn't very precisely locked to the true vblank.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859)  * True if timestamp is considered to be very precise, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			  ktime_t *tvblank, bool in_vblank_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	/* Define requested maximum error on timestamps (nanoseconds). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	int max_error = (int) drm_timestamp_precision * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	/* Query driver if possible and precision timestamping enabled. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (crtc && crtc->funcs->get_vblank_timestamp && max_error > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		ret = crtc->funcs->get_vblank_timestamp(crtc, &max_error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 							tvblank, in_vblank_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	/* GPU high precision timestamp query unsupported or failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	 * Return current monotonic/gettimeofday timestamp as best estimate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		*tvblank = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889)  * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890)  * @crtc: which counter to retrieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892)  * Fetches the "cooked" vblank count value that represents the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893)  * vblank events since the system was booted, including lost events due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894)  * modesetting activity. Note that this timer isn't correct against a racing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895)  * vblank interrupt (since it only reports the software vblank counter), see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896)  * drm_crtc_accurate_vblank_count() for such use-cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898)  * Note that for a given vblank counter value drm_crtc_handle_vblank()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899)  * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900)  * provide a barrier: Any writes done before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901)  * drm_crtc_handle_vblank() will be visible to callers of the later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  * functions, iff the vblank count is the same or a later one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  * See also &drm_vblank_crtc.count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907)  * The software vblank counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) EXPORT_SYMBOL(drm_crtc_vblank_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916)  * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917)  *     system timestamp corresponding to that vblank counter value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918)  * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919)  * @pipe: index of CRTC whose counter to retrieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920)  * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922)  * Fetches the "cooked" vblank count value that represents the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923)  * vblank events since the system was booted, including lost events due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924)  * modesetting activity. Returns corresponding system timestamp of the time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925)  * of the vblank interval that corresponds to the current vblank counter value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927)  * This is the legacy version of drm_crtc_vblank_count_and_time().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 				     ktime_t *vblanktime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	u64 vblank_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	unsigned int seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		*vblanktime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		seq = read_seqbegin(&vblank->seqlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		vblank_count = atomic64_read(&vblank->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		*vblanktime = vblank->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	} while (read_seqretry(&vblank->seqlock, seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	return vblank_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951)  * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952)  *     and the system timestamp corresponding to that vblank counter value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953)  * @crtc: which counter to retrieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954)  * @vblanktime: Pointer to time to receive the vblank timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956)  * Fetches the "cooked" vblank count value that represents the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957)  * vblank events since the system was booted, including lost events due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958)  * modesetting activity. Returns corresponding system timestamp of the time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959)  * of the vblank interval that corresponds to the current vblank counter value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961)  * Note that for a given vblank counter value drm_crtc_handle_vblank()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962)  * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963)  * provide a barrier: Any writes done before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964)  * drm_crtc_handle_vblank() will be visible to callers of the later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965)  * functions, iff the vblank count is the same or a later one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967)  * See also &drm_vblank_crtc.count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 				   ktime_t *vblanktime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 					 vblanktime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) static void send_vblank_event(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		struct drm_pending_vblank_event *e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		u64 seq, ktime_t now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	struct timespec64 tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	switch (e->event.base.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	case DRM_EVENT_VBLANK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	case DRM_EVENT_FLIP_COMPLETE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		tv = ktime_to_timespec64(now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		e->event.vbl.sequence = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		 * e->event is a user space structure, with hardcoded unsigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		 * 32-bit seconds/microseconds. This is safe as we always use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		 * monotonic timestamps since linux-4.15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		e->event.vbl.tv_sec = tv.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		e->event.vbl.tv_usec = tv.tv_nsec / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	case DRM_EVENT_CRTC_SEQUENCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		if (seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			e->event.seq.sequence = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		e->event.seq.time_ns = ktime_to_ns(now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	 * Use the same timestamp for any associated fence signal to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	 * mismatch in timestamps for vsync & fence events triggered by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	 * same HW event. Frameworks like SurfaceFlinger in Android expects the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	 * retire-fence timestamp to match exactly with HW vsync as it uses it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	 * for its software vsync modeling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	drm_send_event_timestamp_locked(dev, &e->base, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)  * drm_crtc_arm_vblank_event - arm vblank event after pageflip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)  * @crtc: the source CRTC of the vblank event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)  * @e: the event to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)  * A lot of drivers need to generate vblank events for the very next vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)  * interrupt. For example when the page flip interrupt happens when the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)  * flip gets armed, but not when it actually executes within the next vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)  * period. This helper function implements exactly the required vblank arming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)  * behaviour.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)  * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)  * atomic commit must ensure that the next vblank happens at exactly the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)  * time as the atomic commit is committed to the hardware. This function itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)  * does **not** protect against the next vblank interrupt racing with either this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  * function call or the atomic commit operation. A possible sequence could be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)  * 1. Driver commits new hardware state into vblank-synchronized registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  * 2. A vblank happens, committing the hardware state. Also the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  *    vblank interrupt is fired off and fully processed by the interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)  *    handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)  * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)  * 4. The event is only send out for the next vblank, which is wrong.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)  * An equivalent race can happen when the driver calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)  * drm_crtc_arm_vblank_event() before writing out the new hardware state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)  * The only way to make this work safely is to prevent the vblank from firing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)  * (and the hardware from committing anything else) until the entire atomic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)  * commit sequence has run to completion. If the hardware does not have such a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)  * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)  * Instead drivers need to manually send out the event from their interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)  * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)  * possible race with the hardware committing the atomic update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)  * Caller must hold a vblank reference for the event @e acquired by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)  * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			       struct drm_pending_vblank_event *e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	struct drm_device *dev = crtc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	unsigned int pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	assert_spin_locked(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	e->pipe = pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	list_add_tail(&e->base.link, &dev->vblank_event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)  * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)  * @crtc: the source CRTC of the vblank event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)  * @e: the event to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)  * Updates sequence # and timestamp on event for the most recently processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)  * vblank, and sends it to userspace.  Caller must hold event lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)  * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)  * situation, especially to send out events for atomic commit operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 				struct drm_pending_vblank_event *e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	struct drm_device *dev = crtc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	u64 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	unsigned int pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (drm_dev_has_vblank(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		seq = drm_vblank_count_and_time(dev, pipe, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		now = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	e->pipe = pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	send_vblank_event(dev, e, seq, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) EXPORT_SYMBOL(drm_crtc_send_vblank_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		if (drm_WARN_ON(dev, !crtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		if (crtc->funcs->enable_vblank)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 			return crtc->funcs->enable_vblank(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	} else if (dev->driver->enable_vblank) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		return dev->driver->enable_vblank(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	assert_spin_locked(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	spin_lock(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	if (!vblank->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		 * Enable vblank irqs under vblank_time_lock protection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		 * All vblank count & timestamp updates are held off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		 * until we are done reinitializing master counter and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		 * timestamps. Filtercode in drm_handle_vblank() will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		 * prevent double-accounting of same vblank interval.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		ret = __enable_vblank(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		drm_dbg_core(dev, "enabling vblank on crtc %u, ret: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 			     pipe, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			atomic_dec(&vblank->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			drm_update_vblank_count(dev, pipe, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 			/* drm_update_vblank_count() includes a wmb so we just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			 * need to ensure that the compiler emits the write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 			 * to mark the vblank as enabled after the call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 			 * to drm_update_vblank_count().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 			WRITE_ONCE(vblank->enabled, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	spin_unlock(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	unsigned long irqflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	if (!drm_dev_has_vblank(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	/* Going from 0->1 means we have to enable interrupts again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	if (atomic_add_return(1, &vblank->refcount) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		ret = drm_vblank_enable(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		if (!vblank->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			atomic_dec(&vblank->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)  * drm_crtc_vblank_get - get a reference count on vblank events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)  * @crtc: which CRTC to own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)  * Acquire a reference count on vblank events to avoid having them disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)  * while in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)  * Zero on success or a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) int drm_crtc_vblank_get(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) EXPORT_SYMBOL(drm_crtc_vblank_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	if (drm_WARN_ON(dev, atomic_read(&vblank->refcount) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	/* Last user schedules interrupt disable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	if (atomic_dec_and_test(&vblank->refcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		if (drm_vblank_offdelay == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		else if (drm_vblank_offdelay < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 			vblank_disable_fn(&vblank->disable_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		else if (!dev->vblank_disable_immediate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			mod_timer(&vblank->disable_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 				  jiffies + ((drm_vblank_offdelay * HZ)/1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)  * drm_crtc_vblank_put - give up ownership of vblank events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)  * @crtc: which counter to give up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)  * Release ownership of a given vblank counter, turning off interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) void drm_crtc_vblank_put(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) EXPORT_SYMBOL(drm_crtc_vblank_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)  * drm_wait_one_vblank - wait for one vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)  * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)  * @pipe: CRTC index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)  * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)  * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)  * due to lack of driver support or because the crtc is off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)  * This is the legacy version of drm_crtc_wait_one_vblank().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	u64 last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	ret = drm_vblank_get(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	if (drm_WARN(dev, ret, "vblank not available on crtc %i, ret=%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		     pipe, ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	last = drm_vblank_count(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	ret = wait_event_timeout(vblank->queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 				 last != drm_vblank_count(dev, pipe),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 				 msecs_to_jiffies(100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	drm_WARN(dev, ret == 0, "vblank wait timed out on crtc %i\n", pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	drm_vblank_put(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) EXPORT_SYMBOL(drm_wait_one_vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)  * drm_crtc_wait_one_vblank - wait for one vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)  * @crtc: DRM crtc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)  * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)  * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)  * due to lack of driver support or because the crtc is off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281)  * drm_crtc_vblank_off - disable vblank events on a CRTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)  * @crtc: CRTC in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)  * Drivers can use this function to shut down the vblank interrupt handling when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)  * disabling a crtc. This function ensures that the latest vblank frame count is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)  * stored so that drm_vblank_on can restore it again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288)  * Drivers must use this function when the hardware vblank counter can get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)  * reset, e.g. when suspending or disabling the @crtc in general.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) void drm_crtc_vblank_off(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	struct drm_device *dev = crtc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	unsigned int pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	struct drm_pending_vblank_event *e, *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	u64 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	 * Grab event_lock early to prevent vblank work from being scheduled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	 * while we're in the middle of shutting down vblank interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	spin_lock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	spin_lock(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		    pipe, vblank->enabled, vblank->inmodeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	/* Avoid redundant vblank disables without previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	 * drm_crtc_vblank_on(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		drm_vblank_disable_and_save(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	wake_up(&vblank->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	 * Prevent subsequent drm_vblank_get() from re-enabling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	 * the vblank interrupt by bumping the refcount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	if (!vblank->inmodeset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		atomic_inc(&vblank->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		vblank->inmodeset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	spin_unlock(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	/* Send any queued vblank events, lest the natives grow disquiet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	seq = drm_vblank_count_and_time(dev, pipe, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		if (e->pipe != pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		drm_dbg_core(dev, "Sending premature vblank event on disable: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			     "wanted %llu, current %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			     e->sequence, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		list_del(&e->base.link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		drm_vblank_put(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		send_vblank_event(dev, e, seq, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	/* Cancel any leftover pending vblank work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	drm_vblank_cancel_pending_works(vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	spin_unlock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	/* Will be reset by the modeset helpers when re-enabling the crtc by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	 * calling drm_calc_timestamping_constants(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	vblank->hwmode.crtc_clock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	/* Wait for any vblank work that's still executing to finish */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	drm_vblank_flush_worker(vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) EXPORT_SYMBOL(drm_crtc_vblank_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)  * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)  * @crtc: CRTC in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)  * Drivers can use this function to reset the vblank state to off at load time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363)  * Drivers should use this together with the drm_crtc_vblank_off() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)  * drm_crtc_vblank_on() functions. The difference compared to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)  * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)  * and hence doesn't need to call any driver hooks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)  * This is useful for recovering driver state e.g. on driver load, or on resume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) void drm_crtc_vblank_reset(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	struct drm_device *dev = crtc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	unsigned int pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	spin_lock_irq(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	 * Prevent subsequent drm_vblank_get() from enabling the vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	 * interrupt by bumping the refcount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	if (!vblank->inmodeset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		atomic_inc(&vblank->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		vblank->inmodeset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	spin_unlock_irq(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	drm_WARN_ON(dev, !list_empty(&dev->vblank_event_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	drm_WARN_ON(dev, !list_empty(&vblank->pending_work));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) EXPORT_SYMBOL(drm_crtc_vblank_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)  * drm_crtc_set_max_vblank_count - configure the hw max vblank counter value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)  * @crtc: CRTC in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)  * @max_vblank_count: max hardware vblank counter value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)  * Update the maximum hardware vblank counter value for @crtc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)  * at runtime. Useful for hardware where the operation of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399)  * hardware vblank counter depends on the currently active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)  * display configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)  * For example, if the hardware vblank counter does not work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)  * when a specific connector is active the maximum can be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)  * to zero. And when that specific connector isn't active the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)  * maximum can again be set to the appropriate non-zero value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)  * If used, must be called before drm_vblank_on().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 				   u32 max_vblank_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	struct drm_device *dev = crtc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	unsigned int pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	drm_WARN_ON(dev, dev->max_vblank_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	drm_WARN_ON(dev, !READ_ONCE(vblank->inmodeset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	vblank->max_vblank_count = max_vblank_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) EXPORT_SYMBOL(drm_crtc_set_max_vblank_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)  * drm_crtc_vblank_on - enable vblank events on a CRTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)  * @crtc: CRTC in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427)  * This functions restores the vblank interrupt state captured with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428)  * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)  * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)  * unbalanced and so can also be unconditionally called in driver load code to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431)  * reflect the current hardware state of the crtc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) void drm_crtc_vblank_on(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	struct drm_device *dev = crtc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	unsigned int pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	spin_lock_irq(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		    pipe, vblank->enabled, vblank->inmodeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	/* Drop our private "prevent drm_vblank_get" refcount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	if (vblank->inmodeset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		atomic_dec(&vblank->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		vblank->inmodeset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	drm_reset_vblank_timestamp(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	 * re-enable interrupts if there are users left, or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	 * user wishes vblank interrupts to be enabled all the time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		drm_WARN_ON(dev, drm_vblank_enable(dev, pipe));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	spin_unlock_irq(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) EXPORT_SYMBOL(drm_crtc_vblank_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)  * drm_vblank_restore - estimate missed vblanks and update vblank count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)  * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)  * @pipe: CRTC index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)  * Power manamement features can cause frame counter resets between vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)  * disable and enable. Drivers can use this function in their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)  * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)  * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)  * vblank counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)  * This function is the legacy version of drm_crtc_vblank_restore().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) void drm_vblank_restore(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	ktime_t t_vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	struct drm_vblank_crtc *vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	int framedur_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	u64 diff_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	u32 cur_vblank, diff = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	int count = DRM_TIMESTAMP_MAXRETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	assert_spin_locked(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	assert_spin_locked(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	drm_WARN_ONCE(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		      drm_debug_enabled(DRM_UT_VBL) && !vblank->framedur_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		      "Cannot compute missed vblanks without frame duration\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	framedur_ns = vblank->framedur_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		cur_vblank = __get_vblank_counter(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	} while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	if (framedur_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	drm_dbg_vbl(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		    "missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		    diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) EXPORT_SYMBOL(drm_vblank_restore);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516)  * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)  * @crtc: CRTC in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)  * Power manamement features can cause frame counter resets between vblank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)  * disable and enable. Drivers can use this function in their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)  * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)  * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)  * vblank counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) void drm_crtc_vblank_restore(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	drm_vblank_restore(crtc->dev, drm_crtc_index(crtc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) EXPORT_SYMBOL(drm_crtc_vblank_restore);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 					  unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	if (!drm_dev_has_vblank(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	 * To avoid all the problems that might happen if interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	 * were enabled/disabled around or between these calls, we just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	 * have the kernel take a reference on the CRTC (just once though
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	 * to avoid corrupting the count if multiple, mismatch calls occur),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	 * so that interrupts remain enabled in the interim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	if (!vblank->inmodeset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		vblank->inmodeset = 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		if (drm_vblank_get(dev, pipe) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 			vblank->inmodeset |= 0x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 					   unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	if (!drm_dev_has_vblank(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	if (vblank->inmodeset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		spin_lock_irq(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		drm_reset_vblank_timestamp(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		spin_unlock_irq(&dev->vbl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		if (vblank->inmodeset & 0x2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 			drm_vblank_put(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		vblank->inmodeset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 				 struct drm_file *file_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	struct drm_modeset_ctl *modeset = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	unsigned int pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	/* If drm_vblank_init() hasn't been called yet, just no-op */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	if (!drm_dev_has_vblank(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	/* KMS drivers handle this internally */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	pipe = modeset->crtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	if (pipe >= dev->num_crtcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	switch (modeset->cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	case _DRM_PRE_MODESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		drm_legacy_vblank_pre_modeset(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	case _DRM_POST_MODESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		drm_legacy_vblank_post_modeset(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 				  u64 req_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 				  union drm_wait_vblank *vblwait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 				  struct drm_file *file_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	struct drm_pending_vblank_event *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	u64 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	e = kzalloc(sizeof(*e), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	if (e == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	e->pipe = pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	e->event.base.type = DRM_EVENT_VBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	e->event.base.length = sizeof(e->event.vbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	e->event.vbl.user_data = vblwait->request.signal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	e->event.vbl.crtc_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		if (crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 			e->event.vbl.crtc_id = crtc->base.id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	spin_lock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	 * drm_crtc_vblank_off() might have been called after we called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	 * vblank disable, so no need for further locking.  The reference from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	 * drm_vblank_get() protects against vblank disable from another source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	if (!READ_ONCE(vblank->enabled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 					    &e->event.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	seq = drm_vblank_count_and_time(dev, pipe, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	drm_dbg_core(dev, "event on vblank count %llu, current %llu, crtc %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		     req_seq, seq, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	e->sequence = req_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	if (drm_vblank_passed(seq, req_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		drm_vblank_put(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		send_vblank_event(dev, e, seq, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		vblwait->reply.sequence = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		/* drm_handle_vblank_events will call drm_vblank_put */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		list_add_tail(&e->base.link, &dev->vblank_event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		vblwait->reply.sequence = req_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	spin_unlock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	spin_unlock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	kfree(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) err_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	drm_vblank_put(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	if (vblwait->request.sequence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	return _DRM_VBLANK_RELATIVE ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		(vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 					  _DRM_VBLANK_EVENT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 					  _DRM_VBLANK_NEXTONMISS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703)  * Widen a 32-bit param to 64-bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705)  * \param narrow 32-bit value (missing upper 32 bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)  * \param near 64-bit value that should be 'close' to near
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)  * This function returns a 64-bit value using the lower 32-bits from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709)  * 'narrow' and constructing the upper 32-bits so that the result is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)  * as close as possible to 'near'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) static u64 widen_32_to_64(u32 narrow, u64 near)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	return near + (s32) (narrow - near);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 				  struct drm_wait_vblank_reply *reply)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	 * to store the seconds. This is safe as we always use monotonic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	 * timestamps since linux-4.15.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	reply->sequence = drm_vblank_count_and_time(dev, pipe, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	ts = ktime_to_timespec64(now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	reply->tval_sec = (u32)ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	reply->tval_usec = ts.tv_nsec / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 			  struct drm_file *file_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	struct drm_crtc *crtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	struct drm_vblank_crtc *vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	union drm_wait_vblank *vblwait = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	u64 req_seq, seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	unsigned int pipe_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	unsigned int flags, pipe, high_pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	if (!dev->irq_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	if (vblwait->request.type &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	    ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	      _DRM_VBLANK_HIGH_CRTC_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		drm_dbg_core(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 			     "Unsupported type value 0x%x, supported mask 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 			     vblwait->request.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 			     (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			      _DRM_VBLANK_HIGH_CRTC_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	if (high_pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		pipe_index = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		pipe_index = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	/* Convert lease-relative crtc index into global crtc index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		pipe = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		drm_for_each_crtc(crtc, dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 			if (drm_lease_held(file_priv, crtc->base.id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 				if (pipe_index == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 				pipe_index--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 			pipe++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		pipe = pipe_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	if (pipe >= dev->num_crtcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	/* If the counter is currently enabled and accurate, short-circuit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	 * queries to return the cached timestamp of the last vblank.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	if (dev->vblank_disable_immediate &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	    drm_wait_vblank_is_query(vblwait) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	    READ_ONCE(vblank->enabled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	ret = drm_vblank_get(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		drm_dbg_core(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 			     "crtc %d failed to acquire vblank counter, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 			     pipe, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	seq = drm_vblank_count(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	case _DRM_VBLANK_RELATIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		req_seq = seq + vblwait->request.sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		vblwait->request.sequence = req_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	case _DRM_VBLANK_ABSOLUTE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		req_seq = widen_32_to_64(vblwait->request.sequence, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	    drm_vblank_passed(seq, req_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		req_seq = seq + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		vblwait->request.sequence = req_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	if (flags & _DRM_VBLANK_EVENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		/* must hold on to the vblank ref until the event fires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		 * drm_vblank_put will be called asynchronously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	if (req_seq != seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		int wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		drm_dbg_core(dev, "waiting on vblank count %llu, crtc %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			     req_seq, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		wait = wait_event_interruptible_timeout(vblank->queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 			drm_vblank_passed(drm_vblank_count(dev, pipe), req_seq) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 				      !READ_ONCE(vblank->enabled),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 			msecs_to_jiffies(3000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		switch (wait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 			/* timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 			ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 		case -ERESTARTSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 			/* interrupted by signal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 			ret = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	if (ret != -EINTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		drm_dbg_core(dev, "crtc %d returning %u to client\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 			     pipe, vblwait->reply.sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		drm_dbg_core(dev, "crtc %d vblank wait interrupted by signal\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 			     pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	drm_vblank_put(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	bool high_prec = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	struct drm_pending_vblank_event *e, *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	u64 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	assert_spin_locked(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	seq = drm_vblank_count_and_time(dev, pipe, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 		if (e->pipe != pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 		if (!drm_vblank_passed(seq, e->sequence))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 		drm_dbg_core(dev, "vblank event on %llu, current %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 			     e->sequence, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 		list_del(&e->base.link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 		drm_vblank_put(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 		send_vblank_event(dev, e, seq, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	if (crtc && crtc->funcs->get_vblank_timestamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		high_prec = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	trace_drm_vblank_event(pipe, seq, now, high_prec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)  * drm_handle_vblank - handle a vblank event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911)  * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912)  * @pipe: index of CRTC where this event occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)  * Drivers should call this routine in their vblank interrupt handlers to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915)  * update the vblank counter and send any signals that may be pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917)  * This is the legacy version of drm_crtc_handle_vblank().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	unsigned long irqflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	bool disable_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	if (drm_WARN_ON_ONCE(dev, !drm_dev_has_vblank(dev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	spin_lock_irqsave(&dev->event_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	/* Need timestamp lock to prevent concurrent execution with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	 * vblank enable/disable, as this would cause inconsistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	 * or corrupted timestamps and vblank counts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	spin_lock(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	/* Vblank irq handling disabled. Nothing to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	if (!vblank->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		spin_unlock(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		spin_unlock_irqrestore(&dev->event_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	drm_update_vblank_count(dev, pipe, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	spin_unlock(&dev->vblank_time_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	wake_up(&vblank->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	/* With instant-off, we defer disabling the interrupt until after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	 * we finish processing the following vblank after all events have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	 * been signaled. The disable has to be last (after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	 * drm_handle_vblank_events) so that the timestamp is always accurate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	disable_irq = (dev->vblank_disable_immediate &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 		       drm_vblank_offdelay > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		       !atomic_read(&vblank->refcount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	drm_handle_vblank_events(dev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	drm_handle_vblank_works(vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	if (disable_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 		vblank_disable_fn(&vblank->disable_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) EXPORT_SYMBOL(drm_handle_vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974)  * drm_crtc_handle_vblank - handle a vblank event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975)  * @crtc: where this event occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977)  * Drivers should call this routine in their vblank interrupt handlers to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978)  * update the vblank counter and send any signals that may be pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)  * This is the native KMS version of drm_handle_vblank().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982)  * Note that for a given vblank counter value drm_crtc_handle_vblank()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983)  * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984)  * provide a barrier: Any writes done before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985)  * drm_crtc_handle_vblank() will be visible to callers of the later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986)  * functions, iff the vblank count is the same or a later one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988)  * See also &drm_vblank_crtc.count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991)  * True if the event was successfully handled, false on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) EXPORT_SYMBOL(drm_crtc_handle_vblank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000)  * Get crtc VBLANK count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002)  * \param dev DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)  * \param data user arguement, pointing to a drm_crtc_get_sequence structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004)  * \param file_priv drm file private for the user's open file descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 				struct drm_file *file_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	struct drm_crtc *crtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	struct drm_vblank_crtc *vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	int pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	struct drm_crtc_get_sequence *get_seq = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	bool vblank_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	if (!dev->irq_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	if (!crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	vblank_enabled = dev->vblank_disable_immediate && READ_ONCE(vblank->enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	if (!vblank_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		ret = drm_crtc_vblank_get(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 			drm_dbg_core(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 				     "crtc %d failed to acquire vblank counter, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 				     pipe, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	drm_modeset_lock(&crtc->mutex, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	if (crtc->state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		get_seq->active = crtc->state->enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		get_seq->active = crtc->enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	drm_modeset_unlock(&crtc->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	get_seq->sequence_ns = ktime_to_ns(now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	if (!vblank_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		drm_crtc_vblank_put(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056)  * Queue a event for VBLANK sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058)  * \param dev DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059)  * \param data user arguement, pointing to a drm_crtc_queue_sequence structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060)  * \param file_priv drm file private for the user's open file descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 				  struct drm_file *file_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	struct drm_crtc *crtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	struct drm_vblank_crtc *vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	int pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	struct drm_crtc_queue_sequence *queue_seq = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	struct drm_pending_vblank_event *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	u64 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	u64 req_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	if (!dev->irq_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	if (!crtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	flags = queue_seq->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	/* Check valid flag bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 		      DRM_CRTC_SEQUENCE_NEXT_ON_MISS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	pipe = drm_crtc_index(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	vblank = &dev->vblank[pipe];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	e = kzalloc(sizeof(*e), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	if (e == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	ret = drm_crtc_vblank_get(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		drm_dbg_core(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 			     "crtc %d failed to acquire vblank counter, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 			     pipe, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 		goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	seq = drm_vblank_count_and_time(dev, pipe, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	req_seq = queue_seq->sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		req_seq += seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && drm_vblank_passed(seq, req_seq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 		req_seq = seq + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	e->pipe = pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	e->event.base.type = DRM_EVENT_CRTC_SEQUENCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	e->event.base.length = sizeof(e->event.seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	e->event.seq.user_data = queue_seq->user_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	spin_lock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	 * drm_crtc_vblank_off() might have been called after we called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	 * vblank disable, so no need for further locking.  The reference from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	 * drm_crtc_vblank_get() protects against vblank disable from another source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	if (!READ_ONCE(vblank->enabled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 					    &e->event.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 		goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	e->sequence = req_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	if (drm_vblank_passed(seq, req_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 		drm_crtc_vblank_put(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 		send_vblank_event(dev, e, seq, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		queue_seq->sequence = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		/* drm_handle_vblank_events will call drm_vblank_put */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 		list_add_tail(&e->base.link, &dev->vblank_event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 		queue_seq->sequence = req_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	spin_unlock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	spin_unlock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	drm_crtc_vblank_put(crtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	kfree(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164)