18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012 Red Hat
48c2ecf20Sopenharmony_ci * based in parts on udlfb.c:
58c2ecf20Sopenharmony_ci * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
68c2ecf20Sopenharmony_ci * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
78c2ecf20Sopenharmony_ci * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "udl_drv.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define MAX_CMD_PIXELS		255
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define RLX_HEADER_BYTES	7
178c2ecf20Sopenharmony_ci#define MIN_RLX_PIX_BYTES       4
188c2ecf20Sopenharmony_ci#define MIN_RLX_CMD_BYTES	(RLX_HEADER_BYTES + MIN_RLX_PIX_BYTES)
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define RLE_HEADER_BYTES	6
218c2ecf20Sopenharmony_ci#define MIN_RLE_PIX_BYTES	3
228c2ecf20Sopenharmony_ci#define MIN_RLE_CMD_BYTES	(RLE_HEADER_BYTES + MIN_RLE_PIX_BYTES)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define RAW_HEADER_BYTES	6
258c2ecf20Sopenharmony_ci#define MIN_RAW_PIX_BYTES	2
268c2ecf20Sopenharmony_ci#define MIN_RAW_CMD_BYTES	(RAW_HEADER_BYTES + MIN_RAW_PIX_BYTES)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * Trims identical data from front and back of line
308c2ecf20Sopenharmony_ci * Sets new front buffer address and width
318c2ecf20Sopenharmony_ci * And returns byte count of identical pixels
328c2ecf20Sopenharmony_ci * Assumes CPU natural alignment (unsigned long)
338c2ecf20Sopenharmony_ci * for back and front buffer ptrs and width
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci#if 0
368c2ecf20Sopenharmony_cistatic int udl_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	int j, k;
398c2ecf20Sopenharmony_ci	const unsigned long *back = (const unsigned long *) bback;
408c2ecf20Sopenharmony_ci	const unsigned long *front = (const unsigned long *) *bfront;
418c2ecf20Sopenharmony_ci	const int width = *width_bytes / sizeof(unsigned long);
428c2ecf20Sopenharmony_ci	int identical = width;
438c2ecf20Sopenharmony_ci	int start = width;
448c2ecf20Sopenharmony_ci	int end = width;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	for (j = 0; j < width; j++) {
478c2ecf20Sopenharmony_ci		if (back[j] != front[j]) {
488c2ecf20Sopenharmony_ci			start = j;
498c2ecf20Sopenharmony_ci			break;
508c2ecf20Sopenharmony_ci		}
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	for (k = width - 1; k > j; k--) {
548c2ecf20Sopenharmony_ci		if (back[k] != front[k]) {
558c2ecf20Sopenharmony_ci			end = k+1;
568c2ecf20Sopenharmony_ci			break;
578c2ecf20Sopenharmony_ci		}
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	identical = start + (width - end);
618c2ecf20Sopenharmony_ci	*bfront = (u8 *) &front[start];
628c2ecf20Sopenharmony_ci	*width_bytes = (end - start) * sizeof(unsigned long);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	return identical * sizeof(unsigned long);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci#endif
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic inline u16 pixel32_to_be16(const uint32_t pixel)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	return (((pixel >> 3) & 0x001f) |
718c2ecf20Sopenharmony_ci		((pixel >> 5) & 0x07e0) |
728c2ecf20Sopenharmony_ci		((pixel >> 8) & 0xf800));
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic inline u16 get_pixel_val16(const uint8_t *pixel, int log_bpp)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	u16 pixel_val16;
788c2ecf20Sopenharmony_ci	if (log_bpp == 1)
798c2ecf20Sopenharmony_ci		pixel_val16 = *(const uint16_t *)pixel;
808c2ecf20Sopenharmony_ci	else
818c2ecf20Sopenharmony_ci		pixel_val16 = pixel32_to_be16(*(const uint32_t *)pixel);
828c2ecf20Sopenharmony_ci	return pixel_val16;
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * Render a command stream for an encoded horizontal line segment of pixels.
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * A command buffer holds several commands.
898c2ecf20Sopenharmony_ci * It always begins with a fresh command header
908c2ecf20Sopenharmony_ci * (the protocol doesn't require this, but we enforce it to allow
918c2ecf20Sopenharmony_ci * multiple buffers to be potentially encoded and sent in parallel).
928c2ecf20Sopenharmony_ci * A single command encodes one contiguous horizontal line of pixels
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * The function relies on the client to do all allocation, so that
958c2ecf20Sopenharmony_ci * rendering can be done directly to output buffers (e.g. USB URBs).
968c2ecf20Sopenharmony_ci * The function fills the supplied command buffer, providing information
978c2ecf20Sopenharmony_ci * on where it left off, so the client may call in again with additional
988c2ecf20Sopenharmony_ci * buffers if the line will take several buffers to complete.
998c2ecf20Sopenharmony_ci *
1008c2ecf20Sopenharmony_ci * A single command can transmit a maximum of 256 pixels,
1018c2ecf20Sopenharmony_ci * regardless of the compression ratio (protocol design limit).
1028c2ecf20Sopenharmony_ci * To the hardware, 0 for a size byte means 256
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci * Rather than 256 pixel commands which are either rl or raw encoded,
1058c2ecf20Sopenharmony_ci * the rlx command simply assumes alternating raw and rl spans within one cmd.
1068c2ecf20Sopenharmony_ci * This has a slightly larger header overhead, but produces more even results.
1078c2ecf20Sopenharmony_ci * It also processes all data (read and write) in a single pass.
1088c2ecf20Sopenharmony_ci * Performance benchmarks of common cases show it having just slightly better
1098c2ecf20Sopenharmony_ci * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
1108c2ecf20Sopenharmony_ci * But for very rl friendly data, will compress not quite as well.
1118c2ecf20Sopenharmony_ci */
1128c2ecf20Sopenharmony_cistatic void udl_compress_hline16(
1138c2ecf20Sopenharmony_ci	const u8 **pixel_start_ptr,
1148c2ecf20Sopenharmony_ci	const u8 *const pixel_end,
1158c2ecf20Sopenharmony_ci	uint32_t *device_address_ptr,
1168c2ecf20Sopenharmony_ci	uint8_t **command_buffer_ptr,
1178c2ecf20Sopenharmony_ci	const uint8_t *const cmd_buffer_end, int log_bpp)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	const int bpp = 1 << log_bpp;
1208c2ecf20Sopenharmony_ci	const u8 *pixel = *pixel_start_ptr;
1218c2ecf20Sopenharmony_ci	uint32_t dev_addr  = *device_address_ptr;
1228c2ecf20Sopenharmony_ci	uint8_t *cmd = *command_buffer_ptr;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	while ((pixel_end > pixel) &&
1258c2ecf20Sopenharmony_ci	       (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
1268c2ecf20Sopenharmony_ci		uint8_t *raw_pixels_count_byte = NULL;
1278c2ecf20Sopenharmony_ci		uint8_t *cmd_pixels_count_byte = NULL;
1288c2ecf20Sopenharmony_ci		const u8 *raw_pixel_start = NULL;
1298c2ecf20Sopenharmony_ci		const u8 *cmd_pixel_start, *cmd_pixel_end = NULL;
1308c2ecf20Sopenharmony_ci		uint16_t pixel_val16;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci		*cmd++ = 0xaf;
1338c2ecf20Sopenharmony_ci		*cmd++ = 0x6b;
1348c2ecf20Sopenharmony_ci		*cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
1358c2ecf20Sopenharmony_ci		*cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
1368c2ecf20Sopenharmony_ci		*cmd++ = (uint8_t) ((dev_addr) & 0xFF);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		cmd_pixels_count_byte = cmd++; /*  we'll know this later */
1398c2ecf20Sopenharmony_ci		cmd_pixel_start = pixel;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		raw_pixels_count_byte = cmd++; /*  we'll know this later */
1428c2ecf20Sopenharmony_ci		raw_pixel_start = pixel;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci		cmd_pixel_end = pixel + (min3(MAX_CMD_PIXELS + 1UL,
1458c2ecf20Sopenharmony_ci					(unsigned long)(pixel_end - pixel) >> log_bpp,
1468c2ecf20Sopenharmony_ci					(unsigned long)(cmd_buffer_end - 1 - cmd) / 2) << log_bpp);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci		pixel_val16 = get_pixel_val16(pixel, log_bpp);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci		while (pixel < cmd_pixel_end) {
1518c2ecf20Sopenharmony_ci			const u8 *const start = pixel;
1528c2ecf20Sopenharmony_ci			const uint16_t repeating_pixel_val16 = pixel_val16;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci			put_unaligned_be16(pixel_val16, cmd);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci			cmd += 2;
1578c2ecf20Sopenharmony_ci			pixel += bpp;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci			while (pixel < cmd_pixel_end) {
1608c2ecf20Sopenharmony_ci				pixel_val16 = get_pixel_val16(pixel, log_bpp);
1618c2ecf20Sopenharmony_ci				if (pixel_val16 != repeating_pixel_val16)
1628c2ecf20Sopenharmony_ci					break;
1638c2ecf20Sopenharmony_ci				pixel += bpp;
1648c2ecf20Sopenharmony_ci			}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci			if (unlikely(pixel > start + bpp)) {
1678c2ecf20Sopenharmony_ci				/* go back and fill in raw pixel count */
1688c2ecf20Sopenharmony_ci				*raw_pixels_count_byte = (((start -
1698c2ecf20Sopenharmony_ci						raw_pixel_start) >> log_bpp) + 1) & 0xFF;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci				/* immediately after raw data is repeat byte */
1728c2ecf20Sopenharmony_ci				*cmd++ = (((pixel - start) >> log_bpp) - 1) & 0xFF;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci				/* Then start another raw pixel span */
1758c2ecf20Sopenharmony_ci				raw_pixel_start = pixel;
1768c2ecf20Sopenharmony_ci				raw_pixels_count_byte = cmd++;
1778c2ecf20Sopenharmony_ci			}
1788c2ecf20Sopenharmony_ci		}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci		if (pixel > raw_pixel_start) {
1818c2ecf20Sopenharmony_ci			/* finalize last RAW span */
1828c2ecf20Sopenharmony_ci			*raw_pixels_count_byte = ((pixel - raw_pixel_start) >> log_bpp) & 0xFF;
1838c2ecf20Sopenharmony_ci		} else {
1848c2ecf20Sopenharmony_ci			/* undo unused byte */
1858c2ecf20Sopenharmony_ci			cmd--;
1868c2ecf20Sopenharmony_ci		}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci		*cmd_pixels_count_byte = ((pixel - cmd_pixel_start) >> log_bpp) & 0xFF;
1898c2ecf20Sopenharmony_ci		dev_addr += ((pixel - cmd_pixel_start) >> log_bpp) * 2;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
1938c2ecf20Sopenharmony_ci		/* Fill leftover bytes with no-ops */
1948c2ecf20Sopenharmony_ci		if (cmd_buffer_end > cmd)
1958c2ecf20Sopenharmony_ci			memset(cmd, 0xAF, cmd_buffer_end - cmd);
1968c2ecf20Sopenharmony_ci		cmd = (uint8_t *) cmd_buffer_end;
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	*command_buffer_ptr = cmd;
2008c2ecf20Sopenharmony_ci	*pixel_start_ptr = pixel;
2018c2ecf20Sopenharmony_ci	*device_address_ptr = dev_addr;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	return;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci/*
2078c2ecf20Sopenharmony_ci * There are 3 copies of every pixel: The front buffer that the fbdev
2088c2ecf20Sopenharmony_ci * client renders to, the actual framebuffer across the USB bus in hardware
2098c2ecf20Sopenharmony_ci * (that we can only write to, slowly, and can never read), and (optionally)
2108c2ecf20Sopenharmony_ci * our shadow copy that tracks what's been sent to that hardware buffer.
2118c2ecf20Sopenharmony_ci */
2128c2ecf20Sopenharmony_ciint udl_render_hline(struct drm_device *dev, int log_bpp, struct urb **urb_ptr,
2138c2ecf20Sopenharmony_ci		     const char *front, char **urb_buf_ptr,
2148c2ecf20Sopenharmony_ci		     u32 byte_offset, u32 device_byte_offset,
2158c2ecf20Sopenharmony_ci		     u32 byte_width)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	const u8 *line_start, *line_end, *next_pixel;
2188c2ecf20Sopenharmony_ci	u32 base16 = 0 + (device_byte_offset >> log_bpp) * 2;
2198c2ecf20Sopenharmony_ci	struct urb *urb = *urb_ptr;
2208c2ecf20Sopenharmony_ci	u8 *cmd = *urb_buf_ptr;
2218c2ecf20Sopenharmony_ci	u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	BUG_ON(!(log_bpp == 1 || log_bpp == 2));
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	line_start = (u8 *) (front + byte_offset);
2268c2ecf20Sopenharmony_ci	next_pixel = line_start;
2278c2ecf20Sopenharmony_ci	line_end = next_pixel + byte_width;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	while (next_pixel < line_end) {
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci		udl_compress_hline16(&next_pixel,
2328c2ecf20Sopenharmony_ci			     line_end, &base16,
2338c2ecf20Sopenharmony_ci			     (u8 **) &cmd, (u8 *) cmd_end, log_bpp);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci		if (cmd >= cmd_end) {
2368c2ecf20Sopenharmony_ci			int len = cmd - (u8 *) urb->transfer_buffer;
2378c2ecf20Sopenharmony_ci			int ret = udl_submit_urb(dev, urb, len);
2388c2ecf20Sopenharmony_ci			if (ret)
2398c2ecf20Sopenharmony_ci				return ret;
2408c2ecf20Sopenharmony_ci			urb = udl_get_urb(dev);
2418c2ecf20Sopenharmony_ci			if (!urb)
2428c2ecf20Sopenharmony_ci				return -EAGAIN;
2438c2ecf20Sopenharmony_ci			*urb_ptr = urb;
2448c2ecf20Sopenharmony_ci			cmd = urb->transfer_buffer;
2458c2ecf20Sopenharmony_ci			cmd_end = &cmd[urb->transfer_buffer_length];
2468c2ecf20Sopenharmony_ci		}
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	*urb_buf_ptr = cmd;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return 0;
2528c2ecf20Sopenharmony_ci}
253