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 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 /* query/inject/cure begin */
896 int amdgpu_ras_query_error_status(struct amdgpu_device *adev,
897 struct ras_query_if *info)
899 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
900 struct ras_err_data err_data = {0, 0, 0, NULL};
906 switch (info->head.block) {
907 case AMDGPU_RAS_BLOCK__UMC:
908 if (adev->umc.ras_funcs &&
909 adev->umc.ras_funcs->query_ras_error_count)
910 adev->umc.ras_funcs->query_ras_error_count(adev, &err_data);
911 /* umc query_ras_error_address is also responsible for clearing
914 if (adev->umc.ras_funcs &&
915 adev->umc.ras_funcs->query_ras_error_address)
916 adev->umc.ras_funcs->query_ras_error_address(adev, &err_data);
918 case AMDGPU_RAS_BLOCK__SDMA:
919 if (adev->sdma.funcs->query_ras_error_count) {
920 for (i = 0; i < adev->sdma.num_instances; i++)
921 adev->sdma.funcs->query_ras_error_count(adev, i,
925 case AMDGPU_RAS_BLOCK__GFX:
926 if (adev->gfx.ras_funcs &&
927 adev->gfx.ras_funcs->query_ras_error_count)
928 adev->gfx.ras_funcs->query_ras_error_count(adev, &err_data);
930 if (adev->gfx.ras_funcs &&
931 adev->gfx.ras_funcs->query_ras_error_status)
932 adev->gfx.ras_funcs->query_ras_error_status(adev);
934 case AMDGPU_RAS_BLOCK__MMHUB:
935 if (adev->mmhub.ras_funcs &&
936 adev->mmhub.ras_funcs->query_ras_error_count)
937 adev->mmhub.ras_funcs->query_ras_error_count(adev, &err_data);
939 if (adev->mmhub.ras_funcs &&
940 adev->mmhub.ras_funcs->query_ras_error_status)
941 adev->mmhub.ras_funcs->query_ras_error_status(adev);
943 case AMDGPU_RAS_BLOCK__PCIE_BIF:
944 if (adev->nbio.ras_funcs &&
945 adev->nbio.ras_funcs->query_ras_error_count)
946 adev->nbio.ras_funcs->query_ras_error_count(adev, &err_data);
948 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
949 if (adev->gmc.xgmi.ras_funcs &&
950 adev->gmc.xgmi.ras_funcs->query_ras_error_count)
951 adev->gmc.xgmi.ras_funcs->query_ras_error_count(adev, &err_data);
953 case AMDGPU_RAS_BLOCK__HDP:
954 if (adev->hdp.ras_funcs &&
955 adev->hdp.ras_funcs->query_ras_error_count)
956 adev->hdp.ras_funcs->query_ras_error_count(adev, &err_data);
958 case AMDGPU_RAS_BLOCK__MCA:
959 amdgpu_ras_mca_query_error_status(adev, &info->head, &err_data);
965 obj->err_data.ue_count += err_data.ue_count;
966 obj->err_data.ce_count += err_data.ce_count;
968 info->ue_count = obj->err_data.ue_count;
969 info->ce_count = obj->err_data.ce_count;
971 if (err_data.ce_count) {
972 if (adev->smuio.funcs &&
973 adev->smuio.funcs->get_socket_id &&
974 adev->smuio.funcs->get_die_id) {
975 dev_info(adev->dev, "socket: %d, die: %d "
976 "%ld correctable hardware errors "
977 "detected in %s block, no user "
978 "action is needed.\n",
979 adev->smuio.funcs->get_socket_id(adev),
980 adev->smuio.funcs->get_die_id(adev),
981 obj->err_data.ce_count,
982 get_ras_block_str(&info->head));
984 dev_info(adev->dev, "%ld correctable hardware errors "
985 "detected in %s block, no user "
986 "action is needed.\n",
987 obj->err_data.ce_count,
988 get_ras_block_str(&info->head));
991 if (err_data.ue_count) {
992 if (adev->smuio.funcs &&
993 adev->smuio.funcs->get_socket_id &&
994 adev->smuio.funcs->get_die_id) {
995 dev_info(adev->dev, "socket: %d, die: %d "
996 "%ld uncorrectable hardware errors "
997 "detected in %s block\n",
998 adev->smuio.funcs->get_socket_id(adev),
999 adev->smuio.funcs->get_die_id(adev),
1000 obj->err_data.ue_count,
1001 get_ras_block_str(&info->head));
1003 dev_info(adev->dev, "%ld uncorrectable hardware errors "
1004 "detected in %s block\n",
1005 obj->err_data.ue_count,
1006 get_ras_block_str(&info->head));
1010 if (!amdgpu_persistent_edc_harvesting_supported(adev))
1011 amdgpu_ras_reset_error_status(adev, info->head.block);
1016 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev,
1017 enum amdgpu_ras_block block)
1019 if (!amdgpu_ras_is_supported(adev, block))
1023 case AMDGPU_RAS_BLOCK__GFX:
1024 if (adev->gfx.ras_funcs &&
1025 adev->gfx.ras_funcs->reset_ras_error_count)
1026 adev->gfx.ras_funcs->reset_ras_error_count(adev);
1028 if (adev->gfx.ras_funcs &&
1029 adev->gfx.ras_funcs->reset_ras_error_status)
1030 adev->gfx.ras_funcs->reset_ras_error_status(adev);
1032 case AMDGPU_RAS_BLOCK__MMHUB:
1033 if (adev->mmhub.ras_funcs &&
1034 adev->mmhub.ras_funcs->reset_ras_error_count)
1035 adev->mmhub.ras_funcs->reset_ras_error_count(adev);
1037 if (adev->mmhub.ras_funcs &&
1038 adev->mmhub.ras_funcs->reset_ras_error_status)
1039 adev->mmhub.ras_funcs->reset_ras_error_status(adev);
1041 case AMDGPU_RAS_BLOCK__SDMA:
1042 if (adev->sdma.funcs->reset_ras_error_count)
1043 adev->sdma.funcs->reset_ras_error_count(adev);
1045 case AMDGPU_RAS_BLOCK__HDP:
1046 if (adev->hdp.ras_funcs &&
1047 adev->hdp.ras_funcs->reset_ras_error_count)
1048 adev->hdp.ras_funcs->reset_ras_error_count(adev);
1057 /* Trigger XGMI/WAFL error */
1058 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev,
1059 struct ta_ras_trigger_error_input *block_info)
1063 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
1064 dev_warn(adev->dev, "Failed to disallow df cstate");
1066 if (amdgpu_dpm_allow_xgmi_power_down(adev, false))
1067 dev_warn(adev->dev, "Failed to disallow XGMI power down");
1069 ret = psp_ras_trigger_error(&adev->psp, block_info);
1071 if (amdgpu_ras_intr_triggered())
1074 if (amdgpu_dpm_allow_xgmi_power_down(adev, true))
1075 dev_warn(adev->dev, "Failed to allow XGMI power down");
1077 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW))
1078 dev_warn(adev->dev, "Failed to allow df cstate");
1083 /* wrapper of psp_ras_trigger_error */
1084 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
1085 struct ras_inject_if *info)
1087 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1088 struct ta_ras_trigger_error_input block_info = {
1089 .block_id = amdgpu_ras_block_to_ta(info->head.block),
1090 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
1091 .sub_block_index = info->head.sub_block_index,
1092 .address = info->address,
1093 .value = info->value,
1100 /* Calculate XGMI relative offset */
1101 if (adev->gmc.xgmi.num_physical_nodes > 1) {
1102 block_info.address =
1103 amdgpu_xgmi_get_relative_phy_addr(adev,
1104 block_info.address);
1107 switch (info->head.block) {
1108 case AMDGPU_RAS_BLOCK__GFX:
1109 if (adev->gfx.ras_funcs &&
1110 adev->gfx.ras_funcs->ras_error_inject)
1111 ret = adev->gfx.ras_funcs->ras_error_inject(adev, info);
1115 case AMDGPU_RAS_BLOCK__UMC:
1116 case AMDGPU_RAS_BLOCK__SDMA:
1117 case AMDGPU_RAS_BLOCK__MMHUB:
1118 case AMDGPU_RAS_BLOCK__PCIE_BIF:
1119 case AMDGPU_RAS_BLOCK__MCA:
1120 ret = psp_ras_trigger_error(&adev->psp, &block_info);
1122 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
1123 ret = amdgpu_ras_error_inject_xgmi(adev, &block_info);
1126 dev_info(adev->dev, "%s error injection is not supported yet\n",
1127 get_ras_block_str(&info->head));
1132 dev_err(adev->dev, "ras inject %s failed %d\n",
1133 get_ras_block_str(&info->head), ret);
1139 * amdgpu_ras_query_error_count -- Get error counts of all IPs
1140 * adev: pointer to AMD GPU device
1141 * ce_count: pointer to an integer to be set to the count of correctible errors.
1142 * ue_count: pointer to an integer to be set to the count of uncorrectible
1145 * If set, @ce_count or @ue_count, count and return the corresponding
1146 * error counts in those integer pointers. Return 0 if the device
1147 * supports RAS. Return -EOPNOTSUPP if the device doesn't support RAS.
1149 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
1150 unsigned long *ce_count,
1151 unsigned long *ue_count)
1153 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1154 struct ras_manager *obj;
1155 unsigned long ce, ue;
1157 if (!adev->ras_enabled || !con)
1160 /* Don't count since no reporting.
1162 if (!ce_count && !ue_count)
1167 list_for_each_entry(obj, &con->head, node) {
1168 struct ras_query_if info = {
1173 res = amdgpu_ras_query_error_status(adev, &info);
1177 ce += info.ce_count;
1178 ue += info.ue_count;
1189 /* query/inject/cure end */
1194 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1195 struct ras_badpage **bps, unsigned int *count);
1197 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
1200 case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
1202 case AMDGPU_RAS_RETIRE_PAGE_PENDING:
1204 case AMDGPU_RAS_RETIRE_PAGE_FAULT:
1211 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
1213 * It allows user to read the bad pages of vram on the gpu through
1214 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
1216 * It outputs multiple lines, and each line stands for one gpu page.
1218 * The format of one line is below,
1219 * gpu pfn : gpu page size : flags
1221 * gpu pfn and gpu page size are printed in hex format.
1222 * flags can be one of below character,
1224 * R: reserved, this gpu page is reserved and not able to use.
1226 * P: pending for reserve, this gpu page is marked as bad, will be reserved
1227 * in next window of page_reserve.
1229 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
1233 * .. code-block:: bash
1235 * 0x00000001 : 0x00001000 : R
1236 * 0x00000002 : 0x00001000 : P
1240 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
1241 struct kobject *kobj, struct bin_attribute *attr,
1242 char *buf, loff_t ppos, size_t count)
1244 struct amdgpu_ras *con =
1245 container_of(attr, struct amdgpu_ras, badpages_attr);
1246 struct amdgpu_device *adev = con->adev;
1247 const unsigned int element_size =
1248 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
1249 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
1250 unsigned int end = div64_ul(ppos + count - 1, element_size);
1252 struct ras_badpage *bps = NULL;
1253 unsigned int bps_count = 0;
1255 memset(buf, 0, count);
1257 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1260 for (; start < end && start < bps_count; start++)
1261 s += scnprintf(&buf[s], element_size + 1,
1262 "0x%08x : 0x%08x : %1s\n",
1265 amdgpu_ras_badpage_flags_str(bps[start].flags));
1272 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1273 struct device_attribute *attr, char *buf)
1275 struct amdgpu_ras *con =
1276 container_of(attr, struct amdgpu_ras, features_attr);
1278 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
1281 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1283 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1285 sysfs_remove_file_from_group(&adev->dev->kobj,
1286 &con->badpages_attr.attr,
1290 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1292 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1293 struct attribute *attrs[] = {
1294 &con->features_attr.attr,
1297 struct attribute_group group = {
1298 .name = RAS_FS_NAME,
1302 sysfs_remove_group(&adev->dev->kobj, &group);
1307 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1308 struct ras_fs_if *head)
1310 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1312 if (!obj || obj->attr_inuse)
1317 memcpy(obj->fs_data.sysfs_name,
1319 sizeof(obj->fs_data.sysfs_name));
1321 obj->sysfs_attr = (struct device_attribute){
1323 .name = obj->fs_data.sysfs_name,
1326 .show = amdgpu_ras_sysfs_read,
1328 sysfs_attr_init(&obj->sysfs_attr.attr);
1330 if (sysfs_add_file_to_group(&adev->dev->kobj,
1331 &obj->sysfs_attr.attr,
1337 obj->attr_inuse = 1;
1342 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1343 struct ras_common_if *head)
1345 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1347 if (!obj || !obj->attr_inuse)
1350 sysfs_remove_file_from_group(&adev->dev->kobj,
1351 &obj->sysfs_attr.attr,
1353 obj->attr_inuse = 0;
1359 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1361 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1362 struct ras_manager *obj, *tmp;
1364 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1365 amdgpu_ras_sysfs_remove(adev, &obj->head);
1368 if (amdgpu_bad_page_threshold != 0)
1369 amdgpu_ras_sysfs_remove_bad_page_node(adev);
1371 amdgpu_ras_sysfs_remove_feature_node(adev);
1378 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1380 * Normally when there is an uncorrectable error, the driver will reset
1381 * the GPU to recover. However, in the event of an unrecoverable error,
1382 * the driver provides an interface to reboot the system automatically
1385 * The following file in debugfs provides that interface:
1386 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1390 * .. code-block:: bash
1392 * echo true > .../ras/auto_reboot
1396 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1398 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1399 struct drm_minor *minor = adev_to_drm(adev)->primary;
1402 dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1403 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev,
1404 &amdgpu_ras_debugfs_ctrl_ops);
1405 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev,
1406 &amdgpu_ras_debugfs_eeprom_ops);
1407 debugfs_create_u32("bad_page_cnt_threshold", 0444, dir,
1408 &con->bad_page_cnt_threshold);
1409 debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled);
1410 debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled);
1411 debugfs_create_file("ras_eeprom_size", S_IRUGO, dir, adev,
1412 &amdgpu_ras_debugfs_eeprom_size_ops);
1413 con->de_ras_eeprom_table = debugfs_create_file("ras_eeprom_table",
1415 &amdgpu_ras_debugfs_eeprom_table_ops);
1416 amdgpu_ras_debugfs_set_ret_size(&con->eeprom_control);
1419 * After one uncorrectable error happens, usually GPU recovery will
1420 * be scheduled. But due to the known problem in GPU recovery failing
1421 * to bring GPU back, below interface provides one direct way to
1422 * user to reboot system automatically in such case within
1423 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1424 * will never be called.
1426 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot);
1429 * User could set this not to clean up hardware's error count register
1430 * of RAS IPs during ras recovery.
1432 debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir,
1433 &con->disable_ras_err_cnt_harvest);
1437 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1438 struct ras_fs_if *head,
1441 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1448 memcpy(obj->fs_data.debugfs_name,
1450 sizeof(obj->fs_data.debugfs_name));
1452 debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir,
1453 obj, &amdgpu_ras_debugfs_ops);
1456 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1458 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1460 struct ras_manager *obj;
1461 struct ras_fs_if fs_info;
1464 * it won't be called in resume path, no need to check
1465 * suspend and gpu reset status
1467 if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con)
1470 dir = amdgpu_ras_debugfs_create_ctrl_node(adev);
1472 list_for_each_entry(obj, &con->head, node) {
1473 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1474 (obj->attr_inuse == 1)) {
1475 sprintf(fs_info.debugfs_name, "%s_err_inject",
1476 get_ras_block_str(&obj->head));
1477 fs_info.head = obj->head;
1478 amdgpu_ras_debugfs_create(adev, &fs_info, dir);
1486 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1487 amdgpu_ras_sysfs_badpages_read, NULL, 0);
1488 static DEVICE_ATTR(features, S_IRUGO,
1489 amdgpu_ras_sysfs_features_read, NULL);
1490 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1492 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1493 struct attribute_group group = {
1494 .name = RAS_FS_NAME,
1496 struct attribute *attrs[] = {
1497 &con->features_attr.attr,
1500 struct bin_attribute *bin_attrs[] = {
1506 /* add features entry */
1507 con->features_attr = dev_attr_features;
1508 group.attrs = attrs;
1509 sysfs_attr_init(attrs[0]);
1511 if (amdgpu_bad_page_threshold != 0) {
1512 /* add bad_page_features entry */
1513 bin_attr_gpu_vram_bad_pages.private = NULL;
1514 con->badpages_attr = bin_attr_gpu_vram_bad_pages;
1515 bin_attrs[0] = &con->badpages_attr;
1516 group.bin_attrs = bin_attrs;
1517 sysfs_bin_attr_init(bin_attrs[0]);
1520 r = sysfs_create_group(&adev->dev->kobj, &group);
1522 dev_err(adev->dev, "Failed to create RAS sysfs group!");
1527 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1529 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1530 struct ras_manager *con_obj, *ip_obj, *tmp;
1532 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1533 list_for_each_entry_safe(con_obj, tmp, &con->head, node) {
1534 ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head);
1540 amdgpu_ras_sysfs_remove_all(adev);
1546 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1548 struct ras_ih_data *data = &obj->ih_data;
1549 struct amdgpu_iv_entry entry;
1551 struct ras_err_data err_data = {0, 0, 0, NULL};
1553 while (data->rptr != data->wptr) {
1555 memcpy(&entry, &data->ring[data->rptr],
1556 data->element_size);
1559 data->rptr = (data->aligned_element_size +
1560 data->rptr) % data->ring_size;
1563 if (amdgpu_ras_is_poison_mode_supported(obj->adev) &&
1564 obj->head.block == AMDGPU_RAS_BLOCK__UMC)
1565 dev_info(obj->adev->dev,
1566 "Poison is created, no user action is needed.\n");
1568 /* Let IP handle its data, maybe we need get the output
1569 * from the callback to udpate the error type/count, etc
1571 ret = data->cb(obj->adev, &err_data, &entry);
1572 /* ue will trigger an interrupt, and in that case
1573 * we need do a reset to recovery the whole system.
1574 * But leave IP do that recovery, here we just dispatch
1577 if (ret == AMDGPU_RAS_SUCCESS) {
1578 /* these counts could be left as 0 if
1579 * some blocks do not count error number
1581 obj->err_data.ue_count += err_data.ue_count;
1582 obj->err_data.ce_count += err_data.ce_count;
1589 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1591 struct ras_ih_data *data =
1592 container_of(work, struct ras_ih_data, ih_work);
1593 struct ras_manager *obj =
1594 container_of(data, struct ras_manager, ih_data);
1596 amdgpu_ras_interrupt_handler(obj);
1599 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1600 struct ras_dispatch_if *info)
1602 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1603 struct ras_ih_data *data = &obj->ih_data;
1608 if (data->inuse == 0)
1611 /* Might be overflow... */
1612 memcpy(&data->ring[data->wptr], info->entry,
1613 data->element_size);
1616 data->wptr = (data->aligned_element_size +
1617 data->wptr) % data->ring_size;
1619 schedule_work(&data->ih_work);
1624 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1625 struct ras_ih_if *info)
1627 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1628 struct ras_ih_data *data;
1633 data = &obj->ih_data;
1634 if (data->inuse == 0)
1637 cancel_work_sync(&data->ih_work);
1640 memset(data, 0, sizeof(*data));
1646 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1647 struct ras_ih_if *info)
1649 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1650 struct ras_ih_data *data;
1653 /* in case we registe the IH before enable ras feature */
1654 obj = amdgpu_ras_create_obj(adev, &info->head);
1660 data = &obj->ih_data;
1661 /* add the callback.etc */
1662 *data = (struct ras_ih_data) {
1665 .element_size = sizeof(struct amdgpu_iv_entry),
1670 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1672 data->aligned_element_size = ALIGN(data->element_size, 8);
1673 /* the ring can store 64 iv entries. */
1674 data->ring_size = 64 * data->aligned_element_size;
1675 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1687 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1689 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1690 struct ras_manager *obj, *tmp;
1692 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1693 struct ras_ih_if info = {
1696 amdgpu_ras_interrupt_remove_handler(adev, &info);
1703 /* traversal all IPs except NBIO to query error counter */
1704 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1706 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1707 struct ras_manager *obj;
1709 if (!adev->ras_enabled || !con)
1712 list_for_each_entry(obj, &con->head, node) {
1713 struct ras_query_if info = {
1718 * PCIE_BIF IP has one different isr by ras controller
1719 * interrupt, the specific ras counter query will be
1720 * done in that isr. So skip such block from common
1721 * sync flood interrupt isr calling.
1723 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1726 amdgpu_ras_query_error_status(adev, &info);
1730 /* Parse RdRspStatus and WrRspStatus */
1731 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
1732 struct ras_query_if *info)
1735 * Only two block need to query read/write
1736 * RspStatus at current state
1738 switch (info->head.block) {
1739 case AMDGPU_RAS_BLOCK__GFX:
1740 if (adev->gfx.ras_funcs &&
1741 adev->gfx.ras_funcs->query_ras_error_status)
1742 adev->gfx.ras_funcs->query_ras_error_status(adev);
1744 case AMDGPU_RAS_BLOCK__MMHUB:
1745 if (adev->mmhub.ras_funcs &&
1746 adev->mmhub.ras_funcs->query_ras_error_status)
1747 adev->mmhub.ras_funcs->query_ras_error_status(adev);
1754 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
1756 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1757 struct ras_manager *obj;
1759 if (!adev->ras_enabled || !con)
1762 list_for_each_entry(obj, &con->head, node) {
1763 struct ras_query_if info = {
1767 amdgpu_ras_error_status_query(adev, &info);
1771 /* recovery begin */
1773 /* return 0 on success.
1774 * caller need free bps.
1776 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1777 struct ras_badpage **bps, unsigned int *count)
1779 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1780 struct ras_err_handler_data *data;
1782 int ret = 0, status;
1784 if (!con || !con->eh_data || !bps || !count)
1787 mutex_lock(&con->recovery_lock);
1788 data = con->eh_data;
1789 if (!data || data->count == 0) {
1795 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1801 for (; i < data->count; i++) {
1802 (*bps)[i] = (struct ras_badpage){
1803 .bp = data->bps[i].retired_page,
1804 .size = AMDGPU_GPU_PAGE_SIZE,
1805 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1807 status = amdgpu_vram_mgr_query_page_status(
1808 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1809 data->bps[i].retired_page);
1810 if (status == -EBUSY)
1811 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1812 else if (status == -ENOENT)
1813 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1816 *count = data->count;
1818 mutex_unlock(&con->recovery_lock);
1822 static void amdgpu_ras_do_recovery(struct work_struct *work)
1824 struct amdgpu_ras *ras =
1825 container_of(work, struct amdgpu_ras, recovery_work);
1826 struct amdgpu_device *remote_adev = NULL;
1827 struct amdgpu_device *adev = ras->adev;
1828 struct list_head device_list, *device_list_handle = NULL;
1830 if (!ras->disable_ras_err_cnt_harvest) {
1831 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
1833 /* Build list of devices to query RAS related errors */
1834 if (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
1835 device_list_handle = &hive->device_list;
1837 INIT_LIST_HEAD(&device_list);
1838 list_add_tail(&adev->gmc.xgmi.head, &device_list);
1839 device_list_handle = &device_list;
1842 list_for_each_entry(remote_adev,
1843 device_list_handle, gmc.xgmi.head) {
1844 amdgpu_ras_query_err_status(remote_adev);
1845 amdgpu_ras_log_on_err_counter(remote_adev);
1848 amdgpu_put_xgmi_hive(hive);
1851 if (amdgpu_device_should_recover_gpu(ras->adev))
1852 amdgpu_device_gpu_recover(ras->adev, NULL);
1853 atomic_set(&ras->in_recovery, 0);
1856 /* alloc/realloc bps array */
1857 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1858 struct ras_err_handler_data *data, int pages)
1860 unsigned int old_space = data->count + data->space_left;
1861 unsigned int new_space = old_space + pages;
1862 unsigned int align_space = ALIGN(new_space, 512);
1863 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1871 memcpy(bps, data->bps,
1872 data->count * sizeof(*data->bps));
1877 data->space_left += align_space - old_space;
1881 /* it deal with vram only. */
1882 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1883 struct eeprom_table_record *bps, int pages)
1885 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1886 struct ras_err_handler_data *data;
1890 if (!con || !con->eh_data || !bps || pages <= 0)
1893 mutex_lock(&con->recovery_lock);
1894 data = con->eh_data;
1898 for (i = 0; i < pages; i++) {
1899 if (amdgpu_ras_check_bad_page_unlock(con,
1900 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT))
1903 if (!data->space_left &&
1904 amdgpu_ras_realloc_eh_data_space(adev, data, 256)) {
1909 amdgpu_vram_mgr_reserve_range(
1910 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1911 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT,
1912 AMDGPU_GPU_PAGE_SIZE);
1914 memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps));
1919 mutex_unlock(&con->recovery_lock);
1925 * write error record array to eeprom, the function should be
1926 * protected by recovery_lock
1928 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1930 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1931 struct ras_err_handler_data *data;
1932 struct amdgpu_ras_eeprom_control *control;
1935 if (!con || !con->eh_data)
1938 control = &con->eeprom_control;
1939 data = con->eh_data;
1940 save_count = data->count - control->ras_num_recs;
1941 /* only new entries are saved */
1942 if (save_count > 0) {
1943 if (amdgpu_ras_eeprom_append(control,
1944 &data->bps[control->ras_num_recs],
1946 dev_err(adev->dev, "Failed to save EEPROM table data!");
1950 dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
1957 * read error record array in eeprom and reserve enough space for
1958 * storing new bad pages
1960 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1962 struct amdgpu_ras_eeprom_control *control =
1963 &adev->psp.ras_context.ras->eeprom_control;
1964 struct eeprom_table_record *bps;
1967 /* no bad page record, skip eeprom access */
1968 if (control->ras_num_recs == 0 || amdgpu_bad_page_threshold == 0)
1971 bps = kcalloc(control->ras_num_recs, sizeof(*bps), GFP_KERNEL);
1975 ret = amdgpu_ras_eeprom_read(control, bps, control->ras_num_recs);
1977 dev_err(adev->dev, "Failed to load EEPROM table records!");
1979 ret = amdgpu_ras_add_bad_pages(adev, bps, control->ras_num_recs);
1985 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
1988 struct ras_err_handler_data *data = con->eh_data;
1991 addr >>= AMDGPU_GPU_PAGE_SHIFT;
1992 for (i = 0; i < data->count; i++)
1993 if (addr == data->bps[i].retired_page)
2000 * check if an address belongs to bad page
2002 * Note: this check is only for umc block
2004 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
2007 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2010 if (!con || !con->eh_data)
2013 mutex_lock(&con->recovery_lock);
2014 ret = amdgpu_ras_check_bad_page_unlock(con, addr);
2015 mutex_unlock(&con->recovery_lock);
2019 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
2022 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2025 * Justification of value bad_page_cnt_threshold in ras structure
2027 * Generally, -1 <= amdgpu_bad_page_threshold <= max record length
2028 * in eeprom, and introduce two scenarios accordingly.
2030 * Bad page retirement enablement:
2031 * - If amdgpu_bad_page_threshold = -1,
2032 * bad_page_cnt_threshold = typical value by formula.
2034 * - When the value from user is 0 < amdgpu_bad_page_threshold <
2035 * max record length in eeprom, use it directly.
2037 * Bad page retirement disablement:
2038 * - If amdgpu_bad_page_threshold = 0, bad page retirement
2039 * functionality is disabled, and bad_page_cnt_threshold will
2043 if (amdgpu_bad_page_threshold < 0) {
2044 u64 val = adev->gmc.mc_vram_size;
2046 do_div(val, RAS_BAD_PAGE_COVER);
2047 con->bad_page_cnt_threshold = min(lower_32_bits(val),
2050 con->bad_page_cnt_threshold = min_t(int, max_count,
2051 amdgpu_bad_page_threshold);
2055 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
2057 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2058 struct ras_err_handler_data **data;
2059 u32 max_eeprom_records_count = 0;
2060 bool exc_err_limit = false;
2066 /* Allow access to RAS EEPROM via debugfs, when the ASIC
2067 * supports RAS and debugfs is enabled, but when
2068 * adev->ras_enabled is unset, i.e. when "ras_enable"
2069 * module parameter is set to 0.
2073 if (!adev->ras_enabled)
2076 data = &con->eh_data;
2077 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
2083 mutex_init(&con->recovery_lock);
2084 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
2085 atomic_set(&con->in_recovery, 0);
2087 max_eeprom_records_count = amdgpu_ras_eeprom_max_record_count();
2088 amdgpu_ras_validate_threshold(adev, max_eeprom_records_count);
2090 /* Todo: During test the SMU might fail to read the eeprom through I2C
2091 * when the GPU is pending on XGMI reset during probe time
2092 * (Mostly after second bus reset), skip it now
2094 if (adev->gmc.xgmi.pending_reset)
2096 ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit);
2098 * This calling fails when exc_err_limit is true or
2101 if (exc_err_limit || ret)
2104 if (con->eeprom_control.ras_num_recs) {
2105 ret = amdgpu_ras_load_bad_pages(adev);
2109 if (adev->smu.ppt_funcs && adev->smu.ppt_funcs->send_hbm_bad_pages_num)
2110 adev->smu.ppt_funcs->send_hbm_bad_pages_num(&adev->smu, con->eeprom_control.ras_num_recs);
2113 #ifdef CONFIG_X86_MCE_AMD
2114 if ((adev->asic_type == CHIP_ALDEBARAN) &&
2115 (adev->gmc.xgmi.connected_to_cpu))
2116 amdgpu_register_bad_pages_mca_notifier(adev);
2121 kfree((*data)->bps);
2123 con->eh_data = NULL;
2125 dev_warn(adev->dev, "Failed to initialize ras recovery! (%d)\n", ret);
2128 * Except error threshold exceeding case, other failure cases in this
2129 * function would not fail amdgpu driver init.
2139 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
2141 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2142 struct ras_err_handler_data *data = con->eh_data;
2144 /* recovery_init failed to init it, fini is useless */
2148 cancel_work_sync(&con->recovery_work);
2150 mutex_lock(&con->recovery_lock);
2151 con->eh_data = NULL;
2154 mutex_unlock(&con->recovery_lock);
2160 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev)
2162 return adev->asic_type == CHIP_VEGA10 ||
2163 adev->asic_type == CHIP_VEGA20 ||
2164 adev->asic_type == CHIP_ARCTURUS ||
2165 adev->asic_type == CHIP_ALDEBARAN ||
2166 adev->asic_type == CHIP_SIENNA_CICHLID;
2170 * this is workaround for vega20 workstation sku,
2171 * force enable gfx ras, ignore vbios gfx ras flag
2172 * due to GC EDC can not write
2174 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev)
2176 struct atom_context *ctx = adev->mode_info.atom_context;
2181 if (strnstr(ctx->vbios_version, "D16406",
2182 sizeof(ctx->vbios_version)) ||
2183 strnstr(ctx->vbios_version, "D36002",
2184 sizeof(ctx->vbios_version)))
2185 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX);
2189 * check hardware's ras ability which will be saved in hw_supported.
2190 * if hardware does not support ras, we can skip some ras initializtion and
2191 * forbid some ras operations from IP.
2192 * if software itself, say boot parameter, limit the ras ability. We still
2193 * need allow IP do some limited operations, like disable. In such case,
2194 * we have to initialize ras as normal. but need check if operation is
2195 * allowed or not in each function.
2197 static void amdgpu_ras_check_supported(struct amdgpu_device *adev)
2199 adev->ras_hw_enabled = adev->ras_enabled = 0;
2201 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
2202 !amdgpu_ras_asic_supported(adev))
2205 if (!adev->gmc.xgmi.connected_to_cpu) {
2206 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
2207 dev_info(adev->dev, "MEM ECC is active.\n");
2208 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC |
2209 1 << AMDGPU_RAS_BLOCK__DF);
2211 dev_info(adev->dev, "MEM ECC is not presented.\n");
2214 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
2215 dev_info(adev->dev, "SRAM ECC is active.\n");
2216 adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
2217 1 << AMDGPU_RAS_BLOCK__DF);
2219 dev_info(adev->dev, "SRAM ECC is not presented.\n");
2222 /* driver only manages a few IP blocks RAS feature
2223 * when GPU is connected cpu through XGMI */
2224 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX |
2225 1 << AMDGPU_RAS_BLOCK__SDMA |
2226 1 << AMDGPU_RAS_BLOCK__MMHUB);
2229 amdgpu_ras_get_quirks(adev);
2231 /* hw_supported needs to be aligned with RAS block mask. */
2232 adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK;
2234 adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 :
2235 adev->ras_hw_enabled & amdgpu_ras_mask;
2238 static void amdgpu_ras_counte_dw(struct work_struct *work)
2240 struct amdgpu_ras *con = container_of(work, struct amdgpu_ras,
2241 ras_counte_delay_work.work);
2242 struct amdgpu_device *adev = con->adev;
2243 struct drm_device *dev = adev_to_drm(adev);
2244 unsigned long ce_count, ue_count;
2247 res = pm_runtime_get_sync(dev->dev);
2251 /* Cache new values.
2253 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2254 atomic_set(&con->ras_ce_count, ce_count);
2255 atomic_set(&con->ras_ue_count, ue_count);
2258 pm_runtime_mark_last_busy(dev->dev);
2260 pm_runtime_put_autosuspend(dev->dev);
2263 int amdgpu_ras_init(struct amdgpu_device *adev)
2265 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2267 bool df_poison, umc_poison;
2272 con = kmalloc(sizeof(struct amdgpu_ras) +
2273 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT +
2274 sizeof(struct ras_manager) * AMDGPU_RAS_MCA_BLOCK_COUNT,
2275 GFP_KERNEL|__GFP_ZERO);
2280 INIT_DELAYED_WORK(&con->ras_counte_delay_work, amdgpu_ras_counte_dw);
2281 atomic_set(&con->ras_ce_count, 0);
2282 atomic_set(&con->ras_ue_count, 0);
2284 con->objs = (struct ras_manager *)(con + 1);
2286 amdgpu_ras_set_context(adev, con);
2288 amdgpu_ras_check_supported(adev);
2290 if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) {
2291 /* set gfx block ras context feature for VEGA20 Gaming
2292 * send ras disable cmd to ras ta during ras late init.
2294 if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) {
2295 con->features |= BIT(AMDGPU_RAS_BLOCK__GFX);
2305 INIT_LIST_HEAD(&con->head);
2306 /* Might need get this flag from vbios. */
2307 con->flags = RAS_DEFAULT_FLAGS;
2309 /* initialize nbio ras function ahead of any other
2310 * ras functions so hardware fatal error interrupt
2311 * can be enabled as early as possible */
2312 switch (adev->asic_type) {
2315 case CHIP_ALDEBARAN:
2316 if (!adev->gmc.xgmi.connected_to_cpu)
2317 adev->nbio.ras_funcs = &nbio_v7_4_ras_funcs;
2320 /* nbio ras is not available */
2324 if (adev->nbio.ras_funcs &&
2325 adev->nbio.ras_funcs->init_ras_controller_interrupt) {
2326 r = adev->nbio.ras_funcs->init_ras_controller_interrupt(adev);
2331 if (adev->nbio.ras_funcs &&
2332 adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt) {
2333 r = adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt(adev);
2338 /* Init poison supported flag, the default value is false */
2339 if (adev->df.funcs &&
2340 adev->df.funcs->query_ras_poison_mode &&
2341 adev->umc.ras_funcs &&
2342 adev->umc.ras_funcs->query_ras_poison_mode) {
2344 adev->df.funcs->query_ras_poison_mode(adev);
2346 adev->umc.ras_funcs->query_ras_poison_mode(adev);
2347 /* Only poison is set in both DF and UMC, we can support it */
2348 if (df_poison && umc_poison)
2349 con->poison_supported = true;
2350 else if (df_poison != umc_poison)
2351 dev_warn(adev->dev, "Poison setting is inconsistent in DF/UMC(%d:%d)!\n",
2352 df_poison, umc_poison);
2355 if (amdgpu_ras_fs_init(adev)) {
2360 dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
2361 "hardware ability[%x] ras_mask[%x]\n",
2362 adev->ras_hw_enabled, adev->ras_enabled);
2366 amdgpu_ras_set_context(adev, NULL);
2372 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev)
2374 if (adev->gmc.xgmi.connected_to_cpu)
2379 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev,
2380 struct ras_common_if *ras_block)
2382 struct ras_query_if info = {
2386 if (!amdgpu_persistent_edc_harvesting_supported(adev))
2389 if (amdgpu_ras_query_error_status(adev, &info) != 0)
2390 DRM_WARN("RAS init harvest failure");
2392 if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0)
2393 DRM_WARN("RAS init harvest reset failure");
2398 bool amdgpu_ras_is_poison_mode_supported(struct amdgpu_device *adev)
2400 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2405 return con->poison_supported;
2408 /* helper function to handle common stuff in ip late init phase */
2409 int amdgpu_ras_late_init(struct amdgpu_device *adev,
2410 struct ras_common_if *ras_block,
2411 struct ras_fs_if *fs_info,
2412 struct ras_ih_if *ih_info)
2414 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2415 unsigned long ue_count, ce_count;
2418 /* disable RAS feature per IP block if it is not supported */
2419 if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
2420 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
2424 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
2426 if (adev->in_suspend || amdgpu_in_reset(adev)) {
2427 /* in resume phase, if fail to enable ras,
2428 * clean up all ras fs nodes, and disable ras */
2434 /* check for errors on warm reset edc persisant supported ASIC */
2435 amdgpu_persistent_edc_harvesting(adev, ras_block);
2437 /* in resume phase, no need to create ras fs node */
2438 if (adev->in_suspend || amdgpu_in_reset(adev))
2442 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
2447 r = amdgpu_ras_sysfs_create(adev, fs_info);
2451 /* Those are the cached values at init.
2453 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2454 atomic_set(&con->ras_ce_count, ce_count);
2455 atomic_set(&con->ras_ue_count, ue_count);
2460 amdgpu_ras_sysfs_remove(adev, ras_block);
2463 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2465 amdgpu_ras_feature_enable(adev, ras_block, 0);
2469 /* helper function to remove ras fs node and interrupt handler */
2470 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
2471 struct ras_common_if *ras_block,
2472 struct ras_ih_if *ih_info)
2474 if (!ras_block || !ih_info)
2477 amdgpu_ras_sysfs_remove(adev, ras_block);
2479 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2480 amdgpu_ras_feature_enable(adev, ras_block, 0);
2483 /* do some init work after IP late init as dependence.
2484 * and it runs in resume/gpu reset/booting up cases.
2486 void amdgpu_ras_resume(struct amdgpu_device *adev)
2488 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2489 struct ras_manager *obj, *tmp;
2491 if (!adev->ras_enabled || !con) {
2492 /* clean ras context for VEGA20 Gaming after send ras disable cmd */
2493 amdgpu_release_ras_context(adev);
2498 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2499 /* Set up all other IPs which are not implemented. There is a
2500 * tricky thing that IP's actual ras error type should be
2501 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2502 * ERROR_NONE make sense anyway.
2504 amdgpu_ras_enable_all_features(adev, 1);
2506 /* We enable ras on all hw_supported block, but as boot
2507 * parameter might disable some of them and one or more IP has
2508 * not implemented yet. So we disable them on behalf.
2510 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2511 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2512 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2513 /* there should be no any reference. */
2514 WARN_ON(alive_obj(obj));
2520 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2522 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2524 if (!adev->ras_enabled || !con)
2527 amdgpu_ras_disable_all_features(adev, 0);
2528 /* Make sure all ras objects are disabled. */
2530 amdgpu_ras_disable_all_features(adev, 1);
2533 /* do some fini work before IP fini as dependence */
2534 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2536 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2538 if (!adev->ras_enabled || !con)
2542 /* Need disable ras on all IPs here before ip [hw/sw]fini */
2543 amdgpu_ras_disable_all_features(adev, 0);
2544 amdgpu_ras_recovery_fini(adev);
2548 int amdgpu_ras_fini(struct amdgpu_device *adev)
2550 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2552 if (!adev->ras_enabled || !con)
2555 amdgpu_ras_fs_fini(adev);
2556 amdgpu_ras_interrupt_remove_all(adev);
2558 WARN(con->features, "Feature mask is not cleared");
2561 amdgpu_ras_disable_all_features(adev, 1);
2563 cancel_delayed_work_sync(&con->ras_counte_delay_work);
2565 amdgpu_ras_set_context(adev, NULL);
2571 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2573 amdgpu_ras_check_supported(adev);
2574 if (!adev->ras_hw_enabled)
2577 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2578 dev_info(adev->dev, "uncorrectable hardware error"
2579 "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2581 amdgpu_ras_reset_gpu(adev);
2585 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
2587 if (adev->asic_type == CHIP_VEGA20 &&
2588 adev->pm.fw_version <= 0x283400) {
2589 return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
2590 amdgpu_ras_intr_triggered();
2596 void amdgpu_release_ras_context(struct amdgpu_device *adev)
2598 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2603 if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) {
2604 con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX);
2605 amdgpu_ras_set_context(adev, NULL);
2610 #ifdef CONFIG_X86_MCE_AMD
2611 static struct amdgpu_device *find_adev(uint32_t node_id)
2614 struct amdgpu_device *adev = NULL;
2616 for (i = 0; i < mce_adev_list.num_gpu; i++) {
2617 adev = mce_adev_list.devs[i];
2619 if (adev && adev->gmc.xgmi.connected_to_cpu &&
2620 adev->gmc.xgmi.physical_node_id == node_id)
2628 #define GET_MCA_IPID_GPUID(m) (((m) >> 44) & 0xF)
2629 #define GET_UMC_INST(m) (((m) >> 21) & 0x7)
2630 #define GET_CHAN_INDEX(m) ((((m) >> 12) & 0x3) | (((m) >> 18) & 0x4))
2631 #define GPU_ID_OFFSET 8
2633 static int amdgpu_bad_page_notifier(struct notifier_block *nb,
2634 unsigned long val, void *data)
2636 struct mce *m = (struct mce *)data;
2637 struct amdgpu_device *adev = NULL;
2638 uint32_t gpu_id = 0;
2639 uint32_t umc_inst = 0;
2640 uint32_t ch_inst, channel_index = 0;
2641 struct ras_err_data err_data = {0, 0, 0, NULL};
2642 struct eeprom_table_record err_rec;
2643 uint64_t retired_page;
2646 * If the error was generated in UMC_V2, which belongs to GPU UMCs,
2647 * and error occurred in DramECC (Extended error code = 0) then only
2648 * process the error, else bail out.
2650 if (!m || !((smca_get_bank_type(m->bank) == SMCA_UMC_V2) &&
2651 (XEC(m->status, 0x3f) == 0x0)))
2655 * If it is correctable error, return.
2657 if (mce_is_correctable(m))
2661 * GPU Id is offset by GPU_ID_OFFSET in MCA_IPID_UMC register.
2663 gpu_id = GET_MCA_IPID_GPUID(m->ipid) - GPU_ID_OFFSET;
2665 adev = find_adev(gpu_id);
2667 DRM_WARN("%s: Unable to find adev for gpu_id: %d\n", __func__,
2673 * If it is uncorrectable error, then find out UMC instance and
2676 umc_inst = GET_UMC_INST(m->ipid);
2677 ch_inst = GET_CHAN_INDEX(m->ipid);
2679 dev_info(adev->dev, "Uncorrectable error detected in UMC inst: %d, chan_idx: %d",
2682 memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
2685 * Translate UMC channel address to Physical address
2688 adev->umc.channel_idx_tbl[umc_inst * adev->umc.channel_inst_num
2691 retired_page = ADDR_OF_8KB_BLOCK(m->addr) |
2692 ADDR_OF_256B_BLOCK(channel_index) |
2693 OFFSET_IN_256B_BLOCK(m->addr);
2695 err_rec.address = m->addr;
2696 err_rec.retired_page = retired_page >> AMDGPU_GPU_PAGE_SHIFT;
2697 err_rec.ts = (uint64_t)ktime_get_real_seconds();
2698 err_rec.err_type = AMDGPU_RAS_EEPROM_ERR_NON_RECOVERABLE;
2700 err_rec.mem_channel = channel_index;
2701 err_rec.mcumc_id = umc_inst;
2703 err_data.err_addr = &err_rec;
2704 err_data.err_addr_cnt = 1;
2706 if (amdgpu_bad_page_threshold != 0) {
2707 amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
2708 err_data.err_addr_cnt);
2709 amdgpu_ras_save_bad_pages(adev);
2715 static struct notifier_block amdgpu_bad_page_nb = {
2716 .notifier_call = amdgpu_bad_page_notifier,
2717 .priority = MCE_PRIO_UC,
2720 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev)
2723 * Add the adev to the mce_adev_list.
2724 * During mode2 reset, amdgpu device is temporarily
2725 * removed from the mgpu_info list which can cause
2726 * page retirement to fail.
2727 * Use this list instead of mgpu_info to find the amdgpu
2728 * device on which the UMC error was reported.
2730 mce_adev_list.devs[mce_adev_list.num_gpu++] = adev;
2733 * Register the x86 notifier only once
2734 * with MCE subsystem.
2736 if (notifier_registered == false) {
2737 mce_register_decode_chain(&amdgpu_bad_page_nb);
2738 notifier_registered = true;