178ee8d1cSJulian Grajkowski /* SPDX-License-Identifier: BSD-3-Clause */
278ee8d1cSJulian Grajkowski /* Copyright(c) 2007-2022 Intel Corporation */
378ee8d1cSJulian Grajkowski /**
478ee8d1cSJulian Grajkowski *****************************************************************************
578ee8d1cSJulian Grajkowski * @file dc_buffers.c
678ee8d1cSJulian Grajkowski *
778ee8d1cSJulian Grajkowski * @defgroup Dc_DataCompression DC Data Compression
878ee8d1cSJulian Grajkowski *
978ee8d1cSJulian Grajkowski * @ingroup Dc_DataCompression
1078ee8d1cSJulian Grajkowski *
1178ee8d1cSJulian Grajkowski * @description
1278ee8d1cSJulian Grajkowski * Implementation of the buffer management operations for
1378ee8d1cSJulian Grajkowski * Data Compression service.
1478ee8d1cSJulian Grajkowski *
1578ee8d1cSJulian Grajkowski *****************************************************************************/
1678ee8d1cSJulian Grajkowski
1778ee8d1cSJulian Grajkowski /*
1878ee8d1cSJulian Grajkowski *******************************************************************************
1978ee8d1cSJulian Grajkowski * Include public/global header files
2078ee8d1cSJulian Grajkowski *******************************************************************************
2178ee8d1cSJulian Grajkowski */
2278ee8d1cSJulian Grajkowski #include "cpa.h"
2378ee8d1cSJulian Grajkowski #include "cpa_dc.h"
2478ee8d1cSJulian Grajkowski #include "cpa_dc_bp.h"
2578ee8d1cSJulian Grajkowski
2678ee8d1cSJulian Grajkowski #include "sal_types_compression.h"
2778ee8d1cSJulian Grajkowski #include "icp_qat_fw_comp.h"
28a977168cSMichal Gulbicki #include "sal_hw_gen.h"
2978ee8d1cSJulian Grajkowski
3078ee8d1cSJulian Grajkowski #define CPA_DC_CEIL_DIV(x, y) (((x) + (y)-1) / (y))
3178ee8d1cSJulian Grajkowski #define DC_DEST_BUFF_EXTRA_DEFLATE_GEN2 (55)
32a977168cSMichal Gulbicki #define DC_DEST_BUFF_EXTRA_DEFLATE_GEN4_STATIC (1029)
33a977168cSMichal Gulbicki #define DC_DEST_BUFF_EXTRA_DEFLATE_GEN4_DYN (512)
34a977168cSMichal Gulbicki #define DC_DEST_BUFF_MIN_EXTRA_BYTES(x) ((x < 8) ? (8 - x) : 0)
35a977168cSMichal Gulbicki #define DC_BUF_MAX_SIZE (0xFFFFFFFF)
3678ee8d1cSJulian Grajkowski
3778ee8d1cSJulian Grajkowski CpaStatus
cpaDcBufferListGetMetaSize(const CpaInstanceHandle instanceHandle,Cpa32U numBuffers,Cpa32U * pSizeInBytes)3878ee8d1cSJulian Grajkowski cpaDcBufferListGetMetaSize(const CpaInstanceHandle instanceHandle,
3978ee8d1cSJulian Grajkowski Cpa32U numBuffers,
4078ee8d1cSJulian Grajkowski Cpa32U *pSizeInBytes)
4178ee8d1cSJulian Grajkowski {
4278ee8d1cSJulian Grajkowski CpaInstanceHandle insHandle = NULL;
4378ee8d1cSJulian Grajkowski
4478ee8d1cSJulian Grajkowski if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle) {
4578ee8d1cSJulian Grajkowski insHandle = dcGetFirstHandle();
4678ee8d1cSJulian Grajkowski } else {
4778ee8d1cSJulian Grajkowski insHandle = instanceHandle;
4878ee8d1cSJulian Grajkowski }
4978ee8d1cSJulian Grajkowski
5078ee8d1cSJulian Grajkowski LAC_CHECK_INSTANCE_HANDLE(insHandle);
5178ee8d1cSJulian Grajkowski LAC_CHECK_NULL_PARAM(pSizeInBytes);
5278ee8d1cSJulian Grajkowski
5378ee8d1cSJulian Grajkowski /* Ensure this is a compression instance */
5478ee8d1cSJulian Grajkowski SAL_CHECK_INSTANCE_TYPE(insHandle, SAL_SERVICE_TYPE_COMPRESSION);
5578ee8d1cSJulian Grajkowski
5678ee8d1cSJulian Grajkowski if (0 == numBuffers) {
5778ee8d1cSJulian Grajkowski QAT_UTILS_LOG("Number of buffers is 0.\n");
5878ee8d1cSJulian Grajkowski return CPA_STATUS_INVALID_PARAM;
5978ee8d1cSJulian Grajkowski }
6078ee8d1cSJulian Grajkowski
6178ee8d1cSJulian Grajkowski *pSizeInBytes = (sizeof(icp_buffer_list_desc_t) +
6278ee8d1cSJulian Grajkowski (sizeof(icp_flat_buffer_desc_t) * (numBuffers + 1)) +
6378ee8d1cSJulian Grajkowski ICP_DESCRIPTOR_ALIGNMENT_BYTES);
6478ee8d1cSJulian Grajkowski
6578ee8d1cSJulian Grajkowski return CPA_STATUS_SUCCESS;
6678ee8d1cSJulian Grajkowski }
6778ee8d1cSJulian Grajkowski
6878ee8d1cSJulian Grajkowski CpaStatus
cpaDcBnpBufferListGetMetaSize(const CpaInstanceHandle instanceHandle,Cpa32U numJobs,Cpa32U * pSizeInBytes)6978ee8d1cSJulian Grajkowski cpaDcBnpBufferListGetMetaSize(const CpaInstanceHandle instanceHandle,
7078ee8d1cSJulian Grajkowski Cpa32U numJobs,
7178ee8d1cSJulian Grajkowski Cpa32U *pSizeInBytes)
7278ee8d1cSJulian Grajkowski {
7378ee8d1cSJulian Grajkowski return CPA_STATUS_UNSUPPORTED;
7478ee8d1cSJulian Grajkowski }
7578ee8d1cSJulian Grajkowski
7678ee8d1cSJulian Grajkowski static inline CpaStatus
dcDeflateBoundGen2(CpaDcHuffType huffType,Cpa32U inputSize,Cpa32U * outputSize)7778ee8d1cSJulian Grajkowski dcDeflateBoundGen2(CpaDcHuffType huffType, Cpa32U inputSize, Cpa32U *outputSize)
7878ee8d1cSJulian Grajkowski {
79a977168cSMichal Gulbicki Cpa64U inBufferSize = inputSize;
80a977168cSMichal Gulbicki Cpa64U outBufferSize = 0;
81a977168cSMichal Gulbicki
8278ee8d1cSJulian Grajkowski /* Formula for GEN2 deflate:
8378ee8d1cSJulian Grajkowski * ceil(9 * Total input bytes / 8) + 55 bytes.
8478ee8d1cSJulian Grajkowski * 55 bytes is the skid pad value for GEN2 devices.
85a977168cSMichal Gulbicki * Adding extra bytes = `DC_DEST_BUFF_MIN_EXTRA_BYTES(inputSize)`
86a977168cSMichal Gulbicki * when calculated value from `CPA_DC_CEIL_DIV(9 * inputSize, 8) +
87a977168cSMichal Gulbicki * DC_DEST_BUFF_EXTRA_DEFLATE_GEN2` is less than 64 bytes to
88a977168cSMichal Gulbicki * achieve a safer output buffer size of 64 bytes.
8978ee8d1cSJulian Grajkowski */
90a977168cSMichal Gulbicki outBufferSize = CPA_DC_CEIL_DIV(9 * inBufferSize, 8) +
91a977168cSMichal Gulbicki DC_DEST_BUFF_EXTRA_DEFLATE_GEN2 +
92a977168cSMichal Gulbicki DC_DEST_BUFF_MIN_EXTRA_BYTES(inputSize);
9378ee8d1cSJulian Grajkowski
94a977168cSMichal Gulbicki if (outBufferSize > DC_BUF_MAX_SIZE)
95a977168cSMichal Gulbicki *outputSize = DC_BUF_MAX_SIZE;
96a977168cSMichal Gulbicki else
97a977168cSMichal Gulbicki *outputSize = (Cpa32U)outBufferSize;
98a977168cSMichal Gulbicki
99a977168cSMichal Gulbicki return CPA_STATUS_SUCCESS;
100a977168cSMichal Gulbicki }
101a977168cSMichal Gulbicki
102a977168cSMichal Gulbicki static inline CpaStatus
dcDeflateBoundGen4(CpaDcHuffType huffType,Cpa32U inputSize,Cpa32U * outputSize)103a977168cSMichal Gulbicki dcDeflateBoundGen4(CpaDcHuffType huffType, Cpa32U inputSize, Cpa32U *outputSize)
104a977168cSMichal Gulbicki {
105a977168cSMichal Gulbicki Cpa64U outputSizeLong;
106a977168cSMichal Gulbicki Cpa64U inputSizeLong = (Cpa64U)inputSize;
107a977168cSMichal Gulbicki
108a977168cSMichal Gulbicki switch (huffType) {
109a977168cSMichal Gulbicki case CPA_DC_HT_STATIC:
110a977168cSMichal Gulbicki /* Formula for GEN4 static deflate:
111a977168cSMichal Gulbicki * ceil((9*sourceLen)/8) + 5 + 1024. */
112a977168cSMichal Gulbicki outputSizeLong = CPA_DC_CEIL_DIV(9 * inputSizeLong, 8) +
113a977168cSMichal Gulbicki DC_DEST_BUFF_EXTRA_DEFLATE_GEN4_STATIC;
114a977168cSMichal Gulbicki break;
115a977168cSMichal Gulbicki case CPA_DC_HT_FULL_DYNAMIC:
116a977168cSMichal Gulbicki /* Formula for GEN4 dynamic deflate:
117a977168cSMichal Gulbicki * Ceil ((9*sourceLen)/8)▒| +
118a977168cSMichal Gulbicki * ((((8/7) * sourceLen)/ 16KB) * (150+5)) + 512
119a977168cSMichal Gulbicki */
120a977168cSMichal Gulbicki outputSizeLong = DC_DEST_BUFF_EXTRA_DEFLATE_GEN4_DYN;
121a977168cSMichal Gulbicki outputSizeLong += CPA_DC_CEIL_DIV(9 * inputSizeLong, 8);
122a977168cSMichal Gulbicki outputSizeLong += ((8 * inputSizeLong * 155) / 7) / (16 * 1024);
123a977168cSMichal Gulbicki break;
124a977168cSMichal Gulbicki default:
125a977168cSMichal Gulbicki return CPA_STATUS_INVALID_PARAM;
126a977168cSMichal Gulbicki }
127a977168cSMichal Gulbicki
128a977168cSMichal Gulbicki /* Avoid output size overflow */
129a977168cSMichal Gulbicki if (outputSizeLong & 0xffffffff00000000UL)
130a977168cSMichal Gulbicki return CPA_STATUS_INVALID_PARAM;
131a977168cSMichal Gulbicki
132a977168cSMichal Gulbicki *outputSize = (Cpa32U)outputSizeLong;
13378ee8d1cSJulian Grajkowski return CPA_STATUS_SUCCESS;
13478ee8d1cSJulian Grajkowski }
13578ee8d1cSJulian Grajkowski
13678ee8d1cSJulian Grajkowski CpaStatus
cpaDcDeflateCompressBound(const CpaInstanceHandle dcInstance,CpaDcHuffType huffType,Cpa32U inputSize,Cpa32U * outputSize)13778ee8d1cSJulian Grajkowski cpaDcDeflateCompressBound(const CpaInstanceHandle dcInstance,
13878ee8d1cSJulian Grajkowski CpaDcHuffType huffType,
13978ee8d1cSJulian Grajkowski Cpa32U inputSize,
14078ee8d1cSJulian Grajkowski Cpa32U *outputSize)
14178ee8d1cSJulian Grajkowski {
142a977168cSMichal Gulbicki sal_compression_service_t *pService = NULL;
14378ee8d1cSJulian Grajkowski CpaInstanceHandle insHandle = NULL;
14478ee8d1cSJulian Grajkowski
14578ee8d1cSJulian Grajkowski if (CPA_INSTANCE_HANDLE_SINGLE == dcInstance) {
14678ee8d1cSJulian Grajkowski insHandle = dcGetFirstHandle();
14778ee8d1cSJulian Grajkowski } else {
14878ee8d1cSJulian Grajkowski insHandle = dcInstance;
14978ee8d1cSJulian Grajkowski }
15078ee8d1cSJulian Grajkowski
15178ee8d1cSJulian Grajkowski LAC_CHECK_INSTANCE_HANDLE(insHandle);
15278ee8d1cSJulian Grajkowski LAC_CHECK_NULL_PARAM(outputSize);
15378ee8d1cSJulian Grajkowski /* Ensure this is a compression instance */
15478ee8d1cSJulian Grajkowski SAL_CHECK_INSTANCE_TYPE(insHandle, SAL_SERVICE_TYPE_COMPRESSION);
15578ee8d1cSJulian Grajkowski if (!inputSize) {
15678ee8d1cSJulian Grajkowski QAT_UTILS_LOG(
15778ee8d1cSJulian Grajkowski "The input size needs to be greater than zero.\n");
15878ee8d1cSJulian Grajkowski return CPA_STATUS_INVALID_PARAM;
15978ee8d1cSJulian Grajkowski }
16078ee8d1cSJulian Grajkowski
16178ee8d1cSJulian Grajkowski if ((CPA_DC_HT_STATIC != huffType) &&
16278ee8d1cSJulian Grajkowski (CPA_DC_HT_FULL_DYNAMIC != huffType)) {
16378ee8d1cSJulian Grajkowski QAT_UTILS_LOG("Invalid huffType value.\n");
16478ee8d1cSJulian Grajkowski return CPA_STATUS_INVALID_PARAM;
16578ee8d1cSJulian Grajkowski }
16678ee8d1cSJulian Grajkowski
167a977168cSMichal Gulbicki pService = (sal_compression_service_t *)insHandle;
168a977168cSMichal Gulbicki if (isDcGen4x(pService)) {
169a977168cSMichal Gulbicki return dcDeflateBoundGen4(huffType, inputSize, outputSize);
170a977168cSMichal Gulbicki } else {
17178ee8d1cSJulian Grajkowski return dcDeflateBoundGen2(huffType, inputSize, outputSize);
17278ee8d1cSJulian Grajkowski }
173a977168cSMichal Gulbicki }
174*266b0663SKrzysztof Zdziarski
175*266b0663SKrzysztof Zdziarski CpaStatus
cpaDcLZ4CompressBound(const CpaInstanceHandle dcInstance,Cpa32U inputSize,Cpa32U * outputSize)176*266b0663SKrzysztof Zdziarski cpaDcLZ4CompressBound(const CpaInstanceHandle dcInstance,
177*266b0663SKrzysztof Zdziarski Cpa32U inputSize,
178*266b0663SKrzysztof Zdziarski Cpa32U *outputSize)
179*266b0663SKrzysztof Zdziarski {
180*266b0663SKrzysztof Zdziarski return CPA_STATUS_UNSUPPORTED;
181*266b0663SKrzysztof Zdziarski }
182*266b0663SKrzysztof Zdziarski
183*266b0663SKrzysztof Zdziarski CpaStatus
cpaDcLZ4SCompressBound(const CpaInstanceHandle dcInstance,Cpa32U inputSize,Cpa32U * outputSize)184*266b0663SKrzysztof Zdziarski cpaDcLZ4SCompressBound(const CpaInstanceHandle dcInstance,
185*266b0663SKrzysztof Zdziarski Cpa32U inputSize,
186*266b0663SKrzysztof Zdziarski Cpa32U *outputSize)
187*266b0663SKrzysztof Zdziarski {
188*266b0663SKrzysztof Zdziarski return CPA_STATUS_UNSUPPORTED;
189*266b0663SKrzysztof Zdziarski }
190