blob: 5c1a6df1cf58c658630010ea7ed771fd92b848d2 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Christian Walter7706a042019-07-05 19:46:30 +02003
Christian Walter7706a042019-07-05 19:46:30 +02004#include <console/console.h>
5#include <security/tpm/tis.h>
6#include <arch/acpigen.h>
7#include <device/device.h>
Christian Walter542268192019-07-16 20:07:36 +02008#include <drivers/intel/ptt/ptt.h>
Christian Walter7706a042019-07-05 19:46:30 +02009
10#include "tpm.h"
11#include "chip.h"
12
Arthur Heymans0ca944b2019-11-20 19:51:06 +010013static unsigned int tpm_is_open;
Christian Walter7706a042019-07-05 19:46:30 +020014
15static 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
24static 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
35int tis_open(void)
36{
Arthur Heymans0ca944b2019-11-20 19:51:06 +010037 if (tpm_is_open) {
Christian Walter7706a042019-07-05 19:46:30 +020038 printk(BIOS_ERR, "%s called twice.\n", __func__);
39 return -1;
40 }
41
Christian Walter542268192019-07-16 20:07:36 +020042 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 Walter7706a042019-07-05 19:46:30 +020050 return 0;
51}
52
53int tis_close(void)
54{
Arthur Heymans0ca944b2019-11-20 19:51:06 +010055 if (tpm_is_open) {
Christian Walter7706a042019-07-05 19:46:30 +020056
57 /*
58 * Do we need to do something here, like waiting for a
59 * transaction to stop?
60 */
Arthur Heymans0ca944b2019-11-20 19:51:06 +010061 tpm_is_open = 0;
Christian Walter7706a042019-07-05 19:46:30 +020062 }
63
64 return 0;
65}
66
67int 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
84int 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 Walter7706a042019-07-05 19:46:30 +020096static 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 Rudolphc83bab62019-12-13 12:16:06 +0100110 acpi_device_write_uid(dev);
Christian Walter7706a042019-07-05 19:46:30 +0200111
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
124static const char *crb_tpm_acpi_name(const struct device *dev)
125{
126 return "TPM";
127}
128
Kyösti Mälkki35a047c2019-11-05 18:38:00 +0200129static struct device_operations __unused crb_ops = {
Christian Walter7706a042019-07-05 19:46:30 +0200130 .read_resources = DEVICE_NOOP,
131 .set_resources = DEVICE_NOOP,
132#if CONFIG(HAVE_ACPI_TABLES)
133 .acpi_name = crb_tpm_acpi_name,
Nico Huber68680dd2020-03-31 17:34:52 +0200134 .acpi_fill_ssdt = crb_tpm_fill_ssdt,
Christian Walter7706a042019-07-05 19:46:30 +0200135#endif
136
137};
138
139static void enable_dev(struct device *dev)
140{
Kyösti Mälkki35a047c2019-11-05 18:38:00 +0200141#if !DEVTREE_EARLY
Christian Walter7706a042019-07-05 19:46:30 +0200142 dev->ops = &crb_ops;
Kyösti Mälkki35a047c2019-11-05 18:38:00 +0200143#endif
Christian Walter7706a042019-07-05 19:46:30 +0200144}
145
Kyösti Mälkki35a047c2019-11-05 18:38:00 +0200146struct chip_operations drivers_crb_ops = {
147 CHIP_NAME("CRB TPM")
148 .enable_dev = enable_dev
149};