amdgpu_powerplay.c 7.8 KB

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