1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2004,2007,2008 IBM Corporation
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Dave Safford <safford@watson.ibm.com>
8 * Reiner Sailer <sailer@watson.ibm.com>
9 * Kylene Hall <kjhall@us.ibm.com>
10 * Debora Velarde <dvelarde@us.ibm.com>
12 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
14 * Device driver for TCG/TCPA TPM (trusted platform module).
15 * Specifications at www.trustedcomputinggroup.org
17 #ifndef __LINUX_TPM_H__
18 #define __LINUX_TPM_H__
20 #include <linux/hw_random.h>
21 #include <linux/acpi.h>
22 #include <linux/cdev.h>
24 #include <crypto/hash_info.h>
26 #define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */
27 #define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
30 struct trusted_key_payload
;
31 struct trusted_key_options
;
34 TPM_ALG_ERROR
= 0x0000,
35 TPM_ALG_SHA1
= 0x0004,
36 TPM_ALG_KEYEDHASH
= 0x0008,
37 TPM_ALG_SHA256
= 0x000B,
38 TPM_ALG_SHA384
= 0x000C,
39 TPM_ALG_SHA512
= 0x000D,
40 TPM_ALG_NULL
= 0x0010,
41 TPM_ALG_SM3_256
= 0x0012,
46 u8 digest
[TPM_MAX_DIGEST_SIZE
];
49 struct tpm_bank_info
{
56 TPM_OPS_AUTO_STARTUP
= BIT(0),
59 struct tpm_class_ops
{
61 const u8 req_complete_mask
;
62 const u8 req_complete_val
;
63 bool (*req_canceled
)(struct tpm_chip
*chip
, u8 status
);
64 int (*recv
) (struct tpm_chip
*chip
, u8
*buf
, size_t len
);
65 int (*send
) (struct tpm_chip
*chip
, u8
*buf
, size_t len
);
66 void (*cancel
) (struct tpm_chip
*chip
);
67 u8 (*status
) (struct tpm_chip
*chip
);
68 void (*update_timeouts
)(struct tpm_chip
*chip
,
69 unsigned long *timeout_cap
);
70 int (*go_idle
)(struct tpm_chip
*chip
);
71 int (*cmd_ready
)(struct tpm_chip
*chip
);
72 int (*request_locality
)(struct tpm_chip
*chip
, int loc
);
73 int (*relinquish_locality
)(struct tpm_chip
*chip
, int loc
);
74 void (*clk_enable
)(struct tpm_chip
*chip
, bool value
);
77 #define TPM_NUM_EVENT_LOG_FILES 3
79 /* Indexes the duration array */
86 TPM_NUM_DURATIONS
= TPM_UNDEFINED
,
89 #define TPM_PPI_VERSION_LEN 3
100 void *bios_event_log_end
;
103 struct tpm_chip_seqops
{
104 struct tpm_chip
*chip
;
105 const struct seq_operations
*seqops
;
114 /* A driver callback under ops cannot be run unless ops_sem is held
115 * (sometimes implicitly, eg for the sysfs code). ops becomes null
116 * when the driver is unregistered, see tpm_try_get_ops.
118 struct rw_semaphore ops_sem
;
119 const struct tpm_class_ops
*ops
;
121 struct tpm_bios_log log
;
122 struct tpm_chip_seqops bin_log_seqops
;
123 struct tpm_chip_seqops ascii_log_seqops
;
127 int dev_num
; /* /dev/tpm# */
128 unsigned long is_open
; /* only one allowed */
133 struct mutex tpm_mutex
; /* tpm is processing */
135 unsigned long timeout_a
; /* jiffies */
136 unsigned long timeout_b
; /* jiffies */
137 unsigned long timeout_c
; /* jiffies */
138 unsigned long timeout_d
; /* jiffies */
139 bool timeout_adjusted
;
140 unsigned long duration
[TPM_NUM_DURATIONS
]; /* jiffies */
141 bool duration_adjusted
;
143 struct dentry
*bios_dir
[TPM_NUM_EVENT_LOG_FILES
];
145 const struct attribute_group
*groups
[3];
146 unsigned int groups_cnt
;
148 u32 nr_allocated_banks
;
149 struct tpm_bank_info
*allocated_banks
;
151 acpi_handle acpi_dev_handle
;
152 char ppi_version
[TPM_PPI_VERSION_LEN
+ 1];
153 #endif /* CONFIG_ACPI */
155 struct tpm_space work_space
;
160 /* active locality */
164 #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
166 extern int tpm_is_tpm2(struct tpm_chip
*chip
);
167 extern int tpm_pcr_read(struct tpm_chip
*chip
, u32 pcr_idx
,
168 struct tpm_digest
*digest
);
169 extern int tpm_pcr_extend(struct tpm_chip
*chip
, u32 pcr_idx
,
170 struct tpm_digest
*digests
);
171 extern int tpm_send(struct tpm_chip
*chip
, void *cmd
, size_t buflen
);
172 extern int tpm_get_random(struct tpm_chip
*chip
, u8
*data
, size_t max
);
173 extern int tpm_seal_trusted(struct tpm_chip
*chip
,
174 struct trusted_key_payload
*payload
,
175 struct trusted_key_options
*options
);
176 extern int tpm_unseal_trusted(struct tpm_chip
*chip
,
177 struct trusted_key_payload
*payload
,
178 struct trusted_key_options
*options
);
179 extern struct tpm_chip
*tpm_default_chip(void);
181 static inline int tpm_is_tpm2(struct tpm_chip
*chip
)
186 static inline int tpm_pcr_read(struct tpm_chip
*chip
, int pcr_idx
,
187 struct tpm_digest
*digest
)
192 static inline int tpm_pcr_extend(struct tpm_chip
*chip
, u32 pcr_idx
,
193 struct tpm_digest
*digests
)
198 static inline int tpm_send(struct tpm_chip
*chip
, void *cmd
, size_t buflen
)
202 static inline int tpm_get_random(struct tpm_chip
*chip
, u8
*data
, size_t max
)
207 static inline int tpm_seal_trusted(struct tpm_chip
*chip
,
208 struct trusted_key_payload
*payload
,
209 struct trusted_key_options
*options
)
213 static inline int tpm_unseal_trusted(struct tpm_chip
*chip
,
214 struct trusted_key_payload
*payload
,
215 struct trusted_key_options
*options
)
219 static inline struct tpm_chip
*tpm_default_chip(void)