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 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
84 size_t size, loff_t *pos)
86 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
87 struct ras_query_if info = {
93 if (amdgpu_ras_error_query(obj->adev, &info))
96 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
103 s = min_t(u64, s, size);
106 if (copy_to_user(buf, &val[*pos], s))
114 static const struct file_operations amdgpu_ras_debugfs_ops = {
115 .owner = THIS_MODULE,
116 .read = amdgpu_ras_debugfs_read,
118 .llseek = default_llseek
121 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
125 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
127 if (strcmp(name, ras_block_str(i)) == 0)
133 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
134 const char __user *buf, size_t size,
135 loff_t *pos, struct ras_debug_if *data)
137 ssize_t s = min_t(u64, 64, size);
150 memset(str, 0, sizeof(str));
151 memset(data, 0, sizeof(*data));
153 if (copy_from_user(str, buf, s))
156 if (sscanf(str, "disable %32s", block_name) == 1)
158 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
160 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
162 else if (str[0] && str[1] && str[2] && str[3])
163 /* ascii string, but commands are not matched. */
167 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
170 data->head.block = block_id;
171 /* only ue and ce errors are supported */
172 if (!memcmp("ue", err, 2))
173 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
174 else if (!memcmp("ce", err, 2))
175 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
182 if (sscanf(str, "%*s %*s %*s %u %llu %llu",
183 &sub_block, &address, &value) != 3)
184 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
185 &sub_block, &address, &value) != 3)
187 data->head.sub_block_index = sub_block;
188 data->inject.address = address;
189 data->inject.value = value;
192 if (size < sizeof(*data))
195 if (copy_from_user(data, buf, sizeof(*data)))
203 * DOC: AMDGPU RAS debugfs control interface
205 * It accepts struct ras_debug_if who has two members.
207 * First member: ras_debug_if::head or ras_debug_if::inject.
209 * head is used to indicate which IP block will be under control.
211 * head has four members, they are block, type, sub_block_index, name.
212 * block: which IP will be under control.
213 * type: what kind of error will be enabled/disabled/injected.
214 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
215 * name: the name of IP.
217 * inject has two more members than head, they are address, value.
218 * As their names indicate, inject operation will write the
219 * value to the address.
221 * The second member: struct ras_debug_if::op.
222 * It has three kinds of operations.
224 * - 0: disable RAS on the block. Take ::head as its data.
225 * - 1: enable RAS on the block. Take ::head as its data.
226 * - 2: inject errors on the block. Take ::inject as its data.
228 * How to use the interface?
232 * Copy the struct ras_debug_if in your codes and initialize it.
233 * Write the struct to the control node.
237 * .. code-block:: bash
239 * echo op block [error [sub_block address value]] > .../ras/ras_ctrl
243 * op: disable, enable, inject
244 * disable: only block is needed
245 * enable: block and error are needed
246 * inject: error, address, value are needed
247 * block: umc, sdma, gfx, .........
248 * see ras_block_string[] for details
250 * ue: multi_uncorrectable
251 * ce: single_correctable
253 * sub block index, pass 0 if there is no sub block
255 * here are some examples for bash commands:
257 * .. code-block:: bash
259 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
260 * echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
261 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
263 * How to check the result?
265 * For disable/enable, please check ras features at
266 * /sys/class/drm/card[0/1/2...]/device/ras/features
268 * For inject, please check corresponding err count at
269 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
272 * Operations are only allowed on blocks which are supported.
273 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
274 * to see which blocks support RAS on a particular asic.
277 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
278 size_t size, loff_t *pos)
280 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
281 struct ras_debug_if data;
284 if (amdgpu_ras_intr_triggered()) {
285 DRM_WARN("RAS WARN: error injection currently inaccessible\n");
289 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
293 if (!amdgpu_ras_is_supported(adev, data.head.block))
298 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
301 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
304 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
305 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
310 /* umc ce/ue error injection for a bad page is not allowed */
311 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
312 amdgpu_ras_check_bad_page(adev, data.inject.address)) {
313 DRM_WARN("RAS WARN: 0x%llx has been marked as bad before error injection!\n",
314 data.inject.address);
318 /* data.inject.address is offset instead of absolute gpu address */
319 ret = amdgpu_ras_error_inject(adev, &data.inject);
333 * DOC: AMDGPU RAS debugfs EEPROM table reset interface
335 * Some boards contain an EEPROM which is used to persistently store a list of
336 * bad pages which experiences ECC errors in vram. This interface provides
337 * a way to reset the EEPROM, e.g., after testing error injection.
341 * .. code-block:: bash
343 * echo 1 > ../ras/ras_eeprom_reset
345 * will reset EEPROM table to 0 entries.
348 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, const char __user *buf,
349 size_t size, loff_t *pos)
351 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
354 ret = amdgpu_ras_eeprom_reset_table(&adev->psp.ras.ras->eeprom_control);
356 return ret == 1 ? size : -EIO;
359 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
360 .owner = THIS_MODULE,
362 .write = amdgpu_ras_debugfs_ctrl_write,
363 .llseek = default_llseek
366 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
367 .owner = THIS_MODULE,
369 .write = amdgpu_ras_debugfs_eeprom_write,
370 .llseek = default_llseek
374 * DOC: AMDGPU RAS sysfs Error Count Interface
376 * It allows the user to read the error count for each IP block on the gpu through
377 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
379 * It outputs the multiple lines which report the uncorrected (ue) and corrected
382 * The format of one line is below,
388 * .. code-block:: bash
394 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
395 struct device_attribute *attr, char *buf)
397 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
398 struct ras_query_if info = {
402 if (amdgpu_ras_intr_triggered())
403 return snprintf(buf, PAGE_SIZE,
404 "Query currently inaccessible\n");
406 if (amdgpu_ras_error_query(obj->adev, &info))
409 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
411 "ce", info.ce_count);
416 #define get_obj(obj) do { (obj)->use++; } while (0)
417 #define alive_obj(obj) ((obj)->use)
419 static inline void put_obj(struct ras_manager *obj)
421 if (obj && --obj->use == 0)
422 list_del(&obj->node);
423 if (obj && obj->use < 0) {
424 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
428 /* make one obj and return it. */
429 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
430 struct ras_common_if *head)
432 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
433 struct ras_manager *obj;
438 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
441 obj = &con->objs[head->block];
442 /* already exist. return obj? */
448 list_add(&obj->node, &con->head);
454 /* return an obj equal to head, or the first when head is NULL */
455 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
456 struct ras_common_if *head)
458 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
459 struct ras_manager *obj;
466 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
469 obj = &con->objs[head->block];
471 if (alive_obj(obj)) {
472 WARN_ON(head->block != obj->head.block);
476 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
478 if (alive_obj(obj)) {
479 WARN_ON(i != obj->head.block);
489 /* feature ctl begin */
490 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
491 struct ras_common_if *head)
493 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
495 return con->hw_supported & BIT(head->block);
498 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
499 struct ras_common_if *head)
501 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
503 return con->features & BIT(head->block);
507 * if obj is not created, then create one.
508 * set feature enable flag.
510 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
511 struct ras_common_if *head, int enable)
513 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
514 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
516 /* If hardware does not support ras, then do not create obj.
517 * But if hardware support ras, we can create the obj.
518 * Ras framework checks con->hw_supported to see if it need do
519 * corresponding initialization.
520 * IP checks con->support to see if it need disable ras.
522 if (!amdgpu_ras_is_feature_allowed(adev, head))
524 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
529 obj = amdgpu_ras_create_obj(adev, head);
533 /* In case we create obj somewhere else */
536 con->features |= BIT(head->block);
538 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
539 con->features &= ~BIT(head->block);
547 /* wrapper of psp_ras_enable_features */
548 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
549 struct ras_common_if *head, bool enable)
551 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
552 union ta_ras_cmd_input info;
559 info.disable_features = (struct ta_ras_disable_features_input) {
560 .block_id = amdgpu_ras_block_to_ta(head->block),
561 .error_type = amdgpu_ras_error_to_ta(head->type),
564 info.enable_features = (struct ta_ras_enable_features_input) {
565 .block_id = amdgpu_ras_block_to_ta(head->block),
566 .error_type = amdgpu_ras_error_to_ta(head->type),
570 /* Do not enable if it is not allowed. */
571 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
572 /* Are we alerady in that state we are going to set? */
573 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
576 if (!amdgpu_ras_intr_triggered()) {
577 ret = psp_ras_enable_features(&adev->psp, &info, enable);
579 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
580 enable ? "enable":"disable",
581 ras_block_str(head->block),
583 if (ret == TA_RAS_STATUS__RESET_NEEDED)
590 __amdgpu_ras_feature_enable(adev, head, enable);
595 /* Only used in device probe stage and called only once. */
596 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
597 struct ras_common_if *head, bool enable)
599 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
605 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
607 /* There is no harm to issue a ras TA cmd regardless of
608 * the currecnt ras state.
609 * If current state == target state, it will do nothing
610 * But sometimes it requests driver to reset and repost
611 * with error code -EAGAIN.
613 ret = amdgpu_ras_feature_enable(adev, head, 1);
614 /* With old ras TA, we might fail to enable ras.
615 * Log it and just setup the object.
616 * TODO need remove this WA in the future.
618 if (ret == -EINVAL) {
619 ret = __amdgpu_ras_feature_enable(adev, head, 1);
621 DRM_INFO("RAS INFO: %s setup object\n",
622 ras_block_str(head->block));
625 /* setup the object then issue a ras TA disable cmd.*/
626 ret = __amdgpu_ras_feature_enable(adev, head, 1);
630 ret = amdgpu_ras_feature_enable(adev, head, 0);
633 ret = amdgpu_ras_feature_enable(adev, head, enable);
638 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
641 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
642 struct ras_manager *obj, *tmp;
644 list_for_each_entry_safe(obj, tmp, &con->head, node) {
646 * aka just release the obj and corresponding flags
649 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
652 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
657 return con->features;
660 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
663 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
664 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
666 const enum amdgpu_ras_error_type default_ras_type =
667 AMDGPU_RAS_ERROR__NONE;
669 for (i = 0; i < ras_block_count; i++) {
670 struct ras_common_if head = {
672 .type = default_ras_type,
673 .sub_block_index = 0,
675 strcpy(head.name, ras_block_str(i));
678 * bypass psp. vbios enable ras for us.
679 * so just create the obj
681 if (__amdgpu_ras_feature_enable(adev, &head, 1))
684 if (amdgpu_ras_feature_enable(adev, &head, 1))
689 return con->features;
691 /* feature ctl end */
693 /* query/inject/cure begin */
694 int amdgpu_ras_error_query(struct amdgpu_device *adev,
695 struct ras_query_if *info)
697 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
698 struct ras_err_data err_data = {0, 0, 0, NULL};
704 switch (info->head.block) {
705 case AMDGPU_RAS_BLOCK__UMC:
706 if (adev->umc.funcs->query_ras_error_count)
707 adev->umc.funcs->query_ras_error_count(adev, &err_data);
708 /* umc query_ras_error_address is also responsible for clearing
711 if (adev->umc.funcs->query_ras_error_address)
712 adev->umc.funcs->query_ras_error_address(adev, &err_data);
714 case AMDGPU_RAS_BLOCK__SDMA:
715 if (adev->sdma.funcs->query_ras_error_count) {
716 for (i = 0; i < adev->sdma.num_instances; i++)
717 adev->sdma.funcs->query_ras_error_count(adev, i,
721 case AMDGPU_RAS_BLOCK__GFX:
722 if (adev->gfx.funcs->query_ras_error_count)
723 adev->gfx.funcs->query_ras_error_count(adev, &err_data);
725 case AMDGPU_RAS_BLOCK__MMHUB:
726 if (adev->mmhub.funcs->query_ras_error_count)
727 adev->mmhub.funcs->query_ras_error_count(adev, &err_data);
729 case AMDGPU_RAS_BLOCK__PCIE_BIF:
730 if (adev->nbio.funcs->query_ras_error_count)
731 adev->nbio.funcs->query_ras_error_count(adev, &err_data);
733 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
734 amdgpu_xgmi_query_ras_error_count(adev, &err_data);
740 obj->err_data.ue_count += err_data.ue_count;
741 obj->err_data.ce_count += err_data.ce_count;
743 info->ue_count = obj->err_data.ue_count;
744 info->ce_count = obj->err_data.ce_count;
746 if (err_data.ce_count) {
747 dev_info(adev->dev, "%ld correctable errors detected in %s block\n",
748 obj->err_data.ce_count, ras_block_str(info->head.block));
750 if (err_data.ue_count) {
751 dev_info(adev->dev, "%ld uncorrectable errors detected in %s block\n",
752 obj->err_data.ue_count, ras_block_str(info->head.block));
758 /* wrapper of psp_ras_trigger_error */
759 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
760 struct ras_inject_if *info)
762 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
763 struct ta_ras_trigger_error_input block_info = {
764 .block_id = amdgpu_ras_block_to_ta(info->head.block),
765 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
766 .sub_block_index = info->head.sub_block_index,
767 .address = info->address,
768 .value = info->value,
775 /* Calculate XGMI relative offset */
776 if (adev->gmc.xgmi.num_physical_nodes > 1) {
778 amdgpu_xgmi_get_relative_phy_addr(adev,
782 switch (info->head.block) {
783 case AMDGPU_RAS_BLOCK__GFX:
784 if (adev->gfx.funcs->ras_error_inject)
785 ret = adev->gfx.funcs->ras_error_inject(adev, info);
789 case AMDGPU_RAS_BLOCK__UMC:
790 case AMDGPU_RAS_BLOCK__MMHUB:
791 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
792 case AMDGPU_RAS_BLOCK__PCIE_BIF:
793 ret = psp_ras_trigger_error(&adev->psp, &block_info);
796 DRM_INFO("%s error injection is not supported yet\n",
797 ras_block_str(info->head.block));
802 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
803 ras_block_str(info->head.block),
809 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
810 struct ras_cure_if *info)
812 /* psp fw has no cure interface for now. */
816 /* get the total error counts on all IPs */
817 unsigned long amdgpu_ras_query_error_count(struct amdgpu_device *adev,
820 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
821 struct ras_manager *obj;
822 struct ras_err_data data = {0, 0};
827 list_for_each_entry(obj, &con->head, node) {
828 struct ras_query_if info = {
832 if (amdgpu_ras_error_query(adev, &info))
835 data.ce_count += info.ce_count;
836 data.ue_count += info.ue_count;
839 return is_ce ? data.ce_count : data.ue_count;
841 /* query/inject/cure end */
846 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
847 struct ras_badpage **bps, unsigned int *count);
849 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
852 case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
854 case AMDGPU_RAS_RETIRE_PAGE_PENDING:
856 case AMDGPU_RAS_RETIRE_PAGE_FAULT:
863 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
865 * It allows user to read the bad pages of vram on the gpu through
866 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
868 * It outputs multiple lines, and each line stands for one gpu page.
870 * The format of one line is below,
871 * gpu pfn : gpu page size : flags
873 * gpu pfn and gpu page size are printed in hex format.
874 * flags can be one of below character,
876 * R: reserved, this gpu page is reserved and not able to use.
878 * P: pending for reserve, this gpu page is marked as bad, will be reserved
879 * in next window of page_reserve.
881 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
885 * .. code-block:: bash
887 * 0x00000001 : 0x00001000 : R
888 * 0x00000002 : 0x00001000 : P
892 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
893 struct kobject *kobj, struct bin_attribute *attr,
894 char *buf, loff_t ppos, size_t count)
896 struct amdgpu_ras *con =
897 container_of(attr, struct amdgpu_ras, badpages_attr);
898 struct amdgpu_device *adev = con->adev;
899 const unsigned int element_size =
900 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
901 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
902 unsigned int end = div64_ul(ppos + count - 1, element_size);
904 struct ras_badpage *bps = NULL;
905 unsigned int bps_count = 0;
907 memset(buf, 0, count);
909 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
912 for (; start < end && start < bps_count; start++)
913 s += scnprintf(&buf[s], element_size + 1,
914 "0x%08x : 0x%08x : %1s\n",
917 amdgpu_ras_badpage_flags_str(bps[start].flags));
924 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
925 struct device_attribute *attr, char *buf)
927 struct amdgpu_ras *con =
928 container_of(attr, struct amdgpu_ras, features_attr);
930 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
933 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
935 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
936 struct attribute *attrs[] = {
937 &con->features_attr.attr,
940 struct bin_attribute *bin_attrs[] = {
944 struct attribute_group group = {
947 .bin_attrs = bin_attrs,
950 con->features_attr = (struct device_attribute) {
955 .show = amdgpu_ras_sysfs_features_read,
958 con->badpages_attr = (struct bin_attribute) {
960 .name = "gpu_vram_bad_pages",
965 .read = amdgpu_ras_sysfs_badpages_read,
968 sysfs_attr_init(attrs[0]);
969 sysfs_bin_attr_init(bin_attrs[0]);
971 return sysfs_create_group(&adev->dev->kobj, &group);
974 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
976 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
977 struct attribute *attrs[] = {
978 &con->features_attr.attr,
981 struct bin_attribute *bin_attrs[] = {
985 struct attribute_group group = {
988 .bin_attrs = bin_attrs,
991 sysfs_remove_group(&adev->dev->kobj, &group);
996 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
997 struct ras_fs_if *head)
999 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1001 if (!obj || obj->attr_inuse)
1006 memcpy(obj->fs_data.sysfs_name,
1008 sizeof(obj->fs_data.sysfs_name));
1010 obj->sysfs_attr = (struct device_attribute){
1012 .name = obj->fs_data.sysfs_name,
1015 .show = amdgpu_ras_sysfs_read,
1017 sysfs_attr_init(&obj->sysfs_attr.attr);
1019 if (sysfs_add_file_to_group(&adev->dev->kobj,
1020 &obj->sysfs_attr.attr,
1026 obj->attr_inuse = 1;
1031 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1032 struct ras_common_if *head)
1034 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1036 if (!obj || !obj->attr_inuse)
1039 sysfs_remove_file_from_group(&adev->dev->kobj,
1040 &obj->sysfs_attr.attr,
1042 obj->attr_inuse = 0;
1048 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1050 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1051 struct ras_manager *obj, *tmp;
1053 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1054 amdgpu_ras_sysfs_remove(adev, &obj->head);
1057 amdgpu_ras_sysfs_remove_feature_node(adev);
1064 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1066 * Normally when there is an uncorrectable error, the driver will reset
1067 * the GPU to recover. However, in the event of an unrecoverable error,
1068 * the driver provides an interface to reboot the system automatically
1071 * The following file in debugfs provides that interface:
1072 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1076 * .. code-block:: bash
1078 * echo true > .../ras/auto_reboot
1082 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1084 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1085 struct drm_minor *minor = adev->ddev->primary;
1087 con->dir = debugfs_create_dir("ras", minor->debugfs_root);
1088 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
1089 adev, &amdgpu_ras_debugfs_ctrl_ops);
1090 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, con->dir,
1091 adev, &amdgpu_ras_debugfs_eeprom_ops);
1094 * After one uncorrectable error happens, usually GPU recovery will
1095 * be scheduled. But due to the known problem in GPU recovery failing
1096 * to bring GPU back, below interface provides one direct way to
1097 * user to reboot system automatically in such case within
1098 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1099 * will never be called.
1101 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, con->dir,
1105 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1106 struct ras_fs_if *head)
1108 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1109 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1111 if (!obj || obj->ent)
1116 memcpy(obj->fs_data.debugfs_name,
1118 sizeof(obj->fs_data.debugfs_name));
1120 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
1121 S_IWUGO | S_IRUGO, con->dir, obj,
1122 &amdgpu_ras_debugfs_ops);
1125 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1127 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1128 struct ras_manager *obj;
1129 struct ras_fs_if fs_info;
1132 * it won't be called in resume path, no need to check
1133 * suspend and gpu reset status
1138 amdgpu_ras_debugfs_create_ctrl_node(adev);
1140 list_for_each_entry(obj, &con->head, node) {
1141 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1142 (obj->attr_inuse == 1)) {
1143 sprintf(fs_info.debugfs_name, "%s_err_inject",
1144 ras_block_str(obj->head.block));
1145 fs_info.head = obj->head;
1146 amdgpu_ras_debugfs_create(adev, &fs_info);
1151 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1152 struct ras_common_if *head)
1154 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1156 if (!obj || !obj->ent)
1159 debugfs_remove(obj->ent);
1164 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1166 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1167 struct ras_manager *obj, *tmp;
1169 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1170 amdgpu_ras_debugfs_remove(adev, &obj->head);
1173 debugfs_remove_recursive(con->dir);
1180 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1182 amdgpu_ras_sysfs_create_feature_node(adev);
1187 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1189 amdgpu_ras_debugfs_remove_all(adev);
1190 amdgpu_ras_sysfs_remove_all(adev);
1196 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1198 struct ras_ih_data *data = &obj->ih_data;
1199 struct amdgpu_iv_entry entry;
1201 struct ras_err_data err_data = {0, 0, 0, NULL};
1203 while (data->rptr != data->wptr) {
1205 memcpy(&entry, &data->ring[data->rptr],
1206 data->element_size);
1209 data->rptr = (data->aligned_element_size +
1210 data->rptr) % data->ring_size;
1212 /* Let IP handle its data, maybe we need get the output
1213 * from the callback to udpate the error type/count, etc
1216 ret = data->cb(obj->adev, &err_data, &entry);
1217 /* ue will trigger an interrupt, and in that case
1218 * we need do a reset to recovery the whole system.
1219 * But leave IP do that recovery, here we just dispatch
1222 if (ret == AMDGPU_RAS_SUCCESS) {
1223 /* these counts could be left as 0 if
1224 * some blocks do not count error number
1226 obj->err_data.ue_count += err_data.ue_count;
1227 obj->err_data.ce_count += err_data.ce_count;
1233 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1235 struct ras_ih_data *data =
1236 container_of(work, struct ras_ih_data, ih_work);
1237 struct ras_manager *obj =
1238 container_of(data, struct ras_manager, ih_data);
1240 amdgpu_ras_interrupt_handler(obj);
1243 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1244 struct ras_dispatch_if *info)
1246 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1247 struct ras_ih_data *data = &obj->ih_data;
1252 if (data->inuse == 0)
1255 /* Might be overflow... */
1256 memcpy(&data->ring[data->wptr], info->entry,
1257 data->element_size);
1260 data->wptr = (data->aligned_element_size +
1261 data->wptr) % data->ring_size;
1263 schedule_work(&data->ih_work);
1268 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1269 struct ras_ih_if *info)
1271 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1272 struct ras_ih_data *data;
1277 data = &obj->ih_data;
1278 if (data->inuse == 0)
1281 cancel_work_sync(&data->ih_work);
1284 memset(data, 0, sizeof(*data));
1290 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1291 struct ras_ih_if *info)
1293 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1294 struct ras_ih_data *data;
1297 /* in case we registe the IH before enable ras feature */
1298 obj = amdgpu_ras_create_obj(adev, &info->head);
1304 data = &obj->ih_data;
1305 /* add the callback.etc */
1306 *data = (struct ras_ih_data) {
1309 .element_size = sizeof(struct amdgpu_iv_entry),
1314 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1316 data->aligned_element_size = ALIGN(data->element_size, 8);
1317 /* the ring can store 64 iv entries. */
1318 data->ring_size = 64 * data->aligned_element_size;
1319 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1331 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1333 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1334 struct ras_manager *obj, *tmp;
1336 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1337 struct ras_ih_if info = {
1340 amdgpu_ras_interrupt_remove_handler(adev, &info);
1347 /* traversal all IPs except NBIO to query error counter */
1348 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1350 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1351 struct ras_manager *obj;
1356 list_for_each_entry(obj, &con->head, node) {
1357 struct ras_query_if info = {
1362 * PCIE_BIF IP has one different isr by ras controller
1363 * interrupt, the specific ras counter query will be
1364 * done in that isr. So skip such block from common
1365 * sync flood interrupt isr calling.
1367 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1370 amdgpu_ras_error_query(adev, &info);
1374 /* recovery begin */
1376 /* return 0 on success.
1377 * caller need free bps.
1379 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1380 struct ras_badpage **bps, unsigned int *count)
1382 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1383 struct ras_err_handler_data *data;
1387 if (!con || !con->eh_data || !bps || !count)
1390 mutex_lock(&con->recovery_lock);
1391 data = con->eh_data;
1392 if (!data || data->count == 0) {
1398 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1404 for (; i < data->count; i++) {
1405 (*bps)[i] = (struct ras_badpage){
1406 .bp = data->bps[i].retired_page,
1407 .size = AMDGPU_GPU_PAGE_SIZE,
1408 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1411 if (data->last_reserved <= i)
1412 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1413 else if (data->bps_bo[i] == NULL)
1414 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1417 *count = data->count;
1419 mutex_unlock(&con->recovery_lock);
1423 static void amdgpu_ras_do_recovery(struct work_struct *work)
1425 struct amdgpu_ras *ras =
1426 container_of(work, struct amdgpu_ras, recovery_work);
1429 * Query and print non zero error counter per IP block for
1430 * awareness before recovering GPU.
1432 amdgpu_ras_log_on_err_counter(ras->adev);
1434 if (amdgpu_device_should_recover_gpu(ras->adev))
1435 amdgpu_device_gpu_recover(ras->adev, 0);
1436 atomic_set(&ras->in_recovery, 0);
1439 /* alloc/realloc bps array */
1440 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1441 struct ras_err_handler_data *data, int pages)
1443 unsigned int old_space = data->count + data->space_left;
1444 unsigned int new_space = old_space + pages;
1445 unsigned int align_space = ALIGN(new_space, 512);
1446 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1447 struct amdgpu_bo **bps_bo =
1448 kmalloc(align_space * sizeof(*data->bps_bo), GFP_KERNEL);
1450 if (!bps || !bps_bo) {
1457 memcpy(bps, data->bps,
1458 data->count * sizeof(*data->bps));
1462 memcpy(bps_bo, data->bps_bo,
1463 data->count * sizeof(*data->bps_bo));
1464 kfree(data->bps_bo);
1468 data->bps_bo = bps_bo;
1469 data->space_left += align_space - old_space;
1473 /* it deal with vram only. */
1474 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1475 struct eeprom_table_record *bps, int pages)
1477 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1478 struct ras_err_handler_data *data;
1481 if (!con || !con->eh_data || !bps || pages <= 0)
1484 mutex_lock(&con->recovery_lock);
1485 data = con->eh_data;
1489 if (data->space_left <= pages)
1490 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1495 memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
1496 data->count += pages;
1497 data->space_left -= pages;
1500 mutex_unlock(&con->recovery_lock);
1506 * write error record array to eeprom, the function should be
1507 * protected by recovery_lock
1509 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1511 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1512 struct ras_err_handler_data *data;
1513 struct amdgpu_ras_eeprom_control *control;
1516 if (!con || !con->eh_data)
1519 control = &con->eeprom_control;
1520 data = con->eh_data;
1521 save_count = data->count - control->num_recs;
1522 /* only new entries are saved */
1524 if (amdgpu_ras_eeprom_process_recods(control,
1525 &data->bps[control->num_recs],
1528 DRM_ERROR("Failed to save EEPROM table data!");
1536 * read error record array in eeprom and reserve enough space for
1537 * storing new bad pages
1539 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1541 struct amdgpu_ras_eeprom_control *control =
1542 &adev->psp.ras.ras->eeprom_control;
1543 struct eeprom_table_record *bps = NULL;
1546 /* no bad page record, skip eeprom access */
1547 if (!control->num_recs)
1550 bps = kcalloc(control->num_recs, sizeof(*bps), GFP_KERNEL);
1554 if (amdgpu_ras_eeprom_process_recods(control, bps, false,
1555 control->num_recs)) {
1556 DRM_ERROR("Failed to load EEPROM table records!");
1561 ret = amdgpu_ras_add_bad_pages(adev, bps, control->num_recs);
1569 * check if an address belongs to bad page
1571 * Note: this check is only for umc block
1573 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
1576 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1577 struct ras_err_handler_data *data;
1581 if (!con || !con->eh_data)
1584 mutex_lock(&con->recovery_lock);
1585 data = con->eh_data;
1589 addr >>= AMDGPU_GPU_PAGE_SHIFT;
1590 for (i = 0; i < data->count; i++)
1591 if (addr == data->bps[i].retired_page) {
1597 mutex_unlock(&con->recovery_lock);
1601 /* called in gpu recovery/init */
1602 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1604 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1605 struct ras_err_handler_data *data;
1607 struct amdgpu_bo *bo = NULL;
1610 if (!con || !con->eh_data)
1613 mutex_lock(&con->recovery_lock);
1614 data = con->eh_data;
1617 /* reserve vram at driver post stage. */
1618 for (i = data->last_reserved; i < data->count; i++) {
1619 bp = data->bps[i].retired_page;
1621 /* There are two cases of reserve error should be ignored:
1622 * 1) a ras bad page has been allocated (used by someone);
1623 * 2) a ras bad page has been reserved (duplicate error injection
1626 if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
1627 AMDGPU_GPU_PAGE_SIZE,
1628 AMDGPU_GEM_DOMAIN_VRAM,
1630 DRM_WARN("RAS WARN: reserve vram for retired page %llx fail\n", bp);
1632 data->bps_bo[i] = bo;
1633 data->last_reserved = i + 1;
1637 /* continue to save bad pages to eeprom even reesrve_vram fails */
1638 ret = amdgpu_ras_save_bad_pages(adev);
1640 mutex_unlock(&con->recovery_lock);
1644 /* called when driver unload */
1645 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1647 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1648 struct ras_err_handler_data *data;
1649 struct amdgpu_bo *bo;
1652 if (!con || !con->eh_data)
1655 mutex_lock(&con->recovery_lock);
1656 data = con->eh_data;
1660 for (i = data->last_reserved - 1; i >= 0; i--) {
1661 bo = data->bps_bo[i];
1663 amdgpu_bo_free_kernel(&bo, NULL, NULL);
1665 data->bps_bo[i] = bo;
1666 data->last_reserved = i;
1669 mutex_unlock(&con->recovery_lock);
1673 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1675 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1676 struct ras_err_handler_data **data;
1680 data = &con->eh_data;
1684 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
1690 mutex_init(&con->recovery_lock);
1691 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1692 atomic_set(&con->in_recovery, 0);
1695 ret = amdgpu_ras_eeprom_init(&con->eeprom_control);
1699 if (con->eeprom_control.num_recs) {
1700 ret = amdgpu_ras_load_bad_pages(adev);
1703 ret = amdgpu_ras_reserve_bad_pages(adev);
1711 amdgpu_ras_release_bad_pages(adev);
1713 kfree((*data)->bps);
1714 kfree((*data)->bps_bo);
1716 con->eh_data = NULL;
1718 DRM_WARN("Failed to initialize ras recovery!\n");
1723 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1725 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1726 struct ras_err_handler_data *data = con->eh_data;
1728 /* recovery_init failed to init it, fini is useless */
1732 cancel_work_sync(&con->recovery_work);
1733 amdgpu_ras_release_bad_pages(adev);
1735 mutex_lock(&con->recovery_lock);
1736 con->eh_data = NULL;
1738 kfree(data->bps_bo);
1740 mutex_unlock(&con->recovery_lock);
1746 /* return 0 if ras will reset gpu and repost.*/
1747 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1750 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1755 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1760 * check hardware's ras ability which will be saved in hw_supported.
1761 * if hardware does not support ras, we can skip some ras initializtion and
1762 * forbid some ras operations from IP.
1763 * if software itself, say boot parameter, limit the ras ability. We still
1764 * need allow IP do some limited operations, like disable. In such case,
1765 * we have to initialize ras as normal. but need check if operation is
1766 * allowed or not in each function.
1768 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1769 uint32_t *hw_supported, uint32_t *supported)
1774 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
1775 (adev->asic_type != CHIP_VEGA20 &&
1776 adev->asic_type != CHIP_ARCTURUS))
1779 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
1780 DRM_INFO("HBM ECC is active.\n");
1781 *hw_supported |= (1 << AMDGPU_RAS_BLOCK__UMC |
1782 1 << AMDGPU_RAS_BLOCK__DF);
1784 DRM_INFO("HBM ECC is not presented.\n");
1786 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
1787 DRM_INFO("SRAM ECC is active.\n");
1788 *hw_supported |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
1789 1 << AMDGPU_RAS_BLOCK__DF);
1791 DRM_INFO("SRAM ECC is not presented.\n");
1793 /* hw_supported needs to be aligned with RAS block mask. */
1794 *hw_supported &= AMDGPU_RAS_BLOCK_MASK;
1796 *supported = amdgpu_ras_enable == 0 ?
1797 0 : *hw_supported & amdgpu_ras_mask;
1800 int amdgpu_ras_init(struct amdgpu_device *adev)
1802 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1808 con = kmalloc(sizeof(struct amdgpu_ras) +
1809 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1810 GFP_KERNEL|__GFP_ZERO);
1814 con->objs = (struct ras_manager *)(con + 1);
1816 amdgpu_ras_set_context(adev, con);
1818 amdgpu_ras_check_supported(adev, &con->hw_supported,
1820 if (!con->hw_supported) {
1821 amdgpu_ras_set_context(adev, NULL);
1827 INIT_LIST_HEAD(&con->head);
1828 /* Might need get this flag from vbios. */
1829 con->flags = RAS_DEFAULT_FLAGS;
1831 if (adev->nbio.funcs->init_ras_controller_interrupt) {
1832 r = adev->nbio.funcs->init_ras_controller_interrupt(adev);
1837 if (adev->nbio.funcs->init_ras_err_event_athub_interrupt) {
1838 r = adev->nbio.funcs->init_ras_err_event_athub_interrupt(adev);
1843 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1845 if (amdgpu_ras_fs_init(adev))
1848 DRM_INFO("RAS INFO: ras initialized successfully, "
1849 "hardware ability[%x] ras_mask[%x]\n",
1850 con->hw_supported, con->supported);
1853 amdgpu_ras_set_context(adev, NULL);
1859 /* helper function to handle common stuff in ip late init phase */
1860 int amdgpu_ras_late_init(struct amdgpu_device *adev,
1861 struct ras_common_if *ras_block,
1862 struct ras_fs_if *fs_info,
1863 struct ras_ih_if *ih_info)
1867 /* disable RAS feature per IP block if it is not supported */
1868 if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
1869 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
1873 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
1876 /* request gpu reset. will run again */
1877 amdgpu_ras_request_reset_on_boot(adev,
1880 } else if (adev->in_suspend || adev->in_gpu_reset) {
1881 /* in resume phase, if fail to enable ras,
1882 * clean up all ras fs nodes, and disable ras */
1888 /* in resume phase, no need to create ras fs node */
1889 if (adev->in_suspend || adev->in_gpu_reset)
1893 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
1898 r = amdgpu_ras_sysfs_create(adev, fs_info);
1904 amdgpu_ras_sysfs_remove(adev, ras_block);
1907 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
1909 amdgpu_ras_feature_enable(adev, ras_block, 0);
1913 /* helper function to remove ras fs node and interrupt handler */
1914 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
1915 struct ras_common_if *ras_block,
1916 struct ras_ih_if *ih_info)
1918 if (!ras_block || !ih_info)
1921 amdgpu_ras_sysfs_remove(adev, ras_block);
1923 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
1924 amdgpu_ras_feature_enable(adev, ras_block, 0);
1927 /* do some init work after IP late init as dependence.
1928 * and it runs in resume/gpu reset/booting up cases.
1930 void amdgpu_ras_resume(struct amdgpu_device *adev)
1932 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1933 struct ras_manager *obj, *tmp;
1938 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1939 /* Set up all other IPs which are not implemented. There is a
1940 * tricky thing that IP's actual ras error type should be
1941 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1942 * ERROR_NONE make sense anyway.
1944 amdgpu_ras_enable_all_features(adev, 1);
1946 /* We enable ras on all hw_supported block, but as boot
1947 * parameter might disable some of them and one or more IP has
1948 * not implemented yet. So we disable them on behalf.
1950 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1951 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1952 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1953 /* there should be no any reference. */
1954 WARN_ON(alive_obj(obj));
1959 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1960 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1961 /* setup ras obj state as disabled.
1962 * for init_by_vbios case.
1963 * if we want to enable ras, just enable it in a normal way.
1964 * If we want do disable it, need setup ras obj as enabled,
1965 * then issue another TA disable cmd.
1966 * See feature_enable_on_boot
1968 amdgpu_ras_disable_all_features(adev, 1);
1969 amdgpu_ras_reset_gpu(adev);
1973 void amdgpu_ras_suspend(struct amdgpu_device *adev)
1975 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1980 amdgpu_ras_disable_all_features(adev, 0);
1981 /* Make sure all ras objects are disabled. */
1983 amdgpu_ras_disable_all_features(adev, 1);
1986 /* do some fini work before IP fini as dependence */
1987 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1989 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1994 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1995 amdgpu_ras_disable_all_features(adev, 0);
1996 amdgpu_ras_recovery_fini(adev);
2000 int amdgpu_ras_fini(struct amdgpu_device *adev)
2002 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2007 amdgpu_ras_fs_fini(adev);
2008 amdgpu_ras_interrupt_remove_all(adev);
2010 WARN(con->features, "Feature mask is not cleared");
2013 amdgpu_ras_disable_all_features(adev, 1);
2015 amdgpu_ras_set_context(adev, NULL);
2021 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2023 uint32_t hw_supported, supported;
2025 amdgpu_ras_check_supported(adev, &hw_supported, &supported);
2029 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2030 DRM_WARN("RAS event of type ERREVENT_ATHUB_INTERRUPT detected!\n");
2032 amdgpu_ras_reset_gpu(adev);