blob: e7ea28c0c5b85f7b766a0c4cbbc12ef414226af4 [file] [log] [blame]
Alex Deucheraaa36a92015-04-20 17:31:14 -04001/*
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 */
23
24#include <linux/firmware.h>
25#include <linux/seq_file.h>
26#include "drmP.h"
27#include "amdgpu.h"
28#include "amdgpu_pm.h"
29#include "amdgpu_atombios.h"
30#include "vid.h"
31#include "vi_dpm.h"
32#include "amdgpu_dpm.h"
33#include "cz_dpm.h"
34#include "cz_ppsmc.h"
35#include "atom.h"
36
37#include "smu/smu_8_0_d.h"
38#include "smu/smu_8_0_sh_mask.h"
39#include "gca/gfx_8_0_d.h"
40#include "gca/gfx_8_0_sh_mask.h"
41#include "gmc/gmc_8_1_d.h"
42#include "bif/bif_5_1_d.h"
43#include "gfx_v8_0.h"
44
Sonny Jiang564ea792015-05-12 16:13:35 -040045static void cz_dpm_powergate_uvd(struct amdgpu_device *adev, bool gate);
Sonny Jiangb7a077692015-05-28 15:47:53 -040046static void cz_dpm_powergate_vce(struct amdgpu_device *adev, bool gate);
Tom St Denise701f972016-08-25 12:41:15 -040047static void cz_dpm_fini(struct amdgpu_device *adev);
Sonny Jiang564ea792015-05-12 16:13:35 -040048
Alex Deucheraaa36a92015-04-20 17:31:14 -040049static struct cz_ps *cz_get_ps(struct amdgpu_ps *rps)
50{
51 struct cz_ps *ps = rps->ps_priv;
52
53 return ps;
54}
55
56static struct cz_power_info *cz_get_pi(struct amdgpu_device *adev)
57{
58 struct cz_power_info *pi = adev->pm.dpm.priv;
59
60 return pi;
61}
62
63static uint16_t cz_convert_8bit_index_to_voltage(struct amdgpu_device *adev,
64 uint16_t voltage)
65{
66 uint16_t tmp = 6200 - voltage * 25;
67
68 return tmp;
69}
70
71static void cz_construct_max_power_limits_table(struct amdgpu_device *adev,
72 struct amdgpu_clock_and_voltage_limits *table)
73{
74 struct cz_power_info *pi = cz_get_pi(adev);
75 struct amdgpu_clock_voltage_dependency_table *dep_table =
76 &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
77
78 if (dep_table->count > 0) {
79 table->sclk = dep_table->entries[dep_table->count - 1].clk;
80 table->vddc = cz_convert_8bit_index_to_voltage(adev,
81 dep_table->entries[dep_table->count - 1].v);
82 }
83
84 table->mclk = pi->sys_info.nbp_memory_clock[0];
85
86}
87
88union igp_info {
89 struct _ATOM_INTEGRATED_SYSTEM_INFO info;
90 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
91 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
92 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_9 info_9;
93};
94
95static int cz_parse_sys_info_table(struct amdgpu_device *adev)
96{
97 struct cz_power_info *pi = cz_get_pi(adev);
98 struct amdgpu_mode_info *mode_info = &adev->mode_info;
99 int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
100 union igp_info *igp_info;
101 u8 frev, crev;
102 u16 data_offset;
103 int i = 0;
104
105 if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
106 &frev, &crev, &data_offset)) {
107 igp_info = (union igp_info *)(mode_info->atom_context->bios +
108 data_offset);
109
110 if (crev != 9) {
111 DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
112 return -EINVAL;
113 }
114 pi->sys_info.bootup_sclk =
115 le32_to_cpu(igp_info->info_9.ulBootUpEngineClock);
116 pi->sys_info.bootup_uma_clk =
117 le32_to_cpu(igp_info->info_9.ulBootUpUMAClock);
118 pi->sys_info.dentist_vco_freq =
119 le32_to_cpu(igp_info->info_9.ulDentistVCOFreq);
120 pi->sys_info.bootup_nb_voltage_index =
121 le16_to_cpu(igp_info->info_9.usBootUpNBVoltage);
122
123 if (igp_info->info_9.ucHtcTmpLmt == 0)
124 pi->sys_info.htc_tmp_lmt = 203;
125 else
126 pi->sys_info.htc_tmp_lmt = igp_info->info_9.ucHtcTmpLmt;
127
128 if (igp_info->info_9.ucHtcHystLmt == 0)
129 pi->sys_info.htc_hyst_lmt = 5;
130 else
131 pi->sys_info.htc_hyst_lmt = igp_info->info_9.ucHtcHystLmt;
132
133 if (pi->sys_info.htc_tmp_lmt <= pi->sys_info.htc_hyst_lmt) {
134 DRM_ERROR("The htcTmpLmt should be larger than htcHystLmt.\n");
135 return -EINVAL;
136 }
137
138 if (le32_to_cpu(igp_info->info_9.ulSystemConfig) & (1 << 3) &&
139 pi->enable_nb_ps_policy)
140 pi->sys_info.nb_dpm_enable = true;
141 else
142 pi->sys_info.nb_dpm_enable = false;
143
144 for (i = 0; i < CZ_NUM_NBPSTATES; i++) {
145 if (i < CZ_NUM_NBPMEMORY_CLOCK)
146 pi->sys_info.nbp_memory_clock[i] =
147 le32_to_cpu(igp_info->info_9.ulNbpStateMemclkFreq[i]);
148 pi->sys_info.nbp_n_clock[i] =
149 le32_to_cpu(igp_info->info_9.ulNbpStateNClkFreq[i]);
150 }
151
152 for (i = 0; i < CZ_MAX_DISPLAY_CLOCK_LEVEL; i++)
153 pi->sys_info.display_clock[i] =
154 le32_to_cpu(igp_info->info_9.sDispClkVoltageMapping[i].ulMaximumSupportedCLK);
155
156 for (i = 0; i < CZ_NUM_NBPSTATES; i++)
157 pi->sys_info.nbp_voltage_index[i] =
158 le32_to_cpu(igp_info->info_9.usNBPStateVoltage[i]);
159
160 if (le32_to_cpu(igp_info->info_9.ulGPUCapInfo) &
161 SYS_INFO_GPUCAPS__ENABEL_DFS_BYPASS)
162 pi->caps_enable_dfs_bypass = true;
163
164 pi->sys_info.uma_channel_number =
165 igp_info->info_9.ucUMAChannelNumber;
166
167 cz_construct_max_power_limits_table(adev,
168 &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
169 }
170
171 return 0;
172}
173
174static void cz_patch_voltage_values(struct amdgpu_device *adev)
175{
176 int i;
177 struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
178 &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
179 struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
180 &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
181 struct amdgpu_clock_voltage_dependency_table *acp_table =
182 &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
183
184 if (uvd_table->count) {
185 for (i = 0; i < uvd_table->count; i++)
186 uvd_table->entries[i].v =
187 cz_convert_8bit_index_to_voltage(adev,
188 uvd_table->entries[i].v);
189 }
190
191 if (vce_table->count) {
192 for (i = 0; i < vce_table->count; i++)
193 vce_table->entries[i].v =
194 cz_convert_8bit_index_to_voltage(adev,
195 vce_table->entries[i].v);
196 }
197
198 if (acp_table->count) {
199 for (i = 0; i < acp_table->count; i++)
200 acp_table->entries[i].v =
201 cz_convert_8bit_index_to_voltage(adev,
202 acp_table->entries[i].v);
203 }
204
205}
206
207static void cz_construct_boot_state(struct amdgpu_device *adev)
208{
209 struct cz_power_info *pi = cz_get_pi(adev);
210
211 pi->boot_pl.sclk = pi->sys_info.bootup_sclk;
212 pi->boot_pl.vddc_index = pi->sys_info.bootup_nb_voltage_index;
213 pi->boot_pl.ds_divider_index = 0;
214 pi->boot_pl.ss_divider_index = 0;
215 pi->boot_pl.allow_gnb_slow = 1;
216 pi->boot_pl.force_nbp_state = 0;
217 pi->boot_pl.display_wm = 0;
218 pi->boot_pl.vce_wm = 0;
219
220}
221
222static void cz_patch_boot_state(struct amdgpu_device *adev,
223 struct cz_ps *ps)
224{
225 struct cz_power_info *pi = cz_get_pi(adev);
226
227 ps->num_levels = 1;
228 ps->levels[0] = pi->boot_pl;
229}
230
231union pplib_clock_info {
232 struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
233 struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
234 struct _ATOM_PPLIB_CZ_CLOCK_INFO carrizo;
235};
236
237static void cz_parse_pplib_clock_info(struct amdgpu_device *adev,
238 struct amdgpu_ps *rps, int index,
239 union pplib_clock_info *clock_info)
240{
241 struct cz_power_info *pi = cz_get_pi(adev);
242 struct cz_ps *ps = cz_get_ps(rps);
243 struct cz_pl *pl = &ps->levels[index];
244 struct amdgpu_clock_voltage_dependency_table *table =
245 &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
246
247 pl->sclk = table->entries[clock_info->carrizo.index].clk;
248 pl->vddc_index = table->entries[clock_info->carrizo.index].v;
249
250 ps->num_levels = index + 1;
251
252 if (pi->caps_sclk_ds) {
253 pl->ds_divider_index = 5;
254 pl->ss_divider_index = 5;
255 }
256
257}
258
259static void cz_parse_pplib_non_clock_info(struct amdgpu_device *adev,
260 struct amdgpu_ps *rps,
261 struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
262 u8 table_rev)
263{
264 struct cz_ps *ps = cz_get_ps(rps);
265
266 rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
267 rps->class = le16_to_cpu(non_clock_info->usClassification);
268 rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
269
270 if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
271 rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
272 rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
273 } else {
274 rps->vclk = 0;
275 rps->dclk = 0;
276 }
277
278 if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
279 adev->pm.dpm.boot_ps = rps;
280 cz_patch_boot_state(adev, ps);
281 }
282 if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
283 adev->pm.dpm.uvd_ps = rps;
284
285}
286
287union power_info {
288 struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
289 struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
290 struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
291 struct _ATOM_PPLIB_POWERPLAYTABLE4 pplib4;
292 struct _ATOM_PPLIB_POWERPLAYTABLE5 pplib5;
293};
294
295union pplib_power_state {
296 struct _ATOM_PPLIB_STATE v1;
297 struct _ATOM_PPLIB_STATE_V2 v2;
298};
299
300static int cz_parse_power_table(struct amdgpu_device *adev)
301{
302 struct amdgpu_mode_info *mode_info = &adev->mode_info;
303 struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
304 union pplib_power_state *power_state;
305 int i, j, k, non_clock_array_index, clock_array_index;
306 union pplib_clock_info *clock_info;
307 struct _StateArray *state_array;
308 struct _ClockInfoArray *clock_info_array;
309 struct _NonClockInfoArray *non_clock_info_array;
310 union power_info *power_info;
311 int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
312 u16 data_offset;
313 u8 frev, crev;
314 u8 *power_state_offset;
315 struct cz_ps *ps;
316
317 if (!amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
318 &frev, &crev, &data_offset))
319 return -EINVAL;
320 power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
321
322 state_array = (struct _StateArray *)
323 (mode_info->atom_context->bios + data_offset +
324 le16_to_cpu(power_info->pplib.usStateArrayOffset));
325 clock_info_array = (struct _ClockInfoArray *)
326 (mode_info->atom_context->bios + data_offset +
327 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
328 non_clock_info_array = (struct _NonClockInfoArray *)
329 (mode_info->atom_context->bios + data_offset +
330 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
331
332 adev->pm.dpm.ps = kzalloc(sizeof(struct amdgpu_ps) *
333 state_array->ucNumEntries, GFP_KERNEL);
334
335 if (!adev->pm.dpm.ps)
336 return -ENOMEM;
337
338 power_state_offset = (u8 *)state_array->states;
339 adev->pm.dpm.platform_caps =
340 le32_to_cpu(power_info->pplib.ulPlatformCaps);
341 adev->pm.dpm.backbias_response_time =
342 le16_to_cpu(power_info->pplib.usBackbiasTime);
343 adev->pm.dpm.voltage_response_time =
344 le16_to_cpu(power_info->pplib.usVoltageTime);
345
346 for (i = 0; i < state_array->ucNumEntries; i++) {
347 power_state = (union pplib_power_state *)power_state_offset;
348 non_clock_array_index = power_state->v2.nonClockInfoIndex;
349 non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
350 &non_clock_info_array->nonClockInfo[non_clock_array_index];
351
352 ps = kzalloc(sizeof(struct cz_ps), GFP_KERNEL);
353 if (ps == NULL) {
Tom St Deniscc945ce2016-08-25 12:16:24 -0400354 for (j = 0; j < i; j++)
355 kfree(adev->pm.dpm.ps[j].ps_priv);
Alex Deucheraaa36a92015-04-20 17:31:14 -0400356 kfree(adev->pm.dpm.ps);
357 return -ENOMEM;
358 }
359
360 adev->pm.dpm.ps[i].ps_priv = ps;
361 k = 0;
362 for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
363 clock_array_index = power_state->v2.clockInfoIndex[j];
364 if (clock_array_index >= clock_info_array->ucNumEntries)
365 continue;
366 if (k >= CZ_MAX_HARDWARE_POWERLEVELS)
367 break;
368 clock_info = (union pplib_clock_info *)
369 &clock_info_array->clockInfo[clock_array_index *
370 clock_info_array->ucEntrySize];
371 cz_parse_pplib_clock_info(adev, &adev->pm.dpm.ps[i],
372 k, clock_info);
373 k++;
374 }
375 cz_parse_pplib_non_clock_info(adev, &adev->pm.dpm.ps[i],
376 non_clock_info,
377 non_clock_info_array->ucEntrySize);
378 power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
379 }
380 adev->pm.dpm.num_ps = state_array->ucNumEntries;
381
382 return 0;
383}
384
385static int cz_process_firmware_header(struct amdgpu_device *adev)
386{
387 struct cz_power_info *pi = cz_get_pi(adev);
388 u32 tmp;
389 int ret;
390
391 ret = cz_read_smc_sram_dword(adev, SMU8_FIRMWARE_HEADER_LOCATION +
392 offsetof(struct SMU8_Firmware_Header,
393 DpmTable),
394 &tmp, pi->sram_end);
395
396 if (ret == 0)
397 pi->dpm_table_start = tmp;
398
399 return ret;
400}
401
402static int cz_dpm_init(struct amdgpu_device *adev)
403{
404 struct cz_power_info *pi;
405 int ret, i;
406
407 pi = kzalloc(sizeof(struct cz_power_info), GFP_KERNEL);
408 if (NULL == pi)
409 return -ENOMEM;
410
411 adev->pm.dpm.priv = pi;
412
413 ret = amdgpu_get_platform_caps(adev);
414 if (ret)
Tom St Denise701f972016-08-25 12:41:15 -0400415 goto err;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400416
417 ret = amdgpu_parse_extended_power_table(adev);
418 if (ret)
Tom St Denise701f972016-08-25 12:41:15 -0400419 goto err;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400420
421 pi->sram_end = SMC_RAM_END;
422
423 /* set up DPM defaults */
424 for (i = 0; i < CZ_MAX_HARDWARE_POWERLEVELS; i++)
425 pi->active_target[i] = CZ_AT_DFLT;
426
427 pi->mgcg_cgtt_local0 = 0x0;
428 pi->mgcg_cgtt_local1 = 0x0;
429 pi->clock_slow_down_step = 25000;
430 pi->skip_clock_slow_down = 1;
Edward O'Callaghaned5121a2016-07-12 10:17:52 +1000431 pi->enable_nb_ps_policy = false;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400432 pi->caps_power_containment = true;
433 pi->caps_cac = true;
434 pi->didt_enabled = false;
435 if (pi->didt_enabled) {
436 pi->caps_sq_ramping = true;
437 pi->caps_db_ramping = true;
438 pi->caps_td_ramping = true;
439 pi->caps_tcp_ramping = true;
440 }
Rex Zhu66bc3f72016-07-28 17:36:35 +0800441 if (amdgpu_sclk_deep_sleep_en)
442 pi->caps_sclk_ds = true;
443 else
444 pi->caps_sclk_ds = false;
445
Alex Deucheraaa36a92015-04-20 17:31:14 -0400446 pi->voting_clients = 0x00c00033;
447 pi->auto_thermal_throttling_enabled = true;
448 pi->bapm_enabled = false;
449 pi->disable_nb_ps3_in_battery = false;
450 pi->voltage_drop_threshold = 0;
451 pi->caps_sclk_throttle_low_notification = false;
452 pi->gfx_pg_threshold = 500;
453 pi->caps_fps = true;
454 /* uvd */
Alex Deuchere3b04bc2016-02-05 10:56:22 -0500455 pi->caps_uvd_pg = (adev->pg_flags & AMD_PG_SUPPORT_UVD) ? true : false;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400456 pi->caps_uvd_dpm = true;
457 /* vce */
Alex Deuchere3b04bc2016-02-05 10:56:22 -0500458 pi->caps_vce_pg = (adev->pg_flags & AMD_PG_SUPPORT_VCE) ? true : false;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400459 pi->caps_vce_dpm = true;
460 /* acp */
Alex Deuchere3b04bc2016-02-05 10:56:22 -0500461 pi->caps_acp_pg = (adev->pg_flags & AMD_PG_SUPPORT_ACP) ? true : false;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400462 pi->caps_acp_dpm = true;
463
464 pi->caps_stable_power_state = false;
465 pi->nb_dpm_enabled_by_driver = true;
466 pi->nb_dpm_enabled = false;
467 pi->caps_voltage_island = false;
468 /* flags which indicate need to upload pptable */
469 pi->need_pptable_upload = true;
470
471 ret = cz_parse_sys_info_table(adev);
472 if (ret)
Tom St Denise701f972016-08-25 12:41:15 -0400473 goto err;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400474
475 cz_patch_voltage_values(adev);
476 cz_construct_boot_state(adev);
477
478 ret = cz_parse_power_table(adev);
479 if (ret)
Tom St Denise701f972016-08-25 12:41:15 -0400480 goto err;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400481
482 ret = cz_process_firmware_header(adev);
483 if (ret)
Tom St Denise701f972016-08-25 12:41:15 -0400484 goto err;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400485
486 pi->dpm_enabled = true;
Sonny Jiang564ea792015-05-12 16:13:35 -0400487 pi->uvd_dynamic_pg = false;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400488
489 return 0;
Tom St Denise701f972016-08-25 12:41:15 -0400490err:
491 cz_dpm_fini(adev);
492 return ret;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400493}
494
495static void cz_dpm_fini(struct amdgpu_device *adev)
496{
497 int i;
498
499 for (i = 0; i < adev->pm.dpm.num_ps; i++)
500 kfree(adev->pm.dpm.ps[i].ps_priv);
501
502 kfree(adev->pm.dpm.ps);
503 kfree(adev->pm.dpm.priv);
504 amdgpu_free_extended_power_table(adev);
505}
506
Alex Deucherf2d52cd2015-07-14 16:16:29 -0400507#define ixSMUSVI_NB_CURRENTVID 0xD8230044
508#define CURRENT_NB_VID_MASK 0xff000000
509#define CURRENT_NB_VID__SHIFT 24
510#define ixSMUSVI_GFX_CURRENTVID 0xD8230048
511#define CURRENT_GFX_VID_MASK 0xff000000
512#define CURRENT_GFX_VID__SHIFT 24
513
Alex Deucheraaa36a92015-04-20 17:31:14 -0400514static void
515cz_dpm_debugfs_print_current_performance_level(struct amdgpu_device *adev,
516 struct seq_file *m)
517{
Alex Deucheracc6a1a2015-07-22 12:03:50 -0400518 struct cz_power_info *pi = cz_get_pi(adev);
Alex Deucheraaa36a92015-04-20 17:31:14 -0400519 struct amdgpu_clock_voltage_dependency_table *table =
520 &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
Alex Deucheracc6a1a2015-07-22 12:03:50 -0400521 struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
522 &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
523 struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
524 &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
525 u32 sclk_index = REG_GET_FIELD(RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX),
526 TARGET_AND_CURRENT_PROFILE_INDEX, CURR_SCLK_INDEX);
527 u32 uvd_index = REG_GET_FIELD(RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX_2),
528 TARGET_AND_CURRENT_PROFILE_INDEX_2, CURR_UVD_INDEX);
529 u32 vce_index = REG_GET_FIELD(RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX_2),
530 TARGET_AND_CURRENT_PROFILE_INDEX_2, CURR_VCE_INDEX);
531 u32 sclk, vclk, dclk, ecclk, tmp;
Alex Deucherf2d52cd2015-07-14 16:16:29 -0400532 u16 vddnb, vddgfx;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400533
Alex Deucheracc6a1a2015-07-22 12:03:50 -0400534 if (sclk_index >= NUM_SCLK_LEVELS) {
535 seq_printf(m, "invalid sclk dpm profile %d\n", sclk_index);
Alex Deucheraaa36a92015-04-20 17:31:14 -0400536 } else {
Alex Deucheracc6a1a2015-07-22 12:03:50 -0400537 sclk = table->entries[sclk_index].clk;
538 seq_printf(m, "%u sclk: %u\n", sclk_index, sclk);
539 }
540
541 tmp = (RREG32_SMC(ixSMUSVI_NB_CURRENTVID) &
542 CURRENT_NB_VID_MASK) >> CURRENT_NB_VID__SHIFT;
543 vddnb = cz_convert_8bit_index_to_voltage(adev, (u16)tmp);
544 tmp = (RREG32_SMC(ixSMUSVI_GFX_CURRENTVID) &
545 CURRENT_GFX_VID_MASK) >> CURRENT_GFX_VID__SHIFT;
546 vddgfx = cz_convert_8bit_index_to_voltage(adev, (u16)tmp);
547 seq_printf(m, "vddnb: %u vddgfx: %u\n", vddnb, vddgfx);
548
549 seq_printf(m, "uvd %sabled\n", pi->uvd_power_gated ? "dis" : "en");
550 if (!pi->uvd_power_gated) {
551 if (uvd_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
552 seq_printf(m, "invalid uvd dpm level %d\n", uvd_index);
553 } else {
554 vclk = uvd_table->entries[uvd_index].vclk;
555 dclk = uvd_table->entries[uvd_index].dclk;
556 seq_printf(m, "%u uvd vclk: %u dclk: %u\n", uvd_index, vclk, dclk);
557 }
558 }
559
560 seq_printf(m, "vce %sabled\n", pi->vce_power_gated ? "dis" : "en");
561 if (!pi->vce_power_gated) {
562 if (vce_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
563 seq_printf(m, "invalid vce dpm level %d\n", vce_index);
564 } else {
565 ecclk = vce_table->entries[vce_index].ecclk;
566 seq_printf(m, "%u vce ecclk: %u\n", vce_index, ecclk);
567 }
Alex Deucheraaa36a92015-04-20 17:31:14 -0400568 }
569}
570
571static void cz_dpm_print_power_state(struct amdgpu_device *adev,
572 struct amdgpu_ps *rps)
573{
574 int i;
575 struct cz_ps *ps = cz_get_ps(rps);
576
577 amdgpu_dpm_print_class_info(rps->class, rps->class2);
578 amdgpu_dpm_print_cap_info(rps->caps);
579
580 DRM_INFO("\tuvd vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
581 for (i = 0; i < ps->num_levels; i++) {
582 struct cz_pl *pl = &ps->levels[i];
583
584 DRM_INFO("\t\tpower level %d sclk: %u vddc: %u\n",
585 i, pl->sclk,
586 cz_convert_8bit_index_to_voltage(adev, pl->vddc_index));
587 }
588
589 amdgpu_dpm_print_ps_status(adev, rps);
590}
591
592static void cz_dpm_set_funcs(struct amdgpu_device *adev);
593
yanyang15fc3aee2015-05-22 14:39:35 -0400594static int cz_dpm_early_init(void *handle)
Alex Deucheraaa36a92015-04-20 17:31:14 -0400595{
yanyang15fc3aee2015-05-22 14:39:35 -0400596 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
597
Alex Deucheraaa36a92015-04-20 17:31:14 -0400598 cz_dpm_set_funcs(adev);
599
600 return 0;
601}
602
Sonny Jiang564ea792015-05-12 16:13:35 -0400603
yanyang15fc3aee2015-05-22 14:39:35 -0400604static int cz_dpm_late_init(void *handle)
Sonny Jiang564ea792015-05-12 16:13:35 -0400605{
yanyang15fc3aee2015-05-22 14:39:35 -0400606 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
607
Sonny Jiang6d8db6ce2015-06-10 13:46:36 -0400608 if (amdgpu_dpm) {
Alex Deucherfa022a92015-09-30 17:05:40 -0400609 int ret;
610 /* init the sysfs and debugfs files late */
611 ret = amdgpu_pm_sysfs_init(adev);
612 if (ret)
613 return ret;
614
Sonny Jiang6d8db6ce2015-06-10 13:46:36 -0400615 /* powerdown unused blocks for now */
616 cz_dpm_powergate_uvd(adev, true);
617 cz_dpm_powergate_vce(adev, true);
618 }
Sonny Jiang564ea792015-05-12 16:13:35 -0400619
620 return 0;
621}
622
yanyang15fc3aee2015-05-22 14:39:35 -0400623static int cz_dpm_sw_init(void *handle)
Alex Deucheraaa36a92015-04-20 17:31:14 -0400624{
yanyang15fc3aee2015-05-22 14:39:35 -0400625 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400626 int ret = 0;
627 /* fix me to add thermal support TODO */
628
629 /* default to balanced state */
630 adev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
631 adev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
632 adev->pm.dpm.forced_level = AMDGPU_DPM_FORCED_LEVEL_AUTO;
633 adev->pm.default_sclk = adev->clock.default_sclk;
634 adev->pm.default_mclk = adev->clock.default_mclk;
635 adev->pm.current_sclk = adev->clock.default_sclk;
636 adev->pm.current_mclk = adev->clock.default_mclk;
637 adev->pm.int_thermal_type = THERMAL_TYPE_NONE;
638
639 if (amdgpu_dpm == 0)
640 return 0;
641
642 mutex_lock(&adev->pm.mutex);
643 ret = cz_dpm_init(adev);
644 if (ret)
645 goto dpm_init_failed;
646
647 adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps = adev->pm.dpm.boot_ps;
648 if (amdgpu_dpm == 1)
649 amdgpu_pm_print_power_states(adev);
650
Alex Deucheraaa36a92015-04-20 17:31:14 -0400651 mutex_unlock(&adev->pm.mutex);
652 DRM_INFO("amdgpu: dpm initialized\n");
653
654 return 0;
655
656dpm_init_failed:
657 cz_dpm_fini(adev);
658 mutex_unlock(&adev->pm.mutex);
659 DRM_ERROR("amdgpu: dpm initialization failed\n");
660
661 return ret;
662}
663
yanyang15fc3aee2015-05-22 14:39:35 -0400664static int cz_dpm_sw_fini(void *handle)
Alex Deucheraaa36a92015-04-20 17:31:14 -0400665{
yanyang15fc3aee2015-05-22 14:39:35 -0400666 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
667
Alex Deucheraaa36a92015-04-20 17:31:14 -0400668 mutex_lock(&adev->pm.mutex);
669 amdgpu_pm_sysfs_fini(adev);
670 cz_dpm_fini(adev);
671 mutex_unlock(&adev->pm.mutex);
672
673 return 0;
674}
675
676static void cz_reset_ap_mask(struct amdgpu_device *adev)
677{
678 struct cz_power_info *pi = cz_get_pi(adev);
679
680 pi->active_process_mask = 0;
681
682}
683
684static int cz_dpm_download_pptable_from_smu(struct amdgpu_device *adev,
685 void **table)
686{
687 int ret = 0;
688
689 ret = cz_smu_download_pptable(adev, table);
690
691 return ret;
692}
693
694static int cz_dpm_upload_pptable_to_smu(struct amdgpu_device *adev)
695{
696 struct cz_power_info *pi = cz_get_pi(adev);
697 struct SMU8_Fusion_ClkTable *clock_table;
698 struct atom_clock_dividers dividers;
699 void *table = NULL;
700 uint8_t i = 0;
701 int ret = 0;
702
703 struct amdgpu_clock_voltage_dependency_table *vddc_table =
704 &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
705 struct amdgpu_clock_voltage_dependency_table *vddgfx_table =
706 &adev->pm.dpm.dyn_state.vddgfx_dependency_on_sclk;
707 struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
708 &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
709 struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
710 &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
711 struct amdgpu_clock_voltage_dependency_table *acp_table =
712 &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
713
714 if (!pi->need_pptable_upload)
715 return 0;
716
717 ret = cz_dpm_download_pptable_from_smu(adev, &table);
718 if (ret) {
719 DRM_ERROR("amdgpu: Failed to get power play table from SMU!\n");
720 return -EINVAL;
721 }
722
723 clock_table = (struct SMU8_Fusion_ClkTable *)table;
724 /* patch clock table */
725 if (vddc_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
726 vddgfx_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
727 uvd_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
728 vce_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
729 acp_table->count > CZ_MAX_HARDWARE_POWERLEVELS) {
730 DRM_ERROR("amdgpu: Invalid Clock Voltage Dependency Table!\n");
731 return -EINVAL;
732 }
733
734 for (i = 0; i < CZ_MAX_HARDWARE_POWERLEVELS; i++) {
735
736 /* vddc sclk */
737 clock_table->SclkBreakdownTable.ClkLevel[i].GnbVid =
738 (i < vddc_table->count) ? (uint8_t)vddc_table->entries[i].v : 0;
739 clock_table->SclkBreakdownTable.ClkLevel[i].Frequency =
740 (i < vddc_table->count) ? vddc_table->entries[i].clk : 0;
741 ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
742 clock_table->SclkBreakdownTable.ClkLevel[i].Frequency,
743 false, &dividers);
744 if (ret)
745 return ret;
746 clock_table->SclkBreakdownTable.ClkLevel[i].DfsDid =
747 (uint8_t)dividers.post_divider;
748
749 /* vddgfx sclk */
750 clock_table->SclkBreakdownTable.ClkLevel[i].GfxVid =
751 (i < vddgfx_table->count) ? (uint8_t)vddgfx_table->entries[i].v : 0;
752
753 /* acp breakdown */
754 clock_table->AclkBreakdownTable.ClkLevel[i].GfxVid =
755 (i < acp_table->count) ? (uint8_t)acp_table->entries[i].v : 0;
756 clock_table->AclkBreakdownTable.ClkLevel[i].Frequency =
757 (i < acp_table->count) ? acp_table->entries[i].clk : 0;
758 ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
759 clock_table->SclkBreakdownTable.ClkLevel[i].Frequency,
760 false, &dividers);
761 if (ret)
762 return ret;
763 clock_table->AclkBreakdownTable.ClkLevel[i].DfsDid =
764 (uint8_t)dividers.post_divider;
765
766 /* uvd breakdown */
767 clock_table->VclkBreakdownTable.ClkLevel[i].GfxVid =
768 (i < uvd_table->count) ? (uint8_t)uvd_table->entries[i].v : 0;
769 clock_table->VclkBreakdownTable.ClkLevel[i].Frequency =
770 (i < uvd_table->count) ? uvd_table->entries[i].vclk : 0;
771 ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
772 clock_table->VclkBreakdownTable.ClkLevel[i].Frequency,
773 false, &dividers);
774 if (ret)
775 return ret;
776 clock_table->VclkBreakdownTable.ClkLevel[i].DfsDid =
777 (uint8_t)dividers.post_divider;
778
779 clock_table->DclkBreakdownTable.ClkLevel[i].GfxVid =
780 (i < uvd_table->count) ? (uint8_t)uvd_table->entries[i].v : 0;
781 clock_table->DclkBreakdownTable.ClkLevel[i].Frequency =
782 (i < uvd_table->count) ? uvd_table->entries[i].dclk : 0;
783 ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
784 clock_table->DclkBreakdownTable.ClkLevel[i].Frequency,
785 false, &dividers);
786 if (ret)
787 return ret;
788 clock_table->DclkBreakdownTable.ClkLevel[i].DfsDid =
789 (uint8_t)dividers.post_divider;
790
791 /* vce breakdown */
792 clock_table->EclkBreakdownTable.ClkLevel[i].GfxVid =
793 (i < vce_table->count) ? (uint8_t)vce_table->entries[i].v : 0;
794 clock_table->EclkBreakdownTable.ClkLevel[i].Frequency =
795 (i < vce_table->count) ? vce_table->entries[i].ecclk : 0;
796 ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
797 clock_table->EclkBreakdownTable.ClkLevel[i].Frequency,
798 false, &dividers);
799 if (ret)
800 return ret;
801 clock_table->EclkBreakdownTable.ClkLevel[i].DfsDid =
802 (uint8_t)dividers.post_divider;
803 }
804
805 /* its time to upload to SMU */
806 ret = cz_smu_upload_pptable(adev);
807 if (ret) {
808 DRM_ERROR("amdgpu: Failed to put power play table to SMU!\n");
809 return ret;
810 }
811
812 return 0;
813}
814
815static void cz_init_sclk_limit(struct amdgpu_device *adev)
816{
817 struct cz_power_info *pi = cz_get_pi(adev);
818 struct amdgpu_clock_voltage_dependency_table *table =
819 &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
820 uint32_t clock = 0, level;
821
822 if (!table || !table->count) {
823 DRM_ERROR("Invalid Voltage Dependency table.\n");
824 return;
825 }
826
827 pi->sclk_dpm.soft_min_clk = 0;
828 pi->sclk_dpm.hard_min_clk = 0;
829 cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxSclkLevel);
830 level = cz_get_argument(adev);
831 if (level < table->count)
832 clock = table->entries[level].clk;
833 else {
834 DRM_ERROR("Invalid SLCK Voltage Dependency table entry.\n");
835 clock = table->entries[table->count - 1].clk;
836 }
837
838 pi->sclk_dpm.soft_max_clk = clock;
839 pi->sclk_dpm.hard_max_clk = clock;
840
841}
842
843static void cz_init_uvd_limit(struct amdgpu_device *adev)
844{
845 struct cz_power_info *pi = cz_get_pi(adev);
846 struct amdgpu_uvd_clock_voltage_dependency_table *table =
847 &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
848 uint32_t clock = 0, level;
849
850 if (!table || !table->count) {
851 DRM_ERROR("Invalid Voltage Dependency table.\n");
852 return;
853 }
854
855 pi->uvd_dpm.soft_min_clk = 0;
856 pi->uvd_dpm.hard_min_clk = 0;
857 cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxUvdLevel);
858 level = cz_get_argument(adev);
859 if (level < table->count)
860 clock = table->entries[level].vclk;
861 else {
862 DRM_ERROR("Invalid UVD Voltage Dependency table entry.\n");
863 clock = table->entries[table->count - 1].vclk;
864 }
865
866 pi->uvd_dpm.soft_max_clk = clock;
867 pi->uvd_dpm.hard_max_clk = clock;
868
869}
870
871static void cz_init_vce_limit(struct amdgpu_device *adev)
872{
873 struct cz_power_info *pi = cz_get_pi(adev);
874 struct amdgpu_vce_clock_voltage_dependency_table *table =
875 &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
876 uint32_t clock = 0, level;
877
878 if (!table || !table->count) {
879 DRM_ERROR("Invalid Voltage Dependency table.\n");
880 return;
881 }
882
Sonny Jiangb7a077692015-05-28 15:47:53 -0400883 pi->vce_dpm.soft_min_clk = table->entries[0].ecclk;
884 pi->vce_dpm.hard_min_clk = table->entries[0].ecclk;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400885 cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxEclkLevel);
886 level = cz_get_argument(adev);
887 if (level < table->count)
Sonny Jiangb7a077692015-05-28 15:47:53 -0400888 clock = table->entries[level].ecclk;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400889 else {
890 /* future BIOS would fix this error */
891 DRM_ERROR("Invalid VCE Voltage Dependency table entry.\n");
Sonny Jiangb7a077692015-05-28 15:47:53 -0400892 clock = table->entries[table->count - 1].ecclk;
Alex Deucheraaa36a92015-04-20 17:31:14 -0400893 }
894
895 pi->vce_dpm.soft_max_clk = clock;
896 pi->vce_dpm.hard_max_clk = clock;
897
898}
899
900static void cz_init_acp_limit(struct amdgpu_device *adev)
901{
902 struct cz_power_info *pi = cz_get_pi(adev);
903 struct amdgpu_clock_voltage_dependency_table *table =
904 &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
905 uint32_t clock = 0, level;
906
907 if (!table || !table->count) {
908 DRM_ERROR("Invalid Voltage Dependency table.\n");
909 return;
910 }
911
912 pi->acp_dpm.soft_min_clk = 0;
913 pi->acp_dpm.hard_min_clk = 0;
914 cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxAclkLevel);
915 level = cz_get_argument(adev);
916 if (level < table->count)
917 clock = table->entries[level].clk;
918 else {
919 DRM_ERROR("Invalid ACP Voltage Dependency table entry.\n");
920 clock = table->entries[table->count - 1].clk;
921 }
922
923 pi->acp_dpm.soft_max_clk = clock;
924 pi->acp_dpm.hard_max_clk = clock;
925
926}
927
928static void cz_init_pg_state(struct amdgpu_device *adev)
929{
930 struct cz_power_info *pi = cz_get_pi(adev);
931
932 pi->uvd_power_gated = false;
933 pi->vce_power_gated = false;
934 pi->acp_power_gated = false;
935
936}
937
938static void cz_init_sclk_threshold(struct amdgpu_device *adev)
939{
940 struct cz_power_info *pi = cz_get_pi(adev);
941
942 pi->low_sclk_interrupt_threshold = 0;
943
944}
945
946static void cz_dpm_setup_asic(struct amdgpu_device *adev)
947{
948 cz_reset_ap_mask(adev);
949 cz_dpm_upload_pptable_to_smu(adev);
950 cz_init_sclk_limit(adev);
951 cz_init_uvd_limit(adev);
952 cz_init_vce_limit(adev);
953 cz_init_acp_limit(adev);
954 cz_init_pg_state(adev);
955 cz_init_sclk_threshold(adev);
956
957}
958
959static bool cz_check_smu_feature(struct amdgpu_device *adev,
960 uint32_t feature)
961{
962 uint32_t smu_feature = 0;
963 int ret;
964
965 ret = cz_send_msg_to_smc_with_parameter(adev,
966 PPSMC_MSG_GetFeatureStatus, 0);
967 if (ret) {
968 DRM_ERROR("Failed to get SMU features from SMC.\n");
969 return false;
970 } else {
971 smu_feature = cz_get_argument(adev);
972 if (feature & smu_feature)
973 return true;
974 }
975
976 return false;
977}
978
979static bool cz_check_for_dpm_enabled(struct amdgpu_device *adev)
980{
981 if (cz_check_smu_feature(adev,
982 SMU_EnabledFeatureScoreboard_SclkDpmOn))
983 return true;
984
985 return false;
986}
987
988static void cz_program_voting_clients(struct amdgpu_device *adev)
989{
990 WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, PPCZ_VOTINGRIGHTSCLIENTS_DFLT0);
991}
992
993static void cz_clear_voting_clients(struct amdgpu_device *adev)
994{
995 WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, 0);
996}
997
998static int cz_start_dpm(struct amdgpu_device *adev)
999{
1000 int ret = 0;
1001
1002 if (amdgpu_dpm) {
1003 ret = cz_send_msg_to_smc_with_parameter(adev,
1004 PPSMC_MSG_EnableAllSmuFeatures, SCLK_DPM_MASK);
1005 if (ret) {
1006 DRM_ERROR("SMU feature: SCLK_DPM enable failed\n");
1007 return -EINVAL;
1008 }
1009 }
1010
1011 return 0;
1012}
1013
1014static int cz_stop_dpm(struct amdgpu_device *adev)
1015{
1016 int ret = 0;
1017
1018 if (amdgpu_dpm && adev->pm.dpm_enabled) {
1019 ret = cz_send_msg_to_smc_with_parameter(adev,
1020 PPSMC_MSG_DisableAllSmuFeatures, SCLK_DPM_MASK);
1021 if (ret) {
1022 DRM_ERROR("SMU feature: SCLK_DPM disable failed\n");
1023 return -EINVAL;
1024 }
1025 }
1026
1027 return 0;
1028}
1029
1030static uint32_t cz_get_sclk_level(struct amdgpu_device *adev,
1031 uint32_t clock, uint16_t msg)
1032{
1033 int i = 0;
1034 struct amdgpu_clock_voltage_dependency_table *table =
1035 &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
1036
1037 switch (msg) {
1038 case PPSMC_MSG_SetSclkSoftMin:
1039 case PPSMC_MSG_SetSclkHardMin:
1040 for (i = 0; i < table->count; i++)
1041 if (clock <= table->entries[i].clk)
1042 break;
1043 if (i == table->count)
1044 i = table->count - 1;
1045 break;
1046 case PPSMC_MSG_SetSclkSoftMax:
1047 case PPSMC_MSG_SetSclkHardMax:
1048 for (i = table->count - 1; i >= 0; i--)
1049 if (clock >= table->entries[i].clk)
1050 break;
1051 if (i < 0)
1052 i = 0;
1053 break;
1054 default:
1055 break;
1056 }
1057
1058 return i;
1059}
1060
Sonny Jiangb7a077692015-05-28 15:47:53 -04001061static uint32_t cz_get_eclk_level(struct amdgpu_device *adev,
1062 uint32_t clock, uint16_t msg)
1063{
1064 int i = 0;
1065 struct amdgpu_vce_clock_voltage_dependency_table *table =
1066 &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1067
1068 if (table->count == 0)
1069 return 0;
1070
1071 switch (msg) {
1072 case PPSMC_MSG_SetEclkSoftMin:
1073 case PPSMC_MSG_SetEclkHardMin:
1074 for (i = 0; i < table->count-1; i++)
1075 if (clock <= table->entries[i].ecclk)
1076 break;
1077 break;
1078 case PPSMC_MSG_SetEclkSoftMax:
1079 case PPSMC_MSG_SetEclkHardMax:
1080 for (i = table->count - 1; i > 0; i--)
1081 if (clock >= table->entries[i].ecclk)
1082 break;
1083 break;
1084 default:
1085 break;
1086 }
1087
1088 return i;
1089}
1090
Alex Deucherd83b1e812015-12-18 11:06:42 -05001091static uint32_t cz_get_uvd_level(struct amdgpu_device *adev,
1092 uint32_t clock, uint16_t msg)
1093{
1094 int i = 0;
1095 struct amdgpu_uvd_clock_voltage_dependency_table *table =
1096 &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
1097
1098 switch (msg) {
1099 case PPSMC_MSG_SetUvdSoftMin:
1100 case PPSMC_MSG_SetUvdHardMin:
1101 for (i = 0; i < table->count; i++)
1102 if (clock <= table->entries[i].vclk)
1103 break;
1104 if (i == table->count)
1105 i = table->count - 1;
1106 break;
1107 case PPSMC_MSG_SetUvdSoftMax:
1108 case PPSMC_MSG_SetUvdHardMax:
1109 for (i = table->count - 1; i >= 0; i--)
1110 if (clock >= table->entries[i].vclk)
1111 break;
1112 if (i < 0)
1113 i = 0;
1114 break;
1115 default:
1116 break;
1117 }
1118
1119 return i;
1120}
1121
Alex Deucheraaa36a92015-04-20 17:31:14 -04001122static int cz_program_bootup_state(struct amdgpu_device *adev)
1123{
1124 struct cz_power_info *pi = cz_get_pi(adev);
1125 uint32_t soft_min_clk = 0;
1126 uint32_t soft_max_clk = 0;
1127 int ret = 0;
1128
1129 pi->sclk_dpm.soft_min_clk = pi->sys_info.bootup_sclk;
1130 pi->sclk_dpm.soft_max_clk = pi->sys_info.bootup_sclk;
1131
1132 soft_min_clk = cz_get_sclk_level(adev,
1133 pi->sclk_dpm.soft_min_clk,
1134 PPSMC_MSG_SetSclkSoftMin);
1135 soft_max_clk = cz_get_sclk_level(adev,
1136 pi->sclk_dpm.soft_max_clk,
1137 PPSMC_MSG_SetSclkSoftMax);
1138
1139 ret = cz_send_msg_to_smc_with_parameter(adev,
1140 PPSMC_MSG_SetSclkSoftMin, soft_min_clk);
1141 if (ret)
1142 return -EINVAL;
1143
1144 ret = cz_send_msg_to_smc_with_parameter(adev,
1145 PPSMC_MSG_SetSclkSoftMax, soft_max_clk);
1146 if (ret)
1147 return -EINVAL;
1148
1149 return 0;
1150}
1151
1152/* TODO */
1153static int cz_disable_cgpg(struct amdgpu_device *adev)
1154{
1155 return 0;
1156}
1157
1158/* TODO */
1159static int cz_enable_cgpg(struct amdgpu_device *adev)
1160{
1161 return 0;
1162}
1163
1164/* TODO */
1165static int cz_program_pt_config_registers(struct amdgpu_device *adev)
1166{
1167 return 0;
1168}
1169
1170static void cz_do_enable_didt(struct amdgpu_device *adev, bool enable)
1171{
1172 struct cz_power_info *pi = cz_get_pi(adev);
1173 uint32_t reg = 0;
1174
1175 if (pi->caps_sq_ramping) {
1176 reg = RREG32_DIDT(ixDIDT_SQ_CTRL0);
1177 if (enable)
1178 reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 1);
1179 else
1180 reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 0);
1181 WREG32_DIDT(ixDIDT_SQ_CTRL0, reg);
1182 }
1183 if (pi->caps_db_ramping) {
1184 reg = RREG32_DIDT(ixDIDT_DB_CTRL0);
1185 if (enable)
1186 reg = REG_SET_FIELD(reg, DIDT_DB_CTRL0, DIDT_CTRL_EN, 1);
1187 else
1188 reg = REG_SET_FIELD(reg, DIDT_DB_CTRL0, DIDT_CTRL_EN, 0);
1189 WREG32_DIDT(ixDIDT_DB_CTRL0, reg);
1190 }
1191 if (pi->caps_td_ramping) {
1192 reg = RREG32_DIDT(ixDIDT_TD_CTRL0);
1193 if (enable)
1194 reg = REG_SET_FIELD(reg, DIDT_TD_CTRL0, DIDT_CTRL_EN, 1);
1195 else
1196 reg = REG_SET_FIELD(reg, DIDT_TD_CTRL0, DIDT_CTRL_EN, 0);
1197 WREG32_DIDT(ixDIDT_TD_CTRL0, reg);
1198 }
1199 if (pi->caps_tcp_ramping) {
1200 reg = RREG32_DIDT(ixDIDT_TCP_CTRL0);
1201 if (enable)
1202 reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 1);
1203 else
1204 reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 0);
1205 WREG32_DIDT(ixDIDT_TCP_CTRL0, reg);
1206 }
1207
1208}
1209
1210static int cz_enable_didt(struct amdgpu_device *adev, bool enable)
1211{
1212 struct cz_power_info *pi = cz_get_pi(adev);
1213 int ret;
1214
1215 if (pi->caps_sq_ramping || pi->caps_db_ramping ||
1216 pi->caps_td_ramping || pi->caps_tcp_ramping) {
1217 if (adev->gfx.gfx_current_status != AMDGPU_GFX_SAFE_MODE) {
1218 ret = cz_disable_cgpg(adev);
1219 if (ret) {
1220 DRM_ERROR("Pre Di/Dt disable cg/pg failed\n");
1221 return -EINVAL;
1222 }
1223 adev->gfx.gfx_current_status = AMDGPU_GFX_SAFE_MODE;
1224 }
1225
1226 ret = cz_program_pt_config_registers(adev);
1227 if (ret) {
1228 DRM_ERROR("Di/Dt config failed\n");
1229 return -EINVAL;
1230 }
1231 cz_do_enable_didt(adev, enable);
1232
1233 if (adev->gfx.gfx_current_status == AMDGPU_GFX_SAFE_MODE) {
1234 ret = cz_enable_cgpg(adev);
1235 if (ret) {
1236 DRM_ERROR("Post Di/Dt enable cg/pg failed\n");
1237 return -EINVAL;
1238 }
1239 adev->gfx.gfx_current_status = AMDGPU_GFX_NORMAL_MODE;
1240 }
1241 }
1242
1243 return 0;
1244}
1245
1246/* TODO */
1247static void cz_reset_acp_boot_level(struct amdgpu_device *adev)
1248{
1249}
1250
1251static void cz_update_current_ps(struct amdgpu_device *adev,
1252 struct amdgpu_ps *rps)
1253{
1254 struct cz_power_info *pi = cz_get_pi(adev);
1255 struct cz_ps *ps = cz_get_ps(rps);
1256
1257 pi->current_ps = *ps;
1258 pi->current_rps = *rps;
1259 pi->current_rps.ps_priv = ps;
1260
1261}
1262
1263static void cz_update_requested_ps(struct amdgpu_device *adev,
1264 struct amdgpu_ps *rps)
1265{
1266 struct cz_power_info *pi = cz_get_pi(adev);
1267 struct cz_ps *ps = cz_get_ps(rps);
1268
1269 pi->requested_ps = *ps;
1270 pi->requested_rps = *rps;
1271 pi->requested_rps.ps_priv = ps;
1272
1273}
1274
1275/* PP arbiter support needed TODO */
1276static void cz_apply_state_adjust_rules(struct amdgpu_device *adev,
1277 struct amdgpu_ps *new_rps,
1278 struct amdgpu_ps *old_rps)
1279{
1280 struct cz_ps *ps = cz_get_ps(new_rps);
1281 struct cz_power_info *pi = cz_get_pi(adev);
1282 struct amdgpu_clock_and_voltage_limits *limits =
1283 &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
1284 /* 10kHz memory clock */
1285 uint32_t mclk = 0;
1286
1287 ps->force_high = false;
1288 ps->need_dfs_bypass = true;
1289 pi->video_start = new_rps->dclk || new_rps->vclk ||
1290 new_rps->evclk || new_rps->ecclk;
1291
1292 if ((new_rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) ==
1293 ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
1294 pi->battery_state = true;
1295 else
1296 pi->battery_state = false;
1297
1298 if (pi->caps_stable_power_state)
1299 mclk = limits->mclk;
1300
1301 if (mclk > pi->sys_info.nbp_memory_clock[CZ_NUM_NBPMEMORY_CLOCK - 1])
1302 ps->force_high = true;
1303
1304}
1305
1306static int cz_dpm_enable(struct amdgpu_device *adev)
1307{
Samuel Li7a753c32015-10-08 16:28:41 -04001308 const char *chip_name;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001309 int ret = 0;
1310
1311 /* renable will hang up SMU, so check first */
1312 if (cz_check_for_dpm_enabled(adev))
1313 return -EINVAL;
1314
1315 cz_program_voting_clients(adev);
1316
Samuel Li7a753c32015-10-08 16:28:41 -04001317 switch (adev->asic_type) {
1318 case CHIP_CARRIZO:
1319 chip_name = "carrizo";
1320 break;
1321 case CHIP_STONEY:
1322 chip_name = "stoney";
1323 break;
1324 default:
1325 BUG();
1326 }
1327
1328
Alex Deucheraaa36a92015-04-20 17:31:14 -04001329 ret = cz_start_dpm(adev);
1330 if (ret) {
Samuel Li7a753c32015-10-08 16:28:41 -04001331 DRM_ERROR("%s DPM enable failed\n", chip_name);
Alex Deucheraaa36a92015-04-20 17:31:14 -04001332 return -EINVAL;
1333 }
1334
1335 ret = cz_program_bootup_state(adev);
1336 if (ret) {
Samuel Li7a753c32015-10-08 16:28:41 -04001337 DRM_ERROR("%s bootup state program failed\n", chip_name);
Alex Deucheraaa36a92015-04-20 17:31:14 -04001338 return -EINVAL;
1339 }
1340
1341 ret = cz_enable_didt(adev, true);
1342 if (ret) {
Samuel Li7a753c32015-10-08 16:28:41 -04001343 DRM_ERROR("%s enable di/dt failed\n", chip_name);
Alex Deucheraaa36a92015-04-20 17:31:14 -04001344 return -EINVAL;
1345 }
1346
1347 cz_reset_acp_boot_level(adev);
1348
1349 cz_update_current_ps(adev, adev->pm.dpm.boot_ps);
1350
1351 return 0;
1352}
1353
yanyang15fc3aee2015-05-22 14:39:35 -04001354static int cz_dpm_hw_init(void *handle)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001355{
yanyang15fc3aee2015-05-22 14:39:35 -04001356 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Sonny Jiang46651cc2015-04-30 17:12:14 -04001357 int ret = 0;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001358
1359 mutex_lock(&adev->pm.mutex);
1360
Alex Deucher05188312015-06-09 17:32:53 -04001361 /* smu init only needs to be called at startup, not resume.
1362 * It should be in sw_init, but requires the fw info gathered
1363 * in sw_init from other IP modules.
1364 */
Alex Deucheraaa36a92015-04-20 17:31:14 -04001365 ret = cz_smu_init(adev);
1366 if (ret) {
1367 DRM_ERROR("amdgpu: smc initialization failed\n");
1368 mutex_unlock(&adev->pm.mutex);
1369 return ret;
1370 }
1371
1372 /* do the actual fw loading */
1373 ret = cz_smu_start(adev);
1374 if (ret) {
1375 DRM_ERROR("amdgpu: smc start failed\n");
1376 mutex_unlock(&adev->pm.mutex);
1377 return ret;
1378 }
1379
Sonny Jiang46651cc2015-04-30 17:12:14 -04001380 if (!amdgpu_dpm) {
1381 adev->pm.dpm_enabled = false;
1382 mutex_unlock(&adev->pm.mutex);
1383 return ret;
1384 }
1385
Alex Deucheraaa36a92015-04-20 17:31:14 -04001386 /* cz dpm setup asic */
1387 cz_dpm_setup_asic(adev);
1388
1389 /* cz dpm enable */
1390 ret = cz_dpm_enable(adev);
1391 if (ret)
1392 adev->pm.dpm_enabled = false;
1393 else
1394 adev->pm.dpm_enabled = true;
1395
1396 mutex_unlock(&adev->pm.mutex);
1397
1398 return 0;
1399}
1400
1401static int cz_dpm_disable(struct amdgpu_device *adev)
1402{
1403 int ret = 0;
1404
1405 if (!cz_check_for_dpm_enabled(adev))
1406 return -EINVAL;
1407
1408 ret = cz_enable_didt(adev, false);
1409 if (ret) {
Samuel Li7a753c32015-10-08 16:28:41 -04001410 DRM_ERROR("disable di/dt failed\n");
Alex Deucheraaa36a92015-04-20 17:31:14 -04001411 return -EINVAL;
1412 }
1413
Sonny Jiang564ea792015-05-12 16:13:35 -04001414 /* powerup blocks */
1415 cz_dpm_powergate_uvd(adev, false);
Sonny Jiangb7a077692015-05-28 15:47:53 -04001416 cz_dpm_powergate_vce(adev, false);
Sonny Jiang564ea792015-05-12 16:13:35 -04001417
Alex Deucheraaa36a92015-04-20 17:31:14 -04001418 cz_clear_voting_clients(adev);
1419 cz_stop_dpm(adev);
1420 cz_update_current_ps(adev, adev->pm.dpm.boot_ps);
1421
1422 return 0;
1423}
1424
yanyang15fc3aee2015-05-22 14:39:35 -04001425static int cz_dpm_hw_fini(void *handle)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001426{
1427 int ret = 0;
yanyang15fc3aee2015-05-22 14:39:35 -04001428 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001429
1430 mutex_lock(&adev->pm.mutex);
1431
Alex Deucher05188312015-06-09 17:32:53 -04001432 /* smu fini only needs to be called at teardown, not suspend.
1433 * It should be in sw_fini, but we put it here for symmetry
1434 * with smu init.
1435 */
Alex Deucheraaa36a92015-04-20 17:31:14 -04001436 cz_smu_fini(adev);
1437
1438 if (adev->pm.dpm_enabled) {
1439 ret = cz_dpm_disable(adev);
Alex Deucheraaa36a92015-04-20 17:31:14 -04001440
1441 adev->pm.dpm.current_ps =
1442 adev->pm.dpm.requested_ps =
1443 adev->pm.dpm.boot_ps;
1444 }
1445
1446 adev->pm.dpm_enabled = false;
1447
1448 mutex_unlock(&adev->pm.mutex);
1449
Alex Deucher10457452015-04-30 11:42:54 -04001450 return ret;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001451}
1452
yanyang15fc3aee2015-05-22 14:39:35 -04001453static int cz_dpm_suspend(void *handle)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001454{
1455 int ret = 0;
yanyang15fc3aee2015-05-22 14:39:35 -04001456 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001457
1458 if (adev->pm.dpm_enabled) {
1459 mutex_lock(&adev->pm.mutex);
1460
1461 ret = cz_dpm_disable(adev);
Alex Deucheraaa36a92015-04-20 17:31:14 -04001462
1463 adev->pm.dpm.current_ps =
1464 adev->pm.dpm.requested_ps =
1465 adev->pm.dpm.boot_ps;
1466
1467 mutex_unlock(&adev->pm.mutex);
1468 }
1469
Alex Deucher10457452015-04-30 11:42:54 -04001470 return ret;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001471}
1472
yanyang15fc3aee2015-05-22 14:39:35 -04001473static int cz_dpm_resume(void *handle)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001474{
1475 int ret = 0;
yanyang15fc3aee2015-05-22 14:39:35 -04001476 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001477
1478 mutex_lock(&adev->pm.mutex);
Alex Deucheraaa36a92015-04-20 17:31:14 -04001479
1480 /* do the actual fw loading */
1481 ret = cz_smu_start(adev);
1482 if (ret) {
1483 DRM_ERROR("amdgpu: smc start failed\n");
1484 mutex_unlock(&adev->pm.mutex);
1485 return ret;
1486 }
1487
Sonny Jiang46651cc2015-04-30 17:12:14 -04001488 if (!amdgpu_dpm) {
1489 adev->pm.dpm_enabled = false;
1490 mutex_unlock(&adev->pm.mutex);
1491 return ret;
1492 }
1493
Alex Deucheraaa36a92015-04-20 17:31:14 -04001494 /* cz dpm setup asic */
1495 cz_dpm_setup_asic(adev);
1496
1497 /* cz dpm enable */
1498 ret = cz_dpm_enable(adev);
1499 if (ret)
1500 adev->pm.dpm_enabled = false;
1501 else
1502 adev->pm.dpm_enabled = true;
1503
1504 mutex_unlock(&adev->pm.mutex);
1505 /* upon resume, re-compute the clocks */
1506 if (adev->pm.dpm_enabled)
1507 amdgpu_pm_compute_clocks(adev);
1508
1509 return 0;
1510}
1511
yanyang15fc3aee2015-05-22 14:39:35 -04001512static int cz_dpm_set_clockgating_state(void *handle,
1513 enum amd_clockgating_state state)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001514{
1515 return 0;
1516}
1517
yanyang15fc3aee2015-05-22 14:39:35 -04001518static int cz_dpm_set_powergating_state(void *handle,
1519 enum amd_powergating_state state)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001520{
1521 return 0;
1522}
1523
1524/* borrowed from KV, need future unify */
1525static int cz_dpm_get_temperature(struct amdgpu_device *adev)
1526{
1527 int actual_temp = 0;
1528 uint32_t temp = RREG32_SMC(0xC0300E0C);
1529
1530 if (temp)
1531 actual_temp = 1000 * ((temp / 8) - 49);
1532
1533 return actual_temp;
1534}
1535
1536static int cz_dpm_pre_set_power_state(struct amdgpu_device *adev)
1537{
1538 struct cz_power_info *pi = cz_get_pi(adev);
1539 struct amdgpu_ps requested_ps = *adev->pm.dpm.requested_ps;
1540 struct amdgpu_ps *new_ps = &requested_ps;
1541
1542 cz_update_requested_ps(adev, new_ps);
1543 cz_apply_state_adjust_rules(adev, &pi->requested_rps,
1544 &pi->current_rps);
1545
1546 return 0;
1547}
1548
1549static int cz_dpm_update_sclk_limit(struct amdgpu_device *adev)
1550{
1551 struct cz_power_info *pi = cz_get_pi(adev);
1552 struct amdgpu_clock_and_voltage_limits *limits =
1553 &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
1554 uint32_t clock, stable_ps_clock = 0;
1555
1556 clock = pi->sclk_dpm.soft_min_clk;
1557
1558 if (pi->caps_stable_power_state) {
1559 stable_ps_clock = limits->sclk * 75 / 100;
1560 if (clock < stable_ps_clock)
1561 clock = stable_ps_clock;
1562 }
1563
1564 if (clock != pi->sclk_dpm.soft_min_clk) {
1565 pi->sclk_dpm.soft_min_clk = clock;
1566 cz_send_msg_to_smc_with_parameter(adev,
1567 PPSMC_MSG_SetSclkSoftMin,
1568 cz_get_sclk_level(adev, clock,
1569 PPSMC_MSG_SetSclkSoftMin));
1570 }
1571
1572 if (pi->caps_stable_power_state &&
1573 pi->sclk_dpm.soft_max_clk != clock) {
1574 pi->sclk_dpm.soft_max_clk = clock;
1575 cz_send_msg_to_smc_with_parameter(adev,
1576 PPSMC_MSG_SetSclkSoftMax,
1577 cz_get_sclk_level(adev, clock,
1578 PPSMC_MSG_SetSclkSoftMax));
1579 } else {
1580 cz_send_msg_to_smc_with_parameter(adev,
1581 PPSMC_MSG_SetSclkSoftMax,
1582 cz_get_sclk_level(adev,
1583 pi->sclk_dpm.soft_max_clk,
1584 PPSMC_MSG_SetSclkSoftMax));
1585 }
1586
1587 return 0;
1588}
1589
1590static int cz_dpm_set_deep_sleep_sclk_threshold(struct amdgpu_device *adev)
1591{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001592 struct cz_power_info *pi = cz_get_pi(adev);
1593
1594 if (pi->caps_sclk_ds) {
1595 cz_send_msg_to_smc_with_parameter(adev,
1596 PPSMC_MSG_SetMinDeepSleepSclk,
1597 CZ_MIN_DEEP_SLEEP_SCLK);
1598 }
1599
Muhammad Falak R Wanid05f2c72016-05-17 15:12:44 +05301600 return 0;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001601}
1602
1603/* ?? without dal support, is this still needed in setpowerstate list*/
1604static int cz_dpm_set_watermark_threshold(struct amdgpu_device *adev)
1605{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001606 struct cz_power_info *pi = cz_get_pi(adev);
1607
1608 cz_send_msg_to_smc_with_parameter(adev,
1609 PPSMC_MSG_SetWatermarkFrequency,
1610 pi->sclk_dpm.soft_max_clk);
1611
Muhammad Falak R Wanid05f2c72016-05-17 15:12:44 +05301612 return 0;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001613}
1614
1615static int cz_dpm_enable_nbdpm(struct amdgpu_device *adev)
1616{
1617 int ret = 0;
1618 struct cz_power_info *pi = cz_get_pi(adev);
1619
1620 /* also depend on dal NBPStateDisableRequired */
1621 if (pi->nb_dpm_enabled_by_driver && !pi->nb_dpm_enabled) {
1622 ret = cz_send_msg_to_smc_with_parameter(adev,
1623 PPSMC_MSG_EnableAllSmuFeatures,
1624 NB_DPM_MASK);
1625 if (ret) {
1626 DRM_ERROR("amdgpu: nb dpm enable failed\n");
1627 return ret;
1628 }
1629 pi->nb_dpm_enabled = true;
1630 }
1631
1632 return ret;
1633}
1634
1635static void cz_dpm_nbdpm_lm_pstate_enable(struct amdgpu_device *adev,
1636 bool enable)
1637{
1638 if (enable)
1639 cz_send_msg_to_smc(adev, PPSMC_MSG_EnableLowMemoryPstate);
1640 else
1641 cz_send_msg_to_smc(adev, PPSMC_MSG_DisableLowMemoryPstate);
1642
1643}
1644
1645static int cz_dpm_update_low_memory_pstate(struct amdgpu_device *adev)
1646{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001647 struct cz_power_info *pi = cz_get_pi(adev);
1648 struct cz_ps *ps = &pi->requested_ps;
1649
1650 if (pi->sys_info.nb_dpm_enable) {
1651 if (ps->force_high)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001652 cz_dpm_nbdpm_lm_pstate_enable(adev, false);
Alex Deucher362eda02015-09-03 00:53:24 -04001653 else
1654 cz_dpm_nbdpm_lm_pstate_enable(adev, true);
Alex Deucheraaa36a92015-04-20 17:31:14 -04001655 }
1656
Muhammad Falak R Wanid05f2c72016-05-17 15:12:44 +05301657 return 0;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001658}
1659
1660/* with dpm enabled */
1661static int cz_dpm_set_power_state(struct amdgpu_device *adev)
1662{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001663 cz_dpm_update_sclk_limit(adev);
1664 cz_dpm_set_deep_sleep_sclk_threshold(adev);
1665 cz_dpm_set_watermark_threshold(adev);
1666 cz_dpm_enable_nbdpm(adev);
1667 cz_dpm_update_low_memory_pstate(adev);
1668
Muhammad Falak R Wanid05f2c72016-05-17 15:12:44 +05301669 return 0;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001670}
1671
1672static void cz_dpm_post_set_power_state(struct amdgpu_device *adev)
1673{
1674 struct cz_power_info *pi = cz_get_pi(adev);
1675 struct amdgpu_ps *ps = &pi->requested_rps;
1676
1677 cz_update_current_ps(adev, ps);
1678
1679}
1680
1681static int cz_dpm_force_highest(struct amdgpu_device *adev)
1682{
1683 struct cz_power_info *pi = cz_get_pi(adev);
1684 int ret = 0;
1685
1686 if (pi->sclk_dpm.soft_min_clk != pi->sclk_dpm.soft_max_clk) {
1687 pi->sclk_dpm.soft_min_clk =
1688 pi->sclk_dpm.soft_max_clk;
1689 ret = cz_send_msg_to_smc_with_parameter(adev,
1690 PPSMC_MSG_SetSclkSoftMin,
1691 cz_get_sclk_level(adev,
1692 pi->sclk_dpm.soft_min_clk,
1693 PPSMC_MSG_SetSclkSoftMin));
1694 if (ret)
1695 return ret;
1696 }
1697
1698 return ret;
1699}
1700
1701static int cz_dpm_force_lowest(struct amdgpu_device *adev)
1702{
1703 struct cz_power_info *pi = cz_get_pi(adev);
1704 int ret = 0;
1705
1706 if (pi->sclk_dpm.soft_max_clk != pi->sclk_dpm.soft_min_clk) {
1707 pi->sclk_dpm.soft_max_clk = pi->sclk_dpm.soft_min_clk;
1708 ret = cz_send_msg_to_smc_with_parameter(adev,
1709 PPSMC_MSG_SetSclkSoftMax,
1710 cz_get_sclk_level(adev,
1711 pi->sclk_dpm.soft_max_clk,
1712 PPSMC_MSG_SetSclkSoftMax));
1713 if (ret)
1714 return ret;
1715 }
1716
1717 return ret;
1718}
1719
1720static uint32_t cz_dpm_get_max_sclk_level(struct amdgpu_device *adev)
1721{
1722 struct cz_power_info *pi = cz_get_pi(adev);
1723
1724 if (!pi->max_sclk_level) {
1725 cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxSclkLevel);
1726 pi->max_sclk_level = cz_get_argument(adev) + 1;
1727 }
1728
1729 if (pi->max_sclk_level > CZ_MAX_HARDWARE_POWERLEVELS) {
1730 DRM_ERROR("Invalid max sclk level!\n");
1731 return -EINVAL;
1732 }
1733
1734 return pi->max_sclk_level;
1735}
1736
1737static int cz_dpm_unforce_dpm_levels(struct amdgpu_device *adev)
1738{
1739 struct cz_power_info *pi = cz_get_pi(adev);
1740 struct amdgpu_clock_voltage_dependency_table *dep_table =
1741 &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
1742 uint32_t level = 0;
1743 int ret = 0;
1744
1745 pi->sclk_dpm.soft_min_clk = dep_table->entries[0].clk;
1746 level = cz_dpm_get_max_sclk_level(adev) - 1;
1747 if (level < dep_table->count)
1748 pi->sclk_dpm.soft_max_clk = dep_table->entries[level].clk;
1749 else
1750 pi->sclk_dpm.soft_max_clk =
1751 dep_table->entries[dep_table->count - 1].clk;
1752
1753 /* get min/max sclk soft value
1754 * notify SMU to execute */
1755 ret = cz_send_msg_to_smc_with_parameter(adev,
1756 PPSMC_MSG_SetSclkSoftMin,
1757 cz_get_sclk_level(adev,
1758 pi->sclk_dpm.soft_min_clk,
1759 PPSMC_MSG_SetSclkSoftMin));
1760 if (ret)
1761 return ret;
1762
1763 ret = cz_send_msg_to_smc_with_parameter(adev,
1764 PPSMC_MSG_SetSclkSoftMax,
1765 cz_get_sclk_level(adev,
1766 pi->sclk_dpm.soft_max_clk,
1767 PPSMC_MSG_SetSclkSoftMax));
1768 if (ret)
1769 return ret;
1770
Alex Deucher1a45e8a2015-07-14 17:37:48 -04001771 DRM_DEBUG("DPM unforce state min=%d, max=%d.\n",
1772 pi->sclk_dpm.soft_min_clk,
1773 pi->sclk_dpm.soft_max_clk);
Alex Deucheraaa36a92015-04-20 17:31:14 -04001774
1775 return 0;
1776}
1777
Alex Deucherd83b1e812015-12-18 11:06:42 -05001778static int cz_dpm_uvd_force_highest(struct amdgpu_device *adev)
1779{
1780 struct cz_power_info *pi = cz_get_pi(adev);
1781 int ret = 0;
1782
1783 if (pi->uvd_dpm.soft_min_clk != pi->uvd_dpm.soft_max_clk) {
1784 pi->uvd_dpm.soft_min_clk =
1785 pi->uvd_dpm.soft_max_clk;
1786 ret = cz_send_msg_to_smc_with_parameter(adev,
1787 PPSMC_MSG_SetUvdSoftMin,
1788 cz_get_uvd_level(adev,
1789 pi->uvd_dpm.soft_min_clk,
1790 PPSMC_MSG_SetUvdSoftMin));
1791 if (ret)
1792 return ret;
1793 }
1794
1795 return ret;
1796}
1797
1798static int cz_dpm_uvd_force_lowest(struct amdgpu_device *adev)
1799{
1800 struct cz_power_info *pi = cz_get_pi(adev);
1801 int ret = 0;
1802
1803 if (pi->uvd_dpm.soft_max_clk != pi->uvd_dpm.soft_min_clk) {
1804 pi->uvd_dpm.soft_max_clk = pi->uvd_dpm.soft_min_clk;
1805 ret = cz_send_msg_to_smc_with_parameter(adev,
1806 PPSMC_MSG_SetUvdSoftMax,
1807 cz_get_uvd_level(adev,
1808 pi->uvd_dpm.soft_max_clk,
1809 PPSMC_MSG_SetUvdSoftMax));
1810 if (ret)
1811 return ret;
1812 }
1813
1814 return ret;
1815}
1816
1817static uint32_t cz_dpm_get_max_uvd_level(struct amdgpu_device *adev)
1818{
1819 struct cz_power_info *pi = cz_get_pi(adev);
1820
1821 if (!pi->max_uvd_level) {
1822 cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxUvdLevel);
1823 pi->max_uvd_level = cz_get_argument(adev) + 1;
1824 }
1825
1826 if (pi->max_uvd_level > CZ_MAX_HARDWARE_POWERLEVELS) {
1827 DRM_ERROR("Invalid max uvd level!\n");
1828 return -EINVAL;
1829 }
1830
1831 return pi->max_uvd_level;
1832}
1833
1834static int cz_dpm_unforce_uvd_dpm_levels(struct amdgpu_device *adev)
1835{
1836 struct cz_power_info *pi = cz_get_pi(adev);
1837 struct amdgpu_uvd_clock_voltage_dependency_table *dep_table =
1838 &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
1839 uint32_t level = 0;
1840 int ret = 0;
1841
1842 pi->uvd_dpm.soft_min_clk = dep_table->entries[0].vclk;
1843 level = cz_dpm_get_max_uvd_level(adev) - 1;
1844 if (level < dep_table->count)
1845 pi->uvd_dpm.soft_max_clk = dep_table->entries[level].vclk;
1846 else
1847 pi->uvd_dpm.soft_max_clk =
1848 dep_table->entries[dep_table->count - 1].vclk;
1849
1850 /* get min/max sclk soft value
1851 * notify SMU to execute */
1852 ret = cz_send_msg_to_smc_with_parameter(adev,
1853 PPSMC_MSG_SetUvdSoftMin,
1854 cz_get_uvd_level(adev,
1855 pi->uvd_dpm.soft_min_clk,
1856 PPSMC_MSG_SetUvdSoftMin));
1857 if (ret)
1858 return ret;
1859
1860 ret = cz_send_msg_to_smc_with_parameter(adev,
1861 PPSMC_MSG_SetUvdSoftMax,
1862 cz_get_uvd_level(adev,
1863 pi->uvd_dpm.soft_max_clk,
1864 PPSMC_MSG_SetUvdSoftMax));
1865 if (ret)
1866 return ret;
1867
1868 DRM_DEBUG("DPM uvd unforce state min=%d, max=%d.\n",
1869 pi->uvd_dpm.soft_min_clk,
1870 pi->uvd_dpm.soft_max_clk);
1871
1872 return 0;
1873}
1874
Alex Deucher044c0622015-12-18 11:25:16 -05001875static int cz_dpm_vce_force_highest(struct amdgpu_device *adev)
1876{
1877 struct cz_power_info *pi = cz_get_pi(adev);
1878 int ret = 0;
1879
1880 if (pi->vce_dpm.soft_min_clk != pi->vce_dpm.soft_max_clk) {
1881 pi->vce_dpm.soft_min_clk =
1882 pi->vce_dpm.soft_max_clk;
1883 ret = cz_send_msg_to_smc_with_parameter(adev,
1884 PPSMC_MSG_SetEclkSoftMin,
1885 cz_get_eclk_level(adev,
1886 pi->vce_dpm.soft_min_clk,
1887 PPSMC_MSG_SetEclkSoftMin));
1888 if (ret)
1889 return ret;
1890 }
1891
1892 return ret;
1893}
1894
1895static int cz_dpm_vce_force_lowest(struct amdgpu_device *adev)
1896{
1897 struct cz_power_info *pi = cz_get_pi(adev);
1898 int ret = 0;
1899
1900 if (pi->vce_dpm.soft_max_clk != pi->vce_dpm.soft_min_clk) {
1901 pi->vce_dpm.soft_max_clk = pi->vce_dpm.soft_min_clk;
1902 ret = cz_send_msg_to_smc_with_parameter(adev,
1903 PPSMC_MSG_SetEclkSoftMax,
1904 cz_get_uvd_level(adev,
1905 pi->vce_dpm.soft_max_clk,
1906 PPSMC_MSG_SetEclkSoftMax));
1907 if (ret)
1908 return ret;
1909 }
1910
1911 return ret;
1912}
1913
1914static uint32_t cz_dpm_get_max_vce_level(struct amdgpu_device *adev)
1915{
1916 struct cz_power_info *pi = cz_get_pi(adev);
1917
1918 if (!pi->max_vce_level) {
1919 cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxEclkLevel);
1920 pi->max_vce_level = cz_get_argument(adev) + 1;
1921 }
1922
1923 if (pi->max_vce_level > CZ_MAX_HARDWARE_POWERLEVELS) {
1924 DRM_ERROR("Invalid max vce level!\n");
1925 return -EINVAL;
1926 }
1927
1928 return pi->max_vce_level;
1929}
1930
1931static int cz_dpm_unforce_vce_dpm_levels(struct amdgpu_device *adev)
1932{
1933 struct cz_power_info *pi = cz_get_pi(adev);
1934 struct amdgpu_vce_clock_voltage_dependency_table *dep_table =
1935 &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1936 uint32_t level = 0;
1937 int ret = 0;
1938
1939 pi->vce_dpm.soft_min_clk = dep_table->entries[0].ecclk;
1940 level = cz_dpm_get_max_vce_level(adev) - 1;
1941 if (level < dep_table->count)
1942 pi->vce_dpm.soft_max_clk = dep_table->entries[level].ecclk;
1943 else
1944 pi->vce_dpm.soft_max_clk =
1945 dep_table->entries[dep_table->count - 1].ecclk;
1946
1947 /* get min/max sclk soft value
1948 * notify SMU to execute */
1949 ret = cz_send_msg_to_smc_with_parameter(adev,
1950 PPSMC_MSG_SetEclkSoftMin,
1951 cz_get_eclk_level(adev,
1952 pi->vce_dpm.soft_min_clk,
1953 PPSMC_MSG_SetEclkSoftMin));
1954 if (ret)
1955 return ret;
1956
1957 ret = cz_send_msg_to_smc_with_parameter(adev,
1958 PPSMC_MSG_SetEclkSoftMax,
1959 cz_get_eclk_level(adev,
1960 pi->vce_dpm.soft_max_clk,
1961 PPSMC_MSG_SetEclkSoftMax));
1962 if (ret)
1963 return ret;
1964
1965 DRM_DEBUG("DPM vce unforce state min=%d, max=%d.\n",
1966 pi->vce_dpm.soft_min_clk,
1967 pi->vce_dpm.soft_max_clk);
1968
1969 return 0;
1970}
1971
Alex Deucheraaa36a92015-04-20 17:31:14 -04001972static int cz_dpm_force_dpm_level(struct amdgpu_device *adev,
Alex Deucher85cfe092015-07-14 12:26:41 -04001973 enum amdgpu_dpm_forced_level level)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001974{
1975 int ret = 0;
1976
1977 switch (level) {
1978 case AMDGPU_DPM_FORCED_LEVEL_HIGH:
Alex Deucher5f576422015-12-18 11:28:49 -05001979 /* sclk */
Alex Deucher85cfe092015-07-14 12:26:41 -04001980 ret = cz_dpm_unforce_dpm_levels(adev);
1981 if (ret)
1982 return ret;
Alex Deucheraaa36a92015-04-20 17:31:14 -04001983 ret = cz_dpm_force_highest(adev);
1984 if (ret)
1985 return ret;
Alex Deucher5f576422015-12-18 11:28:49 -05001986
1987 /* uvd */
1988 ret = cz_dpm_unforce_uvd_dpm_levels(adev);
1989 if (ret)
1990 return ret;
1991 ret = cz_dpm_uvd_force_highest(adev);
1992 if (ret)
1993 return ret;
Alex Deucher403664b2015-12-18 11:33:30 -05001994
1995 /* vce */
1996 ret = cz_dpm_unforce_vce_dpm_levels(adev);
1997 if (ret)
1998 return ret;
1999 ret = cz_dpm_vce_force_highest(adev);
2000 if (ret)
2001 return ret;
Alex Deucheraaa36a92015-04-20 17:31:14 -04002002 break;
2003 case AMDGPU_DPM_FORCED_LEVEL_LOW:
Alex Deucher5f576422015-12-18 11:28:49 -05002004 /* sclk */
Alex Deucher85cfe092015-07-14 12:26:41 -04002005 ret = cz_dpm_unforce_dpm_levels(adev);
2006 if (ret)
2007 return ret;
Alex Deucheraaa36a92015-04-20 17:31:14 -04002008 ret = cz_dpm_force_lowest(adev);
2009 if (ret)
2010 return ret;
Alex Deucher5f576422015-12-18 11:28:49 -05002011
2012 /* uvd */
2013 ret = cz_dpm_unforce_uvd_dpm_levels(adev);
2014 if (ret)
2015 return ret;
2016 ret = cz_dpm_uvd_force_lowest(adev);
2017 if (ret)
2018 return ret;
Alex Deucher403664b2015-12-18 11:33:30 -05002019
2020 /* vce */
2021 ret = cz_dpm_unforce_vce_dpm_levels(adev);
2022 if (ret)
2023 return ret;
2024 ret = cz_dpm_vce_force_lowest(adev);
2025 if (ret)
2026 return ret;
Alex Deucheraaa36a92015-04-20 17:31:14 -04002027 break;
2028 case AMDGPU_DPM_FORCED_LEVEL_AUTO:
Alex Deucher5f576422015-12-18 11:28:49 -05002029 /* sclk */
Alex Deucheraaa36a92015-04-20 17:31:14 -04002030 ret = cz_dpm_unforce_dpm_levels(adev);
2031 if (ret)
2032 return ret;
Alex Deucher5f576422015-12-18 11:28:49 -05002033
2034 /* uvd */
2035 ret = cz_dpm_unforce_uvd_dpm_levels(adev);
2036 if (ret)
2037 return ret;
Alex Deucher403664b2015-12-18 11:33:30 -05002038
2039 /* vce */
2040 ret = cz_dpm_unforce_vce_dpm_levels(adev);
2041 if (ret)
2042 return ret;
Alex Deucheraaa36a92015-04-20 17:31:14 -04002043 break;
2044 default:
2045 break;
2046 }
2047
Alex Deucher58829aa2015-07-14 12:29:00 -04002048 adev->pm.dpm.forced_level = level;
2049
Alex Deucheraaa36a92015-04-20 17:31:14 -04002050 return ret;
2051}
2052
2053/* fix me, display configuration change lists here
2054 * mostly dal related*/
2055static void cz_dpm_display_configuration_changed(struct amdgpu_device *adev)
2056{
2057}
2058
2059static uint32_t cz_dpm_get_sclk(struct amdgpu_device *adev, bool low)
2060{
2061 struct cz_power_info *pi = cz_get_pi(adev);
2062 struct cz_ps *requested_state = cz_get_ps(&pi->requested_rps);
2063
2064 if (low)
2065 return requested_state->levels[0].sclk;
2066 else
2067 return requested_state->levels[requested_state->num_levels - 1].sclk;
2068
2069}
2070
2071static uint32_t cz_dpm_get_mclk(struct amdgpu_device *adev, bool low)
2072{
2073 struct cz_power_info *pi = cz_get_pi(adev);
2074
2075 return pi->sys_info.bootup_uma_clk;
2076}
2077
Sonny Jiang564ea792015-05-12 16:13:35 -04002078static int cz_enable_uvd_dpm(struct amdgpu_device *adev, bool enable)
2079{
2080 struct cz_power_info *pi = cz_get_pi(adev);
2081 int ret = 0;
2082
2083 if (enable && pi->caps_uvd_dpm ) {
2084 pi->dpm_flags |= DPMFlags_UVD_Enabled;
2085 DRM_DEBUG("UVD DPM Enabled.\n");
2086
2087 ret = cz_send_msg_to_smc_with_parameter(adev,
2088 PPSMC_MSG_EnableAllSmuFeatures, UVD_DPM_MASK);
2089 } else {
2090 pi->dpm_flags &= ~DPMFlags_UVD_Enabled;
2091 DRM_DEBUG("UVD DPM Stopped\n");
2092
2093 ret = cz_send_msg_to_smc_with_parameter(adev,
2094 PPSMC_MSG_DisableAllSmuFeatures, UVD_DPM_MASK);
2095 }
2096
2097 return ret;
2098}
2099
2100static int cz_update_uvd_dpm(struct amdgpu_device *adev, bool gate)
2101{
2102 return cz_enable_uvd_dpm(adev, !gate);
2103}
2104
2105
2106static void cz_dpm_powergate_uvd(struct amdgpu_device *adev, bool gate)
2107{
2108 struct cz_power_info *pi = cz_get_pi(adev);
2109 int ret;
2110
2111 if (pi->uvd_power_gated == gate)
2112 return;
2113
2114 pi->uvd_power_gated = gate;
2115
2116 if (gate) {
2117 if (pi->caps_uvd_pg) {
2118 /* disable clockgating so we can properly shut down the block */
yanyang15fc3aee2015-05-22 14:39:35 -04002119 ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
2120 AMD_CG_STATE_UNGATE);
Tom St Denis0da31ff2016-07-28 09:46:00 -04002121 if (ret) {
2122 DRM_ERROR("UVD DPM Power Gating failed to set clockgating state\n");
2123 return;
2124 }
2125
Sonny Jiang564ea792015-05-12 16:13:35 -04002126 /* shutdown the UVD block */
yanyang15fc3aee2015-05-22 14:39:35 -04002127 ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
2128 AMD_PG_STATE_GATE);
Tom St Denis0da31ff2016-07-28 09:46:00 -04002129
2130 if (ret) {
2131 DRM_ERROR("UVD DPM Power Gating failed to set powergating state\n");
2132 return;
2133 }
Sonny Jiang564ea792015-05-12 16:13:35 -04002134 }
2135 cz_update_uvd_dpm(adev, gate);
Tom St Denis0da31ff2016-07-28 09:46:00 -04002136 if (pi->caps_uvd_pg) {
Sonny Jiang564ea792015-05-12 16:13:35 -04002137 /* power off the UVD block */
Tom St Denis0da31ff2016-07-28 09:46:00 -04002138 ret = cz_send_msg_to_smc(adev, PPSMC_MSG_UVDPowerOFF);
2139 if (ret) {
2140 DRM_ERROR("UVD DPM Power Gating failed to send SMU PowerOFF message\n");
2141 return;
2142 }
2143 }
Sonny Jiang564ea792015-05-12 16:13:35 -04002144 } else {
2145 if (pi->caps_uvd_pg) {
2146 /* power on the UVD block */
2147 if (pi->uvd_dynamic_pg)
Tom St Denis0da31ff2016-07-28 09:46:00 -04002148 ret = cz_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_UVDPowerON, 1);
Sonny Jiang564ea792015-05-12 16:13:35 -04002149 else
Tom St Denis0da31ff2016-07-28 09:46:00 -04002150 ret = cz_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_UVDPowerON, 0);
2151
2152 if (ret) {
2153 DRM_ERROR("UVD DPM Power Gating Failed to send SMU PowerON message\n");
2154 return;
2155 }
2156
Sonny Jiang564ea792015-05-12 16:13:35 -04002157 /* re-init the UVD block */
yanyang15fc3aee2015-05-22 14:39:35 -04002158 ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
2159 AMD_PG_STATE_UNGATE);
Tom St Denis0da31ff2016-07-28 09:46:00 -04002160
2161 if (ret) {
2162 DRM_ERROR("UVD DPM Power Gating Failed to set powergating state\n");
2163 return;
2164 }
2165
Sonny Jiang564ea792015-05-12 16:13:35 -04002166 /* enable clockgating. hw will dynamically gate/ungate clocks on the fly */
yanyang15fc3aee2015-05-22 14:39:35 -04002167 ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
2168 AMD_CG_STATE_GATE);
Tom St Denis0da31ff2016-07-28 09:46:00 -04002169 if (ret) {
2170 DRM_ERROR("UVD DPM Power Gating Failed to set clockgating state\n");
2171 return;
2172 }
Sonny Jiang564ea792015-05-12 16:13:35 -04002173 }
2174 cz_update_uvd_dpm(adev, gate);
2175 }
2176}
2177
Sonny Jiangb7a077692015-05-28 15:47:53 -04002178static int cz_enable_vce_dpm(struct amdgpu_device *adev, bool enable)
2179{
2180 struct cz_power_info *pi = cz_get_pi(adev);
2181 int ret = 0;
2182
2183 if (enable && pi->caps_vce_dpm) {
2184 pi->dpm_flags |= DPMFlags_VCE_Enabled;
2185 DRM_DEBUG("VCE DPM Enabled.\n");
2186
2187 ret = cz_send_msg_to_smc_with_parameter(adev,
2188 PPSMC_MSG_EnableAllSmuFeatures, VCE_DPM_MASK);
2189
2190 } else {
2191 pi->dpm_flags &= ~DPMFlags_VCE_Enabled;
2192 DRM_DEBUG("VCE DPM Stopped\n");
2193
2194 ret = cz_send_msg_to_smc_with_parameter(adev,
2195 PPSMC_MSG_DisableAllSmuFeatures, VCE_DPM_MASK);
2196 }
2197
2198 return ret;
2199}
2200
2201static int cz_update_vce_dpm(struct amdgpu_device *adev)
2202{
2203 struct cz_power_info *pi = cz_get_pi(adev);
2204 struct amdgpu_vce_clock_voltage_dependency_table *table =
2205 &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
2206
2207 /* Stable Pstate is enabled and we need to set the VCE DPM to highest level */
2208 if (pi->caps_stable_power_state) {
2209 pi->vce_dpm.hard_min_clk = table->entries[table->count-1].ecclk;
2210
2211 } else { /* non-stable p-state cases. without vce.Arbiter.EcclkHardMin */
Alex Deucher403664b2015-12-18 11:33:30 -05002212 /* leave it as set by user */
2213 /*pi->vce_dpm.hard_min_clk = table->entries[0].ecclk;*/
Sonny Jiangb7a077692015-05-28 15:47:53 -04002214 }
2215
2216 cz_send_msg_to_smc_with_parameter(adev,
2217 PPSMC_MSG_SetEclkHardMin,
2218 cz_get_eclk_level(adev,
2219 pi->vce_dpm.hard_min_clk,
2220 PPSMC_MSG_SetEclkHardMin));
2221 return 0;
2222}
2223
2224static void cz_dpm_powergate_vce(struct amdgpu_device *adev, bool gate)
2225{
2226 struct cz_power_info *pi = cz_get_pi(adev);
2227
2228 if (pi->caps_vce_pg) {
2229 if (pi->vce_power_gated != gate) {
2230 if (gate) {
2231 /* disable clockgating so we can properly shut down the block */
2232 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
2233 AMD_CG_STATE_UNGATE);
2234 /* shutdown the VCE block */
2235 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
2236 AMD_PG_STATE_GATE);
2237
2238 cz_enable_vce_dpm(adev, false);
Alex Deucher89913ea2016-02-29 16:11:07 -05002239 cz_send_msg_to_smc(adev, PPSMC_MSG_VCEPowerOFF);
Sonny Jiangb7a077692015-05-28 15:47:53 -04002240 pi->vce_power_gated = true;
2241 } else {
2242 cz_send_msg_to_smc(adev, PPSMC_MSG_VCEPowerON);
2243 pi->vce_power_gated = false;
2244
2245 /* re-init the VCE block */
2246 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
2247 AMD_PG_STATE_UNGATE);
2248 /* enable clockgating. hw will dynamically gate/ungate clocks on the fly */
2249 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
2250 AMD_CG_STATE_GATE);
2251
2252 cz_update_vce_dpm(adev);
2253 cz_enable_vce_dpm(adev, true);
2254 }
2255 } else {
2256 if (! pi->vce_power_gated) {
2257 cz_update_vce_dpm(adev);
2258 }
2259 }
2260 } else { /*pi->caps_vce_pg*/
Arindam Nathfb065ce2016-06-20 14:17:49 +05302261 pi->vce_power_gated = gate;
Sonny Jiangb7a077692015-05-28 15:47:53 -04002262 cz_update_vce_dpm(adev);
Alex Deucherb3dae782016-02-25 11:24:52 -05002263 cz_enable_vce_dpm(adev, !gate);
Sonny Jiangb7a077692015-05-28 15:47:53 -04002264 }
Sonny Jiangb7a077692015-05-28 15:47:53 -04002265}
2266
yanyang15fc3aee2015-05-22 14:39:35 -04002267const struct amd_ip_funcs cz_dpm_ip_funcs = {
Tom St Denis88a907d2016-05-04 14:28:35 -04002268 .name = "cz_dpm",
Alex Deucheraaa36a92015-04-20 17:31:14 -04002269 .early_init = cz_dpm_early_init,
Sonny Jiang564ea792015-05-12 16:13:35 -04002270 .late_init = cz_dpm_late_init,
Alex Deucheraaa36a92015-04-20 17:31:14 -04002271 .sw_init = cz_dpm_sw_init,
2272 .sw_fini = cz_dpm_sw_fini,
2273 .hw_init = cz_dpm_hw_init,
2274 .hw_fini = cz_dpm_hw_fini,
2275 .suspend = cz_dpm_suspend,
2276 .resume = cz_dpm_resume,
2277 .is_idle = NULL,
2278 .wait_for_idle = NULL,
2279 .soft_reset = NULL,
Alex Deucheraaa36a92015-04-20 17:31:14 -04002280 .set_clockgating_state = cz_dpm_set_clockgating_state,
2281 .set_powergating_state = cz_dpm_set_powergating_state,
2282};
2283
2284static const struct amdgpu_dpm_funcs cz_dpm_funcs = {
2285 .get_temperature = cz_dpm_get_temperature,
2286 .pre_set_power_state = cz_dpm_pre_set_power_state,
2287 .set_power_state = cz_dpm_set_power_state,
2288 .post_set_power_state = cz_dpm_post_set_power_state,
2289 .display_configuration_changed = cz_dpm_display_configuration_changed,
2290 .get_sclk = cz_dpm_get_sclk,
2291 .get_mclk = cz_dpm_get_mclk,
2292 .print_power_state = cz_dpm_print_power_state,
2293 .debugfs_print_current_performance_level =
2294 cz_dpm_debugfs_print_current_performance_level,
2295 .force_performance_level = cz_dpm_force_dpm_level,
2296 .vblank_too_short = NULL,
Sonny Jiang564ea792015-05-12 16:13:35 -04002297 .powergate_uvd = cz_dpm_powergate_uvd,
Sonny Jiangb7a077692015-05-28 15:47:53 -04002298 .powergate_vce = cz_dpm_powergate_vce,
Alex Deucheraaa36a92015-04-20 17:31:14 -04002299};
2300
2301static void cz_dpm_set_funcs(struct amdgpu_device *adev)
2302{
2303 if (NULL == adev->pm.funcs)
2304 adev->pm.funcs = &cz_dpm_funcs;
2305}