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>
28 #include "amdgpu_ras.h"
29 #include "amdgpu_atomfirmware.h"
32 /* interrupt bottom half */
33 struct work_struct ih_work;
39 unsigned int ring_size;
40 unsigned int element_size;
41 unsigned int aligned_element_size;
48 char debugfs_name[32];
52 unsigned long ue_count;
53 unsigned long ce_count;
56 struct ras_err_handler_data {
57 /* point to bad pages array */
62 /* the count of entries */
64 /* the space can place new entries */
66 /* last reserved entry's index + 1 */
71 struct ras_common_if head;
75 struct list_head node;
77 struct amdgpu_device *adev;
81 struct device_attribute sysfs_attr;
85 struct ras_fs_data fs_data;
88 struct ras_ih_data ih_data;
90 struct ras_err_data err_data;
99 const char *ras_error_string[] = {
102 "single_correctable",
103 "multi_uncorrectable",
107 const char *ras_block_string[] = {
124 #define ras_err_str(i) (ras_error_string[ffs(i)])
125 #define ras_block_str(i) (ras_block_string[i])
127 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
128 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET 2
129 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
131 static void amdgpu_ras_self_test(struct amdgpu_device *adev)
136 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
137 size_t size, loff_t *pos)
139 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
140 struct ras_query_if info = {
146 if (amdgpu_ras_error_query(obj->adev, &info))
149 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
151 "ce", info.ce_count);
156 s = min_t(u64, s, size);
159 if (copy_to_user(buf, &val[*pos], s))
167 static const struct file_operations amdgpu_ras_debugfs_ops = {
168 .owner = THIS_MODULE,
169 .read = amdgpu_ras_debugfs_read,
171 .llseek = default_llseek
174 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
178 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
180 if (strcmp(name, ras_block_str(i)) == 0)
186 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
187 const char __user *buf, size_t size,
188 loff_t *pos, struct ras_debug_if *data)
190 ssize_t s = min_t(u64, 64, size);
202 memset(str, 0, sizeof(str));
203 memset(data, 0, sizeof(*data));
205 if (copy_from_user(str, buf, s))
208 if (sscanf(str, "disable %32s", block_name) == 1)
210 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
212 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
214 else if (str[0] && str[1] && str[2] && str[3])
215 /* ascii string, but commands are not matched. */
219 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
222 data->head.block = block_id;
223 data->head.type = memcmp("ue", err, 2) == 0 ?
224 AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE :
225 AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
229 if (sscanf(str, "%*s %*s %*s %llu %llu",
230 &address, &value) != 2)
231 if (sscanf(str, "%*s %*s %*s 0x%llx 0x%llx",
232 &address, &value) != 2)
234 data->inject.address = address;
235 data->inject.value = value;
238 if (size < sizeof(*data))
241 if (copy_from_user(data, buf, sizeof(*data)))
248 * DOC: ras debugfs control interface
250 * It accepts struct ras_debug_if who has two members.
252 * First member: ras_debug_if::head or ras_debug_if::inject.
254 * head is used to indicate which IP block will be under control.
256 * head has four members, they are block, type, sub_block_index, name.
257 * block: which IP will be under control.
258 * type: what kind of error will be enabled/disabled/injected.
259 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
260 * name: the name of IP.
262 * inject has two more members than head, they are address, value.
263 * As their names indicate, inject operation will write the
264 * value to the address.
266 * Second member: struct ras_debug_if::op.
267 * It has three kinds of operations.
268 * 0: disable RAS on the block. Take ::head as its data.
269 * 1: enable RAS on the block. Take ::head as its data.
270 * 2: inject errors on the block. Take ::inject as its data.
272 * How to use the interface?
274 * copy the struct ras_debug_if in your codes and initialize it.
275 * write the struct to the control node.
278 * echo op block [error [address value]] > .../ras/ras_ctrl
279 * op: disable, enable, inject
280 * disable: only block is needed
281 * enable: block and error are needed
282 * inject: error, address, value are needed
283 * block: umc, smda, gfx, .........
284 * see ras_block_string[] for details
286 * ue: multi_uncorrectable
287 * ce: single_correctable
289 * here are some examples for bash commands,
290 * echo inject umc ue 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
291 * echo inject umc ce 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
292 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
294 * How to check the result?
296 * For disable/enable, please check ras features at
297 * /sys/class/drm/card[0/1/2...]/device/ras/features
299 * For inject, please check corresponding err count at
300 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
302 * NOTE: operation is only allowed on blocks which are supported.
303 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
305 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
306 size_t size, loff_t *pos)
308 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
309 struct ras_debug_if data;
312 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
316 if (!amdgpu_ras_is_supported(adev, data.head.block))
321 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
324 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
327 ret = amdgpu_ras_error_inject(adev, &data.inject);
340 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
341 .owner = THIS_MODULE,
343 .write = amdgpu_ras_debugfs_ctrl_write,
344 .llseek = default_llseek
347 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
348 struct device_attribute *attr, char *buf)
350 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
351 struct ras_query_if info = {
355 if (amdgpu_ras_error_query(obj->adev, &info))
358 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
360 "ce", info.ce_count);
365 #define get_obj(obj) do { (obj)->use++; } while (0)
366 #define alive_obj(obj) ((obj)->use)
368 static inline void put_obj(struct ras_manager *obj)
370 if (obj && --obj->use == 0)
371 list_del(&obj->node);
372 if (obj && obj->use < 0) {
373 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
377 /* make one obj and return it. */
378 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
379 struct ras_common_if *head)
381 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
382 struct ras_manager *obj;
387 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
390 obj = &con->objs[head->block];
391 /* already exist. return obj? */
397 list_add(&obj->node, &con->head);
403 /* return an obj equal to head, or the first when head is NULL */
404 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
405 struct ras_common_if *head)
407 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
408 struct ras_manager *obj;
415 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
418 obj = &con->objs[head->block];
420 if (alive_obj(obj)) {
421 WARN_ON(head->block != obj->head.block);
425 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
427 if (alive_obj(obj)) {
428 WARN_ON(i != obj->head.block);
438 /* feature ctl begin */
439 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
440 struct ras_common_if *head)
442 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
444 return con->hw_supported & BIT(head->block);
447 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
448 struct ras_common_if *head)
450 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
452 return con->features & BIT(head->block);
456 * if obj is not created, then create one.
457 * set feature enable flag.
459 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
460 struct ras_common_if *head, int enable)
462 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
463 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
465 /* If hardware does not support ras, then do not create obj.
466 * But if hardware support ras, we can create the obj.
467 * Ras framework checks con->hw_supported to see if it need do
468 * corresponding initialization.
469 * IP checks con->support to see if it need disable ras.
471 if (!amdgpu_ras_is_feature_allowed(adev, head))
473 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
478 obj = amdgpu_ras_create_obj(adev, head);
482 /* In case we create obj somewhere else */
485 con->features |= BIT(head->block);
487 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
488 con->features &= ~BIT(head->block);
496 /* wrapper of psp_ras_enable_features */
497 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
498 struct ras_common_if *head, bool enable)
500 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
501 union ta_ras_cmd_input info;
508 info.disable_features = (struct ta_ras_disable_features_input) {
509 .block_id = amdgpu_ras_block_to_ta(head->block),
510 .error_type = amdgpu_ras_error_to_ta(head->type),
513 info.enable_features = (struct ta_ras_enable_features_input) {
514 .block_id = amdgpu_ras_block_to_ta(head->block),
515 .error_type = amdgpu_ras_error_to_ta(head->type),
519 /* Do not enable if it is not allowed. */
520 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
521 /* Are we alerady in that state we are going to set? */
522 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
525 ret = psp_ras_enable_features(&adev->psp, &info, enable);
527 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
528 enable ? "enable":"disable",
529 ras_block_str(head->block),
531 if (ret == TA_RAS_STATUS__RESET_NEEDED)
537 __amdgpu_ras_feature_enable(adev, head, enable);
542 /* Only used in device probe stage and called only once. */
543 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
544 struct ras_common_if *head, bool enable)
546 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
552 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
554 /* There is no harm to issue a ras TA cmd regardless of
555 * the currecnt ras state.
556 * If current state == target state, it will do nothing
557 * But sometimes it requests driver to reset and repost
558 * with error code -EAGAIN.
560 ret = amdgpu_ras_feature_enable(adev, head, 1);
561 /* With old ras TA, we might fail to enable ras.
562 * Log it and just setup the object.
563 * TODO need remove this WA in the future.
565 if (ret == -EINVAL) {
566 ret = __amdgpu_ras_feature_enable(adev, head, 1);
568 DRM_INFO("RAS INFO: %s setup object\n",
569 ras_block_str(head->block));
572 /* setup the object then issue a ras TA disable cmd.*/
573 ret = __amdgpu_ras_feature_enable(adev, head, 1);
577 ret = amdgpu_ras_feature_enable(adev, head, 0);
580 ret = amdgpu_ras_feature_enable(adev, head, enable);
585 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
588 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
589 struct ras_manager *obj, *tmp;
591 list_for_each_entry_safe(obj, tmp, &con->head, node) {
593 * aka just release the obj and corresponding flags
596 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
599 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
604 return con->features;
607 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
610 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
611 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
613 const enum amdgpu_ras_error_type default_ras_type =
614 AMDGPU_RAS_ERROR__NONE;
616 for (i = 0; i < ras_block_count; i++) {
617 struct ras_common_if head = {
619 .type = default_ras_type,
620 .sub_block_index = 0,
622 strcpy(head.name, ras_block_str(i));
625 * bypass psp. vbios enable ras for us.
626 * so just create the obj
628 if (__amdgpu_ras_feature_enable(adev, &head, 1))
631 if (amdgpu_ras_feature_enable(adev, &head, 1))
636 return con->features;
638 /* feature ctl end */
640 /* query/inject/cure begin */
641 int amdgpu_ras_error_query(struct amdgpu_device *adev,
642 struct ras_query_if *info)
644 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
648 /* TODO might read the register to read the count */
650 info->ue_count = obj->err_data.ue_count;
651 info->ce_count = obj->err_data.ce_count;
656 /* wrapper of psp_ras_trigger_error */
657 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
658 struct ras_inject_if *info)
660 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
661 struct ta_ras_trigger_error_input block_info = {
662 .block_id = amdgpu_ras_block_to_ta(info->head.block),
663 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
664 .sub_block_index = info->head.sub_block_index,
665 .address = info->address,
666 .value = info->value,
673 ret = psp_ras_trigger_error(&adev->psp, &block_info);
675 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
676 ras_block_str(info->head.block),
682 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
683 struct ras_cure_if *info)
685 /* psp fw has no cure interface for now. */
689 /* get the total error counts on all IPs */
690 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
693 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
694 struct ras_manager *obj;
695 struct ras_err_data data = {0, 0};
700 list_for_each_entry(obj, &con->head, node) {
701 struct ras_query_if info = {
705 if (amdgpu_ras_error_query(adev, &info))
708 data.ce_count += info.ce_count;
709 data.ue_count += info.ue_count;
712 return is_ce ? data.ce_count : data.ue_count;
714 /* query/inject/cure end */
719 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
720 struct ras_badpage **bps, unsigned int *count);
722 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
736 * DOC: ras sysfs gpu_vram_bad_pages interface
738 * It allows user to read the bad pages of vram on the gpu through
739 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
741 * It outputs multiple lines, and each line stands for one gpu page.
743 * The format of one line is below,
744 * gpu pfn : gpu page size : flags
746 * gpu pfn and gpu page size are printed in hex format.
747 * flags can be one of below character,
748 * R: reserved, this gpu page is reserved and not able to use.
749 * P: pending for reserve, this gpu page is marked as bad, will be reserved
750 * in next window of page_reserve.
751 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
754 * 0x00000001 : 0x00001000 : R
755 * 0x00000002 : 0x00001000 : P
758 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
759 struct kobject *kobj, struct bin_attribute *attr,
760 char *buf, loff_t ppos, size_t count)
762 struct amdgpu_ras *con =
763 container_of(attr, struct amdgpu_ras, badpages_attr);
764 struct amdgpu_device *adev = con->adev;
765 const unsigned int element_size =
766 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
767 unsigned int start = (ppos + element_size - 1) / element_size;
768 unsigned int end = (ppos + count - 1) / element_size;
770 struct ras_badpage *bps = NULL;
771 unsigned int bps_count = 0;
773 memset(buf, 0, count);
775 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
778 for (; start < end && start < bps_count; start++)
779 s += scnprintf(&buf[s], element_size + 1,
780 "0x%08x : 0x%08x : %1s\n",
783 amdgpu_ras_badpage_flags_str(bps[start].flags));
790 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
791 struct device_attribute *attr, char *buf)
793 struct amdgpu_ras *con =
794 container_of(attr, struct amdgpu_ras, features_attr);
795 struct drm_device *ddev = dev_get_drvdata(dev);
796 struct amdgpu_device *adev = ddev->dev_private;
797 struct ras_common_if head;
798 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
801 struct ras_manager *obj;
803 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
805 for (i = 0; i < ras_block_count; i++) {
808 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
809 obj = amdgpu_ras_find_obj(adev, &head);
810 s += scnprintf(&buf[s], PAGE_SIZE - s,
813 ras_err_str(obj->head.type));
815 s += scnprintf(&buf[s], PAGE_SIZE - s,
823 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
825 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
826 struct attribute *attrs[] = {
827 &con->features_attr.attr,
830 struct bin_attribute *bin_attrs[] = {
834 struct attribute_group group = {
837 .bin_attrs = bin_attrs,
840 con->features_attr = (struct device_attribute) {
845 .show = amdgpu_ras_sysfs_features_read,
848 con->badpages_attr = (struct bin_attribute) {
850 .name = "gpu_vram_bad_pages",
855 .read = amdgpu_ras_sysfs_badpages_read,
858 sysfs_attr_init(attrs[0]);
859 sysfs_bin_attr_init(bin_attrs[0]);
861 return sysfs_create_group(&adev->dev->kobj, &group);
864 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
866 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
867 struct attribute *attrs[] = {
868 &con->features_attr.attr,
871 struct bin_attribute *bin_attrs[] = {
875 struct attribute_group group = {
878 .bin_attrs = bin_attrs,
881 sysfs_remove_group(&adev->dev->kobj, &group);
886 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
887 struct ras_fs_if *head)
889 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
891 if (!obj || obj->attr_inuse)
896 memcpy(obj->fs_data.sysfs_name,
898 sizeof(obj->fs_data.sysfs_name));
900 obj->sysfs_attr = (struct device_attribute){
902 .name = obj->fs_data.sysfs_name,
905 .show = amdgpu_ras_sysfs_read,
907 sysfs_attr_init(&obj->sysfs_attr.attr);
909 if (sysfs_add_file_to_group(&adev->dev->kobj,
910 &obj->sysfs_attr.attr,
921 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
922 struct ras_common_if *head)
924 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
926 if (!obj || !obj->attr_inuse)
929 sysfs_remove_file_from_group(&adev->dev->kobj,
930 &obj->sysfs_attr.attr,
938 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
940 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
941 struct ras_manager *obj, *tmp;
943 list_for_each_entry_safe(obj, tmp, &con->head, node) {
944 amdgpu_ras_sysfs_remove(adev, &obj->head);
947 amdgpu_ras_sysfs_remove_feature_node(adev);
954 static int amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
956 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
957 struct drm_minor *minor = adev->ddev->primary;
958 struct dentry *root = minor->debugfs_root, *dir;
961 dir = debugfs_create_dir("ras", root);
967 ent = debugfs_create_file("ras_ctrl",
968 S_IWUGO | S_IRUGO, con->dir,
969 adev, &amdgpu_ras_debugfs_ctrl_ops);
971 debugfs_remove(con->dir);
979 int amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
980 struct ras_fs_if *head)
982 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
983 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
986 if (!obj || obj->ent)
991 memcpy(obj->fs_data.debugfs_name,
993 sizeof(obj->fs_data.debugfs_name));
995 ent = debugfs_create_file(obj->fs_data.debugfs_name,
996 S_IWUGO | S_IRUGO, con->dir,
997 obj, &amdgpu_ras_debugfs_ops);
1007 int amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1008 struct ras_common_if *head)
1010 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1012 if (!obj || !obj->ent)
1015 debugfs_remove(obj->ent);
1022 static int amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1024 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1025 struct ras_manager *obj, *tmp;
1027 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1028 amdgpu_ras_debugfs_remove(adev, &obj->head);
1031 debugfs_remove(con->ent);
1032 debugfs_remove(con->dir);
1042 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1044 amdgpu_ras_sysfs_create_feature_node(adev);
1045 amdgpu_ras_debugfs_create_ctrl_node(adev);
1050 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1052 amdgpu_ras_debugfs_remove_all(adev);
1053 amdgpu_ras_sysfs_remove_all(adev);
1059 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1061 struct ras_ih_data *data = &obj->ih_data;
1062 struct amdgpu_iv_entry entry;
1065 while (data->rptr != data->wptr) {
1067 memcpy(&entry, &data->ring[data->rptr],
1068 data->element_size);
1071 data->rptr = (data->aligned_element_size +
1072 data->rptr) % data->ring_size;
1074 /* Let IP handle its data, maybe we need get the output
1075 * from the callback to udpate the error type/count, etc
1078 ret = data->cb(obj->adev, &entry);
1079 /* ue will trigger an interrupt, and in that case
1080 * we need do a reset to recovery the whole system.
1081 * But leave IP do that recovery, here we just dispatch
1084 if (ret == AMDGPU_RAS_UE) {
1085 obj->err_data.ue_count++;
1087 /* Might need get ce count by register, but not all IP
1088 * saves ce count, some IP just use one bit or two bits
1089 * to indicate ce happened.
1095 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1097 struct ras_ih_data *data =
1098 container_of(work, struct ras_ih_data, ih_work);
1099 struct ras_manager *obj =
1100 container_of(data, struct ras_manager, ih_data);
1102 amdgpu_ras_interrupt_handler(obj);
1105 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1106 struct ras_dispatch_if *info)
1108 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1109 struct ras_ih_data *data = &obj->ih_data;
1114 if (data->inuse == 0)
1117 /* Might be overflow... */
1118 memcpy(&data->ring[data->wptr], info->entry,
1119 data->element_size);
1122 data->wptr = (data->aligned_element_size +
1123 data->wptr) % data->ring_size;
1125 schedule_work(&data->ih_work);
1130 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1131 struct ras_ih_if *info)
1133 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1134 struct ras_ih_data *data;
1139 data = &obj->ih_data;
1140 if (data->inuse == 0)
1143 cancel_work_sync(&data->ih_work);
1146 memset(data, 0, sizeof(*data));
1152 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1153 struct ras_ih_if *info)
1155 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1156 struct ras_ih_data *data;
1159 /* in case we registe the IH before enable ras feature */
1160 obj = amdgpu_ras_create_obj(adev, &info->head);
1166 data = &obj->ih_data;
1167 /* add the callback.etc */
1168 *data = (struct ras_ih_data) {
1171 .element_size = sizeof(struct amdgpu_iv_entry),
1176 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1178 data->aligned_element_size = ALIGN(data->element_size, 8);
1179 /* the ring can store 64 iv entries. */
1180 data->ring_size = 64 * data->aligned_element_size;
1181 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1193 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1195 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1196 struct ras_manager *obj, *tmp;
1198 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1199 struct ras_ih_if info = {
1202 amdgpu_ras_interrupt_remove_handler(adev, &info);
1209 /* recovery begin */
1211 /* return 0 on success.
1212 * caller need free bps.
1214 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1215 struct ras_badpage **bps, unsigned int *count)
1217 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1218 struct ras_err_handler_data *data;
1222 if (!con || !con->eh_data || !bps || !count)
1225 mutex_lock(&con->recovery_lock);
1226 data = con->eh_data;
1227 if (!data || data->count == 0) {
1232 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1238 for (; i < data->count; i++) {
1239 (*bps)[i] = (struct ras_badpage){
1240 .bp = data->bps[i].bp,
1241 .size = AMDGPU_GPU_PAGE_SIZE,
1245 if (data->last_reserved <= i)
1246 (*bps)[i].flags = 1;
1247 else if (data->bps[i].bo == NULL)
1248 (*bps)[i].flags = 2;
1251 *count = data->count;
1253 mutex_unlock(&con->recovery_lock);
1257 static void amdgpu_ras_do_recovery(struct work_struct *work)
1259 struct amdgpu_ras *ras =
1260 container_of(work, struct amdgpu_ras, recovery_work);
1262 amdgpu_device_gpu_recover(ras->adev, 0);
1263 atomic_set(&ras->in_recovery, 0);
1266 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1267 struct amdgpu_bo **bo_ptr)
1269 /* no need to free it actually. */
1270 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1274 /* reserve vram with size@offset */
1275 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1276 uint64_t offset, uint64_t size,
1277 struct amdgpu_bo **bo_ptr)
1279 struct ttm_operation_ctx ctx = { false, false };
1280 struct amdgpu_bo_param bp;
1283 struct amdgpu_bo *bo;
1287 memset(&bp, 0, sizeof(bp));
1289 bp.byte_align = PAGE_SIZE;
1290 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1291 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1292 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1293 bp.type = ttm_bo_type_kernel;
1296 r = amdgpu_bo_create(adev, &bp, &bo);
1300 r = amdgpu_bo_reserve(bo, false);
1304 offset = ALIGN(offset, PAGE_SIZE);
1305 for (i = 0; i < bo->placement.num_placement; ++i) {
1306 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1307 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1310 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1311 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1315 r = amdgpu_bo_pin_restricted(bo,
1316 AMDGPU_GEM_DOMAIN_VRAM,
1325 amdgpu_bo_unreserve(bo);
1329 amdgpu_bo_unreserve(bo);
1331 amdgpu_bo_unref(&bo);
1335 /* alloc/realloc bps array */
1336 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1337 struct ras_err_handler_data *data, int pages)
1339 unsigned int old_space = data->count + data->space_left;
1340 unsigned int new_space = old_space + pages;
1341 unsigned int align_space = ALIGN(new_space, 1024);
1342 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1348 memcpy(tmp, data->bps,
1349 data->count * sizeof(*data->bps));
1354 data->space_left += align_space - old_space;
1358 /* it deal with vram only. */
1359 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1360 unsigned long *bps, int pages)
1362 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1363 struct ras_err_handler_data *data;
1367 if (!con || !con->eh_data || !bps || pages <= 0)
1370 mutex_lock(&con->recovery_lock);
1371 data = con->eh_data;
1375 if (data->space_left <= pages)
1376 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1382 data->bps[data->count++].bp = bps[i];
1384 data->space_left -= pages;
1386 mutex_unlock(&con->recovery_lock);
1391 /* called in gpu recovery/init */
1392 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1394 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1395 struct ras_err_handler_data *data;
1397 struct amdgpu_bo *bo;
1400 if (!con || !con->eh_data)
1403 mutex_lock(&con->recovery_lock);
1404 data = con->eh_data;
1407 /* reserve vram at driver post stage. */
1408 for (i = data->last_reserved; i < data->count; i++) {
1409 bp = data->bps[i].bp;
1411 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1413 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1415 data->bps[i].bo = bo;
1416 data->last_reserved = i + 1;
1419 mutex_unlock(&con->recovery_lock);
1423 /* called when driver unload */
1424 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1426 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1427 struct ras_err_handler_data *data;
1428 struct amdgpu_bo *bo;
1431 if (!con || !con->eh_data)
1434 mutex_lock(&con->recovery_lock);
1435 data = con->eh_data;
1439 for (i = data->last_reserved - 1; i >= 0; i--) {
1440 bo = data->bps[i].bo;
1442 amdgpu_ras_release_vram(adev, &bo);
1444 data->bps[i].bo = bo;
1445 data->last_reserved = i;
1448 mutex_unlock(&con->recovery_lock);
1452 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1455 * write the array to eeprom when SMU disabled.
1460 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1463 * read the array to eeprom when SMU disabled.
1468 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1470 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1471 struct ras_err_handler_data **data = &con->eh_data;
1473 *data = kmalloc(sizeof(**data),
1474 GFP_KERNEL|__GFP_ZERO);
1478 mutex_init(&con->recovery_lock);
1479 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1480 atomic_set(&con->in_recovery, 0);
1483 amdgpu_ras_load_bad_pages(adev);
1484 amdgpu_ras_reserve_bad_pages(adev);
1489 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1491 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1492 struct ras_err_handler_data *data = con->eh_data;
1494 cancel_work_sync(&con->recovery_work);
1495 amdgpu_ras_save_bad_pages(adev);
1496 amdgpu_ras_release_bad_pages(adev);
1498 mutex_lock(&con->recovery_lock);
1499 con->eh_data = NULL;
1502 mutex_unlock(&con->recovery_lock);
1508 /* return 0 if ras will reset gpu and repost.*/
1509 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1512 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1517 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1522 * check hardware's ras ability which will be saved in hw_supported.
1523 * if hardware does not support ras, we can skip some ras initializtion and
1524 * forbid some ras operations from IP.
1525 * if software itself, say boot parameter, limit the ras ability. We still
1526 * need allow IP do some limited operations, like disable. In such case,
1527 * we have to initialize ras as normal. but need check if operation is
1528 * allowed or not in each function.
1530 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1531 uint32_t *hw_supported, uint32_t *supported)
1536 if (amdgpu_sriov_vf(adev) ||
1537 adev->asic_type != CHIP_VEGA20)
1540 if (adev->is_atom_fw &&
1541 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1542 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1543 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1545 *supported = amdgpu_ras_enable == 0 ?
1546 0 : *hw_supported & amdgpu_ras_mask;
1549 int amdgpu_ras_init(struct amdgpu_device *adev)
1551 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1556 con = kmalloc(sizeof(struct amdgpu_ras) +
1557 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1558 GFP_KERNEL|__GFP_ZERO);
1562 con->objs = (struct ras_manager *)(con + 1);
1564 amdgpu_ras_set_context(adev, con);
1566 amdgpu_ras_check_supported(adev, &con->hw_supported,
1569 INIT_LIST_HEAD(&con->head);
1570 /* Might need get this flag from vbios. */
1571 con->flags = RAS_DEFAULT_FLAGS;
1573 if (amdgpu_ras_recovery_init(adev))
1576 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1578 if (amdgpu_ras_fs_init(adev))
1581 amdgpu_ras_self_test(adev);
1583 DRM_INFO("RAS INFO: ras initialized successfully, "
1584 "hardware ability[%x] ras_mask[%x]\n",
1585 con->hw_supported, con->supported);
1588 amdgpu_ras_recovery_fini(adev);
1590 amdgpu_ras_set_context(adev, NULL);
1596 /* do some init work after IP late init as dependence.
1598 * gpu reset will re-enable ras, need fint out one way to run it again.
1599 * for now, if a gpu reset happened, unless IP enable its ras, the ras state
1600 * will be showed as disabled.
1602 void amdgpu_ras_post_init(struct amdgpu_device *adev)
1604 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1605 struct ras_manager *obj, *tmp;
1610 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1611 /* Set up all other IPs which are not implemented. There is a
1612 * tricky thing that IP's actual ras error type should be
1613 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1614 * ERROR_NONE make sense anyway.
1616 amdgpu_ras_enable_all_features(adev, 1);
1618 /* We enable ras on all hw_supported block, but as boot
1619 * parameter might disable some of them and one or more IP has
1620 * not implemented yet. So we disable them on behalf.
1622 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1623 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1624 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1625 /* there should be no any reference. */
1626 WARN_ON(alive_obj(obj));
1631 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1632 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1633 /* setup ras obj state as disabled.
1634 * for init_by_vbios case.
1635 * if we want to enable ras, just enable it in a normal way.
1636 * If we want do disable it, need setup ras obj as enabled,
1637 * then issue another TA disable cmd.
1638 * See feature_enable_on_boot
1640 amdgpu_ras_disable_all_features(adev, 1);
1641 amdgpu_ras_reset_gpu(adev, 0);
1645 /* do some fini work before IP fini as dependence */
1646 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1648 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1653 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1654 amdgpu_ras_disable_all_features(adev, 0);
1655 amdgpu_ras_recovery_fini(adev);
1659 int amdgpu_ras_fini(struct amdgpu_device *adev)
1661 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1666 amdgpu_ras_fs_fini(adev);
1667 amdgpu_ras_interrupt_remove_all(adev);
1669 WARN(con->features, "Feature mask is not cleared");
1672 amdgpu_ras_disable_all_features(adev, 1);
1674 amdgpu_ras_set_context(adev, NULL);