amd_powerplay.c 32.6 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
/*
 * 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.
 *
 */
23
#include "pp_debug.h"
24 25 26
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/gfp.h>
27
#include <linux/slab.h>
28 29
#include "amd_shared.h"
#include "amd_powerplay.h"
30
#include "pp_instance.h"
31
#include "power_state.h"
32

33 34
#define PP_DPM_DISABLED 0xCCCC

R
Rex Zhu 已提交
35 36 37
static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
		void *input, void *output);

38
static inline int pp_check(struct pp_instance *handle)
39
{
40 41
	if (handle == NULL || handle->pp_valid != PP_VALID)
		return -EINVAL;
42

43
	if (handle->hwmgr == NULL || handle->hwmgr->smumgr_funcs == NULL)
44 45
		return -EINVAL;

46 47
	if (handle->pm_en == 0)
		return PP_DPM_DISABLED;
48

49
	if (handle->hwmgr->hwmgr_func == NULL)
50
		return PP_DPM_DISABLED;
51

52 53
	return 0;
}
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
static int amd_powerplay_create(struct amd_pp_init *pp_init,
				void **handle)
{
	struct pp_instance *instance;

	if (pp_init == NULL || handle == NULL)
		return -EINVAL;

	instance = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
	if (instance == NULL)
		return -ENOMEM;

	instance->pp_valid = PP_VALID;
	instance->chip_family = pp_init->chip_family;
	instance->chip_id = pp_init->chip_id;
	instance->pm_en = pp_init->pm_en;
	instance->feature_mask = pp_init->feature_mask;
	instance->device = pp_init->device;
	mutex_init(&instance->pp_lock);
	*handle = instance;
	return 0;
}

static int amd_powerplay_destroy(void *handle)
{
	struct pp_instance *instance = (struct pp_instance *)handle;

	kfree(instance->hwmgr);
	instance->hwmgr = NULL;

	kfree(instance);
	instance = NULL;
	return 0;
}

90 91 92
static int pp_early_init(void *handle)
{
	int ret;
93 94 95 96 97 98
	struct pp_instance *pp_handle = NULL;

	pp_handle = cgs_register_pp_handle(handle, amd_powerplay_create);

	if (!pp_handle)
		return -EINVAL;
99

100
	ret = hwmgr_early_init(pp_handle);
101
	if (ret)
102
		return -EINVAL;
103

104
	return 0;
105 106
}

107
static int pp_sw_init(void *handle)
108
{
109
	struct pp_hwmgr *hwmgr;
110
	int ret = 0;
111
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
112

113
	ret = pp_check(pp_handle);
114

115
	if (ret >= 0) {
116
		hwmgr = pp_handle->hwmgr;
117

118
		if (hwmgr->smumgr_funcs->smu_init == NULL)
119
			return -EINVAL;
120

121
		ret = hwmgr->smumgr_funcs->smu_init(hwmgr);
122

123 124 125 126
		pr_info("amdgpu: powerplay sw initialized\n");
	}
	return ret;
}
127

128 129
static int pp_sw_fini(void *handle)
{
130
	struct pp_hwmgr *hwmgr;
131 132 133 134
	int ret = 0;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;

	ret = pp_check(pp_handle);
135
	if (ret >= 0) {
136
		hwmgr = pp_handle->hwmgr;
137

138
		if (hwmgr->smumgr_funcs->smu_fini == NULL)
139 140
			return -EINVAL;

141
		ret = hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
142
	}
143
	return ret;
144 145 146 147
}

static int pp_hw_init(void *handle)
{
148
	int ret = 0;
149
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
150
	struct pp_hwmgr *hwmgr;
151

152
	ret = pp_check(pp_handle);
153

154
	if (ret >= 0) {
155
		hwmgr = pp_handle->hwmgr;
156

157
		if (hwmgr->smumgr_funcs->start_smu == NULL)
158
			return -EINVAL;
159

160
		if(hwmgr->smumgr_funcs->start_smu(pp_handle->hwmgr)) {
161
			pr_err("smc start failed\n");
162
			hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
163 164 165
			return -EINVAL;;
		}
		if (ret == PP_DPM_DISABLED)
166 167 168 169
			goto exit;
		ret = hwmgr_hw_init(pp_handle);
		if (ret)
			goto exit;
170
	}
171 172
	return ret;
exit:
173
	pp_handle->pm_en = 0;
174 175 176
	cgs_notify_dpm_enabled(hwmgr->device, false);
	return 0;

177 178 179 180
}

static int pp_hw_fini(void *handle)
{
181 182
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
183

184
	ret = pp_check(pp_handle);
185
	if (ret == 0)
186
		hwmgr_hw_fini(pp_handle);
187

188 189 190
	return 0;
}

R
Rex Zhu 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203
static int pp_late_init(void *handle)
{
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;

	ret = pp_check(pp_handle);
	if (ret == 0)
		pp_dpm_dispatch_tasks(pp_handle,
					AMD_PP_TASK_COMPLETE_INIT, NULL, NULL);

	return 0;
}

