rv_hwmgr.c 29.4 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 35 36 37
/*
 * 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"
#include "rv_hwmgr.h"
#include "power_state.h"
#include "rv_smumgr.h"
38
#include "pp_soc15.h"
39 40

#define RAVEN_MAX_DEEPSLEEP_DIVIDER_ID     5
41
#define RAVEN_MINIMUM_ENGINE_CLOCK         800   /* 8Mhz, the low boundary of engine clock allowed on this chip */
42
#define SCLK_MIN_DIV_INTV_SHIFT         12
43
#define RAVEN_DISPCLK_BYPASS_THRESHOLD     10000 /* 100Mhz */
44 45
#define SMC_RAM_END                     0x40000

46
static const unsigned long PhwRaven_Magic = (unsigned long) PHM_Rv_Magic;
47 48


49 50
int rv_display_clock_voltage_request(struct pp_hwmgr *hwmgr,
		struct pp_display_clock_request *clock_req);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78


static struct rv_power_state *cast_rv_ps(struct pp_hw_power_state *hw_ps)
{
	if (PhwRaven_Magic != hw_ps->magic)
		return NULL;

	return (struct rv_power_state *)hw_ps;
}

static const struct rv_power_state *cast_const_rv_ps(
				const struct pp_hw_power_state *hw_ps)
{
	if (PhwRaven_Magic != hw_ps->magic)
		return NULL;

	return (struct rv_power_state *)hw_ps;
}

static int rv_initialize_dpm_defaults(struct pp_hwmgr *hwmgr)
{
	struct rv_hwmgr *rv_hwmgr = (struct rv_hwmgr *)(hwmgr->backend);

	rv_hwmgr->dce_slow_sclk_threshold = 30000;
	rv_hwmgr->thermal_auto_throttling_treshold = 0;
	rv_hwmgr->is_nb_dpm_enabled = 1;
	rv_hwmgr->dpm_flags = 1;
	rv_hwmgr->gfx_off_controled_by_driver = false;
79 80 81
	rv_hwmgr->need_min_deep_sleep_dcefclk = true;
	rv_hwmgr->num_active_display = 0;
	rv_hwmgr->deep_sleep_dcefclk = 0;
82 83 84 85 86 87 88 89

	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,
90
				PHM_PlatformCaps_PowerPlaySupport);
91 92 93 94 95 96 97 98 99 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	return 0;
}

static int rv_construct_max_power_limits_table(struct pp_hwmgr *hwmgr,
			struct phm_clock_and_voltage_limits *table)
{
	return 0;
}

static int rv_init_dynamic_state_adjustment_rule_settings(
							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;
}

static int rv_get_system_info_data(struct pp_hwmgr *hwmgr)
{
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)hwmgr->backend;

	rv_data->sys_info.htc_hyst_lmt = 5;
	rv_data->sys_info.htc_tmp_lmt = 203;

	if (rv_data->thermal_auto_throttling_treshold == 0)
		 rv_data->thermal_auto_throttling_treshold = 203;

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

	rv_init_dynamic_state_adjustment_rule_settings(hwmgr);

	return 0;
}

static int rv_construct_boot_state(struct pp_hwmgr *hwmgr)
{
	return 0;
}

160
static int rv_set_clock_limit(struct pp_hwmgr *hwmgr, const void *input)
161
{
162 163 164 165 166 167 168 169 170 171
	struct PP_Clocks clocks = {0};
	struct pp_display_clock_request clock_req;

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

	PP_ASSERT_WITH_CODE(!rv_display_clock_voltage_request(hwmgr, &clock_req),
				"Attempt to set DCF Clock Failed!", return -EINVAL);

172 173 174
	return 0;
}

175
static int rv_set_deep_sleep_dcefclk(struct pp_hwmgr *hwmgr, uint32_t clock)
176
{
177
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
178

179 180
	if (rv_data->need_min_deep_sleep_dcefclk && rv_data->deep_sleep_dcefclk != clock/100) {
		rv_data->deep_sleep_dcefclk = clock/100;
181
		smum_send_msg_to_smc_with_parameter(hwmgr,
182 183 184 185 186 187 188 189 190
					PPSMC_MSG_SetMinDeepSleepDcefclk,
					rv_data->deep_sleep_dcefclk);
	}
	return 0;
}

