Lines Matching refs:chip

35 static int tpm_request_locality(struct tpm_chip *chip)  in tpm_request_locality()  argument
39 if (!chip->ops->request_locality) in tpm_request_locality()
42 rc = chip->ops->request_locality(chip, 0); in tpm_request_locality()
46 chip->locality = rc; in tpm_request_locality()
50 static void tpm_relinquish_locality(struct tpm_chip *chip) in tpm_relinquish_locality() argument
54 if (!chip->ops->relinquish_locality) in tpm_relinquish_locality()
57 rc = chip->ops->relinquish_locality(chip, chip->locality); in tpm_relinquish_locality()
59 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); in tpm_relinquish_locality()
61 chip->locality = -1; in tpm_relinquish_locality()
64 static int tpm_cmd_ready(struct tpm_chip *chip) in tpm_cmd_ready() argument
66 if (!chip->ops->cmd_ready) in tpm_cmd_ready()
69 return chip->ops->cmd_ready(chip); in tpm_cmd_ready()
72 static int tpm_go_idle(struct tpm_chip *chip) in tpm_go_idle() argument
74 if (!chip->ops->go_idle) in tpm_go_idle()
77 return chip->ops->go_idle(chip); in tpm_go_idle()
80 static void tpm_clk_enable(struct tpm_chip *chip) in tpm_clk_enable() argument
82 if (chip->ops->clk_enable) in tpm_clk_enable()
83 chip->ops->clk_enable(chip, true); in tpm_clk_enable()
86 static void tpm_clk_disable(struct tpm_chip *chip) in tpm_clk_disable() argument
88 if (chip->ops->clk_enable) in tpm_clk_disable()
89 chip->ops->clk_enable(chip, false); in tpm_clk_disable()
100 int tpm_chip_start(struct tpm_chip *chip) in tpm_chip_start() argument
104 tpm_clk_enable(chip); in tpm_chip_start()
106 if (chip->locality == -1) { in tpm_chip_start()
107 ret = tpm_request_locality(chip); in tpm_chip_start()
109 tpm_clk_disable(chip); in tpm_chip_start()
114 ret = tpm_cmd_ready(chip); in tpm_chip_start()
116 tpm_relinquish_locality(chip); in tpm_chip_start()
117 tpm_clk_disable(chip); in tpm_chip_start()
133 void tpm_chip_stop(struct tpm_chip *chip) in tpm_chip_stop() argument
135 tpm_go_idle(chip); in tpm_chip_stop()
136 tpm_relinquish_locality(chip); in tpm_chip_stop()
137 tpm_clk_disable(chip); in tpm_chip_stop()
152 int tpm_try_get_ops(struct tpm_chip *chip) in tpm_try_get_ops() argument
156 get_device(&chip->dev); in tpm_try_get_ops()
158 down_read(&chip->ops_sem); in tpm_try_get_ops()
159 if (!chip->ops) in tpm_try_get_ops()
162 mutex_lock(&chip->tpm_mutex); in tpm_try_get_ops()
163 rc = tpm_chip_start(chip); in tpm_try_get_ops()
169 mutex_unlock(&chip->tpm_mutex); in tpm_try_get_ops()
171 up_read(&chip->ops_sem); in tpm_try_get_ops()
172 put_device(&chip->dev); in tpm_try_get_ops()
184 void tpm_put_ops(struct tpm_chip *chip) in tpm_put_ops() argument
186 tpm_chip_stop(chip); in tpm_put_ops()
187 mutex_unlock(&chip->tpm_mutex); in tpm_put_ops()
188 up_read(&chip->ops_sem); in tpm_put_ops()
189 put_device(&chip->dev); in tpm_put_ops()
198 struct tpm_chip *chip, *res = NULL; in tpm_default_chip() local
206 chip = idr_get_next(&dev_nums_idr, &chip_num); in tpm_default_chip()
207 if (chip) { in tpm_default_chip()
208 get_device(&chip->dev); in tpm_default_chip()
209 res = chip; in tpm_default_chip()
235 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip) in tpm_find_get_ops() argument
239 if (chip) { in tpm_find_get_ops()
240 if (!tpm_try_get_ops(chip)) in tpm_find_get_ops()
241 return chip; in tpm_find_get_ops()
245 chip = tpm_default_chip(); in tpm_find_get_ops()
246 if (!chip) in tpm_find_get_ops()
248 rc = tpm_try_get_ops(chip); in tpm_find_get_ops()
250 put_device(&chip->dev); in tpm_find_get_ops()
253 return chip; in tpm_find_get_ops()
264 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_dev_release() local
267 idr_remove(&dev_nums_idr, chip->dev_num); in tpm_dev_release()
270 kfree(chip->work_space.context_buf); in tpm_dev_release()
271 kfree(chip->work_space.session_buf); in tpm_dev_release()
272 kfree(chip->allocated_banks); in tpm_dev_release()
273 kfree(chip); in tpm_dev_release()
287 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_class_shutdown() local
289 down_write(&chip->ops_sem); in tpm_class_shutdown()
290 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_class_shutdown()
291 if (!tpm_chip_start(chip)) { in tpm_class_shutdown()
292 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_class_shutdown()
293 tpm_chip_stop(chip); in tpm_class_shutdown()
296 chip->ops = NULL; in tpm_class_shutdown()
297 up_write(&chip->ops_sem); in tpm_class_shutdown()
315 struct tpm_chip *chip; in tpm_chip_alloc() local
318 chip = kzalloc(sizeof(*chip), GFP_KERNEL); in tpm_chip_alloc()
319 if (chip == NULL) in tpm_chip_alloc()
322 mutex_init(&chip->tpm_mutex); in tpm_chip_alloc()
323 init_rwsem(&chip->ops_sem); in tpm_chip_alloc()
325 chip->ops = ops; in tpm_chip_alloc()
332 kfree(chip); in tpm_chip_alloc()
335 chip->dev_num = rc; in tpm_chip_alloc()
337 device_initialize(&chip->dev); in tpm_chip_alloc()
339 chip->dev.class = tpm_class; in tpm_chip_alloc()
340 chip->dev.class->shutdown_pre = tpm_class_shutdown; in tpm_chip_alloc()
341 chip->dev.release = tpm_dev_release; in tpm_chip_alloc()
342 chip->dev.parent = pdev; in tpm_chip_alloc()
343 chip->dev.groups = chip->groups; in tpm_chip_alloc()
345 if (chip->dev_num == 0) in tpm_chip_alloc()
346 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); in tpm_chip_alloc()
348 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); in tpm_chip_alloc()
350 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); in tpm_chip_alloc()
355 chip->flags |= TPM_CHIP_FLAG_VIRTUAL; in tpm_chip_alloc()
357 cdev_init(&chip->cdev, &tpm_fops); in tpm_chip_alloc()
358 chip->cdev.owner = THIS_MODULE; in tpm_chip_alloc()
360 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE); in tpm_chip_alloc()
366 chip->locality = -1; in tpm_chip_alloc()
367 return chip; in tpm_chip_alloc()
370 put_device(&chip->dev); in tpm_chip_alloc()
390 struct tpm_chip *chip; in tpmm_chip_alloc() local
393 chip = tpm_chip_alloc(pdev, ops); in tpmm_chip_alloc()
394 if (IS_ERR(chip)) in tpmm_chip_alloc()
395 return chip; in tpmm_chip_alloc()
399 &chip->dev); in tpmm_chip_alloc()
403 dev_set_drvdata(pdev, chip); in tpmm_chip_alloc()
405 return chip; in tpmm_chip_alloc()
409 static int tpm_add_char_device(struct tpm_chip *chip) in tpm_add_char_device() argument
413 rc = cdev_device_add(&chip->cdev, &chip->dev); in tpm_add_char_device()
415 dev_err(&chip->dev, in tpm_add_char_device()
417 dev_name(&chip->dev), MAJOR(chip->dev.devt), in tpm_add_char_device()
418 MINOR(chip->dev.devt), rc); in tpm_add_char_device()
422 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) { in tpm_add_char_device()
423 rc = tpm_devs_add(chip); in tpm_add_char_device()
430 idr_replace(&dev_nums_idr, chip, chip->dev_num); in tpm_add_char_device()
436 cdev_device_del(&chip->cdev, &chip->dev); in tpm_add_char_device()
440 static void tpm_del_char_device(struct tpm_chip *chip) in tpm_del_char_device() argument
442 cdev_device_del(&chip->cdev, &chip->dev); in tpm_del_char_device()
446 idr_replace(&dev_nums_idr, NULL, chip->dev_num); in tpm_del_char_device()
450 down_write(&chip->ops_sem); in tpm_del_char_device()
457 if (chip->ops) { in tpm_del_char_device()
458 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_del_char_device()
459 if (!tpm_chip_start(chip)) { in tpm_del_char_device()
460 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_del_char_device()
461 tpm_chip_stop(chip); in tpm_del_char_device()
464 chip->ops = NULL; in tpm_del_char_device()
466 up_write(&chip->ops_sem); in tpm_del_char_device()
469 static void tpm_del_legacy_sysfs(struct tpm_chip *chip) in tpm_del_legacy_sysfs() argument
473 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) || in tpm_del_legacy_sysfs()
474 tpm_is_firmware_upgrade(chip)) in tpm_del_legacy_sysfs()
477 sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); in tpm_del_legacy_sysfs()
479 for (i = chip->groups[0]->attrs; *i != NULL; ++i) in tpm_del_legacy_sysfs()
480 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); in tpm_del_legacy_sysfs()
487 static int tpm_add_legacy_sysfs(struct tpm_chip *chip) in tpm_add_legacy_sysfs() argument
492 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) || in tpm_add_legacy_sysfs()
493 tpm_is_firmware_upgrade(chip)) in tpm_add_legacy_sysfs()
497 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL); in tpm_add_legacy_sysfs()
502 for (i = chip->groups[0]->attrs; *i != NULL; ++i) { in tpm_add_legacy_sysfs()
504 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL); in tpm_add_legacy_sysfs()
506 tpm_del_legacy_sysfs(chip); in tpm_add_legacy_sysfs()
522 static bool tpm_amd_is_rng_defective(struct tpm_chip *chip) in tpm_amd_is_rng_defective() argument
528 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) in tpm_amd_is_rng_defective()
531 ret = tpm_request_locality(chip); in tpm_amd_is_rng_defective()
535 ret = tpm2_get_tpm_pt(chip, TPM2_PT_MANUFACTURER, &val1, NULL); in tpm_amd_is_rng_defective()
542 ret = tpm2_get_tpm_pt(chip, TPM2_PT_FIRMWARE_VERSION_1, &val1, NULL); in tpm_amd_is_rng_defective()
545 ret = tpm2_get_tpm_pt(chip, TPM2_PT_FIRMWARE_VERSION_2, &val2, NULL); in tpm_amd_is_rng_defective()
548 tpm_relinquish_locality(chip); in tpm_amd_is_rng_defective()
564 dev_warn(&chip->dev, in tpm_amd_is_rng_defective()
573 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); in tpm_hwrng_read() local
575 return tpm_get_random(chip, data, max); in tpm_hwrng_read()
578 static int tpm_add_hwrng(struct tpm_chip *chip) in tpm_add_hwrng() argument
580 if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM) || tpm_is_firmware_upgrade(chip) || in tpm_add_hwrng()
581 tpm_amd_is_rng_defective(chip)) in tpm_add_hwrng()
584 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), in tpm_add_hwrng()
585 "tpm-rng-%d", chip->dev_num); in tpm_add_hwrng()
586 chip->hwrng.name = chip->hwrng_name; in tpm_add_hwrng()
587 chip->hwrng.read = tpm_hwrng_read; in tpm_add_hwrng()
588 return hwrng_register(&chip->hwrng); in tpm_add_hwrng()
591 static int tpm_get_pcr_allocation(struct tpm_chip *chip) in tpm_get_pcr_allocation() argument
595 if (tpm_is_firmware_upgrade(chip)) in tpm_get_pcr_allocation()
598 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ? in tpm_get_pcr_allocation()
599 tpm2_get_pcr_allocation(chip) : in tpm_get_pcr_allocation()
600 tpm1_get_pcr_allocation(chip); in tpm_get_pcr_allocation()
619 int tpm_chip_register(struct tpm_chip *chip) in tpm_chip_register() argument
623 rc = tpm_chip_start(chip); in tpm_chip_register()
626 rc = tpm_auto_startup(chip); in tpm_chip_register()
628 tpm_chip_stop(chip); in tpm_chip_register()
632 rc = tpm_get_pcr_allocation(chip); in tpm_chip_register()
633 tpm_chip_stop(chip); in tpm_chip_register()
637 tpm_sysfs_add_device(chip); in tpm_chip_register()
639 tpm_bios_log_setup(chip); in tpm_chip_register()
641 tpm_add_ppi(chip); in tpm_chip_register()
643 rc = tpm_add_hwrng(chip); in tpm_chip_register()
647 rc = tpm_add_char_device(chip); in tpm_chip_register()
651 rc = tpm_add_legacy_sysfs(chip); in tpm_chip_register()
653 tpm_chip_unregister(chip); in tpm_chip_register()
660 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip)) in tpm_chip_register()
661 hwrng_unregister(&chip->hwrng); in tpm_chip_register()
663 tpm_bios_log_teardown(chip); in tpm_chip_register()
682 void tpm_chip_unregister(struct tpm_chip *chip) in tpm_chip_unregister() argument
684 tpm_del_legacy_sysfs(chip); in tpm_chip_unregister()
685 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip)) in tpm_chip_unregister()
686 hwrng_unregister(&chip->hwrng); in tpm_chip_unregister()
687 tpm_bios_log_teardown(chip); in tpm_chip_unregister()
688 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) in tpm_chip_unregister()
689 tpm_devs_remove(chip); in tpm_chip_unregister()
690 tpm_del_char_device(chip); in tpm_chip_unregister()