pp_psm.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright 2017 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/slab.h>
  26. #include "pp_psm.h"
  27. int psm_init_power_state_table(struct pp_hwmgr *hwmgr)
  28. {
  29. int result;
  30. unsigned int i;
  31. unsigned int table_entries;
  32. struct pp_power_state *state;
  33. int size;
  34. if (hwmgr->hwmgr_func->get_num_of_pp_table_entries == NULL)
  35. return 0;
  36. if (hwmgr->hwmgr_func->get_power_state_size == NULL)
  37. return 0;
  38. hwmgr->num_ps = table_entries = hwmgr->hwmgr_func->get_num_of_pp_table_entries(hwmgr);
  39. hwmgr->ps_size = size = hwmgr->hwmgr_func->get_power_state_size(hwmgr) +
  40. sizeof(struct pp_power_state);
  41. if (table_entries == 0 || size == 0) {
  42. pr_warn("Please check whether power state management is supported on this asic\n");
  43. return 0;
  44. }
  45. hwmgr->ps = kcalloc(table_entries, size, GFP_KERNEL);
  46. if (hwmgr->ps == NULL)
  47. return -ENOMEM;
  48. hwmgr->request_ps = kzalloc(size, GFP_KERNEL);
  49. if (hwmgr->request_ps == NULL) {
  50. kfree(hwmgr->ps);
  51. hwmgr->ps = NULL;
  52. return -ENOMEM;
  53. }
  54. hwmgr->current_ps = kzalloc(size, GFP_KERNEL);
  55. if (hwmgr->current_ps == NULL) {
  56. kfree(hwmgr->request_ps);
  57. kfree(hwmgr->ps);
  58. hwmgr->request_ps = NULL;
  59. hwmgr->ps = NULL;
  60. return -ENOMEM;
  61. }
  62. state = hwmgr->ps;
  63. for (i = 0; i < table_entries; i++) {
  64. result = hwmgr->hwmgr_func->get_pp_table_entry(hwmgr, i, state);
  65. if (state->classification.flags & PP_StateClassificationFlag_Boot) {
  66. hwmgr->boot_ps = state;
  67. memcpy(hwmgr->current_ps, state, size);
  68. memcpy(hwmgr->request_ps, state, size);
  69. }
  70. state->id = i + 1; /* assigned unique num for every power state id */
  71. if (state->classification.flags & PP_StateClassificationFlag_Uvd)
  72. hwmgr->uvd_ps = state;
  73. state = (struct pp_power_state *)((unsigned long)state + size);
  74. }
  75. return 0;
  76. }
  77. int psm_fini_power_state_table(struct pp_hwmgr *hwmgr)
  78. {
  79. if (hwmgr == NULL)
  80. return -EINVAL;
  81. if (!hwmgr->ps)
  82. return 0;
  83. kfree(hwmgr->current_ps);
  84. kfree(hwmgr->request_ps);
  85. kfree(hwmgr->ps);
  86. hwmgr->request_ps = NULL;
  87. hwmgr->ps = NULL;
  88. hwmgr->current_ps = NULL;
  89. return 0;
  90. }
  91. static int psm_get_ui_state(struct pp_hwmgr *hwmgr,
  92. enum PP_StateUILabel ui_label,
  93. unsigned long *state_id)
  94. {
  95. struct pp_power_state *state;
  96. int table_entries;
  97. int i;
  98. table_entries = hwmgr->num_ps;
  99. state = hwmgr->ps;
  100. for (i = 0; i < table_entries; i++) {
  101. if (state->classification.ui_label & ui_label) {
  102. *state_id = state->id;
  103. return 0;
  104. }
  105. state = (struct pp_power_state *)((unsigned long)state + hwmgr->ps_size);
  106. }
  107. return -EINVAL;
  108. }
  109. static int psm_get_state_by_classification(struct pp_hwmgr *hwmgr,
  110. enum PP_StateClassificationFlag flag,
  111. unsigned long *state_id)
  112. {
  113. struct pp_power_state *state;
  114. int table_entries;
  115. int i;
  116. table_entries = hwmgr->num_ps;
  117. state = hwmgr->ps;
  118. for (i = 0; i < table_entries; i++) {
  119. if (state->classification.flags & flag) {
  120. *state_id = state->id;
  121. return 0;
  122. }
  123. state = (struct pp_power_state *)((unsigned long)state + hwmgr->ps_size);
  124. }
  125. return -EINVAL;
  126. }
  127. static int psm_set_states(struct pp_hwmgr *hwmgr, unsigned long state_id)
  128. {
  129. struct pp_power_state *state;
  130. int table_entries;
  131. int i;
  132. table_entries = hwmgr->num_ps;
  133. state = hwmgr->ps;
  134. for (i = 0; i < table_entries; i++) {
  135. if (state->id == state_id) {
  136. memcpy(hwmgr->request_ps, state, hwmgr->ps_size);
  137. return 0;
  138. }
  139. state = (struct pp_power_state *)((unsigned long)state + hwmgr->ps_size);
  140. }
  141. return -EINVAL;
  142. }
  143. int psm_set_boot_states(struct pp_hwmgr *hwmgr)
  144. {
  145. unsigned long state_id;
  146. int ret = -EINVAL;
  147. if (!hwmgr->ps)
  148. return 0;
  149. if (!psm_get_state_by_classification(hwmgr, PP_StateClassificationFlag_Boot,
  150. &state_id))
  151. ret = psm_set_states(hwmgr, state_id);
  152. return ret;
  153. }
  154. int psm_set_performance_states(struct pp_hwmgr *hwmgr)
  155. {
  156. unsigned long state_id;
  157. int ret = -EINVAL;
  158. if (!hwmgr->ps)
  159. return 0;
  160. if (!psm_get_ui_state(hwmgr, PP_StateUILabel_Performance,
  161. &state_id))
  162. ret = psm_set_states(hwmgr, state_id);
  163. return ret;
  164. }
  165. int psm_set_user_performance_state(struct pp_hwmgr *hwmgr,
  166. enum PP_StateUILabel label_id,
  167. struct pp_power_state **state)
  168. {
  169. int table_entries;
  170. int i;
  171. if (!hwmgr->ps)
  172. return 0;
  173. table_entries = hwmgr->num_ps;
  174. *state = hwmgr->ps;
  175. restart_search:
  176. for (i = 0; i < table_entries; i++) {
  177. if ((*state)->classification.ui_label & label_id)
  178. return 0;
  179. *state = (struct pp_power_state *)((uintptr_t)*state + hwmgr->ps_size);
  180. }
  181. switch (label_id) {
  182. case PP_StateUILabel_Battery:
  183. case PP_StateUILabel_Balanced:
  184. label_id = PP_StateUILabel_Performance;
  185. goto restart_search;
  186. default:
  187. break;
  188. }
  189. return -EINVAL;
  190. }
  191. static void power_state_management(struct pp_hwmgr *hwmgr,
  192. struct pp_power_state *new_ps)
  193. {
  194. struct pp_power_state *pcurrent;
  195. struct pp_power_state *requested;
  196. bool equal;
  197. if (new_ps != NULL)
  198. requested = new_ps;
  199. else
  200. requested = hwmgr->request_ps;
  201. pcurrent = hwmgr->current_ps;
  202. phm_apply_state_adjust_rules(hwmgr, requested, pcurrent);
  203. if (pcurrent == NULL || (0 != phm_check_states_equal(hwmgr,
  204. &pcurrent->hardware, &requested->hardware, &equal)))
  205. equal = false;
  206. if (!equal || phm_check_smc_update_required_for_display_configuration(hwmgr)) {
  207. phm_set_power_state(hwmgr, &pcurrent->hardware, &requested->hardware);
  208. memcpy(hwmgr->current_ps, hwmgr->request_ps, hwmgr->ps_size);
  209. }
  210. }
  211. int psm_adjust_power_state_dynamic(struct pp_hwmgr *hwmgr, bool skip,
  212. struct pp_power_state *new_ps)
  213. {
  214. uint32_t index;
  215. long workload;
  216. if (skip)
  217. return 0;
  218. phm_display_configuration_changed(hwmgr);
  219. if (hwmgr->ps)
  220. power_state_management(hwmgr, new_ps);
  221. else
  222. /*
  223. * for vega12/vega20 which does not support power state manager
  224. * DAL clock limits should also be honoured
  225. */
  226. phm_apply_clock_adjust_rules(hwmgr);
  227. phm_notify_smc_display_config_after_ps_adjustment(hwmgr);
  228. if (!phm_force_dpm_levels(hwmgr, hwmgr->request_dpm_level))
  229. hwmgr->dpm_level = hwmgr->request_dpm_level;
  230. if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
  231. index = fls(hwmgr->workload_mask);
  232. index = index > 0 && index <= Workload_Policy_Max ? index - 1 : 0;
  233. workload = hwmgr->workload_setting[index];
  234. if (hwmgr->power_profile_mode != workload && hwmgr->hwmgr_func->set_power_profile_mode)
  235. hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, &workload, 0);
  236. }
  237. return 0;
  238. }