static int rv_set_active_display_count(struct pp_hwmgr *hwmgr, uint32_t count)
{
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
191

192 193
	if (rv_data->num_active_display != count) {
		rv_data->num_active_display = count;
194
		smum_send_msg_to_smc_with_parameter(hwmgr,
195
				PPSMC_MSG_SetDisplayCount,
196 197
				rv_data->num_active_display);
	}
198

199 200 201
	return 0;
}

202 203 204 205
static int rv_set_power_state_tasks(struct pp_hwmgr *hwmgr, const void *input)
{
	return rv_set_clock_limit(hwmgr, input);
}
206

207
static int rv_init_power_gate_state(struct pp_hwmgr *hwmgr)
208 209 210 211 212 213 214 215 216 217 218
{
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);

	rv_data->vcn_power_gated = true;
	rv_data->isp_tileA_power_gated = true;
	rv_data->isp_tileB_power_gated = true;

	return 0;
}


219 220 221 222
static int rv_setup_asic_task(struct pp_hwmgr *hwmgr)
{
	return rv_init_power_gate_state(hwmgr);
}
223

224
static int rv_reset_cc6_data(struct pp_hwmgr *hwmgr)
225 226 227 228 229 230 231 232 233 234 235
{
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);

	rv_data->separation_time = 0;
	rv_data->cc6_disable = false;
	rv_data->pstate_disable = false;
	rv_data->cc6_setting_changed = false;

	return 0;
}

236 237 238 239
static int rv_power_off_asic(struct pp_hwmgr *hwmgr)
{
	return rv_reset_cc6_data(hwmgr);
}
240

241
static int rv_disable_gfx_off(struct pp_hwmgr *hwmgr)
242 243 244 245
{
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);

	if (rv_data->gfx_off_controled_by_driver)
246
		smum_send_msg_to_smc(hwmgr,
247 248 249 250 251
						PPSMC_MSG_DisableGfxOff);

	return 0;
}

252 253 254 255
static int rv_disable_dpm_tasks(struct pp_hwmgr *hwmgr)
{
	return rv_disable_gfx_off(hwmgr);
}
256

257
static int rv_enable_gfx_off(struct pp_hwmgr *hwmgr)
258 259 260 261
{
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);

	if (rv_data->gfx_off_controled_by_driver)
262
		smum_send_msg_to_smc(hwmgr,
263 264 265 266 267
						PPSMC_MSG_EnableGfxOff);

	return 0;
}

268 269 270 271
static int rv_enable_dpm_tasks(struct pp_hwmgr *hwmgr)
{
	return rv_enable_gfx_off(hwmgr);
}
272 273 274 275 276 277 278 279 280

static int rv_apply_state_adjust_rules(struct pp_hwmgr *hwmgr,
				struct pp_power_state  *prequest_ps,
			const struct pp_power_state *pcurrent_ps)
{
	return 0;
}

/* temporary hardcoded clock voltage breakdown tables */
281
static const DpmClock_t VddDcfClk[]= {
282 283 284 285 286
	{ 300, 2600},
	{ 600, 3200},
	{ 600, 3600},
};

287
static const DpmClock_t VddSocClk[]= {
288 289 290 291 292
	{ 478, 2600},
	{ 722, 3200},
	{ 722, 3600},
};

293
static const DpmClock_t VddFClk[]= {
294 295 296 297 298
	{ 400, 2600},
	{1200, 3200},
	{1200, 3600},
};

299
static const DpmClock_t VddDispClk[]= {
300 301 302 303 304
	{ 435, 2600},
	{ 661, 3200},
	{1086, 3600},
};

305
static const DpmClock_t VddDppClk[]= {
306 307 308 309 310
	{ 435, 2600},
	{ 661, 3200},
	{ 661, 3600},
};

311
static const DpmClock_t VddPhyClk[]= {
312 313 314 315 316 317 318
	{ 540, 2600},
	{ 810, 3200},
	{ 810, 3600},
};

static int rv_get_clock_voltage_dependency_table(struct pp_hwmgr *hwmgr,
			struct rv_voltage_dependency_table **pptable,
319
			uint32_t num_entry, const DpmClock_t *pclk_dependency_table)
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
{
	uint32_t table_size, i;
	struct rv_voltage_dependency_table *ptable;

	table_size = sizeof(uint32_t) + sizeof(struct rv_voltage_dependency_table) * num_entry;
	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;
}


