1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Platform Monitory Technology Telemetry driver
5 * Copyright (c) 2020, Intel Corporation.
11 #include <linux/kernel.h>
12 #include <linux/io-64-nonatomic-lo-hi.h>
13 #include <linux/module.h>
15 #include <linux/pci.h>
20 #define PMT_XA_START 0
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
25 bool intel_pmt_is_early_client_hw(struct device *dev)
27 struct intel_vsec_device *ivdev = dev_to_ivdev(dev);
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).
34 return !!(ivdev->info->quirks & VSEC_QUIRK_EARLY_HW);
36 EXPORT_SYMBOL_GPL(intel_pmt_is_early_client_hw);
39 pmt_memcpy64_fromio(void *to, const u64 __iomem *from, size_t count)
44 if (!IS_ALIGNED((unsigned long)from, 8))
47 for (i = 0; i < count/8; i++)
48 buf[i] = readq(&from[i]);
50 /* Copy any remaining bytes */
53 u64 tmp = readq(&from[i]);
55 memcpy(&buf[i], &tmp, remain);
65 intel_pmt_read(struct file *filp, struct kobject *kobj,
66 struct bin_attribute *attr, char *buf, loff_t off,
69 struct intel_pmt_entry *entry = container_of(attr,
70 struct intel_pmt_entry,
76 if (off >= entry->size)
79 if (count > entry->size - off)
80 count = entry->size - off;
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);
86 memcpy_fromio(buf, entry->base + off, count);
92 intel_pmt_mmap(struct file *filp, struct kobject *kobj,
93 struct bin_attribute *attr, struct vm_area_struct *vma)
95 struct intel_pmt_entry *entry = container_of(attr,
96 struct intel_pmt_entry,
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);
104 if (vma->vm_flags & (VM_WRITE | VM_MAYWRITE))
107 psize = (PFN_UP(entry->base_addr + entry->size) - pfn) * PAGE_SIZE;
109 dev_err(dev, "Requested mmap size is too large\n");
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))
122 guid_show(struct device *dev, struct device_attribute *attr, char *buf)
124 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
126 return sprintf(buf, "0x%x\n", entry->guid);
128 static DEVICE_ATTR_RO(guid);
130 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
133 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
135 return sprintf(buf, "%zu\n", entry->size);
137 static DEVICE_ATTR_RO(size);
140 offset_show(struct device *dev, struct device_attribute *attr, char *buf)
142 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
144 return sprintf(buf, "%lu\n", offset_in_page(entry->base_addr));
146 static DEVICE_ATTR_RO(offset);
148 static struct attribute *intel_pmt_attrs[] = {
151 &dev_attr_offset.attr,
154 ATTRIBUTE_GROUPS(intel_pmt);
156 static struct class intel_pmt_class = {
158 .owner = THIS_MODULE,
159 .dev_groups = intel_pmt_groups,
162 static int intel_pmt_populate_entry(struct intel_pmt_entry *entry,
163 struct intel_pmt_header *header,
165 struct resource *disc_res)
167 struct pci_dev *pci_dev = to_pci_dev(dev->parent);
171 * The base offset should always be 8 byte aligned.
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.
177 bir = GET_BIR(header->base_offset);
179 /* Local access and BARID only for now */
180 switch (header->access_type) {
184 "Unsupported BAR index %d for access type %d\n",
185 bir, header->access_type);
189 * For access_type LOCAL, the base address is as follows:
190 * base address = end of discovery region + base offset
192 entry->base_addr = disc_res->end + 1 + header->base_offset;
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.
202 if (intel_pmt_is_early_client_hw(dev)) {
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) +
213 if (!entry->base_addr)
220 * If another BAR was specified then the base offset
221 * represents the offset within that BAR. SO retrieve the
222 * address from the parent PCI device and add offset.
224 entry->base_addr = pci_resource_start(pci_dev, bir) +
225 GET_ADDRESS(header->base_offset);
228 dev_err(dev, "Unsupported access type %d\n",
229 header->access_type);
233 entry->guid = header->guid;
234 entry->size = header->size;
239 static int intel_pmt_dev_register(struct intel_pmt_entry *entry,
240 struct intel_pmt_namespace *ns,
241 struct device *parent)
243 struct resource res = {0};
247 ret = xa_alloc(ns->xa, &entry->devid, entry, PMT_XA_LIMIT, GFP_KERNEL);
251 dev = device_create(&intel_pmt_class, parent, MKDEV(0, 0), entry,
252 "%s%d", ns->name, entry->devid);
255 dev_err(parent, "Could not create %s%d device node\n",
256 ns->name, entry->devid);
258 goto fail_dev_create;
261 entry->kobj = &dev->kobj;
264 ret = sysfs_create_group(entry->kobj, ns->attr_grp);
269 /* if size is 0 assume no data buffer, so no file needed */
273 res.start = entry->base_addr;
274 res.end = res.start + entry->size - 1;
275 res.flags = IORESOURCE_MEM;
277 entry->base = devm_ioremap_resource(dev, &res);
278 if (IS_ERR(entry->base)) {
279 ret = PTR_ERR(entry->base);
283 sysfs_bin_attr_init(&entry->pmt_bin_attr);
284 entry->pmt_bin_attr.attr.name = ns->name;
285 entry->pmt_bin_attr.attr.mode = 0440;
286 entry->pmt_bin_attr.mmap = intel_pmt_mmap;
287 entry->pmt_bin_attr.read = intel_pmt_read;
288 entry->pmt_bin_attr.size = entry->size;
290 ret = sysfs_create_bin_file(&dev->kobj, &entry->pmt_bin_attr);
296 sysfs_remove_group(entry->kobj, ns->attr_grp);
298 device_unregister(dev);
300 xa_erase(ns->xa, entry->devid);
305 int intel_pmt_dev_create(struct intel_pmt_entry *entry, struct intel_pmt_namespace *ns,
306 struct intel_vsec_device *intel_vsec_dev, int idx)
308 struct device *dev = &intel_vsec_dev->auxdev.dev;
309 struct intel_pmt_header header;
310 struct resource *disc_res;
313 disc_res = &intel_vsec_dev->resource[idx];
315 entry->disc_table = devm_ioremap_resource(dev, disc_res);
316 if (IS_ERR(entry->disc_table))
317 return PTR_ERR(entry->disc_table);
319 ret = ns->pmt_header_decode(entry, &header, dev);
323 ret = intel_pmt_populate_entry(entry, &header, dev, disc_res);
327 return intel_pmt_dev_register(entry, ns, dev);
330 EXPORT_SYMBOL_GPL(intel_pmt_dev_create);
332 void intel_pmt_dev_destroy(struct intel_pmt_entry *entry,
333 struct intel_pmt_namespace *ns)
335 struct device *dev = kobj_to_dev(entry->kobj);
338 sysfs_remove_bin_file(entry->kobj, &entry->pmt_bin_attr);
341 sysfs_remove_group(entry->kobj, ns->attr_grp);
343 device_unregister(dev);
344 xa_erase(ns->xa, entry->devid);
346 EXPORT_SYMBOL_GPL(intel_pmt_dev_destroy);
348 static int __init pmt_class_init(void)
350 return class_register(&intel_pmt_class);
353 static void __exit pmt_class_exit(void)
355 class_unregister(&intel_pmt_class);
358 module_init(pmt_class_init);
359 module_exit(pmt_class_exit);
362 MODULE_DESCRIPTION("Intel PMT Class driver");
363 MODULE_LICENSE("GPL v2");