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  * @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  * blend - blend the pixels from all planes and compute crc
94  * @wb: The writeback frame buffer metadata
95  * @crtc_state: The crtc state
96  * @crc32: The crc output of the final frame
97  * @output_buffer: A buffer of a row that will receive the result of the blend(s)
98  * @stage_buffer: The line with the pixels from plane being blend to the output
99  * @row_size: The size, in bytes, of a single row
100  *
101  * This function blends the pixels (Using the `pre_mul_alpha_blend`)
102  * from all planes, calculates the crc32 of the output from the former step,
103  * and, if necessary, convert and store the output to the writeback buffer.
104  */
105 static void blend(struct vkms_writeback_job *wb,
106 		  struct vkms_crtc_state *crtc_state,
107 		  u32 *crc32, struct line_buffer *stage_buffer,
108 		  struct line_buffer *output_buffer, size_t row_size)
109 {
110 	struct vkms_plane_state **plane = crtc_state->active_planes;
111 	u32 n_active_planes = crtc_state->num_active_planes;
112 	int y_pos;
113 
114 	const struct pixel_argb_u16 background_color = { .a = 0xffff };
115 
116 	size_t crtc_y_limit = crtc_state->base.crtc->mode.vdisplay;
117 
118 	for (size_t y = 0; y < crtc_y_limit; y++) {
119 		fill_background(&background_color, output_buffer);
120 
121 		/* The active planes are composed associatively in z-order. */
122 		for (size_t i = 0; i < n_active_planes; i++) {
123 			y_pos = get_y_pos(plane[i]->frame_info, y);
124 
125 			if (!check_limit(plane[i]->frame_info, y_pos))
126 				continue;
127 
128 			vkms_compose_row(stage_buffer, plane[i], y_pos);
129 			pre_mul_alpha_blend(plane[i]->frame_info, stage_buffer,
130 					    output_buffer);
131 		}
132 
133 		*crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
134 
135 		if (wb)
136 			vkms_writeback_row(wb, output_buffer, y_pos);
137 	}
138 }
139 
140 static int check_format_funcs(struct vkms_crtc_state *crtc_state,
141 			      struct vkms_writeback_job *active_wb)
142 {
143 	struct vkms_plane_state **planes = crtc_state->active_planes;
144 	u32 n_active_planes = crtc_state->num_active_planes;
145 
146 	for (size_t i = 0; i < n_active_planes; i++)
147 		if (!planes[i]->pixel_read)
148 			return -1;
149 
150 	if (active_wb && !active_wb->pixel_write)
151 		return -1;
152 
153 	return 0;
154 }
155 
156 static int check_iosys_map(struct vkms_crtc_state *crtc_state)
157 {
158 	struct vkms_plane_state **plane_state = crtc_state->active_planes;
159 	u32 n_active_planes = crtc_state->num_active_planes;
160 
161 	for (size_t i = 0; i < n_active_planes; i++)
162 		if (iosys_map_is_null(&plane_state[i]->frame_info->map[0]))
163 			return -1;
164 
165 	return 0;
166 }
167 
168 static int compose_active_planes(struct vkms_writeback_job *active_wb,
169 				 struct vkms_crtc_state *crtc_state,
170 				 u32 *crc32)
171 {
172 	size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
173 	struct line_buffer output_buffer, stage_buffer;
174 	int ret = 0;
175 
176 	/*
177 	 * This check exists so we can call `crc32_le` for the entire line
178 	 * instead doing it for each channel of each pixel in case
179 	 * `struct `pixel_argb_u16` had any gap added by the compiler
180 	 * between the struct fields.
181 	 */
182 	static_assert(sizeof(struct pixel_argb_u16) == 8);
183 
184 	if (WARN_ON(check_iosys_map(crtc_state)))
185 		return -EINVAL;
186 
187 	if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
188 		return -EINVAL;
189 
190 	line_width = crtc_state->base.crtc->mode.hdisplay;
191 	stage_buffer.n_pixels = line_width;
192 	output_buffer.n_pixels = line_width;
193 
194 	stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
195 	if (!stage_buffer.pixels) {
196 		DRM_ERROR("Cannot allocate memory for the output line buffer");
197 		return -ENOMEM;
198 	}
199 
200 	output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
201 	if (!output_buffer.pixels) {
202 		DRM_ERROR("Cannot allocate memory for intermediate line buffer");
203 		ret = -ENOMEM;
204 		goto free_stage_buffer;
205 	}
206 
207 	blend(active_wb, crtc_state, crc32, &stage_buffer,
208 	      &output_buffer, line_width * pixel_size);
209 
210 	kvfree(output_buffer.pixels);
211 free_stage_buffer:
212 	kvfree(stage_buffer.pixels);
213 
214 	return ret;
215 }
216 
217 /**
218  * vkms_composer_worker - ordered work_struct to compute CRC
219  *
220  * @work: work_struct
221  *
222  * Work handler for composing and computing CRCs. work_struct scheduled in
223  * an ordered workqueue that's periodically scheduled to run by
224  * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail().
225  */
226 void vkms_composer_worker(struct work_struct *work)
227 {
228 	struct vkms_crtc_state *crtc_state = container_of(work,
229 						struct vkms_crtc_state,
230 						composer_work);
231 	struct drm_crtc *crtc = crtc_state->base.crtc;
232 	struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
233 	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
234 	bool crc_pending, wb_pending;
235 	u64 frame_start, frame_end;
236 	u32 crc32 = 0;
237 	int ret;
238 
239 	spin_lock_irq(&out->composer_lock);
240 	frame_start = crtc_state->frame_start;
241 	frame_end = crtc_state->frame_end;
242 	crc_pending = crtc_state->crc_pending;
243 	wb_pending = crtc_state->wb_pending;
244 	crtc_state->frame_start = 0;
245 	crtc_state->frame_end = 0;
246 	crtc_state->crc_pending = false;
247 	spin_unlock_irq(&out->composer_lock);
248 
249 	/*
250 	 * We raced with the vblank hrtimer and previous work already computed
251 	 * the crc, nothing to do.
252 	 */
253 	if (!crc_pending)
254 		return;
255 
256 	if (wb_pending)
257 		ret = compose_active_planes(active_wb, crtc_state, &crc32);
258 	else
259 		ret = compose_active_planes(NULL, crtc_state, &crc32);
260 
261 	if (ret)
262 		return;
263 
264 	if (wb_pending) {
265 		drm_writeback_signal_completion(&out->wb_connector, 0);
266 		spin_lock_irq(&out->composer_lock);
267 		crtc_state->wb_pending = false;
268 		spin_unlock_irq(&out->composer_lock);
269 	}
270 
271 	/*
272 	 * The worker can fall behind the vblank hrtimer, make sure we catch up.
273 	 */
274 	while (frame_start <= frame_end)
275 		drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
276 }
277 
278 static const char * const pipe_crc_sources[] = {"auto"};
279 
280 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
281 					size_t *count)
282 {
283 	*count = ARRAY_SIZE(pipe_crc_sources);
284 	return pipe_crc_sources;
285 }
286 
287 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
288 {
289 	int ret = 0;
290 
291 	if (!src_name) {
292 		*enabled = false;
293 	} else if (strcmp(src_name, "auto") == 0) {
294 		*enabled = true;
295 	} else {
296 		*enabled = false;
297 		ret = -EINVAL;
298 	}
299 
300 	return ret;
301 }
302 
303 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
304 			   size_t *values_cnt)
305 {
306 	bool enabled;
307 
308 	if (vkms_crc_parse_source(src_name, &enabled) < 0) {
309 		DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
310 		return -EINVAL;
311 	}
312 
313 	*values_cnt = 1;
314 
315 	return 0;
316 }
317 
318 void vkms_set_composer(struct vkms_output *out, bool enabled)
319 {
320 	bool old_enabled;
321 
322 	if (enabled)
323 		drm_crtc_vblank_get(&out->crtc);
324 
325 	spin_lock_irq(&out->lock);
326 	old_enabled = out->composer_enabled;
327 	out->composer_enabled = enabled;
328 	spin_unlock_irq(&out->lock);
329 
330 	if (old_enabled)
331 		drm_crtc_vblank_put(&out->crtc);
332 }
333 
334 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
335 {
336 	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
337 	bool enabled = false;
338 	int ret = 0;
339 
340 	ret = vkms_crc_parse_source(src_name, &enabled);
341 
342 	vkms_set_composer(out, enabled);
343 
344 	return ret;
345 }
346