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