Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 2 | |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 3 | #include <console/console.h> |
| 4 | #include <security/tpm/tis.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 5 | #include <acpi/acpigen.h> |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 6 | #include <device/device.h> |
Christian Walter | 54226819 | 2019-07-16 20:07:36 +0200 | [diff] [blame] | 7 | #include <drivers/intel/ptt/ptt.h> |
Michał Żygowski | ea66f82 | 2022-05-17 11:02:06 +0200 | [diff] [blame] | 8 | #include <drivers/tpm/tpm_ppi.h> |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 9 | #include <security/tpm/tss.h> |
| 10 | #include <endian.h> |
| 11 | #include <smbios.h> |
| 12 | #include <string.h> |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 13 | |
| 14 | #include "tpm.h" |
| 15 | #include "chip.h" |
| 16 | |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 17 | static const struct { |
| 18 | uint16_t vid; |
| 19 | uint16_t did; |
| 20 | const char *device_name; |
| 21 | } dev_map[] = { |
| 22 | {0x1ae0, 0x0028, "CR50"}, |
| 23 | {0xa13a, 0x8086, "Intel iTPM"} |
| 24 | }; |
| 25 | |
Sergii Dmytruk | 1a90314 | 2024-04-12 15:47:04 +0300 | [diff] [blame] | 26 | static const char *tis_get_dev_name(struct crb_tpm_info *info) |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 27 | { |
| 28 | int i; |
| 29 | |
| 30 | for (i = 0; i < ARRAY_SIZE(dev_map); i++) |
| 31 | if ((dev_map[i].vid == info->vendor_id) && (dev_map[i].did == info->device_id)) |
| 32 | return dev_map[i].device_name; |
| 33 | return "Unknown"; |
| 34 | } |
| 35 | |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 36 | static tpm_result_t crb_tpm_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, uint8_t *recvbuf, |
| 37 | size_t *rbuf_len) |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 38 | { |
Sergii Dmytruk | 1a90314 | 2024-04-12 15:47:04 +0300 | [diff] [blame] | 39 | int len = crb_tpm_process_command(sendbuf, sbuf_size, recvbuf, *rbuf_len); |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 40 | |
| 41 | if (len == 0) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 42 | return TPM_CB_FAIL; |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 43 | |
| 44 | *rbuf_len = len; |
| 45 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 46 | return TPM_SUCCESS; |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Sergii Dmytruk | 3e5cefc | 2022-11-01 00:48:43 +0200 | [diff] [blame^] | 49 | tis_sendrecv_fn crb_tis_probe(enum tpm_family *family) |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 50 | { |
Sergii Dmytruk | 1a90314 | 2024-04-12 15:47:04 +0300 | [diff] [blame] | 51 | struct crb_tpm_info info; |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 52 | |
| 53 | /* Wake TPM up (if necessary) */ |
Sergii Dmytruk | 1a90314 | 2024-04-12 15:47:04 +0300 | [diff] [blame] | 54 | if (crb_tpm_init()) |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 55 | return NULL; |
| 56 | |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame] | 57 | /* CRB interface exists only in TPM2 */ |
| 58 | if (family != NULL) |
| 59 | *family = TPM_2; |
| 60 | |
Sergii Dmytruk | 1a90314 | 2024-04-12 15:47:04 +0300 | [diff] [blame] | 61 | crb_tpm_get_info(&info); |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 62 | |
| 63 | printk(BIOS_INFO, "Initialized TPM device %s revision %d\n", tis_get_dev_name(&info), |
| 64 | info.revision); |
| 65 | |
| 66 | if (CONFIG(HAVE_INTEL_PTT)) { |
| 67 | if (!ptt_active()) { |
| 68 | printk(BIOS_ERR, "%s: Intel PTT is not active.\n", __func__); |
| 69 | return NULL; |
| 70 | } |
| 71 | printk(BIOS_DEBUG, "%s: Intel PTT is active.\n", __func__); |
| 72 | } |
| 73 | |
| 74 | return &crb_tpm_sendrecv; |
| 75 | } |
| 76 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 77 | static void crb_tpm_fill_ssdt(const struct device *dev) |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 78 | { |
| 79 | const char *path = acpi_device_path(dev); |
| 80 | if (!path) { |
| 81 | path = "\\_SB_.TPM"; |
| 82 | printk(BIOS_DEBUG, "Using default TPM2 ACPI path: '%s'\n", path); |
| 83 | } |
| 84 | |
| 85 | /* Device */ |
| 86 | acpigen_write_device(path); |
| 87 | |
| 88 | acpigen_write_name_string("_HID", "MSFT0101"); |
| 89 | acpigen_write_name_string("_CID", "MSFT0101"); |
| 90 | |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 91 | acpi_device_write_uid(dev); |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 92 | |
| 93 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 94 | |
| 95 | /* Resources */ |
| 96 | acpigen_write_name("_CRS"); |
| 97 | acpigen_write_resourcetemplate_header(); |
| 98 | acpigen_write_mem32fixed(1, TPM_CRB_BASE_ADDRESS, 0x5000); |
| 99 | |
| 100 | acpigen_write_resourcetemplate_footer(); |
| 101 | |
Michał Żygowski | ea66f82 | 2022-05-17 11:02:06 +0200 | [diff] [blame] | 102 | if (!CONFIG(CHROMEOS) && CONFIG(TPM_PPI)) |
| 103 | tpm_ppi_acpi_fill_ssdt(dev); |
| 104 | |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 105 | acpigen_pop_len(); /* Device */ |
| 106 | } |
| 107 | |
| 108 | static const char *crb_tpm_acpi_name(const struct device *dev) |
| 109 | { |
| 110 | return "TPM"; |
| 111 | } |
| 112 | |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 113 | #if CONFIG(GENERATE_SMBIOS_TABLES) && CONFIG(TPM2) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 114 | static tpm_result_t tpm_get_cap(uint32_t property, uint32_t *value) |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 115 | { |
| 116 | TPMS_CAPABILITY_DATA cap_data; |
| 117 | int i; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 118 | tpm_result_t rc; |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 119 | |
| 120 | if (!value) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 121 | return TPM_CB_INVALID_ARG; |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 122 | |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 123 | rc = tlcl2_get_capability(TPM_CAP_TPM_PROPERTIES, property, 1, &cap_data); |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 124 | |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 125 | if (rc) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 126 | return rc; |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 127 | |
| 128 | for (i = 0 ; i < cap_data.data.tpmProperties.count; i++) { |
| 129 | if (cap_data.data.tpmProperties.tpmProperty[i].property == property) { |
| 130 | *value = cap_data.data.tpmProperties.tpmProperty[i].value; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 131 | return TPM_SUCCESS; |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 135 | return TPM_CB_FAIL; |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int smbios_write_type43_tpm(struct device *dev, int *handle, unsigned long *current) |
| 139 | { |
Sergii Dmytruk | 1a90314 | 2024-04-12 15:47:04 +0300 | [diff] [blame] | 140 | struct crb_tpm_info info; |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 141 | uint32_t tpm_manuf, tpm_family; |
| 142 | uint32_t fw_ver1, fw_ver2; |
| 143 | uint8_t major_spec_ver, minor_spec_ver; |
| 144 | |
Sergii Dmytruk | 47e9e8c | 2022-11-02 00:50:03 +0200 | [diff] [blame] | 145 | if (tlcl_get_family() == TPM_1) |
| 146 | return 0; |
| 147 | |
Sergii Dmytruk | 1a90314 | 2024-04-12 15:47:04 +0300 | [diff] [blame] | 148 | crb_tpm_get_info(&info); |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 149 | |
| 150 | /* If any of these have invalid values, assume TPM not present or disabled */ |
| 151 | if (info.vendor_id == 0 || info.vendor_id == 0xFFFF || |
| 152 | info.device_id == 0 || info.device_id == 0xFFFF) { |
| 153 | printk(BIOS_DEBUG, "%s: Invalid Vendor ID/Device ID\n", __func__); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* Vendor ID is the value returned by TPM2_GetCapabiltiy TPM_PT_MANUFACTURER */ |
| 158 | if (tpm_get_cap(TPM_PT_MANUFACTURER, &tpm_manuf)) { |
| 159 | printk(BIOS_DEBUG, "TPM2_GetCap TPM_PT_MANUFACTURER failed\n"); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | tpm_manuf = be32toh(tpm_manuf); |
| 164 | |
| 165 | if (tpm_get_cap(TPM_PT_FIRMWARE_VERSION_1, &fw_ver1)) { |
| 166 | printk(BIOS_DEBUG, "TPM2_GetCap TPM_PT_FIRMWARE_VERSION_1 failed\n"); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | if (tpm_get_cap(TPM_PT_FIRMWARE_VERSION_2, &fw_ver2)) { |
| 171 | printk(BIOS_DEBUG, "TPM2_GetCap TPM_PT_FIRMWARE_VERSION_2 failed\n"); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | if (tpm_get_cap(TPM_PT_FAMILY_INDICATOR, &tpm_family)) { |
| 176 | printk(BIOS_DEBUG, "TPM2_GetCap TPM_PT_FAMILY_INDICATOR failed\n"); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | tpm_family = be32toh(tpm_family); |
| 181 | |
| 182 | if (!strncmp((char *)&tpm_family, "2.0", 4)) { |
| 183 | major_spec_ver = 2; |
| 184 | minor_spec_ver = 0; |
| 185 | } else { |
| 186 | printk(BIOS_ERR, "%s: Invalid TPM family\n", __func__); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | return smbios_write_type43(current, handle, tpm_manuf, major_spec_ver, minor_spec_ver, |
| 191 | fw_ver1, fw_ver2, tis_get_dev_name(&info), |
| 192 | SMBIOS_TPM_DEVICE_CHARACTERISTICS_NOT_SUPPORTED, 0); |
| 193 | } |
| 194 | #endif |
| 195 | |
Bill XIE | ac136250 | 2022-07-08 16:53:21 +0800 | [diff] [blame] | 196 | static struct device_operations __maybe_unused crb_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 197 | .read_resources = noop_read_resources, |
| 198 | .set_resources = noop_set_resources, |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 199 | #if CONFIG(HAVE_ACPI_TABLES) |
| 200 | .acpi_name = crb_tpm_acpi_name, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 201 | .acpi_fill_ssdt = crb_tpm_fill_ssdt, |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 202 | #endif |
Michał Żygowski | 283e587 | 2022-05-04 14:12:17 +0200 | [diff] [blame] | 203 | #if CONFIG(GENERATE_SMBIOS_TABLES) && CONFIG(TPM2) |
| 204 | .get_smbios_data = smbios_write_type43_tpm, |
| 205 | #endif |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | static void enable_dev(struct device *dev) |
| 209 | { |
Kyösti Mälkki | 35a047c | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 210 | #if !DEVTREE_EARLY |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 211 | dev->ops = &crb_ops; |
Kyösti Mälkki | 35a047c | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 212 | #endif |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Kyösti Mälkki | 35a047c | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 215 | struct chip_operations drivers_crb_ops = { |
Nicholas Sudsgaard | bfb11be | 2024-01-30 09:53:46 +0900 | [diff] [blame] | 216 | .name = "CRB TPM", |
Kyösti Mälkki | 35a047c | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 217 | .enable_dev = enable_dev |
| 218 | }; |