amd_powerplay.c 20 KB

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