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 static 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)) {
321 dev_warn(adev->dev, "RAS WARN: input address "
322 "0x%llx is invalid.",
323 data.inject.address);
328 /* umc ce/ue error injection for a bad page is not allowed */
329 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
330 amdgpu_ras_check_bad_page(adev, data.inject.address)) {
331 dev_warn(adev->dev, "RAS WARN: 0x%llx has been marked "
332 "as bad before error injection!\n",
333 data.inject.address);
337 /* data.inject.address is offset instead of absolute gpu address */
338 ret = amdgpu_ras_error_inject(adev, &data.inject);
352 * DOC: AMDGPU RAS debugfs EEPROM table reset interface
354 * Some boards contain an EEPROM which is used to persistently store a list of
355 * bad pages which experiences ECC errors in vram. This interface provides
356 * a way to reset the EEPROM, e.g., after testing error injection.
360 * .. code-block:: bash
362 * echo 1 > ../ras/ras_eeprom_reset
364 * will reset EEPROM table to 0 entries.
367 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, const char __user *buf,
368 size_t size, loff_t *pos)
370 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
373 ret = amdgpu_ras_eeprom_reset_table(&adev->psp.ras.ras->eeprom_control);
375 return ret == 1 ? size : -EIO;
378 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
379 .owner = THIS_MODULE,
381 .write = amdgpu_ras_debugfs_ctrl_write,
382 .llseek = default_llseek
385 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
386 .owner = THIS_MODULE,
388 .write = amdgpu_ras_debugfs_eeprom_write,
389 .llseek = default_llseek
393 * DOC: AMDGPU RAS sysfs Error Count Interface
395 * It allows the user to read the error count for each IP block on the gpu through
396 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
398 * It outputs the multiple lines which report the uncorrected (ue) and corrected
401 * The format of one line is below,
407 * .. code-block:: bash
413 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
417 struct ras_query_if info = {
421 if (!amdgpu_ras_get_error_query_ready(obj->adev))
422 return snprintf(buf, PAGE_SIZE,
423 "Query currently inaccessible\n");
425 if (amdgpu_ras_error_query(obj->adev, &info))
428 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
430 "ce", info.ce_count);
435 #define get_obj(obj) do { (obj)->use++; } while (0)
436 #define alive_obj(obj) ((obj)->use)
438 static inline void put_obj(struct ras_manager *obj)
440 if (obj && --obj->use == 0)
441 list_del(&obj->node);
442 if (obj && obj->use < 0) {
443 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
447 /* make one obj and return it. */
448 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
449 struct ras_common_if *head)
451 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
452 struct ras_manager *obj;
457 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
460 obj = &con->objs[head->block];
461 /* already exist. return obj? */
467 list_add(&obj->node, &con->head);
473 /* return an obj equal to head, or the first when head is NULL */
474 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
475 struct ras_common_if *head)
477 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
478 struct ras_manager *obj;
485 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
488 obj = &con->objs[head->block];
490 if (alive_obj(obj)) {
491 WARN_ON(head->block != obj->head.block);
495 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
497 if (alive_obj(obj)) {
498 WARN_ON(i != obj->head.block);
508 static void amdgpu_ras_parse_status_code(struct amdgpu_device *adev,
509 const char* invoke_type,
510 const char* block_name,
511 enum ta_ras_status ret)
514 case TA_RAS_STATUS__SUCCESS:
516 case TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE:
518 "RAS WARN: %s %s currently unavailable\n",
524 "RAS ERROR: %s %s error failed ret 0x%X\n",
531 /* feature ctl begin */
532 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
533 struct ras_common_if *head)
535 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
537 return con->hw_supported & BIT(head->block);
540 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
541 struct ras_common_if *head)
543 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
545 return con->features & BIT(head->block);
549 * if obj is not created, then create one.
550 * set feature enable flag.
552 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
553 struct ras_common_if *head, int enable)
555 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
556 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
558 /* If hardware does not support ras, then do not create obj.
559 * But if hardware support ras, we can create the obj.
560 * Ras framework checks con->hw_supported to see if it need do
561 * corresponding initialization.
562 * IP checks con->support to see if it need disable ras.
564 if (!amdgpu_ras_is_feature_allowed(adev, head))
566 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
571 obj = amdgpu_ras_create_obj(adev, head);
575 /* In case we create obj somewhere else */
578 con->features |= BIT(head->block);
580 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
581 con->features &= ~BIT(head->block);
589 /* wrapper of psp_ras_enable_features */
590 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
591 struct ras_common_if *head, bool enable)
593 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
594 union ta_ras_cmd_input *info;
600 info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
605 info->disable_features = (struct ta_ras_disable_features_input) {
606 .block_id = amdgpu_ras_block_to_ta(head->block),
607 .error_type = amdgpu_ras_error_to_ta(head->type),
610 info->enable_features = (struct ta_ras_enable_features_input) {
611 .block_id = amdgpu_ras_block_to_ta(head->block),
612 .error_type = amdgpu_ras_error_to_ta(head->type),
616 /* Do not enable if it is not allowed. */
617 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
618 /* Are we alerady in that state we are going to set? */
619 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head))) {
624 if (!amdgpu_ras_intr_triggered()) {
625 ret = psp_ras_enable_features(&adev->psp, info, enable);
627 amdgpu_ras_parse_status_code(adev,
628 enable ? "enable":"disable",
629 ras_block_str(head->block),
630 (enum ta_ras_status)ret);
631 if (ret == TA_RAS_STATUS__RESET_NEEDED)
641 __amdgpu_ras_feature_enable(adev, head, enable);
648 /* Only used in device probe stage and called only once. */
649 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
650 struct ras_common_if *head, bool enable)
652 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
658 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
660 /* There is no harm to issue a ras TA cmd regardless of
661 * the currecnt ras state.
662 * If current state == target state, it will do nothing
663 * But sometimes it requests driver to reset and repost
664 * with error code -EAGAIN.
666 ret = amdgpu_ras_feature_enable(adev, head, 1);
667 /* With old ras TA, we might fail to enable ras.
668 * Log it and just setup the object.
669 * TODO need remove this WA in the future.
671 if (ret == -EINVAL) {
672 ret = __amdgpu_ras_feature_enable(adev, head, 1);
675 "RAS INFO: %s setup object\n",
676 ras_block_str(head->block));
679 /* setup the object then issue a ras TA disable cmd.*/
680 ret = __amdgpu_ras_feature_enable(adev, head, 1);
684 ret = amdgpu_ras_feature_enable(adev, head, 0);
687 ret = amdgpu_ras_feature_enable(adev, head, enable);
692 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
695 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
696 struct ras_manager *obj, *tmp;
698 list_for_each_entry_safe(obj, tmp, &con->head, node) {
700 * aka just release the obj and corresponding flags
703 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
706 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
711 return con->features;
714 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
717 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
718 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
720 const enum amdgpu_ras_error_type default_ras_type =
721 AMDGPU_RAS_ERROR__NONE;
723 for (i = 0; i < ras_block_count; i++) {
724 struct ras_common_if head = {
726 .type = default_ras_type,
727 .sub_block_index = 0,
729 strcpy(head.name, ras_block_str(i));
732 * bypass psp. vbios enable ras for us.
733 * so just create the obj
735 if (__amdgpu_ras_feature_enable(adev, &head, 1))
738 if (amdgpu_ras_feature_enable(adev, &head, 1))
743 return con->features;
745 /* feature ctl end */
747 /* query/inject/cure begin */
748 int amdgpu_ras_error_query(struct amdgpu_device *adev,
749 struct ras_query_if *info)
751 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
752 struct ras_err_data err_data = {0, 0, 0, NULL};
758 switch (info->head.block) {
759 case AMDGPU_RAS_BLOCK__UMC:
760 if (adev->umc.funcs->query_ras_error_count)
761 adev->umc.funcs->query_ras_error_count(adev, &err_data);
762 /* umc query_ras_error_address is also responsible for clearing
765 if (adev->umc.funcs->query_ras_error_address)
766 adev->umc.funcs->query_ras_error_address(adev, &err_data);
768 case AMDGPU_RAS_BLOCK__SDMA:
769 if (adev->sdma.funcs->query_ras_error_count) {
770 for (i = 0; i < adev->sdma.num_instances; i++)
771 adev->sdma.funcs->query_ras_error_count(adev, i,
775 case AMDGPU_RAS_BLOCK__GFX:
776 if (adev->gfx.funcs->query_ras_error_count)
777 adev->gfx.funcs->query_ras_error_count(adev, &err_data);
779 case AMDGPU_RAS_BLOCK__MMHUB:
780 if (adev->mmhub.funcs->query_ras_error_count)
781 adev->mmhub.funcs->query_ras_error_count(adev, &err_data);
783 case AMDGPU_RAS_BLOCK__PCIE_BIF:
784 if (adev->nbio.funcs->query_ras_error_count)
785 adev->nbio.funcs->query_ras_error_count(adev, &err_data);
787 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
788 amdgpu_xgmi_query_ras_error_count(adev, &err_data);
794 obj->err_data.ue_count += err_data.ue_count;
795 obj->err_data.ce_count += err_data.ce_count;
797 info->ue_count = obj->err_data.ue_count;
798 info->ce_count = obj->err_data.ce_count;
800 if (err_data.ce_count) {
801 dev_info(adev->dev, "%ld correctable hardware errors "
802 "detected in %s block, no user "
803 "action is needed.\n",
804 obj->err_data.ce_count,
805 ras_block_str(info->head.block));
807 if (err_data.ue_count) {
808 dev_info(adev->dev, "%ld uncorrectable hardware errors "
809 "detected in %s block\n",
810 obj->err_data.ue_count,
811 ras_block_str(info->head.block));
817 /* Trigger XGMI/WAFL error */
818 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev,
819 struct ta_ras_trigger_error_input *block_info)
823 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
824 dev_warn(adev->dev, "Failed to disallow df cstate");
826 if (amdgpu_dpm_allow_xgmi_power_down(adev, false))
827 dev_warn(adev->dev, "Failed to disallow XGMI power down");
829 ret = psp_ras_trigger_error(&adev->psp, block_info);
831 if (amdgpu_ras_intr_triggered())
834 if (amdgpu_dpm_allow_xgmi_power_down(adev, true))
835 dev_warn(adev->dev, "Failed to allow XGMI power down");
837 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
838 dev_warn(adev->dev, "Failed to allow df cstate");
843 /* wrapper of psp_ras_trigger_error */
844 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
845 struct ras_inject_if *info)
847 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
848 struct ta_ras_trigger_error_input block_info = {
849 .block_id = amdgpu_ras_block_to_ta(info->head.block),
850 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
851 .sub_block_index = info->head.sub_block_index,
852 .address = info->address,
853 .value = info->value,
860 /* Calculate XGMI relative offset */
861 if (adev->gmc.xgmi.num_physical_nodes > 1) {
863 amdgpu_xgmi_get_relative_phy_addr(adev,
867 switch (info->head.block) {
868 case AMDGPU_RAS_BLOCK__GFX:
869 if (adev->gfx.funcs->ras_error_inject)
870 ret = adev->gfx.funcs->ras_error_inject(adev, info);
874 case AMDGPU_RAS_BLOCK__UMC:
875 case AMDGPU_RAS_BLOCK__MMHUB:
876 case AMDGPU_RAS_BLOCK__PCIE_BIF:
877 ret = psp_ras_trigger_error(&adev->psp, &block_info);
879 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
880 ret = amdgpu_ras_error_inject_xgmi(adev, &block_info);
883 dev_info(adev->dev, "%s error injection is not supported yet\n",
884 ras_block_str(info->head.block));
888 amdgpu_ras_parse_status_code(adev,
890 ras_block_str(info->head.block),
891 (enum ta_ras_status)ret);
896 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
897 struct ras_cure_if *info)
899 /* psp fw has no cure interface for now. */
903 /* get the total error counts on all IPs */
904 unsigned long amdgpu_ras_query_error_count(struct amdgpu_device *adev,
907 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
908 struct ras_manager *obj;
909 struct ras_err_data data = {0, 0};
914 list_for_each_entry(obj, &con->head, node) {
915 struct ras_query_if info = {
919 if (amdgpu_ras_error_query(adev, &info))
922 data.ce_count += info.ce_count;
923 data.ue_count += info.ue_count;
926 return is_ce ? data.ce_count : data.ue_count;
928 /* query/inject/cure end */
933 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
934 struct ras_badpage **bps, unsigned int *count);
936 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
939 case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
941 case AMDGPU_RAS_RETIRE_PAGE_PENDING:
943 case AMDGPU_RAS_RETIRE_PAGE_FAULT:
950 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
952 * It allows user to read the bad pages of vram on the gpu through
953 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
955 * It outputs multiple lines, and each line stands for one gpu page.
957 * The format of one line is below,
958 * gpu pfn : gpu page size : flags
960 * gpu pfn and gpu page size are printed in hex format.
961 * flags can be one of below character,
963 * R: reserved, this gpu page is reserved and not able to use.
965 * P: pending for reserve, this gpu page is marked as bad, will be reserved
966 * in next window of page_reserve.
968 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
972 * .. code-block:: bash
974 * 0x00000001 : 0x00001000 : R
975 * 0x00000002 : 0x00001000 : P
979 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
980 struct kobject *kobj, struct bin_attribute *attr,
981 char *buf, loff_t ppos, size_t count)
983 struct amdgpu_ras *con =
984 container_of(attr, struct amdgpu_ras, badpages_attr);
985 struct amdgpu_device *adev = con->adev;
986 const unsigned int element_size =
987 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
988 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
989 unsigned int end = div64_ul(ppos + count - 1, element_size);
991 struct ras_badpage *bps = NULL;
992 unsigned int bps_count = 0;
994 memset(buf, 0, count);
996 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
999 for (; start < end && start < bps_count; start++)
1000 s += scnprintf(&buf[s], element_size + 1,
1001 "0x%08x : 0x%08x : %1s\n",
1004 amdgpu_ras_badpage_flags_str(bps[start].flags));
1011 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1012 struct device_attribute *attr, char *buf)
1014 struct amdgpu_ras *con =
1015 container_of(attr, struct amdgpu_ras, features_attr);
1017 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
1020 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
1022 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1023 struct attribute *attrs[] = {
1024 &con->features_attr.attr,
1027 struct bin_attribute *bin_attrs[] = {
1028 &con->badpages_attr,
1031 struct attribute_group group = {
1034 .bin_attrs = bin_attrs,
1037 con->features_attr = (struct device_attribute) {
1042 .show = amdgpu_ras_sysfs_features_read,
1045 con->badpages_attr = (struct bin_attribute) {
1047 .name = "gpu_vram_bad_pages",
1052 .read = amdgpu_ras_sysfs_badpages_read,
1055 sysfs_attr_init(attrs[0]);
1056 sysfs_bin_attr_init(bin_attrs[0]);
1058 return sysfs_create_group(&adev->dev->kobj, &group);
1061 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1063 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1064 struct attribute *attrs[] = {
1065 &con->features_attr.attr,
1068 struct bin_attribute *bin_attrs[] = {
1069 &con->badpages_attr,
1072 struct attribute_group group = {
1075 .bin_attrs = bin_attrs,
1078 sysfs_remove_group(&adev->dev->kobj, &group);
1083 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1084 struct ras_fs_if *head)
1086 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1088 if (!obj || obj->attr_inuse)
1093 memcpy(obj->fs_data.sysfs_name,
1095 sizeof(obj->fs_data.sysfs_name));
1097 obj->sysfs_attr = (struct device_attribute){
1099 .name = obj->fs_data.sysfs_name,
1102 .show = amdgpu_ras_sysfs_read,
1104 sysfs_attr_init(&obj->sysfs_attr.attr);
1106 if (sysfs_add_file_to_group(&adev->dev->kobj,
1107 &obj->sysfs_attr.attr,
1113 obj->attr_inuse = 1;
1118 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1119 struct ras_common_if *head)
1121 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1123 if (!obj || !obj->attr_inuse)
1126 sysfs_remove_file_from_group(&adev->dev->kobj,
1127 &obj->sysfs_attr.attr,
1129 obj->attr_inuse = 0;
1135 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1137 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1138 struct ras_manager *obj, *tmp;
1140 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1141 amdgpu_ras_sysfs_remove(adev, &obj->head);
1144 amdgpu_ras_sysfs_remove_feature_node(adev);
1151 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1153 * Normally when there is an uncorrectable error, the driver will reset
1154 * the GPU to recover. However, in the event of an unrecoverable error,
1155 * the driver provides an interface to reboot the system automatically
1158 * The following file in debugfs provides that interface:
1159 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1163 * .. code-block:: bash
1165 * echo true > .../ras/auto_reboot
1169 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1171 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1172 struct drm_minor *minor = adev->ddev->primary;
1174 con->dir = debugfs_create_dir("ras", minor->debugfs_root);
1175 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
1176 adev, &amdgpu_ras_debugfs_ctrl_ops);
1177 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, con->dir,
1178 adev, &amdgpu_ras_debugfs_eeprom_ops);
1181 * After one uncorrectable error happens, usually GPU recovery will
1182 * be scheduled. But due to the known problem in GPU recovery failing
1183 * to bring GPU back, below interface provides one direct way to
1184 * user to reboot system automatically in such case within
1185 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1186 * will never be called.
1188 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, con->dir,
1192 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1193 struct ras_fs_if *head)
1195 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1196 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1198 if (!obj || obj->ent)
1203 memcpy(obj->fs_data.debugfs_name,
1205 sizeof(obj->fs_data.debugfs_name));
1207 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
1208 S_IWUGO | S_IRUGO, con->dir, obj,
1209 &amdgpu_ras_debugfs_ops);
1212 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1214 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1215 struct ras_manager *obj;
1216 struct ras_fs_if fs_info;
1219 * it won't be called in resume path, no need to check
1220 * suspend and gpu reset status
1225 amdgpu_ras_debugfs_create_ctrl_node(adev);
1227 list_for_each_entry(obj, &con->head, node) {
1228 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1229 (obj->attr_inuse == 1)) {
1230 sprintf(fs_info.debugfs_name, "%s_err_inject",
1231 ras_block_str(obj->head.block));
1232 fs_info.head = obj->head;
1233 amdgpu_ras_debugfs_create(adev, &fs_info);
1238 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1239 struct ras_common_if *head)
1241 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1243 if (!obj || !obj->ent)
1246 debugfs_remove(obj->ent);
1251 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1253 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1254 struct ras_manager *obj, *tmp;
1256 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1257 amdgpu_ras_debugfs_remove(adev, &obj->head);
1260 debugfs_remove_recursive(con->dir);
1267 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1269 amdgpu_ras_sysfs_create_feature_node(adev);
1274 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1276 amdgpu_ras_debugfs_remove_all(adev);
1277 amdgpu_ras_sysfs_remove_all(adev);
1283 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1285 struct ras_ih_data *data = &obj->ih_data;
1286 struct amdgpu_iv_entry entry;
1288 struct ras_err_data err_data = {0, 0, 0, NULL};
1290 while (data->rptr != data->wptr) {
1292 memcpy(&entry, &data->ring[data->rptr],
1293 data->element_size);
1296 data->rptr = (data->aligned_element_size +
1297 data->rptr) % data->ring_size;
1299 /* Let IP handle its data, maybe we need get the output
1300 * from the callback to udpate the error type/count, etc
1303 ret = data->cb(obj->adev, &err_data, &entry);
1304 /* ue will trigger an interrupt, and in that case
1305 * we need do a reset to recovery the whole system.
1306 * But leave IP do that recovery, here we just dispatch
1309 if (ret == AMDGPU_RAS_SUCCESS) {
1310 /* these counts could be left as 0 if
1311 * some blocks do not count error number
1313 obj->err_data.ue_count += err_data.ue_count;
1314 obj->err_data.ce_count += err_data.ce_count;
1320 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1322 struct ras_ih_data *data =
1323 container_of(work, struct ras_ih_data, ih_work);
1324 struct ras_manager *obj =
1325 container_of(data, struct ras_manager, ih_data);
1327 amdgpu_ras_interrupt_handler(obj);
1330 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1331 struct ras_dispatch_if *info)
1333 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1334 struct ras_ih_data *data = &obj->ih_data;
1339 if (data->inuse == 0)
1342 /* Might be overflow... */
1343 memcpy(&data->ring[data->wptr], info->entry,
1344 data->element_size);
1347 data->wptr = (data->aligned_element_size +
1348 data->wptr) % data->ring_size;
1350 schedule_work(&data->ih_work);
1355 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1356 struct ras_ih_if *info)
1358 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1359 struct ras_ih_data *data;
1364 data = &obj->ih_data;
1365 if (data->inuse == 0)
1368 cancel_work_sync(&data->ih_work);
1371 memset(data, 0, sizeof(*data));
1377 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1378 struct ras_ih_if *info)
1380 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1381 struct ras_ih_data *data;
1384 /* in case we registe the IH before enable ras feature */
1385 obj = amdgpu_ras_create_obj(adev, &info->head);
1391 data = &obj->ih_data;
1392 /* add the callback.etc */
1393 *data = (struct ras_ih_data) {
1396 .element_size = sizeof(struct amdgpu_iv_entry),
1401 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1403 data->aligned_element_size = ALIGN(data->element_size, 8);
1404 /* the ring can store 64 iv entries. */
1405 data->ring_size = 64 * data->aligned_element_size;
1406 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1418 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1420 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1421 struct ras_manager *obj, *tmp;
1423 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1424 struct ras_ih_if info = {
1427 amdgpu_ras_interrupt_remove_handler(adev, &info);
1434 /* traversal all IPs except NBIO to query error counter */
1435 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1437 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1438 struct ras_manager *obj;
1443 list_for_each_entry(obj, &con->head, node) {
1444 struct ras_query_if info = {
1449 * PCIE_BIF IP has one different isr by ras controller
1450 * interrupt, the specific ras counter query will be
1451 * done in that isr. So skip such block from common
1452 * sync flood interrupt isr calling.
1454 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1457 amdgpu_ras_error_query(adev, &info);
1461 /* recovery begin */
1463 /* return 0 on success.
1464 * caller need free bps.
1466 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1467 struct ras_badpage **bps, unsigned int *count)
1469 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1470 struct ras_err_handler_data *data;
1474 if (!con || !con->eh_data || !bps || !count)
1477 mutex_lock(&con->recovery_lock);
1478 data = con->eh_data;
1479 if (!data || data->count == 0) {
1485 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1491 for (; i < data->count; i++) {
1492 (*bps)[i] = (struct ras_badpage){
1493 .bp = data->bps[i].retired_page,
1494 .size = AMDGPU_GPU_PAGE_SIZE,
1495 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1498 if (data->last_reserved <= i)
1499 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1500 else if (data->bps_bo[i] == NULL)
1501 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1504 *count = data->count;
1506 mutex_unlock(&con->recovery_lock);
1510 static void amdgpu_ras_do_recovery(struct work_struct *work)
1512 struct amdgpu_ras *ras =
1513 container_of(work, struct amdgpu_ras, recovery_work);
1514 struct amdgpu_device *remote_adev = NULL;
1515 struct amdgpu_device *adev = ras->adev;
1516 struct list_head device_list, *device_list_handle = NULL;
1517 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev, false);
1519 /* Build list of devices to query RAS related errors */
1520 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
1521 device_list_handle = &hive->device_list;
1523 INIT_LIST_HEAD(&device_list);
1524 list_add_tail(&adev->gmc.xgmi.head, &device_list);
1525 device_list_handle = &device_list;
1528 list_for_each_entry(remote_adev, device_list_handle, gmc.xgmi.head) {
1529 amdgpu_ras_log_on_err_counter(remote_adev);
1532 if (amdgpu_device_should_recover_gpu(ras->adev))
1533 amdgpu_device_gpu_recover(ras->adev, 0);
1534 atomic_set(&ras->in_recovery, 0);
1537 /* alloc/realloc bps array */
1538 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1539 struct ras_err_handler_data *data, int pages)
1541 unsigned int old_space = data->count + data->space_left;
1542 unsigned int new_space = old_space + pages;
1543 unsigned int align_space = ALIGN(new_space, 512);
1544 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1545 struct amdgpu_bo **bps_bo =
1546 kmalloc(align_space * sizeof(*data->bps_bo), GFP_KERNEL);
1548 if (!bps || !bps_bo) {
1555 memcpy(bps, data->bps,
1556 data->count * sizeof(*data->bps));
1560 memcpy(bps_bo, data->bps_bo,
1561 data->count * sizeof(*data->bps_bo));
1562 kfree(data->bps_bo);
1566 data->bps_bo = bps_bo;
1567 data->space_left += align_space - old_space;
1571 /* it deal with vram only. */
1572 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1573 struct eeprom_table_record *bps, int pages)
1575 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1576 struct ras_err_handler_data *data;
1579 if (!con || !con->eh_data || !bps || pages <= 0)
1582 mutex_lock(&con->recovery_lock);
1583 data = con->eh_data;
1587 if (data->space_left <= pages)
1588 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1593 memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
1594 data->count += pages;
1595 data->space_left -= pages;
1598 mutex_unlock(&con->recovery_lock);
1604 * write error record array to eeprom, the function should be
1605 * protected by recovery_lock
1607 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1609 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1610 struct ras_err_handler_data *data;
1611 struct amdgpu_ras_eeprom_control *control;
1614 if (!con || !con->eh_data)
1617 control = &con->eeprom_control;
1618 data = con->eh_data;
1619 save_count = data->count - control->num_recs;
1620 /* only new entries are saved */
1622 if (amdgpu_ras_eeprom_process_recods(control,
1623 &data->bps[control->num_recs],
1626 dev_err(adev->dev, "Failed to save EEPROM table data!");
1634 * read error record array in eeprom and reserve enough space for
1635 * storing new bad pages
1637 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1639 struct amdgpu_ras_eeprom_control *control =
1640 &adev->psp.ras.ras->eeprom_control;
1641 struct eeprom_table_record *bps = NULL;
1644 /* no bad page record, skip eeprom access */
1645 if (!control->num_recs)
1648 bps = kcalloc(control->num_recs, sizeof(*bps), GFP_KERNEL);
1652 if (amdgpu_ras_eeprom_process_recods(control, bps, false,
1653 control->num_recs)) {
1654 dev_err(adev->dev, "Failed to load EEPROM table records!");
1659 ret = amdgpu_ras_add_bad_pages(adev, bps, control->num_recs);
1667 * check if an address belongs to bad page
1669 * Note: this check is only for umc block
1671 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
1674 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1675 struct ras_err_handler_data *data;
1679 if (!con || !con->eh_data)
1682 mutex_lock(&con->recovery_lock);
1683 data = con->eh_data;
1687 addr >>= AMDGPU_GPU_PAGE_SHIFT;
1688 for (i = 0; i < data->count; i++)
1689 if (addr == data->bps[i].retired_page) {
1695 mutex_unlock(&con->recovery_lock);
1699 /* called in gpu recovery/init */
1700 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1702 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1703 struct ras_err_handler_data *data;
1705 struct amdgpu_bo *bo = NULL;
1708 if (!con || !con->eh_data)
1711 mutex_lock(&con->recovery_lock);
1712 data = con->eh_data;
1715 /* reserve vram at driver post stage. */
1716 for (i = data->last_reserved; i < data->count; i++) {
1717 bp = data->bps[i].retired_page;
1719 /* There are two cases of reserve error should be ignored:
1720 * 1) a ras bad page has been allocated (used by someone);
1721 * 2) a ras bad page has been reserved (duplicate error injection
1724 if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
1725 AMDGPU_GPU_PAGE_SIZE,
1726 AMDGPU_GEM_DOMAIN_VRAM,
1728 dev_warn(adev->dev, "RAS WARN: reserve vram for "
1729 "retired page %llx fail\n", bp);
1731 data->bps_bo[i] = bo;
1732 data->last_reserved = i + 1;
1736 /* continue to save bad pages to eeprom even reesrve_vram fails */
1737 ret = amdgpu_ras_save_bad_pages(adev);
1739 mutex_unlock(&con->recovery_lock);
1743 /* called when driver unload */
1744 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1746 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1747 struct ras_err_handler_data *data;
1748 struct amdgpu_bo *bo;
1751 if (!con || !con->eh_data)
1754 mutex_lock(&con->recovery_lock);
1755 data = con->eh_data;
1759 for (i = data->last_reserved - 1; i >= 0; i--) {
1760 bo = data->bps_bo[i];
1762 amdgpu_bo_free_kernel(&bo, NULL, NULL);
1764 data->bps_bo[i] = bo;
1765 data->last_reserved = i;
1768 mutex_unlock(&con->recovery_lock);
1772 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1774 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1775 struct ras_err_handler_data **data;
1779 data = &con->eh_data;
1783 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
1789 mutex_init(&con->recovery_lock);
1790 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1791 atomic_set(&con->in_recovery, 0);
1794 ret = amdgpu_ras_eeprom_init(&con->eeprom_control);
1798 if (con->eeprom_control.num_recs) {
1799 ret = amdgpu_ras_load_bad_pages(adev);
1802 ret = amdgpu_ras_reserve_bad_pages(adev);
1810 amdgpu_ras_release_bad_pages(adev);
1812 kfree((*data)->bps);
1813 kfree((*data)->bps_bo);
1815 con->eh_data = NULL;
1817 dev_warn(adev->dev, "Failed to initialize ras recovery!\n");
1822 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1824 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1825 struct ras_err_handler_data *data = con->eh_data;
1827 /* recovery_init failed to init it, fini is useless */
1831 cancel_work_sync(&con->recovery_work);
1832 amdgpu_ras_release_bad_pages(adev);
1834 mutex_lock(&con->recovery_lock);
1835 con->eh_data = NULL;
1837 kfree(data->bps_bo);
1839 mutex_unlock(&con->recovery_lock);
1845 /* return 0 if ras will reset gpu and repost.*/
1846 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1849 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1854 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1859 * check hardware's ras ability which will be saved in hw_supported.
1860 * if hardware does not support ras, we can skip some ras initializtion and
1861 * forbid some ras operations from IP.
1862 * if software itself, say boot parameter, limit the ras ability. We still
1863 * need allow IP do some limited operations, like disable. In such case,
1864 * we have to initialize ras as normal. but need check if operation is
1865 * allowed or not in each function.
1867 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1868 uint32_t *hw_supported, uint32_t *supported)
1873 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
1874 (adev->asic_type != CHIP_VEGA20 &&
1875 adev->asic_type != CHIP_ARCTURUS))
1878 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
1879 dev_info(adev->dev, "HBM ECC is active.\n");
1880 *hw_supported |= (1 << AMDGPU_RAS_BLOCK__UMC |
1881 1 << AMDGPU_RAS_BLOCK__DF);
1883 dev_info(adev->dev, "HBM ECC is not presented.\n");
1885 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
1886 dev_info(adev->dev, "SRAM ECC is active.\n");
1887 *hw_supported |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
1888 1 << AMDGPU_RAS_BLOCK__DF);
1890 dev_info(adev->dev, "SRAM ECC is not presented.\n");
1892 /* hw_supported needs to be aligned with RAS block mask. */
1893 *hw_supported &= AMDGPU_RAS_BLOCK_MASK;
1895 *supported = amdgpu_ras_enable == 0 ?
1896 0 : *hw_supported & amdgpu_ras_mask;
1899 int amdgpu_ras_init(struct amdgpu_device *adev)
1901 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1907 con = kmalloc(sizeof(struct amdgpu_ras) +
1908 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1909 GFP_KERNEL|__GFP_ZERO);
1913 con->objs = (struct ras_manager *)(con + 1);
1915 amdgpu_ras_set_context(adev, con);
1917 amdgpu_ras_check_supported(adev, &con->hw_supported,
1919 if (!con->hw_supported) {
1925 INIT_LIST_HEAD(&con->head);
1926 /* Might need get this flag from vbios. */
1927 con->flags = RAS_DEFAULT_FLAGS;
1929 if (adev->nbio.funcs->init_ras_controller_interrupt) {
1930 r = adev->nbio.funcs->init_ras_controller_interrupt(adev);
1935 if (adev->nbio.funcs->init_ras_err_event_athub_interrupt) {
1936 r = adev->nbio.funcs->init_ras_err_event_athub_interrupt(adev);
1941 if (amdgpu_ras_fs_init(adev)) {
1946 dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
1947 "hardware ability[%x] ras_mask[%x]\n",
1948 con->hw_supported, con->supported);
1951 amdgpu_ras_set_context(adev, NULL);
1957 /* helper function to handle common stuff in ip late init phase */
1958 int amdgpu_ras_late_init(struct amdgpu_device *adev,
1959 struct ras_common_if *ras_block,
1960 struct ras_fs_if *fs_info,
1961 struct ras_ih_if *ih_info)
1965 /* disable RAS feature per IP block if it is not supported */
1966 if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
1967 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
1971 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
1974 /* request gpu reset. will run again */
1975 amdgpu_ras_request_reset_on_boot(adev,
1978 } else if (adev->in_suspend || adev->in_gpu_reset) {
1979 /* in resume phase, if fail to enable ras,
1980 * clean up all ras fs nodes, and disable ras */
1986 /* in resume phase, no need to create ras fs node */
1987 if (adev->in_suspend || adev->in_gpu_reset)
1991 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
1996 r = amdgpu_ras_sysfs_create(adev, fs_info);
2002 amdgpu_ras_sysfs_remove(adev, ras_block);
2005 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2007 amdgpu_ras_feature_enable(adev, ras_block, 0);
2011 /* helper function to remove ras fs node and interrupt handler */
2012 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
2013 struct ras_common_if *ras_block,
2014 struct ras_ih_if *ih_info)
2016 if (!ras_block || !ih_info)
2019 amdgpu_ras_sysfs_remove(adev, ras_block);
2021 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2022 amdgpu_ras_feature_enable(adev, ras_block, 0);
2025 /* do some init work after IP late init as dependence.
2026 * and it runs in resume/gpu reset/booting up cases.
2028 void amdgpu_ras_resume(struct amdgpu_device *adev)
2030 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2031 struct ras_manager *obj, *tmp;
2036 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2037 /* Set up all other IPs which are not implemented. There is a
2038 * tricky thing that IP's actual ras error type should be
2039 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2040 * ERROR_NONE make sense anyway.
2042 amdgpu_ras_enable_all_features(adev, 1);
2044 /* We enable ras on all hw_supported block, but as boot
2045 * parameter might disable some of them and one or more IP has
2046 * not implemented yet. So we disable them on behalf.
2048 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2049 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2050 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2051 /* there should be no any reference. */
2052 WARN_ON(alive_obj(obj));
2057 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
2058 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
2059 /* setup ras obj state as disabled.
2060 * for init_by_vbios case.
2061 * if we want to enable ras, just enable it in a normal way.
2062 * If we want do disable it, need setup ras obj as enabled,
2063 * then issue another TA disable cmd.
2064 * See feature_enable_on_boot
2066 amdgpu_ras_disable_all_features(adev, 1);
2067 amdgpu_ras_reset_gpu(adev);
2071 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2073 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2078 amdgpu_ras_disable_all_features(adev, 0);
2079 /* Make sure all ras objects are disabled. */
2081 amdgpu_ras_disable_all_features(adev, 1);
2084 /* do some fini work before IP fini as dependence */
2085 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2087 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2092 /* Need disable ras on all IPs here before ip [hw/sw]fini */
2093 amdgpu_ras_disable_all_features(adev, 0);
2094 amdgpu_ras_recovery_fini(adev);
2098 int amdgpu_ras_fini(struct amdgpu_device *adev)
2100 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2105 amdgpu_ras_fs_fini(adev);
2106 amdgpu_ras_interrupt_remove_all(adev);
2108 WARN(con->features, "Feature mask is not cleared");
2111 amdgpu_ras_disable_all_features(adev, 1);
2113 amdgpu_ras_set_context(adev, NULL);
2119 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2121 uint32_t hw_supported, supported;
2123 amdgpu_ras_check_supported(adev, &hw_supported, &supported);
2127 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2128 dev_info(adev->dev, "uncorrectable hardware error"
2129 "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2131 amdgpu_ras_reset_gpu(adev);