blob: a7d4fa73473dbeb641063b21fa4591c7faed5885 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Christian Walter7706a042019-07-05 19:46:30 +02002
Christian Walter7706a042019-07-05 19:46:30 +02003#include <console/console.h>
4#include <security/tpm/tis.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpigen.h>
Christian Walter7706a042019-07-05 19:46:30 +02006#include <device/device.h>
Christian Walter542268192019-07-16 20:07:36 +02007#include <drivers/intel/ptt/ptt.h>
Michał Żygowskiea66f822022-05-17 11:02:06 +02008#include <drivers/tpm/tpm_ppi.h>
Michał Żygowski283e5872022-05-04 14:12:17 +02009#include <security/tpm/tss.h>
10#include <endian.h>
11#include <smbios.h>
12#include <string.h>
Christian Walter7706a042019-07-05 19:46:30 +020013
14#include "tpm.h"
15#include "chip.h"
16
Sergii Dmytruk4ee03172022-12-22 19:35:25 +020017static unsigned int tpm_is_open;
18
Christian Walter7706a042019-07-05 19:46:30 +020019static const struct {
20 uint16_t vid;
21 uint16_t did;
22 const char *device_name;
23} dev_map[] = {
24 {0x1ae0, 0x0028, "CR50"},
25 {0xa13a, 0x8086, "Intel iTPM"}
26};
27
28static const char *tis_get_dev_name(struct tpm2_info *info)
29{
30 int i;
31
32 for (i = 0; i < ARRAY_SIZE(dev_map); i++)
33 if ((dev_map[i].vid == info->vendor_id) && (dev_map[i].did == info->device_id))
34 return dev_map[i].device_name;
35 return "Unknown";
36}
37
Sergii Dmytruk4ee03172022-12-22 19:35:25 +020038int tis_open(void)
39{
40 if (tpm_is_open) {
41 printk(BIOS_ERR, "%s called twice.\n", __func__);
42 return -1;
43 }
44
45 if (CONFIG(HAVE_INTEL_PTT)) {
46 if (!ptt_active()) {
47 printk(BIOS_ERR, "%s: Intel PTT is not active.\n", __func__);
48 return -1;
49 }
50 printk(BIOS_DEBUG, "%s: Intel PTT is active.\n", __func__);
51 }
52
53 return 0;
54}
55
56int tis_init(void)
57{
58 struct tpm2_info info;
59
60 // Wake TPM up (if necessary)
61 if (tpm2_init() != 0)
62 return -1;
63
64 tpm2_get_info(&info);
65
66 printk(BIOS_INFO, "Initialized TPM device %s revision %d\n", tis_get_dev_name(&info),
67 info.revision);
68
69 return 0;
70}
71
72int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, uint8_t *recvbuf, size_t *rbuf_len)
Christian Walter7706a042019-07-05 19:46:30 +020073{
74 int len = tpm2_process_command(sendbuf, sbuf_size, recvbuf, *rbuf_len);
75
76 if (len == 0)
77 return -1;
78
79 *rbuf_len = len;
80
81 return 0;
82}
83
Furquan Shaikh7536a392020-04-24 21:59:21 -070084static void crb_tpm_fill_ssdt(const struct device *dev)
Christian Walter7706a042019-07-05 19:46:30 +020085{
86 const char *path = acpi_device_path(dev);
87 if (!path) {
88 path = "\\_SB_.TPM";
89 printk(BIOS_DEBUG, "Using default TPM2 ACPI path: '%s'\n", path);
90 }
91
92 /* Device */
93 acpigen_write_device(path);
94
95 acpigen_write_name_string("_HID", "MSFT0101");
96 acpigen_write_name_string("_CID", "MSFT0101");
97
Patrick Rudolphc83bab62019-12-13 12:16:06 +010098 acpi_device_write_uid(dev);
Christian Walter7706a042019-07-05 19:46:30 +020099
100 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
101
102 /* Resources */
103 acpigen_write_name("_CRS");
104 acpigen_write_resourcetemplate_header();
105 acpigen_write_mem32fixed(1, TPM_CRB_BASE_ADDRESS, 0x5000);
106
107 acpigen_write_resourcetemplate_footer();
108
Michał Żygowskiea66f822022-05-17 11:02:06 +0200109 if (!CONFIG(CHROMEOS) && CONFIG(TPM_PPI))
110 tpm_ppi_acpi_fill_ssdt(dev);
111
Christian Walter7706a042019-07-05 19:46:30 +0200112 acpigen_pop_len(); /* Device */
113}
114
115static const char *crb_tpm_acpi_name(const struct device *dev)
116{
117 return "TPM";
118}
119
Michał Żygowski283e5872022-05-04 14:12:17 +0200120#if CONFIG(GENERATE_SMBIOS_TABLES) && CONFIG(TPM2)
121static int tpm_get_cap(uint32_t property, uint32_t *value)
122{
123 TPMS_CAPABILITY_DATA cap_data;
124 int i;
125 uint32_t status;
126
127 if (!value)
128 return -1;
129
130 status = tlcl_get_capability(TPM_CAP_TPM_PROPERTIES, property, 1, &cap_data);
131
132 if (status)
133 return -1;
134
135 for (i = 0 ; i < cap_data.data.tpmProperties.count; i++) {
136 if (cap_data.data.tpmProperties.tpmProperty[i].property == property) {
137 *value = cap_data.data.tpmProperties.tpmProperty[i].value;
138 return 0;
139 }
140 }
141
142 return -1;
143}
144
145static int smbios_write_type43_tpm(struct device *dev, int *handle, unsigned long *current)
146{
147 struct tpm2_info info;
148 uint32_t tpm_manuf, tpm_family;
149 uint32_t fw_ver1, fw_ver2;
150 uint8_t major_spec_ver, minor_spec_ver;
151
152 tpm2_get_info(&info);
153
154 /* If any of these have invalid values, assume TPM not present or disabled */
155 if (info.vendor_id == 0 || info.vendor_id == 0xFFFF ||
156 info.device_id == 0 || info.device_id == 0xFFFF) {
157 printk(BIOS_DEBUG, "%s: Invalid Vendor ID/Device ID\n", __func__);
158 return 0;
159 }
160
161 /* Vendor ID is the value returned by TPM2_GetCapabiltiy TPM_PT_MANUFACTURER */
162 if (tpm_get_cap(TPM_PT_MANUFACTURER, &tpm_manuf)) {
163 printk(BIOS_DEBUG, "TPM2_GetCap TPM_PT_MANUFACTURER failed\n");
164 return 0;
165 }
166
167 tpm_manuf = be32toh(tpm_manuf);
168
169 if (tpm_get_cap(TPM_PT_FIRMWARE_VERSION_1, &fw_ver1)) {
170 printk(BIOS_DEBUG, "TPM2_GetCap TPM_PT_FIRMWARE_VERSION_1 failed\n");
171 return 0;
172 }
173
174 if (tpm_get_cap(TPM_PT_FIRMWARE_VERSION_2, &fw_ver2)) {
175 printk(BIOS_DEBUG, "TPM2_GetCap TPM_PT_FIRMWARE_VERSION_2 failed\n");
176 return 0;
177 }
178
179 if (tpm_get_cap(TPM_PT_FAMILY_INDICATOR, &tpm_family)) {
180 printk(BIOS_DEBUG, "TPM2_GetCap TPM_PT_FAMILY_INDICATOR failed\n");
181 return 0;
182 }
183
184 tpm_family = be32toh(tpm_family);
185
186 if (!strncmp((char *)&tpm_family, "2.0", 4)) {
187 major_spec_ver = 2;
188 minor_spec_ver = 0;
189 } else {
190 printk(BIOS_ERR, "%s: Invalid TPM family\n", __func__);
191 return 0;
192 }
193
194 return smbios_write_type43(current, handle, tpm_manuf, major_spec_ver, minor_spec_ver,
195 fw_ver1, fw_ver2, tis_get_dev_name(&info),
196 SMBIOS_TPM_DEVICE_CHARACTERISTICS_NOT_SUPPORTED, 0);
197}
198#endif
199
Bill XIEac1362502022-07-08 16:53:21 +0800200static struct device_operations __maybe_unused crb_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200201 .read_resources = noop_read_resources,
202 .set_resources = noop_set_resources,
Christian Walter7706a042019-07-05 19:46:30 +0200203#if CONFIG(HAVE_ACPI_TABLES)
204 .acpi_name = crb_tpm_acpi_name,
Nico Huber68680dd2020-03-31 17:34:52 +0200205 .acpi_fill_ssdt = crb_tpm_fill_ssdt,
Christian Walter7706a042019-07-05 19:46:30 +0200206#endif
Michał Żygowski283e5872022-05-04 14:12:17 +0200207#if CONFIG(GENERATE_SMBIOS_TABLES) && CONFIG(TPM2)
208 .get_smbios_data = smbios_write_type43_tpm,
209#endif
Christian Walter7706a042019-07-05 19:46:30 +0200210};
211
212static void enable_dev(struct device *dev)
213{
Kyösti Mälkki35a047c2019-11-05 18:38:00 +0200214#if !DEVTREE_EARLY
Christian Walter7706a042019-07-05 19:46:30 +0200215 dev->ops = &crb_ops;
Kyösti Mälkki35a047c2019-11-05 18:38:00 +0200216#endif
Christian Walter7706a042019-07-05 19:46:30 +0200217}
218
Kyösti Mälkki35a047c2019-11-05 18:38:00 +0200219struct chip_operations drivers_crb_ops = {
220 CHIP_NAME("CRB TPM")
221 .enable_dev = enable_dev
222};