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>
32 #include "amdgpu_ras.h"
33 #include "amdgpu_atomfirmware.h"
34 #include "amdgpu_xgmi.h"
35 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h"
37 static const char *RAS_FS_NAME = "ras";
39 const char *ras_error_string[] = {
43 "multi_uncorrectable",
47 const char *ras_block_string[] = {
64 #define ras_err_str(i) (ras_error_string[ffs(i)])
65 #define ras_block_str(i) (ras_block_string[i])
67 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
69 /* inject address is 52 bits */
70 #define RAS_UMC_INJECT_ADDR_LIMIT (0x1ULL << 52)
72 /* typical ECC bad page rate(1 bad page per 100MB VRAM) */
73 #define RAS_BAD_PAGE_RATE (100 * 1024 * 1024ULL)
75 enum amdgpu_ras_retire_page_reservation {
76 AMDGPU_RAS_RETIRE_PAGE_RESERVED,
77 AMDGPU_RAS_RETIRE_PAGE_PENDING,
78 AMDGPU_RAS_RETIRE_PAGE_FAULT,
81 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
83 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
85 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
88 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
90 if (adev && amdgpu_ras_get_context(adev))
91 amdgpu_ras_get_context(adev)->error_query_ready = ready;
94 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
96 if (adev && amdgpu_ras_get_context(adev))
97 return amdgpu_ras_get_context(adev)->error_query_ready;
102 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
103 size_t size, loff_t *pos)
105 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
106 struct ras_query_if info = {
112 if (amdgpu_ras_query_error_status(obj->adev, &info))
115 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
117 "ce", info.ce_count);
122 s = min_t(u64, s, size);
125 if (copy_to_user(buf, &val[*pos], s))
133 static const struct file_operations amdgpu_ras_debugfs_ops = {
134 .owner = THIS_MODULE,
135 .read = amdgpu_ras_debugfs_read,
137 .llseek = default_llseek
140 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
144 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
146 if (strcmp(name, ras_block_str(i)) == 0)
152 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
153 const char __user *buf, size_t size,
154 loff_t *pos, struct ras_debug_if *data)
156 ssize_t s = min_t(u64, 64, size);
169 memset(str, 0, sizeof(str));
170 memset(data, 0, sizeof(*data));
172 if (copy_from_user(str, buf, s))
175 if (sscanf(str, "disable %32s", block_name) == 1)
177 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
179 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
181 else if (str[0] && str[1] && str[2] && str[3])
182 /* ascii string, but commands are not matched. */
186 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
189 data->head.block = block_id;
190 /* only ue and ce errors are supported */
191 if (!memcmp("ue", err, 2))
192 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
193 else if (!memcmp("ce", err, 2))
194 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
201 if (sscanf(str, "%*s %*s %*s %u %llu %llu",
202 &sub_block, &address, &value) != 3)
203 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
204 &sub_block, &address, &value) != 3)
206 data->head.sub_block_index = sub_block;
207 data->inject.address = address;
208 data->inject.value = value;
211 if (size < sizeof(*data))
214 if (copy_from_user(data, buf, sizeof(*data)))
222 * DOC: AMDGPU RAS debugfs control interface
224 * It accepts struct ras_debug_if who has two members.
226 * First member: ras_debug_if::head or ras_debug_if::inject.
228 * head is used to indicate which IP block will be under control.
230 * head has four members, they are block, type, sub_block_index, name.
231 * block: which IP will be under control.
232 * type: what kind of error will be enabled/disabled/injected.
233 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
234 * name: the name of IP.
236 * inject has two more members than head, they are address, value.
237 * As their names indicate, inject operation will write the
238 * value to the address.
240 * The second member: struct ras_debug_if::op.
241 * It has three kinds of operations.
243 * - 0: disable RAS on the block. Take ::head as its data.
244 * - 1: enable RAS on the block. Take ::head as its data.
245 * - 2: inject errors on the block. Take ::inject as its data.
247 * How to use the interface?
251 * Copy the struct ras_debug_if in your codes and initialize it.
252 * Write the struct to the control node.
256 * .. code-block:: bash
258 * echo op block [error [sub_block address value]] > .../ras/ras_ctrl
262 * op: disable, enable, inject
263 * disable: only block is needed
264 * enable: block and error are needed
265 * inject: error, address, value are needed
266 * block: umc, sdma, gfx, .........
267 * see ras_block_string[] for details
269 * ue: multi_uncorrectable
270 * ce: single_correctable
272 * sub block index, pass 0 if there is no sub block
274 * here are some examples for bash commands:
276 * .. code-block:: bash
278 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
279 * echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
280 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
282 * How to check the result?
284 * For disable/enable, please check ras features at
285 * /sys/class/drm/card[0/1/2...]/device/ras/features
287 * For inject, please check corresponding err count at
288 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
291 * Operations are only allowed on blocks which are supported.
292 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
293 * to see which blocks support RAS on a particular asic.
296 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
297 size_t size, loff_t *pos)
299 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
300 struct ras_debug_if data;
303 if (!amdgpu_ras_get_error_query_ready(adev)) {
304 dev_warn(adev->dev, "RAS WARN: error injection "
305 "currently inaccessible\n");
309 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
313 if (!amdgpu_ras_is_supported(adev, data.head.block))
318 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
321 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
324 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
325 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
326 dev_warn(adev->dev, "RAS WARN: input address "
327 "0x%llx is invalid.",
328 data.inject.address);
333 /* umc ce/ue error injection for a bad page is not allowed */
334 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
335 amdgpu_ras_check_bad_page(adev, data.inject.address)) {
336 dev_warn(adev->dev, "RAS WARN: 0x%llx has been marked "
337 "as bad before error injection!\n",
338 data.inject.address);
342 /* data.inject.address is offset instead of absolute gpu address */
343 ret = amdgpu_ras_error_inject(adev, &data.inject);
357 * DOC: AMDGPU RAS debugfs EEPROM table reset interface
359 * Some boards contain an EEPROM which is used to persistently store a list of
360 * bad pages which experiences ECC errors in vram. This interface provides
361 * a way to reset the EEPROM, e.g., after testing error injection.
365 * .. code-block:: bash
367 * echo 1 > ../ras/ras_eeprom_reset
369 * will reset EEPROM table to 0 entries.
372 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, const char __user *buf,
373 size_t size, loff_t *pos)
375 struct amdgpu_device *adev =
376 (struct amdgpu_device *)file_inode(f)->i_private;
379 ret = amdgpu_ras_eeprom_reset_table(
380 &(amdgpu_ras_get_context(adev)->eeprom_control));
383 amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS;
390 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
391 .owner = THIS_MODULE,
393 .write = amdgpu_ras_debugfs_ctrl_write,
394 .llseek = default_llseek
397 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
398 .owner = THIS_MODULE,
400 .write = amdgpu_ras_debugfs_eeprom_write,
401 .llseek = default_llseek
405 * DOC: AMDGPU RAS sysfs Error Count Interface
407 * It allows the user to read the error count for each IP block on the gpu through
408 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
410 * It outputs the multiple lines which report the uncorrected (ue) and corrected
413 * The format of one line is below,
419 * .. code-block:: bash
425 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
426 struct device_attribute *attr, char *buf)
428 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
429 struct ras_query_if info = {
433 if (!amdgpu_ras_get_error_query_ready(obj->adev))
434 return sysfs_emit(buf, "Query currently inaccessible\n");
436 if (amdgpu_ras_query_error_status(obj->adev, &info))
439 return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count,
440 "ce", info.ce_count);
445 #define get_obj(obj) do { (obj)->use++; } while (0)
446 #define alive_obj(obj) ((obj)->use)
448 static inline void put_obj(struct ras_manager *obj)
450 if (obj && --obj->use == 0)
451 list_del(&obj->node);
452 if (obj && obj->use < 0) {
453 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
457 /* make one obj and return it. */
458 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
459 struct ras_common_if *head)
461 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
462 struct ras_manager *obj;
464 if (!adev->ras_features || !con)
467 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
470 obj = &con->objs[head->block];
471 /* already exist. return obj? */
477 list_add(&obj->node, &con->head);
483 /* return an obj equal to head, or the first when head is NULL */
484 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
485 struct ras_common_if *head)
487 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
488 struct ras_manager *obj;
491 if (!adev->ras_features || !con)
495 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
498 obj = &con->objs[head->block];
500 if (alive_obj(obj)) {
501 WARN_ON(head->block != obj->head.block);
505 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
507 if (alive_obj(obj)) {
508 WARN_ON(i != obj->head.block);
518 static void amdgpu_ras_parse_status_code(struct amdgpu_device *adev,
519 const char* invoke_type,
520 const char* block_name,
521 enum ta_ras_status ret)
524 case TA_RAS_STATUS__SUCCESS:
526 case TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE:
528 "RAS WARN: %s %s currently unavailable\n",
534 "RAS ERROR: %s %s error failed ret 0x%X\n",
541 /* feature ctl begin */
542 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
543 struct ras_common_if *head)
545 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
547 return con->hw_supported & BIT(head->block);
550 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
551 struct ras_common_if *head)
553 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
555 return con->features & BIT(head->block);
559 * if obj is not created, then create one.
560 * set feature enable flag.
562 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
563 struct ras_common_if *head, int enable)
565 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
566 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
568 /* If hardware does not support ras, then do not create obj.
569 * But if hardware support ras, we can create the obj.
570 * Ras framework checks con->hw_supported to see if it need do
571 * corresponding initialization.
572 * IP checks con->support to see if it need disable ras.
574 if (!amdgpu_ras_is_feature_allowed(adev, head))
576 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
581 obj = amdgpu_ras_create_obj(adev, head);
585 /* In case we create obj somewhere else */
588 con->features |= BIT(head->block);
590 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
591 /* skip clean gfx ras context feature for VEGA20 Gaming.
594 if (!(!adev->ras_features && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)))
595 con->features &= ~BIT(head->block);
603 /* wrapper of psp_ras_enable_features */
604 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
605 struct ras_common_if *head, bool enable)
607 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
608 union ta_ras_cmd_input *info;
614 info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
619 info->disable_features = (struct ta_ras_disable_features_input) {
620 .block_id = amdgpu_ras_block_to_ta(head->block),
621 .error_type = amdgpu_ras_error_to_ta(head->type),
624 info->enable_features = (struct ta_ras_enable_features_input) {
625 .block_id = amdgpu_ras_block_to_ta(head->block),
626 .error_type = amdgpu_ras_error_to_ta(head->type),
630 /* Do not enable if it is not allowed. */
631 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
632 /* Are we alerady in that state we are going to set? */
633 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head))) {
638 if (!amdgpu_ras_intr_triggered()) {
639 ret = psp_ras_enable_features(&adev->psp, info, enable);
641 amdgpu_ras_parse_status_code(adev,
642 enable ? "enable":"disable",
643 ras_block_str(head->block),
644 (enum ta_ras_status)ret);
645 if (ret == TA_RAS_STATUS__RESET_NEEDED)
655 __amdgpu_ras_feature_enable(adev, head, enable);
662 /* Only used in device probe stage and called only once. */
663 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
664 struct ras_common_if *head, bool enable)
666 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
672 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
674 /* There is no harm to issue a ras TA cmd regardless of
675 * the currecnt ras state.
676 * If current state == target state, it will do nothing
677 * But sometimes it requests driver to reset and repost
678 * with error code -EAGAIN.
680 ret = amdgpu_ras_feature_enable(adev, head, 1);
681 /* With old ras TA, we might fail to enable ras.
682 * Log it and just setup the object.
683 * TODO need remove this WA in the future.
685 if (ret == -EINVAL) {
686 ret = __amdgpu_ras_feature_enable(adev, head, 1);
689 "RAS INFO: %s setup object\n",
690 ras_block_str(head->block));
693 /* setup the object then issue a ras TA disable cmd.*/
694 ret = __amdgpu_ras_feature_enable(adev, head, 1);
698 /* gfx block ras dsiable cmd must send to ras-ta */
699 if (head->block == AMDGPU_RAS_BLOCK__GFX)
700 con->features |= BIT(head->block);
702 ret = amdgpu_ras_feature_enable(adev, head, 0);
705 ret = amdgpu_ras_feature_enable(adev, head, enable);
710 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
713 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
714 struct ras_manager *obj, *tmp;
716 list_for_each_entry_safe(obj, tmp, &con->head, node) {
718 * aka just release the obj and corresponding flags
721 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
724 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
729 return con->features;
732 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
735 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
736 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
738 const enum amdgpu_ras_error_type default_ras_type =
739 AMDGPU_RAS_ERROR__NONE;
741 for (i = 0; i < ras_block_count; i++) {
742 struct ras_common_if head = {
744 .type = default_ras_type,
745 .sub_block_index = 0,
747 strcpy(head.name, ras_block_str(i));
750 * bypass psp. vbios enable ras for us.
751 * so just create the obj
753 if (__amdgpu_ras_feature_enable(adev, &head, 1))
756 if (amdgpu_ras_feature_enable(adev, &head, 1))
761 return con->features;
763 /* feature ctl end */
765 /* query/inject/cure begin */
766 int amdgpu_ras_query_error_status(struct amdgpu_device *adev,
767 struct ras_query_if *info)
769 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
770 struct ras_err_data err_data = {0, 0, 0, NULL};
776 switch (info->head.block) {
777 case AMDGPU_RAS_BLOCK__UMC:
778 if (adev->umc.funcs->query_ras_error_count)
779 adev->umc.funcs->query_ras_error_count(adev, &err_data);
780 /* umc query_ras_error_address is also responsible for clearing
783 if (adev->umc.funcs->query_ras_error_address)
784 adev->umc.funcs->query_ras_error_address(adev, &err_data);
786 case AMDGPU_RAS_BLOCK__SDMA:
787 if (adev->sdma.funcs->query_ras_error_count) {
788 for (i = 0; i < adev->sdma.num_instances; i++)
789 adev->sdma.funcs->query_ras_error_count(adev, i,
793 case AMDGPU_RAS_BLOCK__GFX:
794 if (adev->gfx.funcs->query_ras_error_count)
795 adev->gfx.funcs->query_ras_error_count(adev, &err_data);
797 if (adev->gfx.funcs->query_ras_error_status)
798 adev->gfx.funcs->query_ras_error_status(adev);
800 case AMDGPU_RAS_BLOCK__MMHUB:
801 if (adev->mmhub.funcs->query_ras_error_count)
802 adev->mmhub.funcs->query_ras_error_count(adev, &err_data);
804 if (adev->mmhub.funcs->query_ras_error_status)
805 adev->mmhub.funcs->query_ras_error_status(adev);
807 case AMDGPU_RAS_BLOCK__PCIE_BIF:
808 if (adev->nbio.funcs->query_ras_error_count)
809 adev->nbio.funcs->query_ras_error_count(adev, &err_data);
811 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
812 amdgpu_xgmi_query_ras_error_count(adev, &err_data);
818 obj->err_data.ue_count += err_data.ue_count;
819 obj->err_data.ce_count += err_data.ce_count;
821 info->ue_count = obj->err_data.ue_count;
822 info->ce_count = obj->err_data.ce_count;
824 if (err_data.ce_count) {
825 dev_info(adev->dev, "%ld correctable hardware errors "
826 "detected in %s block, no user "
827 "action is needed.\n",
828 obj->err_data.ce_count,
829 ras_block_str(info->head.block));
831 if (err_data.ue_count) {
832 dev_info(adev->dev, "%ld uncorrectable hardware errors "
833 "detected in %s block\n",
834 obj->err_data.ue_count,
835 ras_block_str(info->head.block));
841 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev,
842 enum amdgpu_ras_block block)
844 if (!amdgpu_ras_is_supported(adev, block))
848 case AMDGPU_RAS_BLOCK__GFX:
849 if (adev->gfx.funcs->reset_ras_error_count)
850 adev->gfx.funcs->reset_ras_error_count(adev);
852 if (adev->gfx.funcs->reset_ras_error_status)
853 adev->gfx.funcs->reset_ras_error_status(adev);
855 case AMDGPU_RAS_BLOCK__MMHUB:
856 if (adev->mmhub.funcs->reset_ras_error_count)
857 adev->mmhub.funcs->reset_ras_error_count(adev);
859 case AMDGPU_RAS_BLOCK__SDMA:
860 if (adev->sdma.funcs->reset_ras_error_count)
861 adev->sdma.funcs->reset_ras_error_count(adev);
870 /* Trigger XGMI/WAFL error */
871 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev,
872 struct ta_ras_trigger_error_input *block_info)
876 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
877 dev_warn(adev->dev, "Failed to disallow df cstate");
879 if (amdgpu_dpm_allow_xgmi_power_down(adev, false))
880 dev_warn(adev->dev, "Failed to disallow XGMI power down");
882 ret = psp_ras_trigger_error(&adev->psp, block_info);
884 if (amdgpu_ras_intr_triggered())
887 if (amdgpu_dpm_allow_xgmi_power_down(adev, true))
888 dev_warn(adev->dev, "Failed to allow XGMI power down");
890 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW))
891 dev_warn(adev->dev, "Failed to allow df cstate");
896 /* wrapper of psp_ras_trigger_error */
897 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
898 struct ras_inject_if *info)
900 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
901 struct ta_ras_trigger_error_input block_info = {
902 .block_id = amdgpu_ras_block_to_ta(info->head.block),
903 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
904 .sub_block_index = info->head.sub_block_index,
905 .address = info->address,
906 .value = info->value,
913 /* Calculate XGMI relative offset */
914 if (adev->gmc.xgmi.num_physical_nodes > 1) {
916 amdgpu_xgmi_get_relative_phy_addr(adev,
920 switch (info->head.block) {
921 case AMDGPU_RAS_BLOCK__GFX:
922 if (adev->gfx.funcs->ras_error_inject)
923 ret = adev->gfx.funcs->ras_error_inject(adev, info);
927 case AMDGPU_RAS_BLOCK__UMC:
928 case AMDGPU_RAS_BLOCK__MMHUB:
929 case AMDGPU_RAS_BLOCK__PCIE_BIF:
930 ret = psp_ras_trigger_error(&adev->psp, &block_info);
932 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
933 ret = amdgpu_ras_error_inject_xgmi(adev, &block_info);
936 dev_info(adev->dev, "%s error injection is not supported yet\n",
937 ras_block_str(info->head.block));
941 amdgpu_ras_parse_status_code(adev,
943 ras_block_str(info->head.block),
944 (enum ta_ras_status)ret);
949 /* get the total error counts on all IPs */
950 unsigned long amdgpu_ras_query_error_count(struct amdgpu_device *adev,
953 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
954 struct ras_manager *obj;
955 struct ras_err_data data = {0, 0};
957 if (!adev->ras_features || !con)
960 list_for_each_entry(obj, &con->head, node) {
961 struct ras_query_if info = {
965 if (amdgpu_ras_query_error_status(adev, &info))
968 data.ce_count += info.ce_count;
969 data.ue_count += info.ue_count;
972 return is_ce ? data.ce_count : data.ue_count;
974 /* query/inject/cure end */
979 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
980 struct ras_badpage **bps, unsigned int *count);
982 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
985 case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
987 case AMDGPU_RAS_RETIRE_PAGE_PENDING:
989 case AMDGPU_RAS_RETIRE_PAGE_FAULT:
996 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
998 * It allows user to read the bad pages of vram on the gpu through
999 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
1001 * It outputs multiple lines, and each line stands for one gpu page.
1003 * The format of one line is below,
1004 * gpu pfn : gpu page size : flags
1006 * gpu pfn and gpu page size are printed in hex format.
1007 * flags can be one of below character,
1009 * R: reserved, this gpu page is reserved and not able to use.
1011 * P: pending for reserve, this gpu page is marked as bad, will be reserved
1012 * in next window of page_reserve.
1014 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
1018 * .. code-block:: bash
1020 * 0x00000001 : 0x00001000 : R
1021 * 0x00000002 : 0x00001000 : P
1025 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
1026 struct kobject *kobj, struct bin_attribute *attr,
1027 char *buf, loff_t ppos, size_t count)
1029 struct amdgpu_ras *con =
1030 container_of(attr, struct amdgpu_ras, badpages_attr);
1031 struct amdgpu_device *adev = con->adev;
1032 const unsigned int element_size =
1033 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
1034 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
1035 unsigned int end = div64_ul(ppos + count - 1, element_size);
1037 struct ras_badpage *bps = NULL;
1038 unsigned int bps_count = 0;
1040 memset(buf, 0, count);
1042 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1045 for (; start < end && start < bps_count; start++)
1046 s += scnprintf(&buf[s], element_size + 1,
1047 "0x%08x : 0x%08x : %1s\n",
1050 amdgpu_ras_badpage_flags_str(bps[start].flags));
1057 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1058 struct device_attribute *attr, char *buf)
1060 struct amdgpu_ras *con =
1061 container_of(attr, struct amdgpu_ras, features_attr);
1063 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
1066 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1068 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1070 sysfs_remove_file_from_group(&adev->dev->kobj,
1071 &con->badpages_attr.attr,
1075 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1077 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1078 struct attribute *attrs[] = {
1079 &con->features_attr.attr,
1082 struct attribute_group group = {
1083 .name = RAS_FS_NAME,
1087 sysfs_remove_group(&adev->dev->kobj, &group);
1092 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1093 struct ras_fs_if *head)
1095 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1097 if (!obj || obj->attr_inuse)
1102 memcpy(obj->fs_data.sysfs_name,
1104 sizeof(obj->fs_data.sysfs_name));
1106 obj->sysfs_attr = (struct device_attribute){
1108 .name = obj->fs_data.sysfs_name,
1111 .show = amdgpu_ras_sysfs_read,
1113 sysfs_attr_init(&obj->sysfs_attr.attr);
1115 if (sysfs_add_file_to_group(&adev->dev->kobj,
1116 &obj->sysfs_attr.attr,
1122 obj->attr_inuse = 1;
1127 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1128 struct ras_common_if *head)
1130 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1132 if (!obj || !obj->attr_inuse)
1135 sysfs_remove_file_from_group(&adev->dev->kobj,
1136 &obj->sysfs_attr.attr,
1138 obj->attr_inuse = 0;
1144 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1146 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1147 struct ras_manager *obj, *tmp;
1149 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1150 amdgpu_ras_sysfs_remove(adev, &obj->head);
1153 if (amdgpu_bad_page_threshold != 0)
1154 amdgpu_ras_sysfs_remove_bad_page_node(adev);
1156 amdgpu_ras_sysfs_remove_feature_node(adev);
1163 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1165 * Normally when there is an uncorrectable error, the driver will reset
1166 * the GPU to recover. However, in the event of an unrecoverable error,
1167 * the driver provides an interface to reboot the system automatically
1170 * The following file in debugfs provides that interface:
1171 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1175 * .. code-block:: bash
1177 * echo true > .../ras/auto_reboot
1181 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1183 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1185 struct drm_minor *minor = adev_to_drm(adev)->primary;
1187 dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1188 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev,
1189 &amdgpu_ras_debugfs_ctrl_ops);
1190 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev,
1191 &amdgpu_ras_debugfs_eeprom_ops);
1194 * After one uncorrectable error happens, usually GPU recovery will
1195 * be scheduled. But due to the known problem in GPU recovery failing
1196 * to bring GPU back, below interface provides one direct way to
1197 * user to reboot system automatically in such case within
1198 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1199 * will never be called.
1201 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot);
1204 * User could set this not to clean up hardware's error count register
1205 * of RAS IPs during ras recovery.
1207 debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir,
1208 &con->disable_ras_err_cnt_harvest);
1212 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1213 struct ras_fs_if *head,
1216 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1223 memcpy(obj->fs_data.debugfs_name,
1225 sizeof(obj->fs_data.debugfs_name));
1227 debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir,
1228 obj, &amdgpu_ras_debugfs_ops);
1231 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1233 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1235 struct ras_manager *obj;
1236 struct ras_fs_if fs_info;
1239 * it won't be called in resume path, no need to check
1240 * suspend and gpu reset status
1242 if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con)
1245 dir = amdgpu_ras_debugfs_create_ctrl_node(adev);
1247 list_for_each_entry(obj, &con->head, node) {
1248 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1249 (obj->attr_inuse == 1)) {
1250 sprintf(fs_info.debugfs_name, "%s_err_inject",
1251 ras_block_str(obj->head.block));
1252 fs_info.head = obj->head;
1253 amdgpu_ras_debugfs_create(adev, &fs_info, dir);
1261 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1262 amdgpu_ras_sysfs_badpages_read, NULL, 0);
1263 static DEVICE_ATTR(features, S_IRUGO,
1264 amdgpu_ras_sysfs_features_read, NULL);
1265 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1267 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1268 struct attribute_group group = {
1269 .name = RAS_FS_NAME,
1271 struct attribute *attrs[] = {
1272 &con->features_attr.attr,
1275 struct bin_attribute *bin_attrs[] = {
1281 /* add features entry */
1282 con->features_attr = dev_attr_features;
1283 group.attrs = attrs;
1284 sysfs_attr_init(attrs[0]);
1286 if (amdgpu_bad_page_threshold != 0) {
1287 /* add bad_page_features entry */
1288 bin_attr_gpu_vram_bad_pages.private = NULL;
1289 con->badpages_attr = bin_attr_gpu_vram_bad_pages;
1290 bin_attrs[0] = &con->badpages_attr;
1291 group.bin_attrs = bin_attrs;
1292 sysfs_bin_attr_init(bin_attrs[0]);
1295 r = sysfs_create_group(&adev->dev->kobj, &group);
1297 dev_err(adev->dev, "Failed to create RAS sysfs group!");
1302 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1304 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1305 struct ras_manager *con_obj, *ip_obj, *tmp;
1307 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1308 list_for_each_entry_safe(con_obj, tmp, &con->head, node) {
1309 ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head);
1315 amdgpu_ras_sysfs_remove_all(adev);
1321 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1323 struct ras_ih_data *data = &obj->ih_data;
1324 struct amdgpu_iv_entry entry;
1326 struct ras_err_data err_data = {0, 0, 0, NULL};
1328 while (data->rptr != data->wptr) {
1330 memcpy(&entry, &data->ring[data->rptr],
1331 data->element_size);
1334 data->rptr = (data->aligned_element_size +
1335 data->rptr) % data->ring_size;
1337 /* Let IP handle its data, maybe we need get the output
1338 * from the callback to udpate the error type/count, etc
1341 ret = data->cb(obj->adev, &err_data, &entry);
1342 /* ue will trigger an interrupt, and in that case
1343 * we need do a reset to recovery the whole system.
1344 * But leave IP do that recovery, here we just dispatch
1347 if (ret == AMDGPU_RAS_SUCCESS) {
1348 /* these counts could be left as 0 if
1349 * some blocks do not count error number
1351 obj->err_data.ue_count += err_data.ue_count;
1352 obj->err_data.ce_count += err_data.ce_count;
1358 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1360 struct ras_ih_data *data =
1361 container_of(work, struct ras_ih_data, ih_work);
1362 struct ras_manager *obj =
1363 container_of(data, struct ras_manager, ih_data);
1365 amdgpu_ras_interrupt_handler(obj);
1368 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1369 struct ras_dispatch_if *info)
1371 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1372 struct ras_ih_data *data = &obj->ih_data;
1377 if (data->inuse == 0)
1380 /* Might be overflow... */
1381 memcpy(&data->ring[data->wptr], info->entry,
1382 data->element_size);
1385 data->wptr = (data->aligned_element_size +
1386 data->wptr) % data->ring_size;
1388 schedule_work(&data->ih_work);
1393 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1394 struct ras_ih_if *info)
1396 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1397 struct ras_ih_data *data;
1402 data = &obj->ih_data;
1403 if (data->inuse == 0)
1406 cancel_work_sync(&data->ih_work);
1409 memset(data, 0, sizeof(*data));
1415 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1416 struct ras_ih_if *info)
1418 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1419 struct ras_ih_data *data;
1422 /* in case we registe the IH before enable ras feature */
1423 obj = amdgpu_ras_create_obj(adev, &info->head);
1429 data = &obj->ih_data;
1430 /* add the callback.etc */
1431 *data = (struct ras_ih_data) {
1434 .element_size = sizeof(struct amdgpu_iv_entry),
1439 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1441 data->aligned_element_size = ALIGN(data->element_size, 8);
1442 /* the ring can store 64 iv entries. */
1443 data->ring_size = 64 * data->aligned_element_size;
1444 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1456 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1458 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1459 struct ras_manager *obj, *tmp;
1461 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1462 struct ras_ih_if info = {
1465 amdgpu_ras_interrupt_remove_handler(adev, &info);
1472 /* traversal all IPs except NBIO to query error counter */
1473 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1475 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1476 struct ras_manager *obj;
1478 if (!adev->ras_features || !con)
1481 list_for_each_entry(obj, &con->head, node) {
1482 struct ras_query_if info = {
1487 * PCIE_BIF IP has one different isr by ras controller
1488 * interrupt, the specific ras counter query will be
1489 * done in that isr. So skip such block from common
1490 * sync flood interrupt isr calling.
1492 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1495 amdgpu_ras_query_error_status(adev, &info);
1499 /* Parse RdRspStatus and WrRspStatus */
1500 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
1501 struct ras_query_if *info)
1504 * Only two block need to query read/write
1505 * RspStatus at current state
1507 switch (info->head.block) {
1508 case AMDGPU_RAS_BLOCK__GFX:
1509 if (adev->gfx.funcs->query_ras_error_status)
1510 adev->gfx.funcs->query_ras_error_status(adev);
1512 case AMDGPU_RAS_BLOCK__MMHUB:
1513 if (adev->mmhub.funcs->query_ras_error_status)
1514 adev->mmhub.funcs->query_ras_error_status(adev);
1521 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
1523 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1524 struct ras_manager *obj;
1526 if (!adev->ras_features || !con)
1529 list_for_each_entry(obj, &con->head, node) {
1530 struct ras_query_if info = {
1534 amdgpu_ras_error_status_query(adev, &info);
1538 /* recovery begin */
1540 /* return 0 on success.
1541 * caller need free bps.
1543 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1544 struct ras_badpage **bps, unsigned int *count)
1546 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1547 struct ras_err_handler_data *data;
1549 int ret = 0, status;
1551 if (!con || !con->eh_data || !bps || !count)
1554 mutex_lock(&con->recovery_lock);
1555 data = con->eh_data;
1556 if (!data || data->count == 0) {
1562 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1568 for (; i < data->count; i++) {
1569 (*bps)[i] = (struct ras_badpage){
1570 .bp = data->bps[i].retired_page,
1571 .size = AMDGPU_GPU_PAGE_SIZE,
1572 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1574 status = amdgpu_vram_mgr_query_page_status(
1575 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1576 data->bps[i].retired_page);
1577 if (status == -EBUSY)
1578 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1579 else if (status == -ENOENT)
1580 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1583 *count = data->count;
1585 mutex_unlock(&con->recovery_lock);
1589 static void amdgpu_ras_do_recovery(struct work_struct *work)
1591 struct amdgpu_ras *ras =
1592 container_of(work, struct amdgpu_ras, recovery_work);
1593 struct amdgpu_device *remote_adev = NULL;
1594 struct amdgpu_device *adev = ras->adev;
1595 struct list_head device_list, *device_list_handle = NULL;
1597 if (!ras->disable_ras_err_cnt_harvest) {
1598 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
1600 /* Build list of devices to query RAS related errors */
1601 if (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
1602 device_list_handle = &hive->device_list;
1604 INIT_LIST_HEAD(&device_list);
1605 list_add_tail(&adev->gmc.xgmi.head, &device_list);
1606 device_list_handle = &device_list;
1609 list_for_each_entry(remote_adev,
1610 device_list_handle, gmc.xgmi.head) {
1611 amdgpu_ras_query_err_status(remote_adev);
1612 amdgpu_ras_log_on_err_counter(remote_adev);
1615 amdgpu_put_xgmi_hive(hive);
1618 if (amdgpu_device_should_recover_gpu(ras->adev))
1619 amdgpu_device_gpu_recover(ras->adev, NULL);
1620 atomic_set(&ras->in_recovery, 0);
1623 /* alloc/realloc bps array */
1624 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1625 struct ras_err_handler_data *data, int pages)
1627 unsigned int old_space = data->count + data->space_left;
1628 unsigned int new_space = old_space + pages;
1629 unsigned int align_space = ALIGN(new_space, 512);
1630 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1638 memcpy(bps, data->bps,
1639 data->count * sizeof(*data->bps));
1644 data->space_left += align_space - old_space;
1648 /* it deal with vram only. */
1649 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1650 struct eeprom_table_record *bps, int pages)
1652 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1653 struct ras_err_handler_data *data;
1657 if (!con || !con->eh_data || !bps || pages <= 0)
1660 mutex_lock(&con->recovery_lock);
1661 data = con->eh_data;
1665 for (i = 0; i < pages; i++) {
1666 if (amdgpu_ras_check_bad_page_unlock(con,
1667 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT))
1670 if (!data->space_left &&
1671 amdgpu_ras_realloc_eh_data_space(adev, data, 256)) {
1676 amdgpu_vram_mgr_reserve_range(
1677 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1678 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT,
1679 AMDGPU_GPU_PAGE_SIZE);
1681 memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps));
1686 mutex_unlock(&con->recovery_lock);
1692 * write error record array to eeprom, the function should be
1693 * protected by recovery_lock
1695 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1697 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1698 struct ras_err_handler_data *data;
1699 struct amdgpu_ras_eeprom_control *control;
1702 if (!con || !con->eh_data)
1705 control = &con->eeprom_control;
1706 data = con->eh_data;
1707 save_count = data->count - control->num_recs;
1708 /* only new entries are saved */
1709 if (save_count > 0) {
1710 if (amdgpu_ras_eeprom_process_recods(control,
1711 &data->bps[control->num_recs],
1714 dev_err(adev->dev, "Failed to save EEPROM table data!");
1718 dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
1725 * read error record array in eeprom and reserve enough space for
1726 * storing new bad pages
1728 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1730 struct amdgpu_ras_eeprom_control *control =
1731 &adev->psp.ras.ras->eeprom_control;
1732 struct eeprom_table_record *bps = NULL;
1735 /* no bad page record, skip eeprom access */
1736 if (!control->num_recs || (amdgpu_bad_page_threshold == 0))
1739 bps = kcalloc(control->num_recs, sizeof(*bps), GFP_KERNEL);
1743 if (amdgpu_ras_eeprom_process_recods(control, bps, false,
1744 control->num_recs)) {
1745 dev_err(adev->dev, "Failed to load EEPROM table records!");
1750 ret = amdgpu_ras_add_bad_pages(adev, bps, control->num_recs);
1757 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
1760 struct ras_err_handler_data *data = con->eh_data;
1763 addr >>= AMDGPU_GPU_PAGE_SHIFT;
1764 for (i = 0; i < data->count; i++)
1765 if (addr == data->bps[i].retired_page)
1772 * check if an address belongs to bad page
1774 * Note: this check is only for umc block
1776 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
1779 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1782 if (!con || !con->eh_data)
1785 mutex_lock(&con->recovery_lock);
1786 ret = amdgpu_ras_check_bad_page_unlock(con, addr);
1787 mutex_unlock(&con->recovery_lock);
1791 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
1792 uint32_t max_length)
1794 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1795 int tmp_threshold = amdgpu_bad_page_threshold;
1799 * Justification of value bad_page_cnt_threshold in ras structure
1801 * Generally, -1 <= amdgpu_bad_page_threshold <= max record length
1802 * in eeprom, and introduce two scenarios accordingly.
1804 * Bad page retirement enablement:
1805 * - If amdgpu_bad_page_threshold = -1,
1806 * bad_page_cnt_threshold = typical value by formula.
1808 * - When the value from user is 0 < amdgpu_bad_page_threshold <
1809 * max record length in eeprom, use it directly.
1811 * Bad page retirement disablement:
1812 * - If amdgpu_bad_page_threshold = 0, bad page retirement
1813 * functionality is disabled, and bad_page_cnt_threshold will
1817 if (tmp_threshold < -1)
1819 else if (tmp_threshold > max_length)
1820 tmp_threshold = max_length;
1822 if (tmp_threshold == -1) {
1823 val = adev->gmc.mc_vram_size;
1824 do_div(val, RAS_BAD_PAGE_RATE);
1825 con->bad_page_cnt_threshold = min(lower_32_bits(val),
1828 con->bad_page_cnt_threshold = tmp_threshold;
1832 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1834 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1835 struct ras_err_handler_data **data;
1836 uint32_t max_eeprom_records_len = 0;
1837 bool exc_err_limit = false;
1840 if (adev->ras_features && con)
1841 data = &con->eh_data;
1845 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
1851 mutex_init(&con->recovery_lock);
1852 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1853 atomic_set(&con->in_recovery, 0);
1856 max_eeprom_records_len = amdgpu_ras_eeprom_get_record_max_length();
1857 amdgpu_ras_validate_threshold(adev, max_eeprom_records_len);
1859 /* Todo: During test the SMU might fail to read the eeprom through I2C
1860 * when the GPU is pending on XGMI reset during probe time
1861 * (Mostly after second bus reset), skip it now
1863 if (adev->gmc.xgmi.pending_reset)
1865 ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit);
1867 * This calling fails when exc_err_limit is true or
1870 if (exc_err_limit || ret)
1873 if (con->eeprom_control.num_recs) {
1874 ret = amdgpu_ras_load_bad_pages(adev);
1882 kfree((*data)->bps);
1884 con->eh_data = NULL;
1886 dev_warn(adev->dev, "Failed to initialize ras recovery!\n");
1889 * Except error threshold exceeding case, other failure cases in this
1890 * function would not fail amdgpu driver init.
1900 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1902 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1903 struct ras_err_handler_data *data = con->eh_data;
1905 /* recovery_init failed to init it, fini is useless */
1909 cancel_work_sync(&con->recovery_work);
1911 mutex_lock(&con->recovery_lock);
1912 con->eh_data = NULL;
1915 mutex_unlock(&con->recovery_lock);
1921 /* return 0 if ras will reset gpu and repost.*/
1922 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1925 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1930 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1934 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev)
1936 return adev->asic_type == CHIP_VEGA10 ||
1937 adev->asic_type == CHIP_VEGA20 ||
1938 adev->asic_type == CHIP_ARCTURUS ||
1939 adev->asic_type == CHIP_SIENNA_CICHLID;
1943 * check hardware's ras ability which will be saved in hw_supported.
1944 * if hardware does not support ras, we can skip some ras initializtion and
1945 * forbid some ras operations from IP.
1946 * if software itself, say boot parameter, limit the ras ability. We still
1947 * need allow IP do some limited operations, like disable. In such case,
1948 * we have to initialize ras as normal. but need check if operation is
1949 * allowed or not in each function.
1951 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1952 uint32_t *hw_supported, uint32_t *supported)
1957 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
1958 !amdgpu_ras_asic_supported(adev))
1961 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
1962 dev_info(adev->dev, "MEM ECC is active.\n");
1963 *hw_supported |= (1 << AMDGPU_RAS_BLOCK__UMC |
1964 1 << AMDGPU_RAS_BLOCK__DF);
1966 dev_info(adev->dev, "MEM ECC is not presented.\n");
1968 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
1969 dev_info(adev->dev, "SRAM ECC is active.\n");
1970 *hw_supported |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
1971 1 << AMDGPU_RAS_BLOCK__DF);
1973 dev_info(adev->dev, "SRAM ECC is not presented.\n");
1975 /* hw_supported needs to be aligned with RAS block mask. */
1976 *hw_supported &= AMDGPU_RAS_BLOCK_MASK;
1978 *supported = amdgpu_ras_enable == 0 ?
1979 0 : *hw_supported & amdgpu_ras_mask;
1980 adev->ras_features = *supported;
1983 int amdgpu_ras_init(struct amdgpu_device *adev)
1985 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1991 con = kmalloc(sizeof(struct amdgpu_ras) +
1992 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1993 GFP_KERNEL|__GFP_ZERO);
1997 con->objs = (struct ras_manager *)(con + 1);
1999 amdgpu_ras_set_context(adev, con);
2001 amdgpu_ras_check_supported(adev, &con->hw_supported,
2003 if (!con->hw_supported || (adev->asic_type == CHIP_VEGA10)) {
2004 /* set gfx block ras context feature for VEGA20 Gaming
2005 * send ras disable cmd to ras ta during ras late init.
2007 if (!adev->ras_features && adev->asic_type == CHIP_VEGA20) {
2008 con->features |= BIT(AMDGPU_RAS_BLOCK__GFX);
2018 INIT_LIST_HEAD(&con->head);
2019 /* Might need get this flag from vbios. */
2020 con->flags = RAS_DEFAULT_FLAGS;
2022 if (adev->nbio.funcs->init_ras_controller_interrupt) {
2023 r = adev->nbio.funcs->init_ras_controller_interrupt(adev);
2028 if (adev->nbio.funcs->init_ras_err_event_athub_interrupt) {
2029 r = adev->nbio.funcs->init_ras_err_event_athub_interrupt(adev);
2034 if (amdgpu_ras_fs_init(adev)) {
2039 dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
2040 "hardware ability[%x] ras_mask[%x]\n",
2041 con->hw_supported, con->supported);
2044 amdgpu_ras_set_context(adev, NULL);
2050 /* helper function to handle common stuff in ip late init phase */
2051 int amdgpu_ras_late_init(struct amdgpu_device *adev,
2052 struct ras_common_if *ras_block,
2053 struct ras_fs_if *fs_info,
2054 struct ras_ih_if *ih_info)
2058 /* disable RAS feature per IP block if it is not supported */
2059 if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
2060 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
2064 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
2067 /* request gpu reset. will run again */
2068 amdgpu_ras_request_reset_on_boot(adev,
2071 } else if (adev->in_suspend || amdgpu_in_reset(adev)) {
2072 /* in resume phase, if fail to enable ras,
2073 * clean up all ras fs nodes, and disable ras */
2079 /* in resume phase, no need to create ras fs node */
2080 if (adev->in_suspend || amdgpu_in_reset(adev))
2084 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
2089 r = amdgpu_ras_sysfs_create(adev, fs_info);
2095 amdgpu_ras_sysfs_remove(adev, ras_block);
2098 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2100 amdgpu_ras_feature_enable(adev, ras_block, 0);
2104 /* helper function to remove ras fs node and interrupt handler */
2105 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
2106 struct ras_common_if *ras_block,
2107 struct ras_ih_if *ih_info)
2109 if (!ras_block || !ih_info)
2112 amdgpu_ras_sysfs_remove(adev, ras_block);
2114 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2115 amdgpu_ras_feature_enable(adev, ras_block, 0);
2118 /* do some init work after IP late init as dependence.
2119 * and it runs in resume/gpu reset/booting up cases.
2121 void amdgpu_ras_resume(struct amdgpu_device *adev)
2123 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2124 struct ras_manager *obj, *tmp;
2126 if (!adev->ras_features || !con) {
2127 /* clean ras context for VEGA20 Gaming after send ras disable cmd */
2128 amdgpu_release_ras_context(adev);
2133 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2134 /* Set up all other IPs which are not implemented. There is a
2135 * tricky thing that IP's actual ras error type should be
2136 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2137 * ERROR_NONE make sense anyway.
2139 amdgpu_ras_enable_all_features(adev, 1);
2141 /* We enable ras on all hw_supported block, but as boot
2142 * parameter might disable some of them and one or more IP has
2143 * not implemented yet. So we disable them on behalf.
2145 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2146 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2147 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2148 /* there should be no any reference. */
2149 WARN_ON(alive_obj(obj));
2154 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
2155 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
2156 /* setup ras obj state as disabled.
2157 * for init_by_vbios case.
2158 * if we want to enable ras, just enable it in a normal way.
2159 * If we want do disable it, need setup ras obj as enabled,
2160 * then issue another TA disable cmd.
2161 * See feature_enable_on_boot
2163 amdgpu_ras_disable_all_features(adev, 1);
2164 amdgpu_ras_reset_gpu(adev);
2168 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2170 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2172 if (!adev->ras_features || !con)
2175 amdgpu_ras_disable_all_features(adev, 0);
2176 /* Make sure all ras objects are disabled. */
2178 amdgpu_ras_disable_all_features(adev, 1);
2181 /* do some fini work before IP fini as dependence */
2182 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2184 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2186 if (!adev->ras_features || !con)
2189 /* Need disable ras on all IPs here before ip [hw/sw]fini */
2190 amdgpu_ras_disable_all_features(adev, 0);
2191 amdgpu_ras_recovery_fini(adev);
2195 int amdgpu_ras_fini(struct amdgpu_device *adev)
2197 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2199 if (!adev->ras_features || !con)
2202 amdgpu_ras_fs_fini(adev);
2203 amdgpu_ras_interrupt_remove_all(adev);
2205 WARN(con->features, "Feature mask is not cleared");
2208 amdgpu_ras_disable_all_features(adev, 1);
2210 amdgpu_ras_set_context(adev, NULL);
2216 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2218 uint32_t hw_supported, supported;
2220 amdgpu_ras_check_supported(adev, &hw_supported, &supported);
2224 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2225 dev_info(adev->dev, "uncorrectable hardware error"
2226 "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2228 amdgpu_ras_reset_gpu(adev);
2232 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
2234 if (adev->asic_type == CHIP_VEGA20 &&
2235 adev->pm.fw_version <= 0x283400) {
2236 return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
2237 amdgpu_ras_intr_triggered();
2243 void amdgpu_release_ras_context(struct amdgpu_device *adev)
2245 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2250 if (!adev->ras_features && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) {
2251 con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX);
2252 amdgpu_ras_set_context(adev, NULL);