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