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"
38 static const char *RAS_FS_NAME = "ras";
40 const char *ras_error_string[] = {
44 "multi_uncorrectable",
48 const char *ras_block_string[] = {
65 #define ras_err_str(i) (ras_error_string[ffs(i)])
66 #define ras_block_str(i) (ras_block_string[i])
68 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
70 /* inject address is 52 bits */
71 #define RAS_UMC_INJECT_ADDR_LIMIT (0x1ULL << 52)
73 /* typical ECC bad page rate(1 bad page per 100MB VRAM) */
74 #define RAS_BAD_PAGE_RATE (100 * 1024 * 1024ULL)
76 enum amdgpu_ras_retire_page_reservation {
77 AMDGPU_RAS_RETIRE_PAGE_RESERVED,
78 AMDGPU_RAS_RETIRE_PAGE_PENDING,
79 AMDGPU_RAS_RETIRE_PAGE_FAULT,
82 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
84 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
86 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
89 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
91 if (adev && amdgpu_ras_get_context(adev))
92 amdgpu_ras_get_context(adev)->error_query_ready = ready;
95 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
97 if (adev && amdgpu_ras_get_context(adev))
98 return amdgpu_ras_get_context(adev)->error_query_ready;
103 static int amdgpu_reserve_page_direct(struct amdgpu_device *adev, uint64_t address)
105 struct ras_err_data err_data = {0, 0, 0, NULL};
106 struct eeprom_table_record err_rec;
108 if ((address >= adev->gmc.mc_vram_size) ||
109 (address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
111 "RAS WARN: input address 0x%llx is invalid.\n",
116 if (amdgpu_ras_check_bad_page(adev, address)) {
118 "RAS WARN: 0x%llx has already been marked as bad page!\n",
123 memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
125 err_rec.address = address;
126 err_rec.retired_page = address >> AMDGPU_GPU_PAGE_SHIFT;
127 err_rec.ts = (uint64_t)ktime_get_real_seconds();
128 err_rec.err_type = AMDGPU_RAS_EEPROM_ERR_NON_RECOVERABLE;
130 err_data.err_addr = &err_rec;
131 err_data.err_addr_cnt = 1;
133 if (amdgpu_bad_page_threshold != 0) {
134 amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
135 err_data.err_addr_cnt);
136 amdgpu_ras_save_bad_pages(adev);
139 dev_warn(adev->dev, "WARNING: THIS IS ONLY FOR TEST PURPOSES AND WILL CORRUPT RAS EEPROM\n");
140 dev_warn(adev->dev, "Clear EEPROM:\n");
141 dev_warn(adev->dev, " echo 1 > /sys/kernel/debug/dri/0/ras/ras_eeprom_reset\n");
146 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
147 size_t size, loff_t *pos)
149 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
150 struct ras_query_if info = {
156 if (amdgpu_ras_query_error_status(obj->adev, &info))
159 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
161 "ce", info.ce_count);
166 s = min_t(u64, s, size);
169 if (copy_to_user(buf, &val[*pos], s))
177 static const struct file_operations amdgpu_ras_debugfs_ops = {
178 .owner = THIS_MODULE,
179 .read = amdgpu_ras_debugfs_read,
181 .llseek = default_llseek
184 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
188 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
190 if (strcmp(name, ras_block_str(i)) == 0)
196 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
197 const char __user *buf, size_t size,
198 loff_t *pos, struct ras_debug_if *data)
200 ssize_t s = min_t(u64, 64, size);
213 memset(str, 0, sizeof(str));
214 memset(data, 0, sizeof(*data));
216 if (copy_from_user(str, buf, s))
219 if (sscanf(str, "disable %32s", block_name) == 1)
221 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
223 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
225 else if (strstr(str, "retire_page") != NULL)
227 else if (str[0] && str[1] && str[2] && str[3])
228 /* ascii string, but commands are not matched. */
233 if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
234 sscanf(str, "%*s %llu", &address) != 1)
238 data->inject.address = address;
243 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
246 data->head.block = block_id;
247 /* only ue and ce errors are supported */
248 if (!memcmp("ue", err, 2))
249 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
250 else if (!memcmp("ce", err, 2))
251 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
258 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
259 &sub_block, &address, &value) != 3 &&
260 sscanf(str, "%*s %*s %*s %u %llu %llu",
261 &sub_block, &address, &value) != 3)
263 data->head.sub_block_index = sub_block;
264 data->inject.address = address;
265 data->inject.value = value;
268 if (size < sizeof(*data))
271 if (copy_from_user(data, buf, sizeof(*data)))
279 * DOC: AMDGPU RAS debugfs control interface
281 * The control interface accepts struct ras_debug_if which has two members.
283 * First member: ras_debug_if::head or ras_debug_if::inject.
285 * head is used to indicate which IP block will be under control.
287 * head has four members, they are block, type, sub_block_index, name.
288 * block: which IP will be under control.
289 * type: what kind of error will be enabled/disabled/injected.
290 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
291 * name: the name of IP.
293 * inject has two more members than head, they are address, value.
294 * As their names indicate, inject operation will write the
295 * value to the address.
297 * The second member: struct ras_debug_if::op.
298 * It has three kinds of operations.
300 * - 0: disable RAS on the block. Take ::head as its data.
301 * - 1: enable RAS on the block. Take ::head as its data.
302 * - 2: inject errors on the block. Take ::inject as its data.
304 * How to use the interface?
308 * Copy the struct ras_debug_if in your code and initialize it.
309 * Write the struct to the control interface.
313 * .. code-block:: bash
315 * echo "disable <block>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
316 * echo "enable <block> <error>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
317 * echo "inject <block> <error> <sub-block> <address> <value> > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
319 * Where N, is the card which you want to affect.
321 * "disable" requires only the block.
322 * "enable" requires the block and error type.
323 * "inject" requires the block, error type, address, and value.
325 * The block is one of: umc, sdma, gfx, etc.
326 * see ras_block_string[] for details
328 * The error type is one of: ue, ce, where,
329 * ue is multi-uncorrectable
330 * ce is single-correctable
332 * The sub-block is a the sub-block index, pass 0 if there is no sub-block.
333 * The address and value are hexadecimal numbers, leading 0x is optional.
337 * .. code-block:: bash
339 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
340 * echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
341 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
343 * How to check the result of the operation?
345 * To check disable/enable, see "ras" features at,
346 * /sys/class/drm/card[0/1/2...]/device/ras/features
348 * To check inject, see the corresponding error count at,
349 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx|sdma|umc|...]_err_count
352 * Operations are only allowed on blocks which are supported.
353 * Check the "ras" mask at /sys/module/amdgpu/parameters/ras_mask
354 * to see which blocks support RAS on a particular asic.
357 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
358 size_t size, loff_t *pos)
360 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
361 struct ras_debug_if data;
364 if (!amdgpu_ras_get_error_query_ready(adev)) {
365 dev_warn(adev->dev, "RAS WARN: error injection "
366 "currently inaccessible\n");
370 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
375 ret = amdgpu_reserve_page_direct(adev, data.inject.address);
382 if (!amdgpu_ras_is_supported(adev, data.head.block))
387 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
390 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
393 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
394 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
395 dev_warn(adev->dev, "RAS WARN: input address "
396 "0x%llx is invalid.",
397 data.inject.address);
402 /* umc ce/ue error injection for a bad page is not allowed */
403 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
404 amdgpu_ras_check_bad_page(adev, data.inject.address)) {
405 dev_warn(adev->dev, "RAS WARN: 0x%llx has been marked "
406 "as bad before error injection!\n",
407 data.inject.address);
411 /* data.inject.address is offset instead of absolute gpu address */
412 ret = amdgpu_ras_error_inject(adev, &data.inject);
426 * DOC: AMDGPU RAS debugfs EEPROM table reset interface
428 * Some boards contain an EEPROM which is used to persistently store a list of
429 * bad pages which experiences ECC errors in vram. This interface provides
430 * a way to reset the EEPROM, e.g., after testing error injection.
434 * .. code-block:: bash
436 * echo 1 > ../ras/ras_eeprom_reset
438 * will reset EEPROM table to 0 entries.
441 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, const char __user *buf,
442 size_t size, loff_t *pos)
444 struct amdgpu_device *adev =
445 (struct amdgpu_device *)file_inode(f)->i_private;
448 ret = amdgpu_ras_eeprom_reset_table(
449 &(amdgpu_ras_get_context(adev)->eeprom_control));
452 amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS;
459 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
460 .owner = THIS_MODULE,
462 .write = amdgpu_ras_debugfs_ctrl_write,
463 .llseek = default_llseek
466 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
467 .owner = THIS_MODULE,
469 .write = amdgpu_ras_debugfs_eeprom_write,
470 .llseek = default_llseek
474 * DOC: AMDGPU RAS sysfs Error Count Interface
476 * It allows the user to read the error count for each IP block on the gpu through
477 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
479 * It outputs the multiple lines which report the uncorrected (ue) and corrected
482 * The format of one line is below,
488 * .. code-block:: bash
494 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
495 struct device_attribute *attr, char *buf)
497 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
498 struct ras_query_if info = {
502 if (!amdgpu_ras_get_error_query_ready(obj->adev))
503 return sysfs_emit(buf, "Query currently inaccessible\n");
505 if (amdgpu_ras_query_error_status(obj->adev, &info))
509 if (obj->adev->asic_type == CHIP_ALDEBARAN) {
510 if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
511 DRM_WARN("Failed to reset error counter and error status");
514 return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count,
515 "ce", info.ce_count);
520 #define get_obj(obj) do { (obj)->use++; } while (0)
521 #define alive_obj(obj) ((obj)->use)
523 static inline void put_obj(struct ras_manager *obj)
525 if (obj && (--obj->use == 0))
526 list_del(&obj->node);
527 if (obj && (obj->use < 0))
528 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
531 /* make one obj and return it. */
532 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
533 struct ras_common_if *head)
535 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
536 struct ras_manager *obj;
538 if (!adev->ras_enabled || !con)
541 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
544 obj = &con->objs[head->block];
545 /* already exist. return obj? */
551 list_add(&obj->node, &con->head);
557 /* return an obj equal to head, or the first when head is NULL */
558 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
559 struct ras_common_if *head)
561 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
562 struct ras_manager *obj;
565 if (!adev->ras_enabled || !con)
569 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
572 obj = &con->objs[head->block];
574 if (alive_obj(obj)) {
575 WARN_ON(head->block != obj->head.block);
579 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
581 if (alive_obj(obj)) {
582 WARN_ON(i != obj->head.block);
592 /* feature ctl begin */
593 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
594 struct ras_common_if *head)
596 return adev->ras_hw_enabled & BIT(head->block);
599 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
600 struct ras_common_if *head)
602 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
604 return con->features & BIT(head->block);
608 * if obj is not created, then create one.
609 * set feature enable flag.
611 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
612 struct ras_common_if *head, int enable)
614 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
615 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
617 /* If hardware does not support ras, then do not create obj.
618 * But if hardware support ras, we can create the obj.
619 * Ras framework checks con->hw_supported to see if it need do
620 * corresponding initialization.
621 * IP checks con->support to see if it need disable ras.
623 if (!amdgpu_ras_is_feature_allowed(adev, head))
625 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
630 obj = amdgpu_ras_create_obj(adev, head);
634 /* In case we create obj somewhere else */
637 con->features |= BIT(head->block);
639 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
640 con->features &= ~BIT(head->block);
648 /* wrapper of psp_ras_enable_features */
649 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
650 struct ras_common_if *head, bool enable)
652 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
653 union ta_ras_cmd_input *info;
659 info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
664 info->disable_features = (struct ta_ras_disable_features_input) {
665 .block_id = amdgpu_ras_block_to_ta(head->block),
666 .error_type = amdgpu_ras_error_to_ta(head->type),
669 info->enable_features = (struct ta_ras_enable_features_input) {
670 .block_id = amdgpu_ras_block_to_ta(head->block),
671 .error_type = amdgpu_ras_error_to_ta(head->type),
675 /* Do not enable if it is not allowed. */
676 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
677 /* Are we alerady in that state we are going to set? */
678 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head))) {
683 if (!amdgpu_ras_intr_triggered()) {
684 ret = psp_ras_enable_features(&adev->psp, info, enable);
686 dev_err(adev->dev, "ras %s %s failed %d\n",
687 enable ? "enable":"disable",
688 ras_block_str(head->block),
695 __amdgpu_ras_feature_enable(adev, head, enable);
702 /* Only used in device probe stage and called only once. */
703 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
704 struct ras_common_if *head, bool enable)
706 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
712 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
714 /* There is no harm to issue a ras TA cmd regardless of
715 * the currecnt ras state.
716 * If current state == target state, it will do nothing
717 * But sometimes it requests driver to reset and repost
718 * with error code -EAGAIN.
720 ret = amdgpu_ras_feature_enable(adev, head, 1);
721 /* With old ras TA, we might fail to enable ras.
722 * Log it and just setup the object.
723 * TODO need remove this WA in the future.
725 if (ret == -EINVAL) {
726 ret = __amdgpu_ras_feature_enable(adev, head, 1);
729 "RAS INFO: %s setup object\n",
730 ras_block_str(head->block));
733 /* setup the object then issue a ras TA disable cmd.*/
734 ret = __amdgpu_ras_feature_enable(adev, head, 1);
738 /* gfx block ras dsiable cmd must send to ras-ta */
739 if (head->block == AMDGPU_RAS_BLOCK__GFX)
740 con->features |= BIT(head->block);
742 ret = amdgpu_ras_feature_enable(adev, head, 0);
744 /* clean gfx block ras features flag */
745 if (adev->ras_enabled && head->block == AMDGPU_RAS_BLOCK__GFX)
746 con->features &= ~BIT(head->block);
749 ret = amdgpu_ras_feature_enable(adev, head, enable);
754 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
757 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
758 struct ras_manager *obj, *tmp;
760 list_for_each_entry_safe(obj, tmp, &con->head, node) {
762 * aka just release the obj and corresponding flags
765 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
768 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
773 return con->features;
776 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
779 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
780 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
782 const enum amdgpu_ras_error_type default_ras_type =
783 AMDGPU_RAS_ERROR__NONE;
785 for (i = 0; i < ras_block_count; i++) {
786 struct ras_common_if head = {
788 .type = default_ras_type,
789 .sub_block_index = 0,
791 strcpy(head.name, ras_block_str(i));
794 * bypass psp. vbios enable ras for us.
795 * so just create the obj
797 if (__amdgpu_ras_feature_enable(adev, &head, 1))
800 if (amdgpu_ras_feature_enable(adev, &head, 1))
805 return con->features;
807 /* feature ctl end */
809 /* query/inject/cure begin */
810 int amdgpu_ras_query_error_status(struct amdgpu_device *adev,
811 struct ras_query_if *info)
813 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
814 struct ras_err_data err_data = {0, 0, 0, NULL};
820 switch (info->head.block) {
821 case AMDGPU_RAS_BLOCK__UMC:
822 if (adev->umc.ras_funcs &&
823 adev->umc.ras_funcs->query_ras_error_count)
824 adev->umc.ras_funcs->query_ras_error_count(adev, &err_data);
825 /* umc query_ras_error_address is also responsible for clearing
828 if (adev->umc.ras_funcs &&
829 adev->umc.ras_funcs->query_ras_error_address)
830 adev->umc.ras_funcs->query_ras_error_address(adev, &err_data);
832 case AMDGPU_RAS_BLOCK__SDMA:
833 if (adev->sdma.funcs->query_ras_error_count) {
834 for (i = 0; i < adev->sdma.num_instances; i++)
835 adev->sdma.funcs->query_ras_error_count(adev, i,
839 case AMDGPU_RAS_BLOCK__GFX:
840 if (adev->gfx.ras_funcs &&
841 adev->gfx.ras_funcs->query_ras_error_count)
842 adev->gfx.ras_funcs->query_ras_error_count(adev, &err_data);
844 if (adev->gfx.ras_funcs &&
845 adev->gfx.ras_funcs->query_ras_error_status)
846 adev->gfx.ras_funcs->query_ras_error_status(adev);
848 case AMDGPU_RAS_BLOCK__MMHUB:
849 if (adev->mmhub.ras_funcs &&
850 adev->mmhub.ras_funcs->query_ras_error_count)
851 adev->mmhub.ras_funcs->query_ras_error_count(adev, &err_data);
853 if (adev->mmhub.ras_funcs &&
854 adev->mmhub.ras_funcs->query_ras_error_status)
855 adev->mmhub.ras_funcs->query_ras_error_status(adev);
857 case AMDGPU_RAS_BLOCK__PCIE_BIF:
858 if (adev->nbio.ras_funcs &&
859 adev->nbio.ras_funcs->query_ras_error_count)
860 adev->nbio.ras_funcs->query_ras_error_count(adev, &err_data);
862 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
863 if (adev->gmc.xgmi.ras_funcs &&
864 adev->gmc.xgmi.ras_funcs->query_ras_error_count)
865 adev->gmc.xgmi.ras_funcs->query_ras_error_count(adev, &err_data);
867 case AMDGPU_RAS_BLOCK__HDP:
868 if (adev->hdp.ras_funcs &&
869 adev->hdp.ras_funcs->query_ras_error_count)
870 adev->hdp.ras_funcs->query_ras_error_count(adev, &err_data);
876 obj->err_data.ue_count += err_data.ue_count;
877 obj->err_data.ce_count += err_data.ce_count;
879 info->ue_count = obj->err_data.ue_count;
880 info->ce_count = obj->err_data.ce_count;
882 if (err_data.ce_count) {
883 if (adev->smuio.funcs &&
884 adev->smuio.funcs->get_socket_id &&
885 adev->smuio.funcs->get_die_id) {
886 dev_info(adev->dev, "socket: %d, die: %d "
887 "%ld correctable hardware errors "
888 "detected in %s block, no user "
889 "action is needed.\n",
890 adev->smuio.funcs->get_socket_id(adev),
891 adev->smuio.funcs->get_die_id(adev),
892 obj->err_data.ce_count,
893 ras_block_str(info->head.block));
895 dev_info(adev->dev, "%ld correctable hardware errors "
896 "detected in %s block, no user "
897 "action is needed.\n",
898 obj->err_data.ce_count,
899 ras_block_str(info->head.block));
902 if (err_data.ue_count) {
903 if (adev->smuio.funcs &&
904 adev->smuio.funcs->get_socket_id &&
905 adev->smuio.funcs->get_die_id) {
906 dev_info(adev->dev, "socket: %d, die: %d "
907 "%ld uncorrectable hardware errors "
908 "detected in %s block\n",
909 adev->smuio.funcs->get_socket_id(adev),
910 adev->smuio.funcs->get_die_id(adev),
911 obj->err_data.ue_count,
912 ras_block_str(info->head.block));
914 dev_info(adev->dev, "%ld uncorrectable hardware errors "
915 "detected in %s block\n",
916 obj->err_data.ue_count,
917 ras_block_str(info->head.block));
924 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev,
925 enum amdgpu_ras_block block)
927 if (!amdgpu_ras_is_supported(adev, block))
931 case AMDGPU_RAS_BLOCK__GFX:
932 if (adev->gfx.ras_funcs &&
933 adev->gfx.ras_funcs->reset_ras_error_count)
934 adev->gfx.ras_funcs->reset_ras_error_count(adev);
936 if (adev->gfx.ras_funcs &&
937 adev->gfx.ras_funcs->reset_ras_error_status)
938 adev->gfx.ras_funcs->reset_ras_error_status(adev);
940 case AMDGPU_RAS_BLOCK__MMHUB:
941 if (adev->mmhub.ras_funcs &&
942 adev->mmhub.ras_funcs->reset_ras_error_count)
943 adev->mmhub.ras_funcs->reset_ras_error_count(adev);
945 if (adev->mmhub.ras_funcs &&
946 adev->mmhub.ras_funcs->reset_ras_error_status)
947 adev->mmhub.ras_funcs->reset_ras_error_status(adev);
949 case AMDGPU_RAS_BLOCK__SDMA:
950 if (adev->sdma.funcs->reset_ras_error_count)
951 adev->sdma.funcs->reset_ras_error_count(adev);
953 case AMDGPU_RAS_BLOCK__HDP:
954 if (adev->hdp.ras_funcs &&
955 adev->hdp.ras_funcs->reset_ras_error_count)
956 adev->hdp.ras_funcs->reset_ras_error_count(adev);
965 /* Trigger XGMI/WAFL error */
966 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev,
967 struct ta_ras_trigger_error_input *block_info)
971 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
972 dev_warn(adev->dev, "Failed to disallow df cstate");
974 if (amdgpu_dpm_allow_xgmi_power_down(adev, false))
975 dev_warn(adev->dev, "Failed to disallow XGMI power down");
977 ret = psp_ras_trigger_error(&adev->psp, block_info);
979 if (amdgpu_ras_intr_triggered())
982 if (amdgpu_dpm_allow_xgmi_power_down(adev, true))
983 dev_warn(adev->dev, "Failed to allow XGMI power down");
985 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW))
986 dev_warn(adev->dev, "Failed to allow df cstate");
991 /* wrapper of psp_ras_trigger_error */
992 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
993 struct ras_inject_if *info)
995 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
996 struct ta_ras_trigger_error_input block_info = {
997 .block_id = amdgpu_ras_block_to_ta(info->head.block),
998 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
999 .sub_block_index = info->head.sub_block_index,
1000 .address = info->address,
1001 .value = info->value,
1008 /* Calculate XGMI relative offset */
1009 if (adev->gmc.xgmi.num_physical_nodes > 1) {
1010 block_info.address =
1011 amdgpu_xgmi_get_relative_phy_addr(adev,
1012 block_info.address);
1015 switch (info->head.block) {
1016 case AMDGPU_RAS_BLOCK__GFX:
1017 if (adev->gfx.ras_funcs &&
1018 adev->gfx.ras_funcs->ras_error_inject)
1019 ret = adev->gfx.ras_funcs->ras_error_inject(adev, info);
1023 case AMDGPU_RAS_BLOCK__UMC:
1024 case AMDGPU_RAS_BLOCK__SDMA:
1025 case AMDGPU_RAS_BLOCK__MMHUB:
1026 case AMDGPU_RAS_BLOCK__PCIE_BIF:
1027 ret = psp_ras_trigger_error(&adev->psp, &block_info);
1029 case AMDGPU_RAS_BLOCK__XGMI_WAFL:
1030 ret = amdgpu_ras_error_inject_xgmi(adev, &block_info);
1033 dev_info(adev->dev, "%s error injection is not supported yet\n",
1034 ras_block_str(info->head.block));
1039 dev_err(adev->dev, "ras inject %s failed %d\n",
1040 ras_block_str(info->head.block), ret);
1045 /* get the total error counts on all IPs */
1046 unsigned long amdgpu_ras_query_error_count(struct amdgpu_device *adev,
1049 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1050 struct ras_manager *obj;
1051 struct ras_err_data data = {0, 0};
1053 if (!adev->ras_enabled || !con)
1056 list_for_each_entry(obj, &con->head, node) {
1057 struct ras_query_if info = {
1061 if (amdgpu_ras_query_error_status(adev, &info))
1064 data.ce_count += info.ce_count;
1065 data.ue_count += info.ue_count;
1068 return is_ce ? data.ce_count : data.ue_count;
1070 /* query/inject/cure end */
1075 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1076 struct ras_badpage **bps, unsigned int *count);
1078 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
1081 case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
1083 case AMDGPU_RAS_RETIRE_PAGE_PENDING:
1085 case AMDGPU_RAS_RETIRE_PAGE_FAULT:
1092 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
1094 * It allows user to read the bad pages of vram on the gpu through
1095 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
1097 * It outputs multiple lines, and each line stands for one gpu page.
1099 * The format of one line is below,
1100 * gpu pfn : gpu page size : flags
1102 * gpu pfn and gpu page size are printed in hex format.
1103 * flags can be one of below character,
1105 * R: reserved, this gpu page is reserved and not able to use.
1107 * P: pending for reserve, this gpu page is marked as bad, will be reserved
1108 * in next window of page_reserve.
1110 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
1114 * .. code-block:: bash
1116 * 0x00000001 : 0x00001000 : R
1117 * 0x00000002 : 0x00001000 : P
1121 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
1122 struct kobject *kobj, struct bin_attribute *attr,
1123 char *buf, loff_t ppos, size_t count)
1125 struct amdgpu_ras *con =
1126 container_of(attr, struct amdgpu_ras, badpages_attr);
1127 struct amdgpu_device *adev = con->adev;
1128 const unsigned int element_size =
1129 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
1130 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
1131 unsigned int end = div64_ul(ppos + count - 1, element_size);
1133 struct ras_badpage *bps = NULL;
1134 unsigned int bps_count = 0;
1136 memset(buf, 0, count);
1138 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1141 for (; start < end && start < bps_count; start++)
1142 s += scnprintf(&buf[s], element_size + 1,
1143 "0x%08x : 0x%08x : %1s\n",
1146 amdgpu_ras_badpage_flags_str(bps[start].flags));
1153 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1154 struct device_attribute *attr, char *buf)
1156 struct amdgpu_ras *con =
1157 container_of(attr, struct amdgpu_ras, features_attr);
1159 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
1162 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1164 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1166 sysfs_remove_file_from_group(&adev->dev->kobj,
1167 &con->badpages_attr.attr,
1171 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1173 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1174 struct attribute *attrs[] = {
1175 &con->features_attr.attr,
1178 struct attribute_group group = {
1179 .name = RAS_FS_NAME,
1183 sysfs_remove_group(&adev->dev->kobj, &group);
1188 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1189 struct ras_fs_if *head)
1191 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1193 if (!obj || obj->attr_inuse)
1198 memcpy(obj->fs_data.sysfs_name,
1200 sizeof(obj->fs_data.sysfs_name));
1202 obj->sysfs_attr = (struct device_attribute){
1204 .name = obj->fs_data.sysfs_name,
1207 .show = amdgpu_ras_sysfs_read,
1209 sysfs_attr_init(&obj->sysfs_attr.attr);
1211 if (sysfs_add_file_to_group(&adev->dev->kobj,
1212 &obj->sysfs_attr.attr,
1218 obj->attr_inuse = 1;
1223 int amdgpu_ras_sysfs_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->attr_inuse)
1231 sysfs_remove_file_from_group(&adev->dev->kobj,
1232 &obj->sysfs_attr.attr,
1234 obj->attr_inuse = 0;
1240 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1242 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1243 struct ras_manager *obj, *tmp;
1245 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1246 amdgpu_ras_sysfs_remove(adev, &obj->head);
1249 if (amdgpu_bad_page_threshold != 0)
1250 amdgpu_ras_sysfs_remove_bad_page_node(adev);
1252 amdgpu_ras_sysfs_remove_feature_node(adev);
1259 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1261 * Normally when there is an uncorrectable error, the driver will reset
1262 * the GPU to recover. However, in the event of an unrecoverable error,
1263 * the driver provides an interface to reboot the system automatically
1266 * The following file in debugfs provides that interface:
1267 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1271 * .. code-block:: bash
1273 * echo true > .../ras/auto_reboot
1277 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1279 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1280 struct drm_minor *minor = adev_to_drm(adev)->primary;
1283 dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1284 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev,
1285 &amdgpu_ras_debugfs_ctrl_ops);
1286 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev,
1287 &amdgpu_ras_debugfs_eeprom_ops);
1288 debugfs_create_u32("bad_page_cnt_threshold", 0444, dir,
1289 &con->bad_page_cnt_threshold);
1290 debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled);
1291 debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled);
1294 * After one uncorrectable error happens, usually GPU recovery will
1295 * be scheduled. But due to the known problem in GPU recovery failing
1296 * to bring GPU back, below interface provides one direct way to
1297 * user to reboot system automatically in such case within
1298 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1299 * will never be called.
1301 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot);
1304 * User could set this not to clean up hardware's error count register
1305 * of RAS IPs during ras recovery.
1307 debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir,
1308 &con->disable_ras_err_cnt_harvest);
1312 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1313 struct ras_fs_if *head,
1316 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1323 memcpy(obj->fs_data.debugfs_name,
1325 sizeof(obj->fs_data.debugfs_name));
1327 debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir,
1328 obj, &amdgpu_ras_debugfs_ops);
1331 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1333 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1335 struct ras_manager *obj;
1336 struct ras_fs_if fs_info;
1339 * it won't be called in resume path, no need to check
1340 * suspend and gpu reset status
1342 if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con)
1345 dir = amdgpu_ras_debugfs_create_ctrl_node(adev);
1347 list_for_each_entry(obj, &con->head, node) {
1348 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1349 (obj->attr_inuse == 1)) {
1350 sprintf(fs_info.debugfs_name, "%s_err_inject",
1351 ras_block_str(obj->head.block));
1352 fs_info.head = obj->head;
1353 amdgpu_ras_debugfs_create(adev, &fs_info, dir);
1361 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1362 amdgpu_ras_sysfs_badpages_read, NULL, 0);
1363 static DEVICE_ATTR(features, S_IRUGO,
1364 amdgpu_ras_sysfs_features_read, NULL);
1365 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1367 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1368 struct attribute_group group = {
1369 .name = RAS_FS_NAME,
1371 struct attribute *attrs[] = {
1372 &con->features_attr.attr,
1375 struct bin_attribute *bin_attrs[] = {
1381 /* add features entry */
1382 con->features_attr = dev_attr_features;
1383 group.attrs = attrs;
1384 sysfs_attr_init(attrs[0]);
1386 if (amdgpu_bad_page_threshold != 0) {
1387 /* add bad_page_features entry */
1388 bin_attr_gpu_vram_bad_pages.private = NULL;
1389 con->badpages_attr = bin_attr_gpu_vram_bad_pages;
1390 bin_attrs[0] = &con->badpages_attr;
1391 group.bin_attrs = bin_attrs;
1392 sysfs_bin_attr_init(bin_attrs[0]);
1395 r = sysfs_create_group(&adev->dev->kobj, &group);
1397 dev_err(adev->dev, "Failed to create RAS sysfs group!");
1402 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1404 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1405 struct ras_manager *con_obj, *ip_obj, *tmp;
1407 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1408 list_for_each_entry_safe(con_obj, tmp, &con->head, node) {
1409 ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head);
1415 amdgpu_ras_sysfs_remove_all(adev);
1421 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1423 struct ras_ih_data *data = &obj->ih_data;
1424 struct amdgpu_iv_entry entry;
1426 struct ras_err_data err_data = {0, 0, 0, NULL};
1428 while (data->rptr != data->wptr) {
1430 memcpy(&entry, &data->ring[data->rptr],
1431 data->element_size);
1434 data->rptr = (data->aligned_element_size +
1435 data->rptr) % data->ring_size;
1437 /* Let IP handle its data, maybe we need get the output
1438 * from the callback to udpate the error type/count, etc
1441 ret = data->cb(obj->adev, &err_data, &entry);
1442 /* ue will trigger an interrupt, and in that case
1443 * we need do a reset to recovery the whole system.
1444 * But leave IP do that recovery, here we just dispatch
1447 if (ret == AMDGPU_RAS_SUCCESS) {
1448 /* these counts could be left as 0 if
1449 * some blocks do not count error number
1451 obj->err_data.ue_count += err_data.ue_count;
1452 obj->err_data.ce_count += err_data.ce_count;
1458 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1460 struct ras_ih_data *data =
1461 container_of(work, struct ras_ih_data, ih_work);
1462 struct ras_manager *obj =
1463 container_of(data, struct ras_manager, ih_data);
1465 amdgpu_ras_interrupt_handler(obj);
1468 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1469 struct ras_dispatch_if *info)
1471 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1472 struct ras_ih_data *data = &obj->ih_data;
1477 if (data->inuse == 0)
1480 /* Might be overflow... */
1481 memcpy(&data->ring[data->wptr], info->entry,
1482 data->element_size);
1485 data->wptr = (data->aligned_element_size +
1486 data->wptr) % data->ring_size;
1488 schedule_work(&data->ih_work);
1493 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1494 struct ras_ih_if *info)
1496 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1497 struct ras_ih_data *data;
1502 data = &obj->ih_data;
1503 if (data->inuse == 0)
1506 cancel_work_sync(&data->ih_work);
1509 memset(data, 0, sizeof(*data));
1515 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1516 struct ras_ih_if *info)
1518 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1519 struct ras_ih_data *data;
1522 /* in case we registe the IH before enable ras feature */
1523 obj = amdgpu_ras_create_obj(adev, &info->head);
1529 data = &obj->ih_data;
1530 /* add the callback.etc */
1531 *data = (struct ras_ih_data) {
1534 .element_size = sizeof(struct amdgpu_iv_entry),
1539 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1541 data->aligned_element_size = ALIGN(data->element_size, 8);
1542 /* the ring can store 64 iv entries. */
1543 data->ring_size = 64 * data->aligned_element_size;
1544 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1556 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1558 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1559 struct ras_manager *obj, *tmp;
1561 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1562 struct ras_ih_if info = {
1565 amdgpu_ras_interrupt_remove_handler(adev, &info);
1572 /* traversal all IPs except NBIO to query error counter */
1573 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1575 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1576 struct ras_manager *obj;
1578 if (!adev->ras_enabled || !con)
1581 list_for_each_entry(obj, &con->head, node) {
1582 struct ras_query_if info = {
1587 * PCIE_BIF IP has one different isr by ras controller
1588 * interrupt, the specific ras counter query will be
1589 * done in that isr. So skip such block from common
1590 * sync flood interrupt isr calling.
1592 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1595 amdgpu_ras_query_error_status(adev, &info);
1599 /* Parse RdRspStatus and WrRspStatus */
1600 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
1601 struct ras_query_if *info)
1604 * Only two block need to query read/write
1605 * RspStatus at current state
1607 switch (info->head.block) {
1608 case AMDGPU_RAS_BLOCK__GFX:
1609 if (adev->gfx.ras_funcs &&
1610 adev->gfx.ras_funcs->query_ras_error_status)
1611 adev->gfx.ras_funcs->query_ras_error_status(adev);
1613 case AMDGPU_RAS_BLOCK__MMHUB:
1614 if (adev->mmhub.ras_funcs &&
1615 adev->mmhub.ras_funcs->query_ras_error_status)
1616 adev->mmhub.ras_funcs->query_ras_error_status(adev);
1623 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
1625 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1626 struct ras_manager *obj;
1628 if (!adev->ras_enabled || !con)
1631 list_for_each_entry(obj, &con->head, node) {
1632 struct ras_query_if info = {
1636 amdgpu_ras_error_status_query(adev, &info);
1640 /* recovery begin */
1642 /* return 0 on success.
1643 * caller need free bps.
1645 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1646 struct ras_badpage **bps, unsigned int *count)
1648 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1649 struct ras_err_handler_data *data;
1651 int ret = 0, status;
1653 if (!con || !con->eh_data || !bps || !count)
1656 mutex_lock(&con->recovery_lock);
1657 data = con->eh_data;
1658 if (!data || data->count == 0) {
1664 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1670 for (; i < data->count; i++) {
1671 (*bps)[i] = (struct ras_badpage){
1672 .bp = data->bps[i].retired_page,
1673 .size = AMDGPU_GPU_PAGE_SIZE,
1674 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1676 status = amdgpu_vram_mgr_query_page_status(
1677 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1678 data->bps[i].retired_page);
1679 if (status == -EBUSY)
1680 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1681 else if (status == -ENOENT)
1682 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1685 *count = data->count;
1687 mutex_unlock(&con->recovery_lock);
1691 static void amdgpu_ras_do_recovery(struct work_struct *work)
1693 struct amdgpu_ras *ras =
1694 container_of(work, struct amdgpu_ras, recovery_work);
1695 struct amdgpu_device *remote_adev = NULL;
1696 struct amdgpu_device *adev = ras->adev;
1697 struct list_head device_list, *device_list_handle = NULL;
1699 if (!ras->disable_ras_err_cnt_harvest) {
1700 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
1702 /* Build list of devices to query RAS related errors */
1703 if (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
1704 device_list_handle = &hive->device_list;
1706 INIT_LIST_HEAD(&device_list);
1707 list_add_tail(&adev->gmc.xgmi.head, &device_list);
1708 device_list_handle = &device_list;
1711 list_for_each_entry(remote_adev,
1712 device_list_handle, gmc.xgmi.head) {
1713 amdgpu_ras_query_err_status(remote_adev);
1714 amdgpu_ras_log_on_err_counter(remote_adev);
1717 amdgpu_put_xgmi_hive(hive);
1720 if (amdgpu_device_should_recover_gpu(ras->adev))
1721 amdgpu_device_gpu_recover(ras->adev, NULL);
1722 atomic_set(&ras->in_recovery, 0);
1725 /* alloc/realloc bps array */
1726 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1727 struct ras_err_handler_data *data, int pages)
1729 unsigned int old_space = data->count + data->space_left;
1730 unsigned int new_space = old_space + pages;
1731 unsigned int align_space = ALIGN(new_space, 512);
1732 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1740 memcpy(bps, data->bps,
1741 data->count * sizeof(*data->bps));
1746 data->space_left += align_space - old_space;
1750 /* it deal with vram only. */
1751 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1752 struct eeprom_table_record *bps, int pages)
1754 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1755 struct ras_err_handler_data *data;
1759 if (!con || !con->eh_data || !bps || pages <= 0)
1762 mutex_lock(&con->recovery_lock);
1763 data = con->eh_data;
1767 for (i = 0; i < pages; i++) {
1768 if (amdgpu_ras_check_bad_page_unlock(con,
1769 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT))
1772 if (!data->space_left &&
1773 amdgpu_ras_realloc_eh_data_space(adev, data, 256)) {
1778 amdgpu_vram_mgr_reserve_range(
1779 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM),
1780 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT,
1781 AMDGPU_GPU_PAGE_SIZE);
1783 memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps));
1788 mutex_unlock(&con->recovery_lock);
1794 * write error record array to eeprom, the function should be
1795 * protected by recovery_lock
1797 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1799 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1800 struct ras_err_handler_data *data;
1801 struct amdgpu_ras_eeprom_control *control;
1804 if (!con || !con->eh_data)
1807 control = &con->eeprom_control;
1808 data = con->eh_data;
1809 save_count = data->count - control->num_recs;
1810 /* only new entries are saved */
1811 if (save_count > 0) {
1812 if (amdgpu_ras_eeprom_process_recods(control,
1813 &data->bps[control->num_recs],
1816 dev_err(adev->dev, "Failed to save EEPROM table data!");
1820 dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
1827 * read error record array in eeprom and reserve enough space for
1828 * storing new bad pages
1830 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1832 struct amdgpu_ras_eeprom_control *control =
1833 &adev->psp.ras.ras->eeprom_control;
1834 struct eeprom_table_record *bps = NULL;
1837 /* no bad page record, skip eeprom access */
1838 if (!control->num_recs || (amdgpu_bad_page_threshold == 0))
1841 bps = kcalloc(control->num_recs, sizeof(*bps), GFP_KERNEL);
1845 if (amdgpu_ras_eeprom_process_recods(control, bps, false,
1846 control->num_recs)) {
1847 dev_err(adev->dev, "Failed to load EEPROM table records!");
1852 ret = amdgpu_ras_add_bad_pages(adev, bps, control->num_recs);
1859 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
1862 struct ras_err_handler_data *data = con->eh_data;
1865 addr >>= AMDGPU_GPU_PAGE_SHIFT;
1866 for (i = 0; i < data->count; i++)
1867 if (addr == data->bps[i].retired_page)
1874 * check if an address belongs to bad page
1876 * Note: this check is only for umc block
1878 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
1881 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1884 if (!con || !con->eh_data)
1887 mutex_lock(&con->recovery_lock);
1888 ret = amdgpu_ras_check_bad_page_unlock(con, addr);
1889 mutex_unlock(&con->recovery_lock);
1893 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
1894 uint32_t max_length)
1896 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1897 int tmp_threshold = amdgpu_bad_page_threshold;
1901 * Justification of value bad_page_cnt_threshold in ras structure
1903 * Generally, -1 <= amdgpu_bad_page_threshold <= max record length
1904 * in eeprom, and introduce two scenarios accordingly.
1906 * Bad page retirement enablement:
1907 * - If amdgpu_bad_page_threshold = -1,
1908 * bad_page_cnt_threshold = typical value by formula.
1910 * - When the value from user is 0 < amdgpu_bad_page_threshold <
1911 * max record length in eeprom, use it directly.
1913 * Bad page retirement disablement:
1914 * - If amdgpu_bad_page_threshold = 0, bad page retirement
1915 * functionality is disabled, and bad_page_cnt_threshold will
1919 if (tmp_threshold < -1)
1921 else if (tmp_threshold > max_length)
1922 tmp_threshold = max_length;
1924 if (tmp_threshold == -1) {
1925 val = adev->gmc.mc_vram_size;
1926 do_div(val, RAS_BAD_PAGE_RATE);
1927 con->bad_page_cnt_threshold = min(lower_32_bits(val),
1930 con->bad_page_cnt_threshold = tmp_threshold;
1934 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1936 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1937 struct ras_err_handler_data **data;
1938 uint32_t max_eeprom_records_len = 0;
1939 bool exc_err_limit = false;
1942 if (adev->ras_enabled && con)
1943 data = &con->eh_data;
1947 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
1953 mutex_init(&con->recovery_lock);
1954 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1955 atomic_set(&con->in_recovery, 0);
1958 max_eeprom_records_len = amdgpu_ras_eeprom_get_record_max_length();
1959 amdgpu_ras_validate_threshold(adev, max_eeprom_records_len);
1961 /* Todo: During test the SMU might fail to read the eeprom through I2C
1962 * when the GPU is pending on XGMI reset during probe time
1963 * (Mostly after second bus reset), skip it now
1965 if (adev->gmc.xgmi.pending_reset)
1967 ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit);
1969 * This calling fails when exc_err_limit is true or
1972 if (exc_err_limit || ret)
1975 if (con->eeprom_control.num_recs) {
1976 ret = amdgpu_ras_load_bad_pages(adev);
1984 kfree((*data)->bps);
1986 con->eh_data = NULL;
1988 dev_warn(adev->dev, "Failed to initialize ras recovery!\n");
1991 * Except error threshold exceeding case, other failure cases in this
1992 * function would not fail amdgpu driver init.
2002 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
2004 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2005 struct ras_err_handler_data *data = con->eh_data;
2007 /* recovery_init failed to init it, fini is useless */
2011 cancel_work_sync(&con->recovery_work);
2013 mutex_lock(&con->recovery_lock);
2014 con->eh_data = NULL;
2017 mutex_unlock(&con->recovery_lock);
2023 /* return 0 if ras will reset gpu and repost.*/
2024 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
2027 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
2032 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
2036 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev)
2038 return adev->asic_type == CHIP_VEGA10 ||
2039 adev->asic_type == CHIP_VEGA20 ||
2040 adev->asic_type == CHIP_ARCTURUS ||
2041 adev->asic_type == CHIP_ALDEBARAN ||
2042 adev->asic_type == CHIP_SIENNA_CICHLID;
2046 * this is workaround for vega20 workstation sku,
2047 * force enable gfx ras, ignore vbios gfx ras flag
2048 * due to GC EDC can not write
2050 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev)
2052 struct atom_context *ctx = adev->mode_info.atom_context;
2057 if (strnstr(ctx->vbios_version, "D16406",
2058 sizeof(ctx->vbios_version)))
2059 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX);
2063 * check hardware's ras ability which will be saved in hw_supported.
2064 * if hardware does not support ras, we can skip some ras initializtion and
2065 * forbid some ras operations from IP.
2066 * if software itself, say boot parameter, limit the ras ability. We still
2067 * need allow IP do some limited operations, like disable. In such case,
2068 * we have to initialize ras as normal. but need check if operation is
2069 * allowed or not in each function.
2071 static void amdgpu_ras_check_supported(struct amdgpu_device *adev)
2073 adev->ras_hw_enabled = adev->ras_enabled = 0;
2075 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
2076 !amdgpu_ras_asic_supported(adev))
2079 if (!adev->gmc.xgmi.connected_to_cpu) {
2080 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
2081 dev_info(adev->dev, "MEM ECC is active.\n");
2082 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC |
2083 1 << AMDGPU_RAS_BLOCK__DF);
2085 dev_info(adev->dev, "MEM ECC is not presented.\n");
2088 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
2089 dev_info(adev->dev, "SRAM ECC is active.\n");
2090 adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
2091 1 << AMDGPU_RAS_BLOCK__DF);
2093 dev_info(adev->dev, "SRAM ECC is not presented.\n");
2096 /* driver only manages a few IP blocks RAS feature
2097 * when GPU is connected cpu through XGMI */
2098 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX |
2099 1 << AMDGPU_RAS_BLOCK__SDMA |
2100 1 << AMDGPU_RAS_BLOCK__MMHUB);
2103 amdgpu_ras_get_quirks(adev);
2105 /* hw_supported needs to be aligned with RAS block mask. */
2106 adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK;
2108 adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 :
2109 adev->ras_hw_enabled & amdgpu_ras_mask;
2112 int amdgpu_ras_init(struct amdgpu_device *adev)
2114 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2120 con = kmalloc(sizeof(struct amdgpu_ras) +
2121 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
2122 GFP_KERNEL|__GFP_ZERO);
2126 con->objs = (struct ras_manager *)(con + 1);
2128 amdgpu_ras_set_context(adev, con);
2130 amdgpu_ras_check_supported(adev);
2132 if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) {
2133 /* set gfx block ras context feature for VEGA20 Gaming
2134 * send ras disable cmd to ras ta during ras late init.
2136 if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) {
2137 con->features |= BIT(AMDGPU_RAS_BLOCK__GFX);
2147 INIT_LIST_HEAD(&con->head);
2148 /* Might need get this flag from vbios. */
2149 con->flags = RAS_DEFAULT_FLAGS;
2151 /* initialize nbio ras function ahead of any other
2152 * ras functions so hardware fatal error interrupt
2153 * can be enabled as early as possible */
2154 switch (adev->asic_type) {
2157 case CHIP_ALDEBARAN:
2158 if (!adev->gmc.xgmi.connected_to_cpu)
2159 adev->nbio.ras_funcs = &nbio_v7_4_ras_funcs;
2162 /* nbio ras is not available */
2166 if (adev->nbio.ras_funcs &&
2167 adev->nbio.ras_funcs->init_ras_controller_interrupt) {
2168 r = adev->nbio.ras_funcs->init_ras_controller_interrupt(adev);
2173 if (adev->nbio.ras_funcs &&
2174 adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt) {
2175 r = adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt(adev);
2180 if (amdgpu_ras_fs_init(adev)) {
2185 dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
2186 "hardware ability[%x] ras_mask[%x]\n",
2187 adev->ras_hw_enabled, adev->ras_enabled);
2191 amdgpu_ras_set_context(adev, NULL);
2197 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev)
2199 if (adev->gmc.xgmi.connected_to_cpu)
2204 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev,
2205 struct ras_common_if *ras_block)
2207 struct ras_query_if info = {
2211 if (!amdgpu_persistent_edc_harvesting_supported(adev))
2214 if (amdgpu_ras_query_error_status(adev, &info) != 0)
2215 DRM_WARN("RAS init harvest failure");
2217 if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0)
2218 DRM_WARN("RAS init harvest reset failure");
2223 /* helper function to handle common stuff in ip late init phase */
2224 int amdgpu_ras_late_init(struct amdgpu_device *adev,
2225 struct ras_common_if *ras_block,
2226 struct ras_fs_if *fs_info,
2227 struct ras_ih_if *ih_info)
2231 /* disable RAS feature per IP block if it is not supported */
2232 if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
2233 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
2237 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
2240 /* request gpu reset. will run again */
2241 amdgpu_ras_request_reset_on_boot(adev,
2244 } else if (adev->in_suspend || amdgpu_in_reset(adev)) {
2245 /* in resume phase, if fail to enable ras,
2246 * clean up all ras fs nodes, and disable ras */
2252 /* check for errors on warm reset edc persisant supported ASIC */
2253 amdgpu_persistent_edc_harvesting(adev, ras_block);
2255 /* in resume phase, no need to create ras fs node */
2256 if (adev->in_suspend || amdgpu_in_reset(adev))
2260 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
2265 r = amdgpu_ras_sysfs_create(adev, fs_info);
2271 amdgpu_ras_sysfs_remove(adev, ras_block);
2274 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2276 amdgpu_ras_feature_enable(adev, ras_block, 0);
2280 /* helper function to remove ras fs node and interrupt handler */
2281 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
2282 struct ras_common_if *ras_block,
2283 struct ras_ih_if *ih_info)
2285 if (!ras_block || !ih_info)
2288 amdgpu_ras_sysfs_remove(adev, ras_block);
2290 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
2291 amdgpu_ras_feature_enable(adev, ras_block, 0);
2294 /* do some init work after IP late init as dependence.
2295 * and it runs in resume/gpu reset/booting up cases.
2297 void amdgpu_ras_resume(struct amdgpu_device *adev)
2299 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2300 struct ras_manager *obj, *tmp;
2302 if (!adev->ras_enabled || !con) {
2303 /* clean ras context for VEGA20 Gaming after send ras disable cmd */
2304 amdgpu_release_ras_context(adev);
2309 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2310 /* Set up all other IPs which are not implemented. There is a
2311 * tricky thing that IP's actual ras error type should be
2312 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2313 * ERROR_NONE make sense anyway.
2315 amdgpu_ras_enable_all_features(adev, 1);
2317 /* We enable ras on all hw_supported block, but as boot
2318 * parameter might disable some of them and one or more IP has
2319 * not implemented yet. So we disable them on behalf.
2321 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2322 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2323 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2324 /* there should be no any reference. */
2325 WARN_ON(alive_obj(obj));
2330 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
2331 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
2332 /* setup ras obj state as disabled.
2333 * for init_by_vbios case.
2334 * if we want to enable ras, just enable it in a normal way.
2335 * If we want do disable it, need setup ras obj as enabled,
2336 * then issue another TA disable cmd.
2337 * See feature_enable_on_boot
2339 amdgpu_ras_disable_all_features(adev, 1);
2340 amdgpu_ras_reset_gpu(adev);
2344 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2346 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2348 if (!adev->ras_enabled || !con)
2351 amdgpu_ras_disable_all_features(adev, 0);
2352 /* Make sure all ras objects are disabled. */
2354 amdgpu_ras_disable_all_features(adev, 1);
2357 /* do some fini work before IP fini as dependence */
2358 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2360 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2362 if (!adev->ras_enabled || !con)
2365 /* Need disable ras on all IPs here before ip [hw/sw]fini */
2366 amdgpu_ras_disable_all_features(adev, 0);
2367 amdgpu_ras_recovery_fini(adev);
2371 int amdgpu_ras_fini(struct amdgpu_device *adev)
2373 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2375 if (!adev->ras_enabled || !con)
2378 amdgpu_ras_fs_fini(adev);
2379 amdgpu_ras_interrupt_remove_all(adev);
2381 WARN(con->features, "Feature mask is not cleared");
2384 amdgpu_ras_disable_all_features(adev, 1);
2386 amdgpu_ras_set_context(adev, NULL);
2392 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2394 amdgpu_ras_check_supported(adev);
2395 if (!adev->ras_hw_enabled)
2398 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2399 dev_info(adev->dev, "uncorrectable hardware error"
2400 "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2402 amdgpu_ras_reset_gpu(adev);
2406 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
2408 if (adev->asic_type == CHIP_VEGA20 &&
2409 adev->pm.fw_version <= 0x283400) {
2410 return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
2411 amdgpu_ras_intr_triggered();
2417 void amdgpu_release_ras_context(struct amdgpu_device *adev)
2419 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2424 if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) {
2425 con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX);
2426 amdgpu_ras_set_context(adev, NULL);