f594e916868f1fc3d8b66f1e455265d6d1b32290
[dpdk.git] / drivers / crypto / ccp / ccp_dev.h
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
3  */
4
5 #ifndef _CCP_DEV_H_
6 #define _CCP_DEV_H_
7
8 #include <limits.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <string.h>
12
13 #include <rte_bus_pci.h>
14 #include <rte_atomic.h>
15 #include <rte_byteorder.h>
16 #include <rte_io.h>
17 #include <rte_pci.h>
18 #include <rte_spinlock.h>
19 #include <rte_crypto_sym.h>
20 #include <rte_cryptodev.h>
21
22 /**< CCP sspecific */
23 #define MAX_HW_QUEUES                   5
24
25 /**< CCP Register Mappings */
26 #define Q_MASK_REG                      0x000
27 #define TRNG_OUT_REG                    0x00c
28
29 /* CCP Version 5 Specifics */
30 #define CMD_QUEUE_MASK_OFFSET           0x00
31 #define CMD_QUEUE_PRIO_OFFSET           0x04
32 #define CMD_REQID_CONFIG_OFFSET         0x08
33 #define CMD_CMD_TIMEOUT_OFFSET          0x10
34 #define LSB_PUBLIC_MASK_LO_OFFSET       0x18
35 #define LSB_PUBLIC_MASK_HI_OFFSET       0x1C
36 #define LSB_PRIVATE_MASK_LO_OFFSET      0x20
37 #define LSB_PRIVATE_MASK_HI_OFFSET      0x24
38
39 #define CMD_Q_CONTROL_BASE              0x0000
40 #define CMD_Q_TAIL_LO_BASE              0x0004
41 #define CMD_Q_HEAD_LO_BASE              0x0008
42 #define CMD_Q_INT_ENABLE_BASE           0x000C
43 #define CMD_Q_INTERRUPT_STATUS_BASE     0x0010
44
45 #define CMD_Q_STATUS_BASE               0x0100
46 #define CMD_Q_INT_STATUS_BASE           0x0104
47
48 #define CMD_CONFIG_0_OFFSET             0x6000
49 #define CMD_TRNG_CTL_OFFSET             0x6008
50 #define CMD_AES_MASK_OFFSET             0x6010
51 #define CMD_CLK_GATE_CTL_OFFSET         0x603C
52
53 /* Address offset between two virtual queue registers */
54 #define CMD_Q_STATUS_INCR               0x1000
55
56 /* Bit masks */
57 #define CMD_Q_RUN                       0x1
58 #define CMD_Q_SIZE                      0x1F
59 #define CMD_Q_SHIFT                     3
60 #define COMMANDS_PER_QUEUE              2048
61
62 #define QUEUE_SIZE_VAL                  ((ffs(COMMANDS_PER_QUEUE) - 2) & \
63                                          CMD_Q_SIZE)
64 #define Q_DESC_SIZE                     sizeof(struct ccp_desc)
65 #define Q_SIZE(n)                       (COMMANDS_PER_QUEUE*(n))
66
67 #define INT_COMPLETION                  0x1
68 #define INT_ERROR                       0x2
69 #define INT_QUEUE_STOPPED               0x4
70 #define ALL_INTERRUPTS                  (INT_COMPLETION| \
71                                          INT_ERROR| \
72                                          INT_QUEUE_STOPPED)
73
74 #define LSB_REGION_WIDTH                5
75 #define MAX_LSB_CNT                     8
76
77 #define LSB_SIZE                        16
78 #define LSB_ITEM_SIZE                   32
79 #define SLSB_MAP_SIZE                   (MAX_LSB_CNT * LSB_SIZE)
80
81 /* bitmap */
82 enum {
83         BITS_PER_WORD = sizeof(unsigned long) * CHAR_BIT
84 };
85
86 #define WORD_OFFSET(b) ((b) / BITS_PER_WORD)
87 #define BIT_OFFSET(b)  ((b) % BITS_PER_WORD)
88
89 #define CCP_DIV_ROUND_UP(n, d)  (((n) + (d) - 1) / (d))
90 #define CCP_BITMAP_SIZE(nr) \
91         CCP_DIV_ROUND_UP(nr, CHAR_BIT * sizeof(unsigned long))
92
93 #define CCP_BITMAP_FIRST_WORD_MASK(start) \
94         (~0UL << ((start) & (BITS_PER_WORD - 1)))
95 #define CCP_BITMAP_LAST_WORD_MASK(nbits) \
96         (~0UL >> (-(nbits) & (BITS_PER_WORD - 1)))
97
98 #define __ccp_round_mask(x, y) ((typeof(x))((y)-1))
99 #define ccp_round_down(x, y) ((x) & ~__ccp_round_mask(x, y))
100
101 /** CCP registers Write/Read */
102
103 static inline void ccp_pci_reg_write(void *base, int offset,
104                                      uint32_t value)
105 {
106         volatile void *reg_addr = ((uint8_t *)base + offset);
107
108         rte_write32((rte_cpu_to_le_32(value)), reg_addr);
109 }
110
111 static inline uint32_t ccp_pci_reg_read(void *base, int offset)
112 {
113         volatile void *reg_addr = ((uint8_t *)base + offset);
114
115         return rte_le_to_cpu_32(rte_read32(reg_addr));
116 }
117
118 #define CCP_READ_REG(hw_addr, reg_offset) \
119         ccp_pci_reg_read(hw_addr, reg_offset)
120
121 #define CCP_WRITE_REG(hw_addr, reg_offset, value) \
122         ccp_pci_reg_write(hw_addr, reg_offset, value)
123
124 TAILQ_HEAD(ccp_list, ccp_device);
125
126 extern struct ccp_list ccp_list;
127
128 /**
129  * CCP device version
130  */
131 enum ccp_device_version {
132         CCP_VERSION_5A = 0,
133         CCP_VERSION_5B,
134 };
135
136 /**
137  * A structure describing a CCP command queue.
138  */
139 struct ccp_queue {
140         struct ccp_device *dev;
141         char memz_name[RTE_MEMZONE_NAMESIZE];
142
143         rte_atomic64_t free_slots;
144         /**< available free slots updated from enq/deq calls */
145
146         /* Queue identifier */
147         uint64_t id;    /**< queue id */
148         uint64_t qidx;  /**< queue index */
149         uint64_t qsize; /**< queue size */
150
151         /* Queue address */
152         struct ccp_desc *qbase_desc;
153         void *qbase_addr;
154         phys_addr_t qbase_phys_addr;
155         /**< queue-page registers addr */
156         void *reg_base;
157
158         uint32_t qcontrol;
159         /**< queue ctrl reg */
160
161         int lsb;
162         /**< lsb region assigned to queue */
163         unsigned long lsbmask;
164         /**< lsb regions queue can access */
165         unsigned long lsbmap[CCP_BITMAP_SIZE(LSB_SIZE)];
166         /**< all lsb resources which queue is using */
167         uint32_t sb_key;
168         /**< lsb assigned for queue */
169         uint32_t sb_iv;
170         /**< lsb assigned for iv */
171         uint32_t sb_sha;
172         /**< lsb assigned for sha ctx */
173         uint32_t sb_hmac;
174         /**< lsb assigned for hmac ctx */
175 } ____cacheline_aligned;
176
177 /**
178  * A structure describing a CCP device.
179  */
180 struct ccp_device {
181         TAILQ_ENTRY(ccp_device) next;
182         int id;
183         /**< ccp dev id on platform */
184         struct ccp_queue cmd_q[MAX_HW_QUEUES];
185         /**< ccp queue */
186         int cmd_q_count;
187         /**< no. of ccp Queues */
188         struct rte_pci_device pci;
189         /**< ccp pci identifier */
190         unsigned long lsbmap[CCP_BITMAP_SIZE(SLSB_MAP_SIZE)];
191         /**< shared lsb mask of ccp */
192         rte_spinlock_t lsb_lock;
193         /**< protection for shared lsb region allocation */
194         int qidx;
195         /**< current queue index */
196 } __rte_cache_aligned;
197
198 /**
199  * descriptor for version 5 CPP commands
200  * 8 32-bit words:
201  * word 0: function; engine; control bits
202  * word 1: length of source data
203  * word 2: low 32 bits of source pointer
204  * word 3: upper 16 bits of source pointer; source memory type
205  * word 4: low 32 bits of destination pointer
206  * word 5: upper 16 bits of destination pointer; destination memory
207  * type
208  * word 6: low 32 bits of key pointer
209  * word 7: upper 16 bits of key pointer; key memory type
210  */
211 struct dword0 {
212         uint32_t soc:1;
213         uint32_t ioc:1;
214         uint32_t rsvd1:1;
215         uint32_t init:1;
216         uint32_t eom:1;
217         uint32_t function:15;
218         uint32_t engine:4;
219         uint32_t prot:1;
220         uint32_t rsvd2:7;
221 };
222
223 struct dword3 {
224         uint32_t src_hi:16;
225         uint32_t src_mem:2;
226         uint32_t lsb_cxt_id:8;
227         uint32_t rsvd1:5;
228         uint32_t fixed:1;
229 };
230
231 union dword4 {
232         uint32_t dst_lo;        /* NON-SHA */
233         uint32_t sha_len_lo;    /* SHA */
234 };
235
236 union dword5 {
237         struct {
238                 uint32_t dst_hi:16;
239                 uint32_t dst_mem:2;
240                 uint32_t rsvd1:13;
241                 uint32_t fixed:1;
242         }
243         fields;
244         uint32_t sha_len_hi;
245 };
246
247 struct dword7 {
248         uint32_t key_hi:16;
249         uint32_t key_mem:2;
250         uint32_t rsvd1:14;
251 };
252
253 struct ccp_desc {
254         struct dword0 dw0;
255         uint32_t length;
256         uint32_t src_lo;
257         struct dword3 dw3;
258         union dword4 dw4;
259         union dword5 dw5;
260         uint32_t key_lo;
261         struct dword7 dw7;
262 };
263
264 static inline uint32_t
265 low32_value(unsigned long addr)
266 {
267         return ((uint64_t)addr) & 0x0ffffffff;
268 }
269
270 static inline uint32_t
271 high32_value(unsigned long addr)
272 {
273         return ((uint64_t)addr >> 32) & 0x00000ffff;
274 }
275
276 /**
277  * Detect ccp platform and initialize all ccp devices
278  *
279  * @param ccp_id rte_pci_id list for supported CCP devices
280  * @return no. of successfully initialized CCP devices
281  */
282 int ccp_probe_devices(const struct rte_pci_id *ccp_id);
283
284 #endif /* _CCP_DEV_H_ */