amd_powerplay.c 35.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
static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
36
		enum amd_pm_state_type *user_state);
R
Rex Zhu 已提交
37

38
static inline int pp_check(struct pp_instance *handle)
39
{
R
Rex Zhu 已提交
40
	if (handle == NULL)
41
		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
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->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;

81 82 83
	kfree(instance->hwmgr->hardcode_pp_table);
	instance->hwmgr->hardcode_pp_table = NULL;

84 85 86 87 88 89 90 91
	kfree(instance->hwmgr);
	instance->hwmgr = NULL;

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

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

	pp_handle = cgs_register_pp_handle(handle, amd_powerplay_create);

	if (!pp_handle)
		return -EINVAL;
101

102
	ret = hwmgr_early_init(pp_handle);
103
	if (ret)
104
		return -EINVAL;
105

106
	return 0;
107 108
}

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

115
	ret = pp_check(pp_handle);
116

117
	if (ret >= 0) {
118
		hwmgr = pp_handle->hwmgr;
119

120
		if (hwmgr->smumgr_funcs->smu_init == NULL)
121
			return -EINVAL;
122

123
		ret = hwmgr->smumgr_funcs->smu_init(hwmgr);
124

125
		pr_debug("amdgpu: powerplay sw initialized\n");
126 127 128
	}
	return ret;
}
129

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

	ret = pp_check(pp_handle);
137
	if (ret >= 0) {
138
		hwmgr = pp_handle->hwmgr;
139

140
		if (hwmgr->smumgr_funcs->smu_fini == NULL)
141 142
			return -EINVAL;

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

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

154
	ret = pp_check(pp_handle);
155

156
	if (ret >= 0) {
157
		hwmgr = pp_handle->hwmgr;
158

159
		if (hwmgr->smumgr_funcs->start_smu == NULL)
160
			return -EINVAL;
161

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

179 180 181 182
}

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

186
	ret = pp_check(pp_handle);
187
	if (ret == 0)
188
		hwmgr_hw_fini(pp_handle);
189

190 191 192
	return 0;
}

R
Rex Zhu 已提交
193 194 195 196 197 198 199 200
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,
201
					AMD_PP_TASK_COMPLETE_INIT, NULL);
R
Rex Zhu 已提交
202 203 204 205

	return 0;
}

206 207 208 209 210 211
static void pp_late_fini(void *handle)
{
	amd_powerplay_destroy(handle);
}


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

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

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

static int pp_set_powergating_state(void *handle,
				    enum amd_powergating_state state)
{
230
	struct pp_hwmgr  *hwmgr;
231 232
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
233

234
	ret = pp_check(pp_handle);
235

236
	if (ret)
237
		return ret;
238

239
	hwmgr = pp_handle->hwmgr;
240 241

	if (hwmgr->hwmgr_func->enable_per_cu_power_gating == NULL) {
242
		pr_info("%s was not implemented.\n", __func__);
243 244
		return 0;
	}
245 246 247

	/* Enable/disable GFX per cu powergating through SMU */
	return hwmgr->hwmgr_func->enable_per_cu_power_gating(hwmgr,
248
			state == AMD_PG_STATE_GATE);
249 250 251 252
}

static int pp_suspend(void *handle)
{
253 254
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
255

256
	ret = pp_check(pp_handle);
257 258 259
	if (ret == 0)
		hwmgr_hw_suspend(pp_handle);
	return 0;
260 261 262 263
}

static int pp_resume(void *handle)
{
264
	struct pp_hwmgr  *hwmgr;
265
	int ret;
266
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
267

268
	ret = pp_check(pp_handle);
269

270 271
	if (ret < 0)
		return ret;
272

273
	hwmgr = pp_handle->hwmgr;
274

275
	if (hwmgr->smumgr_funcs->start_smu == NULL)
276 277
		return -EINVAL;

278
	if (hwmgr->smumgr_funcs->start_smu(pp_handle->hwmgr)) {
279
		pr_err("smc start failed\n");
280
		hwmgr->smumgr_funcs->smu_fini(pp_handle->hwmgr);
281
		return -EINVAL;
282 283
	}

284
	if (ret == PP_DPM_DISABLED)
M
Monk Liu 已提交
285
		return 0;
286

287
	return hwmgr_hw_resume(pp_handle);
288 289 290
}

