cz_dpm.c 49.8 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 38 39 40 41 42 43 44
/*
 * Copyright 2014 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/firmware.h>
#include <linux/seq_file.h>
#include "drmP.h"
#include "amdgpu.h"
#include "amdgpu_pm.h"
#include "amdgpu_atombios.h"
#include "vid.h"
#include "vi_dpm.h"
#include "amdgpu_dpm.h"
#include "cz_dpm.h"
#include "cz_ppsmc.h"
#include "atom.h"

#include "smu/smu_8_0_d.h"
#include "smu/smu_8_0_sh_mask.h"
#include "gca/gfx_8_0_d.h"
#include "gca/gfx_8_0_sh_mask.h"
#include "gmc/gmc_8_1_d.h"
#include "bif/bif_5_1_d.h"
#include "gfx_v8_0.h"

45
static void cz_dpm_powergate_uvd(struct amdgpu_device *adev, bool gate);
S
Sonny Jiang 已提交
46
static void cz_dpm_powergate_vce(struct amdgpu_device *adev, bool gate);
47

48 49 50 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 79 80 81 82 83 84 85 86 87 88 89 90 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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
static struct cz_ps *cz_get_ps(struct amdgpu_ps *rps)
{
	struct cz_ps *ps = rps->ps_priv;

	return ps;
}

static struct cz_power_info *cz_get_pi(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = adev->pm.dpm.priv;

	return pi;
}

static uint16_t cz_convert_8bit_index_to_voltage(struct amdgpu_device *adev,
							uint16_t voltage)
{
	uint16_t tmp = 6200 - voltage * 25;

	return tmp;
}

static void cz_construct_max_power_limits_table(struct amdgpu_device *adev,
				struct amdgpu_clock_and_voltage_limits *table)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_clock_voltage_dependency_table *dep_table =
		&adev->pm.dpm.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(adev,
				dep_table->entries[dep_table->count - 1].v);
	}

	table->mclk = pi->sys_info.nbp_memory_clock[0];

}

union igp_info {
	struct _ATOM_INTEGRATED_SYSTEM_INFO info;
	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_9 info_9;
};

static int cz_parse_sys_info_table(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_mode_info *mode_info = &adev->mode_info;
	int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
	union igp_info *igp_info;
	u8 frev, crev;
	u16 data_offset;
	int i = 0;

	if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
				   &frev, &crev, &data_offset)) {
		igp_info = (union igp_info *)(mode_info->atom_context->bios +
					      data_offset);

		if (crev != 9) {
			DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
			return -EINVAL;
		}
		pi->sys_info.bootup_sclk =
			le32_to_cpu(igp_info->info_9.ulBootUpEngineClock);
		pi->sys_info.bootup_uma_clk =
			le32_to_cpu(igp_info->info_9.ulBootUpUMAClock);
		pi->sys_info.dentist_vco_freq =
			le32_to_cpu(igp_info->info_9.ulDentistVCOFreq);
		pi->sys_info.bootup_nb_voltage_index =
			le16_to_cpu(igp_info->info_9.usBootUpNBVoltage);

		if (igp_info->info_9.ucHtcTmpLmt == 0)
			pi->sys_info.htc_tmp_lmt = 203;
		else
			pi->sys_info.htc_tmp_lmt = igp_info->info_9.ucHtcTmpLmt;

		if (igp_info->info_9.ucHtcHystLmt == 0)
			pi->sys_info.htc_hyst_lmt = 5;
		else
			pi->sys_info.htc_hyst_lmt = igp_info->info_9.ucHtcHystLmt;

		if (pi->sys_info.htc_tmp_lmt <= pi->sys_info.htc_hyst_lmt) {
			DRM_ERROR("The htcTmpLmt should be larger than htcHystLmt.\n");
			return -EINVAL;
		}

		if (le32_to_cpu(igp_info->info_9.ulSystemConfig) & (1 << 3) &&
				pi->enable_nb_ps_policy)
			pi->sys_info.nb_dpm_enable = true;
		else
			pi->sys_info.nb_dpm_enable = false;

		for (i = 0; i < CZ_NUM_NBPSTATES; i++) {
			if (i < CZ_NUM_NBPMEMORY_CLOCK)
				pi->sys_info.nbp_memory_clock[i] =
				le32_to_cpu(igp_info->info_9.ulNbpStateMemclkFreq[i]);
			pi->sys_info.nbp_n_clock[i] =
			le32_to_cpu(igp_info->info_9.ulNbpStateNClkFreq[i]);
		}

		for (i = 0; i < CZ_MAX_DISPLAY_CLOCK_LEVEL; i++)
			pi->sys_info.display_clock[i] =
			le32_to_cpu(igp_info->info_9.sDispClkVoltageMapping[i].ulMaximumSupportedCLK);

		for (i = 0; i < CZ_NUM_NBPSTATES; i++)
			pi->sys_info.nbp_voltage_index[i] =
				le32_to_cpu(igp_info->info_9.usNBPStateVoltage[i]);

		if (le32_to_cpu(igp_info->info_9.ulGPUCapInfo) &
			SYS_INFO_GPUCAPS__ENABEL_DFS_BYPASS)
			pi->caps_enable_dfs_bypass = true;

		pi->sys_info.uma_channel_number =
			igp_info->info_9.ucUMAChannelNumber;

		cz_construct_max_power_limits_table(adev,
			&adev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
	}

	return 0;
}

static void cz_patch_voltage_values(struct amdgpu_device *adev)
{
	int i;
	struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
		&adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
	struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
		&adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
	struct amdgpu_clock_voltage_dependency_table *acp_table =
		&adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;

	if (uvd_table->count) {
		for (i = 0; i < uvd_table->count; i++)
			uvd_table->entries[i].v =
				cz_convert_8bit_index_to_voltage(adev,
						uvd_table->entries[i].v);
	}

	if (vce_table->count) {
		for (i = 0; i < vce_table->count; i++)
			vce_table->entries[i].v =
				cz_convert_8bit_index_to_voltage(adev,
						vce_table->entries[i].v);
	}

	if (acp_table->count) {
		for (i = 0; i < acp_table->count; i++)
			acp_table->entries[i].v =
				cz_convert_8bit_index_to_voltage(adev,
						acp_table->entries[i].v);
	}

}

static void cz_construct_boot_state(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);

	pi->boot_pl.sclk = pi->sys_info.bootup_sclk;
	pi->boot_pl.vddc_index = pi->sys_info.bootup_nb_voltage_index;
	pi->boot_pl.ds_divider_index = 0;
	pi->boot_pl.ss_divider_index = 0;
	pi->boot_pl.allow_gnb_slow = 1;
	pi->boot_pl.force_nbp_state = 0;
	pi->boot_pl.display_wm = 0;
	pi->boot_pl.vce_wm = 0;

}

static void cz_patch_boot_state(struct amdgpu_device *adev,
				struct cz_ps *ps)
{
	struct cz_power_info *pi = cz_get_pi(adev);

	ps->num_levels = 1;
	ps->levels[0] = pi->boot_pl;
}

union pplib_clock_info {
	struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
	struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
	struct _ATOM_PPLIB_CZ_CLOCK_INFO carrizo;
};

static void cz_parse_pplib_clock_info(struct amdgpu_device *adev,
					struct amdgpu_ps *rps, int index,
					union pplib_clock_info *clock_info)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct cz_ps *ps = cz_get_ps(rps);
	struct cz_pl *pl = &ps->levels[index];
	struct amdgpu_clock_voltage_dependency_table *table =
			&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;

	pl->sclk = table->entries[clock_info->carrizo.index].clk;
	pl->vddc_index = table->entries[clock_info->carrizo.index].v;

	ps->num_levels = index + 1;

	if (pi->caps_sclk_ds) {
		pl->ds_divider_index = 5;
		pl->ss_divider_index = 5;
	}

}

static void cz_parse_pplib_non_clock_info(struct amdgpu_device *adev,
			struct amdgpu_ps *rps,
			struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
			u8 table_rev)
{
	struct cz_ps *ps = cz_get_ps(rps);

	rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
	rps->class = le16_to_cpu(non_clock_info->usClassification);
	rps->class2 = le16_to_cpu(non_clock_info->usClassification2);

	if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
		rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
		rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
	} else {
		rps->vclk = 0;
		rps->dclk = 0;
	}

	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
		adev->pm.dpm.boot_ps = rps;
		cz_patch_boot_state(adev, ps);
	}
	if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
		adev->pm.dpm.uvd_ps = rps;

}

union power_info {
	struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
	struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
	struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
	struct _ATOM_PPLIB_POWERPLAYTABLE4 pplib4;
	struct _ATOM_PPLIB_POWERPLAYTABLE5 pplib5;
};

union pplib_power_state {
	struct _ATOM_PPLIB_STATE v1;
	struct _ATOM_PPLIB_STATE_V2 v2;
};

static int cz_parse_power_table(struct amdgpu_device *adev)
{
	struct amdgpu_mode_info *mode_info = &adev->mode_info;
	struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
	union pplib_power_state *power_state;
	int i, j, k, non_clock_array_index, clock_array_index;
	union pplib_clock_info *clock_info;
	struct _StateArray *state_array;
	struct _ClockInfoArray *clock_info_array;
	struct _NonClockInfoArray *non_clock_info_array;
	union power_info *power_info;
	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
	u16 data_offset;
	u8 frev, crev;
	u8 *power_state_offset;
	struct cz_ps *ps;

	if (!amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
				    &frev, &crev, &data_offset))
		return -EINVAL;
	power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);

	state_array = (struct _StateArray *)
		(mode_info->atom_context->bios + data_offset +
		le16_to_cpu(power_info->pplib.usStateArrayOffset));
	clock_info_array = (struct _ClockInfoArray *)
		(mode_info->atom_context->bios + data_offset +
		le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
	non_clock_info_array = (struct _NonClockInfoArray *)
		(mode_info->atom_context->bios + data_offset +
		le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));

	adev->pm.dpm.ps = kzalloc(sizeof(struct amdgpu_ps) *
					state_array->ucNumEntries, GFP_KERNEL);

	if (!adev->pm.dpm.ps)
		return -ENOMEM;

	power_state_offset = (u8 *)state_array->states;
	adev->pm.dpm.platform_caps =
			le32_to_cpu(power_info->pplib.ulPlatformCaps);
	adev->pm.dpm.backbias_response_time =
			le16_to_cpu(power_info->pplib.usBackbiasTime);
	adev->pm.dpm.voltage_response_time =
			le16_to_cpu(power_info->pplib.usVoltageTime);

	for (i = 0; i < state_array->ucNumEntries; i++) {
		power_state = (union pplib_power_state *)power_state_offset;
		non_clock_array_index = power_state->v2.nonClockInfoIndex;
		non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
			&non_clock_info_array->nonClockInfo[non_clock_array_index];

		ps = kzalloc(sizeof(struct cz_ps), GFP_KERNEL);
		if (ps == NULL) {
			kfree(adev->pm.dpm.ps);
			return -ENOMEM;
		}

		adev->pm.dpm.ps[i].ps_priv = ps;
		k = 0;
		for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
			clock_array_index = power_state->v2.clockInfoIndex[j];
			if (clock_array_index >= clock_info_array->ucNumEntries)
				continue;
			if (k >= CZ_MAX_HARDWARE_POWERLEVELS)
				break;
			clock_info = (union pplib_clock_info *)
				&clock_info_array->clockInfo[clock_array_index *
				clock_info_array->ucEntrySize];
			cz_parse_pplib_clock_info(adev, &adev->pm.dpm.ps[i],
				k, clock_info);
			k++;
		}
		cz_parse_pplib_non_clock_info(adev, &adev->pm.dpm.ps[i],
					non_clock_info,
					non_clock_info_array->ucEntrySize);
		power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
	}
	adev->pm.dpm.num_ps = state_array->ucNumEntries;

	return 0;
}

static int cz_process_firmware_header(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	u32 tmp;
	int ret;

	ret = cz_read_smc_sram_dword(adev, SMU8_FIRMWARE_HEADER_LOCATION +
				     offsetof(struct SMU8_Firmware_Header,
				     DpmTable),
				     &tmp, pi->sram_end);

	if (ret == 0)
		pi->dpm_table_start = tmp;

	return ret;
}

static int cz_dpm_init(struct amdgpu_device *adev)
{
	struct cz_power_info *pi;
	int ret, i;

	pi = kzalloc(sizeof(struct cz_power_info), GFP_KERNEL);
	if (NULL == pi)
		return -ENOMEM;

	adev->pm.dpm.priv = pi;

	ret = amdgpu_get_platform_caps(adev);
	if (ret)
		return ret;

	ret = amdgpu_parse_extended_power_table(adev);
	if (ret)
		return ret;

	pi->sram_end = SMC_RAM_END;

	/* set up DPM defaults */
	for (i = 0; i < CZ_MAX_HARDWARE_POWERLEVELS; i++)
		pi->active_target[i] = CZ_AT_DFLT;

	pi->mgcg_cgtt_local0 = 0x0;
	pi->mgcg_cgtt_local1 = 0x0;
	pi->clock_slow_down_step = 25000;
	pi->skip_clock_slow_down = 1;
