xref: /linux/drivers/gpu/drm/xe/xe_ras.c (revision 7d1f84f1b94e79cb95d974ab3ec54cb3a4d64f5b)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2026 Intel Corporation
4  */
5 
6 #include "xe_configfs.h"
7 #include "xe_debugfs.h"
8 #include "xe_device.h"
9 #include "xe_drm_ras.h"
10 #include "xe_log.h"
11 #include "xe_pm.h"
12 #include "xe_printk.h"
13 #include "xe_ras.h"
14 #include "xe_survivability_mode.h"
15 #include "xe_sysctrl.h"
16 #include "xe_sysctrl_event_types.h"
17 #include "xe_sysctrl_mailbox.h"
18 #include "xe_sysctrl_mailbox_types.h"
19 
20 #define CORE_COMPUTE_UNCORR_TYPE	GENMASK(26, 25)
21 /*
22  * Uncorrectable error type for core compute errors.
23  * 0 - Correctable Error
24  * 1 - Local Uncorrectable Error
25  * 2 - Global Uncorrectable Error
26  * 3 - Informational Error
27  */
28 #define  GLOBAL_UNCORR_ERROR		2
29 
30 /* Severity of detected errors  */
31 enum xe_ras_severity {
32 	XE_RAS_SEV_NOT_SUPPORTED = 0,
33 	XE_RAS_SEV_CORRECTABLE,
34 	XE_RAS_SEV_UNCORRECTABLE,
35 	XE_RAS_SEV_INFORMATIONAL,
36 	XE_RAS_SEV_MAX
37 };
38 
39 /* Major IP blocks/components where errors can originate */
40 enum xe_ras_component {
41 	XE_RAS_COMP_NOT_SUPPORTED = 0,
42 	XE_RAS_COMP_DEVICE_MEMORY,
43 	XE_RAS_COMP_CORE_COMPUTE,
44 	XE_RAS_COMP_RESERVED,
45 	XE_RAS_COMP_PCIE,
46 	XE_RAS_COMP_FABRIC,
47 	XE_RAS_COMP_SOC_INTERNAL,
48 	XE_RAS_COMP_MAX
49 };
50 
51 #define CHECK_COMPONENT(RAS_COMP, LOG_COMP) \
52 	static_assert(MAKE_XE_LOG_COMPONENT(HARDWARE, (RAS_COMP)) == (LOG_COMP))
53 	/* make sure components definitions maintain stable relation */
54 	CHECK_COMPONENT(XE_RAS_COMP_DEVICE_MEMORY, XE_LOG_COMPONENT_DEVICE_MEMORY);
55 	CHECK_COMPONENT(XE_RAS_COMP_CORE_COMPUTE, XE_LOG_COMPONENT_CORE_COMPUTE);
56 	CHECK_COMPONENT(XE_RAS_COMP_PCIE, XE_LOG_COMPONENT_PCIE);
57 	CHECK_COMPONENT(XE_RAS_COMP_FABRIC, XE_LOG_COMPONENT_FABRIC);
58 	CHECK_COMPONENT(XE_RAS_COMP_SOC_INTERNAL, XE_LOG_COMPONENT_SOC_INTERNAL);
59 #undef CHECK_COMPONENT
60 
61 /* RAS response status codes */
62 enum xe_ras_response_status {
63 	XE_RAS_STATUS_SUCCESS = 0,
64 	XE_RAS_STATUS_INVALID_PARAM,
65 	XE_RAS_STATUS_OP_NOT_SUPPORTED,
66 	XE_RAS_STATUS_TIMEOUT,
67 	XE_RAS_STATUS_HARDWARE_FAILURE,
68 	XE_RAS_STATUS_INSUFFICIENT_RESOURCES,
69 	XE_RAS_STATUS_MAX
70 };
71 
72 /* GPU health values */
73 enum xe_ras_health {
74 	XE_RAS_HEALTH_OK = 0,
75 	XE_RAS_HEALTH_WARNING,
76 	XE_RAS_HEALTH_CRITICAL,
77 	XE_RAS_HEALTH_MAX
78 };
79 
80 static const char *const xe_ras_severities[] = {
81 	[XE_RAS_SEV_NOT_SUPPORTED]		= "Not Supported",
82 	[XE_RAS_SEV_CORRECTABLE]		= "Correctable Error",
83 	[XE_RAS_SEV_UNCORRECTABLE]		= "Uncorrectable Error",
84 	[XE_RAS_SEV_INFORMATIONAL]		= "Informational Error",
85 };
86 static_assert(ARRAY_SIZE(xe_ras_severities) == XE_RAS_SEV_MAX);
87 
88 static const char *const xe_ras_components[] = {
89 	[XE_RAS_COMP_NOT_SUPPORTED]		= "Not Supported",
90 	[XE_RAS_COMP_DEVICE_MEMORY]		= "Device Memory",
91 	[XE_RAS_COMP_CORE_COMPUTE]		= "Core Compute",
92 	[XE_RAS_COMP_RESERVED]			= "Reserved",
93 	[XE_RAS_COMP_PCIE]			= "PCIe",
94 	[XE_RAS_COMP_FABRIC]			= "Fabric",
95 	[XE_RAS_COMP_SOC_INTERNAL]		= "SoC Internal",
96 };
97 static_assert(ARRAY_SIZE(xe_ras_components) == XE_RAS_COMP_MAX);
98 
99 static const char * const gpu_health_states[] = {
100 	[XE_RAS_HEALTH_OK]		= "ok",
101 	[XE_RAS_HEALTH_WARNING]		= "warning",
102 	[XE_RAS_HEALTH_CRITICAL]	= "critical",
103 };
104 static_assert(ARRAY_SIZE(gpu_health_states) == XE_RAS_HEALTH_MAX);
105 
106 static int get_counter(struct xe_device *xe, struct xe_ras_error_class *counter, u32 *value);
107 
108 static u8 drm_to_xe_ras_severity(u8 severity)
109 {
110 	switch (severity) {
111 	case DRM_XE_RAS_ERR_SEV_CORRECTABLE:
112 		return XE_RAS_SEV_CORRECTABLE;
113 	case DRM_XE_RAS_ERR_SEV_UNCORRECTABLE:
114 		return XE_RAS_SEV_UNCORRECTABLE;
115 	default:
116 		return XE_RAS_SEV_NOT_SUPPORTED;
117 	}
118 }
119 
120 static u8 xe_to_drm_ras_severity(u8 severity)
121 {
122 	switch (severity) {
123 	case XE_RAS_SEV_CORRECTABLE:
124 		return DRM_XE_RAS_ERR_SEV_CORRECTABLE;
125 	case XE_RAS_SEV_UNCORRECTABLE:
126 		return DRM_XE_RAS_ERR_SEV_UNCORRECTABLE;
127 	default:
128 		return DRM_XE_RAS_ERR_SEV_MAX;
129 	}
130 }
131 
132 static u8 drm_to_xe_ras_component(u8 component)
133 {
134 	switch (component) {
135 	case DRM_XE_RAS_ERR_COMP_CORE_COMPUTE:
136 		return XE_RAS_COMP_CORE_COMPUTE;
137 	case DRM_XE_RAS_ERR_COMP_SOC_INTERNAL:
138 		return XE_RAS_COMP_SOC_INTERNAL;
139 	case DRM_XE_RAS_ERR_COMP_DEVICE_MEMORY:
140 		return XE_RAS_COMP_DEVICE_MEMORY;
141 	case DRM_XE_RAS_ERR_COMP_PCIE:
142 		return XE_RAS_COMP_PCIE;
143 	case DRM_XE_RAS_ERR_COMP_FABRIC:
144 		return XE_RAS_COMP_FABRIC;
145 	default:
146 		return XE_RAS_COMP_NOT_SUPPORTED;
147 	}
148 }
149 
150 static u8 xe_to_drm_ras_component(u8 component)
151 {
152 	switch (component) {
153 	case XE_RAS_COMP_DEVICE_MEMORY:
154 		return DRM_XE_RAS_ERR_COMP_DEVICE_MEMORY;
155 	case XE_RAS_COMP_CORE_COMPUTE:
156 		return DRM_XE_RAS_ERR_COMP_CORE_COMPUTE;
157 	case XE_RAS_COMP_PCIE:
158 		return DRM_XE_RAS_ERR_COMP_PCIE;
159 	case XE_RAS_COMP_FABRIC:
160 		return DRM_XE_RAS_ERR_COMP_FABRIC;
161 	case XE_RAS_COMP_SOC_INTERNAL:
162 		return DRM_XE_RAS_ERR_COMP_SOC_INTERNAL;
163 	default:
164 		return DRM_XE_RAS_ERR_COMP_MAX;
165 	}
166 }
167 
168 static int ras_status_to_errno(u32 status)
169 {
170 	switch (status) {
171 	case XE_RAS_STATUS_SUCCESS:
172 		return 0;
173 	case XE_RAS_STATUS_INVALID_PARAM:
174 		return -EINVAL;
175 	case XE_RAS_STATUS_OP_NOT_SUPPORTED:
176 		return -EOPNOTSUPP;
177 	case XE_RAS_STATUS_TIMEOUT:
178 		return -ETIMEDOUT;
179 	case XE_RAS_STATUS_HARDWARE_FAILURE:
180 		return -EIO;
181 	case XE_RAS_STATUS_INSUFFICIENT_RESOURCES:
182 		return -ENOSPC;
183 	default:
184 		return -EPROTO;
185 	}
186 }
187 
188 static inline const char *sev_to_str(u8 severity)
189 {
190 	if (severity >= XE_RAS_SEV_MAX)
191 		severity = XE_RAS_SEV_NOT_SUPPORTED;
192 
193 	return xe_ras_severities[severity];
194 }
195 
196 static inline const char *comp_to_str(u8 component)
197 {
198 	if (component >= XE_RAS_COMP_MAX)
199 		component = XE_RAS_COMP_NOT_SUPPORTED;
200 
201 	return xe_ras_components[component];
202 }
203 
204 static bool ras_counter_is_valid(struct xe_device *xe, struct xe_ras_error_class *counter)
205 {
206 	u8 severity = counter->common.severity;
207 	u8 component = counter->common.component;
208 
209 	if (!in_range(severity, XE_RAS_SEV_NOT_SUPPORTED + 1, XE_RAS_SEV_MAX - 1)) {
210 		xe_err(xe, "sysctrl: unexpected severity %u\n", severity);
211 		return false;
212 	}
213 
214 	if (!in_range(component, XE_RAS_COMP_NOT_SUPPORTED + 1, XE_RAS_COMP_MAX - 1)) {
215 		xe_err(xe, "sysctrl: unexpected component %u\n", component);
216 		return false;
217 	}
218 
219 	return true;
220 }
221 
222 static struct pci_dev *find_usp_dev(struct pci_dev *pdev)
223 {
224 	struct pci_dev *vsp;
225 
226 	/*
227 	 * Device Hierarchy:
228 	 *
229 	 * Upstream Switch Port (USP) --> Virtual Switch Port (VSP) --> SGunit (GPU endpoint)
230 	 */
231 	vsp = pci_upstream_bridge(pdev);
232 	if (!vsp)
233 		return NULL;
234 
235 	return pci_upstream_bridge(vsp);
236 }
237 
238 static void ras_usp_aer_init(struct xe_device *xe)
239 {
240 	struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
241 	struct pci_dev *usp;
242 	u16 aer_cap;
243 	u32 status;
244 
245 	usp = find_usp_dev(pdev);
246 	if (!usp)
247 		return;
248 
249 	aer_cap = pci_find_ext_capability(usp, PCI_EXT_CAP_ID_ERR);
250 	if (!aer_cap) {
251 		dev_warn(&usp->dev, "AER capability unavailable\n");
252 		return;
253 	}
254 
255 	/*
256 	 * Clear any stale Uncorrectable Internal Error Status event in Uncorrectable Error
257 	 * Status Register.
258 	 */
259 	pci_read_config_dword(usp, aer_cap + PCI_ERR_UNCOR_STATUS, &status);
260 	if (status & PCI_ERR_UNC_INTN)
261 		pci_write_config_dword(usp, aer_cap + PCI_ERR_UNCOR_STATUS, PCI_ERR_UNC_INTN);
262 
263 	/*
264 	 * All errors are steered to USP which is a PCIe AER Compliant device.
265 	 * Downgrade all the errors to non-fatal to prevent PCIe bus driver
266 	 * from triggering a Secondary Bus Reset (SBR). This allows error
267 	 * detection, containment and recovery in the driver.
268 	 *
269 	 * The Uncorrectable Error Severity Register has the 'Uncorrectable
270 	 * Internal Error Severity' set to fatal by default. Set this to
271 	 * non-fatal and unmask the error.
272 	 */
273 
274 	/* Downgrade Uncorrectable Internal Error to non-fatal */
275 	pci_clear_and_set_config_dword(usp, aer_cap + PCI_ERR_UNCOR_SEVER, PCI_ERR_UNC_INTN, 0);
276 
277 	/* Unmask Uncorrectable Internal Error */
278 	pci_clear_and_set_config_dword(usp, aer_cap + PCI_ERR_UNCOR_MASK, PCI_ERR_UNC_INTN, 0);
279 
280 	pci_save_state(usp);
281 	dev_dbg(&usp->dev, "Uncorrectable Internal Errors downgraded and unmasked\n");
282 }
283 
284 static void ras_send_error_event(struct xe_device *xe, u8 severity, u8 component)
285 {
286 	struct xe_ras_error_class counter = {0};
287 	u8 drm_severity, drm_component;
288 	u32 value;
289 	int ret;
290 
291 	counter.common.severity = severity;
292 	counter.common.component = component;
293 
294 	ret = get_counter(xe, &counter, &value);
295 	if (ret)
296 		return;
297 
298 	drm_severity = xe_to_drm_ras_severity(severity);
299 	drm_component = xe_to_drm_ras_component(component);
300 
301 	xe_drm_ras_event(xe, drm_component, drm_severity, value);
302 }
303 
304 static u8 handle_core_compute_errors(struct xe_ras_error_array *arr)
305 {
306 	struct xe_ras_compute_error *error_info = (void *)arr->details;
307 	u8 uncorr_type;
308 
309 	uncorr_type = FIELD_GET(CORE_COMPUTE_UNCORR_TYPE, error_info->log_header);
310 
311 	/* Request a reset if error is global */
312 	if (uncorr_type == GLOBAL_UNCORR_ERROR)
313 		return XE_RAS_RECOVERY_ACTION_RESET;
314 
315 	/*
316 	 * No action needed for other errors.
317 	 * Local errors are recovered using an engine reset by GuC.
318 	 */
319 	return XE_RAS_RECOVERY_ACTION_RECOVERED;
320 }
321 
322 static void punit_error_handler(struct xe_device *xe)
323 {
324 	xe_device_set_wedged_method(xe, DRM_WEDGE_RECOVERY_COLD_RESET);
325 	xe_device_declare_wedged(xe);
326 }
327 
328 static u8 handle_soc_internal_errors(struct xe_device *xe, struct xe_ras_error_array *arr)
329 {
330 	struct xe_ras_soc_error *info = (void *)arr->details;
331 	struct xe_ras_soc_error_source *source = &info->source;
332 	struct xe_ras_error_class *counter = &arr->counter;
333 
334 	if (source->csc) {
335 		struct xe_ras_csc_error *csc_error = (void *)info->details;
336 
337 		/*
338 		 * CSC uncorrectable errors are classified as hardware errors and firmware errors.
339 		 * CSC firmware errors are critical errors that can be recovered only by firmware
340 		 * update via SPI driver. On a CSC firmware error, PCODE enables FDO mode and sets
341 		 * the bit in the capability register. On receiving this error, the driver enables
342 		 * runtime survivability mode which notifies userspace that a firmware update
343 		 * is required.
344 		 */
345 		if (csc_error->hec_fw_error) {
346 			xe_err(xe, "[RAS]: CSC %s detected: 0x%x\n",
347 			       sev_to_str(counter->common.severity),
348 			       csc_error->hec_fw_error);
349 			xe_survivability_mode_runtime_enable(xe);
350 			return XE_RAS_RECOVERY_ACTION_DISCONNECT;
351 		}
352 	} else if (source->ieh) {
353 		struct xe_ras_ieh_error *ieh_error = (void *)info->details;
354 
355 		if (ieh_error->global_error_status & XE_RAS_SOC_IEH_PUNIT) {
356 			xe_err(xe, "[RAS]: PUNIT %s detected: 0x%x\n",
357 			       sev_to_str(counter->common.severity),
358 			       ieh_error->global_error_status);
359 			punit_error_handler(xe);
360 			return XE_RAS_RECOVERY_ACTION_DISCONNECT;
361 		}
362 	}
363 
364 	/* For other SoC internal errors, request a reset as recovery mechanism */
365 	return XE_RAS_RECOVERY_ACTION_RESET;
366 }
367 
368 static u8 handle_device_memory_errors(struct xe_device *xe, struct xe_ras_error_array *arr)
369 {
370 	struct xe_ras_memory_error *info = (void *)arr->details;
371 
372 	/*
373 	 * For memory errors, the recovery action depends on the error category
374 	 *
375 	 * TODO: Double-bit ECC errors: Page offlining
376 	 * Poison and data parity errors: Log only
377 	 * For any other memory errors, request a reset as recovery mechanism
378 	 */
379 	switch (info->category) {
380 	case XE_RAS_MEMORY_POISON:
381 		xe_info(xe, "[RAS]: Poison error detected\n");
382 		break;
383 	case XE_RAS_MEMORY_DATA_PARITY:
384 		xe_info(xe, "[RAS]: Data parity error detected\n");
385 		break;
386 	case XE_RAS_MEMORY_DB_ECC:
387 		xe_info(xe, "[RAS]: Double-bit ECC error detected at sw address 0x%llx\n",
388 			info->sw_address);
389 		/* TODO: Add page offlining for Double-bit ECC error */
390 		fallthrough;
391 	default:
392 		return XE_RAS_RECOVERY_ACTION_RESET;
393 	}
394 
395 	return XE_RAS_RECOVERY_ACTION_RECOVERED;
396 }
397 
398 void xe_ras_counter_threshold_crossed(struct xe_device *xe,
399 				      struct xe_sysctrl_event_response *response)
400 {
401 	struct xe_ras_threshold_crossed *pending = (void *)&response->data;
402 	struct xe_ras_error_class *errors = pending->counters;
403 	u32 id, ncounters = pending->ncounters;
404 	u8 sent = 0;
405 
406 	BUILD_BUG_ON(sizeof(response->data) < sizeof(*pending));
407 	BUILD_BUG_ON(BITS_PER_TYPE(sent) < XE_RAS_COMP_MAX);
408 	xe_device_assert_mem_access(xe);
409 
410 	if (!ncounters || ncounters > XE_RAS_NUM_COUNTERS)
411 		xe_err(xe, "sysctrl: unexpected counter threshold crossed %u\n", ncounters);
412 	else
413 		xe_warn(xe, "[RAS]: counter threshold crossed, %u new errors\n", ncounters);
414 
415 	for (id = 0; id < ncounters && id < XE_RAS_NUM_COUNTERS; id++) {
416 		u8 severity, component;
417 
418 		severity = errors[id].common.severity;
419 		component = errors[id].common.component;
420 
421 		if (!ras_counter_is_valid(xe, &errors[id]))
422 			continue;
423 
424 		xe_warn(xe, "[RAS]: %s %s detected\n",
425 			comp_to_str(component), sev_to_str(severity));
426 
427 		/* Send event once per component */
428 		if (sent & BIT(component))
429 			continue;
430 		sent |= BIT(component);
431 
432 		ras_send_error_event(xe, severity, component);
433 	}
434 }
435 
436 static int get_counter(struct xe_device *xe, struct xe_ras_error_class *counter, u32 *value)
437 {
438 	struct xe_ras_get_counter_response response = {0};
439 	struct xe_ras_get_counter_request request = {0};
440 	struct xe_sysctrl_mailbox_command command = {0};
441 	struct xe_ras_error_common *common;
442 	size_t rlen;
443 	int ret;
444 
445 	request.counter = *counter;
446 
447 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_GET_COUNTER,
448 				  &request, sizeof(request), &response, sizeof(response));
449 
450 	ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
451 	if (ret) {
452 		xe_err(xe, "sysctrl: failed to get counter %d\n", ret);
453 		return ret;
454 	}
455 
456 	if (rlen != sizeof(response)) {
457 		xe_err(xe, "sysctrl: unexpected get counter response length %zu (expected %zu)\n",
458 		       rlen, sizeof(response));
459 		return -EIO;
460 	}
461 
462 	if (!ras_counter_is_valid(xe, &response.counter))
463 		return -EBADMSG;
464 
465 	common = &response.counter.common;
466 	*value = response.value;
467 
468 	xe_dbg(xe, "[RAS]: get counter %u for %s %s\n", *value, comp_to_str(common->component),
469 	       sev_to_str(common->severity));
470 
471 	return 0;
472 }
473 
474 /**
475  * xe_ras_process_errors() - Process and contain hardware errors
476  * @xe: xe device instance
477  *
478  * Get error details from system controller and return recovery
479  * method.
480  *
481  * Returns: recovery action to be taken
482  */
483 enum xe_ras_recovery_action xe_ras_process_errors(struct xe_device *xe)
484 {
485 	struct xe_sysctrl_mailbox_command command = {0};
486 	enum xe_ras_recovery_action final_action;
487 	u32 remaining = XE_SYSCTRL_FLOOD_LIMIT;
488 	struct xe_ras_get_soc_error response;
489 	u8 sent = 0;
490 	size_t rlen;
491 	int ret;
492 
493 	if (xe_fault_wedge_cold_reset()) {
494 		xe_err(xe, "[RAS]: cold-reset wedge injected\n");
495 		punit_error_handler(xe);
496 		return XE_RAS_RECOVERY_ACTION_DISCONNECT;
497 	}
498 
499 	if (!xe->info.has_sysctrl)
500 		return XE_RAS_RECOVERY_ACTION_RESET;
501 
502 	BUILD_BUG_ON(BITS_PER_TYPE(sent) < XE_RAS_COMP_MAX);
503 	/* Default action */
504 	final_action = XE_RAS_RECOVERY_ACTION_RECOVERED;
505 
506 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_GET_SOC_ERROR,
507 				  NULL, 0, &response, sizeof(response));
508 
509 	do {
510 		memset(&response, 0, sizeof(response));
511 
512 		ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
513 		if (ret) {
514 			xe_err(xe, "sysctrl: failed to get soc error %d\n", ret);
515 			goto err;
516 		}
517 
518 		if (rlen != sizeof(response)) {
519 			xe_err(xe, "sysctrl: unexpected get soc error response length %zu (expected %zu)\n",
520 			       rlen, sizeof(response));
521 			goto err;
522 		}
523 
524 		/* Report if number of errors exceeds the maximum errors supported */
525 		if (response.num_errors > XE_RAS_NUM_ERROR_ARR)
526 			xe_err(xe, "sysctrl: number of errors received %d out of bound (%d)\n",
527 			       response.num_errors, XE_RAS_NUM_ERROR_ARR);
528 
529 		for (int i = 0; i < response.num_errors && i < XE_RAS_NUM_ERROR_ARR; i++) {
530 			struct xe_ras_error_array *arr = &response.arr[i];
531 			enum xe_ras_recovery_action action;
532 			u8 component, severity;
533 
534 			component = arr->counter.common.component;
535 			severity = arr->counter.common.severity;
536 
537 			if (!ras_counter_is_valid(xe, &arr->counter))
538 				continue;
539 
540 			xe_info(xe, "[RAS]: %s %s detected\n", comp_to_str(component),
541 				sev_to_str(severity));
542 
543 			/* Send event once per component */
544 			if (!(sent & BIT(component))) {
545 				sent |= BIT(component);
546 				ras_send_error_event(xe, severity, component);
547 			}
548 
549 			switch (component) {
550 			case XE_RAS_COMP_CORE_COMPUTE:
551 				action = handle_core_compute_errors(arr);
552 				break;
553 			case XE_RAS_COMP_SOC_INTERNAL:
554 				action = handle_soc_internal_errors(xe, arr);
555 				break;
556 			case XE_RAS_COMP_DEVICE_MEMORY:
557 				action = handle_device_memory_errors(xe, arr);
558 				break;
559 			default:
560 				/* For any other component, reset */
561 				action = XE_RAS_RECOVERY_ACTION_RESET;
562 				break;
563 			}
564 
565 			/* Process and log all errors and then trigger highest recovery action */
566 			if (action > final_action)
567 				final_action = action;
568 		}
569 
570 		/* Treat flooding as a system controller error */
571 		if (!--remaining) {
572 			xe_err(xe, "[RAS]: sysctrl: get soc error response flooding\n");
573 			goto err;
574 		}
575 
576 	} while (response.additional_errors);
577 
578 	return final_action;
579 
580 err:
581 	return XE_RAS_RECOVERY_ACTION_RESET;
582 }
583 
584 /**
585  * xe_ras_get_counter() - Get error counter value
586  * @xe: Xe device instance
587  * @severity: Error severity to be queried (&enum drm_xe_ras_error_severity)
588  * @component: Error component to be queried (&enum drm_xe_ras_error_component)
589  * @value: Counter value
590  *
591  * This function retrieves the value of a specific error counter based on
592  * the error severity and component.
593  *
594  * Return: 0 on success, negative error code on failure.
595  */
596 int xe_ras_get_counter(struct xe_device *xe, u8 severity, u8 component, u32 *value)
597 {
598 	struct xe_ras_error_class counter = {0};
599 
600 	counter.common.severity = drm_to_xe_ras_severity(severity);
601 	counter.common.component = drm_to_xe_ras_component(component);
602 
603 	guard(xe_pm_runtime)(xe);
604 	return get_counter(xe, &counter, value);
605 }
606 
607 /**
608  * xe_ras_clear_counter() - Clear error counter value
609  * @xe: Xe device instance
610  * @severity: Error severity to be cleared (&enum drm_xe_ras_error_severity)
611  * @component: Error component to be cleared (&enum drm_xe_ras_error_component)
612  *
613  * This function clears the value of a specific error counter based on
614  * the error severity and component.
615  *
616  * Return: 0 on success, negative error code on failure.
617  */
618 int xe_ras_clear_counter(struct xe_device *xe, u8 severity, u8 component)
619 {
620 	struct xe_ras_clear_counter_response response = {0};
621 	struct xe_ras_clear_counter_request request = {0};
622 	struct xe_sysctrl_mailbox_command command = {0};
623 	struct xe_ras_error_class *counter;
624 	size_t rlen;
625 	int ret;
626 
627 	counter = &request.counter;
628 	counter->common.severity = drm_to_xe_ras_severity(severity);
629 	counter->common.component = drm_to_xe_ras_component(component);
630 
631 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_CLEAR_COUNTER,
632 				  &request, sizeof(request), &response, sizeof(response));
633 
634 	guard(xe_pm_runtime)(xe);
635 	ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
636 	if (ret) {
637 		xe_err(xe, "sysctrl: failed to clear counter %d\n", ret);
638 		return ret;
639 	}
640 
641 	if (rlen != sizeof(response)) {
642 		xe_err(xe, "sysctrl: unexpected clear counter response length %zu (expected %zu)\n",
643 		       rlen, sizeof(response));
644 		return -EIO;
645 	}
646 
647 	ret = ras_status_to_errno(response.status);
648 	if (ret) {
649 		xe_err(xe, "sysctrl: clear counter command failed with status %#x\n",
650 		       response.status);
651 		return ret;
652 	}
653 
654 	counter = &response.counter;
655 
656 	if (!ras_counter_is_valid(xe, counter))
657 		return -EBADMSG;
658 
659 	xe_dbg(xe, "[RAS]: clear counter for %s %s\n", comp_to_str(counter->common.component),
660 	       sev_to_str(counter->common.severity));
661 
662 	return 0;
663 }
664 
665 /**
666  * xe_ras_get_threshold() - Get error counter threshold
667  * @xe: Xe device instance
668  * @severity: Error severity to be queried (&enum drm_xe_ras_error_severity)
669  * @component: Error component to be queried (&enum drm_xe_ras_error_component)
670  * @threshold: Counter threshold
671  *
672  * This function retrieves the error threshold of a specific counter based on
673  * severity and component.
674  *
675  * Return: 0 on success, negative error code on failure.
676  */
677 int xe_ras_get_threshold(struct xe_device *xe, u8 severity, u8 component, u32 *threshold)
678 {
679 	struct xe_ras_get_threshold_response response = {};
680 	struct xe_ras_get_threshold_request request = {};
681 	struct xe_sysctrl_mailbox_command command = {};
682 	struct xe_ras_error_class *counter;
683 	size_t len;
684 	int ret;
685 
686 	counter = &request.counter;
687 	counter->common.severity = drm_to_xe_ras_severity(severity);
688 	counter->common.component = drm_to_xe_ras_component(component);
689 
690 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_GET_THRESHOLD,
691 				  &request, sizeof(request), &response, sizeof(response));
692 
693 	guard(xe_pm_runtime)(xe);
694 	ret = xe_sysctrl_send_command(&xe->sc, &command, &len);
695 	if (ret) {
696 		xe_err(xe, "sysctrl: failed to get threshold %d\n", ret);
697 		return ret;
698 	}
699 
700 	if (len != sizeof(response)) {
701 		xe_err(xe, "sysctrl: unexpected get threshold response length %zu (expected %zu)\n",
702 		       len, sizeof(response));
703 		return -EIO;
704 	}
705 
706 	if (!ras_counter_is_valid(xe, &response.counter))
707 		return -EBADMSG;
708 
709 	counter = &response.counter;
710 	*threshold = response.threshold;
711 
712 	xe_dbg(xe, "[RAS]: get threshold %u for %s %s\n", *threshold,
713 	       comp_to_str(counter->common.component), sev_to_str(counter->common.severity));
714 	return 0;
715 }
716 
717 /**
718  * xe_ras_set_threshold() - Set error counter threshold
719  * @xe: Xe device instance
720  * @severity: Error severity to be set (&enum drm_xe_ras_error_severity)
721  * @component: Error component to be set (&enum drm_xe_ras_error_component)
722  * @threshold: Counter threshold
723  *
724  * This function sets the error threshold of a specific counter based on
725  * severity and component.
726  *
727  * Return: 0 on success, negative error code on failure.
728  */
729 int xe_ras_set_threshold(struct xe_device *xe, u8 severity, u8 component, u32 threshold)
730 {
731 	struct xe_ras_set_threshold_response response = {};
732 	struct xe_ras_set_threshold_request request = {};
733 	struct xe_sysctrl_mailbox_command command = {};
734 	struct xe_ras_error_class *counter;
735 	size_t len;
736 	int ret;
737 
738 	counter = &request.counter;
739 	counter->common.severity = drm_to_xe_ras_severity(severity);
740 	counter->common.component = drm_to_xe_ras_component(component);
741 	request.threshold = threshold;
742 
743 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_SET_THRESHOLD,
744 				  &request, sizeof(request), &response, sizeof(response));
745 
746 	guard(xe_pm_runtime)(xe);
747 	ret = xe_sysctrl_send_command(&xe->sc, &command, &len);
748 	if (ret) {
749 		xe_err(xe, "sysctrl: failed to set threshold %d\n", ret);
750 		return ret;
751 	}
752 
753 	if (len != sizeof(response)) {
754 		xe_err(xe, "sysctrl: unexpected set threshold response length %zu (expected %zu)\n",
755 		       len, sizeof(response));
756 		return -EIO;
757 	}
758 
759 	ret = ras_status_to_errno(response.status);
760 	if (ret) {
761 		xe_err(xe, "sysctrl: set threshold command failed with status %#x\n",
762 		       response.status);
763 		return ret;
764 	}
765 
766 	counter = &response.counter;
767 
768 	if (!ras_counter_is_valid(xe, counter))
769 		return -EBADMSG;
770 
771 	xe_dbg(xe, "[RAS]: set threshold %u for %s %s\n", response.threshold,
772 	       comp_to_str(counter->common.component), sev_to_str(counter->common.severity));
773 	return 0;
774 }
775 
776 static ssize_t gpu_health_show(struct device *dev, struct device_attribute *attr, char *buf)
777 {
778 	struct xe_ras_get_health_response response = {0};
779 	struct xe_sysctrl_mailbox_command command = {0};
780 	struct xe_ras_get_health_request request = {0};
781 	struct xe_device *xe = kdev_to_xe_device(dev);
782 	const char *health;
783 	size_t rlen;
784 	int ret;
785 
786 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_GET_HEALTH,
787 				  &request, sizeof(request), &response, sizeof(response));
788 	guard(xe_pm_runtime)(xe);
789 	ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
790 	if (ret) {
791 		xe_err(xe, "sysctrl: failed to get health %d\n", ret);
792 		return ret;
793 	}
794 
795 	if (rlen != sizeof(response)) {
796 		xe_err(xe, "sysctrl: unexpected get health response length %zu (expected %zu)\n",
797 		       rlen, sizeof(response));
798 		return -EIO;
799 	}
800 	if (response.health >= XE_RAS_HEALTH_MAX) {
801 		xe_err(xe, "sysctrl: invalid health state %u\n",
802 		       response.health);
803 		return -EIO;
804 	}
805 
806 	health = gpu_health_states[response.health];
807 
808 	xe_dbg(xe, "[RAS]: get health: %s\n", health);
809 
810 	return sysfs_emit(buf, "%s\n", health);
811 }
812 
813 static ssize_t gpu_health_store(struct device *dev, struct device_attribute *attr,
814 				const char *buf, size_t count)
815 {
816 	struct xe_ras_set_health_response response = {0};
817 	struct xe_sysctrl_mailbox_command command = {0};
818 	struct xe_ras_set_health_request request = {0};
819 	struct xe_device *xe = kdev_to_xe_device(dev);
820 	const char *health;
821 	size_t rlen;
822 	int state;
823 	int ret;
824 
825 	state = sysfs_match_string(gpu_health_states, buf);
826 	if (state < 0)
827 		return -EINVAL;
828 
829 	request.health = state;
830 
831 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_SET_HEALTH,
832 				  &request, sizeof(request), &response, sizeof(response));
833 	guard(xe_pm_runtime)(xe);
834 	ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
835 	if (ret) {
836 		xe_err(xe, "sysctrl: failed to set health %d\n", ret);
837 		return ret;
838 	}
839 
840 	if (rlen != sizeof(response)) {
841 		xe_err(xe, "sysctrl: unexpected set health response length %zu (expected %zu)\n",
842 		       rlen, sizeof(response));
843 		return -EIO;
844 	}
845 
846 	ret = ras_status_to_errno(response.status);
847 	if (ret) {
848 		xe_err(xe, "sysctrl: set health command failed with status %#x\n",
849 		       response.status);
850 		return ret;
851 	}
852 
853 	if (response.health >= XE_RAS_HEALTH_MAX) {
854 		xe_err(xe, "sysctrl: invalid health state %u\n",
855 		       response.health);
856 		return -EIO;
857 	}
858 
859 	health = gpu_health_states[response.health];
860 
861 	xe_dbg(xe, "[RAS]: set health: %s\n", health);
862 
863 	return count;
864 }
865 static DEVICE_ATTR_RW(gpu_health);
866 
867 static struct attribute *gpu_health_attrs[] = {
868 	&dev_attr_gpu_health.attr,
869 	NULL
870 };
871 
872 /**
873  * DOC: GPU Health Indicator
874  *
875  * On Intel Xe platforms that support the gpu health indicator interface,
876  * the driver exposes this sysfs attribute for in-band access to the gpu
877  * health state::
878  *
879  *     /sys/bus/pci/devices/<device>/gpu_health
880  *
881  * Reading the attribute is available to all users and returns a single
882  * line containing the current gpu health state, whereas writing is
883  * restricted to administrative users and updates the state to one of the
884  * valid values.
885  *
886  * Management tools and administrators use this interface to query the
887  * current gpu health state (e.g. for telemetry/monitoring) and to
888  * update it - for example, to mark the gpu as ``warning`` or ``critical``
889  * after diagnostics, or reset it back to ``ok`` once remediated.
890  *
891  * The valid values for the gpu health state are:
892  *
893  * - ``ok``
894  *     The gpu is healthy and operating within normal parameters.
895  *
896  * - ``warning``
897  *     The gpu is experiencing minor issues but remains operational.
898  *
899  * - ``critical``
900  *     The gpu is in a critical state and may not be operational.
901  *
902  * See Documentation/ABI/testing/sysfs-driver-intel-xe-ras for the ABI
903  * specification.
904  */
905 static const struct attribute_group gpu_health_group = {
906 	.attrs = gpu_health_attrs,
907 };
908 
909 /**
910  * xe_ras_init - Initialize Xe RAS
911  * @xe: xe device instance
912  *
913  * Initialize Xe RAS
914  */
915 void xe_ras_init(struct xe_device *xe)
916 {
917 	int ret;
918 
919 	/*
920 	 * TODO: Replace platform check with xe->info.has_disable_vram_page_offline
921 	 * once the feature flag is plumbed through device info.
922 	 */
923 	if (xe->info.platform == XE_CRESCENTISLAND)
924 		xe->ras.disable_vram_page_offline =
925 			xe_configfs_get_disable_vram_page_offline(to_pci_dev(xe->drm.dev));
926 
927 	xe_drm_ras_init(xe);
928 
929 	if (!xe->info.has_sysctrl)
930 		return;
931 
932 	if (IS_ENABLED(CONFIG_PCIEAER))
933 		ras_usp_aer_init(xe);
934 
935 	ret = devm_device_add_group(xe->drm.dev, &gpu_health_group);
936 	if (ret)
937 		xe_err(xe, "Failed to create GPU health sysfs, err=%d\n", ret);
938 }
939