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