428
	pi->enable_nb_ps_policy = 0;
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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	pi->caps_power_containment = true;
	pi->caps_cac = true;
	pi->didt_enabled = false;
	if (pi->didt_enabled) {
		pi->caps_sq_ramping = true;
		pi->caps_db_ramping = true;
		pi->caps_td_ramping = true;
		pi->caps_tcp_ramping = true;
	}
	pi->caps_sclk_ds = true;
	pi->voting_clients = 0x00c00033;
	pi->auto_thermal_throttling_enabled = true;
	pi->bapm_enabled = false;
	pi->disable_nb_ps3_in_battery = false;
	pi->voltage_drop_threshold = 0;
	pi->caps_sclk_throttle_low_notification = false;
	pi->gfx_pg_threshold = 500;
	pi->caps_fps = true;
	/* uvd */
	pi->caps_uvd_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_UVD) ? true : false;
	pi->caps_uvd_dpm = true;
	/* vce */
	pi->caps_vce_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_VCE) ? true : false;
	pi->caps_vce_dpm = true;
	/* acp */
	pi->caps_acp_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_ACP) ? true : false;
	pi->caps_acp_dpm = true;

	pi->caps_stable_power_state = false;
	pi->nb_dpm_enabled_by_driver = true;
	pi->nb_dpm_enabled = false;
	pi->caps_voltage_island = false;
	/* flags which indicate need to upload pptable */
	pi->need_pptable_upload = true;

	ret = cz_parse_sys_info_table(adev);
	if (ret)
		return ret;

	cz_patch_voltage_values(adev);
	cz_construct_boot_state(adev);

	ret = cz_parse_power_table(adev);
	if (ret)
		return ret;

	ret = cz_process_firmware_header(adev);
	if (ret)
		return ret;

	pi->dpm_enabled = true;
