1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 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"},
34 MODULE_DEVICE_TABLE(vio
, tpm_ibmvtpm_device_table
);
37 * ibmvtpm_send_crq_word() - Send a CRQ request
38 * @vdev: vio device struct
39 * @w1: pre-constructed first word of tpm crq (second word is reserved)
45 static int ibmvtpm_send_crq_word(struct vio_dev
*vdev
, u64 w1
)
47 return plpar_hcall_norets(H_SEND_CRQ
, vdev
->unit_address
, w1
, 0);
51 * ibmvtpm_send_crq() - Send a CRQ request
53 * @vdev: vio device struct
59 * The ibmvtpm crq is defined as follows:
61 * Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
62 * -----------------------------------------------------------------------
63 * Word0 | Valid | Type | Length | Data
64 * -----------------------------------------------------------------------
66 * -----------------------------------------------------------------------
68 * Which matches the following structure (on bigendian host):
70 * struct ibmvtpm_crq {
76 * } __attribute__((packed, aligned(8)));
78 * However, the value is passed in a register so just compute the numeric value
79 * to load into the register avoiding byteswap altogether. Endian only affects
80 * memory loads and stores - registers are internally represented the same.
83 * 0 (H_SUCCESS) - Success
86 static int ibmvtpm_send_crq(struct vio_dev
*vdev
,
87 u8 valid
, u8 msg
, u16 len
, u32 data
)
89 u64 w1
= ((u64
)valid
<< 56) | ((u64
)msg
<< 48) | ((u64
)len
<< 32) |
91 return ibmvtpm_send_crq_word(vdev
, w1
);
95 * tpm_ibmvtpm_recv - Receive data after send
97 * @chip: tpm chip struct
98 * @buf: buffer to read
99 * @count: size of buffer
102 * Number of bytes read
104 static int tpm_ibmvtpm_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
106 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
110 if (!ibmvtpm
->rtce_buf
) {
111 dev_err(ibmvtpm
->dev
, "ibmvtpm device is not ready\n");
115 sig
= wait_event_interruptible(ibmvtpm
->wq
, !ibmvtpm
->tpm_processing_cmd
);
119 len
= ibmvtpm
->res_len
;
122 dev_err(ibmvtpm
->dev
,
123 "Invalid size in recv: count=%zd, crq_size=%d\n",
128 spin_lock(&ibmvtpm
->rtce_lock
);
129 memcpy((void *)buf
, (void *)ibmvtpm
->rtce_buf
, len
);
130 memset(ibmvtpm
->rtce_buf
, 0, len
);
131 ibmvtpm
->res_len
= 0;
132 spin_unlock(&ibmvtpm
->rtce_lock
);
137 * tpm_ibmvtpm_send() - Send a TPM command
138 * @chip: tpm chip struct
139 * @buf: buffer contains data to send
140 * @count: size of buffer
146 static int tpm_ibmvtpm_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
148 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
151 if (!ibmvtpm
->rtce_buf
) {
152 dev_err(ibmvtpm
->dev
, "ibmvtpm device is not ready\n");
156 if (count
> ibmvtpm
->rtce_size
) {
157 dev_err(ibmvtpm
->dev
,
158 "Invalid size in send: count=%zd, rtce_size=%d\n",
159 count
, ibmvtpm
->rtce_size
);
163 if (ibmvtpm
->tpm_processing_cmd
) {
164 dev_info(ibmvtpm
->dev
,
165 "Need to wait for TPM to finish\n");
166 /* wait for previous command to finish */
167 sig
= wait_event_interruptible(ibmvtpm
->wq
, !ibmvtpm
->tpm_processing_cmd
);
172 spin_lock(&ibmvtpm
->rtce_lock
);
173 ibmvtpm
->res_len
= 0;
174 memcpy((void *)ibmvtpm
->rtce_buf
, (void *)buf
, count
);
177 * set the processing flag before the Hcall, since we may get the
178 * result (interrupt) before even being able to check rc.
180 ibmvtpm
->tpm_processing_cmd
= true;
182 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
,
183 IBMVTPM_VALID_CMD
, VTPM_TPM_COMMAND
,
184 count
, ibmvtpm
->rtce_dma_handle
);
185 if (rc
!= H_SUCCESS
) {
186 dev_err(ibmvtpm
->dev
, "tpm_ibmvtpm_send failed rc=%d\n", rc
);
188 ibmvtpm
->tpm_processing_cmd
= false;
192 spin_unlock(&ibmvtpm
->rtce_lock
);
196 static void tpm_ibmvtpm_cancel(struct tpm_chip
*chip
)
201 static u8
tpm_ibmvtpm_status(struct tpm_chip
*chip
)
207 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
209 * @ibmvtpm: vtpm device struct
213 * Non-zero on failure.
215 static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev
*ibmvtpm
)
219 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
,
220 IBMVTPM_VALID_CMD
, VTPM_GET_RTCE_BUFFER_SIZE
, 0, 0);
222 dev_err(ibmvtpm
->dev
,
223 "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc
);
229 * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
230 * - Note that this is vtpm version and not tpm version
232 * @ibmvtpm: vtpm device struct
236 * Non-zero on failure.
238 static int ibmvtpm_crq_get_version(struct ibmvtpm_dev
*ibmvtpm
)
242 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
,
243 IBMVTPM_VALID_CMD
, VTPM_GET_VERSION
, 0, 0);
245 dev_err(ibmvtpm
->dev
,
246 "ibmvtpm_crq_get_version failed rc=%d\n", rc
);
252 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
253 * @ibmvtpm: vtpm device struct
257 * Non-zero on failure.
259 static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev
*ibmvtpm
)
263 rc
= ibmvtpm_send_crq_word(ibmvtpm
->vdev
, INIT_CRQ_COMP_CMD
);
265 dev_err(ibmvtpm
->dev
,
266 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc
);
272 * ibmvtpm_crq_send_init - Send a CRQ initialize message
273 * @ibmvtpm: vtpm device struct
277 * Non-zero on failure.
279 static int ibmvtpm_crq_send_init(struct ibmvtpm_dev
*ibmvtpm
)
283 rc
= ibmvtpm_send_crq_word(ibmvtpm
->vdev
, INIT_CRQ_CMD
);
285 dev_err(ibmvtpm
->dev
,
286 "ibmvtpm_crq_send_init failed rc=%d\n", rc
);
292 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
293 * @vdev: vio device struct
297 static int tpm_ibmvtpm_remove(struct vio_dev
*vdev
)
299 struct tpm_chip
*chip
= dev_get_drvdata(&vdev
->dev
);
300 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
303 tpm_chip_unregister(chip
);
305 free_irq(vdev
->irq
, ibmvtpm
);
310 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
311 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
313 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->crq_dma_handle
,
314 CRQ_RES_BUF_SIZE
, DMA_BIDIRECTIONAL
);
315 free_page((unsigned long)ibmvtpm
->crq_queue
.crq_addr
);
317 if (ibmvtpm
->rtce_buf
) {
318 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->rtce_dma_handle
,
319 ibmvtpm
->rtce_size
, DMA_BIDIRECTIONAL
);
320 kfree(ibmvtpm
->rtce_buf
);
324 /* For tpm_ibmvtpm_get_desired_dma */
325 dev_set_drvdata(&vdev
->dev
, NULL
);
331 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
332 * @vdev: vio device struct
335 * Number of bytes the driver needs to DMA map.
337 static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev
*vdev
)
339 struct tpm_chip
*chip
= dev_get_drvdata(&vdev
->dev
);
340 struct ibmvtpm_dev
*ibmvtpm
;
343 * ibmvtpm initializes at probe time, so the data we are
344 * asking for may not be set yet. Estimate that 4K required
345 * for TCE-mapped buffer in addition to CRQ.
348 ibmvtpm
= dev_get_drvdata(&chip
->dev
);
350 return CRQ_RES_BUF_SIZE
+ PAGE_SIZE
;
352 return CRQ_RES_BUF_SIZE
+ ibmvtpm
->rtce_size
;
356 * tpm_ibmvtpm_suspend - Suspend
357 * @dev: device struct
361 static int tpm_ibmvtpm_suspend(struct device
*dev
)
363 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
364 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
367 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
,
368 IBMVTPM_VALID_CMD
, VTPM_PREPARE_TO_SUSPEND
, 0, 0);
370 dev_err(ibmvtpm
->dev
,
371 "tpm_ibmvtpm_suspend failed rc=%d\n", rc
);
377 * ibmvtpm_reset_crq - Reset CRQ
379 * @ibmvtpm: ibm vtpm struct
383 * Non-zero on failure.
385 static int ibmvtpm_reset_crq(struct ibmvtpm_dev
*ibmvtpm
)
392 rc
= plpar_hcall_norets(H_FREE_CRQ
,
393 ibmvtpm
->vdev
->unit_address
);
394 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
396 memset(ibmvtpm
->crq_queue
.crq_addr
, 0, CRQ_RES_BUF_SIZE
);
397 ibmvtpm
->crq_queue
.index
= 0;
399 return plpar_hcall_norets(H_REG_CRQ
, ibmvtpm
->vdev
->unit_address
,
400 ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
);
404 * tpm_ibmvtpm_resume - Resume from suspend
406 * @dev: device struct
410 static int tpm_ibmvtpm_resume(struct device
*dev
)
412 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
413 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
419 rc
= plpar_hcall_norets(H_ENABLE_CRQ
,
420 ibmvtpm
->vdev
->unit_address
);
421 } while (rc
== H_IN_PROGRESS
|| rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
424 dev_err(dev
, "Error enabling ibmvtpm rc=%d\n", rc
);
428 rc
= vio_enable_interrupts(ibmvtpm
->vdev
);
430 dev_err(dev
, "Error vio_enable_interrupts rc=%d\n", rc
);
434 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
436 dev_err(dev
, "Error send_init rc=%d\n", rc
);
441 static bool tpm_ibmvtpm_req_canceled(struct tpm_chip
*chip
, u8 status
)
443 return (status
== 0);
446 static const struct tpm_class_ops tpm_ibmvtpm
= {
447 .recv
= tpm_ibmvtpm_recv
,
448 .send
= tpm_ibmvtpm_send
,
449 .cancel
= tpm_ibmvtpm_cancel
,
450 .status
= tpm_ibmvtpm_status
,
451 .req_complete_mask
= 0,
452 .req_complete_val
= 0,
453 .req_canceled
= tpm_ibmvtpm_req_canceled
,
456 static const struct dev_pm_ops tpm_ibmvtpm_pm_ops
= {
457 .suspend
= tpm_ibmvtpm_suspend
,
458 .resume
= tpm_ibmvtpm_resume
,
462 * ibmvtpm_crq_get_next - Get next responded crq
464 * @ibmvtpm: vtpm device struct
466 * Return: vtpm crq pointer or NULL.
468 static struct ibmvtpm_crq
*ibmvtpm_crq_get_next(struct ibmvtpm_dev
*ibmvtpm
)
470 struct ibmvtpm_crq_queue
*crq_q
= &ibmvtpm
->crq_queue
;
471 struct ibmvtpm_crq
*crq
= &crq_q
->crq_addr
[crq_q
->index
];
473 if (crq
->valid
& VTPM_MSG_RES
) {
474 if (++crq_q
->index
== crq_q
->num_entry
)
483 * ibmvtpm_crq_process - Process responded crq
485 * @crq: crq to be processed
486 * @ibmvtpm: vtpm device struct
489 static void ibmvtpm_crq_process(struct ibmvtpm_crq
*crq
,
490 struct ibmvtpm_dev
*ibmvtpm
)
494 switch (crq
->valid
) {
498 dev_info(ibmvtpm
->dev
, "CRQ initialized\n");
499 rc
= ibmvtpm_crq_send_init_complete(ibmvtpm
);
501 dev_err(ibmvtpm
->dev
, "Unable to send CRQ init complete rc=%d\n", rc
);
503 case INIT_CRQ_COMP_RES
:
504 dev_info(ibmvtpm
->dev
,
505 "CRQ initialization completed\n");
508 dev_err(ibmvtpm
->dev
, "Unknown crq message type: %d\n", crq
->msg
);
511 case IBMVTPM_VALID_CMD
:
513 case VTPM_GET_RTCE_BUFFER_SIZE_RES
:
514 if (be16_to_cpu(crq
->len
) <= 0) {
515 dev_err(ibmvtpm
->dev
, "Invalid rtce size\n");
518 ibmvtpm
->rtce_size
= be16_to_cpu(crq
->len
);
519 ibmvtpm
->rtce_buf
= kmalloc(ibmvtpm
->rtce_size
,
521 if (!ibmvtpm
->rtce_buf
) {
522 dev_err(ibmvtpm
->dev
, "Failed to allocate memory for rtce buffer\n");
526 ibmvtpm
->rtce_dma_handle
= dma_map_single(ibmvtpm
->dev
,
527 ibmvtpm
->rtce_buf
, ibmvtpm
->rtce_size
,
530 if (dma_mapping_error(ibmvtpm
->dev
,
531 ibmvtpm
->rtce_dma_handle
)) {
532 kfree(ibmvtpm
->rtce_buf
);
533 ibmvtpm
->rtce_buf
= NULL
;
534 dev_err(ibmvtpm
->dev
, "Failed to dma map rtce buffer\n");
538 case VTPM_GET_VERSION_RES
:
539 ibmvtpm
->vtpm_version
= be32_to_cpu(crq
->data
);
541 case VTPM_TPM_COMMAND_RES
:
542 /* len of the data in rtce buffer */
543 ibmvtpm
->res_len
= be16_to_cpu(crq
->len
);
544 ibmvtpm
->tpm_processing_cmd
= false;
545 wake_up_interruptible(&ibmvtpm
->wq
);
555 * ibmvtpm_interrupt - Interrupt handler
557 * @irq: irq number to handle
558 * @vtpm_instance: vtpm that received interrupt
563 static irqreturn_t
ibmvtpm_interrupt(int irq
, void *vtpm_instance
)
565 struct ibmvtpm_dev
*ibmvtpm
= (struct ibmvtpm_dev
*) vtpm_instance
;
566 struct ibmvtpm_crq
*crq
;
568 /* while loop is needed for initial setup (get version and
569 * get rtce_size). There should be only one tpm request at any
572 while ((crq
= ibmvtpm_crq_get_next(ibmvtpm
)) != NULL
) {
573 ibmvtpm_crq_process(crq
, ibmvtpm
);
582 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
584 * @vio_dev: vio device struct
585 * @id: vio device id struct
589 * Non-zero on failure.
591 static int tpm_ibmvtpm_probe(struct vio_dev
*vio_dev
,
592 const struct vio_device_id
*id
)
594 struct ibmvtpm_dev
*ibmvtpm
;
595 struct device
*dev
= &vio_dev
->dev
;
596 struct ibmvtpm_crq_queue
*crq_q
;
597 struct tpm_chip
*chip
;
598 int rc
= -ENOMEM
, rc1
;
600 chip
= tpmm_chip_alloc(dev
, &tpm_ibmvtpm
);
602 return PTR_ERR(chip
);
604 ibmvtpm
= kzalloc(sizeof(struct ibmvtpm_dev
), GFP_KERNEL
);
606 dev_err(dev
, "kzalloc for ibmvtpm failed\n");
611 ibmvtpm
->vdev
= vio_dev
;
613 crq_q
= &ibmvtpm
->crq_queue
;
614 crq_q
->crq_addr
= (struct ibmvtpm_crq
*)get_zeroed_page(GFP_KERNEL
);
615 if (!crq_q
->crq_addr
) {
616 dev_err(dev
, "Unable to allocate memory for crq_addr\n");
620 crq_q
->num_entry
= CRQ_RES_BUF_SIZE
/ sizeof(*crq_q
->crq_addr
);
621 ibmvtpm
->crq_dma_handle
= dma_map_single(dev
, crq_q
->crq_addr
,
625 if (dma_mapping_error(dev
, ibmvtpm
->crq_dma_handle
)) {
626 dev_err(dev
, "dma mapping failed\n");
630 rc
= plpar_hcall_norets(H_REG_CRQ
, vio_dev
->unit_address
,
631 ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
);
632 if (rc
== H_RESOURCE
)
633 rc
= ibmvtpm_reset_crq(ibmvtpm
);
636 dev_err(dev
, "Unable to register CRQ rc=%d\n", rc
);
637 goto reg_crq_cleanup
;
640 rc
= request_irq(vio_dev
->irq
, ibmvtpm_interrupt
, 0,
641 tpm_ibmvtpm_driver_name
, ibmvtpm
);
643 dev_err(dev
, "Error %d register irq 0x%x\n", rc
, vio_dev
->irq
);
644 goto init_irq_cleanup
;
647 rc
= vio_enable_interrupts(vio_dev
);
649 dev_err(dev
, "Error %d enabling interrupts\n", rc
);
650 goto init_irq_cleanup
;
653 init_waitqueue_head(&ibmvtpm
->wq
);
657 dev_set_drvdata(&chip
->dev
, ibmvtpm
);
659 spin_lock_init(&ibmvtpm
->rtce_lock
);
661 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
663 goto init_irq_cleanup
;
665 rc
= ibmvtpm_crq_get_version(ibmvtpm
);
667 goto init_irq_cleanup
;
669 rc
= ibmvtpm_crq_get_rtce_size(ibmvtpm
);
671 goto init_irq_cleanup
;
673 return tpm_chip_register(chip
);
676 rc1
= plpar_hcall_norets(H_FREE_CRQ
, vio_dev
->unit_address
);
677 } while (rc1
== H_BUSY
|| H_IS_LONG_BUSY(rc1
));
679 dma_unmap_single(dev
, ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
,
684 free_page((unsigned long)crq_q
->crq_addr
);
691 static struct vio_driver ibmvtpm_driver
= {
692 .id_table
= tpm_ibmvtpm_device_table
,
693 .probe
= tpm_ibmvtpm_probe
,
694 .remove
= tpm_ibmvtpm_remove
,
695 .get_desired_dma
= tpm_ibmvtpm_get_desired_dma
,
696 .name
= tpm_ibmvtpm_driver_name
,
697 .pm
= &tpm_ibmvtpm_pm_ops
,
701 * ibmvtpm_module_init - Initialize ibm vtpm module.
706 * Non-zero on failure.
708 static int __init
ibmvtpm_module_init(void)
710 return vio_register_driver(&ibmvtpm_driver
);
714 * ibmvtpm_module_exit - Tear down ibm vtpm module.
716 static void __exit
ibmvtpm_module_exit(void)
718 vio_unregister_driver(&ibmvtpm_driver
);
721 module_init(ibmvtpm_module_init
);
722 module_exit(ibmvtpm_module_exit
);
724 MODULE_AUTHOR("adlai@us.ibm.com");
725 MODULE_DESCRIPTION("IBM vTPM Driver");
726 MODULE_VERSION("1.0");
727 MODULE_LICENSE("GPL");