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);
337 /* address was offset, now it is absolute.*/
338 data.inject.address += adev->gmc.vram_start;
339 if (data.inject.address > adev->gmc.vram_end)
342 data.inject.address = amdgpu_bo_gpu_offset(bo);
343 ret = amdgpu_ras_error_inject(adev, &data.inject);
344 amdgpu_ras_release_vram(adev, &bo);
357 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
358 .owner = THIS_MODULE,
360 .write = amdgpu_ras_debugfs_ctrl_write,
361 .llseek = default_llseek
364 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
365 struct device_attribute *attr, char *buf)
367 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
368 struct ras_query_if info = {
372 if (amdgpu_ras_error_query(obj->adev, &info))
375 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
377 "ce", info.ce_count);
382 #define get_obj(obj) do { (obj)->use++; } while (0)
383 #define alive_obj(obj) ((obj)->use)
385 static inline void put_obj(struct ras_manager *obj)
387 if (obj && --obj->use == 0)
388 list_del(&obj->node);
389 if (obj && obj->use < 0) {
390 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
394 /* make one obj and return it. */
395 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
396 struct ras_common_if *head)
398 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
399 struct ras_manager *obj;
404 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
407 obj = &con->objs[head->block];
408 /* already exist. return obj? */
414 list_add(&obj->node, &con->head);
420 /* return an obj equal to head, or the first when head is NULL */
421 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
422 struct ras_common_if *head)
424 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
425 struct ras_manager *obj;
432 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
435 obj = &con->objs[head->block];
437 if (alive_obj(obj)) {
438 WARN_ON(head->block != obj->head.block);
442 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
444 if (alive_obj(obj)) {
445 WARN_ON(i != obj->head.block);
455 /* feature ctl begin */
456 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
457 struct ras_common_if *head)
459 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
461 return con->hw_supported & BIT(head->block);
464 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
465 struct ras_common_if *head)
467 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
469 return con->features & BIT(head->block);
473 * if obj is not created, then create one.
474 * set feature enable flag.
476 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
477 struct ras_common_if *head, int enable)
479 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
480 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
482 /* If hardware does not support ras, then do not create obj.
483 * But if hardware support ras, we can create the obj.
484 * Ras framework checks con->hw_supported to see if it need do
485 * corresponding initialization.
486 * IP checks con->support to see if it need disable ras.
488 if (!amdgpu_ras_is_feature_allowed(adev, head))
490 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
495 obj = amdgpu_ras_create_obj(adev, head);
499 /* In case we create obj somewhere else */
502 con->features |= BIT(head->block);
504 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
505 con->features &= ~BIT(head->block);
513 /* wrapper of psp_ras_enable_features */
514 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
515 struct ras_common_if *head, bool enable)
517 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
518 union ta_ras_cmd_input info;
525 info.disable_features = (struct ta_ras_disable_features_input) {
526 .block_id = amdgpu_ras_block_to_ta(head->block),
527 .error_type = amdgpu_ras_error_to_ta(head->type),
530 info.enable_features = (struct ta_ras_enable_features_input) {
531 .block_id = amdgpu_ras_block_to_ta(head->block),
532 .error_type = amdgpu_ras_error_to_ta(head->type),
536 /* Do not enable if it is not allowed. */
537 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
538 /* Are we alerady in that state we are going to set? */
539 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
542 ret = psp_ras_enable_features(&adev->psp, &info, enable);
544 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
545 enable ? "enable":"disable",
546 ras_block_str(head->block),
548 if (ret == TA_RAS_STATUS__RESET_NEEDED)
554 __amdgpu_ras_feature_enable(adev, head, enable);
559 /* Only used in device probe stage and called only once. */
560 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
561 struct ras_common_if *head, bool enable)
563 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
569 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
571 /* There is no harm to issue a ras TA cmd regardless of
572 * the currecnt ras state.
573 * If current state == target state, it will do nothing
574 * But sometimes it requests driver to reset and repost
575 * with error code -EAGAIN.
577 ret = amdgpu_ras_feature_enable(adev, head, 1);
578 /* With old ras TA, we might fail to enable ras.
579 * Log it and just setup the object.
580 * TODO need remove this WA in the future.
582 if (ret == -EINVAL) {
583 ret = __amdgpu_ras_feature_enable(adev, head, 1);
585 DRM_INFO("RAS INFO: %s setup object\n",
586 ras_block_str(head->block));
589 /* setup the object then issue a ras TA disable cmd.*/
590 ret = __amdgpu_ras_feature_enable(adev, head, 1);
594 ret = amdgpu_ras_feature_enable(adev, head, 0);
597 ret = amdgpu_ras_feature_enable(adev, head, enable);
602 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
605 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
606 struct ras_manager *obj, *tmp;
608 list_for_each_entry_safe(obj, tmp, &con->head, node) {
610 * aka just release the obj and corresponding flags
613 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
616 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
621 return con->features;
624 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
627 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
628 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
630 const enum amdgpu_ras_error_type default_ras_type =
631 AMDGPU_RAS_ERROR__NONE;
633 for (i = 0; i < ras_block_count; i++) {
634 struct ras_common_if head = {
636 .type = default_ras_type,
637 .sub_block_index = 0,
639 strcpy(head.name, ras_block_str(i));
642 * bypass psp. vbios enable ras for us.
643 * so just create the obj
645 if (__amdgpu_ras_feature_enable(adev, &head, 1))
648 if (amdgpu_ras_feature_enable(adev, &head, 1))
653 return con->features;
655 /* feature ctl end */
657 /* query/inject/cure begin */
658 int amdgpu_ras_error_query(struct amdgpu_device *adev,
659 struct ras_query_if *info)
661 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
665 /* TODO might read the register to read the count */
667 info->ue_count = obj->err_data.ue_count;
668 info->ce_count = obj->err_data.ce_count;
673 /* wrapper of psp_ras_trigger_error */
674 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
675 struct ras_inject_if *info)
677 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
678 struct ta_ras_trigger_error_input block_info = {
679 .block_id = amdgpu_ras_block_to_ta(info->head.block),
680 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
681 .sub_block_index = info->head.sub_block_index,
682 .address = info->address,
683 .value = info->value,
690 ret = psp_ras_trigger_error(&adev->psp, &block_info);
692 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
693 ras_block_str(info->head.block),
699 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
700 struct ras_cure_if *info)
702 /* psp fw has no cure interface for now. */
706 /* get the total error counts on all IPs */
707 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
710 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
711 struct ras_manager *obj;
712 struct ras_err_data data = {0, 0};
717 list_for_each_entry(obj, &con->head, node) {
718 struct ras_query_if info = {
722 if (amdgpu_ras_error_query(adev, &info))
725 data.ce_count += info.ce_count;
726 data.ue_count += info.ue_count;
729 return is_ce ? data.ce_count : data.ue_count;
731 /* query/inject/cure end */
736 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
737 struct ras_badpage **bps, unsigned int *count);
739 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
753 * DOC: ras sysfs gpu_vram_bad_pages interface
755 * It allows user to read the bad pages of vram on the gpu through
756 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
758 * It outputs multiple lines, and each line stands for one gpu page.
760 * The format of one line is below,
761 * gpu pfn : gpu page size : flags
763 * gpu pfn and gpu page size are printed in hex format.
764 * flags can be one of below character,
765 * R: reserved, this gpu page is reserved and not able to use.
766 * P: pending for reserve, this gpu page is marked as bad, will be reserved
767 * in next window of page_reserve.
768 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
771 * 0x00000001 : 0x00001000 : R
772 * 0x00000002 : 0x00001000 : P
775 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
776 struct kobject *kobj, struct bin_attribute *attr,
777 char *buf, loff_t ppos, size_t count)
779 struct amdgpu_ras *con =
780 container_of(attr, struct amdgpu_ras, badpages_attr);
781 struct amdgpu_device *adev = con->adev;
782 const unsigned int element_size =
783 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
784 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
785 unsigned int end = div64_ul(ppos + count - 1, element_size);
787 struct ras_badpage *bps = NULL;
788 unsigned int bps_count = 0;
790 memset(buf, 0, count);
792 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
795 for (; start < end && start < bps_count; start++)
796 s += scnprintf(&buf[s], element_size + 1,
797 "0x%08x : 0x%08x : %1s\n",
800 amdgpu_ras_badpage_flags_str(bps[start].flags));
807 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
808 struct device_attribute *attr, char *buf)
810 struct amdgpu_ras *con =
811 container_of(attr, struct amdgpu_ras, features_attr);
812 struct drm_device *ddev = dev_get_drvdata(dev);
813 struct amdgpu_device *adev = ddev->dev_private;
814 struct ras_common_if head;
815 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
818 struct ras_manager *obj;
820 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
822 for (i = 0; i < ras_block_count; i++) {
825 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
826 obj = amdgpu_ras_find_obj(adev, &head);
827 s += scnprintf(&buf[s], PAGE_SIZE - s,
830 ras_err_str(obj->head.type));
832 s += scnprintf(&buf[s], PAGE_SIZE - s,
840 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
842 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
843 struct attribute *attrs[] = {
844 &con->features_attr.attr,
847 struct bin_attribute *bin_attrs[] = {
851 struct attribute_group group = {
854 .bin_attrs = bin_attrs,
857 con->features_attr = (struct device_attribute) {
862 .show = amdgpu_ras_sysfs_features_read,
865 con->badpages_attr = (struct bin_attribute) {
867 .name = "gpu_vram_bad_pages",
872 .read = amdgpu_ras_sysfs_badpages_read,
875 sysfs_attr_init(attrs[0]);
876 sysfs_bin_attr_init(bin_attrs[0]);
878 return sysfs_create_group(&adev->dev->kobj, &group);
881 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
883 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
884 struct attribute *attrs[] = {
885 &con->features_attr.attr,
888 struct bin_attribute *bin_attrs[] = {
892 struct attribute_group group = {
895 .bin_attrs = bin_attrs,
898 sysfs_remove_group(&adev->dev->kobj, &group);
903 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
904 struct ras_fs_if *head)
906 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
908 if (!obj || obj->attr_inuse)
913 memcpy(obj->fs_data.sysfs_name,
915 sizeof(obj->fs_data.sysfs_name));
917 obj->sysfs_attr = (struct device_attribute){
919 .name = obj->fs_data.sysfs_name,
922 .show = amdgpu_ras_sysfs_read,
924 sysfs_attr_init(&obj->sysfs_attr.attr);
926 if (sysfs_add_file_to_group(&adev->dev->kobj,
927 &obj->sysfs_attr.attr,
938 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
939 struct ras_common_if *head)
941 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
943 if (!obj || !obj->attr_inuse)
946 sysfs_remove_file_from_group(&adev->dev->kobj,
947 &obj->sysfs_attr.attr,
955 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
957 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
958 struct ras_manager *obj, *tmp;
960 list_for_each_entry_safe(obj, tmp, &con->head, node) {
961 amdgpu_ras_sysfs_remove(adev, &obj->head);
964 amdgpu_ras_sysfs_remove_feature_node(adev);
971 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
973 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
974 struct drm_minor *minor = adev->ddev->primary;
976 con->dir = debugfs_create_dir("ras", minor->debugfs_root);
977 con->ent = debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
978 adev, &amdgpu_ras_debugfs_ctrl_ops);
981 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
982 struct ras_fs_if *head)
984 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
985 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
987 if (!obj || obj->ent)
992 memcpy(obj->fs_data.debugfs_name,
994 sizeof(obj->fs_data.debugfs_name));
996 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
997 S_IWUGO | S_IRUGO, con->dir, obj,
998 &amdgpu_ras_debugfs_ops);
1001 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1002 struct ras_common_if *head)
1004 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1006 if (!obj || !obj->ent)
1009 debugfs_remove(obj->ent);
1014 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1016 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1017 struct ras_manager *obj, *tmp;
1019 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1020 amdgpu_ras_debugfs_remove(adev, &obj->head);
1023 debugfs_remove(con->ent);
1024 debugfs_remove(con->dir);
1032 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1034 amdgpu_ras_sysfs_create_feature_node(adev);
1035 amdgpu_ras_debugfs_create_ctrl_node(adev);
1040 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1042 amdgpu_ras_debugfs_remove_all(adev);
1043 amdgpu_ras_sysfs_remove_all(adev);
1049 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1051 struct ras_ih_data *data = &obj->ih_data;
1052 struct amdgpu_iv_entry entry;
1055 while (data->rptr != data->wptr) {
1057 memcpy(&entry, &data->ring[data->rptr],
1058 data->element_size);
1061 data->rptr = (data->aligned_element_size +
1062 data->rptr) % data->ring_size;
1064 /* Let IP handle its data, maybe we need get the output
1065 * from the callback to udpate the error type/count, etc
1068 ret = data->cb(obj->adev, &entry);
1069 /* ue will trigger an interrupt, and in that case
1070 * we need do a reset to recovery the whole system.
1071 * But leave IP do that recovery, here we just dispatch
1074 if (ret == AMDGPU_RAS_UE) {
1075 obj->err_data.ue_count++;
1077 /* Might need get ce count by register, but not all IP
1078 * saves ce count, some IP just use one bit or two bits
1079 * to indicate ce happened.
1085 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1087 struct ras_ih_data *data =
1088 container_of(work, struct ras_ih_data, ih_work);
1089 struct ras_manager *obj =
1090 container_of(data, struct ras_manager, ih_data);
1092 amdgpu_ras_interrupt_handler(obj);
1095 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1096 struct ras_dispatch_if *info)
1098 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1099 struct ras_ih_data *data = &obj->ih_data;
1104 if (data->inuse == 0)
1107 /* Might be overflow... */
1108 memcpy(&data->ring[data->wptr], info->entry,
1109 data->element_size);
1112 data->wptr = (data->aligned_element_size +
1113 data->wptr) % data->ring_size;
1115 schedule_work(&data->ih_work);
1120 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1121 struct ras_ih_if *info)
1123 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1124 struct ras_ih_data *data;
1129 data = &obj->ih_data;
1130 if (data->inuse == 0)
1133 cancel_work_sync(&data->ih_work);
1136 memset(data, 0, sizeof(*data));
1142 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1143 struct ras_ih_if *info)
1145 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1146 struct ras_ih_data *data;
1149 /* in case we registe the IH before enable ras feature */
1150 obj = amdgpu_ras_create_obj(adev, &info->head);
1156 data = &obj->ih_data;
1157 /* add the callback.etc */
1158 *data = (struct ras_ih_data) {
1161 .element_size = sizeof(struct amdgpu_iv_entry),
1166 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1168 data->aligned_element_size = ALIGN(data->element_size, 8);
1169 /* the ring can store 64 iv entries. */
1170 data->ring_size = 64 * data->aligned_element_size;
1171 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1183 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1185 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1186 struct ras_manager *obj, *tmp;
1188 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1189 struct ras_ih_if info = {
1192 amdgpu_ras_interrupt_remove_handler(adev, &info);
1199 /* recovery begin */
1201 /* return 0 on success.
1202 * caller need free bps.
1204 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1205 struct ras_badpage **bps, unsigned int *count)
1207 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1208 struct ras_err_handler_data *data;
1212 if (!con || !con->eh_data || !bps || !count)
1215 mutex_lock(&con->recovery_lock);
1216 data = con->eh_data;
1217 if (!data || data->count == 0) {
1222 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1228 for (; i < data->count; i++) {
1229 (*bps)[i] = (struct ras_badpage){
1230 .bp = data->bps[i].bp,
1231 .size = AMDGPU_GPU_PAGE_SIZE,
1235 if (data->last_reserved <= i)
1236 (*bps)[i].flags = 1;
1237 else if (data->bps[i].bo == NULL)
1238 (*bps)[i].flags = 2;
1241 *count = data->count;
1243 mutex_unlock(&con->recovery_lock);
1247 static void amdgpu_ras_do_recovery(struct work_struct *work)
1249 struct amdgpu_ras *ras =
1250 container_of(work, struct amdgpu_ras, recovery_work);
1252 amdgpu_device_gpu_recover(ras->adev, 0);
1253 atomic_set(&ras->in_recovery, 0);
1256 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1257 struct amdgpu_bo **bo_ptr)
1259 /* no need to free it actually. */
1260 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1264 /* reserve vram with size@offset */
1265 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1266 uint64_t offset, uint64_t size,
1267 struct amdgpu_bo **bo_ptr)
1269 struct ttm_operation_ctx ctx = { false, false };
1270 struct amdgpu_bo_param bp;
1273 struct amdgpu_bo *bo;
1277 memset(&bp, 0, sizeof(bp));
1279 bp.byte_align = PAGE_SIZE;
1280 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1281 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1282 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1283 bp.type = ttm_bo_type_kernel;
1286 r = amdgpu_bo_create(adev, &bp, &bo);
1290 r = amdgpu_bo_reserve(bo, false);
1294 offset = ALIGN(offset, PAGE_SIZE);
1295 for (i = 0; i < bo->placement.num_placement; ++i) {
1296 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1297 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1300 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1301 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1305 r = amdgpu_bo_pin_restricted(bo,
1306 AMDGPU_GEM_DOMAIN_VRAM,
1315 amdgpu_bo_unreserve(bo);
1319 amdgpu_bo_unreserve(bo);
1321 amdgpu_bo_unref(&bo);
1325 /* alloc/realloc bps array */
1326 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1327 struct ras_err_handler_data *data, int pages)
1329 unsigned int old_space = data->count + data->space_left;
1330 unsigned int new_space = old_space + pages;
1331 unsigned int align_space = ALIGN(new_space, 1024);
1332 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1338 memcpy(tmp, data->bps,
1339 data->count * sizeof(*data->bps));
1344 data->space_left += align_space - old_space;
1348 /* it deal with vram only. */
1349 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1350 unsigned long *bps, int pages)
1352 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1353 struct ras_err_handler_data *data;
1357 if (!con || !con->eh_data || !bps || pages <= 0)
1360 mutex_lock(&con->recovery_lock);
1361 data = con->eh_data;
1365 if (data->space_left <= pages)
1366 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1372 data->bps[data->count++].bp = bps[i];
1374 data->space_left -= pages;
1376 mutex_unlock(&con->recovery_lock);
1381 /* called in gpu recovery/init */
1382 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1384 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1385 struct ras_err_handler_data *data;
1387 struct amdgpu_bo *bo;
1390 if (!con || !con->eh_data)
1393 mutex_lock(&con->recovery_lock);
1394 data = con->eh_data;
1397 /* reserve vram at driver post stage. */
1398 for (i = data->last_reserved; i < data->count; i++) {
1399 bp = data->bps[i].bp;
1401 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1403 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1405 data->bps[i].bo = bo;
1406 data->last_reserved = i + 1;
1409 mutex_unlock(&con->recovery_lock);
1413 /* called when driver unload */
1414 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1416 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1417 struct ras_err_handler_data *data;
1418 struct amdgpu_bo *bo;
1421 if (!con || !con->eh_data)
1424 mutex_lock(&con->recovery_lock);
1425 data = con->eh_data;
1429 for (i = data->last_reserved - 1; i >= 0; i--) {
1430 bo = data->bps[i].bo;
1432 amdgpu_ras_release_vram(adev, &bo);
1434 data->bps[i].bo = bo;
1435 data->last_reserved = i;
1438 mutex_unlock(&con->recovery_lock);
1442 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1445 * write the array to eeprom when SMU disabled.
1450 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1453 * read the array to eeprom when SMU disabled.
1458 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1460 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1461 struct ras_err_handler_data **data = &con->eh_data;
1463 *data = kmalloc(sizeof(**data),
1464 GFP_KERNEL|__GFP_ZERO);
1468 mutex_init(&con->recovery_lock);
1469 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1470 atomic_set(&con->in_recovery, 0);
1473 amdgpu_ras_load_bad_pages(adev);
1474 amdgpu_ras_reserve_bad_pages(adev);
1479 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1481 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1482 struct ras_err_handler_data *data = con->eh_data;
1484 cancel_work_sync(&con->recovery_work);
1485 amdgpu_ras_save_bad_pages(adev);
1486 amdgpu_ras_release_bad_pages(adev);
1488 mutex_lock(&con->recovery_lock);
1489 con->eh_data = NULL;
1492 mutex_unlock(&con->recovery_lock);
1498 /* return 0 if ras will reset gpu and repost.*/
1499 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1502 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1507 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1512 * check hardware's ras ability which will be saved in hw_supported.
1513 * if hardware does not support ras, we can skip some ras initializtion and
1514 * forbid some ras operations from IP.
1515 * if software itself, say boot parameter, limit the ras ability. We still
1516 * need allow IP do some limited operations, like disable. In such case,
1517 * we have to initialize ras as normal. but need check if operation is
1518 * allowed or not in each function.
1520 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1521 uint32_t *hw_supported, uint32_t *supported)
1526 if (amdgpu_sriov_vf(adev) ||
1527 adev->asic_type != CHIP_VEGA20)
1530 if (adev->is_atom_fw &&
1531 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1532 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1533 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1535 *supported = amdgpu_ras_enable == 0 ?
1536 0 : *hw_supported & amdgpu_ras_mask;
1539 int amdgpu_ras_init(struct amdgpu_device *adev)
1541 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1546 con = kmalloc(sizeof(struct amdgpu_ras) +
1547 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1548 GFP_KERNEL|__GFP_ZERO);
1552 con->objs = (struct ras_manager *)(con + 1);
1554 amdgpu_ras_set_context(adev, con);
1556 amdgpu_ras_check_supported(adev, &con->hw_supported,
1559 INIT_LIST_HEAD(&con->head);
1560 /* Might need get this flag from vbios. */
1561 con->flags = RAS_DEFAULT_FLAGS;
1563 if (amdgpu_ras_recovery_init(adev))
1566 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1568 if (amdgpu_ras_fs_init(adev))
1571 amdgpu_ras_self_test(adev);
1573 DRM_INFO("RAS INFO: ras initialized successfully, "
1574 "hardware ability[%x] ras_mask[%x]\n",
1575 con->hw_supported, con->supported);
1578 amdgpu_ras_recovery_fini(adev);
1580 amdgpu_ras_set_context(adev, NULL);
1586 /* do some init work after IP late init as dependence.
1587 * and it runs in resume/gpu reset/booting up cases.
1589 void amdgpu_ras_resume(struct amdgpu_device *adev)
1591 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1592 struct ras_manager *obj, *tmp;
1597 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1598 /* Set up all other IPs which are not implemented. There is a
1599 * tricky thing that IP's actual ras error type should be
1600 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1601 * ERROR_NONE make sense anyway.
1603 amdgpu_ras_enable_all_features(adev, 1);
1605 /* We enable ras on all hw_supported block, but as boot
1606 * parameter might disable some of them and one or more IP has
1607 * not implemented yet. So we disable them on behalf.
1609 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1610 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1611 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1612 /* there should be no any reference. */
1613 WARN_ON(alive_obj(obj));
1618 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1619 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1620 /* setup ras obj state as disabled.
1621 * for init_by_vbios case.
1622 * if we want to enable ras, just enable it in a normal way.
1623 * If we want do disable it, need setup ras obj as enabled,
1624 * then issue another TA disable cmd.
1625 * See feature_enable_on_boot
1627 amdgpu_ras_disable_all_features(adev, 1);
1628 amdgpu_ras_reset_gpu(adev, 0);
1632 void amdgpu_ras_suspend(struct amdgpu_device *adev)
1634 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1639 amdgpu_ras_disable_all_features(adev, 0);
1640 /* Make sure all ras objects are disabled. */
1642 amdgpu_ras_disable_all_features(adev, 1);
1645 /* do some fini work before IP fini as dependence */
1646 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1648 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1653 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1654 amdgpu_ras_disable_all_features(adev, 0);
1655 amdgpu_ras_recovery_fini(adev);
1659 int amdgpu_ras_fini(struct amdgpu_device *adev)
1661 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1666 amdgpu_ras_fs_fini(adev);
1667 amdgpu_ras_interrupt_remove_all(adev);
1669 WARN(con->features, "Feature mask is not cleared");
1672 amdgpu_ras_disable_all_features(adev, 1);
1674 amdgpu_ras_set_context(adev, NULL);