1e184b1e5SDavid E. Box // SPDX-License-Identifier: GPL-2.0 2e184b1e5SDavid E. Box /* 3e184b1e5SDavid E. Box * Intel Platform Monitory Technology Telemetry driver 4e184b1e5SDavid E. Box * 5e184b1e5SDavid E. Box * Copyright (c) 2020, Intel Corporation. 6e184b1e5SDavid E. Box * All Rights Reserved. 7e184b1e5SDavid E. Box * 8e184b1e5SDavid E. Box * Author: "David E. Box" <david.e.box@linux.intel.com> 9e184b1e5SDavid E. Box */ 10e184b1e5SDavid E. Box 11a3c8f906SDavid E. Box #include <linux/auxiliary_bus.h> 12e184b1e5SDavid E. Box #include <linux/kernel.h> 13e184b1e5SDavid E. Box #include <linux/module.h> 14e184b1e5SDavid E. Box #include <linux/pci.h> 15e184b1e5SDavid E. Box #include <linux/slab.h> 16e184b1e5SDavid E. Box #include <linux/uaccess.h> 17e184b1e5SDavid E. Box #include <linux/overflow.h> 18e184b1e5SDavid E. Box 19a3c8f906SDavid E. Box #include "../vsec.h" 20e184b1e5SDavid E. Box #include "class.h" 21e184b1e5SDavid E. Box 22e184b1e5SDavid E. Box #define TELEM_SIZE_OFFSET 0x0 23e184b1e5SDavid E. Box #define TELEM_GUID_OFFSET 0x4 24e184b1e5SDavid E. Box #define TELEM_BASE_OFFSET 0x8 25e184b1e5SDavid E. Box #define TELEM_ACCESS(v) ((v) & GENMASK(3, 0)) 26ba7e421eSDavid E. Box #define TELEM_TYPE(v) (((v) & GENMASK(7, 4)) >> 4) 27e184b1e5SDavid E. Box /* size is in bytes */ 28e184b1e5SDavid E. Box #define TELEM_SIZE(v) (((v) & GENMASK(27, 12)) >> 10) 29e184b1e5SDavid E. Box 30e184b1e5SDavid E. Box /* Used by client hardware to identify a fixed telemetry entry*/ 31e184b1e5SDavid E. Box #define TELEM_CLIENT_FIXED_BLOCK_GUID 0x10000000 32e184b1e5SDavid E. Box 33*416eeb2eSDavid E. Box #define NUM_BYTES_QWORD(v) ((v) << 3) 34*416eeb2eSDavid E. Box #define SAMPLE_ID_OFFSET(v) ((v) << 3) 35*416eeb2eSDavid E. Box 36*416eeb2eSDavid E. Box #define NUM_BYTES_DWORD(v) ((v) << 2) 37*416eeb2eSDavid E. Box #define SAMPLE_ID_OFFSET32(v) ((v) << 2) 38*416eeb2eSDavid E. Box 39*416eeb2eSDavid E. Box /* Protects access to the xarray of telemetry endpoint handles */ 40*416eeb2eSDavid E. Box static DEFINE_MUTEX(ep_lock); 41*416eeb2eSDavid E. Box 42ba7e421eSDavid E. Box enum telem_type { 43ba7e421eSDavid E. Box TELEM_TYPE_PUNIT = 0, 44ba7e421eSDavid E. Box TELEM_TYPE_CRASHLOG, 45ba7e421eSDavid E. Box TELEM_TYPE_PUNIT_FIXED, 46ba7e421eSDavid E. Box }; 47ba7e421eSDavid E. Box 48e184b1e5SDavid E. Box struct pmt_telem_priv { 49e184b1e5SDavid E. Box int num_entries; 50e184b1e5SDavid E. Box struct intel_pmt_entry entry[]; 51e184b1e5SDavid E. Box }; 52e184b1e5SDavid E. Box 53e184b1e5SDavid E. Box static bool pmt_telem_region_overlaps(struct intel_pmt_entry *entry, 54e184b1e5SDavid E. Box struct device *dev) 55e184b1e5SDavid E. Box { 56e184b1e5SDavid E. Box u32 guid = readl(entry->disc_table + TELEM_GUID_OFFSET); 57e184b1e5SDavid E. Box 58ba7e421eSDavid E. Box if (intel_pmt_is_early_client_hw(dev)) { 59ba7e421eSDavid E. Box u32 type = TELEM_TYPE(readl(entry->disc_table)); 60e184b1e5SDavid E. Box 61ba7e421eSDavid E. Box if ((type == TELEM_TYPE_PUNIT_FIXED) || 62ba7e421eSDavid E. Box (guid == TELEM_CLIENT_FIXED_BLOCK_GUID)) 63ba7e421eSDavid E. Box return true; 64ba7e421eSDavid E. Box } 65ba7e421eSDavid E. Box 66ba7e421eSDavid E. Box return false; 67e184b1e5SDavid E. Box } 68e184b1e5SDavid E. Box 69e184b1e5SDavid E. Box static int pmt_telem_header_decode(struct intel_pmt_entry *entry, 70e184b1e5SDavid E. Box struct device *dev) 71e184b1e5SDavid E. Box { 72e184b1e5SDavid E. Box void __iomem *disc_table = entry->disc_table; 734d1b7efeSDavid E. Box struct intel_pmt_header *header = &entry->header; 74e184b1e5SDavid E. Box 75e184b1e5SDavid E. Box if (pmt_telem_region_overlaps(entry, dev)) 76e184b1e5SDavid E. Box return 1; 77e184b1e5SDavid E. Box 78e184b1e5SDavid E. Box header->access_type = TELEM_ACCESS(readl(disc_table)); 79e184b1e5SDavid E. Box header->guid = readl(disc_table + TELEM_GUID_OFFSET); 80e184b1e5SDavid E. Box header->base_offset = readl(disc_table + TELEM_BASE_OFFSET); 81e184b1e5SDavid E. Box 82e184b1e5SDavid E. Box /* Size is measured in DWORDS, but accessor returns bytes */ 83e184b1e5SDavid E. Box header->size = TELEM_SIZE(readl(disc_table)); 84e184b1e5SDavid E. Box 85ef195e8aSDavid E. Box /* 86ef195e8aSDavid E. Box * Some devices may expose non-functioning entries that are 87ef195e8aSDavid E. Box * reserved for future use. They have zero size. Do not fail 88ef195e8aSDavid E. Box * probe for these. Just ignore them. 89ef195e8aSDavid E. Box */ 903f581602SDavid E. Box if (header->size == 0 || header->access_type == 0xF) 91ef195e8aSDavid E. Box return 1; 92ef195e8aSDavid E. Box 93e184b1e5SDavid E. Box return 0; 94e184b1e5SDavid E. Box } 95e184b1e5SDavid E. Box 96*416eeb2eSDavid E. Box static int pmt_telem_add_endpoint(struct intel_pmt_entry *entry, 97*416eeb2eSDavid E. Box struct pci_dev *pdev) 98*416eeb2eSDavid E. Box { 99*416eeb2eSDavid E. Box struct telem_endpoint *ep; 100*416eeb2eSDavid E. Box 101*416eeb2eSDavid E. Box /* Endpoint lifetimes are managed by kref, not devres */ 102*416eeb2eSDavid E. Box entry->ep = kzalloc(sizeof(*(entry->ep)), GFP_KERNEL); 103*416eeb2eSDavid E. Box if (!entry->ep) 104*416eeb2eSDavid E. Box return -ENOMEM; 105*416eeb2eSDavid E. Box 106*416eeb2eSDavid E. Box ep = entry->ep; 107*416eeb2eSDavid E. Box ep->pcidev = pdev; 108*416eeb2eSDavid E. Box ep->header.access_type = entry->header.access_type; 109*416eeb2eSDavid E. Box ep->header.guid = entry->header.guid; 110*416eeb2eSDavid E. Box ep->header.base_offset = entry->header.base_offset; 111*416eeb2eSDavid E. Box ep->header.size = entry->header.size; 112*416eeb2eSDavid E. Box ep->base = entry->base; 113*416eeb2eSDavid E. Box ep->present = true; 114*416eeb2eSDavid E. Box 115*416eeb2eSDavid E. Box kref_init(&ep->kref); 116*416eeb2eSDavid E. Box 117*416eeb2eSDavid E. Box return 0; 118*416eeb2eSDavid E. Box } 119*416eeb2eSDavid E. Box 120e184b1e5SDavid E. Box static DEFINE_XARRAY_ALLOC(telem_array); 121e184b1e5SDavid E. Box static struct intel_pmt_namespace pmt_telem_ns = { 122e184b1e5SDavid E. Box .name = "telem", 123e184b1e5SDavid E. Box .xa = &telem_array, 124e184b1e5SDavid E. Box .pmt_header_decode = pmt_telem_header_decode, 125*416eeb2eSDavid E. Box .pmt_add_endpoint = pmt_telem_add_endpoint, 126e184b1e5SDavid E. Box }; 127e184b1e5SDavid E. Box 128*416eeb2eSDavid E. Box /* Called when all users unregister and the device is removed */ 129*416eeb2eSDavid E. Box static void pmt_telem_ep_release(struct kref *kref) 130*416eeb2eSDavid E. Box { 131*416eeb2eSDavid E. Box struct telem_endpoint *ep; 132*416eeb2eSDavid E. Box 133*416eeb2eSDavid E. Box ep = container_of(kref, struct telem_endpoint, kref); 134*416eeb2eSDavid E. Box kfree(ep); 135*416eeb2eSDavid E. Box } 136*416eeb2eSDavid E. Box 137*416eeb2eSDavid E. Box unsigned long pmt_telem_get_next_endpoint(unsigned long start) 138*416eeb2eSDavid E. Box { 139*416eeb2eSDavid E. Box struct intel_pmt_entry *entry; 140*416eeb2eSDavid E. Box unsigned long found_idx; 141*416eeb2eSDavid E. Box 142*416eeb2eSDavid E. Box mutex_lock(&ep_lock); 143*416eeb2eSDavid E. Box xa_for_each_start(&telem_array, found_idx, entry, start) { 144*416eeb2eSDavid E. Box /* 145*416eeb2eSDavid E. Box * Return first found index after start. 146*416eeb2eSDavid E. Box * 0 is not valid id. 147*416eeb2eSDavid E. Box */ 148*416eeb2eSDavid E. Box if (found_idx > start) 149*416eeb2eSDavid E. Box break; 150*416eeb2eSDavid E. Box } 151*416eeb2eSDavid E. Box mutex_unlock(&ep_lock); 152*416eeb2eSDavid E. Box 153*416eeb2eSDavid E. Box return found_idx == start ? 0 : found_idx; 154*416eeb2eSDavid E. Box } 155*416eeb2eSDavid E. Box EXPORT_SYMBOL_NS_GPL(pmt_telem_get_next_endpoint, INTEL_PMT_TELEMETRY); 156*416eeb2eSDavid E. Box 157*416eeb2eSDavid E. Box struct telem_endpoint *pmt_telem_register_endpoint(int devid) 158*416eeb2eSDavid E. Box { 159*416eeb2eSDavid E. Box struct intel_pmt_entry *entry; 160*416eeb2eSDavid E. Box unsigned long index = devid; 161*416eeb2eSDavid E. Box 162*416eeb2eSDavid E. Box mutex_lock(&ep_lock); 163*416eeb2eSDavid E. Box entry = xa_find(&telem_array, &index, index, XA_PRESENT); 164*416eeb2eSDavid E. Box if (!entry) { 165*416eeb2eSDavid E. Box mutex_unlock(&ep_lock); 166*416eeb2eSDavid E. Box return ERR_PTR(-ENXIO); 167*416eeb2eSDavid E. Box } 168*416eeb2eSDavid E. Box 169*416eeb2eSDavid E. Box kref_get(&entry->ep->kref); 170*416eeb2eSDavid E. Box mutex_unlock(&ep_lock); 171*416eeb2eSDavid E. Box 172*416eeb2eSDavid E. Box return entry->ep; 173*416eeb2eSDavid E. Box } 174*416eeb2eSDavid E. Box EXPORT_SYMBOL_NS_GPL(pmt_telem_register_endpoint, INTEL_PMT_TELEMETRY); 175*416eeb2eSDavid E. Box 176*416eeb2eSDavid E. Box void pmt_telem_unregister_endpoint(struct telem_endpoint *ep) 177*416eeb2eSDavid E. Box { 178*416eeb2eSDavid E. Box kref_put(&ep->kref, pmt_telem_ep_release); 179*416eeb2eSDavid E. Box } 180*416eeb2eSDavid E. Box EXPORT_SYMBOL_NS_GPL(pmt_telem_unregister_endpoint, INTEL_PMT_TELEMETRY); 181*416eeb2eSDavid E. Box 182*416eeb2eSDavid E. Box int pmt_telem_get_endpoint_info(int devid, struct telem_endpoint_info *info) 183*416eeb2eSDavid E. Box { 184*416eeb2eSDavid E. Box struct intel_pmt_entry *entry; 185*416eeb2eSDavid E. Box unsigned long index = devid; 186*416eeb2eSDavid E. Box int err = 0; 187*416eeb2eSDavid E. Box 188*416eeb2eSDavid E. Box if (!info) 189*416eeb2eSDavid E. Box return -EINVAL; 190*416eeb2eSDavid E. Box 191*416eeb2eSDavid E. Box mutex_lock(&ep_lock); 192*416eeb2eSDavid E. Box entry = xa_find(&telem_array, &index, index, XA_PRESENT); 193*416eeb2eSDavid E. Box if (!entry) { 194*416eeb2eSDavid E. Box err = -ENXIO; 195*416eeb2eSDavid E. Box goto unlock; 196*416eeb2eSDavid E. Box } 197*416eeb2eSDavid E. Box 198*416eeb2eSDavid E. Box info->pdev = entry->ep->pcidev; 199*416eeb2eSDavid E. Box info->header = entry->ep->header; 200*416eeb2eSDavid E. Box 201*416eeb2eSDavid E. Box unlock: 202*416eeb2eSDavid E. Box mutex_unlock(&ep_lock); 203*416eeb2eSDavid E. Box return err; 204*416eeb2eSDavid E. Box 205*416eeb2eSDavid E. Box } 206*416eeb2eSDavid E. Box EXPORT_SYMBOL_NS_GPL(pmt_telem_get_endpoint_info, INTEL_PMT_TELEMETRY); 207*416eeb2eSDavid E. Box 208*416eeb2eSDavid E. Box int pmt_telem_read(struct telem_endpoint *ep, u32 id, u64 *data, u32 count) 209*416eeb2eSDavid E. Box { 210*416eeb2eSDavid E. Box u32 offset, size; 211*416eeb2eSDavid E. Box 212*416eeb2eSDavid E. Box if (!ep->present) 213*416eeb2eSDavid E. Box return -ENODEV; 214*416eeb2eSDavid E. Box 215*416eeb2eSDavid E. Box offset = SAMPLE_ID_OFFSET(id); 216*416eeb2eSDavid E. Box size = ep->header.size; 217*416eeb2eSDavid E. Box 218*416eeb2eSDavid E. Box if (offset + NUM_BYTES_QWORD(count) > size) 219*416eeb2eSDavid E. Box return -EINVAL; 220*416eeb2eSDavid E. Box 221*416eeb2eSDavid E. Box memcpy_fromio(data, ep->base + offset, NUM_BYTES_QWORD(count)); 222*416eeb2eSDavid E. Box 223*416eeb2eSDavid E. Box return ep->present ? 0 : -EPIPE; 224*416eeb2eSDavid E. Box } 225*416eeb2eSDavid E. Box EXPORT_SYMBOL_NS_GPL(pmt_telem_read, INTEL_PMT_TELEMETRY); 226*416eeb2eSDavid E. Box 227*416eeb2eSDavid E. Box int pmt_telem_read32(struct telem_endpoint *ep, u32 id, u32 *data, u32 count) 228*416eeb2eSDavid E. Box { 229*416eeb2eSDavid E. Box u32 offset, size; 230*416eeb2eSDavid E. Box 231*416eeb2eSDavid E. Box if (!ep->present) 232*416eeb2eSDavid E. Box return -ENODEV; 233*416eeb2eSDavid E. Box 234*416eeb2eSDavid E. Box offset = SAMPLE_ID_OFFSET32(id); 235*416eeb2eSDavid E. Box size = ep->header.size; 236*416eeb2eSDavid E. Box 237*416eeb2eSDavid E. Box if (offset + NUM_BYTES_DWORD(count) > size) 238*416eeb2eSDavid E. Box return -EINVAL; 239*416eeb2eSDavid E. Box 240*416eeb2eSDavid E. Box memcpy_fromio(data, ep->base + offset, NUM_BYTES_DWORD(count)); 241*416eeb2eSDavid E. Box 242*416eeb2eSDavid E. Box return ep->present ? 0 : -EPIPE; 243*416eeb2eSDavid E. Box } 244*416eeb2eSDavid E. Box EXPORT_SYMBOL_NS_GPL(pmt_telem_read32, INTEL_PMT_TELEMETRY); 245*416eeb2eSDavid E. Box 246*416eeb2eSDavid E. Box struct telem_endpoint * 247*416eeb2eSDavid E. Box pmt_telem_find_and_register_endpoint(struct pci_dev *pcidev, u32 guid, u16 pos) 248*416eeb2eSDavid E. Box { 249*416eeb2eSDavid E. Box int devid = 0; 250*416eeb2eSDavid E. Box int inst = 0; 251*416eeb2eSDavid E. Box int err = 0; 252*416eeb2eSDavid E. Box 253*416eeb2eSDavid E. Box while ((devid = pmt_telem_get_next_endpoint(devid))) { 254*416eeb2eSDavid E. Box struct telem_endpoint_info ep_info; 255*416eeb2eSDavid E. Box 256*416eeb2eSDavid E. Box err = pmt_telem_get_endpoint_info(devid, &ep_info); 257*416eeb2eSDavid E. Box if (err) 258*416eeb2eSDavid E. Box return ERR_PTR(err); 259*416eeb2eSDavid E. Box 260*416eeb2eSDavid E. Box if (ep_info.header.guid == guid && ep_info.pdev == pcidev) { 261*416eeb2eSDavid E. Box if (inst == pos) 262*416eeb2eSDavid E. Box return pmt_telem_register_endpoint(devid); 263*416eeb2eSDavid E. Box ++inst; 264*416eeb2eSDavid E. Box } 265*416eeb2eSDavid E. Box } 266*416eeb2eSDavid E. Box 267*416eeb2eSDavid E. Box return ERR_PTR(-ENXIO); 268*416eeb2eSDavid E. Box } 269*416eeb2eSDavid E. Box EXPORT_SYMBOL_NS_GPL(pmt_telem_find_and_register_endpoint, INTEL_PMT_TELEMETRY); 270*416eeb2eSDavid E. Box 271a3c8f906SDavid E. Box static void pmt_telem_remove(struct auxiliary_device *auxdev) 272e184b1e5SDavid E. Box { 273a3c8f906SDavid E. Box struct pmt_telem_priv *priv = auxiliary_get_drvdata(auxdev); 274e184b1e5SDavid E. Box int i; 275e184b1e5SDavid E. Box 276*416eeb2eSDavid E. Box mutex_lock(&ep_lock); 277*416eeb2eSDavid E. Box for (i = 0; i < priv->num_entries; i++) { 278*416eeb2eSDavid E. Box struct intel_pmt_entry *entry = &priv->entry[i]; 279*416eeb2eSDavid E. Box 280*416eeb2eSDavid E. Box kref_put(&entry->ep->kref, pmt_telem_ep_release); 281*416eeb2eSDavid E. Box intel_pmt_dev_destroy(entry, &pmt_telem_ns); 282e184b1e5SDavid E. Box } 283*416eeb2eSDavid E. Box mutex_unlock(&ep_lock); 284*416eeb2eSDavid E. Box }; 285e184b1e5SDavid E. Box 286a3c8f906SDavid E. Box static int pmt_telem_probe(struct auxiliary_device *auxdev, const struct auxiliary_device_id *id) 287e184b1e5SDavid E. Box { 288a3c8f906SDavid E. Box struct intel_vsec_device *intel_vsec_dev = auxdev_to_ivdev(auxdev); 289e184b1e5SDavid E. Box struct pmt_telem_priv *priv; 290e184b1e5SDavid E. Box size_t size; 291e184b1e5SDavid E. Box int i, ret; 292e184b1e5SDavid E. Box 293a3c8f906SDavid E. Box size = struct_size(priv, entry, intel_vsec_dev->num_resources); 294a3c8f906SDavid E. Box priv = devm_kzalloc(&auxdev->dev, size, GFP_KERNEL); 295e184b1e5SDavid E. Box if (!priv) 296e184b1e5SDavid E. Box return -ENOMEM; 297e184b1e5SDavid E. Box 298a3c8f906SDavid E. Box auxiliary_set_drvdata(auxdev, priv); 299e184b1e5SDavid E. Box 300a3c8f906SDavid E. Box for (i = 0; i < intel_vsec_dev->num_resources; i++) { 3012cdfa0c2SPrarit Bhargava struct intel_pmt_entry *entry = &priv->entry[priv->num_entries]; 302e184b1e5SDavid E. Box 303*416eeb2eSDavid E. Box mutex_lock(&ep_lock); 304a3c8f906SDavid E. Box ret = intel_pmt_dev_create(entry, &pmt_telem_ns, intel_vsec_dev, i); 305*416eeb2eSDavid E. Box mutex_unlock(&ep_lock); 306e184b1e5SDavid E. Box if (ret < 0) 307e184b1e5SDavid E. Box goto abort_probe; 308e184b1e5SDavid E. Box if (ret) 309e184b1e5SDavid E. Box continue; 310e184b1e5SDavid E. Box 311e184b1e5SDavid E. Box priv->num_entries++; 312e184b1e5SDavid E. Box } 313e184b1e5SDavid E. Box 314e184b1e5SDavid E. Box return 0; 315e184b1e5SDavid E. Box abort_probe: 316a3c8f906SDavid E. Box pmt_telem_remove(auxdev); 317e184b1e5SDavid E. Box return ret; 318e184b1e5SDavid E. Box } 319e184b1e5SDavid E. Box 320a3c8f906SDavid E. Box static const struct auxiliary_device_id pmt_telem_id_table[] = { 321a3c8f906SDavid E. Box { .name = "intel_vsec.telemetry" }, 322a3c8f906SDavid E. Box {} 323a3c8f906SDavid E. Box }; 324a3c8f906SDavid E. Box MODULE_DEVICE_TABLE(auxiliary, pmt_telem_id_table); 325a3c8f906SDavid E. Box 326a3c8f906SDavid E. Box static struct auxiliary_driver pmt_telem_aux_driver = { 327a3c8f906SDavid E. Box .id_table = pmt_telem_id_table, 328e184b1e5SDavid E. Box .remove = pmt_telem_remove, 329e184b1e5SDavid E. Box .probe = pmt_telem_probe, 330e184b1e5SDavid E. Box }; 331e184b1e5SDavid E. Box 332e184b1e5SDavid E. Box static int __init pmt_telem_init(void) 333e184b1e5SDavid E. Box { 334a3c8f906SDavid E. Box return auxiliary_driver_register(&pmt_telem_aux_driver); 335e184b1e5SDavid E. Box } 336e184b1e5SDavid E. Box module_init(pmt_telem_init); 337e184b1e5SDavid E. Box 338e184b1e5SDavid E. Box static void __exit pmt_telem_exit(void) 339e184b1e5SDavid E. Box { 340a3c8f906SDavid E. Box auxiliary_driver_unregister(&pmt_telem_aux_driver); 341e184b1e5SDavid E. Box xa_destroy(&telem_array); 342e184b1e5SDavid E. Box } 343e184b1e5SDavid E. Box module_exit(pmt_telem_exit); 344e184b1e5SDavid E. Box 345e184b1e5SDavid E. Box MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>"); 346e184b1e5SDavid E. Box MODULE_DESCRIPTION("Intel PMT Telemetry driver"); 347e184b1e5SDavid E. Box MODULE_LICENSE("GPL v2"); 348d9080843SDavid E. Box MODULE_IMPORT_NS(INTEL_PMT); 349