1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/kthread.h>
16#include <linux/sched.h>
17#include <linux/interrupt.h>
18#include <linux/spinlock.h>
19#include <linux/rwlock_types.h>
20#include <linux/types.h>
21#include <linux/mutex.h>
22#include <linux/delay.h>
23#include <linux/hw_random.h>
24#include <linux/cpu.h>
25#ifdef CONFIG_X86
26#include <asm/cpu_device_id.h>
27#endif
28#include <linux/ccp.h>
29
30#include "ccp-dev.h"
31
32MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
33MODULE_LICENSE("GPL");
34MODULE_VERSION("1.0.0");
35MODULE_DESCRIPTION("AMD Cryptographic Coprocessor driver");
36
37struct ccp_tasklet_data {
38 struct completion completion;
39 struct ccp_cmd *cmd;
40};
41
42
43
44
45
46
47
48
49
50
51
52static DEFINE_RWLOCK(ccp_unit_lock);
53static LIST_HEAD(ccp_units);
54
55
56static DEFINE_SPINLOCK(ccp_rr_lock);
57static struct ccp_device *ccp_rr;
58
59
60static atomic_t ccp_unit_ordinal;
61unsigned int ccp_increment_unit_ordinal(void)
62{
63 return atomic_inc_return(&ccp_unit_ordinal);
64}
65
66
67
68
69
70
71
72
73
74
75
76void ccp_add_device(struct ccp_device *ccp)
77{
78 unsigned long flags;
79
80 write_lock_irqsave(&ccp_unit_lock, flags);
81 list_add_tail(&ccp->entry, &ccp_units);
82 if (!ccp_rr)
83
84
85
86 ccp_rr = ccp;
87 write_unlock_irqrestore(&ccp_unit_lock, flags);
88}
89
90
91
92
93
94
95
96
97
98
99void ccp_del_device(struct ccp_device *ccp)
100{
101 unsigned long flags;
102
103 write_lock_irqsave(&ccp_unit_lock, flags);
104 if (ccp_rr == ccp) {
105
106
107
108
109 if (list_is_last(&ccp_rr->entry, &ccp_units))
110 ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
111 entry);
112 else
113 ccp_rr = list_next_entry(ccp_rr, entry);
114 }
115 list_del(&ccp->entry);
116 if (list_empty(&ccp_units))
117 ccp_rr = NULL;
118 write_unlock_irqrestore(&ccp_unit_lock, flags);
119}
120
121static struct ccp_device *ccp_get_device(void)
122{
123 unsigned long flags;
124 struct ccp_device *dp = NULL;
125
126
127
128
129 read_lock_irqsave(&ccp_unit_lock, flags);
130 if (!list_empty(&ccp_units)) {
131 spin_lock(&ccp_rr_lock);
132 dp = ccp_rr;
133 if (list_is_last(&ccp_rr->entry, &ccp_units))
134 ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
135 entry);
136 else
137 ccp_rr = list_next_entry(ccp_rr, entry);
138 spin_unlock(&ccp_rr_lock);
139 }
140 read_unlock_irqrestore(&ccp_unit_lock, flags);
141
142 return dp;
143}
144
145
146
147
148
149
150int ccp_present(void)
151{
152 unsigned long flags;
153 int ret;
154
155 read_lock_irqsave(&ccp_unit_lock, flags);
156 ret = list_empty(&ccp_units);
157 read_unlock_irqrestore(&ccp_unit_lock, flags);
158
159 return ret ? -ENODEV : 0;
160}
161EXPORT_SYMBOL_GPL(ccp_present);
162
163
164
165
166
167
168
169unsigned int ccp_version(void)
170{
171 struct ccp_device *dp;
172 unsigned long flags;
173 int ret = 0;
174
175 read_lock_irqsave(&ccp_unit_lock, flags);
176 if (!list_empty(&ccp_units)) {
177 dp = list_first_entry(&ccp_units, struct ccp_device, entry);
178 ret = dp->vdata->version;
179 }
180 read_unlock_irqrestore(&ccp_unit_lock, flags);
181
182 return ret;
183}
184EXPORT_SYMBOL_GPL(ccp_version);
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207int ccp_enqueue_cmd(struct ccp_cmd *cmd)
208{
209 struct ccp_device *ccp = ccp_get_device();
210 unsigned long flags;
211 unsigned int i;
212 int ret;
213
214 if (!ccp)
215 return -ENODEV;
216
217
218 if (!cmd->callback)
219 return -EINVAL;
220
221 cmd->ccp = ccp;
222
223 spin_lock_irqsave(&ccp->cmd_lock, flags);
224
225 i = ccp->cmd_q_count;
226
227 if (ccp->cmd_count >= MAX_CMD_QLEN) {
228 ret = -EBUSY;
229 if (cmd->flags & CCP_CMD_MAY_BACKLOG)
230 list_add_tail(&cmd->entry, &ccp->backlog);
231 } else {
232 ret = -EINPROGRESS;
233 ccp->cmd_count++;
234 list_add_tail(&cmd->entry, &ccp->cmd);
235
236
237 if (!ccp->suspending) {
238 for (i = 0; i < ccp->cmd_q_count; i++) {
239 if (ccp->cmd_q[i].active)
240 continue;
241
242 break;
243 }
244 }
245 }
246
247 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
248
249
250 if (i < ccp->cmd_q_count)
251 wake_up_process(ccp->cmd_q[i].kthread);
252
253 return ret;
254}
255EXPORT_SYMBOL_GPL(ccp_enqueue_cmd);
256
257static void ccp_do_cmd_backlog(struct work_struct *work)
258{
259 struct ccp_cmd *cmd = container_of(work, struct ccp_cmd, work);
260 struct ccp_device *ccp = cmd->ccp;
261 unsigned long flags;
262 unsigned int i;
263
264 cmd->callback(cmd->data, -EINPROGRESS);
265
266 spin_lock_irqsave(&ccp->cmd_lock, flags);
267
268 ccp->cmd_count++;
269 list_add_tail(&cmd->entry, &ccp->cmd);
270
271
272 for (i = 0; i < ccp->cmd_q_count; i++) {
273 if (ccp->cmd_q[i].active)
274 continue;
275
276 break;
277 }
278
279 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
280
281
282 if (i < ccp->cmd_q_count)
283 wake_up_process(ccp->cmd_q[i].kthread);
284}
285
286static struct ccp_cmd *ccp_dequeue_cmd(struct ccp_cmd_queue *cmd_q)
287{
288 struct ccp_device *ccp = cmd_q->ccp;
289 struct ccp_cmd *cmd = NULL;
290 struct ccp_cmd *backlog = NULL;
291 unsigned long flags;
292
293 spin_lock_irqsave(&ccp->cmd_lock, flags);
294
295 cmd_q->active = 0;
296
297 if (ccp->suspending) {
298 cmd_q->suspended = 1;
299
300 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
301 wake_up_interruptible(&ccp->suspend_queue);
302
303 return NULL;
304 }
305
306 if (ccp->cmd_count) {
307 cmd_q->active = 1;
308
309 cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
310 list_del(&cmd->entry);
311
312 ccp->cmd_count--;
313 }
314
315 if (!list_empty(&ccp->backlog)) {
316 backlog = list_first_entry(&ccp->backlog, struct ccp_cmd,
317 entry);
318 list_del(&backlog->entry);
319 }
320
321 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
322
323 if (backlog) {
324 INIT_WORK(&backlog->work, ccp_do_cmd_backlog);
325 schedule_work(&backlog->work);
326 }
327
328 return cmd;
329}
330
331static void ccp_do_cmd_complete(unsigned long data)
332{
333 struct ccp_tasklet_data *tdata = (struct ccp_tasklet_data *)data;
334 struct ccp_cmd *cmd = tdata->cmd;
335
336 cmd->callback(cmd->data, cmd->ret);
337 complete(&tdata->completion);
338}
339
340
341
342
343
344
345int ccp_cmd_queue_thread(void *data)
346{
347 struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data;
348 struct ccp_cmd *cmd;
349 struct ccp_tasklet_data tdata;
350 struct tasklet_struct tasklet;
351
352 tasklet_init(&tasklet, ccp_do_cmd_complete, (unsigned long)&tdata);
353
354 set_current_state(TASK_INTERRUPTIBLE);
355 while (!kthread_should_stop()) {
356 schedule();
357
358 set_current_state(TASK_INTERRUPTIBLE);
359
360 cmd = ccp_dequeue_cmd(cmd_q);
361 if (!cmd)
362 continue;
363
364 __set_current_state(TASK_RUNNING);
365
366
367 cmd->ret = ccp_run_cmd(cmd_q, cmd);
368
369
370 tdata.cmd = cmd;
371 init_completion(&tdata.completion);
372 tasklet_schedule(&tasklet);
373 wait_for_completion(&tdata.completion);
374 }
375
376 __set_current_state(TASK_RUNNING);
377
378 return 0;
379}
380
381
382
383
384
385
386struct ccp_device *ccp_alloc_struct(struct device *dev)
387{
388 struct ccp_device *ccp;
389
390 ccp = devm_kzalloc(dev, sizeof(*ccp), GFP_KERNEL);
391 if (!ccp)
392 return NULL;
393 ccp->dev = dev;
394
395 INIT_LIST_HEAD(&ccp->cmd);
396 INIT_LIST_HEAD(&ccp->backlog);
397
398 spin_lock_init(&ccp->cmd_lock);
399 mutex_init(&ccp->req_mutex);
400 mutex_init(&ccp->ksb_mutex);
401 ccp->ksb_count = KSB_COUNT;
402 ccp->ksb_start = 0;
403
404 ccp->ord = ccp_increment_unit_ordinal();
405 snprintf(ccp->name, MAX_CCP_NAME_LEN, "ccp-%u", ccp->ord);
406 snprintf(ccp->rngname, MAX_CCP_NAME_LEN, "ccp-%u-rng", ccp->ord);
407
408 return ccp;
409}
410
411#ifdef CONFIG_PM
412bool ccp_queues_suspended(struct ccp_device *ccp)
413{
414 unsigned int suspended = 0;
415 unsigned long flags;
416 unsigned int i;
417
418 spin_lock_irqsave(&ccp->cmd_lock, flags);
419
420 for (i = 0; i < ccp->cmd_q_count; i++)
421 if (ccp->cmd_q[i].suspended)
422 suspended++;
423
424 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
425
426 return ccp->cmd_q_count == suspended;
427}
428#endif
429
430static int __init ccp_mod_init(void)
431{
432#ifdef CONFIG_X86
433 int ret;
434
435 ret = ccp_pci_init();
436 if (ret)
437 return ret;
438
439
440 if (ccp_present() != 0) {
441 ccp_pci_exit();
442 return -ENODEV;
443 }
444
445 return 0;
446#endif
447
448#ifdef CONFIG_ARM64
449 int ret;
450
451 ret = ccp_platform_init();
452 if (ret)
453 return ret;
454
455
456 if (ccp_present() != 0) {
457 ccp_platform_exit();
458 return -ENODEV;
459 }
460
461 return 0;
462#endif
463
464 return -ENODEV;
465}
466
467static void __exit ccp_mod_exit(void)
468{
469#ifdef CONFIG_X86
470 ccp_pci_exit();
471#endif
472
473#ifdef CONFIG_ARM64
474 ccp_platform_exit();
475#endif
476}
477
478module_init(ccp_mod_init);
479module_exit(ccp_mod_exit);
480