204 205 206 207 208 209
static void pp_late_fini(void *handle)
{
	amd_powerplay_destroy(handle);
}


210 211
static bool pp_is_idle(void *handle)
{
212
	return false;
213 214 215 216 217 218 219 220 221 222 223 224 225
}

static int pp_wait_for_idle(void *handle)
{
	return 0;
}

static int pp_sw_reset(void *handle)
{
	return 0;
}


226
int amd_set_clockgating_by_smu(void *handle, uint32_t msg_id)
227
{
228
	struct pp_hwmgr  *hwmgr;
229 230
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
231

232
	ret = pp_check(pp_handle);
233

234
	if (!ret)
235
		return ret;
236

237
	hwmgr = pp_handle->hwmgr;
238

239
	if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
240
		pr_info("%s was not implemented.\n", __func__);
241
		return 0;
242
	}
243

244
	return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
245 246 247 248 249
}

static int pp_set_powergating_state(void *handle,
				    enum amd_powergating_state state)
{
250
	struct pp_hwmgr  *hwmgr;
251 252
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
253

254
	ret = pp_check(pp_handle);
255

256
	if (ret)
257
		return ret;
258

259
	hwmgr = pp_handle->hwmgr;
260 261

	if (hwmgr->hwmgr_func->enable_per_cu_power_gating == NULL) {
262
		pr_info("%s was not implemented.\n", __func__);
263 264
		return 0;
	}
265 266 267

	/* Enable/disable GFX per cu powergating through SMU */
	return hwmgr->hwmgr_func->enable_per_cu_power_gating(hwmgr,
268
			state == AMD_PG_STATE_GATE);
269 270 271 272
}

static int pp_suspend(void *handle)
{
273 274
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
275

276
	ret = pp_check(pp_handle);
277 278 279
	if (ret == 0)
		hwmgr_hw_suspend(pp_handle);
	return 0;
280 281 282 283
}

static int pp_resume(void *handle)
{
284
	struct pp_hwmgr  *hwmgr;
285
	int ret;
286
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
287

288
	ret = pp_check(pp_handle);
289

290 291
	if (ret < 0)
		return ret;
292

293
	hwmgr = pp_handle->hwmgr;
294

295
	if (hwmgr->smumgr_funcs->start_smu == NULL)
296 297
		return -EINVAL;

298
	if (hwmgr->smumgr_funcs->start_smu(pp_handle->hwmgr)) {
299
		pr_err("smc start failed\n");
300
		hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
301
		return -EINVAL;
302 303
	}

304
	if (ret == PP_DPM_DISABLED)
M
Monk Liu 已提交
305
		return 0;
306

307
	return hwmgr_hw_resume(pp_handle);
308 309 310
}

const struct amd_ip_funcs pp_ip_funcs = {
311
	.name = "powerplay",
312
	.early_init = pp_early_init,
R
Rex Zhu 已提交
313
	.late_init = pp_late_init,
314 315 316 317
	.sw_init = pp_sw_init,
	.sw_fini = pp_sw_fini,
	.hw_init = pp_hw_init,
	.hw_fini = pp_hw_fini,
318
	.late_fini = pp_late_fini,
319 320 321 322 323
	.suspend = pp_suspend,
	.resume = pp_resume,
	.is_idle = pp_is_idle,
	.wait_for_idle = pp_wait_for_idle,
	.soft_reset = pp_sw_reset,
324
	.set_clockgating_state = NULL,
325 326 327 328 329 330 331 332 333 334 335 336 337
	.set_powergating_state = pp_set_powergating_state,
};

static int pp_dpm_load_fw(void *handle)
{
	return 0;
}

static int pp_dpm_fw_loading_complete(void *handle)
{
	return 0;
}

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
static void pp_dpm_en_umd_pstate(struct pp_hwmgr  *hwmgr,
						enum amd_dpm_forced_level *level)
{
	uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
					AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
					AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
					AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;

	if (!(hwmgr->dpm_level & profile_mode_mask)) {
		/* enter umd pstate, save current level, disable gfx cg*/
		if (*level & profile_mode_mask) {
			hwmgr->saved_dpm_level = hwmgr->dpm_level;
			hwmgr->en_umd_pstate = true;
			cgs_set_clockgating_state(hwmgr->device,
						AMD_IP_BLOCK_TYPE_GFX,
						AMD_CG_STATE_UNGATE);
			cgs_set_powergating_state(hwmgr->device,
					AMD_IP_BLOCK_TYPE_GFX,
					AMD_PG_STATE_UNGATE);
		}
	} else {
		/* exit umd pstate, restore level, enable gfx cg*/
		if (!(*level & profile_mode_mask)) {
			if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
				*level = hwmgr->saved_dpm_level;
			hwmgr->en_umd_pstate = false;
			cgs_set_clockgating_state(hwmgr->device,
					AMD_IP_BLOCK_TYPE_GFX,
					AMD_CG_STATE_GATE);
			cgs_set_powergating_state(hwmgr->device,
					AMD_IP_BLOCK_TYPE_GFX,
					AMD_PG_STATE_GATE);
		}
	}
}

