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