cz_hwmgr.c 55.7 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
/*
 * 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 <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include "atom-types.h"
#include "atombios.h"
#include "processpptables.h"
29
#include "pp_debug.h"
30 31
#include "cgs_common.h"
#include "smu/smu_8_0_d.h"
32 33
#include "smu8_fusion.h"
#include "smu/smu_8_0_sh_mask.h"
34 35 36 37 38 39
#include "smumgr.h"
#include "hwmgr.h"
#include "hardwaremanager.h"
#include "cz_ppsmc.h"
#include "cz_hwmgr.h"
#include "power_state.h"
40
#include "cz_clockpowergating.h"
41
#include "pp_debug.h"
42 43 44 45 46 47 48

#define ixSMUSVI_NB_CURRENTVID 0xD8230044
#define CURRENT_NB_VID_MASK 0xff000000
#define CURRENT_NB_VID__SHIFT 24
#define ixSMUSVI_GFX_CURRENTVID  0xD8230048
#define CURRENT_GFX_VID_MASK 0xff000000
#define CURRENT_GFX_VID__SHIFT 24
49 50 51 52 53 54 55 56 57 58 59

static const unsigned long PhwCz_Magic = (unsigned long) PHM_Cz_Magic;

static struct cz_power_state *cast_PhwCzPowerState(struct pp_hw_power_state *hw_ps)
{
	if (PhwCz_Magic != hw_ps->magic)
		return NULL;

	return (struct cz_power_state *)hw_ps;
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73
static const struct cz_power_state *cast_const_PhwCzPowerState(
				const struct pp_hw_power_state *hw_ps)
{
	if (PhwCz_Magic != hw_ps->magic)
		return NULL;

	return (struct cz_power_state *)hw_ps;
}

uint32_t cz_get_eclk_level(struct pp_hwmgr *hwmgr,
					uint32_t clock, uint32_t msg)
{
	int i = 0;
	struct phm_vce_clock_voltage_dependency_table *ptable =
74
		hwmgr->dyn_state.vce_clock_voltage_dependency_table;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

	switch (msg) {
	case PPSMC_MSG_SetEclkSoftMin:
	case PPSMC_MSG_SetEclkHardMin:
		for (i = 0; i < (int)ptable->count; i++) {
			if (clock <= ptable->entries[i].ecclk)
				break;
		}
		break;

	case PPSMC_MSG_SetEclkSoftMax:
	case PPSMC_MSG_SetEclkHardMax:
		for (i = ptable->count - 1; i >= 0; i--) {
			if (clock >= ptable->entries[i].ecclk)
				break;
		}
		break;

	default:
		break;
	}

	return i;
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
static uint32_t cz_get_sclk_level(struct pp_hwmgr *hwmgr,
				uint32_t clock, uint32_t msg)
{
	int i = 0;
	struct phm_clock_voltage_dependency_table *table =
				hwmgr->dyn_state.vddc_dependency_on_sclk;

	switch (msg) {
	case PPSMC_MSG_SetSclkSoftMin:
	case PPSMC_MSG_SetSclkHardMin:
		for (i = 0; i < (int)table->count; i++) {
			if (clock <= table->entries[i].clk)
				break;
		}
		break;

	case PPSMC_MSG_SetSclkSoftMax:
	case PPSMC_MSG_SetSclkHardMax:
		for (i = table->count - 1; i >= 0; i--) {
			if (clock >= table->entries[i].clk)
				break;
		}
		break;

	default:
		break;
	}
	return i;
}

130 131 132 133 134
static uint32_t cz_get_uvd_level(struct pp_hwmgr *hwmgr,
					uint32_t clock, uint32_t msg)
{
	int i = 0;
	struct phm_uvd_clock_voltage_dependency_table *ptable =
135
		hwmgr->dyn_state.uvd_clock_voltage_dependency_table;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

	switch (msg) {
	case PPSMC_MSG_SetUvdSoftMin:
	case PPSMC_MSG_SetUvdHardMin:
		for (i = 0; i < (int)ptable->count; i++) {
			if (clock <= ptable->entries[i].vclk)
				break;
		}
		break;

	case PPSMC_MSG_SetUvdSoftMax:
	case PPSMC_MSG_SetUvdHardMax:
		for (i = ptable->count - 1; i >= 0; i--) {
			if (clock >= ptable->entries[i].vclk)
				break;
		}
		break;

	default:
		break;
	}

	return i;
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
static uint32_t cz_get_max_sclk_level(struct pp_hwmgr *hwmgr)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	if (cz_hwmgr->max_sclk_level == 0) {
		smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetMaxSclkLevel);
		cz_hwmgr->max_sclk_level = smum_get_argument(hwmgr->smumgr) + 1;
	}

	return cz_hwmgr->max_sclk_level;
}

static int cz_initialize_dpm_defaults(struct pp_hwmgr *hwmgr)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	uint32_t i;
177 178
	struct cgs_system_info sys_info = {0};
	int result;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

	cz_hwmgr->gfx_ramp_step = 256*25/100;
	cz_hwmgr->gfx_ramp_delay = 1; /* by default, we delay 1us */

	for (i = 0; i < CZ_MAX_HARDWARE_POWERLEVELS; i++)
		cz_hwmgr->activity_target[i] = CZ_AT_DFLT;

	cz_hwmgr->mgcg_cgtt_local0 = 0x00000000;
	cz_hwmgr->mgcg_cgtt_local1 = 0x00000000;
	cz_hwmgr->clock_slow_down_freq = 25000;
	cz_hwmgr->skip_clock_slow_down = 1;
	cz_hwmgr->enable_nb_ps_policy = 1; /* disable until UNB is ready, Enabled */
	cz_hwmgr->voltage_drop_in_dce_power_gating = 0; /* disable until fully verified */
	cz_hwmgr->voting_rights_clients = 0x00C00033;
	cz_hwmgr->static_screen_threshold = 8;
	cz_hwmgr->ddi_power_gating_disabled = 0;
	cz_hwmgr->bapm_enabled = 1;
	cz_hwmgr->voltage_drop_threshold = 0;
	cz_hwmgr->gfx_power_gating_threshold = 500;
	cz_hwmgr->vce_slow_sclk_threshold = 20000;
	cz_hwmgr->dce_slow_sclk_threshold = 30000;
	cz_hwmgr->disable_driver_thermal_policy = 1;
	cz_hwmgr->disable_nb_ps3_in_battery = 0;

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

	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
				    PHM_PlatformCaps_NonABMSupportInPPLib);

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

	cz_hwmgr->override_dynamic_mgpg = 1;

	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
				  PHM_PlatformCaps_DynamicPatchPowerState);

	cz_hwmgr->thermal_auto_throttling_treshold = 0;
	cz_hwmgr->tdr_clock = 0;
	cz_hwmgr->disable_gfx_power_gating_in_uvd = 0;

	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
					PHM_PlatformCaps_DynamicUVDState);

224 225 226 227 228
	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
			PHM_PlatformCaps_UVDDPM);
	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
			PHM_PlatformCaps_VCEDPM);

229 230 231 232
	cz_hwmgr->cc6_settings.cpu_cc6_disable = false;
	cz_hwmgr->cc6_settings.cpu_pstate_disable = false;
	cz_hwmgr->cc6_settings.nb_pstate_switch_disable = false;
	cz_hwmgr->cc6_settings.cpu_pstate_separation_time = 0;
233 234 235 236

	phm_cap_set(hwmgr->platform_descriptor.platformCaps,
				   PHM_PlatformCaps_DisableVoltageIsland);

237 238
	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
		      PHM_PlatformCaps_UVDPowerGating);
239 240
	phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
		      PHM_PlatformCaps_VCEPowerGating);
241 242 243 244 245 246 247 248 249 250 251
	sys_info.size = sizeof(struct cgs_system_info);
	sys_info.info_id = CGS_SYSTEM_INFO_PG_FLAGS;
	result = cgs_query_system_info(hwmgr->device, &sys_info);
	if (!result) {
		if (sys_info.value & AMD_PG_SUPPORT_UVD)
			phm_cap_set(hwmgr->platform_descriptor.platformCaps,
				      PHM_PlatformCaps_UVDPowerGating);
		if (sys_info.value & AMD_PG_SUPPORT_VCE)
			phm_cap_set(hwmgr->platform_descriptor.platformCaps,
				      PHM_PlatformCaps_VCEPowerGating);
	}
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	return 0;
}

static uint32_t cz_convert_8Bit_index_to_voltage(
			struct pp_hwmgr *hwmgr, uint16_t voltage)
{
	return 6200 - (voltage * 25);
}

