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(void);
118 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
120 if (adev && amdgpu_ras_get_context(adev))
121 amdgpu_ras_get_context(adev)->error_query_ready = ready;
124 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
126 if (adev && amdgpu_ras_get_context(adev))
127 return amdgpu_ras_get_context(adev)->error_query_ready;
132 static int amdgpu_reserve_page_direct(struct amdgpu_device *adev, uint64_t address)
134 struct ras_err_data err_data = {0, 0, 0, NULL};
135 struct eeprom_table_record err_rec;
137 if ((address >= adev->gmc.mc_vram_size) ||
138 (address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
140 "RAS WARN: input address 0x%llx is invalid.\n",
145 if (amdgpu_ras_check_bad_page(adev, address)) {
147 "RAS WARN: 0x%llx has already been marked as bad page!\n",
152 memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
154 err_rec.address = address;
155 err_rec.retired_page = address >> AMDGPU_GPU_PAGE_SHIFT;
156 err_rec.ts = (uint64_t)ktime_get_real_seconds();
157 err_rec.err_type = AMDGPU_RAS_EEPROM_ERR_NON_RECOVERABLE;
159 err_data.err_addr = &err_rec;
160 err_data.err_addr_cnt = 1;
162 if (amdgpu_bad_page_threshold != 0) {
163 amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
164 err_data.err_addr_cnt);
165 amdgpu_ras_save_bad_pages(adev);
168 dev_warn(adev->dev, "WARNING: THIS IS ONLY FOR TEST PURPOSES AND WILL CORRUPT RAS EEPROM\n");
169 dev_warn(adev->dev, "Clear EEPROM:\n");
170 dev_warn(adev->dev, " echo 1 > /sys/kernel/debug/dri/0/ras/ras_eeprom_reset\n");
175 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
176 size_t size, loff_t *pos)
178 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
179 struct ras_query_if info = {
185 if (amdgpu_ras_query_error_status(obj->adev, &info))
188 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
190 "ce", info.ce_count);
195 s = min_t(u64, s, size);
198 if (copy_to_user(buf, &val[*pos], s))
206 static const struct file_operations amdgpu_ras_debugfs_ops = {
207 .owner = THIS_MODULE,
208 .read = amdgpu_ras_debugfs_read,
210 .llseek = default_llseek
213 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
217 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
219 if (strcmp(name, ras_block_string[i]) == 0)
225 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
226 const char __user *buf, size_t size,
227 loff_t *pos, struct ras_debug_if *data)
229 ssize_t s = min_t(u64, 64, size);
242 memset(str, 0, sizeof(str));
243 memset(data, 0, sizeof(*data));
245 if (copy_from_user(str, buf, s))
248 if (sscanf(str, "disable %32s", block_name) == 1)
250 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
252 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
254 else if (strstr(str, "retire_page") != NULL)
256 else if (str[0] && str[1] && str[2] && str[3])
257 /* ascii string, but commands are not matched. */
262 if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
263 sscanf(str, "%*s %llu", &address) != 1)
267 data->inject.address = address;
272 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
275 data->head.block = block_id;
276 /* only ue and ce errors are supported */
277 if (!memcmp("ue", err, 2))
278 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
279 else if (!memcmp("ce", err, 2))
280 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
287 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
288 &sub_block, &address, &value) != 3 &&
289 sscanf(str, "%*s %*s %*s %u %llu %llu",
290 &sub_block, &address, &value) != 3)
292 data->head.sub_block_index = sub_block;
293 data->inject.address = address;
294 data->inject.value = value;
297 if (size < sizeof(*data))
300 if (copy_from_user(data, buf, sizeof(*data)))
308 * DOC: AMDGPU RAS debugfs control interface
310 * The control interface accepts struct ras_debug_if which has two members.
312 * First member: ras_debug_if::head or ras_debug_if::inject.
314 * head is used to indicate which IP block will be under control.
316 * head has four members, they are block, type, sub_block_index, name.
317 * block: which IP will be under control.
318 * type: what kind of error will be enabled/disabled/injected.
319 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
320 * name: the name of IP.
322 * inject has two more members than head, they are address, value.
323 * As their names indicate, inject operation will write the
324 * value to the address.
326 * The second member: struct ras_debug_if::op.
327 * It has three kinds of operations.
329 * - 0: disable RAS on the block. Take ::head as its data.
330 * - 1: enable RAS on the block. Take ::head as its data.
331 * - 2: inject errors on the block. Take ::inject as its data.
333 * How to use the interface?
337 * Copy the struct ras_debug_if in your code and initialize it.
338 * Write the struct to the control interface.
342 * .. code-block:: bash
344 * echo "disable <block>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
345 * echo "enable <block> <error>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
346 * echo "inject <block> <error> <sub-block> <address> <value> > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
348 * Where N, is the card which you want to affect.
350 * "disable" requires only the block.
351 * "enable" requires the block and error type.
352 * "inject" requires the block, error type, address, and value.
354 * The block is one of: umc, sdma, gfx, etc.
355 * see ras_block_string[] for details
357 * The error type is one of: ue, ce, where,
358 * ue is multi-uncorrectable
359 * ce is single-correctable
361 * The sub-block is a the sub-block index, pass 0 if there is no sub-block.
362 * The address and value are hexadecimal numbers, leading 0x is optional.
366 * .. code-block:: bash
368 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
369 * echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
370 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
372 * How to check the result of the operation?
374 * To check disable/enable, see "ras" features at,
375 * /sys/class/drm/card[0/1/2...]/device/ras/features
377 * To check inject, see the corresponding error count at,
378 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx|sdma|umc|...]_err_count
381 * Operations are only allowed on blocks which are supported.
382 * Check the "ras" mask at /sys/module/amdgpu/parameters/ras_mask
383 * to see which blocks support RAS on a particular asic.
386 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f,
387 const char __user *buf,
388 size_t size, loff_t *pos)
390 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
391 struct ras_debug_if data;
394 if (!amdgpu_ras_get_error_query_ready(adev)) {
395 dev_warn(adev->dev, "RAS WARN: error injection "
396 "currently inaccessible\n");
400 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
405 ret = amdgpu_reserve_page_direct(adev, data.inject.address);
412 if (!amdgpu_ras_is_supported(adev, data.head.block))
417 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
420 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
423 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
424 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
425 dev_warn(adev->dev, "RAS WARN: input address "
426 "0x%llx is invalid.",
427 data.inject.address);
432 /* umc ce/ue error injection for a bad page is not allowed */
433 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
434 amdgpu_ras_check_bad_page(adev, data.inject.address)) {
435 dev_warn(adev->dev, "RAS WARN: inject: 0x%llx has "
436 "already been marked as bad!\n",
437 data.inject.address);
441 /* data.inject.address is offset instead of absolute gpu address */
442 ret = amdgpu_ras_error_inject(adev, &data.inject);
456 * DOC: AMDGPU RAS debugfs EEPROM table reset interface
458 * Some boards contain an EEPROM which is used to persistently store a list of
459 * bad pages which experiences ECC errors in vram. This interface provides
460 * a way to reset the EEPROM, e.g., after testing error injection.
464 * .. code-block:: bash
466 * echo 1 > ../ras/ras_eeprom_reset
468 * will reset EEPROM table to 0 entries.
471 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f,
472 const char __user *buf,
473 size_t size, loff_t *pos)
475 struct amdgpu_device *adev =
476 (struct amdgpu_device *)file_inode(f)->i_private;
479 ret = amdgpu_ras_eeprom_reset_table(
480 &(amdgpu_ras_get_context(adev)->eeprom_control));
483 /* Something was written to EEPROM.
485 amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS;
492 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
493 .owner = THIS_MODULE,
495 .write = amdgpu_ras_debugfs_ctrl_write,
496 .llseek = default_llseek
499 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
500 .owner = THIS_MODULE,
502 .write = amdgpu_ras_debugfs_eeprom_write,
503 .llseek = default_llseek
507 * DOC: AMDGPU RAS sysfs Error Count Interface
509 * It allows the user to read the error count for each IP block on the gpu through
510 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
512 * It outputs the multiple lines which report the uncorrected (ue) and corrected
515 * The format of one line is below,
521 * .. code-block:: bash
527 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
528 struct device_attribute *attr, char *buf)
530 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
531 struct ras_query_if info = {
535 if (!amdgpu_ras_get_error_query_ready(obj->adev))
536 return sysfs_emit(buf, "Query currently inaccessible\n");
538 if (amdgpu_ras_query_error_status(obj->adev, &info))
541 if (obj->adev->asic_type == CHIP_ALDEBARAN) {
542 if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
543 DRM_WARN("Failed to reset error counter and error status");
546 return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count,
547 "ce", info.ce_count);
552 #define get_obj(obj) do { (obj)->use++; } while (0)
553 #define alive_obj(obj) ((obj)->use)
555 static inline void put_obj(struct ras_manager *obj)
557 if (obj && (--obj->use == 0))
558 list_del(&obj->node);
559 if (obj && (obj->use < 0))
560 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", get_ras_block_str(&obj->head));
563 /* make one obj and return it. */
564 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
565 struct ras_common_if *head)
567 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
568 struct ras_manager *obj;
570 if (!adev->ras_enabled || !con)
573 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
576 if (head->block == AMDGPU_RAS_BLOCK__MCA) {
577 if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
580 obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
582 obj = &con->objs[head->block];
584 /* already exist. return obj? */
590 list_add(&obj->node, &con->head);
596 /* return an obj equal to head, or the first when head is NULL */
597 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
598 struct ras_common_if *head)
600 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
601 struct ras_manager *obj;
604 if (!adev->ras_enabled || !con)
608 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
611 if (head->block == AMDGPU_RAS_BLOCK__MCA) {
612 if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
615 obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
617 obj = &con->objs[head->block];
622 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT + AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
633 /* feature ctl begin */
634 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
635 struct ras_common_if *head)
637 return adev->ras_hw_enabled & BIT(head->block);
640 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
641 struct ras_common_if *head)
643 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
645 return con->features & BIT(head->block);
649 * if obj is not created, then create one.
650 * set feature enable flag.
652 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
653 struct ras_common_if *head, int enable)
655 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
656 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
658 /* If hardware does not support ras, then do not create obj.
659 * But if hardware support ras, we can create the obj.
660 * Ras framework checks con->hw_supported to see if it need do
661 * corresponding initialization.
662 * IP checks con->support to see if it need disable ras.
664 if (!amdgpu_ras_is_feature_allowed(adev, head))
669 obj = amdgpu_ras_create_obj(adev, head);
673 /* In case we create obj somewhere else */
676 con->features |= BIT(head->block);
678 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
679 con->features &= ~BIT(head->block);
687 /* wrapper of psp_ras_enable_features */
688 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
689 struct ras_common_if *head, bool enable)
691 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
692 union ta_ras_cmd_input *info;
698 info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
703 info->disable_features = (struct ta_ras_disable_features_input) {
704 .block_id = amdgpu_ras_block_to_ta(head->block),
705 .error_type = amdgpu_ras_error_to_ta(head->type),
708 info->enable_features = (struct ta_ras_enable_features_input) {
709 .block_id = amdgpu_ras_block_to_ta(head->block),
710 .error_type = amdgpu_ras_error_to_ta(head->type),
714 /* Do not enable if it is not allowed. */
715 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
717 if (!amdgpu_ras_intr_triggered()) {
718 ret = psp_ras_enable_features(&adev->psp, info, enable);
720 dev_err(adev->dev, "ras %s %s failed poison:%d ret:%d\n",
721 enable ? "enable":"disable",
722 get_ras_block_str(head),
723 amdgpu_ras_is_poison_mode_supported(adev), ret);
729 __amdgpu_ras_feature_enable(adev, head, enable);
736 /* Only used in device probe stage and called only once. */
737 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
738 struct ras_common_if *head, bool enable)
740 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
746 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
748 /* There is no harm to issue a ras TA cmd regardless of
749 * the currecnt ras state.
750 * If current state == target state, it will do nothing
751 * But sometimes it requests driver to reset and repost
752 * with error code -EAGAIN.
754 ret = amdgpu_ras_feature_enable(adev, head, 1);
755 /* With old ras TA, we might fail to enable ras.
756 * Log it and just setup the object.
757 * TODO need remove this WA in the future.
759 if (ret == -EINVAL) {
760 ret = __amdgpu_ras_feature_enable(adev, head, 1);
763 "RAS INFO: %s setup object\n",
764 get_ras_block_str(head));
767 /* setup the object then issue a ras TA disable cmd.*/
768 ret = __amdgpu_ras_feature_enable(adev, head, 1);
772 /* gfx block ras dsiable cmd must send to ras-ta */
773 if (head->block == AMDGPU_RAS_BLOCK__GFX)
774 con->features |= BIT(head->block);
776 ret = amdgpu_ras_feature_enable(adev, head, 0);
778 /* clean gfx block ras features flag */
779 if (adev->ras_enabled && head->block == AMDGPU_RAS_BLOCK__GFX)
780 con->features &= ~BIT(head->block);
783 ret = amdgpu_ras_feature_enable(adev, head, enable);
788 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
791 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
792 struct ras_manager *obj, *tmp;
794 list_for_each_entry_safe(obj, tmp, &con->head, node) {
796 * aka just release the obj and corresponding flags
799 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
802 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
807 return con->features;
810 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
813 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
815 const enum amdgpu_ras_error_type default_ras_type = AMDGPU_RAS_ERROR__NONE;
817 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
818 struct ras_common_if head = {
820 .type = default_ras_type,
821 .sub_block_index = 0,
824 if (i == AMDGPU_RAS_BLOCK__MCA)
829 * bypass psp. vbios enable ras for us.
830 * so just create the obj
832 if (__amdgpu_ras_feature_enable(adev, &head, 1))
835 if (amdgpu_ras_feature_enable(adev, &head, 1))
840 for (i = 0; i < AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
841 struct ras_common_if head = {
842 .block = AMDGPU_RAS_BLOCK__MCA,
843 .type = default_ras_type,
844 .sub_block_index = i,
849 * bypass psp. vbios enable ras for us.
850 * so just create the obj
852 if (__amdgpu_ras_feature_enable(adev, &head, 1))
855 if (amdgpu_ras_feature_enable(adev, &head, 1))
860 return con->features;
862 /* feature ctl end */
865 void amdgpu_ras_mca_query_error_status(struct amdgpu_device *adev,
866 struct ras_common_if *ras_block,
867 struct ras_err_data *err_data)
869 switch (ras_block->sub_block_index) {
870 case AMDGPU_RAS_MCA_BLOCK__MP0:
871 if (adev->mca.mp0.ras_funcs &&
872 adev->mca.mp0.ras_funcs->query_ras_error_count)
873 adev->mca.mp0.ras_funcs->query_ras_error_count(adev, &err_data);
875 case AMDGPU_RAS_MCA_BLOCK__MP1:
876 if (adev->mca.mp1.ras_funcs &&
877 adev->mca.mp1.ras_funcs->query_ras_error_count)
878 adev->mca.mp1.ras_funcs->query_ras_error_count(adev, &err_data);
880 case AMDGPU_RAS_MCA_BLOCK__MPIO:
881 if (adev->mca.mpio.ras_funcs &&
882 adev->mca.mpio.ras_funcs->query_ras_error_count)
883 adev->mca.mpio.ras_funcs->query_ras_error_count(adev, &err_data);
890 /* query/inject/cure begin */
891 int amdgpu_ras_query_error_status(struct amdgpu_device *adev,
892 struct ras_query_if *info)
894 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
895 struct ras_err_data err_data = {0, 0, 0, NULL};
901 switch (info->head.block) {
902 case AMDGPU_RAS_BLOCK__UMC:
903 if (adev->umc.ras_funcs &&
904 adev->umc.ras_funcs->query_ras_error_count)
905 adev->umc.ras_funcs->query_ras_error_count(adev, &err_data);
906 /* umc query_ras_error_address is also responsible for clearing
909 if (adev->umc.ras_funcs &&
910 adev->umc.ras_funcs->query_ras_error_address)
911 adev->umc.ras_funcs->query_ras_error_address(adev, &err_data);
913 case AMDGPU_RAS_BLOCK__SDMA:
914 if (adev->sdma.funcs->query_ras_error_count) {
915 for (i = 0; i < adev->sdma.num_instances; i++)
916 adev->sdma.funcs->query_ras_error_count(adev, i,
920 case AMDGPU_RAS_BLOCK__GFX:
921 if (adev->gfx.ras_funcs &&
922 adev->gfx.ras_funcs->query_ras_error_count)
923 adev->gfx.ras_funcs->query_ras_error_count(adev, &err_data);
925 if (adev->gfx.ras_funcs &&
926 adev->gfx.ras_funcs->query_ras_error_status)
927 adev->gfx.ras_funcs->query_ras_error_status(adev);
929 case AMDGPU_RAS_BLOCK__MMHUB:
930 if (adev->mmhub.ras_funcs &&
931 adev->mmhub.ras_funcs->query_ras_error_count)
932 adev->mmhub.ras_funcs->query_ras_error_count(adev, &err_data);
934 if (adev->mmhub.ras_funcs &&
935 adev->mmhub.ras_funcs->query_ras_error_status)
936 adev->mmhub.ras_funcs->query_ras_error_status(adev);
938 case AMDGPU_RAS_BLOCK__PCIE_BIF:
939 if (adev->nbio.ras_funcs &&
940 adev->nbio.ras_funcs->query_ras_error_count)
941 adev->nbio.ras_funcs->query_ras_error_count(adev, &err_data);
943 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
944 if (adev->gmc.xgmi.ras_funcs &&
945 adev->gmc.xgmi.ras_funcs->query_ras_error_count)
946 adev->gmc.xgmi.ras_funcs->query_ras_error_count(adev, &err_data);
948 case AMDGPU_RAS_BLOCK__HDP:
949 if (adev->hdp.ras_funcs &&
950 adev->hdp.ras_funcs->query_ras_error_count)
951 adev->hdp.ras_funcs->query_ras_error_count(adev, &err_data);
953 case AMDGPU_RAS_BLOCK__MCA:
954 amdgpu_ras_mca_query_error_status(adev, &info->head, &err_data);
960 obj->err_data.ue_count += err_data.ue_count;
961 obj->err_data.ce_count += err_data.ce_count;
963 info->ue_count = obj->err_data.ue_count;
964 info->ce_count = obj->err_data.ce_count;
966 if (err_data.ce_count) {
967 if (adev->smuio.funcs &&
968 adev->smuio.funcs->get_socket_id &&
969 adev->smuio.funcs->get_die_id) {
970 dev_info(adev->dev, "socket: %d, die: %d "
971 "%ld correctable hardware errors "
972 "detected in %s block, no user "
973 "action is needed.\n",
974 adev->smuio.funcs->get_socket_id(adev),
975 adev->smuio.funcs->get_die_id(adev),
976 obj->err_data.ce_count,
977 get_ras_block_str(&info->head));
979 dev_info(adev->dev, "%ld correctable hardware errors "
980 "detected in %s block, no user "
981 "action is needed.\n",
982 obj->err_data.ce_count,
983 get_ras_block_str(&info->head));
986 if (err_data.ue_count) {
987 if (adev->smuio.funcs &&
988 adev->smuio.funcs->get_socket_id &&
989 adev->smuio.funcs->get_die_id) {
990 dev_info(adev->dev, "socket: %d, die: %d "
991 "%ld uncorrectable hardware errors "
992 "detected in %s block\n",
993 adev->smuio.funcs->get_socket_id(adev),
994 adev->smuio.funcs->get_die_id(adev),
995 obj->err_data.ue_count,
996 get_ras_block_str(&info->head));
998 dev_info(adev->dev, "%ld uncorrectable hardware errors "
999 "detected in %s block\n",
1000 obj->err_data.ue_count,
1001 get_ras_block_str(&info->head));
1005 if (!amdgpu_persistent_edc_harvesting_supported(adev))
1006 amdgpu_ras_reset_error_status(adev, info->head.block);
1011 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev,
1012 enum amdgpu_ras_block block)
1014 if (!amdgpu_ras_is_supported(adev, block))
1018 case AMDGPU_RAS_BLOCK__GFX:
1019 if (adev->gfx.ras_funcs &&
1020 adev->gfx.ras_funcs->reset_ras_error_count)
1021 adev->gfx.ras_funcs->reset_ras_error_count(adev);
1023 if (adev->gfx.ras_funcs &&
1024 adev->gfx.ras_funcs->reset_ras_error_status)
1025 adev->gfx.ras_funcs->reset_ras_error_status(adev);
1027 case AMDGPU_RAS_BLOCK__MMHUB:
1028 if (adev->mmhub.ras_funcs &&
1029 adev->mmhub.ras_funcs->reset_ras_error_count)
1030 adev->mmhub.ras_funcs->reset_ras_error_count(adev);
1032 if (adev->mmhub.ras_funcs &&
1033 adev->mmhub.ras_funcs->reset_ras_error_status)
1034 adev->mmhub.ras_funcs->reset_ras_error_status(adev);
1036 case AMDGPU_RAS_BLOCK__SDMA:
1037 if (adev->sdma.funcs->reset_ras_error_count)
1038 adev->sdma.funcs->reset_ras_error_count(adev);
1040 case AMDGPU_RAS_BLOCK__HDP:
1041 if (adev->hdp.ras_funcs &&
1042 adev->hdp.ras_funcs->reset_ras_error_count)
1043 adev->hdp.ras_funcs->reset_ras_error_count(adev);
1052 /* Trigger XGMI/WAFL error */
1053 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev,
1054 struct ta_ras_trigger_error_input *block_info)
1058 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
1059 dev_warn(adev->dev, "Failed to disallow df cstate");
1061 if (amdgpu_dpm_allow_xgmi_power_down(adev, false))
1062 dev_warn(adev->dev, "Failed to disallow XGMI power down");
1064 ret = psp_ras_trigger_error(&adev->psp, block_info);
1066 if (amdgpu_ras_intr_triggered())
1069 if (amdgpu_dpm_allow_xgmi_power_down(adev, true))
1070 dev_warn(adev->dev, "Failed to allow XGMI power down");
1072 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW))
1073 dev_warn(adev->dev, "Failed to allow df cstate");
1078 /* wrapper of psp_ras_trigger_error */
1079 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
1080 struct ras_inject_if *info)
1082 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1083 struct ta_ras_trigger_error_input block_info = {
1084 .block_id = amdgpu_ras_block_to_ta(info->head.block),
1085 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
1086 .sub_block_index = info->head.sub_block_index,
1087 .address = info->address,
1088 .value = info->value,
1095 /* Calculate XGMI relative offset */
1096 if (adev->gmc.xgmi.num_physical_nodes > 1) {
1097 block_info.address =
1098 amdgpu_xgmi_get_relative_phy_addr(adev,
1099 block_info.address);
1102 switch (info->head.block) {
1103 case AMDGPU_RAS_BLOCK__GFX:
1104 if (adev->gfx.ras_funcs &&
1105 adev->gfx.ras_funcs->ras_error_inject)
1106 ret = adev->gfx.ras_funcs->ras_error_inject(adev, info);
1110 case AMDGPU_RAS_BLOCK__UMC:
1111 case AMDGPU_RAS_BLOCK__SDMA:
1112 case AMDGPU_RAS_BLOCK__MMHUB:
1113 case AMDGPU_RAS_BLOCK__PCIE_BIF:
1114 case AMDGPU_RAS_BLOCK__MCA:
1115 ret = psp_ras_trigger_error(&adev->psp, &block_info);
1117 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
1118 ret = amdgpu_ras_error_inject_xgmi(adev, &block_info);
1121 dev_info(adev->dev, "%s error injection is not supported yet\n",
1122 get_ras_block_str(&info->head));
1127 dev_err(adev->dev, "ras inject %s failed %d\n",
1128 get_ras_block_str(&info->head), ret);
1134 * amdgpu_ras_query_error_count -- Get error counts of all IPs
1135 * adev: pointer to AMD GPU device
1136 * ce_count: pointer to an integer to be set to the count of correctible errors.
1137 * ue_count: pointer to an integer to be set to the count of uncorrectible
1140 * If set, @ce_count or @ue_count, count and return the corresponding
1141 * error counts in those integer pointers. Return 0 if the device
1142 * supports RAS. Return -EOPNOTSUPP if the device doesn't support RAS.
1144 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
1145 unsigned long *ce_count,
1146 unsigned long *ue_count)
1148 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1149 struct ras_manager *obj;
1150 unsigned long ce, ue;
1152 if (!adev->ras_enabled || !con)
1155 /* Don't count since no reporting.
1157 if (!ce_count && !ue_count)
1162 list_for_each_entry(obj, &con->head, node) {
1163 struct ras_query_if info = {
1168 res = amdgpu_ras_query_error_status(adev, &info);
1172 ce += info.ce_count;
1173 ue += info.ue_count;
1184 /* query/inject/cure end */
1189 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1190 struct ras_badpage **bps, unsigned int *count);
1192 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
1195 case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
1197 case AMDGPU_RAS_RETIRE_PAGE_PENDING:
1199 case AMDGPU_RAS_RETIRE_PAGE_FAULT:
1206 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
1208 * It allows user to read the bad pages of vram on the gpu through
1209 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
1211 * It outputs multiple lines, and each line stands for one gpu page.
1213 * The format of one line is below,
1214 * gpu pfn : gpu page size : flags
1216 * gpu pfn and gpu page size are printed in hex format.
1217 * flags can be one of below character,
1219 * R: reserved, this gpu page is reserved and not able to use.
1221 * P: pending for reserve, this gpu page is marked as bad, will be reserved
1222 * in next window of page_reserve.
1224 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
1228 * .. code-block:: bash
1230 * 0x00000001 : 0x00001000 : R
1231 * 0x00000002 : 0x00001000 : P
1235 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
1236 struct kobject *kobj, struct bin_attribute *attr,
1237 char *buf, loff_t ppos, size_t count)
1239 struct amdgpu_ras *con =
1240 container_of(attr, struct amdgpu_ras, badpages_attr);
1241 struct amdgpu_device *adev = con->adev;
1242 const unsigned int element_size =
1243 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
1244 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
1245 unsigned int end = div64_ul(ppos + count - 1, element_size);
1247 struct ras_badpage *bps = NULL;
1248 unsigned int bps_count = 0;
1250 memset(buf, 0, count);
1252 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1255 for (; start < end && start < bps_count; start++)
1256 s += scnprintf(&buf[s], element_size + 1,
1257 "0x%08x : 0x%08x : %1s\n",
1260 amdgpu_ras_badpage_flags_str(bps[start].flags));
1267 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1268 struct device_attribute *attr, char *buf)
1270 struct amdgpu_ras *con =
1271 container_of(attr, struct amdgpu_ras, features_attr);
1273 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
1276 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1278 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1280 sysfs_remove_file_from_group(&adev->dev->kobj,
1281 &con->badpages_attr.attr,
1285 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1287 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1288 struct attribute *attrs[] = {
1289 &con->features_attr.attr,
1292 struct attribute_group group = {
1293 .name = RAS_FS_NAME,
1297 sysfs_remove_group(&adev->dev->kobj, &group);
1302 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1303 struct ras_fs_if *head)
1305 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1307 if (!obj || obj->attr_inuse)
1312 memcpy(obj->fs_data.sysfs_name,
1314 sizeof(obj->fs_data.sysfs_name));
1316 obj->sysfs_attr = (struct device_attribute){
1318 .name = obj->fs_data.sysfs_name,
1321 .show = amdgpu_ras_sysfs_read,
1323 sysfs_attr_init(&obj->sysfs_attr.attr);
1325 if (sysfs_add_file_to_group(&adev->dev->kobj,
1326 &obj->sysfs_attr.attr,
1332 obj->attr_inuse = 1;
1337 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1338 struct ras_common_if *head)
1340 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1342 if (!obj || !obj->attr_inuse)
1345 sysfs_remove_file_from_group(&adev->dev->kobj,
1346 &obj->sysfs_attr.attr,
1348 obj->attr_inuse = 0;
1354 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1356 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1357 struct ras_manager *obj, *tmp;
1359 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1360 amdgpu_ras_sysfs_remove(adev, &obj->head);
1363 if (amdgpu_bad_page_threshold != 0)
1364 amdgpu_ras_sysfs_remove_bad_page_node(adev);
1366 amdgpu_ras_sysfs_remove_feature_node(adev);
1373 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1375 * Normally when there is an uncorrectable error, the driver will reset
1376 * the GPU to recover. However, in the event of an unrecoverable error,
1377 * the driver provides an interface to reboot the system automatically
1380 * The following file in debugfs provides that interface:
1381 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1385 * .. code-block:: bash
1387 * echo true > .../ras/auto_reboot
1391 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1393 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1394 struct drm_minor *minor = adev_to_drm(adev)->primary;
1397 dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1398 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev,
1399 &amdgpu_ras_debugfs_ctrl_ops);
1400 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev,
1401 &amdgpu_ras_debugfs_eeprom_ops);
1402 debugfs_create_u32("bad_page_cnt_threshold", 0444, dir,
1403 &con->bad_page_cnt_threshold);
1404 debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled);
1405 debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled);
1406 debugfs_create_file("ras_eeprom_size", S_IRUGO, dir, adev,
1407 &amdgpu_ras_debugfs_eeprom_size_ops);
1408 con->de_ras_eeprom_table = debugfs_create_file("ras_eeprom_table",
1410 &amdgpu_ras_debugfs_eeprom_table_ops);
1411 amdgpu_ras_debugfs_set_ret_size(&con->eeprom_control);
1414 * After one uncorrectable error happens, usually GPU recovery will
1415 * be scheduled. But due to the known problem in GPU recovery failing
1416 * to bring GPU back, below interface provides one direct way to
1417 * user to reboot system automatically in such case within
1418 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1419 * will never be called.
1421 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot);
1424 * User could set this not to clean up hardware's error count register
1425 * of RAS IPs during ras recovery.
1427 debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir,
1428 &con->disable_ras_err_cnt_harvest);
1432 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1433 struct ras_fs_if *head,
1436 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1443 memcpy(obj->fs_data.debugfs_name,
1445 sizeof(obj->fs_data.debugfs_name));
1447 debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir,
1448 obj, &amdgpu_ras_debugfs_ops);
1451 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1453 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1455 struct ras_manager *obj;
1456 struct ras_fs_if fs_info;
1459 * it won't be called in resume path, no need to check
1460 * suspend and gpu reset status
1462 if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con)
1465 dir = amdgpu_ras_debugfs_create_ctrl_node(adev);
1467 list_for_each_entry(obj, &con->head, node) {
1468 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1469 (obj->attr_inuse == 1)) {
1470 sprintf(fs_info.debugfs_name, "%s_err_inject",
1471 get_ras_block_str(&obj->head));
1472 fs_info.head = obj->head;
1473 amdgpu_ras_debugfs_create(adev, &fs_info, dir);
1481 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1482 amdgpu_ras_sysfs_badpages_read, NULL, 0);
1483 static DEVICE_ATTR(features, S_IRUGO,
1484 amdgpu_ras_sysfs_features_read, NULL);
1485 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1487 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1488 struct attribute_group group = {
1489 .name = RAS_FS_NAME,
1491 struct attribute *attrs[] = {
1492 &con->features_attr.attr,
1495 struct bin_attribute *bin_attrs[] = {
1501 /* add features entry */
1502 con->features_attr = dev_attr_features;
1503 group.attrs = attrs;
1504 sysfs_attr_init(attrs[0]);
1506 if (amdgpu_bad_page_threshold != 0) {
1507 /* add bad_page_features entry */
1508 bin_attr_gpu_vram_bad_pages.private = NULL;
1509 con->badpages_attr = bin_attr_gpu_vram_bad_pages;
1510 bin_attrs[0] = &con->badpages_attr;
1511 group.bin_attrs = bin_attrs;
1512 sysfs_bin_attr_init(bin_attrs[0]);
1515 r = sysfs_create_group(&adev->dev->kobj, &group);
1517 dev_err(adev->dev, "Failed to create RAS sysfs group!");
1522 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1524 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1525 struct ras_manager *con_obj, *ip_obj, *tmp;
1527 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1528 list_for_each_entry_safe(con_obj, tmp, &con->head, node) {
1529 ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head);
1535 amdgpu_ras_sysfs_remove_all(adev);
1541 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1543 struct ras_ih_data *data = &obj->ih_data;
1544 struct amdgpu_iv_entry entry;
1546 struct ras_err_data err_data = {0, 0, 0, NULL};
1548 while (data->rptr != data->wptr) {
1550 memcpy(&entry, &data->ring[data->rptr],
1551 data->element_size);
1554 data->rptr = (data->aligned_element_size +
1555 data->rptr) % data->ring_size;
1558 if (amdgpu_ras_is_poison_mode_supported(obj->adev) &&
1559 obj->head.block == AMDGPU_RAS_BLOCK__UMC)
1560 dev_info(obj->adev->dev,
1561 "Poison is created, no user action is needed.\n");
1563 /* Let IP handle its data, maybe we need get the output
1564 * from the callback to udpate the error type/count, etc
1566 ret = data->cb(obj->adev, &err_data, &entry);
1567 /* ue will trigger an interrupt, and in that case
1568 * we need do a reset to recovery the whole system.
1569 * But leave IP do that recovery, here we just dispatch
1572 if (ret == AMDGPU_RAS_SUCCESS) {
1573 /* these counts could be left as 0 if
1574 * some blocks do not count error number
1576 obj->err_data.ue_count += err_data.ue_count;
1577 obj->err_data.ce_count += err_data.ce_count;
1584 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1586 struct ras_ih_data *data =
1587 container_of(work, struct ras_ih_data, ih_work);
1588 struct ras_manager *obj =
1589 container_of(data, struct ras_manager, ih_data);
1591 amdgpu_ras_interrupt_handler(obj);
1594 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1595 struct ras_dispatch_if *info)
1597 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1598 struct ras_ih_data *data = &obj->ih_data;
1603 if (data->inuse == 0)
1606 /* Might be overflow... */
1607 memcpy(&data->ring[data->wptr], info->entry,
1608 data->element_size);
1611 data->wptr = (data->aligned_element_size +
1612 data->wptr) % data->ring_size;
1614 schedule_work(&data->ih_work);
1619 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1620 struct ras_ih_if *info)
1622 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1623 struct ras_ih_data *data;
1628 data = &obj->ih_data;
1629 if (data->inuse == 0)
1632 cancel_work_sync(&data->ih_work);
1635 memset(data, 0, sizeof(*data));
1641 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1642 struct ras_ih_if *info)
1644 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1645 struct ras_ih_data *data;
1648 /* in case we registe the IH before enable ras feature */
1649 obj = amdgpu_ras_create_obj(adev, &info->head);
1655 data = &obj->ih_data;
1656 /* add the callback.etc */
1657 *data = (struct ras_ih_data) {
1660 .element_size = sizeof(struct amdgpu_iv_entry),
1665 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1667 data->aligned_element_size = ALIGN(data->element_size, 8);
1668 /* the ring can store 64 iv entries. */
1669 data->ring_size = 64 * data->aligned_element_size;
1670 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1682 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1684 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1685 struct ras_manager *obj, *tmp;
1687 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1688 struct ras_ih_if info = {
1691 amdgpu_ras_interrupt_remove_handler(adev, &info);
1698 /* traversal all IPs except NBIO to query error counter */
1699 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1701 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1702 struct ras_manager *obj;
1704 if (!adev->ras_enabled || !con)
1707 list_for_each_entry(obj, &con->head, node) {
1708 struct ras_query_if info = {
1713 * PCIE_BIF IP has one different isr by ras controller
1714 * interrupt, the specific ras counter query will be
1715 * done in that isr. So skip such block from common
1716 * sync flood interrupt isr calling.
1718 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1721 amdgpu_ras_query_error_status(adev, &info);
1725 /* Parse RdRspStatus and WrRspStatus */
1726 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
1727 struct ras_query_if *info)
1730 * Only two block need to query read/write
1731 * RspStatus at current state
1733 switch (info->head.block) {
1734 case AMDGPU_RAS_BLOCK__GFX:
1735 if (adev->gfx.ras_funcs &&
1736 adev->gfx.ras_funcs->query_ras_error_status)
1737 adev->gfx.ras_funcs->query_ras_error_status(adev);
1739 case AMDGPU_RAS_BLOCK__MMHUB:
1740 if (adev->mmhub.ras_funcs &&
1741 adev->mmhub.ras_funcs->query_ras_error_status)
1742 adev->mmhub.ras_funcs->query_ras_error_status(adev);
1749 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
1751 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1752 struct ras_manager *obj;
1754 if (!adev->ras_enabled || !con)
1757 list_for_each_entry(obj, &con->head, node) {
1758 struct ras_query_if info = {
1762 amdgpu_ras_error_status_query(adev, &info);
1766 /* recovery begin */
1768 /* return 0 on success.
1769 * caller need free bps.
1771 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1772 struct ras_badpage **bps, unsigned int *count)
1774 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1775 struct ras_err_handler_data *data;
1777 int ret = 0, status;
1779 if (!con || !con->eh_data || !bps || !count)
1782 mutex_lock(&con->recovery_lock);
1783 data = con->eh_data;
1784 if (!data || data->count == 0) {
1790 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1796 for (; i < data->count; i++) {
1797 (*bps)[i] = (struct ras_badpage){
1798 .bp = data->bps[i].retired_page,
1799 .size = AMDGPU_GPU_PAGE_SIZE,
1800 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1802 status = amdgpu_vram_mgr_query_page_status(
1803 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1804 data->bps[i].retired_page);
1805 if (status == -EBUSY)
1806 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1807 else if (status == -ENOENT)
1808 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1811 *count = data->count;
1813 mutex_unlock(&con->recovery_lock);
1817 static void amdgpu_ras_do_recovery(struct work_struct *work)
1819 struct amdgpu_ras *ras =
1820 container_of(work, struct amdgpu_ras, recovery_work);
1821 struct amdgpu_device *remote_adev = NULL;
1822 struct amdgpu_device *adev = ras->adev;
1823 struct list_head device_list, *device_list_handle = NULL;
1825 if (!ras->disable_ras_err_cnt_harvest) {
1826 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
1828 /* Build list of devices to query RAS related errors */
1829 if (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
1830 device_list_handle = &hive->device_list;
1832 INIT_LIST_HEAD(&device_list);
1833 list_add_tail(&adev->gmc.xgmi.head, &device_list);
1834 device_list_handle = &device_list;
1837 list_for_each_entry(remote_adev,
1838 device_list_handle, gmc.xgmi.head) {
1839 amdgpu_ras_query_err_status(remote_adev);
1840 amdgpu_ras_log_on_err_counter(remote_adev);
1843 amdgpu_put_xgmi_hive(hive);
1846 if (amdgpu_device_should_recover_gpu(ras->adev))
1847 amdgpu_device_gpu_recover(ras->adev, NULL);
1848 atomic_set(&ras->in_recovery, 0);
1851 /* alloc/realloc bps array */
1852 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1853 struct ras_err_handler_data *data, int pages)
1855 unsigned int old_space = data->count + data->space_left;
1856 unsigned int new_space = old_space + pages;
1857 unsigned int align_space = ALIGN(new_space, 512);
1858 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1866 memcpy(bps, data->bps,
1867 data->count * sizeof(*data->bps));
1872 data->space_left += align_space - old_space;
1876 /* it deal with vram only. */
1877 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1878 struct eeprom_table_record *bps, int pages)
1880 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1881 struct ras_err_handler_data *data;
1885 if (!con || !con->eh_data || !bps || pages <= 0)
1888 mutex_lock(&con->recovery_lock);
1889 data = con->eh_data;
1893 for (i = 0; i < pages; i++) {
1894 if (amdgpu_ras_check_bad_page_unlock(con,
1895 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT))
1898 if (!data->space_left &&
1899 amdgpu_ras_realloc_eh_data_space(adev, data, 256)) {
1904 amdgpu_vram_mgr_reserve_range(
1905 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1906 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT,
1907 AMDGPU_GPU_PAGE_SIZE);
1909 memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps));
1914 mutex_unlock(&con->recovery_lock);
1920 * write error record array to eeprom, the function should be
1921 * protected by recovery_lock
1923 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1925 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1926 struct ras_err_handler_data *data;
1927 struct amdgpu_ras_eeprom_control *control;
1930 if (!con || !con->eh_data)
1933 control = &con->eeprom_control;
1934 data = con->eh_data;
1935 save_count = data->count - control->ras_num_recs;
1936 /* only new entries are saved */
1937 if (save_count > 0) {
1938 if (amdgpu_ras_eeprom_append(control,
1939 &data->bps[control->ras_num_recs],
1941 dev_err(adev->dev, "Failed to save EEPROM table data!");
1945 dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
1952 * read error record array in eeprom and reserve enough space for
1953 * storing new bad pages
1955 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1957 struct amdgpu_ras_eeprom_control *control =
1958 &adev->psp.ras_context.ras->eeprom_control;
1959 struct eeprom_table_record *bps;
1962 /* no bad page record, skip eeprom access */
1963 if (control->ras_num_recs == 0 || amdgpu_bad_page_threshold == 0)
1966 bps = kcalloc(control->ras_num_recs, sizeof(*bps), GFP_KERNEL);
1970 ret = amdgpu_ras_eeprom_read(control, bps, control->ras_num_recs);
1972 dev_err(adev->dev, "Failed to load EEPROM table records!");
1974 ret = amdgpu_ras_add_bad_pages(adev, bps, control->ras_num_recs);
1980 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
1983 struct ras_err_handler_data *data = con->eh_data;
1986 addr >>= AMDGPU_GPU_PAGE_SHIFT;
1987 for (i = 0; i < data->count; i++)
1988 if (addr == data->bps[i].retired_page)
1995 * check if an address belongs to bad page
1997 * Note: this check is only for umc block
1999 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
2002 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2005 if (!con || !con->eh_data)
2008 mutex_lock(&con->recovery_lock);
2009 ret = amdgpu_ras_check_bad_page_unlock(con, addr);
2010 mutex_unlock(&con->recovery_lock);
2014 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
2017 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2020 * Justification of value bad_page_cnt_threshold in ras structure
2022 * Generally, -1 <= amdgpu_bad_page_threshold <= max record length
2023 * in eeprom, and introduce two scenarios accordingly.
2025 * Bad page retirement enablement:
2026 * - If amdgpu_bad_page_threshold = -1,
2027 * bad_page_cnt_threshold = typical value by formula.
2029 * - When the value from user is 0 < amdgpu_bad_page_threshold <
2030 * max record length in eeprom, use it directly.
2032 * Bad page retirement disablement:
2033 * - If amdgpu_bad_page_threshold = 0, bad page retirement
2034 * functionality is disabled, and bad_page_cnt_threshold will
2038 if (amdgpu_bad_page_threshold < 0) {
2039 u64 val = adev->gmc.mc_vram_size;
2041 do_div(val, RAS_BAD_PAGE_COVER);
2042 con->bad_page_cnt_threshold = min(lower_32_bits(val),
2045 con->bad_page_cnt_threshold = min_t(int, max_count,
2046 amdgpu_bad_page_threshold);
2050 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
2052 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2053 struct ras_err_handler_data **data;
2054 u32 max_eeprom_records_count = 0;
2055 bool exc_err_limit = false;
2061 /* Allow access to RAS EEPROM via debugfs, when the ASIC
2062 * supports RAS and debugfs is enabled, but when
2063 * adev->ras_enabled is unset, i.e. when "ras_enable"
2064 * module parameter is set to 0.
2068 if (!adev->ras_enabled)
2071 data = &con->eh_data;
2072 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
2078 mutex_init(&con->recovery_lock);
2079 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
2080 atomic_set(&con->in_recovery, 0);
2082 max_eeprom_records_count = amdgpu_ras_eeprom_max_record_count();
2083 amdgpu_ras_validate_threshold(adev, max_eeprom_records_count);
2085 /* Todo: During test the SMU might fail to read the eeprom through I2C
2086 * when the GPU is pending on XGMI reset during probe time
2087 * (Mostly after second bus reset), skip it now
2089 if (adev->gmc.xgmi.pending_reset)
2091 ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit);
2093 * This calling fails when exc_err_limit is true or
2096 if (exc_err_limit || ret)
2099 if (con->eeprom_control.ras_num_recs) {
2100 ret = amdgpu_ras_load_bad_pages(adev);
2104 if (adev->smu.ppt_funcs && adev->smu.ppt_funcs->send_hbm_bad_pages_num)
2105 adev->smu.ppt_funcs->send_hbm_bad_pages_num(&adev->smu, con->eeprom_control.ras_num_recs);
2108 #ifdef CONFIG_X86_MCE_AMD
2109 if ((adev->asic_type == CHIP_ALDEBARAN) &&
2110 (adev->gmc.xgmi.connected_to_cpu))
2111 amdgpu_register_bad_pages_mca_notifier();
2116 kfree((*data)->bps);
2118 con->eh_data = NULL;
2120 dev_warn(adev->dev, "Failed to initialize ras recovery! (%d)\n", ret);
2123 * Except error threshold exceeding case, other failure cases in this
2124 * function would not fail amdgpu driver init.
2134 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
2136 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2137 struct ras_err_handler_data *data = con->eh_data;
2139 /* recovery_init failed to init it, fini is useless */
2143 cancel_work_sync(&con->recovery_work);
2145 mutex_lock(&con->recovery_lock);
2146 con->eh_data = NULL;
2149 mutex_unlock(&con->recovery_lock);
2155 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev)
2157 return adev->asic_type == CHIP_VEGA10 ||
2158 adev->asic_type == CHIP_VEGA20 ||
2159 adev->asic_type == CHIP_ARCTURUS ||
2160 adev->asic_type == CHIP_ALDEBARAN ||
2161 adev->asic_type == CHIP_SIENNA_CICHLID;
2165 * this is workaround for vega20 workstation sku,
2166 * force enable gfx ras, ignore vbios gfx ras flag
2167 * due to GC EDC can not write
2169 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev)
2171 struct atom_context *ctx = adev->mode_info.atom_context;
2176 if (strnstr(ctx->vbios_version, "D16406",
2177 sizeof(ctx->vbios_version)) ||
2178 strnstr(ctx->vbios_version, "D36002",
2179 sizeof(ctx->vbios_version)))
2180 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX);
2184 * check hardware's ras ability which will be saved in hw_supported.
2185 * if hardware does not support ras, we can skip some ras initializtion and
2186 * forbid some ras operations from IP.
2187 * if software itself, say boot parameter, limit the ras ability. We still
2188 * need allow IP do some limited operations, like disable. In such case,
2189 * we have to initialize ras as normal. but need check if operation is
2190 * allowed or not in each function.
2192 static void amdgpu_ras_check_supported(struct amdgpu_device *adev)
2194 adev->ras_hw_enabled = adev->ras_enabled = 0;
2196 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
2197 !amdgpu_ras_asic_supported(adev))
2200 if (!adev->gmc.xgmi.connected_to_cpu) {
2201 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
2202 dev_info(adev->dev, "MEM ECC is active.\n");
2203 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC |
2204 1 << AMDGPU_RAS_BLOCK__DF);
2206 dev_info(adev->dev, "MEM ECC is not presented.\n");
2209 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
2210 dev_info(adev->dev, "SRAM ECC is active.\n");
2211 adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
2212 1 << AMDGPU_RAS_BLOCK__DF);
2214 dev_info(adev->dev, "SRAM ECC is not presented.\n");
2217 /* driver only manages a few IP blocks RAS feature
2218 * when GPU is connected cpu through XGMI */
2219 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX |
2220 1 << AMDGPU_RAS_BLOCK__SDMA |
2221 1 << AMDGPU_RAS_BLOCK__MMHUB);
2224 amdgpu_ras_get_quirks(adev);
2226 /* hw_supported needs to be aligned with RAS block mask. */
2227 adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK;
2229 adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 :
2230 adev->ras_hw_enabled & amdgpu_ras_mask;
2233 static void amdgpu_ras_counte_dw(struct work_struct *work)
2235 struct amdgpu_ras *con = container_of(work, struct amdgpu_ras,
2236 ras_counte_delay_work.work);
2237 struct amdgpu_device *adev = con->adev;
2238 struct drm_device *dev = adev_to_drm(adev);
2239 unsigned long ce_count, ue_count;
2242 res = pm_runtime_get_sync(dev->dev);
2246 /* Cache new values.
2248 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2249 atomic_set(&con->ras_ce_count, ce_count);
2250 atomic_set(&con->ras_ue_count, ue_count);
2253 pm_runtime_mark_last_busy(dev->dev);
2255 pm_runtime_put_autosuspend(dev->dev);
2258 int amdgpu_ras_init(struct amdgpu_device *adev)
2260 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2262 bool df_poison, umc_poison;
2267 con = kmalloc(sizeof(struct amdgpu_ras) +
2268 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT +
2269 sizeof(struct ras_manager) * AMDGPU_RAS_MCA_BLOCK_COUNT,
2270 GFP_KERNEL|__GFP_ZERO);
2275 INIT_DELAYED_WORK(&con->ras_counte_delay_work, amdgpu_ras_counte_dw);
2276 atomic_set(&con->ras_ce_count, 0);
2277 atomic_set(&con->ras_ue_count, 0);
2279 con->objs = (struct ras_manager *)(con + 1);
2281 amdgpu_ras_set_context(adev, con);
2283 amdgpu_ras_check_supported(adev);
2285 if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) {
2286 /* set gfx block ras context feature for VEGA20 Gaming
2287 * send ras disable cmd to ras ta during ras late init.
2289 if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) {
2290 con->features |= BIT(AMDGPU_RAS_BLOCK__GFX);
2300 INIT_LIST_HEAD(&con->head);
2301 /* Might need get this flag from vbios. */
2302 con->flags = RAS_DEFAULT_FLAGS;
2304 /* initialize nbio ras function ahead of any other
2305 * ras functions so hardware fatal error interrupt
2306 * can be enabled as early as possible */
2307 switch (adev->asic_type) {
2310 case CHIP_ALDEBARAN:
2311 if (!adev->gmc.xgmi.connected_to_cpu)
2312 adev->nbio.ras_funcs = &nbio_v7_4_ras_funcs;
2315 /* nbio ras is not available */
2319 if (adev->nbio.ras_funcs &&
2320 adev->nbio.ras_funcs->init_ras_controller_interrupt) {
2321 r = adev->nbio.ras_funcs->init_ras_controller_interrupt(adev);
2326 if (adev->nbio.ras_funcs &&
2327 adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt) {
2328 r = adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt(adev);
2333 /* Init poison supported flag, the default value is false */
2334 if (adev->df.funcs &&
2335 adev->df.funcs->query_ras_poison_mode &&
2336 adev->umc.ras_funcs &&
2337 adev->umc.ras_funcs->query_ras_poison_mode) {
2339 adev->df.funcs->query_ras_poison_mode(adev);
2341 adev->umc.ras_funcs->query_ras_poison_mode(adev);
2342 /* Only poison is set in both DF and UMC, we can support it */
2343 if (df_poison && umc_poison)
2344 con->poison_supported = true;
2345 else if (df_poison != umc_poison)
2346 dev_warn(adev->dev, "Poison setting is inconsistent in DF/UMC(%d:%d)!\n",
2347 df_poison, umc_poison);
2350 if (amdgpu_ras_fs_init(adev)) {
2355 dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
2356 "hardware ability[%x] ras_mask[%x]\n",
2357 adev->ras_hw_enabled, adev->ras_enabled);
2361 amdgpu_ras_set_context(adev, NULL);
2367 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev)
2369 if (adev->gmc.xgmi.connected_to_cpu)
2374 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev,
2375 struct ras_common_if *ras_block)
2377 struct ras_query_if info = {
2381 if (!amdgpu_persistent_edc_harvesting_supported(adev))
2384 if (amdgpu_ras_query_error_status(adev, &info) != 0)
2385 DRM_WARN("RAS init harvest failure");
2387 if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0)
2388 DRM_WARN("RAS init harvest reset failure");
2393 bool amdgpu_ras_is_poison_mode_supported(struct amdgpu_device *adev)
2395 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2400 return con->poison_supported;
2403 /* helper function to handle common stuff in ip late init phase */
2404 int amdgpu_ras_late_init(struct amdgpu_device *adev,
2405 struct ras_common_if *ras_block,
2406 struct ras_fs_if *fs_info,
2407 struct ras_ih_if *ih_info)
2409 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2410 unsigned long ue_count, ce_count;
2413 /* disable RAS feature per IP block if it is not supported */
2414 if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
2415 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
2419 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
2421 if (adev->in_suspend || amdgpu_in_reset(adev)) {
2422 /* in resume phase, if fail to enable ras,
2423 * clean up all ras fs nodes, and disable ras */
2429 /* check for errors on warm reset edc persisant supported ASIC */
2430 amdgpu_persistent_edc_harvesting(adev, ras_block);
2432 /* in resume phase, no need to create ras fs node */
2433 if (adev->in_suspend || amdgpu_in_reset(adev))
2437 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
2442 r = amdgpu_ras_sysfs_create(adev, fs_info);
2446 /* Those are the cached values at init.
2448 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2449 atomic_set(&con->ras_ce_count, ce_count);
2450 atomic_set(&con->ras_ue_count, ue_count);
2455 amdgpu_ras_sysfs_remove(adev, ras_block);
2458 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2460 amdgpu_ras_feature_enable(adev, ras_block, 0);
2464 /* helper function to remove ras fs node and interrupt handler */
2465 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
2466 struct ras_common_if *ras_block,
2467 struct ras_ih_if *ih_info)
2469 if (!ras_block || !ih_info)
2472 amdgpu_ras_sysfs_remove(adev, ras_block);
2474 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2475 amdgpu_ras_feature_enable(adev, ras_block, 0);
2478 /* do some init work after IP late init as dependence.
2479 * and it runs in resume/gpu reset/booting up cases.
2481 void amdgpu_ras_resume(struct amdgpu_device *adev)
2483 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2484 struct ras_manager *obj, *tmp;
2486 if (!adev->ras_enabled || !con) {
2487 /* clean ras context for VEGA20 Gaming after send ras disable cmd */
2488 amdgpu_release_ras_context(adev);
2493 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2494 /* Set up all other IPs which are not implemented. There is a
2495 * tricky thing that IP's actual ras error type should be
2496 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2497 * ERROR_NONE make sense anyway.
2499 amdgpu_ras_enable_all_features(adev, 1);
2501 /* We enable ras on all hw_supported block, but as boot
2502 * parameter might disable some of them and one or more IP has
2503 * not implemented yet. So we disable them on behalf.
2505 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2506 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2507 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2508 /* there should be no any reference. */
2509 WARN_ON(alive_obj(obj));
2515 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2517 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2519 if (!adev->ras_enabled || !con)
2522 amdgpu_ras_disable_all_features(adev, 0);
2523 /* Make sure all ras objects are disabled. */
2525 amdgpu_ras_disable_all_features(adev, 1);
2528 /* do some fini work before IP fini as dependence */
2529 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2531 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2533 if (!adev->ras_enabled || !con)
2537 /* Need disable ras on all IPs here before ip [hw/sw]fini */
2538 amdgpu_ras_disable_all_features(adev, 0);
2539 amdgpu_ras_recovery_fini(adev);
2543 int amdgpu_ras_fini(struct amdgpu_device *adev)
2545 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2547 if (!adev->ras_enabled || !con)
2550 amdgpu_ras_fs_fini(adev);
2551 amdgpu_ras_interrupt_remove_all(adev);
2553 WARN(con->features, "Feature mask is not cleared");
2556 amdgpu_ras_disable_all_features(adev, 1);
2558 cancel_delayed_work_sync(&con->ras_counte_delay_work);
2560 amdgpu_ras_set_context(adev, NULL);
2566 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2568 amdgpu_ras_check_supported(adev);
2569 if (!adev->ras_hw_enabled)
2572 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2573 dev_info(adev->dev, "uncorrectable hardware error"
2574 "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2576 amdgpu_ras_reset_gpu(adev);
2580 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
2582 if (adev->asic_type == CHIP_VEGA20 &&
2583 adev->pm.fw_version <= 0x283400) {
2584 return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
2585 amdgpu_ras_intr_triggered();
2591 void amdgpu_release_ras_context(struct amdgpu_device *adev)
2593 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2598 if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) {
2599 con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX);
2600 amdgpu_ras_set_context(adev, NULL);
2605 #ifdef CONFIG_X86_MCE_AMD
2606 static struct amdgpu_device *find_adev(uint32_t node_id)
2608 struct amdgpu_gpu_instance *gpu_instance;
2610 struct amdgpu_device *adev = NULL;
2612 mutex_lock(&mgpu_info.mutex);
2614 for (i = 0; i < mgpu_info.num_gpu; i++) {
2615 gpu_instance = &(mgpu_info.gpu_ins[i]);
2616 adev = gpu_instance->adev;
2618 if (adev->gmc.xgmi.connected_to_cpu &&
2619 adev->gmc.xgmi.physical_node_id == node_id)
2624 mutex_unlock(&mgpu_info.mutex);
2629 #define GET_MCA_IPID_GPUID(m) (((m) >> 44) & 0xF)
2630 #define GET_UMC_INST(m) (((m) >> 21) & 0x7)
2631 #define GET_CHAN_INDEX(m) ((((m) >> 12) & 0x3) | (((m) >> 18) & 0x4))
2632 #define GPU_ID_OFFSET 8
2634 static int amdgpu_bad_page_notifier(struct notifier_block *nb,
2635 unsigned long val, void *data)
2637 struct mce *m = (struct mce *)data;
2638 struct amdgpu_device *adev = NULL;
2639 uint32_t gpu_id = 0;
2640 uint32_t umc_inst = 0;
2641 uint32_t ch_inst, channel_index = 0;
2642 struct ras_err_data err_data = {0, 0, 0, NULL};
2643 struct eeprom_table_record err_rec;
2644 uint64_t retired_page;
2647 * If the error was generated in UMC_V2, which belongs to GPU UMCs,
2648 * and error occurred in DramECC (Extended error code = 0) then only
2649 * process the error, else bail out.
2651 if (!m || !((smca_get_bank_type(m->bank) == SMCA_UMC_V2) &&
2652 (XEC(m->status, 0x3f) == 0x0)))
2656 * If it is correctable error, return.
2658 if (mce_is_correctable(m))
2662 * GPU Id is offset by GPU_ID_OFFSET in MCA_IPID_UMC register.
2664 gpu_id = GET_MCA_IPID_GPUID(m->ipid) - GPU_ID_OFFSET;
2666 adev = find_adev(gpu_id);
2668 DRM_WARN("%s: Unable to find adev for gpu_id: %d\n", __func__,
2674 * If it is uncorrectable error, then find out UMC instance and
2677 umc_inst = GET_UMC_INST(m->ipid);
2678 ch_inst = GET_CHAN_INDEX(m->ipid);
2680 dev_info(adev->dev, "Uncorrectable error detected in UMC inst: %d, chan_idx: %d",
2683 memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
2686 * Translate UMC channel address to Physical address
2689 adev->umc.channel_idx_tbl[umc_inst * adev->umc.channel_inst_num
2692 retired_page = ADDR_OF_8KB_BLOCK(m->addr) |
2693 ADDR_OF_256B_BLOCK(channel_index) |
2694 OFFSET_IN_256B_BLOCK(m->addr);
2696 err_rec.address = m->addr;
2697 err_rec.retired_page = retired_page >> AMDGPU_GPU_PAGE_SHIFT;
2698 err_rec.ts = (uint64_t)ktime_get_real_seconds();
2699 err_rec.err_type = AMDGPU_RAS_EEPROM_ERR_NON_RECOVERABLE;
2701 err_rec.mem_channel = channel_index;
2702 err_rec.mcumc_id = umc_inst;
2704 err_data.err_addr = &err_rec;
2705 err_data.err_addr_cnt = 1;
2707 if (amdgpu_bad_page_threshold != 0) {
2708 amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
2709 err_data.err_addr_cnt);
2710 amdgpu_ras_save_bad_pages(adev);
2716 static struct notifier_block amdgpu_bad_page_nb = {
2717 .notifier_call = amdgpu_bad_page_notifier,
2718 .priority = MCE_PRIO_UC,
2721 static void amdgpu_register_bad_pages_mca_notifier(void)
2724 * Register the x86 notifier only once
2725 * with MCE subsystem.
2727 if (notifier_registered == false) {
2728 mce_register_decode_chain(&amdgpu_bad_page_nb);
2729 notifier_registered = true;