Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef __842_H__
0004 #define __842_H__
0005 
0006 /* The 842 compressed format is made up of multiple blocks, each of
0007  * which have the format:
0008  *
0009  * <template>[arg1][arg2][arg3][arg4]
0010  *
0011  * where there are between 0 and 4 template args, depending on the specific
0012  * template operation.  For normal operations, each arg is either a specific
0013  * number of data bytes to add to the output buffer, or an index pointing
0014  * to a previously-written number of data bytes to copy to the output buffer.
0015  *
0016  * The template code is a 5-bit value.  This code indicates what to do with
0017  * the following data.  Template codes from 0 to 0x19 should use the template
0018  * table, the static "decomp_ops" table used in decompress.  For each template
0019  * (table row), there are between 1 and 4 actions; each action corresponds to
0020  * an arg following the template code bits.  Each action is either a "data"
0021  * type action, or a "index" type action, and each action results in 2, 4, or 8
0022  * bytes being written to the output buffer.  Each template (i.e. all actions
0023  * in the table row) will add up to 8 bytes being written to the output buffer.
0024  * Any row with less than 4 actions is padded with noop actions, indicated by
0025  * N0 (for which there is no corresponding arg in the compressed data buffer).
0026  *
0027  * "Data" actions, indicated in the table by D2, D4, and D8, mean that the
0028  * corresponding arg is 2, 4, or 8 bytes, respectively, in the compressed data
0029  * buffer should be copied directly to the output buffer.
0030  *
0031  * "Index" actions, indicated in the table by I2, I4, and I8, mean the
0032  * corresponding arg is an index parameter that points to, respectively, a 2,
0033  * 4, or 8 byte value already in the output buffer, that should be copied to
0034  * the end of the output buffer.  Essentially, the index points to a position
0035  * in a ring buffer that contains the last N bytes of output buffer data.
0036  * The number of bits for each index's arg are: 8 bits for I2, 9 bits for I4,
0037  * and 8 bits for I8.  Since each index points to a 2, 4, or 8 byte section,
0038  * this means that I2 can reference 512 bytes ((2^8 bits = 256) * 2 bytes), I4
0039  * can reference 2048 bytes ((2^9 = 512) * 4 bytes), and I8 can reference 2048
0040  * bytes ((2^8 = 256) * 8 bytes).  Think of it as a kind-of ring buffer for
0041  * each of I2, I4, and I8 that are updated for each byte written to the output
0042  * buffer.  In this implementation, the output buffer is directly used for each
0043  * index; there is no additional memory required.  Note that the index is into
0044  * a ring buffer, not a sliding window; for example, if there have been 260
0045  * bytes written to the output buffer, an I2 index of 0 would index to byte 256
0046  * in the output buffer, while an I2 index of 16 would index to byte 16 in the
0047  * output buffer.
0048  *
0049  * There are also 3 special template codes; 0x1b for "repeat", 0x1c for
0050  * "zeros", and 0x1e for "end".  The "repeat" operation is followed by a 6 bit
0051  * arg N indicating how many times to repeat.  The last 8 bytes written to the
0052  * output buffer are written again to the output buffer, N + 1 times.  The
0053  * "zeros" operation, which has no arg bits, writes 8 zeros to the output
0054  * buffer.  The "end" operation, which also has no arg bits, signals the end
0055  * of the compressed data.  There may be some number of padding (don't care,
0056  * but usually 0) bits after the "end" operation bits, to fill the buffer
0057  * length to a specific byte multiple (usually a multiple of 8, 16, or 32
0058  * bytes).
0059  *
0060  * This software implementation also uses one of the undefined template values,
0061  * 0x1d as a special "short data" template code, to represent less than 8 bytes
0062  * of uncompressed data.  It is followed by a 3 bit arg N indicating how many
0063  * data bytes will follow, and then N bytes of data, which should be copied to
0064  * the output buffer.  This allows the software 842 compressor to accept input
0065  * buffers that are not an exact multiple of 8 bytes long.  However, those
0066  * compressed buffers containing this sw-only template will be rejected by
0067  * the 842 hardware decompressor, and must be decompressed with this software
0068  * library.  The 842 software compression module includes a parameter to
0069  * disable using this sw-only "short data" template, and instead simply
0070  * reject any input buffer that is not a multiple of 8 bytes long.
0071  *
0072  * After all actions for each operation code are processed, another template
0073  * code is in the next 5 bits.  The decompression ends once the "end" template
0074  * code is detected.
0075  */
0076 
0077 #include <linux/module.h>
0078 #include <linux/kernel.h>
0079 #include <linux/bitops.h>
0080 #include <linux/crc32.h>
0081 #include <asm/unaligned.h>
0082 
0083 #include <linux/sw842.h>
0084 
0085 /* special templates */
0086 #define OP_REPEAT   (0x1B)
0087 #define OP_ZEROS    (0x1C)
0088 #define OP_END      (0x1E)
0089 
0090 /* sw only template - this is not in the hw design; it's used only by this
0091  * software compressor and decompressor, to allow input buffers that aren't
0092  * a multiple of 8.
0093  */
0094 #define OP_SHORT_DATA   (0x1D)
0095 
0096 /* additional bits of each op param */
0097 #define OP_BITS     (5)
0098 #define REPEAT_BITS (6)
0099 #define SHORT_DATA_BITS (3)
0100 #define I2_BITS     (8)
0101 #define I4_BITS     (9)
0102 #define I8_BITS     (8)
0103 #define CRC_BITS    (32)
0104 
0105 #define REPEAT_BITS_MAX     (0x3f)
0106 #define SHORT_DATA_BITS_MAX (0x7)
0107 
0108 /* Arbitrary values used to indicate action */
0109 #define OP_ACTION   (0x70)
0110 #define OP_ACTION_INDEX (0x10)
0111 #define OP_ACTION_DATA  (0x20)
0112 #define OP_ACTION_NOOP  (0x40)
0113 #define OP_AMOUNT   (0x0f)
0114 #define OP_AMOUNT_0 (0x00)
0115 #define OP_AMOUNT_2 (0x02)
0116 #define OP_AMOUNT_4 (0x04)
0117 #define OP_AMOUNT_8 (0x08)
0118 
0119 #define D2      (OP_ACTION_DATA  | OP_AMOUNT_2)
0120 #define D4      (OP_ACTION_DATA  | OP_AMOUNT_4)
0121 #define D8      (OP_ACTION_DATA  | OP_AMOUNT_8)
0122 #define I2      (OP_ACTION_INDEX | OP_AMOUNT_2)
0123 #define I4      (OP_ACTION_INDEX | OP_AMOUNT_4)
0124 #define I8      (OP_ACTION_INDEX | OP_AMOUNT_8)
0125 #define N0      (OP_ACTION_NOOP  | OP_AMOUNT_0)
0126 
0127 /* the max of the regular templates - not including the special templates */
0128 #define OPS_MAX     (0x1a)
0129 
0130 #endif