static int cz_construct_max_power_limits_table(struct pp_hwmgr *hwmgr,
			struct phm_clock_and_voltage_limits *table)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)hwmgr->backend;
	struct cz_sys_info *sys_info = &cz_hwmgr->sys_info;
	struct phm_clock_voltage_dependency_table *dep_table =
				hwmgr->dyn_state.vddc_dependency_on_sclk;

	if (dep_table->count > 0) {
		table->sclk = dep_table->entries[dep_table->count-1].clk;
		table->vddc = cz_convert_8Bit_index_to_voltage(hwmgr,
		   (uint16_t)dep_table->entries[dep_table->count-1].v);
	}
	table->mclk = sys_info->nbp_memory_clock[0];
	return 0;
}

static int cz_init_dynamic_state_adjustment_rule_settings(
			struct pp_hwmgr *hwmgr,
			ATOM_CLK_VOLT_CAPABILITY *disp_voltage_table)
{
	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) {
		printk(KERN_ERR "[ powerplay ] 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;
}

static int cz_get_system_info_data(struct pp_hwmgr *hwmgr)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)hwmgr->backend;
	ATOM_INTEGRATED_SYSTEM_INFO_V1_9 *info = NULL;
	uint32_t i;
	int result = 0;
	uint8_t frev, crev;
	uint16_t size;

	info = (ATOM_INTEGRATED_SYSTEM_INFO_V1_9 *) cgs_atom_get_data_table(
			hwmgr->device,
			GetIndexIntoMasterTable(DATA, IntegratedSystemInfo),
			&size, &frev, &crev);

	if (crev != 9) {
		printk(KERN_ERR "[ powerplay ] Unsupported IGP table: %d %d\n", frev, crev);
		return -EINVAL;
	}

	if (info == NULL) {
		printk(KERN_ERR "[ powerplay ] Could not retrieve the Integrated System Info Table!\n");
		return -EINVAL;
	}

	cz_hwmgr->sys_info.bootup_uma_clock =
				   le32_to_cpu(info->ulBootUpUMAClock);

	cz_hwmgr->sys_info.bootup_engine_clock =
				le32_to_cpu(info->ulBootUpEngineClock);

	cz_hwmgr->sys_info.dentist_vco_freq =
				   le32_to_cpu(info->ulDentistVCOFreq);

	cz_hwmgr->sys_info.system_config =
				     le32_to_cpu(info->ulSystemConfig);

	cz_hwmgr->sys_info.bootup_nb_voltage_index =
				  le16_to_cpu(info->usBootUpNBVoltage);

	cz_hwmgr->sys_info.htc_hyst_lmt =
			(info->ucHtcHystLmt == 0) ? 5 : info->ucHtcHystLmt;

	cz_hwmgr->sys_info.htc_tmp_lmt =
			(info->ucHtcTmpLmt == 0) ? 203 : info->ucHtcTmpLmt;

	if (cz_hwmgr->sys_info.htc_tmp_lmt <=
			cz_hwmgr->sys_info.htc_hyst_lmt) {
		printk(KERN_ERR "[ powerplay ] The htcTmpLmt should be larger than htcHystLmt.\n");
		return -EINVAL;
	}

	cz_hwmgr->sys_info.nb_dpm_enable =
				cz_hwmgr->enable_nb_ps_policy &&
				(le32_to_cpu(info->ulSystemConfig) >> 3 & 0x1);

	for (i = 0; i < CZ_NUM_NBPSTATES; i++) {
		if (i < CZ_NUM_NBPMEMORYCLOCK) {
			cz_hwmgr->sys_info.nbp_memory_clock[i] =
			  le32_to_cpu(info->ulNbpStateMemclkFreq[i]);
		}
		cz_hwmgr->sys_info.nbp_n_clock[i] =
			    le32_to_cpu(info->ulNbpStateNClkFreq[i]);
	}

	for (i = 0; i < MAX_DISPLAY_CLOCK_LEVEL; i++) {
		cz_hwmgr->sys_info.display_clock[i] =
					le32_to_cpu(info->sDispClkVoltageMapping[i].ulMaximumSupportedCLK);
	}

	/* Here use 4 levels, make sure not exceed */
	for (i = 0; i < CZ_NUM_NBPSTATES; i++) {
		cz_hwmgr->sys_info.nbp_voltage_index[i] =
			     le16_to_cpu(info->usNBPStateVoltage[i]);
	}

	if (!cz_hwmgr->sys_info.nb_dpm_enable) {
		for (i = 1; i < CZ_NUM_NBPSTATES; i++) {
			if (i < CZ_NUM_NBPMEMORYCLOCK) {
				cz_hwmgr->sys_info.nbp_memory_clock[i] =
				    cz_hwmgr->sys_info.nbp_memory_clock[0];
			}
			cz_hwmgr->sys_info.nbp_n_clock[i] =
				    cz_hwmgr->sys_info.nbp_n_clock[0];
			cz_hwmgr->sys_info.nbp_voltage_index[i] =
				    cz_hwmgr->sys_info.nbp_voltage_index[0];
		}
	}

	if (le32_to_cpu(info->ulGPUCapInfo) &
		SYS_INFO_GPUCAPS__ENABEL_DFS_BYPASS) {
		phm_cap_set(hwmgr->platform_descriptor.platformCaps,
				    PHM_PlatformCaps_EnableDFSBypass);
	}

	cz_hwmgr->sys_info.uma_channel_number = info->ucUMAChannelNumber;

	cz_construct_max_power_limits_table (hwmgr,
				    &hwmgr->dyn_state.max_clock_voltage_on_ac);

	cz_init_dynamic_state_adjustment_rule_settings(hwmgr,
				    &info->sDISPCLK_Voltage[0]);

	return result;
}

static int cz_construct_boot_state(struct pp_hwmgr *hwmgr)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	cz_hwmgr->boot_power_level.engineClock =
				cz_hwmgr->sys_info.bootup_engine_clock;

	cz_hwmgr->boot_power_level.vddcIndex =
			(uint8_t)cz_hwmgr->sys_info.bootup_nb_voltage_index;

	cz_hwmgr->boot_power_level.dsDividerIndex = 0;
	cz_hwmgr->boot_power_level.ssDividerIndex = 0;
	cz_hwmgr->boot_power_level.allowGnbSlow = 1;
	cz_hwmgr->boot_power_level.forceNBPstate = 0;
	cz_hwmgr->boot_power_level.hysteresis_up = 0;
	cz_hwmgr->boot_power_level.numSIMDToPowerDown = 0;
	cz_hwmgr->boot_power_level.display_wm = 0;
	cz_hwmgr->boot_power_level.vce_wm = 0;

	return 0;
}

static int cz_tf_reset_active_process_mask(struct pp_hwmgr *hwmgr, void *input,
					void *output, void *storage, int result)
{
	return 0;
}

static int cz_tf_upload_pptable_to_smu(struct pp_hwmgr *hwmgr, void *input,
451
				       void *output, void *storage, int result)
