1 /*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23 #include "pp_debug.h"
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/gfp.h>
27 #include <linux/slab.h>
28 #include <linux/firmware.h>
29 #include <linux/reboot.h>
30 #include "amd_shared.h"
31 #include "amd_powerplay.h"
32 #include "power_state.h"
33 #include "amdgpu.h"
34 #include "hwmgr.h"
35 #include "amdgpu_dpm_internal.h"
36 #include "amdgpu_display.h"
37
38 static const struct amd_pm_funcs pp_dpm_funcs;
39
amd_powerplay_create(struct amdgpu_device * adev)40 static int amd_powerplay_create(struct amdgpu_device *adev)
41 {
42 struct pp_hwmgr *hwmgr;
43
44 if (adev == NULL)
45 return -EINVAL;
46
47 hwmgr = kzalloc(sizeof(struct pp_hwmgr), GFP_KERNEL);
48 if (hwmgr == NULL)
49 return -ENOMEM;
50
51 hwmgr->adev = adev;
52 hwmgr->not_vf = !amdgpu_sriov_vf(adev);
53 hwmgr->device = amdgpu_cgs_create_device(adev);
54 mutex_init(&hwmgr->msg_lock);
55 hwmgr->chip_family = adev->family;
56 hwmgr->chip_id = adev->asic_type;
57 hwmgr->feature_mask = adev->pm.pp_feature;
58 hwmgr->display_config = &adev->pm.pm_display_cfg;
59 adev->powerplay.pp_handle = hwmgr;
60 adev->powerplay.pp_funcs = &pp_dpm_funcs;
61 return 0;
62 }
63
64
amd_powerplay_destroy(struct amdgpu_device * adev)65 static void amd_powerplay_destroy(struct amdgpu_device *adev)
66 {
67 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
68
69 mutex_destroy(&hwmgr->msg_lock);
70
71 kfree(hwmgr->hardcode_pp_table);
72 hwmgr->hardcode_pp_table = NULL;
73
74 kfree(hwmgr);
75 hwmgr = NULL;
76 }
77
pp_early_init(struct amdgpu_ip_block * ip_block)78 static int pp_early_init(struct amdgpu_ip_block *ip_block)
79 {
80 int ret;
81 struct amdgpu_device *adev = ip_block->adev;
82 ret = amd_powerplay_create(adev);
83
84 if (ret != 0)
85 return ret;
86
87 ret = hwmgr_early_init(adev->powerplay.pp_handle);
88 if (ret)
89 return -EINVAL;
90
91 return 0;
92 }
93
pp_swctf_delayed_work_handler(struct work_struct * work)94 static void pp_swctf_delayed_work_handler(struct work_struct *work)
95 {
96 struct pp_hwmgr *hwmgr =
97 container_of(work, struct pp_hwmgr, swctf_delayed_work.work);
98 struct amdgpu_device *adev = hwmgr->adev;
99 struct amdgpu_dpm_thermal *range =
100 &adev->pm.dpm.thermal;
101 uint32_t gpu_temperature, size = sizeof(gpu_temperature);
102 int ret;
103
104 /*
105 * If the hotspot/edge temperature is confirmed as below SW CTF setting point
106 * after the delay enforced, nothing will be done.
107 * Otherwise, a graceful shutdown will be performed to prevent further damage.
108 */
109 if (range->sw_ctf_threshold &&
110 hwmgr->hwmgr_func->read_sensor) {
111 ret = hwmgr->hwmgr_func->read_sensor(hwmgr,
112 AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
113 &gpu_temperature,
114 &size);
115 /*
116 * For some legacy ASICs, hotspot temperature retrieving might be not
117 * supported. Check the edge temperature instead then.
118 */
119 if (ret == -EOPNOTSUPP)
120 ret = hwmgr->hwmgr_func->read_sensor(hwmgr,
121 AMDGPU_PP_SENSOR_EDGE_TEMP,
122 &gpu_temperature,
123 &size);
124 if (!ret && gpu_temperature / 1000 < range->sw_ctf_threshold)
125 return;
126 }
127
128 dev_emerg(adev->dev, "ERROR: GPU over temperature range(SW CTF) detected!\n");
129 dev_emerg(adev->dev, "ERROR: System is going to shutdown due to GPU SW CTF!\n");
130 orderly_poweroff(true);
131 }
132
pp_sw_init(struct amdgpu_ip_block * ip_block)133 static int pp_sw_init(struct amdgpu_ip_block *ip_block)
134 {
135 struct amdgpu_device *adev = ip_block->adev;
136 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
137 int ret = 0;
138
139 ret = hwmgr_sw_init(hwmgr);
140
141 pr_debug("powerplay sw init %s\n", ret ? "failed" : "successfully");
142
143 if (!ret)
144 INIT_DELAYED_WORK(&hwmgr->swctf_delayed_work,
145 pp_swctf_delayed_work_handler);
146
147 return ret;
148 }
149
pp_sw_fini(struct amdgpu_ip_block * ip_block)150 static int pp_sw_fini(struct amdgpu_ip_block *ip_block)
151 {
152 struct amdgpu_device *adev = ip_block->adev;
153 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
154
155 hwmgr_sw_fini(hwmgr);
156
157 amdgpu_ucode_release(&adev->pm.fw);
158
159 return 0;
160 }
161
pp_hw_init(struct amdgpu_ip_block * ip_block)162 static int pp_hw_init(struct amdgpu_ip_block *ip_block)
163 {
164 int ret = 0;
165 struct amdgpu_device *adev = ip_block->adev;
166 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
167
168 ret = hwmgr_hw_init(hwmgr);
169
170 if (ret)
171 pr_err("powerplay hw init failed\n");
172
173 return ret;
174 }
175
pp_hw_fini(struct amdgpu_ip_block * ip_block)176 static int pp_hw_fini(struct amdgpu_ip_block *ip_block)
177 {
178 struct pp_hwmgr *hwmgr = ip_block->adev->powerplay.pp_handle;
179
180 cancel_delayed_work_sync(&hwmgr->swctf_delayed_work);
181
182 hwmgr_hw_fini(hwmgr);
183
184 return 0;
185 }
186
pp_reserve_vram_for_smu(struct amdgpu_device * adev)187 static void pp_reserve_vram_for_smu(struct amdgpu_device *adev)
188 {
189 int r = -EINVAL;
190 void *cpu_ptr = NULL;
191 uint64_t gpu_addr;
192 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
193
194 if (amdgpu_bo_create_kernel(adev, adev->pm.smu_prv_buffer_size,
195 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
196 &adev->pm.smu_prv_buffer,
197 &gpu_addr,
198 &cpu_ptr)) {
199 DRM_ERROR("amdgpu: failed to create smu prv buffer\n");
200 return;
201 }
202
203 if (hwmgr->hwmgr_func->notify_cac_buffer_info)
204 r = hwmgr->hwmgr_func->notify_cac_buffer_info(hwmgr,
205 lower_32_bits((unsigned long)cpu_ptr),
206 upper_32_bits((unsigned long)cpu_ptr),
207 lower_32_bits(gpu_addr),
208 upper_32_bits(gpu_addr),
209 adev->pm.smu_prv_buffer_size);
210
211 if (r) {
212 amdgpu_bo_free_kernel(&adev->pm.smu_prv_buffer, NULL, NULL);
213 adev->pm.smu_prv_buffer = NULL;
214 DRM_ERROR("amdgpu: failed to notify SMU buffer address\n");
215 }
216 }
217
pp_late_init(struct amdgpu_ip_block * ip_block)218 static int pp_late_init(struct amdgpu_ip_block *ip_block)
219 {
220 struct amdgpu_device *adev = ip_block->adev;
221 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
222
223 if (hwmgr && hwmgr->pm_en)
224 hwmgr_handle_task(hwmgr,
225 AMD_PP_TASK_COMPLETE_INIT, NULL);
226 if (adev->pm.smu_prv_buffer_size != 0)
227 pp_reserve_vram_for_smu(adev);
228
229 return 0;
230 }
231
pp_late_fini(struct amdgpu_ip_block * ip_block)232 static void pp_late_fini(struct amdgpu_ip_block *ip_block)
233 {
234 struct amdgpu_device *adev = ip_block->adev;
235
236 if (adev->pm.smu_prv_buffer)
237 amdgpu_bo_free_kernel(&adev->pm.smu_prv_buffer, NULL, NULL);
238 amd_powerplay_destroy(adev);
239 }
240
241
pp_is_idle(void * handle)242 static bool pp_is_idle(void *handle)
243 {
244 return false;
245 }
246
pp_set_powergating_state(struct amdgpu_ip_block * ip_block,enum amd_powergating_state state)247 static int pp_set_powergating_state(struct amdgpu_ip_block *ip_block,
248 enum amd_powergating_state state)
249 {
250 return 0;
251 }
252
pp_suspend(struct amdgpu_ip_block * ip_block)253 static int pp_suspend(struct amdgpu_ip_block *ip_block)
254 {
255 struct amdgpu_device *adev = ip_block->adev;
256 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
257
258 cancel_delayed_work_sync(&hwmgr->swctf_delayed_work);
259
260 return hwmgr_suspend(hwmgr);
261 }
262
pp_resume(struct amdgpu_ip_block * ip_block)263 static int pp_resume(struct amdgpu_ip_block *ip_block)
264 {
265 struct pp_hwmgr *hwmgr = ip_block->adev->powerplay.pp_handle;
266
267 return hwmgr_resume(hwmgr);
268 }
269
pp_set_clockgating_state(struct amdgpu_ip_block * ip_block,enum amd_clockgating_state state)270 static int pp_set_clockgating_state(struct amdgpu_ip_block *ip_block,
271 enum amd_clockgating_state state)
272 {
273 return 0;
274 }
275
276 static const struct amd_ip_funcs pp_ip_funcs = {
277 .name = "powerplay",
278 .early_init = pp_early_init,
279 .late_init = pp_late_init,
280 .sw_init = pp_sw_init,
281 .sw_fini = pp_sw_fini,
282 .hw_init = pp_hw_init,
283 .hw_fini = pp_hw_fini,
284 .late_fini = pp_late_fini,
285 .suspend = pp_suspend,
286 .resume = pp_resume,
287 .is_idle = pp_is_idle,
288 .set_clockgating_state = pp_set_clockgating_state,
289 .set_powergating_state = pp_set_powergating_state,
290 };
291
292 const struct amdgpu_ip_block_version pp_smu_ip_block =
293 {
294 .type = AMD_IP_BLOCK_TYPE_SMC,
295 .major = 1,
296 .minor = 0,
297 .rev = 0,
298 .funcs = &pp_ip_funcs,
299 };
300
301 /* This interface only be supported On Vi,
302 * because only smu7/8 can help to load gfx/sdma fw,
303 * smu need to be enabled before load other ip's fw.
304 * so call start smu to load smu7 fw and other ip's fw
305 */
pp_dpm_load_fw(void * handle)306 static int pp_dpm_load_fw(void *handle)
307 {
308 struct pp_hwmgr *hwmgr = handle;
309
310 if (!hwmgr || !hwmgr->smumgr_funcs || !hwmgr->smumgr_funcs->start_smu)
311 return -EINVAL;
312
313 if (hwmgr->smumgr_funcs->start_smu(hwmgr)) {
314 pr_err("fw load failed\n");
315 return -EINVAL;
316 }
317
318 return 0;
319 }
320
pp_dpm_fw_loading_complete(void * handle)321 static int pp_dpm_fw_loading_complete(void *handle)
322 {
323 return 0;
324 }
325
pp_set_clockgating_by_smu(void * handle,uint32_t msg_id)326 static int pp_set_clockgating_by_smu(void *handle, uint32_t msg_id)
327 {
328 struct pp_hwmgr *hwmgr = handle;
329
330 if (!hwmgr || !hwmgr->pm_en)
331 return -EINVAL;
332
333 if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
334 pr_info_ratelimited("%s was not implemented.\n", __func__);
335 return 0;
336 }
337
338 return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
339 }
340
pp_dpm_en_umd_pstate(struct pp_hwmgr * hwmgr,enum amd_dpm_forced_level * level)341 static void pp_dpm_en_umd_pstate(struct pp_hwmgr *hwmgr,
342 enum amd_dpm_forced_level *level)
343 {
344 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
345 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
346 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
347 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
348
349 if (!(hwmgr->dpm_level & profile_mode_mask)) {
350 /* enter umd pstate, save current level, disable gfx cg*/
351 if (*level & profile_mode_mask) {
352 hwmgr->saved_dpm_level = hwmgr->dpm_level;
353 hwmgr->en_umd_pstate = true;
354 }
355 } else {
356 /* exit umd pstate, restore level, enable gfx cg*/
357 if (!(*level & profile_mode_mask)) {
358 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
359 *level = hwmgr->saved_dpm_level;
360 hwmgr->en_umd_pstate = false;
361 }
362 }
363 }
364
pp_dpm_force_performance_level(void * handle,enum amd_dpm_forced_level level)365 static int pp_dpm_force_performance_level(void *handle,
366 enum amd_dpm_forced_level level)
367 {
368 struct pp_hwmgr *hwmgr = handle;
369
370 if (!hwmgr || !hwmgr->pm_en)
371 return -EINVAL;
372
373 if (level == hwmgr->dpm_level)
374 return 0;
375
376 pp_dpm_en_umd_pstate(hwmgr, &level);
377 hwmgr->request_dpm_level = level;
378 hwmgr_handle_task(hwmgr, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
379
380 return 0;
381 }
382
pp_dpm_get_performance_level(void * handle)383 static enum amd_dpm_forced_level pp_dpm_get_performance_level(
384 void *handle)
385 {
386 struct pp_hwmgr *hwmgr = handle;
387
388 if (!hwmgr || !hwmgr->pm_en)
389 return -EINVAL;
390
391 return hwmgr->dpm_level;
392 }
393
pp_dpm_get_sclk(void * handle,bool low)394 static uint32_t pp_dpm_get_sclk(void *handle, bool low)
395 {
396 struct pp_hwmgr *hwmgr = handle;
397
398 if (!hwmgr || !hwmgr->pm_en)
399 return 0;
400
401 if (hwmgr->hwmgr_func->get_sclk == NULL) {
402 pr_info_ratelimited("%s was not implemented.\n", __func__);
403 return 0;
404 }
405 return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
406 }
407
pp_dpm_get_mclk(void * handle,bool low)408 static uint32_t pp_dpm_get_mclk(void *handle, bool low)
409 {
410 struct pp_hwmgr *hwmgr = handle;
411
412 if (!hwmgr || !hwmgr->pm_en)
413 return 0;
414
415 if (hwmgr->hwmgr_func->get_mclk == NULL) {
416 pr_info_ratelimited("%s was not implemented.\n", __func__);
417 return 0;
418 }
419 return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
420 }
421
pp_dpm_powergate_vce(void * handle,bool gate)422 static void pp_dpm_powergate_vce(void *handle, bool gate)
423 {
424 struct pp_hwmgr *hwmgr = handle;
425
426 if (!hwmgr || !hwmgr->pm_en)
427 return;
428
429 if (hwmgr->hwmgr_func->powergate_vce == NULL) {
430 pr_info_ratelimited("%s was not implemented.\n", __func__);
431 return;
432 }
433 hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
434 }
435
pp_dpm_powergate_uvd(void * handle,bool gate)436 static void pp_dpm_powergate_uvd(void *handle, bool gate)
437 {
438 struct pp_hwmgr *hwmgr = handle;
439
440 if (!hwmgr || !hwmgr->pm_en)
441 return;
442
443 if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
444 pr_info_ratelimited("%s was not implemented.\n", __func__);
445 return;
446 }
447 hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
448 }
449
pp_dpm_dispatch_tasks(void * handle,enum amd_pp_task task_id,enum amd_pm_state_type * user_state)450 static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
451 enum amd_pm_state_type *user_state)
452 {
453 struct pp_hwmgr *hwmgr = handle;
454
455 if (!hwmgr || !hwmgr->pm_en)
456 return -EINVAL;
457
458 return hwmgr_handle_task(hwmgr, task_id, user_state);
459 }
460
pp_dpm_get_current_power_state(void * handle)461 static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
462 {
463 struct pp_hwmgr *hwmgr = handle;
464 struct pp_power_state *state;
465 enum amd_pm_state_type pm_type;
466
467 if (!hwmgr || !hwmgr->pm_en || !hwmgr->current_ps)
468 return -EINVAL;
469
470 state = hwmgr->current_ps;
471
472 switch (state->classification.ui_label) {
473 case PP_StateUILabel_Battery:
474 pm_type = POWER_STATE_TYPE_BATTERY;
475 break;
476 case PP_StateUILabel_Balanced:
477 pm_type = POWER_STATE_TYPE_BALANCED;
478 break;
479 case PP_StateUILabel_Performance:
480 pm_type = POWER_STATE_TYPE_PERFORMANCE;
481 break;
482 default:
483 if (state->classification.flags & PP_StateClassificationFlag_Boot)
484 pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
485 else
486 pm_type = POWER_STATE_TYPE_DEFAULT;
487 break;
488 }
489
490 return pm_type;
491 }
492
pp_dpm_set_fan_control_mode(void * handle,uint32_t mode)493 static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
494 {
495 struct pp_hwmgr *hwmgr = handle;
496
497 if (!hwmgr || !hwmgr->pm_en)
498 return -EOPNOTSUPP;
499
500 if (hwmgr->hwmgr_func->set_fan_control_mode == NULL)
501 return -EOPNOTSUPP;
502
503 if (mode == U32_MAX)
504 return -EINVAL;
505
506 hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
507
508 return 0;
509 }
510
pp_dpm_get_fan_control_mode(void * handle,uint32_t * fan_mode)511 static int pp_dpm_get_fan_control_mode(void *handle, uint32_t *fan_mode)
512 {
513 struct pp_hwmgr *hwmgr = handle;
514
515 if (!hwmgr || !hwmgr->pm_en)
516 return -EOPNOTSUPP;
517
518 if (hwmgr->hwmgr_func->get_fan_control_mode == NULL)
519 return -EOPNOTSUPP;
520
521 if (!fan_mode)
522 return -EINVAL;
523
524 *fan_mode = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
525 return 0;
526 }
527
pp_dpm_set_fan_speed_pwm(void * handle,uint32_t speed)528 static int pp_dpm_set_fan_speed_pwm(void *handle, uint32_t speed)
529 {
530 struct pp_hwmgr *hwmgr = handle;
531
532 if (!hwmgr || !hwmgr->pm_en)
533 return -EOPNOTSUPP;
534
535 if (hwmgr->hwmgr_func->set_fan_speed_pwm == NULL)
536 return -EOPNOTSUPP;
537
538 if (speed == U32_MAX)
539 return -EINVAL;
540
541 return hwmgr->hwmgr_func->set_fan_speed_pwm(hwmgr, speed);
542 }
543
pp_dpm_get_fan_speed_pwm(void * handle,uint32_t * speed)544 static int pp_dpm_get_fan_speed_pwm(void *handle, uint32_t *speed)
545 {
546 struct pp_hwmgr *hwmgr = handle;
547
548 if (!hwmgr || !hwmgr->pm_en)
549 return -EOPNOTSUPP;
550
551 if (hwmgr->hwmgr_func->get_fan_speed_pwm == NULL)
552 return -EOPNOTSUPP;
553
554 if (!speed)
555 return -EINVAL;
556
557 return hwmgr->hwmgr_func->get_fan_speed_pwm(hwmgr, speed);
558 }
559
pp_dpm_get_fan_speed_rpm(void * handle,uint32_t * rpm)560 static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
561 {
562 struct pp_hwmgr *hwmgr = handle;
563
564 if (!hwmgr || !hwmgr->pm_en)
565 return -EOPNOTSUPP;
566
567 if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
568 return -EOPNOTSUPP;
569
570 if (!rpm)
571 return -EINVAL;
572
573 return hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
574 }
575
pp_dpm_set_fan_speed_rpm(void * handle,uint32_t rpm)576 static int pp_dpm_set_fan_speed_rpm(void *handle, uint32_t rpm)
577 {
578 struct pp_hwmgr *hwmgr = handle;
579
580 if (!hwmgr || !hwmgr->pm_en)
581 return -EOPNOTSUPP;
582
583 if (hwmgr->hwmgr_func->set_fan_speed_rpm == NULL)
584 return -EOPNOTSUPP;
585
586 if (rpm == U32_MAX)
587 return -EINVAL;
588
589 return hwmgr->hwmgr_func->set_fan_speed_rpm(hwmgr, rpm);
590 }
591
pp_dpm_get_pp_num_states(void * handle,struct pp_states_info * data)592 static int pp_dpm_get_pp_num_states(void *handle,
593 struct pp_states_info *data)
594 {
595 struct pp_hwmgr *hwmgr = handle;
596 int i;
597
598 memset(data, 0, sizeof(*data));
599
600 if (!hwmgr || !hwmgr->pm_en || !hwmgr->ps)
601 return -EINVAL;
602
603 data->nums = hwmgr->num_ps;
604
605 for (i = 0; i < hwmgr->num_ps; i++) {
606 struct pp_power_state *state = (struct pp_power_state *)
607 ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
608 switch (state->classification.ui_label) {
609 case PP_StateUILabel_Battery:
610 data->states[i] = POWER_STATE_TYPE_BATTERY;
611 break;
612 case PP_StateUILabel_Balanced:
613 data->states[i] = POWER_STATE_TYPE_BALANCED;
614 break;
615 case PP_StateUILabel_Performance:
616 data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
617 break;
618 default:
619 if (state->classification.flags & PP_StateClassificationFlag_Boot)
620 data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
621 else
622 data->states[i] = POWER_STATE_TYPE_DEFAULT;
623 }
624 }
625 return 0;
626 }
627
pp_dpm_get_pp_table(void * handle,char ** table)628 static int pp_dpm_get_pp_table(void *handle, char **table)
629 {
630 struct pp_hwmgr *hwmgr = handle;
631
632 if (!hwmgr || !hwmgr->pm_en || !hwmgr->soft_pp_table)
633 return -EINVAL;
634
635 *table = (char *)hwmgr->soft_pp_table;
636 return hwmgr->soft_pp_table_size;
637 }
638
amd_powerplay_reset(void * handle)639 static int amd_powerplay_reset(void *handle)
640 {
641 struct pp_hwmgr *hwmgr = handle;
642 int ret;
643
644 ret = hwmgr_hw_fini(hwmgr);
645 if (ret)
646 return ret;
647
648 ret = hwmgr_hw_init(hwmgr);
649 if (ret)
650 return ret;
651
652 return hwmgr_handle_task(hwmgr, AMD_PP_TASK_COMPLETE_INIT, NULL);
653 }
654
pp_dpm_set_pp_table(void * handle,const char * buf,size_t size)655 static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
656 {
657 struct pp_hwmgr *hwmgr = handle;
658 int ret = -ENOMEM;
659
660 if (!hwmgr || !hwmgr->pm_en)
661 return -EINVAL;
662
663 if (!hwmgr->hardcode_pp_table) {
664 hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
665 hwmgr->soft_pp_table_size,
666 GFP_KERNEL);
667 if (!hwmgr->hardcode_pp_table)
668 return ret;
669 }
670
671 memcpy(hwmgr->hardcode_pp_table, buf, size);
672
673 hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
674
675 ret = amd_powerplay_reset(handle);
676 if (ret)
677 return ret;
678
679 if (hwmgr->hwmgr_func->avfs_control)
680 ret = hwmgr->hwmgr_func->avfs_control(hwmgr, false);
681
682 return ret;
683 }
684
pp_dpm_force_clock_level(void * handle,enum pp_clock_type type,uint32_t mask)685 static int pp_dpm_force_clock_level(void *handle,
686 enum pp_clock_type type, uint32_t mask)
687 {
688 struct pp_hwmgr *hwmgr = handle;
689
690 if (!hwmgr || !hwmgr->pm_en)
691 return -EINVAL;
692
693 if (hwmgr->hwmgr_func->force_clock_level == NULL) {
694 pr_info_ratelimited("%s was not implemented.\n", __func__);
695 return 0;
696 }
697
698 if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
699 pr_debug("force clock level is for dpm manual mode only.\n");
700 return -EINVAL;
701 }
702
703 return hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
704 }
705
pp_dpm_emit_clock_levels(void * handle,enum pp_clock_type type,char * buf,int * offset)706 static int pp_dpm_emit_clock_levels(void *handle,
707 enum pp_clock_type type,
708 char *buf,
709 int *offset)
710 {
711 struct pp_hwmgr *hwmgr = handle;
712
713 if (!hwmgr || !hwmgr->pm_en)
714 return -EOPNOTSUPP;
715
716 if (!hwmgr->hwmgr_func->emit_clock_levels)
717 return -ENOENT;
718
719 return hwmgr->hwmgr_func->emit_clock_levels(hwmgr, type, buf, offset);
720 }
721
pp_dpm_print_clock_levels(void * handle,enum pp_clock_type type,char * buf)722 static int pp_dpm_print_clock_levels(void *handle,
723 enum pp_clock_type type, char *buf)
724 {
725 struct pp_hwmgr *hwmgr = handle;
726
727 if (!hwmgr || !hwmgr->pm_en)
728 return -EINVAL;
729
730 if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
731 pr_info_ratelimited("%s was not implemented.\n", __func__);
732 return 0;
733 }
734 return hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
735 }
736
pp_dpm_get_sclk_od(void * handle)737 static int pp_dpm_get_sclk_od(void *handle)
738 {
739 struct pp_hwmgr *hwmgr = handle;
740
741 if (!hwmgr || !hwmgr->pm_en)
742 return -EINVAL;
743
744 if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
745 pr_info_ratelimited("%s was not implemented.\n", __func__);
746 return 0;
747 }
748 return hwmgr->hwmgr_func->get_sclk_od(hwmgr);
749 }
750
pp_dpm_set_sclk_od(void * handle,uint32_t value)751 static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
752 {
753 struct pp_hwmgr *hwmgr = handle;
754
755 if (!hwmgr || !hwmgr->pm_en)
756 return -EINVAL;
757
758 if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
759 pr_info_ratelimited("%s was not implemented.\n", __func__);
760 return 0;
761 }
762
763 return hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
764 }
765
pp_dpm_get_mclk_od(void * handle)766 static int pp_dpm_get_mclk_od(void *handle)
767 {
768 struct pp_hwmgr *hwmgr = handle;
769
770 if (!hwmgr || !hwmgr->pm_en)
771 return -EINVAL;
772
773 if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
774 pr_info_ratelimited("%s was not implemented.\n", __func__);
775 return 0;
776 }
777 return hwmgr->hwmgr_func->get_mclk_od(hwmgr);
778 }
779
pp_dpm_set_mclk_od(void * handle,uint32_t value)780 static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
781 {
782 struct pp_hwmgr *hwmgr = handle;
783
784 if (!hwmgr || !hwmgr->pm_en)
785 return -EINVAL;
786
787 if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
788 pr_info_ratelimited("%s was not implemented.\n", __func__);
789 return 0;
790 }
791 return hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
792 }
793
pp_dpm_read_sensor(void * handle,int idx,void * value,int * size)794 static int pp_dpm_read_sensor(void *handle, int idx,
795 void *value, int *size)
796 {
797 struct pp_hwmgr *hwmgr = handle;
798
799 if (!hwmgr || !hwmgr->pm_en || !value)
800 return -EINVAL;
801
802 switch (idx) {
803 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
804 *((uint32_t *)value) = hwmgr->pstate_sclk * 100;
805 return 0;
806 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
807 *((uint32_t *)value) = hwmgr->pstate_mclk * 100;
808 return 0;
809 case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK:
810 *((uint32_t *)value) = hwmgr->pstate_sclk_peak * 100;
811 return 0;
812 case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK:
813 *((uint32_t *)value) = hwmgr->pstate_mclk_peak * 100;
814 return 0;
815 case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
816 *((uint32_t *)value) = hwmgr->thermal_controller.fanInfo.ulMinRPM;
817 return 0;
818 case AMDGPU_PP_SENSOR_MAX_FAN_RPM:
819 *((uint32_t *)value) = hwmgr->thermal_controller.fanInfo.ulMaxRPM;
820 return 0;
821 default:
822 return hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
823 }
824 }
825
826 static struct amd_vce_state*
pp_dpm_get_vce_clock_state(void * handle,unsigned idx)827 pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
828 {
829 struct pp_hwmgr *hwmgr = handle;
830
831 if (!hwmgr || !hwmgr->pm_en)
832 return NULL;
833
834 if (idx < hwmgr->num_vce_state_tables)
835 return &hwmgr->vce_states[idx];
836 return NULL;
837 }
838
pp_get_power_profile_mode(void * handle,char * buf)839 static int pp_get_power_profile_mode(void *handle, char *buf)
840 {
841 struct pp_hwmgr *hwmgr = handle;
842
843 if (!hwmgr || !hwmgr->pm_en || !hwmgr->hwmgr_func->get_power_profile_mode)
844 return -EOPNOTSUPP;
845 if (!buf)
846 return -EINVAL;
847
848 return hwmgr->hwmgr_func->get_power_profile_mode(hwmgr, buf);
849 }
850
pp_set_power_profile_mode(void * handle,long * input,uint32_t size)851 static int pp_set_power_profile_mode(void *handle, long *input, uint32_t size)
852 {
853 struct pp_hwmgr *hwmgr = handle;
854
855 if (!hwmgr || !hwmgr->pm_en || !hwmgr->hwmgr_func->set_power_profile_mode)
856 return -EOPNOTSUPP;
857
858 if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
859 pr_debug("power profile setting is for manual dpm mode only.\n");
860 return -EINVAL;
861 }
862
863 return hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, input, size);
864 }
865
pp_set_fine_grain_clk_vol(void * handle,uint32_t type,long * input,uint32_t size)866 static int pp_set_fine_grain_clk_vol(void *handle, uint32_t type, long *input, uint32_t size)
867 {
868 struct pp_hwmgr *hwmgr = handle;
869
870 if (!hwmgr || !hwmgr->pm_en)
871 return -EINVAL;
872
873 if (hwmgr->hwmgr_func->set_fine_grain_clk_vol == NULL)
874 return 0;
875
876 return hwmgr->hwmgr_func->set_fine_grain_clk_vol(hwmgr, type, input, size);
877 }
878
pp_odn_edit_dpm_table(void * handle,enum PP_OD_DPM_TABLE_COMMAND type,long * input,uint32_t size)879 static int pp_odn_edit_dpm_table(void *handle, enum PP_OD_DPM_TABLE_COMMAND type,
880 long *input, uint32_t size)
881 {
882 struct pp_hwmgr *hwmgr = handle;
883
884 if (!hwmgr || !hwmgr->pm_en)
885 return -EINVAL;
886
887 if (hwmgr->hwmgr_func->odn_edit_dpm_table == NULL) {
888 pr_info_ratelimited("%s was not implemented.\n", __func__);
889 return 0;
890 }
891
892 return hwmgr->hwmgr_func->odn_edit_dpm_table(hwmgr, type, input, size);
893 }
894
pp_dpm_set_mp1_state(void * handle,enum pp_mp1_state mp1_state)895 static int pp_dpm_set_mp1_state(void *handle, enum pp_mp1_state mp1_state)
896 {
897 struct pp_hwmgr *hwmgr = handle;
898
899 if (!hwmgr)
900 return -EINVAL;
901
902 if (!hwmgr->pm_en)
903 return 0;
904
905 if (hwmgr->hwmgr_func->set_mp1_state)
906 return hwmgr->hwmgr_func->set_mp1_state(hwmgr, mp1_state);
907
908 return 0;
909 }
910
pp_dpm_switch_power_profile(void * handle,enum PP_SMC_POWER_PROFILE type,bool en)911 static int pp_dpm_switch_power_profile(void *handle,
912 enum PP_SMC_POWER_PROFILE type, bool en)
913 {
914 struct pp_hwmgr *hwmgr = handle;
915 long workload[1];
916 uint32_t index;
917
918 if (!hwmgr || !hwmgr->pm_en)
919 return -EINVAL;
920
921 if (hwmgr->hwmgr_func->set_power_profile_mode == NULL) {
922 pr_info_ratelimited("%s was not implemented.\n", __func__);
923 return -EINVAL;
924 }
925
926 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
927 return -EINVAL;
928
929 if (!en) {
930 hwmgr->workload_mask &= ~(1 << hwmgr->workload_prority[type]);
931 index = fls(hwmgr->workload_mask);
932 index = index > 0 && index <= Workload_Policy_Max ? index - 1 : 0;
933 workload[0] = hwmgr->workload_setting[index];
934 } else {
935 hwmgr->workload_mask |= (1 << hwmgr->workload_prority[type]);
936 index = fls(hwmgr->workload_mask);
937 index = index <= Workload_Policy_Max ? index - 1 : 0;
938 workload[0] = hwmgr->workload_setting[index];
939 }
940
941 if (type == PP_SMC_POWER_PROFILE_COMPUTE &&
942 hwmgr->hwmgr_func->disable_power_features_for_compute_performance) {
943 if (hwmgr->hwmgr_func->disable_power_features_for_compute_performance(hwmgr, en))
944 return -EINVAL;
945 }
946
947 if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
948 hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, workload, 0);
949
950 return 0;
951 }
952
pp_set_power_limit(void * handle,uint32_t limit)953 static int pp_set_power_limit(void *handle, uint32_t limit)
954 {
955 struct pp_hwmgr *hwmgr = handle;
956 uint32_t max_power_limit;
957
958 if (!hwmgr || !hwmgr->pm_en)
959 return -EINVAL;
960
961 if (hwmgr->hwmgr_func->set_power_limit == NULL) {
962 pr_info_ratelimited("%s was not implemented.\n", __func__);
963 return -EINVAL;
964 }
965
966 if (limit == 0)
967 limit = hwmgr->default_power_limit;
968
969 max_power_limit = hwmgr->default_power_limit;
970 if (hwmgr->od_enabled) {
971 max_power_limit *= (100 + hwmgr->platform_descriptor.TDPODLimit);
972 max_power_limit /= 100;
973 }
974
975 if (limit > max_power_limit)
976 return -EINVAL;
977
978 hwmgr->hwmgr_func->set_power_limit(hwmgr, limit);
979 hwmgr->power_limit = limit;
980 return 0;
981 }
982
pp_get_power_limit(void * handle,uint32_t * limit,enum pp_power_limit_level pp_limit_level,enum pp_power_type power_type)983 static int pp_get_power_limit(void *handle, uint32_t *limit,
984 enum pp_power_limit_level pp_limit_level,
985 enum pp_power_type power_type)
986 {
987 struct pp_hwmgr *hwmgr = handle;
988 int ret = 0;
989
990 if (!hwmgr || !hwmgr->pm_en || !limit)
991 return -EINVAL;
992
993 if (power_type != PP_PWR_TYPE_SUSTAINED)
994 return -EOPNOTSUPP;
995
996 switch (pp_limit_level) {
997 case PP_PWR_LIMIT_CURRENT:
998 *limit = hwmgr->power_limit;
999 break;
1000 case PP_PWR_LIMIT_DEFAULT:
1001 *limit = hwmgr->default_power_limit;
1002 break;
1003 case PP_PWR_LIMIT_MAX:
1004 *limit = hwmgr->default_power_limit;
1005 if (hwmgr->od_enabled) {
1006 *limit *= (100 + hwmgr->platform_descriptor.TDPODLimit);
1007 *limit /= 100;
1008 }
1009 break;
1010 case PP_PWR_LIMIT_MIN:
1011 *limit = 0;
1012 break;
1013 default:
1014 ret = -EOPNOTSUPP;
1015 break;
1016 }
1017
1018 return ret;
1019 }
1020
pp_display_configuration_change(void * handle,const struct amd_pp_display_configuration * display_config)1021 static int pp_display_configuration_change(void *handle,
1022 const struct amd_pp_display_configuration *display_config)
1023 {
1024 struct pp_hwmgr *hwmgr = handle;
1025
1026 if (!hwmgr || !hwmgr->pm_en)
1027 return -EINVAL;
1028
1029 phm_store_dal_configuration_data(hwmgr, display_config);
1030 return 0;
1031 }
1032
pp_get_display_power_level(void * handle,struct amd_pp_simple_clock_info * output)1033 static int pp_get_display_power_level(void *handle,
1034 struct amd_pp_simple_clock_info *output)
1035 {
1036 struct pp_hwmgr *hwmgr = handle;
1037
1038 if (!hwmgr || !hwmgr->pm_en || !output)
1039 return -EINVAL;
1040
1041 return phm_get_dal_power_level(hwmgr, output);
1042 }
1043
pp_get_current_clocks(void * handle,struct amd_pp_clock_info * clocks)1044 static int pp_get_current_clocks(void *handle,
1045 struct amd_pp_clock_info *clocks)
1046 {
1047 struct amd_pp_simple_clock_info simple_clocks = { 0 };
1048 struct pp_clock_info hw_clocks;
1049 struct pp_hwmgr *hwmgr = handle;
1050 int ret = 0;
1051
1052 if (!hwmgr || !hwmgr->pm_en)
1053 return -EINVAL;
1054
1055 phm_get_dal_power_level(hwmgr, &simple_clocks);
1056
1057 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1058 PHM_PlatformCaps_PowerContainment))
1059 ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
1060 &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment);
1061 else
1062 ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
1063 &hw_clocks, PHM_PerformanceLevelDesignation_Activity);
1064
1065 if (ret) {
1066 pr_debug("Error in phm_get_clock_info \n");
1067 return -EINVAL;
1068 }
1069
1070 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1071 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1072 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1073 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1074 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1075 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1076
1077 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1078 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1079
1080 if (simple_clocks.level == 0)
1081 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1082 else
1083 clocks->max_clocks_state = simple_clocks.level;
1084
1085 if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
1086 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1087 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1088 }
1089 return 0;
1090 }
1091
pp_get_clock_by_type(void * handle,enum amd_pp_clock_type type,struct amd_pp_clocks * clocks)1092 static int pp_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
1093 {
1094 struct pp_hwmgr *hwmgr = handle;
1095
1096 if (!hwmgr || !hwmgr->pm_en)
1097 return -EINVAL;
1098
1099 if (clocks == NULL)
1100 return -EINVAL;
1101
1102 return phm_get_clock_by_type(hwmgr, type, clocks);
1103 }
1104
pp_get_clock_by_type_with_latency(void * handle,enum amd_pp_clock_type type,struct pp_clock_levels_with_latency * clocks)1105 static int pp_get_clock_by_type_with_latency(void *handle,
1106 enum amd_pp_clock_type type,
1107 struct pp_clock_levels_with_latency *clocks)
1108 {
1109 struct pp_hwmgr *hwmgr = handle;
1110
1111 if (!hwmgr || !hwmgr->pm_en || !clocks)
1112 return -EINVAL;
1113
1114 return phm_get_clock_by_type_with_latency(hwmgr, type, clocks);
1115 }
1116
pp_get_clock_by_type_with_voltage(void * handle,enum amd_pp_clock_type type,struct pp_clock_levels_with_voltage * clocks)1117 static int pp_get_clock_by_type_with_voltage(void *handle,
1118 enum amd_pp_clock_type type,
1119 struct pp_clock_levels_with_voltage *clocks)
1120 {
1121 struct pp_hwmgr *hwmgr = handle;
1122
1123 if (!hwmgr || !hwmgr->pm_en || !clocks)
1124 return -EINVAL;
1125
1126 return phm_get_clock_by_type_with_voltage(hwmgr, type, clocks);
1127 }
1128
pp_set_watermarks_for_clocks_ranges(void * handle,void * clock_ranges)1129 static int pp_set_watermarks_for_clocks_ranges(void *handle,
1130 void *clock_ranges)
1131 {
1132 struct pp_hwmgr *hwmgr = handle;
1133
1134 if (!hwmgr || !hwmgr->pm_en || !clock_ranges)
1135 return -EINVAL;
1136
1137 return phm_set_watermarks_for_clocks_ranges(hwmgr,
1138 clock_ranges);
1139 }
1140
pp_display_clock_voltage_request(void * handle,struct pp_display_clock_request * clock)1141 static int pp_display_clock_voltage_request(void *handle,
1142 struct pp_display_clock_request *clock)
1143 {
1144 struct pp_hwmgr *hwmgr = handle;
1145
1146 if (!hwmgr || !hwmgr->pm_en || !clock)
1147 return -EINVAL;
1148
1149 return phm_display_clock_voltage_request(hwmgr, clock);
1150 }
1151
pp_get_display_mode_validation_clocks(void * handle,struct amd_pp_simple_clock_info * clocks)1152 static int pp_get_display_mode_validation_clocks(void *handle,
1153 struct amd_pp_simple_clock_info *clocks)
1154 {
1155 struct pp_hwmgr *hwmgr = handle;
1156 int ret = 0;
1157
1158 if (!hwmgr || !hwmgr->pm_en || !clocks)
1159 return -EINVAL;
1160
1161 clocks->level = PP_DAL_POWERLEVEL_7;
1162
1163 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
1164 ret = phm_get_max_high_clocks(hwmgr, clocks);
1165
1166 return ret;
1167 }
1168
pp_dpm_powergate_mmhub(void * handle)1169 static int pp_dpm_powergate_mmhub(void *handle)
1170 {
1171 struct pp_hwmgr *hwmgr = handle;
1172
1173 if (!hwmgr || !hwmgr->pm_en)
1174 return -EINVAL;
1175
1176 if (hwmgr->hwmgr_func->powergate_mmhub == NULL) {
1177 pr_info_ratelimited("%s was not implemented.\n", __func__);
1178 return 0;
1179 }
1180
1181 return hwmgr->hwmgr_func->powergate_mmhub(hwmgr);
1182 }
1183
pp_dpm_powergate_gfx(void * handle,bool gate)1184 static int pp_dpm_powergate_gfx(void *handle, bool gate)
1185 {
1186 struct pp_hwmgr *hwmgr = handle;
1187
1188 if (!hwmgr || !hwmgr->pm_en)
1189 return 0;
1190
1191 if (hwmgr->hwmgr_func->powergate_gfx == NULL) {
1192 pr_info_ratelimited("%s was not implemented.\n", __func__);
1193 return 0;
1194 }
1195
1196 return hwmgr->hwmgr_func->powergate_gfx(hwmgr, gate);
1197 }
1198
pp_dpm_powergate_acp(void * handle,bool gate)1199 static void pp_dpm_powergate_acp(void *handle, bool gate)
1200 {
1201 struct pp_hwmgr *hwmgr = handle;
1202
1203 if (!hwmgr || !hwmgr->pm_en)
1204 return;
1205
1206 if (hwmgr->hwmgr_func->powergate_acp == NULL) {
1207 pr_info_ratelimited("%s was not implemented.\n", __func__);
1208 return;
1209 }
1210
1211 hwmgr->hwmgr_func->powergate_acp(hwmgr, gate);
1212 }
1213
pp_dpm_powergate_sdma(void * handle,bool gate)1214 static void pp_dpm_powergate_sdma(void *handle, bool gate)
1215 {
1216 struct pp_hwmgr *hwmgr = handle;
1217
1218 if (!hwmgr)
1219 return;
1220
1221 if (hwmgr->hwmgr_func->powergate_sdma == NULL) {
1222 pr_info_ratelimited("%s was not implemented.\n", __func__);
1223 return;
1224 }
1225
1226 hwmgr->hwmgr_func->powergate_sdma(hwmgr, gate);
1227 }
1228
pp_set_powergating_by_smu(void * handle,uint32_t block_type,bool gate,int inst)1229 static int pp_set_powergating_by_smu(void *handle,
1230 uint32_t block_type,
1231 bool gate,
1232 int inst)
1233 {
1234 int ret = 0;
1235
1236 switch (block_type) {
1237 case AMD_IP_BLOCK_TYPE_UVD:
1238 case AMD_IP_BLOCK_TYPE_VCN:
1239 pp_dpm_powergate_uvd(handle, gate);
1240 break;
1241 case AMD_IP_BLOCK_TYPE_VCE:
1242 pp_dpm_powergate_vce(handle, gate);
1243 break;
1244 case AMD_IP_BLOCK_TYPE_GMC:
1245 /*
1246 * For now, this is only used on PICASSO.
1247 * And only "gate" operation is supported.
1248 */
1249 if (gate)
1250 pp_dpm_powergate_mmhub(handle);
1251 break;
1252 case AMD_IP_BLOCK_TYPE_GFX:
1253 ret = pp_dpm_powergate_gfx(handle, gate);
1254 break;
1255 case AMD_IP_BLOCK_TYPE_ACP:
1256 pp_dpm_powergate_acp(handle, gate);
1257 break;
1258 case AMD_IP_BLOCK_TYPE_SDMA:
1259 pp_dpm_powergate_sdma(handle, gate);
1260 break;
1261 default:
1262 break;
1263 }
1264 return ret;
1265 }
1266
pp_notify_smu_enable_pwe(void * handle)1267 static int pp_notify_smu_enable_pwe(void *handle)
1268 {
1269 struct pp_hwmgr *hwmgr = handle;
1270
1271 if (!hwmgr || !hwmgr->pm_en)
1272 return -EINVAL;
1273
1274 if (hwmgr->hwmgr_func->smus_notify_pwe == NULL) {
1275 pr_info_ratelimited("%s was not implemented.\n", __func__);
1276 return -EINVAL;
1277 }
1278
1279 hwmgr->hwmgr_func->smus_notify_pwe(hwmgr);
1280
1281 return 0;
1282 }
1283
pp_enable_mgpu_fan_boost(void * handle)1284 static int pp_enable_mgpu_fan_boost(void *handle)
1285 {
1286 struct pp_hwmgr *hwmgr = handle;
1287
1288 if (!hwmgr)
1289 return -EINVAL;
1290
1291 if (!hwmgr->pm_en ||
1292 hwmgr->hwmgr_func->enable_mgpu_fan_boost == NULL)
1293 return 0;
1294
1295 hwmgr->hwmgr_func->enable_mgpu_fan_boost(hwmgr);
1296
1297 return 0;
1298 }
1299
pp_set_min_deep_sleep_dcefclk(void * handle,uint32_t clock)1300 static int pp_set_min_deep_sleep_dcefclk(void *handle, uint32_t clock)
1301 {
1302 struct pp_hwmgr *hwmgr = handle;
1303
1304 if (!hwmgr || !hwmgr->pm_en)
1305 return -EINVAL;
1306
1307 if (hwmgr->hwmgr_func->set_min_deep_sleep_dcefclk == NULL) {
1308 pr_debug("%s was not implemented.\n", __func__);
1309 return -EINVAL;
1310 }
1311
1312 hwmgr->hwmgr_func->set_min_deep_sleep_dcefclk(hwmgr, clock);
1313
1314 return 0;
1315 }
1316
pp_set_hard_min_dcefclk_by_freq(void * handle,uint32_t clock)1317 static int pp_set_hard_min_dcefclk_by_freq(void *handle, uint32_t clock)
1318 {
1319 struct pp_hwmgr *hwmgr = handle;
1320
1321 if (!hwmgr || !hwmgr->pm_en)
1322 return -EINVAL;
1323
1324 if (hwmgr->hwmgr_func->set_hard_min_dcefclk_by_freq == NULL) {
1325 pr_debug("%s was not implemented.\n", __func__);
1326 return -EINVAL;
1327 }
1328
1329 hwmgr->hwmgr_func->set_hard_min_dcefclk_by_freq(hwmgr, clock);
1330
1331 return 0;
1332 }
1333
pp_set_hard_min_fclk_by_freq(void * handle,uint32_t clock)1334 static int pp_set_hard_min_fclk_by_freq(void *handle, uint32_t clock)
1335 {
1336 struct pp_hwmgr *hwmgr = handle;
1337
1338 if (!hwmgr || !hwmgr->pm_en)
1339 return -EINVAL;
1340
1341 if (hwmgr->hwmgr_func->set_hard_min_fclk_by_freq == NULL) {
1342 pr_debug("%s was not implemented.\n", __func__);
1343 return -EINVAL;
1344 }
1345
1346 hwmgr->hwmgr_func->set_hard_min_fclk_by_freq(hwmgr, clock);
1347
1348 return 0;
1349 }
1350
pp_set_active_display_count(void * handle,uint32_t count)1351 static int pp_set_active_display_count(void *handle, uint32_t count)
1352 {
1353 struct pp_hwmgr *hwmgr = handle;
1354
1355 if (!hwmgr || !hwmgr->pm_en)
1356 return -EINVAL;
1357
1358 return phm_set_active_display_count(hwmgr, count);
1359 }
1360
pp_get_asic_baco_capability(void * handle)1361 static int pp_get_asic_baco_capability(void *handle)
1362 {
1363 struct pp_hwmgr *hwmgr = handle;
1364
1365 if (!hwmgr)
1366 return false;
1367
1368 if (!(hwmgr->not_vf && amdgpu_dpm) ||
1369 !hwmgr->hwmgr_func->get_bamaco_support)
1370 return false;
1371
1372 return hwmgr->hwmgr_func->get_bamaco_support(hwmgr);
1373 }
1374
pp_get_asic_baco_state(void * handle,int * state)1375 static int pp_get_asic_baco_state(void *handle, int *state)
1376 {
1377 struct pp_hwmgr *hwmgr = handle;
1378
1379 if (!hwmgr)
1380 return -EINVAL;
1381
1382 if (!hwmgr->pm_en || !hwmgr->hwmgr_func->get_asic_baco_state)
1383 return 0;
1384
1385 hwmgr->hwmgr_func->get_asic_baco_state(hwmgr, (enum BACO_STATE *)state);
1386
1387 return 0;
1388 }
1389
pp_set_asic_baco_state(void * handle,int state)1390 static int pp_set_asic_baco_state(void *handle, int state)
1391 {
1392 struct pp_hwmgr *hwmgr = handle;
1393
1394 if (!hwmgr)
1395 return -EINVAL;
1396
1397 if (!(hwmgr->not_vf && amdgpu_dpm) ||
1398 !hwmgr->hwmgr_func->set_asic_baco_state)
1399 return 0;
1400
1401 hwmgr->hwmgr_func->set_asic_baco_state(hwmgr, (enum BACO_STATE)state);
1402
1403 return 0;
1404 }
1405
pp_get_ppfeature_status(void * handle,char * buf)1406 static int pp_get_ppfeature_status(void *handle, char *buf)
1407 {
1408 struct pp_hwmgr *hwmgr = handle;
1409
1410 if (!hwmgr || !hwmgr->pm_en || !buf)
1411 return -EINVAL;
1412
1413 if (hwmgr->hwmgr_func->get_ppfeature_status == NULL) {
1414 pr_info_ratelimited("%s was not implemented.\n", __func__);
1415 return -EINVAL;
1416 }
1417
1418 return hwmgr->hwmgr_func->get_ppfeature_status(hwmgr, buf);
1419 }
1420
pp_set_ppfeature_status(void * handle,uint64_t ppfeature_masks)1421 static int pp_set_ppfeature_status(void *handle, uint64_t ppfeature_masks)
1422 {
1423 struct pp_hwmgr *hwmgr = handle;
1424
1425 if (!hwmgr || !hwmgr->pm_en)
1426 return -EINVAL;
1427
1428 if (hwmgr->hwmgr_func->set_ppfeature_status == NULL) {
1429 pr_info_ratelimited("%s was not implemented.\n", __func__);
1430 return -EINVAL;
1431 }
1432
1433 return hwmgr->hwmgr_func->set_ppfeature_status(hwmgr, ppfeature_masks);
1434 }
1435
pp_asic_reset_mode_2(void * handle)1436 static int pp_asic_reset_mode_2(void *handle)
1437 {
1438 struct pp_hwmgr *hwmgr = handle;
1439
1440 if (!hwmgr || !hwmgr->pm_en)
1441 return -EINVAL;
1442
1443 if (hwmgr->hwmgr_func->asic_reset == NULL) {
1444 pr_info_ratelimited("%s was not implemented.\n", __func__);
1445 return -EINVAL;
1446 }
1447
1448 return hwmgr->hwmgr_func->asic_reset(hwmgr, SMU_ASIC_RESET_MODE_2);
1449 }
1450
pp_smu_i2c_bus_access(void * handle,bool acquire)1451 static int pp_smu_i2c_bus_access(void *handle, bool acquire)
1452 {
1453 struct pp_hwmgr *hwmgr = handle;
1454
1455 if (!hwmgr || !hwmgr->pm_en)
1456 return -EINVAL;
1457
1458 if (hwmgr->hwmgr_func->smu_i2c_bus_access == NULL) {
1459 pr_info_ratelimited("%s was not implemented.\n", __func__);
1460 return -EINVAL;
1461 }
1462
1463 return hwmgr->hwmgr_func->smu_i2c_bus_access(hwmgr, acquire);
1464 }
1465
pp_set_df_cstate(void * handle,enum pp_df_cstate state)1466 static int pp_set_df_cstate(void *handle, enum pp_df_cstate state)
1467 {
1468 struct pp_hwmgr *hwmgr = handle;
1469
1470 if (!hwmgr)
1471 return -EINVAL;
1472
1473 if (!hwmgr->pm_en || !hwmgr->hwmgr_func->set_df_cstate)
1474 return 0;
1475
1476 hwmgr->hwmgr_func->set_df_cstate(hwmgr, state);
1477
1478 return 0;
1479 }
1480
pp_set_xgmi_pstate(void * handle,uint32_t pstate)1481 static int pp_set_xgmi_pstate(void *handle, uint32_t pstate)
1482 {
1483 struct pp_hwmgr *hwmgr = handle;
1484
1485 if (!hwmgr)
1486 return -EINVAL;
1487
1488 if (!hwmgr->pm_en || !hwmgr->hwmgr_func->set_xgmi_pstate)
1489 return 0;
1490
1491 hwmgr->hwmgr_func->set_xgmi_pstate(hwmgr, pstate);
1492
1493 return 0;
1494 }
1495
pp_get_gpu_metrics(void * handle,void ** table)1496 static ssize_t pp_get_gpu_metrics(void *handle, void **table)
1497 {
1498 struct pp_hwmgr *hwmgr = handle;
1499
1500 if (!hwmgr)
1501 return -EINVAL;
1502
1503 if (!hwmgr->pm_en || !hwmgr->hwmgr_func->get_gpu_metrics)
1504 return -EOPNOTSUPP;
1505
1506 return hwmgr->hwmgr_func->get_gpu_metrics(hwmgr, table);
1507 }
1508
pp_gfx_state_change_set(void * handle,uint32_t state)1509 static int pp_gfx_state_change_set(void *handle, uint32_t state)
1510 {
1511 struct pp_hwmgr *hwmgr = handle;
1512
1513 if (!hwmgr || !hwmgr->pm_en)
1514 return -EINVAL;
1515
1516 if (hwmgr->hwmgr_func->gfx_state_change == NULL) {
1517 pr_info_ratelimited("%s was not implemented.\n", __func__);
1518 return -EINVAL;
1519 }
1520
1521 hwmgr->hwmgr_func->gfx_state_change(hwmgr, state);
1522 return 0;
1523 }
1524
pp_get_prv_buffer_details(void * handle,void ** addr,size_t * size)1525 static int pp_get_prv_buffer_details(void *handle, void **addr, size_t *size)
1526 {
1527 struct pp_hwmgr *hwmgr = handle;
1528 struct amdgpu_device *adev = hwmgr->adev;
1529 int err;
1530
1531 if (!addr || !size)
1532 return -EINVAL;
1533
1534 *addr = NULL;
1535 *size = 0;
1536 if (adev->pm.smu_prv_buffer) {
1537 err = amdgpu_bo_kmap(adev->pm.smu_prv_buffer, addr);
1538 if (err)
1539 return err;
1540 *size = adev->pm.smu_prv_buffer_size;
1541 }
1542
1543 return 0;
1544 }
1545
pp_pm_compute_clocks(void * handle)1546 static void pp_pm_compute_clocks(void *handle)
1547 {
1548 struct pp_hwmgr *hwmgr = handle;
1549 struct amdgpu_device *adev = hwmgr->adev;
1550
1551 if (!adev->dc_enabled) {
1552 amdgpu_dpm_get_active_displays(adev);
1553 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
1554 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
1555 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
1556 /* we have issues with mclk switching with
1557 * refresh rates over 120 hz on the non-DC code.
1558 */
1559 if (adev->pm.pm_display_cfg.vrefresh > 120)
1560 adev->pm.pm_display_cfg.min_vblank_time = 0;
1561
1562 pp_display_configuration_change(handle,
1563 &adev->pm.pm_display_cfg);
1564 }
1565
1566 pp_dpm_dispatch_tasks(handle,
1567 AMD_PP_TASK_DISPLAY_CONFIG_CHANGE,
1568 NULL);
1569 }
1570
1571 static const struct amd_pm_funcs pp_dpm_funcs = {
1572 .load_firmware = pp_dpm_load_fw,
1573 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
1574 .force_performance_level = pp_dpm_force_performance_level,
1575 .get_performance_level = pp_dpm_get_performance_level,
1576 .get_current_power_state = pp_dpm_get_current_power_state,
1577 .dispatch_tasks = pp_dpm_dispatch_tasks,
1578 .set_fan_control_mode = pp_dpm_set_fan_control_mode,
1579 .get_fan_control_mode = pp_dpm_get_fan_control_mode,
1580 .set_fan_speed_pwm = pp_dpm_set_fan_speed_pwm,
1581 .get_fan_speed_pwm = pp_dpm_get_fan_speed_pwm,
1582 .get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
1583 .set_fan_speed_rpm = pp_dpm_set_fan_speed_rpm,
1584 .get_pp_num_states = pp_dpm_get_pp_num_states,
1585 .get_pp_table = pp_dpm_get_pp_table,
1586 .set_pp_table = pp_dpm_set_pp_table,
1587 .force_clock_level = pp_dpm_force_clock_level,
1588 .emit_clock_levels = pp_dpm_emit_clock_levels,
1589 .print_clock_levels = pp_dpm_print_clock_levels,
1590 .get_sclk_od = pp_dpm_get_sclk_od,
1591 .set_sclk_od = pp_dpm_set_sclk_od,
1592 .get_mclk_od = pp_dpm_get_mclk_od,
1593 .set_mclk_od = pp_dpm_set_mclk_od,
1594 .read_sensor = pp_dpm_read_sensor,
1595 .get_vce_clock_state = pp_dpm_get_vce_clock_state,
1596 .switch_power_profile = pp_dpm_switch_power_profile,
1597 .set_clockgating_by_smu = pp_set_clockgating_by_smu,
1598 .set_powergating_by_smu = pp_set_powergating_by_smu,
1599 .get_power_profile_mode = pp_get_power_profile_mode,
1600 .set_power_profile_mode = pp_set_power_profile_mode,
1601 .set_fine_grain_clk_vol = pp_set_fine_grain_clk_vol,
1602 .odn_edit_dpm_table = pp_odn_edit_dpm_table,
1603 .set_mp1_state = pp_dpm_set_mp1_state,
1604 .set_power_limit = pp_set_power_limit,
1605 .get_power_limit = pp_get_power_limit,
1606 /* export to DC */
1607 .get_sclk = pp_dpm_get_sclk,
1608 .get_mclk = pp_dpm_get_mclk,
1609 .display_configuration_change = pp_display_configuration_change,
1610 .get_display_power_level = pp_get_display_power_level,
1611 .get_current_clocks = pp_get_current_clocks,
1612 .get_clock_by_type = pp_get_clock_by_type,
1613 .get_clock_by_type_with_latency = pp_get_clock_by_type_with_latency,
1614 .get_clock_by_type_with_voltage = pp_get_clock_by_type_with_voltage,
1615 .set_watermarks_for_clocks_ranges = pp_set_watermarks_for_clocks_ranges,
1616 .display_clock_voltage_request = pp_display_clock_voltage_request,
1617 .get_display_mode_validation_clocks = pp_get_display_mode_validation_clocks,
1618 .notify_smu_enable_pwe = pp_notify_smu_enable_pwe,
1619 .enable_mgpu_fan_boost = pp_enable_mgpu_fan_boost,
1620 .set_active_display_count = pp_set_active_display_count,
1621 .set_min_deep_sleep_dcefclk = pp_set_min_deep_sleep_dcefclk,
1622 .set_hard_min_dcefclk_by_freq = pp_set_hard_min_dcefclk_by_freq,
1623 .set_hard_min_fclk_by_freq = pp_set_hard_min_fclk_by_freq,
1624 .get_asic_baco_capability = pp_get_asic_baco_capability,
1625 .get_asic_baco_state = pp_get_asic_baco_state,
1626 .set_asic_baco_state = pp_set_asic_baco_state,
1627 .get_ppfeature_status = pp_get_ppfeature_status,
1628 .set_ppfeature_status = pp_set_ppfeature_status,
1629 .asic_reset_mode_2 = pp_asic_reset_mode_2,
1630 .smu_i2c_bus_access = pp_smu_i2c_bus_access,
1631 .set_df_cstate = pp_set_df_cstate,
1632 .set_xgmi_pstate = pp_set_xgmi_pstate,
1633 .get_gpu_metrics = pp_get_gpu_metrics,
1634 .gfx_state_change_set = pp_gfx_state_change_set,
1635 .get_smu_prv_buf_details = pp_get_prv_buffer_details,
1636 .pm_compute_clocks = pp_pm_compute_clocks,
1637 };
1638