Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / x86 / intel_pmt_class.c
blobc8939fba45090788519b71afebd2a7b7d2a1d11b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel Platform Monitory Technology Telemetry driver
5 * Copyright (c) 2020, Intel Corporation.
6 * All Rights Reserved.
8 * Author: "Alexander Duyck" <alexander.h.duyck@linux.intel.com>
9 */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mm.h>
14 #include <linux/pci.h>
16 #include "intel_pmt_class.h"
18 #define PMT_XA_START 0
19 #define PMT_XA_MAX INT_MAX
20 #define PMT_XA_LIMIT XA_LIMIT(PMT_XA_START, PMT_XA_MAX)
23 * sysfs
25 static ssize_t
26 intel_pmt_read(struct file *filp, struct kobject *kobj,
27 struct bin_attribute *attr, char *buf, loff_t off,
28 size_t count)
30 struct intel_pmt_entry *entry = container_of(attr,
31 struct intel_pmt_entry,
32 pmt_bin_attr);
34 if (off < 0)
35 return -EINVAL;
37 if (off >= entry->size)
38 return 0;
40 if (count > entry->size - off)
41 count = entry->size - off;
43 memcpy_fromio(buf, entry->base + off, count);
45 return count;
48 static int
49 intel_pmt_mmap(struct file *filp, struct kobject *kobj,
50 struct bin_attribute *attr, struct vm_area_struct *vma)
52 struct intel_pmt_entry *entry = container_of(attr,
53 struct intel_pmt_entry,
54 pmt_bin_attr);
55 unsigned long vsize = vma->vm_end - vma->vm_start;
56 struct device *dev = kobj_to_dev(kobj);
57 unsigned long phys = entry->base_addr;
58 unsigned long pfn = PFN_DOWN(phys);
59 unsigned long psize;
61 if (vma->vm_flags & (VM_WRITE | VM_MAYWRITE))
62 return -EROFS;
64 psize = (PFN_UP(entry->base_addr + entry->size) - pfn) * PAGE_SIZE;
65 if (vsize > psize) {
66 dev_err(dev, "Requested mmap size is too large\n");
67 return -EINVAL;
70 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
71 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
72 vsize, vma->vm_page_prot))
73 return -EAGAIN;
75 return 0;
78 static ssize_t
79 guid_show(struct device *dev, struct device_attribute *attr, char *buf)
81 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
83 return sprintf(buf, "0x%x\n", entry->guid);
85 static DEVICE_ATTR_RO(guid);
87 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
88 char *buf)
90 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
92 return sprintf(buf, "%zu\n", entry->size);
94 static DEVICE_ATTR_RO(size);
96 static ssize_t
97 offset_show(struct device *dev, struct device_attribute *attr, char *buf)
99 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
101 return sprintf(buf, "%lu\n", offset_in_page(entry->base_addr));
103 static DEVICE_ATTR_RO(offset);
105 static struct attribute *intel_pmt_attrs[] = {
106 &dev_attr_guid.attr,
107 &dev_attr_size.attr,
108 &dev_attr_offset.attr,
109 NULL
111 ATTRIBUTE_GROUPS(intel_pmt);
113 static struct class intel_pmt_class = {
114 .name = "intel_pmt",
115 .owner = THIS_MODULE,
116 .dev_groups = intel_pmt_groups,
119 static int intel_pmt_populate_entry(struct intel_pmt_entry *entry,
120 struct intel_pmt_header *header,
121 struct device *dev,
122 struct resource *disc_res)
124 struct pci_dev *pci_dev = to_pci_dev(dev->parent);
125 u8 bir;
128 * The base offset should always be 8 byte aligned.
130 * For non-local access types the lower 3 bits of base offset
131 * contains the index of the base address register where the
132 * telemetry can be found.
134 bir = GET_BIR(header->base_offset);
136 /* Local access and BARID only for now */
137 switch (header->access_type) {
138 case ACCESS_LOCAL:
139 if (bir) {
140 dev_err(dev,
141 "Unsupported BAR index %d for access type %d\n",
142 bir, header->access_type);
143 return -EINVAL;
146 * For access_type LOCAL, the base address is as follows:
147 * base address = end of discovery region + base offset
149 entry->base_addr = disc_res->end + 1 + header->base_offset;
150 break;
151 case ACCESS_BARID:
153 * If another BAR was specified then the base offset
154 * represents the offset within that BAR. SO retrieve the
155 * address from the parent PCI device and add offset.
157 entry->base_addr = pci_resource_start(pci_dev, bir) +
158 GET_ADDRESS(header->base_offset);
159 break;
160 default:
161 dev_err(dev, "Unsupported access type %d\n",
162 header->access_type);
163 return -EINVAL;
166 entry->guid = header->guid;
167 entry->size = header->size;
169 return 0;
172 static int intel_pmt_dev_register(struct intel_pmt_entry *entry,
173 struct intel_pmt_namespace *ns,
174 struct device *parent)
176 struct resource res;
177 struct device *dev;
178 int ret;
180 ret = xa_alloc(ns->xa, &entry->devid, entry, PMT_XA_LIMIT, GFP_KERNEL);
181 if (ret)
182 return ret;
184 dev = device_create(&intel_pmt_class, parent, MKDEV(0, 0), entry,
185 "%s%d", ns->name, entry->devid);
187 if (IS_ERR(dev)) {
188 dev_err(parent, "Could not create %s%d device node\n",
189 ns->name, entry->devid);
190 ret = PTR_ERR(dev);
191 goto fail_dev_create;
194 entry->kobj = &dev->kobj;
196 if (ns->attr_grp) {
197 ret = sysfs_create_group(entry->kobj, ns->attr_grp);
198 if (ret)
199 goto fail_sysfs;
202 /* if size is 0 assume no data buffer, so no file needed */
203 if (!entry->size)
204 return 0;
206 res.start = entry->base_addr;
207 res.end = res.start + entry->size - 1;
208 res.flags = IORESOURCE_MEM;
210 entry->base = devm_ioremap_resource(dev, &res);
211 if (IS_ERR(entry->base)) {
212 ret = PTR_ERR(entry->base);
213 goto fail_ioremap;
216 sysfs_bin_attr_init(&entry->pmt_bin_attr);
217 entry->pmt_bin_attr.attr.name = ns->name;
218 entry->pmt_bin_attr.attr.mode = 0440;
219 entry->pmt_bin_attr.mmap = intel_pmt_mmap;
220 entry->pmt_bin_attr.read = intel_pmt_read;
221 entry->pmt_bin_attr.size = entry->size;
223 ret = sysfs_create_bin_file(&dev->kobj, &entry->pmt_bin_attr);
224 if (!ret)
225 return 0;
227 fail_ioremap:
228 if (ns->attr_grp)
229 sysfs_remove_group(entry->kobj, ns->attr_grp);
230 fail_sysfs:
231 device_unregister(dev);
232 fail_dev_create:
233 xa_erase(ns->xa, entry->devid);
235 return ret;
238 int intel_pmt_dev_create(struct intel_pmt_entry *entry,
239 struct intel_pmt_namespace *ns,
240 struct platform_device *pdev, int idx)
242 struct intel_pmt_header header;
243 struct resource *disc_res;
244 int ret = -ENODEV;
246 disc_res = platform_get_resource(pdev, IORESOURCE_MEM, idx);
247 if (!disc_res)
248 return ret;
250 entry->disc_table = devm_platform_ioremap_resource(pdev, idx);
251 if (IS_ERR(entry->disc_table))
252 return PTR_ERR(entry->disc_table);
254 ret = ns->pmt_header_decode(entry, &header, &pdev->dev);
255 if (ret)
256 return ret;
258 ret = intel_pmt_populate_entry(entry, &header, &pdev->dev, disc_res);
259 if (ret)
260 return ret;
262 return intel_pmt_dev_register(entry, ns, &pdev->dev);
265 EXPORT_SYMBOL_GPL(intel_pmt_dev_create);
267 void intel_pmt_dev_destroy(struct intel_pmt_entry *entry,
268 struct intel_pmt_namespace *ns)
270 struct device *dev = kobj_to_dev(entry->kobj);
272 if (entry->size)
273 sysfs_remove_bin_file(entry->kobj, &entry->pmt_bin_attr);
275 if (ns->attr_grp)
276 sysfs_remove_group(entry->kobj, ns->attr_grp);
278 device_unregister(dev);
279 xa_erase(ns->xa, entry->devid);
281 EXPORT_SYMBOL_GPL(intel_pmt_dev_destroy);
283 static int __init pmt_class_init(void)
285 return class_register(&intel_pmt_class);
288 static void __exit pmt_class_exit(void)
290 class_unregister(&intel_pmt_class);
293 module_init(pmt_class_init);
294 module_exit(pmt_class_exit);
296 MODULE_AUTHOR("Alexander Duyck <alexander.h.duyck@linux.intel.com>");
297 MODULE_DESCRIPTION("Intel PMT Class driver");
298 MODULE_LICENSE("GPL v2");