amd_powerplay.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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 "amd_shared.h"
  29. #include "amd_powerplay.h"
  30. #include "pp_instance.h"
  31. #include "power_state.h"
  32. #include "eventmanager.h"
  33. static inline int pp_check(struct pp_instance *handle)
  34. {
  35. if (handle == NULL || handle->pp_valid != PP_VALID)
  36. return -EINVAL;
  37. if (handle->smu_mgr == NULL || handle->smu_mgr->smumgr_funcs == NULL)
  38. return -EINVAL;
  39. if (handle->pm_en == 0)
  40. return PP_DPM_DISABLED;
  41. if (handle->hwmgr == NULL || handle->hwmgr->hwmgr_func == NULL
  42. || handle->eventmgr == NULL)
  43. return PP_DPM_DISABLED;
  44. return 0;
  45. }
  46. static int pp_early_init(void *handle)
  47. {
  48. int ret;
  49. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  50. ret = smum_early_init(pp_handle);
  51. if (ret)
  52. return ret;
  53. if ((pp_handle->pm_en == 0)
  54. || cgs_is_virtualization_enabled(pp_handle->device))
  55. return PP_DPM_DISABLED;
  56. ret = hwmgr_early_init(pp_handle);
  57. if (ret) {
  58. pp_handle->pm_en = 0;
  59. return PP_DPM_DISABLED;
  60. }
  61. ret = eventmgr_early_init(pp_handle);
  62. if (ret) {
  63. kfree(pp_handle->hwmgr);
  64. pp_handle->hwmgr = NULL;
  65. pp_handle->pm_en = 0;
  66. return PP_DPM_DISABLED;
  67. }
  68. return 0;
  69. }
  70. static int pp_sw_init(void *handle)
  71. {
  72. struct pp_smumgr *smumgr;
  73. int ret = 0;
  74. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  75. ret = pp_check(pp_handle);
  76. if (ret == 0 || ret == PP_DPM_DISABLED) {
  77. smumgr = pp_handle->smu_mgr;
  78. if (smumgr->smumgr_funcs->smu_init == NULL)
  79. return -EINVAL;
  80. ret = smumgr->smumgr_funcs->smu_init(smumgr);
  81. pr_info("amdgpu: powerplay sw initialized\n");
  82. }
  83. return ret;
  84. }
  85. static int pp_sw_fini(void *handle)
  86. {
  87. struct pp_smumgr *smumgr;
  88. int ret = 0;
  89. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  90. ret = pp_check(pp_handle);
  91. if (ret == 0 || ret == PP_DPM_DISABLED) {
  92. smumgr = pp_handle->smu_mgr;
  93. if (smumgr->smumgr_funcs->smu_fini == NULL)
  94. return -EINVAL;
  95. ret = smumgr->smumgr_funcs->smu_fini(smumgr);
  96. }
  97. return ret;
  98. }
  99. static int pp_hw_init(void *handle)
  100. {
  101. struct pp_smumgr *smumgr;
  102. struct pp_eventmgr *eventmgr;
  103. int ret = 0;
  104. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  105. ret = pp_check(pp_handle);
  106. if (ret == 0 || ret == PP_DPM_DISABLED) {
  107. smumgr = pp_handle->smu_mgr;
  108. if (smumgr->smumgr_funcs->start_smu == NULL)
  109. return -EINVAL;
  110. if(smumgr->smumgr_funcs->start_smu(smumgr)) {
  111. pr_err("smc start failed\n");
  112. smumgr->smumgr_funcs->smu_fini(smumgr);
  113. return -EINVAL;;
  114. }
  115. if (ret == PP_DPM_DISABLED)
  116. return PP_DPM_DISABLED;
  117. }
  118. ret = hwmgr_hw_init(pp_handle);
  119. if (ret)
  120. goto err;
  121. eventmgr = pp_handle->eventmgr;
  122. if (eventmgr->pp_eventmgr_init == NULL ||
  123. eventmgr->pp_eventmgr_init(eventmgr))
  124. goto err;
  125. return 0;
  126. err:
  127. pp_handle->pm_en = 0;
  128. kfree(pp_handle->eventmgr);
  129. kfree(pp_handle->hwmgr);
  130. pp_handle->hwmgr = NULL;
  131. pp_handle->eventmgr = NULL;
  132. return PP_DPM_DISABLED;
  133. }
  134. static int pp_hw_fini(void *handle)
  135. {
  136. struct pp_eventmgr *eventmgr;
  137. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  138. int ret = 0;
  139. ret = pp_check(pp_handle);
  140. if (ret == 0) {
  141. eventmgr = pp_handle->eventmgr;
  142. if (eventmgr->pp_eventmgr_fini != NULL)
  143. eventmgr->pp_eventmgr_fini(eventmgr);
  144. hwmgr_hw_fini(pp_handle);
  145. }
  146. return 0;
  147. }
  148. static bool pp_is_idle(void *handle)
  149. {
  150. return false;
  151. }
  152. static int pp_wait_for_idle(void *handle)
  153. {
  154. return 0;
  155. }
  156. static int pp_sw_reset(void *handle)
  157. {
  158. return 0;
  159. }
  160. int amd_set_clockgating_by_smu(void *handle, uint32_t msg_id)
  161. {
  162. struct pp_hwmgr *hwmgr;
  163. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  164. int ret = 0;
  165. ret = pp_check(pp_handle);
  166. if (ret != 0)
  167. return ret;
  168. hwmgr = pp_handle->hwmgr;
  169. if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
  170. pr_info("%s was not implemented.\n", __func__);
  171. return 0;
  172. }
  173. return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  174. }
  175. static int pp_set_powergating_state(void *handle,
  176. enum amd_powergating_state state)
  177. {
  178. struct pp_hwmgr *hwmgr;
  179. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  180. int ret = 0;
  181. ret = pp_check(pp_handle);
  182. if (ret != 0)
  183. return ret;
  184. hwmgr = pp_handle->hwmgr;
  185. if (hwmgr->hwmgr_func->enable_per_cu_power_gating == NULL) {
  186. pr_info("%s was not implemented.\n", __func__);
  187. return 0;
  188. }
  189. /* Enable/disable GFX per cu powergating through SMU */
  190. return hwmgr->hwmgr_func->enable_per_cu_power_gating(hwmgr,
  191. state == AMD_PG_STATE_GATE);
  192. }
  193. static int pp_suspend(void *handle)
  194. {
  195. struct pp_eventmgr *eventmgr;
  196. struct pem_event_data event_data = { {0} };
  197. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  198. int ret = 0;
  199. ret = pp_check(pp_handle);
  200. if (ret == PP_DPM_DISABLED)
  201. return 0;
  202. else if (ret != 0)
  203. return ret;
  204. eventmgr = pp_handle->eventmgr;
  205. pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
  206. return 0;
  207. }
  208. static int pp_resume(void *handle)
  209. {
  210. struct pp_eventmgr *eventmgr;
  211. struct pem_event_data event_data = { {0} };
  212. struct pp_smumgr *smumgr;
  213. int ret, ret1;
  214. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  215. ret1 = pp_check(pp_handle);
  216. if (ret1 != 0 && ret1 != PP_DPM_DISABLED)
  217. return ret1;
  218. smumgr = pp_handle->smu_mgr;
  219. if (smumgr->smumgr_funcs->start_smu == NULL)
  220. return -EINVAL;
  221. ret = smumgr->smumgr_funcs->start_smu(smumgr);
  222. if (ret) {
  223. pr_err("smc start failed\n");
  224. smumgr->smumgr_funcs->smu_fini(smumgr);
  225. return ret;
  226. }
  227. if (ret1 == PP_DPM_DISABLED)
  228. return 0;
  229. eventmgr = pp_handle->eventmgr;
  230. pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
  231. return 0;
  232. }
  233. const struct amd_ip_funcs pp_ip_funcs = {
  234. .name = "powerplay",
  235. .early_init = pp_early_init,
  236. .late_init = NULL,
  237. .sw_init = pp_sw_init,
  238. .sw_fini = pp_sw_fini,
  239. .hw_init = pp_hw_init,
  240. .hw_fini = pp_hw_fini,
  241. .suspend = pp_suspend,
  242. .resume = pp_resume,
  243. .is_idle = pp_is_idle,
  244. .wait_for_idle = pp_wait_for_idle,
  245. .soft_reset = pp_sw_reset,
  246. .set_clockgating_state = NULL,
  247. .set_powergating_state = pp_set_powergating_state,
  248. };
  249. static int pp_dpm_load_fw(void *handle)
  250. {
  251. return 0;
  252. }
  253. static int pp_dpm_fw_loading_complete(void *handle)
  254. {
  255. return 0;
  256. }
  257. static int pp_dpm_force_performance_level(void *handle,
  258. enum amd_dpm_forced_level level)
  259. {
  260. struct pp_hwmgr *hwmgr;
  261. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  262. int ret = 0;
  263. ret = pp_check(pp_handle);
  264. if (ret != 0)
  265. return ret;
  266. hwmgr = pp_handle->hwmgr;
  267. if (hwmgr->hwmgr_func->force_dpm_level == NULL) {
  268. pr_info("%s was not implemented.\n", __func__);
  269. return 0;
  270. }
  271. mutex_lock(&pp_handle->pp_lock);
  272. hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
  273. mutex_unlock(&pp_handle->pp_lock);
  274. return 0;
  275. }
  276. static enum amd_dpm_forced_level pp_dpm_get_performance_level(
  277. void *handle)
  278. {
  279. struct pp_hwmgr *hwmgr;
  280. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  281. int ret = 0;
  282. enum amd_dpm_forced_level level;
  283. ret = pp_check(pp_handle);
  284. if (ret != 0)
  285. return ret;
  286. hwmgr = pp_handle->hwmgr;
  287. mutex_lock(&pp_handle->pp_lock);
  288. level = hwmgr->dpm_level;
  289. mutex_unlock(&pp_handle->pp_lock);
  290. return level;
  291. }
  292. static int pp_dpm_get_sclk(void *handle, bool low)
  293. {
  294. struct pp_hwmgr *hwmgr;
  295. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  296. int ret = 0;
  297. ret = pp_check(pp_handle);
  298. if (ret != 0)
  299. return ret;
  300. hwmgr = pp_handle->hwmgr;
  301. if (hwmgr->hwmgr_func->get_sclk == NULL) {
  302. pr_info("%s was not implemented.\n", __func__);
  303. return 0;
  304. }
  305. mutex_lock(&pp_handle->pp_lock);
  306. ret = hwmgr->hwmgr_func->get_sclk(hwmgr, low);
  307. mutex_unlock(&pp_handle->pp_lock);
  308. return ret;
  309. }
  310. static int pp_dpm_get_mclk(void *handle, bool low)
  311. {
  312. struct pp_hwmgr *hwmgr;
  313. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  314. int ret = 0;
  315. ret = pp_check(pp_handle);
  316. if (ret != 0)
  317. return ret;
  318. hwmgr = pp_handle->hwmgr;
  319. if (hwmgr->hwmgr_func->get_mclk == NULL) {
  320. pr_info("%s was not implemented.\n", __func__);
  321. return 0;
  322. }
  323. mutex_lock(&pp_handle->pp_lock);
  324. ret = hwmgr->hwmgr_func->get_mclk(hwmgr, low);
  325. mutex_unlock(&pp_handle->pp_lock);
  326. return ret;
  327. }
  328. static int pp_dpm_powergate_vce(void *handle, bool gate)
  329. {
  330. struct pp_hwmgr *hwmgr;
  331. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  332. int ret = 0;
  333. ret = pp_check(pp_handle);
  334. if (ret != 0)
  335. return ret;
  336. hwmgr = pp_handle->hwmgr;
  337. if (hwmgr->hwmgr_func->powergate_vce == NULL) {
  338. pr_info("%s was not implemented.\n", __func__);
  339. return 0;
  340. }
  341. mutex_lock(&pp_handle->pp_lock);
  342. ret = hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
  343. mutex_unlock(&pp_handle->pp_lock);
  344. return ret;
  345. }
  346. static int pp_dpm_powergate_uvd(void *handle, bool gate)
  347. {
  348. struct pp_hwmgr *hwmgr;
  349. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  350. int ret = 0;
  351. ret = pp_check(pp_handle);
  352. if (ret != 0)
  353. return ret;
  354. hwmgr = pp_handle->hwmgr;
  355. if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
  356. pr_info("%s was not implemented.\n", __func__);
  357. return 0;
  358. }
  359. mutex_lock(&pp_handle->pp_lock);
  360. ret = hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
  361. mutex_unlock(&pp_handle->pp_lock);
  362. return ret;
  363. }
  364. static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
  365. {
  366. switch (state) {
  367. case POWER_STATE_TYPE_BATTERY:
  368. return PP_StateUILabel_Battery;
  369. case POWER_STATE_TYPE_BALANCED:
  370. return PP_StateUILabel_Balanced;
  371. case POWER_STATE_TYPE_PERFORMANCE:
  372. return PP_StateUILabel_Performance;
  373. default:
  374. return PP_StateUILabel_None;
  375. }
  376. }
  377. static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id,
  378. void *input, void *output)
  379. {
  380. int ret = 0;
  381. struct pem_event_data data = { {0} };
  382. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  383. ret = pp_check(pp_handle);
  384. if (ret != 0)
  385. return ret;
  386. mutex_lock(&pp_handle->pp_lock);
  387. switch (event_id) {
  388. case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
  389. ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
  390. break;
  391. case AMD_PP_EVENT_ENABLE_USER_STATE:
  392. {
  393. enum amd_pm_state_type ps;
  394. if (input == NULL) {
  395. ret = -EINVAL;
  396. break;
  397. }
  398. ps = *(unsigned long *)input;
  399. data.requested_ui_label = power_state_convert(ps);
  400. ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
  401. break;
  402. }
  403. case AMD_PP_EVENT_COMPLETE_INIT:
  404. ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
  405. break;
  406. case AMD_PP_EVENT_READJUST_POWER_STATE:
  407. ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
  408. break;
  409. default:
  410. break;
  411. }
  412. mutex_unlock(&pp_handle->pp_lock);
  413. return ret;
  414. }
  415. static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
  416. {
  417. struct pp_hwmgr *hwmgr;
  418. struct pp_power_state *state;
  419. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  420. int ret = 0;
  421. enum amd_pm_state_type pm_type;
  422. ret = pp_check(pp_handle);
  423. if (ret != 0)
  424. return ret;
  425. hwmgr = pp_handle->hwmgr;
  426. if (hwmgr->current_ps == NULL)
  427. return -EINVAL;
  428. mutex_lock(&pp_handle->pp_lock);
  429. state = hwmgr->current_ps;
  430. switch (state->classification.ui_label) {
  431. case PP_StateUILabel_Battery:
  432. pm_type = POWER_STATE_TYPE_BATTERY;
  433. break;
  434. case PP_StateUILabel_Balanced:
  435. pm_type = POWER_STATE_TYPE_BALANCED;
  436. break;
  437. case PP_StateUILabel_Performance:
  438. pm_type = POWER_STATE_TYPE_PERFORMANCE;
  439. break;
  440. default:
  441. if (state->classification.flags & PP_StateClassificationFlag_Boot)
  442. pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
  443. else
  444. pm_type = POWER_STATE_TYPE_DEFAULT;
  445. break;
  446. }
  447. mutex_unlock(&pp_handle->pp_lock);
  448. return pm_type;
  449. }
  450. static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
  451. {
  452. struct pp_hwmgr *hwmgr;
  453. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  454. int ret = 0;
  455. ret = pp_check(pp_handle);
  456. if (ret != 0)
  457. return ret;
  458. hwmgr = pp_handle->hwmgr;
  459. if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
  460. pr_info("%s was not implemented.\n", __func__);
  461. return 0;
  462. }
  463. mutex_lock(&pp_handle->pp_lock);
  464. ret = hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
  465. mutex_unlock(&pp_handle->pp_lock);
  466. return ret;
  467. }
  468. static int pp_dpm_get_fan_control_mode(void *handle)
  469. {
  470. struct pp_hwmgr *hwmgr;
  471. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  472. int ret = 0;
  473. ret = pp_check(pp_handle);
  474. if (ret != 0)
  475. return ret;
  476. hwmgr = pp_handle->hwmgr;
  477. if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
  478. pr_info("%s was not implemented.\n", __func__);
  479. return 0;
  480. }
  481. mutex_lock(&pp_handle->pp_lock);
  482. ret = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
  483. mutex_unlock(&pp_handle->pp_lock);
  484. return ret;
  485. }
  486. static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
  487. {
  488. struct pp_hwmgr *hwmgr;
  489. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  490. int ret = 0;
  491. ret = pp_check(pp_handle);
  492. if (ret != 0)
  493. return ret;
  494. hwmgr = pp_handle->hwmgr;
  495. if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
  496. pr_info("%s was not implemented.\n", __func__);
  497. return 0;
  498. }
  499. mutex_lock(&pp_handle->pp_lock);
  500. ret = hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
  501. mutex_unlock(&pp_handle->pp_lock);
  502. return ret;
  503. }
  504. static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
  505. {
  506. struct pp_hwmgr *hwmgr;
  507. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  508. int ret = 0;
  509. ret = pp_check(pp_handle);
  510. if (ret != 0)
  511. return ret;
  512. hwmgr = pp_handle->hwmgr;
  513. if (hwmgr->hwmgr_func->get_fan_speed_percent == NULL) {
  514. pr_info("%s was not implemented.\n", __func__);
  515. return 0;
  516. }
  517. mutex_lock(&pp_handle->pp_lock);
  518. ret = hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
  519. mutex_unlock(&pp_handle->pp_lock);
  520. return ret;
  521. }
  522. static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
  523. {
  524. struct pp_hwmgr *hwmgr;
  525. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  526. int ret = 0;
  527. ret = pp_check(pp_handle);
  528. if (ret != 0)
  529. return ret;
  530. hwmgr = pp_handle->hwmgr;
  531. if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
  532. return -EINVAL;
  533. mutex_lock(&pp_handle->pp_lock);
  534. ret = hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
  535. mutex_unlock(&pp_handle->pp_lock);
  536. return ret;
  537. }
  538. static int pp_dpm_get_temperature(void *handle)
  539. {
  540. struct pp_hwmgr *hwmgr;
  541. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  542. int ret = 0;
  543. ret = pp_check(pp_handle);
  544. if (ret != 0)
  545. return ret;
  546. hwmgr = pp_handle->hwmgr;
  547. if (hwmgr->hwmgr_func->get_temperature == NULL) {
  548. pr_info("%s was not implemented.\n", __func__);
  549. return 0;
  550. }
  551. mutex_lock(&pp_handle->pp_lock);
  552. ret = hwmgr->hwmgr_func->get_temperature(hwmgr);
  553. mutex_unlock(&pp_handle->pp_lock);
  554. return ret;
  555. }
  556. static int pp_dpm_get_pp_num_states(void *handle,
  557. struct pp_states_info *data)
  558. {
  559. struct pp_hwmgr *hwmgr;
  560. int i;
  561. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  562. int ret = 0;
  563. ret = pp_check(pp_handle);
  564. if (ret != 0)
  565. return ret;
  566. hwmgr = pp_handle->hwmgr;
  567. if (hwmgr->ps == NULL)
  568. return -EINVAL;
  569. mutex_lock(&pp_handle->pp_lock);
  570. data->nums = hwmgr->num_ps;
  571. for (i = 0; i < hwmgr->num_ps; i++) {
  572. struct pp_power_state *state = (struct pp_power_state *)
  573. ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
  574. switch (state->classification.ui_label) {
  575. case PP_StateUILabel_Battery:
  576. data->states[i] = POWER_STATE_TYPE_BATTERY;
  577. break;
  578. case PP_StateUILabel_Balanced:
  579. data->states[i] = POWER_STATE_TYPE_BALANCED;
  580. break;
  581. case PP_StateUILabel_Performance:
  582. data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
  583. break;
  584. default:
  585. if (state->classification.flags & PP_StateClassificationFlag_Boot)
  586. data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
  587. else
  588. data->states[i] = POWER_STATE_TYPE_DEFAULT;
  589. }
  590. }
  591. mutex_unlock(&pp_handle->pp_lock);
  592. return 0;
  593. }
  594. static int pp_dpm_get_pp_table(void *handle, char **table)
  595. {
  596. struct pp_hwmgr *hwmgr;
  597. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  598. int ret = 0;
  599. int size = 0;
  600. ret = pp_check(pp_handle);
  601. if (ret != 0)
  602. return ret;
  603. hwmgr = pp_handle->hwmgr;
  604. if (!hwmgr->soft_pp_table)
  605. return -EINVAL;
  606. mutex_lock(&pp_handle->pp_lock);
  607. *table = (char *)hwmgr->soft_pp_table;
  608. size = hwmgr->soft_pp_table_size;
  609. mutex_unlock(&pp_handle->pp_lock);
  610. return size;
  611. }
  612. static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
  613. {
  614. struct pp_hwmgr *hwmgr;
  615. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  616. int ret = 0;
  617. ret = pp_check(pp_handle);
  618. if (ret != 0)
  619. return ret;
  620. hwmgr = pp_handle->hwmgr;
  621. mutex_lock(&pp_handle->pp_lock);
  622. if (!hwmgr->hardcode_pp_table) {
  623. hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
  624. hwmgr->soft_pp_table_size,
  625. GFP_KERNEL);
  626. if (!hwmgr->hardcode_pp_table) {
  627. mutex_unlock(&pp_handle->pp_lock);
  628. return -ENOMEM;
  629. }
  630. }
  631. memcpy(hwmgr->hardcode_pp_table, buf, size);
  632. hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
  633. mutex_unlock(&pp_handle->pp_lock);
  634. ret = amd_powerplay_reset(handle);
  635. if (ret)
  636. return ret;
  637. if (hwmgr->hwmgr_func->avfs_control) {
  638. ret = hwmgr->hwmgr_func->avfs_control(hwmgr, false);
  639. if (ret)
  640. return ret;
  641. }
  642. return 0;
  643. }
  644. static int pp_dpm_force_clock_level(void *handle,
  645. enum pp_clock_type type, uint32_t mask)
  646. {
  647. struct pp_hwmgr *hwmgr;
  648. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  649. int ret = 0;
  650. ret = pp_check(pp_handle);
  651. if (ret != 0)
  652. return ret;
  653. hwmgr = pp_handle->hwmgr;
  654. if (hwmgr->hwmgr_func->force_clock_level == NULL) {
  655. pr_info("%s was not implemented.\n", __func__);
  656. return 0;
  657. }
  658. mutex_lock(&pp_handle->pp_lock);
  659. hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
  660. mutex_unlock(&pp_handle->pp_lock);
  661. return ret;
  662. }
  663. static int pp_dpm_print_clock_levels(void *handle,
  664. enum pp_clock_type type, char *buf)
  665. {
  666. struct pp_hwmgr *hwmgr;
  667. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  668. int ret = 0;
  669. ret = pp_check(pp_handle);
  670. if (ret != 0)
  671. return ret;
  672. hwmgr = pp_handle->hwmgr;
  673. if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
  674. pr_info("%s was not implemented.\n", __func__);
  675. return 0;
  676. }
  677. mutex_lock(&pp_handle->pp_lock);
  678. ret = hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
  679. mutex_unlock(&pp_handle->pp_lock);
  680. return ret;
  681. }
  682. static int pp_dpm_get_sclk_od(void *handle)
  683. {
  684. struct pp_hwmgr *hwmgr;
  685. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  686. int ret = 0;
  687. ret = pp_check(pp_handle);
  688. if (ret != 0)
  689. return ret;
  690. hwmgr = pp_handle->hwmgr;
  691. if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
  692. pr_info("%s was not implemented.\n", __func__);
  693. return 0;
  694. }
  695. mutex_lock(&pp_handle->pp_lock);
  696. ret = hwmgr->hwmgr_func->get_sclk_od(hwmgr);
  697. mutex_unlock(&pp_handle->pp_lock);
  698. return ret;
  699. }
  700. static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
  701. {
  702. struct pp_hwmgr *hwmgr;
  703. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  704. int ret = 0;
  705. ret = pp_check(pp_handle);
  706. if (ret != 0)
  707. return ret;
  708. hwmgr = pp_handle->hwmgr;
  709. if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
  710. pr_info("%s was not implemented.\n", __func__);
  711. return 0;
  712. }
  713. mutex_lock(&pp_handle->pp_lock);
  714. ret = hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
  715. mutex_unlock(&pp_handle->pp_lock);
  716. return ret;
  717. }
  718. static int pp_dpm_get_mclk_od(void *handle)
  719. {
  720. struct pp_hwmgr *hwmgr;
  721. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  722. int ret = 0;
  723. ret = pp_check(pp_handle);
  724. if (ret != 0)
  725. return ret;
  726. hwmgr = pp_handle->hwmgr;
  727. if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
  728. pr_info("%s was not implemented.\n", __func__);
  729. return 0;
  730. }
  731. mutex_lock(&pp_handle->pp_lock);
  732. ret = hwmgr->hwmgr_func->get_mclk_od(hwmgr);
  733. mutex_unlock(&pp_handle->pp_lock);
  734. return ret;
  735. }
  736. static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
  737. {
  738. struct pp_hwmgr *hwmgr;
  739. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  740. int ret = 0;
  741. ret = pp_check(pp_handle);
  742. if (ret != 0)
  743. return ret;
  744. hwmgr = pp_handle->hwmgr;
  745. if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
  746. pr_info("%s was not implemented.\n", __func__);
  747. return 0;
  748. }
  749. mutex_lock(&pp_handle->pp_lock);
  750. ret = hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
  751. mutex_unlock(&pp_handle->pp_lock);
  752. return ret;
  753. }
  754. static int pp_dpm_read_sensor(void *handle, int idx,
  755. void *value, int *size)
  756. {
  757. struct pp_hwmgr *hwmgr;
  758. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  759. int ret = 0;
  760. ret = pp_check(pp_handle);
  761. if (ret != 0)
  762. return ret;
  763. hwmgr = pp_handle->hwmgr;
  764. if (hwmgr->hwmgr_func->read_sensor == NULL) {
  765. pr_info("%s was not implemented.\n", __func__);
  766. return 0;
  767. }
  768. mutex_lock(&pp_handle->pp_lock);
  769. ret = hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
  770. mutex_unlock(&pp_handle->pp_lock);
  771. return ret;
  772. }
  773. static struct amd_vce_state*
  774. pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
  775. {
  776. struct pp_hwmgr *hwmgr;
  777. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  778. int ret = 0;
  779. ret = pp_check(pp_handle);
  780. if (ret != 0)
  781. return NULL;
  782. hwmgr = pp_handle->hwmgr;
  783. if (hwmgr && idx < hwmgr->num_vce_state_tables)
  784. return &hwmgr->vce_states[idx];
  785. return NULL;
  786. }
  787. static int pp_dpm_reset_power_profile_state(void *handle,
  788. struct amd_pp_profile *request)
  789. {
  790. struct pp_hwmgr *hwmgr;
  791. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  792. if (!request || pp_check(pp_handle))
  793. return -EINVAL;
  794. hwmgr = pp_handle->hwmgr;
  795. if (hwmgr->hwmgr_func->set_power_profile_state == NULL) {
  796. pr_info("%s was not implemented.\n", __func__);
  797. return 0;
  798. }
  799. if (request->type == AMD_PP_GFX_PROFILE) {
  800. hwmgr->gfx_power_profile = hwmgr->default_gfx_power_profile;
  801. return hwmgr->hwmgr_func->set_power_profile_state(hwmgr,
  802. &hwmgr->gfx_power_profile);
  803. } else if (request->type == AMD_PP_COMPUTE_PROFILE) {
  804. hwmgr->compute_power_profile =
  805. hwmgr->default_compute_power_profile;
  806. return hwmgr->hwmgr_func->set_power_profile_state(hwmgr,
  807. &hwmgr->compute_power_profile);
  808. } else
  809. return -EINVAL;
  810. }
  811. static int pp_dpm_get_power_profile_state(void *handle,
  812. struct amd_pp_profile *query)
  813. {
  814. struct pp_hwmgr *hwmgr;
  815. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  816. if (!query || pp_check(pp_handle))
  817. return -EINVAL;
  818. hwmgr = pp_handle->hwmgr;
  819. if (query->type == AMD_PP_GFX_PROFILE)
  820. memcpy(query, &hwmgr->gfx_power_profile,
  821. sizeof(struct amd_pp_profile));
  822. else if (query->type == AMD_PP_COMPUTE_PROFILE)
  823. memcpy(query, &hwmgr->compute_power_profile,
  824. sizeof(struct amd_pp_profile));
  825. else
  826. return -EINVAL;
  827. return 0;
  828. }
  829. static int pp_dpm_set_power_profile_state(void *handle,
  830. struct amd_pp_profile *request)
  831. {
  832. struct pp_hwmgr *hwmgr;
  833. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  834. int ret = -1;
  835. if (!request || pp_check(pp_handle))
  836. return -EINVAL;
  837. hwmgr = pp_handle->hwmgr;
  838. if (hwmgr->hwmgr_func->set_power_profile_state == NULL) {
  839. pr_info("%s was not implemented.\n", __func__);
  840. return 0;
  841. }
  842. if (request->min_sclk ||
  843. request->min_mclk ||
  844. request->activity_threshold ||
  845. request->up_hyst ||
  846. request->down_hyst) {
  847. if (request->type == AMD_PP_GFX_PROFILE)
  848. memcpy(&hwmgr->gfx_power_profile, request,
  849. sizeof(struct amd_pp_profile));
  850. else if (request->type == AMD_PP_COMPUTE_PROFILE)
  851. memcpy(&hwmgr->compute_power_profile, request,
  852. sizeof(struct amd_pp_profile));
  853. else
  854. return -EINVAL;
  855. if (request->type == hwmgr->current_power_profile)
  856. ret = hwmgr->hwmgr_func->set_power_profile_state(
  857. hwmgr,
  858. request);
  859. } else {
  860. /* set power profile if it exists */
  861. switch (request->type) {
  862. case AMD_PP_GFX_PROFILE:
  863. ret = hwmgr->hwmgr_func->set_power_profile_state(
  864. hwmgr,
  865. &hwmgr->gfx_power_profile);
  866. break;
  867. case AMD_PP_COMPUTE_PROFILE:
  868. ret = hwmgr->hwmgr_func->set_power_profile_state(
  869. hwmgr,
  870. &hwmgr->compute_power_profile);
  871. break;
  872. default:
  873. return -EINVAL;
  874. }
  875. }
  876. if (!ret)
  877. hwmgr->current_power_profile = request->type;
  878. return 0;
  879. }
  880. static int pp_dpm_switch_power_profile(void *handle,
  881. enum amd_pp_profile_type type)
  882. {
  883. struct pp_hwmgr *hwmgr;
  884. struct amd_pp_profile request = {0};
  885. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  886. if (pp_check(pp_handle))
  887. return -EINVAL;
  888. hwmgr = pp_handle->hwmgr;
  889. if (hwmgr->current_power_profile != type) {
  890. request.type = type;
  891. pp_dpm_set_power_profile_state(handle, &request);
  892. }
  893. return 0;
  894. }
  895. const struct amd_powerplay_funcs pp_dpm_funcs = {
  896. .get_temperature = pp_dpm_get_temperature,
  897. .load_firmware = pp_dpm_load_fw,
  898. .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
  899. .force_performance_level = pp_dpm_force_performance_level,
  900. .get_performance_level = pp_dpm_get_performance_level,
  901. .get_current_power_state = pp_dpm_get_current_power_state,
  902. .get_sclk = pp_dpm_get_sclk,
  903. .get_mclk = pp_dpm_get_mclk,
  904. .powergate_vce = pp_dpm_powergate_vce,
  905. .powergate_uvd = pp_dpm_powergate_uvd,
  906. .dispatch_tasks = pp_dpm_dispatch_tasks,
  907. .set_fan_control_mode = pp_dpm_set_fan_control_mode,
  908. .get_fan_control_mode = pp_dpm_get_fan_control_mode,
  909. .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
  910. .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
  911. .get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
  912. .get_pp_num_states = pp_dpm_get_pp_num_states,
  913. .get_pp_table = pp_dpm_get_pp_table,
  914. .set_pp_table = pp_dpm_set_pp_table,
  915. .force_clock_level = pp_dpm_force_clock_level,
  916. .print_clock_levels = pp_dpm_print_clock_levels,
  917. .get_sclk_od = pp_dpm_get_sclk_od,
  918. .set_sclk_od = pp_dpm_set_sclk_od,
  919. .get_mclk_od = pp_dpm_get_mclk_od,
  920. .set_mclk_od = pp_dpm_set_mclk_od,
  921. .read_sensor = pp_dpm_read_sensor,
  922. .get_vce_clock_state = pp_dpm_get_vce_clock_state,
  923. .reset_power_profile_state = pp_dpm_reset_power_profile_state,
  924. .get_power_profile_state = pp_dpm_get_power_profile_state,
  925. .set_power_profile_state = pp_dpm_set_power_profile_state,
  926. .switch_power_profile = pp_dpm_switch_power_profile,
  927. };
  928. int amd_powerplay_create(struct amd_pp_init *pp_init,
  929. void **handle)
  930. {
  931. struct pp_instance *instance;
  932. if (pp_init == NULL || handle == NULL)
  933. return -EINVAL;
  934. instance = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
  935. if (instance == NULL)
  936. return -ENOMEM;
  937. instance->pp_valid = PP_VALID;
  938. instance->chip_family = pp_init->chip_family;
  939. instance->chip_id = pp_init->chip_id;
  940. instance->pm_en = pp_init->pm_en;
  941. instance->feature_mask = pp_init->feature_mask;
  942. instance->device = pp_init->device;
  943. mutex_init(&instance->pp_lock);
  944. *handle = instance;
  945. return 0;
  946. }
  947. int amd_powerplay_destroy(void *handle)
  948. {
  949. struct pp_instance *instance = (struct pp_instance *)handle;
  950. if (instance->pm_en) {
  951. kfree(instance->eventmgr);
  952. kfree(instance->hwmgr);
  953. instance->hwmgr = NULL;
  954. instance->eventmgr = NULL;
  955. }
  956. kfree(instance->smu_mgr);
  957. instance->smu_mgr = NULL;
  958. kfree(instance);
  959. instance = NULL;
  960. return 0;
  961. }
  962. int amd_powerplay_reset(void *handle)
  963. {
  964. struct pp_instance *instance = (struct pp_instance *)handle;
  965. struct pp_eventmgr *eventmgr;
  966. struct pem_event_data event_data = { {0} };
  967. int ret;
  968. if (cgs_is_virtualization_enabled(instance->smu_mgr->device))
  969. return PP_DPM_DISABLED;
  970. ret = pp_check(instance);
  971. if (ret != 0)
  972. return ret;
  973. ret = pp_hw_fini(handle);
  974. if (ret)
  975. return ret;
  976. ret = hwmgr_hw_init(instance);
  977. if (ret)
  978. return PP_DPM_DISABLED;
  979. eventmgr = instance->eventmgr;
  980. if (eventmgr->pp_eventmgr_init == NULL)
  981. return PP_DPM_DISABLED;
  982. ret = eventmgr->pp_eventmgr_init(eventmgr);
  983. if (ret)
  984. return ret;
  985. return pem_handle_event(eventmgr, AMD_PP_EVENT_COMPLETE_INIT, &event_data);
  986. }
  987. /* export this function to DAL */
  988. int amd_powerplay_display_configuration_change(void *handle,
  989. const struct amd_pp_display_configuration *display_config)
  990. {
  991. struct pp_hwmgr *hwmgr;
  992. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  993. int ret = 0;
  994. ret = pp_check(pp_handle);
  995. if (ret != 0)
  996. return ret;
  997. hwmgr = pp_handle->hwmgr;
  998. mutex_lock(&pp_handle->pp_lock);
  999. phm_store_dal_configuration_data(hwmgr, display_config);
  1000. mutex_unlock(&pp_handle->pp_lock);
  1001. return 0;
  1002. }
  1003. int amd_powerplay_get_display_power_level(void *handle,
  1004. struct amd_pp_simple_clock_info *output)
  1005. {
  1006. struct pp_hwmgr *hwmgr;
  1007. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  1008. int ret = 0;
  1009. ret = pp_check(pp_handle);
  1010. if (ret != 0)
  1011. return ret;
  1012. hwmgr = pp_handle->hwmgr;
  1013. if (output == NULL)
  1014. return -EINVAL;
  1015. mutex_lock(&pp_handle->pp_lock);
  1016. ret = phm_get_dal_power_level(hwmgr, output);
  1017. mutex_unlock(&pp_handle->pp_lock);
  1018. return ret;
  1019. }
  1020. int amd_powerplay_get_current_clocks(void *handle,
  1021. struct amd_pp_clock_info *clocks)
  1022. {
  1023. struct amd_pp_simple_clock_info simple_clocks;
  1024. struct pp_clock_info hw_clocks;
  1025. struct pp_hwmgr *hwmgr;
  1026. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  1027. int ret = 0;
  1028. ret = pp_check(pp_handle);
  1029. if (ret != 0)
  1030. return ret;
  1031. hwmgr = pp_handle->hwmgr;
  1032. mutex_lock(&pp_handle->pp_lock);
  1033. phm_get_dal_power_level(hwmgr, &simple_clocks);
  1034. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1035. PHM_PlatformCaps_PowerContainment))
  1036. ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
  1037. &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment);
  1038. else
  1039. ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
  1040. &hw_clocks, PHM_PerformanceLevelDesignation_Activity);
  1041. if (ret != 0) {
  1042. pr_info("Error in phm_get_clock_info \n");
  1043. mutex_unlock(&pp_handle->pp_lock);
  1044. return -EINVAL;
  1045. }
  1046. clocks->min_engine_clock = hw_clocks.min_eng_clk;
  1047. clocks->max_engine_clock = hw_clocks.max_eng_clk;
  1048. clocks->min_memory_clock = hw_clocks.min_mem_clk;
  1049. clocks->max_memory_clock = hw_clocks.max_mem_clk;
  1050. clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
  1051. clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
  1052. clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
  1053. clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
  1054. clocks->max_clocks_state = simple_clocks.level;
  1055. if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
  1056. clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
  1057. clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
  1058. }
  1059. mutex_unlock(&pp_handle->pp_lock);
  1060. return 0;
  1061. }
  1062. int amd_powerplay_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
  1063. {
  1064. struct pp_hwmgr *hwmgr;
  1065. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  1066. int ret = 0;
  1067. ret = pp_check(pp_handle);
  1068. if (ret != 0)
  1069. return ret;
  1070. hwmgr = pp_handle->hwmgr;
  1071. if (clocks == NULL)
  1072. return -EINVAL;
  1073. mutex_lock(&pp_handle->pp_lock);
  1074. ret = phm_get_clock_by_type(hwmgr, type, clocks);
  1075. mutex_unlock(&pp_handle->pp_lock);
  1076. return ret;
  1077. }
  1078. int amd_powerplay_get_clock_by_type_with_latency(void *handle,
  1079. enum amd_pp_clock_type type,
  1080. struct pp_clock_levels_with_latency *clocks)
  1081. {
  1082. struct pp_hwmgr *hwmgr;
  1083. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  1084. int ret = 0;
  1085. ret = pp_check(pp_handle);
  1086. if (ret != 0)
  1087. return ret;
  1088. if (!clocks)
  1089. return -EINVAL;
  1090. mutex_lock(&pp_handle->pp_lock);
  1091. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  1092. ret = phm_get_clock_by_type_with_latency(hwmgr, type, clocks);
  1093. mutex_unlock(&pp_handle->pp_lock);
  1094. return ret;
  1095. }
  1096. int amd_powerplay_get_clock_by_type_with_voltage(void *handle,
  1097. enum amd_pp_clock_type type,
  1098. struct pp_clock_levels_with_voltage *clocks)
  1099. {
  1100. struct pp_hwmgr *hwmgr;
  1101. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  1102. int ret = 0;
  1103. ret = pp_check(pp_handle);
  1104. if (ret != 0)
  1105. return ret;
  1106. if (!clocks)
  1107. return -EINVAL;
  1108. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  1109. mutex_lock(&pp_handle->pp_lock);
  1110. ret = phm_get_clock_by_type_with_voltage(hwmgr, type, clocks);
  1111. mutex_unlock(&pp_handle->pp_lock);
  1112. return ret;
  1113. }
  1114. int amd_powerplay_set_watermarks_for_clocks_ranges(void *handle,
  1115. struct pp_wm_sets_with_clock_ranges_soc15 *wm_with_clock_ranges)
  1116. {
  1117. struct pp_hwmgr *hwmgr;
  1118. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  1119. int ret = 0;
  1120. ret = pp_check(pp_handle);
  1121. if (ret != 0)
  1122. return ret;
  1123. if (!wm_with_clock_ranges)
  1124. return -EINVAL;
  1125. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  1126. mutex_lock(&pp_handle->pp_lock);
  1127. ret = phm_set_watermarks_for_clocks_ranges(hwmgr,
  1128. wm_with_clock_ranges);
  1129. mutex_unlock(&pp_handle->pp_lock);
  1130. return ret;
  1131. }
  1132. int amd_powerplay_display_clock_voltage_request(void *handle,
  1133. struct pp_display_clock_request *clock)
  1134. {
  1135. struct pp_hwmgr *hwmgr;
  1136. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  1137. int ret = 0;
  1138. ret = pp_check(pp_handle);
  1139. if (ret != 0)
  1140. return ret;
  1141. if (!clock)
  1142. return -EINVAL;
  1143. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  1144. mutex_lock(&pp_handle->pp_lock);
  1145. ret = phm_display_clock_voltage_request(hwmgr, clock);
  1146. mutex_unlock(&pp_handle->pp_lock);
  1147. return ret;
  1148. }
  1149. int amd_powerplay_get_display_mode_validation_clocks(void *handle,
  1150. struct amd_pp_simple_clock_info *clocks)
  1151. {
  1152. struct pp_hwmgr *hwmgr;
  1153. struct pp_instance *pp_handle = (struct pp_instance *)handle;
  1154. int ret = 0;
  1155. ret = pp_check(pp_handle);
  1156. if (ret != 0)
  1157. return ret;
  1158. hwmgr = pp_handle->hwmgr;
  1159. if (clocks == NULL)
  1160. return -EINVAL;
  1161. mutex_lock(&pp_handle->pp_lock);
  1162. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
  1163. ret = phm_get_max_high_clocks(hwmgr, clocks);
  1164. mutex_unlock(&pp_handle->pp_lock);
  1165. return ret;
  1166. }