1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright 2018-2019, 2021 NXP
4  *
5  * Brief   Scatter-Gather Table management utilities header.
6  */
7 #ifndef __CAAM_UTILS_SGT_H__
8 #define __CAAM_UTILS_SGT_H__
9 
10 #include <caam_types.h>
11 #include <utee_types.h>
12 
13 #define BP_SGT_V2_OFFSET	48
14 #define BS_SGT_V2_OFFSET	12
15 #define SGT_V2_OFFSET_MAX_VALUE GENMASK_64(BS_SGT_V2_OFFSET - 1, 0)
16 #define BM_SGT_V2_OFFSET \
17 	SHIFT_U64(GENMASK_64(BS_SGT_V2_OFFSET - 1, 0), BP_SGT_V2_OFFSET)
18 #define BV_SGT_V2_OFFSET(_x) SHIFT_U64(((uint64_t)_x), BP_SGT_V2_OFFSET)
19 #define SGT_V2_ENTRY_OFFSET(_x) \
20 	((((uint64_t)_x) & BM_SGT_V2_OFFSET) >> BP_SGT_V2_OFFSET)
21 
22 #define BP_SGT_V2_AVAIL_LENGTH	      0
23 #define BS_SGT_V2_AVAIL_LENGTH	      32
24 #define SGT_V2_AVAIL_LENGTH_MAX_VALUE GENMASK_64(BS_SGT_V2_AVAIL_LENGTH - 1, 0)
25 #define BM_SGT_V2_AVAIL_LENGTH \
26 	SHIFT_U64(SGT_V2_AVAIL_LENGTH_MAX_VALUE, BP_SGT_V2_AVAIL_LENGTH)
27 #define BV_SGT_V2_AVAIL_LENGTH(_x) \
28 	SHIFT_U64(((uint64_t)_x), BP_SGT_V2_AVAIL_LENGTH)
29 #define SGT_V2_ENTRY_AVAIL_LENGTH(_x) \
30 	((((uint64_t)_x) & BM_SGT_V2_AVAIL_LENGTH) >> BP_SGT_V2_AVAIL_LENGTH)
31 
32 #define BP_SGT_V2_F   63
33 #define BM_SGT_V2_F   BIT64(BP_SGT_V2_F)
34 #define BP_SGT_V2_IVP 46
35 #define BM_SGT_V2_IVP BIT64(BP_SGT_V2_IVP)
36 
37 /*
38  * Scatter/Gather Table type for input and output data
39  */
40 union caamsgt {
41 	struct {
42 		/* W0 - Address pointer (MS 8 LSBs) */
43 		uint32_t ptr_ms;
44 		/* W1 - Address pointer (LS 32 bits) */
45 		uint32_t ptr_ls;
46 		/* W2 - Length 30bits, 1bit Final, 1bit Extension */
47 		uint32_t len_f_e;
48 		/* W3- Offset in memory buffer (13 LSBs) */
49 		uint32_t offset;
50 	} v1;
51 	struct {
52 		uint64_t w1; /* Address of the data */
53 		uint64_t w2; /* Final bit, offset and length */
54 	} v2;
55 };
56 
57 /*
58  * Data buffer encoded in SGT format
59  */
60 struct caamsgtbuf {
61 	union caamsgt *sgt;  /* SGT Array */
62 	struct caambuf *buf; /* Buffer Array */
63 	unsigned int number; /* Number of SGT/Buf */
64 	size_t length;	     /* Total length of the data encoded */
65 	paddr_t paddr;	     /* Physical address to use in CAAM descriptor */
66 	bool sgt_type;	     /* Define the data format */
67 };
68 
69 /*
70  * Allocate data of type struct caamsgtbuf
71  *
72  * @data    [out] Data object allocated
73  */
74 enum caam_status caam_sgtbuf_alloc(struct caamsgtbuf *data);
75 
76 /*
77  * Free data of type struct caamsgtbuf
78  *
79  * @data    Data object to free
80  */
81 void caam_sgtbuf_free(struct caamsgtbuf *data);
82 
83 /*
84  * Cache operation on SGT table
85  *
86  * @op     Cache operation
87  * @insgt  SGT table
88  * @length Length of data to maintain
89  */
90 void caam_sgt_cache_op(enum utee_cache_operation op, struct caamsgtbuf *insgt,
91 		       size_t length);
92 
93 /*
94  * Set a Scatter Gather Table Entry
95  *
96  * @sgt      SGT entry
97  * @paddr    Data's physical address
98  * @len      Data's length
99  * @offset   Offset to start in data buffer
100  * @final_e  Final entry in the table if true
101  */
102 void caam_sgt_set_entry(union caamsgt *sgt, vaddr_t paddr, size_t len,
103 			unsigned int offset, bool final_e);
104 
105 #define CAAM_SGT_ENTRY(sgt, paddr, len) \
106 	caam_sgt_set_entry(sgt, paddr, len, 0, false)
107 #define CAAM_SGT_ENTRY_FINAL(sgt, paddr, len) \
108 	caam_sgt_set_entry(sgt, paddr, len, 0, true)
109 
110 /*
111  * Build a SGT object with @data buffer.
112  * If the @data buffer is a buffer mapped on non-contiguous physical areas,
113  * convert it in SGT entries.
114  * Fill the CAAM SGT table with the buffer list in @sgt parameter
115  *
116  * @sgt [in/out] SGT buffer list and table
117  */
118 void caam_sgt_fill_table(struct caamsgtbuf *sgt);
119 
120 /*
121  * Derive a CAAM SGT table from the @from SGT table starting @offset.
122  * Allocate the resulting SGT table derived.
123  *
124  * @sgt     [out] SGT buffer list and table
125  * @from    Input SGT table
126  * @offset  Offset to start
127  * @length  Length of the new SGT data
128  */
129 enum caam_status caam_sgt_derive(struct caamsgtbuf *sgt,
130 				 const struct caamsgtbuf *from, size_t offset,
131 				 size_t length);
132 
133 /*
134  * Print the details of an SGT entry using the trace macro
135  *
136  * @idx [in]Index of the sgt to print
137  * @sgt [in] SGT buffer list and table
138  */
139 void sgt_entry_trace(unsigned int idx, const struct caamsgtbuf *sgt);
140 
141 /*
142  * Add an @offset to the SGT entry
143  *
144  * @sgt     [in/out] Sgt entry
145  * @offset  Offset to add
146  */
147 void sgt_entry_offset(union caamsgt *sgt, unsigned int offset);
148 
149 #endif /* __CAAM_UTILS_SGT_H__ */
150