amd_powerplay.c 32.9 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

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

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

41
	if (handle->hwmgr == NULL || handle->hwmgr->smumgr_funcs == NULL)
42 43
		return -EINVAL;

44 45
	if (handle->pm_en == 0)
		return PP_DPM_DISABLED;
46

47
	if (handle->hwmgr->hwmgr_func == NULL)
48
		return PP_DPM_DISABLED;
49

50 51
	return 0;
}
52

53 54 55 56
static int pp_early_init(void *handle)
{
	int ret;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
57

58
	ret = hwmgr_early_init(pp_handle);
59
	if (ret)
60
		return -EINVAL;
61 62 63 64

	if ((pp_handle->pm_en == 0)
		|| cgs_is_virtualization_enabled(pp_handle->device))
		return PP_DPM_DISABLED;
65

66
	return 0;
67 68
}

69
static int pp_sw_init(void *handle)
70
{
71
	struct pp_hwmgr *hwmgr;
72
	int ret = 0;
73
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
74

75
	ret = pp_check(pp_handle);
76

77
	if (ret == 0 || ret == PP_DPM_DISABLED) {
78
		hwmgr = pp_handle->hwmgr;
79

80
		if (hwmgr->smumgr_funcs->smu_init == NULL)
81
			return -EINVAL;
82

83
		ret = hwmgr->smumgr_funcs->smu_init(hwmgr);
84

85 86 87 88
		pr_info("amdgpu: powerplay sw initialized\n");
	}
	return ret;
}
89

90 91
static int pp_sw_fini(void *handle)
{
92
	struct pp_hwmgr *hwmgr;
93 94 95 96 97
	int ret = 0;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;

	ret = pp_check(pp_handle);
	if (ret == 0 || ret == PP_DPM_DISABLED) {
98
		hwmgr = pp_handle->hwmgr;
99

100
		if (hwmgr->smumgr_funcs->smu_fini == NULL)
101 102
			return -EINVAL;

103
		ret = hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
104
	}
105
	return ret;
106 107 108 109
}

static int pp_hw_init(void *handle)
{
110
	int ret = 0;
111
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
112
	struct pp_hwmgr *hwmgr;
113

114
	ret = pp_check(pp_handle);
115

116
	if (ret == 0 || ret == PP_DPM_DISABLED) {
117
		hwmgr = pp_handle->hwmgr;
118

119
		if (hwmgr->smumgr_funcs->start_smu == NULL)
120
			return -EINVAL;
121

122
		if(hwmgr->smumgr_funcs->start_smu(pp_handle->hwmgr)) {
123
			pr_err("smc start failed\n");
124
			hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
125 126 127 128
			return -EINVAL;;
		}
		if (ret == PP_DPM_DISABLED)
			return PP_DPM_DISABLED;
129
	}
130

131 132 133
	ret = hwmgr_hw_init(pp_handle);
	if (ret)
		goto err;
134
	return 0;
135 136 137
err:
	pp_handle->pm_en = 0;
	return PP_DPM_DISABLED;
138 139 140 141
}

static int pp_hw_fini(void *handle)
{
142 143
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
144

145
	ret = pp_check(pp_handle);
146
	if (ret == 0)
147
		hwmgr_hw_fini(pp_handle);
148

149 150 151
	return 0;
}

R
Rex Zhu 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164
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;
}

165 166
static bool pp_is_idle(void *handle)
{
167
	return false;
168 169 170 171 172 173 174 175 176 177 178 179 180
}

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

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


181
int amd_set_clockgating_by_smu(void *handle, uint32_t msg_id)
182
{
183
	struct pp_hwmgr  *hwmgr;
184 185
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
186

187
	ret = pp_check(pp_handle);
188

189 190
	if (ret != 0)
		return ret;
191

192
	hwmgr = pp_handle->hwmgr;
193

194
	if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
195
		pr_info("%s was not implemented.\n", __func__);
196
		return 0;
197
	}
198

199
	return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
200 201 202 203 204
}

static int pp_set_powergating_state(void *handle,
				    enum amd_powergating_state state)
{
205
	struct pp_hwmgr  *hwmgr;
206 207
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
208

209
	ret = pp_check(pp_handle);
210

211 212
	if (ret != 0)
		return ret;
213

214
	hwmgr = pp_handle->hwmgr;
215 216

