amdgpu_powerplay.c 8.0 KB

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