xref: /openbmc/linux/drivers/gpu/drm/vkms/vkms_crtc.c (revision 08283d30)
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <drm/drm_atomic.h>
4 #include <drm/drm_atomic_helper.h>
5 #include <drm/drm_probe_helper.h>
6 #include <drm/drm_vblank.h>
7 
8 #include "vkms_drv.h"
9 
10 static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer)
11 {
12 	struct vkms_output *output = container_of(timer, struct vkms_output,
13 						  vblank_hrtimer);
14 	struct drm_crtc *crtc = &output->crtc;
15 	struct vkms_crtc_state *state;
16 	u64 ret_overrun;
17 	bool ret;
18 
19 	spin_lock(&output->lock);
20 
21 	ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer,
22 					  output->period_ns);
23 	WARN_ON(ret_overrun != 1);
24 
25 	ret = drm_crtc_handle_vblank(crtc);
26 	if (!ret)
27 		DRM_ERROR("vkms failure on handling vblank");
28 
29 	state = output->composer_state;
30 	if (state && output->composer_enabled) {
31 		u64 frame = drm_crtc_accurate_vblank_count(crtc);
32 
33 		/* update frame_start only if a queued vkms_composer_worker()
34 		 * has read the data
35 		 */
36 		spin_lock(&output->composer_lock);
37 		if (!state->crc_pending)
38 			state->frame_start = frame;
39 		else
40 			DRM_DEBUG_DRIVER("crc worker falling behind, frame_start: %llu, frame_end: %llu\n",
41 					 state->frame_start, frame);
42 		state->frame_end = frame;
43 		state->crc_pending = true;
44 		spin_unlock(&output->composer_lock);
45 
46 		ret = queue_work(output->composer_workq, &state->composer_work);
47 		if (!ret)
48 			DRM_DEBUG_DRIVER("Composer worker already queued\n");
49 	}
50 
51 	spin_unlock(&output->lock);
52 
53 	return HRTIMER_RESTART;
54 }
55 
56 static int vkms_enable_vblank(struct drm_crtc *crtc)
57 {
58 	struct drm_device *dev = crtc->dev;
59 	unsigned int pipe = drm_crtc_index(crtc);
60 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
61 	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
62 
63 	drm_calc_timestamping_constants(crtc, &crtc->mode);
64 
65 	hrtimer_init(&out->vblank_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
66 	out->vblank_hrtimer.function = &vkms_vblank_simulate;
67 	out->period_ns = ktime_set(0, vblank->framedur_ns);
68 	hrtimer_start(&out->vblank_hrtimer, out->period_ns, HRTIMER_MODE_REL);
69 
70 	return 0;
71 }
72 
73 static void vkms_disable_vblank(struct drm_crtc *crtc)
74 {
75 	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
76 
77 	hrtimer_cancel(&out->vblank_hrtimer);
78 }
79 
80 bool vkms_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
81 			       int *max_error, ktime_t *vblank_time,
82 			       bool in_vblank_irq)
83 {
84 	struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
85 	struct vkms_output *output = &vkmsdev->output;
86 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
87 
88 	*vblank_time = output->vblank_hrtimer.node.expires;
89 
90 	if (WARN_ON(*vblank_time == vblank->time))
91 		return true;
92 
93 	/*
94 	 * To prevent races we roll the hrtimer forward before we do any
95 	 * interrupt processing - this is how real hw works (the interrupt is
96 	 * only generated after all the vblank registers are updated) and what
97 	 * the vblank core expects. Therefore we need to always correct the
98 	 * timestampe by one frame.
99 	 */
100 	*vblank_time -= output->period_ns;
101 
102 	return true;
103 }
104 
105 static struct drm_crtc_state *
106 vkms_atomic_crtc_duplicate_state(struct drm_crtc *crtc)
107 {
108 	struct vkms_crtc_state *vkms_state;
109 
110 	if (WARN_ON(!crtc->state))
111 		return NULL;
112 
113 	vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
114 	if (!vkms_state)
115 		return NULL;
116 
117 	__drm_atomic_helper_crtc_duplicate_state(crtc, &vkms_state->base);
118 
119 	INIT_WORK(&vkms_state->composer_work, vkms_composer_worker);
120 
121 	return &vkms_state->base;
122 }
123 
124 static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc,
125 					   struct drm_crtc_state *state)
126 {
127 	struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state);
128 
129 	__drm_atomic_helper_crtc_destroy_state(state);
130 
131 	WARN_ON(work_pending(&vkms_state->composer_work));
132 	kfree(vkms_state->active_planes);
133 	kfree(vkms_state);
134 }
135 
136 static void vkms_atomic_crtc_reset(struct drm_crtc *crtc)
137 {
138 	struct vkms_crtc_state *vkms_state =
139 		kzalloc(sizeof(*vkms_state), GFP_KERNEL);
140 
141 	if (crtc->state)
142 		vkms_atomic_crtc_destroy_state(crtc, crtc->state);
143 
144 	__drm_atomic_helper_crtc_reset(crtc, &vkms_state->base);
145 	if (vkms_state)
146 		INIT_WORK(&vkms_state->composer_work, vkms_composer_worker);
147 }
148 
149 static const struct drm_crtc_funcs vkms_crtc_funcs = {
150 	.set_config             = drm_atomic_helper_set_config,
151 	.destroy                = drm_crtc_cleanup,
152 	.page_flip              = drm_atomic_helper_page_flip,
153 	.reset                  = vkms_atomic_crtc_reset,
154 	.atomic_duplicate_state = vkms_atomic_crtc_duplicate_state,
155 	.atomic_destroy_state   = vkms_atomic_crtc_destroy_state,
156 	.enable_vblank		= vkms_enable_vblank,
157 	.disable_vblank		= vkms_disable_vblank,
158 	.get_crc_sources	= vkms_get_crc_sources,
159 	.set_crc_source		= vkms_set_crc_source,
160 	.verify_crc_source	= vkms_verify_crc_source,
161 };
162 
163 static int vkms_crtc_atomic_check(struct drm_crtc *crtc,
164 				  struct drm_crtc_state *state)
165 {
166 	struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state);
167 	struct drm_plane *plane;
168 	struct drm_plane_state *plane_state;
169 	int i = 0, ret;
170 
171 	if (vkms_state->active_planes)
172 		return 0;
173 
174 	ret = drm_atomic_add_affected_planes(state->state, crtc);
175 	if (ret < 0)
176 		return ret;
177 
178 	drm_for_each_plane_mask(plane, crtc->dev, state->plane_mask) {
179 		plane_state = drm_atomic_get_existing_plane_state(state->state,
180 								  plane);
181 		WARN_ON(!plane_state);
182 
183 		if (!plane_state->visible)
184 			continue;
185 
186 		i++;
187 	}
188 
189 	vkms_state->active_planes = kcalloc(i, sizeof(plane), GFP_KERNEL);
190 	if (!vkms_state->active_planes)
191 		return -ENOMEM;
192 	vkms_state->num_active_planes = i;
193 
194 	i = 0;
195 	drm_for_each_plane_mask(plane, crtc->dev, state->plane_mask) {
196 		plane_state = drm_atomic_get_existing_plane_state(state->state,
197 								  plane);
198 
199 		if (!plane_state->visible)
200 			continue;
201 
202 		vkms_state->active_planes[i++] =
203 			to_vkms_plane_state(plane_state);
204 	}
205 
206 	return 0;
207 }
208 
209 static void vkms_crtc_atomic_enable(struct drm_crtc *crtc,
210 				    struct drm_crtc_state *old_state)
211 {
212 	drm_crtc_vblank_on(crtc);
213 }
214 
215 static void vkms_crtc_atomic_disable(struct drm_crtc *crtc,
216 				     struct drm_crtc_state *old_state)
217 {
218 	drm_crtc_vblank_off(crtc);
219 }
220 
221 static void vkms_crtc_atomic_begin(struct drm_crtc *crtc,
222 				   struct drm_crtc_state *old_crtc_state)
223 {
224 	struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
225 
226 	/* This lock is held across the atomic commit to block vblank timer
227 	 * from scheduling vkms_composer_worker until the composer is updated
228 	 */
229 	spin_lock_irq(&vkms_output->lock);
230 }
231 
232 static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
233 				   struct drm_crtc_state *old_crtc_state)
234 {
235 	struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
236 
237 	if (crtc->state->event) {
238 		spin_lock(&crtc->dev->event_lock);
239 
240 		if (drm_crtc_vblank_get(crtc) != 0)
241 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
242 		else
243 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
244 
245 		spin_unlock(&crtc->dev->event_lock);
246 
247 		crtc->state->event = NULL;
248 	}
249 
250 	vkms_output->composer_state = to_vkms_crtc_state(crtc->state);
251 
252 	spin_unlock_irq(&vkms_output->lock);
253 }
254 
255 static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = {
256 	.atomic_check	= vkms_crtc_atomic_check,
257 	.atomic_begin	= vkms_crtc_atomic_begin,
258 	.atomic_flush	= vkms_crtc_atomic_flush,
259 	.atomic_enable	= vkms_crtc_atomic_enable,
260 	.atomic_disable	= vkms_crtc_atomic_disable,
261 };
262 
263 int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
264 		   struct drm_plane *primary, struct drm_plane *cursor)
265 {
266 	struct vkms_output *vkms_out = drm_crtc_to_vkms_output(crtc);
267 	int ret;
268 
269 	ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
270 					&vkms_crtc_funcs, NULL);
271 	if (ret) {
272 		DRM_ERROR("Failed to init CRTC\n");
273 		return ret;
274 	}
275 
276 	drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs);
277 
278 	spin_lock_init(&vkms_out->lock);
279 	spin_lock_init(&vkms_out->composer_lock);
280 
281 	vkms_out->composer_workq = alloc_ordered_workqueue("vkms_composer", 0);
282 	if (!vkms_out->composer_workq)
283 		return -ENOMEM;
284 
285 	return ret;
286 }
287