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