Lines Matching +full:chip +full:- +full:to +full:- +full:chip
1 // SPDX-License-Identifier: GPL-2.0-only
13 * Maintained by: <tpmdd-[email protected]>
15 * TPM chip management routines.
40 static int tpm_request_locality(struct tpm_chip *chip) in tpm_request_locality() argument
44 if (!chip->ops->request_locality) in tpm_request_locality()
47 rc = chip->ops->request_locality(chip, 0); in tpm_request_locality()
51 chip->locality = rc; in tpm_request_locality()
55 static void tpm_relinquish_locality(struct tpm_chip *chip) in tpm_relinquish_locality() argument
59 if (!chip->ops->relinquish_locality) in tpm_relinquish_locality()
62 rc = chip->ops->relinquish_locality(chip, chip->locality); in tpm_relinquish_locality()
64 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); in tpm_relinquish_locality()
66 chip->locality = -1; in tpm_relinquish_locality()
69 static int tpm_cmd_ready(struct tpm_chip *chip) in tpm_cmd_ready() argument
71 if (!chip->ops->cmd_ready) in tpm_cmd_ready()
74 return chip->ops->cmd_ready(chip); in tpm_cmd_ready()
77 static int tpm_go_idle(struct tpm_chip *chip) in tpm_go_idle() argument
79 if (!chip->ops->go_idle) in tpm_go_idle()
82 return chip->ops->go_idle(chip); in tpm_go_idle()
85 static void tpm_clk_enable(struct tpm_chip *chip) in tpm_clk_enable() argument
87 if (chip->ops->clk_enable) in tpm_clk_enable()
88 chip->ops->clk_enable(chip, true); in tpm_clk_enable()
91 static void tpm_clk_disable(struct tpm_chip *chip) in tpm_clk_disable() argument
93 if (chip->ops->clk_enable) in tpm_clk_disable()
94 chip->ops->clk_enable(chip, false); in tpm_clk_disable()
98 * tpm_chip_start() - power on the TPM
99 * @chip: a TPM chip to use
102 * * The response length - OK
103 * * -errno - A system error
105 int tpm_chip_start(struct tpm_chip *chip) in tpm_chip_start() argument
109 tpm_clk_enable(chip); in tpm_chip_start()
111 if (chip->locality == -1) { in tpm_chip_start()
112 ret = tpm_request_locality(chip); in tpm_chip_start()
114 tpm_clk_disable(chip); in tpm_chip_start()
119 ret = tpm_cmd_ready(chip); in tpm_chip_start()
121 tpm_relinquish_locality(chip); in tpm_chip_start()
122 tpm_clk_disable(chip); in tpm_chip_start()
131 * tpm_chip_stop() - power off the TPM
132 * @chip: a TPM chip to use
135 * * The response length - OK
136 * * -errno - A system error
138 void tpm_chip_stop(struct tpm_chip *chip) in tpm_chip_stop() argument
140 tpm_go_idle(chip); in tpm_chip_stop()
141 tpm_relinquish_locality(chip); in tpm_chip_stop()
142 tpm_clk_disable(chip); in tpm_chip_stop()
147 * tpm_try_get_ops() - Get a ref to the tpm_chip
148 * @chip: Chip to ref
150 * The caller must already have some kind of locking to ensure that chip is
151 * valid. This function will lock the chip so that the ops member can be
155 * Returns -ERRNO if the chip could not be got.
157 int tpm_try_get_ops(struct tpm_chip *chip) in tpm_try_get_ops() argument
159 int rc = -EIO; in tpm_try_get_ops()
161 if (chip->flags & TPM_CHIP_FLAG_DISABLE) in tpm_try_get_ops()
164 get_device(&chip->dev); in tpm_try_get_ops()
166 down_read(&chip->ops_sem); in tpm_try_get_ops()
167 if (!chip->ops) in tpm_try_get_ops()
170 mutex_lock(&chip->tpm_mutex); in tpm_try_get_ops()
173 if (chip->flags & TPM_CHIP_FLAG_SUSPENDED) in tpm_try_get_ops()
176 rc = tpm_chip_start(chip); in tpm_try_get_ops()
182 mutex_unlock(&chip->tpm_mutex); in tpm_try_get_ops()
184 up_read(&chip->ops_sem); in tpm_try_get_ops()
185 put_device(&chip->dev); in tpm_try_get_ops()
191 * tpm_put_ops() - Release a ref to the tpm_chip
192 * @chip: Chip to put
194 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
197 void tpm_put_ops(struct tpm_chip *chip) in tpm_put_ops() argument
199 tpm_chip_stop(chip); in tpm_put_ops()
200 mutex_unlock(&chip->tpm_mutex); in tpm_put_ops()
201 up_read(&chip->ops_sem); in tpm_put_ops()
202 put_device(&chip->dev); in tpm_put_ops()
207 * tpm_default_chip() - find a TPM chip and get a reference to it
211 struct tpm_chip *chip, *res = NULL; in tpm_default_chip() local
219 chip = idr_get_next(&dev_nums_idr, &chip_num); in tpm_default_chip()
220 if (chip) { in tpm_default_chip()
221 get_device(&chip->dev); in tpm_default_chip()
222 res = chip; in tpm_default_chip()
234 * tpm_find_get_ops() - find and reserve a TPM chip
235 * @chip: a &struct tpm_chip instance, %NULL for the default chip
237 * Finds a TPM chip and reserves its class device and operations. The chip must
240 * by accepting NULL, but those callers should be converted to pass in a chip
245 * %NULL if a chip is not found.
246 * %NULL if the chip is not available.
248 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip) in tpm_find_get_ops() argument
252 if (chip) { in tpm_find_get_ops()
253 if (!tpm_try_get_ops(chip)) in tpm_find_get_ops()
254 return chip; in tpm_find_get_ops()
258 chip = tpm_default_chip(); in tpm_find_get_ops()
259 if (!chip) in tpm_find_get_ops()
261 rc = tpm_try_get_ops(chip); in tpm_find_get_ops()
263 put_device(&chip->dev); in tpm_find_get_ops()
266 return chip; in tpm_find_get_ops()
270 * tpm_dev_release() - free chip memory and the device number
271 * @dev: the character device for the TPM chip
277 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_dev_release() local
280 idr_remove(&dev_nums_idr, chip->dev_num); in tpm_dev_release()
283 kfree(chip->work_space.context_buf); in tpm_dev_release()
284 kfree(chip->work_space.session_buf); in tpm_dev_release()
285 kfree(chip->allocated_banks); in tpm_dev_release()
287 kfree(chip->auth); in tpm_dev_release()
289 kfree(chip); in tpm_dev_release()
293 * tpm_class_shutdown() - prepare the TPM device for loss of power.
294 * @dev: device to which the chip is associated.
296 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
297 * TPM 2.0 spec. Then, calls bus- and device- specific shutdown code.
303 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_class_shutdown() local
305 down_write(&chip->ops_sem); in tpm_class_shutdown()
306 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_class_shutdown()
307 if (!tpm_chip_start(chip)) { in tpm_class_shutdown()
308 tpm2_end_auth_session(chip); in tpm_class_shutdown()
309 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_class_shutdown()
310 tpm_chip_stop(chip); in tpm_class_shutdown()
313 chip->ops = NULL; in tpm_class_shutdown()
314 up_write(&chip->ops_sem); in tpm_class_shutdown()
320 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
321 * @pdev: device to which the chip is associated
322 * At this point pdev mst be initialized, but does not have to
327 * device number for it. Must be paired with put_device(&chip->dev).
332 struct tpm_chip *chip; in tpm_chip_alloc() local
335 chip = kzalloc(sizeof(*chip), GFP_KERNEL); in tpm_chip_alloc()
336 if (chip == NULL) in tpm_chip_alloc()
337 return ERR_PTR(-ENOMEM); in tpm_chip_alloc()
339 mutex_init(&chip->tpm_mutex); in tpm_chip_alloc()
340 init_rwsem(&chip->ops_sem); in tpm_chip_alloc()
342 chip->ops = ops; in tpm_chip_alloc()
349 kfree(chip); in tpm_chip_alloc()
352 chip->dev_num = rc; in tpm_chip_alloc()
354 device_initialize(&chip->dev); in tpm_chip_alloc()
356 chip->dev.class = &tpm_class; in tpm_chip_alloc()
357 chip->dev.release = tpm_dev_release; in tpm_chip_alloc()
358 chip->dev.parent = pdev; in tpm_chip_alloc()
359 chip->dev.groups = chip->groups; in tpm_chip_alloc()
361 if (chip->dev_num == 0) in tpm_chip_alloc()
362 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); in tpm_chip_alloc()
364 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); in tpm_chip_alloc()
366 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); in tpm_chip_alloc()
371 chip->flags |= TPM_CHIP_FLAG_VIRTUAL; in tpm_chip_alloc()
373 cdev_init(&chip->cdev, &tpm_fops); in tpm_chip_alloc()
374 chip->cdev.owner = THIS_MODULE; in tpm_chip_alloc()
376 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE); in tpm_chip_alloc()
378 rc = -ENOMEM; in tpm_chip_alloc()
382 chip->locality = -1; in tpm_chip_alloc()
383 return chip; in tpm_chip_alloc()
386 put_device(&chip->dev); in tpm_chip_alloc()
397 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
398 * @pdev: parent device to which the chip is associated
401 * Same as tpm_chip_alloc except devm is used to do the put_device
406 struct tpm_chip *chip; in tpmm_chip_alloc() local
409 chip = tpm_chip_alloc(pdev, ops); in tpmm_chip_alloc()
410 if (IS_ERR(chip)) in tpmm_chip_alloc()
411 return chip; in tpmm_chip_alloc()
415 &chip->dev); in tpmm_chip_alloc()
419 dev_set_drvdata(pdev, chip); in tpmm_chip_alloc()
421 return chip; in tpmm_chip_alloc()
425 static int tpm_add_char_device(struct tpm_chip *chip) in tpm_add_char_device() argument
429 rc = cdev_device_add(&chip->cdev, &chip->dev); in tpm_add_char_device()
431 dev_err(&chip->dev, in tpm_add_char_device()
432 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", in tpm_add_char_device()
433 dev_name(&chip->dev), MAJOR(chip->dev.devt), in tpm_add_char_device()
434 MINOR(chip->dev.devt), rc); in tpm_add_char_device()
438 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) { in tpm_add_char_device()
439 rc = tpm_devs_add(chip); in tpm_add_char_device()
444 /* Make the chip available. */ in tpm_add_char_device()
446 idr_replace(&dev_nums_idr, chip, chip->dev_num); in tpm_add_char_device()
452 cdev_device_del(&chip->cdev, &chip->dev); in tpm_add_char_device()
456 static void tpm_del_char_device(struct tpm_chip *chip) in tpm_del_char_device() argument
458 cdev_device_del(&chip->cdev, &chip->dev); in tpm_del_char_device()
460 /* Make the chip unavailable. */ in tpm_del_char_device()
462 idr_replace(&dev_nums_idr, NULL, chip->dev_num); in tpm_del_char_device()
466 down_write(&chip->ops_sem); in tpm_del_char_device()
469 * Check if chip->ops is still valid: In case that the controller in tpm_del_char_device()
471 * shutdown handler we are called twice and chip->ops to NULL. in tpm_del_char_device()
473 if (chip->ops) { in tpm_del_char_device()
474 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_del_char_device()
475 if (!tpm_chip_start(chip)) { in tpm_del_char_device()
476 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_del_char_device()
477 tpm_chip_stop(chip); in tpm_del_char_device()
480 chip->ops = NULL; in tpm_del_char_device()
482 up_write(&chip->ops_sem); in tpm_del_char_device()
485 static void tpm_del_legacy_sysfs(struct tpm_chip *chip) in tpm_del_legacy_sysfs() argument
489 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) || in tpm_del_legacy_sysfs()
490 tpm_is_firmware_upgrade(chip)) in tpm_del_legacy_sysfs()
493 sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); in tpm_del_legacy_sysfs()
495 for (i = chip->groups[0]->attrs; *i != NULL; ++i) in tpm_del_legacy_sysfs()
496 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); in tpm_del_legacy_sysfs()
500 * parent dev directory to selected names within the tpm chip directory. Old
503 static int tpm_add_legacy_sysfs(struct tpm_chip *chip) in tpm_add_legacy_sysfs() argument
508 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) || in tpm_add_legacy_sysfs()
509 tpm_is_firmware_upgrade(chip)) in tpm_add_legacy_sysfs()
513 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL); in tpm_add_legacy_sysfs()
514 if (rc && rc != -ENOENT) in tpm_add_legacy_sysfs()
517 /* All the names from tpm-sysfs */ in tpm_add_legacy_sysfs()
518 for (i = chip->groups[0]->attrs; *i != NULL; ++i) { in tpm_add_legacy_sysfs()
520 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL); in tpm_add_legacy_sysfs()
522 tpm_del_legacy_sysfs(chip); in tpm_add_legacy_sysfs()
532 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); in tpm_hwrng_read() local
534 return tpm_get_random(chip, data, max); in tpm_hwrng_read()
537 static bool tpm_is_hwrng_enabled(struct tpm_chip *chip) in tpm_is_hwrng_enabled() argument
541 if (tpm_is_firmware_upgrade(chip)) in tpm_is_hwrng_enabled()
543 if (chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED) in tpm_is_hwrng_enabled()
548 static int tpm_add_hwrng(struct tpm_chip *chip) in tpm_add_hwrng() argument
550 if (!tpm_is_hwrng_enabled(chip)) in tpm_add_hwrng()
553 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), in tpm_add_hwrng()
554 "tpm-rng-%d", chip->dev_num); in tpm_add_hwrng()
555 chip->hwrng.name = chip->hwrng_name; in tpm_add_hwrng()
556 chip->hwrng.read = tpm_hwrng_read; in tpm_add_hwrng()
557 return hwrng_register(&chip->hwrng); in tpm_add_hwrng()
560 static int tpm_get_pcr_allocation(struct tpm_chip *chip) in tpm_get_pcr_allocation() argument
564 if (tpm_is_firmware_upgrade(chip)) in tpm_get_pcr_allocation()
567 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ? in tpm_get_pcr_allocation()
568 tpm2_get_pcr_allocation(chip) : in tpm_get_pcr_allocation()
569 tpm1_get_pcr_allocation(chip); in tpm_get_pcr_allocation()
572 return -ENODEV; in tpm_get_pcr_allocation()
578 * tpm_chip_bootstrap() - Boostrap TPM chip after power on
579 * @chip: TPM chip to use.
581 * Initialize TPM chip after power on. This a one-shot function: subsequent
584 int tpm_chip_bootstrap(struct tpm_chip *chip) in tpm_chip_bootstrap() argument
588 if (chip->flags & TPM_CHIP_FLAG_BOOTSTRAPPED) in tpm_chip_bootstrap()
591 rc = tpm_chip_start(chip); in tpm_chip_bootstrap()
595 rc = tpm_auto_startup(chip); in tpm_chip_bootstrap()
599 rc = tpm_get_pcr_allocation(chip); in tpm_chip_bootstrap()
601 tpm_chip_stop(chip); in tpm_chip_bootstrap()
607 chip->flags |= TPM_CHIP_FLAG_BOOTSTRAPPED; in tpm_chip_bootstrap()
614 * tpm_chip_register() - create a character device for the TPM chip
615 * @chip: TPM chip to use.
617 * Creates a character device for the TPM chip and adds sysfs attributes for
618 * the device. As the last step this function adds the chip to the list of TPM
619 * chips available for in-kernel use.
621 * This function should be only called after the chip initialization is
624 int tpm_chip_register(struct tpm_chip *chip) in tpm_chip_register() argument
628 rc = tpm_chip_bootstrap(chip); in tpm_chip_register()
632 tpm_sysfs_add_device(chip); in tpm_chip_register()
634 tpm_bios_log_setup(chip); in tpm_chip_register()
636 tpm_add_ppi(chip); in tpm_chip_register()
638 rc = tpm_add_hwrng(chip); in tpm_chip_register()
642 rc = tpm_add_char_device(chip); in tpm_chip_register()
646 rc = tpm_add_legacy_sysfs(chip); in tpm_chip_register()
648 tpm_chip_unregister(chip); in tpm_chip_register()
655 if (tpm_is_hwrng_enabled(chip)) in tpm_chip_register()
656 hwrng_unregister(&chip->hwrng); in tpm_chip_register()
658 tpm_bios_log_teardown(chip); in tpm_chip_register()
665 * tpm_chip_unregister() - release the TPM driver
666 * @chip: TPM chip to use.
668 * Takes the chip first away from the list of available TPM chips and then
674 * NOTE: This function should be only called before deinitializing chip
677 void tpm_chip_unregister(struct tpm_chip *chip) in tpm_chip_unregister() argument
682 rc = tpm_try_get_ops(chip); in tpm_chip_unregister()
684 tpm2_end_auth_session(chip); in tpm_chip_unregister()
685 tpm_put_ops(chip); in tpm_chip_unregister()
689 tpm_del_legacy_sysfs(chip); in tpm_chip_unregister()
690 if (tpm_is_hwrng_enabled(chip)) in tpm_chip_unregister()
691 hwrng_unregister(&chip->hwrng); in tpm_chip_unregister()
692 tpm_bios_log_teardown(chip); in tpm_chip_unregister()
693 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) in tpm_chip_unregister()
694 tpm_devs_remove(chip); in tpm_chip_unregister()
695 tpm_del_char_device(chip); in tpm_chip_unregister()