452
{
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
	struct SMU8_Fusion_ClkTable *clock_table;
	int ret;
	uint32_t i;
	void *table = NULL;
	pp_atomctrl_clock_dividers_kong dividers;

	struct phm_clock_voltage_dependency_table *vddc_table =
		hwmgr->dyn_state.vddc_dependency_on_sclk;
	struct phm_clock_voltage_dependency_table *vdd_gfx_table =
		hwmgr->dyn_state.vdd_gfx_dependency_on_sclk;
	struct phm_acp_clock_voltage_dependency_table *acp_table =
		hwmgr->dyn_state.acp_clock_voltage_dependency_table;
	struct phm_uvd_clock_voltage_dependency_table *uvd_table =
		hwmgr->dyn_state.uvd_clock_voltage_dependency_table;
	struct phm_vce_clock_voltage_dependency_table *vce_table =
		hwmgr->dyn_state.vce_clock_voltage_dependency_table;

	if (!hwmgr->need_pp_table_upload)
		return 0;

	ret = smum_download_powerplay_table(hwmgr->smumgr, &table);

	PP_ASSERT_WITH_CODE((0 == ret && NULL != table),
			    "Fail to get clock table from SMU!", return -EINVAL;);

	clock_table = (struct SMU8_Fusion_ClkTable *)table;

	/* patch clock table */
	PP_ASSERT_WITH_CODE((vddc_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
			    "Dependency table entry exceeds max limit!", return -EINVAL;);
	PP_ASSERT_WITH_CODE((vdd_gfx_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
			    "Dependency table entry exceeds max limit!", return -EINVAL;);
	PP_ASSERT_WITH_CODE((acp_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
			    "Dependency table entry exceeds max limit!", return -EINVAL;);
	PP_ASSERT_WITH_CODE((uvd_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
			    "Dependency table entry exceeds max limit!", return -EINVAL;);
	PP_ASSERT_WITH_CODE((vce_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
			    "Dependency table entry exceeds max limit!", return -EINVAL;);

	for (i = 0; i < CZ_MAX_HARDWARE_POWERLEVELS; i++) {

		/* vddc_sclk */
		clock_table->SclkBreakdownTable.ClkLevel[i].GnbVid =
			(i < vddc_table->count) ? (uint8_t)vddc_table->entries[i].v : 0;
		clock_table->SclkBreakdownTable.ClkLevel[i].Frequency =
			(i < vddc_table->count) ? vddc_table->entries[i].clk : 0;

		atomctrl_get_engine_pll_dividers_kong(hwmgr,
						      clock_table->SclkBreakdownTable.ClkLevel[i].Frequency,
						      &dividers);

		clock_table->SclkBreakdownTable.ClkLevel[i].DfsDid =
			(uint8_t)dividers.pll_post_divider;

		/* vddgfx_sclk */
		clock_table->SclkBreakdownTable.ClkLevel[i].GfxVid =
			(i < vdd_gfx_table->count) ? (uint8_t)vdd_gfx_table->entries[i].v : 0;

		/* acp breakdown */
		clock_table->AclkBreakdownTable.ClkLevel[i].GfxVid =
			(i < acp_table->count) ? (uint8_t)acp_table->entries[i].v : 0;
		clock_table->AclkBreakdownTable.ClkLevel[i].Frequency =
			(i < acp_table->count) ? acp_table->entries[i].acpclk : 0;

		atomctrl_get_engine_pll_dividers_kong(hwmgr,
						      clock_table->AclkBreakdownTable.ClkLevel[i].Frequency,
						      &dividers);

		clock_table->AclkBreakdownTable.ClkLevel[i].DfsDid =
			(uint8_t)dividers.pll_post_divider;


		/* uvd breakdown */
		clock_table->VclkBreakdownTable.ClkLevel[i].GfxVid =
			(i < uvd_table->count) ? (uint8_t)uvd_table->entries[i].v : 0;
		clock_table->VclkBreakdownTable.ClkLevel[i].Frequency =
			(i < uvd_table->count) ? uvd_table->entries[i].vclk : 0;

		atomctrl_get_engine_pll_dividers_kong(hwmgr,
						      clock_table->VclkBreakdownTable.ClkLevel[i].Frequency,
						      &dividers);

		clock_table->VclkBreakdownTable.ClkLevel[i].DfsDid =
			(uint8_t)dividers.pll_post_divider;

		clock_table->DclkBreakdownTable.ClkLevel[i].GfxVid =
			(i < uvd_table->count) ? (uint8_t)uvd_table->entries[i].v : 0;
		clock_table->DclkBreakdownTable.ClkLevel[i].Frequency =
			(i < uvd_table->count) ? uvd_table->entries[i].dclk : 0;

		atomctrl_get_engine_pll_dividers_kong(hwmgr,
						      clock_table->DclkBreakdownTable.ClkLevel[i].Frequency,
						      &dividers);

		clock_table->DclkBreakdownTable.ClkLevel[i].DfsDid =
			(uint8_t)dividers.pll_post_divider;

		/* vce breakdown */
		clock_table->EclkBreakdownTable.ClkLevel[i].GfxVid =
			(i < vce_table->count) ? (uint8_t)vce_table->entries[i].v : 0;
		clock_table->EclkBreakdownTable.ClkLevel[i].Frequency =
			(i < vce_table->count) ? vce_table->entries[i].ecclk : 0;


		atomctrl_get_engine_pll_dividers_kong(hwmgr,
						      clock_table->EclkBreakdownTable.ClkLevel[i].Frequency,
						      &dividers);

		clock_table->EclkBreakdownTable.ClkLevel[i].DfsDid =
			(uint8_t)dividers.pll_post_divider;

	}
	ret = smum_upload_powerplay_table(hwmgr->smumgr);

	return ret;
568 569 570 571 572 573 574 575 576 577
}

static int cz_tf_init_sclk_limit(struct pp_hwmgr *hwmgr, void *input,
				 void *output, void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	struct phm_clock_voltage_dependency_table *table =
					hwmgr->dyn_state.vddc_dependency_on_sclk;
	unsigned long clock = 0, level;

578
	if (NULL == table || table->count <= 0)
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
		return -EINVAL;

	cz_hwmgr->sclk_dpm.soft_min_clk = table->entries[0].clk;
	cz_hwmgr->sclk_dpm.hard_min_clk = table->entries[0].clk;

	level = cz_get_max_sclk_level(hwmgr) - 1;

	if (level < table->count)
		clock = table->entries[level].clk;
	else
		clock = table->entries[table->count - 1].clk;

	cz_hwmgr->sclk_dpm.soft_max_clk = clock;
	cz_hwmgr->sclk_dpm.hard_max_clk = clock;

	return 0;
}

static int cz_tf_init_uvd_limit(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	struct phm_uvd_clock_voltage_dependency_table *table =
602
				hwmgr->dyn_state.uvd_clock_voltage_dependency_table;
603 604
	unsigned long clock = 0, level;

605
	if (NULL == table || table->count <= 0)
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
		return -EINVAL;

	cz_hwmgr->uvd_dpm.soft_min_clk = 0;
	cz_hwmgr->uvd_dpm.hard_min_clk = 0;

	smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetMaxUvdLevel);
	level = smum_get_argument(hwmgr->smumgr);

	if (level < table->count)
		clock = table->entries[level].vclk;
	else
		clock = table->entries[table->count - 1].vclk;

	cz_hwmgr->uvd_dpm.soft_max_clk = clock;
	cz_hwmgr->uvd_dpm.hard_max_clk = clock;

	return 0;
}

static int cz_tf_init_vce_limit(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	struct phm_vce_clock_voltage_dependency_table *table =
630
				hwmgr->dyn_state.vce_clock_voltage_dependency_table;
631 632
	unsigned long clock = 0, level;

633
	if (NULL == table || table->count <= 0)
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
		return -EINVAL;

	cz_hwmgr->vce_dpm.soft_min_clk = 0;
	cz_hwmgr->vce_dpm.hard_min_clk = 0;

	smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetMaxEclkLevel);
	level = smum_get_argument(hwmgr->smumgr);

	if (level < table->count)
		clock = table->entries[level].ecclk;
	else
		clock = table->entries[table->count - 1].ecclk;

	cz_hwmgr->vce_dpm.soft_max_clk = clock;
	cz_hwmgr->vce_dpm.hard_max_clk = clock;

	return 0;
}

static int cz_tf_init_acp_limit(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	struct phm_acp_clock_voltage_dependency_table *table =
				hwmgr->dyn_state.acp_clock_voltage_dependency_table;
	unsigned long clock = 0, level;

661
	if (NULL == table || table->count <= 0)
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
		return -EINVAL;

	cz_hwmgr->acp_dpm.soft_min_clk = 0;
	cz_hwmgr->acp_dpm.hard_min_clk = 0;

	smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetMaxAclkLevel);
	level = smum_get_argument(hwmgr->smumgr);

	if (level < table->count)
		clock = table->entries[level].acpclk;
	else
		clock = table->entries[table->count - 1].acpclk;

	cz_hwmgr->acp_dpm.soft_max_clk = clock;
	cz_hwmgr->acp_dpm.hard_max_clk = clock;
	return 0;
}

static int cz_tf_init_power_gate_state(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	cz_hwmgr->uvd_power_gated = false;
	cz_hwmgr->vce_power_gated = false;
	cz_hwmgr->samu_power_gated = false;
	cz_hwmgr->acp_power_gated = false;
	cz_hwmgr->pgacpinit = true;

	return 0;
}

static int cz_tf_init_sclk_threshold(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	cz_hwmgr->low_sclk_interrupt_threshold = 0;

	return 0;
}
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
static int cz_tf_update_sclk_limit(struct pp_hwmgr *hwmgr,
					void *input, void *output,
					void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	struct phm_clock_voltage_dependency_table *table =
					hwmgr->dyn_state.vddc_dependency_on_sclk;

	unsigned long clock = 0;
	unsigned long level;
	unsigned long stable_pstate_sclk;
	unsigned long percentage;

	cz_hwmgr->sclk_dpm.soft_min_clk = table->entries[0].clk;
	level = cz_get_max_sclk_level(hwmgr) - 1;

	if (level < table->count)
		cz_hwmgr->sclk_dpm.soft_max_clk  = table->entries[level].clk;
	else
		cz_hwmgr->sclk_dpm.soft_max_clk  = table->entries[table->count - 1].clk;

724 725
	clock = hwmgr->display_config.min_core_set_clock;
	if (clock == 0)
726
		printk(KERN_INFO "[ powerplay ] min_core_set_clock not set\n");
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782

	if (cz_hwmgr->sclk_dpm.hard_min_clk != clock) {
		cz_hwmgr->sclk_dpm.hard_min_clk = clock;

		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
						PPSMC_MSG_SetSclkHardMin,
						 cz_get_sclk_level(hwmgr,
					cz_hwmgr->sclk_dpm.hard_min_clk,
					     PPSMC_MSG_SetSclkHardMin));
	}

	clock = cz_hwmgr->sclk_dpm.soft_min_clk;

	/* update minimum clocks for Stable P-State feature */
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
				     PHM_PlatformCaps_StablePState)) {
		percentage = 75;
		/*Sclk - calculate sclk value based on percentage and find FLOOR sclk from VddcDependencyOnSCLK table  */
		stable_pstate_sclk = (hwmgr->dyn_state.max_clock_voltage_on_ac.mclk *
					percentage) / 100;

		if (clock < stable_pstate_sclk)
			clock = stable_pstate_sclk;
	} else {
		if (clock < hwmgr->gfx_arbiter.sclk)
			clock = hwmgr->gfx_arbiter.sclk;
	}

	if (cz_hwmgr->sclk_dpm.soft_min_clk != clock) {
		cz_hwmgr->sclk_dpm.soft_min_clk = clock;
		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
						PPSMC_MSG_SetSclkSoftMin,
						cz_get_sclk_level(hwmgr,
					cz_hwmgr->sclk_dpm.soft_min_clk,
					     PPSMC_MSG_SetSclkSoftMin));
	}

	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
				    PHM_PlatformCaps_StablePState) &&
			 cz_hwmgr->sclk_dpm.soft_max_clk != clock) {
		cz_hwmgr->sclk_dpm.soft_max_clk = clock;
		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
						PPSMC_MSG_SetSclkSoftMax,
						cz_get_sclk_level(hwmgr,
					cz_hwmgr->sclk_dpm.soft_max_clk,
					PPSMC_MSG_SetSclkSoftMax));
	}

	return 0;
}

