xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/udl/udl_transfer.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2012 Red Hat
4*4882a593Smuzhiyun  * based in parts on udlfb.c:
5*4882a593Smuzhiyun  * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
6*4882a593Smuzhiyun  * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
7*4882a593Smuzhiyun  * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <asm/unaligned.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include "udl_drv.h"
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #define MAX_CMD_PIXELS		255
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define RLX_HEADER_BYTES	7
17*4882a593Smuzhiyun #define MIN_RLX_PIX_BYTES       4
18*4882a593Smuzhiyun #define MIN_RLX_CMD_BYTES	(RLX_HEADER_BYTES + MIN_RLX_PIX_BYTES)
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define RLE_HEADER_BYTES	6
21*4882a593Smuzhiyun #define MIN_RLE_PIX_BYTES	3
22*4882a593Smuzhiyun #define MIN_RLE_CMD_BYTES	(RLE_HEADER_BYTES + MIN_RLE_PIX_BYTES)
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define RAW_HEADER_BYTES	6
25*4882a593Smuzhiyun #define MIN_RAW_PIX_BYTES	2
26*4882a593Smuzhiyun #define MIN_RAW_CMD_BYTES	(RAW_HEADER_BYTES + MIN_RAW_PIX_BYTES)
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  * Trims identical data from front and back of line
30*4882a593Smuzhiyun  * Sets new front buffer address and width
31*4882a593Smuzhiyun  * And returns byte count of identical pixels
32*4882a593Smuzhiyun  * Assumes CPU natural alignment (unsigned long)
33*4882a593Smuzhiyun  * for back and front buffer ptrs and width
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun #if 0
36*4882a593Smuzhiyun static int udl_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	int j, k;
39*4882a593Smuzhiyun 	const unsigned long *back = (const unsigned long *) bback;
40*4882a593Smuzhiyun 	const unsigned long *front = (const unsigned long *) *bfront;
41*4882a593Smuzhiyun 	const int width = *width_bytes / sizeof(unsigned long);
42*4882a593Smuzhiyun 	int identical = width;
43*4882a593Smuzhiyun 	int start = width;
44*4882a593Smuzhiyun 	int end = width;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	for (j = 0; j < width; j++) {
47*4882a593Smuzhiyun 		if (back[j] != front[j]) {
48*4882a593Smuzhiyun 			start = j;
49*4882a593Smuzhiyun 			break;
50*4882a593Smuzhiyun 		}
51*4882a593Smuzhiyun 	}
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	for (k = width - 1; k > j; k--) {
54*4882a593Smuzhiyun 		if (back[k] != front[k]) {
55*4882a593Smuzhiyun 			end = k+1;
56*4882a593Smuzhiyun 			break;
57*4882a593Smuzhiyun 		}
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	identical = start + (width - end);
61*4882a593Smuzhiyun 	*bfront = (u8 *) &front[start];
62*4882a593Smuzhiyun 	*width_bytes = (end - start) * sizeof(unsigned long);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	return identical * sizeof(unsigned long);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun #endif
67*4882a593Smuzhiyun 
pixel32_to_be16(const uint32_t pixel)68*4882a593Smuzhiyun static inline u16 pixel32_to_be16(const uint32_t pixel)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	return (((pixel >> 3) & 0x001f) |
71*4882a593Smuzhiyun 		((pixel >> 5) & 0x07e0) |
72*4882a593Smuzhiyun 		((pixel >> 8) & 0xf800));
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
get_pixel_val16(const uint8_t * pixel,int log_bpp)75*4882a593Smuzhiyun static inline u16 get_pixel_val16(const uint8_t *pixel, int log_bpp)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	u16 pixel_val16;
78*4882a593Smuzhiyun 	if (log_bpp == 1)
79*4882a593Smuzhiyun 		pixel_val16 = *(const uint16_t *)pixel;
80*4882a593Smuzhiyun 	else
81*4882a593Smuzhiyun 		pixel_val16 = pixel32_to_be16(*(const uint32_t *)pixel);
82*4882a593Smuzhiyun 	return pixel_val16;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun  * Render a command stream for an encoded horizontal line segment of pixels.
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * A command buffer holds several commands.
89*4882a593Smuzhiyun  * It always begins with a fresh command header
90*4882a593Smuzhiyun  * (the protocol doesn't require this, but we enforce it to allow
91*4882a593Smuzhiyun  * multiple buffers to be potentially encoded and sent in parallel).
92*4882a593Smuzhiyun  * A single command encodes one contiguous horizontal line of pixels
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * The function relies on the client to do all allocation, so that
95*4882a593Smuzhiyun  * rendering can be done directly to output buffers (e.g. USB URBs).
96*4882a593Smuzhiyun  * The function fills the supplied command buffer, providing information
97*4882a593Smuzhiyun  * on where it left off, so the client may call in again with additional
98*4882a593Smuzhiyun  * buffers if the line will take several buffers to complete.
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  * A single command can transmit a maximum of 256 pixels,
101*4882a593Smuzhiyun  * regardless of the compression ratio (protocol design limit).
102*4882a593Smuzhiyun  * To the hardware, 0 for a size byte means 256
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * Rather than 256 pixel commands which are either rl or raw encoded,
105*4882a593Smuzhiyun  * the rlx command simply assumes alternating raw and rl spans within one cmd.
106*4882a593Smuzhiyun  * This has a slightly larger header overhead, but produces more even results.
107*4882a593Smuzhiyun  * It also processes all data (read and write) in a single pass.
108*4882a593Smuzhiyun  * Performance benchmarks of common cases show it having just slightly better
109*4882a593Smuzhiyun  * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
110*4882a593Smuzhiyun  * But for very rl friendly data, will compress not quite as well.
111*4882a593Smuzhiyun  */
udl_compress_hline16(const u8 ** pixel_start_ptr,const u8 * const pixel_end,uint32_t * device_address_ptr,uint8_t ** command_buffer_ptr,const uint8_t * const cmd_buffer_end,int log_bpp)112*4882a593Smuzhiyun static void udl_compress_hline16(
113*4882a593Smuzhiyun 	const u8 **pixel_start_ptr,
114*4882a593Smuzhiyun 	const u8 *const pixel_end,
115*4882a593Smuzhiyun 	uint32_t *device_address_ptr,
116*4882a593Smuzhiyun 	uint8_t **command_buffer_ptr,
117*4882a593Smuzhiyun 	const uint8_t *const cmd_buffer_end, int log_bpp)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	const int bpp = 1 << log_bpp;
120*4882a593Smuzhiyun 	const u8 *pixel = *pixel_start_ptr;
121*4882a593Smuzhiyun 	uint32_t dev_addr  = *device_address_ptr;
122*4882a593Smuzhiyun 	uint8_t *cmd = *command_buffer_ptr;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	while ((pixel_end > pixel) &&
125*4882a593Smuzhiyun 	       (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
126*4882a593Smuzhiyun 		uint8_t *raw_pixels_count_byte = NULL;
127*4882a593Smuzhiyun 		uint8_t *cmd_pixels_count_byte = NULL;
128*4882a593Smuzhiyun 		const u8 *raw_pixel_start = NULL;
129*4882a593Smuzhiyun 		const u8 *cmd_pixel_start, *cmd_pixel_end = NULL;
130*4882a593Smuzhiyun 		uint16_t pixel_val16;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 		*cmd++ = 0xaf;
133*4882a593Smuzhiyun 		*cmd++ = 0x6b;
134*4882a593Smuzhiyun 		*cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
135*4882a593Smuzhiyun 		*cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
136*4882a593Smuzhiyun 		*cmd++ = (uint8_t) ((dev_addr) & 0xFF);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 		cmd_pixels_count_byte = cmd++; /*  we'll know this later */
139*4882a593Smuzhiyun 		cmd_pixel_start = pixel;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 		raw_pixels_count_byte = cmd++; /*  we'll know this later */
142*4882a593Smuzhiyun 		raw_pixel_start = pixel;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 		cmd_pixel_end = pixel + (min3(MAX_CMD_PIXELS + 1UL,
145*4882a593Smuzhiyun 					(unsigned long)(pixel_end - pixel) >> log_bpp,
146*4882a593Smuzhiyun 					(unsigned long)(cmd_buffer_end - 1 - cmd) / 2) << log_bpp);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 		pixel_val16 = get_pixel_val16(pixel, log_bpp);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 		while (pixel < cmd_pixel_end) {
151*4882a593Smuzhiyun 			const u8 *const start = pixel;
152*4882a593Smuzhiyun 			const uint16_t repeating_pixel_val16 = pixel_val16;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 			put_unaligned_be16(pixel_val16, cmd);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 			cmd += 2;
157*4882a593Smuzhiyun 			pixel += bpp;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 			while (pixel < cmd_pixel_end) {
160*4882a593Smuzhiyun 				pixel_val16 = get_pixel_val16(pixel, log_bpp);
161*4882a593Smuzhiyun 				if (pixel_val16 != repeating_pixel_val16)
162*4882a593Smuzhiyun 					break;
163*4882a593Smuzhiyun 				pixel += bpp;
164*4882a593Smuzhiyun 			}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 			if (unlikely(pixel > start + bpp)) {
167*4882a593Smuzhiyun 				/* go back and fill in raw pixel count */
168*4882a593Smuzhiyun 				*raw_pixels_count_byte = (((start -
169*4882a593Smuzhiyun 						raw_pixel_start) >> log_bpp) + 1) & 0xFF;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 				/* immediately after raw data is repeat byte */
172*4882a593Smuzhiyun 				*cmd++ = (((pixel - start) >> log_bpp) - 1) & 0xFF;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 				/* Then start another raw pixel span */
175*4882a593Smuzhiyun 				raw_pixel_start = pixel;
176*4882a593Smuzhiyun 				raw_pixels_count_byte = cmd++;
177*4882a593Smuzhiyun 			}
178*4882a593Smuzhiyun 		}
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 		if (pixel > raw_pixel_start) {
181*4882a593Smuzhiyun 			/* finalize last RAW span */
182*4882a593Smuzhiyun 			*raw_pixels_count_byte = ((pixel - raw_pixel_start) >> log_bpp) & 0xFF;
183*4882a593Smuzhiyun 		} else {
184*4882a593Smuzhiyun 			/* undo unused byte */
185*4882a593Smuzhiyun 			cmd--;
186*4882a593Smuzhiyun 		}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 		*cmd_pixels_count_byte = ((pixel - cmd_pixel_start) >> log_bpp) & 0xFF;
189*4882a593Smuzhiyun 		dev_addr += ((pixel - cmd_pixel_start) >> log_bpp) * 2;
190*4882a593Smuzhiyun 	}
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
193*4882a593Smuzhiyun 		/* Fill leftover bytes with no-ops */
194*4882a593Smuzhiyun 		if (cmd_buffer_end > cmd)
195*4882a593Smuzhiyun 			memset(cmd, 0xAF, cmd_buffer_end - cmd);
196*4882a593Smuzhiyun 		cmd = (uint8_t *) cmd_buffer_end;
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	*command_buffer_ptr = cmd;
200*4882a593Smuzhiyun 	*pixel_start_ptr = pixel;
201*4882a593Smuzhiyun 	*device_address_ptr = dev_addr;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	return;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun /*
207*4882a593Smuzhiyun  * There are 3 copies of every pixel: The front buffer that the fbdev
208*4882a593Smuzhiyun  * client renders to, the actual framebuffer across the USB bus in hardware
209*4882a593Smuzhiyun  * (that we can only write to, slowly, and can never read), and (optionally)
210*4882a593Smuzhiyun  * our shadow copy that tracks what's been sent to that hardware buffer.
211*4882a593Smuzhiyun  */
udl_render_hline(struct drm_device * dev,int log_bpp,struct urb ** urb_ptr,const char * front,char ** urb_buf_ptr,u32 byte_offset,u32 device_byte_offset,u32 byte_width)212*4882a593Smuzhiyun int udl_render_hline(struct drm_device *dev, int log_bpp, struct urb **urb_ptr,
213*4882a593Smuzhiyun 		     const char *front, char **urb_buf_ptr,
214*4882a593Smuzhiyun 		     u32 byte_offset, u32 device_byte_offset,
215*4882a593Smuzhiyun 		     u32 byte_width)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	const u8 *line_start, *line_end, *next_pixel;
218*4882a593Smuzhiyun 	u32 base16 = 0 + (device_byte_offset >> log_bpp) * 2;
219*4882a593Smuzhiyun 	struct urb *urb = *urb_ptr;
220*4882a593Smuzhiyun 	u8 *cmd = *urb_buf_ptr;
221*4882a593Smuzhiyun 	u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	BUG_ON(!(log_bpp == 1 || log_bpp == 2));
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	line_start = (u8 *) (front + byte_offset);
226*4882a593Smuzhiyun 	next_pixel = line_start;
227*4882a593Smuzhiyun 	line_end = next_pixel + byte_width;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	while (next_pixel < line_end) {
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 		udl_compress_hline16(&next_pixel,
232*4882a593Smuzhiyun 			     line_end, &base16,
233*4882a593Smuzhiyun 			     (u8 **) &cmd, (u8 *) cmd_end, log_bpp);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 		if (cmd >= cmd_end) {
236*4882a593Smuzhiyun 			int len = cmd - (u8 *) urb->transfer_buffer;
237*4882a593Smuzhiyun 			int ret = udl_submit_urb(dev, urb, len);
238*4882a593Smuzhiyun 			if (ret)
239*4882a593Smuzhiyun 				return ret;
240*4882a593Smuzhiyun 			urb = udl_get_urb(dev);
241*4882a593Smuzhiyun 			if (!urb)
242*4882a593Smuzhiyun 				return -EAGAIN;
243*4882a593Smuzhiyun 			*urb_ptr = urb;
244*4882a593Smuzhiyun 			cmd = urb->transfer_buffer;
245*4882a593Smuzhiyun 			cmd_end = &cmd[urb->transfer_buffer_length];
246*4882a593Smuzhiyun 		}
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	*urb_buf_ptr = cmd;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	return 0;
252*4882a593Smuzhiyun }
253