xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c (revision 569d7db70e5dcf13fbf072f10e9096577ac1e565)
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 #include <linux/list_sort.h>
32 
33 #include "amdgpu.h"
34 #include "amdgpu_ras.h"
35 #include "amdgpu_atomfirmware.h"
36 #include "amdgpu_xgmi.h"
37 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h"
38 #include "nbio_v4_3.h"
39 #include "nbio_v7_9.h"
40 #include "atom.h"
41 #include "amdgpu_reset.h"
42 #include "amdgpu_psp.h"
43 
44 #ifdef CONFIG_X86_MCE_AMD
45 #include <asm/mce.h>
46 
47 static bool notifier_registered;
48 #endif
49 static const char *RAS_FS_NAME = "ras";
50 
51 const char *ras_error_string[] = {
52 	"none",
53 	"parity",
54 	"single_correctable",
55 	"multi_uncorrectable",
56 	"poison",
57 };
58 
59 const char *ras_block_string[] = {
60 	"umc",
61 	"sdma",
62 	"gfx",
63 	"mmhub",
64 	"athub",
65 	"pcie_bif",
66 	"hdp",
67 	"xgmi_wafl",
68 	"df",
69 	"smn",
70 	"sem",
71 	"mp0",
72 	"mp1",
73 	"fuse",
74 	"mca",
75 	"vcn",
76 	"jpeg",
77 	"ih",
78 	"mpio",
79 };
80 
81 const char *ras_mca_block_string[] = {
82 	"mca_mp0",
83 	"mca_mp1",
84 	"mca_mpio",
85 	"mca_iohc",
86 };
87 
88 struct amdgpu_ras_block_list {
89 	/* ras block link */
90 	struct list_head node;
91 
92 	struct amdgpu_ras_block_object *ras_obj;
93 };
94 
95 const char *get_ras_block_str(struct ras_common_if *ras_block)
96 {
97 	if (!ras_block)
98 		return "NULL";
99 
100 	if (ras_block->block >= AMDGPU_RAS_BLOCK_COUNT ||
101 	    ras_block->block >= ARRAY_SIZE(ras_block_string))
102 		return "OUT OF RANGE";
103 
104 	if (ras_block->block == AMDGPU_RAS_BLOCK__MCA)
105 		return ras_mca_block_string[ras_block->sub_block_index];
106 
107 	return ras_block_string[ras_block->block];
108 }
109 
110 #define ras_block_str(_BLOCK_) \
111 	(((_BLOCK_) < ARRAY_SIZE(ras_block_string)) ? ras_block_string[_BLOCK_] : "Out Of Range")
112 
113 #define ras_err_str(i) (ras_error_string[ffs(i)])
114 
115 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
116 
117 /* inject address is 52 bits */
118 #define	RAS_UMC_INJECT_ADDR_LIMIT	(0x1ULL << 52)
119 
120 /* typical ECC bad page rate is 1 bad page per 100MB VRAM */
121 #define RAS_BAD_PAGE_COVER              (100 * 1024 * 1024ULL)
122 
123 #define MAX_UMC_POISON_POLLING_TIME_ASYNC  100  //ms
124 
125 #define AMDGPU_RAS_RETIRE_PAGE_INTERVAL 100  //ms
126 
127 enum amdgpu_ras_retire_page_reservation {
128 	AMDGPU_RAS_RETIRE_PAGE_RESERVED,
129 	AMDGPU_RAS_RETIRE_PAGE_PENDING,
130 	AMDGPU_RAS_RETIRE_PAGE_FAULT,
131 };
132 
133 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
134 
135 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
136 				uint64_t addr);
137 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
138 				uint64_t addr);
139 #ifdef CONFIG_X86_MCE_AMD
140 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev);
141 struct mce_notifier_adev_list {
142 	struct amdgpu_device *devs[MAX_GPU_INSTANCE];
143 	int num_gpu;
144 };
145 static struct mce_notifier_adev_list mce_adev_list;
146 #endif
147 
148 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
149 {
150 	if (adev && amdgpu_ras_get_context(adev))
151 		amdgpu_ras_get_context(adev)->error_query_ready = ready;
152 }
153 
154 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
155 {
156 	if (adev && amdgpu_ras_get_context(adev))
157 		return amdgpu_ras_get_context(adev)->error_query_ready;
158 
159 	return false;
160 }
161 
162 static int amdgpu_reserve_page_direct(struct amdgpu_device *adev, uint64_t address)
163 {
164 	struct ras_err_data err_data;
165 	struct eeprom_table_record err_rec;
166 	int ret;
167 
168 	if ((address >= adev->gmc.mc_vram_size) ||
169 	    (address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
170 		dev_warn(adev->dev,
171 		         "RAS WARN: input address 0x%llx is invalid.\n",
172 		         address);
173 		return -EINVAL;
174 	}
175 
176 	if (amdgpu_ras_check_bad_page(adev, address)) {
177 		dev_warn(adev->dev,
178 			 "RAS WARN: 0x%llx has already been marked as bad page!\n",
179 			 address);
180 		return 0;
181 	}
182 
183 	ret = amdgpu_ras_error_data_init(&err_data);
184 	if (ret)
185 		return ret;
186 
187 	memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
188 	err_data.err_addr = &err_rec;
189 	amdgpu_umc_fill_error_record(&err_data, address, address, 0, 0);
190 
191 	if (amdgpu_bad_page_threshold != 0) {
192 		amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
193 					 err_data.err_addr_cnt);
194 		amdgpu_ras_save_bad_pages(adev, NULL);
195 	}
196 
197 	amdgpu_ras_error_data_fini(&err_data);
198 
199 	dev_warn(adev->dev, "WARNING: THIS IS ONLY FOR TEST PURPOSES AND WILL CORRUPT RAS EEPROM\n");
200 	dev_warn(adev->dev, "Clear EEPROM:\n");
201 	dev_warn(adev->dev, "    echo 1 > /sys/kernel/debug/dri/0/ras/ras_eeprom_reset\n");
202 
203 	return 0;
204 }
205 
206 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
207 					size_t size, loff_t *pos)
208 {
209 	struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
210 	struct ras_query_if info = {
211 		.head = obj->head,
212 	};
213 	ssize_t s;
214 	char val[128];
215 
216 	if (amdgpu_ras_query_error_status(obj->adev, &info))
217 		return -EINVAL;
218 
219 	/* Hardware counter will be reset automatically after the query on Vega20 and Arcturus */
220 	if (amdgpu_ip_version(obj->adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 2) &&
221 	    amdgpu_ip_version(obj->adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 4)) {
222 		if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
223 			dev_warn(obj->adev->dev, "Failed to reset error counter and error status");
224 	}
225 
226 	s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
227 			"ue", info.ue_count,
228 			"ce", info.ce_count);
229 	if (*pos >= s)
230 		return 0;
231 
232 	s -= *pos;
233 	s = min_t(u64, s, size);
234 
235 
236 	if (copy_to_user(buf, &val[*pos], s))
237 		return -EINVAL;
238 
239 	*pos += s;
240 
241 	return s;
242 }
243 
244 static const struct file_operations amdgpu_ras_debugfs_ops = {
245 	.owner = THIS_MODULE,
246 	.read = amdgpu_ras_debugfs_read,
247 	.write = NULL,
248 	.llseek = default_llseek
249 };
250 
251 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
252 {
253 	int i;
254 
255 	for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
256 		*block_id = i;
257 		if (strcmp(name, ras_block_string[i]) == 0)
258 			return 0;
259 	}
260 	return -EINVAL;
261 }
262 
263 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
264 		const char __user *buf, size_t size,
265 		loff_t *pos, struct ras_debug_if *data)
266 {
267 	ssize_t s = min_t(u64, 64, size);
268 	char str[65];
269 	char block_name[33];
270 	char err[9] = "ue";
271 	int op = -1;
272 	int block_id;
273 	uint32_t sub_block;
274 	u64 address, value;
275 	/* default value is 0 if the mask is not set by user */
276 	u32 instance_mask = 0;
277 
278 	if (*pos)
279 		return -EINVAL;
280 	*pos = size;
281 
282 	memset(str, 0, sizeof(str));
283 	memset(data, 0, sizeof(*data));
284 
285 	if (copy_from_user(str, buf, s))
286 		return -EINVAL;
287 
288 	if (sscanf(str, "disable %32s", block_name) == 1)
289 		op = 0;
290 	else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
291 		op = 1;
292 	else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
293 		op = 2;
294 	else if (strstr(str, "retire_page") != NULL)
295 		op = 3;
296 	else if (str[0] && str[1] && str[2] && str[3])
297 		/* ascii string, but commands are not matched. */
298 		return -EINVAL;
299 
300 	if (op != -1) {
301 		if (op == 3) {
302 			if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
303 			    sscanf(str, "%*s %llu", &address) != 1)
304 				return -EINVAL;
305 
306 			data->op = op;
307 			data->inject.address = address;
308 
309 			return 0;
310 		}
311 
312 		if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
313 			return -EINVAL;
314 
315 		data->head.block = block_id;
316 		/* only ue, ce and poison errors are supported */
317 		if (!memcmp("ue", err, 2))
318 			data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
319 		else if (!memcmp("ce", err, 2))
320 			data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
321 		else if (!memcmp("poison", err, 6))
322 			data->head.type = AMDGPU_RAS_ERROR__POISON;
323 		else
324 			return -EINVAL;
325 
326 		data->op = op;
327 
328 		if (op == 2) {
329 			if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx 0x%x",
330 				   &sub_block, &address, &value, &instance_mask) != 4 &&
331 			    sscanf(str, "%*s %*s %*s %u %llu %llu %u",
332 				   &sub_block, &address, &value, &instance_mask) != 4 &&
333 				sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
334 				   &sub_block, &address, &value) != 3 &&
335 			    sscanf(str, "%*s %*s %*s %u %llu %llu",
336 				   &sub_block, &address, &value) != 3)
337 				return -EINVAL;
338 			data->head.sub_block_index = sub_block;
339 			data->inject.address = address;
340 			data->inject.value = value;
341 			data->inject.instance_mask = instance_mask;
342 		}
343 	} else {
344 		if (size < sizeof(*data))
345 			return -EINVAL;
346 
347 		if (copy_from_user(data, buf, sizeof(*data)))
348 			return -EINVAL;
349 	}
350 
351 	return 0;
352 }
353 
354 static void amdgpu_ras_instance_mask_check(struct amdgpu_device *adev,
355 				struct ras_debug_if *data)
356 {
357 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
358 	uint32_t mask, inst_mask = data->inject.instance_mask;
359 
360 	/* no need to set instance mask if there is only one instance */
361 	if (num_xcc <= 1 && inst_mask) {
362 		data->inject.instance_mask = 0;
363 		dev_dbg(adev->dev,
364 			"RAS inject mask(0x%x) isn't supported and force it to 0.\n",
365 			inst_mask);
366 
367 		return;
368 	}
369 
370 	switch (data->head.block) {
371 	case AMDGPU_RAS_BLOCK__GFX:
372 		mask = GENMASK(num_xcc - 1, 0);
373 		break;
374 	case AMDGPU_RAS_BLOCK__SDMA:
375 		mask = GENMASK(adev->sdma.num_instances - 1, 0);
376 		break;
377 	case AMDGPU_RAS_BLOCK__VCN:
378 	case AMDGPU_RAS_BLOCK__JPEG:
379 		mask = GENMASK(adev->vcn.num_vcn_inst - 1, 0);
380 		break;
381 	default:
382 		mask = inst_mask;
383 		break;
384 	}
385 
386 	/* remove invalid bits in instance mask */
387 	data->inject.instance_mask &= mask;
388 	if (inst_mask != data->inject.instance_mask)
389 		dev_dbg(adev->dev,
390 			"Adjust RAS inject mask 0x%x to 0x%x\n",
391 			inst_mask, data->inject.instance_mask);
392 }
393 
394 /**
395  * DOC: AMDGPU RAS debugfs control interface
396  *
397  * The control interface accepts struct ras_debug_if which has two members.
398  *
399  * First member: ras_debug_if::head or ras_debug_if::inject.
400  *
401  * head is used to indicate which IP block will be under control.
402  *
403  * head has four members, they are block, type, sub_block_index, name.
404  * block: which IP will be under control.
405  * type: what kind of error will be enabled/disabled/injected.
406  * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
407  * name: the name of IP.
408  *
409  * inject has three more members than head, they are address, value and mask.
410  * As their names indicate, inject operation will write the
411  * value to the address.
412  *
413  * The second member: struct ras_debug_if::op.
414  * It has three kinds of operations.
415  *
416  * - 0: disable RAS on the block. Take ::head as its data.
417  * - 1: enable RAS on the block. Take ::head as its data.
418  * - 2: inject errors on the block. Take ::inject as its data.
419  *
420  * How to use the interface?
421  *
422  * In a program
423  *
424  * Copy the struct ras_debug_if in your code and initialize it.
425  * Write the struct to the control interface.
426  *
427  * From shell
428  *
429  * .. code-block:: bash
430  *
431  *	echo "disable <block>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
432  *	echo "enable  <block> <error>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
433  *	echo "inject  <block> <error> <sub-block> <address> <value> <mask>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
434  *
435  * Where N, is the card which you want to affect.
436  *
437  * "disable" requires only the block.
438  * "enable" requires the block and error type.
439  * "inject" requires the block, error type, address, and value.
440  *
441  * The block is one of: umc, sdma, gfx, etc.
442  *	see ras_block_string[] for details
443  *
444  * The error type is one of: ue, ce and poison where,
445  *	ue is multi-uncorrectable
446  *	ce is single-correctable
447  *	poison is poison
448  *
449  * The sub-block is a the sub-block index, pass 0 if there is no sub-block.
450  * The address and value are hexadecimal numbers, leading 0x is optional.
451  * The mask means instance mask, is optional, default value is 0x1.
452  *
453  * For instance,
454  *
455  * .. code-block:: bash
456  *
457  *	echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
458  *	echo inject umc ce 0 0 0 3 > /sys/kernel/debug/dri/0/ras/ras_ctrl
459  *	echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
460  *
461  * How to check the result of the operation?
462  *
463  * To check disable/enable, see "ras" features at,
464  * /sys/class/drm/card[0/1/2...]/device/ras/features
465  *
466  * To check inject, see the corresponding error count at,
467  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx|sdma|umc|...]_err_count
468  *
469  * .. note::
470  *	Operations are only allowed on blocks which are supported.
471  *	Check the "ras" mask at /sys/module/amdgpu/parameters/ras_mask
472  *	to see which blocks support RAS on a particular asic.
473  *
474  */
475 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f,
476 					     const char __user *buf,
477 					     size_t size, loff_t *pos)
478 {
479 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
480 	struct ras_debug_if data;
481 	int ret = 0;
482 
483 	if (!amdgpu_ras_get_error_query_ready(adev)) {
484 		dev_warn(adev->dev, "RAS WARN: error injection "
485 				"currently inaccessible\n");
486 		return size;
487 	}
488 
489 	ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
490 	if (ret)
491 		return ret;
492 
493 	if (data.op == 3) {
494 		ret = amdgpu_reserve_page_direct(adev, data.inject.address);
495 		if (!ret)
496 			return size;
497 		else
498 			return ret;
499 	}
500 
501 	if (!amdgpu_ras_is_supported(adev, data.head.block))
502 		return -EINVAL;
503 
504 	switch (data.op) {
505 	case 0:
506 		ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
507 		break;
508 	case 1:
509 		ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
510 		break;
511 	case 2:
512 		if ((data.inject.address >= adev->gmc.mc_vram_size &&
513 		    adev->gmc.mc_vram_size) ||
514 		    (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
515 			dev_warn(adev->dev, "RAS WARN: input address "
516 					"0x%llx is invalid.",
517 					data.inject.address);
518 			ret = -EINVAL;
519 			break;
520 		}
521 
522 		/* umc ce/ue error injection for a bad page is not allowed */
523 		if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
524 		    amdgpu_ras_check_bad_page(adev, data.inject.address)) {
525 			dev_warn(adev->dev, "RAS WARN: inject: 0x%llx has "
526 				 "already been marked as bad!\n",
527 				 data.inject.address);
528 			break;
529 		}
530 
531 		amdgpu_ras_instance_mask_check(adev, &data);
532 
533 		/* data.inject.address is offset instead of absolute gpu address */
534 		ret = amdgpu_ras_error_inject(adev, &data.inject);
535 		break;
536 	default:
537 		ret = -EINVAL;
538 		break;
539 	}
540 
541 	if (ret)
542 		return ret;
543 
544 	return size;
545 }
546 
547 /**
548  * DOC: AMDGPU RAS debugfs EEPROM table reset interface
549  *
550  * Some boards contain an EEPROM which is used to persistently store a list of
551  * bad pages which experiences ECC errors in vram.  This interface provides
552  * a way to reset the EEPROM, e.g., after testing error injection.
553  *
554  * Usage:
555  *
556  * .. code-block:: bash
557  *
558  *	echo 1 > ../ras/ras_eeprom_reset
559  *
560  * will reset EEPROM table to 0 entries.
561  *
562  */
563 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f,
564 					       const char __user *buf,
565 					       size_t size, loff_t *pos)
566 {
567 	struct amdgpu_device *adev =
568 		(struct amdgpu_device *)file_inode(f)->i_private;
569 	int ret;
570 
571 	ret = amdgpu_ras_eeprom_reset_table(
572 		&(amdgpu_ras_get_context(adev)->eeprom_control));
573 
574 	if (!ret) {
575 		/* Something was written to EEPROM.
576 		 */
577 		amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS;
578 		return size;
579 	} else {
580 		return ret;
581 	}
582 }
583 
584 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
585 	.owner = THIS_MODULE,
586 	.read = NULL,
587 	.write = amdgpu_ras_debugfs_ctrl_write,
588 	.llseek = default_llseek
589 };
590 
591 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
592 	.owner = THIS_MODULE,
593 	.read = NULL,
594 	.write = amdgpu_ras_debugfs_eeprom_write,
595 	.llseek = default_llseek
596 };
597 
598 /**
599  * DOC: AMDGPU RAS sysfs Error Count Interface
600  *
601  * It allows the user to read the error count for each IP block on the gpu through
602  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
603  *
604  * It outputs the multiple lines which report the uncorrected (ue) and corrected
605  * (ce) error counts.
606  *
607  * The format of one line is below,
608  *
609  * [ce|ue]: count
610  *
611  * Example:
612  *
613  * .. code-block:: bash
614  *
615  *	ue: 0
616  *	ce: 1
617  *
618  */
619 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
620 		struct device_attribute *attr, char *buf)
621 {
622 	struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
623 	struct ras_query_if info = {
624 		.head = obj->head,
625 	};
626 
627 	if (!amdgpu_ras_get_error_query_ready(obj->adev))
628 		return sysfs_emit(buf, "Query currently inaccessible\n");
629 
630 	if (amdgpu_ras_query_error_status(obj->adev, &info))
631 		return -EINVAL;
632 
633 	if (amdgpu_ip_version(obj->adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 2) &&
634 	    amdgpu_ip_version(obj->adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 4)) {
635 		if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
636 			dev_warn(obj->adev->dev, "Failed to reset error counter and error status");
637 	}
638 
639 	if (info.head.block == AMDGPU_RAS_BLOCK__UMC)
640 		return sysfs_emit(buf, "%s: %lu\n%s: %lu\n%s: %lu\n", "ue", info.ue_count,
641 				"ce", info.ce_count, "de", info.de_count);
642 	else
643 		return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count,
644 				"ce", info.ce_count);
645 }
646 
647 /* obj begin */
648 
649 #define get_obj(obj) do { (obj)->use++; } while (0)
650 #define alive_obj(obj) ((obj)->use)
651 
652 static inline void put_obj(struct ras_manager *obj)
653 {
654 	if (obj && (--obj->use == 0)) {
655 		list_del(&obj->node);
656 		amdgpu_ras_error_data_fini(&obj->err_data);
657 	}
658 
659 	if (obj && (obj->use < 0))
660 		DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", get_ras_block_str(&obj->head));
661 }
662 
663 /* make one obj and return it. */
664 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
665 		struct ras_common_if *head)
666 {
667 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
668 	struct ras_manager *obj;
669 
670 	if (!adev->ras_enabled || !con)
671 		return NULL;
672 
673 	if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
674 		return NULL;
675 
676 	if (head->block == AMDGPU_RAS_BLOCK__MCA) {
677 		if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
678 			return NULL;
679 
680 		obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
681 	} else
682 		obj = &con->objs[head->block];
683 
684 	/* already exist. return obj? */
685 	if (alive_obj(obj))
686 		return NULL;
687 
688 	if (amdgpu_ras_error_data_init(&obj->err_data))
689 		return NULL;
690 
691 	obj->head = *head;
692 	obj->adev = adev;
693 	list_add(&obj->node, &con->head);
694 	get_obj(obj);
695 
696 	return obj;
697 }
698 
699 /* return an obj equal to head, or the first when head is NULL */
700 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
701 		struct ras_common_if *head)
702 {
703 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
704 	struct ras_manager *obj;
705 	int i;
706 
707 	if (!adev->ras_enabled || !con)
708 		return NULL;
709 
710 	if (head) {
711 		if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
712 			return NULL;
713 
714 		if (head->block == AMDGPU_RAS_BLOCK__MCA) {
715 			if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
716 				return NULL;
717 
718 			obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
719 		} else
720 			obj = &con->objs[head->block];
721 
722 		if (alive_obj(obj))
723 			return obj;
724 	} else {
725 		for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT + AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
726 			obj = &con->objs[i];
727 			if (alive_obj(obj))
728 				return obj;
729 		}
730 	}
731 
732 	return NULL;
733 }
734 /* obj end */
735 
736 /* feature ctl begin */
737 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
738 					 struct ras_common_if *head)
739 {
740 	return adev->ras_hw_enabled & BIT(head->block);
741 }
742 
743 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
744 		struct ras_common_if *head)
745 {
746 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
747 
748 	return con->features & BIT(head->block);
749 }
750 
751 /*
752  * if obj is not created, then create one.
753  * set feature enable flag.
754  */
755 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
756 		struct ras_common_if *head, int enable)
757 {
758 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
759 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
760 
761 	/* If hardware does not support ras, then do not create obj.
762 	 * But if hardware support ras, we can create the obj.
763 	 * Ras framework checks con->hw_supported to see if it need do
764 	 * corresponding initialization.
765 	 * IP checks con->support to see if it need disable ras.
766 	 */
767 	if (!amdgpu_ras_is_feature_allowed(adev, head))
768 		return 0;
769 
770 	if (enable) {
771 		if (!obj) {
772 			obj = amdgpu_ras_create_obj(adev, head);
773 			if (!obj)
774 				return -EINVAL;
775 		} else {
776 			/* In case we create obj somewhere else */
777 			get_obj(obj);
778 		}
779 		con->features |= BIT(head->block);
780 	} else {
781 		if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
782 			con->features &= ~BIT(head->block);
783 			put_obj(obj);
784 		}
785 	}
786 
787 	return 0;
788 }
789 
790 /* wrapper of psp_ras_enable_features */
791 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
792 		struct ras_common_if *head, bool enable)
793 {
794 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
795 	union ta_ras_cmd_input *info;
796 	int ret;
797 
798 	if (!con)
799 		return -EINVAL;
800 
801 	/* For non-gfx ip, do not enable ras feature if it is not allowed */
802 	/* For gfx ip, regardless of feature support status, */
803 	/* Force issue enable or disable ras feature commands */
804 	if (head->block != AMDGPU_RAS_BLOCK__GFX &&
805 	    !amdgpu_ras_is_feature_allowed(adev, head))
806 		return 0;
807 
808 	/* Only enable gfx ras feature from host side */
809 	if (head->block == AMDGPU_RAS_BLOCK__GFX &&
810 	    !amdgpu_sriov_vf(adev) &&
811 	    !amdgpu_ras_intr_triggered()) {
812 		info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
813 		if (!info)
814 			return -ENOMEM;
815 
816 		if (!enable) {
817 			info->disable_features = (struct ta_ras_disable_features_input) {
818 				.block_id =  amdgpu_ras_block_to_ta(head->block),
819 				.error_type = amdgpu_ras_error_to_ta(head->type),
820 			};
821 		} else {
822 			info->enable_features = (struct ta_ras_enable_features_input) {
823 				.block_id =  amdgpu_ras_block_to_ta(head->block),
824 				.error_type = amdgpu_ras_error_to_ta(head->type),
825 			};
826 		}
827 
828 		ret = psp_ras_enable_features(&adev->psp, info, enable);
829 		if (ret) {
830 			dev_err(adev->dev, "ras %s %s failed poison:%d ret:%d\n",
831 				enable ? "enable":"disable",
832 				get_ras_block_str(head),
833 				amdgpu_ras_is_poison_mode_supported(adev), ret);
834 			kfree(info);
835 			return ret;
836 		}
837 
838 		kfree(info);
839 	}
840 
841 	/* setup the obj */
842 	__amdgpu_ras_feature_enable(adev, head, enable);
843 
844 	return 0;
845 }
846 
847 /* Only used in device probe stage and called only once. */
848 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
849 		struct ras_common_if *head, bool enable)
850 {
851 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
852 	int ret;
853 
854 	if (!con)
855 		return -EINVAL;
856 
857 	if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
858 		if (enable) {
859 			/* There is no harm to issue a ras TA cmd regardless of
860 			 * the currecnt ras state.
861 			 * If current state == target state, it will do nothing
862 			 * But sometimes it requests driver to reset and repost
863 			 * with error code -EAGAIN.
864 			 */
865 			ret = amdgpu_ras_feature_enable(adev, head, 1);
866 			/* With old ras TA, we might fail to enable ras.
867 			 * Log it and just setup the object.
868 			 * TODO need remove this WA in the future.
869 			 */
870 			if (ret == -EINVAL) {
871 				ret = __amdgpu_ras_feature_enable(adev, head, 1);
872 				if (!ret)
873 					dev_info(adev->dev,
874 						"RAS INFO: %s setup object\n",
875 						get_ras_block_str(head));
876 			}
877 		} else {
878 			/* setup the object then issue a ras TA disable cmd.*/
879 			ret = __amdgpu_ras_feature_enable(adev, head, 1);
880 			if (ret)
881 				return ret;
882 
883 			/* gfx block ras dsiable cmd must send to ras-ta */
884 			if (head->block == AMDGPU_RAS_BLOCK__GFX)
885 				con->features |= BIT(head->block);
886 
887 			ret = amdgpu_ras_feature_enable(adev, head, 0);
888 
889 			/* clean gfx block ras features flag */
890 			if (adev->ras_enabled && head->block == AMDGPU_RAS_BLOCK__GFX)
891 				con->features &= ~BIT(head->block);
892 		}
893 	} else
894 		ret = amdgpu_ras_feature_enable(adev, head, enable);
895 
896 	return ret;
897 }
898 
899 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
900 		bool bypass)
901 {
902 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
903 	struct ras_manager *obj, *tmp;
904 
905 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
906 		/* bypass psp.
907 		 * aka just release the obj and corresponding flags
908 		 */
909 		if (bypass) {
910 			if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
911 				break;
912 		} else {
913 			if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
914 				break;
915 		}
916 	}
917 
918 	return con->features;
919 }
920 
921 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
922 		bool bypass)
923 {
924 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
925 	int i;
926 	const enum amdgpu_ras_error_type default_ras_type = AMDGPU_RAS_ERROR__NONE;
927 
928 	for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
929 		struct ras_common_if head = {
930 			.block = i,
931 			.type = default_ras_type,
932 			.sub_block_index = 0,
933 		};
934 
935 		if (i == AMDGPU_RAS_BLOCK__MCA)
936 			continue;
937 
938 		if (bypass) {
939 			/*
940 			 * bypass psp. vbios enable ras for us.
941 			 * so just create the obj
942 			 */
943 			if (__amdgpu_ras_feature_enable(adev, &head, 1))
944 				break;
945 		} else {
946 			if (amdgpu_ras_feature_enable(adev, &head, 1))
947 				break;
948 		}
949 	}
950 
951 	for (i = 0; i < AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
952 		struct ras_common_if head = {
953 			.block = AMDGPU_RAS_BLOCK__MCA,
954 			.type = default_ras_type,
955 			.sub_block_index = i,
956 		};
957 
958 		if (bypass) {
959 			/*
960 			 * bypass psp. vbios enable ras for us.
961 			 * so just create the obj
962 			 */
963 			if (__amdgpu_ras_feature_enable(adev, &head, 1))
964 				break;
965 		} else {
966 			if (amdgpu_ras_feature_enable(adev, &head, 1))
967 				break;
968 		}
969 	}
970 
971 	return con->features;
972 }
973 /* feature ctl end */
974 
975 static int amdgpu_ras_block_match_default(struct amdgpu_ras_block_object *block_obj,
976 		enum amdgpu_ras_block block)
977 {
978 	if (!block_obj)
979 		return -EINVAL;
980 
981 	if (block_obj->ras_comm.block == block)
982 		return 0;
983 
984 	return -EINVAL;
985 }
986 
987 static struct amdgpu_ras_block_object *amdgpu_ras_get_ras_block(struct amdgpu_device *adev,
988 					enum amdgpu_ras_block block, uint32_t sub_block_index)
989 {
990 	struct amdgpu_ras_block_list *node, *tmp;
991 	struct amdgpu_ras_block_object *obj;
992 
993 	if (block >= AMDGPU_RAS_BLOCK__LAST)
994 		return NULL;
995 
996 	list_for_each_entry_safe(node, tmp, &adev->ras_list, node) {
997 		if (!node->ras_obj) {
998 			dev_warn(adev->dev, "Warning: abnormal ras list node.\n");
999 			continue;
1000 		}
1001 
1002 		obj = node->ras_obj;
1003 		if (obj->ras_block_match) {
1004 			if (obj->ras_block_match(obj, block, sub_block_index) == 0)
1005 				return obj;
1006 		} else {
1007 			if (amdgpu_ras_block_match_default(obj, block) == 0)
1008 				return obj;
1009 		}
1010 	}
1011 
1012 	return NULL;
1013 }
1014 
1015 static void amdgpu_ras_get_ecc_info(struct amdgpu_device *adev, struct ras_err_data *err_data)
1016 {
1017 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1018 	int ret = 0;
1019 
1020 	/*
1021 	 * choosing right query method according to
1022 	 * whether smu support query error information
1023 	 */
1024 	ret = amdgpu_dpm_get_ecc_info(adev, (void *)&(ras->umc_ecc));
1025 	if (ret == -EOPNOTSUPP) {
1026 		if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops &&
1027 			adev->umc.ras->ras_block.hw_ops->query_ras_error_count)
1028 			adev->umc.ras->ras_block.hw_ops->query_ras_error_count(adev, err_data);
1029 
1030 		/* umc query_ras_error_address is also responsible for clearing
1031 		 * error status
1032 		 */
1033 		if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops &&
1034 		    adev->umc.ras->ras_block.hw_ops->query_ras_error_address)
1035 			adev->umc.ras->ras_block.hw_ops->query_ras_error_address(adev, err_data);
1036 	} else if (!ret) {
1037 		if (adev->umc.ras &&
1038 			adev->umc.ras->ecc_info_query_ras_error_count)
1039 			adev->umc.ras->ecc_info_query_ras_error_count(adev, err_data);
1040 
1041 		if (adev->umc.ras &&
1042 			adev->umc.ras->ecc_info_query_ras_error_address)
1043 			adev->umc.ras->ecc_info_query_ras_error_address(adev, err_data);
1044 	}
1045 }
1046 
1047 static void amdgpu_ras_error_print_error_data(struct amdgpu_device *adev,
1048 					      struct ras_manager *ras_mgr,
1049 					      struct ras_err_data *err_data,
1050 					      struct ras_query_context *qctx,
1051 					      const char *blk_name,
1052 					      bool is_ue,
1053 					      bool is_de)
1054 {
1055 	struct amdgpu_smuio_mcm_config_info *mcm_info;
1056 	struct ras_err_node *err_node;
1057 	struct ras_err_info *err_info;
1058 	u64 event_id = qctx->event_id;
1059 
1060 	if (is_ue) {
1061 		for_each_ras_error(err_node, err_data) {
1062 			err_info = &err_node->err_info;
1063 			mcm_info = &err_info->mcm_info;
1064 			if (err_info->ue_count) {
1065 				RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, "
1066 					      "%lld new uncorrectable hardware errors detected in %s block\n",
1067 					      mcm_info->socket_id,
1068 					      mcm_info->die_id,
1069 					      err_info->ue_count,
1070 					      blk_name);
1071 			}
1072 		}
1073 
1074 		for_each_ras_error(err_node, &ras_mgr->err_data) {
1075 			err_info = &err_node->err_info;
1076 			mcm_info = &err_info->mcm_info;
1077 			RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, "
1078 				      "%lld uncorrectable hardware errors detected in total in %s block\n",
1079 				      mcm_info->socket_id, mcm_info->die_id, err_info->ue_count, blk_name);
1080 		}
1081 
1082 	} else {
1083 		if (is_de) {
1084 			for_each_ras_error(err_node, err_data) {
1085 				err_info = &err_node->err_info;
1086 				mcm_info = &err_info->mcm_info;
1087 				if (err_info->de_count) {
1088 					RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, "
1089 						      "%lld new deferred hardware errors detected in %s block\n",
1090 						      mcm_info->socket_id,
1091 						      mcm_info->die_id,
1092 						      err_info->de_count,
1093 						      blk_name);
1094 				}
1095 			}
1096 
1097 			for_each_ras_error(err_node, &ras_mgr->err_data) {
1098 				err_info = &err_node->err_info;
1099 				mcm_info = &err_info->mcm_info;
1100 				RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, "
1101 					      "%lld deferred hardware errors detected in total in %s block\n",
1102 					      mcm_info->socket_id, mcm_info->die_id,
1103 					      err_info->de_count, blk_name);
1104 			}
1105 		} else {
1106 			for_each_ras_error(err_node, err_data) {
1107 				err_info = &err_node->err_info;
1108 				mcm_info = &err_info->mcm_info;
1109 				if (err_info->ce_count) {
1110 					RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, "
1111 						      "%lld new correctable hardware errors detected in %s block\n",
1112 						      mcm_info->socket_id,
1113 						      mcm_info->die_id,
1114 						      err_info->ce_count,
1115 						      blk_name);
1116 				}
1117 			}
1118 
1119 			for_each_ras_error(err_node, &ras_mgr->err_data) {
1120 				err_info = &err_node->err_info;
1121 				mcm_info = &err_info->mcm_info;
1122 				RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, "
1123 					      "%lld correctable hardware errors detected in total in %s block\n",
1124 					      mcm_info->socket_id, mcm_info->die_id,
1125 					      err_info->ce_count, blk_name);
1126 			}
1127 		}
1128 	}
1129 }
1130 
1131 static inline bool err_data_has_source_info(struct ras_err_data *data)
1132 {
1133 	return !list_empty(&data->err_node_list);
1134 }
1135 
1136 static void amdgpu_ras_error_generate_report(struct amdgpu_device *adev,
1137 					     struct ras_query_if *query_if,
1138 					     struct ras_err_data *err_data,
1139 					     struct ras_query_context *qctx)
1140 {
1141 	struct ras_manager *ras_mgr = amdgpu_ras_find_obj(adev, &query_if->head);
1142 	const char *blk_name = get_ras_block_str(&query_if->head);
1143 	u64 event_id = qctx->event_id;
1144 
1145 	if (err_data->ce_count) {
1146 		if (err_data_has_source_info(err_data)) {
1147 			amdgpu_ras_error_print_error_data(adev, ras_mgr, err_data, qctx,
1148 							  blk_name, false, false);
1149 		} else if (!adev->aid_mask &&
1150 			   adev->smuio.funcs &&
1151 			   adev->smuio.funcs->get_socket_id &&
1152 			   adev->smuio.funcs->get_die_id) {
1153 			RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d "
1154 				      "%ld correctable hardware errors "
1155 				      "detected in %s block\n",
1156 				      adev->smuio.funcs->get_socket_id(adev),
1157 				      adev->smuio.funcs->get_die_id(adev),
1158 				      ras_mgr->err_data.ce_count,
1159 				      blk_name);
1160 		} else {
1161 			RAS_EVENT_LOG(adev, event_id, "%ld correctable hardware errors "
1162 				      "detected in %s block\n",
1163 				      ras_mgr->err_data.ce_count,
1164 				      blk_name);
1165 		}
1166 	}
1167 
1168 	if (err_data->ue_count) {
1169 		if (err_data_has_source_info(err_data)) {
1170 			amdgpu_ras_error_print_error_data(adev, ras_mgr, err_data, qctx,
1171 							  blk_name, true, false);
1172 		} else if (!adev->aid_mask &&
1173 			   adev->smuio.funcs &&
1174 			   adev->smuio.funcs->get_socket_id &&
1175 			   adev->smuio.funcs->get_die_id) {
1176 			RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d "
1177 				      "%ld uncorrectable hardware errors "
1178 				      "detected in %s block\n",
1179 				      adev->smuio.funcs->get_socket_id(adev),
1180 				      adev->smuio.funcs->get_die_id(adev),
1181 				      ras_mgr->err_data.ue_count,
1182 				      blk_name);
1183 		} else {
1184 			RAS_EVENT_LOG(adev, event_id, "%ld uncorrectable hardware errors "
1185 				      "detected in %s block\n",
1186 				      ras_mgr->err_data.ue_count,
1187 				      blk_name);
1188 		}
1189 	}
1190 
1191 	if (err_data->de_count) {
1192 		if (err_data_has_source_info(err_data)) {
1193 			amdgpu_ras_error_print_error_data(adev, ras_mgr, err_data, qctx,
1194 							  blk_name, false, true);
1195 		} else if (!adev->aid_mask &&
1196 			   adev->smuio.funcs &&
1197 			   adev->smuio.funcs->get_socket_id &&
1198 			   adev->smuio.funcs->get_die_id) {
1199 			RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d "
1200 				      "%ld deferred hardware errors "
1201 				      "detected in %s block\n",
1202 				      adev->smuio.funcs->get_socket_id(adev),
1203 				      adev->smuio.funcs->get_die_id(adev),
1204 				      ras_mgr->err_data.de_count,
1205 				      blk_name);
1206 		} else {
1207 			RAS_EVENT_LOG(adev, event_id, "%ld deferred hardware errors "
1208 				      "detected in %s block\n",
1209 				      ras_mgr->err_data.de_count,
1210 				      blk_name);
1211 		}
1212 	}
1213 }
1214 
1215 static void amdgpu_rasmgr_error_data_statistic_update(struct ras_manager *obj, struct ras_err_data *err_data)
1216 {
1217 	struct ras_err_node *err_node;
1218 	struct ras_err_info *err_info;
1219 
1220 	if (err_data_has_source_info(err_data)) {
1221 		for_each_ras_error(err_node, err_data) {
1222 			err_info = &err_node->err_info;
1223 			amdgpu_ras_error_statistic_de_count(&obj->err_data,
1224 					&err_info->mcm_info, NULL, err_info->de_count);
1225 			amdgpu_ras_error_statistic_ce_count(&obj->err_data,
1226 					&err_info->mcm_info, NULL, err_info->ce_count);
1227 			amdgpu_ras_error_statistic_ue_count(&obj->err_data,
1228 					&err_info->mcm_info, NULL, err_info->ue_count);
1229 		}
1230 	} else {
1231 		/* for legacy asic path which doesn't has error source info */
1232 		obj->err_data.ue_count += err_data->ue_count;
1233 		obj->err_data.ce_count += err_data->ce_count;
1234 		obj->err_data.de_count += err_data->de_count;
1235 	}
1236 }
1237 
1238 static struct ras_manager *get_ras_manager(struct amdgpu_device *adev, enum amdgpu_ras_block blk)
1239 {
1240 	struct ras_common_if head;
1241 
1242 	memset(&head, 0, sizeof(head));
1243 	head.block = blk;
1244 
1245 	return amdgpu_ras_find_obj(adev, &head);
1246 }
1247 
1248 int amdgpu_ras_bind_aca(struct amdgpu_device *adev, enum amdgpu_ras_block blk,
1249 			const struct aca_info *aca_info, void *data)
1250 {
1251 	struct ras_manager *obj;
1252 
1253 	/* in resume phase, no need to create aca fs node */
1254 	if (adev->in_suspend || amdgpu_in_reset(adev))
1255 		return 0;
1256 
1257 	obj = get_ras_manager(adev, blk);
1258 	if (!obj)
1259 		return -EINVAL;
1260 
1261 	return amdgpu_aca_add_handle(adev, &obj->aca_handle, ras_block_str(blk), aca_info, data);
1262 }
1263 
1264 int amdgpu_ras_unbind_aca(struct amdgpu_device *adev, enum amdgpu_ras_block blk)
1265 {
1266 	struct ras_manager *obj;
1267 
1268 	obj = get_ras_manager(adev, blk);
1269 	if (!obj)
1270 		return -EINVAL;
1271 
1272 	amdgpu_aca_remove_handle(&obj->aca_handle);
1273 
1274 	return 0;
1275 }
1276 
1277 static int amdgpu_aca_log_ras_error_data(struct amdgpu_device *adev, enum amdgpu_ras_block blk,
1278 					 enum aca_error_type type, struct ras_err_data *err_data,
1279 					 struct ras_query_context *qctx)
1280 {
1281 	struct ras_manager *obj;
1282 
1283 	obj = get_ras_manager(adev, blk);
1284 	if (!obj)
1285 		return -EINVAL;
1286 
1287 	return amdgpu_aca_get_error_data(adev, &obj->aca_handle, type, err_data, qctx);
1288 }
1289 
1290 ssize_t amdgpu_ras_aca_sysfs_read(struct device *dev, struct device_attribute *attr,
1291 				  struct aca_handle *handle, char *buf, void *data)
1292 {
1293 	struct ras_manager *obj = container_of(handle, struct ras_manager, aca_handle);
1294 	struct ras_query_if info = {
1295 		.head = obj->head,
1296 	};
1297 
1298 	if (amdgpu_ras_query_error_status(obj->adev, &info))
1299 		return -EINVAL;
1300 
1301 	return sysfs_emit(buf, "%s: %lu\n%s: %lu\n%s: %lu\n", "ue", info.ue_count,
1302 			  "ce", info.ce_count, "de", info.de_count);
1303 }
1304 
1305 static int amdgpu_ras_query_error_status_helper(struct amdgpu_device *adev,
1306 						struct ras_query_if *info,
1307 						struct ras_err_data *err_data,
1308 						struct ras_query_context *qctx,
1309 						unsigned int error_query_mode)
1310 {
1311 	enum amdgpu_ras_block blk = info ? info->head.block : AMDGPU_RAS_BLOCK_COUNT;
1312 	struct amdgpu_ras_block_object *block_obj = NULL;
1313 	int ret;
1314 
1315 	if (blk == AMDGPU_RAS_BLOCK_COUNT)
1316 		return -EINVAL;
1317 
1318 	if (error_query_mode == AMDGPU_RAS_INVALID_ERROR_QUERY)
1319 		return -EINVAL;
1320 
1321 	if (error_query_mode == AMDGPU_RAS_DIRECT_ERROR_QUERY) {
1322 		if (info->head.block == AMDGPU_RAS_BLOCK__UMC) {
1323 			amdgpu_ras_get_ecc_info(adev, err_data);
1324 		} else {
1325 			block_obj = amdgpu_ras_get_ras_block(adev, info->head.block, 0);
1326 			if (!block_obj || !block_obj->hw_ops) {
1327 				dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1328 					     get_ras_block_str(&info->head));
1329 				return -EINVAL;
1330 			}
1331 
1332 			if (block_obj->hw_ops->query_ras_error_count)
1333 				block_obj->hw_ops->query_ras_error_count(adev, err_data);
1334 
1335 			if ((info->head.block == AMDGPU_RAS_BLOCK__SDMA) ||
1336 			    (info->head.block == AMDGPU_RAS_BLOCK__GFX) ||
1337 			    (info->head.block == AMDGPU_RAS_BLOCK__MMHUB)) {
1338 				if (block_obj->hw_ops->query_ras_error_status)
1339 					block_obj->hw_ops->query_ras_error_status(adev);
1340 			}
1341 		}
1342 	} else {
1343 		if (amdgpu_aca_is_enabled(adev)) {
1344 			ret = amdgpu_aca_log_ras_error_data(adev, blk, ACA_ERROR_TYPE_UE, err_data, qctx);
1345 			if (ret)
1346 				return ret;
1347 
1348 			ret = amdgpu_aca_log_ras_error_data(adev, blk, ACA_ERROR_TYPE_CE, err_data, qctx);
1349 			if (ret)
1350 				return ret;
1351 
1352 			ret = amdgpu_aca_log_ras_error_data(adev, blk, ACA_ERROR_TYPE_DEFERRED, err_data, qctx);
1353 			if (ret)
1354 				return ret;
1355 		} else {
1356 			/* FIXME: add code to check return value later */
1357 			amdgpu_mca_smu_log_ras_error(adev, blk, AMDGPU_MCA_ERROR_TYPE_UE, err_data, qctx);
1358 			amdgpu_mca_smu_log_ras_error(adev, blk, AMDGPU_MCA_ERROR_TYPE_CE, err_data, qctx);
1359 		}
1360 	}
1361 
1362 	return 0;
1363 }
1364 
1365 /* query/inject/cure begin */
1366 int amdgpu_ras_query_error_status(struct amdgpu_device *adev, struct ras_query_if *info)
1367 {
1368 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1369 	struct ras_err_data err_data;
1370 	struct ras_query_context qctx;
1371 	unsigned int error_query_mode;
1372 	int ret;
1373 
1374 	if (!obj)
1375 		return -EINVAL;
1376 
1377 	ret = amdgpu_ras_error_data_init(&err_data);
1378 	if (ret)
1379 		return ret;
1380 
1381 	if (!amdgpu_ras_get_error_query_mode(adev, &error_query_mode))
1382 		return -EINVAL;
1383 
1384 	memset(&qctx, 0, sizeof(qctx));
1385 	qctx.event_id = amdgpu_ras_acquire_event_id(adev, amdgpu_ras_intr_triggered() ?
1386 						   RAS_EVENT_TYPE_ISR : RAS_EVENT_TYPE_INVALID);
1387 	ret = amdgpu_ras_query_error_status_helper(adev, info,
1388 						   &err_data,
1389 						   &qctx,
1390 						   error_query_mode);
1391 	if (ret)
1392 		goto out_fini_err_data;
1393 
1394 	amdgpu_rasmgr_error_data_statistic_update(obj, &err_data);
1395 
1396 	info->ue_count = obj->err_data.ue_count;
1397 	info->ce_count = obj->err_data.ce_count;
1398 	info->de_count = obj->err_data.de_count;
1399 
1400 	amdgpu_ras_error_generate_report(adev, info, &err_data, &qctx);
1401 
1402 out_fini_err_data:
1403 	amdgpu_ras_error_data_fini(&err_data);
1404 
1405 	return ret;
1406 }
1407 
1408 int amdgpu_ras_reset_error_count(struct amdgpu_device *adev,
1409 		enum amdgpu_ras_block block)
1410 {
1411 	struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev, block, 0);
1412 	const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs;
1413 	const struct aca_smu_funcs *smu_funcs = adev->aca.smu_funcs;
1414 
1415 	if (!block_obj || !block_obj->hw_ops) {
1416 		dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1417 				ras_block_str(block));
1418 		return -EOPNOTSUPP;
1419 	}
1420 
1421 	if (!amdgpu_ras_is_supported(adev, block) ||
1422 	    !amdgpu_ras_get_aca_debug_mode(adev))
1423 		return -EOPNOTSUPP;
1424 
1425 	/* skip ras error reset in gpu reset */
1426 	if ((amdgpu_in_reset(adev) || amdgpu_ras_in_recovery(adev)) &&
1427 	    ((smu_funcs && smu_funcs->set_debug_mode) ||
1428 	     (mca_funcs && mca_funcs->mca_set_debug_mode)))
1429 		return -EOPNOTSUPP;
1430 
1431 	if (block_obj->hw_ops->reset_ras_error_count)
1432 		block_obj->hw_ops->reset_ras_error_count(adev);
1433 
1434 	return 0;
1435 }
1436 
1437 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev,
1438 		enum amdgpu_ras_block block)
1439 {
1440 	struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev, block, 0);
1441 
1442 	if (amdgpu_ras_reset_error_count(adev, block) == -EOPNOTSUPP)
1443 		return 0;
1444 
1445 	if ((block == AMDGPU_RAS_BLOCK__GFX) ||
1446 	    (block == AMDGPU_RAS_BLOCK__MMHUB)) {
1447 		if (block_obj->hw_ops->reset_ras_error_status)
1448 			block_obj->hw_ops->reset_ras_error_status(adev);
1449 	}
1450 
1451 	return 0;
1452 }
1453 
1454 /* wrapper of psp_ras_trigger_error */
1455 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
1456 		struct ras_inject_if *info)
1457 {
1458 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1459 	struct ta_ras_trigger_error_input block_info = {
1460 		.block_id =  amdgpu_ras_block_to_ta(info->head.block),
1461 		.inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
1462 		.sub_block_index = info->head.sub_block_index,
1463 		.address = info->address,
1464 		.value = info->value,
1465 	};
1466 	int ret = -EINVAL;
1467 	struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev,
1468 							info->head.block,
1469 							info->head.sub_block_index);
1470 
1471 	/* inject on guest isn't allowed, return success directly */
1472 	if (amdgpu_sriov_vf(adev))
1473 		return 0;
1474 
1475 	if (!obj)
1476 		return -EINVAL;
1477 
1478 	if (!block_obj || !block_obj->hw_ops)	{
1479 		dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1480 			     get_ras_block_str(&info->head));
1481 		return -EINVAL;
1482 	}
1483 
1484 	/* Calculate XGMI relative offset */
1485 	if (adev->gmc.xgmi.num_physical_nodes > 1 &&
1486 	    info->head.block != AMDGPU_RAS_BLOCK__GFX) {
1487 		block_info.address =
1488 			amdgpu_xgmi_get_relative_phy_addr(adev,
1489 							  block_info.address);
1490 	}
1491 
1492 	if (block_obj->hw_ops->ras_error_inject) {
1493 		if (info->head.block == AMDGPU_RAS_BLOCK__GFX)
1494 			ret = block_obj->hw_ops->ras_error_inject(adev, info, info->instance_mask);
1495 		else /* Special ras_error_inject is defined (e.g: xgmi) */
1496 			ret = block_obj->hw_ops->ras_error_inject(adev, &block_info,
1497 						info->instance_mask);
1498 	} else {
1499 		/* default path */
1500 		ret = psp_ras_trigger_error(&adev->psp, &block_info, info->instance_mask);
1501 	}
1502 
1503 	if (ret)
1504 		dev_err(adev->dev, "ras inject %s failed %d\n",
1505 			get_ras_block_str(&info->head), ret);
1506 
1507 	return ret;
1508 }
1509 
1510 /**
1511  * amdgpu_ras_query_error_count_helper -- Get error counter for specific IP
1512  * @adev: pointer to AMD GPU device
1513  * @ce_count: pointer to an integer to be set to the count of correctible errors.
1514  * @ue_count: pointer to an integer to be set to the count of uncorrectible errors.
1515  * @query_info: pointer to ras_query_if
1516  *
1517  * Return 0 for query success or do nothing, otherwise return an error
1518  * on failures
1519  */
1520 static int amdgpu_ras_query_error_count_helper(struct amdgpu_device *adev,
1521 					       unsigned long *ce_count,
1522 					       unsigned long *ue_count,
1523 					       struct ras_query_if *query_info)
1524 {
1525 	int ret;
1526 
1527 	if (!query_info)
1528 		/* do nothing if query_info is not specified */
1529 		return 0;
1530 
1531 	ret = amdgpu_ras_query_error_status(adev, query_info);
1532 	if (ret)
1533 		return ret;
1534 
1535 	*ce_count += query_info->ce_count;
1536 	*ue_count += query_info->ue_count;
1537 
1538 	/* some hardware/IP supports read to clear
1539 	 * no need to explictly reset the err status after the query call */
1540 	if (amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 2) &&
1541 	    amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 4)) {
1542 		if (amdgpu_ras_reset_error_status(adev, query_info->head.block))
1543 			dev_warn(adev->dev,
1544 				 "Failed to reset error counter and error status\n");
1545 	}
1546 
1547 	return 0;
1548 }
1549 
1550 /**
1551  * amdgpu_ras_query_error_count -- Get error counts of all IPs or specific IP
1552  * @adev: pointer to AMD GPU device
1553  * @ce_count: pointer to an integer to be set to the count of correctible errors.
1554  * @ue_count: pointer to an integer to be set to the count of uncorrectible
1555  * errors.
1556  * @query_info: pointer to ras_query_if if the query request is only for
1557  * specific ip block; if info is NULL, then the qurey request is for
1558  * all the ip blocks that support query ras error counters/status
1559  *
1560  * If set, @ce_count or @ue_count, count and return the corresponding
1561  * error counts in those integer pointers. Return 0 if the device
1562  * supports RAS. Return -EOPNOTSUPP if the device doesn't support RAS.
1563  */
1564 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
1565 				 unsigned long *ce_count,
1566 				 unsigned long *ue_count,
1567 				 struct ras_query_if *query_info)
1568 {
1569 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1570 	struct ras_manager *obj;
1571 	unsigned long ce, ue;
1572 	int ret;
1573 
1574 	if (!adev->ras_enabled || !con)
1575 		return -EOPNOTSUPP;
1576 
1577 	/* Don't count since no reporting.
1578 	 */
1579 	if (!ce_count && !ue_count)
1580 		return 0;
1581 
1582 	ce = 0;
1583 	ue = 0;
1584 	if (!query_info) {
1585 		/* query all the ip blocks that support ras query interface */
1586 		list_for_each_entry(obj, &con->head, node) {
1587 			struct ras_query_if info = {
1588 				.head = obj->head,
1589 			};
1590 
1591 			ret = amdgpu_ras_query_error_count_helper(adev, &ce, &ue, &info);
1592 		}
1593 	} else {
1594 		/* query specific ip block */
1595 		ret = amdgpu_ras_query_error_count_helper(adev, &ce, &ue, query_info);
1596 	}
1597 
1598 	if (ret)
1599 		return ret;
1600 
1601 	if (ce_count)
1602 		*ce_count = ce;
1603 
1604 	if (ue_count)
1605 		*ue_count = ue;
1606 
1607 	return 0;
1608 }
1609 /* query/inject/cure end */
1610 
1611 
1612 /* sysfs begin */
1613 
1614 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1615 		struct ras_badpage **bps, unsigned int *count);
1616 
1617 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
1618 {
1619 	switch (flags) {
1620 	case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
1621 		return "R";
1622 	case AMDGPU_RAS_RETIRE_PAGE_PENDING:
1623 		return "P";
1624 	case AMDGPU_RAS_RETIRE_PAGE_FAULT:
1625 	default:
1626 		return "F";
1627 	}
1628 }
1629 
1630 /**
1631  * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
1632  *
1633  * It allows user to read the bad pages of vram on the gpu through
1634  * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
1635  *
1636  * It outputs multiple lines, and each line stands for one gpu page.
1637  *
1638  * The format of one line is below,
1639  * gpu pfn : gpu page size : flags
1640  *
1641  * gpu pfn and gpu page size are printed in hex format.
1642  * flags can be one of below character,
1643  *
1644  * R: reserved, this gpu page is reserved and not able to use.
1645  *
1646  * P: pending for reserve, this gpu page is marked as bad, will be reserved
1647  * in next window of page_reserve.
1648  *
1649  * F: unable to reserve. this gpu page can't be reserved due to some reasons.
1650  *
1651  * Examples:
1652  *
1653  * .. code-block:: bash
1654  *
1655  *	0x00000001 : 0x00001000 : R
1656  *	0x00000002 : 0x00001000 : P
1657  *
1658  */
1659 
1660 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
1661 		struct kobject *kobj, struct bin_attribute *attr,
1662 		char *buf, loff_t ppos, size_t count)
1663 {
1664 	struct amdgpu_ras *con =
1665 		container_of(attr, struct amdgpu_ras, badpages_attr);
1666 	struct amdgpu_device *adev = con->adev;
1667 	const unsigned int element_size =
1668 		sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
1669 	unsigned int start = div64_ul(ppos + element_size - 1, element_size);
1670 	unsigned int end = div64_ul(ppos + count - 1, element_size);
1671 	ssize_t s = 0;
1672 	struct ras_badpage *bps = NULL;
1673 	unsigned int bps_count = 0;
1674 
1675 	memset(buf, 0, count);
1676 
1677 	if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1678 		return 0;
1679 
1680 	for (; start < end && start < bps_count; start++)
1681 		s += scnprintf(&buf[s], element_size + 1,
1682 				"0x%08x : 0x%08x : %1s\n",
1683 				bps[start].bp,
1684 				bps[start].size,
1685 				amdgpu_ras_badpage_flags_str(bps[start].flags));
1686 
1687 	kfree(bps);
1688 
1689 	return s;
1690 }
1691 
1692 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1693 		struct device_attribute *attr, char *buf)
1694 {
1695 	struct amdgpu_ras *con =
1696 		container_of(attr, struct amdgpu_ras, features_attr);
1697 
1698 	return sysfs_emit(buf, "feature mask: 0x%x\n", con->features);
1699 }
1700 
1701 static ssize_t amdgpu_ras_sysfs_version_show(struct device *dev,
1702 		struct device_attribute *attr, char *buf)
1703 {
1704 	struct amdgpu_ras *con =
1705 		container_of(attr, struct amdgpu_ras, version_attr);
1706 	return sysfs_emit(buf, "table version: 0x%x\n", con->eeprom_control.tbl_hdr.version);
1707 }
1708 
1709 static ssize_t amdgpu_ras_sysfs_schema_show(struct device *dev,
1710 		struct device_attribute *attr, char *buf)
1711 {
1712 	struct amdgpu_ras *con =
1713 		container_of(attr, struct amdgpu_ras, schema_attr);
1714 	return sysfs_emit(buf, "schema: 0x%x\n", con->schema);
1715 }
1716 
1717 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1718 {
1719 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1720 
1721 	if (adev->dev->kobj.sd)
1722 		sysfs_remove_file_from_group(&adev->dev->kobj,
1723 				&con->badpages_attr.attr,
1724 				RAS_FS_NAME);
1725 }
1726 
1727 static int amdgpu_ras_sysfs_remove_dev_attr_node(struct amdgpu_device *adev)
1728 {
1729 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1730 	struct attribute *attrs[] = {
1731 		&con->features_attr.attr,
1732 		&con->version_attr.attr,
1733 		&con->schema_attr.attr,
1734 		NULL
1735 	};
1736 	struct attribute_group group = {
1737 		.name = RAS_FS_NAME,
1738 		.attrs = attrs,
1739 	};
1740 
1741 	if (adev->dev->kobj.sd)
1742 		sysfs_remove_group(&adev->dev->kobj, &group);
1743 
1744 	return 0;
1745 }
1746 
1747 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1748 		struct ras_common_if *head)
1749 {
1750 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1751 
1752 	if (amdgpu_aca_is_enabled(adev))
1753 		return 0;
1754 
1755 	if (!obj || obj->attr_inuse)
1756 		return -EINVAL;
1757 
1758 	get_obj(obj);
1759 
1760 	snprintf(obj->fs_data.sysfs_name, sizeof(obj->fs_data.sysfs_name),
1761 		"%s_err_count", head->name);
1762 
1763 	obj->sysfs_attr = (struct device_attribute){
1764 		.attr = {
1765 			.name = obj->fs_data.sysfs_name,
1766 			.mode = S_IRUGO,
1767 		},
1768 			.show = amdgpu_ras_sysfs_read,
1769 	};
1770 	sysfs_attr_init(&obj->sysfs_attr.attr);
1771 
1772 	if (sysfs_add_file_to_group(&adev->dev->kobj,
1773 				&obj->sysfs_attr.attr,
1774 				RAS_FS_NAME)) {
1775 		put_obj(obj);
1776 		return -EINVAL;
1777 	}
1778 
1779 	obj->attr_inuse = 1;
1780 
1781 	return 0;
1782 }
1783 
1784 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1785 		struct ras_common_if *head)
1786 {
1787 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1788 
1789 	if (amdgpu_aca_is_enabled(adev))
1790 		return 0;
1791 
1792 	if (!obj || !obj->attr_inuse)
1793 		return -EINVAL;
1794 
1795 	if (adev->dev->kobj.sd)
1796 		sysfs_remove_file_from_group(&adev->dev->kobj,
1797 				&obj->sysfs_attr.attr,
1798 				RAS_FS_NAME);
1799 	obj->attr_inuse = 0;
1800 	put_obj(obj);
1801 
1802 	return 0;
1803 }
1804 
1805 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1806 {
1807 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1808 	struct ras_manager *obj, *tmp;
1809 
1810 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
1811 		amdgpu_ras_sysfs_remove(adev, &obj->head);
1812 	}
1813 
1814 	if (amdgpu_bad_page_threshold != 0)
1815 		amdgpu_ras_sysfs_remove_bad_page_node(adev);
1816 
1817 	amdgpu_ras_sysfs_remove_dev_attr_node(adev);
1818 
1819 	return 0;
1820 }
1821 /* sysfs end */
1822 
1823 /**
1824  * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1825  *
1826  * Normally when there is an uncorrectable error, the driver will reset
1827  * the GPU to recover.  However, in the event of an unrecoverable error,
1828  * the driver provides an interface to reboot the system automatically
1829  * in that event.
1830  *
1831  * The following file in debugfs provides that interface:
1832  * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1833  *
1834  * Usage:
1835  *
1836  * .. code-block:: bash
1837  *
1838  *	echo true > .../ras/auto_reboot
1839  *
1840  */
1841 /* debugfs begin */
1842 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1843 {
1844 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1845 	struct amdgpu_ras_eeprom_control *eeprom = &con->eeprom_control;
1846 	struct drm_minor  *minor = adev_to_drm(adev)->primary;
1847 	struct dentry     *dir;
1848 
1849 	dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1850 	debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev,
1851 			    &amdgpu_ras_debugfs_ctrl_ops);
1852 	debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev,
1853 			    &amdgpu_ras_debugfs_eeprom_ops);
1854 	debugfs_create_u32("bad_page_cnt_threshold", 0444, dir,
1855 			   &con->bad_page_cnt_threshold);
1856 	debugfs_create_u32("ras_num_recs", 0444, dir, &eeprom->ras_num_recs);
1857 	debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled);
1858 	debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled);
1859 	debugfs_create_file("ras_eeprom_size", S_IRUGO, dir, adev,
1860 			    &amdgpu_ras_debugfs_eeprom_size_ops);
1861 	con->de_ras_eeprom_table = debugfs_create_file("ras_eeprom_table",
1862 						       S_IRUGO, dir, adev,
1863 						       &amdgpu_ras_debugfs_eeprom_table_ops);
1864 	amdgpu_ras_debugfs_set_ret_size(&con->eeprom_control);
1865 
1866 	/*
1867 	 * After one uncorrectable error happens, usually GPU recovery will
1868 	 * be scheduled. But due to the known problem in GPU recovery failing
1869 	 * to bring GPU back, below interface provides one direct way to
1870 	 * user to reboot system automatically in such case within
1871 	 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1872 	 * will never be called.
1873 	 */
1874 	debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot);
1875 
1876 	/*
1877 	 * User could set this not to clean up hardware's error count register
1878 	 * of RAS IPs during ras recovery.
1879 	 */
1880 	debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir,
1881 			    &con->disable_ras_err_cnt_harvest);
1882 	return dir;
1883 }
1884 
1885 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1886 				      struct ras_fs_if *head,
1887 				      struct dentry *dir)
1888 {
1889 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1890 
1891 	if (!obj || !dir)
1892 		return;
1893 
1894 	get_obj(obj);
1895 
1896 	memcpy(obj->fs_data.debugfs_name,
1897 			head->debugfs_name,
1898 			sizeof(obj->fs_data.debugfs_name));
1899 
1900 	debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir,
1901 			    obj, &amdgpu_ras_debugfs_ops);
1902 }
1903 
1904 static bool amdgpu_ras_aca_is_supported(struct amdgpu_device *adev)
1905 {
1906 	bool ret;
1907 
1908 	switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
1909 	case IP_VERSION(13, 0, 6):
1910 	case IP_VERSION(13, 0, 14):
1911 		ret = true;
1912 		break;
1913 	default:
1914 		ret = false;
1915 		break;
1916 	}
1917 
1918 	return ret;
1919 }
1920 
1921 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1922 {
1923 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1924 	struct dentry *dir;
1925 	struct ras_manager *obj;
1926 	struct ras_fs_if fs_info;
1927 
1928 	/*
1929 	 * it won't be called in resume path, no need to check
1930 	 * suspend and gpu reset status
1931 	 */
1932 	if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con)
1933 		return;
1934 
1935 	dir = amdgpu_ras_debugfs_create_ctrl_node(adev);
1936 
1937 	list_for_each_entry(obj, &con->head, node) {
1938 		if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1939 			(obj->attr_inuse == 1)) {
1940 			sprintf(fs_info.debugfs_name, "%s_err_inject",
1941 					get_ras_block_str(&obj->head));
1942 			fs_info.head = obj->head;
1943 			amdgpu_ras_debugfs_create(adev, &fs_info, dir);
1944 		}
1945 	}
1946 
1947 	if (amdgpu_ras_aca_is_supported(adev)) {
1948 		if (amdgpu_aca_is_enabled(adev))
1949 			amdgpu_aca_smu_debugfs_init(adev, dir);
1950 		else
1951 			amdgpu_mca_smu_debugfs_init(adev, dir);
1952 	}
1953 }
1954 
1955 /* debugfs end */
1956 
1957 /* ras fs */
1958 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1959 		amdgpu_ras_sysfs_badpages_read, NULL, 0);
1960 static DEVICE_ATTR(features, S_IRUGO,
1961 		amdgpu_ras_sysfs_features_read, NULL);
1962 static DEVICE_ATTR(version, 0444,
1963 		amdgpu_ras_sysfs_version_show, NULL);
1964 static DEVICE_ATTR(schema, 0444,
1965 		amdgpu_ras_sysfs_schema_show, NULL);
1966 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1967 {
1968 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1969 	struct attribute_group group = {
1970 		.name = RAS_FS_NAME,
1971 	};
1972 	struct attribute *attrs[] = {
1973 		&con->features_attr.attr,
1974 		&con->version_attr.attr,
1975 		&con->schema_attr.attr,
1976 		NULL
1977 	};
1978 	struct bin_attribute *bin_attrs[] = {
1979 		NULL,
1980 		NULL,
1981 	};
1982 	int r;
1983 
1984 	group.attrs = attrs;
1985 
1986 	/* add features entry */
1987 	con->features_attr = dev_attr_features;
1988 	sysfs_attr_init(attrs[0]);
1989 
1990 	/* add version entry */
1991 	con->version_attr = dev_attr_version;
1992 	sysfs_attr_init(attrs[1]);
1993 
1994 	/* add schema entry */
1995 	con->schema_attr = dev_attr_schema;
1996 	sysfs_attr_init(attrs[2]);
1997 
1998 	if (amdgpu_bad_page_threshold != 0) {
1999 		/* add bad_page_features entry */
2000 		bin_attr_gpu_vram_bad_pages.private = NULL;
2001 		con->badpages_attr = bin_attr_gpu_vram_bad_pages;
2002 		bin_attrs[0] = &con->badpages_attr;
2003 		group.bin_attrs = bin_attrs;
2004 		sysfs_bin_attr_init(bin_attrs[0]);
2005 	}
2006 
2007 	r = sysfs_create_group(&adev->dev->kobj, &group);
2008 	if (r)
2009 		dev_err(adev->dev, "Failed to create RAS sysfs group!");
2010 
2011 	return 0;
2012 }
2013 
2014 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
2015 {
2016 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2017 	struct ras_manager *con_obj, *ip_obj, *tmp;
2018 
2019 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2020 		list_for_each_entry_safe(con_obj, tmp, &con->head, node) {
2021 			ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head);
2022 			if (ip_obj)
2023 				put_obj(ip_obj);
2024 		}
2025 	}
2026 
2027 	amdgpu_ras_sysfs_remove_all(adev);
2028 	return 0;
2029 }
2030 /* ras fs end */
2031 
2032 /* ih begin */
2033 
2034 /* For the hardware that cannot enable bif ring for both ras_controller_irq
2035  * and ras_err_evnet_athub_irq ih cookies, the driver has to poll status
2036  * register to check whether the interrupt is triggered or not, and properly
2037  * ack the interrupt if it is there
2038  */
2039 void amdgpu_ras_interrupt_fatal_error_handler(struct amdgpu_device *adev)
2040 {
2041 	/* Fatal error events are handled on host side */
2042 	if (amdgpu_sriov_vf(adev))
2043 		return;
2044 
2045 	if (adev->nbio.ras &&
2046 	    adev->nbio.ras->handle_ras_controller_intr_no_bifring)
2047 		adev->nbio.ras->handle_ras_controller_intr_no_bifring(adev);
2048 
2049 	if (adev->nbio.ras &&
2050 	    adev->nbio.ras->handle_ras_err_event_athub_intr_no_bifring)
2051 		adev->nbio.ras->handle_ras_err_event_athub_intr_no_bifring(adev);
2052 }
2053 
2054 static void amdgpu_ras_interrupt_poison_consumption_handler(struct ras_manager *obj,
2055 				struct amdgpu_iv_entry *entry)
2056 {
2057 	bool poison_stat = false;
2058 	struct amdgpu_device *adev = obj->adev;
2059 	struct amdgpu_ras_block_object *block_obj =
2060 		amdgpu_ras_get_ras_block(adev, obj->head.block, 0);
2061 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2062 
2063 	if (!block_obj || !con)
2064 		return;
2065 
2066 	/* both query_poison_status and handle_poison_consumption are optional,
2067 	 * but at least one of them should be implemented if we need poison
2068 	 * consumption handler
2069 	 */
2070 	if (block_obj->hw_ops && block_obj->hw_ops->query_poison_status) {
2071 		poison_stat = block_obj->hw_ops->query_poison_status(adev);
2072 		if (!poison_stat) {
2073 			/* Not poison consumption interrupt, no need to handle it */
2074 			dev_info(adev->dev, "No RAS poison status in %s poison IH.\n",
2075 					block_obj->ras_comm.name);
2076 
2077 			return;
2078 		}
2079 	}
2080 
2081 	amdgpu_umc_poison_handler(adev, obj->head.block, 0);
2082 
2083 	if (block_obj->hw_ops && block_obj->hw_ops->handle_poison_consumption)
2084 		poison_stat = block_obj->hw_ops->handle_poison_consumption(adev);
2085 
2086 	/* gpu reset is fallback for failed and default cases.
2087 	 * For RMA case, amdgpu_umc_poison_handler will handle gpu reset.
2088 	 */
2089 	if (poison_stat && !con->is_rma) {
2090 		dev_info(adev->dev, "GPU reset for %s RAS poison consumption is issued!\n",
2091 				block_obj->ras_comm.name);
2092 		amdgpu_ras_reset_gpu(adev);
2093 	}
2094 
2095 	if (!poison_stat)
2096 		amdgpu_gfx_poison_consumption_handler(adev, entry);
2097 }
2098 
2099 static void amdgpu_ras_interrupt_poison_creation_handler(struct ras_manager *obj,
2100 				struct amdgpu_iv_entry *entry)
2101 {
2102 	dev_info(obj->adev->dev,
2103 		"Poison is created\n");
2104 
2105 	if (amdgpu_ip_version(obj->adev, UMC_HWIP, 0) >= IP_VERSION(12, 0, 0)) {
2106 		struct amdgpu_ras *con = amdgpu_ras_get_context(obj->adev);
2107 
2108 		amdgpu_ras_put_poison_req(obj->adev,
2109 			AMDGPU_RAS_BLOCK__UMC, 0, NULL, NULL, false);
2110 
2111 		atomic_inc(&con->page_retirement_req_cnt);
2112 
2113 		wake_up(&con->page_retirement_wq);
2114 	}
2115 }
2116 
2117 static void amdgpu_ras_interrupt_umc_handler(struct ras_manager *obj,
2118 				struct amdgpu_iv_entry *entry)
2119 {
2120 	struct ras_ih_data *data = &obj->ih_data;
2121 	struct ras_err_data err_data;
2122 	int ret;
2123 
2124 	if (!data->cb)
2125 		return;
2126 
2127 	ret = amdgpu_ras_error_data_init(&err_data);
2128 	if (ret)
2129 		return;
2130 
2131 	/* Let IP handle its data, maybe we need get the output
2132 	 * from the callback to update the error type/count, etc
2133 	 */
2134 	amdgpu_ras_set_fed(obj->adev, true);
2135 	ret = data->cb(obj->adev, &err_data, entry);
2136 	/* ue will trigger an interrupt, and in that case
2137 	 * we need do a reset to recovery the whole system.
2138 	 * But leave IP do that recovery, here we just dispatch
2139 	 * the error.
2140 	 */
2141 	if (ret == AMDGPU_RAS_SUCCESS) {
2142 		/* these counts could be left as 0 if
2143 		 * some blocks do not count error number
2144 		 */
2145 		obj->err_data.ue_count += err_data.ue_count;
2146 		obj->err_data.ce_count += err_data.ce_count;
2147 		obj->err_data.de_count += err_data.de_count;
2148 	}
2149 
2150 	amdgpu_ras_error_data_fini(&err_data);
2151 }
2152 
2153 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
2154 {
2155 	struct ras_ih_data *data = &obj->ih_data;
2156 	struct amdgpu_iv_entry entry;
2157 
2158 	while (data->rptr != data->wptr) {
2159 		rmb();
2160 		memcpy(&entry, &data->ring[data->rptr],
2161 				data->element_size);
2162 
2163 		wmb();
2164 		data->rptr = (data->aligned_element_size +
2165 				data->rptr) % data->ring_size;
2166 
2167 		if (amdgpu_ras_is_poison_mode_supported(obj->adev)) {
2168 			if (obj->head.block == AMDGPU_RAS_BLOCK__UMC)
2169 				amdgpu_ras_interrupt_poison_creation_handler(obj, &entry);
2170 			else
2171 				amdgpu_ras_interrupt_poison_consumption_handler(obj, &entry);
2172 		} else {
2173 			if (obj->head.block == AMDGPU_RAS_BLOCK__UMC)
2174 				amdgpu_ras_interrupt_umc_handler(obj, &entry);
2175 			else
2176 				dev_warn(obj->adev->dev,
2177 					"No RAS interrupt handler for non-UMC block with poison disabled.\n");
2178 		}
2179 	}
2180 }
2181 
2182 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
2183 {
2184 	struct ras_ih_data *data =
2185 		container_of(work, struct ras_ih_data, ih_work);
2186 	struct ras_manager *obj =
2187 		container_of(data, struct ras_manager, ih_data);
2188 
2189 	amdgpu_ras_interrupt_handler(obj);
2190 }
2191 
2192 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
2193 		struct ras_dispatch_if *info)
2194 {
2195 	struct ras_manager *obj;
2196 	struct ras_ih_data *data;
2197 
2198 	obj = amdgpu_ras_find_obj(adev, &info->head);
2199 	if (!obj)
2200 		return -EINVAL;
2201 
2202 	data = &obj->ih_data;
2203 
2204 	if (data->inuse == 0)
2205 		return 0;
2206 
2207 	/* Might be overflow... */
2208 	memcpy(&data->ring[data->wptr], info->entry,
2209 			data->element_size);
2210 
2211 	wmb();
2212 	data->wptr = (data->aligned_element_size +
2213 			data->wptr) % data->ring_size;
2214 
2215 	schedule_work(&data->ih_work);
2216 
2217 	return 0;
2218 }
2219 
2220 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
2221 		struct ras_common_if *head)
2222 {
2223 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
2224 	struct ras_ih_data *data;
2225 
2226 	if (!obj)
2227 		return -EINVAL;
2228 
2229 	data = &obj->ih_data;
2230 	if (data->inuse == 0)
2231 		return 0;
2232 
2233 	cancel_work_sync(&data->ih_work);
2234 
2235 	kfree(data->ring);
2236 	memset(data, 0, sizeof(*data));
2237 	put_obj(obj);
2238 
2239 	return 0;
2240 }
2241 
2242 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
2243 		struct ras_common_if *head)
2244 {
2245 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
2246 	struct ras_ih_data *data;
2247 	struct amdgpu_ras_block_object *ras_obj;
2248 
2249 	if (!obj) {
2250 		/* in case we registe the IH before enable ras feature */
2251 		obj = amdgpu_ras_create_obj(adev, head);
2252 		if (!obj)
2253 			return -EINVAL;
2254 	} else
2255 		get_obj(obj);
2256 
2257 	ras_obj = container_of(head, struct amdgpu_ras_block_object, ras_comm);
2258 
2259 	data = &obj->ih_data;
2260 	/* add the callback.etc */
2261 	*data = (struct ras_ih_data) {
2262 		.inuse = 0,
2263 		.cb = ras_obj->ras_cb,
2264 		.element_size = sizeof(struct amdgpu_iv_entry),
2265 		.rptr = 0,
2266 		.wptr = 0,
2267 	};
2268 
2269 	INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
2270 
2271 	data->aligned_element_size = ALIGN(data->element_size, 8);
2272 	/* the ring can store 64 iv entries. */
2273 	data->ring_size = 64 * data->aligned_element_size;
2274 	data->ring = kmalloc(data->ring_size, GFP_KERNEL);
2275 	if (!data->ring) {
2276 		put_obj(obj);
2277 		return -ENOMEM;
2278 	}
2279 
2280 	/* IH is ready */
2281 	data->inuse = 1;
2282 
2283 	return 0;
2284 }
2285 
2286 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
2287 {
2288 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2289 	struct ras_manager *obj, *tmp;
2290 
2291 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
2292 		amdgpu_ras_interrupt_remove_handler(adev, &obj->head);
2293 	}
2294 
2295 	return 0;
2296 }
2297 /* ih end */
2298 
2299 /* traversal all IPs except NBIO to query error counter */
2300 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
2301 {
2302 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2303 	struct ras_manager *obj;
2304 
2305 	if (!adev->ras_enabled || !con)
2306 		return;
2307 
2308 	list_for_each_entry(obj, &con->head, node) {
2309 		struct ras_query_if info = {
2310 			.head = obj->head,
2311 		};
2312 
2313 		/*
2314 		 * PCIE_BIF IP has one different isr by ras controller
2315 		 * interrupt, the specific ras counter query will be
2316 		 * done in that isr. So skip such block from common
2317 		 * sync flood interrupt isr calling.
2318 		 */
2319 		if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
2320 			continue;
2321 
2322 		/*
2323 		 * this is a workaround for aldebaran, skip send msg to
2324 		 * smu to get ecc_info table due to smu handle get ecc
2325 		 * info table failed temporarily.
2326 		 * should be removed until smu fix handle ecc_info table.
2327 		 */
2328 		if ((info.head.block == AMDGPU_RAS_BLOCK__UMC) &&
2329 		    (amdgpu_ip_version(adev, MP1_HWIP, 0) ==
2330 		     IP_VERSION(13, 0, 2)))
2331 			continue;
2332 
2333 		amdgpu_ras_query_error_status(adev, &info);
2334 
2335 		if (amdgpu_ip_version(adev, MP0_HWIP, 0) !=
2336 			    IP_VERSION(11, 0, 2) &&
2337 		    amdgpu_ip_version(adev, MP0_HWIP, 0) !=
2338 			    IP_VERSION(11, 0, 4) &&
2339 		    amdgpu_ip_version(adev, MP0_HWIP, 0) !=
2340 			    IP_VERSION(13, 0, 0)) {
2341 			if (amdgpu_ras_reset_error_status(adev, info.head.block))
2342 				dev_warn(adev->dev, "Failed to reset error counter and error status");
2343 		}
2344 	}
2345 }
2346 
2347 /* Parse RdRspStatus and WrRspStatus */
2348 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
2349 					  struct ras_query_if *info)
2350 {
2351 	struct amdgpu_ras_block_object *block_obj;
2352 	/*
2353 	 * Only two block need to query read/write
2354 	 * RspStatus at current state
2355 	 */
2356 	if ((info->head.block != AMDGPU_RAS_BLOCK__GFX) &&
2357 		(info->head.block != AMDGPU_RAS_BLOCK__MMHUB))
2358 		return;
2359 
2360 	block_obj = amdgpu_ras_get_ras_block(adev,
2361 					info->head.block,
2362 					info->head.sub_block_index);
2363 
2364 	if (!block_obj || !block_obj->hw_ops) {
2365 		dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
2366 			     get_ras_block_str(&info->head));
2367 		return;
2368 	}
2369 
2370 	if (block_obj->hw_ops->query_ras_error_status)
2371 		block_obj->hw_ops->query_ras_error_status(adev);
2372 
2373 }
2374 
2375 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
2376 {
2377 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2378 	struct ras_manager *obj;
2379 
2380 	if (!adev->ras_enabled || !con)
2381 		return;
2382 
2383 	list_for_each_entry(obj, &con->head, node) {
2384 		struct ras_query_if info = {
2385 			.head = obj->head,
2386 		};
2387 
2388 		amdgpu_ras_error_status_query(adev, &info);
2389 	}
2390 }
2391 
2392 /* recovery begin */
2393 
2394 /* return 0 on success.
2395  * caller need free bps.
2396  */
2397 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
2398 		struct ras_badpage **bps, unsigned int *count)
2399 {
2400 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2401 	struct ras_err_handler_data *data;
2402 	int i = 0;
2403 	int ret = 0, status;
2404 
2405 	if (!con || !con->eh_data || !bps || !count)
2406 		return -EINVAL;
2407 
2408 	mutex_lock(&con->recovery_lock);
2409 	data = con->eh_data;
2410 	if (!data || data->count == 0) {
2411 		*bps = NULL;
2412 		ret = -EINVAL;
2413 		goto out;
2414 	}
2415 
2416 	*bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
2417 	if (!*bps) {
2418 		ret = -ENOMEM;
2419 		goto out;
2420 	}
2421 
2422 	for (; i < data->count; i++) {
2423 		(*bps)[i] = (struct ras_badpage){
2424 			.bp = data->bps[i].retired_page,
2425 			.size = AMDGPU_GPU_PAGE_SIZE,
2426 			.flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
2427 		};
2428 		status = amdgpu_vram_mgr_query_page_status(&adev->mman.vram_mgr,
2429 				data->bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT);
2430 		if (status == -EBUSY)
2431 			(*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
2432 		else if (status == -ENOENT)
2433 			(*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
2434 	}
2435 
2436 	*count = data->count;
2437 out:
2438 	mutex_unlock(&con->recovery_lock);
2439 	return ret;
2440 }
2441 
2442 static void amdgpu_ras_set_fed_all(struct amdgpu_device *adev,
2443 				   struct amdgpu_hive_info *hive, bool status)
2444 {
2445 	struct amdgpu_device *tmp_adev;
2446 
2447 	if (hive) {
2448 		list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head)
2449 			amdgpu_ras_set_fed(tmp_adev, status);
2450 	} else {
2451 		amdgpu_ras_set_fed(adev, status);
2452 	}
2453 }
2454 
2455 bool amdgpu_ras_in_recovery(struct amdgpu_device *adev)
2456 {
2457 	struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
2458 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
2459 	int hive_ras_recovery = 0;
2460 
2461 	if (hive) {
2462 		hive_ras_recovery = atomic_read(&hive->ras_recovery);
2463 		amdgpu_put_xgmi_hive(hive);
2464 	}
2465 
2466 	if (ras && (atomic_read(&ras->in_recovery) || hive_ras_recovery))
2467 		return true;
2468 
2469 	return false;
2470 }
2471 
2472 static void amdgpu_ras_do_recovery(struct work_struct *work)
2473 {
2474 	struct amdgpu_ras *ras =
2475 		container_of(work, struct amdgpu_ras, recovery_work);
2476 	struct amdgpu_device *remote_adev = NULL;
2477 	struct amdgpu_device *adev = ras->adev;
2478 	struct list_head device_list, *device_list_handle =  NULL;
2479 	struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
2480 
2481 	if (hive) {
2482 		atomic_set(&hive->ras_recovery, 1);
2483 
2484 		/* If any device which is part of the hive received RAS fatal
2485 		 * error interrupt, set fatal error status on all. This
2486 		 * condition will need a recovery, and flag will be cleared
2487 		 * as part of recovery.
2488 		 */
2489 		list_for_each_entry(remote_adev, &hive->device_list,
2490 				    gmc.xgmi.head)
2491 			if (amdgpu_ras_get_fed_status(remote_adev)) {
2492 				amdgpu_ras_set_fed_all(adev, hive, true);
2493 				break;
2494 			}
2495 	}
2496 	if (!ras->disable_ras_err_cnt_harvest) {
2497 
2498 		/* Build list of devices to query RAS related errors */
2499 		if  (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
2500 			device_list_handle = &hive->device_list;
2501 		} else {
2502 			INIT_LIST_HEAD(&device_list);
2503 			list_add_tail(&adev->gmc.xgmi.head, &device_list);
2504 			device_list_handle = &device_list;
2505 		}
2506 
2507 		list_for_each_entry(remote_adev,
2508 				device_list_handle, gmc.xgmi.head) {
2509 			amdgpu_ras_query_err_status(remote_adev);
2510 			amdgpu_ras_log_on_err_counter(remote_adev);
2511 		}
2512 
2513 	}
2514 
2515 	if (amdgpu_device_should_recover_gpu(ras->adev)) {
2516 		struct amdgpu_reset_context reset_context;
2517 		memset(&reset_context, 0, sizeof(reset_context));
2518 
2519 		reset_context.method = AMD_RESET_METHOD_NONE;
2520 		reset_context.reset_req_dev = adev;
2521 		reset_context.src = AMDGPU_RESET_SRC_RAS;
2522 
2523 		/* Perform full reset in fatal error mode */
2524 		if (!amdgpu_ras_is_poison_mode_supported(ras->adev))
2525 			set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
2526 		else {
2527 			clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
2528 
2529 			if (ras->gpu_reset_flags & AMDGPU_RAS_GPU_RESET_MODE2_RESET) {
2530 				ras->gpu_reset_flags &= ~AMDGPU_RAS_GPU_RESET_MODE2_RESET;
2531 				reset_context.method = AMD_RESET_METHOD_MODE2;
2532 			}
2533 
2534 			/* Fatal error occurs in poison mode, mode1 reset is used to
2535 			 * recover gpu.
2536 			 */
2537 			if (ras->gpu_reset_flags & AMDGPU_RAS_GPU_RESET_MODE1_RESET) {
2538 				ras->gpu_reset_flags &= ~AMDGPU_RAS_GPU_RESET_MODE1_RESET;
2539 				set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
2540 
2541 				psp_fatal_error_recovery_quirk(&adev->psp);
2542 			}
2543 		}
2544 
2545 		amdgpu_device_gpu_recover(ras->adev, NULL, &reset_context);
2546 	}
2547 	atomic_set(&ras->in_recovery, 0);
2548 	if (hive) {
2549 		atomic_set(&hive->ras_recovery, 0);
2550 		amdgpu_put_xgmi_hive(hive);
2551 	}
2552 }
2553 
2554 /* alloc/realloc bps array */
2555 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
2556 		struct ras_err_handler_data *data, int pages)
2557 {
2558 	unsigned int old_space = data->count + data->space_left;
2559 	unsigned int new_space = old_space + pages;
2560 	unsigned int align_space = ALIGN(new_space, 512);
2561 	void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
2562 
2563 	if (!bps) {
2564 		return -ENOMEM;
2565 	}
2566 
2567 	if (data->bps) {
2568 		memcpy(bps, data->bps,
2569 				data->count * sizeof(*data->bps));
2570 		kfree(data->bps);
2571 	}
2572 
2573 	data->bps = bps;
2574 	data->space_left += align_space - old_space;
2575 	return 0;
2576 }
2577 
2578 /* it deal with vram only. */
2579 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
2580 		struct eeprom_table_record *bps, int pages)
2581 {
2582 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2583 	struct ras_err_handler_data *data;
2584 	int ret = 0;
2585 	uint32_t i;
2586 
2587 	if (!con || !con->eh_data || !bps || pages <= 0)
2588 		return 0;
2589 
2590 	mutex_lock(&con->recovery_lock);
2591 	data = con->eh_data;
2592 	if (!data)
2593 		goto out;
2594 
2595 	for (i = 0; i < pages; i++) {
2596 		if (amdgpu_ras_check_bad_page_unlock(con,
2597 			bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT))
2598 			continue;
2599 
2600 		if (!data->space_left &&
2601 			amdgpu_ras_realloc_eh_data_space(adev, data, 256)) {
2602 			ret = -ENOMEM;
2603 			goto out;
2604 		}
2605 
2606 		amdgpu_ras_reserve_page(adev, bps[i].retired_page);
2607 
2608 		memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps));
2609 		data->count++;
2610 		data->space_left--;
2611 	}
2612 out:
2613 	mutex_unlock(&con->recovery_lock);
2614 
2615 	return ret;
2616 }
2617 
2618 /*
2619  * write error record array to eeprom, the function should be
2620  * protected by recovery_lock
2621  * new_cnt: new added UE count, excluding reserved bad pages, can be NULL
2622  */
2623 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev,
2624 		unsigned long *new_cnt)
2625 {
2626 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2627 	struct ras_err_handler_data *data;
2628 	struct amdgpu_ras_eeprom_control *control;
2629 	int save_count;
2630 
2631 	if (!con || !con->eh_data) {
2632 		if (new_cnt)
2633 			*new_cnt = 0;
2634 
2635 		return 0;
2636 	}
2637 
2638 	mutex_lock(&con->recovery_lock);
2639 	control = &con->eeprom_control;
2640 	data = con->eh_data;
2641 	save_count = data->count - control->ras_num_recs;
2642 	mutex_unlock(&con->recovery_lock);
2643 
2644 	if (new_cnt)
2645 		*new_cnt = save_count / adev->umc.retire_unit;
2646 
2647 	/* only new entries are saved */
2648 	if (save_count > 0) {
2649 		if (amdgpu_ras_eeprom_append(control,
2650 					     &data->bps[control->ras_num_recs],
2651 					     save_count)) {
2652 			dev_err(adev->dev, "Failed to save EEPROM table data!");
2653 			return -EIO;
2654 		}
2655 
2656 		dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
2657 	}
2658 
2659 	return 0;
2660 }
2661 
2662 /*
2663  * read error record array in eeprom and reserve enough space for
2664  * storing new bad pages
2665  */
2666 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
2667 {
2668 	struct amdgpu_ras_eeprom_control *control =
2669 		&adev->psp.ras_context.ras->eeprom_control;
2670 	struct eeprom_table_record *bps;
2671 	int ret;
2672 
2673 	/* no bad page record, skip eeprom access */
2674 	if (control->ras_num_recs == 0 || amdgpu_bad_page_threshold == 0)
2675 		return 0;
2676 
2677 	bps = kcalloc(control->ras_num_recs, sizeof(*bps), GFP_KERNEL);
2678 	if (!bps)
2679 		return -ENOMEM;
2680 
2681 	ret = amdgpu_ras_eeprom_read(control, bps, control->ras_num_recs);
2682 	if (ret)
2683 		dev_err(adev->dev, "Failed to load EEPROM table records!");
2684 	else
2685 		ret = amdgpu_ras_add_bad_pages(adev, bps, control->ras_num_recs);
2686 
2687 	kfree(bps);
2688 	return ret;
2689 }
2690 
2691 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
2692 				uint64_t addr)
2693 {
2694 	struct ras_err_handler_data *data = con->eh_data;
2695 	int i;
2696 
2697 	addr >>= AMDGPU_GPU_PAGE_SHIFT;
2698 	for (i = 0; i < data->count; i++)
2699 		if (addr == data->bps[i].retired_page)
2700 			return true;
2701 
2702 	return false;
2703 }
2704 
2705 /*
2706  * check if an address belongs to bad page
2707  *
2708  * Note: this check is only for umc block
2709  */
2710 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
2711 				uint64_t addr)
2712 {
2713 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2714 	bool ret = false;
2715 
2716 	if (!con || !con->eh_data)
2717 		return ret;
2718 
2719 	mutex_lock(&con->recovery_lock);
2720 	ret = amdgpu_ras_check_bad_page_unlock(con, addr);
2721 	mutex_unlock(&con->recovery_lock);
2722 	return ret;
2723 }
2724 
2725 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
2726 					  uint32_t max_count)
2727 {
2728 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2729 
2730 	/*
2731 	 * Justification of value bad_page_cnt_threshold in ras structure
2732 	 *
2733 	 * Generally, 0 <= amdgpu_bad_page_threshold <= max record length
2734 	 * in eeprom or amdgpu_bad_page_threshold == -2, introduce two
2735 	 * scenarios accordingly.
2736 	 *
2737 	 * Bad page retirement enablement:
2738 	 *    - If amdgpu_bad_page_threshold = -2,
2739 	 *      bad_page_cnt_threshold = typical value by formula.
2740 	 *
2741 	 *    - When the value from user is 0 < amdgpu_bad_page_threshold <
2742 	 *      max record length in eeprom, use it directly.
2743 	 *
2744 	 * Bad page retirement disablement:
2745 	 *    - If amdgpu_bad_page_threshold = 0, bad page retirement
2746 	 *      functionality is disabled, and bad_page_cnt_threshold will
2747 	 *      take no effect.
2748 	 */
2749 
2750 	if (amdgpu_bad_page_threshold < 0) {
2751 		u64 val = adev->gmc.mc_vram_size;
2752 
2753 		do_div(val, RAS_BAD_PAGE_COVER);
2754 		con->bad_page_cnt_threshold = min(lower_32_bits(val),
2755 						  max_count);
2756 	} else {
2757 		con->bad_page_cnt_threshold = min_t(int, max_count,
2758 						    amdgpu_bad_page_threshold);
2759 	}
2760 }
2761 
2762 int amdgpu_ras_put_poison_req(struct amdgpu_device *adev,
2763 		enum amdgpu_ras_block block, uint16_t pasid,
2764 		pasid_notify pasid_fn, void *data, uint32_t reset)
2765 {
2766 	int ret = 0;
2767 	struct ras_poison_msg poison_msg;
2768 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2769 
2770 	memset(&poison_msg, 0, sizeof(poison_msg));
2771 	poison_msg.block = block;
2772 	poison_msg.pasid = pasid;
2773 	poison_msg.reset = reset;
2774 	poison_msg.pasid_fn = pasid_fn;
2775 	poison_msg.data = data;
2776 
2777 	ret = kfifo_put(&con->poison_fifo, poison_msg);
2778 	if (!ret) {
2779 		dev_err(adev->dev, "Poison message fifo is full!\n");
2780 		return -ENOSPC;
2781 	}
2782 
2783 	return 0;
2784 }
2785 
2786 static int amdgpu_ras_get_poison_req(struct amdgpu_device *adev,
2787 		struct ras_poison_msg *poison_msg)
2788 {
2789 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2790 
2791 	return kfifo_get(&con->poison_fifo, poison_msg);
2792 }
2793 
2794 static void amdgpu_ras_ecc_log_init(struct ras_ecc_log_info *ecc_log)
2795 {
2796 	mutex_init(&ecc_log->lock);
2797 
2798 	/* Set any value as siphash key */
2799 	memset(&ecc_log->ecc_key, 0xad, sizeof(ecc_log->ecc_key));
2800 
2801 	INIT_RADIX_TREE(&ecc_log->de_page_tree, GFP_KERNEL);
2802 	ecc_log->de_updated = false;
2803 }
2804 
2805 static void amdgpu_ras_ecc_log_fini(struct ras_ecc_log_info *ecc_log)
2806 {
2807 	struct radix_tree_iter iter;
2808 	void __rcu **slot;
2809 	struct ras_ecc_err *ecc_err;
2810 
2811 	mutex_lock(&ecc_log->lock);
2812 	radix_tree_for_each_slot(slot, &ecc_log->de_page_tree, &iter, 0) {
2813 		ecc_err = radix_tree_deref_slot(slot);
2814 		kfree(ecc_err->err_pages.pfn);
2815 		kfree(ecc_err);
2816 		radix_tree_iter_delete(&ecc_log->de_page_tree, &iter, slot);
2817 	}
2818 	mutex_unlock(&ecc_log->lock);
2819 
2820 	mutex_destroy(&ecc_log->lock);
2821 	ecc_log->de_updated = false;
2822 }
2823 
2824 static void amdgpu_ras_do_page_retirement(struct work_struct *work)
2825 {
2826 	struct amdgpu_ras *con = container_of(work, struct amdgpu_ras,
2827 					      page_retirement_dwork.work);
2828 	struct amdgpu_device *adev = con->adev;
2829 	struct ras_err_data err_data;
2830 	unsigned long err_cnt;
2831 
2832 	if (amdgpu_in_reset(adev) || amdgpu_ras_in_recovery(adev))
2833 		return;
2834 
2835 	amdgpu_ras_error_data_init(&err_data);
2836 
2837 	amdgpu_umc_handle_bad_pages(adev, &err_data);
2838 	err_cnt = err_data.err_addr_cnt;
2839 
2840 	amdgpu_ras_error_data_fini(&err_data);
2841 
2842 	if (err_cnt && con->is_rma)
2843 		amdgpu_ras_reset_gpu(adev);
2844 
2845 	mutex_lock(&con->umc_ecc_log.lock);
2846 	if (radix_tree_tagged(&con->umc_ecc_log.de_page_tree,
2847 				UMC_ECC_NEW_DETECTED_TAG))
2848 		schedule_delayed_work(&con->page_retirement_dwork,
2849 			msecs_to_jiffies(AMDGPU_RAS_RETIRE_PAGE_INTERVAL));
2850 	mutex_unlock(&con->umc_ecc_log.lock);
2851 }
2852 
2853 static void amdgpu_ras_poison_creation_handler(struct amdgpu_device *adev,
2854 				uint32_t timeout_ms)
2855 {
2856 	int ret = 0;
2857 	struct ras_ecc_log_info *ecc_log;
2858 	struct ras_query_if info;
2859 	uint32_t timeout = timeout_ms;
2860 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
2861 
2862 	memset(&info, 0, sizeof(info));
2863 	info.head.block = AMDGPU_RAS_BLOCK__UMC;
2864 
2865 	ecc_log = &ras->umc_ecc_log;
2866 	ecc_log->de_updated = false;
2867 	do {
2868 		ret = amdgpu_ras_query_error_status(adev, &info);
2869 		if (ret) {
2870 			dev_err(adev->dev, "Failed to query ras error! ret:%d\n", ret);
2871 			return;
2872 		}
2873 
2874 		if (timeout && !ecc_log->de_updated) {
2875 			msleep(1);
2876 			timeout--;
2877 		}
2878 	} while (timeout && !ecc_log->de_updated);
2879 
2880 	if (timeout_ms && !timeout) {
2881 		dev_warn(adev->dev, "Can't find deferred error\n");
2882 		return;
2883 	}
2884 
2885 	if (!ret)
2886 		schedule_delayed_work(&ras->page_retirement_dwork, 0);
2887 }
2888 
2889 static int amdgpu_ras_poison_consumption_handler(struct amdgpu_device *adev,
2890 			struct ras_poison_msg *poison_msg)
2891 {
2892 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2893 	uint32_t reset = poison_msg->reset;
2894 	uint16_t pasid = poison_msg->pasid;
2895 
2896 	kgd2kfd_set_sram_ecc_flag(adev->kfd.dev);
2897 
2898 	if (poison_msg->pasid_fn)
2899 		poison_msg->pasid_fn(adev, pasid, poison_msg->data);
2900 
2901 	/* for RMA, amdgpu_ras_poison_creation_handler will trigger gpu reset */
2902 	if (reset && !con->is_rma) {
2903 		flush_delayed_work(&con->page_retirement_dwork);
2904 
2905 		con->gpu_reset_flags |= reset;
2906 		amdgpu_ras_reset_gpu(adev);
2907 	}
2908 
2909 	return 0;
2910 }
2911 
2912 static int amdgpu_ras_page_retirement_thread(void *param)
2913 {
2914 	struct amdgpu_device *adev = (struct amdgpu_device *)param;
2915 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2916 	struct ras_poison_msg poison_msg;
2917 	enum amdgpu_ras_block ras_block;
2918 	bool poison_creation_is_handled = false;
2919 
2920 	while (!kthread_should_stop()) {
2921 
2922 		wait_event_interruptible(con->page_retirement_wq,
2923 				kthread_should_stop() ||
2924 				atomic_read(&con->page_retirement_req_cnt));
2925 
2926 		if (kthread_should_stop())
2927 			break;
2928 
2929 		atomic_dec(&con->page_retirement_req_cnt);
2930 
2931 		if (!amdgpu_ras_get_poison_req(adev, &poison_msg))
2932 			continue;
2933 
2934 		ras_block = poison_msg.block;
2935 
2936 		dev_dbg(adev->dev, "Start processing ras block %s(%d)\n",
2937 				ras_block_str(ras_block), ras_block);
2938 
2939 		if (ras_block == AMDGPU_RAS_BLOCK__UMC) {
2940 			amdgpu_ras_poison_creation_handler(adev,
2941 				MAX_UMC_POISON_POLLING_TIME_ASYNC);
2942 			poison_creation_is_handled = true;
2943 		} else {
2944 			/* poison_creation_is_handled:
2945 			 *   false: no poison creation interrupt, but it has poison
2946 			 *          consumption interrupt.
2947 			 *   true: It has poison creation interrupt at the beginning,
2948 			 *         but it has no poison creation interrupt later.
2949 			 */
2950 			amdgpu_ras_poison_creation_handler(adev,
2951 					poison_creation_is_handled ?
2952 					0 : MAX_UMC_POISON_POLLING_TIME_ASYNC);
2953 
2954 			amdgpu_ras_poison_consumption_handler(adev, &poison_msg);
2955 			poison_creation_is_handled = false;
2956 		}
2957 	}
2958 
2959 	return 0;
2960 }
2961 
2962 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
2963 {
2964 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2965 	struct ras_err_handler_data **data;
2966 	u32  max_eeprom_records_count = 0;
2967 	int ret;
2968 
2969 	if (!con || amdgpu_sriov_vf(adev))
2970 		return 0;
2971 
2972 	/* Allow access to RAS EEPROM via debugfs, when the ASIC
2973 	 * supports RAS and debugfs is enabled, but when
2974 	 * adev->ras_enabled is unset, i.e. when "ras_enable"
2975 	 * module parameter is set to 0.
2976 	 */
2977 	con->adev = adev;
2978 
2979 	if (!adev->ras_enabled)
2980 		return 0;
2981 
2982 	data = &con->eh_data;
2983 	*data = kzalloc(sizeof(**data), GFP_KERNEL);
2984 	if (!*data) {
2985 		ret = -ENOMEM;
2986 		goto out;
2987 	}
2988 
2989 	mutex_init(&con->recovery_lock);
2990 	INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
2991 	atomic_set(&con->in_recovery, 0);
2992 	con->eeprom_control.bad_channel_bitmap = 0;
2993 
2994 	max_eeprom_records_count = amdgpu_ras_eeprom_max_record_count(&con->eeprom_control);
2995 	amdgpu_ras_validate_threshold(adev, max_eeprom_records_count);
2996 
2997 	/* Todo: During test the SMU might fail to read the eeprom through I2C
2998 	 * when the GPU is pending on XGMI reset during probe time
2999 	 * (Mostly after second bus reset), skip it now
3000 	 */
3001 	if (adev->gmc.xgmi.pending_reset)
3002 		return 0;
3003 	ret = amdgpu_ras_eeprom_init(&con->eeprom_control);
3004 	/*
3005 	 * This calling fails when is_rma is true or
3006 	 * ret != 0.
3007 	 */
3008 	if (con->is_rma || ret)
3009 		goto free;
3010 
3011 	if (con->eeprom_control.ras_num_recs) {
3012 		ret = amdgpu_ras_load_bad_pages(adev);
3013 		if (ret)
3014 			goto free;
3015 
3016 		amdgpu_dpm_send_hbm_bad_pages_num(adev, con->eeprom_control.ras_num_recs);
3017 
3018 		if (con->update_channel_flag == true) {
3019 			amdgpu_dpm_send_hbm_bad_channel_flag(adev, con->eeprom_control.bad_channel_bitmap);
3020 			con->update_channel_flag = false;
3021 		}
3022 	}
3023 
3024 	mutex_init(&con->page_rsv_lock);
3025 	INIT_KFIFO(con->poison_fifo);
3026 	mutex_init(&con->page_retirement_lock);
3027 	init_waitqueue_head(&con->page_retirement_wq);
3028 	atomic_set(&con->page_retirement_req_cnt, 0);
3029 	con->page_retirement_thread =
3030 		kthread_run(amdgpu_ras_page_retirement_thread, adev, "umc_page_retirement");
3031 	if (IS_ERR(con->page_retirement_thread)) {
3032 		con->page_retirement_thread = NULL;
3033 		dev_warn(adev->dev, "Failed to create umc_page_retirement thread!!!\n");
3034 	}
3035 
3036 	INIT_DELAYED_WORK(&con->page_retirement_dwork, amdgpu_ras_do_page_retirement);
3037 	amdgpu_ras_ecc_log_init(&con->umc_ecc_log);
3038 #ifdef CONFIG_X86_MCE_AMD
3039 	if ((adev->asic_type == CHIP_ALDEBARAN) &&
3040 	    (adev->gmc.xgmi.connected_to_cpu))
3041 		amdgpu_register_bad_pages_mca_notifier(adev);
3042 #endif
3043 	return 0;
3044 
3045 free:
3046 	kfree((*data)->bps);
3047 	kfree(*data);
3048 	con->eh_data = NULL;
3049 out:
3050 	dev_warn(adev->dev, "Failed to initialize ras recovery! (%d)\n", ret);
3051 
3052 	/*
3053 	 * Except error threshold exceeding case, other failure cases in this
3054 	 * function would not fail amdgpu driver init.
3055 	 */
3056 	if (!con->is_rma)
3057 		ret = 0;
3058 	else
3059 		ret = -EINVAL;
3060 
3061 	return ret;
3062 }
3063 
3064 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
3065 {
3066 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3067 	struct ras_err_handler_data *data = con->eh_data;
3068 
3069 	/* recovery_init failed to init it, fini is useless */
3070 	if (!data)
3071 		return 0;
3072 
3073 	if (con->page_retirement_thread)
3074 		kthread_stop(con->page_retirement_thread);
3075 
3076 	atomic_set(&con->page_retirement_req_cnt, 0);
3077 
3078 	mutex_destroy(&con->page_rsv_lock);
3079 
3080 	cancel_work_sync(&con->recovery_work);
3081 
3082 	cancel_delayed_work_sync(&con->page_retirement_dwork);
3083 
3084 	amdgpu_ras_ecc_log_fini(&con->umc_ecc_log);
3085 
3086 	mutex_lock(&con->recovery_lock);
3087 	con->eh_data = NULL;
3088 	kfree(data->bps);
3089 	kfree(data);
3090 	mutex_unlock(&con->recovery_lock);
3091 
3092 	return 0;
3093 }
3094 /* recovery end */
3095 
3096 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev)
3097 {
3098 	if (amdgpu_sriov_vf(adev)) {
3099 		switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
3100 		case IP_VERSION(13, 0, 2):
3101 		case IP_VERSION(13, 0, 6):
3102 		case IP_VERSION(13, 0, 14):
3103 			return true;
3104 		default:
3105 			return false;
3106 		}
3107 	}
3108 
3109 	if (adev->asic_type == CHIP_IP_DISCOVERY) {
3110 		switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
3111 		case IP_VERSION(13, 0, 0):
3112 		case IP_VERSION(13, 0, 6):
3113 		case IP_VERSION(13, 0, 10):
3114 		case IP_VERSION(13, 0, 14):
3115 			return true;
3116 		default:
3117 			return false;
3118 		}
3119 	}
3120 
3121 	return adev->asic_type == CHIP_VEGA10 ||
3122 		adev->asic_type == CHIP_VEGA20 ||
3123 		adev->asic_type == CHIP_ARCTURUS ||
3124 		adev->asic_type == CHIP_ALDEBARAN ||
3125 		adev->asic_type == CHIP_SIENNA_CICHLID;
3126 }
3127 
3128 /*
3129  * this is workaround for vega20 workstation sku,
3130  * force enable gfx ras, ignore vbios gfx ras flag
3131  * due to GC EDC can not write
3132  */
3133 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev)
3134 {
3135 	struct atom_context *ctx = adev->mode_info.atom_context;
3136 
3137 	if (!ctx)
3138 		return;
3139 
3140 	if (strnstr(ctx->vbios_pn, "D16406",
3141 		    sizeof(ctx->vbios_pn)) ||
3142 		strnstr(ctx->vbios_pn, "D36002",
3143 			sizeof(ctx->vbios_pn)))
3144 		adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX);
3145 }
3146 
3147 /* Query ras capablity via atomfirmware interface */
3148 static void amdgpu_ras_query_ras_capablity_from_vbios(struct amdgpu_device *adev)
3149 {
3150 	/* mem_ecc cap */
3151 	if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
3152 		dev_info(adev->dev, "MEM ECC is active.\n");
3153 		adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC |
3154 					 1 << AMDGPU_RAS_BLOCK__DF);
3155 	} else {
3156 		dev_info(adev->dev, "MEM ECC is not presented.\n");
3157 	}
3158 
3159 	/* sram_ecc cap */
3160 	if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
3161 		dev_info(adev->dev, "SRAM ECC is active.\n");
3162 		if (!amdgpu_sriov_vf(adev))
3163 			adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
3164 						  1 << AMDGPU_RAS_BLOCK__DF);
3165 		else
3166 			adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__PCIE_BIF |
3167 						 1 << AMDGPU_RAS_BLOCK__SDMA |
3168 						 1 << AMDGPU_RAS_BLOCK__GFX);
3169 
3170 		/*
3171 		 * VCN/JPEG RAS can be supported on both bare metal and
3172 		 * SRIOV environment
3173 		 */
3174 		if (amdgpu_ip_version(adev, VCN_HWIP, 0) == IP_VERSION(2, 6, 0) ||
3175 		    amdgpu_ip_version(adev, VCN_HWIP, 0) == IP_VERSION(4, 0, 0) ||
3176 		    amdgpu_ip_version(adev, VCN_HWIP, 0) == IP_VERSION(4, 0, 3))
3177 			adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__VCN |
3178 						 1 << AMDGPU_RAS_BLOCK__JPEG);
3179 		else
3180 			adev->ras_hw_enabled &= ~(1 << AMDGPU_RAS_BLOCK__VCN |
3181 						  1 << AMDGPU_RAS_BLOCK__JPEG);
3182 
3183 		/*
3184 		 * XGMI RAS is not supported if xgmi num physical nodes
3185 		 * is zero
3186 		 */
3187 		if (!adev->gmc.xgmi.num_physical_nodes)
3188 			adev->ras_hw_enabled &= ~(1 << AMDGPU_RAS_BLOCK__XGMI_WAFL);
3189 	} else {
3190 		dev_info(adev->dev, "SRAM ECC is not presented.\n");
3191 	}
3192 }
3193 
3194 /* Query poison mode from umc/df IP callbacks */
3195 static void amdgpu_ras_query_poison_mode(struct amdgpu_device *adev)
3196 {
3197 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3198 	bool df_poison, umc_poison;
3199 
3200 	/* poison setting is useless on SRIOV guest */
3201 	if (amdgpu_sriov_vf(adev) || !con)
3202 		return;
3203 
3204 	/* Init poison supported flag, the default value is false */
3205 	if (adev->gmc.xgmi.connected_to_cpu ||
3206 	    adev->gmc.is_app_apu) {
3207 		/* enabled by default when GPU is connected to CPU */
3208 		con->poison_supported = true;
3209 	} else if (adev->df.funcs &&
3210 	    adev->df.funcs->query_ras_poison_mode &&
3211 	    adev->umc.ras &&
3212 	    adev->umc.ras->query_ras_poison_mode) {
3213 		df_poison =
3214 			adev->df.funcs->query_ras_poison_mode(adev);
3215 		umc_poison =
3216 			adev->umc.ras->query_ras_poison_mode(adev);
3217 
3218 		/* Only poison is set in both DF and UMC, we can support it */
3219 		if (df_poison && umc_poison)
3220 			con->poison_supported = true;
3221 		else if (df_poison != umc_poison)
3222 			dev_warn(adev->dev,
3223 				"Poison setting is inconsistent in DF/UMC(%d:%d)!\n",
3224 				df_poison, umc_poison);
3225 	}
3226 }
3227 
3228 /*
3229  * check hardware's ras ability which will be saved in hw_supported.
3230  * if hardware does not support ras, we can skip some ras initializtion and
3231  * forbid some ras operations from IP.
3232  * if software itself, say boot parameter, limit the ras ability. We still
3233  * need allow IP do some limited operations, like disable. In such case,
3234  * we have to initialize ras as normal. but need check if operation is
3235  * allowed or not in each function.
3236  */
3237 static void amdgpu_ras_check_supported(struct amdgpu_device *adev)
3238 {
3239 	adev->ras_hw_enabled = adev->ras_enabled = 0;
3240 
3241 	if (!amdgpu_ras_asic_supported(adev))
3242 		return;
3243 
3244 	/* query ras capability from psp */
3245 	if (amdgpu_psp_get_ras_capability(&adev->psp))
3246 		goto init_ras_enabled_flag;
3247 
3248 	/* query ras capablity from bios */
3249 	if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) {
3250 		amdgpu_ras_query_ras_capablity_from_vbios(adev);
3251 	} else {
3252 		/* driver only manages a few IP blocks RAS feature
3253 		 * when GPU is connected cpu through XGMI */
3254 		adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX |
3255 					   1 << AMDGPU_RAS_BLOCK__SDMA |
3256 					   1 << AMDGPU_RAS_BLOCK__MMHUB);
3257 	}
3258 
3259 	/* apply asic specific settings (vega20 only for now) */
3260 	amdgpu_ras_get_quirks(adev);
3261 
3262 	/* query poison mode from umc/df ip callback */
3263 	amdgpu_ras_query_poison_mode(adev);
3264 
3265 init_ras_enabled_flag:
3266 	/* hw_supported needs to be aligned with RAS block mask. */
3267 	adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK;
3268 
3269 	adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 :
3270 		adev->ras_hw_enabled & amdgpu_ras_mask;
3271 
3272 	/* aca is disabled by default */
3273 	adev->aca.is_enabled = false;
3274 }
3275 
3276 static void amdgpu_ras_counte_dw(struct work_struct *work)
3277 {
3278 	struct amdgpu_ras *con = container_of(work, struct amdgpu_ras,
3279 					      ras_counte_delay_work.work);
3280 	struct amdgpu_device *adev = con->adev;
3281 	struct drm_device *dev = adev_to_drm(adev);
3282 	unsigned long ce_count, ue_count;
3283 	int res;
3284 
3285 	res = pm_runtime_get_sync(dev->dev);
3286 	if (res < 0)
3287 		goto Out;
3288 
3289 	/* Cache new values.
3290 	 */
3291 	if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count, NULL) == 0) {
3292 		atomic_set(&con->ras_ce_count, ce_count);
3293 		atomic_set(&con->ras_ue_count, ue_count);
3294 	}
3295 
3296 	pm_runtime_mark_last_busy(dev->dev);
3297 Out:
3298 	pm_runtime_put_autosuspend(dev->dev);
3299 }
3300 
3301 static int amdgpu_get_ras_schema(struct amdgpu_device *adev)
3302 {
3303 	return  amdgpu_ras_is_poison_mode_supported(adev) ? AMDGPU_RAS_ERROR__POISON : 0 |
3304 			AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE |
3305 			AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE |
3306 			AMDGPU_RAS_ERROR__PARITY;
3307 }
3308 
3309 static void ras_event_mgr_init(struct ras_event_manager *mgr)
3310 {
3311 	int i;
3312 
3313 	for (i = 0; i < ARRAY_SIZE(mgr->seqnos); i++)
3314 		atomic64_set(&mgr->seqnos[i], 0);
3315 }
3316 
3317 static void amdgpu_ras_event_mgr_init(struct amdgpu_device *adev)
3318 {
3319 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
3320 	struct amdgpu_hive_info *hive;
3321 
3322 	if (!ras)
3323 		return;
3324 
3325 	hive = amdgpu_get_xgmi_hive(adev);
3326 	ras->event_mgr = hive ? &hive->event_mgr : &ras->__event_mgr;
3327 
3328 	/* init event manager with node 0 on xgmi system */
3329 	if (!amdgpu_in_reset(adev)) {
3330 		if (!hive || adev->gmc.xgmi.node_id == 0)
3331 			ras_event_mgr_init(ras->event_mgr);
3332 	}
3333 
3334 	if (hive)
3335 		amdgpu_put_xgmi_hive(hive);
3336 }
3337 
3338 static void amdgpu_ras_init_reserved_vram_size(struct amdgpu_device *adev)
3339 {
3340 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3341 
3342 	if (!con || (adev->flags & AMD_IS_APU))
3343 		return;
3344 
3345 	switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
3346 	case IP_VERSION(13, 0, 2):
3347 	case IP_VERSION(13, 0, 6):
3348 	case IP_VERSION(13, 0, 14):
3349 		con->reserved_pages_in_bytes = AMDGPU_RAS_RESERVED_VRAM_SIZE;
3350 		break;
3351 	default:
3352 		break;
3353 	}
3354 }
3355 
3356 int amdgpu_ras_init(struct amdgpu_device *adev)
3357 {
3358 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3359 	int r;
3360 
3361 	if (con)
3362 		return 0;
3363 
3364 	con = kzalloc(sizeof(*con) +
3365 			sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT +
3366 			sizeof(struct ras_manager) * AMDGPU_RAS_MCA_BLOCK_COUNT,
3367 			GFP_KERNEL);
3368 	if (!con)
3369 		return -ENOMEM;
3370 
3371 	con->adev = adev;
3372 	INIT_DELAYED_WORK(&con->ras_counte_delay_work, amdgpu_ras_counte_dw);
3373 	atomic_set(&con->ras_ce_count, 0);
3374 	atomic_set(&con->ras_ue_count, 0);
3375 
3376 	con->objs = (struct ras_manager *)(con + 1);
3377 
3378 	amdgpu_ras_set_context(adev, con);
3379 
3380 	amdgpu_ras_check_supported(adev);
3381 
3382 	if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) {
3383 		/* set gfx block ras context feature for VEGA20 Gaming
3384 		 * send ras disable cmd to ras ta during ras late init.
3385 		 */
3386 		if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) {
3387 			con->features |= BIT(AMDGPU_RAS_BLOCK__GFX);
3388 
3389 			return 0;
3390 		}
3391 
3392 		r = 0;
3393 		goto release_con;
3394 	}
3395 
3396 	con->update_channel_flag = false;
3397 	con->features = 0;
3398 	con->schema = 0;
3399 	INIT_LIST_HEAD(&con->head);
3400 	/* Might need get this flag from vbios. */
3401 	con->flags = RAS_DEFAULT_FLAGS;
3402 
3403 	/* initialize nbio ras function ahead of any other
3404 	 * ras functions so hardware fatal error interrupt
3405 	 * can be enabled as early as possible */
3406 	switch (amdgpu_ip_version(adev, NBIO_HWIP, 0)) {
3407 	case IP_VERSION(7, 4, 0):
3408 	case IP_VERSION(7, 4, 1):
3409 	case IP_VERSION(7, 4, 4):
3410 		if (!adev->gmc.xgmi.connected_to_cpu)
3411 			adev->nbio.ras = &nbio_v7_4_ras;
3412 		break;
3413 	case IP_VERSION(4, 3, 0):
3414 		if (adev->ras_hw_enabled & (1 << AMDGPU_RAS_BLOCK__DF))
3415 			/* unlike other generation of nbio ras,
3416 			 * nbio v4_3 only support fatal error interrupt
3417 			 * to inform software that DF is freezed due to
3418 			 * system fatal error event. driver should not
3419 			 * enable nbio ras in such case. Instead,
3420 			 * check DF RAS */
3421 			adev->nbio.ras = &nbio_v4_3_ras;
3422 		break;
3423 	case IP_VERSION(7, 9, 0):
3424 		if (!adev->gmc.is_app_apu)
3425 			adev->nbio.ras = &nbio_v7_9_ras;
3426 		break;
3427 	default:
3428 		/* nbio ras is not available */
3429 		break;
3430 	}
3431 
3432 	/* nbio ras block needs to be enabled ahead of other ras blocks
3433 	 * to handle fatal error */
3434 	r = amdgpu_nbio_ras_sw_init(adev);
3435 	if (r)
3436 		return r;
3437 
3438 	if (adev->nbio.ras &&
3439 	    adev->nbio.ras->init_ras_controller_interrupt) {
3440 		r = adev->nbio.ras->init_ras_controller_interrupt(adev);
3441 		if (r)
3442 			goto release_con;
3443 	}
3444 
3445 	if (adev->nbio.ras &&
3446 	    adev->nbio.ras->init_ras_err_event_athub_interrupt) {
3447 		r = adev->nbio.ras->init_ras_err_event_athub_interrupt(adev);
3448 		if (r)
3449 			goto release_con;
3450 	}
3451 
3452 	/* Packed socket_id to ras feature mask bits[31:29] */
3453 	if (adev->smuio.funcs &&
3454 	    adev->smuio.funcs->get_socket_id)
3455 		con->features |= ((adev->smuio.funcs->get_socket_id(adev)) <<
3456 					AMDGPU_RAS_FEATURES_SOCKETID_SHIFT);
3457 
3458 	/* Get RAS schema for particular SOC */
3459 	con->schema = amdgpu_get_ras_schema(adev);
3460 
3461 	amdgpu_ras_init_reserved_vram_size(adev);
3462 
3463 	if (amdgpu_ras_fs_init(adev)) {
3464 		r = -EINVAL;
3465 		goto release_con;
3466 	}
3467 
3468 	if (amdgpu_ras_aca_is_supported(adev)) {
3469 		if (amdgpu_aca_is_enabled(adev))
3470 			r = amdgpu_aca_init(adev);
3471 		else
3472 			r = amdgpu_mca_init(adev);
3473 		if (r)
3474 			goto release_con;
3475 	}
3476 
3477 	dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
3478 		 "hardware ability[%x] ras_mask[%x]\n",
3479 		 adev->ras_hw_enabled, adev->ras_enabled);
3480 
3481 	return 0;
3482 release_con:
3483 	amdgpu_ras_set_context(adev, NULL);
3484 	kfree(con);
3485 
3486 	return r;
3487 }
3488 
3489 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev)
3490 {
3491 	if (adev->gmc.xgmi.connected_to_cpu ||
3492 	    adev->gmc.is_app_apu)
3493 		return 1;
3494 	return 0;
3495 }
3496 
3497 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev,
3498 					struct ras_common_if *ras_block)
3499 {
3500 	struct ras_query_if info = {
3501 		.head = *ras_block,
3502 	};
3503 
3504 	if (!amdgpu_persistent_edc_harvesting_supported(adev))
3505 		return 0;
3506 
3507 	if (amdgpu_ras_query_error_status(adev, &info) != 0)
3508 		DRM_WARN("RAS init harvest failure");
3509 
3510 	if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0)
3511 		DRM_WARN("RAS init harvest reset failure");
3512 
3513 	return 0;
3514 }
3515 
3516 bool amdgpu_ras_is_poison_mode_supported(struct amdgpu_device *adev)
3517 {
3518        struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3519 
3520        if (!con)
3521                return false;
3522 
3523        return con->poison_supported;
3524 }
3525 
3526 /* helper function to handle common stuff in ip late init phase */
3527 int amdgpu_ras_block_late_init(struct amdgpu_device *adev,
3528 			 struct ras_common_if *ras_block)
3529 {
3530 	struct amdgpu_ras_block_object *ras_obj = NULL;
3531 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3532 	struct ras_query_if *query_info;
3533 	unsigned long ue_count, ce_count;
3534 	int r;
3535 
3536 	/* disable RAS feature per IP block if it is not supported */
3537 	if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
3538 		amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
3539 		return 0;
3540 	}
3541 
3542 	r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
3543 	if (r) {
3544 		if (adev->in_suspend || amdgpu_in_reset(adev)) {
3545 			/* in resume phase, if fail to enable ras,
3546 			 * clean up all ras fs nodes, and disable ras */
3547 			goto cleanup;
3548 		} else
3549 			return r;
3550 	}
3551 
3552 	/* check for errors on warm reset edc persisant supported ASIC */
3553 	amdgpu_persistent_edc_harvesting(adev, ras_block);
3554 
3555 	/* in resume phase, no need to create ras fs node */
3556 	if (adev->in_suspend || amdgpu_in_reset(adev))
3557 		return 0;
3558 
3559 	ras_obj = container_of(ras_block, struct amdgpu_ras_block_object, ras_comm);
3560 	if (ras_obj->ras_cb || (ras_obj->hw_ops &&
3561 	    (ras_obj->hw_ops->query_poison_status ||
3562 	    ras_obj->hw_ops->handle_poison_consumption))) {
3563 		r = amdgpu_ras_interrupt_add_handler(adev, ras_block);
3564 		if (r)
3565 			goto cleanup;
3566 	}
3567 
3568 	if (ras_obj->hw_ops &&
3569 	    (ras_obj->hw_ops->query_ras_error_count ||
3570 	     ras_obj->hw_ops->query_ras_error_status)) {
3571 		r = amdgpu_ras_sysfs_create(adev, ras_block);
3572 		if (r)
3573 			goto interrupt;
3574 
3575 		/* Those are the cached values at init.
3576 		 */
3577 		query_info = kzalloc(sizeof(*query_info), GFP_KERNEL);
3578 		if (!query_info)
3579 			return -ENOMEM;
3580 		memcpy(&query_info->head, ras_block, sizeof(struct ras_common_if));
3581 
3582 		if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count, query_info) == 0) {
3583 			atomic_set(&con->ras_ce_count, ce_count);
3584 			atomic_set(&con->ras_ue_count, ue_count);
3585 		}
3586 
3587 		kfree(query_info);
3588 	}
3589 
3590 	return 0;
3591 
3592 interrupt:
3593 	if (ras_obj->ras_cb)
3594 		amdgpu_ras_interrupt_remove_handler(adev, ras_block);
3595 cleanup:
3596 	amdgpu_ras_feature_enable(adev, ras_block, 0);
3597 	return r;
3598 }
3599 
3600 static int amdgpu_ras_block_late_init_default(struct amdgpu_device *adev,
3601 			 struct ras_common_if *ras_block)
3602 {
3603 	return amdgpu_ras_block_late_init(adev, ras_block);
3604 }
3605 
3606 /* helper function to remove ras fs node and interrupt handler */
3607 void amdgpu_ras_block_late_fini(struct amdgpu_device *adev,
3608 			  struct ras_common_if *ras_block)
3609 {
3610 	struct amdgpu_ras_block_object *ras_obj;
3611 	if (!ras_block)
3612 		return;
3613 
3614 	amdgpu_ras_sysfs_remove(adev, ras_block);
3615 
3616 	ras_obj = container_of(ras_block, struct amdgpu_ras_block_object, ras_comm);
3617 	if (ras_obj->ras_cb)
3618 		amdgpu_ras_interrupt_remove_handler(adev, ras_block);
3619 }
3620 
3621 static void amdgpu_ras_block_late_fini_default(struct amdgpu_device *adev,
3622 			  struct ras_common_if *ras_block)
3623 {
3624 	return amdgpu_ras_block_late_fini(adev, ras_block);
3625 }
3626 
3627 /* do some init work after IP late init as dependence.
3628  * and it runs in resume/gpu reset/booting up cases.
3629  */
3630 void amdgpu_ras_resume(struct amdgpu_device *adev)
3631 {
3632 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3633 	struct ras_manager *obj, *tmp;
3634 
3635 	if (!adev->ras_enabled || !con) {
3636 		/* clean ras context for VEGA20 Gaming after send ras disable cmd */
3637 		amdgpu_release_ras_context(adev);
3638 
3639 		return;
3640 	}
3641 
3642 	if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
3643 		/* Set up all other IPs which are not implemented. There is a
3644 		 * tricky thing that IP's actual ras error type should be
3645 		 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
3646 		 * ERROR_NONE make sense anyway.
3647 		 */
3648 		amdgpu_ras_enable_all_features(adev, 1);
3649 
3650 		/* We enable ras on all hw_supported block, but as boot
3651 		 * parameter might disable some of them and one or more IP has
3652 		 * not implemented yet. So we disable them on behalf.
3653 		 */
3654 		list_for_each_entry_safe(obj, tmp, &con->head, node) {
3655 			if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
3656 				amdgpu_ras_feature_enable(adev, &obj->head, 0);
3657 				/* there should be no any reference. */
3658 				WARN_ON(alive_obj(obj));
3659 			}
3660 		}
3661 	}
3662 }
3663 
3664 void amdgpu_ras_suspend(struct amdgpu_device *adev)
3665 {
3666 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3667 
3668 	if (!adev->ras_enabled || !con)
3669 		return;
3670 
3671 	amdgpu_ras_disable_all_features(adev, 0);
3672 	/* Make sure all ras objects are disabled. */
3673 	if (AMDGPU_RAS_GET_FEATURES(con->features))
3674 		amdgpu_ras_disable_all_features(adev, 1);
3675 }
3676 
3677 int amdgpu_ras_late_init(struct amdgpu_device *adev)
3678 {
3679 	struct amdgpu_ras_block_list *node, *tmp;
3680 	struct amdgpu_ras_block_object *obj;
3681 	int r;
3682 
3683 	amdgpu_ras_event_mgr_init(adev);
3684 
3685 	if (amdgpu_ras_aca_is_supported(adev)) {
3686 		if (amdgpu_in_reset(adev)) {
3687 			if (amdgpu_aca_is_enabled(adev))
3688 				r = amdgpu_aca_reset(adev);
3689 			else
3690 				r = amdgpu_mca_reset(adev);
3691 			if (r)
3692 				return r;
3693 		}
3694 
3695 		if (!amdgpu_sriov_vf(adev)) {
3696 			if (amdgpu_aca_is_enabled(adev))
3697 				amdgpu_ras_set_aca_debug_mode(adev, false);
3698 			else
3699 				amdgpu_ras_set_mca_debug_mode(adev, false);
3700 		}
3701 	}
3702 
3703 	/* Guest side doesn't need init ras feature */
3704 	if (amdgpu_sriov_vf(adev))
3705 		return 0;
3706 
3707 	list_for_each_entry_safe(node, tmp, &adev->ras_list, node) {
3708 		obj = node->ras_obj;
3709 		if (!obj) {
3710 			dev_warn(adev->dev, "Warning: abnormal ras list node.\n");
3711 			continue;
3712 		}
3713 
3714 		if (!amdgpu_ras_is_supported(adev, obj->ras_comm.block))
3715 			continue;
3716 
3717 		if (obj->ras_late_init) {
3718 			r = obj->ras_late_init(adev, &obj->ras_comm);
3719 			if (r) {
3720 				dev_err(adev->dev, "%s failed to execute ras_late_init! ret:%d\n",
3721 					obj->ras_comm.name, r);
3722 				return r;
3723 			}
3724 		} else
3725 			amdgpu_ras_block_late_init_default(adev, &obj->ras_comm);
3726 	}
3727 
3728 	return 0;
3729 }
3730 
3731 /* do some fini work before IP fini as dependence */
3732 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
3733 {
3734 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3735 
3736 	if (!adev->ras_enabled || !con)
3737 		return 0;
3738 
3739 
3740 	/* Need disable ras on all IPs here before ip [hw/sw]fini */
3741 	if (AMDGPU_RAS_GET_FEATURES(con->features))
3742 		amdgpu_ras_disable_all_features(adev, 0);
3743 	amdgpu_ras_recovery_fini(adev);
3744 	return 0;
3745 }
3746 
3747 int amdgpu_ras_fini(struct amdgpu_device *adev)
3748 {
3749 	struct amdgpu_ras_block_list *ras_node, *tmp;
3750 	struct amdgpu_ras_block_object *obj = NULL;
3751 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3752 
3753 	if (!adev->ras_enabled || !con)
3754 		return 0;
3755 
3756 	list_for_each_entry_safe(ras_node, tmp, &adev->ras_list, node) {
3757 		if (ras_node->ras_obj) {
3758 			obj = ras_node->ras_obj;
3759 			if (amdgpu_ras_is_supported(adev, obj->ras_comm.block) &&
3760 			    obj->ras_fini)
3761 				obj->ras_fini(adev, &obj->ras_comm);
3762 			else
3763 				amdgpu_ras_block_late_fini_default(adev, &obj->ras_comm);
3764 		}
3765 
3766 		/* Clear ras blocks from ras_list and free ras block list node */
3767 		list_del(&ras_node->node);
3768 		kfree(ras_node);
3769 	}
3770 
3771 	amdgpu_ras_fs_fini(adev);
3772 	amdgpu_ras_interrupt_remove_all(adev);
3773 
3774 	if (amdgpu_ras_aca_is_supported(adev)) {
3775 		if (amdgpu_aca_is_enabled(adev))
3776 			amdgpu_aca_fini(adev);
3777 		else
3778 			amdgpu_mca_fini(adev);
3779 	}
3780 
3781 	WARN(AMDGPU_RAS_GET_FEATURES(con->features), "Feature mask is not cleared");
3782 
3783 	if (AMDGPU_RAS_GET_FEATURES(con->features))
3784 		amdgpu_ras_disable_all_features(adev, 0);
3785 
3786 	cancel_delayed_work_sync(&con->ras_counte_delay_work);
3787 
3788 	amdgpu_ras_set_context(adev, NULL);
3789 	kfree(con);
3790 
3791 	return 0;
3792 }
3793 
3794 bool amdgpu_ras_get_fed_status(struct amdgpu_device *adev)
3795 {
3796 	struct amdgpu_ras *ras;
3797 
3798 	ras = amdgpu_ras_get_context(adev);
3799 	if (!ras)
3800 		return false;
3801 
3802 	return atomic_read(&ras->fed);
3803 }
3804 
3805 void amdgpu_ras_set_fed(struct amdgpu_device *adev, bool status)
3806 {
3807 	struct amdgpu_ras *ras;
3808 
3809 	ras = amdgpu_ras_get_context(adev);
3810 	if (ras)
3811 		atomic_set(&ras->fed, !!status);
3812 }
3813 
3814 bool amdgpu_ras_event_id_is_valid(struct amdgpu_device *adev, u64 id)
3815 {
3816 	return !(id & BIT_ULL(63));
3817 }
3818 
3819 u64 amdgpu_ras_acquire_event_id(struct amdgpu_device *adev, enum ras_event_type type)
3820 {
3821 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
3822 	u64 id;
3823 
3824 	switch (type) {
3825 	case RAS_EVENT_TYPE_ISR:
3826 		id = (u64)atomic64_read(&ras->event_mgr->seqnos[type]);
3827 		break;
3828 	case RAS_EVENT_TYPE_INVALID:
3829 	default:
3830 		id = BIT_ULL(63) | 0ULL;
3831 		break;
3832 	}
3833 
3834 	return id;
3835 }
3836 
3837 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
3838 {
3839 	if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
3840 		struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
3841 		u64 event_id = (u64)atomic64_inc_return(&ras->event_mgr->seqnos[RAS_EVENT_TYPE_ISR]);
3842 
3843 		RAS_EVENT_LOG(adev, event_id, "uncorrectable hardware error"
3844 			      "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
3845 
3846 		amdgpu_ras_set_fed(adev, true);
3847 		ras->gpu_reset_flags |= AMDGPU_RAS_GPU_RESET_MODE1_RESET;
3848 		amdgpu_ras_reset_gpu(adev);
3849 	}
3850 }
3851 
3852 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
3853 {
3854 	if (adev->asic_type == CHIP_VEGA20 &&
3855 	    adev->pm.fw_version <= 0x283400) {
3856 		return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
3857 				amdgpu_ras_intr_triggered();
3858 	}
3859 
3860 	return false;
3861 }
3862 
3863 void amdgpu_release_ras_context(struct amdgpu_device *adev)
3864 {
3865 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3866 
3867 	if (!con)
3868 		return;
3869 
3870 	if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) {
3871 		con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX);
3872 		amdgpu_ras_set_context(adev, NULL);
3873 		kfree(con);
3874 	}
3875 }
3876 
3877 #ifdef CONFIG_X86_MCE_AMD
3878 static struct amdgpu_device *find_adev(uint32_t node_id)
3879 {
3880 	int i;
3881 	struct amdgpu_device *adev = NULL;
3882 
3883 	for (i = 0; i < mce_adev_list.num_gpu; i++) {
3884 		adev = mce_adev_list.devs[i];
3885 
3886 		if (adev && adev->gmc.xgmi.connected_to_cpu &&
3887 		    adev->gmc.xgmi.physical_node_id == node_id)
3888 			break;
3889 		adev = NULL;
3890 	}
3891 
3892 	return adev;
3893 }
3894 
3895 #define GET_MCA_IPID_GPUID(m)	(((m) >> 44) & 0xF)
3896 #define GET_UMC_INST(m)		(((m) >> 21) & 0x7)
3897 #define GET_CHAN_INDEX(m)	((((m) >> 12) & 0x3) | (((m) >> 18) & 0x4))
3898 #define GPU_ID_OFFSET		8
3899 
3900 static int amdgpu_bad_page_notifier(struct notifier_block *nb,
3901 				    unsigned long val, void *data)
3902 {
3903 	struct mce *m = (struct mce *)data;
3904 	struct amdgpu_device *adev = NULL;
3905 	uint32_t gpu_id = 0;
3906 	uint32_t umc_inst = 0, ch_inst = 0;
3907 
3908 	/*
3909 	 * If the error was generated in UMC_V2, which belongs to GPU UMCs,
3910 	 * and error occurred in DramECC (Extended error code = 0) then only
3911 	 * process the error, else bail out.
3912 	 */
3913 	if (!m || !((smca_get_bank_type(m->extcpu, m->bank) == SMCA_UMC_V2) &&
3914 		    (XEC(m->status, 0x3f) == 0x0)))
3915 		return NOTIFY_DONE;
3916 
3917 	/*
3918 	 * If it is correctable error, return.
3919 	 */
3920 	if (mce_is_correctable(m))
3921 		return NOTIFY_OK;
3922 
3923 	/*
3924 	 * GPU Id is offset by GPU_ID_OFFSET in MCA_IPID_UMC register.
3925 	 */
3926 	gpu_id = GET_MCA_IPID_GPUID(m->ipid) - GPU_ID_OFFSET;
3927 
3928 	adev = find_adev(gpu_id);
3929 	if (!adev) {
3930 		DRM_WARN("%s: Unable to find adev for gpu_id: %d\n", __func__,
3931 								gpu_id);
3932 		return NOTIFY_DONE;
3933 	}
3934 
3935 	/*
3936 	 * If it is uncorrectable error, then find out UMC instance and
3937 	 * channel index.
3938 	 */
3939 	umc_inst = GET_UMC_INST(m->ipid);
3940 	ch_inst = GET_CHAN_INDEX(m->ipid);
3941 
3942 	dev_info(adev->dev, "Uncorrectable error detected in UMC inst: %d, chan_idx: %d",
3943 			     umc_inst, ch_inst);
3944 
3945 	if (!amdgpu_umc_page_retirement_mca(adev, m->addr, ch_inst, umc_inst))
3946 		return NOTIFY_OK;
3947 	else
3948 		return NOTIFY_DONE;
3949 }
3950 
3951 static struct notifier_block amdgpu_bad_page_nb = {
3952 	.notifier_call  = amdgpu_bad_page_notifier,
3953 	.priority       = MCE_PRIO_UC,
3954 };
3955 
3956 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev)
3957 {
3958 	/*
3959 	 * Add the adev to the mce_adev_list.
3960 	 * During mode2 reset, amdgpu device is temporarily
3961 	 * removed from the mgpu_info list which can cause
3962 	 * page retirement to fail.
3963 	 * Use this list instead of mgpu_info to find the amdgpu
3964 	 * device on which the UMC error was reported.
3965 	 */
3966 	mce_adev_list.devs[mce_adev_list.num_gpu++] = adev;
3967 
3968 	/*
3969 	 * Register the x86 notifier only once
3970 	 * with MCE subsystem.
3971 	 */
3972 	if (notifier_registered == false) {
3973 		mce_register_decode_chain(&amdgpu_bad_page_nb);
3974 		notifier_registered = true;
3975 	}
3976 }
3977 #endif
3978 
3979 struct amdgpu_ras *amdgpu_ras_get_context(struct amdgpu_device *adev)
3980 {
3981 	if (!adev)
3982 		return NULL;
3983 
3984 	return adev->psp.ras_context.ras;
3985 }
3986 
3987 int amdgpu_ras_set_context(struct amdgpu_device *adev, struct amdgpu_ras *ras_con)
3988 {
3989 	if (!adev)
3990 		return -EINVAL;
3991 
3992 	adev->psp.ras_context.ras = ras_con;
3993 	return 0;
3994 }
3995 
3996 /* check if ras is supported on block, say, sdma, gfx */
3997 int amdgpu_ras_is_supported(struct amdgpu_device *adev,
3998 		unsigned int block)
3999 {
4000 	int ret = 0;
4001 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
4002 
4003 	if (block >= AMDGPU_RAS_BLOCK_COUNT)
4004 		return 0;
4005 
4006 	ret = ras && (adev->ras_enabled & (1 << block));
4007 
4008 	/* For the special asic with mem ecc enabled but sram ecc
4009 	 * not enabled, even if the ras block is not supported on
4010 	 * .ras_enabled, if the asic supports poison mode and the
4011 	 * ras block has ras configuration, it can be considered
4012 	 * that the ras block supports ras function.
4013 	 */
4014 	if (!ret &&
4015 	    (block == AMDGPU_RAS_BLOCK__GFX ||
4016 	     block == AMDGPU_RAS_BLOCK__SDMA ||
4017 	     block == AMDGPU_RAS_BLOCK__VCN ||
4018 	     block == AMDGPU_RAS_BLOCK__JPEG) &&
4019 		(amdgpu_ras_mask & (1 << block)) &&
4020 	    amdgpu_ras_is_poison_mode_supported(adev) &&
4021 	    amdgpu_ras_get_ras_block(adev, block, 0))
4022 		ret = 1;
4023 
4024 	return ret;
4025 }
4026 
4027 int amdgpu_ras_reset_gpu(struct amdgpu_device *adev)
4028 {
4029 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
4030 
4031 	/* mode1 is the only selection for RMA status */
4032 	if (ras->is_rma) {
4033 		ras->gpu_reset_flags = 0;
4034 		ras->gpu_reset_flags |= AMDGPU_RAS_GPU_RESET_MODE1_RESET;
4035 	}
4036 
4037 	if (atomic_cmpxchg(&ras->in_recovery, 0, 1) == 0)
4038 		amdgpu_reset_domain_schedule(ras->adev->reset_domain, &ras->recovery_work);
4039 	return 0;
4040 }
4041 
4042 int amdgpu_ras_set_mca_debug_mode(struct amdgpu_device *adev, bool enable)
4043 {
4044 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
4045 	int ret = 0;
4046 
4047 	if (con) {
4048 		ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
4049 		if (!ret)
4050 			con->is_aca_debug_mode = enable;
4051 	}
4052 
4053 	return ret;
4054 }
4055 
4056 int amdgpu_ras_set_aca_debug_mode(struct amdgpu_device *adev, bool enable)
4057 {
4058 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
4059 	int ret = 0;
4060 
4061 	if (con) {
4062 		if (amdgpu_aca_is_enabled(adev))
4063 			ret = amdgpu_aca_smu_set_debug_mode(adev, enable);
4064 		else
4065 			ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
4066 		if (!ret)
4067 			con->is_aca_debug_mode = enable;
4068 	}
4069 
4070 	return ret;
4071 }
4072 
4073 bool amdgpu_ras_get_aca_debug_mode(struct amdgpu_device *adev)
4074 {
4075 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
4076 	const struct aca_smu_funcs *smu_funcs = adev->aca.smu_funcs;
4077 	const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs;
4078 
4079 	if (!con)
4080 		return false;
4081 
4082 	if ((amdgpu_aca_is_enabled(adev) && smu_funcs && smu_funcs->set_debug_mode) ||
4083 	    (!amdgpu_aca_is_enabled(adev) && mca_funcs && mca_funcs->mca_set_debug_mode))
4084 		return con->is_aca_debug_mode;
4085 	else
4086 		return true;
4087 }
4088 
4089 bool amdgpu_ras_get_error_query_mode(struct amdgpu_device *adev,
4090 				     unsigned int *error_query_mode)
4091 {
4092 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
4093 	const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs;
4094 	const struct aca_smu_funcs *smu_funcs = adev->aca.smu_funcs;
4095 
4096 	if (!con) {
4097 		*error_query_mode = AMDGPU_RAS_INVALID_ERROR_QUERY;
4098 		return false;
4099 	}
4100 
4101 	if ((smu_funcs && smu_funcs->set_debug_mode) || (mca_funcs && mca_funcs->mca_set_debug_mode))
4102 		*error_query_mode =
4103 			(con->is_aca_debug_mode) ? AMDGPU_RAS_DIRECT_ERROR_QUERY : AMDGPU_RAS_FIRMWARE_ERROR_QUERY;
4104 	else
4105 		*error_query_mode = AMDGPU_RAS_DIRECT_ERROR_QUERY;
4106 
4107 	return true;
4108 }
4109 
4110 /* Register each ip ras block into amdgpu ras */
4111 int amdgpu_ras_register_ras_block(struct amdgpu_device *adev,
4112 		struct amdgpu_ras_block_object *ras_block_obj)
4113 {
4114 	struct amdgpu_ras_block_list *ras_node;
4115 	if (!adev || !ras_block_obj)
4116 		return -EINVAL;
4117 
4118 	ras_node = kzalloc(sizeof(*ras_node), GFP_KERNEL);
4119 	if (!ras_node)
4120 		return -ENOMEM;
4121 
4122 	INIT_LIST_HEAD(&ras_node->node);
4123 	ras_node->ras_obj = ras_block_obj;
4124 	list_add_tail(&ras_node->node, &adev->ras_list);
4125 
4126 	return 0;
4127 }
4128 
4129 void amdgpu_ras_get_error_type_name(uint32_t err_type, char *err_type_name)
4130 {
4131 	if (!err_type_name)
4132 		return;
4133 
4134 	switch (err_type) {
4135 	case AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE:
4136 		sprintf(err_type_name, "correctable");
4137 		break;
4138 	case AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE:
4139 		sprintf(err_type_name, "uncorrectable");
4140 		break;
4141 	default:
4142 		sprintf(err_type_name, "unknown");
4143 		break;
4144 	}
4145 }
4146 
4147 bool amdgpu_ras_inst_get_memory_id_field(struct amdgpu_device *adev,
4148 					 const struct amdgpu_ras_err_status_reg_entry *reg_entry,
4149 					 uint32_t instance,
4150 					 uint32_t *memory_id)
4151 {
4152 	uint32_t err_status_lo_data, err_status_lo_offset;
4153 
4154 	if (!reg_entry)
4155 		return false;
4156 
4157 	err_status_lo_offset =
4158 		AMDGPU_RAS_REG_ENTRY_OFFSET(reg_entry->hwip, instance,
4159 					    reg_entry->seg_lo, reg_entry->reg_lo);
4160 	err_status_lo_data = RREG32(err_status_lo_offset);
4161 
4162 	if ((reg_entry->flags & AMDGPU_RAS_ERR_STATUS_VALID) &&
4163 	    !REG_GET_FIELD(err_status_lo_data, ERR_STATUS_LO, ERR_STATUS_VALID_FLAG))
4164 		return false;
4165 
4166 	*memory_id = REG_GET_FIELD(err_status_lo_data, ERR_STATUS_LO, MEMORY_ID);
4167 
4168 	return true;
4169 }
4170 
4171 bool amdgpu_ras_inst_get_err_cnt_field(struct amdgpu_device *adev,
4172 				       const struct amdgpu_ras_err_status_reg_entry *reg_entry,
4173 				       uint32_t instance,
4174 				       unsigned long *err_cnt)
4175 {
4176 	uint32_t err_status_hi_data, err_status_hi_offset;
4177 
4178 	if (!reg_entry)
4179 		return false;
4180 
4181 	err_status_hi_offset =
4182 		AMDGPU_RAS_REG_ENTRY_OFFSET(reg_entry->hwip, instance,
4183 					    reg_entry->seg_hi, reg_entry->reg_hi);
4184 	err_status_hi_data = RREG32(err_status_hi_offset);
4185 
4186 	if ((reg_entry->flags & AMDGPU_RAS_ERR_INFO_VALID) &&
4187 	    !REG_GET_FIELD(err_status_hi_data, ERR_STATUS_HI, ERR_INFO_VALID_FLAG))
4188 		/* keep the check here in case we need to refer to the result later */
4189 		dev_dbg(adev->dev, "Invalid err_info field\n");
4190 
4191 	/* read err count */
4192 	*err_cnt = REG_GET_FIELD(err_status_hi_data, ERR_STATUS, ERR_CNT);
4193 
4194 	return true;
4195 }
4196 
4197 void amdgpu_ras_inst_query_ras_error_count(struct amdgpu_device *adev,
4198 					   const struct amdgpu_ras_err_status_reg_entry *reg_list,
4199 					   uint32_t reg_list_size,
4200 					   const struct amdgpu_ras_memory_id_entry *mem_list,
4201 					   uint32_t mem_list_size,
4202 					   uint32_t instance,
4203 					   uint32_t err_type,
4204 					   unsigned long *err_count)
4205 {
4206 	uint32_t memory_id;
4207 	unsigned long err_cnt;
4208 	char err_type_name[16];
4209 	uint32_t i, j;
4210 
4211 	for (i = 0; i < reg_list_size; i++) {
4212 		/* query memory_id from err_status_lo */
4213 		if (!amdgpu_ras_inst_get_memory_id_field(adev, &reg_list[i],
4214 							 instance, &memory_id))
4215 			continue;
4216 
4217 		/* query err_cnt from err_status_hi */
4218 		if (!amdgpu_ras_inst_get_err_cnt_field(adev, &reg_list[i],
4219 						       instance, &err_cnt) ||
4220 		    !err_cnt)
4221 			continue;
4222 
4223 		*err_count += err_cnt;
4224 
4225 		/* log the errors */
4226 		amdgpu_ras_get_error_type_name(err_type, err_type_name);
4227 		if (!mem_list) {
4228 			/* memory_list is not supported */
4229 			dev_info(adev->dev,
4230 				 "%ld %s hardware errors detected in %s, instance: %d, memory_id: %d\n",
4231 				 err_cnt, err_type_name,
4232 				 reg_list[i].block_name,
4233 				 instance, memory_id);
4234 		} else {
4235 			for (j = 0; j < mem_list_size; j++) {
4236 				if (memory_id == mem_list[j].memory_id) {
4237 					dev_info(adev->dev,
4238 						 "%ld %s hardware errors detected in %s, instance: %d, memory block: %s\n",
4239 						 err_cnt, err_type_name,
4240 						 reg_list[i].block_name,
4241 						 instance, mem_list[j].name);
4242 					break;
4243 				}
4244 			}
4245 		}
4246 	}
4247 }
4248 
4249 void amdgpu_ras_inst_reset_ras_error_count(struct amdgpu_device *adev,
4250 					   const struct amdgpu_ras_err_status_reg_entry *reg_list,
4251 					   uint32_t reg_list_size,
4252 					   uint32_t instance)
4253 {
4254 	uint32_t err_status_lo_offset, err_status_hi_offset;
4255 	uint32_t i;
4256 
4257 	for (i = 0; i < reg_list_size; i++) {
4258 		err_status_lo_offset =
4259 			AMDGPU_RAS_REG_ENTRY_OFFSET(reg_list[i].hwip, instance,
4260 						    reg_list[i].seg_lo, reg_list[i].reg_lo);
4261 		err_status_hi_offset =
4262 			AMDGPU_RAS_REG_ENTRY_OFFSET(reg_list[i].hwip, instance,
4263 						    reg_list[i].seg_hi, reg_list[i].reg_hi);
4264 		WREG32(err_status_lo_offset, 0);
4265 		WREG32(err_status_hi_offset, 0);
4266 	}
4267 }
4268 
4269 int amdgpu_ras_error_data_init(struct ras_err_data *err_data)
4270 {
4271 	memset(err_data, 0, sizeof(*err_data));
4272 
4273 	INIT_LIST_HEAD(&err_data->err_node_list);
4274 
4275 	return 0;
4276 }
4277 
4278 static void amdgpu_ras_error_node_release(struct ras_err_node *err_node)
4279 {
4280 	if (!err_node)
4281 		return;
4282 
4283 	list_del(&err_node->node);
4284 	kvfree(err_node);
4285 }
4286 
4287 void amdgpu_ras_error_data_fini(struct ras_err_data *err_data)
4288 {
4289 	struct ras_err_node *err_node, *tmp;
4290 
4291 	list_for_each_entry_safe(err_node, tmp, &err_data->err_node_list, node)
4292 		amdgpu_ras_error_node_release(err_node);
4293 }
4294 
4295 static struct ras_err_node *amdgpu_ras_error_find_node_by_id(struct ras_err_data *err_data,
4296 							     struct amdgpu_smuio_mcm_config_info *mcm_info)
4297 {
4298 	struct ras_err_node *err_node;
4299 	struct amdgpu_smuio_mcm_config_info *ref_id;
4300 
4301 	if (!err_data || !mcm_info)
4302 		return NULL;
4303 
4304 	for_each_ras_error(err_node, err_data) {
4305 		ref_id = &err_node->err_info.mcm_info;
4306 
4307 		if (mcm_info->socket_id == ref_id->socket_id &&
4308 		    mcm_info->die_id == ref_id->die_id)
4309 			return err_node;
4310 	}
4311 
4312 	return NULL;
4313 }
4314 
4315 static struct ras_err_node *amdgpu_ras_error_node_new(void)
4316 {
4317 	struct ras_err_node *err_node;
4318 
4319 	err_node = kvzalloc(sizeof(*err_node), GFP_KERNEL);
4320 	if (!err_node)
4321 		return NULL;
4322 
4323 	INIT_LIST_HEAD(&err_node->node);
4324 
4325 	return err_node;
4326 }
4327 
4328 static int ras_err_info_cmp(void *priv, const struct list_head *a, const struct list_head *b)
4329 {
4330 	struct ras_err_node *nodea = container_of(a, struct ras_err_node, node);
4331 	struct ras_err_node *nodeb = container_of(b, struct ras_err_node, node);
4332 	struct amdgpu_smuio_mcm_config_info *infoa = &nodea->err_info.mcm_info;
4333 	struct amdgpu_smuio_mcm_config_info *infob = &nodeb->err_info.mcm_info;
4334 
4335 	if (unlikely(infoa->socket_id != infob->socket_id))
4336 		return infoa->socket_id - infob->socket_id;
4337 	else
4338 		return infoa->die_id - infob->die_id;
4339 
4340 	return 0;
4341 }
4342 
4343 static struct ras_err_info *amdgpu_ras_error_get_info(struct ras_err_data *err_data,
4344 				struct amdgpu_smuio_mcm_config_info *mcm_info)
4345 {
4346 	struct ras_err_node *err_node;
4347 
4348 	err_node = amdgpu_ras_error_find_node_by_id(err_data, mcm_info);
4349 	if (err_node)
4350 		return &err_node->err_info;
4351 
4352 	err_node = amdgpu_ras_error_node_new();
4353 	if (!err_node)
4354 		return NULL;
4355 
4356 	INIT_LIST_HEAD(&err_node->err_info.err_addr_list);
4357 
4358 	memcpy(&err_node->err_info.mcm_info, mcm_info, sizeof(*mcm_info));
4359 
4360 	err_data->err_list_count++;
4361 	list_add_tail(&err_node->node, &err_data->err_node_list);
4362 	list_sort(NULL, &err_data->err_node_list, ras_err_info_cmp);
4363 
4364 	return &err_node->err_info;
4365 }
4366 
4367 void amdgpu_ras_add_mca_err_addr(struct ras_err_info *err_info, struct ras_err_addr *err_addr)
4368 {
4369 	/* This function will be retired. */
4370 	return;
4371 }
4372 
4373 void amdgpu_ras_del_mca_err_addr(struct ras_err_info *err_info, struct ras_err_addr *mca_err_addr)
4374 {
4375 	list_del(&mca_err_addr->node);
4376 	kfree(mca_err_addr);
4377 }
4378 
4379 int amdgpu_ras_error_statistic_ue_count(struct ras_err_data *err_data,
4380 		struct amdgpu_smuio_mcm_config_info *mcm_info,
4381 		struct ras_err_addr *err_addr, u64 count)
4382 {
4383 	struct ras_err_info *err_info;
4384 
4385 	if (!err_data || !mcm_info)
4386 		return -EINVAL;
4387 
4388 	if (!count)
4389 		return 0;
4390 
4391 	err_info = amdgpu_ras_error_get_info(err_data, mcm_info);
4392 	if (!err_info)
4393 		return -EINVAL;
4394 
4395 	if (err_addr && err_addr->err_status)
4396 		amdgpu_ras_add_mca_err_addr(err_info, err_addr);
4397 
4398 	err_info->ue_count += count;
4399 	err_data->ue_count += count;
4400 
4401 	return 0;
4402 }
4403 
4404 int amdgpu_ras_error_statistic_ce_count(struct ras_err_data *err_data,
4405 		struct amdgpu_smuio_mcm_config_info *mcm_info,
4406 		struct ras_err_addr *err_addr, u64 count)
4407 {
4408 	struct ras_err_info *err_info;
4409 
4410 	if (!err_data || !mcm_info)
4411 		return -EINVAL;
4412 
4413 	if (!count)
4414 		return 0;
4415 
4416 	err_info = amdgpu_ras_error_get_info(err_data, mcm_info);
4417 	if (!err_info)
4418 		return -EINVAL;
4419 
4420 	err_info->ce_count += count;
4421 	err_data->ce_count += count;
4422 
4423 	return 0;
4424 }
4425 
4426 int amdgpu_ras_error_statistic_de_count(struct ras_err_data *err_data,
4427 		struct amdgpu_smuio_mcm_config_info *mcm_info,
4428 		struct ras_err_addr *err_addr, u64 count)
4429 {
4430 	struct ras_err_info *err_info;
4431 
4432 	if (!err_data || !mcm_info)
4433 		return -EINVAL;
4434 
4435 	if (!count)
4436 		return 0;
4437 
4438 	err_info = amdgpu_ras_error_get_info(err_data, mcm_info);
4439 	if (!err_info)
4440 		return -EINVAL;
4441 
4442 	if (err_addr && err_addr->err_status)
4443 		amdgpu_ras_add_mca_err_addr(err_info, err_addr);
4444 
4445 	err_info->de_count += count;
4446 	err_data->de_count += count;
4447 
4448 	return 0;
4449 }
4450 
4451 #define mmMP0_SMN_C2PMSG_92	0x1609C
4452 #define mmMP0_SMN_C2PMSG_126	0x160BE
4453 static void amdgpu_ras_boot_time_error_reporting(struct amdgpu_device *adev,
4454 						 u32 instance)
4455 {
4456 	u32 socket_id, aid_id, hbm_id;
4457 	u32 fw_status;
4458 	u32 boot_error;
4459 	u64 reg_addr;
4460 
4461 	/* The pattern for smn addressing in other SOC could be different from
4462 	 * the one for aqua_vanjaram. We should revisit the code if the pattern
4463 	 * is changed. In such case, replace the aqua_vanjaram implementation
4464 	 * with more common helper */
4465 	reg_addr = (mmMP0_SMN_C2PMSG_92 << 2) +
4466 		   aqua_vanjaram_encode_ext_smn_addressing(instance);
4467 	fw_status = amdgpu_device_indirect_rreg_ext(adev, reg_addr);
4468 
4469 	reg_addr = (mmMP0_SMN_C2PMSG_126 << 2) +
4470 		   aqua_vanjaram_encode_ext_smn_addressing(instance);
4471 	boot_error = amdgpu_device_indirect_rreg_ext(adev, reg_addr);
4472 
4473 	socket_id = AMDGPU_RAS_GPU_ERR_SOCKET_ID(boot_error);
4474 	aid_id = AMDGPU_RAS_GPU_ERR_AID_ID(boot_error);
4475 	hbm_id = AMDGPU_RAS_GPU_ERR_HBM_ID(boot_error);
4476 
4477 	if (AMDGPU_RAS_GPU_ERR_MEM_TRAINING(boot_error))
4478 		dev_info(adev->dev,
4479 			 "socket: %d, aid: %d, hbm: %d, fw_status: 0x%x, memory training failed\n",
4480 			 socket_id, aid_id, hbm_id, fw_status);
4481 
4482 	if (AMDGPU_RAS_GPU_ERR_FW_LOAD(boot_error))
4483 		dev_info(adev->dev,
4484 			 "socket: %d, aid: %d, fw_status: 0x%x, firmware load failed at boot time\n",
4485 			 socket_id, aid_id, fw_status);
4486 
4487 	if (AMDGPU_RAS_GPU_ERR_WAFL_LINK_TRAINING(boot_error))
4488 		dev_info(adev->dev,
4489 			 "socket: %d, aid: %d, fw_status: 0x%x, wafl link training failed\n",
4490 			 socket_id, aid_id, fw_status);
4491 
4492 	if (AMDGPU_RAS_GPU_ERR_XGMI_LINK_TRAINING(boot_error))
4493 		dev_info(adev->dev,
4494 			 "socket: %d, aid: %d, fw_status: 0x%x, xgmi link training failed\n",
4495 			 socket_id, aid_id, fw_status);
4496 
4497 	if (AMDGPU_RAS_GPU_ERR_USR_CP_LINK_TRAINING(boot_error))
4498 		dev_info(adev->dev,
4499 			 "socket: %d, aid: %d, fw_status: 0x%x, usr cp link training failed\n",
4500 			 socket_id, aid_id, fw_status);
4501 
4502 	if (AMDGPU_RAS_GPU_ERR_USR_DP_LINK_TRAINING(boot_error))
4503 		dev_info(adev->dev,
4504 			 "socket: %d, aid: %d, fw_status: 0x%x, usr dp link training failed\n",
4505 			 socket_id, aid_id, fw_status);
4506 
4507 	if (AMDGPU_RAS_GPU_ERR_HBM_MEM_TEST(boot_error))
4508 		dev_info(adev->dev,
4509 			 "socket: %d, aid: %d, hbm: %d, fw_status: 0x%x, hbm memory test failed\n",
4510 			 socket_id, aid_id, hbm_id, fw_status);
4511 
4512 	if (AMDGPU_RAS_GPU_ERR_HBM_BIST_TEST(boot_error))
4513 		dev_info(adev->dev,
4514 			 "socket: %d, aid: %d, hbm: %d, fw_status: 0x%x, hbm bist test failed\n",
4515 			 socket_id, aid_id, hbm_id, fw_status);
4516 }
4517 
4518 static bool amdgpu_ras_boot_error_detected(struct amdgpu_device *adev,
4519 					   u32 instance)
4520 {
4521 	u64 reg_addr;
4522 	u32 reg_data;
4523 	int retry_loop;
4524 
4525 	reg_addr = (mmMP0_SMN_C2PMSG_92 << 2) +
4526 		   aqua_vanjaram_encode_ext_smn_addressing(instance);
4527 
4528 	for (retry_loop = 0; retry_loop < AMDGPU_RAS_BOOT_STATUS_POLLING_LIMIT; retry_loop++) {
4529 		reg_data = amdgpu_device_indirect_rreg_ext(adev, reg_addr);
4530 		if ((reg_data & AMDGPU_RAS_BOOT_STATUS_MASK) == AMDGPU_RAS_BOOT_STEADY_STATUS)
4531 			return false;
4532 		else
4533 			msleep(1);
4534 	}
4535 
4536 	return true;
4537 }
4538 
4539 void amdgpu_ras_query_boot_status(struct amdgpu_device *adev, u32 num_instances)
4540 {
4541 	u32 i;
4542 
4543 	for (i = 0; i < num_instances; i++) {
4544 		if (amdgpu_ras_boot_error_detected(adev, i))
4545 			amdgpu_ras_boot_time_error_reporting(adev, i);
4546 	}
4547 }
4548 
4549 int amdgpu_ras_reserve_page(struct amdgpu_device *adev, uint64_t pfn)
4550 {
4551 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
4552 	struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
4553 	uint64_t start = pfn << AMDGPU_GPU_PAGE_SHIFT;
4554 	int ret = 0;
4555 
4556 	mutex_lock(&con->page_rsv_lock);
4557 	ret = amdgpu_vram_mgr_query_page_status(mgr, start);
4558 	if (ret == -ENOENT)
4559 		ret = amdgpu_vram_mgr_reserve_range(mgr, start, AMDGPU_GPU_PAGE_SIZE);
4560 	mutex_unlock(&con->page_rsv_lock);
4561 
4562 	return ret;
4563 }
4564 
4565 void amdgpu_ras_event_log_print(struct amdgpu_device *adev, u64 event_id,
4566 				const char *fmt, ...)
4567 {
4568 	struct va_format vaf;
4569 	va_list args;
4570 
4571 	va_start(args, fmt);
4572 	vaf.fmt = fmt;
4573 	vaf.va = &args;
4574 
4575 	if (amdgpu_ras_event_id_is_valid(adev, event_id))
4576 		dev_printk(KERN_INFO, adev->dev, "{%llu}%pV", event_id, &vaf);
4577 	else
4578 		dev_printk(KERN_INFO, adev->dev, "%pV", &vaf);
4579 
4580 	va_end(args);
4581 }
4582