blob: 53c0ea9ec9df222b3ec3fab64de475a43cc834e5 [file] [log] [blame]
Thomas Gleixnerb886d832019-06-01 10:08:55 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Rajiv Andrade659aaf22009-02-02 15:23:44 -02002/*
3 * Copyright (C) 2004,2007,2008 IBM Corporation
4 *
5 * Authors:
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>
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
Rajiv Andrade659aaf22009-02-02 15:23:44 -020016 */
17#ifndef __LINUX_TPM_H__
18#define __LINUX_TPM_H__
19
Roberto Sassu901615c2019-02-06 17:24:50 +010020#include <linux/hw_random.h>
21#include <linux/acpi.h>
22#include <linux/cdev.h>
23#include <linux/fs.h>
Roberto Sassuaa042472019-02-06 17:24:48 +010024#include <crypto/hash_info.h>
25
Mimi Zohar1c16c962013-05-21 10:40:47 -040026#define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */
Roberto Sassuaa042472019-02-06 17:24:48 +010027#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
Mimi Zohar1c16c962013-05-21 10:40:47 -040028
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070029struct tpm_chip;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +030030struct trusted_key_payload;
31struct trusted_key_options;
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070032
Roberto Sassuaa042472019-02-06 17:24:48 +010033enum tpm_algorithms {
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,
42};
43
44struct tpm_digest {
45 u16 alg_id;
46 u8 digest[TPM_MAX_DIGEST_SIZE];
47} __packed;
48
Roberto Sassu879b5892019-02-06 17:24:49 +010049struct tpm_bank_info {
50 u16 alg_id;
51 u16 digest_size;
52 u16 crypto_id;
53};
54
Jason Gunthorpecae8b442016-07-12 11:41:49 -060055enum TPM_OPS_FLAGS {
56 TPM_OPS_AUTO_STARTUP = BIT(0),
57};
58
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070059struct tpm_class_ops {
Jason Gunthorpecae8b442016-07-12 11:41:49 -060060 unsigned int flags;
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070061 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);
Jerry Snitselaar36ce0892019-01-30 15:06:58 -070068 void (*update_timeouts)(struct tpm_chip *chip,
Jason Gunthorpe8e54caf2014-05-21 18:26:44 -060069 unsigned long *timeout_cap);
Tomas Winkler627448e2018-06-28 18:13:33 +030070 int (*go_idle)(struct tpm_chip *chip);
71 int (*cmd_ready)(struct tpm_chip *chip);
Jarkko Sakkinen877c57d2017-03-24 11:45:49 +020072 int (*request_locality)(struct tpm_chip *chip, int loc);
Tomas Winkler888d8672018-03-05 13:34:49 +020073 int (*relinquish_locality)(struct tpm_chip *chip, int loc);
Azhar Shaikhb3e958c2017-12-22 12:13:44 -080074 void (*clk_enable)(struct tpm_chip *chip, bool value);
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070075};
76
Roberto Sassu901615c2019-02-06 17:24:50 +010077#define TPM_NUM_EVENT_LOG_FILES 3
78
79/* Indexes the duration array */
80enum tpm_duration {
81 TPM_SHORT = 0,
82 TPM_MEDIUM = 1,
83 TPM_LONG = 2,
84 TPM_LONG_LONG = 3,
85 TPM_UNDEFINED,
86 TPM_NUM_DURATIONS = TPM_UNDEFINED,
87};
88
89#define TPM_PPI_VERSION_LEN 3
90
91struct tpm_space {
92 u32 context_tbl[3];
93 u8 *context_buf;
94 u32 session_tbl[3];
95 u8 *session_buf;
96};
97
98struct tpm_bios_log {
99 void *bios_event_log;
100 void *bios_event_log_end;
101};
102
103struct tpm_chip_seqops {
104 struct tpm_chip *chip;
105 const struct seq_operations *seqops;
106};
107
108struct tpm_chip {
109 struct device dev;
110 struct device devs;
111 struct cdev cdev;
112 struct cdev cdevs;
113
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.
117 */
118 struct rw_semaphore ops_sem;
119 const struct tpm_class_ops *ops;
120
121 struct tpm_bios_log log;
122 struct tpm_chip_seqops bin_log_seqops;
123 struct tpm_chip_seqops ascii_log_seqops;
124
125 unsigned int flags;
126
127 int dev_num; /* /dev/tpm# */
128 unsigned long is_open; /* only one allowed */
129
130 char hwrng_name[64];
131 struct hwrng hwrng;
132
133 struct mutex tpm_mutex; /* tpm is processing */
134
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;
142
143 struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
144
145 const struct attribute_group *groups[3];
146 unsigned int groups_cnt;
147
148 u32 nr_allocated_banks;
149 struct tpm_bank_info *allocated_banks;
150#ifdef CONFIG_ACPI
151 acpi_handle acpi_dev_handle;
152 char ppi_version[TPM_PPI_VERSION_LEN + 1];
153#endif /* CONFIG_ACPI */
154
155 struct tpm_space work_space;
156 u32 last_cc;
157 u32 nr_commands;
158 u32 *cc_attrs_tbl;
159
160 /* active locality */
161 int locality;
162};
163
Randy Dunlapff76ec12009-10-28 12:26:39 -0700164#if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200165
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200166extern int tpm_is_tpm2(struct tpm_chip *chip);
Roberto Sassu879b5892019-02-06 17:24:49 +0100167extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
168 struct tpm_digest *digest);
Roberto Sassu0b6cf6b2019-02-06 17:24:52 +0100169extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
170 struct tpm_digest *digests);
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200171extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
172extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
173extern int tpm_seal_trusted(struct tpm_chip *chip,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300174 struct trusted_key_payload *payload,
175 struct trusted_key_options *options);
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200176extern int tpm_unseal_trusted(struct tpm_chip *chip,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300177 struct trusted_key_payload *payload,
178 struct trusted_key_options *options);
Stefan Bergeraaae8152018-06-26 15:09:30 -0400179extern struct tpm_chip *tpm_default_chip(void);
Mimi Zohard6ba4522009-10-26 09:26:18 -0400180#else
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200181static inline int tpm_is_tpm2(struct tpm_chip *chip)
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300182{
183 return -ENODEV;
184}
Tomas Winkler95adc6b2018-10-19 21:23:07 +0300185
Roberto Sassu879b5892019-02-06 17:24:49 +0100186static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
187 struct tpm_digest *digest)
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200188{
Mimi Zohard6ba4522009-10-26 09:26:18 -0400189 return -ENODEV;
190}
Tomas Winkler95adc6b2018-10-19 21:23:07 +0300191
192static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
Roberto Sassu0b6cf6b2019-02-06 17:24:52 +0100193 struct tpm_digest *digests)
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200194{
Mimi Zohard6ba4522009-10-26 09:26:18 -0400195 return -ENODEV;
196}
Tomas Winkler95adc6b2018-10-19 21:23:07 +0300197
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200198static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
199{
Mimi Zoharc749ba92010-11-23 18:54:16 -0500200 return -ENODEV;
201}
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200202static inline int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max)
203{
Kent Yoder41ab9992012-06-07 13:47:14 -0500204 return -ENODEV;
205}
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300206
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200207static inline int tpm_seal_trusted(struct tpm_chip *chip,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300208 struct trusted_key_payload *payload,
209 struct trusted_key_options *options)
210{
211 return -ENODEV;
212}
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200213static inline int tpm_unseal_trusted(struct tpm_chip *chip,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300214 struct trusted_key_payload *payload,
215 struct trusted_key_options *options)
216{
217 return -ENODEV;
218}
Stefan Bergeraaae8152018-06-26 15:09:30 -0400219static inline struct tpm_chip *tpm_default_chip(void)
220{
221 return NULL;
222}
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200223#endif
224#endif