udl_transfer.c 6.5 KB

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