480
	pi->uvd_dynamic_pg = false;
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

	return 0;
}

static void cz_dpm_fini(struct amdgpu_device *adev)
{
	int i;

	for (i = 0; i < adev->pm.dpm.num_ps; i++)
		kfree(adev->pm.dpm.ps[i].ps_priv);

	kfree(adev->pm.dpm.ps);
	kfree(adev->pm.dpm.priv);
	amdgpu_free_extended_power_table(adev);
}

static void
cz_dpm_debugfs_print_current_performance_level(struct amdgpu_device *adev,
					       struct seq_file *m)
{
	struct amdgpu_clock_voltage_dependency_table *table =
		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
	u32 current_index =
		(RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX) &
		TARGET_AND_CURRENT_PROFILE_INDEX__CURR_SCLK_INDEX_MASK) >>
		TARGET_AND_CURRENT_PROFILE_INDEX__CURR_SCLK_INDEX__SHIFT;
	u32 sclk, tmp;
	u16 vddc;

	if (current_index >= NUM_SCLK_LEVELS) {
		seq_printf(m, "invalid dpm profile %d\n", current_index);
	} else {
		sclk = table->entries[current_index].clk;
		tmp = (RREG32_SMC(ixSMU_VOLTAGE_STATUS) &
			SMU_VOLTAGE_STATUS__SMU_VOLTAGE_CURRENT_LEVEL_MASK) >>
			SMU_VOLTAGE_STATUS__SMU_VOLTAGE_CURRENT_LEVEL__SHIFT;
		vddc = cz_convert_8bit_index_to_voltage(adev, (u16)tmp);
		seq_printf(m, "power level %d    sclk: %u vddc: %u\n",
			   current_index, sclk, vddc);
	}
}

static void cz_dpm_print_power_state(struct amdgpu_device *adev,
					struct amdgpu_ps *rps)
{
	int i;
	struct cz_ps *ps = cz_get_ps(rps);

	amdgpu_dpm_print_class_info(rps->class, rps->class2);
	amdgpu_dpm_print_cap_info(rps->caps);

	DRM_INFO("\tuvd    vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
	for (i = 0; i < ps->num_levels; i++) {
		struct cz_pl *pl = &ps->levels[i];

		DRM_INFO("\t\tpower level %d    sclk: %u vddc: %u\n",
		       i, pl->sclk,
		       cz_convert_8bit_index_to_voltage(adev, pl->vddc_index));
	}

	amdgpu_dpm_print_ps_status(adev, rps);
}

static void cz_dpm_set_funcs(struct amdgpu_device *adev);

546
static int cz_dpm_early_init(void *handle)
547
{
548 549
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

550 551 552 553 554
	cz_dpm_set_funcs(adev);

	return 0;
}

555

556
static int cz_dpm_late_init(void *handle)
557
{
558 559
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

S
Sonny Jiang 已提交
560 561 562 563 564
	if (amdgpu_dpm) {
		/* powerdown unused blocks for now */
		cz_dpm_powergate_uvd(adev, true);
		cz_dpm_powergate_vce(adev, true);
	}
565 566 567 568

	return 0;
}

569
static int cz_dpm_sw_init(void *handle)
570
{
571
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
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 610 611 612 613
	int ret = 0;
	/* fix me to add thermal support TODO */

	/* default to balanced state */
	adev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
	adev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
	adev->pm.dpm.forced_level = AMDGPU_DPM_FORCED_LEVEL_AUTO;
	adev->pm.default_sclk = adev->clock.default_sclk;
	adev->pm.default_mclk = adev->clock.default_mclk;
	adev->pm.current_sclk = adev->clock.default_sclk;
	adev->pm.current_mclk = adev->clock.default_mclk;
	adev->pm.int_thermal_type = THERMAL_TYPE_NONE;

	if (amdgpu_dpm == 0)
		return 0;

	mutex_lock(&adev->pm.mutex);
	ret = cz_dpm_init(adev);
	if (ret)
		goto dpm_init_failed;

	adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps = adev->pm.dpm.boot_ps;
	if (amdgpu_dpm == 1)
		amdgpu_pm_print_power_states(adev);

	ret = amdgpu_pm_sysfs_init(adev);
	if (ret)
		goto dpm_init_failed;

	mutex_unlock(&adev->pm.mutex);
	DRM_INFO("amdgpu: dpm initialized\n");

	return 0;

dpm_init_failed:
	cz_dpm_fini(adev);
	mutex_unlock(&adev->pm.mutex);
	DRM_ERROR("amdgpu: dpm initialization failed\n");

	return ret;
}

614
static int cz_dpm_sw_fini(void *handle)
615
{
616 617
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 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 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 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 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
	mutex_lock(&adev->pm.mutex);
	amdgpu_pm_sysfs_fini(adev);
	cz_dpm_fini(adev);
	mutex_unlock(&adev->pm.mutex);

	return 0;
}

static void cz_reset_ap_mask(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);

	pi->active_process_mask = 0;

}

static int cz_dpm_download_pptable_from_smu(struct amdgpu_device *adev,
							void **table)
{
	int ret = 0;

	ret = cz_smu_download_pptable(adev, table);

	return ret;
}

