hardwaremanager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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/errno.h>
  25. #include "hwmgr.h"
  26. #include "hardwaremanager.h"
  27. #include "power_state.h"
  28. #define TEMP_RANGE_MIN (0)
  29. #define TEMP_RANGE_MAX (80 * 1000)
  30. #define PHM_FUNC_CHECK(hw) \
  31. do { \
  32. if ((hw) == NULL || (hw)->hwmgr_func == NULL) \
  33. return -EINVAL; \
  34. } while (0)
  35. int phm_setup_asic(struct pp_hwmgr *hwmgr)
  36. {
  37. PHM_FUNC_CHECK(hwmgr);
  38. if (NULL != hwmgr->hwmgr_func->asic_setup)
  39. return hwmgr->hwmgr_func->asic_setup(hwmgr);
  40. return 0;
  41. }
  42. int phm_power_down_asic(struct pp_hwmgr *hwmgr)
  43. {
  44. PHM_FUNC_CHECK(hwmgr);
  45. if (NULL != hwmgr->hwmgr_func->power_off_asic)
  46. return hwmgr->hwmgr_func->power_off_asic(hwmgr);
  47. return 0;
  48. }
  49. int phm_set_power_state(struct pp_hwmgr *hwmgr,
  50. const struct pp_hw_power_state *pcurrent_state,
  51. const struct pp_hw_power_state *pnew_power_state)
  52. {
  53. struct phm_set_power_state_input states;
  54. PHM_FUNC_CHECK(hwmgr);
  55. states.pcurrent_state = pcurrent_state;
  56. states.pnew_state = pnew_power_state;
  57. if (NULL != hwmgr->hwmgr_func->power_state_set)
  58. return hwmgr->hwmgr_func->power_state_set(hwmgr, &states);
  59. return 0;
  60. }
  61. int phm_enable_dynamic_state_management(struct pp_hwmgr *hwmgr)
  62. {
  63. struct amdgpu_device *adev = NULL;
  64. int ret = -EINVAL;;
  65. PHM_FUNC_CHECK(hwmgr);
  66. adev = hwmgr->adev;
  67. if (smum_is_dpm_running(hwmgr) && !amdgpu_passthrough(adev)) {
  68. pr_info("dpm has been enabled\n");
  69. return 0;
  70. }
  71. if (NULL != hwmgr->hwmgr_func->dynamic_state_management_enable)
  72. ret = hwmgr->hwmgr_func->dynamic_state_management_enable(hwmgr);
  73. return ret;
  74. }
  75. int phm_disable_dynamic_state_management(struct pp_hwmgr *hwmgr)
  76. {
  77. int ret = -EINVAL;
  78. PHM_FUNC_CHECK(hwmgr);
  79. if (!smum_is_dpm_running(hwmgr)) {
  80. pr_info("dpm has been disabled\n");
  81. return 0;
  82. }
  83. if (hwmgr->hwmgr_func->dynamic_state_management_disable)
  84. ret = hwmgr->hwmgr_func->dynamic_state_management_disable(hwmgr);
  85. return ret;
  86. }
  87. int phm_force_dpm_levels(struct pp_hwmgr *hwmgr, enum amd_dpm_forced_level level)
  88. {
  89. int ret = 0;
  90. PHM_FUNC_CHECK(hwmgr);
  91. if (hwmgr->hwmgr_func->force_dpm_level != NULL)
  92. ret = hwmgr->hwmgr_func->force_dpm_level(hwmgr, level);
  93. return ret;
  94. }
  95. int phm_apply_state_adjust_rules(struct pp_hwmgr *hwmgr,
  96. struct pp_power_state *adjusted_ps,
  97. const struct pp_power_state *current_ps)
  98. {
  99. PHM_FUNC_CHECK(hwmgr);
  100. if (hwmgr->hwmgr_func->apply_state_adjust_rules != NULL)
  101. return hwmgr->hwmgr_func->apply_state_adjust_rules(
  102. hwmgr,
  103. adjusted_ps,
  104. current_ps);
  105. return 0;
  106. }
  107. int phm_apply_clock_adjust_rules(struct pp_hwmgr *hwmgr)
  108. {
  109. PHM_FUNC_CHECK(hwmgr);
  110. if (hwmgr->hwmgr_func->apply_clocks_adjust_rules != NULL)
  111. return hwmgr->hwmgr_func->apply_clocks_adjust_rules(hwmgr);
  112. return 0;
  113. }
  114. int phm_powerdown_uvd(struct pp_hwmgr *hwmgr)
  115. {
  116. PHM_FUNC_CHECK(hwmgr);
  117. if (hwmgr->hwmgr_func->powerdown_uvd != NULL)
  118. return hwmgr->hwmgr_func->powerdown_uvd(hwmgr);
  119. return 0;
  120. }
  121. int phm_enable_clock_power_gatings(struct pp_hwmgr *hwmgr)
  122. {
  123. PHM_FUNC_CHECK(hwmgr);
  124. if (NULL != hwmgr->hwmgr_func->enable_clock_power_gating)
  125. return hwmgr->hwmgr_func->enable_clock_power_gating(hwmgr);
  126. return 0;
  127. }
  128. int phm_disable_clock_power_gatings(struct pp_hwmgr *hwmgr)
  129. {
  130. PHM_FUNC_CHECK(hwmgr);
  131. if (NULL != hwmgr->hwmgr_func->disable_clock_power_gating)
  132. return hwmgr->hwmgr_func->disable_clock_power_gating(hwmgr);
  133. return 0;
  134. }
  135. int phm_pre_display_configuration_changed(struct pp_hwmgr *hwmgr)
  136. {
  137. PHM_FUNC_CHECK(hwmgr);
  138. if (NULL != hwmgr->hwmgr_func->pre_display_config_changed)
  139. hwmgr->hwmgr_func->pre_display_config_changed(hwmgr);
  140. return 0;
  141. }
  142. int phm_display_configuration_changed(struct pp_hwmgr *hwmgr)
  143. {
  144. PHM_FUNC_CHECK(hwmgr);
  145. if (NULL != hwmgr->hwmgr_func->display_config_changed)
  146. hwmgr->hwmgr_func->display_config_changed(hwmgr);
  147. return 0;
  148. }
  149. int phm_notify_smc_display_config_after_ps_adjustment(struct pp_hwmgr *hwmgr)
  150. {
  151. PHM_FUNC_CHECK(hwmgr);
  152. if (NULL != hwmgr->hwmgr_func->notify_smc_display_config_after_ps_adjustment)
  153. hwmgr->hwmgr_func->notify_smc_display_config_after_ps_adjustment(hwmgr);
  154. return 0;
  155. }
  156. int phm_stop_thermal_controller(struct pp_hwmgr *hwmgr)
  157. {
  158. PHM_FUNC_CHECK(hwmgr);
  159. if (hwmgr->hwmgr_func->stop_thermal_controller == NULL)
  160. return -EINVAL;
  161. return hwmgr->hwmgr_func->stop_thermal_controller(hwmgr);
  162. }
  163. int phm_register_irq_handlers(struct pp_hwmgr *hwmgr)
  164. {
  165. PHM_FUNC_CHECK(hwmgr);
  166. if (hwmgr->hwmgr_func->register_irq_handlers != NULL)
  167. return hwmgr->hwmgr_func->register_irq_handlers(hwmgr);
  168. return 0;
  169. }
  170. /**
  171. * Initializes the thermal controller subsystem.
  172. *
  173. * @param pHwMgr the address of the powerplay hardware manager.
  174. * @exception PP_Result_Failed if any of the paramters is NULL, otherwise the return value from the dispatcher.
  175. */
  176. int phm_start_thermal_controller(struct pp_hwmgr *hwmgr)
  177. {
  178. int ret = 0;
  179. struct PP_TemperatureRange range = {TEMP_RANGE_MIN, TEMP_RANGE_MAX};
  180. struct amdgpu_device *adev = hwmgr->adev;
  181. if (hwmgr->hwmgr_func->get_thermal_temperature_range)
  182. hwmgr->hwmgr_func->get_thermal_temperature_range(
  183. hwmgr, &range);
  184. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  185. PHM_PlatformCaps_ThermalController)
  186. && hwmgr->hwmgr_func->start_thermal_controller != NULL)
  187. ret = hwmgr->hwmgr_func->start_thermal_controller(hwmgr, &range);
  188. adev->pm.dpm.thermal.min_temp = range.min;
  189. adev->pm.dpm.thermal.max_temp = range.max;
  190. return ret;
  191. }
  192. bool phm_check_smc_update_required_for_display_configuration(struct pp_hwmgr *hwmgr)
  193. {
  194. PHM_FUNC_CHECK(hwmgr);
  195. if (hwmgr->hwmgr_func->check_smc_update_required_for_display_configuration == NULL)
  196. return false;
  197. return hwmgr->hwmgr_func->check_smc_update_required_for_display_configuration(hwmgr);
  198. }
  199. int phm_check_states_equal(struct pp_hwmgr *hwmgr,
  200. const struct pp_hw_power_state *pstate1,
  201. const struct pp_hw_power_state *pstate2,
  202. bool *equal)
  203. {
  204. PHM_FUNC_CHECK(hwmgr);
  205. if (hwmgr->hwmgr_func->check_states_equal == NULL)
  206. return -EINVAL;
  207. return hwmgr->hwmgr_func->check_states_equal(hwmgr, pstate1, pstate2, equal);
  208. }
  209. int phm_store_dal_configuration_data(struct pp_hwmgr *hwmgr,
  210. const struct amd_pp_display_configuration *display_config)
  211. {
  212. int index = 0;
  213. int number_of_active_display = 0;
  214. PHM_FUNC_CHECK(hwmgr);
  215. if (display_config == NULL)
  216. return -EINVAL;
  217. if (NULL != hwmgr->hwmgr_func->set_deep_sleep_dcefclk)
  218. hwmgr->hwmgr_func->set_deep_sleep_dcefclk(hwmgr, display_config->min_dcef_deep_sleep_set_clk);
  219. for (index = 0; index < display_config->num_path_including_non_display; index++) {
  220. if (display_config->displays[index].controller_id != 0)
  221. number_of_active_display++;
  222. }
  223. if (NULL != hwmgr->hwmgr_func->set_active_display_count)
  224. hwmgr->hwmgr_func->set_active_display_count(hwmgr, number_of_active_display);
  225. if (hwmgr->hwmgr_func->store_cc6_data == NULL)
  226. return -EINVAL;
  227. /* TODO: pass other display configuration in the future */
  228. if (hwmgr->hwmgr_func->store_cc6_data)
  229. hwmgr->hwmgr_func->store_cc6_data(hwmgr,
  230. display_config->cpu_pstate_separation_time,
  231. display_config->cpu_cc6_disable,
  232. display_config->cpu_pstate_disable,
  233. display_config->nb_pstate_switch_disable);
  234. return 0;
  235. }
  236. int phm_get_dal_power_level(struct pp_hwmgr *hwmgr,
  237. struct amd_pp_simple_clock_info *info)
  238. {
  239. PHM_FUNC_CHECK(hwmgr);
  240. if (info == NULL || hwmgr->hwmgr_func->get_dal_power_level == NULL)
  241. return -EINVAL;
  242. return hwmgr->hwmgr_func->get_dal_power_level(hwmgr, info);
  243. }
  244. int phm_set_cpu_power_state(struct pp_hwmgr *hwmgr)
  245. {
  246. PHM_FUNC_CHECK(hwmgr);
  247. if (hwmgr->hwmgr_func->set_cpu_power_state != NULL)
  248. return hwmgr->hwmgr_func->set_cpu_power_state(hwmgr);
  249. return 0;
  250. }
  251. int phm_get_performance_level(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *state,
  252. PHM_PerformanceLevelDesignation designation, uint32_t index,
  253. PHM_PerformanceLevel *level)
  254. {
  255. PHM_FUNC_CHECK(hwmgr);
  256. if (hwmgr->hwmgr_func->get_performance_level == NULL)
  257. return -EINVAL;
  258. return hwmgr->hwmgr_func->get_performance_level(hwmgr, state, designation, index, level);
  259. }
  260. /**
  261. * Gets Clock Info.
  262. *
  263. * @param pHwMgr the address of the powerplay hardware manager.
  264. * @param pPowerState the address of the Power State structure.
  265. * @param pClockInfo the address of PP_ClockInfo structure where the result will be returned.
  266. * @exception PP_Result_Failed if any of the paramters is NULL, otherwise the return value from the back-end.
  267. */
  268. int phm_get_clock_info(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *state, struct pp_clock_info *pclock_info,
  269. PHM_PerformanceLevelDesignation designation)
  270. {
  271. int result;
  272. PHM_PerformanceLevel performance_level;
  273. PHM_FUNC_CHECK(hwmgr);
  274. PP_ASSERT_WITH_CODE((NULL != state), "Invalid Input!", return -EINVAL);
  275. PP_ASSERT_WITH_CODE((NULL != pclock_info), "Invalid Input!", return -EINVAL);
  276. result = phm_get_performance_level(hwmgr, state, PHM_PerformanceLevelDesignation_Activity, 0, &performance_level);
  277. PP_ASSERT_WITH_CODE((0 == result), "Failed to retrieve minimum clocks.", return result);
  278. pclock_info->min_mem_clk = performance_level.memory_clock;
  279. pclock_info->min_eng_clk = performance_level.coreClock;
  280. pclock_info->min_bus_bandwidth = performance_level.nonLocalMemoryFreq * performance_level.nonLocalMemoryWidth;
  281. result = phm_get_performance_level(hwmgr, state, designation,
  282. (hwmgr->platform_descriptor.hardwareActivityPerformanceLevels - 1), &performance_level);
  283. PP_ASSERT_WITH_CODE((0 == result), "Failed to retrieve maximum clocks.", return result);
  284. pclock_info->max_mem_clk = performance_level.memory_clock;
  285. pclock_info->max_eng_clk = performance_level.coreClock;
  286. pclock_info->max_bus_bandwidth = performance_level.nonLocalMemoryFreq * performance_level.nonLocalMemoryWidth;
  287. return 0;
  288. }
  289. int phm_get_current_shallow_sleep_clocks(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *state, struct pp_clock_info *clock_info)
  290. {
  291. PHM_FUNC_CHECK(hwmgr);
  292. if (hwmgr->hwmgr_func->get_current_shallow_sleep_clocks == NULL)
  293. return -EINVAL;
  294. return hwmgr->hwmgr_func->get_current_shallow_sleep_clocks(hwmgr, state, clock_info);
  295. }
  296. int phm_get_clock_by_type(struct pp_hwmgr *hwmgr, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
  297. {
  298. PHM_FUNC_CHECK(hwmgr);
  299. if (hwmgr->hwmgr_func->get_clock_by_type == NULL)
  300. return -EINVAL;
  301. return hwmgr->hwmgr_func->get_clock_by_type(hwmgr, type, clocks);
  302. }
  303. int phm_get_clock_by_type_with_latency(struct pp_hwmgr *hwmgr,
  304. enum amd_pp_clock_type type,
  305. struct pp_clock_levels_with_latency *clocks)
  306. {
  307. PHM_FUNC_CHECK(hwmgr);
  308. if (hwmgr->hwmgr_func->get_clock_by_type_with_latency == NULL)
  309. return -EINVAL;
  310. return hwmgr->hwmgr_func->get_clock_by_type_with_latency(hwmgr, type, clocks);
  311. }
  312. int phm_get_clock_by_type_with_voltage(struct pp_hwmgr *hwmgr,
  313. enum amd_pp_clock_type type,
  314. struct pp_clock_levels_with_voltage *clocks)
  315. {
  316. PHM_FUNC_CHECK(hwmgr);
  317. if (hwmgr->hwmgr_func->get_clock_by_type_with_voltage == NULL)
  318. return -EINVAL;
  319. return hwmgr->hwmgr_func->get_clock_by_type_with_voltage(hwmgr, type, clocks);
  320. }
  321. int phm_set_watermarks_for_clocks_ranges(struct pp_hwmgr *hwmgr,
  322. void *clock_ranges)
  323. {
  324. PHM_FUNC_CHECK(hwmgr);
  325. if (!hwmgr->hwmgr_func->set_watermarks_for_clocks_ranges)
  326. return -EINVAL;
  327. return hwmgr->hwmgr_func->set_watermarks_for_clocks_ranges(hwmgr,
  328. clock_ranges);
  329. }
  330. int phm_display_clock_voltage_request(struct pp_hwmgr *hwmgr,
  331. struct pp_display_clock_request *clock)
  332. {
  333. PHM_FUNC_CHECK(hwmgr);
  334. if (!hwmgr->hwmgr_func->display_clock_voltage_request)
  335. return -EINVAL;
  336. return hwmgr->hwmgr_func->display_clock_voltage_request(hwmgr, clock);
  337. }
  338. int phm_get_max_high_clocks(struct pp_hwmgr *hwmgr, struct amd_pp_simple_clock_info *clocks)
  339. {
  340. PHM_FUNC_CHECK(hwmgr);
  341. if (hwmgr->hwmgr_func->get_max_high_clocks == NULL)
  342. return -EINVAL;
  343. return hwmgr->hwmgr_func->get_max_high_clocks(hwmgr, clocks);
  344. }
  345. int phm_disable_smc_firmware_ctf(struct pp_hwmgr *hwmgr)
  346. {
  347. PHM_FUNC_CHECK(hwmgr);
  348. if (hwmgr->hwmgr_func->disable_smc_firmware_ctf == NULL)
  349. return -EINVAL;
  350. return hwmgr->hwmgr_func->disable_smc_firmware_ctf(hwmgr);
  351. }