drm_irq.c revision 1.10 1 1.9 riastrad /* $NetBSD: drm_irq.c,v 1.10 2018/08/27 06:53:36 riastradh Exp $ */
2 1.9 riastrad
3 1.9 riastrad /*
4 1.9 riastrad * drm_irq.c IRQ and vblank support
5 1.1 riastrad *
6 1.1 riastrad * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
7 1.1 riastrad * \author Gareth Hughes <gareth (at) valinux.com>
8 1.1 riastrad */
9 1.1 riastrad
10 1.1 riastrad /*
11 1.1 riastrad * Created: Fri Mar 19 14:30:16 1999 by faith (at) valinux.com
12 1.1 riastrad *
13 1.1 riastrad * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
14 1.1 riastrad * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 1.1 riastrad * All Rights Reserved.
16 1.1 riastrad *
17 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
18 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
19 1.1 riastrad * to deal in the Software without restriction, including without limitation
20 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
22 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
23 1.1 riastrad *
24 1.1 riastrad * The above copyright notice and this permission notice (including the next
25 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
26 1.1 riastrad * Software.
27 1.1 riastrad *
28 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 1.1 riastrad * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE.
35 1.1 riastrad */
36 1.1 riastrad
37 1.9 riastrad #include <sys/cdefs.h>
38 1.9 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_irq.c,v 1.10 2018/08/27 06:53:36 riastradh Exp $");
39 1.9 riastrad
40 1.1 riastrad #include <drm/drmP.h>
41 1.1 riastrad #include "drm_trace.h"
42 1.9 riastrad #include "drm_internal.h"
43 1.1 riastrad
44 1.1 riastrad #include <linux/interrupt.h> /* For task queue support */
45 1.1 riastrad #include <linux/slab.h>
46 1.1 riastrad
47 1.1 riastrad #include <linux/vgaarb.h>
48 1.1 riastrad #include <linux/export.h>
49 1.10 riastrad #include <linux/moduleparam.h>
50 1.1 riastrad
51 1.2 riastrad #include <linux/atomic.h>
52 1.2 riastrad #include <linux/ktime.h>
53 1.2 riastrad #include <linux/math64.h>
54 1.2 riastrad #include <linux/preempt.h>
55 1.2 riastrad #include <linux/sched.h>
56 1.2 riastrad
57 1.2 riastrad #include <asm/bug.h>
58 1.5 riastrad #include <asm/param.h>
59 1.2 riastrad
60 1.2 riastrad #ifdef __NetBSD__ /* XXX hurk -- selnotify &c. */
61 1.2 riastrad #include <sys/poll.h>
62 1.2 riastrad #include <sys/select.h>
63 1.2 riastrad #endif
64 1.2 riastrad
65 1.1 riastrad /* Access macro for slots in vblank timestamp ringbuffer. */
66 1.9 riastrad #define vblanktimestamp(dev, pipe, count) \
67 1.9 riastrad ((dev)->vblank[pipe].time[(count) % DRM_VBLANKTIME_RBSIZE])
68 1.1 riastrad
69 1.1 riastrad /* Retry timestamp calculation up to 3 times to satisfy
70 1.1 riastrad * drm_timestamp_precision before giving up.
71 1.1 riastrad */
72 1.1 riastrad #define DRM_TIMESTAMP_MAXRETRIES 3
73 1.1 riastrad
74 1.1 riastrad /* Threshold in nanoseconds for detection of redundant
75 1.1 riastrad * vblank irq in drm_handle_vblank(). 1 msec should be ok.
76 1.1 riastrad */
77 1.1 riastrad #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
78 1.1 riastrad
79 1.9 riastrad static bool
80 1.9 riastrad drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
81 1.9 riastrad struct timeval *tvblank, unsigned flags);
82 1.9 riastrad
83 1.9 riastrad static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
84 1.9 riastrad
85 1.9 riastrad /*
86 1.9 riastrad * Default to use monotonic timestamps for wait-for-vblank and page-flip
87 1.9 riastrad * complete events.
88 1.9 riastrad */
89 1.9 riastrad unsigned int drm_timestamp_monotonic = 1;
90 1.9 riastrad
91 1.9 riastrad static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
92 1.9 riastrad
93 1.9 riastrad module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
94 1.9 riastrad module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
95 1.9 riastrad module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
96 1.9 riastrad
97 1.9 riastrad static void store_vblank(struct drm_device *dev, unsigned int pipe,
98 1.9 riastrad u32 vblank_count_inc,
99 1.9 riastrad struct timeval *t_vblank, u32 last)
100 1.9 riastrad {
101 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
102 1.9 riastrad u32 tslot;
103 1.9 riastrad
104 1.9 riastrad assert_spin_locked(&dev->vblank_time_lock);
105 1.9 riastrad
106 1.9 riastrad vblank->last = last;
107 1.9 riastrad
108 1.9 riastrad /* All writers hold the spinlock, but readers are serialized by
109 1.9 riastrad * the latching of vblank->count below.
110 1.9 riastrad */
111 1.9 riastrad tslot = vblank->count + vblank_count_inc;
112 1.9 riastrad vblanktimestamp(dev, pipe, tslot) = *t_vblank;
113 1.9 riastrad
114 1.9 riastrad /*
115 1.9 riastrad * vblank timestamp updates are protected on the write side with
116 1.9 riastrad * vblank_time_lock, but on the read side done locklessly using a
117 1.9 riastrad * sequence-lock on the vblank counter. Ensure correct ordering using
118 1.9 riastrad * memory barrriers. We need the barrier both before and also after the
119 1.9 riastrad * counter update to synchronize with the next timestamp write.
120 1.9 riastrad * The read-side barriers for this are in drm_vblank_count_and_time.
121 1.9 riastrad */
122 1.9 riastrad smp_wmb();
123 1.9 riastrad vblank->count += vblank_count_inc;
124 1.9 riastrad smp_wmb();
125 1.9 riastrad }
126 1.9 riastrad
127 1.1 riastrad /**
128 1.9 riastrad * drm_reset_vblank_timestamp - reset the last timestamp to the last vblank
129 1.9 riastrad * @dev: DRM device
130 1.9 riastrad * @pipe: index of CRTC for which to reset the timestamp
131 1.1 riastrad *
132 1.9 riastrad * Reset the stored timestamp for the current vblank count to correspond
133 1.9 riastrad * to the last vblank occurred.
134 1.9 riastrad *
135 1.9 riastrad * Only to be called from drm_vblank_on().
136 1.1 riastrad *
137 1.9 riastrad * Note: caller must hold dev->vbl_lock since this reads & writes
138 1.9 riastrad * device vblank fields.
139 1.1 riastrad */
140 1.9 riastrad static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
141 1.1 riastrad {
142 1.9 riastrad u32 cur_vblank;
143 1.9 riastrad bool rc;
144 1.9 riastrad struct timeval t_vblank;
145 1.9 riastrad int count = DRM_TIMESTAMP_MAXRETRIES;
146 1.9 riastrad
147 1.9 riastrad spin_lock(&dev->vblank_time_lock);
148 1.9 riastrad
149 1.9 riastrad /*
150 1.9 riastrad * sample the current counter to avoid random jumps
151 1.9 riastrad * when drm_vblank_enable() applies the diff
152 1.9 riastrad */
153 1.9 riastrad do {
154 1.9 riastrad cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
155 1.9 riastrad rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, 0);
156 1.9 riastrad } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
157 1.1 riastrad
158 1.9 riastrad /*
159 1.9 riastrad * Only reinitialize corresponding vblank timestamp if high-precision query
160 1.9 riastrad * available and didn't fail. Otherwise reinitialize delayed at next vblank
161 1.9 riastrad * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
162 1.9 riastrad */
163 1.9 riastrad if (!rc)
164 1.9 riastrad t_vblank = (struct timeval) {0, 0};
165 1.1 riastrad
166 1.9 riastrad /*
167 1.9 riastrad * +1 to make sure user will never see the same
168 1.9 riastrad * vblank counter value before and after a modeset
169 1.9 riastrad */
170 1.9 riastrad store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
171 1.1 riastrad
172 1.9 riastrad spin_unlock(&dev->vblank_time_lock);
173 1.1 riastrad }
174 1.1 riastrad
175 1.9 riastrad /**
176 1.9 riastrad * drm_update_vblank_count - update the master vblank counter
177 1.9 riastrad * @dev: DRM device
178 1.9 riastrad * @pipe: counter to update
179 1.9 riastrad *
180 1.9 riastrad * Call back into the driver to update the appropriate vblank counter
181 1.9 riastrad * (specified by @pipe). Deal with wraparound, if it occurred, and
182 1.9 riastrad * update the last read value so we can deal with wraparound on the next
183 1.9 riastrad * call if necessary.
184 1.9 riastrad *
185 1.9 riastrad * Only necessary when going from off->on, to account for frames we
186 1.9 riastrad * didn't get an interrupt for.
187 1.9 riastrad *
188 1.9 riastrad * Note: caller must hold dev->vbl_lock since this reads & writes
189 1.9 riastrad * device vblank fields.
190 1.1 riastrad */
191 1.9 riastrad static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
192 1.9 riastrad unsigned long flags)
193 1.1 riastrad {
194 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
195 1.9 riastrad u32 cur_vblank, diff;
196 1.9 riastrad bool rc;
197 1.9 riastrad struct timeval t_vblank;
198 1.9 riastrad int count = DRM_TIMESTAMP_MAXRETRIES;
199 1.9 riastrad int framedur_ns = vblank->framedur_ns;
200 1.9 riastrad
201 1.9 riastrad /*
202 1.9 riastrad * Interrupts were disabled prior to this call, so deal with counter
203 1.9 riastrad * wrap if needed.
204 1.9 riastrad * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
205 1.9 riastrad * here if the register is small or we had vblank interrupts off for
206 1.9 riastrad * a long time.
207 1.9 riastrad *
208 1.9 riastrad * We repeat the hardware vblank counter & timestamp query until
209 1.9 riastrad * we get consistent results. This to prevent races between gpu
210 1.9 riastrad * updating its hardware counter while we are retrieving the
211 1.9 riastrad * corresponding vblank timestamp.
212 1.9 riastrad */
213 1.9 riastrad do {
214 1.9 riastrad cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
215 1.9 riastrad rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, flags);
216 1.9 riastrad } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
217 1.9 riastrad
218 1.9 riastrad if (dev->max_vblank_count != 0) {
219 1.9 riastrad /* trust the hw counter when it's around */
220 1.9 riastrad diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
221 1.9 riastrad } else if (rc && framedur_ns) {
222 1.9 riastrad const struct timeval *t_old;
223 1.9 riastrad u64 diff_ns;
224 1.9 riastrad
225 1.9 riastrad t_old = &vblanktimestamp(dev, pipe, vblank->count);
226 1.9 riastrad diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
227 1.9 riastrad
228 1.9 riastrad /*
229 1.9 riastrad * Figure out how many vblanks we've missed based
230 1.9 riastrad * on the difference in the timestamps and the
231 1.9 riastrad * frame/field duration.
232 1.9 riastrad */
233 1.9 riastrad diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
234 1.9 riastrad
235 1.9 riastrad if (diff == 0 && flags & DRM_CALLED_FROM_VBLIRQ)
236 1.9 riastrad DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
237 1.9 riastrad " diff_ns = %lld, framedur_ns = %d)\n",
238 1.9 riastrad pipe, (long long) diff_ns, framedur_ns);
239 1.9 riastrad } else {
240 1.9 riastrad /* some kind of default for drivers w/o accurate vbl timestamping */
241 1.9 riastrad diff = (flags & DRM_CALLED_FROM_VBLIRQ) != 0;
242 1.9 riastrad }
243 1.9 riastrad
244 1.9 riastrad /*
245 1.9 riastrad * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
246 1.9 riastrad * interval? If so then vblank irqs keep running and it will likely
247 1.9 riastrad * happen that the hardware vblank counter is not trustworthy as it
248 1.9 riastrad * might reset at some point in that interval and vblank timestamps
249 1.9 riastrad * are not trustworthy either in that interval. Iow. this can result
250 1.9 riastrad * in a bogus diff >> 1 which must be avoided as it would cause
251 1.9 riastrad * random large forward jumps of the software vblank counter.
252 1.9 riastrad */
253 1.9 riastrad if (diff > 1 && (vblank->inmodeset & 0x2)) {
254 1.9 riastrad DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
255 1.9 riastrad " due to pre-modeset.\n", pipe, diff);
256 1.9 riastrad diff = 1;
257 1.9 riastrad }
258 1.9 riastrad
259 1.9 riastrad /*
260 1.9 riastrad * FIMXE: Need to replace this hack with proper seqlocks.
261 1.9 riastrad *
262 1.9 riastrad * Restrict the bump of the software vblank counter to a safe maximum
263 1.9 riastrad * value of +1 whenever there is the possibility that concurrent readers
264 1.9 riastrad * of vblank timestamps could be active at the moment, as the current
265 1.9 riastrad * implementation of the timestamp caching and updating is not safe
266 1.9 riastrad * against concurrent readers for calls to store_vblank() with a bump
267 1.9 riastrad * of anything but +1. A bump != 1 would very likely return corrupted
268 1.9 riastrad * timestamps to userspace, because the same slot in the cache could
269 1.9 riastrad * be concurrently written by store_vblank() and read by one of those
270 1.9 riastrad * readers without the read-retry logic detecting the collision.
271 1.9 riastrad *
272 1.9 riastrad * Concurrent readers can exist when we are called from the
273 1.9 riastrad * drm_vblank_off() or drm_vblank_on() functions and other non-vblank-
274 1.9 riastrad * irq callers. However, all those calls to us are happening with the
275 1.9 riastrad * vbl_lock locked to prevent drm_vblank_get(), so the vblank refcount
276 1.9 riastrad * can't increase while we are executing. Therefore a zero refcount at
277 1.9 riastrad * this point is safe for arbitrary counter bumps if we are called
278 1.9 riastrad * outside vblank irq, a non-zero count is not 100% safe. Unfortunately
279 1.9 riastrad * we must also accept a refcount of 1, as whenever we are called from
280 1.9 riastrad * drm_vblank_get() -> drm_vblank_enable() the refcount will be 1 and
281 1.9 riastrad * we must let that one pass through in order to not lose vblank counts
282 1.9 riastrad * during vblank irq off - which would completely defeat the whole
283 1.9 riastrad * point of this routine.
284 1.9 riastrad *
285 1.9 riastrad * Whenever we are called from vblank irq, we have to assume concurrent
286 1.9 riastrad * readers exist or can show up any time during our execution, even if
287 1.9 riastrad * the refcount is currently zero, as vblank irqs are usually only
288 1.9 riastrad * enabled due to the presence of readers, and because when we are called
289 1.9 riastrad * from vblank irq we can't hold the vbl_lock to protect us from sudden
290 1.9 riastrad * bumps in vblank refcount. Therefore also restrict bumps to +1 when
291 1.9 riastrad * called from vblank irq.
292 1.9 riastrad */
293 1.9 riastrad if ((diff > 1) && (atomic_read(&vblank->refcount) > 1 ||
294 1.9 riastrad (flags & DRM_CALLED_FROM_VBLIRQ))) {
295 1.9 riastrad DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u "
296 1.9 riastrad "refcount %u, vblirq %u\n", pipe, diff,
297 1.9 riastrad atomic_read(&vblank->refcount),
298 1.9 riastrad (flags & DRM_CALLED_FROM_VBLIRQ) != 0);
299 1.9 riastrad diff = 1;
300 1.9 riastrad }
301 1.9 riastrad
302 1.9 riastrad DRM_DEBUG_VBL("updating vblank count on crtc %u:"
303 1.9 riastrad " current=%u, diff=%u, hw=%u hw_last=%u\n",
304 1.9 riastrad pipe, vblank->count, diff, cur_vblank, vblank->last);
305 1.9 riastrad
306 1.9 riastrad if (diff == 0) {
307 1.9 riastrad WARN_ON_ONCE(cur_vblank != vblank->last);
308 1.9 riastrad return;
309 1.9 riastrad }
310 1.9 riastrad
311 1.9 riastrad /*
312 1.9 riastrad * Only reinitialize corresponding vblank timestamp if high-precision query
313 1.9 riastrad * available and didn't fail, or we were called from the vblank interrupt.
314 1.9 riastrad * Otherwise reinitialize delayed at next vblank interrupt and assign 0
315 1.9 riastrad * for now, to mark the vblanktimestamp as invalid.
316 1.9 riastrad */
317 1.9 riastrad if (!rc && (flags & DRM_CALLED_FROM_VBLIRQ) == 0)
318 1.9 riastrad t_vblank = (struct timeval) {0, 0};
319 1.9 riastrad
320 1.9 riastrad store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
321 1.1 riastrad }
322 1.1 riastrad
323 1.1 riastrad /*
324 1.1 riastrad * Disable vblank irq's on crtc, make sure that last vblank count
325 1.1 riastrad * of hardware and corresponding consistent software vblank counter
326 1.1 riastrad * are preserved, even if there are any spurious vblank irq's after
327 1.1 riastrad * disable.
328 1.1 riastrad */
329 1.9 riastrad static void vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
330 1.1 riastrad {
331 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
332 1.1 riastrad unsigned long irqflags;
333 1.1 riastrad
334 1.1 riastrad /* Prevent vblank irq processing while disabling vblank irqs,
335 1.1 riastrad * so no updates of timestamps or count can happen after we've
336 1.1 riastrad * disabled. Needed to prevent races in case of delayed irq's.
337 1.1 riastrad */
338 1.1 riastrad spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
339 1.1 riastrad
340 1.9 riastrad /*
341 1.9 riastrad * Only disable vblank interrupts if they're enabled. This avoids
342 1.9 riastrad * calling the ->disable_vblank() operation in atomic context with the
343 1.9 riastrad * hardware potentially runtime suspended.
344 1.1 riastrad */
345 1.9 riastrad if (vblank->enabled) {
346 1.9 riastrad dev->driver->disable_vblank(dev, pipe);
347 1.9 riastrad vblank->enabled = false;
348 1.1 riastrad }
349 1.1 riastrad
350 1.9 riastrad /*
351 1.9 riastrad * Always update the count and timestamp to maintain the
352 1.9 riastrad * appearance that the counter has been ticking all along until
353 1.9 riastrad * this time. This makes the count account for the entire time
354 1.9 riastrad * between drm_vblank_on() and drm_vblank_off().
355 1.9 riastrad */
356 1.9 riastrad drm_update_vblank_count(dev, pipe, 0);
357 1.1 riastrad
358 1.1 riastrad spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
359 1.1 riastrad }
360 1.1 riastrad
361 1.1 riastrad static void vblank_disable_fn(unsigned long arg)
362 1.1 riastrad {
363 1.9 riastrad struct drm_vblank_crtc *vblank = (void *)arg;
364 1.9 riastrad struct drm_device *dev = vblank->dev;
365 1.9 riastrad unsigned int pipe = vblank->pipe;
366 1.1 riastrad unsigned long irqflags;
367 1.1 riastrad
368 1.1 riastrad if (!dev->vblank_disable_allowed)
369 1.1 riastrad return;
370 1.1 riastrad
371 1.9 riastrad spin_lock_irqsave(&dev->vbl_lock, irqflags);
372 1.9 riastrad if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
373 1.9 riastrad DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
374 1.9 riastrad vblank_disable_and_save(dev, pipe);
375 1.1 riastrad }
376 1.9 riastrad spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
377 1.1 riastrad }
378 1.1 riastrad
379 1.9 riastrad /**
380 1.9 riastrad * drm_vblank_cleanup - cleanup vblank support
381 1.9 riastrad * @dev: DRM device
382 1.9 riastrad *
383 1.9 riastrad * This function cleans up any resources allocated in drm_vblank_init.
384 1.9 riastrad */
385 1.1 riastrad void drm_vblank_cleanup(struct drm_device *dev)
386 1.1 riastrad {
387 1.9 riastrad unsigned int pipe;
388 1.9 riastrad
389 1.1 riastrad /* Bail if the driver didn't call drm_vblank_init() */
390 1.1 riastrad if (dev->num_crtcs == 0)
391 1.1 riastrad return;
392 1.1 riastrad
393 1.9 riastrad for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
394 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
395 1.9 riastrad
396 1.9 riastrad WARN_ON(vblank->enabled &&
397 1.9 riastrad drm_core_check_feature(dev, DRIVER_MODESET));
398 1.9 riastrad
399 1.9 riastrad del_timer_sync(&vblank->disable_timer);
400 1.3 riastrad #ifdef __NetBSD__
401 1.9 riastrad teardown_timer(&vblank->disable_timer);
402 1.3 riastrad #endif
403 1.9 riastrad }
404 1.1 riastrad
405 1.2 riastrad #ifdef __NetBSD__
406 1.2 riastrad {
407 1.2 riastrad unsigned int i;
408 1.2 riastrad for (i = 0; i < dev->num_crtcs; i++)
409 1.4 riastrad DRM_DESTROY_WAITQUEUE(&dev->vblank[i].queue);
410 1.2 riastrad }
411 1.2 riastrad #endif
412 1.2 riastrad
413 1.4 riastrad kfree(dev->vblank);
414 1.1 riastrad
415 1.1 riastrad dev->num_crtcs = 0;
416 1.2 riastrad
417 1.2 riastrad #ifdef __NetBSD__
418 1.2 riastrad spin_lock_destroy(&dev->vblank_time_lock);
419 1.2 riastrad spin_lock_destroy(&dev->vbl_lock);
420 1.2 riastrad #endif
421 1.1 riastrad }
422 1.1 riastrad EXPORT_SYMBOL(drm_vblank_cleanup);
423 1.1 riastrad
424 1.9 riastrad /**
425 1.9 riastrad * drm_vblank_init - initialize vblank support
426 1.9 riastrad * @dev: DRM device
427 1.9 riastrad * @num_crtcs: number of CRTCs supported by @dev
428 1.9 riastrad *
429 1.9 riastrad * This function initializes vblank support for @num_crtcs display pipelines.
430 1.9 riastrad *
431 1.9 riastrad * Returns:
432 1.9 riastrad * Zero on success or a negative error code on failure.
433 1.9 riastrad */
434 1.9 riastrad int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
435 1.1 riastrad {
436 1.9 riastrad int ret = -ENOMEM;
437 1.9 riastrad unsigned int i;
438 1.1 riastrad
439 1.1 riastrad spin_lock_init(&dev->vbl_lock);
440 1.1 riastrad spin_lock_init(&dev->vblank_time_lock);
441 1.1 riastrad
442 1.1 riastrad dev->num_crtcs = num_crtcs;
443 1.1 riastrad
444 1.4 riastrad dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
445 1.4 riastrad if (!dev->vblank)
446 1.4 riastrad goto err;
447 1.4 riastrad
448 1.9 riastrad for (i = 0; i < num_crtcs; i++) {
449 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[i];
450 1.9 riastrad
451 1.9 riastrad vblank->dev = dev;
452 1.9 riastrad vblank->pipe = i;
453 1.2 riastrad #ifdef __NetBSD__
454 1.9 riastrad DRM_INIT_WAITQUEUE(&vblank->queue, "drmvblnq");
455 1.2 riastrad #else
456 1.9 riastrad init_waitqueue_head(&vblank->queue);
457 1.2 riastrad #endif
458 1.9 riastrad setup_timer(&vblank->disable_timer, vblank_disable_fn,
459 1.9 riastrad (unsigned long)vblank);
460 1.9 riastrad }
461 1.1 riastrad
462 1.4 riastrad DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
463 1.1 riastrad
464 1.1 riastrad /* Driver specific high-precision vblank timestamping supported? */
465 1.1 riastrad if (dev->driver->get_vblank_timestamp)
466 1.1 riastrad DRM_INFO("Driver supports precise vblank timestamp query.\n");
467 1.1 riastrad else
468 1.1 riastrad DRM_INFO("No driver support for vblank timestamp query.\n");
469 1.1 riastrad
470 1.9 riastrad /* Must have precise timestamping for reliable vblank instant disable */
471 1.9 riastrad if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
472 1.9 riastrad dev->vblank_disable_immediate = false;
473 1.9 riastrad DRM_INFO("Setting vblank_disable_immediate to false because "
474 1.9 riastrad "get_vblank_timestamp == NULL\n");
475 1.9 riastrad }
476 1.9 riastrad
477 1.4 riastrad dev->vblank_disable_allowed = false;
478 1.1 riastrad
479 1.1 riastrad return 0;
480 1.1 riastrad
481 1.1 riastrad err:
482 1.9 riastrad dev->num_crtcs = 0;
483 1.1 riastrad return ret;
484 1.1 riastrad }
485 1.1 riastrad EXPORT_SYMBOL(drm_vblank_init);
486 1.1 riastrad
487 1.1 riastrad static void drm_irq_vgaarb_nokms(void *cookie, bool state)
488 1.1 riastrad {
489 1.1 riastrad struct drm_device *dev = cookie;
490 1.1 riastrad
491 1.1 riastrad if (dev->driver->vgaarb_irq) {
492 1.1 riastrad dev->driver->vgaarb_irq(dev, state);
493 1.1 riastrad return;
494 1.1 riastrad }
495 1.1 riastrad
496 1.1 riastrad if (!dev->irq_enabled)
497 1.1 riastrad return;
498 1.1 riastrad
499 1.1 riastrad if (state) {
500 1.1 riastrad if (dev->driver->irq_uninstall)
501 1.1 riastrad dev->driver->irq_uninstall(dev);
502 1.1 riastrad } else {
503 1.1 riastrad if (dev->driver->irq_preinstall)
504 1.1 riastrad dev->driver->irq_preinstall(dev);
505 1.1 riastrad if (dev->driver->irq_postinstall)
506 1.1 riastrad dev->driver->irq_postinstall(dev);
507 1.1 riastrad }
508 1.1 riastrad }
509 1.1 riastrad
510 1.1 riastrad /**
511 1.9 riastrad * drm_irq_install - install IRQ handler
512 1.9 riastrad * @dev: DRM device
513 1.9 riastrad * @irq: IRQ number to install the handler for
514 1.9 riastrad *
515 1.9 riastrad * Initializes the IRQ related data. Installs the handler, calling the driver
516 1.9 riastrad * irq_preinstall() and irq_postinstall() functions before and after the
517 1.9 riastrad * installation.
518 1.1 riastrad *
519 1.9 riastrad * This is the simplified helper interface provided for drivers with no special
520 1.9 riastrad * needs. Drivers which need to install interrupt handlers for multiple
521 1.9 riastrad * interrupts must instead set drm_device->irq_enabled to signal the DRM core
522 1.9 riastrad * that vblank interrupts are available.
523 1.1 riastrad *
524 1.9 riastrad * Returns:
525 1.9 riastrad * Zero on success or a negative error code on failure.
526 1.1 riastrad */
527 1.9 riastrad int drm_irq_install(struct drm_device *dev, int irq)
528 1.1 riastrad {
529 1.1 riastrad int ret;
530 1.1 riastrad unsigned long sh_flags = 0;
531 1.1 riastrad
532 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
533 1.1 riastrad return -EINVAL;
534 1.1 riastrad
535 1.9 riastrad if (irq == 0)
536 1.1 riastrad return -EINVAL;
537 1.1 riastrad
538 1.1 riastrad /* Driver must have been initialized */
539 1.9 riastrad if (!dev->dev_private)
540 1.1 riastrad return -EINVAL;
541 1.1 riastrad
542 1.9 riastrad if (dev->irq_enabled)
543 1.1 riastrad return -EBUSY;
544 1.4 riastrad dev->irq_enabled = true;
545 1.1 riastrad
546 1.9 riastrad DRM_DEBUG("irq=%d\n", irq);
547 1.1 riastrad
548 1.1 riastrad /* Before installing handler */
549 1.1 riastrad if (dev->driver->irq_preinstall)
550 1.1 riastrad dev->driver->irq_preinstall(dev);
551 1.1 riastrad
552 1.1 riastrad /* Install handler */
553 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
554 1.1 riastrad sh_flags = IRQF_SHARED;
555 1.1 riastrad
556 1.2 riastrad #ifdef __NetBSD__
557 1.2 riastrad ret = (*dev->driver->bus->irq_install)(dev, dev->driver->irq_handler,
558 1.9 riastrad sh_flags, dev->devname ? dev->devname : dev->driver->name, dev,
559 1.9 riastrad &dev->irq_cookie);
560 1.2 riastrad #else
561 1.9 riastrad ret = request_irq(irq, dev->driver->irq_handler,
562 1.9 riastrad sh_flags, dev->driver->name, dev);
563 1.2 riastrad #endif
564 1.1 riastrad
565 1.1 riastrad if (ret < 0) {
566 1.4 riastrad dev->irq_enabled = false;
567 1.1 riastrad return ret;
568 1.1 riastrad }
569 1.1 riastrad
570 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
571 1.1 riastrad vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
572 1.1 riastrad
573 1.1 riastrad /* After installing handler */
574 1.1 riastrad if (dev->driver->irq_postinstall)
575 1.1 riastrad ret = dev->driver->irq_postinstall(dev);
576 1.1 riastrad
577 1.1 riastrad if (ret < 0) {
578 1.4 riastrad dev->irq_enabled = false;
579 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
580 1.1 riastrad vga_client_register(dev->pdev, NULL, NULL, NULL);
581 1.2 riastrad #ifdef __NetBSD__
582 1.2 riastrad (*dev->driver->bus->irq_uninstall)(dev, dev->irq_cookie);
583 1.2 riastrad #else
584 1.9 riastrad free_irq(irq, dev);
585 1.2 riastrad #endif
586 1.9 riastrad } else {
587 1.9 riastrad dev->irq = irq;
588 1.1 riastrad }
589 1.1 riastrad
590 1.1 riastrad return ret;
591 1.1 riastrad }
592 1.1 riastrad EXPORT_SYMBOL(drm_irq_install);
593 1.1 riastrad
594 1.1 riastrad /**
595 1.9 riastrad * drm_irq_uninstall - uninstall the IRQ handler
596 1.9 riastrad * @dev: DRM device
597 1.1 riastrad *
598 1.9 riastrad * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
599 1.9 riastrad * This should only be called by drivers which used drm_irq_install() to set up
600 1.9 riastrad * their interrupt handler. Other drivers must only reset
601 1.9 riastrad * drm_device->irq_enabled to false.
602 1.9 riastrad *
603 1.9 riastrad * Note that for kernel modesetting drivers it is a bug if this function fails.
604 1.9 riastrad * The sanity checks are only to catch buggy user modesetting drivers which call
605 1.9 riastrad * the same function through an ioctl.
606 1.1 riastrad *
607 1.9 riastrad * Returns:
608 1.9 riastrad * Zero on success or a negative error code on failure.
609 1.1 riastrad */
610 1.1 riastrad int drm_irq_uninstall(struct drm_device *dev)
611 1.1 riastrad {
612 1.1 riastrad unsigned long irqflags;
613 1.4 riastrad bool irq_enabled;
614 1.4 riastrad int i;
615 1.1 riastrad
616 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
617 1.1 riastrad return -EINVAL;
618 1.1 riastrad
619 1.1 riastrad irq_enabled = dev->irq_enabled;
620 1.4 riastrad dev->irq_enabled = false;
621 1.1 riastrad
622 1.1 riastrad /*
623 1.9 riastrad * Wake up any waiters so they don't hang. This is just to paper over
624 1.9 riastrad * isssues for UMS drivers which aren't in full control of their
625 1.9 riastrad * vblank/irq handling. KMS drivers must ensure that vblanks are all
626 1.9 riastrad * disabled when uninstalling the irq handler.
627 1.1 riastrad */
628 1.1 riastrad if (dev->num_crtcs) {
629 1.1 riastrad spin_lock_irqsave(&dev->vbl_lock, irqflags);
630 1.1 riastrad for (i = 0; i < dev->num_crtcs; i++) {
631 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[i];
632 1.9 riastrad
633 1.9 riastrad if (!vblank->enabled)
634 1.9 riastrad continue;
635 1.9 riastrad
636 1.9 riastrad WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
637 1.9 riastrad
638 1.9 riastrad vblank_disable_and_save(dev, i);
639 1.2 riastrad #ifdef __NetBSD__
640 1.9 riastrad DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->vbl_lock);
641 1.2 riastrad #else
642 1.9 riastrad wake_up(&vblank->queue);
643 1.2 riastrad #endif
644 1.1 riastrad }
645 1.1 riastrad spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
646 1.1 riastrad }
647 1.1 riastrad
648 1.1 riastrad if (!irq_enabled)
649 1.1 riastrad return -EINVAL;
650 1.1 riastrad
651 1.9 riastrad DRM_DEBUG("irq=%d\n", dev->irq);
652 1.1 riastrad
653 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
654 1.1 riastrad vga_client_register(dev->pdev, NULL, NULL, NULL);
655 1.1 riastrad
656 1.1 riastrad if (dev->driver->irq_uninstall)
657 1.1 riastrad dev->driver->irq_uninstall(dev);
658 1.1 riastrad
659 1.2 riastrad #ifdef __NetBSD__
660 1.2 riastrad (*dev->driver->bus->irq_uninstall)(dev, dev->irq_cookie);
661 1.2 riastrad #else
662 1.9 riastrad free_irq(dev->irq, dev);
663 1.2 riastrad #endif
664 1.1 riastrad
665 1.1 riastrad return 0;
666 1.1 riastrad }
667 1.1 riastrad EXPORT_SYMBOL(drm_irq_uninstall);
668 1.1 riastrad
669 1.9 riastrad /*
670 1.1 riastrad * IRQ control ioctl.
671 1.1 riastrad *
672 1.1 riastrad * \param inode device inode.
673 1.1 riastrad * \param file_priv DRM file private.
674 1.1 riastrad * \param cmd command.
675 1.1 riastrad * \param arg user argument, pointing to a drm_control structure.
676 1.1 riastrad * \return zero on success or a negative number on failure.
677 1.1 riastrad *
678 1.1 riastrad * Calls irq_install() or irq_uninstall() according to \p arg.
679 1.1 riastrad */
680 1.1 riastrad int drm_control(struct drm_device *dev, void *data,
681 1.1 riastrad struct drm_file *file_priv)
682 1.1 riastrad {
683 1.1 riastrad struct drm_control *ctl = data;
684 1.9 riastrad int ret = 0, irq;
685 1.1 riastrad
686 1.1 riastrad /* if we haven't irq we fallback for compatibility reasons -
687 1.1 riastrad * this used to be a separate function in drm_dma.h
688 1.1 riastrad */
689 1.1 riastrad
690 1.9 riastrad if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
691 1.9 riastrad return 0;
692 1.9 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET))
693 1.9 riastrad return 0;
694 1.9 riastrad /* UMS was only ever support on pci devices. */
695 1.9 riastrad if (WARN_ON(!dev->pdev))
696 1.9 riastrad return -EINVAL;
697 1.1 riastrad
698 1.1 riastrad switch (ctl->func) {
699 1.1 riastrad case DRM_INST_HANDLER:
700 1.9 riastrad irq = dev->pdev->irq;
701 1.9 riastrad
702 1.1 riastrad if (dev->if_version < DRM_IF_VERSION(1, 2) &&
703 1.9 riastrad ctl->irq != irq)
704 1.1 riastrad return -EINVAL;
705 1.9 riastrad mutex_lock(&dev->struct_mutex);
706 1.9 riastrad ret = drm_irq_install(dev, irq);
707 1.9 riastrad mutex_unlock(&dev->struct_mutex);
708 1.9 riastrad
709 1.9 riastrad return ret;
710 1.1 riastrad case DRM_UNINST_HANDLER:
711 1.9 riastrad mutex_lock(&dev->struct_mutex);
712 1.9 riastrad ret = drm_irq_uninstall(dev);
713 1.9 riastrad mutex_unlock(&dev->struct_mutex);
714 1.9 riastrad
715 1.9 riastrad return ret;
716 1.1 riastrad default:
717 1.1 riastrad return -EINVAL;
718 1.1 riastrad }
719 1.1 riastrad }
720 1.1 riastrad
721 1.1 riastrad /**
722 1.9 riastrad * drm_calc_timestamping_constants - calculate vblank timestamp constants
723 1.9 riastrad * @crtc: drm_crtc whose timestamp constants should be updated.
724 1.9 riastrad * @mode: display mode containing the scanout timings
725 1.1 riastrad *
726 1.4 riastrad * Calculate and store various constants which are later
727 1.4 riastrad * needed by vblank and swap-completion timestamping, e.g,
728 1.4 riastrad * by drm_calc_vbltimestamp_from_scanoutpos(). They are
729 1.9 riastrad * derived from CRTC's true scanout timing, so they take
730 1.4 riastrad * things like panel scaling or other adjustments into account.
731 1.1 riastrad */
732 1.4 riastrad void drm_calc_timestamping_constants(struct drm_crtc *crtc,
733 1.4 riastrad const struct drm_display_mode *mode)
734 1.1 riastrad {
735 1.9 riastrad struct drm_device *dev = crtc->dev;
736 1.9 riastrad unsigned int pipe = drm_crtc_index(crtc);
737 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
738 1.9 riastrad int linedur_ns = 0, framedur_ns = 0;
739 1.4 riastrad int dotclock = mode->crtc_clock;
740 1.1 riastrad
741 1.9 riastrad if (!dev->num_crtcs)
742 1.9 riastrad return;
743 1.9 riastrad
744 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
745 1.9 riastrad return;
746 1.9 riastrad
747 1.4 riastrad /* Valid dotclock? */
748 1.4 riastrad if (dotclock > 0) {
749 1.4 riastrad int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
750 1.1 riastrad
751 1.4 riastrad /*
752 1.4 riastrad * Convert scanline length in pixels and video
753 1.9 riastrad * dot clock to line duration and frame duration
754 1.9 riastrad * in nanoseconds:
755 1.4 riastrad */
756 1.4 riastrad linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
757 1.4 riastrad framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
758 1.1 riastrad
759 1.4 riastrad /*
760 1.4 riastrad * Fields of interlaced scanout modes are only half a frame duration.
761 1.1 riastrad */
762 1.4 riastrad if (mode->flags & DRM_MODE_FLAG_INTERLACE)
763 1.4 riastrad framedur_ns /= 2;
764 1.1 riastrad } else
765 1.9 riastrad DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
766 1.1 riastrad crtc->base.id);
767 1.1 riastrad
768 1.9 riastrad vblank->linedur_ns = linedur_ns;
769 1.9 riastrad vblank->framedur_ns = framedur_ns;
770 1.1 riastrad
771 1.9 riastrad DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
772 1.4 riastrad crtc->base.id, mode->crtc_htotal,
773 1.4 riastrad mode->crtc_vtotal, mode->crtc_vdisplay);
774 1.9 riastrad DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
775 1.9 riastrad crtc->base.id, dotclock, framedur_ns, linedur_ns);
776 1.1 riastrad }
777 1.1 riastrad EXPORT_SYMBOL(drm_calc_timestamping_constants);
778 1.1 riastrad
779 1.1 riastrad /**
780 1.9 riastrad * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
781 1.9 riastrad * @dev: DRM device
782 1.9 riastrad * @pipe: index of CRTC whose vblank timestamp to retrieve
783 1.9 riastrad * @max_error: Desired maximum allowable error in timestamps (nanosecs)
784 1.9 riastrad * On return contains true maximum error of timestamp
785 1.9 riastrad * @vblank_time: Pointer to struct timeval which should receive the timestamp
786 1.9 riastrad * @flags: Flags to pass to driver:
787 1.9 riastrad * 0 = Default,
788 1.9 riastrad * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
789 1.9 riastrad * @mode: mode which defines the scanout timings
790 1.9 riastrad *
791 1.9 riastrad * Implements calculation of exact vblank timestamps from given drm_display_mode
792 1.9 riastrad * timings and current video scanout position of a CRTC. This can be called from
793 1.9 riastrad * within get_vblank_timestamp() implementation of a kms driver to implement the
794 1.9 riastrad * actual timestamping.
795 1.1 riastrad *
796 1.1 riastrad * Should return timestamps conforming to the OML_sync_control OpenML
797 1.1 riastrad * extension specification. The timestamp corresponds to the end of
798 1.1 riastrad * the vblank interval, aka start of scanout of topmost-leftmost display
799 1.1 riastrad * pixel in the following video frame.
800 1.1 riastrad *
801 1.1 riastrad * Requires support for optional dev->driver->get_scanout_position()
802 1.1 riastrad * in kms driver, plus a bit of setup code to provide a drm_display_mode
803 1.1 riastrad * that corresponds to the true scanout timing.
804 1.1 riastrad *
805 1.1 riastrad * The current implementation only handles standard video modes. It
806 1.1 riastrad * returns as no operation if a doublescan or interlaced video mode is
807 1.1 riastrad * active. Higher level code is expected to handle this.
808 1.1 riastrad *
809 1.9 riastrad * Returns:
810 1.9 riastrad * Negative value on error, failure or if not supported in current
811 1.1 riastrad * video mode:
812 1.1 riastrad *
813 1.9 riastrad * -EINVAL - Invalid CRTC.
814 1.1 riastrad * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
815 1.1 riastrad * -ENOTSUPP - Function not supported in current display mode.
816 1.1 riastrad * -EIO - Failed, e.g., due to failed scanout position query.
817 1.1 riastrad *
818 1.1 riastrad * Returns or'ed positive status flags on success:
819 1.1 riastrad *
820 1.1 riastrad * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
821 1.1 riastrad * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
822 1.1 riastrad *
823 1.1 riastrad */
824 1.9 riastrad int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
825 1.9 riastrad unsigned int pipe,
826 1.1 riastrad int *max_error,
827 1.1 riastrad struct timeval *vblank_time,
828 1.1 riastrad unsigned flags,
829 1.4 riastrad const struct drm_display_mode *mode)
830 1.1 riastrad {
831 1.1 riastrad struct timeval tv_etime;
832 1.9 riastrad ktime_t stime, etime;
833 1.9 riastrad unsigned int vbl_status;
834 1.9 riastrad int ret = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
835 1.1 riastrad int vpos, hpos, i;
836 1.9 riastrad int delta_ns, duration_ns;
837 1.1 riastrad
838 1.9 riastrad if (pipe >= dev->num_crtcs) {
839 1.9 riastrad DRM_ERROR("Invalid crtc %u\n", pipe);
840 1.1 riastrad return -EINVAL;
841 1.1 riastrad }
842 1.1 riastrad
843 1.1 riastrad /* Scanout position query not supported? Should not happen. */
844 1.1 riastrad if (!dev->driver->get_scanout_position) {
845 1.1 riastrad DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
846 1.1 riastrad return -EIO;
847 1.1 riastrad }
848 1.1 riastrad
849 1.1 riastrad /* If mode timing undefined, just return as no-op:
850 1.1 riastrad * Happens during initial modesetting of a crtc.
851 1.1 riastrad */
852 1.9 riastrad if (mode->crtc_clock == 0) {
853 1.9 riastrad DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
854 1.1 riastrad return -EAGAIN;
855 1.1 riastrad }
856 1.1 riastrad
857 1.1 riastrad /* Get current scanout position with system timestamp.
858 1.1 riastrad * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
859 1.1 riastrad * if single query takes longer than max_error nanoseconds.
860 1.1 riastrad *
861 1.1 riastrad * This guarantees a tight bound on maximum error if
862 1.1 riastrad * code gets preempted or delayed for some reason.
863 1.1 riastrad */
864 1.1 riastrad for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
865 1.4 riastrad /*
866 1.4 riastrad * Get vertical and horizontal scanout position vpos, hpos,
867 1.4 riastrad * and bounding timestamps stime, etime, pre/post query.
868 1.1 riastrad */
869 1.9 riastrad vbl_status = dev->driver->get_scanout_position(dev, pipe, flags,
870 1.9 riastrad &vpos, &hpos,
871 1.9 riastrad &stime, &etime,
872 1.9 riastrad mode);
873 1.1 riastrad
874 1.1 riastrad /* Return as no-op if scanout query unsupported or failed. */
875 1.1 riastrad if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
876 1.9 riastrad DRM_DEBUG("crtc %u : scanoutpos query failed [0x%x].\n",
877 1.9 riastrad pipe, vbl_status);
878 1.1 riastrad return -EIO;
879 1.1 riastrad }
880 1.1 riastrad
881 1.4 riastrad /* Compute uncertainty in timestamp of scanout position query. */
882 1.1 riastrad duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
883 1.1 riastrad
884 1.1 riastrad /* Accept result with < max_error nsecs timing uncertainty. */
885 1.4 riastrad if (duration_ns <= *max_error)
886 1.1 riastrad break;
887 1.1 riastrad }
888 1.1 riastrad
889 1.1 riastrad /* Noisy system timing? */
890 1.1 riastrad if (i == DRM_TIMESTAMP_MAXRETRIES) {
891 1.9 riastrad DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
892 1.9 riastrad pipe, duration_ns/1000, *max_error/1000, i);
893 1.1 riastrad }
894 1.1 riastrad
895 1.1 riastrad /* Return upper bound of timestamp precision error. */
896 1.4 riastrad *max_error = duration_ns;
897 1.1 riastrad
898 1.1 riastrad /* Check if in vblank area:
899 1.1 riastrad * vpos is >=0 in video scanout area, but negative
900 1.1 riastrad * within vblank area, counting down the number of lines until
901 1.1 riastrad * start of scanout.
902 1.1 riastrad */
903 1.9 riastrad if (vbl_status & DRM_SCANOUTPOS_IN_VBLANK)
904 1.9 riastrad ret |= DRM_VBLANKTIME_IN_VBLANK;
905 1.1 riastrad
906 1.1 riastrad /* Convert scanout position into elapsed time at raw_time query
907 1.1 riastrad * since start of scanout at first display scanline. delta_ns
908 1.1 riastrad * can be negative if start of scanout hasn't happened yet.
909 1.1 riastrad */
910 1.9 riastrad delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
911 1.9 riastrad mode->crtc_clock);
912 1.1 riastrad
913 1.1 riastrad if (!drm_timestamp_monotonic)
914 1.9 riastrad etime = ktime_mono_to_real(etime);
915 1.1 riastrad
916 1.1 riastrad /* save this only for debugging purposes */
917 1.1 riastrad tv_etime = ktime_to_timeval(etime);
918 1.1 riastrad /* Subtract time delta from raw timestamp to get final
919 1.1 riastrad * vblank_time timestamp for end of vblank.
920 1.1 riastrad */
921 1.4 riastrad if (delta_ns < 0)
922 1.4 riastrad etime = ktime_add_ns(etime, -delta_ns);
923 1.4 riastrad else
924 1.4 riastrad etime = ktime_sub_ns(etime, delta_ns);
925 1.1 riastrad *vblank_time = ktime_to_timeval(etime);
926 1.1 riastrad
927 1.9 riastrad DRM_DEBUG_VBL("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
928 1.9 riastrad pipe, vbl_status, hpos, vpos,
929 1.9 riastrad (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
930 1.9 riastrad (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
931 1.9 riastrad duration_ns/1000, i);
932 1.1 riastrad
933 1.9 riastrad return ret;
934 1.1 riastrad }
935 1.1 riastrad EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
936 1.1 riastrad
937 1.1 riastrad static struct timeval get_drm_timestamp(void)
938 1.1 riastrad {
939 1.1 riastrad ktime_t now;
940 1.1 riastrad
941 1.9 riastrad now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
942 1.1 riastrad return ktime_to_timeval(now);
943 1.1 riastrad }
944 1.1 riastrad
945 1.1 riastrad /**
946 1.1 riastrad * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
947 1.9 riastrad * vblank interval
948 1.1 riastrad * @dev: DRM device
949 1.9 riastrad * @pipe: index of CRTC whose vblank timestamp to retrieve
950 1.1 riastrad * @tvblank: Pointer to target struct timeval which should receive the timestamp
951 1.1 riastrad * @flags: Flags to pass to driver:
952 1.9 riastrad * 0 = Default,
953 1.9 riastrad * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
954 1.1 riastrad *
955 1.1 riastrad * Fetches the system timestamp corresponding to the time of the most recent
956 1.9 riastrad * vblank interval on specified CRTC. May call into kms-driver to
957 1.1 riastrad * compute the timestamp with a high-precision GPU specific method.
958 1.1 riastrad *
959 1.1 riastrad * Returns zero if timestamp originates from uncorrected do_gettimeofday()
960 1.1 riastrad * call, i.e., it isn't very precisely locked to the true vblank.
961 1.1 riastrad *
962 1.9 riastrad * Returns:
963 1.9 riastrad * True if timestamp is considered to be very precise, false otherwise.
964 1.1 riastrad */
965 1.9 riastrad static bool
966 1.9 riastrad drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
967 1.9 riastrad struct timeval *tvblank, unsigned flags)
968 1.1 riastrad {
969 1.1 riastrad int ret;
970 1.1 riastrad
971 1.1 riastrad /* Define requested maximum error on timestamps (nanoseconds). */
972 1.1 riastrad int max_error = (int) drm_timestamp_precision * 1000;
973 1.1 riastrad
974 1.1 riastrad /* Query driver if possible and precision timestamping enabled. */
975 1.1 riastrad if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
976 1.9 riastrad ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
977 1.1 riastrad tvblank, flags);
978 1.1 riastrad if (ret > 0)
979 1.9 riastrad return true;
980 1.1 riastrad }
981 1.1 riastrad
982 1.1 riastrad /* GPU high precision timestamp query unsupported or failed.
983 1.1 riastrad * Return current monotonic/gettimeofday timestamp as best estimate.
984 1.1 riastrad */
985 1.1 riastrad *tvblank = get_drm_timestamp();
986 1.1 riastrad
987 1.9 riastrad return false;
988 1.1 riastrad }
989 1.1 riastrad
990 1.1 riastrad /**
991 1.1 riastrad * drm_vblank_count - retrieve "cooked" vblank counter value
992 1.1 riastrad * @dev: DRM device
993 1.9 riastrad * @pipe: index of CRTC for which to retrieve the counter
994 1.1 riastrad *
995 1.1 riastrad * Fetches the "cooked" vblank count value that represents the number of
996 1.1 riastrad * vblank events since the system was booted, including lost events due to
997 1.1 riastrad * modesetting activity.
998 1.9 riastrad *
999 1.9 riastrad * This is the legacy version of drm_crtc_vblank_count().
1000 1.9 riastrad *
1001 1.9 riastrad * Returns:
1002 1.9 riastrad * The software vblank counter.
1003 1.1 riastrad */
1004 1.9 riastrad u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
1005 1.1 riastrad {
1006 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1007 1.9 riastrad
1008 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1009 1.9 riastrad return 0;
1010 1.9 riastrad
1011 1.9 riastrad return vblank->count;
1012 1.1 riastrad }
1013 1.1 riastrad EXPORT_SYMBOL(drm_vblank_count);
1014 1.1 riastrad
1015 1.1 riastrad /**
1016 1.9 riastrad * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
1017 1.9 riastrad * @crtc: which counter to retrieve
1018 1.9 riastrad *
1019 1.9 riastrad * Fetches the "cooked" vblank count value that represents the number of
1020 1.9 riastrad * vblank events since the system was booted, including lost events due to
1021 1.9 riastrad * modesetting activity.
1022 1.9 riastrad *
1023 1.9 riastrad * This is the native KMS version of drm_vblank_count().
1024 1.1 riastrad *
1025 1.9 riastrad * Returns:
1026 1.9 riastrad * The software vblank counter.
1027 1.9 riastrad */
1028 1.9 riastrad u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
1029 1.9 riastrad {
1030 1.9 riastrad return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
1031 1.9 riastrad }
1032 1.9 riastrad EXPORT_SYMBOL(drm_crtc_vblank_count);
1033 1.9 riastrad
1034 1.9 riastrad /**
1035 1.9 riastrad * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
1036 1.9 riastrad * system timestamp corresponding to that vblank counter value.
1037 1.1 riastrad * @dev: DRM device
1038 1.9 riastrad * @pipe: index of CRTC whose counter to retrieve
1039 1.1 riastrad * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
1040 1.1 riastrad *
1041 1.1 riastrad * Fetches the "cooked" vblank count value that represents the number of
1042 1.1 riastrad * vblank events since the system was booted, including lost events due to
1043 1.1 riastrad * modesetting activity. Returns corresponding system timestamp of the time
1044 1.9 riastrad * of the vblank interval that corresponds to the current vblank counter value.
1045 1.9 riastrad *
1046 1.9 riastrad * This is the legacy version of drm_crtc_vblank_count_and_time().
1047 1.1 riastrad */
1048 1.9 riastrad u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
1049 1.1 riastrad struct timeval *vblanktime)
1050 1.1 riastrad {
1051 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1052 1.9 riastrad int count = DRM_TIMESTAMP_MAXRETRIES;
1053 1.1 riastrad u32 cur_vblank;
1054 1.1 riastrad
1055 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1056 1.9 riastrad return 0;
1057 1.9 riastrad
1058 1.9 riastrad /*
1059 1.9 riastrad * Vblank timestamps are read lockless. To ensure consistency the vblank
1060 1.9 riastrad * counter is rechecked and ordering is ensured using memory barriers.
1061 1.9 riastrad * This works like a seqlock. The write-side barriers are in store_vblank.
1062 1.1 riastrad */
1063 1.1 riastrad do {
1064 1.9 riastrad cur_vblank = vblank->count;
1065 1.1 riastrad smp_rmb();
1066 1.9 riastrad *vblanktime = vblanktimestamp(dev, pipe, cur_vblank);
1067 1.9 riastrad smp_rmb();
1068 1.9 riastrad } while (cur_vblank != vblank->count && --count > 0);
1069 1.1 riastrad
1070 1.1 riastrad return cur_vblank;
1071 1.1 riastrad }
1072 1.1 riastrad EXPORT_SYMBOL(drm_vblank_count_and_time);
1073 1.1 riastrad
1074 1.9 riastrad /**
1075 1.9 riastrad * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
1076 1.9 riastrad * and the system timestamp corresponding to that vblank counter value
1077 1.9 riastrad * @crtc: which counter to retrieve
1078 1.9 riastrad * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
1079 1.9 riastrad *
1080 1.9 riastrad * Fetches the "cooked" vblank count value that represents the number of
1081 1.9 riastrad * vblank events since the system was booted, including lost events due to
1082 1.9 riastrad * modesetting activity. Returns corresponding system timestamp of the time
1083 1.9 riastrad * of the vblank interval that corresponds to the current vblank counter value.
1084 1.9 riastrad *
1085 1.9 riastrad * This is the native KMS version of drm_vblank_count_and_time().
1086 1.9 riastrad */
1087 1.9 riastrad u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
1088 1.9 riastrad struct timeval *vblanktime)
1089 1.9 riastrad {
1090 1.9 riastrad return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
1091 1.9 riastrad vblanktime);
1092 1.9 riastrad }
1093 1.9 riastrad EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
1094 1.9 riastrad
1095 1.1 riastrad static void send_vblank_event(struct drm_device *dev,
1096 1.1 riastrad struct drm_pending_vblank_event *e,
1097 1.1 riastrad unsigned long seq, struct timeval *now)
1098 1.1 riastrad {
1099 1.9 riastrad assert_spin_locked(&dev->event_lock);
1100 1.9 riastrad
1101 1.1 riastrad e->event.sequence = seq;
1102 1.1 riastrad e->event.tv_sec = now->tv_sec;
1103 1.1 riastrad e->event.tv_usec = now->tv_usec;
1104 1.1 riastrad
1105 1.1 riastrad list_add_tail(&e->base.link,
1106 1.1 riastrad &e->base.file_priv->event_list);
1107 1.2 riastrad #ifdef __NetBSD__
1108 1.2 riastrad DRM_SPIN_WAKEUP_ONE(&e->base.file_priv->event_wait, &dev->event_lock);
1109 1.2 riastrad selnotify(&e->base.file_priv->event_selq, (POLLIN | POLLRDNORM),
1110 1.2 riastrad NOTE_SUBMIT);
1111 1.2 riastrad #else
1112 1.1 riastrad wake_up_interruptible(&e->base.file_priv->event_wait);
1113 1.2 riastrad #endif
1114 1.1 riastrad trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
1115 1.1 riastrad e->event.sequence);
1116 1.1 riastrad }
1117 1.1 riastrad
1118 1.1 riastrad /**
1119 1.9 riastrad * drm_arm_vblank_event - arm vblank event after pageflip
1120 1.9 riastrad * @dev: DRM device
1121 1.9 riastrad * @pipe: CRTC index
1122 1.9 riastrad * @e: the event to prepare to send
1123 1.9 riastrad *
1124 1.9 riastrad * A lot of drivers need to generate vblank events for the very next vblank
1125 1.9 riastrad * interrupt. For example when the page flip interrupt happens when the page
1126 1.9 riastrad * flip gets armed, but not when it actually executes within the next vblank
1127 1.9 riastrad * period. This helper function implements exactly the required vblank arming
1128 1.9 riastrad * behaviour.
1129 1.9 riastrad *
1130 1.9 riastrad * Caller must hold event lock. Caller must also hold a vblank reference for
1131 1.9 riastrad * the event @e, which will be dropped when the next vblank arrives.
1132 1.9 riastrad *
1133 1.9 riastrad * This is the legacy version of drm_crtc_arm_vblank_event().
1134 1.9 riastrad */
1135 1.9 riastrad void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
1136 1.9 riastrad struct drm_pending_vblank_event *e)
1137 1.9 riastrad {
1138 1.9 riastrad assert_spin_locked(&dev->event_lock);
1139 1.9 riastrad
1140 1.9 riastrad e->pipe = pipe;
1141 1.9 riastrad e->event.sequence = drm_vblank_count(dev, pipe);
1142 1.9 riastrad list_add_tail(&e->base.link, &dev->vblank_event_list);
1143 1.9 riastrad }
1144 1.9 riastrad EXPORT_SYMBOL(drm_arm_vblank_event);
1145 1.9 riastrad
1146 1.9 riastrad /**
1147 1.9 riastrad * drm_crtc_arm_vblank_event - arm vblank event after pageflip
1148 1.9 riastrad * @crtc: the source CRTC of the vblank event
1149 1.9 riastrad * @e: the event to send
1150 1.9 riastrad *
1151 1.9 riastrad * A lot of drivers need to generate vblank events for the very next vblank
1152 1.9 riastrad * interrupt. For example when the page flip interrupt happens when the page
1153 1.9 riastrad * flip gets armed, but not when it actually executes within the next vblank
1154 1.9 riastrad * period. This helper function implements exactly the required vblank arming
1155 1.9 riastrad * behaviour.
1156 1.9 riastrad *
1157 1.9 riastrad * Caller must hold event lock. Caller must also hold a vblank reference for
1158 1.9 riastrad * the event @e, which will be dropped when the next vblank arrives.
1159 1.9 riastrad *
1160 1.9 riastrad * This is the native KMS version of drm_arm_vblank_event().
1161 1.9 riastrad */
1162 1.9 riastrad void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1163 1.9 riastrad struct drm_pending_vblank_event *e)
1164 1.9 riastrad {
1165 1.9 riastrad drm_arm_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
1166 1.9 riastrad }
1167 1.9 riastrad EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
1168 1.9 riastrad
1169 1.9 riastrad /**
1170 1.1 riastrad * drm_send_vblank_event - helper to send vblank event after pageflip
1171 1.1 riastrad * @dev: DRM device
1172 1.9 riastrad * @pipe: CRTC index
1173 1.1 riastrad * @e: the event to send
1174 1.1 riastrad *
1175 1.1 riastrad * Updates sequence # and timestamp on event, and sends it to userspace.
1176 1.1 riastrad * Caller must hold event lock.
1177 1.9 riastrad *
1178 1.9 riastrad * This is the legacy version of drm_crtc_send_vblank_event().
1179 1.1 riastrad */
1180 1.9 riastrad void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1181 1.9 riastrad struct drm_pending_vblank_event *e)
1182 1.1 riastrad {
1183 1.1 riastrad struct timeval now;
1184 1.1 riastrad unsigned int seq;
1185 1.9 riastrad
1186 1.9 riastrad if (dev->num_crtcs > 0) {
1187 1.9 riastrad seq = drm_vblank_count_and_time(dev, pipe, &now);
1188 1.1 riastrad } else {
1189 1.1 riastrad seq = 0;
1190 1.1 riastrad
1191 1.1 riastrad now = get_drm_timestamp();
1192 1.1 riastrad }
1193 1.9 riastrad e->pipe = pipe;
1194 1.1 riastrad send_vblank_event(dev, e, seq, &now);
1195 1.1 riastrad }
1196 1.1 riastrad EXPORT_SYMBOL(drm_send_vblank_event);
1197 1.1 riastrad
1198 1.1 riastrad /**
1199 1.9 riastrad * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
1200 1.9 riastrad * @crtc: the source CRTC of the vblank event
1201 1.9 riastrad * @e: the event to send
1202 1.1 riastrad *
1203 1.9 riastrad * Updates sequence # and timestamp on event, and sends it to userspace.
1204 1.9 riastrad * Caller must hold event lock.
1205 1.1 riastrad *
1206 1.9 riastrad * This is the native KMS version of drm_send_vblank_event().
1207 1.9 riastrad */
1208 1.9 riastrad void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1209 1.9 riastrad struct drm_pending_vblank_event *e)
1210 1.9 riastrad {
1211 1.9 riastrad drm_send_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
1212 1.9 riastrad }
1213 1.9 riastrad EXPORT_SYMBOL(drm_crtc_send_vblank_event);
1214 1.9 riastrad
1215 1.9 riastrad /**
1216 1.9 riastrad * drm_vblank_enable - enable the vblank interrupt on a CRTC
1217 1.9 riastrad * @dev: DRM device
1218 1.9 riastrad * @pipe: CRTC index
1219 1.1 riastrad *
1220 1.9 riastrad * Returns:
1221 1.9 riastrad * Zero on success or a negative error code on failure.
1222 1.1 riastrad */
1223 1.9 riastrad static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
1224 1.1 riastrad {
1225 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1226 1.9 riastrad int ret = 0;
1227 1.1 riastrad
1228 1.9 riastrad assert_spin_locked(&dev->vbl_lock);
1229 1.1 riastrad
1230 1.9 riastrad spin_lock(&dev->vblank_time_lock);
1231 1.1 riastrad
1232 1.9 riastrad if (!vblank->enabled) {
1233 1.9 riastrad /*
1234 1.9 riastrad * Enable vblank irqs under vblank_time_lock protection.
1235 1.9 riastrad * All vblank count & timestamp updates are held off
1236 1.9 riastrad * until we are done reinitializing master counter and
1237 1.9 riastrad * timestamps. Filtercode in drm_handle_vblank() will
1238 1.9 riastrad * prevent double-accounting of same vblank interval.
1239 1.9 riastrad */
1240 1.9 riastrad ret = dev->driver->enable_vblank(dev, pipe);
1241 1.9 riastrad DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
1242 1.9 riastrad if (ret)
1243 1.9 riastrad atomic_dec(&vblank->refcount);
1244 1.9 riastrad else {
1245 1.9 riastrad vblank->enabled = true;
1246 1.9 riastrad drm_update_vblank_count(dev, pipe, 0);
1247 1.9 riastrad }
1248 1.1 riastrad }
1249 1.1 riastrad
1250 1.9 riastrad spin_unlock(&dev->vblank_time_lock);
1251 1.1 riastrad
1252 1.9 riastrad return ret;
1253 1.1 riastrad }
1254 1.1 riastrad
1255 1.1 riastrad /**
1256 1.1 riastrad * drm_vblank_get - get a reference count on vblank events
1257 1.1 riastrad * @dev: DRM device
1258 1.9 riastrad * @pipe: index of CRTC to own
1259 1.1 riastrad *
1260 1.1 riastrad * Acquire a reference count on vblank events to avoid having them disabled
1261 1.1 riastrad * while in use.
1262 1.1 riastrad *
1263 1.9 riastrad * This is the legacy version of drm_crtc_vblank_get().
1264 1.9 riastrad *
1265 1.9 riastrad * Returns:
1266 1.9 riastrad * Zero on success or a negative error code on failure.
1267 1.1 riastrad */
1268 1.9 riastrad int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
1269 1.1 riastrad {
1270 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1271 1.9 riastrad unsigned long irqflags;
1272 1.1 riastrad int ret = 0;
1273 1.1 riastrad
1274 1.9 riastrad if (!dev->num_crtcs)
1275 1.9 riastrad return -EINVAL;
1276 1.9 riastrad
1277 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1278 1.9 riastrad return -EINVAL;
1279 1.9 riastrad
1280 1.1 riastrad spin_lock_irqsave(&dev->vbl_lock, irqflags);
1281 1.1 riastrad /* Going from 0->1 means we have to enable interrupts again */
1282 1.9 riastrad if (atomic_add_return(1, &vblank->refcount) == 1) {
1283 1.9 riastrad ret = drm_vblank_enable(dev, pipe);
1284 1.1 riastrad } else {
1285 1.9 riastrad if (!vblank->enabled) {
1286 1.9 riastrad atomic_dec(&vblank->refcount);
1287 1.1 riastrad ret = -EINVAL;
1288 1.1 riastrad }
1289 1.1 riastrad }
1290 1.1 riastrad spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1291 1.1 riastrad
1292 1.1 riastrad return ret;
1293 1.1 riastrad }
1294 1.1 riastrad EXPORT_SYMBOL(drm_vblank_get);
1295 1.1 riastrad
1296 1.1 riastrad /**
1297 1.9 riastrad * drm_crtc_vblank_get - get a reference count on vblank events
1298 1.9 riastrad * @crtc: which CRTC to own
1299 1.9 riastrad *
1300 1.9 riastrad * Acquire a reference count on vblank events to avoid having them disabled
1301 1.9 riastrad * while in use.
1302 1.9 riastrad *
1303 1.9 riastrad * This is the native kms version of drm_vblank_get().
1304 1.9 riastrad *
1305 1.9 riastrad * Returns:
1306 1.9 riastrad * Zero on success or a negative error code on failure.
1307 1.9 riastrad */
1308 1.9 riastrad int drm_crtc_vblank_get(struct drm_crtc *crtc)
1309 1.9 riastrad {
1310 1.9 riastrad return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
1311 1.9 riastrad }
1312 1.9 riastrad EXPORT_SYMBOL(drm_crtc_vblank_get);
1313 1.9 riastrad
1314 1.9 riastrad /**
1315 1.9 riastrad * drm_vblank_put - release ownership of vblank events
1316 1.1 riastrad * @dev: DRM device
1317 1.9 riastrad * @pipe: index of CRTC to release
1318 1.1 riastrad *
1319 1.1 riastrad * Release ownership of a given vblank counter, turning off interrupts
1320 1.1 riastrad * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1321 1.9 riastrad *
1322 1.9 riastrad * This is the legacy version of drm_crtc_vblank_put().
1323 1.1 riastrad */
1324 1.9 riastrad void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
1325 1.1 riastrad {
1326 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1327 1.9 riastrad
1328 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1329 1.9 riastrad return;
1330 1.9 riastrad
1331 1.9 riastrad if (WARN_ON(atomic_read(&vblank->refcount) == 0))
1332 1.9 riastrad return;
1333 1.1 riastrad
1334 1.1 riastrad /* Last user schedules interrupt disable */
1335 1.9 riastrad if (atomic_dec_and_test(&vblank->refcount)) {
1336 1.9 riastrad if (drm_vblank_offdelay == 0)
1337 1.9 riastrad return;
1338 1.9 riastrad else if (drm_vblank_offdelay < 0)
1339 1.9 riastrad vblank_disable_fn((unsigned long)vblank);
1340 1.9 riastrad else if (!dev->vblank_disable_immediate)
1341 1.9 riastrad mod_timer(&vblank->disable_timer,
1342 1.9 riastrad jiffies + ((drm_vblank_offdelay * HZ)/1000));
1343 1.9 riastrad }
1344 1.1 riastrad }
1345 1.1 riastrad EXPORT_SYMBOL(drm_vblank_put);
1346 1.1 riastrad
1347 1.1 riastrad /**
1348 1.9 riastrad * drm_crtc_vblank_put - give up ownership of vblank events
1349 1.9 riastrad * @crtc: which counter to give up
1350 1.9 riastrad *
1351 1.9 riastrad * Release ownership of a given vblank counter, turning off interrupts
1352 1.9 riastrad * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1353 1.9 riastrad *
1354 1.9 riastrad * This is the native kms version of drm_vblank_put().
1355 1.9 riastrad */
1356 1.9 riastrad void drm_crtc_vblank_put(struct drm_crtc *crtc)
1357 1.9 riastrad {
1358 1.9 riastrad drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1359 1.9 riastrad }
1360 1.9 riastrad EXPORT_SYMBOL(drm_crtc_vblank_put);
1361 1.9 riastrad
1362 1.9 riastrad /**
1363 1.9 riastrad * drm_wait_one_vblank - wait for one vblank
1364 1.9 riastrad * @dev: DRM device
1365 1.9 riastrad * @pipe: CRTC index
1366 1.9 riastrad *
1367 1.9 riastrad * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1368 1.9 riastrad * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1369 1.9 riastrad * due to lack of driver support or because the crtc is off.
1370 1.9 riastrad */
1371 1.9 riastrad void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1372 1.9 riastrad {
1373 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1374 1.9 riastrad int ret;
1375 1.9 riastrad u32 last;
1376 1.9 riastrad
1377 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1378 1.9 riastrad return;
1379 1.9 riastrad
1380 1.9 riastrad ret = drm_vblank_get(dev, pipe);
1381 1.9 riastrad if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
1382 1.9 riastrad return;
1383 1.9 riastrad
1384 1.9 riastrad last = drm_vblank_count(dev, pipe);
1385 1.9 riastrad
1386 1.9 riastrad ret = wait_event_timeout(vblank->queue,
1387 1.9 riastrad last != drm_vblank_count(dev, pipe),
1388 1.9 riastrad msecs_to_jiffies(100));
1389 1.9 riastrad
1390 1.9 riastrad WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1391 1.9 riastrad
1392 1.9 riastrad drm_vblank_put(dev, pipe);
1393 1.9 riastrad }
1394 1.9 riastrad EXPORT_SYMBOL(drm_wait_one_vblank);
1395 1.9 riastrad
1396 1.9 riastrad /**
1397 1.9 riastrad * drm_crtc_wait_one_vblank - wait for one vblank
1398 1.9 riastrad * @crtc: DRM crtc
1399 1.9 riastrad *
1400 1.9 riastrad * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1401 1.9 riastrad * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1402 1.9 riastrad * due to lack of driver support or because the crtc is off.
1403 1.9 riastrad */
1404 1.9 riastrad void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1405 1.9 riastrad {
1406 1.9 riastrad drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1407 1.9 riastrad }
1408 1.9 riastrad EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1409 1.9 riastrad
1410 1.9 riastrad /**
1411 1.1 riastrad * drm_vblank_off - disable vblank events on a CRTC
1412 1.1 riastrad * @dev: DRM device
1413 1.9 riastrad * @pipe: CRTC index
1414 1.9 riastrad *
1415 1.9 riastrad * Drivers can use this function to shut down the vblank interrupt handling when
1416 1.9 riastrad * disabling a crtc. This function ensures that the latest vblank frame count is
1417 1.9 riastrad * stored so that drm_vblank_on() can restore it again.
1418 1.9 riastrad *
1419 1.9 riastrad * Drivers must use this function when the hardware vblank counter can get
1420 1.9 riastrad * reset, e.g. when suspending.
1421 1.1 riastrad *
1422 1.9 riastrad * This is the legacy version of drm_crtc_vblank_off().
1423 1.1 riastrad */
1424 1.9 riastrad void drm_vblank_off(struct drm_device *dev, unsigned int pipe)
1425 1.1 riastrad {
1426 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1427 1.1 riastrad struct drm_pending_vblank_event *e, *t;
1428 1.1 riastrad struct timeval now;
1429 1.1 riastrad unsigned long irqflags;
1430 1.1 riastrad unsigned int seq;
1431 1.1 riastrad
1432 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1433 1.9 riastrad return;
1434 1.9 riastrad
1435 1.9 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
1436 1.9 riastrad
1437 1.9 riastrad spin_lock(&dev->vbl_lock);
1438 1.9 riastrad DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1439 1.9 riastrad pipe, vblank->enabled, vblank->inmodeset);
1440 1.9 riastrad
1441 1.9 riastrad /* Avoid redundant vblank disables without previous drm_vblank_on(). */
1442 1.9 riastrad if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1443 1.9 riastrad vblank_disable_and_save(dev, pipe);
1444 1.9 riastrad
1445 1.2 riastrad #ifdef __NetBSD__
1446 1.9 riastrad DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->vbl_lock);
1447 1.2 riastrad #else
1448 1.9 riastrad wake_up(&vblank->queue);
1449 1.2 riastrad #endif
1450 1.1 riastrad
1451 1.9 riastrad /*
1452 1.9 riastrad * Prevent subsequent drm_vblank_get() from re-enabling
1453 1.9 riastrad * the vblank interrupt by bumping the refcount.
1454 1.9 riastrad */
1455 1.9 riastrad if (!vblank->inmodeset) {
1456 1.9 riastrad atomic_inc(&vblank->refcount);
1457 1.9 riastrad vblank->inmodeset = 1;
1458 1.9 riastrad }
1459 1.9 riastrad spin_unlock(&dev->vbl_lock);
1460 1.9 riastrad
1461 1.1 riastrad /* Send any queued vblank events, lest the natives grow disquiet */
1462 1.9 riastrad seq = drm_vblank_count_and_time(dev, pipe, &now);
1463 1.1 riastrad
1464 1.1 riastrad list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1465 1.9 riastrad if (e->pipe != pipe)
1466 1.1 riastrad continue;
1467 1.9 riastrad DRM_DEBUG("Sending premature vblank event on disable: "
1468 1.9 riastrad "wanted %d, current %d\n",
1469 1.1 riastrad e->event.sequence, seq);
1470 1.1 riastrad list_del(&e->base.link);
1471 1.9 riastrad drm_vblank_put(dev, pipe);
1472 1.1 riastrad send_vblank_event(dev, e, seq, &now);
1473 1.1 riastrad }
1474 1.9 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
1475 1.9 riastrad }
1476 1.9 riastrad EXPORT_SYMBOL(drm_vblank_off);
1477 1.9 riastrad
1478 1.9 riastrad /**
1479 1.9 riastrad * drm_crtc_vblank_off - disable vblank events on a CRTC
1480 1.9 riastrad * @crtc: CRTC in question
1481 1.9 riastrad *
1482 1.9 riastrad * Drivers can use this function to shut down the vblank interrupt handling when
1483 1.9 riastrad * disabling a crtc. This function ensures that the latest vblank frame count is
1484 1.9 riastrad * stored so that drm_vblank_on can restore it again.
1485 1.9 riastrad *
1486 1.9 riastrad * Drivers must use this function when the hardware vblank counter can get
1487 1.9 riastrad * reset, e.g. when suspending.
1488 1.9 riastrad *
1489 1.9 riastrad * This is the native kms version of drm_vblank_off().
1490 1.9 riastrad */
1491 1.9 riastrad void drm_crtc_vblank_off(struct drm_crtc *crtc)
1492 1.9 riastrad {
1493 1.9 riastrad drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1494 1.9 riastrad }
1495 1.9 riastrad EXPORT_SYMBOL(drm_crtc_vblank_off);
1496 1.1 riastrad
1497 1.9 riastrad /**
1498 1.9 riastrad * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1499 1.9 riastrad * @crtc: CRTC in question
1500 1.9 riastrad *
1501 1.9 riastrad * Drivers can use this function to reset the vblank state to off at load time.
1502 1.9 riastrad * Drivers should use this together with the drm_crtc_vblank_off() and
1503 1.9 riastrad * drm_crtc_vblank_on() functions. The difference compared to
1504 1.9 riastrad * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1505 1.9 riastrad * and hence doesn't need to call any driver hooks.
1506 1.9 riastrad */
1507 1.9 riastrad void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1508 1.9 riastrad {
1509 1.9 riastrad struct drm_device *dev = crtc->dev;
1510 1.9 riastrad unsigned long irqflags;
1511 1.9 riastrad unsigned int pipe = drm_crtc_index(crtc);
1512 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1513 1.9 riastrad
1514 1.9 riastrad spin_lock_irqsave(&dev->vbl_lock, irqflags);
1515 1.9 riastrad /*
1516 1.9 riastrad * Prevent subsequent drm_vblank_get() from enabling the vblank
1517 1.9 riastrad * interrupt by bumping the refcount.
1518 1.9 riastrad */
1519 1.9 riastrad if (!vblank->inmodeset) {
1520 1.9 riastrad atomic_inc(&vblank->refcount);
1521 1.9 riastrad vblank->inmodeset = 1;
1522 1.9 riastrad }
1523 1.1 riastrad spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1524 1.9 riastrad
1525 1.9 riastrad WARN_ON(!list_empty(&dev->vblank_event_list));
1526 1.1 riastrad }
1527 1.9 riastrad EXPORT_SYMBOL(drm_crtc_vblank_reset);
1528 1.9 riastrad
1529 1.9 riastrad /**
1530 1.9 riastrad * drm_vblank_on - enable vblank events on a CRTC
1531 1.9 riastrad * @dev: DRM device
1532 1.9 riastrad * @pipe: CRTC index
1533 1.9 riastrad *
1534 1.9 riastrad * This functions restores the vblank interrupt state captured with
1535 1.9 riastrad * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1536 1.9 riastrad * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1537 1.9 riastrad * in driver load code to reflect the current hardware state of the crtc.
1538 1.9 riastrad *
1539 1.9 riastrad * This is the legacy version of drm_crtc_vblank_on().
1540 1.9 riastrad */
1541 1.9 riastrad void drm_vblank_on(struct drm_device *dev, unsigned int pipe)
1542 1.9 riastrad {
1543 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1544 1.9 riastrad unsigned long irqflags;
1545 1.9 riastrad
1546 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1547 1.9 riastrad return;
1548 1.9 riastrad
1549 1.9 riastrad spin_lock_irqsave(&dev->vbl_lock, irqflags);
1550 1.9 riastrad DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1551 1.9 riastrad pipe, vblank->enabled, vblank->inmodeset);
1552 1.9 riastrad
1553 1.9 riastrad /* Drop our private "prevent drm_vblank_get" refcount */
1554 1.9 riastrad if (vblank->inmodeset) {
1555 1.9 riastrad atomic_dec(&vblank->refcount);
1556 1.9 riastrad vblank->inmodeset = 0;
1557 1.9 riastrad }
1558 1.9 riastrad
1559 1.9 riastrad drm_reset_vblank_timestamp(dev, pipe);
1560 1.9 riastrad
1561 1.9 riastrad /*
1562 1.9 riastrad * re-enable interrupts if there are users left, or the
1563 1.9 riastrad * user wishes vblank interrupts to be enabled all the time.
1564 1.9 riastrad */
1565 1.9 riastrad if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1566 1.9 riastrad WARN_ON(drm_vblank_enable(dev, pipe));
1567 1.9 riastrad spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1568 1.9 riastrad }
1569 1.9 riastrad EXPORT_SYMBOL(drm_vblank_on);
1570 1.9 riastrad
1571 1.9 riastrad /**
1572 1.9 riastrad * drm_crtc_vblank_on - enable vblank events on a CRTC
1573 1.9 riastrad * @crtc: CRTC in question
1574 1.9 riastrad *
1575 1.9 riastrad * This functions restores the vblank interrupt state captured with
1576 1.9 riastrad * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1577 1.9 riastrad * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1578 1.9 riastrad * in driver load code to reflect the current hardware state of the crtc.
1579 1.9 riastrad *
1580 1.9 riastrad * This is the native kms version of drm_vblank_on().
1581 1.9 riastrad */
1582 1.9 riastrad void drm_crtc_vblank_on(struct drm_crtc *crtc)
1583 1.9 riastrad {
1584 1.9 riastrad drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1585 1.9 riastrad }
1586 1.9 riastrad EXPORT_SYMBOL(drm_crtc_vblank_on);
1587 1.1 riastrad
1588 1.1 riastrad /**
1589 1.1 riastrad * drm_vblank_pre_modeset - account for vblanks across mode sets
1590 1.1 riastrad * @dev: DRM device
1591 1.9 riastrad * @pipe: CRTC index
1592 1.1 riastrad *
1593 1.1 riastrad * Account for vblank events across mode setting events, which will likely
1594 1.1 riastrad * reset the hardware frame counter.
1595 1.9 riastrad *
1596 1.9 riastrad * This is done by grabbing a temporary vblank reference to ensure that the
1597 1.9 riastrad * vblank interrupt keeps running across the modeset sequence. With this the
1598 1.9 riastrad * software-side vblank frame counting will ensure that there are no jumps or
1599 1.9 riastrad * discontinuities.
1600 1.9 riastrad *
1601 1.9 riastrad * Unfortunately this approach is racy and also doesn't work when the vblank
1602 1.9 riastrad * interrupt stops running, e.g. across system suspend resume. It is therefore
1603 1.9 riastrad * highly recommended that drivers use the newer drm_vblank_off() and
1604 1.9 riastrad * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1605 1.9 riastrad * using "cooked" software vblank frame counters and not relying on any hardware
1606 1.9 riastrad * counters.
1607 1.9 riastrad *
1608 1.9 riastrad * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1609 1.9 riastrad * again.
1610 1.1 riastrad */
1611 1.9 riastrad void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe)
1612 1.1 riastrad {
1613 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1614 1.9 riastrad
1615 1.4 riastrad /* vblank is not initialized (IRQ not installed ?), or has been freed */
1616 1.1 riastrad if (!dev->num_crtcs)
1617 1.1 riastrad return;
1618 1.9 riastrad
1619 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1620 1.9 riastrad return;
1621 1.9 riastrad
1622 1.1 riastrad /*
1623 1.1 riastrad * To avoid all the problems that might happen if interrupts
1624 1.1 riastrad * were enabled/disabled around or between these calls, we just
1625 1.1 riastrad * have the kernel take a reference on the CRTC (just once though
1626 1.1 riastrad * to avoid corrupting the count if multiple, mismatch calls occur),
1627 1.1 riastrad * so that interrupts remain enabled in the interim.
1628 1.1 riastrad */
1629 1.9 riastrad if (!vblank->inmodeset) {
1630 1.9 riastrad vblank->inmodeset = 0x1;
1631 1.9 riastrad if (drm_vblank_get(dev, pipe) == 0)
1632 1.9 riastrad vblank->inmodeset |= 0x2;
1633 1.1 riastrad }
1634 1.1 riastrad }
1635 1.1 riastrad EXPORT_SYMBOL(drm_vblank_pre_modeset);
1636 1.1 riastrad
1637 1.9 riastrad /**
1638 1.9 riastrad * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1639 1.9 riastrad * @dev: DRM device
1640 1.9 riastrad * @pipe: CRTC index
1641 1.9 riastrad *
1642 1.9 riastrad * This function again drops the temporary vblank reference acquired in
1643 1.9 riastrad * drm_vblank_pre_modeset.
1644 1.9 riastrad */
1645 1.9 riastrad void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe)
1646 1.1 riastrad {
1647 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1648 1.1 riastrad unsigned long irqflags;
1649 1.1 riastrad
1650 1.4 riastrad /* vblank is not initialized (IRQ not installed ?), or has been freed */
1651 1.4 riastrad if (!dev->num_crtcs)
1652 1.4 riastrad return;
1653 1.4 riastrad
1654 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1655 1.9 riastrad return;
1656 1.9 riastrad
1657 1.9 riastrad if (vblank->inmodeset) {
1658 1.1 riastrad spin_lock_irqsave(&dev->vbl_lock, irqflags);
1659 1.4 riastrad dev->vblank_disable_allowed = true;
1660 1.9 riastrad drm_reset_vblank_timestamp(dev, pipe);
1661 1.1 riastrad spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1662 1.1 riastrad
1663 1.9 riastrad if (vblank->inmodeset & 0x2)
1664 1.9 riastrad drm_vblank_put(dev, pipe);
1665 1.1 riastrad
1666 1.9 riastrad vblank->inmodeset = 0;
1667 1.1 riastrad }
1668 1.1 riastrad }
1669 1.1 riastrad EXPORT_SYMBOL(drm_vblank_post_modeset);
1670 1.1 riastrad
1671 1.9 riastrad /*
1672 1.1 riastrad * drm_modeset_ctl - handle vblank event counter changes across mode switch
1673 1.1 riastrad * @DRM_IOCTL_ARGS: standard ioctl arguments
1674 1.1 riastrad *
1675 1.1 riastrad * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1676 1.1 riastrad * ioctls around modesetting so that any lost vblank events are accounted for.
1677 1.1 riastrad *
1678 1.1 riastrad * Generally the counter will reset across mode sets. If interrupts are
1679 1.1 riastrad * enabled around this call, we don't have to do anything since the counter
1680 1.1 riastrad * will have already been incremented.
1681 1.1 riastrad */
1682 1.1 riastrad int drm_modeset_ctl(struct drm_device *dev, void *data,
1683 1.1 riastrad struct drm_file *file_priv)
1684 1.1 riastrad {
1685 1.1 riastrad struct drm_modeset_ctl *modeset = data;
1686 1.9 riastrad unsigned int pipe;
1687 1.1 riastrad
1688 1.1 riastrad /* If drm_vblank_init() hasn't been called yet, just no-op */
1689 1.1 riastrad if (!dev->num_crtcs)
1690 1.1 riastrad return 0;
1691 1.1 riastrad
1692 1.1 riastrad /* KMS drivers handle this internally */
1693 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET))
1694 1.1 riastrad return 0;
1695 1.1 riastrad
1696 1.9 riastrad pipe = modeset->crtc;
1697 1.9 riastrad if (pipe >= dev->num_crtcs)
1698 1.1 riastrad return -EINVAL;
1699 1.1 riastrad
1700 1.1 riastrad switch (modeset->cmd) {
1701 1.1 riastrad case _DRM_PRE_MODESET:
1702 1.9 riastrad drm_vblank_pre_modeset(dev, pipe);
1703 1.1 riastrad break;
1704 1.1 riastrad case _DRM_POST_MODESET:
1705 1.9 riastrad drm_vblank_post_modeset(dev, pipe);
1706 1.1 riastrad break;
1707 1.1 riastrad default:
1708 1.1 riastrad return -EINVAL;
1709 1.1 riastrad }
1710 1.1 riastrad
1711 1.1 riastrad return 0;
1712 1.1 riastrad }
1713 1.1 riastrad
1714 1.9 riastrad static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
1715 1.1 riastrad union drm_wait_vblank *vblwait,
1716 1.1 riastrad struct drm_file *file_priv)
1717 1.1 riastrad {
1718 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1719 1.1 riastrad struct drm_pending_vblank_event *e;
1720 1.1 riastrad struct timeval now;
1721 1.1 riastrad unsigned long flags;
1722 1.1 riastrad unsigned int seq;
1723 1.1 riastrad int ret;
1724 1.1 riastrad
1725 1.9 riastrad e = kzalloc(sizeof(*e), GFP_KERNEL);
1726 1.1 riastrad if (e == NULL) {
1727 1.1 riastrad ret = -ENOMEM;
1728 1.1 riastrad goto err_put;
1729 1.1 riastrad }
1730 1.1 riastrad
1731 1.1 riastrad e->pipe = pipe;
1732 1.2 riastrad #ifdef __NetBSD__
1733 1.2 riastrad e->base.pid = curproc->p_pid;
1734 1.2 riastrad #else
1735 1.1 riastrad e->base.pid = current->pid;
1736 1.2 riastrad #endif
1737 1.1 riastrad e->event.base.type = DRM_EVENT_VBLANK;
1738 1.9 riastrad e->event.base.length = sizeof(e->event);
1739 1.1 riastrad e->event.user_data = vblwait->request.signal;
1740 1.1 riastrad e->base.event = &e->event.base;
1741 1.1 riastrad e->base.file_priv = file_priv;
1742 1.1 riastrad e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1743 1.1 riastrad
1744 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags);
1745 1.1 riastrad
1746 1.9 riastrad /*
1747 1.9 riastrad * drm_vblank_off() might have been called after we called
1748 1.9 riastrad * drm_vblank_get(). drm_vblank_off() holds event_lock
1749 1.9 riastrad * around the vblank disable, so no need for further locking.
1750 1.9 riastrad * The reference from drm_vblank_get() protects against
1751 1.9 riastrad * vblank disable from another source.
1752 1.9 riastrad */
1753 1.9 riastrad if (!vblank->enabled) {
1754 1.9 riastrad ret = -EINVAL;
1755 1.9 riastrad goto err_unlock;
1756 1.9 riastrad }
1757 1.9 riastrad
1758 1.9 riastrad if (file_priv->event_space < sizeof(e->event)) {
1759 1.1 riastrad ret = -EBUSY;
1760 1.1 riastrad goto err_unlock;
1761 1.1 riastrad }
1762 1.1 riastrad
1763 1.9 riastrad file_priv->event_space -= sizeof(e->event);
1764 1.1 riastrad seq = drm_vblank_count_and_time(dev, pipe, &now);
1765 1.1 riastrad
1766 1.1 riastrad if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1767 1.1 riastrad (seq - vblwait->request.sequence) <= (1 << 23)) {
1768 1.1 riastrad vblwait->request.sequence = seq + 1;
1769 1.1 riastrad vblwait->reply.sequence = vblwait->request.sequence;
1770 1.1 riastrad }
1771 1.1 riastrad
1772 1.9 riastrad DRM_DEBUG("event on vblank count %d, current %d, crtc %u\n",
1773 1.1 riastrad vblwait->request.sequence, seq, pipe);
1774 1.1 riastrad
1775 1.2 riastrad #ifdef __NetBSD__
1776 1.2 riastrad trace_drm_vblank_event_queued(curproc->p_pid, pipe,
1777 1.2 riastrad vblwait->request.sequence);
1778 1.2 riastrad #else
1779 1.1 riastrad trace_drm_vblank_event_queued(current->pid, pipe,
1780 1.1 riastrad vblwait->request.sequence);
1781 1.2 riastrad #endif
1782 1.1 riastrad
1783 1.1 riastrad e->event.sequence = vblwait->request.sequence;
1784 1.1 riastrad if ((seq - vblwait->request.sequence) <= (1 << 23)) {
1785 1.1 riastrad drm_vblank_put(dev, pipe);
1786 1.1 riastrad send_vblank_event(dev, e, seq, &now);
1787 1.1 riastrad vblwait->reply.sequence = seq;
1788 1.1 riastrad } else {
1789 1.1 riastrad /* drm_handle_vblank_events will call drm_vblank_put */
1790 1.1 riastrad list_add_tail(&e->base.link, &dev->vblank_event_list);
1791 1.1 riastrad vblwait->reply.sequence = vblwait->request.sequence;
1792 1.1 riastrad }
1793 1.1 riastrad
1794 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
1795 1.1 riastrad
1796 1.1 riastrad return 0;
1797 1.1 riastrad
1798 1.1 riastrad err_unlock:
1799 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
1800 1.1 riastrad kfree(e);
1801 1.1 riastrad err_put:
1802 1.1 riastrad drm_vblank_put(dev, pipe);
1803 1.1 riastrad return ret;
1804 1.1 riastrad }
1805 1.1 riastrad
1806 1.9 riastrad /*
1807 1.1 riastrad * Wait for VBLANK.
1808 1.1 riastrad *
1809 1.1 riastrad * \param inode device inode.
1810 1.1 riastrad * \param file_priv DRM file private.
1811 1.1 riastrad * \param cmd command.
1812 1.1 riastrad * \param data user argument, pointing to a drm_wait_vblank structure.
1813 1.1 riastrad * \return zero on success or a negative number on failure.
1814 1.1 riastrad *
1815 1.1 riastrad * This function enables the vblank interrupt on the pipe requested, then
1816 1.1 riastrad * sleeps waiting for the requested sequence number to occur, and drops
1817 1.9 riastrad * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
1818 1.1 riastrad * after a timeout with no further vblank waits scheduled).
1819 1.1 riastrad */
1820 1.1 riastrad int drm_wait_vblank(struct drm_device *dev, void *data,
1821 1.1 riastrad struct drm_file *file_priv)
1822 1.1 riastrad {
1823 1.9 riastrad struct drm_vblank_crtc *vblank;
1824 1.1 riastrad union drm_wait_vblank *vblwait = data;
1825 1.1 riastrad int ret;
1826 1.9 riastrad unsigned int flags, seq, pipe, high_pipe;
1827 1.1 riastrad
1828 1.9 riastrad if (!dev->irq_enabled)
1829 1.9 riastrad return -EINVAL;
1830 1.1 riastrad
1831 1.1 riastrad if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1832 1.1 riastrad return -EINVAL;
1833 1.1 riastrad
1834 1.1 riastrad if (vblwait->request.type &
1835 1.1 riastrad ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1836 1.1 riastrad _DRM_VBLANK_HIGH_CRTC_MASK)) {
1837 1.1 riastrad DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1838 1.1 riastrad vblwait->request.type,
1839 1.1 riastrad (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1840 1.1 riastrad _DRM_VBLANK_HIGH_CRTC_MASK));
1841 1.1 riastrad return -EINVAL;
1842 1.1 riastrad }
1843 1.1 riastrad
1844 1.1 riastrad flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1845 1.9 riastrad high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1846 1.9 riastrad if (high_pipe)
1847 1.9 riastrad pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1848 1.1 riastrad else
1849 1.9 riastrad pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1850 1.9 riastrad if (pipe >= dev->num_crtcs)
1851 1.1 riastrad return -EINVAL;
1852 1.1 riastrad
1853 1.9 riastrad vblank = &dev->vblank[pipe];
1854 1.9 riastrad
1855 1.9 riastrad ret = drm_vblank_get(dev, pipe);
1856 1.1 riastrad if (ret) {
1857 1.1 riastrad DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
1858 1.1 riastrad return ret;
1859 1.1 riastrad }
1860 1.9 riastrad seq = drm_vblank_count(dev, pipe);
1861 1.1 riastrad
1862 1.1 riastrad switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1863 1.1 riastrad case _DRM_VBLANK_RELATIVE:
1864 1.1 riastrad vblwait->request.sequence += seq;
1865 1.1 riastrad vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1866 1.1 riastrad case _DRM_VBLANK_ABSOLUTE:
1867 1.1 riastrad break;
1868 1.1 riastrad default:
1869 1.1 riastrad ret = -EINVAL;
1870 1.1 riastrad goto done;
1871 1.1 riastrad }
1872 1.1 riastrad
1873 1.1 riastrad if (flags & _DRM_VBLANK_EVENT) {
1874 1.1 riastrad /* must hold on to the vblank ref until the event fires
1875 1.1 riastrad * drm_vblank_put will be called asynchronously
1876 1.1 riastrad */
1877 1.9 riastrad return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
1878 1.1 riastrad }
1879 1.1 riastrad
1880 1.1 riastrad if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1881 1.1 riastrad (seq - vblwait->request.sequence) <= (1<<23)) {
1882 1.1 riastrad vblwait->request.sequence = seq + 1;
1883 1.1 riastrad }
1884 1.1 riastrad
1885 1.9 riastrad DRM_DEBUG("waiting on vblank count %d, crtc %u\n",
1886 1.9 riastrad vblwait->request.sequence, pipe);
1887 1.9 riastrad vblank->last_wait = vblwait->request.sequence;
1888 1.2 riastrad #ifdef __NetBSD__
1889 1.2 riastrad {
1890 1.2 riastrad unsigned long irqflags;
1891 1.8 riastrad
1892 1.2 riastrad spin_lock_irqsave(&dev->vbl_lock, irqflags);
1893 1.9 riastrad DRM_SPIN_WAIT_ON(ret, &vblank->queue, &dev->vbl_lock,
1894 1.8 riastrad 3 * HZ,
1895 1.9 riastrad (((drm_vblank_count(dev, pipe) -
1896 1.2 riastrad vblwait->request.sequence) <= (1 << 23)) ||
1897 1.9 riastrad !vblank->enabled ||
1898 1.2 riastrad !dev->irq_enabled));
1899 1.2 riastrad spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1900 1.2 riastrad }
1901 1.2 riastrad #else
1902 1.9 riastrad DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
1903 1.9 riastrad (((drm_vblank_count(dev, pipe) -
1904 1.1 riastrad vblwait->request.sequence) <= (1 << 23)) ||
1905 1.9 riastrad !vblank->enabled ||
1906 1.1 riastrad !dev->irq_enabled));
1907 1.2 riastrad #endif
1908 1.1 riastrad
1909 1.1 riastrad if (ret != -EINTR) {
1910 1.1 riastrad struct timeval now;
1911 1.1 riastrad
1912 1.9 riastrad vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
1913 1.1 riastrad vblwait->reply.tval_sec = now.tv_sec;
1914 1.1 riastrad vblwait->reply.tval_usec = now.tv_usec;
1915 1.1 riastrad
1916 1.1 riastrad DRM_DEBUG("returning %d to client\n",
1917 1.1 riastrad vblwait->reply.sequence);
1918 1.1 riastrad } else {
1919 1.1 riastrad DRM_DEBUG("vblank wait interrupted by signal\n");
1920 1.1 riastrad }
1921 1.1 riastrad
1922 1.1 riastrad done:
1923 1.9 riastrad drm_vblank_put(dev, pipe);
1924 1.1 riastrad return ret;
1925 1.1 riastrad }
1926 1.1 riastrad
1927 1.9 riastrad static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1928 1.1 riastrad {
1929 1.1 riastrad struct drm_pending_vblank_event *e, *t;
1930 1.1 riastrad struct timeval now;
1931 1.1 riastrad unsigned int seq;
1932 1.1 riastrad
1933 1.9 riastrad assert_spin_locked(&dev->event_lock);
1934 1.1 riastrad
1935 1.9 riastrad seq = drm_vblank_count_and_time(dev, pipe, &now);
1936 1.1 riastrad
1937 1.1 riastrad list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1938 1.9 riastrad if (e->pipe != pipe)
1939 1.1 riastrad continue;
1940 1.1 riastrad if ((seq - e->event.sequence) > (1<<23))
1941 1.1 riastrad continue;
1942 1.1 riastrad
1943 1.1 riastrad DRM_DEBUG("vblank event on %d, current %d\n",
1944 1.1 riastrad e->event.sequence, seq);
1945 1.1 riastrad
1946 1.1 riastrad list_del(&e->base.link);
1947 1.9 riastrad drm_vblank_put(dev, pipe);
1948 1.1 riastrad send_vblank_event(dev, e, seq, &now);
1949 1.1 riastrad }
1950 1.1 riastrad
1951 1.9 riastrad trace_drm_vblank_event(pipe, seq);
1952 1.1 riastrad }
1953 1.1 riastrad
1954 1.1 riastrad /**
1955 1.1 riastrad * drm_handle_vblank - handle a vblank event
1956 1.1 riastrad * @dev: DRM device
1957 1.9 riastrad * @pipe: index of CRTC where this event occurred
1958 1.1 riastrad *
1959 1.1 riastrad * Drivers should call this routine in their vblank interrupt handlers to
1960 1.1 riastrad * update the vblank counter and send any signals that may be pending.
1961 1.9 riastrad *
1962 1.9 riastrad * This is the legacy version of drm_crtc_handle_vblank().
1963 1.1 riastrad */
1964 1.9 riastrad bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1965 1.1 riastrad {
1966 1.9 riastrad struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1967 1.1 riastrad unsigned long irqflags;
1968 1.2 riastrad #ifdef __NetBSD__ /* XXX vblank locking */
1969 1.2 riastrad unsigned long irqflags_vbl_lock;
1970 1.2 riastrad #endif
1971 1.1 riastrad
1972 1.9 riastrad if (WARN_ON_ONCE(!dev->num_crtcs))
1973 1.9 riastrad return false;
1974 1.9 riastrad
1975 1.9 riastrad if (WARN_ON(pipe >= dev->num_crtcs))
1976 1.1 riastrad return false;
1977 1.1 riastrad
1978 1.9 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
1979 1.2 riastrad #ifdef __NetBSD__ /* XXX vblank locking */
1980 1.2 riastrad spin_lock_irqsave(&dev->vbl_lock, irqflags_vbl_lock);
1981 1.2 riastrad #endif
1982 1.2 riastrad
1983 1.1 riastrad /* Need timestamp lock to prevent concurrent execution with
1984 1.1 riastrad * vblank enable/disable, as this would cause inconsistent
1985 1.1 riastrad * or corrupted timestamps and vblank counts.
1986 1.1 riastrad */
1987 1.9 riastrad spin_lock(&dev->vblank_time_lock);
1988 1.1 riastrad
1989 1.1 riastrad /* Vblank irq handling disabled. Nothing to do. */
1990 1.9 riastrad if (!vblank->enabled) {
1991 1.9 riastrad spin_unlock(&dev->vblank_time_lock);
1992 1.9 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
1993 1.2 riastrad #ifdef __NetBSD__ /* XXX vblank locking */
1994 1.2 riastrad spin_unlock_irqrestore(&dev->vbl_lock, irqflags_vbl_lock);
1995 1.2 riastrad #endif
1996 1.1 riastrad return false;
1997 1.1 riastrad }
1998 1.1 riastrad
1999 1.9 riastrad drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
2000 1.1 riastrad
2001 1.9 riastrad spin_unlock(&dev->vblank_time_lock);
2002 1.1 riastrad
2003 1.2 riastrad #ifdef __NetBSD__
2004 1.9 riastrad DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->vbl_lock);
2005 1.2 riastrad #else
2006 1.9 riastrad wake_up(&vblank->queue);
2007 1.2 riastrad #endif
2008 1.9 riastrad drm_handle_vblank_events(dev, pipe);
2009 1.9 riastrad
2010 1.9 riastrad /* With instant-off, we defer disabling the interrupt until after
2011 1.9 riastrad * we finish processing the following vblank. The disable has to
2012 1.9 riastrad * be last (after drm_handle_vblank_events) so that the timestamp
2013 1.9 riastrad * is always accurate.
2014 1.9 riastrad */
2015 1.9 riastrad if (dev->vblank_disable_immediate &&
2016 1.9 riastrad drm_vblank_offdelay > 0 &&
2017 1.9 riastrad !atomic_read(&vblank->refcount))
2018 1.9 riastrad vblank_disable_fn((unsigned long)vblank);
2019 1.1 riastrad
2020 1.9 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
2021 1.2 riastrad #ifdef __NetBSD__ /* XXX vblank locking */
2022 1.2 riastrad spin_unlock_irqrestore(&dev->vbl_lock, irqflags_vbl_lock);
2023 1.2 riastrad #endif
2024 1.9 riastrad
2025 1.1 riastrad return true;
2026 1.1 riastrad }
2027 1.1 riastrad EXPORT_SYMBOL(drm_handle_vblank);
2028 1.9 riastrad
2029 1.9 riastrad /**
2030 1.9 riastrad * drm_crtc_handle_vblank - handle a vblank event
2031 1.9 riastrad * @crtc: where this event occurred
2032 1.9 riastrad *
2033 1.9 riastrad * Drivers should call this routine in their vblank interrupt handlers to
2034 1.9 riastrad * update the vblank counter and send any signals that may be pending.
2035 1.9 riastrad *
2036 1.9 riastrad * This is the native KMS version of drm_handle_vblank().
2037 1.9 riastrad *
2038 1.9 riastrad * Returns:
2039 1.9 riastrad * True if the event was successfully handled, false on failure.
2040 1.9 riastrad */
2041 1.9 riastrad bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
2042 1.9 riastrad {
2043 1.9 riastrad return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
2044 1.9 riastrad }
2045 1.9 riastrad EXPORT_SYMBOL(drm_crtc_handle_vblank);
2046 1.9 riastrad
2047 1.9 riastrad /**
2048 1.9 riastrad * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
2049 1.9 riastrad * @dev: DRM device
2050 1.9 riastrad * @pipe: CRTC for which to read the counter
2051 1.9 riastrad *
2052 1.9 riastrad * Drivers can plug this into the .get_vblank_counter() function if
2053 1.9 riastrad * there is no useable hardware frame counter available.
2054 1.9 riastrad *
2055 1.9 riastrad * Returns:
2056 1.9 riastrad * 0
2057 1.9 riastrad */
2058 1.9 riastrad u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
2059 1.9 riastrad {
2060 1.9 riastrad return 0;
2061 1.9 riastrad }
2062 1.9 riastrad EXPORT_SYMBOL(drm_vblank_no_hw_counter);
2063