xref: /linux/drivers/char/tpm/tpm.h (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2004 IBM Corporation
4  * Copyright (C) 2015 Intel Corporation
5  *
6  * Authors:
7  * Leendert van Doorn <leendert@watson.ibm.com>
8  * Dave Safford <safford@watson.ibm.com>
9  * Reiner Sailer <sailer@watson.ibm.com>
10  * Kylene Hall <kjhall@us.ibm.com>
11  *
12  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13  *
14  * Device driver for TCG/TCPA TPM (trusted platform module).
15  * Specifications at www.trustedcomputinggroup.org
16  */
17 
18 #ifndef __TPM_H__
19 #define __TPM_H__
20 
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/hex.h>
24 #include <linux/mutex.h>
25 #include <linux/sched.h>
26 #include <linux/platform_device.h>
27 #include <linux/io.h>
28 #include <linux/tpm.h>
29 #include <linux/tpm_eventlog.h>
30 
31 #ifdef CONFIG_X86
32 #include <asm/cpu_device_id.h>
33 #endif
34 
35 #define TPM_MINOR		224	/* officially assigned */
36 #define TPM_NUM_DEVICES		65536
37 #define TPM_RETRY		50
38 
39 enum tpm_timeout {
40 	TPM_TIMEOUT = 5,	/* msecs */
41 	TPM_TIMEOUT_RETRY = 100, /* msecs */
42 	TPM_TIMEOUT_RANGE_US = 300,	/* usecs */
43 	TPM_TIMEOUT_POLL = 1,	/* msecs */
44 	TPM_TIMEOUT_USECS_MIN = 100,      /* usecs */
45 	TPM_TIMEOUT_USECS_MAX = 500      /* usecs */
46 };
47 
48 /* TPM addresses */
49 enum tpm_addr {
50 	TPM_SUPERIO_ADDR = 0x2E,
51 	TPM_ADDR = 0x4E,
52 };
53 
54 extern const struct class tpm_class;
55 extern const struct class tpmrm_class;
56 extern dev_t tpm_devt;
57 extern const struct file_operations tpm_fops;
58 extern const struct file_operations tpmrm_fops;
59 extern struct idr dev_nums_idr;
60 
61 ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz);
62 int tpm_get_timeouts(struct tpm_chip *);
63 int tpm_auto_startup(struct tpm_chip *chip);
64 
65 int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
66 int tpm1_auto_startup(struct tpm_chip *chip);
67 int tpm1_do_selftest(struct tpm_chip *chip);
68 int tpm1_get_timeouts(struct tpm_chip *chip);
69 unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
70 int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
71 		    const char *log_msg);
72 int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
73 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
74 		    const char *desc, size_t min_cap_length);
75 int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
76 int tpm1_get_pcr_allocation(struct tpm_chip *chip);
77 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
78 int tpm_pm_suspend(struct device *dev);
79 int tpm_pm_resume(struct device *dev);
80 int tpm_class_shutdown(struct device *dev);
81 
82 static inline void tpm_msleep(unsigned int delay_msec)
83 {
84 	usleep_range((delay_msec * 1000) - TPM_TIMEOUT_RANGE_US,
85 		     delay_msec * 1000);
86 };
87 
88 int tpm_chip_bootstrap(struct tpm_chip *chip);
89 int tpm_chip_start(struct tpm_chip *chip);
90 void tpm_chip_stop(struct tpm_chip *chip);
91 
92 struct tpm_chip *tpm_chip_alloc(struct device *dev,
93 				const struct tpm_class_ops *ops);
94 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
95 				 const struct tpm_class_ops *ops);
96 int tpm_chip_register(struct tpm_chip *chip);
97 void tpm_chip_unregister(struct tpm_chip *chip);
98 
99 void tpm_sysfs_add_device(struct tpm_chip *chip);
100 
101 
102 #ifdef CONFIG_ACPI
103 extern void tpm_add_ppi(struct tpm_chip *chip);
104 #else
105 static inline void tpm_add_ppi(struct tpm_chip *chip)
106 {
107 }
108 #endif
109 
110 int tpm2_get_timeouts(struct tpm_chip *chip);
111 int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
112 		  struct tpm_digest *digest, u16 *digest_size_ptr);
113 int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
114 		    struct tpm_digest *digests);
115 int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
116 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
117 			u32 *value, const char *desc);
118 
119 ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip);
120 int tpm2_auto_startup(struct tpm_chip *chip);
121 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
122 unsigned long tpm2_calc_ordinal_duration(u32 ordinal);
123 int tpm2_probe(struct tpm_chip *chip);
124 int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip);
125 int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
126 int tpm2_init_space(struct tpm_space *space, unsigned int buf_size);
127 void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
128 void tpm2_flush_space(struct tpm_chip *chip);
129 int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
130 		       size_t cmdsiz);
131 int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
132 		      size_t *bufsiz);
133 int tpm_devs_add(struct tpm_chip *chip);
134 void tpm_devs_remove(struct tpm_chip *chip);
135 int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
136 		      unsigned int buf_size, unsigned int *offset);
137 int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
138 		      unsigned int *offset, u32 *handle);
139 
140 void tpm_bios_log_setup(struct tpm_chip *chip);
141 void tpm_bios_log_teardown(struct tpm_chip *chip);
142 int tpm_dev_common_init(void);
143 void tpm_dev_common_exit(void);
144 
145 #ifdef CONFIG_TCG_TPM2_HMAC
146 int tpm2_sessions_init(struct tpm_chip *chip);
147 #else
148 static inline int tpm2_sessions_init(struct tpm_chip *chip)
149 {
150 	return 0;
151 }
152 #endif
153 
154 #endif
155