const struct amd_ip_funcs pp_ip_funcs = {
291
	.name = "powerplay",
292
	.early_init = pp_early_init,
R
Rex Zhu 已提交
293
	.late_init = pp_late_init,
294 295 296 297
	.sw_init = pp_sw_init,
	.sw_fini = pp_sw_fini,
	.hw_init = pp_hw_init,
	.hw_fini = pp_hw_fini,
298
	.late_fini = pp_late_fini,
299 300 301 302 303
	.suspend = pp_suspend,
	.resume = pp_resume,
	.is_idle = pp_is_idle,
	.wait_for_idle = pp_wait_for_idle,
	.soft_reset = pp_sw_reset,
304
	.set_clockgating_state = NULL,
305 306 307 308 309 310 311 312 313 314 315 316 317
	.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;
}

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
static int pp_set_clockgating_by_smu(void *handle, uint32_t msg_id)
{
	struct pp_hwmgr  *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;

	ret = pp_check(pp_handle);

	if (ret)
		return ret;

	hwmgr = pp_handle->hwmgr;

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

	return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
}

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

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

382
	ret = pp_check(pp_handle);
383

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

	hwmgr = pp_handle->hwmgr;

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

392
	mutex_lock(&pp_handle->pp_lock);
393 394
	pp_dpm_en_umd_pstate(hwmgr, &level);
	hwmgr->request_dpm_level = level;
395
	hwmgr_handle_task(pp_handle, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
396
	mutex_unlock(&pp_handle->pp_lock);
397

398 399
	return 0;
}
400

401 402 403
static enum amd_dpm_forced_level pp_dpm_get_performance_level(
								void *handle)
{
404
	struct pp_hwmgr  *hwmgr;
405 406
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
407
	enum amd_dpm_forced_level level;
408

409
	ret = pp_check(pp_handle);
410

411
	if (ret)
412
		return ret;
413

414
	hwmgr = pp_handle->hwmgr;
415 416 417 418
	mutex_lock(&pp_handle->pp_lock);
	level = hwmgr->dpm_level;
	mutex_unlock(&pp_handle->pp_lock);
	return level;
419
}
420

421
static uint32_t pp_dpm_get_sclk(void *handle, bool low)
422
{
423
	struct pp_hwmgr  *hwmgr;
424 425
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
426
	uint32_t clk = 0;
427

428
	ret = pp_check(pp_handle);
429

430
	if (ret)
431
		return ret;
432

433
	hwmgr = pp_handle->hwmgr;
434 435

	if (hwmgr->hwmgr_func->get_sclk == NULL) {
436
		pr_info("%s was not implemented.\n", __func__);
437 438
		return 0;
	}
439
	mutex_lock(&pp_handle->pp_lock);
440
	clk = hwmgr->hwmgr_func->get_sclk(hwmgr, low);
441
	mutex_unlock(&pp_handle->pp_lock);
442
	return clk;
443
}
444

445
static uint32_t pp_dpm_get_mclk(void *handle, bool low)
446
{
447
	struct pp_hwmgr  *hwmgr;
448 449
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
450
	uint32_t clk = 0;
451

452
	ret = pp_check(pp_handle);
453

454
	if (ret)
455
		return ret;
456

457
	hwmgr = pp_handle->hwmgr;
458 459

	if (hwmgr->hwmgr_func->get_mclk == NULL) {
460
		pr_info("%s was not implemented.\n", __func__);
461 462
		return 0;
	}
463
	mutex_lock(&pp_handle->pp_lock);
464
	clk = hwmgr->hwmgr_func->get_mclk(hwmgr, low);
465
	mutex_unlock(&pp_handle->pp_lock);
466
	return clk;
467
}
468

