blob: 6b44730ef9d6903aabf946ef8cad7e57b17dd231 [file] [log] [blame]
Tom Lendacky63b94502013-11-12 11:46:16 -06001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
Gary R Hook3f19ce22016-03-01 13:48:54 -06004 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
Tom Lendacky63b94502013-11-12 11:46:16 -06005 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
Gary R Hook956ee212016-07-26 19:09:40 -05007 * Author: Gary R Hook <gary.hook@amd.com>
Tom Lendacky63b94502013-11-12 11:46:16 -06008 *
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.
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/kthread.h>
17#include <linux/sched.h>
18#include <linux/interrupt.h>
19#include <linux/spinlock.h>
Mike Galbraith7587c402016-04-05 15:03:21 +020020#include <linux/spinlock_types.h>
Gary R Hook553d2372016-03-01 13:49:04 -060021#include <linux/types.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060022#include <linux/mutex.h>
23#include <linux/delay.h>
24#include <linux/hw_random.h>
25#include <linux/cpu.h>
Tom Lendackyc4f4b322014-06-05 10:17:57 -050026#ifdef CONFIG_X86
Tom Lendacky63b94502013-11-12 11:46:16 -060027#include <asm/cpu_device_id.h>
Tom Lendackyc4f4b322014-06-05 10:17:57 -050028#endif
Tom Lendacky63b94502013-11-12 11:46:16 -060029#include <linux/ccp.h>
30
31#include "ccp-dev.h"
32
33MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
34MODULE_LICENSE("GPL");
35MODULE_VERSION("1.0.0");
36MODULE_DESCRIPTION("AMD Cryptographic Coprocessor driver");
37
Tom Lendacky530abd82014-01-24 16:18:14 -060038struct ccp_tasklet_data {
39 struct completion completion;
40 struct ccp_cmd *cmd;
41};
42
Gary R Hook553d2372016-03-01 13:49:04 -060043/* List of CCPs, CCP count, read-write access lock, and access functions
44 *
45 * Lock structure: get ccp_unit_lock for reading whenever we need to
46 * examine the CCP list. While holding it for reading we can acquire
47 * the RR lock to update the round-robin next-CCP pointer. The unit lock
48 * must be acquired before the RR lock.
49 *
50 * If the unit-lock is acquired for writing, we have total control over
51 * the list, so there's no value in getting the RR lock.
52 */
53static DEFINE_RWLOCK(ccp_unit_lock);
54static LIST_HEAD(ccp_units);
55
56/* Round-robin counter */
Gary R Hook03a6f292016-03-16 09:02:26 -050057static DEFINE_SPINLOCK(ccp_rr_lock);
Gary R Hook553d2372016-03-01 13:49:04 -060058static struct ccp_device *ccp_rr;
59
60/* Ever-increasing value to produce unique unit numbers */
61static atomic_t ccp_unit_ordinal;
62unsigned int ccp_increment_unit_ordinal(void)
Tom Lendacky63b94502013-11-12 11:46:16 -060063{
Gary R Hook553d2372016-03-01 13:49:04 -060064 return atomic_inc_return(&ccp_unit_ordinal);
Tom Lendacky63b94502013-11-12 11:46:16 -060065}
66
Gary R Hookea0375a2016-03-01 13:49:25 -060067/**
68 * ccp_add_device - add a CCP device to the list
69 *
70 * @ccp: ccp_device struct pointer
71 *
Gary R Hook553d2372016-03-01 13:49:04 -060072 * Put this CCP on the unit list, which makes it available
73 * for use.
Gary R Hookea0375a2016-03-01 13:49:25 -060074 *
75 * Returns zero if a CCP device is present, -ENODEV otherwise.
Gary R Hook553d2372016-03-01 13:49:04 -060076 */
Gary R Hookea0375a2016-03-01 13:49:25 -060077void ccp_add_device(struct ccp_device *ccp)
Tom Lendacky63b94502013-11-12 11:46:16 -060078{
Gary R Hook553d2372016-03-01 13:49:04 -060079 unsigned long flags;
80
81 write_lock_irqsave(&ccp_unit_lock, flags);
82 list_add_tail(&ccp->entry, &ccp_units);
83 if (!ccp_rr)
84 /* We already have the list lock (we're first) so this
85 * pointer can't change on us. Set its initial value.
86 */
87 ccp_rr = ccp;
88 write_unlock_irqrestore(&ccp_unit_lock, flags);
Tom Lendacky63b94502013-11-12 11:46:16 -060089}
90
Gary R Hookea0375a2016-03-01 13:49:25 -060091/**
92 * ccp_del_device - remove a CCP device from the list
93 *
94 * @ccp: ccp_device struct pointer
95 *
96 * Remove this unit from the list of devices. If the next device
Gary R Hook553d2372016-03-01 13:49:04 -060097 * up for use is this one, adjust the pointer. If this is the last
98 * device, NULL the pointer.
99 */
Gary R Hookea0375a2016-03-01 13:49:25 -0600100void ccp_del_device(struct ccp_device *ccp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600101{
Gary R Hook553d2372016-03-01 13:49:04 -0600102 unsigned long flags;
103
104 write_lock_irqsave(&ccp_unit_lock, flags);
105 if (ccp_rr == ccp) {
106 /* ccp_unit_lock is read/write; any read access
107 * will be suspended while we make changes to the
108 * list and RR pointer.
109 */
110 if (list_is_last(&ccp_rr->entry, &ccp_units))
111 ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
112 entry);
113 else
114 ccp_rr = list_next_entry(ccp_rr, entry);
115 }
116 list_del(&ccp->entry);
117 if (list_empty(&ccp_units))
118 ccp_rr = NULL;
119 write_unlock_irqrestore(&ccp_unit_lock, flags);
120}
121
122static struct ccp_device *ccp_get_device(void)
123{
124 unsigned long flags;
125 struct ccp_device *dp = NULL;
126
127 /* We round-robin through the unit list.
128 * The (ccp_rr) pointer refers to the next unit to use.
129 */
130 read_lock_irqsave(&ccp_unit_lock, flags);
131 if (!list_empty(&ccp_units)) {
Gary R Hook03a6f292016-03-16 09:02:26 -0500132 spin_lock(&ccp_rr_lock);
Gary R Hook553d2372016-03-01 13:49:04 -0600133 dp = ccp_rr;
134 if (list_is_last(&ccp_rr->entry, &ccp_units))
135 ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
136 entry);
137 else
138 ccp_rr = list_next_entry(ccp_rr, entry);
Gary R Hook03a6f292016-03-16 09:02:26 -0500139 spin_unlock(&ccp_rr_lock);
Gary R Hook553d2372016-03-01 13:49:04 -0600140 }
141 read_unlock_irqrestore(&ccp_unit_lock, flags);
142
143 return dp;
Tom Lendacky63b94502013-11-12 11:46:16 -0600144}
145
146/**
Tom Lendackyc9f21cb2014-09-05 10:31:09 -0500147 * ccp_present - check if a CCP device is present
148 *
149 * Returns zero if a CCP device is present, -ENODEV otherwise.
150 */
151int ccp_present(void)
152{
Gary R Hook553d2372016-03-01 13:49:04 -0600153 unsigned long flags;
154 int ret;
Tom Lendackyc9f21cb2014-09-05 10:31:09 -0500155
Gary R Hook553d2372016-03-01 13:49:04 -0600156 read_lock_irqsave(&ccp_unit_lock, flags);
157 ret = list_empty(&ccp_units);
158 read_unlock_irqrestore(&ccp_unit_lock, flags);
159
160 return ret ? -ENODEV : 0;
Tom Lendackyc9f21cb2014-09-05 10:31:09 -0500161}
162EXPORT_SYMBOL_GPL(ccp_present);
163
164/**
Gary R Hookc7019c42016-03-01 13:49:15 -0600165 * ccp_version - get the version of the CCP device
166 *
167 * Returns the version from the first unit on the list;
168 * otherwise a zero if no CCP device is present
169 */
170unsigned int ccp_version(void)
171{
172 struct ccp_device *dp;
173 unsigned long flags;
174 int ret = 0;
175
176 read_lock_irqsave(&ccp_unit_lock, flags);
177 if (!list_empty(&ccp_units)) {
178 dp = list_first_entry(&ccp_units, struct ccp_device, entry);
179 ret = dp->vdata->version;
180 }
181 read_unlock_irqrestore(&ccp_unit_lock, flags);
182
183 return ret;
184}
185EXPORT_SYMBOL_GPL(ccp_version);
186
187/**
Tom Lendacky63b94502013-11-12 11:46:16 -0600188 * ccp_enqueue_cmd - queue an operation for processing by the CCP
189 *
190 * @cmd: ccp_cmd struct to be processed
191 *
192 * Queue a cmd to be processed by the CCP. If queueing the cmd
193 * would exceed the defined length of the cmd queue the cmd will
194 * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
195 * result in a return code of -EBUSY.
196 *
197 * The callback routine specified in the ccp_cmd struct will be
198 * called to notify the caller of completion (if the cmd was not
199 * backlogged) or advancement out of the backlog. If the cmd has
200 * advanced out of the backlog the "err" value of the callback
201 * will be -EINPROGRESS. Any other "err" value during callback is
202 * the result of the operation.
203 *
204 * The cmd has been successfully queued if:
205 * the return code is -EINPROGRESS or
206 * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
207 */
208int ccp_enqueue_cmd(struct ccp_cmd *cmd)
209{
210 struct ccp_device *ccp = ccp_get_device();
211 unsigned long flags;
212 unsigned int i;
213 int ret;
214
215 if (!ccp)
216 return -ENODEV;
217
218 /* Caller must supply a callback routine */
219 if (!cmd->callback)
220 return -EINVAL;
221
222 cmd->ccp = ccp;
223
224 spin_lock_irqsave(&ccp->cmd_lock, flags);
225
226 i = ccp->cmd_q_count;
227
228 if (ccp->cmd_count >= MAX_CMD_QLEN) {
229 ret = -EBUSY;
230 if (cmd->flags & CCP_CMD_MAY_BACKLOG)
231 list_add_tail(&cmd->entry, &ccp->backlog);
232 } else {
233 ret = -EINPROGRESS;
234 ccp->cmd_count++;
235 list_add_tail(&cmd->entry, &ccp->cmd);
236
237 /* Find an idle queue */
238 if (!ccp->suspending) {
239 for (i = 0; i < ccp->cmd_q_count; i++) {
240 if (ccp->cmd_q[i].active)
241 continue;
242
243 break;
244 }
245 }
246 }
247
248 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
249
250 /* If we found an idle queue, wake it up */
251 if (i < ccp->cmd_q_count)
252 wake_up_process(ccp->cmd_q[i].kthread);
253
254 return ret;
255}
256EXPORT_SYMBOL_GPL(ccp_enqueue_cmd);
257
258static void ccp_do_cmd_backlog(struct work_struct *work)
259{
260 struct ccp_cmd *cmd = container_of(work, struct ccp_cmd, work);
261 struct ccp_device *ccp = cmd->ccp;
262 unsigned long flags;
263 unsigned int i;
264
265 cmd->callback(cmd->data, -EINPROGRESS);
266
267 spin_lock_irqsave(&ccp->cmd_lock, flags);
268
269 ccp->cmd_count++;
270 list_add_tail(&cmd->entry, &ccp->cmd);
271
272 /* Find an idle queue */
273 for (i = 0; i < ccp->cmd_q_count; i++) {
274 if (ccp->cmd_q[i].active)
275 continue;
276
277 break;
278 }
279
280 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
281
282 /* If we found an idle queue, wake it up */
283 if (i < ccp->cmd_q_count)
284 wake_up_process(ccp->cmd_q[i].kthread);
285}
286
287static struct ccp_cmd *ccp_dequeue_cmd(struct ccp_cmd_queue *cmd_q)
288{
289 struct ccp_device *ccp = cmd_q->ccp;
290 struct ccp_cmd *cmd = NULL;
291 struct ccp_cmd *backlog = NULL;
292 unsigned long flags;
293
294 spin_lock_irqsave(&ccp->cmd_lock, flags);
295
296 cmd_q->active = 0;
297
298 if (ccp->suspending) {
299 cmd_q->suspended = 1;
300
301 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
302 wake_up_interruptible(&ccp->suspend_queue);
303
304 return NULL;
305 }
306
307 if (ccp->cmd_count) {
308 cmd_q->active = 1;
309
310 cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
311 list_del(&cmd->entry);
312
313 ccp->cmd_count--;
314 }
315
316 if (!list_empty(&ccp->backlog)) {
317 backlog = list_first_entry(&ccp->backlog, struct ccp_cmd,
318 entry);
319 list_del(&backlog->entry);
320 }
321
322 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
323
324 if (backlog) {
325 INIT_WORK(&backlog->work, ccp_do_cmd_backlog);
326 schedule_work(&backlog->work);
327 }
328
329 return cmd;
330}
331
Tom Lendacky530abd82014-01-24 16:18:14 -0600332static void ccp_do_cmd_complete(unsigned long data)
Tom Lendacky63b94502013-11-12 11:46:16 -0600333{
Tom Lendacky530abd82014-01-24 16:18:14 -0600334 struct ccp_tasklet_data *tdata = (struct ccp_tasklet_data *)data;
335 struct ccp_cmd *cmd = tdata->cmd;
Tom Lendacky63b94502013-11-12 11:46:16 -0600336
337 cmd->callback(cmd->data, cmd->ret);
Tom Lendacky530abd82014-01-24 16:18:14 -0600338 complete(&tdata->completion);
Tom Lendacky63b94502013-11-12 11:46:16 -0600339}
340
Gary R Hookea0375a2016-03-01 13:49:25 -0600341/**
342 * ccp_cmd_queue_thread - create a kernel thread to manage a CCP queue
343 *
344 * @data: thread-specific data
345 */
346int ccp_cmd_queue_thread(void *data)
Tom Lendacky63b94502013-11-12 11:46:16 -0600347{
348 struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data;
349 struct ccp_cmd *cmd;
Tom Lendacky530abd82014-01-24 16:18:14 -0600350 struct ccp_tasklet_data tdata;
351 struct tasklet_struct tasklet;
352
353 tasklet_init(&tasklet, ccp_do_cmd_complete, (unsigned long)&tdata);
Tom Lendacky63b94502013-11-12 11:46:16 -0600354
355 set_current_state(TASK_INTERRUPTIBLE);
356 while (!kthread_should_stop()) {
357 schedule();
358
359 set_current_state(TASK_INTERRUPTIBLE);
360
361 cmd = ccp_dequeue_cmd(cmd_q);
362 if (!cmd)
363 continue;
364
365 __set_current_state(TASK_RUNNING);
366
367 /* Execute the command */
368 cmd->ret = ccp_run_cmd(cmd_q, cmd);
369
370 /* Schedule the completion callback */
Tom Lendacky530abd82014-01-24 16:18:14 -0600371 tdata.cmd = cmd;
372 init_completion(&tdata.completion);
373 tasklet_schedule(&tasklet);
374 wait_for_completion(&tdata.completion);
Tom Lendacky63b94502013-11-12 11:46:16 -0600375 }
376
377 __set_current_state(TASK_RUNNING);
378
379 return 0;
380}
381
Tom Lendacky63b94502013-11-12 11:46:16 -0600382/**
383 * ccp_alloc_struct - allocate and initialize the ccp_device struct
384 *
385 * @dev: device struct of the CCP
386 */
387struct ccp_device *ccp_alloc_struct(struct device *dev)
388{
389 struct ccp_device *ccp;
390
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600391 ccp = devm_kzalloc(dev, sizeof(*ccp), GFP_KERNEL);
Tom Lendacky8db88462015-02-03 13:07:05 -0600392 if (!ccp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600393 return NULL;
Tom Lendacky63b94502013-11-12 11:46:16 -0600394 ccp->dev = dev;
395
396 INIT_LIST_HEAD(&ccp->cmd);
397 INIT_LIST_HEAD(&ccp->backlog);
398
399 spin_lock_init(&ccp->cmd_lock);
400 mutex_init(&ccp->req_mutex);
Gary R Hook956ee212016-07-26 19:09:40 -0500401 mutex_init(&ccp->sb_mutex);
402 ccp->sb_count = KSB_COUNT;
403 ccp->sb_start = 0;
Tom Lendacky63b94502013-11-12 11:46:16 -0600404
Gary R Hook553d2372016-03-01 13:49:04 -0600405 ccp->ord = ccp_increment_unit_ordinal();
406 snprintf(ccp->name, MAX_CCP_NAME_LEN, "ccp-%u", ccp->ord);
407 snprintf(ccp->rngname, MAX_CCP_NAME_LEN, "ccp-%u-rng", ccp->ord);
408
Tom Lendacky63b94502013-11-12 11:46:16 -0600409 return ccp;
410}
411
Gary R Hook8256e682016-07-26 19:10:02 -0500412int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
413{
414 struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng);
415 u32 trng_value;
416 int len = min_t(int, sizeof(trng_value), max);
417
418 /* Locking is provided by the caller so we can update device
419 * hwrng-related fields safely
420 */
421 trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG);
422 if (!trng_value) {
423 /* Zero is returned if not data is available or if a
424 * bad-entropy error is present. Assume an error if
425 * we exceed TRNG_RETRIES reads of zero.
426 */
427 if (ccp->hwrng_retries++ > TRNG_RETRIES)
428 return -EIO;
429
430 return 0;
431 }
432
433 /* Reset the counter and save the rng value */
434 ccp->hwrng_retries = 0;
435 memcpy(data, &trng_value, len);
436
437 return len;
438}
439
Tom Lendacky63b94502013-11-12 11:46:16 -0600440#ifdef CONFIG_PM
441bool ccp_queues_suspended(struct ccp_device *ccp)
442{
443 unsigned int suspended = 0;
444 unsigned long flags;
445 unsigned int i;
446
447 spin_lock_irqsave(&ccp->cmd_lock, flags);
448
449 for (i = 0; i < ccp->cmd_q_count; i++)
450 if (ccp->cmd_q[i].suspended)
451 suspended++;
452
453 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
454
455 return ccp->cmd_q_count == suspended;
456}
457#endif
458
Tom Lendacky63b94502013-11-12 11:46:16 -0600459static int __init ccp_mod_init(void)
460{
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500461#ifdef CONFIG_X86
Tom Lendackydb34cf92014-01-06 13:34:29 -0600462 int ret;
Tom Lendacky63b94502013-11-12 11:46:16 -0600463
Gary R Hook3f19ce22016-03-01 13:48:54 -0600464 ret = ccp_pci_init();
465 if (ret)
466 return ret;
467
468 /* Don't leave the driver loaded if init failed */
Gary R Hook553d2372016-03-01 13:49:04 -0600469 if (ccp_present() != 0) {
Gary R Hook3f19ce22016-03-01 13:48:54 -0600470 ccp_pci_exit();
Tom Lendacky63b94502013-11-12 11:46:16 -0600471 return -ENODEV;
Fengguang Wud1dd2062013-12-09 20:08:19 +0800472 }
Gary R Hook3f19ce22016-03-01 13:48:54 -0600473
474 return 0;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500475#endif
476
477#ifdef CONFIG_ARM64
478 int ret;
479
480 ret = ccp_platform_init();
481 if (ret)
482 return ret;
483
484 /* Don't leave the driver loaded if init failed */
Gary R Hook553d2372016-03-01 13:49:04 -0600485 if (ccp_present() != 0) {
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500486 ccp_platform_exit();
487 return -ENODEV;
488 }
489
490 return 0;
491#endif
Tom Lendacky63b94502013-11-12 11:46:16 -0600492
493 return -ENODEV;
494}
495
496static void __exit ccp_mod_exit(void)
497{
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500498#ifdef CONFIG_X86
Gary R Hook3f19ce22016-03-01 13:48:54 -0600499 ccp_pci_exit();
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500500#endif
501
502#ifdef CONFIG_ARM64
503 ccp_platform_exit();
504#endif
Tom Lendacky63b94502013-11-12 11:46:16 -0600505}
506
507module_init(ccp_mod_init);
508module_exit(ccp_mod_exit);