1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/poll.h>
19#include <linux/slab.h>
20#include <linux/mutex.h>
21#include <linux/spinlock.h>
22#include <linux/freezer.h>
23#include <linux/major.h>
24#include <linux/tpm_eventlog.h>
25#include <linux/hw_random.h>
26#include "tpm.h"
27
28DEFINE_IDR(dev_nums_idr);
29static DEFINE_MUTEX(idr_lock);
30
31struct class *tpm_class;
32struct class *tpmrm_class;
33dev_t tpm_devt;
34
35static int tpm_request_locality(struct tpm_chip *chip)
36{
37 int rc;
38
39 if (!chip->ops->request_locality)
40 return 0;
41
42 rc = chip->ops->request_locality(chip, 0);
43 if (rc < 0)
44 return rc;
45
46 chip->locality = rc;
47 return 0;
48}
49
50static void tpm_relinquish_locality(struct tpm_chip *chip)
51{
52 int rc;
53
54 if (!chip->ops->relinquish_locality)
55 return;
56
57 rc = chip->ops->relinquish_locality(chip, chip->locality);
58 if (rc)
59 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
60
61 chip->locality = -1;
62}
63
64static int tpm_cmd_ready(struct tpm_chip *chip)
65{
66 if (!chip->ops->cmd_ready)
67 return 0;
68
69 return chip->ops->cmd_ready(chip);
70}
71
72static int tpm_go_idle(struct tpm_chip *chip)
73{
74 if (!chip->ops->go_idle)
75 return 0;
76
77 return chip->ops->go_idle(chip);
78}
79
80static void tpm_clk_enable(struct tpm_chip *chip)
81{
82 if (chip->ops->clk_enable)
83 chip->ops->clk_enable(chip, true);
84}
85
86static void tpm_clk_disable(struct tpm_chip *chip)
87{
88 if (chip->ops->clk_enable)
89 chip->ops->clk_enable(chip, false);
90}
91
92
93
94
95
96
97
98
99
100int tpm_chip_start(struct tpm_chip *chip)
101{
102 int ret;
103
104 tpm_clk_enable(chip);
105
106 if (chip->locality == -1) {
107 ret = tpm_request_locality(chip);
108 if (ret) {
109 tpm_clk_disable(chip);
110 return ret;
111 }
112 }
113
114 ret = tpm_cmd_ready(chip);
115 if (ret) {
116 tpm_relinquish_locality(chip);
117 tpm_clk_disable(chip);
118 return ret;
119 }
120
121 return 0;
122}
123EXPORT_SYMBOL_GPL(tpm_chip_start);
124
125
126
127
128
129
130
131
132
133void tpm_chip_stop(struct tpm_chip *chip)
134{
135 tpm_go_idle(chip);
136 tpm_relinquish_locality(chip);
137 tpm_clk_disable(chip);
138}
139EXPORT_SYMBOL_GPL(tpm_chip_stop);
140
141
142
143
144
145
146
147
148
149
150
151
152int tpm_try_get_ops(struct tpm_chip *chip)
153{
154 int rc = -EIO;
155
156 get_device(&chip->dev);
157
158 down_read(&chip->ops_sem);
159 if (!chip->ops)
160 goto out_ops;
161
162 mutex_lock(&chip->tpm_mutex);
163 rc = tpm_chip_start(chip);
164 if (rc)
165 goto out_lock;
166
167 return 0;
168out_lock:
169 mutex_unlock(&chip->tpm_mutex);
170out_ops:
171 up_read(&chip->ops_sem);
172 put_device(&chip->dev);
173 return rc;
174}
175EXPORT_SYMBOL_GPL(tpm_try_get_ops);
176
177
178
179
180
181
182
183
184void tpm_put_ops(struct tpm_chip *chip)
185{
186 tpm_chip_stop(chip);
187 mutex_unlock(&chip->tpm_mutex);
188 up_read(&chip->ops_sem);
189 put_device(&chip->dev);
190}
191EXPORT_SYMBOL_GPL(tpm_put_ops);
192
193
194
195
196struct tpm_chip *tpm_default_chip(void)
197{
198 struct tpm_chip *chip, *res = NULL;
199 int chip_num = 0;
200 int chip_prev;
201
202 mutex_lock(&idr_lock);
203
204 do {
205 chip_prev = chip_num;
206 chip = idr_get_next(&dev_nums_idr, &chip_num);
207 if (chip) {
208 get_device(&chip->dev);
209 res = chip;
210 break;
211 }
212 } while (chip_prev != chip_num);
213
214 mutex_unlock(&idr_lock);
215
216 return res;
217}
218EXPORT_SYMBOL_GPL(tpm_default_chip);
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
236{
237 int rc;
238
239 if (chip) {
240 if (!tpm_try_get_ops(chip))
241 return chip;
242 return NULL;
243 }
244
245 chip = tpm_default_chip();
246 if (!chip)
247 return NULL;
248 rc = tpm_try_get_ops(chip);
249
250 put_device(&chip->dev);
251 if (rc)
252 return NULL;
253 return chip;
254}
255
256
257
258
259
260
261
262static void tpm_dev_release(struct device *dev)
263{
264 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
265
266 mutex_lock(&idr_lock);
267 idr_remove(&dev_nums_idr, chip->dev_num);
268 mutex_unlock(&idr_lock);
269
270 kfree(chip->log.bios_event_log);
271 kfree(chip->work_space.context_buf);
272 kfree(chip->work_space.session_buf);
273 kfree(chip->allocated_banks);
274 kfree(chip);
275}
276
277static void tpm_devs_release(struct device *dev)
278{
279 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
280
281
282 put_device(&chip->dev);
283}
284
285
286
287
288
289
290
291
292
293
294static int tpm_class_shutdown(struct device *dev)
295{
296 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
297
298 down_write(&chip->ops_sem);
299 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
300 if (!tpm_chip_start(chip)) {
301 tpm2_shutdown(chip, TPM2_SU_CLEAR);
302 tpm_chip_stop(chip);
303 }
304 }
305 chip->ops = NULL;
306 up_write(&chip->ops_sem);
307
308 return 0;
309}
310
311
312
313
314
315
316
317
318
319
320
321struct tpm_chip *tpm_chip_alloc(struct device *pdev,
322 const struct tpm_class_ops *ops)
323{
324 struct tpm_chip *chip;
325 int rc;
326
327 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
328 if (chip == NULL)
329 return ERR_PTR(-ENOMEM);
330
331 mutex_init(&chip->tpm_mutex);
332 init_rwsem(&chip->ops_sem);
333
334 chip->ops = ops;
335
336 mutex_lock(&idr_lock);
337 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
338 mutex_unlock(&idr_lock);
339 if (rc < 0) {
340 dev_err(pdev, "No available tpm device numbers\n");
341 kfree(chip);
342 return ERR_PTR(rc);
343 }
344 chip->dev_num = rc;
345
346 device_initialize(&chip->dev);
347 device_initialize(&chip->devs);
348
349 chip->dev.class = tpm_class;
350 chip->dev.class->shutdown_pre = tpm_class_shutdown;
351 chip->dev.release = tpm_dev_release;
352 chip->dev.parent = pdev;
353 chip->dev.groups = chip->groups;
354
355 chip->devs.parent = pdev;
356 chip->devs.class = tpmrm_class;
357 chip->devs.release = tpm_devs_release;
358
359
360
361
362
363 if (chip->flags & TPM_CHIP_FLAG_TPM2)
364 get_device(&chip->dev);
365
366 if (chip->dev_num == 0)
367 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
368 else
369 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
370
371 chip->devs.devt =
372 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
373
374 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
375 if (rc)
376 goto out;
377 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
378 if (rc)
379 goto out;
380
381 if (!pdev)
382 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
383
384 cdev_init(&chip->cdev, &tpm_fops);
385 cdev_init(&chip->cdevs, &tpmrm_fops);
386 chip->cdev.owner = THIS_MODULE;
387 chip->cdevs.owner = THIS_MODULE;
388
389 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
390 if (rc) {
391 rc = -ENOMEM;
392 goto out;
393 }
394
395 chip->locality = -1;
396 return chip;
397
398out:
399 put_device(&chip->devs);
400 put_device(&chip->dev);
401 return ERR_PTR(rc);
402}
403EXPORT_SYMBOL_GPL(tpm_chip_alloc);
404
405
406
407
408
409
410
411
412struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
413 const struct tpm_class_ops *ops)
414{
415 struct tpm_chip *chip;
416 int rc;
417
418 chip = tpm_chip_alloc(pdev, ops);
419 if (IS_ERR(chip))
420 return chip;
421
422 rc = devm_add_action_or_reset(pdev,
423 (void (*)(void *)) put_device,
424 &chip->dev);
425 if (rc)
426 return ERR_PTR(rc);
427
428 dev_set_drvdata(pdev, chip);
429
430 return chip;
431}
432EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
433
434static int tpm_add_char_device(struct tpm_chip *chip)
435{
436 int rc;
437
438 rc = cdev_device_add(&chip->cdev, &chip->dev);
439 if (rc) {
440 dev_err(&chip->dev,
441 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
442 dev_name(&chip->dev), MAJOR(chip->dev.devt),
443 MINOR(chip->dev.devt), rc);
444 return rc;
445 }
446
447 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
448 rc = cdev_device_add(&chip->cdevs, &chip->devs);
449 if (rc) {
450 dev_err(&chip->devs,
451 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
452 dev_name(&chip->devs), MAJOR(chip->devs.devt),
453 MINOR(chip->devs.devt), rc);
454 return rc;
455 }
456 }
457
458
459 mutex_lock(&idr_lock);
460 idr_replace(&dev_nums_idr, chip, chip->dev_num);
461 mutex_unlock(&idr_lock);
462
463 return rc;
464}
465
466static void tpm_del_char_device(struct tpm_chip *chip)
467{
468 cdev_device_del(&chip->cdev, &chip->dev);
469
470
471 mutex_lock(&idr_lock);
472 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
473 mutex_unlock(&idr_lock);
474
475
476 down_write(&chip->ops_sem);
477 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
478 if (!tpm_chip_start(chip)) {
479 tpm2_shutdown(chip, TPM2_SU_CLEAR);
480 tpm_chip_stop(chip);
481 }
482 }
483 chip->ops = NULL;
484 up_write(&chip->ops_sem);
485}
486
487static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
488{
489 struct attribute **i;
490
491 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
492 return;
493
494 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
495
496 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
497 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
498}
499
500
501
502
503
504static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
505{
506 struct attribute **i;
507 int rc;
508
509 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
510 return 0;
511
512 rc = compat_only_sysfs_link_entry_to_kobj(
513 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL);
514 if (rc && rc != -ENOENT)
515 return rc;
516
517
518 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
519 rc = compat_only_sysfs_link_entry_to_kobj(
520 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL);
521 if (rc) {
522 tpm_del_legacy_sysfs(chip);
523 return rc;
524 }
525 }
526
527 return 0;
528}
529
530static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
531{
532 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
533
534 return tpm_get_random(chip, data, max);
535}
536
537static int tpm_add_hwrng(struct tpm_chip *chip)
538{
539 if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
540 return 0;
541
542 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
543 "tpm-rng-%d", chip->dev_num);
544 chip->hwrng.name = chip->hwrng_name;
545 chip->hwrng.read = tpm_hwrng_read;
546 return hwrng_register(&chip->hwrng);
547}
548
549static int tpm_get_pcr_allocation(struct tpm_chip *chip)
550{
551 int rc;
552
553 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
554 tpm2_get_pcr_allocation(chip) :
555 tpm1_get_pcr_allocation(chip);
556
557 if (rc > 0)
558 return -ENODEV;
559
560 return rc;
561}
562
563
564
565
566
567
568
569
570
571
572
573
574int tpm_chip_register(struct tpm_chip *chip)
575{
576 int rc;
577
578 rc = tpm_chip_start(chip);
579 if (rc)
580 return rc;
581 rc = tpm_auto_startup(chip);
582 if (rc) {
583 tpm_chip_stop(chip);
584 return rc;
585 }
586
587 rc = tpm_get_pcr_allocation(chip);
588 tpm_chip_stop(chip);
589 if (rc)
590 return rc;
591
592 tpm_sysfs_add_device(chip);
593
594 tpm_bios_log_setup(chip);
595
596 tpm_add_ppi(chip);
597
598 rc = tpm_add_hwrng(chip);
599 if (rc)
600 goto out_ppi;
601
602 rc = tpm_add_char_device(chip);
603 if (rc)
604 goto out_hwrng;
605
606 rc = tpm_add_legacy_sysfs(chip);
607 if (rc) {
608 tpm_chip_unregister(chip);
609 return rc;
610 }
611
612 return 0;
613
614out_hwrng:
615 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
616 hwrng_unregister(&chip->hwrng);
617out_ppi:
618 tpm_bios_log_teardown(chip);
619
620 return rc;
621}
622EXPORT_SYMBOL_GPL(tpm_chip_register);
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637void tpm_chip_unregister(struct tpm_chip *chip)
638{
639 tpm_del_legacy_sysfs(chip);
640 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
641 hwrng_unregister(&chip->hwrng);
642 tpm_bios_log_teardown(chip);
643 if (chip->flags & TPM_CHIP_FLAG_TPM2)
644 cdev_device_del(&chip->cdevs, &chip->devs);
645 tpm_del_char_device(chip);
646}
647EXPORT_SYMBOL_GPL(tpm_chip_unregister);
648