smu10_hwmgr.c 33.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright 2015 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */
#include "pp_debug.h"
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include "atom-types.h"
#include "atombios.h"
#include "processpptables.h"
#include "cgs_common.h"
#include "smumgr.h"
#include "hwmgr.h"
#include "hardwaremanager.h"
#include "rv_ppsmc.h"
R
Rex Zhu 已提交
35
#include "smu10_hwmgr.h"
36
#include "power_state.h"
37
#include "soc15_common.h"
38

R
Rex Zhu 已提交
39 40
#define SMU10_MAX_DEEPSLEEP_DIVIDER_ID     5
#define SMU10_MINIMUM_ENGINE_CLOCK         800   /* 8Mhz, the low boundary of engine clock allowed on this chip */
41
#define SCLK_MIN_DIV_INTV_SHIFT         12
R
Rex Zhu 已提交
42
#define SMU10_DISPCLK_BYPASS_THRESHOLD     10000 /* 100Mhz */
43 44
#define SMC_RAM_END                     0x40000

45 46 47 48 49 50 51
#define mmPWR_MISC_CNTL_STATUS					0x0183
#define mmPWR_MISC_CNTL_STATUS_BASE_IDX				0
#define PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN__SHIFT	0x0
#define PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS__SHIFT		0x1
#define PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN_MASK		0x00000001L
#define PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS_MASK		0x00000006L

R
Rex Zhu 已提交
52
static const unsigned long SMU10_Magic = (unsigned long) PHM_Rv_Magic;
53 54


R
Rex Zhu 已提交
55
static int smu10_display_clock_voltage_request(struct pp_hwmgr *hwmgr,
56
		struct pp_display_clock_request *clock_req);
57 58


R
Rex Zhu 已提交
59
static struct smu10_power_state *cast_smu10_ps(struct pp_hw_power_state *hw_ps)
60
{
R
Rex Zhu 已提交
61
	if (SMU10_Magic != hw_ps->magic)
62 63
		return NULL;

R
Rex Zhu 已提交
64
	return (struct smu10_power_state *)hw_ps;
65 66
}

R
Rex Zhu 已提交
67
static const struct smu10_power_state *cast_const_smu10_ps(
68 69
				const struct pp_hw_power_state *hw_ps)
{
R
Rex Zhu 已提交
70
	if (SMU10_Magic != hw_ps->magic)
71 72
		return NULL;

R
Rex Zhu 已提交
73
	return (struct smu10_power_state *)hw_ps;
74 75
}

R
Rex Zhu 已提交
76
static int smu10_initialize_dpm_defaults(struct pp_hwmgr *hwmgr)
77
{
R
Rex Zhu 已提交
78
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
79

R
Rex Zhu 已提交
80 81 82 83 84 85 86
	smu10_data->dce_slow_sclk_threshold = 30000;
	smu10_data->thermal_auto_throttling_treshold = 0;
	smu10_data->is_nb_dpm_enabled = 1;
	smu10_data->dpm_flags = 1;
	smu10_data->need_min_deep_sleep_dcefclk = true;
	smu10_data->num_active_display = 0;
	smu10_data->deep_sleep_dcefclk = 0;
87

88 89 90 91 92
	if (hwmgr->feature_mask & PP_GFXOFF_MASK)
		smu10_data->gfx_off_controled_by_driver = true;
	else
		smu10_data->gfx_off_controled_by_driver = false;

93 94 95 96 97 98 99
	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
					PHM_PlatformCaps_SclkDeepSleep);

	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
				PHM_PlatformCaps_SclkThrottleLowNotification);

	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
100
				PHM_PlatformCaps_PowerPlaySupport);
101 102 103
	return 0;
}

R
Rex Zhu 已提交
104
static int smu10_construct_max_power_limits_table(struct pp_hwmgr *hwmgr,
105 106 107 108 109
			struct phm_clock_and_voltage_limits *table)
{
	return 0;
}

R
Rex Zhu 已提交
110
static int smu10_init_dynamic_state_adjustment_rule_settings(
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
							struct pp_hwmgr *hwmgr)
{
	uint32_t table_size =
		sizeof(struct phm_clock_voltage_dependency_table) +
		(7 * sizeof(struct phm_clock_voltage_dependency_record));

	struct phm_clock_voltage_dependency_table *table_clk_vlt =
					kzalloc(table_size, GFP_KERNEL);

	if (NULL == table_clk_vlt) {
		pr_err("Can not allocate memory!\n");
		return -ENOMEM;
	}

	table_clk_vlt->count = 8;
	table_clk_vlt->entries[0].clk = PP_DAL_POWERLEVEL_0;
	table_clk_vlt->entries[0].v = 0;
	table_clk_vlt->entries[1].clk = PP_DAL_POWERLEVEL_1;
	table_clk_vlt->entries[1].v = 1;
	table_clk_vlt->entries[2].clk = PP_DAL_POWERLEVEL_2;
	table_clk_vlt->entries[2].v = 2;
	table_clk_vlt->entries[3].clk = PP_DAL_POWERLEVEL_3;
	table_clk_vlt->entries[3].v = 3;
	table_clk_vlt->entries[4].clk = PP_DAL_POWERLEVEL_4;
	table_clk_vlt->entries[4].v = 4;
	table_clk_vlt->entries[5].clk = PP_DAL_POWERLEVEL_5;
	table_clk_vlt->entries[5].v = 5;
	table_clk_vlt->entries[6].clk = PP_DAL_POWERLEVEL_6;
	table_clk_vlt->entries[6].v = 6;
	table_clk_vlt->entries[7].clk = PP_DAL_POWERLEVEL_7;
	table_clk_vlt->entries[7].v = 7;
	hwmgr->dyn_state.vddc_dep_on_dal_pwrl = table_clk_vlt;

	return 0;
}

R
Rex Zhu 已提交
147
static int smu10_get_system_info_data(struct pp_hwmgr *hwmgr)
148
{
R
Rex Zhu 已提交
149
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)hwmgr->backend;
150

R
Rex Zhu 已提交
151 152
	smu10_data->sys_info.htc_hyst_lmt = 5;
	smu10_data->sys_info.htc_tmp_lmt = 203;
153

