2 * Copyright 2018 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/debugfs.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/uaccess.h>
28 #include <linux/reboot.h>
29 #include <linux/syscalls.h>
30 #include <linux/pm_runtime.h>
33 #include "amdgpu_ras.h"
34 #include "amdgpu_atomfirmware.h"
35 #include "amdgpu_xgmi.h"
36 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h"
38 #ifdef CONFIG_X86_MCE_AMD
41 static bool notifier_registered;
43 static const char *RAS_FS_NAME = "ras";
45 const char *ras_error_string[] = {
49 "multi_uncorrectable",
53 const char *ras_block_string[] = {
71 const char *ras_mca_block_string[] = {
78 const char *get_ras_block_str(struct ras_common_if *ras_block)
83 if (ras_block->block >= AMDGPU_RAS_BLOCK_COUNT)
84 return "OUT OF RANGE";
86 if (ras_block->block == AMDGPU_RAS_BLOCK__MCA)
87 return ras_mca_block_string[ras_block->sub_block_index];
89 return ras_block_string[ras_block->block];
92 #define ras_err_str(i) (ras_error_string[ffs(i)])
94 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
96 /* inject address is 52 bits */
97 #define RAS_UMC_INJECT_ADDR_LIMIT (0x1ULL << 52)
99 /* typical ECC bad page rate is 1 bad page per 100MB VRAM */
100 #define RAS_BAD_PAGE_COVER (100 * 1024 * 1024ULL)
102 enum amdgpu_ras_retire_page_reservation {
103 AMDGPU_RAS_RETIRE_PAGE_RESERVED,
104 AMDGPU_RAS_RETIRE_PAGE_PENDING,
105 AMDGPU_RAS_RETIRE_PAGE_FAULT,
108 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
110 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
112 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
114 #ifdef CONFIG_X86_MCE_AMD
115 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev);
116 struct mce_notifier_adev_list {
117 struct amdgpu_device *devs[MAX_GPU_INSTANCE];
120 static struct mce_notifier_adev_list mce_adev_list;
123 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
125 if (adev && amdgpu_ras_get_context(adev))
126 amdgpu_ras_get_context(adev)->error_query_ready = ready;
129 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
131 if (adev && amdgpu_ras_get_context(adev))
132 return amdgpu_ras_get_context(adev)->error_query_ready;
137 static int amdgpu_reserve_page_direct(struct amdgpu_device *adev, uint64_t address)
139 struct ras_err_data err_data = {0, 0, 0, NULL};
140 struct eeprom_table_record err_rec;
142 if ((address >= adev->gmc.mc_vram_size) ||
143 (address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
145 "RAS WARN: input address 0x%llx is invalid.\n",
150 if (amdgpu_ras_check_bad_page(adev, address)) {
152 "RAS WARN: 0x%llx has already been marked as bad page!\n",
157 memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
159 err_rec.address = address;
160 err_rec.retired_page = address >> AMDGPU_GPU_PAGE_SHIFT;
161 err_rec.ts = (uint64_t)ktime_get_real_seconds();
162 err_rec.err_type = AMDGPU_RAS_EEPROM_ERR_NON_RECOVERABLE;
164 err_data.err_addr = &err_rec;
165 err_data.err_addr_cnt = 1;
167 if (amdgpu_bad_page_threshold != 0) {
168 amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
169 err_data.err_addr_cnt);
170 amdgpu_ras_save_bad_pages(adev);
173 dev_warn(adev->dev, "WARNING: THIS IS ONLY FOR TEST PURPOSES AND WILL CORRUPT RAS EEPROM\n");
174 dev_warn(adev->dev, "Clear EEPROM:\n");
175 dev_warn(adev->dev, " echo 1 > /sys/kernel/debug/dri/0/ras/ras_eeprom_reset\n");
180 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
181 size_t size, loff_t *pos)
183 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
184 struct ras_query_if info = {
190 if (amdgpu_ras_query_error_status(obj->adev, &info))
193 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
195 "ce", info.ce_count);
200 s = min_t(u64, s, size);
203 if (copy_to_user(buf, &val[*pos], s))
211 static const struct file_operations amdgpu_ras_debugfs_ops = {
212 .owner = THIS_MODULE,
213 .read = amdgpu_ras_debugfs_read,
215 .llseek = default_llseek
218 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
222 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
224 if (strcmp(name, ras_block_string[i]) == 0)
230 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
231 const char __user *buf, size_t size,
232 loff_t *pos, struct ras_debug_if *data)
234 ssize_t s = min_t(u64, 64, size);
247 memset(str, 0, sizeof(str));
248 memset(data, 0, sizeof(*data));
250 if (copy_from_user(str, buf, s))
253 if (sscanf(str, "disable %32s", block_name) == 1)
255 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
257 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
259 else if (strstr(str, "retire_page") != NULL)
261 else if (str[0] && str[1] && str[2] && str[3])
262 /* ascii string, but commands are not matched. */
267 if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
268 sscanf(str, "%*s %llu", &address) != 1)
272 data->inject.address = address;
277 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
280 data->head.block = block_id;
281 /* only ue and ce errors are supported */
282 if (!memcmp("ue", err, 2))
283 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
284 else if (!memcmp("ce", err, 2))
285 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
292 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
293 &sub_block, &address, &value) != 3 &&
294 sscanf(str, "%*s %*s %*s %u %llu %llu",
295 &sub_block, &address, &value) != 3)
297 data->head.sub_block_index = sub_block;
298 data->inject.address = address;
299 data->inject.value = value;
302 if (size < sizeof(*data))
305 if (copy_from_user(data, buf, sizeof(*data)))
313 * DOC: AMDGPU RAS debugfs control interface
315 * The control interface accepts struct ras_debug_if which has two members.
317 * First member: ras_debug_if::head or ras_debug_if::inject.
319 * head is used to indicate which IP block will be under control.
321 * head has four members, they are block, type, sub_block_index, name.
322 * block: which IP will be under control.
323 * type: what kind of error will be enabled/disabled/injected.
324 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
325 * name: the name of IP.
327 * inject has two more members than head, they are address, value.
328 * As their names indicate, inject operation will write the
329 * value to the address.
331 * The second member: struct ras_debug_if::op.
332 * It has three kinds of operations.
334 * - 0: disable RAS on the block. Take ::head as its data.
335 * - 1: enable RAS on the block. Take ::head as its data.
336 * - 2: inject errors on the block. Take ::inject as its data.
338 * How to use the interface?
342 * Copy the struct ras_debug_if in your code and initialize it.
343 * Write the struct to the control interface.
347 * .. code-block:: bash
349 * echo "disable <block>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
350 * echo "enable <block> <error>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
351 * echo "inject <block> <error> <sub-block> <address> <value> > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
353 * Where N, is the card which you want to affect.
355 * "disable" requires only the block.
356 * "enable" requires the block and error type.
357 * "inject" requires the block, error type, address, and value.
359 * The block is one of: umc, sdma, gfx, etc.
360 * see ras_block_string[] for details
362 * The error type is one of: ue, ce, where,
363 * ue is multi-uncorrectable
364 * ce is single-correctable
366 * The sub-block is a the sub-block index, pass 0 if there is no sub-block.
367 * The address and value are hexadecimal numbers, leading 0x is optional.
371 * .. code-block:: bash
373 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
374 * echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
375 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
377 * How to check the result of the operation?
379 * To check disable/enable, see "ras" features at,
380 * /sys/class/drm/card[0/1/2...]/device/ras/features
382 * To check inject, see the corresponding error count at,
383 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx|sdma|umc|...]_err_count
386 * Operations are only allowed on blocks which are supported.
387 * Check the "ras" mask at /sys/module/amdgpu/parameters/ras_mask
388 * to see which blocks support RAS on a particular asic.
391 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f,
392 const char __user *buf,
393 size_t size, loff_t *pos)
395 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
396 struct ras_debug_if data;
399 if (!amdgpu_ras_get_error_query_ready(adev)) {
400 dev_warn(adev->dev, "RAS WARN: error injection "
401 "currently inaccessible\n");
405 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
410 ret = amdgpu_reserve_page_direct(adev, data.inject.address);
417 if (!amdgpu_ras_is_supported(adev, data.head.block))
422 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
425 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
428 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
429 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
430 dev_warn(adev->dev, "RAS WARN: input address "
431 "0x%llx is invalid.",
432 data.inject.address);
437 /* umc ce/ue error injection for a bad page is not allowed */
438 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
439 amdgpu_ras_check_bad_page(adev, data.inject.address)) {
440 dev_warn(adev->dev, "RAS WARN: inject: 0x%llx has "
441 "already been marked as bad!\n",
442 data.inject.address);
446 /* data.inject.address is offset instead of absolute gpu address */
447 ret = amdgpu_ras_error_inject(adev, &data.inject);
461 * DOC: AMDGPU RAS debugfs EEPROM table reset interface
463 * Some boards contain an EEPROM which is used to persistently store a list of
464 * bad pages which experiences ECC errors in vram. This interface provides
465 * a way to reset the EEPROM, e.g., after testing error injection.
469 * .. code-block:: bash
471 * echo 1 > ../ras/ras_eeprom_reset
473 * will reset EEPROM table to 0 entries.
476 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f,
477 const char __user *buf,
478 size_t size, loff_t *pos)
480 struct amdgpu_device *adev =
481 (struct amdgpu_device *)file_inode(f)->i_private;
484 ret = amdgpu_ras_eeprom_reset_table(
485 &(amdgpu_ras_get_context(adev)->eeprom_control));
488 /* Something was written to EEPROM.
490 amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS;
497 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
498 .owner = THIS_MODULE,
500 .write = amdgpu_ras_debugfs_ctrl_write,
501 .llseek = default_llseek
504 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
505 .owner = THIS_MODULE,
507 .write = amdgpu_ras_debugfs_eeprom_write,
508 .llseek = default_llseek
512 * DOC: AMDGPU RAS sysfs Error Count Interface
514 * It allows the user to read the error count for each IP block on the gpu through
515 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
517 * It outputs the multiple lines which report the uncorrected (ue) and corrected
520 * The format of one line is below,
526 * .. code-block:: bash
532 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
536 struct ras_query_if info = {
540 if (!amdgpu_ras_get_error_query_ready(obj->adev))
541 return sysfs_emit(buf, "Query currently inaccessible\n");
543 if (amdgpu_ras_query_error_status(obj->adev, &info))
546 if (obj->adev->asic_type == CHIP_ALDEBARAN) {
547 if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
548 DRM_WARN("Failed to reset error counter and error status");
551 return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count,
552 "ce", info.ce_count);
557 #define get_obj(obj) do { (obj)->use++; } while (0)
558 #define alive_obj(obj) ((obj)->use)
560 static inline void put_obj(struct ras_manager *obj)
562 if (obj && (--obj->use == 0))
563 list_del(&obj->node);
564 if (obj && (obj->use < 0))
565 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", get_ras_block_str(&obj->head));
568 /* make one obj and return it. */
569 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
570 struct ras_common_if *head)
572 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
573 struct ras_manager *obj;
575 if (!adev->ras_enabled || !con)
578 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
581 if (head->block == AMDGPU_RAS_BLOCK__MCA) {
582 if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
585 obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
587 obj = &con->objs[head->block];
589 /* already exist. return obj? */
595 list_add(&obj->node, &con->head);
601 /* return an obj equal to head, or the first when head is NULL */
602 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
603 struct ras_common_if *head)
605 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
606 struct ras_manager *obj;
609 if (!adev->ras_enabled || !con)
613 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
616 if (head->block == AMDGPU_RAS_BLOCK__MCA) {
617 if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
620 obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
622 obj = &con->objs[head->block];
627 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT + AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
638 /* feature ctl begin */
639 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
640 struct ras_common_if *head)
642 return adev->ras_hw_enabled & BIT(head->block);
645 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
646 struct ras_common_if *head)
648 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
650 return con->features & BIT(head->block);
654 * if obj is not created, then create one.
655 * set feature enable flag.
657 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
658 struct ras_common_if *head, int enable)
660 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
661 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
663 /* If hardware does not support ras, then do not create obj.
664 * But if hardware support ras, we can create the obj.
665 * Ras framework checks con->hw_supported to see if it need do
666 * corresponding initialization.
667 * IP checks con->support to see if it need disable ras.
669 if (!amdgpu_ras_is_feature_allowed(adev, head))
674 obj = amdgpu_ras_create_obj(adev, head);
678 /* In case we create obj somewhere else */
681 con->features |= BIT(head->block);
683 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
684 con->features &= ~BIT(head->block);
692 /* wrapper of psp_ras_enable_features */
693 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
694 struct ras_common_if *head, bool enable)
696 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
697 union ta_ras_cmd_input *info;
703 info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
708 info->disable_features = (struct ta_ras_disable_features_input) {
709 .block_id = amdgpu_ras_block_to_ta(head->block),
710 .error_type = amdgpu_ras_error_to_ta(head->type),
713 info->enable_features = (struct ta_ras_enable_features_input) {
714 .block_id = amdgpu_ras_block_to_ta(head->block),
715 .error_type = amdgpu_ras_error_to_ta(head->type),
719 /* Do not enable if it is not allowed. */
720 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
722 if (!amdgpu_ras_intr_triggered()) {
723 ret = psp_ras_enable_features(&adev->psp, info, enable);
725 dev_err(adev->dev, "ras %s %s failed poison:%d ret:%d\n",
726 enable ? "enable":"disable",
727 get_ras_block_str(head),
728 amdgpu_ras_is_poison_mode_supported(adev), ret);
734 __amdgpu_ras_feature_enable(adev, head, enable);
741 /* Only used in device probe stage and called only once. */
742 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
743 struct ras_common_if *head, bool enable)
745 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
751 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
753 /* There is no harm to issue a ras TA cmd regardless of
754 * the currecnt ras state.
755 * If current state == target state, it will do nothing
756 * But sometimes it requests driver to reset and repost
757 * with error code -EAGAIN.
759 ret = amdgpu_ras_feature_enable(adev, head, 1);
760 /* With old ras TA, we might fail to enable ras.
761 * Log it and just setup the object.
762 * TODO need remove this WA in the future.
764 if (ret == -EINVAL) {
765 ret = __amdgpu_ras_feature_enable(adev, head, 1);
768 "RAS INFO: %s setup object\n",
769 get_ras_block_str(head));
772 /* setup the object then issue a ras TA disable cmd.*/
773 ret = __amdgpu_ras_feature_enable(adev, head, 1);
777 /* gfx block ras dsiable cmd must send to ras-ta */
778 if (head->block == AMDGPU_RAS_BLOCK__GFX)
779 con->features |= BIT(head->block);
781 ret = amdgpu_ras_feature_enable(adev, head, 0);
783 /* clean gfx block ras features flag */
784 if (adev->ras_enabled && head->block == AMDGPU_RAS_BLOCK__GFX)
785 con->features &= ~BIT(head->block);
788 ret = amdgpu_ras_feature_enable(adev, head, enable);
793 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
796 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
797 struct ras_manager *obj, *tmp;
799 list_for_each_entry_safe(obj, tmp, &con->head, node) {
801 * aka just release the obj and corresponding flags
804 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
807 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
812 return con->features;
815 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
818 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
820 const enum amdgpu_ras_error_type default_ras_type = AMDGPU_RAS_ERROR__NONE;
822 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
823 struct ras_common_if head = {
825 .type = default_ras_type,
826 .sub_block_index = 0,
829 if (i == AMDGPU_RAS_BLOCK__MCA)
834 * bypass psp. vbios enable ras for us.
835 * so just create the obj
837 if (__amdgpu_ras_feature_enable(adev, &head, 1))
840 if (amdgpu_ras_feature_enable(adev, &head, 1))
845 for (i = 0; i < AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
846 struct ras_common_if head = {
847 .block = AMDGPU_RAS_BLOCK__MCA,
848 .type = default_ras_type,
849 .sub_block_index = i,
854 * bypass psp. vbios enable ras for us.
855 * so just create the obj
857 if (__amdgpu_ras_feature_enable(adev, &head, 1))
860 if (amdgpu_ras_feature_enable(adev, &head, 1))
865 return con->features;
867 /* feature ctl end */
870 static void amdgpu_ras_mca_query_error_status(struct amdgpu_device *adev,
871 struct ras_common_if *ras_block,
872 struct ras_err_data *err_data)
874 switch (ras_block->sub_block_index) {
875 case AMDGPU_RAS_MCA_BLOCK__MP0:
876 if (adev->mca.mp0.ras_funcs &&
877 adev->mca.mp0.ras_funcs->query_ras_error_count)
878 adev->mca.mp0.ras_funcs->query_ras_error_count(adev, &err_data);
880 case AMDGPU_RAS_MCA_BLOCK__MP1:
881 if (adev->mca.mp1.ras_funcs &&
882 adev->mca.mp1.ras_funcs->query_ras_error_count)
883 adev->mca.mp1.ras_funcs->query_ras_error_count(adev, &err_data);
885 case AMDGPU_RAS_MCA_BLOCK__MPIO:
886 if (adev->mca.mpio.ras_funcs &&
887 adev->mca.mpio.ras_funcs->query_ras_error_count)
888 adev->mca.mpio.ras_funcs->query_ras_error_count(adev, &err_data);
895 static void amdgpu_ras_get_ecc_info(struct amdgpu_device *adev, struct ras_err_data *err_data)
897 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
901 * choosing right query method according to
902 * whether smu support query error information
904 ret = smu_get_ecc_info(&adev->smu, (void *)&(ras->umc_ecc));
905 if (ret == -EOPNOTSUPP) {
906 if (adev->umc.ras_funcs &&
907 adev->umc.ras_funcs->query_ras_error_count)
908 adev->umc.ras_funcs->query_ras_error_count(adev, err_data);
910 /* umc query_ras_error_address is also responsible for clearing
913 if (adev->umc.ras_funcs &&
914 adev->umc.ras_funcs->query_ras_error_address)
915 adev->umc.ras_funcs->query_ras_error_address(adev, err_data);
917 if (adev->umc.ras_funcs &&
918 adev->umc.ras_funcs->ecc_info_query_ras_error_count)
919 adev->umc.ras_funcs->ecc_info_query_ras_error_count(adev, err_data);
921 if (adev->umc.ras_funcs &&
922 adev->umc.ras_funcs->ecc_info_query_ras_error_address)
923 adev->umc.ras_funcs->ecc_info_query_ras_error_address(adev, err_data);
927 /* query/inject/cure begin */
928 int amdgpu_ras_query_error_status(struct amdgpu_device *adev,
929 struct ras_query_if *info)
931 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
932 struct ras_err_data err_data = {0, 0, 0, NULL};
938 switch (info->head.block) {
939 case AMDGPU_RAS_BLOCK__UMC:
940 amdgpu_ras_get_ecc_info(adev, &err_data);
942 case AMDGPU_RAS_BLOCK__SDMA:
943 if (adev->sdma.funcs->query_ras_error_count) {
944 for (i = 0; i < adev->sdma.num_instances; i++)
945 adev->sdma.funcs->query_ras_error_count(adev, i,
949 case AMDGPU_RAS_BLOCK__GFX:
950 if (adev->gfx.ras_funcs &&
951 adev->gfx.ras_funcs->query_ras_error_count)
952 adev->gfx.ras_funcs->query_ras_error_count(adev, &err_data);
954 if (adev->gfx.ras_funcs &&
955 adev->gfx.ras_funcs->query_ras_error_status)
956 adev->gfx.ras_funcs->query_ras_error_status(adev);
958 case AMDGPU_RAS_BLOCK__MMHUB:
959 if (adev->mmhub.ras_funcs &&
960 adev->mmhub.ras_funcs->query_ras_error_count)
961 adev->mmhub.ras_funcs->query_ras_error_count(adev, &err_data);
963 if (adev->mmhub.ras_funcs &&
964 adev->mmhub.ras_funcs->query_ras_error_status)
965 adev->mmhub.ras_funcs->query_ras_error_status(adev);
967 case AMDGPU_RAS_BLOCK__PCIE_BIF:
968 if (adev->nbio.ras_funcs &&
969 adev->nbio.ras_funcs->query_ras_error_count)
970 adev->nbio.ras_funcs->query_ras_error_count(adev, &err_data);
972 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
973 if (adev->gmc.xgmi.ras_funcs &&
974 adev->gmc.xgmi.ras_funcs->query_ras_error_count)
975 adev->gmc.xgmi.ras_funcs->query_ras_error_count(adev, &err_data);
977 case AMDGPU_RAS_BLOCK__HDP:
978 if (adev->hdp.ras_funcs &&
979 adev->hdp.ras_funcs->query_ras_error_count)
980 adev->hdp.ras_funcs->query_ras_error_count(adev, &err_data);
982 case AMDGPU_RAS_BLOCK__MCA:
983 amdgpu_ras_mca_query_error_status(adev, &info->head, &err_data);
989 obj->err_data.ue_count += err_data.ue_count;
990 obj->err_data.ce_count += err_data.ce_count;
992 info->ue_count = obj->err_data.ue_count;
993 info->ce_count = obj->err_data.ce_count;
995 if (err_data.ce_count) {
996 if (adev->smuio.funcs &&
997 adev->smuio.funcs->get_socket_id &&
998 adev->smuio.funcs->get_die_id) {
999 dev_info(adev->dev, "socket: %d, die: %d "
1000 "%ld correctable hardware errors "
1001 "detected in %s block, no user "
1002 "action is needed.\n",
1003 adev->smuio.funcs->get_socket_id(adev),
1004 adev->smuio.funcs->get_die_id(adev),
1005 obj->err_data.ce_count,
1006 get_ras_block_str(&info->head));
1008 dev_info(adev->dev, "%ld correctable hardware errors "
1009 "detected in %s block, no user "
1010 "action is needed.\n",
1011 obj->err_data.ce_count,
1012 get_ras_block_str(&info->head));
1015 if (err_data.ue_count) {
1016 if (adev->smuio.funcs &&
1017 adev->smuio.funcs->get_socket_id &&
1018 adev->smuio.funcs->get_die_id) {
1019 dev_info(adev->dev, "socket: %d, die: %d "
1020 "%ld uncorrectable hardware errors "
1021 "detected in %s block\n",
1022 adev->smuio.funcs->get_socket_id(adev),
1023 adev->smuio.funcs->get_die_id(adev),
1024 obj->err_data.ue_count,
1025 get_ras_block_str(&info->head));
1027 dev_info(adev->dev, "%ld uncorrectable hardware errors "
1028 "detected in %s block\n",
1029 obj->err_data.ue_count,
1030 get_ras_block_str(&info->head));
1034 if (!amdgpu_persistent_edc_harvesting_supported(adev))
1035 amdgpu_ras_reset_error_status(adev, info->head.block);
1040 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev,
1041 enum amdgpu_ras_block block)
1043 if (!amdgpu_ras_is_supported(adev, block))
1047 case AMDGPU_RAS_BLOCK__GFX:
1048 if (adev->gfx.ras_funcs &&
1049 adev->gfx.ras_funcs->reset_ras_error_count)
1050 adev->gfx.ras_funcs->reset_ras_error_count(adev);
1052 if (adev->gfx.ras_funcs &&
1053 adev->gfx.ras_funcs->reset_ras_error_status)
1054 adev->gfx.ras_funcs->reset_ras_error_status(adev);
1056 case AMDGPU_RAS_BLOCK__MMHUB:
1057 if (adev->mmhub.ras_funcs &&
1058 adev->mmhub.ras_funcs->reset_ras_error_count)
1059 adev->mmhub.ras_funcs->reset_ras_error_count(adev);
1061 if (adev->mmhub.ras_funcs &&
1062 adev->mmhub.ras_funcs->reset_ras_error_status)
1063 adev->mmhub.ras_funcs->reset_ras_error_status(adev);
1065 case AMDGPU_RAS_BLOCK__SDMA:
1066 if (adev->sdma.funcs->reset_ras_error_count)
1067 adev->sdma.funcs->reset_ras_error_count(adev);
1069 case AMDGPU_RAS_BLOCK__HDP:
1070 if (adev->hdp.ras_funcs &&
1071 adev->hdp.ras_funcs->reset_ras_error_count)
1072 adev->hdp.ras_funcs->reset_ras_error_count(adev);
1081 /* Trigger XGMI/WAFL error */
1082 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev,
1083 struct ta_ras_trigger_error_input *block_info)
1087 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
1088 dev_warn(adev->dev, "Failed to disallow df cstate");
1090 if (amdgpu_dpm_allow_xgmi_power_down(adev, false))
1091 dev_warn(adev->dev, "Failed to disallow XGMI power down");
1093 ret = psp_ras_trigger_error(&adev->psp, block_info);
1095 if (amdgpu_ras_intr_triggered())
1098 if (amdgpu_dpm_allow_xgmi_power_down(adev, true))
1099 dev_warn(adev->dev, "Failed to allow XGMI power down");
1101 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW))
1102 dev_warn(adev->dev, "Failed to allow df cstate");
1107 /* wrapper of psp_ras_trigger_error */
1108 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
1109 struct ras_inject_if *info)
1111 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1112 struct ta_ras_trigger_error_input block_info = {
1113 .block_id = amdgpu_ras_block_to_ta(info->head.block),
1114 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
1115 .sub_block_index = info->head.sub_block_index,
1116 .address = info->address,
1117 .value = info->value,
1124 /* Calculate XGMI relative offset */
1125 if (adev->gmc.xgmi.num_physical_nodes > 1) {
1126 block_info.address =
1127 amdgpu_xgmi_get_relative_phy_addr(adev,
1128 block_info.address);
1131 switch (info->head.block) {
1132 case AMDGPU_RAS_BLOCK__GFX:
1133 if (adev->gfx.ras_funcs &&
1134 adev->gfx.ras_funcs->ras_error_inject)
1135 ret = adev->gfx.ras_funcs->ras_error_inject(adev, info);
1139 case AMDGPU_RAS_BLOCK__UMC:
1140 case AMDGPU_RAS_BLOCK__SDMA:
1141 case AMDGPU_RAS_BLOCK__MMHUB:
1142 case AMDGPU_RAS_BLOCK__PCIE_BIF:
1143 case AMDGPU_RAS_BLOCK__MCA:
1144 ret = psp_ras_trigger_error(&adev->psp, &block_info);
1146 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
1147 ret = amdgpu_ras_error_inject_xgmi(adev, &block_info);
1150 dev_info(adev->dev, "%s error injection is not supported yet\n",
1151 get_ras_block_str(&info->head));
1156 dev_err(adev->dev, "ras inject %s failed %d\n",
1157 get_ras_block_str(&info->head), ret);
1163 * amdgpu_ras_query_error_count -- Get error counts of all IPs
1164 * @adev: pointer to AMD GPU device
1165 * @ce_count: pointer to an integer to be set to the count of correctible errors.
1166 * @ue_count: pointer to an integer to be set to the count of uncorrectible
1169 * If set, @ce_count or @ue_count, count and return the corresponding
1170 * error counts in those integer pointers. Return 0 if the device
1171 * supports RAS. Return -EOPNOTSUPP if the device doesn't support RAS.
1173 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
1174 unsigned long *ce_count,
1175 unsigned long *ue_count)
1177 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1178 struct ras_manager *obj;
1179 unsigned long ce, ue;
1181 if (!adev->ras_enabled || !con)
1184 /* Don't count since no reporting.
1186 if (!ce_count && !ue_count)
1191 list_for_each_entry(obj, &con->head, node) {
1192 struct ras_query_if info = {
1197 res = amdgpu_ras_query_error_status(adev, &info);
1201 ce += info.ce_count;
1202 ue += info.ue_count;
1213 /* query/inject/cure end */
1218 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1219 struct ras_badpage **bps, unsigned int *count);
1221 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
1224 case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
1226 case AMDGPU_RAS_RETIRE_PAGE_PENDING:
1228 case AMDGPU_RAS_RETIRE_PAGE_FAULT:
1235 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
1237 * It allows user to read the bad pages of vram on the gpu through
1238 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
1240 * It outputs multiple lines, and each line stands for one gpu page.
1242 * The format of one line is below,
1243 * gpu pfn : gpu page size : flags
1245 * gpu pfn and gpu page size are printed in hex format.
1246 * flags can be one of below character,
1248 * R: reserved, this gpu page is reserved and not able to use.
1250 * P: pending for reserve, this gpu page is marked as bad, will be reserved
1251 * in next window of page_reserve.
1253 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
1257 * .. code-block:: bash
1259 * 0x00000001 : 0x00001000 : R
1260 * 0x00000002 : 0x00001000 : P
1264 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
1265 struct kobject *kobj, struct bin_attribute *attr,
1266 char *buf, loff_t ppos, size_t count)
1268 struct amdgpu_ras *con =
1269 container_of(attr, struct amdgpu_ras, badpages_attr);
1270 struct amdgpu_device *adev = con->adev;
1271 const unsigned int element_size =
1272 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
1273 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
1274 unsigned int end = div64_ul(ppos + count - 1, element_size);
1276 struct ras_badpage *bps = NULL;
1277 unsigned int bps_count = 0;
1279 memset(buf, 0, count);
1281 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1284 for (; start < end && start < bps_count; start++)
1285 s += scnprintf(&buf[s], element_size + 1,
1286 "0x%08x : 0x%08x : %1s\n",
1289 amdgpu_ras_badpage_flags_str(bps[start].flags));
1296 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1297 struct device_attribute *attr, char *buf)
1299 struct amdgpu_ras *con =
1300 container_of(attr, struct amdgpu_ras, features_attr);
1302 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
1305 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1307 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1309 sysfs_remove_file_from_group(&adev->dev->kobj,
1310 &con->badpages_attr.attr,
1314 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1316 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1317 struct attribute *attrs[] = {
1318 &con->features_attr.attr,
1321 struct attribute_group group = {
1322 .name = RAS_FS_NAME,
1326 sysfs_remove_group(&adev->dev->kobj, &group);
1331 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1332 struct ras_fs_if *head)
1334 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1336 if (!obj || obj->attr_inuse)
1341 memcpy(obj->fs_data.sysfs_name,
1343 sizeof(obj->fs_data.sysfs_name));
1345 obj->sysfs_attr = (struct device_attribute){
1347 .name = obj->fs_data.sysfs_name,
1350 .show = amdgpu_ras_sysfs_read,
1352 sysfs_attr_init(&obj->sysfs_attr.attr);
1354 if (sysfs_add_file_to_group(&adev->dev->kobj,
1355 &obj->sysfs_attr.attr,
1361 obj->attr_inuse = 1;
1366 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1367 struct ras_common_if *head)
1369 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1371 if (!obj || !obj->attr_inuse)
1374 sysfs_remove_file_from_group(&adev->dev->kobj,
1375 &obj->sysfs_attr.attr,
1377 obj->attr_inuse = 0;
1383 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1385 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1386 struct ras_manager *obj, *tmp;
1388 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1389 amdgpu_ras_sysfs_remove(adev, &obj->head);
1392 if (amdgpu_bad_page_threshold != 0)
1393 amdgpu_ras_sysfs_remove_bad_page_node(adev);
1395 amdgpu_ras_sysfs_remove_feature_node(adev);
1402 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1404 * Normally when there is an uncorrectable error, the driver will reset
1405 * the GPU to recover. However, in the event of an unrecoverable error,
1406 * the driver provides an interface to reboot the system automatically
1409 * The following file in debugfs provides that interface:
1410 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1414 * .. code-block:: bash
1416 * echo true > .../ras/auto_reboot
1420 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1422 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1423 struct drm_minor *minor = adev_to_drm(adev)->primary;
1426 dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1427 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev,
1428 &amdgpu_ras_debugfs_ctrl_ops);
1429 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev,
1430 &amdgpu_ras_debugfs_eeprom_ops);
1431 debugfs_create_u32("bad_page_cnt_threshold", 0444, dir,
1432 &con->bad_page_cnt_threshold);
1433 debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled);
1434 debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled);
1435 debugfs_create_file("ras_eeprom_size", S_IRUGO, dir, adev,
1436 &amdgpu_ras_debugfs_eeprom_size_ops);
1437 con->de_ras_eeprom_table = debugfs_create_file("ras_eeprom_table",
1439 &amdgpu_ras_debugfs_eeprom_table_ops);
1440 amdgpu_ras_debugfs_set_ret_size(&con->eeprom_control);
1443 * After one uncorrectable error happens, usually GPU recovery will
1444 * be scheduled. But due to the known problem in GPU recovery failing
1445 * to bring GPU back, below interface provides one direct way to
1446 * user to reboot system automatically in such case within
1447 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1448 * will never be called.
1450 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot);
1453 * User could set this not to clean up hardware's error count register
1454 * of RAS IPs during ras recovery.
1456 debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir,
1457 &con->disable_ras_err_cnt_harvest);
1461 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1462 struct ras_fs_if *head,
1465 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1472 memcpy(obj->fs_data.debugfs_name,
1474 sizeof(obj->fs_data.debugfs_name));
1476 debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir,
1477 obj, &amdgpu_ras_debugfs_ops);
1480 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1482 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1484 struct ras_manager *obj;
1485 struct ras_fs_if fs_info;
1488 * it won't be called in resume path, no need to check
1489 * suspend and gpu reset status
1491 if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con)
1494 dir = amdgpu_ras_debugfs_create_ctrl_node(adev);
1496 list_for_each_entry(obj, &con->head, node) {
1497 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1498 (obj->attr_inuse == 1)) {
1499 sprintf(fs_info.debugfs_name, "%s_err_inject",
1500 get_ras_block_str(&obj->head));
1501 fs_info.head = obj->head;
1502 amdgpu_ras_debugfs_create(adev, &fs_info, dir);
1510 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1511 amdgpu_ras_sysfs_badpages_read, NULL, 0);
1512 static DEVICE_ATTR(features, S_IRUGO,
1513 amdgpu_ras_sysfs_features_read, NULL);
1514 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1516 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1517 struct attribute_group group = {
1518 .name = RAS_FS_NAME,
1520 struct attribute *attrs[] = {
1521 &con->features_attr.attr,
1524 struct bin_attribute *bin_attrs[] = {
1530 /* add features entry */
1531 con->features_attr = dev_attr_features;
1532 group.attrs = attrs;
1533 sysfs_attr_init(attrs[0]);
1535 if (amdgpu_bad_page_threshold != 0) {
1536 /* add bad_page_features entry */
1537 bin_attr_gpu_vram_bad_pages.private = NULL;
1538 con->badpages_attr = bin_attr_gpu_vram_bad_pages;
1539 bin_attrs[0] = &con->badpages_attr;
1540 group.bin_attrs = bin_attrs;
1541 sysfs_bin_attr_init(bin_attrs[0]);
1544 r = sysfs_create_group(&adev->dev->kobj, &group);
1546 dev_err(adev->dev, "Failed to create RAS sysfs group!");
1551 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1553 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1554 struct ras_manager *con_obj, *ip_obj, *tmp;
1556 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1557 list_for_each_entry_safe(con_obj, tmp, &con->head, node) {
1558 ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head);
1564 amdgpu_ras_sysfs_remove_all(adev);
1570 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1572 struct ras_ih_data *data = &obj->ih_data;
1573 struct amdgpu_iv_entry entry;
1575 struct ras_err_data err_data = {0, 0, 0, NULL};
1577 while (data->rptr != data->wptr) {
1579 memcpy(&entry, &data->ring[data->rptr],
1580 data->element_size);
1583 data->rptr = (data->aligned_element_size +
1584 data->rptr) % data->ring_size;
1587 if (amdgpu_ras_is_poison_mode_supported(obj->adev) &&
1588 obj->head.block == AMDGPU_RAS_BLOCK__UMC)
1589 dev_info(obj->adev->dev,
1590 "Poison is created, no user action is needed.\n");
1592 /* Let IP handle its data, maybe we need get the output
1593 * from the callback to udpate the error type/count, etc
1595 ret = data->cb(obj->adev, &err_data, &entry);
1596 /* ue will trigger an interrupt, and in that case
1597 * we need do a reset to recovery the whole system.
1598 * But leave IP do that recovery, here we just dispatch
1601 if (ret == AMDGPU_RAS_SUCCESS) {
1602 /* these counts could be left as 0 if
1603 * some blocks do not count error number
1605 obj->err_data.ue_count += err_data.ue_count;
1606 obj->err_data.ce_count += err_data.ce_count;
1613 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1615 struct ras_ih_data *data =
1616 container_of(work, struct ras_ih_data, ih_work);
1617 struct ras_manager *obj =
1618 container_of(data, struct ras_manager, ih_data);
1620 amdgpu_ras_interrupt_handler(obj);
1623 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1624 struct ras_dispatch_if *info)
1626 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1627 struct ras_ih_data *data = &obj->ih_data;
1632 if (data->inuse == 0)
1635 /* Might be overflow... */
1636 memcpy(&data->ring[data->wptr], info->entry,
1637 data->element_size);
1640 data->wptr = (data->aligned_element_size +
1641 data->wptr) % data->ring_size;
1643 schedule_work(&data->ih_work);
1648 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1649 struct ras_ih_if *info)
1651 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1652 struct ras_ih_data *data;
1657 data = &obj->ih_data;
1658 if (data->inuse == 0)
1661 cancel_work_sync(&data->ih_work);
1664 memset(data, 0, sizeof(*data));
1670 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1671 struct ras_ih_if *info)
1673 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1674 struct ras_ih_data *data;
1677 /* in case we registe the IH before enable ras feature */
1678 obj = amdgpu_ras_create_obj(adev, &info->head);
1684 data = &obj->ih_data;
1685 /* add the callback.etc */
1686 *data = (struct ras_ih_data) {
1689 .element_size = sizeof(struct amdgpu_iv_entry),
1694 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1696 data->aligned_element_size = ALIGN(data->element_size, 8);
1697 /* the ring can store 64 iv entries. */
1698 data->ring_size = 64 * data->aligned_element_size;
1699 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1711 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1713 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1714 struct ras_manager *obj, *tmp;
1716 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1717 struct ras_ih_if info = {
1720 amdgpu_ras_interrupt_remove_handler(adev, &info);
1727 /* traversal all IPs except NBIO to query error counter */
1728 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1730 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1731 struct ras_manager *obj;
1733 if (!adev->ras_enabled || !con)
1736 list_for_each_entry(obj, &con->head, node) {
1737 struct ras_query_if info = {
1742 * PCIE_BIF IP has one different isr by ras controller
1743 * interrupt, the specific ras counter query will be
1744 * done in that isr. So skip such block from common
1745 * sync flood interrupt isr calling.
1747 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1751 * this is a workaround for aldebaran, skip send msg to
1752 * smu to get ecc_info table due to smu handle get ecc
1753 * info table failed temporarily.
1754 * should be removed until smu fix handle ecc_info table.
1756 if ((info.head.block == AMDGPU_RAS_BLOCK__UMC) &&
1757 (adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 2)))
1760 amdgpu_ras_query_error_status(adev, &info);
1764 /* Parse RdRspStatus and WrRspStatus */
1765 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
1766 struct ras_query_if *info)
1769 * Only two block need to query read/write
1770 * RspStatus at current state
1772 switch (info->head.block) {
1773 case AMDGPU_RAS_BLOCK__GFX:
1774 if (adev->gfx.ras_funcs &&
1775 adev->gfx.ras_funcs->query_ras_error_status)
1776 adev->gfx.ras_funcs->query_ras_error_status(adev);
1778 case AMDGPU_RAS_BLOCK__MMHUB:
1779 if (adev->mmhub.ras_funcs &&
1780 adev->mmhub.ras_funcs->query_ras_error_status)
1781 adev->mmhub.ras_funcs->query_ras_error_status(adev);
1788 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
1790 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1791 struct ras_manager *obj;
1793 if (!adev->ras_enabled || !con)
1796 list_for_each_entry(obj, &con->head, node) {
1797 struct ras_query_if info = {
1801 amdgpu_ras_error_status_query(adev, &info);
1805 /* recovery begin */
1807 /* return 0 on success.
1808 * caller need free bps.
1810 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1811 struct ras_badpage **bps, unsigned int *count)
1813 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1814 struct ras_err_handler_data *data;
1816 int ret = 0, status;
1818 if (!con || !con->eh_data || !bps || !count)
1821 mutex_lock(&con->recovery_lock);
1822 data = con->eh_data;
1823 if (!data || data->count == 0) {
1829 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1835 for (; i < data->count; i++) {
1836 (*bps)[i] = (struct ras_badpage){
1837 .bp = data->bps[i].retired_page,
1838 .size = AMDGPU_GPU_PAGE_SIZE,
1839 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1841 status = amdgpu_vram_mgr_query_page_status(
1842 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1843 data->bps[i].retired_page);
1844 if (status == -EBUSY)
1845 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1846 else if (status == -ENOENT)
1847 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1850 *count = data->count;
1852 mutex_unlock(&con->recovery_lock);
1856 static void amdgpu_ras_do_recovery(struct work_struct *work)
1858 struct amdgpu_ras *ras =
1859 container_of(work, struct amdgpu_ras, recovery_work);
1860 struct amdgpu_device *remote_adev = NULL;
1861 struct amdgpu_device *adev = ras->adev;
1862 struct list_head device_list, *device_list_handle = NULL;
1864 if (!ras->disable_ras_err_cnt_harvest) {
1865 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
1867 /* Build list of devices to query RAS related errors */
1868 if (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
1869 device_list_handle = &hive->device_list;
1871 INIT_LIST_HEAD(&device_list);
1872 list_add_tail(&adev->gmc.xgmi.head, &device_list);
1873 device_list_handle = &device_list;
1876 list_for_each_entry(remote_adev,
1877 device_list_handle, gmc.xgmi.head) {
1878 amdgpu_ras_query_err_status(remote_adev);
1879 amdgpu_ras_log_on_err_counter(remote_adev);
1882 amdgpu_put_xgmi_hive(hive);
1885 if (amdgpu_device_should_recover_gpu(ras->adev))
1886 amdgpu_device_gpu_recover(ras->adev, NULL);
1887 atomic_set(&ras->in_recovery, 0);
1890 /* alloc/realloc bps array */
1891 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1892 struct ras_err_handler_data *data, int pages)
1894 unsigned int old_space = data->count + data->space_left;
1895 unsigned int new_space = old_space + pages;
1896 unsigned int align_space = ALIGN(new_space, 512);
1897 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1905 memcpy(bps, data->bps,
1906 data->count * sizeof(*data->bps));
1911 data->space_left += align_space - old_space;
1915 /* it deal with vram only. */
1916 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1917 struct eeprom_table_record *bps, int pages)
1919 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1920 struct ras_err_handler_data *data;
1924 if (!con || !con->eh_data || !bps || pages <= 0)
1927 mutex_lock(&con->recovery_lock);
1928 data = con->eh_data;
1932 for (i = 0; i < pages; i++) {
1933 if (amdgpu_ras_check_bad_page_unlock(con,
1934 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT))
1937 if (!data->space_left &&
1938 amdgpu_ras_realloc_eh_data_space(adev, data, 256)) {
1943 amdgpu_vram_mgr_reserve_range(
1944 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1945 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT,
1946 AMDGPU_GPU_PAGE_SIZE);
1948 memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps));
1953 mutex_unlock(&con->recovery_lock);
1959 * write error record array to eeprom, the function should be
1960 * protected by recovery_lock
1962 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1964 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1965 struct ras_err_handler_data *data;
1966 struct amdgpu_ras_eeprom_control *control;
1969 if (!con || !con->eh_data)
1972 mutex_lock(&con->recovery_lock);
1973 control = &con->eeprom_control;
1974 data = con->eh_data;
1975 save_count = data->count - control->ras_num_recs;
1976 mutex_unlock(&con->recovery_lock);
1977 /* only new entries are saved */
1978 if (save_count > 0) {
1979 if (amdgpu_ras_eeprom_append(control,
1980 &data->bps[control->ras_num_recs],
1982 dev_err(adev->dev, "Failed to save EEPROM table data!");
1986 dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
1993 * read error record array in eeprom and reserve enough space for
1994 * storing new bad pages
1996 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1998 struct amdgpu_ras_eeprom_control *control =
1999 &adev->psp.ras_context.ras->eeprom_control;
2000 struct eeprom_table_record *bps;
2003 /* no bad page record, skip eeprom access */
2004 if (control->ras_num_recs == 0 || amdgpu_bad_page_threshold == 0)
2007 bps = kcalloc(control->ras_num_recs, sizeof(*bps), GFP_KERNEL);
2011 ret = amdgpu_ras_eeprom_read(control, bps, control->ras_num_recs);
2013 dev_err(adev->dev, "Failed to load EEPROM table records!");
2015 ret = amdgpu_ras_add_bad_pages(adev, bps, control->ras_num_recs);
2021 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
2024 struct ras_err_handler_data *data = con->eh_data;
2027 addr >>= AMDGPU_GPU_PAGE_SHIFT;
2028 for (i = 0; i < data->count; i++)
2029 if (addr == data->bps[i].retired_page)
2036 * check if an address belongs to bad page
2038 * Note: this check is only for umc block
2040 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
2043 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2046 if (!con || !con->eh_data)
2049 mutex_lock(&con->recovery_lock);
2050 ret = amdgpu_ras_check_bad_page_unlock(con, addr);
2051 mutex_unlock(&con->recovery_lock);
2055 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
2058 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2061 * Justification of value bad_page_cnt_threshold in ras structure
2063 * Generally, -1 <= amdgpu_bad_page_threshold <= max record length
2064 * in eeprom, and introduce two scenarios accordingly.
2066 * Bad page retirement enablement:
2067 * - If amdgpu_bad_page_threshold = -1,
2068 * bad_page_cnt_threshold = typical value by formula.
2070 * - When the value from user is 0 < amdgpu_bad_page_threshold <
2071 * max record length in eeprom, use it directly.
2073 * Bad page retirement disablement:
2074 * - If amdgpu_bad_page_threshold = 0, bad page retirement
2075 * functionality is disabled, and bad_page_cnt_threshold will
2079 if (amdgpu_bad_page_threshold < 0) {
2080 u64 val = adev->gmc.mc_vram_size;
2082 do_div(val, RAS_BAD_PAGE_COVER);
2083 con->bad_page_cnt_threshold = min(lower_32_bits(val),
2086 con->bad_page_cnt_threshold = min_t(int, max_count,
2087 amdgpu_bad_page_threshold);
2091 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
2093 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2094 struct ras_err_handler_data **data;
2095 u32 max_eeprom_records_count = 0;
2096 bool exc_err_limit = false;
2102 /* Allow access to RAS EEPROM via debugfs, when the ASIC
2103 * supports RAS and debugfs is enabled, but when
2104 * adev->ras_enabled is unset, i.e. when "ras_enable"
2105 * module parameter is set to 0.
2109 if (!adev->ras_enabled)
2112 data = &con->eh_data;
2113 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
2119 mutex_init(&con->recovery_lock);
2120 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
2121 atomic_set(&con->in_recovery, 0);
2123 max_eeprom_records_count = amdgpu_ras_eeprom_max_record_count();
2124 amdgpu_ras_validate_threshold(adev, max_eeprom_records_count);
2126 /* Todo: During test the SMU might fail to read the eeprom through I2C
2127 * when the GPU is pending on XGMI reset during probe time
2128 * (Mostly after second bus reset), skip it now
2130 if (adev->gmc.xgmi.pending_reset)
2132 ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit);
2134 * This calling fails when exc_err_limit is true or
2137 if (exc_err_limit || ret)
2140 if (con->eeprom_control.ras_num_recs) {
2141 ret = amdgpu_ras_load_bad_pages(adev);
2145 if (adev->smu.ppt_funcs && adev->smu.ppt_funcs->send_hbm_bad_pages_num)
2146 adev->smu.ppt_funcs->send_hbm_bad_pages_num(&adev->smu, con->eeprom_control.ras_num_recs);
2149 #ifdef CONFIG_X86_MCE_AMD
2150 if ((adev->asic_type == CHIP_ALDEBARAN) &&
2151 (adev->gmc.xgmi.connected_to_cpu))
2152 amdgpu_register_bad_pages_mca_notifier(adev);
2157 kfree((*data)->bps);
2159 con->eh_data = NULL;
2161 dev_warn(adev->dev, "Failed to initialize ras recovery! (%d)\n", ret);
2164 * Except error threshold exceeding case, other failure cases in this
2165 * function would not fail amdgpu driver init.
2175 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
2177 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2178 struct ras_err_handler_data *data = con->eh_data;
2180 /* recovery_init failed to init it, fini is useless */
2184 cancel_work_sync(&con->recovery_work);
2186 mutex_lock(&con->recovery_lock);
2187 con->eh_data = NULL;
2190 mutex_unlock(&con->recovery_lock);
2196 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev)
2198 return adev->asic_type == CHIP_VEGA10 ||
2199 adev->asic_type == CHIP_VEGA20 ||
2200 adev->asic_type == CHIP_ARCTURUS ||
2201 adev->asic_type == CHIP_ALDEBARAN ||
2202 adev->asic_type == CHIP_SIENNA_CICHLID;
2206 * this is workaround for vega20 workstation sku,
2207 * force enable gfx ras, ignore vbios gfx ras flag
2208 * due to GC EDC can not write
2210 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev)
2212 struct atom_context *ctx = adev->mode_info.atom_context;
2217 if (strnstr(ctx->vbios_version, "D16406",
2218 sizeof(ctx->vbios_version)) ||
2219 strnstr(ctx->vbios_version, "D36002",
2220 sizeof(ctx->vbios_version)))
2221 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX);
2225 * check hardware's ras ability which will be saved in hw_supported.
2226 * if hardware does not support ras, we can skip some ras initializtion and
2227 * forbid some ras operations from IP.
2228 * if software itself, say boot parameter, limit the ras ability. We still
2229 * need allow IP do some limited operations, like disable. In such case,
2230 * we have to initialize ras as normal. but need check if operation is
2231 * allowed or not in each function.
2233 static void amdgpu_ras_check_supported(struct amdgpu_device *adev)
2235 adev->ras_hw_enabled = adev->ras_enabled = 0;
2237 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
2238 !amdgpu_ras_asic_supported(adev))
2241 if (!adev->gmc.xgmi.connected_to_cpu) {
2242 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
2243 dev_info(adev->dev, "MEM ECC is active.\n");
2244 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC |
2245 1 << AMDGPU_RAS_BLOCK__DF);
2247 dev_info(adev->dev, "MEM ECC is not presented.\n");
2250 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
2251 dev_info(adev->dev, "SRAM ECC is active.\n");
2252 adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
2253 1 << AMDGPU_RAS_BLOCK__DF);
2255 dev_info(adev->dev, "SRAM ECC is not presented.\n");
2258 /* driver only manages a few IP blocks RAS feature
2259 * when GPU is connected cpu through XGMI */
2260 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX |
2261 1 << AMDGPU_RAS_BLOCK__SDMA |
2262 1 << AMDGPU_RAS_BLOCK__MMHUB);
2265 amdgpu_ras_get_quirks(adev);
2267 /* hw_supported needs to be aligned with RAS block mask. */
2268 adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK;
2270 adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 :
2271 adev->ras_hw_enabled & amdgpu_ras_mask;
2274 static void amdgpu_ras_counte_dw(struct work_struct *work)
2276 struct amdgpu_ras *con = container_of(work, struct amdgpu_ras,
2277 ras_counte_delay_work.work);
2278 struct amdgpu_device *adev = con->adev;
2279 struct drm_device *dev = adev_to_drm(adev);
2280 unsigned long ce_count, ue_count;
2283 res = pm_runtime_get_sync(dev->dev);
2287 /* Cache new values.
2289 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2290 atomic_set(&con->ras_ce_count, ce_count);
2291 atomic_set(&con->ras_ue_count, ue_count);
2294 pm_runtime_mark_last_busy(dev->dev);
2296 pm_runtime_put_autosuspend(dev->dev);
2299 int amdgpu_ras_init(struct amdgpu_device *adev)
2301 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2303 bool df_poison, umc_poison;
2308 con = kmalloc(sizeof(struct amdgpu_ras) +
2309 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT +
2310 sizeof(struct ras_manager) * AMDGPU_RAS_MCA_BLOCK_COUNT,
2311 GFP_KERNEL|__GFP_ZERO);
2316 INIT_DELAYED_WORK(&con->ras_counte_delay_work, amdgpu_ras_counte_dw);
2317 atomic_set(&con->ras_ce_count, 0);
2318 atomic_set(&con->ras_ue_count, 0);
2320 con->objs = (struct ras_manager *)(con + 1);
2322 amdgpu_ras_set_context(adev, con);
2324 amdgpu_ras_check_supported(adev);
2326 if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) {
2327 /* set gfx block ras context feature for VEGA20 Gaming
2328 * send ras disable cmd to ras ta during ras late init.
2330 if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) {
2331 con->features |= BIT(AMDGPU_RAS_BLOCK__GFX);
2341 INIT_LIST_HEAD(&con->head);
2342 /* Might need get this flag from vbios. */
2343 con->flags = RAS_DEFAULT_FLAGS;
2345 /* initialize nbio ras function ahead of any other
2346 * ras functions so hardware fatal error interrupt
2347 * can be enabled as early as possible */
2348 switch (adev->asic_type) {
2351 case CHIP_ALDEBARAN:
2352 if (!adev->gmc.xgmi.connected_to_cpu)
2353 adev->nbio.ras_funcs = &nbio_v7_4_ras_funcs;
2356 /* nbio ras is not available */
2360 if (adev->nbio.ras_funcs &&
2361 adev->nbio.ras_funcs->init_ras_controller_interrupt) {
2362 r = adev->nbio.ras_funcs->init_ras_controller_interrupt(adev);
2367 if (adev->nbio.ras_funcs &&
2368 adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt) {
2369 r = adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt(adev);
2374 /* Init poison supported flag, the default value is false */
2375 if (adev->gmc.xgmi.connected_to_cpu) {
2376 /* enabled by default when GPU is connected to CPU */
2377 con->poison_supported = true;
2379 else if (adev->df.funcs &&
2380 adev->df.funcs->query_ras_poison_mode &&
2381 adev->umc.ras_funcs &&
2382 adev->umc.ras_funcs->query_ras_poison_mode) {
2384 adev->df.funcs->query_ras_poison_mode(adev);
2386 adev->umc.ras_funcs->query_ras_poison_mode(adev);
2387 /* Only poison is set in both DF and UMC, we can support it */
2388 if (df_poison && umc_poison)
2389 con->poison_supported = true;
2390 else if (df_poison != umc_poison)
2391 dev_warn(adev->dev, "Poison setting is inconsistent in DF/UMC(%d:%d)!\n",
2392 df_poison, umc_poison);
2395 if (amdgpu_ras_fs_init(adev)) {
2400 dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
2401 "hardware ability[%x] ras_mask[%x]\n",
2402 adev->ras_hw_enabled, adev->ras_enabled);
2406 amdgpu_ras_set_context(adev, NULL);
2412 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev)
2414 if (adev->gmc.xgmi.connected_to_cpu)
2419 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev,
2420 struct ras_common_if *ras_block)
2422 struct ras_query_if info = {
2426 if (!amdgpu_persistent_edc_harvesting_supported(adev))
2429 if (amdgpu_ras_query_error_status(adev, &info) != 0)
2430 DRM_WARN("RAS init harvest failure");
2432 if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0)
2433 DRM_WARN("RAS init harvest reset failure");
2438 bool amdgpu_ras_is_poison_mode_supported(struct amdgpu_device *adev)
2440 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2445 return con->poison_supported;
2448 /* helper function to handle common stuff in ip late init phase */
2449 int amdgpu_ras_late_init(struct amdgpu_device *adev,
2450 struct ras_common_if *ras_block,
2451 struct ras_fs_if *fs_info,
2452 struct ras_ih_if *ih_info)
2454 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2455 unsigned long ue_count, ce_count;
2458 /* disable RAS feature per IP block if it is not supported */
2459 if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
2460 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
2464 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
2466 if (adev->in_suspend || amdgpu_in_reset(adev)) {
2467 /* in resume phase, if fail to enable ras,
2468 * clean up all ras fs nodes, and disable ras */
2474 /* check for errors on warm reset edc persisant supported ASIC */
2475 amdgpu_persistent_edc_harvesting(adev, ras_block);
2477 /* in resume phase, no need to create ras fs node */
2478 if (adev->in_suspend || amdgpu_in_reset(adev))
2482 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
2487 r = amdgpu_ras_sysfs_create(adev, fs_info);
2491 /* Those are the cached values at init.
2493 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2494 atomic_set(&con->ras_ce_count, ce_count);
2495 atomic_set(&con->ras_ue_count, ue_count);
2500 amdgpu_ras_sysfs_remove(adev, ras_block);
2503 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2505 amdgpu_ras_feature_enable(adev, ras_block, 0);
2509 /* helper function to remove ras fs node and interrupt handler */
2510 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
2511 struct ras_common_if *ras_block,
2512 struct ras_ih_if *ih_info)
2514 if (!ras_block || !ih_info)
2517 amdgpu_ras_sysfs_remove(adev, ras_block);
2519 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2522 /* do some init work after IP late init as dependence.
2523 * and it runs in resume/gpu reset/booting up cases.
2525 void amdgpu_ras_resume(struct amdgpu_device *adev)
2527 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2528 struct ras_manager *obj, *tmp;
2530 if (!adev->ras_enabled || !con) {
2531 /* clean ras context for VEGA20 Gaming after send ras disable cmd */
2532 amdgpu_release_ras_context(adev);
2537 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2538 /* Set up all other IPs which are not implemented. There is a
2539 * tricky thing that IP's actual ras error type should be
2540 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2541 * ERROR_NONE make sense anyway.
2543 amdgpu_ras_enable_all_features(adev, 1);
2545 /* We enable ras on all hw_supported block, but as boot
2546 * parameter might disable some of them and one or more IP has
2547 * not implemented yet. So we disable them on behalf.
2549 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2550 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2551 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2552 /* there should be no any reference. */
2553 WARN_ON(alive_obj(obj));
2559 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2561 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2563 if (!adev->ras_enabled || !con)
2566 amdgpu_ras_disable_all_features(adev, 0);
2567 /* Make sure all ras objects are disabled. */
2569 amdgpu_ras_disable_all_features(adev, 1);
2572 /* do some fini work before IP fini as dependence */
2573 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2575 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2577 if (!adev->ras_enabled || !con)
2581 /* Need disable ras on all IPs here before ip [hw/sw]fini */
2582 amdgpu_ras_disable_all_features(adev, 0);
2583 amdgpu_ras_recovery_fini(adev);
2587 int amdgpu_ras_fini(struct amdgpu_device *adev)
2589 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2591 if (!adev->ras_enabled || !con)
2594 amdgpu_ras_fs_fini(adev);
2595 amdgpu_ras_interrupt_remove_all(adev);
2597 WARN(con->features, "Feature mask is not cleared");
2600 amdgpu_ras_disable_all_features(adev, 1);
2602 cancel_delayed_work_sync(&con->ras_counte_delay_work);
2604 amdgpu_ras_set_context(adev, NULL);
2610 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2612 amdgpu_ras_check_supported(adev);
2613 if (!adev->ras_hw_enabled)
2616 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2617 dev_info(adev->dev, "uncorrectable hardware error"
2618 "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2620 amdgpu_ras_reset_gpu(adev);
2624 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
2626 if (adev->asic_type == CHIP_VEGA20 &&
2627 adev->pm.fw_version <= 0x283400) {
2628 return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
2629 amdgpu_ras_intr_triggered();
2635 void amdgpu_release_ras_context(struct amdgpu_device *adev)
2637 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2642 if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) {
2643 con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX);
2644 amdgpu_ras_set_context(adev, NULL);
2649 #ifdef CONFIG_X86_MCE_AMD
2650 static struct amdgpu_device *find_adev(uint32_t node_id)
2653 struct amdgpu_device *adev = NULL;
2655 for (i = 0; i < mce_adev_list.num_gpu; i++) {
2656 adev = mce_adev_list.devs[i];
2658 if (adev && adev->gmc.xgmi.connected_to_cpu &&
2659 adev->gmc.xgmi.physical_node_id == node_id)
2667 #define GET_MCA_IPID_GPUID(m) (((m) >> 44) & 0xF)
2668 #define GET_UMC_INST(m) (((m) >> 21) & 0x7)
2669 #define GET_CHAN_INDEX(m) ((((m) >> 12) & 0x3) | (((m) >> 18) & 0x4))
2670 #define GPU_ID_OFFSET 8
2672 static int amdgpu_bad_page_notifier(struct notifier_block *nb,
2673 unsigned long val, void *data)
2675 struct mce *m = (struct mce *)data;
2676 struct amdgpu_device *adev = NULL;
2677 uint32_t gpu_id = 0;
2678 uint32_t umc_inst = 0;
2679 uint32_t ch_inst, channel_index = 0;
2680 struct ras_err_data err_data = {0, 0, 0, NULL};
2681 struct eeprom_table_record err_rec;
2682 uint64_t retired_page;
2685 * If the error was generated in UMC_V2, which belongs to GPU UMCs,
2686 * and error occurred in DramECC (Extended error code = 0) then only
2687 * process the error, else bail out.
2689 if (!m || !((smca_get_bank_type(m->extcpu, m->bank) == SMCA_UMC_V2) &&
2690 (XEC(m->status, 0x3f) == 0x0)))
2694 * If it is correctable error, return.
2696 if (mce_is_correctable(m))
2700 * GPU Id is offset by GPU_ID_OFFSET in MCA_IPID_UMC register.
2702 gpu_id = GET_MCA_IPID_GPUID(m->ipid) - GPU_ID_OFFSET;
2704 adev = find_adev(gpu_id);
2706 DRM_WARN("%s: Unable to find adev for gpu_id: %d\n", __func__,
2712 * If it is uncorrectable error, then find out UMC instance and
2715 umc_inst = GET_UMC_INST(m->ipid);
2716 ch_inst = GET_CHAN_INDEX(m->ipid);
2718 dev_info(adev->dev, "Uncorrectable error detected in UMC inst: %d, chan_idx: %d",
2721 memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
2724 * Translate UMC channel address to Physical address
2727 adev->umc.channel_idx_tbl[umc_inst * adev->umc.channel_inst_num
2730 retired_page = ADDR_OF_8KB_BLOCK(m->addr) |
2731 ADDR_OF_256B_BLOCK(channel_index) |
2732 OFFSET_IN_256B_BLOCK(m->addr);
2734 err_rec.address = m->addr;
2735 err_rec.retired_page = retired_page >> AMDGPU_GPU_PAGE_SHIFT;
2736 err_rec.ts = (uint64_t)ktime_get_real_seconds();
2737 err_rec.err_type = AMDGPU_RAS_EEPROM_ERR_NON_RECOVERABLE;
2739 err_rec.mem_channel = channel_index;
2740 err_rec.mcumc_id = umc_inst;
2742 err_data.err_addr = &err_rec;
2743 err_data.err_addr_cnt = 1;
2745 if (amdgpu_bad_page_threshold != 0) {
2746 amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
2747 err_data.err_addr_cnt);
2748 amdgpu_ras_save_bad_pages(adev);
2754 static struct notifier_block amdgpu_bad_page_nb = {
2755 .notifier_call = amdgpu_bad_page_notifier,
2756 .priority = MCE_PRIO_UC,
2759 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev)
2762 * Add the adev to the mce_adev_list.
2763 * During mode2 reset, amdgpu device is temporarily
2764 * removed from the mgpu_info list which can cause
2765 * page retirement to fail.
2766 * Use this list instead of mgpu_info to find the amdgpu
2767 * device on which the UMC error was reported.
2769 mce_adev_list.devs[mce_adev_list.num_gpu++] = adev;
2772 * Register the x86 notifier only once
2773 * with MCE subsystem.
2775 if (notifier_registered == false) {
2776 mce_register_decode_chain(&amdgpu_bad_page_nb);
2777 notifier_registered = true;