Merge branch 'x86-alternatives-for-linus' of git://git.kernel.org/pub/scm/linux/kerne...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / vkms / vkms_crc.c
1 // SPDX-License-Identifier: GPL-2.0+
2
3 #include "vkms_drv.h"
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>
8
9 /**
10  * compute_crc - Compute CRC value on output frame
11  *
12  * @vaddr_out: address to final framebuffer
13  * @crc_out: framebuffer's metadata
14  *
15  * returns CRC value computed using crc32 on the visible portion of
16  * the final framebuffer at vaddr_out
17  */
18 static uint32_t compute_crc(void *vaddr_out, struct vkms_crc_data *crc_out)
19 {
20         int i, j, src_offset;
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;
25         u32 crc = 0;
26
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)
31                                      + (j * crc_out->cpp);
32                         /* XRGB format ignores Alpha channel */
33                         memset(vaddr_out + src_offset + 24, 0,  8);
34                         crc = crc32_le(crc, vaddr_out + src_offset,
35                                        sizeof(u32));
36                 }
37         }
38
39         return crc;
40 }
41
42 /**
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
48  *
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.
53  *
54  * Todo: Use the alpha value to blend vaddr_src with vaddr_dst
55  *       instead of overwriting it.
56  */
57 static void blend(void *vaddr_dst, void *vaddr_src,
58                   struct vkms_crc_data *crc_dst,
59                   struct vkms_crc_data *crc_src)
60 {
61         int i, j, j_dst, i_dst;
62         int offset_src, offset_dst;
63
64         int x_src = crc_src->src.x1 >> 16;
65         int y_src = crc_src->src.y1 >> 16;
66
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);
71
72         int y_limit = y_src + h_dst;
73         int x_limit = x_src + w_dst;
74
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)
82                                      + (j * crc_src->cpp);
83
84                         memcpy(vaddr_dst + offset_dst,
85                                vaddr_src + offset_src, sizeof(u32));
86                 }
87                 i_dst++;
88         }
89 }
90
91 static void compose_cursor(struct vkms_crc_data *cursor_crc,
92                            struct vkms_crc_data *primary_crc, void *vaddr_out)
93 {
94         struct drm_gem_object *cursor_obj;
95         struct vkms_gem_object *cursor_vkms_obj;
96
97         cursor_obj = drm_gem_fb_get_obj(&cursor_crc->fb, 0);
98         cursor_vkms_obj = drm_gem_to_vkms_gem(cursor_obj);
99
100         mutex_lock(&cursor_vkms_obj->pages_lock);
101         if (!cursor_vkms_obj->vaddr) {
102                 DRM_WARN("cursor plane vaddr is NULL");
103                 goto out;
104         }
105
106         blend(vaddr_out, cursor_vkms_obj->vaddr, primary_crc, cursor_crc);
107
108 out:
109         mutex_unlock(&cursor_vkms_obj->pages_lock);
110 }
111
112 static uint32_t _vkms_get_crc(struct vkms_crc_data *primary_crc,
113                               struct vkms_crc_data *cursor_crc)
114 {
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);
119         u32 crc = 0;
120
121         if (!vaddr_out) {
122                 DRM_ERROR("Failed to allocate memory for output frame.");
123                 return 0;
124         }
125
126         mutex_lock(&vkms_obj->pages_lock);
127         if (WARN_ON(!vkms_obj->vaddr)) {
128                 mutex_unlock(&vkms_obj->pages_lock);
129                 kfree(vaddr_out);
130                 return crc;
131         }
132
133         memcpy(vaddr_out, vkms_obj->vaddr, vkms_obj->gem.size);
134         mutex_unlock(&vkms_obj->pages_lock);
135
136         if (cursor_crc)
137                 compose_cursor(cursor_crc, primary_crc, vaddr_out);
138
139         crc = compute_crc(vaddr_out, primary_crc);
140
141         kfree(vaddr_out);
142
143         return crc;
144 }
145
146 /**
147  * vkms_crc_work_handle - ordered work_struct to compute CRC
148  *
149  * @work: work_struct
150  *
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().
154  */
155 void vkms_crc_work_handle(struct work_struct *work)
156 {
157         struct vkms_crtc_state *crtc_state = container_of(work,
158                                                 struct vkms_crtc_state,
159                                                 crc_work);
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,
163                                                 output);
164         struct vkms_crc_data *primary_crc = NULL;
165         struct vkms_crc_data *cursor_crc = NULL;
166         struct drm_plane *plane;
167         u32 crc32 = 0;
168         u64 frame_start, frame_end;
169         unsigned long flags;
170
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);
175
176         /* _vblank_handle() hasn't updated frame_start yet */
177         if (!frame_start || frame_start == frame_end)
178                 goto out;
179
180         drm_for_each_plane(plane, &vdev->drm) {
181                 struct vkms_plane_state *vplane_state;
182                 struct vkms_crc_data *crc_data;
183
184                 vplane_state = to_vkms_plane_state(plane->state);
185                 crc_data = vplane_state->crc_data;
186
187                 if (drm_framebuffer_read_refcount(&crc_data->fb) == 0)
188                         continue;
189
190                 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
191                         primary_crc = crc_data;
192                 else
193                         cursor_crc = crc_data;
194         }
195
196         if (primary_crc)
197                 crc32 = _vkms_get_crc(primary_crc, cursor_crc);
198
199         frame_end = drm_crtc_accurate_vblank_count(crtc);
200
201         /* queue_work can fail to schedule crc_work; add crc for
202          * missing frames
203          */
204         while (frame_start <= frame_end)
205                 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
206
207 out:
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);
213 }
214
215 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
216 {
217         int ret = 0;
218
219         if (!src_name) {
220                 *enabled = false;
221         } else if (strcmp(src_name, "auto") == 0) {
222                 *enabled = true;
223         } else {
224                 *enabled = false;
225                 ret = -EINVAL;
226         }
227
228         return ret;
229 }
230
231 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
232                            size_t *values_cnt)
233 {
234         bool enabled;
235
236         if (vkms_crc_parse_source(src_name, &enabled) < 0) {
237                 DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
238                 return -EINVAL;
239         }
240
241         *values_cnt = 1;
242
243         return 0;
244 }
245
246 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
247 {
248         struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
249         bool enabled = false;
250         unsigned long flags;
251         int ret = 0;
252
253         ret = vkms_crc_parse_source(src_name, &enabled);
254
255         /* make sure nothing is scheduled on crtc workq */
256         flush_workqueue(out->crc_workq);
257
258         spin_lock_irqsave(&out->lock, flags);
259         out->crc_enabled = enabled;
260         spin_unlock_irqrestore(&out->lock, flags);
261
262         return ret;
263 }