	if (hwmgr->hwmgr_func->enable_per_cu_power_gating == NULL) {
217
		pr_info("%s was not implemented.\n", __func__);
218 219
		return 0;
	}
220 221 222

	/* Enable/disable GFX per cu powergating through SMU */
	return hwmgr->hwmgr_func->enable_per_cu_power_gating(hwmgr,
223
			state == AMD_PG_STATE_GATE);
224 225 226 227
}

static int pp_suspend(void *handle)
{
228 229
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
230

231 232
	ret = pp_check(pp_handle);

233 234 235
	if (ret == PP_DPM_DISABLED)
		return 0;
	else if (ret != 0)
236
		return ret;
237

238
	return hwmgr_hw_suspend(pp_handle);
239 240 241 242
}

static int pp_resume(void *handle)
{
243
	struct pp_hwmgr  *hwmgr;
244 245
	int ret, ret1;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
246

247 248 249 250
	ret1 = pp_check(pp_handle);

	if (ret1 != 0 && ret1 != PP_DPM_DISABLED)
		return ret1;
251

252
	hwmgr = pp_handle->hwmgr;
253

254
	if (hwmgr->smumgr_funcs->start_smu == NULL)
255 256
		return -EINVAL;

257
	ret = hwmgr->smumgr_funcs->start_smu(pp_handle->hwmgr);
258
	if (ret) {
259
		pr_err("smc start failed\n");
260
		hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
261 262 263
		return ret;
	}

264
	if (ret1 == PP_DPM_DISABLED)
M
Monk Liu 已提交
265
		return 0;
266

267
	return hwmgr_hw_resume(pp_handle);
268 269 270
}

const struct amd_ip_funcs pp_ip_funcs = {
271
	.name = "powerplay",
272
	.early_init = pp_early_init,
R
Rex Zhu 已提交
273
	.late_init = pp_late_init,
274 275 276 277 278 279 280 281 282
	.sw_init = pp_sw_init,
	.sw_fini = pp_sw_fini,
	.hw_init = pp_hw_init,
	.hw_fini = pp_hw_fini,
	.suspend = pp_suspend,
	.resume = pp_resume,
	.is_idle = pp_is_idle,
	.wait_for_idle = pp_wait_for_idle,
	.soft_reset = pp_sw_reset,
283
	.set_clockgating_state = NULL,
284 285 286 287 288 289 290 291 292 293 294 295 296
	.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;
}

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
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);
		}
	}
}

333 334 335
static int pp_dpm_force_performance_level(void *handle,
					enum amd_dpm_forced_level level)
{
336
	struct pp_hwmgr  *hwmgr;
337 338
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
339

340
	ret = pp_check(pp_handle);
341

342 343
	if (ret != 0)
		return ret;
344 345 346

	hwmgr = pp_handle->hwmgr;

347 348 349
	if (level == hwmgr->dpm_level)
		return 0;

350
	if (hwmgr->hwmgr_func->force_dpm_level == NULL) {
351
		pr_info("%s was not implemented.\n", __func__);
352 353
		return 0;
	}
354

355
	mutex_lock(&pp_handle->pp_lock);
356 357
	pp_dpm_en_umd_pstate(hwmgr, &level);
	hwmgr->request_dpm_level = level;
358
	hwmgr_handle_task(pp_handle, AMD_PP_TASK_READJUST_POWER_STATE, NULL, NULL);
359 360 361 362
	ret = hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
	if (!ret)
		hwmgr->dpm_level = hwmgr->request_dpm_level;

363
	mutex_unlock(&pp_handle->pp_lock);
364 365
	return 0;
}
366

367 368 369
static enum amd_dpm_forced_level pp_dpm_get_performance_level(
								void *handle)
{
370
	struct pp_hwmgr  *hwmgr;
371 372
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
373
	enum amd_dpm_forced_level level;
374

375
	ret = pp_check(pp_handle);
376

377 378
	if (ret != 0)
		return ret;
379

380
	hwmgr = pp_handle->hwmgr;
381 382 383 384
	mutex_lock(&pp_handle->pp_lock);
	level = hwmgr->dpm_level;
	mutex_unlock(&pp_handle->pp_lock);
	return level;
385
}
386

