18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2004 IBM Corporation
48c2ecf20Sopenharmony_ci * Copyright (C) 2015 Intel Corporation
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Authors:
78c2ecf20Sopenharmony_ci * Leendert van Doorn <leendert@watson.ibm.com>
88c2ecf20Sopenharmony_ci * Dave Safford <safford@watson.ibm.com>
98c2ecf20Sopenharmony_ci * Reiner Sailer <sailer@watson.ibm.com>
108c2ecf20Sopenharmony_ci * Kylene Hall <kjhall@us.ibm.com>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Maintained by: <tpmdd-devel@lists.sourceforge.net>
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Device driver for TCG/TCPA TPM (trusted platform module).
158c2ecf20Sopenharmony_ci * Specifications at www.trustedcomputinggroup.org
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#ifndef __TPM_H__
198c2ecf20Sopenharmony_ci#define __TPM_H__
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <linux/module.h>
228c2ecf20Sopenharmony_ci#include <linux/delay.h>
238c2ecf20Sopenharmony_ci#include <linux/mutex.h>
248c2ecf20Sopenharmony_ci#include <linux/sched.h>
258c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
268c2ecf20Sopenharmony_ci#include <linux/io.h>
278c2ecf20Sopenharmony_ci#include <linux/tpm.h>
288c2ecf20Sopenharmony_ci#include <linux/tpm_eventlog.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#ifdef CONFIG_X86
318c2ecf20Sopenharmony_ci#include <asm/intel-family.h>
328c2ecf20Sopenharmony_ci#endif
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define TPM_MINOR		224	/* officially assigned */
358c2ecf20Sopenharmony_ci#define TPM_BUFSIZE		4096
368c2ecf20Sopenharmony_ci#define TPM_NUM_DEVICES		65536
378c2ecf20Sopenharmony_ci#define TPM_RETRY		50
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cienum tpm_timeout {
408c2ecf20Sopenharmony_ci	TPM_TIMEOUT = 5,	/* msecs */
418c2ecf20Sopenharmony_ci	TPM_TIMEOUT_RETRY = 100, /* msecs */
428c2ecf20Sopenharmony_ci	TPM_TIMEOUT_RANGE_US = 300,	/* usecs */
438c2ecf20Sopenharmony_ci	TPM_TIMEOUT_POLL = 1,	/* msecs */
448c2ecf20Sopenharmony_ci	TPM_TIMEOUT_USECS_MIN = 100,      /* usecs */
458c2ecf20Sopenharmony_ci	TPM_TIMEOUT_USECS_MAX = 500      /* usecs */
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* TPM addresses */
498c2ecf20Sopenharmony_cienum tpm_addr {
508c2ecf20Sopenharmony_ci	TPM_SUPERIO_ADDR = 0x2E,
518c2ecf20Sopenharmony_ci	TPM_ADDR = 0x4E,
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define TPM_WARN_RETRY          0x800
558c2ecf20Sopenharmony_ci#define TPM_WARN_DOING_SELFTEST 0x802
568c2ecf20Sopenharmony_ci#define TPM_ERR_DEACTIVATED     0x6
578c2ecf20Sopenharmony_ci#define TPM_ERR_DISABLED        0x7
588c2ecf20Sopenharmony_ci#define TPM_ERR_INVALID_POSTINIT 38
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define TPM_TAG_RQU_COMMAND 193
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* TPM2 specific constants. */
638c2ecf20Sopenharmony_ci#define TPM2_SPACE_BUFFER_SIZE		16384 /* 16 kB */
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistruct	stclear_flags_t {
668c2ecf20Sopenharmony_ci	__be16	tag;
678c2ecf20Sopenharmony_ci	u8	deactivated;
688c2ecf20Sopenharmony_ci	u8	disableForceClear;
698c2ecf20Sopenharmony_ci	u8	physicalPresence;
708c2ecf20Sopenharmony_ci	u8	physicalPresenceLock;
718c2ecf20Sopenharmony_ci	u8	bGlobalLock;
728c2ecf20Sopenharmony_ci} __packed;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistruct tpm1_version {
758c2ecf20Sopenharmony_ci	u8 major;
768c2ecf20Sopenharmony_ci	u8 minor;
778c2ecf20Sopenharmony_ci	u8 rev_major;
788c2ecf20Sopenharmony_ci	u8 rev_minor;
798c2ecf20Sopenharmony_ci} __packed;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistruct tpm1_version2 {
828c2ecf20Sopenharmony_ci	__be16 tag;
838c2ecf20Sopenharmony_ci	struct tpm1_version version;
848c2ecf20Sopenharmony_ci} __packed;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistruct	timeout_t {
878c2ecf20Sopenharmony_ci	__be32	a;
888c2ecf20Sopenharmony_ci	__be32	b;
898c2ecf20Sopenharmony_ci	__be32	c;
908c2ecf20Sopenharmony_ci	__be32	d;
918c2ecf20Sopenharmony_ci} __packed;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistruct duration_t {
948c2ecf20Sopenharmony_ci	__be32	tpm_short;
958c2ecf20Sopenharmony_ci	__be32	tpm_medium;
968c2ecf20Sopenharmony_ci	__be32	tpm_long;
978c2ecf20Sopenharmony_ci} __packed;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistruct permanent_flags_t {
1008c2ecf20Sopenharmony_ci	__be16	tag;
1018c2ecf20Sopenharmony_ci	u8	disable;
1028c2ecf20Sopenharmony_ci	u8	ownership;
1038c2ecf20Sopenharmony_ci	u8	deactivated;
1048c2ecf20Sopenharmony_ci	u8	readPubek;
1058c2ecf20Sopenharmony_ci	u8	disableOwnerClear;
1068c2ecf20Sopenharmony_ci	u8	allowMaintenance;
1078c2ecf20Sopenharmony_ci	u8	physicalPresenceLifetimeLock;
1088c2ecf20Sopenharmony_ci	u8	physicalPresenceHWEnable;
1098c2ecf20Sopenharmony_ci	u8	physicalPresenceCMDEnable;
1108c2ecf20Sopenharmony_ci	u8	CEKPUsed;
1118c2ecf20Sopenharmony_ci	u8	TPMpost;
1128c2ecf20Sopenharmony_ci	u8	TPMpostLock;
1138c2ecf20Sopenharmony_ci	u8	FIPS;
1148c2ecf20Sopenharmony_ci	u8	operator;
1158c2ecf20Sopenharmony_ci	u8	enableRevokeEK;
1168c2ecf20Sopenharmony_ci	u8	nvLocked;
1178c2ecf20Sopenharmony_ci	u8	readSRKPub;
1188c2ecf20Sopenharmony_ci	u8	tpmEstablished;
1198c2ecf20Sopenharmony_ci	u8	maintenanceDone;
1208c2ecf20Sopenharmony_ci	u8	disableFullDALogicInfo;
1218c2ecf20Sopenharmony_ci} __packed;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_citypedef union {
1248c2ecf20Sopenharmony_ci	struct	permanent_flags_t perm_flags;
1258c2ecf20Sopenharmony_ci	struct	stclear_flags_t	stclear_flags;
1268c2ecf20Sopenharmony_ci	__u8	owned;
1278c2ecf20Sopenharmony_ci	__be32	num_pcrs;
1288c2ecf20Sopenharmony_ci	struct tpm1_version version1;
1298c2ecf20Sopenharmony_ci	struct tpm1_version2 version2;
1308c2ecf20Sopenharmony_ci	__be32	manufacturer_id;
1318c2ecf20Sopenharmony_ci	struct timeout_t  timeout;
1328c2ecf20Sopenharmony_ci	struct duration_t duration;
1338c2ecf20Sopenharmony_ci} cap_t;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cienum tpm_capabilities {
1368c2ecf20Sopenharmony_ci	TPM_CAP_FLAG = 4,
1378c2ecf20Sopenharmony_ci	TPM_CAP_PROP = 5,
1388c2ecf20Sopenharmony_ci	TPM_CAP_VERSION_1_1 = 0x06,
1398c2ecf20Sopenharmony_ci	TPM_CAP_VERSION_1_2 = 0x1A,
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cienum tpm_sub_capabilities {
1438c2ecf20Sopenharmony_ci	TPM_CAP_PROP_PCR = 0x101,
1448c2ecf20Sopenharmony_ci	TPM_CAP_PROP_MANUFACTURER = 0x103,
1458c2ecf20Sopenharmony_ci	TPM_CAP_FLAG_PERM = 0x108,
1468c2ecf20Sopenharmony_ci	TPM_CAP_FLAG_VOL = 0x109,
1478c2ecf20Sopenharmony_ci	TPM_CAP_PROP_OWNER = 0x111,
1488c2ecf20Sopenharmony_ci	TPM_CAP_PROP_TIS_TIMEOUT = 0x115,
1498c2ecf20Sopenharmony_ci	TPM_CAP_PROP_TIS_DURATION = 0x120,
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
1548c2ecf20Sopenharmony_ci * bytes, but 128 is still a relatively large number of random bytes and
1558c2ecf20Sopenharmony_ci * anything much bigger causes users of struct tpm_cmd_t to start getting
1568c2ecf20Sopenharmony_ci * compiler warnings about stack frame size. */
1578c2ecf20Sopenharmony_ci#define TPM_MAX_RNG_DATA	128
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciextern struct class *tpm_class;
1608c2ecf20Sopenharmony_ciextern struct class *tpmrm_class;
1618c2ecf20Sopenharmony_ciextern dev_t tpm_devt;
1628c2ecf20Sopenharmony_ciextern const struct file_operations tpm_fops;
1638c2ecf20Sopenharmony_ciextern const struct file_operations tpmrm_fops;
1648c2ecf20Sopenharmony_ciextern struct idr dev_nums_idr;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cissize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz);
1678c2ecf20Sopenharmony_ciint tpm_get_timeouts(struct tpm_chip *);
1688c2ecf20Sopenharmony_ciint tpm_auto_startup(struct tpm_chip *chip);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ciint tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
1718c2ecf20Sopenharmony_ciint tpm1_auto_startup(struct tpm_chip *chip);
1728c2ecf20Sopenharmony_ciint tpm1_do_selftest(struct tpm_chip *chip);
1738c2ecf20Sopenharmony_ciint tpm1_get_timeouts(struct tpm_chip *chip);
1748c2ecf20Sopenharmony_ciunsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
1758c2ecf20Sopenharmony_ciint tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
1768c2ecf20Sopenharmony_ci		    const char *log_msg);
1778c2ecf20Sopenharmony_ciint tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
1788c2ecf20Sopenharmony_cissize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
1798c2ecf20Sopenharmony_ci		    const char *desc, size_t min_cap_length);
1808c2ecf20Sopenharmony_ciint tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
1818c2ecf20Sopenharmony_ciint tpm1_get_pcr_allocation(struct tpm_chip *chip);
1828c2ecf20Sopenharmony_ciunsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
1838c2ecf20Sopenharmony_ciint tpm_pm_suspend(struct device *dev);
1848c2ecf20Sopenharmony_ciint tpm_pm_resume(struct device *dev);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic inline void tpm_msleep(unsigned int delay_msec)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	usleep_range((delay_msec * 1000) - TPM_TIMEOUT_RANGE_US,
1898c2ecf20Sopenharmony_ci		     delay_msec * 1000);
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciint tpm_chip_start(struct tpm_chip *chip);
1938c2ecf20Sopenharmony_civoid tpm_chip_stop(struct tpm_chip *chip);
1948c2ecf20Sopenharmony_cistruct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistruct tpm_chip *tpm_chip_alloc(struct device *dev,
1978c2ecf20Sopenharmony_ci				const struct tpm_class_ops *ops);
1988c2ecf20Sopenharmony_cistruct tpm_chip *tpmm_chip_alloc(struct device *pdev,
1998c2ecf20Sopenharmony_ci				 const struct tpm_class_ops *ops);
2008c2ecf20Sopenharmony_ciint tpm_chip_register(struct tpm_chip *chip);
2018c2ecf20Sopenharmony_civoid tpm_chip_unregister(struct tpm_chip *chip);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_civoid tpm_sysfs_add_device(struct tpm_chip *chip);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI
2078c2ecf20Sopenharmony_ciextern void tpm_add_ppi(struct tpm_chip *chip);
2088c2ecf20Sopenharmony_ci#else
2098c2ecf20Sopenharmony_cistatic inline void tpm_add_ppi(struct tpm_chip *chip)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci#endif
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ciint tpm2_get_timeouts(struct tpm_chip *chip);
2158c2ecf20Sopenharmony_ciint tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
2168c2ecf20Sopenharmony_ci		  struct tpm_digest *digest, u16 *digest_size_ptr);
2178c2ecf20Sopenharmony_ciint tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
2188c2ecf20Sopenharmony_ci		    struct tpm_digest *digests);
2198c2ecf20Sopenharmony_ciint tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
2208c2ecf20Sopenharmony_cissize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
2218c2ecf20Sopenharmony_ci			u32 *value, const char *desc);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cissize_t tpm2_get_pcr_allocation(struct tpm_chip *chip);
2248c2ecf20Sopenharmony_ciint tpm2_auto_startup(struct tpm_chip *chip);
2258c2ecf20Sopenharmony_civoid tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
2268c2ecf20Sopenharmony_ciunsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
2278c2ecf20Sopenharmony_ciint tpm2_probe(struct tpm_chip *chip);
2288c2ecf20Sopenharmony_ciint tpm2_get_cc_attrs_tbl(struct tpm_chip *chip);
2298c2ecf20Sopenharmony_ciint tpm2_find_cc(struct tpm_chip *chip, u32 cc);
2308c2ecf20Sopenharmony_ciint tpm2_init_space(struct tpm_space *space, unsigned int buf_size);
2318c2ecf20Sopenharmony_civoid tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
2328c2ecf20Sopenharmony_civoid tpm2_flush_space(struct tpm_chip *chip);
2338c2ecf20Sopenharmony_ciint tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
2348c2ecf20Sopenharmony_ci		       size_t cmdsiz);
2358c2ecf20Sopenharmony_ciint tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
2368c2ecf20Sopenharmony_ci		      size_t *bufsiz);
2378c2ecf20Sopenharmony_ciint tpm_devs_add(struct tpm_chip *chip);
2388c2ecf20Sopenharmony_civoid tpm_devs_remove(struct tpm_chip *chip);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_civoid tpm_bios_log_setup(struct tpm_chip *chip);
2418c2ecf20Sopenharmony_civoid tpm_bios_log_teardown(struct tpm_chip *chip);
2428c2ecf20Sopenharmony_ciint tpm_dev_common_init(void);
2438c2ecf20Sopenharmony_civoid tpm_dev_common_exit(void);
2448c2ecf20Sopenharmony_ci#endif
245