1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2026 Intel Corporation 4 */ 5 6 #include <kunit/static_stub.h> 7 #include <kunit/visibility.h> 8 9 #include "abi/xe_log_abi.h" 10 11 #include "xe_device.h" 12 #include "xe_log.h" 13 #include "xe_pci_types.h" 14 #include "xe_printk.h" 15 16 static void log_emit_cper(struct pci_dev *pdev, int cper_sev, enum xe_sigid sigid, 17 u32 component, u32 location, const void *data, size_t len, 18 struct va_format *vaf) 19 { 20 KUNIT_STATIC_STUB_REDIRECT(log_emit_cper, pdev, cper_sev, sigid, 21 component, location, data, len, vaf); 22 /* TODO */ 23 } 24 25 static const char *log_unknown_component_prefix(u32 component) 26 { 27 u32 class = FIELD_GET(XE_LOG_COMPONENT_CLASS_MASK, component); 28 u32 type = FIELD_GET(XE_LOG_COMPONENT_TYPE_MASK, component); 29 30 WARN(IS_ENABLED(CONFIG_DRM_XE_DEBUG), "LOG: unrecognized component %u.%u\n", class, type); 31 switch (class) { 32 #define MAKE_XE_LOG_COMPONENT_CLASS_PREFIX(_CLASS) \ 33 case XE_LOG_COMPONENT_CLASS_##_CLASS: return #_CLASS "? "; 34 MAKE_XE_LOG_COMPONENT_CLASS_PREFIX(SYSTEM) 35 MAKE_XE_LOG_COMPONENT_CLASS_PREFIX(DRIVER) 36 MAKE_XE_LOG_COMPONENT_CLASS_PREFIX(FEATURE) 37 MAKE_XE_LOG_COMPONENT_CLASS_PREFIX(FIRMWARE) 38 MAKE_XE_LOG_COMPONENT_CLASS_PREFIX(HARDWARE) 39 #undef MAKE_XE_LOG_COMPONENT_CLASS_PREFIX 40 } 41 return "COMP? "; 42 } 43 44 static const char *log_component_prefix(u32 component) 45 { 46 switch (component) { 47 #define MAKE_XE_LOG_COMPONENT_CASE_PREFIX(_CLASS, _ID, _TAG, _SIG, _NAME) \ 48 case XE_LOG_COMPONENT_##_TAG: return #_TAG ": "; 49 DEFINE_XE_LOG_COMPONENTS(MAKE_XE_LOG_COMPONENT_CASE_PREFIX) 50 #undef MAKE_XE_LOG_COMPONENT_CASE_PREFIX 51 } 52 53 return component ? log_unknown_component_prefix(component) : ""; 54 } 55 56 static bool allowed_tile_id(struct xe_device *xe, u8 tile_id) 57 { 58 return tile_id < 1 + xe->desc->max_remote_tiles; 59 } 60 61 static bool allowed_gt_id(struct xe_device *xe, u8 gt_id) 62 { 63 return gt_id < (1 + xe->desc->max_remote_tiles) * xe->desc->max_gt_per_tile; 64 } 65 66 static u8 gt_id_to_tile_id(struct xe_device *xe, u8 gt_id) 67 { 68 return gt_id / xe->desc->max_gt_per_tile; 69 } 70 71 static const char *location_suffix(bool valid) 72 { 73 return valid ? ":" : "?"; 74 } 75 76 static const char *log_location_prefix(struct pci_dev *pdev, u32 location, char *buf, size_t size) 77 { 78 u32 type = FIELD_GET(XE_LOG_LOCATION_TYPE_MASK, location); 79 u32 id = FIELD_GET(XE_LOG_LOCATION_ID_MASK, location); 80 81 if (!location || type == XE_LOG_LOCATION_TYPE_DEVICE) { 82 if (id) 83 goto unrecognized; 84 strscpy(buf, "", size); 85 } else if (type == XE_LOG_LOCATION_TYPE_TILE) { 86 struct xe_device *xe = xe_any_to_xe(pdev); 87 bool valid = xe ? allowed_tile_id(xe, id) : false; 88 const char *pad = location_suffix(valid); 89 90 pci_WARN(pdev, !valid && IS_ENABLED(CONFIG_DRM_XE_DEBUG), 91 "LOG: invalid tile identifier: %u\n", id); 92 snprintf(buf, size, "Tile%u%s ", id, pad); 93 } else if (type == XE_LOG_LOCATION_TYPE_GT) { 94 struct xe_device *xe = xe_any_to_xe(pdev); 95 bool valid = xe ? allowed_gt_id(xe, id) : false; 96 const char *pad = location_suffix(valid); 97 u8 tile_id = xe ? gt_id_to_tile_id(xe, id) : 0; 98 99 pci_WARN(pdev, !valid && IS_ENABLED(CONFIG_DRM_XE_DEBUG), 100 "LOG: invalid GT identifier: %u\n", id); 101 snprintf(buf, size, "Tile%u%s GT%u%s ", tile_id, pad, id, pad); 102 } else { 103 goto unrecognized; 104 } 105 106 return buf; 107 108 unrecognized: 109 pci_WARN(pdev, IS_ENABLED(CONFIG_DRM_XE_DEBUG), 110 "LOG: unrecognized location %u.%u\n", type, id); 111 snprintf(buf, size, "LOC%u.%u? ", type, id); 112 return buf; 113 } 114 115 static bool is_hw_sigid(enum xe_sigid sigid) 116 { 117 return (int)sigid >= INTEL_SIGID_GPU_XE_HARDWARE_START; 118 } 119 120 static bool is_sev_error(int cper_sev) 121 { 122 return cper_sev != CPER_SEV_INFORMATIONAL; 123 } 124 125 static const char *log_hwe_prefix(int cper_sev, enum xe_sigid sigid) 126 { 127 return is_sev_error(cper_sev) && is_hw_sigid(sigid) ? HW_ERR : ""; 128 } 129 130 static const char *log_sev_prefix(int cper_sev) 131 { 132 switch (cper_sev) { 133 case CPER_SEV_FATAL: 134 return "FATAL "; 135 case CPER_SEV_RECOVERABLE: 136 return ""; 137 case CPER_SEV_CORRECTED: 138 return "CORRECTED "; 139 case CPER_SEV_INFORMATIONAL: 140 return ""; 141 default: 142 WARN(IS_ENABLED(CONFIG_DRM_XE_DEBUG), "LOG: unknown severity %d\n", cper_sev); 143 return ""; 144 } 145 } 146 147 #define __LOG_DRM_PRINTK_FMT(fmt, args...) "[drm] " fmt, ##args 148 #define __LOG_DRM_PRINTK_ERR_FMT(fmt, args...) __LOG_DRM_PRINTK_FMT("*ERROR* " fmt, args) 149 150 static void log_dmesg_vprintk(struct pci_dev *pdev, int cper_sev, struct va_format *vaf) 151 { 152 KUNIT_STATIC_STUB_REDIRECT(log_dmesg_vprintk, pdev, cper_sev, vaf); 153 154 if (cper_sev == CPER_SEV_INFORMATIONAL) 155 pci_info(pdev, __LOG_DRM_PRINTK_FMT("%pV", vaf)); 156 else 157 pci_err(pdev, __LOG_DRM_PRINTK_ERR_FMT("%pV", vaf)); 158 } 159 160 static void log_dmesg_printf(struct pci_dev *pdev, int cper_sev, const char *fmt, ...) 161 { 162 struct va_format vaf; 163 va_list args; 164 165 va_start(args, fmt); 166 vaf.fmt = fmt; 167 vaf.va = &args; 168 169 log_dmesg_vprintk(pdev, cper_sev, &vaf); 170 171 va_end(args); 172 } 173 174 static void log_emit_dmesg(struct pci_dev *pdev, int cper_sev, enum xe_sigid sigid, 175 u32 component, u32 location, const void *data, size_t len, 176 struct va_format *vaf) 177 { 178 char buf[32]; 179 const char *loc_prefix = log_location_prefix(pdev, location, buf, sizeof(buf)); 180 const char *comp_prefix = log_component_prefix(component); 181 const char *hwe_prefix = log_hwe_prefix(cper_sev, sigid); 182 const char *sev_prefix = log_sev_prefix(cper_sev); 183 184 if (IS_ERR(data)) 185 log_dmesg_printf(pdev, cper_sev, "SIGID=%u %s(%pe) %s%s%s%pV", 186 sigid, sev_prefix, data, hwe_prefix, 187 loc_prefix, comp_prefix, vaf); 188 else if (data && len) 189 log_dmesg_printf(pdev, cper_sev, "SIGID=%u %s(%*phN) %s%s%s%pV", 190 sigid, sev_prefix, (int)len, data, hwe_prefix, 191 loc_prefix, comp_prefix, vaf); 192 else 193 log_dmesg_printf(pdev, cper_sev, "SIGID=%u %s%s%s%s%pV", 194 sigid, sev_prefix, hwe_prefix, 195 loc_prefix, comp_prefix, vaf); 196 } 197 198 /** 199 * __xe_log_emit() - Emit a structured SIGID log entry 200 * @pdev: the &pci_dev device 201 * @cper_sev: CPER severity (CPER_SEV_FATAL, CPER_SEV_RECOVERABLE, ...) 202 * @sigid: signature identifier, see &enum xe_sigid 203 * @component: component identifier 204 * @location: location details of the @component 205 * @data: pointer to the additional details, or ERR_PTR, or NULL if not applicable 206 * @len: length of the @data in bytes, or 0 if not applicable 207 * @fmt: printf-style format string 208 * @...: format arguments 209 * 210 * Emits a dmesg line that includes a single stable, machine-matchable token 211 * ``SIGID=<n>`` followed by the optional severity token (like ``FATAL``) and, 212 * when @data pointer is set, either the error printed with %pe or a packed hex 213 * dump of the @data binary blob. The dmesg line will also include printf-style 214 * text message. 215 * 216 * Note that the full dmesg line, with the free text message, is only a debugging 217 * aid, not an interface! Only the ``SIGID=<n>`` token is stable there. 218 * The durable machine record is the CPER carrying the same SIGID. 219 * 220 * Note: generation of the CPER record is a planned follow-up. 221 * 222 * Examples:: 223 * 224 * <3> xe 0000:03:00.0: [drm] *ERROR* SIGID=104 FATAL (-EPROTO) Invalid GuC reply 225 * <3> xe 0000:03:00.0: [drm] *ERROR* SIGID=106 (-ETIMEDOUT) Engine 'rcs0' hung 226 * <6> xe 0000:03:00.0: [drm] SIGID=103 In survivability mode 227 */ 228 void __xe_log_emit(struct pci_dev *pdev, int cper_sev, enum xe_sigid sigid, 229 u32 component, u32 location, const void *data, size_t len, 230 const char *fmt, ...) 231 { 232 struct va_format vaf; 233 va_list args; 234 235 va_start(args, fmt); 236 vaf.fmt = fmt; 237 vaf.va = &args; 238 239 log_emit_dmesg(pdev, cper_sev, sigid, component, location, data, len, &vaf); 240 log_emit_cper(pdev, cper_sev, sigid, component, location, data, len, &vaf); 241 242 va_end(args); 243 } 244 245 #if IS_BUILTIN(CONFIG_DRM_XE_KUNIT_TEST) 246 #include "tests/xe_log_kunit.c" 247 #endif 248