1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright 2020-2021 NXP 4 * 5 * CAAM DMA data object utilities include file. 6 */ 7 #ifndef __CAAM_UTILS_DMAOBJ_H__ 8 #define __CAAM_UTILS_DMAOBJ_H__ 9 10 #include <caam_types.h> 11 #include <caam_utils_sgt.h> 12 #include <tee_api_types.h> 13 14 /* 15 * CAAM DMA Object type 16 * @priv Private object data not used externally. 17 * @orig Original data buffer 18 * @sgtbuf CAAM SGT/Buffer object 19 */ 20 struct caamdmaobj { 21 void *priv; 22 struct caambuf orig; 23 struct caamsgtbuf sgtbuf; 24 }; 25 26 /* 27 * Initialize a CAAM DMA object of type input data. 28 * If necessary, a new CAAM Buffer will be reallocated if given @data is not 29 * accessible by the CAAM DMA and input data copied into. 30 * 31 * @obj [out] CAAM DMA object initialized 32 * @data Input data pointer 33 * @length Length in bytes of the input data 34 */ 35 TEE_Result caam_dmaobj_init_input(struct caamdmaobj *obj, const void *data, 36 size_t len); 37 /* 38 * Initialize and build the SGT/Buffer Object of a CAAM DMA object of type 39 * input data. 40 * Function call the caam_dmaobj_init_input function and if success 41 * the caam_dmaobj_sgtbuf_build function. If the full size of the input 42 * data can't be handled in the SGT/Buffer Object, returns in error. 43 * 44 * @obj [out] CAAM DMA object initialized 45 * @data Input data pointer 46 * @length Length in bytes of the input data 47 */ 48 TEE_Result caam_dmaobj_input_sgtbuf(struct caamdmaobj *obj, const void *data, 49 size_t len); 50 /* 51 * Initialize a CAAM DMA object of type output data. 52 * If necessary, a new CAAM Buffer will be reallocated if given @data is not 53 * accessible by the CAAM DMA or if the given @length is lower than 54 * @min_length requested for the CAAM operation. 55 * 56 * @obj [out] CAAM DMA object initialized 57 * @data Output data pointer 58 * @length Length in bytes of the output data 59 * @min_length Minimum length in bytes needed for the output data 60 */ 61 TEE_Result caam_dmaobj_init_output(struct caamdmaobj *obj, void *data, 62 size_t length, size_t min_length); 63 64 /* 65 * Initialize and build the SGT/Buffer Object of a CAAM DMA object of type 66 * output data. 67 * Function call the caam_dmaobj_init_output function and if success 68 * the caam_dmaobj_sgtbuf_build function. If the full size of the output 69 * data can't be handled in the SGT/Buffer Object, returns in error. 70 * 71 * Note: to allocate a output buffer, set @data = NULL and length = 0, the 72 * buffer size allocated will be the @min_length size. Caution, the field 73 * orig of the @obj is kept empty. 74 * 75 * @obj [out] CAAM DMA object initialized 76 * @data Output data pointer 77 * @length Length in bytes of the output data 78 * @min_length Minimum length in bytes needed for the output data 79 */ 80 TEE_Result caam_dmaobj_output_sgtbuf(struct caamdmaobj *obj, void *data, 81 size_t length, size_t min_length); 82 83 /* 84 * Push the data to physical memory with a cache clean or flush depending 85 * on the type of data, respectively input or output. 86 * 87 * @obj CAAM DMA object 88 */ 89 void caam_dmaobj_cache_push(struct caamdmaobj *obj); 90 91 /* 92 * Copy the CAAM DMA object buffer to the original data buffer. 93 * Return the number of bytes copied. 94 * 95 * @obj CAAM DMA object 96 */ 97 size_t caam_dmaobj_copy_to_orig(struct caamdmaobj *obj); 98 99 /* 100 * Copy the CAAM DMA object buffer to the original data buffer removing 101 * non-significant first zeros (left zeros). 102 * If all DMA object buffer is zero, left only one zero in the destination. 103 * Return the number of bytes copied. 104 * 105 * @obj CAAM DMA object 106 */ 107 size_t caam_dmaobj_copy_ltrim_to_orig(struct caamdmaobj *obj); 108 109 /* 110 * Free the CAAM DMA object. 111 * If a buffer has been reallocated, free it. 112 * Free the sgtbuf object. 113 * 114 * @obj CAAM DMA object 115 */ 116 void caam_dmaobj_free(struct caamdmaobj *obj); 117 118 /* 119 * Create a CAAM DMA object SGT type with the block buffer @block first and 120 * the CAAM DMA Object after 121 * 122 * @res CAAM DMA object resulting 123 * @block CAAM Block buffer to add first 124 * @obj CAAM DMA object to add secondly 125 */ 126 TEE_Result caam_dmaobj_add_first_block(struct caamdmaobj *obj, 127 struct caamblock *block); 128 129 /* 130 * Derive a CAAM DMA object's sgtbuf object to a new DMA object. 131 * The @from CAAM DMA object sgtbuf must have to be created first to 132 * allocate the DMA Buffers. 133 * 134 * @obj [out] CAAM DMA object derived 135 * @from Original CAAM DMA object 136 * @offset Offset to start from 137 * @length Length in bytes of the data 138 */ 139 TEE_Result caam_dmaobj_derive_sgtbuf(struct caamdmaobj *obj, 140 const struct caamdmaobj *from, 141 size_t offset, size_t length); 142 143 /* 144 * Build the CAAM DMA Object's sgtbuf input and output with the same data 145 * length. 146 * First try to build input sgtbuf of maximum @length starting at @offset. 147 * Then build output sgtbuf with same input data length built start at @offset. 148 * If output sgtbuf built data length is not the same as the input's one, 149 * rebuild the input with same output data length. 150 * 151 * If the both input and output length are not equal returns an error. 152 * 153 * @input CAAM DMA Input object 154 * @output CAAM DMA Output object 155 * @length [in/out] maximum length to do/done 156 * @off Starting offset 157 * @align Buffer allocation alignment 158 */ 159 TEE_Result caam_dmaobj_sgtbuf_inout_build(struct caamdmaobj *input, 160 struct caamdmaobj *output, 161 size_t *length, size_t off, 162 size_t align); 163 164 /* 165 * Prepare input/output CAAM DMA Object's by allocating the DMA Buffers 166 * if needed. 167 * If @input or @output is NULL, allocates DMA buffer of given object. 168 * Else if both objects are set, allocates DMA buffer of the same 169 * size for the @input and @output objects. 170 * Minimum DMA Buffer size allocated is the @min_size value. Even if this 171 * minimum size allocation failed, returns an error. 172 * 173 * @input CAAM DMA object input 174 * @output CAAM DMA object output 175 * @min_size Mimimum length to allocate 176 */ 177 TEE_Result caam_dmaobj_prepare(struct caamdmaobj *input, 178 struct caamdmaobj *output, size_t min_size); 179 180 /* 181 * Build the CAAM DMA Object's sgtbuf object. Try to build a sgtbuf of 182 * maximum @length starting at @offset. 183 * Return the @length mapped in the sgtbuf object. 184 * 185 * @obj CAAM DMA object 186 * @length [in/out] maximum length to do/done 187 * @off Starting offset 188 * @align Buffer allocation alignment 189 */ 190 TEE_Result caam_dmaobj_sgtbuf_build(struct caamdmaobj *obj, size_t *length, 191 size_t off, size_t align); 192 193 #endif /* __CAAM_UTILS_DMAOBJ_H__ */ 194