387
static uint32_t pp_dpm_get_sclk(void *handle, bool low)
388
{
389
	struct pp_hwmgr  *hwmgr;
390 391
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
392
	uint32_t clk = 0;
393

394
	ret = pp_check(pp_handle);
395

396 397
	if (ret != 0)
		return ret;
398

399
	hwmgr = pp_handle->hwmgr;
400 401

	if (hwmgr->hwmgr_func->get_sclk == NULL) {
402
		pr_info("%s was not implemented.\n", __func__);
403 404
		return 0;
	}
405
	mutex_lock(&pp_handle->pp_lock);
406
	clk = hwmgr->hwmgr_func->get_sclk(hwmgr, low);
407
	mutex_unlock(&pp_handle->pp_lock);
408
	return clk;
409
}
410

411
static uint32_t pp_dpm_get_mclk(void *handle, bool low)
412
{
413
	struct pp_hwmgr  *hwmgr;
414 415
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
416
	uint32_t clk = 0;
417

418
	ret = pp_check(pp_handle);
419

420 421
	if (ret != 0)
		return ret;
422

423
	hwmgr = pp_handle->hwmgr;
424 425

	if (hwmgr->hwmgr_func->get_mclk == NULL) {
426
		pr_info("%s was not implemented.\n", __func__);
427 428
		return 0;
	}
429
	mutex_lock(&pp_handle->pp_lock);
430
	clk = hwmgr->hwmgr_func->get_mclk(hwmgr, low);
431
	mutex_unlock(&pp_handle->pp_lock);
432
	return clk;
433
}
434

435
static void pp_dpm_powergate_vce(void *handle, bool gate)
436
{
437
	struct pp_hwmgr  *hwmgr;
438 439
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
440

441
	ret = pp_check(pp_handle);
442

443
	if (ret != 0)
444
		return;
445

446
	hwmgr = pp_handle->hwmgr;
447 448

	if (hwmgr->hwmgr_func->powergate_vce == NULL) {
449
		pr_info("%s was not implemented.\n", __func__);
450
		return;
451
	}
452
	mutex_lock(&pp_handle->pp_lock);
453
	hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
454
	mutex_unlock(&pp_handle->pp_lock);
455
}
456

457
static void pp_dpm_powergate_uvd(void *handle, bool gate)
458
{
459
	struct pp_hwmgr  *hwmgr;
460 461
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
462

463
	ret = pp_check(pp_handle);
464

465
	if (ret != 0)
466
		return;
467

468
	hwmgr = pp_handle->hwmgr;
469 470

	if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
471
		pr_info("%s was not implemented.\n", __func__);
472
		return;
473
	}
474
	mutex_lock(&pp_handle->pp_lock);
475
	hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
476
	mutex_unlock(&pp_handle->pp_lock);
477 478
}

479
static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
480
		void *input, void *output)
481
{
482
	int ret = 0;
483
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
484

485
	ret = pp_check(pp_handle);
486

487 488
	if (ret != 0)
		return ret;
489

490 491
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr_handle_task(pp_handle, task_id, input, output);
492
	mutex_unlock(&pp_handle->pp_lock);
493

494
	return ret;
495
}
496

497
static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
498
{
499 500
	struct pp_hwmgr *hwmgr;
	struct pp_power_state *state;
501 502
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
503
	enum amd_pm_state_type pm_type;
504

505
	ret = pp_check(pp_handle);
506

507 508
	if (ret != 0)
		return ret;
509

510 511 512
	hwmgr = pp_handle->hwmgr;

	if (hwmgr->current_ps == NULL)
513 514
		return -EINVAL;

515 516
	mutex_lock(&pp_handle->pp_lock);

517 518 519 520
	state = hwmgr->current_ps;

	switch (state->classification.ui_label) {
	case PP_StateUILabel_Battery:
521
		pm_type = POWER_STATE_TYPE_BATTERY;
522
		break;
523
	case PP_StateUILabel_Balanced:
524
		pm_type = POWER_STATE_TYPE_BALANCED;
525
		break;
526
	case PP_StateUILabel_Performance:
527
		pm_type = POWER_STATE_TYPE_PERFORMANCE;
528
		break;
529
	default:
530
		if (state->classification.flags & PP_StateClassificationFlag_Boot)
531
			pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
532
		else
533
			pm_type = POWER_STATE_TYPE_DEFAULT;
534
		break;
535
	}
536 537 538
	mutex_unlock(&pp_handle->pp_lock);

	return pm_type;
539
}
540

541
static void pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
542 543
{
	struct pp_hwmgr  *hwmgr;
544 545
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
546

547
	ret = pp_check(pp_handle);
548

549
	if (ret != 0)
550
		return;
551

552
	hwmgr = pp_handle->hwmgr;
553 554

	if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
555
		pr_info("%s was not implemented.\n", __func__);
556
		return;
557
	}
