1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/crc32.h>
4 
5 #include <drm/drm_atomic.h>
6 #include <drm/drm_atomic_helper.h>
7 #include <drm/drm_blend.h>
8 #include <drm/drm_fourcc.h>
9 #include <drm/drm_gem_framebuffer_helper.h>
10 #include <drm/drm_vblank.h>
11 #include <linux/minmax.h>
12 
13 #include "vkms_drv.h"
14 
15 static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
16 {
17 	u32 new_color;
18 
19 	new_color = (src * 0xffff + dst * (0xffff - alpha));
20 
21 	return DIV_ROUND_CLOSEST(new_color, 0xffff);
22 }
23 
24 /**
25  * pre_mul_alpha_blend - alpha blending equation
26  * @src_frame_info: source framebuffer's metadata
27  * @stage_buffer: The line with the pixels from src_plane
28  * @output_buffer: A line buffer that receives all the blends output
29  *
30  * Using the information from the `frame_info`, this blends only the
31  * necessary pixels from the `stage_buffer` to the `output_buffer`
32  * using premultiplied blend formula.
33  *
34  * The current DRM assumption is that pixel color values have been already
35  * pre-multiplied with the alpha channel values. See more
36  * drm_plane_create_blend_mode_property(). Also, this formula assumes a
37  * completely opaque background.
38  */
39 static void pre_mul_alpha_blend(struct vkms_frame_info *frame_info,
40 				struct line_buffer *stage_buffer,
41 				struct line_buffer *output_buffer)
42 {
43 	int x_dst = frame_info->dst.x1;
44 	struct pixel_argb_u16 *out = output_buffer->pixels + x_dst;
45 	struct pixel_argb_u16 *in = stage_buffer->pixels;
46 	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
47 			    stage_buffer->n_pixels);
48 
49 	for (int x = 0; x < x_limit; x++) {
50 		out[x].a = (u16)0xffff;
51 		out[x].r = pre_mul_blend_channel(in[x].r, out[x].r, in[x].a);
52 		out[x].g = pre_mul_blend_channel(in[x].g, out[x].g, in[x].a);
53 		out[x].b = pre_mul_blend_channel(in[x].b, out[x].b, in[x].a);
54 	}
55 }
56 
57 static int get_y_pos(struct vkms_frame_info *frame_info, int y)
58 {
59 	if (frame_info->rotation & DRM_MODE_REFLECT_Y)
60 		return drm_rect_height(&frame_info->rotated) - y - 1;
61 
62 	switch (frame_info->rotation & DRM_MODE_ROTATE_MASK) {
63 	case DRM_MODE_ROTATE_90:
64 		return frame_info->rotated.x2 - y - 1;
65 	case DRM_MODE_ROTATE_270:
66 		return y + frame_info->rotated.x1;
67 	default:
68 		return y;
69 	}
70 }
71 
72 static bool check_limit(struct vkms_frame_info *frame_info, int pos)
73 {
74 	if (drm_rotation_90_or_270(frame_info->rotation)) {
75 		if (pos >= 0 && pos < drm_rect_width(&frame_info->rotated))
76 			return true;
77 	} else {
78 		if (pos >= frame_info->rotated.y1 && pos < frame_info->rotated.y2)
79 			return true;
80 	}
81 
82 	return false;
83 }
84 
85 static void fill_background(const struct pixel_argb_u16 *background_color,
86 			    struct line_buffer *output_buffer)
87 {
88 	for (size_t i = 0; i < output_buffer->n_pixels; i++)
89 		output_buffer->pixels[i] = *background_color;
90 }
91 
92 /**
93  * @wb_frame_info: The writeback frame buffer metadata
94  * @crtc_state: The crtc state
95  * @crc32: The crc output of the final frame
96  * @output_buffer: A buffer of a row that will receive the result of the blend(s)
97  * @stage_buffer: The line with the pixels from plane being blend to the output
98  *
99  * This function blends the pixels (Using the `pre_mul_alpha_blend`)
100  * from all planes, calculates the crc32 of the output from the former step,
101  * and, if necessary, convert and store the output to the writeback buffer.
102  */
103 static void blend(struct vkms_writeback_job *wb,
104 		  struct vkms_crtc_state *crtc_state,
105 		  u32 *crc32, struct line_buffer *stage_buffer,
106 		  struct line_buffer *output_buffer, size_t row_size)
107 {
108 	struct vkms_plane_state **plane = crtc_state->active_planes;
109 	u32 n_active_planes = crtc_state->num_active_planes;
110 	int y_pos;
111 
112 	const struct pixel_argb_u16 background_color = { .a = 0xffff };
113 
114 	size_t crtc_y_limit = crtc_state->base.crtc->mode.vdisplay;
115 
116 	for (size_t y = 0; y < crtc_y_limit; y++) {
117 		fill_background(&background_color, output_buffer);
118 
119 		/* The active planes are composed associatively in z-order. */
120 		for (size_t i = 0; i < n_active_planes; i++) {
121 			y_pos = get_y_pos(plane[i]->frame_info, y);
122 
123 			if (!check_limit(plane[i]->frame_info, y_pos))
124 				continue;
125 
126 			vkms_compose_row(stage_buffer, plane[i], y_pos);
127 			pre_mul_alpha_blend(plane[i]->frame_info, stage_buffer,
128 					    output_buffer);
129 		}
130 
131 		*crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
132 
133 		if (wb)
134 			wb->wb_write(&wb->wb_frame_info, output_buffer, y_pos);
135 	}
136 }
137 
138 static int check_format_funcs(struct vkms_crtc_state *crtc_state,
139 			      struct vkms_writeback_job *active_wb)
140 {
141 	struct vkms_plane_state **planes = crtc_state->active_planes;
142 	u32 n_active_planes = crtc_state->num_active_planes;
143 
144 	for (size_t i = 0; i < n_active_planes; i++)
145 		if (!planes[i]->pixel_read)
146 			return -1;
147 
148 	if (active_wb && !active_wb->wb_write)
149 		return -1;
150 
151 	return 0;
152 }
153 
154 static int check_iosys_map(struct vkms_crtc_state *crtc_state)
155 {
156 	struct vkms_plane_state **plane_state = crtc_state->active_planes;
157 	u32 n_active_planes = crtc_state->num_active_planes;
158 
159 	for (size_t i = 0; i < n_active_planes; i++)
160 		if (iosys_map_is_null(&plane_state[i]->frame_info->map[0]))
161 			return -1;
162 
163 	return 0;
164 }
165 
166 static int compose_active_planes(struct vkms_writeback_job *active_wb,
167 				 struct vkms_crtc_state *crtc_state,
168 				 u32 *crc32)
169 {
170 	size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
171 	struct line_buffer output_buffer, stage_buffer;
172 	int ret = 0;
173 
174 	/*
175 	 * This check exists so we can call `crc32_le` for the entire line
176 	 * instead doing it for each channel of each pixel in case
177 	 * `struct `pixel_argb_u16` had any gap added by the compiler
178 	 * between the struct fields.
179 	 */
180 	static_assert(sizeof(struct pixel_argb_u16) == 8);
181 
182 	if (WARN_ON(check_iosys_map(crtc_state)))
183 		return -EINVAL;
184 
185 	if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
186 		return -EINVAL;
187 
188 	line_width = crtc_state->base.crtc->mode.hdisplay;
189 	stage_buffer.n_pixels = line_width;
190 	output_buffer.n_pixels = line_width;
191 
192 	stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
193 	if (!stage_buffer.pixels) {
194 		DRM_ERROR("Cannot allocate memory for the output line buffer");
195 		return -ENOMEM;
196 	}
197 
198 	output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
199 	if (!output_buffer.pixels) {
200 		DRM_ERROR("Cannot allocate memory for intermediate line buffer");
201 		ret = -ENOMEM;
202 		goto free_stage_buffer;
203 	}
204 
205 	blend(active_wb, crtc_state, crc32, &stage_buffer,
206 	      &output_buffer, line_width * pixel_size);
207 
208 	kvfree(output_buffer.pixels);
209 free_stage_buffer:
210 	kvfree(stage_buffer.pixels);
211 
212 	return ret;
213 }
214 
215 /**
216  * vkms_composer_worker - ordered work_struct to compute CRC
217  *
218  * @work: work_struct
219  *
220  * Work handler for composing and computing CRCs. work_struct scheduled in
221  * an ordered workqueue that's periodically scheduled to run by
222  * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail().
223  */
224 void vkms_composer_worker(struct work_struct *work)
225 {
226 	struct vkms_crtc_state *crtc_state = container_of(work,
227 						struct vkms_crtc_state,
228 						composer_work);
229 	struct drm_crtc *crtc = crtc_state->base.crtc;
230 	struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
231 	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
232 	bool crc_pending, wb_pending;
233 	u64 frame_start, frame_end;
234 	u32 crc32 = 0;
235 	int ret;
236 
237 	spin_lock_irq(&out->composer_lock);
238 	frame_start = crtc_state->frame_start;
239 	frame_end = crtc_state->frame_end;
240 	crc_pending = crtc_state->crc_pending;
241 	wb_pending = crtc_state->wb_pending;
242 	crtc_state->frame_start = 0;
243 	crtc_state->frame_end = 0;
244 	crtc_state->crc_pending = false;
245 	spin_unlock_irq(&out->composer_lock);
246 
247 	/*
248 	 * We raced with the vblank hrtimer and previous work already computed
249 	 * the crc, nothing to do.
250 	 */
251 	if (!crc_pending)
252 		return;
253 
254 	if (wb_pending)
255 		ret = compose_active_planes(active_wb, crtc_state, &crc32);
256 	else
257 		ret = compose_active_planes(NULL, crtc_state, &crc32);
258 
259 	if (ret)
260 		return;
261 
262 	if (wb_pending) {
263 		drm_writeback_signal_completion(&out->wb_connector, 0);
264 		spin_lock_irq(&out->composer_lock);
265 		crtc_state->wb_pending = false;
266 		spin_unlock_irq(&out->composer_lock);
267 	}
268 
269 	/*
270 	 * The worker can fall behind the vblank hrtimer, make sure we catch up.
271 	 */
272 	while (frame_start <= frame_end)
273 		drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
274 }
275 
276 static const char * const pipe_crc_sources[] = {"auto"};
277 
278 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
279 					size_t *count)
280 {
281 	*count = ARRAY_SIZE(pipe_crc_sources);
282 	return pipe_crc_sources;
283 }
284 
285 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
286 {
287 	int ret = 0;
288 
289 	if (!src_name) {
290 		*enabled = false;
291 	} else if (strcmp(src_name, "auto") == 0) {
292 		*enabled = true;
293 	} else {
294 		*enabled = false;
295 		ret = -EINVAL;
296 	}
297 
298 	return ret;
299 }
300 
301 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
302 			   size_t *values_cnt)
303 {
304 	bool enabled;
305 
306 	if (vkms_crc_parse_source(src_name, &enabled) < 0) {
307 		DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
308 		return -EINVAL;
309 	}
310 
311 	*values_cnt = 1;
312 
313 	return 0;
314 }
315 
316 void vkms_set_composer(struct vkms_output *out, bool enabled)
317 {
318 	bool old_enabled;
319 
320 	if (enabled)
321 		drm_crtc_vblank_get(&out->crtc);
322 
323 	spin_lock_irq(&out->lock);
324 	old_enabled = out->composer_enabled;
325 	out->composer_enabled = enabled;
326 	spin_unlock_irq(&out->lock);
327 
328 	if (old_enabled)
329 		drm_crtc_vblank_put(&out->crtc);
330 }
331 
332 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
333 {
334 	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
335 	bool enabled = false;
336 	int ret = 0;
337 
338 	ret = vkms_crc_parse_source(src_name, &enabled);
339 
340 	vkms_set_composer(out, enabled);
341 
342 	return ret;
343 }
344