static int cz_dpm_upload_pptable_to_smu(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct SMU8_Fusion_ClkTable *clock_table;
	struct atom_clock_dividers dividers;
	void *table = NULL;
	uint8_t i = 0;
	int ret = 0;

	struct amdgpu_clock_voltage_dependency_table *vddc_table =
		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
	struct amdgpu_clock_voltage_dependency_table *vddgfx_table =
		&adev->pm.dpm.dyn_state.vddgfx_dependency_on_sclk;
	struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
		&adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
	struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
		&adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
	struct amdgpu_clock_voltage_dependency_table *acp_table =
		&adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;

	if (!pi->need_pptable_upload)
		return 0;

	ret = cz_dpm_download_pptable_from_smu(adev, &table);
	if (ret) {
		DRM_ERROR("amdgpu: Failed to get power play table from SMU!\n");
		return -EINVAL;
	}

	clock_table = (struct SMU8_Fusion_ClkTable *)table;
	/* patch clock table */
	if (vddc_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
			vddgfx_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
			uvd_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
			vce_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
			acp_table->count > CZ_MAX_HARDWARE_POWERLEVELS) {
		DRM_ERROR("amdgpu: Invalid Clock Voltage Dependency Table!\n");
		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;
		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
				clock_table->SclkBreakdownTable.ClkLevel[i].Frequency,
				false, &dividers);
		if (ret)
			return ret;
		clock_table->SclkBreakdownTable.ClkLevel[i].DfsDid =
						(uint8_t)dividers.post_divider;

		/* vddgfx sclk */
		clock_table->SclkBreakdownTable.ClkLevel[i].GfxVid =
			(i < vddgfx_table->count) ? (uint8_t)vddgfx_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].clk : 0;
		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
				clock_table->SclkBreakdownTable.ClkLevel[i].Frequency,
				false, &dividers);
		if (ret)
			return ret;
		clock_table->AclkBreakdownTable.ClkLevel[i].DfsDid =
						(uint8_t)dividers.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;
		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
				clock_table->VclkBreakdownTable.ClkLevel[i].Frequency,
				false, &dividers);
		if (ret)
			return ret;
		clock_table->VclkBreakdownTable.ClkLevel[i].DfsDid =
						(uint8_t)dividers.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;
		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
				clock_table->DclkBreakdownTable.ClkLevel[i].Frequency,
				false, &dividers);
		if (ret)
			return ret;
		clock_table->DclkBreakdownTable.ClkLevel[i].DfsDid =
						(uint8_t)dividers.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;
		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
				clock_table->EclkBreakdownTable.ClkLevel[i].Frequency,
				false, &dividers);
		if (ret)
			return ret;
		clock_table->EclkBreakdownTable.ClkLevel[i].DfsDid =
						(uint8_t)dividers.post_divider;
	}

	/* its time to upload to SMU */
	ret = cz_smu_upload_pptable(adev);
	if (ret) {
		DRM_ERROR("amdgpu: Failed to put power play table to SMU!\n");
		return ret;
	}

	return 0;
}

static void cz_init_sclk_limit(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_clock_voltage_dependency_table *table =
		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
	uint32_t clock = 0, level;

	if (!table || !table->count) {
		DRM_ERROR("Invalid Voltage Dependency table.\n");
		return;
	}

	pi->sclk_dpm.soft_min_clk = 0;
	pi->sclk_dpm.hard_min_clk = 0;
	cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxSclkLevel);
	level = cz_get_argument(adev);
	if (level < table->count)
		clock = table->entries[level].clk;
	else {
		DRM_ERROR("Invalid SLCK Voltage Dependency table entry.\n");
		clock = table->entries[table->count - 1].clk;
	}

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

}

static void cz_init_uvd_limit(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_uvd_clock_voltage_dependency_table *table =
		&adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
	uint32_t clock = 0, level;

	if (!table || !table->count) {
		DRM_ERROR("Invalid Voltage Dependency table.\n");
		return;
	}

	pi->uvd_dpm.soft_min_clk = 0;
	pi->uvd_dpm.hard_min_clk = 0;
	cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxUvdLevel);
	level = cz_get_argument(adev);
	if (level < table->count)
		clock = table->entries[level].vclk;
	else {
		DRM_ERROR("Invalid UVD Voltage Dependency table entry.\n");
		clock = table->entries[table->count - 1].vclk;
	}

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

}

static void cz_init_vce_limit(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_vce_clock_voltage_dependency_table *table =
		&adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
	uint32_t clock = 0, level;

	if (!table || !table->count) {
		DRM_ERROR("Invalid Voltage Dependency table.\n");
		return;
	}

S
Sonny Jiang 已提交
833 834
	pi->vce_dpm.soft_min_clk = table->entries[0].ecclk;
	pi->vce_dpm.hard_min_clk = table->entries[0].ecclk;
835 836 837
	cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxEclkLevel);
	level = cz_get_argument(adev);
	if (level < table->count)
S
Sonny Jiang 已提交
838
		clock = table->entries[level].ecclk;
839 840 841
	else {
		/* future BIOS would fix this error */
		DRM_ERROR("Invalid VCE Voltage Dependency table entry.\n");
S
Sonny Jiang 已提交
842
		clock = table->entries[table->count - 1].ecclk;
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 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 959 960 961 962 963 964 965 966 967 968 969 970 971 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
	}

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

}

static void cz_init_acp_limit(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_clock_voltage_dependency_table *table =
		&adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
	uint32_t clock = 0, level;

	if (!table || !table->count) {
		DRM_ERROR("Invalid Voltage Dependency table.\n");
		return;
	}

	pi->acp_dpm.soft_min_clk = 0;
	pi->acp_dpm.hard_min_clk = 0;
	cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxAclkLevel);
	level = cz_get_argument(adev);
	if (level < table->count)
		clock = table->entries[level].clk;
	else {
		DRM_ERROR("Invalid ACP Voltage Dependency table entry.\n");
		clock = table->entries[table->count - 1].clk;
	}

	pi->acp_dpm.soft_max_clk = clock;
	pi->acp_dpm.hard_max_clk = clock;

}

static void cz_init_pg_state(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);

	pi->uvd_power_gated = false;
	pi->vce_power_gated = false;
	pi->acp_power_gated = false;

}

static void cz_init_sclk_threshold(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);

	pi->low_sclk_interrupt_threshold = 0;

}

static void cz_dpm_setup_asic(struct amdgpu_device *adev)
{
	cz_reset_ap_mask(adev);
	cz_dpm_upload_pptable_to_smu(adev);
	cz_init_sclk_limit(adev);
	cz_init_uvd_limit(adev);
	cz_init_vce_limit(adev);
	cz_init_acp_limit(adev);
	cz_init_pg_state(adev);
	cz_init_sclk_threshold(adev);

}

static bool cz_check_smu_feature(struct amdgpu_device *adev,
				uint32_t feature)
{
	uint32_t smu_feature = 0;
	int ret;

	ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_GetFeatureStatus, 0);
	if (ret) {
		DRM_ERROR("Failed to get SMU features from SMC.\n");
		return false;
	} else {
		smu_feature = cz_get_argument(adev);
		if (feature & smu_feature)
			return true;
	}

	return false;
}

static bool cz_check_for_dpm_enabled(struct amdgpu_device *adev)
{
	if (cz_check_smu_feature(adev,
				SMU_EnabledFeatureScoreboard_SclkDpmOn))
		return true;

	return false;
}

static void cz_program_voting_clients(struct amdgpu_device *adev)
{
	WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, PPCZ_VOTINGRIGHTSCLIENTS_DFLT0);
}

static void cz_clear_voting_clients(struct amdgpu_device *adev)
{
	WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, 0);
}

static int cz_start_dpm(struct amdgpu_device *adev)
{
	int ret = 0;

	if (amdgpu_dpm) {
		ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_EnableAllSmuFeatures, SCLK_DPM_MASK);
		if (ret) {
			DRM_ERROR("SMU feature: SCLK_DPM enable failed\n");
			return -EINVAL;
		}
	}

	return 0;
}

static int cz_stop_dpm(struct amdgpu_device *adev)
{
	int ret = 0;

	if (amdgpu_dpm && adev->pm.dpm_enabled) {
		ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_DisableAllSmuFeatures, SCLK_DPM_MASK);
		if (ret) {
			DRM_ERROR("SMU feature: SCLK_DPM disable failed\n");
			return -EINVAL;
		}
	}

	return 0;
}