R
Rex Zhu 已提交
154 155
	if (smu10_data->thermal_auto_throttling_treshold == 0)
		 smu10_data->thermal_auto_throttling_treshold = 203;
156

R
Rex Zhu 已提交
157
	smu10_construct_max_power_limits_table (hwmgr,
158 159
				    &hwmgr->dyn_state.max_clock_voltage_on_ac);

R
Rex Zhu 已提交
160
	smu10_init_dynamic_state_adjustment_rule_settings(hwmgr);
161 162 163 164

	return 0;
}

R
Rex Zhu 已提交
165
static int smu10_construct_boot_state(struct pp_hwmgr *hwmgr)
166 167 168 169
{
	return 0;
}

R
Rex Zhu 已提交
170
static int smu10_set_clock_limit(struct pp_hwmgr *hwmgr, const void *input)
171
{
172 173 174
	struct PP_Clocks clocks = {0};
	struct pp_display_clock_request clock_req;

175
	clocks.dcefClock = hwmgr->display_config->min_dcef_set_clk;
176 177 178
	clock_req.clock_type = amd_pp_dcf_clock;
	clock_req.clock_freq_in_khz = clocks.dcefClock * 10;

R
Rex Zhu 已提交
179
	PP_ASSERT_WITH_CODE(!smu10_display_clock_voltage_request(hwmgr, &clock_req),
180 181
				"Attempt to set DCF Clock Failed!", return -EINVAL);

182 183 184
	return 0;
}

R
Rex Zhu 已提交
185
static int smu10_set_deep_sleep_dcefclk(struct pp_hwmgr *hwmgr, uint32_t clock)
186
{
R
Rex Zhu 已提交
187
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
188

R
Rex Zhu 已提交
189 190
	if (smu10_data->need_min_deep_sleep_dcefclk && smu10_data->deep_sleep_dcefclk != clock/100) {
		smu10_data->deep_sleep_dcefclk = clock/100;
191
		smum_send_msg_to_smc_with_parameter(hwmgr,
192
					PPSMC_MSG_SetMinDeepSleepDcefclk,
R
Rex Zhu 已提交
193
					smu10_data->deep_sleep_dcefclk);
194 195 196 197
	}
	return 0;
}

R
Rex Zhu 已提交
198
static int smu10_set_active_display_count(struct pp_hwmgr *hwmgr, uint32_t count)
199
{
R
Rex Zhu 已提交
200
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
201

R
Rex Zhu 已提交
202 203
	if (smu10_data->num_active_display != count) {
		smu10_data->num_active_display = count;
204
		smum_send_msg_to_smc_with_parameter(hwmgr,
205
				PPSMC_MSG_SetDisplayCount,
R
Rex Zhu 已提交
206
				smu10_data->num_active_display);
207
	}
208

209 210 211
	return 0;
}

R
Rex Zhu 已提交
212
static int smu10_set_power_state_tasks(struct pp_hwmgr *hwmgr, const void *input)
213
{
R
Rex Zhu 已提交
214
	return smu10_set_clock_limit(hwmgr, input);
215
}
216

R
Rex Zhu 已提交
217
static int smu10_init_power_gate_state(struct pp_hwmgr *hwmgr)
218
{
R
Rex Zhu 已提交
219
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
220
	struct amdgpu_device *adev = hwmgr->adev;
221

R
Rex Zhu 已提交
222 223 224
	smu10_data->vcn_power_gated = true;
	smu10_data->isp_tileA_power_gated = true;
	smu10_data->isp_tileB_power_gated = true;
225

226 227 228 229 230 231
	if (adev->pg_flags & AMD_PG_SUPPORT_GFX_PG)
		return smum_send_msg_to_smc_with_parameter(hwmgr,
							   PPSMC_MSG_SetGfxCGPG,
							   true);
	else
		return 0;
232 233 234
}


R
Rex Zhu 已提交
235
static int smu10_setup_asic_task(struct pp_hwmgr *hwmgr)
236
{
R
Rex Zhu 已提交
237
	return smu10_init_power_gate_state(hwmgr);
238
}
239

R
Rex Zhu 已提交
240
static int smu10_reset_cc6_data(struct pp_hwmgr *hwmgr)
241
{
R
Rex Zhu 已提交
242
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
243

R
Rex Zhu 已提交
244 245 246 247
	smu10_data->separation_time = 0;
	smu10_data->cc6_disable = false;
	smu10_data->pstate_disable = false;
	smu10_data->cc6_setting_changed = false;
248 249 250 251

	return 0;
}

R
Rex Zhu 已提交
252
static int smu10_power_off_asic(struct pp_hwmgr *hwmgr)
253
{
R
Rex Zhu 已提交
254
	return smu10_reset_cc6_data(hwmgr);
255
}
256

257 258 259 260 261 262 263 264 265 266 267 268 269
static bool smu10_is_gfx_on(struct pp_hwmgr *hwmgr)
{
	uint32_t reg;
	struct amdgpu_device *adev = hwmgr->adev;

	reg = RREG32_SOC15(PWR, 0, mmPWR_MISC_CNTL_STATUS);
	if ((reg & PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS_MASK) ==
	    (0x2 << PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS__SHIFT))
		return true;

	return false;
}

R
Rex Zhu 已提交
270
static int smu10_disable_gfx_off(struct pp_hwmgr *hwmgr)
271
{
R
Rex Zhu 已提交
272
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
273

274
	if (smu10_data->gfx_off_controled_by_driver) {
275
		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_DisableGfxOff);
276

277 278 279 280 281
		/* confirm gfx is back to "on" state */
		while (!smu10_is_gfx_on(hwmgr))
			msleep(1);
	}

282 283 284
	return 0;
}

R
Rex Zhu 已提交
285
static int smu10_disable_dpm_tasks(struct pp_hwmgr *hwmgr)
286
{
R
Rex Zhu 已提交
287
	return smu10_disable_gfx_off(hwmgr);
288
}
289

R
Rex Zhu 已提交
290
static int smu10_enable_gfx_off(struct pp_hwmgr *hwmgr)
291
{
R
Rex Zhu 已提交
292
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
293

R
Rex Zhu 已提交
294
	if (smu10_data->gfx_off_controled_by_driver)
295
		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_EnableGfxOff);
296 297 298 299

	return 0;
}

