xref: /linux/drivers/gpu/drm/amd/amdgpu/vpe_v3_0.c (revision d305cb6d999161fd0f8058a48a7b727f9d8b5a58)
1 /*
2  * Copyright 2026 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 #include <linux/firmware.h>
24 #include "amdgpu.h"
25 #include "amdgpu_ucode.h"
26 #include "amdgpu_vpe.h"
27 #include "vpe_v3_0.h"
28 #include "soc15_common.h"
29 #include "ivsrcid/vpe/irqsrcs_vpe_6_1.h"
30 #include "vpe/vpe_3_0_0_offset.h"
31 #include "vpe/vpe_3_0_0_sh_mask.h"
32 
33 MODULE_FIRMWARE("amdgpu/vpe_3_0_0.bin");
34 MODULE_FIRMWARE("amdgpu/vpe_3_0_1.bin");
35 
36 #define VPE_THREAD1_UCODE_OFFSET	0x8000
37 #define regVPEC_F32_CNTL_DEFAULT	0x08084001
38 
39 static uint32_t vpe_v3_0_get_reg_offset(struct amdgpu_vpe *vpe, uint32_t inst, uint32_t offset)
40 {
41 	uint32_t base;
42 
43 	base = vpe->ring.adev->reg_offset[VPE_HWIP][inst][0];
44 
45 	return base + offset;
46 }
47 
48 static int vpe_v3_0_irq_init(struct amdgpu_vpe *vpe)
49 {
50 	struct amdgpu_device *adev = container_of(vpe, struct amdgpu_device, vpe);
51 	int ret;
52 
53 	ret = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_VPE,
54 				VPE_6_1_SRCID__VPE_TRAP,
55 				&adev->vpe.trap_irq);
56 	if (ret)
57 		return ret;
58 
59 	return 0;
60 }
61 
62 static int vpe_v3_0_load_microcode(struct amdgpu_vpe *vpe)
63 {
64 	struct amdgpu_device *adev = vpe->ring.adev;
65 	const struct vpe_firmware_header_v1_0 *vpe_hdr;
66 	const __le32 *data;
67 	uint32_t ucode_offset[2], ucode_size[2], size_dw, ret;
68 	uint32_t f32_offset, f32_cntl, reg_data;
69 
70 	ret = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL));
71 	ret = REG_SET_FIELD(ret, VPEC_CNTL, UMSCH_INT_ENABLE, 0);
72 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL), ret);
73 
74 	reg_data = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL2));
75 	reg_data = REG_SET_FIELD(reg_data, VPEC_CNTL2, IB_FIFO_WATERMARK, 1);
76 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL2), reg_data);
77 
78 	if (amdgpu_vpe_configure_dpm(vpe))
79 		dev_warn(adev->dev, "VPE DPM not enabled.\n");
80 
81 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
82 
83 		f32_offset = vpe_get_reg_offset(vpe, 0, regVPEC_F32_CNTL);
84 		f32_cntl = regVPEC_F32_CNTL_DEFAULT;
85 		f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, HALT, 0);
86 		f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, TH1_RESET, 0);
87 
88 		adev->vpe.cmdbuf_cpu_addr[0] = f32_offset;
89 		adev->vpe.cmdbuf_cpu_addr[1] = f32_cntl;
90 
91 		return amdgpu_vpe_psp_update_sram(adev);
92 	}
93 
94 	/* Halt and Check F32 cleaness */
95 	f32_offset = vpe_get_reg_offset(vpe, 0, regVPEC_F32_CNTL);
96 	f32_cntl = RREG32(f32_offset);
97 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, HALT, 1);
98 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, TH1_RESET, 1);
99 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, TH1_CHECKSUM_CLR, 1);
100 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, TH0_CHECKSUM_CLR, 1);
101 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_F32_CNTL), f32_cntl);
102 
103 	f32_cntl = RREG32(f32_offset);
104 	if (!REG_GET_FIELD(f32_cntl, VPEC_F32_CNTL, HALT)) {
105 		dev_err(adev->dev, "VPEC is not halted");
106 		return -EBUSY;
107 	}
108 
109 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, TH1_CHECKSUM_CLR, 0);
110 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, TH0_CHECKSUM_CLR, 0);
111 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_F32_CNTL), f32_cntl);
112 
113 	reg_data = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_UCODE_CHECKSUM));
114 	if (reg_data) {
115 		dev_err(adev->dev, "VPE FW checksum 0 not clean");
116 		return -EBUSY;
117 	}
118 	reg_data = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_UCODE1_CHECKSUM));
119 	if (reg_data) {
120 		dev_err(adev->dev, "VPE FW checksum 1 not clean");
121 		return -EBUSY;
122 	}
123 
124 	reg_data = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_STATUS2));
125 	if (REG_GET_FIELD(reg_data, VPEC_STATUS2, TH0F32_INSTR_PTR)) {
126 		dev_err(adev->dev, "VPE FW initial status not clean");
127 		return -EBUSY;
128 	}
129 
130 	reg_data = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_STATUS6));
131 	if (REG_GET_FIELD(reg_data, VPEC_STATUS6, TH1F32_INSTR_PTR)) {
132 		dev_err(adev->dev, "VPE FW initial status not clean");
133 		return -EBUSY;
134 	}
135 	/* end of F32 cleaness check */
136 
137 	vpe_hdr = (const struct vpe_firmware_header_v1_0 *)adev->vpe.fw->data;
138 
139 	/* Thread 0(command thread) ucode offset/size */
140 	ucode_offset[0] = le32_to_cpu(vpe_hdr->header.ucode_array_offset_bytes);
141 	ucode_size[0] = le32_to_cpu(vpe_hdr->ctx_ucode_size_bytes);
142 	/* Thread 1(control thread) ucode offset/size */
143 	ucode_offset[1] = le32_to_cpu(vpe_hdr->ctl_ucode_offset);
144 	ucode_size[1] = le32_to_cpu(vpe_hdr->ctl_ucode_size_bytes);
145 
146 	reg_data = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_PG_CNTL));
147 	reg_data = REG_SET_FIELD(reg_data, VPEC_PG_CNTL, PG_EN, 0);
148 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_PG_CNTL), reg_data);
149 
150 	for (int j = 0; j < vpe->num_instances; j++) {
151 		for (int i = 0; i < 2; i++) {
152 			if (i > 0)
153 				WREG32(vpe_get_reg_offset(vpe, j, regVPEC_UCODE_ADDR), VPE_THREAD1_UCODE_OFFSET);
154 			else
155 				WREG32(vpe_get_reg_offset(vpe, j, regVPEC_UCODE_ADDR), 0);
156 
157 			data = (const __le32 *)(adev->vpe.fw->data + ucode_offset[i]);
158 			size_dw = ucode_size[i] / sizeof(__le32);
159 
160 			while (size_dw--) {
161 				if (amdgpu_emu_mode && size_dw % 500 == 0)
162 					msleep(1);
163 				WREG32(vpe_get_reg_offset(vpe, j, regVPEC_UCODE_DATA), le32_to_cpup(data++));
164 			}
165 		}
166 	}
167 
168 	reg_data = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_PG_CNTL));
169 	reg_data = REG_SET_FIELD(reg_data, VPEC_PG_CNTL, PG_EN, 1);
170 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_PG_CNTL), reg_data);
171 
172 	/* Unhalt F32 */
173 	f32_cntl = RREG32(f32_offset);
174 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, HALT, 0);
175 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, TH1_RESET, 0);
176 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_F32_CNTL), f32_cntl);
177 
178 	return 0;
179 }
180 
181 static int vpe_v3_0_ring_start(struct amdgpu_vpe *vpe)
182 {
183 	struct amdgpu_ring *ring = &vpe->ring;
184 	struct amdgpu_device *adev = ring->adev;
185 	uint32_t doorbell, doorbell_offset;
186 	uint32_t rb_bufsz, rb_cntl;
187 	uint32_t ib_cntl, i;
188 	int ret;
189 
190 	for (i = 0; i < vpe->num_instances; i++) {
191 		/* Set ring buffer size in dwords */
192 		rb_bufsz = order_base_2(ring->ring_size / 4);
193 		rb_cntl = RREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_CNTL));
194 		rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RB_SIZE, rb_bufsz);
195 		rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RB_PRIV, 1);
196 		rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RB_VMID, 0);
197 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_CNTL), rb_cntl);
198 
199 		/* Initialize the ring buffer's read and write pointers */
200 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_RPTR), 0);
201 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_RPTR_HI), 0);
202 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_WPTR), 0);
203 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_WPTR_HI), 0);
204 
205 		/* set the wb address whether it's enabled or not */
206 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_RPTR_ADDR_LO),
207 			lower_32_bits(ring->rptr_gpu_addr) & 0xFFFFFFFC);
208 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_RPTR_ADDR_HI),
209 			upper_32_bits(ring->rptr_gpu_addr) & 0xFFFFFFFF);
210 
211 		rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RPTR_WRITEBACK_ENABLE, 1);
212 
213 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_BASE), ring->gpu_addr >> 8);
214 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_BASE_HI), ring->gpu_addr >> 40);
215 
216 		ring->wptr = 0;
217 
218 		/* before programing wptr to a less value, need set minor_ptr_update first */
219 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_MINOR_PTR_UPDATE), 1);
220 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_WPTR), lower_32_bits(ring->wptr) << 2);
221 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_WPTR_HI), upper_32_bits(ring->wptr) << 2);
222 		/* set minor_ptr_update to 0 after wptr programed */
223 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_MINOR_PTR_UPDATE), 0);
224 
225 		doorbell_offset = RREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_DOORBELL_OFFSET));
226 		doorbell_offset = REG_SET_FIELD(doorbell_offset, VPEC_QUEUE0_DOORBELL_OFFSET, OFFSET, ring->doorbell_index + i*4);
227 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_DOORBELL_OFFSET), doorbell_offset);
228 
229 		doorbell = RREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_DOORBELL));
230 		doorbell = REG_SET_FIELD(doorbell, VPEC_QUEUE0_DOORBELL, ENABLE, ring->use_doorbell ? 1 : 0);
231 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_DOORBELL), doorbell);
232 
233 		adev->nbio.funcs->vpe_doorbell_range(adev, i, ring->use_doorbell, ring->doorbell_index + i*4, 4);
234 
235 		rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RPTR_WRITEBACK_ENABLE, 1);
236 		rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RB_ENABLE, 1);
237 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_RB_CNTL), rb_cntl);
238 
239 		ib_cntl = RREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_IB_CNTL));
240 		ib_cntl = REG_SET_FIELD(ib_cntl, VPEC_QUEUE0_IB_CNTL, IB_ENABLE, 1);
241 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE0_IB_CNTL), ib_cntl);
242 	}
243 
244 	ret = amdgpu_ring_test_helper(ring);
245 	if (ret)
246 		return ret;
247 
248 	return 0;
249 }
250 
251 static int vpe_v3_0_ring_stop(struct amdgpu_vpe *vpe)
252 {
253 	struct amdgpu_device *adev = vpe->ring.adev;
254 	uint32_t queue_reset, i;
255 	int ret;
256 
257 	for (i = 0; i < vpe->num_instances; i++) {
258 		queue_reset = RREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE_RESET_REQ));
259 
260 		queue_reset = REG_SET_FIELD(queue_reset, VPEC_QUEUE_RESET_REQ, QUEUE0_RESET, 1);
261 
262 		WREG32(vpe_get_reg_offset(vpe, i, regVPEC_QUEUE_RESET_REQ), queue_reset);
263 		/* timeout length is adev->timeout_usec */
264 		ret = SOC15_WAIT_ON_RREG(VPE, i, regVPEC_QUEUE_RESET_REQ, 0,
265 					 VPEC_QUEUE_RESET_REQ__QUEUE0_RESET_MASK);
266 
267 		if (ret)
268 			dev_err(adev->dev, "VPE queue reset failed\n");
269 	}
270 
271 	vpe->ring.sched.ready = false;
272 
273 	return ret;
274 }
275 
276 static int vpe_v3_0_set_trap_irq_state(struct amdgpu_device *adev,
277 				       struct amdgpu_irq_src *source,
278 				       unsigned int type,
279 				       enum amdgpu_interrupt_state state)
280 {
281 	struct amdgpu_vpe *vpe = &adev->vpe;
282 	uint32_t vpe_cntl;
283 
284 	vpe_cntl = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL));
285 	vpe_cntl = REG_SET_FIELD(vpe_cntl, VPEC_CNTL, TRAP_ENABLE,
286 				 state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
287 
288 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL), vpe_cntl);
289 
290 	return 0;
291 }
292 
293 static int vpe_v3_0_process_trap_irq(struct amdgpu_device *adev,
294 				     struct amdgpu_irq_src *source,
295 				     struct amdgpu_iv_entry *entry)
296 {
297 
298 	DRM_DEBUG("IH: VPE trap\n");
299 
300 	switch (entry->client_id) {
301 	case SOC21_IH_CLIENTID_VPE:
302 		amdgpu_fence_process(&adev->vpe.ring);
303 		break;
304 	default:
305 		break;
306 	}
307 
308 	return 0;
309 }
310 
311 static int vpe_v3_0_set_regs(struct amdgpu_vpe *vpe)
312 {
313 	vpe->regs.queue0_rb_rptr_lo = regVPEC_QUEUE0_RB_RPTR;
314 	vpe->regs.queue0_rb_rptr_hi = regVPEC_QUEUE0_RB_RPTR_HI;
315 	vpe->regs.queue0_rb_wptr_lo = regVPEC_QUEUE0_RB_WPTR;
316 	vpe->regs.queue0_rb_wptr_hi = regVPEC_QUEUE0_RB_WPTR_HI;
317 	vpe->regs.queue0_preempt = regVPEC_QUEUE0_PREEMPT;
318 	vpe->regs.dpm_enable = regVPEC_PUB_DUMMY2;
319 
320 	vpe->regs.dpm_pratio = regVPEC_QUEUE6_DUMMY4;
321 	vpe->regs.dpm_request_interval = regVPEC_QUEUE5_DUMMY3;
322 	vpe->regs.dpm_decision_threshold = regVPEC_QUEUE5_DUMMY4;
323 	vpe->regs.dpm_busy_clamp_threshold = regVPEC_QUEUE7_DUMMY2;
324 	vpe->regs.dpm_idle_clamp_threshold = regVPEC_QUEUE7_DUMMY3;
325 	vpe->regs.dpm_request_lv = regVPEC_QUEUE7_DUMMY1;
326 	vpe->regs.context_indicator = regVPEC_QUEUE6_DUMMY3;
327 
328 	return 0;
329 }
330 
331 static struct vpe_funcs vpe_v3_0_funcs = {
332 	.get_reg_offset = vpe_v3_0_get_reg_offset,
333 	.set_regs = vpe_v3_0_set_regs,
334 	.irq_init = vpe_v3_0_irq_init,
335 	.init_microcode = amdgpu_vpe_init_microcode,
336 	.load_microcode = vpe_v3_0_load_microcode,
337 	.ring_init = amdgpu_vpe_ring_init,
338 	.ring_start = vpe_v3_0_ring_start,
339 	.ring_stop = vpe_v3_0_ring_stop,
340 	.ring_fini = amdgpu_vpe_ring_fini,
341 };
342 
343 static const struct amdgpu_irq_src_funcs vpe_v3_0_trap_irq_funcs = {
344 	.set = vpe_v3_0_set_trap_irq_state,
345 	.process = vpe_v3_0_process_trap_irq,
346 };
347 
348 void vpe_v3_0_set_funcs(struct amdgpu_vpe *vpe)
349 {
350 	vpe->funcs = &vpe_v3_0_funcs;
351 	vpe->trap_irq.funcs = &vpe_v3_0_trap_irq_funcs;
352 }
353