amd_powerplay.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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. */
  23. #include "pp_debug.h"
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/gfp.h>
  27. #include <linux/slab.h>
  28. #include <linux/firmware.h>
  29. #include <linux/reboot.h>
  30. #include "amd_shared.h"
  31. #include "amd_powerplay.h"
  32. #include "power_state.h"
  33. #include "amdgpu.h"
  34. #include "hwmgr.h"
  35. #include "amdgpu_dpm_internal.h"
  36. #include "amdgpu_display.h"
  37. static const struct amd_pm_funcs pp_dpm_funcs;
  38. static int amd_powerplay_create(struct amdgpu_device *adev)
  39. {
  40. struct pp_hwmgr *hwmgr;
  41. if (adev == NULL)
  42. return -EINVAL;
  43. hwmgr = kzalloc(sizeof(struct pp_hwmgr), GFP_KERNEL);
  44. if (hwmgr == NULL)
  45. return -ENOMEM;
  46. hwmgr->adev = adev;
  47. hwmgr->not_vf = !amdgpu_sriov_vf(adev);
  48. hwmgr->device = amdgpu_cgs_create_device(adev);
  49. if (!hwmgr->device) {
  50. kfree(hwmgr);
  51. return -ENOMEM;
  52. }
  53. mutex_init(&hwmgr->msg_lock);
  54. hwmgr->chip_family = adev->family;
  55. hwmgr->chip_id = adev->asic_type;
  56. hwmgr->feature_mask = adev->pm.pp_feature;
  57. hwmgr->display_config = &adev->pm.pm_display_cfg;
  58. adev->powerplay.pp_handle = hwmgr;
  59. adev->powerplay.pp_funcs = &pp_dpm_funcs;
  60. return 0;
  61. }
  62. static void amd_powerplay_destroy(struct amdgpu_device *adev)
  63. {
  64. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  65. mutex_destroy(&hwmgr->msg_lock);
  66. kfree(hwmgr->hardcode_pp_table);
  67. hwmgr->hardcode_pp_table = NULL;
  68. kfree(hwmgr);
  69. hwmgr = NULL;
  70. }
  71. static int pp_early_init(void *handle)
  72. {
  73. int ret;
  74. struct amdgpu_device *adev = handle;
  75. ret = amd_powerplay_create(adev);
  76. if (ret != 0)
  77. return ret;
  78. ret = hwmgr_early_init(adev->powerplay.pp_handle);
  79. if (ret)
  80. return -EINVAL;
  81. return 0;
  82. }
  83. static void pp_swctf_delayed_work_handler(struct work_struct *work)
  84. {
  85. struct pp_hwmgr *hwmgr =
  86. container_of(work, struct pp_hwmgr, swctf_delayed_work.work);
  87. struct amdgpu_device *adev = hwmgr->adev;
  88. struct amdgpu_dpm_thermal *range =
  89. &adev->pm.dpm.thermal;
  90. uint32_t gpu_temperature, size = sizeof(gpu_temperature);
  91. int ret;
  92. /*
  93. * If the hotspot/edge temperature is confirmed as below SW CTF setting point
  94. * after the delay enforced, nothing will be done.
  95. * Otherwise, a graceful shutdown will be performed to prevent further damage.
  96. */
  97. if (range->sw_ctf_threshold &&
  98. hwmgr->hwmgr_func->read_sensor) {
  99. ret = hwmgr->hwmgr_func->read_sensor(hwmgr,
  100. AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
  101. &gpu_temperature,
  102. &size);
  103. /*
  104. * For some legacy ASICs, hotspot temperature retrieving might be not
  105. * supported. Check the edge temperature instead then.
  106. */
  107. if (ret == -EOPNOTSUPP)
  108. ret = hwmgr->hwmgr_func->read_sensor(hwmgr,
  109. AMDGPU_PP_SENSOR_EDGE_TEMP,
  110. &gpu_temperature,
  111. &size);
  112. if (!ret && gpu_temperature / 1000 < range->sw_ctf_threshold)
  113. return;
  114. }
  115. dev_emerg(adev->dev, "ERROR: GPU over temperature range(SW CTF) detected!\n");
  116. dev_emerg(adev->dev, "ERROR: System is going to shutdown due to GPU SW CTF!\n");
  117. orderly_poweroff(true);
  118. }
  119. static int pp_sw_init(void *handle)
  120. {
  121. struct amdgpu_device *adev = handle;
  122. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  123. int ret = 0;
  124. ret = hwmgr_sw_init(hwmgr);
  125. pr_debug("powerplay sw init %s\n", ret ? "failed" : "successfully");
  126. if (!ret)
  127. INIT_DELAYED_WORK(&hwmgr->swctf_delayed_work,
  128. pp_swctf_delayed_work_handler);
  129. return ret;
  130. }
  131. static int pp_sw_fini(void *handle)
  132. {
  133. struct amdgpu_device *adev = handle;
  134. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  135. hwmgr_sw_fini(hwmgr);
  136. amdgpu_ucode_release(&adev->pm.fw);
  137. return 0;
  138. }
  139. static int pp_hw_init(void *handle)
  140. {
  141. int ret = 0;
  142. struct amdgpu_device *adev = handle;
  143. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  144. ret = hwmgr_hw_init(hwmgr);
  145. if (ret)
  146. pr_err("powerplay hw init failed\n");
  147. return ret;
  148. }
  149. static int pp_hw_fini(void *handle)
  150. {
  151. struct amdgpu_device *adev = handle;
  152. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  153. cancel_delayed_work_sync(&hwmgr->swctf_delayed_work);
  154. hwmgr_hw_fini(hwmgr);
  155. return 0;
  156. }
  157. static void pp_reserve_vram_for_smu(struct amdgpu_device *adev)
  158. {
  159. int r = -EINVAL;
  160. void *cpu_ptr = NULL;
  161. uint64_t gpu_addr;
  162. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  163. if (amdgpu_bo_create_kernel(adev, adev->pm.smu_prv_buffer_size,
  164. PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
  165. &adev->pm.smu_prv_buffer,
  166. &gpu_addr,
  167. &cpu_ptr)) {
  168. DRM_ERROR("amdgpu: failed to create smu prv buffer\n");
  169. return;
  170. }
  171. if (hwmgr->hwmgr_func->notify_cac_buffer_info)
  172. r = hwmgr->hwmgr_func->notify_cac_buffer_info(hwmgr,
  173. lower_32_bits((unsigned long)cpu_ptr),
  174. upper_32_bits((unsigned long)cpu_ptr),
  175. lower_32_bits(gpu_addr),
  176. upper_32_bits(gpu_addr),
  177. adev->pm.smu_prv_buffer_size);
  178. if (r) {
  179. amdgpu_bo_free_kernel(&adev->pm.smu_prv_buffer, NULL, NULL);
  180. adev->pm.smu_prv_buffer = NULL;
  181. DRM_ERROR("amdgpu: failed to notify SMU buffer address\n");
  182. }
  183. }
  184. static int pp_late_init(void *handle)
  185. {
  186. struct amdgpu_device *adev = handle;
  187. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  188. if (hwmgr && hwmgr->pm_en)
  189. hwmgr_handle_task(hwmgr,
  190. AMD_PP_TASK_COMPLETE_INIT, NULL);
  191. if (adev->pm.smu_prv_buffer_size != 0)
  192. pp_reserve_vram_for_smu(adev);
  193. return 0;
  194. }
  195. static void pp_late_fini(void *handle)
  196. {
  197. struct amdgpu_device *adev = handle;
  198. if (adev->pm.smu_prv_buffer)
  199. amdgpu_bo_free_kernel(&adev->pm.smu_prv_buffer, NULL, NULL);
  200. amd_powerplay_destroy(adev);
  201. }
  202. static bool pp_is_idle(void *handle)
  203. {
  204. return false;
  205. }
  206. static int pp_wait_for_idle(void *handle)
  207. {
  208. return 0;
  209. }
  210. static int pp_sw_reset(void *handle)
  211. {
  212. return 0;
  213. }
  214. static int pp_set_powergating_state(void *handle,
  215. enum amd_powergating_state state)
  216. {
  217. return 0;
  218. }
  219. static int pp_suspend(void *handle)
  220. {
  221. struct amdgpu_device *adev = handle;
  222. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  223. cancel_delayed_work_sync(&hwmgr->swctf_delayed_work);
  224. return hwmgr_suspend(hwmgr);
  225. }
  226. static int pp_resume(void *handle)
  227. {
  228. struct amdgpu_device *adev = handle;
  229. struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
  230. return hwmgr_resume(hwmgr);
  231. }
  232. static int pp_set_clockgating_state(void *handle,
  233. enum amd_clockgating_state state)
  234. {
  235. return 0;
  236. }
  237. static const struct amd_ip_funcs pp_ip_funcs = {
  238. .name = "powerplay",
  239. .early_init = pp_early_init,
  240. .late_init = pp_late_init,
  241. .sw_init = pp_sw_init,
  242. .sw_fini = pp_sw_fini,
  243. .hw_init = pp_hw_init,
  244. .hw_fini = pp_hw_fini,
  245. .late_fini = pp_late_fini,
  246. .suspend = pp_suspend,
  247. .resume = pp_resume,
  248. .is_idle = pp_is_idle,
  249. .wait_for_idle = pp_wait_for_idle,
  250. .soft_reset = pp_sw_reset,
  251. .set_clockgating_state = pp_set_clockgating_state,
  252. .set_powergating_state = pp_set_powergating_state,
  253. .dump_ip_state = NULL,
  254. .print_ip_state = NULL,
  255. };
  256. const struct amdgpu_ip_block_version pp_smu_ip_block =
  257. {
  258. .type = AMD_IP_BLOCK_TYPE_SMC,
  259. .major = 1,
  260. .minor = 0,
  261. .rev = 0,
  262. .funcs = &pp_ip_funcs,
  263. };
  264. /* This interface only be supported On Vi,
  265. * because only smu7/8 can help to load gfx/sdma fw,
  266. * smu need to be enabled before load other ip's fw.
  267. * so call start smu to load smu7 fw and other ip's fw
  268. */
  269. static int pp_dpm_load_fw(void *handle)
  270. {
  271. struct pp_hwmgr *hwmgr = handle;
  272. if (!hwmgr || !hwmgr->smumgr_funcs || !hwmgr->smumgr_funcs->start_smu)
  273. return -EINVAL;
  274. if (hwmgr->smumgr_funcs->start_smu(hwmgr)) {
  275. pr_err("fw load failed\n");
  276. return -EINVAL;
  277. }
  278. return 0;
  279. }
  280. static int pp_dpm_fw_loading_complete(void *handle)
  281. {
  282. return 0;
  283. }
  284. static int pp_set_clockgating_by_smu(void *handle, uint32_t msg_id)
  285. {
  286. struct pp_hwmgr *hwmgr = handle;
  287. if (!hwmgr || !hwmgr->pm_en)
  288. return -EINVAL;
  289. if (hwmgr->hwmgr_func->update_clock_gatings == NULL) {
  290. pr_info_ratelimited("%s was not implemented.\n", __func__);
  291. return 0;
  292. }
  293. return hwmgr->hwmgr_func->update_clock_gatings(hwmgr, &msg_id);
  294. }
  295. static void pp_dpm_en_umd_pstate(struct pp_hwmgr *hwmgr,
  296. enum amd_dpm_forced_level *level)
  297. {
  298. uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
  299. AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
  300. AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
  301. AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
  302. if (!(hwmgr->dpm_level & profile_mode_mask)) {
  303. /* enter umd pstate, save current level, disable gfx cg*/
  304. if (*level & profile_mode_mask) {
  305. hwmgr->saved_dpm_level = hwmgr->dpm_level;
  306. hwmgr->en_umd_pstate = true;
  307. }
  308. } else {
  309. /* exit umd pstate, restore level, enable gfx cg*/
  310. if (!(*level & profile_mode_mask)) {
  311. if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
  312. *level = hwmgr->saved_dpm_level;
  313. hwmgr->en_umd_pstate = false;
  314. }
  315. }
  316. }
  317. static int pp_dpm_force_performance_level(void *handle,
  318. enum amd_dpm_forced_level level)
  319. {
  320. struct pp_hwmgr *hwmgr = handle;
  321. if (!hwmgr || !hwmgr->pm_en)
  322. return -EINVAL;
  323. if (level == hwmgr->dpm_level)
  324. return 0;
  325. pp_dpm_en_umd_pstate(hwmgr, &level);
  326. hwmgr->request_dpm_level = level;
  327. hwmgr_handle_task(hwmgr, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
  328. return 0;
  329. }
  330. static enum amd_dpm_forced_level pp_dpm_get_performance_level(
  331. void *handle)
  332. {
  333. struct pp_hwmgr *hwmgr = handle;
  334. if (!hwmgr || !hwmgr->pm_en)
  335. return -EINVAL;
  336. return hwmgr->dpm_level;
  337. }
  338. static uint32_t pp_dpm_get_sclk(void *handle, bool low)
  339. {
  340. struct pp_hwmgr *hwmgr = handle;
  341. if (!hwmgr || !hwmgr->pm_en)
  342. return 0;
  343. if (hwmgr->hwmgr_func->get_sclk == NULL) {
  344. pr_info_ratelimited("%s was not implemented.\n", __func__);
  345. return 0;
  346. }
  347. return hwmgr->hwmgr_func->get_sclk(hwmgr, low);
  348. }
  349. static uint32_t pp_dpm_get_mclk(void *handle, bool low)
  350. {
  351. struct pp_hwmgr *hwmgr = handle;
  352. if (!hwmgr || !hwmgr->pm_en)
  353. return 0;
  354. if (hwmgr->hwmgr_func->get_mclk == NULL) {
  355. pr_info_ratelimited("%s was not implemented.\n", __func__);
  356. return 0;
  357. }
  358. return hwmgr->hwmgr_func->get_mclk(hwmgr, low);
  359. }
  360. static void pp_dpm_powergate_vce(void *handle, bool gate)
  361. {
  362. struct pp_hwmgr *hwmgr = handle;
  363. if (!hwmgr || !hwmgr->pm_en)
  364. return;
  365. if (hwmgr->hwmgr_func->powergate_vce == NULL) {
  366. pr_info_ratelimited("%s was not implemented.\n", __func__);
  367. return;
  368. }
  369. hwmgr->hwmgr_func->powergate_vce(hwmgr, gate);
  370. }
  371. static void pp_dpm_powergate_uvd(void *handle, bool gate)
  372. {
  373. struct pp_hwmgr *hwmgr = handle;
  374. if (!hwmgr || !hwmgr->pm_en)
  375. return;
  376. if (hwmgr->hwmgr_func->powergate_uvd == NULL) {
  377. pr_info_ratelimited("%s was not implemented.\n", __func__);
  378. return;
  379. }
  380. hwmgr->hwmgr_func->powergate_uvd(hwmgr, gate);
  381. }
  382. static int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_task task_id,
  383. enum amd_pm_state_type *user_state)
  384. {
  385. struct pp_hwmgr *hwmgr = handle;
  386. if (!hwmgr || !hwmgr->pm_en)
  387. return -EINVAL;
  388. return hwmgr_handle_task(hwmgr, task_id, user_state);
  389. }
  390. static enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
  391. {
  392. struct pp_hwmgr *hwmgr = handle;
  393. struct pp_power_state *state;
  394. enum amd_pm_state_type pm_type;
  395. if (!hwmgr || !hwmgr->pm_en || !hwmgr->current_ps)
  396. return -EINVAL;
  397. state = hwmgr->current_ps;
  398. switch (state->classification.ui_label) {
  399. case PP_StateUILabel_Battery:
  400. pm_type = POWER_STATE_TYPE_BATTERY;
  401. break;
  402. case PP_StateUILabel_Balanced:
  403. pm_type = POWER_STATE_TYPE_BALANCED;
  404. break;
  405. case PP_StateUILabel_Performance:
  406. pm_type = POWER_STATE_TYPE_PERFORMANCE;
  407. break;
  408. default:
  409. if (state->classification.flags & PP_StateClassificationFlag_Boot)
  410. pm_type = POWER_STATE_TYPE_INTERNAL_BOOT;
  411. else
  412. pm_type = POWER_STATE_TYPE_DEFAULT;
  413. break;
  414. }
  415. return pm_type;
  416. }
  417. static int pp_dpm_set_fan_control_mode(void *handle, uint32_t mode)
  418. {
  419. struct pp_hwmgr *hwmgr = handle;
  420. if (!hwmgr || !hwmgr->pm_en)
  421. return -EOPNOTSUPP;
  422. if (hwmgr->hwmgr_func->set_fan_control_mode == NULL)
  423. return -EOPNOTSUPP;
  424. if (mode == U32_MAX)
  425. return -EINVAL;
  426. hwmgr->hwmgr_func->set_fan_control_mode(hwmgr, mode);
  427. return 0;
  428. }
  429. static int pp_dpm_get_fan_control_mode(void *handle, uint32_t *fan_mode)
  430. {
  431. struct pp_hwmgr *hwmgr = handle;
  432. if (!hwmgr || !hwmgr->pm_en)
  433. return -EOPNOTSUPP;
  434. if (hwmgr->hwmgr_func->get_fan_control_mode == NULL)
  435. return -EOPNOTSUPP;
  436. if (!fan_mode)
  437. return -EINVAL;
  438. *fan_mode = hwmgr->hwmgr_func->get_fan_control_mode(hwmgr);
  439. return 0;
  440. }
  441. static int pp_dpm_set_fan_speed_pwm(void *handle, uint32_t speed)
  442. {
  443. struct pp_hwmgr *hwmgr = handle;
  444. if (!hwmgr || !hwmgr->pm_en)
  445. return -EOPNOTSUPP;
  446. if (hwmgr->hwmgr_func->set_fan_speed_pwm == NULL)
  447. return -EOPNOTSUPP;
  448. if (speed == U32_MAX)
  449. return -EINVAL;
  450. return hwmgr->hwmgr_func->set_fan_speed_pwm(hwmgr, speed);
  451. }
  452. static int pp_dpm_get_fan_speed_pwm(void *handle, uint32_t *speed)
  453. {
  454. struct pp_hwmgr *hwmgr = handle;
  455. if (!hwmgr || !hwmgr->pm_en)
  456. return -EOPNOTSUPP;
  457. if (hwmgr->hwmgr_func->get_fan_speed_pwm == NULL)
  458. return -EOPNOTSUPP;
  459. if (!speed)
  460. return -EINVAL;
  461. return hwmgr->hwmgr_func->get_fan_speed_pwm(hwmgr, speed);
  462. }
  463. static int pp_dpm_get_fan_speed_rpm(void *handle, uint32_t *rpm)
  464. {
  465. struct pp_hwmgr *hwmgr = handle;
  466. if (!hwmgr || !hwmgr->pm_en)
  467. return -EOPNOTSUPP;
  468. if (hwmgr->hwmgr_func->get_fan_speed_rpm == NULL)
  469. return -EOPNOTSUPP;
  470. if (!rpm)
  471. return -EINVAL;
  472. return hwmgr->hwmgr_func->get_fan_speed_rpm(hwmgr, rpm);
  473. }
  474. static int pp_dpm_set_fan_speed_rpm(void *handle, uint32_t rpm)
  475. {
  476. struct pp_hwmgr *hwmgr = handle;
  477. if (!hwmgr || !hwmgr->pm_en)
  478. return -EOPNOTSUPP;
  479. if (hwmgr->hwmgr_func->set_fan_speed_rpm == NULL)
  480. return -EOPNOTSUPP;
  481. if (rpm == U32_MAX)
  482. return -EINVAL;
  483. return hwmgr->hwmgr_func->set_fan_speed_rpm(hwmgr, rpm);
  484. }
  485. static int pp_dpm_get_pp_num_states(void *handle,
  486. struct pp_states_info *data)
  487. {
  488. struct pp_hwmgr *hwmgr = handle;
  489. int i;
  490. memset(data, 0, sizeof(*data));
  491. if (!hwmgr || !hwmgr->pm_en || !hwmgr->ps)
  492. return -EINVAL;
  493. data->nums = hwmgr->num_ps;
  494. for (i = 0; i < hwmgr->num_ps; i++) {
  495. struct pp_power_state *state = (struct pp_power_state *)
  496. ((unsigned long)hwmgr->ps + i * hwmgr->ps_size);
  497. switch (state->classification.ui_label) {
  498. case PP_StateUILabel_Battery:
  499. data->states[i] = POWER_STATE_TYPE_BATTERY;
  500. break;
  501. case PP_StateUILabel_Balanced:
  502. data->states[i] = POWER_STATE_TYPE_BALANCED;
  503. break;
  504. case PP_StateUILabel_Performance:
  505. data->states[i] = POWER_STATE_TYPE_PERFORMANCE;
  506. break;
  507. default:
  508. if (state->classification.flags & PP_StateClassificationFlag_Boot)
  509. data->states[i] = POWER_STATE_TYPE_INTERNAL_BOOT;
  510. else
  511. data->states[i] = POWER_STATE_TYPE_DEFAULT;
  512. }
  513. }
  514. return 0;
  515. }
  516. static int pp_dpm_get_pp_table(void *handle, char **table)
  517. {
  518. struct pp_hwmgr *hwmgr = handle;
  519. if (!hwmgr || !hwmgr->pm_en || !hwmgr->soft_pp_table)
  520. return -EINVAL;
  521. *table = (char *)hwmgr->soft_pp_table;
  522. return hwmgr->soft_pp_table_size;
  523. }
  524. static int amd_powerplay_reset(void *handle)
  525. {
  526. struct pp_hwmgr *hwmgr = handle;
  527. int ret;
  528. ret = hwmgr_hw_fini(hwmgr);
  529. if (ret)
  530. return ret;
  531. ret = hwmgr_hw_init(hwmgr);
  532. if (ret)
  533. return ret;
  534. return hwmgr_handle_task(hwmgr, AMD_PP_TASK_COMPLETE_INIT, NULL);
  535. }
  536. static int pp_dpm_set_pp_table(void *handle, const char *buf, size_t size)
  537. {
  538. struct pp_hwmgr *hwmgr = handle;
  539. int ret = -ENOMEM;
  540. if (!hwmgr || !hwmgr->pm_en)
  541. return -EINVAL;
  542. if (!hwmgr->hardcode_pp_table) {
  543. hwmgr->hardcode_pp_table = kmemdup(hwmgr->soft_pp_table,
  544. hwmgr->soft_pp_table_size,
  545. GFP_KERNEL);
  546. if (!hwmgr->hardcode_pp_table)
  547. return ret;
  548. }
  549. memcpy(hwmgr->hardcode_pp_table, buf, size);
  550. hwmgr->soft_pp_table = hwmgr->hardcode_pp_table;
  551. ret = amd_powerplay_reset(handle);
  552. if (ret)
  553. return ret;
  554. if (hwmgr->hwmgr_func->avfs_control)
  555. ret = hwmgr->hwmgr_func->avfs_control(hwmgr, false);
  556. return ret;
  557. }
  558. static int pp_dpm_force_clock_level(void *handle,
  559. enum pp_clock_type type, uint32_t mask)
  560. {
  561. struct pp_hwmgr *hwmgr = handle;
  562. if (!hwmgr || !hwmgr->pm_en)
  563. return -EINVAL;
  564. if (hwmgr->hwmgr_func->force_clock_level == NULL) {
  565. pr_info_ratelimited("%s was not implemented.\n", __func__);
  566. return 0;
  567. }
  568. if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
  569. pr_debug("force clock level is for dpm manual mode only.\n");
  570. return -EINVAL;
  571. }
  572. return hwmgr->hwmgr_func->force_clock_level(hwmgr, type, mask);
  573. }
  574. static int pp_dpm_emit_clock_levels(void *handle,
  575. enum pp_clock_type type,
  576. char *buf,
  577. int *offset)
  578. {
  579. struct pp_hwmgr *hwmgr = handle;
  580. if (!hwmgr || !hwmgr->pm_en)
  581. return -EOPNOTSUPP;
  582. if (!hwmgr->hwmgr_func->emit_clock_levels)
  583. return -ENOENT;
  584. return hwmgr->hwmgr_func->emit_clock_levels(hwmgr, type, buf, offset);
  585. }
  586. static int pp_dpm_print_clock_levels(void *handle,
  587. enum pp_clock_type type, char *buf)
  588. {
  589. struct pp_hwmgr *hwmgr = handle;
  590. if (!hwmgr || !hwmgr->pm_en)
  591. return -EINVAL;
  592. if (hwmgr->hwmgr_func->print_clock_levels == NULL) {
  593. pr_info_ratelimited("%s was not implemented.\n", __func__);
  594. return 0;
  595. }
  596. return hwmgr->hwmgr_func->print_clock_levels(hwmgr, type, buf);
  597. }
  598. static int pp_dpm_get_sclk_od(void *handle)
  599. {
  600. struct pp_hwmgr *hwmgr = handle;
  601. if (!hwmgr || !hwmgr->pm_en)
  602. return -EINVAL;
  603. if (hwmgr->hwmgr_func->get_sclk_od == NULL) {
  604. pr_info_ratelimited("%s was not implemented.\n", __func__);
  605. return 0;
  606. }
  607. return hwmgr->hwmgr_func->get_sclk_od(hwmgr);
  608. }
  609. static int pp_dpm_set_sclk_od(void *handle, uint32_t value)
  610. {
  611. struct pp_hwmgr *hwmgr = handle;
  612. if (!hwmgr || !hwmgr->pm_en)
  613. return -EINVAL;
  614. if (hwmgr->hwmgr_func->set_sclk_od == NULL) {
  615. pr_info_ratelimited("%s was not implemented.\n", __func__);
  616. return 0;
  617. }
  618. return hwmgr->hwmgr_func->set_sclk_od(hwmgr, value);
  619. }
  620. static int pp_dpm_get_mclk_od(void *handle)
  621. {
  622. struct pp_hwmgr *hwmgr = handle;
  623. if (!hwmgr || !hwmgr->pm_en)
  624. return -EINVAL;
  625. if (hwmgr->hwmgr_func->get_mclk_od == NULL) {
  626. pr_info_ratelimited("%s was not implemented.\n", __func__);
  627. return 0;
  628. }
  629. return hwmgr->hwmgr_func->get_mclk_od(hwmgr);
  630. }
  631. static int pp_dpm_set_mclk_od(void *handle, uint32_t value)
  632. {
  633. struct pp_hwmgr *hwmgr = handle;
  634. if (!hwmgr || !hwmgr->pm_en)
  635. return -EINVAL;
  636. if (hwmgr->hwmgr_func->set_mclk_od == NULL) {
  637. pr_info_ratelimited("%s was not implemented.\n", __func__);
  638. return 0;
  639. }
  640. return hwmgr->hwmgr_func->set_mclk_od(hwmgr, value);
  641. }
  642. static int pp_dpm_read_sensor(void *handle, int idx,
  643. void *value, int *size)
  644. {
  645. struct pp_hwmgr *hwmgr = handle;
  646. if (!hwmgr || !hwmgr->pm_en || !value)
  647. return -EINVAL;
  648. switch (idx) {
  649. case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
  650. *((uint32_t *)value) = hwmgr->pstate_sclk * 100;
  651. return 0;
  652. case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
  653. *((uint32_t *)value) = hwmgr->pstate_mclk * 100;
  654. return 0;
  655. case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK:
  656. *((uint32_t *)value) = hwmgr->pstate_sclk_peak * 100;
  657. return 0;
  658. case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK:
  659. *((uint32_t *)value) = hwmgr->pstate_mclk_peak * 100;
  660. return 0;
  661. case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
  662. *((uint32_t *)value) = hwmgr->thermal_controller.fanInfo.ulMinRPM;
  663. return 0;
  664. case AMDGPU_PP_SENSOR_MAX_FAN_RPM:
  665. *((uint32_t *)value) = hwmgr->thermal_controller.fanInfo.ulMaxRPM;
  666. return 0;
  667. default:
  668. return hwmgr->hwmgr_func->read_sensor(hwmgr, idx, value, size);
  669. }
  670. }
  671. static struct amd_vce_state*
  672. pp_dpm_get_vce_clock_state(void *handle, unsigned idx)
  673. {
  674. struct pp_hwmgr *hwmgr = handle;
  675. if (!hwmgr || !hwmgr->pm_en)
  676. return NULL;
  677. if (idx < hwmgr->num_vce_state_tables)
  678. return &hwmgr->vce_states[idx];
  679. return NULL;
  680. }
  681. static int pp_get_power_profile_mode(void *handle, char *buf)
  682. {
  683. struct pp_hwmgr *hwmgr = handle;
  684. if (!hwmgr || !hwmgr->pm_en || !hwmgr->hwmgr_func->get_power_profile_mode)
  685. return -EOPNOTSUPP;
  686. if (!buf)
  687. return -EINVAL;
  688. return hwmgr->hwmgr_func->get_power_profile_mode(hwmgr, buf);
  689. }
  690. static int pp_set_power_profile_mode(void *handle, long *input, uint32_t size)
  691. {
  692. struct pp_hwmgr *hwmgr = handle;
  693. if (!hwmgr || !hwmgr->pm_en || !hwmgr->hwmgr_func->set_power_profile_mode)
  694. return -EOPNOTSUPP;
  695. if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
  696. pr_debug("power profile setting is for manual dpm mode only.\n");
  697. return -EINVAL;
  698. }
  699. return hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, input, size);
  700. }
  701. static int pp_set_fine_grain_clk_vol(void *handle, uint32_t type, long *input, uint32_t size)
  702. {
  703. struct pp_hwmgr *hwmgr = handle;
  704. if (!hwmgr || !hwmgr->pm_en)
  705. return -EINVAL;
  706. if (hwmgr->hwmgr_func->set_fine_grain_clk_vol == NULL)
  707. return 0;
  708. return hwmgr->hwmgr_func->set_fine_grain_clk_vol(hwmgr, type, input, size);
  709. }
  710. static int pp_odn_edit_dpm_table(void *handle, enum PP_OD_DPM_TABLE_COMMAND type,
  711. long *input, uint32_t size)
  712. {
  713. struct pp_hwmgr *hwmgr = handle;
  714. if (!hwmgr || !hwmgr->pm_en)
  715. return -EINVAL;
  716. if (hwmgr->hwmgr_func->odn_edit_dpm_table == NULL) {
  717. pr_info_ratelimited("%s was not implemented.\n", __func__);
  718. return 0;
  719. }
  720. return hwmgr->hwmgr_func->odn_edit_dpm_table(hwmgr, type, input, size);
  721. }
  722. static int pp_dpm_set_mp1_state(void *handle, enum pp_mp1_state mp1_state)
  723. {
  724. struct pp_hwmgr *hwmgr = handle;
  725. if (!hwmgr)
  726. return -EINVAL;
  727. if (!hwmgr->pm_en)
  728. return 0;
  729. if (hwmgr->hwmgr_func->set_mp1_state)
  730. return hwmgr->hwmgr_func->set_mp1_state(hwmgr, mp1_state);
  731. return 0;
  732. }
  733. static int pp_dpm_switch_power_profile(void *handle,
  734. enum PP_SMC_POWER_PROFILE type, bool en)
  735. {
  736. struct pp_hwmgr *hwmgr = handle;
  737. long workload[1];
  738. uint32_t index;
  739. if (!hwmgr || !hwmgr->pm_en)
  740. return -EINVAL;
  741. if (hwmgr->hwmgr_func->set_power_profile_mode == NULL) {
  742. pr_info_ratelimited("%s was not implemented.\n", __func__);
  743. return -EINVAL;
  744. }
  745. if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
  746. return -EINVAL;
  747. if (!en) {
  748. hwmgr->workload_mask &= ~(1 << hwmgr->workload_prority[type]);
  749. index = fls(hwmgr->workload_mask);
  750. index = index > 0 && index <= Workload_Policy_Max ? index - 1 : 0;
  751. workload[0] = hwmgr->workload_setting[index];
  752. } else {
  753. hwmgr->workload_mask |= (1 << hwmgr->workload_prority[type]);
  754. index = fls(hwmgr->workload_mask);
  755. index = index <= Workload_Policy_Max ? index - 1 : 0;
  756. workload[0] = hwmgr->workload_setting[index];
  757. }
  758. if (type == PP_SMC_POWER_PROFILE_COMPUTE &&
  759. hwmgr->hwmgr_func->disable_power_features_for_compute_performance) {
  760. if (hwmgr->hwmgr_func->disable_power_features_for_compute_performance(hwmgr, en))
  761. return -EINVAL;
  762. }
  763. if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
  764. hwmgr->hwmgr_func->set_power_profile_mode(hwmgr, workload, 0);
  765. return 0;
  766. }
  767. static int pp_set_power_limit(void *handle, uint32_t limit)
  768. {
  769. struct pp_hwmgr *hwmgr = handle;
  770. uint32_t max_power_limit;
  771. if (!hwmgr || !hwmgr->pm_en)
  772. return -EINVAL;
  773. if (hwmgr->hwmgr_func->set_power_limit == NULL) {
  774. pr_info_ratelimited("%s was not implemented.\n", __func__);
  775. return -EINVAL;
  776. }
  777. if (limit == 0)
  778. limit = hwmgr->default_power_limit;
  779. max_power_limit = hwmgr->default_power_limit;
  780. if (hwmgr->od_enabled) {
  781. max_power_limit *= (100 + hwmgr->platform_descriptor.TDPODLimit);
  782. max_power_limit /= 100;
  783. }
  784. if (limit > max_power_limit)
  785. return -EINVAL;
  786. hwmgr->hwmgr_func->set_power_limit(hwmgr, limit);
  787. hwmgr->power_limit = limit;
  788. return 0;
  789. }
  790. static int pp_get_power_limit(void *handle, uint32_t *limit,
  791. enum pp_power_limit_level pp_limit_level,
  792. enum pp_power_type power_type)
  793. {
  794. struct pp_hwmgr *hwmgr = handle;
  795. int ret = 0;
  796. if (!hwmgr || !hwmgr->pm_en || !limit)
  797. return -EINVAL;
  798. if (power_type != PP_PWR_TYPE_SUSTAINED)
  799. return -EOPNOTSUPP;
  800. switch (pp_limit_level) {
  801. case PP_PWR_LIMIT_CURRENT:
  802. *limit = hwmgr->power_limit;
  803. break;
  804. case PP_PWR_LIMIT_DEFAULT:
  805. *limit = hwmgr->default_power_limit;
  806. break;
  807. case PP_PWR_LIMIT_MAX:
  808. *limit = hwmgr->default_power_limit;
  809. if (hwmgr->od_enabled) {
  810. *limit *= (100 + hwmgr->platform_descriptor.TDPODLimit);
  811. *limit /= 100;
  812. }
  813. break;
  814. case PP_PWR_LIMIT_MIN:
  815. *limit = 0;
  816. break;
  817. default:
  818. ret = -EOPNOTSUPP;
  819. break;
  820. }
  821. return ret;
  822. }
  823. static int pp_display_configuration_change(void *handle,
  824. const struct amd_pp_display_configuration *display_config)
  825. {
  826. struct pp_hwmgr *hwmgr = handle;
  827. if (!hwmgr || !hwmgr->pm_en)
  828. return -EINVAL;
  829. phm_store_dal_configuration_data(hwmgr, display_config);
  830. return 0;
  831. }
  832. static int pp_get_display_power_level(void *handle,
  833. struct amd_pp_simple_clock_info *output)
  834. {
  835. struct pp_hwmgr *hwmgr = handle;
  836. if (!hwmgr || !hwmgr->pm_en || !output)
  837. return -EINVAL;
  838. return phm_get_dal_power_level(hwmgr, output);
  839. }
  840. static int pp_get_current_clocks(void *handle,
  841. struct amd_pp_clock_info *clocks)
  842. {
  843. struct amd_pp_simple_clock_info simple_clocks = { 0 };
  844. struct pp_clock_info hw_clocks;
  845. struct pp_hwmgr *hwmgr = handle;
  846. int ret = 0;
  847. if (!hwmgr || !hwmgr->pm_en)
  848. return -EINVAL;
  849. phm_get_dal_power_level(hwmgr, &simple_clocks);
  850. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  851. PHM_PlatformCaps_PowerContainment))
  852. ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
  853. &hw_clocks, PHM_PerformanceLevelDesignation_PowerContainment);
  854. else
  855. ret = phm_get_clock_info(hwmgr, &hwmgr->current_ps->hardware,
  856. &hw_clocks, PHM_PerformanceLevelDesignation_Activity);
  857. if (ret) {
  858. pr_debug("Error in phm_get_clock_info \n");
  859. return -EINVAL;
  860. }
  861. clocks->min_engine_clock = hw_clocks.min_eng_clk;
  862. clocks->max_engine_clock = hw_clocks.max_eng_clk;
  863. clocks->min_memory_clock = hw_clocks.min_mem_clk;
  864. clocks->max_memory_clock = hw_clocks.max_mem_clk;
  865. clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
  866. clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
  867. clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
  868. clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
  869. if (simple_clocks.level == 0)
  870. clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
  871. else
  872. clocks->max_clocks_state = simple_clocks.level;
  873. if (0 == phm_get_current_shallow_sleep_clocks(hwmgr, &hwmgr->current_ps->hardware, &hw_clocks)) {
  874. clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
  875. clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
  876. }
  877. return 0;
  878. }
  879. static int pp_get_clock_by_type(void *handle, enum amd_pp_clock_type type, struct amd_pp_clocks *clocks)
  880. {
  881. struct pp_hwmgr *hwmgr = handle;
  882. if (!hwmgr || !hwmgr->pm_en)
  883. return -EINVAL;
  884. if (clocks == NULL)
  885. return -EINVAL;
  886. return phm_get_clock_by_type(hwmgr, type, clocks);
  887. }
  888. static int pp_get_clock_by_type_with_latency(void *handle,
  889. enum amd_pp_clock_type type,
  890. struct pp_clock_levels_with_latency *clocks)
  891. {
  892. struct pp_hwmgr *hwmgr = handle;
  893. if (!hwmgr || !hwmgr->pm_en || !clocks)
  894. return -EINVAL;
  895. return phm_get_clock_by_type_with_latency(hwmgr, type, clocks);
  896. }
  897. static int pp_get_clock_by_type_with_voltage(void *handle,
  898. enum amd_pp_clock_type type,
  899. struct pp_clock_levels_with_voltage *clocks)
  900. {
  901. struct pp_hwmgr *hwmgr = handle;
  902. if (!hwmgr || !hwmgr->pm_en || !clocks)
  903. return -EINVAL;
  904. return phm_get_clock_by_type_with_voltage(hwmgr, type, clocks);
  905. }
  906. static int pp_set_watermarks_for_clocks_ranges(void *handle,
  907. void *clock_ranges)
  908. {
  909. struct pp_hwmgr *hwmgr = handle;
  910. if (!hwmgr || !hwmgr->pm_en || !clock_ranges)
  911. return -EINVAL;
  912. return phm_set_watermarks_for_clocks_ranges(hwmgr,
  913. clock_ranges);
  914. }
  915. static int pp_display_clock_voltage_request(void *handle,
  916. struct pp_display_clock_request *clock)
  917. {
  918. struct pp_hwmgr *hwmgr = handle;
  919. if (!hwmgr || !hwmgr->pm_en || !clock)
  920. return -EINVAL;
  921. return phm_display_clock_voltage_request(hwmgr, clock);
  922. }
  923. static int pp_get_display_mode_validation_clocks(void *handle,
  924. struct amd_pp_simple_clock_info *clocks)
  925. {
  926. struct pp_hwmgr *hwmgr = handle;
  927. int ret = 0;
  928. if (!hwmgr || !hwmgr->pm_en || !clocks)
  929. return -EINVAL;
  930. clocks->level = PP_DAL_POWERLEVEL_7;
  931. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_DynamicPatchPowerState))
  932. ret = phm_get_max_high_clocks(hwmgr, clocks);
  933. return ret;
  934. }
  935. static int pp_dpm_powergate_mmhub(void *handle)
  936. {
  937. struct pp_hwmgr *hwmgr = handle;
  938. if (!hwmgr || !hwmgr->pm_en)
  939. return -EINVAL;
  940. if (hwmgr->hwmgr_func->powergate_mmhub == NULL) {
  941. pr_info_ratelimited("%s was not implemented.\n", __func__);
  942. return 0;
  943. }
  944. return hwmgr->hwmgr_func->powergate_mmhub(hwmgr);
  945. }
  946. static int pp_dpm_powergate_gfx(void *handle, bool gate)
  947. {
  948. struct pp_hwmgr *hwmgr = handle;
  949. if (!hwmgr || !hwmgr->pm_en)
  950. return 0;
  951. if (hwmgr->hwmgr_func->powergate_gfx == NULL) {
  952. pr_info_ratelimited("%s was not implemented.\n", __func__);
  953. return 0;
  954. }
  955. return hwmgr->hwmgr_func->powergate_gfx(hwmgr, gate);
  956. }
  957. static void pp_dpm_powergate_acp(void *handle, bool gate)
  958. {
  959. struct pp_hwmgr *hwmgr = handle;
  960. if (!hwmgr || !hwmgr->pm_en)
  961. return;
  962. if (hwmgr->hwmgr_func->powergate_acp == NULL) {
  963. pr_info_ratelimited("%s was not implemented.\n", __func__);
  964. return;
  965. }
  966. hwmgr->hwmgr_func->powergate_acp(hwmgr, gate);
  967. }
  968. static void pp_dpm_powergate_sdma(void *handle, bool gate)
  969. {
  970. struct pp_hwmgr *hwmgr = handle;
  971. if (!hwmgr)
  972. return;
  973. if (hwmgr->hwmgr_func->powergate_sdma == NULL) {
  974. pr_info_ratelimited("%s was not implemented.\n", __func__);
  975. return;
  976. }
  977. hwmgr->hwmgr_func->powergate_sdma(hwmgr, gate);
  978. }
  979. static int pp_set_powergating_by_smu(void *handle,
  980. uint32_t block_type, bool gate)
  981. {
  982. int ret = 0;
  983. switch (block_type) {
  984. case AMD_IP_BLOCK_TYPE_UVD:
  985. case AMD_IP_BLOCK_TYPE_VCN:
  986. pp_dpm_powergate_uvd(handle, gate);
  987. break;
  988. case AMD_IP_BLOCK_TYPE_VCE:
  989. pp_dpm_powergate_vce(handle, gate);
  990. break;
  991. case AMD_IP_BLOCK_TYPE_GMC:
  992. /*
  993. * For now, this is only used on PICASSO.
  994. * And only "gate" operation is supported.
  995. */
  996. if (gate)
  997. pp_dpm_powergate_mmhub(handle);
  998. break;
  999. case AMD_IP_BLOCK_TYPE_GFX:
  1000. ret = pp_dpm_powergate_gfx(handle, gate);
  1001. break;
  1002. case AMD_IP_BLOCK_TYPE_ACP:
  1003. pp_dpm_powergate_acp(handle, gate);
  1004. break;
  1005. case AMD_IP_BLOCK_TYPE_SDMA:
  1006. pp_dpm_powergate_sdma(handle, gate);
  1007. break;
  1008. default:
  1009. break;
  1010. }
  1011. return ret;
  1012. }
  1013. static int pp_notify_smu_enable_pwe(void *handle)
  1014. {
  1015. struct pp_hwmgr *hwmgr = handle;
  1016. if (!hwmgr || !hwmgr->pm_en)
  1017. return -EINVAL;
  1018. if (hwmgr->hwmgr_func->smus_notify_pwe == NULL) {
  1019. pr_info_ratelimited("%s was not implemented.\n", __func__);
  1020. return -EINVAL;
  1021. }
  1022. hwmgr->hwmgr_func->smus_notify_pwe(hwmgr);
  1023. return 0;
  1024. }
  1025. static int pp_enable_mgpu_fan_boost(void *handle)
  1026. {
  1027. struct pp_hwmgr *hwmgr = handle;
  1028. if (!hwmgr)
  1029. return -EINVAL;
  1030. if (!hwmgr->pm_en ||
  1031. hwmgr->hwmgr_func->enable_mgpu_fan_boost == NULL)
  1032. return 0;
  1033. hwmgr->hwmgr_func->enable_mgpu_fan_boost(hwmgr);
  1034. return 0;
  1035. }
  1036. static int pp_set_min_deep_sleep_dcefclk(void *handle, uint32_t clock)
  1037. {
  1038. struct pp_hwmgr *hwmgr = handle;
  1039. if (!hwmgr || !hwmgr->pm_en)
  1040. return -EINVAL;
  1041. if (hwmgr->hwmgr_func->set_min_deep_sleep_dcefclk == NULL) {
  1042. pr_debug("%s was not implemented.\n", __func__);
  1043. return -EINVAL;
  1044. }
  1045. hwmgr->hwmgr_func->set_min_deep_sleep_dcefclk(hwmgr, clock);
  1046. return 0;
  1047. }
  1048. static int pp_set_hard_min_dcefclk_by_freq(void *handle, uint32_t clock)
  1049. {
  1050. struct pp_hwmgr *hwmgr = handle;
  1051. if (!hwmgr || !hwmgr->pm_en)
  1052. return -EINVAL;
  1053. if (hwmgr->hwmgr_func->set_hard_min_dcefclk_by_freq == NULL) {
  1054. pr_debug("%s was not implemented.\n", __func__);
  1055. return -EINVAL;
  1056. }
  1057. hwmgr->hwmgr_func->set_hard_min_dcefclk_by_freq(hwmgr, clock);
  1058. return 0;
  1059. }
  1060. static int pp_set_hard_min_fclk_by_freq(void *handle, uint32_t clock)
  1061. {
  1062. struct pp_hwmgr *hwmgr = handle;
  1063. if (!hwmgr || !hwmgr->pm_en)
  1064. return -EINVAL;
  1065. if (hwmgr->hwmgr_func->set_hard_min_fclk_by_freq == NULL) {
  1066. pr_debug("%s was not implemented.\n", __func__);
  1067. return -EINVAL;
  1068. }
  1069. hwmgr->hwmgr_func->set_hard_min_fclk_by_freq(hwmgr, clock);
  1070. return 0;
  1071. }
  1072. static int pp_set_active_display_count(void *handle, uint32_t count)
  1073. {
  1074. struct pp_hwmgr *hwmgr = handle;
  1075. if (!hwmgr || !hwmgr->pm_en)
  1076. return -EINVAL;
  1077. return phm_set_active_display_count(hwmgr, count);
  1078. }
  1079. static int pp_get_asic_baco_capability(void *handle)
  1080. {
  1081. struct pp_hwmgr *hwmgr = handle;
  1082. if (!hwmgr)
  1083. return false;
  1084. if (!(hwmgr->not_vf && amdgpu_dpm) ||
  1085. !hwmgr->hwmgr_func->get_bamaco_support)
  1086. return false;
  1087. return hwmgr->hwmgr_func->get_bamaco_support(hwmgr);
  1088. }
  1089. static int pp_get_asic_baco_state(void *handle, int *state)
  1090. {
  1091. struct pp_hwmgr *hwmgr = handle;
  1092. if (!hwmgr)
  1093. return -EINVAL;
  1094. if (!hwmgr->pm_en || !hwmgr->hwmgr_func->get_asic_baco_state)
  1095. return 0;
  1096. hwmgr->hwmgr_func->get_asic_baco_state(hwmgr, (enum BACO_STATE *)state);
  1097. return 0;
  1098. }
  1099. static int pp_set_asic_baco_state(void *handle, int state)
  1100. {
  1101. struct pp_hwmgr *hwmgr = handle;
  1102. if (!hwmgr)
  1103. return -EINVAL;
  1104. if (!(hwmgr->not_vf && amdgpu_dpm) ||
  1105. !hwmgr->hwmgr_func->set_asic_baco_state)
  1106. return 0;
  1107. hwmgr->hwmgr_func->set_asic_baco_state(hwmgr, (enum BACO_STATE)state);
  1108. return 0;
  1109. }
  1110. static int pp_get_ppfeature_status(void *handle, char *buf)
  1111. {
  1112. struct pp_hwmgr *hwmgr = handle;
  1113. if (!hwmgr || !hwmgr->pm_en || !buf)
  1114. return -EINVAL;
  1115. if (hwmgr->hwmgr_func->get_ppfeature_status == NULL) {
  1116. pr_info_ratelimited("%s was not implemented.\n", __func__);
  1117. return -EINVAL;
  1118. }
  1119. return hwmgr->hwmgr_func->get_ppfeature_status(hwmgr, buf);
  1120. }
  1121. static int pp_set_ppfeature_status(void *handle, uint64_t ppfeature_masks)
  1122. {
  1123. struct pp_hwmgr *hwmgr = handle;
  1124. if (!hwmgr || !hwmgr->pm_en)
  1125. return -EINVAL;
  1126. if (hwmgr->hwmgr_func->set_ppfeature_status == NULL) {
  1127. pr_info_ratelimited("%s was not implemented.\n", __func__);
  1128. return -EINVAL;
  1129. }
  1130. return hwmgr->hwmgr_func->set_ppfeature_status(hwmgr, ppfeature_masks);
  1131. }
  1132. static int pp_asic_reset_mode_2(void *handle)
  1133. {
  1134. struct pp_hwmgr *hwmgr = handle;
  1135. if (!hwmgr || !hwmgr->pm_en)
  1136. return -EINVAL;
  1137. if (hwmgr->hwmgr_func->asic_reset == NULL) {
  1138. pr_info_ratelimited("%s was not implemented.\n", __func__);
  1139. return -EINVAL;
  1140. }
  1141. return hwmgr->hwmgr_func->asic_reset(hwmgr, SMU_ASIC_RESET_MODE_2);
  1142. }
  1143. static int pp_smu_i2c_bus_access(void *handle, bool acquire)
  1144. {
  1145. struct pp_hwmgr *hwmgr = handle;
  1146. if (!hwmgr || !hwmgr->pm_en)
  1147. return -EINVAL;
  1148. if (hwmgr->hwmgr_func->smu_i2c_bus_access == NULL) {
  1149. pr_info_ratelimited("%s was not implemented.\n", __func__);
  1150. return -EINVAL;
  1151. }
  1152. return hwmgr->hwmgr_func->smu_i2c_bus_access(hwmgr, acquire);
  1153. }
  1154. static int pp_set_df_cstate(void *handle, enum pp_df_cstate state)
  1155. {
  1156. struct pp_hwmgr *hwmgr = handle;
  1157. if (!hwmgr)
  1158. return -EINVAL;
  1159. if (!hwmgr->pm_en || !hwmgr->hwmgr_func->set_df_cstate)
  1160. return 0;
  1161. hwmgr->hwmgr_func->set_df_cstate(hwmgr, state);
  1162. return 0;
  1163. }
  1164. static int pp_set_xgmi_pstate(void *handle, uint32_t pstate)
  1165. {
  1166. struct pp_hwmgr *hwmgr = handle;
  1167. if (!hwmgr)
  1168. return -EINVAL;
  1169. if (!hwmgr->pm_en || !hwmgr->hwmgr_func->set_xgmi_pstate)
  1170. return 0;
  1171. hwmgr->hwmgr_func->set_xgmi_pstate(hwmgr, pstate);
  1172. return 0;
  1173. }
  1174. static ssize_t pp_get_gpu_metrics(void *handle, void **table)
  1175. {
  1176. struct pp_hwmgr *hwmgr = handle;
  1177. if (!hwmgr)
  1178. return -EINVAL;
  1179. if (!hwmgr->pm_en || !hwmgr->hwmgr_func->get_gpu_metrics)
  1180. return -EOPNOTSUPP;
  1181. return hwmgr->hwmgr_func->get_gpu_metrics(hwmgr, table);
  1182. }
  1183. static int pp_gfx_state_change_set(void *handle, uint32_t state)
  1184. {
  1185. struct pp_hwmgr *hwmgr = handle;
  1186. if (!hwmgr || !hwmgr->pm_en)
  1187. return -EINVAL;
  1188. if (hwmgr->hwmgr_func->gfx_state_change == NULL) {
  1189. pr_info_ratelimited("%s was not implemented.\n", __func__);
  1190. return -EINVAL;
  1191. }
  1192. hwmgr->hwmgr_func->gfx_state_change(hwmgr, state);
  1193. return 0;
  1194. }
  1195. static int pp_get_prv_buffer_details(void *handle, void **addr, size_t *size)
  1196. {
  1197. struct pp_hwmgr *hwmgr = handle;
  1198. struct amdgpu_device *adev = hwmgr->adev;
  1199. int err;
  1200. if (!addr || !size)
  1201. return -EINVAL;
  1202. *addr = NULL;
  1203. *size = 0;
  1204. if (adev->pm.smu_prv_buffer) {
  1205. err = amdgpu_bo_kmap(adev->pm.smu_prv_buffer, addr);
  1206. if (err)
  1207. return err;
  1208. *size = adev->pm.smu_prv_buffer_size;
  1209. }
  1210. return 0;
  1211. }
  1212. static void pp_pm_compute_clocks(void *handle)
  1213. {
  1214. struct pp_hwmgr *hwmgr = handle;
  1215. struct amdgpu_device *adev = hwmgr->adev;
  1216. if (!adev->dc_enabled) {
  1217. amdgpu_dpm_get_active_displays(adev);
  1218. adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
  1219. adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
  1220. adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
  1221. /* we have issues with mclk switching with
  1222. * refresh rates over 120 hz on the non-DC code.
  1223. */
  1224. if (adev->pm.pm_display_cfg.vrefresh > 120)
  1225. adev->pm.pm_display_cfg.min_vblank_time = 0;
  1226. pp_display_configuration_change(handle,
  1227. &adev->pm.pm_display_cfg);
  1228. }
  1229. pp_dpm_dispatch_tasks(handle,
  1230. AMD_PP_TASK_DISPLAY_CONFIG_CHANGE,
  1231. NULL);
  1232. }
  1233. static const struct amd_pm_funcs pp_dpm_funcs = {
  1234. .load_firmware = pp_dpm_load_fw,
  1235. .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
  1236. .force_performance_level = pp_dpm_force_performance_level,
  1237. .get_performance_level = pp_dpm_get_performance_level,
  1238. .get_current_power_state = pp_dpm_get_current_power_state,
  1239. .dispatch_tasks = pp_dpm_dispatch_tasks,
  1240. .set_fan_control_mode = pp_dpm_set_fan_control_mode,
  1241. .get_fan_control_mode = pp_dpm_get_fan_control_mode,
  1242. .set_fan_speed_pwm = pp_dpm_set_fan_speed_pwm,
  1243. .get_fan_speed_pwm = pp_dpm_get_fan_speed_pwm,
  1244. .get_fan_speed_rpm = pp_dpm_get_fan_speed_rpm,
  1245. .set_fan_speed_rpm = pp_dpm_set_fan_speed_rpm,
  1246. .get_pp_num_states = pp_dpm_get_pp_num_states,
  1247. .get_pp_table = pp_dpm_get_pp_table,
  1248. .set_pp_table = pp_dpm_set_pp_table,
  1249. .force_clock_level = pp_dpm_force_clock_level,
  1250. .emit_clock_levels = pp_dpm_emit_clock_levels,
  1251. .print_clock_levels = pp_dpm_print_clock_levels,
  1252. .get_sclk_od = pp_dpm_get_sclk_od,
  1253. .set_sclk_od = pp_dpm_set_sclk_od,
  1254. .get_mclk_od = pp_dpm_get_mclk_od,
  1255. .set_mclk_od = pp_dpm_set_mclk_od,
  1256. .read_sensor = pp_dpm_read_sensor,
  1257. .get_vce_clock_state = pp_dpm_get_vce_clock_state,
  1258. .switch_power_profile = pp_dpm_switch_power_profile,
  1259. .set_clockgating_by_smu = pp_set_clockgating_by_smu,
  1260. .set_powergating_by_smu = pp_set_powergating_by_smu,
  1261. .get_power_profile_mode = pp_get_power_profile_mode,
  1262. .set_power_profile_mode = pp_set_power_profile_mode,
  1263. .set_fine_grain_clk_vol = pp_set_fine_grain_clk_vol,
  1264. .odn_edit_dpm_table = pp_odn_edit_dpm_table,
  1265. .set_mp1_state = pp_dpm_set_mp1_state,
  1266. .set_power_limit = pp_set_power_limit,
  1267. .get_power_limit = pp_get_power_limit,
  1268. /* export to DC */
  1269. .get_sclk = pp_dpm_get_sclk,
  1270. .get_mclk = pp_dpm_get_mclk,
  1271. .display_configuration_change = pp_display_configuration_change,
  1272. .get_display_power_level = pp_get_display_power_level,
  1273. .get_current_clocks = pp_get_current_clocks,
  1274. .get_clock_by_type = pp_get_clock_by_type,
  1275. .get_clock_by_type_with_latency = pp_get_clock_by_type_with_latency,
  1276. .get_clock_by_type_with_voltage = pp_get_clock_by_type_with_voltage,
  1277. .set_watermarks_for_clocks_ranges = pp_set_watermarks_for_clocks_ranges,
  1278. .display_clock_voltage_request = pp_display_clock_voltage_request,
  1279. .get_display_mode_validation_clocks = pp_get_display_mode_validation_clocks,
  1280. .notify_smu_enable_pwe = pp_notify_smu_enable_pwe,
  1281. .enable_mgpu_fan_boost = pp_enable_mgpu_fan_boost,
  1282. .set_active_display_count = pp_set_active_display_count,
  1283. .set_min_deep_sleep_dcefclk = pp_set_min_deep_sleep_dcefclk,
  1284. .set_hard_min_dcefclk_by_freq = pp_set_hard_min_dcefclk_by_freq,
  1285. .set_hard_min_fclk_by_freq = pp_set_hard_min_fclk_by_freq,
  1286. .get_asic_baco_capability = pp_get_asic_baco_capability,
  1287. .get_asic_baco_state = pp_get_asic_baco_state,
  1288. .set_asic_baco_state = pp_set_asic_baco_state,
  1289. .get_ppfeature_status = pp_get_ppfeature_status,
  1290. .set_ppfeature_status = pp_set_ppfeature_status,
  1291. .asic_reset_mode_2 = pp_asic_reset_mode_2,
  1292. .smu_i2c_bus_access = pp_smu_i2c_bus_access,
  1293. .set_df_cstate = pp_set_df_cstate,
  1294. .set_xgmi_pstate = pp_set_xgmi_pstate,
  1295. .get_gpu_metrics = pp_get_gpu_metrics,
  1296. .gfx_state_change_set = pp_gfx_state_change_set,
  1297. .get_smu_prv_buf_details = pp_get_prv_buffer_details,
  1298. .pm_compute_clocks = pp_pm_compute_clocks,
  1299. };