Linux Audio

Check our new training course

Loading...
v6.13.7
  1/*
  2 * Copyright 2014 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 * Authors: Christian König <christian.koenig@amd.com>
 23 */
 24
 25#include <linux/delay.h>
 26#include <linux/firmware.h>
 27
 28#include "amdgpu.h"
 29#include "amdgpu_uvd.h"
 30#include "vid.h"
 31#include "uvd/uvd_5_0_d.h"
 32#include "uvd/uvd_5_0_sh_mask.h"
 33#include "oss/oss_2_0_d.h"
 34#include "oss/oss_2_0_sh_mask.h"
 35#include "bif/bif_5_0_d.h"
 36#include "vi.h"
 37#include "smu/smu_7_1_2_d.h"
 38#include "smu/smu_7_1_2_sh_mask.h"
 39#include "ivsrcid/ivsrcid_vislands30.h"
 40
 41static void uvd_v5_0_set_ring_funcs(struct amdgpu_device *adev);
 42static void uvd_v5_0_set_irq_funcs(struct amdgpu_device *adev);
 43static int uvd_v5_0_start(struct amdgpu_device *adev);
 44static void uvd_v5_0_stop(struct amdgpu_device *adev);
 45static int uvd_v5_0_set_clockgating_state(void *handle,
 46					  enum amd_clockgating_state state);
 47static void uvd_v5_0_enable_mgcg(struct amdgpu_device *adev,
 48				 bool enable);
 49/**
 50 * uvd_v5_0_ring_get_rptr - get read pointer
 51 *
 52 * @ring: amdgpu_ring pointer
 53 *
 54 * Returns the current hardware read pointer
 55 */
 56static uint64_t uvd_v5_0_ring_get_rptr(struct amdgpu_ring *ring)
 57{
 58	struct amdgpu_device *adev = ring->adev;
 59
 60	return RREG32(mmUVD_RBC_RB_RPTR);
 61}
 62
 63/**
 64 * uvd_v5_0_ring_get_wptr - get write pointer
 65 *
 66 * @ring: amdgpu_ring pointer
 67 *
 68 * Returns the current hardware write pointer
 69 */
 70static uint64_t uvd_v5_0_ring_get_wptr(struct amdgpu_ring *ring)
 71{
 72	struct amdgpu_device *adev = ring->adev;
 73
 74	return RREG32(mmUVD_RBC_RB_WPTR);
 75}
 76
 77/**
 78 * uvd_v5_0_ring_set_wptr - set write pointer
 79 *
 80 * @ring: amdgpu_ring pointer
 81 *
 82 * Commits the write pointer to the hardware
 83 */
 84static void uvd_v5_0_ring_set_wptr(struct amdgpu_ring *ring)
 85{
 86	struct amdgpu_device *adev = ring->adev;
 87
 88	WREG32(mmUVD_RBC_RB_WPTR, lower_32_bits(ring->wptr));
 89}
 90
 91static int uvd_v5_0_early_init(struct amdgpu_ip_block *ip_block)
 92{
 93	struct amdgpu_device *adev = ip_block->adev;
 94	adev->uvd.num_uvd_inst = 1;
 95
 96	uvd_v5_0_set_ring_funcs(adev);
 97	uvd_v5_0_set_irq_funcs(adev);
 98
 99	return 0;
100}
101
102static int uvd_v5_0_sw_init(struct amdgpu_ip_block *ip_block)
103{
104	struct amdgpu_ring *ring;
105	struct amdgpu_device *adev = ip_block->adev;
106	int r;
107
108	/* UVD TRAP */
109	r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_UVD_SYSTEM_MESSAGE, &adev->uvd.inst->irq);
110	if (r)
111		return r;
112
113	r = amdgpu_uvd_sw_init(adev);
114	if (r)
115		return r;
116
117	ring = &adev->uvd.inst->ring;
118	sprintf(ring->name, "uvd");
119	r = amdgpu_ring_init(adev, ring, 512, &adev->uvd.inst->irq, 0,
120			     AMDGPU_RING_PRIO_DEFAULT, NULL);
121	if (r)
122		return r;
123
124	r = amdgpu_uvd_resume(adev);
125	if (r)
126		return r;
127
 
 
 
 
128	return r;
129}
130
131static int uvd_v5_0_sw_fini(struct amdgpu_ip_block *ip_block)
132{
133	int r;
134	struct amdgpu_device *adev = ip_block->adev;
135
136	r = amdgpu_uvd_suspend(adev);
137	if (r)
138		return r;
139
140	return amdgpu_uvd_sw_fini(adev);
 
 
 
 
141}
142
143/**
144 * uvd_v5_0_hw_init - start and test UVD block
145 *
146 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
147 *
148 * Initialize the hardware, boot up the VCPU and do some testing
149 */
150static int uvd_v5_0_hw_init(struct amdgpu_ip_block *ip_block)
151{
152	struct amdgpu_device *adev = ip_block->adev;
153	struct amdgpu_ring *ring = &adev->uvd.inst->ring;
154	uint32_t tmp;
155	int r;
156
157	amdgpu_asic_set_uvd_clocks(adev, 10000, 10000);
158	uvd_v5_0_set_clockgating_state(adev, AMD_CG_STATE_UNGATE);
159	uvd_v5_0_enable_mgcg(adev, true);
160
161	r = amdgpu_ring_test_helper(ring);
162	if (r)
163		goto done;
164
 
 
 
 
 
 
 
165	r = amdgpu_ring_alloc(ring, 10);
166	if (r) {
167		DRM_ERROR("amdgpu: ring failed to lock UVD ring (%d).\n", r);
168		goto done;
169	}
170
171	tmp = PACKET0(mmUVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL, 0);
172	amdgpu_ring_write(ring, tmp);
173	amdgpu_ring_write(ring, 0xFFFFF);
174
175	tmp = PACKET0(mmUVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL, 0);
176	amdgpu_ring_write(ring, tmp);
177	amdgpu_ring_write(ring, 0xFFFFF);
178
179	tmp = PACKET0(mmUVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL, 0);
180	amdgpu_ring_write(ring, tmp);
181	amdgpu_ring_write(ring, 0xFFFFF);
182
183	/* Clear timeout status bits */
184	amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_TIMEOUT_STATUS, 0));
185	amdgpu_ring_write(ring, 0x8);
186
187	amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_CNTL, 0));
188	amdgpu_ring_write(ring, 3);
189
190	amdgpu_ring_commit(ring);
191
192done:
193	if (!r)
194		DRM_INFO("UVD initialized successfully.\n");
195
196	return r;
197
198}
199
200/**
201 * uvd_v5_0_hw_fini - stop the hardware block
202 *
203 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
204 *
205 * Stop the UVD block, mark ring as not ready any more
206 */
207static int uvd_v5_0_hw_fini(struct amdgpu_ip_block *ip_block)
208{
209	struct amdgpu_device *adev = ip_block->adev;
210
211	cancel_delayed_work_sync(&adev->uvd.idle_work);
212
213	if (RREG32(mmUVD_STATUS) != 0)
214		uvd_v5_0_stop(adev);
215
216	return 0;
217}
218
219static int uvd_v5_0_prepare_suspend(struct amdgpu_ip_block *ip_block)
220{
221	struct amdgpu_device *adev = ip_block->adev;
222
223	return amdgpu_uvd_prepare_suspend(adev);
224}
225
226static int uvd_v5_0_suspend(struct amdgpu_ip_block *ip_block)
227{
228	int r;
229	struct amdgpu_device *adev = ip_block->adev;
230
231	/*
232	 * Proper cleanups before halting the HW engine:
233	 *   - cancel the delayed idle work
234	 *   - enable powergating
235	 *   - enable clockgating
236	 *   - disable dpm
237	 *
238	 * TODO: to align with the VCN implementation, move the
239	 * jobs for clockgating/powergating/dpm setting to
240	 * ->set_powergating_state().
241	 */
242	cancel_delayed_work_sync(&adev->uvd.idle_work);
243
244	if (adev->pm.dpm_enabled) {
245		amdgpu_dpm_enable_uvd(adev, false);
246	} else {
247		amdgpu_asic_set_uvd_clocks(adev, 0, 0);
248		/* shutdown the UVD block */
249		amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
250						       AMD_PG_STATE_GATE);
251		amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
252						       AMD_CG_STATE_GATE);
253	}
254
255	r = uvd_v5_0_hw_fini(ip_block);
256	if (r)
257		return r;
258
259	return amdgpu_uvd_suspend(adev);
260}
261
262static int uvd_v5_0_resume(struct amdgpu_ip_block *ip_block)
263{
264	int r;
 
 
 
 
 
265
266	r = amdgpu_uvd_resume(ip_block->adev);
267	if (r)
268		return r;
269
270	return uvd_v5_0_hw_init(ip_block);
271}
272
273/**
274 * uvd_v5_0_mc_resume - memory controller programming
275 *
276 * @adev: amdgpu_device pointer
277 *
278 * Let the UVD memory controller know it's offsets
279 */
280static void uvd_v5_0_mc_resume(struct amdgpu_device *adev)
281{
282	uint64_t offset;
283	uint32_t size;
284
285	/* program memory controller bits 0-27 */
286	WREG32(mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW,
287			lower_32_bits(adev->uvd.inst->gpu_addr));
288	WREG32(mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH,
289			upper_32_bits(adev->uvd.inst->gpu_addr));
290
291	offset = AMDGPU_UVD_FIRMWARE_OFFSET;
292	size = AMDGPU_UVD_FIRMWARE_SIZE(adev);
293	WREG32(mmUVD_VCPU_CACHE_OFFSET0, offset >> 3);
294	WREG32(mmUVD_VCPU_CACHE_SIZE0, size);
295
296	offset += size;
297	size = AMDGPU_UVD_HEAP_SIZE;
298	WREG32(mmUVD_VCPU_CACHE_OFFSET1, offset >> 3);
299	WREG32(mmUVD_VCPU_CACHE_SIZE1, size);
300
301	offset += size;
302	size = AMDGPU_UVD_STACK_SIZE +
303	       (AMDGPU_UVD_SESSION_SIZE * adev->uvd.max_handles);
304	WREG32(mmUVD_VCPU_CACHE_OFFSET2, offset >> 3);
305	WREG32(mmUVD_VCPU_CACHE_SIZE2, size);
306
307	WREG32(mmUVD_UDEC_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
308	WREG32(mmUVD_UDEC_DB_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
309	WREG32(mmUVD_UDEC_DBW_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
310}
311
312/**
313 * uvd_v5_0_start - start UVD block
314 *
315 * @adev: amdgpu_device pointer
316 *
317 * Setup and start the UVD block
318 */
319static int uvd_v5_0_start(struct amdgpu_device *adev)
320{
321	struct amdgpu_ring *ring = &adev->uvd.inst->ring;
322	uint32_t rb_bufsz, tmp;
323	uint32_t lmi_swap_cntl;
324	uint32_t mp_swap_cntl;
325	int i, j, r;
326
327	/*disable DPG */
328	WREG32_P(mmUVD_POWER_STATUS, 0, ~(1 << 2));
329
330	/* disable byte swapping */
331	lmi_swap_cntl = 0;
332	mp_swap_cntl = 0;
333
334	uvd_v5_0_mc_resume(adev);
335
 
 
 
 
336	/* disable interupt */
337	WREG32_P(mmUVD_MASTINT_EN, 0, ~(1 << 1));
338
339	/* stall UMC and register bus before resetting VCPU */
340	WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
341	mdelay(1);
342
343	/* put LMI, VCPU, RBC etc... into reset */
344	WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__LMI_SOFT_RESET_MASK |
345		UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK | UVD_SOFT_RESET__LBSI_SOFT_RESET_MASK |
346		UVD_SOFT_RESET__RBC_SOFT_RESET_MASK | UVD_SOFT_RESET__CSM_SOFT_RESET_MASK |
347		UVD_SOFT_RESET__CXW_SOFT_RESET_MASK | UVD_SOFT_RESET__TAP_SOFT_RESET_MASK |
348		UVD_SOFT_RESET__LMI_UMC_SOFT_RESET_MASK);
349	mdelay(5);
350
351	/* take UVD block out of reset */
352	WREG32_P(mmSRBM_SOFT_RESET, 0, ~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
353	mdelay(5);
354
355	/* initialize UVD memory controller */
356	WREG32(mmUVD_LMI_CTRL, 0x40 | (1 << 8) | (1 << 13) |
357			     (1 << 21) | (1 << 9) | (1 << 20));
358
359#ifdef __BIG_ENDIAN
360	/* swap (8 in 32) RB and IB */
361	lmi_swap_cntl = 0xa;
362	mp_swap_cntl = 0;
363#endif
364	WREG32(mmUVD_LMI_SWAP_CNTL, lmi_swap_cntl);
365	WREG32(mmUVD_MP_SWAP_CNTL, mp_swap_cntl);
366
367	WREG32(mmUVD_MPC_SET_MUXA0, 0x40c2040);
368	WREG32(mmUVD_MPC_SET_MUXA1, 0x0);
369	WREG32(mmUVD_MPC_SET_MUXB0, 0x40c2040);
370	WREG32(mmUVD_MPC_SET_MUXB1, 0x0);
371	WREG32(mmUVD_MPC_SET_ALU, 0);
372	WREG32(mmUVD_MPC_SET_MUX, 0x88);
373
374	/* take all subblocks out of reset, except VCPU */
375	WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
376	mdelay(5);
377
378	/* enable VCPU clock */
379	WREG32(mmUVD_VCPU_CNTL,  1 << 9);
380
381	/* enable UMC */
382	WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
383
384	/* boot up the VCPU */
385	WREG32(mmUVD_SOFT_RESET, 0);
386	mdelay(10);
387
388	for (i = 0; i < 10; ++i) {
389		uint32_t status;
390		for (j = 0; j < 100; ++j) {
391			status = RREG32(mmUVD_STATUS);
392			if (status & 2)
393				break;
394			mdelay(10);
395		}
396		r = 0;
397		if (status & 2)
398			break;
399
400		DRM_ERROR("UVD not responding, trying to reset the VCPU!!!\n");
401		WREG32_P(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK,
402				~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
403		mdelay(10);
404		WREG32_P(mmUVD_SOFT_RESET, 0, ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
405		mdelay(10);
406		r = -1;
407	}
408
409	if (r) {
410		DRM_ERROR("UVD not responding, giving up!!!\n");
411		return r;
412	}
413	/* enable master interrupt */
414	WREG32_P(mmUVD_MASTINT_EN, 3 << 1, ~(3 << 1));
415
416	/* clear the bit 4 of UVD_STATUS */
417	WREG32_P(mmUVD_STATUS, 0, ~(2 << 1));
418
419	rb_bufsz = order_base_2(ring->ring_size);
420	tmp = 0;
421	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
422	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
423	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
424	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_WPTR_POLL_EN, 0);
425	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
426	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
427	/* force RBC into idle state */
428	WREG32(mmUVD_RBC_RB_CNTL, tmp);
429
430	/* set the write pointer delay */
431	WREG32(mmUVD_RBC_RB_WPTR_CNTL, 0);
432
433	/* set the wb address */
434	WREG32(mmUVD_RBC_RB_RPTR_ADDR, (upper_32_bits(ring->gpu_addr) >> 2));
435
436	/* program the RB_BASE for ring buffer */
437	WREG32(mmUVD_LMI_RBC_RB_64BIT_BAR_LOW,
438			lower_32_bits(ring->gpu_addr));
439	WREG32(mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH,
440			upper_32_bits(ring->gpu_addr));
441
442	/* Initialize the ring buffer's read and write pointers */
443	WREG32(mmUVD_RBC_RB_RPTR, 0);
444
445	ring->wptr = RREG32(mmUVD_RBC_RB_RPTR);
446	WREG32(mmUVD_RBC_RB_WPTR, lower_32_bits(ring->wptr));
447
448	WREG32_P(mmUVD_RBC_RB_CNTL, 0, ~UVD_RBC_RB_CNTL__RB_NO_FETCH_MASK);
449
450	return 0;
451}
452
453/**
454 * uvd_v5_0_stop - stop UVD block
455 *
456 * @adev: amdgpu_device pointer
457 *
458 * stop the UVD block
459 */
460static void uvd_v5_0_stop(struct amdgpu_device *adev)
461{
462	/* force RBC into idle state */
463	WREG32(mmUVD_RBC_RB_CNTL, 0x11010101);
464
465	/* Stall UMC and register bus before resetting VCPU */
466	WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
467	mdelay(1);
468
469	/* put VCPU into reset */
470	WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
471	mdelay(5);
472
473	/* disable VCPU clock */
474	WREG32(mmUVD_VCPU_CNTL, 0x0);
475
476	/* Unstall UMC and register bus */
477	WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
478
479	WREG32(mmUVD_STATUS, 0);
480}
481
482/**
483 * uvd_v5_0_ring_emit_fence - emit an fence & trap command
484 *
485 * @ring: amdgpu_ring pointer
486 * @addr: address
487 * @seq: sequence number
488 * @flags: fence related flags
489 *
490 * Write a fence and a trap command to the ring.
491 */
492static void uvd_v5_0_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
493				     unsigned flags)
494{
495	WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
496
497	amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
498	amdgpu_ring_write(ring, seq);
499	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
500	amdgpu_ring_write(ring, addr & 0xffffffff);
501	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
502	amdgpu_ring_write(ring, upper_32_bits(addr) & 0xff);
503	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
504	amdgpu_ring_write(ring, 0);
505
506	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
507	amdgpu_ring_write(ring, 0);
508	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
509	amdgpu_ring_write(ring, 0);
510	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
511	amdgpu_ring_write(ring, 2);
512}
513
514/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
515 * uvd_v5_0_ring_test_ring - register write test
516 *
517 * @ring: amdgpu_ring pointer
518 *
519 * Test if we can successfully write to the context register
520 */
521static int uvd_v5_0_ring_test_ring(struct amdgpu_ring *ring)
522{
523	struct amdgpu_device *adev = ring->adev;
524	uint32_t tmp = 0;
525	unsigned i;
526	int r;
527
528	WREG32(mmUVD_CONTEXT_ID, 0xCAFEDEAD);
529	r = amdgpu_ring_alloc(ring, 3);
530	if (r)
 
 
531		return r;
 
532	amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
533	amdgpu_ring_write(ring, 0xDEADBEEF);
534	amdgpu_ring_commit(ring);
535	for (i = 0; i < adev->usec_timeout; i++) {
536		tmp = RREG32(mmUVD_CONTEXT_ID);
537		if (tmp == 0xDEADBEEF)
538			break;
539		udelay(1);
540	}
541
542	if (i >= adev->usec_timeout)
543		r = -ETIMEDOUT;
544
 
 
 
 
 
545	return r;
546}
547
548/**
549 * uvd_v5_0_ring_emit_ib - execute indirect buffer
550 *
551 * @ring: amdgpu_ring pointer
552 * @job: job to retrieve vmid from
553 * @ib: indirect buffer to execute
554 * @flags: unused
555 *
556 * Write ring commands to execute the indirect buffer
557 */
558static void uvd_v5_0_ring_emit_ib(struct amdgpu_ring *ring,
559				  struct amdgpu_job *job,
560				  struct amdgpu_ib *ib,
561				  uint32_t flags)
562{
563	amdgpu_ring_write(ring, PACKET0(mmUVD_LMI_RBC_IB_64BIT_BAR_LOW, 0));
564	amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
565	amdgpu_ring_write(ring, PACKET0(mmUVD_LMI_RBC_IB_64BIT_BAR_HIGH, 0));
566	amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
567	amdgpu_ring_write(ring, PACKET0(mmUVD_RBC_IB_SIZE, 0));
568	amdgpu_ring_write(ring, ib->length_dw);
569}
570
571static void uvd_v5_0_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
572{
573	int i;
574
575	WARN_ON(ring->wptr % 2 || count % 2);
576
577	for (i = 0; i < count / 2; i++) {
578		amdgpu_ring_write(ring, PACKET0(mmUVD_NO_OP, 0));
579		amdgpu_ring_write(ring, 0);
580	}
581}
582
583static bool uvd_v5_0_is_idle(void *handle)
584{
585	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
586
587	return !(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK);
588}
589
590static int uvd_v5_0_wait_for_idle(struct amdgpu_ip_block *ip_block)
591{
592	unsigned i;
593	struct amdgpu_device *adev = ip_block->adev;
594
595	for (i = 0; i < adev->usec_timeout; i++) {
596		if (!(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK))
597			return 0;
598	}
599	return -ETIMEDOUT;
600}
601
602static int uvd_v5_0_soft_reset(struct amdgpu_ip_block *ip_block)
603{
604	struct amdgpu_device *adev = ip_block->adev;
605
606	uvd_v5_0_stop(adev);
607
608	WREG32_P(mmSRBM_SOFT_RESET, SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK,
609			~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
610	mdelay(5);
611
612	return uvd_v5_0_start(adev);
613}
614
615static int uvd_v5_0_set_interrupt_state(struct amdgpu_device *adev,
616					struct amdgpu_irq_src *source,
617					unsigned type,
618					enum amdgpu_interrupt_state state)
619{
620	// TODO
621	return 0;
622}
623
624static int uvd_v5_0_process_interrupt(struct amdgpu_device *adev,
625				      struct amdgpu_irq_src *source,
626				      struct amdgpu_iv_entry *entry)
627{
628	DRM_DEBUG("IH: UVD TRAP\n");
629	amdgpu_fence_process(&adev->uvd.inst->ring);
630	return 0;
631}
632
633static void uvd_v5_0_enable_clock_gating(struct amdgpu_device *adev, bool enable)
634{
635	uint32_t data1, data3, suvd_flags;
636
637	data1 = RREG32(mmUVD_SUVD_CGC_GATE);
638	data3 = RREG32(mmUVD_CGC_GATE);
639
640	suvd_flags = UVD_SUVD_CGC_GATE__SRE_MASK |
641		     UVD_SUVD_CGC_GATE__SIT_MASK |
642		     UVD_SUVD_CGC_GATE__SMP_MASK |
643		     UVD_SUVD_CGC_GATE__SCM_MASK |
644		     UVD_SUVD_CGC_GATE__SDB_MASK;
645
646	if (enable) {
647		data3 |= (UVD_CGC_GATE__SYS_MASK     |
648			UVD_CGC_GATE__UDEC_MASK      |
649			UVD_CGC_GATE__MPEG2_MASK     |
650			UVD_CGC_GATE__RBC_MASK       |
651			UVD_CGC_GATE__LMI_MC_MASK    |
652			UVD_CGC_GATE__IDCT_MASK      |
653			UVD_CGC_GATE__MPRD_MASK      |
654			UVD_CGC_GATE__MPC_MASK       |
655			UVD_CGC_GATE__LBSI_MASK      |
656			UVD_CGC_GATE__LRBBM_MASK     |
657			UVD_CGC_GATE__UDEC_RE_MASK   |
658			UVD_CGC_GATE__UDEC_CM_MASK   |
659			UVD_CGC_GATE__UDEC_IT_MASK   |
660			UVD_CGC_GATE__UDEC_DB_MASK   |
661			UVD_CGC_GATE__UDEC_MP_MASK   |
662			UVD_CGC_GATE__WCB_MASK       |
663			UVD_CGC_GATE__JPEG_MASK      |
664			UVD_CGC_GATE__SCPU_MASK);
665		/* only in pg enabled, we can gate clock to vcpu*/
666		if (adev->pg_flags & AMD_PG_SUPPORT_UVD)
667			data3 |= UVD_CGC_GATE__VCPU_MASK;
668		data3 &= ~UVD_CGC_GATE__REGS_MASK;
669		data1 |= suvd_flags;
670	} else {
671		data3 = 0;
672		data1 = 0;
673	}
674
675	WREG32(mmUVD_SUVD_CGC_GATE, data1);
676	WREG32(mmUVD_CGC_GATE, data3);
677}
678
679static void uvd_v5_0_set_sw_clock_gating(struct amdgpu_device *adev)
680{
681	uint32_t data, data2;
682
683	data = RREG32(mmUVD_CGC_CTRL);
684	data2 = RREG32(mmUVD_SUVD_CGC_CTRL);
685
686
687	data &= ~(UVD_CGC_CTRL__CLK_OFF_DELAY_MASK |
688		  UVD_CGC_CTRL__CLK_GATE_DLY_TIMER_MASK);
689
690
691	data |= UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK |
692		(1 << REG_FIELD_SHIFT(UVD_CGC_CTRL, CLK_GATE_DLY_TIMER)) |
693		(4 << REG_FIELD_SHIFT(UVD_CGC_CTRL, CLK_OFF_DELAY));
694
695	data &= ~(UVD_CGC_CTRL__UDEC_RE_MODE_MASK |
696			UVD_CGC_CTRL__UDEC_CM_MODE_MASK |
697			UVD_CGC_CTRL__UDEC_IT_MODE_MASK |
698			UVD_CGC_CTRL__UDEC_DB_MODE_MASK |
699			UVD_CGC_CTRL__UDEC_MP_MODE_MASK |
700			UVD_CGC_CTRL__SYS_MODE_MASK |
701			UVD_CGC_CTRL__UDEC_MODE_MASK |
702			UVD_CGC_CTRL__MPEG2_MODE_MASK |
703			UVD_CGC_CTRL__REGS_MODE_MASK |
704			UVD_CGC_CTRL__RBC_MODE_MASK |
705			UVD_CGC_CTRL__LMI_MC_MODE_MASK |
706			UVD_CGC_CTRL__LMI_UMC_MODE_MASK |
707			UVD_CGC_CTRL__IDCT_MODE_MASK |
708			UVD_CGC_CTRL__MPRD_MODE_MASK |
709			UVD_CGC_CTRL__MPC_MODE_MASK |
710			UVD_CGC_CTRL__LBSI_MODE_MASK |
711			UVD_CGC_CTRL__LRBBM_MODE_MASK |
712			UVD_CGC_CTRL__WCB_MODE_MASK |
713			UVD_CGC_CTRL__VCPU_MODE_MASK |
714			UVD_CGC_CTRL__JPEG_MODE_MASK |
715			UVD_CGC_CTRL__SCPU_MODE_MASK);
716	data2 &= ~(UVD_SUVD_CGC_CTRL__SRE_MODE_MASK |
717			UVD_SUVD_CGC_CTRL__SIT_MODE_MASK |
718			UVD_SUVD_CGC_CTRL__SMP_MODE_MASK |
719			UVD_SUVD_CGC_CTRL__SCM_MODE_MASK |
720			UVD_SUVD_CGC_CTRL__SDB_MODE_MASK);
721
722	WREG32(mmUVD_CGC_CTRL, data);
723	WREG32(mmUVD_SUVD_CGC_CTRL, data2);
724}
725
726#if 0
727static void uvd_v5_0_set_hw_clock_gating(struct amdgpu_device *adev)
728{
729	uint32_t data, data1, cgc_flags, suvd_flags;
730
731	data = RREG32(mmUVD_CGC_GATE);
732	data1 = RREG32(mmUVD_SUVD_CGC_GATE);
733
734	cgc_flags = UVD_CGC_GATE__SYS_MASK |
735				UVD_CGC_GATE__UDEC_MASK |
736				UVD_CGC_GATE__MPEG2_MASK |
737				UVD_CGC_GATE__RBC_MASK |
738				UVD_CGC_GATE__LMI_MC_MASK |
739				UVD_CGC_GATE__IDCT_MASK |
740				UVD_CGC_GATE__MPRD_MASK |
741				UVD_CGC_GATE__MPC_MASK |
742				UVD_CGC_GATE__LBSI_MASK |
743				UVD_CGC_GATE__LRBBM_MASK |
744				UVD_CGC_GATE__UDEC_RE_MASK |
745				UVD_CGC_GATE__UDEC_CM_MASK |
746				UVD_CGC_GATE__UDEC_IT_MASK |
747				UVD_CGC_GATE__UDEC_DB_MASK |
748				UVD_CGC_GATE__UDEC_MP_MASK |
749				UVD_CGC_GATE__WCB_MASK |
750				UVD_CGC_GATE__VCPU_MASK |
751				UVD_CGC_GATE__SCPU_MASK;
752
753	suvd_flags = UVD_SUVD_CGC_GATE__SRE_MASK |
754				UVD_SUVD_CGC_GATE__SIT_MASK |
755				UVD_SUVD_CGC_GATE__SMP_MASK |
756				UVD_SUVD_CGC_GATE__SCM_MASK |
757				UVD_SUVD_CGC_GATE__SDB_MASK;
758
759	data |= cgc_flags;
760	data1 |= suvd_flags;
761
762	WREG32(mmUVD_CGC_GATE, data);
763	WREG32(mmUVD_SUVD_CGC_GATE, data1);
764}
765#endif
766
767static void uvd_v5_0_enable_mgcg(struct amdgpu_device *adev,
768				 bool enable)
769{
770	u32 orig, data;
771
772	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_UVD_MGCG)) {
773		data = RREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL);
774		data |= 0xfff;
775		WREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL, data);
776
777		orig = data = RREG32(mmUVD_CGC_CTRL);
778		data |= UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
779		if (orig != data)
780			WREG32(mmUVD_CGC_CTRL, data);
781	} else {
782		data = RREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL);
783		data &= ~0xfff;
784		WREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL, data);
785
786		orig = data = RREG32(mmUVD_CGC_CTRL);
787		data &= ~UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
788		if (orig != data)
789			WREG32(mmUVD_CGC_CTRL, data);
790	}
791}
792
793static int uvd_v5_0_set_clockgating_state(void *handle,
794					  enum amd_clockgating_state state)
795{
796	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
797	bool enable = (state == AMD_CG_STATE_GATE);
798	struct amdgpu_ip_block *ip_block;
799
800	ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD);
801	if (!ip_block)
802		return -EINVAL;
803
804	if (enable) {
805		/* wait for STATUS to clear */
806		if (uvd_v5_0_wait_for_idle(ip_block))
807			return -EBUSY;
808		uvd_v5_0_enable_clock_gating(adev, true);
809
810		/* enable HW gates because UVD is idle */
811/*		uvd_v5_0_set_hw_clock_gating(adev); */
812	} else {
813		uvd_v5_0_enable_clock_gating(adev, false);
814	}
815
816	uvd_v5_0_set_sw_clock_gating(adev);
817	return 0;
818}
819
820static int uvd_v5_0_set_powergating_state(void *handle,
821					  enum amd_powergating_state state)
822{
823	/* This doesn't actually powergate the UVD block.
824	 * That's done in the dpm code via the SMC.  This
825	 * just re-inits the block as necessary.  The actual
826	 * gating still happens in the dpm code.  We should
827	 * revisit this when there is a cleaner line between
828	 * the smc and the hw blocks
829	 */
830	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
831	int ret = 0;
 
 
832
833	if (state == AMD_PG_STATE_GATE) {
834		uvd_v5_0_stop(adev);
 
835	} else {
836		ret = uvd_v5_0_start(adev);
837		if (ret)
838			goto out;
839	}
840
841out:
842	return ret;
843}
844
845static void uvd_v5_0_get_clockgating_state(void *handle, u64 *flags)
846{
847	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
848	int data;
849
850	mutex_lock(&adev->pm.mutex);
851
852	if (RREG32_SMC(ixCURRENT_PG_STATUS) &
853				CURRENT_PG_STATUS__UVD_PG_STATUS_MASK) {
854		DRM_INFO("Cannot get clockgating state when UVD is powergated.\n");
855		goto out;
856	}
857
858	/* AMD_CG_SUPPORT_UVD_MGCG */
859	data = RREG32(mmUVD_CGC_CTRL);
860	if (data & UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK)
861		*flags |= AMD_CG_SUPPORT_UVD_MGCG;
862
863out:
864	mutex_unlock(&adev->pm.mutex);
865}
866
867static const struct amd_ip_funcs uvd_v5_0_ip_funcs = {
868	.name = "uvd_v5_0",
869	.early_init = uvd_v5_0_early_init,
 
870	.sw_init = uvd_v5_0_sw_init,
871	.sw_fini = uvd_v5_0_sw_fini,
872	.hw_init = uvd_v5_0_hw_init,
873	.hw_fini = uvd_v5_0_hw_fini,
874	.prepare_suspend = uvd_v5_0_prepare_suspend,
875	.suspend = uvd_v5_0_suspend,
876	.resume = uvd_v5_0_resume,
877	.is_idle = uvd_v5_0_is_idle,
878	.wait_for_idle = uvd_v5_0_wait_for_idle,
879	.soft_reset = uvd_v5_0_soft_reset,
880	.set_clockgating_state = uvd_v5_0_set_clockgating_state,
881	.set_powergating_state = uvd_v5_0_set_powergating_state,
882	.get_clockgating_state = uvd_v5_0_get_clockgating_state,
883};
884
885static const struct amdgpu_ring_funcs uvd_v5_0_ring_funcs = {
886	.type = AMDGPU_RING_TYPE_UVD,
887	.align_mask = 0xf,
888	.support_64bit_ptrs = false,
889	.no_user_fence = true,
890	.get_rptr = uvd_v5_0_ring_get_rptr,
891	.get_wptr = uvd_v5_0_ring_get_wptr,
892	.set_wptr = uvd_v5_0_ring_set_wptr,
893	.parse_cs = amdgpu_uvd_ring_parse_cs,
894	.emit_frame_size =
 
 
895		14, /* uvd_v5_0_ring_emit_fence  x1 no user fence */
896	.emit_ib_size = 6, /* uvd_v5_0_ring_emit_ib */
897	.emit_ib = uvd_v5_0_ring_emit_ib,
898	.emit_fence = uvd_v5_0_ring_emit_fence,
 
 
899	.test_ring = uvd_v5_0_ring_test_ring,
900	.test_ib = amdgpu_uvd_ring_test_ib,
901	.insert_nop = uvd_v5_0_ring_insert_nop,
902	.pad_ib = amdgpu_ring_generic_pad_ib,
903	.begin_use = amdgpu_uvd_ring_begin_use,
904	.end_use = amdgpu_uvd_ring_end_use,
905};
906
907static void uvd_v5_0_set_ring_funcs(struct amdgpu_device *adev)
908{
909	adev->uvd.inst->ring.funcs = &uvd_v5_0_ring_funcs;
910}
911
912static const struct amdgpu_irq_src_funcs uvd_v5_0_irq_funcs = {
913	.set = uvd_v5_0_set_interrupt_state,
914	.process = uvd_v5_0_process_interrupt,
915};
916
917static void uvd_v5_0_set_irq_funcs(struct amdgpu_device *adev)
918{
919	adev->uvd.inst->irq.num_types = 1;
920	adev->uvd.inst->irq.funcs = &uvd_v5_0_irq_funcs;
921}
922
923const struct amdgpu_ip_block_version uvd_v5_0_ip_block =
924{
925		.type = AMD_IP_BLOCK_TYPE_UVD,
926		.major = 5,
927		.minor = 0,
928		.rev = 0,
929		.funcs = &uvd_v5_0_ip_funcs,
930};
v4.10.11
  1/*
  2 * Copyright 2014 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 * Authors: Christian König <christian.koenig@amd.com>
 23 */
 24
 
 25#include <linux/firmware.h>
 26#include <drm/drmP.h>
 27#include "amdgpu.h"
 28#include "amdgpu_uvd.h"
 29#include "vid.h"
 30#include "uvd/uvd_5_0_d.h"
 31#include "uvd/uvd_5_0_sh_mask.h"
 32#include "oss/oss_2_0_d.h"
 33#include "oss/oss_2_0_sh_mask.h"
 34#include "bif/bif_5_0_d.h"
 35#include "vi.h"
 36#include "smu/smu_7_1_2_d.h"
 37#include "smu/smu_7_1_2_sh_mask.h"
 
 38
 39static void uvd_v5_0_set_ring_funcs(struct amdgpu_device *adev);
 40static void uvd_v5_0_set_irq_funcs(struct amdgpu_device *adev);
 41static int uvd_v5_0_start(struct amdgpu_device *adev);
 42static void uvd_v5_0_stop(struct amdgpu_device *adev);
 43static int uvd_v5_0_set_clockgating_state(void *handle,
 44					  enum amd_clockgating_state state);
 45static void uvd_v5_0_enable_mgcg(struct amdgpu_device *adev,
 46				 bool enable);
 47/**
 48 * uvd_v5_0_ring_get_rptr - get read pointer
 49 *
 50 * @ring: amdgpu_ring pointer
 51 *
 52 * Returns the current hardware read pointer
 53 */
 54static uint32_t uvd_v5_0_ring_get_rptr(struct amdgpu_ring *ring)
 55{
 56	struct amdgpu_device *adev = ring->adev;
 57
 58	return RREG32(mmUVD_RBC_RB_RPTR);
 59}
 60
 61/**
 62 * uvd_v5_0_ring_get_wptr - get write pointer
 63 *
 64 * @ring: amdgpu_ring pointer
 65 *
 66 * Returns the current hardware write pointer
 67 */
 68static uint32_t uvd_v5_0_ring_get_wptr(struct amdgpu_ring *ring)
 69{
 70	struct amdgpu_device *adev = ring->adev;
 71
 72	return RREG32(mmUVD_RBC_RB_WPTR);
 73}
 74
 75/**
 76 * uvd_v5_0_ring_set_wptr - set write pointer
 77 *
 78 * @ring: amdgpu_ring pointer
 79 *
 80 * Commits the write pointer to the hardware
 81 */
 82static void uvd_v5_0_ring_set_wptr(struct amdgpu_ring *ring)
 83{
 84	struct amdgpu_device *adev = ring->adev;
 85
 86	WREG32(mmUVD_RBC_RB_WPTR, ring->wptr);
 87}
 88
 89static int uvd_v5_0_early_init(void *handle)
 90{
 91	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
 92
 93	uvd_v5_0_set_ring_funcs(adev);
 94	uvd_v5_0_set_irq_funcs(adev);
 95
 96	return 0;
 97}
 98
 99static int uvd_v5_0_sw_init(void *handle)
