1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012-2020 IBM Corporation
5 * Author: Ashley Lai <ashleydlai@gmail.com>
7 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
9 * Device driver for TCG/TCPA TPM (trusted platform module).
10 * Specifications at www.trustedcomputinggroup.org
13 #include <linux/dma-mapping.h>
14 #include <linux/dmapool.h>
15 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/interrupt.h>
22 #include <linux/wait.h>
26 #include "tpm_ibmvtpm.h"
28 static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm";
30 static const struct vio_device_id tpm_ibmvtpm_device_table[] = {
31 { "IBM,vtpm", "IBM,vtpm"},
32 { "IBM,vtpm", "IBM,vtpm20"},
35 MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table);
38 * ibmvtpm_send_crq_word() - Send a CRQ request
39 * @vdev: vio device struct
40 * @w1: pre-constructed first word of tpm crq (second word is reserved)
46 static int ibmvtpm_send_crq_word(struct vio_dev *vdev, u64 w1)
48 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, 0);
52 * ibmvtpm_send_crq() - Send a CRQ request
54 * @vdev: vio device struct
60 * The ibmvtpm crq is defined as follows:
62 * Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
63 * -----------------------------------------------------------------------
64 * Word0 | Valid | Type | Length | Data
65 * -----------------------------------------------------------------------
67 * -----------------------------------------------------------------------
69 * Which matches the following structure (on bigendian host):
71 * struct ibmvtpm_crq {
77 * } __attribute__((packed, aligned(8)));
79 * However, the value is passed in a register so just compute the numeric value
80 * to load into the register avoiding byteswap altogether. Endian only affects
81 * memory loads and stores - registers are internally represented the same.
84 * 0 (H_SUCCESS) - Success
87 static int ibmvtpm_send_crq(struct vio_dev *vdev,
88 u8 valid, u8 msg, u16 len, u32 data)
90 u64 w1 = ((u64)valid << 56) | ((u64)msg << 48) | ((u64)len << 32) |
92 return ibmvtpm_send_crq_word(vdev, w1);
96 * tpm_ibmvtpm_recv - Receive data after send
98 * @chip: tpm chip struct
99 * @buf: buffer to read
100 * @count: size of buffer
103 * Number of bytes read
105 static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
107 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
111 if (!ibmvtpm->rtce_buf) {
112 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
116 sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
120 len = ibmvtpm->res_len;
123 dev_err(ibmvtpm->dev,
124 "Invalid size in recv: count=%zd, crq_size=%d\n",
129 spin_lock(&ibmvtpm->rtce_lock);
130 memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len);
131 memset(ibmvtpm->rtce_buf, 0, len);
132 ibmvtpm->res_len = 0;
133 spin_unlock(&ibmvtpm->rtce_lock);
138 * ibmvtpm_crq_send_init - Send a CRQ initialize message
139 * @ibmvtpm: vtpm device struct
143 * Non-zero on failure.
145 static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm)
149 rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_CMD);
151 dev_err(ibmvtpm->dev,
152 "%s failed rc=%d\n", __func__, rc);
158 * tpm_ibmvtpm_resume - Resume from suspend
160 * @dev: device struct
164 static int tpm_ibmvtpm_resume(struct device *dev)
166 struct tpm_chip *chip = dev_get_drvdata(dev);
167 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
173 rc = plpar_hcall_norets(H_ENABLE_CRQ,
174 ibmvtpm->vdev->unit_address);
175 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
178 dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc);
182 rc = vio_enable_interrupts(ibmvtpm->vdev);
184 dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc);
188 rc = ibmvtpm_crq_send_init(ibmvtpm);
190 dev_err(dev, "Error send_init rc=%d\n", rc);
196 * tpm_ibmvtpm_send() - Send a TPM command
197 * @chip: tpm chip struct
198 * @buf: buffer contains data to send
199 * @count: size of buffer
205 static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
207 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
211 if (!ibmvtpm->rtce_buf) {
212 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
216 if (count > ibmvtpm->rtce_size) {
217 dev_err(ibmvtpm->dev,
218 "Invalid size in send: count=%zd, rtce_size=%d\n",
219 count, ibmvtpm->rtce_size);
223 if (ibmvtpm->tpm_processing_cmd) {
224 dev_info(ibmvtpm->dev,
225 "Need to wait for TPM to finish\n");
226 /* wait for previous command to finish */
227 sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
232 spin_lock(&ibmvtpm->rtce_lock);
233 ibmvtpm->res_len = 0;
234 memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count);
237 * set the processing flag before the Hcall, since we may get the
238 * result (interrupt) before even being able to check rc.
240 ibmvtpm->tpm_processing_cmd = true;
243 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
244 IBMVTPM_VALID_CMD, VTPM_TPM_COMMAND,
245 count, ibmvtpm->rtce_dma_handle);
246 if (rc != H_SUCCESS) {
248 * H_CLOSED can be returned after LPM resume. Call
249 * tpm_ibmvtpm_resume() to re-enable the CRQ then retry
250 * ibmvtpm_send_crq() once before failing.
252 if (rc == H_CLOSED && retry) {
253 tpm_ibmvtpm_resume(ibmvtpm->dev);
257 dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc);
258 ibmvtpm->tpm_processing_cmd = false;
261 spin_unlock(&ibmvtpm->rtce_lock);
265 static void tpm_ibmvtpm_cancel(struct tpm_chip *chip)
270 static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
276 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
278 * @ibmvtpm: vtpm device struct
282 * Non-zero on failure.
284 static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
288 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
289 IBMVTPM_VALID_CMD, VTPM_GET_RTCE_BUFFER_SIZE, 0, 0);
291 dev_err(ibmvtpm->dev,
292 "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc);
298 * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
299 * - Note that this is vtpm version and not tpm version
301 * @ibmvtpm: vtpm device struct
305 * Non-zero on failure.
307 static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm)
311 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
312 IBMVTPM_VALID_CMD, VTPM_GET_VERSION, 0, 0);
314 dev_err(ibmvtpm->dev,
315 "ibmvtpm_crq_get_version failed rc=%d\n", rc);
321 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
322 * @ibmvtpm: vtpm device struct
326 * Non-zero on failure.
328 static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm)
332 rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_COMP_CMD);
334 dev_err(ibmvtpm->dev,
335 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc);
341 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
342 * @vdev: vio device struct
346 static void tpm_ibmvtpm_remove(struct vio_dev *vdev)
348 struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
349 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
352 tpm_chip_unregister(chip);
354 free_irq(vdev->irq, ibmvtpm);
359 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
360 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
362 dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle,
363 CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL);
364 free_page((unsigned long)ibmvtpm->crq_queue.crq_addr);
366 if (ibmvtpm->rtce_buf) {
367 dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle,
368 ibmvtpm->rtce_size, DMA_BIDIRECTIONAL);
369 kfree(ibmvtpm->rtce_buf);
373 /* For tpm_ibmvtpm_get_desired_dma */
374 dev_set_drvdata(&vdev->dev, NULL);
378 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
379 * @vdev: vio device struct
382 * Number of bytes the driver needs to DMA map.
384 static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev)
386 struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
387 struct ibmvtpm_dev *ibmvtpm;
390 * ibmvtpm initializes at probe time, so the data we are
391 * asking for may not be set yet. Estimate that 4K required
392 * for TCE-mapped buffer in addition to CRQ.
395 ibmvtpm = dev_get_drvdata(&chip->dev);
397 return CRQ_RES_BUF_SIZE + PAGE_SIZE;
399 return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size;
403 * tpm_ibmvtpm_suspend - Suspend
404 * @dev: device struct
408 static int tpm_ibmvtpm_suspend(struct device *dev)
410 struct tpm_chip *chip = dev_get_drvdata(dev);
411 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
414 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
415 IBMVTPM_VALID_CMD, VTPM_PREPARE_TO_SUSPEND, 0, 0);
417 dev_err(ibmvtpm->dev,
418 "tpm_ibmvtpm_suspend failed rc=%d\n", rc);
424 * ibmvtpm_reset_crq - Reset CRQ
426 * @ibmvtpm: ibm vtpm struct
430 * Non-zero on failure.
432 static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm)
439 rc = plpar_hcall_norets(H_FREE_CRQ,
440 ibmvtpm->vdev->unit_address);
441 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
443 memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE);
444 ibmvtpm->crq_queue.index = 0;
446 return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address,
447 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
450 static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status)
452 return (status == 0);
455 static const struct tpm_class_ops tpm_ibmvtpm = {
456 .recv = tpm_ibmvtpm_recv,
457 .send = tpm_ibmvtpm_send,
458 .cancel = tpm_ibmvtpm_cancel,
459 .status = tpm_ibmvtpm_status,
460 .req_complete_mask = 0,
461 .req_complete_val = 0,
462 .req_canceled = tpm_ibmvtpm_req_canceled,
465 static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = {
466 .suspend = tpm_ibmvtpm_suspend,
467 .resume = tpm_ibmvtpm_resume,
471 * ibmvtpm_crq_get_next - Get next responded crq
473 * @ibmvtpm: vtpm device struct
475 * Return: vtpm crq pointer or NULL.
477 static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm)
479 struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue;
480 struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index];
482 if (crq->valid & VTPM_MSG_RES) {
483 if (++crq_q->index == crq_q->num_entry)
492 * ibmvtpm_crq_process - Process responded crq
494 * @crq: crq to be processed
495 * @ibmvtpm: vtpm device struct
498 static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq,
499 struct ibmvtpm_dev *ibmvtpm)
503 switch (crq->valid) {
507 dev_info(ibmvtpm->dev, "CRQ initialized\n");
508 rc = ibmvtpm_crq_send_init_complete(ibmvtpm);
510 dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc);
512 case INIT_CRQ_COMP_RES:
513 dev_info(ibmvtpm->dev,
514 "CRQ initialization completed\n");
517 dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg);
520 case IBMVTPM_VALID_CMD:
522 case VTPM_GET_RTCE_BUFFER_SIZE_RES:
523 if (be16_to_cpu(crq->len) <= 0) {
524 dev_err(ibmvtpm->dev, "Invalid rtce size\n");
527 ibmvtpm->rtce_size = be16_to_cpu(crq->len);
528 ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size,
530 if (!ibmvtpm->rtce_buf) {
531 dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n");
535 ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev,
536 ibmvtpm->rtce_buf, ibmvtpm->rtce_size,
539 if (dma_mapping_error(ibmvtpm->dev,
540 ibmvtpm->rtce_dma_handle)) {
541 kfree(ibmvtpm->rtce_buf);
542 ibmvtpm->rtce_buf = NULL;
543 dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n");
547 case VTPM_GET_VERSION_RES:
548 ibmvtpm->vtpm_version = be32_to_cpu(crq->data);
550 case VTPM_TPM_COMMAND_RES:
551 /* len of the data in rtce buffer */
552 ibmvtpm->res_len = be16_to_cpu(crq->len);
553 ibmvtpm->tpm_processing_cmd = false;
554 wake_up_interruptible(&ibmvtpm->wq);
564 * ibmvtpm_interrupt - Interrupt handler
566 * @irq: irq number to handle
567 * @vtpm_instance: vtpm that received interrupt
572 static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance)
574 struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance;
575 struct ibmvtpm_crq *crq;
577 /* while loop is needed for initial setup (get version and
578 * get rtce_size). There should be only one tpm request at any
581 while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) {
582 ibmvtpm_crq_process(crq, ibmvtpm);
583 wake_up_interruptible(&ibmvtpm->crq_queue.wq);
592 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
594 * @vio_dev: vio device struct
595 * @id: vio device id struct
599 * Non-zero on failure.
601 static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
602 const struct vio_device_id *id)
604 struct ibmvtpm_dev *ibmvtpm;
605 struct device *dev = &vio_dev->dev;
606 struct ibmvtpm_crq_queue *crq_q;
607 struct tpm_chip *chip;
608 int rc = -ENOMEM, rc1;
610 chip = tpmm_chip_alloc(dev, &tpm_ibmvtpm);
612 return PTR_ERR(chip);
614 ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL);
616 dev_err(dev, "kzalloc for ibmvtpm failed\n");
621 ibmvtpm->vdev = vio_dev;
623 crq_q = &ibmvtpm->crq_queue;
624 crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL);
625 if (!crq_q->crq_addr) {
626 dev_err(dev, "Unable to allocate memory for crq_addr\n");
630 crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr);
631 init_waitqueue_head(&crq_q->wq);
632 ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
636 if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
637 dev_err(dev, "dma mapping failed\n");
641 rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
642 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
643 if (rc == H_RESOURCE)
644 rc = ibmvtpm_reset_crq(ibmvtpm);
647 dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
648 goto reg_crq_cleanup;
651 rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
652 tpm_ibmvtpm_driver_name, ibmvtpm);
654 dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq);
655 goto init_irq_cleanup;
658 rc = vio_enable_interrupts(vio_dev);
660 dev_err(dev, "Error %d enabling interrupts\n", rc);
661 goto init_irq_cleanup;
664 init_waitqueue_head(&ibmvtpm->wq);
668 dev_set_drvdata(&chip->dev, ibmvtpm);
670 spin_lock_init(&ibmvtpm->rtce_lock);
672 rc = ibmvtpm_crq_send_init(ibmvtpm);
674 goto init_irq_cleanup;
676 rc = ibmvtpm_crq_get_version(ibmvtpm);
678 goto init_irq_cleanup;
680 rc = ibmvtpm_crq_get_rtce_size(ibmvtpm);
682 goto init_irq_cleanup;
684 if (!wait_event_timeout(ibmvtpm->crq_queue.wq,
685 ibmvtpm->rtce_buf != NULL,
687 dev_err(dev, "CRQ response timed out\n");
688 goto init_irq_cleanup;
691 if (!strcmp(id->compat, "IBM,vtpm20")) {
692 chip->flags |= TPM_CHIP_FLAG_TPM2;
693 rc = tpm2_get_cc_attrs_tbl(chip);
695 goto init_irq_cleanup;
698 return tpm_chip_register(chip);
701 rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
702 } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
704 dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
709 free_page((unsigned long)crq_q->crq_addr);
716 static struct vio_driver ibmvtpm_driver = {
717 .id_table = tpm_ibmvtpm_device_table,
718 .probe = tpm_ibmvtpm_probe,
719 .remove = tpm_ibmvtpm_remove,
720 .get_desired_dma = tpm_ibmvtpm_get_desired_dma,
721 .name = tpm_ibmvtpm_driver_name,
722 .pm = &tpm_ibmvtpm_pm_ops,
726 * ibmvtpm_module_init - Initialize ibm vtpm module.
731 * Non-zero on failure.
733 static int __init ibmvtpm_module_init(void)
735 return vio_register_driver(&ibmvtpm_driver);
739 * ibmvtpm_module_exit - Tear down ibm vtpm module.
741 static void __exit ibmvtpm_module_exit(void)
743 vio_unregister_driver(&ibmvtpm_driver);
746 module_init(ibmvtpm_module_init);
747 module_exit(ibmvtpm_module_exit);
749 MODULE_AUTHOR("adlai@us.ibm.com");
750 MODULE_DESCRIPTION("IBM vTPM Driver");
751 MODULE_VERSION("1.0");
752 MODULE_LICENSE("GPL");