1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Cryptographic Coprocessor (CCP) driver
5 * Copyright (C) 2013,2019 Advanced Micro Devices, Inc.
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * Author: Gary R Hook <gary.hook@amd.com>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/kthread.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/spinlock.h>
17 #include <linux/spinlock_types.h>
18 #include <linux/types.h>
19 #include <linux/mutex.h>
20 #include <linux/delay.h>
21 #include <linux/hw_random.h>
22 #include <linux/cpu.h>
23 #include <linux/atomic.h>
25 #include <asm/cpu_device_id.h>
27 #include <linux/ccp.h>
33 /* Limit CCP use to a specifed number of queues per device */
34 static unsigned int nqueues
= 0;
35 module_param(nqueues
, uint
, 0444);
36 MODULE_PARM_DESC(nqueues
, "Number of queues per CCP (minimum 1; default: all available)");
38 /* Limit the maximum number of configured CCPs */
39 static atomic_t dev_count
= ATOMIC_INIT(0);
40 static unsigned int max_devs
= MAX_CCPS
;
41 module_param(max_devs
, uint
, 0444);
42 MODULE_PARM_DESC(max_devs
, "Maximum number of CCPs to enable (default: all; 0 disables all CCPs)");
44 struct ccp_tasklet_data
{
45 struct completion completion
;
49 /* Human-readable error strings */
50 #define CCP_MAX_ERROR_CODE 64
51 static char *ccp_error_codes
[] = {
55 "ILLEGAL_FUNCTION_TYPE",
56 "ILLEGAL_FUNCTION_MODE",
57 "ILLEGAL_FUNCTION_ENCRYPT",
58 "ILLEGAL_FUNCTION_SIZE",
59 "Zlib_MISSING_INIT_EOM",
60 "ILLEGAL_FUNCTION_RSVD",
61 "ILLEGAL_BUFFER_LENGTH",
68 "Zlib_ILLEGAL_MULTI_QUEUE",
69 "Zlib_ILLEGAL_JOBID_CHANGE",
74 "IDMA1_AXI_SLAVE_FAULT",
80 "ZLIB_UNEXPECTED_EOM",
83 "ZLIB_UNDEFINED_SYMBOL",
84 "ZLIB_UNDEFINED_DISTANCE_S",
85 "ZLIB_CODE_LENGTH_SYMBOL",
86 "ZLIB _VHB_ILLEGAL_FETCH",
87 "ZLIB_UNCOMPRESSED_LEN",
89 "ZLIB_CHECKSUM_MISMATCH0",
97 void ccp_log_error(struct ccp_device
*d
, unsigned int e
)
99 if (WARN_ON(e
>= CCP_MAX_ERROR_CODE
))
102 if (e
< ARRAY_SIZE(ccp_error_codes
))
103 dev_err(d
->dev
, "CCP error %d: %s\n", e
, ccp_error_codes
[e
]);
105 dev_err(d
->dev
, "CCP error %d: Unknown Error\n", e
);
108 /* List of CCPs, CCP count, read-write access lock, and access functions
110 * Lock structure: get ccp_unit_lock for reading whenever we need to
111 * examine the CCP list. While holding it for reading we can acquire
112 * the RR lock to update the round-robin next-CCP pointer. The unit lock
113 * must be acquired before the RR lock.
115 * If the unit-lock is acquired for writing, we have total control over
116 * the list, so there's no value in getting the RR lock.
118 static DEFINE_RWLOCK(ccp_unit_lock
);
119 static LIST_HEAD(ccp_units
);
121 /* Round-robin counter */
122 static DEFINE_SPINLOCK(ccp_rr_lock
);
123 static struct ccp_device
*ccp_rr
;
126 * ccp_add_device - add a CCP device to the list
128 * @ccp: ccp_device struct pointer
130 * Put this CCP on the unit list, which makes it available
133 * Returns zero if a CCP device is present, -ENODEV otherwise.
135 void ccp_add_device(struct ccp_device
*ccp
)
139 write_lock_irqsave(&ccp_unit_lock
, flags
);
140 list_add_tail(&ccp
->entry
, &ccp_units
);
142 /* We already have the list lock (we're first) so this
143 * pointer can't change on us. Set its initial value.
146 write_unlock_irqrestore(&ccp_unit_lock
, flags
);
150 * ccp_del_device - remove a CCP device from the list
152 * @ccp: ccp_device struct pointer
154 * Remove this unit from the list of devices. If the next device
155 * up for use is this one, adjust the pointer. If this is the last
156 * device, NULL the pointer.
158 void ccp_del_device(struct ccp_device
*ccp
)
162 write_lock_irqsave(&ccp_unit_lock
, flags
);
164 /* ccp_unit_lock is read/write; any read access
165 * will be suspended while we make changes to the
166 * list and RR pointer.
168 if (list_is_last(&ccp_rr
->entry
, &ccp_units
))
169 ccp_rr
= list_first_entry(&ccp_units
, struct ccp_device
,
172 ccp_rr
= list_next_entry(ccp_rr
, entry
);
174 list_del(&ccp
->entry
);
175 if (list_empty(&ccp_units
))
177 write_unlock_irqrestore(&ccp_unit_lock
, flags
);
182 int ccp_register_rng(struct ccp_device
*ccp
)
186 dev_dbg(ccp
->dev
, "Registering RNG...\n");
187 /* Register an RNG */
188 ccp
->hwrng
.name
= ccp
->rngname
;
189 ccp
->hwrng
.read
= ccp_trng_read
;
190 ret
= hwrng_register(&ccp
->hwrng
);
192 dev_err(ccp
->dev
, "error registering hwrng (%d)\n", ret
);
197 void ccp_unregister_rng(struct ccp_device
*ccp
)
200 hwrng_unregister(&ccp
->hwrng
);
203 static struct ccp_device
*ccp_get_device(void)
206 struct ccp_device
*dp
= NULL
;
208 /* We round-robin through the unit list.
209 * The (ccp_rr) pointer refers to the next unit to use.
211 read_lock_irqsave(&ccp_unit_lock
, flags
);
212 if (!list_empty(&ccp_units
)) {
213 spin_lock(&ccp_rr_lock
);
215 if (list_is_last(&ccp_rr
->entry
, &ccp_units
))
216 ccp_rr
= list_first_entry(&ccp_units
, struct ccp_device
,
219 ccp_rr
= list_next_entry(ccp_rr
, entry
);
220 spin_unlock(&ccp_rr_lock
);
222 read_unlock_irqrestore(&ccp_unit_lock
, flags
);
228 * ccp_present - check if a CCP device is present
230 * Returns zero if a CCP device is present, -ENODEV otherwise.
232 int ccp_present(void)
237 read_lock_irqsave(&ccp_unit_lock
, flags
);
238 ret
= list_empty(&ccp_units
);
239 read_unlock_irqrestore(&ccp_unit_lock
, flags
);
241 return ret
? -ENODEV
: 0;
243 EXPORT_SYMBOL_GPL(ccp_present
);
246 * ccp_version - get the version of the CCP device
248 * Returns the version from the first unit on the list;
249 * otherwise a zero if no CCP device is present
251 unsigned int ccp_version(void)
253 struct ccp_device
*dp
;
257 read_lock_irqsave(&ccp_unit_lock
, flags
);
258 if (!list_empty(&ccp_units
)) {
259 dp
= list_first_entry(&ccp_units
, struct ccp_device
, entry
);
260 ret
= dp
->vdata
->version
;
262 read_unlock_irqrestore(&ccp_unit_lock
, flags
);
266 EXPORT_SYMBOL_GPL(ccp_version
);
269 * ccp_enqueue_cmd - queue an operation for processing by the CCP
271 * @cmd: ccp_cmd struct to be processed
273 * Queue a cmd to be processed by the CCP. If queueing the cmd
274 * would exceed the defined length of the cmd queue the cmd will
275 * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
276 * result in a return code of -EBUSY.
278 * The callback routine specified in the ccp_cmd struct will be
279 * called to notify the caller of completion (if the cmd was not
280 * backlogged) or advancement out of the backlog. If the cmd has
281 * advanced out of the backlog the "err" value of the callback
282 * will be -EINPROGRESS. Any other "err" value during callback is
283 * the result of the operation.
285 * The cmd has been successfully queued if:
286 * the return code is -EINPROGRESS or
287 * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
289 int ccp_enqueue_cmd(struct ccp_cmd
*cmd
)
291 struct ccp_device
*ccp
;
296 /* Some commands might need to be sent to a specific device */
297 ccp
= cmd
->ccp
? cmd
->ccp
: ccp_get_device();
302 /* Caller must supply a callback routine */
308 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
310 i
= ccp
->cmd_q_count
;
312 if (ccp
->cmd_count
>= MAX_CMD_QLEN
) {
313 if (cmd
->flags
& CCP_CMD_MAY_BACKLOG
) {
315 list_add_tail(&cmd
->entry
, &ccp
->backlog
);
322 list_add_tail(&cmd
->entry
, &ccp
->cmd
);
324 /* Find an idle queue */
325 if (!ccp
->suspending
) {
326 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
327 if (ccp
->cmd_q
[i
].active
)
335 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
337 /* If we found an idle queue, wake it up */
338 if (i
< ccp
->cmd_q_count
)
339 wake_up_process(ccp
->cmd_q
[i
].kthread
);
343 EXPORT_SYMBOL_GPL(ccp_enqueue_cmd
);
345 static void ccp_do_cmd_backlog(struct work_struct
*work
)
347 struct ccp_cmd
*cmd
= container_of(work
, struct ccp_cmd
, work
);
348 struct ccp_device
*ccp
= cmd
->ccp
;
352 cmd
->callback(cmd
->data
, -EINPROGRESS
);
354 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
357 list_add_tail(&cmd
->entry
, &ccp
->cmd
);
359 /* Find an idle queue */
360 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
361 if (ccp
->cmd_q
[i
].active
)
367 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
369 /* If we found an idle queue, wake it up */
370 if (i
< ccp
->cmd_q_count
)
371 wake_up_process(ccp
->cmd_q
[i
].kthread
);
374 static struct ccp_cmd
*ccp_dequeue_cmd(struct ccp_cmd_queue
*cmd_q
)
376 struct ccp_device
*ccp
= cmd_q
->ccp
;
377 struct ccp_cmd
*cmd
= NULL
;
378 struct ccp_cmd
*backlog
= NULL
;
381 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
385 if (ccp
->suspending
) {
386 cmd_q
->suspended
= 1;
388 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
389 wake_up_interruptible(&ccp
->suspend_queue
);
394 if (ccp
->cmd_count
) {
397 cmd
= list_first_entry(&ccp
->cmd
, struct ccp_cmd
, entry
);
398 list_del(&cmd
->entry
);
403 if (!list_empty(&ccp
->backlog
)) {
404 backlog
= list_first_entry(&ccp
->backlog
, struct ccp_cmd
,
406 list_del(&backlog
->entry
);
409 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
412 INIT_WORK(&backlog
->work
, ccp_do_cmd_backlog
);
413 schedule_work(&backlog
->work
);
419 static void ccp_do_cmd_complete(unsigned long data
)
421 struct ccp_tasklet_data
*tdata
= (struct ccp_tasklet_data
*)data
;
422 struct ccp_cmd
*cmd
= tdata
->cmd
;
424 cmd
->callback(cmd
->data
, cmd
->ret
);
426 complete(&tdata
->completion
);
430 * ccp_cmd_queue_thread - create a kernel thread to manage a CCP queue
432 * @data: thread-specific data
434 int ccp_cmd_queue_thread(void *data
)
436 struct ccp_cmd_queue
*cmd_q
= (struct ccp_cmd_queue
*)data
;
438 struct ccp_tasklet_data tdata
;
439 struct tasklet_struct tasklet
;
441 tasklet_init(&tasklet
, ccp_do_cmd_complete
, (unsigned long)&tdata
);
443 set_current_state(TASK_INTERRUPTIBLE
);
444 while (!kthread_should_stop()) {
447 set_current_state(TASK_INTERRUPTIBLE
);
449 cmd
= ccp_dequeue_cmd(cmd_q
);
453 __set_current_state(TASK_RUNNING
);
455 /* Execute the command */
456 cmd
->ret
= ccp_run_cmd(cmd_q
, cmd
);
458 /* Schedule the completion callback */
460 init_completion(&tdata
.completion
);
461 tasklet_schedule(&tasklet
);
462 wait_for_completion(&tdata
.completion
);
465 __set_current_state(TASK_RUNNING
);
471 * ccp_alloc_struct - allocate and initialize the ccp_device struct
473 * @dev: device struct of the CCP
475 struct ccp_device
*ccp_alloc_struct(struct sp_device
*sp
)
477 struct device
*dev
= sp
->dev
;
478 struct ccp_device
*ccp
;
480 ccp
= devm_kzalloc(dev
, sizeof(*ccp
), GFP_KERNEL
);
485 ccp
->axcache
= sp
->axcache
;
487 INIT_LIST_HEAD(&ccp
->cmd
);
488 INIT_LIST_HEAD(&ccp
->backlog
);
490 spin_lock_init(&ccp
->cmd_lock
);
491 mutex_init(&ccp
->req_mutex
);
492 mutex_init(&ccp
->sb_mutex
);
493 ccp
->sb_count
= KSB_COUNT
;
496 /* Initialize the wait queues */
497 init_waitqueue_head(&ccp
->sb_queue
);
498 init_waitqueue_head(&ccp
->suspend_queue
);
500 snprintf(ccp
->name
, MAX_CCP_NAME_LEN
, "ccp-%u", sp
->ord
);
501 snprintf(ccp
->rngname
, MAX_CCP_NAME_LEN
, "ccp-%u-rng", sp
->ord
);
506 int ccp_trng_read(struct hwrng
*rng
, void *data
, size_t max
, bool wait
)
508 struct ccp_device
*ccp
= container_of(rng
, struct ccp_device
, hwrng
);
510 int len
= min_t(int, sizeof(trng_value
), max
);
512 /* Locking is provided by the caller so we can update device
513 * hwrng-related fields safely
515 trng_value
= ioread32(ccp
->io_regs
+ TRNG_OUT_REG
);
517 /* Zero is returned if not data is available or if a
518 * bad-entropy error is present. Assume an error if
519 * we exceed TRNG_RETRIES reads of zero.
521 if (ccp
->hwrng_retries
++ > TRNG_RETRIES
)
527 /* Reset the counter and save the rng value */
528 ccp
->hwrng_retries
= 0;
529 memcpy(data
, &trng_value
, len
);
535 bool ccp_queues_suspended(struct ccp_device
*ccp
)
537 unsigned int suspended
= 0;
541 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
543 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
544 if (ccp
->cmd_q
[i
].suspended
)
547 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
549 return ccp
->cmd_q_count
== suspended
;
552 int ccp_dev_suspend(struct sp_device
*sp
, pm_message_t state
)
554 struct ccp_device
*ccp
= sp
->ccp_data
;
558 /* If there's no device there's nothing to do */
562 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
566 /* Wake all the queue kthreads to prepare for suspend */
567 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
568 wake_up_process(ccp
->cmd_q
[i
].kthread
);
570 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
572 /* Wait for all queue kthreads to say they're done */
573 while (!ccp_queues_suspended(ccp
))
574 wait_event_interruptible(ccp
->suspend_queue
,
575 ccp_queues_suspended(ccp
));
580 int ccp_dev_resume(struct sp_device
*sp
)
582 struct ccp_device
*ccp
= sp
->ccp_data
;
586 /* If there's no device there's nothing to do */
590 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
594 /* Wake up all the kthreads */
595 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
596 ccp
->cmd_q
[i
].suspended
= 0;
597 wake_up_process(ccp
->cmd_q
[i
].kthread
);
600 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
606 int ccp_dev_init(struct sp_device
*sp
)
608 struct device
*dev
= sp
->dev
;
609 struct ccp_device
*ccp
;
613 * Check how many we have so far, and stop after reaching
616 if (atomic_inc_return(&dev_count
) > max_devs
)
617 return 0; /* don't fail the load */
620 ccp
= ccp_alloc_struct(sp
);
625 if (!nqueues
|| (nqueues
> MAX_HW_QUEUES
))
626 ccp
->max_q_count
= MAX_HW_QUEUES
;
628 ccp
->max_q_count
= nqueues
;
630 ccp
->vdata
= (struct ccp_vdata
*)sp
->dev_vdata
->ccp_vdata
;
631 if (!ccp
->vdata
|| !ccp
->vdata
->version
) {
633 dev_err(dev
, "missing driver data\n");
637 ccp
->use_tasklet
= sp
->use_tasklet
;
639 ccp
->io_regs
= sp
->io_map
+ ccp
->vdata
->offset
;
640 if (ccp
->vdata
->setup
)
641 ccp
->vdata
->setup(ccp
);
643 ret
= ccp
->vdata
->perform
->init(ccp
);
645 /* A positive number means that the device cannot be initialized,
646 * but no additional message is required.
651 /* An unexpected problem occurred, and should be reported in the log */
655 dev_notice(dev
, "ccp enabled\n");
660 dev_notice(dev
, "ccp initialization failed\n");
668 void ccp_dev_destroy(struct sp_device
*sp
)
670 struct ccp_device
*ccp
= sp
->ccp_data
;
675 ccp
->vdata
->perform
->destroy(ccp
);