R
Rex Zhu 已提交
300
static int smu10_enable_dpm_tasks(struct pp_hwmgr *hwmgr)
301
{
R
Rex Zhu 已提交
302
	return smu10_enable_gfx_off(hwmgr);
303
}
304

305 306 307 308 309 310 311 312
static int smu10_gfx_off_control(struct pp_hwmgr *hwmgr, bool enable)
{
	if (enable)
		return smu10_enable_gfx_off(hwmgr);
	else
		return smu10_disable_gfx_off(hwmgr);
}

R
Rex Zhu 已提交
313
static int smu10_apply_state_adjust_rules(struct pp_hwmgr *hwmgr,
314 315 316 317 318 319 320
				struct pp_power_state  *prequest_ps,
			const struct pp_power_state *pcurrent_ps)
{
	return 0;
}

/* temporary hardcoded clock voltage breakdown tables */
321
static const DpmClock_t VddDcfClk[]= {
322 323 324 325 326
	{ 300, 2600},
	{ 600, 3200},
	{ 600, 3600},
};

327
static const DpmClock_t VddSocClk[]= {
328 329 330 331 332
	{ 478, 2600},
	{ 722, 3200},
	{ 722, 3600},
};

333
static const DpmClock_t VddFClk[]= {
334 335 336 337 338
	{ 400, 2600},
	{1200, 3200},
	{1200, 3600},
};

339
static const DpmClock_t VddDispClk[]= {
340 341 342 343 344
	{ 435, 2600},
	{ 661, 3200},
	{1086, 3600},
};

345
static const DpmClock_t VddDppClk[]= {
346 347 348 349 350
	{ 435, 2600},
	{ 661, 3200},
	{ 661, 3600},
};

351
static const DpmClock_t VddPhyClk[]= {
352 353 354 355 356
	{ 540, 2600},
	{ 810, 3200},
	{ 810, 3600},
};

R
Rex Zhu 已提交
357 358
static int smu10_get_clock_voltage_dependency_table(struct pp_hwmgr *hwmgr,
			struct smu10_voltage_dependency_table **pptable,
359
			uint32_t num_entry, const DpmClock_t *pclk_dependency_table)
360 361
{
	uint32_t table_size, i;
R
Rex Zhu 已提交
362
	struct smu10_voltage_dependency_table *ptable;
363

R
Rex Zhu 已提交
364
	table_size = sizeof(uint32_t) + sizeof(struct smu10_voltage_dependency_table) * num_entry;
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	ptable = kzalloc(table_size, GFP_KERNEL);

	if (NULL == ptable)
		return -ENOMEM;

	ptable->count = num_entry;

	for (i = 0; i < ptable->count; i++) {
		ptable->entries[i].clk         = pclk_dependency_table->Freq * 100;
		ptable->entries[i].vol         = pclk_dependency_table->Vol;
		pclk_dependency_table++;
	}

	*pptable = ptable;

	return 0;
}


R
Rex Zhu 已提交
384
static int smu10_populate_clock_table(struct pp_hwmgr *hwmgr)
385
{
386
	uint32_t result;
387

R
Rex Zhu 已提交
388 389 390
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
	DpmClocks_t  *table = &(smu10_data->clock_table);
	struct smu10_clock_voltage_information *pinfo = &(smu10_data->clock_vol_info);
391

392
	result = smum_smc_table_manager(hwmgr, (uint8_t *)table, SMU10_CLOCKTABLE, true);
393 394 395 396 397 398

	PP_ASSERT_WITH_CODE((0 == result),
			"Attempt to copy clock table from smc failed",
			return result);

	if (0 == result && table->DcefClocks[0].Freq != 0) {
R
Rex Zhu 已提交
399
		smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dcefclk,
400
						NUM_DCEFCLK_DPM_LEVELS,
R
Rex Zhu 已提交
401 402
						&smu10_data->clock_table.DcefClocks[0]);
		smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_socclk,
403
						NUM_SOCCLK_DPM_LEVELS,
R
Rex Zhu 已提交
404 405
						&smu10_data->clock_table.SocClocks[0]);
		smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_fclk,
406
						NUM_FCLK_DPM_LEVELS,
R
Rex Zhu 已提交
407 408
						&smu10_data->clock_table.FClocks[0]);
		smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_mclk,
409
						NUM_MEMCLK_DPM_LEVELS,
R
Rex Zhu 已提交
410
						&smu10_data->clock_table.MemClocks[0]);
411
	} else {
R
Rex Zhu 已提交
412
		smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dcefclk,
413 414
						ARRAY_SIZE(VddDcfClk),
						&VddDcfClk[0]);
R
Rex Zhu 已提交
415
		smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_socclk,
416 417
						ARRAY_SIZE(VddSocClk),
						&VddSocClk[0]);
R
Rex Zhu 已提交
418
		smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_fclk,
419 420
						ARRAY_SIZE(VddFClk),
						&VddFClk[0]);
421
	}
R
Rex Zhu 已提交
422
	smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dispclk,
423 424
					ARRAY_SIZE(VddDispClk),
					&VddDispClk[0]);
R
Rex Zhu 已提交
425
	smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dppclk,
426
					ARRAY_SIZE(VddDppClk), &VddDppClk[0]);
R
Rex Zhu 已提交
427
	smu10_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_phyclk,
428
					ARRAY_SIZE(VddPhyClk), &VddPhyClk[0]);
429

430
	smum_send_msg_to_smc(hwmgr, PPSMC_MSG_GetMinGfxclkFrequency);
431
	result = smum_get_argument(hwmgr);
432
	smu10_data->gfx_min_freq_limit = result / 10 * 1000;
433

434
	smum_send_msg_to_smc(hwmgr, PPSMC_MSG_GetMaxGfxclkFrequency);
435
	result = smum_get_argument(hwmgr);
436
	smu10_data->gfx_max_freq_limit = result / 10 * 1000;
437

438 439 440
	return 0;
}

