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_device.h" 14 #include "xe_gt.h" 15 #include "xe_heci_gsc.h" 16 #include "xe_mmio.h" 17 #include "xe_pcode_api.h" 18 #include "xe_vsec.h" 19 20 #define MAX_SCRATCH_MMIO 8 21 22 /** 23 * DOC: Xe Boot Survivability 24 * 25 * Boot Survivability is a software based workflow for recovering a system in a failed boot state 26 * Here system recoverability is concerned with recovering the firmware responsible for boot. 27 * 28 * This is implemented by loading the driver with bare minimum (no drm card) to allow the firmware 29 * to be flashed through mei and collect telemetry. The driver's probe flow is modified 30 * such that it enters survivability mode when pcode initialization is incomplete and boot status 31 * denotes a failure. The driver then populates the survivability_mode PCI sysfs indicating 32 * survivability mode and provides additional information required for debug 33 * 34 * KMD exposes below admin-only readable sysfs in survivability mode 35 * 36 * device/survivability_mode: The presence of this file indicates that the card is in survivability 37 * mode. Also, provides additional information on why the driver entered 38 * survivability mode. 39 * 40 * Capability Information - Provides boot status 41 * Postcode Information - Provides information about the failure 42 * Overflow Information - Provides history of previous failures 43 * Auxiliary Information - Certain failures may have information in 44 * addition to postcode information 45 */ 46 47 static u32 aux_history_offset(u32 reg_value) 48 { 49 return REG_FIELD_GET(AUXINFO_HISTORY_OFFSET, reg_value); 50 } 51 52 static void set_survivability_info(struct xe_mmio *mmio, struct xe_survivability_info *info, 53 int id, char *name) 54 { 55 strscpy(info[id].name, name, sizeof(info[id].name)); 56 info[id].reg = PCODE_SCRATCH(id).raw; 57 info[id].value = xe_mmio_read32(mmio, PCODE_SCRATCH(id)); 58 } 59 60 static void populate_survivability_info(struct xe_device *xe) 61 { 62 struct xe_survivability *survivability = &xe->survivability; 63 struct xe_survivability_info *info = survivability->info; 64 struct xe_mmio *mmio; 65 u32 id = 0, reg_value; 66 char name[NAME_MAX]; 67 int index; 68 69 mmio = xe_root_tile_mmio(xe); 70 set_survivability_info(mmio, info, id, "Capability Info"); 71 reg_value = info[id].value; 72 73 if (reg_value & HISTORY_TRACKING) { 74 id++; 75 set_survivability_info(mmio, info, id, "Postcode Info"); 76 77 if (reg_value & OVERFLOW_SUPPORT) { 78 id = REG_FIELD_GET(OVERFLOW_REG_OFFSET, reg_value); 79 set_survivability_info(mmio, info, id, "Overflow Info"); 80 } 81 } 82 83 if (reg_value & AUXINFO_SUPPORT) { 84 id = REG_FIELD_GET(AUXINFO_REG_OFFSET, reg_value); 85 86 for (index = 0; id && reg_value; index++, reg_value = info[id].value, 87 id = aux_history_offset(reg_value)) { 88 snprintf(name, NAME_MAX, "Auxiliary Info %d", index); 89 set_survivability_info(mmio, info, id, name); 90 } 91 } 92 } 93 94 static void log_survivability_info(struct pci_dev *pdev) 95 { 96 struct xe_device *xe = pdev_to_xe_device(pdev); 97 struct xe_survivability *survivability = &xe->survivability; 98 struct xe_survivability_info *info = survivability->info; 99 int id; 100 101 dev_info(&pdev->dev, "Survivability Boot Status : Critical Failure (%d)\n", 102 survivability->boot_status); 103 for (id = 0; id < MAX_SCRATCH_MMIO; id++) { 104 if (info[id].reg) 105 dev_info(&pdev->dev, "%s: 0x%x - 0x%x\n", info[id].name, 106 info[id].reg, info[id].value); 107 } 108 } 109 110 static ssize_t survivability_mode_show(struct device *dev, 111 struct device_attribute *attr, char *buff) 112 { 113 struct pci_dev *pdev = to_pci_dev(dev); 114 struct xe_device *xe = pdev_to_xe_device(pdev); 115 struct xe_survivability *survivability = &xe->survivability; 116 struct xe_survivability_info *info = survivability->info; 117 int index = 0, count = 0; 118 119 for (index = 0; index < MAX_SCRATCH_MMIO; index++) { 120 if (info[index].reg) 121 count += sysfs_emit_at(buff, count, "%s: 0x%x - 0x%x\n", info[index].name, 122 info[index].reg, info[index].value); 123 } 124 125 return count; 126 } 127 128 static DEVICE_ATTR_ADMIN_RO(survivability_mode); 129 130 static void enable_survivability_mode(struct pci_dev *pdev) 131 { 132 struct device *dev = &pdev->dev; 133 struct xe_device *xe = pdev_to_xe_device(pdev); 134 struct xe_survivability *survivability = &xe->survivability; 135 int ret = 0; 136 137 /* set survivability mode */ 138 survivability->mode = true; 139 dev_info(dev, "In Survivability Mode\n"); 140 141 /* create survivability mode sysfs */ 142 ret = sysfs_create_file(&dev->kobj, &dev_attr_survivability_mode.attr); 143 if (ret) { 144 dev_warn(dev, "Failed to create survivability sysfs files\n"); 145 return; 146 } 147 148 xe_heci_gsc_init(xe); 149 150 xe_vsec_init(xe); 151 } 152 153 /** 154 * xe_survivability_mode_enabled - check if survivability mode is enabled 155 * @xe: xe device instance 156 * 157 * Returns true if in survivability mode, false otherwise 158 */ 159 bool xe_survivability_mode_enabled(struct xe_device *xe) 160 { 161 struct xe_survivability *survivability = &xe->survivability; 162 163 return survivability->mode; 164 } 165 166 /** 167 * xe_survivability_mode_required - checks if survivability mode is required 168 * @xe: xe device instance 169 * 170 * This function reads the boot status from Pcode 171 * 172 * Return: true if boot status indicates failure, false otherwise 173 */ 174 bool xe_survivability_mode_required(struct xe_device *xe) 175 { 176 struct xe_survivability *survivability = &xe->survivability; 177 struct xe_mmio *mmio = xe_root_tile_mmio(xe); 178 u32 data; 179 180 if (!IS_DGFX(xe) || xe->info.platform < XE_BATTLEMAGE || IS_SRIOV_VF(xe)) 181 return false; 182 183 data = xe_mmio_read32(mmio, PCODE_SCRATCH(0)); 184 survivability->boot_status = REG_FIELD_GET(BOOT_STATUS, data); 185 186 return (survivability->boot_status == NON_CRITICAL_FAILURE || 187 survivability->boot_status == CRITICAL_FAILURE); 188 } 189 190 /** 191 * xe_survivability_mode_remove - remove survivability mode 192 * @xe: xe device instance 193 * 194 * clean up sysfs entries of survivability mode 195 */ 196 void xe_survivability_mode_remove(struct xe_device *xe) 197 { 198 struct xe_survivability *survivability = &xe->survivability; 199 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 200 struct device *dev = &pdev->dev; 201 202 sysfs_remove_file(&dev->kobj, &dev_attr_survivability_mode.attr); 203 xe_heci_gsc_fini(xe); 204 kfree(survivability->info); 205 pci_set_drvdata(pdev, NULL); 206 } 207 208 /** 209 * xe_survivability_mode_init - Initialize the survivability mode 210 * @xe: xe device instance 211 * 212 * Initializes survivability information and enables survivability mode 213 */ 214 void xe_survivability_mode_init(struct xe_device *xe) 215 { 216 struct xe_survivability *survivability = &xe->survivability; 217 struct xe_survivability_info *info; 218 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 219 220 survivability->size = MAX_SCRATCH_MMIO; 221 222 info = kcalloc(survivability->size, sizeof(*info), GFP_KERNEL); 223 if (!info) 224 return; 225 226 survivability->info = info; 227 228 populate_survivability_info(xe); 229 230 /* Only log debug information and exit if it is a critical failure */ 231 if (survivability->boot_status == CRITICAL_FAILURE) { 232 log_survivability_info(pdev); 233 kfree(survivability->info); 234 return; 235 } 236 237 enable_survivability_mode(pdev); 238 } 239