amdgpu_powerplay.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. * Authors: AMD
  23. *
  24. */
  25. #include "atom.h"
  26. #include "amdgpu.h"
  27. #include "amd_shared.h"
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include "amdgpu_pm.h"
  31. #include <drm/amdgpu_drm.h>
  32. #include "amdgpu_powerplay.h"
  33. #include "si_dpm.h"
  34. #include "cik_dpm.h"
  35. #include "vi_dpm.h"
  36. static int amdgpu_create_pp_handle(struct amdgpu_device *adev)
  37. {
  38. struct amd_pp_init pp_init;
  39. struct amd_powerplay *amd_pp;
  40. int ret;
  41. amd_pp = &(adev->powerplay);
  42. pp_init.chip_family = adev->family;
  43. pp_init.chip_id = adev->asic_type;
  44. pp_init.pm_en = amdgpu_dpm != 0 ? true : false;
  45. pp_init.feature_mask = amdgpu_pp_feature_mask;
  46. pp_init.device = amdgpu_cgs_create_device(adev);
  47. ret = amd_powerplay_create(&pp_init, &(amd_pp->pp_handle));
  48. if (ret)
  49. return -EINVAL;
  50. return 0;
  51. }
  52. static int amdgpu_pp_early_init(void *handle)
  53. {
  54. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  55. struct amd_powerplay *amd_pp;
  56. int ret = 0;
  57. amd_pp = &(adev->powerplay);
  58. adev->pp_enabled = false;
  59. amd_pp->pp_handle = (void *)adev;
  60. switch (adev->asic_type) {
  61. case CHIP_POLARIS11:
  62. case CHIP_POLARIS10:
  63. case CHIP_POLARIS12:
  64. case CHIP_TONGA:
  65. case CHIP_FIJI:
  66. case CHIP_TOPAZ:
  67. case CHIP_CARRIZO:
  68. case CHIP_STONEY:
  69. adev->pp_enabled = true;
  70. if (amdgpu_create_pp_handle(adev))
  71. return -EINVAL;
  72. amd_pp->ip_funcs = &pp_ip_funcs;
  73. amd_pp->pp_funcs = &pp_dpm_funcs;
  74. break;
  75. /* These chips don't have powerplay implemenations */
  76. #ifdef CONFIG_DRM_AMDGPU_SI
  77. case CHIP_TAHITI:
  78. case CHIP_PITCAIRN:
  79. case CHIP_VERDE:
  80. case CHIP_OLAND:
  81. case CHIP_HAINAN:
  82. amd_pp->ip_funcs = &si_dpm_ip_funcs;
  83. break;
  84. #endif
  85. #ifdef CONFIG_DRM_AMDGPU_CIK
  86. case CHIP_BONAIRE:
  87. case CHIP_HAWAII:
  88. amd_pp->ip_funcs = &ci_dpm_ip_funcs;
  89. break;
  90. case CHIP_KABINI:
  91. case CHIP_MULLINS:
  92. case CHIP_KAVERI:
  93. amd_pp->ip_funcs = &kv_dpm_ip_funcs;
  94. break;
  95. #endif
  96. default:
  97. ret = -EINVAL;
  98. break;
  99. }
  100. if (adev->powerplay.ip_funcs->early_init)
  101. ret = adev->powerplay.ip_funcs->early_init(
  102. adev->powerplay.pp_handle);
  103. if (ret == PP_DPM_DISABLED) {
  104. adev->pm.dpm_enabled = false;
  105. return 0;
  106. }
  107. return ret;
  108. }
  109. static int amdgpu_pp_late_init(void *handle)
  110. {
  111. int ret = 0;
  112. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  113. if (adev->powerplay.ip_funcs->late_init)
  114. ret = adev->powerplay.ip_funcs->late_init(
  115. adev->powerplay.pp_handle);
  116. if (adev->pp_enabled && adev->pm.dpm_enabled) {
  117. amdgpu_pm_sysfs_init(adev);
  118. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_COMPLETE_INIT, NULL, NULL);
  119. }
  120. return ret;
  121. }
  122. static int amdgpu_pp_sw_init(void *handle)
  123. {
  124. int ret = 0;
  125. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  126. if (adev->powerplay.ip_funcs->sw_init)
  127. ret = adev->powerplay.ip_funcs->sw_init(
  128. adev->powerplay.pp_handle);
  129. return ret;
  130. }
  131. static int amdgpu_pp_sw_fini(void *handle)
  132. {
  133. int ret = 0;
  134. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  135. if (adev->powerplay.ip_funcs->sw_fini)
  136. ret = adev->powerplay.ip_funcs->sw_fini(
  137. adev->powerplay.pp_handle);
  138. if (ret)
  139. return ret;
  140. return ret;
  141. }
  142. static int amdgpu_pp_hw_init(void *handle)
  143. {
  144. int ret = 0;
  145. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  146. if (adev->pp_enabled && adev->firmware.smu_load)
  147. amdgpu_ucode_init_bo(adev);
  148. if (adev->powerplay.ip_funcs->hw_init)
  149. ret = adev->powerplay.ip_funcs->hw_init(
  150. adev->powerplay.pp_handle);
  151. if (ret == PP_DPM_DISABLED) {
  152. adev->pm.dpm_enabled = false;
  153. return 0;
  154. }
  155. if ((amdgpu_dpm != 0) && !amdgpu_sriov_vf(adev))
  156. adev->pm.dpm_enabled = true;
  157. return ret;
  158. }
  159. static int amdgpu_pp_hw_fini(void *handle)
  160. {
  161. int ret = 0;
  162. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  163. if (adev->powerplay.ip_funcs->hw_fini)
  164. ret = adev->powerplay.ip_funcs->hw_fini(
  165. adev->powerplay.pp_handle);
  166. if (adev->pp_enabled && adev->firmware.smu_load)
  167. amdgpu_ucode_fini_bo(adev);
  168. return ret;
  169. }
  170. static void amdgpu_pp_late_fini(void *handle)
  171. {
  172. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  173. if (adev->powerplay.ip_funcs->late_fini)
  174. adev->powerplay.ip_funcs->late_fini(
  175. adev->powerplay.pp_handle);
  176. if (adev->pp_enabled && adev->pm.dpm_enabled)
  177. amdgpu_pm_sysfs_fini(adev);
  178. amd_powerplay_destroy(adev->powerplay.pp_handle);
  179. }
  180. static int amdgpu_pp_suspend(void *handle)
  181. {
  182. int ret = 0;
  183. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  184. if (adev->powerplay.ip_funcs->suspend)
  185. ret = adev->powerplay.ip_funcs->suspend(
  186. adev->powerplay.pp_handle);
  187. return ret;
  188. }
  189. static int amdgpu_pp_resume(void *handle)
  190. {
  191. int ret = 0;
  192. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  193. if (adev->powerplay.ip_funcs->resume)
  194. ret = adev->powerplay.ip_funcs->resume(
  195. adev->powerplay.pp_handle);
  196. return ret;
  197. }
  198. static int amdgpu_pp_set_clockgating_state(void *handle,
  199. enum amd_clockgating_state state)
  200. {
  201. int ret = 0;
  202. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  203. if (adev->powerplay.ip_funcs->set_clockgating_state)
  204. ret = adev->powerplay.ip_funcs->set_clockgating_state(
  205. adev->powerplay.pp_handle, state);
  206. return ret;
  207. }
  208. static int amdgpu_pp_set_powergating_state(void *handle,
  209. enum amd_powergating_state state)
  210. {
  211. int ret = 0;
  212. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  213. if (adev->powerplay.ip_funcs->set_powergating_state)
  214. ret = adev->powerplay.ip_funcs->set_powergating_state(
  215. adev->powerplay.pp_handle, state);
  216. return ret;
  217. }
  218. static bool amdgpu_pp_is_idle(void *handle)
  219. {
  220. bool ret = true;
  221. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  222. if (adev->powerplay.ip_funcs->is_idle)
  223. ret = adev->powerplay.ip_funcs->is_idle(
  224. adev->powerplay.pp_handle);
  225. return ret;
  226. }
  227. static int amdgpu_pp_wait_for_idle(void *handle)
  228. {
  229. int ret = 0;
  230. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  231. if (adev->powerplay.ip_funcs->wait_for_idle)
  232. ret = adev->powerplay.ip_funcs->wait_for_idle(
  233. adev->powerplay.pp_handle);
  234. return ret;
  235. }
  236. static int amdgpu_pp_soft_reset(void *handle)
  237. {
  238. int ret = 0;
  239. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  240. if (adev->powerplay.ip_funcs->soft_reset)
  241. ret = adev->powerplay.ip_funcs->soft_reset(
  242. adev->powerplay.pp_handle);
  243. return ret;
  244. }
  245. static const struct amd_ip_funcs amdgpu_pp_ip_funcs = {
  246. .name = "amdgpu_powerplay",
  247. .early_init = amdgpu_pp_early_init,
  248. .late_init = amdgpu_pp_late_init,
  249. .sw_init = amdgpu_pp_sw_init,
  250. .sw_fini = amdgpu_pp_sw_fini,
  251. .hw_init = amdgpu_pp_hw_init,
  252. .hw_fini = amdgpu_pp_hw_fini,
  253. .late_fini = amdgpu_pp_late_fini,
  254. .suspend = amdgpu_pp_suspend,
  255. .resume = amdgpu_pp_resume,
  256. .is_idle = amdgpu_pp_is_idle,
  257. .wait_for_idle = amdgpu_pp_wait_for_idle,
  258. .soft_reset = amdgpu_pp_soft_reset,
  259. .set_clockgating_state = amdgpu_pp_set_clockgating_state,
  260. .set_powergating_state = amdgpu_pp_set_powergating_state,
  261. };
  262. const struct amdgpu_ip_block_version amdgpu_pp_ip_block =
  263. {
  264. .type = AMD_IP_BLOCK_TYPE_SMC,
  265. .major = 1,
  266. .minor = 0,
  267. .rev = 0,
  268. .funcs = &amdgpu_pp_ip_funcs,
  269. };