R
Rex Zhu 已提交
441
static int smu10_hwmgr_backend_init(struct pp_hwmgr *hwmgr)
442 443
{
	int result = 0;
R
Rex Zhu 已提交
444
	struct smu10_hwmgr *data;
445

R
Rex Zhu 已提交
446
	data = kzalloc(sizeof(struct smu10_hwmgr), GFP_KERNEL);
447 448 449 450 451
	if (data == NULL)
		return -ENOMEM;

	hwmgr->backend = data;

R
Rex Zhu 已提交
452
	result = smu10_initialize_dpm_defaults(hwmgr);
453
	if (result != 0) {
R
Rex Zhu 已提交
454
		pr_err("smu10_initialize_dpm_defaults failed\n");
455 456 457
		return result;
	}

R
Rex Zhu 已提交
458
	smu10_populate_clock_table(hwmgr);
459

R
Rex Zhu 已提交
460
	result = smu10_get_system_info_data(hwmgr);
461
	if (result != 0) {
R
Rex Zhu 已提交
462
		pr_err("smu10_get_system_info_data failed\n");
463 464 465
		return result;
	}

R
Rex Zhu 已提交
466
	smu10_construct_boot_state(hwmgr);
467 468

	hwmgr->platform_descriptor.hardwareActivityPerformanceLevels =
R
Rex Zhu 已提交
469
						SMU10_MAX_HARDWARE_POWERLEVELS;
470 471

	hwmgr->platform_descriptor.hardwarePerformanceLevels =
R
Rex Zhu 已提交
472
						SMU10_MAX_HARDWARE_POWERLEVELS;
473 474 475 476 477 478 479 480 481

	hwmgr->platform_descriptor.vbiosInterruptId = 0;

	hwmgr->platform_descriptor.clockStep.engineClock = 500;

	hwmgr->platform_descriptor.clockStep.memoryClock = 500;

	hwmgr->platform_descriptor.minimumClocksReductionPercentage = 50;

482 483
	hwmgr->pstate_sclk = SMU10_UMD_PSTATE_GFXCLK * 100;
	hwmgr->pstate_mclk = SMU10_UMD_PSTATE_FCLK * 100;
R
Rex Zhu 已提交
484

485 486 487
	return result;
}

R
Rex Zhu 已提交
488
static int smu10_hwmgr_backend_fini(struct pp_hwmgr *hwmgr)
489
{
R
Rex Zhu 已提交
490 491
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
	struct smu10_clock_voltage_information *pinfo = &(smu10_data->clock_vol_info);
492

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	kfree(pinfo->vdd_dep_on_dcefclk);
	pinfo->vdd_dep_on_dcefclk = NULL;
	kfree(pinfo->vdd_dep_on_socclk);
	pinfo->vdd_dep_on_socclk = NULL;
	kfree(pinfo->vdd_dep_on_fclk);
	pinfo->vdd_dep_on_fclk = NULL;
	kfree(pinfo->vdd_dep_on_dispclk);
	pinfo->vdd_dep_on_dispclk = NULL;
	kfree(pinfo->vdd_dep_on_dppclk);
	pinfo->vdd_dep_on_dppclk = NULL;
	kfree(pinfo->vdd_dep_on_phyclk);
	pinfo->vdd_dep_on_phyclk = NULL;

	kfree(hwmgr->dyn_state.vddc_dep_on_dal_pwrl);
	hwmgr->dyn_state.vddc_dep_on_dal_pwrl = NULL;
508

509 510 511 512 513 514
	kfree(hwmgr->backend);
	hwmgr->backend = NULL;

	return 0;
}

R
Rex Zhu 已提交
515
static int smu10_dpm_force_dpm_level(struct pp_hwmgr *hwmgr,
516 517
				enum amd_dpm_forced_level level)
{
518 519
	struct smu10_hwmgr *data = hwmgr->backend;

520 521 522 523 524 525 526 527 528 529
	if (hwmgr->smu_version < 0x1E3700) {
		pr_info("smu firmware version too old, can not set dpm level\n");
		return 0;
	}

	switch (level) {
	case AMD_DPM_FORCED_LEVEL_HIGH:
	case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
530
						data->gfx_max_freq_limit/100);
531 532
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
R
Rex Zhu 已提交
533
						SMU10_UMD_PSTATE_PEAK_FCLK);
534 535
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinSocclkByFreq,
R
Rex Zhu 已提交
536
						SMU10_UMD_PSTATE_PEAK_SOCCLK);
537 538
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinVcn,
R
Rex Zhu 已提交
539
						SMU10_UMD_PSTATE_VCE);
540 541 542

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
543
						data->gfx_max_freq_limit/100);
544 545
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
R
Rex Zhu 已提交
546
						SMU10_UMD_PSTATE_PEAK_FCLK);
547 548
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxSocclkByFreq,
R
Rex Zhu 已提交
549
						SMU10_UMD_PSTATE_PEAK_SOCCLK);
550 551
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxVcn,
R
Rex Zhu 已提交
552
						SMU10_UMD_PSTATE_VCE);
553 554 555 556
		break;
	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
557
						data->gfx_min_freq_limit/100);
558 559
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
560
						data->gfx_min_freq_limit/100);
561 562 563 564
		break;
	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
R
Rex Zhu 已提交
565
						SMU10_UMD_PSTATE_MIN_FCLK);
566 567
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
R
Rex Zhu 已提交
568
						SMU10_UMD_PSTATE_MIN_FCLK);
569 570 571 572
		break;
	case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
R
Rex Zhu 已提交
573
						SMU10_UMD_PSTATE_GFXCLK);
574 575
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
R
Rex Zhu 已提交
576
						SMU10_UMD_PSTATE_FCLK);
577 578
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinSocclkByFreq,
R
Rex Zhu 已提交
579
						SMU10_UMD_PSTATE_SOCCLK);
580 581
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinVcn,
R
Rex Zhu 已提交
582
						SMU10_UMD_PSTATE_VCE);
583 584 585

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
R
Rex Zhu 已提交
586
						SMU10_UMD_PSTATE_GFXCLK);
587 588
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
R
Rex Zhu 已提交
589
						SMU10_UMD_PSTATE_FCLK);
590 591
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxSocclkByFreq,
R
Rex Zhu 已提交
592
						SMU10_UMD_PSTATE_SOCCLK);
593 594
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxVcn,
R
Rex Zhu 已提交
595
						SMU10_UMD_PSTATE_VCE);
596 597 598 599
		break;
	case AMD_DPM_FORCED_LEVEL_AUTO:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
600
						data->gfx_min_freq_limit/100);
