Linux Audio

Check our new training course

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