1c6f73aabSFrançois Tigeot /*
2c6f73aabSFrançois Tigeot * Copyright 2013 Advanced Micro Devices, Inc.
3c6f73aabSFrançois Tigeot * All Rights Reserved.
4c6f73aabSFrançois Tigeot *
5c6f73aabSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
6c6f73aabSFrançois Tigeot * copy of this software and associated documentation files (the
7c6f73aabSFrançois Tigeot * "Software"), to deal in the Software without restriction, including
8c6f73aabSFrançois Tigeot * without limitation the rights to use, copy, modify, merge, publish,
9c6f73aabSFrançois Tigeot * distribute, sub license, and/or sell copies of the Software, and to
10c6f73aabSFrançois Tigeot * permit persons to whom the Software is furnished to do so, subject to
11c6f73aabSFrançois Tigeot * the following conditions:
12c6f73aabSFrançois Tigeot *
13c6f73aabSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14c6f73aabSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15c6f73aabSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16c6f73aabSFrançois Tigeot * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17c6f73aabSFrançois Tigeot * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18c6f73aabSFrançois Tigeot * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19c6f73aabSFrançois Tigeot * USE OR OTHER DEALINGS IN THE SOFTWARE.
20c6f73aabSFrançois Tigeot *
21c6f73aabSFrançois Tigeot * The above copyright notice and this permission notice (including the
22c6f73aabSFrançois Tigeot * next paragraph) shall be included in all copies or substantial portions
23c6f73aabSFrançois Tigeot * of the Software.
24c6f73aabSFrançois Tigeot *
25c6f73aabSFrançois Tigeot * Authors: Christian König <christian.koenig@amd.com>
26c6f73aabSFrançois Tigeot */
27c6f73aabSFrançois Tigeot
28c6f73aabSFrançois Tigeot #include <linux/firmware.h>
29c6f73aabSFrançois Tigeot #include <drm/drmP.h>
30c6f73aabSFrançois Tigeot #include "radeon.h"
31c6f73aabSFrançois Tigeot #include "radeon_asic.h"
32c6f73aabSFrançois Tigeot #include "cikd.h"
33c6f73aabSFrançois Tigeot
34c59a5c48SFrançois Tigeot #define VCE_V2_0_FW_SIZE (256 * 1024)
35c59a5c48SFrançois Tigeot #define VCE_V2_0_STACK_SIZE (64 * 1024)
36c59a5c48SFrançois Tigeot #define VCE_V2_0_DATA_SIZE (23552 * RADEON_MAX_VCE_HANDLES)
37c59a5c48SFrançois Tigeot
vce_v2_0_set_sw_cg(struct radeon_device * rdev,bool gated)38c6f73aabSFrançois Tigeot static void vce_v2_0_set_sw_cg(struct radeon_device *rdev, bool gated)
39c6f73aabSFrançois Tigeot {
40c6f73aabSFrançois Tigeot u32 tmp;
41c6f73aabSFrançois Tigeot
42c6f73aabSFrançois Tigeot if (gated) {
43c6f73aabSFrançois Tigeot tmp = RREG32(VCE_CLOCK_GATING_B);
44c6f73aabSFrançois Tigeot tmp |= 0xe70000;
45c6f73aabSFrançois Tigeot WREG32(VCE_CLOCK_GATING_B, tmp);
46c6f73aabSFrançois Tigeot
47c6f73aabSFrançois Tigeot tmp = RREG32(VCE_UENC_CLOCK_GATING);
48c6f73aabSFrançois Tigeot tmp |= 0xff000000;
49c6f73aabSFrançois Tigeot WREG32(VCE_UENC_CLOCK_GATING, tmp);
50c6f73aabSFrançois Tigeot
51c6f73aabSFrançois Tigeot tmp = RREG32(VCE_UENC_REG_CLOCK_GATING);
52c6f73aabSFrançois Tigeot tmp &= ~0x3fc;
53c6f73aabSFrançois Tigeot WREG32(VCE_UENC_REG_CLOCK_GATING, tmp);
54c6f73aabSFrançois Tigeot
55c6f73aabSFrançois Tigeot WREG32(VCE_CGTT_CLK_OVERRIDE, 0);
56c6f73aabSFrançois Tigeot } else {
57c6f73aabSFrançois Tigeot tmp = RREG32(VCE_CLOCK_GATING_B);
58c6f73aabSFrançois Tigeot tmp |= 0xe7;
59c6f73aabSFrançois Tigeot tmp &= ~0xe70000;
60c6f73aabSFrançois Tigeot WREG32(VCE_CLOCK_GATING_B, tmp);
61c6f73aabSFrançois Tigeot
62c6f73aabSFrançois Tigeot tmp = RREG32(VCE_UENC_CLOCK_GATING);
63c6f73aabSFrançois Tigeot tmp |= 0x1fe000;
64c6f73aabSFrançois Tigeot tmp &= ~0xff000000;
65c6f73aabSFrançois Tigeot WREG32(VCE_UENC_CLOCK_GATING, tmp);
66c6f73aabSFrançois Tigeot
67c6f73aabSFrançois Tigeot tmp = RREG32(VCE_UENC_REG_CLOCK_GATING);
68c6f73aabSFrançois Tigeot tmp |= 0x3fc;
69c6f73aabSFrançois Tigeot WREG32(VCE_UENC_REG_CLOCK_GATING, tmp);
70c6f73aabSFrançois Tigeot }
71c6f73aabSFrançois Tigeot }
72c6f73aabSFrançois Tigeot
vce_v2_0_set_dyn_cg(struct radeon_device * rdev,bool gated)73c6f73aabSFrançois Tigeot static void vce_v2_0_set_dyn_cg(struct radeon_device *rdev, bool gated)
74c6f73aabSFrançois Tigeot {
75c6f73aabSFrançois Tigeot u32 orig, tmp;
76c6f73aabSFrançois Tigeot
77c6f73aabSFrançois Tigeot tmp = RREG32(VCE_CLOCK_GATING_B);
78c6f73aabSFrançois Tigeot tmp &= ~0x00060006;
79c6f73aabSFrançois Tigeot if (gated) {
80c6f73aabSFrançois Tigeot tmp |= 0xe10000;
81c6f73aabSFrançois Tigeot } else {
82c6f73aabSFrançois Tigeot tmp |= 0xe1;
83c6f73aabSFrançois Tigeot tmp &= ~0xe10000;
84c6f73aabSFrançois Tigeot }
85c6f73aabSFrançois Tigeot WREG32(VCE_CLOCK_GATING_B, tmp);
86c6f73aabSFrançois Tigeot
87c6f73aabSFrançois Tigeot orig = tmp = RREG32(VCE_UENC_CLOCK_GATING);
88c6f73aabSFrançois Tigeot tmp &= ~0x1fe000;
89c6f73aabSFrançois Tigeot tmp &= ~0xff000000;
90c6f73aabSFrançois Tigeot if (tmp != orig)
91c6f73aabSFrançois Tigeot WREG32(VCE_UENC_CLOCK_GATING, tmp);
92c6f73aabSFrançois Tigeot
93c6f73aabSFrançois Tigeot orig = tmp = RREG32(VCE_UENC_REG_CLOCK_GATING);
94c6f73aabSFrançois Tigeot tmp &= ~0x3fc;
95c6f73aabSFrançois Tigeot if (tmp != orig)
96c6f73aabSFrançois Tigeot WREG32(VCE_UENC_REG_CLOCK_GATING, tmp);
97c6f73aabSFrançois Tigeot
98c6f73aabSFrançois Tigeot if (gated)
99c6f73aabSFrançois Tigeot WREG32(VCE_CGTT_CLK_OVERRIDE, 0);
100c6f73aabSFrançois Tigeot }
101c6f73aabSFrançois Tigeot
vce_v2_0_disable_cg(struct radeon_device * rdev)102c6f73aabSFrançois Tigeot static void vce_v2_0_disable_cg(struct radeon_device *rdev)
103c6f73aabSFrançois Tigeot {
104c6f73aabSFrançois Tigeot WREG32(VCE_CGTT_CLK_OVERRIDE, 7);
105c6f73aabSFrançois Tigeot }
106c6f73aabSFrançois Tigeot
107*3f2dd94aSFrançois Tigeot /*
108*3f2dd94aSFrançois Tigeot * Local variable sw_cg is used for debugging purposes, in case we
109*3f2dd94aSFrançois Tigeot * ran into problems with dynamic clock gating. Don't remove it.
110*3f2dd94aSFrançois Tigeot */
vce_v2_0_enable_mgcg(struct radeon_device * rdev,bool enable)111c6f73aabSFrançois Tigeot void vce_v2_0_enable_mgcg(struct radeon_device *rdev, bool enable)
112c6f73aabSFrançois Tigeot {
113c6f73aabSFrançois Tigeot bool sw_cg = false;
114c6f73aabSFrançois Tigeot
115c6f73aabSFrançois Tigeot if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_VCE_MGCG)) {
116c6f73aabSFrançois Tigeot if (sw_cg)
117c6f73aabSFrançois Tigeot vce_v2_0_set_sw_cg(rdev, true);
118c6f73aabSFrançois Tigeot else
119c6f73aabSFrançois Tigeot vce_v2_0_set_dyn_cg(rdev, true);
120c6f73aabSFrançois Tigeot } else {
121c6f73aabSFrançois Tigeot vce_v2_0_disable_cg(rdev);
122c6f73aabSFrançois Tigeot
123c6f73aabSFrançois Tigeot if (sw_cg)
124c6f73aabSFrançois Tigeot vce_v2_0_set_sw_cg(rdev, false);
125c6f73aabSFrançois Tigeot else
126c6f73aabSFrançois Tigeot vce_v2_0_set_dyn_cg(rdev, false);
127c6f73aabSFrançois Tigeot }
128c6f73aabSFrançois Tigeot }
129c6f73aabSFrançois Tigeot
vce_v2_0_init_cg(struct radeon_device * rdev)130c6f73aabSFrançois Tigeot static void vce_v2_0_init_cg(struct radeon_device *rdev)
131c6f73aabSFrançois Tigeot {
132c6f73aabSFrançois Tigeot u32 tmp;
133c6f73aabSFrançois Tigeot
134c6f73aabSFrançois Tigeot tmp = RREG32(VCE_CLOCK_GATING_A);
135c6f73aabSFrançois Tigeot tmp &= ~(CGC_CLK_GATE_DLY_TIMER_MASK | CGC_CLK_GATER_OFF_DLY_TIMER_MASK);
136c6f73aabSFrançois Tigeot tmp |= (CGC_CLK_GATE_DLY_TIMER(0) | CGC_CLK_GATER_OFF_DLY_TIMER(4));
137c6f73aabSFrançois Tigeot tmp |= CGC_UENC_WAIT_AWAKE;
138c6f73aabSFrançois Tigeot WREG32(VCE_CLOCK_GATING_A, tmp);
139c6f73aabSFrançois Tigeot
140c6f73aabSFrançois Tigeot tmp = RREG32(VCE_UENC_CLOCK_GATING);
141c6f73aabSFrançois Tigeot tmp &= ~(CLOCK_ON_DELAY_MASK | CLOCK_OFF_DELAY_MASK);
142c6f73aabSFrançois Tigeot tmp |= (CLOCK_ON_DELAY(0) | CLOCK_OFF_DELAY(4));
143c6f73aabSFrançois Tigeot WREG32(VCE_UENC_CLOCK_GATING, tmp);
144c6f73aabSFrançois Tigeot
145c6f73aabSFrançois Tigeot tmp = RREG32(VCE_CLOCK_GATING_B);
146c6f73aabSFrançois Tigeot tmp |= 0x10;
147c6f73aabSFrançois Tigeot tmp &= ~0x100000;
148c6f73aabSFrançois Tigeot WREG32(VCE_CLOCK_GATING_B, tmp);
149c6f73aabSFrançois Tigeot }
150c6f73aabSFrançois Tigeot
vce_v2_0_bo_size(struct radeon_device * rdev)151c59a5c48SFrançois Tigeot unsigned vce_v2_0_bo_size(struct radeon_device *rdev)
152c59a5c48SFrançois Tigeot {
153c59a5c48SFrançois Tigeot WARN_ON(rdev->vce_fw->datasize > VCE_V2_0_FW_SIZE);
154c59a5c48SFrançois Tigeot return VCE_V2_0_FW_SIZE + VCE_V2_0_STACK_SIZE + VCE_V2_0_DATA_SIZE;
155c59a5c48SFrançois Tigeot }
156c59a5c48SFrançois Tigeot
vce_v2_0_resume(struct radeon_device * rdev)157c6f73aabSFrançois Tigeot int vce_v2_0_resume(struct radeon_device *rdev)
158c6f73aabSFrançois Tigeot {
159c6f73aabSFrançois Tigeot uint64_t addr = rdev->vce.gpu_addr;
160c6f73aabSFrançois Tigeot uint32_t size;
161c6f73aabSFrançois Tigeot
162c6f73aabSFrançois Tigeot WREG32_P(VCE_CLOCK_GATING_A, 0, ~(1 << 16));
163c6f73aabSFrançois Tigeot WREG32_P(VCE_UENC_CLOCK_GATING, 0x1FF000, ~0xFF9FF000);
164c6f73aabSFrançois Tigeot WREG32_P(VCE_UENC_REG_CLOCK_GATING, 0x3F, ~0x3F);
165c6f73aabSFrançois Tigeot WREG32(VCE_CLOCK_GATING_B, 0xf7);
166c6f73aabSFrançois Tigeot
167c6f73aabSFrançois Tigeot WREG32(VCE_LMI_CTRL, 0x00398000);
168c6f73aabSFrançois Tigeot WREG32_P(VCE_LMI_CACHE_CTRL, 0x0, ~0x1);
169c6f73aabSFrançois Tigeot WREG32(VCE_LMI_SWAP_CNTL, 0);
170c6f73aabSFrançois Tigeot WREG32(VCE_LMI_SWAP_CNTL1, 0);
171c6f73aabSFrançois Tigeot WREG32(VCE_LMI_VM_CTRL, 0);
172c6f73aabSFrançois Tigeot
173c59a5c48SFrançois Tigeot WREG32(VCE_LMI_VCPU_CACHE_40BIT_BAR, addr >> 8);
174c59a5c48SFrançois Tigeot
175c59a5c48SFrançois Tigeot addr &= 0xff;
176c59a5c48SFrançois Tigeot size = VCE_V2_0_FW_SIZE;
177c6f73aabSFrançois Tigeot WREG32(VCE_VCPU_CACHE_OFFSET0, addr & 0x7fffffff);
178c6f73aabSFrançois Tigeot WREG32(VCE_VCPU_CACHE_SIZE0, size);
179c6f73aabSFrançois Tigeot
180c6f73aabSFrançois Tigeot addr += size;
181c59a5c48SFrançois Tigeot size = VCE_V2_0_STACK_SIZE;
182c6f73aabSFrançois Tigeot WREG32(VCE_VCPU_CACHE_OFFSET1, addr & 0x7fffffff);
183c6f73aabSFrançois Tigeot WREG32(VCE_VCPU_CACHE_SIZE1, size);
184c6f73aabSFrançois Tigeot
185c6f73aabSFrançois Tigeot addr += size;
186c59a5c48SFrançois Tigeot size = VCE_V2_0_DATA_SIZE;
187c6f73aabSFrançois Tigeot WREG32(VCE_VCPU_CACHE_OFFSET2, addr & 0x7fffffff);
188c6f73aabSFrançois Tigeot WREG32(VCE_VCPU_CACHE_SIZE2, size);
189c6f73aabSFrançois Tigeot
190c6f73aabSFrançois Tigeot WREG32_P(VCE_LMI_CTRL2, 0x0, ~0x100);
191c6f73aabSFrançois Tigeot
192c6f73aabSFrançois Tigeot WREG32_P(VCE_SYS_INT_EN, VCE_SYS_INT_TRAP_INTERRUPT_EN,
193c6f73aabSFrançois Tigeot ~VCE_SYS_INT_TRAP_INTERRUPT_EN);
194c6f73aabSFrançois Tigeot
195c6f73aabSFrançois Tigeot vce_v2_0_init_cg(rdev);
196c6f73aabSFrançois Tigeot
197c6f73aabSFrançois Tigeot return 0;
198c6f73aabSFrançois Tigeot }
199