2 * Copyright (C) 2012 IBM Corporation
4 * Author: Ashley Lai <ashleydlai@gmail.com>
6 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8 * Device driver for TCG/TCPA TPM (trusted platform module).
9 * Specifications at www.trustedcomputinggroup.org
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, version 2 of the
18 #include <linux/dma-mapping.h>
19 #include <linux/dmapool.h>
20 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/interrupt.h>
27 #include <linux/wait.h>
31 #include "tpm_ibmvtpm.h"
33 static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm";
35 static const struct vio_device_id tpm_ibmvtpm_device_table[] = {
36 { "IBM,vtpm", "IBM,vtpm"},
39 MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table);
42 * ibmvtpm_send_crq_word() - Send a CRQ request
43 * @vdev: vio device struct
44 * @w1: pre-constructed first word of tpm crq (second word is reserved)
50 static int ibmvtpm_send_crq_word(struct vio_dev *vdev, u64 w1)
52 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, 0);
56 * ibmvtpm_send_crq() - Send a CRQ request
58 * @vdev: vio device struct
64 * The ibmvtpm crq is defined as follows:
66 * Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
67 * -----------------------------------------------------------------------
68 * Word0 | Valid | Type | Length | Data
69 * -----------------------------------------------------------------------
71 * -----------------------------------------------------------------------
73 * Which matches the following structure (on bigendian host):
75 * struct ibmvtpm_crq {
81 * } __attribute__((packed, aligned(8)));
83 * However, the value is passed in a register so just compute the numeric value
84 * to load into the register avoiding byteswap altogether. Endian only affects
85 * memory loads and stores - registers are internally represented the same.
88 * 0 (H_SUCCESS) - Success
91 static int ibmvtpm_send_crq(struct vio_dev *vdev,
92 u8 valid, u8 msg, u16 len, u32 data)
94 u64 w1 = ((u64)valid << 56) | ((u64)msg << 48) | ((u64)len << 32) |
96 return ibmvtpm_send_crq_word(vdev, w1);
100 * tpm_ibmvtpm_recv - Receive data after send
102 * @chip: tpm chip struct
103 * @buf: buffer to read
104 * @count: size of buffer
107 * Number of bytes read
109 static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
111 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
115 if (!ibmvtpm->rtce_buf) {
116 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
120 sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
124 len = ibmvtpm->res_len;
127 dev_err(ibmvtpm->dev,
128 "Invalid size in recv: count=%zd, crq_size=%d\n",
133 spin_lock(&ibmvtpm->rtce_lock);
134 memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len);
135 memset(ibmvtpm->rtce_buf, 0, len);
136 ibmvtpm->res_len = 0;
137 spin_unlock(&ibmvtpm->rtce_lock);
142 * tpm_ibmvtpm_send() - Send a TPM command
143 * @chip: tpm chip struct
144 * @buf: buffer contains data to send
145 * @count: size of buffer
151 static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
153 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
156 if (!ibmvtpm->rtce_buf) {
157 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
161 if (count > ibmvtpm->rtce_size) {
162 dev_err(ibmvtpm->dev,
163 "Invalid size in send: count=%zd, rtce_size=%d\n",
164 count, ibmvtpm->rtce_size);
168 if (ibmvtpm->tpm_processing_cmd) {
169 dev_info(ibmvtpm->dev,
170 "Need to wait for TPM to finish\n");
171 /* wait for previous command to finish */
172 sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
177 spin_lock(&ibmvtpm->rtce_lock);
178 ibmvtpm->res_len = 0;
179 memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count);
182 * set the processing flag before the Hcall, since we may get the
183 * result (interrupt) before even being able to check rc.
185 ibmvtpm->tpm_processing_cmd = true;
187 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
188 IBMVTPM_VALID_CMD, VTPM_TPM_COMMAND,
189 count, ibmvtpm->rtce_dma_handle);
190 if (rc != H_SUCCESS) {
191 dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc);
193 ibmvtpm->tpm_processing_cmd = false;
197 spin_unlock(&ibmvtpm->rtce_lock);
201 static void tpm_ibmvtpm_cancel(struct tpm_chip *chip)
206 static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
212 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
214 * @ibmvtpm: vtpm device struct
218 * Non-zero on failure.
220 static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
224 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
225 IBMVTPM_VALID_CMD, VTPM_GET_RTCE_BUFFER_SIZE, 0, 0);
227 dev_err(ibmvtpm->dev,
228 "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc);
234 * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
235 * - Note that this is vtpm version and not tpm version
237 * @ibmvtpm: vtpm device struct
241 * Non-zero on failure.
243 static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm)
247 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
248 IBMVTPM_VALID_CMD, VTPM_GET_VERSION, 0, 0);
250 dev_err(ibmvtpm->dev,
251 "ibmvtpm_crq_get_version failed rc=%d\n", rc);
257 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
258 * @ibmvtpm: vtpm device struct
262 * Non-zero on failure.
264 static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm)
268 rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_COMP_CMD);
270 dev_err(ibmvtpm->dev,
271 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc);
277 * ibmvtpm_crq_send_init - Send a CRQ initialize message
278 * @ibmvtpm: vtpm device struct
282 * Non-zero on failure.
284 static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm)
288 rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_CMD);
290 dev_err(ibmvtpm->dev,
291 "ibmvtpm_crq_send_init failed rc=%d\n", rc);
297 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
298 * @vdev: vio device struct
302 static int tpm_ibmvtpm_remove(struct vio_dev *vdev)
304 struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
305 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
308 tpm_chip_unregister(chip);
310 free_irq(vdev->irq, ibmvtpm);
315 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
316 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
318 dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle,
319 CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL);
320 free_page((unsigned long)ibmvtpm->crq_queue.crq_addr);
322 if (ibmvtpm->rtce_buf) {
323 dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle,
324 ibmvtpm->rtce_size, DMA_BIDIRECTIONAL);
325 kfree(ibmvtpm->rtce_buf);
329 /* For tpm_ibmvtpm_get_desired_dma */
330 dev_set_drvdata(&vdev->dev, NULL);
336 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
337 * @vdev: vio device struct
340 * Number of bytes the driver needs to DMA map.
342 static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev)
344 struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
345 struct ibmvtpm_dev *ibmvtpm;
348 * ibmvtpm initializes at probe time, so the data we are
349 * asking for may not be set yet. Estimate that 4K required
350 * for TCE-mapped buffer in addition to CRQ.
353 ibmvtpm = dev_get_drvdata(&chip->dev);
355 return CRQ_RES_BUF_SIZE + PAGE_SIZE;
357 return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size;
361 * tpm_ibmvtpm_suspend - Suspend
362 * @dev: device struct
366 static int tpm_ibmvtpm_suspend(struct device *dev)
368 struct tpm_chip *chip = dev_get_drvdata(dev);
369 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
372 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
373 IBMVTPM_VALID_CMD, VTPM_PREPARE_TO_SUSPEND, 0, 0);
375 dev_err(ibmvtpm->dev,
376 "tpm_ibmvtpm_suspend failed rc=%d\n", rc);
382 * ibmvtpm_reset_crq - Reset CRQ
384 * @ibmvtpm: ibm vtpm struct
388 * Non-zero on failure.
390 static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm)
397 rc = plpar_hcall_norets(H_FREE_CRQ,
398 ibmvtpm->vdev->unit_address);
399 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
401 memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE);
402 ibmvtpm->crq_queue.index = 0;
404 return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address,
405 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
409 * tpm_ibmvtpm_resume - Resume from suspend
411 * @dev: device struct
415 static int tpm_ibmvtpm_resume(struct device *dev)
417 struct tpm_chip *chip = dev_get_drvdata(dev);
418 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
424 rc = plpar_hcall_norets(H_ENABLE_CRQ,
425 ibmvtpm->vdev->unit_address);
426 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
429 dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc);
433 rc = vio_enable_interrupts(ibmvtpm->vdev);
435 dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc);
439 rc = ibmvtpm_crq_send_init(ibmvtpm);
441 dev_err(dev, "Error send_init rc=%d\n", rc);
446 static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status)
448 return (status == 0);
451 static const struct tpm_class_ops tpm_ibmvtpm = {
452 .recv = tpm_ibmvtpm_recv,
453 .send = tpm_ibmvtpm_send,
454 .cancel = tpm_ibmvtpm_cancel,
455 .status = tpm_ibmvtpm_status,
456 .req_complete_mask = 0,
457 .req_complete_val = 0,
458 .req_canceled = tpm_ibmvtpm_req_canceled,
461 static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = {
462 .suspend = tpm_ibmvtpm_suspend,
463 .resume = tpm_ibmvtpm_resume,
467 * ibmvtpm_crq_get_next - Get next responded crq
469 * @ibmvtpm: vtpm device struct
471 * Return: vtpm crq pointer or NULL.
473 static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm)
475 struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue;
476 struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index];
478 if (crq->valid & VTPM_MSG_RES) {
479 if (++crq_q->index == crq_q->num_entry)
488 * ibmvtpm_crq_process - Process responded crq
490 * @crq: crq to be processed
491 * @ibmvtpm: vtpm device struct
494 static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq,
495 struct ibmvtpm_dev *ibmvtpm)
499 switch (crq->valid) {
503 dev_info(ibmvtpm->dev, "CRQ initialized\n");
504 rc = ibmvtpm_crq_send_init_complete(ibmvtpm);
506 dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc);
508 case INIT_CRQ_COMP_RES:
509 dev_info(ibmvtpm->dev,
510 "CRQ initialization completed\n");
513 dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg);
516 case IBMVTPM_VALID_CMD:
518 case VTPM_GET_RTCE_BUFFER_SIZE_RES:
519 if (be16_to_cpu(crq->len) <= 0) {
520 dev_err(ibmvtpm->dev, "Invalid rtce size\n");
523 ibmvtpm->rtce_size = be16_to_cpu(crq->len);
524 ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size,
526 if (!ibmvtpm->rtce_buf) {
527 dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n");
531 ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev,
532 ibmvtpm->rtce_buf, ibmvtpm->rtce_size,
535 if (dma_mapping_error(ibmvtpm->dev,
536 ibmvtpm->rtce_dma_handle)) {
537 kfree(ibmvtpm->rtce_buf);
538 ibmvtpm->rtce_buf = NULL;
539 dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n");
543 case VTPM_GET_VERSION_RES:
544 ibmvtpm->vtpm_version = be32_to_cpu(crq->data);
546 case VTPM_TPM_COMMAND_RES:
547 /* len of the data in rtce buffer */
548 ibmvtpm->res_len = be16_to_cpu(crq->len);
549 ibmvtpm->tpm_processing_cmd = false;
550 wake_up_interruptible(&ibmvtpm->wq);
560 * ibmvtpm_interrupt - Interrupt handler
562 * @irq: irq number to handle
563 * @vtpm_instance: vtpm that received interrupt
568 static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance)
570 struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance;
571 struct ibmvtpm_crq *crq;
573 /* while loop is needed for initial setup (get version and
574 * get rtce_size). There should be only one tpm request at any
577 while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) {
578 ibmvtpm_crq_process(crq, ibmvtpm);
587 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
589 * @vio_dev: vio device struct
590 * @id: vio device id struct
594 * Non-zero on failure.
596 static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
597 const struct vio_device_id *id)
599 struct ibmvtpm_dev *ibmvtpm;
600 struct device *dev = &vio_dev->dev;
601 struct ibmvtpm_crq_queue *crq_q;
602 struct tpm_chip *chip;
603 int rc = -ENOMEM, rc1;
605 chip = tpmm_chip_alloc(dev, &tpm_ibmvtpm);
607 return PTR_ERR(chip);
609 ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL);
611 dev_err(dev, "kzalloc for ibmvtpm failed\n");
616 ibmvtpm->vdev = vio_dev;
618 crq_q = &ibmvtpm->crq_queue;
619 crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL);
620 if (!crq_q->crq_addr) {
621 dev_err(dev, "Unable to allocate memory for crq_addr\n");
625 crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr);
626 ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
630 if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
631 dev_err(dev, "dma mapping failed\n");
635 rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
636 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
637 if (rc == H_RESOURCE)
638 rc = ibmvtpm_reset_crq(ibmvtpm);
641 dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
642 goto reg_crq_cleanup;
645 rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
646 tpm_ibmvtpm_driver_name, ibmvtpm);
648 dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq);
649 goto init_irq_cleanup;
652 rc = vio_enable_interrupts(vio_dev);
654 dev_err(dev, "Error %d enabling interrupts\n", rc);
655 goto init_irq_cleanup;
658 init_waitqueue_head(&ibmvtpm->wq);
662 dev_set_drvdata(&chip->dev, ibmvtpm);
664 spin_lock_init(&ibmvtpm->rtce_lock);
666 rc = ibmvtpm_crq_send_init(ibmvtpm);
668 goto init_irq_cleanup;
670 rc = ibmvtpm_crq_get_version(ibmvtpm);
672 goto init_irq_cleanup;
674 rc = ibmvtpm_crq_get_rtce_size(ibmvtpm);
676 goto init_irq_cleanup;
678 return tpm_chip_register(chip);
681 rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
682 } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
684 dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
689 free_page((unsigned long)crq_q->crq_addr);
696 static struct vio_driver ibmvtpm_driver = {
697 .id_table = tpm_ibmvtpm_device_table,
698 .probe = tpm_ibmvtpm_probe,
699 .remove = tpm_ibmvtpm_remove,
700 .get_desired_dma = tpm_ibmvtpm_get_desired_dma,
701 .name = tpm_ibmvtpm_driver_name,
702 .pm = &tpm_ibmvtpm_pm_ops,
706 * ibmvtpm_module_init - Initialize ibm vtpm module.
711 * Non-zero on failure.
713 static int __init ibmvtpm_module_init(void)
715 return vio_register_driver(&ibmvtpm_driver);
719 * ibmvtpm_module_exit - Tear down ibm vtpm module.
721 static void __exit ibmvtpm_module_exit(void)
723 vio_unregister_driver(&ibmvtpm_driver);
726 module_init(ibmvtpm_module_init);
727 module_exit(ibmvtpm_module_exit);
729 MODULE_AUTHOR("adlai@us.ibm.com");
730 MODULE_DESCRIPTION("IBM vTPM Driver");
731 MODULE_VERSION("1.0");
732 MODULE_LICENSE("GPL");