xref: /openbmc/linux/drivers/gpu/drm/vkms/vkms_crtc.c (revision 4984dd069f2995f239f075199ee8c0d9f020bcd9)
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 = drm_crtc_handle_vblank(crtc);
19 	if (!ret)
20 		DRM_ERROR("vkms failure on handling vblank");
21 
22 	if (state && output->crc_enabled) {
23 		u64 frame = drm_crtc_accurate_vblank_count(crtc);
24 
25 		/* update frame_start only if a queued vkms_crc_work_handle()
26 		 * has read the data
27 		 */
28 		spin_lock(&output->state_lock);
29 		if (!state->frame_start)
30 			state->frame_start = frame;
31 		spin_unlock(&output->state_lock);
32 
33 		ret = queue_work(output->crc_workq, &state->crc_work);
34 		if (!ret)
35 			DRM_WARN("failed to queue vkms_crc_work_handle");
36 	}
37 
38 	ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer,
39 					  output->period_ns);
40 	WARN_ON(ret_overrun != 1);
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 
78 	*vblank_time = output->vblank_hrtimer.node.expires;
79 
80 	if (!in_vblank_irq)
81 		*vblank_time -= output->period_ns;
82 
83 	return true;
84 }
85 
86 static struct drm_crtc_state *
87 vkms_atomic_crtc_duplicate_state(struct drm_crtc *crtc)
88 {
89 	struct vkms_crtc_state *vkms_state;
90 
91 	if (WARN_ON(!crtc->state))
92 		return NULL;
93 
94 	vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
95 	if (!vkms_state)
96 		return NULL;
97 
98 	__drm_atomic_helper_crtc_duplicate_state(crtc, &vkms_state->base);
99 
100 	INIT_WORK(&vkms_state->crc_work, vkms_crc_work_handle);
101 
102 	return &vkms_state->base;
103 }
104 
105 static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc,
106 					   struct drm_crtc_state *state)
107 {
108 	struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state);
109 
110 	__drm_atomic_helper_crtc_destroy_state(state);
111 
112 	if (vkms_state) {
113 		flush_work(&vkms_state->crc_work);
114 		kfree(vkms_state);
115 	}
116 }
117 
118 static void vkms_atomic_crtc_reset(struct drm_crtc *crtc)
119 {
120 	struct vkms_crtc_state *vkms_state =
121 		kzalloc(sizeof(*vkms_state), GFP_KERNEL);
122 
123 	if (crtc->state)
124 		vkms_atomic_crtc_destroy_state(crtc, crtc->state);
125 
126 	__drm_atomic_helper_crtc_reset(crtc, &vkms_state->base);
127 	if (vkms_state)
128 		INIT_WORK(&vkms_state->crc_work, vkms_crc_work_handle);
129 }
130 
131 static const struct drm_crtc_funcs vkms_crtc_funcs = {
132 	.set_config             = drm_atomic_helper_set_config,
133 	.destroy                = drm_crtc_cleanup,
134 	.page_flip              = drm_atomic_helper_page_flip,
135 	.reset                  = vkms_atomic_crtc_reset,
136 	.atomic_duplicate_state = vkms_atomic_crtc_duplicate_state,
137 	.atomic_destroy_state   = vkms_atomic_crtc_destroy_state,
138 	.enable_vblank		= vkms_enable_vblank,
139 	.disable_vblank		= vkms_disable_vblank,
140 	.set_crc_source		= vkms_set_crc_source,
141 	.verify_crc_source	= vkms_verify_crc_source,
142 };
143 
144 static void vkms_crtc_atomic_enable(struct drm_crtc *crtc,
145 				    struct drm_crtc_state *old_state)
146 {
147 	drm_crtc_vblank_on(crtc);
148 }
149 
150 static void vkms_crtc_atomic_disable(struct drm_crtc *crtc,
151 				     struct drm_crtc_state *old_state)
152 {
153 	drm_crtc_vblank_off(crtc);
154 }
155 
156 static void vkms_crtc_atomic_begin(struct drm_crtc *crtc,
157 				   struct drm_crtc_state *old_crtc_state)
158 {
159 	struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
160 
161 	/* This lock is held across the atomic commit to block vblank timer
162 	 * from scheduling vkms_crc_work_handle until the crc_data is updated
163 	 */
164 	spin_lock_irq(&vkms_output->lock);
165 }
166 
167 static void vkms_crtc_atomic_flush(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 	unsigned long flags;
172 
173 	if (crtc->state->event) {
174 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
175 
176 		if (drm_crtc_vblank_get(crtc) != 0)
177 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
178 		else
179 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
180 
181 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
182 
183 		crtc->state->event = NULL;
184 	}
185 
186 	spin_unlock_irq(&vkms_output->lock);
187 }
188 
189 static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = {
190 	.atomic_begin	= vkms_crtc_atomic_begin,
191 	.atomic_flush	= vkms_crtc_atomic_flush,
192 	.atomic_enable	= vkms_crtc_atomic_enable,
193 	.atomic_disable	= vkms_crtc_atomic_disable,
194 };
195 
196 int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
197 		   struct drm_plane *primary, struct drm_plane *cursor)
198 {
199 	struct vkms_output *vkms_out = drm_crtc_to_vkms_output(crtc);
200 	int ret;
201 
202 	ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
203 					&vkms_crtc_funcs, NULL);
204 	if (ret) {
205 		DRM_ERROR("Failed to init CRTC\n");
206 		return ret;
207 	}
208 
209 	drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs);
210 
211 	spin_lock_init(&vkms_out->lock);
212 	spin_lock_init(&vkms_out->state_lock);
213 
214 	vkms_out->crc_workq = alloc_ordered_workqueue("vkms_crc_workq", 0);
215 	if (!vkms_out->crc_workq)
216 		return -ENOMEM;
217 
218 	return ret;
219 }
220