static int rv_populate_clock_table(struct pp_hwmgr *hwmgr)
{
	int result;

	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
	DpmClocks_t  *table = &(rv_data->clock_table);
	struct rv_clock_voltage_information *pinfo = &(rv_data->clock_vol_info);

352
	result = rv_copy_table_from_smc(hwmgr, (uint8_t *)table, CLOCKTABLE);
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

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

	if (0 == result && table->DcefClocks[0].Freq != 0) {
		rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dcefclk,
						NUM_DCEFCLK_DPM_LEVELS,
						&rv_data->clock_table.DcefClocks[0]);
		rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_socclk,
						NUM_SOCCLK_DPM_LEVELS,
						&rv_data->clock_table.SocClocks[0]);
		rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_fclk,
						NUM_FCLK_DPM_LEVELS,
						&rv_data->clock_table.FClocks[0]);
		rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_mclk,
						NUM_MEMCLK_DPM_LEVELS,
						&rv_data->clock_table.MemClocks[0]);
	} else {
		rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dcefclk,
373 374
						ARRAY_SIZE(VddDcfClk),
						&VddDcfClk[0]);
375
		rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_socclk,
376 377
						ARRAY_SIZE(VddSocClk),
						&VddSocClk[0]);
378
		rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_fclk,
379 380
						ARRAY_SIZE(VddFClk),
						&VddFClk[0]);
381 382
	}
	rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dispclk,
383 384
					ARRAY_SIZE(VddDispClk),
					&VddDispClk[0]);
385
	rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dppclk,
386
					ARRAY_SIZE(VddDppClk), &VddDppClk[0]);
387
	rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_phyclk,
388
					ARRAY_SIZE(VddPhyClk), &VddPhyClk[0]);
389

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	PP_ASSERT_WITH_CODE(!smum_send_msg_to_smc(hwmgr,
			PPSMC_MSG_GetMinGfxclkFrequency),
			"Attempt to get min GFXCLK Failed!",
			return -1);
	PP_ASSERT_WITH_CODE(!rv_read_arg_from_smc(hwmgr,
			&result),
			"Attempt to get min GFXCLK Failed!",
			return -1);
	rv_data->gfx_min_freq_limit = result * 100;

	PP_ASSERT_WITH_CODE(!smum_send_msg_to_smc(hwmgr,
			PPSMC_MSG_GetMaxGfxclkFrequency),
			"Attempt to get max GFXCLK Failed!",
			return -1);
	PP_ASSERT_WITH_CODE(!rv_read_arg_from_smc(hwmgr,
			&result),
			"Attempt to get max GFXCLK Failed!",
			return -1);
	rv_data->gfx_max_freq_limit = result * 100;

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 451 452 453 454 455 456 457 458 459 460 461
	return 0;
}

static int rv_hwmgr_backend_init(struct pp_hwmgr *hwmgr)
{
	int result = 0;
	struct rv_hwmgr *data;

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

	hwmgr->backend = data;

	result = rv_initialize_dpm_defaults(hwmgr);
	if (result != 0) {
		pr_err("rv_initialize_dpm_defaults failed\n");
		return result;
	}

	rv_populate_clock_table(hwmgr);

	result = rv_get_system_info_data(hwmgr);
	if (result != 0) {
		pr_err("rv_get_system_info_data failed\n");
		return result;
	}

	rv_construct_boot_state(hwmgr);

	hwmgr->platform_descriptor.hardwareActivityPerformanceLevels =
						RAVEN_MAX_HARDWARE_POWERLEVELS;

	hwmgr->platform_descriptor.hardwarePerformanceLevels =
						RAVEN_MAX_HARDWARE_POWERLEVELS;

	hwmgr->platform_descriptor.vbiosInterruptId = 0;

	hwmgr->platform_descriptor.clockStep.engineClock = 500;

	hwmgr->platform_descriptor.clockStep.memoryClock = 500;

	hwmgr->platform_descriptor.minimumClocksReductionPercentage = 50;

	return result;
}

