1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Intel Platform Monitoring Technology Crashlog 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/auxiliary_bus.h> 12 #include <linux/cleanup.h> 13 #include <linux/intel_vsec.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/mutex.h> 17 #include <linux/pci.h> 18 #include <linux/slab.h> 19 #include <linux/uaccess.h> 20 #include <linux/overflow.h> 21 22 #include "class.h" 23 24 /* Crashlog discovery header types */ 25 #define CRASH_TYPE_OOBMSM 1 26 27 /* Control Flags */ 28 #define CRASHLOG_FLAG_DISABLE BIT(28) 29 30 /* 31 * Bits 29 and 30 control the state of bit 31. 32 * 33 * Bit 29 will clear bit 31, if set, allowing a new crashlog to be captured. 34 * Bit 30 will immediately trigger a crashlog to be generated, setting bit 31. 35 * Bit 31 is the read-only status with a 1 indicating log is complete. 36 */ 37 #define CRASHLOG_FLAG_TRIGGER_CLEAR BIT(29) 38 #define CRASHLOG_FLAG_TRIGGER_EXECUTE BIT(30) 39 #define CRASHLOG_FLAG_TRIGGER_COMPLETE BIT(31) 40 #define CRASHLOG_FLAG_TRIGGER_MASK GENMASK(31, 28) 41 42 /* Crashlog Discovery Header */ 43 #define CONTROL_OFFSET 0x0 44 #define GUID_OFFSET 0x4 45 #define BASE_OFFSET 0x8 46 #define SIZE_OFFSET 0xC 47 #define GET_ACCESS(v) ((v) & GENMASK(3, 0)) 48 #define GET_TYPE(v) (((v) & GENMASK(7, 4)) >> 4) 49 #define GET_VERSION(v) (((v) & GENMASK(19, 16)) >> 16) 50 /* size is in bytes */ 51 #define GET_SIZE(v) ((v) * sizeof(u32)) 52 53 struct crashlog_entry { 54 /* entry must be first member of struct */ 55 struct intel_pmt_entry entry; 56 struct mutex control_mutex; 57 }; 58 59 struct pmt_crashlog_priv { 60 int num_entries; 61 struct crashlog_entry entry[]; 62 }; 63 64 /* 65 * I/O 66 */ 67 68 /* Read, modify, write the control register, setting or clearing @bit based on @set */ 69 static void pmt_crashlog_rmw(struct intel_pmt_entry *entry, u32 bit, bool set) 70 { 71 u32 reg = readl(entry->disc_table + CONTROL_OFFSET); 72 73 reg &= ~CRASHLOG_FLAG_TRIGGER_MASK; 74 75 if (set) 76 reg |= bit; 77 else 78 reg &= ~bit; 79 80 writel(reg, entry->disc_table + CONTROL_OFFSET); 81 } 82 83 /* Read the status register and see if the specified @bit is set */ 84 static bool pmt_crashlog_rc(struct intel_pmt_entry *entry, u32 bit) 85 { 86 u32 reg = readl(entry->disc_table + CONTROL_OFFSET); 87 88 return !!(reg & bit); 89 } 90 91 static bool pmt_crashlog_complete(struct intel_pmt_entry *entry) 92 { 93 /* return current value of the crashlog complete flag */ 94 return pmt_crashlog_rc(entry, CRASHLOG_FLAG_TRIGGER_COMPLETE); 95 } 96 97 static bool pmt_crashlog_disabled(struct intel_pmt_entry *entry) 98 { 99 /* return current value of the crashlog disabled flag */ 100 return pmt_crashlog_rc(entry, CRASHLOG_FLAG_DISABLE); 101 } 102 103 static bool pmt_crashlog_supported(struct intel_pmt_entry *entry) 104 { 105 u32 discovery_header = readl(entry->disc_table + CONTROL_OFFSET); 106 u32 crash_type, version; 107 108 crash_type = GET_TYPE(discovery_header); 109 version = GET_VERSION(discovery_header); 110 111 /* 112 * Currently we only recognize OOBMSM version 0 devices. 113 * We can ignore all other crashlog devices in the system. 114 */ 115 return crash_type == CRASH_TYPE_OOBMSM && version == 0; 116 } 117 118 static void pmt_crashlog_set_disable(struct intel_pmt_entry *entry, 119 bool disable) 120 { 121 pmt_crashlog_rmw(entry, CRASHLOG_FLAG_DISABLE, disable); 122 } 123 124 static void pmt_crashlog_set_clear(struct intel_pmt_entry *entry) 125 { 126 pmt_crashlog_rmw(entry, CRASHLOG_FLAG_TRIGGER_CLEAR, true); 127 } 128 129 static void pmt_crashlog_set_execute(struct intel_pmt_entry *entry) 130 { 131 pmt_crashlog_rmw(entry, CRASHLOG_FLAG_TRIGGER_EXECUTE, true); 132 } 133 134 /* 135 * sysfs 136 */ 137 static ssize_t 138 enable_show(struct device *dev, struct device_attribute *attr, char *buf) 139 { 140 struct intel_pmt_entry *entry = dev_get_drvdata(dev); 141 bool enabled = !pmt_crashlog_disabled(entry); 142 143 return sprintf(buf, "%d\n", enabled); 144 } 145 146 static ssize_t 147 enable_store(struct device *dev, struct device_attribute *attr, 148 const char *buf, size_t count) 149 { 150 struct crashlog_entry *entry; 151 bool enabled; 152 int result; 153 154 entry = dev_get_drvdata(dev); 155 156 result = kstrtobool(buf, &enabled); 157 if (result) 158 return result; 159 160 guard(mutex)(&entry->control_mutex); 161 162 pmt_crashlog_set_disable(&entry->entry, !enabled); 163 164 return count; 165 } 166 static DEVICE_ATTR_RW(enable); 167 168 static ssize_t 169 trigger_show(struct device *dev, struct device_attribute *attr, char *buf) 170 { 171 struct intel_pmt_entry *entry; 172 bool trigger; 173 174 entry = dev_get_drvdata(dev); 175 trigger = pmt_crashlog_complete(entry); 176 177 return sprintf(buf, "%d\n", trigger); 178 } 179 180 static ssize_t 181 trigger_store(struct device *dev, struct device_attribute *attr, 182 const char *buf, size_t count) 183 { 184 struct crashlog_entry *entry; 185 bool trigger; 186 int result; 187 188 entry = dev_get_drvdata(dev); 189 190 result = kstrtobool(buf, &trigger); 191 if (result) 192 return result; 193 194 guard(mutex)(&entry->control_mutex); 195 196 /* if device is currently disabled, return busy */ 197 if (pmt_crashlog_disabled(&entry->entry)) 198 return -EBUSY; 199 200 if (!trigger) { 201 pmt_crashlog_set_clear(&entry->entry); 202 return count; 203 } 204 205 /* we cannot trigger a new crash if one is still pending */ 206 if (pmt_crashlog_complete(&entry->entry)) 207 return -EEXIST; 208 209 pmt_crashlog_set_execute(&entry->entry); 210 211 return count; 212 } 213 static DEVICE_ATTR_RW(trigger); 214 215 static struct attribute *pmt_crashlog_attrs[] = { 216 &dev_attr_enable.attr, 217 &dev_attr_trigger.attr, 218 NULL 219 }; 220 221 static const struct attribute_group pmt_crashlog_group = { 222 .attrs = pmt_crashlog_attrs, 223 }; 224 225 static int pmt_crashlog_header_decode(struct intel_pmt_entry *entry, 226 struct device *dev) 227 { 228 void __iomem *disc_table = entry->disc_table; 229 struct intel_pmt_header *header = &entry->header; 230 struct crashlog_entry *crashlog; 231 232 if (!pmt_crashlog_supported(entry)) 233 return 1; 234 235 /* initialize control mutex */ 236 crashlog = container_of(entry, struct crashlog_entry, entry); 237 mutex_init(&crashlog->control_mutex); 238 239 header->access_type = GET_ACCESS(readl(disc_table)); 240 header->guid = readl(disc_table + GUID_OFFSET); 241 header->base_offset = readl(disc_table + BASE_OFFSET); 242 243 /* Size is measured in DWORDS, but accessor returns bytes */ 244 header->size = GET_SIZE(readl(disc_table + SIZE_OFFSET)); 245 246 entry->attr_grp = &pmt_crashlog_group; 247 248 return 0; 249 } 250 251 static DEFINE_XARRAY_ALLOC(crashlog_array); 252 static struct intel_pmt_namespace pmt_crashlog_ns = { 253 .name = "crashlog", 254 .xa = &crashlog_array, 255 .pmt_header_decode = pmt_crashlog_header_decode, 256 }; 257 258 /* 259 * initialization 260 */ 261 static void pmt_crashlog_remove(struct auxiliary_device *auxdev) 262 { 263 struct pmt_crashlog_priv *priv = auxiliary_get_drvdata(auxdev); 264 int i; 265 266 for (i = 0; i < priv->num_entries; i++) { 267 struct crashlog_entry *crashlog = &priv->entry[i]; 268 269 intel_pmt_dev_destroy(&crashlog->entry, &pmt_crashlog_ns); 270 mutex_destroy(&crashlog->control_mutex); 271 } 272 } 273 274 static int pmt_crashlog_probe(struct auxiliary_device *auxdev, 275 const struct auxiliary_device_id *id) 276 { 277 struct intel_vsec_device *intel_vsec_dev = auxdev_to_ivdev(auxdev); 278 struct pmt_crashlog_priv *priv; 279 size_t size; 280 int i, ret; 281 282 size = struct_size(priv, entry, intel_vsec_dev->num_resources); 283 priv = devm_kzalloc(&auxdev->dev, size, GFP_KERNEL); 284 if (!priv) 285 return -ENOMEM; 286 287 auxiliary_set_drvdata(auxdev, priv); 288 289 for (i = 0; i < intel_vsec_dev->num_resources; i++) { 290 struct intel_pmt_entry *entry = &priv->entry[priv->num_entries].entry; 291 292 ret = intel_pmt_dev_create(entry, &pmt_crashlog_ns, intel_vsec_dev, i); 293 if (ret < 0) 294 goto abort_probe; 295 if (ret) 296 continue; 297 298 priv->num_entries++; 299 } 300 301 return 0; 302 abort_probe: 303 pmt_crashlog_remove(auxdev); 304 return ret; 305 } 306 307 static const struct auxiliary_device_id pmt_crashlog_id_table[] = { 308 { .name = "intel_vsec.crashlog" }, 309 {} 310 }; 311 MODULE_DEVICE_TABLE(auxiliary, pmt_crashlog_id_table); 312 313 static struct auxiliary_driver pmt_crashlog_aux_driver = { 314 .id_table = pmt_crashlog_id_table, 315 .remove = pmt_crashlog_remove, 316 .probe = pmt_crashlog_probe, 317 }; 318 319 static int __init pmt_crashlog_init(void) 320 { 321 return auxiliary_driver_register(&pmt_crashlog_aux_driver); 322 } 323 324 static void __exit pmt_crashlog_exit(void) 325 { 326 auxiliary_driver_unregister(&pmt_crashlog_aux_driver); 327 xa_destroy(&crashlog_array); 328 } 329 330 module_init(pmt_crashlog_init); 331 module_exit(pmt_crashlog_exit); 332 333 MODULE_AUTHOR("Alexander Duyck <alexander.h.duyck@linux.intel.com>"); 334 MODULE_DESCRIPTION("Intel PMT Crashlog driver"); 335 MODULE_LICENSE("GPL v2"); 336 MODULE_IMPORT_NS("INTEL_PMT"); 337