xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_ras.c (revision 68dd5bb1859285b71cb62a10bf107b8ad54064d9)
1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  *
23  */
24 #include <linux/debugfs.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/uaccess.h>
28 #include <linux/reboot.h>
29 #include <linux/syscalls.h>
30 #include <linux/pm_runtime.h>
31 
32 #include "amdgpu.h"
33 #include "amdgpu_ras.h"
34 #include "amdgpu_atomfirmware.h"
35 #include "amdgpu_xgmi.h"
36 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h"
37 #include "nbio_v4_3.h"
38 #include "nbio_v7_9.h"
39 #include "atom.h"
40 #include "amdgpu_reset.h"
41 
42 #ifdef CONFIG_X86_MCE_AMD
43 #include <asm/mce.h>
44 
45 static bool notifier_registered;
46 #endif
47 static const char *RAS_FS_NAME = "ras";
48 
49 const char *ras_error_string[] = {
50 	"none",
51 	"parity",
52 	"single_correctable",
53 	"multi_uncorrectable",
54 	"poison",
55 };
56 
57 const char *ras_block_string[] = {
58 	"umc",
59 	"sdma",
60 	"gfx",
61 	"mmhub",
62 	"athub",
63 	"pcie_bif",
64 	"hdp",
65 	"xgmi_wafl",
66 	"df",
67 	"smn",
68 	"sem",
69 	"mp0",
70 	"mp1",
71 	"fuse",
72 	"mca",
73 	"vcn",
74 	"jpeg",
75 };
76 
77 const char *ras_mca_block_string[] = {
78 	"mca_mp0",
79 	"mca_mp1",
80 	"mca_mpio",
81 	"mca_iohc",
82 };
83 
84 struct amdgpu_ras_block_list {
85 	/* ras block link */
86 	struct list_head node;
87 
88 	struct amdgpu_ras_block_object *ras_obj;
89 };
90 
91 const char *get_ras_block_str(struct ras_common_if *ras_block)
92 {
93 	if (!ras_block)
94 		return "NULL";
95 
96 	if (ras_block->block >= AMDGPU_RAS_BLOCK_COUNT)
97 		return "OUT OF RANGE";
98 
99 	if (ras_block->block == AMDGPU_RAS_BLOCK__MCA)
100 		return ras_mca_block_string[ras_block->sub_block_index];
101 
102 	return ras_block_string[ras_block->block];
103 }
104 
105 #define ras_block_str(_BLOCK_) \
106 	(((_BLOCK_) < ARRAY_SIZE(ras_block_string)) ? ras_block_string[_BLOCK_] : "Out Of Range")
107 
108 #define ras_err_str(i) (ras_error_string[ffs(i)])
109 
110 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
111 
112 /* inject address is 52 bits */
113 #define	RAS_UMC_INJECT_ADDR_LIMIT	(0x1ULL << 52)
114 
115 /* typical ECC bad page rate is 1 bad page per 100MB VRAM */
116 #define RAS_BAD_PAGE_COVER              (100 * 1024 * 1024ULL)
117 
118 enum amdgpu_ras_retire_page_reservation {
119 	AMDGPU_RAS_RETIRE_PAGE_RESERVED,
120 	AMDGPU_RAS_RETIRE_PAGE_PENDING,
121 	AMDGPU_RAS_RETIRE_PAGE_FAULT,
122 };
123 
124 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
125 
126 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
127 				uint64_t addr);
128 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
129 				uint64_t addr);
130 #ifdef CONFIG_X86_MCE_AMD
131 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev);
132 struct mce_notifier_adev_list {
133 	struct amdgpu_device *devs[MAX_GPU_INSTANCE];
134 	int num_gpu;
135 };
136 static struct mce_notifier_adev_list mce_adev_list;
137 #endif
138 
139 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
140 {
141 	if (adev && amdgpu_ras_get_context(adev))
142 		amdgpu_ras_get_context(adev)->error_query_ready = ready;
143 }
144 
145 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
146 {
147 	if (adev && amdgpu_ras_get_context(adev))
148 		return amdgpu_ras_get_context(adev)->error_query_ready;
149 
150 	return false;
151 }
152 
153 static int amdgpu_reserve_page_direct(struct amdgpu_device *adev, uint64_t address)
154 {
155 	struct ras_err_data err_data = {0, 0, 0, NULL};
156 	struct eeprom_table_record err_rec;
157 
158 	if ((address >= adev->gmc.mc_vram_size) ||
159 	    (address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
160 		dev_warn(adev->dev,
161 		         "RAS WARN: input address 0x%llx is invalid.\n",
162 		         address);
163 		return -EINVAL;
164 	}
165 
166 	if (amdgpu_ras_check_bad_page(adev, address)) {
167 		dev_warn(adev->dev,
168 			 "RAS WARN: 0x%llx has already been marked as bad page!\n",
169 			 address);
170 		return 0;
171 	}
172 
173 	memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
174 	err_data.err_addr = &err_rec;
175 	amdgpu_umc_fill_error_record(&err_data, address, address, 0, 0);
176 
177 	if (amdgpu_bad_page_threshold != 0) {
178 		amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
179 					 err_data.err_addr_cnt);
180 		amdgpu_ras_save_bad_pages(adev, NULL);
181 	}
182 
183 	dev_warn(adev->dev, "WARNING: THIS IS ONLY FOR TEST PURPOSES AND WILL CORRUPT RAS EEPROM\n");
184 	dev_warn(adev->dev, "Clear EEPROM:\n");
185 	dev_warn(adev->dev, "    echo 1 > /sys/kernel/debug/dri/0/ras/ras_eeprom_reset\n");
186 
187 	return 0;
188 }
189 
190 #ifdef __linux__
191 
192 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
193 					size_t size, loff_t *pos)
194 {
195 	struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
196 	struct ras_query_if info = {
197 		.head = obj->head,
198 	};
199 	ssize_t s;
200 	char val[128];
201 
202 	if (amdgpu_ras_query_error_status(obj->adev, &info))
203 		return -EINVAL;
204 
205 	/* Hardware counter will be reset automatically after the query on Vega20 and Arcturus */
206 	if (obj->adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 2) &&
207 	    obj->adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 4)) {
208 		if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
209 			dev_warn(obj->adev->dev, "Failed to reset error counter and error status");
210 	}
211 
212 	s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
213 			"ue", info.ue_count,
214 			"ce", info.ce_count);
215 	if (*pos >= s)
216 		return 0;
217 
218 	s -= *pos;
219 	s = min_t(u64, s, size);
220 
221 
222 	if (copy_to_user(buf, &val[*pos], s))
223 		return -EINVAL;
224 
225 	*pos += s;
226 
227 	return s;
228 }
229 
230 static const struct file_operations amdgpu_ras_debugfs_ops = {
231 	.owner = THIS_MODULE,
232 	.read = amdgpu_ras_debugfs_read,
233 	.write = NULL,
234 	.llseek = default_llseek
235 };
236 
237 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
238 {
239 	int i;
240 
241 	for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
242 		*block_id = i;
243 		if (strcmp(name, ras_block_string[i]) == 0)
244 			return 0;
245 	}
246 	return -EINVAL;
247 }
248 
249 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
250 		const char __user *buf, size_t size,
251 		loff_t *pos, struct ras_debug_if *data)
252 {
253 	ssize_t s = min_t(u64, 64, size);
254 	char str[65];
255 	char block_name[33];
256 	char err[9] = "ue";
257 	int op = -1;
258 	int block_id;
259 	uint32_t sub_block;
260 	u64 address, value;
261 	/* default value is 0 if the mask is not set by user */
262 	u32 instance_mask = 0;
263 
264 	if (*pos)
265 		return -EINVAL;
266 	*pos = size;
267 
268 	memset(str, 0, sizeof(str));
269 	memset(data, 0, sizeof(*data));
270 
271 	if (copy_from_user(str, buf, s))
272 		return -EINVAL;
273 
274 	if (sscanf(str, "disable %32s", block_name) == 1)
275 		op = 0;
276 	else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
277 		op = 1;
278 	else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
279 		op = 2;
280 	else if (strstr(str, "retire_page") != NULL)
281 		op = 3;
282 	else if (str[0] && str[1] && str[2] && str[3])
283 		/* ascii string, but commands are not matched. */
284 		return -EINVAL;
285 
286 	if (op != -1) {
287 		if (op == 3) {
288 			if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
289 			    sscanf(str, "%*s %llu", &address) != 1)
290 				return -EINVAL;
291 
292 			data->op = op;
293 			data->inject.address = address;
294 
295 			return 0;
296 		}
297 
298 		if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
299 			return -EINVAL;
300 
301 		data->head.block = block_id;
302 		/* only ue and ce errors are supported */
303 		if (!memcmp("ue", err, 2))
304 			data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
305 		else if (!memcmp("ce", err, 2))
306 			data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
307 		else
308 			return -EINVAL;
309 
310 		data->op = op;
311 
312 		if (op == 2) {
313 			if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx 0x%x",
314 				   &sub_block, &address, &value, &instance_mask) != 4 &&
315 			    sscanf(str, "%*s %*s %*s %u %llu %llu %u",
316 				   &sub_block, &address, &value, &instance_mask) != 4 &&
317 				sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
318 				   &sub_block, &address, &value) != 3 &&
319 			    sscanf(str, "%*s %*s %*s %u %llu %llu",
320 				   &sub_block, &address, &value) != 3)
321 				return -EINVAL;
322 			data->head.sub_block_index = sub_block;
323 			data->inject.address = address;
324 			data->inject.value = value;
325 			data->inject.instance_mask = instance_mask;
326 		}
327 	} else {
328 		if (size < sizeof(*data))
329 			return -EINVAL;
330 
331 		if (copy_from_user(data, buf, sizeof(*data)))
332 			return -EINVAL;
333 	}
334 
335 	return 0;
336 }
337 
338 static void amdgpu_ras_instance_mask_check(struct amdgpu_device *adev,
339 				struct ras_debug_if *data)
340 {
341 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
342 	uint32_t mask, inst_mask = data->inject.instance_mask;
343 
344 	/* no need to set instance mask if there is only one instance */
345 	if (num_xcc <= 1 && inst_mask) {
346 		data->inject.instance_mask = 0;
347 		dev_dbg(adev->dev,
348 			"RAS inject mask(0x%x) isn't supported and force it to 0.\n",
349 			inst_mask);
350 
351 		return;
352 	}
353 
354 	switch (data->head.block) {
355 	case AMDGPU_RAS_BLOCK__GFX:
356 		mask = GENMASK(num_xcc - 1, 0);
357 		break;
358 	case AMDGPU_RAS_BLOCK__SDMA:
359 		mask = GENMASK(adev->sdma.num_instances - 1, 0);
360 		break;
361 	case AMDGPU_RAS_BLOCK__VCN:
362 	case AMDGPU_RAS_BLOCK__JPEG:
363 		mask = GENMASK(adev->vcn.num_vcn_inst - 1, 0);
364 		break;
365 	default:
366 		mask = inst_mask;
367 		break;
368 	}
369 
370 	/* remove invalid bits in instance mask */
371 	data->inject.instance_mask &= mask;
372 	if (inst_mask != data->inject.instance_mask)
373 		dev_dbg(adev->dev,
374 			"Adjust RAS inject mask 0x%x to 0x%x\n",
375 			inst_mask, data->inject.instance_mask);
376 }
377 
378 /**
379  * DOC: AMDGPU RAS debugfs control interface
380  *
381  * The control interface accepts struct ras_debug_if which has two members.
382  *
383  * First member: ras_debug_if::head or ras_debug_if::inject.
384  *
385  * head is used to indicate which IP block will be under control.
386  *
387  * head has four members, they are block, type, sub_block_index, name.
388  * block: which IP will be under control.
389  * type: what kind of error will be enabled/disabled/injected.
390  * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
391  * name: the name of IP.
392  *
393  * inject has three more members than head, they are address, value and mask.
394  * As their names indicate, inject operation will write the
395  * value to the address.
396  *
397  * The second member: struct ras_debug_if::op.
398  * It has three kinds of operations.
399  *
400  * - 0: disable RAS on the block. Take ::head as its data.
401  * - 1: enable RAS on the block. Take ::head as its data.
402  * - 2: inject errors on the block. Take ::inject as its data.
403  *
404  * How to use the interface?
405  *
406  * In a program
407  *
408  * Copy the struct ras_debug_if in your code and initialize it.
409  * Write the struct to the control interface.
410  *
411  * From shell
412  *
413  * .. code-block:: bash
414  *
415  *	echo "disable <block>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
416  *	echo "enable  <block> <error>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
417  *	echo "inject  <block> <error> <sub-block> <address> <value> <mask>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
418  *
419  * Where N, is the card which you want to affect.
420  *
421  * "disable" requires only the block.
422  * "enable" requires the block and error type.
423  * "inject" requires the block, error type, address, and value.
424  *
425  * The block is one of: umc, sdma, gfx, etc.
426  *	see ras_block_string[] for details
427  *
428  * The error type is one of: ue, ce, where,
429  *	ue is multi-uncorrectable
430  *	ce is single-correctable
431  *
432  * The sub-block is a the sub-block index, pass 0 if there is no sub-block.
433  * The address and value are hexadecimal numbers, leading 0x is optional.
434  * The mask means instance mask, is optional, default value is 0x1.
435  *
436  * For instance,
437  *
438  * .. code-block:: bash
439  *
440  *	echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
441  *	echo inject umc ce 0 0 0 3 > /sys/kernel/debug/dri/0/ras/ras_ctrl
442  *	echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
443  *
444  * How to check the result of the operation?
445  *
446  * To check disable/enable, see "ras" features at,
447  * /sys/class/drm/card[0/1/2...]/device/ras/features
448  *
449  * To check inject, see the corresponding error count at,
450  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx|sdma|umc|...]_err_count
451  *
452  * .. note::
453  *	Operations are only allowed on blocks which are supported.
454  *	Check the "ras" mask at /sys/module/amdgpu/parameters/ras_mask
455  *	to see which blocks support RAS on a particular asic.
456  *
457  */
458 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f,
459 					     const char __user *buf,
460 					     size_t size, loff_t *pos)
461 {
462 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
463 	struct ras_debug_if data;
464 	int ret = 0;
465 
466 	if (!amdgpu_ras_get_error_query_ready(adev)) {
467 		dev_warn(adev->dev, "RAS WARN: error injection "
468 				"currently inaccessible\n");
469 		return size;
470 	}
471 
472 	ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
473 	if (ret)
474 		return ret;
475 
476 	if (data.op == 3) {
477 		ret = amdgpu_reserve_page_direct(adev, data.inject.address);
478 		if (!ret)
479 			return size;
480 		else
481 			return ret;
482 	}
483 
484 	if (!amdgpu_ras_is_supported(adev, data.head.block))
485 		return -EINVAL;
486 
487 	switch (data.op) {
488 	case 0:
489 		ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
490 		break;
491 	case 1:
492 		ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
493 		break;
494 	case 2:
495 		if ((data.inject.address >= adev->gmc.mc_vram_size &&
496 		    adev->gmc.mc_vram_size) ||
497 		    (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
498 			dev_warn(adev->dev, "RAS WARN: input address "
499 					"0x%llx is invalid.",
500 					data.inject.address);
501 			ret = -EINVAL;
502 			break;
503 		}
504 
505 		/* umc ce/ue error injection for a bad page is not allowed */
506 		if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
507 		    amdgpu_ras_check_bad_page(adev, data.inject.address)) {
508 			dev_warn(adev->dev, "RAS WARN: inject: 0x%llx has "
509 				 "already been marked as bad!\n",
510 				 data.inject.address);
511 			break;
512 		}
513 
514 		amdgpu_ras_instance_mask_check(adev, &data);
515 
516 		/* data.inject.address is offset instead of absolute gpu address */
517 		ret = amdgpu_ras_error_inject(adev, &data.inject);
518 		break;
519 	default:
520 		ret = -EINVAL;
521 		break;
522 	}
523 
524 	if (ret)
525 		return ret;
526 
527 	return size;
528 }
529 
530 /**
531  * DOC: AMDGPU RAS debugfs EEPROM table reset interface
532  *
533  * Some boards contain an EEPROM which is used to persistently store a list of
534  * bad pages which experiences ECC errors in vram.  This interface provides
535  * a way to reset the EEPROM, e.g., after testing error injection.
536  *
537  * Usage:
538  *
539  * .. code-block:: bash
540  *
541  *	echo 1 > ../ras/ras_eeprom_reset
542  *
543  * will reset EEPROM table to 0 entries.
544  *
545  */
546 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f,
547 					       const char __user *buf,
548 					       size_t size, loff_t *pos)
549 {
550 	struct amdgpu_device *adev =
551 		(struct amdgpu_device *)file_inode(f)->i_private;
552 	int ret;
553 
554 	ret = amdgpu_ras_eeprom_reset_table(
555 		&(amdgpu_ras_get_context(adev)->eeprom_control));
556 
557 	if (!ret) {
558 		/* Something was written to EEPROM.
559 		 */
560 		amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS;
561 		return size;
562 	} else {
563 		return ret;
564 	}
565 }
566 
567 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
568 	.owner = THIS_MODULE,
569 	.read = NULL,
570 	.write = amdgpu_ras_debugfs_ctrl_write,
571 	.llseek = default_llseek
572 };
573 
574 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
575 	.owner = THIS_MODULE,
576 	.read = NULL,
577 	.write = amdgpu_ras_debugfs_eeprom_write,
578 	.llseek = default_llseek
579 };
580 
581 /**
582  * DOC: AMDGPU RAS sysfs Error Count Interface
583  *
584  * It allows the user to read the error count for each IP block on the gpu through
585  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
586  *
587  * It outputs the multiple lines which report the uncorrected (ue) and corrected
588  * (ce) error counts.
589  *
590  * The format of one line is below,
591  *
592  * [ce|ue]: count
593  *
594  * Example:
595  *
596  * .. code-block:: bash
597  *
598  *	ue: 0
599  *	ce: 1
600  *
601  */
602 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
603 		struct device_attribute *attr, char *buf)
604 {
605 	struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
606 	struct ras_query_if info = {
607 		.head = obj->head,
608 	};
609 
610 	if (!amdgpu_ras_get_error_query_ready(obj->adev))
611 		return sysfs_emit(buf, "Query currently inaccessible\n");
612 
613 	if (amdgpu_ras_query_error_status(obj->adev, &info))
614 		return -EINVAL;
615 
616 	if (obj->adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 2) &&
617 	    obj->adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 4)) {
618 		if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
619 			dev_warn(obj->adev->dev, "Failed to reset error counter and error status");
620 	}
621 
622 	return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count,
623 			  "ce", info.ce_count);
624 }
625 
626 #endif /* __linux__ */
627 
628 /* obj begin */
629 
630 #define get_obj(obj) do { (obj)->use++; } while (0)
631 #define alive_obj(obj) ((obj)->use)
632 
633 static inline void put_obj(struct ras_manager *obj)
634 {
635 	if (obj && (--obj->use == 0))
636 		list_del(&obj->node);
637 	if (obj && (obj->use < 0))
638 		DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", get_ras_block_str(&obj->head));
639 }
640 
641 /* make one obj and return it. */
642 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
643 		struct ras_common_if *head)
644 {
645 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
646 	struct ras_manager *obj;
647 
648 	if (!adev->ras_enabled || !con)
649 		return NULL;
650 
651 	if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
652 		return NULL;
653 
654 	if (head->block == AMDGPU_RAS_BLOCK__MCA) {
655 		if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
656 			return NULL;
657 
658 		obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
659 	} else
660 		obj = &con->objs[head->block];
661 
662 	/* already exist. return obj? */
663 	if (alive_obj(obj))
664 		return NULL;
665 
666 	obj->head = *head;
667 	obj->adev = adev;
668 	list_add(&obj->node, &con->head);
669 	get_obj(obj);
670 
671 	return obj;
672 }
673 
674 /* return an obj equal to head, or the first when head is NULL */
675 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
676 		struct ras_common_if *head)
677 {
678 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
679 	struct ras_manager *obj;
680 	int i;
681 
682 	if (!adev->ras_enabled || !con)
683 		return NULL;
684 
685 	if (head) {
686 		if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
687 			return NULL;
688 
689 		if (head->block == AMDGPU_RAS_BLOCK__MCA) {
690 			if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
691 				return NULL;
692 
693 			obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
694 		} else
695 			obj = &con->objs[head->block];
696 
697 		if (alive_obj(obj))
698 			return obj;
699 	} else {
700 		for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT + AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
701 			obj = &con->objs[i];
702 			if (alive_obj(obj))
703 				return obj;
704 		}
705 	}
706 
707 	return NULL;
708 }
709 /* obj end */
710 
711 /* feature ctl begin */
712 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
713 					 struct ras_common_if *head)
714 {
715 	return adev->ras_hw_enabled & BIT(head->block);
716 }
717 
718 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
719 		struct ras_common_if *head)
720 {
721 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
722 
723 	return con->features & BIT(head->block);
724 }
725 
726 /*
727  * if obj is not created, then create one.
728  * set feature enable flag.
729  */
730 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
731 		struct ras_common_if *head, int enable)
732 {
733 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
734 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
735 
736 	/* If hardware does not support ras, then do not create obj.
737 	 * But if hardware support ras, we can create the obj.
738 	 * Ras framework checks con->hw_supported to see if it need do
739 	 * corresponding initialization.
740 	 * IP checks con->support to see if it need disable ras.
741 	 */
742 	if (!amdgpu_ras_is_feature_allowed(adev, head))
743 		return 0;
744 
745 	if (enable) {
746 		if (!obj) {
747 			obj = amdgpu_ras_create_obj(adev, head);
748 			if (!obj)
749 				return -EINVAL;
750 		} else {
751 			/* In case we create obj somewhere else */
752 			get_obj(obj);
753 		}
754 		con->features |= BIT(head->block);
755 	} else {
756 		if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
757 			con->features &= ~BIT(head->block);
758 			put_obj(obj);
759 		}
760 	}
761 
762 	return 0;
763 }
764 
765 /* wrapper of psp_ras_enable_features */
766 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
767 		struct ras_common_if *head, bool enable)
768 {
769 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
770 	union ta_ras_cmd_input *info;
771 	int ret;
772 
773 	if (!con)
774 		return -EINVAL;
775 
776 	/* Do not enable ras feature if it is not allowed */
777 	if (enable &&
778 	    head->block != AMDGPU_RAS_BLOCK__GFX &&
779 	    !amdgpu_ras_is_feature_allowed(adev, head))
780 		return 0;
781 
782 	/* Only enable gfx ras feature from host side */
783 	if (head->block == AMDGPU_RAS_BLOCK__GFX &&
784 	    !amdgpu_sriov_vf(adev) &&
785 	    !amdgpu_ras_intr_triggered()) {
786 		info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
787 		if (!info)
788 			return -ENOMEM;
789 
790 		if (!enable) {
791 			info->disable_features = (struct ta_ras_disable_features_input) {
792 				.block_id =  amdgpu_ras_block_to_ta(head->block),
793 				.error_type = amdgpu_ras_error_to_ta(head->type),
794 			};
795 		} else {
796 			info->enable_features = (struct ta_ras_enable_features_input) {
797 				.block_id =  amdgpu_ras_block_to_ta(head->block),
798 				.error_type = amdgpu_ras_error_to_ta(head->type),
799 			};
800 		}
801 
802 		ret = psp_ras_enable_features(&adev->psp, info, enable);
803 		if (ret) {
804 			dev_err(adev->dev, "ras %s %s failed poison:%d ret:%d\n",
805 				enable ? "enable":"disable",
806 				get_ras_block_str(head),
807 				amdgpu_ras_is_poison_mode_supported(adev), ret);
808 			kfree(info);
809 			return ret;
810 		}
811 
812 		kfree(info);
813 	}
814 
815 	/* setup the obj */
816 	__amdgpu_ras_feature_enable(adev, head, enable);
817 
818 	return 0;
819 }
820 
821 /* Only used in device probe stage and called only once. */
822 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
823 		struct ras_common_if *head, bool enable)
824 {
825 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
826 	int ret;
827 
828 	if (!con)
829 		return -EINVAL;
830 
831 	if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
832 		if (enable) {
833 			/* There is no harm to issue a ras TA cmd regardless of
834 			 * the currecnt ras state.
835 			 * If current state == target state, it will do nothing
836 			 * But sometimes it requests driver to reset and repost
837 			 * with error code -EAGAIN.
838 			 */
839 			ret = amdgpu_ras_feature_enable(adev, head, 1);
840 			/* With old ras TA, we might fail to enable ras.
841 			 * Log it and just setup the object.
842 			 * TODO need remove this WA in the future.
843 			 */
844 			if (ret == -EINVAL) {
845 				ret = __amdgpu_ras_feature_enable(adev, head, 1);
846 				if (!ret)
847 					dev_info(adev->dev,
848 						"RAS INFO: %s setup object\n",
849 						get_ras_block_str(head));
850 			}
851 		} else {
852 			/* setup the object then issue a ras TA disable cmd.*/
853 			ret = __amdgpu_ras_feature_enable(adev, head, 1);
854 			if (ret)
855 				return ret;
856 
857 			/* gfx block ras dsiable cmd must send to ras-ta */
858 			if (head->block == AMDGPU_RAS_BLOCK__GFX)
859 				con->features |= BIT(head->block);
860 
861 			ret = amdgpu_ras_feature_enable(adev, head, 0);
862 
863 			/* clean gfx block ras features flag */
864 			if (adev->ras_enabled && head->block == AMDGPU_RAS_BLOCK__GFX)
865 				con->features &= ~BIT(head->block);
866 		}
867 	} else
868 		ret = amdgpu_ras_feature_enable(adev, head, enable);
869 
870 	return ret;
871 }
872 
873 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
874 		bool bypass)
875 {
876 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
877 	struct ras_manager *obj, *tmp;
878 
879 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
880 		/* bypass psp.
881 		 * aka just release the obj and corresponding flags
882 		 */
883 		if (bypass) {
884 			if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
885 				break;
886 		} else {
887 			if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
888 				break;
889 		}
890 	}
891 
892 	return con->features;
893 }
894 
895 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
896 		bool bypass)
897 {
898 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
899 	int i;
900 	const enum amdgpu_ras_error_type default_ras_type = AMDGPU_RAS_ERROR__NONE;
901 
902 	for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
903 		struct ras_common_if head = {
904 			.block = i,
905 			.type = default_ras_type,
906 			.sub_block_index = 0,
907 		};
908 
909 		if (i == AMDGPU_RAS_BLOCK__MCA)
910 			continue;
911 
912 		if (bypass) {
913 			/*
914 			 * bypass psp. vbios enable ras for us.
915 			 * so just create the obj
916 			 */
917 			if (__amdgpu_ras_feature_enable(adev, &head, 1))
918 				break;
919 		} else {
920 			if (amdgpu_ras_feature_enable(adev, &head, 1))
921 				break;
922 		}
923 	}
924 
925 	for (i = 0; i < AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
926 		struct ras_common_if head = {
927 			.block = AMDGPU_RAS_BLOCK__MCA,
928 			.type = default_ras_type,
929 			.sub_block_index = i,
930 		};
931 
932 		if (bypass) {
933 			/*
934 			 * bypass psp. vbios enable ras for us.
935 			 * so just create the obj
936 			 */
937 			if (__amdgpu_ras_feature_enable(adev, &head, 1))
938 				break;
939 		} else {
940 			if (amdgpu_ras_feature_enable(adev, &head, 1))
941 				break;
942 		}
943 	}
944 
945 	return con->features;
946 }
947 /* feature ctl end */
948 
949 static int amdgpu_ras_block_match_default(struct amdgpu_ras_block_object *block_obj,
950 		enum amdgpu_ras_block block)
951 {
952 	if (!block_obj)
953 		return -EINVAL;
954 
955 	if (block_obj->ras_comm.block == block)
956 		return 0;
957 
958 	return -EINVAL;
959 }
960 
961 static struct amdgpu_ras_block_object *amdgpu_ras_get_ras_block(struct amdgpu_device *adev,
962 					enum amdgpu_ras_block block, uint32_t sub_block_index)
963 {
964 	struct amdgpu_ras_block_list *node, *tmp;
965 	struct amdgpu_ras_block_object *obj;
966 
967 	if (block >= AMDGPU_RAS_BLOCK__LAST)
968 		return NULL;
969 
970 	list_for_each_entry_safe(node, tmp, &adev->ras_list, node) {
971 		if (!node->ras_obj) {
972 			dev_warn(adev->dev, "Warning: abnormal ras list node.\n");
973 			continue;
974 		}
975 
976 		obj = node->ras_obj;
977 		if (obj->ras_block_match) {
978 			if (obj->ras_block_match(obj, block, sub_block_index) == 0)
979 				return obj;
980 		} else {
981 			if (amdgpu_ras_block_match_default(obj, block) == 0)
982 				return obj;
983 		}
984 	}
985 
986 	return NULL;
987 }
988 
989 static void amdgpu_ras_get_ecc_info(struct amdgpu_device *adev, struct ras_err_data *err_data)
990 {
991 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
992 	int ret = 0;
993 
994 	/*
995 	 * choosing right query method according to
996 	 * whether smu support query error information
997 	 */
998 	ret = amdgpu_dpm_get_ecc_info(adev, (void *)&(ras->umc_ecc));
999 	if (ret == -EOPNOTSUPP) {
1000 		if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops &&
1001 			adev->umc.ras->ras_block.hw_ops->query_ras_error_count)
1002 			adev->umc.ras->ras_block.hw_ops->query_ras_error_count(adev, err_data);
1003 
1004 		/* umc query_ras_error_address is also responsible for clearing
1005 		 * error status
1006 		 */
1007 		if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops &&
1008 		    adev->umc.ras->ras_block.hw_ops->query_ras_error_address)
1009 			adev->umc.ras->ras_block.hw_ops->query_ras_error_address(adev, err_data);
1010 	} else if (!ret) {
1011 		if (adev->umc.ras &&
1012 			adev->umc.ras->ecc_info_query_ras_error_count)
1013 			adev->umc.ras->ecc_info_query_ras_error_count(adev, err_data);
1014 
1015 		if (adev->umc.ras &&
1016 			adev->umc.ras->ecc_info_query_ras_error_address)
1017 			adev->umc.ras->ecc_info_query_ras_error_address(adev, err_data);
1018 	}
1019 }
1020 
1021 /* query/inject/cure begin */
1022 int amdgpu_ras_query_error_status(struct amdgpu_device *adev,
1023 				  struct ras_query_if *info)
1024 {
1025 	struct amdgpu_ras_block_object *block_obj = NULL;
1026 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1027 	struct ras_err_data err_data = {0, 0, 0, NULL};
1028 
1029 	if (!obj)
1030 		return -EINVAL;
1031 
1032 	if (info->head.block == AMDGPU_RAS_BLOCK__UMC) {
1033 		amdgpu_ras_get_ecc_info(adev, &err_data);
1034 	} else {
1035 		block_obj = amdgpu_ras_get_ras_block(adev, info->head.block, 0);
1036 		if (!block_obj || !block_obj->hw_ops)   {
1037 			dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1038 				     get_ras_block_str(&info->head));
1039 			return -EINVAL;
1040 		}
1041 
1042 		if (block_obj->hw_ops->query_ras_error_count)
1043 			block_obj->hw_ops->query_ras_error_count(adev, &err_data);
1044 
1045 		if ((info->head.block == AMDGPU_RAS_BLOCK__SDMA) ||
1046 		    (info->head.block == AMDGPU_RAS_BLOCK__GFX) ||
1047 		    (info->head.block == AMDGPU_RAS_BLOCK__MMHUB)) {
1048 				if (block_obj->hw_ops->query_ras_error_status)
1049 					block_obj->hw_ops->query_ras_error_status(adev);
1050 			}
1051 	}
1052 
1053 	obj->err_data.ue_count += err_data.ue_count;
1054 	obj->err_data.ce_count += err_data.ce_count;
1055 
1056 	info->ue_count = obj->err_data.ue_count;
1057 	info->ce_count = obj->err_data.ce_count;
1058 
1059 	if (err_data.ce_count) {
1060 		if (!adev->aid_mask &&
1061 		    adev->smuio.funcs &&
1062 		    adev->smuio.funcs->get_socket_id &&
1063 		    adev->smuio.funcs->get_die_id) {
1064 			dev_info(adev->dev, "socket: %d, die: %d "
1065 					"%ld correctable hardware errors "
1066 					"detected in %s block, no user "
1067 					"action is needed.\n",
1068 					adev->smuio.funcs->get_socket_id(adev),
1069 					adev->smuio.funcs->get_die_id(adev),
1070 					obj->err_data.ce_count,
1071 					get_ras_block_str(&info->head));
1072 		} else {
1073 			dev_info(adev->dev, "%ld correctable hardware errors "
1074 					"detected in %s block, no user "
1075 					"action is needed.\n",
1076 					obj->err_data.ce_count,
1077 					get_ras_block_str(&info->head));
1078 		}
1079 	}
1080 	if (err_data.ue_count) {
1081 		if (!adev->aid_mask &&
1082 		    adev->smuio.funcs &&
1083 		    adev->smuio.funcs->get_socket_id &&
1084 		    adev->smuio.funcs->get_die_id) {
1085 			dev_info(adev->dev, "socket: %d, die: %d "
1086 					"%ld uncorrectable hardware errors "
1087 					"detected in %s block\n",
1088 					adev->smuio.funcs->get_socket_id(adev),
1089 					adev->smuio.funcs->get_die_id(adev),
1090 					obj->err_data.ue_count,
1091 					get_ras_block_str(&info->head));
1092 		} else {
1093 			dev_info(adev->dev, "%ld uncorrectable hardware errors "
1094 					"detected in %s block\n",
1095 					obj->err_data.ue_count,
1096 					get_ras_block_str(&info->head));
1097 		}
1098 	}
1099 
1100 	return 0;
1101 }
1102 
1103 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev,
1104 		enum amdgpu_ras_block block)
1105 {
1106 	struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev, block, 0);
1107 
1108 	if (!amdgpu_ras_is_supported(adev, block))
1109 		return -EINVAL;
1110 
1111 	if (!block_obj || !block_obj->hw_ops)   {
1112 		dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1113 			     ras_block_str(block));
1114 		return -EINVAL;
1115 	}
1116 
1117 	if (block_obj->hw_ops->reset_ras_error_count)
1118 		block_obj->hw_ops->reset_ras_error_count(adev);
1119 
1120 	if ((block == AMDGPU_RAS_BLOCK__GFX) ||
1121 	    (block == AMDGPU_RAS_BLOCK__MMHUB)) {
1122 		if (block_obj->hw_ops->reset_ras_error_status)
1123 			block_obj->hw_ops->reset_ras_error_status(adev);
1124 	}
1125 
1126 	return 0;
1127 }
1128 
1129 /* wrapper of psp_ras_trigger_error */
1130 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
1131 		struct ras_inject_if *info)
1132 {
1133 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1134 	struct ta_ras_trigger_error_input block_info = {
1135 		.block_id =  amdgpu_ras_block_to_ta(info->head.block),
1136 		.inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
1137 		.sub_block_index = info->head.sub_block_index,
1138 		.address = info->address,
1139 		.value = info->value,
1140 	};
1141 	int ret = -EINVAL;
1142 	struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev,
1143 							info->head.block,
1144 							info->head.sub_block_index);
1145 
1146 	/* inject on guest isn't allowed, return success directly */
1147 	if (amdgpu_sriov_vf(adev))
1148 		return 0;
1149 
1150 	if (!obj)
1151 		return -EINVAL;
1152 
1153 	if (!block_obj || !block_obj->hw_ops)	{
1154 		dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1155 			     get_ras_block_str(&info->head));
1156 		return -EINVAL;
1157 	}
1158 
1159 	/* Calculate XGMI relative offset */
1160 	if (adev->gmc.xgmi.num_physical_nodes > 1 &&
1161 	    info->head.block != AMDGPU_RAS_BLOCK__GFX) {
1162 		block_info.address =
1163 			amdgpu_xgmi_get_relative_phy_addr(adev,
1164 							  block_info.address);
1165 	}
1166 
1167 	if (block_obj->hw_ops->ras_error_inject) {
1168 		if (info->head.block == AMDGPU_RAS_BLOCK__GFX)
1169 			ret = block_obj->hw_ops->ras_error_inject(adev, info, info->instance_mask);
1170 		else /* Special ras_error_inject is defined (e.g: xgmi) */
1171 			ret = block_obj->hw_ops->ras_error_inject(adev, &block_info,
1172 						info->instance_mask);
1173 	} else {
1174 		/* default path */
1175 		ret = psp_ras_trigger_error(&adev->psp, &block_info, info->instance_mask);
1176 	}
1177 
1178 	if (ret)
1179 		dev_err(adev->dev, "ras inject %s failed %d\n",
1180 			get_ras_block_str(&info->head), ret);
1181 
1182 	return ret;
1183 }
1184 
1185 /**
1186  * amdgpu_ras_query_error_count_helper -- Get error counter for specific IP
1187  * @adev: pointer to AMD GPU device
1188  * @ce_count: pointer to an integer to be set to the count of correctible errors.
1189  * @ue_count: pointer to an integer to be set to the count of uncorrectible errors.
1190  * @query_info: pointer to ras_query_if
1191  *
1192  * Return 0 for query success or do nothing, otherwise return an error
1193  * on failures
1194  */
1195 static int amdgpu_ras_query_error_count_helper(struct amdgpu_device *adev,
1196 					       unsigned long *ce_count,
1197 					       unsigned long *ue_count,
1198 					       struct ras_query_if *query_info)
1199 {
1200 	int ret;
1201 
1202 	if (!query_info)
1203 		/* do nothing if query_info is not specified */
1204 		return 0;
1205 
1206 	ret = amdgpu_ras_query_error_status(adev, query_info);
1207 	if (ret)
1208 		return ret;
1209 
1210 	*ce_count += query_info->ce_count;
1211 	*ue_count += query_info->ue_count;
1212 
1213 	/* some hardware/IP supports read to clear
1214 	 * no need to explictly reset the err status after the query call */
1215 	if (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 2) &&
1216 	    adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 4)) {
1217 		if (amdgpu_ras_reset_error_status(adev, query_info->head.block))
1218 			dev_warn(adev->dev,
1219 				 "Failed to reset error counter and error status\n");
1220 	}
1221 
1222 	return 0;
1223 }
1224 
1225 /**
1226  * amdgpu_ras_query_error_count -- Get error counts of all IPs or specific IP
1227  * @adev: pointer to AMD GPU device
1228  * @ce_count: pointer to an integer to be set to the count of correctible errors.
1229  * @ue_count: pointer to an integer to be set to the count of uncorrectible
1230  * errors.
1231  * @query_info: pointer to ras_query_if if the query request is only for
1232  * specific ip block; if info is NULL, then the qurey request is for
1233  * all the ip blocks that support query ras error counters/status
1234  *
1235  * If set, @ce_count or @ue_count, count and return the corresponding
1236  * error counts in those integer pointers. Return 0 if the device
1237  * supports RAS. Return -EOPNOTSUPP if the device doesn't support RAS.
1238  */
1239 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
1240 				 unsigned long *ce_count,
1241 				 unsigned long *ue_count,
1242 				 struct ras_query_if *query_info)
1243 {
1244 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1245 	struct ras_manager *obj;
1246 	unsigned long ce, ue;
1247 	int ret;
1248 
1249 	if (!adev->ras_enabled || !con)
1250 		return -EOPNOTSUPP;
1251 
1252 	/* Don't count since no reporting.
1253 	 */
1254 	if (!ce_count && !ue_count)
1255 		return 0;
1256 
1257 	ce = 0;
1258 	ue = 0;
1259 	if (!query_info) {
1260 		/* query all the ip blocks that support ras query interface */
1261 		list_for_each_entry(obj, &con->head, node) {
1262 			struct ras_query_if info = {
1263 				.head = obj->head,
1264 			};
1265 
1266 			ret = amdgpu_ras_query_error_count_helper(adev, &ce, &ue, &info);
1267 		}
1268 	} else {
1269 		/* query specific ip block */
1270 		ret = amdgpu_ras_query_error_count_helper(adev, &ce, &ue, query_info);
1271 	}
1272 
1273 	if (ret)
1274 		return ret;
1275 
1276 	if (ce_count)
1277 		*ce_count = ce;
1278 
1279 	if (ue_count)
1280 		*ue_count = ue;
1281 
1282 	return 0;
1283 }
1284 /* query/inject/cure end */
1285 
1286 #ifdef __linux__
1287 
1288 /* sysfs begin */
1289 
1290 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1291 		struct ras_badpage **bps, unsigned int *count);
1292 
1293 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
1294 {
1295 	switch (flags) {
1296 	case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
1297 		return "R";
1298 	case AMDGPU_RAS_RETIRE_PAGE_PENDING:
1299 		return "P";
1300 	case AMDGPU_RAS_RETIRE_PAGE_FAULT:
1301 	default:
1302 		return "F";
1303 	}
1304 }
1305 
1306 /**
1307  * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
1308  *
1309  * It allows user to read the bad pages of vram on the gpu through
1310  * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
1311  *
1312  * It outputs multiple lines, and each line stands for one gpu page.
1313  *
1314  * The format of one line is below,
1315  * gpu pfn : gpu page size : flags
1316  *
1317  * gpu pfn and gpu page size are printed in hex format.
1318  * flags can be one of below character,
1319  *
1320  * R: reserved, this gpu page is reserved and not able to use.
1321  *
1322  * P: pending for reserve, this gpu page is marked as bad, will be reserved
1323  * in next window of page_reserve.
1324  *
1325  * F: unable to reserve. this gpu page can't be reserved due to some reasons.
1326  *
1327  * Examples:
1328  *
1329  * .. code-block:: bash
1330  *
1331  *	0x00000001 : 0x00001000 : R
1332  *	0x00000002 : 0x00001000 : P
1333  *
1334  */
1335 
1336 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
1337 		struct kobject *kobj, struct bin_attribute *attr,
1338 		char *buf, loff_t ppos, size_t count)
1339 {
1340 	struct amdgpu_ras *con =
1341 		container_of(attr, struct amdgpu_ras, badpages_attr);
1342 	struct amdgpu_device *adev = con->adev;
1343 	const unsigned int element_size =
1344 		sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
1345 	unsigned int start = div64_ul(ppos + element_size - 1, element_size);
1346 	unsigned int end = div64_ul(ppos + count - 1, element_size);
1347 	ssize_t s = 0;
1348 	struct ras_badpage *bps = NULL;
1349 	unsigned int bps_count = 0;
1350 
1351 	memset(buf, 0, count);
1352 
1353 	if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1354 		return 0;
1355 
1356 	for (; start < end && start < bps_count; start++)
1357 		s += scnprintf(&buf[s], element_size + 1,
1358 				"0x%08x : 0x%08x : %1s\n",
1359 				bps[start].bp,
1360 				bps[start].size,
1361 				amdgpu_ras_badpage_flags_str(bps[start].flags));
1362 
1363 	kfree(bps);
1364 
1365 	return s;
1366 }
1367 
1368 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1369 		struct device_attribute *attr, char *buf)
1370 {
1371 	struct amdgpu_ras *con =
1372 		container_of(attr, struct amdgpu_ras, features_attr);
1373 
1374 	return sysfs_emit(buf, "feature mask: 0x%x\n", con->features);
1375 }
1376 
1377 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1378 {
1379 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1380 
1381 	if (adev->dev->kobj.sd)
1382 		sysfs_remove_file_from_group(&adev->dev->kobj,
1383 				&con->badpages_attr.attr,
1384 				RAS_FS_NAME);
1385 }
1386 
1387 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1388 {
1389 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1390 	struct attribute *attrs[] = {
1391 		&con->features_attr.attr,
1392 		NULL
1393 	};
1394 	struct attribute_group group = {
1395 		.name = RAS_FS_NAME,
1396 		.attrs = attrs,
1397 	};
1398 
1399 	if (adev->dev->kobj.sd)
1400 		sysfs_remove_group(&adev->dev->kobj, &group);
1401 
1402 	return 0;
1403 }
1404 
1405 #endif /* __linux__ */
1406 
1407 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1408 		struct ras_common_if *head)
1409 {
1410 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1411 
1412 	if (!obj || obj->attr_inuse)
1413 		return -EINVAL;
1414 
1415 	STUB();
1416 	return -ENOSYS;
1417 #ifdef notyet
1418 	get_obj(obj);
1419 
1420 	snprintf(obj->fs_data.sysfs_name, sizeof(obj->fs_data.sysfs_name),
1421 		"%s_err_count", head->name);
1422 
1423 	obj->sysfs_attr = (struct device_attribute){
1424 		.attr = {
1425 			.name = obj->fs_data.sysfs_name,
1426 			.mode = S_IRUGO,
1427 		},
1428 			.show = amdgpu_ras_sysfs_read,
1429 	};
1430 	sysfs_attr_init(&obj->sysfs_attr.attr);
1431 
1432 	if (sysfs_add_file_to_group(&adev->dev->kobj,
1433 				&obj->sysfs_attr.attr,
1434 				RAS_FS_NAME)) {
1435 		put_obj(obj);
1436 		return -EINVAL;
1437 	}
1438 
1439 	obj->attr_inuse = 1;
1440 
1441 	return 0;
1442 #endif
1443 }
1444 
1445 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1446 		struct ras_common_if *head)
1447 {
1448 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1449 
1450 	if (!obj || !obj->attr_inuse)
1451 		return -EINVAL;
1452 
1453 #ifdef __linux__
1454 	if (adev->dev->kobj.sd)
1455 		sysfs_remove_file_from_group(&adev->dev->kobj,
1456 				&obj->sysfs_attr.attr,
1457 				RAS_FS_NAME);
1458 #endif
1459 	obj->attr_inuse = 0;
1460 	put_obj(obj);
1461 
1462 	return 0;
1463 }
1464 
1465 #ifdef __linux__
1466 
1467 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1468 {
1469 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1470 	struct ras_manager *obj, *tmp;
1471 
1472 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
1473 		amdgpu_ras_sysfs_remove(adev, &obj->head);
1474 	}
1475 
1476 	if (amdgpu_bad_page_threshold != 0)
1477 		amdgpu_ras_sysfs_remove_bad_page_node(adev);
1478 
1479 	amdgpu_ras_sysfs_remove_feature_node(adev);
1480 
1481 	return 0;
1482 }
1483 /* sysfs end */
1484 
1485 /**
1486  * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1487  *
1488  * Normally when there is an uncorrectable error, the driver will reset
1489  * the GPU to recover.  However, in the event of an unrecoverable error,
1490  * the driver provides an interface to reboot the system automatically
1491  * in that event.
1492  *
1493  * The following file in debugfs provides that interface:
1494  * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1495  *
1496  * Usage:
1497  *
1498  * .. code-block:: bash
1499  *
1500  *	echo true > .../ras/auto_reboot
1501  *
1502  */
1503 /* debugfs begin */
1504 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1505 {
1506 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1507 	struct amdgpu_ras_eeprom_control *eeprom = &con->eeprom_control;
1508 	struct drm_minor  *minor = adev_to_drm(adev)->primary;
1509 	struct dentry     *dir;
1510 
1511 	dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1512 	debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev,
1513 			    &amdgpu_ras_debugfs_ctrl_ops);
1514 	debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev,
1515 			    &amdgpu_ras_debugfs_eeprom_ops);
1516 	debugfs_create_u32("bad_page_cnt_threshold", 0444, dir,
1517 			   &con->bad_page_cnt_threshold);
1518 	debugfs_create_u32("ras_num_recs", 0444, dir, &eeprom->ras_num_recs);
1519 	debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled);
1520 	debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled);
1521 	debugfs_create_file("ras_eeprom_size", S_IRUGO, dir, adev,
1522 			    &amdgpu_ras_debugfs_eeprom_size_ops);
1523 	con->de_ras_eeprom_table = debugfs_create_file("ras_eeprom_table",
1524 						       S_IRUGO, dir, adev,
1525 						       &amdgpu_ras_debugfs_eeprom_table_ops);
1526 	amdgpu_ras_debugfs_set_ret_size(&con->eeprom_control);
1527 
1528 	/*
1529 	 * After one uncorrectable error happens, usually GPU recovery will
1530 	 * be scheduled. But due to the known problem in GPU recovery failing
1531 	 * to bring GPU back, below interface provides one direct way to
1532 	 * user to reboot system automatically in such case within
1533 	 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1534 	 * will never be called.
1535 	 */
1536 	debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot);
1537 
1538 	/*
1539 	 * User could set this not to clean up hardware's error count register
1540 	 * of RAS IPs during ras recovery.
1541 	 */
1542 	debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir,
1543 			    &con->disable_ras_err_cnt_harvest);
1544 	return dir;
1545 }
1546 
1547 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1548 				      struct ras_fs_if *head,
1549 				      struct dentry *dir)
1550 {
1551 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1552 
1553 	if (!obj || !dir)
1554 		return;
1555 
1556 	get_obj(obj);
1557 
1558 	memcpy(obj->fs_data.debugfs_name,
1559 			head->debugfs_name,
1560 			sizeof(obj->fs_data.debugfs_name));
1561 
1562 	debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir,
1563 			    obj, &amdgpu_ras_debugfs_ops);
1564 }
1565 
1566 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1567 {
1568 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1569 	struct dentry *dir;
1570 	struct ras_manager *obj;
1571 	struct ras_fs_if fs_info;
1572 
1573 	/*
1574 	 * it won't be called in resume path, no need to check
1575 	 * suspend and gpu reset status
1576 	 */
1577 	if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con)
1578 		return;
1579 
1580 	dir = amdgpu_ras_debugfs_create_ctrl_node(adev);
1581 
1582 	list_for_each_entry(obj, &con->head, node) {
1583 		if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1584 			(obj->attr_inuse == 1)) {
1585 			snprintf(fs_info.debugfs_name, sizeof(fs_info.debugfs_name), "%s_err_inject",
1586 					get_ras_block_str(&obj->head));
1587 			fs_info.head = obj->head;
1588 			amdgpu_ras_debugfs_create(adev, &fs_info, dir);
1589 		}
1590 	}
1591 }
1592 
1593 /* debugfs end */
1594 
1595 /* ras fs */
1596 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1597 		amdgpu_ras_sysfs_badpages_read, NULL, 0);
1598 #endif /* __linux__ */
1599 static DEVICE_ATTR(features, S_IRUGO,
1600 		amdgpu_ras_sysfs_features_read, NULL);
1601 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1602 {
1603 #ifdef __linux__
1604 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1605 	struct attribute_group group = {
1606 		.name = RAS_FS_NAME,
1607 	};
1608 	struct attribute *attrs[] = {
1609 		&con->features_attr.attr,
1610 		NULL
1611 	};
1612 	struct bin_attribute *bin_attrs[] = {
1613 		NULL,
1614 		NULL,
1615 	};
1616 	int r;
1617 
1618 	/* add features entry */
1619 	con->features_attr = dev_attr_features;
1620 	group.attrs = attrs;
1621 	sysfs_attr_init(attrs[0]);
1622 
1623 	if (amdgpu_bad_page_threshold != 0) {
1624 		/* add bad_page_features entry */
1625 		bin_attr_gpu_vram_bad_pages.private = NULL;
1626 		con->badpages_attr = bin_attr_gpu_vram_bad_pages;
1627 		bin_attrs[0] = &con->badpages_attr;
1628 		group.bin_attrs = bin_attrs;
1629 		sysfs_bin_attr_init(bin_attrs[0]);
1630 	}
1631 
1632 	r = sysfs_create_group(&adev->dev->kobj, &group);
1633 	if (r)
1634 		dev_err(adev->dev, "Failed to create RAS sysfs group!");
1635 #endif
1636 
1637 	return 0;
1638 }
1639 
1640 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1641 {
1642 #ifdef __linux__
1643 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1644 	struct ras_manager *con_obj, *ip_obj, *tmp;
1645 
1646 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1647 		list_for_each_entry_safe(con_obj, tmp, &con->head, node) {
1648 			ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head);
1649 			if (ip_obj)
1650 				put_obj(ip_obj);
1651 		}
1652 	}
1653 
1654 	amdgpu_ras_sysfs_remove_all(adev);
1655 #endif
1656 	return 0;
1657 }
1658 /* ras fs end */
1659 
1660 /* ih begin */
1661 
1662 /* For the hardware that cannot enable bif ring for both ras_controller_irq
1663  * and ras_err_evnet_athub_irq ih cookies, the driver has to poll status
1664  * register to check whether the interrupt is triggered or not, and properly
1665  * ack the interrupt if it is there
1666  */
1667 void amdgpu_ras_interrupt_fatal_error_handler(struct amdgpu_device *adev)
1668 {
1669 	/* Fatal error events are handled on host side */
1670 	if (amdgpu_sriov_vf(adev))
1671 		return;
1672 
1673 	if (adev->nbio.ras &&
1674 	    adev->nbio.ras->handle_ras_controller_intr_no_bifring)
1675 		adev->nbio.ras->handle_ras_controller_intr_no_bifring(adev);
1676 
1677 	if (adev->nbio.ras &&
1678 	    adev->nbio.ras->handle_ras_err_event_athub_intr_no_bifring)
1679 		adev->nbio.ras->handle_ras_err_event_athub_intr_no_bifring(adev);
1680 }
1681 
1682 static void amdgpu_ras_interrupt_poison_consumption_handler(struct ras_manager *obj,
1683 				struct amdgpu_iv_entry *entry)
1684 {
1685 	bool poison_stat = false;
1686 	struct amdgpu_device *adev = obj->adev;
1687 	struct amdgpu_ras_block_object *block_obj =
1688 		amdgpu_ras_get_ras_block(adev, obj->head.block, 0);
1689 
1690 	if (!block_obj)
1691 		return;
1692 
1693 	/* both query_poison_status and handle_poison_consumption are optional,
1694 	 * but at least one of them should be implemented if we need poison
1695 	 * consumption handler
1696 	 */
1697 	if (block_obj->hw_ops && block_obj->hw_ops->query_poison_status) {
1698 		poison_stat = block_obj->hw_ops->query_poison_status(adev);
1699 		if (!poison_stat) {
1700 			/* Not poison consumption interrupt, no need to handle it */
1701 			dev_info(adev->dev, "No RAS poison status in %s poison IH.\n",
1702 					block_obj->ras_comm.name);
1703 
1704 			return;
1705 		}
1706 	}
1707 
1708 	amdgpu_umc_poison_handler(adev, false);
1709 
1710 	if (block_obj->hw_ops && block_obj->hw_ops->handle_poison_consumption)
1711 		poison_stat = block_obj->hw_ops->handle_poison_consumption(adev);
1712 
1713 	/* gpu reset is fallback for failed and default cases */
1714 	if (poison_stat) {
1715 		dev_info(adev->dev, "GPU reset for %s RAS poison consumption is issued!\n",
1716 				block_obj->ras_comm.name);
1717 		amdgpu_ras_reset_gpu(adev);
1718 	} else {
1719 		amdgpu_gfx_poison_consumption_handler(adev, entry);
1720 	}
1721 }
1722 
1723 static void amdgpu_ras_interrupt_poison_creation_handler(struct ras_manager *obj,
1724 				struct amdgpu_iv_entry *entry)
1725 {
1726 	dev_info(obj->adev->dev,
1727 		"Poison is created, no user action is needed.\n");
1728 }
1729 
1730 static void amdgpu_ras_interrupt_umc_handler(struct ras_manager *obj,
1731 				struct amdgpu_iv_entry *entry)
1732 {
1733 	struct ras_ih_data *data = &obj->ih_data;
1734 	struct ras_err_data err_data = {0, 0, 0, NULL};
1735 	int ret;
1736 
1737 	if (!data->cb)
1738 		return;
1739 
1740 	/* Let IP handle its data, maybe we need get the output
1741 	 * from the callback to update the error type/count, etc
1742 	 */
1743 	ret = data->cb(obj->adev, &err_data, entry);
1744 	/* ue will trigger an interrupt, and in that case
1745 	 * we need do a reset to recovery the whole system.
1746 	 * But leave IP do that recovery, here we just dispatch
1747 	 * the error.
1748 	 */
1749 	if (ret == AMDGPU_RAS_SUCCESS) {
1750 		/* these counts could be left as 0 if
1751 		 * some blocks do not count error number
1752 		 */
1753 		obj->err_data.ue_count += err_data.ue_count;
1754 		obj->err_data.ce_count += err_data.ce_count;
1755 	}
1756 }
1757 
1758 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1759 {
1760 	struct ras_ih_data *data = &obj->ih_data;
1761 	struct amdgpu_iv_entry entry;
1762 
1763 	while (data->rptr != data->wptr) {
1764 		rmb();
1765 		memcpy(&entry, &data->ring[data->rptr],
1766 				data->element_size);
1767 
1768 		wmb();
1769 		data->rptr = (data->aligned_element_size +
1770 				data->rptr) % data->ring_size;
1771 
1772 		if (amdgpu_ras_is_poison_mode_supported(obj->adev)) {
1773 			if (obj->head.block == AMDGPU_RAS_BLOCK__UMC)
1774 				amdgpu_ras_interrupt_poison_creation_handler(obj, &entry);
1775 			else
1776 				amdgpu_ras_interrupt_poison_consumption_handler(obj, &entry);
1777 		} else {
1778 			if (obj->head.block == AMDGPU_RAS_BLOCK__UMC)
1779 				amdgpu_ras_interrupt_umc_handler(obj, &entry);
1780 			else
1781 				dev_warn(obj->adev->dev,
1782 					"No RAS interrupt handler for non-UMC block with poison disabled.\n");
1783 		}
1784 	}
1785 }
1786 
1787 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1788 {
1789 	struct ras_ih_data *data =
1790 		container_of(work, struct ras_ih_data, ih_work);
1791 	struct ras_manager *obj =
1792 		container_of(data, struct ras_manager, ih_data);
1793 
1794 	amdgpu_ras_interrupt_handler(obj);
1795 }
1796 
1797 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1798 		struct ras_dispatch_if *info)
1799 {
1800 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1801 	struct ras_ih_data *data = &obj->ih_data;
1802 
1803 	if (!obj)
1804 		return -EINVAL;
1805 
1806 	if (data->inuse == 0)
1807 		return 0;
1808 
1809 	/* Might be overflow... */
1810 	memcpy(&data->ring[data->wptr], info->entry,
1811 			data->element_size);
1812 
1813 	wmb();
1814 	data->wptr = (data->aligned_element_size +
1815 			data->wptr) % data->ring_size;
1816 
1817 	schedule_work(&data->ih_work);
1818 
1819 	return 0;
1820 }
1821 
1822 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1823 		struct ras_common_if *head)
1824 {
1825 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1826 	struct ras_ih_data *data;
1827 
1828 	if (!obj)
1829 		return -EINVAL;
1830 
1831 	data = &obj->ih_data;
1832 	if (data->inuse == 0)
1833 		return 0;
1834 
1835 	cancel_work_sync(&data->ih_work);
1836 
1837 	kfree(data->ring);
1838 	memset(data, 0, sizeof(*data));
1839 	put_obj(obj);
1840 
1841 	return 0;
1842 }
1843 
1844 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1845 		struct ras_common_if *head)
1846 {
1847 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1848 	struct ras_ih_data *data;
1849 	struct amdgpu_ras_block_object *ras_obj;
1850 
1851 	if (!obj) {
1852 		/* in case we registe the IH before enable ras feature */
1853 		obj = amdgpu_ras_create_obj(adev, head);
1854 		if (!obj)
1855 			return -EINVAL;
1856 	} else
1857 		get_obj(obj);
1858 
1859 	ras_obj = container_of(head, struct amdgpu_ras_block_object, ras_comm);
1860 
1861 	data = &obj->ih_data;
1862 	/* add the callback.etc */
1863 	*data = (struct ras_ih_data) {
1864 		.inuse = 0,
1865 		.cb = ras_obj->ras_cb,
1866 		.element_size = sizeof(struct amdgpu_iv_entry),
1867 		.rptr = 0,
1868 		.wptr = 0,
1869 	};
1870 
1871 	INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1872 
1873 	data->aligned_element_size = ALIGN(data->element_size, 8);
1874 	/* the ring can store 64 iv entries. */
1875 	data->ring_size = 64 * data->aligned_element_size;
1876 	data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1877 	if (!data->ring) {
1878 		put_obj(obj);
1879 		return -ENOMEM;
1880 	}
1881 
1882 	/* IH is ready */
1883 	data->inuse = 1;
1884 
1885 	return 0;
1886 }
1887 
1888 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1889 {
1890 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1891 	struct ras_manager *obj, *tmp;
1892 
1893 	list_for_each_entry_safe(obj, tmp, &con->head, node) {
1894 		amdgpu_ras_interrupt_remove_handler(adev, &obj->head);
1895 	}
1896 
1897 	return 0;
1898 }
1899 /* ih end */
1900 
1901 /* traversal all IPs except NBIO to query error counter */
1902 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1903 {
1904 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1905 	struct ras_manager *obj;
1906 
1907 	if (!adev->ras_enabled || !con)
1908 		return;
1909 
1910 	list_for_each_entry(obj, &con->head, node) {
1911 		struct ras_query_if info = {
1912 			.head = obj->head,
1913 		};
1914 
1915 		/*
1916 		 * PCIE_BIF IP has one different isr by ras controller
1917 		 * interrupt, the specific ras counter query will be
1918 		 * done in that isr. So skip such block from common
1919 		 * sync flood interrupt isr calling.
1920 		 */
1921 		if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1922 			continue;
1923 
1924 		/*
1925 		 * this is a workaround for aldebaran, skip send msg to
1926 		 * smu to get ecc_info table due to smu handle get ecc
1927 		 * info table failed temporarily.
1928 		 * should be removed until smu fix handle ecc_info table.
1929 		 */
1930 		if ((info.head.block == AMDGPU_RAS_BLOCK__UMC) &&
1931 			(adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 2)))
1932 			continue;
1933 
1934 		amdgpu_ras_query_error_status(adev, &info);
1935 
1936 		if (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 2) &&
1937 		    adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 4) &&
1938 		    adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 0)) {
1939 			if (amdgpu_ras_reset_error_status(adev, info.head.block))
1940 				dev_warn(adev->dev, "Failed to reset error counter and error status");
1941 		}
1942 	}
1943 }
1944 
1945 /* Parse RdRspStatus and WrRspStatus */
1946 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
1947 					  struct ras_query_if *info)
1948 {
1949 	struct amdgpu_ras_block_object *block_obj;
1950 	/*
1951 	 * Only two block need to query read/write
1952 	 * RspStatus at current state
1953 	 */
1954 	if ((info->head.block != AMDGPU_RAS_BLOCK__GFX) &&
1955 		(info->head.block != AMDGPU_RAS_BLOCK__MMHUB))
1956 		return;
1957 
1958 	block_obj = amdgpu_ras_get_ras_block(adev,
1959 					info->head.block,
1960 					info->head.sub_block_index);
1961 
1962 	if (!block_obj || !block_obj->hw_ops) {
1963 		dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1964 			     get_ras_block_str(&info->head));
1965 		return;
1966 	}
1967 
1968 	if (block_obj->hw_ops->query_ras_error_status)
1969 		block_obj->hw_ops->query_ras_error_status(adev);
1970 
1971 }
1972 
1973 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
1974 {
1975 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1976 	struct ras_manager *obj;
1977 
1978 	if (!adev->ras_enabled || !con)
1979 		return;
1980 
1981 	list_for_each_entry(obj, &con->head, node) {
1982 		struct ras_query_if info = {
1983 			.head = obj->head,
1984 		};
1985 
1986 		amdgpu_ras_error_status_query(adev, &info);
1987 	}
1988 }
1989 
1990 /* recovery begin */
1991 
1992 /* return 0 on success.
1993  * caller need free bps.
1994  */
1995 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1996 		struct ras_badpage **bps, unsigned int *count)
1997 {
1998 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1999 	struct ras_err_handler_data *data;
2000 	int i = 0;
2001 	int ret = 0, status;
2002 
2003 	if (!con || !con->eh_data || !bps || !count)
2004 		return -EINVAL;
2005 
2006 	mutex_lock(&con->recovery_lock);
2007 	data = con->eh_data;
2008 	if (!data || data->count == 0) {
2009 		*bps = NULL;
2010 		ret = -EINVAL;
2011 		goto out;
2012 	}
2013 
2014 	*bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
2015 	if (!*bps) {
2016 		ret = -ENOMEM;
2017 		goto out;
2018 	}
2019 
2020 	for (; i < data->count; i++) {
2021 		(*bps)[i] = (struct ras_badpage){
2022 			.bp = data->bps[i].retired_page,
2023 			.size = AMDGPU_GPU_PAGE_SIZE,
2024 			.flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
2025 		};
2026 		status = amdgpu_vram_mgr_query_page_status(&adev->mman.vram_mgr,
2027 				data->bps[i].retired_page);
2028 		if (status == -EBUSY)
2029 			(*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
2030 		else if (status == -ENOENT)
2031 			(*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
2032 	}
2033 
2034 	*count = data->count;
2035 out:
2036 	mutex_unlock(&con->recovery_lock);
2037 	return ret;
2038 }
2039 
2040 static void amdgpu_ras_do_recovery(struct work_struct *work)
2041 {
2042 	struct amdgpu_ras *ras =
2043 		container_of(work, struct amdgpu_ras, recovery_work);
2044 	struct amdgpu_device *remote_adev = NULL;
2045 	struct amdgpu_device *adev = ras->adev;
2046 	struct list_head device_list, *device_list_handle =  NULL;
2047 
2048 	if (!ras->disable_ras_err_cnt_harvest) {
2049 		struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
2050 
2051 		/* Build list of devices to query RAS related errors */
2052 		if  (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
2053 			device_list_handle = &hive->device_list;
2054 		} else {
2055 			INIT_LIST_HEAD(&device_list);
2056 			list_add_tail(&adev->gmc.xgmi.head, &device_list);
2057 			device_list_handle = &device_list;
2058 		}
2059 
2060 		list_for_each_entry(remote_adev,
2061 				device_list_handle, gmc.xgmi.head) {
2062 			amdgpu_ras_query_err_status(remote_adev);
2063 			amdgpu_ras_log_on_err_counter(remote_adev);
2064 		}
2065 
2066 		amdgpu_put_xgmi_hive(hive);
2067 	}
2068 
2069 	if (amdgpu_device_should_recover_gpu(ras->adev)) {
2070 		struct amdgpu_reset_context reset_context;
2071 		memset(&reset_context, 0, sizeof(reset_context));
2072 
2073 		reset_context.method = AMD_RESET_METHOD_NONE;
2074 		reset_context.reset_req_dev = adev;
2075 
2076 		/* Perform full reset in fatal error mode */
2077 		if (!amdgpu_ras_is_poison_mode_supported(ras->adev))
2078 			set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
2079 		else {
2080 			clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
2081 
2082 			if (ras->gpu_reset_flags & AMDGPU_RAS_GPU_RESET_MODE2_RESET) {
2083 				ras->gpu_reset_flags &= ~AMDGPU_RAS_GPU_RESET_MODE2_RESET;
2084 				reset_context.method = AMD_RESET_METHOD_MODE2;
2085 			}
2086 
2087 			/* Fatal error occurs in poison mode, mode1 reset is used to
2088 			 * recover gpu.
2089 			 */
2090 			if (ras->gpu_reset_flags & AMDGPU_RAS_GPU_RESET_MODE1_RESET) {
2091 				ras->gpu_reset_flags &= ~AMDGPU_RAS_GPU_RESET_MODE1_RESET;
2092 				set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
2093 
2094 				psp_fatal_error_recovery_quirk(&adev->psp);
2095 			}
2096 		}
2097 
2098 		amdgpu_device_gpu_recover(ras->adev, NULL, &reset_context);
2099 	}
2100 	atomic_set(&ras->in_recovery, 0);
2101 }
2102 
2103 /* alloc/realloc bps array */
2104 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
2105 		struct ras_err_handler_data *data, int pages)
2106 {
2107 	unsigned int old_space = data->count + data->space_left;
2108 	unsigned int new_space = old_space + pages;
2109 	unsigned int align_space = ALIGN(new_space, 512);
2110 	void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
2111 
2112 	if (!bps) {
2113 		return -ENOMEM;
2114 	}
2115 
2116 	if (data->bps) {
2117 		memcpy(bps, data->bps,
2118 				data->count * sizeof(*data->bps));
2119 		kfree(data->bps);
2120 	}
2121 
2122 	data->bps = bps;
2123 	data->space_left += align_space - old_space;
2124 	return 0;
2125 }
2126 
2127 /* it deal with vram only. */
2128 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
2129 		struct eeprom_table_record *bps, int pages)
2130 {
2131 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2132 	struct ras_err_handler_data *data;
2133 	int ret = 0;
2134 	uint32_t i;
2135 
2136 	if (!con || !con->eh_data || !bps || pages <= 0)
2137 		return 0;
2138 
2139 	mutex_lock(&con->recovery_lock);
2140 	data = con->eh_data;
2141 	if (!data)
2142 		goto out;
2143 
2144 	for (i = 0; i < pages; i++) {
2145 		if (amdgpu_ras_check_bad_page_unlock(con,
2146 			bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT))
2147 			continue;
2148 
2149 		if (!data->space_left &&
2150 			amdgpu_ras_realloc_eh_data_space(adev, data, 256)) {
2151 			ret = -ENOMEM;
2152 			goto out;
2153 		}
2154 
2155 		amdgpu_vram_mgr_reserve_range(&adev->mman.vram_mgr,
2156 			bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT,
2157 			AMDGPU_GPU_PAGE_SIZE);
2158 
2159 		memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps));
2160 		data->count++;
2161 		data->space_left--;
2162 	}
2163 out:
2164 	mutex_unlock(&con->recovery_lock);
2165 
2166 	return ret;
2167 }
2168 
2169 /*
2170  * write error record array to eeprom, the function should be
2171  * protected by recovery_lock
2172  * new_cnt: new added UE count, excluding reserved bad pages, can be NULL
2173  */
2174 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev,
2175 		unsigned long *new_cnt)
2176 {
2177 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2178 	struct ras_err_handler_data *data;
2179 	struct amdgpu_ras_eeprom_control *control;
2180 	int save_count;
2181 
2182 	if (!con || !con->eh_data) {
2183 		if (new_cnt)
2184 			*new_cnt = 0;
2185 
2186 		return 0;
2187 	}
2188 
2189 	mutex_lock(&con->recovery_lock);
2190 	control = &con->eeprom_control;
2191 	data = con->eh_data;
2192 	save_count = data->count - control->ras_num_recs;
2193 	mutex_unlock(&con->recovery_lock);
2194 
2195 	if (new_cnt)
2196 		*new_cnt = save_count / adev->umc.retire_unit;
2197 
2198 	/* only new entries are saved */
2199 	if (save_count > 0) {
2200 		if (amdgpu_ras_eeprom_append(control,
2201 					     &data->bps[control->ras_num_recs],
2202 					     save_count)) {
2203 			dev_err(adev->dev, "Failed to save EEPROM table data!");
2204 			return -EIO;
2205 		}
2206 
2207 		dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
2208 	}
2209 
2210 	return 0;
2211 }
2212 
2213 /*
2214  * read error record array in eeprom and reserve enough space for
2215  * storing new bad pages
2216  */
2217 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
2218 {
2219 	struct amdgpu_ras_eeprom_control *control =
2220 		&adev->psp.ras_context.ras->eeprom_control;
2221 	struct eeprom_table_record *bps;
2222 	int ret;
2223 
2224 	/* no bad page record, skip eeprom access */
2225 	if (control->ras_num_recs == 0 || amdgpu_bad_page_threshold == 0)
2226 		return 0;
2227 
2228 	bps = kcalloc(control->ras_num_recs, sizeof(*bps), GFP_KERNEL);
2229 	if (!bps)
2230 		return -ENOMEM;
2231 
2232 	ret = amdgpu_ras_eeprom_read(control, bps, control->ras_num_recs);
2233 	if (ret)
2234 		dev_err(adev->dev, "Failed to load EEPROM table records!");
2235 	else
2236 		ret = amdgpu_ras_add_bad_pages(adev, bps, control->ras_num_recs);
2237 
2238 	kfree(bps);
2239 	return ret;
2240 }
2241 
2242 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
2243 				uint64_t addr)
2244 {
2245 	struct ras_err_handler_data *data = con->eh_data;
2246 	int i;
2247 
2248 	addr >>= AMDGPU_GPU_PAGE_SHIFT;
2249 	for (i = 0; i < data->count; i++)
2250 		if (addr == data->bps[i].retired_page)
2251 			return true;
2252 
2253 	return false;
2254 }
2255 
2256 /*
2257  * check if an address belongs to bad page
2258  *
2259  * Note: this check is only for umc block
2260  */
2261 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
2262 				uint64_t addr)
2263 {
2264 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2265 	bool ret = false;
2266 
2267 	if (!con || !con->eh_data)
2268 		return ret;
2269 
2270 	mutex_lock(&con->recovery_lock);
2271 	ret = amdgpu_ras_check_bad_page_unlock(con, addr);
2272 	mutex_unlock(&con->recovery_lock);
2273 	return ret;
2274 }
2275 
2276 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
2277 					  uint32_t max_count)
2278 {
2279 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2280 
2281 	/*
2282 	 * Justification of value bad_page_cnt_threshold in ras structure
2283 	 *
2284 	 * Generally, 0 <= amdgpu_bad_page_threshold <= max record length
2285 	 * in eeprom or amdgpu_bad_page_threshold == -2, introduce two
2286 	 * scenarios accordingly.
2287 	 *
2288 	 * Bad page retirement enablement:
2289 	 *    - If amdgpu_bad_page_threshold = -2,
2290 	 *      bad_page_cnt_threshold = typical value by formula.
2291 	 *
2292 	 *    - When the value from user is 0 < amdgpu_bad_page_threshold <
2293 	 *      max record length in eeprom, use it directly.
2294 	 *
2295 	 * Bad page retirement disablement:
2296 	 *    - If amdgpu_bad_page_threshold = 0, bad page retirement
2297 	 *      functionality is disabled, and bad_page_cnt_threshold will
2298 	 *      take no effect.
2299 	 */
2300 
2301 	if (amdgpu_bad_page_threshold < 0) {
2302 		u64 val = adev->gmc.mc_vram_size;
2303 
2304 		do_div(val, RAS_BAD_PAGE_COVER);
2305 		con->bad_page_cnt_threshold = min(lower_32_bits(val),
2306 						  max_count);
2307 	} else {
2308 		con->bad_page_cnt_threshold = min_t(int, max_count,
2309 						    amdgpu_bad_page_threshold);
2310 	}
2311 }
2312 
2313 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
2314 {
2315 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2316 	struct ras_err_handler_data **data;
2317 	u32  max_eeprom_records_count = 0;
2318 	bool exc_err_limit = false;
2319 	int ret;
2320 
2321 	if (!con || amdgpu_sriov_vf(adev))
2322 		return 0;
2323 
2324 	/* Allow access to RAS EEPROM via debugfs, when the ASIC
2325 	 * supports RAS and debugfs is enabled, but when
2326 	 * adev->ras_enabled is unset, i.e. when "ras_enable"
2327 	 * module parameter is set to 0.
2328 	 */
2329 	con->adev = adev;
2330 
2331 	if (!adev->ras_enabled)
2332 		return 0;
2333 
2334 	data = &con->eh_data;
2335 	*data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
2336 	if (!*data) {
2337 		ret = -ENOMEM;
2338 		goto out;
2339 	}
2340 
2341 	rw_init(&con->recovery_lock, "rasrec");
2342 	INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
2343 	atomic_set(&con->in_recovery, 0);
2344 	con->eeprom_control.bad_channel_bitmap = 0;
2345 
2346 	max_eeprom_records_count = amdgpu_ras_eeprom_max_record_count(&con->eeprom_control);
2347 	amdgpu_ras_validate_threshold(adev, max_eeprom_records_count);
2348 
2349 	/* Todo: During test the SMU might fail to read the eeprom through I2C
2350 	 * when the GPU is pending on XGMI reset during probe time
2351 	 * (Mostly after second bus reset), skip it now
2352 	 */
2353 	if (adev->gmc.xgmi.pending_reset)
2354 		return 0;
2355 	ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit);
2356 	/*
2357 	 * This calling fails when exc_err_limit is true or
2358 	 * ret != 0.
2359 	 */
2360 	if (exc_err_limit || ret)
2361 		goto free;
2362 
2363 	if (con->eeprom_control.ras_num_recs) {
2364 		ret = amdgpu_ras_load_bad_pages(adev);
2365 		if (ret)
2366 			goto free;
2367 
2368 		amdgpu_dpm_send_hbm_bad_pages_num(adev, con->eeprom_control.ras_num_recs);
2369 
2370 		if (con->update_channel_flag == true) {
2371 			amdgpu_dpm_send_hbm_bad_channel_flag(adev, con->eeprom_control.bad_channel_bitmap);
2372 			con->update_channel_flag = false;
2373 		}
2374 	}
2375 
2376 #ifdef CONFIG_X86_MCE_AMD
2377 	if ((adev->asic_type == CHIP_ALDEBARAN) &&
2378 	    (adev->gmc.xgmi.connected_to_cpu))
2379 		amdgpu_register_bad_pages_mca_notifier(adev);
2380 #endif
2381 	return 0;
2382 
2383 free:
2384 	kfree((*data)->bps);
2385 	kfree(*data);
2386 	con->eh_data = NULL;
2387 out:
2388 	dev_warn(adev->dev, "Failed to initialize ras recovery! (%d)\n", ret);
2389 
2390 	/*
2391 	 * Except error threshold exceeding case, other failure cases in this
2392 	 * function would not fail amdgpu driver init.
2393 	 */
2394 	if (!exc_err_limit)
2395 		ret = 0;
2396 	else
2397 		ret = -EINVAL;
2398 
2399 	return ret;
2400 }
2401 
2402 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
2403 {
2404 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2405 	struct ras_err_handler_data *data = con->eh_data;
2406 
2407 	/* recovery_init failed to init it, fini is useless */
2408 	if (!data)
2409 		return 0;
2410 
2411 	cancel_work_sync(&con->recovery_work);
2412 
2413 	mutex_lock(&con->recovery_lock);
2414 	con->eh_data = NULL;
2415 	kfree(data->bps);
2416 	kfree(data);
2417 	mutex_unlock(&con->recovery_lock);
2418 
2419 	return 0;
2420 }
2421 /* recovery end */
2422 
2423 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev)
2424 {
2425 	if (amdgpu_sriov_vf(adev)) {
2426 		switch (adev->ip_versions[MP0_HWIP][0]) {
2427 		case IP_VERSION(13, 0, 2):
2428 		case IP_VERSION(13, 0, 6):
2429 			return true;
2430 		default:
2431 			return false;
2432 		}
2433 	}
2434 
2435 	if (adev->asic_type == CHIP_IP_DISCOVERY) {
2436 		switch (adev->ip_versions[MP0_HWIP][0]) {
2437 		case IP_VERSION(13, 0, 0):
2438 		case IP_VERSION(13, 0, 6):
2439 		case IP_VERSION(13, 0, 10):
2440 			return true;
2441 		default:
2442 			return false;
2443 		}
2444 	}
2445 
2446 	return adev->asic_type == CHIP_VEGA10 ||
2447 		adev->asic_type == CHIP_VEGA20 ||
2448 		adev->asic_type == CHIP_ARCTURUS ||
2449 		adev->asic_type == CHIP_ALDEBARAN ||
2450 		adev->asic_type == CHIP_SIENNA_CICHLID;
2451 }
2452 
2453 /*
2454  * this is workaround for vega20 workstation sku,
2455  * force enable gfx ras, ignore vbios gfx ras flag
2456  * due to GC EDC can not write
2457  */
2458 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev)
2459 {
2460 	struct atom_context *ctx = adev->mode_info.atom_context;
2461 
2462 	if (!ctx)
2463 		return;
2464 
2465 	if (strnstr(ctx->vbios_pn, "D16406",
2466 		    sizeof(ctx->vbios_pn)) ||
2467 		strnstr(ctx->vbios_pn, "D36002",
2468 			sizeof(ctx->vbios_pn)))
2469 		adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX);
2470 }
2471 
2472 /*
2473  * check hardware's ras ability which will be saved in hw_supported.
2474  * if hardware does not support ras, we can skip some ras initializtion and
2475  * forbid some ras operations from IP.
2476  * if software itself, say boot parameter, limit the ras ability. We still
2477  * need allow IP do some limited operations, like disable. In such case,
2478  * we have to initialize ras as normal. but need check if operation is
2479  * allowed or not in each function.
2480  */
2481 static void amdgpu_ras_check_supported(struct amdgpu_device *adev)
2482 {
2483 	adev->ras_hw_enabled = adev->ras_enabled = 0;
2484 
2485 	if (!amdgpu_ras_asic_supported(adev))
2486 		return;
2487 
2488 	if (!adev->gmc.xgmi.connected_to_cpu &&	!adev->gmc.is_app_apu) {
2489 		if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
2490 			dev_info(adev->dev, "MEM ECC is active.\n");
2491 			adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC |
2492 						   1 << AMDGPU_RAS_BLOCK__DF);
2493 		} else {
2494 			dev_info(adev->dev, "MEM ECC is not presented.\n");
2495 		}
2496 
2497 		if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
2498 			dev_info(adev->dev, "SRAM ECC is active.\n");
2499 			if (!amdgpu_sriov_vf(adev))
2500 				adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
2501 							    1 << AMDGPU_RAS_BLOCK__DF);
2502 			else
2503 				adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__PCIE_BIF |
2504 								1 << AMDGPU_RAS_BLOCK__SDMA |
2505 								1 << AMDGPU_RAS_BLOCK__GFX);
2506 
2507 			/* VCN/JPEG RAS can be supported on both bare metal and
2508 			 * SRIOV environment
2509 			 */
2510 			if (adev->ip_versions[VCN_HWIP][0] == IP_VERSION(2, 6, 0) ||
2511 			    adev->ip_versions[VCN_HWIP][0] == IP_VERSION(4, 0, 0))
2512 				adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__VCN |
2513 							1 << AMDGPU_RAS_BLOCK__JPEG);
2514 			else
2515 				adev->ras_hw_enabled &= ~(1 << AMDGPU_RAS_BLOCK__VCN |
2516 							1 << AMDGPU_RAS_BLOCK__JPEG);
2517 
2518 			/*
2519 			 * XGMI RAS is not supported if xgmi num physical nodes
2520 			 * is zero
2521 			 */
2522 			if (!adev->gmc.xgmi.num_physical_nodes)
2523 				adev->ras_hw_enabled &= ~(1 << AMDGPU_RAS_BLOCK__XGMI_WAFL);
2524 		} else {
2525 			dev_info(adev->dev, "SRAM ECC is not presented.\n");
2526 		}
2527 	} else {
2528 		/* driver only manages a few IP blocks RAS feature
2529 		 * when GPU is connected cpu through XGMI */
2530 		adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX |
2531 					   1 << AMDGPU_RAS_BLOCK__SDMA |
2532 					   1 << AMDGPU_RAS_BLOCK__MMHUB);
2533 	}
2534 
2535 	amdgpu_ras_get_quirks(adev);
2536 
2537 	/* hw_supported needs to be aligned with RAS block mask. */
2538 	adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK;
2539 
2540 
2541 	/*
2542 	 * Disable ras feature for aqua vanjaram
2543 	 * by default on apu platform.
2544 	 */
2545 	if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6) &&
2546 	    adev->gmc.is_app_apu)
2547 		adev->ras_enabled = amdgpu_ras_enable != 1 ? 0 :
2548 			adev->ras_hw_enabled & amdgpu_ras_mask;
2549 	else
2550 		adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 :
2551 			adev->ras_hw_enabled & amdgpu_ras_mask;
2552 }
2553 
2554 static void amdgpu_ras_counte_dw(struct work_struct *work)
2555 {
2556 	struct amdgpu_ras *con = container_of(work, struct amdgpu_ras,
2557 					      ras_counte_delay_work.work);
2558 	struct amdgpu_device *adev = con->adev;
2559 	struct drm_device *dev = adev_to_drm(adev);
2560 	unsigned long ce_count, ue_count;
2561 	int res;
2562 
2563 	res = pm_runtime_get_sync(dev->dev);
2564 	if (res < 0)
2565 		goto Out;
2566 
2567 	/* Cache new values.
2568 	 */
2569 	if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count, NULL) == 0) {
2570 		atomic_set(&con->ras_ce_count, ce_count);
2571 		atomic_set(&con->ras_ue_count, ue_count);
2572 	}
2573 
2574 	pm_runtime_mark_last_busy(dev->dev);
2575 Out:
2576 	pm_runtime_put_autosuspend(dev->dev);
2577 }
2578 
2579 static void amdgpu_ras_query_poison_mode(struct amdgpu_device *adev)
2580 {
2581 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2582 	bool df_poison, umc_poison;
2583 
2584 	/* poison setting is useless on SRIOV guest */
2585 	if (amdgpu_sriov_vf(adev) || !con)
2586 		return;
2587 
2588 	/* Init poison supported flag, the default value is false */
2589 	if (adev->gmc.xgmi.connected_to_cpu) {
2590 		/* enabled by default when GPU is connected to CPU */
2591 		con->poison_supported = true;
2592 	} else if (adev->df.funcs &&
2593 	    adev->df.funcs->query_ras_poison_mode &&
2594 	    adev->umc.ras &&
2595 	    adev->umc.ras->query_ras_poison_mode) {
2596 		df_poison =
2597 			adev->df.funcs->query_ras_poison_mode(adev);
2598 		umc_poison =
2599 			adev->umc.ras->query_ras_poison_mode(adev);
2600 
2601 		/* Only poison is set in both DF and UMC, we can support it */
2602 		if (df_poison && umc_poison)
2603 			con->poison_supported = true;
2604 		else if (df_poison != umc_poison)
2605 			dev_warn(adev->dev,
2606 				"Poison setting is inconsistent in DF/UMC(%d:%d)!\n",
2607 				df_poison, umc_poison);
2608 	}
2609 }
2610 
2611 int amdgpu_ras_init(struct amdgpu_device *adev)
2612 {
2613 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2614 	int r;
2615 
2616 	if (con)
2617 		return 0;
2618 
2619 	con = kmalloc(sizeof(struct amdgpu_ras) +
2620 			sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT +
2621 			sizeof(struct ras_manager) * AMDGPU_RAS_MCA_BLOCK_COUNT,
2622 			GFP_KERNEL|__GFP_ZERO);
2623 	if (!con)
2624 		return -ENOMEM;
2625 
2626 	con->adev = adev;
2627 	INIT_DELAYED_WORK(&con->ras_counte_delay_work, amdgpu_ras_counte_dw);
2628 	atomic_set(&con->ras_ce_count, 0);
2629 	atomic_set(&con->ras_ue_count, 0);
2630 
2631 	con->objs = (struct ras_manager *)(con + 1);
2632 
2633 	amdgpu_ras_set_context(adev, con);
2634 
2635 	amdgpu_ras_check_supported(adev);
2636 
2637 	if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) {
2638 		/* set gfx block ras context feature for VEGA20 Gaming
2639 		 * send ras disable cmd to ras ta during ras late init.
2640 		 */
2641 		if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) {
2642 			con->features |= BIT(AMDGPU_RAS_BLOCK__GFX);
2643 
2644 			return 0;
2645 		}
2646 
2647 		r = 0;
2648 		goto release_con;
2649 	}
2650 
2651 	con->update_channel_flag = false;
2652 	con->features = 0;
2653 	INIT_LIST_HEAD(&con->head);
2654 	/* Might need get this flag from vbios. */
2655 	con->flags = RAS_DEFAULT_FLAGS;
2656 
2657 	/* initialize nbio ras function ahead of any other
2658 	 * ras functions so hardware fatal error interrupt
2659 	 * can be enabled as early as possible */
2660 	switch (adev->ip_versions[NBIO_HWIP][0]) {
2661 	case IP_VERSION(7, 4, 0):
2662 	case IP_VERSION(7, 4, 1):
2663 	case IP_VERSION(7, 4, 4):
2664 		if (!adev->gmc.xgmi.connected_to_cpu)
2665 			adev->nbio.ras = &nbio_v7_4_ras;
2666 		break;
2667 	case IP_VERSION(4, 3, 0):
2668 		if (adev->ras_hw_enabled & (1 << AMDGPU_RAS_BLOCK__DF))
2669 			/* unlike other generation of nbio ras,
2670 			 * nbio v4_3 only support fatal error interrupt
2671 			 * to inform software that DF is freezed due to
2672 			 * system fatal error event. driver should not
2673 			 * enable nbio ras in such case. Instead,
2674 			 * check DF RAS */
2675 			adev->nbio.ras = &nbio_v4_3_ras;
2676 		break;
2677 	case IP_VERSION(7, 9, 0):
2678 		if (!adev->gmc.is_app_apu)
2679 			adev->nbio.ras = &nbio_v7_9_ras;
2680 		break;
2681 	default:
2682 		/* nbio ras is not available */
2683 		break;
2684 	}
2685 
2686 	/* nbio ras block needs to be enabled ahead of other ras blocks
2687 	 * to handle fatal error */
2688 	r = amdgpu_nbio_ras_sw_init(adev);
2689 	if (r)
2690 		return r;
2691 
2692 	if (adev->nbio.ras &&
2693 	    adev->nbio.ras->init_ras_controller_interrupt) {
2694 		r = adev->nbio.ras->init_ras_controller_interrupt(adev);
2695 		if (r)
2696 			goto release_con;
2697 	}
2698 
2699 	if (adev->nbio.ras &&
2700 	    adev->nbio.ras->init_ras_err_event_athub_interrupt) {
2701 		r = adev->nbio.ras->init_ras_err_event_athub_interrupt(adev);
2702 		if (r)
2703 			goto release_con;
2704 	}
2705 
2706 	amdgpu_ras_query_poison_mode(adev);
2707 
2708 	if (amdgpu_ras_fs_init(adev)) {
2709 		r = -EINVAL;
2710 		goto release_con;
2711 	}
2712 
2713 	dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
2714 		 "hardware ability[%x] ras_mask[%x]\n",
2715 		 adev->ras_hw_enabled, adev->ras_enabled);
2716 
2717 	return 0;
2718 release_con:
2719 	amdgpu_ras_set_context(adev, NULL);
2720 	kfree(con);
2721 
2722 	return r;
2723 }
2724 
2725 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev)
2726 {
2727 	if (adev->gmc.xgmi.connected_to_cpu ||
2728 	    adev->gmc.is_app_apu)
2729 		return 1;
2730 	return 0;
2731 }
2732 
2733 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev,
2734 					struct ras_common_if *ras_block)
2735 {
2736 	struct ras_query_if info = {
2737 		.head = *ras_block,
2738 	};
2739 
2740 	if (!amdgpu_persistent_edc_harvesting_supported(adev))
2741 		return 0;
2742 
2743 	if (amdgpu_ras_query_error_status(adev, &info) != 0)
2744 		DRM_WARN("RAS init harvest failure");
2745 
2746 	if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0)
2747 		DRM_WARN("RAS init harvest reset failure");
2748 
2749 	return 0;
2750 }
2751 
2752 bool amdgpu_ras_is_poison_mode_supported(struct amdgpu_device *adev)
2753 {
2754        struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2755 
2756        if (!con)
2757                return false;
2758 
2759        return con->poison_supported;
2760 }
2761 
2762 /* helper function to handle common stuff in ip late init phase */
2763 int amdgpu_ras_block_late_init(struct amdgpu_device *adev,
2764 			 struct ras_common_if *ras_block)
2765 {
2766 	struct amdgpu_ras_block_object *ras_obj = NULL;
2767 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2768 	struct ras_query_if *query_info;
2769 	unsigned long ue_count, ce_count;
2770 	int r;
2771 
2772 	/* disable RAS feature per IP block if it is not supported */
2773 	if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
2774 		amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
2775 		return 0;
2776 	}
2777 
2778 	r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
2779 	if (r) {
2780 		if (adev->in_suspend || amdgpu_in_reset(adev)) {
2781 			/* in resume phase, if fail to enable ras,
2782 			 * clean up all ras fs nodes, and disable ras */
2783 			goto cleanup;
2784 		} else
2785 			return r;
2786 	}
2787 
2788 	/* check for errors on warm reset edc persisant supported ASIC */
2789 	amdgpu_persistent_edc_harvesting(adev, ras_block);
2790 
2791 	/* in resume phase, no need to create ras fs node */
2792 	if (adev->in_suspend || amdgpu_in_reset(adev))
2793 		return 0;
2794 
2795 	ras_obj = container_of(ras_block, struct amdgpu_ras_block_object, ras_comm);
2796 	if (ras_obj->ras_cb || (ras_obj->hw_ops &&
2797 	    (ras_obj->hw_ops->query_poison_status ||
2798 	    ras_obj->hw_ops->handle_poison_consumption))) {
2799 		r = amdgpu_ras_interrupt_add_handler(adev, ras_block);
2800 		if (r)
2801 			goto cleanup;
2802 	}
2803 
2804 	if (ras_obj->hw_ops &&
2805 	    (ras_obj->hw_ops->query_ras_error_count ||
2806 	     ras_obj->hw_ops->query_ras_error_status)) {
2807 		r = amdgpu_ras_sysfs_create(adev, ras_block);
2808 		if (r)
2809 			goto interrupt;
2810 
2811 		/* Those are the cached values at init.
2812 		 */
2813 		query_info = kzalloc(sizeof(*query_info), GFP_KERNEL);
2814 		if (!query_info)
2815 			return -ENOMEM;
2816 		memcpy(&query_info->head, ras_block, sizeof(struct ras_common_if));
2817 
2818 		if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count, query_info) == 0) {
2819 			atomic_set(&con->ras_ce_count, ce_count);
2820 			atomic_set(&con->ras_ue_count, ue_count);
2821 		}
2822 
2823 		kfree(query_info);
2824 	}
2825 
2826 	return 0;
2827 
2828 interrupt:
2829 	if (ras_obj->ras_cb)
2830 		amdgpu_ras_interrupt_remove_handler(adev, ras_block);
2831 cleanup:
2832 	amdgpu_ras_feature_enable(adev, ras_block, 0);
2833 	return r;
2834 }
2835 
2836 static int amdgpu_ras_block_late_init_default(struct amdgpu_device *adev,
2837 			 struct ras_common_if *ras_block)
2838 {
2839 	return amdgpu_ras_block_late_init(adev, ras_block);
2840 }
2841 
2842 /* helper function to remove ras fs node and interrupt handler */
2843 void amdgpu_ras_block_late_fini(struct amdgpu_device *adev,
2844 			  struct ras_common_if *ras_block)
2845 {
2846 	struct amdgpu_ras_block_object *ras_obj;
2847 	if (!ras_block)
2848 		return;
2849 
2850 	amdgpu_ras_sysfs_remove(adev, ras_block);
2851 
2852 	ras_obj = container_of(ras_block, struct amdgpu_ras_block_object, ras_comm);
2853 	if (ras_obj->ras_cb)
2854 		amdgpu_ras_interrupt_remove_handler(adev, ras_block);
2855 }
2856 
2857 static void amdgpu_ras_block_late_fini_default(struct amdgpu_device *adev,
2858 			  struct ras_common_if *ras_block)
2859 {
2860 	return amdgpu_ras_block_late_fini(adev, ras_block);
2861 }
2862 
2863 /* do some init work after IP late init as dependence.
2864  * and it runs in resume/gpu reset/booting up cases.
2865  */
2866 void amdgpu_ras_resume(struct amdgpu_device *adev)
2867 {
2868 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2869 	struct ras_manager *obj, *tmp;
2870 
2871 	if (!adev->ras_enabled || !con) {
2872 		/* clean ras context for VEGA20 Gaming after send ras disable cmd */
2873 		amdgpu_release_ras_context(adev);
2874 
2875 		return;
2876 	}
2877 
2878 	if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2879 		/* Set up all other IPs which are not implemented. There is a
2880 		 * tricky thing that IP's actual ras error type should be
2881 		 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2882 		 * ERROR_NONE make sense anyway.
2883 		 */
2884 		amdgpu_ras_enable_all_features(adev, 1);
2885 
2886 		/* We enable ras on all hw_supported block, but as boot
2887 		 * parameter might disable some of them and one or more IP has
2888 		 * not implemented yet. So we disable them on behalf.
2889 		 */
2890 		list_for_each_entry_safe(obj, tmp, &con->head, node) {
2891 			if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2892 				amdgpu_ras_feature_enable(adev, &obj->head, 0);
2893 				/* there should be no any reference. */
2894 				WARN_ON(alive_obj(obj));
2895 			}
2896 		}
2897 	}
2898 }
2899 
2900 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2901 {
2902 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2903 
2904 	if (!adev->ras_enabled || !con)
2905 		return;
2906 
2907 	amdgpu_ras_disable_all_features(adev, 0);
2908 	/* Make sure all ras objects are disabled. */
2909 	if (con->features)
2910 		amdgpu_ras_disable_all_features(adev, 1);
2911 }
2912 
2913 int amdgpu_ras_late_init(struct amdgpu_device *adev)
2914 {
2915 	struct amdgpu_ras_block_list *node, *tmp;
2916 	struct amdgpu_ras_block_object *obj;
2917 	int r;
2918 
2919 	/* Guest side doesn't need init ras feature */
2920 	if (amdgpu_sriov_vf(adev))
2921 		return 0;
2922 
2923 	list_for_each_entry_safe(node, tmp, &adev->ras_list, node) {
2924 		if (!node->ras_obj) {
2925 			dev_warn(adev->dev, "Warning: abnormal ras list node.\n");
2926 			continue;
2927 		}
2928 
2929 		obj = node->ras_obj;
2930 		if (obj->ras_late_init) {
2931 			r = obj->ras_late_init(adev, &obj->ras_comm);
2932 			if (r) {
2933 				dev_err(adev->dev, "%s failed to execute ras_late_init! ret:%d\n",
2934 					obj->ras_comm.name, r);
2935 				return r;
2936 			}
2937 		} else
2938 			amdgpu_ras_block_late_init_default(adev, &obj->ras_comm);
2939 	}
2940 
2941 	return 0;
2942 }
2943 
2944 /* do some fini work before IP fini as dependence */
2945 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2946 {
2947 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2948 
2949 	if (!adev->ras_enabled || !con)
2950 		return 0;
2951 
2952 
2953 	/* Need disable ras on all IPs here before ip [hw/sw]fini */
2954 	if (con->features)
2955 		amdgpu_ras_disable_all_features(adev, 0);
2956 	amdgpu_ras_recovery_fini(adev);
2957 	return 0;
2958 }
2959 
2960 int amdgpu_ras_fini(struct amdgpu_device *adev)
2961 {
2962 	struct amdgpu_ras_block_list *ras_node, *tmp;
2963 	struct amdgpu_ras_block_object *obj = NULL;
2964 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2965 
2966 	if (!adev->ras_enabled || !con)
2967 		return 0;
2968 
2969 	list_for_each_entry_safe(ras_node, tmp, &adev->ras_list, node) {
2970 		if (ras_node->ras_obj) {
2971 			obj = ras_node->ras_obj;
2972 			if (amdgpu_ras_is_supported(adev, obj->ras_comm.block) &&
2973 			    obj->ras_fini)
2974 				obj->ras_fini(adev, &obj->ras_comm);
2975 			else
2976 				amdgpu_ras_block_late_fini_default(adev, &obj->ras_comm);
2977 		}
2978 
2979 		/* Clear ras blocks from ras_list and free ras block list node */
2980 		list_del(&ras_node->node);
2981 		kfree(ras_node);
2982 	}
2983 
2984 	amdgpu_ras_fs_fini(adev);
2985 	amdgpu_ras_interrupt_remove_all(adev);
2986 
2987 	WARN(con->features, "Feature mask is not cleared");
2988 
2989 	if (con->features)
2990 		amdgpu_ras_disable_all_features(adev, 1);
2991 
2992 	cancel_delayed_work_sync(&con->ras_counte_delay_work);
2993 
2994 	amdgpu_ras_set_context(adev, NULL);
2995 	kfree(con);
2996 
2997 	return 0;
2998 }
2999 
3000 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
3001 {
3002 	if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
3003 		struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
3004 
3005 		dev_info(adev->dev, "uncorrectable hardware error"
3006 			"(ERREVENT_ATHUB_INTERRUPT) detected!\n");
3007 
3008 		ras->gpu_reset_flags |= AMDGPU_RAS_GPU_RESET_MODE1_RESET;
3009 		amdgpu_ras_reset_gpu(adev);
3010 	}
3011 }
3012 
3013 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
3014 {
3015 	if (adev->asic_type == CHIP_VEGA20 &&
3016 	    adev->pm.fw_version <= 0x283400) {
3017 		return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
3018 				amdgpu_ras_intr_triggered();
3019 	}
3020 
3021 	return false;
3022 }
3023 
3024 void amdgpu_release_ras_context(struct amdgpu_device *adev)
3025 {
3026 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
3027 
3028 	if (!con)
3029 		return;
3030 
3031 	if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) {
3032 		con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX);
3033 		amdgpu_ras_set_context(adev, NULL);
3034 		kfree(con);
3035 	}
3036 }
3037 
3038 #ifdef CONFIG_X86_MCE_AMD
3039 static struct amdgpu_device *find_adev(uint32_t node_id)
3040 {
3041 	int i;
3042 	struct amdgpu_device *adev = NULL;
3043 
3044 	for (i = 0; i < mce_adev_list.num_gpu; i++) {
3045 		adev = mce_adev_list.devs[i];
3046 
3047 		if (adev && adev->gmc.xgmi.connected_to_cpu &&
3048 		    adev->gmc.xgmi.physical_node_id == node_id)
3049 			break;
3050 		adev = NULL;
3051 	}
3052 
3053 	return adev;
3054 }
3055 
3056 #define GET_MCA_IPID_GPUID(m)	(((m) >> 44) & 0xF)
3057 #define GET_UMC_INST(m)		(((m) >> 21) & 0x7)
3058 #define GET_CHAN_INDEX(m)	((((m) >> 12) & 0x3) | (((m) >> 18) & 0x4))
3059 #define GPU_ID_OFFSET		8
3060 
3061 static int amdgpu_bad_page_notifier(struct notifier_block *nb,
3062 				    unsigned long val, void *data)
3063 {
3064 	struct mce *m = (struct mce *)data;
3065 	struct amdgpu_device *adev = NULL;
3066 	uint32_t gpu_id = 0;
3067 	uint32_t umc_inst = 0, ch_inst = 0;
3068 
3069 	/*
3070 	 * If the error was generated in UMC_V2, which belongs to GPU UMCs,
3071 	 * and error occurred in DramECC (Extended error code = 0) then only
3072 	 * process the error, else bail out.
3073 	 */
3074 	if (!m || !((smca_get_bank_type(m->extcpu, m->bank) == SMCA_UMC_V2) &&
3075 		    (XEC(m->status, 0x3f) == 0x0)))
3076 		return NOTIFY_DONE;
3077 
3078 	/*
3079 	 * If it is correctable error, return.
3080 	 */
3081 	if (mce_is_correctable(m))
3082 		return NOTIFY_OK;
3083 
3084 	/*
3085 	 * GPU Id is offset by GPU_ID_OFFSET in MCA_IPID_UMC register.
3086 	 */
3087 	gpu_id = GET_MCA_IPID_GPUID(m->ipid) - GPU_ID_OFFSET;
3088 
3089 	adev = find_adev(gpu_id);
3090 	if (!adev) {
3091 		DRM_WARN("%s: Unable to find adev for gpu_id: %d\n", __func__,
3092 								gpu_id);
3093 		return NOTIFY_DONE;
3094 	}
3095 
3096 	/*
3097 	 * If it is uncorrectable error, then find out UMC instance and
3098 	 * channel index.
3099 	 */
3100 	umc_inst = GET_UMC_INST(m->ipid);
3101 	ch_inst = GET_CHAN_INDEX(m->ipid);
3102 
3103 	dev_info(adev->dev, "Uncorrectable error detected in UMC inst: %d, chan_idx: %d",
3104 			     umc_inst, ch_inst);
3105 
3106 	if (!amdgpu_umc_page_retirement_mca(adev, m->addr, ch_inst, umc_inst))
3107 		return NOTIFY_OK;
3108 	else
3109 		return NOTIFY_DONE;
3110 }
3111 
3112 static struct notifier_block amdgpu_bad_page_nb = {
3113 	.notifier_call  = amdgpu_bad_page_notifier,
3114 	.priority       = MCE_PRIO_UC,
3115 };
3116 
3117 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev)
3118 {
3119 	/*
3120 	 * Add the adev to the mce_adev_list.
3121 	 * During mode2 reset, amdgpu device is temporarily
3122 	 * removed from the mgpu_info list which can cause
3123 	 * page retirement to fail.
3124 	 * Use this list instead of mgpu_info to find the amdgpu
3125 	 * device on which the UMC error was reported.
3126 	 */
3127 	mce_adev_list.devs[mce_adev_list.num_gpu++] = adev;
3128 
3129 	/*
3130 	 * Register the x86 notifier only once
3131 	 * with MCE subsystem.
3132 	 */
3133 	if (notifier_registered == false) {
3134 		mce_register_decode_chain(&amdgpu_bad_page_nb);
3135 		notifier_registered = true;
3136 	}
3137 }
3138 #endif
3139 
3140 struct amdgpu_ras *amdgpu_ras_get_context(struct amdgpu_device *adev)
3141 {
3142 	if (!adev)
3143 		return NULL;
3144 
3145 	return adev->psp.ras_context.ras;
3146 }
3147 
3148 int amdgpu_ras_set_context(struct amdgpu_device *adev, struct amdgpu_ras *ras_con)
3149 {
3150 	if (!adev)
3151 		return -EINVAL;
3152 
3153 	adev->psp.ras_context.ras = ras_con;
3154 	return 0;
3155 }
3156 
3157 /* check if ras is supported on block, say, sdma, gfx */
3158 int amdgpu_ras_is_supported(struct amdgpu_device *adev,
3159 		unsigned int block)
3160 {
3161 	int ret = 0;
3162 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
3163 
3164 	if (block >= AMDGPU_RAS_BLOCK_COUNT)
3165 		return 0;
3166 
3167 	ret = ras && (adev->ras_enabled & (1 << block));
3168 
3169 	/* For the special asic with mem ecc enabled but sram ecc
3170 	 * not enabled, even if the ras block is not supported on
3171 	 * .ras_enabled, if the asic supports poison mode and the
3172 	 * ras block has ras configuration, it can be considered
3173 	 * that the ras block supports ras function.
3174 	 */
3175 	if (!ret &&
3176 	    (block == AMDGPU_RAS_BLOCK__GFX ||
3177 	     block == AMDGPU_RAS_BLOCK__SDMA ||
3178 	     block == AMDGPU_RAS_BLOCK__VCN ||
3179 	     block == AMDGPU_RAS_BLOCK__JPEG) &&
3180 	    amdgpu_ras_is_poison_mode_supported(adev) &&
3181 	    amdgpu_ras_get_ras_block(adev, block, 0))
3182 		ret = 1;
3183 
3184 	return ret;
3185 }
3186 
3187 int amdgpu_ras_reset_gpu(struct amdgpu_device *adev)
3188 {
3189 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
3190 
3191 	if (atomic_cmpxchg(&ras->in_recovery, 0, 1) == 0)
3192 		amdgpu_reset_domain_schedule(ras->adev->reset_domain, &ras->recovery_work);
3193 	return 0;
3194 }
3195 
3196 
3197 /* Register each ip ras block into amdgpu ras */
3198 int amdgpu_ras_register_ras_block(struct amdgpu_device *adev,
3199 		struct amdgpu_ras_block_object *ras_block_obj)
3200 {
3201 	struct amdgpu_ras_block_list *ras_node;
3202 	if (!adev || !ras_block_obj)
3203 		return -EINVAL;
3204 
3205 	ras_node = kzalloc(sizeof(*ras_node), GFP_KERNEL);
3206 	if (!ras_node)
3207 		return -ENOMEM;
3208 
3209 	INIT_LIST_HEAD(&ras_node->node);
3210 	ras_node->ras_obj = ras_block_obj;
3211 	list_add_tail(&ras_node->node, &adev->ras_list);
3212 
3213 	return 0;
3214 }
3215 
3216 void amdgpu_ras_get_error_type_name(uint32_t err_type, char *err_type_name)
3217 {
3218 	if (!err_type_name)
3219 		return;
3220 
3221 	switch (err_type) {
3222 	case AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE:
3223 		snprintf(err_type_name, 16, "correctable");
3224 		break;
3225 	case AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE:
3226 		snprintf(err_type_name, 16, "uncorrectable");
3227 		break;
3228 	default:
3229 		snprintf(err_type_name, 16, "unknown");
3230 		break;
3231 	}
3232 }
3233 
3234 bool amdgpu_ras_inst_get_memory_id_field(struct amdgpu_device *adev,
3235 					 const struct amdgpu_ras_err_status_reg_entry *reg_entry,
3236 					 uint32_t instance,
3237 					 uint32_t *memory_id)
3238 {
3239 	uint32_t err_status_lo_data, err_status_lo_offset;
3240 
3241 	if (!reg_entry)
3242 		return false;
3243 
3244 	err_status_lo_offset =
3245 		AMDGPU_RAS_REG_ENTRY_OFFSET(reg_entry->hwip, instance,
3246 					    reg_entry->seg_lo, reg_entry->reg_lo);
3247 	err_status_lo_data = RREG32(err_status_lo_offset);
3248 
3249 	if ((reg_entry->flags & AMDGPU_RAS_ERR_STATUS_VALID) &&
3250 	    !REG_GET_FIELD(err_status_lo_data, ERR_STATUS_LO, ERR_STATUS_VALID_FLAG))
3251 		return false;
3252 
3253 	*memory_id = REG_GET_FIELD(err_status_lo_data, ERR_STATUS_LO, MEMORY_ID);
3254 
3255 	return true;
3256 }
3257 
3258 bool amdgpu_ras_inst_get_err_cnt_field(struct amdgpu_device *adev,
3259 				       const struct amdgpu_ras_err_status_reg_entry *reg_entry,
3260 				       uint32_t instance,
3261 				       unsigned long *err_cnt)
3262 {
3263 	uint32_t err_status_hi_data, err_status_hi_offset;
3264 
3265 	if (!reg_entry)
3266 		return false;
3267 
3268 	err_status_hi_offset =
3269 		AMDGPU_RAS_REG_ENTRY_OFFSET(reg_entry->hwip, instance,
3270 					    reg_entry->seg_hi, reg_entry->reg_hi);
3271 	err_status_hi_data = RREG32(err_status_hi_offset);
3272 
3273 	if ((reg_entry->flags & AMDGPU_RAS_ERR_INFO_VALID) &&
3274 	    !REG_GET_FIELD(err_status_hi_data, ERR_STATUS_HI, ERR_INFO_VALID_FLAG))
3275 		/* keep the check here in case we need to refer to the result later */
3276 		dev_dbg(adev->dev, "Invalid err_info field\n");
3277 
3278 	/* read err count */
3279 	*err_cnt = REG_GET_FIELD(err_status_hi_data, ERR_STATUS, ERR_CNT);
3280 
3281 	return true;
3282 }
3283 
3284 void amdgpu_ras_inst_query_ras_error_count(struct amdgpu_device *adev,
3285 					   const struct amdgpu_ras_err_status_reg_entry *reg_list,
3286 					   uint32_t reg_list_size,
3287 					   const struct amdgpu_ras_memory_id_entry *mem_list,
3288 					   uint32_t mem_list_size,
3289 					   uint32_t instance,
3290 					   uint32_t err_type,
3291 					   unsigned long *err_count)
3292 {
3293 	uint32_t memory_id;
3294 	unsigned long err_cnt;
3295 	char err_type_name[16];
3296 	uint32_t i, j;
3297 
3298 	for (i = 0; i < reg_list_size; i++) {
3299 		/* query memory_id from err_status_lo */
3300 		if (!amdgpu_ras_inst_get_memory_id_field(adev, &reg_list[i],
3301 							 instance, &memory_id))
3302 			continue;
3303 
3304 		/* query err_cnt from err_status_hi */
3305 		if (!amdgpu_ras_inst_get_err_cnt_field(adev, &reg_list[i],
3306 						       instance, &err_cnt) ||
3307 		    !err_cnt)
3308 			continue;
3309 
3310 		*err_count += err_cnt;
3311 
3312 		/* log the errors */
3313 		amdgpu_ras_get_error_type_name(err_type, err_type_name);
3314 		if (!mem_list) {
3315 			/* memory_list is not supported */
3316 			dev_info(adev->dev,
3317 				 "%ld %s hardware errors detected in %s, instance: %d, memory_id: %d\n",
3318 				 err_cnt, err_type_name,
3319 				 reg_list[i].block_name,
3320 				 instance, memory_id);
3321 		} else {
3322 			for (j = 0; j < mem_list_size; j++) {
3323 				if (memory_id == mem_list[j].memory_id) {
3324 					dev_info(adev->dev,
3325 						 "%ld %s hardware errors detected in %s, instance: %d, memory block: %s\n",
3326 						 err_cnt, err_type_name,
3327 						 reg_list[i].block_name,
3328 						 instance, mem_list[j].name);
3329 					break;
3330 				}
3331 			}
3332 		}
3333 	}
3334 }
3335 
3336 void amdgpu_ras_inst_reset_ras_error_count(struct amdgpu_device *adev,
3337 					   const struct amdgpu_ras_err_status_reg_entry *reg_list,
3338 					   uint32_t reg_list_size,
3339 					   uint32_t instance)
3340 {
3341 	uint32_t err_status_lo_offset, err_status_hi_offset;
3342 	uint32_t i;
3343 
3344 	for (i = 0; i < reg_list_size; i++) {
3345 		err_status_lo_offset =
3346 			AMDGPU_RAS_REG_ENTRY_OFFSET(reg_list[i].hwip, instance,
3347 						    reg_list[i].seg_lo, reg_list[i].reg_lo);
3348 		err_status_hi_offset =
3349 			AMDGPU_RAS_REG_ENTRY_OFFSET(reg_list[i].hwip, instance,
3350 						    reg_list[i].seg_hi, reg_list[i].reg_hi);
3351 		WREG32(err_status_lo_offset, 0);
3352 		WREG32(err_status_hi_offset, 0);
3353 	}
3354 }
3355