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 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 - Send a CRQ request
43 * @vdev: vio device struct
51 static int ibmvtpm_send_crq(struct vio_dev
*vdev
, u64 w1
, u64 w2
)
53 return plpar_hcall_norets(H_SEND_CRQ
, vdev
->unit_address
, w1
, w2
);
57 * tpm_ibmvtpm_recv - Receive data after send
58 * @chip: tpm chip struct
59 * @buf: buffer to read
60 * count: size of buffer
63 * Number of bytes read
65 static int tpm_ibmvtpm_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
67 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
71 if (!ibmvtpm
->rtce_buf
) {
72 dev_err(ibmvtpm
->dev
, "ibmvtpm device is not ready\n");
76 sig
= wait_event_interruptible(ibmvtpm
->wq
, !ibmvtpm
->tpm_processing_cmd
);
80 len
= ibmvtpm
->res_len
;
84 "Invalid size in recv: count=%zd, crq_size=%d\n",
89 spin_lock(&ibmvtpm
->rtce_lock
);
90 memcpy((void *)buf
, (void *)ibmvtpm
->rtce_buf
, len
);
91 memset(ibmvtpm
->rtce_buf
, 0, len
);
93 spin_unlock(&ibmvtpm
->rtce_lock
);
98 * tpm_ibmvtpm_send - Send tpm request
99 * @chip: tpm chip struct
100 * @buf: buffer contains data to send
101 * count: size of buffer
104 * Number of bytes sent
106 static int tpm_ibmvtpm_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
108 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
109 struct ibmvtpm_crq crq
;
110 __be64
*word
= (__be64
*)&crq
;
113 if (!ibmvtpm
->rtce_buf
) {
114 dev_err(ibmvtpm
->dev
, "ibmvtpm device is not ready\n");
118 if (count
> ibmvtpm
->rtce_size
) {
119 dev_err(ibmvtpm
->dev
,
120 "Invalid size in send: count=%zd, rtce_size=%d\n",
121 count
, ibmvtpm
->rtce_size
);
125 if (ibmvtpm
->tpm_processing_cmd
) {
126 dev_info(ibmvtpm
->dev
,
127 "Need to wait for TPM to finish\n");
128 /* wait for previous command to finish */
129 sig
= wait_event_interruptible(ibmvtpm
->wq
, !ibmvtpm
->tpm_processing_cmd
);
134 spin_lock(&ibmvtpm
->rtce_lock
);
135 ibmvtpm
->res_len
= 0;
136 memcpy((void *)ibmvtpm
->rtce_buf
, (void *)buf
, count
);
137 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
138 crq
.msg
= (u8
)VTPM_TPM_COMMAND
;
139 crq
.len
= cpu_to_be16(count
);
140 crq
.data
= cpu_to_be32(ibmvtpm
->rtce_dma_handle
);
143 * set the processing flag before the Hcall, since we may get the
144 * result (interrupt) before even being able to check rc.
146 ibmvtpm
->tpm_processing_cmd
= true;
148 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, be64_to_cpu(word
[0]),
149 be64_to_cpu(word
[1]));
150 if (rc
!= H_SUCCESS
) {
151 dev_err(ibmvtpm
->dev
, "tpm_ibmvtpm_send failed rc=%d\n", rc
);
153 ibmvtpm
->tpm_processing_cmd
= false;
157 spin_unlock(&ibmvtpm
->rtce_lock
);
161 static void tpm_ibmvtpm_cancel(struct tpm_chip
*chip
)
166 static u8
tpm_ibmvtpm_status(struct tpm_chip
*chip
)
172 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
173 * @ibmvtpm: vtpm device struct
179 static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev
*ibmvtpm
)
181 struct ibmvtpm_crq crq
;
182 u64
*buf
= (u64
*) &crq
;
185 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
186 crq
.msg
= (u8
)VTPM_GET_RTCE_BUFFER_SIZE
;
188 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, cpu_to_be64(buf
[0]),
189 cpu_to_be64(buf
[1]));
191 dev_err(ibmvtpm
->dev
,
192 "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc
);
198 * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
199 * - Note that this is vtpm version and not tpm version
200 * @ibmvtpm: vtpm device struct
206 static int ibmvtpm_crq_get_version(struct ibmvtpm_dev
*ibmvtpm
)
208 struct ibmvtpm_crq crq
;
209 u64
*buf
= (u64
*) &crq
;
212 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
213 crq
.msg
= (u8
)VTPM_GET_VERSION
;
215 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, cpu_to_be64(buf
[0]),
216 cpu_to_be64(buf
[1]));
218 dev_err(ibmvtpm
->dev
,
219 "ibmvtpm_crq_get_version failed rc=%d\n", rc
);
225 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
226 * @ibmvtpm: vtpm device struct
232 static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev
*ibmvtpm
)
236 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, INIT_CRQ_COMP_CMD
, 0);
238 dev_err(ibmvtpm
->dev
,
239 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc
);
245 * ibmvtpm_crq_send_init - Send a CRQ initialize message
246 * @ibmvtpm: vtpm device struct
252 static int ibmvtpm_crq_send_init(struct ibmvtpm_dev
*ibmvtpm
)
256 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, INIT_CRQ_CMD
, 0);
258 dev_err(ibmvtpm
->dev
,
259 "ibmvtpm_crq_send_init failed rc=%d\n", rc
);
265 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
266 * @vdev: vio device struct
271 static int tpm_ibmvtpm_remove(struct vio_dev
*vdev
)
273 struct tpm_chip
*chip
= dev_get_drvdata(&vdev
->dev
);
274 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
277 tpm_chip_unregister(chip
);
279 free_irq(vdev
->irq
, ibmvtpm
);
284 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
285 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
287 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->crq_dma_handle
,
288 CRQ_RES_BUF_SIZE
, DMA_BIDIRECTIONAL
);
289 free_page((unsigned long)ibmvtpm
->crq_queue
.crq_addr
);
291 if (ibmvtpm
->rtce_buf
) {
292 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->rtce_dma_handle
,
293 ibmvtpm
->rtce_size
, DMA_BIDIRECTIONAL
);
294 kfree(ibmvtpm
->rtce_buf
);
303 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
304 * @vdev: vio device struct
307 * Number of bytes the driver needs to DMA map
309 static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev
*vdev
)
311 struct tpm_chip
*chip
= dev_get_drvdata(&vdev
->dev
);
312 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
314 /* ibmvtpm initializes at probe time, so the data we are
315 * asking for may not be set yet. Estimate that 4K required
316 * for TCE-mapped buffer in addition to CRQ.
319 return CRQ_RES_BUF_SIZE
+ PAGE_SIZE
;
321 return CRQ_RES_BUF_SIZE
+ ibmvtpm
->rtce_size
;
325 * tpm_ibmvtpm_suspend - Suspend
326 * @dev: device struct
331 static int tpm_ibmvtpm_suspend(struct device
*dev
)
333 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
334 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
335 struct ibmvtpm_crq crq
;
336 u64
*buf
= (u64
*) &crq
;
339 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
340 crq
.msg
= (u8
)VTPM_PREPARE_TO_SUSPEND
;
342 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, cpu_to_be64(buf
[0]),
343 cpu_to_be64(buf
[1]));
345 dev_err(ibmvtpm
->dev
,
346 "tpm_ibmvtpm_suspend failed rc=%d\n", rc
);
352 * ibmvtpm_reset_crq - Reset CRQ
353 * @ibmvtpm: ibm vtpm struct
359 static int ibmvtpm_reset_crq(struct ibmvtpm_dev
*ibmvtpm
)
366 rc
= plpar_hcall_norets(H_FREE_CRQ
,
367 ibmvtpm
->vdev
->unit_address
);
368 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
370 memset(ibmvtpm
->crq_queue
.crq_addr
, 0, CRQ_RES_BUF_SIZE
);
371 ibmvtpm
->crq_queue
.index
= 0;
373 return plpar_hcall_norets(H_REG_CRQ
, ibmvtpm
->vdev
->unit_address
,
374 ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
);
378 * tpm_ibmvtpm_resume - Resume from suspend
379 * @dev: device struct
384 static int tpm_ibmvtpm_resume(struct device
*dev
)
386 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
387 struct ibmvtpm_dev
*ibmvtpm
= dev_get_drvdata(&chip
->dev
);
393 rc
= plpar_hcall_norets(H_ENABLE_CRQ
,
394 ibmvtpm
->vdev
->unit_address
);
395 } while (rc
== H_IN_PROGRESS
|| rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
398 dev_err(dev
, "Error enabling ibmvtpm rc=%d\n", rc
);
402 rc
= vio_enable_interrupts(ibmvtpm
->vdev
);
404 dev_err(dev
, "Error vio_enable_interrupts rc=%d\n", rc
);
408 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
410 dev_err(dev
, "Error send_init rc=%d\n", rc
);
415 static bool tpm_ibmvtpm_req_canceled(struct tpm_chip
*chip
, u8 status
)
417 return (status
== 0);
420 static const struct tpm_class_ops tpm_ibmvtpm
= {
421 .recv
= tpm_ibmvtpm_recv
,
422 .send
= tpm_ibmvtpm_send
,
423 .cancel
= tpm_ibmvtpm_cancel
,
424 .status
= tpm_ibmvtpm_status
,
425 .req_complete_mask
= 0,
426 .req_complete_val
= 0,
427 .req_canceled
= tpm_ibmvtpm_req_canceled
,
430 static const struct dev_pm_ops tpm_ibmvtpm_pm_ops
= {
431 .suspend
= tpm_ibmvtpm_suspend
,
432 .resume
= tpm_ibmvtpm_resume
,
436 * ibmvtpm_crq_get_next - Get next responded crq
437 * @ibmvtpm vtpm device struct
442 static struct ibmvtpm_crq
*ibmvtpm_crq_get_next(struct ibmvtpm_dev
*ibmvtpm
)
444 struct ibmvtpm_crq_queue
*crq_q
= &ibmvtpm
->crq_queue
;
445 struct ibmvtpm_crq
*crq
= &crq_q
->crq_addr
[crq_q
->index
];
447 if (crq
->valid
& VTPM_MSG_RES
) {
448 if (++crq_q
->index
== crq_q
->num_entry
)
457 * ibmvtpm_crq_process - Process responded crq
458 * @crq crq to be processed
459 * @ibmvtpm vtpm device struct
464 static void ibmvtpm_crq_process(struct ibmvtpm_crq
*crq
,
465 struct ibmvtpm_dev
*ibmvtpm
)
469 switch (crq
->valid
) {
473 dev_info(ibmvtpm
->dev
, "CRQ initialized\n");
474 rc
= ibmvtpm_crq_send_init_complete(ibmvtpm
);
476 dev_err(ibmvtpm
->dev
, "Unable to send CRQ init complete rc=%d\n", rc
);
478 case INIT_CRQ_COMP_RES
:
479 dev_info(ibmvtpm
->dev
,
480 "CRQ initialization completed\n");
483 dev_err(ibmvtpm
->dev
, "Unknown crq message type: %d\n", crq
->msg
);
486 case IBMVTPM_VALID_CMD
:
488 case VTPM_GET_RTCE_BUFFER_SIZE_RES
:
489 if (be16_to_cpu(crq
->len
) <= 0) {
490 dev_err(ibmvtpm
->dev
, "Invalid rtce size\n");
493 ibmvtpm
->rtce_size
= be16_to_cpu(crq
->len
);
494 ibmvtpm
->rtce_buf
= kmalloc(ibmvtpm
->rtce_size
,
496 if (!ibmvtpm
->rtce_buf
) {
497 dev_err(ibmvtpm
->dev
, "Failed to allocate memory for rtce buffer\n");
501 ibmvtpm
->rtce_dma_handle
= dma_map_single(ibmvtpm
->dev
,
502 ibmvtpm
->rtce_buf
, ibmvtpm
->rtce_size
,
505 if (dma_mapping_error(ibmvtpm
->dev
,
506 ibmvtpm
->rtce_dma_handle
)) {
507 kfree(ibmvtpm
->rtce_buf
);
508 ibmvtpm
->rtce_buf
= NULL
;
509 dev_err(ibmvtpm
->dev
, "Failed to dma map rtce buffer\n");
513 case VTPM_GET_VERSION_RES
:
514 ibmvtpm
->vtpm_version
= be32_to_cpu(crq
->data
);
516 case VTPM_TPM_COMMAND_RES
:
517 /* len of the data in rtce buffer */
518 ibmvtpm
->res_len
= be16_to_cpu(crq
->len
);
519 ibmvtpm
->tpm_processing_cmd
= false;
520 wake_up_interruptible(&ibmvtpm
->wq
);
530 * ibmvtpm_interrupt - Interrupt handler
531 * @irq: irq number to handle
532 * @vtpm_instance: vtpm that received interrupt
537 static irqreturn_t
ibmvtpm_interrupt(int irq
, void *vtpm_instance
)
539 struct ibmvtpm_dev
*ibmvtpm
= (struct ibmvtpm_dev
*) vtpm_instance
;
540 struct ibmvtpm_crq
*crq
;
542 /* while loop is needed for initial setup (get version and
543 * get rtce_size). There should be only one tpm request at any
546 while ((crq
= ibmvtpm_crq_get_next(ibmvtpm
)) != NULL
) {
547 ibmvtpm_crq_process(crq
, ibmvtpm
);
556 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
557 * @vio_dev: vio device struct
558 * @id: vio device id struct
564 static int tpm_ibmvtpm_probe(struct vio_dev
*vio_dev
,
565 const struct vio_device_id
*id
)
567 struct ibmvtpm_dev
*ibmvtpm
;
568 struct device
*dev
= &vio_dev
->dev
;
569 struct ibmvtpm_crq_queue
*crq_q
;
570 struct tpm_chip
*chip
;
571 int rc
= -ENOMEM
, rc1
;
573 chip
= tpmm_chip_alloc(dev
, &tpm_ibmvtpm
);
575 return PTR_ERR(chip
);
577 ibmvtpm
= kzalloc(sizeof(struct ibmvtpm_dev
), GFP_KERNEL
);
579 dev_err(dev
, "kzalloc for ibmvtpm failed\n");
584 ibmvtpm
->vdev
= vio_dev
;
586 crq_q
= &ibmvtpm
->crq_queue
;
587 crq_q
->crq_addr
= (struct ibmvtpm_crq
*)get_zeroed_page(GFP_KERNEL
);
588 if (!crq_q
->crq_addr
) {
589 dev_err(dev
, "Unable to allocate memory for crq_addr\n");
593 crq_q
->num_entry
= CRQ_RES_BUF_SIZE
/ sizeof(*crq_q
->crq_addr
);
594 ibmvtpm
->crq_dma_handle
= dma_map_single(dev
, crq_q
->crq_addr
,
598 if (dma_mapping_error(dev
, ibmvtpm
->crq_dma_handle
)) {
599 dev_err(dev
, "dma mapping failed\n");
603 rc
= plpar_hcall_norets(H_REG_CRQ
, vio_dev
->unit_address
,
604 ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
);
605 if (rc
== H_RESOURCE
)
606 rc
= ibmvtpm_reset_crq(ibmvtpm
);
609 dev_err(dev
, "Unable to register CRQ rc=%d\n", rc
);
610 goto reg_crq_cleanup
;
613 rc
= request_irq(vio_dev
->irq
, ibmvtpm_interrupt
, 0,
614 tpm_ibmvtpm_driver_name
, ibmvtpm
);
616 dev_err(dev
, "Error %d register irq 0x%x\n", rc
, vio_dev
->irq
);
617 goto init_irq_cleanup
;
620 rc
= vio_enable_interrupts(vio_dev
);
622 dev_err(dev
, "Error %d enabling interrupts\n", rc
);
623 goto init_irq_cleanup
;
626 init_waitqueue_head(&ibmvtpm
->wq
);
630 dev_set_drvdata(&chip
->dev
, ibmvtpm
);
632 spin_lock_init(&ibmvtpm
->rtce_lock
);
634 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
636 goto init_irq_cleanup
;
638 rc
= ibmvtpm_crq_get_version(ibmvtpm
);
640 goto init_irq_cleanup
;
642 rc
= ibmvtpm_crq_get_rtce_size(ibmvtpm
);
644 goto init_irq_cleanup
;
646 return tpm_chip_register(chip
);
649 rc1
= plpar_hcall_norets(H_FREE_CRQ
, vio_dev
->unit_address
);
650 } while (rc1
== H_BUSY
|| H_IS_LONG_BUSY(rc1
));
652 dma_unmap_single(dev
, ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
,
657 free_page((unsigned long)crq_q
->crq_addr
);
664 static struct vio_driver ibmvtpm_driver
= {
665 .id_table
= tpm_ibmvtpm_device_table
,
666 .probe
= tpm_ibmvtpm_probe
,
667 .remove
= tpm_ibmvtpm_remove
,
668 .get_desired_dma
= tpm_ibmvtpm_get_desired_dma
,
669 .name
= tpm_ibmvtpm_driver_name
,
670 .pm
= &tpm_ibmvtpm_pm_ops
,
674 * ibmvtpm_module_init - Initialize ibm vtpm module
680 static int __init
ibmvtpm_module_init(void)
682 return vio_register_driver(&ibmvtpm_driver
);
686 * ibmvtpm_module_exit - Teardown ibm vtpm module
691 static void __exit
ibmvtpm_module_exit(void)
693 vio_unregister_driver(&ibmvtpm_driver
);
696 module_init(ibmvtpm_module_init
);
697 module_exit(ibmvtpm_module_exit
);
699 MODULE_AUTHOR("adlai@us.ibm.com");
700 MODULE_DESCRIPTION("IBM vTPM Driver");
701 MODULE_VERSION("1.0");
702 MODULE_LICENSE("GPL");