xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_ras.c (revision c020cf82e0cc147236f01a8dca7052034cf9d30d)
1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  *
23  */
24 #include <linux/debugfs.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/uaccess.h>
28 #include <linux/reboot.h>
29 #include <linux/syscalls.h>
30 
31 #include "amdgpu.h"
32 #include "amdgpu_ras.h"
33 #include "amdgpu_atomfirmware.h"
34 #include "amdgpu_xgmi.h"
35 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h"
36 
37 const char *ras_error_string[] = {
38 	"none",
39 	"parity",
40 	"single_correctable",
41 	"multi_uncorrectable",
42 	"poison",
43 };
44 
45 const char *ras_block_string[] = {
46 	"umc",
47 	"sdma",
48 	"gfx",
49 	"mmhub",
50 	"athub",
51 	"pcie_bif",
52 	"hdp",
53 	"xgmi_wafl",
54 	"df",
55 	"smn",
56 	"sem",
57 	"mp0",
58 	"mp1",
59 	"fuse",
60 };
61 
62 #define ras_err_str(i) (ras_error_string[ffs(i)])
63 #define ras_block_str(i) (ras_block_string[i])
64 
65 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS		1
66 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET		2
67 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
68 
69 /* inject address is 52 bits */
70 #define	RAS_UMC_INJECT_ADDR_LIMIT	(0x1ULL << 52)
71 
72 enum amdgpu_ras_retire_page_reservation {
73 	AMDGPU_RAS_RETIRE_PAGE_RESERVED,
74 	AMDGPU_RAS_RETIRE_PAGE_PENDING,
75 	AMDGPU_RAS_RETIRE_PAGE_FAULT,
76 };
77 
78 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
79 
80 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
81 				uint64_t addr);
82 
83 #ifdef __linux__
84 
85 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
86 					size_t size, loff_t *pos)
87 {
88 	struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
89 	struct ras_query_if info = {
90 		.head = obj->head,
91 	};
92 	ssize_t s;
93 	char val[128];
94 
95 	if (amdgpu_ras_error_query(obj->adev, &info))
96 		return -EINVAL;
97 
98 	s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
99 			"ue", info.ue_count,
100 			"ce", info.ce_count);
101 	if (*pos >= s)
102 		return 0;
103 
104 	s -= *pos;
105 	s = min_t(u64, s, size);
106 
107 
108 	if (copy_to_user(buf, &val[*pos], s))
109 		return -EINVAL;
110 
111 	*pos += s;
112 
113 	return s;
114 }
115 
116 static const struct file_operations amdgpu_ras_debugfs_ops = {
117 	.owner = THIS_MODULE,
118 	.read = amdgpu_ras_debugfs_read,
119 	.write = NULL,
120 	.llseek = default_llseek
121 };
122 
123 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
124 {
125 	int i;
126 
127 	for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
128 		*block_id = i;
129 		if (strcmp(name, ras_block_str(i)) == 0)
130 			return 0;
131 	}
132 	return -EINVAL;
133 }
134 
135 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
136 		const char __user *buf, size_t size,
137 		loff_t *pos, struct ras_debug_if *data)
138 {
139 	ssize_t s = min_t(u64, 64, size);
140 	char str[65];
141 	char block_name[33];
142 	char err[9] = "ue";
143 	int op = -1;
144 	int block_id;
145 	uint32_t sub_block;
146 	u64 address, value;
147 
148 	if (*pos)
149 		return -EINVAL;
150 	*pos = size;
151 
152 	memset(str, 0, sizeof(str));
153 	memset(data, 0, sizeof(*data));
154 
155 	if (copy_from_user(str, buf, s))
156 		return -EINVAL;
157 
158 	if (sscanf(str, "disable %32s", block_name) == 1)
159 		op = 0;
160 	else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
161 		op = 1;
162 	else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
163 		op = 2;
164 	else if (str[0] && str[1] && str[2] && str[3])
165 		/* ascii string, but commands are not matched. */
166 		return -EINVAL;
167 
168 	if (op != -1) {
169 		if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
170 			return -EINVAL;
171 
172 		data->head.block = block_id;
173 		/* only ue and ce errors are supported */
174 		if (!memcmp("ue", err, 2))
175 			data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
176 		else if (!memcmp("ce", err, 2))
177 			data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
178 		else
179 			return -EINVAL;
180 
181 		data->op = op;
182 
183 		if (op == 2) {
184 			if (sscanf(str, "%*s %*s %*s %u %llu %llu",
185 						&sub_block, &address, &value) != 3)
186 				if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
187 							&sub_block, &address, &value) != 3)
188 					return -EINVAL;
189 			data->head.sub_block_index = sub_block;
190 			data->inject.address = address;
191 			data->inject.value = value;
192 		}
193 	} else {
194 		if (size < sizeof(*data))
195 			return -EINVAL;
196 
197 		if (copy_from_user(data, buf, sizeof(*data)))
198 			return -EINVAL;
199 	}
200 
201 	return 0;
202 }
203 
204 /**
205  * DOC: AMDGPU RAS debugfs control interface
206  *
207  * It accepts struct ras_debug_if who has two members.
208  *
209  * First member: ras_debug_if::head or ras_debug_if::inject.
210  *
211  * head is used to indicate which IP block will be under control.
212  *
213  * head has four members, they are block, type, sub_block_index, name.
214  * block: which IP will be under control.
215  * type: what kind of error will be enabled/disabled/injected.
216  * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
217  * name: the name of IP.
218  *
219  * inject has two more members than head, they are address, value.
220  * As their names indicate, inject operation will write the
221  * value to the address.
222  *
223  * The second member: struct ras_debug_if::op.
224  * It has three kinds of operations.
225  *
226  * - 0: disable RAS on the block. Take ::head as its data.
227  * - 1: enable RAS on the block. Take ::head as its data.
228  * - 2: inject errors on the block. Take ::inject as its data.
229  *
230  * How to use the interface?
231  *
232  * Programs
233  *
234  * Copy the struct ras_debug_if in your codes and initialize it.
235  * Write the struct to the control node.
236  *
237  * Shells
238  *
239  * .. code-block:: bash
240  *
241  *	echo op block [error [sub_block address value]] > .../ras/ras_ctrl
242  *
243  * Parameters:
244  *
245  * op: disable, enable, inject
246  *	disable: only block is needed
247  *	enable: block and error are needed
248  *	inject: error, address, value are needed
249  * block: umc, sdma, gfx, .........
250  *	see ras_block_string[] for details
251  * error: ue, ce
252  *	ue: multi_uncorrectable
253  *	ce: single_correctable
254  * sub_block:
255  *	sub block index, pass 0 if there is no sub block
256  *
257  * here are some examples for bash commands:
258  *
259  * .. code-block:: bash
260  *
261  *	echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
262  *	echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
263  *	echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
264  *
265  * How to check the result?
266  *
267  * For disable/enable, please check ras features at
268  * /sys/class/drm/card[0/1/2...]/device/ras/features
269  *
270  * For inject, please check corresponding err count at
271  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
272  *
273  * .. note::
274  *	Operations are only allowed on blocks which are supported.
275  *	Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
276  *	to see which blocks support RAS on a particular asic.
277  *
278  */
279 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
280 		size_t size, loff_t *pos)
281 {
282 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
283 	struct ras_debug_if data;
284 	int ret = 0;
285 
286 	if (amdgpu_ras_intr_triggered()) {
287 		DRM_WARN("RAS WARN: error injection currently inaccessible\n");
288 		return size;
289 	}
290 
291 	ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
292 	if (ret)
293 		return -EINVAL;
294 
295 	if (!amdgpu_ras_is_supported(adev, data.head.block))
296 		return -EINVAL;
297 
298 	switch (data.op) {
299 	case 0:
300 		ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
301 		break;
302 	case 1:
303 		ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
304 		break;
305 	case 2:
306 		if ((data.inject.address >= adev->gmc.mc_vram_size) ||
307 		    (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
308 			ret = -EINVAL;
309 			break;
310 		}
311 
312 		/* umc ce/ue error injection for a bad page is not allowed */
313 		if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
314 		    amdgpu_ras_check_bad_page(adev, data.inject.address)) {
315 			DRM_WARN("RAS WARN: 0x%llx has been marked as bad before error injection!\n",
316 					data.inject.address);
317 			break;
318 		}
319 
320 		/* data.inject.address is offset instead of absolute gpu address */
321 		ret = amdgpu_ras_error_inject(adev, &data.inject);
322 		break;
323 	default:
324 		ret = -EINVAL;
325 		break;
326 	}
327 
328 	if (ret)
329 		return -EINVAL;
330 
331 	return size;
332 }
333 
334 /**
335  * DOC: AMDGPU RAS debugfs EEPROM table reset interface
336  *
337  * Some boards contain an EEPROM which is used to persistently store a list of
338  * bad pages which experiences ECC errors in vram.  This interface provides
339  * a way to reset the EEPROM, e.g., after testing error injection.
340  *
341  * Usage:
342  *
343  * .. code-block:: bash
344  *
345  *	echo 1 > ../ras/ras_eeprom_reset
346  *
347  * will reset EEPROM table to 0 entries.
348  *
349  */
350 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, const char __user *buf,
351 		size_t size, loff_t *pos)
352 {
353 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
354 	int ret;
355 
356 	ret = amdgpu_ras_eeprom_reset_table(&adev->psp.ras.ras->eeprom_control);
357 
358 	return ret == 1 ? size : -EIO;
359 }
360 
361 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
362 	.owner = THIS_MODULE,
363 	.read = NULL,
364 	.write = amdgpu_ras_debugfs_ctrl_write,
365 	.llseek = default_llseek
366 };
367 
368 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
369 	.owner = THIS_MODULE,
370 	.read = NULL,
371 	.write = amdgpu_ras_debugfs_eeprom_write,
372 	.llseek = default_llseek
373 };
374 
375 /**
376  * DOC: AMDGPU RAS sysfs Error Count Interface
377  *
378  * It allows the user to read the error count for each IP block on the gpu through
379  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
380  *
381  * It outputs the multiple lines which report the uncorrected (ue) and corrected
382  * (ce) error counts.
383  *
384  * The format of one line is below,
385  *
386  * [ce|ue]: count
387  *
388  * Example:
389  *
390  * .. code-block:: bash
391  *
392  *	ue: 0
393  *	ce: 1
394  *
395  */
396 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
397 		struct device_attribute *attr, char *buf)
398 {
399 	struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
400 	struct ras_query_if info = {
401 		.head = obj->head,
402 	};
403 
404 	if (amdgpu_ras_intr_triggered())
405 		return snprintf(buf, PAGE_SIZE,
406 				"Query currently inaccessible\n");
407 
408 	if (amdgpu_ras_error_query(obj->adev, &info))
409 		return -EINVAL;
410 
411 	return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
412 			"ue", info.ue_count,
413 			"ce", info.ce_count);
414 }
415 
416 #endif /* __linux__ */
417 
418 /* obj begin */
419 
420 #define get_obj(obj) do { (obj)->use++; } while (0)
421 #define alive_obj(obj) ((obj)->use)
422 
423 static inline void put_obj(struct ras_manager *obj)
424 {
425 	if (obj && --obj->use == 0)
426 		list_del(&obj->node);
427 	if (obj && obj->use < 0) {
428 		 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
429 	}
430 }
431 
432 /* make one obj and return it. */
433 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
434 		struct ras_common_if *head)
435 {
436 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
437 	struct ras_manager *obj;
438 
439 	if (!con)
440 		return NULL;
441 
442 	if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
443 		return NULL;
444 
445 	obj = &con->objs[head->block];
446 	/* already exist. return obj? */
447 	if (alive_obj(obj))
448 		return NULL;
449 
450 	obj->head = *head;
451 	obj->adev = adev;
452 	list_add(&obj->node, &con->head);
453 	get_obj(obj);
454 
455 	return obj;
456 }
457 
458 /* return an obj equal to head, or the first when head is NULL */
459 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
460 		struct ras_common_if *head)
461 {
462 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
463 	struct ras_manager *obj;
464 	int i;
465 
466 	if (!con)
467 		return NULL;
468 
469 	if (head) {
470 		if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
471 			return NULL;
472 
473 		obj = &con->objs[head->block];
474 
475 		if (alive_obj(obj)) {
476 			WARN_ON(head->block != obj->head.block);
477 			return obj;
478 		}
479 	} else {
480 		for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
481 			obj = &con->objs[i];
482 			if (alive_obj(obj)) {
483 				WARN_ON(i != obj->head.block);
484 				return obj;
485 			}
486 		}
487 	}
488 
489 	return NULL;
490 }
491 /* obj end */
492 
493 /* feature ctl begin */
494 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
495 		struct ras_common_if *head)
496 {
497 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
498 
499 	return con->hw_supported & BIT(head->block);
500 }
501 
502 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
503 		struct ras_common_if *head)
504 {
505 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
506 
507 	return con->features & BIT(head->block);
508 }
509 
510 /*
511  * if obj is not created, then create one.
512  * set feature enable flag.
513  */
514 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
515 		struct ras_common_if *head, int enable)
516 {
517 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
518 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
519 
520 	/* If hardware does not support ras, then do not create obj.
521 	 * But if hardware support ras, we can create the obj.
522 	 * Ras framework checks con->hw_supported to see if it need do
523 	 * corresponding initialization.
524 	 * IP checks con->support to see if it need disable ras.
525 	 */
526 	if (!amdgpu_ras_is_feature_allowed(adev, head))
527 		return 0;
528 	if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
529 		return 0;
530 
531 	if (enable) {
532 		if (!obj) {
533 			obj = amdgpu_ras_create_obj(adev, head);
534 			if (!obj)
535 				return -EINVAL;
536 		} else {
537 			/* In case we create obj somewhere else */
538 			get_obj(obj);
539 		}
540 		con->features |= BIT(head->block);
541 	} else {
542 		if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
543 			con->features &= ~BIT(head->block);
544 			put_obj(obj);
545 		}
546 	}
547 
548 	return 0;
549 }
550 
551 /* wrapper of psp_ras_enable_features */
552 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
553 		struct ras_common_if *head, bool enable)
554 {
555 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
556 	union ta_ras_cmd_input info;
557 	int ret;
558 
559 	if (!con)
560 		return -EINVAL;
561 
562 	if (!enable) {
563 		info.disable_features = (struct ta_ras_disable_features_input) {
564 			.block_id =  amdgpu_ras_block_to_ta(head->block),
565 			.error_type = amdgpu_ras_error_to_ta(head->type),
566 		};
567 	} else {
568 		info.enable_features = (struct ta_ras_enable_features_input) {
569 			.block_id =  amdgpu_ras_block_to_ta(head->block),
570 			.error_type = amdgpu_ras_error_to_ta(head->type),
571 		};
572 	}
573 
574 	/* Do not enable if it is not allowed. */
575 	WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
576 	/* Are we alerady in that state we are going to set? */
577 	if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
578 		return 0;
579 
580 	if (!amdgpu_ras_intr_triggered()) {
581 		ret = psp_ras_enable_features(&adev->psp, &info, enable);
582 		if (ret) {
583 			DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
584 					enable ? "enable":"disable",
585 					ras_block_str(head->block),
586 					ret);
587 			if (ret == TA_RAS_STATUS__RESET_NEEDED)
588 				return -EAGAIN;
589 			return -EINVAL;
590 		}
591 	}
592 
593 	/* setup the obj */
594 	__amdgpu_ras_feature_enable(adev, head, enable);
595 
596 	return 0;
597 }
598 
599 /* Only used in device probe stage and called only once. */
600 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
601 		struct ras_common_if *head, bool enable)
602 {
603 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
604 	int ret;
605 
606 	if (!con)
607 		return -EINVAL;
608 
609 	if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
610 		if (enable) {
611 			/* There is no harm to issue a ras TA cmd regardless of
612 			 * the currecnt ras state.
613 			 * If current state == target state, it will do nothing
614 			 * But sometimes it requests driver to reset and repost
615 			 * with error code -EAGAIN.
616 			 */
617 			ret = amdgpu_ras_feature_enable(adev, head, 1);
618 			/* With old ras TA, we might fail to enable ras.
619 			 * Log it and just setup the object.
620 			 * TODO need remove this WA in the future.
621 			 */
622 			if (ret == -EINVAL) {
623 				ret = __amdgpu_ras_feature_enable(adev, head, 1);
624 				if (!ret)
625 					DRM_INFO("RAS INFO: %s setup object\n",
626 						ras_block_str(head->block));
627 			}
628 		} else {
629 			/* setup the object then issue a ras TA disable cmd.*/
630 			ret = __amdgpu_ras_feature_enable(adev, head, 1);
631 			if (ret)
632 				return ret;
633 
634 			ret = amdgpu_ras_feature_enable(adev, head, 0);
635 		}
636 	} else
637 		ret = amdgpu_ras_feature_enable(adev, head, enable);
638 
639 	return ret;
640 }
641 
642 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
643 		bool bypass)
644 {
645 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
646 	struct ras_manager *obj, *tmp;
647 
648 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
649 		/* bypass psp.
650 		 * aka just release the obj and corresponding flags
651 		 */
652 		if (bypass) {
653 			if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
654 				break;
655 		} else {
656 			if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
657 				break;
658 		}
659 	}
660 
661 	return con->features;
662 }
663 
664 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
665 		bool bypass)
666 {
667 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
668 	int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
669 	int i;
670 	const enum amdgpu_ras_error_type default_ras_type =
671 		AMDGPU_RAS_ERROR__NONE;
672 
673 	for (i = 0; i < ras_block_count; i++) {
674 		struct ras_common_if head = {
675 			.block = i,
676 			.type = default_ras_type,
677 			.sub_block_index = 0,
678 		};
679 		strlcpy(head.name, ras_block_str(i), sizeof(head.name));
680 		if (bypass) {
681 			/*
682 			 * bypass psp. vbios enable ras for us.
683 			 * so just create the obj
684 			 */
685 			if (__amdgpu_ras_feature_enable(adev, &head, 1))
686 				break;
687 		} else {
688 			if (amdgpu_ras_feature_enable(adev, &head, 1))
689 				break;
690 		}
691 	}
692 
693 	return con->features;
694 }
695 /* feature ctl end */
696 
697 /* query/inject/cure begin */
698 int amdgpu_ras_error_query(struct amdgpu_device *adev,
699 		struct ras_query_if *info)
700 {
701 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
702 	struct ras_err_data err_data = {0, 0, 0, NULL};
703 	int i;
704 
705 	if (!obj)
706 		return -EINVAL;
707 
708 	switch (info->head.block) {
709 	case AMDGPU_RAS_BLOCK__UMC:
710 		if (adev->umc.funcs->query_ras_error_count)
711 			adev->umc.funcs->query_ras_error_count(adev, &err_data);
712 		/* umc query_ras_error_address is also responsible for clearing
713 		 * error status
714 		 */
715 		if (adev->umc.funcs->query_ras_error_address)
716 			adev->umc.funcs->query_ras_error_address(adev, &err_data);
717 		break;
718 	case AMDGPU_RAS_BLOCK__SDMA:
719 		if (adev->sdma.funcs->query_ras_error_count) {
720 			for (i = 0; i < adev->sdma.num_instances; i++)
721 				adev->sdma.funcs->query_ras_error_count(adev, i,
722 									&err_data);
723 		}
724 		break;
725 	case AMDGPU_RAS_BLOCK__GFX:
726 		if (adev->gfx.funcs->query_ras_error_count)
727 			adev->gfx.funcs->query_ras_error_count(adev, &err_data);
728 		break;
729 	case AMDGPU_RAS_BLOCK__MMHUB:
730 		if (adev->mmhub.funcs->query_ras_error_count)
731 			adev->mmhub.funcs->query_ras_error_count(adev, &err_data);
732 		break;
733 	case AMDGPU_RAS_BLOCK__PCIE_BIF:
734 		if (adev->nbio.funcs->query_ras_error_count)
735 			adev->nbio.funcs->query_ras_error_count(adev, &err_data);
736 		break;
737 	case AMDGPU_RAS_BLOCK__XGMI_WAFL:
738 		amdgpu_xgmi_query_ras_error_count(adev, &err_data);
739 		break;
740 	default:
741 		break;
742 	}
743 
744 	obj->err_data.ue_count += err_data.ue_count;
745 	obj->err_data.ce_count += err_data.ce_count;
746 
747 	info->ue_count = obj->err_data.ue_count;
748 	info->ce_count = obj->err_data.ce_count;
749 
750 	if (err_data.ce_count) {
751 		dev_info(adev->dev, "%ld correctable errors detected in %s block\n",
752 			 obj->err_data.ce_count, ras_block_str(info->head.block));
753 	}
754 	if (err_data.ue_count) {
755 		dev_info(adev->dev, "%ld uncorrectable errors detected in %s block\n",
756 			 obj->err_data.ue_count, ras_block_str(info->head.block));
757 	}
758 
759 	return 0;
760 }
761 
762 /* wrapper of psp_ras_trigger_error */
763 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
764 		struct ras_inject_if *info)
765 {
766 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
767 	struct ta_ras_trigger_error_input block_info = {
768 		.block_id =  amdgpu_ras_block_to_ta(info->head.block),
769 		.inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
770 		.sub_block_index = info->head.sub_block_index,
771 		.address = info->address,
772 		.value = info->value,
773 	};
774 	int ret = 0;
775 
776 	if (!obj)
777 		return -EINVAL;
778 
779 	/* Calculate XGMI relative offset */
780 	if (adev->gmc.xgmi.num_physical_nodes > 1) {
781 		block_info.address =
782 			amdgpu_xgmi_get_relative_phy_addr(adev,
783 							  block_info.address);
784 	}
785 
786 	switch (info->head.block) {
787 	case AMDGPU_RAS_BLOCK__GFX:
788 		if (adev->gfx.funcs->ras_error_inject)
789 			ret = adev->gfx.funcs->ras_error_inject(adev, info);
790 		else
791 			ret = -EINVAL;
792 		break;
793 	case AMDGPU_RAS_BLOCK__UMC:
794 	case AMDGPU_RAS_BLOCK__MMHUB:
795 	case AMDGPU_RAS_BLOCK__XGMI_WAFL:
796 	case AMDGPU_RAS_BLOCK__PCIE_BIF:
797 		ret = psp_ras_trigger_error(&adev->psp, &block_info);
798 		break;
799 	default:
800 		DRM_INFO("%s error injection is not supported yet\n",
801 			 ras_block_str(info->head.block));
802 		ret = -EINVAL;
803 	}
804 
805 	if (ret)
806 		DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
807 				ras_block_str(info->head.block),
808 				ret);
809 
810 	return ret;
811 }
812 
813 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
814 		struct ras_cure_if *info)
815 {
816 	/* psp fw has no cure interface for now. */
817 	return 0;
818 }
819 
820 /* get the total error counts on all IPs */
821 unsigned long amdgpu_ras_query_error_count(struct amdgpu_device *adev,
822 		bool is_ce)
823 {
824 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
825 	struct ras_manager *obj;
826 	struct ras_err_data data = {0, 0};
827 
828 	if (!con)
829 		return 0;
830 
831 	list_for_each_entry(obj, &con->head, node) {
832 		struct ras_query_if info = {
833 			.head = obj->head,
834 		};
835 
836 		if (amdgpu_ras_error_query(adev, &info))
837 			return 0;
838 
839 		data.ce_count += info.ce_count;
840 		data.ue_count += info.ue_count;
841 	}
842 
843 	return is_ce ? data.ce_count : data.ue_count;
844 }
845 /* query/inject/cure end */
846 
847 #ifdef __linux__
848 
849 /* sysfs begin */
850 
851 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
852 		struct ras_badpage **bps, unsigned int *count);
853 
854 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
855 {
856 	switch (flags) {
857 	case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
858 		return "R";
859 	case AMDGPU_RAS_RETIRE_PAGE_PENDING:
860 		return "P";
861 	case AMDGPU_RAS_RETIRE_PAGE_FAULT:
862 	default:
863 		return "F";
864 	};
865 }
866 
867 /**
868  * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
869  *
870  * It allows user to read the bad pages of vram on the gpu through
871  * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
872  *
873  * It outputs multiple lines, and each line stands for one gpu page.
874  *
875  * The format of one line is below,
876  * gpu pfn : gpu page size : flags
877  *
878  * gpu pfn and gpu page size are printed in hex format.
879  * flags can be one of below character,
880  *
881  * R: reserved, this gpu page is reserved and not able to use.
882  *
883  * P: pending for reserve, this gpu page is marked as bad, will be reserved
884  * in next window of page_reserve.
885  *
886  * F: unable to reserve. this gpu page can't be reserved due to some reasons.
887  *
888  * Examples:
889  *
890  * .. code-block:: bash
891  *
892  *	0x00000001 : 0x00001000 : R
893  *	0x00000002 : 0x00001000 : P
894  *
895  */
896 
897 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
898 		struct kobject *kobj, struct bin_attribute *attr,
899 		char *buf, loff_t ppos, size_t count)
900 {
901 	struct amdgpu_ras *con =
902 		container_of(attr, struct amdgpu_ras, badpages_attr);
903 	struct amdgpu_device *adev = con->adev;
904 	const unsigned int element_size =
905 		sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
906 	unsigned int start = div64_ul(ppos + element_size - 1, element_size);
907 	unsigned int end = div64_ul(ppos + count - 1, element_size);
908 	ssize_t s = 0;
909 	struct ras_badpage *bps = NULL;
910 	unsigned int bps_count = 0;
911 
912 	memset(buf, 0, count);
913 
914 	if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
915 		return 0;
916 
917 	for (; start < end && start < bps_count; start++)
918 		s += scnprintf(&buf[s], element_size + 1,
919 				"0x%08x : 0x%08x : %1s\n",
920 				bps[start].bp,
921 				bps[start].size,
922 				amdgpu_ras_badpage_flags_str(bps[start].flags));
923 
924 	kfree(bps);
925 
926 	return s;
927 }
928 
929 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
930 		struct device_attribute *attr, char *buf)
931 {
932 	struct amdgpu_ras *con =
933 		container_of(attr, struct amdgpu_ras, features_attr);
934 
935 	return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
936 }
937 
938 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
939 {
940 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
941 	struct attribute *attrs[] = {
942 		&con->features_attr.attr,
943 		NULL
944 	};
945 	struct bin_attribute *bin_attrs[] = {
946 		&con->badpages_attr,
947 		NULL
948 	};
949 	struct attribute_group group = {
950 		.name = "ras",
951 		.attrs = attrs,
952 		.bin_attrs = bin_attrs,
953 	};
954 
955 	con->features_attr = (struct device_attribute) {
956 		.attr = {
957 			.name = "features",
958 			.mode = S_IRUGO,
959 		},
960 			.show = amdgpu_ras_sysfs_features_read,
961 	};
962 
963 	con->badpages_attr = (struct bin_attribute) {
964 		.attr = {
965 			.name = "gpu_vram_bad_pages",
966 			.mode = S_IRUGO,
967 		},
968 		.size = 0,
969 		.private = NULL,
970 		.read = amdgpu_ras_sysfs_badpages_read,
971 	};
972 
973 	sysfs_attr_init(attrs[0]);
974 	sysfs_bin_attr_init(bin_attrs[0]);
975 
976 	return sysfs_create_group(&adev->dev->kobj, &group);
977 }
978 
979 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
980 {
981 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
982 	struct attribute *attrs[] = {
983 		&con->features_attr.attr,
984 		NULL
985 	};
986 	struct bin_attribute *bin_attrs[] = {
987 		&con->badpages_attr,
988 		NULL
989 	};
990 	struct attribute_group group = {
991 		.name = "ras",
992 		.attrs = attrs,
993 		.bin_attrs = bin_attrs,
994 	};
995 
996 	sysfs_remove_group(&adev->dev->kobj, &group);
997 
998 	return 0;
999 }
1000 
1001 #endif /* __linux__ */
1002 
1003 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1004 		struct ras_fs_if *head)
1005 {
1006 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1007 
1008 	if (!obj || obj->attr_inuse)
1009 		return -EINVAL;
1010 
1011 	STUB();
1012 	return -ENOSYS;
1013 #ifdef notyet
1014 	get_obj(obj);
1015 
1016 	memcpy(obj->fs_data.sysfs_name,
1017 			head->sysfs_name,
1018 			sizeof(obj->fs_data.sysfs_name));
1019 
1020 	obj->sysfs_attr = (struct device_attribute){
1021 		.attr = {
1022 			.name = obj->fs_data.sysfs_name,
1023 			.mode = S_IRUGO,
1024 		},
1025 			.show = amdgpu_ras_sysfs_read,
1026 	};
1027 	sysfs_attr_init(&obj->sysfs_attr.attr);
1028 
1029 	if (sysfs_add_file_to_group(&adev->dev->kobj,
1030 				&obj->sysfs_attr.attr,
1031 				"ras")) {
1032 		put_obj(obj);
1033 		return -EINVAL;
1034 	}
1035 
1036 	obj->attr_inuse = 1;
1037 
1038 	return 0;
1039 #endif
1040 }
1041 
1042 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1043 		struct ras_common_if *head)
1044 {
1045 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1046 
1047 	if (!obj || !obj->attr_inuse)
1048 		return -EINVAL;
1049 
1050 	sysfs_remove_file_from_group(&adev->dev->kobj,
1051 				&obj->sysfs_attr.attr,
1052 				"ras");
1053 	obj->attr_inuse = 0;
1054 	put_obj(obj);
1055 
1056 	return 0;
1057 }
1058 
1059 #ifdef __linux__
1060 
1061 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1062 {
1063 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1064 	struct ras_manager *obj, *tmp;
1065 
1066 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
1067 		amdgpu_ras_sysfs_remove(adev, &obj->head);
1068 	}
1069 
1070 	amdgpu_ras_sysfs_remove_feature_node(adev);
1071 
1072 	return 0;
1073 }
1074 /* sysfs end */
1075 
1076 /**
1077  * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1078  *
1079  * Normally when there is an uncorrectable error, the driver will reset
1080  * the GPU to recover.  However, in the event of an unrecoverable error,
1081  * the driver provides an interface to reboot the system automatically
1082  * in that event.
1083  *
1084  * The following file in debugfs provides that interface:
1085  * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1086  *
1087  * Usage:
1088  *
1089  * .. code-block:: bash
1090  *
1091  *	echo true > .../ras/auto_reboot
1092  *
1093  */
1094 /* debugfs begin */
1095 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1096 {
1097 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1098 	struct drm_minor *minor = adev->ddev->primary;
1099 
1100 	con->dir = debugfs_create_dir("ras", minor->debugfs_root);
1101 	debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
1102 				adev, &amdgpu_ras_debugfs_ctrl_ops);
1103 	debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, con->dir,
1104 				adev, &amdgpu_ras_debugfs_eeprom_ops);
1105 
1106 	/*
1107 	 * After one uncorrectable error happens, usually GPU recovery will
1108 	 * be scheduled. But due to the known problem in GPU recovery failing
1109 	 * to bring GPU back, below interface provides one direct way to
1110 	 * user to reboot system automatically in such case within
1111 	 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1112 	 * will never be called.
1113 	 */
1114 	debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, con->dir,
1115 				&con->reboot);
1116 }
1117 
1118 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1119 		struct ras_fs_if *head)
1120 {
1121 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1122 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1123 
1124 	if (!obj || obj->ent)
1125 		return;
1126 
1127 	get_obj(obj);
1128 
1129 	memcpy(obj->fs_data.debugfs_name,
1130 			head->debugfs_name,
1131 			sizeof(obj->fs_data.debugfs_name));
1132 
1133 	obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
1134 				       S_IWUGO | S_IRUGO, con->dir, obj,
1135 				       &amdgpu_ras_debugfs_ops);
1136 }
1137 
1138 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1139 {
1140 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1141 	struct ras_manager *obj;
1142 	struct ras_fs_if fs_info;
1143 
1144 	/*
1145 	 * it won't be called in resume path, no need to check
1146 	 * suspend and gpu reset status
1147 	 */
1148 	if (!con)
1149 		return;
1150 
1151 	amdgpu_ras_debugfs_create_ctrl_node(adev);
1152 
1153 	list_for_each_entry(obj, &con->head, node) {
1154 		if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1155 			(obj->attr_inuse == 1)) {
1156 			sprintf(fs_info.debugfs_name, "%s_err_inject",
1157 					ras_block_str(obj->head.block));
1158 			fs_info.head = obj->head;
1159 			amdgpu_ras_debugfs_create(adev, &fs_info);
1160 		}
1161 	}
1162 }
1163 
1164 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1165 		struct ras_common_if *head)
1166 {
1167 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1168 
1169 	if (!obj || !obj->ent)
1170 		return;
1171 
1172 	debugfs_remove(obj->ent);
1173 	obj->ent = NULL;
1174 	put_obj(obj);
1175 }
1176 
1177 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1178 {
1179 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1180 	struct ras_manager *obj, *tmp;
1181 
1182 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
1183 		amdgpu_ras_debugfs_remove(adev, &obj->head);
1184 	}
1185 
1186 	debugfs_remove_recursive(con->dir);
1187 	con->dir = NULL;
1188 }
1189 /* debugfs end */
1190 
1191 #endif /* __linux__ */
1192 
1193 /* ras fs */
1194 
1195 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1196 {
1197 #ifdef __linux__
1198 	amdgpu_ras_sysfs_create_feature_node(adev);
1199 #endif
1200 
1201 	return 0;
1202 }
1203 
1204 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1205 {
1206 #ifdef __linux__
1207 	amdgpu_ras_debugfs_remove_all(adev);
1208 	amdgpu_ras_sysfs_remove_all(adev);
1209 #endif
1210 	return 0;
1211 }
1212 /* ras fs end */
1213 
1214 /* ih begin */
1215 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1216 {
1217 	struct ras_ih_data *data = &obj->ih_data;
1218 	struct amdgpu_iv_entry entry;
1219 	int ret;
1220 	struct ras_err_data err_data = {0, 0, 0, NULL};
1221 
1222 	while (data->rptr != data->wptr) {
1223 		rmb();
1224 		memcpy(&entry, &data->ring[data->rptr],
1225 				data->element_size);
1226 
1227 		wmb();
1228 		data->rptr = (data->aligned_element_size +
1229 				data->rptr) % data->ring_size;
1230 
1231 		/* Let IP handle its data, maybe we need get the output
1232 		 * from the callback to udpate the error type/count, etc
1233 		 */
1234 		if (data->cb) {
1235 			ret = data->cb(obj->adev, &err_data, &entry);
1236 			/* ue will trigger an interrupt, and in that case
1237 			 * we need do a reset to recovery the whole system.
1238 			 * But leave IP do that recovery, here we just dispatch
1239 			 * the error.
1240 			 */
1241 			if (ret == AMDGPU_RAS_SUCCESS) {
1242 				/* these counts could be left as 0 if
1243 				 * some blocks do not count error number
1244 				 */
1245 				obj->err_data.ue_count += err_data.ue_count;
1246 				obj->err_data.ce_count += err_data.ce_count;
1247 			}
1248 		}
1249 	}
1250 }
1251 
1252 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1253 {
1254 	struct ras_ih_data *data =
1255 		container_of(work, struct ras_ih_data, ih_work);
1256 	struct ras_manager *obj =
1257 		container_of(data, struct ras_manager, ih_data);
1258 
1259 	amdgpu_ras_interrupt_handler(obj);
1260 }
1261 
1262 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1263 		struct ras_dispatch_if *info)
1264 {
1265 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1266 	struct ras_ih_data *data = &obj->ih_data;
1267 
1268 	if (!obj)
1269 		return -EINVAL;
1270 
1271 	if (data->inuse == 0)
1272 		return 0;
1273 
1274 	/* Might be overflow... */
1275 	memcpy(&data->ring[data->wptr], info->entry,
1276 			data->element_size);
1277 
1278 	wmb();
1279 	data->wptr = (data->aligned_element_size +
1280 			data->wptr) % data->ring_size;
1281 
1282 	schedule_work(&data->ih_work);
1283 
1284 	return 0;
1285 }
1286 
1287 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1288 		struct ras_ih_if *info)
1289 {
1290 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1291 	struct ras_ih_data *data;
1292 
1293 	if (!obj)
1294 		return -EINVAL;
1295 
1296 	data = &obj->ih_data;
1297 	if (data->inuse == 0)
1298 		return 0;
1299 
1300 	cancel_work_sync(&data->ih_work);
1301 
1302 	kfree(data->ring);
1303 	memset(data, 0, sizeof(*data));
1304 	put_obj(obj);
1305 
1306 	return 0;
1307 }
1308 
1309 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1310 		struct ras_ih_if *info)
1311 {
1312 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1313 	struct ras_ih_data *data;
1314 
1315 	if (!obj) {
1316 		/* in case we registe the IH before enable ras feature */
1317 		obj = amdgpu_ras_create_obj(adev, &info->head);
1318 		if (!obj)
1319 			return -EINVAL;
1320 	} else
1321 		get_obj(obj);
1322 
1323 	data = &obj->ih_data;
1324 	/* add the callback.etc */
1325 	*data = (struct ras_ih_data) {
1326 		.inuse = 0,
1327 		.cb = info->cb,
1328 		.element_size = sizeof(struct amdgpu_iv_entry),
1329 		.rptr = 0,
1330 		.wptr = 0,
1331 	};
1332 
1333 	INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1334 
1335 	data->aligned_element_size = roundup2(data->element_size, 8);
1336 	/* the ring can store 64 iv entries. */
1337 	data->ring_size = 64 * data->aligned_element_size;
1338 	data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1339 	if (!data->ring) {
1340 		put_obj(obj);
1341 		return -ENOMEM;
1342 	}
1343 
1344 	/* IH is ready */
1345 	data->inuse = 1;
1346 
1347 	return 0;
1348 }
1349 
1350 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1351 {
1352 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1353 	struct ras_manager *obj, *tmp;
1354 
1355 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
1356 		struct ras_ih_if info = {
1357 			.head = obj->head,
1358 		};
1359 		amdgpu_ras_interrupt_remove_handler(adev, &info);
1360 	}
1361 
1362 	return 0;
1363 }
1364 /* ih end */
1365 
1366 /* traversal all IPs except NBIO to query error counter */
1367 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1368 {
1369 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1370 	struct ras_manager *obj;
1371 
1372 	if (!con)
1373 		return;
1374 
1375 	list_for_each_entry(obj, &con->head, node) {
1376 		struct ras_query_if info = {
1377 			.head = obj->head,
1378 		};
1379 
1380 		/*
1381 		 * PCIE_BIF IP has one different isr by ras controller
1382 		 * interrupt, the specific ras counter query will be
1383 		 * done in that isr. So skip such block from common
1384 		 * sync flood interrupt isr calling.
1385 		 */
1386 		if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1387 			continue;
1388 
1389 		amdgpu_ras_error_query(adev, &info);
1390 	}
1391 }
1392 
1393 /* recovery begin */
1394 
1395 /* return 0 on success.
1396  * caller need free bps.
1397  */
1398 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1399 		struct ras_badpage **bps, unsigned int *count)
1400 {
1401 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1402 	struct ras_err_handler_data *data;
1403 	int i = 0;
1404 	int ret = 0;
1405 
1406 	if (!con || !con->eh_data || !bps || !count)
1407 		return -EINVAL;
1408 
1409 	mutex_lock(&con->recovery_lock);
1410 	data = con->eh_data;
1411 	if (!data || data->count == 0) {
1412 		*bps = NULL;
1413 		ret = -EINVAL;
1414 		goto out;
1415 	}
1416 
1417 	*bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1418 	if (!*bps) {
1419 		ret = -ENOMEM;
1420 		goto out;
1421 	}
1422 
1423 	for (; i < data->count; i++) {
1424 		(*bps)[i] = (struct ras_badpage){
1425 			.bp = data->bps[i].retired_page,
1426 			.size = AMDGPU_GPU_PAGE_SIZE,
1427 			.flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1428 		};
1429 
1430 		if (data->last_reserved <= i)
1431 			(*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1432 		else if (data->bps_bo[i] == NULL)
1433 			(*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1434 	}
1435 
1436 	*count = data->count;
1437 out:
1438 	mutex_unlock(&con->recovery_lock);
1439 	return ret;
1440 }
1441 
1442 static void amdgpu_ras_do_recovery(struct work_struct *work)
1443 {
1444 	struct amdgpu_ras *ras =
1445 		container_of(work, struct amdgpu_ras, recovery_work);
1446 	struct amdgpu_device *remote_adev = NULL;
1447 	struct amdgpu_device *adev = ras->adev;
1448 	struct list_head device_list, *device_list_handle =  NULL;
1449 	struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev, false);
1450 
1451 	/* Build list of devices to query RAS related errors */
1452 	if  (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
1453 		device_list_handle = &hive->device_list;
1454 	} else {
1455 		list_add_tail(&adev->gmc.xgmi.head, &device_list);
1456 		device_list_handle = &device_list;
1457 	}
1458 
1459 	list_for_each_entry(remote_adev, device_list_handle, gmc.xgmi.head) {
1460 		amdgpu_ras_log_on_err_counter(remote_adev);
1461 	}
1462 
1463 	if (amdgpu_device_should_recover_gpu(ras->adev))
1464 		amdgpu_device_gpu_recover(ras->adev, 0);
1465 	atomic_set(&ras->in_recovery, 0);
1466 }
1467 
1468 /* alloc/realloc bps array */
1469 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1470 		struct ras_err_handler_data *data, int pages)
1471 {
1472 	unsigned int old_space = data->count + data->space_left;
1473 	unsigned int new_space = old_space + pages;
1474 	unsigned int align_space = roundup2(new_space, 512);
1475 	void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1476 	struct amdgpu_bo **bps_bo =
1477 			kmalloc(align_space * sizeof(*data->bps_bo), GFP_KERNEL);
1478 
1479 	if (!bps || !bps_bo) {
1480 		kfree(bps);
1481 		kfree(bps_bo);
1482 		return -ENOMEM;
1483 	}
1484 
1485 	if (data->bps) {
1486 		memcpy(bps, data->bps,
1487 				data->count * sizeof(*data->bps));
1488 		kfree(data->bps);
1489 	}
1490 	if (data->bps_bo) {
1491 		memcpy(bps_bo, data->bps_bo,
1492 				data->count * sizeof(*data->bps_bo));
1493 		kfree(data->bps_bo);
1494 	}
1495 
1496 	data->bps = bps;
1497 	data->bps_bo = bps_bo;
1498 	data->space_left += align_space - old_space;
1499 	return 0;
1500 }
1501 
1502 /* it deal with vram only. */
1503 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1504 		struct eeprom_table_record *bps, int pages)
1505 {
1506 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1507 	struct ras_err_handler_data *data;
1508 	int ret = 0;
1509 
1510 	if (!con || !con->eh_data || !bps || pages <= 0)
1511 		return 0;
1512 
1513 	mutex_lock(&con->recovery_lock);
1514 	data = con->eh_data;
1515 	if (!data)
1516 		goto out;
1517 
1518 	if (data->space_left <= pages)
1519 		if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1520 			ret = -ENOMEM;
1521 			goto out;
1522 		}
1523 
1524 	memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
1525 	data->count += pages;
1526 	data->space_left -= pages;
1527 
1528 out:
1529 	mutex_unlock(&con->recovery_lock);
1530 
1531 	return ret;
1532 }
1533 
1534 /*
1535  * write error record array to eeprom, the function should be
1536  * protected by recovery_lock
1537  */
1538 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1539 {
1540 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1541 	struct ras_err_handler_data *data;
1542 	struct amdgpu_ras_eeprom_control *control;
1543 	int save_count;
1544 
1545 	if (!con || !con->eh_data)
1546 		return 0;
1547 
1548 	control = &con->eeprom_control;
1549 	data = con->eh_data;
1550 	save_count = data->count - control->num_recs;
1551 	/* only new entries are saved */
1552 	if (save_count > 0)
1553 		if (amdgpu_ras_eeprom_process_recods(control,
1554 							&data->bps[control->num_recs],
1555 							true,
1556 							save_count)) {
1557 			DRM_ERROR("Failed to save EEPROM table data!");
1558 			return -EIO;
1559 		}
1560 
1561 	return 0;
1562 }
1563 
1564 /*
1565  * read error record array in eeprom and reserve enough space for
1566  * storing new bad pages
1567  */
1568 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1569 {
1570 	struct amdgpu_ras_eeprom_control *control =
1571 					&adev->psp.ras.ras->eeprom_control;
1572 	struct eeprom_table_record *bps = NULL;
1573 	int ret = 0;
1574 
1575 	/* no bad page record, skip eeprom access */
1576 	if (!control->num_recs)
1577 		return ret;
1578 
1579 	bps = kcalloc(control->num_recs, sizeof(*bps), GFP_KERNEL);
1580 	if (!bps)
1581 		return -ENOMEM;
1582 
1583 	if (amdgpu_ras_eeprom_process_recods(control, bps, false,
1584 		control->num_recs)) {
1585 		DRM_ERROR("Failed to load EEPROM table records!");
1586 		ret = -EIO;
1587 		goto out;
1588 	}
1589 
1590 	ret = amdgpu_ras_add_bad_pages(adev, bps, control->num_recs);
1591 
1592 out:
1593 	kfree(bps);
1594 	return ret;
1595 }
1596 
1597 /*
1598  * check if an address belongs to bad page
1599  *
1600  * Note: this check is only for umc block
1601  */
1602 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
1603 				uint64_t addr)
1604 {
1605 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1606 	struct ras_err_handler_data *data;
1607 	int i;
1608 	bool ret = false;
1609 
1610 	if (!con || !con->eh_data)
1611 		return ret;
1612 
1613 	mutex_lock(&con->recovery_lock);
1614 	data = con->eh_data;
1615 	if (!data)
1616 		goto out;
1617 
1618 	addr >>= AMDGPU_GPU_PAGE_SHIFT;
1619 	for (i = 0; i < data->count; i++)
1620 		if (addr == data->bps[i].retired_page) {
1621 			ret = true;
1622 			goto out;
1623 		}
1624 
1625 out:
1626 	mutex_unlock(&con->recovery_lock);
1627 	return ret;
1628 }
1629 
1630 /* called in gpu recovery/init */
1631 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1632 {
1633 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1634 	struct ras_err_handler_data *data;
1635 	uint64_t bp;
1636 	struct amdgpu_bo *bo = NULL;
1637 	int i, ret = 0;
1638 
1639 	if (!con || !con->eh_data)
1640 		return 0;
1641 
1642 	mutex_lock(&con->recovery_lock);
1643 	data = con->eh_data;
1644 	if (!data)
1645 		goto out;
1646 	/* reserve vram at driver post stage. */
1647 	for (i = data->last_reserved; i < data->count; i++) {
1648 		bp = data->bps[i].retired_page;
1649 
1650 		/* There are two cases of reserve error should be ignored:
1651 		 * 1) a ras bad page has been allocated (used by someone);
1652 		 * 2) a ras bad page has been reserved (duplicate error injection
1653 		 *    for one page);
1654 		 */
1655 		if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
1656 					       AMDGPU_GPU_PAGE_SIZE,
1657 					       AMDGPU_GEM_DOMAIN_VRAM,
1658 					       &bo, NULL))
1659 			DRM_WARN("RAS WARN: reserve vram for retired page %llx fail\n", bp);
1660 
1661 		data->bps_bo[i] = bo;
1662 		data->last_reserved = i + 1;
1663 		bo = NULL;
1664 	}
1665 
1666 	/* continue to save bad pages to eeprom even reesrve_vram fails */
1667 	ret = amdgpu_ras_save_bad_pages(adev);
1668 out:
1669 	mutex_unlock(&con->recovery_lock);
1670 	return ret;
1671 }
1672 
1673 /* called when driver unload */
1674 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1675 {
1676 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1677 	struct ras_err_handler_data *data;
1678 	struct amdgpu_bo *bo;
1679 	int i;
1680 
1681 	if (!con || !con->eh_data)
1682 		return 0;
1683 
1684 	mutex_lock(&con->recovery_lock);
1685 	data = con->eh_data;
1686 	if (!data)
1687 		goto out;
1688 
1689 	for (i = data->last_reserved - 1; i >= 0; i--) {
1690 		bo = data->bps_bo[i];
1691 
1692 		amdgpu_bo_free_kernel(&bo, NULL, NULL);
1693 
1694 		data->bps_bo[i] = bo;
1695 		data->last_reserved = i;
1696 	}
1697 out:
1698 	mutex_unlock(&con->recovery_lock);
1699 	return 0;
1700 }
1701 
1702 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1703 {
1704 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1705 	struct ras_err_handler_data **data;
1706 	int ret;
1707 
1708 	if (con)
1709 		data = &con->eh_data;
1710 	else
1711 		return 0;
1712 
1713 	*data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
1714 	if (!*data) {
1715 		ret = -ENOMEM;
1716 		goto out;
1717 	}
1718 
1719 	rw_init(&con->recovery_lock, "rasrec");
1720 	INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1721 	atomic_set(&con->in_recovery, 0);
1722 	con->adev = adev;
1723 
1724 	ret = amdgpu_ras_eeprom_init(&con->eeprom_control);
1725 	if (ret)
1726 		goto free;
1727 
1728 	if (con->eeprom_control.num_recs) {
1729 		ret = amdgpu_ras_load_bad_pages(adev);
1730 		if (ret)
1731 			goto free;
1732 		ret = amdgpu_ras_reserve_bad_pages(adev);
1733 		if (ret)
1734 			goto release;
1735 	}
1736 
1737 	return 0;
1738 
1739 release:
1740 	amdgpu_ras_release_bad_pages(adev);
1741 free:
1742 	kfree((*data)->bps);
1743 	kfree((*data)->bps_bo);
1744 	kfree(*data);
1745 	con->eh_data = NULL;
1746 out:
1747 	DRM_WARN("Failed to initialize ras recovery!\n");
1748 
1749 	return ret;
1750 }
1751 
1752 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1753 {
1754 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1755 	struct ras_err_handler_data *data = con->eh_data;
1756 
1757 	/* recovery_init failed to init it, fini is useless */
1758 	if (!data)
1759 		return 0;
1760 
1761 	cancel_work_sync(&con->recovery_work);
1762 	amdgpu_ras_release_bad_pages(adev);
1763 
1764 	mutex_lock(&con->recovery_lock);
1765 	con->eh_data = NULL;
1766 	kfree(data->bps);
1767 	kfree(data->bps_bo);
1768 	kfree(data);
1769 	mutex_unlock(&con->recovery_lock);
1770 
1771 	return 0;
1772 }
1773 /* recovery end */
1774 
1775 /* return 0 if ras will reset gpu and repost.*/
1776 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1777 		unsigned int block)
1778 {
1779 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1780 
1781 	if (!ras)
1782 		return -EINVAL;
1783 
1784 	ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1785 	return 0;
1786 }
1787 
1788 /*
1789  * check hardware's ras ability which will be saved in hw_supported.
1790  * if hardware does not support ras, we can skip some ras initializtion and
1791  * forbid some ras operations from IP.
1792  * if software itself, say boot parameter, limit the ras ability. We still
1793  * need allow IP do some limited operations, like disable. In such case,
1794  * we have to initialize ras as normal. but need check if operation is
1795  * allowed or not in each function.
1796  */
1797 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1798 		uint32_t *hw_supported, uint32_t *supported)
1799 {
1800 	*hw_supported = 0;
1801 	*supported = 0;
1802 
1803 	if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
1804 	    (adev->asic_type != CHIP_VEGA20 &&
1805 	     adev->asic_type != CHIP_ARCTURUS))
1806 		return;
1807 
1808 	if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
1809 		DRM_INFO("HBM ECC is active.\n");
1810 		*hw_supported |= (1 << AMDGPU_RAS_BLOCK__UMC |
1811 				1 << AMDGPU_RAS_BLOCK__DF);
1812 	} else
1813 		DRM_INFO("HBM ECC is not presented.\n");
1814 
1815 	if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
1816 		DRM_INFO("SRAM ECC is active.\n");
1817 		*hw_supported |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
1818 				1 << AMDGPU_RAS_BLOCK__DF);
1819 	} else
1820 		DRM_INFO("SRAM ECC is not presented.\n");
1821 
1822 	/* hw_supported needs to be aligned with RAS block mask. */
1823 	*hw_supported &= AMDGPU_RAS_BLOCK_MASK;
1824 
1825 	*supported = amdgpu_ras_enable == 0 ?
1826 			0 : *hw_supported & amdgpu_ras_mask;
1827 }
1828 
1829 int amdgpu_ras_init(struct amdgpu_device *adev)
1830 {
1831 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1832 	int r;
1833 
1834 	if (con)
1835 		return 0;
1836 
1837 	con = kmalloc(sizeof(struct amdgpu_ras) +
1838 			sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1839 			GFP_KERNEL|__GFP_ZERO);
1840 	if (!con)
1841 		return -ENOMEM;
1842 
1843 	con->objs = (struct ras_manager *)(con + 1);
1844 
1845 	amdgpu_ras_set_context(adev, con);
1846 
1847 	amdgpu_ras_check_supported(adev, &con->hw_supported,
1848 			&con->supported);
1849 	if (!con->hw_supported) {
1850 		amdgpu_ras_set_context(adev, NULL);
1851 		kfree(con);
1852 		return 0;
1853 	}
1854 
1855 	con->features = 0;
1856 	INIT_LIST_HEAD(&con->head);
1857 	/* Might need get this flag from vbios. */
1858 	con->flags = RAS_DEFAULT_FLAGS;
1859 
1860 	if (adev->nbio.funcs->init_ras_controller_interrupt) {
1861 		r = adev->nbio.funcs->init_ras_controller_interrupt(adev);
1862 		if (r)
1863 			return r;
1864 	}
1865 
1866 	if (adev->nbio.funcs->init_ras_err_event_athub_interrupt) {
1867 		r = adev->nbio.funcs->init_ras_err_event_athub_interrupt(adev);
1868 		if (r)
1869 			return r;
1870 	}
1871 
1872 	amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1873 
1874 	if (amdgpu_ras_fs_init(adev))
1875 		goto fs_out;
1876 
1877 	DRM_INFO("RAS INFO: ras initialized successfully, "
1878 			"hardware ability[%x] ras_mask[%x]\n",
1879 			con->hw_supported, con->supported);
1880 	return 0;
1881 fs_out:
1882 	amdgpu_ras_set_context(adev, NULL);
1883 	kfree(con);
1884 
1885 	return -EINVAL;
1886 }
1887 
1888 /* helper function to handle common stuff in ip late init phase */
1889 int amdgpu_ras_late_init(struct amdgpu_device *adev,
1890 			 struct ras_common_if *ras_block,
1891 			 struct ras_fs_if *fs_info,
1892 			 struct ras_ih_if *ih_info)
1893 {
1894 	int r;
1895 
1896 	/* disable RAS feature per IP block if it is not supported */
1897 	if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
1898 		amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
1899 		return 0;
1900 	}
1901 
1902 	r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
1903 	if (r) {
1904 		if (r == -EAGAIN) {
1905 			/* request gpu reset. will run again */
1906 			amdgpu_ras_request_reset_on_boot(adev,
1907 					ras_block->block);
1908 			return 0;
1909 		} else if (adev->in_suspend || adev->in_gpu_reset) {
1910 			/* in resume phase, if fail to enable ras,
1911 			 * clean up all ras fs nodes, and disable ras */
1912 			goto cleanup;
1913 		} else
1914 			return r;
1915 	}
1916 
1917 	/* in resume phase, no need to create ras fs node */
1918 	if (adev->in_suspend || adev->in_gpu_reset)
1919 		return 0;
1920 
1921 	if (ih_info->cb) {
1922 		r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
1923 		if (r)
1924 			goto interrupt;
1925 	}
1926 
1927 	r = amdgpu_ras_sysfs_create(adev, fs_info);
1928 	if (r)
1929 		goto sysfs;
1930 
1931 	return 0;
1932 cleanup:
1933 	amdgpu_ras_sysfs_remove(adev, ras_block);
1934 sysfs:
1935 	if (ih_info->cb)
1936 		amdgpu_ras_interrupt_remove_handler(adev, ih_info);
1937 interrupt:
1938 	amdgpu_ras_feature_enable(adev, ras_block, 0);
1939 	return r;
1940 }
1941 
1942 /* helper function to remove ras fs node and interrupt handler */
1943 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
1944 			  struct ras_common_if *ras_block,
1945 			  struct ras_ih_if *ih_info)
1946 {
1947 	if (!ras_block || !ih_info)
1948 		return;
1949 
1950 	amdgpu_ras_sysfs_remove(adev, ras_block);
1951 	if (ih_info->cb)
1952                 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
1953 	amdgpu_ras_feature_enable(adev, ras_block, 0);
1954 }
1955 
1956 /* do some init work after IP late init as dependence.
1957  * and it runs in resume/gpu reset/booting up cases.
1958  */
1959 void amdgpu_ras_resume(struct amdgpu_device *adev)
1960 {
1961 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1962 	struct ras_manager *obj, *tmp;
1963 
1964 	if (!con)
1965 		return;
1966 
1967 	if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1968 		/* Set up all other IPs which are not implemented. There is a
1969 		 * tricky thing that IP's actual ras error type should be
1970 		 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1971 		 * ERROR_NONE make sense anyway.
1972 		 */
1973 		amdgpu_ras_enable_all_features(adev, 1);
1974 
1975 		/* We enable ras on all hw_supported block, but as boot
1976 		 * parameter might disable some of them and one or more IP has
1977 		 * not implemented yet. So we disable them on behalf.
1978 		 */
1979 		list_for_each_entry_safe(obj, tmp, &con->head, node) {
1980 			if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1981 				amdgpu_ras_feature_enable(adev, &obj->head, 0);
1982 				/* there should be no any reference. */
1983 				WARN_ON(alive_obj(obj));
1984 			}
1985 		}
1986 	}
1987 
1988 	if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1989 		con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1990 		/* setup ras obj state as disabled.
1991 		 * for init_by_vbios case.
1992 		 * if we want to enable ras, just enable it in a normal way.
1993 		 * If we want do disable it, need setup ras obj as enabled,
1994 		 * then issue another TA disable cmd.
1995 		 * See feature_enable_on_boot
1996 		 */
1997 		amdgpu_ras_disable_all_features(adev, 1);
1998 		amdgpu_ras_reset_gpu(adev);
1999 	}
2000 }
2001 
2002 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2003 {
2004 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2005 
2006 	if (!con)
2007 		return;
2008 
2009 	amdgpu_ras_disable_all_features(adev, 0);
2010 	/* Make sure all ras objects are disabled. */
2011 	if (con->features)
2012 		amdgpu_ras_disable_all_features(adev, 1);
2013 }
2014 
2015 /* do some fini work before IP fini as dependence */
2016 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2017 {
2018 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2019 
2020 	if (!con)
2021 		return 0;
2022 
2023 	/* Need disable ras on all IPs here before ip [hw/sw]fini */
2024 	amdgpu_ras_disable_all_features(adev, 0);
2025 	amdgpu_ras_recovery_fini(adev);
2026 	return 0;
2027 }
2028 
2029 int amdgpu_ras_fini(struct amdgpu_device *adev)
2030 {
2031 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2032 
2033 	if (!con)
2034 		return 0;
2035 
2036 	amdgpu_ras_fs_fini(adev);
2037 	amdgpu_ras_interrupt_remove_all(adev);
2038 
2039 	WARN(con->features, "Feature mask is not cleared");
2040 
2041 	if (con->features)
2042 		amdgpu_ras_disable_all_features(adev, 1);
2043 
2044 	amdgpu_ras_set_context(adev, NULL);
2045 	kfree(con);
2046 
2047 	return 0;
2048 }
2049 
2050 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2051 {
2052 	uint32_t hw_supported, supported;
2053 
2054 	amdgpu_ras_check_supported(adev, &hw_supported, &supported);
2055 	if (!hw_supported)
2056 		return;
2057 
2058 	if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2059 		DRM_WARN("RAS event of type ERREVENT_ATHUB_INTERRUPT detected!\n");
2060 
2061 		amdgpu_ras_reset_gpu(adev);
2062 	}
2063 }
2064