static int rv_hwmgr_backend_fini(struct pp_hwmgr *hwmgr)
{
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
	struct rv_clock_voltage_information *pinfo = &(rv_data->clock_vol_info);

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
	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;
477

478 479 480 481 482 483 484 485 486
	kfree(hwmgr->backend);
	hwmgr->backend = NULL;

	return 0;
}

static int rv_dpm_force_dpm_level(struct pp_hwmgr *hwmgr,
				enum amd_dpm_forced_level level)
{
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 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
	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,
						RAVEN_UMD_PSTATE_PEAK_GFXCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
						RAVEN_UMD_PSTATE_PEAK_FCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinSocclkByFreq,
						RAVEN_UMD_PSTATE_PEAK_SOCCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinVcn,
						RAVEN_UMD_PSTATE_VCE);

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
						RAVEN_UMD_PSTATE_PEAK_GFXCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
						RAVEN_UMD_PSTATE_PEAK_FCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxSocclkByFreq,
						RAVEN_UMD_PSTATE_PEAK_SOCCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxVcn,
						RAVEN_UMD_PSTATE_VCE);
		break;
	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
						RAVEN_UMD_PSTATE_MIN_GFXCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
						RAVEN_UMD_PSTATE_MIN_GFXCLK);
		break;
	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
						RAVEN_UMD_PSTATE_MIN_FCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
						RAVEN_UMD_PSTATE_MIN_FCLK);
		break;
	case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
						RAVEN_UMD_PSTATE_GFXCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
						RAVEN_UMD_PSTATE_FCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinSocclkByFreq,
						RAVEN_UMD_PSTATE_SOCCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinVcn,
						RAVEN_UMD_PSTATE_VCE);

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
						RAVEN_UMD_PSTATE_GFXCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
						RAVEN_UMD_PSTATE_FCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxSocclkByFreq,
						RAVEN_UMD_PSTATE_SOCCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxVcn,
						RAVEN_UMD_PSTATE_VCE);
		break;
	case AMD_DPM_FORCED_LEVEL_AUTO:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
						RAVEN_UMD_PSTATE_MIN_GFXCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
						RAVEN_UMD_PSTATE_MIN_FCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinSocclkByFreq,
						RAVEN_UMD_PSTATE_MIN_SOCCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinVcn,
						RAVEN_UMD_PSTATE_MIN_VCE);

		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
						RAVEN_UMD_PSTATE_PEAK_GFXCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
						RAVEN_UMD_PSTATE_PEAK_FCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxSocclkByFreq,
						RAVEN_UMD_PSTATE_PEAK_SOCCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxVcn,
						RAVEN_UMD_PSTATE_VCE);
		break;
	case AMD_DPM_FORCED_LEVEL_LOW:
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinGfxClk,
						RAVEN_UMD_PSTATE_MIN_GFXCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxGfxClk,
						RAVEN_UMD_PSTATE_MIN_GFXCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetHardMinFclkByFreq,
						RAVEN_UMD_PSTATE_MIN_FCLK);
		smum_send_msg_to_smc_with_parameter(hwmgr,
						PPSMC_MSG_SetSoftMaxFclkByFreq,
						RAVEN_UMD_PSTATE_MIN_FCLK);
		break;
	case AMD_DPM_FORCED_LEVEL_MANUAL:
	case AMD_DPM_FORCED_LEVEL_PROFILE_EXIT:
	default:
		break;
	}
610 611 612
	return 0;
}

613
static uint32_t rv_dpm_get_mclk(struct pp_hwmgr *hwmgr, bool low)
614
{
615 616 617 618 619 620 621 622 623 624 625 626
	struct rv_hwmgr *data;

	if (hwmgr == NULL)
		return -EINVAL;

	data = (struct rv_hwmgr *)(hwmgr->backend);

	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;
627 628
}

629
static uint32_t rv_dpm_get_sclk(struct pp_hwmgr *hwmgr, bool low)
630
{
631 632 633 634 635 636 637 638 639 640 641
	struct rv_hwmgr *data;

	if (hwmgr == NULL)
		return -EINVAL;

	data = (struct rv_hwmgr *)(hwmgr->backend);

	if (low)
		return data->gfx_min_freq_limit;
	else
		return data->gfx_max_freq_limit;
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
}

static int rv_dpm_patch_boot_state(struct pp_hwmgr *hwmgr,
					struct pp_hw_power_state *hw_ps)
{
	return 0;
}

