2 * Copyright 2018 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/debugfs.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
28 #include "amdgpu_ras.h"
29 #include "amdgpu_atomfirmware.h"
32 /* interrupt bottom half */
33 struct work_struct ih_work;
39 unsigned int ring_size;
40 unsigned int element_size;
41 unsigned int aligned_element_size;
48 char debugfs_name[32];
52 unsigned long ue_count;
53 unsigned long ce_count;
56 struct ras_err_handler_data {
57 /* point to bad pages array */
62 /* the count of entries */
64 /* the space can place new entries */
66 /* last reserved entry's index + 1 */
71 struct ras_common_if head;
75 struct list_head node;
77 struct amdgpu_device *adev;
81 struct device_attribute sysfs_attr;
85 struct ras_fs_data fs_data;
88 struct ras_ih_data ih_data;
90 struct ras_err_data err_data;
99 const char *ras_error_string[] = {
102 "single_correctable",
103 "multi_uncorrectable",
107 const char *ras_block_string[] = {
124 #define ras_err_str(i) (ras_error_string[ffs(i)])
125 #define ras_block_str(i) (ras_block_string[i])
127 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
128 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET 2
129 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
131 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
132 uint64_t offset, uint64_t size,
133 struct amdgpu_bo **bo_ptr);
134 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
135 struct amdgpu_bo **bo_ptr);
137 static void amdgpu_ras_self_test(struct amdgpu_device *adev)
142 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
143 size_t size, loff_t *pos)
145 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
146 struct ras_query_if info = {
152 if (amdgpu_ras_error_query(obj->adev, &info))
155 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
157 "ce", info.ce_count);
162 s = min_t(u64, s, size);
165 if (copy_to_user(buf, &val[*pos], s))
173 static const struct file_operations amdgpu_ras_debugfs_ops = {
174 .owner = THIS_MODULE,
175 .read = amdgpu_ras_debugfs_read,
177 .llseek = default_llseek
180 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
184 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
186 if (strcmp(name, ras_block_str(i)) == 0)
192 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
193 const char __user *buf, size_t size,
194 loff_t *pos, struct ras_debug_if *data)
196 ssize_t s = min_t(u64, 64, size);
208 memset(str, 0, sizeof(str));
209 memset(data, 0, sizeof(*data));
211 if (copy_from_user(str, buf, s))
214 if (sscanf(str, "disable %32s", block_name) == 1)
216 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
218 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
220 else if (str[0] && str[1] && str[2] && str[3])
221 /* ascii string, but commands are not matched. */
225 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
228 data->head.block = block_id;
229 data->head.type = memcmp("ue", err, 2) == 0 ?
230 AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE :
231 AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
235 if (sscanf(str, "%*s %*s %*s %llu %llu",
236 &address, &value) != 2)
237 if (sscanf(str, "%*s %*s %*s 0x%llx 0x%llx",
238 &address, &value) != 2)
240 data->inject.address = address;
241 data->inject.value = value;
244 if (size < sizeof(*data))
247 if (copy_from_user(data, buf, sizeof(*data)))
254 * DOC: AMDGPU RAS debugfs control interface
256 * It accepts struct ras_debug_if who has two members.
258 * First member: ras_debug_if::head or ras_debug_if::inject.
260 * head is used to indicate which IP block will be under control.
262 * head has four members, they are block, type, sub_block_index, name.
263 * block: which IP will be under control.
264 * type: what kind of error will be enabled/disabled/injected.
265 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
266 * name: the name of IP.
268 * inject has two more members than head, they are address, value.
269 * As their names indicate, inject operation will write the
270 * value to the address.
272 * Second member: struct ras_debug_if::op.
273 * It has three kinds of operations.
274 * 0: disable RAS on the block. Take ::head as its data.
275 * 1: enable RAS on the block. Take ::head as its data.
276 * 2: inject errors on the block. Take ::inject as its data.
278 * How to use the interface?
280 * copy the struct ras_debug_if in your codes and initialize it.
281 * write the struct to the control node.
284 * echo op block [error [address value]] > .../ras/ras_ctrl
285 * op: disable, enable, inject
286 * disable: only block is needed
287 * enable: block and error are needed
288 * inject: error, address, value are needed
289 * block: umc, smda, gfx, .........
290 * see ras_block_string[] for details
292 * ue: multi_uncorrectable
293 * ce: single_correctable
295 * here are some examples for bash commands,
296 * echo inject umc ue 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
297 * echo inject umc ce 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
298 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
300 * How to check the result?
302 * For disable/enable, please check ras features at
303 * /sys/class/drm/card[0/1/2...]/device/ras/features
305 * For inject, please check corresponding err count at
306 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
308 * NOTE: operation is only allowed on blocks which are supported.
309 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
311 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
312 size_t size, loff_t *pos)
314 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
315 struct ras_debug_if data;
316 struct amdgpu_bo *bo;
319 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
323 if (!amdgpu_ras_is_supported(adev, data.head.block))
328 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
331 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
334 ret = amdgpu_ras_reserve_vram(adev,
335 data.inject.address, PAGE_SIZE, &bo);
336 /* This address might be used already on failure. In fact we can
337 * perform an injection in such case.
341 data.inject.address = amdgpu_bo_gpu_offset(bo);
342 ret = amdgpu_ras_error_inject(adev, &data.inject);
343 amdgpu_ras_release_vram(adev, &bo);
356 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
357 .owner = THIS_MODULE,
359 .write = amdgpu_ras_debugfs_ctrl_write,
360 .llseek = default_llseek
363 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
364 struct device_attribute *attr, char *buf)
366 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
367 struct ras_query_if info = {
371 if (amdgpu_ras_error_query(obj->adev, &info))
374 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
376 "ce", info.ce_count);
381 #define get_obj(obj) do { (obj)->use++; } while (0)
382 #define alive_obj(obj) ((obj)->use)
384 static inline void put_obj(struct ras_manager *obj)
386 if (obj && --obj->use == 0)
387 list_del(&obj->node);
388 if (obj && obj->use < 0) {
389 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
393 /* make one obj and return it. */
394 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
395 struct ras_common_if *head)
397 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
398 struct ras_manager *obj;
403 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
406 obj = &con->objs[head->block];
407 /* already exist. return obj? */
413 list_add(&obj->node, &con->head);
419 /* return an obj equal to head, or the first when head is NULL */
420 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
421 struct ras_common_if *head)
423 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
424 struct ras_manager *obj;
431 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
434 obj = &con->objs[head->block];
436 if (alive_obj(obj)) {
437 WARN_ON(head->block != obj->head.block);
441 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
443 if (alive_obj(obj)) {
444 WARN_ON(i != obj->head.block);
454 /* feature ctl begin */
455 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
456 struct ras_common_if *head)
458 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
460 return con->hw_supported & BIT(head->block);
463 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
464 struct ras_common_if *head)
466 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
468 return con->features & BIT(head->block);
472 * if obj is not created, then create one.
473 * set feature enable flag.
475 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
476 struct ras_common_if *head, int enable)
478 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
479 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
481 /* If hardware does not support ras, then do not create obj.
482 * But if hardware support ras, we can create the obj.
483 * Ras framework checks con->hw_supported to see if it need do
484 * corresponding initialization.
485 * IP checks con->support to see if it need disable ras.
487 if (!amdgpu_ras_is_feature_allowed(adev, head))
489 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
494 obj = amdgpu_ras_create_obj(adev, head);
498 /* In case we create obj somewhere else */
501 con->features |= BIT(head->block);
503 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
504 con->features &= ~BIT(head->block);
512 /* wrapper of psp_ras_enable_features */
513 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
514 struct ras_common_if *head, bool enable)
516 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
517 union ta_ras_cmd_input info;
524 info.disable_features = (struct ta_ras_disable_features_input) {
525 .block_id = amdgpu_ras_block_to_ta(head->block),
526 .error_type = amdgpu_ras_error_to_ta(head->type),
529 info.enable_features = (struct ta_ras_enable_features_input) {
530 .block_id = amdgpu_ras_block_to_ta(head->block),
531 .error_type = amdgpu_ras_error_to_ta(head->type),
535 /* Do not enable if it is not allowed. */
536 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
537 /* Are we alerady in that state we are going to set? */
538 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
541 ret = psp_ras_enable_features(&adev->psp, &info, enable);
543 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
544 enable ? "enable":"disable",
545 ras_block_str(head->block),
547 if (ret == TA_RAS_STATUS__RESET_NEEDED)
553 __amdgpu_ras_feature_enable(adev, head, enable);
558 /* Only used in device probe stage and called only once. */
559 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
560 struct ras_common_if *head, bool enable)
562 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
568 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
570 /* There is no harm to issue a ras TA cmd regardless of
571 * the currecnt ras state.
572 * If current state == target state, it will do nothing
573 * But sometimes it requests driver to reset and repost
574 * with error code -EAGAIN.
576 ret = amdgpu_ras_feature_enable(adev, head, 1);
577 /* With old ras TA, we might fail to enable ras.
578 * Log it and just setup the object.
579 * TODO need remove this WA in the future.
581 if (ret == -EINVAL) {
582 ret = __amdgpu_ras_feature_enable(adev, head, 1);
584 DRM_INFO("RAS INFO: %s setup object\n",
585 ras_block_str(head->block));
588 /* setup the object then issue a ras TA disable cmd.*/
589 ret = __amdgpu_ras_feature_enable(adev, head, 1);
593 ret = amdgpu_ras_feature_enable(adev, head, 0);
596 ret = amdgpu_ras_feature_enable(adev, head, enable);
601 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
604 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
605 struct ras_manager *obj, *tmp;
607 list_for_each_entry_safe(obj, tmp, &con->head, node) {
609 * aka just release the obj and corresponding flags
612 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
615 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
620 return con->features;
623 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
626 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
627 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
629 const enum amdgpu_ras_error_type default_ras_type =
630 AMDGPU_RAS_ERROR__NONE;
632 for (i = 0; i < ras_block_count; i++) {
633 struct ras_common_if head = {
635 .type = default_ras_type,
636 .sub_block_index = 0,
638 strcpy(head.name, ras_block_str(i));
641 * bypass psp. vbios enable ras for us.
642 * so just create the obj
644 if (__amdgpu_ras_feature_enable(adev, &head, 1))
647 if (amdgpu_ras_feature_enable(adev, &head, 1))
652 return con->features;
654 /* feature ctl end */
656 /* query/inject/cure begin */
657 int amdgpu_ras_error_query(struct amdgpu_device *adev,
658 struct ras_query_if *info)
660 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
664 /* TODO might read the register to read the count */
666 info->ue_count = obj->err_data.ue_count;
667 info->ce_count = obj->err_data.ce_count;
672 /* wrapper of psp_ras_trigger_error */
673 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
674 struct ras_inject_if *info)
676 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
677 struct ta_ras_trigger_error_input block_info = {
678 .block_id = amdgpu_ras_block_to_ta(info->head.block),
679 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
680 .sub_block_index = info->head.sub_block_index,
681 .address = info->address,
682 .value = info->value,
689 ret = psp_ras_trigger_error(&adev->psp, &block_info);
691 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
692 ras_block_str(info->head.block),
698 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
699 struct ras_cure_if *info)
701 /* psp fw has no cure interface for now. */
705 /* get the total error counts on all IPs */
706 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
709 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
710 struct ras_manager *obj;
711 struct ras_err_data data = {0, 0};
716 list_for_each_entry(obj, &con->head, node) {
717 struct ras_query_if info = {
721 if (amdgpu_ras_error_query(adev, &info))
724 data.ce_count += info.ce_count;
725 data.ue_count += info.ue_count;
728 return is_ce ? data.ce_count : data.ue_count;
730 /* query/inject/cure end */
735 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
736 struct ras_badpage **bps, unsigned int *count);
738 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
752 * DOC: ras sysfs gpu_vram_bad_pages interface
754 * It allows user to read the bad pages of vram on the gpu through
755 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
757 * It outputs multiple lines, and each line stands for one gpu page.
759 * The format of one line is below,
760 * gpu pfn : gpu page size : flags
762 * gpu pfn and gpu page size are printed in hex format.
763 * flags can be one of below character,
764 * R: reserved, this gpu page is reserved and not able to use.
765 * P: pending for reserve, this gpu page is marked as bad, will be reserved
766 * in next window of page_reserve.
767 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
770 * 0x00000001 : 0x00001000 : R
771 * 0x00000002 : 0x00001000 : P
774 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
775 struct kobject *kobj, struct bin_attribute *attr,
776 char *buf, loff_t ppos, size_t count)
778 struct amdgpu_ras *con =
779 container_of(attr, struct amdgpu_ras, badpages_attr);
780 struct amdgpu_device *adev = con->adev;
781 const unsigned int element_size =
782 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
783 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
784 unsigned int end = div64_ul(ppos + count - 1, element_size);
786 struct ras_badpage *bps = NULL;
787 unsigned int bps_count = 0;
789 memset(buf, 0, count);
791 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
794 for (; start < end && start < bps_count; start++)
795 s += scnprintf(&buf[s], element_size + 1,
796 "0x%08x : 0x%08x : %1s\n",
799 amdgpu_ras_badpage_flags_str(bps[start].flags));
806 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
807 struct device_attribute *attr, char *buf)
809 struct amdgpu_ras *con =
810 container_of(attr, struct amdgpu_ras, features_attr);
811 struct drm_device *ddev = dev_get_drvdata(dev);
812 struct amdgpu_device *adev = ddev->dev_private;
813 struct ras_common_if head;
814 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
817 struct ras_manager *obj;
819 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
821 for (i = 0; i < ras_block_count; i++) {
824 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
825 obj = amdgpu_ras_find_obj(adev, &head);
826 s += scnprintf(&buf[s], PAGE_SIZE - s,
829 ras_err_str(obj->head.type));
831 s += scnprintf(&buf[s], PAGE_SIZE - s,
839 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
841 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
842 struct attribute *attrs[] = {
843 &con->features_attr.attr,
846 struct bin_attribute *bin_attrs[] = {
850 struct attribute_group group = {
853 .bin_attrs = bin_attrs,
856 con->features_attr = (struct device_attribute) {
861 .show = amdgpu_ras_sysfs_features_read,
864 con->badpages_attr = (struct bin_attribute) {
866 .name = "gpu_vram_bad_pages",
871 .read = amdgpu_ras_sysfs_badpages_read,
874 sysfs_attr_init(attrs[0]);
875 sysfs_bin_attr_init(bin_attrs[0]);
877 return sysfs_create_group(&adev->dev->kobj, &group);
880 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
882 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
883 struct attribute *attrs[] = {
884 &con->features_attr.attr,
887 struct bin_attribute *bin_attrs[] = {
891 struct attribute_group group = {
894 .bin_attrs = bin_attrs,
897 sysfs_remove_group(&adev->dev->kobj, &group);
902 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
903 struct ras_fs_if *head)
905 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
907 if (!obj || obj->attr_inuse)
912 memcpy(obj->fs_data.sysfs_name,
914 sizeof(obj->fs_data.sysfs_name));
916 obj->sysfs_attr = (struct device_attribute){
918 .name = obj->fs_data.sysfs_name,
921 .show = amdgpu_ras_sysfs_read,
923 sysfs_attr_init(&obj->sysfs_attr.attr);
925 if (sysfs_add_file_to_group(&adev->dev->kobj,
926 &obj->sysfs_attr.attr,
937 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
938 struct ras_common_if *head)
940 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
942 if (!obj || !obj->attr_inuse)
945 sysfs_remove_file_from_group(&adev->dev->kobj,
946 &obj->sysfs_attr.attr,
954 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
956 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
957 struct ras_manager *obj, *tmp;
959 list_for_each_entry_safe(obj, tmp, &con->head, node) {
960 amdgpu_ras_sysfs_remove(adev, &obj->head);
963 amdgpu_ras_sysfs_remove_feature_node(adev);
970 static int amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
972 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
973 struct drm_minor *minor = adev->ddev->primary;
974 struct dentry *root = minor->debugfs_root, *dir;
977 dir = debugfs_create_dir("ras", root);
983 ent = debugfs_create_file("ras_ctrl",
984 S_IWUGO | S_IRUGO, con->dir,
985 adev, &amdgpu_ras_debugfs_ctrl_ops);
987 debugfs_remove(con->dir);
995 int amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
996 struct ras_fs_if *head)
998 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
999 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1002 if (!obj || obj->ent)
1007 memcpy(obj->fs_data.debugfs_name,
1009 sizeof(obj->fs_data.debugfs_name));
1011 ent = debugfs_create_file(obj->fs_data.debugfs_name,
1012 S_IWUGO | S_IRUGO, con->dir,
1013 obj, &amdgpu_ras_debugfs_ops);
1023 int amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1024 struct ras_common_if *head)
1026 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1028 if (!obj || !obj->ent)
1031 debugfs_remove(obj->ent);
1038 static int amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1040 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1041 struct ras_manager *obj, *tmp;
1043 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1044 amdgpu_ras_debugfs_remove(adev, &obj->head);
1047 debugfs_remove(con->ent);
1048 debugfs_remove(con->dir);
1058 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1060 amdgpu_ras_sysfs_create_feature_node(adev);
1061 amdgpu_ras_debugfs_create_ctrl_node(adev);
1066 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1068 amdgpu_ras_debugfs_remove_all(adev);
1069 amdgpu_ras_sysfs_remove_all(adev);
1075 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1077 struct ras_ih_data *data = &obj->ih_data;
1078 struct amdgpu_iv_entry entry;
1081 while (data->rptr != data->wptr) {
1083 memcpy(&entry, &data->ring[data->rptr],
1084 data->element_size);
1087 data->rptr = (data->aligned_element_size +
1088 data->rptr) % data->ring_size;
1090 /* Let IP handle its data, maybe we need get the output
1091 * from the callback to udpate the error type/count, etc
1094 ret = data->cb(obj->adev, &entry);
1095 /* ue will trigger an interrupt, and in that case
1096 * we need do a reset to recovery the whole system.
1097 * But leave IP do that recovery, here we just dispatch
1100 if (ret == AMDGPU_RAS_UE) {
1101 obj->err_data.ue_count++;
1103 /* Might need get ce count by register, but not all IP
1104 * saves ce count, some IP just use one bit or two bits
1105 * to indicate ce happened.
1111 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1113 struct ras_ih_data *data =
1114 container_of(work, struct ras_ih_data, ih_work);
1115 struct ras_manager *obj =
1116 container_of(data, struct ras_manager, ih_data);
1118 amdgpu_ras_interrupt_handler(obj);
1121 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1122 struct ras_dispatch_if *info)
1124 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1125 struct ras_ih_data *data = &obj->ih_data;
1130 if (data->inuse == 0)
1133 /* Might be overflow... */
1134 memcpy(&data->ring[data->wptr], info->entry,
1135 data->element_size);
1138 data->wptr = (data->aligned_element_size +
1139 data->wptr) % data->ring_size;
1141 schedule_work(&data->ih_work);
1146 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1147 struct ras_ih_if *info)
1149 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1150 struct ras_ih_data *data;
1155 data = &obj->ih_data;
1156 if (data->inuse == 0)
1159 cancel_work_sync(&data->ih_work);
1162 memset(data, 0, sizeof(*data));
1168 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1169 struct ras_ih_if *info)
1171 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1172 struct ras_ih_data *data;
1175 /* in case we registe the IH before enable ras feature */
1176 obj = amdgpu_ras_create_obj(adev, &info->head);
1182 data = &obj->ih_data;
1183 /* add the callback.etc */
1184 *data = (struct ras_ih_data) {
1187 .element_size = sizeof(struct amdgpu_iv_entry),
1192 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1194 data->aligned_element_size = ALIGN(data->element_size, 8);
1195 /* the ring can store 64 iv entries. */
1196 data->ring_size = 64 * data->aligned_element_size;
1197 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1209 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1211 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1212 struct ras_manager *obj, *tmp;
1214 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1215 struct ras_ih_if info = {
1218 amdgpu_ras_interrupt_remove_handler(adev, &info);
1225 /* recovery begin */
1227 /* return 0 on success.
1228 * caller need free bps.
1230 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1231 struct ras_badpage **bps, unsigned int *count)
1233 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1234 struct ras_err_handler_data *data;
1238 if (!con || !con->eh_data || !bps || !count)
1241 mutex_lock(&con->recovery_lock);
1242 data = con->eh_data;
1243 if (!data || data->count == 0) {
1248 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1254 for (; i < data->count; i++) {
1255 (*bps)[i] = (struct ras_badpage){
1256 .bp = data->bps[i].bp,
1257 .size = AMDGPU_GPU_PAGE_SIZE,
1261 if (data->last_reserved <= i)
1262 (*bps)[i].flags = 1;
1263 else if (data->bps[i].bo == NULL)
1264 (*bps)[i].flags = 2;
1267 *count = data->count;
1269 mutex_unlock(&con->recovery_lock);
1273 static void amdgpu_ras_do_recovery(struct work_struct *work)
1275 struct amdgpu_ras *ras =
1276 container_of(work, struct amdgpu_ras, recovery_work);
1278 amdgpu_device_gpu_recover(ras->adev, 0);
1279 atomic_set(&ras->in_recovery, 0);
1282 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1283 struct amdgpu_bo **bo_ptr)
1285 /* no need to free it actually. */
1286 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1290 /* reserve vram with size@offset */
1291 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1292 uint64_t offset, uint64_t size,
1293 struct amdgpu_bo **bo_ptr)
1295 struct ttm_operation_ctx ctx = { false, false };
1296 struct amdgpu_bo_param bp;
1299 struct amdgpu_bo *bo;
1303 memset(&bp, 0, sizeof(bp));
1305 bp.byte_align = PAGE_SIZE;
1306 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1307 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1308 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1309 bp.type = ttm_bo_type_kernel;
1312 r = amdgpu_bo_create(adev, &bp, &bo);
1316 r = amdgpu_bo_reserve(bo, false);
1320 offset = ALIGN(offset, PAGE_SIZE);
1321 for (i = 0; i < bo->placement.num_placement; ++i) {
1322 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1323 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1326 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1327 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1331 r = amdgpu_bo_pin_restricted(bo,
1332 AMDGPU_GEM_DOMAIN_VRAM,
1341 amdgpu_bo_unreserve(bo);
1345 amdgpu_bo_unreserve(bo);
1347 amdgpu_bo_unref(&bo);
1351 /* alloc/realloc bps array */
1352 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1353 struct ras_err_handler_data *data, int pages)
1355 unsigned int old_space = data->count + data->space_left;
1356 unsigned int new_space = old_space + pages;
1357 unsigned int align_space = ALIGN(new_space, 1024);
1358 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1364 memcpy(tmp, data->bps,
1365 data->count * sizeof(*data->bps));
1370 data->space_left += align_space - old_space;
1374 /* it deal with vram only. */
1375 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1376 unsigned long *bps, int pages)
1378 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1379 struct ras_err_handler_data *data;
1383 if (!con || !con->eh_data || !bps || pages <= 0)
1386 mutex_lock(&con->recovery_lock);
1387 data = con->eh_data;
1391 if (data->space_left <= pages)
1392 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1398 data->bps[data->count++].bp = bps[i];
1400 data->space_left -= pages;
1402 mutex_unlock(&con->recovery_lock);
1407 /* called in gpu recovery/init */
1408 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1410 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1411 struct ras_err_handler_data *data;
1413 struct amdgpu_bo *bo;
1416 if (!con || !con->eh_data)
1419 mutex_lock(&con->recovery_lock);
1420 data = con->eh_data;
1423 /* reserve vram at driver post stage. */
1424 for (i = data->last_reserved; i < data->count; i++) {
1425 bp = data->bps[i].bp;
1427 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1429 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1431 data->bps[i].bo = bo;
1432 data->last_reserved = i + 1;
1435 mutex_unlock(&con->recovery_lock);
1439 /* called when driver unload */
1440 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1442 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1443 struct ras_err_handler_data *data;
1444 struct amdgpu_bo *bo;
1447 if (!con || !con->eh_data)
1450 mutex_lock(&con->recovery_lock);
1451 data = con->eh_data;
1455 for (i = data->last_reserved - 1; i >= 0; i--) {
1456 bo = data->bps[i].bo;
1458 amdgpu_ras_release_vram(adev, &bo);
1460 data->bps[i].bo = bo;
1461 data->last_reserved = i;
1464 mutex_unlock(&con->recovery_lock);
1468 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1471 * write the array to eeprom when SMU disabled.
1476 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1479 * read the array to eeprom when SMU disabled.
1484 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1486 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1487 struct ras_err_handler_data **data = &con->eh_data;
1489 *data = kmalloc(sizeof(**data),
1490 GFP_KERNEL|__GFP_ZERO);
1494 mutex_init(&con->recovery_lock);
1495 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1496 atomic_set(&con->in_recovery, 0);
1499 amdgpu_ras_load_bad_pages(adev);
1500 amdgpu_ras_reserve_bad_pages(adev);
1505 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1507 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1508 struct ras_err_handler_data *data = con->eh_data;
1510 cancel_work_sync(&con->recovery_work);
1511 amdgpu_ras_save_bad_pages(adev);
1512 amdgpu_ras_release_bad_pages(adev);
1514 mutex_lock(&con->recovery_lock);
1515 con->eh_data = NULL;
1518 mutex_unlock(&con->recovery_lock);
1524 /* return 0 if ras will reset gpu and repost.*/
1525 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1528 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1533 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1538 * check hardware's ras ability which will be saved in hw_supported.
1539 * if hardware does not support ras, we can skip some ras initializtion and
1540 * forbid some ras operations from IP.
1541 * if software itself, say boot parameter, limit the ras ability. We still
1542 * need allow IP do some limited operations, like disable. In such case,
1543 * we have to initialize ras as normal. but need check if operation is
1544 * allowed or not in each function.
1546 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1547 uint32_t *hw_supported, uint32_t *supported)
1552 if (amdgpu_sriov_vf(adev) ||
1553 adev->asic_type != CHIP_VEGA20)
1556 if (adev->is_atom_fw &&
1557 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1558 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1559 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1561 *supported = amdgpu_ras_enable == 0 ?
1562 0 : *hw_supported & amdgpu_ras_mask;
1565 int amdgpu_ras_init(struct amdgpu_device *adev)
1567 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1572 con = kmalloc(sizeof(struct amdgpu_ras) +
1573 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1574 GFP_KERNEL|__GFP_ZERO);
1578 con->objs = (struct ras_manager *)(con + 1);
1580 amdgpu_ras_set_context(adev, con);
1582 amdgpu_ras_check_supported(adev, &con->hw_supported,
1585 INIT_LIST_HEAD(&con->head);
1586 /* Might need get this flag from vbios. */
1587 con->flags = RAS_DEFAULT_FLAGS;
1589 if (amdgpu_ras_recovery_init(adev))
1592 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1594 if (amdgpu_ras_fs_init(adev))
1597 amdgpu_ras_self_test(adev);
1599 DRM_INFO("RAS INFO: ras initialized successfully, "
1600 "hardware ability[%x] ras_mask[%x]\n",
1601 con->hw_supported, con->supported);
1604 amdgpu_ras_recovery_fini(adev);
1606 amdgpu_ras_set_context(adev, NULL);
1612 /* do some init work after IP late init as dependence.
1613 * and it runs in resume/gpu reset/booting up cases.
1615 void amdgpu_ras_resume(struct amdgpu_device *adev)
1617 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1618 struct ras_manager *obj, *tmp;
1623 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1624 /* Set up all other IPs which are not implemented. There is a
1625 * tricky thing that IP's actual ras error type should be
1626 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1627 * ERROR_NONE make sense anyway.
1629 amdgpu_ras_enable_all_features(adev, 1);
1631 /* We enable ras on all hw_supported block, but as boot
1632 * parameter might disable some of them and one or more IP has
1633 * not implemented yet. So we disable them on behalf.
1635 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1636 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1637 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1638 /* there should be no any reference. */
1639 WARN_ON(alive_obj(obj));
1644 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1645 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1646 /* setup ras obj state as disabled.
1647 * for init_by_vbios case.
1648 * if we want to enable ras, just enable it in a normal way.
1649 * If we want do disable it, need setup ras obj as enabled,
1650 * then issue another TA disable cmd.
1651 * See feature_enable_on_boot
1653 amdgpu_ras_disable_all_features(adev, 1);
1654 amdgpu_ras_reset_gpu(adev, 0);
1658 void amdgpu_ras_suspend(struct amdgpu_device *adev)
1660 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1665 amdgpu_ras_disable_all_features(adev, 0);
1666 /* Make sure all ras objects are disabled. */
1668 amdgpu_ras_disable_all_features(adev, 1);
1671 /* do some fini work before IP fini as dependence */
1672 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1674 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1679 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1680 amdgpu_ras_disable_all_features(adev, 0);
1681 amdgpu_ras_recovery_fini(adev);
1685 int amdgpu_ras_fini(struct amdgpu_device *adev)
1687 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1692 amdgpu_ras_fs_fini(adev);
1693 amdgpu_ras_interrupt_remove_all(adev);
1695 WARN(con->features, "Feature mask is not cleared");
1698 amdgpu_ras_disable_all_features(adev, 1);
1700 amdgpu_ras_set_context(adev, NULL);