469
static void pp_dpm_powergate_vce(void *handle, bool gate)
470
{
471
	struct pp_hwmgr  *hwmgr;
472 473
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
474

475
	ret = pp_check(pp_handle);
476

477
	if (ret)
478
		return;
479

480
	hwmgr = pp_handle->hwmgr;
481 482

	if (hwmgr->hwmgr_func->powergate_vce == NULL) {
483
		pr_info("%s was not implemented.\n", __func__);
484
		return;
485
	}
486
	mutex_lock(&pp_handle->pp_lock);
487
	hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
488
	mutex_unlock(&pp_handle->pp_lock);
489
}
490

491
static void pp_dpm_powergate_uvd(void *handle, bool gate)
492
{
493
	struct pp_hwmgr  *hwmgr;
494 495
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
496

497
	ret = pp_check(pp_handle);
498

499
	if (ret)
500
		return;
501

502
	hwmgr = pp_handle->hwmgr;
503 504

	if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
505
		pr_info("%s was not implemented.\n", __func__);
506
		return;
507
	}
508
	mutex_lock(&pp_handle->pp_lock);
509
	hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
510
	mutex_unlock(&pp_handle->pp_lock);
511 512
}

513
static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
514
		enum amd_pm_state_type *user_state)
515
{
516
	int ret = 0;
517
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
518

519
	ret = pp_check(pp_handle);
520

521
	if (ret)
522
		return ret;
523

524
	mutex_lock(&pp_handle->pp_lock);
525
	ret = hwmgr_handle_task(pp_handle, task_id, user_state);
526
	mutex_unlock(&pp_handle->pp_lock);
527

528
	return ret;
529
}
530

531
static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
532
{
533 534
	struct pp_hwmgr *hwmgr;
	struct pp_power_state *state;
535 536
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
537
	enum amd_pm_state_type pm_type;
538

539
	ret = pp_check(pp_handle);
540

541
	if (ret)
542
		return ret;
543

544 545 546
	hwmgr = pp_handle->hwmgr;

	if (hwmgr->current_ps == NULL)
547 548
		return -EINVAL;

549 550
	mutex_lock(&pp_handle->pp_lock);

551 552 553 554
	state = hwmgr->current_ps;

	switch (state->classification.ui_label) {
	case PP_StateUILabel_Battery:
555
		pm_type = POWER_STATE_TYPE_BATTERY;
556
		break;
557
	case PP_StateUILabel_Balanced:
558
		pm_type = POWER_STATE_TYPE_BALANCED;
559
		break;
560
	case PP_StateUILabel_Performance:
561
		pm_type = POWER_STATE_TYPE_PERFORMANCE;
562
		break;
563
	default:
564
		if (state->classification.flags & PP_StateClassificationFlag_Boot)
565
			pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
566
		else
567
			pm_type = POWER_STATE_TYPE_DEFAULT;
568
		break;
569
	}
570 571 572
	mutex_unlock(&pp_handle->pp_lock);

	return pm_type;
573
}
574

575
static void pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
576 577
{
	struct pp_hwmgr  *hwmgr;
578 579
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
580

581
	ret = pp_check(pp_handle);
582

583
	if (ret)
584
		return;
585

586
	hwmgr = pp_handle->hwmgr;
587 588

	if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
589
		pr_info("%s was not implemented.\n", __func__);
590
		return;
591
	}
592
	mutex_lock(&pp_handle->pp_lock);
593
	hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
594
	mutex_unlock(&pp_handle->pp_lock);
595 596
}

597
static uint32_t pp_dpm_get_fan_control_mode(void *handle)
598 599
{
	struct pp_hwmgr  *hwmgr;
600 601
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
602
	uint32_t mode = 0;
603

604
	ret = pp_check(pp_handle);
605

606
	if (ret)
607
		return ret;
608

609
	hwmgr = pp_handle->hwmgr;
610 611

	if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
612
		pr_info("%s was not implemented.\n", __func__);
613 614
		return 0;
	}
615
	mutex_lock(&pp_handle->pp_lock);
