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;
93 const char *ras_error_string[] = {
97 "multi_uncorrectable",
101 const char *ras_block_string[] = {
118 #define ras_err_str(i) (ras_error_string[ffs(i)])
119 #define ras_block_str(i) (ras_block_string[i])
121 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
122 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
124 static void amdgpu_ras_self_test(struct amdgpu_device *adev)
129 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
130 size_t size, loff_t *pos)
132 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
133 struct ras_query_if info = {
139 if (amdgpu_ras_error_query(obj->adev, &info))
142 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
144 "ce", info.ce_count);
149 s = min_t(u64, s, size);
152 if (copy_to_user(buf, &val[*pos], s))
160 static const struct file_operations amdgpu_ras_debugfs_ops = {
161 .owner = THIS_MODULE,
162 .read = amdgpu_ras_debugfs_read,
164 .llseek = default_llseek
167 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
171 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
173 if (strcmp(name, ras_block_str(i)) == 0)
179 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
180 const char __user *buf, size_t size,
181 loff_t *pos, struct ras_debug_if *data)
183 ssize_t s = min_t(u64, 64, size);
195 memset(str, 0, sizeof(str));
196 memset(data, 0, sizeof(*data));
198 if (copy_from_user(str, buf, s))
201 if (sscanf(str, "disable %32s", block_name) == 1)
203 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
205 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
207 else if (str[0] && str[1] && str[2] && str[3])
208 /* ascii string, but commands are not matched. */
212 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
215 data->head.block = block_id;
216 data->head.type = memcmp("ue", err, 2) == 0 ?
217 AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE :
218 AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
222 if (sscanf(str, "%*s %*s %*s %llu %llu",
223 &address, &value) != 2)
224 if (sscanf(str, "%*s %*s %*s 0x%llx 0x%llx",
225 &address, &value) != 2)
227 data->inject.address = address;
228 data->inject.value = value;
231 if (size < sizeof(*data))
234 if (copy_from_user(data, buf, sizeof(*data)))
241 * DOC: ras debugfs control interface
243 * It accepts struct ras_debug_if who has two members.
245 * First member: ras_debug_if::head or ras_debug_if::inject.
247 * head is used to indicate which IP block will be under control.
249 * head has four members, they are block, type, sub_block_index, name.
250 * block: which IP will be under control.
251 * type: what kind of error will be enabled/disabled/injected.
252 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
253 * name: the name of IP.
255 * inject has two more members than head, they are address, value.
256 * As their names indicate, inject operation will write the
257 * value to the address.
259 * Second member: struct ras_debug_if::op.
260 * It has three kinds of operations.
261 * 0: disable RAS on the block. Take ::head as its data.
262 * 1: enable RAS on the block. Take ::head as its data.
263 * 2: inject errors on the block. Take ::inject as its data.
265 * How to use the interface?
267 * copy the struct ras_debug_if in your codes and initialize it.
268 * write the struct to the control node.
271 * echo op block [error [address value]] > .../ras/ras_ctrl
272 * op: disable, enable, inject
273 * disable: only block is needed
274 * enable: block and error are needed
275 * inject: error, address, value are needed
276 * block: umc, smda, gfx, .........
277 * see ras_block_string[] for details
279 * ue: multi_uncorrectable
280 * ce: single_correctable
282 * here are some examples for bash commands,
283 * echo inject umc ue 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
284 * echo inject umc ce 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
285 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
287 * How to check the result?
289 * For disable/enable, please check ras features at
290 * /sys/class/drm/card[0/1/2...]/device/ras/features
292 * For inject, please check corresponding err count at
293 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
295 * NOTE: operation is only allowed on blocks which are supported.
296 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
298 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
299 size_t size, loff_t *pos)
301 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
302 struct ras_debug_if data;
305 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
309 if (!amdgpu_ras_is_supported(adev, data.head.block))
314 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
317 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
320 ret = amdgpu_ras_error_inject(adev, &data.inject);
333 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
334 .owner = THIS_MODULE,
336 .write = amdgpu_ras_debugfs_ctrl_write,
337 .llseek = default_llseek
340 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
341 struct device_attribute *attr, char *buf)
343 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
344 struct ras_query_if info = {
348 if (amdgpu_ras_error_query(obj->adev, &info))
351 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
353 "ce", info.ce_count);
358 #define get_obj(obj) do { (obj)->use++; } while (0)
359 #define alive_obj(obj) ((obj)->use)
361 static inline void put_obj(struct ras_manager *obj)
363 if (obj && --obj->use == 0)
364 list_del(&obj->node);
365 if (obj && obj->use < 0) {
366 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
370 /* make one obj and return it. */
371 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
372 struct ras_common_if *head)
374 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
375 struct ras_manager *obj;
380 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
383 obj = &con->objs[head->block];
384 /* already exist. return obj? */
390 list_add(&obj->node, &con->head);
396 /* return an obj equal to head, or the first when head is NULL */
397 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
398 struct ras_common_if *head)
400 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
401 struct ras_manager *obj;
408 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
411 obj = &con->objs[head->block];
413 if (alive_obj(obj)) {
414 WARN_ON(head->block != obj->head.block);
418 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
420 if (alive_obj(obj)) {
421 WARN_ON(i != obj->head.block);
431 /* feature ctl begin */
432 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
433 struct ras_common_if *head)
435 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
437 return con->hw_supported & BIT(head->block);
440 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
441 struct ras_common_if *head)
443 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
445 return con->features & BIT(head->block);
449 * if obj is not created, then create one.
450 * set feature enable flag.
452 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
453 struct ras_common_if *head, int enable)
455 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
456 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
458 /* If hardware does not support ras, then do not create obj.
459 * But if hardware support ras, we can create the obj.
460 * Ras framework checks con->hw_supported to see if it need do
461 * corresponding initialization.
462 * IP checks con->support to see if it need disable ras.
464 if (!amdgpu_ras_is_feature_allowed(adev, head))
466 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
471 obj = amdgpu_ras_create_obj(adev, head);
475 /* In case we create obj somewhere else */
478 con->features |= BIT(head->block);
480 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
481 con->features &= ~BIT(head->block);
489 /* wrapper of psp_ras_enable_features */
490 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
491 struct ras_common_if *head, bool enable)
493 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
494 union ta_ras_cmd_input info;
501 info.disable_features = (struct ta_ras_disable_features_input) {
502 .block_id = amdgpu_ras_block_to_ta(head->block),
503 .error_type = amdgpu_ras_error_to_ta(head->type),
506 info.enable_features = (struct ta_ras_enable_features_input) {
507 .block_id = amdgpu_ras_block_to_ta(head->block),
508 .error_type = amdgpu_ras_error_to_ta(head->type),
512 /* Do not enable if it is not allowed. */
513 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
514 /* Are we alerady in that state we are going to set? */
515 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
518 ret = psp_ras_enable_features(&adev->psp, &info, enable);
520 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
521 enable ? "enable":"disable",
522 ras_block_str(head->block),
528 __amdgpu_ras_feature_enable(adev, head, enable);
533 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
536 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
537 struct ras_manager *obj, *tmp;
539 list_for_each_entry_safe(obj, tmp, &con->head, node) {
541 * aka just release the obj and corresponding flags
544 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
547 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
552 return con->features;
555 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
558 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
559 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
562 for (i = 0; i < ras_block_count; i++) {
563 struct ras_common_if head = {
565 .type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE,
566 .sub_block_index = 0,
568 strcpy(head.name, ras_block_str(i));
571 * bypass psp. vbios enable ras for us.
572 * so just create the obj
574 if (__amdgpu_ras_feature_enable(adev, &head, 1))
577 if (amdgpu_ras_feature_enable(adev, &head, 1))
582 return con->features;
584 /* feature ctl end */
586 /* query/inject/cure begin */
587 int amdgpu_ras_error_query(struct amdgpu_device *adev,
588 struct ras_query_if *info)
590 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
594 /* TODO might read the register to read the count */
596 info->ue_count = obj->err_data.ue_count;
597 info->ce_count = obj->err_data.ce_count;
602 /* wrapper of psp_ras_trigger_error */
603 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
604 struct ras_inject_if *info)
606 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
607 struct ta_ras_trigger_error_input block_info = {
608 .block_id = amdgpu_ras_block_to_ta(info->head.block),
609 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
610 .sub_block_index = info->head.sub_block_index,
611 .address = info->address,
612 .value = info->value,
619 ret = psp_ras_trigger_error(&adev->psp, &block_info);
621 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
622 ras_block_str(info->head.block),
628 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
629 struct ras_cure_if *info)
631 /* psp fw has no cure interface for now. */
635 /* get the total error counts on all IPs */
636 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
639 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
640 struct ras_manager *obj;
641 struct ras_err_data data = {0, 0};
646 list_for_each_entry(obj, &con->head, node) {
647 struct ras_query_if info = {
651 if (amdgpu_ras_error_query(adev, &info))
654 data.ce_count += info.ce_count;
655 data.ue_count += info.ue_count;
658 return is_ce ? data.ce_count : data.ue_count;
660 /* query/inject/cure end */
665 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
666 struct device_attribute *attr, char *buf)
668 struct amdgpu_ras *con =
669 container_of(attr, struct amdgpu_ras, features_attr);
670 struct drm_device *ddev = dev_get_drvdata(dev);
671 struct amdgpu_device *adev = ddev->dev_private;
672 struct ras_common_if head;
673 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
676 struct ras_manager *obj;
678 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
680 for (i = 0; i < ras_block_count; i++) {
683 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
684 obj = amdgpu_ras_find_obj(adev, &head);
685 s += scnprintf(&buf[s], PAGE_SIZE - s,
688 ras_err_str(obj->head.type));
690 s += scnprintf(&buf[s], PAGE_SIZE - s,
698 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
700 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
701 struct attribute *attrs[] = {
702 &con->features_attr.attr,
705 struct attribute_group group = {
710 con->features_attr = (struct device_attribute) {
715 .show = amdgpu_ras_sysfs_features_read,
717 sysfs_attr_init(attrs[0]);
719 return sysfs_create_group(&adev->dev->kobj, &group);
722 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
724 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
725 struct attribute *attrs[] = {
726 &con->features_attr.attr,
729 struct attribute_group group = {
734 sysfs_remove_group(&adev->dev->kobj, &group);
739 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
740 struct ras_fs_if *head)
742 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
744 if (!obj || obj->attr_inuse)
749 memcpy(obj->fs_data.sysfs_name,
751 sizeof(obj->fs_data.sysfs_name));
753 obj->sysfs_attr = (struct device_attribute){
755 .name = obj->fs_data.sysfs_name,
758 .show = amdgpu_ras_sysfs_read,
760 sysfs_attr_init(&obj->sysfs_attr.attr);
762 if (sysfs_add_file_to_group(&adev->dev->kobj,
763 &obj->sysfs_attr.attr,
774 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
775 struct ras_common_if *head)
777 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
779 if (!obj || !obj->attr_inuse)
782 sysfs_remove_file_from_group(&adev->dev->kobj,
783 &obj->sysfs_attr.attr,
791 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
793 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
794 struct ras_manager *obj, *tmp;
796 list_for_each_entry_safe(obj, tmp, &con->head, node) {
797 amdgpu_ras_sysfs_remove(adev, &obj->head);
800 amdgpu_ras_sysfs_remove_feature_node(adev);
807 static int amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
809 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
810 struct drm_minor *minor = adev->ddev->primary;
811 struct dentry *root = minor->debugfs_root, *dir;
814 dir = debugfs_create_dir("ras", root);
820 ent = debugfs_create_file("ras_ctrl",
821 S_IWUGO | S_IRUGO, con->dir,
822 adev, &amdgpu_ras_debugfs_ctrl_ops);
824 debugfs_remove(con->dir);
832 int amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
833 struct ras_fs_if *head)
835 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
836 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
839 if (!obj || obj->ent)
844 memcpy(obj->fs_data.debugfs_name,
846 sizeof(obj->fs_data.debugfs_name));
848 ent = debugfs_create_file(obj->fs_data.debugfs_name,
849 S_IWUGO | S_IRUGO, con->dir,
850 obj, &amdgpu_ras_debugfs_ops);
860 int amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
861 struct ras_common_if *head)
863 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
865 if (!obj || !obj->ent)
868 debugfs_remove(obj->ent);
875 static int amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
877 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
878 struct ras_manager *obj, *tmp;
880 list_for_each_entry_safe(obj, tmp, &con->head, node) {
881 amdgpu_ras_debugfs_remove(adev, &obj->head);
884 debugfs_remove(con->ent);
885 debugfs_remove(con->dir);
895 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
897 amdgpu_ras_sysfs_create_feature_node(adev);
898 amdgpu_ras_debugfs_create_ctrl_node(adev);
903 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
905 amdgpu_ras_debugfs_remove_all(adev);
906 amdgpu_ras_sysfs_remove_all(adev);
912 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
914 struct ras_ih_data *data = &obj->ih_data;
915 struct amdgpu_iv_entry entry;
918 while (data->rptr != data->wptr) {
920 memcpy(&entry, &data->ring[data->rptr],
924 data->rptr = (data->aligned_element_size +
925 data->rptr) % data->ring_size;
927 /* Let IP handle its data, maybe we need get the output
928 * from the callback to udpate the error type/count, etc
931 ret = data->cb(obj->adev, &entry);
932 /* ue will trigger an interrupt, and in that case
933 * we need do a reset to recovery the whole system.
934 * But leave IP do that recovery, here we just dispatch
937 if (ret == AMDGPU_RAS_UE) {
938 obj->err_data.ue_count++;
940 /* Might need get ce count by register, but not all IP
941 * saves ce count, some IP just use one bit or two bits
942 * to indicate ce happened.
948 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
950 struct ras_ih_data *data =
951 container_of(work, struct ras_ih_data, ih_work);
952 struct ras_manager *obj =
953 container_of(data, struct ras_manager, ih_data);
955 amdgpu_ras_interrupt_handler(obj);
958 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
959 struct ras_dispatch_if *info)
961 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
962 struct ras_ih_data *data = &obj->ih_data;
967 if (data->inuse == 0)
970 /* Might be overflow... */
971 memcpy(&data->ring[data->wptr], info->entry,
975 data->wptr = (data->aligned_element_size +
976 data->wptr) % data->ring_size;
978 schedule_work(&data->ih_work);
983 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
984 struct ras_ih_if *info)
986 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
987 struct ras_ih_data *data;
992 data = &obj->ih_data;
993 if (data->inuse == 0)
996 cancel_work_sync(&data->ih_work);
999 memset(data, 0, sizeof(*data));
1005 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1006 struct ras_ih_if *info)
1008 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1009 struct ras_ih_data *data;
1012 /* in case we registe the IH before enable ras feature */
1013 obj = amdgpu_ras_create_obj(adev, &info->head);
1019 data = &obj->ih_data;
1020 /* add the callback.etc */
1021 *data = (struct ras_ih_data) {
1024 .element_size = sizeof(struct amdgpu_iv_entry),
1029 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1031 data->aligned_element_size = ALIGN(data->element_size, 8);
1032 /* the ring can store 64 iv entries. */
1033 data->ring_size = 64 * data->aligned_element_size;
1034 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1046 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1048 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1049 struct ras_manager *obj, *tmp;
1051 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1052 struct ras_ih_if info = {
1055 amdgpu_ras_interrupt_remove_handler(adev, &info);
1062 /* recovery begin */
1063 static void amdgpu_ras_do_recovery(struct work_struct *work)
1065 struct amdgpu_ras *ras =
1066 container_of(work, struct amdgpu_ras, recovery_work);
1068 amdgpu_device_gpu_recover(ras->adev, 0);
1069 atomic_set(&ras->in_recovery, 0);
1072 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1073 struct amdgpu_bo **bo_ptr)
1075 /* no need to free it actually. */
1076 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1080 /* reserve vram with size@offset */
1081 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1082 uint64_t offset, uint64_t size,
1083 struct amdgpu_bo **bo_ptr)
1085 struct ttm_operation_ctx ctx = { false, false };
1086 struct amdgpu_bo_param bp;
1089 struct amdgpu_bo *bo;
1093 memset(&bp, 0, sizeof(bp));
1095 bp.byte_align = PAGE_SIZE;
1096 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1097 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1098 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1099 bp.type = ttm_bo_type_kernel;
1102 r = amdgpu_bo_create(adev, &bp, &bo);
1106 r = amdgpu_bo_reserve(bo, false);
1110 offset = ALIGN(offset, PAGE_SIZE);
1111 for (i = 0; i < bo->placement.num_placement; ++i) {
1112 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1113 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1116 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1117 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1121 r = amdgpu_bo_pin_restricted(bo,
1122 AMDGPU_GEM_DOMAIN_VRAM,
1131 amdgpu_bo_unreserve(bo);
1135 amdgpu_bo_unreserve(bo);
1137 amdgpu_bo_unref(&bo);
1141 /* alloc/realloc bps array */
1142 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1143 struct ras_err_handler_data *data, int pages)
1145 unsigned int old_space = data->count + data->space_left;
1146 unsigned int new_space = old_space + pages;
1147 unsigned int align_space = ALIGN(new_space, 1024);
1148 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1154 memcpy(tmp, data->bps,
1155 data->count * sizeof(*data->bps));
1160 data->space_left += align_space - old_space;
1164 /* it deal with vram only. */
1165 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1166 unsigned long *bps, int pages)
1168 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1169 struct ras_err_handler_data *data;
1173 if (!con || !con->eh_data || !bps || pages <= 0)
1176 mutex_lock(&con->recovery_lock);
1177 data = con->eh_data;
1181 if (data->space_left <= pages)
1182 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1188 data->bps[data->count++].bp = bps[i];
1190 data->space_left -= pages;
1192 mutex_unlock(&con->recovery_lock);
1197 /* called in gpu recovery/init */
1198 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1200 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1201 struct ras_err_handler_data *data;
1203 struct amdgpu_bo *bo;
1206 if (!con || !con->eh_data)
1209 mutex_lock(&con->recovery_lock);
1210 data = con->eh_data;
1213 /* reserve vram at driver post stage. */
1214 for (i = data->last_reserved; i < data->count; i++) {
1215 bp = data->bps[i].bp;
1217 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1219 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1221 data->bps[i].bo = bo;
1222 data->last_reserved = i + 1;
1225 mutex_unlock(&con->recovery_lock);
1229 /* called when driver unload */
1230 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1232 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1233 struct ras_err_handler_data *data;
1234 struct amdgpu_bo *bo;
1237 if (!con || !con->eh_data)
1240 mutex_lock(&con->recovery_lock);
1241 data = con->eh_data;
1245 for (i = data->last_reserved - 1; i >= 0; i--) {
1246 bo = data->bps[i].bo;
1248 amdgpu_ras_release_vram(adev, &bo);
1250 data->bps[i].bo = bo;
1251 data->last_reserved = i;
1254 mutex_unlock(&con->recovery_lock);
1258 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1261 * write the array to eeprom when SMU disabled.
1266 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1269 * read the array to eeprom when SMU disabled.
1274 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1276 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1277 struct ras_err_handler_data **data = &con->eh_data;
1279 *data = kmalloc(sizeof(**data),
1280 GFP_KERNEL|__GFP_ZERO);
1284 mutex_init(&con->recovery_lock);
1285 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1286 atomic_set(&con->in_recovery, 0);
1289 amdgpu_ras_load_bad_pages(adev);
1290 amdgpu_ras_reserve_bad_pages(adev);
1295 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1297 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1298 struct ras_err_handler_data *data = con->eh_data;
1300 cancel_work_sync(&con->recovery_work);
1301 amdgpu_ras_save_bad_pages(adev);
1302 amdgpu_ras_release_bad_pages(adev);
1304 mutex_lock(&con->recovery_lock);
1305 con->eh_data = NULL;
1308 mutex_unlock(&con->recovery_lock);
1315 * check hardware's ras ability which will be saved in hw_supported.
1316 * if hardware does not support ras, we can skip some ras initializtion and
1317 * forbid some ras operations from IP.
1318 * if software itself, say boot parameter, limit the ras ability. We still
1319 * need allow IP do some limited operations, like disable. In such case,
1320 * we have to initialize ras as normal. but need check if operation is
1321 * allowed or not in each function.
1323 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1324 uint32_t *hw_supported, uint32_t *supported)
1329 if (amdgpu_sriov_vf(adev) ||
1330 adev->asic_type != CHIP_VEGA20)
1333 if (adev->is_atom_fw &&
1334 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1335 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1336 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1338 *supported = amdgpu_ras_enable == 0 ?
1339 0 : *hw_supported & amdgpu_ras_mask;
1342 int amdgpu_ras_init(struct amdgpu_device *adev)
1344 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1349 con = kmalloc(sizeof(struct amdgpu_ras) +
1350 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1351 GFP_KERNEL|__GFP_ZERO);
1355 con->objs = (struct ras_manager *)(con + 1);
1357 amdgpu_ras_set_context(adev, con);
1359 amdgpu_ras_check_supported(adev, &con->hw_supported,
1362 INIT_LIST_HEAD(&con->head);
1363 /* Might need get this flag from vbios. */
1364 con->flags = RAS_DEFAULT_FLAGS;
1366 if (amdgpu_ras_recovery_init(adev))
1369 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1371 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
1372 amdgpu_ras_enable_all_features(adev, 1);
1374 if (amdgpu_ras_fs_init(adev))
1377 amdgpu_ras_self_test(adev);
1379 DRM_INFO("RAS INFO: ras initialized successfully, "
1380 "hardware ability[%x] ras_mask[%x]\n",
1381 con->hw_supported, con->supported);
1384 amdgpu_ras_recovery_fini(adev);
1386 amdgpu_ras_set_context(adev, NULL);
1392 /* do some init work after IP late init as dependence */
1393 void amdgpu_ras_post_init(struct amdgpu_device *adev)
1395 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1396 struct ras_manager *obj, *tmp;
1401 /* We enable ras on all hw_supported block, but as boot parameter might
1402 * disable some of them and one or more IP has not implemented yet.
1403 * So we disable them on behalf.
1405 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1406 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1407 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1408 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1409 /* there should be no any reference. */
1410 WARN_ON(alive_obj(obj));
1416 /* do some fini work before IP fini as dependence */
1417 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1419 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1424 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1425 amdgpu_ras_disable_all_features(adev, 0);
1426 amdgpu_ras_recovery_fini(adev);
1430 int amdgpu_ras_fini(struct amdgpu_device *adev)
1432 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1437 amdgpu_ras_fs_fini(adev);
1438 amdgpu_ras_interrupt_remove_all(adev);
1440 WARN(con->features, "Feature mask is not cleared");
1443 amdgpu_ras_disable_all_features(adev, 1);
1445 amdgpu_ras_set_context(adev, NULL);