static int rv_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 rv_power_state *rv_ps = cast_rv_ps(hw_ps);

658
	rv_ps->levels[index].engine_clock = 0;
659

660
	rv_ps->levels[index].vddc_index = 0;
661 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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
	rv_ps->level = index + 1;

	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep)) {
		rv_ps->levels[index].ds_divider_index = 5;
		rv_ps->levels[index].ss_divider_index = 5;
	}

	return 0;
}

static int rv_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 rv_dpm_get_pp_table_entry(struct pp_hwmgr *hwmgr,
		    unsigned long entry, struct pp_power_state *ps)
{
	int result;
	struct rv_power_state *rv_ps;

	ps->hardware.magic = PhwRaven_Magic;

	rv_ps = cast_rv_ps(&(ps->hardware));

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

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

	return result;
}

static int rv_get_power_state_size(struct pp_hwmgr *hwmgr)
{
	return sizeof(struct rv_power_state);
}

static int rv_set_cpu_power_state(struct pp_hwmgr *hwmgr)
{
	return 0;
}


static int rv_store_cc6_data(struct pp_hwmgr *hwmgr, uint32_t separation_time,
			bool cc6_disable, bool pstate_disable, bool pstate_switch_disable)
{
	return 0;
}

static int rv_get_dal_power_level(struct pp_hwmgr *hwmgr,
		struct amd_pp_simple_clock_info *info)
{
	return -EINVAL;
}

static int rv_force_clock_level(struct pp_hwmgr *hwmgr,
		enum pp_clock_type type, uint32_t mask)
{
	return 0;
}

static int rv_print_clock_levels(struct pp_hwmgr *hwmgr,
		enum pp_clock_type type, char *buf)
{
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
	struct rv_hwmgr *data = (struct rv_hwmgr *)(hwmgr->backend);
	struct rv_voltage_dependency_table *mclk_table =
			data->clock_vol_info.vdd_dep_on_fclk;
	int i, now, size = 0;

