1f06d5e42SLikun Gao /*
2f06d5e42SLikun Gao * Copyright 2020 Advanced Micro Devices, Inc.
3f06d5e42SLikun Gao *
4f06d5e42SLikun Gao * Permission is hereby granted, free of charge, to any person obtaining a
5f06d5e42SLikun Gao * copy of this software and associated documentation files (the "Software"),
6f06d5e42SLikun Gao * to deal in the Software without restriction, including without limitation
7f06d5e42SLikun Gao * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f06d5e42SLikun Gao * and/or sell copies of the Software, and to permit persons to whom the
9f06d5e42SLikun Gao * Software is furnished to do so, subject to the following conditions:
10f06d5e42SLikun Gao *
11f06d5e42SLikun Gao * The above copyright notice and this permission notice shall be included in
12f06d5e42SLikun Gao * all copies or substantial portions of the Software.
13f06d5e42SLikun Gao *
14f06d5e42SLikun Gao * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f06d5e42SLikun Gao * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f06d5e42SLikun Gao * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17f06d5e42SLikun Gao * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18f06d5e42SLikun Gao * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19f06d5e42SLikun Gao * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20f06d5e42SLikun Gao * OTHER DEALINGS IN THE SOFTWARE.
21f06d5e42SLikun Gao *
22f06d5e42SLikun Gao */
23f06d5e42SLikun Gao #include "amdgpu.h"
24f06d5e42SLikun Gao #include "amdgpu_atombios.h"
25f06d5e42SLikun Gao #include "hdp_v5_0.h"
26f06d5e42SLikun Gao
27f06d5e42SLikun Gao #include "hdp/hdp_5_0_0_offset.h"
28f06d5e42SLikun Gao #include "hdp/hdp_5_0_0_sh_mask.h"
29f06d5e42SLikun Gao #include <uapi/linux/kfd_ioctl.h>
30f06d5e42SLikun Gao
hdp_v5_0_flush_hdp(struct amdgpu_device * adev,struct amdgpu_ring * ring)31f06d5e42SLikun Gao static void hdp_v5_0_flush_hdp(struct amdgpu_device *adev,
32f06d5e42SLikun Gao struct amdgpu_ring *ring)
33f06d5e42SLikun Gao {
34*c9ad5cbfSAlex Deucher if (!ring || !ring->funcs->emit_wreg) {
35a9cb2cc5SVictor Zhao WREG32((adev->rmmio_remap.reg_offset + KFD_MMIO_REMAP_HDP_MEM_FLUSH_CNTL) >> 2, 0);
36*c9ad5cbfSAlex Deucher RREG32((adev->rmmio_remap.reg_offset + KFD_MMIO_REMAP_HDP_MEM_FLUSH_CNTL) >> 2);
37*c9ad5cbfSAlex Deucher } else {
38f06d5e42SLikun Gao amdgpu_ring_emit_wreg(ring, (adev->rmmio_remap.reg_offset + KFD_MMIO_REMAP_HDP_MEM_FLUSH_CNTL) >> 2, 0);
39f06d5e42SLikun Gao }
40*c9ad5cbfSAlex Deucher }
41f06d5e42SLikun Gao
hdp_v5_0_invalidate_hdp(struct amdgpu_device * adev,struct amdgpu_ring * ring)42f06d5e42SLikun Gao static void hdp_v5_0_invalidate_hdp(struct amdgpu_device *adev,
43f06d5e42SLikun Gao struct amdgpu_ring *ring)
44f06d5e42SLikun Gao {
45f06d5e42SLikun Gao if (!ring || !ring->funcs->emit_wreg) {
46f06d5e42SLikun Gao WREG32_SOC15_NO_KIQ(HDP, 0, mmHDP_READ_CACHE_INVALIDATE, 1);
47*c9ad5cbfSAlex Deucher RREG32_SOC15_NO_KIQ(HDP, 0, mmHDP_READ_CACHE_INVALIDATE);
48f06d5e42SLikun Gao } else {
49f06d5e42SLikun Gao amdgpu_ring_emit_wreg(ring, SOC15_REG_OFFSET(
50f06d5e42SLikun Gao HDP, 0, mmHDP_READ_CACHE_INVALIDATE), 1);
51f06d5e42SLikun Gao }
52f06d5e42SLikun Gao }
53f06d5e42SLikun Gao
hdp_v5_0_update_mem_power_gating(struct amdgpu_device * adev,bool enable)54f06d5e42SLikun Gao static void hdp_v5_0_update_mem_power_gating(struct amdgpu_device *adev,
55f06d5e42SLikun Gao bool enable)
56f06d5e42SLikun Gao {
57f06d5e42SLikun Gao uint32_t hdp_clk_cntl, hdp_clk_cntl1;
58f06d5e42SLikun Gao uint32_t hdp_mem_pwr_cntl;
59f06d5e42SLikun Gao
60f06d5e42SLikun Gao if (!(adev->cg_flags & (AMD_CG_SUPPORT_HDP_LS |
61f06d5e42SLikun Gao AMD_CG_SUPPORT_HDP_DS |
62f06d5e42SLikun Gao AMD_CG_SUPPORT_HDP_SD)))
63f06d5e42SLikun Gao return;
64f06d5e42SLikun Gao
65f06d5e42SLikun Gao hdp_clk_cntl = hdp_clk_cntl1 = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL);
66f06d5e42SLikun Gao hdp_mem_pwr_cntl = RREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL);
67f06d5e42SLikun Gao
68f06d5e42SLikun Gao /* Before doing clock/power mode switch,
69f06d5e42SLikun Gao * forced on IPH & RC clock */
70f06d5e42SLikun Gao hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL,
71f06d5e42SLikun Gao IPH_MEM_CLK_SOFT_OVERRIDE, 1);
72f06d5e42SLikun Gao hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL,
73f06d5e42SLikun Gao RC_MEM_CLK_SOFT_OVERRIDE, 1);
74f06d5e42SLikun Gao WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl);
75f06d5e42SLikun Gao
76f06d5e42SLikun Gao /* HDP 5.0 doesn't support dynamic power mode switch,
77f06d5e42SLikun Gao * disable clock and power gating before any changing */
78f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
79f06d5e42SLikun Gao IPH_MEM_POWER_CTRL_EN, 0);
80f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
81f06d5e42SLikun Gao IPH_MEM_POWER_LS_EN, 0);
82f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
83f06d5e42SLikun Gao IPH_MEM_POWER_DS_EN, 0);
84f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
85f06d5e42SLikun Gao IPH_MEM_POWER_SD_EN, 0);
86f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
87f06d5e42SLikun Gao RC_MEM_POWER_CTRL_EN, 0);
88f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
89f06d5e42SLikun Gao RC_MEM_POWER_LS_EN, 0);
90f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
91f06d5e42SLikun Gao RC_MEM_POWER_DS_EN, 0);
92f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
93f06d5e42SLikun Gao RC_MEM_POWER_SD_EN, 0);
94f06d5e42SLikun Gao WREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL, hdp_mem_pwr_cntl);
95f06d5e42SLikun Gao
96ff4b601aSEvan Quan /* Already disabled above. The actions below are for "enabled" only */
97ff4b601aSEvan Quan if (enable) {
98f06d5e42SLikun Gao /* only one clock gating mode (LS/DS/SD) can be enabled */
99f06d5e42SLikun Gao if (adev->cg_flags & AMD_CG_SUPPORT_HDP_LS) {
100f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
101f06d5e42SLikun Gao HDP_MEM_POWER_CTRL,
102ff4b601aSEvan Quan IPH_MEM_POWER_LS_EN, 1);
103f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
104f06d5e42SLikun Gao HDP_MEM_POWER_CTRL,
105ff4b601aSEvan Quan RC_MEM_POWER_LS_EN, 1);
106f06d5e42SLikun Gao } else if (adev->cg_flags & AMD_CG_SUPPORT_HDP_DS) {
107f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
108f06d5e42SLikun Gao HDP_MEM_POWER_CTRL,
109ff4b601aSEvan Quan IPH_MEM_POWER_DS_EN, 1);
110f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
111f06d5e42SLikun Gao HDP_MEM_POWER_CTRL,
112ff4b601aSEvan Quan RC_MEM_POWER_DS_EN, 1);
113f06d5e42SLikun Gao } else if (adev->cg_flags & AMD_CG_SUPPORT_HDP_SD) {
114f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
115f06d5e42SLikun Gao HDP_MEM_POWER_CTRL,
116ff4b601aSEvan Quan IPH_MEM_POWER_SD_EN, 1);
117ff4b601aSEvan Quan /* RC should not use shut down mode, fallback to ds or ls if allowed */
118ff4b601aSEvan Quan if (adev->cg_flags & AMD_CG_SUPPORT_HDP_DS)
119f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
120f06d5e42SLikun Gao HDP_MEM_POWER_CTRL,
121ff4b601aSEvan Quan RC_MEM_POWER_DS_EN, 1);
122ff4b601aSEvan Quan else if (adev->cg_flags & AMD_CG_SUPPORT_HDP_LS)
123ff4b601aSEvan Quan hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
124ff4b601aSEvan Quan HDP_MEM_POWER_CTRL,
125ff4b601aSEvan Quan RC_MEM_POWER_LS_EN, 1);
126f06d5e42SLikun Gao }
127f06d5e42SLikun Gao
128f06d5e42SLikun Gao /* confirmed that IPH_MEM_POWER_CTRL_EN and RC_MEM_POWER_CTRL_EN have to
129f06d5e42SLikun Gao * be set for SRAM LS/DS/SD */
130f06d5e42SLikun Gao if (adev->cg_flags & (AMD_CG_SUPPORT_HDP_LS | AMD_CG_SUPPORT_HDP_DS |
131f06d5e42SLikun Gao AMD_CG_SUPPORT_HDP_SD)) {
132f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
133f06d5e42SLikun Gao IPH_MEM_POWER_CTRL_EN, 1);
134f06d5e42SLikun Gao hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
135f06d5e42SLikun Gao RC_MEM_POWER_CTRL_EN, 1);
136ff4b601aSEvan Quan WREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL, hdp_mem_pwr_cntl);
137ff4b601aSEvan Quan }
138f06d5e42SLikun Gao }
139f06d5e42SLikun Gao
140ff4b601aSEvan Quan /* disable IPH & RC clock override after clock/power mode changing */
141ff4b601aSEvan Quan hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL,
142ff4b601aSEvan Quan IPH_MEM_CLK_SOFT_OVERRIDE, 0);
143ff4b601aSEvan Quan hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL,
144ff4b601aSEvan Quan RC_MEM_CLK_SOFT_OVERRIDE, 0);
145ff4b601aSEvan Quan WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl);
146f06d5e42SLikun Gao }
147f06d5e42SLikun Gao
hdp_v5_0_update_medium_grain_clock_gating(struct amdgpu_device * adev,bool enable)148f06d5e42SLikun Gao static void hdp_v5_0_update_medium_grain_clock_gating(struct amdgpu_device *adev,
149f06d5e42SLikun Gao bool enable)
150f06d5e42SLikun Gao {
151f06d5e42SLikun Gao uint32_t hdp_clk_cntl;
152f06d5e42SLikun Gao
153f06d5e42SLikun Gao if (!(adev->cg_flags & AMD_CG_SUPPORT_HDP_MGCG))
154f06d5e42SLikun Gao return;
155f06d5e42SLikun Gao
156f06d5e42SLikun Gao hdp_clk_cntl = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL);
157f06d5e42SLikun Gao
158f06d5e42SLikun Gao if (enable) {
159f06d5e42SLikun Gao hdp_clk_cntl &=
160f06d5e42SLikun Gao ~(uint32_t)
161f06d5e42SLikun Gao (HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK |
162f06d5e42SLikun Gao HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK |
163f06d5e42SLikun Gao HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK |
164f06d5e42SLikun Gao HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK |
165f06d5e42SLikun Gao HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK |
166f06d5e42SLikun Gao HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK);
167f06d5e42SLikun Gao } else {
168f06d5e42SLikun Gao hdp_clk_cntl |= HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK |
169f06d5e42SLikun Gao HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK |
170f06d5e42SLikun Gao HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK |
171f06d5e42SLikun Gao HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK |
172f06d5e42SLikun Gao HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK |
173f06d5e42SLikun Gao HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK;
174f06d5e42SLikun Gao }
175f06d5e42SLikun Gao
176f06d5e42SLikun Gao WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl);
177f06d5e42SLikun Gao }
178f06d5e42SLikun Gao
hdp_v5_0_update_clock_gating(struct amdgpu_device * adev,bool enable)179f06d5e42SLikun Gao static void hdp_v5_0_update_clock_gating(struct amdgpu_device *adev,
180f06d5e42SLikun Gao bool enable)
181f06d5e42SLikun Gao {
182f06d5e42SLikun Gao hdp_v5_0_update_mem_power_gating(adev, enable);
183f06d5e42SLikun Gao hdp_v5_0_update_medium_grain_clock_gating(adev, enable);
184f06d5e42SLikun Gao }
185f06d5e42SLikun Gao
hdp_v5_0_get_clockgating_state(struct amdgpu_device * adev,u64 * flags)186f06d5e42SLikun Gao static void hdp_v5_0_get_clockgating_state(struct amdgpu_device *adev,
18725faeddcSEvan Quan u64 *flags)
188f06d5e42SLikun Gao {
189f06d5e42SLikun Gao uint32_t tmp;
190f06d5e42SLikun Gao
191f06d5e42SLikun Gao /* AMD_CG_SUPPORT_HDP_MGCG */
192f06d5e42SLikun Gao tmp = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL);
193f06d5e42SLikun Gao if (!(tmp & (HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK |
194f06d5e42SLikun Gao HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK |
195f06d5e42SLikun Gao HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK |
196f06d5e42SLikun Gao HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK |
197f06d5e42SLikun Gao HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK |
198f06d5e42SLikun Gao HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK)))
199f06d5e42SLikun Gao *flags |= AMD_CG_SUPPORT_HDP_MGCG;
200f06d5e42SLikun Gao
201f06d5e42SLikun Gao /* AMD_CG_SUPPORT_HDP_LS/DS/SD */
202f06d5e42SLikun Gao tmp = RREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL);
203f06d5e42SLikun Gao if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_LS_EN_MASK)
204f06d5e42SLikun Gao *flags |= AMD_CG_SUPPORT_HDP_LS;
205f06d5e42SLikun Gao else if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_DS_EN_MASK)
206f06d5e42SLikun Gao *flags |= AMD_CG_SUPPORT_HDP_DS;
207f06d5e42SLikun Gao else if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_SD_EN_MASK)
208f06d5e42SLikun Gao *flags |= AMD_CG_SUPPORT_HDP_SD;
209f06d5e42SLikun Gao }
210f06d5e42SLikun Gao
hdp_v5_0_init_registers(struct amdgpu_device * adev)211f06d5e42SLikun Gao static void hdp_v5_0_init_registers(struct amdgpu_device *adev)
212f06d5e42SLikun Gao {
213f06d5e42SLikun Gao u32 tmp;
214f06d5e42SLikun Gao
215f06d5e42SLikun Gao tmp = RREG32_SOC15(HDP, 0, mmHDP_MISC_CNTL);
216f06d5e42SLikun Gao tmp |= HDP_MISC_CNTL__FLUSH_INVALIDATE_CACHE_MASK;
217f06d5e42SLikun Gao WREG32_SOC15(HDP, 0, mmHDP_MISC_CNTL, tmp);
218f06d5e42SLikun Gao }
219f06d5e42SLikun Gao
220f06d5e42SLikun Gao const struct amdgpu_hdp_funcs hdp_v5_0_funcs = {
221f06d5e42SLikun Gao .flush_hdp = hdp_v5_0_flush_hdp,
222f06d5e42SLikun Gao .invalidate_hdp = hdp_v5_0_invalidate_hdp,
223f06d5e42SLikun Gao .update_clock_gating = hdp_v5_0_update_clock_gating,
224f06d5e42SLikun Gao .get_clock_gating_state = hdp_v5_0_get_clockgating_state,
225f06d5e42SLikun Gao .init_registers = hdp_v5_0_init_registers,
226f06d5e42SLikun Gao };
227