2 * Copyright (C) 2014 Intel Corporation
5 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
7 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
9 * This device driver implements the TPM interface as defined in
10 * the TCG CRB 2.0 TPM specification.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; version 2
18 #include <linux/acpi.h>
19 #include <linux/highmem.h>
20 #include <linux/rculist.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
25 #define ACPI_SIG_TPM2 "TPM2"
27 static const u8 CRB_ACPI_START_UUID
[] = {
28 /* 0000 */ 0xAB, 0x6C, 0xBF, 0x6B, 0x63, 0x54, 0x14, 0x47,
29 /* 0008 */ 0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4
33 CRB_ACPI_START_REVISION_ID
= 1,
34 CRB_ACPI_START_INDEX
= 1,
38 CRB_CTRL_REQ_CMD_READY
= BIT(0),
39 CRB_CTRL_REQ_GO_IDLE
= BIT(1),
43 CRB_CTRL_STS_ERROR
= BIT(0),
44 CRB_CTRL_STS_TPM_IDLE
= BIT(1),
48 CRB_START_INVOKE
= BIT(0),
52 CRB_CANCEL_INVOKE
= BIT(0),
55 struct crb_control_area
{
70 CRB_DRV_STS_COMPLETE
= BIT(0),
74 CRB_FL_ACPI_START
= BIT(0),
75 CRB_FL_CRB_START
= BIT(1),
81 struct crb_control_area __iomem
*cca
;
88 * crb_go_idle - request tpm crb device to go the idle state
91 * @priv: crb private data
93 * Write CRB_CTRL_REQ_GO_IDLE to TPM_CRB_CTRL_REQ
94 * The device should respond within TIMEOUT_C by clearing the bit.
95 * Anyhow, we do not wait here as a consequent CMD_READY request
96 * will be handled correctly even if idle was not completed.
98 * The function does nothing for devices with ACPI-start method.
102 static int __maybe_unused
crb_go_idle(struct device
*dev
, struct crb_priv
*priv
)
104 if (priv
->flags
& CRB_FL_ACPI_START
)
107 iowrite32(CRB_CTRL_REQ_GO_IDLE
, &priv
->cca
->req
);
108 /* we don't really care when this settles */
114 * crb_cmd_ready - request tpm crb device to enter ready state
117 * @priv: crb private data
119 * Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ
120 * and poll till the device acknowledge it by clearing the bit.
121 * The device should respond within TIMEOUT_C.
123 * The function does nothing for devices with ACPI-start method
125 * Return: 0 on success -ETIME on timeout;
127 static int __maybe_unused
crb_cmd_ready(struct device
*dev
,
128 struct crb_priv
*priv
)
132 if (priv
->flags
& CRB_FL_ACPI_START
)
135 iowrite32(CRB_CTRL_REQ_CMD_READY
, &priv
->cca
->req
);
138 stop
= ktime_add(start
, ms_to_ktime(TPM2_TIMEOUT_C
));
140 if (!(ioread32(&priv
->cca
->req
) & CRB_CTRL_REQ_CMD_READY
))
142 usleep_range(50, 100);
143 } while (ktime_before(ktime_get(), stop
));
145 if (ioread32(&priv
->cca
->req
) & CRB_CTRL_REQ_CMD_READY
) {
146 dev_warn(dev
, "cmdReady timed out\n");
153 static u8
crb_status(struct tpm_chip
*chip
)
155 struct crb_priv
*priv
= dev_get_drvdata(&chip
->dev
);
158 if ((ioread32(&priv
->cca
->start
) & CRB_START_INVOKE
) !=
160 sts
|= CRB_DRV_STS_COMPLETE
;
165 static int crb_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
167 struct crb_priv
*priv
= dev_get_drvdata(&chip
->dev
);
168 unsigned int expected
;
174 if (ioread32(&priv
->cca
->sts
) & CRB_CTRL_STS_ERROR
)
177 memcpy_fromio(buf
, priv
->rsp
, 6);
178 expected
= be32_to_cpup((__be32
*) &buf
[2]);
180 if (expected
> count
)
183 memcpy_fromio(&buf
[6], &priv
->rsp
[6], expected
- 6);
188 static int crb_do_acpi_start(struct tpm_chip
*chip
)
190 union acpi_object
*obj
;
193 obj
= acpi_evaluate_dsm(chip
->acpi_dev_handle
,
195 CRB_ACPI_START_REVISION_ID
,
196 CRB_ACPI_START_INDEX
,
200 rc
= obj
->integer
.value
== 0 ? 0 : -ENXIO
;
205 static int crb_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
207 struct crb_priv
*priv
= dev_get_drvdata(&chip
->dev
);
210 /* Zero the cancel register so that the next command will not get
213 iowrite32(0, &priv
->cca
->cancel
);
215 if (len
> priv
->cmd_size
) {
216 dev_err(&chip
->dev
, "invalid command count value %zd %d\n",
217 len
, priv
->cmd_size
);
221 memcpy_toio(priv
->cmd
, buf
, len
);
223 /* Make sure that cmd is populated before issuing start. */
226 if (priv
->flags
& CRB_FL_CRB_START
)
227 iowrite32(CRB_START_INVOKE
, &priv
->cca
->start
);
229 if (priv
->flags
& CRB_FL_ACPI_START
)
230 rc
= crb_do_acpi_start(chip
);
235 static void crb_cancel(struct tpm_chip
*chip
)
237 struct crb_priv
*priv
= dev_get_drvdata(&chip
->dev
);
239 iowrite32(CRB_CANCEL_INVOKE
, &priv
->cca
->cancel
);
241 if ((priv
->flags
& CRB_FL_ACPI_START
) && crb_do_acpi_start(chip
))
242 dev_err(&chip
->dev
, "ACPI Start failed\n");
245 static bool crb_req_canceled(struct tpm_chip
*chip
, u8 status
)
247 struct crb_priv
*priv
= dev_get_drvdata(&chip
->dev
);
248 u32 cancel
= ioread32(&priv
->cca
->cancel
);
250 return (cancel
& CRB_CANCEL_INVOKE
) == CRB_CANCEL_INVOKE
;
253 static const struct tpm_class_ops tpm_crb
= {
254 .flags
= TPM_OPS_AUTO_STARTUP
,
255 .status
= crb_status
,
258 .cancel
= crb_cancel
,
259 .req_canceled
= crb_req_canceled
,
260 .req_complete_mask
= CRB_DRV_STS_COMPLETE
,
261 .req_complete_val
= CRB_DRV_STS_COMPLETE
,
264 static int crb_check_resource(struct acpi_resource
*ares
, void *data
)
266 struct resource
*io_res
= data
;
267 struct resource_win win
;
268 struct resource
*res
= &(win
.res
);
270 if (acpi_dev_resource_memory(ares
, res
) ||
271 acpi_dev_resource_address_space(ares
, &win
)) {
279 static void __iomem
*crb_map_res(struct device
*dev
, struct crb_priv
*priv
,
280 struct resource
*io_res
, u64 start
, u32 size
)
282 struct resource new_res
= {
284 .end
= start
+ size
- 1,
285 .flags
= IORESOURCE_MEM
,
288 /* Detect a 64 bit address on a 32 bit system */
289 if (start
!= new_res
.start
)
290 return (void __iomem
*) ERR_PTR(-EINVAL
);
292 if (!resource_contains(io_res
, &new_res
))
293 return devm_ioremap_resource(dev
, &new_res
);
295 return priv
->iobase
+ (new_res
.start
- io_res
->start
);
298 static int crb_map_io(struct acpi_device
*device
, struct crb_priv
*priv
,
299 struct acpi_table_tpm2
*buf
)
301 struct list_head resources
;
302 struct resource io_res
;
303 struct device
*dev
= &device
->dev
;
311 INIT_LIST_HEAD(&resources
);
312 ret
= acpi_dev_get_resources(device
, &resources
, crb_check_resource
,
316 acpi_dev_free_resource_list(&resources
);
318 if (resource_type(&io_res
) != IORESOURCE_MEM
) {
319 dev_err(dev
, FW_BUG
"TPM2 ACPI table does not define a memory resource\n");
323 priv
->iobase
= devm_ioremap_resource(dev
, &io_res
);
324 if (IS_ERR(priv
->iobase
))
325 return PTR_ERR(priv
->iobase
);
327 priv
->cca
= crb_map_res(dev
, priv
, &io_res
, buf
->control_address
,
328 sizeof(struct crb_control_area
));
329 if (IS_ERR(priv
->cca
))
330 return PTR_ERR(priv
->cca
);
333 * PTT HW bug w/a: wake up the device to access
334 * possibly not retained registers.
336 ret
= crb_cmd_ready(dev
, priv
);
340 pa_high
= ioread32(&priv
->cca
->cmd_pa_high
);
341 pa_low
= ioread32(&priv
->cca
->cmd_pa_low
);
342 cmd_pa
= ((u64
)pa_high
<< 32) | pa_low
;
343 cmd_size
= ioread32(&priv
->cca
->cmd_size
);
345 dev_dbg(dev
, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
346 pa_high
, pa_low
, cmd_size
);
348 priv
->cmd
= crb_map_res(dev
, priv
, &io_res
, cmd_pa
, cmd_size
);
349 if (IS_ERR(priv
->cmd
)) {
350 ret
= PTR_ERR(priv
->cmd
);
354 memcpy_fromio(&rsp_pa
, &priv
->cca
->rsp_pa
, 8);
355 rsp_pa
= le64_to_cpu(rsp_pa
);
356 rsp_size
= ioread32(&priv
->cca
->rsp_size
);
358 if (cmd_pa
!= rsp_pa
) {
359 priv
->rsp
= crb_map_res(dev
, priv
, &io_res
, rsp_pa
, rsp_size
);
360 ret
= PTR_ERR_OR_ZERO(priv
->rsp
);
364 /* According to the PTP specification, overlapping command and response
365 * buffer sizes must be identical.
367 if (cmd_size
!= rsp_size
) {
368 dev_err(dev
, FW_BUG
"overlapping command and response buffer sizes are not identical");
373 priv
->cmd_size
= cmd_size
;
375 priv
->rsp
= priv
->cmd
;
378 crb_go_idle(dev
, priv
);
383 static int crb_acpi_add(struct acpi_device
*device
)
385 struct acpi_table_tpm2
*buf
;
386 struct crb_priv
*priv
;
387 struct tpm_chip
*chip
;
388 struct device
*dev
= &device
->dev
;
393 status
= acpi_get_table(ACPI_SIG_TPM2
, 1,
394 (struct acpi_table_header
**) &buf
);
395 if (ACPI_FAILURE(status
) || buf
->header
.length
< sizeof(*buf
)) {
396 dev_err(dev
, FW_BUG
"failed to get TPM2 ACPI table\n");
400 /* Should the FIFO driver handle this? */
401 sm
= buf
->start_method
;
402 if (sm
== ACPI_TPM2_MEMORY_MAPPED
)
405 priv
= devm_kzalloc(dev
, sizeof(struct crb_priv
), GFP_KERNEL
);
409 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
410 * report only ACPI start but in practice seems to require both
411 * ACPI start and CRB start.
413 if (sm
== ACPI_TPM2_COMMAND_BUFFER
|| sm
== ACPI_TPM2_MEMORY_MAPPED
||
414 !strcmp(acpi_device_hid(device
), "MSFT0101"))
415 priv
->flags
|= CRB_FL_CRB_START
;
417 if (sm
== ACPI_TPM2_START_METHOD
||
418 sm
== ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD
)
419 priv
->flags
|= CRB_FL_ACPI_START
;
421 rc
= crb_map_io(device
, priv
, buf
);
425 chip
= tpmm_chip_alloc(dev
, &tpm_crb
);
427 return PTR_ERR(chip
);
429 dev_set_drvdata(&chip
->dev
, priv
);
430 chip
->acpi_dev_handle
= device
->handle
;
431 chip
->flags
= TPM_CHIP_FLAG_TPM2
;
433 rc
= crb_cmd_ready(dev
, priv
);
437 pm_runtime_get_noresume(dev
);
438 pm_runtime_set_active(dev
);
439 pm_runtime_enable(dev
);
441 rc
= tpm_chip_register(chip
);
443 crb_go_idle(dev
, priv
);
444 pm_runtime_put_noidle(dev
);
445 pm_runtime_disable(dev
);
454 static int crb_acpi_remove(struct acpi_device
*device
)
456 struct device
*dev
= &device
->dev
;
457 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
459 tpm_chip_unregister(chip
);
461 pm_runtime_disable(dev
);
467 static int crb_pm_runtime_suspend(struct device
*dev
)
469 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
470 struct crb_priv
*priv
= dev_get_drvdata(&chip
->dev
);
472 return crb_go_idle(dev
, priv
);
475 static int crb_pm_runtime_resume(struct device
*dev
)
477 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
478 struct crb_priv
*priv
= dev_get_drvdata(&chip
->dev
);
480 return crb_cmd_ready(dev
, priv
);
482 #endif /* CONFIG_PM */
484 static const struct dev_pm_ops crb_pm
= {
485 SET_SYSTEM_SLEEP_PM_OPS(tpm_pm_suspend
, tpm_pm_resume
)
486 SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend
, crb_pm_runtime_resume
, NULL
)
489 static struct acpi_device_id crb_device_ids
[] = {
493 MODULE_DEVICE_TABLE(acpi
, crb_device_ids
);
495 static struct acpi_driver crb_acpi_driver
= {
497 .ids
= crb_device_ids
,
500 .remove
= crb_acpi_remove
,
507 module_acpi_driver(crb_acpi_driver
);
508 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
509 MODULE_DESCRIPTION("TPM2 Driver");
510 MODULE_VERSION("0.1");
511 MODULE_LICENSE("GPL");