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 const char *ras_error_string[] = {
41 "multi_uncorrectable",
45 const char *ras_block_string[] = {
62 #define ras_err_str(i) (ras_error_string[ffs(i)])
63 #define ras_block_str(i) (ras_block_string[i])
65 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
66 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET 2
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 enum amdgpu_ras_retire_page_reservation {
73 AMDGPU_RAS_RETIRE_PAGE_RESERVED,
74 AMDGPU_RAS_RETIRE_PAGE_PENDING,
75 AMDGPU_RAS_RETIRE_PAGE_FAULT,
78 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
80 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
83 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
85 if (adev && amdgpu_ras_get_context(adev))
86 amdgpu_ras_get_context(adev)->error_query_ready = ready;
89 bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
91 if (adev && amdgpu_ras_get_context(adev))
92 return amdgpu_ras_get_context(adev)->error_query_ready;
97 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
98 size_t size, loff_t *pos)
100 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
101 struct ras_query_if info = {
107 if (amdgpu_ras_error_query(obj->adev, &info))
110 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
112 "ce", info.ce_count);
117 s = min_t(u64, s, size);
120 if (copy_to_user(buf, &val[*pos], s))
128 static const struct file_operations amdgpu_ras_debugfs_ops = {
129 .owner = THIS_MODULE,
130 .read = amdgpu_ras_debugfs_read,
132 .llseek = default_llseek
135 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
139 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
141 if (strcmp(name, ras_block_str(i)) == 0)
147 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
148 const char __user *buf, size_t size,
149 loff_t *pos, struct ras_debug_if *data)
151 ssize_t s = min_t(u64, 64, size);
164 memset(str, 0, sizeof(str));
165 memset(data, 0, sizeof(*data));
167 if (copy_from_user(str, buf, s))
170 if (sscanf(str, "disable %32s", block_name) == 1)
172 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
174 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
176 else if (str[0] && str[1] && str[2] && str[3])
177 /* ascii string, but commands are not matched. */
181 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
184 data->head.block = block_id;
185 /* only ue and ce errors are supported */
186 if (!memcmp("ue", err, 2))
187 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
188 else if (!memcmp("ce", err, 2))
189 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
196 if (sscanf(str, "%*s %*s %*s %u %llu %llu",
197 &sub_block, &address, &value) != 3)
198 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
199 &sub_block, &address, &value) != 3)
201 data->head.sub_block_index = sub_block;
202 data->inject.address = address;
203 data->inject.value = value;
206 if (size < sizeof(*data))
209 if (copy_from_user(data, buf, sizeof(*data)))
217 * DOC: AMDGPU RAS debugfs control interface
219 * It accepts struct ras_debug_if who has two members.
221 * First member: ras_debug_if::head or ras_debug_if::inject.
223 * head is used to indicate which IP block will be under control.
225 * head has four members, they are block, type, sub_block_index, name.
226 * block: which IP will be under control.
227 * type: what kind of error will be enabled/disabled/injected.
228 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
229 * name: the name of IP.
231 * inject has two more members than head, they are address, value.
232 * As their names indicate, inject operation will write the
233 * value to the address.
235 * The second member: struct ras_debug_if::op.
236 * It has three kinds of operations.
238 * - 0: disable RAS on the block. Take ::head as its data.
239 * - 1: enable RAS on the block. Take ::head as its data.
240 * - 2: inject errors on the block. Take ::inject as its data.
242 * How to use the interface?
246 * Copy the struct ras_debug_if in your codes and initialize it.
247 * Write the struct to the control node.
251 * .. code-block:: bash
253 * echo op block [error [sub_block address value]] > .../ras/ras_ctrl
257 * op: disable, enable, inject
258 * disable: only block is needed
259 * enable: block and error are needed
260 * inject: error, address, value are needed
261 * block: umc, sdma, gfx, .........
262 * see ras_block_string[] for details
264 * ue: multi_uncorrectable
265 * ce: single_correctable
267 * sub block index, pass 0 if there is no sub block
269 * here are some examples for bash commands:
271 * .. code-block:: bash
273 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
274 * echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
275 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
277 * How to check the result?
279 * For disable/enable, please check ras features at
280 * /sys/class/drm/card[0/1/2...]/device/ras/features
282 * For inject, please check corresponding err count at
283 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
286 * Operations are only allowed on blocks which are supported.
287 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
288 * to see which blocks support RAS on a particular asic.
291 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
292 size_t size, loff_t *pos)
294 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
295 struct ras_debug_if data;
298 if (!amdgpu_ras_get_error_query_ready(adev)) {
299 dev_warn(adev->dev, "RAS WARN: error injection "
300 "currently inaccessible\n");
304 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
308 if (!amdgpu_ras_is_supported(adev, data.head.block))
313 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
316 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
319 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
320 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
325 /* umc ce/ue error injection for a bad page is not allowed */
326 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
327 amdgpu_ras_check_bad_page(adev, data.inject.address)) {
328 dev_warn(adev->dev, "RAS WARN: 0x%llx has been marked "
329 "as bad before error injection!\n",
330 data.inject.address);
334 /* data.inject.address is offset instead of absolute gpu address */
335 ret = amdgpu_ras_error_inject(adev, &data.inject);
349 * DOC: AMDGPU RAS debugfs EEPROM table reset interface
351 * Some boards contain an EEPROM which is used to persistently store a list of
352 * bad pages which experiences ECC errors in vram. This interface provides
353 * a way to reset the EEPROM, e.g., after testing error injection.
357 * .. code-block:: bash
359 * echo 1 > ../ras/ras_eeprom_reset
361 * will reset EEPROM table to 0 entries.
364 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, const char __user *buf,
365 size_t size, loff_t *pos)
367 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
370 ret = amdgpu_ras_eeprom_reset_table(&adev->psp.ras.ras->eeprom_control);
372 return ret == 1 ? size : -EIO;
375 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
376 .owner = THIS_MODULE,
378 .write = amdgpu_ras_debugfs_ctrl_write,
379 .llseek = default_llseek
382 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
383 .owner = THIS_MODULE,
385 .write = amdgpu_ras_debugfs_eeprom_write,
386 .llseek = default_llseek
390 * DOC: AMDGPU RAS sysfs Error Count Interface
392 * It allows the user to read the error count for each IP block on the gpu through
393 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
395 * It outputs the multiple lines which report the uncorrected (ue) and corrected
398 * The format of one line is below,
404 * .. code-block:: bash
410 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
411 struct device_attribute *attr, char *buf)
413 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
414 struct ras_query_if info = {
418 if (!amdgpu_ras_get_error_query_ready(obj->adev))
419 return snprintf(buf, PAGE_SIZE,
420 "Query currently inaccessible\n");
422 if (amdgpu_ras_error_query(obj->adev, &info))
425 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
427 "ce", info.ce_count);
432 #define get_obj(obj) do { (obj)->use++; } while (0)
433 #define alive_obj(obj) ((obj)->use)
435 static inline void put_obj(struct ras_manager *obj)
437 if (obj && --obj->use == 0)
438 list_del(&obj->node);
439 if (obj && obj->use < 0) {
440 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
444 /* make one obj and return it. */
445 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
446 struct ras_common_if *head)
448 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
449 struct ras_manager *obj;
454 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
457 obj = &con->objs[head->block];
458 /* already exist. return obj? */
464 list_add(&obj->node, &con->head);
470 /* return an obj equal to head, or the first when head is NULL */
471 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
472 struct ras_common_if *head)
474 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
475 struct ras_manager *obj;
482 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
485 obj = &con->objs[head->block];
487 if (alive_obj(obj)) {
488 WARN_ON(head->block != obj->head.block);
492 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
494 if (alive_obj(obj)) {
495 WARN_ON(i != obj->head.block);
505 void amdgpu_ras_parse_status_code(struct amdgpu_device* adev,
506 const char* invoke_type,
507 const char* block_name,
508 enum ta_ras_status ret)
511 case TA_RAS_STATUS__SUCCESS:
513 case TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE:
515 "RAS WARN: %s %s currently unavailable\n",
521 "RAS ERROR: %s %s error failed ret 0x%X\n",
528 /* feature ctl begin */
529 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
530 struct ras_common_if *head)
532 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
534 return con->hw_supported & BIT(head->block);
537 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
538 struct ras_common_if *head)
540 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
542 return con->features & BIT(head->block);
546 * if obj is not created, then create one.
547 * set feature enable flag.
549 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
550 struct ras_common_if *head, int enable)
552 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
553 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
555 /* If hardware does not support ras, then do not create obj.
556 * But if hardware support ras, we can create the obj.
557 * Ras framework checks con->hw_supported to see if it need do
558 * corresponding initialization.
559 * IP checks con->support to see if it need disable ras.
561 if (!amdgpu_ras_is_feature_allowed(adev, head))
563 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
568 obj = amdgpu_ras_create_obj(adev, head);
572 /* In case we create obj somewhere else */
575 con->features |= BIT(head->block);
577 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
578 con->features &= ~BIT(head->block);
586 /* wrapper of psp_ras_enable_features */
587 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
588 struct ras_common_if *head, bool enable)
590 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
591 union ta_ras_cmd_input *info;
597 info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
602 info->disable_features = (struct ta_ras_disable_features_input) {
603 .block_id = amdgpu_ras_block_to_ta(head->block),
604 .error_type = amdgpu_ras_error_to_ta(head->type),
607 info->enable_features = (struct ta_ras_enable_features_input) {
608 .block_id = amdgpu_ras_block_to_ta(head->block),
609 .error_type = amdgpu_ras_error_to_ta(head->type),
613 /* Do not enable if it is not allowed. */
614 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
615 /* Are we alerady in that state we are going to set? */
616 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head))) {
621 if (!amdgpu_ras_intr_triggered()) {
622 ret = psp_ras_enable_features(&adev->psp, info, enable);
624 amdgpu_ras_parse_status_code(adev,
625 enable ? "enable":"disable",
626 ras_block_str(head->block),
627 (enum ta_ras_status)ret);
628 if (ret == TA_RAS_STATUS__RESET_NEEDED)
638 __amdgpu_ras_feature_enable(adev, head, enable);
645 /* Only used in device probe stage and called only once. */
646 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
647 struct ras_common_if *head, bool enable)
649 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
655 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
657 /* There is no harm to issue a ras TA cmd regardless of
658 * the currecnt ras state.
659 * If current state == target state, it will do nothing
660 * But sometimes it requests driver to reset and repost
661 * with error code -EAGAIN.
663 ret = amdgpu_ras_feature_enable(adev, head, 1);
664 /* With old ras TA, we might fail to enable ras.
665 * Log it and just setup the object.
666 * TODO need remove this WA in the future.
668 if (ret == -EINVAL) {
669 ret = __amdgpu_ras_feature_enable(adev, head, 1);
672 "RAS INFO: %s setup object\n",
673 ras_block_str(head->block));
676 /* setup the object then issue a ras TA disable cmd.*/
677 ret = __amdgpu_ras_feature_enable(adev, head, 1);
681 ret = amdgpu_ras_feature_enable(adev, head, 0);
684 ret = amdgpu_ras_feature_enable(adev, head, enable);
689 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
692 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
693 struct ras_manager *obj, *tmp;
695 list_for_each_entry_safe(obj, tmp, &con->head, node) {
697 * aka just release the obj and corresponding flags
700 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
703 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
708 return con->features;
711 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
714 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
715 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
717 const enum amdgpu_ras_error_type default_ras_type =
718 AMDGPU_RAS_ERROR__NONE;
720 for (i = 0; i < ras_block_count; i++) {
721 struct ras_common_if head = {
723 .type = default_ras_type,
724 .sub_block_index = 0,
726 strcpy(head.name, ras_block_str(i));
729 * bypass psp. vbios enable ras for us.
730 * so just create the obj
732 if (__amdgpu_ras_feature_enable(adev, &head, 1))
735 if (amdgpu_ras_feature_enable(adev, &head, 1))
740 return con->features;
742 /* feature ctl end */
744 /* query/inject/cure begin */
745 int amdgpu_ras_error_query(struct amdgpu_device *adev,
746 struct ras_query_if *info)
748 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
749 struct ras_err_data err_data = {0, 0, 0, NULL};
755 switch (info->head.block) {
756 case AMDGPU_RAS_BLOCK__UMC:
757 if (adev->umc.funcs->query_ras_error_count)
758 adev->umc.funcs->query_ras_error_count(adev, &err_data);
759 /* umc query_ras_error_address is also responsible for clearing
762 if (adev->umc.funcs->query_ras_error_address)
763 adev->umc.funcs->query_ras_error_address(adev, &err_data);
765 case AMDGPU_RAS_BLOCK__SDMA:
766 if (adev->sdma.funcs->query_ras_error_count) {
767 for (i = 0; i < adev->sdma.num_instances; i++)
768 adev->sdma.funcs->query_ras_error_count(adev, i,
772 case AMDGPU_RAS_BLOCK__GFX:
773 if (adev->gfx.funcs->query_ras_error_count)
774 adev->gfx.funcs->query_ras_error_count(adev, &err_data);
776 case AMDGPU_RAS_BLOCK__MMHUB:
777 if (adev->mmhub.funcs->query_ras_error_count)
778 adev->mmhub.funcs->query_ras_error_count(adev, &err_data);
780 case AMDGPU_RAS_BLOCK__PCIE_BIF:
781 if (adev->nbio.funcs->query_ras_error_count)
782 adev->nbio.funcs->query_ras_error_count(adev, &err_data);
784 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
785 amdgpu_xgmi_query_ras_error_count(adev, &err_data);
791 obj->err_data.ue_count += err_data.ue_count;
792 obj->err_data.ce_count += err_data.ce_count;
794 info->ue_count = obj->err_data.ue_count;
795 info->ce_count = obj->err_data.ce_count;
797 if (err_data.ce_count) {
798 dev_info(adev->dev, "%ld correctable hardware errors "
799 "detected in %s block, no user "
800 "action is needed.\n",
801 obj->err_data.ce_count,
802 ras_block_str(info->head.block));
804 if (err_data.ue_count) {
805 dev_info(adev->dev, "%ld uncorrectable hardware errors "
806 "detected in %s block\n",
807 obj->err_data.ue_count,
808 ras_block_str(info->head.block));
814 /* wrapper of psp_ras_trigger_error */
815 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
816 struct ras_inject_if *info)
818 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
819 struct ta_ras_trigger_error_input block_info = {
820 .block_id = amdgpu_ras_block_to_ta(info->head.block),
821 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
822 .sub_block_index = info->head.sub_block_index,
823 .address = info->address,
824 .value = info->value,
831 /* Calculate XGMI relative offset */
832 if (adev->gmc.xgmi.num_physical_nodes > 1) {
834 amdgpu_xgmi_get_relative_phy_addr(adev,
838 switch (info->head.block) {
839 case AMDGPU_RAS_BLOCK__GFX:
840 if (adev->gfx.funcs->ras_error_inject)
841 ret = adev->gfx.funcs->ras_error_inject(adev, info);
845 case AMDGPU_RAS_BLOCK__UMC:
846 case AMDGPU_RAS_BLOCK__MMHUB:
847 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
848 case AMDGPU_RAS_BLOCK__PCIE_BIF:
849 ret = psp_ras_trigger_error(&adev->psp, &block_info);
852 dev_info(adev->dev, "%s error injection is not supported yet\n",
853 ras_block_str(info->head.block));
857 amdgpu_ras_parse_status_code(adev,
859 ras_block_str(info->head.block),
860 (enum ta_ras_status)ret);
865 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
866 struct ras_cure_if *info)
868 /* psp fw has no cure interface for now. */
872 /* get the total error counts on all IPs */
873 unsigned long amdgpu_ras_query_error_count(struct amdgpu_device *adev,
876 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
877 struct ras_manager *obj;
878 struct ras_err_data data = {0, 0};
883 list_for_each_entry(obj, &con->head, node) {
884 struct ras_query_if info = {
888 if (amdgpu_ras_error_query(adev, &info))
891 data.ce_count += info.ce_count;
892 data.ue_count += info.ue_count;
895 return is_ce ? data.ce_count : data.ue_count;
897 /* query/inject/cure end */
902 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
903 struct ras_badpage **bps, unsigned int *count);
905 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
908 case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
910 case AMDGPU_RAS_RETIRE_PAGE_PENDING:
912 case AMDGPU_RAS_RETIRE_PAGE_FAULT:
919 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
921 * It allows user to read the bad pages of vram on the gpu through
922 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
924 * It outputs multiple lines, and each line stands for one gpu page.
926 * The format of one line is below,
927 * gpu pfn : gpu page size : flags
929 * gpu pfn and gpu page size are printed in hex format.
930 * flags can be one of below character,
932 * R: reserved, this gpu page is reserved and not able to use.
934 * P: pending for reserve, this gpu page is marked as bad, will be reserved
935 * in next window of page_reserve.
937 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
941 * .. code-block:: bash
943 * 0x00000001 : 0x00001000 : R
944 * 0x00000002 : 0x00001000 : P
948 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
949 struct kobject *kobj, struct bin_attribute *attr,
950 char *buf, loff_t ppos, size_t count)
952 struct amdgpu_ras *con =
953 container_of(attr, struct amdgpu_ras, badpages_attr);
954 struct amdgpu_device *adev = con->adev;
955 const unsigned int element_size =
956 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
957 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
958 unsigned int end = div64_ul(ppos + count - 1, element_size);
960 struct ras_badpage *bps = NULL;
961 unsigned int bps_count = 0;
963 memset(buf, 0, count);
965 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
968 for (; start < end && start < bps_count; start++)
969 s += scnprintf(&buf[s], element_size + 1,
970 "0x%08x : 0x%08x : %1s\n",
973 amdgpu_ras_badpage_flags_str(bps[start].flags));
980 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
981 struct device_attribute *attr, char *buf)
983 struct amdgpu_ras *con =
984 container_of(attr, struct amdgpu_ras, features_attr);
986 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
989 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
991 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
992 struct attribute *attrs[] = {
993 &con->features_attr.attr,
996 struct bin_attribute *bin_attrs[] = {
1000 struct attribute_group group = {
1003 .bin_attrs = bin_attrs,
1006 con->features_attr = (struct device_attribute) {
1011 .show = amdgpu_ras_sysfs_features_read,
1014 con->badpages_attr = (struct bin_attribute) {
1016 .name = "gpu_vram_bad_pages",
1021 .read = amdgpu_ras_sysfs_badpages_read,
1024 sysfs_attr_init(attrs[0]);
1025 sysfs_bin_attr_init(bin_attrs[0]);
1027 return sysfs_create_group(&adev->dev->kobj, &group);
1030 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1032 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1033 struct attribute *attrs[] = {
1034 &con->features_attr.attr,
1037 struct bin_attribute *bin_attrs[] = {
1038 &con->badpages_attr,
1041 struct attribute_group group = {
1044 .bin_attrs = bin_attrs,
1047 sysfs_remove_group(&adev->dev->kobj, &group);
1052 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1053 struct ras_fs_if *head)
1055 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1057 if (!obj || obj->attr_inuse)
1062 memcpy(obj->fs_data.sysfs_name,
1064 sizeof(obj->fs_data.sysfs_name));
1066 obj->sysfs_attr = (struct device_attribute){
1068 .name = obj->fs_data.sysfs_name,
1071 .show = amdgpu_ras_sysfs_read,
1073 sysfs_attr_init(&obj->sysfs_attr.attr);
1075 if (sysfs_add_file_to_group(&adev->dev->kobj,
1076 &obj->sysfs_attr.attr,
1082 obj->attr_inuse = 1;
1087 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1088 struct ras_common_if *head)
1090 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1092 if (!obj || !obj->attr_inuse)
1095 sysfs_remove_file_from_group(&adev->dev->kobj,
1096 &obj->sysfs_attr.attr,
1098 obj->attr_inuse = 0;
1104 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1106 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1107 struct ras_manager *obj, *tmp;
1109 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1110 amdgpu_ras_sysfs_remove(adev, &obj->head);
1113 amdgpu_ras_sysfs_remove_feature_node(adev);
1120 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1122 * Normally when there is an uncorrectable error, the driver will reset
1123 * the GPU to recover. However, in the event of an unrecoverable error,
1124 * the driver provides an interface to reboot the system automatically
1127 * The following file in debugfs provides that interface:
1128 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1132 * .. code-block:: bash
1134 * echo true > .../ras/auto_reboot
1138 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1140 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1141 struct drm_minor *minor = adev->ddev->primary;
1143 con->dir = debugfs_create_dir("ras", minor->debugfs_root);
1144 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
1145 adev, &amdgpu_ras_debugfs_ctrl_ops);
1146 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, con->dir,
1147 adev, &amdgpu_ras_debugfs_eeprom_ops);
1150 * After one uncorrectable error happens, usually GPU recovery will
1151 * be scheduled. But due to the known problem in GPU recovery failing
1152 * to bring GPU back, below interface provides one direct way to
1153 * user to reboot system automatically in such case within
1154 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1155 * will never be called.
1157 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, con->dir,
1161 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1162 struct ras_fs_if *head)
1164 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1165 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1167 if (!obj || obj->ent)
1172 memcpy(obj->fs_data.debugfs_name,
1174 sizeof(obj->fs_data.debugfs_name));
1176 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
1177 S_IWUGO | S_IRUGO, con->dir, obj,
1178 &amdgpu_ras_debugfs_ops);
1181 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1183 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1184 struct ras_manager *obj;
1185 struct ras_fs_if fs_info;
1188 * it won't be called in resume path, no need to check
1189 * suspend and gpu reset status
1194 amdgpu_ras_debugfs_create_ctrl_node(adev);
1196 list_for_each_entry(obj, &con->head, node) {
1197 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1198 (obj->attr_inuse == 1)) {
1199 sprintf(fs_info.debugfs_name, "%s_err_inject",
1200 ras_block_str(obj->head.block));
1201 fs_info.head = obj->head;
1202 amdgpu_ras_debugfs_create(adev, &fs_info);
1207 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1208 struct ras_common_if *head)
1210 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1212 if (!obj || !obj->ent)
1215 debugfs_remove(obj->ent);
1220 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1222 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1223 struct ras_manager *obj, *tmp;
1225 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1226 amdgpu_ras_debugfs_remove(adev, &obj->head);
1229 debugfs_remove_recursive(con->dir);
1236 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1238 amdgpu_ras_sysfs_create_feature_node(adev);
1243 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1245 amdgpu_ras_debugfs_remove_all(adev);
1246 amdgpu_ras_sysfs_remove_all(adev);
1252 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1254 struct ras_ih_data *data = &obj->ih_data;
1255 struct amdgpu_iv_entry entry;
1257 struct ras_err_data err_data = {0, 0, 0, NULL};
1259 while (data->rptr != data->wptr) {
1261 memcpy(&entry, &data->ring[data->rptr],
1262 data->element_size);
1265 data->rptr = (data->aligned_element_size +
1266 data->rptr) % data->ring_size;
1268 /* Let IP handle its data, maybe we need get the output
1269 * from the callback to udpate the error type/count, etc
1272 ret = data->cb(obj->adev, &err_data, &entry);
1273 /* ue will trigger an interrupt, and in that case
1274 * we need do a reset to recovery the whole system.
1275 * But leave IP do that recovery, here we just dispatch
1278 if (ret == AMDGPU_RAS_SUCCESS) {
1279 /* these counts could be left as 0 if
1280 * some blocks do not count error number
1282 obj->err_data.ue_count += err_data.ue_count;
1283 obj->err_data.ce_count += err_data.ce_count;
1289 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1291 struct ras_ih_data *data =
1292 container_of(work, struct ras_ih_data, ih_work);
1293 struct ras_manager *obj =
1294 container_of(data, struct ras_manager, ih_data);
1296 amdgpu_ras_interrupt_handler(obj);
1299 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1300 struct ras_dispatch_if *info)
1302 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1303 struct ras_ih_data *data = &obj->ih_data;
1308 if (data->inuse == 0)
1311 /* Might be overflow... */
1312 memcpy(&data->ring[data->wptr], info->entry,
1313 data->element_size);
1316 data->wptr = (data->aligned_element_size +
1317 data->wptr) % data->ring_size;
1319 schedule_work(&data->ih_work);
1324 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1325 struct ras_ih_if *info)
1327 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1328 struct ras_ih_data *data;
1333 data = &obj->ih_data;
1334 if (data->inuse == 0)
1337 cancel_work_sync(&data->ih_work);
1340 memset(data, 0, sizeof(*data));
1346 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1347 struct ras_ih_if *info)
1349 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1350 struct ras_ih_data *data;
1353 /* in case we registe the IH before enable ras feature */
1354 obj = amdgpu_ras_create_obj(adev, &info->head);
1360 data = &obj->ih_data;
1361 /* add the callback.etc */
1362 *data = (struct ras_ih_data) {
1365 .element_size = sizeof(struct amdgpu_iv_entry),
1370 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1372 data->aligned_element_size = ALIGN(data->element_size, 8);
1373 /* the ring can store 64 iv entries. */
1374 data->ring_size = 64 * data->aligned_element_size;
1375 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1387 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1389 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1390 struct ras_manager *obj, *tmp;
1392 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1393 struct ras_ih_if info = {
1396 amdgpu_ras_interrupt_remove_handler(adev, &info);
1403 /* traversal all IPs except NBIO to query error counter */
1404 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1406 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1407 struct ras_manager *obj;
1412 list_for_each_entry(obj, &con->head, node) {
1413 struct ras_query_if info = {
1418 * PCIE_BIF IP has one different isr by ras controller
1419 * interrupt, the specific ras counter query will be
1420 * done in that isr. So skip such block from common
1421 * sync flood interrupt isr calling.
1423 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1426 amdgpu_ras_error_query(adev, &info);
1430 /* recovery begin */
1432 /* return 0 on success.
1433 * caller need free bps.
1435 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1436 struct ras_badpage **bps, unsigned int *count)
1438 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1439 struct ras_err_handler_data *data;
1443 if (!con || !con->eh_data || !bps || !count)
1446 mutex_lock(&con->recovery_lock);
1447 data = con->eh_data;
1448 if (!data || data->count == 0) {
1454 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1460 for (; i < data->count; i++) {
1461 (*bps)[i] = (struct ras_badpage){
1462 .bp = data->bps[i].retired_page,
1463 .size = AMDGPU_GPU_PAGE_SIZE,
1464 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1467 if (data->last_reserved <= i)
1468 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1469 else if (data->bps_bo[i] == NULL)
1470 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1473 *count = data->count;
1475 mutex_unlock(&con->recovery_lock);
1479 static void amdgpu_ras_do_recovery(struct work_struct *work)
1481 struct amdgpu_ras *ras =
1482 container_of(work, struct amdgpu_ras, recovery_work);
1483 struct amdgpu_device *remote_adev = NULL;
1484 struct amdgpu_device *adev = ras->adev;
1485 struct list_head device_list, *device_list_handle = NULL;
1486 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev, false);
1488 /* Build list of devices to query RAS related errors */
1489 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
1490 device_list_handle = &hive->device_list;
1492 INIT_LIST_HEAD(&device_list);
1493 list_add_tail(&adev->gmc.xgmi.head, &device_list);
1494 device_list_handle = &device_list;
1497 list_for_each_entry(remote_adev, device_list_handle, gmc.xgmi.head) {
1498 amdgpu_ras_log_on_err_counter(remote_adev);
1501 if (amdgpu_device_should_recover_gpu(ras->adev))
1502 amdgpu_device_gpu_recover(ras->adev, 0);
1503 atomic_set(&ras->in_recovery, 0);
1506 /* alloc/realloc bps array */
1507 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1508 struct ras_err_handler_data *data, int pages)
1510 unsigned int old_space = data->count + data->space_left;
1511 unsigned int new_space = old_space + pages;
1512 unsigned int align_space = ALIGN(new_space, 512);
1513 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1514 struct amdgpu_bo **bps_bo =
1515 kmalloc(align_space * sizeof(*data->bps_bo), GFP_KERNEL);
1517 if (!bps || !bps_bo) {
1524 memcpy(bps, data->bps,
1525 data->count * sizeof(*data->bps));
1529 memcpy(bps_bo, data->bps_bo,
1530 data->count * sizeof(*data->bps_bo));
1531 kfree(data->bps_bo);
1535 data->bps_bo = bps_bo;
1536 data->space_left += align_space - old_space;
1540 /* it deal with vram only. */
1541 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1542 struct eeprom_table_record *bps, int pages)
1544 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1545 struct ras_err_handler_data *data;
1548 if (!con || !con->eh_data || !bps || pages <= 0)
1551 mutex_lock(&con->recovery_lock);
1552 data = con->eh_data;
1556 if (data->space_left <= pages)
1557 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1562 memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
1563 data->count += pages;
1564 data->space_left -= pages;
1567 mutex_unlock(&con->recovery_lock);
1573 * write error record array to eeprom, the function should be
1574 * protected by recovery_lock
1576 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1578 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1579 struct ras_err_handler_data *data;
1580 struct amdgpu_ras_eeprom_control *control;
1583 if (!con || !con->eh_data)
1586 control = &con->eeprom_control;
1587 data = con->eh_data;
1588 save_count = data->count - control->num_recs;
1589 /* only new entries are saved */
1591 if (amdgpu_ras_eeprom_process_recods(control,
1592 &data->bps[control->num_recs],
1595 dev_err(adev->dev, "Failed to save EEPROM table data!");
1603 * read error record array in eeprom and reserve enough space for
1604 * storing new bad pages
1606 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1608 struct amdgpu_ras_eeprom_control *control =
1609 &adev->psp.ras.ras->eeprom_control;
1610 struct eeprom_table_record *bps = NULL;
1613 /* no bad page record, skip eeprom access */
1614 if (!control->num_recs)
1617 bps = kcalloc(control->num_recs, sizeof(*bps), GFP_KERNEL);
1621 if (amdgpu_ras_eeprom_process_recods(control, bps, false,
1622 control->num_recs)) {
1623 dev_err(adev->dev, "Failed to load EEPROM table records!");
1628 ret = amdgpu_ras_add_bad_pages(adev, bps, control->num_recs);
1636 * check if an address belongs to bad page
1638 * Note: this check is only for umc block
1640 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
1643 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1644 struct ras_err_handler_data *data;
1648 if (!con || !con->eh_data)
1651 mutex_lock(&con->recovery_lock);
1652 data = con->eh_data;
1656 addr >>= AMDGPU_GPU_PAGE_SHIFT;
1657 for (i = 0; i < data->count; i++)
1658 if (addr == data->bps[i].retired_page) {
1664 mutex_unlock(&con->recovery_lock);
1668 /* called in gpu recovery/init */
1669 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1671 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1672 struct ras_err_handler_data *data;
1674 struct amdgpu_bo *bo = NULL;
1677 if (!con || !con->eh_data)
1680 mutex_lock(&con->recovery_lock);
1681 data = con->eh_data;
1684 /* reserve vram at driver post stage. */
1685 for (i = data->last_reserved; i < data->count; i++) {
1686 bp = data->bps[i].retired_page;
1688 /* There are two cases of reserve error should be ignored:
1689 * 1) a ras bad page has been allocated (used by someone);
1690 * 2) a ras bad page has been reserved (duplicate error injection
1693 if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
1694 AMDGPU_GPU_PAGE_SIZE,
1695 AMDGPU_GEM_DOMAIN_VRAM,
1697 dev_warn(adev->dev, "RAS WARN: reserve vram for "
1698 "retired page %llx fail\n", bp);
1700 data->bps_bo[i] = bo;
1701 data->last_reserved = i + 1;
1705 /* continue to save bad pages to eeprom even reesrve_vram fails */
1706 ret = amdgpu_ras_save_bad_pages(adev);
1708 mutex_unlock(&con->recovery_lock);
1712 /* called when driver unload */
1713 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1715 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1716 struct ras_err_handler_data *data;
1717 struct amdgpu_bo *bo;
1720 if (!con || !con->eh_data)
1723 mutex_lock(&con->recovery_lock);
1724 data = con->eh_data;
1728 for (i = data->last_reserved - 1; i >= 0; i--) {
1729 bo = data->bps_bo[i];
1731 amdgpu_bo_free_kernel(&bo, NULL, NULL);
1733 data->bps_bo[i] = bo;
1734 data->last_reserved = i;
1737 mutex_unlock(&con->recovery_lock);
1741 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1743 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1744 struct ras_err_handler_data **data;
1748 data = &con->eh_data;
1752 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
1758 mutex_init(&con->recovery_lock);
1759 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1760 atomic_set(&con->in_recovery, 0);
1763 ret = amdgpu_ras_eeprom_init(&con->eeprom_control);
1767 if (con->eeprom_control.num_recs) {
1768 ret = amdgpu_ras_load_bad_pages(adev);
1771 ret = amdgpu_ras_reserve_bad_pages(adev);
1779 amdgpu_ras_release_bad_pages(adev);
1781 kfree((*data)->bps);
1782 kfree((*data)->bps_bo);
1784 con->eh_data = NULL;
1786 dev_warn(adev->dev, "Failed to initialize ras recovery!\n");
1791 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1793 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1794 struct ras_err_handler_data *data = con->eh_data;
1796 /* recovery_init failed to init it, fini is useless */
1800 cancel_work_sync(&con->recovery_work);
1801 amdgpu_ras_release_bad_pages(adev);
1803 mutex_lock(&con->recovery_lock);
1804 con->eh_data = NULL;
1806 kfree(data->bps_bo);
1808 mutex_unlock(&con->recovery_lock);
1814 /* return 0 if ras will reset gpu and repost.*/
1815 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1818 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1823 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1828 * check hardware's ras ability which will be saved in hw_supported.
1829 * if hardware does not support ras, we can skip some ras initializtion and
1830 * forbid some ras operations from IP.
1831 * if software itself, say boot parameter, limit the ras ability. We still
1832 * need allow IP do some limited operations, like disable. In such case,
1833 * we have to initialize ras as normal. but need check if operation is
1834 * allowed or not in each function.
1836 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1837 uint32_t *hw_supported, uint32_t *supported)
1842 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
1843 (adev->asic_type != CHIP_VEGA20 &&
1844 adev->asic_type != CHIP_ARCTURUS))
1847 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
1848 dev_info(adev->dev, "HBM ECC is active.\n");
1849 *hw_supported |= (1 << AMDGPU_RAS_BLOCK__UMC |
1850 1 << AMDGPU_RAS_BLOCK__DF);
1852 dev_info(adev->dev, "HBM ECC is not presented.\n");
1854 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
1855 dev_info(adev->dev, "SRAM ECC is active.\n");
1856 *hw_supported |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
1857 1 << AMDGPU_RAS_BLOCK__DF);
1859 dev_info(adev->dev, "SRAM ECC is not presented.\n");
1861 /* hw_supported needs to be aligned with RAS block mask. */
1862 *hw_supported &= AMDGPU_RAS_BLOCK_MASK;
1864 *supported = amdgpu_ras_enable == 0 ?
1865 0 : *hw_supported & amdgpu_ras_mask;
1868 int amdgpu_ras_init(struct amdgpu_device *adev)
1870 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1876 con = kmalloc(sizeof(struct amdgpu_ras) +
1877 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1878 GFP_KERNEL|__GFP_ZERO);
1882 con->objs = (struct ras_manager *)(con + 1);
1884 amdgpu_ras_set_context(adev, con);
1886 amdgpu_ras_check_supported(adev, &con->hw_supported,
1888 if (!con->hw_supported) {
1889 amdgpu_ras_set_context(adev, NULL);
1895 INIT_LIST_HEAD(&con->head);
1896 /* Might need get this flag from vbios. */
1897 con->flags = RAS_DEFAULT_FLAGS;
1899 if (adev->nbio.funcs->init_ras_controller_interrupt) {
1900 r = adev->nbio.funcs->init_ras_controller_interrupt(adev);
1905 if (adev->nbio.funcs->init_ras_err_event_athub_interrupt) {
1906 r = adev->nbio.funcs->init_ras_err_event_athub_interrupt(adev);
1911 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1913 if (amdgpu_ras_fs_init(adev))
1916 dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
1917 "hardware ability[%x] ras_mask[%x]\n",
1918 con->hw_supported, con->supported);
1921 amdgpu_ras_set_context(adev, NULL);
1927 /* helper function to handle common stuff in ip late init phase */
1928 int amdgpu_ras_late_init(struct amdgpu_device *adev,
1929 struct ras_common_if *ras_block,
1930 struct ras_fs_if *fs_info,
1931 struct ras_ih_if *ih_info)
1935 /* disable RAS feature per IP block if it is not supported */
1936 if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
1937 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
1941 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
1944 /* request gpu reset. will run again */
1945 amdgpu_ras_request_reset_on_boot(adev,
1948 } else if (adev->in_suspend || adev->in_gpu_reset) {
1949 /* in resume phase, if fail to enable ras,
1950 * clean up all ras fs nodes, and disable ras */
1956 /* in resume phase, no need to create ras fs node */
1957 if (adev->in_suspend || adev->in_gpu_reset)
1961 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
1966 r = amdgpu_ras_sysfs_create(adev, fs_info);
1972 amdgpu_ras_sysfs_remove(adev, ras_block);
1975 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
1977 amdgpu_ras_feature_enable(adev, ras_block, 0);
1981 /* helper function to remove ras fs node and interrupt handler */
1982 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
1983 struct ras_common_if *ras_block,
1984 struct ras_ih_if *ih_info)
1986 if (!ras_block || !ih_info)
1989 amdgpu_ras_sysfs_remove(adev, ras_block);
1991 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
1992 amdgpu_ras_feature_enable(adev, ras_block, 0);
1995 /* do some init work after IP late init as dependence.
1996 * and it runs in resume/gpu reset/booting up cases.
1998 void amdgpu_ras_resume(struct amdgpu_device *adev)
2000 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2001 struct ras_manager *obj, *tmp;
2006 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2007 /* Set up all other IPs which are not implemented. There is a
2008 * tricky thing that IP's actual ras error type should be
2009 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2010 * ERROR_NONE make sense anyway.
2012 amdgpu_ras_enable_all_features(adev, 1);
2014 /* We enable ras on all hw_supported block, but as boot
2015 * parameter might disable some of them and one or more IP has
2016 * not implemented yet. So we disable them on behalf.
2018 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2019 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2020 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2021 /* there should be no any reference. */
2022 WARN_ON(alive_obj(obj));
2027 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
2028 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
2029 /* setup ras obj state as disabled.
2030 * for init_by_vbios case.
2031 * if we want to enable ras, just enable it in a normal way.
2032 * If we want do disable it, need setup ras obj as enabled,
2033 * then issue another TA disable cmd.
2034 * See feature_enable_on_boot
2036 amdgpu_ras_disable_all_features(adev, 1);
2037 amdgpu_ras_reset_gpu(adev);
2041 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2043 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2048 amdgpu_ras_disable_all_features(adev, 0);
2049 /* Make sure all ras objects are disabled. */
2051 amdgpu_ras_disable_all_features(adev, 1);
2054 /* do some fini work before IP fini as dependence */
2055 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2057 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2062 /* Need disable ras on all IPs here before ip [hw/sw]fini */
2063 amdgpu_ras_disable_all_features(adev, 0);
2064 amdgpu_ras_recovery_fini(adev);
2068 int amdgpu_ras_fini(struct amdgpu_device *adev)
2070 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2075 amdgpu_ras_fs_fini(adev);
2076 amdgpu_ras_interrupt_remove_all(adev);
2078 WARN(con->features, "Feature mask is not cleared");
2081 amdgpu_ras_disable_all_features(adev, 1);
2083 amdgpu_ras_set_context(adev, NULL);
2089 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2091 uint32_t hw_supported, supported;
2093 amdgpu_ras_check_supported(adev, &hw_supported, &supported);
2097 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2098 dev_info(adev->dev, "uncorrectable hardware error"
2099 "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2101 amdgpu_ras_reset_gpu(adev);