xref: /linux/drivers/gpu/drm/xe/xe_ras.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
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_declare_wedged(xe, DRM_WEDGE_RECOVERY_COLD_RESET);
325 }
326 
327 static u8 handle_soc_internal_errors(struct xe_device *xe, struct xe_ras_error_array *arr)
328 {
329 	struct xe_ras_soc_error *info = (void *)arr->details;
330 	struct xe_ras_soc_error_source *source = &info->source;
331 	struct xe_ras_error_class *counter = &arr->counter;
332 
333 	if (source->csc) {
334 		struct xe_ras_csc_error *csc_error = (void *)info->details;
335 
336 		/*
337 		 * CSC uncorrectable errors are classified as hardware errors and firmware errors.
338 		 * CSC firmware errors are critical errors that can be recovered only by firmware
339 		 * update via SPI driver. On a CSC firmware error, PCODE enables FDO mode and sets
340 		 * the bit in the capability register. On receiving this error, the driver enables
341 		 * runtime survivability mode which notifies userspace that a firmware update
342 		 * is required.
343 		 */
344 		if (csc_error->hec_fw_error) {
345 			xe_err(xe, "[RAS]: CSC %s detected: 0x%x\n",
346 			       sev_to_str(counter->common.severity),
347 			       csc_error->hec_fw_error);
348 			xe_survivability_mode_runtime_enable(xe);
349 			return XE_RAS_RECOVERY_ACTION_DISCONNECT;
350 		}
351 	} else if (source->ieh) {
352 		struct xe_ras_ieh_error *ieh_error = (void *)info->details;
353 
354 		if (ieh_error->global_error_status & XE_RAS_SOC_IEH_PUNIT) {
355 			xe_err(xe, "[RAS]: PUNIT %s detected: 0x%x\n",
356 			       sev_to_str(counter->common.severity),
357 			       ieh_error->global_error_status);
358 			punit_error_handler(xe);
359 			return XE_RAS_RECOVERY_ACTION_DISCONNECT;
360 		}
361 	}
362 
363 	/* For other SoC internal errors, request a reset as recovery mechanism */
364 	return XE_RAS_RECOVERY_ACTION_RESET;
365 }
366 
367 static u8 handle_device_memory_errors(struct xe_device *xe, struct xe_ras_error_array *arr)
368 {
369 	struct xe_ras_memory_error *info = (void *)arr->details;
370 
371 	/*
372 	 * For memory errors, the recovery action depends on the error category
373 	 *
374 	 * TODO: Double-bit ECC errors: Page offlining
375 	 * Poison and data parity errors: Log only
376 	 * For any other memory errors, request a reset as recovery mechanism
377 	 */
378 	switch (info->category) {
379 	case XE_RAS_MEMORY_POISON:
380 		xe_info(xe, "[RAS]: Poison error detected\n");
381 		break;
382 	case XE_RAS_MEMORY_DATA_PARITY:
383 		xe_info(xe, "[RAS]: Data parity error detected\n");
384 		break;
385 	case XE_RAS_MEMORY_DB_ECC:
386 		xe_info(xe, "[RAS]: Double-bit ECC error detected at sw address 0x%llx\n",
387 			info->sw_address);
388 		/* TODO: Add page offlining for Double-bit ECC error */
389 		fallthrough;
390 	default:
391 		return XE_RAS_RECOVERY_ACTION_RESET;
392 	}
393 
394 	return XE_RAS_RECOVERY_ACTION_RECOVERED;
395 }
396 
397 void xe_ras_counter_threshold_crossed(struct xe_device *xe,
398 				      struct xe_sysctrl_event_response *response)
399 {
400 	struct xe_ras_threshold_crossed *pending = (void *)&response->data;
401 	struct xe_ras_error_class *errors = pending->counters;
402 	u32 id, ncounters = pending->ncounters;
403 	u8 sent = 0;
404 
405 	BUILD_BUG_ON(sizeof(response->data) < sizeof(*pending));
406 	BUILD_BUG_ON(BITS_PER_TYPE(sent) < XE_RAS_COMP_MAX);
407 	xe_device_assert_mem_access(xe);
408 
409 	if (!ncounters || ncounters > XE_RAS_NUM_COUNTERS)
410 		xe_err(xe, "sysctrl: unexpected counter threshold crossed %u\n", ncounters);
411 	else
412 		xe_warn(xe, "[RAS]: counter threshold crossed, %u new errors\n", ncounters);
413 
414 	for (id = 0; id < ncounters && id < XE_RAS_NUM_COUNTERS; id++) {
415 		u8 severity, component;
416 
417 		severity = errors[id].common.severity;
418 		component = errors[id].common.component;
419 
420 		if (!ras_counter_is_valid(xe, &errors[id]))
421 			continue;
422 
423 		xe_warn(xe, "[RAS]: %s %s detected\n",
424 			comp_to_str(component), sev_to_str(severity));
425 
426 		/* Send event once per component */
427 		if (sent & BIT(component))
428 			continue;
429 		sent |= BIT(component);
430 
431 		ras_send_error_event(xe, severity, component);
432 	}
433 }
434 
435 static int get_counter(struct xe_device *xe, struct xe_ras_error_class *counter, u32 *value)
436 {
437 	struct xe_ras_get_counter_response response = {0};
438 	struct xe_ras_get_counter_request request = {0};
439 	struct xe_sysctrl_mailbox_command command = {0};
440 	struct xe_ras_error_common *common;
441 	size_t rlen;
442 	int ret;
443 
444 	request.counter = *counter;
445 
446 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_GET_COUNTER,
447 				  &request, sizeof(request), &response, sizeof(response));
448 
449 	ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
450 	if (ret) {
451 		xe_err(xe, "sysctrl: failed to get counter %d\n", ret);
452 		return ret;
453 	}
454 
455 	if (rlen != sizeof(response)) {
456 		xe_err(xe, "sysctrl: unexpected get counter response length %zu (expected %zu)\n",
457 		       rlen, sizeof(response));
458 		return -EIO;
459 	}
460 
461 	if (!ras_counter_is_valid(xe, &response.counter))
462 		return -EBADMSG;
463 
464 	common = &response.counter.common;
465 	*value = response.value;
466 
467 	xe_dbg(xe, "[RAS]: get counter %u for %s %s\n", *value, comp_to_str(common->component),
468 	       sev_to_str(common->severity));
469 
470 	return 0;
471 }
472 
473 /**
474  * xe_ras_process_errors() - Process and contain hardware errors
475  * @xe: xe device instance
476  *
477  * Get error details from system controller and return recovery
478  * method.
479  *
480  * Returns: recovery action to be taken
481  */
482 enum xe_ras_recovery_action xe_ras_process_errors(struct xe_device *xe)
483 {
484 	struct xe_sysctrl_mailbox_command command = {0};
485 	enum xe_ras_recovery_action final_action;
486 	u32 remaining = XE_SYSCTRL_FLOOD_LIMIT;
487 	struct xe_ras_get_soc_error response;
488 	u8 sent = 0;
489 	size_t rlen;
490 	int ret;
491 
492 	if (xe_fault_wedge_cold_reset()) {
493 		xe_err(xe, "[RAS]: cold-reset wedge injected\n");
494 		punit_error_handler(xe);
495 		return XE_RAS_RECOVERY_ACTION_DISCONNECT;
496 	}
497 
498 	if (!xe->info.has_sysctrl)
499 		return XE_RAS_RECOVERY_ACTION_RESET;
500 
501 	BUILD_BUG_ON(BITS_PER_TYPE(sent) < XE_RAS_COMP_MAX);
502 	/* Default action */
503 	final_action = XE_RAS_RECOVERY_ACTION_RECOVERED;
504 
505 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_GET_SOC_ERROR,
506 				  NULL, 0, &response, sizeof(response));
507 
508 	do {
509 		memset(&response, 0, sizeof(response));
510 
511 		ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
512 		if (ret) {
513 			xe_err(xe, "sysctrl: failed to get soc error %d\n", ret);
514 			goto err;
515 		}
516 
517 		if (rlen != sizeof(response)) {
518 			xe_err(xe, "sysctrl: unexpected get soc error response length %zu (expected %zu)\n",
519 			       rlen, sizeof(response));
520 			goto err;
521 		}
522 
523 		/* Report if number of errors exceeds the maximum errors supported */
524 		if (response.num_errors > XE_RAS_NUM_ERROR_ARR)
525 			xe_err(xe, "sysctrl: number of errors received %d out of bound (%d)\n",
526 			       response.num_errors, XE_RAS_NUM_ERROR_ARR);
527 
528 		for (int i = 0; i < response.num_errors && i < XE_RAS_NUM_ERROR_ARR; i++) {
529 			struct xe_ras_error_array *arr = &response.arr[i];
530 			enum xe_ras_recovery_action action;
531 			u8 component, severity;
532 
533 			component = arr->counter.common.component;
534 			severity = arr->counter.common.severity;
535 
536 			if (!ras_counter_is_valid(xe, &arr->counter))
537 				continue;
538 
539 			xe_info(xe, "[RAS]: %s %s detected\n", comp_to_str(component),
540 				sev_to_str(severity));
541 
542 			/* Send event once per component */
543 			if (!(sent & BIT(component))) {
544 				sent |= BIT(component);
545 				ras_send_error_event(xe, severity, component);
546 			}
547 
548 			switch (component) {
549 			case XE_RAS_COMP_CORE_COMPUTE:
550 				action = handle_core_compute_errors(arr);
551 				break;
552 			case XE_RAS_COMP_SOC_INTERNAL:
553 				action = handle_soc_internal_errors(xe, arr);
554 				break;
555 			case XE_RAS_COMP_DEVICE_MEMORY:
556 				action = handle_device_memory_errors(xe, arr);
557 				break;
558 			default:
559 				/* For any other component, reset */
560 				action = XE_RAS_RECOVERY_ACTION_RESET;
561 				break;
562 			}
563 
564 			/* Process and log all errors and then trigger highest recovery action */
565 			if (action > final_action)
566 				final_action = action;
567 		}
568 
569 		/* Treat flooding as a system controller error */
570 		if (!--remaining) {
571 			xe_err(xe, "[RAS]: sysctrl: get soc error response flooding\n");
572 			goto err;
573 		}
574 
575 	} while (response.additional_errors);
576 
577 	return final_action;
578 
579 err:
580 	return XE_RAS_RECOVERY_ACTION_RESET;
581 }
582 
583 /**
584  * xe_ras_get_counter() - Get error counter value
585  * @xe: Xe device instance
586  * @severity: Error severity to be queried (&enum drm_xe_ras_error_severity)
587  * @component: Error component to be queried (&enum drm_xe_ras_error_component)
588  * @value: Counter value
589  *
590  * This function retrieves the value of a specific error counter based on
591  * the error severity and component.
592  *
593  * Return: 0 on success, negative error code on failure.
594  */
595 int xe_ras_get_counter(struct xe_device *xe, u8 severity, u8 component, u32 *value)
596 {
597 	struct xe_ras_error_class counter = {0};
598 
599 	counter.common.severity = drm_to_xe_ras_severity(severity);
600 	counter.common.component = drm_to_xe_ras_component(component);
601 
602 	guard(xe_pm_runtime)(xe);
603 	return get_counter(xe, &counter, value);
604 }
605 
606 /**
607  * xe_ras_clear_counter() - Clear error counter value
608  * @xe: Xe device instance
609  * @severity: Error severity to be cleared (&enum drm_xe_ras_error_severity)
610  * @component: Error component to be cleared (&enum drm_xe_ras_error_component)
611  *
612  * This function clears the value of a specific error counter based on
613  * the error severity and component.
614  *
615  * Return: 0 on success, negative error code on failure.
616  */
617 int xe_ras_clear_counter(struct xe_device *xe, u8 severity, u8 component)
618 {
619 	struct xe_ras_clear_counter_response response = {0};
620 	struct xe_ras_clear_counter_request request = {0};
621 	struct xe_sysctrl_mailbox_command command = {0};
622 	struct xe_ras_error_class *counter;
623 	size_t rlen;
624 	int ret;
625 
626 	counter = &request.counter;
627 	counter->common.severity = drm_to_xe_ras_severity(severity);
628 	counter->common.component = drm_to_xe_ras_component(component);
629 
630 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_CLEAR_COUNTER,
631 				  &request, sizeof(request), &response, sizeof(response));
632 
633 	guard(xe_pm_runtime)(xe);
634 	ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
635 	if (ret) {
636 		xe_err(xe, "sysctrl: failed to clear counter %d\n", ret);
637 		return ret;
638 	}
639 
640 	if (rlen != sizeof(response)) {
641 		xe_err(xe, "sysctrl: unexpected clear counter response length %zu (expected %zu)\n",
642 		       rlen, sizeof(response));
643 		return -EIO;
644 	}
645 
646 	ret = ras_status_to_errno(response.status);
647 	if (ret) {
648 		xe_err(xe, "sysctrl: clear counter command failed with status %#x\n",
649 		       response.status);
650 		return ret;
651 	}
652 
653 	counter = &response.counter;
654 
655 	if (!ras_counter_is_valid(xe, counter))
656 		return -EBADMSG;
657 
658 	xe_dbg(xe, "[RAS]: clear counter for %s %s\n", comp_to_str(counter->common.component),
659 	       sev_to_str(counter->common.severity));
660 
661 	return 0;
662 }
663 
664 /**
665  * xe_ras_get_threshold() - Get error counter threshold
666  * @xe: Xe device instance
667  * @severity: Error severity to be queried (&enum drm_xe_ras_error_severity)
668  * @component: Error component to be queried (&enum drm_xe_ras_error_component)
669  * @threshold: Counter threshold
670  *
671  * This function retrieves the error threshold of a specific counter based on
672  * severity and component.
673  *
674  * Return: 0 on success, negative error code on failure.
675  */
676 int xe_ras_get_threshold(struct xe_device *xe, u8 severity, u8 component, u32 *threshold)
677 {
678 	struct xe_ras_get_threshold_response response = {};
679 	struct xe_ras_get_threshold_request request = {};
680 	struct xe_sysctrl_mailbox_command command = {};
681 	struct xe_ras_error_class *counter;
682 	size_t len;
683 	int ret;
684 
685 	counter = &request.counter;
686 	counter->common.severity = drm_to_xe_ras_severity(severity);
687 	counter->common.component = drm_to_xe_ras_component(component);
688 
689 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_GET_THRESHOLD,
690 				  &request, sizeof(request), &response, sizeof(response));
691 
692 	guard(xe_pm_runtime)(xe);
693 	ret = xe_sysctrl_send_command(&xe->sc, &command, &len);
694 	if (ret) {
695 		xe_err(xe, "sysctrl: failed to get threshold %d\n", ret);
696 		return ret;
697 	}
698 
699 	if (len != sizeof(response)) {
700 		xe_err(xe, "sysctrl: unexpected get threshold response length %zu (expected %zu)\n",
701 		       len, sizeof(response));
702 		return -EIO;
703 	}
704 
705 	if (!ras_counter_is_valid(xe, &response.counter))
706 		return -EBADMSG;
707 
708 	counter = &response.counter;
709 	*threshold = response.threshold;
710 
711 	xe_dbg(xe, "[RAS]: get threshold %u for %s %s\n", *threshold,
712 	       comp_to_str(counter->common.component), sev_to_str(counter->common.severity));
713 	return 0;
714 }
715 
716 /**
717  * xe_ras_set_threshold() - Set error counter threshold
718  * @xe: Xe device instance
719  * @severity: Error severity to be set (&enum drm_xe_ras_error_severity)
720  * @component: Error component to be set (&enum drm_xe_ras_error_component)
721  * @threshold: Counter threshold
722  *
723  * This function sets the error threshold of a specific counter based on
724  * severity and component.
725  *
726  * Return: 0 on success, negative error code on failure.
727  */
728 int xe_ras_set_threshold(struct xe_device *xe, u8 severity, u8 component, u32 threshold)
729 {
730 	struct xe_ras_set_threshold_response response = {};
731 	struct xe_ras_set_threshold_request request = {};
732 	struct xe_sysctrl_mailbox_command command = {};
733 	struct xe_ras_error_class *counter;
734 	size_t len;
735 	int ret;
736 
737 	counter = &request.counter;
738 	counter->common.severity = drm_to_xe_ras_severity(severity);
739 	counter->common.component = drm_to_xe_ras_component(component);
740 	request.threshold = threshold;
741 
742 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_SET_THRESHOLD,
743 				  &request, sizeof(request), &response, sizeof(response));
744 
745 	guard(xe_pm_runtime)(xe);
746 	ret = xe_sysctrl_send_command(&xe->sc, &command, &len);
747 	if (ret) {
748 		xe_err(xe, "sysctrl: failed to set threshold %d\n", ret);
749 		return ret;
750 	}
751 
752 	if (len != sizeof(response)) {
753 		xe_err(xe, "sysctrl: unexpected set threshold response length %zu (expected %zu)\n",
754 		       len, sizeof(response));
755 		return -EIO;
756 	}
757 
758 	ret = ras_status_to_errno(response.status);
759 	if (ret) {
760 		xe_err(xe, "sysctrl: set threshold command failed with status %#x\n",
761 		       response.status);
762 		return ret;
763 	}
764 
765 	counter = &response.counter;
766 
767 	if (!ras_counter_is_valid(xe, counter))
768 		return -EBADMSG;
769 
770 	xe_dbg(xe, "[RAS]: set threshold %u for %s %s\n", response.threshold,
771 	       comp_to_str(counter->common.component), sev_to_str(counter->common.severity));
772 	return 0;
773 }
774 
775 static ssize_t gpu_health_show(struct device *dev, struct device_attribute *attr, char *buf)
776 {
777 	struct xe_ras_get_health_response response = {0};
778 	struct xe_sysctrl_mailbox_command command = {0};
779 	struct xe_ras_get_health_request request = {0};
780 	struct xe_device *xe = kdev_to_xe_device(dev);
781 	const char *health;
782 	size_t rlen;
783 	int ret;
784 
785 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_GET_HEALTH,
786 				  &request, sizeof(request), &response, sizeof(response));
787 	guard(xe_pm_runtime)(xe);
788 	ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
789 	if (ret) {
790 		xe_err(xe, "sysctrl: failed to get health %d\n", ret);
791 		return ret;
792 	}
793 
794 	if (rlen != sizeof(response)) {
795 		xe_err(xe, "sysctrl: unexpected get health response length %zu (expected %zu)\n",
796 		       rlen, sizeof(response));
797 		return -EIO;
798 	}
799 	if (response.health >= XE_RAS_HEALTH_MAX) {
800 		xe_err(xe, "sysctrl: invalid health state %u\n",
801 		       response.health);
802 		return -EIO;
803 	}
804 
805 	health = gpu_health_states[response.health];
806 
807 	xe_dbg(xe, "[RAS]: get health: %s\n", health);
808 
809 	return sysfs_emit(buf, "%s\n", health);
810 }
811 
812 static ssize_t gpu_health_store(struct device *dev, struct device_attribute *attr,
813 				const char *buf, size_t count)
814 {
815 	struct xe_ras_set_health_response response = {0};
816 	struct xe_sysctrl_mailbox_command command = {0};
817 	struct xe_ras_set_health_request request = {0};
818 	struct xe_device *xe = kdev_to_xe_device(dev);
819 	const char *health;
820 	size_t rlen;
821 	int state;
822 	int ret;
823 
824 	state = sysfs_match_string(gpu_health_states, buf);
825 	if (state < 0)
826 		return -EINVAL;
827 
828 	request.health = state;
829 
830 	xe_sysctrl_create_command(&command, XE_SYSCTRL_GROUP_GFSP, XE_SYSCTRL_CMD_SET_HEALTH,
831 				  &request, sizeof(request), &response, sizeof(response));
832 	guard(xe_pm_runtime)(xe);
833 	ret = xe_sysctrl_send_command(&xe->sc, &command, &rlen);
834 	if (ret) {
835 		xe_err(xe, "sysctrl: failed to set health %d\n", ret);
836 		return ret;
837 	}
838 
839 	if (rlen != sizeof(response)) {
840 		xe_err(xe, "sysctrl: unexpected set health response length %zu (expected %zu)\n",
841 		       rlen, sizeof(response));
842 		return -EIO;
843 	}
844 
845 	ret = ras_status_to_errno(response.status);
846 	if (ret) {
847 		xe_err(xe, "sysctrl: set health command failed with status %#x\n",
848 		       response.status);
849 		return ret;
850 	}
851 
852 	if (response.health >= XE_RAS_HEALTH_MAX) {
853 		xe_err(xe, "sysctrl: invalid health state %u\n",
854 		       response.health);
855 		return -EIO;
856 	}
857 
858 	health = gpu_health_states[response.health];
859 
860 	xe_dbg(xe, "[RAS]: set health: %s\n", health);
861 
862 	return count;
863 }
864 static DEVICE_ATTR_RW(gpu_health);
865 
866 static struct attribute *gpu_health_attrs[] = {
867 	&dev_attr_gpu_health.attr,
868 	NULL
869 };
870 
871 /**
872  * DOC: GPU Health Indicator
873  *
874  * On Intel Xe platforms that support the gpu health indicator interface,
875  * the driver exposes this sysfs attribute for in-band access to the gpu
876  * health state::
877  *
878  *     /sys/bus/pci/devices/<device>/gpu_health
879  *
880  * Reading the attribute is available to all users and returns a single
881  * line containing the current gpu health state, whereas writing is
882  * restricted to administrative users and updates the state to one of the
883  * valid values.
884  *
885  * Management tools and administrators use this interface to query the
886  * current gpu health state (e.g. for telemetry/monitoring) and to
887  * update it - for example, to mark the gpu as ``warning`` or ``critical``
888  * after diagnostics, or reset it back to ``ok`` once remediated.
889  *
890  * The valid values for the gpu health state are:
891  *
892  * - ``ok``
893  *     The gpu is healthy and operating within normal parameters.
894  *
895  * - ``warning``
896  *     The gpu is experiencing minor issues but remains operational.
897  *
898  * - ``critical``
899  *     The gpu is in a critical state and may not be operational.
900  *
901  * See Documentation/ABI/testing/sysfs-driver-intel-xe-ras for the ABI
902  * specification.
903  */
904 static const struct attribute_group gpu_health_group = {
905 	.attrs = gpu_health_attrs,
906 };
907 
908 /**
909  * xe_ras_init - Initialize Xe RAS
910  * @xe: xe device instance
911  *
912  * Initialize Xe RAS
913  */
914 void xe_ras_init(struct xe_device *xe)
915 {
916 	int ret;
917 
918 	/*
919 	 * TODO: Replace platform check with xe->info.has_disable_vram_page_offline
920 	 * once the feature flag is plumbed through device info.
921 	 */
922 	if (xe->info.platform == XE_CRESCENTISLAND)
923 		xe->ras.disable_vram_page_offline =
924 			xe_configfs_get_disable_vram_page_offline(to_pci_dev(xe->drm.dev));
925 
926 	xe_drm_ras_init(xe);
927 
928 	if (!xe->info.has_sysctrl)
929 		return;
930 
931 	if (IS_ENABLED(CONFIG_PCIEAER))
932 		ras_usp_aer_init(xe);
933 
934 	ret = devm_device_add_group(xe->drm.dev, &gpu_health_group);
935 	if (ret)
936 		xe_err(xe, "Failed to create GPU health sysfs, err=%d\n", ret);
937 }
938