616
	mode = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
617
	mutex_unlock(&pp_handle->pp_lock);
618
	return mode;
619 620 621 622 623
}

static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
{
	struct pp_hwmgr  *hwmgr;
624 625
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
626

627
	ret = pp_check(pp_handle);
628

629
	if (ret)
630
		return ret;
631

632
	hwmgr = pp_handle->hwmgr;
633 634

	if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
635
		pr_info("%s was not implemented.\n", __func__);
636 637
		return 0;
	}
638 639 640 641
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
642 643 644 645 646
}

static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
{
	struct pp_hwmgr  *hwmgr;
647 648
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
649

650
	ret = pp_check(pp_handle);
651

652
	if (ret)
653
		return ret;
654

655
	hwmgr = pp_handle->hwmgr;
656 657

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

662 663 664 665
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
666 667
}

668 669 670
static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
{
	struct pp_hwmgr *hwmgr;
671 672
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
673

674
	ret = pp_check(pp_handle);
675

676
	if (ret)
677
		return ret;
678

679
	hwmgr = pp_handle->hwmgr;
680 681 682 683

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

684 685 686 687
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
688 689
}

690 691 692 693 694
static int pp_dpm_get_pp_num_states(void *handle,
		struct pp_states_info *data)
{
	struct pp_hwmgr *hwmgr;
	int i;
695 696
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
697

698 699
	memset(data, 0, sizeof(*data));

700
	ret = pp_check(pp_handle);
701

702
	if (ret)
703 704 705
		return ret;

	hwmgr = pp_handle->hwmgr;
706

707
	if (hwmgr->ps == NULL)
708 709
		return -EINVAL;

710 711
	mutex_lock(&pp_handle->pp_lock);

712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	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;
		}
	}
734
	mutex_unlock(&pp_handle->pp_lock);
735 736 737 738 739 740
	return 0;
}

static int pp_dpm_get_pp_table(void *handle, char **table)
{
	struct pp_hwmgr *hwmgr;
741 742
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
743
	int size = 0;
744

745
	ret = pp_check(pp_handle);
746

747
	if (ret)
748
		return ret;
749

750
	hwmgr = pp_handle->hwmgr;
751

752 753 754
	if (!hwmgr->soft_pp_table)
		return -EINVAL;

755
	mutex_lock(&pp_handle->pp_lock);
756
	*table = (char *)hwmgr->soft_pp_table;
757 758 759
	size = hwmgr->soft_pp_table_size;
	mutex_unlock(&pp_handle->pp_lock);
	return size;
760 761
}

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
static int amd_powerplay_reset(void *handle)
{
	struct pp_instance *instance = (struct pp_instance *)handle;
	int ret;

	ret = pp_check(instance);
	if (ret)
		return ret;

	ret = pp_hw_fini(instance);
	if (ret)
		return ret;

	ret = hwmgr_hw_init(instance);
	if (ret)
		return ret;

779
	return hwmgr_handle_task(instance, AMD_PP_TASK_COMPLETE_INIT, NULL);
780 781
}

782 783 784
static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
{
	struct pp_hwmgr *hwmgr;
785 786
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
787

788
	ret = pp_check(pp_handle);
789

790
	if (ret)
791
		return ret;
792

793
	hwmgr = pp_handle->hwmgr;
794
	mutex_lock(&pp_handle->pp_lock);
795
	if (!hwmgr->hardcode_pp_table) {
796 797 798
		hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
						   hwmgr->soft_pp_table_size,
						   GFP_KERNEL);
799 800
		if (!hwmgr->hardcode_pp_table) {
			mutex_unlock(&pp_handle->pp_lock);
801
			return -ENOMEM;
802
		}
803
	}
804

805 806 807
	memcpy(hwmgr->hardcode_pp_table, buf, size);

	hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
808
	mutex_unlock(&pp_handle->pp_lock);
809

810 811 812 813 814 815 816 817 818 819 820
	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;
821 822 823
}

