Merge tag 'vfio-v5.12-rc6' of git://github.com/awilliam/linux-vfio
[linux-2.6-microblaze.git] / drivers / platform / x86 / intel_pmt_class.c
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 "intel_pmt_class.h"
17
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)
21
22 /*
23  * sysfs
24  */
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)
29 {
30         struct intel_pmt_entry *entry = container_of(attr,
31                                                      struct intel_pmt_entry,
32                                                      pmt_bin_attr);
33
34         if (off < 0)
35                 return -EINVAL;
36
37         if (off >= entry->size)
38                 return 0;
39
40         if (count > entry->size - off)
41                 count = entry->size - off;
42
43         memcpy_fromio(buf, entry->base + off, count);
44
45         return count;
46 }
47
48 static int
49 intel_pmt_mmap(struct file *filp, struct kobject *kobj,
50                 struct bin_attribute *attr, struct vm_area_struct *vma)
51 {
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;
60
61         if (vma->vm_flags & (VM_WRITE | VM_MAYWRITE))
62                 return -EROFS;
63
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;
68         }
69
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;
74
75         return 0;
76 }
77
78 static ssize_t
79 guid_show(struct device *dev, struct device_attribute *attr, char *buf)
80 {
81         struct intel_pmt_entry *entry = dev_get_drvdata(dev);
82
83         return sprintf(buf, "0x%x\n", entry->guid);
84 }
85 static DEVICE_ATTR_RO(guid);
86
87 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
88                          char *buf)
89 {
90         struct intel_pmt_entry *entry = dev_get_drvdata(dev);
91
92         return sprintf(buf, "%zu\n", entry->size);
93 }
94 static DEVICE_ATTR_RO(size);
95
96 static ssize_t
97 offset_show(struct device *dev, struct device_attribute *attr, char *buf)
98 {
99         struct intel_pmt_entry *entry = dev_get_drvdata(dev);
100
101         return sprintf(buf, "%lu\n", offset_in_page(entry->base_addr));
102 }
103 static DEVICE_ATTR_RO(offset);
104
105 static struct attribute *intel_pmt_attrs[] = {
106         &dev_attr_guid.attr,
107         &dev_attr_size.attr,
108         &dev_attr_offset.attr,
109         NULL
110 };
111 ATTRIBUTE_GROUPS(intel_pmt);
112
113 static struct class intel_pmt_class = {
114         .name = "intel_pmt",
115         .owner = THIS_MODULE,
116         .dev_groups = intel_pmt_groups,
117 };
118
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)
123 {
124         struct pci_dev *pci_dev = to_pci_dev(dev->parent);
125         u8 bir;
126
127         /*
128          * The base offset should always be 8 byte aligned.
129          *
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.
133          */
134         bir = GET_BIR(header->base_offset);
135
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;
144                 }
145                 /*
146                  * For access_type LOCAL, the base address is as follows:
147                  * base address = end of discovery region + base offset
148                  */
149                 entry->base_addr = disc_res->end + 1 + header->base_offset;
150                 break;
151         case ACCESS_BARID:
152                 /*
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.
156                  */
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;
164         }
165
166         entry->guid = header->guid;
167         entry->size = header->size;
168
169         return 0;
170 }
171
172 static int intel_pmt_dev_register(struct intel_pmt_entry *entry,
173                                   struct intel_pmt_namespace *ns,
174                                   struct device *parent)
175 {
176         struct resource res = {0};
177         struct device *dev;
178         int ret;
179
180         ret = xa_alloc(ns->xa, &entry->devid, entry, PMT_XA_LIMIT, GFP_KERNEL);
181         if (ret)
182                 return ret;
183
184         dev = device_create(&intel_pmt_class, parent, MKDEV(0, 0), entry,
185                             "%s%d", ns->name, entry->devid);
186
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;
192         }
193
194         entry->kobj = &dev->kobj;
195
196         if (ns->attr_grp) {
197                 ret = sysfs_create_group(entry->kobj, ns->attr_grp);
198                 if (ret)
199                         goto fail_sysfs;
200         }
201
202         /* if size is 0 assume no data buffer, so no file needed */
203         if (!entry->size)
204                 return 0;
205
206         res.start = entry->base_addr;
207         res.end = res.start + entry->size - 1;
208         res.flags = IORESOURCE_MEM;
209
210         entry->base = devm_ioremap_resource(dev, &res);
211         if (IS_ERR(entry->base)) {
212                 ret = PTR_ERR(entry->base);
213                 goto fail_ioremap;
214         }
215
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;
222
223         ret = sysfs_create_bin_file(&dev->kobj, &entry->pmt_bin_attr);
224         if (!ret)
225                 return 0;
226
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);
234
235         return ret;
236 }
237
238 int intel_pmt_dev_create(struct intel_pmt_entry *entry,
239                          struct intel_pmt_namespace *ns,
240                          struct platform_device *pdev, int idx)
241 {
242         struct intel_pmt_header header;
243         struct resource *disc_res;
244         int ret = -ENODEV;
245
246         disc_res = platform_get_resource(pdev, IORESOURCE_MEM, idx);
247         if (!disc_res)
248                 return ret;
249
250         entry->disc_table = devm_platform_ioremap_resource(pdev, idx);
251         if (IS_ERR(entry->disc_table))
252                 return PTR_ERR(entry->disc_table);
253
254         ret = ns->pmt_header_decode(entry, &header, &pdev->dev);
255         if (ret)
256                 return ret;
257
258         ret = intel_pmt_populate_entry(entry, &header, &pdev->dev, disc_res);
259         if (ret)
260                 return ret;
261
262         return intel_pmt_dev_register(entry, ns, &pdev->dev);
263
264 }
265 EXPORT_SYMBOL_GPL(intel_pmt_dev_create);
266
267 void intel_pmt_dev_destroy(struct intel_pmt_entry *entry,
268                            struct intel_pmt_namespace *ns)
269 {
270         struct device *dev = kobj_to_dev(entry->kobj);
271
272         if (entry->size)
273                 sysfs_remove_bin_file(entry->kobj, &entry->pmt_bin_attr);
274
275         if (ns->attr_grp)
276                 sysfs_remove_group(entry->kobj, ns->attr_grp);
277
278         device_unregister(dev);
279         xa_erase(ns->xa, entry->devid);
280 }
281 EXPORT_SYMBOL_GPL(intel_pmt_dev_destroy);
282
283 static int __init pmt_class_init(void)
284 {
285         return class_register(&intel_pmt_class);
286 }
287
288 static void __exit pmt_class_exit(void)
289 {
290         class_unregister(&intel_pmt_class);
291 }
292
293 module_init(pmt_class_init);
294 module_exit(pmt_class_exit);
295
296 MODULE_AUTHOR("Alexander Duyck <alexander.h.duyck@linux.intel.com>");
297 MODULE_DESCRIPTION("Intel PMT Class driver");
298 MODULE_LICENSE("GPL v2");