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"
34 /* interrupt bottom half */
35 struct work_struct ih_work;
41 unsigned int ring_size;
42 unsigned int element_size;
43 unsigned int aligned_element_size;
50 char debugfs_name[32];
54 unsigned long ue_count;
55 unsigned long ce_count;
58 struct ras_err_handler_data {
59 /* point to bad pages array */
64 /* the count of entries */
66 /* the space can place new entries */
68 /* last reserved entry's index + 1 */
73 struct ras_common_if head;
77 struct list_head node;
79 struct amdgpu_device *adev;
83 struct device_attribute sysfs_attr;
87 struct ras_fs_data fs_data;
90 struct ras_ih_data ih_data;
92 struct ras_err_data err_data;
101 const char *ras_error_string[] = {
104 "single_correctable",
105 "multi_uncorrectable",
109 const char *ras_block_string[] = {
126 #define ras_err_str(i) (ras_error_string[ffs(i)])
127 #define ras_block_str(i) (ras_block_string[i])
129 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
130 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET 2
131 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
133 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
134 uint64_t offset, uint64_t size,
135 struct amdgpu_bo **bo_ptr);
136 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
137 struct amdgpu_bo **bo_ptr);
139 static void amdgpu_ras_self_test(struct amdgpu_device *adev)
144 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
145 size_t size, loff_t *pos)
147 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
148 struct ras_query_if info = {
154 if (amdgpu_ras_error_query(obj->adev, &info))
157 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
159 "ce", info.ce_count);
164 s = min_t(u64, s, size);
167 if (copy_to_user(buf, &val[*pos], s))
175 static const struct file_operations amdgpu_ras_debugfs_ops = {
176 .owner = THIS_MODULE,
177 .read = amdgpu_ras_debugfs_read,
179 .llseek = default_llseek
182 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
186 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
188 if (strcmp(name, ras_block_str(i)) == 0)
194 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
195 const char __user *buf, size_t size,
196 loff_t *pos, struct ras_debug_if *data)
198 ssize_t s = min_t(u64, 64, size);
210 memset(str, 0, sizeof(str));
211 memset(data, 0, sizeof(*data));
213 if (copy_from_user(str, buf, s))
216 if (sscanf(str, "disable %32s", block_name) == 1)
218 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
220 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
222 else if (str[0] && str[1] && str[2] && str[3])
223 /* ascii string, but commands are not matched. */
227 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
230 data->head.block = block_id;
231 data->head.type = memcmp("ue", err, 2) == 0 ?
232 AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE :
233 AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
237 if (sscanf(str, "%*s %*s %*s %llu %llu",
238 &address, &value) != 2)
239 if (sscanf(str, "%*s %*s %*s 0x%llx 0x%llx",
240 &address, &value) != 2)
242 data->inject.address = address;
243 data->inject.value = value;
246 if (size < sizeof(*data))
249 if (copy_from_user(data, buf, sizeof(*data)))
256 * DOC: AMDGPU RAS debugfs control interface
258 * It accepts struct ras_debug_if who has two members.
260 * First member: ras_debug_if::head or ras_debug_if::inject.
262 * head is used to indicate which IP block will be under control.
264 * head has four members, they are block, type, sub_block_index, name.
265 * block: which IP will be under control.
266 * type: what kind of error will be enabled/disabled/injected.
267 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
268 * name: the name of IP.
270 * inject has two more members than head, they are address, value.
271 * As their names indicate, inject operation will write the
272 * value to the address.
274 * Second member: struct ras_debug_if::op.
275 * It has three kinds of operations.
276 * 0: disable RAS on the block. Take ::head as its data.
277 * 1: enable RAS on the block. Take ::head as its data.
278 * 2: inject errors on the block. Take ::inject as its data.
280 * How to use the interface?
282 * copy the struct ras_debug_if in your codes and initialize it.
283 * write the struct to the control node.
286 * echo op block [error [address value]] > .../ras/ras_ctrl
287 * op: disable, enable, inject
288 * disable: only block is needed
289 * enable: block and error are needed
290 * inject: error, address, value are needed
291 * block: umc, smda, gfx, .........
292 * see ras_block_string[] for details
294 * ue: multi_uncorrectable
295 * ce: single_correctable
297 * here are some examples for bash commands,
298 * echo inject umc ue 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
299 * echo inject umc ce 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
300 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
302 * How to check the result?
304 * For disable/enable, please check ras features at
305 * /sys/class/drm/card[0/1/2...]/device/ras/features
307 * For inject, please check corresponding err count at
308 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
310 * NOTE: operation is only allowed on blocks which are supported.
311 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
313 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
314 size_t size, loff_t *pos)
316 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
317 struct ras_debug_if data;
318 struct amdgpu_bo *bo;
321 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
325 if (!amdgpu_ras_is_supported(adev, data.head.block))
330 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
333 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
336 ret = amdgpu_ras_reserve_vram(adev,
337 data.inject.address, PAGE_SIZE, &bo);
338 /* This address might be used already on failure. In fact we can
339 * perform an injection in such case.
343 data.inject.address = amdgpu_bo_gpu_offset(bo);
344 ret = amdgpu_ras_error_inject(adev, &data.inject);
345 amdgpu_ras_release_vram(adev, &bo);
358 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
359 .owner = THIS_MODULE,
361 .write = amdgpu_ras_debugfs_ctrl_write,
362 .llseek = default_llseek
365 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
366 struct device_attribute *attr, char *buf)
368 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
369 struct ras_query_if info = {
373 if (amdgpu_ras_error_query(obj->adev, &info))
376 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
378 "ce", info.ce_count);
383 #define get_obj(obj) do { (obj)->use++; } while (0)
384 #define alive_obj(obj) ((obj)->use)
386 static inline void put_obj(struct ras_manager *obj)
388 if (obj && --obj->use == 0)
389 list_del(&obj->node);
390 if (obj && obj->use < 0) {
391 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
395 /* make one obj and return it. */
396 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
397 struct ras_common_if *head)
399 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
400 struct ras_manager *obj;
405 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
408 obj = &con->objs[head->block];
409 /* already exist. return obj? */
415 list_add(&obj->node, &con->head);
421 /* return an obj equal to head, or the first when head is NULL */
422 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
423 struct ras_common_if *head)
425 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
426 struct ras_manager *obj;
433 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
436 obj = &con->objs[head->block];
438 if (alive_obj(obj)) {
439 WARN_ON(head->block != obj->head.block);
443 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
445 if (alive_obj(obj)) {
446 WARN_ON(i != obj->head.block);
456 /* feature ctl begin */
457 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
458 struct ras_common_if *head)
460 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
462 return con->hw_supported & BIT(head->block);
465 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
466 struct ras_common_if *head)
468 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
470 return con->features & BIT(head->block);
474 * if obj is not created, then create one.
475 * set feature enable flag.
477 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
478 struct ras_common_if *head, int enable)
480 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
481 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
483 /* If hardware does not support ras, then do not create obj.
484 * But if hardware support ras, we can create the obj.
485 * Ras framework checks con->hw_supported to see if it need do
486 * corresponding initialization.
487 * IP checks con->support to see if it need disable ras.
489 if (!amdgpu_ras_is_feature_allowed(adev, head))
491 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
496 obj = amdgpu_ras_create_obj(adev, head);
500 /* In case we create obj somewhere else */
503 con->features |= BIT(head->block);
505 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
506 con->features &= ~BIT(head->block);
514 /* wrapper of psp_ras_enable_features */
515 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
516 struct ras_common_if *head, bool enable)
518 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
519 union ta_ras_cmd_input info;
526 info.disable_features = (struct ta_ras_disable_features_input) {
527 .block_id = amdgpu_ras_block_to_ta(head->block),
528 .error_type = amdgpu_ras_error_to_ta(head->type),
531 info.enable_features = (struct ta_ras_enable_features_input) {
532 .block_id = amdgpu_ras_block_to_ta(head->block),
533 .error_type = amdgpu_ras_error_to_ta(head->type),
537 /* Do not enable if it is not allowed. */
538 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
539 /* Are we alerady in that state we are going to set? */
540 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
543 ret = psp_ras_enable_features(&adev->psp, &info, enable);
545 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
546 enable ? "enable":"disable",
547 ras_block_str(head->block),
549 if (ret == TA_RAS_STATUS__RESET_NEEDED)
555 __amdgpu_ras_feature_enable(adev, head, enable);
560 /* Only used in device probe stage and called only once. */
561 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
562 struct ras_common_if *head, bool enable)
564 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
570 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
572 /* There is no harm to issue a ras TA cmd regardless of
573 * the currecnt ras state.
574 * If current state == target state, it will do nothing
575 * But sometimes it requests driver to reset and repost
576 * with error code -EAGAIN.
578 ret = amdgpu_ras_feature_enable(adev, head, 1);
579 /* With old ras TA, we might fail to enable ras.
580 * Log it and just setup the object.
581 * TODO need remove this WA in the future.
583 if (ret == -EINVAL) {
584 ret = __amdgpu_ras_feature_enable(adev, head, 1);
586 DRM_INFO("RAS INFO: %s setup object\n",
587 ras_block_str(head->block));
590 /* setup the object then issue a ras TA disable cmd.*/
591 ret = __amdgpu_ras_feature_enable(adev, head, 1);
595 ret = amdgpu_ras_feature_enable(adev, head, 0);
598 ret = amdgpu_ras_feature_enable(adev, head, enable);
603 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
606 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
607 struct ras_manager *obj, *tmp;
609 list_for_each_entry_safe(obj, tmp, &con->head, node) {
611 * aka just release the obj and corresponding flags
614 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
617 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
622 return con->features;
625 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
628 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
629 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
631 const enum amdgpu_ras_error_type default_ras_type =
632 AMDGPU_RAS_ERROR__NONE;
634 for (i = 0; i < ras_block_count; i++) {
635 struct ras_common_if head = {
637 .type = default_ras_type,
638 .sub_block_index = 0,
640 strcpy(head.name, ras_block_str(i));
643 * bypass psp. vbios enable ras for us.
644 * so just create the obj
646 if (__amdgpu_ras_feature_enable(adev, &head, 1))
649 if (amdgpu_ras_feature_enable(adev, &head, 1))
654 return con->features;
656 /* feature ctl end */
658 /* query/inject/cure begin */
659 int amdgpu_ras_error_query(struct amdgpu_device *adev,
660 struct ras_query_if *info)
662 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
666 /* TODO might read the register to read the count */
668 info->ue_count = obj->err_data.ue_count;
669 info->ce_count = obj->err_data.ce_count;
674 /* wrapper of psp_ras_trigger_error */
675 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
676 struct ras_inject_if *info)
678 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
679 struct ta_ras_trigger_error_input block_info = {
680 .block_id = amdgpu_ras_block_to_ta(info->head.block),
681 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
682 .sub_block_index = info->head.sub_block_index,
683 .address = info->address,
684 .value = info->value,
691 ret = psp_ras_trigger_error(&adev->psp, &block_info);
693 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
694 ras_block_str(info->head.block),
700 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
701 struct ras_cure_if *info)
703 /* psp fw has no cure interface for now. */
707 /* get the total error counts on all IPs */
708 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
711 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
712 struct ras_manager *obj;
713 struct ras_err_data data = {0, 0};
718 list_for_each_entry(obj, &con->head, node) {
719 struct ras_query_if info = {
723 if (amdgpu_ras_error_query(adev, &info))
726 data.ce_count += info.ce_count;
727 data.ue_count += info.ue_count;
730 return is_ce ? data.ce_count : data.ue_count;
732 /* query/inject/cure end */
737 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
738 struct ras_badpage **bps, unsigned int *count);
740 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
754 * DOC: ras sysfs gpu_vram_bad_pages interface
756 * It allows user to read the bad pages of vram on the gpu through
757 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
759 * It outputs multiple lines, and each line stands for one gpu page.
761 * The format of one line is below,
762 * gpu pfn : gpu page size : flags
764 * gpu pfn and gpu page size are printed in hex format.
765 * flags can be one of below character,
766 * R: reserved, this gpu page is reserved and not able to use.
767 * P: pending for reserve, this gpu page is marked as bad, will be reserved
768 * in next window of page_reserve.
769 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
772 * 0x00000001 : 0x00001000 : R
773 * 0x00000002 : 0x00001000 : P
776 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
777 struct kobject *kobj, struct bin_attribute *attr,
778 char *buf, loff_t ppos, size_t count)
780 struct amdgpu_ras *con =
781 container_of(attr, struct amdgpu_ras, badpages_attr);
782 struct amdgpu_device *adev = con->adev;
783 const unsigned int element_size =
784 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
785 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
786 unsigned int end = div64_ul(ppos + count - 1, element_size);
788 struct ras_badpage *bps = NULL;
789 unsigned int bps_count = 0;
791 memset(buf, 0, count);
793 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
796 for (; start < end && start < bps_count; start++)
797 s += scnprintf(&buf[s], element_size + 1,
798 "0x%08x : 0x%08x : %1s\n",
801 amdgpu_ras_badpage_flags_str(bps[start].flags));
808 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
809 struct device_attribute *attr, char *buf)
811 struct amdgpu_ras *con =
812 container_of(attr, struct amdgpu_ras, features_attr);
813 struct drm_device *ddev = dev_get_drvdata(dev);
814 struct amdgpu_device *adev = ddev->dev_private;
815 struct ras_common_if head;
816 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
819 struct ras_manager *obj;
821 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
823 for (i = 0; i < ras_block_count; i++) {
826 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
827 obj = amdgpu_ras_find_obj(adev, &head);
828 s += scnprintf(&buf[s], PAGE_SIZE - s,
831 ras_err_str(obj->head.type));
833 s += scnprintf(&buf[s], PAGE_SIZE - s,
841 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
843 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
844 struct attribute *attrs[] = {
845 &con->features_attr.attr,
848 struct bin_attribute *bin_attrs[] = {
852 struct attribute_group group = {
855 .bin_attrs = bin_attrs,
858 con->features_attr = (struct device_attribute) {
863 .show = amdgpu_ras_sysfs_features_read,
866 con->badpages_attr = (struct bin_attribute) {
868 .name = "gpu_vram_bad_pages",
873 .read = amdgpu_ras_sysfs_badpages_read,
876 sysfs_attr_init(attrs[0]);
877 sysfs_bin_attr_init(bin_attrs[0]);
879 return sysfs_create_group(&adev->dev->kobj, &group);
882 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
884 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
885 struct attribute *attrs[] = {
886 &con->features_attr.attr,
889 struct bin_attribute *bin_attrs[] = {
893 struct attribute_group group = {
896 .bin_attrs = bin_attrs,
899 sysfs_remove_group(&adev->dev->kobj, &group);
904 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
905 struct ras_fs_if *head)
907 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
909 if (!obj || obj->attr_inuse)
914 memcpy(obj->fs_data.sysfs_name,
916 sizeof(obj->fs_data.sysfs_name));
918 obj->sysfs_attr = (struct device_attribute){
920 .name = obj->fs_data.sysfs_name,
923 .show = amdgpu_ras_sysfs_read,
925 sysfs_attr_init(&obj->sysfs_attr.attr);
927 if (sysfs_add_file_to_group(&adev->dev->kobj,
928 &obj->sysfs_attr.attr,
939 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
940 struct ras_common_if *head)
942 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
944 if (!obj || !obj->attr_inuse)
947 sysfs_remove_file_from_group(&adev->dev->kobj,
948 &obj->sysfs_attr.attr,
956 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
958 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
959 struct ras_manager *obj, *tmp;
961 list_for_each_entry_safe(obj, tmp, &con->head, node) {
962 amdgpu_ras_sysfs_remove(adev, &obj->head);
965 amdgpu_ras_sysfs_remove_feature_node(adev);
972 static int amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
974 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
975 struct drm_minor *minor = adev->ddev->primary;
976 struct dentry *root = minor->debugfs_root, *dir;
979 dir = debugfs_create_dir("ras", root);
985 ent = debugfs_create_file("ras_ctrl",
986 S_IWUGO | S_IRUGO, con->dir,
987 adev, &amdgpu_ras_debugfs_ctrl_ops);
989 debugfs_remove(con->dir);
997 int amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
998 struct ras_fs_if *head)
1000 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1001 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1004 if (!obj || obj->ent)
1009 memcpy(obj->fs_data.debugfs_name,
1011 sizeof(obj->fs_data.debugfs_name));
1013 ent = debugfs_create_file(obj->fs_data.debugfs_name,
1014 S_IWUGO | S_IRUGO, con->dir,
1015 obj, &amdgpu_ras_debugfs_ops);
1025 int amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1026 struct ras_common_if *head)
1028 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1030 if (!obj || !obj->ent)
1033 debugfs_remove(obj->ent);
1040 static int amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1042 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1043 struct ras_manager *obj, *tmp;
1045 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1046 amdgpu_ras_debugfs_remove(adev, &obj->head);
1049 debugfs_remove(con->ent);
1050 debugfs_remove(con->dir);
1060 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1062 amdgpu_ras_sysfs_create_feature_node(adev);
1063 amdgpu_ras_debugfs_create_ctrl_node(adev);
1068 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1070 amdgpu_ras_debugfs_remove_all(adev);
1071 amdgpu_ras_sysfs_remove_all(adev);
1077 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1079 struct ras_ih_data *data = &obj->ih_data;
1080 struct amdgpu_iv_entry entry;
1083 while (data->rptr != data->wptr) {
1085 memcpy(&entry, &data->ring[data->rptr],
1086 data->element_size);
1089 data->rptr = (data->aligned_element_size +
1090 data->rptr) % data->ring_size;
1092 /* Let IP handle its data, maybe we need get the output
1093 * from the callback to udpate the error type/count, etc
1096 ret = data->cb(obj->adev, &entry);
1097 /* ue will trigger an interrupt, and in that case
1098 * we need do a reset to recovery the whole system.
1099 * But leave IP do that recovery, here we just dispatch
1102 if (ret == AMDGPU_RAS_UE) {
1103 obj->err_data.ue_count++;
1105 /* Might need get ce count by register, but not all IP
1106 * saves ce count, some IP just use one bit or two bits
1107 * to indicate ce happened.
1113 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1115 struct ras_ih_data *data =
1116 container_of(work, struct ras_ih_data, ih_work);
1117 struct ras_manager *obj =
1118 container_of(data, struct ras_manager, ih_data);
1120 amdgpu_ras_interrupt_handler(obj);
1123 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1124 struct ras_dispatch_if *info)
1126 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1127 struct ras_ih_data *data = &obj->ih_data;
1132 if (data->inuse == 0)
1135 /* Might be overflow... */
1136 memcpy(&data->ring[data->wptr], info->entry,
1137 data->element_size);
1140 data->wptr = (data->aligned_element_size +
1141 data->wptr) % data->ring_size;
1143 schedule_work(&data->ih_work);
1148 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1149 struct ras_ih_if *info)
1151 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1152 struct ras_ih_data *data;
1157 data = &obj->ih_data;
1158 if (data->inuse == 0)
1161 cancel_work_sync(&data->ih_work);
1164 memset(data, 0, sizeof(*data));
1170 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1171 struct ras_ih_if *info)
1173 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1174 struct ras_ih_data *data;
1177 /* in case we registe the IH before enable ras feature */
1178 obj = amdgpu_ras_create_obj(adev, &info->head);
1184 data = &obj->ih_data;
1185 /* add the callback.etc */
1186 *data = (struct ras_ih_data) {
1189 .element_size = sizeof(struct amdgpu_iv_entry),
1194 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1196 data->aligned_element_size = ALIGN(data->element_size, 8);
1197 /* the ring can store 64 iv entries. */
1198 data->ring_size = 64 * data->aligned_element_size;
1199 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1211 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1213 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1214 struct ras_manager *obj, *tmp;
1216 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1217 struct ras_ih_if info = {
1220 amdgpu_ras_interrupt_remove_handler(adev, &info);
1227 /* recovery begin */
1229 /* return 0 on success.
1230 * caller need free bps.
1232 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1233 struct ras_badpage **bps, unsigned int *count)
1235 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1236 struct ras_err_handler_data *data;
1240 if (!con || !con->eh_data || !bps || !count)
1243 mutex_lock(&con->recovery_lock);
1244 data = con->eh_data;
1245 if (!data || data->count == 0) {
1250 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1256 for (; i < data->count; i++) {
1257 (*bps)[i] = (struct ras_badpage){
1258 .bp = data->bps[i].bp,
1259 .size = AMDGPU_GPU_PAGE_SIZE,
1263 if (data->last_reserved <= i)
1264 (*bps)[i].flags = 1;
1265 else if (data->bps[i].bo == NULL)
1266 (*bps)[i].flags = 2;
1269 *count = data->count;
1271 mutex_unlock(&con->recovery_lock);
1275 static void amdgpu_ras_do_recovery(struct work_struct *work)
1277 struct amdgpu_ras *ras =
1278 container_of(work, struct amdgpu_ras, recovery_work);
1280 amdgpu_device_gpu_recover(ras->adev, 0);
1281 atomic_set(&ras->in_recovery, 0);
1284 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1285 struct amdgpu_bo **bo_ptr)
1287 /* no need to free it actually. */
1288 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1292 /* reserve vram with size@offset */
1293 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1294 uint64_t offset, uint64_t size,
1295 struct amdgpu_bo **bo_ptr)
1297 struct ttm_operation_ctx ctx = { false, false };
1298 struct amdgpu_bo_param bp;
1301 struct amdgpu_bo *bo;
1305 memset(&bp, 0, sizeof(bp));
1307 bp.byte_align = PAGE_SIZE;
1308 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1309 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1310 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1311 bp.type = ttm_bo_type_kernel;
1314 r = amdgpu_bo_create(adev, &bp, &bo);
1318 r = amdgpu_bo_reserve(bo, false);
1322 offset = ALIGN(offset, PAGE_SIZE);
1323 for (i = 0; i < bo->placement.num_placement; ++i) {
1324 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1325 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1328 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1329 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1333 r = amdgpu_bo_pin_restricted(bo,
1334 AMDGPU_GEM_DOMAIN_VRAM,
1343 amdgpu_bo_unreserve(bo);
1347 amdgpu_bo_unreserve(bo);
1349 amdgpu_bo_unref(&bo);
1353 /* alloc/realloc bps array */
1354 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1355 struct ras_err_handler_data *data, int pages)
1357 unsigned int old_space = data->count + data->space_left;
1358 unsigned int new_space = old_space + pages;
1359 unsigned int align_space = ALIGN(new_space, 1024);
1360 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1366 memcpy(tmp, data->bps,
1367 data->count * sizeof(*data->bps));
1372 data->space_left += align_space - old_space;
1376 /* it deal with vram only. */
1377 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1378 unsigned long *bps, int pages)
1380 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1381 struct ras_err_handler_data *data;
1385 if (!con || !con->eh_data || !bps || pages <= 0)
1388 mutex_lock(&con->recovery_lock);
1389 data = con->eh_data;
1393 if (data->space_left <= pages)
1394 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1400 data->bps[data->count++].bp = bps[i];
1402 data->space_left -= pages;
1404 mutex_unlock(&con->recovery_lock);
1409 /* called in gpu recovery/init */
1410 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1412 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1413 struct ras_err_handler_data *data;
1415 struct amdgpu_bo *bo;
1418 if (!con || !con->eh_data)
1421 mutex_lock(&con->recovery_lock);
1422 data = con->eh_data;
1425 /* reserve vram at driver post stage. */
1426 for (i = data->last_reserved; i < data->count; i++) {
1427 bp = data->bps[i].bp;
1429 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1431 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1433 data->bps[i].bo = bo;
1434 data->last_reserved = i + 1;
1437 mutex_unlock(&con->recovery_lock);
1441 /* called when driver unload */
1442 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1444 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1445 struct ras_err_handler_data *data;
1446 struct amdgpu_bo *bo;
1449 if (!con || !con->eh_data)
1452 mutex_lock(&con->recovery_lock);
1453 data = con->eh_data;
1457 for (i = data->last_reserved - 1; i >= 0; i--) {
1458 bo = data->bps[i].bo;
1460 amdgpu_ras_release_vram(adev, &bo);
1462 data->bps[i].bo = bo;
1463 data->last_reserved = i;
1466 mutex_unlock(&con->recovery_lock);
1470 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1473 * write the array to eeprom when SMU disabled.
1478 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1481 * read the array to eeprom when SMU disabled.
1486 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1488 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1489 struct ras_err_handler_data **data = &con->eh_data;
1491 *data = kmalloc(sizeof(**data),
1492 GFP_KERNEL|__GFP_ZERO);
1496 mutex_init(&con->recovery_lock);
1497 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1498 atomic_set(&con->in_recovery, 0);
1501 amdgpu_ras_load_bad_pages(adev);
1502 amdgpu_ras_reserve_bad_pages(adev);
1507 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1509 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1510 struct ras_err_handler_data *data = con->eh_data;
1512 cancel_work_sync(&con->recovery_work);
1513 amdgpu_ras_save_bad_pages(adev);
1514 amdgpu_ras_release_bad_pages(adev);
1516 mutex_lock(&con->recovery_lock);
1517 con->eh_data = NULL;
1520 mutex_unlock(&con->recovery_lock);
1526 /* return 0 if ras will reset gpu and repost.*/
1527 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1530 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1535 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1540 * check hardware's ras ability which will be saved in hw_supported.
1541 * if hardware does not support ras, we can skip some ras initializtion and
1542 * forbid some ras operations from IP.
1543 * if software itself, say boot parameter, limit the ras ability. We still
1544 * need allow IP do some limited operations, like disable. In such case,
1545 * we have to initialize ras as normal. but need check if operation is
1546 * allowed or not in each function.
1548 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1549 uint32_t *hw_supported, uint32_t *supported)
1554 if (amdgpu_sriov_vf(adev) ||
1555 adev->asic_type != CHIP_VEGA20)
1558 if (adev->is_atom_fw &&
1559 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1560 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1561 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1563 *supported = amdgpu_ras_enable == 0 ?
1564 0 : *hw_supported & amdgpu_ras_mask;
1567 int amdgpu_ras_init(struct amdgpu_device *adev)
1569 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1574 con = kmalloc(sizeof(struct amdgpu_ras) +
1575 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1576 GFP_KERNEL|__GFP_ZERO);
1580 con->objs = (struct ras_manager *)(con + 1);
1582 amdgpu_ras_set_context(adev, con);
1584 amdgpu_ras_check_supported(adev, &con->hw_supported,
1587 INIT_LIST_HEAD(&con->head);
1588 /* Might need get this flag from vbios. */
1589 con->flags = RAS_DEFAULT_FLAGS;
1591 if (amdgpu_ras_recovery_init(adev))
1594 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1596 if (amdgpu_ras_fs_init(adev))
1599 amdgpu_ras_self_test(adev);
1601 DRM_INFO("RAS INFO: ras initialized successfully, "
1602 "hardware ability[%x] ras_mask[%x]\n",
1603 con->hw_supported, con->supported);
1606 amdgpu_ras_recovery_fini(adev);
1608 amdgpu_ras_set_context(adev, NULL);
1614 /* do some init work after IP late init as dependence.
1615 * and it runs in resume/gpu reset/booting up cases.
1617 void amdgpu_ras_resume(struct amdgpu_device *adev)
1619 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1620 struct ras_manager *obj, *tmp;
1625 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1626 /* Set up all other IPs which are not implemented. There is a
1627 * tricky thing that IP's actual ras error type should be
1628 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1629 * ERROR_NONE make sense anyway.
1631 amdgpu_ras_enable_all_features(adev, 1);
1633 /* We enable ras on all hw_supported block, but as boot
1634 * parameter might disable some of them and one or more IP has
1635 * not implemented yet. So we disable them on behalf.
1637 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1638 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1639 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1640 /* there should be no any reference. */
1641 WARN_ON(alive_obj(obj));
1646 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1647 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1648 /* setup ras obj state as disabled.
1649 * for init_by_vbios case.
1650 * if we want to enable ras, just enable it in a normal way.
1651 * If we want do disable it, need setup ras obj as enabled,
1652 * then issue another TA disable cmd.
1653 * See feature_enable_on_boot
1655 amdgpu_ras_disable_all_features(adev, 1);
1656 amdgpu_ras_reset_gpu(adev, 0);
1660 void amdgpu_ras_suspend(struct amdgpu_device *adev)
1662 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1667 amdgpu_ras_disable_all_features(adev, 0);
1668 /* Make sure all ras objects are disabled. */
1670 amdgpu_ras_disable_all_features(adev, 1);
1673 /* do some fini work before IP fini as dependence */
1674 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1676 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1681 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1682 amdgpu_ras_disable_all_features(adev, 0);
1683 amdgpu_ras_recovery_fini(adev);
1687 int amdgpu_ras_fini(struct amdgpu_device *adev)
1689 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1694 amdgpu_ras_fs_fini(adev);
1695 amdgpu_ras_interrupt_remove_all(adev);
1697 WARN(con->features, "Feature mask is not cleared");
1700 amdgpu_ras_disable_all_features(adev, 1);
1702 amdgpu_ras_set_context(adev, NULL);