static int cz_tf_set_deep_sleep_sclk_threshold(struct pp_hwmgr *hwmgr,
					void *input, void *output,
					void *storage, int result)
{
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
783 784 785 786 787
				PHM_PlatformCaps_SclkDeepSleep)) {
		uint32_t clks = hwmgr->display_config.min_core_set_clock_in_sr;
		if (clks == 0)
			clks = CZ_MIN_DEEP_SLEEP_SCLK;

788 789
		PP_DBG_LOG("Setting Deep Sleep Clock: %d\n", clks);

790
		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
791 792
				PPSMC_MSG_SetMinDeepSleepSclk,
				clks);
793 794 795 796 797 798 799 800 801 802 803 804 805 806
	}

	return 0;
}

static int cz_tf_set_watermark_threshold(struct pp_hwmgr *hwmgr,
					void *input, void *output,
					void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr =
				  (struct cz_hwmgr *)(hwmgr->backend);

	smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
					PPSMC_MSG_SetWatermarkFrequency,
807
					cz_hwmgr->sclk_dpm.soft_max_clk);
808 809 810 811 812 813 814 815 816 817 818

	return 0;
}

static int cz_tf_set_enabled_levels(struct pp_hwmgr *hwmgr,
					void *input, void *output,
					void *storage, int result)
{
	return 0;
}

819

820 821 822 823 824
static int cz_tf_enable_nb_dpm(struct pp_hwmgr *hwmgr,
					void *input, void *output,
					void *storage, int result)
{
	int ret = 0;
825

826
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
827 828
	unsigned long dpm_features = 0;

829
	if (!cz_hwmgr->is_nb_dpm_enabled) {
830
		PP_DBG_LOG("enabling ALL SMU features.\n");
831 832
		dpm_features |= NB_DPM_MASK;
		ret = smum_send_msg_to_smc_with_parameter(
833 834 835
							  hwmgr->smumgr,
							  PPSMC_MSG_EnableAllSmuFeatures,
							  dpm_features);
836 837 838
		if (ret == 0)
			cz_hwmgr->is_nb_dpm_enabled = true;
	}
839

840 841 842
	return ret;
}

843 844 845 846 847
static int cz_nbdpm_pstate_enable_disable(struct pp_hwmgr *hwmgr, bool enable, bool lock)
{
	struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);

	if (hw_data->is_nb_dpm_enabled) {
848 849 850
		if (enable) {
			PP_DBG_LOG("enable Low Memory PState.\n");

851 852 853
			return smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
						PPSMC_MSG_EnableLowMemoryPstate,
						(lock ? 1 : 0));
854 855 856
		} else {
			PP_DBG_LOG("disable Low Memory PState.\n");

857 858 859
			return smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
						PPSMC_MSG_DisableLowMemoryPstate,
						(lock ? 1 : 0));
860
		}
861 862 863 864 865
	}

	return 0;
}

866 867 868 869
static int cz_tf_update_low_mem_pstate(struct pp_hwmgr *hwmgr,
					void *input, void *output,
					void *storage, int result)
{
870 871 872
	bool disable_switch;
	bool enable_low_mem_state;
	struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
873 874 875
	const struct phm_set_power_state_input *states = (struct phm_set_power_state_input *)input;
	const struct cz_power_state *pnew_state = cast_const_PhwCzPowerState(states->pnew_state);

876
	if (hw_data->sys_info.nb_dpm_enable) {
877 878
		disable_switch = hw_data->cc6_settings.nb_pstate_switch_disable ? true : false;
		enable_low_mem_state = hw_data->cc6_settings.nb_pstate_switch_disable ? false : true;
879

880
		if (pnew_state->action == FORCE_HIGH)
881
			cz_nbdpm_pstate_enable_disable(hwmgr, false, disable_switch);
882
		else if (pnew_state->action == CANCEL_FORCE_HIGH)
883
			cz_nbdpm_pstate_enable_disable(hwmgr, true, disable_switch);
884
		else
885
			cz_nbdpm_pstate_enable_disable(hwmgr, enable_low_mem_state, disable_switch);
886 887 888 889
	}
	return 0;
}

890
static const struct phm_master_table_item cz_set_power_state_list[] = {
891 892 893 894 895 896 897 898 899
	{NULL, cz_tf_update_sclk_limit},
	{NULL, cz_tf_set_deep_sleep_sclk_threshold},
	{NULL, cz_tf_set_watermark_threshold},
	{NULL, cz_tf_set_enabled_levels},
	{NULL, cz_tf_enable_nb_dpm},
	{NULL, cz_tf_update_low_mem_pstate},
	{NULL, NULL}
};

900
static const struct phm_master_table_header cz_set_power_state_master = {
901 902 903 904
	0,
	PHM_MasterTableFlag_None,
	cz_set_power_state_list
};
905

906
static const struct phm_master_table_item cz_setup_asic_list[] = {
907 908 909 910 911 912 913 914 915 916 917
	{NULL, cz_tf_reset_active_process_mask},
	{NULL, cz_tf_upload_pptable_to_smu},
	{NULL, cz_tf_init_sclk_limit},
	{NULL, cz_tf_init_uvd_limit},
	{NULL, cz_tf_init_vce_limit},
	{NULL, cz_tf_init_acp_limit},
	{NULL, cz_tf_init_power_gate_state},
	{NULL, cz_tf_init_sclk_threshold},
	{NULL, NULL}
};

918
static const struct phm_master_table_header cz_setup_asic_master = {
919 920 921 922 923
	0,
	PHM_MasterTableFlag_None,
	cz_setup_asic_list
};

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
static int cz_tf_power_up_display_clock_sys_pll(struct pp_hwmgr *hwmgr,
					void *input, void *output,
					void *storage, int result)
{
	struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
	hw_data->disp_clk_bypass_pending = false;
	hw_data->disp_clk_bypass = false;

	return 0;
}

static int cz_tf_clear_nb_dpm_flag(struct pp_hwmgr *hwmgr,
					void *input, void *output,
					void *storage, int result)
{
	struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
	hw_data->is_nb_dpm_enabled = false;

	return 0;
}

static int cz_tf_reset_cc6_data(struct pp_hwmgr *hwmgr,
					void *input, void *output,
					void *storage, int result)
{
	struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);

	hw_data->cc6_settings.cc6_setting_changed = false;
	hw_data->cc6_settings.cpu_pstate_separation_time = 0;
	hw_data->cc6_settings.cpu_cc6_disable = false;
	hw_data->cc6_settings.cpu_pstate_disable = false;

	return 0;
}

959
static const struct phm_master_table_item cz_power_down_asic_list[] = {
960 961 962 963 964 965
	{NULL, cz_tf_power_up_display_clock_sys_pll},
	{NULL, cz_tf_clear_nb_dpm_flag},
	{NULL, cz_tf_reset_cc6_data},
	{NULL, NULL}
};

966
static const struct phm_master_table_header cz_power_down_asic_master = {
967 968 969 970 971
	0,
	PHM_MasterTableFlag_None,
	cz_power_down_asic_list
};

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
static int cz_tf_program_voting_clients(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	PHMCZ_WRITE_SMC_REGISTER(hwmgr->device, CG_FREQ_TRAN_VOTING_0,
				PPCZ_VOTINGRIGHTSCLIENTS_DFLT0);
	return 0;
}

static int cz_tf_start_dpm(struct pp_hwmgr *hwmgr, void *input, void *output,
			   void *storage, int result)
{
	int res = 0xff;
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	unsigned long dpm_features = 0;

	cz_hwmgr->dpm_flags |= DPMFlags_SCLK_Enabled;
	dpm_features |= SCLK_DPM_MASK;

	res = smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
				PPSMC_MSG_EnableAllSmuFeatures,
				dpm_features);

	return res;
}

