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