1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/dma-fence.h>
5 #include <drm/drm_atomic.h>
6 #include <drm/drm_atomic_helper.h>
7 #include <drm/drm_probe_helper.h>
8 #include <drm/drm_vblank.h>
12 static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer)
14 struct vkms_output *output = container_of(timer, struct vkms_output,
16 struct drm_crtc *crtc = &output->crtc;
17 struct vkms_crtc_state *state;
19 bool ret, fence_cookie, composer_enabled;
21 fence_cookie = dma_fence_begin_signalling();
23 ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer,
26 pr_warn("%s: vblank timer overrun\n", __func__);
28 ret = drm_crtc_handle_vblank(crtc);
30 DRM_ERROR("vkms failure on handling vblank");
32 state = output->composer_state;
33 composer_enabled = output->composer_enabled;
34 mutex_unlock(&output->enabled_lock);
36 if (state && composer_enabled) {
37 u64 frame = drm_crtc_accurate_vblank_count(crtc);
39 /* update frame_start only if a queued vkms_composer_worker()
42 spin_lock(&output->composer_lock);
43 if (!state->crc_pending)
44 state->frame_start = frame;
46 DRM_DEBUG_DRIVER("crc worker falling behind, frame_start: %llu, frame_end: %llu\n",
47 state->frame_start, frame);
48 state->frame_end = frame;
49 state->crc_pending = true;
50 spin_unlock(&output->composer_lock);
52 ret = queue_work(output->composer_workq, &state->composer_work);
54 DRM_DEBUG_DRIVER("Composer worker already queued\n");
57 dma_fence_end_signalling(fence_cookie);
59 return HRTIMER_RESTART;
62 static int vkms_enable_vblank(struct drm_crtc *crtc)
64 struct drm_device *dev = crtc->dev;
65 unsigned int pipe = drm_crtc_index(crtc);
66 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
67 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
69 drm_calc_timestamping_constants(crtc, &crtc->mode);
71 hrtimer_init(&out->vblank_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
72 out->vblank_hrtimer.function = &vkms_vblank_simulate;
73 out->period_ns = ktime_set(0, vblank->framedur_ns);
74 hrtimer_start(&out->vblank_hrtimer, out->period_ns, HRTIMER_MODE_REL);
79 static void vkms_disable_vblank(struct drm_crtc *crtc)
81 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
83 hrtimer_cancel(&out->vblank_hrtimer);
86 static bool vkms_get_vblank_timestamp(struct drm_crtc *crtc,
87 int *max_error, ktime_t *vblank_time,
90 struct drm_device *dev = crtc->dev;
91 unsigned int pipe = crtc->index;
92 struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
93 struct vkms_output *output = &vkmsdev->output;
94 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
96 if (!READ_ONCE(vblank->enabled)) {
97 *vblank_time = ktime_get();
101 *vblank_time = READ_ONCE(output->vblank_hrtimer.node.expires);
103 if (WARN_ON(*vblank_time == vblank->time))
107 * To prevent races we roll the hrtimer forward before we do any
108 * interrupt processing - this is how real hw works (the interrupt is
109 * only generated after all the vblank registers are updated) and what
110 * the vblank core expects. Therefore we need to always correct the
111 * timestampe by one frame.
113 *vblank_time -= output->period_ns;
118 static struct drm_crtc_state *
119 vkms_atomic_crtc_duplicate_state(struct drm_crtc *crtc)
121 struct vkms_crtc_state *vkms_state;
123 if (WARN_ON(!crtc->state))
126 vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
130 __drm_atomic_helper_crtc_duplicate_state(crtc, &vkms_state->base);
132 INIT_WORK(&vkms_state->composer_work, vkms_composer_worker);
134 return &vkms_state->base;
137 static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc,
138 struct drm_crtc_state *state)
140 struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state);
142 __drm_atomic_helper_crtc_destroy_state(state);
144 WARN_ON(work_pending(&vkms_state->composer_work));
145 kfree(vkms_state->active_planes);
149 static void vkms_atomic_crtc_reset(struct drm_crtc *crtc)
151 struct vkms_crtc_state *vkms_state =
152 kzalloc(sizeof(*vkms_state), GFP_KERNEL);
155 vkms_atomic_crtc_destroy_state(crtc, crtc->state);
157 __drm_atomic_helper_crtc_reset(crtc, &vkms_state->base);
159 INIT_WORK(&vkms_state->composer_work, vkms_composer_worker);
162 static const struct drm_crtc_funcs vkms_crtc_funcs = {
163 .set_config = drm_atomic_helper_set_config,
164 .page_flip = drm_atomic_helper_page_flip,
165 .reset = vkms_atomic_crtc_reset,
166 .atomic_duplicate_state = vkms_atomic_crtc_duplicate_state,
167 .atomic_destroy_state = vkms_atomic_crtc_destroy_state,
168 .enable_vblank = vkms_enable_vblank,
169 .disable_vblank = vkms_disable_vblank,
170 .get_vblank_timestamp = vkms_get_vblank_timestamp,
171 .get_crc_sources = vkms_get_crc_sources,
172 .set_crc_source = vkms_set_crc_source,
173 .verify_crc_source = vkms_verify_crc_source,
176 static int vkms_crtc_atomic_check(struct drm_crtc *crtc,
177 struct drm_atomic_state *state)
179 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
181 struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(crtc_state);
182 struct drm_plane *plane;
183 struct drm_plane_state *plane_state;
186 if (vkms_state->active_planes)
189 ret = drm_atomic_add_affected_planes(crtc_state->state, crtc);
193 drm_for_each_plane_mask(plane, crtc->dev, crtc_state->plane_mask) {
194 plane_state = drm_atomic_get_existing_plane_state(crtc_state->state,
196 WARN_ON(!plane_state);
198 if (!plane_state->visible)
204 vkms_state->active_planes = kcalloc(i, sizeof(plane), GFP_KERNEL);
205 if (!vkms_state->active_planes)
207 vkms_state->num_active_planes = i;
210 drm_for_each_plane_mask(plane, crtc->dev, crtc_state->plane_mask) {
211 plane_state = drm_atomic_get_existing_plane_state(crtc_state->state,
214 if (!plane_state->visible)
217 vkms_state->active_planes[i++] =
218 to_vkms_plane_state(plane_state);
224 static void vkms_crtc_atomic_enable(struct drm_crtc *crtc,
225 struct drm_atomic_state *state)
227 drm_crtc_vblank_on(crtc);
230 static void vkms_crtc_atomic_disable(struct drm_crtc *crtc,
231 struct drm_atomic_state *state)
233 drm_crtc_vblank_off(crtc);
236 static void vkms_crtc_atomic_begin(struct drm_crtc *crtc,
237 struct drm_atomic_state *state)
239 struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
241 /* This lock is held across the atomic commit to block vblank timer
242 * from scheduling vkms_composer_worker until the composer is updated
244 spin_lock_irq(&vkms_output->lock);
247 static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
248 struct drm_atomic_state *state)
250 struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
252 if (crtc->state->event) {
253 spin_lock(&crtc->dev->event_lock);
255 if (drm_crtc_vblank_get(crtc) != 0)
256 drm_crtc_send_vblank_event(crtc, crtc->state->event);
258 drm_crtc_arm_vblank_event(crtc, crtc->state->event);
260 spin_unlock(&crtc->dev->event_lock);
262 crtc->state->event = NULL;
265 vkms_output->composer_state = to_vkms_crtc_state(crtc->state);
267 spin_unlock_irq(&vkms_output->lock);
270 static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = {
271 .atomic_check = vkms_crtc_atomic_check,
272 .atomic_begin = vkms_crtc_atomic_begin,
273 .atomic_flush = vkms_crtc_atomic_flush,
274 .atomic_enable = vkms_crtc_atomic_enable,
275 .atomic_disable = vkms_crtc_atomic_disable,
278 int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
279 struct drm_plane *primary, struct drm_plane *cursor)
281 struct vkms_output *vkms_out = drm_crtc_to_vkms_output(crtc);
284 ret = drmm_crtc_init_with_planes(dev, crtc, primary, cursor,
285 &vkms_crtc_funcs, NULL);
287 DRM_ERROR("Failed to init CRTC\n");
291 drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs);
293 drm_mode_crtc_set_gamma_size(crtc, VKMS_LUT_SIZE);
294 drm_crtc_enable_color_mgmt(crtc, 0, false, VKMS_LUT_SIZE);
296 spin_lock_init(&vkms_out->lock);
297 spin_lock_init(&vkms_out->composer_lock);
298 mutex_init(&vkms_out->enabled_lock);
300 vkms_out->composer_workq = alloc_ordered_workqueue("vkms_composer", 0);
301 if (!vkms_out->composer_workq)