be4f95b2d59c5ab0fd91da598b73487513c5b34f
[platform/kernel/linux-starfive.git] / drivers / gpu / drm / gud / gud_pipe.c
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2020 Noralf Trønnes
4  */
5
6 #include <linux/lz4.h>
7 #include <linux/usb.h>
8 #include <linux/workqueue.h>
9
10 #include <drm/drm_atomic.h>
11 #include <drm/drm_connector.h>
12 #include <drm/drm_damage_helper.h>
13 #include <drm/drm_drv.h>
14 #include <drm/drm_format_helper.h>
15 #include <drm/drm_fourcc.h>
16 #include <drm/drm_framebuffer.h>
17 #include <drm/drm_gem.h>
18 #include <drm/drm_gem_framebuffer_helper.h>
19 #include <drm/drm_print.h>
20 #include <drm/drm_rect.h>
21 #include <drm/drm_simple_kms_helper.h>
22 #include <drm/gud.h>
23
24 #include "gud_internal.h"
25
26 /*
27  * Some userspace rendering loops runs all displays in the same loop.
28  * This means that a fast display will have to wait for a slow one.
29  * For this reason gud does flushing asynchronous by default.
30  * The down side is that in e.g. a single display setup userspace thinks
31  * the display is insanely fast since the driver reports back immediately
32  * that the flush/pageflip is done. This wastes CPU and power.
33  * Such users might want to set this module parameter to false.
34  */
35 static bool gud_async_flush = true;
36 module_param_named(async_flush, gud_async_flush, bool, 0644);
37 MODULE_PARM_DESC(async_flush, "Enable asynchronous flushing [default=true]");
38
39 /*
40  * FIXME: The driver is probably broken on Big Endian machines.
41  * See discussion:
42  * https://lore.kernel.org/dri-devel/CAKb7UvihLX0hgBOP3VBG7O+atwZcUVCPVuBdfmDMpg0NjXe-cQ@mail.gmail.com/
43  */
44
45 static bool gud_is_big_endian(void)
46 {
47 #if defined(__BIG_ENDIAN)
48         return true;
49 #else
50         return false;
51 #endif
52 }
53
54 static size_t gud_xrgb8888_to_r124(u8 *dst, const struct drm_format_info *format,
55                                    void *src, struct drm_framebuffer *fb,
56                                    struct drm_rect *rect)
57 {
58         unsigned int block_width = drm_format_info_block_width(format, 0);
59         unsigned int bits_per_pixel = 8 / block_width;
60         unsigned int x, y, width, height;
61         u8 pix, *pix8, *block = dst; /* Assign to silence compiler warning */
62         size_t len;
63         void *buf;
64
65         WARN_ON_ONCE(format->char_per_block[0] != 1);
66
67         /* Start on a byte boundary */
68         rect->x1 = ALIGN_DOWN(rect->x1, block_width);
69         width = drm_rect_width(rect);
70         height = drm_rect_height(rect);
71         len = drm_format_info_min_pitch(format, 0, width) * height;
72
73         buf = kmalloc(width * height, GFP_KERNEL);
74         if (!buf)
75                 return 0;
76
77         drm_fb_xrgb8888_to_gray8(buf, src, fb, rect);
78         pix8 = buf;
79
80         for (y = 0; y < height; y++) {
81                 for (x = 0; x < width; x++) {
82                         unsigned int pixpos = x % block_width; /* within byte from the left */
83                         unsigned int pixshift = (block_width - pixpos - 1) * bits_per_pixel;
84
85                         if (!pixpos) {
86                                 block = dst++;
87                                 *block = 0;
88                         }
89
90                         pix = (*pix8++) >> (8 - bits_per_pixel);
91                         *block |= pix << pixshift;
92                 }
93         }
94
95         kfree(buf);
96
97         return len;
98 }
99
100 static size_t gud_xrgb8888_to_color(u8 *dst, const struct drm_format_info *format,
101                                     void *src, struct drm_framebuffer *fb,
102                                     struct drm_rect *rect)
103 {
104         unsigned int block_width = drm_format_info_block_width(format, 0);
105         unsigned int bits_per_pixel = 8 / block_width;
106         u8 r, g, b, pix, *block = dst; /* Assign to silence compiler warning */
107         unsigned int x, y, width;
108         u32 *pix32;
109         size_t len;
110
111         /* Start on a byte boundary */
112         rect->x1 = ALIGN_DOWN(rect->x1, block_width);
113         width = drm_rect_width(rect);
114         len = drm_format_info_min_pitch(format, 0, width) * drm_rect_height(rect);
115
116         for (y = rect->y1; y < rect->y2; y++) {
117                 pix32 = src + (y * fb->pitches[0]);
118                 pix32 += rect->x1;
119
120                 for (x = 0; x < width; x++) {
121                         unsigned int pixpos = x % block_width; /* within byte from the left */
122                         unsigned int pixshift = (block_width - pixpos - 1) * bits_per_pixel;
123
124                         if (!pixpos) {
125                                 block = dst++;
126                                 *block = 0;
127                         }
128
129                         r = *pix32 >> 16;
130                         g = *pix32 >> 8;
131                         b = *pix32++;
132
133                         switch (format->format) {
134                         case GUD_DRM_FORMAT_XRGB1111:
135                                 pix = ((r >> 7) << 2) | ((g >> 7) << 1) | (b >> 7);
136                                 break;
137                         default:
138                                 WARN_ON_ONCE(1);
139                                 return len;
140                         }
141
142                         *block |= pix << pixshift;
143                 }
144         }
145
146         return len;
147 }
148
149 static int gud_prep_flush(struct gud_device *gdrm, struct drm_framebuffer *fb,
150                           const struct drm_format_info *format, struct drm_rect *rect,
151                           struct gud_set_buffer_req *req)
152 {
153         struct dma_buf_attachment *import_attach = fb->obj[0]->import_attach;
154         u8 compression = gdrm->compression;
155         struct dma_buf_map map[DRM_FORMAT_MAX_PLANES];
156         struct dma_buf_map map_data[DRM_FORMAT_MAX_PLANES];
157         void *vaddr, *buf;
158         size_t pitch, len;
159         int ret = 0;
160
161         pitch = drm_format_info_min_pitch(format, 0, drm_rect_width(rect));
162         len = pitch * drm_rect_height(rect);
163         if (len > gdrm->bulk_len)
164                 return -E2BIG;
165
166         ret = drm_gem_fb_vmap(fb, map, map_data);
167         if (ret)
168                 return ret;
169
170         vaddr = map_data[0].vaddr;
171
172         ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
173         if (ret)
174                 goto vunmap;
175 retry:
176         if (compression)
177                 buf = gdrm->compress_buf;
178         else
179                 buf = gdrm->bulk_buf;
180
181         /*
182          * Imported buffers are assumed to be write-combined and thus uncached
183          * with slow reads (at least on ARM).
184          */
185         if (format != fb->format) {
186                 if (format->format == GUD_DRM_FORMAT_R1) {
187                         len = gud_xrgb8888_to_r124(buf, format, vaddr, fb, rect);
188                         if (!len) {
189                                 ret = -ENOMEM;
190                                 goto end_cpu_access;
191                         }
192                 } else if (format->format == DRM_FORMAT_R8) {
193                         drm_fb_xrgb8888_to_gray8(buf, vaddr, fb, rect);
194                 } else if (format->format == DRM_FORMAT_RGB565) {
195                         drm_fb_xrgb8888_to_rgb565(buf, vaddr, fb, rect, gud_is_big_endian());
196                 } else {
197                         len = gud_xrgb8888_to_color(buf, format, vaddr, fb, rect);
198                 }
199         } else if (gud_is_big_endian() && format->cpp[0] > 1) {
200                 drm_fb_swab(buf, vaddr, fb, rect, !import_attach);
201         } else if (compression && !import_attach && pitch == fb->pitches[0]) {
202                 /* can compress directly from the framebuffer */
203                 buf = vaddr + rect->y1 * pitch;
204         } else {
205                 drm_fb_memcpy(buf, vaddr, fb, rect);
206         }
207
208         memset(req, 0, sizeof(*req));
209         req->x = cpu_to_le32(rect->x1);
210         req->y = cpu_to_le32(rect->y1);
211         req->width = cpu_to_le32(drm_rect_width(rect));
212         req->height = cpu_to_le32(drm_rect_height(rect));
213         req->length = cpu_to_le32(len);
214
215         if (compression & GUD_COMPRESSION_LZ4) {
216                 int complen;
217
218                 complen = LZ4_compress_default(buf, gdrm->bulk_buf, len, len, gdrm->lz4_comp_mem);
219                 if (complen <= 0) {
220                         compression = 0;
221                         goto retry;
222                 }
223
224                 req->compression = GUD_COMPRESSION_LZ4;
225                 req->compressed_length = cpu_to_le32(complen);
226         }
227
228 end_cpu_access:
229         drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
230 vunmap:
231         drm_gem_fb_vunmap(fb, map);
232
233         return ret;
234 }
235
236 struct gud_usb_bulk_context {
237         struct timer_list timer;
238         struct usb_sg_request sgr;
239 };
240
241 static void gud_usb_bulk_timeout(struct timer_list *t)
242 {
243         struct gud_usb_bulk_context *ctx = from_timer(ctx, t, timer);
244
245         usb_sg_cancel(&ctx->sgr);
246 }
247
248 static int gud_usb_bulk(struct gud_device *gdrm, size_t len)
249 {
250         struct gud_usb_bulk_context ctx;
251         int ret;
252
253         ret = usb_sg_init(&ctx.sgr, gud_to_usb_device(gdrm), gdrm->bulk_pipe, 0,
254                           gdrm->bulk_sgt.sgl, gdrm->bulk_sgt.nents, len, GFP_KERNEL);
255         if (ret)
256                 return ret;
257
258         timer_setup_on_stack(&ctx.timer, gud_usb_bulk_timeout, 0);
259         mod_timer(&ctx.timer, jiffies + msecs_to_jiffies(3000));
260
261         usb_sg_wait(&ctx.sgr);
262
263         if (!del_timer_sync(&ctx.timer))
264                 ret = -ETIMEDOUT;
265         else if (ctx.sgr.status < 0)
266                 ret = ctx.sgr.status;
267         else if (ctx.sgr.bytes != len)
268                 ret = -EIO;
269
270         destroy_timer_on_stack(&ctx.timer);
271
272         return ret;
273 }
274
275 static int gud_flush_rect(struct gud_device *gdrm, struct drm_framebuffer *fb,
276                           const struct drm_format_info *format, struct drm_rect *rect)
277 {
278         struct gud_set_buffer_req req;
279         size_t len, trlen;
280         int ret;
281
282         drm_dbg(&gdrm->drm, "Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
283
284         ret = gud_prep_flush(gdrm, fb, format, rect, &req);
285         if (ret)
286                 return ret;
287
288         len = le32_to_cpu(req.length);
289
290         if (req.compression)
291                 trlen = le32_to_cpu(req.compressed_length);
292         else
293                 trlen = len;
294
295         gdrm->stats_length += len;
296         /* Did it wrap around? */
297         if (gdrm->stats_length <= len && gdrm->stats_actual_length) {
298                 gdrm->stats_length = len;
299                 gdrm->stats_actual_length = 0;
300         }
301         gdrm->stats_actual_length += trlen;
302
303         if (!(gdrm->flags & GUD_DISPLAY_FLAG_FULL_UPDATE) || gdrm->prev_flush_failed) {
304                 ret = gud_usb_set(gdrm, GUD_REQ_SET_BUFFER, 0, &req, sizeof(req));
305                 if (ret)
306                         return ret;
307         }
308
309         ret = gud_usb_bulk(gdrm, trlen);
310         if (ret)
311                 gdrm->stats_num_errors++;
312
313         return ret;
314 }
315
316 void gud_clear_damage(struct gud_device *gdrm)
317 {
318         gdrm->damage.x1 = INT_MAX;
319         gdrm->damage.y1 = INT_MAX;
320         gdrm->damage.x2 = 0;
321         gdrm->damage.y2 = 0;
322 }
323
324 static void gud_add_damage(struct gud_device *gdrm, struct drm_rect *damage)
325 {
326         gdrm->damage.x1 = min(gdrm->damage.x1, damage->x1);
327         gdrm->damage.y1 = min(gdrm->damage.y1, damage->y1);
328         gdrm->damage.x2 = max(gdrm->damage.x2, damage->x2);
329         gdrm->damage.y2 = max(gdrm->damage.y2, damage->y2);
330 }
331
332 static void gud_retry_failed_flush(struct gud_device *gdrm, struct drm_framebuffer *fb,
333                                    struct drm_rect *damage)
334 {
335         /*
336          * pipe_update waits for the worker when the display mode is going to change.
337          * This ensures that the width and height is still the same making it safe to
338          * add back the damage.
339          */
340
341         mutex_lock(&gdrm->damage_lock);
342         if (!gdrm->fb) {
343                 drm_framebuffer_get(fb);
344                 gdrm->fb = fb;
345         }
346         gud_add_damage(gdrm, damage);
347         mutex_unlock(&gdrm->damage_lock);
348
349         /* Retry only once to avoid a possible storm in case of continues errors. */
350         if (!gdrm->prev_flush_failed)
351                 queue_work(system_long_wq, &gdrm->work);
352         gdrm->prev_flush_failed = true;
353 }
354
355 void gud_flush_work(struct work_struct *work)
356 {
357         struct gud_device *gdrm = container_of(work, struct gud_device, work);
358         const struct drm_format_info *format;
359         struct drm_framebuffer *fb;
360         struct drm_rect damage;
361         unsigned int i, lines;
362         int idx, ret = 0;
363         size_t pitch;
364
365         if (!drm_dev_enter(&gdrm->drm, &idx))
366                 return;
367
368         mutex_lock(&gdrm->damage_lock);
369         fb = gdrm->fb;
370         gdrm->fb = NULL;
371         damage = gdrm->damage;
372         gud_clear_damage(gdrm);
373         mutex_unlock(&gdrm->damage_lock);
374
375         if (!fb)
376                 goto out;
377
378         format = fb->format;
379         if (format->format == DRM_FORMAT_XRGB8888 && gdrm->xrgb8888_emulation_format)
380                 format = gdrm->xrgb8888_emulation_format;
381
382         /* Split update if it's too big */
383         pitch = drm_format_info_min_pitch(format, 0, drm_rect_width(&damage));
384         lines = drm_rect_height(&damage);
385
386         if (gdrm->bulk_len < lines * pitch)
387                 lines = gdrm->bulk_len / pitch;
388
389         for (i = 0; i < DIV_ROUND_UP(drm_rect_height(&damage), lines); i++) {
390                 struct drm_rect rect = damage;
391
392                 rect.y1 += i * lines;
393                 rect.y2 = min_t(u32, rect.y1 + lines, damage.y2);
394
395                 ret = gud_flush_rect(gdrm, fb, format, &rect);
396                 if (ret) {
397                         if (ret != -ENODEV && ret != -ECONNRESET &&
398                             ret != -ESHUTDOWN && ret != -EPROTO) {
399                                 bool prev_flush_failed = gdrm->prev_flush_failed;
400
401                                 gud_retry_failed_flush(gdrm, fb, &damage);
402                                 if (!prev_flush_failed)
403                                         dev_err_ratelimited(fb->dev->dev,
404                                                             "Failed to flush framebuffer: error=%d\n", ret);
405                         }
406                         break;
407                 }
408
409                 gdrm->prev_flush_failed = false;
410         }
411
412         drm_framebuffer_put(fb);
413 out:
414         drm_dev_exit(idx);
415 }
416
417 static void gud_fb_queue_damage(struct gud_device *gdrm, struct drm_framebuffer *fb,
418                                 struct drm_rect *damage)
419 {
420         struct drm_framebuffer *old_fb = NULL;
421
422         mutex_lock(&gdrm->damage_lock);
423
424         if (fb != gdrm->fb) {
425                 old_fb = gdrm->fb;
426                 drm_framebuffer_get(fb);
427                 gdrm->fb = fb;
428         }
429
430         gud_add_damage(gdrm, damage);
431
432         mutex_unlock(&gdrm->damage_lock);
433
434         queue_work(system_long_wq, &gdrm->work);
435
436         if (old_fb)
437                 drm_framebuffer_put(old_fb);
438 }
439
440 int gud_pipe_check(struct drm_simple_display_pipe *pipe,
441                    struct drm_plane_state *new_plane_state,
442                    struct drm_crtc_state *new_crtc_state)
443 {
444         struct gud_device *gdrm = to_gud_device(pipe->crtc.dev);
445         struct drm_plane_state *old_plane_state = pipe->plane.state;
446         const struct drm_display_mode *mode = &new_crtc_state->mode;
447         struct drm_atomic_state *state = new_plane_state->state;
448         struct drm_framebuffer *old_fb = old_plane_state->fb;
449         struct drm_connector_state *connector_state = NULL;
450         struct drm_framebuffer *fb = new_plane_state->fb;
451         const struct drm_format_info *format = fb->format;
452         struct drm_connector *connector;
453         unsigned int i, num_properties;
454         struct gud_state_req *req;
455         int idx, ret;
456         size_t len;
457
458         if (WARN_ON_ONCE(!fb))
459                 return -EINVAL;
460
461         if (old_plane_state->rotation != new_plane_state->rotation)
462                 new_crtc_state->mode_changed = true;
463
464         if (old_fb && old_fb->format != format)
465                 new_crtc_state->mode_changed = true;
466
467         if (!new_crtc_state->mode_changed && !new_crtc_state->connectors_changed)
468                 return 0;
469
470         /* Only one connector is supported */
471         if (hweight32(new_crtc_state->connector_mask) != 1)
472                 return -EINVAL;
473
474         if (format->format == DRM_FORMAT_XRGB8888 && gdrm->xrgb8888_emulation_format)
475                 format = gdrm->xrgb8888_emulation_format;
476
477         for_each_new_connector_in_state(state, connector, connector_state, i) {
478                 if (connector_state->crtc)
479                         break;
480         }
481
482         /*
483          * DRM_IOCTL_MODE_OBJ_SETPROPERTY on the rotation property will not have
484          * the connector included in the state.
485          */
486         if (!connector_state) {
487                 struct drm_connector_list_iter conn_iter;
488
489                 drm_connector_list_iter_begin(pipe->crtc.dev, &conn_iter);
490                 drm_for_each_connector_iter(connector, &conn_iter) {
491                         if (connector->state->crtc) {
492                                 connector_state = connector->state;
493                                 break;
494                         }
495                 }
496                 drm_connector_list_iter_end(&conn_iter);
497         }
498
499         if (WARN_ON_ONCE(!connector_state))
500                 return -ENOENT;
501
502         len = struct_size(req, properties,
503                           GUD_PROPERTIES_MAX_NUM + GUD_CONNECTOR_PROPERTIES_MAX_NUM);
504         req = kzalloc(len, GFP_KERNEL);
505         if (!req)
506                 return -ENOMEM;
507
508         gud_from_display_mode(&req->mode, mode);
509
510         req->format = gud_from_fourcc(format->format);
511         if (WARN_ON_ONCE(!req->format)) {
512                 ret = -EINVAL;
513                 goto out;
514         }
515
516         req->connector = drm_connector_index(connector_state->connector);
517
518         ret = gud_connector_fill_properties(connector_state, req->properties);
519         if (ret < 0)
520                 goto out;
521
522         num_properties = ret;
523         for (i = 0; i < gdrm->num_properties; i++) {
524                 u16 prop = gdrm->properties[i];
525                 u64 val;
526
527                 switch (prop) {
528                 case GUD_PROPERTY_ROTATION:
529                         /* DRM UAPI matches the protocol so use value directly */
530                         val = new_plane_state->rotation;
531                         break;
532                 default:
533                         WARN_ON_ONCE(1);
534                         ret = -EINVAL;
535                         goto out;
536                 }
537
538                 req->properties[num_properties + i].prop = cpu_to_le16(prop);
539                 req->properties[num_properties + i].val = cpu_to_le64(val);
540                 num_properties++;
541         }
542
543         if (drm_dev_enter(fb->dev, &idx)) {
544                 len = struct_size(req, properties, num_properties);
545                 ret = gud_usb_set(gdrm, GUD_REQ_SET_STATE_CHECK, 0, req, len);
546                 drm_dev_exit(idx);
547         }  else {
548                 ret = -ENODEV;
549         }
550 out:
551         kfree(req);
552
553         return ret;
554 }
555
556 void gud_pipe_update(struct drm_simple_display_pipe *pipe,
557                      struct drm_plane_state *old_state)
558 {
559         struct drm_device *drm = pipe->crtc.dev;
560         struct gud_device *gdrm = to_gud_device(drm);
561         struct drm_plane_state *state = pipe->plane.state;
562         struct drm_framebuffer *fb = state->fb;
563         struct drm_crtc *crtc = &pipe->crtc;
564         struct drm_rect damage;
565         int idx;
566
567         if (crtc->state->mode_changed || !crtc->state->enable) {
568                 cancel_work_sync(&gdrm->work);
569                 mutex_lock(&gdrm->damage_lock);
570                 if (gdrm->fb) {
571                         drm_framebuffer_put(gdrm->fb);
572                         gdrm->fb = NULL;
573                 }
574                 gud_clear_damage(gdrm);
575                 mutex_unlock(&gdrm->damage_lock);
576         }
577
578         if (!drm_dev_enter(drm, &idx))
579                 return;
580
581         if (!old_state->fb)
582                 gud_usb_set_u8(gdrm, GUD_REQ_SET_CONTROLLER_ENABLE, 1);
583
584         if (fb && (crtc->state->mode_changed || crtc->state->connectors_changed))
585                 gud_usb_set(gdrm, GUD_REQ_SET_STATE_COMMIT, 0, NULL, 0);
586
587         if (crtc->state->active_changed)
588                 gud_usb_set_u8(gdrm, GUD_REQ_SET_DISPLAY_ENABLE, crtc->state->active);
589
590         if (drm_atomic_helper_damage_merged(old_state, state, &damage)) {
591                 if (gdrm->flags & GUD_DISPLAY_FLAG_FULL_UPDATE)
592                         drm_rect_init(&damage, 0, 0, fb->width, fb->height);
593                 gud_fb_queue_damage(gdrm, fb, &damage);
594                 if (!gud_async_flush)
595                         flush_work(&gdrm->work);
596         }
597
598         if (!crtc->state->enable)
599                 gud_usb_set_u8(gdrm, GUD_REQ_SET_CONTROLLER_ENABLE, 0);
600
601         drm_dev_exit(idx);
602 }