2 * Copyright (C) 2004 IBM Corporation
3 * Copyright (C) 2015 Intel 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>
11 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
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
26 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/mutex.h>
29 #include <linux/sched.h>
30 #include <linux/platform_device.h>
32 #include <linux/tpm.h>
33 #include <linux/highmem.h>
34 #include <linux/tpm_eventlog.h>
37 #include <asm/intel-family.h>
40 #define TPM_MINOR 224 /* officially assigned */
41 #define TPM_BUFSIZE 4096
42 #define TPM_NUM_DEVICES 65536
46 TPM_TIMEOUT = 5, /* msecs */
47 TPM_TIMEOUT_RETRY = 100, /* msecs */
48 TPM_TIMEOUT_RANGE_US = 300, /* usecs */
49 TPM_TIMEOUT_POLL = 1, /* msecs */
50 TPM_TIMEOUT_USECS_MIN = 100, /* usecs */
51 TPM_TIMEOUT_USECS_MAX = 500 /* usecs */
56 TPM_SUPERIO_ADDR = 0x2E,
60 #define TPM_WARN_RETRY 0x800
61 #define TPM_WARN_DOING_SELFTEST 0x802
62 #define TPM_ERR_DEACTIVATED 0x6
63 #define TPM_ERR_DISABLED 0x7
64 #define TPM_ERR_INVALID_POSTINIT 38
66 #define TPM_HEADER_SIZE 10
69 TPM2_PLATFORM_PCR = 24,
70 TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR + 7) / 8),
75 TPM2_TIMEOUT_B = 2000,
78 TPM2_DURATION_SHORT = 20,
79 TPM2_DURATION_MEDIUM = 750,
80 TPM2_DURATION_LONG = 2000,
81 TPM2_DURATION_LONG_LONG = 300000,
82 TPM2_DURATION_DEFAULT = 120000,
85 enum tpm2_structures {
86 TPM2_ST_NO_SESSIONS = 0x8001,
87 TPM2_ST_SESSIONS = 0x8002,
90 /* Indicates from what layer of the software stack the error comes from */
91 #define TSS2_RC_LAYER_SHIFT 16
92 #define TSS2_RESMGR_TPM_RC_LAYER (11 << TSS2_RC_LAYER_SHIFT)
94 enum tpm2_return_codes {
95 TPM2_RC_SUCCESS = 0x0000,
96 TPM2_RC_HASH = 0x0083, /* RC_FMT1 */
97 TPM2_RC_HANDLE = 0x008B,
98 TPM2_RC_INITIALIZE = 0x0100, /* RC_VER1 */
99 TPM2_RC_FAILURE = 0x0101,
100 TPM2_RC_DISABLED = 0x0120,
101 TPM2_RC_COMMAND_CODE = 0x0143,
102 TPM2_RC_TESTING = 0x090A, /* RC_WARN */
103 TPM2_RC_REFERENCE_H0 = 0x0910,
104 TPM2_RC_RETRY = 0x0922,
107 enum tpm2_command_codes {
108 TPM2_CC_FIRST = 0x011F,
109 TPM2_CC_HIERARCHY_CONTROL = 0x0121,
110 TPM2_CC_HIERARCHY_CHANGE_AUTH = 0x0129,
111 TPM2_CC_CREATE_PRIMARY = 0x0131,
112 TPM2_CC_SEQUENCE_COMPLETE = 0x013E,
113 TPM2_CC_SELF_TEST = 0x0143,
114 TPM2_CC_STARTUP = 0x0144,
115 TPM2_CC_SHUTDOWN = 0x0145,
116 TPM2_CC_NV_READ = 0x014E,
117 TPM2_CC_CREATE = 0x0153,
118 TPM2_CC_LOAD = 0x0157,
119 TPM2_CC_SEQUENCE_UPDATE = 0x015C,
120 TPM2_CC_UNSEAL = 0x015E,
121 TPM2_CC_CONTEXT_LOAD = 0x0161,
122 TPM2_CC_CONTEXT_SAVE = 0x0162,
123 TPM2_CC_FLUSH_CONTEXT = 0x0165,
124 TPM2_CC_VERIFY_SIGNATURE = 0x0177,
125 TPM2_CC_GET_CAPABILITY = 0x017A,
126 TPM2_CC_GET_RANDOM = 0x017B,
127 TPM2_CC_PCR_READ = 0x017E,
128 TPM2_CC_PCR_EXTEND = 0x0182,
129 TPM2_CC_EVENT_SEQUENCE_COMPLETE = 0x0185,
130 TPM2_CC_HASH_SEQUENCE_START = 0x0186,
131 TPM2_CC_CREATE_LOADED = 0x0191,
132 TPM2_CC_LAST = 0x0193, /* Spec 1.36 */
135 enum tpm2_permanent_handles {
136 TPM2_RS_PW = 0x40000009,
139 enum tpm2_capabilities {
140 TPM2_CAP_HANDLES = 1,
141 TPM2_CAP_COMMANDS = 2,
143 TPM2_CAP_TPM_PROPERTIES = 6,
146 enum tpm2_properties {
147 TPM_PT_TOTAL_COMMANDS = 0x0129,
150 enum tpm2_startup_types {
151 TPM2_SU_CLEAR = 0x0000,
152 TPM2_SU_STATE = 0x0001,
156 TPM2_CC_ATTR_CHANDLES = 25,
157 TPM2_CC_ATTR_RHANDLE = 28,
160 #define TPM_VID_INTEL 0x8086
161 #define TPM_VID_WINBOND 0x1050
162 #define TPM_VID_STM 0x104A
164 enum tpm_chip_flags {
165 TPM_CHIP_FLAG_TPM2 = BIT(1),
166 TPM_CHIP_FLAG_IRQ = BIT(2),
167 TPM_CHIP_FLAG_VIRTUAL = BIT(3),
168 TPM_CHIP_FLAG_HAVE_TIMEOUTS = BIT(4),
169 TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5),
172 #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
183 #define TPM_TAG_RQU_COMMAND 193
185 struct stclear_flags_t {
188 u8 disableForceClear;
190 u8 physicalPresenceLock;
194 struct tpm_version_t {
201 struct tpm_version_1_2_t {
222 struct permanent_flags_t {
228 u8 disableOwnerClear;
230 u8 physicalPresenceLifetimeLock;
231 u8 physicalPresenceHWEnable;
232 u8 physicalPresenceCMDEnable;
243 u8 disableFullDALogicInfo;
247 struct permanent_flags_t perm_flags;
248 struct stclear_flags_t stclear_flags;
251 struct tpm_version_t tpm_version;
252 struct tpm_version_1_2_t tpm_version_1_2;
253 __be32 manufacturer_id;
254 struct timeout_t timeout;
255 struct duration_t duration;
258 enum tpm_capabilities {
261 TPM_CAP_VERSION_1_1 = 0x06,
262 TPM_CAP_VERSION_1_2 = 0x1A,
265 enum tpm_sub_capabilities {
266 TPM_CAP_PROP_PCR = 0x101,
267 TPM_CAP_PROP_MANUFACTURER = 0x103,
268 TPM_CAP_FLAG_PERM = 0x108,
269 TPM_CAP_FLAG_VOL = 0x109,
270 TPM_CAP_PROP_OWNER = 0x111,
271 TPM_CAP_PROP_TIS_TIMEOUT = 0x115,
272 TPM_CAP_PROP_TIS_DURATION = 0x120,
276 /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
277 * bytes, but 128 is still a relatively large number of random bytes and
278 * anything much bigger causes users of struct tpm_cmd_t to start getting
279 * compiler warnings about stack frame size. */
280 #define TPM_MAX_RNG_DATA 128
282 /* A string buffer type for constructing TPM commands. This is based on the
283 * ideas of string buffer code in security/keys/trusted.h but is heap based
284 * in order to keep the stack usage minimal.
288 TPM_BUF_OVERFLOW = BIT(0),
292 struct page *data_page;
297 static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
299 struct tpm_header *head = (struct tpm_header *)buf->data;
301 head->tag = cpu_to_be16(tag);
302 head->length = cpu_to_be32(sizeof(*head));
303 head->ordinal = cpu_to_be32(ordinal);
306 static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
308 buf->data_page = alloc_page(GFP_HIGHUSER);
313 buf->data = kmap(buf->data_page);
314 tpm_buf_reset(buf, tag, ordinal);
318 static inline void tpm_buf_destroy(struct tpm_buf *buf)
320 kunmap(buf->data_page);
321 __free_page(buf->data_page);
324 static inline u32 tpm_buf_length(struct tpm_buf *buf)
326 struct tpm_header *head = (struct tpm_header *)buf->data;
328 return be32_to_cpu(head->length);
331 static inline u16 tpm_buf_tag(struct tpm_buf *buf)
333 struct tpm_header *head = (struct tpm_header *)buf->data;
335 return be16_to_cpu(head->tag);
338 static inline void tpm_buf_append(struct tpm_buf *buf,
339 const unsigned char *new_data,
340 unsigned int new_len)
342 struct tpm_header *head = (struct tpm_header *)buf->data;
343 u32 len = tpm_buf_length(buf);
345 /* Return silently if overflow has already happened. */
346 if (buf->flags & TPM_BUF_OVERFLOW)
349 if ((len + new_len) > PAGE_SIZE) {
350 WARN(1, "tpm_buf: overflow\n");
351 buf->flags |= TPM_BUF_OVERFLOW;
355 memcpy(&buf->data[len], new_data, new_len);
356 head->length = cpu_to_be32(len + new_len);
359 static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
361 tpm_buf_append(buf, &value, 1);
364 static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
366 __be16 value2 = cpu_to_be16(value);
368 tpm_buf_append(buf, (u8 *) &value2, 2);
371 static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
373 __be32 value2 = cpu_to_be32(value);
375 tpm_buf_append(buf, (u8 *) &value2, 4);
378 extern struct class *tpm_class;
379 extern struct class *tpmrm_class;
380 extern dev_t tpm_devt;
381 extern const struct file_operations tpm_fops;
382 extern const struct file_operations tpmrm_fops;
383 extern struct idr dev_nums_idr;
385 ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz);
386 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
387 size_t min_rsp_body_length, const char *desc);
388 int tpm_get_timeouts(struct tpm_chip *);
389 int tpm_auto_startup(struct tpm_chip *chip);
391 int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
392 int tpm1_auto_startup(struct tpm_chip *chip);
393 int tpm1_do_selftest(struct tpm_chip *chip);
394 int tpm1_get_timeouts(struct tpm_chip *chip);
395 unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
396 int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
397 const char *log_msg);
398 int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
399 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
400 const char *desc, size_t min_cap_length);
401 int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
402 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
403 int tpm_pm_suspend(struct device *dev);
404 int tpm_pm_resume(struct device *dev);
406 static inline void tpm_msleep(unsigned int delay_msec)
408 usleep_range((delay_msec * 1000) - TPM_TIMEOUT_RANGE_US,
412 int tpm_chip_start(struct tpm_chip *chip);
413 void tpm_chip_stop(struct tpm_chip *chip);
414 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip);
415 __must_check int tpm_try_get_ops(struct tpm_chip *chip);
416 void tpm_put_ops(struct tpm_chip *chip);
418 struct tpm_chip *tpm_chip_alloc(struct device *dev,
419 const struct tpm_class_ops *ops);
420 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
421 const struct tpm_class_ops *ops);
422 int tpm_chip_register(struct tpm_chip *chip);
423 void tpm_chip_unregister(struct tpm_chip *chip);
425 void tpm_sysfs_add_device(struct tpm_chip *chip);
429 extern void tpm_add_ppi(struct tpm_chip *chip);
431 static inline void tpm_add_ppi(struct tpm_chip *chip)
436 static inline u32 tpm2_rc_value(u32 rc)
438 return (rc & BIT(7)) ? rc & 0xff : rc;
441 int tpm2_get_timeouts(struct tpm_chip *chip);
442 int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
443 struct tpm_digest *digest, u16 *digest_size_ptr);
444 int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
445 struct tpm_digest *digests);
446 int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
447 void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
448 int tpm2_seal_trusted(struct tpm_chip *chip,
449 struct trusted_key_payload *payload,
450 struct trusted_key_options *options);
451 int tpm2_unseal_trusted(struct tpm_chip *chip,
452 struct trusted_key_payload *payload,
453 struct trusted_key_options *options);
454 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
455 u32 *value, const char *desc);
457 int tpm2_auto_startup(struct tpm_chip *chip);
458 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
459 unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
460 int tpm2_probe(struct tpm_chip *chip);
461 int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
462 int tpm2_init_space(struct tpm_space *space);
463 void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
464 void tpm2_flush_space(struct tpm_chip *chip);
465 int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
467 int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
470 int tpm_bios_log_setup(struct tpm_chip *chip);
471 void tpm_bios_log_teardown(struct tpm_chip *chip);
472 int tpm_dev_common_init(void);
473 void tpm_dev_common_exit(void);