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