601 602
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
R
Rex Zhu 已提交
603
						SMU10_UMD_PSTATE_MIN_FCLK);
604 605
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinSocclkByFreq,
R
Rex Zhu 已提交
606
						SMU10_UMD_PSTATE_MIN_SOCCLK);
607 608
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinVcn,
R
Rex Zhu 已提交
609
						SMU10_UMD_PSTATE_MIN_VCE);
610 611 612

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
613
						data->gfx_max_freq_limit/100);
614 615
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
R
Rex Zhu 已提交
616
						SMU10_UMD_PSTATE_PEAK_FCLK);
617 618
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxSocclkByFreq,
R
Rex Zhu 已提交
619
						SMU10_UMD_PSTATE_PEAK_SOCCLK);
620 621
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxVcn,
R
Rex Zhu 已提交
622
						SMU10_UMD_PSTATE_VCE);
623 624 625 626
		break;
	case AMD_DPM_FORCED_LEVEL_LOW:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
627
						data->gfx_min_freq_limit/100);
628 629
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
630
						data->gfx_min_freq_limit/100);
631 632
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
R
Rex Zhu 已提交
633
						SMU10_UMD_PSTATE_MIN_FCLK);
634 635
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
R
Rex Zhu 已提交
636
						SMU10_UMD_PSTATE_MIN_FCLK);
637 638 639 640 641 642
		break;
	case AMD_DPM_FORCED_LEVEL_MANUAL:
	case AMD_DPM_FORCED_LEVEL_PROFILE_EXIT:
	default:
		break;
	}
643 644 645
	return 0;
}

R
Rex Zhu 已提交
646
static uint32_t smu10_dpm_get_mclk(struct pp_hwmgr *hwmgr, bool low)
647
{
R
Rex Zhu 已提交
648
	struct smu10_hwmgr *data;
649 650 651 652

	if (hwmgr == NULL)
		return -EINVAL;

R
Rex Zhu 已提交
653
	data = (struct smu10_hwmgr *)(hwmgr->backend);
654 655 656 657 658 659

	if (low)
		return data->clock_vol_info.vdd_dep_on_fclk->entries[0].clk;
	else
		return data->clock_vol_info.vdd_dep_on_fclk->entries[
			data->clock_vol_info.vdd_dep_on_fclk->count - 1].clk;
660 661
}

R
Rex Zhu 已提交
662
static uint32_t smu10_dpm_get_sclk(struct pp_hwmgr *hwmgr, bool low)
663
{
R
Rex Zhu 已提交
664
	struct smu10_hwmgr *data;
665 666 667 668

	if (hwmgr == NULL)
		return -EINVAL;

R
Rex Zhu 已提交
669
	data = (struct smu10_hwmgr *)(hwmgr->backend);
670 671 672 673 674

	if (low)
		return data->gfx_min_freq_limit;
	else
		return data->gfx_max_freq_limit;
675 676
}

R
Rex Zhu 已提交
677
static int smu10_dpm_patch_boot_state(struct pp_hwmgr *hwmgr,
678 679 680 681 682
					struct pp_hw_power_state *hw_ps)
{
	return 0;
}

R
Rex Zhu 已提交
683
static int smu10_dpm_get_pp_table_entry_callback(
684 685 686 687 688
						     struct pp_hwmgr *hwmgr,
					   struct pp_hw_power_state *hw_ps,
							  unsigned int index,
						     const void *clock_info)
{
R
Rex Zhu 已提交
689
	struct smu10_power_state *smu10_ps = cast_smu10_ps(hw_ps);
690

R
Rex Zhu 已提交
691
	smu10_ps->levels[index].engine_clock = 0;
692

R
Rex Zhu 已提交
693 694
	smu10_ps->levels[index].vddc_index = 0;
	smu10_ps->level = index + 1;
695 696

	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep)) {
R
Rex Zhu 已提交
697 698
		smu10_ps->levels[index].ds_divider_index = 5;
		smu10_ps->levels[index].ss_divider_index = 5;
699 700 701 702 703
	}

	return 0;
}

R
Rex Zhu 已提交
704
static int smu10_dpm_get_num_of_pp_table_entries(struct pp_hwmgr *hwmgr)
705 706 707 708 709 710 711 712 713
{
	int result;
	unsigned long ret = 0;

	result = pp_tables_get_num_of_entries(hwmgr, &ret);

	return result ? 0 : ret;
}

R
Rex Zhu 已提交
714
static int smu10_dpm_get_pp_table_entry(struct pp_hwmgr *hwmgr,
715 716 717
		    unsigned long entry, struct pp_power_state *ps)
{
	int result;
R
Rex Zhu 已提交
718
	struct smu10_power_state *smu10_ps;
719

R
Rex Zhu 已提交
720
	ps->hardware.magic = SMU10_Magic;
721

R
Rex Zhu 已提交
722
	smu10_ps = cast_smu10_ps(&(ps->hardware));
723 724

	result = pp_tables_get_entry(hwmgr, entry, ps,
R
Rex Zhu 已提交
725
			smu10_dpm_get_pp_table_entry_callback);
726

R
Rex Zhu 已提交
727 728
	smu10_ps->uvd_clocks.vclk = ps->uvd_clocks.VCLK;
	smu10_ps->uvd_clocks.dclk = ps->uvd_clocks.DCLK;
729 730 731 732

	return result;
}

R
Rex Zhu 已提交
733
static int smu10_get_power_state_size(struct pp_hwmgr *hwmgr)
734
{
R
Rex Zhu 已提交
735
	return sizeof(struct smu10_power_state);
736 737
}

R
Rex Zhu 已提交
738
static int smu10_set_cpu_power_state(struct pp_hwmgr *hwmgr)
739 740 741 742 743
{
	return 0;
}


R
Rex Zhu 已提交
744
static int smu10_store_cc6_data(struct pp_hwmgr *hwmgr, uint32_t separation_time,
745 746
			bool cc6_disable, bool pstate_disable, bool pstate_switch_disable)
{
747 748 749 750 751 752 753 754 755 756
	struct smu10_hwmgr *data = (struct smu10_hwmgr *)(hwmgr->backend);

	if (separation_time != data->separation_time ||
			cc6_disable != data->cc6_disable ||
			pstate_disable != data->pstate_disable) {
		data->separation_time = separation_time;
		data->cc6_disable = cc6_disable;
		data->pstate_disable = pstate_disable;
		data->cc6_setting_changed = true;
	}
757 758 759
	return 0;
}

