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 #include <linux/pm_runtime.h>
31 
32 #include "amdgpu.h"
33 #include "amdgpu_ras.h"
34 #include "amdgpu_atomfirmware.h"
35 #include "amdgpu_xgmi.h"
36 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h"
37 #include "atom.h"
38 
39 static const char *RAS_FS_NAME = "ras";
40 
41 const char *ras_error_string[] = {
42 	"none",
43 	"parity",
44 	"single_correctable",
45 	"multi_uncorrectable",
46 	"poison",
47 };
48 
49 const char *ras_block_string[] = {
50 	"umc",
51 	"sdma",
52 	"gfx",
53 	"mmhub",
54 	"athub",
55 	"pcie_bif",
56 	"hdp",
57 	"xgmi_wafl",
58 	"df",
59 	"smn",
60 	"sem",
61 	"mp0",
62 	"mp1",
63 	"fuse",
64 	"mca",
65 };
66 
67 const char *ras_mca_block_string[] = {
68 	"mca_mp0",
69 	"mca_mp1",
70 	"mca_mpio",
71 	"mca_iohc",
72 };
73 
74 const char *get_ras_block_str(struct ras_common_if *ras_block)
75 {
76 	if (!ras_block)
77 		return "NULL";
78 
79 	if (ras_block->block >= AMDGPU_RAS_BLOCK_COUNT)
80 		return "OUT OF RANGE";
81 
82 	if (ras_block->block == AMDGPU_RAS_BLOCK__MCA)
83 		return ras_mca_block_string[ras_block->sub_block_index];
84 
85 	return ras_block_string[ras_block->block];
86 }
87 
88 #define ras_err_str(i) (ras_error_string[ffs(i)])
89 
90 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
91 
92 /* inject address is 52 bits */
93 #define	RAS_UMC_INJECT_ADDR_LIMIT	(0x1ULL << 52)
94 
95 /* typical ECC bad page rate is 1 bad page per 100MB VRAM */
96 #define RAS_BAD_PAGE_COVER              (100 * 1024 * 1024ULL)
97 
98 enum amdgpu_ras_retire_page_reservation {
99 	AMDGPU_RAS_RETIRE_PAGE_RESERVED,
100 	AMDGPU_RAS_RETIRE_PAGE_PENDING,
101 	AMDGPU_RAS_RETIRE_PAGE_FAULT,
102 };
103 
104 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
105 
106 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
107 				uint64_t addr);
108 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
109 				uint64_t addr);
110 
111 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
112 {
113 	if (adev && amdgpu_ras_get_context(adev))
114 		amdgpu_ras_get_context(adev)->error_query_ready = ready;
115 }
116 
117 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
118 {
119 	if (adev && amdgpu_ras_get_context(adev))
120 		return amdgpu_ras_get_context(adev)->error_query_ready;
121 
122 	return false;
123 }
124 
125 static int amdgpu_reserve_page_direct(struct amdgpu_device *adev, uint64_t address)
126 {
127 	struct ras_err_data err_data = {0, 0, 0, NULL};
128 	struct eeprom_table_record err_rec;
129 
130 	if ((address >= adev->gmc.mc_vram_size) ||
131 	    (address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
132 		dev_warn(adev->dev,
133 		         "RAS WARN: input address 0x%llx is invalid.\n",
134 		         address);
135 		return -EINVAL;
136 	}
137 
138 	if (amdgpu_ras_check_bad_page(adev, address)) {
139 		dev_warn(adev->dev,
140 			 "RAS WARN: 0x%llx has already been marked as bad page!\n",
141 			 address);
142 		return 0;
143 	}
144 
145 	memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
146 
147 	err_rec.address = address;
148 	err_rec.retired_page = address >> AMDGPU_GPU_PAGE_SHIFT;
149 	err_rec.ts = (uint64_t)ktime_get_real_seconds();
150 	err_rec.err_type = AMDGPU_RAS_EEPROM_ERR_NON_RECOVERABLE;
151 
152 	err_data.err_addr = &err_rec;
153 	err_data.err_addr_cnt = 1;
154 
155 	if (amdgpu_bad_page_threshold != 0) {
156 		amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
157 					 err_data.err_addr_cnt);
158 		amdgpu_ras_save_bad_pages(adev);
159 	}
160 
161 	dev_warn(adev->dev, "WARNING: THIS IS ONLY FOR TEST PURPOSES AND WILL CORRUPT RAS EEPROM\n");
162 	dev_warn(adev->dev, "Clear EEPROM:\n");
163 	dev_warn(adev->dev, "    echo 1 > /sys/kernel/debug/dri/0/ras/ras_eeprom_reset\n");
164 
165 	return 0;
166 }
167 
168 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
169 					size_t size, loff_t *pos)
170 {
171 	struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
172 	struct ras_query_if info = {
173 		.head = obj->head,
174 	};
175 	ssize_t s;
176 	char val[128];
177 
178 	if (amdgpu_ras_query_error_status(obj->adev, &info))
179 		return -EINVAL;
180 
181 	s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
182 			"ue", info.ue_count,
183 			"ce", info.ce_count);
184 	if (*pos >= s)
185 		return 0;
186 
187 	s -= *pos;
188 	s = min_t(u64, s, size);
189 
190 
191 	if (copy_to_user(buf, &val[*pos], s))
192 		return -EINVAL;
193 
194 	*pos += s;
195 
196 	return s;
197 }
198 
199 static const struct file_operations amdgpu_ras_debugfs_ops = {
200 	.owner = THIS_MODULE,
201 	.read = amdgpu_ras_debugfs_read,
202 	.write = NULL,
203 	.llseek = default_llseek
204 };
205 
206 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
207 {
208 	int i;
209 
210 	for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
211 		*block_id = i;
212 		if (strcmp(name, ras_block_string[i]) == 0)
213 			return 0;
214 	}
215 	return -EINVAL;
216 }
217 
218 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
219 		const char __user *buf, size_t size,
220 		loff_t *pos, struct ras_debug_if *data)
221 {
222 	ssize_t s = min_t(u64, 64, size);
223 	char str[65];
224 	char block_name[33];
225 	char err[9] = "ue";
226 	int op = -1;
227 	int block_id;
228 	uint32_t sub_block;
229 	u64 address, value;
230 
231 	if (*pos)
232 		return -EINVAL;
233 	*pos = size;
234 
235 	memset(str, 0, sizeof(str));
236 	memset(data, 0, sizeof(*data));
237 
238 	if (copy_from_user(str, buf, s))
239 		return -EINVAL;
240 
241 	if (sscanf(str, "disable %32s", block_name) == 1)
242 		op = 0;
243 	else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
244 		op = 1;
245 	else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
246 		op = 2;
247 	else if (strstr(str, "retire_page") != NULL)
248 		op = 3;
249 	else if (str[0] && str[1] && str[2] && str[3])
250 		/* ascii string, but commands are not matched. */
251 		return -EINVAL;
252 
253 	if (op != -1) {
254 		if (op == 3) {
255 			if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
256 			    sscanf(str, "%*s %llu", &address) != 1)
257 				return -EINVAL;
258 
259 			data->op = op;
260 			data->inject.address = address;
261 
262 			return 0;
263 		}
264 
265 		if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
266 			return -EINVAL;
267 
268 		data->head.block = block_id;
269 		/* only ue and ce errors are supported */
270 		if (!memcmp("ue", err, 2))
271 			data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
272 		else if (!memcmp("ce", err, 2))
273 			data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
274 		else
275 			return -EINVAL;
276 
277 		data->op = op;
278 
279 		if (op == 2) {
280 			if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
281 				   &sub_block, &address, &value) != 3 &&
282 			    sscanf(str, "%*s %*s %*s %u %llu %llu",
283 				   &sub_block, &address, &value) != 3)
284 				return -EINVAL;
285 			data->head.sub_block_index = sub_block;
286 			data->inject.address = address;
287 			data->inject.value = value;
288 		}
289 	} else {
290 		if (size < sizeof(*data))
291 			return -EINVAL;
292 
293 		if (copy_from_user(data, buf, sizeof(*data)))
294 			return -EINVAL;
295 	}
296 
297 	return 0;
298 }
299 
300 /**
301  * DOC: AMDGPU RAS debugfs control interface
302  *
303  * The control interface accepts struct ras_debug_if which has two members.
304  *
305  * First member: ras_debug_if::head or ras_debug_if::inject.
306  *
307  * head is used to indicate which IP block will be under control.
308  *
309  * head has four members, they are block, type, sub_block_index, name.
310  * block: which IP will be under control.
311  * type: what kind of error will be enabled/disabled/injected.
312  * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
313  * name: the name of IP.
314  *
315  * inject has two more members than head, they are address, value.
316  * As their names indicate, inject operation will write the
317  * value to the address.
318  *
319  * The second member: struct ras_debug_if::op.
320  * It has three kinds of operations.
321  *
322  * - 0: disable RAS on the block. Take ::head as its data.
323  * - 1: enable RAS on the block. Take ::head as its data.
324  * - 2: inject errors on the block. Take ::inject as its data.
325  *
326  * How to use the interface?
327  *
328  * In a program
329  *
330  * Copy the struct ras_debug_if in your code and initialize it.
331  * Write the struct to the control interface.
332  *
333  * From shell
334  *
335  * .. code-block:: bash
336  *
337  *	echo "disable <block>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
338  *	echo "enable  <block> <error>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
339  *	echo "inject  <block> <error> <sub-block> <address> <value> > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
340  *
341  * Where N, is the card which you want to affect.
342  *
343  * "disable" requires only the block.
344  * "enable" requires the block and error type.
345  * "inject" requires the block, error type, address, and value.
346  *
347  * The block is one of: umc, sdma, gfx, etc.
348  *	see ras_block_string[] for details
349  *
350  * The error type is one of: ue, ce, where,
351  *	ue is multi-uncorrectable
352  *	ce is single-correctable
353  *
354  * The sub-block is a the sub-block index, pass 0 if there is no sub-block.
355  * The address and value are hexadecimal numbers, leading 0x is optional.
356  *
357  * For instance,
358  *
359  * .. code-block:: bash
360  *
361  *	echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
362  *	echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
363  *	echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
364  *
365  * How to check the result of the operation?
366  *
367  * To check disable/enable, see "ras" features at,
368  * /sys/class/drm/card[0/1/2...]/device/ras/features
369  *
370  * To check inject, see the corresponding error count at,
371  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx|sdma|umc|...]_err_count
372  *
373  * .. note::
374  *	Operations are only allowed on blocks which are supported.
375  *	Check the "ras" mask at /sys/module/amdgpu/parameters/ras_mask
376  *	to see which blocks support RAS on a particular asic.
377  *
378  */
379 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f,
380 					     const char __user *buf,
381 					     size_t size, loff_t *pos)
382 {
383 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
384 	struct ras_debug_if data;
385 	int ret = 0;
386 
387 	if (!amdgpu_ras_get_error_query_ready(adev)) {
388 		dev_warn(adev->dev, "RAS WARN: error injection "
389 				"currently inaccessible\n");
390 		return size;
391 	}
392 
393 	ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
394 	if (ret)
395 		return ret;
396 
397 	if (data.op == 3) {
398 		ret = amdgpu_reserve_page_direct(adev, data.inject.address);
399 		if (!ret)
400 			return size;
401 		else
402 			return ret;
403 	}
404 
405 	if (!amdgpu_ras_is_supported(adev, data.head.block))
406 		return -EINVAL;
407 
408 	switch (data.op) {
409 	case 0:
410 		ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
411 		break;
412 	case 1:
413 		ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
414 		break;
415 	case 2:
416 		if ((data.inject.address >= adev->gmc.mc_vram_size) ||
417 		    (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
418 			dev_warn(adev->dev, "RAS WARN: input address "
419 					"0x%llx is invalid.",
420 					data.inject.address);
421 			ret = -EINVAL;
422 			break;
423 		}
424 
425 		/* umc ce/ue error injection for a bad page is not allowed */
426 		if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
427 		    amdgpu_ras_check_bad_page(adev, data.inject.address)) {
428 			dev_warn(adev->dev, "RAS WARN: inject: 0x%llx has "
429 				 "already been marked as bad!\n",
430 				 data.inject.address);
431 			break;
432 		}
433 
434 		/* data.inject.address is offset instead of absolute gpu address */
435 		ret = amdgpu_ras_error_inject(adev, &data.inject);
436 		break;
437 	default:
438 		ret = -EINVAL;
439 		break;
440 	}
441 
442 	if (ret)
443 		return -EINVAL;
444 
445 	return size;
446 }
447 
448 /**
449  * DOC: AMDGPU RAS debugfs EEPROM table reset interface
450  *
451  * Some boards contain an EEPROM which is used to persistently store a list of
452  * bad pages which experiences ECC errors in vram.  This interface provides
453  * a way to reset the EEPROM, e.g., after testing error injection.
454  *
455  * Usage:
456  *
457  * .. code-block:: bash
458  *
459  *	echo 1 > ../ras/ras_eeprom_reset
460  *
461  * will reset EEPROM table to 0 entries.
462  *
463  */
464 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f,
465 					       const char __user *buf,
466 					       size_t size, loff_t *pos)
467 {
468 	struct amdgpu_device *adev =
469 		(struct amdgpu_device *)file_inode(f)->i_private;
470 	int ret;
471 
472 	ret = amdgpu_ras_eeprom_reset_table(
473 		&(amdgpu_ras_get_context(adev)->eeprom_control));
474 
475 	if (!ret) {
476 		/* Something was written to EEPROM.
477 		 */
478 		amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS;
479 		return size;
480 	} else {
481 		return ret;
482 	}
483 }
484 
485 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
486 	.owner = THIS_MODULE,
487 	.read = NULL,
488 	.write = amdgpu_ras_debugfs_ctrl_write,
489 	.llseek = default_llseek
490 };
491 
492 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
493 	.owner = THIS_MODULE,
494 	.read = NULL,
495 	.write = amdgpu_ras_debugfs_eeprom_write,
496 	.llseek = default_llseek
497 };
498 
499 /**
500  * DOC: AMDGPU RAS sysfs Error Count Interface
501  *
502  * It allows the user to read the error count for each IP block on the gpu through
503  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
504  *
505  * It outputs the multiple lines which report the uncorrected (ue) and corrected
506  * (ce) error counts.
507  *
508  * The format of one line is below,
509  *
510  * [ce|ue]: count
511  *
512  * Example:
513  *
514  * .. code-block:: bash
515  *
516  *	ue: 0
517  *	ce: 1
518  *
519  */
520 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
521 		struct device_attribute *attr, char *buf)
522 {
523 	struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
524 	struct ras_query_if info = {
525 		.head = obj->head,
526 	};
527 
528 	if (!amdgpu_ras_get_error_query_ready(obj->adev))
529 		return sysfs_emit(buf, "Query currently inaccessible\n");
530 
531 	if (amdgpu_ras_query_error_status(obj->adev, &info))
532 		return -EINVAL;
533 
534 	if (obj->adev->asic_type == CHIP_ALDEBARAN) {
535 		if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
536 			DRM_WARN("Failed to reset error counter and error status");
537 	}
538 
539 	return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count,
540 			  "ce", info.ce_count);
541 }
542 
543 /* obj begin */
544 
545 #define get_obj(obj) do { (obj)->use++; } while (0)
546 #define alive_obj(obj) ((obj)->use)
547 
548 static inline void put_obj(struct ras_manager *obj)
549 {
550 	if (obj && (--obj->use == 0))
551 		list_del(&obj->node);
552 	if (obj && (obj->use < 0))
553 		DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", get_ras_block_str(&obj->head));
554 }
555 
556 /* make one obj and return it. */
557 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
558 		struct ras_common_if *head)
559 {
560 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
561 	struct ras_manager *obj;
562 
563 	if (!adev->ras_enabled || !con)
564 		return NULL;
565 
566 	if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
567 		return NULL;
568 
569 	if (head->block == AMDGPU_RAS_BLOCK__MCA) {
570 		if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
571 			return NULL;
572 
573 		obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
574 	} else
575 		obj = &con->objs[head->block];
576 
577 	/* already exist. return obj? */
578 	if (alive_obj(obj))
579 		return NULL;
580 
581 	obj->head = *head;
582 	obj->adev = adev;
583 	list_add(&obj->node, &con->head);
584 	get_obj(obj);
585 
586 	return obj;
587 }
588 
589 /* return an obj equal to head, or the first when head is NULL */
590 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
591 		struct ras_common_if *head)
592 {
593 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
594 	struct ras_manager *obj;
595 	int i;
596 
597 	if (!adev->ras_enabled || !con)
598 		return NULL;
599 
600 	if (head) {
601 		if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
602 			return NULL;
603 
604 		if (head->block == AMDGPU_RAS_BLOCK__MCA) {
605 			if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
606 				return NULL;
607 
608 			obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
609 		} else
610 			obj = &con->objs[head->block];
611 
612 		if (alive_obj(obj))
613 			return obj;
614 	} else {
615 		for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT + AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
616 			obj = &con->objs[i];
617 			if (alive_obj(obj))
618 				return obj;
619 		}
620 	}
621 
622 	return NULL;
623 }
624 /* obj end */
625 
626 /* feature ctl begin */
627 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
628 					 struct ras_common_if *head)
629 {
630 	return adev->ras_hw_enabled & BIT(head->block);
631 }
632 
633 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
634 		struct ras_common_if *head)
635 {
636 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
637 
638 	return con->features & BIT(head->block);
639 }
640 
641 /*
642  * if obj is not created, then create one.
643  * set feature enable flag.
644  */
645 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
646 		struct ras_common_if *head, int enable)
647 {
648 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
649 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
650 
651 	/* If hardware does not support ras, then do not create obj.
652 	 * But if hardware support ras, we can create the obj.
653 	 * Ras framework checks con->hw_supported to see if it need do
654 	 * corresponding initialization.
655 	 * IP checks con->support to see if it need disable ras.
656 	 */
657 	if (!amdgpu_ras_is_feature_allowed(adev, head))
658 		return 0;
659 
660 	if (enable) {
661 		if (!obj) {
662 			obj = amdgpu_ras_create_obj(adev, head);
663 			if (!obj)
664 				return -EINVAL;
665 		} else {
666 			/* In case we create obj somewhere else */
667 			get_obj(obj);
668 		}
669 		con->features |= BIT(head->block);
670 	} else {
671 		if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
672 			con->features &= ~BIT(head->block);
673 			put_obj(obj);
674 		}
675 	}
676 
677 	return 0;
678 }
679 
680 /* wrapper of psp_ras_enable_features */
681 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
682 		struct ras_common_if *head, bool enable)
683 {
684 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
685 	union ta_ras_cmd_input *info;
686 	int ret;
687 
688 	if (!con)
689 		return -EINVAL;
690 
691 	info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
692 	if (!info)
693 		return -ENOMEM;
694 
695 	if (!enable) {
696 		info->disable_features = (struct ta_ras_disable_features_input) {
697 			.block_id =  amdgpu_ras_block_to_ta(head->block),
698 			.error_type = amdgpu_ras_error_to_ta(head->type),
699 		};
700 	} else {
701 		info->enable_features = (struct ta_ras_enable_features_input) {
702 			.block_id =  amdgpu_ras_block_to_ta(head->block),
703 			.error_type = amdgpu_ras_error_to_ta(head->type),
704 		};
705 	}
706 
707 	/* Do not enable if it is not allowed. */
708 	WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
709 
710 	if (!amdgpu_ras_intr_triggered()) {
711 		ret = psp_ras_enable_features(&adev->psp, info, enable);
712 		if (ret) {
713 			dev_err(adev->dev, "ras %s %s failed poison:%d ret:%d\n",
714 				enable ? "enable":"disable",
715 				get_ras_block_str(head),
716 				amdgpu_ras_is_poison_mode_supported(adev), ret);
717 			goto out;
718 		}
719 	}
720 
721 	/* setup the obj */
722 	__amdgpu_ras_feature_enable(adev, head, enable);
723 	ret = 0;
724 out:
725 	kfree(info);
726 	return ret;
727 }
728 
729 /* Only used in device probe stage and called only once. */
730 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
731 		struct ras_common_if *head, bool enable)
732 {
733 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
734 	int ret;
735 
736 	if (!con)
737 		return -EINVAL;
738 
739 	if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
740 		if (enable) {
741 			/* There is no harm to issue a ras TA cmd regardless of
742 			 * the currecnt ras state.
743 			 * If current state == target state, it will do nothing
744 			 * But sometimes it requests driver to reset and repost
745 			 * with error code -EAGAIN.
746 			 */
747 			ret = amdgpu_ras_feature_enable(adev, head, 1);
748 			/* With old ras TA, we might fail to enable ras.
749 			 * Log it and just setup the object.
750 			 * TODO need remove this WA in the future.
751 			 */
752 			if (ret == -EINVAL) {
753 				ret = __amdgpu_ras_feature_enable(adev, head, 1);
754 				if (!ret)
755 					dev_info(adev->dev,
756 						"RAS INFO: %s setup object\n",
757 						get_ras_block_str(head));
758 			}
759 		} else {
760 			/* setup the object then issue a ras TA disable cmd.*/
761 			ret = __amdgpu_ras_feature_enable(adev, head, 1);
762 			if (ret)
763 				return ret;
764 
765 			/* gfx block ras dsiable cmd must send to ras-ta */
766 			if (head->block == AMDGPU_RAS_BLOCK__GFX)
767 				con->features |= BIT(head->block);
768 
769 			ret = amdgpu_ras_feature_enable(adev, head, 0);
770 
771 			/* clean gfx block ras features flag */
772 			if (adev->ras_enabled && head->block == AMDGPU_RAS_BLOCK__GFX)
773 				con->features &= ~BIT(head->block);
774 		}
775 	} else
776 		ret = amdgpu_ras_feature_enable(adev, head, enable);
777 
778 	return ret;
779 }
780 
781 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
782 		bool bypass)
783 {
784 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
785 	struct ras_manager *obj, *tmp;
786 
787 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
788 		/* bypass psp.
789 		 * aka just release the obj and corresponding flags
790 		 */
791 		if (bypass) {
792 			if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
793 				break;
794 		} else {
795 			if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
796 				break;
797 		}
798 	}
799 
800 	return con->features;
801 }
802 
803 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
804 		bool bypass)
805 {
806 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
807 	int i;
808 	const enum amdgpu_ras_error_type default_ras_type = AMDGPU_RAS_ERROR__NONE;
809 
810 	for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
811 		struct ras_common_if head = {
812 			.block = i,
813 			.type = default_ras_type,
814 			.sub_block_index = 0,
815 		};
816 
817 		if (i == AMDGPU_RAS_BLOCK__MCA)
818 			continue;
819 
820 		if (bypass) {
821 			/*
822 			 * bypass psp. vbios enable ras for us.
823 			 * so just create the obj
824 			 */
825 			if (__amdgpu_ras_feature_enable(adev, &head, 1))
826 				break;
827 		} else {
828 			if (amdgpu_ras_feature_enable(adev, &head, 1))
829 				break;
830 		}
831 	}
832 
833 	for (i = 0; i < AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
834 		struct ras_common_if head = {
835 			.block = AMDGPU_RAS_BLOCK__MCA,
836 			.type = default_ras_type,
837 			.sub_block_index = i,
838 		};
839 
840 		if (bypass) {
841 			/*
842 			 * bypass psp. vbios enable ras for us.
843 			 * so just create the obj
844 			 */
845 			if (__amdgpu_ras_feature_enable(adev, &head, 1))
846 				break;
847 		} else {
848 			if (amdgpu_ras_feature_enable(adev, &head, 1))
849 				break;
850 		}
851 	}
852 
853 	return con->features;
854 }
855 /* feature ctl end */
856 
857 
858 void amdgpu_ras_mca_query_error_status(struct amdgpu_device *adev,
859 				       struct ras_common_if *ras_block,
860 				       struct ras_err_data  *err_data)
861 {
862 	switch (ras_block->sub_block_index) {
863 	case AMDGPU_RAS_MCA_BLOCK__MP0:
864 		if (adev->mca.mp0.ras_funcs &&
865 		    adev->mca.mp0.ras_funcs->query_ras_error_count)
866 			adev->mca.mp0.ras_funcs->query_ras_error_count(adev, &err_data);
867 		break;
868 	case AMDGPU_RAS_MCA_BLOCK__MP1:
869 		if (adev->mca.mp1.ras_funcs &&
870 		    adev->mca.mp1.ras_funcs->query_ras_error_count)
871 			adev->mca.mp1.ras_funcs->query_ras_error_count(adev, &err_data);
872 		break;
873 	case AMDGPU_RAS_MCA_BLOCK__MPIO:
874 		if (adev->mca.mpio.ras_funcs &&
875 		    adev->mca.mpio.ras_funcs->query_ras_error_count)
876 			adev->mca.mpio.ras_funcs->query_ras_error_count(adev, &err_data);
877 		break;
878 	default:
879 		break;
880 	}
881 }
882 
883 /* query/inject/cure begin */
884 int amdgpu_ras_query_error_status(struct amdgpu_device *adev,
885 				  struct ras_query_if *info)
886 {
887 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
888 	struct ras_err_data err_data = {0, 0, 0, NULL};
889 	int i;
890 
891 	if (!obj)
892 		return -EINVAL;
893 
894 	switch (info->head.block) {
895 	case AMDGPU_RAS_BLOCK__UMC:
896 		if (adev->umc.ras_funcs &&
897 		    adev->umc.ras_funcs->query_ras_error_count)
898 			adev->umc.ras_funcs->query_ras_error_count(adev, &err_data);
899 		/* umc query_ras_error_address is also responsible for clearing
900 		 * error status
901 		 */
902 		if (adev->umc.ras_funcs &&
903 		    adev->umc.ras_funcs->query_ras_error_address)
904 			adev->umc.ras_funcs->query_ras_error_address(adev, &err_data);
905 		break;
906 	case AMDGPU_RAS_BLOCK__SDMA:
907 		if (adev->sdma.funcs->query_ras_error_count) {
908 			for (i = 0; i < adev->sdma.num_instances; i++)
909 				adev->sdma.funcs->query_ras_error_count(adev, i,
910 									&err_data);
911 		}
912 		break;
913 	case AMDGPU_RAS_BLOCK__GFX:
914 		if (adev->gfx.ras_funcs &&
915 		    adev->gfx.ras_funcs->query_ras_error_count)
916 			adev->gfx.ras_funcs->query_ras_error_count(adev, &err_data);
917 
918 		if (adev->gfx.ras_funcs &&
919 		    adev->gfx.ras_funcs->query_ras_error_status)
920 			adev->gfx.ras_funcs->query_ras_error_status(adev);
921 		break;
922 	case AMDGPU_RAS_BLOCK__MMHUB:
923 		if (adev->mmhub.ras_funcs &&
924 		    adev->mmhub.ras_funcs->query_ras_error_count)
925 			adev->mmhub.ras_funcs->query_ras_error_count(adev, &err_data);
926 
927 		if (adev->mmhub.ras_funcs &&
928 		    adev->mmhub.ras_funcs->query_ras_error_status)
929 			adev->mmhub.ras_funcs->query_ras_error_status(adev);
930 		break;
931 	case AMDGPU_RAS_BLOCK__PCIE_BIF:
932 		if (adev->nbio.ras_funcs &&
933 		    adev->nbio.ras_funcs->query_ras_error_count)
934 			adev->nbio.ras_funcs->query_ras_error_count(adev, &err_data);
935 		break;
936 	case AMDGPU_RAS_BLOCK__XGMI_WAFL:
937 		if (adev->gmc.xgmi.ras_funcs &&
938 		    adev->gmc.xgmi.ras_funcs->query_ras_error_count)
939 			adev->gmc.xgmi.ras_funcs->query_ras_error_count(adev, &err_data);
940 		break;
941 	case AMDGPU_RAS_BLOCK__HDP:
942 		if (adev->hdp.ras_funcs &&
943 		    adev->hdp.ras_funcs->query_ras_error_count)
944 			adev->hdp.ras_funcs->query_ras_error_count(adev, &err_data);
945 		break;
946 	case AMDGPU_RAS_BLOCK__MCA:
947 		amdgpu_ras_mca_query_error_status(adev, &info->head, &err_data);
948 		break;
949 	default:
950 		break;
951 	}
952 
953 	obj->err_data.ue_count += err_data.ue_count;
954 	obj->err_data.ce_count += err_data.ce_count;
955 
956 	info->ue_count = obj->err_data.ue_count;
957 	info->ce_count = obj->err_data.ce_count;
958 
959 	if (err_data.ce_count) {
960 		if (adev->smuio.funcs &&
961 		    adev->smuio.funcs->get_socket_id &&
962 		    adev->smuio.funcs->get_die_id) {
963 			dev_info(adev->dev, "socket: %d, die: %d "
964 					"%ld correctable hardware errors "
965 					"detected in %s block, no user "
966 					"action is needed.\n",
967 					adev->smuio.funcs->get_socket_id(adev),
968 					adev->smuio.funcs->get_die_id(adev),
969 					obj->err_data.ce_count,
970 					get_ras_block_str(&info->head));
971 		} else {
972 			dev_info(adev->dev, "%ld correctable hardware errors "
973 					"detected in %s block, no user "
974 					"action is needed.\n",
975 					obj->err_data.ce_count,
976 					get_ras_block_str(&info->head));
977 		}
978 	}
979 	if (err_data.ue_count) {
980 		if (adev->smuio.funcs &&
981 		    adev->smuio.funcs->get_socket_id &&
982 		    adev->smuio.funcs->get_die_id) {
983 			dev_info(adev->dev, "socket: %d, die: %d "
984 					"%ld uncorrectable hardware errors "
985 					"detected in %s block\n",
986 					adev->smuio.funcs->get_socket_id(adev),
987 					adev->smuio.funcs->get_die_id(adev),
988 					obj->err_data.ue_count,
989 					get_ras_block_str(&info->head));
990 		} else {
991 			dev_info(adev->dev, "%ld uncorrectable hardware errors "
992 					"detected in %s block\n",
993 					obj->err_data.ue_count,
994 					get_ras_block_str(&info->head));
995 		}
996 	}
997 
998 	return 0;
999 }
1000 
1001 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev,
1002 		enum amdgpu_ras_block block)
1003 {
1004 	if (!amdgpu_ras_is_supported(adev, block))
1005 		return -EINVAL;
1006 
1007 	switch (block) {
1008 	case AMDGPU_RAS_BLOCK__GFX:
1009 		if (adev->gfx.ras_funcs &&
1010 		    adev->gfx.ras_funcs->reset_ras_error_count)
1011 			adev->gfx.ras_funcs->reset_ras_error_count(adev);
1012 
1013 		if (adev->gfx.ras_funcs &&
1014 		    adev->gfx.ras_funcs->reset_ras_error_status)
1015 			adev->gfx.ras_funcs->reset_ras_error_status(adev);
1016 		break;
1017 	case AMDGPU_RAS_BLOCK__MMHUB:
1018 		if (adev->mmhub.ras_funcs &&
1019 		    adev->mmhub.ras_funcs->reset_ras_error_count)
1020 			adev->mmhub.ras_funcs->reset_ras_error_count(adev);
1021 
1022 		if (adev->mmhub.ras_funcs &&
1023 		    adev->mmhub.ras_funcs->reset_ras_error_status)
1024 			adev->mmhub.ras_funcs->reset_ras_error_status(adev);
1025 		break;
1026 	case AMDGPU_RAS_BLOCK__SDMA:
1027 		if (adev->sdma.funcs->reset_ras_error_count)
1028 			adev->sdma.funcs->reset_ras_error_count(adev);
1029 		break;
1030 	case AMDGPU_RAS_BLOCK__HDP:
1031 		if (adev->hdp.ras_funcs &&
1032 		    adev->hdp.ras_funcs->reset_ras_error_count)
1033 			adev->hdp.ras_funcs->reset_ras_error_count(adev);
1034 		break;
1035 	default:
1036 		break;
1037 	}
1038 
1039 	return 0;
1040 }
1041 
1042 /* Trigger XGMI/WAFL error */
1043 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev,
1044 				 struct ta_ras_trigger_error_input *block_info)
1045 {
1046 	int ret;
1047 
1048 	if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
1049 		dev_warn(adev->dev, "Failed to disallow df cstate");
1050 
1051 	if (amdgpu_dpm_allow_xgmi_power_down(adev, false))
1052 		dev_warn(adev->dev, "Failed to disallow XGMI power down");
1053 
1054 	ret = psp_ras_trigger_error(&adev->psp, block_info);
1055 
1056 	if (amdgpu_ras_intr_triggered())
1057 		return ret;
1058 
1059 	if (amdgpu_dpm_allow_xgmi_power_down(adev, true))
1060 		dev_warn(adev->dev, "Failed to allow XGMI power down");
1061 
1062 	if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW))
1063 		dev_warn(adev->dev, "Failed to allow df cstate");
1064 
1065 	return ret;
1066 }
1067 
1068 /* wrapper of psp_ras_trigger_error */
1069 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
1070 		struct ras_inject_if *info)
1071 {
1072 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1073 	struct ta_ras_trigger_error_input block_info = {
1074 		.block_id =  amdgpu_ras_block_to_ta(info->head.block),
1075 		.inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
1076 		.sub_block_index = info->head.sub_block_index,
1077 		.address = info->address,
1078 		.value = info->value,
1079 	};
1080 	int ret = 0;
1081 
1082 	if (!obj)
1083 		return -EINVAL;
1084 
1085 	/* Calculate XGMI relative offset */
1086 	if (adev->gmc.xgmi.num_physical_nodes > 1) {
1087 		block_info.address =
1088 			amdgpu_xgmi_get_relative_phy_addr(adev,
1089 							  block_info.address);
1090 	}
1091 
1092 	switch (info->head.block) {
1093 	case AMDGPU_RAS_BLOCK__GFX:
1094 		if (adev->gfx.ras_funcs &&
1095 		    adev->gfx.ras_funcs->ras_error_inject)
1096 			ret = adev->gfx.ras_funcs->ras_error_inject(adev, info);
1097 		else
1098 			ret = -EINVAL;
1099 		break;
1100 	case AMDGPU_RAS_BLOCK__UMC:
1101 	case AMDGPU_RAS_BLOCK__SDMA:
1102 	case AMDGPU_RAS_BLOCK__MMHUB:
1103 	case AMDGPU_RAS_BLOCK__PCIE_BIF:
1104 	case AMDGPU_RAS_BLOCK__MCA:
1105 		ret = psp_ras_trigger_error(&adev->psp, &block_info);
1106 		break;
1107 	case AMDGPU_RAS_BLOCK__XGMI_WAFL:
1108 		ret = amdgpu_ras_error_inject_xgmi(adev, &block_info);
1109 		break;
1110 	default:
1111 		dev_info(adev->dev, "%s error injection is not supported yet\n",
1112 			 get_ras_block_str(&info->head));
1113 		ret = -EINVAL;
1114 	}
1115 
1116 	if (ret)
1117 		dev_err(adev->dev, "ras inject %s failed %d\n",
1118 			get_ras_block_str(&info->head), ret);
1119 
1120 	return ret;
1121 }
1122 
1123 /**
1124  * amdgpu_ras_query_error_count -- Get error counts of all IPs
1125  * adev: pointer to AMD GPU device
1126  * ce_count: pointer to an integer to be set to the count of correctible errors.
1127  * ue_count: pointer to an integer to be set to the count of uncorrectible
1128  * errors.
1129  *
1130  * If set, @ce_count or @ue_count, count and return the corresponding
1131  * error counts in those integer pointers. Return 0 if the device
1132  * supports RAS. Return -EOPNOTSUPP if the device doesn't support RAS.
1133  */
1134 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
1135 				 unsigned long *ce_count,
1136 				 unsigned long *ue_count)
1137 {
1138 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1139 	struct ras_manager *obj;
1140 	unsigned long ce, ue;
1141 
1142 	if (!adev->ras_enabled || !con)
1143 		return -EOPNOTSUPP;
1144 
1145 	/* Don't count since no reporting.
1146 	 */
1147 	if (!ce_count && !ue_count)
1148 		return 0;
1149 
1150 	ce = 0;
1151 	ue = 0;
1152 	list_for_each_entry(obj, &con->head, node) {
1153 		struct ras_query_if info = {
1154 			.head = obj->head,
1155 		};
1156 		int res;
1157 
1158 		res = amdgpu_ras_query_error_status(adev, &info);
1159 		if (res)
1160 			return res;
1161 
1162 		ce += info.ce_count;
1163 		ue += info.ue_count;
1164 	}
1165 
1166 	if (ce_count)
1167 		*ce_count = ce;
1168 
1169 	if (ue_count)
1170 		*ue_count = ue;
1171 
1172 	return 0;
1173 }
1174 /* query/inject/cure end */
1175 
1176 
1177 /* sysfs begin */
1178 
1179 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1180 		struct ras_badpage **bps, unsigned int *count);
1181 
1182 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
1183 {
1184 	switch (flags) {
1185 	case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
1186 		return "R";
1187 	case AMDGPU_RAS_RETIRE_PAGE_PENDING:
1188 		return "P";
1189 	case AMDGPU_RAS_RETIRE_PAGE_FAULT:
1190 	default:
1191 		return "F";
1192 	}
1193 }
1194 
1195 /**
1196  * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
1197  *
1198  * It allows user to read the bad pages of vram on the gpu through
1199  * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
1200  *
1201  * It outputs multiple lines, and each line stands for one gpu page.
1202  *
1203  * The format of one line is below,
1204  * gpu pfn : gpu page size : flags
1205  *
1206  * gpu pfn and gpu page size are printed in hex format.
1207  * flags can be one of below character,
1208  *
1209  * R: reserved, this gpu page is reserved and not able to use.
1210  *
1211  * P: pending for reserve, this gpu page is marked as bad, will be reserved
1212  * in next window of page_reserve.
1213  *
1214  * F: unable to reserve. this gpu page can't be reserved due to some reasons.
1215  *
1216  * Examples:
1217  *
1218  * .. code-block:: bash
1219  *
1220  *	0x00000001 : 0x00001000 : R
1221  *	0x00000002 : 0x00001000 : P
1222  *
1223  */
1224 
1225 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
1226 		struct kobject *kobj, struct bin_attribute *attr,
1227 		char *buf, loff_t ppos, size_t count)
1228 {
1229 	struct amdgpu_ras *con =
1230 		container_of(attr, struct amdgpu_ras, badpages_attr);
1231 	struct amdgpu_device *adev = con->adev;
1232 	const unsigned int element_size =
1233 		sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
1234 	unsigned int start = div64_ul(ppos + element_size - 1, element_size);
1235 	unsigned int end = div64_ul(ppos + count - 1, element_size);
1236 	ssize_t s = 0;
1237 	struct ras_badpage *bps = NULL;
1238 	unsigned int bps_count = 0;
1239 
1240 	memset(buf, 0, count);
1241 
1242 	if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1243 		return 0;
1244 
1245 	for (; start < end && start < bps_count; start++)
1246 		s += scnprintf(&buf[s], element_size + 1,
1247 				"0x%08x : 0x%08x : %1s\n",
1248 				bps[start].bp,
1249 				bps[start].size,
1250 				amdgpu_ras_badpage_flags_str(bps[start].flags));
1251 
1252 	kfree(bps);
1253 
1254 	return s;
1255 }
1256 
1257 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1258 		struct device_attribute *attr, char *buf)
1259 {
1260 	struct amdgpu_ras *con =
1261 		container_of(attr, struct amdgpu_ras, features_attr);
1262 
1263 	return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
1264 }
1265 
1266 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1267 {
1268 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1269 
1270 	sysfs_remove_file_from_group(&adev->dev->kobj,
1271 				&con->badpages_attr.attr,
1272 				RAS_FS_NAME);
1273 }
1274 
1275 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1276 {
1277 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1278 	struct attribute *attrs[] = {
1279 		&con->features_attr.attr,
1280 		NULL
1281 	};
1282 	struct attribute_group group = {
1283 		.name = RAS_FS_NAME,
1284 		.attrs = attrs,
1285 	};
1286 
1287 	sysfs_remove_group(&adev->dev->kobj, &group);
1288 
1289 	return 0;
1290 }
1291 
1292 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1293 		struct ras_fs_if *head)
1294 {
1295 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1296 
1297 	if (!obj || obj->attr_inuse)
1298 		return -EINVAL;
1299 
1300 	get_obj(obj);
1301 
1302 	memcpy(obj->fs_data.sysfs_name,
1303 			head->sysfs_name,
1304 			sizeof(obj->fs_data.sysfs_name));
1305 
1306 	obj->sysfs_attr = (struct device_attribute){
1307 		.attr = {
1308 			.name = obj->fs_data.sysfs_name,
1309 			.mode = S_IRUGO,
1310 		},
1311 			.show = amdgpu_ras_sysfs_read,
1312 	};
1313 	sysfs_attr_init(&obj->sysfs_attr.attr);
1314 
1315 	if (sysfs_add_file_to_group(&adev->dev->kobj,
1316 				&obj->sysfs_attr.attr,
1317 				RAS_FS_NAME)) {
1318 		put_obj(obj);
1319 		return -EINVAL;
1320 	}
1321 
1322 	obj->attr_inuse = 1;
1323 
1324 	return 0;
1325 }
1326 
1327 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1328 		struct ras_common_if *head)
1329 {
1330 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1331 
1332 	if (!obj || !obj->attr_inuse)
1333 		return -EINVAL;
1334 
1335 	sysfs_remove_file_from_group(&adev->dev->kobj,
1336 				&obj->sysfs_attr.attr,
1337 				RAS_FS_NAME);
1338 	obj->attr_inuse = 0;
1339 	put_obj(obj);
1340 
1341 	return 0;
1342 }
1343 
1344 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1345 {
1346 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1347 	struct ras_manager *obj, *tmp;
1348 
1349 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
1350 		amdgpu_ras_sysfs_remove(adev, &obj->head);
1351 	}
1352 
1353 	if (amdgpu_bad_page_threshold != 0)
1354 		amdgpu_ras_sysfs_remove_bad_page_node(adev);
1355 
1356 	amdgpu_ras_sysfs_remove_feature_node(adev);
1357 
1358 	return 0;
1359 }
1360 /* sysfs end */
1361 
1362 /**
1363  * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1364  *
1365  * Normally when there is an uncorrectable error, the driver will reset
1366  * the GPU to recover.  However, in the event of an unrecoverable error,
1367  * the driver provides an interface to reboot the system automatically
1368  * in that event.
1369  *
1370  * The following file in debugfs provides that interface:
1371  * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1372  *
1373  * Usage:
1374  *
1375  * .. code-block:: bash
1376  *
1377  *	echo true > .../ras/auto_reboot
1378  *
1379  */
1380 /* debugfs begin */
1381 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1382 {
1383 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1384 	struct drm_minor  *minor = adev_to_drm(adev)->primary;
1385 	struct dentry     *dir;
1386 
1387 	dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1388 	debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev,
1389 			    &amdgpu_ras_debugfs_ctrl_ops);
1390 	debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev,
1391 			    &amdgpu_ras_debugfs_eeprom_ops);
1392 	debugfs_create_u32("bad_page_cnt_threshold", 0444, dir,
1393 			   &con->bad_page_cnt_threshold);
1394 	debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled);
1395 	debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled);
1396 	debugfs_create_file("ras_eeprom_size", S_IRUGO, dir, adev,
1397 			    &amdgpu_ras_debugfs_eeprom_size_ops);
1398 	con->de_ras_eeprom_table = debugfs_create_file("ras_eeprom_table",
1399 						       S_IRUGO, dir, adev,
1400 						       &amdgpu_ras_debugfs_eeprom_table_ops);
1401 	amdgpu_ras_debugfs_set_ret_size(&con->eeprom_control);
1402 
1403 	/*
1404 	 * After one uncorrectable error happens, usually GPU recovery will
1405 	 * be scheduled. But due to the known problem in GPU recovery failing
1406 	 * to bring GPU back, below interface provides one direct way to
1407 	 * user to reboot system automatically in such case within
1408 	 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1409 	 * will never be called.
1410 	 */
1411 	debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot);
1412 
1413 	/*
1414 	 * User could set this not to clean up hardware's error count register
1415 	 * of RAS IPs during ras recovery.
1416 	 */
1417 	debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir,
1418 			    &con->disable_ras_err_cnt_harvest);
1419 	return dir;
1420 }
1421 
1422 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1423 				      struct ras_fs_if *head,
1424 				      struct dentry *dir)
1425 {
1426 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1427 
1428 	if (!obj || !dir)
1429 		return;
1430 
1431 	get_obj(obj);
1432 
1433 	memcpy(obj->fs_data.debugfs_name,
1434 			head->debugfs_name,
1435 			sizeof(obj->fs_data.debugfs_name));
1436 
1437 	debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir,
1438 			    obj, &amdgpu_ras_debugfs_ops);
1439 }
1440 
1441 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1442 {
1443 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1444 	struct dentry *dir;
1445 	struct ras_manager *obj;
1446 	struct ras_fs_if fs_info;
1447 
1448 	/*
1449 	 * it won't be called in resume path, no need to check
1450 	 * suspend and gpu reset status
1451 	 */
1452 	if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con)
1453 		return;
1454 
1455 	dir = amdgpu_ras_debugfs_create_ctrl_node(adev);
1456 
1457 	list_for_each_entry(obj, &con->head, node) {
1458 		if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1459 			(obj->attr_inuse == 1)) {
1460 			sprintf(fs_info.debugfs_name, "%s_err_inject",
1461 					get_ras_block_str(&obj->head));
1462 			fs_info.head = obj->head;
1463 			amdgpu_ras_debugfs_create(adev, &fs_info, dir);
1464 		}
1465 	}
1466 }
1467 
1468 /* debugfs end */
1469 
1470 /* ras fs */
1471 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1472 		amdgpu_ras_sysfs_badpages_read, NULL, 0);
1473 static DEVICE_ATTR(features, S_IRUGO,
1474 		amdgpu_ras_sysfs_features_read, NULL);
1475 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1476 {
1477 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1478 	struct attribute_group group = {
1479 		.name = RAS_FS_NAME,
1480 	};
1481 	struct attribute *attrs[] = {
1482 		&con->features_attr.attr,
1483 		NULL
1484 	};
1485 	struct bin_attribute *bin_attrs[] = {
1486 		NULL,
1487 		NULL,
1488 	};
1489 	int r;
1490 
1491 	/* add features entry */
1492 	con->features_attr = dev_attr_features;
1493 	group.attrs = attrs;
1494 	sysfs_attr_init(attrs[0]);
1495 
1496 	if (amdgpu_bad_page_threshold != 0) {
1497 		/* add bad_page_features entry */
1498 		bin_attr_gpu_vram_bad_pages.private = NULL;
1499 		con->badpages_attr = bin_attr_gpu_vram_bad_pages;
1500 		bin_attrs[0] = &con->badpages_attr;
1501 		group.bin_attrs = bin_attrs;
1502 		sysfs_bin_attr_init(bin_attrs[0]);
1503 	}
1504 
1505 	r = sysfs_create_group(&adev->dev->kobj, &group);
1506 	if (r)
1507 		dev_err(adev->dev, "Failed to create RAS sysfs group!");
1508 
1509 	return 0;
1510 }
1511 
1512 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1513 {
1514 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1515 	struct ras_manager *con_obj, *ip_obj, *tmp;
1516 
1517 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1518 		list_for_each_entry_safe(con_obj, tmp, &con->head, node) {
1519 			ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head);
1520 			if (ip_obj)
1521 				put_obj(ip_obj);
1522 		}
1523 	}
1524 
1525 	amdgpu_ras_sysfs_remove_all(adev);
1526 	return 0;
1527 }
1528 /* ras fs end */
1529 
1530 /* ih begin */
1531 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1532 {
1533 	struct ras_ih_data *data = &obj->ih_data;
1534 	struct amdgpu_iv_entry entry;
1535 	int ret;
1536 	struct ras_err_data err_data = {0, 0, 0, NULL};
1537 
1538 	while (data->rptr != data->wptr) {
1539 		rmb();
1540 		memcpy(&entry, &data->ring[data->rptr],
1541 				data->element_size);
1542 
1543 		wmb();
1544 		data->rptr = (data->aligned_element_size +
1545 				data->rptr) % data->ring_size;
1546 
1547 		if (data->cb) {
1548 			if (amdgpu_ras_is_poison_mode_supported(obj->adev) &&
1549 			    obj->head.block == AMDGPU_RAS_BLOCK__UMC)
1550 				dev_info(obj->adev->dev,
1551 						"Poison is created, no user action is needed.\n");
1552 			else {
1553 				/* Let IP handle its data, maybe we need get the output
1554 				 * from the callback to udpate the error type/count, etc
1555 				 */
1556 				ret = data->cb(obj->adev, &err_data, &entry);
1557 				/* ue will trigger an interrupt, and in that case
1558 				 * we need do a reset to recovery the whole system.
1559 				 * But leave IP do that recovery, here we just dispatch
1560 				 * the error.
1561 				 */
1562 				if (ret == AMDGPU_RAS_SUCCESS) {
1563 					/* these counts could be left as 0 if
1564 					 * some blocks do not count error number
1565 					 */
1566 					obj->err_data.ue_count += err_data.ue_count;
1567 					obj->err_data.ce_count += err_data.ce_count;
1568 				}
1569 			}
1570 		}
1571 	}
1572 }
1573 
1574 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1575 {
1576 	struct ras_ih_data *data =
1577 		container_of(work, struct ras_ih_data, ih_work);
1578 	struct ras_manager *obj =
1579 		container_of(data, struct ras_manager, ih_data);
1580 
1581 	amdgpu_ras_interrupt_handler(obj);
1582 }
1583 
1584 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1585 		struct ras_dispatch_if *info)
1586 {
1587 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1588 	struct ras_ih_data *data = &obj->ih_data;
1589 
1590 	if (!obj)
1591 		return -EINVAL;
1592 
1593 	if (data->inuse == 0)
1594 		return 0;
1595 
1596 	/* Might be overflow... */
1597 	memcpy(&data->ring[data->wptr], info->entry,
1598 			data->element_size);
1599 
1600 	wmb();
1601 	data->wptr = (data->aligned_element_size +
1602 			data->wptr) % data->ring_size;
1603 
1604 	schedule_work(&data->ih_work);
1605 
1606 	return 0;
1607 }
1608 
1609 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1610 		struct ras_ih_if *info)
1611 {
1612 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1613 	struct ras_ih_data *data;
1614 
1615 	if (!obj)
1616 		return -EINVAL;
1617 
1618 	data = &obj->ih_data;
1619 	if (data->inuse == 0)
1620 		return 0;
1621 
1622 	cancel_work_sync(&data->ih_work);
1623 
1624 	kfree(data->ring);
1625 	memset(data, 0, sizeof(*data));
1626 	put_obj(obj);
1627 
1628 	return 0;
1629 }
1630 
1631 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1632 		struct ras_ih_if *info)
1633 {
1634 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1635 	struct ras_ih_data *data;
1636 
1637 	if (!obj) {
1638 		/* in case we registe the IH before enable ras feature */
1639 		obj = amdgpu_ras_create_obj(adev, &info->head);
1640 		if (!obj)
1641 			return -EINVAL;
1642 	} else
1643 		get_obj(obj);
1644 
1645 	data = &obj->ih_data;
1646 	/* add the callback.etc */
1647 	*data = (struct ras_ih_data) {
1648 		.inuse = 0,
1649 		.cb = info->cb,
1650 		.element_size = sizeof(struct amdgpu_iv_entry),
1651 		.rptr = 0,
1652 		.wptr = 0,
1653 	};
1654 
1655 	INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1656 
1657 	data->aligned_element_size = ALIGN(data->element_size, 8);
1658 	/* the ring can store 64 iv entries. */
1659 	data->ring_size = 64 * data->aligned_element_size;
1660 	data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1661 	if (!data->ring) {
1662 		put_obj(obj);
1663 		return -ENOMEM;
1664 	}
1665 
1666 	/* IH is ready */
1667 	data->inuse = 1;
1668 
1669 	return 0;
1670 }
1671 
1672 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1673 {
1674 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1675 	struct ras_manager *obj, *tmp;
1676 
1677 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
1678 		struct ras_ih_if info = {
1679 			.head = obj->head,
1680 		};
1681 		amdgpu_ras_interrupt_remove_handler(adev, &info);
1682 	}
1683 
1684 	return 0;
1685 }
1686 /* ih end */
1687 
1688 /* traversal all IPs except NBIO to query error counter */
1689 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1690 {
1691 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1692 	struct ras_manager *obj;
1693 
1694 	if (!adev->ras_enabled || !con)
1695 		return;
1696 
1697 	list_for_each_entry(obj, &con->head, node) {
1698 		struct ras_query_if info = {
1699 			.head = obj->head,
1700 		};
1701 
1702 		/*
1703 		 * PCIE_BIF IP has one different isr by ras controller
1704 		 * interrupt, the specific ras counter query will be
1705 		 * done in that isr. So skip such block from common
1706 		 * sync flood interrupt isr calling.
1707 		 */
1708 		if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1709 			continue;
1710 
1711 		amdgpu_ras_query_error_status(adev, &info);
1712 	}
1713 }
1714 
1715 /* Parse RdRspStatus and WrRspStatus */
1716 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
1717 					  struct ras_query_if *info)
1718 {
1719 	/*
1720 	 * Only two block need to query read/write
1721 	 * RspStatus at current state
1722 	 */
1723 	switch (info->head.block) {
1724 	case AMDGPU_RAS_BLOCK__GFX:
1725 		if (adev->gfx.ras_funcs &&
1726 		    adev->gfx.ras_funcs->query_ras_error_status)
1727 			adev->gfx.ras_funcs->query_ras_error_status(adev);
1728 		break;
1729 	case AMDGPU_RAS_BLOCK__MMHUB:
1730 		if (adev->mmhub.ras_funcs &&
1731 		    adev->mmhub.ras_funcs->query_ras_error_status)
1732 			adev->mmhub.ras_funcs->query_ras_error_status(adev);
1733 		break;
1734 	default:
1735 		break;
1736 	}
1737 }
1738 
1739 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
1740 {
1741 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1742 	struct ras_manager *obj;
1743 
1744 	if (!adev->ras_enabled || !con)
1745 		return;
1746 
1747 	list_for_each_entry(obj, &con->head, node) {
1748 		struct ras_query_if info = {
1749 			.head = obj->head,
1750 		};
1751 
1752 		amdgpu_ras_error_status_query(adev, &info);
1753 	}
1754 }
1755 
1756 /* recovery begin */
1757 
1758 /* return 0 on success.
1759  * caller need free bps.
1760  */
1761 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1762 		struct ras_badpage **bps, unsigned int *count)
1763 {
1764 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1765 	struct ras_err_handler_data *data;
1766 	int i = 0;
1767 	int ret = 0, status;
1768 
1769 	if (!con || !con->eh_data || !bps || !count)
1770 		return -EINVAL;
1771 
1772 	mutex_lock(&con->recovery_lock);
1773 	data = con->eh_data;
1774 	if (!data || data->count == 0) {
1775 		*bps = NULL;
1776 		ret = -EINVAL;
1777 		goto out;
1778 	}
1779 
1780 	*bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1781 	if (!*bps) {
1782 		ret = -ENOMEM;
1783 		goto out;
1784 	}
1785 
1786 	for (; i < data->count; i++) {
1787 		(*bps)[i] = (struct ras_badpage){
1788 			.bp = data->bps[i].retired_page,
1789 			.size = AMDGPU_GPU_PAGE_SIZE,
1790 			.flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1791 		};
1792 		status = amdgpu_vram_mgr_query_page_status(
1793 				ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1794 				data->bps[i].retired_page);
1795 		if (status == -EBUSY)
1796 			(*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1797 		else if (status == -ENOENT)
1798 			(*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1799 	}
1800 
1801 	*count = data->count;
1802 out:
1803 	mutex_unlock(&con->recovery_lock);
1804 	return ret;
1805 }
1806 
1807 static void amdgpu_ras_do_recovery(struct work_struct *work)
1808 {
1809 	struct amdgpu_ras *ras =
1810 		container_of(work, struct amdgpu_ras, recovery_work);
1811 	struct amdgpu_device *remote_adev = NULL;
1812 	struct amdgpu_device *adev = ras->adev;
1813 	struct list_head device_list, *device_list_handle =  NULL;
1814 
1815 	if (!ras->disable_ras_err_cnt_harvest) {
1816 		struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
1817 
1818 		/* Build list of devices to query RAS related errors */
1819 		if  (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
1820 			device_list_handle = &hive->device_list;
1821 		} else {
1822 			INIT_LIST_HEAD(&device_list);
1823 			list_add_tail(&adev->gmc.xgmi.head, &device_list);
1824 			device_list_handle = &device_list;
1825 		}
1826 
1827 		list_for_each_entry(remote_adev,
1828 				device_list_handle, gmc.xgmi.head) {
1829 			amdgpu_ras_query_err_status(remote_adev);
1830 			amdgpu_ras_log_on_err_counter(remote_adev);
1831 		}
1832 
1833 		amdgpu_put_xgmi_hive(hive);
1834 	}
1835 
1836 	if (amdgpu_device_should_recover_gpu(ras->adev))
1837 		amdgpu_device_gpu_recover(ras->adev, NULL);
1838 	atomic_set(&ras->in_recovery, 0);
1839 }
1840 
1841 /* alloc/realloc bps array */
1842 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1843 		struct ras_err_handler_data *data, int pages)
1844 {
1845 	unsigned int old_space = data->count + data->space_left;
1846 	unsigned int new_space = old_space + pages;
1847 	unsigned int align_space = ALIGN(new_space, 512);
1848 	void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1849 
1850 	if (!bps) {
1851 		kfree(bps);
1852 		return -ENOMEM;
1853 	}
1854 
1855 	if (data->bps) {
1856 		memcpy(bps, data->bps,
1857 				data->count * sizeof(*data->bps));
1858 		kfree(data->bps);
1859 	}
1860 
1861 	data->bps = bps;
1862 	data->space_left += align_space - old_space;
1863 	return 0;
1864 }
1865 
1866 /* it deal with vram only. */
1867 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1868 		struct eeprom_table_record *bps, int pages)
1869 {
1870 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1871 	struct ras_err_handler_data *data;
1872 	int ret = 0;
1873 	uint32_t i;
1874 
1875 	if (!con || !con->eh_data || !bps || pages <= 0)
1876 		return 0;
1877 
1878 	mutex_lock(&con->recovery_lock);
1879 	data = con->eh_data;
1880 	if (!data)
1881 		goto out;
1882 
1883 	for (i = 0; i < pages; i++) {
1884 		if (amdgpu_ras_check_bad_page_unlock(con,
1885 			bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT))
1886 			continue;
1887 
1888 		if (!data->space_left &&
1889 			amdgpu_ras_realloc_eh_data_space(adev, data, 256)) {
1890 			ret = -ENOMEM;
1891 			goto out;
1892 		}
1893 
1894 		amdgpu_vram_mgr_reserve_range(
1895 			ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1896 			bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT,
1897 			AMDGPU_GPU_PAGE_SIZE);
1898 
1899 		memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps));
1900 		data->count++;
1901 		data->space_left--;
1902 	}
1903 out:
1904 	mutex_unlock(&con->recovery_lock);
1905 
1906 	return ret;
1907 }
1908 
1909 /*
1910  * write error record array to eeprom, the function should be
1911  * protected by recovery_lock
1912  */
1913 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1914 {
1915 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1916 	struct ras_err_handler_data *data;
1917 	struct amdgpu_ras_eeprom_control *control;
1918 	int save_count;
1919 
1920 	if (!con || !con->eh_data)
1921 		return 0;
1922 
1923 	control = &con->eeprom_control;
1924 	data = con->eh_data;
1925 	save_count = data->count - control->ras_num_recs;
1926 	/* only new entries are saved */
1927 	if (save_count > 0) {
1928 		if (amdgpu_ras_eeprom_append(control,
1929 					     &data->bps[control->ras_num_recs],
1930 					     save_count)) {
1931 			dev_err(adev->dev, "Failed to save EEPROM table data!");
1932 			return -EIO;
1933 		}
1934 
1935 		dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
1936 	}
1937 
1938 	return 0;
1939 }
1940 
1941 /*
1942  * read error record array in eeprom and reserve enough space for
1943  * storing new bad pages
1944  */
1945 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1946 {
1947 	struct amdgpu_ras_eeprom_control *control =
1948 		&adev->psp.ras_context.ras->eeprom_control;
1949 	struct eeprom_table_record *bps;
1950 	int ret;
1951 
1952 	/* no bad page record, skip eeprom access */
1953 	if (control->ras_num_recs == 0 || amdgpu_bad_page_threshold == 0)
1954 		return 0;
1955 
1956 	bps = kcalloc(control->ras_num_recs, sizeof(*bps), GFP_KERNEL);
1957 	if (!bps)
1958 		return -ENOMEM;
1959 
1960 	ret = amdgpu_ras_eeprom_read(control, bps, control->ras_num_recs);
1961 	if (ret)
1962 		dev_err(adev->dev, "Failed to load EEPROM table records!");
1963 	else
1964 		ret = amdgpu_ras_add_bad_pages(adev, bps, control->ras_num_recs);
1965 
1966 	kfree(bps);
1967 	return ret;
1968 }
1969 
1970 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
1971 				uint64_t addr)
1972 {
1973 	struct ras_err_handler_data *data = con->eh_data;
1974 	int i;
1975 
1976 	addr >>= AMDGPU_GPU_PAGE_SHIFT;
1977 	for (i = 0; i < data->count; i++)
1978 		if (addr == data->bps[i].retired_page)
1979 			return true;
1980 
1981 	return false;
1982 }
1983 
1984 /*
1985  * check if an address belongs to bad page
1986  *
1987  * Note: this check is only for umc block
1988  */
1989 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
1990 				uint64_t addr)
1991 {
1992 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1993 	bool ret = false;
1994 
1995 	if (!con || !con->eh_data)
1996 		return ret;
1997 
1998 	mutex_lock(&con->recovery_lock);
1999 	ret = amdgpu_ras_check_bad_page_unlock(con, addr);
2000 	mutex_unlock(&con->recovery_lock);
2001 	return ret;
2002 }
2003 
2004 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
2005 					  uint32_t max_count)
2006 {
2007 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2008 
2009 	/*
2010 	 * Justification of value bad_page_cnt_threshold in ras structure
2011 	 *
2012 	 * Generally, -1 <= amdgpu_bad_page_threshold <= max record length
2013 	 * in eeprom, and introduce two scenarios accordingly.
2014 	 *
2015 	 * Bad page retirement enablement:
2016 	 *    - If amdgpu_bad_page_threshold = -1,
2017 	 *      bad_page_cnt_threshold = typical value by formula.
2018 	 *
2019 	 *    - When the value from user is 0 < amdgpu_bad_page_threshold <
2020 	 *      max record length in eeprom, use it directly.
2021 	 *
2022 	 * Bad page retirement disablement:
2023 	 *    - If amdgpu_bad_page_threshold = 0, bad page retirement
2024 	 *      functionality is disabled, and bad_page_cnt_threshold will
2025 	 *      take no effect.
2026 	 */
2027 
2028 	if (amdgpu_bad_page_threshold < 0) {
2029 		u64 val = adev->gmc.mc_vram_size;
2030 
2031 		do_div(val, RAS_BAD_PAGE_COVER);
2032 		con->bad_page_cnt_threshold = min(lower_32_bits(val),
2033 						  max_count);
2034 	} else {
2035 		con->bad_page_cnt_threshold = min_t(int, max_count,
2036 						    amdgpu_bad_page_threshold);
2037 	}
2038 }
2039 
2040 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
2041 {
2042 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2043 	struct ras_err_handler_data **data;
2044 	u32  max_eeprom_records_count = 0;
2045 	bool exc_err_limit = false;
2046 	int ret;
2047 
2048 	if (!con)
2049 		return 0;
2050 
2051 	/* Allow access to RAS EEPROM via debugfs, when the ASIC
2052 	 * supports RAS and debugfs is enabled, but when
2053 	 * adev->ras_enabled is unset, i.e. when "ras_enable"
2054 	 * module parameter is set to 0.
2055 	 */
2056 	con->adev = adev;
2057 
2058 	if (!adev->ras_enabled)
2059 		return 0;
2060 
2061 	data = &con->eh_data;
2062 	*data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
2063 	if (!*data) {
2064 		ret = -ENOMEM;
2065 		goto out;
2066 	}
2067 
2068 	mutex_init(&con->recovery_lock);
2069 	INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
2070 	atomic_set(&con->in_recovery, 0);
2071 
2072 	max_eeprom_records_count = amdgpu_ras_eeprom_max_record_count();
2073 	amdgpu_ras_validate_threshold(adev, max_eeprom_records_count);
2074 
2075 	/* Todo: During test the SMU might fail to read the eeprom through I2C
2076 	 * when the GPU is pending on XGMI reset during probe time
2077 	 * (Mostly after second bus reset), skip it now
2078 	 */
2079 	if (adev->gmc.xgmi.pending_reset)
2080 		return 0;
2081 	ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit);
2082 	/*
2083 	 * This calling fails when exc_err_limit is true or
2084 	 * ret != 0.
2085 	 */
2086 	if (exc_err_limit || ret)
2087 		goto free;
2088 
2089 	if (con->eeprom_control.ras_num_recs) {
2090 		ret = amdgpu_ras_load_bad_pages(adev);
2091 		if (ret)
2092 			goto free;
2093 
2094 		if (adev->smu.ppt_funcs && adev->smu.ppt_funcs->send_hbm_bad_pages_num)
2095 			adev->smu.ppt_funcs->send_hbm_bad_pages_num(&adev->smu, con->eeprom_control.ras_num_recs);
2096 	}
2097 
2098 	return 0;
2099 
2100 free:
2101 	kfree((*data)->bps);
2102 	kfree(*data);
2103 	con->eh_data = NULL;
2104 out:
2105 	dev_warn(adev->dev, "Failed to initialize ras recovery! (%d)\n", ret);
2106 
2107 	/*
2108 	 * Except error threshold exceeding case, other failure cases in this
2109 	 * function would not fail amdgpu driver init.
2110 	 */
2111 	if (!exc_err_limit)
2112 		ret = 0;
2113 	else
2114 		ret = -EINVAL;
2115 
2116 	return ret;
2117 }
2118 
2119 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
2120 {
2121 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2122 	struct ras_err_handler_data *data = con->eh_data;
2123 
2124 	/* recovery_init failed to init it, fini is useless */
2125 	if (!data)
2126 		return 0;
2127 
2128 	cancel_work_sync(&con->recovery_work);
2129 
2130 	mutex_lock(&con->recovery_lock);
2131 	con->eh_data = NULL;
2132 	kfree(data->bps);
2133 	kfree(data);
2134 	mutex_unlock(&con->recovery_lock);
2135 
2136 	return 0;
2137 }
2138 /* recovery end */
2139 
2140 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev)
2141 {
2142 	return adev->asic_type == CHIP_VEGA10 ||
2143 		adev->asic_type == CHIP_VEGA20 ||
2144 		adev->asic_type == CHIP_ARCTURUS ||
2145 		adev->asic_type == CHIP_ALDEBARAN ||
2146 		adev->asic_type == CHIP_SIENNA_CICHLID;
2147 }
2148 
2149 /*
2150  * this is workaround for vega20 workstation sku,
2151  * force enable gfx ras, ignore vbios gfx ras flag
2152  * due to GC EDC can not write
2153  */
2154 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev)
2155 {
2156 	struct atom_context *ctx = adev->mode_info.atom_context;
2157 
2158 	if (!ctx)
2159 		return;
2160 
2161 	if (strnstr(ctx->vbios_version, "D16406",
2162 		    sizeof(ctx->vbios_version)) ||
2163 		strnstr(ctx->vbios_version, "D36002",
2164 			sizeof(ctx->vbios_version)))
2165 		adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX);
2166 }
2167 
2168 /*
2169  * check hardware's ras ability which will be saved in hw_supported.
2170  * if hardware does not support ras, we can skip some ras initializtion and
2171  * forbid some ras operations from IP.
2172  * if software itself, say boot parameter, limit the ras ability. We still
2173  * need allow IP do some limited operations, like disable. In such case,
2174  * we have to initialize ras as normal. but need check if operation is
2175  * allowed or not in each function.
2176  */
2177 static void amdgpu_ras_check_supported(struct amdgpu_device *adev)
2178 {
2179 	adev->ras_hw_enabled = adev->ras_enabled = 0;
2180 
2181 	if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
2182 	    !amdgpu_ras_asic_supported(adev))
2183 		return;
2184 
2185 	if (!adev->gmc.xgmi.connected_to_cpu) {
2186 		if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
2187 			dev_info(adev->dev, "MEM ECC is active.\n");
2188 			adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC |
2189 						   1 << AMDGPU_RAS_BLOCK__DF);
2190 		} else {
2191 			dev_info(adev->dev, "MEM ECC is not presented.\n");
2192 		}
2193 
2194 		if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
2195 			dev_info(adev->dev, "SRAM ECC is active.\n");
2196 			adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
2197 						    1 << AMDGPU_RAS_BLOCK__DF);
2198 		} else {
2199 			dev_info(adev->dev, "SRAM ECC is not presented.\n");
2200 		}
2201 	} else {
2202 		/* driver only manages a few IP blocks RAS feature
2203 		 * when GPU is connected cpu through XGMI */
2204 		adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX |
2205 					   1 << AMDGPU_RAS_BLOCK__SDMA |
2206 					   1 << AMDGPU_RAS_BLOCK__MMHUB);
2207 	}
2208 
2209 	amdgpu_ras_get_quirks(adev);
2210 
2211 	/* hw_supported needs to be aligned with RAS block mask. */
2212 	adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK;
2213 
2214 	adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 :
2215 		adev->ras_hw_enabled & amdgpu_ras_mask;
2216 }
2217 
2218 static void amdgpu_ras_counte_dw(struct work_struct *work)
2219 {
2220 	struct amdgpu_ras *con = container_of(work, struct amdgpu_ras,
2221 					      ras_counte_delay_work.work);
2222 	struct amdgpu_device *adev = con->adev;
2223 	struct drm_device *dev = adev_to_drm(adev);
2224 	unsigned long ce_count, ue_count;
2225 	int res;
2226 
2227 	res = pm_runtime_get_sync(dev->dev);
2228 	if (res < 0)
2229 		goto Out;
2230 
2231 	/* Cache new values.
2232 	 */
2233 	if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2234 		atomic_set(&con->ras_ce_count, ce_count);
2235 		atomic_set(&con->ras_ue_count, ue_count);
2236 	}
2237 
2238 	pm_runtime_mark_last_busy(dev->dev);
2239 Out:
2240 	pm_runtime_put_autosuspend(dev->dev);
2241 }
2242 
2243 int amdgpu_ras_init(struct amdgpu_device *adev)
2244 {
2245 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2246 	int r;
2247 	bool df_poison, umc_poison;
2248 
2249 	if (con)
2250 		return 0;
2251 
2252 	con = kmalloc(sizeof(struct amdgpu_ras) +
2253 			sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT +
2254 			sizeof(struct ras_manager) * AMDGPU_RAS_MCA_BLOCK_COUNT,
2255 			GFP_KERNEL|__GFP_ZERO);
2256 	if (!con)
2257 		return -ENOMEM;
2258 
2259 	con->adev = adev;
2260 	INIT_DELAYED_WORK(&con->ras_counte_delay_work, amdgpu_ras_counte_dw);
2261 	atomic_set(&con->ras_ce_count, 0);
2262 	atomic_set(&con->ras_ue_count, 0);
2263 
2264 	con->objs = (struct ras_manager *)(con + 1);
2265 
2266 	amdgpu_ras_set_context(adev, con);
2267 
2268 	amdgpu_ras_check_supported(adev);
2269 
2270 	if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) {
2271 		/* set gfx block ras context feature for VEGA20 Gaming
2272 		 * send ras disable cmd to ras ta during ras late init.
2273 		 */
2274 		if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) {
2275 			con->features |= BIT(AMDGPU_RAS_BLOCK__GFX);
2276 
2277 			return 0;
2278 		}
2279 
2280 		r = 0;
2281 		goto release_con;
2282 	}
2283 
2284 	con->features = 0;
2285 	INIT_LIST_HEAD(&con->head);
2286 	/* Might need get this flag from vbios. */
2287 	con->flags = RAS_DEFAULT_FLAGS;
2288 
2289 	/* initialize nbio ras function ahead of any other
2290 	 * ras functions so hardware fatal error interrupt
2291 	 * can be enabled as early as possible */
2292 	switch (adev->asic_type) {
2293 	case CHIP_VEGA20:
2294 	case CHIP_ARCTURUS:
2295 	case CHIP_ALDEBARAN:
2296 		if (!adev->gmc.xgmi.connected_to_cpu)
2297 			adev->nbio.ras_funcs = &nbio_v7_4_ras_funcs;
2298 		break;
2299 	default:
2300 		/* nbio ras is not available */
2301 		break;
2302 	}
2303 
2304 	if (adev->nbio.ras_funcs &&
2305 	    adev->nbio.ras_funcs->init_ras_controller_interrupt) {
2306 		r = adev->nbio.ras_funcs->init_ras_controller_interrupt(adev);
2307 		if (r)
2308 			goto release_con;
2309 	}
2310 
2311 	if (adev->nbio.ras_funcs &&
2312 	    adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt) {
2313 		r = adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt(adev);
2314 		if (r)
2315 			goto release_con;
2316 	}
2317 
2318 	/* Init poison supported flag, the default value is false */
2319 	if (adev->df.funcs &&
2320 	    adev->df.funcs->query_ras_poison_mode &&
2321 	    adev->umc.ras_funcs &&
2322 	    adev->umc.ras_funcs->query_ras_poison_mode) {
2323 		df_poison =
2324 			adev->df.funcs->query_ras_poison_mode(adev);
2325 		umc_poison =
2326 			adev->umc.ras_funcs->query_ras_poison_mode(adev);
2327 		/* Only poison is set in both DF and UMC, we can support it */
2328 		if (df_poison && umc_poison)
2329 			con->poison_supported = true;
2330 		else if (df_poison != umc_poison)
2331 			dev_warn(adev->dev, "Poison setting is inconsistent in DF/UMC(%d:%d)!\n",
2332 					df_poison, umc_poison);
2333 	}
2334 
2335 	if (amdgpu_ras_fs_init(adev)) {
2336 		r = -EINVAL;
2337 		goto release_con;
2338 	}
2339 
2340 	dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
2341 		 "hardware ability[%x] ras_mask[%x]\n",
2342 		 adev->ras_hw_enabled, adev->ras_enabled);
2343 
2344 	return 0;
2345 release_con:
2346 	amdgpu_ras_set_context(adev, NULL);
2347 	kfree(con);
2348 
2349 	return r;
2350 }
2351 
2352 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev)
2353 {
2354 	if (adev->gmc.xgmi.connected_to_cpu)
2355 		return 1;
2356 	return 0;
2357 }
2358 
2359 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev,
2360 					struct ras_common_if *ras_block)
2361 {
2362 	struct ras_query_if info = {
2363 		.head = *ras_block,
2364 	};
2365 
2366 	if (!amdgpu_persistent_edc_harvesting_supported(adev))
2367 		return 0;
2368 
2369 	if (amdgpu_ras_query_error_status(adev, &info) != 0)
2370 		DRM_WARN("RAS init harvest failure");
2371 
2372 	if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0)
2373 		DRM_WARN("RAS init harvest reset failure");
2374 
2375 	return 0;
2376 }
2377 
2378 bool amdgpu_ras_is_poison_mode_supported(struct amdgpu_device *adev)
2379 {
2380        struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2381 
2382        if (!con)
2383                return false;
2384 
2385        return con->poison_supported;
2386 }
2387 
2388 /* helper function to handle common stuff in ip late init phase */
2389 int amdgpu_ras_late_init(struct amdgpu_device *adev,
2390 			 struct ras_common_if *ras_block,
2391 			 struct ras_fs_if *fs_info,
2392 			 struct ras_ih_if *ih_info)
2393 {
2394 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2395 	unsigned long ue_count, ce_count;
2396 	int r;
2397 
2398 	/* disable RAS feature per IP block if it is not supported */
2399 	if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
2400 		amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
2401 		return 0;
2402 	}
2403 
2404 	r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
2405 	if (r) {
2406 		if (adev->in_suspend || amdgpu_in_reset(adev)) {
2407 			/* in resume phase, if fail to enable ras,
2408 			 * clean up all ras fs nodes, and disable ras */
2409 			goto cleanup;
2410 		} else
2411 			return r;
2412 	}
2413 
2414 	/* check for errors on warm reset edc persisant supported ASIC */
2415 	amdgpu_persistent_edc_harvesting(adev, ras_block);
2416 
2417 	/* in resume phase, no need to create ras fs node */
2418 	if (adev->in_suspend || amdgpu_in_reset(adev))
2419 		return 0;
2420 
2421 	if (ih_info->cb) {
2422 		r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
2423 		if (r)
2424 			goto interrupt;
2425 	}
2426 
2427 	r = amdgpu_ras_sysfs_create(adev, fs_info);
2428 	if (r)
2429 		goto sysfs;
2430 
2431 	/* Those are the cached values at init.
2432 	 */
2433 	if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2434 		atomic_set(&con->ras_ce_count, ce_count);
2435 		atomic_set(&con->ras_ue_count, ue_count);
2436 	}
2437 
2438 	return 0;
2439 cleanup:
2440 	amdgpu_ras_sysfs_remove(adev, ras_block);
2441 sysfs:
2442 	if (ih_info->cb)
2443 		amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2444 interrupt:
2445 	amdgpu_ras_feature_enable(adev, ras_block, 0);
2446 	return r;
2447 }
2448 
2449 /* helper function to remove ras fs node and interrupt handler */
2450 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
2451 			  struct ras_common_if *ras_block,
2452 			  struct ras_ih_if *ih_info)
2453 {
2454 	if (!ras_block || !ih_info)
2455 		return;
2456 
2457 	amdgpu_ras_sysfs_remove(adev, ras_block);
2458 	if (ih_info->cb)
2459 		amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2460 	amdgpu_ras_feature_enable(adev, ras_block, 0);
2461 }
2462 
2463 /* do some init work after IP late init as dependence.
2464  * and it runs in resume/gpu reset/booting up cases.
2465  */
2466 void amdgpu_ras_resume(struct amdgpu_device *adev)
2467 {
2468 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2469 	struct ras_manager *obj, *tmp;
2470 
2471 	if (!adev->ras_enabled || !con) {
2472 		/* clean ras context for VEGA20 Gaming after send ras disable cmd */
2473 		amdgpu_release_ras_context(adev);
2474 
2475 		return;
2476 	}
2477 
2478 	if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2479 		/* Set up all other IPs which are not implemented. There is a
2480 		 * tricky thing that IP's actual ras error type should be
2481 		 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2482 		 * ERROR_NONE make sense anyway.
2483 		 */
2484 		amdgpu_ras_enable_all_features(adev, 1);
2485 
2486 		/* We enable ras on all hw_supported block, but as boot
2487 		 * parameter might disable some of them and one or more IP has
2488 		 * not implemented yet. So we disable them on behalf.
2489 		 */
2490 		list_for_each_entry_safe(obj, tmp, &con->head, node) {
2491 			if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2492 				amdgpu_ras_feature_enable(adev, &obj->head, 0);
2493 				/* there should be no any reference. */
2494 				WARN_ON(alive_obj(obj));
2495 			}
2496 		}
2497 	}
2498 }
2499 
2500 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2501 {
2502 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2503 
2504 	if (!adev->ras_enabled || !con)
2505 		return;
2506 
2507 	amdgpu_ras_disable_all_features(adev, 0);
2508 	/* Make sure all ras objects are disabled. */
2509 	if (con->features)
2510 		amdgpu_ras_disable_all_features(adev, 1);
2511 }
2512 
2513 /* do some fini work before IP fini as dependence */
2514 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2515 {
2516 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2517 
2518 	if (!adev->ras_enabled || !con)
2519 		return 0;
2520 
2521 
2522 	/* Need disable ras on all IPs here before ip [hw/sw]fini */
2523 	amdgpu_ras_disable_all_features(adev, 0);
2524 	amdgpu_ras_recovery_fini(adev);
2525 	return 0;
2526 }
2527 
2528 int amdgpu_ras_fini(struct amdgpu_device *adev)
2529 {
2530 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2531 
2532 	if (!adev->ras_enabled || !con)
2533 		return 0;
2534 
2535 	amdgpu_ras_fs_fini(adev);
2536 	amdgpu_ras_interrupt_remove_all(adev);
2537 
2538 	WARN(con->features, "Feature mask is not cleared");
2539 
2540 	if (con->features)
2541 		amdgpu_ras_disable_all_features(adev, 1);
2542 
2543 	cancel_delayed_work_sync(&con->ras_counte_delay_work);
2544 
2545 	amdgpu_ras_set_context(adev, NULL);
2546 	kfree(con);
2547 
2548 	return 0;
2549 }
2550 
2551 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2552 {
2553 	amdgpu_ras_check_supported(adev);
2554 	if (!adev->ras_hw_enabled)
2555 		return;
2556 
2557 	if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2558 		dev_info(adev->dev, "uncorrectable hardware error"
2559 			"(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2560 
2561 		amdgpu_ras_reset_gpu(adev);
2562 	}
2563 }
2564 
2565 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
2566 {
2567 	if (adev->asic_type == CHIP_VEGA20 &&
2568 	    adev->pm.fw_version <= 0x283400) {
2569 		return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
2570 				amdgpu_ras_intr_triggered();
2571 	}
2572 
2573 	return false;
2574 }
2575 
2576 void amdgpu_release_ras_context(struct amdgpu_device *adev)
2577 {
2578 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2579 
2580 	if (!con)
2581 		return;
2582 
2583 	if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) {
2584 		con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX);
2585 		amdgpu_ras_set_context(adev, NULL);
2586 		kfree(con);
2587 	}
2588 }
2589