static int cz_tf_program_bootup_state(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	cz_hwmgr->sclk_dpm.soft_min_clk = cz_hwmgr->sys_info.bootup_engine_clock;
	cz_hwmgr->sclk_dpm.soft_max_clk = cz_hwmgr->sys_info.bootup_engine_clock;

	smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
				PPSMC_MSG_SetSclkSoftMin,
				cz_get_sclk_level(hwmgr,
				cz_hwmgr->sclk_dpm.soft_min_clk,
				PPSMC_MSG_SetSclkSoftMin));

	smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
				PPSMC_MSG_SetSclkSoftMax,
				cz_get_sclk_level(hwmgr,
				cz_hwmgr->sclk_dpm.soft_max_clk,
				PPSMC_MSG_SetSclkSoftMax));

	return 0;
}

int cz_tf_reset_acp_boot_level(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	cz_hwmgr->acp_boot_level = 0xff;
	return 0;
}

static bool cz_dpm_check_smu_features(struct pp_hwmgr *hwmgr,
				unsigned long check_feature)
{
	int result;
	unsigned long features;

	result = smum_send_msg_to_smc_with_parameter(hwmgr->smumgr, PPSMC_MSG_GetFeatureStatus, 0);
	if (result == 0) {
		features = smum_get_argument(hwmgr->smumgr);
		if (features & check_feature)
			return true;
	}

	return result;
}

static int cz_tf_check_for_dpm_disabled(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	if (cz_dpm_check_smu_features(hwmgr, SMU_EnabledFeatureScoreboard_SclkDpmOn))
		return PP_Result_TableImmediateExit;
	return 0;
}

static int cz_tf_enable_didt(struct pp_hwmgr *hwmgr, void *input,
				void *output, void *storage, int result)
{
	/* TO DO */
	return 0;
}

static int cz_tf_check_for_dpm_enabled(struct pp_hwmgr *hwmgr,
						void *input, void *output,
						void *storage, int result)
{
	if (!cz_dpm_check_smu_features(hwmgr,
			     SMU_EnabledFeatureScoreboard_SclkDpmOn))
		return PP_Result_TableImmediateExit;
	return 0;
}

1070
static const struct phm_master_table_item cz_disable_dpm_list[] = {
1071 1072 1073 1074 1075
	{ NULL, cz_tf_check_for_dpm_enabled},
	{NULL, NULL},
};


1076
static const struct phm_master_table_header cz_disable_dpm_master = {
1077 1078 1079 1080 1081
	0,
	PHM_MasterTableFlag_None,
	cz_disable_dpm_list
};

1082
static const struct phm_master_table_item cz_enable_dpm_list[] = {
1083 1084 1085 1086 1087 1088 1089 1090 1091
	{ NULL, cz_tf_check_for_dpm_disabled },
	{ NULL, cz_tf_program_voting_clients },
	{ NULL, cz_tf_start_dpm},
	{ NULL, cz_tf_program_bootup_state},
	{ NULL, cz_tf_enable_didt },
	{ NULL, cz_tf_reset_acp_boot_level },
	{NULL, NULL},
};

1092
static const struct phm_master_table_header cz_enable_dpm_master = {
1093 1094 1095 1096 1097
	0,
	PHM_MasterTableFlag_None,
	cz_enable_dpm_list
};

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
static int cz_apply_state_adjust_rules(struct pp_hwmgr *hwmgr,
				struct pp_power_state  *prequest_ps,
			const struct pp_power_state *pcurrent_ps)
{
	struct cz_power_state *cz_ps =
				cast_PhwCzPowerState(&prequest_ps->hardware);

	const struct cz_power_state *cz_current_ps =
				cast_const_PhwCzPowerState(&pcurrent_ps->hardware);

	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
1109
	struct PP_Clocks clocks = {0, 0, 0, 0};
1110
	bool force_high;
1111 1112
	uint32_t  num_of_active_displays = 0;
	struct cgs_display_info info = {0};
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123

	cz_ps->evclk = hwmgr->vce_arbiter.evclk;
	cz_ps->ecclk = hwmgr->vce_arbiter.ecclk;

	cz_ps->need_dfs_bypass = true;

	cz_hwmgr->video_start = (hwmgr->uvd_arbiter.vclk != 0 || hwmgr->uvd_arbiter.dclk != 0 ||
				hwmgr->vce_arbiter.evclk != 0 || hwmgr->vce_arbiter.ecclk != 0);

	cz_hwmgr->battery_state = (PP_StateUILabel_Battery == prequest_ps->classification.ui_label);

1124 1125
	clocks.memoryClock = hwmgr->display_config.min_mem_set_clock != 0 ?
				hwmgr->display_config.min_mem_set_clock :
1126 1127 1128 1129 1130
				cz_hwmgr->sys_info.nbp_memory_clock[1];

	cgs_get_active_displays_info(hwmgr->device, &info);
	num_of_active_displays = info.display_count;

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_StablePState))
		clocks.memoryClock = hwmgr->dyn_state.max_clock_voltage_on_ac.mclk;

	if (clocks.memoryClock < hwmgr->gfx_arbiter.mclk)
		clocks.memoryClock = hwmgr->gfx_arbiter.mclk;

	force_high = (clocks.memoryClock > cz_hwmgr->sys_info.nbp_memory_clock[CZ_NUM_NBPMEMORYCLOCK - 1])
			|| (num_of_active_displays >= 3);

	cz_ps->action = cz_current_ps->action;

1142
	if (!force_high && (cz_ps->action == FORCE_HIGH))
1143
		cz_ps->action = CANCEL_FORCE_HIGH;
1144
	else if (force_high && (cz_ps->action != FORCE_HIGH))
1145 1146 1147 1148 1149 1150 1151
		cz_ps->action = FORCE_HIGH;
	else
		cz_ps->action = DO_NOTHING;

	return 0;
}

1152 1153 1154
static int cz_hwmgr_backend_init(struct pp_hwmgr *hwmgr)
{
	int result = 0;
1155 1156 1157 1158 1159 1160 1161
	struct cz_hwmgr *data;

	data = kzalloc(sizeof(struct cz_hwmgr), GFP_KERNEL);
	if (data == NULL)
		return -ENOMEM;

	hwmgr->backend = data;
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

	result = cz_initialize_dpm_defaults(hwmgr);
	if (result != 0) {
		printk(KERN_ERR "[ powerplay ] cz_initialize_dpm_defaults failed\n");
		return result;
	}

	result = cz_get_system_info_data(hwmgr);
	if (result != 0) {
		printk(KERN_ERR "[ powerplay ] cz_get_system_info_data failed\n");
		return result;
	}

	cz_construct_boot_state(hwmgr);

	result = phm_construct_table(hwmgr, &cz_setup_asic_master,
				&(hwmgr->setup_asic));
	if (result != 0) {
		printk(KERN_ERR "[ powerplay ] Fail to construct setup ASIC\n");
		return result;
	}

1184 1185 1186 1187 1188 1189 1190
	result = phm_construct_table(hwmgr, &cz_power_down_asic_master,
				&(hwmgr->power_down_asic));
	if (result != 0) {
		printk(KERN_ERR "[ powerplay ] Fail to construct power down ASIC\n");
		return result;
	}

1191 1192
	result = phm_construct_table(hwmgr, &cz_disable_dpm_master,
				&(hwmgr->disable_dynamic_state_management));
1193 1194 1195 1196
	if (result != 0) {
		printk(KERN_ERR "[ powerplay ] Fail to disable_dynamic_state\n");
		return result;
	}
1197 1198
	result = phm_construct_table(hwmgr, &cz_enable_dpm_master,
				&(hwmgr->enable_dynamic_state_management));
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
	if (result != 0) {
		printk(KERN_ERR "[ powerplay ] Fail to enable_dynamic_state\n");
		return result;
	}
	result = phm_construct_table(hwmgr, &cz_set_power_state_master,
				&(hwmgr->set_power_state));
	if (result != 0) {
		printk(KERN_ERR "[ powerplay ] Fail to construct set_power_state\n");
		return result;
	}
1209
	hwmgr->platform_descriptor.hardwareActivityPerformanceLevels =  CZ_MAX_HARDWARE_POWERLEVELS;
1210

1211 1212 1213 1214 1215
	result = phm_construct_table(hwmgr, &cz_phm_enable_clock_power_gatings_master, &(hwmgr->enable_clock_power_gatings));
	if (result != 0) {
		printk(KERN_ERR "[ powerplay ] Fail to construct enable_clock_power_gatings\n");
		return result;
	}
1216 1217 1218 1219 1220
	return result;
}

static int cz_hwmgr_backend_fini(struct pp_hwmgr *hwmgr)
{
1221
	if (hwmgr != NULL && hwmgr->backend != NULL) {
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
		kfree(hwmgr->backend);
		kfree(hwmgr);
	}
	return 0;
}

