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