1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
13 #include <rte_bus_pci.h>
14 #include <rte_atomic.h>
15 #include <rte_byteorder.h>
18 #include <rte_spinlock.h>
19 #include <rte_crypto_sym.h>
20 #include <rte_cryptodev.h>
23 #define MAX_HW_QUEUES 5
24 #define CCP_MAX_TRNG_RETRIES 10
25 #define CCP_ALIGN(x, y) ((((x) + (y - 1)) / y) * y)
27 /**< CCP Register Mappings */
28 #define Q_MASK_REG 0x000
29 #define TRNG_OUT_REG 0x00c
31 /* CCP Version 5 Specifics */
32 #define CMD_QUEUE_MASK_OFFSET 0x00
33 #define CMD_QUEUE_PRIO_OFFSET 0x04
34 #define CMD_REQID_CONFIG_OFFSET 0x08
35 #define CMD_CMD_TIMEOUT_OFFSET 0x10
36 #define LSB_PUBLIC_MASK_LO_OFFSET 0x18
37 #define LSB_PUBLIC_MASK_HI_OFFSET 0x1C
38 #define LSB_PRIVATE_MASK_LO_OFFSET 0x20
39 #define LSB_PRIVATE_MASK_HI_OFFSET 0x24
41 #define CMD_Q_CONTROL_BASE 0x0000
42 #define CMD_Q_TAIL_LO_BASE 0x0004
43 #define CMD_Q_HEAD_LO_BASE 0x0008
44 #define CMD_Q_INT_ENABLE_BASE 0x000C
45 #define CMD_Q_INTERRUPT_STATUS_BASE 0x0010
47 #define CMD_Q_STATUS_BASE 0x0100
48 #define CMD_Q_INT_STATUS_BASE 0x0104
50 #define CMD_CONFIG_0_OFFSET 0x6000
51 #define CMD_TRNG_CTL_OFFSET 0x6008
52 #define CMD_AES_MASK_OFFSET 0x6010
53 #define CMD_CLK_GATE_CTL_OFFSET 0x603C
55 /* Address offset between two virtual queue registers */
56 #define CMD_Q_STATUS_INCR 0x1000
60 #define CMD_Q_SIZE 0x1F
62 #define COMMANDS_PER_QUEUE 2048
64 #define QUEUE_SIZE_VAL ((ffs(COMMANDS_PER_QUEUE) - 2) & \
66 #define Q_DESC_SIZE sizeof(struct ccp_desc)
67 #define Q_SIZE(n) (COMMANDS_PER_QUEUE*(n))
69 #define INT_COMPLETION 0x1
71 #define INT_QUEUE_STOPPED 0x4
72 #define ALL_INTERRUPTS (INT_COMPLETION| \
76 #define LSB_REGION_WIDTH 5
80 #define LSB_ITEM_SIZE 32
81 #define SLSB_MAP_SIZE (MAX_LSB_CNT * LSB_SIZE)
82 #define LSB_ENTRY_NUMBER(LSB_ADDR) (LSB_ADDR / LSB_ITEM_SIZE)
84 /* General CCP Defines */
86 #define CCP_SB_BYTES 32
88 #define CCP_CMD_DW0(p) ((p)->dw0)
89 #define CCP_CMD_SOC(p) (CCP_CMD_DW0(p).soc)
90 #define CCP_CMD_IOC(p) (CCP_CMD_DW0(p).ioc)
91 #define CCP_CMD_INIT(p) (CCP_CMD_DW0(p).init)
92 #define CCP_CMD_EOM(p) (CCP_CMD_DW0(p).eom)
93 #define CCP_CMD_FUNCTION(p) (CCP_CMD_DW0(p).function)
94 #define CCP_CMD_ENGINE(p) (CCP_CMD_DW0(p).engine)
95 #define CCP_CMD_PROT(p) (CCP_CMD_DW0(p).prot)
98 #define CCP_CMD_DW1(p) ((p)->length)
99 #define CCP_CMD_LEN(p) (CCP_CMD_DW1(p))
102 #define CCP_CMD_DW2(p) ((p)->src_lo)
103 #define CCP_CMD_SRC_LO(p) (CCP_CMD_DW2(p))
106 #define CCP_CMD_DW3(p) ((p)->dw3)
107 #define CCP_CMD_SRC_MEM(p) ((p)->dw3.src_mem)
108 #define CCP_CMD_SRC_HI(p) ((p)->dw3.src_hi)
109 #define CCP_CMD_LSB_ID(p) ((p)->dw3.lsb_cxt_id)
110 #define CCP_CMD_FIX_SRC(p) ((p)->dw3.fixed)
113 #define CCP_CMD_DW4(p) ((p)->dw4)
114 #define CCP_CMD_DST_LO(p) (CCP_CMD_DW4(p).dst_lo)
115 #define CCP_CMD_DW5(p) ((p)->dw5.fields.dst_hi)
116 #define CCP_CMD_DST_HI(p) (CCP_CMD_DW5(p))
117 #define CCP_CMD_DST_MEM(p) ((p)->dw5.fields.dst_mem)
118 #define CCP_CMD_FIX_DST(p) ((p)->dw5.fields.fixed)
119 #define CCP_CMD_SHA_LO(p) ((p)->dw4.sha_len_lo)
120 #define CCP_CMD_SHA_HI(p) ((p)->dw5.sha_len_hi)
123 #define CCP_CMD_DW6(p) ((p)->key_lo)
124 #define CCP_CMD_KEY_LO(p) (CCP_CMD_DW6(p))
125 #define CCP_CMD_DW7(p) ((p)->dw7)
126 #define CCP_CMD_KEY_HI(p) ((p)->dw7.key_hi)
127 #define CCP_CMD_KEY_MEM(p) ((p)->dw7.key_mem)
131 BITS_PER_WORD = sizeof(unsigned long) * CHAR_BIT
134 #define WORD_OFFSET(b) ((b) / BITS_PER_WORD)
135 #define BIT_OFFSET(b) ((b) % BITS_PER_WORD)
137 #define CCP_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
138 #define CCP_BITMAP_SIZE(nr) \
139 CCP_DIV_ROUND_UP(nr, CHAR_BIT * sizeof(unsigned long))
141 #define CCP_BITMAP_FIRST_WORD_MASK(start) \
142 (~0UL << ((start) & (BITS_PER_WORD - 1)))
143 #define CCP_BITMAP_LAST_WORD_MASK(nbits) \
144 (~0UL >> (-(nbits) & (BITS_PER_WORD - 1)))
146 #define __ccp_round_mask(x, y) ((typeof(x))((y)-1))
147 #define ccp_round_down(x, y) ((x) & ~__ccp_round_mask(x, y))
149 /** CCP registers Write/Read */
151 static inline void ccp_pci_reg_write(void *base, int offset,
154 volatile void *reg_addr = ((uint8_t *)base + offset);
156 rte_write32((rte_cpu_to_le_32(value)), reg_addr);
159 static inline uint32_t ccp_pci_reg_read(void *base, int offset)
161 volatile void *reg_addr = ((uint8_t *)base + offset);
163 return rte_le_to_cpu_32(rte_read32(reg_addr));
166 #define CCP_READ_REG(hw_addr, reg_offset) \
167 ccp_pci_reg_read(hw_addr, reg_offset)
169 #define CCP_WRITE_REG(hw_addr, reg_offset, value) \
170 ccp_pci_reg_write(hw_addr, reg_offset, value)
172 TAILQ_HEAD(ccp_list, ccp_device);
174 extern struct ccp_list ccp_list;
179 enum ccp_device_version {
185 * A structure describing a CCP command queue.
188 struct ccp_device *dev;
189 char memz_name[RTE_MEMZONE_NAMESIZE];
191 rte_atomic64_t free_slots;
192 /**< available free slots updated from enq/deq calls */
194 /* Queue identifier */
195 uint64_t id; /**< queue id */
196 uint64_t qidx; /**< queue index */
197 uint64_t qsize; /**< queue size */
200 struct ccp_desc *qbase_desc;
202 phys_addr_t qbase_phys_addr;
203 /**< queue-page registers addr */
207 /**< queue ctrl reg */
210 /**< lsb region assigned to queue */
211 unsigned long lsbmask;
212 /**< lsb regions queue can access */
213 unsigned long lsbmap[CCP_BITMAP_SIZE(LSB_SIZE)];
214 /**< all lsb resources which queue is using */
216 /**< lsb assigned for queue */
218 /**< lsb assigned for iv */
220 /**< lsb assigned for sha ctx */
222 /**< lsb assigned for hmac ctx */
223 } ____cacheline_aligned;
226 * A structure describing a CCP device.
229 TAILQ_ENTRY(ccp_device) next;
231 /**< ccp dev id on platform */
232 struct ccp_queue cmd_q[MAX_HW_QUEUES];
235 /**< no. of ccp Queues */
236 struct rte_pci_device pci;
237 /**< ccp pci identifier */
238 unsigned long lsbmap[CCP_BITMAP_SIZE(SLSB_MAP_SIZE)];
239 /**< shared lsb mask of ccp */
240 rte_spinlock_t lsb_lock;
241 /**< protection for shared lsb region allocation */
243 /**< current queue index */
245 /**< retry counter for CCP TRNG */
246 } __rte_cache_aligned;
248 /**< CCP H/W engine related */
250 * ccp_engine - CCP operation identifiers
252 * @CCP_ENGINE_AES: AES operation
253 * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation
254 * @CCP_ENGINE_3DES: DES/3DES operation
255 * @CCP_ENGINE_SHA: SHA operation
256 * @CCP_ENGINE_RSA: RSA operation
257 * @CCP_ENGINE_PASSTHRU: pass-through operation
258 * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
259 * @CCP_ENGINE_ECC: ECC operation
263 CCP_ENGINE_XTS_AES_128,
268 CCP_ENGINE_ZLIB_DECOMPRESS,
273 /* Passthru engine */
275 * ccp_passthru_bitwise - type of bitwise passthru operation
277 * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed
278 * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask
279 * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask
280 * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask
281 * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask
283 enum ccp_passthru_bitwise {
284 CCP_PASSTHRU_BITWISE_NOOP = 0,
285 CCP_PASSTHRU_BITWISE_AND,
286 CCP_PASSTHRU_BITWISE_OR,
287 CCP_PASSTHRU_BITWISE_XOR,
288 CCP_PASSTHRU_BITWISE_MASK,
289 CCP_PASSTHRU_BITWISE__LAST,
293 * ccp_passthru_byteswap - type of byteswap passthru operation
295 * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed
296 * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words
297 * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words
299 enum ccp_passthru_byteswap {
300 CCP_PASSTHRU_BYTESWAP_NOOP = 0,
301 CCP_PASSTHRU_BYTESWAP_32BIT,
302 CCP_PASSTHRU_BYTESWAP_256BIT,
303 CCP_PASSTHRU_BYTESWAP__LAST,
309 struct ccp_passthru {
310 phys_addr_t src_addr;
311 phys_addr_t dest_addr;
312 enum ccp_passthru_bitwise bit_mod;
313 enum ccp_passthru_byteswap byte_swap;
318 /* CCP version 5: Union to define the function field (cmd_reg1/dword0) */
366 * descriptor for version 5 CPP commands
368 * word 0: function; engine; control bits
369 * word 1: length of source data
370 * word 2: low 32 bits of source pointer
371 * word 3: upper 16 bits of source pointer; source memory type
372 * word 4: low 32 bits of destination pointer
373 * word 5: upper 16 bits of destination pointer; destination memory
375 * word 6: low 32 bits of key pointer
376 * word 7: upper 16 bits of key pointer; key memory type
384 uint32_t function:15;
393 uint32_t lsb_cxt_id:8;
399 uint32_t dst_lo; /* NON-SHA */
400 uint32_t sha_len_lo; /* SHA */
435 CCP_MEMTYPE_SYSTEM = 0,
442 * cmd id to follow order
450 CCP_CMD_NOT_SUPPORTED,
453 static inline uint32_t
454 low32_value(unsigned long addr)
456 return ((uint64_t)addr) & 0x0ffffffff;
459 static inline uint32_t
460 high32_value(unsigned long addr)
462 return ((uint64_t)addr >> 32) & 0x00000ffff;
468 int ccp_dev_start(struct rte_cryptodev *dev);
471 * Detect ccp platform and initialize all ccp devices
473 * @param ccp_id rte_pci_id list for supported CCP devices
474 * @return no. of successfully initialized CCP devices
476 int ccp_probe_devices(const struct rte_pci_id *ccp_id);
479 * allocate a ccp command queue
481 * @dev rte crypto device
482 * @param slot_req number of required
483 * @return allotted CCP queue on success otherwise NULL
485 struct ccp_queue *ccp_allot_queue(struct rte_cryptodev *dev, int slot_req);
490 * @param trng_value data pointer to write RNG value
491 * @return 0 on success otherwise -1
493 int ccp_read_hwrng(uint32_t *trng_value);
495 #endif /* _CCP_DEV_H_ */