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