842_compress.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * 842 Software Compression
  4. *
  5. * Copyright (C) 2015 Dan Streetman, IBM Corp
  6. *
  7. * See 842.h for details of the 842 compressed format.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #define MODULE_NAME "842_compress"
  11. #include <linux/hashtable.h>
  12. #include "842.h"
  13. #include "842_debugfs.h"
  14. #define SW842_HASHTABLE8_BITS (10)
  15. #define SW842_HASHTABLE4_BITS (11)
  16. #define SW842_HASHTABLE2_BITS (10)
  17. /* By default, we allow compressing input buffers of any length, but we must
  18. * use the non-standard "short data" template so the decompressor can correctly
  19. * reproduce the uncompressed data buffer at the right length. However the
  20. * hardware 842 compressor will not recognize the "short data" template, and
  21. * will fail to decompress any compressed buffer containing it (I have no idea
  22. * why anyone would want to use software to compress and hardware to decompress
  23. * but that's beside the point). This parameter forces the compression
  24. * function to simply reject any input buffer that isn't a multiple of 8 bytes
  25. * long, instead of using the "short data" template, so that all compressed
  26. * buffers produced by this function will be decompressable by the 842 hardware
  27. * decompressor. Unless you have a specific need for that, leave this disabled
  28. * so that any length buffer can be compressed.
  29. */
  30. static bool sw842_strict;
  31. module_param_named(strict, sw842_strict, bool, 0644);
  32. static u8 comp_ops[OPS_MAX][5] = { /* params size in bits */
  33. { I8, N0, N0, N0, 0x19 }, /* 8 */
  34. { I4, I4, N0, N0, 0x18 }, /* 18 */
  35. { I4, I2, I2, N0, 0x17 }, /* 25 */
  36. { I2, I2, I4, N0, 0x13 }, /* 25 */
  37. { I2, I2, I2, I2, 0x12 }, /* 32 */
  38. { I4, I2, D2, N0, 0x16 }, /* 33 */
  39. { I4, D2, I2, N0, 0x15 }, /* 33 */
  40. { I2, D2, I4, N0, 0x0e }, /* 33 */
  41. { D2, I2, I4, N0, 0x09 }, /* 33 */
  42. { I2, I2, I2, D2, 0x11 }, /* 40 */
  43. { I2, I2, D2, I2, 0x10 }, /* 40 */
  44. { I2, D2, I2, I2, 0x0d }, /* 40 */
  45. { D2, I2, I2, I2, 0x08 }, /* 40 */
  46. { I4, D4, N0, N0, 0x14 }, /* 41 */
  47. { D4, I4, N0, N0, 0x04 }, /* 41 */
  48. { I2, I2, D4, N0, 0x0f }, /* 48 */
  49. { I2, D2, I2, D2, 0x0c }, /* 48 */
  50. { I2, D4, I2, N0, 0x0b }, /* 48 */
  51. { D2, I2, I2, D2, 0x07 }, /* 48 */
  52. { D2, I2, D2, I2, 0x06 }, /* 48 */
  53. { D4, I2, I2, N0, 0x03 }, /* 48 */
  54. { I2, D2, D4, N0, 0x0a }, /* 56 */
  55. { D2, I2, D4, N0, 0x05 }, /* 56 */
  56. { D4, I2, D2, N0, 0x02 }, /* 56 */
  57. { D4, D2, I2, N0, 0x01 }, /* 56 */
  58. { D8, N0, N0, N0, 0x00 }, /* 64 */
  59. };
  60. struct sw842_hlist_node8 {
  61. struct hlist_node node;
  62. u64 data;
  63. u8 index;
  64. };
  65. struct sw842_hlist_node4 {
  66. struct hlist_node node;
  67. u32 data;
  68. u16 index;
  69. };
  70. struct sw842_hlist_node2 {
  71. struct hlist_node node;
  72. u16 data;
  73. u8 index;
  74. };
  75. #define INDEX_NOT_FOUND (-1)
  76. #define INDEX_NOT_CHECKED (-2)
  77. struct sw842_param {
  78. u8 *in;
  79. u8 *instart;
  80. u64 ilen;
  81. u8 *out;
  82. u64 olen;
  83. u8 bit;
  84. u64 data8[1];
  85. u32 data4[2];
  86. u16 data2[4];
  87. int index8[1];
  88. int index4[2];
  89. int index2[4];
  90. DECLARE_HASHTABLE(htable8, SW842_HASHTABLE8_BITS);
  91. DECLARE_HASHTABLE(htable4, SW842_HASHTABLE4_BITS);
  92. DECLARE_HASHTABLE(htable2, SW842_HASHTABLE2_BITS);
  93. struct sw842_hlist_node8 node8[1 << I8_BITS];
  94. struct sw842_hlist_node4 node4[1 << I4_BITS];
  95. struct sw842_hlist_node2 node2[1 << I2_BITS];
  96. };
  97. #define get_input_data(p, o, b) \
  98. be##b##_to_cpu(get_unaligned((__be##b *)((p)->in + (o))))
  99. #define init_hashtable_nodes(p, b) do { \
  100. int _i; \
  101. hash_init((p)->htable##b); \
  102. for (_i = 0; _i < ARRAY_SIZE((p)->node##b); _i++) { \
  103. (p)->node##b[_i].index = _i; \
  104. (p)->node##b[_i].data = 0; \
  105. INIT_HLIST_NODE(&(p)->node##b[_i].node); \
  106. } \
  107. } while (0)
  108. #define find_index(p, b, n) ({ \
  109. struct sw842_hlist_node##b *_n; \
  110. p->index##b[n] = INDEX_NOT_FOUND; \
  111. hash_for_each_possible(p->htable##b, _n, node, p->data##b[n]) { \
  112. if (p->data##b[n] == _n->data) { \
  113. p->index##b[n] = _n->index; \
  114. break; \
  115. } \
  116. } \
  117. p->index##b[n] >= 0; \
  118. })
  119. #define check_index(p, b, n) \
  120. ((p)->index##b[n] == INDEX_NOT_CHECKED \
  121. ? find_index(p, b, n) \
  122. : (p)->index##b[n] >= 0)
  123. #define replace_hash(p, b, i, d) do { \
  124. struct sw842_hlist_node##b *_n = &(p)->node##b[(i)+(d)]; \
  125. hash_del(&_n->node); \
  126. _n->data = (p)->data##b[d]; \
  127. pr_debug("add hash index%x %x pos %x data %lx\n", b, \
  128. (unsigned int)_n->index, \
  129. (unsigned int)((p)->in - (p)->instart), \
  130. (unsigned long)_n->data); \
  131. hash_add((p)->htable##b, &_n->node, _n->data); \
  132. } while (0)
  133. static u8 bmask[8] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
  134. static int add_bits(struct sw842_param *p, u64 d, u8 n);
  135. static int __split_add_bits(struct sw842_param *p, u64 d, u8 n, u8 s)
  136. {
  137. int ret;
  138. if (n <= s)
  139. return -EINVAL;
  140. ret = add_bits(p, d >> s, n - s);
  141. if (ret)
  142. return ret;
  143. return add_bits(p, d & GENMASK_ULL(s - 1, 0), s);
  144. }
  145. static int add_bits(struct sw842_param *p, u64 d, u8 n)
  146. {
  147. int b = p->bit, bits = b + n, s = round_up(bits, 8) - bits;
  148. u64 o;
  149. u8 *out = p->out;
  150. pr_debug("add %u bits %lx\n", (unsigned char)n, (unsigned long)d);
  151. if (n > 64)
  152. return -EINVAL;
  153. /* split this up if writing to > 8 bytes (i.e. n == 64 && p->bit > 0),
  154. * or if we're at the end of the output buffer and would write past end
  155. */
  156. if (bits > 64)
  157. return __split_add_bits(p, d, n, 32);
  158. else if (p->olen < 8 && bits > 32 && bits <= 56)
  159. return __split_add_bits(p, d, n, 16);
  160. else if (p->olen < 4 && bits > 16 && bits <= 24)
  161. return __split_add_bits(p, d, n, 8);
  162. if (DIV_ROUND_UP(bits, 8) > p->olen)
  163. return -ENOSPC;
  164. o = *out & bmask[b];
  165. d <<= s;
  166. if (bits <= 8)
  167. *out = o | d;
  168. else if (bits <= 16)
  169. put_unaligned(cpu_to_be16(o << 8 | d), (__be16 *)out);
  170. else if (bits <= 24)
  171. put_unaligned(cpu_to_be32(o << 24 | d << 8), (__be32 *)out);
  172. else if (bits <= 32)
  173. put_unaligned(cpu_to_be32(o << 24 | d), (__be32 *)out);
  174. else if (bits <= 40)
  175. put_unaligned(cpu_to_be64(o << 56 | d << 24), (__be64 *)out);
  176. else if (bits <= 48)
  177. put_unaligned(cpu_to_be64(o << 56 | d << 16), (__be64 *)out);
  178. else if (bits <= 56)
  179. put_unaligned(cpu_to_be64(o << 56 | d << 8), (__be64 *)out);
  180. else
  181. put_unaligned(cpu_to_be64(o << 56 | d), (__be64 *)out);
  182. p->bit += n;
  183. if (p->bit > 7) {
  184. p->out += p->bit / 8;
  185. p->olen -= p->bit / 8;
  186. p->bit %= 8;
  187. }
  188. return 0;
  189. }
  190. static int add_template(struct sw842_param *p, u8 c)
  191. {
  192. int ret, i, b = 0;
  193. u8 *t = comp_ops[c];
  194. bool inv = false;
  195. if (c >= OPS_MAX)
  196. return -EINVAL;
  197. pr_debug("template %x\n", t[4]);
  198. ret = add_bits(p, t[4], OP_BITS);
  199. if (ret)
  200. return ret;
  201. for (i = 0; i < 4; i++) {
  202. pr_debug("op %x\n", t[i]);
  203. switch (t[i] & OP_AMOUNT) {
  204. case OP_AMOUNT_8:
  205. if (b)
  206. inv = true;
  207. else if (t[i] & OP_ACTION_INDEX)
  208. ret = add_bits(p, p->index8[0], I8_BITS);
  209. else if (t[i] & OP_ACTION_DATA)
  210. ret = add_bits(p, p->data8[0], 64);
  211. else
  212. inv = true;
  213. break;
  214. case OP_AMOUNT_4:
  215. if (b == 2 && t[i] & OP_ACTION_DATA)
  216. ret = add_bits(p, get_input_data(p, 2, 32), 32);
  217. else if (b != 0 && b != 4)
  218. inv = true;
  219. else if (t[i] & OP_ACTION_INDEX)
  220. ret = add_bits(p, p->index4[b >> 2], I4_BITS);
  221. else if (t[i] & OP_ACTION_DATA)
  222. ret = add_bits(p, p->data4[b >> 2], 32);
  223. else
  224. inv = true;
  225. break;
  226. case OP_AMOUNT_2:
  227. if (b != 0 && b != 2 && b != 4 && b != 6)
  228. inv = true;
  229. if (t[i] & OP_ACTION_INDEX)
  230. ret = add_bits(p, p->index2[b >> 1], I2_BITS);
  231. else if (t[i] & OP_ACTION_DATA)
  232. ret = add_bits(p, p->data2[b >> 1], 16);
  233. else
  234. inv = true;
  235. break;
  236. case OP_AMOUNT_0:
  237. inv = (b != 8) || !(t[i] & OP_ACTION_NOOP);
  238. break;
  239. default:
  240. inv = true;
  241. break;
  242. }
  243. if (ret)
  244. return ret;
  245. if (inv) {
  246. pr_err("Invalid templ %x op %d : %x %x %x %x\n",
  247. c, i, t[0], t[1], t[2], t[3]);
  248. return -EINVAL;
  249. }
  250. b += t[i] & OP_AMOUNT;
  251. }
  252. if (b != 8) {
  253. pr_err("Invalid template %x len %x : %x %x %x %x\n",
  254. c, b, t[0], t[1], t[2], t[3]);
  255. return -EINVAL;
  256. }
  257. if (sw842_template_counts)
  258. atomic_inc(&template_count[t[4]]);
  259. return 0;
  260. }
  261. static int add_repeat_template(struct sw842_param *p, u8 r)
  262. {
  263. int ret;
  264. /* repeat param is 0-based */
  265. if (!r || --r > REPEAT_BITS_MAX)
  266. return -EINVAL;
  267. ret = add_bits(p, OP_REPEAT, OP_BITS);
  268. if (ret)
  269. return ret;
  270. ret = add_bits(p, r, REPEAT_BITS);
  271. if (ret)
  272. return ret;
  273. if (sw842_template_counts)
  274. atomic_inc(&template_repeat_count);
  275. return 0;
  276. }
  277. static int add_short_data_template(struct sw842_param *p, u8 b)
  278. {
  279. int ret, i;
  280. if (!b || b > SHORT_DATA_BITS_MAX)
  281. return -EINVAL;
  282. ret = add_bits(p, OP_SHORT_DATA, OP_BITS);
  283. if (ret)
  284. return ret;
  285. ret = add_bits(p, b, SHORT_DATA_BITS);
  286. if (ret)
  287. return ret;
  288. for (i = 0; i < b; i++) {
  289. ret = add_bits(p, p->in[i], 8);
  290. if (ret)
  291. return ret;
  292. }
  293. if (sw842_template_counts)
  294. atomic_inc(&template_short_data_count);
  295. return 0;
  296. }
  297. static int add_zeros_template(struct sw842_param *p)
  298. {
  299. int ret = add_bits(p, OP_ZEROS, OP_BITS);
  300. if (ret)
  301. return ret;
  302. if (sw842_template_counts)
  303. atomic_inc(&template_zeros_count);
  304. return 0;
  305. }
  306. static int add_end_template(struct sw842_param *p)
  307. {
  308. int ret = add_bits(p, OP_END, OP_BITS);
  309. if (ret)
  310. return ret;
  311. if (sw842_template_counts)
  312. atomic_inc(&template_end_count);
  313. return 0;
  314. }
  315. static bool check_template(struct sw842_param *p, u8 c)
  316. {
  317. u8 *t = comp_ops[c];
  318. int i, match, b = 0;
  319. if (c >= OPS_MAX)
  320. return false;
  321. for (i = 0; i < 4; i++) {
  322. if (t[i] & OP_ACTION_INDEX) {
  323. if (t[i] & OP_AMOUNT_2)
  324. match = check_index(p, 2, b >> 1);
  325. else if (t[i] & OP_AMOUNT_4)
  326. match = check_index(p, 4, b >> 2);
  327. else if (t[i] & OP_AMOUNT_8)
  328. match = check_index(p, 8, 0);
  329. else
  330. return false;
  331. if (!match)
  332. return false;
  333. }
  334. b += t[i] & OP_AMOUNT;
  335. }
  336. return true;
  337. }
  338. static void get_next_data(struct sw842_param *p)
  339. {
  340. p->data8[0] = get_input_data(p, 0, 64);
  341. p->data4[0] = get_input_data(p, 0, 32);
  342. p->data4[1] = get_input_data(p, 4, 32);
  343. p->data2[0] = get_input_data(p, 0, 16);
  344. p->data2[1] = get_input_data(p, 2, 16);
  345. p->data2[2] = get_input_data(p, 4, 16);
  346. p->data2[3] = get_input_data(p, 6, 16);
  347. }
  348. /* update the hashtable entries.
  349. * only call this after finding/adding the current template
  350. * the dataN fields for the current 8 byte block must be already updated
  351. */
  352. static void update_hashtables(struct sw842_param *p)
  353. {
  354. u64 pos = p->in - p->instart;
  355. u64 n8 = (pos >> 3) % (1 << I8_BITS);
  356. u64 n4 = (pos >> 2) % (1 << I4_BITS);
  357. u64 n2 = (pos >> 1) % (1 << I2_BITS);
  358. replace_hash(p, 8, n8, 0);
  359. replace_hash(p, 4, n4, 0);
  360. replace_hash(p, 4, n4, 1);
  361. replace_hash(p, 2, n2, 0);
  362. replace_hash(p, 2, n2, 1);
  363. replace_hash(p, 2, n2, 2);
  364. replace_hash(p, 2, n2, 3);
  365. }
  366. /* find the next template to use, and add it
  367. * the p->dataN fields must already be set for the current 8 byte block
  368. */
  369. static int process_next(struct sw842_param *p)
  370. {
  371. int ret, i;
  372. p->index8[0] = INDEX_NOT_CHECKED;
  373. p->index4[0] = INDEX_NOT_CHECKED;
  374. p->index4[1] = INDEX_NOT_CHECKED;
  375. p->index2[0] = INDEX_NOT_CHECKED;
  376. p->index2[1] = INDEX_NOT_CHECKED;
  377. p->index2[2] = INDEX_NOT_CHECKED;
  378. p->index2[3] = INDEX_NOT_CHECKED;
  379. /* check up to OPS_MAX - 1; last op is our fallback */
  380. for (i = 0; i < OPS_MAX - 1; i++) {
  381. if (check_template(p, i))
  382. break;
  383. }
  384. ret = add_template(p, i);
  385. if (ret)
  386. return ret;
  387. return 0;
  388. }
  389. /**
  390. * sw842_compress
  391. *
  392. * Compress the uncompressed buffer of length @ilen at @in to the output buffer
  393. * @out, using no more than @olen bytes, using the 842 compression format.
  394. *
  395. * Returns: 0 on success, error on failure. The @olen parameter
  396. * will contain the number of output bytes written on success, or
  397. * 0 on error.
  398. */
  399. int sw842_compress(const u8 *in, unsigned int ilen,
  400. u8 *out, unsigned int *olen, void *wmem)
  401. {
  402. struct sw842_param *p = (struct sw842_param *)wmem;
  403. int ret;
  404. u64 last, next, pad, total;
  405. u8 repeat_count = 0;
  406. u32 crc;
  407. BUILD_BUG_ON(sizeof(*p) > SW842_MEM_COMPRESS);
  408. init_hashtable_nodes(p, 8);
  409. init_hashtable_nodes(p, 4);
  410. init_hashtable_nodes(p, 2);
  411. p->in = (u8 *)in;
  412. p->instart = p->in;
  413. p->ilen = ilen;
  414. p->out = out;
  415. p->olen = *olen;
  416. p->bit = 0;
  417. total = p->olen;
  418. *olen = 0;
  419. /* if using strict mode, we can only compress a multiple of 8 */
  420. if (sw842_strict && (ilen % 8)) {
  421. pr_err("Using strict mode, can't compress len %d\n", ilen);
  422. return -EINVAL;
  423. }
  424. /* let's compress at least 8 bytes, mkay? */
  425. if (unlikely(ilen < 8))
  426. goto skip_comp;
  427. /* make initial 'last' different so we don't match the first time */
  428. last = ~get_unaligned((u64 *)p->in);
  429. while (p->ilen > 7) {
  430. next = get_unaligned((u64 *)p->in);
  431. /* must get the next data, as we need to update the hashtable
  432. * entries with the new data every time
  433. */
  434. get_next_data(p);
  435. /* we don't care about endianness in last or next;
  436. * we're just comparing 8 bytes to another 8 bytes,
  437. * they're both the same endianness
  438. */
  439. if (next == last) {
  440. /* repeat count bits are 0-based, so we stop at +1 */
  441. if (++repeat_count <= REPEAT_BITS_MAX)
  442. goto repeat;
  443. }
  444. if (repeat_count) {
  445. ret = add_repeat_template(p, repeat_count);
  446. repeat_count = 0;
  447. if (next == last) /* reached max repeat bits */
  448. goto repeat;
  449. }
  450. if (next == 0)
  451. ret = add_zeros_template(p);
  452. else
  453. ret = process_next(p);
  454. if (ret)
  455. return ret;
  456. repeat:
  457. last = next;
  458. update_hashtables(p);
  459. p->in += 8;
  460. p->ilen -= 8;
  461. }
  462. if (repeat_count) {
  463. ret = add_repeat_template(p, repeat_count);
  464. if (ret)
  465. return ret;
  466. }
  467. skip_comp:
  468. if (p->ilen > 0) {
  469. ret = add_short_data_template(p, p->ilen);
  470. if (ret)
  471. return ret;
  472. p->in += p->ilen;
  473. p->ilen = 0;
  474. }
  475. ret = add_end_template(p);
  476. if (ret)
  477. return ret;
  478. /*
  479. * crc(0:31) is appended to target data starting with the next
  480. * bit after End of stream template.
  481. * nx842 calculates CRC for data in big-endian format. So doing
  482. * same here so that sw842 decompression can be used for both
  483. * compressed data.
  484. */
  485. crc = crc32_be(0, in, ilen);
  486. ret = add_bits(p, crc, CRC_BITS);
  487. if (ret)
  488. return ret;
  489. if (p->bit) {
  490. p->out++;
  491. p->olen--;
  492. p->bit = 0;
  493. }
  494. /* pad compressed length to multiple of 8 */
  495. pad = (8 - ((total - p->olen) % 8)) % 8;
  496. if (pad) {
  497. if (pad > p->olen) /* we were so close! */
  498. return -ENOSPC;
  499. memset(p->out, 0, pad);
  500. p->out += pad;
  501. p->olen -= pad;
  502. }
  503. if (unlikely((total - p->olen) > UINT_MAX))
  504. return -ENOSPC;
  505. *olen = total - p->olen;
  506. return 0;
  507. }
  508. EXPORT_SYMBOL_GPL(sw842_compress);
  509. static int __init sw842_init(void)
  510. {
  511. if (sw842_template_counts)
  512. sw842_debugfs_create();
  513. return 0;
  514. }
  515. module_init(sw842_init);
  516. static void __exit sw842_exit(void)
  517. {
  518. if (sw842_template_counts)
  519. sw842_debugfs_remove();
  520. }
  521. module_exit(sw842_exit);
  522. MODULE_LICENSE("GPL");
  523. MODULE_DESCRIPTION("Software 842 Compressor");
  524. MODULE_AUTHOR("Dan Streetman <[email protected]>");