static uint32_t cz_get_sclk_level(struct amdgpu_device *adev,
				uint32_t clock, uint16_t msg)
{
	int i = 0;
	struct amdgpu_clock_voltage_dependency_table *table =
		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;

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

	return i;
}

S
Sonny Jiang 已提交
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
static uint32_t cz_get_eclk_level(struct amdgpu_device *adev,
				uint32_t clock, uint16_t msg)
{
	int i = 0;
	struct amdgpu_vce_clock_voltage_dependency_table *table =
		&adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;

	if (table->count == 0)
		return 0;

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

	return i;
}

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 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 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 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
static int cz_program_bootup_state(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	uint32_t soft_min_clk = 0;
	uint32_t soft_max_clk = 0;
	int ret = 0;

	pi->sclk_dpm.soft_min_clk = pi->sys_info.bootup_sclk;
	pi->sclk_dpm.soft_max_clk = pi->sys_info.bootup_sclk;

	soft_min_clk = cz_get_sclk_level(adev,
				pi->sclk_dpm.soft_min_clk,
				PPSMC_MSG_SetSclkSoftMin);
	soft_max_clk = cz_get_sclk_level(adev,
				pi->sclk_dpm.soft_max_clk,
				PPSMC_MSG_SetSclkSoftMax);

	ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetSclkSoftMin, soft_min_clk);
	if (ret)
		return -EINVAL;

	ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetSclkSoftMax, soft_max_clk);
	if (ret)
		return -EINVAL;

	return 0;
}

/* TODO */
static int cz_disable_cgpg(struct amdgpu_device *adev)
{
	return 0;
}

/* TODO */
static int cz_enable_cgpg(struct amdgpu_device *adev)
{
	return 0;
}

/* TODO */
static int cz_program_pt_config_registers(struct amdgpu_device *adev)
{
	return 0;
}

static void cz_do_enable_didt(struct amdgpu_device *adev, bool enable)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	uint32_t reg = 0;

	if (pi->caps_sq_ramping) {
		reg = RREG32_DIDT(ixDIDT_SQ_CTRL0);
		if (enable)
			reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 1);
		else
			reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 0);
		WREG32_DIDT(ixDIDT_SQ_CTRL0, reg);
	}
	if (pi->caps_db_ramping) {
		reg = RREG32_DIDT(ixDIDT_DB_CTRL0);
		if (enable)
			reg = REG_SET_FIELD(reg, DIDT_DB_CTRL0, DIDT_CTRL_EN, 1);
		else
			reg = REG_SET_FIELD(reg, DIDT_DB_CTRL0, DIDT_CTRL_EN, 0);
		WREG32_DIDT(ixDIDT_DB_CTRL0, reg);
	}
	if (pi->caps_td_ramping) {
		reg = RREG32_DIDT(ixDIDT_TD_CTRL0);
		if (enable)
			reg = REG_SET_FIELD(reg, DIDT_TD_CTRL0, DIDT_CTRL_EN, 1);
		else
			reg = REG_SET_FIELD(reg, DIDT_TD_CTRL0, DIDT_CTRL_EN, 0);
		WREG32_DIDT(ixDIDT_TD_CTRL0, reg);
	}
	if (pi->caps_tcp_ramping) {
		reg = RREG32_DIDT(ixDIDT_TCP_CTRL0);
		if (enable)
			reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 1);
		else
			reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 0);
		WREG32_DIDT(ixDIDT_TCP_CTRL0, reg);
	}

}

static int cz_enable_didt(struct amdgpu_device *adev, bool enable)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	int ret;

	if (pi->caps_sq_ramping || pi->caps_db_ramping ||
			pi->caps_td_ramping || pi->caps_tcp_ramping) {
		if (adev->gfx.gfx_current_status != AMDGPU_GFX_SAFE_MODE) {
			ret = cz_disable_cgpg(adev);
			if (ret) {
				DRM_ERROR("Pre Di/Dt disable cg/pg failed\n");
				return -EINVAL;
			}
			adev->gfx.gfx_current_status = AMDGPU_GFX_SAFE_MODE;
		}

		ret = cz_program_pt_config_registers(adev);
		if (ret) {
			DRM_ERROR("Di/Dt config failed\n");
			return -EINVAL;
		}
		cz_do_enable_didt(adev, enable);

		if (adev->gfx.gfx_current_status == AMDGPU_GFX_SAFE_MODE) {
			ret = cz_enable_cgpg(adev);
			if (ret) {
				DRM_ERROR("Post Di/Dt enable cg/pg failed\n");
				return -EINVAL;
			}
			adev->gfx.gfx_current_status = AMDGPU_GFX_NORMAL_MODE;
		}
	}

	return 0;
}

/* TODO */
static void cz_reset_acp_boot_level(struct amdgpu_device *adev)
{
}

static void cz_update_current_ps(struct amdgpu_device *adev,
					struct amdgpu_ps *rps)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct cz_ps *ps = cz_get_ps(rps);

	pi->current_ps = *ps;
	pi->current_rps = *rps;
	pi->current_rps.ps_priv = ps;

}

static void cz_update_requested_ps(struct amdgpu_device *adev,
					struct amdgpu_ps *rps)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct cz_ps *ps = cz_get_ps(rps);

	pi->requested_ps = *ps;
	pi->requested_rps = *rps;
	pi->requested_rps.ps_priv = ps;

}

/* PP arbiter support needed TODO */
static void cz_apply_state_adjust_rules(struct amdgpu_device *adev,
					struct amdgpu_ps *new_rps,
					struct amdgpu_ps *old_rps)
{
	struct cz_ps *ps = cz_get_ps(new_rps);
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_clock_and_voltage_limits *limits =
		&adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
	/* 10kHz memory clock */
	uint32_t mclk = 0;

	ps->force_high = false;
	ps->need_dfs_bypass = true;
	pi->video_start = new_rps->dclk || new_rps->vclk ||
				new_rps->evclk || new_rps->ecclk;