374 375 376
static int pp_dpm_force_performance_level(void *handle,
					enum amd_dpm_forced_level level)
{
377
	struct pp_hwmgr  *hwmgr;
378 379
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
380

381
	ret = pp_check(pp_handle);
382

383
	if (ret)
384
		return ret;
385 386 387

	hwmgr = pp_handle->hwmgr;

388 389 390
	if (level == hwmgr->dpm_level)
		return 0;

391
	if (hwmgr->hwmgr_func->force_dpm_level == NULL) {
392
		pr_info("%s was not implemented.\n", __func__);
393 394
		return 0;
	}
395

396
	mutex_lock(&pp_handle->pp_lock);
397 398
	pp_dpm_en_umd_pstate(hwmgr, &level);
	hwmgr->request_dpm_level = level;
399
	hwmgr_handle_task(pp_handle, AMD_PP_TASK_READJUST_POWER_STATE, NULL, NULL);
400 401 402 403
	ret = hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
	if (!ret)
		hwmgr->dpm_level = hwmgr->request_dpm_level;

404
	mutex_unlock(&pp_handle->pp_lock);
405 406
	return 0;
}
407

408 409 410
static enum amd_dpm_forced_level pp_dpm_get_performance_level(
								void *handle)
{
411
	struct pp_hwmgr  *hwmgr;
412 413
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
414
	enum amd_dpm_forced_level level;
415

416
	ret = pp_check(pp_handle);
417

418
	if (ret)
419
		return ret;
420

421
	hwmgr = pp_handle->hwmgr;
422 423 424 425
	mutex_lock(&pp_handle->pp_lock);
	level = hwmgr->dpm_level;
	mutex_unlock(&pp_handle->pp_lock);
	return level;
426
}
427

428
static uint32_t pp_dpm_get_sclk(void *handle, bool low)
429
{
430
	struct pp_hwmgr  *hwmgr;
431 432
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
433
	uint32_t clk = 0;
434

435
	ret = pp_check(pp_handle);
436

437
	if (ret)
438
		return ret;
439

440
	hwmgr = pp_handle->hwmgr;
441 442

	if (hwmgr->hwmgr_func->get_sclk == NULL) {
443
		pr_info("%s was not implemented.\n", __func__);
444 445
		return 0;
	}
446
	mutex_lock(&pp_handle->pp_lock);
447
	clk = hwmgr->hwmgr_func->get_sclk(hwmgr, low);
448
	mutex_unlock(&pp_handle->pp_lock);
449
	return clk;
450
}
451

452
static uint32_t pp_dpm_get_mclk(void *handle, bool low)
453
{
454
	struct pp_hwmgr  *hwmgr;
455 456
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
457
	uint32_t clk = 0;
458

459
	ret = pp_check(pp_handle);
460

461
	if (ret)
462
		return ret;
463

464
	hwmgr = pp_handle->hwmgr;
465 466

	if (hwmgr->hwmgr_func->get_mclk == NULL) {
467
		pr_info("%s was not implemented.\n", __func__);
468 469
		return 0;
	}
470
	mutex_lock(&pp_handle->pp_lock);
471
	clk = hwmgr->hwmgr_func->get_mclk(hwmgr, low);
472
	mutex_unlock(&pp_handle->pp_lock);
473
	return clk;
474
}
475

476
static void pp_dpm_powergate_vce(void *handle, bool gate)
477
{
478
	struct pp_hwmgr  *hwmgr;
479 480
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
481

482
	ret = pp_check(pp_handle);
483

484
	if (ret)
485
		return;
486

487
	hwmgr = pp_handle->hwmgr;
488 489

	if (hwmgr->hwmgr_func->powergate_vce == NULL) {
490
		pr_info("%s was not implemented.\n", __func__);
491
		return;
492
	}
493
	mutex_lock(&pp_handle->pp_lock);
494
	hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
495
	mutex_unlock(&pp_handle->pp_lock);
496
}
497

498
static void pp_dpm_powergate_uvd(void *handle, bool gate)
499
{
500
	struct pp_hwmgr  *hwmgr;
501 502
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
503

504
	ret = pp_check(pp_handle);
505

506
	if (ret)
507
		return;
508

509
	hwmgr = pp_handle->hwmgr;
510 511

	if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
512
		pr_info("%s was not implemented.\n", __func__);
513
		return;
514
	}
515
	mutex_lock(&pp_handle->pp_lock);
516
	hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
517
	mutex_unlock(&pp_handle->pp_lock);
518 519
}

520
static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
521
		void *input, void *output)
522
{
523
	int ret = 0;
524
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
525

526
	ret = pp_check(pp_handle);
527

528
	if (ret)
529
		return ret;
530

531 532
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr_handle_task(pp_handle, task_id, input, output);
533
	mutex_unlock(&pp_handle->pp_lock);
534

535
	return ret;
536
}
537

