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