int cz_phm_force_dpm_highest(struct pp_hwmgr *hwmgr)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	if (cz_hwmgr->sclk_dpm.soft_min_clk !=
				cz_hwmgr->sclk_dpm.soft_max_clk)
		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
						PPSMC_MSG_SetSclkSoftMin,
						cz_get_sclk_level(hwmgr,
						cz_hwmgr->sclk_dpm.soft_max_clk,
						PPSMC_MSG_SetSclkSoftMin));
	return 0;
}

int cz_phm_unforce_dpm_levels(struct pp_hwmgr *hwmgr)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	struct phm_clock_voltage_dependency_table *table =
				hwmgr->dyn_state.vddc_dependency_on_sclk;
	unsigned long clock = 0, level;

1249
	if (NULL == table || table->count <= 0)
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
		return -EINVAL;

	cz_hwmgr->sclk_dpm.soft_min_clk = table->entries[0].clk;
	cz_hwmgr->sclk_dpm.hard_min_clk = table->entries[0].clk;

	level = cz_get_max_sclk_level(hwmgr) - 1;

	if (level < table->count)
		clock = table->entries[level].clk;
	else
		clock = table->entries[table->count - 1].clk;

	cz_hwmgr->sclk_dpm.soft_max_clk = clock;
	cz_hwmgr->sclk_dpm.hard_max_clk = clock;

	smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
				PPSMC_MSG_SetSclkSoftMin,
				cz_get_sclk_level(hwmgr,
				cz_hwmgr->sclk_dpm.soft_min_clk,
				PPSMC_MSG_SetSclkSoftMin));

	smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
				PPSMC_MSG_SetSclkSoftMax,
				cz_get_sclk_level(hwmgr,
				cz_hwmgr->sclk_dpm.soft_max_clk,
				PPSMC_MSG_SetSclkSoftMax));

	return 0;
}

int cz_phm_force_dpm_lowest(struct pp_hwmgr *hwmgr)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	if (cz_hwmgr->sclk_dpm.soft_min_clk !=
				cz_hwmgr->sclk_dpm.soft_max_clk) {
		cz_hwmgr->sclk_dpm.soft_max_clk =
			cz_hwmgr->sclk_dpm.soft_min_clk;

		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
				PPSMC_MSG_SetSclkSoftMax,
				cz_get_sclk_level(hwmgr,
				cz_hwmgr->sclk_dpm.soft_max_clk,
				PPSMC_MSG_SetSclkSoftMax));
	}

	return 0;
}

static int cz_dpm_force_dpm_level(struct pp_hwmgr *hwmgr,
				enum amd_dpm_forced_level level)
{
	int ret = 0;

	switch (level) {
	case AMD_DPM_FORCED_LEVEL_HIGH:
		ret = cz_phm_force_dpm_highest(hwmgr);
		if (ret)
			return ret;
		break;
	case AMD_DPM_FORCED_LEVEL_LOW:
		ret = cz_phm_force_dpm_lowest(hwmgr);
		if (ret)
			return ret;
		break;
	case AMD_DPM_FORCED_LEVEL_AUTO:
		ret = cz_phm_unforce_dpm_levels(hwmgr);
		if (ret)
			return ret;
		break;
	default:
		break;
	}

	hwmgr->dpm_level = level;

	return ret;
}

1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
int cz_dpm_powerdown_uvd(struct pp_hwmgr *hwmgr)
{
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
					 PHM_PlatformCaps_UVDPowerGating))
		return smum_send_msg_to_smc(hwmgr->smumgr,
						     PPSMC_MSG_UVDPowerOFF);
	return 0;
}

int cz_dpm_powerup_uvd(struct pp_hwmgr *hwmgr)
{
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
					 PHM_PlatformCaps_UVDPowerGating)) {
		if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
				  PHM_PlatformCaps_UVDDynamicPowerGating)) {
			return smum_send_msg_to_smc_with_parameter(
								hwmgr->smumgr,
						   PPSMC_MSG_UVDPowerON, 1);
		} else {
			return smum_send_msg_to_smc_with_parameter(
								hwmgr->smumgr,
						   PPSMC_MSG_UVDPowerON, 0);
		}
	}

	return 0;
}

int cz_dpm_update_uvd_dpm(struct pp_hwmgr *hwmgr, bool bgate)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	struct phm_uvd_clock_voltage_dependency_table *ptable =
1361
		hwmgr->dyn_state.uvd_clock_voltage_dependency_table;
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376

	if (!bgate) {
		/* Stable Pstate is enabled and we need to set the UVD DPM to highest level */
		if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
					 PHM_PlatformCaps_StablePState)) {
			cz_hwmgr->uvd_dpm.hard_min_clk =
				   ptable->entries[ptable->count - 1].vclk;

			smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
						     PPSMC_MSG_SetUvdHardMin,
						      cz_get_uvd_level(hwmgr,
					     cz_hwmgr->uvd_dpm.hard_min_clk,
						   PPSMC_MSG_SetUvdHardMin));

			cz_enable_disable_uvd_dpm(hwmgr, true);
1377
		} else {
1378
			cz_enable_disable_uvd_dpm(hwmgr, true);
1379 1380
		}
	} else {
1381
		cz_enable_disable_uvd_dpm(hwmgr, false);
1382
	}
1383 1384 1385 1386 1387 1388 1389 1390

	return 0;
}

int  cz_dpm_update_vce_dpm(struct pp_hwmgr *hwmgr)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	struct phm_vce_clock_voltage_dependency_table *ptable =
1391
		hwmgr->dyn_state.vce_clock_voltage_dependency_table;
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462

	/* Stable Pstate is enabled and we need to set the VCE DPM to highest level */
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
					 PHM_PlatformCaps_StablePState)) {
		cz_hwmgr->vce_dpm.hard_min_clk =
				  ptable->entries[ptable->count - 1].ecclk;

		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
					PPSMC_MSG_SetEclkHardMin,
					cz_get_eclk_level(hwmgr,
					     cz_hwmgr->vce_dpm.hard_min_clk,
						PPSMC_MSG_SetEclkHardMin));
	} else {
		/*EPR# 419220 -HW limitation to to */
		cz_hwmgr->vce_dpm.hard_min_clk = hwmgr->vce_arbiter.ecclk;
		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
					    PPSMC_MSG_SetEclkHardMin,
					    cz_get_eclk_level(hwmgr,
				     cz_hwmgr->vce_dpm.hard_min_clk,
					  PPSMC_MSG_SetEclkHardMin));

	}
	return 0;
}

int cz_dpm_powerdown_vce(struct pp_hwmgr *hwmgr)
{
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
					 PHM_PlatformCaps_VCEPowerGating))
		return smum_send_msg_to_smc(hwmgr->smumgr,
						     PPSMC_MSG_VCEPowerOFF);
	return 0;
}

int cz_dpm_powerup_vce(struct pp_hwmgr *hwmgr)
{
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
					 PHM_PlatformCaps_VCEPowerGating))
		return smum_send_msg_to_smc(hwmgr->smumgr,
						     PPSMC_MSG_VCEPowerON);
	return 0;
}

static int cz_dpm_get_mclk(struct pp_hwmgr *hwmgr, bool low)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	return cz_hwmgr->sys_info.bootup_uma_clock;
}

static int cz_dpm_get_sclk(struct pp_hwmgr *hwmgr, bool low)
{
	struct pp_power_state  *ps;
	struct cz_power_state  *cz_ps;

	if (hwmgr == NULL)
		return -EINVAL;

	ps = hwmgr->request_ps;

	if (ps == NULL)
		return -EINVAL;

	cz_ps = cast_PhwCzPowerState(&ps->hardware);

	if (low)
		return cz_ps->levels[0].engineClock;
	else
		return cz_ps->levels[cz_ps->level-1].engineClock;
}

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
static int cz_dpm_patch_boot_state(struct pp_hwmgr *hwmgr,
					struct pp_hw_power_state *hw_ps)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
	struct cz_power_state *cz_ps = cast_PhwCzPowerState(hw_ps);

	cz_ps->level = 1;
	cz_ps->nbps_flags = 0;
	cz_ps->bapm_flags = 0;
	cz_ps->levels[0] = cz_hwmgr->boot_power_level;

	return 0;
}

static int cz_dpm_get_pp_table_entry_callback(
						     struct pp_hwmgr *hwmgr,
					   struct pp_hw_power_state *hw_ps,
							  unsigned int index,
						     const void *clock_info)
{
	struct cz_power_state *cz_ps = cast_PhwCzPowerState(hw_ps);

	const ATOM_PPLIB_CZ_CLOCK_INFO *cz_clock_info = clock_info;

	struct phm_clock_voltage_dependency_table *table =
				    hwmgr->dyn_state.vddc_dependency_on_sclk;
	uint8_t clock_info_index = cz_clock_info->index;

	if (clock_info_index > (uint8_t)(hwmgr->platform_descriptor.hardwareActivityPerformanceLevels - 1))
		clock_info_index = (uint8_t)(hwmgr->platform_descriptor.hardwareActivityPerformanceLevels - 1);

	cz_ps->levels[index].engineClock = table->entries[clock_info_index].clk;
	cz_ps->levels[index].vddcIndex = (uint8_t)table->entries[clock_info_index].v;

	cz_ps->level = index + 1;

	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep)) {
		cz_ps->levels[index].dsDividerIndex = 5;
		cz_ps->levels[index].ssDividerIndex = 5;
	}

	return 0;
}

