Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 3 | |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 4 | #include <console/console.h> |
| 5 | #include <security/tpm/tis.h> |
| 6 | #include <arch/acpigen.h> |
| 7 | #include <device/device.h> |
Christian Walter | 54226819 | 2019-07-16 20:07:36 +0200 | [diff] [blame] | 8 | #include <drivers/intel/ptt/ptt.h> |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 9 | |
| 10 | #include "tpm.h" |
| 11 | #include "chip.h" |
| 12 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 13 | static unsigned int tpm_is_open; |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 14 | |
| 15 | static const struct { |
| 16 | uint16_t vid; |
| 17 | uint16_t did; |
| 18 | const char *device_name; |
| 19 | } dev_map[] = { |
| 20 | {0x1ae0, 0x0028, "CR50"}, |
| 21 | {0xa13a, 0x8086, "Intel iTPM"} |
| 22 | }; |
| 23 | |
| 24 | static const char *tis_get_dev_name(struct tpm2_info *info) |
| 25 | { |
| 26 | int i; |
| 27 | |
| 28 | for (i = 0; i < ARRAY_SIZE(dev_map); i++) |
| 29 | if ((dev_map[i].vid == info->vendor_id) && (dev_map[i].did == info->device_id)) |
| 30 | return dev_map[i].device_name; |
| 31 | return "Unknown"; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | int tis_open(void) |
| 36 | { |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 37 | if (tpm_is_open) { |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 38 | printk(BIOS_ERR, "%s called twice.\n", __func__); |
| 39 | return -1; |
| 40 | } |
| 41 | |
Christian Walter | 54226819 | 2019-07-16 20:07:36 +0200 | [diff] [blame] | 42 | if (CONFIG(HAVE_INTEL_PTT)) { |
| 43 | if (!ptt_active()) { |
| 44 | printk(BIOS_ERR, "%s: Intel PTT is not active.\n", __func__); |
| 45 | return -1; |
| 46 | } |
| 47 | printk(BIOS_DEBUG, "%s: Intel PTT is active.\n", __func__); |
| 48 | } |
| 49 | |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | int tis_close(void) |
| 54 | { |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 55 | if (tpm_is_open) { |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Do we need to do something here, like waiting for a |
| 59 | * transaction to stop? |
| 60 | */ |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 61 | tpm_is_open = 0; |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int tis_init(void) |
| 68 | { |
| 69 | struct tpm2_info info; |
| 70 | |
| 71 | // Wake TPM up (if necessary) |
| 72 | if (tpm2_init() != 0) |
| 73 | return -1; |
| 74 | |
| 75 | tpm2_get_info(&info); |
| 76 | |
| 77 | printk(BIOS_INFO, "Initialized TPM device %s revision %d\n", tis_get_dev_name(&info), |
| 78 | info.revision); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, uint8_t *recvbuf, size_t *rbuf_len) |
| 85 | { |
| 86 | int len = tpm2_process_command(sendbuf, sbuf_size, recvbuf, *rbuf_len); |
| 87 | |
| 88 | if (len == 0) |
| 89 | return -1; |
| 90 | |
| 91 | *rbuf_len = len; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 96 | static void crb_tpm_fill_ssdt(struct device *dev) |
| 97 | { |
| 98 | const char *path = acpi_device_path(dev); |
| 99 | if (!path) { |
| 100 | path = "\\_SB_.TPM"; |
| 101 | printk(BIOS_DEBUG, "Using default TPM2 ACPI path: '%s'\n", path); |
| 102 | } |
| 103 | |
| 104 | /* Device */ |
| 105 | acpigen_write_device(path); |
| 106 | |
| 107 | acpigen_write_name_string("_HID", "MSFT0101"); |
| 108 | acpigen_write_name_string("_CID", "MSFT0101"); |
| 109 | |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 110 | acpi_device_write_uid(dev); |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 111 | |
| 112 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 113 | |
| 114 | /* Resources */ |
| 115 | acpigen_write_name("_CRS"); |
| 116 | acpigen_write_resourcetemplate_header(); |
| 117 | acpigen_write_mem32fixed(1, TPM_CRB_BASE_ADDRESS, 0x5000); |
| 118 | |
| 119 | acpigen_write_resourcetemplate_footer(); |
| 120 | |
| 121 | acpigen_pop_len(); /* Device */ |
| 122 | } |
| 123 | |
| 124 | static const char *crb_tpm_acpi_name(const struct device *dev) |
| 125 | { |
| 126 | return "TPM"; |
| 127 | } |
| 128 | |
Kyösti Mälkki | 35a047c | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 129 | static struct device_operations __unused crb_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame^] | 130 | .read_resources = noop_read_resources, |
| 131 | .set_resources = noop_set_resources, |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 132 | #if CONFIG(HAVE_ACPI_TABLES) |
| 133 | .acpi_name = crb_tpm_acpi_name, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 134 | .acpi_fill_ssdt = crb_tpm_fill_ssdt, |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 135 | #endif |
| 136 | |
| 137 | }; |
| 138 | |
| 139 | static void enable_dev(struct device *dev) |
| 140 | { |
Kyösti Mälkki | 35a047c | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 141 | #if !DEVTREE_EARLY |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 142 | dev->ops = &crb_ops; |
Kyösti Mälkki | 35a047c | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 143 | #endif |
Christian Walter | 7706a04 | 2019-07-05 19:46:30 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Kyösti Mälkki | 35a047c | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 146 | struct chip_operations drivers_crb_ops = { |
| 147 | CHIP_NAME("CRB TPM") |
| 148 | .enable_dev = enable_dev |
| 149 | }; |