R
Rex Zhu 已提交
760
static int smu10_get_dal_power_level(struct pp_hwmgr *hwmgr,
761 762 763 764 765
		struct amd_pp_simple_clock_info *info)
{
	return -EINVAL;
}

R
Rex Zhu 已提交
766
static int smu10_force_clock_level(struct pp_hwmgr *hwmgr,
767 768
		enum pp_clock_type type, uint32_t mask)
{
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	struct smu10_hwmgr *data = hwmgr->backend;
	struct smu10_voltage_dependency_table *mclk_table =
					data->clock_vol_info.vdd_dep_on_fclk;
	uint32_t low, high;

	low = mask ? (ffs(mask) - 1) : 0;
	high = mask ? (fls(mask) - 1) : 0;

	switch (type) {
	case PP_SCLK:
		if (low > 2 || high > 2) {
			pr_info("Currently sclk only support 3 levels on RV\n");
			return -EINVAL;
		}

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
						low == 2 ? data->gfx_max_freq_limit/100 :
						low == 1 ? SMU10_UMD_PSTATE_GFXCLK :
						data->gfx_min_freq_limit/100);

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
						high == 0 ? data->gfx_min_freq_limit/100 :
						high == 1 ? SMU10_UMD_PSTATE_GFXCLK :
						data->gfx_max_freq_limit/100);
		break;

	case PP_MCLK:
		if (low > mclk_table->count - 1 || high > mclk_table->count - 1)
			return -EINVAL;

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
						mclk_table->entries[low].clk/100);

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
						mclk_table->entries[high].clk/100);
		break;

	case PP_PCIE:
	default:
		break;
	}
814 815 816
	return 0;
}

R
Rex Zhu 已提交
817
static int smu10_print_clock_levels(struct pp_hwmgr *hwmgr,
818 819
		enum pp_clock_type type, char *buf)
{
R
Rex Zhu 已提交
820 821
	struct smu10_hwmgr *data = (struct smu10_hwmgr *)(hwmgr->backend);
	struct smu10_voltage_dependency_table *mclk_table =
822
			data->clock_vol_info.vdd_dep_on_fclk;
823
	uint32_t i, now, size = 0;
824 825 826

	switch (type) {
	case PP_SCLK:
827
		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_GetGfxclkFrequency);
828
		now = smum_get_argument(hwmgr);
829

830 831 832 833 834 835 836 837
	/* driver only know min/max gfx_clk, Add level 1 for all other gfx clks */
		if (now == data->gfx_max_freq_limit/100)
			i = 2;
		else if (now == data->gfx_min_freq_limit/100)
			i = 0;
		else
			i = 1;

838
		size += sprintf(buf + size, "0: %uMhz %s\n",
839 840
					data->gfx_min_freq_limit/100,
					i == 0 ? "*" : "");
841
		size += sprintf(buf + size, "1: %uMhz %s\n",
842 843 844 845 846
					i == 1 ? now : SMU10_UMD_PSTATE_GFXCLK,
					i == 1 ? "*" : "");
		size += sprintf(buf + size, "2: %uMhz %s\n",
					data->gfx_max_freq_limit/100,
					i == 2 ? "*" : "");
847 848
		break;
	case PP_MCLK:
849
		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_GetFclkFrequency);
850
		now = smum_get_argument(hwmgr);
851 852 853 854 855 856 857 858 859 860 861 862 863

		for (i = 0; i < mclk_table->count; i++)
			size += sprintf(buf + size, "%d: %uMhz %s\n",
					i,
					mclk_table->entries[i].clk / 100,
					((mclk_table->entries[i].clk / 100)
					 == now) ? "*" : "");
		break;
	default:
		break;
	}

	return size;
864 865
}

R
Rex Zhu 已提交
866
static int smu10_get_performance_level(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *state,
867 868 869
				PHM_PerformanceLevelDesignation designation, uint32_t index,
				PHM_PerformanceLevel *level)
{
R
Rex Zhu 已提交
870
	struct smu10_hwmgr *data;
871 872 873 874

	if (level == NULL || hwmgr == NULL || state == NULL)
		return -EINVAL;

R
Rex Zhu 已提交
875
	data = (struct smu10_hwmgr *)(hwmgr->backend);
876

877
	if (index == 0) {
878
		level->memory_clock = data->clock_vol_info.vdd_dep_on_fclk->entries[0].clk;
879 880 881 882 883
		level->coreClock = data->gfx_min_freq_limit;
	} else {
		level->memory_clock = data->clock_vol_info.vdd_dep_on_fclk->entries[
			data->clock_vol_info.vdd_dep_on_fclk->count - 1].clk;
		level->coreClock = data->gfx_max_freq_limit;
884
	}
885

886 887 888 889 890 891
	level->nonLocalMemoryFreq = 0;
	level->nonLocalMemoryWidth = 0;

	return 0;
}

R
Rex Zhu 已提交
892
static int smu10_get_current_shallow_sleep_clocks(struct pp_hwmgr *hwmgr,
893 894
	const struct pp_hw_power_state *state, struct pp_clock_info *clock_info)
{
R
Rex Zhu 已提交
895
	const struct smu10_power_state *ps = cast_const_smu10_ps(state);
896 897 898 899 900 901 902 903 904 905 906 907 908 909

	clock_info->min_eng_clk = ps->levels[0].engine_clock / (1 << (ps->levels[0].ss_divider_index));
	clock_info->max_eng_clk = ps->levels[ps->level - 1].engine_clock / (1 << (ps->levels[ps->level - 1].ss_divider_index));

	return 0;
}

#define MEM_FREQ_LOW_LATENCY        25000
#define MEM_FREQ_HIGH_LATENCY       80000
#define MEM_LATENCY_HIGH            245
#define MEM_LATENCY_LOW             35
#define MEM_LATENCY_ERR             0xFFFF


