2 * Copyright 2018 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/debugfs.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/uaccess.h>
28 #include <linux/reboot.h>
29 #include <linux/syscalls.h>
32 #include "amdgpu_ras.h"
33 #include "amdgpu_atomfirmware.h"
34 #include "amdgpu_xgmi.h"
35 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h"
37 static const char *RAS_FS_NAME = "ras";
39 const char *ras_error_string[] = {
43 "multi_uncorrectable",
47 const char *ras_block_string[] = {
64 #define ras_err_str(i) (ras_error_string[ffs(i)])
65 #define ras_block_str(i) (ras_block_string[i])
67 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
69 /* inject address is 52 bits */
70 #define RAS_UMC_INJECT_ADDR_LIMIT (0x1ULL << 52)
72 /* typical ECC bad page rate(1 bad page per 100MB VRAM) */
73 #define RAS_BAD_PAGE_RATE (100 * 1024 * 1024ULL)
75 enum amdgpu_ras_retire_page_reservation {
76 AMDGPU_RAS_RETIRE_PAGE_RESERVED,
77 AMDGPU_RAS_RETIRE_PAGE_PENDING,
78 AMDGPU_RAS_RETIRE_PAGE_FAULT,
81 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
83 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
86 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
88 if (adev && amdgpu_ras_get_context(adev))
89 amdgpu_ras_get_context(adev)->error_query_ready = ready;
92 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
94 if (adev && amdgpu_ras_get_context(adev))
95 return amdgpu_ras_get_context(adev)->error_query_ready;
100 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
101 size_t size, loff_t *pos)
103 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
104 struct ras_query_if info = {
110 if (amdgpu_ras_error_query(obj->adev, &info))
113 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
115 "ce", info.ce_count);
120 s = min_t(u64, s, size);
123 if (copy_to_user(buf, &val[*pos], s))
131 static const struct file_operations amdgpu_ras_debugfs_ops = {
132 .owner = THIS_MODULE,
133 .read = amdgpu_ras_debugfs_read,
135 .llseek = default_llseek
138 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
142 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
144 if (strcmp(name, ras_block_str(i)) == 0)
150 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
151 const char __user *buf, size_t size,
152 loff_t *pos, struct ras_debug_if *data)
154 ssize_t s = min_t(u64, 64, size);
167 memset(str, 0, sizeof(str));
168 memset(data, 0, sizeof(*data));
170 if (copy_from_user(str, buf, s))
173 if (sscanf(str, "disable %32s", block_name) == 1)
175 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
177 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
179 else if (str[0] && str[1] && str[2] && str[3])
180 /* ascii string, but commands are not matched. */
184 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
187 data->head.block = block_id;
188 /* only ue and ce errors are supported */
189 if (!memcmp("ue", err, 2))
190 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
191 else if (!memcmp("ce", err, 2))
192 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
199 if (sscanf(str, "%*s %*s %*s %u %llu %llu",
200 &sub_block, &address, &value) != 3)
201 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
202 &sub_block, &address, &value) != 3)
204 data->head.sub_block_index = sub_block;
205 data->inject.address = address;
206 data->inject.value = value;
209 if (size < sizeof(*data))
212 if (copy_from_user(data, buf, sizeof(*data)))
220 * DOC: AMDGPU RAS debugfs control interface
222 * It accepts struct ras_debug_if who has two members.
224 * First member: ras_debug_if::head or ras_debug_if::inject.
226 * head is used to indicate which IP block will be under control.
228 * head has four members, they are block, type, sub_block_index, name.
229 * block: which IP will be under control.
230 * type: what kind of error will be enabled/disabled/injected.
231 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
232 * name: the name of IP.
234 * inject has two more members than head, they are address, value.
235 * As their names indicate, inject operation will write the
236 * value to the address.
238 * The second member: struct ras_debug_if::op.
239 * It has three kinds of operations.
241 * - 0: disable RAS on the block. Take ::head as its data.
242 * - 1: enable RAS on the block. Take ::head as its data.
243 * - 2: inject errors on the block. Take ::inject as its data.
245 * How to use the interface?
249 * Copy the struct ras_debug_if in your codes and initialize it.
250 * Write the struct to the control node.
254 * .. code-block:: bash
256 * echo op block [error [sub_block address value]] > .../ras/ras_ctrl
260 * op: disable, enable, inject
261 * disable: only block is needed
262 * enable: block and error are needed
263 * inject: error, address, value are needed
264 * block: umc, sdma, gfx, .........
265 * see ras_block_string[] for details
267 * ue: multi_uncorrectable
268 * ce: single_correctable
270 * sub block index, pass 0 if there is no sub block
272 * here are some examples for bash commands:
274 * .. code-block:: bash
276 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
277 * echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
278 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
280 * How to check the result?
282 * For disable/enable, please check ras features at
283 * /sys/class/drm/card[0/1/2...]/device/ras/features
285 * For inject, please check corresponding err count at
286 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
289 * Operations are only allowed on blocks which are supported.
290 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
291 * to see which blocks support RAS on a particular asic.
294 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
295 size_t size, loff_t *pos)
297 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
298 struct ras_debug_if data;
301 if (!amdgpu_ras_get_error_query_ready(adev)) {
302 dev_warn(adev->dev, "RAS WARN: error injection "
303 "currently inaccessible\n");
307 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
311 if (!amdgpu_ras_is_supported(adev, data.head.block))
316 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
319 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
322 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
323 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
324 dev_warn(adev->dev, "RAS WARN: input address "
325 "0x%llx is invalid.",
326 data.inject.address);
331 /* umc ce/ue error injection for a bad page is not allowed */
332 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
333 amdgpu_ras_check_bad_page(adev, data.inject.address)) {
334 dev_warn(adev->dev, "RAS WARN: 0x%llx has been marked "
335 "as bad before error injection!\n",
336 data.inject.address);
340 /* data.inject.address is offset instead of absolute gpu address */
341 ret = amdgpu_ras_error_inject(adev, &data.inject);
355 * DOC: AMDGPU RAS debugfs EEPROM table reset interface
357 * Some boards contain an EEPROM which is used to persistently store a list of
358 * bad pages which experiences ECC errors in vram. This interface provides
359 * a way to reset the EEPROM, e.g., after testing error injection.
363 * .. code-block:: bash
365 * echo 1 > ../ras/ras_eeprom_reset
367 * will reset EEPROM table to 0 entries.
370 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, const char __user *buf,
371 size_t size, loff_t *pos)
373 struct amdgpu_device *adev =
374 (struct amdgpu_device *)file_inode(f)->i_private;
377 ret = amdgpu_ras_eeprom_reset_table(
378 &(amdgpu_ras_get_context(adev)->eeprom_control));
381 amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS;
388 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
389 .owner = THIS_MODULE,
391 .write = amdgpu_ras_debugfs_ctrl_write,
392 .llseek = default_llseek
395 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
396 .owner = THIS_MODULE,
398 .write = amdgpu_ras_debugfs_eeprom_write,
399 .llseek = default_llseek
403 * DOC: AMDGPU RAS sysfs Error Count Interface
405 * It allows the user to read the error count for each IP block on the gpu through
406 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
408 * It outputs the multiple lines which report the uncorrected (ue) and corrected
411 * The format of one line is below,
417 * .. code-block:: bash
423 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
424 struct device_attribute *attr, char *buf)
426 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
427 struct ras_query_if info = {
431 if (!amdgpu_ras_get_error_query_ready(obj->adev))
432 return snprintf(buf, PAGE_SIZE,
433 "Query currently inaccessible\n");
435 if (amdgpu_ras_error_query(obj->adev, &info))
438 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
440 "ce", info.ce_count);
445 #define get_obj(obj) do { (obj)->use++; } while (0)
446 #define alive_obj(obj) ((obj)->use)
448 static inline void put_obj(struct ras_manager *obj)
450 if (obj && --obj->use == 0)
451 list_del(&obj->node);
452 if (obj && obj->use < 0) {
453 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
457 /* make one obj and return it. */
458 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
459 struct ras_common_if *head)
461 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
462 struct ras_manager *obj;
467 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
470 obj = &con->objs[head->block];
471 /* already exist. return obj? */
477 list_add(&obj->node, &con->head);
483 /* return an obj equal to head, or the first when head is NULL */
484 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
485 struct ras_common_if *head)
487 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
488 struct ras_manager *obj;
495 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
498 obj = &con->objs[head->block];
500 if (alive_obj(obj)) {
501 WARN_ON(head->block != obj->head.block);
505 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
507 if (alive_obj(obj)) {
508 WARN_ON(i != obj->head.block);
518 static void amdgpu_ras_parse_status_code(struct amdgpu_device *adev,
519 const char* invoke_type,
520 const char* block_name,
521 enum ta_ras_status ret)
524 case TA_RAS_STATUS__SUCCESS:
526 case TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE:
528 "RAS WARN: %s %s currently unavailable\n",
534 "RAS ERROR: %s %s error failed ret 0x%X\n",
541 /* feature ctl begin */
542 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
543 struct ras_common_if *head)
545 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
547 return con->hw_supported & BIT(head->block);
550 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
551 struct ras_common_if *head)
553 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
555 return con->features & BIT(head->block);
559 * if obj is not created, then create one.
560 * set feature enable flag.
562 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
563 struct ras_common_if *head, int enable)
565 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
566 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
568 /* If hardware does not support ras, then do not create obj.
569 * But if hardware support ras, we can create the obj.
570 * Ras framework checks con->hw_supported to see if it need do
571 * corresponding initialization.
572 * IP checks con->support to see if it need disable ras.
574 if (!amdgpu_ras_is_feature_allowed(adev, head))
576 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
581 obj = amdgpu_ras_create_obj(adev, head);
585 /* In case we create obj somewhere else */
588 con->features |= BIT(head->block);
590 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
591 con->features &= ~BIT(head->block);
599 /* wrapper of psp_ras_enable_features */
600 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
601 struct ras_common_if *head, bool enable)
603 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
604 union ta_ras_cmd_input *info;
610 info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
615 info->disable_features = (struct ta_ras_disable_features_input) {
616 .block_id = amdgpu_ras_block_to_ta(head->block),
617 .error_type = amdgpu_ras_error_to_ta(head->type),
620 info->enable_features = (struct ta_ras_enable_features_input) {
621 .block_id = amdgpu_ras_block_to_ta(head->block),
622 .error_type = amdgpu_ras_error_to_ta(head->type),
626 /* Do not enable if it is not allowed. */
627 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
628 /* Are we alerady in that state we are going to set? */
629 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head))) {
634 if (!amdgpu_ras_intr_triggered()) {
635 ret = psp_ras_enable_features(&adev->psp, info, enable);
637 amdgpu_ras_parse_status_code(adev,
638 enable ? "enable":"disable",
639 ras_block_str(head->block),
640 (enum ta_ras_status)ret);
641 if (ret == TA_RAS_STATUS__RESET_NEEDED)
651 __amdgpu_ras_feature_enable(adev, head, enable);
658 /* Only used in device probe stage and called only once. */
659 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
660 struct ras_common_if *head, bool enable)
662 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
668 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
670 /* There is no harm to issue a ras TA cmd regardless of
671 * the currecnt ras state.
672 * If current state == target state, it will do nothing
673 * But sometimes it requests driver to reset and repost
674 * with error code -EAGAIN.
676 ret = amdgpu_ras_feature_enable(adev, head, 1);
677 /* With old ras TA, we might fail to enable ras.
678 * Log it and just setup the object.
679 * TODO need remove this WA in the future.
681 if (ret == -EINVAL) {
682 ret = __amdgpu_ras_feature_enable(adev, head, 1);
685 "RAS INFO: %s setup object\n",
686 ras_block_str(head->block));
689 /* setup the object then issue a ras TA disable cmd.*/
690 ret = __amdgpu_ras_feature_enable(adev, head, 1);
694 ret = amdgpu_ras_feature_enable(adev, head, 0);
697 ret = amdgpu_ras_feature_enable(adev, head, enable);
702 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
705 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
706 struct ras_manager *obj, *tmp;
708 list_for_each_entry_safe(obj, tmp, &con->head, node) {
710 * aka just release the obj and corresponding flags
713 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
716 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
721 return con->features;
724 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
727 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
728 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
730 const enum amdgpu_ras_error_type default_ras_type =
731 AMDGPU_RAS_ERROR__NONE;
733 for (i = 0; i < ras_block_count; i++) {
734 struct ras_common_if head = {
736 .type = default_ras_type,
737 .sub_block_index = 0,
739 strcpy(head.name, ras_block_str(i));
742 * bypass psp. vbios enable ras for us.
743 * so just create the obj
745 if (__amdgpu_ras_feature_enable(adev, &head, 1))
748 if (amdgpu_ras_feature_enable(adev, &head, 1))
753 return con->features;
755 /* feature ctl end */
757 /* query/inject/cure begin */
758 int amdgpu_ras_error_query(struct amdgpu_device *adev,
759 struct ras_query_if *info)
761 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
762 struct ras_err_data err_data = {0, 0, 0, NULL};
768 switch (info->head.block) {
769 case AMDGPU_RAS_BLOCK__UMC:
770 if (adev->umc.funcs->query_ras_error_count)
771 adev->umc.funcs->query_ras_error_count(adev, &err_data);
772 /* umc query_ras_error_address is also responsible for clearing
775 if (adev->umc.funcs->query_ras_error_address)
776 adev->umc.funcs->query_ras_error_address(adev, &err_data);
778 case AMDGPU_RAS_BLOCK__SDMA:
779 if (adev->sdma.funcs->query_ras_error_count) {
780 for (i = 0; i < adev->sdma.num_instances; i++)
781 adev->sdma.funcs->query_ras_error_count(adev, i,
785 case AMDGPU_RAS_BLOCK__GFX:
786 if (adev->gfx.funcs->query_ras_error_count)
787 adev->gfx.funcs->query_ras_error_count(adev, &err_data);
789 case AMDGPU_RAS_BLOCK__MMHUB:
790 if (adev->mmhub.funcs->query_ras_error_count)
791 adev->mmhub.funcs->query_ras_error_count(adev, &err_data);
793 case AMDGPU_RAS_BLOCK__PCIE_BIF:
794 if (adev->nbio.funcs->query_ras_error_count)
795 adev->nbio.funcs->query_ras_error_count(adev, &err_data);
797 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
798 amdgpu_xgmi_query_ras_error_count(adev, &err_data);
804 obj->err_data.ue_count += err_data.ue_count;
805 obj->err_data.ce_count += err_data.ce_count;
807 info->ue_count = obj->err_data.ue_count;
808 info->ce_count = obj->err_data.ce_count;
810 if (err_data.ce_count) {
811 dev_info(adev->dev, "%ld correctable hardware errors "
812 "detected in %s block, no user "
813 "action is needed.\n",
814 obj->err_data.ce_count,
815 ras_block_str(info->head.block));
817 if (err_data.ue_count) {
818 dev_info(adev->dev, "%ld uncorrectable hardware errors "
819 "detected in %s block\n",
820 obj->err_data.ue_count,
821 ras_block_str(info->head.block));
827 /* Trigger XGMI/WAFL error */
828 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev,
829 struct ta_ras_trigger_error_input *block_info)
833 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
834 dev_warn(adev->dev, "Failed to disallow df cstate");
836 if (amdgpu_dpm_allow_xgmi_power_down(adev, false))
837 dev_warn(adev->dev, "Failed to disallow XGMI power down");
839 ret = psp_ras_trigger_error(&adev->psp, block_info);
841 if (amdgpu_ras_intr_triggered())
844 if (amdgpu_dpm_allow_xgmi_power_down(adev, true))
845 dev_warn(adev->dev, "Failed to allow XGMI power down");
847 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
848 dev_warn(adev->dev, "Failed to allow df cstate");
853 /* wrapper of psp_ras_trigger_error */
854 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
855 struct ras_inject_if *info)
857 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
858 struct ta_ras_trigger_error_input block_info = {
859 .block_id = amdgpu_ras_block_to_ta(info->head.block),
860 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
861 .sub_block_index = info->head.sub_block_index,
862 .address = info->address,
863 .value = info->value,
870 /* Calculate XGMI relative offset */
871 if (adev->gmc.xgmi.num_physical_nodes > 1) {
873 amdgpu_xgmi_get_relative_phy_addr(adev,
877 switch (info->head.block) {
878 case AMDGPU_RAS_BLOCK__GFX:
879 if (adev->gfx.funcs->ras_error_inject)
880 ret = adev->gfx.funcs->ras_error_inject(adev, info);
884 case AMDGPU_RAS_BLOCK__UMC:
885 case AMDGPU_RAS_BLOCK__MMHUB:
886 case AMDGPU_RAS_BLOCK__PCIE_BIF:
887 ret = psp_ras_trigger_error(&adev->psp, &block_info);
889 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
890 ret = amdgpu_ras_error_inject_xgmi(adev, &block_info);
893 dev_info(adev->dev, "%s error injection is not supported yet\n",
894 ras_block_str(info->head.block));
898 amdgpu_ras_parse_status_code(adev,
900 ras_block_str(info->head.block),
901 (enum ta_ras_status)ret);
906 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
907 struct ras_cure_if *info)
909 /* psp fw has no cure interface for now. */
913 /* get the total error counts on all IPs */
914 unsigned long amdgpu_ras_query_error_count(struct amdgpu_device *adev,
917 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
918 struct ras_manager *obj;
919 struct ras_err_data data = {0, 0};
924 list_for_each_entry(obj, &con->head, node) {
925 struct ras_query_if info = {
929 if (amdgpu_ras_error_query(adev, &info))
932 data.ce_count += info.ce_count;
933 data.ue_count += info.ue_count;
936 return is_ce ? data.ce_count : data.ue_count;
938 /* query/inject/cure end */
943 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
944 struct ras_badpage **bps, unsigned int *count);
946 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
949 case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
951 case AMDGPU_RAS_RETIRE_PAGE_PENDING:
953 case AMDGPU_RAS_RETIRE_PAGE_FAULT:
960 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
962 * It allows user to read the bad pages of vram on the gpu through
963 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
965 * It outputs multiple lines, and each line stands for one gpu page.
967 * The format of one line is below,
968 * gpu pfn : gpu page size : flags
970 * gpu pfn and gpu page size are printed in hex format.
971 * flags can be one of below character,
973 * R: reserved, this gpu page is reserved and not able to use.
975 * P: pending for reserve, this gpu page is marked as bad, will be reserved
976 * in next window of page_reserve.
978 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
982 * .. code-block:: bash
984 * 0x00000001 : 0x00001000 : R
985 * 0x00000002 : 0x00001000 : P
989 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
990 struct kobject *kobj, struct bin_attribute *attr,
991 char *buf, loff_t ppos, size_t count)
993 struct amdgpu_ras *con =
994 container_of(attr, struct amdgpu_ras, badpages_attr);
995 struct amdgpu_device *adev = con->adev;
996 const unsigned int element_size =
997 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
998 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
999 unsigned int end = div64_ul(ppos + count - 1, element_size);
1001 struct ras_badpage *bps = NULL;
1002 unsigned int bps_count = 0;
1004 memset(buf, 0, count);
1006 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1009 for (; start < end && start < bps_count; start++)
1010 s += scnprintf(&buf[s], element_size + 1,
1011 "0x%08x : 0x%08x : %1s\n",
1014 amdgpu_ras_badpage_flags_str(bps[start].flags));
1021 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1022 struct device_attribute *attr, char *buf)
1024 struct amdgpu_ras *con =
1025 container_of(attr, struct amdgpu_ras, features_attr);
1027 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
1030 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1032 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1034 sysfs_remove_file_from_group(&adev->dev->kobj,
1035 &con->badpages_attr.attr,
1039 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1041 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1042 struct attribute *attrs[] = {
1043 &con->features_attr.attr,
1046 struct attribute_group group = {
1047 .name = RAS_FS_NAME,
1051 sysfs_remove_group(&adev->dev->kobj, &group);
1056 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1057 struct ras_fs_if *head)
1059 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1061 if (!obj || obj->attr_inuse)
1066 memcpy(obj->fs_data.sysfs_name,
1068 sizeof(obj->fs_data.sysfs_name));
1070 obj->sysfs_attr = (struct device_attribute){
1072 .name = obj->fs_data.sysfs_name,
1075 .show = amdgpu_ras_sysfs_read,
1077 sysfs_attr_init(&obj->sysfs_attr.attr);
1079 if (sysfs_add_file_to_group(&adev->dev->kobj,
1080 &obj->sysfs_attr.attr,
1086 obj->attr_inuse = 1;
1091 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1092 struct ras_common_if *head)
1094 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1096 if (!obj || !obj->attr_inuse)
1099 sysfs_remove_file_from_group(&adev->dev->kobj,
1100 &obj->sysfs_attr.attr,
1102 obj->attr_inuse = 0;
1108 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1110 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1111 struct ras_manager *obj, *tmp;
1113 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1114 amdgpu_ras_sysfs_remove(adev, &obj->head);
1117 if (amdgpu_bad_page_threshold != 0)
1118 amdgpu_ras_sysfs_remove_bad_page_node(adev);
1120 amdgpu_ras_sysfs_remove_feature_node(adev);
1127 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1129 * Normally when there is an uncorrectable error, the driver will reset
1130 * the GPU to recover. However, in the event of an unrecoverable error,
1131 * the driver provides an interface to reboot the system automatically
1134 * The following file in debugfs provides that interface:
1135 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1139 * .. code-block:: bash
1141 * echo true > .../ras/auto_reboot
1145 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1147 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1148 struct drm_minor *minor = adev_to_drm(adev)->primary;
1150 con->dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1151 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
1152 adev, &amdgpu_ras_debugfs_ctrl_ops);
1153 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, con->dir,
1154 adev, &amdgpu_ras_debugfs_eeprom_ops);
1157 * After one uncorrectable error happens, usually GPU recovery will
1158 * be scheduled. But due to the known problem in GPU recovery failing
1159 * to bring GPU back, below interface provides one direct way to
1160 * user to reboot system automatically in such case within
1161 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1162 * will never be called.
1164 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, con->dir,
1168 * User could set this not to clean up hardware's error count register
1169 * of RAS IPs during ras recovery.
1171 debugfs_create_bool("disable_ras_err_cnt_harvest", 0644,
1172 con->dir, &con->disable_ras_err_cnt_harvest);
1175 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1176 struct ras_fs_if *head)
1178 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1179 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1181 if (!obj || obj->ent)
1186 memcpy(obj->fs_data.debugfs_name,
1188 sizeof(obj->fs_data.debugfs_name));
1190 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
1191 S_IWUGO | S_IRUGO, con->dir, obj,
1192 &amdgpu_ras_debugfs_ops);
1195 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1197 #if defined(CONFIG_DEBUG_FS)
1198 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1199 struct ras_manager *obj;
1200 struct ras_fs_if fs_info;
1203 * it won't be called in resume path, no need to check
1204 * suspend and gpu reset status
1209 amdgpu_ras_debugfs_create_ctrl_node(adev);
1211 list_for_each_entry(obj, &con->head, node) {
1212 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1213 (obj->attr_inuse == 1)) {
1214 sprintf(fs_info.debugfs_name, "%s_err_inject",
1215 ras_block_str(obj->head.block));
1216 fs_info.head = obj->head;
1217 amdgpu_ras_debugfs_create(adev, &fs_info);
1223 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1224 struct ras_common_if *head)
1226 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1228 if (!obj || !obj->ent)
1235 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1237 #if defined(CONFIG_DEBUG_FS)
1238 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1239 struct ras_manager *obj, *tmp;
1241 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1242 amdgpu_ras_debugfs_remove(adev, &obj->head);
1251 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1252 amdgpu_ras_sysfs_badpages_read, NULL, 0);
1253 static DEVICE_ATTR(features, S_IRUGO,
1254 amdgpu_ras_sysfs_features_read, NULL);
1255 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1257 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1258 struct attribute_group group = {
1259 .name = RAS_FS_NAME,
1261 struct attribute *attrs[] = {
1262 &con->features_attr.attr,
1265 struct bin_attribute *bin_attrs[] = {
1271 /* add features entry */
1272 con->features_attr = dev_attr_features;
1273 group.attrs = attrs;
1274 sysfs_attr_init(attrs[0]);
1276 if (amdgpu_bad_page_threshold != 0) {
1277 /* add bad_page_features entry */
1278 bin_attr_gpu_vram_bad_pages.private = NULL;
1279 con->badpages_attr = bin_attr_gpu_vram_bad_pages;
1280 bin_attrs[0] = &con->badpages_attr;
1281 group.bin_attrs = bin_attrs;
1282 sysfs_bin_attr_init(bin_attrs[0]);
1285 r = sysfs_create_group(&adev->dev->kobj, &group);
1287 dev_err(adev->dev, "Failed to create RAS sysfs group!");
1292 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1294 amdgpu_ras_debugfs_remove_all(adev);
1295 amdgpu_ras_sysfs_remove_all(adev);
1301 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1303 struct ras_ih_data *data = &obj->ih_data;
1304 struct amdgpu_iv_entry entry;
1306 struct ras_err_data err_data = {0, 0, 0, NULL};
1308 while (data->rptr != data->wptr) {
1310 memcpy(&entry, &data->ring[data->rptr],
1311 data->element_size);
1314 data->rptr = (data->aligned_element_size +
1315 data->rptr) % data->ring_size;
1317 /* Let IP handle its data, maybe we need get the output
1318 * from the callback to udpate the error type/count, etc
1321 ret = data->cb(obj->adev, &err_data, &entry);
1322 /* ue will trigger an interrupt, and in that case
1323 * we need do a reset to recovery the whole system.
1324 * But leave IP do that recovery, here we just dispatch
1327 if (ret == AMDGPU_RAS_SUCCESS) {
1328 /* these counts could be left as 0 if
1329 * some blocks do not count error number
1331 obj->err_data.ue_count += err_data.ue_count;
1332 obj->err_data.ce_count += err_data.ce_count;
1338 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1340 struct ras_ih_data *data =
1341 container_of(work, struct ras_ih_data, ih_work);
1342 struct ras_manager *obj =
1343 container_of(data, struct ras_manager, ih_data);
1345 amdgpu_ras_interrupt_handler(obj);
1348 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1349 struct ras_dispatch_if *info)
1351 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1352 struct ras_ih_data *data = &obj->ih_data;
1357 if (data->inuse == 0)
1360 /* Might be overflow... */
1361 memcpy(&data->ring[data->wptr], info->entry,
1362 data->element_size);
1365 data->wptr = (data->aligned_element_size +
1366 data->wptr) % data->ring_size;
1368 schedule_work(&data->ih_work);
1373 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1374 struct ras_ih_if *info)
1376 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1377 struct ras_ih_data *data;
1382 data = &obj->ih_data;
1383 if (data->inuse == 0)
1386 cancel_work_sync(&data->ih_work);
1389 memset(data, 0, sizeof(*data));
1395 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1396 struct ras_ih_if *info)
1398 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1399 struct ras_ih_data *data;
1402 /* in case we registe the IH before enable ras feature */
1403 obj = amdgpu_ras_create_obj(adev, &info->head);
1409 data = &obj->ih_data;
1410 /* add the callback.etc */
1411 *data = (struct ras_ih_data) {
1414 .element_size = sizeof(struct amdgpu_iv_entry),
1419 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1421 data->aligned_element_size = ALIGN(data->element_size, 8);
1422 /* the ring can store 64 iv entries. */
1423 data->ring_size = 64 * data->aligned_element_size;
1424 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1436 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1438 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1439 struct ras_manager *obj, *tmp;
1441 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1442 struct ras_ih_if info = {
1445 amdgpu_ras_interrupt_remove_handler(adev, &info);
1452 /* traversal all IPs except NBIO to query error counter */
1453 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1455 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1456 struct ras_manager *obj;
1461 list_for_each_entry(obj, &con->head, node) {
1462 struct ras_query_if info = {
1467 * PCIE_BIF IP has one different isr by ras controller
1468 * interrupt, the specific ras counter query will be
1469 * done in that isr. So skip such block from common
1470 * sync flood interrupt isr calling.
1472 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1475 amdgpu_ras_error_query(adev, &info);
1479 /* Parse RdRspStatus and WrRspStatus */
1480 void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
1481 struct ras_query_if *info)
1484 * Only two block need to query read/write
1485 * RspStatus at current state
1487 switch (info->head.block) {
1488 case AMDGPU_RAS_BLOCK__GFX:
1489 if (adev->gfx.funcs->query_ras_error_status)
1490 adev->gfx.funcs->query_ras_error_status(adev);
1492 case AMDGPU_RAS_BLOCK__MMHUB:
1493 if (adev->mmhub.funcs->query_ras_error_status)
1494 adev->mmhub.funcs->query_ras_error_status(adev);
1501 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
1503 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1504 struct ras_manager *obj;
1509 list_for_each_entry(obj, &con->head, node) {
1510 struct ras_query_if info = {
1514 amdgpu_ras_error_status_query(adev, &info);
1518 /* recovery begin */
1520 /* return 0 on success.
1521 * caller need free bps.
1523 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1524 struct ras_badpage **bps, unsigned int *count)
1526 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1527 struct ras_err_handler_data *data;
1531 if (!con || !con->eh_data || !bps || !count)
1534 mutex_lock(&con->recovery_lock);
1535 data = con->eh_data;
1536 if (!data || data->count == 0) {
1542 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1548 for (; i < data->count; i++) {
1549 (*bps)[i] = (struct ras_badpage){
1550 .bp = data->bps[i].retired_page,
1551 .size = AMDGPU_GPU_PAGE_SIZE,
1552 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1555 if (data->last_reserved <= i)
1556 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1557 else if (data->bps_bo[i] == NULL)
1558 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1561 *count = data->count;
1563 mutex_unlock(&con->recovery_lock);
1567 static void amdgpu_ras_do_recovery(struct work_struct *work)
1569 struct amdgpu_ras *ras =
1570 container_of(work, struct amdgpu_ras, recovery_work);
1571 struct amdgpu_device *remote_adev = NULL;
1572 struct amdgpu_device *adev = ras->adev;
1573 struct list_head device_list, *device_list_handle = NULL;
1575 if (!ras->disable_ras_err_cnt_harvest) {
1576 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
1578 /* Build list of devices to query RAS related errors */
1579 if (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
1580 device_list_handle = &hive->device_list;
1582 INIT_LIST_HEAD(&device_list);
1583 list_add_tail(&adev->gmc.xgmi.head, &device_list);
1584 device_list_handle = &device_list;
1587 list_for_each_entry(remote_adev,
1588 device_list_handle, gmc.xgmi.head) {
1589 amdgpu_ras_query_err_status(remote_adev);
1590 amdgpu_ras_log_on_err_counter(remote_adev);
1593 amdgpu_put_xgmi_hive(hive);
1596 if (amdgpu_device_should_recover_gpu(ras->adev))
1597 amdgpu_device_gpu_recover(ras->adev, NULL);
1598 atomic_set(&ras->in_recovery, 0);
1601 /* alloc/realloc bps array */
1602 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1603 struct ras_err_handler_data *data, int pages)
1605 unsigned int old_space = data->count + data->space_left;
1606 unsigned int new_space = old_space + pages;
1607 unsigned int align_space = ALIGN(new_space, 512);
1608 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1609 struct amdgpu_bo **bps_bo =
1610 kmalloc(align_space * sizeof(*data->bps_bo), GFP_KERNEL);
1612 if (!bps || !bps_bo) {
1619 memcpy(bps, data->bps,
1620 data->count * sizeof(*data->bps));
1624 memcpy(bps_bo, data->bps_bo,
1625 data->count * sizeof(*data->bps_bo));
1626 kfree(data->bps_bo);
1630 data->bps_bo = bps_bo;
1631 data->space_left += align_space - old_space;
1635 /* it deal with vram only. */
1636 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1637 struct eeprom_table_record *bps, int pages)
1639 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1640 struct ras_err_handler_data *data;
1643 if (!con || !con->eh_data || !bps || pages <= 0)
1646 mutex_lock(&con->recovery_lock);
1647 data = con->eh_data;
1651 if (data->space_left <= pages)
1652 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1657 memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
1658 data->count += pages;
1659 data->space_left -= pages;
1662 mutex_unlock(&con->recovery_lock);
1668 * write error record array to eeprom, the function should be
1669 * protected by recovery_lock
1671 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1673 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1674 struct ras_err_handler_data *data;
1675 struct amdgpu_ras_eeprom_control *control;
1678 if (!con || !con->eh_data)
1681 control = &con->eeprom_control;
1682 data = con->eh_data;
1683 save_count = data->count - control->num_recs;
1684 /* only new entries are saved */
1685 if (save_count > 0) {
1686 if (amdgpu_ras_eeprom_process_recods(control,
1687 &data->bps[control->num_recs],
1690 dev_err(adev->dev, "Failed to save EEPROM table data!");
1694 dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
1701 * read error record array in eeprom and reserve enough space for
1702 * storing new bad pages
1704 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1706 struct amdgpu_ras_eeprom_control *control =
1707 &adev->psp.ras.ras->eeprom_control;
1708 struct eeprom_table_record *bps = NULL;
1711 /* no bad page record, skip eeprom access */
1712 if (!control->num_recs || (amdgpu_bad_page_threshold == 0))
1715 bps = kcalloc(control->num_recs, sizeof(*bps), GFP_KERNEL);
1719 if (amdgpu_ras_eeprom_process_recods(control, bps, false,
1720 control->num_recs)) {
1721 dev_err(adev->dev, "Failed to load EEPROM table records!");
1726 ret = amdgpu_ras_add_bad_pages(adev, bps, control->num_recs);
1734 * check if an address belongs to bad page
1736 * Note: this check is only for umc block
1738 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
1741 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1742 struct ras_err_handler_data *data;
1746 if (!con || !con->eh_data)
1749 mutex_lock(&con->recovery_lock);
1750 data = con->eh_data;
1754 addr >>= AMDGPU_GPU_PAGE_SHIFT;
1755 for (i = 0; i < data->count; i++)
1756 if (addr == data->bps[i].retired_page) {
1762 mutex_unlock(&con->recovery_lock);
1766 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
1767 uint32_t max_length)
1769 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1770 int tmp_threshold = amdgpu_bad_page_threshold;
1774 * Justification of value bad_page_cnt_threshold in ras structure
1776 * Generally, -1 <= amdgpu_bad_page_threshold <= max record length
1777 * in eeprom, and introduce two scenarios accordingly.
1779 * Bad page retirement enablement:
1780 * - If amdgpu_bad_page_threshold = -1,
1781 * bad_page_cnt_threshold = typical value by formula.
1783 * - When the value from user is 0 < amdgpu_bad_page_threshold <
1784 * max record length in eeprom, use it directly.
1786 * Bad page retirement disablement:
1787 * - If amdgpu_bad_page_threshold = 0, bad page retirement
1788 * functionality is disabled, and bad_page_cnt_threshold will
1792 if (tmp_threshold < -1)
1794 else if (tmp_threshold > max_length)
1795 tmp_threshold = max_length;
1797 if (tmp_threshold == -1) {
1798 val = adev->gmc.mc_vram_size;
1799 do_div(val, RAS_BAD_PAGE_RATE);
1800 con->bad_page_cnt_threshold = min(lower_32_bits(val),
1803 con->bad_page_cnt_threshold = tmp_threshold;
1807 /* called in gpu recovery/init */
1808 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1810 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1811 struct ras_err_handler_data *data;
1813 struct amdgpu_bo *bo = NULL;
1816 /* Not reserve bad page when amdgpu_bad_page_threshold == 0. */
1817 if (!con || !con->eh_data || (amdgpu_bad_page_threshold == 0))
1820 mutex_lock(&con->recovery_lock);
1821 data = con->eh_data;
1824 /* reserve vram at driver post stage. */
1825 for (i = data->last_reserved; i < data->count; i++) {
1826 bp = data->bps[i].retired_page;
1828 /* There are two cases of reserve error should be ignored:
1829 * 1) a ras bad page has been allocated (used by someone);
1830 * 2) a ras bad page has been reserved (duplicate error injection
1833 if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
1834 AMDGPU_GPU_PAGE_SIZE,
1835 AMDGPU_GEM_DOMAIN_VRAM,
1837 dev_warn(adev->dev, "RAS WARN: reserve vram for "
1838 "retired page %llx fail\n", bp);
1840 data->bps_bo[i] = bo;
1841 data->last_reserved = i + 1;
1845 /* continue to save bad pages to eeprom even reesrve_vram fails */
1846 ret = amdgpu_ras_save_bad_pages(adev);
1848 mutex_unlock(&con->recovery_lock);
1852 /* called when driver unload */
1853 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1855 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1856 struct ras_err_handler_data *data;
1857 struct amdgpu_bo *bo;
1860 if (!con || !con->eh_data)
1863 mutex_lock(&con->recovery_lock);
1864 data = con->eh_data;
1868 for (i = data->last_reserved - 1; i >= 0; i--) {
1869 bo = data->bps_bo[i];
1871 amdgpu_bo_free_kernel(&bo, NULL, NULL);
1873 data->bps_bo[i] = bo;
1874 data->last_reserved = i;
1877 mutex_unlock(&con->recovery_lock);
1881 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1883 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1884 struct ras_err_handler_data **data;
1885 uint32_t max_eeprom_records_len = 0;
1886 bool exc_err_limit = false;
1890 data = &con->eh_data;
1894 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
1900 mutex_init(&con->recovery_lock);
1901 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1902 atomic_set(&con->in_recovery, 0);
1905 max_eeprom_records_len = amdgpu_ras_eeprom_get_record_max_length();
1906 amdgpu_ras_validate_threshold(adev, max_eeprom_records_len);
1908 ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit);
1910 * This calling fails when exc_err_limit is true or
1913 if (exc_err_limit || ret)
1916 if (con->eeprom_control.num_recs) {
1917 ret = amdgpu_ras_load_bad_pages(adev);
1920 ret = amdgpu_ras_reserve_bad_pages(adev);
1928 amdgpu_ras_release_bad_pages(adev);
1930 kfree((*data)->bps);
1931 kfree((*data)->bps_bo);
1933 con->eh_data = NULL;
1935 dev_warn(adev->dev, "Failed to initialize ras recovery!\n");
1938 * Except error threshold exceeding case, other failure cases in this
1939 * function would not fail amdgpu driver init.
1949 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1951 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1952 struct ras_err_handler_data *data = con->eh_data;
1954 /* recovery_init failed to init it, fini is useless */
1958 cancel_work_sync(&con->recovery_work);
1959 amdgpu_ras_release_bad_pages(adev);
1961 mutex_lock(&con->recovery_lock);
1962 con->eh_data = NULL;
1964 kfree(data->bps_bo);
1966 mutex_unlock(&con->recovery_lock);
1972 /* return 0 if ras will reset gpu and repost.*/
1973 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1976 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1981 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1985 static int amdgpu_ras_check_asic_type(struct amdgpu_device *adev)
1987 if (adev->asic_type != CHIP_VEGA10 &&
1988 adev->asic_type != CHIP_VEGA20 &&
1989 adev->asic_type != CHIP_ARCTURUS &&
1990 adev->asic_type != CHIP_SIENNA_CICHLID)
1997 * check hardware's ras ability which will be saved in hw_supported.
1998 * if hardware does not support ras, we can skip some ras initializtion and
1999 * forbid some ras operations from IP.
2000 * if software itself, say boot parameter, limit the ras ability. We still
2001 * need allow IP do some limited operations, like disable. In such case,
2002 * we have to initialize ras as normal. but need check if operation is
2003 * allowed or not in each function.
2005 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
2006 uint32_t *hw_supported, uint32_t *supported)
2011 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
2012 amdgpu_ras_check_asic_type(adev))
2015 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
2016 dev_info(adev->dev, "HBM ECC is active.\n");
2017 *hw_supported |= (1 << AMDGPU_RAS_BLOCK__UMC |
2018 1 << AMDGPU_RAS_BLOCK__DF);
2020 dev_info(adev->dev, "HBM ECC is not presented.\n");
2022 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
2023 dev_info(adev->dev, "SRAM ECC is active.\n");
2024 *hw_supported |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
2025 1 << AMDGPU_RAS_BLOCK__DF);
2027 dev_info(adev->dev, "SRAM ECC is not presented.\n");
2029 /* hw_supported needs to be aligned with RAS block mask. */
2030 *hw_supported &= AMDGPU_RAS_BLOCK_MASK;
2032 *supported = amdgpu_ras_enable == 0 ?
2033 0 : *hw_supported & amdgpu_ras_mask;
2034 adev->ras_features = *supported;
2037 int amdgpu_ras_init(struct amdgpu_device *adev)
2039 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2045 con = kmalloc(sizeof(struct amdgpu_ras) +
2046 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
2047 GFP_KERNEL|__GFP_ZERO);
2051 con->objs = (struct ras_manager *)(con + 1);
2053 amdgpu_ras_set_context(adev, con);
2055 amdgpu_ras_check_supported(adev, &con->hw_supported,
2057 if (!con->hw_supported || (adev->asic_type == CHIP_VEGA10)) {
2063 INIT_LIST_HEAD(&con->head);
2064 /* Might need get this flag from vbios. */
2065 con->flags = RAS_DEFAULT_FLAGS;
2067 if (adev->nbio.funcs->init_ras_controller_interrupt) {
2068 r = adev->nbio.funcs->init_ras_controller_interrupt(adev);
2073 if (adev->nbio.funcs->init_ras_err_event_athub_interrupt) {
2074 r = adev->nbio.funcs->init_ras_err_event_athub_interrupt(adev);
2079 if (amdgpu_ras_fs_init(adev)) {
2084 dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
2085 "hardware ability[%x] ras_mask[%x]\n",
2086 con->hw_supported, con->supported);
2089 amdgpu_ras_set_context(adev, NULL);
2095 /* helper function to handle common stuff in ip late init phase */
2096 int amdgpu_ras_late_init(struct amdgpu_device *adev,
2097 struct ras_common_if *ras_block,
2098 struct ras_fs_if *fs_info,
2099 struct ras_ih_if *ih_info)
2103 /* disable RAS feature per IP block if it is not supported */
2104 if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
2105 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
2109 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
2112 /* request gpu reset. will run again */
2113 amdgpu_ras_request_reset_on_boot(adev,
2116 } else if (adev->in_suspend || amdgpu_in_reset(adev)) {
2117 /* in resume phase, if fail to enable ras,
2118 * clean up all ras fs nodes, and disable ras */
2124 /* in resume phase, no need to create ras fs node */
2125 if (adev->in_suspend || amdgpu_in_reset(adev))
2129 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
2134 r = amdgpu_ras_sysfs_create(adev, fs_info);
2140 amdgpu_ras_sysfs_remove(adev, ras_block);
2143 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2145 amdgpu_ras_feature_enable(adev, ras_block, 0);
2149 /* helper function to remove ras fs node and interrupt handler */
2150 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
2151 struct ras_common_if *ras_block,
2152 struct ras_ih_if *ih_info)
2154 if (!ras_block || !ih_info)
2157 amdgpu_ras_sysfs_remove(adev, ras_block);
2159 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2160 amdgpu_ras_feature_enable(adev, ras_block, 0);
2163 /* do some init work after IP late init as dependence.
2164 * and it runs in resume/gpu reset/booting up cases.
2166 void amdgpu_ras_resume(struct amdgpu_device *adev)
2168 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2169 struct ras_manager *obj, *tmp;
2174 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2175 /* Set up all other IPs which are not implemented. There is a
2176 * tricky thing that IP's actual ras error type should be
2177 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2178 * ERROR_NONE make sense anyway.
2180 amdgpu_ras_enable_all_features(adev, 1);
2182 /* We enable ras on all hw_supported block, but as boot
2183 * parameter might disable some of them and one or more IP has
2184 * not implemented yet. So we disable them on behalf.
2186 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2187 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2188 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2189 /* there should be no any reference. */
2190 WARN_ON(alive_obj(obj));
2195 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
2196 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
2197 /* setup ras obj state as disabled.
2198 * for init_by_vbios case.
2199 * if we want to enable ras, just enable it in a normal way.
2200 * If we want do disable it, need setup ras obj as enabled,
2201 * then issue another TA disable cmd.
2202 * See feature_enable_on_boot
2204 amdgpu_ras_disable_all_features(adev, 1);
2205 amdgpu_ras_reset_gpu(adev);
2209 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2211 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2216 amdgpu_ras_disable_all_features(adev, 0);
2217 /* Make sure all ras objects are disabled. */
2219 amdgpu_ras_disable_all_features(adev, 1);
2222 /* do some fini work before IP fini as dependence */
2223 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2225 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2230 /* Need disable ras on all IPs here before ip [hw/sw]fini */
2231 amdgpu_ras_disable_all_features(adev, 0);
2232 amdgpu_ras_recovery_fini(adev);
2236 int amdgpu_ras_fini(struct amdgpu_device *adev)
2238 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2243 amdgpu_ras_fs_fini(adev);
2244 amdgpu_ras_interrupt_remove_all(adev);
2246 WARN(con->features, "Feature mask is not cleared");
2249 amdgpu_ras_disable_all_features(adev, 1);
2251 amdgpu_ras_set_context(adev, NULL);
2257 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2259 uint32_t hw_supported, supported;
2261 amdgpu_ras_check_supported(adev, &hw_supported, &supported);
2265 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2266 dev_info(adev->dev, "uncorrectable hardware error"
2267 "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2269 amdgpu_ras_reset_gpu(adev);
2273 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
2275 if (adev->asic_type == CHIP_VEGA20 &&
2276 adev->pm.fw_version <= 0x283400) {
2277 return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
2278 amdgpu_ras_intr_triggered();
2284 bool amdgpu_ras_check_err_threshold(struct amdgpu_device *adev)
2286 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2287 bool exc_err_limit = false;
2289 if (con && (amdgpu_bad_page_threshold != 0))
2290 amdgpu_ras_eeprom_check_err_threshold(&con->eeprom_control,
2294 * We are only interested in variable exc_err_limit,
2295 * as it says if GPU is in bad state or not.
2297 return exc_err_limit;