1 // SPDX-License-Identifier: GPL-2.0+
4 #include <drm/drm_atomic_helper.h>
5 #include <drm/drm_crtc_helper.h>
7 static enum hrtimer_restart
vkms_vblank_simulate(struct hrtimer
*timer
)
9 struct vkms_output
*output
= container_of(timer
, struct vkms_output
,
11 struct drm_crtc
*crtc
= &output
->crtc
;
15 ret
= drm_crtc_handle_vblank(crtc
);
17 DRM_ERROR("vkms failure on handling vblank");
19 ret_overrun
= hrtimer_forward_now(&output
->vblank_hrtimer
,
22 return HRTIMER_RESTART
;
25 static int vkms_enable_vblank(struct drm_crtc
*crtc
)
27 struct drm_device
*dev
= crtc
->dev
;
28 unsigned int pipe
= drm_crtc_index(crtc
);
29 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
30 struct vkms_output
*out
= drm_crtc_to_vkms_output(crtc
);
32 drm_calc_timestamping_constants(crtc
, &crtc
->mode
);
34 hrtimer_init(&out
->vblank_hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
35 out
->vblank_hrtimer
.function
= &vkms_vblank_simulate
;
36 out
->period_ns
= ktime_set(0, vblank
->framedur_ns
);
37 hrtimer_start(&out
->vblank_hrtimer
, out
->period_ns
, HRTIMER_MODE_REL
);
42 static void vkms_disable_vblank(struct drm_crtc
*crtc
)
44 struct vkms_output
*out
= drm_crtc_to_vkms_output(crtc
);
46 hrtimer_cancel(&out
->vblank_hrtimer
);
49 bool vkms_get_vblank_timestamp(struct drm_device
*dev
, unsigned int pipe
,
50 int *max_error
, ktime_t
*vblank_time
,
53 struct vkms_device
*vkmsdev
= drm_device_to_vkms_device(dev
);
54 struct vkms_output
*output
= &vkmsdev
->output
;
56 *vblank_time
= output
->vblank_hrtimer
.node
.expires
;
59 *vblank_time
-= output
->period_ns
;
64 static const struct drm_crtc_funcs vkms_crtc_funcs
= {
65 .set_config
= drm_atomic_helper_set_config
,
66 .destroy
= drm_crtc_cleanup
,
67 .page_flip
= drm_atomic_helper_page_flip
,
68 .reset
= drm_atomic_helper_crtc_reset
,
69 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
70 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
71 .enable_vblank
= vkms_enable_vblank
,
72 .disable_vblank
= vkms_disable_vblank
,
75 static void vkms_crtc_atomic_enable(struct drm_crtc
*crtc
,
76 struct drm_crtc_state
*old_state
)
78 drm_crtc_vblank_on(crtc
);
81 static void vkms_crtc_atomic_disable(struct drm_crtc
*crtc
,
82 struct drm_crtc_state
*old_state
)
84 drm_crtc_vblank_off(crtc
);
87 static void vkms_crtc_atomic_flush(struct drm_crtc
*crtc
,
88 struct drm_crtc_state
*old_crtc_state
)
92 if (crtc
->state
->event
) {
93 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
95 if (drm_crtc_vblank_get(crtc
) != 0)
96 drm_crtc_send_vblank_event(crtc
, crtc
->state
->event
);
98 drm_crtc_arm_vblank_event(crtc
, crtc
->state
->event
);
100 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
102 crtc
->state
->event
= NULL
;
106 static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs
= {
107 .atomic_flush
= vkms_crtc_atomic_flush
,
108 .atomic_enable
= vkms_crtc_atomic_enable
,
109 .atomic_disable
= vkms_crtc_atomic_disable
,
112 int vkms_crtc_init(struct drm_device
*dev
, struct drm_crtc
*crtc
,
113 struct drm_plane
*primary
, struct drm_plane
*cursor
)
117 ret
= drm_crtc_init_with_planes(dev
, crtc
, primary
, cursor
,
118 &vkms_crtc_funcs
, NULL
);
120 DRM_ERROR("Failed to init CRTC\n");
124 drm_crtc_helper_add(crtc
, &vkms_crtc_helper_funcs
);