static int cz_dpm_get_num_of_pp_table_entries(struct pp_hwmgr *hwmgr)
{
	int result;
	unsigned long ret = 0;

	result = pp_tables_get_num_of_entries(hwmgr, &ret);

	return result ? 0 : ret;
}

static int cz_dpm_get_pp_table_entry(struct pp_hwmgr *hwmgr,
		    unsigned long entry, struct pp_power_state *ps)
{
	int result;
	struct cz_power_state *cz_ps;

	ps->hardware.magic = PhwCz_Magic;

	cz_ps = cast_PhwCzPowerState(&(ps->hardware));

	result = pp_tables_get_entry(hwmgr, entry, ps,
			cz_dpm_get_pp_table_entry_callback);

	cz_ps->uvd_clocks.vclk = ps->uvd_clocks.VCLK;
	cz_ps->uvd_clocks.dclk = ps->uvd_clocks.DCLK;

	return result;
}

int cz_get_power_state_size(struct pp_hwmgr *hwmgr)
{
	return sizeof(struct cz_power_state);
}

1541
static void cz_hw_print_display_cfg(
1542
	const struct cc6_settings *cc6_settings)
1543 1544 1545 1546
{
	PP_DBG_LOG("New Display Configuration:\n");

	PP_DBG_LOG("   cpu_cc6_disable: %d\n",
1547
			cc6_settings->cpu_cc6_disable);
1548
	PP_DBG_LOG("   cpu_pstate_disable: %d\n",
1549
			cc6_settings->cpu_pstate_disable);
1550
	PP_DBG_LOG("   nb_pstate_switch_disable: %d\n",
1551
			cc6_settings->nb_pstate_switch_disable);
1552
	PP_DBG_LOG("   cpu_pstate_separation_time: %d\n\n",
1553
			cc6_settings->cpu_pstate_separation_time);
1554 1555
}

1556
 static int cz_set_cpu_power_state(struct pp_hwmgr *hwmgr)
1557 1558 1559
{
	struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
	uint32_t data = 0;
1560

1561
	if (hw_data->cc6_settings.cc6_setting_changed) {
1562 1563

		hw_data->cc6_settings.cc6_setting_changed = false;
1564

1565
		cz_hw_print_display_cfg(&hw_data->cc6_settings);
1566

1567
		data |= (hw_data->cc6_settings.cpu_pstate_separation_time
1568 1569 1570
			& PWRMGT_SEPARATION_TIME_MASK)
			<< PWRMGT_SEPARATION_TIME_SHIFT;

1571
		data |= (hw_data->cc6_settings.cpu_cc6_disable ? 0x1 : 0x0)
1572 1573
			<< PWRMGT_DISABLE_CPU_CSTATES_SHIFT;

1574
		data |= (hw_data->cc6_settings.cpu_pstate_disable ? 0x1 : 0x0)
1575 1576
			<< PWRMGT_DISABLE_CPU_PSTATES_SHIFT;

1577 1578 1579
		PP_DBG_LOG("SetDisplaySizePowerParams data: 0x%X\n",
			data);

1580 1581 1582 1583 1584 1585 1586 1587
		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
						PPSMC_MSG_SetDisplaySizePowerParams,
						data);
	}

	return 0;
}

1588

1589
static int cz_store_cc6_data(struct pp_hwmgr *hwmgr, uint32_t separation_time,
1590
			bool cc6_disable, bool pstate_disable, bool pstate_switch_disable)
1591
{
1592 1593
	struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);

1594
	if (separation_time !=
1595 1596 1597 1598
	    hw_data->cc6_settings.cpu_pstate_separation_time ||
	    cc6_disable != hw_data->cc6_settings.cpu_cc6_disable ||
	    pstate_disable != hw_data->cc6_settings.cpu_pstate_disable ||
	    pstate_switch_disable != hw_data->cc6_settings.nb_pstate_switch_disable) {
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609

		hw_data->cc6_settings.cc6_setting_changed = true;

		hw_data->cc6_settings.cpu_pstate_separation_time =
			separation_time;
		hw_data->cc6_settings.cpu_cc6_disable =
			cc6_disable;
		hw_data->cc6_settings.cpu_pstate_disable =
			pstate_disable;
		hw_data->cc6_settings.nb_pstate_switch_disable =
			pstate_switch_disable;
1610 1611

	}
1612

1613 1614 1615
	return 0;
}

1616
static int cz_get_dal_power_level(struct pp_hwmgr *hwmgr,
R
Rex Zhu 已提交
1617
		struct amd_pp_simple_clock_info *info)
1618 1619
{
	uint32_t i;
R
Rex Zhu 已提交
1620
	const struct phm_clock_voltage_dependency_table *table =
1621
			hwmgr->dyn_state.vddc_dep_on_dal_pwrl;
1622
	const struct phm_clock_and_voltage_limits *limits =
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
			&hwmgr->dyn_state.max_clock_voltage_on_ac;

	info->engine_max_clock = limits->sclk;
	info->memory_max_clock = limits->mclk;

	for (i = table->count - 1; i > 0; i--) {
		if (limits->vddc >= table->entries[i].v) {
			info->level = table->entries[i].clk;
			return 0;
		}
	}
	return -EINVAL;
}

1637
static int cz_force_clock_level(struct pp_hwmgr *hwmgr,
1638
		enum pp_clock_type type, uint32_t mask)
1639 1640 1641 1642 1643 1644 1645 1646
{
	if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
		return -EINVAL;

	switch (type) {
	case PP_SCLK:
		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
				PPSMC_MSG_SetSclkSoftMin,
1647
				mask);
1648 1649
		smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
				PPSMC_MSG_SetSclkSoftMax,
1650
				mask);
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
		break;
	default:
		break;
	}

	return 0;
}

static int cz_print_clock_levels(struct pp_hwmgr *hwmgr,
		enum pp_clock_type type, char *buf)
{
	struct phm_clock_voltage_dependency_table *sclk_table =
			hwmgr->dyn_state.vddc_dependency_on_sclk;
	int i, now, size = 0;

