Skip to content

Commit 3064abf

Browse files
keith-packardairlied
authored andcommitted
drm: Add CRTC_GET_SEQUENCE and CRTC_QUEUE_SEQUENCE ioctls [v3]
These provide crtc-id based functions instead of pipe-number, while also offering higher resolution time (ns) and wider frame count (64) as required by the Vulkan API. v2: * Check for DRIVER_MODESET in new crtc-based vblank ioctls Failing to check this will oops the driver. * Ensure vblank interupt is running in crtc_get_sequence ioctl The sequence and timing values are not correct while the interrupt is off, so make sure it's running before asking for them. * Short-circuit get_sequence if the counter is enabled and accurate Steal the idea from the code in wait_vblank to avoid the expense of drm_vblank_get/put * Return active state of crtc in crtc_get_sequence ioctl Might be useful for applications that aren't in charge of modesetting? * Use drm_crtc_vblank_get/put in new crtc-based vblank sequence ioctls Daniel Vetter prefers these over the old drm_vblank_put/get APIs. * Return s64 ns instead of u64 in new sequence event Suggested-by: Daniel Vetter <daniel@ffwll.ch> Suggested-by: Ville Syrjälä <ville.syrjala@linux.intel.com> v3: * Removed FIRST_PIXEL_OUT_FLAG * Document that the timestamp in the query and event are that of the first pixel leaving the display engine for the display (using the same wording as the Vulkan spec). Suggested-by: Michel Dänzer <michel@daenzer.net> Acked-by: Dave Airlie <airlied@redhat.com> [airlied: left->leaves (Michel)] Signed-off-by: Keith Packard <keithp@keithp.com> Reviewed-by: Sean Paul <seanpaul@chromium.org> Signed-off-by: Dave Airlie <airlied@redhat.com>
1 parent bd386e5 commit 3064abf

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

drivers/gpu/drm/drm_internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data,
7070
int drm_legacy_irq_control(struct drm_device *dev, void *data,
7171
struct drm_file *file_priv);
7272

73+
int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
74+
struct drm_file *filp);
75+
76+
int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
77+
struct drm_file *filp);
78+
7379
/* drm_auth.c */
7480
int drm_getmagic(struct drm_device *dev, void *data,
7581
struct drm_file *file_priv);

drivers/gpu/drm/drm_ioctl.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,8 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
663663
DRM_UNLOCKED|DRM_RENDER_ALLOW),
664664
DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_SIGNAL, drm_syncobj_signal_ioctl,
665665
DRM_UNLOCKED|DRM_RENDER_ALLOW),
666+
DRM_IOCTL_DEF(DRM_IOCTL_CRTC_GET_SEQUENCE, drm_crtc_get_sequence_ioctl, DRM_UNLOCKED),
667+
DRM_IOCTL_DEF(DRM_IOCTL_CRTC_QUEUE_SEQUENCE, drm_crtc_queue_sequence_ioctl, DRM_UNLOCKED),
666668
};
667669

668670
#define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )

drivers/gpu/drm/drm_vblank.c

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,11 @@ static void send_vblank_event(struct drm_device *dev,
819819
e->event.vbl.tv_sec = tv.tv_sec;
820820
e->event.vbl.tv_usec = tv.tv_nsec / 1000;
821821
break;
822+
case DRM_EVENT_CRTC_SEQUENCE:
823+
if (seq)
824+
e->event.seq.sequence = seq;
825+
e->event.seq.time_ns = ktime_to_ns(now);
826+
break;
822827
}
823828
trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
824829
drm_send_event_locked(dev, &e->base);
@@ -1650,3 +1655,166 @@ bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
16501655
return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
16511656
}
16521657
EXPORT_SYMBOL(drm_crtc_handle_vblank);
1658+
1659+
/*
1660+
* Get crtc VBLANK count.
1661+
*
1662+
* \param dev DRM device
1663+
* \param data user arguement, pointing to a drm_crtc_get_sequence structure.
1664+
* \param file_priv drm file private for the user's open file descriptor
1665+
*/
1666+
1667+
int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
1668+
struct drm_file *file_priv)
1669+
{
1670+
struct drm_crtc *crtc;
1671+
struct drm_vblank_crtc *vblank;
1672+
int pipe;
1673+
struct drm_crtc_get_sequence *get_seq = data;
1674+
ktime_t now;
1675+
bool vblank_enabled;
1676+
int ret;
1677+
1678+
if (!drm_core_check_feature(dev, DRIVER_MODESET))
1679+
return -EINVAL;
1680+
1681+
if (!dev->irq_enabled)
1682+
return -EINVAL;
1683+
1684+
crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id);
1685+
if (!crtc)
1686+
return -ENOENT;
1687+
1688+
pipe = drm_crtc_index(crtc);
1689+
1690+
vblank = &dev->vblank[pipe];
1691+
vblank_enabled = dev->vblank_disable_immediate && READ_ONCE(vblank->enabled);
1692+
1693+
if (!vblank_enabled) {
1694+
ret = drm_crtc_vblank_get(crtc);
1695+
if (ret) {
1696+
DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
1697+
return ret;
1698+
}
1699+
}
1700+
drm_modeset_lock(&crtc->mutex, NULL);
1701+
if (crtc->state)
1702+
get_seq->active = crtc->state->enable;
1703+
else
1704+
get_seq->active = crtc->enabled;
1705+
drm_modeset_unlock(&crtc->mutex);
1706+
get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now);
1707+
get_seq->sequence_ns = ktime_to_ns(now);
1708+
if (!vblank_enabled)
1709+
drm_crtc_vblank_put(crtc);
1710+
return 0;
1711+
}
1712+
1713+
/*
1714+
* Queue a event for VBLANK sequence
1715+
*
1716+
* \param dev DRM device
1717+
* \param data user arguement, pointing to a drm_crtc_queue_sequence structure.
1718+
* \param file_priv drm file private for the user's open file descriptor
1719+
*/
1720+
1721+
int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
1722+
struct drm_file *file_priv)
1723+
{
1724+
struct drm_crtc *crtc;
1725+
struct drm_vblank_crtc *vblank;
1726+
int pipe;
1727+
struct drm_crtc_queue_sequence *queue_seq = data;
1728+
ktime_t now;
1729+
struct drm_pending_vblank_event *e;
1730+
u32 flags;
1731+
u64 seq;
1732+
u64 req_seq;
1733+
int ret;
1734+
unsigned long spin_flags;
1735+
1736+
if (!drm_core_check_feature(dev, DRIVER_MODESET))
1737+
return -EINVAL;
1738+
1739+
if (!dev->irq_enabled)
1740+
return -EINVAL;
1741+
1742+
crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id);
1743+
if (!crtc)
1744+
return -ENOENT;
1745+
1746+
flags = queue_seq->flags;
1747+
/* Check valid flag bits */
1748+
if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE|
1749+
DRM_CRTC_SEQUENCE_NEXT_ON_MISS))
1750+
return -EINVAL;
1751+
1752+
pipe = drm_crtc_index(crtc);
1753+
1754+
vblank = &dev->vblank[pipe];
1755+
1756+
e = kzalloc(sizeof(*e), GFP_KERNEL);
1757+
if (e == NULL)
1758+
return -ENOMEM;
1759+
1760+
ret = drm_crtc_vblank_get(crtc);
1761+
if (ret) {
1762+
DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
1763+
goto err_free;
1764+
}
1765+
1766+
seq = drm_vblank_count_and_time(dev, pipe, &now);
1767+
req_seq = queue_seq->sequence;
1768+
1769+
if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
1770+
req_seq += seq;
1771+
1772+
if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && vblank_passed(seq, req_seq))
1773+
req_seq = seq + 1;
1774+
1775+
e->pipe = pipe;
1776+
e->event.base.type = DRM_EVENT_CRTC_SEQUENCE;
1777+
e->event.base.length = sizeof(e->event.seq);
1778+
e->event.seq.user_data = queue_seq->user_data;
1779+
1780+
spin_lock_irqsave(&dev->event_lock, spin_flags);
1781+
1782+
/*
1783+
* drm_crtc_vblank_off() might have been called after we called
1784+
* drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1785+
* vblank disable, so no need for further locking. The reference from
1786+
* drm_crtc_vblank_get() protects against vblank disable from another source.
1787+
*/
1788+
if (!READ_ONCE(vblank->enabled)) {
1789+
ret = -EINVAL;
1790+
goto err_unlock;
1791+
}
1792+
1793+
ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1794+
&e->event.base);
1795+
1796+
if (ret)
1797+
goto err_unlock;
1798+
1799+
e->sequence = req_seq;
1800+
1801+
if (vblank_passed(seq, req_seq)) {
1802+
drm_crtc_vblank_put(crtc);
1803+
send_vblank_event(dev, e, seq, now);
1804+
queue_seq->sequence = seq;
1805+
} else {
1806+
/* drm_handle_vblank_events will call drm_vblank_put */
1807+
list_add_tail(&e->base.link, &dev->vblank_event_list);
1808+
queue_seq->sequence = req_seq;
1809+
}
1810+
1811+
spin_unlock_irqrestore(&dev->event_lock, spin_flags);
1812+
return 0;
1813+
1814+
err_unlock:
1815+
spin_unlock_irqrestore(&dev->event_lock, spin_flags);
1816+
drm_crtc_vblank_put(crtc);
1817+
err_free:
1818+
kfree(e);
1819+
return ret;
1820+
}

