Commit | Line | Data |
---|---|---|
3ed4351a DV |
1 | /* |
2 | * drm_irq.c IRQ and vblank support | |
3 | * | |
4 | * \author Rickard E. (Rik) Faith <faith@valinux.com> | |
5 | * \author Gareth Hughes <gareth@valinux.com> | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | * copy of this software and associated documentation files (the "Software"), | |
9 | * to deal in the Software without restriction, including without limitation | |
10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | * and/or sell copies of the Software, and to permit persons to whom the | |
12 | * Software is furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice (including the next | |
15 | * paragraph) shall be included in all copies or substantial portions of the | |
16 | * Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
22 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
23 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
24 | * OTHER DEALINGS IN THE SOFTWARE. | |
25 | */ | |
26 | ||
27 | #include <drm/drm_vblank.h> | |
28 | #include <drm/drmP.h> | |
29 | #include <linux/export.h> | |
30 | ||
31 | #include "drm_trace.h" | |
32 | #include "drm_internal.h" | |
33 | ||
57d30230 DV |
34 | /** |
35 | * DOC: vblank handling | |
36 | * | |
37 | * Vertical blanking plays a major role in graphics rendering. To achieve | |
38 | * tear-free display, users must synchronize page flips and/or rendering to | |
39 | * vertical blanking. The DRM API offers ioctls to perform page flips | |
40 | * synchronized to vertical blanking and wait for vertical blanking. | |
41 | * | |
42 | * The DRM core handles most of the vertical blanking management logic, which | |
43 | * involves filtering out spurious interrupts, keeping race-free blanking | |
44 | * counters, coping with counter wrap-around and resets and keeping use counts. | |
45 | * It relies on the driver to generate vertical blanking interrupts and | |
46 | * optionally provide a hardware vertical blanking counter. | |
47 | * | |
48 | * Drivers must initialize the vertical blanking handling core with a call to | |
49 | * drm_vblank_init(). Minimally, a driver needs to implement | |
50 | * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call | |
51 | * drm_crtc_handle_vblank() in it's vblank interrupt handler for working vblank | |
52 | * support. | |
53 | * | |
54 | * Vertical blanking interrupts can be enabled by the DRM core or by drivers | |
55 | * themselves (for instance to handle page flipping operations). The DRM core | |
56 | * maintains a vertical blanking use count to ensure that the interrupts are not | |
57 | * disabled while a user still needs them. To increment the use count, drivers | |
58 | * call drm_crtc_vblank_get() and release the vblank reference again with | |
59 | * drm_crtc_vblank_put(). In between these two calls vblank interrupts are | |
60 | * guaranteed to be enabled. | |
61 | * | |
62 | * On many hardware disabling the vblank interrupt cannot be done in a race-free | |
63 | * manner, see &drm_driver.vblank_disable_immediate and | |
64 | * &drm_driver.max_vblank_count. In that case the vblank core only disables the | |
65 | * vblanks after a timer has expired, which can be configured through the | |
66 | * ``vblankoffdelay`` module parameter. | |
67 | */ | |
68 | ||
3ed4351a DV |
69 | /* Retry timestamp calculation up to 3 times to satisfy |
70 | * drm_timestamp_precision before giving up. | |
71 | */ | |
72 | #define DRM_TIMESTAMP_MAXRETRIES 3 | |
73 | ||
74 | /* Threshold in nanoseconds for detection of redundant | |
75 | * vblank irq in drm_handle_vblank(). 1 msec should be ok. | |
76 | */ | |
77 | #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000 | |
78 | ||
79 | static bool | |
80 | drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe, | |
81 | struct timeval *tvblank, bool in_vblank_irq); | |
82 | ||
83 | static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */ | |
84 | ||
85 | /* | |
86 | * Default to use monotonic timestamps for wait-for-vblank and page-flip | |
87 | * complete events. | |
88 | */ | |
89 | unsigned int drm_timestamp_monotonic = 1; | |
90 | ||
91 | static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */ | |
92 | ||
93 | module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600); | |
94 | module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600); | |
95 | module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600); | |
96 | MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)"); | |
97 | MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]"); | |
98 | MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps"); | |
99 | ||
100 | static void store_vblank(struct drm_device *dev, unsigned int pipe, | |
101 | u32 vblank_count_inc, | |
102 | struct timeval *t_vblank, u32 last) | |
103 | { | |
104 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
105 | ||
106 | assert_spin_locked(&dev->vblank_time_lock); | |
107 | ||
108 | vblank->last = last; | |
109 | ||
110 | write_seqlock(&vblank->seqlock); | |
111 | vblank->time = *t_vblank; | |
112 | vblank->count += vblank_count_inc; | |
113 | write_sequnlock(&vblank->seqlock); | |
114 | } | |
115 | ||
116 | /* | |
117 | * "No hw counter" fallback implementation of .get_vblank_counter() hook, | |
118 | * if there is no useable hardware frame counter available. | |
119 | */ | |
120 | static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe) | |
121 | { | |
122 | WARN_ON_ONCE(dev->max_vblank_count != 0); | |
123 | return 0; | |
124 | } | |
125 | ||
126 | static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe) | |
127 | { | |
128 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { | |
129 | struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe); | |
130 | ||
131 | if (crtc->funcs->get_vblank_counter) | |
132 | return crtc->funcs->get_vblank_counter(crtc); | |
133 | } | |
134 | ||
135 | if (dev->driver->get_vblank_counter) | |
136 | return dev->driver->get_vblank_counter(dev, pipe); | |
137 | ||
138 | return drm_vblank_no_hw_counter(dev, pipe); | |
139 | } | |
140 | ||
141 | /* | |
142 | * Reset the stored timestamp for the current vblank count to correspond | |
143 | * to the last vblank occurred. | |
144 | * | |
145 | * Only to be called from drm_crtc_vblank_on(). | |
146 | * | |
147 | * Note: caller must hold &drm_device.vbl_lock since this reads & writes | |
148 | * device vblank fields. | |
149 | */ | |
150 | static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe) | |
151 | { | |
152 | u32 cur_vblank; | |
153 | bool rc; | |
154 | struct timeval t_vblank; | |
155 | int count = DRM_TIMESTAMP_MAXRETRIES; | |
156 | ||
157 | spin_lock(&dev->vblank_time_lock); | |
158 | ||
159 | /* | |
160 | * sample the current counter to avoid random jumps | |
161 | * when drm_vblank_enable() applies the diff | |
162 | */ | |
163 | do { | |
164 | cur_vblank = __get_vblank_counter(dev, pipe); | |
165 | rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false); | |
166 | } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0); | |
167 | ||
168 | /* | |
169 | * Only reinitialize corresponding vblank timestamp if high-precision query | |
170 | * available and didn't fail. Otherwise reinitialize delayed at next vblank | |
171 | * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid. | |
172 | */ | |
173 | if (!rc) | |
174 | t_vblank = (struct timeval) {0, 0}; | |
175 | ||
176 | /* | |
177 | * +1 to make sure user will never see the same | |
178 | * vblank counter value before and after a modeset | |
179 | */ | |
180 | store_vblank(dev, pipe, 1, &t_vblank, cur_vblank); | |
181 | ||
182 | spin_unlock(&dev->vblank_time_lock); | |
183 | } | |
184 | ||
185 | /* | |
186 | * Call back into the driver to update the appropriate vblank counter | |
187 | * (specified by @pipe). Deal with wraparound, if it occurred, and | |
188 | * update the last read value so we can deal with wraparound on the next | |
189 | * call if necessary. | |
190 | * | |
191 | * Only necessary when going from off->on, to account for frames we | |
192 | * didn't get an interrupt for. | |
193 | * | |
194 | * Note: caller must hold &drm_device.vbl_lock since this reads & writes | |
195 | * device vblank fields. | |
196 | */ | |
197 | static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, | |
198 | bool in_vblank_irq) | |
199 | { | |
200 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
201 | u32 cur_vblank, diff; | |
202 | bool rc; | |
203 | struct timeval t_vblank; | |
204 | int count = DRM_TIMESTAMP_MAXRETRIES; | |
205 | int framedur_ns = vblank->framedur_ns; | |
206 | ||
207 | /* | |
208 | * Interrupts were disabled prior to this call, so deal with counter | |
209 | * wrap if needed. | |
210 | * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events | |
211 | * here if the register is small or we had vblank interrupts off for | |
212 | * a long time. | |
213 | * | |
214 | * We repeat the hardware vblank counter & timestamp query until | |
215 | * we get consistent results. This to prevent races between gpu | |
216 | * updating its hardware counter while we are retrieving the | |
217 | * corresponding vblank timestamp. | |
218 | */ | |
219 | do { | |
220 | cur_vblank = __get_vblank_counter(dev, pipe); | |
221 | rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq); | |
222 | } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0); | |
223 | ||
224 | if (dev->max_vblank_count != 0) { | |
225 | /* trust the hw counter when it's around */ | |
226 | diff = (cur_vblank - vblank->last) & dev->max_vblank_count; | |
227 | } else if (rc && framedur_ns) { | |
228 | const struct timeval *t_old; | |
229 | u64 diff_ns; | |
230 | ||
231 | t_old = &vblank->time; | |
232 | diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old); | |
233 | ||
234 | /* | |
235 | * Figure out how many vblanks we've missed based | |
236 | * on the difference in the timestamps and the | |
237 | * frame/field duration. | |
238 | */ | |
239 | diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns); | |
240 | ||
241 | if (diff == 0 && in_vblank_irq) | |
242 | DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored." | |
243 | " diff_ns = %lld, framedur_ns = %d)\n", | |
244 | pipe, (long long) diff_ns, framedur_ns); | |
245 | } else { | |
246 | /* some kind of default for drivers w/o accurate vbl timestamping */ | |
247 | diff = in_vblank_irq ? 1 : 0; | |
248 | } | |
249 | ||
250 | /* | |
251 | * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset | |
252 | * interval? If so then vblank irqs keep running and it will likely | |
253 | * happen that the hardware vblank counter is not trustworthy as it | |
254 | * might reset at some point in that interval and vblank timestamps | |
255 | * are not trustworthy either in that interval. Iow. this can result | |
256 | * in a bogus diff >> 1 which must be avoided as it would cause | |
257 | * random large forward jumps of the software vblank counter. | |
258 | */ | |
259 | if (diff > 1 && (vblank->inmodeset & 0x2)) { | |
260 | DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u" | |
261 | " due to pre-modeset.\n", pipe, diff); | |
262 | diff = 1; | |
263 | } | |
264 | ||
265 | DRM_DEBUG_VBL("updating vblank count on crtc %u:" | |
266 | " current=%u, diff=%u, hw=%u hw_last=%u\n", | |
267 | pipe, vblank->count, diff, cur_vblank, vblank->last); | |
268 | ||
269 | if (diff == 0) { | |
270 | WARN_ON_ONCE(cur_vblank != vblank->last); | |
271 | return; | |
272 | } | |
273 | ||
274 | /* | |
275 | * Only reinitialize corresponding vblank timestamp if high-precision query | |
276 | * available and didn't fail, or we were called from the vblank interrupt. | |
277 | * Otherwise reinitialize delayed at next vblank interrupt and assign 0 | |
278 | * for now, to mark the vblanktimestamp as invalid. | |
279 | */ | |
280 | if (!rc && in_vblank_irq) | |
281 | t_vblank = (struct timeval) {0, 0}; | |
282 | ||
283 | store_vblank(dev, pipe, diff, &t_vblank, cur_vblank); | |
284 | } | |
285 | ||
286 | static u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe) | |
287 | { | |
288 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
289 | ||
290 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
291 | return 0; | |
292 | ||
293 | return vblank->count; | |
294 | } | |
295 | ||
296 | /** | |
297 | * drm_accurate_vblank_count - retrieve the master vblank counter | |
298 | * @crtc: which counter to retrieve | |
299 | * | |
57d30230 DV |
300 | * This function is similar to drm_crtc_vblank_count() but this function |
301 | * interpolates to handle a race with vblank interrupts using the high precision | |
302 | * timestamping support. | |
3ed4351a | 303 | * |
57d30230 DV |
304 | * This is mostly useful for hardware that can obtain the scanout position, but |
305 | * doesn't have a hardware frame counter. | |
3ed4351a DV |
306 | */ |
307 | u32 drm_accurate_vblank_count(struct drm_crtc *crtc) | |
308 | { | |
309 | struct drm_device *dev = crtc->dev; | |
310 | unsigned int pipe = drm_crtc_index(crtc); | |
311 | u32 vblank; | |
312 | unsigned long flags; | |
313 | ||
314 | WARN(!dev->driver->get_vblank_timestamp, | |
315 | "This function requires support for accurate vblank timestamps."); | |
316 | ||
317 | spin_lock_irqsave(&dev->vblank_time_lock, flags); | |
318 | ||
319 | drm_update_vblank_count(dev, pipe, false); | |
320 | vblank = drm_vblank_count(dev, pipe); | |
321 | ||
322 | spin_unlock_irqrestore(&dev->vblank_time_lock, flags); | |
323 | ||
324 | return vblank; | |
325 | } | |
326 | EXPORT_SYMBOL(drm_accurate_vblank_count); | |
327 | ||
328 | static void __disable_vblank(struct drm_device *dev, unsigned int pipe) | |
329 | { | |
330 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { | |
331 | struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe); | |
332 | ||
333 | if (crtc->funcs->disable_vblank) { | |
334 | crtc->funcs->disable_vblank(crtc); | |
335 | return; | |
336 | } | |
337 | } | |
338 | ||
339 | dev->driver->disable_vblank(dev, pipe); | |
340 | } | |
341 | ||
342 | /* | |
343 | * Disable vblank irq's on crtc, make sure that last vblank count | |
344 | * of hardware and corresponding consistent software vblank counter | |
345 | * are preserved, even if there are any spurious vblank irq's after | |
346 | * disable. | |
347 | */ | |
348 | void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe) | |
349 | { | |
350 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
351 | unsigned long irqflags; | |
352 | ||
353 | assert_spin_locked(&dev->vbl_lock); | |
354 | ||
355 | /* Prevent vblank irq processing while disabling vblank irqs, | |
356 | * so no updates of timestamps or count can happen after we've | |
357 | * disabled. Needed to prevent races in case of delayed irq's. | |
358 | */ | |
359 | spin_lock_irqsave(&dev->vblank_time_lock, irqflags); | |
360 | ||
361 | /* | |
362 | * Only disable vblank interrupts if they're enabled. This avoids | |
363 | * calling the ->disable_vblank() operation in atomic context with the | |
364 | * hardware potentially runtime suspended. | |
365 | */ | |
366 | if (vblank->enabled) { | |
367 | __disable_vblank(dev, pipe); | |
368 | vblank->enabled = false; | |
369 | } | |
370 | ||
371 | /* | |
372 | * Always update the count and timestamp to maintain the | |
373 | * appearance that the counter has been ticking all along until | |
374 | * this time. This makes the count account for the entire time | |
375 | * between drm_crtc_vblank_on() and drm_crtc_vblank_off(). | |
376 | */ | |
377 | drm_update_vblank_count(dev, pipe, false); | |
378 | ||
379 | spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags); | |
380 | } | |
381 | ||
382 | static void vblank_disable_fn(unsigned long arg) | |
383 | { | |
384 | struct drm_vblank_crtc *vblank = (void *)arg; | |
385 | struct drm_device *dev = vblank->dev; | |
386 | unsigned int pipe = vblank->pipe; | |
387 | unsigned long irqflags; | |
388 | ||
389 | spin_lock_irqsave(&dev->vbl_lock, irqflags); | |
390 | if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) { | |
391 | DRM_DEBUG("disabling vblank on crtc %u\n", pipe); | |
392 | drm_vblank_disable_and_save(dev, pipe); | |
393 | } | |
394 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); | |
395 | } | |
396 | ||
397 | /** | |
398 | * drm_vblank_cleanup - cleanup vblank support | |
399 | * @dev: DRM device | |
400 | * | |
57d30230 DV |
401 | * This function cleans up any resources allocated in drm_vblank_init(). It is |
402 | * called by the DRM core when @dev is finalized. | |
403 | * | |
404 | * Drivers can call drm_vblank_cleanup() if they need to quiescent the vblank | |
405 | * interrupt in their unload code. But in general this should be handled by | |
406 | * disabling all active &drm_crtc through e.g. drm_atomic_helper_shutdown, which | |
407 | * should end up calling drm_crtc_vblank_off(). | |
16584b20 | 408 | * |
3ed4351a DV |
409 | */ |
410 | void drm_vblank_cleanup(struct drm_device *dev) | |
411 | { | |
412 | unsigned int pipe; | |
413 | ||
414 | /* Bail if the driver didn't call drm_vblank_init() */ | |
415 | if (dev->num_crtcs == 0) | |
416 | return; | |
417 | ||
418 | for (pipe = 0; pipe < dev->num_crtcs; pipe++) { | |
419 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
420 | ||
421 | WARN_ON(READ_ONCE(vblank->enabled) && | |
422 | drm_core_check_feature(dev, DRIVER_MODESET)); | |
423 | ||
424 | del_timer_sync(&vblank->disable_timer); | |
425 | } | |
426 | ||
427 | kfree(dev->vblank); | |
428 | ||
429 | dev->num_crtcs = 0; | |
430 | } | |
431 | EXPORT_SYMBOL(drm_vblank_cleanup); | |
432 | ||
433 | /** | |
434 | * drm_vblank_init - initialize vblank support | |
435 | * @dev: DRM device | |
436 | * @num_crtcs: number of CRTCs supported by @dev | |
437 | * | |
438 | * This function initializes vblank support for @num_crtcs display pipelines. | |
57d30230 DV |
439 | * Drivers do not need to call drm_vblank_cleanup(), cleanup is already handled |
440 | * by the DRM core, or through calling drm_dev_fini() for drivers with a | |
441 | * &drm_driver.release callback. | |
3ed4351a DV |
442 | * |
443 | * Returns: | |
444 | * Zero on success or a negative error code on failure. | |
445 | */ | |
446 | int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs) | |
447 | { | |
448 | int ret = -ENOMEM; | |
449 | unsigned int i; | |
450 | ||
451 | spin_lock_init(&dev->vbl_lock); | |
452 | spin_lock_init(&dev->vblank_time_lock); | |
453 | ||
454 | dev->num_crtcs = num_crtcs; | |
455 | ||
456 | dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL); | |
457 | if (!dev->vblank) | |
458 | goto err; | |
459 | ||
460 | for (i = 0; i < num_crtcs; i++) { | |
461 | struct drm_vblank_crtc *vblank = &dev->vblank[i]; | |
462 | ||
463 | vblank->dev = dev; | |
464 | vblank->pipe = i; | |
465 | init_waitqueue_head(&vblank->queue); | |
466 | setup_timer(&vblank->disable_timer, vblank_disable_fn, | |
467 | (unsigned long)vblank); | |
468 | seqlock_init(&vblank->seqlock); | |
469 | } | |
470 | ||
471 | DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n"); | |
472 | ||
473 | /* Driver specific high-precision vblank timestamping supported? */ | |
474 | if (dev->driver->get_vblank_timestamp) | |
475 | DRM_INFO("Driver supports precise vblank timestamp query.\n"); | |
476 | else | |
477 | DRM_INFO("No driver support for vblank timestamp query.\n"); | |
478 | ||
479 | /* Must have precise timestamping for reliable vblank instant disable */ | |
480 | if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) { | |
481 | dev->vblank_disable_immediate = false; | |
482 | DRM_INFO("Setting vblank_disable_immediate to false because " | |
483 | "get_vblank_timestamp == NULL\n"); | |
484 | } | |
485 | ||
486 | return 0; | |
487 | ||
488 | err: | |
489 | dev->num_crtcs = 0; | |
490 | return ret; | |
491 | } | |
492 | EXPORT_SYMBOL(drm_vblank_init); | |
493 | ||
494 | /** | |
495 | * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC | |
496 | * @crtc: which CRTC's vblank waitqueue to retrieve | |
497 | * | |
498 | * This function returns a pointer to the vblank waitqueue for the CRTC. | |
499 | * Drivers can use this to implement vblank waits using wait_event() and related | |
500 | * functions. | |
501 | */ | |
502 | wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc) | |
503 | { | |
504 | return &crtc->dev->vblank[drm_crtc_index(crtc)].queue; | |
505 | } | |
506 | EXPORT_SYMBOL(drm_crtc_vblank_waitqueue); | |
507 | ||
508 | ||
509 | /** | |
510 | * drm_calc_timestamping_constants - calculate vblank timestamp constants | |
511 | * @crtc: drm_crtc whose timestamp constants should be updated. | |
512 | * @mode: display mode containing the scanout timings | |
513 | * | |
57d30230 DV |
514 | * Calculate and store various constants which are later needed by vblank and |
515 | * swap-completion timestamping, e.g, by | |
516 | * drm_calc_vbltimestamp_from_scanoutpos(). They are derived from CRTC's true | |
517 | * scanout timing, so they take things like panel scaling or other adjustments | |
518 | * into account. | |
3ed4351a DV |
519 | */ |
520 | void drm_calc_timestamping_constants(struct drm_crtc *crtc, | |
521 | const struct drm_display_mode *mode) | |
522 | { | |
523 | struct drm_device *dev = crtc->dev; | |
524 | unsigned int pipe = drm_crtc_index(crtc); | |
525 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
526 | int linedur_ns = 0, framedur_ns = 0; | |
527 | int dotclock = mode->crtc_clock; | |
528 | ||
529 | if (!dev->num_crtcs) | |
530 | return; | |
531 | ||
532 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
533 | return; | |
534 | ||
535 | /* Valid dotclock? */ | |
536 | if (dotclock > 0) { | |
537 | int frame_size = mode->crtc_htotal * mode->crtc_vtotal; | |
538 | ||
539 | /* | |
540 | * Convert scanline length in pixels and video | |
541 | * dot clock to line duration and frame duration | |
542 | * in nanoseconds: | |
543 | */ | |
544 | linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock); | |
545 | framedur_ns = div_u64((u64) frame_size * 1000000, dotclock); | |
546 | ||
547 | /* | |
548 | * Fields of interlaced scanout modes are only half a frame duration. | |
549 | */ | |
550 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) | |
551 | framedur_ns /= 2; | |
552 | } else | |
553 | DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n", | |
554 | crtc->base.id); | |
555 | ||
556 | vblank->linedur_ns = linedur_ns; | |
557 | vblank->framedur_ns = framedur_ns; | |
558 | vblank->hwmode = *mode; | |
559 | ||
560 | DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n", | |
561 | crtc->base.id, mode->crtc_htotal, | |
562 | mode->crtc_vtotal, mode->crtc_vdisplay); | |
563 | DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n", | |
564 | crtc->base.id, dotclock, framedur_ns, linedur_ns); | |
565 | } | |
566 | EXPORT_SYMBOL(drm_calc_timestamping_constants); | |
567 | ||
568 | /** | |
569 | * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper | |
570 | * @dev: DRM device | |
571 | * @pipe: index of CRTC whose vblank timestamp to retrieve | |
572 | * @max_error: Desired maximum allowable error in timestamps (nanosecs) | |
573 | * On return contains true maximum error of timestamp | |
574 | * @vblank_time: Pointer to struct timeval which should receive the timestamp | |
575 | * @in_vblank_irq: | |
576 | * True when called from drm_crtc_handle_vblank(). Some drivers | |
577 | * need to apply some workarounds for gpu-specific vblank irq quirks | |
578 | * if flag is set. | |
579 | * | |
580 | * Implements calculation of exact vblank timestamps from given drm_display_mode | |
57d30230 DV |
581 | * timings and current video scanout position of a CRTC. This can be directly |
582 | * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver | |
583 | * if &drm_driver.get_scanout_position is implemented. | |
3ed4351a | 584 | * |
57d30230 DV |
585 | * The current implementation only handles standard video modes. For double scan |
586 | * and interlaced modes the driver is supposed to adjust the hardware mode | |
587 | * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to | |
588 | * match the scanout position reported. | |
3ed4351a DV |
589 | * |
590 | * Note that atomic drivers must call drm_calc_timestamping_constants() before | |
591 | * enabling a CRTC. The atomic helpers already take care of that in | |
592 | * drm_atomic_helper_update_legacy_modeset_state(). | |
593 | * | |
594 | * Returns: | |
595 | * | |
596 | * Returns true on success, and false on failure, i.e. when no accurate | |
597 | * timestamp could be acquired. | |
598 | */ | |
599 | bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, | |
600 | unsigned int pipe, | |
601 | int *max_error, | |
602 | struct timeval *vblank_time, | |
603 | bool in_vblank_irq) | |
604 | { | |
605 | struct timeval tv_etime; | |
606 | ktime_t stime, etime; | |
607 | bool vbl_status; | |
608 | struct drm_crtc *crtc; | |
609 | const struct drm_display_mode *mode; | |
610 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
611 | int vpos, hpos, i; | |
612 | int delta_ns, duration_ns; | |
613 | ||
614 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) | |
615 | return false; | |
616 | ||
617 | crtc = drm_crtc_from_index(dev, pipe); | |
618 | ||
619 | if (pipe >= dev->num_crtcs || !crtc) { | |
620 | DRM_ERROR("Invalid crtc %u\n", pipe); | |
621 | return false; | |
622 | } | |
623 | ||
624 | /* Scanout position query not supported? Should not happen. */ | |
625 | if (!dev->driver->get_scanout_position) { | |
626 | DRM_ERROR("Called from driver w/o get_scanout_position()!?\n"); | |
627 | return false; | |
628 | } | |
629 | ||
630 | if (drm_drv_uses_atomic_modeset(dev)) | |
631 | mode = &vblank->hwmode; | |
632 | else | |
633 | mode = &crtc->hwmode; | |
634 | ||
635 | /* If mode timing undefined, just return as no-op: | |
636 | * Happens during initial modesetting of a crtc. | |
637 | */ | |
638 | if (mode->crtc_clock == 0) { | |
639 | DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe); | |
640 | WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev)); | |
641 | ||
642 | return false; | |
643 | } | |
644 | ||
645 | /* Get current scanout position with system timestamp. | |
646 | * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times | |
647 | * if single query takes longer than max_error nanoseconds. | |
648 | * | |
649 | * This guarantees a tight bound on maximum error if | |
650 | * code gets preempted or delayed for some reason. | |
651 | */ | |
652 | for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) { | |
653 | /* | |
654 | * Get vertical and horizontal scanout position vpos, hpos, | |
655 | * and bounding timestamps stime, etime, pre/post query. | |
656 | */ | |
657 | vbl_status = dev->driver->get_scanout_position(dev, pipe, | |
658 | in_vblank_irq, | |
659 | &vpos, &hpos, | |
660 | &stime, &etime, | |
661 | mode); | |
662 | ||
663 | /* Return as no-op if scanout query unsupported or failed. */ | |
664 | if (!vbl_status) { | |
665 | DRM_DEBUG("crtc %u : scanoutpos query failed.\n", | |
666 | pipe); | |
667 | return false; | |
668 | } | |
669 | ||
670 | /* Compute uncertainty in timestamp of scanout position query. */ | |
671 | duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime); | |
672 | ||
673 | /* Accept result with < max_error nsecs timing uncertainty. */ | |
674 | if (duration_ns <= *max_error) | |
675 | break; | |
676 | } | |
677 | ||
678 | /* Noisy system timing? */ | |
679 | if (i == DRM_TIMESTAMP_MAXRETRIES) { | |
680 | DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n", | |
681 | pipe, duration_ns/1000, *max_error/1000, i); | |
682 | } | |
683 | ||
684 | /* Return upper bound of timestamp precision error. */ | |
685 | *max_error = duration_ns; | |
686 | ||
687 | /* Convert scanout position into elapsed time at raw_time query | |
688 | * since start of scanout at first display scanline. delta_ns | |
689 | * can be negative if start of scanout hasn't happened yet. | |
690 | */ | |
691 | delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos), | |
692 | mode->crtc_clock); | |
693 | ||
694 | if (!drm_timestamp_monotonic) | |
695 | etime = ktime_mono_to_real(etime); | |
696 | ||
697 | /* save this only for debugging purposes */ | |
698 | tv_etime = ktime_to_timeval(etime); | |
699 | /* Subtract time delta from raw timestamp to get final | |
700 | * vblank_time timestamp for end of vblank. | |
701 | */ | |
702 | etime = ktime_sub_ns(etime, delta_ns); | |
703 | *vblank_time = ktime_to_timeval(etime); | |
704 | ||
705 | DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n", | |
706 | pipe, hpos, vpos, | |
707 | (long)tv_etime.tv_sec, (long)tv_etime.tv_usec, | |
708 | (long)vblank_time->tv_sec, (long)vblank_time->tv_usec, | |
709 | duration_ns/1000, i); | |
710 | ||
711 | return true; | |
712 | } | |
713 | EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos); | |
714 | ||
715 | static struct timeval get_drm_timestamp(void) | |
716 | { | |
717 | ktime_t now; | |
718 | ||
719 | now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real(); | |
720 | return ktime_to_timeval(now); | |
721 | } | |
722 | ||
723 | /** | |
724 | * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent | |
725 | * vblank interval | |
726 | * @dev: DRM device | |
727 | * @pipe: index of CRTC whose vblank timestamp to retrieve | |
728 | * @tvblank: Pointer to target struct timeval which should receive the timestamp | |
729 | * @in_vblank_irq: | |
730 | * True when called from drm_crtc_handle_vblank(). Some drivers | |
731 | * need to apply some workarounds for gpu-specific vblank irq quirks | |
732 | * if flag is set. | |
733 | * | |
734 | * Fetches the system timestamp corresponding to the time of the most recent | |
735 | * vblank interval on specified CRTC. May call into kms-driver to | |
736 | * compute the timestamp with a high-precision GPU specific method. | |
737 | * | |
738 | * Returns zero if timestamp originates from uncorrected do_gettimeofday() | |
739 | * call, i.e., it isn't very precisely locked to the true vblank. | |
740 | * | |
741 | * Returns: | |
742 | * True if timestamp is considered to be very precise, false otherwise. | |
743 | */ | |
744 | static bool | |
745 | drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe, | |
746 | struct timeval *tvblank, bool in_vblank_irq) | |
747 | { | |
748 | bool ret = false; | |
749 | ||
750 | /* Define requested maximum error on timestamps (nanoseconds). */ | |
751 | int max_error = (int) drm_timestamp_precision * 1000; | |
752 | ||
753 | /* Query driver if possible and precision timestamping enabled. */ | |
754 | if (dev->driver->get_vblank_timestamp && (max_error > 0)) | |
755 | ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error, | |
756 | tvblank, in_vblank_irq); | |
757 | ||
758 | /* GPU high precision timestamp query unsupported or failed. | |
759 | * Return current monotonic/gettimeofday timestamp as best estimate. | |
760 | */ | |
761 | if (!ret) | |
762 | *tvblank = get_drm_timestamp(); | |
763 | ||
764 | return ret; | |
765 | } | |
766 | ||
767 | /** | |
768 | * drm_crtc_vblank_count - retrieve "cooked" vblank counter value | |
769 | * @crtc: which counter to retrieve | |
770 | * | |
771 | * Fetches the "cooked" vblank count value that represents the number of | |
772 | * vblank events since the system was booted, including lost events due to | |
57d30230 DV |
773 | * modesetting activity. Note that this timer isn't correct against a racing |
774 | * vblank interrupt (since it only reports the software vblank counter), see | |
775 | * drm_accurate_vblank_count() for such use-cases. | |
3ed4351a DV |
776 | * |
777 | * Returns: | |
778 | * The software vblank counter. | |
779 | */ | |
780 | u32 drm_crtc_vblank_count(struct drm_crtc *crtc) | |
781 | { | |
782 | return drm_vblank_count(crtc->dev, drm_crtc_index(crtc)); | |
783 | } | |
784 | EXPORT_SYMBOL(drm_crtc_vblank_count); | |
785 | ||
3ed4351a DV |
786 | static u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe, |
787 | struct timeval *vblanktime) | |
788 | { | |
789 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
790 | u32 vblank_count; | |
791 | unsigned int seq; | |
792 | ||
793 | if (WARN_ON(pipe >= dev->num_crtcs)) { | |
794 | *vblanktime = (struct timeval) { 0 }; | |
795 | return 0; | |
796 | } | |
797 | ||
798 | do { | |
799 | seq = read_seqbegin(&vblank->seqlock); | |
800 | vblank_count = vblank->count; | |
801 | *vblanktime = vblank->time; | |
802 | } while (read_seqretry(&vblank->seqlock, seq)); | |
803 | ||
804 | return vblank_count; | |
805 | } | |
806 | ||
807 | /** | |
808 | * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value | |
809 | * and the system timestamp corresponding to that vblank counter value | |
810 | * @crtc: which counter to retrieve | |
811 | * @vblanktime: Pointer to struct timeval to receive the vblank timestamp. | |
812 | * | |
813 | * Fetches the "cooked" vblank count value that represents the number of | |
814 | * vblank events since the system was booted, including lost events due to | |
815 | * modesetting activity. Returns corresponding system timestamp of the time | |
816 | * of the vblank interval that corresponds to the current vblank counter value. | |
817 | */ | |
818 | u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, | |
819 | struct timeval *vblanktime) | |
820 | { | |
821 | return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc), | |
822 | vblanktime); | |
823 | } | |
824 | EXPORT_SYMBOL(drm_crtc_vblank_count_and_time); | |
825 | ||
826 | static void send_vblank_event(struct drm_device *dev, | |
827 | struct drm_pending_vblank_event *e, | |
828 | unsigned long seq, struct timeval *now) | |
829 | { | |
830 | e->event.sequence = seq; | |
831 | e->event.tv_sec = now->tv_sec; | |
832 | e->event.tv_usec = now->tv_usec; | |
833 | ||
834 | trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, | |
835 | e->event.sequence); | |
836 | ||
837 | drm_send_event_locked(dev, &e->base); | |
838 | } | |
839 | ||
840 | /** | |
841 | * drm_crtc_arm_vblank_event - arm vblank event after pageflip | |
842 | * @crtc: the source CRTC of the vblank event | |
843 | * @e: the event to send | |
844 | * | |
845 | * A lot of drivers need to generate vblank events for the very next vblank | |
846 | * interrupt. For example when the page flip interrupt happens when the page | |
847 | * flip gets armed, but not when it actually executes within the next vblank | |
848 | * period. This helper function implements exactly the required vblank arming | |
849 | * behaviour. | |
850 | * | |
851 | * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an | |
852 | * atomic commit must ensure that the next vblank happens at exactly the same | |
853 | * time as the atomic commit is committed to the hardware. This function itself | |
854 | * does **not** protect again the next vblank interrupt racing with either this | |
855 | * function call or the atomic commit operation. A possible sequence could be: | |
856 | * | |
857 | * 1. Driver commits new hardware state into vblank-synchronized registers. | |
858 | * 2. A vblank happens, committing the hardware state. Also the corresponding | |
859 | * vblank interrupt is fired off and fully processed by the interrupt | |
860 | * handler. | |
861 | * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event(). | |
862 | * 4. The event is only send out for the next vblank, which is wrong. | |
863 | * | |
864 | * An equivalent race can happen when the driver calls | |
865 | * drm_crtc_arm_vblank_event() before writing out the new hardware state. | |
866 | * | |
867 | * The only way to make this work safely is to prevent the vblank from firing | |
868 | * (and the hardware from committing anything else) until the entire atomic | |
869 | * commit sequence has run to completion. If the hardware does not have such a | |
870 | * feature (e.g. using a "go" bit), then it is unsafe to use this functions. | |
871 | * Instead drivers need to manually send out the event from their interrupt | |
872 | * handler by calling drm_crtc_send_vblank_event() and make sure that there's no | |
873 | * possible race with the hardware committing the atomic update. | |
874 | * | |
57d30230 DV |
875 | * Caller must hold a vblank reference for the event @e, which will be dropped |
876 | * when the next vblank arrives. | |
3ed4351a DV |
877 | */ |
878 | void drm_crtc_arm_vblank_event(struct drm_crtc *crtc, | |
879 | struct drm_pending_vblank_event *e) | |
880 | { | |
881 | struct drm_device *dev = crtc->dev; | |
882 | unsigned int pipe = drm_crtc_index(crtc); | |
883 | ||
884 | assert_spin_locked(&dev->event_lock); | |
885 | ||
886 | e->pipe = pipe; | |
887 | e->event.sequence = drm_vblank_count(dev, pipe); | |
888 | e->event.crtc_id = crtc->base.id; | |
889 | list_add_tail(&e->base.link, &dev->vblank_event_list); | |
890 | } | |
891 | EXPORT_SYMBOL(drm_crtc_arm_vblank_event); | |
892 | ||
893 | /** | |
894 | * drm_crtc_send_vblank_event - helper to send vblank event after pageflip | |
895 | * @crtc: the source CRTC of the vblank event | |
896 | * @e: the event to send | |
897 | * | |
898 | * Updates sequence # and timestamp on event for the most recently processed | |
899 | * vblank, and sends it to userspace. Caller must hold event lock. | |
900 | * | |
901 | * See drm_crtc_arm_vblank_event() for a helper which can be used in certain | |
902 | * situation, especially to send out events for atomic commit operations. | |
903 | */ | |
904 | void drm_crtc_send_vblank_event(struct drm_crtc *crtc, | |
905 | struct drm_pending_vblank_event *e) | |
906 | { | |
907 | struct drm_device *dev = crtc->dev; | |
908 | unsigned int seq, pipe = drm_crtc_index(crtc); | |
909 | struct timeval now; | |
910 | ||
911 | if (dev->num_crtcs > 0) { | |
912 | seq = drm_vblank_count_and_time(dev, pipe, &now); | |
913 | } else { | |
914 | seq = 0; | |
915 | ||
916 | now = get_drm_timestamp(); | |
917 | } | |
918 | e->pipe = pipe; | |
919 | e->event.crtc_id = crtc->base.id; | |
920 | send_vblank_event(dev, e, seq, &now); | |
921 | } | |
922 | EXPORT_SYMBOL(drm_crtc_send_vblank_event); | |
923 | ||
924 | static int __enable_vblank(struct drm_device *dev, unsigned int pipe) | |
925 | { | |
926 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { | |
927 | struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe); | |
928 | ||
929 | if (crtc->funcs->enable_vblank) | |
930 | return crtc->funcs->enable_vblank(crtc); | |
931 | } | |
932 | ||
933 | return dev->driver->enable_vblank(dev, pipe); | |
934 | } | |
935 | ||
3ed4351a DV |
936 | static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe) |
937 | { | |
938 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
939 | int ret = 0; | |
940 | ||
941 | assert_spin_locked(&dev->vbl_lock); | |
942 | ||
943 | spin_lock(&dev->vblank_time_lock); | |
944 | ||
945 | if (!vblank->enabled) { | |
946 | /* | |
947 | * Enable vblank irqs under vblank_time_lock protection. | |
948 | * All vblank count & timestamp updates are held off | |
949 | * until we are done reinitializing master counter and | |
950 | * timestamps. Filtercode in drm_handle_vblank() will | |
951 | * prevent double-accounting of same vblank interval. | |
952 | */ | |
953 | ret = __enable_vblank(dev, pipe); | |
954 | DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret); | |
955 | if (ret) { | |
956 | atomic_dec(&vblank->refcount); | |
957 | } else { | |
958 | drm_update_vblank_count(dev, pipe, 0); | |
959 | /* drm_update_vblank_count() includes a wmb so we just | |
960 | * need to ensure that the compiler emits the write | |
961 | * to mark the vblank as enabled after the call | |
962 | * to drm_update_vblank_count(). | |
963 | */ | |
964 | WRITE_ONCE(vblank->enabled, true); | |
965 | } | |
966 | } | |
967 | ||
968 | spin_unlock(&dev->vblank_time_lock); | |
969 | ||
970 | return ret; | |
971 | } | |
972 | ||
3ed4351a DV |
973 | static int drm_vblank_get(struct drm_device *dev, unsigned int pipe) |
974 | { | |
975 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
976 | unsigned long irqflags; | |
977 | int ret = 0; | |
978 | ||
979 | if (!dev->num_crtcs) | |
980 | return -EINVAL; | |
981 | ||
982 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
983 | return -EINVAL; | |
984 | ||
985 | spin_lock_irqsave(&dev->vbl_lock, irqflags); | |
986 | /* Going from 0->1 means we have to enable interrupts again */ | |
987 | if (atomic_add_return(1, &vblank->refcount) == 1) { | |
988 | ret = drm_vblank_enable(dev, pipe); | |
989 | } else { | |
990 | if (!vblank->enabled) { | |
991 | atomic_dec(&vblank->refcount); | |
992 | ret = -EINVAL; | |
993 | } | |
994 | } | |
995 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); | |
996 | ||
997 | return ret; | |
998 | } | |
999 | ||
1000 | /** | |
1001 | * drm_crtc_vblank_get - get a reference count on vblank events | |
1002 | * @crtc: which CRTC to own | |
1003 | * | |
1004 | * Acquire a reference count on vblank events to avoid having them disabled | |
1005 | * while in use. | |
1006 | * | |
1007 | * Returns: | |
1008 | * Zero on success or a negative error code on failure. | |
1009 | */ | |
1010 | int drm_crtc_vblank_get(struct drm_crtc *crtc) | |
1011 | { | |
1012 | return drm_vblank_get(crtc->dev, drm_crtc_index(crtc)); | |
1013 | } | |
1014 | EXPORT_SYMBOL(drm_crtc_vblank_get); | |
1015 | ||
3ed4351a DV |
1016 | static void drm_vblank_put(struct drm_device *dev, unsigned int pipe) |
1017 | { | |
1018 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
1019 | ||
1020 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
1021 | return; | |
1022 | ||
1023 | if (WARN_ON(atomic_read(&vblank->refcount) == 0)) | |
1024 | return; | |
1025 | ||
1026 | /* Last user schedules interrupt disable */ | |
1027 | if (atomic_dec_and_test(&vblank->refcount)) { | |
1028 | if (drm_vblank_offdelay == 0) | |
1029 | return; | |
1030 | else if (drm_vblank_offdelay < 0) | |
1031 | vblank_disable_fn((unsigned long)vblank); | |
1032 | else if (!dev->vblank_disable_immediate) | |
1033 | mod_timer(&vblank->disable_timer, | |
1034 | jiffies + ((drm_vblank_offdelay * HZ)/1000)); | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | /** | |
1039 | * drm_crtc_vblank_put - give up ownership of vblank events | |
1040 | * @crtc: which counter to give up | |
1041 | * | |
1042 | * Release ownership of a given vblank counter, turning off interrupts | |
1043 | * if possible. Disable interrupts after drm_vblank_offdelay milliseconds. | |
1044 | */ | |
1045 | void drm_crtc_vblank_put(struct drm_crtc *crtc) | |
1046 | { | |
1047 | drm_vblank_put(crtc->dev, drm_crtc_index(crtc)); | |
1048 | } | |
1049 | EXPORT_SYMBOL(drm_crtc_vblank_put); | |
1050 | ||
1051 | /** | |
1052 | * drm_wait_one_vblank - wait for one vblank | |
1053 | * @dev: DRM device | |
1054 | * @pipe: CRTC index | |
1055 | * | |
1056 | * This waits for one vblank to pass on @pipe, using the irq driver interfaces. | |
1057 | * It is a failure to call this when the vblank irq for @pipe is disabled, e.g. | |
1058 | * due to lack of driver support or because the crtc is off. | |
57d30230 DV |
1059 | * |
1060 | * This is the legacy version of drm_crtc_wait_one_vblank(). | |
3ed4351a DV |
1061 | */ |
1062 | void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe) | |
1063 | { | |
1064 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
1065 | int ret; | |
1066 | u32 last; | |
1067 | ||
1068 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
1069 | return; | |
1070 | ||
1071 | ret = drm_vblank_get(dev, pipe); | |
1072 | if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret)) | |
1073 | return; | |
1074 | ||
1075 | last = drm_vblank_count(dev, pipe); | |
1076 | ||
1077 | ret = wait_event_timeout(vblank->queue, | |
1078 | last != drm_vblank_count(dev, pipe), | |
1079 | msecs_to_jiffies(100)); | |
1080 | ||
1081 | WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe); | |
1082 | ||
1083 | drm_vblank_put(dev, pipe); | |
1084 | } | |
1085 | EXPORT_SYMBOL(drm_wait_one_vblank); | |
1086 | ||
1087 | /** | |
1088 | * drm_crtc_wait_one_vblank - wait for one vblank | |
1089 | * @crtc: DRM crtc | |
1090 | * | |
1091 | * This waits for one vblank to pass on @crtc, using the irq driver interfaces. | |
1092 | * It is a failure to call this when the vblank irq for @crtc is disabled, e.g. | |
1093 | * due to lack of driver support or because the crtc is off. | |
1094 | */ | |
1095 | void drm_crtc_wait_one_vblank(struct drm_crtc *crtc) | |
1096 | { | |
1097 | drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc)); | |
1098 | } | |
1099 | EXPORT_SYMBOL(drm_crtc_wait_one_vblank); | |
1100 | ||
1101 | /** | |
1102 | * drm_crtc_vblank_off - disable vblank events on a CRTC | |
1103 | * @crtc: CRTC in question | |
1104 | * | |
1105 | * Drivers can use this function to shut down the vblank interrupt handling when | |
1106 | * disabling a crtc. This function ensures that the latest vblank frame count is | |
1107 | * stored so that drm_vblank_on can restore it again. | |
1108 | * | |
1109 | * Drivers must use this function when the hardware vblank counter can get | |
57d30230 | 1110 | * reset, e.g. when suspending or disabling the @crtc in general. |
3ed4351a DV |
1111 | */ |
1112 | void drm_crtc_vblank_off(struct drm_crtc *crtc) | |
1113 | { | |
1114 | struct drm_device *dev = crtc->dev; | |
1115 | unsigned int pipe = drm_crtc_index(crtc); | |
1116 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
1117 | struct drm_pending_vblank_event *e, *t; | |
1118 | struct timeval now; | |
1119 | unsigned long irqflags; | |
1120 | unsigned int seq; | |
1121 | ||
1122 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
1123 | return; | |
1124 | ||
1125 | spin_lock_irqsave(&dev->event_lock, irqflags); | |
1126 | ||
1127 | spin_lock(&dev->vbl_lock); | |
1128 | DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n", | |
1129 | pipe, vblank->enabled, vblank->inmodeset); | |
1130 | ||
1131 | /* Avoid redundant vblank disables without previous | |
1132 | * drm_crtc_vblank_on(). */ | |
1133 | if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset) | |
1134 | drm_vblank_disable_and_save(dev, pipe); | |
1135 | ||
1136 | wake_up(&vblank->queue); | |
1137 | ||
1138 | /* | |
1139 | * Prevent subsequent drm_vblank_get() from re-enabling | |
1140 | * the vblank interrupt by bumping the refcount. | |
1141 | */ | |
1142 | if (!vblank->inmodeset) { | |
1143 | atomic_inc(&vblank->refcount); | |
1144 | vblank->inmodeset = 1; | |
1145 | } | |
1146 | spin_unlock(&dev->vbl_lock); | |
1147 | ||
1148 | /* Send any queued vblank events, lest the natives grow disquiet */ | |
1149 | seq = drm_vblank_count_and_time(dev, pipe, &now); | |
1150 | ||
1151 | list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { | |
1152 | if (e->pipe != pipe) | |
1153 | continue; | |
1154 | DRM_DEBUG("Sending premature vblank event on disable: " | |
1155 | "wanted %u, current %u\n", | |
1156 | e->event.sequence, seq); | |
1157 | list_del(&e->base.link); | |
1158 | drm_vblank_put(dev, pipe); | |
1159 | send_vblank_event(dev, e, seq, &now); | |
1160 | } | |
1161 | spin_unlock_irqrestore(&dev->event_lock, irqflags); | |
1162 | ||
1163 | /* Will be reset by the modeset helpers when re-enabling the crtc by | |
1164 | * calling drm_calc_timestamping_constants(). */ | |
1165 | vblank->hwmode.crtc_clock = 0; | |
1166 | } | |
1167 | EXPORT_SYMBOL(drm_crtc_vblank_off); | |
1168 | ||
1169 | /** | |
1170 | * drm_crtc_vblank_reset - reset vblank state to off on a CRTC | |
1171 | * @crtc: CRTC in question | |
1172 | * | |
1173 | * Drivers can use this function to reset the vblank state to off at load time. | |
1174 | * Drivers should use this together with the drm_crtc_vblank_off() and | |
1175 | * drm_crtc_vblank_on() functions. The difference compared to | |
1176 | * drm_crtc_vblank_off() is that this function doesn't save the vblank counter | |
1177 | * and hence doesn't need to call any driver hooks. | |
57d30230 DV |
1178 | * |
1179 | * This is useful for recovering driver state e.g. on driver load, or on resume. | |
3ed4351a DV |
1180 | */ |
1181 | void drm_crtc_vblank_reset(struct drm_crtc *crtc) | |
1182 | { | |
1183 | struct drm_device *dev = crtc->dev; | |
1184 | unsigned long irqflags; | |
1185 | unsigned int pipe = drm_crtc_index(crtc); | |
1186 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
1187 | ||
1188 | spin_lock_irqsave(&dev->vbl_lock, irqflags); | |
1189 | /* | |
1190 | * Prevent subsequent drm_vblank_get() from enabling the vblank | |
1191 | * interrupt by bumping the refcount. | |
1192 | */ | |
1193 | if (!vblank->inmodeset) { | |
1194 | atomic_inc(&vblank->refcount); | |
1195 | vblank->inmodeset = 1; | |
1196 | } | |
1197 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); | |
1198 | ||
1199 | WARN_ON(!list_empty(&dev->vblank_event_list)); | |
1200 | } | |
1201 | EXPORT_SYMBOL(drm_crtc_vblank_reset); | |
1202 | ||
1203 | /** | |
1204 | * drm_crtc_vblank_on - enable vblank events on a CRTC | |
1205 | * @crtc: CRTC in question | |
1206 | * | |
1207 | * This functions restores the vblank interrupt state captured with | |
57d30230 DV |
1208 | * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note |
1209 | * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be | |
1210 | * unbalanced and so can also be unconditionally called in driver load code to | |
1211 | * reflect the current hardware state of the crtc. | |
3ed4351a DV |
1212 | */ |
1213 | void drm_crtc_vblank_on(struct drm_crtc *crtc) | |
1214 | { | |
1215 | struct drm_device *dev = crtc->dev; | |
1216 | unsigned int pipe = drm_crtc_index(crtc); | |
1217 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
1218 | unsigned long irqflags; | |
1219 | ||
1220 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
1221 | return; | |
1222 | ||
1223 | spin_lock_irqsave(&dev->vbl_lock, irqflags); | |
1224 | DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n", | |
1225 | pipe, vblank->enabled, vblank->inmodeset); | |
1226 | ||
1227 | /* Drop our private "prevent drm_vblank_get" refcount */ | |
1228 | if (vblank->inmodeset) { | |
1229 | atomic_dec(&vblank->refcount); | |
1230 | vblank->inmodeset = 0; | |
1231 | } | |
1232 | ||
1233 | drm_reset_vblank_timestamp(dev, pipe); | |
1234 | ||
1235 | /* | |
1236 | * re-enable interrupts if there are users left, or the | |
1237 | * user wishes vblank interrupts to be enabled all the time. | |
1238 | */ | |
1239 | if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0) | |
1240 | WARN_ON(drm_vblank_enable(dev, pipe)); | |
1241 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); | |
1242 | } | |
1243 | EXPORT_SYMBOL(drm_crtc_vblank_on); | |
1244 | ||
1245 | static void drm_legacy_vblank_pre_modeset(struct drm_device *dev, | |
1246 | unsigned int pipe) | |
1247 | { | |
1248 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
1249 | ||
1250 | /* vblank is not initialized (IRQ not installed ?), or has been freed */ | |
1251 | if (!dev->num_crtcs) | |
1252 | return; | |
1253 | ||
1254 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
1255 | return; | |
1256 | ||
1257 | /* | |
1258 | * To avoid all the problems that might happen if interrupts | |
1259 | * were enabled/disabled around or between these calls, we just | |
1260 | * have the kernel take a reference on the CRTC (just once though | |
1261 | * to avoid corrupting the count if multiple, mismatch calls occur), | |
1262 | * so that interrupts remain enabled in the interim. | |
1263 | */ | |
1264 | if (!vblank->inmodeset) { | |
1265 | vblank->inmodeset = 0x1; | |
1266 | if (drm_vblank_get(dev, pipe) == 0) | |
1267 | vblank->inmodeset |= 0x2; | |
1268 | } | |
1269 | } | |
1270 | ||
1271 | static void drm_legacy_vblank_post_modeset(struct drm_device *dev, | |
1272 | unsigned int pipe) | |
1273 | { | |
1274 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
1275 | unsigned long irqflags; | |
1276 | ||
1277 | /* vblank is not initialized (IRQ not installed ?), or has been freed */ | |
1278 | if (!dev->num_crtcs) | |
1279 | return; | |
1280 | ||
1281 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
1282 | return; | |
1283 | ||
1284 | if (vblank->inmodeset) { | |
1285 | spin_lock_irqsave(&dev->vbl_lock, irqflags); | |
1286 | drm_reset_vblank_timestamp(dev, pipe); | |
1287 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); | |
1288 | ||
1289 | if (vblank->inmodeset & 0x2) | |
1290 | drm_vblank_put(dev, pipe); | |
1291 | ||
1292 | vblank->inmodeset = 0; | |
1293 | } | |
1294 | } | |
1295 | ||
1296 | int drm_legacy_modeset_ctl(struct drm_device *dev, void *data, | |
1297 | struct drm_file *file_priv) | |
1298 | { | |
1299 | struct drm_modeset_ctl *modeset = data; | |
1300 | unsigned int pipe; | |
1301 | ||
1302 | /* If drm_vblank_init() hasn't been called yet, just no-op */ | |
1303 | if (!dev->num_crtcs) | |
1304 | return 0; | |
1305 | ||
1306 | /* KMS drivers handle this internally */ | |
1307 | if (!drm_core_check_feature(dev, DRIVER_LEGACY)) | |
1308 | return 0; | |
1309 | ||
1310 | pipe = modeset->crtc; | |
1311 | if (pipe >= dev->num_crtcs) | |
1312 | return -EINVAL; | |
1313 | ||
1314 | switch (modeset->cmd) { | |
1315 | case _DRM_PRE_MODESET: | |
1316 | drm_legacy_vblank_pre_modeset(dev, pipe); | |
1317 | break; | |
1318 | case _DRM_POST_MODESET: | |
1319 | drm_legacy_vblank_post_modeset(dev, pipe); | |
1320 | break; | |
1321 | default: | |
1322 | return -EINVAL; | |
1323 | } | |
1324 | ||
1325 | return 0; | |
1326 | } | |
1327 | ||
1328 | static inline bool vblank_passed(u32 seq, u32 ref) | |
1329 | { | |
1330 | return (seq - ref) <= (1 << 23); | |
1331 | } | |
1332 | ||
1333 | static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe, | |
1334 | union drm_wait_vblank *vblwait, | |
1335 | struct drm_file *file_priv) | |
1336 | { | |
1337 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
1338 | struct drm_pending_vblank_event *e; | |
1339 | struct timeval now; | |
1340 | unsigned long flags; | |
1341 | unsigned int seq; | |
1342 | int ret; | |
1343 | ||
1344 | e = kzalloc(sizeof(*e), GFP_KERNEL); | |
1345 | if (e == NULL) { | |
1346 | ret = -ENOMEM; | |
1347 | goto err_put; | |
1348 | } | |
1349 | ||
1350 | e->pipe = pipe; | |
1351 | e->event.base.type = DRM_EVENT_VBLANK; | |
1352 | e->event.base.length = sizeof(e->event); | |
1353 | e->event.user_data = vblwait->request.signal; | |
1354 | ||
1355 | spin_lock_irqsave(&dev->event_lock, flags); | |
1356 | ||
1357 | /* | |
1358 | * drm_crtc_vblank_off() might have been called after we called | |
1359 | * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the | |
1360 | * vblank disable, so no need for further locking. The reference from | |
1361 | * drm_vblank_get() protects against vblank disable from another source. | |
1362 | */ | |
1363 | if (!READ_ONCE(vblank->enabled)) { | |
1364 | ret = -EINVAL; | |
1365 | goto err_unlock; | |
1366 | } | |
1367 | ||
1368 | ret = drm_event_reserve_init_locked(dev, file_priv, &e->base, | |
1369 | &e->event.base); | |
1370 | ||
1371 | if (ret) | |
1372 | goto err_unlock; | |
1373 | ||
1374 | seq = drm_vblank_count_and_time(dev, pipe, &now); | |
1375 | ||
1376 | DRM_DEBUG("event on vblank count %u, current %u, crtc %u\n", | |
1377 | vblwait->request.sequence, seq, pipe); | |
1378 | ||
1379 | trace_drm_vblank_event_queued(file_priv, pipe, | |
1380 | vblwait->request.sequence); | |
1381 | ||
1382 | e->event.sequence = vblwait->request.sequence; | |
1383 | if (vblank_passed(seq, vblwait->request.sequence)) { | |
1384 | drm_vblank_put(dev, pipe); | |
1385 | send_vblank_event(dev, e, seq, &now); | |
1386 | vblwait->reply.sequence = seq; | |
1387 | } else { | |
1388 | /* drm_handle_vblank_events will call drm_vblank_put */ | |
1389 | list_add_tail(&e->base.link, &dev->vblank_event_list); | |
1390 | vblwait->reply.sequence = vblwait->request.sequence; | |
1391 | } | |
1392 | ||
1393 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
1394 | ||
1395 | return 0; | |
1396 | ||
1397 | err_unlock: | |
1398 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
1399 | kfree(e); | |
1400 | err_put: | |
1401 | drm_vblank_put(dev, pipe); | |
1402 | return ret; | |
1403 | } | |
1404 | ||
1405 | static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait) | |
1406 | { | |
1407 | if (vblwait->request.sequence) | |
1408 | return false; | |
1409 | ||
1410 | return _DRM_VBLANK_RELATIVE == | |
1411 | (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK | | |
1412 | _DRM_VBLANK_EVENT | | |
1413 | _DRM_VBLANK_NEXTONMISS)); | |
1414 | } | |
1415 | ||
1416 | /* | |
1417 | * Wait for VBLANK. | |
1418 | * | |
1419 | * \param inode device inode. | |
1420 | * \param file_priv DRM file private. | |
1421 | * \param cmd command. | |
1422 | * \param data user argument, pointing to a drm_wait_vblank structure. | |
1423 | * \return zero on success or a negative number on failure. | |
1424 | * | |
1425 | * This function enables the vblank interrupt on the pipe requested, then | |
1426 | * sleeps waiting for the requested sequence number to occur, and drops | |
1427 | * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that | |
1428 | * after a timeout with no further vblank waits scheduled). | |
1429 | */ | |
1430 | int drm_wait_vblank(struct drm_device *dev, void *data, | |
1431 | struct drm_file *file_priv) | |
1432 | { | |
1433 | struct drm_vblank_crtc *vblank; | |
1434 | union drm_wait_vblank *vblwait = data; | |
1435 | int ret; | |
1436 | unsigned int flags, seq, pipe, high_pipe; | |
1437 | ||
1438 | if (!dev->irq_enabled) | |
1439 | return -EINVAL; | |
1440 | ||
1441 | if (vblwait->request.type & _DRM_VBLANK_SIGNAL) | |
1442 | return -EINVAL; | |
1443 | ||
1444 | if (vblwait->request.type & | |
1445 | ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | | |
1446 | _DRM_VBLANK_HIGH_CRTC_MASK)) { | |
1447 | DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n", | |
1448 | vblwait->request.type, | |
1449 | (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | | |
1450 | _DRM_VBLANK_HIGH_CRTC_MASK)); | |
1451 | return -EINVAL; | |
1452 | } | |
1453 | ||
1454 | flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; | |
1455 | high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK); | |
1456 | if (high_pipe) | |
1457 | pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT; | |
1458 | else | |
1459 | pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0; | |
1460 | if (pipe >= dev->num_crtcs) | |
1461 | return -EINVAL; | |
1462 | ||
1463 | vblank = &dev->vblank[pipe]; | |
1464 | ||
1465 | /* If the counter is currently enabled and accurate, short-circuit | |
1466 | * queries to return the cached timestamp of the last vblank. | |
1467 | */ | |
1468 | if (dev->vblank_disable_immediate && | |
1469 | drm_wait_vblank_is_query(vblwait) && | |
1470 | READ_ONCE(vblank->enabled)) { | |
1471 | struct timeval now; | |
1472 | ||
1473 | vblwait->reply.sequence = | |
1474 | drm_vblank_count_and_time(dev, pipe, &now); | |
1475 | vblwait->reply.tval_sec = now.tv_sec; | |
1476 | vblwait->reply.tval_usec = now.tv_usec; | |
1477 | return 0; | |
1478 | } | |
1479 | ||
1480 | ret = drm_vblank_get(dev, pipe); | |
1481 | if (ret) { | |
1482 | DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret); | |
1483 | return ret; | |
1484 | } | |
1485 | seq = drm_vblank_count(dev, pipe); | |
1486 | ||
1487 | switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) { | |
1488 | case _DRM_VBLANK_RELATIVE: | |
1489 | vblwait->request.sequence += seq; | |
1490 | vblwait->request.type &= ~_DRM_VBLANK_RELATIVE; | |
1491 | case _DRM_VBLANK_ABSOLUTE: | |
1492 | break; | |
1493 | default: | |
1494 | ret = -EINVAL; | |
1495 | goto done; | |
1496 | } | |
1497 | ||
1498 | if ((flags & _DRM_VBLANK_NEXTONMISS) && | |
1499 | vblank_passed(seq, vblwait->request.sequence)) | |
1500 | vblwait->request.sequence = seq + 1; | |
1501 | ||
1502 | if (flags & _DRM_VBLANK_EVENT) { | |
1503 | /* must hold on to the vblank ref until the event fires | |
1504 | * drm_vblank_put will be called asynchronously | |
1505 | */ | |
1506 | return drm_queue_vblank_event(dev, pipe, vblwait, file_priv); | |
1507 | } | |
1508 | ||
1509 | if (vblwait->request.sequence != seq) { | |
1510 | DRM_DEBUG("waiting on vblank count %u, crtc %u\n", | |
1511 | vblwait->request.sequence, pipe); | |
1512 | DRM_WAIT_ON(ret, vblank->queue, 3 * HZ, | |
1513 | vblank_passed(drm_vblank_count(dev, pipe), | |
1514 | vblwait->request.sequence) || | |
1515 | !READ_ONCE(vblank->enabled)); | |
1516 | } | |
1517 | ||
1518 | if (ret != -EINTR) { | |
1519 | struct timeval now; | |
1520 | ||
1521 | vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now); | |
1522 | vblwait->reply.tval_sec = now.tv_sec; | |
1523 | vblwait->reply.tval_usec = now.tv_usec; | |
1524 | ||
1525 | DRM_DEBUG("crtc %d returning %u to client\n", | |
1526 | pipe, vblwait->reply.sequence); | |
1527 | } else { | |
1528 | DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe); | |
1529 | } | |
1530 | ||
1531 | done: | |
1532 | drm_vblank_put(dev, pipe); | |
1533 | return ret; | |
1534 | } | |
1535 | ||
1536 | static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe) | |
1537 | { | |
1538 | struct drm_pending_vblank_event *e, *t; | |
1539 | struct timeval now; | |
1540 | unsigned int seq; | |
1541 | ||
1542 | assert_spin_locked(&dev->event_lock); | |
1543 | ||
1544 | seq = drm_vblank_count_and_time(dev, pipe, &now); | |
1545 | ||
1546 | list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { | |
1547 | if (e->pipe != pipe) | |
1548 | continue; | |
1549 | if (!vblank_passed(seq, e->event.sequence)) | |
1550 | continue; | |
1551 | ||
1552 | DRM_DEBUG("vblank event on %u, current %u\n", | |
1553 | e->event.sequence, seq); | |
1554 | ||
1555 | list_del(&e->base.link); | |
1556 | drm_vblank_put(dev, pipe); | |
1557 | send_vblank_event(dev, e, seq, &now); | |
1558 | } | |
1559 | ||
1560 | trace_drm_vblank_event(pipe, seq); | |
1561 | } | |
1562 | ||
1563 | /** | |
1564 | * drm_handle_vblank - handle a vblank event | |
1565 | * @dev: DRM device | |
1566 | * @pipe: index of CRTC where this event occurred | |
1567 | * | |
1568 | * Drivers should call this routine in their vblank interrupt handlers to | |
1569 | * update the vblank counter and send any signals that may be pending. | |
1570 | * | |
1571 | * This is the legacy version of drm_crtc_handle_vblank(). | |
1572 | */ | |
1573 | bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe) | |
1574 | { | |
1575 | struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; | |
1576 | unsigned long irqflags; | |
1577 | bool disable_irq; | |
1578 | ||
1579 | if (WARN_ON_ONCE(!dev->num_crtcs)) | |
1580 | return false; | |
1581 | ||
1582 | if (WARN_ON(pipe >= dev->num_crtcs)) | |
1583 | return false; | |
1584 | ||
1585 | spin_lock_irqsave(&dev->event_lock, irqflags); | |
1586 | ||
1587 | /* Need timestamp lock to prevent concurrent execution with | |
1588 | * vblank enable/disable, as this would cause inconsistent | |
1589 | * or corrupted timestamps and vblank counts. | |
1590 | */ | |
1591 | spin_lock(&dev->vblank_time_lock); | |
1592 | ||
1593 | /* Vblank irq handling disabled. Nothing to do. */ | |
1594 | if (!vblank->enabled) { | |
1595 | spin_unlock(&dev->vblank_time_lock); | |
1596 | spin_unlock_irqrestore(&dev->event_lock, irqflags); | |
1597 | return false; | |
1598 | } | |
1599 | ||
1600 | drm_update_vblank_count(dev, pipe, true); | |
1601 | ||
1602 | spin_unlock(&dev->vblank_time_lock); | |
1603 | ||
1604 | wake_up(&vblank->queue); | |
1605 | ||
1606 | /* With instant-off, we defer disabling the interrupt until after | |
1607 | * we finish processing the following vblank after all events have | |
1608 | * been signaled. The disable has to be last (after | |
1609 | * drm_handle_vblank_events) so that the timestamp is always accurate. | |
1610 | */ | |
1611 | disable_irq = (dev->vblank_disable_immediate && | |
1612 | drm_vblank_offdelay > 0 && | |
1613 | !atomic_read(&vblank->refcount)); | |
1614 | ||
1615 | drm_handle_vblank_events(dev, pipe); | |
1616 | ||
1617 | spin_unlock_irqrestore(&dev->event_lock, irqflags); | |
1618 | ||
1619 | if (disable_irq) | |
1620 | vblank_disable_fn((unsigned long)vblank); | |
1621 | ||
1622 | return true; | |
1623 | } | |
1624 | EXPORT_SYMBOL(drm_handle_vblank); | |
1625 | ||
1626 | /** | |
1627 | * drm_crtc_handle_vblank - handle a vblank event | |
1628 | * @crtc: where this event occurred | |
1629 | * | |
1630 | * Drivers should call this routine in their vblank interrupt handlers to | |
1631 | * update the vblank counter and send any signals that may be pending. | |
1632 | * | |
1633 | * This is the native KMS version of drm_handle_vblank(). | |
1634 | * | |
1635 | * Returns: | |
1636 | * True if the event was successfully handled, false on failure. | |
1637 | */ | |
1638 | bool drm_crtc_handle_vblank(struct drm_crtc *crtc) | |
1639 | { | |
1640 | return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc)); | |
1641 | } | |
1642 | EXPORT_SYMBOL(drm_crtc_handle_vblank); |