xref: /OK3568_Linux_fs/kernel/lib/842/842.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun #ifndef __842_H__
4*4882a593Smuzhiyun #define __842_H__
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun /* The 842 compressed format is made up of multiple blocks, each of
7*4882a593Smuzhiyun  * which have the format:
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * <template>[arg1][arg2][arg3][arg4]
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * where there are between 0 and 4 template args, depending on the specific
12*4882a593Smuzhiyun  * template operation.  For normal operations, each arg is either a specific
13*4882a593Smuzhiyun  * number of data bytes to add to the output buffer, or an index pointing
14*4882a593Smuzhiyun  * to a previously-written number of data bytes to copy to the output buffer.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * The template code is a 5-bit value.  This code indicates what to do with
17*4882a593Smuzhiyun  * the following data.  Template codes from 0 to 0x19 should use the template
18*4882a593Smuzhiyun  * table, the static "decomp_ops" table used in decompress.  For each template
19*4882a593Smuzhiyun  * (table row), there are between 1 and 4 actions; each action corresponds to
20*4882a593Smuzhiyun  * an arg following the template code bits.  Each action is either a "data"
21*4882a593Smuzhiyun  * type action, or a "index" type action, and each action results in 2, 4, or 8
22*4882a593Smuzhiyun  * bytes being written to the output buffer.  Each template (i.e. all actions
23*4882a593Smuzhiyun  * in the table row) will add up to 8 bytes being written to the output buffer.
24*4882a593Smuzhiyun  * Any row with less than 4 actions is padded with noop actions, indicated by
25*4882a593Smuzhiyun  * N0 (for which there is no corresponding arg in the compressed data buffer).
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * "Data" actions, indicated in the table by D2, D4, and D8, mean that the
28*4882a593Smuzhiyun  * corresponding arg is 2, 4, or 8 bytes, respectively, in the compressed data
29*4882a593Smuzhiyun  * buffer should be copied directly to the output buffer.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * "Index" actions, indicated in the table by I2, I4, and I8, mean the
32*4882a593Smuzhiyun  * corresponding arg is an index parameter that points to, respectively, a 2,
33*4882a593Smuzhiyun  * 4, or 8 byte value already in the output buffer, that should be copied to
34*4882a593Smuzhiyun  * the end of the output buffer.  Essentially, the index points to a position
35*4882a593Smuzhiyun  * in a ring buffer that contains the last N bytes of output buffer data.
36*4882a593Smuzhiyun  * The number of bits for each index's arg are: 8 bits for I2, 9 bits for I4,
37*4882a593Smuzhiyun  * and 8 bits for I8.  Since each index points to a 2, 4, or 8 byte section,
38*4882a593Smuzhiyun  * this means that I2 can reference 512 bytes ((2^8 bits = 256) * 2 bytes), I4
39*4882a593Smuzhiyun  * can reference 2048 bytes ((2^9 = 512) * 4 bytes), and I8 can reference 2048
40*4882a593Smuzhiyun  * bytes ((2^8 = 256) * 8 bytes).  Think of it as a kind-of ring buffer for
41*4882a593Smuzhiyun  * each of I2, I4, and I8 that are updated for each byte written to the output
42*4882a593Smuzhiyun  * buffer.  In this implementation, the output buffer is directly used for each
43*4882a593Smuzhiyun  * index; there is no additional memory required.  Note that the index is into
44*4882a593Smuzhiyun  * a ring buffer, not a sliding window; for example, if there have been 260
45*4882a593Smuzhiyun  * bytes written to the output buffer, an I2 index of 0 would index to byte 256
46*4882a593Smuzhiyun  * in the output buffer, while an I2 index of 16 would index to byte 16 in the
47*4882a593Smuzhiyun  * output buffer.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * There are also 3 special template codes; 0x1b for "repeat", 0x1c for
50*4882a593Smuzhiyun  * "zeros", and 0x1e for "end".  The "repeat" operation is followed by a 6 bit
51*4882a593Smuzhiyun  * arg N indicating how many times to repeat.  The last 8 bytes written to the
52*4882a593Smuzhiyun  * output buffer are written again to the output buffer, N + 1 times.  The
53*4882a593Smuzhiyun  * "zeros" operation, which has no arg bits, writes 8 zeros to the output
54*4882a593Smuzhiyun  * buffer.  The "end" operation, which also has no arg bits, signals the end
55*4882a593Smuzhiyun  * of the compressed data.  There may be some number of padding (don't care,
56*4882a593Smuzhiyun  * but usually 0) bits after the "end" operation bits, to fill the buffer
57*4882a593Smuzhiyun  * length to a specific byte multiple (usually a multiple of 8, 16, or 32
58*4882a593Smuzhiyun  * bytes).
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * This software implementation also uses one of the undefined template values,
61*4882a593Smuzhiyun  * 0x1d as a special "short data" template code, to represent less than 8 bytes
62*4882a593Smuzhiyun  * of uncompressed data.  It is followed by a 3 bit arg N indicating how many
63*4882a593Smuzhiyun  * data bytes will follow, and then N bytes of data, which should be copied to
64*4882a593Smuzhiyun  * the output buffer.  This allows the software 842 compressor to accept input
65*4882a593Smuzhiyun  * buffers that are not an exact multiple of 8 bytes long.  However, those
66*4882a593Smuzhiyun  * compressed buffers containing this sw-only template will be rejected by
67*4882a593Smuzhiyun  * the 842 hardware decompressor, and must be decompressed with this software
68*4882a593Smuzhiyun  * library.  The 842 software compression module includes a parameter to
69*4882a593Smuzhiyun  * disable using this sw-only "short data" template, and instead simply
70*4882a593Smuzhiyun  * reject any input buffer that is not a multiple of 8 bytes long.
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * After all actions for each operation code are processed, another template
73*4882a593Smuzhiyun  * code is in the next 5 bits.  The decompression ends once the "end" template
74*4882a593Smuzhiyun  * code is detected.
75*4882a593Smuzhiyun  */
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #include <linux/module.h>
78*4882a593Smuzhiyun #include <linux/kernel.h>
79*4882a593Smuzhiyun #include <linux/bitops.h>
80*4882a593Smuzhiyun #include <linux/crc32.h>
81*4882a593Smuzhiyun #include <asm/unaligned.h>
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #include <linux/sw842.h>
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /* special templates */
86*4882a593Smuzhiyun #define OP_REPEAT	(0x1B)
87*4882a593Smuzhiyun #define OP_ZEROS	(0x1C)
88*4882a593Smuzhiyun #define OP_END		(0x1E)
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* sw only template - this is not in the hw design; it's used only by this
91*4882a593Smuzhiyun  * software compressor and decompressor, to allow input buffers that aren't
92*4882a593Smuzhiyun  * a multiple of 8.
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun #define OP_SHORT_DATA	(0x1D)
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /* additional bits of each op param */
97*4882a593Smuzhiyun #define OP_BITS		(5)
98*4882a593Smuzhiyun #define REPEAT_BITS	(6)
99*4882a593Smuzhiyun #define SHORT_DATA_BITS	(3)
100*4882a593Smuzhiyun #define I2_BITS		(8)
101*4882a593Smuzhiyun #define I4_BITS		(9)
102*4882a593Smuzhiyun #define I8_BITS		(8)
103*4882a593Smuzhiyun #define CRC_BITS	(32)
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun #define REPEAT_BITS_MAX		(0x3f)
106*4882a593Smuzhiyun #define SHORT_DATA_BITS_MAX	(0x7)
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /* Arbitrary values used to indicate action */
109*4882a593Smuzhiyun #define OP_ACTION	(0x70)
110*4882a593Smuzhiyun #define OP_ACTION_INDEX	(0x10)
111*4882a593Smuzhiyun #define OP_ACTION_DATA	(0x20)
112*4882a593Smuzhiyun #define OP_ACTION_NOOP	(0x40)
113*4882a593Smuzhiyun #define OP_AMOUNT	(0x0f)
114*4882a593Smuzhiyun #define OP_AMOUNT_0	(0x00)
115*4882a593Smuzhiyun #define OP_AMOUNT_2	(0x02)
116*4882a593Smuzhiyun #define OP_AMOUNT_4	(0x04)
117*4882a593Smuzhiyun #define OP_AMOUNT_8	(0x08)
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun #define D2		(OP_ACTION_DATA  | OP_AMOUNT_2)
120*4882a593Smuzhiyun #define D4		(OP_ACTION_DATA  | OP_AMOUNT_4)
121*4882a593Smuzhiyun #define D8		(OP_ACTION_DATA  | OP_AMOUNT_8)
122*4882a593Smuzhiyun #define I2		(OP_ACTION_INDEX | OP_AMOUNT_2)
123*4882a593Smuzhiyun #define I4		(OP_ACTION_INDEX | OP_AMOUNT_4)
124*4882a593Smuzhiyun #define I8		(OP_ACTION_INDEX | OP_AMOUNT_8)
125*4882a593Smuzhiyun #define N0		(OP_ACTION_NOOP  | OP_AMOUNT_0)
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /* the max of the regular templates - not including the special templates */
128*4882a593Smuzhiyun #define OPS_MAX		(0x1a)
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun #endif
131