xref: /openbmc/linux/lib/zlib_dfltcc/dfltcc.h (revision 55fd7e02)
1 // SPDX-License-Identifier: Zlib
2 #ifndef DFLTCC_H
3 #define DFLTCC_H
4 
5 #include "../zlib_deflate/defutil.h"
6 #include <asm/facility.h>
7 #include <asm/setup.h>
8 
9 /*
10  * Tuning parameters.
11  */
12 #define DFLTCC_LEVEL_MASK 0x2 /* DFLTCC compression for level 1 only */
13 #define DFLTCC_LEVEL_MASK_DEBUG 0x3fe /* DFLTCC compression for all levels */
14 #define DFLTCC_BLOCK_SIZE 1048576
15 #define DFLTCC_FIRST_FHT_BLOCK_SIZE 4096
16 #define DFLTCC_DHT_MIN_SAMPLE_SIZE 4096
17 #define DFLTCC_RIBM 0
18 
19 #define DFLTCC_FACILITY 151
20 
21 /*
22  * Parameter Block for Query Available Functions.
23  */
24 struct dfltcc_qaf_param {
25     char fns[16];
26     char reserved1[8];
27     char fmts[2];
28     char reserved2[6];
29 };
30 
31 static_assert(sizeof(struct dfltcc_qaf_param) == 32);
32 
33 #define DFLTCC_FMT0 0
34 
35 /*
36  * Parameter Block for Generate Dynamic-Huffman Table, Compress and Expand.
37  */
38 struct dfltcc_param_v0 {
39     uint16_t pbvn;                     /* Parameter-Block-Version Number */
40     uint8_t mvn;                       /* Model-Version Number */
41     uint8_t ribm;                      /* Reserved for IBM use */
42     unsigned reserved32 : 31;
43     unsigned cf : 1;                   /* Continuation Flag */
44     uint8_t reserved64[8];
45     unsigned nt : 1;                   /* New Task */
46     unsigned reserved129 : 1;
47     unsigned cvt : 1;                  /* Check Value Type */
48     unsigned reserved131 : 1;
49     unsigned htt : 1;                  /* Huffman-Table Type */
50     unsigned bcf : 1;                  /* Block-Continuation Flag */
51     unsigned bcc : 1;                  /* Block Closing Control */
52     unsigned bhf : 1;                  /* Block Header Final */
53     unsigned reserved136 : 1;
54     unsigned reserved137 : 1;
55     unsigned dhtgc : 1;                /* DHT Generation Control */
56     unsigned reserved139 : 5;
57     unsigned reserved144 : 5;
58     unsigned sbb : 3;                  /* Sub-Byte Boundary */
59     uint8_t oesc;                      /* Operation-Ending-Supplemental Code */
60     unsigned reserved160 : 12;
61     unsigned ifs : 4;                  /* Incomplete-Function Status */
62     uint16_t ifl;                      /* Incomplete-Function Length */
63     uint8_t reserved192[8];
64     uint8_t reserved256[8];
65     uint8_t reserved320[4];
66     uint16_t hl;                       /* History Length */
67     unsigned reserved368 : 1;
68     uint16_t ho : 15;                  /* History Offset */
69     uint32_t cv;                       /* Check Value */
70     unsigned eobs : 15;                /* End-of-block Symbol */
71     unsigned reserved431: 1;
72     uint8_t eobl : 4;                  /* End-of-block Length */
73     unsigned reserved436 : 12;
74     unsigned reserved448 : 4;
75     uint16_t cdhtl : 12;               /* Compressed-Dynamic-Huffman Table
76                                           Length */
77     uint8_t reserved464[6];
78     uint8_t cdht[288];
79     uint8_t reserved[32];
80     uint8_t csb[1152];
81 };
82 
83 static_assert(sizeof(struct dfltcc_param_v0) == 1536);
84 
85 #define CVT_CRC32 0
86 #define CVT_ADLER32 1
87 #define HTT_FIXED 0
88 #define HTT_DYNAMIC 1
89 
90 /*
91  *  Extension of inflate_state and deflate_state for DFLTCC.
92  */
93 struct dfltcc_state {
94     struct dfltcc_param_v0 param;      /* Parameter block */
95     struct dfltcc_qaf_param af;        /* Available functions */
96     uLong level_mask;                  /* Levels on which to use DFLTCC */
97     uLong block_size;                  /* New block each X bytes */
98     uLong block_threshold;             /* New block after total_in > X */
99     uLong dht_threshold;               /* New block only if avail_in >= X */
100     char msg[64];                      /* Buffer for strm->msg */
101 };
102 
103 /* Resides right after inflate_state or deflate_state */
104 #define GET_DFLTCC_STATE(state) ((struct dfltcc_state *)((state) + 1))
105 
106 /* External functions */
107 int dfltcc_can_deflate(z_streamp strm);
108 int dfltcc_deflate(z_streamp strm,
109                    int flush,
110                    block_state *result);
111 void dfltcc_reset(z_streamp strm, uInt size);
112 int dfltcc_can_inflate(z_streamp strm);
113 typedef enum {
114     DFLTCC_INFLATE_CONTINUE,
115     DFLTCC_INFLATE_BREAK,
116     DFLTCC_INFLATE_SOFTWARE,
117 } dfltcc_inflate_action;
118 dfltcc_inflate_action dfltcc_inflate(z_streamp strm,
119                                      int flush, int *ret);
120 static inline int is_dfltcc_enabled(void)
121 {
122 return (zlib_dfltcc_support != ZLIB_DFLTCC_DISABLED &&
123         test_facility(DFLTCC_FACILITY));
124 }
125 
126 #define DEFLATE_RESET_HOOK(strm) \
127     dfltcc_reset((strm), sizeof(deflate_state))
128 
129 #define DEFLATE_HOOK dfltcc_deflate
130 
131 #define DEFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_deflate((strm)))
132 
133 #define DEFLATE_DFLTCC_ENABLED() is_dfltcc_enabled()
134 
135 #define INFLATE_RESET_HOOK(strm) \
136     dfltcc_reset((strm), sizeof(struct inflate_state))
137 
138 #define INFLATE_TYPEDO_HOOK(strm, flush) \
139     if (dfltcc_can_inflate((strm))) { \
140         dfltcc_inflate_action action; \
141 \
142         RESTORE(); \
143         action = dfltcc_inflate((strm), (flush), &ret); \
144         LOAD(); \
145         if (action == DFLTCC_INFLATE_CONTINUE) \
146             break; \
147         else if (action == DFLTCC_INFLATE_BREAK) \
148             goto inf_leave; \
149     }
150 
151 #define INFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_inflate((strm)))
152 
153 #define INFLATE_NEED_UPDATEWINDOW(strm) (!dfltcc_can_inflate((strm)))
154 
155 #endif /* DFLTCC_H */
156