100{
101	struct amdgpu_ring *ring;
102	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
103	int r;
104
105	/* UVD TRAP */
106	r = amdgpu_irq_add_id(adev, 124, &adev->uvd.irq);
107	if (r)
108		return r;
109
110	r = amdgpu_uvd_sw_init(adev);
111	if (r)
112		return r;
113
 
 
 
 
 
 
 
114	r = amdgpu_uvd_resume(adev);
115	if (r)
116		return r;
117
118	ring = &adev->uvd.ring;
119	sprintf(ring->name, "uvd");
120	r = amdgpu_ring_init(adev, ring, 512, &adev->uvd.irq, 0);
121
122	return r;
123}
124
125static int uvd_v5_0_sw_fini(void *handle)
126{
127	int r;
128	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
129
130	r = amdgpu_uvd_suspend(adev);
131	if (r)
132		return r;
133
134	r = amdgpu_uvd_sw_fini(adev);
135	if (r)
136		return r;
137
138	return r;
139}
140
141/**
142 * uvd_v5_0_hw_init - start and test UVD block
143 *
144 * @adev: amdgpu_device pointer
145 *
146 * Initialize the hardware, boot up the VCPU and do some testing
147 */
148static int uvd_v5_0_hw_init(void *handle)
149{
150	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
151	struct amdgpu_ring *ring = &adev->uvd.ring;
152	uint32_t tmp;
153	int r;
154
155	r = uvd_v5_0_start(adev);
 
 
 
 
156	if (r)
157		goto done;
158
159	ring->ready = true;
160	r = amdgpu_ring_test_ring(ring);
161	if (r) {
162		ring->ready = false;
163		goto done;
164	}
165
166	r = amdgpu_ring_alloc(ring, 10);
167	if (r) {
168		DRM_ERROR("amdgpu: ring failed to lock UVD ring (%d).\n", r);
169		goto done;
170	}
171
172	tmp = PACKET0(mmUVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL, 0);
173	amdgpu_ring_write(ring, tmp);
174	amdgpu_ring_write(ring, 0xFFFFF);
175
176	tmp = PACKET0(mmUVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL, 0);
177	amdgpu_ring_write(ring, tmp);
178	amdgpu_ring_write(ring, 0xFFFFF);
179
180	tmp = PACKET0(mmUVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL, 0);
181	amdgpu_ring_write(ring, tmp);
182	amdgpu_ring_write(ring, 0xFFFFF);
183
184	/* Clear timeout status bits */
185	amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_TIMEOUT_STATUS, 0));
186	amdgpu_ring_write(ring, 0x8);
187
188	amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_CNTL, 0));
189	amdgpu_ring_write(ring, 3);
190
191	amdgpu_ring_commit(ring);
 
