842.h 5.8 KB

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