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