	if ((new_rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) ==
			ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
		pi->battery_state = true;
	else
		pi->battery_state = false;

	if (pi->caps_stable_power_state)
		mclk = limits->mclk;

	if (mclk > pi->sys_info.nbp_memory_clock[CZ_NUM_NBPMEMORY_CLOCK - 1])
		ps->force_high = true;

}

static int cz_dpm_enable(struct amdgpu_device *adev)
{
	int ret = 0;

	/* renable will hang up SMU, so check first */
	if (cz_check_for_dpm_enabled(adev))
		return -EINVAL;

	cz_program_voting_clients(adev);

	ret = cz_start_dpm(adev);
	if (ret) {
		DRM_ERROR("Carrizo DPM enable failed\n");
		return -EINVAL;
	}

	ret = cz_program_bootup_state(adev);
	if (ret) {
		DRM_ERROR("Carrizo bootup state program failed\n");
		return -EINVAL;
	}

	ret = cz_enable_didt(adev, true);
	if (ret) {
		DRM_ERROR("Carrizo enable di/dt failed\n");
		return -EINVAL;
	}

	cz_reset_acp_boot_level(adev);

	cz_update_current_ps(adev, adev->pm.dpm.boot_ps);

	return 0;
}

1260
static int cz_dpm_hw_init(void *handle)
1261
{
1262
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
S
Sonny Jiang 已提交
1263
	int ret = 0;
1264 1265 1266

	mutex_lock(&adev->pm.mutex);

1267 1268 1269 1270
	/* smu init only needs to be called at startup, not resume.
	 * It should be in sw_init, but requires the fw info gathered
	 * in sw_init from other IP modules.
	 */
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	ret = cz_smu_init(adev);
	if (ret) {
		DRM_ERROR("amdgpu: smc initialization failed\n");
		mutex_unlock(&adev->pm.mutex);
		return ret;
	}

	/* do the actual fw loading */
	ret = cz_smu_start(adev);
	if (ret) {
		DRM_ERROR("amdgpu: smc start failed\n");
		mutex_unlock(&adev->pm.mutex);
		return ret;
	}

S
Sonny Jiang 已提交
1286 1287 1288 1289 1290 1291
	if (!amdgpu_dpm) {
		adev->pm.dpm_enabled = false;
		mutex_unlock(&adev->pm.mutex);
		return ret;
	}

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
	/* cz dpm setup asic */
	cz_dpm_setup_asic(adev);

	/* cz dpm enable */
	ret = cz_dpm_enable(adev);
	if (ret)
		adev->pm.dpm_enabled = false;
	else
		adev->pm.dpm_enabled = true;

	mutex_unlock(&adev->pm.mutex);

	return 0;
}

static int cz_dpm_disable(struct amdgpu_device *adev)
{
	int ret = 0;

	if (!cz_check_for_dpm_enabled(adev))
		return -EINVAL;

	ret = cz_enable_didt(adev, false);
	if (ret) {
		DRM_ERROR("Carrizo disable di/dt failed\n");
		return -EINVAL;
	}

1320 1321
	/* powerup blocks */
	cz_dpm_powergate_uvd(adev, false);
S
Sonny Jiang 已提交
1322
	cz_dpm_powergate_vce(adev, false);
1323

1324 1325 1326 1327 1328 1329 1330
	cz_clear_voting_clients(adev);
	cz_stop_dpm(adev);
	cz_update_current_ps(adev, adev->pm.dpm.boot_ps);

	return 0;
}

1331
static int cz_dpm_hw_fini(void *handle)
1332 1333
{
	int ret = 0;
1334
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1335 1336 1337

	mutex_lock(&adev->pm.mutex);

1338 1339 1340 1341
	/* smu fini only needs to be called at teardown, not suspend.
	 * It should be in sw_fini, but we put it here for symmetry
	 * with smu init.
	 */
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
	cz_smu_fini(adev);

	if (adev->pm.dpm_enabled) {
		ret = cz_dpm_disable(adev);

		adev->pm.dpm.current_ps =
			adev->pm.dpm.requested_ps =
			adev->pm.dpm.boot_ps;
	}

	adev->pm.dpm_enabled = false;

	mutex_unlock(&adev->pm.mutex);

1356
	return ret;
1357 1358
}

1359
static int cz_dpm_suspend(void *handle)
1360 1361
{
	int ret = 0;
1362
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375

	if (adev->pm.dpm_enabled) {
		mutex_lock(&adev->pm.mutex);

		ret = cz_dpm_disable(adev);

		adev->pm.dpm.current_ps =
			adev->pm.dpm.requested_ps =
			adev->pm.dpm.boot_ps;

		mutex_unlock(&adev->pm.mutex);
	}

1376
	return ret;
1377 1378
}

1379
static int cz_dpm_resume(void *handle)
1380 1381
{
	int ret = 0;
1382
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393

	mutex_lock(&adev->pm.mutex);

	/* do the actual fw loading */
	ret = cz_smu_start(adev);
	if (ret) {
		DRM_ERROR("amdgpu: smc start failed\n");
		mutex_unlock(&adev->pm.mutex);
		return ret;
	}

S
Sonny Jiang 已提交
1394 1395 1396 1397 1398 1399
	if (!amdgpu_dpm) {
		adev->pm.dpm_enabled = false;
		mutex_unlock(&adev->pm.mutex);
		return ret;
	}

1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
	/* cz dpm setup asic */
	cz_dpm_setup_asic(adev);

	/* cz dpm enable */
	ret = cz_dpm_enable(adev);
	if (ret)
		adev->pm.dpm_enabled = false;
	else
		adev->pm.dpm_enabled = true;

	mutex_unlock(&adev->pm.mutex);
	/* upon resume, re-compute the clocks */
	if (adev->pm.dpm_enabled)
		amdgpu_pm_compute_clocks(adev);

	return 0;
}

1418 1419
static int cz_dpm_set_clockgating_state(void *handle,
					enum amd_clockgating_state state)
1420 1421 1422 1423
{
	return 0;
}

1424 1425
static int cz_dpm_set_powergating_state(void *handle,
					enum amd_powergating_state state)
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 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 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 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 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
{
	return 0;
}

/* borrowed from KV, need future unify */
static int cz_dpm_get_temperature(struct amdgpu_device *adev)
{
	int actual_temp = 0;
	uint32_t temp = RREG32_SMC(0xC0300E0C);

	if (temp)
		actual_temp = 1000 * ((temp / 8) - 49);

	return actual_temp;
}

static int cz_dpm_pre_set_power_state(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_ps requested_ps = *adev->pm.dpm.requested_ps;
	struct amdgpu_ps *new_ps = &requested_ps;

	cz_update_requested_ps(adev, new_ps);
	cz_apply_state_adjust_rules(adev, &pi->requested_rps,
					&pi->current_rps);

	return 0;
}

static int cz_dpm_update_sclk_limit(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_clock_and_voltage_limits *limits =
		&adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
	uint32_t clock, stable_ps_clock = 0;

	clock = pi->sclk_dpm.soft_min_clk;

	if (pi->caps_stable_power_state) {
		stable_ps_clock = limits->sclk * 75 / 100;
		if (clock < stable_ps_clock)
			clock = stable_ps_clock;
	}

	if (clock != pi->sclk_dpm.soft_min_clk) {
		pi->sclk_dpm.soft_min_clk = clock;
		cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetSclkSoftMin,
				cz_get_sclk_level(adev, clock,
					PPSMC_MSG_SetSclkSoftMin));
	}

	if (pi->caps_stable_power_state &&
			pi->sclk_dpm.soft_max_clk != clock) {
		pi->sclk_dpm.soft_max_clk = clock;
		cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetSclkSoftMax,
				cz_get_sclk_level(adev, clock,
					PPSMC_MSG_SetSclkSoftMax));
	} else {
		cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetSclkSoftMax,
				cz_get_sclk_level(adev,
					pi->sclk_dpm.soft_max_clk,
					PPSMC_MSG_SetSclkSoftMax));
	}

	return 0;
}

static int cz_dpm_set_deep_sleep_sclk_threshold(struct amdgpu_device *adev)
{
	int ret = 0;
	struct cz_power_info *pi = cz_get_pi(adev);

	if (pi->caps_sclk_ds) {
		cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetMinDeepSleepSclk,
				CZ_MIN_DEEP_SLEEP_SCLK);
	}

	return ret;
}

