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