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 __be64
*word
= (__be64
*)&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
= cpu_to_be16(count
);
149 crq
.data
= cpu_to_be32(ibmvtpm
->rtce_dma_handle
);
151 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, be64_to_cpu(word
[0]),
152 be64_to_cpu(word
[1]));
153 if (rc
!= H_SUCCESS
) {
154 dev_err(ibmvtpm
->dev
, "tpm_ibmvtpm_send failed rc=%d\n", rc
);
159 spin_unlock(&ibmvtpm
->rtce_lock
);
163 static void tpm_ibmvtpm_cancel(struct tpm_chip
*chip
)
168 static u8
tpm_ibmvtpm_status(struct tpm_chip
*chip
)
174 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
175 * @ibmvtpm: vtpm device struct
181 static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev
*ibmvtpm
)
183 struct ibmvtpm_crq crq
;
184 u64
*buf
= (u64
*) &crq
;
187 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
188 crq
.msg
= (u8
)VTPM_GET_RTCE_BUFFER_SIZE
;
190 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, cpu_to_be64(buf
[0]),
191 cpu_to_be64(buf
[1]));
193 dev_err(ibmvtpm
->dev
,
194 "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc
);
200 * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
201 * - Note that this is vtpm version and not tpm version
202 * @ibmvtpm: vtpm device struct
208 static int ibmvtpm_crq_get_version(struct ibmvtpm_dev
*ibmvtpm
)
210 struct ibmvtpm_crq crq
;
211 u64
*buf
= (u64
*) &crq
;
214 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
215 crq
.msg
= (u8
)VTPM_GET_VERSION
;
217 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, cpu_to_be64(buf
[0]),
218 cpu_to_be64(buf
[1]));
220 dev_err(ibmvtpm
->dev
,
221 "ibmvtpm_crq_get_version failed rc=%d\n", rc
);
227 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
228 * @ibmvtpm: vtpm device struct
234 static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev
*ibmvtpm
)
238 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, INIT_CRQ_COMP_CMD
, 0);
240 dev_err(ibmvtpm
->dev
,
241 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc
);
247 * ibmvtpm_crq_send_init - Send a CRQ initialize message
248 * @ibmvtpm: vtpm device struct
254 static int ibmvtpm_crq_send_init(struct ibmvtpm_dev
*ibmvtpm
)
258 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, INIT_CRQ_CMD
, 0);
260 dev_err(ibmvtpm
->dev
,
261 "ibmvtpm_crq_send_init failed rc=%d\n", rc
);
267 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
268 * @vdev: vio device struct
273 static int tpm_ibmvtpm_remove(struct vio_dev
*vdev
)
275 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(&vdev
->dev
);
276 struct tpm_chip
*chip
= dev_get_drvdata(ibmvtpm
->dev
);
279 tpm_chip_unregister(chip
);
281 free_irq(vdev
->irq
, ibmvtpm
);
286 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
287 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
289 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->crq_dma_handle
,
290 CRQ_RES_BUF_SIZE
, DMA_BIDIRECTIONAL
);
291 free_page((unsigned long)ibmvtpm
->crq_queue
.crq_addr
);
293 if (ibmvtpm
->rtce_buf
) {
294 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->rtce_dma_handle
,
295 ibmvtpm
->rtce_size
, DMA_BIDIRECTIONAL
);
296 kfree(ibmvtpm
->rtce_buf
);
305 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
306 * @vdev: vio device struct
309 * Number of bytes the driver needs to DMA map
311 static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev
*vdev
)
313 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(&vdev
->dev
);
315 /* ibmvtpm initializes at probe time, so the data we are
316 * asking for may not be set yet. Estimate that 4K required
317 * for TCE-mapped buffer in addition to CRQ.
320 return CRQ_RES_BUF_SIZE
+ PAGE_SIZE
;
322 return CRQ_RES_BUF_SIZE
+ ibmvtpm
->rtce_size
;
326 * tpm_ibmvtpm_suspend - Suspend
327 * @dev: device struct
332 static int tpm_ibmvtpm_suspend(struct device
*dev
)
334 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(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 ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(dev
);
392 rc
= plpar_hcall_norets(H_ENABLE_CRQ
,
393 ibmvtpm
->vdev
->unit_address
);
394 } while (rc
== H_IN_PROGRESS
|| rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
397 dev_err(dev
, "Error enabling ibmvtpm rc=%d\n", rc
);
401 rc
= vio_enable_interrupts(ibmvtpm
->vdev
);
403 dev_err(dev
, "Error vio_enable_interrupts rc=%d\n", rc
);
407 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
409 dev_err(dev
, "Error send_init rc=%d\n", rc
);
414 static bool tpm_ibmvtpm_req_canceled(struct tpm_chip
*chip
, u8 status
)
416 return (status
== 0);
419 static const struct tpm_class_ops tpm_ibmvtpm
= {
420 .recv
= tpm_ibmvtpm_recv
,
421 .send
= tpm_ibmvtpm_send
,
422 .cancel
= tpm_ibmvtpm_cancel
,
423 .status
= tpm_ibmvtpm_status
,
424 .req_complete_mask
= 0,
425 .req_complete_val
= 0,
426 .req_canceled
= tpm_ibmvtpm_req_canceled
,
429 static const struct dev_pm_ops tpm_ibmvtpm_pm_ops
= {
430 .suspend
= tpm_ibmvtpm_suspend
,
431 .resume
= tpm_ibmvtpm_resume
,
435 * ibmvtpm_crq_get_next - Get next responded crq
436 * @ibmvtpm vtpm device struct
441 static struct ibmvtpm_crq
*ibmvtpm_crq_get_next(struct ibmvtpm_dev
*ibmvtpm
)
443 struct ibmvtpm_crq_queue
*crq_q
= &ibmvtpm
->crq_queue
;
444 struct ibmvtpm_crq
*crq
= &crq_q
->crq_addr
[crq_q
->index
];
446 if (crq
->valid
& VTPM_MSG_RES
) {
447 if (++crq_q
->index
== crq_q
->num_entry
)
456 * ibmvtpm_crq_process - Process responded crq
457 * @crq crq to be processed
458 * @ibmvtpm vtpm device struct
463 static void ibmvtpm_crq_process(struct ibmvtpm_crq
*crq
,
464 struct ibmvtpm_dev
*ibmvtpm
)
468 switch (crq
->valid
) {
472 dev_info(ibmvtpm
->dev
, "CRQ initialized\n");
473 rc
= ibmvtpm_crq_send_init_complete(ibmvtpm
);
475 dev_err(ibmvtpm
->dev
, "Unable to send CRQ init complete rc=%d\n", rc
);
477 case INIT_CRQ_COMP_RES
:
478 dev_info(ibmvtpm
->dev
,
479 "CRQ initialization completed\n");
482 dev_err(ibmvtpm
->dev
, "Unknown crq message type: %d\n", crq
->msg
);
485 case IBMVTPM_VALID_CMD
:
487 case VTPM_GET_RTCE_BUFFER_SIZE_RES
:
488 if (be16_to_cpu(crq
->len
) <= 0) {
489 dev_err(ibmvtpm
->dev
, "Invalid rtce size\n");
492 ibmvtpm
->rtce_size
= be16_to_cpu(crq
->len
);
493 ibmvtpm
->rtce_buf
= kmalloc(ibmvtpm
->rtce_size
,
495 if (!ibmvtpm
->rtce_buf
) {
496 dev_err(ibmvtpm
->dev
, "Failed to allocate memory for rtce buffer\n");
500 ibmvtpm
->rtce_dma_handle
= dma_map_single(ibmvtpm
->dev
,
501 ibmvtpm
->rtce_buf
, ibmvtpm
->rtce_size
,
504 if (dma_mapping_error(ibmvtpm
->dev
,
505 ibmvtpm
->rtce_dma_handle
)) {
506 kfree(ibmvtpm
->rtce_buf
);
507 ibmvtpm
->rtce_buf
= NULL
;
508 dev_err(ibmvtpm
->dev
, "Failed to dma map rtce buffer\n");
512 case VTPM_GET_VERSION_RES
:
513 ibmvtpm
->vtpm_version
= be32_to_cpu(crq
->data
);
515 case VTPM_TPM_COMMAND_RES
:
516 /* len of the data in rtce buffer */
517 ibmvtpm
->res_len
= be16_to_cpu(crq
->len
);
518 wake_up_interruptible(&ibmvtpm
->wq
);
528 * ibmvtpm_interrupt - Interrupt handler
529 * @irq: irq number to handle
530 * @vtpm_instance: vtpm that received interrupt
535 static irqreturn_t
ibmvtpm_interrupt(int irq
, void *vtpm_instance
)
537 struct ibmvtpm_dev
*ibmvtpm
= (struct ibmvtpm_dev
*) vtpm_instance
;
538 struct ibmvtpm_crq
*crq
;
540 /* while loop is needed for initial setup (get version and
541 * get rtce_size). There should be only one tpm request at any
544 while ((crq
= ibmvtpm_crq_get_next(ibmvtpm
)) != NULL
) {
545 ibmvtpm_crq_process(crq
, ibmvtpm
);
554 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
555 * @vio_dev: vio device struct
556 * @id: vio device id struct
562 static int tpm_ibmvtpm_probe(struct vio_dev
*vio_dev
,
563 const struct vio_device_id
*id
)
565 struct ibmvtpm_dev
*ibmvtpm
;
566 struct device
*dev
= &vio_dev
->dev
;
567 struct ibmvtpm_crq_queue
*crq_q
;
568 struct tpm_chip
*chip
;
569 int rc
= -ENOMEM
, rc1
;
571 chip
= tpmm_chip_alloc(dev
, &tpm_ibmvtpm
);
573 return PTR_ERR(chip
);
575 ibmvtpm
= kzalloc(sizeof(struct ibmvtpm_dev
), GFP_KERNEL
);
577 dev_err(dev
, "kzalloc for ibmvtpm failed\n");
582 ibmvtpm
->vdev
= vio_dev
;
584 crq_q
= &ibmvtpm
->crq_queue
;
585 crq_q
->crq_addr
= (struct ibmvtpm_crq
*)get_zeroed_page(GFP_KERNEL
);
586 if (!crq_q
->crq_addr
) {
587 dev_err(dev
, "Unable to allocate memory for crq_addr\n");
591 crq_q
->num_entry
= CRQ_RES_BUF_SIZE
/ sizeof(*crq_q
->crq_addr
);
592 ibmvtpm
->crq_dma_handle
= dma_map_single(dev
, crq_q
->crq_addr
,
596 if (dma_mapping_error(dev
, ibmvtpm
->crq_dma_handle
)) {
597 dev_err(dev
, "dma mapping failed\n");
601 rc
= plpar_hcall_norets(H_REG_CRQ
, vio_dev
->unit_address
,
602 ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
);
603 if (rc
== H_RESOURCE
)
604 rc
= ibmvtpm_reset_crq(ibmvtpm
);
607 dev_err(dev
, "Unable to register CRQ rc=%d\n", rc
);
608 goto reg_crq_cleanup
;
611 rc
= request_irq(vio_dev
->irq
, ibmvtpm_interrupt
, 0,
612 tpm_ibmvtpm_driver_name
, ibmvtpm
);
614 dev_err(dev
, "Error %d register irq 0x%x\n", rc
, vio_dev
->irq
);
615 goto init_irq_cleanup
;
618 rc
= vio_enable_interrupts(vio_dev
);
620 dev_err(dev
, "Error %d enabling interrupts\n", rc
);
621 goto init_irq_cleanup
;
624 init_waitqueue_head(&ibmvtpm
->wq
);
628 TPM_VPRIV(chip
) = (void *)ibmvtpm
;
630 spin_lock_init(&ibmvtpm
->rtce_lock
);
632 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
634 goto init_irq_cleanup
;
636 rc
= ibmvtpm_crq_get_version(ibmvtpm
);
638 goto init_irq_cleanup
;
640 rc
= ibmvtpm_crq_get_rtce_size(ibmvtpm
);
642 goto init_irq_cleanup
;
644 return tpm_chip_register(chip
);
647 rc1
= plpar_hcall_norets(H_FREE_CRQ
, vio_dev
->unit_address
);
648 } while (rc1
== H_BUSY
|| H_IS_LONG_BUSY(rc1
));
650 dma_unmap_single(dev
, ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
,
655 free_page((unsigned long)crq_q
->crq_addr
);
662 static struct vio_driver ibmvtpm_driver
= {
663 .id_table
= tpm_ibmvtpm_device_table
,
664 .probe
= tpm_ibmvtpm_probe
,
665 .remove
= tpm_ibmvtpm_remove
,
666 .get_desired_dma
= tpm_ibmvtpm_get_desired_dma
,
667 .name
= tpm_ibmvtpm_driver_name
,
668 .pm
= &tpm_ibmvtpm_pm_ops
,
672 * ibmvtpm_module_init - Initialize ibm vtpm module
678 static int __init
ibmvtpm_module_init(void)
680 return vio_register_driver(&ibmvtpm_driver
);
684 * ibmvtpm_module_exit - Teardown ibm vtpm module
689 static void __exit
ibmvtpm_module_exit(void)
691 vio_unregister_driver(&ibmvtpm_driver
);
694 module_init(ibmvtpm_module_init
);
695 module_exit(ibmvtpm_module_exit
);
697 MODULE_AUTHOR("adlai@us.ibm.com");
698 MODULE_DESCRIPTION("IBM vTPM Driver");
699 MODULE_VERSION("1.0");
700 MODULE_LICENSE("GPL");