xref: /linux/drivers/platform/x86/intel/pmt/telemetry.c (revision 5c68005083d620b1499fc81926a514d39ae8b88c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel Platform Monitory Technology Telemetry driver
4  *
5  * Copyright (c) 2020, Intel Corporation.
6  * All Rights Reserved.
7  *
8  * Author: "David E. Box" <david.e.box@linux.intel.com>
9  */
10 
11 #include <linux/auxiliary_bus.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/slab.h>
16 #include <linux/uaccess.h>
17 #include <linux/overflow.h>
18 
19 #include "../vsec.h"
20 #include "class.h"
21 
22 #define TELEM_SIZE_OFFSET	0x0
23 #define TELEM_GUID_OFFSET	0x4
24 #define TELEM_BASE_OFFSET	0x8
25 #define TELEM_ACCESS(v)		((v) & GENMASK(3, 0))
26 #define TELEM_TYPE(v)		(((v) & GENMASK(7, 4)) >> 4)
27 /* size is in bytes */
28 #define TELEM_SIZE(v)		(((v) & GENMASK(27, 12)) >> 10)
29 
30 /* Used by client hardware to identify a fixed telemetry entry*/
31 #define TELEM_CLIENT_FIXED_BLOCK_GUID	0x10000000
32 
33 enum telem_type {
34 	TELEM_TYPE_PUNIT = 0,
35 	TELEM_TYPE_CRASHLOG,
36 	TELEM_TYPE_PUNIT_FIXED,
37 };
38 
39 struct pmt_telem_priv {
40 	int				num_entries;
41 	struct intel_pmt_entry		entry[];
42 };
43 
44 static bool pmt_telem_region_overlaps(struct intel_pmt_entry *entry,
45 				      struct device *dev)
46 {
47 	u32 guid = readl(entry->disc_table + TELEM_GUID_OFFSET);
48 
49 	if (intel_pmt_is_early_client_hw(dev)) {
50 		u32 type = TELEM_TYPE(readl(entry->disc_table));
51 
52 		if ((type == TELEM_TYPE_PUNIT_FIXED) ||
53 		    (guid == TELEM_CLIENT_FIXED_BLOCK_GUID))
54 			return true;
55 	}
56 
57 	return false;
58 }
59 
60 static int pmt_telem_header_decode(struct intel_pmt_entry *entry,
61 				   struct intel_pmt_header *header,
62 				   struct device *dev)
63 {
64 	void __iomem *disc_table = entry->disc_table;
65 
66 	if (pmt_telem_region_overlaps(entry, dev))
67 		return 1;
68 
69 	header->access_type = TELEM_ACCESS(readl(disc_table));
70 	header->guid = readl(disc_table + TELEM_GUID_OFFSET);
71 	header->base_offset = readl(disc_table + TELEM_BASE_OFFSET);
72 
73 	/* Size is measured in DWORDS, but accessor returns bytes */
74 	header->size = TELEM_SIZE(readl(disc_table));
75 
76 	/*
77 	 * Some devices may expose non-functioning entries that are
78 	 * reserved for future use. They have zero size. Do not fail
79 	 * probe for these. Just ignore them.
80 	 */
81 	if (header->size == 0 || header->access_type == 0xF)
82 		return 1;
83 
84 	return 0;
85 }
86 
87 static DEFINE_XARRAY_ALLOC(telem_array);
88 static struct intel_pmt_namespace pmt_telem_ns = {
89 	.name = "telem",
90 	.xa = &telem_array,
91 	.pmt_header_decode = pmt_telem_header_decode,
92 };
93 
94 static void pmt_telem_remove(struct auxiliary_device *auxdev)
95 {
96 	struct pmt_telem_priv *priv = auxiliary_get_drvdata(auxdev);
97 	int i;
98 
99 	for (i = 0; i < priv->num_entries; i++)
100 		intel_pmt_dev_destroy(&priv->entry[i], &pmt_telem_ns);
101 }
102 
103 static int pmt_telem_probe(struct auxiliary_device *auxdev, const struct auxiliary_device_id *id)
104 {
105 	struct intel_vsec_device *intel_vsec_dev = auxdev_to_ivdev(auxdev);
106 	struct pmt_telem_priv *priv;
107 	size_t size;
108 	int i, ret;
109 
110 	size = struct_size(priv, entry, intel_vsec_dev->num_resources);
111 	priv = devm_kzalloc(&auxdev->dev, size, GFP_KERNEL);
112 	if (!priv)
113 		return -ENOMEM;
114 
115 	auxiliary_set_drvdata(auxdev, priv);
116 
117 	for (i = 0; i < intel_vsec_dev->num_resources; i++) {
118 		struct intel_pmt_entry *entry = &priv->entry[priv->num_entries];
119 
120 		ret = intel_pmt_dev_create(entry, &pmt_telem_ns, intel_vsec_dev, i);
121 		if (ret < 0)
122 			goto abort_probe;
123 		if (ret)
124 			continue;
125 
126 		priv->num_entries++;
127 	}
128 
129 	return 0;
130 abort_probe:
131 	pmt_telem_remove(auxdev);
132 	return ret;
133 }
134 
135 static const struct auxiliary_device_id pmt_telem_id_table[] = {
136 	{ .name = "intel_vsec.telemetry" },
137 	{}
138 };
139 MODULE_DEVICE_TABLE(auxiliary, pmt_telem_id_table);
140 
141 static struct auxiliary_driver pmt_telem_aux_driver = {
142 	.id_table	= pmt_telem_id_table,
143 	.remove		= pmt_telem_remove,
144 	.probe		= pmt_telem_probe,
145 };
146 
147 static int __init pmt_telem_init(void)
148 {
149 	return auxiliary_driver_register(&pmt_telem_aux_driver);
150 }
151 module_init(pmt_telem_init);
152 
153 static void __exit pmt_telem_exit(void)
154 {
155 	auxiliary_driver_unregister(&pmt_telem_aux_driver);
156 	xa_destroy(&telem_array);
157 }
158 module_exit(pmt_telem_exit);
159 
160 MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
161 MODULE_DESCRIPTION("Intel PMT Telemetry driver");
162 MODULE_LICENSE("GPL v2");
163 MODULE_IMPORT_NS(INTEL_PMT);
164