xref: /linux/drivers/char/tpm/eventlog/of.c (revision 4d40b59d8b0568769d10ac9b2a97e0af0a39d371)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2012 IBM Corporation
4  *
5  * Author: Ashley Lai <ashleydlai@gmail.com>
6  *         Nayna Jain <nayna@linux.vnet.ibm.com>
7  *
8  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
9  *
10  * Read the event log created by the firmware on PPC64
11  */
12 
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/io.h>
16 #include <linux/ioport.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/of_reserved_mem.h>
20 #include <linux/tpm_eventlog.h>
21 
22 #include "../tpm.h"
23 #include "common.h"
24 
tpm_read_log_memory_region(struct tpm_chip * chip)25 static int tpm_read_log_memory_region(struct tpm_chip *chip)
26 {
27 	struct resource res;
28 	int rc;
29 
30 	rc = of_reserved_mem_region_to_resource(chip->dev.parent->of_node, 0, &res);
31 	if (rc)
32 		return rc;
33 
34 	chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
35 						 MEMREMAP_WB);
36 	if (IS_ERR(chip->log.bios_event_log))
37 		return -ENOMEM;
38 
39 	chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
40 
41 	return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
42 		EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
43 }
44 
tpm_read_log_of(struct tpm_chip * chip)45 int tpm_read_log_of(struct tpm_chip *chip)
46 {
47 	struct device_node *np;
48 	const u32 *sizep;
49 	const u64 *basep;
50 	struct tpm_bios_log *log;
51 	u32 size;
52 	u64 base;
53 
54 	log = &chip->log;
55 	if (chip->dev.parent && chip->dev.parent->of_node)
56 		np = chip->dev.parent->of_node;
57 	else
58 		return -ENODEV;
59 
60 	if (of_property_read_bool(np, "powered-while-suspended"))
61 		chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
62 
63 	sizep = of_get_property(np, "linux,sml-size", NULL);
64 	basep = of_get_property(np, "linux,sml-base", NULL);
65 	if (sizep == NULL && basep == NULL)
66 		return tpm_read_log_memory_region(chip);
67 	if (sizep == NULL || basep == NULL)
68 		return -EIO;
69 
70 	/*
71 	 * For both vtpm/tpm, firmware has log addr and log size in big
72 	 * endian format. But in case of vtpm, there is a method called
73 	 * sml-handover which is run during kernel init even before
74 	 * device tree is setup. This sml-handover function takes care
75 	 * of endianness and writes to sml-base and sml-size in little
76 	 * endian format. For this reason, vtpm doesn't need conversion
77 	 * but physical tpm needs the conversion.
78 	 */
79 	if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
80 	    of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
81 		size = be32_to_cpup((__force __be32 *)sizep);
82 		base = be64_to_cpup((__force __be64 *)basep);
83 	} else {
84 		size = *sizep;
85 		base = *basep;
86 	}
87 
88 	if (size == 0) {
89 		dev_warn(&chip->dev, "%s: Event log area empty\n", __func__);
90 		return -EIO;
91 	}
92 
93 	log->bios_event_log = devm_kmemdup(&chip->dev, __va(base), size, GFP_KERNEL);
94 	if (!log->bios_event_log)
95 		return -ENOMEM;
96 
97 	log->bios_event_log_end = log->bios_event_log + size;
98 
99 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
100 		return EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
101 	return EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
102 }
103