1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/crc32.h>
4 #include <drm/drm_atomic.h>
5 #include <drm/drm_atomic_helper.h>
6 #include <drm/drm_gem_framebuffer_helper.h>
9 * compute_crc - Compute CRC value on output frame
11 * @vaddr_out: address to final framebuffer
12 * @crc_out: framebuffer's metadata
14 * returns CRC value computed using crc32 on the visible portion of
15 * the final framebuffer at vaddr_out
17 static uint32_t compute_crc(void *vaddr_out, struct vkms_crc_data *crc_out)
20 int x_src = crc_out->src.x1 >> 16;
21 int y_src = crc_out->src.y1 >> 16;
22 int h_src = drm_rect_height(&crc_out->src) >> 16;
23 int w_src = drm_rect_width(&crc_out->src) >> 16;
26 for (i = y_src; i < y_src + h_src; ++i) {
27 for (j = x_src; j < x_src + w_src; ++j) {
28 src_offset = crc_out->offset
29 + (i * crc_out->pitch)
31 /* XRGB format ignores Alpha channel */
32 memset(vaddr_out + src_offset + 24, 0, 8);
33 crc = crc32_le(crc, vaddr_out + src_offset,
42 * blend - belnd value at vaddr_src with value at vaddr_dst
43 * @vaddr_dst: destination address
44 * @vaddr_src: source address
45 * @crc_dst: destination framebuffer's metadata
46 * @crc_src: source framebuffer's metadata
48 * Blend value at vaddr_src with value at vaddr_dst.
49 * Currently, this function write value at vaddr_src on value
50 * at vaddr_dst using buffer's metadata to locate the new values
51 * from vaddr_src and their distenation at vaddr_dst.
53 * Todo: Use the alpha value to blend vaddr_src with vaddr_dst
54 * instead of overwriting it.
56 static void blend(void *vaddr_dst, void *vaddr_src,
57 struct vkms_crc_data *crc_dst,
58 struct vkms_crc_data *crc_src)
60 int i, j, j_dst, i_dst;
61 int offset_src, offset_dst;
63 int x_src = crc_src->src.x1 >> 16;
64 int y_src = crc_src->src.y1 >> 16;
66 int x_dst = crc_src->dst.x1;
67 int y_dst = crc_src->dst.y1;
68 int h_dst = drm_rect_height(&crc_src->dst);
69 int w_dst = drm_rect_width(&crc_src->dst);
71 int y_limit = y_src + h_dst;
72 int x_limit = x_src + w_dst;
74 for (i = y_src, i_dst = y_dst; i < y_limit; ++i) {
75 for (j = x_src, j_dst = x_dst; j < x_limit; ++j) {
76 offset_dst = crc_dst->offset
77 + (i_dst * crc_dst->pitch)
78 + (j_dst++ * crc_dst->cpp);
79 offset_src = crc_src->offset
80 + (i * crc_src->pitch)
83 memcpy(vaddr_dst + offset_dst,
84 vaddr_src + offset_src, sizeof(u32));
90 static void compose_cursor(struct vkms_crc_data *cursor_crc,
91 struct vkms_crc_data *primary_crc, void *vaddr_out)
93 struct drm_gem_object *cursor_obj;
94 struct vkms_gem_object *cursor_vkms_obj;
96 cursor_obj = drm_gem_fb_get_obj(&cursor_crc->fb, 0);
97 cursor_vkms_obj = drm_gem_to_vkms_gem(cursor_obj);
99 mutex_lock(&cursor_vkms_obj->pages_lock);
100 if (!cursor_vkms_obj->vaddr) {
101 DRM_WARN("cursor plane vaddr is NULL");
105 blend(vaddr_out, cursor_vkms_obj->vaddr, primary_crc, cursor_crc);
108 mutex_unlock(&cursor_vkms_obj->pages_lock);
111 static uint32_t _vkms_get_crc(struct vkms_crc_data *primary_crc,
112 struct vkms_crc_data *cursor_crc)
114 struct drm_framebuffer *fb = &primary_crc->fb;
115 struct drm_gem_object *gem_obj = drm_gem_fb_get_obj(fb, 0);
116 struct vkms_gem_object *vkms_obj = drm_gem_to_vkms_gem(gem_obj);
117 void *vaddr_out = kzalloc(vkms_obj->gem.size, GFP_KERNEL);
121 DRM_ERROR("Failed to allocate memory for output frame.");
125 mutex_lock(&vkms_obj->pages_lock);
126 if (WARN_ON(!vkms_obj->vaddr)) {
127 mutex_unlock(&vkms_obj->pages_lock);
132 memcpy(vaddr_out, vkms_obj->vaddr, vkms_obj->gem.size);
133 mutex_unlock(&vkms_obj->pages_lock);
136 compose_cursor(cursor_crc, primary_crc, vaddr_out);
138 crc = compute_crc(vaddr_out, primary_crc);
146 * vkms_crc_work_handle - ordered work_struct to compute CRC
150 * Work handler for computing CRCs. work_struct scheduled in
151 * an ordered workqueue that's periodically scheduled to run by
152 * _vblank_handle() and flushed at vkms_atomic_crtc_destroy_state().
154 void vkms_crc_work_handle(struct work_struct *work)
156 struct vkms_crtc_state *crtc_state = container_of(work,
157 struct vkms_crtc_state,
159 struct drm_crtc *crtc = crtc_state->base.crtc;
160 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
161 struct vkms_device *vdev = container_of(out, struct vkms_device,
163 struct vkms_crc_data *primary_crc = NULL;
164 struct vkms_crc_data *cursor_crc = NULL;
165 struct drm_plane *plane;
167 u64 frame_start, frame_end;
170 spin_lock_irqsave(&out->state_lock, flags);
171 frame_start = crtc_state->frame_start;
172 frame_end = crtc_state->frame_end;
173 spin_unlock_irqrestore(&out->state_lock, flags);
175 /* _vblank_handle() hasn't updated frame_start yet */
176 if (!frame_start || frame_start == frame_end)
179 drm_for_each_plane(plane, &vdev->drm) {
180 struct vkms_plane_state *vplane_state;
181 struct vkms_crc_data *crc_data;
183 vplane_state = to_vkms_plane_state(plane->state);
184 crc_data = vplane_state->crc_data;
186 if (drm_framebuffer_read_refcount(&crc_data->fb) == 0)
189 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
190 primary_crc = crc_data;
192 cursor_crc = crc_data;
196 crc32 = _vkms_get_crc(primary_crc, cursor_crc);
198 frame_end = drm_crtc_accurate_vblank_count(crtc);
200 /* queue_work can fail to schedule crc_work; add crc for
203 while (frame_start <= frame_end)
204 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
207 /* to avoid using the same value for frame number again */
208 spin_lock_irqsave(&out->state_lock, flags);
209 crtc_state->frame_end = frame_end;
210 crtc_state->frame_start = 0;
211 spin_unlock_irqrestore(&out->state_lock, flags);
214 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
220 } else if (strcmp(src_name, "auto") == 0) {
230 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
235 if (vkms_crc_parse_source(src_name, &enabled) < 0) {
236 DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
245 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
247 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
248 bool enabled = false;
252 ret = vkms_crc_parse_source(src_name, &enabled);
254 /* make sure nothing is scheduled on crtc workq */
255 flush_workqueue(out->crc_workq);
257 spin_lock_irqsave(&out->lock, flags);
258 out->crc_enabled = enabled;
259 spin_unlock_irqrestore(&out->lock, flags);