include/drm/drm_vblank.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct drm_pending_vblank_event {
5757
union {
5858
struct drm_event base;
5959
struct drm_event_vblank vbl;
60+
struct drm_event_crtc_sequence seq;
6061
} event;
6162
};
6263

include/uapi/drm/drm.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,28 @@ struct drm_syncobj_array {
737737
__u32 pad;
738738
};
739739

740+
/* Query current scanout sequence number */
741+
struct drm_crtc_get_sequence {
742+
__u32 crtc_id; /* requested crtc_id */
743+
__u32 active; /* return: crtc output is active */
744+
__u64 sequence; /* return: most recent vblank sequence */
745+
__s64 sequence_ns; /* return: most recent time of first pixel out */
746+
};
747+
748+
/* Queue event to be delivered at specified sequence. Time stamp marks
749+
* when the first pixel of the refresh cycle leaves the display engine
750+
* for the display
751+
*/
752+
#define DRM_CRTC_SEQUENCE_RELATIVE 0x00000001 /* sequence is relative to current */
753+
#define DRM_CRTC_SEQUENCE_NEXT_ON_MISS 0x00000002 /* Use next sequence if we've missed */
754+
755+
struct drm_crtc_queue_sequence {
756+
__u32 crtc_id;
757+
__u32 flags;
758+
__u64 sequence; /* on input, target sequence. on output, actual sequence */
759+
__u64 user_data; /* user data passed to event */
760+
};
761+
740762
#if defined(__cplusplus)
741763
}
742764
#endif
@@ -819,6 +841,9 @@ extern "C" {
819841

820842
#define DRM_IOCTL_WAIT_VBLANK DRM_IOWR(0x3a, union drm_wait_vblank)
821843

844+
#define DRM_IOCTL_CRTC_GET_SEQUENCE DRM_IOWR(0x3b, struct drm_crtc_get_sequence)
845+
#define DRM_IOCTL_CRTC_QUEUE_SEQUENCE DRM_IOWR(0x3c, struct drm_crtc_queue_sequence)
846+
822847
#define DRM_IOCTL_UPDATE_DRAW DRM_IOW(0x3f, struct drm_update_draw)
823848

824849
#define DRM_IOCTL_MODE_GETRESOURCES DRM_IOWR(0xA0, struct drm_mode_card_res)
@@ -893,6 +918,7 @@ struct drm_event {
893918

894919
#define DRM_EVENT_VBLANK 0x01
895920
#define DRM_EVENT_FLIP_COMPLETE 0x02
921+
#define DRM_EVENT_CRTC_SEQUENCE 0x03
896922

897923
struct drm_event_vblank {
898924
struct drm_event base;
@@ -903,6 +929,16 @@ struct drm_event_vblank {
903929
__u32 crtc_id; /* 0 on older kernels that do not support this */
904930
};
905931

932+
/* Event delivered at sequence. Time stamp marks when the first pixel
933+
* of the refresh cycle leaves the display engine for the display
934+
*/
935+
struct drm_event_crtc_sequence {
936+
struct drm_event base;
937+
__u64 user_data;
938+
__s64 time_ns;
939+
__u64 sequence;
940+
};
941+
906942
/* typedef area */
907943
#ifndef __KERNEL__
908944
typedef struct drm_clip_rect drm_clip_rect_t;

0 commit comments

Comments
 (0)