udl_transfer.c 7.7 KB

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