static int pp_dpm_force_clock_level(void *handle,
824
		enum pp_clock_type type, uint32_t mask)
825 826
{
	struct pp_hwmgr *hwmgr;
827 828
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
829

830
	ret = pp_check(pp_handle);
831

832
	if (ret)
833
		return ret;
834

835
	hwmgr = pp_handle->hwmgr;
836 837

	if (hwmgr->hwmgr_func->force_clock_level == NULL) {
838
		pr_info("%s was not implemented.\n", __func__);
839 840
		return 0;
	}
841
	mutex_lock(&pp_handle->pp_lock);
842 843 844 845
	if (hwmgr->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL)
		ret = hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
	else
		ret = -EINVAL;
846 847
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
848 849 850 851 852 853
}

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

857
	ret = pp_check(pp_handle);
858

859
	if (ret)
860
		return ret;
861

862
	hwmgr = pp_handle->hwmgr;
863

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

874 875 876
static int pp_dpm_get_sclk_od(void *handle)
{
	struct pp_hwmgr *hwmgr;
877 878
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
879

880
	ret = pp_check(pp_handle);
881

882
	if (ret)
883
		return ret;
884

885
	hwmgr = pp_handle->hwmgr;
886 887

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

static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
{
	struct pp_hwmgr *hwmgr;
900 901
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
902

903
	ret = pp_check(pp_handle);
904

905
	if (ret)
906
		return ret;
907

908
	hwmgr = pp_handle->hwmgr;
909 910

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

915 916
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
917
	mutex_unlock(&pp_handle->pp_lock);
918
	return ret;
919 920
}

921 922 923
static int pp_dpm_get_mclk_od(void *handle)
{
	struct pp_hwmgr *hwmgr;
924 925
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
926

927
	ret = pp_check(pp_handle);
928

929
	if (ret)
930
		return ret;
931

932
	hwmgr = pp_handle->hwmgr;
933 934

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

static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
{
	struct pp_hwmgr *hwmgr;
947 948
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
949

950
	ret = pp_check(pp_handle);
951

952
	if (ret)
953
		return ret;
954

955
	hwmgr = pp_handle->hwmgr;
956 957

	if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
958
		pr_info("%s was not implemented.\n", __func__);
959 960
		return 0;
	}
961 962 963 964
	mutex_lock(&pp_handle->pp_lock);
	ret = hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
965 966
}

967 968
static int pp_dpm_read_sensor(void *handle, int idx,
			      void *value, int *size)
969 970
{
	struct pp_hwmgr *hwmgr;
971 972
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
973

974
	ret = pp_check(pp_handle);
975
	if (ret)
976
		return ret;
977

978 979 980
	if (value == NULL)
		return -EINVAL;

981
	hwmgr = pp_handle->hwmgr;
982

983 984 985 986 987 988
	switch (idx) {
	case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
		*((uint32_t *)value) = hwmgr->pstate_sclk;
		return 0;
	case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
		*((uint32_t *)value) = hwmgr->pstate_mclk;
989
		return 0;
990 991 992 993 994
	default:
		mutex_lock(&pp_handle->pp_lock);
		ret = hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
		mutex_unlock(&pp_handle->pp_lock);
		return ret;
995 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
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;
}

1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
static int pp_get_power_profile_mode(void *handle, char *buf)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;

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

	hwmgr = pp_handle->hwmgr;

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

	return hwmgr->hwmgr_func->get_power_profile_mode(hwmgr, buf);
}

static int pp_set_power_profile_mode(void *handle, long *input, uint32_t size)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
1091
	int ret = -EINVAL;
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

	if (pp_check(pp_handle))
		return -EINVAL;

	hwmgr = pp_handle->hwmgr;

	if (hwmgr->hwmgr_func->set_power_profile_mode == NULL) {
		pr_info("%s was not implemented.\n", __func__);
		return -EINVAL;
	}
1102 1103 1104 1105 1106
	mutex_lock(&pp_handle->pp_lock);
	if (hwmgr->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL)
		ret = hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, input, size);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
