soc/intel/alderlake: Add ADL-P 4+4 with 28W TDP
[coreboot.git] / src / drivers / crb / tis.c
bloba7d4fa73473dbeb641063b21fa4591c7faed5885
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <security/tpm/tis.h>
5 #include <acpi/acpigen.h>
6 #include <device/device.h>
7 #include <drivers/intel/ptt/ptt.h>
8 #include <drivers/tpm/tpm_ppi.h>
9 #include <security/tpm/tss.h>
10 #include <endian.h>
11 #include <smbios.h>
12 #include <string.h>
14 #include "tpm.h"
15 #include "chip.h"
17 static unsigned int tpm_is_open;
19 static 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"}
28 static const char *tis_get_dev_name(struct tpm2_info *info)
30 int i;
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";
38 int tis_open(void)
40 if (tpm_is_open) {
41 printk(BIOS_ERR, "%s called twice.\n", __func__);
42 return -1;
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;
50 printk(BIOS_DEBUG, "%s: Intel PTT is active.\n", __func__);
53 return 0;
56 int tis_init(void)
58 struct tpm2_info info;
60 // Wake TPM up (if necessary)
61 if (tpm2_init() != 0)
62 return -1;
64 tpm2_get_info(&info);
66 printk(BIOS_INFO, "Initialized TPM device %s revision %d\n", tis_get_dev_name(&info),
67 info.revision);
69 return 0;
72 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, uint8_t *recvbuf, size_t *rbuf_len)
74 int len = tpm2_process_command(sendbuf, sbuf_size, recvbuf, *rbuf_len);
76 if (len == 0)
77 return -1;
79 *rbuf_len = len;
81 return 0;
84 static void crb_tpm_fill_ssdt(const struct device *dev)
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);
92 /* Device */
93 acpigen_write_device(path);
95 acpigen_write_name_string("_HID", "MSFT0101");
96 acpigen_write_name_string("_CID", "MSFT0101");
98 acpi_device_write_uid(dev);
100 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
102 /* Resources */
103 acpigen_write_name("_CRS");
104 acpigen_write_resourcetemplate_header();
105 acpigen_write_mem32fixed(1, TPM_CRB_BASE_ADDRESS, 0x5000);
107 acpigen_write_resourcetemplate_footer();
109 if (!CONFIG(CHROMEOS) && CONFIG(TPM_PPI))
110 tpm_ppi_acpi_fill_ssdt(dev);
112 acpigen_pop_len(); /* Device */
115 static const char *crb_tpm_acpi_name(const struct device *dev)
117 return "TPM";
120 #if CONFIG(GENERATE_SMBIOS_TABLES) && CONFIG(TPM2)
121 static int tpm_get_cap(uint32_t property, uint32_t *value)
123 TPMS_CAPABILITY_DATA cap_data;
124 int i;
125 uint32_t status;
127 if (!value)
128 return -1;
130 status = tlcl_get_capability(TPM_CAP_TPM_PROPERTIES, property, 1, &cap_data);
132 if (status)
133 return -1;
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;
142 return -1;
145 static int smbios_write_type43_tpm(struct device *dev, int *handle, unsigned long *current)
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;
152 tpm2_get_info(&info);
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;
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;
167 tpm_manuf = be32toh(tpm_manuf);
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;
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;
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;
184 tpm_family = be32toh(tpm_family);
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;
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);
198 #endif
200 static struct device_operations __maybe_unused crb_ops = {
201 .read_resources = noop_read_resources,
202 .set_resources = noop_set_resources,
203 #if CONFIG(HAVE_ACPI_TABLES)
204 .acpi_name = crb_tpm_acpi_name,
205 .acpi_fill_ssdt = crb_tpm_fill_ssdt,
206 #endif
207 #if CONFIG(GENERATE_SMBIOS_TABLES) && CONFIG(TPM2)
208 .get_smbios_data = smbios_write_type43_tpm,
209 #endif
212 static void enable_dev(struct device *dev)
214 #if !DEVTREE_EARLY
215 dev->ops = &crb_ops;
216 #endif
219 struct chip_operations drivers_crb_ops = {
220 CHIP_NAME("CRB TPM")
221 .enable_dev = enable_dev