1 /*
2 * Copyright 2019 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 "amdgpu.h"
25 #include "amdgpu_jpeg.h"
26 #include "amdgpu_pm.h"
27 #include "soc15.h"
28 #include "soc15d.h"
29 #include "jpeg_v2_0.h"
30
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"
34
35 #define mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET 0x1bfff
36 #define mmUVD_JPEG_GPCOM_CMD_INTERNAL_OFFSET 0x4029
37 #define mmUVD_JPEG_GPCOM_DATA0_INTERNAL_OFFSET 0x402a
38 #define mmUVD_JPEG_GPCOM_DATA1_INTERNAL_OFFSET 0x402b
39 #define mmUVD_LMI_JRBC_RB_MEM_WR_64BIT_BAR_LOW_INTERNAL_OFFSET 0x40ea
40 #define mmUVD_LMI_JRBC_RB_MEM_WR_64BIT_BAR_HIGH_INTERNAL_OFFSET 0x40eb
41 #define mmUVD_LMI_JRBC_IB_VMID_INTERNAL_OFFSET 0x40cf
42 #define mmUVD_LMI_JPEG_VMID_INTERNAL_OFFSET 0x40d1
43 #define mmUVD_LMI_JRBC_IB_64BIT_BAR_LOW_INTERNAL_OFFSET 0x40e8
44 #define mmUVD_LMI_JRBC_IB_64BIT_BAR_HIGH_INTERNAL_OFFSET 0x40e9
45 #define mmUVD_JRBC_IB_SIZE_INTERNAL_OFFSET 0x4082
46 #define mmUVD_LMI_JRBC_RB_MEM_RD_64BIT_BAR_LOW_INTERNAL_OFFSET 0x40ec
47 #define mmUVD_LMI_JRBC_RB_MEM_RD_64BIT_BAR_HIGH_INTERNAL_OFFSET 0x40ed
48 #define mmUVD_JRBC_RB_COND_RD_TIMER_INTERNAL_OFFSET 0x4085
49 #define mmUVD_JRBC_RB_REF_DATA_INTERNAL_OFFSET 0x4084
50 #define mmUVD_JRBC_STATUS_INTERNAL_OFFSET 0x4089
51 #define mmUVD_JPEG_PITCH_INTERNAL_OFFSET 0x401f
52
53 #define JRBC_DEC_EXTERNAL_REG_WRITE_ADDR 0x18000
54
55 static void jpeg_v2_0_set_dec_ring_funcs(struct amdgpu_device *adev);
56 static void jpeg_v2_0_set_irq_funcs(struct amdgpu_device *adev);
57 static int jpeg_v2_0_set_powergating_state(void *handle,
58 enum amd_powergating_state state);
59
60 /**
61 * jpeg_v2_0_early_init - set function pointers
62 *
63 * @handle: amdgpu_device pointer
64 *
65 * Set ring and irq function pointers
66 */
jpeg_v2_0_early_init(void * handle)67 static int jpeg_v2_0_early_init(void *handle)
68 {
69 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
70
71 adev->jpeg.num_jpeg_inst = 1;
72
73 jpeg_v2_0_set_dec_ring_funcs(adev);
74 jpeg_v2_0_set_irq_funcs(adev);
75
76 return 0;
77 }
78
79 /**
80 * jpeg_v2_0_sw_init - sw init for JPEG block
81 *
82 * @handle: amdgpu_device pointer
83 *
84 * Load firmware and sw initialization
85 */
jpeg_v2_0_sw_init(void * handle)86 static int jpeg_v2_0_sw_init(void *handle)
87 {
88 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
89 struct amdgpu_ring *ring;
90 int r;
91
92 /* JPEG TRAP */
93 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VCN,
94 VCN_2_0__SRCID__JPEG_DECODE, &adev->jpeg.inst->irq);
95 if (r)
96 return r;
97
98 r = amdgpu_jpeg_sw_init(adev);
99 if (r)
100 return r;
101
102 r = amdgpu_jpeg_resume(adev);
103 if (r)
104 return r;
105
106 ring = &adev->jpeg.inst->ring_dec;
107 ring->use_doorbell = true;
108 ring->doorbell_index = (adev->doorbell_index.vcn.vcn_ring0_1 << 1) + 1;
109 sprintf(ring->name, "jpeg_dec");
110 r = amdgpu_ring_init(adev, ring, 512, &adev->jpeg.inst->irq,
111 0, AMDGPU_RING_PRIO_DEFAULT);
112 if (r)
113 return r;
114
115 adev->jpeg.internal.jpeg_pitch = mmUVD_JPEG_PITCH_INTERNAL_OFFSET;
116 adev->jpeg.inst->external.jpeg_pitch = SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_PITCH);
117
118 return 0;
119 }
120
121 /**
122 * jpeg_v2_0_sw_fini - sw fini for JPEG block
123 *
124 * @handle: amdgpu_device pointer
125 *
126 * JPEG suspend and free up sw allocation
127 */
jpeg_v2_0_sw_fini(void * handle)128 static int jpeg_v2_0_sw_fini(void *handle)
129 {
130 int r;
131 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
132
133 r = amdgpu_jpeg_suspend(adev);
134 if (r)
135 return r;
136
137 r = amdgpu_jpeg_sw_fini(adev);
138
139 return r;
140 }
141
142 /**
143 * jpeg_v2_0_hw_init - start and test JPEG block
144 *
145 * @handle: amdgpu_device pointer
146 *
147 */
jpeg_v2_0_hw_init(void * handle)148 static int jpeg_v2_0_hw_init(void *handle)
149 {
150 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
151 struct amdgpu_ring *ring = &adev->jpeg.inst->ring_dec;
152 int r;
153
154 adev->nbio.funcs->vcn_doorbell_range(adev, ring->use_doorbell,
155 (adev->doorbell_index.vcn.vcn_ring0_1 << 1), 0);
156
157 r = amdgpu_ring_test_helper(ring);
158 if (!r)
159 DRM_INFO("JPEG decode initialized successfully.\n");
160
161 return r;
162 }
163
164 /**
165 * jpeg_v2_0_hw_fini - stop the hardware block
166 *
167 * @handle: amdgpu_device pointer
168 *
169 * Stop the JPEG block, mark ring as not ready any more
170 */
jpeg_v2_0_hw_fini(void * handle)171 static int jpeg_v2_0_hw_fini(void *handle)
172 {
173 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
174
175 cancel_delayed_work_sync(&adev->vcn.idle_work);
176
177 if (adev->jpeg.cur_state != AMD_PG_STATE_GATE &&
178 RREG32_SOC15(JPEG, 0, mmUVD_JRBC_STATUS))
179 jpeg_v2_0_set_powergating_state(adev, AMD_PG_STATE_GATE);
180
181 return 0;
182 }
183
184 /**
185 * jpeg_v2_0_suspend - suspend JPEG block
186 *
187 * @handle: amdgpu_device pointer
188 *
189 * HW fini and suspend JPEG block
190 */
jpeg_v2_0_suspend(void * handle)191 static int jpeg_v2_0_suspend(void *handle)
192 {
193 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
194 int r;
195
196 r = jpeg_v2_0_hw_fini(adev);
197 if (r)
198 return r;
199
200 r = amdgpu_jpeg_suspend(adev);
201
202 return r;
203 }
204
205 /**
206 * jpeg_v2_0_resume - resume JPEG block
207 *
208 * @handle: amdgpu_device pointer
209 *
210 * Resume firmware and hw init JPEG block
211 */
jpeg_v2_0_resume(void * handle)212 static int jpeg_v2_0_resume(void *handle)
213 {
214 int r;
215 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
216
217 r = amdgpu_jpeg_resume(adev);
218 if (r)
219 return r;
220
221 r = jpeg_v2_0_hw_init(adev);
222
223 return r;
224 }
225
jpeg_v2_0_disable_power_gating(struct amdgpu_device * adev)226 static int jpeg_v2_0_disable_power_gating(struct amdgpu_device *adev)
227 {
228 uint32_t data;
229 int r = 0;
230
231 if (adev->pg_flags & AMD_PG_SUPPORT_JPEG) {
232 data = 1 << UVD_PGFSM_CONFIG__UVDJ_PWR_CONFIG__SHIFT;
233 WREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_PGFSM_CONFIG), data);
234
235 r = SOC15_WAIT_ON_RREG(JPEG, 0,
236 mmUVD_PGFSM_STATUS, UVD_PGFSM_STATUS_UVDJ_PWR_ON,
237 UVD_PGFSM_STATUS__UVDJ_PWR_STATUS_MASK);
238
239 if (r) {
240 DRM_ERROR("amdgpu: JPEG disable power gating failed\n");
241 return r;
242 }
243 }
244
245 /* Removing the anti hang mechanism to indicate the UVDJ tile is ON */
246 data = RREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_POWER_STATUS)) & ~0x1;
247 WREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_POWER_STATUS), data);
248
249 return 0;
250 }
251
jpeg_v2_0_enable_power_gating(struct amdgpu_device * adev)252 static int jpeg_v2_0_enable_power_gating(struct amdgpu_device* adev)
253 {
254 if (adev->pg_flags & AMD_PG_SUPPORT_JPEG) {
255 uint32_t data;
256 int r = 0;
257
258 data = RREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_POWER_STATUS));
259 data &= ~UVD_JPEG_POWER_STATUS__JPEG_POWER_STATUS_MASK;
260 data |= 0x1; //UVD_JPEG_POWER_STATUS__JPEG_POWER_STATUS_TILES_OFF;
261 WREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JPEG_POWER_STATUS), data);
262
263 data = 2 << UVD_PGFSM_CONFIG__UVDJ_PWR_CONFIG__SHIFT;
264 WREG32(SOC15_REG_OFFSET(JPEG, 0, mmUVD_PGFSM_CONFIG), data);
265
266 r = SOC15_WAIT_ON_RREG(JPEG, 0, mmUVD_PGFSM_STATUS,
267 (2 << UVD_PGFSM_STATUS__UVDJ_PWR_STATUS__SHIFT),
268 UVD_PGFSM_STATUS__UVDJ_PWR_STATUS_MASK);
269
270 if (r) {
271 DRM_ERROR("amdgpu: JPEG enable power gating failed\n");
272 return r;
273 }
274 }
275
276 return 0;
277 }
278
jpeg_v2_0_disable_clock_gating(struct amdgpu_device * adev)279 static void jpeg_v2_0_disable_clock_gating(struct amdgpu_device* adev)
280 {
281 uint32_t data;
282
283 data = RREG32_SOC15(JPEG, 0, mmJPEG_CGC_CTRL);
284 if (adev->cg_flags & AMD_CG_SUPPORT_JPEG_MGCG)
285 data |= 1 << JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
286 else
287 data &= ~JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
288
289 data |= 1 << JPEG_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
290 data |= 4 << JPEG_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
291 WREG32_SOC15(JPEG, 0, mmJPEG_CGC_CTRL, data);
292
293 data = RREG32_SOC15(JPEG, 0, mmJPEG_CGC_GATE);
294 data &= ~(JPEG_CGC_GATE__JPEG_DEC_MASK
295 | JPEG_CGC_GATE__JPEG2_DEC_MASK
296 | JPEG_CGC_GATE__JPEG_ENC_MASK
297 | JPEG_CGC_GATE__JMCIF_MASK
298 | JPEG_CGC_GATE__JRBBM_MASK);
299 WREG32_SOC15(JPEG, 0, mmJPEG_CGC_GATE, data);
300 }
301
jpeg_v2_0_enable_clock_gating(struct amdgpu_device * adev)302 static void jpeg_v2_0_enable_clock_gating(struct amdgpu_device* adev)
303 {
304 uint32_t data;
305
306 data = RREG32_SOC15(JPEG, 0, mmJPEG_CGC_CTRL);
307 if (adev->cg_flags & AMD_CG_SUPPORT_JPEG_MGCG)
308 data |= 1 << JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
309 else
310 data |= 0 << JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
311
312 data |= 1 << JPEG_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
313 data |= 4 << JPEG_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
314 WREG32_SOC15(JPEG, 0, mmJPEG_CGC_CTRL, data);
315
316 data = RREG32_SOC15(JPEG, 0, mmJPEG_CGC_GATE);
317 data |= (JPEG_CGC_GATE__JPEG_DEC_MASK
318 |JPEG_CGC_GATE__JPEG2_DEC_MASK
319 |JPEG_CGC_GATE__JPEG_ENC_MASK
320 |JPEG_CGC_GATE__JMCIF_MASK
321 |JPEG_CGC_GATE__JRBBM_MASK);
322 WREG32_SOC15(JPEG, 0, mmJPEG_CGC_GATE, data);
323 }
324
325 /**
326 * jpeg_v2_0_start - start JPEG block
327 *
328 * @adev: amdgpu_device pointer
329 *
330 * Setup and start the JPEG block
331 */
jpeg_v2_0_start(struct amdgpu_device * adev)332 static int jpeg_v2_0_start(struct amdgpu_device *adev)
333 {
334 struct amdgpu_ring *ring = &adev->jpeg.inst->ring_dec;
335 int r;
336
337 if (adev->pm.dpm_enabled)
338 amdgpu_dpm_enable_jpeg(adev, true);
339
340 /* disable power gating */
341 r = jpeg_v2_0_disable_power_gating(adev);
342 if (r)
343 return r;
344
345 /* JPEG disable CGC */
346 jpeg_v2_0_disable_clock_gating(adev);
347
348 WREG32_SOC15(JPEG, 0, mmJPEG_DEC_GFX10_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
349
350 /* enable JMI channel */
351 WREG32_P(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JMI_CNTL), 0,
352 ~UVD_JMI_CNTL__SOFT_RESET_MASK);
353
354 /* enable System Interrupt for JRBC */
355 WREG32_P(SOC15_REG_OFFSET(JPEG, 0, mmJPEG_SYS_INT_EN),
356 JPEG_SYS_INT_EN__DJRBC_MASK,
357 ~JPEG_SYS_INT_EN__DJRBC_MASK);
358
359 WREG32_SOC15(JPEG, 0, mmUVD_LMI_JRBC_RB_VMID, 0);
360 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_CNTL, (0x00000001L | 0x00000002L));
361 WREG32_SOC15(JPEG, 0, mmUVD_LMI_JRBC_RB_64BIT_BAR_LOW,
362 lower_32_bits(ring->gpu_addr));
363 WREG32_SOC15(JPEG, 0, mmUVD_LMI_JRBC_RB_64BIT_BAR_HIGH,
364 upper_32_bits(ring->gpu_addr));
365 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_RPTR, 0);
366 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_WPTR, 0);
367 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_CNTL, 0x00000002L);
368 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_SIZE, ring->ring_size / 4);
369 ring->wptr = RREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_WPTR);
370
371 return 0;
372 }
373
374 /**
375 * jpeg_v2_0_stop - stop JPEG block
376 *
377 * @adev: amdgpu_device pointer
378 *
379 * stop the JPEG block
380 */
jpeg_v2_0_stop(struct amdgpu_device * adev)381 static int jpeg_v2_0_stop(struct amdgpu_device *adev)
382 {
383 int r;
384
385 /* reset JMI */
386 WREG32_P(SOC15_REG_OFFSET(JPEG, 0, mmUVD_JMI_CNTL),
387 UVD_JMI_CNTL__SOFT_RESET_MASK,
388 ~UVD_JMI_CNTL__SOFT_RESET_MASK);
389
390 /* enable JPEG CGC */
391 jpeg_v2_0_enable_clock_gating(adev);
392
393 /* enable power gating */
394 r = jpeg_v2_0_enable_power_gating(adev);
395 if (r)
396 return r;
397
398 if (adev->pm.dpm_enabled)
399 amdgpu_dpm_enable_jpeg(adev, false);
400
401 return 0;
402 }
403
404 /**
405 * jpeg_v2_0_dec_ring_get_rptr - get read pointer
406 *
407 * @ring: amdgpu_ring pointer
408 *
409 * Returns the current hardware read pointer
410 */
jpeg_v2_0_dec_ring_get_rptr(struct amdgpu_ring * ring)411 static uint64_t jpeg_v2_0_dec_ring_get_rptr(struct amdgpu_ring *ring)
412 {
413 struct amdgpu_device *adev = ring->adev;
414
415 return RREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_RPTR);
416 }
417
418 /**
419 * jpeg_v2_0_dec_ring_get_wptr - get write pointer
420 *
421 * @ring: amdgpu_ring pointer
422 *
423 * Returns the current hardware write pointer
424 */
jpeg_v2_0_dec_ring_get_wptr(struct amdgpu_ring * ring)425 static uint64_t jpeg_v2_0_dec_ring_get_wptr(struct amdgpu_ring *ring)
426 {
427 struct amdgpu_device *adev = ring->adev;
428
429 if (ring->use_doorbell)
430 return adev->wb.wb[ring->wptr_offs];
431 else
432 return RREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_WPTR);
433 }
434
435 /**
436 * jpeg_v2_0_dec_ring_set_wptr - set write pointer
437 *
438 * @ring: amdgpu_ring pointer
439 *
440 * Commits the write pointer to the hardware
441 */
jpeg_v2_0_dec_ring_set_wptr(struct amdgpu_ring * ring)442 static void jpeg_v2_0_dec_ring_set_wptr(struct amdgpu_ring *ring)
443 {
444 struct amdgpu_device *adev = ring->adev;
445
446 if (ring->use_doorbell) {
447 adev->wb.wb[ring->wptr_offs] = lower_32_bits(ring->wptr);
448 WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr));
449 } else {
450 WREG32_SOC15(JPEG, 0, mmUVD_JRBC_RB_WPTR, lower_32_bits(ring->wptr));
451 }
452 }
453
454 /**
455 * jpeg_v2_0_dec_ring_insert_start - insert a start command
456 *
457 * @ring: amdgpu_ring pointer
458 *
459 * Write a start command to the ring.
460 */
jpeg_v2_0_dec_ring_insert_start(struct amdgpu_ring * ring)461 void jpeg_v2_0_dec_ring_insert_start(struct amdgpu_ring *ring)
462 {
463 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
464 0, 0, PACKETJ_TYPE0));
465 amdgpu_ring_write(ring, 0x68e04);
466
467 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
468 0, 0, PACKETJ_TYPE0));
469 amdgpu_ring_write(ring, 0x80010000);
470 }
471
472 /**
473 * jpeg_v2_0_dec_ring_insert_end - insert a end command
474 *
475 * @ring: amdgpu_ring pointer
476 *
477 * Write a end command to the ring.
478 */
jpeg_v2_0_dec_ring_insert_end(struct amdgpu_ring * ring)479 void jpeg_v2_0_dec_ring_insert_end(struct amdgpu_ring *ring)
480 {
481 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
482 0, 0, PACKETJ_TYPE0));
483 amdgpu_ring_write(ring, 0x68e04);
484
485 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
486 0, 0, PACKETJ_TYPE0));
487 amdgpu_ring_write(ring, 0x00010000);
488 }
489
490 /**
491 * jpeg_v2_0_dec_ring_emit_fence - emit an fence & trap command
492 *
493 * @ring: amdgpu_ring pointer
494 * @fence: fence to emit
495 *
496 * Write a fence and a trap command to the ring.
497 */
jpeg_v2_0_dec_ring_emit_fence(struct amdgpu_ring * ring,u64 addr,u64 seq,unsigned flags)498 void jpeg_v2_0_dec_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
499 unsigned flags)
500 {
501 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
502
503 amdgpu_ring_write(ring, PACKETJ(mmUVD_JPEG_GPCOM_DATA0_INTERNAL_OFFSET,
504 0, 0, PACKETJ_TYPE0));
505 amdgpu_ring_write(ring, seq);
506
507 amdgpu_ring_write(ring, PACKETJ(mmUVD_JPEG_GPCOM_DATA1_INTERNAL_OFFSET,
508 0, 0, PACKETJ_TYPE0));
509 amdgpu_ring_write(ring, seq);
510
511 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_RB_MEM_WR_64BIT_BAR_LOW_INTERNAL_OFFSET,
512 0, 0, PACKETJ_TYPE0));
513 amdgpu_ring_write(ring, lower_32_bits(addr));
514
515 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_RB_MEM_WR_64BIT_BAR_HIGH_INTERNAL_OFFSET,
516 0, 0, PACKETJ_TYPE0));
517 amdgpu_ring_write(ring, upper_32_bits(addr));
518
519 amdgpu_ring_write(ring, PACKETJ(mmUVD_JPEG_GPCOM_CMD_INTERNAL_OFFSET,
520 0, 0, PACKETJ_TYPE0));
521 amdgpu_ring_write(ring, 0x8);
522
523 amdgpu_ring_write(ring, PACKETJ(mmUVD_JPEG_GPCOM_CMD_INTERNAL_OFFSET,
524 0, PACKETJ_CONDITION_CHECK0, PACKETJ_TYPE4));
525 amdgpu_ring_write(ring, 0);
526
527 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
528 0, 0, PACKETJ_TYPE0));
529 amdgpu_ring_write(ring, 0x3fbc);
530
531 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
532 0, 0, PACKETJ_TYPE0));
533 amdgpu_ring_write(ring, 0x1);
534
535 amdgpu_ring_write(ring, PACKETJ(0, 0, 0, PACKETJ_TYPE7));
536 amdgpu_ring_write(ring, 0);
537 }
538
539 /**
540 * jpeg_v2_0_dec_ring_emit_ib - execute indirect buffer
541 *
542 * @ring: amdgpu_ring pointer
543 * @ib: indirect buffer to execute
544 *
545 * Write ring commands to execute the indirect buffer.
546 */
jpeg_v2_0_dec_ring_emit_ib(struct amdgpu_ring * ring,struct amdgpu_job * job,struct amdgpu_ib * ib,uint32_t flags)547 void jpeg_v2_0_dec_ring_emit_ib(struct amdgpu_ring *ring,
548 struct amdgpu_job *job,
549 struct amdgpu_ib *ib,
550 uint32_t flags)
551 {
552 unsigned vmid = AMDGPU_JOB_GET_VMID(job);
553
554 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_IB_VMID_INTERNAL_OFFSET,
555 0, 0, PACKETJ_TYPE0));
556 amdgpu_ring_write(ring, (vmid | (vmid << 4)));
557
558 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JPEG_VMID_INTERNAL_OFFSET,
559 0, 0, PACKETJ_TYPE0));
560 amdgpu_ring_write(ring, (vmid | (vmid << 4)));
561
562 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_IB_64BIT_BAR_LOW_INTERNAL_OFFSET,
563 0, 0, PACKETJ_TYPE0));
564 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
565
566 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_IB_64BIT_BAR_HIGH_INTERNAL_OFFSET,
567 0, 0, PACKETJ_TYPE0));
568 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
569
570 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_IB_SIZE_INTERNAL_OFFSET,
571 0, 0, PACKETJ_TYPE0));
572 amdgpu_ring_write(ring, ib->length_dw);
573
574 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_RB_MEM_RD_64BIT_BAR_LOW_INTERNAL_OFFSET,
575 0, 0, PACKETJ_TYPE0));
576 amdgpu_ring_write(ring, lower_32_bits(ring->gpu_addr));
577
578 amdgpu_ring_write(ring, PACKETJ(mmUVD_LMI_JRBC_RB_MEM_RD_64BIT_BAR_HIGH_INTERNAL_OFFSET,
579 0, 0, PACKETJ_TYPE0));
580 amdgpu_ring_write(ring, upper_32_bits(ring->gpu_addr));
581
582 amdgpu_ring_write(ring, PACKETJ(0, 0, PACKETJ_CONDITION_CHECK0, PACKETJ_TYPE2));
583 amdgpu_ring_write(ring, 0);
584
585 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_RB_COND_RD_TIMER_INTERNAL_OFFSET,
586 0, 0, PACKETJ_TYPE0));
587 amdgpu_ring_write(ring, 0x01400200);
588
589 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_RB_REF_DATA_INTERNAL_OFFSET,
590 0, 0, PACKETJ_TYPE0));
591 amdgpu_ring_write(ring, 0x2);
592
593 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_STATUS_INTERNAL_OFFSET,
594 0, PACKETJ_CONDITION_CHECK3, PACKETJ_TYPE3));
595 amdgpu_ring_write(ring, 0x2);
596 }
597
jpeg_v2_0_dec_ring_emit_reg_wait(struct amdgpu_ring * ring,uint32_t reg,uint32_t val,uint32_t mask)598 void jpeg_v2_0_dec_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
599 uint32_t val, uint32_t mask)
600 {
601 uint32_t reg_offset = (reg << 2);
602
603 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_RB_COND_RD_TIMER_INTERNAL_OFFSET,
604 0, 0, PACKETJ_TYPE0));
605 amdgpu_ring_write(ring, 0x01400200);
606
607 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_RB_REF_DATA_INTERNAL_OFFSET,
608 0, 0, PACKETJ_TYPE0));
609 amdgpu_ring_write(ring, val);
610
611 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
612 0, 0, PACKETJ_TYPE0));
613 if (reg_offset >= 0x10000 && reg_offset <= 0x105ff) {
614 amdgpu_ring_write(ring, 0);
615 amdgpu_ring_write(ring,
616 PACKETJ((reg_offset >> 2), 0, 0, PACKETJ_TYPE3));
617 } else {
618 amdgpu_ring_write(ring, reg_offset);
619 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
620 0, 0, PACKETJ_TYPE3));
621 }
622 amdgpu_ring_write(ring, mask);
623 }
624
jpeg_v2_0_dec_ring_emit_vm_flush(struct amdgpu_ring * ring,unsigned vmid,uint64_t pd_addr)625 void jpeg_v2_0_dec_ring_emit_vm_flush(struct amdgpu_ring *ring,
626 unsigned vmid, uint64_t pd_addr)
627 {
628 struct amdgpu_vmhub *hub = &ring->adev->vmhub[ring->funcs->vmhub];
629 uint32_t data0, data1, mask;
630
631 pd_addr = amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
632
633 /* wait for register write */
634 data0 = hub->ctx0_ptb_addr_lo32 + vmid * hub->ctx_addr_distance;
635 data1 = lower_32_bits(pd_addr);
636 mask = 0xffffffff;
637 jpeg_v2_0_dec_ring_emit_reg_wait(ring, data0, data1, mask);
638 }
639
jpeg_v2_0_dec_ring_emit_wreg(struct amdgpu_ring * ring,uint32_t reg,uint32_t val)640 void jpeg_v2_0_dec_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg, uint32_t val)
641 {
642 uint32_t reg_offset = (reg << 2);
643
644 amdgpu_ring_write(ring, PACKETJ(mmUVD_JRBC_EXTERNAL_REG_INTERNAL_OFFSET,
645 0, 0, PACKETJ_TYPE0));
646 if (reg_offset >= 0x10000 && reg_offset <= 0x105ff) {
647 amdgpu_ring_write(ring, 0);
648 amdgpu_ring_write(ring,
649 PACKETJ((reg_offset >> 2), 0, 0, PACKETJ_TYPE0));
650 } else {
651 amdgpu_ring_write(ring, reg_offset);
652 amdgpu_ring_write(ring, PACKETJ(JRBC_DEC_EXTERNAL_REG_WRITE_ADDR,
653 0, 0, PACKETJ_TYPE0));
654 }
655 amdgpu_ring_write(ring, val);
656 }
657
jpeg_v2_0_dec_ring_nop(struct amdgpu_ring * ring,uint32_t count)658 void jpeg_v2_0_dec_ring_nop(struct amdgpu_ring *ring, uint32_t count)
659 {
660 int i;
661
662 WARN_ON(ring->wptr % 2 || count % 2);
663
664 for (i = 0; i < count / 2; i++) {
665 amdgpu_ring_write(ring, PACKETJ(0, 0, 0, PACKETJ_TYPE6));
666 amdgpu_ring_write(ring, 0);
667 }
668 }
669
jpeg_v2_0_is_idle(void * handle)670 static bool jpeg_v2_0_is_idle(void *handle)
671 {
672 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
673
674 return ((RREG32_SOC15(JPEG, 0, mmUVD_JRBC_STATUS) &
675 UVD_JRBC_STATUS__RB_JOB_DONE_MASK) ==
676 UVD_JRBC_STATUS__RB_JOB_DONE_MASK);
677 }
678
jpeg_v2_0_wait_for_idle(void * handle)679 static int jpeg_v2_0_wait_for_idle(void *handle)
680 {
681 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
682 int ret;
683
684 ret = SOC15_WAIT_ON_RREG(JPEG, 0, mmUVD_JRBC_STATUS, UVD_JRBC_STATUS__RB_JOB_DONE_MASK,
685 UVD_JRBC_STATUS__RB_JOB_DONE_MASK);
686
687 return ret;
688 }
689
jpeg_v2_0_set_clockgating_state(void * handle,enum amd_clockgating_state state)690 static int jpeg_v2_0_set_clockgating_state(void *handle,
691 enum amd_clockgating_state state)
692 {
693 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
694 bool enable = (state == AMD_CG_STATE_GATE);
695
696 if (enable) {
697 if (!jpeg_v2_0_is_idle(handle))
698 return -EBUSY;
699 jpeg_v2_0_enable_clock_gating(adev);
700 } else {
701 jpeg_v2_0_disable_clock_gating(adev);
702 }
703
704 return 0;
705 }
706
jpeg_v2_0_set_powergating_state(void * handle,enum amd_powergating_state state)707 static int jpeg_v2_0_set_powergating_state(void *handle,
708 enum amd_powergating_state state)
709 {
710 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
711 int ret;
712
713 if (state == adev->jpeg.cur_state)
714 return 0;
715
716 if (state == AMD_PG_STATE_GATE)
717 ret = jpeg_v2_0_stop(adev);
718 else
719 ret = jpeg_v2_0_start(adev);
720
721 if (!ret)
722 adev->jpeg.cur_state = state;
723
724 return ret;
725 }
726
jpeg_v2_0_set_interrupt_state(struct amdgpu_device * adev,struct amdgpu_irq_src * source,unsigned type,enum amdgpu_interrupt_state state)727 static int jpeg_v2_0_set_interrupt_state(struct amdgpu_device *adev,
728 struct amdgpu_irq_src *source,
729 unsigned type,
730 enum amdgpu_interrupt_state state)
731 {
732 return 0;
733 }
734
jpeg_v2_0_process_interrupt(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)735 static int jpeg_v2_0_process_interrupt(struct amdgpu_device *adev,
736 struct amdgpu_irq_src *source,
737 struct amdgpu_iv_entry *entry)
738 {
739 DRM_DEBUG("IH: JPEG TRAP\n");
740
741 switch (entry->src_id) {
742 case VCN_2_0__SRCID__JPEG_DECODE:
743 amdgpu_fence_process(&adev->jpeg.inst->ring_dec);
744 break;
745 default:
746 DRM_ERROR("Unhandled interrupt: %d %d\n",
747 entry->src_id, entry->src_data[0]);
748 break;
749 }
750
751 return 0;
752 }
753
754 static const struct amd_ip_funcs jpeg_v2_0_ip_funcs = {
755 .name = "jpeg_v2_0",
756 .early_init = jpeg_v2_0_early_init,
757 .late_init = NULL,
758 .sw_init = jpeg_v2_0_sw_init,
759 .sw_fini = jpeg_v2_0_sw_fini,
760 .hw_init = jpeg_v2_0_hw_init,
761 .hw_fini = jpeg_v2_0_hw_fini,
762 .suspend = jpeg_v2_0_suspend,
763 .resume = jpeg_v2_0_resume,
764 .is_idle = jpeg_v2_0_is_idle,
765 .wait_for_idle = jpeg_v2_0_wait_for_idle,
766 .check_soft_reset = NULL,
767 .pre_soft_reset = NULL,
768 .soft_reset = NULL,
769 .post_soft_reset = NULL,
770 .set_clockgating_state = jpeg_v2_0_set_clockgating_state,
771 .set_powergating_state = jpeg_v2_0_set_powergating_state,
772 };
773
774 static const struct amdgpu_ring_funcs jpeg_v2_0_dec_ring_vm_funcs = {
775 .type = AMDGPU_RING_TYPE_VCN_JPEG,
776 .align_mask = 0xf,
777 .vmhub = AMDGPU_MMHUB_0,
778 .get_rptr = jpeg_v2_0_dec_ring_get_rptr,
779 .get_wptr = jpeg_v2_0_dec_ring_get_wptr,
780 .set_wptr = jpeg_v2_0_dec_ring_set_wptr,
781 .emit_frame_size =
782 SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
783 SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
784 8 + /* jpeg_v2_0_dec_ring_emit_vm_flush */
785 18 + 18 + /* jpeg_v2_0_dec_ring_emit_fence x2 vm fence */
786 8 + 16,
787 .emit_ib_size = 22, /* jpeg_v2_0_dec_ring_emit_ib */
788 .emit_ib = jpeg_v2_0_dec_ring_emit_ib,
789 .emit_fence = jpeg_v2_0_dec_ring_emit_fence,
790 .emit_vm_flush = jpeg_v2_0_dec_ring_emit_vm_flush,
791 .test_ring = amdgpu_jpeg_dec_ring_test_ring,
792 .test_ib = amdgpu_jpeg_dec_ring_test_ib,
793 .insert_nop = jpeg_v2_0_dec_ring_nop,
794 .insert_start = jpeg_v2_0_dec_ring_insert_start,
795 .insert_end = jpeg_v2_0_dec_ring_insert_end,
796 .pad_ib = amdgpu_ring_generic_pad_ib,
797 .begin_use = amdgpu_jpeg_ring_begin_use,
798 .end_use = amdgpu_jpeg_ring_end_use,
799 .emit_wreg = jpeg_v2_0_dec_ring_emit_wreg,
800 .emit_reg_wait = jpeg_v2_0_dec_ring_emit_reg_wait,
801 .emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper,
802 };
803
jpeg_v2_0_set_dec_ring_funcs(struct amdgpu_device * adev)804 static void jpeg_v2_0_set_dec_ring_funcs(struct amdgpu_device *adev)
805 {
806 adev->jpeg.inst->ring_dec.funcs = &jpeg_v2_0_dec_ring_vm_funcs;
807 DRM_INFO("JPEG decode is enabled in VM mode\n");
808 }
809
810 static const struct amdgpu_irq_src_funcs jpeg_v2_0_irq_funcs = {
811 .set = jpeg_v2_0_set_interrupt_state,
812 .process = jpeg_v2_0_process_interrupt,
813 };
814
jpeg_v2_0_set_irq_funcs(struct amdgpu_device * adev)815 static void jpeg_v2_0_set_irq_funcs(struct amdgpu_device *adev)
816 {
817 adev->jpeg.inst->irq.num_types = 1;
818 adev->jpeg.inst->irq.funcs = &jpeg_v2_0_irq_funcs;
819 }
820
821 const struct amdgpu_ip_block_version jpeg_v2_0_ip_block =
822 {
823 .type = AMD_IP_BLOCK_TYPE_JPEG,
824 .major = 2,
825 .minor = 0,
826 .rev = 0,
827 .funcs = &jpeg_v2_0_ip_funcs,
828 };
829