538
static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
539
{
540 541
	struct pp_hwmgr *hwmgr;
	struct pp_power_state *state;
542 543
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
544
	enum amd_pm_state_type pm_type;
545

546
	ret = pp_check(pp_handle);
547

548
	if (ret)
549
		return ret;
550

551 552 553
	hwmgr = pp_handle->hwmgr;

	if (hwmgr->current_ps == NULL)
554 555
		return -EINVAL;

556 557
	mutex_lock(&pp_handle->pp_lock);

558 559 560 561
	state = hwmgr->current_ps;

	switch (state->classification.ui_label) {
	case PP_StateUILabel_Battery:
562
		pm_type = POWER_STATE_TYPE_BATTERY;
563
		break;
564
	case PP_StateUILabel_Balanced:
565
		pm_type = POWER_STATE_TYPE_BALANCED;
566
		break;
567
	case PP_StateUILabel_Performance:
568
		pm_type = POWER_STATE_TYPE_PERFORMANCE;
569
		break;
570
	default:
571
		if (state->classification.flags & PP_StateClassificationFlag_Boot)
572
			pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
573
		else
574
			pm_type = POWER_STATE_TYPE_DEFAULT;
575
		break;
576
	}
577 578 579
	mutex_unlock(&pp_handle->pp_lock);

	return pm_type;
580
}
581

582
static void pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
583 584
{
	struct pp_hwmgr  *hwmgr;
585 586
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
587

588
	ret = pp_check(pp_handle);
589

590
	if (ret)
591
		return;
592

593
	hwmgr = pp_handle->hwmgr;
594 595

	if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
596
		pr_info("%s was not implemented.\n", __func__);
597
		return;
598
	}
599
	mutex_lock(&pp_handle->pp_lock);
600
	hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
601
	mutex_unlock(&pp_handle->pp_lock);
602 603
}

604
static uint32_t pp_dpm_get_fan_control_mode(void *handle)
605 606
{
	struct pp_hwmgr  *hwmgr;
607 608
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
609
	uint32_t mode = 0;
610

611
	ret = pp_check(pp_handle);
612

613
	if (ret)
614
		return ret;
615

616
	hwmgr = pp_handle->hwmgr;
617 618

	if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
619
		pr_info("%s was not implemented.\n", __func__);
620 621
		return 0;
	}
622
	mutex_lock(&pp_handle->pp_lock);
623
	mode = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
624
	mutex_unlock(&pp_handle->pp_lock);
625
	return mode;
626 627 628 629 630
}

static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
{
	struct pp_hwmgr  *hwmgr;
631 632
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
633

634
	ret = pp_check(pp_handle);
635

636
	if (ret)
637
		return ret;
638

639
	hwmgr = pp_handle->hwmgr;
640 641

	if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
642
		pr_info("%s was not implemented.\n", __func__);
643 644
		return 0;
	}
645 646 647 648
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
649 650 651 652 653
}

static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
{
	struct pp_hwmgr  *hwmgr;
654 655
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
656

657
	ret = pp_check(pp_handle);
658

659
	if (ret)
660
		return ret;
661

662
	hwmgr = pp_handle->hwmgr;
663 664

	if (hwmgr->hwmgr_func->get_fan_speed_percent == NULL) {
665
		pr_info("%s was not implemented.\n", __func__);
666 667
		return 0;
	}
668

669 670 671 672
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
673 674
}

675 676 677
static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
{
	struct pp_hwmgr *hwmgr;
678 679
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
680

681
	ret = pp_check(pp_handle);
682

683
	if (ret)
684
		return ret;
685

686
	hwmgr = pp_handle->hwmgr;
687 688 689 690

	if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
		return -EINVAL;

691 692 693 694
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
695 696
}

697 698 699
static int pp_dpm_get_temperature(void *handle)
{
	struct pp_hwmgr  *hwmgr;
700 701
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
702

703
	ret = pp_check(pp_handle);
704

705
	if (ret)
706
		return ret;
707

708
	hwmgr = pp_handle->hwmgr;
709 710

	if (hwmgr->hwmgr_func->get_temperature == NULL) {
711
		pr_info("%s was not implemented.\n", __func__);
712 713
		return 0;
	}
714 715 716 717
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_temperature(hwmgr);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
718
}
719

720 721 722 723 724
static int pp_dpm_get_pp_num_states(void *handle,
		struct pp_states_info *data)
{
	struct pp_hwmgr *hwmgr;
	int i;
725 726
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
727

728
	ret = pp_check(pp_handle);
729

730
	if (ret)
731 732 733
		return ret;

	hwmgr = pp_handle->hwmgr;
734

735
	if (hwmgr->ps == NULL)
736 737
		return -EINVAL;

738 739
	mutex_lock(&pp_handle->pp_lock);

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
	data->nums = hwmgr->num_ps;