558
	mutex_lock(&pp_handle->pp_lock);
559
	hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
560
	mutex_unlock(&pp_handle->pp_lock);
561 562
}

563
static uint32_t pp_dpm_get_fan_control_mode(void *handle)
564 565
{
	struct pp_hwmgr  *hwmgr;
566 567
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
568
	uint32_t mode = 0;
569

570
	ret = pp_check(pp_handle);
571

572 573
	if (ret != 0)
		return ret;
574

575
	hwmgr = pp_handle->hwmgr;
576 577

	if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
578
		pr_info("%s was not implemented.\n", __func__);
579 580
		return 0;
	}
581
	mutex_lock(&pp_handle->pp_lock);
582
	mode = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
583
	mutex_unlock(&pp_handle->pp_lock);
584
	return mode;
585 586 587 588 589
}

static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
{
	struct pp_hwmgr  *hwmgr;
590 591
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
592

593
	ret = pp_check(pp_handle);
594

595 596
	if (ret != 0)
		return ret;
597

598
	hwmgr = pp_handle->hwmgr;
599 600

	if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
601
		pr_info("%s was not implemented.\n", __func__);
602 603
		return 0;
	}
604 605 606 607
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
608 609 610 611 612
}

static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
{
	struct pp_hwmgr  *hwmgr;
613 614
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
615

616
	ret = pp_check(pp_handle);
617

618 619
	if (ret != 0)
		return ret;
620

621
	hwmgr = pp_handle->hwmgr;
622 623

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

628 629 630 631
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
632 633
}

634 635 636
static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
{
	struct pp_hwmgr *hwmgr;
637 638
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
639

640
	ret = pp_check(pp_handle);
641

642 643
	if (ret != 0)
		return ret;
644

645
	hwmgr = pp_handle->hwmgr;
646 647 648 649

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

650 651 652 653
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
654 655
}

656 657 658
static int pp_dpm_get_temperature(void *handle)
{
	struct pp_hwmgr  *hwmgr;
659 660
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
661

662
	ret = pp_check(pp_handle);
663

664 665
	if (ret != 0)
		return ret;
666

667
	hwmgr = pp_handle->hwmgr;
668 669

	if (hwmgr->hwmgr_func->get_temperature == NULL) {
670
		pr_info("%s was not implemented.\n", __func__);
671 672
		return 0;
	}
673 674 675 676
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_temperature(hwmgr);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
677
}
678

679 680 681 682 683
static int pp_dpm_get_pp_num_states(void *handle,
		struct pp_states_info *data)
{
	struct pp_hwmgr *hwmgr;
	int i;
684 685
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
686

687
	ret = pp_check(pp_handle);
688

689 690 691 692
	if (ret != 0)
		return ret;

	hwmgr = pp_handle->hwmgr;
693

694
	if (hwmgr->ps == NULL)
695 696
		return -EINVAL;

697 698
	mutex_lock(&pp_handle->pp_lock);

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	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;
		}
	}
721
	mutex_unlock(&pp_handle->pp_lock);
722 723 724 725 726 727
	return 0;
}

static int pp_dpm_get_pp_table(void *handle, char **table)
{
	struct pp_hwmgr *hwmgr;
728 729
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
730
	int size = 0;
731

732
	ret = pp_check(pp_handle);
733

734 735
	if (ret != 0)
		return ret;
736

737
	hwmgr = pp_handle->hwmgr;
738

739 740 741
	if (!hwmgr->soft_pp_table)
		return -EINVAL;

742
	mutex_lock(&pp_handle->pp_lock);
743
	*table = (char *)hwmgr->soft_pp_table;
744 745 746
	size = hwmgr->soft_pp_table_size;
	mutex_unlock(&pp_handle->pp_lock);
	return size;
747 748 749 750 751
}

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

755
	ret = pp_check(pp_handle);
756

757 758
	if (ret != 0)
		return ret;
759

760
	hwmgr = pp_handle->hwmgr;
761
	mutex_lock(&pp_handle->pp_lock);
762
	if (!hwmgr->hardcode_pp_table) {
763 764 765
		hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
						   hwmgr->soft_pp_table_size,
						   GFP_KERNEL);
766 767
		if (!hwmgr->hardcode_pp_table) {
			mutex_unlock(&pp_handle->pp_lock);
768
			return -ENOMEM;
769
		}
