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