1
2
3
4
5
6
7
8
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 "../vsec.h"
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
23
24
25
26
27
28
29
30static const struct pci_device_id pmt_telem_early_client_pci_ids[] = {
31 { PCI_VDEVICE(INTEL, 0x467d) },
32 { PCI_VDEVICE(INTEL, 0x490e) },
33 { PCI_VDEVICE(INTEL, 0x9a0d) },
34 { }
35};
36
37bool intel_pmt_is_early_client_hw(struct device *dev)
38{
39 struct pci_dev *parent = to_pci_dev(dev->parent);
40
41 return !!pci_match_id(pmt_telem_early_client_pci_ids, parent);
42}
43EXPORT_SYMBOL_GPL(intel_pmt_is_early_client_hw);
44
45
46
47
48static ssize_t
49intel_pmt_read(struct file *filp, struct kobject *kobj,
50 struct bin_attribute *attr, char *buf, loff_t off,
51 size_t count)
52{
53 struct intel_pmt_entry *entry = container_of(attr,
54 struct intel_pmt_entry,
55 pmt_bin_attr);
56
57 if (off < 0)
58 return -EINVAL;
59
60 if (off >= entry->size)
61 return 0;
62
63 if (count > entry->size - off)
64 count = entry->size - off;
65
66 memcpy_fromio(buf, entry->base + off, count);
67
68 return count;
69}
70
71static int
72intel_pmt_mmap(struct file *filp, struct kobject *kobj,
73 struct bin_attribute *attr, struct vm_area_struct *vma)
74{
75 struct intel_pmt_entry *entry = container_of(attr,
76 struct intel_pmt_entry,
77 pmt_bin_attr);
78 unsigned long vsize = vma->vm_end - vma->vm_start;
79 struct device *dev = kobj_to_dev(kobj);
80 unsigned long phys = entry->base_addr;
81 unsigned long pfn = PFN_DOWN(phys);
82 unsigned long psize;
83
84 if (vma->vm_flags & (VM_WRITE | VM_MAYWRITE))
85 return -EROFS;
86
87 psize = (PFN_UP(entry->base_addr + entry->size) - pfn) * PAGE_SIZE;
88 if (vsize > psize) {
89 dev_err(dev, "Requested mmap size is too large\n");
90 return -EINVAL;
91 }
92
93 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
94 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
95 vsize, vma->vm_page_prot))
96 return -EAGAIN;
97
98 return 0;
99}
100
101static ssize_t
102guid_show(struct device *dev, struct device_attribute *attr, char *buf)
103{
104 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
105
106 return sprintf(buf, "0x%x\n", entry->guid);
107}
108static DEVICE_ATTR_RO(guid);
109
110static ssize_t size_show(struct device *dev, struct device_attribute *attr,
111 char *buf)
112{
113 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
114
115 return sprintf(buf, "%zu\n", entry->size);
116}
117static DEVICE_ATTR_RO(size);
118
119static ssize_t
120offset_show(struct device *dev, struct device_attribute *attr, char *buf)
121{
122 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
123
124 return sprintf(buf, "%lu\n", offset_in_page(entry->base_addr));
125}
126static DEVICE_ATTR_RO(offset);
127
128static struct attribute *intel_pmt_attrs[] = {
129 &dev_attr_guid.attr,
130 &dev_attr_size.attr,
131 &dev_attr_offset.attr,
132 NULL
133};
134ATTRIBUTE_GROUPS(intel_pmt);
135
136static struct class intel_pmt_class = {
137 .name = "intel_pmt",
138 .owner = THIS_MODULE,
139 .dev_groups = intel_pmt_groups,
140};
141
142static int intel_pmt_populate_entry(struct intel_pmt_entry *entry,
143 struct intel_pmt_header *header,
144 struct device *dev,
145 struct resource *disc_res)
146{
147 struct pci_dev *pci_dev = to_pci_dev(dev->parent);
148 u8 bir;
149
150
151
152
153
154
155
156
157 bir = GET_BIR(header->base_offset);
158
159
160 switch (header->access_type) {
161 case ACCESS_LOCAL:
162 if (bir) {
163 dev_err(dev,
164 "Unsupported BAR index %d for access type %d\n",
165 bir, header->access_type);
166 return -EINVAL;
167 }
168
169
170
171
172 entry->base_addr = disc_res->end + 1 + header->base_offset;
173
174
175
176
177
178
179
180
181
182 if (intel_pmt_is_early_client_hw(dev)) {
183 int i;
184
185 entry->base_addr = 0;
186 for (i = 0; i < 6; i++)
187 if (disc_res->start >= pci_resource_start(pci_dev, i) &&
188 (disc_res->start <= pci_resource_end(pci_dev, i))) {
189 entry->base_addr = pci_resource_start(pci_dev, i) +
190 header->base_offset;
191 break;
192 }
193 if (!entry->base_addr)
194 return -EINVAL;
195 }
196
197 break;
198 case ACCESS_BARID:
199
200
201
202
203
204 entry->base_addr = pci_resource_start(pci_dev, bir) +
205 GET_ADDRESS(header->base_offset);
206 break;
207 default:
208 dev_err(dev, "Unsupported access type %d\n",
209 header->access_type);
210 return -EINVAL;
211 }
212
213 entry->guid = header->guid;
214 entry->size = header->size;
215
216 return 0;
217}
218
219static int intel_pmt_dev_register(struct intel_pmt_entry *entry,
220 struct intel_pmt_namespace *ns,
221 struct device *parent)
222{
223 struct resource res = {0};
224 struct device *dev;
225 int ret;
226
227 ret = xa_alloc(ns->xa, &entry->devid, entry, PMT_XA_LIMIT, GFP_KERNEL);
228 if (ret)
229 return ret;
230
231 dev = device_create(&intel_pmt_class, parent, MKDEV(0, 0), entry,
232 "%s%d", ns->name, entry->devid);
233
234 if (IS_ERR(dev)) {
235 dev_err(parent, "Could not create %s%d device node\n",
236 ns->name, entry->devid);
237 ret = PTR_ERR(dev);
238 goto fail_dev_create;
239 }
240
241 entry->kobj = &dev->kobj;
242
243 if (ns->attr_grp) {
244 ret = sysfs_create_group(entry->kobj, ns->attr_grp);
245 if (ret)
246 goto fail_sysfs;
247 }
248
249
250 if (!entry->size)
251 return 0;
252
253 res.start = entry->base_addr;
254 res.end = res.start + entry->size - 1;
255 res.flags = IORESOURCE_MEM;
256
257 entry->base = devm_ioremap_resource(dev, &res);
258 if (IS_ERR(entry->base)) {
259 ret = PTR_ERR(entry->base);
260 goto fail_ioremap;
261 }
262
263 sysfs_bin_attr_init(&entry->pmt_bin_attr);
264 entry->pmt_bin_attr.attr.name = ns->name;
265 entry->pmt_bin_attr.attr.mode = 0440;
266 entry->pmt_bin_attr.mmap = intel_pmt_mmap;
267 entry->pmt_bin_attr.read = intel_pmt_read;
268 entry->pmt_bin_attr.size = entry->size;
269
270 ret = sysfs_create_bin_file(&dev->kobj, &entry->pmt_bin_attr);
271 if (!ret)
272 return 0;
273
274fail_ioremap:
275 if (ns->attr_grp)
276 sysfs_remove_group(entry->kobj, ns->attr_grp);
277fail_sysfs:
278 device_unregister(dev);
279fail_dev_create:
280 xa_erase(ns->xa, entry->devid);
281
282 return ret;
283}
284
285int intel_pmt_dev_create(struct intel_pmt_entry *entry, struct intel_pmt_namespace *ns,
286 struct intel_vsec_device *intel_vsec_dev, int idx)
287{
288 struct device *dev = &intel_vsec_dev->auxdev.dev;
289 struct intel_pmt_header header;
290 struct resource *disc_res;
291 int ret;
292
293 disc_res = &intel_vsec_dev->resource[idx];
294
295 entry->disc_table = devm_ioremap_resource(dev, disc_res);
296 if (IS_ERR(entry->disc_table))
297 return PTR_ERR(entry->disc_table);
298
299 ret = ns->pmt_header_decode(entry, &header, dev);
300 if (ret)
301 return ret;
302
303 ret = intel_pmt_populate_entry(entry, &header, dev, disc_res);
304 if (ret)
305 return ret;
306
307 return intel_pmt_dev_register(entry, ns, dev);
308
309}
310EXPORT_SYMBOL_GPL(intel_pmt_dev_create);
311
312void intel_pmt_dev_destroy(struct intel_pmt_entry *entry,
313 struct intel_pmt_namespace *ns)
314{
315 struct device *dev = kobj_to_dev(entry->kobj);
316
317 if (entry->size)
318 sysfs_remove_bin_file(entry->kobj, &entry->pmt_bin_attr);
319
320 if (ns->attr_grp)
321 sysfs_remove_group(entry->kobj, ns->attr_grp);
322
323 device_unregister(dev);
324 xa_erase(ns->xa, entry->devid);
325}
326EXPORT_SYMBOL_GPL(intel_pmt_dev_destroy);
327
328static int __init pmt_class_init(void)
329{
330 return class_register(&intel_pmt_class);
331}
332
333static void __exit pmt_class_exit(void)
334{
335 class_unregister(&intel_pmt_class);
336}
337
338module_init(pmt_class_init);
339module_exit(pmt_class_exit);
340
341MODULE_AUTHOR("Alexander Duyck <alexander.h.duyck@linux.intel.com>");
342MODULE_DESCRIPTION("Intel PMT Class driver");
343MODULE_LICENSE("GPL v2");
344