xref: /linux/drivers/gpu/drm/amd/amdgpu/imu_v12_1.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /*
2  * Copyright 2025 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  */
23 
24 #include <linux/firmware.h>
25 #include "amdgpu.h"
26 #include "amdgpu_imu.h"
27 #include "amdgpu_dpm.h"
28 
29 #include "imu_v12_1.h"
30 
31 #include "gc/gc_12_1_0_offset.h"
32 #include "gc/gc_12_1_0_sh_mask.h"
33 #include "mmhub/mmhub_4_2_0_offset.h"
34 
35 #define TRANSFER_RAM_MASK	0x001c0000
36 
37 static int imu_v12_1_init_microcode(struct amdgpu_device *adev)
38 {
39 	char ucode_prefix[15];
40 	int err;
41 	const struct imu_firmware_header_v1_0 *imu_hdr;
42 	struct amdgpu_firmware_info *info = NULL;
43 
44 	DRM_DEBUG("\n");
45 
46 	amdgpu_ucode_ip_version_decode(adev, GC_HWIP, ucode_prefix, sizeof(ucode_prefix));
47 	err = amdgpu_ucode_request(adev, &adev->gfx.imu_fw, AMDGPU_UCODE_REQUIRED,
48 				   "amdgpu/%s_imu.bin", ucode_prefix);
49 	if (err)
50 		goto out;
51 
52 	imu_hdr = (const struct imu_firmware_header_v1_0 *)adev->gfx.imu_fw->data;
53 	adev->gfx.imu_fw_version = le32_to_cpu(imu_hdr->header.ucode_version);
54 
55 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
56 		info = &adev->firmware.ucode[AMDGPU_UCODE_ID_IMU_I];
57 		info->ucode_id = AMDGPU_UCODE_ID_IMU_I;
58 		info->fw = adev->gfx.imu_fw;
59 		adev->firmware.fw_size +=
60 			ALIGN(le32_to_cpu(imu_hdr->imu_iram_ucode_size_bytes), PAGE_SIZE);
61 		info = &adev->firmware.ucode[AMDGPU_UCODE_ID_IMU_D];
62 		info->ucode_id = AMDGPU_UCODE_ID_IMU_D;
63 		info->fw = adev->gfx.imu_fw;
64 		adev->firmware.fw_size +=
65 			ALIGN(le32_to_cpu(imu_hdr->imu_dram_ucode_size_bytes), PAGE_SIZE);
66 	}
67 
68 out:
69 	if (err) {
70 		dev_err(adev->dev,
71 			"gfx12: Failed to load firmware \"%s_imu.bin\"\n",
72 			ucode_prefix);
73 		amdgpu_ucode_release(&adev->gfx.imu_fw);
74 	}
75 
76 	return err;
77 }
78 
79 static void imu_v12_1_xcc_load_microcode(struct amdgpu_device *adev,
80 					 int xcc_id)
81 {
82 	const struct imu_firmware_header_v1_0 *hdr;
83 	const __le32 *fw_data;
84 	unsigned i, fw_size;
85 
86 	hdr = (const struct imu_firmware_header_v1_0 *)adev->gfx.imu_fw->data;
87 	fw_data = (const __le32 *)(adev->gfx.imu_fw->data +
88 			le32_to_cpu(hdr->header.ucode_array_offset_bytes));
89 	fw_size = le32_to_cpu(hdr->imu_iram_ucode_size_bytes) / 4;
90 
91 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regGFX_IMU_I_RAM_ADDR, 0);
92 
93 	for (i = 0; i < fw_size; i++)
94 		WREG32_SOC15(GC, GET_INST(GC, xcc_id),
95 			     regGFX_IMU_I_RAM_DATA,
96 			     le32_to_cpup(fw_data++));
97 
98 	WREG32_SOC15(GC, GET_INST(GC, xcc_id),
99 		     regGFX_IMU_I_RAM_ADDR,
100 		     adev->gfx.imu_fw_version);
101 
102 	fw_data = (const __le32 *)(adev->gfx.imu_fw->data +
103 			le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
104 			le32_to_cpu(hdr->imu_iram_ucode_size_bytes));
105 	fw_size = le32_to_cpu(hdr->imu_dram_ucode_size_bytes) / 4;
106 
107 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regGFX_IMU_D_RAM_ADDR, 0);
108 
109 	for (i = 0; i < fw_size; i++)
110 		WREG32_SOC15(GC, GET_INST(GC, xcc_id),
111 			     regGFX_IMU_D_RAM_DATA,
112 			     le32_to_cpup(fw_data++));
113 
114 	WREG32_SOC15(GC, GET_INST(GC, xcc_id),
115 		     regGFX_IMU_D_RAM_ADDR,
116 		     adev->gfx.imu_fw_version);
117 }
118 
119 static int imu_v12_1_load_microcode(struct amdgpu_device *adev)
120 {
121 	int i, num_xcc;
122 
123 	if (!adev->gfx.imu_fw)
124 		return -EINVAL;
125 
126 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
127 	for (i = 0; i < num_xcc; i++) {
128 		imu_v12_1_xcc_load_microcode(adev, i);
129 	}
130 
131 	return 0;
132 }
133 
134 #define regGFX_IMU_PARTITION_SWITCH		0x5f8c
135 #define regGFX_IMU_PARTITION_SWITCH_BASE_IDX	1
136 #define GFX_IMU_PARTITION_SWITCH__PARTITION_MODE__SHIFT		0x0
137 #define GFX_IMU_PARTITION_SWITCH__TOTAL_XCCS_IN_XCP__SHIFT	0x2
138 #define GFX_IMU_PARTITION_SWITCH__VIRTUAL_XCC_ID__SHIFT		0x9
139 #define GFX_IMU_PARTITION_SWITCH__PHYSICAL_XCC_MASK__SHIFT	0x10
140 #define GFX_IMU_PARTITION_SWITCH__PARTITION_MODE_MASK		0x00000003L
141 #define GFX_IMU_PARTITION_SWITCH__TOTAL_XCCS_IN_XCP_MASK	0x0000003CL
142 #define GFX_IMU_PARTITION_SWITCH__VIRTUAL_XCC_ID_MASK		0x00000E00L
143 #define GFX_IMU_PARTITION_SWITCH__PHYSICAL_XCC_MASK_MASK	0x00FF0000L
144 
145 static int imu_v12_1_switch_compute_partition(struct amdgpu_device *adev,
146 					      int num_xccs_per_xcp,
147 					      int compute_partition_mode)
148 {
149 	int ret, i, num_xcc;
150 	u32 tmp = 0, physical_xcc_mask = 0;
151 
152 	if (adev->psp.funcs) {
153 		ret = psp_spatial_partition(&adev->psp, compute_partition_mode);
154 		if (ret)
155 			return ret;
156 	} else {
157 		num_xcc = NUM_XCC(adev->gfx.xcc_mask);
158 		/* 00 - SPX; 01 - DPX; 10 - QPX; 11 - CPX */
159 		switch (compute_partition_mode) {
160 		case AMDGPU_SPX_PARTITION_MODE:
161 			compute_partition_mode = 0;
162 			break;
163 		case AMDGPU_DPX_PARTITION_MODE:
164 			compute_partition_mode = 1;
165 			break;
166 		case AMDGPU_QPX_PARTITION_MODE:
167 			compute_partition_mode = 2;
168 			break;
169 		case AMDGPU_CPX_PARTITION_MODE:
170 			compute_partition_mode = 3;
171 			break;
172 		default:
173 			dev_err(adev->dev, "Invalid compute partition mode\n");
174 			return -EINVAL;
175 		}
176 
177 		num_xcc = NUM_XCC(adev->gfx.xcc_mask);
178 		for(i = 0; i < num_xcc; i++) {
179 			if (i % num_xccs_per_xcp == 0) {
180 				physical_xcc_mask =
181 					adev->gfx.xcc_mask &
182 					(GENMASK(GET_INST(GC, i + num_xccs_per_xcp - 1),
183 						 GET_INST(GC, i)));
184 			}
185 			tmp = REG_SET_FIELD(tmp, GFX_IMU_PARTITION_SWITCH,
186 					    PARTITION_MODE, compute_partition_mode);
187 			tmp = REG_SET_FIELD(tmp, GFX_IMU_PARTITION_SWITCH,
188 					    TOTAL_XCCS_IN_XCP, num_xccs_per_xcp);
189 			tmp = REG_SET_FIELD(tmp, GFX_IMU_PARTITION_SWITCH,
190 					    VIRTUAL_XCC_ID, i % num_xccs_per_xcp);
191 			tmp = REG_SET_FIELD(tmp, GFX_IMU_PARTITION_SWITCH,
192 					    PHYSICAL_XCC_MASK, physical_xcc_mask);
193 			WREG32_SOC15(GC, GET_INST(GC, i), regGFX_IMU_PARTITION_SWITCH, tmp);
194 		}
195 		ret = 0;
196 	}
197 
198 	adev->gfx.num_xcc_per_xcp = num_xccs_per_xcp;
199 	return 0;
200 }
201 
202 #define regGFX_IMU_MCM_ADDR_LUT_PF		0x5f8d
203 #define regGFX_IMU_MCM_ADDR_LUT_PF_BASE_IDX	1
204 
205 static void imu_v12_1_init_mcm_addr_lut(struct amdgpu_device *adev)
206 {
207 	/* Encoding mcm_addr_lut in SPX mode for now
208 	 * I've noticed variations in encoding
209 	 * Need to revisit the configuration later
210 	 * XCD0:: Bits-[3:0 ] AID-01 XCD-00
211 	 * XCD1:: Bits-[7:4 ] AID-01 XCD-01
212 	 * XCD2:: Bits-[11:8 ] AID-01 XCD-10
213 	 * XCD3:: Bits-[15:12] AID-01 XCD-11
214 	 * XCD4:: Bits-[19:16] AID-10 XCD-00
215 	 * XCD5:: Bits-[23:20] AID-10 XCD-01
216 	 * XCD6:: Bits-[27:24] AID-10 XCD-10
217 	 * XCD7:: Bits-[31:28] AID-10 XCD-11
218 	 */
219 	int data, encode_data;
220 	int i, j, num_xcc;
221 
222 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
223 	for (i = 0; i < (num_xcc / adev->gfx.num_xcc_per_xcp); i++) {
224 		encode_data = 0;
225 		for (j = 0; j < adev->gfx.num_xcc_per_xcp; j++) {
226 			data = ((GET_INST(GC, i * adev->gfx.num_xcc_per_xcp + j) / 4 + 1) << 2) |
227 			        (GET_INST(GC, i * adev->gfx.num_xcc_per_xcp + j) % 4);
228 			encode_data |= data << (j * 4);
229 		}
230 		for (j = 0; j < adev->gfx.num_xcc_per_xcp; j++)
231 			WREG32_SOC15(GC, GET_INST(GC, i * adev->gfx.num_xcc_per_xcp + j),
232 					regGFX_IMU_MCM_ADDR_LUT_PF, encode_data);
233 	}
234 }
235 
236 const struct amdgpu_imu_funcs gfx_v12_1_imu_funcs = {
237 	.init_microcode = imu_v12_1_init_microcode,
238 	.load_microcode = imu_v12_1_load_microcode,
239 	.switch_compute_partition = imu_v12_1_switch_compute_partition,
240 	.init_mcm_addr_lut = imu_v12_1_init_mcm_addr_lut,
241 };
242