2 * Copyright (C) 2004,2007,2008 IBM Corporation
13 * Device driver for TCG/TCPA TPM (trusted platform module).
14 * Specifications at www.trustedcomputinggroup.org
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation, version 2 of the
22 #ifndef __LINUX_TPM_H__
23 #define __LINUX_TPM_H__
25 #include <linux/hw_random.h>
26 #include <linux/acpi.h>
27 #include <linux/cdev.h>
29 #include <crypto/hash_info.h>
31 #define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */
32 #define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
35 struct trusted_key_payload;
36 struct trusted_key_options;
39 TPM_ALG_ERROR = 0x0000,
40 TPM_ALG_SHA1 = 0x0004,
41 TPM_ALG_KEYEDHASH = 0x0008,
42 TPM_ALG_SHA256 = 0x000B,
43 TPM_ALG_SHA384 = 0x000C,
44 TPM_ALG_SHA512 = 0x000D,
45 TPM_ALG_NULL = 0x0010,
46 TPM_ALG_SM3_256 = 0x0012,
51 u8 digest[TPM_MAX_DIGEST_SIZE];
54 struct tpm_bank_info {
61 TPM_OPS_AUTO_STARTUP = BIT(0),
64 struct tpm_class_ops {
66 const u8 req_complete_mask;
67 const u8 req_complete_val;
68 bool (*req_canceled)(struct tpm_chip *chip, u8 status);
69 int (*recv) (struct tpm_chip *chip, u8 *buf, size_t len);
70 int (*send) (struct tpm_chip *chip, u8 *buf, size_t len);
71 void (*cancel) (struct tpm_chip *chip);
72 u8 (*status) (struct tpm_chip *chip);
73 void (*update_timeouts)(struct tpm_chip *chip,
74 unsigned long *timeout_cap);
75 int (*go_idle)(struct tpm_chip *chip);
76 int (*cmd_ready)(struct tpm_chip *chip);
77 int (*request_locality)(struct tpm_chip *chip, int loc);
78 int (*relinquish_locality)(struct tpm_chip *chip, int loc);
79 void (*clk_enable)(struct tpm_chip *chip, bool value);
82 #define TPM_NUM_EVENT_LOG_FILES 3
84 /* Indexes the duration array */
91 TPM_NUM_DURATIONS = TPM_UNDEFINED,
94 #define TPM_PPI_VERSION_LEN 3
103 struct tpm_bios_log {
104 void *bios_event_log;
105 void *bios_event_log_end;
108 struct tpm_chip_seqops {
109 struct tpm_chip *chip;
110 const struct seq_operations *seqops;
119 /* A driver callback under ops cannot be run unless ops_sem is held
120 * (sometimes implicitly, eg for the sysfs code). ops becomes null
121 * when the driver is unregistered, see tpm_try_get_ops.
123 struct rw_semaphore ops_sem;
124 const struct tpm_class_ops *ops;
126 struct tpm_bios_log log;
127 struct tpm_chip_seqops bin_log_seqops;
128 struct tpm_chip_seqops ascii_log_seqops;
132 int dev_num; /* /dev/tpm# */
133 unsigned long is_open; /* only one allowed */
138 struct mutex tpm_mutex; /* tpm is processing */
140 unsigned long timeout_a; /* jiffies */
141 unsigned long timeout_b; /* jiffies */
142 unsigned long timeout_c; /* jiffies */
143 unsigned long timeout_d; /* jiffies */
144 bool timeout_adjusted;
145 unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
146 bool duration_adjusted;
148 struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
150 const struct attribute_group *groups[3];
151 unsigned int groups_cnt;
153 u32 nr_allocated_banks;
154 struct tpm_bank_info *allocated_banks;
156 acpi_handle acpi_dev_handle;
157 char ppi_version[TPM_PPI_VERSION_LEN + 1];
158 #endif /* CONFIG_ACPI */
160 struct tpm_space work_space;
165 /* active locality */
169 #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
171 extern int tpm_is_tpm2(struct tpm_chip *chip);
172 extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
173 struct tpm_digest *digest);
174 extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
175 struct tpm_digest *digests);
176 extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
177 extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
178 extern int tpm_seal_trusted(struct tpm_chip *chip,
179 struct trusted_key_payload *payload,
180 struct trusted_key_options *options);
181 extern int tpm_unseal_trusted(struct tpm_chip *chip,
182 struct trusted_key_payload *payload,
183 struct trusted_key_options *options);
184 extern struct tpm_chip *tpm_default_chip(void);
186 static inline int tpm_is_tpm2(struct tpm_chip *chip)
191 static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
192 struct tpm_digest *digest)
197 static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
198 struct tpm_digest *digests)
203 static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
207 static inline int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max)
212 static inline int tpm_seal_trusted(struct tpm_chip *chip,
213 struct trusted_key_payload *payload,
214 struct trusted_key_options *options)
218 static inline int tpm_unseal_trusted(struct tpm_chip *chip,
219 struct trusted_key_payload *payload,
220 struct trusted_key_options *options)
224 static inline struct tpm_chip *tpm_default_chip(void)