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/kernel.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <linux/overflow.h>
17 
18 #include "class.h"
19 
20 #define TELEM_DEV_NAME		"pmt_telemetry"
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 /* size is in bytes */
27 #define TELEM_SIZE(v)		(((v) & GENMASK(27, 12)) >> 10)
28 
29 /* Used by client hardware to identify a fixed telemetry entry*/
30 #define TELEM_CLIENT_FIXED_BLOCK_GUID	0x10000000
31 
32 struct pmt_telem_priv {
33 	int				num_entries;
34 	struct intel_pmt_entry		entry[];
35 };
36 
37 static bool pmt_telem_region_overlaps(struct intel_pmt_entry *entry,
38 				      struct device *dev)
39 {
40 	u32 guid = readl(entry->disc_table + TELEM_GUID_OFFSET);
41 
42 	if (guid != TELEM_CLIENT_FIXED_BLOCK_GUID)
43 		return false;
44 
45 	return intel_pmt_is_early_client_hw(dev);
46 }
47 
48 static int pmt_telem_header_decode(struct intel_pmt_entry *entry,
49 				   struct intel_pmt_header *header,
50 				   struct device *dev)
51 {
52 	void __iomem *disc_table = entry->disc_table;
53 
54 	if (pmt_telem_region_overlaps(entry, dev))
55 		return 1;
56 
57 	header->access_type = TELEM_ACCESS(readl(disc_table));
58 	header->guid = readl(disc_table + TELEM_GUID_OFFSET);
59 	header->base_offset = readl(disc_table + TELEM_BASE_OFFSET);
60 
61 	/* Size is measured in DWORDS, but accessor returns bytes */
62 	header->size = TELEM_SIZE(readl(disc_table));
63 
64 	/*
65 	 * Some devices may expose non-functioning entries that are
66 	 * reserved for future use. They have zero size. Do not fail
67 	 * probe for these. Just ignore them.
68 	 */
69 	if (header->size == 0)
70 		return 1;
71 
72 	return 0;
73 }
74 
75 static DEFINE_XARRAY_ALLOC(telem_array);
76 static struct intel_pmt_namespace pmt_telem_ns = {
77 	.name = "telem",
78 	.xa = &telem_array,
79 	.pmt_header_decode = pmt_telem_header_decode,
80 };
81 
82 static int pmt_telem_remove(struct platform_device *pdev)
83 {
84 	struct pmt_telem_priv *priv = platform_get_drvdata(pdev);
85 	int i;
86 
87 	for (i = 0; i < priv->num_entries; i++)
88 		intel_pmt_dev_destroy(&priv->entry[i], &pmt_telem_ns);
89 
90 	return 0;
91 }
92 
93 static int pmt_telem_probe(struct platform_device *pdev)
94 {
95 	struct pmt_telem_priv *priv;
96 	size_t size;
97 	int i, ret;
98 
99 	size = struct_size(priv, entry, pdev->num_resources);
100 	priv = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
101 	if (!priv)
102 		return -ENOMEM;
103 
104 	platform_set_drvdata(pdev, priv);
105 
106 	for (i = 0; i < pdev->num_resources; i++) {
107 		struct intel_pmt_entry *entry = &priv->entry[i];
108 
109 		ret = intel_pmt_dev_create(entry, &pmt_telem_ns, pdev, i);
110 		if (ret < 0)
111 			goto abort_probe;
112 		if (ret)
113 			continue;
114 
115 		priv->num_entries++;
116 	}
117 
118 	return 0;
119 abort_probe:
120 	pmt_telem_remove(pdev);
121 	return ret;
122 }
123 
124 static struct platform_driver pmt_telem_driver = {
125 	.driver = {
126 		.name   = TELEM_DEV_NAME,
127 	},
128 	.remove = pmt_telem_remove,
129 	.probe  = pmt_telem_probe,
130 };
131 
132 static int __init pmt_telem_init(void)
133 {
134 	return platform_driver_register(&pmt_telem_driver);
135 }
136 module_init(pmt_telem_init);
137 
138 static void __exit pmt_telem_exit(void)
139 {
140 	platform_driver_unregister(&pmt_telem_driver);
141 	xa_destroy(&telem_array);
142 }
143 module_exit(pmt_telem_exit);
144 
145 MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
146 MODULE_DESCRIPTION("Intel PMT Telemetry driver");
147 MODULE_ALIAS("platform:" TELEM_DEV_NAME);
148 MODULE_LICENSE("GPL v2");
149