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     char msg[64];                      /* Buffer for strm->msg */
97 };
98 
99 /*
100  *  Extension of inflate_state and deflate_state for DFLTCC.
101  */
102 struct dfltcc_deflate_state {
103     struct dfltcc_state common;        /* Parameter block */
104     uLong level_mask;                  /* Levels on which to use DFLTCC */
105     uLong block_size;                  /* New block each X bytes */
106     uLong block_threshold;             /* New block after total_in > X */
107     uLong dht_threshold;               /* New block only if avail_in >= X */
108 };
109 
110 #define ALIGN_UP(p, size) (__typeof__(p))(((uintptr_t)(p) + ((size) - 1)) & ~((size) - 1))
111 /* Resides right after inflate_state or deflate_state */
112 #define GET_DFLTCC_STATE(state) ((struct dfltcc_state *)((char *)(state) + ALIGN_UP(sizeof(*state), 8)))
113 
114 void dfltcc_reset_state(struct dfltcc_state *dfltcc_state);
115 
is_dfltcc_enabled(void)116 static inline int is_dfltcc_enabled(void)
117 {
118 return (zlib_dfltcc_support != ZLIB_DFLTCC_DISABLED &&
119         test_facility(DFLTCC_FACILITY));
120 }
121 
122 #define DEFLATE_DFLTCC_ENABLED() is_dfltcc_enabled()
123 
124 #endif /* DFLTCC_H */
125