xref: /linux/drivers/gpu/drm/amd/amdgpu/vpe_v6_1.c (revision 16e5ac127d8d18adf85fe5ba847d77b58d1ed418)
1 /*
2  * Copyright 2022 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 <drm/drm_drv.h>
25 
26 #include "amdgpu.h"
27 #include "amdgpu_ucode.h"
28 #include "amdgpu_vpe.h"
29 #include "vpe_v6_1.h"
30 #include "soc15_common.h"
31 #include "ivsrcid/vpe/irqsrcs_vpe_6_1.h"
32 #include "vpe/vpe_6_1_0_offset.h"
33 #include "vpe/vpe_6_1_0_sh_mask.h"
34 
35 MODULE_FIRMWARE("amdgpu/vpe_6_1_0.bin");
36 
37 #define VPE_THREAD1_UCODE_OFFSET	0x8000
38 
39 static uint32_t vpe_v6_1_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][0][0];
44 
45 	return base + offset;
46 }
47 
48 static void vpe_v6_1_halt(struct amdgpu_vpe *vpe, bool halt)
49 {
50 	struct amdgpu_device *adev = vpe->ring.adev;
51 	uint32_t f32_cntl;
52 
53 	f32_cntl = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_F32_CNTL));
54 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, HALT, halt ? 1 : 0);
55 	f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, TH1_RESET, halt ? 1 : 0);
56 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_F32_CNTL), f32_cntl);
57 }
58 
59 static int vpe_v6_1_irq_init(struct amdgpu_vpe *vpe)
60 {
61 	struct amdgpu_device *adev = container_of(vpe, struct amdgpu_device, vpe);
62 	int ret;
63 
64 	ret = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_VPE,
65 				VPE_6_1_SRCID__VPE_TRAP,
66 				&adev->vpe.trap_irq);
67 	if (ret)
68 		return ret;
69 
70 	return 0;
71 }
72 
73 static int vpe_v6_1_load_microcode(struct amdgpu_vpe *vpe)
74 {
75 	struct amdgpu_device *adev = vpe->ring.adev;
76 	const struct vpe_firmware_header_v1_0 *vpe_hdr;
77 	const __le32 *data;
78 	uint32_t ucode_offset[2], ucode_size[2];
79 	uint32_t i, size_dw;
80 	uint32_t ret;
81 
82 	// disable UMSCH_INT_ENABLE
83 	ret = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL));
84 	ret = REG_SET_FIELD(ret, VPEC_CNTL, UMSCH_INT_ENABLE, 0);
85 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL), ret);
86 
87 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
88 		uint32_t f32_offset, f32_cntl;
89 
90 		f32_offset = vpe_get_reg_offset(vpe, 0, regVPEC_F32_CNTL);
91 		f32_cntl = RREG32(f32_offset);
92 		f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, HALT, 0);
93 		f32_cntl = REG_SET_FIELD(f32_cntl, VPEC_F32_CNTL, TH1_RESET, 0);
94 
95 		adev->vpe.cmdbuf_cpu_addr[0] = f32_offset;
96 		adev->vpe.cmdbuf_cpu_addr[1] = f32_cntl;
97 
98 		amdgpu_vpe_psp_update_sram(adev);
99 		return 0;
100 	}
101 
102 	vpe_hdr = (const struct vpe_firmware_header_v1_0 *)adev->vpe.fw->data;
103 
104 	/* Thread 0(command thread) ucode offset/size */
105 	ucode_offset[0] = le32_to_cpu(vpe_hdr->header.ucode_array_offset_bytes);
106 	ucode_size[0] = le32_to_cpu(vpe_hdr->ctx_ucode_size_bytes);
107 	/* Thread 1(control thread) ucode offset/size */
108 	ucode_offset[1] = le32_to_cpu(vpe_hdr->ctl_ucode_offset);
109 	ucode_size[1] = le32_to_cpu(vpe_hdr->ctl_ucode_size_bytes);
110 
111 	vpe_v6_1_halt(vpe, true);
112 
113 	for (i = 0; i < 2; i++) {
114 		if (i > 0)
115 			WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_UCODE_ADDR), VPE_THREAD1_UCODE_OFFSET);
116 		else
117 			WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_UCODE_ADDR), 0);
118 
119 		data = (const __le32 *)(adev->vpe.fw->data + ucode_offset[i]);
120 		size_dw = ucode_size[i] / sizeof(__le32);
121 
122 		while (size_dw--) {
123 			if (amdgpu_emu_mode && size_dw % 500 == 0)
124 				msleep(1);
125 			WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_UCODE_DATA), le32_to_cpup(data++));
126 		}
127 
128 	}
129 
130 	vpe_v6_1_halt(vpe, false);
131 
132 	return 0;
133 }
134 
135 static int vpe_v6_1_ring_start(struct amdgpu_vpe *vpe)
136 {
137 	struct amdgpu_ring *ring = &vpe->ring;
138 	struct amdgpu_device *adev = ring->adev;
139 	uint32_t rb_bufsz, rb_cntl;
140 	uint32_t ib_cntl;
141 	uint32_t doorbell, doorbell_offset;
142 	int ret;
143 
144 	rb_bufsz = order_base_2(ring->ring_size / 4);
145 	rb_cntl = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_CNTL));
146 	rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RB_SIZE, rb_bufsz);
147 	rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RB_PRIV, 1);
148 	rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RB_VMID, 0);
149 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_CNTL), rb_cntl);
150 
151 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_RPTR), 0);
152 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_RPTR_HI), 0);
153 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_WPTR), 0);
154 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_WPTR_HI), 0);
155 
156 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_RPTR_ADDR_LO),
157 	       lower_32_bits(ring->rptr_gpu_addr) & 0xFFFFFFFC);
158 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_RPTR_ADDR_HI),
159 	       upper_32_bits(ring->rptr_gpu_addr) & 0xFFFFFFFF);
160 
161 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_BASE), ring->gpu_addr >> 8);
162 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_BASE_HI), ring->gpu_addr >> 40);
163 
164 	ring->wptr = 0;
165 
166 	/* before programing wptr to a less value, need set minor_ptr_update first */
167 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_MINOR_PTR_UPDATE), 1);
168 
169 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_WPTR), lower_32_bits(ring->wptr) << 2);
170 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_WPTR_HI), upper_32_bits(ring->wptr) << 2);
171 
172 	/* set minor_ptr_update to 0 after wptr programed */
173 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_MINOR_PTR_UPDATE), 0);
174 
175 	doorbell = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_DOORBELL));
176 	doorbell_offset = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_DOORBELL_OFFSET));
177 
178 	doorbell = REG_SET_FIELD(doorbell, VPEC_QUEUE0_DOORBELL, ENABLE, ring->use_doorbell ? 1 : 0);
179 	doorbell_offset = REG_SET_FIELD(doorbell_offset, VPEC_QUEUE0_DOORBELL_OFFSET, OFFSET, ring->doorbell_index);
180 
181 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_DOORBELL), doorbell);
182 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_DOORBELL_OFFSET), doorbell_offset);
183 
184 	adev->nbio.funcs->vpe_doorbell_range(adev, 0, ring->use_doorbell, ring->doorbell_index, 2);
185 
186 	rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RPTR_WRITEBACK_ENABLE, 1);
187 	rb_cntl = REG_SET_FIELD(rb_cntl, VPEC_QUEUE0_RB_CNTL, RB_ENABLE, 1);
188 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_RB_CNTL), rb_cntl);
189 
190 	ib_cntl = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_IB_CNTL));
191 	ib_cntl = REG_SET_FIELD(ib_cntl, VPEC_QUEUE0_IB_CNTL, IB_ENABLE, 1);
192 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_IB_CNTL), ib_cntl);
193 
194 	ring->sched.ready = true;
195 
196 	ret = amdgpu_ring_test_helper(ring);
197 	if (ret) {
198 		ring->sched.ready = false;
199 		return ret;
200 	}
201 
202 	return 0;
203 }
204 
205 static int vpe_v_6_1_ring_stop(struct amdgpu_vpe *vpe)
206 {
207 	struct amdgpu_device *adev = vpe->ring.adev;
208 	uint32_t queue_reset;
209 	int ret;
210 
211 	queue_reset = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE_RESET_REQ));
212 	queue_reset = REG_SET_FIELD(queue_reset, VPEC_QUEUE_RESET_REQ, QUEUE0_RESET, 1);
213 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE_RESET_REQ), queue_reset);
214 
215 	ret = SOC15_WAIT_ON_RREG(VPE, 0, regVPEC_QUEUE_RESET_REQ, 0,
216 				 VPEC_QUEUE_RESET_REQ__QUEUE0_RESET_MASK);
217 	if (ret)
218 		dev_err(adev->dev, "VPE queue reset failed\n");
219 
220 	vpe->ring.sched.ready = false;
221 
222 	return ret;
223 }
224 
225 static int vpe_v6_1_set_trap_irq_state(struct amdgpu_device *adev,
226 				       struct amdgpu_irq_src *source,
227 				       unsigned int type,
228 				       enum amdgpu_interrupt_state state)
229 {
230 	struct amdgpu_vpe *vpe = &adev->vpe;
231 	uint32_t vpe_cntl;
232 
233 	vpe_cntl = RREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL));
234 	vpe_cntl = REG_SET_FIELD(vpe_cntl, VPEC_CNTL, TRAP_ENABLE,
235 				 state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
236 	WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_CNTL), vpe_cntl);
237 
238 	return 0;
239 }
240 
241 static int vpe_v6_1_process_trap_irq(struct amdgpu_device *adev,
242 				     struct amdgpu_irq_src *source,
243 				     struct amdgpu_iv_entry *entry)
244 {
245 
246 	dev_dbg(adev->dev, "IH: VPE trap\n");
247 
248 	switch (entry->client_id) {
249 	case SOC21_IH_CLIENTID_VPE:
250 		amdgpu_fence_process(&adev->vpe.ring);
251 		break;
252 	default:
253 		break;
254 	}
255 
256 	return 0;
257 }
258 
259 static int vpe_v6_1_set_regs(struct amdgpu_vpe *vpe)
260 {
261 	vpe->regs.queue0_rb_rptr_lo = regVPEC_QUEUE0_RB_RPTR;
262 	vpe->regs.queue0_rb_rptr_hi = regVPEC_QUEUE0_RB_RPTR_HI;
263 	vpe->regs.queue0_rb_wptr_lo = regVPEC_QUEUE0_RB_WPTR;
264 	vpe->regs.queue0_rb_wptr_hi = regVPEC_QUEUE0_RB_WPTR_HI;
265 	vpe->regs.queue0_preempt = regVPEC_QUEUE0_PREEMPT;
266 
267 	return 0;
268 }
269 
270 static const struct vpe_funcs vpe_v6_1_funcs = {
271 	.get_reg_offset = vpe_v6_1_get_reg_offset,
272 	.set_regs = vpe_v6_1_set_regs,
273 	.irq_init = vpe_v6_1_irq_init,
274 	.init_microcode = amdgpu_vpe_init_microcode,
275 	.load_microcode = vpe_v6_1_load_microcode,
276 	.ring_init = amdgpu_vpe_ring_init,
277 	.ring_start = vpe_v6_1_ring_start,
278 	.ring_stop = vpe_v_6_1_ring_stop,
279 	.ring_fini = amdgpu_vpe_ring_fini,
280 };
281 
282 static const struct amdgpu_irq_src_funcs vpe_v6_1_trap_irq_funcs = {
283 	.set = vpe_v6_1_set_trap_irq_state,
284 	.process = vpe_v6_1_process_trap_irq,
285 };
286 
287 void vpe_v6_1_set_funcs(struct amdgpu_vpe *vpe)
288 {
289 	vpe->funcs = &vpe_v6_1_funcs;
290 	vpe->trap_irq.funcs = &vpe_v6_1_trap_irq_funcs;
291 }
292