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