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>
30 #include "amdgpu_ras.h"
31 #include "amdgpu_atomfirmware.h"
33 const char *ras_error_string[] = {
37 "multi_uncorrectable",
41 const char *ras_block_string[] = {
58 #define ras_err_str(i) (ras_error_string[ffs(i)])
59 #define ras_block_str(i) (ras_block_string[i])
61 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
62 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET 2
63 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
65 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
66 uint64_t offset, uint64_t size,
67 struct amdgpu_bo **bo_ptr);
68 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
69 struct amdgpu_bo **bo_ptr);
71 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
72 size_t size, loff_t *pos)
74 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
75 struct ras_query_if info = {
81 if (amdgpu_ras_error_query(obj->adev, &info))
84 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
91 s = min_t(u64, s, size);
94 if (copy_to_user(buf, &val[*pos], s))
102 static const struct file_operations amdgpu_ras_debugfs_ops = {
103 .owner = THIS_MODULE,
104 .read = amdgpu_ras_debugfs_read,
106 .llseek = default_llseek
109 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
113 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
115 if (strcmp(name, ras_block_str(i)) == 0)
121 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
122 const char __user *buf, size_t size,
123 loff_t *pos, struct ras_debug_if *data)
125 ssize_t s = min_t(u64, 64, size);
137 memset(str, 0, sizeof(str));
138 memset(data, 0, sizeof(*data));
140 if (copy_from_user(str, buf, s))
143 if (sscanf(str, "disable %32s", block_name) == 1)
145 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
147 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
149 else if (str[0] && str[1] && str[2] && str[3])
150 /* ascii string, but commands are not matched. */
154 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
157 data->head.block = block_id;
158 data->head.type = memcmp("ue", err, 2) == 0 ?
159 AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE :
160 AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
164 if (sscanf(str, "%*s %*s %*s %llu %llu",
165 &address, &value) != 2)
166 if (sscanf(str, "%*s %*s %*s 0x%llx 0x%llx",
167 &address, &value) != 2)
169 data->inject.address = address;
170 data->inject.value = value;
173 if (size < sizeof(*data))
176 if (copy_from_user(data, buf, sizeof(*data)))
183 * DOC: AMDGPU RAS debugfs control interface
185 * It accepts struct ras_debug_if who has two members.
187 * First member: ras_debug_if::head or ras_debug_if::inject.
189 * head is used to indicate which IP block will be under control.
191 * head has four members, they are block, type, sub_block_index, name.
192 * block: which IP will be under control.
193 * type: what kind of error will be enabled/disabled/injected.
194 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
195 * name: the name of IP.
197 * inject has two more members than head, they are address, value.
198 * As their names indicate, inject operation will write the
199 * value to the address.
201 * Second member: struct ras_debug_if::op.
202 * It has three kinds of operations.
203 * 0: disable RAS on the block. Take ::head as its data.
204 * 1: enable RAS on the block. Take ::head as its data.
205 * 2: inject errors on the block. Take ::inject as its data.
207 * How to use the interface?
209 * copy the struct ras_debug_if in your codes and initialize it.
210 * write the struct to the control node.
213 * echo op block [error [address value]] > .../ras/ras_ctrl
214 * op: disable, enable, inject
215 * disable: only block is needed
216 * enable: block and error are needed
217 * inject: error, address, value are needed
218 * block: umc, smda, gfx, .........
219 * see ras_block_string[] for details
221 * ue: multi_uncorrectable
222 * ce: single_correctable
224 * here are some examples for bash commands,
225 * echo inject umc ue 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
226 * echo inject umc ce 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
227 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
229 * How to check the result?
231 * For disable/enable, please check ras features at
232 * /sys/class/drm/card[0/1/2...]/device/ras/features
234 * For inject, please check corresponding err count at
235 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
237 * NOTE: operation is only allowed on blocks which are supported.
238 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
240 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
241 size_t size, loff_t *pos)
243 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
244 struct ras_debug_if data;
245 struct amdgpu_bo *bo;
248 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
252 if (!amdgpu_ras_is_supported(adev, data.head.block))
257 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
260 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
263 ret = amdgpu_ras_reserve_vram(adev,
264 data.inject.address, PAGE_SIZE, &bo);
266 /* address was offset, now it is absolute.*/
267 data.inject.address += adev->gmc.vram_start;
268 if (data.inject.address > adev->gmc.vram_end)
271 data.inject.address = amdgpu_bo_gpu_offset(bo);
272 ret = amdgpu_ras_error_inject(adev, &data.inject);
273 amdgpu_ras_release_vram(adev, &bo);
286 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
287 .owner = THIS_MODULE,
289 .write = amdgpu_ras_debugfs_ctrl_write,
290 .llseek = default_llseek
293 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
297 struct ras_query_if info = {
301 if (amdgpu_ras_error_query(obj->adev, &info))
304 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
306 "ce", info.ce_count);
311 #define get_obj(obj) do { (obj)->use++; } while (0)
312 #define alive_obj(obj) ((obj)->use)
314 static inline void put_obj(struct ras_manager *obj)
316 if (obj && --obj->use == 0)
317 list_del(&obj->node);
318 if (obj && obj->use < 0) {
319 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
323 /* make one obj and return it. */
324 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
325 struct ras_common_if *head)
327 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
328 struct ras_manager *obj;
333 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
336 obj = &con->objs[head->block];
337 /* already exist. return obj? */
343 list_add(&obj->node, &con->head);
349 /* return an obj equal to head, or the first when head is NULL */
350 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
351 struct ras_common_if *head)
353 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
354 struct ras_manager *obj;
361 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
364 obj = &con->objs[head->block];
366 if (alive_obj(obj)) {
367 WARN_ON(head->block != obj->head.block);
371 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
373 if (alive_obj(obj)) {
374 WARN_ON(i != obj->head.block);
384 /* feature ctl begin */
385 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
386 struct ras_common_if *head)
388 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
390 return con->hw_supported & BIT(head->block);
393 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
394 struct ras_common_if *head)
396 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
398 return con->features & BIT(head->block);
402 * if obj is not created, then create one.
403 * set feature enable flag.
405 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
406 struct ras_common_if *head, int enable)
408 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
409 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
411 /* If hardware does not support ras, then do not create obj.
412 * But if hardware support ras, we can create the obj.
413 * Ras framework checks con->hw_supported to see if it need do
414 * corresponding initialization.
415 * IP checks con->support to see if it need disable ras.
417 if (!amdgpu_ras_is_feature_allowed(adev, head))
419 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
424 obj = amdgpu_ras_create_obj(adev, head);
428 /* In case we create obj somewhere else */
431 con->features |= BIT(head->block);
433 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
434 con->features &= ~BIT(head->block);
442 /* wrapper of psp_ras_enable_features */
443 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
444 struct ras_common_if *head, bool enable)
446 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
447 union ta_ras_cmd_input info;
454 info.disable_features = (struct ta_ras_disable_features_input) {
455 .block_id = amdgpu_ras_block_to_ta(head->block),
456 .error_type = amdgpu_ras_error_to_ta(head->type),
459 info.enable_features = (struct ta_ras_enable_features_input) {
460 .block_id = amdgpu_ras_block_to_ta(head->block),
461 .error_type = amdgpu_ras_error_to_ta(head->type),
465 /* Do not enable if it is not allowed. */
466 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
467 /* Are we alerady in that state we are going to set? */
468 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
471 ret = psp_ras_enable_features(&adev->psp, &info, enable);
473 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
474 enable ? "enable":"disable",
475 ras_block_str(head->block),
477 if (ret == TA_RAS_STATUS__RESET_NEEDED)
483 __amdgpu_ras_feature_enable(adev, head, enable);
488 /* Only used in device probe stage and called only once. */
489 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
490 struct ras_common_if *head, bool enable)
492 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
498 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
500 /* There is no harm to issue a ras TA cmd regardless of
501 * the currecnt ras state.
502 * If current state == target state, it will do nothing
503 * But sometimes it requests driver to reset and repost
504 * with error code -EAGAIN.
506 ret = amdgpu_ras_feature_enable(adev, head, 1);
507 /* With old ras TA, we might fail to enable ras.
508 * Log it and just setup the object.
509 * TODO need remove this WA in the future.
511 if (ret == -EINVAL) {
512 ret = __amdgpu_ras_feature_enable(adev, head, 1);
514 DRM_INFO("RAS INFO: %s setup object\n",
515 ras_block_str(head->block));
518 /* setup the object then issue a ras TA disable cmd.*/
519 ret = __amdgpu_ras_feature_enable(adev, head, 1);
523 ret = amdgpu_ras_feature_enable(adev, head, 0);
526 ret = amdgpu_ras_feature_enable(adev, head, enable);
531 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
534 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
535 struct ras_manager *obj, *tmp;
537 list_for_each_entry_safe(obj, tmp, &con->head, node) {
539 * aka just release the obj and corresponding flags
542 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
545 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
550 return con->features;
553 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
556 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
557 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
559 const enum amdgpu_ras_error_type default_ras_type =
560 AMDGPU_RAS_ERROR__NONE;
562 for (i = 0; i < ras_block_count; i++) {
563 struct ras_common_if head = {
565 .type = default_ras_type,
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);
591 struct ras_err_data err_data = {0, 0};
596 switch (info->head.block) {
597 case AMDGPU_RAS_BLOCK__UMC:
598 if (adev->umc.funcs->query_ras_error_count)
599 adev->umc.funcs->query_ras_error_count(adev, &err_data);
605 obj->err_data.ue_count += err_data.ue_count;
606 obj->err_data.ce_count += err_data.ce_count;
608 info->ue_count = obj->err_data.ue_count;
609 info->ce_count = obj->err_data.ce_count;
611 if (err_data.ce_count)
612 dev_info(adev->dev, "%ld correctable errors detected in %s block\n",
613 obj->err_data.ce_count, ras_block_str(info->head.block));
614 if (err_data.ue_count)
615 dev_info(adev->dev, "%ld uncorrectable errors detected in %s block\n",
616 obj->err_data.ue_count, ras_block_str(info->head.block));
621 /* wrapper of psp_ras_trigger_error */
622 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
623 struct ras_inject_if *info)
625 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
626 struct ta_ras_trigger_error_input block_info = {
627 .block_id = amdgpu_ras_block_to_ta(info->head.block),
628 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
629 .sub_block_index = info->head.sub_block_index,
630 .address = info->address,
631 .value = info->value,
638 if (block_info.block_id != TA_RAS_BLOCK__UMC) {
639 DRM_INFO("%s error injection is not supported yet\n",
640 ras_block_str(info->head.block));
644 ret = psp_ras_trigger_error(&adev->psp, &block_info);
646 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
647 ras_block_str(info->head.block),
653 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
654 struct ras_cure_if *info)
656 /* psp fw has no cure interface for now. */
660 /* get the total error counts on all IPs */
661 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
664 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
665 struct ras_manager *obj;
666 struct ras_err_data data = {0, 0};
671 list_for_each_entry(obj, &con->head, node) {
672 struct ras_query_if info = {
676 if (amdgpu_ras_error_query(adev, &info))
679 data.ce_count += info.ce_count;
680 data.ue_count += info.ue_count;
683 return is_ce ? data.ce_count : data.ue_count;
685 /* query/inject/cure end */
690 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
691 struct ras_badpage **bps, unsigned int *count);
693 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
707 * DOC: ras sysfs gpu_vram_bad_pages interface
709 * It allows user to read the bad pages of vram on the gpu through
710 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
712 * It outputs multiple lines, and each line stands for one gpu page.
714 * The format of one line is below,
715 * gpu pfn : gpu page size : flags
717 * gpu pfn and gpu page size are printed in hex format.
718 * flags can be one of below character,
719 * R: reserved, this gpu page is reserved and not able to use.
720 * P: pending for reserve, this gpu page is marked as bad, will be reserved
721 * in next window of page_reserve.
722 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
725 * 0x00000001 : 0x00001000 : R
726 * 0x00000002 : 0x00001000 : P
729 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
730 struct kobject *kobj, struct bin_attribute *attr,
731 char *buf, loff_t ppos, size_t count)
733 struct amdgpu_ras *con =
734 container_of(attr, struct amdgpu_ras, badpages_attr);
735 struct amdgpu_device *adev = con->adev;
736 const unsigned int element_size =
737 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
738 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
739 unsigned int end = div64_ul(ppos + count - 1, element_size);
741 struct ras_badpage *bps = NULL;
742 unsigned int bps_count = 0;
744 memset(buf, 0, count);
746 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
749 for (; start < end && start < bps_count; start++)
750 s += scnprintf(&buf[s], element_size + 1,
751 "0x%08x : 0x%08x : %1s\n",
754 amdgpu_ras_badpage_flags_str(bps[start].flags));
761 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
762 struct device_attribute *attr, char *buf)
764 struct amdgpu_ras *con =
765 container_of(attr, struct amdgpu_ras, features_attr);
766 struct drm_device *ddev = dev_get_drvdata(dev);
767 struct amdgpu_device *adev = ddev->dev_private;
768 struct ras_common_if head;
769 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
772 struct ras_manager *obj;
774 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
776 for (i = 0; i < ras_block_count; i++) {
779 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
780 obj = amdgpu_ras_find_obj(adev, &head);
781 s += scnprintf(&buf[s], PAGE_SIZE - s,
784 ras_err_str(obj->head.type));
786 s += scnprintf(&buf[s], PAGE_SIZE - s,
794 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
796 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
797 struct attribute *attrs[] = {
798 &con->features_attr.attr,
801 struct bin_attribute *bin_attrs[] = {
805 struct attribute_group group = {
808 .bin_attrs = bin_attrs,
811 con->features_attr = (struct device_attribute) {
816 .show = amdgpu_ras_sysfs_features_read,
819 con->badpages_attr = (struct bin_attribute) {
821 .name = "gpu_vram_bad_pages",
826 .read = amdgpu_ras_sysfs_badpages_read,
829 sysfs_attr_init(attrs[0]);
830 sysfs_bin_attr_init(bin_attrs[0]);
832 return sysfs_create_group(&adev->dev->kobj, &group);
835 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
837 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
838 struct attribute *attrs[] = {
839 &con->features_attr.attr,
842 struct bin_attribute *bin_attrs[] = {
846 struct attribute_group group = {
849 .bin_attrs = bin_attrs,
852 sysfs_remove_group(&adev->dev->kobj, &group);
857 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
858 struct ras_fs_if *head)
860 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
862 if (!obj || obj->attr_inuse)
867 memcpy(obj->fs_data.sysfs_name,
869 sizeof(obj->fs_data.sysfs_name));
871 obj->sysfs_attr = (struct device_attribute){
873 .name = obj->fs_data.sysfs_name,
876 .show = amdgpu_ras_sysfs_read,
878 sysfs_attr_init(&obj->sysfs_attr.attr);
880 if (sysfs_add_file_to_group(&adev->dev->kobj,
881 &obj->sysfs_attr.attr,
892 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
893 struct ras_common_if *head)
895 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
897 if (!obj || !obj->attr_inuse)
900 sysfs_remove_file_from_group(&adev->dev->kobj,
901 &obj->sysfs_attr.attr,
909 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
911 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
912 struct ras_manager *obj, *tmp;
914 list_for_each_entry_safe(obj, tmp, &con->head, node) {
915 amdgpu_ras_sysfs_remove(adev, &obj->head);
918 amdgpu_ras_sysfs_remove_feature_node(adev);
925 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
927 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
928 struct drm_minor *minor = adev->ddev->primary;
930 con->dir = debugfs_create_dir("ras", minor->debugfs_root);
931 con->ent = debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
932 adev, &amdgpu_ras_debugfs_ctrl_ops);
935 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
936 struct ras_fs_if *head)
938 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
939 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
941 if (!obj || obj->ent)
946 memcpy(obj->fs_data.debugfs_name,
948 sizeof(obj->fs_data.debugfs_name));
950 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
951 S_IWUGO | S_IRUGO, con->dir, obj,
952 &amdgpu_ras_debugfs_ops);
955 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
956 struct ras_common_if *head)
958 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
960 if (!obj || !obj->ent)
963 debugfs_remove(obj->ent);
968 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
970 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
971 struct ras_manager *obj, *tmp;
973 list_for_each_entry_safe(obj, tmp, &con->head, node) {
974 amdgpu_ras_debugfs_remove(adev, &obj->head);
977 debugfs_remove(con->ent);
978 debugfs_remove(con->dir);
986 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
988 amdgpu_ras_sysfs_create_feature_node(adev);
989 amdgpu_ras_debugfs_create_ctrl_node(adev);
994 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
996 amdgpu_ras_debugfs_remove_all(adev);
997 amdgpu_ras_sysfs_remove_all(adev);
1003 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1005 struct ras_ih_data *data = &obj->ih_data;
1006 struct amdgpu_iv_entry entry;
1008 struct ras_err_data err_data = {0, 0};
1010 while (data->rptr != data->wptr) {
1012 memcpy(&entry, &data->ring[data->rptr],
1013 data->element_size);
1016 data->rptr = (data->aligned_element_size +
1017 data->rptr) % data->ring_size;
1019 /* Let IP handle its data, maybe we need get the output
1020 * from the callback to udpate the error type/count, etc
1023 ret = data->cb(obj->adev, &entry);
1024 /* ue will trigger an interrupt, and in that case
1025 * we need do a reset to recovery the whole system.
1026 * But leave IP do that recovery, here we just dispatch
1029 if (ret == AMDGPU_RAS_UE) {
1030 obj->err_data.ue_count++;
1032 /* Might need get ce count by register, but not all IP
1033 * saves ce count, some IP just use one bit or two bits
1034 * to indicate ce happened.
1040 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1042 struct ras_ih_data *data =
1043 container_of(work, struct ras_ih_data, ih_work);
1044 struct ras_manager *obj =
1045 container_of(data, struct ras_manager, ih_data);
1047 amdgpu_ras_interrupt_handler(obj);
1050 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1051 struct ras_dispatch_if *info)
1053 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1054 struct ras_ih_data *data = &obj->ih_data;
1059 if (data->inuse == 0)
1062 /* Might be overflow... */
1063 memcpy(&data->ring[data->wptr], info->entry,
1064 data->element_size);
1067 data->wptr = (data->aligned_element_size +
1068 data->wptr) % data->ring_size;
1070 schedule_work(&data->ih_work);
1075 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1076 struct ras_ih_if *info)
1078 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1079 struct ras_ih_data *data;
1084 data = &obj->ih_data;
1085 if (data->inuse == 0)
1088 cancel_work_sync(&data->ih_work);
1091 memset(data, 0, sizeof(*data));
1097 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1098 struct ras_ih_if *info)
1100 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1101 struct ras_ih_data *data;
1104 /* in case we registe the IH before enable ras feature */
1105 obj = amdgpu_ras_create_obj(adev, &info->head);
1111 data = &obj->ih_data;
1112 /* add the callback.etc */
1113 *data = (struct ras_ih_data) {
1116 .element_size = sizeof(struct amdgpu_iv_entry),
1121 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1123 data->aligned_element_size = ALIGN(data->element_size, 8);
1124 /* the ring can store 64 iv entries. */
1125 data->ring_size = 64 * data->aligned_element_size;
1126 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1138 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1140 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1141 struct ras_manager *obj, *tmp;
1143 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1144 struct ras_ih_if info = {
1147 amdgpu_ras_interrupt_remove_handler(adev, &info);
1154 /* recovery begin */
1156 /* return 0 on success.
1157 * caller need free bps.
1159 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1160 struct ras_badpage **bps, unsigned int *count)
1162 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1163 struct ras_err_handler_data *data;
1167 if (!con || !con->eh_data || !bps || !count)
1170 mutex_lock(&con->recovery_lock);
1171 data = con->eh_data;
1172 if (!data || data->count == 0) {
1177 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1183 for (; i < data->count; i++) {
1184 (*bps)[i] = (struct ras_badpage){
1185 .bp = data->bps[i].bp,
1186 .size = AMDGPU_GPU_PAGE_SIZE,
1190 if (data->last_reserved <= i)
1191 (*bps)[i].flags = 1;
1192 else if (data->bps[i].bo == NULL)
1193 (*bps)[i].flags = 2;
1196 *count = data->count;
1198 mutex_unlock(&con->recovery_lock);
1202 static void amdgpu_ras_do_recovery(struct work_struct *work)
1204 struct amdgpu_ras *ras =
1205 container_of(work, struct amdgpu_ras, recovery_work);
1207 amdgpu_device_gpu_recover(ras->adev, 0);
1208 atomic_set(&ras->in_recovery, 0);
1211 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1212 struct amdgpu_bo **bo_ptr)
1214 /* no need to free it actually. */
1215 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1219 /* reserve vram with size@offset */
1220 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1221 uint64_t offset, uint64_t size,
1222 struct amdgpu_bo **bo_ptr)
1224 struct ttm_operation_ctx ctx = { false, false };
1225 struct amdgpu_bo_param bp;
1228 struct amdgpu_bo *bo;
1232 memset(&bp, 0, sizeof(bp));
1234 bp.byte_align = PAGE_SIZE;
1235 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1236 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1237 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1238 bp.type = ttm_bo_type_kernel;
1241 r = amdgpu_bo_create(adev, &bp, &bo);
1245 r = amdgpu_bo_reserve(bo, false);
1249 offset = ALIGN(offset, PAGE_SIZE);
1250 for (i = 0; i < bo->placement.num_placement; ++i) {
1251 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1252 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1255 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1256 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1260 r = amdgpu_bo_pin_restricted(bo,
1261 AMDGPU_GEM_DOMAIN_VRAM,
1270 amdgpu_bo_unreserve(bo);
1274 amdgpu_bo_unreserve(bo);
1276 amdgpu_bo_unref(&bo);
1280 /* alloc/realloc bps array */
1281 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1282 struct ras_err_handler_data *data, int pages)
1284 unsigned int old_space = data->count + data->space_left;
1285 unsigned int new_space = old_space + pages;
1286 unsigned int align_space = ALIGN(new_space, 1024);
1287 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1293 memcpy(tmp, data->bps,
1294 data->count * sizeof(*data->bps));
1299 data->space_left += align_space - old_space;
1303 /* it deal with vram only. */
1304 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1305 unsigned long *bps, int pages)
1307 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1308 struct ras_err_handler_data *data;
1312 if (!con || !con->eh_data || !bps || pages <= 0)
1315 mutex_lock(&con->recovery_lock);
1316 data = con->eh_data;
1320 if (data->space_left <= pages)
1321 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1327 data->bps[data->count++].bp = bps[i];
1329 data->space_left -= pages;
1331 mutex_unlock(&con->recovery_lock);
1336 /* called in gpu recovery/init */
1337 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1339 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1340 struct ras_err_handler_data *data;
1342 struct amdgpu_bo *bo;
1345 if (!con || !con->eh_data)
1348 mutex_lock(&con->recovery_lock);
1349 data = con->eh_data;
1352 /* reserve vram at driver post stage. */
1353 for (i = data->last_reserved; i < data->count; i++) {
1354 bp = data->bps[i].bp;
1356 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1358 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1360 data->bps[i].bo = bo;
1361 data->last_reserved = i + 1;
1364 mutex_unlock(&con->recovery_lock);
1368 /* called when driver unload */
1369 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1371 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1372 struct ras_err_handler_data *data;
1373 struct amdgpu_bo *bo;
1376 if (!con || !con->eh_data)
1379 mutex_lock(&con->recovery_lock);
1380 data = con->eh_data;
1384 for (i = data->last_reserved - 1; i >= 0; i--) {
1385 bo = data->bps[i].bo;
1387 amdgpu_ras_release_vram(adev, &bo);
1389 data->bps[i].bo = bo;
1390 data->last_reserved = i;
1393 mutex_unlock(&con->recovery_lock);
1397 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1400 * write the array to eeprom when SMU disabled.
1405 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1408 * read the array to eeprom when SMU disabled.
1413 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1415 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1416 struct ras_err_handler_data **data = &con->eh_data;
1418 *data = kmalloc(sizeof(**data),
1419 GFP_KERNEL|__GFP_ZERO);
1423 mutex_init(&con->recovery_lock);
1424 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1425 atomic_set(&con->in_recovery, 0);
1428 amdgpu_ras_load_bad_pages(adev);
1429 amdgpu_ras_reserve_bad_pages(adev);
1434 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1436 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1437 struct ras_err_handler_data *data = con->eh_data;
1439 cancel_work_sync(&con->recovery_work);
1440 amdgpu_ras_save_bad_pages(adev);
1441 amdgpu_ras_release_bad_pages(adev);
1443 mutex_lock(&con->recovery_lock);
1444 con->eh_data = NULL;
1447 mutex_unlock(&con->recovery_lock);
1453 /* return 0 if ras will reset gpu and repost.*/
1454 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1457 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1462 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1467 * check hardware's ras ability which will be saved in hw_supported.
1468 * if hardware does not support ras, we can skip some ras initializtion and
1469 * forbid some ras operations from IP.
1470 * if software itself, say boot parameter, limit the ras ability. We still
1471 * need allow IP do some limited operations, like disable. In such case,
1472 * we have to initialize ras as normal. but need check if operation is
1473 * allowed or not in each function.
1475 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1476 uint32_t *hw_supported, uint32_t *supported)
1481 if (amdgpu_sriov_vf(adev) ||
1482 adev->asic_type != CHIP_VEGA20)
1485 if (adev->is_atom_fw &&
1486 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1487 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1488 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1490 *supported = amdgpu_ras_enable == 0 ?
1491 0 : *hw_supported & amdgpu_ras_mask;
1494 int amdgpu_ras_init(struct amdgpu_device *adev)
1496 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1501 con = kmalloc(sizeof(struct amdgpu_ras) +
1502 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1503 GFP_KERNEL|__GFP_ZERO);
1507 con->objs = (struct ras_manager *)(con + 1);
1509 amdgpu_ras_set_context(adev, con);
1511 amdgpu_ras_check_supported(adev, &con->hw_supported,
1513 if (!con->hw_supported) {
1514 amdgpu_ras_set_context(adev, NULL);
1520 INIT_LIST_HEAD(&con->head);
1521 /* Might need get this flag from vbios. */
1522 con->flags = RAS_DEFAULT_FLAGS;
1524 if (amdgpu_ras_recovery_init(adev))
1527 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1529 if (amdgpu_ras_fs_init(adev))
1532 DRM_INFO("RAS INFO: ras initialized successfully, "
1533 "hardware ability[%x] ras_mask[%x]\n",
1534 con->hw_supported, con->supported);
1537 amdgpu_ras_recovery_fini(adev);
1539 amdgpu_ras_set_context(adev, NULL);
1545 /* do some init work after IP late init as dependence.
1546 * and it runs in resume/gpu reset/booting up cases.
1548 void amdgpu_ras_resume(struct amdgpu_device *adev)
1550 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1551 struct ras_manager *obj, *tmp;
1556 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1557 /* Set up all other IPs which are not implemented. There is a
1558 * tricky thing that IP's actual ras error type should be
1559 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1560 * ERROR_NONE make sense anyway.
1562 amdgpu_ras_enable_all_features(adev, 1);
1564 /* We enable ras on all hw_supported block, but as boot
1565 * parameter might disable some of them and one or more IP has
1566 * not implemented yet. So we disable them on behalf.
1568 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1569 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1570 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1571 /* there should be no any reference. */
1572 WARN_ON(alive_obj(obj));
1577 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1578 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1579 /* setup ras obj state as disabled.
1580 * for init_by_vbios case.
1581 * if we want to enable ras, just enable it in a normal way.
1582 * If we want do disable it, need setup ras obj as enabled,
1583 * then issue another TA disable cmd.
1584 * See feature_enable_on_boot
1586 amdgpu_ras_disable_all_features(adev, 1);
1587 amdgpu_ras_reset_gpu(adev, 0);
1591 void amdgpu_ras_suspend(struct amdgpu_device *adev)
1593 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1598 amdgpu_ras_disable_all_features(adev, 0);
1599 /* Make sure all ras objects are disabled. */
1601 amdgpu_ras_disable_all_features(adev, 1);
1604 /* do some fini work before IP fini as dependence */
1605 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1607 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1612 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1613 amdgpu_ras_disable_all_features(adev, 0);
1614 amdgpu_ras_recovery_fini(adev);
1618 int amdgpu_ras_fini(struct amdgpu_device *adev)
1620 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1625 amdgpu_ras_fs_fini(adev);
1626 amdgpu_ras_interrupt_remove_all(adev);
1628 WARN(con->features, "Feature mask is not cleared");
1631 amdgpu_ras_disable_all_features(adev, 1);
1633 amdgpu_ras_set_context(adev, NULL);