2 * Copyright (C) 2004 IBM Corporation
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2 of the
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
26 #include <linux/sched.h>
27 #include <linux/poll.h>
28 #include <linux/spinlock.h>
32 TPM_MINOR = 224, /* officially assigned */
34 TPM_NUM_DEVICES = 256,
35 TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
38 /* PCI configuration addresses */
39 enum tpm_pci_config_addr {
40 PCI_GEN_PMCON_1 = 0xA0,
48 TPM_INTERUPT_REG = 0x0A,
49 TPM_BASE_ADDR_LO = 0x08,
50 TPM_BASE_ADDR_HI = 0x09,
51 TPM_UNLOCK_VALUE = 0x55,
52 TPM_LOCK_VALUE = 0xAA,
53 TPM_DISABLE_INTERUPT_VALUE = 0x00
57 static LIST_HEAD(tpm_chip_list);
58 static DEFINE_SPINLOCK(driver_lock);
59 static int dev_mask[TPM_NUM_MASK_ENTRIES];
61 static void user_reader_timeout(unsigned long ptr)
63 struct tpm_chip *chip = (struct tpm_chip *) ptr;
65 down(&chip->buffer_mutex);
66 atomic_set(&chip->data_pending, 0);
67 memset(chip->data_buffer, 0, TPM_BUFSIZE);
68 up(&chip->buffer_mutex);
72 * Initialize the LPC bus and enable the TPM ports
74 int tpm_lpc_bus_init(struct pci_dev *pci_dev, u16 base)
79 switch (pci_dev->vendor) {
80 case PCI_VENDOR_ID_INTEL:
81 switch (pci_dev->device) {
82 case PCI_DEVICE_ID_INTEL_82801CA_12:
83 case PCI_DEVICE_ID_INTEL_82801DB_12:
87 /* init ICH (enable LPC) */
88 pci_read_config_dword(pci_dev, PCI_GEN1_DEC, &lpcenable);
89 lpcenable |= 0x20000000;
90 pci_write_config_dword(pci_dev, PCI_GEN1_DEC, lpcenable);
93 pci_read_config_dword(pci_dev, PCI_GEN1_DEC,
95 if ((lpcenable & 0x20000000) == 0) {
96 dev_err(&pci_dev->dev,
97 "cannot enable LPC\n");
102 /* initialize TPM registers */
103 pci_read_config_dword(pci_dev, PCI_GEN2_DEC, &tmp);
106 tmp = (tmp & 0xFFFF0000) | (base & 0xFFF0);
109 (tmp & 0xFFFF0000) | (base & 0xFFF0) |
112 pci_write_config_dword(pci_dev, PCI_GEN2_DEC, tmp);
115 pci_read_config_dword(pci_dev, PCI_GEN_PMCON_1,
117 tmp |= 0x00000004; /* enable CLKRUN */
118 pci_write_config_dword(pci_dev, PCI_GEN_PMCON_1,
122 case PCI_VENDOR_ID_AMD:
127 tpm_write_index(TPM_LOCK_REG, TPM_UNLOCK_VALUE);
128 tpm_write_index(TPM_INTERUPT_REG, TPM_DISABLE_INTERUPT_VALUE);
129 tpm_write_index(TPM_BASE_ADDR_LO, base);
130 tpm_write_index(TPM_BASE_ADDR_HI, (base & 0xFF00) >> 8);
131 tpm_write_index(TPM_LOCK_REG, TPM_LOCK_VALUE);
136 EXPORT_SYMBOL_GPL(tpm_lpc_bus_init);
139 * Internal kernel interface to transmit TPM commands
141 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
149 native_size = (__force __be32 *) (buf + 2);
150 count = be32_to_cpu(*native_size);
154 if (count > bufsiz) {
155 dev_err(&chip->pci_dev->dev,
156 "invalid count value %x %zx \n", count, bufsiz);
160 down(&chip->tpm_mutex);
162 if ((len = chip->vendor->send(chip, (u8 *) buf, count)) < 0) {
163 dev_err(&chip->pci_dev->dev,
164 "tpm_transmit: tpm_send: error %zd\n", len);
168 stop = jiffies + 2 * 60 * HZ;
170 u8 status = inb(chip->vendor->base + 1);
171 if ((status & chip->vendor->req_complete_mask) ==
172 chip->vendor->req_complete_val) {
175 msleep(TPM_TIMEOUT); /* CHECK */
177 } while (time_before(jiffies, stop));
180 chip->vendor->cancel(chip);
181 dev_err(&chip->pci_dev->dev, "Time expired\n");
182 up(&chip->tpm_mutex);
186 len = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
188 dev_err(&chip->pci_dev->dev,
189 "tpm_transmit: tpm_recv: error %zd\n", len);
190 up(&chip->tpm_mutex);
194 #define TPM_DIGEST_SIZE 20
195 #define CAP_PCR_RESULT_SIZE 18
196 static const u8 cap_pcr[] = {
197 0, 193, /* TPM_TAG_RQU_COMMAND */
198 0, 0, 0, 22, /* length */
199 0, 0, 0, 101, /* TPM_ORD_GetCapability */
205 #define READ_PCR_RESULT_SIZE 30
206 static const u8 pcrread[] = {
207 0, 193, /* TPM_TAG_RQU_COMMAND */
208 0, 0, 0, 14, /* length */
209 0, 0, 0, 21, /* TPM_ORD_PcrRead */
210 0, 0, 0, 0 /* PCR index */
213 static ssize_t show_pcrs(struct device *dev, struct device_attribute *attr, char *buf)
215 u8 data[READ_PCR_RESULT_SIZE];
217 int i, j, index, num_pcrs;
220 struct tpm_chip *chip =
221 pci_get_drvdata(container_of(dev, struct pci_dev, dev));
225 memcpy(data, cap_pcr, sizeof(cap_pcr));
226 if ((len = tpm_transmit(chip, data, sizeof(data)))
227 < CAP_PCR_RESULT_SIZE)
230 num_pcrs = be32_to_cpu(*((__force __be32 *) (data + 14)));
232 for (i = 0; i < num_pcrs; i++) {
233 memcpy(data, pcrread, sizeof(pcrread));
234 index = cpu_to_be32(i);
235 memcpy(data + 10, &index, 4);
236 if ((len = tpm_transmit(chip, data, sizeof(data)))
237 < READ_PCR_RESULT_SIZE)
239 str += sprintf(str, "PCR-%02d: ", i);
240 for (j = 0; j < TPM_DIGEST_SIZE; j++)
241 str += sprintf(str, "%02X ", *(data + 10 + j));
242 str += sprintf(str, "\n");
247 static DEVICE_ATTR(pcrs, S_IRUGO, show_pcrs, NULL);
249 #define READ_PUBEK_RESULT_SIZE 314
250 static const u8 readpubek[] = {
251 0, 193, /* TPM_TAG_RQU_COMMAND */
252 0, 0, 0, 30, /* length */
253 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
256 static ssize_t show_pubek(struct device *dev, struct device_attribute *attr, char *buf)
258 u8 data[READ_PUBEK_RESULT_SIZE];
264 struct tpm_chip *chip =
265 pci_get_drvdata(container_of(dev, struct pci_dev, dev));
269 memcpy(data, readpubek, sizeof(readpubek));
270 memset(data + sizeof(readpubek), 0, 20); /* zero nonce */
272 if ((len = tpm_transmit(chip, data, sizeof(data))) <
273 READ_PUBEK_RESULT_SIZE)
277 ignore header 10 bytes
278 algorithm 32 bits (1 == RSA )
281 parameters (RSA 12->bytes: keybit, #primes, expbit)
284 ignore checksum 20 bytes
287 native_val = (__force __be32 *) (data + 34);
291 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
292 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
293 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
294 "Modulus length: %d\nModulus: \n",
295 data[10], data[11], data[12], data[13], data[14],
296 data[15], data[16], data[17], data[22], data[23],
297 data[24], data[25], data[26], data[27], data[28],
298 data[29], data[30], data[31], data[32], data[33],
299 be32_to_cpu(*native_val)
302 for (i = 0; i < 256; i++) {
303 str += sprintf(str, "%02X ", data[i + 39]);
304 if ((i + 1) % 16 == 0)
305 str += sprintf(str, "\n");
310 static DEVICE_ATTR(pubek, S_IRUGO, show_pubek, NULL);
312 #define CAP_VER_RESULT_SIZE 18
313 static const u8 cap_version[] = {
314 0, 193, /* TPM_TAG_RQU_COMMAND */
315 0, 0, 0, 18, /* length */
316 0, 0, 0, 101, /* TPM_ORD_GetCapability */
321 #define CAP_MANUFACTURER_RESULT_SIZE 18
322 static const u8 cap_manufacturer[] = {
323 0, 193, /* TPM_TAG_RQU_COMMAND */
324 0, 0, 0, 22, /* length */
325 0, 0, 0, 101, /* TPM_ORD_GetCapability */
331 static ssize_t show_caps(struct device *dev, struct device_attribute *attr, char *buf)
333 u8 data[READ_PUBEK_RESULT_SIZE];
337 struct tpm_chip *chip =
338 pci_get_drvdata(container_of(dev, struct pci_dev, dev));
342 memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
344 if ((len = tpm_transmit(chip, data, sizeof(data))) <
345 CAP_MANUFACTURER_RESULT_SIZE)
348 str += sprintf(str, "Manufacturer: 0x%x\n",
349 be32_to_cpu(*(data + 14)));
351 memcpy(data, cap_version, sizeof(cap_version));
353 if ((len = tpm_transmit(chip, data, sizeof(data))) <
358 sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
359 (int) data[14], (int) data[15], (int) data[16],
365 static DEVICE_ATTR(caps, S_IRUGO, show_caps, NULL);
368 * Device file system interface to the TPM
370 int tpm_open(struct inode *inode, struct file *file)
372 int rc = 0, minor = iminor(inode);
373 struct tpm_chip *chip = NULL, *pos;
375 spin_lock(&driver_lock);
377 list_for_each_entry(pos, &tpm_chip_list, list) {
378 if (pos->vendor->miscdev.minor == minor) {
389 if (chip->num_opens) {
390 dev_dbg(&chip->pci_dev->dev,
391 "Another process owns this TPM\n");
397 pci_dev_get(chip->pci_dev);
399 spin_unlock(&driver_lock);
401 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
402 if (chip->data_buffer == NULL) {
404 pci_dev_put(chip->pci_dev);
408 atomic_set(&chip->data_pending, 0);
410 file->private_data = chip;
414 spin_unlock(&driver_lock);
418 EXPORT_SYMBOL_GPL(tpm_open);
420 int tpm_release(struct inode *inode, struct file *file)
422 struct tpm_chip *chip = file->private_data;
424 file->private_data = NULL;
426 spin_lock(&driver_lock);
428 spin_unlock(&driver_lock);
430 down(&chip->timer_manipulation_mutex);
431 if (timer_pending(&chip->user_read_timer))
432 del_singleshot_timer_sync(&chip->user_read_timer);
433 else if (timer_pending(&chip->device_timer))
434 del_singleshot_timer_sync(&chip->device_timer);
435 up(&chip->timer_manipulation_mutex);
437 kfree(chip->data_buffer);
438 atomic_set(&chip->data_pending, 0);
440 pci_dev_put(chip->pci_dev);
444 EXPORT_SYMBOL_GPL(tpm_release);
446 ssize_t tpm_write(struct file * file, const char __user * buf,
447 size_t size, loff_t * off)
449 struct tpm_chip *chip = file->private_data;
450 int in_size = size, out_size;
452 /* cannot perform a write until the read has cleared
453 either via tpm_read or a user_read_timer timeout */
454 while (atomic_read(&chip->data_pending) != 0)
457 down(&chip->buffer_mutex);
459 if (in_size > TPM_BUFSIZE)
460 in_size = TPM_BUFSIZE;
463 (chip->data_buffer, (void __user *) buf, in_size)) {
464 up(&chip->buffer_mutex);
468 /* atomic tpm command send and result receive */
469 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
471 atomic_set(&chip->data_pending, out_size);
472 up(&chip->buffer_mutex);
474 /* Set a timeout by which the reader must come claim the result */
475 down(&chip->timer_manipulation_mutex);
476 init_timer(&chip->user_read_timer);
477 chip->user_read_timer.function = user_reader_timeout;
478 chip->user_read_timer.data = (unsigned long) chip;
479 chip->user_read_timer.expires = jiffies + (60 * HZ);
480 add_timer(&chip->user_read_timer);
481 up(&chip->timer_manipulation_mutex);
486 EXPORT_SYMBOL_GPL(tpm_write);
488 ssize_t tpm_read(struct file * file, char __user * buf,
489 size_t size, loff_t * off)
491 struct tpm_chip *chip = file->private_data;
492 int ret_size = -ENODATA;
494 if (atomic_read(&chip->data_pending) != 0) { /* Result available */
495 down(&chip->timer_manipulation_mutex);
496 del_singleshot_timer_sync(&chip->user_read_timer);
497 up(&chip->timer_manipulation_mutex);
499 down(&chip->buffer_mutex);
501 ret_size = atomic_read(&chip->data_pending);
502 atomic_set(&chip->data_pending, 0);
504 if (ret_size == 0) /* timeout just occurred */
506 else if (ret_size > 0) { /* relay data */
510 if (copy_to_user((void __user *) buf,
511 chip->data_buffer, ret_size)) {
515 up(&chip->buffer_mutex);
521 EXPORT_SYMBOL_GPL(tpm_read);
523 void __devexit tpm_remove(struct pci_dev *pci_dev)
525 struct tpm_chip *chip = pci_get_drvdata(pci_dev);
528 dev_err(&pci_dev->dev, "No device data found\n");
532 spin_lock(&driver_lock);
534 list_del(&chip->list);
536 spin_unlock(&driver_lock);
538 pci_set_drvdata(pci_dev, NULL);
539 misc_deregister(&chip->vendor->miscdev);
541 device_remove_file(&pci_dev->dev, &dev_attr_pubek);
542 device_remove_file(&pci_dev->dev, &dev_attr_pcrs);
543 device_remove_file(&pci_dev->dev, &dev_attr_caps);
545 pci_disable_device(pci_dev);
547 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &= !(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
551 pci_dev_put(pci_dev);
554 EXPORT_SYMBOL_GPL(tpm_remove);
556 static u8 savestate[] = {
557 0, 193, /* TPM_TAG_RQU_COMMAND */
558 0, 0, 0, 10, /* blob length (in bytes) */
559 0, 0, 0, 152 /* TPM_ORD_SaveState */
563 * We are about to suspend. Save the TPM state
564 * so that it can be restored.
566 int tpm_pm_suspend(struct pci_dev *pci_dev, pm_message_t pm_state)
568 struct tpm_chip *chip = pci_get_drvdata(pci_dev);
572 tpm_transmit(chip, savestate, sizeof(savestate));
576 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
579 * Resume from a power safe. The BIOS already restored
582 int tpm_pm_resume(struct pci_dev *pci_dev)
584 struct tpm_chip *chip = pci_get_drvdata(pci_dev);
589 spin_lock(&driver_lock);
590 tpm_lpc_bus_init(pci_dev, chip->vendor->base);
591 spin_unlock(&driver_lock);
596 EXPORT_SYMBOL_GPL(tpm_pm_resume);
599 * Called from tpm_<specific>.c probe function only for devices
600 * the driver has determined it should claim. Prior to calling
601 * this function the specific probe function has called pci_enable_device
602 * upon errant exit from this function specific probe function should call
605 int tpm_register_hardware(struct pci_dev *pci_dev,
606 struct tpm_vendor_specific *entry)
609 struct tpm_chip *chip;
612 /* Driver specific per-device data */
613 chip = kmalloc(sizeof(*chip), GFP_KERNEL);
617 memset(chip, 0, sizeof(struct tpm_chip));
619 init_MUTEX(&chip->buffer_mutex);
620 init_MUTEX(&chip->tpm_mutex);
621 init_MUTEX(&chip->timer_manipulation_mutex);
622 INIT_LIST_HEAD(&chip->list);
624 chip->vendor = entry;
628 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
629 for (j = 0; j < 8 * sizeof(int); j++)
630 if ((dev_mask[i] & (1 << j)) == 0) {
632 i * TPM_NUM_MASK_ENTRIES + j;
633 dev_mask[i] |= 1 << j;
634 goto dev_num_search_complete;
637 dev_num_search_complete:
638 if (chip->dev_num < 0) {
639 dev_err(&pci_dev->dev,
640 "No available tpm device numbers\n");
643 } else if (chip->dev_num == 0)
644 chip->vendor->miscdev.minor = TPM_MINOR;
646 chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
648 snprintf(devname, sizeof(devname), "%s%d", "tpm", chip->dev_num);
649 chip->vendor->miscdev.name = devname;
651 chip->vendor->miscdev.dev = &(pci_dev->dev);
652 chip->pci_dev = pci_dev_get(pci_dev);
654 if (misc_register(&chip->vendor->miscdev)) {
655 dev_err(&chip->pci_dev->dev,
656 "unable to misc_register %s, minor %d\n",
657 chip->vendor->miscdev.name,
658 chip->vendor->miscdev.minor);
659 pci_dev_put(pci_dev);
661 dev_mask[i] &= !(1 << j);
665 pci_set_drvdata(pci_dev, chip);
667 list_add(&chip->list, &tpm_chip_list);
669 device_create_file(&pci_dev->dev, &dev_attr_pubek);
670 device_create_file(&pci_dev->dev, &dev_attr_pcrs);
671 device_create_file(&pci_dev->dev, &dev_attr_caps);
676 EXPORT_SYMBOL_GPL(tpm_register_hardware);
678 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
679 MODULE_DESCRIPTION("TPM Driver");
680 MODULE_VERSION("2.0");
681 MODULE_LICENSE("GPL");