1 // SPDX-License-Identifier: MIT
3 * Copyright 2020 Noralf Trønnes
8 #include <linux/workqueue.h>
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>
24 #include "gud_internal.h"
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.
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]");
40 * FIXME: The driver is probably broken on Big Endian machines.
42 * https://lore.kernel.org/dri-devel/CAKb7UvihLX0hgBOP3VBG7O+atwZcUVCPVuBdfmDMpg0NjXe-cQ@mail.gmail.com/
45 static bool gud_is_big_endian(void)
47 #if defined(__BIG_ENDIAN)
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)
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 */
65 WARN_ON_ONCE(format->char_per_block[0] != 1);
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;
73 buf = kmalloc(width * height, GFP_KERNEL);
77 drm_fb_xrgb8888_to_gray8(buf, src, fb, rect);
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;
90 pix = (*pix8++) >> (8 - bits_per_pixel);
91 *block |= pix << pixshift;
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)
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;
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);
116 for (y = rect->y1; y < rect->y2; y++) {
117 pix32 = src + (y * fb->pitches[0]);
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;
133 switch (format->format) {
134 case GUD_DRM_FORMAT_XRGB1111:
135 pix = ((r >> 7) << 2) | ((g >> 7) << 1) | (b >> 7);
142 *block |= pix << pixshift;
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)
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];
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)
166 ret = drm_gem_fb_vmap(fb, map, map_data);
170 vaddr = map_data[0].vaddr;
172 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
177 buf = gdrm->compress_buf;
179 buf = gdrm->bulk_buf;
182 * Imported buffers are assumed to be write-combined and thus uncached
183 * with slow reads (at least on ARM).
185 if (format != fb->format) {
186 if (format->format == GUD_DRM_FORMAT_R1) {
187 len = gud_xrgb8888_to_r124(buf, format, vaddr, fb, rect);
192 } else if (format->format == DRM_FORMAT_RGB565) {
193 drm_fb_xrgb8888_to_rgb565(buf, vaddr, fb, rect, gud_is_big_endian());
195 len = gud_xrgb8888_to_color(buf, format, vaddr, fb, rect);
197 } else if (gud_is_big_endian() && format->cpp[0] > 1) {
198 drm_fb_swab(buf, vaddr, fb, rect, !import_attach);
199 } else if (compression && !import_attach && pitch == fb->pitches[0]) {
200 /* can compress directly from the framebuffer */
201 buf = vaddr + rect->y1 * pitch;
203 drm_fb_memcpy(buf, vaddr, fb, rect);
206 memset(req, 0, sizeof(*req));
207 req->x = cpu_to_le32(rect->x1);
208 req->y = cpu_to_le32(rect->y1);
209 req->width = cpu_to_le32(drm_rect_width(rect));
210 req->height = cpu_to_le32(drm_rect_height(rect));
211 req->length = cpu_to_le32(len);
213 if (compression & GUD_COMPRESSION_LZ4) {
216 complen = LZ4_compress_default(buf, gdrm->bulk_buf, len, len, gdrm->lz4_comp_mem);
222 req->compression = GUD_COMPRESSION_LZ4;
223 req->compressed_length = cpu_to_le32(complen);
227 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
229 drm_gem_fb_vunmap(fb, map);
234 struct gud_usb_bulk_context {
235 struct timer_list timer;
236 struct usb_sg_request sgr;
239 static void gud_usb_bulk_timeout(struct timer_list *t)
241 struct gud_usb_bulk_context *ctx = from_timer(ctx, t, timer);
243 usb_sg_cancel(&ctx->sgr);
246 static int gud_usb_bulk(struct gud_device *gdrm, size_t len)
248 struct gud_usb_bulk_context ctx;
251 ret = usb_sg_init(&ctx.sgr, gud_to_usb_device(gdrm), gdrm->bulk_pipe, 0,
252 gdrm->bulk_sgt.sgl, gdrm->bulk_sgt.nents, len, GFP_KERNEL);
256 timer_setup_on_stack(&ctx.timer, gud_usb_bulk_timeout, 0);
257 mod_timer(&ctx.timer, jiffies + msecs_to_jiffies(3000));
259 usb_sg_wait(&ctx.sgr);
261 if (!del_timer_sync(&ctx.timer))
263 else if (ctx.sgr.status < 0)
264 ret = ctx.sgr.status;
265 else if (ctx.sgr.bytes != len)
268 destroy_timer_on_stack(&ctx.timer);
273 static int gud_flush_rect(struct gud_device *gdrm, struct drm_framebuffer *fb,
274 const struct drm_format_info *format, struct drm_rect *rect)
276 struct gud_set_buffer_req req;
280 drm_dbg(&gdrm->drm, "Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
282 ret = gud_prep_flush(gdrm, fb, format, rect, &req);
286 len = le32_to_cpu(req.length);
289 trlen = le32_to_cpu(req.compressed_length);
293 gdrm->stats_length += len;
294 /* Did it wrap around? */
295 if (gdrm->stats_length <= len && gdrm->stats_actual_length) {
296 gdrm->stats_length = len;
297 gdrm->stats_actual_length = 0;
299 gdrm->stats_actual_length += trlen;
301 if (!(gdrm->flags & GUD_DISPLAY_FLAG_FULL_UPDATE) || gdrm->prev_flush_failed) {
302 ret = gud_usb_set(gdrm, GUD_REQ_SET_BUFFER, 0, &req, sizeof(req));
307 ret = gud_usb_bulk(gdrm, trlen);
309 gdrm->stats_num_errors++;
314 void gud_clear_damage(struct gud_device *gdrm)
316 gdrm->damage.x1 = INT_MAX;
317 gdrm->damage.y1 = INT_MAX;
322 static void gud_add_damage(struct gud_device *gdrm, struct drm_rect *damage)
324 gdrm->damage.x1 = min(gdrm->damage.x1, damage->x1);
325 gdrm->damage.y1 = min(gdrm->damage.y1, damage->y1);
326 gdrm->damage.x2 = max(gdrm->damage.x2, damage->x2);
327 gdrm->damage.y2 = max(gdrm->damage.y2, damage->y2);
330 static void gud_retry_failed_flush(struct gud_device *gdrm, struct drm_framebuffer *fb,
331 struct drm_rect *damage)
334 * pipe_update waits for the worker when the display mode is going to change.
335 * This ensures that the width and height is still the same making it safe to
336 * add back the damage.
339 mutex_lock(&gdrm->damage_lock);
341 drm_framebuffer_get(fb);
344 gud_add_damage(gdrm, damage);
345 mutex_unlock(&gdrm->damage_lock);
347 /* Retry only once to avoid a possible storm in case of continues errors. */
348 if (!gdrm->prev_flush_failed)
349 queue_work(system_long_wq, &gdrm->work);
350 gdrm->prev_flush_failed = true;
353 void gud_flush_work(struct work_struct *work)
355 struct gud_device *gdrm = container_of(work, struct gud_device, work);
356 const struct drm_format_info *format;
357 struct drm_framebuffer *fb;
358 struct drm_rect damage;
359 unsigned int i, lines;
363 if (!drm_dev_enter(&gdrm->drm, &idx))
366 mutex_lock(&gdrm->damage_lock);
369 damage = gdrm->damage;
370 gud_clear_damage(gdrm);
371 mutex_unlock(&gdrm->damage_lock);
377 if (format->format == DRM_FORMAT_XRGB8888 && gdrm->xrgb8888_emulation_format)
378 format = gdrm->xrgb8888_emulation_format;
380 /* Split update if it's too big */
381 pitch = drm_format_info_min_pitch(format, 0, drm_rect_width(&damage));
382 lines = drm_rect_height(&damage);
384 if (gdrm->bulk_len < lines * pitch)
385 lines = gdrm->bulk_len / pitch;
387 for (i = 0; i < DIV_ROUND_UP(drm_rect_height(&damage), lines); i++) {
388 struct drm_rect rect = damage;
390 rect.y1 += i * lines;
391 rect.y2 = min_t(u32, rect.y1 + lines, damage.y2);
393 ret = gud_flush_rect(gdrm, fb, format, &rect);
395 if (ret != -ENODEV && ret != -ECONNRESET &&
396 ret != -ESHUTDOWN && ret != -EPROTO) {
397 bool prev_flush_failed = gdrm->prev_flush_failed;
399 gud_retry_failed_flush(gdrm, fb, &damage);
400 if (!prev_flush_failed)
401 dev_err_ratelimited(fb->dev->dev,
402 "Failed to flush framebuffer: error=%d\n", ret);
407 gdrm->prev_flush_failed = false;
410 drm_framebuffer_put(fb);
415 static void gud_fb_queue_damage(struct gud_device *gdrm, struct drm_framebuffer *fb,
416 struct drm_rect *damage)
418 struct drm_framebuffer *old_fb = NULL;
420 mutex_lock(&gdrm->damage_lock);
422 if (fb != gdrm->fb) {
424 drm_framebuffer_get(fb);
428 gud_add_damage(gdrm, damage);
430 mutex_unlock(&gdrm->damage_lock);
432 queue_work(system_long_wq, &gdrm->work);
435 drm_framebuffer_put(old_fb);
438 int gud_pipe_check(struct drm_simple_display_pipe *pipe,
439 struct drm_plane_state *new_plane_state,
440 struct drm_crtc_state *new_crtc_state)
442 struct gud_device *gdrm = to_gud_device(pipe->crtc.dev);
443 struct drm_plane_state *old_plane_state = pipe->plane.state;
444 const struct drm_display_mode *mode = &new_crtc_state->mode;
445 struct drm_atomic_state *state = new_plane_state->state;
446 struct drm_framebuffer *old_fb = old_plane_state->fb;
447 struct drm_connector_state *connector_state = NULL;
448 struct drm_framebuffer *fb = new_plane_state->fb;
449 const struct drm_format_info *format = fb->format;
450 struct drm_connector *connector;
451 unsigned int i, num_properties;
452 struct gud_state_req *req;
456 if (WARN_ON_ONCE(!fb))
459 if (old_plane_state->rotation != new_plane_state->rotation)
460 new_crtc_state->mode_changed = true;
462 if (old_fb && old_fb->format != format)
463 new_crtc_state->mode_changed = true;
465 if (!new_crtc_state->mode_changed && !new_crtc_state->connectors_changed)
468 /* Only one connector is supported */
469 if (hweight32(new_crtc_state->connector_mask) != 1)
472 if (format->format == DRM_FORMAT_XRGB8888 && gdrm->xrgb8888_emulation_format)
473 format = gdrm->xrgb8888_emulation_format;
475 for_each_new_connector_in_state(state, connector, connector_state, i) {
476 if (connector_state->crtc)
481 * DRM_IOCTL_MODE_OBJ_SETPROPERTY on the rotation property will not have
482 * the connector included in the state.
484 if (!connector_state) {
485 struct drm_connector_list_iter conn_iter;
487 drm_connector_list_iter_begin(pipe->crtc.dev, &conn_iter);
488 drm_for_each_connector_iter(connector, &conn_iter) {
489 if (connector->state->crtc) {
490 connector_state = connector->state;
494 drm_connector_list_iter_end(&conn_iter);
497 if (WARN_ON_ONCE(!connector_state))
500 len = struct_size(req, properties,
501 GUD_PROPERTIES_MAX_NUM + GUD_CONNECTOR_PROPERTIES_MAX_NUM);
502 req = kzalloc(len, GFP_KERNEL);
506 gud_from_display_mode(&req->mode, mode);
508 req->format = gud_from_fourcc(format->format);
509 if (WARN_ON_ONCE(!req->format)) {
514 req->connector = drm_connector_index(connector_state->connector);
516 ret = gud_connector_fill_properties(connector_state, req->properties);
520 num_properties = ret;
521 for (i = 0; i < gdrm->num_properties; i++) {
522 u16 prop = gdrm->properties[i];
526 case GUD_PROPERTY_ROTATION:
527 /* DRM UAPI matches the protocol so use value directly */
528 val = new_plane_state->rotation;
536 req->properties[num_properties + i].prop = cpu_to_le16(prop);
537 req->properties[num_properties + i].val = cpu_to_le64(val);
541 if (drm_dev_enter(fb->dev, &idx)) {
542 len = struct_size(req, properties, num_properties);
543 ret = gud_usb_set(gdrm, GUD_REQ_SET_STATE_CHECK, 0, req, len);
554 void gud_pipe_update(struct drm_simple_display_pipe *pipe,
555 struct drm_plane_state *old_state)
557 struct drm_device *drm = pipe->crtc.dev;
558 struct gud_device *gdrm = to_gud_device(drm);
559 struct drm_plane_state *state = pipe->plane.state;
560 struct drm_framebuffer *fb = state->fb;
561 struct drm_crtc *crtc = &pipe->crtc;
562 struct drm_rect damage;
565 if (crtc->state->mode_changed || !crtc->state->enable) {
566 cancel_work_sync(&gdrm->work);
567 mutex_lock(&gdrm->damage_lock);
569 drm_framebuffer_put(gdrm->fb);
572 gud_clear_damage(gdrm);
573 mutex_unlock(&gdrm->damage_lock);
576 if (!drm_dev_enter(drm, &idx))
580 gud_usb_set_u8(gdrm, GUD_REQ_SET_CONTROLLER_ENABLE, 1);
582 if (fb && (crtc->state->mode_changed || crtc->state->connectors_changed))
583 gud_usb_set(gdrm, GUD_REQ_SET_STATE_COMMIT, 0, NULL, 0);
585 if (crtc->state->active_changed)
586 gud_usb_set_u8(gdrm, GUD_REQ_SET_DISPLAY_ENABLE, crtc->state->active);
588 if (drm_atomic_helper_damage_merged(old_state, state, &damage)) {
589 if (gdrm->flags & GUD_DISPLAY_FLAG_FULL_UPDATE)
590 drm_rect_init(&damage, 0, 0, fb->width, fb->height);
591 gud_fb_queue_damage(gdrm, fb, &damage);
592 if (!gud_async_flush)
593 flush_work(&gdrm->work);
596 if (!crtc->state->enable)
597 gud_usb_set_u8(gdrm, GUD_REQ_SET_CONTROLLER_ENABLE, 0);