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, 0, 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 iosys_map map[DRM_FORMAT_MAX_PLANES];
156 struct iosys_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_R8) {
193 drm_fb_xrgb8888_to_gray8(buf, 0, vaddr, fb, rect);
194 } else if (format->format == DRM_FORMAT_RGB332) {
195 drm_fb_xrgb8888_to_rgb332(buf, 0, vaddr, fb, rect);
196 } else if (format->format == DRM_FORMAT_RGB565) {
197 drm_fb_xrgb8888_to_rgb565(buf, 0, vaddr, fb, rect, gud_is_big_endian());
198 } else if (format->format == DRM_FORMAT_RGB888) {
199 drm_fb_xrgb8888_to_rgb888(buf, 0, vaddr, fb, rect);
201 len = gud_xrgb8888_to_color(buf, format, vaddr, fb, rect);
203 } else if (gud_is_big_endian() && format->cpp[0] > 1) {
204 drm_fb_swab(buf, 0, vaddr, fb, rect, !import_attach);
205 } else if (compression && !import_attach && pitch == fb->pitches[0]) {
206 /* can compress directly from the framebuffer */
207 buf = vaddr + rect->y1 * pitch;
209 drm_fb_memcpy(buf, 0, vaddr, fb, rect);
212 memset(req, 0, sizeof(*req));
213 req->x = cpu_to_le32(rect->x1);
214 req->y = cpu_to_le32(rect->y1);
215 req->width = cpu_to_le32(drm_rect_width(rect));
216 req->height = cpu_to_le32(drm_rect_height(rect));
217 req->length = cpu_to_le32(len);
219 if (compression & GUD_COMPRESSION_LZ4) {
222 complen = LZ4_compress_default(buf, gdrm->bulk_buf, len, len, gdrm->lz4_comp_mem);
228 req->compression = GUD_COMPRESSION_LZ4;
229 req->compressed_length = cpu_to_le32(complen);
233 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
235 drm_gem_fb_vunmap(fb, map);
240 struct gud_usb_bulk_context {
241 struct timer_list timer;
242 struct usb_sg_request sgr;
245 static void gud_usb_bulk_timeout(struct timer_list *t)
247 struct gud_usb_bulk_context *ctx = from_timer(ctx, t, timer);
249 usb_sg_cancel(&ctx->sgr);
252 static int gud_usb_bulk(struct gud_device *gdrm, size_t len)
254 struct gud_usb_bulk_context ctx;
257 ret = usb_sg_init(&ctx.sgr, gud_to_usb_device(gdrm), gdrm->bulk_pipe, 0,
258 gdrm->bulk_sgt.sgl, gdrm->bulk_sgt.nents, len, GFP_KERNEL);
262 timer_setup_on_stack(&ctx.timer, gud_usb_bulk_timeout, 0);
263 mod_timer(&ctx.timer, jiffies + msecs_to_jiffies(3000));
265 usb_sg_wait(&ctx.sgr);
267 if (!del_timer_sync(&ctx.timer))
269 else if (ctx.sgr.status < 0)
270 ret = ctx.sgr.status;
271 else if (ctx.sgr.bytes != len)
274 destroy_timer_on_stack(&ctx.timer);
279 static int gud_flush_rect(struct gud_device *gdrm, struct drm_framebuffer *fb,
280 const struct drm_format_info *format, struct drm_rect *rect)
282 struct gud_set_buffer_req req;
286 drm_dbg(&gdrm->drm, "Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
288 ret = gud_prep_flush(gdrm, fb, format, rect, &req);
292 len = le32_to_cpu(req.length);
295 trlen = le32_to_cpu(req.compressed_length);
299 gdrm->stats_length += len;
300 /* Did it wrap around? */
301 if (gdrm->stats_length <= len && gdrm->stats_actual_length) {
302 gdrm->stats_length = len;
303 gdrm->stats_actual_length = 0;
305 gdrm->stats_actual_length += trlen;
307 if (!(gdrm->flags & GUD_DISPLAY_FLAG_FULL_UPDATE) || gdrm->prev_flush_failed) {
308 ret = gud_usb_set(gdrm, GUD_REQ_SET_BUFFER, 0, &req, sizeof(req));
313 ret = gud_usb_bulk(gdrm, trlen);
315 gdrm->stats_num_errors++;
320 void gud_clear_damage(struct gud_device *gdrm)
322 gdrm->damage.x1 = INT_MAX;
323 gdrm->damage.y1 = INT_MAX;
328 static void gud_add_damage(struct gud_device *gdrm, struct drm_rect *damage)
330 gdrm->damage.x1 = min(gdrm->damage.x1, damage->x1);
331 gdrm->damage.y1 = min(gdrm->damage.y1, damage->y1);
332 gdrm->damage.x2 = max(gdrm->damage.x2, damage->x2);
333 gdrm->damage.y2 = max(gdrm->damage.y2, damage->y2);
336 static void gud_retry_failed_flush(struct gud_device *gdrm, struct drm_framebuffer *fb,
337 struct drm_rect *damage)
340 * pipe_update waits for the worker when the display mode is going to change.
341 * This ensures that the width and height is still the same making it safe to
342 * add back the damage.
345 mutex_lock(&gdrm->damage_lock);
347 drm_framebuffer_get(fb);
350 gud_add_damage(gdrm, damage);
351 mutex_unlock(&gdrm->damage_lock);
353 /* Retry only once to avoid a possible storm in case of continues errors. */
354 if (!gdrm->prev_flush_failed)
355 queue_work(system_long_wq, &gdrm->work);
356 gdrm->prev_flush_failed = true;
359 void gud_flush_work(struct work_struct *work)
361 struct gud_device *gdrm = container_of(work, struct gud_device, work);
362 const struct drm_format_info *format;
363 struct drm_framebuffer *fb;
364 struct drm_rect damage;
365 unsigned int i, lines;
369 if (!drm_dev_enter(&gdrm->drm, &idx))
372 mutex_lock(&gdrm->damage_lock);
375 damage = gdrm->damage;
376 gud_clear_damage(gdrm);
377 mutex_unlock(&gdrm->damage_lock);
383 if (format->format == DRM_FORMAT_XRGB8888 && gdrm->xrgb8888_emulation_format)
384 format = gdrm->xrgb8888_emulation_format;
386 /* Split update if it's too big */
387 pitch = drm_format_info_min_pitch(format, 0, drm_rect_width(&damage));
388 lines = drm_rect_height(&damage);
390 if (gdrm->bulk_len < lines * pitch)
391 lines = gdrm->bulk_len / pitch;
393 for (i = 0; i < DIV_ROUND_UP(drm_rect_height(&damage), lines); i++) {
394 struct drm_rect rect = damage;
396 rect.y1 += i * lines;
397 rect.y2 = min_t(u32, rect.y1 + lines, damage.y2);
399 ret = gud_flush_rect(gdrm, fb, format, &rect);
401 if (ret != -ENODEV && ret != -ECONNRESET &&
402 ret != -ESHUTDOWN && ret != -EPROTO) {
403 bool prev_flush_failed = gdrm->prev_flush_failed;
405 gud_retry_failed_flush(gdrm, fb, &damage);
406 if (!prev_flush_failed)
407 dev_err_ratelimited(fb->dev->dev,
408 "Failed to flush framebuffer: error=%d\n", ret);
413 gdrm->prev_flush_failed = false;
416 drm_framebuffer_put(fb);
421 static void gud_fb_queue_damage(struct gud_device *gdrm, struct drm_framebuffer *fb,
422 struct drm_rect *damage)
424 struct drm_framebuffer *old_fb = NULL;
426 mutex_lock(&gdrm->damage_lock);
428 if (fb != gdrm->fb) {
430 drm_framebuffer_get(fb);
434 gud_add_damage(gdrm, damage);
436 mutex_unlock(&gdrm->damage_lock);
438 queue_work(system_long_wq, &gdrm->work);
441 drm_framebuffer_put(old_fb);
444 int gud_pipe_check(struct drm_simple_display_pipe *pipe,
445 struct drm_plane_state *new_plane_state,
446 struct drm_crtc_state *new_crtc_state)
448 struct gud_device *gdrm = to_gud_device(pipe->crtc.dev);
449 struct drm_plane_state *old_plane_state = pipe->plane.state;
450 const struct drm_display_mode *mode = &new_crtc_state->mode;
451 struct drm_atomic_state *state = new_plane_state->state;
452 struct drm_framebuffer *old_fb = old_plane_state->fb;
453 struct drm_connector_state *connector_state = NULL;
454 struct drm_framebuffer *fb = new_plane_state->fb;
455 const struct drm_format_info *format = fb->format;
456 struct drm_connector *connector;
457 unsigned int i, num_properties;
458 struct gud_state_req *req;
462 if (WARN_ON_ONCE(!fb))
465 if (old_plane_state->rotation != new_plane_state->rotation)
466 new_crtc_state->mode_changed = true;
468 if (old_fb && old_fb->format != format)
469 new_crtc_state->mode_changed = true;
471 if (!new_crtc_state->mode_changed && !new_crtc_state->connectors_changed)
474 /* Only one connector is supported */
475 if (hweight32(new_crtc_state->connector_mask) != 1)
478 if (format->format == DRM_FORMAT_XRGB8888 && gdrm->xrgb8888_emulation_format)
479 format = gdrm->xrgb8888_emulation_format;
481 for_each_new_connector_in_state(state, connector, connector_state, i) {
482 if (connector_state->crtc)
487 * DRM_IOCTL_MODE_OBJ_SETPROPERTY on the rotation property will not have
488 * the connector included in the state.
490 if (!connector_state) {
491 struct drm_connector_list_iter conn_iter;
493 drm_connector_list_iter_begin(pipe->crtc.dev, &conn_iter);
494 drm_for_each_connector_iter(connector, &conn_iter) {
495 if (connector->state->crtc) {
496 connector_state = connector->state;
500 drm_connector_list_iter_end(&conn_iter);
503 if (WARN_ON_ONCE(!connector_state))
506 len = struct_size(req, properties,
507 GUD_PROPERTIES_MAX_NUM + GUD_CONNECTOR_PROPERTIES_MAX_NUM);
508 req = kzalloc(len, GFP_KERNEL);
512 gud_from_display_mode(&req->mode, mode);
514 req->format = gud_from_fourcc(format->format);
515 if (WARN_ON_ONCE(!req->format)) {
520 req->connector = drm_connector_index(connector_state->connector);
522 ret = gud_connector_fill_properties(connector_state, req->properties);
526 num_properties = ret;
527 for (i = 0; i < gdrm->num_properties; i++) {
528 u16 prop = gdrm->properties[i];
532 case GUD_PROPERTY_ROTATION:
533 /* DRM UAPI matches the protocol so use value directly */
534 val = new_plane_state->rotation;
542 req->properties[num_properties + i].prop = cpu_to_le16(prop);
543 req->properties[num_properties + i].val = cpu_to_le64(val);
547 if (drm_dev_enter(fb->dev, &idx)) {
548 len = struct_size(req, properties, num_properties);
549 ret = gud_usb_set(gdrm, GUD_REQ_SET_STATE_CHECK, 0, req, len);
560 void gud_pipe_update(struct drm_simple_display_pipe *pipe,
561 struct drm_plane_state *old_state)
563 struct drm_device *drm = pipe->crtc.dev;
564 struct gud_device *gdrm = to_gud_device(drm);
565 struct drm_plane_state *state = pipe->plane.state;
566 struct drm_framebuffer *fb = state->fb;
567 struct drm_crtc *crtc = &pipe->crtc;
568 struct drm_rect damage;
571 if (crtc->state->mode_changed || !crtc->state->enable) {
572 cancel_work_sync(&gdrm->work);
573 mutex_lock(&gdrm->damage_lock);
575 drm_framebuffer_put(gdrm->fb);
578 gud_clear_damage(gdrm);
579 mutex_unlock(&gdrm->damage_lock);
582 if (!drm_dev_enter(drm, &idx))
586 gud_usb_set_u8(gdrm, GUD_REQ_SET_CONTROLLER_ENABLE, 1);
588 if (fb && (crtc->state->mode_changed || crtc->state->connectors_changed))
589 gud_usb_set(gdrm, GUD_REQ_SET_STATE_COMMIT, 0, NULL, 0);
591 if (crtc->state->active_changed)
592 gud_usb_set_u8(gdrm, GUD_REQ_SET_DISPLAY_ENABLE, crtc->state->active);
594 if (drm_atomic_helper_damage_merged(old_state, state, &damage)) {
595 if (gdrm->flags & GUD_DISPLAY_FLAG_FULL_UPDATE)
596 drm_rect_init(&damage, 0, 0, fb->width, fb->height);
597 gud_fb_queue_damage(gdrm, fb, &damage);
598 if (!gud_async_flush)
599 flush_work(&gdrm->work);
602 if (!crtc->state->enable)
603 gud_usb_set_u8(gdrm, GUD_REQ_SET_CONTROLLER_ENABLE, 0);