rv_hwmgr.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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/slab.h>
  27. #include "atom-types.h"
  28. #include "atombios.h"
  29. #include "processpptables.h"
  30. #include "cgs_common.h"
  31. #include "smumgr.h"
  32. #include "hwmgr.h"
  33. #include "hardwaremanager.h"
  34. #include "rv_ppsmc.h"
  35. #include "rv_hwmgr.h"
  36. #include "power_state.h"
  37. #include "rv_smumgr.h"
  38. #include "pp_soc15.h"
  39. #define RAVEN_MAX_DEEPSLEEP_DIVIDER_ID 5
  40. #define RAVEN_MINIMUM_ENGINE_CLOCK 800 //8Mhz, the low boundary of engine clock allowed on this chip
  41. #define SCLK_MIN_DIV_INTV_SHIFT 12
  42. #define RAVEN_DISPCLK_BYPASS_THRESHOLD 10000 //100mhz
  43. #define SMC_RAM_END 0x40000
  44. static const unsigned long PhwRaven_Magic = (unsigned long) PHM_Rv_Magic;
  45. int rv_display_clock_voltage_request(struct pp_hwmgr *hwmgr,
  46. struct pp_display_clock_request *clock_req);
  47. struct phm_vq_budgeting_record rv_vqtable[] = {
  48. /* _TBD
  49. * CUs, SSP low, SSP High, Min Sclk Low, Min Sclk, High, AWD/non-AWD, DCLK, ECLK, Sustainable Sclk, Sustainable CUs */
  50. { 8, 0, 45, 0, 0, VQ_DisplayConfig_NoneAWD, 80000, 120000, 4, 0 },
  51. };
  52. static struct rv_power_state *cast_rv_ps(struct pp_hw_power_state *hw_ps)
  53. {
  54. if (PhwRaven_Magic != hw_ps->magic)
  55. return NULL;
  56. return (struct rv_power_state *)hw_ps;
  57. }
  58. static const struct rv_power_state *cast_const_rv_ps(
  59. const struct pp_hw_power_state *hw_ps)
  60. {
  61. if (PhwRaven_Magic != hw_ps->magic)
  62. return NULL;
  63. return (struct rv_power_state *)hw_ps;
  64. }
  65. static int rv_init_vq_budget_table(struct pp_hwmgr *hwmgr)
  66. {
  67. uint32_t table_size, i;
  68. struct phm_vq_budgeting_table *ptable;
  69. uint32_t num_entries = ARRAY_SIZE(rv_vqtable);
  70. if (hwmgr->dyn_state.vq_budgeting_table != NULL)
  71. return 0;
  72. table_size = sizeof(struct phm_vq_budgeting_table) +
  73. sizeof(struct phm_vq_budgeting_record) * (num_entries - 1);
  74. ptable = kzalloc(table_size, GFP_KERNEL);
  75. if (NULL == ptable)
  76. return -ENOMEM;
  77. ptable->numEntries = (uint8_t) num_entries;
  78. for (i = 0; i < ptable->numEntries; i++) {
  79. ptable->entries[i].ulCUs = rv_vqtable[i].ulCUs;
  80. ptable->entries[i].ulSustainableSOCPowerLimitLow = rv_vqtable[i].ulSustainableSOCPowerLimitLow;
  81. ptable->entries[i].ulSustainableSOCPowerLimitHigh = rv_vqtable[i].ulSustainableSOCPowerLimitHigh;
  82. ptable->entries[i].ulMinSclkLow = rv_vqtable[i].ulMinSclkLow;
  83. ptable->entries[i].ulMinSclkHigh = rv_vqtable[i].ulMinSclkHigh;
  84. ptable->entries[i].ucDispConfig = rv_vqtable[i].ucDispConfig;
  85. ptable->entries[i].ulDClk = rv_vqtable[i].ulDClk;
  86. ptable->entries[i].ulEClk = rv_vqtable[i].ulEClk;
  87. ptable->entries[i].ulSustainableSclk = rv_vqtable[i].ulSustainableSclk;
  88. ptable->entries[i].ulSustainableCUs = rv_vqtable[i].ulSustainableCUs;
  89. }
  90. hwmgr->dyn_state.vq_budgeting_table = ptable;
  91. return 0;
  92. }
  93. static int rv_initialize_dpm_defaults(struct pp_hwmgr *hwmgr)
  94. {
  95. struct rv_hwmgr *rv_hwmgr = (struct rv_hwmgr *)(hwmgr->backend);
  96. struct cgs_system_info sys_info = {0};
  97. int result;
  98. rv_hwmgr->ddi_power_gating_disabled = 0;
  99. rv_hwmgr->bapm_enabled = 1;
  100. rv_hwmgr->dce_slow_sclk_threshold = 30000;
  101. rv_hwmgr->disable_driver_thermal_policy = 1;
  102. rv_hwmgr->thermal_auto_throttling_treshold = 0;
  103. rv_hwmgr->is_nb_dpm_enabled = 1;
  104. rv_hwmgr->dpm_flags = 1;
  105. rv_hwmgr->disable_smu_acp_s3_handshake = 1;
  106. rv_hwmgr->disable_notify_smu_vpu_recovery = 0;
  107. rv_hwmgr->gfx_off_controled_by_driver = false;
  108. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  109. PHM_PlatformCaps_DynamicM3Arbiter);
  110. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  111. PHM_PlatformCaps_UVDPowerGating);
  112. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  113. PHM_PlatformCaps_UVDDynamicPowerGating);
  114. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  115. PHM_PlatformCaps_VCEPowerGating);
  116. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  117. PHM_PlatformCaps_SamuPowerGating);
  118. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  119. PHM_PlatformCaps_ACP);
  120. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  121. PHM_PlatformCaps_SclkDeepSleep);
  122. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  123. PHM_PlatformCaps_GFXDynamicMGPowerGating);
  124. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  125. PHM_PlatformCaps_SclkThrottleLowNotification);
  126. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  127. PHM_PlatformCaps_DisableVoltageIsland);
  128. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  129. PHM_PlatformCaps_DynamicUVDState);
  130. sys_info.size = sizeof(struct cgs_system_info);
  131. sys_info.info_id = CGS_SYSTEM_INFO_PG_FLAGS;
  132. result = cgs_query_system_info(hwmgr->device, &sys_info);
  133. if (!result) {
  134. if (sys_info.value & AMD_PG_SUPPORT_GFX_DMG)
  135. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  136. PHM_PlatformCaps_GFXDynamicMGPowerGating);
  137. }
  138. return 0;
  139. }
  140. static int rv_construct_max_power_limits_table(struct pp_hwmgr *hwmgr,
  141. struct phm_clock_and_voltage_limits *table)
  142. {
  143. return 0;
  144. }
  145. static int rv_init_dynamic_state_adjustment_rule_settings(
  146. struct pp_hwmgr *hwmgr)
  147. {
  148. uint32_t table_size =
  149. sizeof(struct phm_clock_voltage_dependency_table) +
  150. (7 * sizeof(struct phm_clock_voltage_dependency_record));
  151. struct phm_clock_voltage_dependency_table *table_clk_vlt =
  152. kzalloc(table_size, GFP_KERNEL);
  153. if (NULL == table_clk_vlt) {
  154. pr_err("Can not allocate memory!\n");
  155. return -ENOMEM;
  156. }
  157. table_clk_vlt->count = 8;
  158. table_clk_vlt->entries[0].clk = PP_DAL_POWERLEVEL_0;
  159. table_clk_vlt->entries[0].v = 0;
  160. table_clk_vlt->entries[1].clk = PP_DAL_POWERLEVEL_1;
  161. table_clk_vlt->entries[1].v = 1;
  162. table_clk_vlt->entries[2].clk = PP_DAL_POWERLEVEL_2;
  163. table_clk_vlt->entries[2].v = 2;
  164. table_clk_vlt->entries[3].clk = PP_DAL_POWERLEVEL_3;
  165. table_clk_vlt->entries[3].v = 3;
  166. table_clk_vlt->entries[4].clk = PP_DAL_POWERLEVEL_4;
  167. table_clk_vlt->entries[4].v = 4;
  168. table_clk_vlt->entries[5].clk = PP_DAL_POWERLEVEL_5;
  169. table_clk_vlt->entries[5].v = 5;
  170. table_clk_vlt->entries[6].clk = PP_DAL_POWERLEVEL_6;
  171. table_clk_vlt->entries[6].v = 6;
  172. table_clk_vlt->entries[7].clk = PP_DAL_POWERLEVEL_7;
  173. table_clk_vlt->entries[7].v = 7;
  174. hwmgr->dyn_state.vddc_dep_on_dal_pwrl = table_clk_vlt;
  175. return 0;
  176. }
  177. static int rv_get_system_info_data(struct pp_hwmgr *hwmgr)
  178. {
  179. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)hwmgr->backend;
  180. rv_data->sys_info.htc_hyst_lmt = 5;
  181. rv_data->sys_info.htc_tmp_lmt = 203;
  182. if (rv_data->thermal_auto_throttling_treshold == 0)
  183. rv_data->thermal_auto_throttling_treshold = 203;
  184. rv_construct_max_power_limits_table (hwmgr,
  185. &hwmgr->dyn_state.max_clock_voltage_on_ac);
  186. rv_init_dynamic_state_adjustment_rule_settings(hwmgr);
  187. return 0;
  188. }
  189. static int rv_construct_boot_state(struct pp_hwmgr *hwmgr)
  190. {
  191. return 0;
  192. }
  193. static int rv_tf_set_clock_limit(struct pp_hwmgr *hwmgr, void *input,
  194. void *output, void *storage, int result)
  195. {
  196. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  197. struct PP_Clocks clocks = {0};
  198. struct pp_display_clock_request clock_req;
  199. clocks.dcefClock = hwmgr->display_config.min_dcef_set_clk;
  200. clocks.dcefClockInSR = hwmgr->display_config.min_dcef_deep_sleep_set_clk;
  201. clock_req.clock_type = amd_pp_dcf_clock;
  202. clock_req.clock_freq_in_khz = clocks.dcefClock * 10;
  203. if (clocks.dcefClock == 0 && clocks.dcefClockInSR == 0)
  204. clock_req.clock_freq_in_khz = rv_data->dcf_actual_hard_min_freq;
  205. PP_ASSERT_WITH_CODE(!rv_display_clock_voltage_request(hwmgr, &clock_req),
  206. "Attempt to set DCF Clock Failed!", return -EINVAL);
  207. if(rv_data->need_min_deep_sleep_dcefclk && 0 != clocks.dcefClockInSR)
  208. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  209. PPSMC_MSG_SetMinDeepSleepDcefclk,
  210. clocks.dcefClockInSR / 100);
  211. /*
  212. if(!rv_data->isp_tileA_power_gated || !rv_data->isp_tileB_power_gated) {
  213. if ((hwmgr->ispArbiter.iclk != 0) && (rv_data->ISPActualHardMinFreq != (hwmgr->ispArbiter.iclk / 100) )) {
  214. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  215. PPSMC_MSG_SetHardMinIspclkByFreq, hwmgr->ispArbiter.iclk / 100);
  216. rv_read_arg_from_smc(hwmgr->smumgr, &rv_data->ISPActualHardMinFreq),
  217. }
  218. } */
  219. if (((hwmgr->uvd_arbiter.vclk_soft_min / 100) != rv_data->vclk_soft_min) ||
  220. ((hwmgr->uvd_arbiter.dclk_soft_min / 100) != rv_data->dclk_soft_min)) {
  221. rv_data->vclk_soft_min = hwmgr->uvd_arbiter.vclk_soft_min / 100;
  222. rv_data->dclk_soft_min = hwmgr->uvd_arbiter.dclk_soft_min / 100;
  223. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  224. PPSMC_MSG_SetSoftMinVcn,
  225. (rv_data->vclk_soft_min << 16) | rv_data->vclk_soft_min);
  226. }
  227. if((hwmgr->gfx_arbiter.sclk_hard_min != 0) &&
  228. ((hwmgr->gfx_arbiter.sclk_hard_min / 100) != rv_data->soc_actual_hard_min_freq)) {
  229. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  230. PPSMC_MSG_SetHardMinSocclkByFreq,
  231. hwmgr->gfx_arbiter.sclk_hard_min / 100);
  232. rv_read_arg_from_smc(hwmgr->smumgr, &rv_data->soc_actual_hard_min_freq);
  233. }
  234. if ((hwmgr->gfx_arbiter.gfxclk != 0) &&
  235. (rv_data->gfx_actual_soft_min_freq != (hwmgr->gfx_arbiter.gfxclk))) {
  236. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  237. PPSMC_MSG_SetMinVideoGfxclkFreq,
  238. hwmgr->gfx_arbiter.gfxclk / 100);
  239. rv_read_arg_from_smc(hwmgr->smumgr, &rv_data->gfx_actual_soft_min_freq);
  240. }
  241. if ((hwmgr->gfx_arbiter.fclk != 0) &&
  242. (rv_data->fabric_actual_soft_min_freq != (hwmgr->gfx_arbiter.fclk / 100))) {
  243. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  244. PPSMC_MSG_SetMinVideoFclkFreq,
  245. hwmgr->gfx_arbiter.fclk / 100);
  246. rv_read_arg_from_smc(hwmgr->smumgr, &rv_data->fabric_actual_soft_min_freq);
  247. }
  248. return 0;
  249. }
  250. static int rv_tf_set_num_active_display(struct pp_hwmgr *hwmgr, void *input,
  251. void *output, void *storage, int result)
  252. {
  253. uint32_t num_of_active_displays = 0;
  254. struct cgs_display_info info = {0};
  255. cgs_get_active_displays_info(hwmgr->device, &info);
  256. num_of_active_displays = info.display_count;
  257. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  258. PPSMC_MSG_SetDisplayCount,
  259. num_of_active_displays);
  260. return 0;
  261. }
  262. static const struct phm_master_table_item rv_set_power_state_list[] = {
  263. { .tableFunction = rv_tf_set_clock_limit },
  264. { .tableFunction = rv_tf_set_num_active_display },
  265. { }
  266. };
  267. static const struct phm_master_table_header rv_set_power_state_master = {
  268. 0,
  269. PHM_MasterTableFlag_None,
  270. rv_set_power_state_list
  271. };
  272. static int rv_tf_init_power_gate_state(struct pp_hwmgr *hwmgr, void *input,
  273. void *output, void *storage, int result)
  274. {
  275. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  276. rv_data->vcn_power_gated = true;
  277. rv_data->isp_tileA_power_gated = true;
  278. rv_data->isp_tileB_power_gated = true;
  279. return 0;
  280. }
  281. static const struct phm_master_table_item rv_setup_asic_list[] = {
  282. { .tableFunction = rv_tf_init_power_gate_state },
  283. { }
  284. };
  285. static const struct phm_master_table_header rv_setup_asic_master = {
  286. 0,
  287. PHM_MasterTableFlag_None,
  288. rv_setup_asic_list
  289. };
  290. static int rv_tf_reset_cc6_data(struct pp_hwmgr *hwmgr,
  291. void *input, void *output,
  292. void *storage, int result)
  293. {
  294. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  295. rv_data->separation_time = 0;
  296. rv_data->cc6_disable = false;
  297. rv_data->pstate_disable = false;
  298. rv_data->cc6_setting_changed = false;
  299. return 0;
  300. }
  301. static const struct phm_master_table_item rv_power_down_asic_list[] = {
  302. { .tableFunction = rv_tf_reset_cc6_data },
  303. { }
  304. };
  305. static const struct phm_master_table_header rv_power_down_asic_master = {
  306. 0,
  307. PHM_MasterTableFlag_None,
  308. rv_power_down_asic_list
  309. };
  310. static int rv_tf_disable_gfx_off(struct pp_hwmgr *hwmgr,
  311. void *input, void *output,
  312. void *storage, int result)
  313. {
  314. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  315. if (rv_data->gfx_off_controled_by_driver)
  316. smum_send_msg_to_smc(hwmgr->smumgr,
  317. PPSMC_MSG_DisableGfxOff);
  318. return 0;
  319. }
  320. static const struct phm_master_table_item rv_disable_dpm_list[] = {
  321. { .tableFunction = rv_tf_disable_gfx_off },
  322. { },
  323. };
  324. static const struct phm_master_table_header rv_disable_dpm_master = {
  325. 0,
  326. PHM_MasterTableFlag_None,
  327. rv_disable_dpm_list
  328. };
  329. static int rv_tf_enable_gfx_off(struct pp_hwmgr *hwmgr,
  330. void *input, void *output,
  331. void *storage, int result)
  332. {
  333. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  334. if (rv_data->gfx_off_controled_by_driver)
  335. smum_send_msg_to_smc(hwmgr->smumgr,
  336. PPSMC_MSG_EnableGfxOff);
  337. return 0;
  338. }
  339. static const struct phm_master_table_item rv_enable_dpm_list[] = {
  340. { .tableFunction = rv_tf_enable_gfx_off },
  341. { },
  342. };
  343. static const struct phm_master_table_header rv_enable_dpm_master = {
  344. 0,
  345. PHM_MasterTableFlag_None,
  346. rv_enable_dpm_list
  347. };
  348. static int rv_apply_state_adjust_rules(struct pp_hwmgr *hwmgr,
  349. struct pp_power_state *prequest_ps,
  350. const struct pp_power_state *pcurrent_ps)
  351. {
  352. return 0;
  353. }
  354. /* temporary hardcoded clock voltage breakdown tables */
  355. DpmClock_t VddDcfClk[]= {
  356. { 300, 2600},
  357. { 600, 3200},
  358. { 600, 3600},
  359. };
  360. DpmClock_t VddSocClk[]= {
  361. { 478, 2600},
  362. { 722, 3200},
  363. { 722, 3600},
  364. };
  365. DpmClock_t VddFClk[]= {
  366. { 400, 2600},
  367. {1200, 3200},
  368. {1200, 3600},
  369. };
  370. DpmClock_t VddDispClk[]= {
  371. { 435, 2600},
  372. { 661, 3200},
  373. {1086, 3600},
  374. };
  375. DpmClock_t VddDppClk[]= {
  376. { 435, 2600},
  377. { 661, 3200},
  378. { 661, 3600},
  379. };
  380. DpmClock_t VddPhyClk[]= {
  381. { 540, 2600},
  382. { 810, 3200},
  383. { 810, 3600},
  384. };
  385. static int rv_get_clock_voltage_dependency_table(struct pp_hwmgr *hwmgr,
  386. struct rv_voltage_dependency_table **pptable,
  387. uint32_t num_entry, DpmClock_t *pclk_dependency_table)
  388. {
  389. uint32_t table_size, i;
  390. struct rv_voltage_dependency_table *ptable;
  391. table_size = sizeof(uint32_t) + sizeof(struct rv_voltage_dependency_table) * num_entry;
  392. ptable = kzalloc(table_size, GFP_KERNEL);
  393. if (NULL == ptable)
  394. return -ENOMEM;
  395. ptable->count = num_entry;
  396. for (i = 0; i < ptable->count; i++) {
  397. ptable->entries[i].clk = pclk_dependency_table->Freq * 100;
  398. ptable->entries[i].vol = pclk_dependency_table->Vol;
  399. pclk_dependency_table++;
  400. }
  401. *pptable = ptable;
  402. return 0;
  403. }
  404. static int rv_populate_clock_table(struct pp_hwmgr *hwmgr)
  405. {
  406. int result;
  407. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  408. DpmClocks_t *table = &(rv_data->clock_table);
  409. struct rv_clock_voltage_information *pinfo = &(rv_data->clock_vol_info);
  410. result = rv_copy_table_from_smc(hwmgr->smumgr, (uint8_t *)table, CLOCKTABLE);
  411. PP_ASSERT_WITH_CODE((0 == result),
  412. "Attempt to copy clock table from smc failed",
  413. return result);
  414. if (0 == result && table->DcefClocks[0].Freq != 0) {
  415. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dcefclk,
  416. NUM_DCEFCLK_DPM_LEVELS,
  417. &rv_data->clock_table.DcefClocks[0]);
  418. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_socclk,
  419. NUM_SOCCLK_DPM_LEVELS,
  420. &rv_data->clock_table.SocClocks[0]);
  421. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_fclk,
  422. NUM_FCLK_DPM_LEVELS,
  423. &rv_data->clock_table.FClocks[0]);
  424. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_mclk,
  425. NUM_MEMCLK_DPM_LEVELS,
  426. &rv_data->clock_table.MemClocks[0]);
  427. } else {
  428. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dcefclk,
  429. ARRAY_SIZE(VddDcfClk),
  430. &VddDcfClk[0]);
  431. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_socclk,
  432. ARRAY_SIZE(VddSocClk),
  433. &VddSocClk[0]);
  434. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_fclk,
  435. ARRAY_SIZE(VddFClk),
  436. &VddFClk[0]);
  437. }
  438. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dispclk,
  439. ARRAY_SIZE(VddDispClk),
  440. &VddDispClk[0]);
  441. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_dppclk,
  442. ARRAY_SIZE(VddDppClk), &VddDppClk[0]);
  443. rv_get_clock_voltage_dependency_table(hwmgr, &pinfo->vdd_dep_on_phyclk,
  444. ARRAY_SIZE(VddPhyClk), &VddPhyClk[0]);
  445. return 0;
  446. }
  447. static int rv_hwmgr_backend_init(struct pp_hwmgr *hwmgr)
  448. {
  449. int result = 0;
  450. struct rv_hwmgr *data;
  451. data = kzalloc(sizeof(struct rv_hwmgr), GFP_KERNEL);
  452. if (data == NULL)
  453. return -ENOMEM;
  454. hwmgr->backend = data;
  455. result = rv_initialize_dpm_defaults(hwmgr);
  456. if (result != 0) {
  457. pr_err("rv_initialize_dpm_defaults failed\n");
  458. return result;
  459. }
  460. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  461. PHM_PlatformCaps_PowerPlaySupport);
  462. rv_populate_clock_table(hwmgr);
  463. result = rv_get_system_info_data(hwmgr);
  464. if (result != 0) {
  465. pr_err("rv_get_system_info_data failed\n");
  466. return result;
  467. }
  468. rv_construct_boot_state(hwmgr);
  469. result = phm_construct_table(hwmgr, &rv_setup_asic_master,
  470. &(hwmgr->setup_asic));
  471. if (result != 0) {
  472. pr_err("Fail to construct setup ASIC\n");
  473. return result;
  474. }
  475. result = phm_construct_table(hwmgr, &rv_power_down_asic_master,
  476. &(hwmgr->power_down_asic));
  477. if (result != 0) {
  478. pr_err("Fail to construct power down ASIC\n");
  479. return result;
  480. }
  481. result = phm_construct_table(hwmgr, &rv_set_power_state_master,
  482. &(hwmgr->set_power_state));
  483. if (result != 0) {
  484. pr_err("Fail to construct set_power_state\n");
  485. return result;
  486. }
  487. result = phm_construct_table(hwmgr, &rv_disable_dpm_master,
  488. &(hwmgr->disable_dynamic_state_management));
  489. if (result != 0) {
  490. pr_err("Fail to disable_dynamic_state\n");
  491. return result;
  492. }
  493. result = phm_construct_table(hwmgr, &rv_enable_dpm_master,
  494. &(hwmgr->enable_dynamic_state_management));
  495. if (result != 0) {
  496. pr_err("Fail to enable_dynamic_state\n");
  497. return result;
  498. }
  499. hwmgr->platform_descriptor.hardwareActivityPerformanceLevels =
  500. RAVEN_MAX_HARDWARE_POWERLEVELS;
  501. hwmgr->platform_descriptor.hardwarePerformanceLevels =
  502. RAVEN_MAX_HARDWARE_POWERLEVELS;
  503. hwmgr->platform_descriptor.vbiosInterruptId = 0;
  504. hwmgr->platform_descriptor.clockStep.engineClock = 500;
  505. hwmgr->platform_descriptor.clockStep.memoryClock = 500;
  506. hwmgr->platform_descriptor.minimumClocksReductionPercentage = 50;
  507. rv_init_vq_budget_table(hwmgr);
  508. return result;
  509. }
  510. static int rv_hwmgr_backend_fini(struct pp_hwmgr *hwmgr)
  511. {
  512. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  513. struct rv_clock_voltage_information *pinfo = &(rv_data->clock_vol_info);
  514. phm_destroy_table(hwmgr, &(hwmgr->set_power_state));
  515. phm_destroy_table(hwmgr, &(hwmgr->enable_dynamic_state_management));
  516. phm_destroy_table(hwmgr, &(hwmgr->disable_dynamic_state_management));
  517. phm_destroy_table(hwmgr, &(hwmgr->power_down_asic));
  518. phm_destroy_table(hwmgr, &(hwmgr->setup_asic));
  519. if (pinfo->vdd_dep_on_dcefclk) {
  520. kfree(pinfo->vdd_dep_on_dcefclk);
  521. pinfo->vdd_dep_on_dcefclk = NULL;
  522. }
  523. if (pinfo->vdd_dep_on_socclk) {
  524. kfree(pinfo->vdd_dep_on_socclk);
  525. pinfo->vdd_dep_on_socclk = NULL;
  526. }
  527. if (pinfo->vdd_dep_on_fclk) {
  528. kfree(pinfo->vdd_dep_on_fclk);
  529. pinfo->vdd_dep_on_fclk = NULL;
  530. }
  531. if (pinfo->vdd_dep_on_dispclk) {
  532. kfree(pinfo->vdd_dep_on_dispclk);
  533. pinfo->vdd_dep_on_dispclk = NULL;
  534. }
  535. if (pinfo->vdd_dep_on_dppclk) {
  536. kfree(pinfo->vdd_dep_on_dppclk);
  537. pinfo->vdd_dep_on_dppclk = NULL;
  538. }
  539. if (pinfo->vdd_dep_on_phyclk) {
  540. kfree(pinfo->vdd_dep_on_phyclk);
  541. pinfo->vdd_dep_on_phyclk = NULL;
  542. }
  543. if (NULL != hwmgr->dyn_state.vddc_dep_on_dal_pwrl) {
  544. kfree(hwmgr->dyn_state.vddc_dep_on_dal_pwrl);
  545. hwmgr->dyn_state.vddc_dep_on_dal_pwrl = NULL;
  546. }
  547. if (NULL != hwmgr->dyn_state.vq_budgeting_table) {
  548. kfree(hwmgr->dyn_state.vq_budgeting_table);
  549. hwmgr->dyn_state.vq_budgeting_table = NULL;
  550. }
  551. kfree(hwmgr->backend);
  552. hwmgr->backend = NULL;
  553. return 0;
  554. }
  555. static int rv_dpm_force_dpm_level(struct pp_hwmgr *hwmgr,
  556. enum amd_dpm_forced_level level)
  557. {
  558. return 0;
  559. }
  560. static int rv_dpm_get_mclk(struct pp_hwmgr *hwmgr, bool low)
  561. {
  562. return 0;
  563. }
  564. static int rv_dpm_get_sclk(struct pp_hwmgr *hwmgr, bool low)
  565. {
  566. return 0;
  567. }
  568. static int rv_dpm_patch_boot_state(struct pp_hwmgr *hwmgr,
  569. struct pp_hw_power_state *hw_ps)
  570. {
  571. return 0;
  572. }
  573. static int rv_dpm_get_pp_table_entry_callback(
  574. struct pp_hwmgr *hwmgr,
  575. struct pp_hw_power_state *hw_ps,
  576. unsigned int index,
  577. const void *clock_info)
  578. {
  579. struct rv_power_state *rv_ps = cast_rv_ps(hw_ps);
  580. const ATOM_PPLIB_CZ_CLOCK_INFO *rv_clock_info = clock_info;
  581. struct phm_clock_voltage_dependency_table *table =
  582. hwmgr->dyn_state.vddc_dependency_on_sclk;
  583. uint8_t clock_info_index = rv_clock_info->index;
  584. if (clock_info_index > (uint8_t)(hwmgr->platform_descriptor.hardwareActivityPerformanceLevels - 1))
  585. clock_info_index = (uint8_t)(hwmgr->platform_descriptor.hardwareActivityPerformanceLevels - 1);
  586. rv_ps->levels[index].engine_clock = table->entries[clock_info_index].clk;
  587. rv_ps->levels[index].vddc_index = (uint8_t)table->entries[clock_info_index].v;
  588. rv_ps->level = index + 1;
  589. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep)) {
  590. rv_ps->levels[index].ds_divider_index = 5;
  591. rv_ps->levels[index].ss_divider_index = 5;
  592. }
  593. return 0;
  594. }
  595. static int rv_dpm_get_num_of_pp_table_entries(struct pp_hwmgr *hwmgr)
  596. {
  597. int result;
  598. unsigned long ret = 0;
  599. result = pp_tables_get_num_of_entries(hwmgr, &ret);
  600. return result ? 0 : ret;
  601. }
  602. static int rv_dpm_get_pp_table_entry(struct pp_hwmgr *hwmgr,
  603. unsigned long entry, struct pp_power_state *ps)
  604. {
  605. int result;
  606. struct rv_power_state *rv_ps;
  607. ps->hardware.magic = PhwRaven_Magic;
  608. rv_ps = cast_rv_ps(&(ps->hardware));
  609. result = pp_tables_get_entry(hwmgr, entry, ps,
  610. rv_dpm_get_pp_table_entry_callback);
  611. rv_ps->uvd_clocks.vclk = ps->uvd_clocks.VCLK;
  612. rv_ps->uvd_clocks.dclk = ps->uvd_clocks.DCLK;
  613. return result;
  614. }
  615. static int rv_get_power_state_size(struct pp_hwmgr *hwmgr)
  616. {
  617. return sizeof(struct rv_power_state);
  618. }
  619. static int rv_set_cpu_power_state(struct pp_hwmgr *hwmgr)
  620. {
  621. return 0;
  622. }
  623. static int rv_store_cc6_data(struct pp_hwmgr *hwmgr, uint32_t separation_time,
  624. bool cc6_disable, bool pstate_disable, bool pstate_switch_disable)
  625. {
  626. return 0;
  627. }
  628. static int rv_get_dal_power_level(struct pp_hwmgr *hwmgr,
  629. struct amd_pp_simple_clock_info *info)
  630. {
  631. return -EINVAL;
  632. }
  633. static int rv_force_clock_level(struct pp_hwmgr *hwmgr,
  634. enum pp_clock_type type, uint32_t mask)
  635. {
  636. return 0;
  637. }
  638. static int rv_print_clock_levels(struct pp_hwmgr *hwmgr,
  639. enum pp_clock_type type, char *buf)
  640. {
  641. return 0;
  642. }
  643. static int rv_get_performance_level(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *state,
  644. PHM_PerformanceLevelDesignation designation, uint32_t index,
  645. PHM_PerformanceLevel *level)
  646. {
  647. const struct rv_power_state *ps;
  648. struct rv_hwmgr *data;
  649. uint32_t level_index;
  650. uint32_t i;
  651. uint32_t vol_dep_record_index = 0;
  652. if (level == NULL || hwmgr == NULL || state == NULL)
  653. return -EINVAL;
  654. data = (struct rv_hwmgr *)(hwmgr->backend);
  655. ps = cast_const_rv_ps(state);
  656. level_index = index > ps->level - 1 ? ps->level - 1 : index;
  657. level->coreClock = ps->levels[level_index].engine_clock;
  658. if (designation == PHM_PerformanceLevelDesignation_PowerContainment) {
  659. for (i = 1; i < ps->level; i++) {
  660. if (ps->levels[i].engine_clock > data->dce_slow_sclk_threshold) {
  661. level->coreClock = ps->levels[i].engine_clock;
  662. break;
  663. }
  664. }
  665. }
  666. if (level_index == 0) {
  667. vol_dep_record_index = data->clock_vol_info.vdd_dep_on_fclk->count - 1;
  668. level->memory_clock =
  669. data->clock_vol_info.vdd_dep_on_fclk->entries[vol_dep_record_index].clk;
  670. } else
  671. level->memory_clock = data->clock_vol_info.vdd_dep_on_fclk->entries[0].clk;
  672. level->nonLocalMemoryFreq = 0;
  673. level->nonLocalMemoryWidth = 0;
  674. return 0;
  675. }
  676. static int rv_get_current_shallow_sleep_clocks(struct pp_hwmgr *hwmgr,
  677. const struct pp_hw_power_state *state, struct pp_clock_info *clock_info)
  678. {
  679. const struct rv_power_state *ps = cast_const_rv_ps(state);
  680. clock_info->min_eng_clk = ps->levels[0].engine_clock / (1 << (ps->levels[0].ss_divider_index));
  681. clock_info->max_eng_clk = ps->levels[ps->level - 1].engine_clock / (1 << (ps->levels[ps->level - 1].ss_divider_index));
  682. return 0;
  683. }
  684. #define MEM_FREQ_LOW_LATENCY 25000
  685. #define MEM_FREQ_HIGH_LATENCY 80000
  686. #define MEM_LATENCY_HIGH 245
  687. #define MEM_LATENCY_LOW 35
  688. #define MEM_LATENCY_ERR 0xFFFF
  689. static uint32_t rv_get_mem_latency(struct pp_hwmgr *hwmgr,
  690. uint32_t clock)
  691. {
  692. if (clock >= MEM_FREQ_LOW_LATENCY &&
  693. clock < MEM_FREQ_HIGH_LATENCY)
  694. return MEM_LATENCY_HIGH;
  695. else if (clock >= MEM_FREQ_HIGH_LATENCY)
  696. return MEM_LATENCY_LOW;
  697. else
  698. return MEM_LATENCY_ERR;
  699. }
  700. static int rv_get_clock_by_type_with_latency(struct pp_hwmgr *hwmgr,
  701. enum amd_pp_clock_type type,
  702. struct pp_clock_levels_with_latency *clocks)
  703. {
  704. uint32_t i;
  705. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  706. struct rv_clock_voltage_information *pinfo = &(rv_data->clock_vol_info);
  707. struct rv_voltage_dependency_table *pclk_vol_table;
  708. bool latency_required = false;
  709. if (pinfo == NULL)
  710. return -EINVAL;
  711. switch (type) {
  712. case amd_pp_mem_clock:
  713. pclk_vol_table = pinfo->vdd_dep_on_mclk;
  714. latency_required = true;
  715. break;
  716. case amd_pp_f_clock:
  717. pclk_vol_table = pinfo->vdd_dep_on_fclk;
  718. latency_required = true;
  719. break;
  720. case amd_pp_dcf_clock:
  721. pclk_vol_table = pinfo->vdd_dep_on_dcefclk;
  722. break;
  723. case amd_pp_disp_clock:
  724. pclk_vol_table = pinfo->vdd_dep_on_dispclk;
  725. break;
  726. case amd_pp_phy_clock:
  727. pclk_vol_table = pinfo->vdd_dep_on_phyclk;
  728. break;
  729. case amd_pp_dpp_clock:
  730. pclk_vol_table = pinfo->vdd_dep_on_dppclk;
  731. default:
  732. return -EINVAL;
  733. }
  734. if (pclk_vol_table == NULL || pclk_vol_table->count == 0)
  735. return -EINVAL;
  736. clocks->num_levels = 0;
  737. for (i = 0; i < pclk_vol_table->count; i++) {
  738. clocks->data[i].clocks_in_khz = pclk_vol_table->entries[i].clk;
  739. clocks->data[i].latency_in_us = latency_required ?
  740. rv_get_mem_latency(hwmgr,
  741. pclk_vol_table->entries[i].clk) :
  742. 0;
  743. clocks->num_levels++;
  744. }
  745. return 0;
  746. }
  747. static int rv_get_clock_by_type_with_voltage(struct pp_hwmgr *hwmgr,
  748. enum amd_pp_clock_type type,
  749. struct pp_clock_levels_with_voltage *clocks)
  750. {
  751. uint32_t i;
  752. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  753. struct rv_clock_voltage_information *pinfo = &(rv_data->clock_vol_info);
  754. struct rv_voltage_dependency_table *pclk_vol_table = NULL;
  755. if (pinfo == NULL)
  756. return -EINVAL;
  757. switch (type) {
  758. case amd_pp_mem_clock:
  759. pclk_vol_table = pinfo->vdd_dep_on_mclk;
  760. break;
  761. case amd_pp_f_clock:
  762. pclk_vol_table = pinfo->vdd_dep_on_fclk;
  763. break;
  764. case amd_pp_dcf_clock:
  765. pclk_vol_table = pinfo->vdd_dep_on_dcefclk;
  766. break;
  767. case amd_pp_soc_clock:
  768. pclk_vol_table = pinfo->vdd_dep_on_socclk;
  769. break;
  770. default:
  771. return -EINVAL;
  772. }
  773. if (pclk_vol_table == NULL || pclk_vol_table->count == 0)
  774. return -EINVAL;
  775. clocks->num_levels = 0;
  776. for (i = 0; i < pclk_vol_table->count; i++) {
  777. clocks->data[i].clocks_in_khz = pclk_vol_table->entries[i].clk;
  778. clocks->data[i].voltage_in_mv = pclk_vol_table->entries[i].vol;
  779. clocks->num_levels++;
  780. }
  781. return 0;
  782. }
  783. int rv_display_clock_voltage_request(struct pp_hwmgr *hwmgr,
  784. struct pp_display_clock_request *clock_req)
  785. {
  786. int result = 0;
  787. struct rv_hwmgr *rv_data = (struct rv_hwmgr *)(hwmgr->backend);
  788. enum amd_pp_clock_type clk_type = clock_req->clock_type;
  789. uint32_t clk_freq = clock_req->clock_freq_in_khz / 1000;
  790. PPSMC_Msg msg;
  791. switch (clk_type) {
  792. case amd_pp_dcf_clock:
  793. if (clk_freq == rv_data->dcf_actual_hard_min_freq)
  794. return 0;
  795. msg = PPSMC_MSG_SetHardMinDcefclkByFreq;
  796. rv_data->dcf_actual_hard_min_freq = clk_freq;
  797. break;
  798. case amd_pp_soc_clock:
  799. msg = PPSMC_MSG_SetHardMinSocclkByFreq;
  800. break;
  801. case amd_pp_f_clock:
  802. if (clk_freq == rv_data->f_actual_hard_min_freq)
  803. return 0;
  804. rv_data->f_actual_hard_min_freq = clk_freq;
  805. msg = PPSMC_MSG_SetHardMinFclkByFreq;
  806. break;
  807. default:
  808. pr_info("[DisplayClockVoltageRequest]Invalid Clock Type!");
  809. return -EINVAL;
  810. }
  811. result = smum_send_msg_to_smc_with_parameter(hwmgr->smumgr, msg,
  812. clk_freq);
  813. return result;
  814. }
  815. static int rv_get_max_high_clocks(struct pp_hwmgr *hwmgr, struct amd_pp_simple_clock_info *clocks)
  816. {
  817. return -EINVAL;
  818. }
  819. static int rv_thermal_get_temperature(struct pp_hwmgr *hwmgr)
  820. {
  821. uint32_t reg_offset = soc15_get_register_offset(THM_HWID, 0,
  822. mmTHM_TCON_CUR_TMP_BASE_IDX, mmTHM_TCON_CUR_TMP);
  823. uint32_t reg_value = cgs_read_register(hwmgr->device, reg_offset);
  824. int cur_temp =
  825. (reg_value & THM_TCON_CUR_TMP__CUR_TEMP_MASK) >> THM_TCON_CUR_TMP__CUR_TEMP__SHIFT;
  826. if (cur_temp & THM_TCON_CUR_TMP__CUR_TEMP_RANGE_SEL_MASK)
  827. cur_temp = ((cur_temp / 8) - 49) * PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
  828. else
  829. cur_temp = (cur_temp / 8) * PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
  830. return cur_temp;
  831. }
  832. static int rv_read_sensor(struct pp_hwmgr *hwmgr, int idx,
  833. void *value, int *size)
  834. {
  835. switch (idx) {
  836. case AMDGPU_PP_SENSOR_GPU_TEMP:
  837. *((uint32_t *)value) = rv_thermal_get_temperature(hwmgr);
  838. return 0;
  839. default:
  840. return -EINVAL;
  841. }
  842. }
  843. static const struct pp_hwmgr_func rv_hwmgr_funcs = {
  844. .backend_init = rv_hwmgr_backend_init,
  845. .backend_fini = rv_hwmgr_backend_fini,
  846. .asic_setup = NULL,
  847. .apply_state_adjust_rules = rv_apply_state_adjust_rules,
  848. .force_dpm_level = rv_dpm_force_dpm_level,
  849. .get_power_state_size = rv_get_power_state_size,
  850. .powerdown_uvd = NULL,
  851. .powergate_uvd = NULL,
  852. .powergate_vce = NULL,
  853. .get_mclk = rv_dpm_get_mclk,
  854. .get_sclk = rv_dpm_get_sclk,
  855. .patch_boot_state = rv_dpm_patch_boot_state,
  856. .get_pp_table_entry = rv_dpm_get_pp_table_entry,
  857. .get_num_of_pp_table_entries = rv_dpm_get_num_of_pp_table_entries,
  858. .set_cpu_power_state = rv_set_cpu_power_state,
  859. .store_cc6_data = rv_store_cc6_data,
  860. .force_clock_level = rv_force_clock_level,
  861. .print_clock_levels = rv_print_clock_levels,
  862. .get_dal_power_level = rv_get_dal_power_level,
  863. .get_performance_level = rv_get_performance_level,
  864. .get_current_shallow_sleep_clocks = rv_get_current_shallow_sleep_clocks,
  865. .get_clock_by_type_with_latency = rv_get_clock_by_type_with_latency,
  866. .get_clock_by_type_with_voltage = rv_get_clock_by_type_with_voltage,
  867. .get_max_high_clocks = rv_get_max_high_clocks,
  868. .read_sensor = rv_read_sensor,
  869. };
  870. int rv_init_function_pointers(struct pp_hwmgr *hwmgr)
  871. {
  872. hwmgr->hwmgr_func = &rv_hwmgr_funcs;
  873. hwmgr->pptable_func = &pptable_funcs;
  874. return 0;
  875. }