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 * ibmvtpm_get_data - Retrieve ibm vtpm data
63 static struct ibmvtpm_dev
*ibmvtpm_get_data(const struct device
*dev
)
65 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
67 return (struct ibmvtpm_dev
*)TPM_VPRIV(chip
);
72 * tpm_ibmvtpm_recv - Receive data after send
73 * @chip: tpm chip struct
74 * @buf: buffer to read
75 * count: size of buffer
78 * Number of bytes read
80 static int tpm_ibmvtpm_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
82 struct ibmvtpm_dev
*ibmvtpm
;
86 ibmvtpm
= (struct ibmvtpm_dev
*)TPM_VPRIV(chip
);
88 if (!ibmvtpm
->rtce_buf
) {
89 dev_err(ibmvtpm
->dev
, "ibmvtpm device is not ready\n");
93 sig
= wait_event_interruptible(ibmvtpm
->wq
, ibmvtpm
->res_len
!= 0);
97 len
= ibmvtpm
->res_len
;
100 dev_err(ibmvtpm
->dev
,
101 "Invalid size in recv: count=%zd, crq_size=%d\n",
106 spin_lock(&ibmvtpm
->rtce_lock
);
107 memcpy((void *)buf
, (void *)ibmvtpm
->rtce_buf
, len
);
108 memset(ibmvtpm
->rtce_buf
, 0, len
);
109 ibmvtpm
->res_len
= 0;
110 spin_unlock(&ibmvtpm
->rtce_lock
);
115 * tpm_ibmvtpm_send - Send tpm request
116 * @chip: tpm chip struct
117 * @buf: buffer contains data to send
118 * count: size of buffer
121 * Number of bytes sent
123 static int tpm_ibmvtpm_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
125 struct ibmvtpm_dev
*ibmvtpm
;
126 struct ibmvtpm_crq crq
;
127 u64
*word
= (u64
*) &crq
;
130 ibmvtpm
= (struct ibmvtpm_dev
*)TPM_VPRIV(chip
);
132 if (!ibmvtpm
->rtce_buf
) {
133 dev_err(ibmvtpm
->dev
, "ibmvtpm device is not ready\n");
137 if (count
> ibmvtpm
->rtce_size
) {
138 dev_err(ibmvtpm
->dev
,
139 "Invalid size in send: count=%zd, rtce_size=%d\n",
140 count
, ibmvtpm
->rtce_size
);
144 spin_lock(&ibmvtpm
->rtce_lock
);
145 memcpy((void *)ibmvtpm
->rtce_buf
, (void *)buf
, count
);
146 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
147 crq
.msg
= (u8
)VTPM_TPM_COMMAND
;
148 crq
.len
= (u16
)count
;
149 crq
.data
= ibmvtpm
->rtce_dma_handle
;
151 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, word
[0], word
[1]);
152 if (rc
!= H_SUCCESS
) {
153 dev_err(ibmvtpm
->dev
, "tpm_ibmvtpm_send failed rc=%d\n", rc
);
158 spin_unlock(&ibmvtpm
->rtce_lock
);
162 static void tpm_ibmvtpm_cancel(struct tpm_chip
*chip
)
167 static u8
tpm_ibmvtpm_status(struct tpm_chip
*chip
)
173 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
174 * @ibmvtpm: vtpm device struct
180 static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev
*ibmvtpm
)
182 struct ibmvtpm_crq crq
;
183 u64
*buf
= (u64
*) &crq
;
186 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
187 crq
.msg
= (u8
)VTPM_GET_RTCE_BUFFER_SIZE
;
189 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, buf
[0], 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
, buf
[0], buf
[1]);
217 dev_err(ibmvtpm
->dev
,
218 "ibmvtpm_crq_get_version failed rc=%d\n", rc
);
224 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
225 * @ibmvtpm: vtpm device struct
231 static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev
*ibmvtpm
)
235 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, INIT_CRQ_COMP_CMD
, 0);
237 dev_err(ibmvtpm
->dev
,
238 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc
);
244 * ibmvtpm_crq_send_init - Send a CRQ initialize message
245 * @ibmvtpm: vtpm device struct
251 static int ibmvtpm_crq_send_init(struct ibmvtpm_dev
*ibmvtpm
)
255 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, INIT_CRQ_CMD
, 0);
257 dev_err(ibmvtpm
->dev
,
258 "ibmvtpm_crq_send_init failed rc=%d\n", rc
);
264 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
265 * @vdev: vio device struct
270 static int tpm_ibmvtpm_remove(struct vio_dev
*vdev
)
272 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(&vdev
->dev
);
273 struct tpm_chip
*chip
= dev_get_drvdata(ibmvtpm
->dev
);
276 tpm_chip_unregister(chip
);
278 free_irq(vdev
->irq
, ibmvtpm
);
283 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
284 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
286 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->crq_dma_handle
,
287 CRQ_RES_BUF_SIZE
, DMA_BIDIRECTIONAL
);
288 free_page((unsigned long)ibmvtpm
->crq_queue
.crq_addr
);
290 if (ibmvtpm
->rtce_buf
) {
291 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->rtce_dma_handle
,
292 ibmvtpm
->rtce_size
, DMA_BIDIRECTIONAL
);
293 kfree(ibmvtpm
->rtce_buf
);
302 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
303 * @vdev: vio device struct
306 * Number of bytes the driver needs to DMA map
308 static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev
*vdev
)
310 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(&vdev
->dev
);
312 /* ibmvtpm initializes at probe time, so the data we are
313 * asking for may not be set yet. Estimate that 4K required
314 * for TCE-mapped buffer in addition to CRQ.
317 return CRQ_RES_BUF_SIZE
+ PAGE_SIZE
;
319 return CRQ_RES_BUF_SIZE
+ ibmvtpm
->rtce_size
;
323 * tpm_ibmvtpm_suspend - Suspend
324 * @dev: device struct
329 static int tpm_ibmvtpm_suspend(struct device
*dev
)
331 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(dev
);
332 struct ibmvtpm_crq crq
;
333 u64
*buf
= (u64
*) &crq
;
336 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
337 crq
.msg
= (u8
)VTPM_PREPARE_TO_SUSPEND
;
339 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, buf
[0], buf
[1]);
341 dev_err(ibmvtpm
->dev
,
342 "tpm_ibmvtpm_suspend failed rc=%d\n", rc
);
348 * ibmvtpm_reset_crq - Reset CRQ
349 * @ibmvtpm: ibm vtpm struct
355 static int ibmvtpm_reset_crq(struct ibmvtpm_dev
*ibmvtpm
)
362 rc
= plpar_hcall_norets(H_FREE_CRQ
,
363 ibmvtpm
->vdev
->unit_address
);
364 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
366 memset(ibmvtpm
->crq_queue
.crq_addr
, 0, CRQ_RES_BUF_SIZE
);
367 ibmvtpm
->crq_queue
.index
= 0;
369 return plpar_hcall_norets(H_REG_CRQ
, ibmvtpm
->vdev
->unit_address
,
370 ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
);
374 * tpm_ibmvtpm_resume - Resume from suspend
375 * @dev: device struct
380 static int tpm_ibmvtpm_resume(struct device
*dev
)
382 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(dev
);
388 rc
= plpar_hcall_norets(H_ENABLE_CRQ
,
389 ibmvtpm
->vdev
->unit_address
);
390 } while (rc
== H_IN_PROGRESS
|| rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
393 dev_err(dev
, "Error enabling ibmvtpm rc=%d\n", rc
);
397 rc
= vio_enable_interrupts(ibmvtpm
->vdev
);
399 dev_err(dev
, "Error vio_enable_interrupts rc=%d\n", rc
);
403 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
405 dev_err(dev
, "Error send_init rc=%d\n", rc
);
410 static bool tpm_ibmvtpm_req_canceled(struct tpm_chip
*chip
, u8 status
)
412 return (status
== 0);
415 static const struct tpm_class_ops tpm_ibmvtpm
= {
416 .recv
= tpm_ibmvtpm_recv
,
417 .send
= tpm_ibmvtpm_send
,
418 .cancel
= tpm_ibmvtpm_cancel
,
419 .status
= tpm_ibmvtpm_status
,
420 .req_complete_mask
= 0,
421 .req_complete_val
= 0,
422 .req_canceled
= tpm_ibmvtpm_req_canceled
,
425 static const struct dev_pm_ops tpm_ibmvtpm_pm_ops
= {
426 .suspend
= tpm_ibmvtpm_suspend
,
427 .resume
= tpm_ibmvtpm_resume
,
431 * ibmvtpm_crq_get_next - Get next responded crq
432 * @ibmvtpm vtpm device struct
437 static struct ibmvtpm_crq
*ibmvtpm_crq_get_next(struct ibmvtpm_dev
*ibmvtpm
)
439 struct ibmvtpm_crq_queue
*crq_q
= &ibmvtpm
->crq_queue
;
440 struct ibmvtpm_crq
*crq
= &crq_q
->crq_addr
[crq_q
->index
];
442 if (crq
->valid
& VTPM_MSG_RES
) {
443 if (++crq_q
->index
== crq_q
->num_entry
)
452 * ibmvtpm_crq_process - Process responded crq
453 * @crq crq to be processed
454 * @ibmvtpm vtpm device struct
459 static void ibmvtpm_crq_process(struct ibmvtpm_crq
*crq
,
460 struct ibmvtpm_dev
*ibmvtpm
)
464 switch (crq
->valid
) {
468 dev_info(ibmvtpm
->dev
, "CRQ initialized\n");
469 rc
= ibmvtpm_crq_send_init_complete(ibmvtpm
);
471 dev_err(ibmvtpm
->dev
, "Unable to send CRQ init complete rc=%d\n", rc
);
473 case INIT_CRQ_COMP_RES
:
474 dev_info(ibmvtpm
->dev
,
475 "CRQ initialization completed\n");
478 dev_err(ibmvtpm
->dev
, "Unknown crq message type: %d\n", crq
->msg
);
481 case IBMVTPM_VALID_CMD
:
483 case VTPM_GET_RTCE_BUFFER_SIZE_RES
:
485 dev_err(ibmvtpm
->dev
, "Invalid rtce size\n");
488 ibmvtpm
->rtce_size
= crq
->len
;
489 ibmvtpm
->rtce_buf
= kmalloc(ibmvtpm
->rtce_size
,
491 if (!ibmvtpm
->rtce_buf
) {
492 dev_err(ibmvtpm
->dev
, "Failed to allocate memory for rtce buffer\n");
496 ibmvtpm
->rtce_dma_handle
= dma_map_single(ibmvtpm
->dev
,
497 ibmvtpm
->rtce_buf
, ibmvtpm
->rtce_size
,
500 if (dma_mapping_error(ibmvtpm
->dev
,
501 ibmvtpm
->rtce_dma_handle
)) {
502 kfree(ibmvtpm
->rtce_buf
);
503 ibmvtpm
->rtce_buf
= NULL
;
504 dev_err(ibmvtpm
->dev
, "Failed to dma map rtce buffer\n");
508 case VTPM_GET_VERSION_RES
:
509 ibmvtpm
->vtpm_version
= crq
->data
;
511 case VTPM_TPM_COMMAND_RES
:
512 /* len of the data in rtce buffer */
513 ibmvtpm
->res_len
= crq
->len
;
514 wake_up_interruptible(&ibmvtpm
->wq
);
524 * ibmvtpm_interrupt - Interrupt handler
525 * @irq: irq number to handle
526 * @vtpm_instance: vtpm that received interrupt
531 static irqreturn_t
ibmvtpm_interrupt(int irq
, void *vtpm_instance
)
533 struct ibmvtpm_dev
*ibmvtpm
= (struct ibmvtpm_dev
*) vtpm_instance
;
534 struct ibmvtpm_crq
*crq
;
536 /* while loop is needed for initial setup (get version and
537 * get rtce_size). There should be only one tpm request at any
540 while ((crq
= ibmvtpm_crq_get_next(ibmvtpm
)) != NULL
) {
541 ibmvtpm_crq_process(crq
, ibmvtpm
);
550 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
551 * @vio_dev: vio device struct
552 * @id: vio device id struct
558 static int tpm_ibmvtpm_probe(struct vio_dev
*vio_dev
,
559 const struct vio_device_id
*id
)
561 struct ibmvtpm_dev
*ibmvtpm
;
562 struct device
*dev
= &vio_dev
->dev
;
563 struct ibmvtpm_crq_queue
*crq_q
;
564 struct tpm_chip
*chip
;
565 int rc
= -ENOMEM
, rc1
;
567 chip
= tpmm_chip_alloc(dev
, &tpm_ibmvtpm
);
569 return PTR_ERR(chip
);
571 ibmvtpm
= kzalloc(sizeof(struct ibmvtpm_dev
), GFP_KERNEL
);
573 dev_err(dev
, "kzalloc for ibmvtpm failed\n");
577 crq_q
= &ibmvtpm
->crq_queue
;
578 crq_q
->crq_addr
= (struct ibmvtpm_crq
*)get_zeroed_page(GFP_KERNEL
);
579 if (!crq_q
->crq_addr
) {
580 dev_err(dev
, "Unable to allocate memory for crq_addr\n");
584 crq_q
->num_entry
= CRQ_RES_BUF_SIZE
/ sizeof(*crq_q
->crq_addr
);
585 ibmvtpm
->crq_dma_handle
= dma_map_single(dev
, crq_q
->crq_addr
,
589 if (dma_mapping_error(dev
, ibmvtpm
->crq_dma_handle
)) {
590 dev_err(dev
, "dma mapping failed\n");
594 rc
= plpar_hcall_norets(H_REG_CRQ
, vio_dev
->unit_address
,
595 ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
);
596 if (rc
== H_RESOURCE
)
597 rc
= ibmvtpm_reset_crq(ibmvtpm
);
600 dev_err(dev
, "Unable to register CRQ rc=%d\n", rc
);
601 goto reg_crq_cleanup
;
604 rc
= request_irq(vio_dev
->irq
, ibmvtpm_interrupt
, 0,
605 tpm_ibmvtpm_driver_name
, ibmvtpm
);
607 dev_err(dev
, "Error %d register irq 0x%x\n", rc
, vio_dev
->irq
);
608 goto init_irq_cleanup
;
611 rc
= vio_enable_interrupts(vio_dev
);
613 dev_err(dev
, "Error %d enabling interrupts\n", rc
);
614 goto init_irq_cleanup
;
617 init_waitqueue_head(&ibmvtpm
->wq
);
622 ibmvtpm
->vdev
= vio_dev
;
623 TPM_VPRIV(chip
) = (void *)ibmvtpm
;
625 spin_lock_init(&ibmvtpm
->rtce_lock
);
627 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
629 goto init_irq_cleanup
;
631 rc
= ibmvtpm_crq_get_version(ibmvtpm
);
633 goto init_irq_cleanup
;
635 rc
= ibmvtpm_crq_get_rtce_size(ibmvtpm
);
637 goto init_irq_cleanup
;
639 return tpm_chip_register(chip
);
642 rc1
= plpar_hcall_norets(H_FREE_CRQ
, vio_dev
->unit_address
);
643 } while (rc1
== H_BUSY
|| H_IS_LONG_BUSY(rc1
));
645 dma_unmap_single(dev
, ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
,
650 free_page((unsigned long)crq_q
->crq_addr
);
657 static struct vio_driver ibmvtpm_driver
= {
658 .id_table
= tpm_ibmvtpm_device_table
,
659 .probe
= tpm_ibmvtpm_probe
,
660 .remove
= tpm_ibmvtpm_remove
,
661 .get_desired_dma
= tpm_ibmvtpm_get_desired_dma
,
662 .name
= tpm_ibmvtpm_driver_name
,
663 .pm
= &tpm_ibmvtpm_pm_ops
,
667 * ibmvtpm_module_init - Initialize ibm vtpm module
673 static int __init
ibmvtpm_module_init(void)
675 return vio_register_driver(&ibmvtpm_driver
);
679 * ibmvtpm_module_exit - Teardown ibm vtpm module
684 static void __exit
ibmvtpm_module_exit(void)
686 vio_unregister_driver(&ibmvtpm_driver
);
689 module_init(ibmvtpm_module_init
);
690 module_exit(ibmvtpm_module_exit
);
692 MODULE_AUTHOR("adlai@us.ibm.com");
693 MODULE_DESCRIPTION("IBM vTPM Driver");
694 MODULE_VERSION("1.0");
695 MODULE_LICENSE("GPL");