	for (i = 0; i < hwmgr->num_ps; i++) {
		struct pp_power_state *state = (struct pp_power_state *)
				((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
		switch (state->classification.ui_label) {
		case PP_StateUILabel_Battery:
			data->states[i] = POWER_STATE_TYPE_BATTERY;
			break;
		case PP_StateUILabel_Balanced:
			data->states[i] = POWER_STATE_TYPE_BALANCED;
			break;
		case PP_StateUILabel_Performance:
			data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
			break;
		default:
			if (state->classification.flags & PP_StateClassificationFlag_Boot)
				data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
			else
				data->states[i] = POWER_STATE_TYPE_DEFAULT;
		}
	}
762
	mutex_unlock(&pp_handle->pp_lock);
763 764 765 766 767 768
	return 0;
}

static int pp_dpm_get_pp_table(void *handle, char **table)
{
	struct pp_hwmgr *hwmgr;
769 770
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
771
	int size = 0;
772

773
	ret = pp_check(pp_handle);
774

775
	if (ret)
776
		return ret;
777

778
	hwmgr = pp_handle->hwmgr;
779

780 781 782
	if (!hwmgr->soft_pp_table)
		return -EINVAL;

783
	mutex_lock(&pp_handle->pp_lock);
784
	*table = (char *)hwmgr->soft_pp_table;
785 786 787
	size = hwmgr->soft_pp_table_size;
	mutex_unlock(&pp_handle->pp_lock);
	return size;
788 789 790 791 792
}

static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
{
	struct pp_hwmgr *hwmgr;
793 794
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
795

796
	ret = pp_check(pp_handle);
797

798
	if (ret)
799
		return ret;
800

801
	hwmgr = pp_handle->hwmgr;
802
	mutex_lock(&pp_handle->pp_lock);
803
	if (!hwmgr->hardcode_pp_table) {
804 805 806
		hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
						   hwmgr->soft_pp_table_size,
						   GFP_KERNEL);
807 808
		if (!hwmgr->hardcode_pp_table) {
			mutex_unlock(&pp_handle->pp_lock);
809
			return -ENOMEM;
810
		}
811
	}
812

813 814 815
	memcpy(hwmgr->hardcode_pp_table, buf, size);

	hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
816
	mutex_unlock(&pp_handle->pp_lock);
817

818 819 820 821 822 823 824 825 826 827 828
	ret = amd_powerplay_reset(handle);
	if (ret)
		return ret;

	if (hwmgr->hwmgr_func->avfs_control) {
		ret = hwmgr->hwmgr_func->avfs_control(hwmgr, false);
		if (ret)
			return ret;
	}

	return 0;
829 830 831
}

static int pp_dpm_force_clock_level(void *handle,
832
		enum pp_clock_type type, uint32_t mask)
833 834
{
	struct pp_hwmgr *hwmgr;
835 836
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
837

838
	ret = pp_check(pp_handle);
839

840
	if (ret)
841
		return ret;
842

843
	hwmgr = pp_handle->hwmgr;
844 845

	if (hwmgr->hwmgr_func->force_clock_level == NULL) {
846
		pr_info("%s was not implemented.\n", __func__);
847 848
		return 0;
	}
849 850 851 852
	mutex_lock(&pp_handle->pp_lock);
	hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
853 854 855 856 857 858
}

static int pp_dpm_print_clock_levels(void *handle,
		enum pp_clock_type type, char *buf)
{
	struct pp_hwmgr *hwmgr;
859 860
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
861

862
	ret = pp_check(pp_handle);
863

864
	if (ret)
865
		return ret;
866

867
	hwmgr = pp_handle->hwmgr;
868

869
	if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
870
		pr_info("%s was not implemented.\n", __func__);
871 872
		return 0;
	}
873 874 875 876
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
877 878
}

879 880 881
static int pp_dpm_get_sclk_od(void *handle)
{
	struct pp_hwmgr *hwmgr;
882 883
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
884

885
	ret = pp_check(pp_handle);
886

887
	if (ret)
888
		return ret;
889

890
	hwmgr = pp_handle->hwmgr;
891 892

	if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
893
		pr_info("%s was not implemented.\n", __func__);
894 895
		return 0;
	}
896 897 898 899
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_sclk_od(hwmgr);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
900 901 902 903 904
}

static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
{
	struct pp_hwmgr *hwmgr;
905 906
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
907

908
	ret = pp_check(pp_handle);
909

910
	if (ret)
911
		return ret;
912

913
	hwmgr = pp_handle->hwmgr;
914 915

	if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
916
		pr_info("%s was not implemented.\n", __func__);
917 918 919
		return 0;
	}

920 921
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
922
	mutex_unlock(&pp_handle->pp_lock);
923
	return ret;
924 925
}

926 927 928
static int pp_dpm_get_mclk_od(void *handle)
{
	struct pp_hwmgr *hwmgr;
929 930
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
931

932
	ret = pp_check(pp_handle);
933

934
	if (ret)
935
		return ret;
936

937
	hwmgr = pp_handle->hwmgr;
938 939

	if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
940
		pr_info("%s was not implemented.\n", __func__);
941 942
		return 0;
	}
