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