	switch (type) {
	case PP_SCLK:
		now = PHM_GET_FIELD(cgs_read_ind_register(hwmgr->device,
				CGS_IND_REG__SMC,
				ixTARGET_AND_CURRENT_PROFILE_INDEX),
				TARGET_AND_CURRENT_PROFILE_INDEX,
				CURR_SCLK_INDEX);

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

1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
static int cz_get_performance_level(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *state,
				PHM_PerformanceLevelDesignation designation, uint32_t index,
				PHM_PerformanceLevel *level)
{
	const struct cz_power_state *ps;
	struct cz_hwmgr *data;
	uint32_t level_index;
	uint32_t i;

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

	data = (struct cz_hwmgr *)(hwmgr->backend);
	ps = cast_const_PhwCzPowerState(state);
1699

1700
	level_index = index > ps->level - 1 ? ps->level - 1 : index;
1701
	level->coreClock = ps->levels[level_index].engineClock;
1702

1703 1704 1705 1706 1707 1708 1709 1710 1711
	if (designation == PHM_PerformanceLevelDesignation_PowerContainment) {
		for (i = 1; i < ps->level; i++) {
			if (ps->levels[i].engineClock > data->dce_slow_sclk_threshold) {
				level->coreClock = ps->levels[i].engineClock;
				break;
			}
		}
	}

1712
	if (level_index == 0)
1713 1714 1715 1716
		level->memory_clock = data->sys_info.nbp_memory_clock[CZ_NUM_NBPMEMORYCLOCK - 1];
	else
		level->memory_clock = data->sys_info.nbp_memory_clock[0];

1717
	level->vddc = (cz_convert_8Bit_index_to_voltage(hwmgr, ps->levels[level_index].vddcIndex) + 2) / 4;
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
	level->nonLocalMemoryFreq = 0;
	level->nonLocalMemoryWidth = 0;

	return 0;
}

static int cz_get_current_shallow_sleep_clocks(struct pp_hwmgr *hwmgr,
	const struct pp_hw_power_state *state, struct pp_clock_info *clock_info)
{
	const struct cz_power_state *ps = cast_const_PhwCzPowerState(state);

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

	return 0;
}

static int cz_get_clock_by_type(struct pp_hwmgr *hwmgr, enum amd_pp_clock_type type,
						struct amd_pp_clocks *clocks)
{
	struct cz_hwmgr *data = (struct cz_hwmgr *)(hwmgr->backend);
	int i;
	struct phm_clock_voltage_dependency_table *table;

	clocks->count = cz_get_max_sclk_level(hwmgr);
	switch (type) {
	case amd_pp_disp_clock:
		for (i = 0; i < clocks->count; i++)
			clocks->clock[i] = data->sys_info.display_clock[i];
		break;
	case amd_pp_sys_clock:
		table = hwmgr->dyn_state.vddc_dependency_on_sclk;
		for (i = 0; i < clocks->count; i++)
			clocks->clock[i] = table->entries[i].clk;
		break;
	case amd_pp_mem_clock:
		clocks->count = CZ_NUM_NBPMEMORYCLOCK;
		for (i = 0; i < clocks->count; i++)
			clocks->clock[i] = data->sys_info.nbp_memory_clock[clocks->count - 1 - i];
		break;
	default:
		return -1;
	}

	return 0;
}

static int cz_get_max_high_clocks(struct pp_hwmgr *hwmgr, struct amd_pp_simple_clock_info *clocks)
{
	struct phm_clock_voltage_dependency_table *table =
					hwmgr->dyn_state.vddc_dependency_on_sclk;
	unsigned long level;
	const struct phm_clock_and_voltage_limits *limits =
			&hwmgr->dyn_state.max_clock_voltage_on_ac;

	if ((NULL == table) || (table->count <= 0) || (clocks == NULL))
		return -EINVAL;

	level = cz_get_max_sclk_level(hwmgr) - 1;

	if (level < table->count)
		clocks->engine_max_clock = table->entries[level].clk;
	else
		clocks->engine_max_clock = table->entries[table->count - 1].clk;

	clocks->memory_max_clock = limits->mclk;

	return 0;
}

1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
static int cz_thermal_get_temperature(struct pp_hwmgr *hwmgr)
{
	int actual_temp = 0;
	uint32_t val = cgs_read_ind_register(hwmgr->device,
					     CGS_IND_REG__SMC, ixTHM_TCON_CUR_TMP);
	uint32_t temp = PHM_GET_FIELD(val, THM_TCON_CUR_TMP, CUR_TEMP);

	if (PHM_GET_FIELD(val, THM_TCON_CUR_TMP, CUR_TEMP_RANGE_SEL))
		actual_temp = ((temp / 8) - 49) * PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
	else
		actual_temp = (temp / 8) * PP_TEMPERATURE_UNITS_PER_CENTIGRADES;

	return actual_temp;
}

1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
static int cz_read_sensor(struct pp_hwmgr *hwmgr, int idx, int32_t *value)
{
	struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);

	struct phm_clock_voltage_dependency_table *table =
				hwmgr->dyn_state.vddc_dependency_on_sclk;

	struct phm_vce_clock_voltage_dependency_table *vce_table =
		hwmgr->dyn_state.vce_clock_voltage_dependency_table;

	struct phm_uvd_clock_voltage_dependency_table *uvd_table =
		hwmgr->dyn_state.uvd_clock_voltage_dependency_table;

	uint32_t sclk_index = PHM_GET_FIELD(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixTARGET_AND_CURRENT_PROFILE_INDEX),
					TARGET_AND_CURRENT_PROFILE_INDEX, CURR_SCLK_INDEX);
	uint32_t uvd_index = PHM_GET_FIELD(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixTARGET_AND_CURRENT_PROFILE_INDEX_2),
					TARGET_AND_CURRENT_PROFILE_INDEX_2, CURR_UVD_INDEX);
	uint32_t vce_index = PHM_GET_FIELD(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixTARGET_AND_CURRENT_PROFILE_INDEX_2),
					TARGET_AND_CURRENT_PROFILE_INDEX_2, CURR_VCE_INDEX);

	uint32_t sclk, vclk, dclk, ecclk, tmp, activity_percent;
	uint16_t vddnb, vddgfx;
	int result;

	switch (idx) {
	case AMDGPU_PP_SENSOR_GFX_SCLK:
		if (sclk_index < NUM_SCLK_LEVELS) {
			sclk = table->entries[sclk_index].clk;
			*value = sclk;
			return 0;
		}
		return -EINVAL;
	case AMDGPU_PP_SENSOR_VDDNB:
		tmp = (cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixSMUSVI_NB_CURRENTVID) &
			CURRENT_NB_VID_MASK) >> CURRENT_NB_VID__SHIFT;
		vddnb = cz_convert_8Bit_index_to_voltage(hwmgr, tmp);
		*value = vddnb;
		return 0;
	case AMDGPU_PP_SENSOR_VDDGFX:
		tmp = (cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixSMUSVI_GFX_CURRENTVID) &
			CURRENT_GFX_VID_MASK) >> CURRENT_GFX_VID__SHIFT;
		vddgfx = cz_convert_8Bit_index_to_voltage(hwmgr, (u16)tmp);
		*value = vddgfx;
		return 0;
	case AMDGPU_PP_SENSOR_UVD_VCLK:
		if (!cz_hwmgr->uvd_power_gated) {
			if (uvd_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
				return -EINVAL;
			} else {
				vclk = uvd_table->entries[uvd_index].vclk;
				*value = vclk;
				return 0;
			}
		}
		*value = 0;
		return 0;
	case AMDGPU_PP_SENSOR_UVD_DCLK:
		if (!cz_hwmgr->uvd_power_gated) {
			if (uvd_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
				return -EINVAL;
			} else {
				dclk = uvd_table->entries[uvd_index].dclk;
				*value = dclk;
				return 0;
			}
		}
		*value = 0;
		return 0;
	case AMDGPU_PP_SENSOR_VCE_ECCLK:
		if (!cz_hwmgr->vce_power_gated) {
			if (vce_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
				return -EINVAL;
			} else {
				ecclk = vce_table->entries[vce_index].ecclk;
				*value = ecclk;
				return 0;
			}
		}
		*value = 0;
		return 0;
	case AMDGPU_PP_SENSOR_GPU_LOAD:
		result = smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetAverageGraphicsActivity);
		if (0 == result) {
			activity_percent = cgs_read_register(hwmgr->device, mmSMU_MP1_SRBM2P_ARG_0);
			activity_percent = activity_percent > 100 ? 100 : activity_percent;
		} else {
			activity_percent = 50;
		}
		*value = activity_percent;
		return 0;
1893 1894 1895 1896 1897 1898
	case AMDGPU_PP_SENSOR_UVD_POWER:
		*value = cz_hwmgr->uvd_power_gated ? 0 : 1;
		return 0;
	case AMDGPU_PP_SENSOR_VCE_POWER:
		*value = cz_hwmgr->vce_power_gated ? 0 : 1;
		return 0;
1899 1900 1901
	case AMDGPU_PP_SENSOR_GPU_TEMP:
		*value = cz_thermal_get_temperature(hwmgr);
		return 0;
1902 1903 1904 1905 1906
	default:
		return -EINVAL;
	}
}

1907 1908 1909 1910
static const struct pp_hwmgr_func cz_hwmgr_funcs = {
	.backend_init = cz_hwmgr_backend_init,
	.backend_fini = cz_hwmgr_backend_fini,
	.asic_setup = NULL,
1911
	.apply_state_adjust_rules = cz_apply_state_adjust_rules,
1912 1913
	.force_dpm_level = cz_dpm_force_dpm_level,
	.get_power_state_size = cz_get_power_state_size,
1914 1915 1916 1917 1918
	.powerdown_uvd = cz_dpm_powerdown_uvd,
	.powergate_uvd = cz_dpm_powergate_uvd,
	.powergate_vce = cz_dpm_powergate_vce,
	.get_mclk = cz_dpm_get_mclk,
	.get_sclk = cz_dpm_get_sclk,
1919 1920 1921
	.patch_boot_state = cz_dpm_patch_boot_state,
	.get_pp_table_entry = cz_dpm_get_pp_table_entry,
	.get_num_of_pp_table_entries = cz_dpm_get_num_of_pp_table_entries,
1922 1923
	.set_cpu_power_state = cz_set_cpu_power_state,
	.store_cc6_data = cz_store_cc6_data,
1924 1925
	.force_clock_level = cz_force_clock_level,
	.print_clock_levels = cz_print_clock_levels,
1926 1927 1928 1929 1930
	.get_dal_power_level = cz_get_dal_power_level,
	.get_performance_level = cz_get_performance_level,
	.get_current_shallow_sleep_clocks = cz_get_current_shallow_sleep_clocks,
	.get_clock_by_type = cz_get_clock_by_type,
	.get_max_high_clocks = cz_get_max_high_clocks,
1931
	.read_sensor = cz_read_sensor,
1932 1933 1934 1935 1936 1937
};

int cz_hwmgr_init(struct pp_hwmgr *hwmgr)
{
	hwmgr->hwmgr_func = &cz_hwmgr_funcs;
	hwmgr->pptable_func = &pptable_funcs;
1938
	return 0;
1939
}