1107 1108
}

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
static int pp_odn_edit_dpm_table(void *handle, uint32_t type, long *input, uint32_t size)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;

	if (pp_check(pp_handle))
		return -EINVAL;

	hwmgr = pp_handle->hwmgr;

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

	return hwmgr->hwmgr_func->odn_edit_dpm_table(hwmgr, type, input, size);
}

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

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
static int pp_dpm_notify_smu_memory_info(void *handle,
					uint32_t virtual_addr_low,
					uint32_t virtual_addr_hi,
					uint32_t mc_addr_low,
					uint32_t mc_addr_hi,
					uint32_t size)
{
	struct pp_hwmgr  *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;

	ret = pp_check(pp_handle);

	if (ret)
		return ret;

	hwmgr = pp_handle->hwmgr;

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

	mutex_lock(&pp_handle->pp_lock);

	ret = hwmgr->hwmgr_func->notify_cac_buffer_info(hwmgr, virtual_addr_low,
					virtual_addr_hi, mc_addr_low, mc_addr_hi,
					size);

	mutex_unlock(&pp_handle->pp_lock);

	return ret;
}

1240
static int pp_display_configuration_change(void *handle,
1241
	const struct amd_pp_display_configuration *display_config)
1242 1243
{
	struct pp_hwmgr  *hwmgr;
1244 1245
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1246

1247
	ret = pp_check(pp_handle);
1248

1249
	if (ret)
1250
		return ret;
1251

1252
	hwmgr = pp_handle->hwmgr;
1253
	mutex_lock(&pp_handle->pp_lock);
1254
	phm_store_dal_configuration_data(hwmgr, display_config);
1255
	mutex_unlock(&pp_handle->pp_lock);
1256 1257
	return 0;
}
1258

1259
static int pp_get_display_power_level(void *handle,
R
Rex Zhu 已提交
1260
		struct amd_pp_simple_clock_info *output)
1261 1262
{
	struct pp_hwmgr  *hwmgr;
1263 1264
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1265

1266
	ret = pp_check(pp_handle);
1267

1268
	if (ret)
1269
		return ret;
1270

1271
	hwmgr = pp_handle->hwmgr;
1272

1273 1274
	if (output == NULL)
		return -EINVAL;
1275

1276 1277 1278 1279
	mutex_lock(&pp_handle->pp_lock);
	ret = phm_get_dal_power_level(hwmgr, output);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
1280
}
1281

1282
static int pp_get_current_clocks(void *handle,
1283
		struct amd_pp_clock_info *clocks)
1284 1285 1286
{
	struct amd_pp_simple_clock_info simple_clocks;
	struct pp_clock_info hw_clocks;
1287 1288 1289
	struct pp_hwmgr  *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1290

1291
	ret = pp_check(pp_handle);
1292

1293
	if (ret)
1294
		return ret;
1295

1296
	hwmgr = pp_handle->hwmgr;
1297

1298 1299
	mutex_lock(&pp_handle->pp_lock);

1300 1301
	phm_get_dal_power_level(hwmgr, &simple_clocks);

1302 1303 1304 1305 1306 1307 1308 1309
	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);

1310
	if (ret) {
1311 1312 1313
		pr_info("Error in phm_get_clock_info \n");
		mutex_unlock(&pp_handle->pp_lock);
		return -EINVAL;
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	}

	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;
	}
1332
	mutex_unlock(&pp_handle->pp_lock);
1333 1334 1335
	return 0;
}

1336
static int pp_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
1337
{
1338 1339 1340
	struct pp_hwmgr  *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1341

1342
	ret = pp_check(pp_handle);
1343

1344
	if (ret)
1345 1346 1347
		return ret;

	hwmgr = pp_handle->hwmgr;
1348 1349

	if (clocks == NULL)
1350 1351
		return -EINVAL;

1352 1353 1354 1355
	mutex_lock(&pp_handle->pp_lock);
	ret = phm_get_clock_by_type(hwmgr, type, clocks);
	mutex_unlock(&pp_handle->pp_lock);
	return ret;
1356 1357
}