/* ?? without dal support, is this still needed in setpowerstate list*/
static int cz_dpm_set_watermark_threshold(struct amdgpu_device *adev)
{
	int ret = 0;
	struct cz_power_info *pi = cz_get_pi(adev);

	cz_send_msg_to_smc_with_parameter(adev,
			PPSMC_MSG_SetWatermarkFrequency,
			pi->sclk_dpm.soft_max_clk);

	return ret;
}

static int cz_dpm_enable_nbdpm(struct amdgpu_device *adev)
{
	int ret = 0;
	struct cz_power_info *pi = cz_get_pi(adev);

	/* also depend on dal NBPStateDisableRequired */
	if (pi->nb_dpm_enabled_by_driver && !pi->nb_dpm_enabled) {
		ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_EnableAllSmuFeatures,
				NB_DPM_MASK);
		if (ret) {
			DRM_ERROR("amdgpu: nb dpm enable failed\n");
			return ret;
		}
		pi->nb_dpm_enabled = true;
	}

	return ret;
}

static void cz_dpm_nbdpm_lm_pstate_enable(struct amdgpu_device *adev,
							bool enable)
{
	if (enable)
		cz_send_msg_to_smc(adev, PPSMC_MSG_EnableLowMemoryPstate);
	else
		cz_send_msg_to_smc(adev, PPSMC_MSG_DisableLowMemoryPstate);

}

static int cz_dpm_update_low_memory_pstate(struct amdgpu_device *adev)
{
	int ret = 0;
	struct cz_power_info *pi = cz_get_pi(adev);
	struct cz_ps *ps = &pi->requested_ps;

	if (pi->sys_info.nb_dpm_enable) {
		if (ps->force_high)
			cz_dpm_nbdpm_lm_pstate_enable(adev, true);
		else
			cz_dpm_nbdpm_lm_pstate_enable(adev, false);
	}

	return ret;
}

/* with dpm enabled */
static int cz_dpm_set_power_state(struct amdgpu_device *adev)
{
	int ret = 0;

	cz_dpm_update_sclk_limit(adev);
	cz_dpm_set_deep_sleep_sclk_threshold(adev);
	cz_dpm_set_watermark_threshold(adev);
	cz_dpm_enable_nbdpm(adev);
	cz_dpm_update_low_memory_pstate(adev);

	return ret;
}

static void cz_dpm_post_set_power_state(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_ps *ps = &pi->requested_rps;

	cz_update_current_ps(adev, ps);

}

static int cz_dpm_force_highest(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	int ret = 0;

	if (pi->sclk_dpm.soft_min_clk != pi->sclk_dpm.soft_max_clk) {
		pi->sclk_dpm.soft_min_clk =
			pi->sclk_dpm.soft_max_clk;
		ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetSclkSoftMin,
				cz_get_sclk_level(adev,
					pi->sclk_dpm.soft_min_clk,
					PPSMC_MSG_SetSclkSoftMin));
		if (ret)
			return ret;
	}

	return ret;
}

static int cz_dpm_force_lowest(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	int ret = 0;

	if (pi->sclk_dpm.soft_max_clk != pi->sclk_dpm.soft_min_clk) {
		pi->sclk_dpm.soft_max_clk = pi->sclk_dpm.soft_min_clk;
		ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetSclkSoftMax,
				cz_get_sclk_level(adev,
					pi->sclk_dpm.soft_max_clk,
					PPSMC_MSG_SetSclkSoftMax));
		if (ret)
			return ret;
	}

	return ret;
}

static uint32_t cz_dpm_get_max_sclk_level(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);

	if (!pi->max_sclk_level) {
		cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxSclkLevel);
		pi->max_sclk_level = cz_get_argument(adev) + 1;
	}

	if (pi->max_sclk_level > CZ_MAX_HARDWARE_POWERLEVELS) {
		DRM_ERROR("Invalid max sclk level!\n");
		return -EINVAL;
	}

	return pi->max_sclk_level;
}

static int cz_dpm_unforce_dpm_levels(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_clock_voltage_dependency_table *dep_table =
		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
	uint32_t level = 0;
	int ret = 0;

	pi->sclk_dpm.soft_min_clk = dep_table->entries[0].clk;
	level = cz_dpm_get_max_sclk_level(adev) - 1;
	if (level < dep_table->count)
		pi->sclk_dpm.soft_max_clk = dep_table->entries[level].clk;
	else
		pi->sclk_dpm.soft_max_clk =
			dep_table->entries[dep_table->count - 1].clk;

	/* get min/max sclk soft value
	 * notify SMU to execute */
	ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetSclkSoftMin,
				cz_get_sclk_level(adev,
					pi->sclk_dpm.soft_min_clk,
					PPSMC_MSG_SetSclkSoftMin));
	if (ret)
		return ret;

	ret = cz_send_msg_to_smc_with_parameter(adev,
				PPSMC_MSG_SetSclkSoftMax,
				cz_get_sclk_level(adev,
					pi->sclk_dpm.soft_max_clk,
					PPSMC_MSG_SetSclkSoftMax));
	if (ret)
		return ret;

	DRM_INFO("DPM unforce state min=%d, max=%d.\n",
			pi->sclk_dpm.soft_min_clk,
			pi->sclk_dpm.soft_max_clk);

	return 0;
}

static int cz_dpm_force_dpm_level(struct amdgpu_device *adev,
				enum amdgpu_dpm_forced_level level)
{
	int ret = 0;

	switch (level) {
	case AMDGPU_DPM_FORCED_LEVEL_HIGH:
		ret = cz_dpm_force_highest(adev);
		if (ret)
			return ret;
		break;
	case AMDGPU_DPM_FORCED_LEVEL_LOW:
		ret = cz_dpm_force_lowest(adev);
		if (ret)
			return ret;
		break;
	case AMDGPU_DPM_FORCED_LEVEL_AUTO:
		ret = cz_dpm_unforce_dpm_levels(adev);
		if (ret)
			return ret;
		break;
	default:
		break;
	}

	return ret;
}

/* fix me, display configuration change lists here
 * mostly dal related*/
static void cz_dpm_display_configuration_changed(struct amdgpu_device *adev)
{
}

static uint32_t cz_dpm_get_sclk(struct amdgpu_device *adev, bool low)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct cz_ps *requested_state = cz_get_ps(&pi->requested_rps);

	if (low)
		return requested_state->levels[0].sclk;
	else
		return requested_state->levels[requested_state->num_levels - 1].sclk;

}

static uint32_t cz_dpm_get_mclk(struct amdgpu_device *adev, bool low)
{
	struct cz_power_info *pi = cz_get_pi(adev);

	return pi->sys_info.bootup_uma_clk;
}

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
static int cz_enable_uvd_dpm(struct amdgpu_device *adev, bool enable)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	int ret = 0;

	if (enable && pi->caps_uvd_dpm ) {
		pi->dpm_flags |= DPMFlags_UVD_Enabled;
		DRM_DEBUG("UVD DPM Enabled.\n");

		ret = cz_send_msg_to_smc_with_parameter(adev,
			PPSMC_MSG_EnableAllSmuFeatures, UVD_DPM_MASK);
	} else {
		pi->dpm_flags &= ~DPMFlags_UVD_Enabled;
		DRM_DEBUG("UVD DPM Stopped\n");

		ret = cz_send_msg_to_smc_with_parameter(adev,
			PPSMC_MSG_DisableAllSmuFeatures, UVD_DPM_MASK);
	}

	return ret;
}

