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);
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 if (block_info.block_id != TA_RAS_BLOCK__UMC) {
620 DRM_INFO("%s error injection is not supported yet\n",
621 ras_block_str(info->head.block));
625 ret = psp_ras_trigger_error(&adev->psp, &block_info);
627 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
628 ras_block_str(info->head.block),
634 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
635 struct ras_cure_if *info)
637 /* psp fw has no cure interface for now. */
641 /* get the total error counts on all IPs */
642 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
645 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
646 struct ras_manager *obj;
647 struct ras_err_data data = {0, 0};
652 list_for_each_entry(obj, &con->head, node) {
653 struct ras_query_if info = {
657 if (amdgpu_ras_error_query(adev, &info))
660 data.ce_count += info.ce_count;
661 data.ue_count += info.ue_count;
664 return is_ce ? data.ce_count : data.ue_count;
666 /* query/inject/cure end */
671 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
672 struct ras_badpage **bps, unsigned int *count);
674 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
688 * DOC: ras sysfs gpu_vram_bad_pages interface
690 * It allows user to read the bad pages of vram on the gpu through
691 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
693 * It outputs multiple lines, and each line stands for one gpu page.
695 * The format of one line is below,
696 * gpu pfn : gpu page size : flags
698 * gpu pfn and gpu page size are printed in hex format.
699 * flags can be one of below character,
700 * R: reserved, this gpu page is reserved and not able to use.
701 * P: pending for reserve, this gpu page is marked as bad, will be reserved
702 * in next window of page_reserve.
703 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
706 * 0x00000001 : 0x00001000 : R
707 * 0x00000002 : 0x00001000 : P
710 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
711 struct kobject *kobj, struct bin_attribute *attr,
712 char *buf, loff_t ppos, size_t count)
714 struct amdgpu_ras *con =
715 container_of(attr, struct amdgpu_ras, badpages_attr);
716 struct amdgpu_device *adev = con->adev;
717 const unsigned int element_size =
718 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
719 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
720 unsigned int end = div64_ul(ppos + count - 1, element_size);
722 struct ras_badpage *bps = NULL;
723 unsigned int bps_count = 0;
725 memset(buf, 0, count);
727 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
730 for (; start < end && start < bps_count; start++)
731 s += scnprintf(&buf[s], element_size + 1,
732 "0x%08x : 0x%08x : %1s\n",
735 amdgpu_ras_badpage_flags_str(bps[start].flags));
742 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
743 struct device_attribute *attr, char *buf)
745 struct amdgpu_ras *con =
746 container_of(attr, struct amdgpu_ras, features_attr);
747 struct drm_device *ddev = dev_get_drvdata(dev);
748 struct amdgpu_device *adev = ddev->dev_private;
749 struct ras_common_if head;
750 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
753 struct ras_manager *obj;
755 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
757 for (i = 0; i < ras_block_count; i++) {
760 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
761 obj = amdgpu_ras_find_obj(adev, &head);
762 s += scnprintf(&buf[s], PAGE_SIZE - s,
765 ras_err_str(obj->head.type));
767 s += scnprintf(&buf[s], PAGE_SIZE - s,
775 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
777 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
778 struct attribute *attrs[] = {
779 &con->features_attr.attr,
782 struct bin_attribute *bin_attrs[] = {
786 struct attribute_group group = {
789 .bin_attrs = bin_attrs,
792 con->features_attr = (struct device_attribute) {
797 .show = amdgpu_ras_sysfs_features_read,
800 con->badpages_attr = (struct bin_attribute) {
802 .name = "gpu_vram_bad_pages",
807 .read = amdgpu_ras_sysfs_badpages_read,
810 sysfs_attr_init(attrs[0]);
811 sysfs_bin_attr_init(bin_attrs[0]);
813 return sysfs_create_group(&adev->dev->kobj, &group);
816 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
818 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
819 struct attribute *attrs[] = {
820 &con->features_attr.attr,
823 struct bin_attribute *bin_attrs[] = {
827 struct attribute_group group = {
830 .bin_attrs = bin_attrs,
833 sysfs_remove_group(&adev->dev->kobj, &group);
838 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
839 struct ras_fs_if *head)
841 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
843 if (!obj || obj->attr_inuse)
848 memcpy(obj->fs_data.sysfs_name,
850 sizeof(obj->fs_data.sysfs_name));
852 obj->sysfs_attr = (struct device_attribute){
854 .name = obj->fs_data.sysfs_name,
857 .show = amdgpu_ras_sysfs_read,
859 sysfs_attr_init(&obj->sysfs_attr.attr);
861 if (sysfs_add_file_to_group(&adev->dev->kobj,
862 &obj->sysfs_attr.attr,
873 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
874 struct ras_common_if *head)
876 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
878 if (!obj || !obj->attr_inuse)
881 sysfs_remove_file_from_group(&adev->dev->kobj,
882 &obj->sysfs_attr.attr,
890 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
892 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
893 struct ras_manager *obj, *tmp;
895 list_for_each_entry_safe(obj, tmp, &con->head, node) {
896 amdgpu_ras_sysfs_remove(adev, &obj->head);
899 amdgpu_ras_sysfs_remove_feature_node(adev);
906 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
908 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
909 struct drm_minor *minor = adev->ddev->primary;
911 con->dir = debugfs_create_dir("ras", minor->debugfs_root);
912 con->ent = debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
913 adev, &amdgpu_ras_debugfs_ctrl_ops);
916 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
917 struct ras_fs_if *head)
919 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
920 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
922 if (!obj || obj->ent)
927 memcpy(obj->fs_data.debugfs_name,
929 sizeof(obj->fs_data.debugfs_name));
931 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
932 S_IWUGO | S_IRUGO, con->dir, obj,
933 &amdgpu_ras_debugfs_ops);
936 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
937 struct ras_common_if *head)
939 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
941 if (!obj || !obj->ent)
944 debugfs_remove(obj->ent);
949 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
951 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
952 struct ras_manager *obj, *tmp;
954 list_for_each_entry_safe(obj, tmp, &con->head, node) {
955 amdgpu_ras_debugfs_remove(adev, &obj->head);
958 debugfs_remove(con->ent);
959 debugfs_remove(con->dir);
967 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
969 amdgpu_ras_sysfs_create_feature_node(adev);
970 amdgpu_ras_debugfs_create_ctrl_node(adev);
975 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
977 amdgpu_ras_debugfs_remove_all(adev);
978 amdgpu_ras_sysfs_remove_all(adev);
984 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
986 struct ras_ih_data *data = &obj->ih_data;
987 struct amdgpu_iv_entry entry;
990 while (data->rptr != data->wptr) {
992 memcpy(&entry, &data->ring[data->rptr],
996 data->rptr = (data->aligned_element_size +
997 data->rptr) % data->ring_size;
999 /* Let IP handle its data, maybe we need get the output
1000 * from the callback to udpate the error type/count, etc
1003 ret = data->cb(obj->adev, &entry);
1004 /* ue will trigger an interrupt, and in that case
1005 * we need do a reset to recovery the whole system.
1006 * But leave IP do that recovery, here we just dispatch
1009 if (ret == AMDGPU_RAS_UE) {
1010 obj->err_data.ue_count++;
1012 /* Might need get ce count by register, but not all IP
1013 * saves ce count, some IP just use one bit or two bits
1014 * to indicate ce happened.
1020 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1022 struct ras_ih_data *data =
1023 container_of(work, struct ras_ih_data, ih_work);
1024 struct ras_manager *obj =
1025 container_of(data, struct ras_manager, ih_data);
1027 amdgpu_ras_interrupt_handler(obj);
1030 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1031 struct ras_dispatch_if *info)
1033 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1034 struct ras_ih_data *data = &obj->ih_data;
1039 if (data->inuse == 0)
1042 /* Might be overflow... */
1043 memcpy(&data->ring[data->wptr], info->entry,
1044 data->element_size);
1047 data->wptr = (data->aligned_element_size +
1048 data->wptr) % data->ring_size;
1050 schedule_work(&data->ih_work);
1055 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1056 struct ras_ih_if *info)
1058 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1059 struct ras_ih_data *data;
1064 data = &obj->ih_data;
1065 if (data->inuse == 0)
1068 cancel_work_sync(&data->ih_work);
1071 memset(data, 0, sizeof(*data));
1077 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1078 struct ras_ih_if *info)
1080 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1081 struct ras_ih_data *data;
1084 /* in case we registe the IH before enable ras feature */
1085 obj = amdgpu_ras_create_obj(adev, &info->head);
1091 data = &obj->ih_data;
1092 /* add the callback.etc */
1093 *data = (struct ras_ih_data) {
1096 .element_size = sizeof(struct amdgpu_iv_entry),
1101 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1103 data->aligned_element_size = ALIGN(data->element_size, 8);
1104 /* the ring can store 64 iv entries. */
1105 data->ring_size = 64 * data->aligned_element_size;
1106 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1118 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1120 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1121 struct ras_manager *obj, *tmp;
1123 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1124 struct ras_ih_if info = {
1127 amdgpu_ras_interrupt_remove_handler(adev, &info);
1134 /* recovery begin */
1136 /* return 0 on success.
1137 * caller need free bps.
1139 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1140 struct ras_badpage **bps, unsigned int *count)
1142 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1143 struct ras_err_handler_data *data;
1147 if (!con || !con->eh_data || !bps || !count)
1150 mutex_lock(&con->recovery_lock);
1151 data = con->eh_data;
1152 if (!data || data->count == 0) {
1157 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1163 for (; i < data->count; i++) {
1164 (*bps)[i] = (struct ras_badpage){
1165 .bp = data->bps[i].bp,
1166 .size = AMDGPU_GPU_PAGE_SIZE,
1170 if (data->last_reserved <= i)
1171 (*bps)[i].flags = 1;
1172 else if (data->bps[i].bo == NULL)
1173 (*bps)[i].flags = 2;
1176 *count = data->count;
1178 mutex_unlock(&con->recovery_lock);
1182 static void amdgpu_ras_do_recovery(struct work_struct *work)
1184 struct amdgpu_ras *ras =
1185 container_of(work, struct amdgpu_ras, recovery_work);
1187 amdgpu_device_gpu_recover(ras->adev, 0);
1188 atomic_set(&ras->in_recovery, 0);
1191 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1192 struct amdgpu_bo **bo_ptr)
1194 /* no need to free it actually. */
1195 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1199 /* reserve vram with size@offset */
1200 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1201 uint64_t offset, uint64_t size,
1202 struct amdgpu_bo **bo_ptr)
1204 struct ttm_operation_ctx ctx = { false, false };
1205 struct amdgpu_bo_param bp;
1208 struct amdgpu_bo *bo;
1212 memset(&bp, 0, sizeof(bp));
1214 bp.byte_align = PAGE_SIZE;
1215 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1216 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1217 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1218 bp.type = ttm_bo_type_kernel;
1221 r = amdgpu_bo_create(adev, &bp, &bo);
1225 r = amdgpu_bo_reserve(bo, false);
1229 offset = ALIGN(offset, PAGE_SIZE);
1230 for (i = 0; i < bo->placement.num_placement; ++i) {
1231 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1232 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1235 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1236 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1240 r = amdgpu_bo_pin_restricted(bo,
1241 AMDGPU_GEM_DOMAIN_VRAM,
1250 amdgpu_bo_unreserve(bo);
1254 amdgpu_bo_unreserve(bo);
1256 amdgpu_bo_unref(&bo);
1260 /* alloc/realloc bps array */
1261 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1262 struct ras_err_handler_data *data, int pages)
1264 unsigned int old_space = data->count + data->space_left;
1265 unsigned int new_space = old_space + pages;
1266 unsigned int align_space = ALIGN(new_space, 1024);
1267 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1273 memcpy(tmp, data->bps,
1274 data->count * sizeof(*data->bps));
1279 data->space_left += align_space - old_space;
1283 /* it deal with vram only. */
1284 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1285 unsigned long *bps, int pages)
1287 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1288 struct ras_err_handler_data *data;
1292 if (!con || !con->eh_data || !bps || pages <= 0)
1295 mutex_lock(&con->recovery_lock);
1296 data = con->eh_data;
1300 if (data->space_left <= pages)
1301 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1307 data->bps[data->count++].bp = bps[i];
1309 data->space_left -= pages;
1311 mutex_unlock(&con->recovery_lock);
1316 /* called in gpu recovery/init */
1317 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1319 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1320 struct ras_err_handler_data *data;
1322 struct amdgpu_bo *bo;
1325 if (!con || !con->eh_data)
1328 mutex_lock(&con->recovery_lock);
1329 data = con->eh_data;
1332 /* reserve vram at driver post stage. */
1333 for (i = data->last_reserved; i < data->count; i++) {
1334 bp = data->bps[i].bp;
1336 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1338 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1340 data->bps[i].bo = bo;
1341 data->last_reserved = i + 1;
1344 mutex_unlock(&con->recovery_lock);
1348 /* called when driver unload */
1349 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1351 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1352 struct ras_err_handler_data *data;
1353 struct amdgpu_bo *bo;
1356 if (!con || !con->eh_data)
1359 mutex_lock(&con->recovery_lock);
1360 data = con->eh_data;
1364 for (i = data->last_reserved - 1; i >= 0; i--) {
1365 bo = data->bps[i].bo;
1367 amdgpu_ras_release_vram(adev, &bo);
1369 data->bps[i].bo = bo;
1370 data->last_reserved = i;
1373 mutex_unlock(&con->recovery_lock);
1377 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1380 * write the array to eeprom when SMU disabled.
1385 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1388 * read the array to eeprom when SMU disabled.
1393 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1395 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1396 struct ras_err_handler_data **data = &con->eh_data;
1398 *data = kmalloc(sizeof(**data),
1399 GFP_KERNEL|__GFP_ZERO);
1403 mutex_init(&con->recovery_lock);
1404 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1405 atomic_set(&con->in_recovery, 0);
1408 amdgpu_ras_load_bad_pages(adev);
1409 amdgpu_ras_reserve_bad_pages(adev);
1414 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1416 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1417 struct ras_err_handler_data *data = con->eh_data;
1419 cancel_work_sync(&con->recovery_work);
1420 amdgpu_ras_save_bad_pages(adev);
1421 amdgpu_ras_release_bad_pages(adev);
1423 mutex_lock(&con->recovery_lock);
1424 con->eh_data = NULL;
1427 mutex_unlock(&con->recovery_lock);
1433 /* return 0 if ras will reset gpu and repost.*/
1434 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1437 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1442 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1447 * check hardware's ras ability which will be saved in hw_supported.
1448 * if hardware does not support ras, we can skip some ras initializtion and
1449 * forbid some ras operations from IP.
1450 * if software itself, say boot parameter, limit the ras ability. We still
1451 * need allow IP do some limited operations, like disable. In such case,
1452 * we have to initialize ras as normal. but need check if operation is
1453 * allowed or not in each function.
1455 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1456 uint32_t *hw_supported, uint32_t *supported)
1461 if (amdgpu_sriov_vf(adev) ||
1462 adev->asic_type != CHIP_VEGA20)
1465 if (adev->is_atom_fw &&
1466 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1467 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1468 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1470 *supported = amdgpu_ras_enable == 0 ?
1471 0 : *hw_supported & amdgpu_ras_mask;
1474 int amdgpu_ras_init(struct amdgpu_device *adev)
1476 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1481 con = kmalloc(sizeof(struct amdgpu_ras) +
1482 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1483 GFP_KERNEL|__GFP_ZERO);
1487 con->objs = (struct ras_manager *)(con + 1);
1489 amdgpu_ras_set_context(adev, con);
1491 amdgpu_ras_check_supported(adev, &con->hw_supported,
1493 if (!con->hw_supported) {
1494 amdgpu_ras_set_context(adev, NULL);
1500 INIT_LIST_HEAD(&con->head);
1501 /* Might need get this flag from vbios. */
1502 con->flags = RAS_DEFAULT_FLAGS;
1504 if (amdgpu_ras_recovery_init(adev))
1507 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1509 if (amdgpu_ras_fs_init(adev))
1512 DRM_INFO("RAS INFO: ras initialized successfully, "
1513 "hardware ability[%x] ras_mask[%x]\n",
1514 con->hw_supported, con->supported);
1517 amdgpu_ras_recovery_fini(adev);
1519 amdgpu_ras_set_context(adev, NULL);
1525 /* do some init work after IP late init as dependence.
1526 * and it runs in resume/gpu reset/booting up cases.
1528 void amdgpu_ras_resume(struct amdgpu_device *adev)
1530 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1531 struct ras_manager *obj, *tmp;
1536 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1537 /* Set up all other IPs which are not implemented. There is a
1538 * tricky thing that IP's actual ras error type should be
1539 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1540 * ERROR_NONE make sense anyway.
1542 amdgpu_ras_enable_all_features(adev, 1);
1544 /* We enable ras on all hw_supported block, but as boot
1545 * parameter might disable some of them and one or more IP has
1546 * not implemented yet. So we disable them on behalf.
1548 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1549 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1550 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1551 /* there should be no any reference. */
1552 WARN_ON(alive_obj(obj));
1557 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1558 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1559 /* setup ras obj state as disabled.
1560 * for init_by_vbios case.
1561 * if we want to enable ras, just enable it in a normal way.
1562 * If we want do disable it, need setup ras obj as enabled,
1563 * then issue another TA disable cmd.
1564 * See feature_enable_on_boot
1566 amdgpu_ras_disable_all_features(adev, 1);
1567 amdgpu_ras_reset_gpu(adev, 0);
1571 void amdgpu_ras_suspend(struct amdgpu_device *adev)
1573 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1578 amdgpu_ras_disable_all_features(adev, 0);
1579 /* Make sure all ras objects are disabled. */
1581 amdgpu_ras_disable_all_features(adev, 1);
1584 /* do some fini work before IP fini as dependence */
1585 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1587 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1592 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1593 amdgpu_ras_disable_all_features(adev, 0);
1594 amdgpu_ras_recovery_fini(adev);
1598 int amdgpu_ras_fini(struct amdgpu_device *adev)
1600 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1605 amdgpu_ras_fs_fini(adev);
1606 amdgpu_ras_interrupt_remove_all(adev);
1608 WARN(con->features, "Feature mask is not cleared");
1611 amdgpu_ras_disable_all_features(adev, 1);
1613 amdgpu_ras_set_context(adev, NULL);