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