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