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