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/log2.h> 13 #include <linux/intel_vsec.h> 14 #include <linux/io-64-nonatomic-lo-hi.h> 15 #include <linux/minmax.h> 16 #include <linux/module.h> 17 #include <linux/mm.h> 18 #include <linux/pci.h> 19 #include <linux/sysfs.h> 20 21 #include "class.h" 22 23 #define PMT_XA_START 1 24 #define PMT_XA_MAX INT_MAX 25 #define PMT_XA_LIMIT XA_LIMIT(PMT_XA_START, PMT_XA_MAX) 26 #define GUID_SPR_PUNIT 0x9956f43f 27 28 bool intel_pmt_is_early_client_hw(struct device *dev) 29 { 30 struct intel_vsec_device *ivdev = dev_to_ivdev(dev); 31 32 /* 33 * Early implementations of PMT on client platforms have some 34 * differences from the server platforms (which use the Out Of Band 35 * Management Services Module OOBMSM). 36 */ 37 return !!(ivdev->quirks & VSEC_QUIRK_EARLY_HW); 38 } 39 EXPORT_SYMBOL_NS_GPL(intel_pmt_is_early_client_hw, "INTEL_PMT"); 40 41 static inline int 42 pmt_memcpy64_fromio(void *to, const u64 __iomem *from, size_t count) 43 { 44 int i, remain; 45 u64 *buf = to; 46 47 if (!IS_ALIGNED((unsigned long)from, 8)) 48 return -EFAULT; 49 50 for (i = 0; i < count/8; i++) 51 buf[i] = readq(&from[i]); 52 53 /* Copy any remaining bytes */ 54 remain = count % 8; 55 if (remain) { 56 u64 tmp = readq(&from[i]); 57 58 memcpy(&buf[i], &tmp, remain); 59 } 60 61 return count; 62 } 63 64 int pmt_telem_read_mmio(struct device *dev, struct pmt_callbacks *cb, u32 guid, void *buf, 65 void __iomem *addr, loff_t off, u32 count) 66 { 67 if (cb && cb->read_telem) 68 return cb->read_telem(dev, guid, buf, off, count); 69 70 addr += off; 71 72 if (guid == GUID_SPR_PUNIT) 73 /* PUNIT on SPR only supports aligned 64-bit read */ 74 return pmt_memcpy64_fromio(buf, addr, count); 75 76 memcpy_fromio(buf, addr, count); 77 78 return count; 79 } 80 EXPORT_SYMBOL_NS_GPL(pmt_telem_read_mmio, "INTEL_PMT"); 81 82 /* 83 * sysfs 84 */ 85 static ssize_t 86 intel_pmt_read(struct file *filp, struct kobject *kobj, 87 const struct bin_attribute *attr, char *buf, loff_t off, 88 size_t count) 89 { 90 struct intel_pmt_entry *entry = container_of(attr, 91 struct intel_pmt_entry, 92 pmt_bin_attr); 93 94 if (off < 0) 95 return -EINVAL; 96 97 if (off >= entry->size) 98 return 0; 99 100 if (count > entry->size - off) 101 count = entry->size - off; 102 103 count = pmt_telem_read_mmio(entry->ep->dev, entry->cb, entry->header.guid, buf, 104 entry->base, off, count); 105 106 return count; 107 } 108 109 static int 110 intel_pmt_mmap(struct file *filp, struct kobject *kobj, 111 const struct bin_attribute *attr, struct vm_area_struct *vma) 112 { 113 struct intel_pmt_entry *entry = container_of(attr, 114 struct intel_pmt_entry, 115 pmt_bin_attr); 116 unsigned long vsize = vma->vm_end - vma->vm_start; 117 struct device *dev = kobj_to_dev(kobj); 118 unsigned long phys = entry->base_addr; 119 unsigned long pfn = PFN_DOWN(phys); 120 unsigned long psize; 121 122 if (vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) 123 return -EROFS; 124 125 psize = (PFN_UP(entry->base_addr + entry->size) - pfn) * PAGE_SIZE; 126 if (vsize > psize) { 127 dev_err(dev, "Requested mmap size is too large\n"); 128 return -EINVAL; 129 } 130 131 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 132 if (io_remap_pfn_range(vma, vma->vm_start, pfn, 133 vsize, vma->vm_page_prot)) 134 return -EAGAIN; 135 136 return 0; 137 } 138 139 static ssize_t 140 guid_show(struct device *dev, struct device_attribute *attr, char *buf) 141 { 142 struct intel_pmt_entry *entry = dev_get_drvdata(dev); 143 144 return sysfs_emit(buf, "0x%x\n", entry->guid); 145 } 146 static DEVICE_ATTR_RO(guid); 147 148 static ssize_t size_show(struct device *dev, struct device_attribute *attr, 149 char *buf) 150 { 151 struct intel_pmt_entry *entry = dev_get_drvdata(dev); 152 153 return sysfs_emit(buf, "%zu\n", entry->size); 154 } 155 static DEVICE_ATTR_RO(size); 156 157 static ssize_t 158 offset_show(struct device *dev, struct device_attribute *attr, char *buf) 159 { 160 struct intel_pmt_entry *entry = dev_get_drvdata(dev); 161 162 return sysfs_emit(buf, "%lu\n", offset_in_page(entry->base_addr)); 163 } 164 static DEVICE_ATTR_RO(offset); 165 166 static struct attribute *intel_pmt_attrs[] = { 167 &dev_attr_guid.attr, 168 &dev_attr_size.attr, 169 &dev_attr_offset.attr, 170 NULL 171 }; 172 173 static umode_t intel_pmt_attr_visible(struct kobject *kobj, 174 struct attribute *attr, int n) 175 { 176 struct device *dev = container_of(kobj, struct device, kobj); 177 struct auxiliary_device *auxdev = to_auxiliary_dev(dev->parent); 178 struct intel_vsec_device *ivdev = auxdev_to_ivdev(auxdev); 179 180 /* 181 * Place the discovery features folder in /sys/class/intel_pmt, but 182 * exclude the common attributes as they are not applicable. 183 */ 184 if (ivdev->cap_id == ilog2(VSEC_CAP_DISCOVERY)) 185 return 0; 186 187 return attr->mode; 188 } 189 190 static bool intel_pmt_group_visible(struct kobject *kobj) 191 { 192 return true; 193 } 194 DEFINE_SYSFS_GROUP_VISIBLE(intel_pmt); 195 196 static const struct attribute_group intel_pmt_group = { 197 .attrs = intel_pmt_attrs, 198 .is_visible = SYSFS_GROUP_VISIBLE(intel_pmt), 199 }; 200 __ATTRIBUTE_GROUPS(intel_pmt); 201 202 struct class intel_pmt_class = { 203 .name = "intel_pmt", 204 .dev_groups = intel_pmt_groups, 205 }; 206 EXPORT_SYMBOL_GPL(intel_pmt_class); 207 208 static int intel_pmt_populate_entry(struct intel_pmt_entry *entry, 209 struct intel_vsec_device *ivdev, 210 int idx) 211 { 212 struct pci_dev *pci_dev = to_pci_dev(ivdev->dev); 213 struct device *dev = &ivdev->auxdev.dev; 214 struct intel_pmt_header *header = &entry->header; 215 struct resource *disc_res; 216 u8 bir; 217 218 /* 219 * The base offset should always be 8 byte aligned. 220 * 221 * For non-local access types the lower 3 bits of base offset 222 * contains the index of the base address register where the 223 * telemetry can be found. 224 */ 225 bir = GET_BIR(header->base_offset); 226 227 /* Local access and BARID only for now */ 228 switch (header->access_type) { 229 case ACCESS_LOCAL: 230 if (bir) { 231 dev_err(dev, 232 "Unsupported BAR index %d for access type %d\n", 233 bir, header->access_type); 234 return -EINVAL; 235 } 236 /* 237 * For access_type LOCAL, the base address is as follows: 238 * base address = end of discovery region + base offset 239 */ 240 disc_res = &ivdev->resource[idx]; 241 entry->base_addr = disc_res->end + 1 + header->base_offset; 242 243 /* 244 * Some hardware use a different calculation for the base address 245 * when access_type == ACCESS_LOCAL. On the these systems 246 * ACCESS_LOCAL refers to an address in the same BAR as the 247 * header but at a fixed offset. But as the header address was 248 * supplied to the driver, we don't know which BAR it was in. 249 * So search for the bar whose range includes the header address. 250 */ 251 if (intel_pmt_is_early_client_hw(dev)) { 252 int i; 253 254 entry->base_addr = 0; 255 for (i = 0; i < 6; i++) 256 if (disc_res->start >= pci_resource_start(pci_dev, i) && 257 (disc_res->start <= pci_resource_end(pci_dev, i))) { 258 entry->base_addr = pci_resource_start(pci_dev, i) + 259 header->base_offset; 260 break; 261 } 262 if (!entry->base_addr) 263 return -EINVAL; 264 } 265 266 break; 267 case ACCESS_BARID: 268 /* Use the provided base address if it exists */ 269 if (ivdev->base_addr) { 270 entry->base_addr = ivdev->base_addr + 271 GET_ADDRESS(header->base_offset); 272 break; 273 } 274 275 /* 276 * If another BAR was specified then the base offset 277 * represents the offset within that BAR. SO retrieve the 278 * address from the parent PCI device and add offset. 279 */ 280 entry->base_addr = pci_resource_start(pci_dev, bir) + 281 GET_ADDRESS(header->base_offset); 282 break; 283 default: 284 dev_err(dev, "Unsupported access type %d\n", 285 header->access_type); 286 return -EINVAL; 287 } 288 289 entry->pcidev = pci_dev; 290 entry->guid = header->guid; 291 entry->size = header->size; 292 entry->cb = ivdev->priv_data; 293 294 return 0; 295 } 296 297 static int intel_pmt_dev_register(struct intel_pmt_entry *entry, 298 struct intel_pmt_namespace *ns, 299 struct device *parent) 300 { 301 struct intel_vsec_device *ivdev = dev_to_ivdev(parent); 302 struct resource res = {0}; 303 struct device *dev; 304 int ret; 305 306 ret = xa_alloc(ns->xa, &entry->devid, entry, PMT_XA_LIMIT, GFP_KERNEL); 307 if (ret) 308 return ret; 309 310 dev = device_create(&intel_pmt_class, parent, MKDEV(0, 0), entry, 311 "%s%d", ns->name, entry->devid); 312 313 if (IS_ERR(dev)) { 314 dev_err(parent, "Could not create %s%d device node\n", 315 ns->name, entry->devid); 316 ret = PTR_ERR(dev); 317 goto fail_dev_create; 318 } 319 320 entry->kobj = &dev->kobj; 321 322 if (entry->attr_grp) { 323 ret = sysfs_create_group(entry->kobj, entry->attr_grp); 324 if (ret) 325 goto fail_sysfs_create_group; 326 } 327 328 /* if size is 0 assume no data buffer, so no file needed */ 329 if (!entry->size) 330 return 0; 331 332 res.start = entry->base_addr; 333 res.end = res.start + entry->size - 1; 334 res.flags = IORESOURCE_MEM; 335 336 entry->base = devm_ioremap_resource(dev, &res); 337 if (IS_ERR(entry->base)) { 338 ret = PTR_ERR(entry->base); 339 goto fail_ioremap; 340 } 341 342 sysfs_bin_attr_init(&entry->pmt_bin_attr); 343 entry->pmt_bin_attr.attr.name = ns->name; 344 entry->pmt_bin_attr.attr.mode = 0440; 345 entry->pmt_bin_attr.mmap = intel_pmt_mmap; 346 entry->pmt_bin_attr.read = intel_pmt_read; 347 entry->pmt_bin_attr.size = entry->size; 348 349 ret = sysfs_create_bin_file(&dev->kobj, &entry->pmt_bin_attr); 350 if (ret) 351 goto fail_ioremap; 352 353 if (ns->pmt_add_endpoint) { 354 ret = ns->pmt_add_endpoint(ivdev, entry); 355 if (ret) 356 goto fail_add_endpoint; 357 } 358 359 return 0; 360 361 fail_add_endpoint: 362 sysfs_remove_bin_file(entry->kobj, &entry->pmt_bin_attr); 363 fail_ioremap: 364 if (entry->attr_grp) 365 sysfs_remove_group(entry->kobj, entry->attr_grp); 366 fail_sysfs_create_group: 367 device_unregister(dev); 368 fail_dev_create: 369 xa_erase(ns->xa, entry->devid); 370 371 return ret; 372 } 373 374 int intel_pmt_dev_create(struct intel_pmt_entry *entry, struct intel_pmt_namespace *ns, 375 struct intel_vsec_device *intel_vsec_dev, int idx) 376 { 377 struct device *dev = &intel_vsec_dev->auxdev.dev; 378 struct resource *disc_res; 379 int ret; 380 381 disc_res = &intel_vsec_dev->resource[idx]; 382 383 entry->disc_table = devm_ioremap_resource(dev, disc_res); 384 if (IS_ERR(entry->disc_table)) 385 return PTR_ERR(entry->disc_table); 386 387 /* 388 * The mapped discovery resource may be smaller than disc_header (its 389 * size is the namespace's DVSEC entry_size in dwords, which can be 390 * less than 4). Cap the copy to the actual resource size to avoid 391 * reading past the mapped region; any unread dwords stay zero from 392 * the zero-initialized allocation of the containing struct. 393 */ 394 memcpy_fromio(entry->disc_header, entry->disc_table, 395 min(sizeof(entry->disc_header), resource_size(disc_res))); 396 397 if (ns->pmt_pre_decode) { 398 ret = ns->pmt_pre_decode(intel_vsec_dev, entry); 399 if (ret) 400 return ret; 401 } 402 403 ret = ns->pmt_header_decode(entry, dev); 404 if (ret) 405 return ret; 406 407 if (ns->pmt_post_decode) { 408 ret = ns->pmt_post_decode(intel_vsec_dev, entry); 409 if (ret) 410 return ret; 411 } 412 413 ret = intel_pmt_populate_entry(entry, intel_vsec_dev, idx); 414 if (ret) 415 return ret; 416 417 return intel_pmt_dev_register(entry, ns, dev); 418 } 419 EXPORT_SYMBOL_NS_GPL(intel_pmt_dev_create, "INTEL_PMT"); 420 421 void intel_pmt_dev_destroy(struct intel_pmt_entry *entry, 422 struct intel_pmt_namespace *ns) 423 { 424 struct device *dev = kobj_to_dev(entry->kobj); 425 426 if (entry->size) 427 sysfs_remove_bin_file(entry->kobj, &entry->pmt_bin_attr); 428 429 if (entry->attr_grp) 430 sysfs_remove_group(entry->kobj, entry->attr_grp); 431 432 device_unregister(dev); 433 xa_erase(ns->xa, entry->devid); 434 } 435 EXPORT_SYMBOL_NS_GPL(intel_pmt_dev_destroy, "INTEL_PMT"); 436 437 static int __init pmt_class_init(void) 438 { 439 return class_register(&intel_pmt_class); 440 } 441 442 static void __exit pmt_class_exit(void) 443 { 444 class_unregister(&intel_pmt_class); 445 } 446 447 module_init(pmt_class_init); 448 module_exit(pmt_class_exit); 449 450 MODULE_AUTHOR("Alexander Duyck <alexander.h.duyck@linux.intel.com>"); 451 MODULE_DESCRIPTION("Intel PMT Class driver"); 452 MODULE_LICENSE("GPL v2"); 453