amdgpu_powerplay.c 8.0 KB

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