1bb7743bcSHuang Rui /*
2bb7743bcSHuang Rui  * Copyright 2018 Advanced Micro Devices, Inc.
3bb7743bcSHuang Rui  *
4bb7743bcSHuang Rui  * Permission is hereby granted, free of charge, to any person obtaining a
5bb7743bcSHuang Rui  * copy of this software and associated documentation files (the "Software"),
6bb7743bcSHuang Rui  * to deal in the Software without restriction, including without limitation
7bb7743bcSHuang Rui  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bb7743bcSHuang Rui  * and/or sell copies of the Software, and to permit persons to whom the
9bb7743bcSHuang Rui  * Software is furnished to do so, subject to the following conditions:
10bb7743bcSHuang Rui  *
11bb7743bcSHuang Rui  * The above copyright notice and this permission notice shall be included in
12bb7743bcSHuang Rui  * all copies or substantial portions of the Software.
13bb7743bcSHuang Rui  *
14bb7743bcSHuang Rui  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bb7743bcSHuang Rui  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bb7743bcSHuang Rui  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bb7743bcSHuang Rui  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18bb7743bcSHuang Rui  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19bb7743bcSHuang Rui  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20bb7743bcSHuang Rui  * OTHER DEALINGS IN THE SOFTWARE.
21bb7743bcSHuang Rui  *
22bb7743bcSHuang Rui  */
23fdf2f6c5SSam Ravnborg 
2415aa1305SLikun Gao #include <linux/firmware.h>
25bb7743bcSHuang Rui #include "amdgpu.h"
26bb7743bcSHuang Rui #include "amdgpu_sdma.h"
27bfcf62c2SHawking Zhang #include "amdgpu_ras.h"
284c02f730S[email protected] #include "amdgpu_reset.h"
29bb7743bcSHuang Rui 
30ca1eb732SJack Xiao #define AMDGPU_CSA_SDMA_SIZE 64
31ca1eb732SJack Xiao /* SDMA CSA reside in the 3rd page of CSA */
32ca1eb732SJack Xiao #define AMDGPU_CSA_SDMA_OFFSET (4096 * 2)
33ca1eb732SJack Xiao 
34bb7743bcSHuang Rui /*
35bb7743bcSHuang Rui  * GPU SDMA IP block helpers function.
36bb7743bcSHuang Rui  */
37bb7743bcSHuang Rui 
amdgpu_sdma_get_instance_from_ring(struct amdgpu_ring * ring)38ccf191f8SRex Zhu struct amdgpu_sdma_instance *amdgpu_sdma_get_instance_from_ring(struct amdgpu_ring *ring)
39bb7743bcSHuang Rui {
40bb7743bcSHuang Rui 	struct amdgpu_device *adev = ring->adev;
41bb7743bcSHuang Rui 	int i;
42bb7743bcSHuang Rui 
43bb7743bcSHuang Rui 	for (i = 0; i < adev->sdma.num_instances; i++)
449194a339SChristian König 		if (ring == &adev->sdma.instance[i].ring ||
459194a339SChristian König 		    ring == &adev->sdma.instance[i].page)
46bb7743bcSHuang Rui 			return &adev->sdma.instance[i];
479194a339SChristian König 
48bb7743bcSHuang Rui 	return NULL;
49bb7743bcSHuang Rui }
50f6cffc0dSRex Zhu 
amdgpu_sdma_get_index_from_ring(struct amdgpu_ring * ring,uint32_t * index)51f6cffc0dSRex Zhu int amdgpu_sdma_get_index_from_ring(struct amdgpu_ring *ring, uint32_t *index)
52f6cffc0dSRex Zhu {
53f6cffc0dSRex Zhu 	struct amdgpu_device *adev = ring->adev;
54f6cffc0dSRex Zhu 	int i;
55f6cffc0dSRex Zhu 
56f6cffc0dSRex Zhu 	for (i = 0; i < adev->sdma.num_instances; i++) {
57f6cffc0dSRex Zhu 		if (ring == &adev->sdma.instance[i].ring ||
58f6cffc0dSRex Zhu 			ring == &adev->sdma.instance[i].page) {
59f6cffc0dSRex Zhu 			*index = i;
60f6cffc0dSRex Zhu 			return 0;
61f6cffc0dSRex Zhu 		}
62f6cffc0dSRex Zhu 	}
63f6cffc0dSRex Zhu 
64f6cffc0dSRex Zhu 	return -EINVAL;
65f6cffc0dSRex Zhu }
66ca1eb732SJack Xiao 
amdgpu_sdma_get_csa_mc_addr(struct amdgpu_ring * ring,unsigned int vmid)67ca1eb732SJack Xiao uint64_t amdgpu_sdma_get_csa_mc_addr(struct amdgpu_ring *ring,
68e03f04b8SSrinivasan Shanmugam 				     unsigned int vmid)
69ca1eb732SJack Xiao {
70ca1eb732SJack Xiao 	struct amdgpu_device *adev = ring->adev;
71ca1eb732SJack Xiao 	uint64_t csa_mc_addr;
72ca1eb732SJack Xiao 	uint32_t index = 0;
73ca1eb732SJack Xiao 	int r;
74ca1eb732SJack Xiao 
756325b38dSMonk Liu 	/* don't enable OS preemption on SDMA under SRIOV */
7602ff519eSAlex Deucher 	if (amdgpu_sriov_vf(adev) || vmid == 0 || !adev->gfx.mcbp)
77ca1eb732SJack Xiao 		return 0;
78ca1eb732SJack Xiao 
7906589195SJack Xiao 	if (ring->is_mes_queue) {
8006589195SJack Xiao 		uint32_t offset = 0;
8106589195SJack Xiao 
8206589195SJack Xiao 		offset = offsetof(struct amdgpu_mes_ctx_meta_data,
8306589195SJack Xiao 				  sdma[ring->idx].sdma_meta_data);
8406589195SJack Xiao 		csa_mc_addr = amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
8506589195SJack Xiao 	} else {
86ca1eb732SJack Xiao 		r = amdgpu_sdma_get_index_from_ring(ring, &index);
87ca1eb732SJack Xiao 
88ca1eb732SJack Xiao 		if (r || index > 31)
89ca1eb732SJack Xiao 			csa_mc_addr = 0;
90ca1eb732SJack Xiao 		else
91ca1eb732SJack Xiao 			csa_mc_addr = amdgpu_csa_vaddr(adev) +
92ca1eb732SJack Xiao 				AMDGPU_CSA_SDMA_OFFSET +
93ca1eb732SJack Xiao 				index * AMDGPU_CSA_SDMA_SIZE;
9406589195SJack Xiao 	}
95ca1eb732SJack Xiao 
96ca1eb732SJack Xiao 	return csa_mc_addr;
97ca1eb732SJack Xiao }
98bfcf62c2SHawking Zhang 
amdgpu_sdma_ras_late_init(struct amdgpu_device * adev,struct ras_common_if * ras_block)99bfcf62c2SHawking Zhang int amdgpu_sdma_ras_late_init(struct amdgpu_device *adev,
1004e9b1fa5Syipechai 			      struct ras_common_if *ras_block)
101bfcf62c2SHawking Zhang {
102bfcf62c2SHawking Zhang 	int r, i;
103bfcf62c2SHawking Zhang 
104caae42f0Syipechai 	r = amdgpu_ras_block_late_init(adev, ras_block);
105bfcf62c2SHawking Zhang 	if (r)
106683bac6bSyipechai 		return r;
107bfcf62c2SHawking Zhang 
108caae42f0Syipechai 	if (amdgpu_ras_is_supported(adev, ras_block->block)) {
109bfcf62c2SHawking Zhang 		for (i = 0; i < adev->sdma.num_instances; i++) {
110bfcf62c2SHawking Zhang 			r = amdgpu_irq_get(adev, &adev->sdma.ecc_irq,
111bfcf62c2SHawking Zhang 				AMDGPU_SDMA_IRQ_INSTANCE0 + i);
112bfcf62c2SHawking Zhang 			if (r)
113bfcf62c2SHawking Zhang 				goto late_fini;
114bfcf62c2SHawking Zhang 		}
115bfcf62c2SHawking Zhang 	}
116bfcf62c2SHawking Zhang 
117bfcf62c2SHawking Zhang 	return 0;
118bfcf62c2SHawking Zhang 
119bfcf62c2SHawking Zhang late_fini:
120caae42f0Syipechai 	amdgpu_ras_block_late_fini(adev, ras_block);
121bfcf62c2SHawking Zhang 	return r;
122bfcf62c2SHawking Zhang }
1234c65dd10STao Zhou 
amdgpu_sdma_process_ras_data_cb(struct amdgpu_device * adev,void * err_data,struct amdgpu_iv_entry * entry)1244c65dd10STao Zhou int amdgpu_sdma_process_ras_data_cb(struct amdgpu_device *adev,
1254c65dd10STao Zhou 		void *err_data,
1264c65dd10STao Zhou 		struct amdgpu_iv_entry *entry)
1274c65dd10STao Zhou {
1284c65dd10STao Zhou 	kgd2kfd_set_sram_ecc_flag(adev->kfd.dev);
129950d6425SStanley.Yang 
130950d6425SStanley.Yang 	if (amdgpu_sriov_vf(adev))
131950d6425SStanley.Yang 		return AMDGPU_RAS_SUCCESS;
132950d6425SStanley.Yang 
13361934624SGuchun Chen 	amdgpu_ras_reset_gpu(adev);
1344c65dd10STao Zhou 
1354c65dd10STao Zhou 	return AMDGPU_RAS_SUCCESS;
1364c65dd10STao Zhou }
1374c65dd10STao Zhou 
amdgpu_sdma_process_ecc_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)1384c65dd10STao Zhou int amdgpu_sdma_process_ecc_irq(struct amdgpu_device *adev,
1394c65dd10STao Zhou 				      struct amdgpu_irq_src *source,
1404c65dd10STao Zhou 				      struct amdgpu_iv_entry *entry)
1414c65dd10STao Zhou {
1424c65dd10STao Zhou 	struct ras_common_if *ras_if = adev->sdma.ras_if;
1434c65dd10STao Zhou 	struct ras_dispatch_if ih_data = {
1444c65dd10STao Zhou 		.entry = entry,
1454c65dd10STao Zhou 	};
1464c65dd10STao Zhou 
1474c65dd10STao Zhou 	if (!ras_if)
1484c65dd10STao Zhou 		return 0;
1494c65dd10STao Zhou 
1504c65dd10STao Zhou 	ih_data.head = *ras_if;
1514c65dd10STao Zhou 
1524c65dd10STao Zhou 	amdgpu_ras_interrupt_dispatch(adev, &ih_data);
1534c65dd10STao Zhou 	return 0;
1544c65dd10STao Zhou }
15515aa1305SLikun Gao 
amdgpu_sdma_init_inst_ctx(struct amdgpu_sdma_instance * sdma_inst)15615aa1305SLikun Gao static int amdgpu_sdma_init_inst_ctx(struct amdgpu_sdma_instance *sdma_inst)
15715aa1305SLikun Gao {
15852642d13SLikun Gao 	uint16_t version_major;
15952642d13SLikun Gao 	const struct common_firmware_header *header = NULL;
16015aa1305SLikun Gao 	const struct sdma_firmware_header_v1_0 *hdr;
16152642d13SLikun Gao 	const struct sdma_firmware_header_v2_0 *hdr_v2;
162b412351eSLikun Gao 	const struct sdma_firmware_header_v3_0 *hdr_v3;
16315aa1305SLikun Gao 
16452642d13SLikun Gao 	header = (const struct common_firmware_header *)
16552642d13SLikun Gao 		sdma_inst->fw->data;
16652642d13SLikun Gao 	version_major = le16_to_cpu(header->header_version_major);
16752642d13SLikun Gao 
16852642d13SLikun Gao 	switch (version_major) {
16952642d13SLikun Gao 	case 1:
17015aa1305SLikun Gao 		hdr = (const struct sdma_firmware_header_v1_0 *)sdma_inst->fw->data;
17115aa1305SLikun Gao 		sdma_inst->fw_version = le32_to_cpu(hdr->header.ucode_version);
17215aa1305SLikun Gao 		sdma_inst->feature_version = le32_to_cpu(hdr->ucode_feature_version);
17352642d13SLikun Gao 		break;
17452642d13SLikun Gao 	case 2:
17552642d13SLikun Gao 		hdr_v2 = (const struct sdma_firmware_header_v2_0 *)sdma_inst->fw->data;
17652642d13SLikun Gao 		sdma_inst->fw_version = le32_to_cpu(hdr_v2->header.ucode_version);
17752642d13SLikun Gao 		sdma_inst->feature_version = le32_to_cpu(hdr_v2->ucode_feature_version);
17852642d13SLikun Gao 		break;
179b412351eSLikun Gao 	case 3:
180b412351eSLikun Gao 		hdr_v3 = (const struct sdma_firmware_header_v3_0 *)sdma_inst->fw->data;
181b412351eSLikun Gao 		sdma_inst->fw_version = le32_to_cpu(hdr_v3->header.ucode_version);
182b412351eSLikun Gao 		sdma_inst->feature_version = le32_to_cpu(hdr_v3->ucode_feature_version);
183b412351eSLikun Gao 		break;
18452642d13SLikun Gao 	default:
18552642d13SLikun Gao 		return -EINVAL;
18652642d13SLikun Gao 	}
18715aa1305SLikun Gao 
18815aa1305SLikun Gao 	if (sdma_inst->feature_version >= 20)
18915aa1305SLikun Gao 		sdma_inst->burst_nop = true;
19015aa1305SLikun Gao 
19115aa1305SLikun Gao 	return 0;
19215aa1305SLikun Gao }
19315aa1305SLikun Gao 
amdgpu_sdma_destroy_inst_ctx(struct amdgpu_device * adev,bool duplicate)19415aa1305SLikun Gao void amdgpu_sdma_destroy_inst_ctx(struct amdgpu_device *adev,
19515aa1305SLikun Gao 				  bool duplicate)
19615aa1305SLikun Gao {
19715aa1305SLikun Gao 	int i;
19815aa1305SLikun Gao 
19915aa1305SLikun Gao 	for (i = 0; i < adev->sdma.num_instances; i++) {
200e4322982SMario Limonciello 		amdgpu_ucode_release(&adev->sdma.instance[i].fw);
20115aa1305SLikun Gao 		if (duplicate)
20215aa1305SLikun Gao 			break;
20315aa1305SLikun Gao 	}
20415aa1305SLikun Gao 
20515aa1305SLikun Gao 	memset((void *)adev->sdma.instance, 0,
20615aa1305SLikun Gao 	       sizeof(struct amdgpu_sdma_instance) * AMDGPU_MAX_SDMA_INSTANCES);
20715aa1305SLikun Gao }
20815aa1305SLikun Gao 
amdgpu_sdma_init_microcode(struct amdgpu_device * adev,u32 instance,bool duplicate)20915aa1305SLikun Gao int amdgpu_sdma_init_microcode(struct amdgpu_device *adev,
2101336b4e7SMario Limonciello 			       u32 instance, bool duplicate)
21115aa1305SLikun Gao {
21215aa1305SLikun Gao 	struct amdgpu_firmware_info *info = NULL;
21315aa1305SLikun Gao 	const struct common_firmware_header *header = NULL;
2141336b4e7SMario Limonciello 	int err, i;
21552642d13SLikun Gao 	const struct sdma_firmware_header_v2_0 *sdma_hdr;
216807d90b5SLikun Gao 	const struct sdma_firmware_header_v3_0 *sdma_hv3;
21752642d13SLikun Gao 	uint16_t version_major;
2181336b4e7SMario Limonciello 	char ucode_prefix[30];
21915aa1305SLikun Gao 
2201336b4e7SMario Limonciello 	amdgpu_ucode_ip_version_decode(adev, SDMA0_HWIP, ucode_prefix, sizeof(ucode_prefix));
2211336b4e7SMario Limonciello 	if (instance == 0)
2223618fa26SYang Wang 		err = amdgpu_ucode_request(adev, &adev->sdma.instance[instance].fw,
223ea5d4934SMario Limonciello 					   AMDGPU_UCODE_REQUIRED,
2243618fa26SYang Wang 					   "amdgpu/%s.bin", ucode_prefix);
2251336b4e7SMario Limonciello 	else
2263618fa26SYang Wang 		err = amdgpu_ucode_request(adev, &adev->sdma.instance[instance].fw,
227ea5d4934SMario Limonciello 					   AMDGPU_UCODE_REQUIRED,
2283618fa26SYang Wang 					   "amdgpu/%s%d.bin", ucode_prefix, instance);
22915aa1305SLikun Gao 	if (err)
23015aa1305SLikun Gao 		goto out;
23152642d13SLikun Gao 
23252642d13SLikun Gao 	header = (const struct common_firmware_header *)
23352642d13SLikun Gao 		adev->sdma.instance[instance].fw->data;
23452642d13SLikun Gao 	version_major = le16_to_cpu(header->header_version_major);
23552642d13SLikun Gao 
236525530adSYang Yingliang 	if ((duplicate && instance) || (!duplicate && version_major > 1)) {
237525530adSYang Yingliang 		err = -EINVAL;
238525530adSYang Yingliang 		goto out;
239525530adSYang Yingliang 	}
24052642d13SLikun Gao 
24115aa1305SLikun Gao 	err = amdgpu_sdma_init_inst_ctx(&adev->sdma.instance[instance]);
24215aa1305SLikun Gao 	if (err)
24315aa1305SLikun Gao 		goto out;
24415aa1305SLikun Gao 
24515aa1305SLikun Gao 	if (duplicate) {
24615aa1305SLikun Gao 		for (i = 1; i < adev->sdma.num_instances; i++)
24715aa1305SLikun Gao 			memcpy((void *)&adev->sdma.instance[i],
24815aa1305SLikun Gao 			       (void *)&adev->sdma.instance[0],
24915aa1305SLikun Gao 			       sizeof(struct amdgpu_sdma_instance));
25015aa1305SLikun Gao 	}
25115aa1305SLikun Gao 
25215aa1305SLikun Gao 	DRM_DEBUG("psp_load == '%s'\n",
25315aa1305SLikun Gao 		  adev->firmware.load_type == AMDGPU_FW_LOAD_PSP ? "true" : "false");
25415aa1305SLikun Gao 
25515aa1305SLikun Gao 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
25652642d13SLikun Gao 		switch (version_major) {
25752642d13SLikun Gao 		case 1:
25815aa1305SLikun Gao 			for (i = 0; i < adev->sdma.num_instances; i++) {
25915aa1305SLikun Gao 				if (!duplicate && (instance != i))
26015aa1305SLikun Gao 					continue;
26115aa1305SLikun Gao 				else {
2623525844dSLijo Lazar 					/* Use a single copy per SDMA firmware type. PSP uses the same instance for all
2633525844dSLijo Lazar 					 * groups of SDMAs */
2643d1bb1a2SHawking Zhang 					if ((amdgpu_ip_version(adev, SDMA0_HWIP, 0) ==
2653d1bb1a2SHawking Zhang 						IP_VERSION(4, 4, 2) ||
2663d1bb1a2SHawking Zhang 					     amdgpu_ip_version(adev, SDMA0_HWIP, 0) ==
267968e3811SLe Ma 						IP_VERSION(4, 4, 4) ||
268968e3811SLe Ma 					     amdgpu_ip_version(adev, SDMA0_HWIP, 0) ==
2693d1bb1a2SHawking Zhang 						IP_VERSION(4, 4, 5)) &&
2704e8303cfSLijo Lazar 					    adev->firmware.load_type ==
2714e8303cfSLijo Lazar 						AMDGPU_FW_LOAD_PSP &&
2723525844dSLijo Lazar 					    adev->sdma.num_inst_per_aid == i) {
2733525844dSLijo Lazar 						break;
2743525844dSLijo Lazar 					}
27515aa1305SLikun Gao 					info = &adev->firmware.ucode[AMDGPU_UCODE_ID_SDMA0 + i];
27615aa1305SLikun Gao 					info->ucode_id = AMDGPU_UCODE_ID_SDMA0 + i;
27715aa1305SLikun Gao 					info->fw = adev->sdma.instance[i].fw;
27815aa1305SLikun Gao 					adev->firmware.fw_size +=
27915aa1305SLikun Gao 						ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
28015aa1305SLikun Gao 				}
28115aa1305SLikun Gao 			}
28252642d13SLikun Gao 			break;
28352642d13SLikun Gao 		case 2:
28452642d13SLikun Gao 			sdma_hdr = (const struct sdma_firmware_header_v2_0 *)
28552642d13SLikun Gao 				adev->sdma.instance[0].fw->data;
28652642d13SLikun Gao 			info = &adev->firmware.ucode[AMDGPU_UCODE_ID_SDMA_UCODE_TH0];
28752642d13SLikun Gao 			info->ucode_id = AMDGPU_UCODE_ID_SDMA_UCODE_TH0;
28852642d13SLikun Gao 			info->fw = adev->sdma.instance[0].fw;
28952642d13SLikun Gao 			adev->firmware.fw_size +=
29052642d13SLikun Gao 				ALIGN(le32_to_cpu(sdma_hdr->ctx_ucode_size_bytes), PAGE_SIZE);
29152642d13SLikun Gao 			info = &adev->firmware.ucode[AMDGPU_UCODE_ID_SDMA_UCODE_TH1];
29252642d13SLikun Gao 			info->ucode_id = AMDGPU_UCODE_ID_SDMA_UCODE_TH1;
29352642d13SLikun Gao 			info->fw = adev->sdma.instance[0].fw;
29452642d13SLikun Gao 			adev->firmware.fw_size +=
29552642d13SLikun Gao 				ALIGN(le32_to_cpu(sdma_hdr->ctl_ucode_size_bytes), PAGE_SIZE);
29652642d13SLikun Gao 			break;
297807d90b5SLikun Gao 		case 3:
298807d90b5SLikun Gao 			sdma_hv3 = (const struct sdma_firmware_header_v3_0 *)
299807d90b5SLikun Gao 				adev->sdma.instance[0].fw->data;
300807d90b5SLikun Gao 			info = &adev->firmware.ucode[AMDGPU_UCODE_ID_SDMA_RS64];
301807d90b5SLikun Gao 			info->ucode_id = AMDGPU_UCODE_ID_SDMA_RS64;
302807d90b5SLikun Gao 			info->fw = adev->sdma.instance[0].fw;
303807d90b5SLikun Gao 			adev->firmware.fw_size +=
304807d90b5SLikun Gao 				ALIGN(le32_to_cpu(sdma_hv3->ucode_size_bytes), PAGE_SIZE);
305807d90b5SLikun Gao 			break;
30652642d13SLikun Gao 		default:
307525530adSYang Yingliang 			err = -EINVAL;
30852642d13SLikun Gao 		}
30915aa1305SLikun Gao 	}
31015aa1305SLikun Gao 
31115aa1305SLikun Gao out:
3126675402aSMario Limonciello 	if (err)
31315aa1305SLikun Gao 		amdgpu_sdma_destroy_inst_ctx(adev, duplicate);
31415aa1305SLikun Gao 	return err;
31515aa1305SLikun Gao }
316571c0536SAlex Deucher 
amdgpu_sdma_ras_sw_init(struct amdgpu_device * adev)317a57b24e1SYiPeng Chai int amdgpu_sdma_ras_sw_init(struct amdgpu_device *adev)
318a57b24e1SYiPeng Chai {
319a57b24e1SYiPeng Chai 	int err = 0;
320a57b24e1SYiPeng Chai 	struct amdgpu_sdma_ras *ras = NULL;
321a57b24e1SYiPeng Chai 
322a57b24e1SYiPeng Chai 	/* adev->sdma.ras is NULL, which means sdma does not
323a57b24e1SYiPeng Chai 	 * support ras function, then do nothing here.
324a57b24e1SYiPeng Chai 	 */
325a57b24e1SYiPeng Chai 	if (!adev->sdma.ras)
326a57b24e1SYiPeng Chai 		return 0;
327a57b24e1SYiPeng Chai 
328a57b24e1SYiPeng Chai 	ras = adev->sdma.ras;
329a57b24e1SYiPeng Chai 
330a57b24e1SYiPeng Chai 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
331a57b24e1SYiPeng Chai 	if (err) {
332a57b24e1SYiPeng Chai 		dev_err(adev->dev, "Failed to register sdma ras block!\n");
333a57b24e1SYiPeng Chai 		return err;
334a57b24e1SYiPeng Chai 	}
335a57b24e1SYiPeng Chai 
336a57b24e1SYiPeng Chai 	strcpy(ras->ras_block.ras_comm.name, "sdma");
337a57b24e1SYiPeng Chai 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__SDMA;
338a57b24e1SYiPeng Chai 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
339a57b24e1SYiPeng Chai 	adev->sdma.ras_if = &ras->ras_block.ras_comm;
340a57b24e1SYiPeng Chai 
341a57b24e1SYiPeng Chai 	/* If not define special ras_late_init function, use default ras_late_init */
342a57b24e1SYiPeng Chai 	if (!ras->ras_block.ras_late_init)
343a57b24e1SYiPeng Chai 		ras->ras_block.ras_late_init = amdgpu_sdma_ras_late_init;
344a57b24e1SYiPeng Chai 
345a57b24e1SYiPeng Chai 	/* If not defined special ras_cb function, use default ras_cb */
346a57b24e1SYiPeng Chai 	if (!ras->ras_block.ras_cb)
347a57b24e1SYiPeng Chai 		ras->ras_block.ras_cb = amdgpu_sdma_process_ras_data_cb;
348a57b24e1SYiPeng Chai 
349a57b24e1SYiPeng Chai 	return 0;
350a57b24e1SYiPeng Chai }
351d2e3961aSJesse Zhang 
352d2e3961aSJesse Zhang /*
353d2e3961aSJesse Zhang  * debugfs for to enable/disable sdma job submission to specific core.
354d2e3961aSJesse Zhang  */
355d2e3961aSJesse Zhang #if defined(CONFIG_DEBUG_FS)
amdgpu_debugfs_sdma_sched_mask_set(void * data,u64 val)356d2e3961aSJesse Zhang static int amdgpu_debugfs_sdma_sched_mask_set(void *data, u64 val)
357d2e3961aSJesse Zhang {
358d2e3961aSJesse Zhang 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
35977bd621dS[email protected] 	u64 i, num_ring;
360d2e3961aSJesse Zhang 	u64 mask = 0;
36177bd621dS[email protected] 	struct amdgpu_ring *ring, *page = NULL;
362d2e3961aSJesse Zhang 
363d2e3961aSJesse Zhang 	if (!adev)
364d2e3961aSJesse Zhang 		return -ENODEV;
365d2e3961aSJesse Zhang 
36677bd621dS[email protected] 	/* Determine the number of rings per SDMA instance
36777bd621dS[email protected] 	 * (1 for sdma gfx ring, 2 if page queue exists)
36877bd621dS[email protected] 	 */
36977bd621dS[email protected] 	if (adev->sdma.has_page_queue)
37077bd621dS[email protected] 		num_ring = 2;
37177bd621dS[email protected] 	else
37277bd621dS[email protected] 		num_ring = 1;
37377bd621dS[email protected] 
37477bd621dS[email protected] 	/* Calculate the maximum possible mask value
37577bd621dS[email protected] 	 * based on the number of SDMA instances and rings
37677bd621dS[email protected] 	*/
37777bd621dS[email protected] 	mask = BIT_ULL(adev->sdma.num_instances * num_ring) - 1;
37877bd621dS[email protected] 
379d2e3961aSJesse Zhang 	if ((val & mask) == 0)
380d2e3961aSJesse Zhang 		return -EINVAL;
381d2e3961aSJesse Zhang 
382d2e3961aSJesse Zhang 	for (i = 0; i < adev->sdma.num_instances; ++i) {
383d2e3961aSJesse Zhang 		ring = &adev->sdma.instance[i].ring;
38477bd621dS[email protected] 		if (adev->sdma.has_page_queue)
38577bd621dS[email protected] 			page = &adev->sdma.instance[i].page;
38677bd621dS[email protected] 		if (val & BIT_ULL(i * num_ring))
387d2e3961aSJesse Zhang 			ring->sched.ready = true;
388d2e3961aSJesse Zhang 		else
389d2e3961aSJesse Zhang 			ring->sched.ready = false;
39077bd621dS[email protected] 
39177bd621dS[email protected] 		if (page) {
39277bd621dS[email protected] 			if (val & BIT_ULL(i * num_ring + 1))
39377bd621dS[email protected] 				page->sched.ready = true;
39477bd621dS[email protected] 			else
39577bd621dS[email protected] 				page->sched.ready = false;
39677bd621dS[email protected] 		}
397d2e3961aSJesse Zhang 	}
398d2e3961aSJesse Zhang 	/* publish sched.ready flag update effective immediately across smp */
399d2e3961aSJesse Zhang 	smp_rmb();
400d2e3961aSJesse Zhang 	return 0;
401d2e3961aSJesse Zhang }
402d2e3961aSJesse Zhang 
amdgpu_debugfs_sdma_sched_mask_get(void * data,u64 * val)403d2e3961aSJesse Zhang static int amdgpu_debugfs_sdma_sched_mask_get(void *data, u64 *val)
404d2e3961aSJesse Zhang {
405d2e3961aSJesse Zhang 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
40677bd621dS[email protected] 	u64 i, num_ring;
407d2e3961aSJesse Zhang 	u64 mask = 0;
40877bd621dS[email protected] 	struct amdgpu_ring *ring, *page = NULL;
409d2e3961aSJesse Zhang 
410d2e3961aSJesse Zhang 	if (!adev)
411d2e3961aSJesse Zhang 		return -ENODEV;
41277bd621dS[email protected] 
41377bd621dS[email protected] 	/* Determine the number of rings per SDMA instance
41477bd621dS[email protected] 	 * (1 for sdma gfx ring, 2 if page queue exists)
41577bd621dS[email protected] 	 */
41677bd621dS[email protected] 	if (adev->sdma.has_page_queue)
41777bd621dS[email protected] 		num_ring = 2;
41877bd621dS[email protected] 	else
41977bd621dS[email protected] 		num_ring = 1;
42077bd621dS[email protected] 
421d2e3961aSJesse Zhang 	for (i = 0; i < adev->sdma.num_instances; ++i) {
422d2e3961aSJesse Zhang 		ring = &adev->sdma.instance[i].ring;
42377bd621dS[email protected] 		if (adev->sdma.has_page_queue)
42477bd621dS[email protected] 			page = &adev->sdma.instance[i].page;
42577bd621dS[email protected] 
426d2e3961aSJesse Zhang 		if (ring->sched.ready)
42777bd621dS[email protected] 			mask |= BIT_ULL(i * num_ring);
42877bd621dS[email protected] 		else
42977bd621dS[email protected] 			mask &= ~BIT_ULL(i * num_ring);
43077bd621dS[email protected] 
43177bd621dS[email protected] 		if (page) {
43277bd621dS[email protected] 			if (page->sched.ready)
43377bd621dS[email protected] 				mask |= BIT_ULL(i * num_ring + 1);
43477bd621dS[email protected] 			else
43577bd621dS[email protected] 				mask &= ~BIT_ULL(i * num_ring + 1);
43677bd621dS[email protected] 		}
437d2e3961aSJesse Zhang 	}
438d2e3961aSJesse Zhang 
439d2e3961aSJesse Zhang 	*val = mask;
440d2e3961aSJesse Zhang 	return 0;
441d2e3961aSJesse Zhang }
442d2e3961aSJesse Zhang 
443d2e3961aSJesse Zhang DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_sdma_sched_mask_fops,
444d2e3961aSJesse Zhang 			 amdgpu_debugfs_sdma_sched_mask_get,
445d2e3961aSJesse Zhang 			 amdgpu_debugfs_sdma_sched_mask_set, "%llx\n");
446d2e3961aSJesse Zhang 
447d2e3961aSJesse Zhang #endif
448d2e3961aSJesse Zhang 
amdgpu_debugfs_sdma_sched_mask_init(struct amdgpu_device * adev)449d2e3961aSJesse Zhang void amdgpu_debugfs_sdma_sched_mask_init(struct amdgpu_device *adev)
450d2e3961aSJesse Zhang {
451d2e3961aSJesse Zhang #if defined(CONFIG_DEBUG_FS)
452d2e3961aSJesse Zhang 	struct drm_minor *minor = adev_to_drm(adev)->primary;
453d2e3961aSJesse Zhang 	struct dentry *root = minor->debugfs_root;
454d2e3961aSJesse Zhang 	char name[32];
455d2e3961aSJesse Zhang 
456d2e3961aSJesse Zhang 	if (!(adev->sdma.num_instances > 1))
457d2e3961aSJesse Zhang 		return;
458d2e3961aSJesse Zhang 	sprintf(name, "amdgpu_sdma_sched_mask");
459d2e3961aSJesse Zhang 	debugfs_create_file(name, 0600, root, adev,
460d2e3961aSJesse Zhang 			    &amdgpu_debugfs_sdma_sched_mask_fops);
461d2e3961aSJesse Zhang #endif
462d2e3961aSJesse Zhang }
46359fd50b8S[email protected] 
amdgpu_get_sdma_reset_mask(struct device * dev,struct device_attribute * attr,char * buf)46459fd50b8S[email protected] static ssize_t amdgpu_get_sdma_reset_mask(struct device *dev,
46559fd50b8S[email protected] 						struct device_attribute *attr,
46659fd50b8S[email protected] 						char *buf)
46759fd50b8S[email protected] {
46859fd50b8S[email protected] 	struct drm_device *ddev = dev_get_drvdata(dev);
46959fd50b8S[email protected] 	struct amdgpu_device *adev = drm_to_adev(ddev);
47059fd50b8S[email protected] 
47159fd50b8S[email protected] 	if (!adev)
47259fd50b8S[email protected] 		return -ENODEV;
47359fd50b8S[email protected] 
47459fd50b8S[email protected] 	return amdgpu_show_reset_mask(buf, adev->sdma.supported_reset);
47559fd50b8S[email protected] }
47659fd50b8S[email protected] 
47759fd50b8S[email protected] static DEVICE_ATTR(sdma_reset_mask, 0444,
47859fd50b8S[email protected] 		   amdgpu_get_sdma_reset_mask, NULL);
47959fd50b8S[email protected] 
amdgpu_sdma_sysfs_reset_mask_init(struct amdgpu_device * adev)48059fd50b8S[email protected] int amdgpu_sdma_sysfs_reset_mask_init(struct amdgpu_device *adev)
48159fd50b8S[email protected] {
48259fd50b8S[email protected] 	int r = 0;
48359fd50b8S[email protected] 
48459fd50b8S[email protected] 	if (!amdgpu_gpu_recovery)
48559fd50b8S[email protected] 		return r;
48659fd50b8S[email protected] 
48759fd50b8S[email protected] 	if (adev->sdma.num_instances) {
48859fd50b8S[email protected] 		r = device_create_file(adev->dev, &dev_attr_sdma_reset_mask);
48959fd50b8S[email protected] 		if (r)
49059fd50b8S[email protected] 			return r;
49159fd50b8S[email protected] 	}
49259fd50b8S[email protected] 
49359fd50b8S[email protected] 	return r;
49459fd50b8S[email protected] }
49559fd50b8S[email protected] 
amdgpu_sdma_sysfs_reset_mask_fini(struct amdgpu_device * adev)49659fd50b8S[email protected] void amdgpu_sdma_sysfs_reset_mask_fini(struct amdgpu_device *adev)
49759fd50b8S[email protected] {
49859fd50b8S[email protected] 	if (!amdgpu_gpu_recovery)
49959fd50b8S[email protected] 		return;
50059fd50b8S[email protected] 
5012f1b1352S[email protected] 	if (adev->dev->kobj.sd) {
50259fd50b8S[email protected] 		if (adev->sdma.num_instances)
50359fd50b8S[email protected] 			device_remove_file(adev->dev, &dev_attr_sdma_reset_mask);
50459fd50b8S[email protected] 	}
5052f1b1352S[email protected] }
506f3304495S[email protected] 
amdgpu_sdma_get_shared_ring(struct amdgpu_device * adev,struct amdgpu_ring * ring)507*b09cdeb4S[email protected] struct amdgpu_ring *amdgpu_sdma_get_shared_ring(struct amdgpu_device *adev, struct amdgpu_ring *ring)
508*b09cdeb4S[email protected] {
509*b09cdeb4S[email protected] 	if (adev->sdma.has_page_queue &&
510*b09cdeb4S[email protected] 	    (ring->me < adev->sdma.num_instances) &&
511*b09cdeb4S[email protected] 	    (ring == &adev->sdma.instance[ring->me].ring))
512*b09cdeb4S[email protected] 		return &adev->sdma.instance[ring->me].page;
513*b09cdeb4S[email protected] 	else
514*b09cdeb4S[email protected] 		return NULL;
515*b09cdeb4S[email protected] }
516*b09cdeb4S[email protected] 
517*b09cdeb4S[email protected] /**
518*b09cdeb4S[email protected] * amdgpu_sdma_is_shared_inv_eng - Check if a ring is an SDMA ring that shares a VM invalidation engine
519*b09cdeb4S[email protected] * @adev: Pointer to the AMDGPU device structure
520*b09cdeb4S[email protected] * @ring: Pointer to the ring structure to check
521*b09cdeb4S[email protected] *
522*b09cdeb4S[email protected] * This function checks if the given ring is an SDMA ring that shares a VM invalidation engine.
523*b09cdeb4S[email protected] * It returns true if the ring is such an SDMA ring, false otherwise.
524*b09cdeb4S[email protected] */
amdgpu_sdma_is_shared_inv_eng(struct amdgpu_device * adev,struct amdgpu_ring * ring)525*b09cdeb4S[email protected] bool amdgpu_sdma_is_shared_inv_eng(struct amdgpu_device *adev, struct amdgpu_ring *ring)
526*b09cdeb4S[email protected] {
527*b09cdeb4S[email protected] 	int i = ring->me;
528*b09cdeb4S[email protected] 
529*b09cdeb4S[email protected] 	if (!adev->sdma.has_page_queue || i >= adev->sdma.num_instances)
530*b09cdeb4S[email protected] 		return false;
531*b09cdeb4S[email protected] 
532*b09cdeb4S[email protected] 	if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) ||
533*b09cdeb4S[email protected] 	    amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 4) ||
534*b09cdeb4S[email protected] 	    amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 5, 0))
535*b09cdeb4S[email protected] 		return (ring == &adev->sdma.instance[i].page);
536*b09cdeb4S[email protected] 	else
537*b09cdeb4S[email protected] 		return false;
538*b09cdeb4S[email protected] }
539*b09cdeb4S[email protected] 
540f3304495S[email protected] /**
541f3304495S[email protected]  * amdgpu_sdma_register_on_reset_callbacks - Register SDMA reset callbacks
542f3304495S[email protected]  * @funcs: Pointer to the callback structure containing pre_reset and post_reset functions
543f3304495S[email protected]  *
544f3304495S[email protected]  * This function allows KFD and AMDGPU to register their own callbacks for handling
545f3304495S[email protected]  * pre-reset and post-reset operations for engine reset. These are needed because engine
546f3304495S[email protected]  * reset will stop all queues on that engine.
547f3304495S[email protected]  */
amdgpu_sdma_register_on_reset_callbacks(struct amdgpu_device * adev,struct sdma_on_reset_funcs * funcs)548f3304495S[email protected] void amdgpu_sdma_register_on_reset_callbacks(struct amdgpu_device *adev, struct sdma_on_reset_funcs *funcs)
549f3304495S[email protected] {
550f3304495S[email protected] 	if (!funcs)
551f3304495S[email protected] 		return;
552f3304495S[email protected] 
553fdbfaaaaS[email protected] 	/* Ensure the reset_callback_list is initialized */
554fdbfaaaaS[email protected] 	if (!adev->sdma.reset_callback_list.next) {
555fdbfaaaaS[email protected] 		INIT_LIST_HEAD(&adev->sdma.reset_callback_list);
556fdbfaaaaS[email protected] 	}
557f3304495S[email protected] 	/* Initialize the list node in the callback structure */
558f3304495S[email protected] 	INIT_LIST_HEAD(&funcs->list);
559f3304495S[email protected] 
560f3304495S[email protected] 	/* Add the callback structure to the global list */
561f3304495S[email protected] 	list_add_tail(&funcs->list, &adev->sdma.reset_callback_list);
562f3304495S[email protected] }
563f3304495S[email protected] 
564f3304495S[email protected] /**
565f3304495S[email protected]  * amdgpu_sdma_reset_engine - Reset a specific SDMA engine
566f3304495S[email protected]  * @adev: Pointer to the AMDGPU device
567f3304495S[email protected]  * @instance_id: ID of the SDMA engine instance to reset
568f3304495S[email protected]  *
569f3304495S[email protected]  * This function performs the following steps:
570f3304495S[email protected]  * 1. Calls all registered pre_reset callbacks to allow KFD and AMDGPU to save their state.
571f3304495S[email protected]  * 2. Resets the specified SDMA engine instance.
572f3304495S[email protected]  * 3. Calls all registered post_reset callbacks to allow KFD and AMDGPU to restore their state.
573f3304495S[email protected]  *
574f3304495S[email protected]  * Returns: 0 on success, or a negative error code on failure.
575f3304495S[email protected]  */
amdgpu_sdma_reset_engine(struct amdgpu_device * adev,uint32_t instance_id)576e02fcf73SAlex Deucher int amdgpu_sdma_reset_engine(struct amdgpu_device *adev, uint32_t instance_id)
577f3304495S[email protected] {
578f3304495S[email protected] 	struct sdma_on_reset_funcs *funcs;
5794c02f730S[email protected] 	int ret = 0;
580*b09cdeb4S[email protected] 	struct amdgpu_sdma_instance *sdma_instance = &adev->sdma.instance[instance_id];
5814c02f730S[email protected] 	struct amdgpu_ring *gfx_ring = &sdma_instance->ring;
5824c02f730S[email protected] 	struct amdgpu_ring *page_ring = &sdma_instance->page;
5834c02f730S[email protected] 	bool gfx_sched_stopped = false, page_sched_stopped = false;
5844c02f730S[email protected] 
585e02fcf73SAlex Deucher 	mutex_lock(&sdma_instance->engine_reset_mutex);
5864c02f730S[email protected] 	/* Stop the scheduler's work queue for the GFX and page rings if they are running.
5874c02f730S[email protected] 	* This ensures that no new tasks are submitted to the queues while
5884c02f730S[email protected] 	* the reset is in progress.
5894c02f730S[email protected] 	*/
5904c02f730S[email protected] 	if (!amdgpu_ring_sched_ready(gfx_ring)) {
5914c02f730S[email protected] 		drm_sched_wqueue_stop(&gfx_ring->sched);
592fdbfaaaaS[email protected] 		gfx_sched_stopped = true;
5934c02f730S[email protected] 	}
5944c02f730S[email protected] 
5954c02f730S[email protected] 	if (adev->sdma.has_page_queue && !amdgpu_ring_sched_ready(page_ring)) {
5964c02f730S[email protected] 		drm_sched_wqueue_stop(&page_ring->sched);
5974c02f730S[email protected] 		page_sched_stopped = true;
5984c02f730S[email protected] 	}
599f3304495S[email protected] 
600f3304495S[email protected] 	/* Invoke all registered pre_reset callbacks */
601f3304495S[email protected] 	list_for_each_entry(funcs, &adev->sdma.reset_callback_list, list) {
602f3304495S[email protected] 		if (funcs->pre_reset) {
603f3304495S[email protected] 			ret = funcs->pre_reset(adev, instance_id);
604f3304495S[email protected] 			if (ret) {
605f3304495S[email protected] 				dev_err(adev->dev,
606f3304495S[email protected] 				"beforeReset callback failed for instance %u: %d\n",
607f3304495S[email protected] 					instance_id, ret);
6084c02f730S[email protected] 				goto exit;
609f3304495S[email protected] 			}
610f3304495S[email protected] 		}
611f3304495S[email protected] 	}
612f3304495S[email protected] 
613f3304495S[email protected] 	/* Perform the SDMA reset for the specified instance */
614f3304495S[email protected] 	ret = amdgpu_dpm_reset_sdma(adev, 1 << instance_id);
615f3304495S[email protected] 	if (ret) {
616f3304495S[email protected] 		dev_err(adev->dev, "Failed to reset SDMA instance %u\n", instance_id);
6174c02f730S[email protected] 		goto exit;
618f3304495S[email protected] 	}
619f3304495S[email protected] 
620f3304495S[email protected] 	/* Invoke all registered post_reset callbacks */
621f3304495S[email protected] 	list_for_each_entry(funcs, &adev->sdma.reset_callback_list, list) {
622f3304495S[email protected] 		if (funcs->post_reset) {
623f3304495S[email protected] 			ret = funcs->post_reset(adev, instance_id);
624f3304495S[email protected] 			if (ret) {
625f3304495S[email protected] 				dev_err(adev->dev,
626f3304495S[email protected] 				"afterReset callback failed for instance %u: %d\n",
627f3304495S[email protected] 					instance_id, ret);
6284c02f730S[email protected] 				goto exit;
629f3304495S[email protected] 			}
630f3304495S[email protected] 		}
631f3304495S[email protected] 	}
632f3304495S[email protected] 
6334c02f730S[email protected] exit:
6344c02f730S[email protected] 	/* Restart the scheduler's work queue for the GFX and page rings
6354c02f730S[email protected] 	 * if they were stopped by this function. This allows new tasks
6364c02f730S[email protected] 	 * to be submitted to the queues after the reset is complete.
6374c02f730S[email protected] 	 */
638cc63bcfdS[email protected] 	if (!ret) {
6394c02f730S[email protected] 		if (gfx_sched_stopped && amdgpu_ring_sched_ready(gfx_ring)) {
6404c02f730S[email protected] 			drm_sched_wqueue_start(&gfx_ring->sched);
6414c02f730S[email protected] 		}
6424c02f730S[email protected] 		if (page_sched_stopped && amdgpu_ring_sched_ready(page_ring)) {
6434c02f730S[email protected] 			drm_sched_wqueue_start(&page_ring->sched);
6444c02f730S[email protected] 		}
6454c02f730S[email protected] 	}
646e02fcf73SAlex Deucher 	mutex_unlock(&sdma_instance->engine_reset_mutex);
6474c02f730S[email protected] 
6484c02f730S[email protected] 	return ret;
649f3304495S[email protected] }
650