192done:
193	if (!r)
194		DRM_INFO("UVD initialized successfully.\n");
195
196	return r;
 
197}
198
199/**
200 * uvd_v5_0_hw_fini - stop the hardware block
201 *
202 * @adev: amdgpu_device pointer
203 *
204 * Stop the UVD block, mark ring as not ready any more
205 */
206static int uvd_v5_0_hw_fini(void *handle)
207{
208	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
209	struct amdgpu_ring *ring = &adev->uvd.ring;
 
210
211	uvd_v5_0_stop(adev);
212	ring->ready = false;
213
214	return 0;
215}
216
217static int uvd_v5_0_suspend(void *handle)
 
 
 
 
 
 
 
218{
219	int r;
220	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
 
 
 
 
 
 
 
 
 
 
 
 
221
222	r = uvd_v5_0_hw_fini(adev);
223	if (r)
224		return r;
225	uvd_v5_0_set_clockgating_state(adev, AMD_CG_STATE_GATE);
 
 
 
 
 
 
226
227	r = amdgpu_uvd_suspend(adev);
228	if (r)
229		return r;
230
231	return r;
232}
233
234static int uvd_v5_0_resume(void *handle)
235{
236	int r;
237	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
238
239	r = amdgpu_uvd_resume(adev);
240	if (r)
241		return r;
242
243	r = uvd_v5_0_hw_init(adev);
244	if (r)
245		return r;
246
247	return r;
248}
249
250/**
251 * uvd_v5_0_mc_resume - memory controller programming
252 *
253 * @adev: amdgpu_device pointer
254 *
255 * Let the UVD memory controller know it's offsets
256 */
257static void uvd_v5_0_mc_resume(struct amdgpu_device *adev)
258{
259	uint64_t offset;
260	uint32_t size;
261
262	/* programm memory controller bits 0-27 */
263	WREG32(mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW,
264			lower_32_bits(adev->uvd.gpu_addr));
265	WREG32(mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH,
266			upper_32_bits(adev->uvd.gpu_addr));
267
268	offset = AMDGPU_UVD_FIRMWARE_OFFSET;
269	size = AMDGPU_GPU_PAGE_ALIGN(adev->uvd.fw->size + 4);
270	WREG32(mmUVD_VCPU_CACHE_OFFSET0, offset >> 3);
271	WREG32(mmUVD_VCPU_CACHE_SIZE0, size);
272
273	offset += size;
274	size = AMDGPU_UVD_HEAP_SIZE;
275	WREG32(mmUVD_VCPU_CACHE_OFFSET1, offset >> 3);
276	WREG32(mmUVD_VCPU_CACHE_SIZE1, size);
277
278	offset += size;
279	size = AMDGPU_UVD_STACK_SIZE +
280	       (AMDGPU_UVD_SESSION_SIZE * adev->uvd.max_handles);
281	WREG32(mmUVD_VCPU_CACHE_OFFSET2, offset >> 3);
282	WREG32(mmUVD_VCPU_CACHE_SIZE2, size);
283
284	WREG32(mmUVD_UDEC_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
285	WREG32(mmUVD_UDEC_DB_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
286	WREG32(mmUVD_UDEC_DBW_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
287}
288
289/**
290 * uvd_v5_0_start - start UVD block
291 *
292 * @adev: amdgpu_device pointer
293 *
294 * Setup and start the UVD block
295 */
296static int uvd_v5_0_start(struct amdgpu_device *adev)
297{
298	struct amdgpu_ring *ring = &adev->uvd.ring;
299	uint32_t rb_bufsz, tmp;
300	uint32_t lmi_swap_cntl;
301	uint32_t mp_swap_cntl;
302	int i, j, r;
303
304	/*disable DPG */
305	WREG32_P(mmUVD_POWER_STATUS, 0, ~(1 << 2));
306
307	/* disable byte swapping */
308	lmi_swap_cntl = 0;
309	mp_swap_cntl = 0;
310
311	uvd_v5_0_mc_resume(adev);
312
313	amdgpu_asic_set_uvd_clocks(adev, 10000, 10000);
314	uvd_v5_0_set_clockgating_state(adev, AMD_CG_STATE_UNGATE);
315	uvd_v5_0_enable_mgcg(adev, true);
316
317	/* disable interupt */
318	WREG32_P(mmUVD_MASTINT_EN, 0, ~(1 << 1));
319
320	/* stall UMC and register bus before resetting VCPU */
321	WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
322	mdelay(1);
323
324	/* put LMI, VCPU, RBC etc... into reset */
325	WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__LMI_SOFT_RESET_MASK |
326		UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK | UVD_SOFT_RESET__LBSI_SOFT_RESET_MASK |
327		UVD_SOFT_RESET__RBC_SOFT_RESET_MASK | UVD_SOFT_RESET__CSM_SOFT_RESET_MASK |
328		UVD_SOFT_RESET__CXW_SOFT_RESET_MASK | UVD_SOFT_RESET__TAP_SOFT_RESET_MASK |
329		UVD_SOFT_RESET__LMI_UMC_SOFT_RESET_MASK);
330	mdelay(5);
331
332	/* take UVD block out of reset */
333	WREG32_P(mmSRBM_SOFT_RESET, 0, ~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
334	mdelay(5);
335
336	/* initialize UVD memory controller */
337	WREG32(mmUVD_LMI_CTRL, 0x40 | (1 << 8) | (1 << 13) |
338			     (1 << 21) | (1 << 9) | (1 << 20));
339
340#ifdef __BIG_ENDIAN
341	/* swap (8 in 32) RB and IB */
342	lmi_swap_cntl = 0xa;
343	mp_swap_cntl = 0;
344#endif
345	WREG32(mmUVD_LMI_SWAP_CNTL, lmi_swap_cntl);
346	WREG32(mmUVD_MP_SWAP_CNTL, mp_swap_cntl);
347
348	WREG32(mmUVD_MPC_SET_MUXA0, 0x40c2040);
349	WREG32(mmUVD_MPC_SET_MUXA1, 0x0);
350	WREG32(mmUVD_MPC_SET_MUXB0, 0x40c2040);
351	WREG32(mmUVD_MPC_SET_MUXB1, 0x0);
352	WREG32(mmUVD_MPC_SET_ALU, 0);
353	WREG32(mmUVD_MPC_SET_MUX, 0x88);
354
355	/* take all subblocks out of reset, except VCPU */
356	WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
357	mdelay(5);
358
359	/* enable VCPU clock */
360	WREG32(mmUVD_VCPU_CNTL,  1 << 9);
361
362	/* enable UMC */
363	WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
364
365	/* boot up the VCPU */
366	WREG32(mmUVD_SOFT_RESET, 0);
367	mdelay(10);
368
369	for (i = 0; i < 10; ++i) {
370		uint32_t status;
371		for (j = 0; j < 100; ++j) {
372			status = RREG32(mmUVD_STATUS);
373			if (status & 2)
374				break;
375			mdelay(10);
376		}
377		r = 0;
378		if (status & 2)
379			break;
380
381		DRM_ERROR("UVD not responding, trying to reset the VCPU!!!\n");
382		WREG32_P(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK,
383				~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
384		mdelay(10);
385		WREG32_P(mmUVD_SOFT_RESET, 0, ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
386		mdelay(10);
387		r = -1;
388	}
389
390	if (r) {
391		DRM_ERROR("UVD not responding, giving up!!!\n");
392		return r;
393	}
394	/* enable master interrupt */
395	WREG32_P(mmUVD_MASTINT_EN, 3 << 1, ~(3 << 1));
396
397	/* clear the bit 4 of UVD_STATUS */
398	WREG32_P(mmUVD_STATUS, 0, ~(2 << 1));
399
400	rb_bufsz = order_base_2(ring->ring_size);
401	tmp = 0;
402	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
403	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
404	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
405	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_WPTR_POLL_EN, 0);
406	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
407	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
408	/* force RBC into idle state */
409	WREG32(mmUVD_RBC_RB_CNTL, tmp);
410
411	/* set the write pointer delay */
412	WREG32(mmUVD_RBC_RB_WPTR_CNTL, 0);
413
414	/* set the wb address */
415	WREG32(mmUVD_RBC_RB_RPTR_ADDR, (upper_32_bits(ring->gpu_addr) >> 2));
416
417	/* programm the RB_BASE for ring buffer */
418	WREG32(mmUVD_LMI_RBC_RB_64BIT_BAR_LOW,
419			lower_32_bits(ring->gpu_addr));
420	WREG32(mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH,
421			upper_32_bits(ring->gpu_addr));
422
423	/* Initialize the ring buffer's read and write pointers */
424	WREG32(mmUVD_RBC_RB_RPTR, 0);
425
426	ring->wptr = RREG32(mmUVD_RBC_RB_RPTR);
427	WREG32(mmUVD_RBC_RB_WPTR, ring->wptr);
428
429	WREG32_P(mmUVD_RBC_RB_CNTL, 0, ~UVD_RBC_RB_CNTL__RB_NO_FETCH_MASK);
430
431	return 0;
432}
433
434/**
435 * uvd_v5_0_stop - stop UVD block
436 *
437 * @adev: amdgpu_device pointer
438 *
439 * stop the UVD block
440 */
441static void uvd_v5_0_stop(struct amdgpu_device *adev)
442{
443	/* force RBC into idle state */
444	WREG32(mmUVD_RBC_RB_CNTL, 0x11010101);
445
446	/* Stall UMC and register bus before resetting VCPU */
447	WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
448	mdelay(1);
449
450	/* put VCPU into reset */
451	WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
452	mdelay(5);
453
454	/* disable VCPU clock */
455	WREG32(mmUVD_VCPU_CNTL, 0x0);
456
457	/* Unstall UMC and register bus */
458	WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
 
 
459}
460
461/**
462 * uvd_v5_0_ring_emit_fence - emit an fence & trap command
463 *
464 * @ring: amdgpu_ring pointer
465 * @fence: fence to emit
 
 
466 *
467 * Write a fence and a trap command to the ring.
468 */
469static void uvd_v5_0_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
470				     unsigned flags)
471{
472	WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
473
474	amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
475	amdgpu_ring_write(ring, seq);
476	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
477	amdgpu_ring_write(ring, addr & 0xffffffff);
478	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
479	amdgpu_ring_write(ring, upper_32_bits(addr) & 0xff);
480	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
481	amdgpu_ring_write(ring, 0);
482
483	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
484	amdgpu_ring_write(ring, 0);
485	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
486	amdgpu_ring_write(ring, 0);
487	amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
488	amdgpu_ring_write(ring, 2);
489}
490
491/**
492 * uvd_v5_0_ring_emit_hdp_flush - emit an hdp flush
493 *
494 * @ring: amdgpu_ring pointer
495 *
496 * Emits an hdp flush.
497 */
498static void uvd_v5_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
499{
500	amdgpu_ring_write(ring, PACKET0(mmHDP_MEM_COHERENCY_FLUSH_CNTL, 0));
501	amdgpu_ring_write(ring, 0);
502}
503
504/**
505 * uvd_v5_0_ring_hdp_invalidate - emit an hdp invalidate
506 *
507 * @ring: amdgpu_ring pointer
508 *
509 * Emits an hdp invalidate.
510 */
511static void uvd_v5_0_ring_emit_hdp_invalidate(struct amdgpu_ring *ring)
512{
513	amdgpu_ring_write(ring, PACKET0(mmHDP_DEBUG0, 0));
514	amdgpu_ring_write(ring, 1);
515}
516
517/**
518 * uvd_v5_0_ring_test_ring - register write test
519 *
520 * @ring: amdgpu_ring pointer
521 *
522 * Test if we can successfully write to the context register
523 */
524static int uvd_v5_0_ring_test_ring(struct amdgpu_ring *ring)
525{
526	struct amdgpu_device *adev = ring->adev;
527	uint32_t tmp = 0;
528	unsigned i;
529	int r;
530
531	WREG32(mmUVD_CONTEXT_ID, 0xCAFEDEAD);
532	r = amdgpu_ring_alloc(ring, 3);
533	if (r) {
534		DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
535			  ring->idx, r);
536		return r;
537	}
538	amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
539	amdgpu_ring_write(ring, 0xDEADBEEF);
540	amdgpu_ring_commit(ring);
541	for (i = 0; i < adev->usec_timeout; i++) {
542		tmp = RREG32(mmUVD_CONTEXT_ID);
543		if (tmp == 0xDEADBEEF)
544			break;
545		DRM_UDELAY(1);
546	}
547
548	if (i < adev->usec_timeout) {
549		DRM_INFO("ring test on %d succeeded in %d usecs\n",
550			 ring->idx, i);
551	} else {
552		DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
553			  ring->idx, tmp);
554		r = -EINVAL;
555	}
556	return r;
557}
558
559/**
560 * uvd_v5_0_ring_emit_ib - execute indirect buffer
561 *
562 * @ring: amdgpu_ring pointer
 
563 * @ib: indirect buffer to execute
 
564 *
565 * Write ring commands to execute the indirect buffer
566 */
567static void uvd_v5_0_ring_emit_ib(struct amdgpu_ring *ring,
 
568				  struct amdgpu_ib *ib,
569				  unsigned vm_id, bool ctx_switch)
570{
571	amdgpu_ring_write(ring, PACKET0(mmUVD_LMI_RBC_IB_64BIT_BAR_LOW, 0));
572	amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
573	amdgpu_ring_write(ring, PACKET0(mmUVD_LMI_RBC_IB_64BIT_BAR_HIGH, 0));
574	amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
575	amdgpu_ring_write(ring, PACKET0(mmUVD_RBC_IB_SIZE, 0));
576	amdgpu_ring_write(ring, ib->length_dw);
577}
578
 
 
 
 
 
 
 
 
 
 
 
 
579static bool uvd_v5_0_is_idle(void *handle)
580{
581	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
582
583	return !(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK);
584}
585
586static int uvd_v5_0_wait_for_idle(void *handle)
587{
588	unsigned i;
589	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
590
591	for (i = 0; i < adev->usec_timeout; i++) {
592		if (!(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK))
593			return 0;
594	}
595	return -ETIMEDOUT;
596}
597
598static int uvd_v5_0_soft_reset(void *handle)
599{
600	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
601
602	uvd_v5_0_stop(adev);
603
604	WREG32_P(mmSRBM_SOFT_RESET, SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK,
605			~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
606	mdelay(5);
607
608	return uvd_v5_0_start(adev);
609}
610
611static int uvd_v5_0_set_interrupt_state(struct amdgpu_device *adev,
612					struct amdgpu_irq_src *source,
613					unsigned type,
614					enum amdgpu_interrupt_state state)
615{
616	// TODO
617	return 0;
618}
619
620static int uvd_v5_0_process_interrupt(struct amdgpu_device *adev,
621				      struct amdgpu_irq_src *source,
622				      struct amdgpu_iv_entry *entry)
623{
624	DRM_DEBUG("IH: UVD TRAP\n");
625	amdgpu_fence_process(&adev->uvd.ring);
626	return 0;
627}
628
629static void uvd_v5_0_enable_clock_gating(struct amdgpu_device *adev, bool enable)
630{
631	uint32_t data1, data3, suvd_flags;
632
633	data1 = RREG32(mmUVD_SUVD_CGC_GATE);
634	data3 = RREG32(mmUVD_CGC_GATE);
635
636	suvd_flags = UVD_SUVD_CGC_GATE__SRE_MASK |
637		     UVD_SUVD_CGC_GATE__SIT_MASK |
638		     UVD_SUVD_CGC_GATE__SMP_MASK |
639		     UVD_SUVD_CGC_GATE__SCM_MASK |
640		     UVD_SUVD_CGC_GATE__SDB_MASK;
641
642	if (enable) {
643		data3 |= (UVD_CGC_GATE__SYS_MASK     |
644			UVD_CGC_GATE__UDEC_MASK      |
645			UVD_CGC_GATE__MPEG2_MASK     |
646			UVD_CGC_GATE__RBC_MASK       |
647			UVD_CGC_GATE__LMI_MC_MASK    |
648			UVD_CGC_GATE__IDCT_MASK      |
649			UVD_CGC_GATE__MPRD_MASK      |
650			UVD_CGC_GATE__MPC_MASK       |
651			UVD_CGC_GATE__LBSI_MASK      |
652			UVD_CGC_GATE__LRBBM_MASK     |
653			UVD_CGC_GATE__UDEC_RE_MASK   |
654			UVD_CGC_GATE__UDEC_CM_MASK   |
655			UVD_CGC_GATE__UDEC_IT_MASK   |
656			UVD_CGC_GATE__UDEC_DB_MASK   |
657			UVD_CGC_GATE__UDEC_MP_MASK   |
658			UVD_CGC_GATE__WCB_MASK       |
659			UVD_CGC_GATE__JPEG_MASK      |
660			UVD_CGC_GATE__SCPU_MASK);
661		/* only in pg enabled, we can gate clock to vcpu*/
662		if (adev->pg_flags & AMD_PG_SUPPORT_UVD)
663			data3 |= UVD_CGC_GATE__VCPU_MASK;
664		data3 &= ~UVD_CGC_GATE__REGS_MASK;
665		data1 |= suvd_flags;
666	} else {
667		data3 = 0;
668		data1 = 0;
669	}
670
671	WREG32(mmUVD_SUVD_CGC_GATE, data1);
672	WREG32(mmUVD_CGC_GATE, data3);
673}
674
675static void uvd_v5_0_set_sw_clock_gating(struct amdgpu_device *adev)
676{
677	uint32_t data, data2;
678
679	data = RREG32(mmUVD_CGC_CTRL);
680	data2 = RREG32(mmUVD_SUVD_CGC_CTRL);
681
682
683	data &= ~(UVD_CGC_CTRL__CLK_OFF_DELAY_MASK |
684		  UVD_CGC_CTRL__CLK_GATE_DLY_TIMER_MASK);
685
686
687	data |= UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK |
688		(1 << REG_FIELD_SHIFT(UVD_CGC_CTRL, CLK_GATE_DLY_TIMER)) |
689		(4 << REG_FIELD_SHIFT(UVD_CGC_CTRL, CLK_OFF_DELAY));
690
691	data &= ~(UVD_CGC_CTRL__UDEC_RE_MODE_MASK |
692			UVD_CGC_CTRL__UDEC_CM_MODE_MASK |
693			UVD_CGC_CTRL__UDEC_IT_MODE_MASK |
694			UVD_CGC_CTRL__UDEC_DB_MODE_MASK |
695			UVD_CGC_CTRL__UDEC_MP_MODE_MASK |
696			UVD_CGC_CTRL__SYS_MODE_MASK |
697			UVD_CGC_CTRL__UDEC_MODE_MASK |
698			UVD_CGC_CTRL__MPEG2_MODE_MASK |
699			UVD_CGC_CTRL__REGS_MODE_MASK |
700			UVD_CGC_CTRL__RBC_MODE_MASK |
701			UVD_CGC_CTRL__LMI_MC_MODE_MASK |
702			UVD_CGC_CTRL__LMI_UMC_MODE_MASK |
703			UVD_CGC_CTRL__IDCT_MODE_MASK |
704			UVD_CGC_CTRL__MPRD_MODE_MASK |
705			UVD_CGC_CTRL__MPC_MODE_MASK |
706			UVD_CGC_CTRL__LBSI_MODE_MASK |
707			UVD_CGC_CTRL__LRBBM_MODE_MASK |
708			UVD_CGC_CTRL__WCB_MODE_MASK |
709			UVD_CGC_CTRL__VCPU_MODE_MASK |
710			UVD_CGC_CTRL__JPEG_MODE_MASK |
711			UVD_CGC_CTRL__SCPU_MODE_MASK);
712	data2 &= ~(UVD_SUVD_CGC_CTRL__SRE_MODE_MASK |
713			UVD_SUVD_CGC_CTRL__SIT_MODE_MASK |
714			UVD_SUVD_CGC_CTRL__SMP_MODE_MASK |
715			UVD_SUVD_CGC_CTRL__SCM_MODE_MASK |
716			UVD_SUVD_CGC_CTRL__SDB_MODE_MASK);
717
718	WREG32(mmUVD_CGC_CTRL, data);
719	WREG32(mmUVD_SUVD_CGC_CTRL, data2);
720}
721
722#if 0
723static void uvd_v5_0_set_hw_clock_gating(struct amdgpu_device *adev)
724{
725	uint32_t data, data1, cgc_flags, suvd_flags;
726
727	data = RREG32(mmUVD_CGC_GATE);
728	data1 = RREG32(mmUVD_SUVD_CGC_GATE);
729
730	cgc_flags = UVD_CGC_GATE__SYS_MASK |
731				UVD_CGC_GATE__UDEC_MASK |
732				UVD_CGC_GATE__MPEG2_MASK |
733				UVD_CGC_GATE__RBC_MASK |
734				UVD_CGC_GATE__LMI_MC_MASK |
735				UVD_CGC_GATE__IDCT_MASK |
736				UVD_CGC_GATE__MPRD_MASK |
737				UVD_CGC_GATE__MPC_MASK |
738				UVD_CGC_GATE__LBSI_MASK |
739				UVD_CGC_GATE__LRBBM_MASK |
740				UVD_CGC_GATE__UDEC_RE_MASK |
741				UVD_CGC_GATE__UDEC_CM_MASK |
742				UVD_CGC_GATE__UDEC_IT_MASK |
743				UVD_CGC_GATE__UDEC_DB_MASK |
744				UVD_CGC_GATE__UDEC_MP_MASK |
745				UVD_CGC_GATE__WCB_MASK |
746				UVD_CGC_GATE__VCPU_MASK |
747				UVD_CGC_GATE__SCPU_MASK;
748
749	suvd_flags = UVD_SUVD_CGC_GATE__SRE_MASK |
750				UVD_SUVD_CGC_GATE__SIT_MASK |
751				UVD_SUVD_CGC_GATE__SMP_MASK |
752				UVD_SUVD_CGC_GATE__SCM_MASK |
753				UVD_SUVD_CGC_GATE__SDB_MASK;
754
755	data |= cgc_flags;
756	data1 |= suvd_flags;
757
758	WREG32(mmUVD_CGC_GATE, data);
759	WREG32(mmUVD_SUVD_CGC_GATE, data1);
760}
761#endif
762
763static void uvd_v5_0_enable_mgcg(struct amdgpu_device *adev,
764				 bool enable)
765{
766	u32 orig, data;
767
768	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_UVD_MGCG)) {
769		data = RREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL);
770		data |= 0xfff;
771		WREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL, data);
772
773		orig = data = RREG32(mmUVD_CGC_CTRL);
774		data |= UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
775		if (orig != data)
776			WREG32(mmUVD_CGC_CTRL, data);
777	} else {
778		data = RREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL);
779		data &= ~0xfff;
780		WREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL, data);
781
782		orig = data = RREG32(mmUVD_CGC_CTRL);
783		data &= ~UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
784		if (orig != data)
785			WREG32(mmUVD_CGC_CTRL, data);
786	}
787}
788
789static int uvd_v5_0_set_clockgating_state(void *handle,
790					  enum amd_clockgating_state state)
791{
792	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
793	bool enable = (state == AMD_CG_STATE_GATE) ? true : false;
 
794
795	if (!(adev->cg_flags & AMD_CG_SUPPORT_UVD_MGCG))
796		return 0;
 
797
798	if (enable) {
799		/* wait for STATUS to clear */
800		if (uvd_v5_0_wait_for_idle(handle))
801			return -EBUSY;
802		uvd_v5_0_enable_clock_gating(adev, true);
803
804		/* enable HW gates because UVD is idle */
805/*		uvd_v5_0_set_hw_clock_gating(adev); */
806	} else {
807		uvd_v5_0_enable_clock_gating(adev, false);
808	}
809
810	uvd_v5_0_set_sw_clock_gating(adev);
811	return 0;
812}
813
814static int uvd_v5_0_set_powergating_state(void *handle,
815					  enum amd_powergating_state state)
816{
817	/* This doesn't actually powergate the UVD block.
818	 * That's done in the dpm code via the SMC.  This
819	 * just re-inits the block as necessary.  The actual
820	 * gating still happens in the dpm code.  We should
821	 * revisit this when there is a cleaner line between
822	 * the smc and the hw blocks
823	 */
824	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
825
826	if (!(adev->pg_flags & AMD_PG_SUPPORT_UVD))
827		return 0;
828
829	if (state == AMD_PG_STATE_GATE) {
830		uvd_v5_0_stop(adev);
831		return 0;
832	} else {
833		return uvd_v5_0_start(adev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
834	}
 
 
 
 
 
 
 
 
835}
836
837static const struct amd_ip_funcs uvd_v5_0_ip_funcs = {
838	.name = "uvd_v5_0",
839	.early_init = uvd_v5_0_early_init,
840	.late_init = NULL,
841	.sw_init = uvd_v5_0_sw_init,
842	.sw_fini = uvd_v5_0_sw_fini,
843	.hw_init = uvd_v5_0_hw_init,
844	.hw_fini = uvd_v5_0_hw_fini,
 
845	.suspend = uvd_v5_0_suspend,
846	.resume = uvd_v5_0_resume,
847	.is_idle = uvd_v5_0_is_idle,
848	.wait_for_idle = uvd_v5_0_wait_for_idle,
849	.soft_reset = uvd_v5_0_soft_reset,
850	.set_clockgating_state = uvd_v5_0_set_clockgating_state,
851	.set_powergating_state = uvd_v5_0_set_powergating_state,
 
852};
853
854static const struct amdgpu_ring_funcs uvd_v5_0_ring_funcs = {
855	.type = AMDGPU_RING_TYPE_UVD,
856	.align_mask = 0xf,
857	.nop = PACKET0(mmUVD_NO_OP, 0),
 
858	.get_rptr = uvd_v5_0_ring_get_rptr,
859	.get_wptr = uvd_v5_0_ring_get_wptr,
860	.set_wptr = uvd_v5_0_ring_set_wptr,
861	.parse_cs = amdgpu_uvd_ring_parse_cs,
862	.emit_frame_size =
863		2 + /* uvd_v5_0_ring_emit_hdp_flush */
864		2 + /* uvd_v5_0_ring_emit_hdp_invalidate */
865		14, /* uvd_v5_0_ring_emit_fence  x1 no user fence */
866	.emit_ib_size = 6, /* uvd_v5_0_ring_emit_ib */
867	.emit_ib = uvd_v5_0_ring_emit_ib,
868	.emit_fence = uvd_v5_0_ring_emit_fence,
869	.emit_hdp_flush = uvd_v5_0_ring_emit_hdp_flush,
870	.emit_hdp_invalidate = uvd_v5_0_ring_emit_hdp_invalidate,
871	.test_ring = uvd_v5_0_ring_test_ring,
872	.test_ib = amdgpu_uvd_ring_test_ib,
873	.insert_nop = amdgpu_ring_insert_nop,
874	.pad_ib = amdgpu_ring_generic_pad_ib,
875	.begin_use = amdgpu_uvd_ring_begin_use,
876	.end_use = amdgpu_uvd_ring_end_use,
877};
878
879static void uvd_v5_0_set_ring_funcs(struct amdgpu_device *adev)
880{
881	adev->uvd.ring.funcs = &uvd_v5_0_ring_funcs;
882}
883
884static const struct amdgpu_irq_src_funcs uvd_v5_0_irq_funcs = {
885	.set = uvd_v5_0_set_interrupt_state,
886	.process = uvd_v5_0_process_interrupt,
887};
888
889static void uvd_v5_0_set_irq_funcs(struct amdgpu_device *adev)
890{
891	adev->uvd.irq.num_types = 1;
892	adev->uvd.irq.funcs = &uvd_v5_0_irq_funcs;
893}
894
895const struct amdgpu_ip_block_version uvd_v5_0_ip_block =
896{
897		.type = AMD_IP_BLOCK_TYPE_UVD,
898		.major = 5,
899		.minor = 0,
900		.rev = 0,
901		.funcs = &uvd_v5_0_ip_funcs,
902};