943 944 945 946
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_mclk_od(hwmgr);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
947 948 949 950 951
}

static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
{
	struct pp_hwmgr *hwmgr;
952 953
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
954

955
	ret = pp_check(pp_handle);
956

957
	if (ret)
958
		return ret;
959

960
	hwmgr = pp_handle->hwmgr;
961 962

	if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
963
		pr_info("%s was not implemented.\n", __func__);
964 965
		return 0;
	}
966 967 968 969
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
970 971
}

972 973
static int pp_dpm_read_sensor(void *handle, int idx,
			      void *value, int *size)
974 975
{
	struct pp_hwmgr *hwmgr;
976 977
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
978

979
	ret = pp_check(pp_handle);
980

981
	if (ret)
982
		return ret;
983

984
	hwmgr = pp_handle->hwmgr;
985 986

	if (hwmgr->hwmgr_func->read_sensor == NULL) {
987
		pr_info("%s was not implemented.\n", __func__);
988 989 990
		return 0;
	}

991 992 993 994 995
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
	mutex_unlock(&pp_handle->pp_lock);

	return ret;
996 997
}

998 999 1000 1001
static struct amd_vce_state*
pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
{
	struct pp_hwmgr *hwmgr;
1002 1003
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1004

1005
	ret = pp_check(pp_handle);
1006

1007
	if (ret)
1008 1009 1010 1011 1012 1013
		return NULL;

	hwmgr = pp_handle->hwmgr;

	if (hwmgr && idx < hwmgr->num_vce_state_tables)
		return &hwmgr->vce_states[idx];
1014 1015 1016
	return NULL;
}

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 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
static int pp_dpm_reset_power_profile_state(void *handle,
		struct amd_pp_profile *request)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;

	if (!request || pp_check(pp_handle))
		return -EINVAL;

	hwmgr = pp_handle->hwmgr;

	if (hwmgr->hwmgr_func->set_power_profile_state == NULL) {
		pr_info("%s was not implemented.\n", __func__);
		return 0;
	}

	if (request->type == AMD_PP_GFX_PROFILE) {
		hwmgr->gfx_power_profile = hwmgr->default_gfx_power_profile;
		return hwmgr->hwmgr_func->set_power_profile_state(hwmgr,
				&hwmgr->gfx_power_profile);
	} else if (request->type == AMD_PP_COMPUTE_PROFILE) {
		hwmgr->compute_power_profile =
				hwmgr->default_compute_power_profile;
		return hwmgr->hwmgr_func->set_power_profile_state(hwmgr,
				&hwmgr->compute_power_profile);
	} else
		return -EINVAL;
}

static int pp_dpm_get_power_profile_state(void *handle,
		struct amd_pp_profile *query)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;

	if (!query || pp_check(pp_handle))
		return -EINVAL;

	hwmgr = pp_handle->hwmgr;

	if (query->type == AMD_PP_GFX_PROFILE)
		memcpy(query, &hwmgr->gfx_power_profile,
				sizeof(struct amd_pp_profile));
	else if (query->type == AMD_PP_COMPUTE_PROFILE)
		memcpy(query, &hwmgr->compute_power_profile,
				sizeof(struct amd_pp_profile));
	else
		return -EINVAL;

	return 0;
}

static int pp_dpm_set_power_profile_state(void *handle,
		struct amd_pp_profile *request)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = -1;

	if (!request || pp_check(pp_handle))
		return -EINVAL;

	hwmgr = pp_handle->hwmgr;

	if (hwmgr->hwmgr_func->set_power_profile_state == NULL) {
		pr_info("%s was not implemented.\n", __func__);
		return 0;
	}

	if (request->min_sclk ||
		request->min_mclk ||
		request->activity_threshold ||
		request->up_hyst ||
		request->down_hyst) {
		if (request->type == AMD_PP_GFX_PROFILE)
			memcpy(&hwmgr->gfx_power_profile, request,
					sizeof(struct amd_pp_profile));
		else if (request->type == AMD_PP_COMPUTE_PROFILE)
			memcpy(&hwmgr->compute_power_profile, request,
					sizeof(struct amd_pp_profile));
		else
			return -EINVAL;

		if (request->type == hwmgr->current_power_profile)
			ret = hwmgr->hwmgr_func->set_power_profile_state(
					hwmgr,
					request);
	} else {
		/* set power profile if it exists */
		switch (request->type) {
		case AMD_PP_GFX_PROFILE:
			ret = hwmgr->hwmgr_func->set_power_profile_state(
					hwmgr,
					&hwmgr->gfx_power_profile);
			break;
		case AMD_PP_COMPUTE_PROFILE:
			ret = hwmgr->hwmgr_func->set_power_profile_state(
					hwmgr,
					&hwmgr->compute_power_profile);
			break;
		default:
			return -EINVAL;
		}
	}

	if (!ret)
		hwmgr->current_power_profile = request->type;

	return 0;
}