	switch (type) {
	case PP_SCLK:
		PP_ASSERT_WITH_CODE(!smum_send_msg_to_smc(hwmgr,
				PPSMC_MSG_GetGfxclkFrequency),
				"Attempt to get current GFXCLK Failed!",
				return -1);
		PP_ASSERT_WITH_CODE(!rv_read_arg_from_smc(hwmgr,
				&now),
				"Attempt to get current GFXCLK Failed!",
				return -1);

		size += sprintf(buf + size, "0: %uMhz %s\n",
				data->gfx_min_freq_limit / 100,
				((data->gfx_min_freq_limit / 100)
				 == now) ? "*" : "");
		size += sprintf(buf + size, "1: %uMhz %s\n",
				data->gfx_max_freq_limit / 100,
				((data->gfx_max_freq_limit / 100)
				 == now) ? "*" : "");
		break;
	case PP_MCLK:
		PP_ASSERT_WITH_CODE(!smum_send_msg_to_smc(hwmgr,
				PPSMC_MSG_GetFclkFrequency),
				"Attempt to get current MEMCLK Failed!",
				return -1);
		PP_ASSERT_WITH_CODE(!rv_read_arg_from_smc(hwmgr,
				&now),
				"Attempt to get current MEMCLK Failed!",
				return -1);

		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;
779 780 781 782 783 784 785 786 787 788 789 790 791
}

static int rv_get_performance_level(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *state,
				PHM_PerformanceLevelDesignation designation, uint32_t index,
				PHM_PerformanceLevel *level)
{
	struct rv_hwmgr *data;

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

	data = (struct rv_hwmgr *)(hwmgr->backend);

792
	if (index == 0) {
793
		level->memory_clock = data->clock_vol_info.vdd_dep_on_fclk->entries[0].clk;
794 795 796 797 798
		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;
799
	}
800

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
	level->nonLocalMemoryFreq = 0;
	level->nonLocalMemoryWidth = 0;

	return 0;
}

static int rv_get_current_shallow_sleep_clocks(struct pp_hwmgr *hwmgr,
	const struct pp_hw_power_state *state, struct pp_clock_info *clock_info)
{
	const struct rv_power_state *ps = cast_const_rv_ps(state);

	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


static uint32_t rv_get_mem_latency(struct pp_hwmgr *hwmgr,
		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;
}

837 838
static int rv_get_clock_by_type_with_latency(struct pp_hwmgr *hwmgr,
		enum amd_pp_clock_type type,
839 840 841 842 843
		struct pp_clock_levels_with_latency *clocks)
{
	uint32_t i;
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
	struct rv_clock_voltage_information *pinfo = &(rv_data->clock_vol_info);
844 845
	struct rv_voltage_dependency_table *pclk_vol_table;
	bool latency_required = false;
846

847 848
	if (pinfo == NULL)
		return -EINVAL;
849 850 851

	switch (type) {
	case amd_pp_mem_clock:
852 853
		pclk_vol_table = pinfo->vdd_dep_on_mclk;
		latency_required = true;
854
		break;
855 856 857
	case amd_pp_f_clock:
		pclk_vol_table = pinfo->vdd_dep_on_fclk;
		latency_required = true;
858
		break;
859 860
	case amd_pp_dcf_clock:
		pclk_vol_table = pinfo->vdd_dep_on_dcefclk;
861
		break;
862 863 864 865 866 867 868 869
	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;
870
	default:
871 872 873 874 875 876 877 878 879 880 881 882 883 884
		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 ?
						rv_get_mem_latency(hwmgr,
						pclk_vol_table->entries[i].clk) :
						0;
		clocks->num_levels++;
885 886 887 888 889 890 891 892 893 894 895 896
	}

	return 0;
}

static int rv_get_clock_by_type_with_voltage(struct pp_hwmgr *hwmgr,
		enum amd_pp_clock_type type,
		struct pp_clock_levels_with_voltage *clocks)
{
	uint32_t i;
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
	struct rv_clock_voltage_information *pinfo = &(rv_data->clock_vol_info);
897 898 899 900
	struct rv_voltage_dependency_table *pclk_vol_table = NULL;

	if (pinfo == NULL)
		return -EINVAL;
901 902 903 904 905

	switch (type) {
	case amd_pp_mem_clock:
		pclk_vol_table = pinfo->vdd_dep_on_mclk;
		break;
906 907
	case amd_pp_f_clock:
		pclk_vol_table = pinfo->vdd_dep_on_fclk;
908
		break;
909 910
	case amd_pp_dcf_clock:
		pclk_vol_table = pinfo->vdd_dep_on_dcefclk;
911
		break;
912 913
	case amd_pp_soc_clock:
		pclk_vol_table = pinfo->vdd_dep_on_socclk;
914 915 916 917 918
		break;
	default:
		return -EINVAL;
	}

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

922
	clocks->num_levels = 0;
923 924 925 926 927 928 929 930 931 932 933 934 935
	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;
}

int rv_display_clock_voltage_request(struct pp_hwmgr *hwmgr,
		struct pp_display_clock_request *clock_req)
{
	int result = 0;
936
	struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
937
	enum amd_pp_clock_type clk_type = clock_req->clock_type;
938
	uint32_t clk_freq = clock_req->clock_freq_in_khz / 1000;
939 940 941
	PPSMC_Msg        msg;

	switch (clk_type) {
942 943 944
	case amd_pp_dcf_clock:
		if (clk_freq == rv_data->dcf_actual_hard_min_freq)
			return 0;
945
		msg =  PPSMC_MSG_SetHardMinDcefclkByFreq;
946
		rv_data->dcf_actual_hard_min_freq = clk_freq;
947 948 949 950
		break;
	case amd_pp_soc_clock:
		 msg = PPSMC_MSG_SetHardMinSocclkByFreq;
		break;
951 952 953 954
	case amd_pp_f_clock:
		if (clk_freq == rv_data->f_actual_hard_min_freq)
			return 0;
		rv_data->f_actual_hard_min_freq = clk_freq;
955 956 957 958 959 960 961
		msg = PPSMC_MSG_SetHardMinFclkByFreq;
		break;
	default:
		pr_info("[DisplayClockVoltageRequest]Invalid Clock Type!");
		return -EINVAL;
	}

962
	result = smum_send_msg_to_smc_with_parameter(hwmgr, msg,
963 964 965 966 967 968 969
							clk_freq);

	return result;
}

static int rv_get_max_high_clocks(struct pp_hwmgr *hwmgr, struct amd_pp_simple_clock_info *clocks)
{
970 971
	clocks->engine_max_clock = 80000; /* driver can't get engine clock, temp hard code to 800MHz */
	return 0;
972 973
}

974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
static int rv_thermal_get_temperature(struct pp_hwmgr *hwmgr)
{
	uint32_t reg_offset = soc15_get_register_offset(THM_HWID, 0,
			mmTHM_TCON_CUR_TMP_BASE_IDX, mmTHM_TCON_CUR_TMP);
	uint32_t reg_value = cgs_read_register(hwmgr->device, reg_offset);
	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;
}

990 991 992
static int rv_read_sensor(struct pp_hwmgr *hwmgr, int idx,
			  void *value, int *size)
{
993 994 995
	uint32_t sclk, mclk;
	int ret = 0;

996
	switch (idx) {
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	case AMDGPU_PP_SENSOR_GFX_SCLK:
		ret = smum_send_msg_to_smc(hwmgr, PPSMC_MSG_GetGfxclkFrequency);
		if (!ret) {
			rv_read_arg_from_smc(hwmgr, &sclk);
			/* in units of 10KHZ */
			*((uint32_t *)value) = sclk * 100;
			*size = 4;
		}
		break;
	case AMDGPU_PP_SENSOR_GFX_MCLK:
		ret = smum_send_msg_to_smc(hwmgr, PPSMC_MSG_GetFclkFrequency);
		if (!ret) {
			rv_read_arg_from_smc(hwmgr, &mclk);
			/* in units of 10KHZ */
			*((uint32_t *)value) = mclk * 100;
			*size = 4;
		}
		break;
1015 1016
	case AMDGPU_PP_SENSOR_GPU_TEMP:
		*((uint32_t *)value) = rv_thermal_get_temperature(hwmgr);
1017
		break;
1018
	default:
1019 1020
		ret = -EINVAL;
		break;
1021
	}
1022 1023

	return ret;
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
}

static const struct pp_hwmgr_func rv_hwmgr_funcs = {
	.backend_init = rv_hwmgr_backend_init,
	.backend_fini = rv_hwmgr_backend_fini,
	.asic_setup = NULL,
	.apply_state_adjust_rules = rv_apply_state_adjust_rules,
	.force_dpm_level = rv_dpm_force_dpm_level,
	.get_power_state_size = rv_get_power_state_size,
	.powerdown_uvd = NULL,
	.powergate_uvd = NULL,
	.powergate_vce = NULL,
	.get_mclk = rv_dpm_get_mclk,
	.get_sclk = rv_dpm_get_sclk,
	.patch_boot_state = rv_dpm_patch_boot_state,
	.get_pp_table_entry = rv_dpm_get_pp_table_entry,
	.get_num_of_pp_table_entries = rv_dpm_get_num_of_pp_table_entries,
	.set_cpu_power_state = rv_set_cpu_power_state,
	.store_cc6_data = rv_store_cc6_data,
	.force_clock_level = rv_force_clock_level,
	.print_clock_levels = rv_print_clock_levels,
	.get_dal_power_level = rv_get_dal_power_level,
	.get_performance_level = rv_get_performance_level,
	.get_current_shallow_sleep_clocks = rv_get_current_shallow_sleep_clocks,
	.get_clock_by_type_with_latency = rv_get_clock_by_type_with_latency,
	.get_clock_by_type_with_voltage = rv_get_clock_by_type_with_voltage,
	.get_max_high_clocks = rv_get_max_high_clocks,
	.read_sensor = rv_read_sensor,
1052 1053
	.set_active_display_count = rv_set_active_display_count,
	.set_deep_sleep_dcefclk = rv_set_deep_sleep_dcefclk,
1054 1055 1056 1057 1058
	.dynamic_state_management_enable = rv_enable_dpm_tasks,
	.power_off_asic = rv_power_off_asic,
	.asic_setup = rv_setup_asic_task,
	.power_state_set = rv_set_power_state_tasks,
	.dynamic_state_management_disable = rv_disable_dpm_tasks,
1059 1060 1061 1062 1063 1064 1065 1066
};

int rv_init_function_pointers(struct pp_hwmgr *hwmgr)
{
	hwmgr->hwmgr_func = &rv_hwmgr_funcs;
	hwmgr->pptable_func = &pptable_funcs;
	return 0;
}