amd_powerplay.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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 "amd_shared.h"
  30. #include "amd_powerplay.h"
  31. #include "power_state.h"
  32. #include "amdgpu.h"
  33. #include "hwmgr.h"
  34. static const struct amd_pm_funcs pp_dpm_funcs;
  35. static int amd_powerplay_create(struct amdgpu_device *adev)
  36. {
  37. struct pp_hwmgr *hwmgr;
  38. if (adev == NULL)
  39. return -EINVAL;
  40. hwmgr = kzalloc(sizeof(struct pp_hwmgr), GFP_KERNEL);
  41. if (hwmgr == NULL)
  42. return -ENOMEM;
  43. hwmgr->adev = adev;
  44. hwmgr->not_vf = !amdgpu_sriov_vf(adev);
  45. hwmgr->pm_en = (amdgpu_dpm && hwmgr->not_vf) ? true : false;
  46. hwmgr->device = amdgpu_cgs_create_device(adev);
  47. mutex_init(&hwmgr->smu_lock);
  48. hwmgr->chip_family = adev->family;
  49. hwmgr->chip_id = adev->asic_type;
  50. hwmgr->feature_mask = adev->powerplay.pp_feature;
  51. hwmgr->display_config = &adev->pm.pm_display_cfg;
  52. adev->powerplay.pp_handle = hwmgr;
  53. adev->powerplay.pp_funcs = &pp_dpm_funcs;
  54. return 0;
  55. }
  56. static void amd_powerplay_destroy(struct amdgpu_device *adev)
  57. {
  58. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  59. kfree(hwmgr->hardcode_pp_table);
  60. hwmgr->hardcode_pp_table = NULL;
  61. kfree(hwmgr);
  62. hwmgr = NULL;
  63. }
  64. static int pp_early_init(void *handle)
  65. {
  66. int ret;
  67. struct amdgpu_device *adev = handle;
  68. ret = amd_powerplay_create(adev);
  69. if (ret != 0)
  70. return ret;
  71. ret = hwmgr_early_init(adev->powerplay.pp_handle);
  72. if (ret)
  73. return -EINVAL;
  74. return 0;
  75. }
  76. static int pp_sw_init(void *handle)
  77. {
  78. struct amdgpu_device *adev = handle;
  79. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  80. int ret = 0;
  81. ret = hwmgr_sw_init(hwmgr);
  82. pr_debug("powerplay sw init %s\n", ret ? "failed" : "successfully");
  83. return ret;
  84. }
  85. static int pp_sw_fini(void *handle)
  86. {
  87. struct amdgpu_device *adev = handle;
  88. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  89. hwmgr_sw_fini(hwmgr);
  90. release_firmware(adev->pm.fw);
  91. adev->pm.fw = NULL;
  92. return 0;
  93. }
  94. static int pp_hw_init(void *handle)
  95. {
  96. int ret = 0;
  97. struct amdgpu_device *adev = handle;
  98. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  99. ret = hwmgr_hw_init(hwmgr);
  100. if (ret)
  101. pr_err("powerplay hw init failed\n");
  102. return ret;
  103. }
  104. static int pp_hw_fini(void *handle)
  105. {
  106. struct amdgpu_device *adev = handle;
  107. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  108. hwmgr_hw_fini(hwmgr);
  109. return 0;
  110. }
  111. static void pp_reserve_vram_for_smu(struct amdgpu_device *adev)
  112. {
  113. int r = -EINVAL;
  114. void *cpu_ptr = NULL;
  115. uint64_t gpu_addr;
  116. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  117. if (amdgpu_bo_create_kernel(adev, adev->pm.smu_prv_buffer_size,
  118. PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
  119. &adev->pm.smu_prv_buffer,
  120. &gpu_addr,
  121. &cpu_ptr)) {
  122. DRM_ERROR("amdgpu: failed to create smu prv buffer\n");
  123. return;
  124. }
  125. if (hwmgr->hwmgr_func->notify_cac_buffer_info)
  126. r = hwmgr->hwmgr_func->notify_cac_buffer_info(hwmgr,
  127. lower_32_bits((unsigned long)cpu_ptr),
  128. upper_32_bits((unsigned long)cpu_ptr),
  129. lower_32_bits(gpu_addr),
  130. upper_32_bits(gpu_addr),
  131. adev->pm.smu_prv_buffer_size);
  132. if (r) {
  133. amdgpu_bo_free_kernel(&adev->pm.smu_prv_buffer, NULL, NULL);
  134. adev->pm.smu_prv_buffer = NULL;
  135. DRM_ERROR("amdgpu: failed to notify SMU buffer address\n");
  136. }
  137. }
  138. static int pp_late_init(void *handle)
  139. {
  140. struct amdgpu_device *adev = handle;
  141. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  142. if (hwmgr && hwmgr->pm_en) {
  143. mutex_lock(&hwmgr->smu_lock);
  144. hwmgr_handle_task(hwmgr,
  145. AMD_PP_TASK_COMPLETE_INIT, NULL);
  146. mutex_unlock(&hwmgr->smu_lock);
  147. }
  148. if (adev->pm.smu_prv_buffer_size != 0)
  149. pp_reserve_vram_for_smu(adev);
  150. return 0;
  151. }
  152. static void pp_late_fini(void *handle)
  153. {
  154. struct amdgpu_device *adev = handle;
  155. if (adev->pm.smu_prv_buffer)
  156. amdgpu_bo_free_kernel(&adev->pm.smu_prv_buffer, NULL, NULL);
  157. amd_powerplay_destroy(adev);
  158. }
  159. static bool pp_is_idle(void *handle)
  160. {
  161. return false;
  162. }
  163. static int pp_wait_for_idle(void *handle)
  164. {
  165. return 0;
  166. }
  167. static int pp_sw_reset(void *handle)
  168. {
  169. return 0;
  170. }
  171. static int pp_set_powergating_state(void *handle,
  172. enum amd_powergating_state state)
  173. {
  174. return 0;
  175. }
  176. static int pp_suspend(void *handle)
  177. {
  178. struct amdgpu_device *adev = handle;
  179. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  180. return hwmgr_suspend(hwmgr);
  181. }
  182. static int pp_resume(void *handle)
  183. {
  184. struct amdgpu_device *adev = handle;
  185. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  186. return hwmgr_resume(hwmgr);
  187. }
  188. static int pp_set_clockgating_state(void *handle,
  189. enum amd_clockgating_state state)
  190. {
  191. return 0;
  192. }
  193. static const struct amd_ip_funcs pp_ip_funcs = {
  194. .name = "powerplay",
  195. .early_init = pp_early_init,
  196. .late_init = pp_late_init,
  197. .sw_init = pp_sw_init,
  198. .sw_fini = pp_sw_fini,
  199. .hw_init = pp_hw_init,
  200. .hw_fini = pp_hw_fini,
  201. .late_fini = pp_late_fini,
  202. .suspend = pp_suspend,
  203. .resume = pp_resume,
  204. .is_idle = pp_is_idle,
  205. .wait_for_idle = pp_wait_for_idle,
  206. .soft_reset = pp_sw_reset,
  207. .set_clockgating_state = pp_set_clockgating_state,
  208. .set_powergating_state = pp_set_powergating_state,
  209. };
  210. const struct amdgpu_ip_block_version pp_smu_ip_block =
  211. {
  212. .type = AMD_IP_BLOCK_TYPE_SMC,
  213. .major = 1,
  214. .minor = 0,
  215. .rev = 0,
  216. .funcs = &pp_ip_funcs,
  217. };
  218. /* This interface only be supported On Vi,
  219. * because only smu7/8 can help to load gfx/sdma fw,
  220. * smu need to be enabled before load other ip's fw.
  221. * so call start smu to load smu7 fw and other ip's fw
  222. */
  223. static int pp_dpm_load_fw(void *handle)
  224. {
  225. struct pp_hwmgr *hwmgr = handle;
  226. if (!hwmgr || !hwmgr->smumgr_funcs || !hwmgr->smumgr_funcs->start_smu)
  227. return -EINVAL;
  228. if (hwmgr->smumgr_funcs->start_smu(hwmgr)) {
  229. pr_err("fw load failed\n");
  230. return -EINVAL;
  231. }
  232. return 0;
  233. }
  234. static int pp_dpm_fw_loading_complete(void *handle)
  235. {
  236. return 0;
  237. }
  238. static int pp_set_clockgating_by_smu(void *handle, uint32_t msg_id)
  239. {
  240. struct pp_hwmgr *hwmgr = handle;
  241. if (!hwmgr || !hwmgr->pm_en)
  242. return -EINVAL;
  243. if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
  244. pr_info("%s was not implemented.\n", __func__);
  245. return 0;
  246. }
  247. return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  248. }
  249. static void pp_dpm_en_umd_pstate(struct pp_hwmgr *hwmgr,
  250. enum amd_dpm_forced_level *level)
  251. {
  252. uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
  253. AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
  254. AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
  255. AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
  256. if (!(hwmgr->dpm_level & profile_mode_mask)) {
  257. /* enter umd pstate, save current level, disable gfx cg*/
  258. if (*level & profile_mode_mask) {
  259. hwmgr->saved_dpm_level = hwmgr->dpm_level;
  260. hwmgr->en_umd_pstate = true;
  261. amdgpu_device_ip_set_clockgating_state(hwmgr->adev,
  262. AMD_IP_BLOCK_TYPE_GFX,
  263. AMD_CG_STATE_UNGATE);
  264. amdgpu_device_ip_set_powergating_state(hwmgr->adev,
  265. AMD_IP_BLOCK_TYPE_GFX,
  266. AMD_PG_STATE_UNGATE);
  267. }
  268. } else {
  269. /* exit umd pstate, restore level, enable gfx cg*/
  270. if (!(*level & profile_mode_mask)) {
  271. if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
  272. *level = hwmgr->saved_dpm_level;
  273. hwmgr->en_umd_pstate = false;
  274. amdgpu_device_ip_set_clockgating_state(hwmgr->adev,
  275. AMD_IP_BLOCK_TYPE_GFX,
  276. AMD_CG_STATE_GATE);
  277. amdgpu_device_ip_set_powergating_state(hwmgr->adev,
  278. AMD_IP_BLOCK_TYPE_GFX,
  279. AMD_PG_STATE_GATE);
  280. }
  281. }
  282. }
  283. static int pp_dpm_force_performance_level(void *handle,
  284. enum amd_dpm_forced_level level)
  285. {
  286. struct pp_hwmgr *hwmgr = handle;
  287. if (!hwmgr || !hwmgr->pm_en)
  288. return -EINVAL;
  289. if (level == hwmgr->dpm_level)
  290. return 0;
  291. mutex_lock(&hwmgr->smu_lock);
  292. pp_dpm_en_umd_pstate(hwmgr, &level);
  293. hwmgr->request_dpm_level = level;
  294. hwmgr_handle_task(hwmgr, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
  295. mutex_unlock(&hwmgr->smu_lock);
  296. return 0;
  297. }
  298. static enum amd_dpm_forced_level pp_dpm_get_performance_level(
  299. void *handle)
  300. {
  301. struct pp_hwmgr *hwmgr = handle;
  302. enum amd_dpm_forced_level level;
  303. if (!hwmgr || !hwmgr->pm_en)
  304. return -EINVAL;
  305. mutex_lock(&hwmgr->smu_lock);
  306. level = hwmgr->dpm_level;
  307. mutex_unlock(&hwmgr->smu_lock);
  308. return level;
  309. }
  310. static uint32_t pp_dpm_get_sclk(void *handle, bool low)
  311. {
  312. struct pp_hwmgr *hwmgr = handle;
  313. uint32_t clk = 0;
  314. if (!hwmgr || !hwmgr->pm_en)
  315. return 0;
  316. if (hwmgr->hwmgr_func->get_sclk == NULL) {
  317. pr_info("%s was not implemented.\n", __func__);
  318. return 0;
  319. }
  320. mutex_lock(&hwmgr->smu_lock);
  321. clk = hwmgr->hwmgr_func->get_sclk(hwmgr, low);
  322. mutex_unlock(&hwmgr->smu_lock);
  323. return clk;
  324. }
  325. static uint32_t pp_dpm_get_mclk(void *handle, bool low)
  326. {
  327. struct pp_hwmgr *hwmgr = handle;
  328. uint32_t clk = 0;
  329. if (!hwmgr || !hwmgr->pm_en)
  330. return 0;
  331. if (hwmgr->hwmgr_func->get_mclk == NULL) {
  332. pr_info("%s was not implemented.\n", __func__);
  333. return 0;
  334. }
  335. mutex_lock(&hwmgr->smu_lock);
  336. clk = hwmgr->hwmgr_func->get_mclk(hwmgr, low);
  337. mutex_unlock(&hwmgr->smu_lock);
  338. return clk;
  339. }
  340. static void pp_dpm_powergate_vce(void *handle, bool gate)
  341. {
  342. struct pp_hwmgr *hwmgr = handle;
  343. if (!hwmgr || !hwmgr->pm_en)
  344. return;
  345. if (hwmgr->hwmgr_func->powergate_vce == NULL) {
  346. pr_info("%s was not implemented.\n", __func__);
  347. return;
  348. }
  349. mutex_lock(&hwmgr->smu_lock);
  350. hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
  351. mutex_unlock(&hwmgr->smu_lock);
  352. }
  353. static void pp_dpm_powergate_uvd(void *handle, bool gate)
  354. {
  355. struct pp_hwmgr *hwmgr = handle;
  356. if (!hwmgr || !hwmgr->pm_en)
  357. return;
  358. if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
  359. pr_info("%s was not implemented.\n", __func__);
  360. return;
  361. }
  362. mutex_lock(&hwmgr->smu_lock);
  363. hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
  364. mutex_unlock(&hwmgr->smu_lock);
  365. }
  366. static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
  367. enum amd_pm_state_type *user_state)
  368. {
  369. int ret = 0;
  370. struct pp_hwmgr *hwmgr = handle;
  371. if (!hwmgr || !hwmgr->pm_en)
  372. return -EINVAL;
  373. mutex_lock(&hwmgr->smu_lock);
  374. ret = hwmgr_handle_task(hwmgr, task_id, user_state);
  375. mutex_unlock(&hwmgr->smu_lock);
  376. return ret;
  377. }
  378. static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
  379. {
  380. struct pp_hwmgr *hwmgr = handle;
  381. struct pp_power_state *state;
  382. enum amd_pm_state_type pm_type;
  383. if (!hwmgr || !hwmgr->pm_en || !hwmgr->current_ps)
  384. return -EINVAL;
  385. mutex_lock(&hwmgr->smu_lock);
  386. state = hwmgr->current_ps;
  387. switch (state->classification.ui_label) {
  388. case PP_StateUILabel_Battery:
  389. pm_type = POWER_STATE_TYPE_BATTERY;
  390. break;
  391. case PP_StateUILabel_Balanced:
  392. pm_type = POWER_STATE_TYPE_BALANCED;
  393. break;
  394. case PP_StateUILabel_Performance:
  395. pm_type = POWER_STATE_TYPE_PERFORMANCE;
  396. break;
  397. default:
  398. if (state->classification.flags & PP_StateClassificationFlag_Boot)
  399. pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
  400. else
  401. pm_type = POWER_STATE_TYPE_DEFAULT;
  402. break;
  403. }
  404. mutex_unlock(&hwmgr->smu_lock);
  405. return pm_type;
  406. }
  407. static void pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
  408. {
  409. struct pp_hwmgr *hwmgr = handle;
  410. if (!hwmgr || !hwmgr->pm_en)
  411. return;
  412. if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
  413. pr_info("%s was not implemented.\n", __func__);
  414. return;
  415. }
  416. mutex_lock(&hwmgr->smu_lock);
  417. hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
  418. mutex_unlock(&hwmgr->smu_lock);
  419. }
  420. static uint32_t pp_dpm_get_fan_control_mode(void *handle)
  421. {
  422. struct pp_hwmgr *hwmgr = handle;
  423. uint32_t mode = 0;
  424. if (!hwmgr || !hwmgr->pm_en)
  425. return 0;
  426. if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
  427. pr_info("%s was not implemented.\n", __func__);
  428. return 0;
  429. }
  430. mutex_lock(&hwmgr->smu_lock);
  431. mode = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
  432. mutex_unlock(&hwmgr->smu_lock);
  433. return mode;
  434. }
  435. static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
  436. {
  437. struct pp_hwmgr *hwmgr = handle;
  438. int ret = 0;
  439. if (!hwmgr || !hwmgr->pm_en)
  440. return -EINVAL;
  441. if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
  442. pr_info("%s was not implemented.\n", __func__);
  443. return 0;
  444. }
  445. mutex_lock(&hwmgr->smu_lock);
  446. ret = hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
  447. mutex_unlock(&hwmgr->smu_lock);
  448. return ret;
  449. }
  450. static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
  451. {
  452. struct pp_hwmgr *hwmgr = handle;
  453. int ret = 0;
  454. if (!hwmgr || !hwmgr->pm_en)
  455. return -EINVAL;
  456. if (hwmgr->hwmgr_func->get_fan_speed_percent == NULL) {
  457. pr_info("%s was not implemented.\n", __func__);
  458. return 0;
  459. }
  460. mutex_lock(&hwmgr->smu_lock);
  461. ret = hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
  462. mutex_unlock(&hwmgr->smu_lock);
  463. return ret;
  464. }
  465. static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
  466. {
  467. struct pp_hwmgr *hwmgr = handle;
  468. int ret = 0;
  469. if (!hwmgr || !hwmgr->pm_en)
  470. return -EINVAL;
  471. if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
  472. return -EINVAL;
  473. mutex_lock(&hwmgr->smu_lock);
  474. ret = hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
  475. mutex_unlock(&hwmgr->smu_lock);
  476. return ret;
  477. }
  478. static int pp_dpm_set_fan_speed_rpm(void *handle, uint32_t rpm)
  479. {
  480. struct pp_hwmgr *hwmgr = handle;
  481. int ret = 0;
  482. if (!hwmgr || !hwmgr->pm_en)
  483. return -EINVAL;
  484. if (hwmgr->hwmgr_func->set_fan_speed_rpm == NULL) {
  485. pr_info("%s was not implemented.\n", __func__);
  486. return 0;
  487. }
  488. mutex_lock(&hwmgr->smu_lock);
  489. ret = hwmgr->hwmgr_func->set_fan_speed_rpm(hwmgr, rpm);
  490. mutex_unlock(&hwmgr->smu_lock);
  491. return ret;
  492. }
  493. static int pp_dpm_get_pp_num_states(void *handle,
  494. struct pp_states_info *data)
  495. {
  496. struct pp_hwmgr *hwmgr = handle;
  497. int i;
  498. memset(data, 0, sizeof(*data));
  499. if (!hwmgr || !hwmgr->pm_en ||!hwmgr->ps)
  500. return -EINVAL;
  501. mutex_lock(&hwmgr->smu_lock);
  502. data->nums = hwmgr->num_ps;
  503. for (i = 0; i < hwmgr->num_ps; i++) {
  504. struct pp_power_state *state = (struct pp_power_state *)
  505. ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
  506. switch (state->classification.ui_label) {
  507. case PP_StateUILabel_Battery:
  508. data->states[i] = POWER_STATE_TYPE_BATTERY;
  509. break;
  510. case PP_StateUILabel_Balanced:
  511. data->states[i] = POWER_STATE_TYPE_BALANCED;
  512. break;
  513. case PP_StateUILabel_Performance:
  514. data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
  515. break;
  516. default:
  517. if (state->classification.flags & PP_StateClassificationFlag_Boot)
  518. data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
  519. else
  520. data->states[i] = POWER_STATE_TYPE_DEFAULT;
  521. }
  522. }
  523. mutex_unlock(&hwmgr->smu_lock);
  524. return 0;
  525. }
  526. static int pp_dpm_get_pp_table(void *handle, char **table)
  527. {
  528. struct pp_hwmgr *hwmgr = handle;
  529. int size = 0;
  530. if (!hwmgr || !hwmgr->pm_en ||!hwmgr->soft_pp_table)
  531. return -EINVAL;
  532. mutex_lock(&hwmgr->smu_lock);
  533. *table = (char *)hwmgr->soft_pp_table;
  534. size = hwmgr->soft_pp_table_size;
  535. mutex_unlock(&hwmgr->smu_lock);
  536. return size;
  537. }
  538. static int amd_powerplay_reset(void *handle)
  539. {
  540. struct pp_hwmgr *hwmgr = handle;
  541. int ret;
  542. ret = hwmgr_hw_fini(hwmgr);
  543. if (ret)
  544. return ret;
  545. ret = hwmgr_hw_init(hwmgr);
  546. if (ret)
  547. return ret;
  548. return hwmgr_handle_task(hwmgr, AMD_PP_TASK_COMPLETE_INIT, NULL);
  549. }
  550. static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
  551. {
  552. struct pp_hwmgr *hwmgr = handle;
  553. int ret = -ENOMEM;
  554. if (!hwmgr || !hwmgr->pm_en)
  555. return -EINVAL;
  556. mutex_lock(&hwmgr->smu_lock);
  557. if (!hwmgr->hardcode_pp_table) {
  558. hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
  559. hwmgr->soft_pp_table_size,
  560. GFP_KERNEL);
  561. if (!hwmgr->hardcode_pp_table)
  562. goto err;
  563. }
  564. memcpy(hwmgr->hardcode_pp_table, buf, size);
  565. hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
  566. ret = amd_powerplay_reset(handle);
  567. if (ret)
  568. goto err;
  569. if (hwmgr->hwmgr_func->avfs_control) {
  570. ret = hwmgr->hwmgr_func->avfs_control(hwmgr, false);
  571. if (ret)
  572. goto err;
  573. }
  574. mutex_unlock(&hwmgr->smu_lock);
  575. return 0;
  576. err:
  577. mutex_unlock(&hwmgr->smu_lock);
  578. return ret;
  579. }
  580. static int pp_dpm_force_clock_level(void *handle,
  581. enum pp_clock_type type, uint32_t mask)
  582. {
  583. struct pp_hwmgr *hwmgr = handle;
  584. int ret = 0;
  585. if (!hwmgr || !hwmgr->pm_en)
  586. return -EINVAL;
  587. if (hwmgr->hwmgr_func->force_clock_level == NULL) {
  588. pr_info("%s was not implemented.\n", __func__);
  589. return 0;
  590. }
  591. if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
  592. pr_info("force clock level is for dpm manual mode only.\n");
  593. return -EINVAL;
  594. }
  595. mutex_lock(&hwmgr->smu_lock);
  596. ret = hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
  597. mutex_unlock(&hwmgr->smu_lock);
  598. return ret;
  599. }
  600. static int pp_dpm_print_clock_levels(void *handle,
  601. enum pp_clock_type type, char *buf)
  602. {
  603. struct pp_hwmgr *hwmgr = handle;
  604. int ret = 0;
  605. if (!hwmgr || !hwmgr->pm_en)
  606. return -EINVAL;
  607. if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
  608. pr_info("%s was not implemented.\n", __func__);
  609. return 0;
  610. }
  611. mutex_lock(&hwmgr->smu_lock);
  612. ret = hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
  613. mutex_unlock(&hwmgr->smu_lock);
  614. return ret;
  615. }
  616. static int pp_dpm_get_sclk_od(void *handle)
  617. {
  618. struct pp_hwmgr *hwmgr = handle;
  619. int ret = 0;
  620. if (!hwmgr || !hwmgr->pm_en)
  621. return -EINVAL;
  622. if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
  623. pr_info("%s was not implemented.\n", __func__);
  624. return 0;
  625. }
  626. mutex_lock(&hwmgr->smu_lock);
  627. ret = hwmgr->hwmgr_func->get_sclk_od(hwmgr);
  628. mutex_unlock(&hwmgr->smu_lock);
  629. return ret;
  630. }
  631. static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
  632. {
  633. struct pp_hwmgr *hwmgr = handle;
  634. int ret = 0;
  635. if (!hwmgr || !hwmgr->pm_en)
  636. return -EINVAL;
  637. if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
  638. pr_info("%s was not implemented.\n", __func__);
  639. return 0;
  640. }
  641. mutex_lock(&hwmgr->smu_lock);
  642. ret = hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
  643. mutex_unlock(&hwmgr->smu_lock);
  644. return ret;
  645. }
  646. static int pp_dpm_get_mclk_od(void *handle)
  647. {
  648. struct pp_hwmgr *hwmgr = handle;
  649. int ret = 0;
  650. if (!hwmgr || !hwmgr->pm_en)
  651. return -EINVAL;
  652. if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
  653. pr_info("%s was not implemented.\n", __func__);
  654. return 0;
  655. }
  656. mutex_lock(&hwmgr->smu_lock);
  657. ret = hwmgr->hwmgr_func->get_mclk_od(hwmgr);
  658. mutex_unlock(&hwmgr->smu_lock);
  659. return ret;
  660. }
  661. static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
  662. {
  663. struct pp_hwmgr *hwmgr = handle;
  664. int ret = 0;
  665. if (!hwmgr || !hwmgr->pm_en)
  666. return -EINVAL;
  667. if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
  668. pr_info("%s was not implemented.\n", __func__);
  669. return 0;
  670. }
  671. mutex_lock(&hwmgr->smu_lock);
  672. ret = hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
  673. mutex_unlock(&hwmgr->smu_lock);
  674. return ret;
  675. }
  676. static int pp_dpm_read_sensor(void *handle, int idx,
  677. void *value, int *size)
  678. {
  679. struct pp_hwmgr *hwmgr = handle;
  680. int ret = 0;
  681. if (!hwmgr || !hwmgr->pm_en || !value)
  682. return -EINVAL;
  683. switch (idx) {
  684. case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
  685. *((uint32_t *)value) = hwmgr->pstate_sclk;
  686. return 0;
  687. case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
  688. *((uint32_t *)value) = hwmgr->pstate_mclk;
  689. return 0;
  690. case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
  691. *((uint32_t *)value) = hwmgr->thermal_controller.fanInfo.ulMinRPM;
  692. return 0;
  693. case AMDGPU_PP_SENSOR_MAX_FAN_RPM:
  694. *((uint32_t *)value) = hwmgr->thermal_controller.fanInfo.ulMaxRPM;
  695. return 0;
  696. default:
  697. mutex_lock(&hwmgr->smu_lock);
  698. ret = hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
  699. mutex_unlock(&hwmgr->smu_lock);
  700. return ret;
  701. }
  702. }
  703. static struct amd_vce_state*
  704. pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
  705. {
  706. struct pp_hwmgr *hwmgr = handle;
  707. if (!hwmgr || !hwmgr->pm_en)
  708. return NULL;
  709. if (idx < hwmgr->num_vce_state_tables)
  710. return &hwmgr->vce_states[idx];
  711. return NULL;
  712. }
  713. static int pp_get_power_profile_mode(void *handle, char *buf)
  714. {
  715. struct pp_hwmgr *hwmgr = handle;
  716. if (!hwmgr || !hwmgr->pm_en || !buf)
  717. return -EINVAL;
  718. if (hwmgr->hwmgr_func->get_power_profile_mode == NULL) {
  719. pr_info("%s was not implemented.\n", __func__);
  720. return snprintf(buf, PAGE_SIZE, "\n");
  721. }
  722. return hwmgr->hwmgr_func->get_power_profile_mode(hwmgr, buf);
  723. }
  724. static int pp_set_power_profile_mode(void *handle, long *input, uint32_t size)
  725. {
  726. struct pp_hwmgr *hwmgr = handle;
  727. int ret = -EINVAL;
  728. if (!hwmgr || !hwmgr->pm_en)
  729. return ret;
  730. if (hwmgr->hwmgr_func->set_power_profile_mode == NULL) {
  731. pr_info("%s was not implemented.\n", __func__);
  732. return ret;
  733. }
  734. if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
  735. pr_info("power profile setting is for manual dpm mode only.\n");
  736. return ret;
  737. }
  738. mutex_lock(&hwmgr->smu_lock);
  739. ret = hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, input, size);
  740. mutex_unlock(&hwmgr->smu_lock);
  741. return ret;
  742. }
  743. static int pp_odn_edit_dpm_table(void *handle, uint32_t type, long *input, uint32_t size)
  744. {
  745. struct pp_hwmgr *hwmgr = handle;
  746. if (!hwmgr || !hwmgr->pm_en)
  747. return -EINVAL;
  748. if (hwmgr->hwmgr_func->odn_edit_dpm_table == NULL) {
  749. pr_info("%s was not implemented.\n", __func__);
  750. return -EINVAL;
  751. }
  752. return hwmgr->hwmgr_func->odn_edit_dpm_table(hwmgr, type, input, size);
  753. }
  754. static int pp_dpm_switch_power_profile(void *handle,
  755. enum PP_SMC_POWER_PROFILE type, bool en)
  756. {
  757. struct pp_hwmgr *hwmgr = handle;
  758. long workload;
  759. uint32_t index;
  760. if (!hwmgr || !hwmgr->pm_en)
  761. return -EINVAL;
  762. if (hwmgr->hwmgr_func->set_power_profile_mode == NULL) {
  763. pr_info("%s was not implemented.\n", __func__);
  764. return -EINVAL;
  765. }
  766. if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
  767. return -EINVAL;
  768. mutex_lock(&hwmgr->smu_lock);
  769. if (!en) {
  770. hwmgr->workload_mask &= ~(1 << hwmgr->workload_prority[type]);
  771. index = fls(hwmgr->workload_mask);
  772. index = index > 0 && index <= Workload_Policy_Max ? index - 1 : 0;
  773. workload = hwmgr->workload_setting[index];
  774. } else {
  775. hwmgr->workload_mask |= (1 << hwmgr->workload_prority[type]);
  776. index = fls(hwmgr->workload_mask);
  777. index = index <= Workload_Policy_Max ? index - 1 : 0;
  778. workload = hwmgr->workload_setting[index];
  779. }
  780. if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
  781. hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, &workload, 0);
  782. mutex_unlock(&hwmgr->smu_lock);
  783. return 0;
  784. }
  785. static int pp_set_power_limit(void *handle, uint32_t limit)
  786. {
  787. struct pp_hwmgr *hwmgr = handle;
  788. uint32_t max_power_limit;
  789. if (!hwmgr || !hwmgr->pm_en)
  790. return -EINVAL;
  791. if (hwmgr->hwmgr_func->set_power_limit == NULL) {
  792. pr_info("%s was not implemented.\n", __func__);
  793. return -EINVAL;
  794. }
  795. if (limit == 0)
  796. limit = hwmgr->default_power_limit;
  797. max_power_limit = hwmgr->default_power_limit;
  798. if (hwmgr->od_enabled) {
  799. max_power_limit *= (100 + hwmgr->platform_descriptor.TDPODLimit);
  800. max_power_limit /= 100;
  801. }
  802. if (limit > max_power_limit)
  803. return -EINVAL;
  804. mutex_lock(&hwmgr->smu_lock);
  805. hwmgr->hwmgr_func->set_power_limit(hwmgr, limit);
  806. hwmgr->power_limit = limit;
  807. mutex_unlock(&hwmgr->smu_lock);
  808. return 0;
  809. }
  810. static int pp_get_power_limit(void *handle, uint32_t *limit, bool default_limit)
  811. {
  812. struct pp_hwmgr *hwmgr = handle;
  813. if (!hwmgr || !hwmgr->pm_en ||!limit)
  814. return -EINVAL;
  815. mutex_lock(&hwmgr->smu_lock);
  816. if (default_limit) {
  817. *limit = hwmgr->default_power_limit;
  818. if (hwmgr->od_enabled) {
  819. *limit *= (100 + hwmgr->platform_descriptor.TDPODLimit);
  820. *limit /= 100;
  821. }
  822. }
  823. else
  824. *limit = hwmgr->power_limit;
  825. mutex_unlock(&hwmgr->smu_lock);
  826. return 0;
  827. }
  828. static int pp_display_configuration_change(void *handle,
  829. const struct amd_pp_display_configuration *display_config)
  830. {
  831. struct pp_hwmgr *hwmgr = handle;
  832. if (!hwmgr || !hwmgr->pm_en)
  833. return -EINVAL;
  834. mutex_lock(&hwmgr->smu_lock);
  835. phm_store_dal_configuration_data(hwmgr, display_config);
  836. mutex_unlock(&hwmgr->smu_lock);
  837. return 0;
  838. }
  839. static int pp_get_display_power_level(void *handle,
  840. struct amd_pp_simple_clock_info *output)
  841. {
  842. struct pp_hwmgr *hwmgr = handle;
  843. int ret = 0;
  844. if (!hwmgr || !hwmgr->pm_en ||!output)
  845. return -EINVAL;
  846. mutex_lock(&hwmgr->smu_lock);
  847. ret = phm_get_dal_power_level(hwmgr, output);
  848. mutex_unlock(&hwmgr->smu_lock);
  849. return ret;
  850. }
  851. static int pp_get_current_clocks(void *handle,
  852. struct amd_pp_clock_info *clocks)
  853. {
  854. struct amd_pp_simple_clock_info simple_clocks = { 0 };
  855. struct pp_clock_info hw_clocks;
  856. struct pp_hwmgr *hwmgr = handle;
  857. int ret = 0;
  858. if (!hwmgr || !hwmgr->pm_en)
  859. return -EINVAL;
  860. mutex_lock(&hwmgr->smu_lock);
  861. phm_get_dal_power_level(hwmgr, &simple_clocks);
  862. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  863. PHM_PlatformCaps_PowerContainment))
  864. ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
  865. &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment);
  866. else
  867. ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
  868. &hw_clocks, PHM_PerformanceLevelDesignation_Activity);
  869. if (ret) {
  870. pr_info("Error in phm_get_clock_info \n");
  871. mutex_unlock(&hwmgr->smu_lock);
  872. return -EINVAL;
  873. }
  874. clocks->min_engine_clock = hw_clocks.min_eng_clk;
  875. clocks->max_engine_clock = hw_clocks.max_eng_clk;
  876. clocks->min_memory_clock = hw_clocks.min_mem_clk;
  877. clocks->max_memory_clock = hw_clocks.max_mem_clk;
  878. clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
  879. clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
  880. clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
  881. clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
  882. if (simple_clocks.level == 0)
  883. clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
  884. else
  885. clocks->max_clocks_state = simple_clocks.level;
  886. if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
  887. clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
  888. clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
  889. }
  890. mutex_unlock(&hwmgr->smu_lock);
  891. return 0;
  892. }
  893. static int pp_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
  894. {
  895. struct pp_hwmgr *hwmgr = handle;
  896. int ret = 0;
  897. if (!hwmgr || !hwmgr->pm_en)
  898. return -EINVAL;
  899. if (clocks == NULL)
  900. return -EINVAL;
  901. mutex_lock(&hwmgr->smu_lock);
  902. ret = phm_get_clock_by_type(hwmgr, type, clocks);
  903. mutex_unlock(&hwmgr->smu_lock);
  904. return ret;
  905. }
  906. static int pp_get_clock_by_type_with_latency(void *handle,
  907. enum amd_pp_clock_type type,
  908. struct pp_clock_levels_with_latency *clocks)
  909. {
  910. struct pp_hwmgr *hwmgr = handle;
  911. int ret = 0;
  912. if (!hwmgr || !hwmgr->pm_en ||!clocks)
  913. return -EINVAL;
  914. mutex_lock(&hwmgr->smu_lock);
  915. ret = phm_get_clock_by_type_with_latency(hwmgr, type, clocks);
  916. mutex_unlock(&hwmgr->smu_lock);
  917. return ret;
  918. }
  919. static int pp_get_clock_by_type_with_voltage(void *handle,
  920. enum amd_pp_clock_type type,
  921. struct pp_clock_levels_with_voltage *clocks)
  922. {
  923. struct pp_hwmgr *hwmgr = handle;
  924. int ret = 0;
  925. if (!hwmgr || !hwmgr->pm_en ||!clocks)
  926. return -EINVAL;
  927. mutex_lock(&hwmgr->smu_lock);
  928. ret = phm_get_clock_by_type_with_voltage(hwmgr, type, clocks);
  929. mutex_unlock(&hwmgr->smu_lock);
  930. return ret;
  931. }
  932. static int pp_set_watermarks_for_clocks_ranges(void *handle,
  933. void *clock_ranges)
  934. {
  935. struct pp_hwmgr *hwmgr = handle;
  936. int ret = 0;
  937. if (!hwmgr || !hwmgr->pm_en || !clock_ranges)
  938. return -EINVAL;
  939. mutex_lock(&hwmgr->smu_lock);
  940. ret = phm_set_watermarks_for_clocks_ranges(hwmgr,
  941. clock_ranges);
  942. mutex_unlock(&hwmgr->smu_lock);
  943. return ret;
  944. }
  945. static int pp_display_clock_voltage_request(void *handle,
  946. struct pp_display_clock_request *clock)
  947. {
  948. struct pp_hwmgr *hwmgr = handle;
  949. int ret = 0;
  950. if (!hwmgr || !hwmgr->pm_en ||!clock)
  951. return -EINVAL;
  952. mutex_lock(&hwmgr->smu_lock);
  953. ret = phm_display_clock_voltage_request(hwmgr, clock);
  954. mutex_unlock(&hwmgr->smu_lock);
  955. return ret;
  956. }
  957. static int pp_get_display_mode_validation_clocks(void *handle,
  958. struct amd_pp_simple_clock_info *clocks)
  959. {
  960. struct pp_hwmgr *hwmgr = handle;
  961. int ret = 0;
  962. if (!hwmgr || !hwmgr->pm_en ||!clocks)
  963. return -EINVAL;
  964. clocks->level = PP_DAL_POWERLEVEL_7;
  965. mutex_lock(&hwmgr->smu_lock);
  966. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
  967. ret = phm_get_max_high_clocks(hwmgr, clocks);
  968. mutex_unlock(&hwmgr->smu_lock);
  969. return ret;
  970. }
  971. static int pp_dpm_powergate_mmhub(void *handle)
  972. {
  973. struct pp_hwmgr *hwmgr = handle;
  974. if (!hwmgr || !hwmgr->pm_en)
  975. return -EINVAL;
  976. if (hwmgr->hwmgr_func->powergate_mmhub == NULL) {
  977. pr_info("%s was not implemented.\n", __func__);
  978. return 0;
  979. }
  980. return hwmgr->hwmgr_func->powergate_mmhub(hwmgr);
  981. }
  982. static int pp_dpm_powergate_gfx(void *handle, bool gate)
  983. {
  984. struct pp_hwmgr *hwmgr = handle;
  985. if (!hwmgr || !hwmgr->pm_en)
  986. return 0;
  987. if (hwmgr->hwmgr_func->powergate_gfx == NULL) {
  988. pr_info("%s was not implemented.\n", __func__);
  989. return 0;
  990. }
  991. return hwmgr->hwmgr_func->powergate_gfx(hwmgr, gate);
  992. }
  993. static void pp_dpm_powergate_acp(void *handle, bool gate)
  994. {
  995. struct pp_hwmgr *hwmgr = handle;
  996. if (!hwmgr || !hwmgr->pm_en)
  997. return;
  998. if (hwmgr->hwmgr_func->powergate_acp == NULL) {
  999. pr_info("%s was not implemented.\n", __func__);
  1000. return;
  1001. }
  1002. hwmgr->hwmgr_func->powergate_acp(hwmgr, gate);
  1003. }
  1004. static void pp_dpm_powergate_sdma(void *handle, bool gate)
  1005. {
  1006. struct pp_hwmgr *hwmgr = handle;
  1007. if (!hwmgr)
  1008. return;
  1009. if (hwmgr->hwmgr_func->powergate_sdma == NULL) {
  1010. pr_info("%s was not implemented.\n", __func__);
  1011. return;
  1012. }
  1013. hwmgr->hwmgr_func->powergate_sdma(hwmgr, gate);
  1014. }
  1015. static int pp_set_powergating_by_smu(void *handle,
  1016. uint32_t block_type, bool gate)
  1017. {
  1018. int ret = 0;
  1019. switch (block_type) {
  1020. case AMD_IP_BLOCK_TYPE_UVD:
  1021. case AMD_IP_BLOCK_TYPE_VCN:
  1022. pp_dpm_powergate_uvd(handle, gate);
  1023. break;
  1024. case AMD_IP_BLOCK_TYPE_VCE:
  1025. pp_dpm_powergate_vce(handle, gate);
  1026. break;
  1027. case AMD_IP_BLOCK_TYPE_GMC:
  1028. pp_dpm_powergate_mmhub(handle);
  1029. break;
  1030. case AMD_IP_BLOCK_TYPE_GFX:
  1031. ret = pp_dpm_powergate_gfx(handle, gate);
  1032. break;
  1033. case AMD_IP_BLOCK_TYPE_ACP:
  1034. pp_dpm_powergate_acp(handle, gate);
  1035. break;
  1036. case AMD_IP_BLOCK_TYPE_SDMA:
  1037. pp_dpm_powergate_sdma(handle, gate);
  1038. break;
  1039. default:
  1040. break;
  1041. }
  1042. return ret;
  1043. }
  1044. static int pp_notify_smu_enable_pwe(void *handle)
  1045. {
  1046. struct pp_hwmgr *hwmgr = handle;
  1047. if (!hwmgr || !hwmgr->pm_en)
  1048. return -EINVAL;
  1049. if (hwmgr->hwmgr_func->smus_notify_pwe == NULL) {
  1050. pr_info("%s was not implemented.\n", __func__);
  1051. return -EINVAL;;
  1052. }
  1053. mutex_lock(&hwmgr->smu_lock);
  1054. hwmgr->hwmgr_func->smus_notify_pwe(hwmgr);
  1055. mutex_unlock(&hwmgr->smu_lock);
  1056. return 0;
  1057. }
  1058. static int pp_enable_mgpu_fan_boost(void *handle)
  1059. {
  1060. struct pp_hwmgr *hwmgr = handle;
  1061. if (!hwmgr)
  1062. return -EINVAL;
  1063. if (!hwmgr->pm_en ||
  1064. hwmgr->hwmgr_func->enable_mgpu_fan_boost == NULL)
  1065. return 0;
  1066. mutex_lock(&hwmgr->smu_lock);
  1067. hwmgr->hwmgr_func->enable_mgpu_fan_boost(hwmgr);
  1068. mutex_unlock(&hwmgr->smu_lock);
  1069. return 0;
  1070. }
  1071. static const struct amd_pm_funcs pp_dpm_funcs = {
  1072. .load_firmware = pp_dpm_load_fw,
  1073. .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
  1074. .force_performance_level = pp_dpm_force_performance_level,
  1075. .get_performance_level = pp_dpm_get_performance_level,
  1076. .get_current_power_state = pp_dpm_get_current_power_state,
  1077. .dispatch_tasks = pp_dpm_dispatch_tasks,
  1078. .set_fan_control_mode = pp_dpm_set_fan_control_mode,
  1079. .get_fan_control_mode = pp_dpm_get_fan_control_mode,
  1080. .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
  1081. .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
  1082. .get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
  1083. .set_fan_speed_rpm = pp_dpm_set_fan_speed_rpm,
  1084. .get_pp_num_states = pp_dpm_get_pp_num_states,
  1085. .get_pp_table = pp_dpm_get_pp_table,
  1086. .set_pp_table = pp_dpm_set_pp_table,
  1087. .force_clock_level = pp_dpm_force_clock_level,
  1088. .print_clock_levels = pp_dpm_print_clock_levels,
  1089. .get_sclk_od = pp_dpm_get_sclk_od,
  1090. .set_sclk_od = pp_dpm_set_sclk_od,
  1091. .get_mclk_od = pp_dpm_get_mclk_od,
  1092. .set_mclk_od = pp_dpm_set_mclk_od,
  1093. .read_sensor = pp_dpm_read_sensor,
  1094. .get_vce_clock_state = pp_dpm_get_vce_clock_state,
  1095. .switch_power_profile = pp_dpm_switch_power_profile,
  1096. .set_clockgating_by_smu = pp_set_clockgating_by_smu,
  1097. .set_powergating_by_smu = pp_set_powergating_by_smu,
  1098. .get_power_profile_mode = pp_get_power_profile_mode,
  1099. .set_power_profile_mode = pp_set_power_profile_mode,
  1100. .odn_edit_dpm_table = pp_odn_edit_dpm_table,
  1101. .set_power_limit = pp_set_power_limit,
  1102. .get_power_limit = pp_get_power_limit,
  1103. /* export to DC */
  1104. .get_sclk = pp_dpm_get_sclk,
  1105. .get_mclk = pp_dpm_get_mclk,
  1106. .display_configuration_change = pp_display_configuration_change,
  1107. .get_display_power_level = pp_get_display_power_level,
  1108. .get_current_clocks = pp_get_current_clocks,
  1109. .get_clock_by_type = pp_get_clock_by_type,
  1110. .get_clock_by_type_with_latency = pp_get_clock_by_type_with_latency,
  1111. .get_clock_by_type_with_voltage = pp_get_clock_by_type_with_voltage,
  1112. .set_watermarks_for_clocks_ranges = pp_set_watermarks_for_clocks_ranges,
  1113. .display_clock_voltage_request = pp_display_clock_voltage_request,
  1114. .get_display_mode_validation_clocks = pp_get_display_mode_validation_clocks,
  1115. .notify_smu_enable_pwe = pp_notify_smu_enable_pwe,
  1116. .enable_mgpu_fan_boost = pp_enable_mgpu_fan_boost,
  1117. };