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_gem_framebuffer_helper.h> 8 #include <drm/drm_vblank.h> 9 10 #include "vkms_drv.h" 11 12 /** 13 * compute_crc - Compute CRC value on output frame 14 * 15 * @vaddr_out: address to final framebuffer 16 * @composer: framebuffer's metadata 17 * 18 * returns CRC value computed using crc32 on the visible portion of 19 * the final framebuffer at vaddr_out 20 */ 21 static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer) 22 { 23 int i, j, src_offset; 24 int x_src = composer->src.x1 >> 16; 25 int y_src = composer->src.y1 >> 16; 26 int h_src = drm_rect_height(&composer->src) >> 16; 27 int w_src = drm_rect_width(&composer->src) >> 16; 28 u32 crc = 0; 29 30 for (i = y_src; i < y_src + h_src; ++i) { 31 for (j = x_src; j < x_src + w_src; ++j) { 32 src_offset = composer->offset 33 + (i * composer->pitch) 34 + (j * composer->cpp); 35 crc = crc32_le(crc, vaddr_out + src_offset, 36 sizeof(u32)); 37 } 38 } 39 40 return crc; 41 } 42 43 static u8 blend_channel(u8 src, u8 dst, u8 alpha) 44 { 45 u32 pre_blend; 46 u8 new_color; 47 48 pre_blend = (src * 255 + dst * (255 - alpha)); 49 50 /* Faster div by 255 */ 51 new_color = ((pre_blend + ((pre_blend + 257) >> 8)) >> 8); 52 53 return new_color; 54 } 55 56 static void alpha_blending(const u8 *argb_src, u8 *argb_dst) 57 { 58 u8 alpha; 59 60 alpha = argb_src[3]; 61 argb_dst[0] = blend_channel(argb_src[0], argb_dst[0], alpha); 62 argb_dst[1] = blend_channel(argb_src[1], argb_dst[1], alpha); 63 argb_dst[2] = blend_channel(argb_src[2], argb_dst[2], alpha); 64 /* Opaque primary */ 65 argb_dst[3] = 0xFF; 66 } 67 68 /** 69 * blend - blend value at vaddr_src with value at vaddr_dst 70 * @vaddr_dst: destination address 71 * @vaddr_src: source address 72 * @dst_composer: destination framebuffer's metadata 73 * @src_composer: source framebuffer's metadata 74 * 75 * Blend the vaddr_src value with the vaddr_dst value using the pre-multiplied 76 * alpha blending equation, since DRM currently assumes that the pixel color 77 * values have already been pre-multiplied with the alpha channel values. See 78 * more drm_plane_create_blend_mode_property(). This function uses buffer's 79 * metadata to locate the new composite values at vaddr_dst. 80 */ 81 static void blend(void *vaddr_dst, void *vaddr_src, 82 struct vkms_composer *dst_composer, 83 struct vkms_composer *src_composer) 84 { 85 int i, j, j_dst, i_dst; 86 int offset_src, offset_dst; 87 u8 *pixel_dst, *pixel_src; 88 89 int x_src = src_composer->src.x1 >> 16; 90 int y_src = src_composer->src.y1 >> 16; 91 92 int x_dst = src_composer->dst.x1; 93 int y_dst = src_composer->dst.y1; 94 int h_dst = drm_rect_height(&src_composer->dst); 95 int w_dst = drm_rect_width(&src_composer->dst); 96 97 int y_limit = y_src + h_dst; 98 int x_limit = x_src + w_dst; 99 100 for (i = y_src, i_dst = y_dst; i < y_limit; ++i) { 101 for (j = x_src, j_dst = x_dst; j < x_limit; ++j) { 102 offset_dst = dst_composer->offset 103 + (i_dst * dst_composer->pitch) 104 + (j_dst++ * dst_composer->cpp); 105 offset_src = src_composer->offset 106 + (i * src_composer->pitch) 107 + (j * src_composer->cpp); 108 109 pixel_src = (u8 *)(vaddr_src + offset_src); 110 pixel_dst = (u8 *)(vaddr_dst + offset_dst); 111 alpha_blending(pixel_src, pixel_dst); 112 } 113 i_dst++; 114 } 115 } 116 117 static void compose_cursor(struct vkms_composer *cursor_composer, 118 struct vkms_composer *primary_composer, 119 void *vaddr_out) 120 { 121 struct drm_gem_object *cursor_obj; 122 struct vkms_gem_object *cursor_vkms_obj; 123 124 cursor_obj = drm_gem_fb_get_obj(&cursor_composer->fb, 0); 125 cursor_vkms_obj = drm_gem_to_vkms_gem(cursor_obj); 126 127 if (WARN_ON(!cursor_vkms_obj->vaddr)) 128 return; 129 130 blend(vaddr_out, cursor_vkms_obj->vaddr, 131 primary_composer, cursor_composer); 132 } 133 134 static uint32_t _vkms_get_crc(struct vkms_composer *primary_composer, 135 struct vkms_composer *cursor_composer) 136 { 137 struct drm_framebuffer *fb = &primary_composer->fb; 138 struct drm_gem_object *gem_obj = drm_gem_fb_get_obj(fb, 0); 139 struct vkms_gem_object *vkms_obj = drm_gem_to_vkms_gem(gem_obj); 140 void *vaddr_out = kzalloc(vkms_obj->gem.size, GFP_KERNEL); 141 u32 crc = 0; 142 143 if (!vaddr_out) { 144 DRM_ERROR("Failed to allocate memory for output frame."); 145 return 0; 146 } 147 148 if (WARN_ON(!vkms_obj->vaddr)) { 149 kfree(vaddr_out); 150 return crc; 151 } 152 153 memcpy(vaddr_out, vkms_obj->vaddr, vkms_obj->gem.size); 154 155 if (cursor_composer) 156 compose_cursor(cursor_composer, primary_composer, vaddr_out); 157 158 crc = compute_crc(vaddr_out, primary_composer); 159 160 kfree(vaddr_out); 161 162 return crc; 163 } 164 165 /** 166 * vkms_composer_worker - ordered work_struct to compute CRC 167 * 168 * @work: work_struct 169 * 170 * Work handler for composing and computing CRCs. work_struct scheduled in 171 * an ordered workqueue that's periodically scheduled to run by 172 * _vblank_handle() and flushed at vkms_atomic_crtc_destroy_state(). 173 */ 174 void vkms_composer_worker(struct work_struct *work) 175 { 176 struct vkms_crtc_state *crtc_state = container_of(work, 177 struct vkms_crtc_state, 178 composer_work); 179 struct drm_crtc *crtc = crtc_state->base.crtc; 180 struct vkms_output *out = drm_crtc_to_vkms_output(crtc); 181 struct vkms_composer *primary_composer = NULL; 182 struct vkms_composer *cursor_composer = NULL; 183 u32 crc32 = 0; 184 u64 frame_start, frame_end; 185 bool crc_pending; 186 187 spin_lock_irq(&out->composer_lock); 188 frame_start = crtc_state->frame_start; 189 frame_end = crtc_state->frame_end; 190 crc_pending = crtc_state->crc_pending; 191 crtc_state->frame_start = 0; 192 crtc_state->frame_end = 0; 193 crtc_state->crc_pending = false; 194 spin_unlock_irq(&out->composer_lock); 195 196 /* 197 * We raced with the vblank hrtimer and previous work already computed 198 * the crc, nothing to do. 199 */ 200 if (!crc_pending) 201 return; 202 203 if (crtc_state->num_active_planes >= 1) 204 primary_composer = crtc_state->active_planes[0]->composer; 205 206 if (crtc_state->num_active_planes == 2) 207 cursor_composer = crtc_state->active_planes[1]->composer; 208 209 if (primary_composer) 210 crc32 = _vkms_get_crc(primary_composer, cursor_composer); 211 212 /* 213 * The worker can fall behind the vblank hrtimer, make sure we catch up. 214 */ 215 while (frame_start <= frame_end) 216 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32); 217 } 218 219 static const char * const pipe_crc_sources[] = {"auto"}; 220 221 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc, 222 size_t *count) 223 { 224 *count = ARRAY_SIZE(pipe_crc_sources); 225 return pipe_crc_sources; 226 } 227 228 static int vkms_crc_parse_source(const char *src_name, bool *enabled) 229 { 230 int ret = 0; 231 232 if (!src_name) { 233 *enabled = false; 234 } else if (strcmp(src_name, "auto") == 0) { 235 *enabled = true; 236 } else { 237 *enabled = false; 238 ret = -EINVAL; 239 } 240 241 return ret; 242 } 243 244 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name, 245 size_t *values_cnt) 246 { 247 bool enabled; 248 249 if (vkms_crc_parse_source(src_name, &enabled) < 0) { 250 DRM_DEBUG_DRIVER("unknown source %s\n", src_name); 251 return -EINVAL; 252 } 253 254 *values_cnt = 1; 255 256 return 0; 257 } 258 259 static void vkms_set_composer(struct vkms_output *out, bool enabled) 260 { 261 bool old_enabled; 262 263 if (enabled) 264 drm_crtc_vblank_get(&out->crtc); 265 266 spin_lock_irq(&out->lock); 267 old_enabled = out->composer_enabled; 268 out->composer_enabled = enabled; 269 spin_unlock_irq(&out->lock); 270 271 if (old_enabled) 272 drm_crtc_vblank_put(&out->crtc); 273 } 274 275 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name) 276 { 277 struct vkms_output *out = drm_crtc_to_vkms_output(crtc); 278 bool enabled = false; 279 int ret = 0; 280 281 ret = vkms_crc_parse_source(src_name, &enabled); 282 283 vkms_set_composer(out, enabled); 284 285 return ret; 286 } 287