1 // SPDX-License-Identifier: GPL-2.0+
4 #include <linux/crc32.h>
5 #include <drm/drm_atomic.h>
6 #include <drm/drm_atomic_helper.h>
7 #include <drm/drm_gem_framebuffer_helper.h>
10 * compute_crc - Compute CRC value on output frame
12 * @vaddr_out: address to final framebuffer
13 * @crc_out: framebuffer's metadata
15 * returns CRC value computed using crc32 on the visible portion of
16 * the final framebuffer at vaddr_out
18 static uint32_t compute_crc(void *vaddr_out, struct vkms_crc_data *crc_out)
21 int x_src = crc_out->src.x1 >> 16;
22 int y_src = crc_out->src.y1 >> 16;
23 int h_src = drm_rect_height(&crc_out->src) >> 16;
24 int w_src = drm_rect_width(&crc_out->src) >> 16;
27 for (i = y_src; i < y_src + h_src; ++i) {
28 for (j = x_src; j < x_src + w_src; ++j) {
29 src_offset = crc_out->offset
30 + (i * crc_out->pitch)
32 /* XRGB format ignores Alpha channel */
33 memset(vaddr_out + src_offset + 24, 0, 8);
34 crc = crc32_le(crc, vaddr_out + src_offset,
43 * blend - belnd value at vaddr_src with value at vaddr_dst
44 * @vaddr_dst: destination address
45 * @vaddr_src: source address
46 * @crc_dst: destination framebuffer's metadata
47 * @crc_src: source framebuffer's metadata
49 * Blend value at vaddr_src with value at vaddr_dst.
50 * Currently, this function write value at vaddr_src on value
51 * at vaddr_dst using buffer's metadata to locate the new values
52 * from vaddr_src and their distenation at vaddr_dst.
54 * Todo: Use the alpha value to blend vaddr_src with vaddr_dst
55 * instead of overwriting it.
57 static void blend(void *vaddr_dst, void *vaddr_src,
58 struct vkms_crc_data *crc_dst,
59 struct vkms_crc_data *crc_src)
61 int i, j, j_dst, i_dst;
62 int offset_src, offset_dst;
64 int x_src = crc_src->src.x1 >> 16;
65 int y_src = crc_src->src.y1 >> 16;
67 int x_dst = crc_src->dst.x1;
68 int y_dst = crc_src->dst.y1;
69 int h_dst = drm_rect_height(&crc_src->dst);
70 int w_dst = drm_rect_width(&crc_src->dst);
72 int y_limit = y_src + h_dst;
73 int x_limit = x_src + w_dst;
75 for (i = y_src, i_dst = y_dst; i < y_limit; ++i) {
76 for (j = x_src, j_dst = x_dst; j < x_limit; ++j) {
77 offset_dst = crc_dst->offset
78 + (i_dst * crc_dst->pitch)
79 + (j_dst++ * crc_dst->cpp);
80 offset_src = crc_src->offset
81 + (i * crc_src->pitch)
84 memcpy(vaddr_dst + offset_dst,
85 vaddr_src + offset_src, sizeof(u32));
91 static void compose_cursor(struct vkms_crc_data *cursor_crc,
92 struct vkms_crc_data *primary_crc, void *vaddr_out)
94 struct drm_gem_object *cursor_obj;
95 struct vkms_gem_object *cursor_vkms_obj;
97 cursor_obj = drm_gem_fb_get_obj(&cursor_crc->fb, 0);
98 cursor_vkms_obj = drm_gem_to_vkms_gem(cursor_obj);
100 mutex_lock(&cursor_vkms_obj->pages_lock);
101 if (!cursor_vkms_obj->vaddr) {
102 DRM_WARN("cursor plane vaddr is NULL");
106 blend(vaddr_out, cursor_vkms_obj->vaddr, primary_crc, cursor_crc);
109 mutex_unlock(&cursor_vkms_obj->pages_lock);
112 static uint32_t _vkms_get_crc(struct vkms_crc_data *primary_crc,
113 struct vkms_crc_data *cursor_crc)
115 struct drm_framebuffer *fb = &primary_crc->fb;
116 struct drm_gem_object *gem_obj = drm_gem_fb_get_obj(fb, 0);
117 struct vkms_gem_object *vkms_obj = drm_gem_to_vkms_gem(gem_obj);
118 void *vaddr_out = kzalloc(vkms_obj->gem.size, GFP_KERNEL);
122 DRM_ERROR("Failed to allocate memory for output frame.");
126 mutex_lock(&vkms_obj->pages_lock);
127 if (WARN_ON(!vkms_obj->vaddr)) {
128 mutex_unlock(&vkms_obj->pages_lock);
133 memcpy(vaddr_out, vkms_obj->vaddr, vkms_obj->gem.size);
134 mutex_unlock(&vkms_obj->pages_lock);
137 compose_cursor(cursor_crc, primary_crc, vaddr_out);
139 crc = compute_crc(vaddr_out, primary_crc);
147 * vkms_crc_work_handle - ordered work_struct to compute CRC
151 * Work handler for computing CRCs. work_struct scheduled in
152 * an ordered workqueue that's periodically scheduled to run by
153 * _vblank_handle() and flushed at vkms_atomic_crtc_destroy_state().
155 void vkms_crc_work_handle(struct work_struct *work)
157 struct vkms_crtc_state *crtc_state = container_of(work,
158 struct vkms_crtc_state,
160 struct drm_crtc *crtc = crtc_state->base.crtc;
161 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
162 struct vkms_device *vdev = container_of(out, struct vkms_device,
164 struct vkms_crc_data *primary_crc = NULL;
165 struct vkms_crc_data *cursor_crc = NULL;
166 struct drm_plane *plane;
168 u64 frame_start, frame_end;
171 spin_lock_irqsave(&out->state_lock, flags);
172 frame_start = crtc_state->frame_start;
173 frame_end = crtc_state->frame_end;
174 spin_unlock_irqrestore(&out->state_lock, flags);
176 /* _vblank_handle() hasn't updated frame_start yet */
177 if (!frame_start || frame_start == frame_end)
180 drm_for_each_plane(plane, &vdev->drm) {
181 struct vkms_plane_state *vplane_state;
182 struct vkms_crc_data *crc_data;
184 vplane_state = to_vkms_plane_state(plane->state);
185 crc_data = vplane_state->crc_data;
187 if (drm_framebuffer_read_refcount(&crc_data->fb) == 0)
190 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
191 primary_crc = crc_data;
193 cursor_crc = crc_data;
197 crc32 = _vkms_get_crc(primary_crc, cursor_crc);
199 frame_end = drm_crtc_accurate_vblank_count(crtc);
201 /* queue_work can fail to schedule crc_work; add crc for
204 while (frame_start <= frame_end)
205 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
208 /* to avoid using the same value for frame number again */
209 spin_lock_irqsave(&out->state_lock, flags);
210 crtc_state->frame_end = frame_end;
211 crtc_state->frame_start = 0;
212 spin_unlock_irqrestore(&out->state_lock, flags);
215 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
221 } else if (strcmp(src_name, "auto") == 0) {
231 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
236 if (vkms_crc_parse_source(src_name, &enabled) < 0) {
237 DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
246 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
248 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
249 bool enabled = false;
253 ret = vkms_crc_parse_source(src_name, &enabled);
255 /* make sure nothing is scheduled on crtc workq */
256 flush_workqueue(out->crc_workq);
258 spin_lock_irqsave(&out->lock, flags);
259 out->crc_enabled = enabled;
260 spin_unlock_irqrestore(&out->lock, flags);