2 * Copyright 2022 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.
23 #include <linux/firmware.h>
24 #include <drm/drm_drv.h>
27 #include "amdgpu_ucode.h"
28 #include "amdgpu_vpe.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"
35 MODULE_FIRMWARE("amdgpu/vpe_6_1_0.bin");
37 #define VPE_THREAD1_UCODE_OFFSET 0x8000
39 static uint32_t vpe_v6_1_get_reg_offset(struct amdgpu_vpe *vpe, uint32_t inst, uint32_t offset)
43 base = vpe->ring.adev->reg_offset[VPE_HWIP][0][0];
48 static void vpe_v6_1_halt(struct amdgpu_vpe *vpe, bool halt)
50 struct amdgpu_device *adev = vpe->ring.adev;
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);
59 static int vpe_v6_1_irq_init(struct amdgpu_vpe *vpe)
61 struct amdgpu_device *adev = container_of(vpe, struct amdgpu_device, vpe);
64 ret = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_VPE,
65 VPE_6_1_SRCID__VPE_TRAP,
73 static int vpe_v6_1_load_microcode(struct amdgpu_vpe *vpe)
75 struct amdgpu_device *adev = vpe->ring.adev;
76 const struct vpe_firmware_header_v1_0 *vpe_hdr;
78 uint32_t ucode_offset[2], ucode_size[2];
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);
87 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
88 uint32_t f32_offset, f32_cntl;
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);
95 adev->vpe.cmdbuf_cpu_addr[0] = f32_offset;
96 adev->vpe.cmdbuf_cpu_addr[1] = f32_cntl;
98 amdgpu_vpe_psp_update_sram(adev);
102 vpe_hdr = (const struct vpe_firmware_header_v1_0 *)adev->vpe.fw->data;
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);
111 vpe_v6_1_halt(vpe, true);
113 for (i = 0; i < 2; i++) {
115 WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_UCODE_ADDR), VPE_THREAD1_UCODE_OFFSET);
117 WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_UCODE_ADDR), 0);
119 data = (const __le32 *)(adev->vpe.fw->data + ucode_offset[i]);
120 size_dw = ucode_size[i] / sizeof(__le32);
123 if (amdgpu_emu_mode && size_dw % 500 == 0)
125 WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_UCODE_DATA), le32_to_cpup(data++));
130 vpe_v6_1_halt(vpe, false);
135 static int vpe_v6_1_ring_start(struct amdgpu_vpe *vpe)
137 struct amdgpu_ring *ring = &vpe->ring;
138 struct amdgpu_device *adev = ring->adev;
139 uint32_t rb_bufsz, rb_cntl;
141 uint32_t doorbell, doorbell_offset;
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);
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);
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);
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);
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);
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);
172 /* set minor_ptr_update to 0 after wptr programed */
173 WREG32(vpe_get_reg_offset(vpe, 0, regVPEC_QUEUE0_MINOR_PTR_UPDATE), 0);
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));
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);
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);
184 adev->nbio.funcs->vpe_doorbell_range(adev, 0, ring->use_doorbell, ring->doorbell_index, 2);
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);
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);
194 ring->sched.ready = true;
196 ret = amdgpu_ring_test_helper(ring);
198 ring->sched.ready = false;
205 static int vpe_v_6_1_ring_stop(struct amdgpu_vpe *vpe)
207 struct amdgpu_device *adev = vpe->ring.adev;
208 uint32_t queue_reset;
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);
215 ret = SOC15_WAIT_ON_RREG(VPE, 0, regVPEC_QUEUE_RESET_REQ, 0,
216 VPEC_QUEUE_RESET_REQ__QUEUE0_RESET_MASK);
218 dev_err(adev->dev, "VPE queue reset failed\n");
220 vpe->ring.sched.ready = false;
225 static int vpe_v6_1_set_trap_irq_state(struct amdgpu_device *adev,
226 struct amdgpu_irq_src *source,
228 enum amdgpu_interrupt_state state)
230 struct amdgpu_vpe *vpe = &adev->vpe;
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);
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)
246 dev_dbg(adev->dev, "IH: VPE trap\n");
248 switch (entry->client_id) {
249 case SOC21_IH_CLIENTID_VPE:
250 amdgpu_fence_process(&adev->vpe.ring);
259 static int vpe_v6_1_set_regs(struct amdgpu_vpe *vpe)
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;
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,
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,
287 void vpe_v6_1_set_funcs(struct amdgpu_vpe *vpe)
289 vpe->funcs = &vpe_v6_1_funcs;
290 vpe->trap_irq.funcs = &vpe_v6_1_trap_irq_funcs;