amdgpu_powerplay.c 8.2 KB

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