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