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: "Alexander Duyck" <alexander.h.duyck@linux.intel.com>
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mm.h>
14 #include <linux/pci.h>
15 
16 #include "../vsec.h"
17 #include "class.h"
18 
19 #define PMT_XA_START		0
20 #define PMT_XA_MAX		INT_MAX
21 #define PMT_XA_LIMIT		XA_LIMIT(PMT_XA_START, PMT_XA_MAX)
22 
23 /*
24  * Early implementations of PMT on client platforms have some
25  * differences from the server platforms (which use the Out Of Band
26  * Management Services Module OOBMSM). This list tracks those
27  * platforms as needed to handle those differences. Newer client
28  * platforms are expected to be fully compatible with server.
29  */
30 static const struct pci_device_id pmt_telem_early_client_pci_ids[] = {
31 	{ PCI_VDEVICE(INTEL, 0x467d) }, /* ADL */
32 	{ PCI_VDEVICE(INTEL, 0x490e) }, /* DG1 */
33 	{ PCI_VDEVICE(INTEL, 0x9a0d) }, /* TGL */
34 	{ }
35 };
36 
37 bool intel_pmt_is_early_client_hw(struct device *dev)
38 {
39 	struct pci_dev *parent = to_pci_dev(dev->parent);
40 
41 	return !!pci_match_id(pmt_telem_early_client_pci_ids, parent);
42 }
43 EXPORT_SYMBOL_GPL(intel_pmt_is_early_client_hw);
44 
45 /*
46  * sysfs
47  */
48 static ssize_t
49 intel_pmt_read(struct file *filp, struct kobject *kobj,
50 	       struct bin_attribute *attr, char *buf, loff_t off,
51 	       size_t count)
52 {
53 	struct intel_pmt_entry *entry = container_of(attr,
54 						     struct intel_pmt_entry,
55 						     pmt_bin_attr);
56 
57 	if (off < 0)
58 		return -EINVAL;
59 
60 	if (off >= entry->size)
61 		return 0;
62 
63 	if (count > entry->size - off)
64 		count = entry->size - off;
65 
66 	memcpy_fromio(buf, entry->base + off, count);
67 
68 	return count;
69 }
70 
71 static int
72 intel_pmt_mmap(struct file *filp, struct kobject *kobj,
73 		struct bin_attribute *attr, struct vm_area_struct *vma)
74 {
75 	struct intel_pmt_entry *entry = container_of(attr,
76 						     struct intel_pmt_entry,
77 						     pmt_bin_attr);
78 	unsigned long vsize = vma->vm_end - vma->vm_start;
79 	struct device *dev = kobj_to_dev(kobj);
80 	unsigned long phys = entry->base_addr;
81 	unsigned long pfn = PFN_DOWN(phys);
82 	unsigned long psize;
83 
84 	if (vma->vm_flags & (VM_WRITE | VM_MAYWRITE))
85 		return -EROFS;
86 
87 	psize = (PFN_UP(entry->base_addr + entry->size) - pfn) * PAGE_SIZE;
88 	if (vsize > psize) {
89 		dev_err(dev, "Requested mmap size is too large\n");
90 		return -EINVAL;
91 	}
92 
93 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
94 	if (io_remap_pfn_range(vma, vma->vm_start, pfn,
95 		vsize, vma->vm_page_prot))
96 		return -EAGAIN;
97 
98 	return 0;
99 }
100 
101 static ssize_t
102 guid_show(struct device *dev, struct device_attribute *attr, char *buf)
103 {
104 	struct intel_pmt_entry *entry = dev_get_drvdata(dev);
105 
106 	return sprintf(buf, "0x%x\n", entry->guid);
107 }
108 static DEVICE_ATTR_RO(guid);
109 
110 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
111 			 char *buf)
112 {
113 	struct intel_pmt_entry *entry = dev_get_drvdata(dev);
114 
115 	return sprintf(buf, "%zu\n", entry->size);
116 }
117 static DEVICE_ATTR_RO(size);
118 
119 static ssize_t
120 offset_show(struct device *dev, struct device_attribute *attr, char *buf)
121 {
122 	struct intel_pmt_entry *entry = dev_get_drvdata(dev);
123 
124 	return sprintf(buf, "%lu\n", offset_in_page(entry->base_addr));
125 }
126 static DEVICE_ATTR_RO(offset);
127 
128 static struct attribute *intel_pmt_attrs[] = {
129 	&dev_attr_guid.attr,
130 	&dev_attr_size.attr,
131 	&dev_attr_offset.attr,
132 	NULL
133 };
134 ATTRIBUTE_GROUPS(intel_pmt);
135 
136 static struct class intel_pmt_class = {
137 	.name = "intel_pmt",
138 	.owner = THIS_MODULE,
139 	.dev_groups = intel_pmt_groups,
140 };
141 
142 static int intel_pmt_populate_entry(struct intel_pmt_entry *entry,
143 				    struct intel_pmt_header *header,
144 				    struct device *dev,
145 				    struct resource *disc_res)
146 {
147 	struct pci_dev *pci_dev = to_pci_dev(dev->parent);
148 	u8 bir;
149 
150 	/*
151 	 * The base offset should always be 8 byte aligned.
152 	 *
153 	 * For non-local access types the lower 3 bits of base offset
154 	 * contains the index of the base address register where the
155 	 * telemetry can be found.
156 	 */
157 	bir = GET_BIR(header->base_offset);
158 
159 	/* Local access and BARID only for now */
160 	switch (header->access_type) {
161 	case ACCESS_LOCAL:
162 		if (bir) {
163 			dev_err(dev,
164 				"Unsupported BAR index %d for access type %d\n",
165 				bir, header->access_type);
166 			return -EINVAL;
167 		}
168 		/*
169 		 * For access_type LOCAL, the base address is as follows:
170 		 * base address = end of discovery region + base offset
171 		 */
172 		entry->base_addr = disc_res->end + 1 + header->base_offset;
173 
174 		/*
175 		 * Some hardware use a different calculation for the base address
176 		 * when access_type == ACCESS_LOCAL. On the these systems
177 		 * ACCCESS_LOCAL refers to an address in the same BAR as the
178 		 * header but at a fixed offset. But as the header address was
179 		 * supplied to the driver, we don't know which BAR it was in.
180 		 * So search for the bar whose range includes the header address.
181 		 */
182 		if (intel_pmt_is_early_client_hw(dev)) {
183 			int i;
184 
185 			entry->base_addr = 0;
186 			for (i = 0; i < 6; i++)
187 				if (disc_res->start >= pci_resource_start(pci_dev, i) &&
188 				   (disc_res->start <= pci_resource_end(pci_dev, i))) {
189 					entry->base_addr = pci_resource_start(pci_dev, i) +
190 							   header->base_offset;
191 					break;
192 				}
193 			if (!entry->base_addr)
194 				return -EINVAL;
195 		}
196 
197 		break;
198 	case ACCESS_BARID:
199 		/*
200 		 * If another BAR was specified then the base offset
201 		 * represents the offset within that BAR. SO retrieve the
202 		 * address from the parent PCI device and add offset.
203 		 */
204 		entry->base_addr = pci_resource_start(pci_dev, bir) +
205 				   GET_ADDRESS(header->base_offset);
206 		break;
207 	default:
208 		dev_err(dev, "Unsupported access type %d\n",
209 			header->access_type);
210 		return -EINVAL;
211 	}
212 
213 	entry->guid = header->guid;
214 	entry->size = header->size;
215 
216 	return 0;
217 }
218 
219 static int intel_pmt_dev_register(struct intel_pmt_entry *entry,
220 				  struct intel_pmt_namespace *ns,
221 				  struct device *parent)
222 {
223 	struct resource res = {0};
224 	struct device *dev;
225 	int ret;
226 
227 	ret = xa_alloc(ns->xa, &entry->devid, entry, PMT_XA_LIMIT, GFP_KERNEL);
228 	if (ret)
229 		return ret;
230 
231 	dev = device_create(&intel_pmt_class, parent, MKDEV(0, 0), entry,
232 			    "%s%d", ns->name, entry->devid);
233 
234 	if (IS_ERR(dev)) {
235 		dev_err(parent, "Could not create %s%d device node\n",
236 			ns->name, entry->devid);
237 		ret = PTR_ERR(dev);
238 		goto fail_dev_create;
239 	}
240 
241 	entry->kobj = &dev->kobj;
242 
243 	if (ns->attr_grp) {
244 		ret = sysfs_create_group(entry->kobj, ns->attr_grp);
245 		if (ret)
246 			goto fail_sysfs;
247 	}
248 
249 	/* if size is 0 assume no data buffer, so no file needed */
250 	if (!entry->size)
251 		return 0;
252 
253 	res.start = entry->base_addr;
254 	res.end = res.start + entry->size - 1;
255 	res.flags = IORESOURCE_MEM;
256 
257 	entry->base = devm_ioremap_resource(dev, &res);
258 	if (IS_ERR(entry->base)) {
259 		ret = PTR_ERR(entry->base);
260 		goto fail_ioremap;
261 	}
262 
263 	sysfs_bin_attr_init(&entry->pmt_bin_attr);
264 	entry->pmt_bin_attr.attr.name = ns->name;
265 	entry->pmt_bin_attr.attr.mode = 0440;
266 	entry->pmt_bin_attr.mmap = intel_pmt_mmap;
267 	entry->pmt_bin_attr.read = intel_pmt_read;
268 	entry->pmt_bin_attr.size = entry->size;
269 
270 	ret = sysfs_create_bin_file(&dev->kobj, &entry->pmt_bin_attr);
271 	if (!ret)
272 		return 0;
273 
274 fail_ioremap:
275 	if (ns->attr_grp)
276 		sysfs_remove_group(entry->kobj, ns->attr_grp);
277 fail_sysfs:
278 	device_unregister(dev);
279 fail_dev_create:
280 	xa_erase(ns->xa, entry->devid);
281 
282 	return ret;
283 }
284 
285 int intel_pmt_dev_create(struct intel_pmt_entry *entry, struct intel_pmt_namespace *ns,
286 			 struct intel_vsec_device *intel_vsec_dev, int idx)
287 {
288 	struct device *dev = &intel_vsec_dev->auxdev.dev;
289 	struct intel_pmt_header header;
290 	struct resource	*disc_res;
291 	int ret;
292 
293 	disc_res = &intel_vsec_dev->resource[idx];
294 
295 	entry->disc_table = devm_ioremap_resource(dev, disc_res);
296 	if (IS_ERR(entry->disc_table))
297 		return PTR_ERR(entry->disc_table);
298 
299 	ret = ns->pmt_header_decode(entry, &header, dev);
300 	if (ret)
301 		return ret;
302 
303 	ret = intel_pmt_populate_entry(entry, &header, dev, disc_res);
304 	if (ret)
305 		return ret;
306 
307 	return intel_pmt_dev_register(entry, ns, dev);
308 
309 }
310 EXPORT_SYMBOL_GPL(intel_pmt_dev_create);
311 
312 void intel_pmt_dev_destroy(struct intel_pmt_entry *entry,
313 			   struct intel_pmt_namespace *ns)
314 {
315 	struct device *dev = kobj_to_dev(entry->kobj);
316 
317 	if (entry->size)
318 		sysfs_remove_bin_file(entry->kobj, &entry->pmt_bin_attr);
319 
320 	if (ns->attr_grp)
321 		sysfs_remove_group(entry->kobj, ns->attr_grp);
322 
323 	device_unregister(dev);
324 	xa_erase(ns->xa, entry->devid);
325 }
326 EXPORT_SYMBOL_GPL(intel_pmt_dev_destroy);
327 
328 static int __init pmt_class_init(void)
329 {
330 	return class_register(&intel_pmt_class);
331 }
332 
333 static void __exit pmt_class_exit(void)
334 {
335 	class_unregister(&intel_pmt_class);
336 }
337 
338 module_init(pmt_class_init);
339 module_exit(pmt_class_exit);
340 
341 MODULE_AUTHOR("Alexander Duyck <alexander.h.duyck@linux.intel.com>");
342 MODULE_DESCRIPTION("Intel PMT Class driver");
343 MODULE_LICENSE("GPL v2");
344