static int cz_update_uvd_dpm(struct amdgpu_device *adev, bool gate)
{
	return cz_enable_uvd_dpm(adev, !gate);
}


static void cz_dpm_powergate_uvd(struct amdgpu_device *adev, bool gate)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	int ret;

	if (pi->uvd_power_gated == gate)
		return;

	pi->uvd_power_gated = gate;

	if (gate) {
		if (pi->caps_uvd_pg) {
			/* disable clockgating so we can properly shut down the block */
1783 1784
			ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
							    AMD_CG_STATE_UNGATE);
1785
			/* shutdown the UVD block */
1786 1787
			ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
							    AMD_PG_STATE_GATE);
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
			/* XXX: check for errors */
		}
		cz_update_uvd_dpm(adev, gate);
		if (pi->caps_uvd_pg)
			/* power off the UVD block */
			cz_send_msg_to_smc(adev, PPSMC_MSG_UVDPowerOFF);
	} else {
		if (pi->caps_uvd_pg) {
			/* power on the UVD block */
			if (pi->uvd_dynamic_pg)
				cz_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_UVDPowerON, 1);
			else
				cz_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_UVDPowerON, 0);
			/* re-init the UVD block */
1802 1803
			ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
							    AMD_PG_STATE_UNGATE);
1804
			/* enable clockgating. hw will dynamically gate/ungate clocks on the fly */
1805 1806
			ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
							    AMD_CG_STATE_GATE);
1807 1808 1809 1810 1811 1812
			/* XXX: check for errors */
		}
		cz_update_uvd_dpm(adev, gate);
	}
}

S
Sonny Jiang 已提交
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 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
static int cz_enable_vce_dpm(struct amdgpu_device *adev, bool enable)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	int ret = 0;

	if (enable && pi->caps_vce_dpm) {
		pi->dpm_flags |= DPMFlags_VCE_Enabled;
		DRM_DEBUG("VCE DPM Enabled.\n");

		ret = cz_send_msg_to_smc_with_parameter(adev,
			PPSMC_MSG_EnableAllSmuFeatures, VCE_DPM_MASK);

	} else {
		pi->dpm_flags &= ~DPMFlags_VCE_Enabled;
		DRM_DEBUG("VCE DPM Stopped\n");

		ret = cz_send_msg_to_smc_with_parameter(adev,
			PPSMC_MSG_DisableAllSmuFeatures, VCE_DPM_MASK);
	}

	return ret;
}

static int cz_update_vce_dpm(struct amdgpu_device *adev)
{
	struct cz_power_info *pi = cz_get_pi(adev);
	struct amdgpu_vce_clock_voltage_dependency_table *table =
		&adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;

	/* Stable Pstate is enabled and we need to set the VCE DPM to highest level */
	if (pi->caps_stable_power_state) {
		pi->vce_dpm.hard_min_clk = table->entries[table->count-1].ecclk;

	} else { /* non-stable p-state cases. without vce.Arbiter.EcclkHardMin */
		pi->vce_dpm.hard_min_clk = table->entries[0].ecclk;
	}

	cz_send_msg_to_smc_with_parameter(adev,
		PPSMC_MSG_SetEclkHardMin,
		cz_get_eclk_level(adev,
			pi->vce_dpm.hard_min_clk,
			PPSMC_MSG_SetEclkHardMin));
	return 0;
}

static void cz_dpm_powergate_vce(struct amdgpu_device *adev, bool gate)
{
	struct cz_power_info *pi = cz_get_pi(adev);

	if (pi->caps_vce_pg) {
		if (pi->vce_power_gated != gate) {
			if (gate) {
				/* disable clockgating so we can properly shut down the block */
				amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
							    AMD_CG_STATE_UNGATE);
				/* shutdown the VCE block */
				amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
							    AMD_PG_STATE_GATE);

				cz_enable_vce_dpm(adev, false);
				/* TODO: to figure out why vce can't be poweroff. */
				/* cz_send_msg_to_smc(adev, PPSMC_MSG_VCEPowerOFF); */
				pi->vce_power_gated = true;
			} else {
				cz_send_msg_to_smc(adev, PPSMC_MSG_VCEPowerON);
				pi->vce_power_gated = false;

				/* re-init the VCE block */
				amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
							    AMD_PG_STATE_UNGATE);
				/* enable clockgating. hw will dynamically gate/ungate clocks on the fly */
				amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
							    AMD_CG_STATE_GATE);

				cz_update_vce_dpm(adev);
				cz_enable_vce_dpm(adev, true);
			}
		} else {
			if (! pi->vce_power_gated) {
				cz_update_vce_dpm(adev);
			}
		}
	} else { /*pi->caps_vce_pg*/
		cz_update_vce_dpm(adev);
		cz_enable_vce_dpm(adev, true);
	}

	return;
}

1903
const struct amd_ip_funcs cz_dpm_ip_funcs = {
1904
	.early_init = cz_dpm_early_init,
1905
	.late_init = cz_dpm_late_init,
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
	.sw_init = cz_dpm_sw_init,
	.sw_fini = cz_dpm_sw_fini,
	.hw_init = cz_dpm_hw_init,
	.hw_fini = cz_dpm_hw_fini,
	.suspend = cz_dpm_suspend,
	.resume = cz_dpm_resume,
	.is_idle = NULL,
	.wait_for_idle = NULL,
	.soft_reset = NULL,
	.print_status = NULL,
	.set_clockgating_state = cz_dpm_set_clockgating_state,
	.set_powergating_state = cz_dpm_set_powergating_state,
};

static const struct amdgpu_dpm_funcs cz_dpm_funcs = {
	.get_temperature = cz_dpm_get_temperature,
	.pre_set_power_state = cz_dpm_pre_set_power_state,
	.set_power_state = cz_dpm_set_power_state,
	.post_set_power_state = cz_dpm_post_set_power_state,
	.display_configuration_changed = cz_dpm_display_configuration_changed,
	.get_sclk = cz_dpm_get_sclk,
	.get_mclk = cz_dpm_get_mclk,
	.print_power_state = cz_dpm_print_power_state,
	.debugfs_print_current_performance_level =
				cz_dpm_debugfs_print_current_performance_level,
	.force_performance_level = cz_dpm_force_dpm_level,
	.vblank_too_short = NULL,
1933
	.powergate_uvd = cz_dpm_powergate_uvd,
S
Sonny Jiang 已提交
1934
	.powergate_vce = cz_dpm_powergate_vce,
1935 1936 1937 1938 1939 1940 1941
};

static void cz_dpm_set_funcs(struct amdgpu_device *adev)
{
	if (NULL == adev->pm.funcs)
		adev->pm.funcs = &cz_dpm_funcs;
}