class.c 11 KB

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