1358
static int pp_get_clock_by_type_with_latency(void *handle,
1359 1360 1361 1362 1363 1364 1365 1366
		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);
1367
	if (ret)
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
		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;
}

1380
static int pp_get_clock_by_type_with_voltage(void *handle,
1381 1382 1383 1384 1385 1386 1387 1388
		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);
1389
	if (ret)
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
		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;
}

1405
static int pp_set_watermarks_for_clocks_ranges(void *handle,
1406 1407 1408 1409 1410 1411 1412
		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);
1413
	if (ret)
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
		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;
}

1429
static int pp_display_clock_voltage_request(void *handle,
1430 1431 1432 1433 1434 1435 1436
		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);
1437
	if (ret)
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
		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;
}

1452
static int pp_get_display_mode_validation_clocks(void *handle,
1453
		struct amd_pp_simple_clock_info *clocks)
1454 1455
{
	struct pp_hwmgr  *hwmgr;
1456 1457
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;
1458

1459
	ret = pp_check(pp_handle);
1460

1461
	if (ret)
1462 1463 1464
		return ret;

	hwmgr = pp_handle->hwmgr;
1465

1466 1467
	if (clocks == NULL)
		return -EINVAL;
1468

1469 1470
	mutex_lock(&pp_handle->pp_lock);

1471
	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
1472
		ret = phm_get_max_high_clocks(hwmgr, clocks);
1473

1474
	mutex_unlock(&pp_handle->pp_lock);
1475
	return ret;
1476 1477
}

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
static int pp_set_mmhub_powergating_by_smu(void *handle)
{
	struct pp_hwmgr *hwmgr;
	struct pp_instance *pp_handle = (struct pp_instance *)handle;
	int ret = 0;

	ret = pp_check(pp_handle);

	if (ret)
		return ret;

	hwmgr = pp_handle->hwmgr;

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

	return hwmgr->hwmgr_func->set_mmhub_powergating_by_smu(hwmgr);
}

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
const struct amd_pm_funcs pp_dpm_funcs = {
	.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,
	.powergate_vce = pp_dpm_powergate_vce,
	.powergate_uvd = pp_dpm_powergate_uvd,
	.dispatch_tasks = pp_dpm_dispatch_tasks,
	.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,
	.get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
	.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,
	.get_sclk_od = pp_dpm_get_sclk_od,
	.set_sclk_od = pp_dpm_set_sclk_od,
	.get_mclk_od = pp_dpm_get_mclk_od,
	.set_mclk_od = pp_dpm_set_mclk_od,
	.read_sensor = pp_dpm_read_sensor,
	.get_vce_clock_state = pp_dpm_get_vce_clock_state,
	.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,
	.set_clockgating_by_smu = pp_set_clockgating_by_smu,
1529
	.notify_smu_memory_info = pp_dpm_notify_smu_memory_info,
1530 1531
	.get_power_profile_mode = pp_get_power_profile_mode,
	.set_power_profile_mode = pp_set_power_profile_mode,
1532
	.odn_edit_dpm_table = pp_odn_edit_dpm_table,
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
/* export to DC */
	.get_sclk = pp_dpm_get_sclk,
	.get_mclk = pp_dpm_get_mclk,
	.display_configuration_change = pp_display_configuration_change,
	.get_display_power_level = pp_get_display_power_level,
	.get_current_clocks = pp_get_current_clocks,
	.get_clock_by_type = pp_get_clock_by_type,
	.get_clock_by_type_with_latency = pp_get_clock_by_type_with_latency,
	.get_clock_by_type_with_voltage = pp_get_clock_by_type_with_voltage,
	.set_watermarks_for_clocks_ranges = pp_set_watermarks_for_clocks_ranges,
	.display_clock_voltage_request = pp_display_clock_voltage_request,
	.get_display_mode_validation_clocks = pp_get_display_mode_validation_clocks,
1545
	.set_mmhub_powergating_by_smu = pp_set_mmhub_powergating_by_smu,
1546
};