40 #include <linux/slab.h>
43 #include <linux/export.h>
46 #define vblanktimestamp(dev, crtc, count) ( \
47 (dev)->_vblank_time[(crtc) * DRM_VBLANKTIME_RBSIZE + \
48 ((count) % DRM_VBLANKTIME_RBSIZE)])
53 #define DRM_TIMESTAMP_MAXRETRIES 3
58 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
74 struct drm_file *file_priv)
78 if (!dev->driver->bus->irq_by_busid)
81 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
84 return dev->driver->bus->irq_by_busid(dev, p);
92 memset(&dev->_vblank_time[crtc * DRM_VBLANKTIME_RBSIZE], 0,
93 DRM_VBLANKTIME_RBSIZE *
sizeof(
struct timeval));
104 unsigned long irqflags;
116 dev->driver->disable_vblank(dev, crtc);
117 dev->vblank_enabled[crtc] = 0;
132 dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
134 }
while (dev->last_vblank[crtc] != dev->driver->get_vblank_counter(dev, crtc));
140 diff_ns = timeval_to_ns(&tvblank) -
155 if ((vblrc > 0) && (
abs64(diff_ns) > 1000000)) {
161 clear_vblank_timestamps(dev, crtc);
163 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
166 static void vblank_disable_fn(
unsigned long arg)
169 unsigned long irqflags;
172 if (!dev->vblank_disable_allowed)
175 for (i = 0; i < dev->num_crtcs; i++) {
178 dev->vblank_enabled[i]) {
179 DRM_DEBUG(
"disabling vblank on crtc %d\n", i);
180 vblank_disable_and_save(dev, i);
182 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
189 if (dev->num_crtcs == 0)
194 vblank_disable_fn((
unsigned long)dev);
196 kfree(dev->vbl_queue);
197 kfree(dev->_vblank_count);
198 kfree(dev->vblank_refcount);
199 kfree(dev->vblank_enabled);
200 kfree(dev->last_vblank);
201 kfree(dev->last_vblank_wait);
202 kfree(dev->vblank_inmodeset);
203 kfree(dev->_vblank_time);
213 setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
218 dev->num_crtcs = num_crtcs;
226 if (!dev->_vblank_count)
231 if (!dev->vblank_refcount)
234 dev->vblank_enabled = kcalloc(num_crtcs,
sizeof(
int),
GFP_KERNEL);
235 if (!dev->vblank_enabled)
238 dev->last_vblank = kcalloc(num_crtcs,
sizeof(
u32),
GFP_KERNEL);
239 if (!dev->last_vblank)
242 dev->last_vblank_wait = kcalloc(num_crtcs,
sizeof(
u32),
GFP_KERNEL);
243 if (!dev->last_vblank_wait)
246 dev->vblank_inmodeset = kcalloc(num_crtcs,
sizeof(
int),
GFP_KERNEL);
247 if (!dev->vblank_inmodeset)
250 dev->_vblank_time = kcalloc(num_crtcs * DRM_VBLANKTIME_RBSIZE,
252 if (!dev->_vblank_time)
255 DRM_INFO(
"Supports vblank timestamp caching Rev 1 (10.10.2010).\n");
258 if (dev->driver->get_vblank_timestamp)
259 DRM_INFO(
"Driver supports precise vblank timestamp query.\n");
261 DRM_INFO(
"No driver support for vblank timestamp query.\n");
264 for (i = 0; i < num_crtcs; i++) {
270 dev->vblank_disable_allowed = 0;
279 static void drm_irq_vgaarb_nokms(
void *
cookie,
bool state)
283 if (dev->driver->vgaarb_irq) {
284 dev->driver->vgaarb_irq(dev, state);
288 if (!dev->irq_enabled)
292 if (dev->driver->irq_uninstall)
293 dev->driver->irq_uninstall(dev);
295 if (dev->driver->irq_preinstall)
296 dev->driver->irq_preinstall(dev);
297 if (dev->driver->irq_postinstall)
298 dev->driver->irq_postinstall(dev);
314 unsigned long sh_flags = 0;
317 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
320 if (drm_dev_to_irq(dev) == 0)
326 if (!dev->dev_private) {
331 if (dev->irq_enabled) {
335 dev->irq_enabled = 1;
338 DRM_DEBUG(
"irq=%d\n", drm_dev_to_irq(dev));
341 if (dev->driver->irq_preinstall)
342 dev->driver->irq_preinstall(dev);
345 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
349 irqname = dev->devname;
351 irqname = dev->driver->name;
353 ret =
request_irq(drm_dev_to_irq(dev), dev->driver->irq_handler,
354 sh_flags, irqname, dev);
358 dev->irq_enabled = 0;
363 if (!drm_core_check_feature(dev, DRIVER_MODESET))
367 if (dev->driver->irq_postinstall)
368 ret = dev->driver->irq_postinstall(dev);
372 dev->irq_enabled = 0;
374 if (!drm_core_check_feature(dev, DRIVER_MODESET))
392 unsigned long irqflags;
395 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
399 irq_enabled = dev->irq_enabled;
400 dev->irq_enabled = 0;
406 if (dev->num_crtcs) {
408 for (i = 0; i < dev->num_crtcs; i++) {
410 dev->vblank_enabled[
i] = 0;
411 dev->last_vblank[
i] =
412 dev->driver->get_vblank_counter(dev, i);
414 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
420 DRM_DEBUG(
"irq=%d\n", drm_dev_to_irq(dev));
422 if (!drm_core_check_feature(dev, DRIVER_MODESET))
425 if (dev->driver->irq_uninstall)
426 dev->driver->irq_uninstall(dev);
446 struct drm_file *file_priv)
457 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
459 if (drm_core_check_feature(dev, DRIVER_MODESET))
461 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
462 ctl->
irq != drm_dev_to_irq(dev))
466 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
468 if (drm_core_check_feature(dev, DRIVER_MODESET))
490 s64 linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
494 dotclock = (
u64) crtc->
hwmode.clock * 1000;
508 pixeldur_ns = (
s64) div64_u64(1000000000, dotclock);
509 linedur_ns = (
s64) div64_u64(((
u64) crtc->
hwmode.crtc_htotal *
510 1000000000), dotclock);
511 framedur_ns = (
s64) crtc->
hwmode.crtc_vtotal * linedur_ns;
513 DRM_ERROR(
"crtc %d: Can't calculate constants, dotclock = 0!\n",
520 DRM_DEBUG(
"crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
523 DRM_DEBUG(
"crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
524 crtc->
base.id, (
int) dotclock/1000, (
int) framedur_ns,
525 (
int) linedur_ns, (
int) pixeldur_ns);
579 struct timeval stime, raw_time;
583 s64 framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
587 DRM_ERROR(
"Invalid crtc %d\n", crtc);
592 if (!dev->driver->get_scanout_position) {
593 DRM_ERROR(
"Called from driver w/o get_scanout_position()!?\n");
609 if (vtotal <= 0 || vdisplay <= 0 || framedur_ns == 0) {
610 DRM_DEBUG(
"crtc %d: Noop due to uninitialized mode.\n", crtc);
631 vbl_status = dev->driver->get_scanout_position(dev, crtc, &vpos, &hpos);
639 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
640 DRM_DEBUG(
"crtc %d : scanoutpos query failed [%d].\n",
645 duration_ns = timeval_to_ns(&raw_time) - timeval_to_ns(&stime);
648 if (duration_ns <= (
s64) *max_error)
653 if (i == DRM_TIMESTAMP_MAXRETRIES) {
654 DRM_DEBUG(
"crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
655 crtc, (
int) duration_ns/1000, *max_error/1000, i);
659 *max_error = (
int) duration_ns;
666 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
672 delta_ns = (
s64) vpos * linedur_ns + (
s64) hpos * pixeldur_ns;
684 if ((flags & DRM_CALLED_FROM_VBLIRQ) && !invbl &&
685 ((vdisplay -
vpos) < vtotal / 100)) {
686 delta_ns = delta_ns - framedur_ns;
695 *vblank_time =
ns_to_timeval(timeval_to_ns(&raw_time) - delta_ns);
697 DRM_DEBUG(
"crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
698 crtc, (
int)vbl_status, hpos, vpos,
701 (
int)duration_ns/1000, i);
703 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
705 vbl_status |= DRM_VBLANKTIME_INVBL;
740 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
741 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
796 cur_vblank =
atomic_read(&dev->_vblank_count[crtc]);
799 }
while (cur_vblank !=
atomic_read(&dev->_vblank_count[crtc]));
821 static void drm_update_vblank_count(
struct drm_device *dev,
int crtc)
823 u32 cur_vblank, diff, tslot,
rc;
839 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
841 }
while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
844 diff = cur_vblank - dev->last_vblank[crtc];
845 if (cur_vblank < dev->last_vblank[crtc]) {
846 diff += dev->max_vblank_count;
848 DRM_DEBUG(
"last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
849 crtc, dev->last_vblank[crtc], cur_vblank, diff);
852 DRM_DEBUG(
"enabling vblank interrupts on crtc %d, missed %d\n",
860 tslot =
atomic_read(&dev->_vblank_count[crtc]) + diff;
882 unsigned long irqflags, irqflags2;
889 if (!dev->vblank_enabled[crtc]) {
896 ret = dev->driver->enable_vblank(dev, crtc);
897 DRM_DEBUG(
"enabling vblank on crtc %d, ret: %d\n",
902 dev->vblank_enabled[crtc] = 1;
903 drm_update_vblank_count(dev, crtc);
906 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags2);
908 if (!dev->vblank_enabled[crtc]) {
913 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
941 struct drm_pending_vblank_event *
e, *
t;
943 unsigned long irqflags;
947 vblank_disable_and_save(dev, crtc);
955 DRM_DEBUG(
"Sending premature vblank event on disable: \
956 wanted %d, current %d\n",
957 e->event.sequence, seq);
959 e->event.sequence = seq;
960 e->event.tv_sec = now.
tv_sec;
961 e->event.tv_usec = now.
tv_usec;
963 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
965 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
969 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
993 if (!dev->vblank_inmodeset[crtc]) {
994 dev->vblank_inmodeset[crtc] = 0x1;
996 dev->vblank_inmodeset[crtc] |= 0x2;
1003 unsigned long irqflags;
1005 if (dev->vblank_inmodeset[crtc]) {
1007 dev->vblank_disable_allowed = 1;
1008 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1010 if (dev->vblank_inmodeset[crtc] & 0x2)
1013 dev->vblank_inmodeset[crtc] = 0;
1030 struct drm_file *file_priv)
1036 if (!dev->num_crtcs)
1040 if (drm_core_check_feature(dev, DRIVER_MODESET))
1043 crtc = modeset->
crtc;
1044 if (crtc >= dev->num_crtcs)
1047 switch (modeset->
cmd) {
1061 static int drm_queue_vblank_event(
struct drm_device *dev,
int pipe,
1063 struct drm_file *file_priv)
1065 struct drm_pending_vblank_event *
e;
1067 unsigned long flags;
1080 e->event.base.length =
sizeof e->event;
1081 e->event.user_data = vblwait->
request.signal;
1082 e->base.event = &e->event.base;
1083 e->base.file_priv = file_priv;
1084 e->base.destroy = (
void (*) (
struct drm_pending_event *))
kfree;
1088 if (file_priv->event_space <
sizeof e->event) {
1093 file_priv->event_space -=
sizeof e->event;
1097 (seq - vblwait->
request.sequence) <= (1 << 23)) {
1098 vblwait->
request.sequence = seq + 1;
1102 DRM_DEBUG(
"event on vblank count %d, current %d, crtc %d\n",
1103 vblwait->
request.sequence, seq, pipe);
1105 trace_drm_vblank_event_queued(
current->pid, pipe,
1108 e->event.sequence = vblwait->
request.sequence;
1109 if ((seq - vblwait->
request.sequence) <= (1 << 23)) {
1110 e->event.sequence = seq;
1111 e->event.tv_sec = now.tv_sec;
1112 e->event.tv_usec = now.tv_usec;
1114 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
1116 vblwait->
reply.sequence = seq;
1117 trace_drm_vblank_event_delivered(
current->pid, pipe,
1125 spin_unlock_irqrestore(&dev->event_lock, flags);
1130 spin_unlock_irqrestore(&dev->event_lock, flags);
1152 struct drm_file *file_priv)
1156 unsigned int flags, seq, crtc, high_crtc;
1158 if ((!drm_dev_to_irq(dev)) || (!dev->irq_enabled))
1167 DRM_ERROR(
"Unsupported type value 0x%x, supported mask 0x%x\n",
1180 if (crtc >= dev->num_crtcs)
1185 DRM_DEBUG(
"failed to acquire vblank counter, %d\n", ret);
1192 vblwait->
request.sequence += seq;
1205 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
1209 (seq - vblwait->
request.sequence) <= (1<<23)) {
1210 vblwait->
request.sequence = seq + 1;
1213 DRM_DEBUG(
"waiting on vblank count %d, crtc %d\n",
1214 vblwait->
request.sequence, crtc);
1215 dev->last_vblank_wait[crtc] = vblwait->
request.sequence;
1218 vblwait->
request.sequence) <= (1 << 23)) ||
1219 !dev->irq_enabled));
1221 if (ret != -
EINTR) {
1228 DRM_DEBUG(
"returning %d to client\n",
1229 vblwait->
reply.sequence);
1231 DRM_DEBUG(
"vblank wait interrupted by signal\n");
1239 static void drm_handle_vblank_events(
struct drm_device *dev,
int crtc)
1241 struct drm_pending_vblank_event *
e, *
t;
1243 unsigned long flags;
1251 if (e->pipe != crtc)
1253 if ((seq - e->event.sequence) > (1<<23))
1256 DRM_DEBUG(
"vblank event on %d, current %d\n",
1257 e->event.sequence, seq);
1259 e->event.sequence = seq;
1260 e->event.tv_sec = now.tv_sec;
1261 e->event.tv_usec = now.tv_usec;
1263 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1265 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
1269 spin_unlock_irqrestore(&dev->event_lock, flags);
1271 trace_drm_vblank_event(crtc, seq);
1287 unsigned long irqflags;
1289 if (!dev->num_crtcs)
1299 if (!dev->vblank_enabled[crtc]) {
1300 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
1309 vblcount =
atomic_read(&dev->_vblank_count[crtc]);
1313 diff_ns = timeval_to_ns(&tvblank) -
1336 DRM_DEBUG(
"crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1337 crtc, (
int) diff_ns);
1341 drm_handle_vblank_events(dev, crtc);
1343 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);