2 * Copyright 2019 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.
25 #include "amdgpu_jpeg.h"
26 #include "amdgpu_pm.h"
29 #include "jpeg_v2_0.h"
31 #include "vcn/vcn_2_0_0_offset.h"
32 #include "vcn/vcn_2_0_0_sh_mask.h"
33 #include "ivsrcid/vcn/irqsrcs_vcn_2_0.h"
35 static void jpeg_v2_0_set_dec_ring_funcs(struct amdgpu_device *adev);
36 static void jpeg_v2_0_set_irq_funcs(struct amdgpu_device *adev);
37 static int jpeg_v2_0_set_powergating_state(void *handle,
38 enum amd_powergating_state state);
41 * jpeg_v2_0_early_init - set function pointers
43 * @handle: amdgpu_device pointer
45 * Set ring and irq function pointers
47 static int jpeg_v2_0_early_init(void *handle)
49 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
51 adev->jpeg.num_jpeg_inst = 1;
53 jpeg_v2_0_set_dec_ring_funcs(adev);
54 jpeg_v2_0_set_irq_funcs(adev);
60 * jpeg_v2_0_sw_init - sw init for JPEG block
62 * @handle: amdgpu_device pointer
64 * Load firmware and sw initialization
66 static int jpeg_v2_0_sw_init(void *handle)
68 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
69 struct amdgpu_ring *ring;
73 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VCN,
74 VCN_2_0__SRCID__JPEG_DECODE, &adev->jpeg.inst->irq);
78 r = amdgpu_jpeg_sw_init(adev);
82 r = amdgpu_jpeg_resume(adev);
86 ring = &adev->jpeg.inst->ring_dec;
87 ring->use_doorbell = true;
88 ring->doorbell_index = (adev->doorbell_index.vcn.vcn_ring0_1 << 1) + 1;
89 sprintf(ring->name, "jpeg_dec");
90 r = amdgpu_ring_init(adev, ring, 512, &adev->jpeg.inst->irq,
91 0, AMDGPU_RING_PRIO_DEFAULT, NULL);
95 adev->jpeg.internal.jpeg_pitch = mmUVD_JPEG_PITCH_INTERNAL_OFFSET;
96 adev->jpeg.inst->external.jpeg_pitch = SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_PITCH);
102 * jpeg_v2_0_sw_fini - sw fini for JPEG block
104 * @handle: amdgpu_device pointer
106 * JPEG suspend and free up sw allocation
108 static int jpeg_v2_0_sw_fini(void *handle)
111 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
113 r = amdgpu_jpeg_suspend(adev);
117 r = amdgpu_jpeg_sw_fini(adev);
123 * jpeg_v2_0_hw_init - start and test JPEG block
125 * @handle: amdgpu_device pointer
128 static int jpeg_v2_0_hw_init(void *handle)
130 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
131 struct amdgpu_ring *ring = &adev->jpeg.inst->ring_dec;
134 adev->nbio.funcs->vcn_doorbell_range(adev, ring->use_doorbell,
135 (adev->doorbell_index.vcn.vcn_ring0_1 << 1), 0);
137 r = amdgpu_ring_test_helper(ring);
139 DRM_INFO("JPEG decode initialized successfully.\n");
145 * jpeg_v2_0_hw_fini - stop the hardware block
147 * @handle: amdgpu_device pointer
149 * Stop the JPEG block, mark ring as not ready any more
151 static int jpeg_v2_0_hw_fini(void *handle)
153 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
155 cancel_delayed_work_sync(&adev->vcn.idle_work);
157 if (adev->jpeg.cur_state != AMD_PG_STATE_GATE &&
158 RREG32_SOC15(JPEG, 0, mmUVD_JRBC_STATUS))
159 jpeg_v2_0_set_powergating_state(adev, AMD_PG_STATE_GATE);
165 * jpeg_v2_0_suspend - suspend JPEG block
167 * @handle: amdgpu_device pointer
169 * HW fini and suspend JPEG block
171 static int jpeg_v2_0_suspend(void *handle)
173 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
176 r = jpeg_v2_0_hw_fini(adev);
180 r = amdgpu_jpeg_suspend(adev);
186 * jpeg_v2_0_resume - resume JPEG block
188 * @handle: amdgpu_device pointer
190 * Resume firmware and hw init JPEG block
192 static int jpeg_v2_0_resume(void *handle)
195 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
197 r = amdgpu_jpeg_resume(adev);
201 r = jpeg_v2_0_hw_init(adev);
206 static int jpeg_v2_0_disable_power_gating(struct amdgpu_device *adev)
211 if (adev->pg_flags & AMD_PG_SUPPORT_JPEG) {
212 data = 1 << UVD_PGFSM_CONFIG__UVDJ_PWR_CONFIG__SHIFT;
213 WREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_PGFSM_CONFIG), data);
215 r = SOC15_WAIT_ON_RREG(JPEG, 0,
216 mmUVD_PGFSM_STATUS, UVD_PGFSM_STATUS_UVDJ_PWR_ON,
217 UVD_PGFSM_STATUS__UVDJ_PWR_STATUS_MASK);
220 DRM_ERROR("amdgpu: JPEG disable power gating failed\n");
225 /* Removing the anti hang mechanism to indicate the UVDJ tile is ON */
226 data = RREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_POWER_STATUS)) & ~0x1;
227 WREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_POWER_STATUS), data);
232 static int jpeg_v2_0_enable_power_gating(struct amdgpu_device *adev)
234 if (adev->pg_flags & AMD_PG_SUPPORT_JPEG) {
238 data = RREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_POWER_STATUS));
239 data &= ~UVD_JPEG_POWER_STATUS__JPEG_POWER_STATUS_MASK;
240 data |= 0x1; //UVD_JPEG_POWER_STATUS__JPEG_POWER_STATUS_TILES_OFF;
241 WREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_POWER_STATUS), data);
243 data = 2 << UVD_PGFSM_CONFIG__UVDJ_PWR_CONFIG__SHIFT;
244 WREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_PGFSM_CONFIG), data);
246 r = SOC15_WAIT_ON_RREG(JPEG, 0, mmUVD_PGFSM_STATUS,
247 (2 << UVD_PGFSM_STATUS__UVDJ_PWR_STATUS__SHIFT),
248 UVD_PGFSM_STATUS__UVDJ_PWR_STATUS_MASK);
251 DRM_ERROR("amdgpu: JPEG enable power gating failed\n");
259 static void jpeg_v2_0_disable_clock_gating(struct amdgpu_device *adev)
263 data = RREG32_SOC15(JPEG, 0, mmJPEG_CGC_CTRL);
264 if (adev->cg_flags & AMD_CG_SUPPORT_JPEG_MGCG)
265 data |= 1 << JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
267 data &= ~JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
269 data |= 1 << JPEG_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
270 data |= 4 << JPEG_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
271 WREG32_SOC15(JPEG, 0, mmJPEG_CGC_CTRL, data);
273 data = RREG32_SOC15(JPEG, 0, mmJPEG_CGC_GATE);
274 data &= ~(JPEG_CGC_GATE__JPEG_DEC_MASK
275 | JPEG_CGC_GATE__JPEG2_DEC_MASK
276 | JPEG_CGC_GATE__JPEG_ENC_MASK
277 | JPEG_CGC_GATE__JMCIF_MASK
278 | JPEG_CGC_GATE__JRBBM_MASK);
279 WREG32_SOC15(JPEG, 0, mmJPEG_CGC_GATE, data);
282 static void jpeg_v2_0_enable_clock_gating(struct amdgpu_device *adev)
286 data = RREG32_SOC15(JPEG, 0, mmJPEG_CGC_CTRL);
287 if (adev->cg_flags & AMD_CG_SUPPORT_JPEG_MGCG)
288 data |= 1 << JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
290 data |= 0 << JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
292 data |= 1 << JPEG_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
293 data |= 4 << JPEG_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
294 WREG32_SOC15(JPEG, 0, mmJPEG_CGC_CTRL, data);
296 data = RREG32_SOC15(JPEG, 0, mmJPEG_CGC_GATE);
297 data |= (JPEG_CGC_GATE__JPEG_DEC_MASK
298 |JPEG_CGC_GATE__JPEG2_DEC_MASK
299 |JPEG_CGC_GATE__JPEG_ENC_MASK
300 |JPEG_CGC_GATE__JMCIF_MASK
301 |JPEG_CGC_GATE__JRBBM_MASK);
302 WREG32_SOC15(JPEG, 0, mmJPEG_CGC_GATE, data);
306 * jpeg_v2_0_start - start JPEG block
308 * @adev: amdgpu_device pointer
310 * Setup and start the JPEG block
312 static int jpeg_v2_0_start(struct amdgpu_device *adev)
314 struct amdgpu_ring *ring = &adev->jpeg.inst->ring_dec;
317 if (adev->pm.dpm_enabled)
318 amdgpu_dpm_enable_jpeg(adev, true);
320 /* disable power gating */
321 r = jpeg_v2_0_disable_power_gating(adev);
325 /* JPEG disable CGC */
326 jpeg_v2_0_disable_clock_gating(adev);
328 WREG32_SOC15(JPEG, 0, mmJPEG_DEC_GFX10_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
330 /* enable JMI channel */
331 WREG32_P(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JMI_CNTL), 0,
332 ~UVD_JMI_CNTL__SOFT_RESET_MASK);
334 /* enable System Interrupt for JRBC */
335 WREG32_P(SOC15_REG_OFFSET(JPEG, 0, mmJPEG_SYS_INT_EN),
336 JPEG_SYS_INT_EN__DJRBC_MASK,
337 ~JPEG_SYS_INT_EN__DJRBC_MASK);
339 WREG32_SOC15(JPEG, 0, mmUVD_LMI_JRBC_RB_VMID, 0);
340 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_CNTL, (0x00000001L | 0x00000002L));
341 WREG32_SOC15(JPEG, 0, mmUVD_LMI_JRBC_RB_64BIT_BAR_LOW,
342 lower_32_bits(ring->gpu_addr));
343 WREG32_SOC15(JPEG, 0, mmUVD_LMI_JRBC_RB_64BIT_BAR_HIGH,
344 upper_32_bits(ring->gpu_addr));
345 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_RPTR, 0);
346 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_WPTR, 0);
347 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_CNTL, 0x00000002L);
348 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_SIZE, ring->ring_size / 4);
349 ring->wptr = RREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_WPTR);
355 * jpeg_v2_0_stop - stop JPEG block
357 * @adev: amdgpu_device pointer
359 * stop the JPEG block
361 static int jpeg_v2_0_stop(struct amdgpu_device *adev)
366 WREG32_P(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JMI_CNTL),
367 UVD_JMI_CNTL__SOFT_RESET_MASK,
368 ~UVD_JMI_CNTL__SOFT_RESET_MASK);
370 /* enable JPEG CGC */
371 jpeg_v2_0_enable_clock_gating(adev);
373 /* enable power gating */
374 r = jpeg_v2_0_enable_power_gating(adev);
378 if (adev->pm.dpm_enabled)
379 amdgpu_dpm_enable_jpeg(adev, false);
385 * jpeg_v2_0_dec_ring_get_rptr - get read pointer
387 * @ring: amdgpu_ring pointer
389 * Returns the current hardware read pointer
391 static uint64_t jpeg_v2_0_dec_ring_get_rptr(struct amdgpu_ring *ring)
393 struct amdgpu_device *adev = ring->adev;
395 return RREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_RPTR);
399 * jpeg_v2_0_dec_ring_get_wptr - get write pointer
401 * @ring: amdgpu_ring pointer
403 * Returns the current hardware write pointer
405 static uint64_t jpeg_v2_0_dec_ring_get_wptr(struct amdgpu_ring *ring)
407 struct amdgpu_device *adev = ring->adev;
409 if (ring->use_doorbell)
410 return *ring->wptr_cpu_addr;
412 return RREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_WPTR);
416 * jpeg_v2_0_dec_ring_set_wptr - set write pointer
418 * @ring: amdgpu_ring pointer
420 * Commits the write pointer to the hardware
422 static void jpeg_v2_0_dec_ring_set_wptr(struct amdgpu_ring *ring)
424 struct amdgpu_device *adev = ring->adev;
426 if (ring->use_doorbell) {
427 *ring->wptr_cpu_addr = lower_32_bits(ring->wptr);
428 WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr));
430 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_WPTR, lower_32_bits(ring->wptr));
435 * jpeg_v2_0_dec_ring_insert_start - insert a start command
437 * @ring: amdgpu_ring pointer
439 * Write a start command to the ring.
441 void jpeg_v2_0_dec_ring_insert_start(struct amdgpu_ring *ring)
443 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
444 0, 0, PACKETJ_TYPE0));
445 amdgpu_ring_write(ring, 0x68e04);
447 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
448 0, 0, PACKETJ_TYPE0));
449 amdgpu_ring_write(ring, 0x80010000);
453 * jpeg_v2_0_dec_ring_insert_end - insert a end command
455 * @ring: amdgpu_ring pointer
457 * Write a end command to the ring.
459 void jpeg_v2_0_dec_ring_insert_end(struct amdgpu_ring *ring)
461 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
462 0, 0, PACKETJ_TYPE0));
463 amdgpu_ring_write(ring, 0x68e04);
465 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
466 0, 0, PACKETJ_TYPE0));
467 amdgpu_ring_write(ring, 0x00010000);
471 * jpeg_v2_0_dec_ring_emit_fence - emit an fence & trap command
473 * @ring: amdgpu_ring pointer
475 * @seq: sequence number
476 * @flags: fence related flags
478 * Write a fence and a trap command to the ring.
480 void jpeg_v2_0_dec_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
483 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
485 amdgpu_ring_write(ring, PACKETJ(mmUVD_JPEG_GPCOM_DATA0_INTERNAL_OFFSET,
486 0, 0, PACKETJ_TYPE0));
487 amdgpu_ring_write(ring, seq);
489 amdgpu_ring_write(ring, PACKETJ(mmUVD_JPEG_GPCOM_DATA1_INTERNAL_OFFSET,
490 0, 0, PACKETJ_TYPE0));
491 amdgpu_ring_write(ring, seq);
493 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_RB_MEM_WR_64BIT_BAR_LOW_INTERNAL_OFFSET,
494 0, 0, PACKETJ_TYPE0));
495 amdgpu_ring_write(ring, lower_32_bits(addr));
497 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_RB_MEM_WR_64BIT_BAR_HIGH_INTERNAL_OFFSET,
498 0, 0, PACKETJ_TYPE0));
499 amdgpu_ring_write(ring, upper_32_bits(addr));
501 amdgpu_ring_write(ring, PACKETJ(mmUVD_JPEG_GPCOM_CMD_INTERNAL_OFFSET,
502 0, 0, PACKETJ_TYPE0));
503 amdgpu_ring_write(ring, 0x8);
505 amdgpu_ring_write(ring, PACKETJ(mmUVD_JPEG_GPCOM_CMD_INTERNAL_OFFSET,
506 0, PACKETJ_CONDITION_CHECK0, PACKETJ_TYPE4));
507 amdgpu_ring_write(ring, 0);
509 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
510 0, 0, PACKETJ_TYPE0));
511 amdgpu_ring_write(ring, 0x3fbc);
513 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
514 0, 0, PACKETJ_TYPE0));
515 amdgpu_ring_write(ring, 0x1);
517 amdgpu_ring_write(ring, PACKETJ(0, 0, 0, PACKETJ_TYPE7));
518 amdgpu_ring_write(ring, 0);
522 * jpeg_v2_0_dec_ring_emit_ib - execute indirect buffer
524 * @ring: amdgpu_ring pointer
525 * @job: job to retrieve vmid from
526 * @ib: indirect buffer to execute
529 * Write ring commands to execute the indirect buffer.
531 void jpeg_v2_0_dec_ring_emit_ib(struct amdgpu_ring *ring,
532 struct amdgpu_job *job,
533 struct amdgpu_ib *ib,
536 unsigned vmid = AMDGPU_JOB_GET_VMID(job);
538 amdgpu_ring_write(ring, PACKETJ(mmUVD_JPEG_IH_CTRL_INTERNAL_OFFSET,
539 0, 0, PACKETJ_TYPE0));
540 amdgpu_ring_write(ring, (vmid << JPEG_IH_CTRL__IH_VMID__SHIFT));
542 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_IB_VMID_INTERNAL_OFFSET,
543 0, 0, PACKETJ_TYPE0));
544 amdgpu_ring_write(ring, (vmid | (vmid << 4)));
546 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JPEG_VMID_INTERNAL_OFFSET,
547 0, 0, PACKETJ_TYPE0));
548 amdgpu_ring_write(ring, (vmid | (vmid << 4)));
550 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_IB_64BIT_BAR_LOW_INTERNAL_OFFSET,
551 0, 0, PACKETJ_TYPE0));
552 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
554 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_IB_64BIT_BAR_HIGH_INTERNAL_OFFSET,
555 0, 0, PACKETJ_TYPE0));
556 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
558 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_IB_SIZE_INTERNAL_OFFSET,
559 0, 0, PACKETJ_TYPE0));
560 amdgpu_ring_write(ring, ib->length_dw);
562 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_RB_MEM_RD_64BIT_BAR_LOW_INTERNAL_OFFSET,
563 0, 0, PACKETJ_TYPE0));
564 amdgpu_ring_write(ring, lower_32_bits(ring->gpu_addr));
566 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_RB_MEM_RD_64BIT_BAR_HIGH_INTERNAL_OFFSET,
567 0, 0, PACKETJ_TYPE0));
568 amdgpu_ring_write(ring, upper_32_bits(ring->gpu_addr));
570 amdgpu_ring_write(ring, PACKETJ(0, 0, PACKETJ_CONDITION_CHECK0, PACKETJ_TYPE2));
571 amdgpu_ring_write(ring, 0);
573 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_RB_COND_RD_TIMER_INTERNAL_OFFSET,
574 0, 0, PACKETJ_TYPE0));
575 amdgpu_ring_write(ring, 0x01400200);
577 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_RB_REF_DATA_INTERNAL_OFFSET,
578 0, 0, PACKETJ_TYPE0));
579 amdgpu_ring_write(ring, 0x2);
581 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_STATUS_INTERNAL_OFFSET,
582 0, PACKETJ_CONDITION_CHECK3, PACKETJ_TYPE3));
583 amdgpu_ring_write(ring, 0x2);
586 void jpeg_v2_0_dec_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
587 uint32_t val, uint32_t mask)
589 uint32_t reg_offset = (reg << 2);
591 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_RB_COND_RD_TIMER_INTERNAL_OFFSET,
592 0, 0, PACKETJ_TYPE0));
593 amdgpu_ring_write(ring, 0x01400200);
595 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_RB_REF_DATA_INTERNAL_OFFSET,
596 0, 0, PACKETJ_TYPE0));
597 amdgpu_ring_write(ring, val);
599 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
600 0, 0, PACKETJ_TYPE0));
601 if (reg_offset >= 0x10000 && reg_offset <= 0x105ff) {
602 amdgpu_ring_write(ring, 0);
603 amdgpu_ring_write(ring,
604 PACKETJ((reg_offset >> 2), 0, 0, PACKETJ_TYPE3));
606 amdgpu_ring_write(ring, reg_offset);
607 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
608 0, 0, PACKETJ_TYPE3));
610 amdgpu_ring_write(ring, mask);
613 void jpeg_v2_0_dec_ring_emit_vm_flush(struct amdgpu_ring *ring,
614 unsigned vmid, uint64_t pd_addr)
616 struct amdgpu_vmhub *hub = &ring->adev->vmhub[ring->funcs->vmhub];
617 uint32_t data0, data1, mask;
619 pd_addr = amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
621 /* wait for register write */
622 data0 = hub->ctx0_ptb_addr_lo32 + vmid * hub->ctx_addr_distance;
623 data1 = lower_32_bits(pd_addr);
625 jpeg_v2_0_dec_ring_emit_reg_wait(ring, data0, data1, mask);
628 void jpeg_v2_0_dec_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg, uint32_t val)
630 uint32_t reg_offset = (reg << 2);
632 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
633 0, 0, PACKETJ_TYPE0));
634 if (reg_offset >= 0x10000 && reg_offset <= 0x105ff) {
635 amdgpu_ring_write(ring, 0);
636 amdgpu_ring_write(ring,
637 PACKETJ((reg_offset >> 2), 0, 0, PACKETJ_TYPE0));
639 amdgpu_ring_write(ring, reg_offset);
640 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
641 0, 0, PACKETJ_TYPE0));
643 amdgpu_ring_write(ring, val);
646 void jpeg_v2_0_dec_ring_nop(struct amdgpu_ring *ring, uint32_t count)
650 WARN_ON(ring->wptr % 2 || count % 2);
652 for (i = 0; i < count / 2; i++) {
653 amdgpu_ring_write(ring, PACKETJ(0, 0, 0, PACKETJ_TYPE6));
654 amdgpu_ring_write(ring, 0);
658 static bool jpeg_v2_0_is_idle(void *handle)
660 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
662 return ((RREG32_SOC15(JPEG, 0, mmUVD_JRBC_STATUS) &
663 UVD_JRBC_STATUS__RB_JOB_DONE_MASK) ==
664 UVD_JRBC_STATUS__RB_JOB_DONE_MASK);
667 static int jpeg_v2_0_wait_for_idle(void *handle)
669 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
672 ret = SOC15_WAIT_ON_RREG(JPEG, 0, mmUVD_JRBC_STATUS, UVD_JRBC_STATUS__RB_JOB_DONE_MASK,
673 UVD_JRBC_STATUS__RB_JOB_DONE_MASK);
678 static int jpeg_v2_0_set_clockgating_state(void *handle,
679 enum amd_clockgating_state state)
681 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
682 bool enable = (state == AMD_CG_STATE_GATE);
685 if (!jpeg_v2_0_is_idle(handle))
687 jpeg_v2_0_enable_clock_gating(adev);
689 jpeg_v2_0_disable_clock_gating(adev);
695 static int jpeg_v2_0_set_powergating_state(void *handle,
696 enum amd_powergating_state state)
698 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
701 if (state == adev->jpeg.cur_state)
704 if (state == AMD_PG_STATE_GATE)
705 ret = jpeg_v2_0_stop(adev);
707 ret = jpeg_v2_0_start(adev);
710 adev->jpeg.cur_state = state;
715 static int jpeg_v2_0_set_interrupt_state(struct amdgpu_device *adev,
716 struct amdgpu_irq_src *source,
718 enum amdgpu_interrupt_state state)
723 static int jpeg_v2_0_process_interrupt(struct amdgpu_device *adev,
724 struct amdgpu_irq_src *source,
725 struct amdgpu_iv_entry *entry)
727 DRM_DEBUG("IH: JPEG TRAP\n");
729 switch (entry->src_id) {
730 case VCN_2_0__SRCID__JPEG_DECODE:
731 amdgpu_fence_process(&adev->jpeg.inst->ring_dec);
734 DRM_ERROR("Unhandled interrupt: %d %d\n",
735 entry->src_id, entry->src_data[0]);
742 static const struct amd_ip_funcs jpeg_v2_0_ip_funcs = {
744 .early_init = jpeg_v2_0_early_init,
746 .sw_init = jpeg_v2_0_sw_init,
747 .sw_fini = jpeg_v2_0_sw_fini,
748 .hw_init = jpeg_v2_0_hw_init,
749 .hw_fini = jpeg_v2_0_hw_fini,
750 .suspend = jpeg_v2_0_suspend,
751 .resume = jpeg_v2_0_resume,
752 .is_idle = jpeg_v2_0_is_idle,
753 .wait_for_idle = jpeg_v2_0_wait_for_idle,
754 .check_soft_reset = NULL,
755 .pre_soft_reset = NULL,
757 .post_soft_reset = NULL,
758 .set_clockgating_state = jpeg_v2_0_set_clockgating_state,
759 .set_powergating_state = jpeg_v2_0_set_powergating_state,
762 static const struct amdgpu_ring_funcs jpeg_v2_0_dec_ring_vm_funcs = {
763 .type = AMDGPU_RING_TYPE_VCN_JPEG,
765 .vmhub = AMDGPU_MMHUB_0,
766 .get_rptr = jpeg_v2_0_dec_ring_get_rptr,
767 .get_wptr = jpeg_v2_0_dec_ring_get_wptr,
768 .set_wptr = jpeg_v2_0_dec_ring_set_wptr,
770 SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
771 SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
772 8 + /* jpeg_v2_0_dec_ring_emit_vm_flush */
773 18 + 18 + /* jpeg_v2_0_dec_ring_emit_fence x2 vm fence */
775 .emit_ib_size = 24, /* jpeg_v2_0_dec_ring_emit_ib */
776 .emit_ib = jpeg_v2_0_dec_ring_emit_ib,
777 .emit_fence = jpeg_v2_0_dec_ring_emit_fence,
778 .emit_vm_flush = jpeg_v2_0_dec_ring_emit_vm_flush,
779 .test_ring = amdgpu_jpeg_dec_ring_test_ring,
780 .test_ib = amdgpu_jpeg_dec_ring_test_ib,
781 .insert_nop = jpeg_v2_0_dec_ring_nop,
782 .insert_start = jpeg_v2_0_dec_ring_insert_start,
783 .insert_end = jpeg_v2_0_dec_ring_insert_end,
784 .pad_ib = amdgpu_ring_generic_pad_ib,
785 .begin_use = amdgpu_jpeg_ring_begin_use,
786 .end_use = amdgpu_jpeg_ring_end_use,
787 .emit_wreg = jpeg_v2_0_dec_ring_emit_wreg,
788 .emit_reg_wait = jpeg_v2_0_dec_ring_emit_reg_wait,
789 .emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper,
792 static void jpeg_v2_0_set_dec_ring_funcs(struct amdgpu_device *adev)
794 adev->jpeg.inst->ring_dec.funcs = &jpeg_v2_0_dec_ring_vm_funcs;
795 DRM_INFO("JPEG decode is enabled in VM mode\n");
798 static const struct amdgpu_irq_src_funcs jpeg_v2_0_irq_funcs = {
799 .set = jpeg_v2_0_set_interrupt_state,
800 .process = jpeg_v2_0_process_interrupt,
803 static void jpeg_v2_0_set_irq_funcs(struct amdgpu_device *adev)
805 adev->jpeg.inst->irq.num_types = 1;
806 adev->jpeg.inst->irq.funcs = &jpeg_v2_0_irq_funcs;
809 const struct amdgpu_ip_block_version jpeg_v2_0_ip_block =
811 .type = AMD_IP_BLOCK_TYPE_JPEG,
815 .funcs = &jpeg_v2_0_ip_funcs,