2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 * Author: Gary R Hook <gary.hook@amd.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/kthread.h>
18 #include <linux/interrupt.h>
19 #include <linux/ccp.h>
23 static u32
ccp_alloc_ksb(struct ccp_cmd_queue
*cmd_q
, unsigned int count
)
26 struct ccp_device
*ccp
= cmd_q
->ccp
;
29 mutex_lock(&ccp
->sb_mutex
);
31 start
= (u32
)bitmap_find_next_zero_area(ccp
->sb
,
35 if (start
<= ccp
->sb_count
) {
36 bitmap_set(ccp
->sb
, start
, count
);
38 mutex_unlock(&ccp
->sb_mutex
);
44 mutex_unlock(&ccp
->sb_mutex
);
46 /* Wait for KSB entries to become available */
47 if (wait_event_interruptible(ccp
->sb_queue
, ccp
->sb_avail
))
51 return KSB_START
+ start
;
54 static void ccp_free_ksb(struct ccp_cmd_queue
*cmd_q
, unsigned int start
,
57 struct ccp_device
*ccp
= cmd_q
->ccp
;
62 mutex_lock(&ccp
->sb_mutex
);
64 bitmap_clear(ccp
->sb
, start
- KSB_START
, count
);
68 mutex_unlock(&ccp
->sb_mutex
);
70 wake_up_interruptible_all(&ccp
->sb_queue
);
73 static unsigned int ccp_get_free_slots(struct ccp_cmd_queue
*cmd_q
)
75 return CMD_Q_DEPTH(ioread32(cmd_q
->reg_status
));
78 static int ccp_do_cmd(struct ccp_op
*op
, u32
*cr
, unsigned int cr_count
)
80 struct ccp_cmd_queue
*cmd_q
= op
->cmd_q
;
81 struct ccp_device
*ccp
= cmd_q
->ccp
;
82 void __iomem
*cr_addr
;
87 /* We could read a status register to see how many free slots
88 * are actually available, but reading that register resets it
89 * and you could lose some error information.
93 cr0
= (cmd_q
->id
<< REQ0_CMD_Q_SHIFT
)
94 | (op
->jobid
<< REQ0_JOBID_SHIFT
)
95 | REQ0_WAIT_FOR_WRITE
;
98 cr0
|= REQ0_STOP_ON_COMPLETE
99 | REQ0_INT_ON_COMPLETE
;
101 if (op
->ioc
|| !cmd_q
->free_slots
)
102 cr0
|= REQ0_INT_ON_COMPLETE
;
104 /* Start at CMD_REQ1 */
105 cr_addr
= ccp
->io_regs
+ CMD_REQ0
+ CMD_REQ_INCR
;
107 mutex_lock(&ccp
->req_mutex
);
109 /* Write CMD_REQ1 through CMD_REQx first */
110 for (i
= 0; i
< cr_count
; i
++, cr_addr
+= CMD_REQ_INCR
)
111 iowrite32(*(cr
+ i
), cr_addr
);
113 /* Tell the CCP to start */
115 iowrite32(cr0
, ccp
->io_regs
+ CMD_REQ0
);
117 mutex_unlock(&ccp
->req_mutex
);
119 if (cr0
& REQ0_INT_ON_COMPLETE
) {
120 /* Wait for the job to complete */
121 ret
= wait_event_interruptible(cmd_q
->int_queue
,
123 if (ret
|| cmd_q
->cmd_error
) {
124 /* On error delete all related jobs from the queue */
125 cmd
= (cmd_q
->id
<< DEL_Q_ID_SHIFT
)
128 iowrite32(cmd
, ccp
->io_regs
+ DEL_CMD_Q_JOB
);
132 } else if (op
->soc
) {
133 /* Delete just head job from the queue on SoC */
135 | (cmd_q
->id
<< DEL_Q_ID_SHIFT
)
138 iowrite32(cmd
, ccp
->io_regs
+ DEL_CMD_Q_JOB
);
141 cmd_q
->free_slots
= CMD_Q_DEPTH(cmd_q
->q_status
);
149 static int ccp_perform_aes(struct ccp_op
*op
)
153 /* Fill out the register contents for REQ1 through REQ6 */
154 cr
[0] = (CCP_ENGINE_AES
<< REQ1_ENGINE_SHIFT
)
155 | (op
->u
.aes
.type
<< REQ1_AES_TYPE_SHIFT
)
156 | (op
->u
.aes
.mode
<< REQ1_AES_MODE_SHIFT
)
157 | (op
->u
.aes
.action
<< REQ1_AES_ACTION_SHIFT
)
158 | (op
->sb_key
<< REQ1_KEY_KSB_SHIFT
);
159 cr
[1] = op
->src
.u
.dma
.length
- 1;
160 cr
[2] = ccp_addr_lo(&op
->src
.u
.dma
);
161 cr
[3] = (op
->sb_ctx
<< REQ4_KSB_SHIFT
)
162 | (CCP_MEMTYPE_SYSTEM
<< REQ4_MEMTYPE_SHIFT
)
163 | ccp_addr_hi(&op
->src
.u
.dma
);
164 cr
[4] = ccp_addr_lo(&op
->dst
.u
.dma
);
165 cr
[5] = (CCP_MEMTYPE_SYSTEM
<< REQ6_MEMTYPE_SHIFT
)
166 | ccp_addr_hi(&op
->dst
.u
.dma
);
168 if (op
->u
.aes
.mode
== CCP_AES_MODE_CFB
)
169 cr
[0] |= ((0x7f) << REQ1_AES_CFB_SIZE_SHIFT
);
177 return ccp_do_cmd(op
, cr
, ARRAY_SIZE(cr
));
180 static int ccp_perform_xts_aes(struct ccp_op
*op
)
184 /* Fill out the register contents for REQ1 through REQ6 */
185 cr
[0] = (CCP_ENGINE_XTS_AES_128
<< REQ1_ENGINE_SHIFT
)
186 | (op
->u
.xts
.action
<< REQ1_AES_ACTION_SHIFT
)
187 | (op
->u
.xts
.unit_size
<< REQ1_XTS_AES_SIZE_SHIFT
)
188 | (op
->sb_key
<< REQ1_KEY_KSB_SHIFT
);
189 cr
[1] = op
->src
.u
.dma
.length
- 1;
190 cr
[2] = ccp_addr_lo(&op
->src
.u
.dma
);
191 cr
[3] = (op
->sb_ctx
<< REQ4_KSB_SHIFT
)
192 | (CCP_MEMTYPE_SYSTEM
<< REQ4_MEMTYPE_SHIFT
)
193 | ccp_addr_hi(&op
->src
.u
.dma
);
194 cr
[4] = ccp_addr_lo(&op
->dst
.u
.dma
);
195 cr
[5] = (CCP_MEMTYPE_SYSTEM
<< REQ6_MEMTYPE_SHIFT
)
196 | ccp_addr_hi(&op
->dst
.u
.dma
);
204 return ccp_do_cmd(op
, cr
, ARRAY_SIZE(cr
));
207 static int ccp_perform_sha(struct ccp_op
*op
)
211 /* Fill out the register contents for REQ1 through REQ6 */
212 cr
[0] = (CCP_ENGINE_SHA
<< REQ1_ENGINE_SHIFT
)
213 | (op
->u
.sha
.type
<< REQ1_SHA_TYPE_SHIFT
)
215 cr
[1] = op
->src
.u
.dma
.length
- 1;
216 cr
[2] = ccp_addr_lo(&op
->src
.u
.dma
);
217 cr
[3] = (op
->sb_ctx
<< REQ4_KSB_SHIFT
)
218 | (CCP_MEMTYPE_SYSTEM
<< REQ4_MEMTYPE_SHIFT
)
219 | ccp_addr_hi(&op
->src
.u
.dma
);
223 cr
[4] = lower_32_bits(op
->u
.sha
.msg_bits
);
224 cr
[5] = upper_32_bits(op
->u
.sha
.msg_bits
);
230 return ccp_do_cmd(op
, cr
, ARRAY_SIZE(cr
));
233 static int ccp_perform_rsa(struct ccp_op
*op
)
237 /* Fill out the register contents for REQ1 through REQ6 */
238 cr
[0] = (CCP_ENGINE_RSA
<< REQ1_ENGINE_SHIFT
)
239 | (op
->u
.rsa
.mod_size
<< REQ1_RSA_MOD_SIZE_SHIFT
)
240 | (op
->sb_key
<< REQ1_KEY_KSB_SHIFT
)
242 cr
[1] = op
->u
.rsa
.input_len
- 1;
243 cr
[2] = ccp_addr_lo(&op
->src
.u
.dma
);
244 cr
[3] = (op
->sb_ctx
<< REQ4_KSB_SHIFT
)
245 | (CCP_MEMTYPE_SYSTEM
<< REQ4_MEMTYPE_SHIFT
)
246 | ccp_addr_hi(&op
->src
.u
.dma
);
247 cr
[4] = ccp_addr_lo(&op
->dst
.u
.dma
);
248 cr
[5] = (CCP_MEMTYPE_SYSTEM
<< REQ6_MEMTYPE_SHIFT
)
249 | ccp_addr_hi(&op
->dst
.u
.dma
);
251 return ccp_do_cmd(op
, cr
, ARRAY_SIZE(cr
));
254 static int ccp_perform_passthru(struct ccp_op
*op
)
258 /* Fill out the register contents for REQ1 through REQ6 */
259 cr
[0] = (CCP_ENGINE_PASSTHRU
<< REQ1_ENGINE_SHIFT
)
260 | (op
->u
.passthru
.bit_mod
<< REQ1_PT_BW_SHIFT
)
261 | (op
->u
.passthru
.byte_swap
<< REQ1_PT_BS_SHIFT
);
263 if (op
->src
.type
== CCP_MEMTYPE_SYSTEM
)
264 cr
[1] = op
->src
.u
.dma
.length
- 1;
266 cr
[1] = op
->dst
.u
.dma
.length
- 1;
268 if (op
->src
.type
== CCP_MEMTYPE_SYSTEM
) {
269 cr
[2] = ccp_addr_lo(&op
->src
.u
.dma
);
270 cr
[3] = (CCP_MEMTYPE_SYSTEM
<< REQ4_MEMTYPE_SHIFT
)
271 | ccp_addr_hi(&op
->src
.u
.dma
);
273 if (op
->u
.passthru
.bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
)
274 cr
[3] |= (op
->sb_key
<< REQ4_KSB_SHIFT
);
276 cr
[2] = op
->src
.u
.sb
* CCP_SB_BYTES
;
277 cr
[3] = (CCP_MEMTYPE_SB
<< REQ4_MEMTYPE_SHIFT
);
280 if (op
->dst
.type
== CCP_MEMTYPE_SYSTEM
) {
281 cr
[4] = ccp_addr_lo(&op
->dst
.u
.dma
);
282 cr
[5] = (CCP_MEMTYPE_SYSTEM
<< REQ6_MEMTYPE_SHIFT
)
283 | ccp_addr_hi(&op
->dst
.u
.dma
);
285 cr
[4] = op
->dst
.u
.sb
* CCP_SB_BYTES
;
286 cr
[5] = (CCP_MEMTYPE_SB
<< REQ6_MEMTYPE_SHIFT
);
292 return ccp_do_cmd(op
, cr
, ARRAY_SIZE(cr
));
295 static int ccp_perform_ecc(struct ccp_op
*op
)
299 /* Fill out the register contents for REQ1 through REQ6 */
300 cr
[0] = REQ1_ECC_AFFINE_CONVERT
301 | (CCP_ENGINE_ECC
<< REQ1_ENGINE_SHIFT
)
302 | (op
->u
.ecc
.function
<< REQ1_ECC_FUNCTION_SHIFT
)
304 cr
[1] = op
->src
.u
.dma
.length
- 1;
305 cr
[2] = ccp_addr_lo(&op
->src
.u
.dma
);
306 cr
[3] = (CCP_MEMTYPE_SYSTEM
<< REQ4_MEMTYPE_SHIFT
)
307 | ccp_addr_hi(&op
->src
.u
.dma
);
308 cr
[4] = ccp_addr_lo(&op
->dst
.u
.dma
);
309 cr
[5] = (CCP_MEMTYPE_SYSTEM
<< REQ6_MEMTYPE_SHIFT
)
310 | ccp_addr_hi(&op
->dst
.u
.dma
);
312 return ccp_do_cmd(op
, cr
, ARRAY_SIZE(cr
));
315 static int ccp_init(struct ccp_device
*ccp
)
317 struct device
*dev
= ccp
->dev
;
318 struct ccp_cmd_queue
*cmd_q
;
319 struct dma_pool
*dma_pool
;
320 char dma_pool_name
[MAX_DMAPOOL_NAME_LEN
];
321 unsigned int qmr
, qim
, i
;
324 /* Find available queues */
326 qmr
= ioread32(ccp
->io_regs
+ Q_MASK_REG
);
327 for (i
= 0; i
< MAX_HW_QUEUES
; i
++) {
328 if (!(qmr
& (1 << i
)))
331 /* Allocate a dma pool for this queue */
332 snprintf(dma_pool_name
, sizeof(dma_pool_name
), "%s_q%d",
334 dma_pool
= dma_pool_create(dma_pool_name
, dev
,
335 CCP_DMAPOOL_MAX_SIZE
,
336 CCP_DMAPOOL_ALIGN
, 0);
338 dev_err(dev
, "unable to allocate dma pool\n");
343 cmd_q
= &ccp
->cmd_q
[ccp
->cmd_q_count
];
348 cmd_q
->dma_pool
= dma_pool
;
350 /* Reserve 2 KSB regions for the queue */
351 cmd_q
->sb_key
= KSB_START
+ ccp
->sb_start
++;
352 cmd_q
->sb_ctx
= KSB_START
+ ccp
->sb_start
++;
355 /* Preset some register values and masks that are queue
358 cmd_q
->reg_status
= ccp
->io_regs
+ CMD_Q_STATUS_BASE
+
359 (CMD_Q_STATUS_INCR
* i
);
360 cmd_q
->reg_int_status
= ccp
->io_regs
+ CMD_Q_INT_STATUS_BASE
+
361 (CMD_Q_STATUS_INCR
* i
);
362 cmd_q
->int_ok
= 1 << (i
* 2);
363 cmd_q
->int_err
= 1 << ((i
* 2) + 1);
365 cmd_q
->free_slots
= ccp_get_free_slots(cmd_q
);
367 init_waitqueue_head(&cmd_q
->int_queue
);
369 /* Build queue interrupt mask (two interrupts per queue) */
370 qim
|= cmd_q
->int_ok
| cmd_q
->int_err
;
373 /* For arm64 set the recommended queue cache settings */
374 iowrite32(ccp
->axcache
, ccp
->io_regs
+ CMD_Q_CACHE_BASE
+
375 (CMD_Q_CACHE_INC
* i
));
378 dev_dbg(dev
, "queue #%u available\n", i
);
380 if (ccp
->cmd_q_count
== 0) {
381 dev_notice(dev
, "no command queues available\n");
385 dev_notice(dev
, "%u command queues available\n", ccp
->cmd_q_count
);
387 /* Disable and clear interrupts until ready */
388 iowrite32(0x00, ccp
->io_regs
+ IRQ_MASK_REG
);
389 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
390 cmd_q
= &ccp
->cmd_q
[i
];
392 ioread32(cmd_q
->reg_int_status
);
393 ioread32(cmd_q
->reg_status
);
395 iowrite32(qim
, ccp
->io_regs
+ IRQ_STATUS_REG
);
398 ret
= ccp
->get_irq(ccp
);
400 dev_err(dev
, "unable to allocate an IRQ\n");
404 /* Initialize the queues used to wait for KSB space and suspend */
405 init_waitqueue_head(&ccp
->sb_queue
);
406 init_waitqueue_head(&ccp
->suspend_queue
);
408 dev_dbg(dev
, "Starting threads...\n");
409 /* Create a kthread for each queue */
410 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
411 struct task_struct
*kthread
;
413 cmd_q
= &ccp
->cmd_q
[i
];
415 kthread
= kthread_create(ccp_cmd_queue_thread
, cmd_q
,
416 "%s-q%u", ccp
->name
, cmd_q
->id
);
417 if (IS_ERR(kthread
)) {
418 dev_err(dev
, "error creating queue thread (%ld)\n",
420 ret
= PTR_ERR(kthread
);
424 cmd_q
->kthread
= kthread
;
425 wake_up_process(kthread
);
428 dev_dbg(dev
, "Enabling interrupts...\n");
429 /* Enable interrupts */
430 iowrite32(qim
, ccp
->io_regs
+ IRQ_MASK_REG
);
432 dev_dbg(dev
, "Registering device...\n");
435 ret
= ccp_register_rng(ccp
);
439 /* Register the DMA engine support */
440 ret
= ccp_dmaengine_register(ccp
);
447 ccp_unregister_rng(ccp
);
450 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
451 if (ccp
->cmd_q
[i
].kthread
)
452 kthread_stop(ccp
->cmd_q
[i
].kthread
);
457 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
458 dma_pool_destroy(ccp
->cmd_q
[i
].dma_pool
);
463 static void ccp_destroy(struct ccp_device
*ccp
)
465 struct ccp_cmd_queue
*cmd_q
;
469 /* Unregister the DMA engine */
470 ccp_dmaengine_unregister(ccp
);
472 /* Unregister the RNG */
473 ccp_unregister_rng(ccp
);
475 /* Remove this device from the list of available units */
478 /* Build queue interrupt mask (two interrupt masks per queue) */
480 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
481 cmd_q
= &ccp
->cmd_q
[i
];
482 qim
|= cmd_q
->int_ok
| cmd_q
->int_err
;
485 /* Disable and clear interrupts */
486 iowrite32(0x00, ccp
->io_regs
+ IRQ_MASK_REG
);
487 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
488 cmd_q
= &ccp
->cmd_q
[i
];
490 ioread32(cmd_q
->reg_int_status
);
491 ioread32(cmd_q
->reg_status
);
493 iowrite32(qim
, ccp
->io_regs
+ IRQ_STATUS_REG
);
495 /* Stop the queue kthreads */
496 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
497 if (ccp
->cmd_q
[i
].kthread
)
498 kthread_stop(ccp
->cmd_q
[i
].kthread
);
502 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
503 dma_pool_destroy(ccp
->cmd_q
[i
].dma_pool
);
505 /* Flush the cmd and backlog queue */
506 while (!list_empty(&ccp
->cmd
)) {
507 /* Invoke the callback directly with an error code */
508 cmd
= list_first_entry(&ccp
->cmd
, struct ccp_cmd
, entry
);
509 list_del(&cmd
->entry
);
510 cmd
->callback(cmd
->data
, -ENODEV
);
512 while (!list_empty(&ccp
->backlog
)) {
513 /* Invoke the callback directly with an error code */
514 cmd
= list_first_entry(&ccp
->backlog
, struct ccp_cmd
, entry
);
515 list_del(&cmd
->entry
);
516 cmd
->callback(cmd
->data
, -ENODEV
);
520 static irqreturn_t
ccp_irq_handler(int irq
, void *data
)
522 struct device
*dev
= data
;
523 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
524 struct ccp_cmd_queue
*cmd_q
;
528 status
= ioread32(ccp
->io_regs
+ IRQ_STATUS_REG
);
530 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
531 cmd_q
= &ccp
->cmd_q
[i
];
533 q_int
= status
& (cmd_q
->int_ok
| cmd_q
->int_err
);
535 cmd_q
->int_status
= status
;
536 cmd_q
->q_status
= ioread32(cmd_q
->reg_status
);
537 cmd_q
->q_int_status
= ioread32(cmd_q
->reg_int_status
);
539 /* On error, only save the first error value */
540 if ((q_int
& cmd_q
->int_err
) && !cmd_q
->cmd_error
)
541 cmd_q
->cmd_error
= CMD_Q_ERROR(cmd_q
->q_status
);
545 /* Acknowledge the interrupt and wake the kthread */
546 iowrite32(q_int
, ccp
->io_regs
+ IRQ_STATUS_REG
);
547 wake_up_interruptible(&cmd_q
->int_queue
);
554 static const struct ccp_actions ccp3_actions
= {
555 .aes
= ccp_perform_aes
,
556 .xts_aes
= ccp_perform_xts_aes
,
557 .sha
= ccp_perform_sha
,
558 .rsa
= ccp_perform_rsa
,
559 .passthru
= ccp_perform_passthru
,
560 .ecc
= ccp_perform_ecc
,
561 .sballoc
= ccp_alloc_ksb
,
562 .sbfree
= ccp_free_ksb
,
564 .destroy
= ccp_destroy
,
565 .get_free_slots
= ccp_get_free_slots
,
566 .irqhandler
= ccp_irq_handler
,
569 struct ccp_vdata ccpv3
= {
570 .version
= CCP_VERSION(3, 0),
572 .perform
= &ccp3_actions
,