amd_powerplay.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/gfp.h>
  26. #include <linux/slab.h>
  27. #include "amd_shared.h"
  28. #include "amd_powerplay.h"
  29. #include "pp_instance.h"
  30. #include "power_state.h"
  31. #include "eventmanager.h"
  32. #include "pp_debug.h"
  33. #define PP_CHECK(handle) \
  34. do { \
  35. if ((handle) == NULL || (handle)->pp_valid != PP_VALID) \
  36. return -EINVAL; \
  37. } while (0)
  38. #define PP_CHECK_HW(hwmgr) \
  39. do { \
  40. if ((hwmgr) == NULL || (hwmgr)->hwmgr_func == NULL) \
  41. return -EINVAL; \
  42. } while (0)
  43. static int pp_early_init(void *handle)
  44. {
  45. return 0;
  46. }
  47. static int pp_sw_init(void *handle)
  48. {
  49. struct pp_instance *pp_handle;
  50. struct pp_hwmgr *hwmgr;
  51. int ret = 0;
  52. if (handle == NULL)
  53. return -EINVAL;
  54. pp_handle = (struct pp_instance *)handle;
  55. hwmgr = pp_handle->hwmgr;
  56. PP_CHECK_HW(hwmgr);
  57. if (hwmgr->pptable_func == NULL ||
  58. hwmgr->pptable_func->pptable_init == NULL ||
  59. hwmgr->hwmgr_func->backend_init == NULL)
  60. return -EINVAL;
  61. ret = hwmgr->pptable_func->pptable_init(hwmgr);
  62. if (ret)
  63. goto err;
  64. ret = hwmgr->hwmgr_func->backend_init(hwmgr);
  65. if (ret)
  66. goto err;
  67. pr_info("amdgpu: powerplay initialized\n");
  68. return 0;
  69. err:
  70. pr_err("amdgpu: powerplay initialization failed\n");
  71. return ret;
  72. }
  73. static int pp_sw_fini(void *handle)
  74. {
  75. struct pp_instance *pp_handle;
  76. struct pp_hwmgr *hwmgr;
  77. int ret = 0;
  78. if (handle == NULL)
  79. return -EINVAL;
  80. pp_handle = (struct pp_instance *)handle;
  81. hwmgr = pp_handle->hwmgr;
  82. PP_CHECK_HW(hwmgr);
  83. if (hwmgr->hwmgr_func->backend_fini != NULL)
  84. ret = hwmgr->hwmgr_func->backend_fini(hwmgr);
  85. return ret;
  86. }
  87. static int pp_hw_init(void *handle)
  88. {
  89. struct pp_instance *pp_handle;
  90. struct pp_smumgr *smumgr;
  91. struct pp_eventmgr *eventmgr;
  92. int ret = 0;
  93. if (handle == NULL)
  94. return -EINVAL;
  95. pp_handle = (struct pp_instance *)handle;
  96. smumgr = pp_handle->smu_mgr;
  97. if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
  98. smumgr->smumgr_funcs->smu_init == NULL ||
  99. smumgr->smumgr_funcs->start_smu == NULL)
  100. return -EINVAL;
  101. ret = smumgr->smumgr_funcs->smu_init(smumgr);
  102. if (ret) {
  103. printk(KERN_ERR "[ powerplay ] smc initialization failed\n");
  104. return ret;
  105. }
  106. ret = smumgr->smumgr_funcs->start_smu(smumgr);
  107. if (ret) {
  108. printk(KERN_ERR "[ powerplay ] smc start failed\n");
  109. smumgr->smumgr_funcs->smu_fini(smumgr);
  110. return ret;
  111. }
  112. hw_init_power_state_table(pp_handle->hwmgr);
  113. eventmgr = pp_handle->eventmgr;
  114. if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
  115. return -EINVAL;
  116. ret = eventmgr->pp_eventmgr_init(eventmgr);
  117. return 0;
  118. }
  119. static int pp_hw_fini(void *handle)
  120. {
  121. struct pp_instance *pp_handle;
  122. struct pp_smumgr *smumgr;
  123. struct pp_eventmgr *eventmgr;
  124. if (handle == NULL)
  125. return -EINVAL;
  126. pp_handle = (struct pp_instance *)handle;
  127. eventmgr = pp_handle->eventmgr;
  128. if (eventmgr != NULL || eventmgr->pp_eventmgr_fini != NULL)
  129. eventmgr->pp_eventmgr_fini(eventmgr);
  130. smumgr = pp_handle->smu_mgr;
  131. if (smumgr != NULL || smumgr->smumgr_funcs != NULL ||
  132. smumgr->smumgr_funcs->smu_fini != NULL)
  133. smumgr->smumgr_funcs->smu_fini(smumgr);
  134. return 0;
  135. }
  136. static bool pp_is_idle(void *handle)
  137. {
  138. return 0;
  139. }
  140. static int pp_wait_for_idle(void *handle)
  141. {
  142. return 0;
  143. }
  144. static int pp_sw_reset(void *handle)
  145. {
  146. return 0;
  147. }
  148. static int pp_set_clockgating_state(void *handle,
  149. enum amd_clockgating_state state)
  150. {
  151. struct pp_hwmgr *hwmgr;
  152. uint32_t msg_id, pp_state;
  153. if (handle == NULL)
  154. return -EINVAL;
  155. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  156. PP_CHECK_HW(hwmgr);
  157. if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
  158. printk(KERN_INFO "%s was not implemented.\n", __func__);
  159. return 0;
  160. }
  161. if (state == AMD_CG_STATE_UNGATE)
  162. pp_state = 0;
  163. else
  164. pp_state = PP_STATE_CG | PP_STATE_LS;
  165. /* Enable/disable GFX blocks clock gating through SMU */
  166. msg_id = PP_CG_MSG_ID(PP_GROUP_GFX,
  167. PP_BLOCK_GFX_CG,
  168. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  169. pp_state);
  170. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  171. msg_id = PP_CG_MSG_ID(PP_GROUP_GFX,
  172. PP_BLOCK_GFX_3D,
  173. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  174. pp_state);
  175. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  176. msg_id = PP_CG_MSG_ID(PP_GROUP_GFX,
  177. PP_BLOCK_GFX_RLC,
  178. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  179. pp_state);
  180. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  181. msg_id = PP_CG_MSG_ID(PP_GROUP_GFX,
  182. PP_BLOCK_GFX_CP,
  183. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  184. pp_state);
  185. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  186. msg_id = PP_CG_MSG_ID(PP_GROUP_GFX,
  187. PP_BLOCK_GFX_MG,
  188. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  189. pp_state);
  190. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  191. /* Enable/disable System blocks clock gating through SMU */
  192. msg_id = PP_CG_MSG_ID(PP_GROUP_SYS,
  193. PP_BLOCK_SYS_BIF,
  194. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  195. pp_state);
  196. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  197. msg_id = PP_CG_MSG_ID(PP_GROUP_SYS,
  198. PP_BLOCK_SYS_BIF,
  199. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  200. pp_state);
  201. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  202. msg_id = PP_CG_MSG_ID(PP_GROUP_SYS,
  203. PP_BLOCK_SYS_MC,
  204. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  205. pp_state);
  206. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  207. msg_id = PP_CG_MSG_ID(PP_GROUP_SYS,
  208. PP_BLOCK_SYS_ROM,
  209. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  210. pp_state);
  211. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  212. msg_id = PP_CG_MSG_ID(PP_GROUP_SYS,
  213. PP_BLOCK_SYS_DRM,
  214. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  215. pp_state);
  216. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  217. msg_id = PP_CG_MSG_ID(PP_GROUP_SYS,
  218. PP_BLOCK_SYS_HDP,
  219. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  220. pp_state);
  221. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  222. msg_id = PP_CG_MSG_ID(PP_GROUP_SYS,
  223. PP_BLOCK_SYS_SDMA,
  224. PP_STATE_SUPPORT_CG | PP_STATE_SUPPORT_LS,
  225. pp_state);
  226. hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  227. return 0;
  228. }
  229. static int pp_set_powergating_state(void *handle,
  230. enum amd_powergating_state state)
  231. {
  232. struct pp_hwmgr *hwmgr;
  233. if (handle == NULL)
  234. return -EINVAL;
  235. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  236. PP_CHECK_HW(hwmgr);
  237. if (hwmgr->hwmgr_func->enable_per_cu_power_gating == NULL) {
  238. printk(KERN_INFO "%s was not implemented.\n", __func__);
  239. return 0;
  240. }
  241. /* Enable/disable GFX per cu powergating through SMU */
  242. return hwmgr->hwmgr_func->enable_per_cu_power_gating(hwmgr,
  243. state == AMD_PG_STATE_GATE ? true : false);
  244. }
  245. static int pp_suspend(void *handle)
  246. {
  247. struct pp_instance *pp_handle;
  248. struct pp_eventmgr *eventmgr;
  249. struct pem_event_data event_data = { {0} };
  250. if (handle == NULL)
  251. return -EINVAL;
  252. pp_handle = (struct pp_instance *)handle;
  253. eventmgr = pp_handle->eventmgr;
  254. pem_handle_event(eventmgr, AMD_PP_EVENT_SUSPEND, &event_data);
  255. return 0;
  256. }
  257. static int pp_resume(void *handle)
  258. {
  259. struct pp_instance *pp_handle;
  260. struct pp_eventmgr *eventmgr;
  261. struct pem_event_data event_data = { {0} };
  262. struct pp_smumgr *smumgr;
  263. int ret;
  264. if (handle == NULL)
  265. return -EINVAL;
  266. pp_handle = (struct pp_instance *)handle;
  267. smumgr = pp_handle->smu_mgr;
  268. if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
  269. smumgr->smumgr_funcs->start_smu == NULL)
  270. return -EINVAL;
  271. ret = smumgr->smumgr_funcs->start_smu(smumgr);
  272. if (ret) {
  273. printk(KERN_ERR "[ powerplay ] smc start failed\n");
  274. smumgr->smumgr_funcs->smu_fini(smumgr);
  275. return ret;
  276. }
  277. eventmgr = pp_handle->eventmgr;
  278. pem_handle_event(eventmgr, AMD_PP_EVENT_RESUME, &event_data);
  279. return 0;
  280. }
  281. const struct amd_ip_funcs pp_ip_funcs = {
  282. .name = "powerplay",
  283. .early_init = pp_early_init,
  284. .late_init = NULL,
  285. .sw_init = pp_sw_init,
  286. .sw_fini = pp_sw_fini,
  287. .hw_init = pp_hw_init,
  288. .hw_fini = pp_hw_fini,
  289. .suspend = pp_suspend,
  290. .resume = pp_resume,
  291. .is_idle = pp_is_idle,
  292. .wait_for_idle = pp_wait_for_idle,
  293. .soft_reset = pp_sw_reset,
  294. .set_clockgating_state = pp_set_clockgating_state,
  295. .set_powergating_state = pp_set_powergating_state,
  296. };
  297. static int pp_dpm_load_fw(void *handle)
  298. {
  299. return 0;
  300. }
  301. static int pp_dpm_fw_loading_complete(void *handle)
  302. {
  303. return 0;
  304. }
  305. static int pp_dpm_force_performance_level(void *handle,
  306. enum amd_dpm_forced_level level)
  307. {
  308. struct pp_instance *pp_handle;
  309. struct pp_hwmgr *hwmgr;
  310. if (handle == NULL)
  311. return -EINVAL;
  312. pp_handle = (struct pp_instance *)handle;
  313. hwmgr = pp_handle->hwmgr;
  314. PP_CHECK_HW(hwmgr);
  315. if (hwmgr->hwmgr_func->force_dpm_level == NULL) {
  316. printk(KERN_INFO "%s was not implemented.\n", __func__);
  317. return 0;
  318. }
  319. hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
  320. return 0;
  321. }
  322. static enum amd_dpm_forced_level pp_dpm_get_performance_level(
  323. void *handle)
  324. {
  325. struct pp_hwmgr *hwmgr;
  326. if (handle == NULL)
  327. return -EINVAL;
  328. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  329. if (hwmgr == NULL)
  330. return -EINVAL;
  331. return (((struct pp_instance *)handle)->hwmgr->dpm_level);
  332. }
  333. static int pp_dpm_get_sclk(void *handle, bool low)
  334. {
  335. struct pp_hwmgr *hwmgr;
  336. if (handle == NULL)
  337. return -EINVAL;
  338. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  339. PP_CHECK_HW(hwmgr);
  340. if (hwmgr->hwmgr_func->get_sclk == NULL) {
  341. printk(KERN_INFO "%s was not implemented.\n", __func__);
  342. return 0;
  343. }
  344. return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
  345. }
  346. static int pp_dpm_get_mclk(void *handle, bool low)
  347. {
  348. struct pp_hwmgr *hwmgr;
  349. if (handle == NULL)
  350. return -EINVAL;
  351. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  352. PP_CHECK_HW(hwmgr);
  353. if (hwmgr->hwmgr_func->get_mclk == NULL) {
  354. printk(KERN_INFO "%s was not implemented.\n", __func__);
  355. return 0;
  356. }
  357. return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
  358. }
  359. static int pp_dpm_powergate_vce(void *handle, bool gate)
  360. {
  361. struct pp_hwmgr *hwmgr;
  362. if (handle == NULL)
  363. return -EINVAL;
  364. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  365. PP_CHECK_HW(hwmgr);
  366. if (hwmgr->hwmgr_func->powergate_vce == NULL) {
  367. printk(KERN_INFO "%s was not implemented.\n", __func__);
  368. return 0;
  369. }
  370. return hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
  371. }
  372. static int pp_dpm_powergate_uvd(void *handle, bool gate)
  373. {
  374. struct pp_hwmgr *hwmgr;
  375. if (handle == NULL)
  376. return -EINVAL;
  377. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  378. PP_CHECK_HW(hwmgr);
  379. if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
  380. printk(KERN_INFO "%s was not implemented.\n", __func__);
  381. return 0;
  382. }
  383. return hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
  384. }
  385. static enum PP_StateUILabel power_state_convert(enum amd_pm_state_type state)
  386. {
  387. switch (state) {
  388. case POWER_STATE_TYPE_BATTERY:
  389. return PP_StateUILabel_Battery;
  390. case POWER_STATE_TYPE_BALANCED:
  391. return PP_StateUILabel_Balanced;
  392. case POWER_STATE_TYPE_PERFORMANCE:
  393. return PP_StateUILabel_Performance;
  394. default:
  395. return PP_StateUILabel_None;
  396. }
  397. }
  398. int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id, void *input, void *output)
  399. {
  400. int ret = 0;
  401. struct pp_instance *pp_handle;
  402. struct pem_event_data data = { {0} };
  403. pp_handle = (struct pp_instance *)handle;
  404. if (pp_handle == NULL)
  405. return -EINVAL;
  406. switch (event_id) {
  407. case AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE:
  408. ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
  409. break;
  410. case AMD_PP_EVENT_ENABLE_USER_STATE:
  411. {
  412. enum amd_pm_state_type ps;
  413. if (input == NULL)
  414. return -EINVAL;
  415. ps = *(unsigned long *)input;
  416. data.requested_ui_label = power_state_convert(ps);
  417. ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
  418. break;
  419. }
  420. case AMD_PP_EVENT_COMPLETE_INIT:
  421. ret = pem_handle_event(pp_handle->eventmgr, event_id, &data);
  422. break;
  423. default:
  424. break;
  425. }
  426. return ret;
  427. }
  428. enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
  429. {
  430. struct pp_hwmgr *hwmgr;
  431. struct pp_power_state *state;
  432. if (handle == NULL)
  433. return -EINVAL;
  434. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  435. if (hwmgr == NULL || hwmgr->current_ps == NULL)
  436. return -EINVAL;
  437. state = hwmgr->current_ps;
  438. switch (state->classification.ui_label) {
  439. case PP_StateUILabel_Battery:
  440. return POWER_STATE_TYPE_BATTERY;
  441. case PP_StateUILabel_Balanced:
  442. return POWER_STATE_TYPE_BALANCED;
  443. case PP_StateUILabel_Performance:
  444. return POWER_STATE_TYPE_PERFORMANCE;
  445. default:
  446. if (state->classification.flags & PP_StateClassificationFlag_Boot)
  447. return POWER_STATE_TYPE_INTERNAL_BOOT;
  448. else
  449. return POWER_STATE_TYPE_DEFAULT;
  450. }
  451. }
  452. static void
  453. pp_debugfs_print_current_performance_level(void *handle,
  454. struct seq_file *m)
  455. {
  456. struct pp_hwmgr *hwmgr;
  457. if (handle == NULL)
  458. return;
  459. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  460. if (hwmgr == NULL || hwmgr->hwmgr_func == NULL)
  461. return;
  462. if (hwmgr->hwmgr_func->print_current_perforce_level == NULL) {
  463. printk(KERN_INFO "%s was not implemented.\n", __func__);
  464. return;
  465. }
  466. hwmgr->hwmgr_func->print_current_perforce_level(hwmgr, m);
  467. }
  468. static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
  469. {
  470. struct pp_hwmgr *hwmgr;
  471. if (handle == NULL)
  472. return -EINVAL;
  473. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  474. PP_CHECK_HW(hwmgr);
  475. if (hwmgr->hwmgr_func->set_fan_control_mode == NULL) {
  476. printk(KERN_INFO "%s was not implemented.\n", __func__);
  477. return 0;
  478. }
  479. return hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
  480. }
  481. static int pp_dpm_get_fan_control_mode(void *handle)
  482. {
  483. struct pp_hwmgr *hwmgr;
  484. if (handle == NULL)
  485. return -EINVAL;
  486. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  487. PP_CHECK_HW(hwmgr);
  488. if (hwmgr->hwmgr_func->get_fan_control_mode == NULL) {
  489. printk(KERN_INFO "%s was not implemented.\n", __func__);
  490. return 0;
  491. }
  492. return hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
  493. }
  494. static int pp_dpm_set_fan_speed_percent(void *handle, uint32_t percent)
  495. {
  496. struct pp_hwmgr *hwmgr;
  497. if (handle == NULL)
  498. return -EINVAL;
  499. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  500. PP_CHECK_HW(hwmgr);
  501. if (hwmgr->hwmgr_func->set_fan_speed_percent == NULL) {
  502. printk(KERN_INFO "%s was not implemented.\n", __func__);
  503. return 0;
  504. }
  505. return hwmgr->hwmgr_func->set_fan_speed_percent(hwmgr, percent);
  506. }
  507. static int pp_dpm_get_fan_speed_percent(void *handle, uint32_t *speed)
  508. {
  509. struct pp_hwmgr *hwmgr;
  510. if (handle == NULL)
  511. return -EINVAL;
  512. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  513. PP_CHECK_HW(hwmgr);
  514. if (hwmgr->hwmgr_func->get_fan_speed_percent == NULL) {
  515. printk(KERN_INFO "%s was not implemented.\n", __func__);
  516. return 0;
  517. }
  518. return hwmgr->hwmgr_func->get_fan_speed_percent(hwmgr, speed);
  519. }
  520. static int pp_dpm_get_temperature(void *handle)
  521. {
  522. struct pp_hwmgr *hwmgr;
  523. if (handle == NULL)
  524. return -EINVAL;
  525. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  526. PP_CHECK_HW(hwmgr);
  527. if (hwmgr->hwmgr_func->get_temperature == NULL) {
  528. printk(KERN_INFO "%s was not implemented.\n", __func__);
  529. return 0;
  530. }
  531. return hwmgr->hwmgr_func->get_temperature(hwmgr);
  532. }
  533. static int pp_dpm_get_pp_num_states(void *handle,
  534. struct pp_states_info *data)
  535. {
  536. struct pp_hwmgr *hwmgr;
  537. int i;
  538. if (!handle)
  539. return -EINVAL;
  540. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  541. if (hwmgr == NULL || hwmgr->ps == NULL)
  542. return -EINVAL;
  543. data->nums = hwmgr->num_ps;
  544. for (i = 0; i < hwmgr->num_ps; i++) {
  545. struct pp_power_state *state = (struct pp_power_state *)
  546. ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
  547. switch (state->classification.ui_label) {
  548. case PP_StateUILabel_Battery:
  549. data->states[i] = POWER_STATE_TYPE_BATTERY;
  550. break;
  551. case PP_StateUILabel_Balanced:
  552. data->states[i] = POWER_STATE_TYPE_BALANCED;
  553. break;
  554. case PP_StateUILabel_Performance:
  555. data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
  556. break;
  557. default:
  558. if (state->classification.flags & PP_StateClassificationFlag_Boot)
  559. data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
  560. else
  561. data->states[i] = POWER_STATE_TYPE_DEFAULT;
  562. }
  563. }
  564. return 0;
  565. }
  566. static int pp_dpm_get_pp_table(void *handle, char **table)
  567. {
  568. struct pp_hwmgr *hwmgr;
  569. if (!handle)
  570. return -EINVAL;
  571. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  572. PP_CHECK_HW(hwmgr);
  573. if (hwmgr->hwmgr_func->get_pp_table == NULL) {
  574. printk(KERN_INFO "%s was not implemented.\n", __func__);
  575. return 0;
  576. }
  577. return hwmgr->hwmgr_func->get_pp_table(hwmgr, table);
  578. }
  579. static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
  580. {
  581. struct pp_hwmgr *hwmgr;
  582. if (!handle)
  583. return -EINVAL;
  584. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  585. PP_CHECK_HW(hwmgr);
  586. if (hwmgr->hwmgr_func->set_pp_table == NULL) {
  587. printk(KERN_INFO "%s was not implemented.\n", __func__);
  588. return 0;
  589. }
  590. return hwmgr->hwmgr_func->set_pp_table(hwmgr, buf, size);
  591. }
  592. static int pp_dpm_force_clock_level(void *handle,
  593. enum pp_clock_type type, uint32_t mask)
  594. {
  595. struct pp_hwmgr *hwmgr;
  596. if (!handle)
  597. return -EINVAL;
  598. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  599. PP_CHECK_HW(hwmgr);
  600. if (hwmgr->hwmgr_func->force_clock_level == NULL) {
  601. printk(KERN_INFO "%s was not implemented.\n", __func__);
  602. return 0;
  603. }
  604. return hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
  605. }
  606. static int pp_dpm_print_clock_levels(void *handle,
  607. enum pp_clock_type type, char *buf)
  608. {
  609. struct pp_hwmgr *hwmgr;
  610. if (!handle)
  611. return -EINVAL;
  612. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  613. PP_CHECK_HW(hwmgr);
  614. if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
  615. printk(KERN_INFO "%s was not implemented.\n", __func__);
  616. return 0;
  617. }
  618. return hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
  619. }
  620. const struct amd_powerplay_funcs pp_dpm_funcs = {
  621. .get_temperature = pp_dpm_get_temperature,
  622. .load_firmware = pp_dpm_load_fw,
  623. .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
  624. .force_performance_level = pp_dpm_force_performance_level,
  625. .get_performance_level = pp_dpm_get_performance_level,
  626. .get_current_power_state = pp_dpm_get_current_power_state,
  627. .get_sclk = pp_dpm_get_sclk,
  628. .get_mclk = pp_dpm_get_mclk,
  629. .powergate_vce = pp_dpm_powergate_vce,
  630. .powergate_uvd = pp_dpm_powergate_uvd,
  631. .dispatch_tasks = pp_dpm_dispatch_tasks,
  632. .print_current_performance_level = pp_debugfs_print_current_performance_level,
  633. .set_fan_control_mode = pp_dpm_set_fan_control_mode,
  634. .get_fan_control_mode = pp_dpm_get_fan_control_mode,
  635. .set_fan_speed_percent = pp_dpm_set_fan_speed_percent,
  636. .get_fan_speed_percent = pp_dpm_get_fan_speed_percent,
  637. .get_pp_num_states = pp_dpm_get_pp_num_states,
  638. .get_pp_table = pp_dpm_get_pp_table,
  639. .set_pp_table = pp_dpm_set_pp_table,
  640. .force_clock_level = pp_dpm_force_clock_level,
  641. .print_clock_levels = pp_dpm_print_clock_levels,
  642. };
  643. static int amd_pp_instance_init(struct amd_pp_init *pp_init,
  644. struct amd_powerplay *amd_pp)
  645. {
  646. int ret;
  647. struct pp_instance *handle;
  648. handle = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
  649. if (handle == NULL)
  650. return -ENOMEM;
  651. handle->pp_valid = PP_VALID;
  652. ret = smum_init(pp_init, handle);
  653. if (ret)
  654. goto fail_smum;
  655. ret = hwmgr_init(pp_init, handle);
  656. if (ret)
  657. goto fail_hwmgr;
  658. ret = eventmgr_init(handle);
  659. if (ret)
  660. goto fail_eventmgr;
  661. amd_pp->pp_handle = handle;
  662. return 0;
  663. fail_eventmgr:
  664. hwmgr_fini(handle->hwmgr);
  665. fail_hwmgr:
  666. smum_fini(handle->smu_mgr);
  667. fail_smum:
  668. kfree(handle);
  669. return ret;
  670. }
  671. static int amd_pp_instance_fini(void *handle)
  672. {
  673. struct pp_instance *instance = (struct pp_instance *)handle;
  674. if (instance == NULL)
  675. return -EINVAL;
  676. eventmgr_fini(instance->eventmgr);
  677. hwmgr_fini(instance->hwmgr);
  678. smum_fini(instance->smu_mgr);
  679. kfree(handle);
  680. return 0;
  681. }
  682. int amd_powerplay_init(struct amd_pp_init *pp_init,
  683. struct amd_powerplay *amd_pp)
  684. {
  685. int ret;
  686. if (pp_init == NULL || amd_pp == NULL)
  687. return -EINVAL;
  688. ret = amd_pp_instance_init(pp_init, amd_pp);
  689. if (ret)
  690. return ret;
  691. amd_pp->ip_funcs = &pp_ip_funcs;
  692. amd_pp->pp_funcs = &pp_dpm_funcs;
  693. return 0;
  694. }
  695. int amd_powerplay_fini(void *handle)
  696. {
  697. amd_pp_instance_fini(handle);
  698. return 0;
  699. }
  700. /* export this function to DAL */
  701. int amd_powerplay_display_configuration_change(void *handle,
  702. const struct amd_pp_display_configuration *display_config)
  703. {
  704. struct pp_hwmgr *hwmgr;
  705. PP_CHECK((struct pp_instance *)handle);
  706. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  707. phm_store_dal_configuration_data(hwmgr, display_config);
  708. return 0;
  709. }
  710. int amd_powerplay_get_display_power_level(void *handle,
  711. struct amd_pp_simple_clock_info *output)
  712. {
  713. struct pp_hwmgr *hwmgr;
  714. PP_CHECK((struct pp_instance *)handle);
  715. if (output == NULL)
  716. return -EINVAL;
  717. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  718. return phm_get_dal_power_level(hwmgr, output);
  719. }
  720. int amd_powerplay_get_current_clocks(void *handle,
  721. struct amd_pp_clock_info *clocks)
  722. {
  723. struct pp_hwmgr *hwmgr;
  724. struct amd_pp_simple_clock_info simple_clocks;
  725. struct pp_clock_info hw_clocks;
  726. PP_CHECK((struct pp_instance *)handle);
  727. if (clocks == NULL)
  728. return -EINVAL;
  729. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  730. phm_get_dal_power_level(hwmgr, &simple_clocks);
  731. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_PowerContainment)) {
  732. if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment))
  733. PP_ASSERT_WITH_CODE(0, "Error in PHM_GetPowerContainmentClockInfo", return -1);
  734. } else {
  735. if (0 != phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks, PHM_PerformanceLevelDesignation_Activity))
  736. PP_ASSERT_WITH_CODE(0, "Error in PHM_GetClockInfo", return -1);
  737. }
  738. clocks->min_engine_clock = hw_clocks.min_eng_clk;
  739. clocks->max_engine_clock = hw_clocks.max_eng_clk;
  740. clocks->min_memory_clock = hw_clocks.min_mem_clk;
  741. clocks->max_memory_clock = hw_clocks.max_mem_clk;
  742. clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
  743. clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
  744. clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
  745. clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
  746. clocks->max_clocks_state = simple_clocks.level;
  747. if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
  748. clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
  749. clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
  750. }
  751. return 0;
  752. }
  753. int amd_powerplay_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
  754. {
  755. int result = -1;
  756. struct pp_hwmgr *hwmgr;
  757. PP_CHECK((struct pp_instance *)handle);
  758. if (clocks == NULL)
  759. return -EINVAL;
  760. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  761. result = phm_get_clock_by_type(hwmgr, type, clocks);
  762. return result;
  763. }
  764. int amd_powerplay_get_display_mode_validation_clocks(void *handle,
  765. struct amd_pp_simple_clock_info *clocks)
  766. {
  767. int result = -1;
  768. struct pp_hwmgr *hwmgr;
  769. PP_CHECK((struct pp_instance *)handle);
  770. if (clocks == NULL)
  771. return -EINVAL;
  772. hwmgr = ((struct pp_instance *)handle)->hwmgr;
  773. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
  774. result = phm_get_max_high_clocks(hwmgr, clocks);
  775. return result;
  776. }