static int pp_dpm_switch_power_profile(void *handle,
		enum amd_pp_profile_type type)
{
	struct pp_hwmgr *hwmgr;
	struct amd_pp_profile request = {0};
	struct pp_instance *pp_handle = (struct pp_instance *)handle;

	if (pp_check(pp_handle))
		return -EINVAL;

	hwmgr = pp_handle->hwmgr;

	if (hwmgr->current_power_profile != type) {
		request.type = type;
		pp_dpm_set_power_profile_state(handle, &request);
	}

	return 0;
}

1148
const struct amd_pm_funcs pp_dpm_funcs = {
1149
	.get_temperature = pp_dpm_get_temperature,
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	.load_firmware = pp_dpm_load_fw,
	.wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
	.force_performance_level = pp_dpm_force_performance_level,
	.get_performance_level = pp_dpm_get_performance_level,
	.get_current_power_state = pp_dpm_get_current_power_state,
	.get_sclk = pp_dpm_get_sclk,
	.get_mclk = pp_dpm_get_mclk,
	.powergate_vce = pp_dpm_powergate_vce,
	.powergate_uvd = pp_dpm_powergate_uvd,
	.dispatch_tasks = pp_dpm_dispatch_tasks,
1160 1161 1162 1163
	.set_fan_control_mode = pp_dpm_set_fan_control_mode,
	.get_fan_control_mode = pp_dpm_get_fan_control_mode,
	.set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
	.get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
1164
	.get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
1165 1166 1167 1168 1169
	.get_pp_num_states = pp_dpm_get_pp_num_states,
	.get_pp_table = pp_dpm_get_pp_table,
	.set_pp_table = pp_dpm_set_pp_table,
	.force_clock_level = pp_dpm_force_clock_level,
	.print_clock_levels = pp_dpm_print_clock_levels,
1170 1171
	.get_sclk_od = pp_dpm_get_sclk_od,
	.set_sclk_od = pp_dpm_set_sclk_od,
1172 1173
	.get_mclk_od = pp_dpm_get_mclk_od,
	.set_mclk_od = pp_dpm_set_mclk_od,
1174
	.read_sensor = pp_dpm_read_sensor,
1175
	.get_vce_clock_state = pp_dpm_get_vce_clock_state,
1176 1177 1178 1179
	.reset_power_profile_state = pp_dpm_reset_power_profile_state,
	.get_power_profile_state = pp_dpm_get_power_profile_state,
	.set_power_profile_state = pp_dpm_set_power_profile_state,
	.switch_power_profile = pp_dpm_switch_power_profile,
1180 1181
};

1182 1183 1184 1185 1186
int amd_powerplay_reset(void *handle)
{
	struct pp_instance *instance = (struct pp_instance *)handle;
	int ret;

1187
	ret = pp_check(instance);
1188
	if (!ret)
1189 1190
		return ret;

1191
	ret = pp_hw_fini(instance);
1192 1193 1194
	if (ret)
		return ret;

1195 1196
	ret = hwmgr_hw_init(instance);
	if (ret)
1197
		return ret;
1198

1199
	return hwmgr_handle_task(instance, AMD_PP_TASK_COMPLETE_INIT, NULL, NULL);
1200 1201
}

1202 1203
/* export this function to DAL */

1204 1205
int amd_powerplay_display_configuration_change(void *handle,
	const struct amd_pp_display_configuration *display_config)
1206 1207
{
	struct pp_hwmgr  *hwmgr;
1208 1209
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1210

1211
	ret = pp_check(pp_handle);
1212

1213
	if (ret)
1214
		return ret;
1215

1216
	hwmgr = pp_handle->hwmgr;
1217
	mutex_lock(&pp_handle->pp_lock);
1218
	phm_store_dal_configuration_data(hwmgr, display_config);
1219
	mutex_unlock(&pp_handle->pp_lock);
1220 1221
	return 0;
}
1222

1223
int amd_powerplay_get_display_power_level(void *handle,
R
Rex Zhu 已提交
1224
		struct amd_pp_simple_clock_info *output)
1225 1226
{
	struct pp_hwmgr  *hwmgr;
1227 1228
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1229

1230
	ret = pp_check(pp_handle);
1231

1232
	if (ret)
1233
		return ret;
1234

1235
	hwmgr = pp_handle->hwmgr;
1236

1237 1238
	if (output == NULL)
		return -EINVAL;
1239

1240 1241 1242 1243
	mutex_lock(&pp_handle->pp_lock);
	ret = phm_get_dal_power_level(hwmgr, output);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
1244
}
1245 1246

int amd_powerplay_get_current_clocks(void *handle,
1247
		struct amd_pp_clock_info *clocks)
