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