1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Cryptographic Coprocessor (CCP) driver
5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
7 * Author: Gary R Hook <gary.hook@amd.com>
10 #include <linux/kernel.h>
11 #include <linux/kthread.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/interrupt.h>
14 #include <linux/compiler.h>
15 #include <linux/ccp.h>
19 /* Allocate the requested number of contiguous LSB slots
20 * from the LSB bitmap. Look in the private range for this
21 * queue first; failing that, check the public area.
22 * If no space is available, wait around.
23 * Return: first slot number
25 static u32
ccp_lsb_alloc(struct ccp_cmd_queue
*cmd_q
, unsigned int count
)
27 struct ccp_device
*ccp
;
30 /* First look at the map for the queue */
31 if (cmd_q
->lsb
>= 0) {
32 start
= (u32
)bitmap_find_next_zero_area(cmd_q
->lsbmap
,
35 if (start
< LSB_SIZE
) {
36 bitmap_set(cmd_q
->lsbmap
, start
, count
);
37 return start
+ cmd_q
->lsb
* LSB_SIZE
;
41 /* No joy; try to get an entry from the shared blocks */
44 mutex_lock(&ccp
->sb_mutex
);
46 start
= (u32
)bitmap_find_next_zero_area(ccp
->lsbmap
,
47 MAX_LSB_CNT
* LSB_SIZE
,
50 if (start
<= MAX_LSB_CNT
* LSB_SIZE
) {
51 bitmap_set(ccp
->lsbmap
, start
, count
);
53 mutex_unlock(&ccp
->sb_mutex
);
59 mutex_unlock(&ccp
->sb_mutex
);
61 /* Wait for KSB entries to become available */
62 if (wait_event_interruptible(ccp
->sb_queue
, ccp
->sb_avail
))
67 /* Free a number of LSB slots from the bitmap, starting at
68 * the indicated starting slot number.
70 static void ccp_lsb_free(struct ccp_cmd_queue
*cmd_q
, unsigned int start
,
76 if (cmd_q
->lsb
== start
) {
77 /* An entry from the private LSB */
78 bitmap_clear(cmd_q
->lsbmap
, start
, count
);
80 /* From the shared LSBs */
81 struct ccp_device
*ccp
= cmd_q
->ccp
;
83 mutex_lock(&ccp
->sb_mutex
);
84 bitmap_clear(ccp
->lsbmap
, start
, count
);
86 mutex_unlock(&ccp
->sb_mutex
);
87 wake_up_interruptible_all(&ccp
->sb_queue
);
91 /* CCP version 5: Union to define the function field (cmd_reg1/dword0) */
137 #define CCP_AES_SIZE(p) ((p)->aes.size)
138 #define CCP_AES_ENCRYPT(p) ((p)->aes.encrypt)
139 #define CCP_AES_MODE(p) ((p)->aes.mode)
140 #define CCP_AES_TYPE(p) ((p)->aes.type)
141 #define CCP_XTS_SIZE(p) ((p)->aes_xts.size)
142 #define CCP_XTS_TYPE(p) ((p)->aes_xts.type)
143 #define CCP_XTS_ENCRYPT(p) ((p)->aes_xts.encrypt)
144 #define CCP_DES3_SIZE(p) ((p)->des3.size)
145 #define CCP_DES3_ENCRYPT(p) ((p)->des3.encrypt)
146 #define CCP_DES3_MODE(p) ((p)->des3.mode)
147 #define CCP_DES3_TYPE(p) ((p)->des3.type)
148 #define CCP_SHA_TYPE(p) ((p)->sha.type)
149 #define CCP_RSA_SIZE(p) ((p)->rsa.size)
150 #define CCP_PT_BYTESWAP(p) ((p)->pt.byteswap)
151 #define CCP_PT_BITWISE(p) ((p)->pt.bitwise)
152 #define CCP_ECC_MODE(p) ((p)->ecc.mode)
153 #define CCP_ECC_AFFINE(p) ((p)->ecc.one)
156 #define CCP5_CMD_DW0(p) ((p)->dw0)
157 #define CCP5_CMD_SOC(p) (CCP5_CMD_DW0(p).soc)
158 #define CCP5_CMD_IOC(p) (CCP5_CMD_DW0(p).ioc)
159 #define CCP5_CMD_INIT(p) (CCP5_CMD_DW0(p).init)
160 #define CCP5_CMD_EOM(p) (CCP5_CMD_DW0(p).eom)
161 #define CCP5_CMD_FUNCTION(p) (CCP5_CMD_DW0(p).function)
162 #define CCP5_CMD_ENGINE(p) (CCP5_CMD_DW0(p).engine)
163 #define CCP5_CMD_PROT(p) (CCP5_CMD_DW0(p).prot)
166 #define CCP5_CMD_DW1(p) ((p)->length)
167 #define CCP5_CMD_LEN(p) (CCP5_CMD_DW1(p))
170 #define CCP5_CMD_DW2(p) ((p)->src_lo)
171 #define CCP5_CMD_SRC_LO(p) (CCP5_CMD_DW2(p))
174 #define CCP5_CMD_DW3(p) ((p)->dw3)
175 #define CCP5_CMD_SRC_MEM(p) ((p)->dw3.src_mem)
176 #define CCP5_CMD_SRC_HI(p) ((p)->dw3.src_hi)
177 #define CCP5_CMD_LSB_ID(p) ((p)->dw3.lsb_cxt_id)
178 #define CCP5_CMD_FIX_SRC(p) ((p)->dw3.fixed)
181 #define CCP5_CMD_DW4(p) ((p)->dw4)
182 #define CCP5_CMD_DST_LO(p) (CCP5_CMD_DW4(p).dst_lo)
183 #define CCP5_CMD_DW5(p) ((p)->dw5.fields.dst_hi)
184 #define CCP5_CMD_DST_HI(p) (CCP5_CMD_DW5(p))
185 #define CCP5_CMD_DST_MEM(p) ((p)->dw5.fields.dst_mem)
186 #define CCP5_CMD_FIX_DST(p) ((p)->dw5.fields.fixed)
187 #define CCP5_CMD_SHA_LO(p) ((p)->dw4.sha_len_lo)
188 #define CCP5_CMD_SHA_HI(p) ((p)->dw5.sha_len_hi)
191 #define CCP5_CMD_DW6(p) ((p)->key_lo)
192 #define CCP5_CMD_KEY_LO(p) (CCP5_CMD_DW6(p))
193 #define CCP5_CMD_DW7(p) ((p)->dw7)
194 #define CCP5_CMD_KEY_HI(p) ((p)->dw7.key_hi)
195 #define CCP5_CMD_KEY_MEM(p) ((p)->dw7.key_mem)
197 static inline u32
low_address(unsigned long addr
)
199 return (u64
)addr
& 0x0ffffffff;
202 static inline u32
high_address(unsigned long addr
)
204 return ((u64
)addr
>> 32) & 0x00000ffff;
207 static unsigned int ccp5_get_free_slots(struct ccp_cmd_queue
*cmd_q
)
209 unsigned int head_idx
, n
;
210 u32 head_lo
, queue_start
;
212 queue_start
= low_address(cmd_q
->qdma_tail
);
213 head_lo
= ioread32(cmd_q
->reg_head_lo
);
214 head_idx
= (head_lo
- queue_start
) / sizeof(struct ccp5_desc
);
216 n
= head_idx
+ COMMANDS_PER_QUEUE
- cmd_q
->qidx
- 1;
218 return n
% COMMANDS_PER_QUEUE
; /* Always one unused spot */
221 static int ccp5_do_cmd(struct ccp5_desc
*desc
,
222 struct ccp_cmd_queue
*cmd_q
)
232 if (CCP5_CMD_SOC(desc
)) {
233 CCP5_CMD_IOC(desc
) = 1;
234 CCP5_CMD_SOC(desc
) = 0;
236 mutex_lock(&cmd_q
->q_mutex
);
238 mP
= (__le32
*)&cmd_q
->qbase
[cmd_q
->qidx
];
240 for (i
= 0; i
< 8; i
++)
241 mP
[i
] = cpu_to_le32(dP
[i
]); /* handle endianness */
243 cmd_q
->qidx
= (cmd_q
->qidx
+ 1) % COMMANDS_PER_QUEUE
;
245 /* The data used by this command must be flushed to memory */
248 /* Write the new tail address back to the queue register */
249 tail
= low_address(cmd_q
->qdma_tail
+ cmd_q
->qidx
* Q_DESC_SIZE
);
250 iowrite32(tail
, cmd_q
->reg_tail_lo
);
252 /* Turn the queue back on using our cached control register */
253 iowrite32(cmd_q
->qcontrol
| CMD5_Q_RUN
, cmd_q
->reg_control
);
254 mutex_unlock(&cmd_q
->q_mutex
);
256 if (CCP5_CMD_IOC(desc
)) {
257 /* Wait for the job to complete */
258 ret
= wait_event_interruptible(cmd_q
->int_queue
,
260 if (ret
|| cmd_q
->cmd_error
) {
261 /* Log the error and flush the queue by
262 * moving the head pointer
264 if (cmd_q
->cmd_error
)
265 ccp_log_error(cmd_q
->ccp
,
267 iowrite32(tail
, cmd_q
->reg_head_lo
);
277 static int ccp5_perform_aes(struct ccp_op
*op
)
279 struct ccp5_desc desc
;
280 union ccp_function function
;
281 u32 key_addr
= op
->sb_key
* LSB_ITEM_SIZE
;
283 op
->cmd_q
->total_aes_ops
++;
285 /* Zero out all the fields of the command desc */
286 memset(&desc
, 0, Q_DESC_SIZE
);
288 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_AES
;
290 CCP5_CMD_SOC(&desc
) = op
->soc
;
291 CCP5_CMD_IOC(&desc
) = 1;
292 CCP5_CMD_INIT(&desc
) = op
->init
;
293 CCP5_CMD_EOM(&desc
) = op
->eom
;
294 CCP5_CMD_PROT(&desc
) = 0;
297 CCP_AES_ENCRYPT(&function
) = op
->u
.aes
.action
;
298 CCP_AES_MODE(&function
) = op
->u
.aes
.mode
;
299 CCP_AES_TYPE(&function
) = op
->u
.aes
.type
;
300 CCP_AES_SIZE(&function
) = op
->u
.aes
.size
;
302 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
304 CCP5_CMD_LEN(&desc
) = op
->src
.u
.dma
.length
;
306 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
307 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
308 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
310 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
311 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
312 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
314 CCP5_CMD_KEY_LO(&desc
) = lower_32_bits(key_addr
);
315 CCP5_CMD_KEY_HI(&desc
) = 0;
316 CCP5_CMD_KEY_MEM(&desc
) = CCP_MEMTYPE_SB
;
317 CCP5_CMD_LSB_ID(&desc
) = op
->sb_ctx
;
319 return ccp5_do_cmd(&desc
, op
->cmd_q
);
322 static int ccp5_perform_xts_aes(struct ccp_op
*op
)
324 struct ccp5_desc desc
;
325 union ccp_function function
;
326 u32 key_addr
= op
->sb_key
* LSB_ITEM_SIZE
;
328 op
->cmd_q
->total_xts_aes_ops
++;
330 /* Zero out all the fields of the command desc */
331 memset(&desc
, 0, Q_DESC_SIZE
);
333 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_XTS_AES_128
;
335 CCP5_CMD_SOC(&desc
) = op
->soc
;
336 CCP5_CMD_IOC(&desc
) = 1;
337 CCP5_CMD_INIT(&desc
) = op
->init
;
338 CCP5_CMD_EOM(&desc
) = op
->eom
;
339 CCP5_CMD_PROT(&desc
) = 0;
342 CCP_XTS_TYPE(&function
) = op
->u
.xts
.type
;
343 CCP_XTS_ENCRYPT(&function
) = op
->u
.xts
.action
;
344 CCP_XTS_SIZE(&function
) = op
->u
.xts
.unit_size
;
345 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
347 CCP5_CMD_LEN(&desc
) = op
->src
.u
.dma
.length
;
349 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
350 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
351 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
353 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
354 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
355 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
357 CCP5_CMD_KEY_LO(&desc
) = lower_32_bits(key_addr
);
358 CCP5_CMD_KEY_HI(&desc
) = 0;
359 CCP5_CMD_KEY_MEM(&desc
) = CCP_MEMTYPE_SB
;
360 CCP5_CMD_LSB_ID(&desc
) = op
->sb_ctx
;
362 return ccp5_do_cmd(&desc
, op
->cmd_q
);
365 static int ccp5_perform_sha(struct ccp_op
*op
)
367 struct ccp5_desc desc
;
368 union ccp_function function
;
370 op
->cmd_q
->total_sha_ops
++;
372 /* Zero out all the fields of the command desc */
373 memset(&desc
, 0, Q_DESC_SIZE
);
375 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_SHA
;
377 CCP5_CMD_SOC(&desc
) = op
->soc
;
378 CCP5_CMD_IOC(&desc
) = 1;
379 CCP5_CMD_INIT(&desc
) = 1;
380 CCP5_CMD_EOM(&desc
) = op
->eom
;
381 CCP5_CMD_PROT(&desc
) = 0;
384 CCP_SHA_TYPE(&function
) = op
->u
.sha
.type
;
385 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
387 CCP5_CMD_LEN(&desc
) = op
->src
.u
.dma
.length
;
389 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
390 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
391 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
393 CCP5_CMD_LSB_ID(&desc
) = op
->sb_ctx
;
396 CCP5_CMD_SHA_LO(&desc
) = lower_32_bits(op
->u
.sha
.msg_bits
);
397 CCP5_CMD_SHA_HI(&desc
) = upper_32_bits(op
->u
.sha
.msg_bits
);
399 CCP5_CMD_SHA_LO(&desc
) = 0;
400 CCP5_CMD_SHA_HI(&desc
) = 0;
403 return ccp5_do_cmd(&desc
, op
->cmd_q
);
406 static int ccp5_perform_des3(struct ccp_op
*op
)
408 struct ccp5_desc desc
;
409 union ccp_function function
;
410 u32 key_addr
= op
->sb_key
* LSB_ITEM_SIZE
;
412 op
->cmd_q
->total_3des_ops
++;
414 /* Zero out all the fields of the command desc */
415 memset(&desc
, 0, sizeof(struct ccp5_desc
));
417 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_DES3
;
419 CCP5_CMD_SOC(&desc
) = op
->soc
;
420 CCP5_CMD_IOC(&desc
) = 1;
421 CCP5_CMD_INIT(&desc
) = op
->init
;
422 CCP5_CMD_EOM(&desc
) = op
->eom
;
423 CCP5_CMD_PROT(&desc
) = 0;
426 CCP_DES3_ENCRYPT(&function
) = op
->u
.des3
.action
;
427 CCP_DES3_MODE(&function
) = op
->u
.des3
.mode
;
428 CCP_DES3_TYPE(&function
) = op
->u
.des3
.type
;
429 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
431 CCP5_CMD_LEN(&desc
) = op
->src
.u
.dma
.length
;
433 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
434 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
435 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
437 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
438 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
439 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
441 CCP5_CMD_KEY_LO(&desc
) = lower_32_bits(key_addr
);
442 CCP5_CMD_KEY_HI(&desc
) = 0;
443 CCP5_CMD_KEY_MEM(&desc
) = CCP_MEMTYPE_SB
;
444 CCP5_CMD_LSB_ID(&desc
) = op
->sb_ctx
;
446 return ccp5_do_cmd(&desc
, op
->cmd_q
);
449 static int ccp5_perform_rsa(struct ccp_op
*op
)
451 struct ccp5_desc desc
;
452 union ccp_function function
;
454 op
->cmd_q
->total_rsa_ops
++;
456 /* Zero out all the fields of the command desc */
457 memset(&desc
, 0, Q_DESC_SIZE
);
459 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_RSA
;
461 CCP5_CMD_SOC(&desc
) = op
->soc
;
462 CCP5_CMD_IOC(&desc
) = 1;
463 CCP5_CMD_INIT(&desc
) = 0;
464 CCP5_CMD_EOM(&desc
) = 1;
465 CCP5_CMD_PROT(&desc
) = 0;
468 CCP_RSA_SIZE(&function
) = (op
->u
.rsa
.mod_size
+ 7) >> 3;
469 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
471 CCP5_CMD_LEN(&desc
) = op
->u
.rsa
.input_len
;
473 /* Source is from external memory */
474 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
475 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
476 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
478 /* Destination is in external memory */
479 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
480 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
481 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
483 /* Key (Exponent) is in external memory */
484 CCP5_CMD_KEY_LO(&desc
) = ccp_addr_lo(&op
->exp
.u
.dma
);
485 CCP5_CMD_KEY_HI(&desc
) = ccp_addr_hi(&op
->exp
.u
.dma
);
486 CCP5_CMD_KEY_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
488 return ccp5_do_cmd(&desc
, op
->cmd_q
);
491 static int ccp5_perform_passthru(struct ccp_op
*op
)
493 struct ccp5_desc desc
;
494 union ccp_function function
;
495 struct ccp_dma_info
*saddr
= &op
->src
.u
.dma
;
496 struct ccp_dma_info
*daddr
= &op
->dst
.u
.dma
;
499 op
->cmd_q
->total_pt_ops
++;
501 memset(&desc
, 0, Q_DESC_SIZE
);
503 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_PASSTHRU
;
505 CCP5_CMD_SOC(&desc
) = 0;
506 CCP5_CMD_IOC(&desc
) = 1;
507 CCP5_CMD_INIT(&desc
) = 0;
508 CCP5_CMD_EOM(&desc
) = op
->eom
;
509 CCP5_CMD_PROT(&desc
) = 0;
512 CCP_PT_BYTESWAP(&function
) = op
->u
.passthru
.byte_swap
;
513 CCP_PT_BITWISE(&function
) = op
->u
.passthru
.bit_mod
;
514 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
516 /* Length of source data is always 256 bytes */
517 if (op
->src
.type
== CCP_MEMTYPE_SYSTEM
)
518 CCP5_CMD_LEN(&desc
) = saddr
->length
;
520 CCP5_CMD_LEN(&desc
) = daddr
->length
;
522 if (op
->src
.type
== CCP_MEMTYPE_SYSTEM
) {
523 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
524 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
525 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
527 if (op
->u
.passthru
.bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
)
528 CCP5_CMD_LSB_ID(&desc
) = op
->sb_key
;
530 u32 key_addr
= op
->src
.u
.sb
* CCP_SB_BYTES
;
532 CCP5_CMD_SRC_LO(&desc
) = lower_32_bits(key_addr
);
533 CCP5_CMD_SRC_HI(&desc
) = 0;
534 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SB
;
537 if (op
->dst
.type
== CCP_MEMTYPE_SYSTEM
) {
538 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
539 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
540 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
542 u32 key_addr
= op
->dst
.u
.sb
* CCP_SB_BYTES
;
544 CCP5_CMD_DST_LO(&desc
) = lower_32_bits(key_addr
);
545 CCP5_CMD_DST_HI(&desc
) = 0;
546 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SB
;
549 return ccp5_do_cmd(&desc
, op
->cmd_q
);
552 static int ccp5_perform_ecc(struct ccp_op
*op
)
554 struct ccp5_desc desc
;
555 union ccp_function function
;
557 op
->cmd_q
->total_ecc_ops
++;
559 /* Zero out all the fields of the command desc */
560 memset(&desc
, 0, Q_DESC_SIZE
);
562 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_ECC
;
564 CCP5_CMD_SOC(&desc
) = 0;
565 CCP5_CMD_IOC(&desc
) = 1;
566 CCP5_CMD_INIT(&desc
) = 0;
567 CCP5_CMD_EOM(&desc
) = 1;
568 CCP5_CMD_PROT(&desc
) = 0;
571 function
.ecc
.mode
= op
->u
.ecc
.function
;
572 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
574 CCP5_CMD_LEN(&desc
) = op
->src
.u
.dma
.length
;
576 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
577 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
578 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
580 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
581 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
582 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
584 return ccp5_do_cmd(&desc
, op
->cmd_q
);
587 static int ccp_find_lsb_regions(struct ccp_cmd_queue
*cmd_q
, u64 status
)
589 int q_mask
= 1 << cmd_q
->id
;
593 /* Build a bit mask to know which LSBs this queue has access to.
594 * Don't bother with segment 0 as it has special privileges.
596 for (j
= 1; j
< MAX_LSB_CNT
; j
++) {
598 bitmap_set(cmd_q
->lsbmask
, j
, 1);
599 status
>>= LSB_REGION_WIDTH
;
601 queues
= bitmap_weight(cmd_q
->lsbmask
, MAX_LSB_CNT
);
602 dev_dbg(cmd_q
->ccp
->dev
, "Queue %d can access %d LSB regions\n",
605 return queues
? 0 : -EINVAL
;
608 static int ccp_find_and_assign_lsb_to_q(struct ccp_device
*ccp
,
609 int lsb_cnt
, int n_lsbs
,
610 unsigned long *lsb_pub
)
612 DECLARE_BITMAP(qlsb
, MAX_LSB_CNT
);
618 * If the count of potential LSBs available to a queue matches the
619 * ordinal given to us in lsb_cnt:
620 * Copy the mask of possible LSBs for this queue into "qlsb";
621 * For each bit in qlsb, see if the corresponding bit in the
622 * aggregation mask is set; if so, we have a match.
623 * If we have a match, clear the bit in the aggregation to
624 * mark it as no longer available.
625 * If there is no match, clear the bit in qlsb and keep looking.
627 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
628 struct ccp_cmd_queue
*cmd_q
= &ccp
->cmd_q
[i
];
630 qlsb_wgt
= bitmap_weight(cmd_q
->lsbmask
, MAX_LSB_CNT
);
632 if (qlsb_wgt
== lsb_cnt
) {
633 bitmap_copy(qlsb
, cmd_q
->lsbmask
, MAX_LSB_CNT
);
635 bitno
= find_first_bit(qlsb
, MAX_LSB_CNT
);
636 while (bitno
< MAX_LSB_CNT
) {
637 if (test_bit(bitno
, lsb_pub
)) {
638 /* We found an available LSB
639 * that this queue can access
642 bitmap_clear(lsb_pub
, bitno
, 1);
644 "Queue %d gets LSB %d\n",
648 bitmap_clear(qlsb
, bitno
, 1);
649 bitno
= find_first_bit(qlsb
, MAX_LSB_CNT
);
651 if (bitno
>= MAX_LSB_CNT
)
659 /* For each queue, from the most- to least-constrained:
660 * find an LSB that can be assigned to the queue. If there are N queues that
661 * can only use M LSBs, where N > M, fail; otherwise, every queue will get a
662 * dedicated LSB. Remaining LSB regions become a shared resource.
663 * If we have fewer LSBs than queues, all LSB regions become shared resources.
665 static int ccp_assign_lsbs(struct ccp_device
*ccp
)
667 DECLARE_BITMAP(lsb_pub
, MAX_LSB_CNT
);
668 DECLARE_BITMAP(qlsb
, MAX_LSB_CNT
);
674 bitmap_zero(lsb_pub
, MAX_LSB_CNT
);
676 /* Create an aggregate bitmap to get a total count of available LSBs */
677 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
679 lsb_pub
, ccp
->cmd_q
[i
].lsbmask
,
682 n_lsbs
= bitmap_weight(lsb_pub
, MAX_LSB_CNT
);
684 if (n_lsbs
>= ccp
->cmd_q_count
) {
685 /* We have enough LSBS to give every queue a private LSB.
686 * Brute force search to start with the queues that are more
687 * constrained in LSB choice. When an LSB is privately
688 * assigned, it is removed from the public mask.
689 * This is an ugly N squared algorithm with some optimization.
692 n_lsbs
&& (lsb_cnt
<= MAX_LSB_CNT
);
694 rc
= ccp_find_and_assign_lsb_to_q(ccp
, lsb_cnt
, n_lsbs
,
703 /* What's left of the LSBs, according to the public mask, now become
704 * shared. Any zero bits in the lsb_pub mask represent an LSB region
705 * that can't be used as a shared resource, so mark the LSB slots for
708 bitmap_copy(qlsb
, lsb_pub
, MAX_LSB_CNT
);
710 bitno
= find_first_zero_bit(qlsb
, MAX_LSB_CNT
);
711 while (bitno
< MAX_LSB_CNT
) {
712 bitmap_set(ccp
->lsbmap
, bitno
* LSB_SIZE
, LSB_SIZE
);
713 bitmap_set(qlsb
, bitno
, 1);
714 bitno
= find_first_zero_bit(qlsb
, MAX_LSB_CNT
);
720 static void ccp5_disable_queue_interrupts(struct ccp_device
*ccp
)
724 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
725 iowrite32(0x0, ccp
->cmd_q
[i
].reg_int_enable
);
728 static void ccp5_enable_queue_interrupts(struct ccp_device
*ccp
)
732 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
733 iowrite32(SUPPORTED_INTERRUPTS
, ccp
->cmd_q
[i
].reg_int_enable
);
736 static void ccp5_irq_bh(unsigned long data
)
738 struct ccp_device
*ccp
= (struct ccp_device
*)data
;
742 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
743 struct ccp_cmd_queue
*cmd_q
= &ccp
->cmd_q
[i
];
745 status
= ioread32(cmd_q
->reg_interrupt_status
);
748 cmd_q
->int_status
= status
;
749 cmd_q
->q_status
= ioread32(cmd_q
->reg_status
);
750 cmd_q
->q_int_status
= ioread32(cmd_q
->reg_int_status
);
752 /* On error, only save the first error value */
753 if ((status
& INT_ERROR
) && !cmd_q
->cmd_error
)
754 cmd_q
->cmd_error
= CMD_Q_ERROR(cmd_q
->q_status
);
758 /* Acknowledge the interrupt and wake the kthread */
759 iowrite32(status
, cmd_q
->reg_interrupt_status
);
760 wake_up_interruptible(&cmd_q
->int_queue
);
763 ccp5_enable_queue_interrupts(ccp
);
766 static irqreturn_t
ccp5_irq_handler(int irq
, void *data
)
768 struct ccp_device
*ccp
= (struct ccp_device
*)data
;
770 ccp5_disable_queue_interrupts(ccp
);
771 ccp
->total_interrupts
++;
772 if (ccp
->use_tasklet
)
773 tasklet_schedule(&ccp
->irq_tasklet
);
775 ccp5_irq_bh((unsigned long)ccp
);
779 static int ccp5_init(struct ccp_device
*ccp
)
781 struct device
*dev
= ccp
->dev
;
782 struct ccp_cmd_queue
*cmd_q
;
783 struct dma_pool
*dma_pool
;
784 char dma_pool_name
[MAX_DMAPOOL_NAME_LEN
];
787 u32 status_lo
, status_hi
;
790 /* Find available queues */
791 qmr
= ioread32(ccp
->io_regs
+ Q_MASK_REG
);
793 * Check for a access to the registers. If this read returns
794 * 0xffffffff, it's likely that the system is running a broken
795 * BIOS which disallows access to the device. Stop here and fail
796 * the initialization (but not the load, as the PSP could get
797 * properly initialized).
799 if (qmr
== 0xffffffff) {
800 dev_notice(dev
, "ccp: unable to access the device: you might be running a broken BIOS.\n");
804 for (i
= 0; (i
< MAX_HW_QUEUES
) && (ccp
->cmd_q_count
< ccp
->max_q_count
); i
++) {
805 if (!(qmr
& (1 << i
)))
808 /* Allocate a dma pool for this queue */
809 snprintf(dma_pool_name
, sizeof(dma_pool_name
), "%s_q%d",
811 dma_pool
= dma_pool_create(dma_pool_name
, dev
,
812 CCP_DMAPOOL_MAX_SIZE
,
813 CCP_DMAPOOL_ALIGN
, 0);
815 dev_err(dev
, "unable to allocate dma pool\n");
820 cmd_q
= &ccp
->cmd_q
[ccp
->cmd_q_count
];
825 cmd_q
->dma_pool
= dma_pool
;
826 mutex_init(&cmd_q
->q_mutex
);
828 /* Page alignment satisfies our needs for N <= 128 */
829 BUILD_BUG_ON(COMMANDS_PER_QUEUE
> 128);
830 cmd_q
->qsize
= Q_SIZE(Q_DESC_SIZE
);
831 cmd_q
->qbase
= dmam_alloc_coherent(dev
, cmd_q
->qsize
,
835 dev_err(dev
, "unable to allocate command queue\n");
841 /* Preset some register values and masks that are queue
844 cmd_q
->reg_control
= ccp
->io_regs
+
845 CMD5_Q_STATUS_INCR
* (i
+ 1);
846 cmd_q
->reg_tail_lo
= cmd_q
->reg_control
+ CMD5_Q_TAIL_LO_BASE
;
847 cmd_q
->reg_head_lo
= cmd_q
->reg_control
+ CMD5_Q_HEAD_LO_BASE
;
848 cmd_q
->reg_int_enable
= cmd_q
->reg_control
+
849 CMD5_Q_INT_ENABLE_BASE
;
850 cmd_q
->reg_interrupt_status
= cmd_q
->reg_control
+
851 CMD5_Q_INTERRUPT_STATUS_BASE
;
852 cmd_q
->reg_status
= cmd_q
->reg_control
+ CMD5_Q_STATUS_BASE
;
853 cmd_q
->reg_int_status
= cmd_q
->reg_control
+
854 CMD5_Q_INT_STATUS_BASE
;
855 cmd_q
->reg_dma_status
= cmd_q
->reg_control
+
856 CMD5_Q_DMA_STATUS_BASE
;
857 cmd_q
->reg_dma_read_status
= cmd_q
->reg_control
+
858 CMD5_Q_DMA_READ_STATUS_BASE
;
859 cmd_q
->reg_dma_write_status
= cmd_q
->reg_control
+
860 CMD5_Q_DMA_WRITE_STATUS_BASE
;
862 init_waitqueue_head(&cmd_q
->int_queue
);
864 dev_dbg(dev
, "queue #%u available\n", i
);
867 if (ccp
->cmd_q_count
== 0) {
868 dev_notice(dev
, "no command queues available\n");
873 /* Turn off the queues and disable interrupts until ready */
874 ccp5_disable_queue_interrupts(ccp
);
875 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
876 cmd_q
= &ccp
->cmd_q
[i
];
878 cmd_q
->qcontrol
= 0; /* Start with nothing */
879 iowrite32(cmd_q
->qcontrol
, cmd_q
->reg_control
);
881 ioread32(cmd_q
->reg_int_status
);
882 ioread32(cmd_q
->reg_status
);
884 /* Clear the interrupt status */
885 iowrite32(SUPPORTED_INTERRUPTS
, cmd_q
->reg_interrupt_status
);
888 dev_dbg(dev
, "Requesting an IRQ...\n");
890 ret
= sp_request_ccp_irq(ccp
->sp
, ccp5_irq_handler
, ccp
->name
, ccp
);
892 dev_err(dev
, "unable to allocate an IRQ\n");
895 /* Initialize the ISR tasklet */
896 if (ccp
->use_tasklet
)
897 tasklet_init(&ccp
->irq_tasklet
, ccp5_irq_bh
,
900 dev_dbg(dev
, "Loading LSB map...\n");
901 /* Copy the private LSB mask to the public registers */
902 status_lo
= ioread32(ccp
->io_regs
+ LSB_PRIVATE_MASK_LO_OFFSET
);
903 status_hi
= ioread32(ccp
->io_regs
+ LSB_PRIVATE_MASK_HI_OFFSET
);
904 iowrite32(status_lo
, ccp
->io_regs
+ LSB_PUBLIC_MASK_LO_OFFSET
);
905 iowrite32(status_hi
, ccp
->io_regs
+ LSB_PUBLIC_MASK_HI_OFFSET
);
906 status
= ((u64
)status_hi
<<30) | (u64
)status_lo
;
908 dev_dbg(dev
, "Configuring virtual queues...\n");
909 /* Configure size of each virtual queue accessible to host */
910 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
914 cmd_q
= &ccp
->cmd_q
[i
];
916 cmd_q
->qcontrol
&= ~(CMD5_Q_SIZE
<< CMD5_Q_SHIFT
);
917 cmd_q
->qcontrol
|= QUEUE_SIZE_VAL
<< CMD5_Q_SHIFT
;
919 cmd_q
->qdma_tail
= cmd_q
->qbase_dma
;
920 dma_addr_lo
= low_address(cmd_q
->qdma_tail
);
921 iowrite32((u32
)dma_addr_lo
, cmd_q
->reg_tail_lo
);
922 iowrite32((u32
)dma_addr_lo
, cmd_q
->reg_head_lo
);
924 dma_addr_hi
= high_address(cmd_q
->qdma_tail
);
925 cmd_q
->qcontrol
|= (dma_addr_hi
<< 16);
926 iowrite32(cmd_q
->qcontrol
, cmd_q
->reg_control
);
928 /* Find the LSB regions accessible to the queue */
929 ccp_find_lsb_regions(cmd_q
, status
);
930 cmd_q
->lsb
= -1; /* Unassigned value */
933 dev_dbg(dev
, "Assigning LSBs...\n");
934 ret
= ccp_assign_lsbs(ccp
);
936 dev_err(dev
, "Unable to assign LSBs (%d)\n", ret
);
940 /* Optimization: pre-allocate LSB slots for each queue */
941 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
942 ccp
->cmd_q
[i
].sb_key
= ccp_lsb_alloc(&ccp
->cmd_q
[i
], 2);
943 ccp
->cmd_q
[i
].sb_ctx
= ccp_lsb_alloc(&ccp
->cmd_q
[i
], 2);
946 dev_dbg(dev
, "Starting threads...\n");
947 /* Create a kthread for each queue */
948 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
949 struct task_struct
*kthread
;
951 cmd_q
= &ccp
->cmd_q
[i
];
953 kthread
= kthread_create(ccp_cmd_queue_thread
, cmd_q
,
954 "%s-q%u", ccp
->name
, cmd_q
->id
);
955 if (IS_ERR(kthread
)) {
956 dev_err(dev
, "error creating queue thread (%ld)\n",
958 ret
= PTR_ERR(kthread
);
962 cmd_q
->kthread
= kthread
;
963 wake_up_process(kthread
);
966 dev_dbg(dev
, "Enabling interrupts...\n");
967 ccp5_enable_queue_interrupts(ccp
);
969 dev_dbg(dev
, "Registering device...\n");
970 /* Put this on the unit list to make it available */
973 ret
= ccp_register_rng(ccp
);
977 /* Register the DMA engine support */
978 ret
= ccp_dmaengine_register(ccp
);
982 #ifdef CONFIG_CRYPTO_DEV_CCP_DEBUGFS
983 /* Set up debugfs entries */
984 ccp5_debugfs_setup(ccp
);
990 ccp_unregister_rng(ccp
);
993 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
994 if (ccp
->cmd_q
[i
].kthread
)
995 kthread_stop(ccp
->cmd_q
[i
].kthread
);
998 sp_free_ccp_irq(ccp
->sp
, ccp
);
1001 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
1002 dma_pool_destroy(ccp
->cmd_q
[i
].dma_pool
);
1007 static void ccp5_destroy(struct ccp_device
*ccp
)
1009 struct ccp_cmd_queue
*cmd_q
;
1010 struct ccp_cmd
*cmd
;
1013 /* Unregister the DMA engine */
1014 ccp_dmaengine_unregister(ccp
);
1016 /* Unregister the RNG */
1017 ccp_unregister_rng(ccp
);
1019 /* Remove this device from the list of available units first */
1020 ccp_del_device(ccp
);
1022 #ifdef CONFIG_CRYPTO_DEV_CCP_DEBUGFS
1023 /* We're in the process of tearing down the entire driver;
1024 * when all the devices are gone clean up debugfs
1027 ccp5_debugfs_destroy();
1030 /* Disable and clear interrupts */
1031 ccp5_disable_queue_interrupts(ccp
);
1032 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
1033 cmd_q
= &ccp
->cmd_q
[i
];
1035 /* Turn off the run bit */
1036 iowrite32(cmd_q
->qcontrol
& ~CMD5_Q_RUN
, cmd_q
->reg_control
);
1038 /* Clear the interrupt status */
1039 iowrite32(SUPPORTED_INTERRUPTS
, cmd_q
->reg_interrupt_status
);
1040 ioread32(cmd_q
->reg_int_status
);
1041 ioread32(cmd_q
->reg_status
);
1044 /* Stop the queue kthreads */
1045 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
1046 if (ccp
->cmd_q
[i
].kthread
)
1047 kthread_stop(ccp
->cmd_q
[i
].kthread
);
1049 sp_free_ccp_irq(ccp
->sp
, ccp
);
1051 /* Flush the cmd and backlog queue */
1052 while (!list_empty(&ccp
->cmd
)) {
1053 /* Invoke the callback directly with an error code */
1054 cmd
= list_first_entry(&ccp
->cmd
, struct ccp_cmd
, entry
);
1055 list_del(&cmd
->entry
);
1056 cmd
->callback(cmd
->data
, -ENODEV
);
1058 while (!list_empty(&ccp
->backlog
)) {
1059 /* Invoke the callback directly with an error code */
1060 cmd
= list_first_entry(&ccp
->backlog
, struct ccp_cmd
, entry
);
1061 list_del(&cmd
->entry
);
1062 cmd
->callback(cmd
->data
, -ENODEV
);
1066 static void ccp5_config(struct ccp_device
*ccp
)
1069 iowrite32(0x0, ccp
->io_regs
+ CMD5_REQID_CONFIG_OFFSET
);
1072 static void ccp5other_config(struct ccp_device
*ccp
)
1077 /* We own all of the queues on the NTB CCP */
1079 iowrite32(0x00012D57, ccp
->io_regs
+ CMD5_TRNG_CTL_OFFSET
);
1080 iowrite32(0x00000003, ccp
->io_regs
+ CMD5_CONFIG_0_OFFSET
);
1081 for (i
= 0; i
< 12; i
++) {
1082 rnd
= ioread32(ccp
->io_regs
+ TRNG_OUT_REG
);
1083 iowrite32(rnd
, ccp
->io_regs
+ CMD5_AES_MASK_OFFSET
);
1086 iowrite32(0x0000001F, ccp
->io_regs
+ CMD5_QUEUE_MASK_OFFSET
);
1087 iowrite32(0x00005B6D, ccp
->io_regs
+ CMD5_QUEUE_PRIO_OFFSET
);
1088 iowrite32(0x00000000, ccp
->io_regs
+ CMD5_CMD_TIMEOUT_OFFSET
);
1090 iowrite32(0x3FFFFFFF, ccp
->io_regs
+ LSB_PRIVATE_MASK_LO_OFFSET
);
1091 iowrite32(0x000003FF, ccp
->io_regs
+ LSB_PRIVATE_MASK_HI_OFFSET
);
1093 iowrite32(0x00108823, ccp
->io_regs
+ CMD5_CLK_GATE_CTL_OFFSET
);
1098 /* Version 5 adds some function, but is essentially the same as v5 */
1099 static const struct ccp_actions ccp5_actions
= {
1100 .aes
= ccp5_perform_aes
,
1101 .xts_aes
= ccp5_perform_xts_aes
,
1102 .sha
= ccp5_perform_sha
,
1103 .des3
= ccp5_perform_des3
,
1104 .rsa
= ccp5_perform_rsa
,
1105 .passthru
= ccp5_perform_passthru
,
1106 .ecc
= ccp5_perform_ecc
,
1107 .sballoc
= ccp_lsb_alloc
,
1108 .sbfree
= ccp_lsb_free
,
1110 .destroy
= ccp5_destroy
,
1111 .get_free_slots
= ccp5_get_free_slots
,
1114 const struct ccp_vdata ccpv5a
= {
1115 .version
= CCP_VERSION(5, 0),
1116 .setup
= ccp5_config
,
1117 .perform
= &ccp5_actions
,
1119 .rsamax
= CCP5_RSA_MAX_WIDTH
,
1122 const struct ccp_vdata ccpv5b
= {
1123 .version
= CCP_VERSION(5, 0),
1124 .dma_chan_attr
= DMA_PRIVATE
,
1125 .setup
= ccp5other_config
,
1126 .perform
= &ccp5_actions
,
1128 .rsamax
= CCP5_RSA_MAX_WIDTH
,