770
	}
771

772 773 774
	memcpy(hwmgr->hardcode_pp_table, buf, size);

	hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
775
	mutex_unlock(&pp_handle->pp_lock);
776

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

static int pp_dpm_force_clock_level(void *handle,
791
		enum pp_clock_type type, uint32_t mask)
792 793
{
	struct pp_hwmgr *hwmgr;
794 795
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
796

797
	ret = pp_check(pp_handle);
798

799 800
	if (ret != 0)
		return ret;
801

802
	hwmgr = pp_handle->hwmgr;
803 804

	if (hwmgr->hwmgr_func->force_clock_level == NULL) {
805
		pr_info("%s was not implemented.\n", __func__);
806 807
		return 0;
	}
808 809 810 811
	mutex_lock(&pp_handle->pp_lock);
	hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
812 813 814 815 816 817
}

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

821
	ret = pp_check(pp_handle);
822

823 824
	if (ret != 0)
		return ret;
825

826
	hwmgr = pp_handle->hwmgr;
827

828
	if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
829
		pr_info("%s was not implemented.\n", __func__);
830 831
		return 0;
	}
832 833 834 835
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
836 837
}

838 839 840
static int pp_dpm_get_sclk_od(void *handle)
{
	struct pp_hwmgr *hwmgr;
841 842
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
843

844
	ret = pp_check(pp_handle);
845

846 847
	if (ret != 0)
		return ret;
848

849
	hwmgr = pp_handle->hwmgr;
850 851

	if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
852
		pr_info("%s was not implemented.\n", __func__);
853 854
		return 0;
	}
855 856 857 858
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_sclk_od(hwmgr);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
859 860 861 862 863
}

static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
{
	struct pp_hwmgr *hwmgr;
864 865
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
866

867
	ret = pp_check(pp_handle);
868

869 870
	if (ret != 0)
		return ret;
871

872
	hwmgr = pp_handle->hwmgr;
873 874

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

879 880
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
881
	mutex_unlock(&pp_handle->pp_lock);
882
	return ret;
883 884
}

885 886 887
static int pp_dpm_get_mclk_od(void *handle)
{
	struct pp_hwmgr *hwmgr;
888 889
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
890

891
	ret = pp_check(pp_handle);
892

893 894
	if (ret != 0)
		return ret;
895

896
	hwmgr = pp_handle->hwmgr;
897 898

	if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
899
		pr_info("%s was not implemented.\n", __func__);
900 901
		return 0;
	}
902 903 904 905
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_mclk_od(hwmgr);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
906 907 908 909 910
}

static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
{
	struct pp_hwmgr *hwmgr;
911 912
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
913

914
	ret = pp_check(pp_handle);
915

916 917
	if (ret != 0)
		return ret;
918

919
	hwmgr = pp_handle->hwmgr;
920 921

	if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
922
		pr_info("%s was not implemented.\n", __func__);
923 924
		return 0;
	}
925 926 927 928
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
929 930
}

931 932
static int pp_dpm_read_sensor(void *handle, int idx,
			      void *value, int *size)
933 934
{
	struct pp_hwmgr *hwmgr;
935 936
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
937

938
	ret = pp_check(pp_handle);
939

940 941
	if (ret != 0)
		return ret;
942

943
	hwmgr = pp_handle->hwmgr;
944 945

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

950 951 952 953 954
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
	mutex_unlock(&pp_handle->pp_lock);

	return ret;
955 956
}

957 958 959 960
static struct amd_vce_state*
pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
{
	struct pp_hwmgr *hwmgr;
961 962
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
963

964
	ret = pp_check(pp_handle);
965

966 967 968 969 970 971 972
	if (ret != 0)
		return NULL;

	hwmgr = pp_handle->hwmgr;

	if (hwmgr && idx < hwmgr->num_vce_state_tables)
		return &hwmgr->vce_states[idx];
973 974 975
	return NULL;
}

976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 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
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;
}

1107
const struct amd_pm_funcs pp_dpm_funcs = {
1108
	.get_temperature = pp_dpm_get_temperature,
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	.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,
1119 1120 1121 1122
	.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,
1123
	.get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
1124 1125 1126 1127 1128
	.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,
1129 1130
	.get_sclk_od = pp_dpm_get_sclk_od,
	.set_sclk_od = pp_dpm_set_sclk_od,
1131 1132
	.get_mclk_od = pp_dpm_get_mclk_od,
	.set_mclk_od = pp_dpm_set_mclk_od,
1133
	.read_sensor = pp_dpm_read_sensor,
1134
	.get_vce_clock_state = pp_dpm_get_vce_clock_state,
1135 1136 1137 1138
	.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,
1139 1140
};