R
Rex Zhu 已提交
910
static uint32_t smu10_get_mem_latency(struct pp_hwmgr *hwmgr,
911 912 913 914 915 916 917 918 919 920 921
		uint32_t clock)
{
	if (clock >= MEM_FREQ_LOW_LATENCY &&
			clock < MEM_FREQ_HIGH_LATENCY)
		return MEM_LATENCY_HIGH;
	else if (clock >= MEM_FREQ_HIGH_LATENCY)
		return MEM_LATENCY_LOW;
	else
		return MEM_LATENCY_ERR;
}

R
Rex Zhu 已提交
922
static int smu10_get_clock_by_type_with_latency(struct pp_hwmgr *hwmgr,
923
		enum amd_pp_clock_type type,
924 925 926
		struct pp_clock_levels_with_latency *clocks)
{
	uint32_t i;
R
Rex Zhu 已提交
927 928 929
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
	struct smu10_clock_voltage_information *pinfo = &(smu10_data->clock_vol_info);
	struct smu10_voltage_dependency_table *pclk_vol_table;
930
	bool latency_required = false;
931

932 933
	if (pinfo == NULL)
		return -EINVAL;
934 935 936

	switch (type) {
	case amd_pp_mem_clock:
937 938
		pclk_vol_table = pinfo->vdd_dep_on_mclk;
		latency_required = true;
939
		break;
940 941 942
	case amd_pp_f_clock:
		pclk_vol_table = pinfo->vdd_dep_on_fclk;
		latency_required = true;
943
		break;
944 945
	case amd_pp_dcf_clock:
		pclk_vol_table = pinfo->vdd_dep_on_dcefclk;
946
		break;
947 948 949 950 951 952 953 954
	case amd_pp_disp_clock:
		pclk_vol_table = pinfo->vdd_dep_on_dispclk;
		break;
	case amd_pp_phy_clock:
		pclk_vol_table = pinfo->vdd_dep_on_phyclk;
		break;
	case amd_pp_dpp_clock:
		pclk_vol_table = pinfo->vdd_dep_on_dppclk;
955
	default:
956 957 958 959 960 961 962 963 964 965
		return -EINVAL;
	}

	if (pclk_vol_table == NULL || pclk_vol_table->count == 0)
		return -EINVAL;

	clocks->num_levels = 0;
	for (i = 0; i < pclk_vol_table->count; i++) {
		clocks->data[i].clocks_in_khz = pclk_vol_table->entries[i].clk;
		clocks->data[i].latency_in_us = latency_required ?
R
Rex Zhu 已提交
966
						smu10_get_mem_latency(hwmgr,
967 968 969
						pclk_vol_table->entries[i].clk) :
						0;
		clocks->num_levels++;
970 971 972 973 974
	}

	return 0;
}

R
Rex Zhu 已提交
975
static int smu10_get_clock_by_type_with_voltage(struct pp_hwmgr *hwmgr,
976 977 978 979
		enum amd_pp_clock_type type,
		struct pp_clock_levels_with_voltage *clocks)
{
	uint32_t i;
R
Rex Zhu 已提交
980 981 982
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
	struct smu10_clock_voltage_information *pinfo = &(smu10_data->clock_vol_info);
	struct smu10_voltage_dependency_table *pclk_vol_table = NULL;
983 984 985

	if (pinfo == NULL)
		return -EINVAL;
986 987 988 989 990

	switch (type) {
	case amd_pp_mem_clock:
		pclk_vol_table = pinfo->vdd_dep_on_mclk;
		break;
991 992
	case amd_pp_f_clock:
		pclk_vol_table = pinfo->vdd_dep_on_fclk;
993
		break;
994 995
	case amd_pp_dcf_clock:
		pclk_vol_table = pinfo->vdd_dep_on_dcefclk;
996
		break;
997 998
	case amd_pp_soc_clock:
		pclk_vol_table = pinfo->vdd_dep_on_socclk;
999 1000 1001 1002 1003
		break;
	default:
		return -EINVAL;
	}

1004
	if (pclk_vol_table == NULL || pclk_vol_table->count == 0)
1005 1006
		return -EINVAL;

1007
	clocks->num_levels = 0;
1008 1009 1010 1011 1012 1013 1014 1015 1016
	for (i = 0; i < pclk_vol_table->count; i++) {
		clocks->data[i].clocks_in_khz = pclk_vol_table->entries[i].clk;
		clocks->data[i].voltage_in_mv = pclk_vol_table->entries[i].vol;
		clocks->num_levels++;
	}

	return 0;
}

R
Rex Zhu 已提交
1017
static int smu10_display_clock_voltage_request(struct pp_hwmgr *hwmgr,
1018 1019
		struct pp_display_clock_request *clock_req)
{
R
Rex Zhu 已提交
1020
	struct smu10_hwmgr *smu10_data = (struct smu10_hwmgr *)(hwmgr->backend);
1021
	enum amd_pp_clock_type clk_type = clock_req->clock_type;
1022
	uint32_t clk_freq = clock_req->clock_freq_in_khz / 1000;
1023 1024 1025
	PPSMC_Msg        msg;

	switch (clk_type) {
1026
	case amd_pp_dcf_clock:
R
Rex Zhu 已提交
1027
		if (clk_freq == smu10_data->dcf_actual_hard_min_freq)
1028
			return 0;
1029
		msg =  PPSMC_MSG_SetHardMinDcefclkByFreq;
R
Rex Zhu 已提交
1030
		smu10_data->dcf_actual_hard_min_freq = clk_freq;
1031 1032 1033 1034
		break;
	case amd_pp_soc_clock:
		 msg = PPSMC_MSG_SetHardMinSocclkByFreq;
		break;
1035
	case amd_pp_f_clock:
R
Rex Zhu 已提交
1036
		if (clk_freq == smu10_data->f_actual_hard_min_freq)
1037
			return 0;
R
Rex Zhu 已提交
1038
		smu10_data->f_actual_hard_min_freq = clk_freq;
1039 1040 1041 1042 1043 1044 1045
		msg = PPSMC_MSG_SetHardMinFclkByFreq;
		break;
	default:
		pr_info("[DisplayClockVoltageRequest]Invalid Clock Type!");
		return -EINVAL;
	}

1046
	smum_send_msg_to_smc_with_parameter(hwmgr, msg, clk_freq);
1047

1048
	return 0;
1049 1050
}

