1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2025 Intel Corporation 4 */ 5 6 #include "xe_survivability_mode.h" 7 #include "xe_survivability_mode_types.h" 8 9 #include <linux/kobject.h> 10 #include <linux/pci.h> 11 #include <linux/sysfs.h> 12 13 #include "xe_configfs.h" 14 #include "xe_device.h" 15 #include "xe_heci_gsc.h" 16 #include "xe_i2c.h" 17 #include "xe_log.h" 18 #include "xe_mmio.h" 19 #include "xe_nvm.h" 20 #include "xe_pcode_api.h" 21 #include "xe_printk.h" 22 #include "xe_vsec.h" 23 24 /** 25 * DOC: Survivability Mode 26 * 27 * Survivability Mode is a software based workflow for recovering a system in a failed boot state 28 * Here system recoverability is concerned with recovering the firmware responsible for boot. 29 * 30 * Boot Survivability 31 * =================== 32 * 33 * Boot Survivability is implemented by loading the driver with bare minimum (no drm card) to allow 34 * the firmware to be flashed through mei driver and collect telemetry. The driver's probe flow is 35 * modified such that it enters survivability mode when pcode initialization is incomplete and boot 36 * status denotes a failure. 37 * 38 * Survivability mode can also be entered manually using the survivability mode attribute available 39 * through configfs which is beneficial in several usecases. It can be used to address scenarios 40 * where pcode does not detect failure or for validation purposes. It can also be used in 41 * In-Field-Repair (IFR) to repair a single card without impacting the other cards in a node. 42 * 43 * Use below command enable survivability mode manually:: 44 * 45 * # echo 1 > /sys/kernel/config/xe/0000:03:00.0/survivability_mode 46 * 47 * It is the responsibility of the user to clear the mode once firmware flash is complete. 48 * 49 * Refer :ref:`xe_configfs` for more details on how to use configfs 50 * 51 * Survivability mode is indicated by the below admin-only readable sysfs entry. It 52 * provides information about the type of survivability mode (Boot/Runtime). 53 * 54 * .. code-block:: shell 55 * 56 * # cat /sys/bus/pci/devices/<device>/survivability_mode 57 * Boot 58 * 59 * Any additional debug information if present will be visible under the directory 60 * ``survivability_info``:: 61 * 62 * /sys/bus/pci/devices/<device>/survivability_info/ 63 * ├── aux_info0 64 * ├── aux_info1 65 * ├── aux_info2 66 * ├── aux_info3 67 * ├── aux_info4 68 * ├── capability_info 69 * ├── fdo_mode 70 * ├── postcode_trace 71 * └── postcode_trace_overflow 72 * 73 * This directory has the following attributes 74 * 75 * - ``capability_info`` : Indicates Boot status and support for additional information 76 * 77 * - ``postcode_trace``, ``postcode_trace_overflow`` : Each postcode is a 8bit value and 78 * represents a boot failure event. When a new failure event is logged by PCODE the 79 * existing postcodes are shifted left. These entries provide a history of 8 postcodes. 80 * 81 * - ``aux_info<n>`` : Some failures have additional debug information 82 * 83 * - ``fdo_mode`` : To allow recovery in scenarios where MEI itself fails, a new SPI Flash 84 * Descriptor Override (FDO) mode is added in v2 survivability breadcrumbs. This mode is enabled 85 * by PCODE and provides the ability to directly update the firmware via SPI Driver without 86 * any dependency on MEI. Xe KMD initializes the nvm aux driver if FDO mode is enabled. 87 * 88 * Runtime Survivability 89 * ===================== 90 * 91 * Certain runtime firmware errors can cause the device to enter a wedged state 92 * (:ref:`xe-device-wedging`) requiring a firmware flash to restore normal operation. 93 * Runtime Survivability Mode indicates that a firmware flash is necessary to recover the device and 94 * is indicated by the presence of survivability mode sysfs. 95 * Survivability mode sysfs provides information about the type of survivability mode. 96 * 97 * .. code-block:: shell 98 * 99 * # cat /sys/bus/pci/devices/<device>/survivability_mode 100 * Runtime 101 * 102 * On some CSC firmware errors, PCODE sets FDO mode and the only recovery possible is through 103 * firmware flash using SPI driver. Userspace can check if FDO mode is set by checking the below 104 * sysfs entry. 105 * 106 * .. code-block:: shell 107 * 108 * # cat /sys/bus/pci/devices/<device>/survivability_info/fdo_mode 109 * enabled 110 * 111 * When such errors occur, userspace is notified with the drm device wedged uevent and runtime 112 * survivability mode. User can then initiate a firmware flash using userspace tools like fwupd 113 * to restore device to normal operation. 114 */ 115 116 static const char * const reg_map[] = { 117 [CAPABILITY_INFO] = "Capability Info", 118 [POSTCODE_TRACE] = "Postcode trace", 119 [POSTCODE_TRACE_OVERFLOW] = "Postcode trace overflow", 120 [AUX_INFO0] = "Auxiliary Info 0", 121 [AUX_INFO1] = "Auxiliary Info 1", 122 [AUX_INFO2] = "Auxiliary Info 2", 123 [AUX_INFO3] = "Auxiliary Info 3", 124 [AUX_INFO4] = "Auxiliary Info 4", 125 }; 126 127 #define FDO_INFO (MAX_SCRATCH_REG + 1) 128 129 struct xe_survivability_attribute { 130 struct device_attribute attr; 131 u8 index; 132 }; 133 134 static struct 135 xe_survivability_attribute *dev_attr_to_survivability_attr(struct device_attribute *attr) 136 { 137 return container_of(attr, struct xe_survivability_attribute, attr); 138 } 139 140 static void set_survivability_info(struct xe_mmio *mmio, u32 *info, int id) 141 { 142 info[id] = xe_mmio_read32(mmio, PCODE_SCRATCH(id)); 143 } 144 145 static void populate_survivability_info(struct xe_device *xe) 146 { 147 struct xe_survivability *survivability = &xe->survivability; 148 u32 *info = survivability->info; 149 struct xe_mmio *mmio; 150 u32 id = 0, reg_value; 151 152 mmio = xe_root_tile_mmio(xe); 153 set_survivability_info(mmio, info, CAPABILITY_INFO); 154 reg_value = info[CAPABILITY_INFO]; 155 156 survivability->version = REG_FIELD_GET(BREADCRUMB_VERSION, reg_value); 157 /* FDO mode is exposed only from version 2 */ 158 if (survivability->version >= 2) 159 survivability->fdo_mode = REG_FIELD_GET(FDO_MODE, reg_value); 160 161 if (reg_value & HISTORY_TRACKING) { 162 set_survivability_info(mmio, info, POSTCODE_TRACE); 163 164 if (reg_value & OVERFLOW_SUPPORT) 165 set_survivability_info(mmio, info, POSTCODE_TRACE_OVERFLOW); 166 } 167 168 /* Traverse the linked list of aux info registers */ 169 if (reg_value & AUXINFO_SUPPORT) { 170 for (id = REG_FIELD_GET(AUXINFO_REG_OFFSET, reg_value); 171 id >= AUX_INFO0 && id < MAX_SCRATCH_REG; 172 id = REG_FIELD_GET(AUXINFO_HISTORY_OFFSET, info[id])) 173 set_survivability_info(mmio, info, id); 174 } 175 } 176 177 static const char *boot_status_str(u8 boot_status) 178 { 179 switch (boot_status) { 180 case CRITICAL_FAILURE: 181 return "Critical Failure"; 182 case NON_CRITICAL_FAILURE: 183 return "Non Critical Failure"; 184 default: 185 return "Other"; 186 } 187 } 188 189 static void log_survivability_info(struct xe_device *xe) 190 { 191 struct xe_survivability *survivability = &xe->survivability; 192 u32 *info = survivability->info; 193 int id; 194 195 xe_log_info(xe, SURVIVABILITY, "Boot Status: %#x (%s)\n", 196 survivability->boot_status, 197 boot_status_str(survivability->boot_status)); 198 199 for (id = 0; id < MAX_SCRATCH_REG; id++) { 200 if (!info[id]) 201 continue; 202 xe_log_info(xe, SURVIVABILITY, "%s: %#x\n", reg_map[id], info[id]); 203 } 204 } 205 206 static int check_boot_failure(struct xe_device *xe) 207 { 208 struct xe_survivability *survivability = &xe->survivability; 209 210 return survivability->boot_status == NON_CRITICAL_FAILURE || 211 survivability->boot_status == CRITICAL_FAILURE; 212 } 213 214 static ssize_t survivability_mode_show(struct device *dev, 215 struct device_attribute *attr, char *buff) 216 { 217 struct pci_dev *pdev = to_pci_dev(dev); 218 struct xe_device *xe = pdev_to_xe_device(pdev); 219 struct xe_survivability *survivability = &xe->survivability; 220 221 return sysfs_emit(buff, "%s\n", survivability->type ? "Runtime" : "Boot"); 222 } 223 224 static DEVICE_ATTR_ADMIN_RO(survivability_mode); 225 226 static ssize_t survivability_info_show(struct device *dev, 227 struct device_attribute *attr, char *buff) 228 { 229 struct xe_survivability_attribute *sa = dev_attr_to_survivability_attr(attr); 230 struct pci_dev *pdev = to_pci_dev(dev); 231 struct xe_device *xe = pdev_to_xe_device(pdev); 232 struct xe_survivability *survivability = &xe->survivability; 233 u32 *info = survivability->info; 234 235 if (sa->index == FDO_INFO) 236 return sysfs_emit(buff, "%s\n", str_enabled_disabled(survivability->fdo_mode)); 237 238 return sysfs_emit(buff, "0x%x\n", info[sa->index]); 239 } 240 241 #define SURVIVABILITY_ATTR_RO(name, _index) \ 242 struct xe_survivability_attribute attr_##name = { \ 243 .attr = __ATTR(name, 0400, survivability_info_show, NULL), \ 244 .index = _index, \ 245 } 246 247 static SURVIVABILITY_ATTR_RO(capability_info, CAPABILITY_INFO); 248 static SURVIVABILITY_ATTR_RO(postcode_trace, POSTCODE_TRACE); 249 static SURVIVABILITY_ATTR_RO(postcode_trace_overflow, POSTCODE_TRACE_OVERFLOW); 250 static SURVIVABILITY_ATTR_RO(aux_info0, AUX_INFO0); 251 static SURVIVABILITY_ATTR_RO(aux_info1, AUX_INFO1); 252 static SURVIVABILITY_ATTR_RO(aux_info2, AUX_INFO2); 253 static SURVIVABILITY_ATTR_RO(aux_info3, AUX_INFO3); 254 static SURVIVABILITY_ATTR_RO(aux_info4, AUX_INFO4); 255 static SURVIVABILITY_ATTR_RO(fdo_mode, FDO_INFO); 256 257 static void xe_survivability_mode_fini(void *arg) 258 { 259 struct xe_device *xe = arg; 260 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 261 struct device *dev = &pdev->dev; 262 263 device_remove_file(dev, &dev_attr_survivability_mode); 264 } 265 266 static umode_t survivability_info_attrs_visible(struct kobject *kobj, struct attribute *attr, 267 int idx) 268 { 269 struct xe_device *xe = kdev_to_xe_device(kobj_to_dev(kobj)); 270 struct xe_survivability *survivability = &xe->survivability; 271 u32 *info = survivability->info; 272 273 /* 274 * Last index in survivability_info_attrs is fdo mode and is applicable only in 275 * version 2 of survivability mode 276 */ 277 if (idx == MAX_SCRATCH_REG && survivability->version >= 2) 278 return 0400; 279 280 if (idx < MAX_SCRATCH_REG && info[idx]) 281 return 0400; 282 283 return 0; 284 } 285 286 /* Attributes are ordered according to enum scratch_reg */ 287 static struct attribute *survivability_info_attrs[] = { 288 &attr_capability_info.attr.attr, 289 &attr_postcode_trace.attr.attr, 290 &attr_postcode_trace_overflow.attr.attr, 291 &attr_aux_info0.attr.attr, 292 &attr_aux_info1.attr.attr, 293 &attr_aux_info2.attr.attr, 294 &attr_aux_info3.attr.attr, 295 &attr_aux_info4.attr.attr, 296 &attr_fdo_mode.attr.attr, 297 NULL, 298 }; 299 300 static const struct attribute_group survivability_info_group = { 301 .name = "survivability_info", 302 .attrs = survivability_info_attrs, 303 .is_visible = survivability_info_attrs_visible, 304 }; 305 306 static int create_survivability_sysfs(struct pci_dev *pdev) 307 { 308 /* Survivability info is required if not enabled via configfs */ 309 bool needs_info = !xe_configfs_get_survivability_mode(pdev); 310 struct xe_device *xe = pdev_to_xe_device(pdev); 311 struct device *dev = &pdev->dev; 312 int ret; 313 314 ret = device_create_file(dev, &dev_attr_survivability_mode); 315 if (ret) 316 goto failed; 317 318 ret = devm_add_action_or_reset(xe->drm.dev, 319 xe_survivability_mode_fini, xe); 320 if (ret) 321 goto failed; 322 323 if (needs_info) { 324 ret = devm_device_add_group(dev, &survivability_info_group); 325 if (ret) 326 goto failed; 327 } 328 329 return 0; 330 331 failed: 332 xe_err(xe, "Failed to create survivability sysfs files: %pe\n", ERR_PTR(ret)); 333 /* no sysfs, dump Survivability info to dmesg instead */ 334 if (needs_info) 335 log_survivability_info(xe); 336 return ret; 337 } 338 339 static int enable_boot_survivability_mode(struct pci_dev *pdev) 340 { 341 struct xe_device *xe = pdev_to_xe_device(pdev); 342 struct xe_survivability *survivability = &xe->survivability; 343 int ret; 344 345 ret = create_survivability_sysfs(pdev); 346 if (ret) 347 goto failed; 348 349 /* Make sure xe_heci_gsc_init() and xe_i2c_probe() are aware of survivability */ 350 survivability->mode = true; 351 352 xe_heci_gsc_init(xe); 353 354 xe_vsec_init(xe); 355 356 if (survivability->fdo_mode) { 357 ret = xe_nvm_init(xe); 358 if (ret) 359 goto failed; 360 } 361 362 ret = xe_i2c_probe(xe); 363 if (ret) 364 goto failed; 365 366 if (check_boot_failure(xe)) 367 xe_log_err_fatal(pdev, SURVIVABILITY, 0, "Boot Mode enabled!\n"); 368 else 369 xe_log_info(pdev, SURVIVABILITY, "Boot Mode enabled!\n"); 370 371 return 0; 372 373 failed: 374 xe_log_err_fatal(pdev, SURVIVABILITY, ret, "Failed to enable Boot Mode!\n"); 375 survivability->mode = false; 376 return ret; 377 } 378 379 /** 380 * xe_survivability_mode_is_boot_enabled- check if boot survivability mode is enabled 381 * @xe: xe device instance 382 * 383 * Returns true if in boot survivability mode of type, else false 384 */ 385 bool xe_survivability_mode_is_boot_enabled(struct xe_device *xe) 386 { 387 struct xe_survivability *survivability = &xe->survivability; 388 389 return survivability->mode && survivability->type == XE_SURVIVABILITY_TYPE_BOOT; 390 } 391 392 /** 393 * xe_survivability_mode_is_requested - check if it's possible to enable survivability 394 * mode that was requested by firmware or userspace 395 * @xe: xe device instance 396 * 397 * This function reads configfs and boot status from Pcode. 398 * 399 * Return: true if platform support is available and boot status indicates 400 * failure or if survivability mode is requested, false otherwise. 401 */ 402 bool xe_survivability_mode_is_requested(struct xe_device *xe) 403 { 404 struct xe_survivability *survivability = &xe->survivability; 405 struct xe_mmio *mmio = xe_root_tile_mmio(xe); 406 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 407 u32 data; 408 bool survivability_mode; 409 410 if (!IS_DGFX(xe) || IS_SRIOV_VF(xe) || xe->info.platform < XE_BATTLEMAGE) 411 return false; 412 413 survivability_mode = xe_configfs_get_survivability_mode(pdev); 414 /* Enable survivability mode if set via configfs */ 415 if (survivability_mode) 416 return true; 417 418 data = xe_mmio_read32(mmio, PCODE_SCRATCH(0)); 419 survivability->boot_status = REG_FIELD_GET(BOOT_STATUS, data); 420 421 return check_boot_failure(xe); 422 } 423 424 /** 425 * xe_survivability_mode_runtime_enable - Initialize and enable runtime survivability mode 426 * @xe: xe device instance 427 * 428 * Initialize survivability information and enable runtime survivability mode. 429 * Runtime survivability mode is enabled when certain errors cause the device to be 430 * in non-recoverable state. The device is declared wedged with the appropriate 431 * recovery method and survivability mode sysfs exposed to userspace 432 */ 433 void xe_survivability_mode_runtime_enable(struct xe_device *xe) 434 { 435 struct xe_survivability *survivability = &xe->survivability; 436 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 437 438 if (!IS_DGFX(xe) || IS_SRIOV_VF(xe) || xe->info.platform < XE_BATTLEMAGE) { 439 xe_log_err(xe, SURVIVABILITY, -EOPNOTSUPP, "Runtime Mode not supported!\n"); 440 return; 441 } 442 443 populate_survivability_info(xe); 444 create_survivability_sysfs(pdev); 445 446 survivability->type = XE_SURVIVABILITY_TYPE_RUNTIME; 447 xe_log_err(xe, SURVIVABILITY, 0, "Runtime Mode enabled!\n"); 448 449 xe_device_set_wedged_method(xe, DRM_WEDGE_RECOVERY_VENDOR); 450 xe_device_declare_wedged(xe); 451 452 xe_log_err(xe, SURVIVABILITY, 0, "Firmware flash required!\n"); 453 xe_info(xe, "Please refer to the userspace documentation for more details how to flash the firmware on %s!\n", 454 xe->info.platform_name); 455 } 456 457 /** 458 * xe_survivability_mode_boot_enable - Initialize and enable boot survivability mode 459 * @xe: xe device instance 460 * 461 * Initialize survivability information and enable boot survivability mode 462 * 463 * Return: 0 if boot survivability mode is enabled or not requested, negative error 464 * code otherwise. 465 */ 466 int xe_survivability_mode_boot_enable(struct xe_device *xe) 467 { 468 struct xe_survivability *survivability = &xe->survivability; 469 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 470 471 if (!xe_survivability_mode_is_requested(xe)) 472 return 0; 473 474 populate_survivability_info(xe); 475 476 /* 477 * v2 supports survivability mode for critical errors 478 */ 479 if (survivability->version < 2 && survivability->boot_status == CRITICAL_FAILURE) { 480 log_survivability_info(xe); 481 return -ENXIO; 482 } 483 484 survivability->type = XE_SURVIVABILITY_TYPE_BOOT; 485 486 return enable_boot_survivability_mode(pdev); 487 } 488