1141 1142
int amd_powerplay_create(struct amd_pp_init *pp_init,
				void **handle)
1143
{
1144
	struct pp_instance *instance;
1145

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

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

1153 1154 1155 1156 1157 1158
	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;
1159
	mutex_init(&instance->pp_lock);
1160
	*handle = instance;
1161 1162 1163
	return 0;
}

1164
int amd_powerplay_destroy(void *handle)
1165 1166
{
	struct pp_instance *instance = (struct pp_instance *)handle;
1167

1168 1169
	kfree(instance->hwmgr);
	instance->hwmgr = NULL;
1170

1171 1172
	kfree(instance);
	instance = NULL;
1173 1174
	return 0;
}
1175

1176 1177 1178 1179 1180
int amd_powerplay_reset(void *handle)
{
	struct pp_instance *instance = (struct pp_instance *)handle;
	int ret;

1181
	if (cgs_is_virtualization_enabled(instance->hwmgr->device))
1182
		return PP_DPM_DISABLED;
1183

1184 1185
	ret = pp_check(instance);
	if (ret != 0)
1186 1187
		return ret;

1188
	ret = pp_hw_fini(instance);
1189 1190 1191
	if (ret)
		return ret;

1192 1193 1194
	ret = hwmgr_hw_init(instance);
	if (ret)
		return PP_DPM_DISABLED;
1195

1196
	return hwmgr_handle_task(instance, AMD_PP_TASK_COMPLETE_INIT, NULL, NULL);
1197 1198
}

1199 1200
/* export this function to DAL */

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

1208
	ret = pp_check(pp_handle);
1209

1210 1211
	if (ret != 0)
		return ret;
1212

1213
	hwmgr = pp_handle->hwmgr;
1214
	mutex_lock(&pp_handle->pp_lock);
1215
	phm_store_dal_configuration_data(hwmgr, display_config);
1216
	mutex_unlock(&pp_handle->pp_lock);
1217 1218
	return 0;
}
1219

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

1227
	ret = pp_check(pp_handle);
1228

1229 1230
	if (ret != 0)
		return ret;
1231

1232
	hwmgr = pp_handle->hwmgr;
1233

1234 1235
	if (output == NULL)
		return -EINVAL;
1236

1237 1238 1239 1240
	mutex_lock(&pp_handle->pp_lock);
	ret = phm_get_dal_power_level(hwmgr, output);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
1241
}
1242 1243

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

1252
	ret = pp_check(pp_handle);
1253

1254 1255
	if (ret != 0)
		return ret;
1256

1257
	hwmgr = pp_handle->hwmgr;
1258

1259 1260
	mutex_lock(&pp_handle->pp_lock);

1261 1262
	phm_get_dal_power_level(hwmgr, &simple_clocks);

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
	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);

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

	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;
	}
1293
	mutex_unlock(&pp_handle->pp_lock);
1294 1295 1296 1297 1298
	return 0;
}

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

1303
	ret = pp_check(pp_handle);
1304

1305 1306 1307 1308
	if (ret != 0)
		return ret;

	hwmgr = pp_handle->hwmgr;
1309 1310

	if (clocks == NULL)
1311 1312
		return -EINVAL;

1313 1314 1315 1316
	mutex_lock(&pp_handle->pp_lock);
	ret = phm_get_clock_by_type(hwmgr, type, clocks);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
1317 1318
}

1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
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);
	if (ret != 0)
		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);
	if (ret != 0)
		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);
	if (ret != 0)
		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);
	if (ret != 0)
		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;
}

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

1420
	ret = pp_check(pp_handle);
1421

1422 1423 1424 1425
	if (ret != 0)
		return ret;

	hwmgr = pp_handle->hwmgr;
1426

1427 1428
	if (clocks == NULL)
		return -EINVAL;
1429

1430 1431
	mutex_lock(&pp_handle->pp_lock);

1432
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
1433
		ret = phm_get_max_high_clocks(hwmgr, clocks);
1434

1435
	mutex_unlock(&pp_handle->pp_lock);
1436
	return ret;
1437 1438
}