R
Rex Zhu 已提交
1051
static int smu10_get_max_high_clocks(struct pp_hwmgr *hwmgr, struct amd_pp_simple_clock_info *clocks)
1052
{
1053 1054
	clocks->engine_max_clock = 80000; /* driver can't get engine clock, temp hard code to 800MHz */
	return 0;
1055 1056
}

R
Rex Zhu 已提交
1057
static int smu10_thermal_get_temperature(struct pp_hwmgr *hwmgr)
1058
{
1059 1060
	struct amdgpu_device *adev = hwmgr->adev;
	uint32_t reg_value = RREG32_SOC15(THM, 0, mmTHM_TCON_CUR_TMP);
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	int cur_temp =
		(reg_value & THM_TCON_CUR_TMP__CUR_TEMP_MASK) >> THM_TCON_CUR_TMP__CUR_TEMP__SHIFT;

	if (cur_temp & THM_TCON_CUR_TMP__CUR_TEMP_RANGE_SEL_MASK)
		cur_temp = ((cur_temp / 8) - 49) * PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
	else
		cur_temp = (cur_temp / 8) * PP_TEMPERATURE_UNITS_PER_CENTIGRADES;

	return cur_temp;
}

R
Rex Zhu 已提交
1072
static int smu10_read_sensor(struct pp_hwmgr *hwmgr, int idx,
1073 1074
			  void *value, int *size)
{
1075 1076 1077
	uint32_t sclk, mclk;
	int ret = 0;

1078
	switch (idx) {
1079
	case AMDGPU_PP_SENSOR_GFX_SCLK:
1080
		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_GetGfxclkFrequency);
1081
		sclk = smum_get_argument(hwmgr);
1082
			/* in units of 10KHZ */
1083 1084
		*((uint32_t *)value) = sclk * 100;
		*size = 4;
1085 1086
		break;
	case AMDGPU_PP_SENSOR_GFX_MCLK:
1087
		smum_send_msg_to_smc(hwmgr, PPSMC_MSG_GetFclkFrequency);
1088
		mclk = smum_get_argument(hwmgr);
1089
			/* in units of 10KHZ */
1090 1091
		*((uint32_t *)value) = mclk * 100;
		*size = 4;
1092
		break;
1093
	case AMDGPU_PP_SENSOR_GPU_TEMP:
R
Rex Zhu 已提交
1094
		*((uint32_t *)value) = smu10_thermal_get_temperature(hwmgr);
1095
		break;
1096
	default:
1097 1098
		ret = -EINVAL;
		break;
1099
	}
1100 1101

	return ret;
1102 1103
}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
static int smu10_set_watermarks_for_clocks_ranges(struct pp_hwmgr *hwmgr,
		struct pp_wm_sets_with_clock_ranges_soc15 *wm_with_clock_ranges)
{
	struct smu10_hwmgr *data = hwmgr->backend;
	Watermarks_t *table = &(data->water_marks_table);
	int result = 0;

	smu_set_watermarks_for_clocks_ranges(table,wm_with_clock_ranges);
	smum_smc_table_manager(hwmgr, (uint8_t *)table, (uint16_t)SMU10_WMTABLE, false);
	data->water_marks_exist = true;
	return result;
}
R
Rex Zhu 已提交
1116
static int smu10_set_mmhub_powergating_by_smu(struct pp_hwmgr *hwmgr)
1117 1118 1119 1120
{
	return smum_send_msg_to_smc(hwmgr, PPSMC_MSG_PowerGateMmHub);
}

R
Rex Zhu 已提交
1121 1122 1123
static const struct pp_hwmgr_func smu10_hwmgr_funcs = {
	.backend_init = smu10_hwmgr_backend_init,
	.backend_fini = smu10_hwmgr_backend_fini,
1124
	.asic_setup = NULL,
R
Rex Zhu 已提交
1125 1126 1127
	.apply_state_adjust_rules = smu10_apply_state_adjust_rules,
	.force_dpm_level = smu10_dpm_force_dpm_level,
	.get_power_state_size = smu10_get_power_state_size,
1128 1129 1130
	.powerdown_uvd = NULL,
	.powergate_uvd = NULL,
	.powergate_vce = NULL,
R
Rex Zhu 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	.get_mclk = smu10_dpm_get_mclk,
	.get_sclk = smu10_dpm_get_sclk,
	.patch_boot_state = smu10_dpm_patch_boot_state,
	.get_pp_table_entry = smu10_dpm_get_pp_table_entry,
	.get_num_of_pp_table_entries = smu10_dpm_get_num_of_pp_table_entries,
	.set_cpu_power_state = smu10_set_cpu_power_state,
	.store_cc6_data = smu10_store_cc6_data,
	.force_clock_level = smu10_force_clock_level,
	.print_clock_levels = smu10_print_clock_levels,
	.get_dal_power_level = smu10_get_dal_power_level,
	.get_performance_level = smu10_get_performance_level,
	.get_current_shallow_sleep_clocks = smu10_get_current_shallow_sleep_clocks,
	.get_clock_by_type_with_latency = smu10_get_clock_by_type_with_latency,
	.get_clock_by_type_with_voltage = smu10_get_clock_by_type_with_voltage,
1145
	.set_watermarks_for_clocks_ranges = smu10_set_watermarks_for_clocks_ranges,
R
Rex Zhu 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	.get_max_high_clocks = smu10_get_max_high_clocks,
	.read_sensor = smu10_read_sensor,
	.set_active_display_count = smu10_set_active_display_count,
	.set_deep_sleep_dcefclk = smu10_set_deep_sleep_dcefclk,
	.dynamic_state_management_enable = smu10_enable_dpm_tasks,
	.power_off_asic = smu10_power_off_asic,
	.asic_setup = smu10_setup_asic_task,
	.power_state_set = smu10_set_power_state_tasks,
	.dynamic_state_management_disable = smu10_disable_dpm_tasks,
	.set_mmhub_powergating_by_smu = smu10_set_mmhub_powergating_by_smu,
1156
	.gfx_off_control = smu10_gfx_off_control,
1157 1158
};

R
Rex Zhu 已提交
1159
int smu10_init_function_pointers(struct pp_hwmgr *hwmgr)
1160
{
R
Rex Zhu 已提交
1161
	hwmgr->hwmgr_func = &smu10_hwmgr_funcs;
1162 1163 1164
	hwmgr->pptable_func = &pptable_funcs;
	return 0;
}