1248 1249 1250
{
	struct amd_pp_simple_clock_info simple_clocks;
	struct pp_clock_info hw_clocks;
1251 1252 1253
	struct pp_hwmgr  *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1254

1255
	ret = pp_check(pp_handle);
1256

1257
	if (ret)
1258
		return ret;
1259

1260
	hwmgr = pp_handle->hwmgr;
1261

1262 1263
	mutex_lock(&pp_handle->pp_lock);

1264 1265
	phm_get_dal_power_level(hwmgr, &simple_clocks);

1266 1267 1268 1269 1270 1271 1272 1273
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
					PHM_PlatformCaps_PowerContainment))
		ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
					&hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment);
	else
		ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
					&hw_clocks, PHM_PerformanceLevelDesignation_Activity);

1274
	if (ret) {
1275 1276 1277
		pr_info("Error in phm_get_clock_info \n");
		mutex_unlock(&pp_handle->pp_lock);
		return -EINVAL;
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	}

	clocks->min_engine_clock = hw_clocks.min_eng_clk;
	clocks->max_engine_clock = hw_clocks.max_eng_clk;
	clocks->min_memory_clock = hw_clocks.min_mem_clk;
	clocks->max_memory_clock = hw_clocks.max_mem_clk;
	clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
	clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;

	clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
	clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;

	clocks->max_clocks_state = simple_clocks.level;

	if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
		clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
		clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
	}
1296
	mutex_unlock(&pp_handle->pp_lock);
1297 1298 1299 1300 1301
	return 0;
}

int amd_powerplay_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
{
1302 1303 1304
	struct pp_hwmgr  *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1305

1306
	ret = pp_check(pp_handle);
1307

1308
	if (ret)
1309 1310 1311
		return ret;

	hwmgr = pp_handle->hwmgr;
1312 1313

	if (clocks == NULL)
1314 1315
		return -EINVAL;

1316 1317 1318 1319
	mutex_lock(&pp_handle->pp_lock);
	ret = phm_get_clock_by_type(hwmgr, type, clocks);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
1320 1321
}

1322 1323 1324 1325 1326 1327 1328 1329 1330
int amd_powerplay_get_clock_by_type_with_latency(void *handle,
		enum amd_pp_clock_type type,
		struct pp_clock_levels_with_latency *clocks)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;

	ret = pp_check(pp_handle);
1331
	if (ret)
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
		return ret;

	if (!clocks)
		return -EINVAL;

	mutex_lock(&pp_handle->pp_lock);
	hwmgr = ((struct pp_instance *)handle)->hwmgr;
	ret = phm_get_clock_by_type_with_latency(hwmgr, type, clocks);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
}

int amd_powerplay_get_clock_by_type_with_voltage(void *handle,
		enum amd_pp_clock_type type,
		struct pp_clock_levels_with_voltage *clocks)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;

	ret = pp_check(pp_handle);
1353
	if (ret)
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
		return ret;

	if (!clocks)
		return -EINVAL;

	hwmgr = ((struct pp_instance *)handle)->hwmgr;

	mutex_lock(&pp_handle->pp_lock);

	ret = phm_get_clock_by_type_with_voltage(hwmgr, type, clocks);

	mutex_unlock(&pp_handle->pp_lock);
	return ret;
}

int amd_powerplay_set_watermarks_for_clocks_ranges(void *handle,
		struct pp_wm_sets_with_clock_ranges_soc15 *wm_with_clock_ranges)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;

	ret = pp_check(pp_handle);
1377
	if (ret)
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
		return ret;

	if (!wm_with_clock_ranges)
		return -EINVAL;

	hwmgr = ((struct pp_instance *)handle)->hwmgr;

	mutex_lock(&pp_handle->pp_lock);
	ret = phm_set_watermarks_for_clocks_ranges(hwmgr,
			wm_with_clock_ranges);
	mutex_unlock(&pp_handle->pp_lock);

	return ret;
}

int amd_powerplay_display_clock_voltage_request(void *handle,
		struct pp_display_clock_request *clock)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;

	ret = pp_check(pp_handle);
1401
	if (ret)
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
		return ret;

	if (!clock)
		return -EINVAL;

	hwmgr = ((struct pp_instance *)handle)->hwmgr;

	mutex_lock(&pp_handle->pp_lock);
	ret = phm_display_clock_voltage_request(hwmgr, clock);
	mutex_unlock(&pp_handle->pp_lock);

	return ret;
}

1416 1417
int amd_powerplay_get_display_mode_validation_clocks(void *handle,
		struct amd_pp_simple_clock_info *clocks)
1418 1419
{
	struct pp_hwmgr  *hwmgr;
1420 1421
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1422

1423
	ret = pp_check(pp_handle);
1424

1425
	if (ret)
1426 1427 1428
		return ret;

	hwmgr = pp_handle->hwmgr;
1429

1430 1431
	if (clocks == NULL)
		return -EINVAL;
1432

1433 1434
	mutex_lock(&pp_handle->pp_lock);

1435
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
1436
		ret = phm_get_max_high_clocks(hwmgr, clocks);
1437

1438
	mutex_unlock(&pp_handle->pp_lock);
1439
	return ret;
1440 1441
}